// File: /workspace/github_c_makefile_star2_O3/Carton32[P]quectel-CM/mbim-cm.c
// Function: mbim_read_thread
static void * mbim_read_thread(void *param) {
    PROFILE_T *profile = (PROFILE_T *)param;
    const char *cdc_wdm = (const char *)profile->qmichannel;
    int wait_for_request_quit = 0;

    mbim_verbose = debug_qmi;
    s_tid_reader = pthread_self();

    if (profile->qmap_mode > 1 && profile->qmapnet_adapter[0]) {
        if (!profile->proxy[0])
             sprintf(profile->proxy, "%s", QUECTEL_MBIM_PROXY);
        mbim_sessionID = profile->pdp;
    }

    if (profile->proxy[0]) {
        mbim_fd = cm_open_proxy(profile->proxy);
    } 
    else {
        mbim_fd = cm_open_dev(cdc_wdm);
    }

    if (mbim_fd <= 0) {
        mbim_debug("fail to open (%s), errno: %d (%s)", cdc_wdm, errno, strerror(errno));
        goto __quit;
    }

    dbg_time("cdc_wdm_fd = %d", mbim_fd);

    qmidevice_send_event_to_main(RIL_INDICATE_DEVICE_CONNECTED);

    while (mbim_fd > 0) {
        struct pollfd pollfds[] = {{mbim_fd, POLLIN, 0}, {qmidevice_control_fd[1], POLLIN, 0}, {qmi_over_mbim_sk[1], POLLIN, 0}};
        int ne, ret, nevents = 2;

        if (pollfds[nevents].fd != -1)
            nevents++;

        ret = poll(pollfds, nevents, wait_for_request_quit ? 1000 : -1);

        if (ret == 0 && wait_for_request_quit) {
            break;
        }

        if (ret < 0) {
            mbim_debug("%s poll=%d, errno: %d (%s)", __func__, ret, errno, strerror(errno));
            break;
        }

        for (ne = 0; ne < nevents; ne++) {
            int fd = pollfds[ne].fd;
            short revents = pollfds[ne].revents;

            if (revents & (POLLERR | POLLHUP | POLLNVAL)) {
                mbim_debug("%s poll err/hup/inval", __func__);
                mbim_debug("epoll fd = %d, events = 0x%04x", fd, revents);
                if (revents & (POLLERR | POLLHUP | POLLNVAL))
                goto __quit;
            }

            if ((revents & POLLIN) == 0)
                continue;

            if (mbim_fd == fd) {
                ssize_t nreads;
                MBIM_MESSAGE_HEADER *pResponse = (MBIM_MESSAGE_HEADER *) cm_recv_buf;

                if (profile->proxy[0])
                    nreads = mbim_proxy_read(fd, pResponse, sizeof(cm_recv_buf));
                else
                    nreads = read(fd, pResponse, sizeof(cm_recv_buf));

                if (nreads <= 0) {
                    mbim_debug("%s read=%d errno: %d (%s)",  __func__, (int)nreads, errno, strerror(errno));
                    break;
                }

                mbim_recv_command(pResponse, nreads);
            }
            else if (fd == qmidevice_control_fd[1]) {
                int triger_event;
                if (read(fd, &triger_event, sizeof(triger_event)) == sizeof(triger_event)) {
                    //mbim_debug("triger_event = 0x%x", triger_event);
                    switch (triger_event) {
                        case RIL_REQUEST_QUIT:
                            goto __quit;
                        break;
                        case SIG_EVENT_STOP:
                            wait_for_request_quit = 1;
                        break;
                        default:
                        break;
                    }
                }
            }
            else if (fd == qmi_over_mbim_sk[1]) {
                    ssize_t nreads = read(fd, cm_recv_buf, sizeof(cm_recv_buf));
                    if (nreads > 0)
                        QmiThreadRecvQMI((PQCQMIMSG)cm_recv_buf);
            }
        }
    }

__quit:
    if (mbim_fd != -1) { close(mbim_fd); mbim_fd = -1; }
    mbim_recv_command(NULL, 0);
    qmidevice_send_event_to_main(RIL_INDICATE_DEVICE_DISCONNECTED);
    mbim_debug("%s exit", __func__);
    s_tid_reader = 0;

    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r15
leaq 0x17f65(%rip), %rax     # 0x2a3d0
movl (%rax), %eax
movl %eax, 0x198bd(%rip)     # 0x2bd30
callq 0x5400
movq %rax, 0x198c9(%rip)     # 0x2bd48
cmpl $0x2, 0x10a(%r15)
jl 0x124b9
cmpb $0x0, 0xca(%r15)
je 0x124b9
cmpb $0x0, 0x28(%r15)
jne 0x124af
leaq 0x28(%r15), %rax
movups 0xc078(%rip), %xmm0     # 0x1e51d
movups %xmm0, (%rax)
movl $0x79786f, 0xf(%rax)    # imm = 0x79786F
movl 0x48(%r15), %eax
movl %eax, 0x19a5b(%rip)     # 0x2bf14
leaq 0x8a(%r15), %r14
leaq 0x28(%r15), %rbx
cmpb $0x0, 0x28(%r15)
je 0x124d5
movq %rbx, %rdi
callq 0xeae1
jmp 0x124dd
movq %r14, %rdi
callq 0xe8d8
movl %eax, %ebp
movl %eax, 0x17e27(%rip)     # 0x2a30c
movq 0x17ad4(%rip), %rax     # 0x29fc0
movq (%rax), %r15
callq 0xea3b
movq %rax, %r12
testl %ebp, %ebp
jle 0x127ef
movl 0x17e07(%rip), %ecx     # 0x2a30c
leaq 0xbc55(%rip), %rsi      # 0x1e161
movq %r15, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0x18788(%rip), %rax     # 0x2aca8
movq (%rax), %r14
testq %r14, %r14
je 0x12547
callq 0xea3b
movl 0x17dd9(%rip), %ecx     # 0x2a30c
leaq 0xbc27(%rip), %rsi      # 0x1e161
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
movl $0x1002, %edi           # imm = 0x1002
callq 0x7354
movl 0x17db5(%rip), %edi     # 0x2a30c
testl %edi, %edi
jle 0x12912
movq %rbx, 0x30(%rsp)
movl $0x1, %ecx
leaq 0x17e68(%rip), %rax     # 0x2a3d8
leaq 0x10(%rsp), %rsi
leaq 0xfe67(%rip), %rdx      # 0x223e3
xorl %r8d, %r8d
movq %rdx, %r15
movl %edi, 0x10(%rsp)
movl %ecx, 0x14(%rsp)
movl 0x4(%rax), %eax
movl %eax, 0x18(%rsp)
movl %ecx, 0x1c(%rsp)
movl 0x17d69(%rip), %eax     # 0x2a304
movl %eax, 0x20(%rsp)
movl %ecx, 0x24(%rsp)
xorl %r12d, %r12d
cmpl $-0x1, %eax
sete %r12b
xorq $0x3, %r12
xorl %edx, %edx
movl %r8d, %ebp
testl %r8d, %r8d
setne %bl
sete %dl
negl %edx
orl $0x3e8, %edx            # imm = 0x3E8
movq %rsi, %rdi
movq %r12, %rsi
callq 0x53c0
movl %eax, %r14d
testl %eax, %eax
sete %al
testb %al, %bl
jne 0x1290c
testl %r14d, %r14d
js 0x129c8
xorl %ebx, %ebx
movq %r15, %rdx
movl 0x10(%rsp,%rbx,8), %r13d
movswl 0x16(%rsp,%rbx,8), %r14d
testb $0x38, %r14b
jne 0x12863
testb $0x1, %r14b
je 0x12723
cmpl %r13d, 0x17cf6(%rip)    # 0x2a30c
jne 0x1267a
movq 0x30(%rsp), %rax
cmpb $0x0, (%rax)
je 0x126ef
movl $0xc, %edx
movl %r13d, %edi
leaq 0x186fb(%rip), %rsi     # 0x2ad30
callq 0x5250
movq %rax, %r14
cmpq $0xc, %rax
jne 0x12706
leaq 0x186e2(%rip), %rdi     # 0x2ad30
movl 0x4(%rdi), %edx
cmpq $0x1000, %rdx           # imm = 0x1000
ja 0x12712
addq $-0xc, %rdx
leaq 0xc(%rdi), %rsi
movl %r13d, %edi
callq 0x5250
movq %rax, %r14
addq $0xc, %r14
jmp 0x12706
leaq 0x17d57(%rip), %rax     # 0x2a3d8
cmpl 0x4(%rax), %r13d
jne 0x126c0
movl $0x4, %edx
movl %r13d, %edi
leaq 0xc(%rsp), %rsi
callq 0x5250
cmpq $0x4, %rax
jne 0x1271c
movl $0x1, %eax
movl 0xc(%rsp), %ecx
cmpl $0x2, %ecx
movq %r15, %rdx
je 0x12721
cmpl $0x1000, %ecx           # imm = 0x1000
je 0x1290c
movl %ebp, %eax
jmp 0x12721
cmpl 0x17c3d(%rip), %r13d    # 0x2a304
jne 0x12723
movl $0x1000, %edx           # imm = 0x1000
movl %r13d, %edi
leaq 0x18658(%rip), %r14     # 0x2ad30
movq %r14, %rsi
callq 0x5250
testq %rax, %rax
jle 0x12717
movq %r14, %rdi
callq 0xa3ae
jmp 0x12717
movl $0x1000, %edx           # imm = 0x1000
movl %r13d, %edi
leaq 0x18632(%rip), %rsi     # 0x2ad30
callq 0x5250
movq %rax, %r14
testq %r14, %r14
leaq 0x18620(%rip), %rdi     # 0x2ad30
jle 0x12756
callq 0x15edb
movq %r15, %rdx
jmp 0x12723
movl %ebp, %eax
movq %r15, %rdx
movl %eax, %ebp
incq %rbx
cmpq %rbx, %r12
jne 0x125f0
movl 0x17bd7(%rip), %edi     # 0x2a30c
testl %edi, %edi
movl $0x1, %ecx
leaq 0x17c95(%rip), %rax     # 0x2a3d8
leaq 0x10(%rsp), %rsi
movl %ebp, %r8d
jg 0x1257f
jmp 0x12912
movq 0x17863(%rip), %rax     # 0x29fc0
movq (%rax), %rbx
callq 0xea3b
movq %rax, 0x28(%rsp)
callq 0x5070
movq %rax, %r12
movl (%rax), %r13d
movl %r13d, %edi
callq 0x5530
movq %rax, (%rsp)
movq %rbx, %rdi
leaq 0xba4d(%rip), %rsi      # 0x1e1d8
movq 0x28(%rsp), %rdx
movq %r15, %rbx
movq %r15, %rcx
movl %r14d, %r8d
movl %r13d, %r9d
xorl %eax, %eax
callq 0x52c0
leaq 0x184fe(%rip), %rax     # 0x2aca8
movq (%rax), %r13
testq %r13, %r13
je 0x127e7
callq 0xea3b
movq %rax, %r15
movl (%r12), %r12d
movl %r12d, %edi
callq 0x5530
movq %rax, (%rsp)
movq %r13, %rdi
leaq 0xba04(%rip), %rsi      # 0x1e1d8
movq %r15, %rdx
movq %rbx, %rcx
movl %r14d, %r8d
movl %r12d, %r9d
xorl %eax, %eax
callq 0x52c0
movq %rbx, %rdx
jmp 0x1272f
callq 0x5070
movq %rax, %rbx
movl (%rax), %ebp
movl %ebp, %edi
callq 0x5530
leaq 0xfbb4(%rip), %rsi      # 0x223bb
movq %r15, %rdi
movq %r12, %rdx
movq %r14, %rcx
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
leaq 0x18484(%rip), %rax     # 0x2aca8
movq (%rax), %r15
testq %r15, %r15
je 0x1290c
callq 0xea3b
movq %rax, %r12
movl (%rbx), %ebx
movl %ebx, %edi
callq 0x5530
leaq 0xfb73(%rip), %rsi      # 0x223bb
movq %r15, %rdi
movq %r12, %rdx
movq %r14, %rcx
movl %ebx, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
jmp 0x1290c
movq 0x17756(%rip), %rax     # 0x29fc0
movq (%rax), %rbx
callq 0xea3b
leaq 0xb91f(%rip), %rsi      # 0x1e198
leaq 0xfb63(%rip), %rcx      # 0x223e3
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0x18414(%rip), %r15     # 0x2aca8
movq (%r15), %rbx
testq %rbx, %rbx
je 0x128bc
callq 0xea3b
leaq 0xb8f0(%rip), %rsi      # 0x1e198
leaq 0xfb34(%rip), %rcx      # 0x223e3
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
movq 0x176fd(%rip), %rax     # 0x29fc0
movq (%rax), %rbx
callq 0xea3b
leaq 0xbac4(%rip), %rsi      # 0x1e396
movq %rbx, %rdi
movq %rax, %rdx
movl %r13d, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x52c0
movq (%r15), %rbx
testq %rbx, %rbx
je 0x1290c
callq 0xea3b
leaq 0xba9d(%rip), %rsi      # 0x1e396
movq %rbx, %rdi
movq %rax, %rdx
movl %r13d, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x52c0
movl 0x179fa(%rip), %edi     # 0x2a30c
cmpl $-0x1, %edi
je 0x12926
callq 0x5230
movl $0xffffffff, 0x179e6(%rip) # imm = 0xFFFFFFFF
leaq 0x19423(%rip), %rbx     # 0x2bd50
movq %rbx, %rdi
callq 0x5560
leaq 0x1944c(%rip), %rdi     # 0x2bd88
callq 0x5280
movq %rbx, %rdi
callq 0x5340
movl $0x1003, %edi           # imm = 0x1003
callq 0x7354
movq 0x17666(%rip), %rax     # 0x29fc0
movq (%rax), %rbx
callq 0xea3b
leaq 0xb8bf(%rip), %rsi      # 0x1e228
leaq 0xfa73(%rip), %rcx      # 0x223e3
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0x18324(%rip), %rax     # 0x2aca8
movq (%rax), %rbx
testq %rbx, %rbx
je 0x129ac
callq 0xea3b
leaq 0xb890(%rip), %rsi      # 0x1e228
leaq 0xfa44(%rip), %rcx      # 0x223e3
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
movq $0x0, 0x19391(%rip) # 0x2bd48
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x175f1(%rip), %rax     # 0x29fc0
movq (%rax), %r15
callq 0xea3b
movq %rax, %r12
callq 0x5070
movq %rax, %rbx
movl (%rax), %ebp
movl %ebp, %edi
callq 0x5530
movq %rax, (%rsp)
leaq 0xb781(%rip), %rsi      # 0x1e177
leaq 0xf9e6(%rip), %rcx      # 0x223e3
movq %r15, %rdi
movq %r12, %rdx
movl %r14d, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x52c0
leaq 0x18291(%rip), %rax     # 0x2aca8
movq (%rax), %r15
testq %r15, %r15
je 0x1290c
callq 0xea3b
movq %rax, %r12
movl (%rbx), %ebx
movl %ebx, %edi
callq 0x5530
movq %rax, (%rsp)
leaq 0xb738(%rip), %rsi      # 0x1e177
leaq 0xf99d(%rip), %rcx      # 0x223e3
movq %r15, %rdi
movq %r12, %rdx
movl %r14d, %r8d
movl %ebx, %r9d
xorl %eax, %eax
callq 0x52c0
jmp 0x1290c