// File: /workspace/github_c_makefile_star2_O1/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 0x17e0d(%rip), %rax     # 0x2a3d0
movl (%rax), %eax
movl %eax, 0x19765(%rip)     # 0x2bd30
callq 0x5400
movq %rax, 0x19771(%rip)     # 0x2bd48
cmpl $0x2, 0x10a(%r15)
jl 0x12611
cmpb $0x0, 0xca(%r15)
je 0x12611
cmpb $0x0, 0x28(%r15)
jne 0x12607
leaq 0x28(%r15), %rax
movups 0xbf22(%rip), %xmm0     # 0x1e51f
movups %xmm0, (%rax)
movl $0x79786f, 0xf(%rax)    # imm = 0x79786F
movl 0x48(%r15), %eax
movl %eax, 0x19903(%rip)     # 0x2bf14
leaq 0x8a(%r15), %r14
leaq 0x28(%r15), %rdi
cmpb $0x0, 0x28(%r15)
movq %rdi, 0x38(%rsp)
je 0x1262f
callq 0xebd5
jmp 0x12637
movq %r14, %rdi
callq 0xe9cc
movl %eax, %ebx
movl %eax, 0x17ccd(%rip)     # 0x2a30c
movq 0x1797a(%rip), %rax     # 0x29fc0
movq (%rax), %r15
callq 0xeb2f
movq %rax, %r12
testl %ebx, %ebx
jle 0x12ad9
movl 0x17cad(%rip), %ecx     # 0x2a30c
leaq 0xbafb(%rip), %rsi      # 0x1e161
movq %r15, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0x1862e(%rip), %rax     # 0x2aca8
movq (%rax), %rbx
testq %rbx, %rbx
je 0x126a1
callq 0xeb2f
movl 0x17c7f(%rip), %ecx     # 0x2a30c
leaq 0xbacd(%rip), %rsi      # 0x1e161
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
movl $0x1002, %edi           # imm = 0x1002
callq 0x7444
movl $0x0, 0x8(%rsp)
movl 0x17c53(%rip), %eax     # 0x2a30c
testl %eax, %eax
jle 0x12b44
movl %eax, 0x10(%rsp)
movl $0x1, %eax
movl %eax, 0x14(%rsp)
leaq 0x17d03(%rip), %rcx     # 0x2a3d8
movl 0x4(%rcx), %ecx
movl %ecx, 0x18(%rsp)
movl %eax, 0x1c(%rsp)
movl 0x17c1e(%rip), %ecx     # 0x2a304
movl %ecx, 0x20(%rsp)
movl %eax, 0x24(%rsp)
xorl %r13d, %r13d
cmpl $-0x1, %ecx
sete %r13b
xorl $0x3, %r13d
xorl %edx, %edx
movl 0x8(%rsp), %ebp
testl %ebp, %ebp
sete %dl
negl %edx
orl $0x3e8, %edx            # imm = 0x3E8
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x53c0
movl %eax, %r14d
movl $0x8, %ebx
testl %ebp, %ebp
je 0x12742
testl %r14d, %r14d
jne 0x12742
movl $0x1, 0x8(%rsp)
testl %ebx, %ebx
je 0x126b3
jmp 0x12aca
testl %r14d, %r14d
js 0x12a20
movl %r13d, %eax
movq %rax, 0x40(%rsp)
xorl %r15d, %r15d
movl 0x10(%rsp,%r15,8), %r14d
movswl 0x16(%rsp,%r15,8), %r13d
testb $0x38, %r13b
je 0x12826
movq 0x1784e(%rip), %rax     # 0x29fc0
movq (%rax), %rbx
callq 0xeb2f
movq %rbx, %rdi
leaq 0xba14(%rip), %r12      # 0x1e198
movq %r12, %rsi
movq %rax, %rdx
leaq 0xfc2a(%rip), %rbp      # 0x223bb
movq %rbp, %rcx
xorl %eax, %eax
callq 0x52c0
leaq 0x18506(%rip), %rax     # 0x2aca8
movq (%rax), %rbx
testq %rbx, %rbx
je 0x127c2
callq 0xeb2f
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x52c0
movq 0x177f7(%rip), %rax     # 0x29fc0
movq (%rax), %rbx
callq 0xeb2f
movq %rbx, %rdi
leaq 0xbbbb(%rip), %r12      # 0x1e396
movq %r12, %rsi
movq %rax, %rdx
movl %r14d, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x52c0
leaq 0x184b3(%rip), %rax     # 0x2aca8
movq (%rax), %rbx
movl $0x4, %ebp
testq %rbx, %rbx
je 0x129f1
callq 0xeb2f
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
movl %r14d, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x52c0
jmp 0x129f1
movl $0xd, %ebp
testb $0x1, %r13b
je 0x129f1
cmpl %r14d, 0x17ad0(%rip)    # 0x2a30c
jne 0x128a2
movq 0x38(%rsp), %rax
cmpb $0x0, (%rax)
leaq 0x184e3(%rip), %rbx     # 0x2ad30
je 0x1292c
movl $0xc, %edx
movl %r14d, %edi
movq %rbx, %rsi
callq 0x5250
movq %rax, %r13
cmpq $0xc, %rax
jne 0x1293f
movl 0x4(%rbx), %edx
movl $0xc, %r13d
cmpq $0x1000, %rdx           # imm = 0x1000
ja 0x1293f
addq $-0xc, %rdx
leaq 0xc(%rbx), %rsi
movl %r14d, %edi
callq 0x5250
movq %rax, %r13
addq $0xc, %r13
jmp 0x1293f
leaq 0x17b2f(%rip), %rax     # 0x2a3d8
cmpl 0x4(%rax), %r14d
leaq 0x1847c(%rip), %rbx     # 0x2ad30
jne 0x128f9
movl $0x4, %edx
movl %r14d, %edi
leaq 0xc(%rsp), %rsi
callq 0x5250
xorl %ebp, %ebp
movb $0x1, %cl
cmpq $0x4, %rax
jne 0x12a1a
movl 0xc(%rsp), %eax
cmpl $0x1000, %eax           # imm = 0x1000
je 0x12a13
cmpl $0x2, %eax
jne 0x128f2
movl $0x1, 0x8(%rsp)
xorl %ebp, %ebp
jmp 0x12a1a
cmpl 0x17a04(%rip), %r14d    # 0x2a304
jne 0x129ef
movl $0x1000, %edx           # imm = 0x1000
movl %r14d, %edi
movq %rbx, %rsi
callq 0x5250
testq %rax, %rax
jle 0x129ef
movq %rbx, %rdi
callq 0xa452
jmp 0x129ef
movl $0x1000, %edx           # imm = 0x1000
movl %r14d, %edi
movq %rbx, %rsi
callq 0x5250
movq %rax, %r13
testq %r13, %r13
jle 0x12953
movq %rbx, %rdi
callq 0x1609e
xorl %ebp, %ebp
jmp 0x129ea
movq 0x17666(%rip), %rax     # 0x29fc0
movq (%rax), %r12
callq 0xeb2f
movq %rax, %rbx
callq 0x5070
movq %rax, %r14
movl (%rax), %ebp
movl %ebp, %edi
callq 0x5530
movq %rax, (%rsp)
movq %r12, %rdi
leaq 0xb854(%rip), %rsi      # 0x1e1d8
movq %rbx, %rdx
leaq 0xfa2d(%rip), %r12      # 0x223bb
movq %r12, %rcx
movl %r13d, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x52c0
leaq 0x18303(%rip), %rax     # 0x2aca8
movq (%rax), %rbx
movl $0xb, %ebp
testq %rbx, %rbx
je 0x129ea
callq 0xeb2f
movq %rax, 0x30(%rsp)
movl (%r14), %r14d
movl %r14d, %edi
callq 0x5530
movq %rax, (%rsp)
movq %rbx, %rdi
leaq 0xb803(%rip), %rsi      # 0x1e1d8
movq 0x30(%rsp), %rdx
movq %r12, %rcx
movl %r13d, %r8d
movl %r14d, %r9d
xorl %eax, %eax
callq 0x52c0
testq %r13, %r13
jle 0x129f1
xorl %ebp, %ebp
xorl %ebx, %ebx
testl %ebp, %ebp
je 0x12a00
cmpl $0xd, %ebp
jne 0x12aba
incq %r15
cmpq %r15, 0x40(%rsp)
jne 0x12756
jmp 0x12735
movl $0x4, %ebp
xorl %ecx, %ecx
testb %cl, %cl
jne 0x129ef
jmp 0x129f1
movq 0x17599(%rip), %rax     # 0x29fc0
movq (%rax), %r12
callq 0xeb2f
movq %rax, %rbp
callq 0x5070
movq %rax, %r13
movl (%rax), %r15d
movl %r15d, %edi
callq 0x5530
movq %rax, (%rsp)
movq %r12, %rdi
leaq 0xb724(%rip), %rsi      # 0x1e177
movq %rbp, %rdx
leaq 0xf95e(%rip), %rcx      # 0x223bb
movl %r14d, %r8d
movl %r15d, %r9d
xorl %eax, %eax
callq 0x52c0
leaq 0x18237(%rip), %rax     # 0x2aca8
movq (%rax), %r12
testq %r12, %r12
je 0x12735
callq 0xeb2f
movq %rax, %r15
movl (%r13), %ebp
movl %ebp, %edi
callq 0x5530
movq %rax, (%rsp)
movq %r12, %rdi
leaq 0xb6d9(%rip), %rsi      # 0x1e177
movq %r15, %rdx
leaq 0xf913(%rip), %rcx      # 0x223bb
movl %r14d, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x52c0
jmp 0x12735
cmpl $0xb, %ebp
je 0x12735
movl %ebp, %ebx
jmp 0x12735
cmpl $0x8, %ebx
je 0x12b44
cmpl $0x4, %ebx
je 0x12b44
jmp 0x12bef
callq 0x5070
movq %rax, %rbx
movl (%rax), %ebp
movl %ebp, %edi
callq 0x5530
leaq 0xf8a2(%rip), %rsi      # 0x22393
movq %r15, %rdi
movq %r12, %rdx
movq %r14, %rcx
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
leaq 0x1819a(%rip), %rax     # 0x2aca8
movq (%rax), %r15
testq %r15, %r15
je 0x12b44
callq 0xeb2f
movq %rax, %r12
movl (%rbx), %ebx
movl %ebx, %edi
callq 0x5530
leaq 0xf865(%rip), %rsi      # 0x22393
movq %r15, %rdi
movq %r12, %rdx
movq %r14, %rcx
movl %ebx, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
movl 0x177c2(%rip), %edi     # 0x2a30c
cmpl $-0x1, %edi
je 0x12b5e
callq 0x5230
movl $0xffffffff, 0x177ae(%rip) # imm = 0xFFFFFFFF
leaq 0x191eb(%rip), %rbx     # 0x2bd50
movq %rbx, %rdi
callq 0x5560
leaq 0x19214(%rip), %rdi     # 0x2bd88
callq 0x5280
movq %rbx, %rdi
callq 0x5340
movl $0x1003, %edi           # imm = 0x1003
callq 0x7444
movq 0x1742e(%rip), %rax     # 0x29fc0
movq (%rax), %rbx
callq 0xeb2f
leaq 0xb687(%rip), %rsi      # 0x1e228
leaq 0xf813(%rip), %rcx      # 0x223bb
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0x180ec(%rip), %rax     # 0x2aca8
movq (%rax), %rbx
testq %rbx, %rbx
je 0x12be4
callq 0xeb2f
leaq 0xb658(%rip), %rsi      # 0x1e228
leaq 0xf7e4(%rip), %rcx      # 0x223bb
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x52c0
movq $0x0, 0x19159(%rip) # 0x2bd48
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq