// File: /workspace/github_c_makefile_star2_O3/Carton32[P]quectel-CM/quectel-qmi-proxy.c
// Function: qmi_proxy_loop
static void *qmi_proxy_loop(void *param)
{
    PQCQMIMSG pQMI = (PQCQMIMSG)qmi_buf;
    struct qlistnode *con_node;
    QMI_PROXY_CONNECTION *qmi_con;

    (void)param;
    dprintf("%s enter thread_id %p\n", __func__, (void *)pthread_self());

    qlist_init(&qmi_proxy_connection);
    qlist_init(&qmi_proxy_ctl_msg);

    while (cdc_wdm_fd > 0 && qmi_proxy_quit == 0) {
        struct pollfd pollfds[2+64];
        int ne, ret, nevents = 0;
        ssize_t nreads;

        pollfds[nevents].fd = cdc_wdm_fd;
        pollfds[nevents].events = POLLIN;
        pollfds[nevents].revents= 0;
        nevents++;
        
        if (qmi_proxy_server_fd > 0) {
            pollfds[nevents].fd = qmi_proxy_server_fd;
            pollfds[nevents].events = POLLIN;
            pollfds[nevents].revents= 0;
            nevents++;
        }

        qlist_for_each(con_node, &qmi_proxy_connection) {
            qmi_con = qnode_to_item(con_node, QMI_PROXY_CONNECTION, qnode);
            
            pollfds[nevents].fd = qmi_con->ClientFd;
            pollfds[nevents].events = POLLIN;
            pollfds[nevents].revents= 0;
            nevents++;

            if (nevents == (sizeof(pollfds)/sizeof(pollfds[0])))
                break;
        }

#if 0
        dprintf("poll ");
        for (ne = 0; ne < nevents; ne++) {
            dprintf("%d ", pollfds[ne].fd);
        }
        dprintf("\n");
#endif

        do {
            //ret = poll(pollfds, nevents, -1);
            ret = poll(pollfds, nevents, (qmi_proxy_server_fd > 0) ? -1 : 200);
         } while (ret == -1 && errno == EINTR && qmi_proxy_quit == 0);
         
        if (ret < 0) {
            dprintf("%s poll=%d, errno: %d (%s)\n", __func__, ret, errno, strerror(errno));
            goto qmi_proxy_loop_exit;
        }

        for (ne = 0; ne < nevents; ne++) {
            int fd = pollfds[ne].fd;
            short revents = pollfds[ne].revents;

            if (revents & (POLLERR | POLLHUP | POLLNVAL)) {
                dprintf("%s poll fd = %d, revents = %04x\n", __func__, fd, revents);
                if (fd == cdc_wdm_fd) {
                    goto qmi_proxy_loop_exit;
                } else if(fd == qmi_proxy_server_fd) {
                
                } else {
                    cleanup_qmi_connection(fd);
                }

                continue;
            }

            if (!(pollfds[ne].revents & POLLIN)) {
                continue;
            }

            if (fd == qmi_proxy_server_fd) {
                accept_qmi_connection(fd);
            }
            else if (fd == cdc_wdm_fd) {
                nreads = read(fd, pQMI, sizeof(qmi_buf));
                if (nreads <= 0) {
                    dprintf("%s read=%d errno: %d (%s)\n",  __func__, (int)nreads, errno, strerror(errno));
                    goto qmi_proxy_loop_exit;
                }
#ifdef QUECTEL_QMI_MERGE
                if(merge_qmi_rsp_packet(pQMI, &nreads))
                    continue;             
#endif
                if (nreads != (le16toh(pQMI->QMIHdr.Length) + 1)) {
                    dprintf("%s nreads=%d,  pQCQMI->QMIHdr.Length = %d\n",  __func__, (int)nreads, le16toh(pQMI->QMIHdr.Length));
                    continue;
                }

                dump_qmi(pQMI, fd, 'r');
                recv_qmi_from_dev(pQMI);
                if (modem_reset_flag)
                    goto qmi_proxy_loop_exit;
            }
            else {
                nreads = read(fd, pQMI, sizeof(qmi_buf));
  
                if (nreads <= 0) {
                    dprintf("%s read=%d errno: %d (%s)",  __func__, (int)nreads, errno, strerror(errno));
                    cleanup_qmi_connection(fd);
                    break;
                }

                if (nreads != (le16toh(pQMI->QMIHdr.Length) + 1)) {
                    dprintf("%s nreads=%d,  pQCQMI->QMIHdr.Length = %d\n",  __func__, (int)nreads, le16toh(pQMI->QMIHdr.Length));
                    continue;
                }

                dump_qmi(pQMI, fd, 'r');
                recv_qmi_from_client(pQMI, nreads, fd);
            }
        }
    }

qmi_proxy_loop_exit:
    while (!qlist_empty(&qmi_proxy_connection)) {
        QMI_PROXY_CONNECTION *qmi_con = qnode_to_item(qlist_head(&qmi_proxy_connection), QMI_PROXY_CONNECTION, qnode);

        cleanup_qmi_connection(qmi_con->ClientFd);
    }
    
    dprintf("%s exit, thread_id %p\n", __func__, (void *)pthread_self());

    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2b8, %rsp            # imm = 0x2B8
movq 0x34fc(%rip), %rax      # 0x4fd0
movq (%rax), %r14
callq 0x133c
callq 0x1190
movq %rax, %r15
leaq 0x1780(%rip), %rsi      # 0x326b
leaq 0x365e(%rip), %rdx      # 0x5150
leaq 0x178c(%rip), %rcx      # 0x3285
movq %r14, %rdi
movq %rax, %r8
xorl %eax, %eax
callq 0x1120
leaq 0x46e3(%rip), %rbp      # 0x61f0
movq %rbp, 0x46dc(%rip)      # 0x61f0
movq %rbp, 0x46dd(%rip)      # 0x61f8
leaq 0x46de(%rip), %rax      # 0x6200
movq %rax, 0x46d7(%rip)      # 0x6200
movq %rax, 0x46d8(%rip)      # 0x6208
movl 0x35fa(%rip), %eax      # 0x5130
testl %eax, %eax
jle 0x2229
testb $0x1, 0x368f(%rip)      # 0x51d4
jne 0x2229
leaq 0x20(%rsp), %r14
movq %r15, 0x18(%rsp)
movl %eax, 0x20(%rsp)
movl $0x1, 0x24(%rsp)
movl 0x35cd(%rip), %eax      # 0x5134
testl %eax, %eax
jle 0x1b7e
movl %eax, 0x28(%rsp)
movl $0x1, 0x2c(%rsp)
movl $0x2, %ebx
jmp 0x1b83
movl $0x1, %ebx
movq %rbp, %rax
movq (%rax), %rax
cmpq %rbp, %rax
je 0x1bab
movl 0x20(%rax), %ecx
movl %ecx, 0x20(%rsp,%rbx,8)
movl $0x1, 0x24(%rsp,%rbx,8)
incq %rbx
cmpq $0x42, %rbx
jne 0x1b86
movl $0x42, %ebx
movslq %ebx, %r13
xorl %edx, %edx
cmpl $0x0, 0x357d(%rip)      # 0x5134
setg %dl
negl %edx
orl $0xc8, %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0x1160
movl %eax, %r12d
cmpl $-0x1, %eax
jne 0x1bf1
callq 0x1060
cmpl $0x4, (%rax)
jne 0x21de
testb $0x1, 0x35ea(%rip)      # 0x51d4
je 0x1bae
jmp 0x21de
testl %r12d, %r12d
js 0x21de
movl %ebx, %ebx
xorl %r14d, %r14d
movl 0x20(%rsp,%r14,8), %r13d
movswl 0x26(%rsp,%r14,8), %r12d
testb $0x38, %r12b
je 0x1c6b
movq 0x33b9(%rip), %rax      # 0x4fd0
movq (%rax), %r15
callq 0x133c
movq %r15, %rdi
leaq 0x168a(%rip), %rsi      # 0x32b3
leaq 0x3520(%rip), %rdx      # 0x5150
leaq 0x164e(%rip), %rcx      # 0x3285
movl %r13d, %r8d
movl %r12d, %r9d
xorl %eax, %eax
callq 0x1120
cmpl 0x34e5(%rip), %r13d     # 0x5130
je 0x2224
cmpl 0x34dc(%rip), %r13d     # 0x5134
je 0x1e9e
movl %r13d, %edi
callq 0x22d3
jmp 0x1e9e
testb $0x1, %r12b
je 0x1e9e
cmpl 0x34b8(%rip), %r13d     # 0x5134
jne 0x1d41
movl $0x80, 0x14(%rsp)
movl %r13d, %edi
leaq 0x230(%rsp), %rsi
leaq 0x14(%rsp), %rdx
callq 0x11d0
movl %eax, %r12d
movl $0x28, %edi
callq 0x1140
testq %rax, %rax
je 0x1d17
movq %rax, %r13
movq %rax, (%rax)
movq %rax, 0x8(%rax)
addq $0x10, %rax
movq %rax, 0x10(%r13)
movq %rax, 0x18(%r13)
movl %r12d, 0x20(%r13)
movl $0x0, 0x24(%r13)
movq 0x32f6(%rip), %rax      # 0x4fd0
movq (%rax), %r15
callq 0x133c
movl 0x20(%r13), %ecx
movq %r15, %rdi
leaq 0x16a9(%rip), %rsi      # 0x3399
leaq 0x3459(%rip), %rdx      # 0x5150
xorl %eax, %eax
callq 0x1120
movq %rbp, (%r13)
movq 0x44ef(%rip), %rax      # 0x61f8
movq %rax, 0x8(%r13)
movq %r13, (%rax)
movq %r13, 0x44e1(%rip)      # 0x61f8
movl %r12d, %edi
movl $0x3, %esi
xorl %eax, %eax
callq 0x1090
orl $0x800, %eax            # imm = 0x800
movl %r12d, %edi
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x1090
jmp 0x1e9e
movl 0x33e8(%rip), %r15d     # 0x5130
movl $0x1000, %edx           # imm = 0x1000
movl %r13d, %edi
leaq 0x3499(%rip), %rsi      # 0x51f0
callq 0x10f0
movq %rax, %r12
cmpl %r15d, %r13d
jne 0x1e00
testq %r12, %r12
jle 0x2292
movzwl 0x3479(%rip), %eax      # 0x51f1
incq %rax
cmpq %rax, %r12
jne 0x1e4f
leaq 0x3465(%rip), %rdi      # 0x51f0
movl %r13d, %esi
movl $0x72, %edx
callq 0x23f1
cmpl $-0x1, 0x3395(%rip)     # 0x5134
je 0x1e8a
movb 0x3449(%rip), %al       # 0x51f4
testb %al, %al
je 0x1eaf
movq 0x4436(%rip), %r15      # 0x61f0
cmpq %rbp, %r15
je 0x1e91
leaq 0x10(%r15), %r12
movq 0x10(%r15), %r13
cmpq %r12, %r13
je 0x1dfb
cmpb 0x10(%r13), %al
jne 0x1df5
movb 0x3419(%rip), %cl       # 0x51f5
testb %cl, %cl
je 0x1de6
cmpb 0x11(%r13), %cl
jne 0x1df5
movl 0x20(%r15), %edi
callq 0x2463
movb 0x33ff(%rip), %al       # 0x51f4
movq (%r13), %r13
jmp 0x1dcb
movq (%r15), %r15
jmp 0x1dba
testq %r12, %r12
jle 0x216b
movzwl 0x33e1(%rip), %eax      # 0x51f1
incq %rax
cmpq %rax, %r12
jne 0x1e4f
leaq 0x33d1(%rip), %r15      # 0x51f0
movq %r15, %rdi
movl %r13d, %esi
movl $0x72, %edx
callq 0x23f1
cmpl $0x0, 0x32fe(%rip)      # 0x5134
jle 0x1e45
cmpb $0x0, 0x33b5(%rip)      # 0x51f4
je 0x2072
movq %r15, %rdi
callq 0x24da
jmp 0x1e9e
movq 0x317a(%rip), %rax      # 0x4fd0
movq (%rax), %r15
callq 0x133c
movzwl 0x338b(%rip), %r9d      # 0x51f1
movq %r15, %rdi
leaq 0x1485(%rip), %rsi      # 0x32f5
leaq 0x32d9(%rip), %rdx      # 0x5150
leaq 0x1407(%rip), %rcx      # 0x3285
movl %r12d, %r8d
xorl %eax, %eax
callq 0x1120
jmp 0x1e9e
movb $0x1, 0x437f(%rip)      # 0x6210
cmpb $0x0, 0x3348(%rip)      # 0x51e0
jne 0x2224
incq %r14
cmpq %rbx, %r14
jne 0x1bff
jmp 0x21bb
cmpb $0x1, 0x3340(%rip)      # 0x51f6
setne %al
leaq 0x4340(%rip), %rcx      # 0x6200
movq (%rcx), %r13
cmpq %rcx, %r13
sete %cl
orb %al, %cl
jne 0x20ca
movq 0x4318(%rip), %r15      # 0x61f0
cmpq %rbp, %r15
je 0x20ac
movl 0x20(%r15), %edi
cmpl 0x10(%r13), %edi
jne 0x206a
callq 0x2463
movzwl 0x32fd(%rip), %eax      # 0x51f8
movzwl %ax, %ecx
leal -0x23(%rcx), %edx
cmpl $0x2, %edx
jae 0x1f58
movzwl 0x32f4(%rip), %ecx      # 0x5201
orw 0x32eb(%rip), %cx       # 0x51ff
jne 0x205d
leaq 0x10(%r15), %rcx
movq 0x10(%r15), %r12
cmpq %rcx, %r12
je 0x205d
movb 0x32d5(%rip), %dl       # 0x5206
movb 0x32cf(%rip), %sil      # 0x5207
cmpb 0x10(%r12), %dl
jne 0x1f4a
cmpb 0x11(%r12), %sil
je 0x2000
movq (%r12), %r12
cmpq %rcx, %r12
jne 0x1f38
jmp 0x205d
cmpl $0x22, %ecx
jne 0x206a
movzwl 0x3299(%rip), %eax      # 0x5201
orw 0x3290(%rip), %ax       # 0x51ff
jne 0x206a
movl $0x18, %edi
callq 0x1140
movq %rax, %r12
movq %rax, (%rax)
movq %rax, 0x8(%rax)
movb 0x3277(%rip), %al       # 0x5206
movb %al, 0x10(%r12)
movb 0x326d(%rip), %al       # 0x5207
movb %al, 0x11(%r12)
movl $0x0, 0x14(%r12)
movq 0x3021(%rip), %rax      # 0x4fd0
movq (%rax), %rax
movq %rax, 0x8(%rsp)
callq 0x133c
movl 0x20(%r15), %ecx
movzbl 0x10(%r12), %r8d
movzbl 0x11(%r12), %r9d
movq 0x8(%rsp), %rdi
leaq 0x13e6(%rip), %rsi      # 0x33be
leaq 0x3171(%rip), %rdx      # 0x5150
xorl %eax, %eax
callq 0x1120
leaq 0x10(%r15), %rax
movq %rax, (%r12)
movq 0x18(%r15), %rax
movq %rax, 0x8(%r12)
movq %r12, (%rax)
movq %r12, 0x18(%r15)
jmp 0x206a
movq 0x2fc9(%rip), %rax      # 0x4fd0
movq (%rax), %rax
movq %rax, 0x8(%rsp)
callq 0x133c
movl 0x20(%r15), %ecx
movzbl 0x10(%r12), %r8d
movzbl 0x11(%r12), %r9d
movq 0x8(%rsp), %rdi
leaq 0x13b9(%rip), %rsi      # 0x33e9
leaq 0x3119(%rip), %rdx      # 0x5150
xorl %eax, %eax
callq 0x1120
movq (%r12), %rax
movq 0x8(%r12), %rcx
movq %rcx, 0x8(%rax)
movq %rax, (%rcx)
movq %r12, %rdi
callq 0x1030
movzwl 0x319b(%rip), %eax      # 0x51f8
cmpw $0x24, %ax
jne 0x206a
movb $0x1, 0x3176(%rip)      # 0x51e0
movq (%r15), %r15
jmp 0x1ed8
cmpw $0x27, 0x317e(%rip)     # 0x51f8
jne 0x210d
movq 0x2f49(%rip), %rax      # 0x4fd0
movq (%rax), %r15
callq 0x133c
movq %r15, %rdi
leaq 0x137b(%rip), %rsi      # 0x3414
leaq 0x30b0(%rip), %rdx      # 0x5150
xorl %eax, %eax
callq 0x1120
jmp 0x1e9e
movq (%r13), %rax
movq 0x8(%r13), %rcx
movq %rcx, 0x8(%rax)
movq %rax, (%rcx)
movq %r13, %rdi
callq 0x1030
movq 0x4136(%rip), %r13      # 0x6200
leaq 0x412f(%rip), %rax      # 0x6200
cmpq %rax, %r13
je 0x1e91
movq 0x410f(%rip), %r15      # 0x61f0
cmpq %rbp, %r15
je 0x1e91
leaq 0x14(%r13), %r12
movl 0x20(%r15), %eax
cmpl 0x10(%r13), %eax
jne 0x2100
movq %r12, %rdi
callq 0x24da
movq (%r15), %r15
cmpq %rbp, %r15
jne 0x20ee
jmp 0x1e91
leaq 0x40ec(%rip), %rax      # 0x6200
cmpq %rax, 0x40e5(%rip)      # 0x6200
jne 0x2125
movq %r15, %rdi
callq 0x24da
movq %r12, %rdi
addq $0x18, %rdi
callq 0x1140
movq %r15, %rsi
movq %rax, %r15
movl %r13d, 0x10(%rax)
leaq 0x14(%rax), %rdi
movq %r12, %rdx
callq 0x1130
leaq 0x40b2(%rip), %rax      # 0x6200
movq %rax, (%r15)
movq 0x40b0(%rip), %rax      # 0x6208
movq %rax, 0x8(%r15)
movq %r15, (%rax)
movq %r15, 0x40a2(%rip)      # 0x6208
jmp 0x1e9e
movq 0x2e5e(%rip), %rax      # 0x4fd0
movq (%rax), %rbx
callq 0x133c
callq 0x1060
movl (%rax), %r15d
movl %r15d, %edi
callq 0x1200
movq %rax, (%rsp)
movq %rbx, %rdi
leaq 0x118b(%rip), %rsi      # 0x3323
leaq 0x2fb1(%rip), %rdx      # 0x5150
leaq 0x10df(%rip), %rcx      # 0x3285
movl %r12d, %r8d
movl %r15d, %r9d
xorl %eax, %eax
callq 0x1120
movl %r13d, %edi
callq 0x22d3
movl 0x2f6f(%rip), %eax      # 0x5130
testl %eax, %eax
movq 0x18(%rsp), %r15
leaq 0x20(%rsp), %r14
jle 0x2229
testb $0x1, 0x2ffe(%rip)      # 0x51d4
je 0x1b55
jmp 0x2229
movq 0x2deb(%rip), %rax      # 0x4fd0
movq (%rax), %r14
callq 0x133c
callq 0x1060
movl (%rax), %ebp
movl %ebp, %edi
callq 0x1200
movq %rax, (%rsp)
leaq 0x108e(%rip), %rsi      # 0x3294
leaq 0x2f43(%rip), %rdx      # 0x5150
leaq 0x1071(%rip), %rcx      # 0x3285
movq %r14, %rdi
movl %r12d, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x1120
movq 0x18(%rsp), %r15
movq 0x3fc0(%rip), %rax      # 0x61f0
leaq 0x3fb9(%rip), %rbx      # 0x61f0
cmpq %rbx, %rax
je 0x224d
movl 0x20(%rax), %edi
callq 0x22d3
movq 0x3fa5(%rip), %rax      # 0x61f0
jmp 0x2237
movq 0x2d7c(%rip), %rax      # 0x4fd0
movq (%rax), %r14
callq 0x133c
leaq 0x10dd(%rip), %rsi      # 0x3340
leaq 0x2ee6(%rip), %rdx      # 0x5150
leaq 0x1014(%rip), %rcx      # 0x3285
movq %r14, %rdi
movq %r15, %r8
xorl %eax, %eax
callq 0x1120
xorl %eax, %eax
addq $0x2b8, %rsp            # imm = 0x2B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2d37(%rip), %rax      # 0x4fd0
movq (%rax), %r14
callq 0x133c
callq 0x1060
movl (%rax), %ebp
movl %ebp, %edi
callq 0x1200
movq %rax, (%rsp)
leaq 0x101d(%rip), %rsi      # 0x32d7
leaq 0x2e8f(%rip), %rdx      # 0x5150
leaq 0xfbd(%rip), %rcx       # 0x3285
movq %r14, %rdi
movl %r12d, %r8d
jmp 0x221a