// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movq %rdi, %r15
leaq 0x1782d(%rip), %rax     # 0x293d0
movl (%rax), %eax
movl %eax, 0x19185(%rip)     # 0x2ad30
callq 0x5410
movq %rax, 0x19191(%rip)     # 0x2ad48
cmpl $0x2, 0x10a(%r15)
jl 0x11bf1
cmpb $0x0, 0xca(%r15)
je 0x11bf1
cmpb $0x0, 0x28(%r15)
jne 0x11be7
leaq 0x28(%r15), %rax
movups 0xa940(%rip), %xmm0     # 0x1c51d
movups %xmm0, (%rax)
movl $0x79786f, 0xf(%rax)    # imm = 0x79786F
movl 0x48(%r15), %eax
movl %eax, 0x19323(%rip)     # 0x2af14
leaq 0x8a(%r15), %r14
leaq 0x28(%r15), %rdi
cmpb $0x0, 0x28(%r15)
movq %rdi, 0x40(%rsp)
je 0x11c0f
callq 0xe3d6
jmp 0x11c17
movq %r14, %rdi
callq 0xe1ec
movl %eax, %ebp
movl %eax, 0x176ed(%rip)     # 0x2930c
movq 0x17392(%rip), %rax     # 0x28fb8
movq (%rax), %r15
callq 0xe343
movq %rax, %r12
testl %ebp, %ebp
jle 0x11f86
movl 0x176cd(%rip), %ecx     # 0x2930c
leaq 0xa51b(%rip), %rsi      # 0x1c161
movq %r15, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0x1804e(%rip), %rax     # 0x29ca8
movq (%rax), %r14
testq %r14, %r14
je 0x11c81
callq 0xe343
movl 0x1769f(%rip), %ecx     # 0x2930c
leaq 0xa4ed(%rip), %rsi      # 0x1c161
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
movl $0x1002, %edi           # imm = 0x1002
callq 0x7244
leaq 0x10(%rsp), %rsi
leaq 0x18099(%rip), %r15     # 0x29d30
leaq 0xc(%r15), %rax
movq %rax, 0x38(%rsp)
xorl %ebp, %ebp
movl 0x17664(%rip), %edi     # 0x2930c
testl %edi, %edi
jle 0x11ff7
movl %edi, 0x10(%rsp)
pushq $0x1
popq %rcx
movl %ecx, 0x14(%rsp)
leaq 0x17716(%rip), %rax     # 0x293d8
movl 0x4(%rax), %eax
movl %eax, 0x18(%rsp)
movl %ecx, 0x1c(%rsp)
movl 0x17631(%rip), %eax     # 0x29304
movl %eax, 0x20(%rsp)
movl %ecx, 0x24(%rsp)
xorl %r12d, %r12d
cmpl $-0x1, %eax
sete %r12b
xorq $0x3, %r12
xorl %edx, %edx
testl %ebp, %ebp
setne %bl
sete %dl
negl %edx
orl $0x3e8, %edx            # imm = 0x3E8
movq %rsi, %rdi
movq %r12, %rsi
callq 0x53d0
movl %eax, %r14d
testl %eax, %eax
sete %al
testb %al, %bl
jne 0x11ff1
testl %r14d, %r14d
js 0x1208b
xorl %ebx, %ebx
cmpq %rbx, %r12
je 0x11ecd
movl 0x10(%rsp,%rbx,8), %r13d
movswl 0x16(%rsp,%rbx,8), %r14d
testb $0x38, %r14b
jne 0x11ed7
testb $0x1, %r14b
je 0x11e2f
cmpl %r13d, 0x175bc(%rip)    # 0x2930c
jne 0x11da6
movq 0x40(%rsp), %rax
cmpb $0x0, (%rax)
je 0x11e0f
movl %r13d, %edi
movq %r15, %rsi
pushq $0xc
popq %rdx
callq 0x5250
movq %rax, %r14
cmpq $0xc, %rax
jne 0x11e22
movl 0x4(%r15), %edx
cmpq $0x1000, %rdx           # imm = 0x1000
ja 0x11e27
addq $-0xc, %rdx
movl %r13d, %edi
movq 0x38(%rsp), %rsi
callq 0x5250
movq %rax, %r14
addq $0xc, %r14
jmp 0x11e22
leaq 0x1762b(%rip), %rax     # 0x293d8
cmpl 0x4(%rax), %r13d
jne 0x11de7
movl %r13d, %edi
leaq 0xc(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x5250
cmpq $0x4, %rax
jne 0x11de1
movl 0xc(%rsp), %ecx
pushq $0x1
popq %rax
cmpl $0x2, %ecx
je 0x11de3
cmpl $0x1000, %ecx           # imm = 0x1000
je 0x11ff1
movl %ebp, %eax
movl %eax, %ebp
jmp 0x11e2f
cmpl 0x17516(%rip), %r13d    # 0x29304
jne 0x11e2f
movl $0x1000, %edx           # imm = 0x1000
movl %r13d, %edi
movq %r15, %rsi
callq 0x5250
testq %rax, %rax
jle 0x11e2f
movq %r15, %rdi
callq 0x9fbb
jmp 0x11e2f
movl $0x1000, %edx           # imm = 0x1000
movl %r13d, %edi
movq %r15, %rsi
callq 0x5250
movq %rax, %r14
testq %r14, %r14
jle 0x11e37
movq %r15, %rdi
callq 0x14fc4
incq %rbx
jmp 0x11d21
movq 0x1717a(%rip), %rax     # 0x28fb8
movq (%rax), %rbx
callq 0xe343
movq %rax, 0x30(%rsp)
callq 0x5070
movq %rax, %r12
movl (%rax), %r13d
movl %r13d, %edi
callq 0x5540
movq %rax, (%rsp)
movq %rbx, %rdi
leaq 0xa36c(%rip), %rsi      # 0x1c1d8
movq 0x30(%rsp), %rdx
leaq 0xe580(%rip), %rcx      # 0x203f8
movl %r14d, %r8d
movl %r13d, %r9d
xorl %eax, %eax
callq 0x52c0
leaq 0x17e1c(%rip), %rax     # 0x29ca8
movq (%rax), %r13
testq %r13, %r13
je 0x11ecd
callq 0xe343
movq %rax, %rbx
movl (%r12), %r12d
movl %r12d, %edi
callq 0x5540
movq %rax, (%rsp)
movq %r13, %rdi
leaq 0xa322(%rip), %rsi      # 0x1c1d8
movq %rbx, %rdx
leaq 0xe538(%rip), %rcx      # 0x203f8
movl %r14d, %r8d
movl %r12d, %r9d
xorl %eax, %eax
callq 0x52c0
leaq 0x10(%rsp), %rsi
jmp 0x11ca2
movq 0x170da(%rip), %rax     # 0x28fb8
movq (%rax), %rbx
callq 0xe343
leaq 0xa2ab(%rip), %rsi      # 0x1c198
leaq 0xe504(%rip), %rcx      # 0x203f8
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0x17da0(%rip), %r15     # 0x29ca8
movq (%r15), %rbx
testq %rbx, %rbx
je 0x11f30
callq 0xe343
leaq 0xa27c(%rip), %rsi      # 0x1c198
leaq 0xe4d5(%rip), %rcx      # 0x203f8
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
movq 0x17081(%rip), %rax     # 0x28fb8
movq (%rax), %rbx
callq 0xe343
leaq 0xa450(%rip), %rsi      # 0x1c396
movq %rbx, %rdi
movq %rax, %rdx
movl %r13d, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x52c0
movq (%r15), %rbx
testq %rbx, %rbx
je 0x11ff1
callq 0xe343
leaq 0xa425(%rip), %rsi      # 0x1c396
movq %rbx, %rdi
movq %rax, %rdx
movl %r13d, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x52c0
jmp 0x11ff1
callq 0x5070
movq %rax, %rbx
movl (%rax), %ebp
movl %ebp, %edi
callq 0x5540
leaq 0xe432(%rip), %rsi      # 0x203d0
movq %r15, %rdi
movq %r12, %rdx
movq %r14, %rcx
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
leaq 0x17ced(%rip), %rax     # 0x29ca8
movq (%rax), %r15
testq %r15, %r15
je 0x11ff1
callq 0xe343
movq %rax, %r12
movl (%rbx), %ebx
movl %ebx, %edi
callq 0x5540
leaq 0xe3f5(%rip), %rsi      # 0x203d0
movq %r15, %rdi
movq %r12, %rdx
movq %r14, %rcx
movl %ebx, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
movl 0x17315(%rip), %edi     # 0x2930c
cmpl $-0x1, %edi
je 0x12008
callq 0x5230
orl $-0x1, 0x17304(%rip)    # 0x2930c
xorl %edi, %edi
callq 0x14fc4
movl $0x1003, %edi           # imm = 0x1003
callq 0x7244
movq 0x16f98(%rip), %rax     # 0x28fb8
movq (%rax), %rbx
callq 0xe343
leaq 0xa1f9(%rip), %rsi      # 0x1c228
leaq 0xe3c2(%rip), %rcx      # 0x203f8
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0x17c5e(%rip), %rax     # 0x29ca8
movq (%rax), %rbx
testq %rbx, %rbx
je 0x12072
callq 0xe343
leaq 0xa1ca(%rip), %rsi      # 0x1c228
leaq 0xe393(%rip), %rcx      # 0x203f8
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
andq $0x0, 0x18cce(%rip)     # 0x2ad48
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x16f26(%rip), %rax     # 0x28fb8
movq (%rax), %r15
callq 0xe343
movq %rax, %r12
callq 0x5070
movq %rax, %rbx
movl (%rax), %ebp
movl %ebp, %edi
callq 0x5540
movq %rax, (%rsp)
leaq 0xa0be(%rip), %rsi      # 0x1c177
leaq 0xe338(%rip), %rcx      # 0x203f8
movq %r15, %rdi
movq %r12, %rdx
movl %r14d, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x52c0
leaq 0x17bce(%rip), %rax     # 0x29ca8
movq (%rax), %r15
testq %r15, %r15
je 0x11ff1
callq 0xe343
movq %rax, %r12
movl (%rbx), %ebx
movl %ebx, %edi
callq 0x5540
movq %rax, (%rsp)
leaq 0xa075(%rip), %rsi      # 0x1c177
leaq 0xe2ef(%rip), %rcx      # 0x203f8
movq %r15, %rdi
movq %r12, %rdx
movl %r14d, %r8d
movl %ebx, %r9d
xorl %eax, %eax
callq 0x52c0
jmp 0x11ff1