// File: /workspace/github_c_makefile_star2_O0/Carton32[P]quectel-CM/QmiWwanCM.c
// Function: QmiWwanThread
static void * QmiWwanThread(void *pData) {
    PROFILE_T *profile = (PROFILE_T *)pData;
    const char *cdc_wdm = (const char *)profile->qmichannel;
    int wait_for_request_quit = 0;
    char num = cdc_wdm[strlen(cdc_wdm)-1];
	
    if (profile->proxy[0]) {
         if (!strncmp(profile->proxy, QUECTEL_QMI_PROXY, strlen(QUECTEL_QMI_PROXY))) {
            snprintf(profile->proxy, sizeof(profile->proxy), "%s%c", QUECTEL_QMI_PROXY, num);
         }
    }
    else if (profile->qmap_mode > 1) {
        snprintf(profile->proxy, sizeof(profile->proxy), "%s%c", QUECTEL_QMI_PROXY, num);
    }
    
    if (profile->proxy[0])
        cdc_wdm_fd = cm_open_proxy(profile->proxy);
    else
        cdc_wdm_fd = cm_open_dev(cdc_wdm);

    if (cdc_wdm_fd == -1) {
        dbg_time("%s Failed to open %s, errno: %d (%s)", __func__, cdc_wdm, errno, strerror(errno));
        qmidevice_send_event_to_main(RIL_INDICATE_DEVICE_DISCONNECTED);
        pthread_exit(NULL);
        return NULL;
    }

    dbg_time("cdc_wdm_fd = %d", cdc_wdm_fd);

    qmidevice_send_event_to_main(RIL_INDICATE_DEVICE_CONNECTED);
    while (1) {
        struct pollfd pollfds[] = {{qmidevice_control_fd[1], POLLIN, 0}, {cdc_wdm_fd, POLLIN, 0}};
        int ne, ret, nevents = sizeof(pollfds)/sizeof(pollfds[0]);

        do {
            ret = poll(pollfds, nevents, wait_for_request_quit ? 1000 : -1);
         } while ((ret < 0) && (errno == EINTR));

	if (ret == 0 && wait_for_request_quit) {
            QmiThreadRecvQMI(NULL);
            continue;
	}

        if (ret <= 0) {
            dbg_time("%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;

            //dbg_time("{%d, %x, %x}", pollfds[ne].fd, pollfds[ne].events, pollfds[ne].revents);

            if (revents & (POLLERR | POLLHUP | POLLNVAL)) {
                dbg_time("%s poll err/hup/inval", __func__);
                dbg_time("poll fd = %d, events = 0x%04x", fd, revents);
                if (fd == cdc_wdm_fd) {
                } else {
                }
                if (revents & (POLLHUP | POLLNVAL)) //EC20 bug, Can get POLLERR
                    goto __QmiWwanThread_quit;
            }

            if ((revents & POLLIN) == 0)
                continue;

            if (fd == qmidevice_control_fd[1]) {
                int triger_event;
                if (read(fd, &triger_event, sizeof(triger_event)) == sizeof(triger_event)) {
                    //DBG("triger_event = 0x%x", triger_event);
                    switch (triger_event) {
                        case RIL_REQUEST_QUIT:
                            goto __QmiWwanThread_quit;
                        break;
                        case SIG_EVENT_STOP:
                            wait_for_request_quit = 1;
                        break;
                        default:
                        break;
                    }
                }
            }

            if (fd == cdc_wdm_fd) {
                ssize_t nreads;
                PQCQMIMSG pResponse = (PQCQMIMSG)cm_recv_buf;
                
                if (!profile->proxy[0])
                    nreads = read(fd, cm_recv_buf, sizeof(cm_recv_buf));
                else
                    nreads = qmi_proxy_read(fd, cm_recv_buf, sizeof(cm_recv_buf));
                //dbg_time("%s read=%d errno: %d (%s)",  __func__, (int)nreads, errno, strerror(errno));
                if (nreads <= 0) {
                    dbg_time("%s read=%d errno: %d (%s)",  __func__, (int)nreads, errno, strerror(errno));
                    break;
                }
#ifdef QUECTEL_QMI_MERGE
                if((profile->qmap_mode == 0 || profile->qmap_mode == 1) && QmiWwanMergeQmiRsp(cm_recv_buf, &nreads))
                    continue;             
#endif
                if (nreads != (le16_to_cpu(pResponse->QMIHdr.Length) + 1)) {
                    dbg_time("%s nreads=%d,  pQCQMI->QMIHdr.Length = %d",  __func__, (int)nreads, le16_to_cpu(pResponse->QMIHdr.Length));
                    continue;
                }

                QmiThreadRecvQMI(pResponse);
            }
        }
    }

__QmiWwanThread_quit:
    if (cdc_wdm_fd != -1) { close(cdc_wdm_fd); cdc_wdm_fd = -1; }
    qmidevice_send_event_to_main(RIL_INDICATE_DEVICE_DISCONNECTED);
    QmiThreadRecvQMI(NULL); //main thread may pending on QmiThreadSendQMI()
    dbg_time("%s exit", __func__);
    pthread_exit(NULL);
    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
addq $0x8a, %rax
movq %rax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rdi
callq 0x5160
movq %rax, %rcx
movq -0x60(%rbp), %rax
subq $0x1, %rcx
movb (%rax,%rcx), %al
movb %al, -0x1d(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x28(%rax)
je 0x5e1f
movq -0x10(%rbp), %rdi
addq $0x28, %rdi
leaq 0x25366(%rip), %rsi     # 0x2b14d
movl $0x11, %edx
callq 0x5090
cmpl $0x0, %eax
jne 0x5e1d
movq -0x10(%rbp), %rdi
addq $0x28, %rdi
movsbl -0x1d(%rbp), %r8d
movl $0x20, %esi
leaq 0x25350(%rip), %rdx     # 0x2b15f
leaq 0x25337(%rip), %rcx     # 0x2b14d
movb $0x0, %al
callq 0x51c0
jmp 0x5e55
movq -0x10(%rbp), %rax
cmpl $0x1, 0x10a(%rax)
jle 0x5e53
movq -0x10(%rbp), %rdi
addq $0x28, %rdi
movsbl -0x1d(%rbp), %r8d
movl $0x20, %esi
leaq 0x2531a(%rip), %rdx     # 0x2b15f
leaq 0x25301(%rip), %rcx     # 0x2b14d
movb $0x0, %al
callq 0x51c0
jmp 0x5e55
movq -0x10(%rbp), %rax
cmpb $0x0, 0x28(%rax)
je 0x5e74
movq -0x10(%rbp), %rdi
addq $0x28, %rdi
callq 0x139f0
movl %eax, 0x3147e(%rip)     # 0x372f0
jmp 0x5e83
movq -0x18(%rbp), %rdi
callq 0x13770
movl %eax, 0x3146d(%rip)     # 0x372f0
cmpl $-0x1, 0x31466(%rip)    # 0x372f0
jne 0x5f88
jmp 0x5e92
movq 0x31147(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0x80(%rbp)
callq 0x13930
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x70(%rbp)
callq 0x5070
movl (%rax), %eax
movl %eax, -0x64(%rbp)
callq 0x5070
movl (%rax), %edi
callq 0x5540
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rdx
movq -0x70(%rbp), %r8
movl -0x64(%rbp), %r9d
leaq 0x25286(%rip), %rsi     # 0x2b164
leaq 0x252aa(%rip), %rcx     # 0x2b18f
movq %rax, (%rsp)
movb $0x0, %al
callq 0x52c0
leaq 0x31f71(%rip), %rax     # 0x37e68
cmpq $0x0, (%rax)
je 0x5f73
leaq 0x31f64(%rip), %rax     # 0x37e68
movq (%rax), %rax
movq %rax, -0xa0(%rbp)
callq 0x13930
movq %rax, -0x98(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x90(%rbp)
callq 0x5070
movl (%rax), %eax
movl %eax, -0x84(%rbp)
callq 0x5070
movl (%rax), %edi
callq 0x5540
movq -0xa0(%rbp), %rdi
movq -0x98(%rbp), %rdx
movq -0x90(%rbp), %r8
movl -0x84(%rbp), %r9d
leaq 0x25203(%rip), %rsi     # 0x2b164
leaq 0x25227(%rip), %rcx     # 0x2b18f
movq %rax, (%rsp)
movb $0x0, %al
callq 0x52c0
jmp 0x5f75
movl $0x1003, %edi           # imm = 0x1003
callq 0x8240
xorl %eax, %eax
movl %eax, %edi
callq 0x5360
jmp 0x5f8a
movq 0x3104f(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0xa8(%rbp)
callq 0x13930
movq -0xa8(%rbp), %rdi
movq %rax, %rdx
movl 0x31340(%rip), %ecx     # 0x372f0
leaq 0x251e6(%rip), %rsi     # 0x2b19d
movb $0x0, %al
callq 0x52c0
leaq 0x31ea3(%rip), %rax     # 0x37e68
cmpq $0x0, (%rax)
je 0x5fff
leaq 0x31e96(%rip), %rax     # 0x37e68
movq (%rax), %rax
movq %rax, -0xb0(%rbp)
callq 0x13930
movq -0xb0(%rbp), %rdi
movq %rax, %rdx
movl 0x312ff(%rip), %ecx     # 0x372f0
leaq 0x251a5(%rip), %rsi     # 0x2b19d
movb $0x0, %al
callq 0x52c0
jmp 0x6001
movl $0x1002, %edi           # imm = 0x1002
callq 0x8240
leaq 0x31582(%rip), %rax     # 0x37594
movl 0x4(%rax), %eax
movl %eax, -0x30(%rbp)
movw $0x1, -0x2c(%rbp)
movw $0x0, -0x2a(%rbp)
movl 0x312c6(%rip), %eax     # 0x372f0
movl %eax, -0x28(%rbp)
movw $0x1, -0x24(%rbp)
movw $0x0, -0x22(%rbp)
movl $0x2, -0x3c(%rbp)
leaq -0x30(%rbp), %rdi
movslq -0x3c(%rbp), %rsi
movl -0x1c(%rbp), %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x3e8, %eax            # imm = 0x3E8
cmpl $0x0, %ecx
cmovnel %eax, %edx
callq 0x53d0
movl %eax, -0x38(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x38(%rbp)
movb %al, -0xb1(%rbp)
jge 0x6082
callq 0x5070
cmpl $0x4, (%rax)
sete %al
movb %al, -0xb1(%rbp)
movb -0xb1(%rbp), %al
testb $0x1, %al
jne 0x6040
cmpl $0x0, -0x38(%rbp)
jne 0x60a6
cmpl $0x0, -0x1c(%rbp)
je 0x60a6
xorl %eax, %eax
movl %eax, %edi
callq 0xc800
jmp 0x600b
cmpl $0x0, -0x38(%rbp)
jg 0x61ae
jmp 0x60b2
movq 0x30f27(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0xd0(%rbp)
callq 0x13930
movq %rax, -0xc8(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0xbc(%rbp)
callq 0x5070
movl (%rax), %eax
movl %eax, -0xb8(%rbp)
callq 0x5070
movl (%rax), %edi
callq 0x5540
movq -0xd0(%rbp), %rdi
movq -0xc8(%rbp), %rdx
movl -0xbc(%rbp), %r8d
movl -0xb8(%rbp), %r9d
leaq 0x2509f(%rip), %rsi     # 0x2b1b3
leaq 0x25074(%rip), %rcx     # 0x2b18f
movq %rax, (%rsp)
movb $0x0, %al
callq 0x52c0
leaq 0x31d3b(%rip), %rax     # 0x37e68
cmpq $0x0, (%rax)
je 0x61a7
leaq 0x31d2e(%rip), %rax     # 0x37e68
movq (%rax), %rax
movq %rax, -0xe8(%rbp)
callq 0x13930
movq %rax, -0xe0(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0xd8(%rbp)
callq 0x5070
movl (%rax), %eax
movl %eax, -0xd4(%rbp)
callq 0x5070
movl (%rax), %edi
callq 0x5540
movq -0xe8(%rbp), %rdi
movq -0xe0(%rbp), %rdx
movl -0xd8(%rbp), %r8d
movl -0xd4(%rbp), %r9d
leaq 0x2501e(%rip), %rsi     # 0x2b1b3
leaq 0x24ff3(%rip), %rcx     # 0x2b18f
movq %rax, (%rsp)
movb $0x0, %al
callq 0x52c0
jmp 0x61a9
jmp 0x65f7
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x65f2
movslq -0x34(%rbp), %rax
movl -0x30(%rbp,%rax,8), %eax
movl %eax, -0x40(%rbp)
movslq -0x34(%rbp), %rax
movw -0x2a(%rbp,%rax,8), %ax
movw %ax, -0x42(%rbp)
movswl -0x42(%rbp), %eax
andl $0x38, %eax
cmpl $0x0, %eax
je 0x6303
jmp 0x61eb
movq 0x30dee(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0xf0(%rbp)
callq 0x13930
movq -0xf0(%rbp), %rdi
movq %rax, %rdx
leaq 0x24fc2(%rip), %rsi     # 0x2b1d4
leaq 0x24f76(%rip), %rcx     # 0x2b18f
movb $0x0, %al
callq 0x52c0
leaq 0x31c41(%rip), %rax     # 0x37e68
cmpq $0x0, (%rax)
je 0x6262
leaq 0x31c34(%rip), %rax     # 0x37e68
movq (%rax), %rax
movq %rax, -0xf8(%rbp)
callq 0x13930
movq -0xf8(%rbp), %rdi
movq %rax, %rdx
leaq 0x24f80(%rip), %rsi     # 0x2b1d4
leaq 0x24f34(%rip), %rcx     # 0x2b18f
movb $0x0, %al
callq 0x52c0
jmp 0x6264
jmp 0x6266
movq 0x30d73(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0x100(%rbp)
callq 0x13930
movq -0x100(%rbp), %rdi
movq %rax, %rdx
movl -0x40(%rbp), %ecx
movswl -0x42(%rbp), %r8d
leaq 0x24f5b(%rip), %rsi     # 0x2b1f0
movb $0x0, %al
callq 0x52c0
leaq 0x31bc5(%rip), %rax     # 0x37e68
cmpq $0x0, (%rax)
je 0x62df
leaq 0x31bb8(%rip), %rax     # 0x37e68
movq (%rax), %rax
movq %rax, -0x108(%rbp)
callq 0x13930
movq -0x108(%rbp), %rdi
movq %rax, %rdx
movl -0x40(%rbp), %ecx
movswl -0x42(%rbp), %r8d
leaq 0x24f18(%rip), %rsi     # 0x2b1f0
movb $0x0, %al
callq 0x52c0
jmp 0x62e1
movl -0x40(%rbp), %eax
cmpl 0x31006(%rip), %eax     # 0x372f0
jne 0x62ee
jmp 0x62f0
jmp 0x62f0
movswl -0x42(%rbp), %eax
andl $0x30, %eax
cmpl $0x0, %eax
je 0x6301
jmp 0x65f9
jmp 0x6303
movswl -0x42(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x6314
jmp 0x65e4
movl -0x40(%rbp), %eax
leaq 0x31276(%rip), %rcx     # 0x37594
cmpl 0x4(%rcx), %eax
jne 0x636d
movl -0x40(%rbp), %edi
leaq -0x48(%rbp), %rsi
movl $0x4, %edx
callq 0x5250
cmpq $0x4, %rax
jne 0x636b
movl -0x48(%rbp), %eax
movl %eax, -0x10c(%rbp)
subl $0x2, %eax
je 0x635e
jmp 0x634a
movl -0x10c(%rbp), %eax
subl $0x1000, %eax           # imm = 0x1000
jne 0x6367
jmp 0x6359
jmp 0x65f9
movl $0x1, -0x1c(%rbp)
jmp 0x6369
jmp 0x6369
jmp 0x636b
jmp 0x636d
movl -0x40(%rbp), %eax
cmpl 0x30f7a(%rip), %eax     # 0x372f0
jne 0x65e2
leaq 0x31b6d(%rip), %rax     # 0x37ef0
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x28(%rax)
jne 0x63ab
movl -0x40(%rbp), %edi
leaq 0x31b55(%rip), %rsi     # 0x37ef0
movl $0x1000, %edx           # imm = 0x1000
callq 0x5250
movq %rax, -0x50(%rbp)
jmp 0x63c3
movl -0x40(%rbp), %edi
leaq 0x31b3b(%rip), %rsi     # 0x37ef0
movl $0x1000, %edx           # imm = 0x1000
callq 0x6f50
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jg 0x64ce
jmp 0x63d0
movq 0x30c09(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0x128(%rbp)
callq 0x13930
movq %rax, -0x120(%rbp)
movq -0x50(%rbp), %rax
movl %eax, -0x114(%rbp)
callq 0x5070
movl (%rax), %eax
movl %eax, -0x110(%rbp)
callq 0x5070
movl (%rax), %edi
callq 0x5540
movq -0x128(%rbp), %rdi
movq -0x120(%rbp), %rdx
movl -0x114(%rbp), %r8d
movl -0x110(%rbp), %r9d
leaq 0x24de1(%rip), %rsi     # 0x2b214
leaq 0x24d55(%rip), %rcx     # 0x2b18f
movq %rax, (%rsp)
movb $0x0, %al
callq 0x52c0
leaq 0x31a1c(%rip), %rax     # 0x37e68
cmpq $0x0, (%rax)
je 0x64c7
leaq 0x31a0f(%rip), %rax     # 0x37e68
movq (%rax), %rax
movq %rax, -0x140(%rbp)
callq 0x13930
movq %rax, -0x138(%rbp)
movq -0x50(%rbp), %rax
movl %eax, -0x130(%rbp)
callq 0x5070
movl (%rax), %eax
movl %eax, -0x12c(%rbp)
callq 0x5070
movl (%rax), %edi
callq 0x5540
movq -0x140(%rbp), %rdi
movq -0x138(%rbp), %rdx
movl -0x130(%rbp), %r8d
movl -0x12c(%rbp), %r9d
leaq 0x24d5f(%rip), %rsi     # 0x2b214
leaq 0x24cd3(%rip), %rcx     # 0x2b18f
movq %rax, (%rsp)
movb $0x0, %al
callq 0x52c0
jmp 0x64c9
jmp 0x65f2
movq -0x50(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x58(%rbp), %rax
movzwl 0x1(%rax), %edi
callq 0x13ed0
movw %ax, %cx
movq -0x148(%rbp), %rax
movzwl %cx, %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
je 0x65d9
jmp 0x6504
movq 0x30ad5(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0x160(%rbp)
callq 0x13930
movq %rax, -0x158(%rbp)
movq -0x50(%rbp), %rax
movl %eax, -0x14c(%rbp)
movq -0x58(%rbp), %rax
movzwl 0x1(%rax), %edi
callq 0x13ed0
movq -0x160(%rbp), %rdi
movq -0x158(%rbp), %rdx
movl -0x14c(%rbp), %r8d
movzwl %ax, %r9d
leaq 0x24cdc(%rip), %rsi     # 0x2b234
leaq 0x24c30(%rip), %rcx     # 0x2b18f
movb $0x0, %al
callq 0x52c0
leaq 0x318fb(%rip), %rax     # 0x37e68
cmpq $0x0, (%rax)
je 0x65d5
leaq 0x318ee(%rip), %rax     # 0x37e68
movq (%rax), %rax
movq %rax, -0x178(%rbp)
callq 0x13930
movq %rax, -0x170(%rbp)
movq -0x50(%rbp), %rax
movl %eax, -0x164(%rbp)
movq -0x58(%rbp), %rax
movzwl 0x1(%rax), %edi
callq 0x13ed0
movq -0x178(%rbp), %rdi
movq -0x170(%rbp), %rdx
movl -0x164(%rbp), %r8d
movzwl %ax, %r9d
leaq 0x24c6d(%rip), %rsi     # 0x2b234
leaq 0x24bc1(%rip), %rcx     # 0x2b18f
movb $0x0, %al
callq 0x52c0
jmp 0x65d7
jmp 0x65e4
movq -0x58(%rbp), %rdi
callq 0xc800
jmp 0x65e4
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x61b5
jmp 0x600b
jmp 0x65f9
cmpl $-0x1, 0x30cf0(%rip)    # 0x372f0
je 0x6617
movl 0x30ce8(%rip), %edi     # 0x372f0
callq 0x5230
movl $0xffffffff, 0x30cd9(%rip) # imm = 0xFFFFFFFF
movl $0x1003, %edi           # imm = 0x1003
callq 0x8240
xorl %eax, %eax
movl %eax, %edi
callq 0xc800
movq 0x309af(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0x180(%rbp)
callq 0x13930
movq -0x180(%rbp), %rdi
movq %rax, %rdx
leaq 0x24c13(%rip), %rsi     # 0x2b264
leaq 0x24b37(%rip), %rcx     # 0x2b18f
movb $0x0, %al
callq 0x52c0
leaq 0x31802(%rip), %rax     # 0x37e68
cmpq $0x0, (%rax)
je 0x66a1
leaq 0x317f5(%rip), %rax     # 0x37e68
movq (%rax), %rax
movq %rax, -0x188(%rbp)
callq 0x13930
movq -0x188(%rbp), %rdi
movq %rax, %rdx
leaq 0x24bd1(%rip), %rsi     # 0x2b264
leaq 0x24af5(%rip), %rcx     # 0x2b18f
movb $0x0, %al
callq 0x52c0
jmp 0x66a3
xorl %eax, %eax
movl %eax, %edi
callq 0x5360
nopl (%rax)