// File: /workspace/github_c_makefile_star2_O1/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 0x34f1(%rip), %r15      # 0x4fd0
movq (%r15), %rbx
callq 0x133c
callq 0x1190
leaq 0x1778(%rip), %rsi      # 0x326b
leaq 0x3656(%rip), %rdx      # 0x5150
leaq 0x1784(%rip), %rcx      # 0x3285
movq %rbx, %rdi
movq %rax, 0x18(%rsp)
movq %rax, %r8
xorl %eax, %eax
callq 0x1120
leaq 0x46d6(%rip), %rdx      # 0x61f0
movq %rdx, 0x46cf(%rip)      # 0x61f0
movq %rdx, 0x46d0(%rip)      # 0x61f8
leaq 0x46d1(%rip), %rax      # 0x6200
movq %rax, 0x46ca(%rip)      # 0x6200
movq %rax, 0x46cb(%rip)      # 0x6208
leaq 0x20(%rsp), %r12
movl 0x35e8(%rip), %eax      # 0x5130
testl %eax, %eax
jle 0x22a9
testb $0x1, 0x367d(%rip)      # 0x51d4
jne 0x22a9
movl %eax, 0x20(%rsp)
movl $0x1, 0x24(%rsp)
movl 0x35c5(%rip), %eax      # 0x5134
testl %eax, %eax
jle 0x1b87
movl %eax, 0x28(%rsp)
movl $0x1, 0x2c(%rsp)
movl $0x2, %r14d
jmp 0x1b8d
movl $0x1, %r14d
movq %rdx, %rax
movq (%rax), %rax
cmpq %rdx, %rax
je 0x1bb8
movl 0x20(%rax), %ecx
movl %ecx, 0x20(%rsp,%r14,8)
movl $0x1, 0x24(%rsp,%r14,8)
incq %r14
cmpq $0x42, %r14
jne 0x1b90
movl $0x42, %r14d
movslq %r14d, %r13
xorl %edx, %edx
cmpl $0x0, 0x3570(%rip)      # 0x5134
setg %dl
negl %edx
orl $0xc8, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x1160
movl %eax, %ebx
cmpl $-0x1, %eax
jne 0x1bf4
callq 0x1060
cmpl $0x4, (%rax)
jne 0x1bf4
testb $0x1, 0x35e2(%rip)      # 0x51d4
je 0x1bbb
testl %ebx, %ebx
js 0x2243
movl %r14d, %r12d
xorl %r14d, %r14d
movl 0x20(%rsp,%r14,8), %ebx
movswl 0x26(%rsp,%r14,8), %r13d
testb $0x38, %r13b
je 0x1c6e
movq (%r15), %rbp
callq 0x133c
movq %rbp, %rdi
leaq 0x168e(%rip), %rsi      # 0x32b3
leaq 0x3524(%rip), %rdx      # 0x5150
leaq 0x1652(%rip), %rcx      # 0x3285
movl %ebx, %r8d
movl %r13d, %r9d
xorl %eax, %eax
callq 0x1120
movl $0xd, %ebp
cmpl 0x34e5(%rip), %ebx      # 0x5130
je 0x1f5d
movl $0x10, %ebp
cmpl 0x34d8(%rip), %ebx      # 0x5134
je 0x1f5d
movl %ebx, %edi
callq 0x230d
jmp 0x1f5d
movl $0x10, %ebp
testb $0x1, %r13b
je 0x1f5d
cmpl 0x34b1(%rip), %ebx      # 0x5134
jne 0x1d46
movl $0x80, 0x14(%rsp)
movl %ebx, %edi
leaq 0x230(%rsp), %rsi
leaq 0x14(%rsp), %rdx
callq 0x11d0
movl %eax, %ebx
movl $0x28, %edi
callq 0x1140
testq %rax, %rax
je 0x1d1c
movq %rax, %r13
movq %rax, (%rax)
movq %rax, 0x8(%rax)
addq $0x10, %rax
movq %rax, 0x10(%r13)
movq %rax, 0x18(%r13)
movl %ebx, 0x20(%r13)
movl $0x0, 0x24(%r13)
movq (%r15), %rbp
callq 0x133c
movl 0x20(%r13), %ecx
movq %rbp, %rdi
leaq 0x16ab(%rip), %rsi      # 0x3399
leaq 0x345b(%rip), %rdx      # 0x5150
xorl %eax, %eax
callq 0x1120
leaq 0x44ed(%rip), %rax      # 0x61f0
movq %rax, (%r13)
movq 0x44ea(%rip), %rax      # 0x61f8
movq %rax, 0x8(%r13)
movq %r13, (%rax)
movq %r13, 0x44dc(%rip)      # 0x61f8
movl %ebx, %edi
movl $0x3, %esi
xorl %eax, %eax
callq 0x1090
orl $0x800, %eax            # imm = 0x800
movl %ebx, %edi
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x1090
xorl %ebp, %ebp
jmp 0x1f5d
movl 0x33e3(%rip), %r15d     # 0x5130
movl $0x1000, %edx           # imm = 0x1000
movl %ebx, %edi
leaq 0x3495(%rip), %rsi      # 0x51f0
callq 0x10f0
movq %rax, %r13
cmpl %r15d, %ebx
jne 0x1e0f
testq %r13, %r13
movq 0x325a(%rip), %r15      # 0x4fd0
jle 0x1ea1
movzwl 0x346e(%rip), %eax      # 0x51f1
incq %rax
cmpq %rax, %r13
jne 0x1e6a
leaq 0x345a(%rip), %rdi      # 0x51f0
movl %ebx, %esi
movl $0x72, %edx
callq 0x2444
cmpl $-0x1, 0x338b(%rip)     # 0x5134
je 0x1f3d
cmpb $0x0, 0x343e(%rip)      # 0x51f4
leaq 0x4433(%rip), %rbp      # 0x61f0
je 0x1f8a
movq 0x4426(%rip), %rbx      # 0x61f0
cmpq %rbp, %rbx
je 0x1f44
leaq 0x10(%rbx), %r15
movq 0x10(%rbx), %r13
cmpq %r15, %r13
je 0x1e0a
movb 0x340e(%rip), %al       # 0x51f4
cmpb 0x10(%r13), %al
jne 0x1e04
movb 0x3403(%rip), %al       # 0x51f5
testb %al, %al
je 0x1dfc
cmpb 0x11(%r13), %al
jne 0x1e04
movl 0x20(%rbx), %edi
callq 0x24b6
movq (%r13), %r13
jmp 0x1ddb
movq (%rbx), %rbx
jmp 0x1dca
testq %r13, %r13
movq 0x31b7(%rip), %r15      # 0x4fd0
jle 0x1ee7
movzwl 0x33cb(%rip), %eax      # 0x51f1
incq %rax
cmpq %rax, %r13
jne 0x1e6a
leaq 0x33bb(%rip), %rbp      # 0x51f0
movq %rbp, %rdi
movl %ebx, %esi
movl $0x72, %edx
callq 0x2444
cmpl $0x0, 0x32e9(%rip)      # 0x5134
jle 0x1f7d
cmpb $0x0, 0x339c(%rip)      # 0x51f4
je 0x213e
leaq 0x338b(%rip), %rdi      # 0x51f0
jmp 0x1f80
movq (%r15), %rbx
callq 0x133c
movzwl 0x3377(%rip), %r9d      # 0x51f1
movq %rbx, %rdi
leaq 0x1471(%rip), %rsi      # 0x32f5
leaq 0x32c5(%rip), %rdx      # 0x5150
leaq 0x13f3(%rip), %rcx      # 0x3285
movl %r13d, %r8d
xorl %eax, %eax
callq 0x1120
jmp 0x1f5d
movq (%r15), %rbx
callq 0x133c
callq 0x1060
movl (%rax), %ebp
movl %ebp, %edi
callq 0x1200
movq %rax, (%rsp)
movq %rbx, %rdi
leaq 0x1412(%rip), %rsi      # 0x32d7
leaq 0x3284(%rip), %rdx      # 0x5150
leaq 0x13b2(%rip), %rcx      # 0x3285
movl %r13d, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x1120
movl $0xd, %ebp
jmp 0x1f5d
movq (%r15), %rbp
callq 0x133c
callq 0x1060
movl (%rax), %r15d
movl %r15d, %edi
callq 0x1200
movq %rax, (%rsp)
movq %rbp, %rdi
leaq 0x1416(%rip), %rsi      # 0x3323
leaq 0x323c(%rip), %rdx      # 0x5150
leaq 0x136a(%rip), %rcx      # 0x3285
movl %r13d, %r8d
movl %r15d, %r9d
movq 0x30a8(%rip), %r15      # 0x4fd0
xorl %eax, %eax
callq 0x1120
movl %ebx, %edi
callq 0x230d
movl $0xe, %ebp
jmp 0x1f5d
movb $0x1, 0x42cc(%rip)      # 0x6210
movl $0xd, %ebp
cmpb $0x0, 0x3290(%rip)      # 0x51e0
movq 0x3079(%rip), %r15      # 0x4fd0
je 0x1d3f
xorl %eax, %eax
testl %ebp, %ebp
je 0x1f6c
cmpl $0x10, %ebp
jne 0x2289
incq %r14
cmpq %r12, %r14
jne 0x1c02
jmp 0x2290
movq %rbp, %rdi
callq 0x252d
jmp 0x1d3f
cmpb $0x1, 0x3265(%rip)      # 0x51f6
setne %al
leaq 0x4265(%rip), %rcx      # 0x6200
movq (%rcx), %rbx
cmpq %rcx, %rbx
sete %cl
orb %al, %cl
jne 0x218b
movq 0x423d(%rip), %r15      # 0x61f0
cmpq %rbp, %r15
je 0x2171
movl 0x10(%rbx), %edi
cmpl %edi, 0x20(%r15)
jne 0x2136
callq 0x24b6
movzwl 0x3223(%rip), %eax      # 0x51f8
leal -0x23(%rax), %ecx
cmpl $0x2, %ecx
jae 0x202c
movzwl 0x321d(%rip), %eax      # 0x5201
orw 0x3214(%rip), %ax       # 0x51ff
jne 0x2125
leaq 0x10(%r15), %rax
movq 0x10(%r15), %r13
cmpq %rax, %r13
je 0x2125
movb 0x31fe(%rip), %cl       # 0x5206
movb 0x31f9(%rip), %dl       # 0x5207
cmpb 0x10(%r13), %cl
jne 0x201e
cmpb 0x11(%r13), %dl
je 0x20ce
movq (%r13), %r13
cmpq %rax, %r13
jne 0x200e
jmp 0x2125
cmpl $0x22, %eax
jne 0x2136
movzwl 0x31c5(%rip), %eax      # 0x5201
orw 0x31bc(%rip), %ax       # 0x51ff
jne 0x2136
movl $0x18, %edi
callq 0x1140
movq %rax, %r13
movq %rax, (%rax)
movq %rax, 0x8(%rax)
movb 0x31a3(%rip), %al       # 0x5206
movb %al, 0x10(%r13)
movb 0x319a(%rip), %al       # 0x5207
movb %al, 0x11(%r13)
movl $0x0, 0x14(%r13)
movq 0x2f50(%rip), %rax      # 0x4fd0
movq (%rax), %rbp
callq 0x133c
movl 0x20(%r15), %ecx
movzbl 0x10(%r13), %r8d
movzbl 0x11(%r13), %r9d
movq %rbp, %rdi
leaq 0x4150(%rip), %rbp      # 0x61f0
leaq 0x1317(%rip), %rsi      # 0x33be
leaq 0x30a2(%rip), %rdx      # 0x5150
xorl %eax, %eax
callq 0x1120
leaq 0x10(%r15), %rax
movq %rax, (%r13)
movq 0x18(%r15), %rax
movq %rax, 0x8(%r13)
movq %r13, (%rax)
movq %r13, 0x18(%r15)
jmp 0x2136
movq 0x2efb(%rip), %rax      # 0x4fd0
movq (%rax), %rbp
callq 0x133c
movl 0x20(%r15), %ecx
movzbl 0x10(%r13), %r8d
movzbl 0x11(%r13), %r9d
movq %rbp, %rdi
leaq 0x40fb(%rip), %rbp      # 0x61f0
leaq 0x12ed(%rip), %rsi      # 0x33e9
leaq 0x304d(%rip), %rdx      # 0x5150
xorl %eax, %eax
callq 0x1120
movq (%r13), %rax
movq 0x8(%r13), %rcx
movq %rcx, 0x8(%rax)
movq 0x8(%r13), %rcx
movq %rax, (%rcx)
movq %r13, %rdi
callq 0x1030
cmpw $0x24, 0x30cb(%rip)     # 0x51f8
jne 0x2136
movb $0x1, 0x30aa(%rip)      # 0x51e0
movq (%r15), %r15
jmp 0x1fb3
cmpw $0x27, 0x30b2(%rip)     # 0x51f8
jne 0x21d6
movq (%r15), %rbx
callq 0x133c
movq %rbx, %rdi
leaq 0x12b6(%rip), %rsi      # 0x3414
leaq 0x2feb(%rip), %rdx      # 0x5150
xorl %eax, %eax
callq 0x1120
jmp 0x1d3f
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movq %rcx, 0x8(%rax)
movq 0x8(%rbx), %rcx
movq %rax, (%rcx)
movq %rbx, %rdi
callq 0x1030
movq 0x406e(%rip), %r15      # 0x6200
leaq 0x4067(%rip), %rax      # 0x6200
cmpq %rax, %r15
je 0x1f44
movq 0x4047(%rip), %r13      # 0x61f0
cmpq %rbp, %r13
je 0x1f44
leaq 0x14(%r15), %rbx
movl 0x20(%r13), %eax
cmpl 0x10(%r15), %eax
jne 0x21c8
movq %rbx, %rdi
callq 0x252d
movq (%r13), %r13
cmpq %rbp, %r13
jne 0x21b6
jmp 0x1f44
leaq 0x4023(%rip), %rbp      # 0x6200
cmpq %rbp, 0x401c(%rip)      # 0x6200
jne 0x21f2
leaq 0x3003(%rip), %rdi      # 0x51f0
callq 0x252d
movl %r13d, %r15d
leaq 0x18(%r15), %rdi
callq 0x1140
movq %rax, %r13
movq %rax, (%rax)
movq %rax, 0x8(%rax)
movl %ebx, 0x10(%rax)
leaq 0x14(%rax), %rdi
leaq 0x2fda(%rip), %rsi      # 0x51f0
movq %r15, %rdx
callq 0x1130
movq %rbp, (%r13)
movq 0x3fdf(%rip), %rax      # 0x6208
movq %rax, 0x8(%r13)
movq %r13, (%rax)
movq %r13, 0x3fd1(%rip)      # 0x6208
movq 0x2d92(%rip), %r15      # 0x4fd0
jmp 0x1d3f
movq (%r15), %r13
callq 0x133c
callq 0x1060
movl (%rax), %ebp
movl %ebp, %edi
callq 0x1200
movq %rax, (%rsp)
movq %r13, %rdi
leaq 0x102d(%rip), %rsi      # 0x3294
leaq 0x2ee2(%rip), %rdx      # 0x5150
leaq 0x1010(%rip), %rcx      # 0x3285
movl %ebx, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x1120
movl $0xd, %eax
jmp 0x2290
cmpl $0xe, %ebp
je 0x2290
movl %ebp, %eax
testl %eax, %eax
leaq 0x3f57(%rip), %rdx      # 0x61f0
leaq 0x20(%rsp), %r12
je 0x1b42
cmpl $0xd, %eax
jne 0x22f9
movq 0x3f40(%rip), %rax      # 0x61f0
leaq 0x3f39(%rip), %rbx      # 0x61f0
cmpq %rbx, %rax
je 0x22cd
movl 0x20(%rax), %edi
callq 0x230d
movq 0x3f25(%rip), %rax      # 0x61f0
jmp 0x22b7
movq (%r15), %rbx
callq 0x133c
leaq 0x1064(%rip), %rsi      # 0x3340
leaq 0x2e6d(%rip), %rdx      # 0x5150
leaq 0xf9b(%rip), %rcx       # 0x3285
movq %rbx, %rdi
movq 0x18(%rsp), %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