// File: /workspace/github_c_makefile_star2_O2/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 0x34eb(%rip), %r14      # 0x4fd0
movq (%r14), %rbx
callq 0x133c
callq 0x1190
leaq 0x1772(%rip), %rsi      # 0x326b
leaq 0x3650(%rip), %rdx      # 0x5150
leaq 0x177e(%rip), %rcx      # 0x3285
movq %rbx, %rdi
movq %rax, 0x10(%rsp)
movq %rax, %r8
xorl %eax, %eax
callq 0x1120
leaq 0x46d0(%rip), %r15      # 0x61f0
movq %r15, 0x46c9(%rip)      # 0x61f0
movq %r15, 0x46ca(%rip)      # 0x61f8
leaq 0x46cb(%rip), %rax      # 0x6200
movq %rax, 0x46c4(%rip)      # 0x6200
movq %rax, 0x46c5(%rip)      # 0x6208
pushq $0x1
popq %rcx
pushq $0x2
popq %rdx
leaq 0x20(%rsp), %r12
movl 0x35dc(%rip), %eax      # 0x5130
testl %eax, %eax
jle 0x21c1
testb $0x1, 0x3671(%rip)      # 0x51d4
jne 0x21c1
movl %eax, 0x20(%rsp)
movl $0x1, 0x24(%rsp)
movl 0x35b9(%rip), %eax      # 0x5134
movq %rcx, %r14
testl %eax, %eax
jle 0x1b91
movl %eax, 0x28(%rsp)
movl $0x1, 0x2c(%rsp)
movq %rdx, %r14
movq %r15, %rax
movq (%rax), %rax
cmpq %r15, %rax
je 0x1bbc
movl 0x20(%rax), %ecx
movl %ecx, 0x20(%rsp,%r14,8)
movl $0x1, 0x24(%rsp,%r14,8)
incq %r14
cmpq $0x42, %r14
jne 0x1b94
pushq $0x42
popq %rax
movl %eax, %r14d
movslq %r14d, %rbp
xorl %edx, %edx
cmpl $0x0, 0x356c(%rip)      # 0x5134
setg %dl
negl %edx
orl $0xc8, %edx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x1160
movl %eax, %ebx
cmpl $-0x1, %eax
jne 0x1c01
callq 0x1060
cmpl $0x4, (%rax)
jne 0x216e
testb $0x1, 0x35da(%rip)      # 0x51d4
je 0x1bbf
jmp 0x216e
testl %ebx, %ebx
js 0x216e
movl %r14d, %eax
movq %rax, 0x18(%rsp)
xorl %r12d, %r12d
movq 0x33b5(%rip), %r14      # 0x4fd0
cmpq 0x18(%rsp), %r12
je 0x1b43
movl 0x20(%rsp,%r12,8), %ebp
movswl 0x26(%rsp,%r12,8), %ebx
testb $0x38, %bl
je 0x1c87
movq (%r14), %r13
callq 0x133c
movq %r13, %rdi
leaq 0x166b(%rip), %rsi      # 0x32b3
leaq 0x3501(%rip), %rdx      # 0x5150
leaq 0x162f(%rip), %rcx      # 0x3285
movl %ebp, %r8d
movl %ebx, %r9d
xorl %eax, %eax
callq 0x1120
cmpl 0x34c7(%rip), %ebp      # 0x5130
je 0x21c1
cmpl 0x34bf(%rip), %ebp      # 0x5134
je 0x1ea7
movl %ebp, %edi
callq 0x225c
jmp 0x1ea7
testb $0x1, %bl
je 0x1ea7
cmpl 0x349e(%rip), %ebp      # 0x5134
jne 0x1d44
movl $0x80, 0xc(%rsp)
movl %ebp, %edi
leaq 0x230(%rsp), %rsi
leaq 0xc(%rsp), %rdx
callq 0x11d0
movl %eax, %ebx
pushq $0x28
popq %rdi
callq 0x1140
testq %rax, %rax
je 0x1d20
movq %rax, %rbp
movq %rax, (%rax)
movq %rax, 0x8(%rax)
addq $0x10, %rax
movq %rax, 0x10(%rbp)
movq %rax, 0x18(%rbp)
movl %ebx, 0x20(%rbp)
andl $0x0, 0x24(%rbp)
movq (%r14), %r13
callq 0x133c
movl 0x20(%rbp), %ecx
movq %r13, %rdi
leaq 0x16a0(%rip), %rsi      # 0x3399
leaq 0x3450(%rip), %rdx      # 0x5150
xorl %eax, %eax
callq 0x1120
movq %r15, (%rbp)
movq 0x44e6(%rip), %rax      # 0x61f8
movq %rax, 0x8(%rbp)
movq %rbp, (%rax)
movq %rbp, 0x44d8(%rip)      # 0x61f8
movl %ebx, %edi
pushq $0x3
popq %rsi
xorl %eax, %eax
callq 0x1090
orl $0x800, %eax            # imm = 0x800
movl %ebx, %edi
pushq $0x4
popq %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x1090
jmp 0x1ea7
movl 0x33e5(%rip), %r14d     # 0x5130
movl $0x1000, %edx           # imm = 0x1000
movl %ebp, %edi
leaq 0x3497(%rip), %rsi      # 0x51f0
callq 0x10f0
movq %rax, %rbx
cmpl %r14d, %ebp
jne 0x1dfd
testq %rbx, %rbx
movq 0x325c(%rip), %r14      # 0x4fd0
jle 0x2214
movzwl 0x3470(%rip), %eax      # 0x51f1
incq %rax
cmpq %rax, %rbx
jne 0x1e4f
leaq 0x345c(%rip), %rdi      # 0x51f0
movl %ebp, %esi
pushq $0x72
popq %rdx
callq 0x2373
cmpl $-0x1, 0x338f(%rip)     # 0x5134
je 0x1e83
cmpb $0x0, 0x3442(%rip)      # 0x51f4
je 0x1eaf
movq %r15, %rbx
movq (%rbx), %rbx
cmpq %r15, %rbx
je 0x1e8a
leaq 0x10(%rbx), %r14
movq %r14, %r13
movq (%r13), %r13
cmpq %r14, %r13
je 0x1dbb
movb 0x3417(%rip), %al       # 0x51f4
cmpb 0x10(%r13), %al
jne 0x1dce
movb 0x340c(%rip), %al       # 0x51f5
testb %al, %al
je 0x1df3
cmpb 0x11(%r13), %al
jne 0x1dce
movl 0x20(%rbx), %edi
callq 0x23e3
jmp 0x1dce
testq %rbx, %rbx
movq 0x31c9(%rip), %r14      # 0x4fd0
jle 0x2121
movzwl 0x33dd(%rip), %eax      # 0x51f1
incq %rax
cmpq %rax, %rbx
jne 0x1e4f
leaq 0x33cd(%rip), %r13      # 0x51f0
movq %r13, %rdi
movl %ebp, %esi
pushq $0x72
popq %rdx
callq 0x2373
cmpl $0x0, 0x32fd(%rip)      # 0x5134
jle 0x1e9f
cmpb $0x0, 0x33b4(%rip)      # 0x51f4
je 0x2038
leaq 0x33a3(%rip), %rdi      # 0x51f0
jmp 0x1ea2
movq (%r14), %r13
callq 0x133c
movzwl 0x3392(%rip), %r9d      # 0x51f1
movq %r13, %rdi
leaq 0x148c(%rip), %rsi      # 0x32f5
leaq 0x32e0(%rip), %rdx      # 0x5150
leaq 0x140e(%rip), %rcx      # 0x3285
movl %ebx, %r8d
xorl %eax, %eax
callq 0x1120
jmp 0x1ea7
movb $0x1, 0x4386(%rip)      # 0x6210
cmpb $0x0, 0x334f(%rip)      # 0x51e0
movq 0x3138(%rip), %r14      # 0x4fd0
je 0x1ea7
jmp 0x21c1
movq %r13, %rdi
callq 0x2463
incq %r12
jmp 0x1c1b
cmpb $0x1, 0x3340(%rip)      # 0x51f6
setne %al
leaq 0x4340(%rip), %rdx      # 0x6200
movq (%rdx), %rbp
cmpq %rdx, %rbp
sete %cl
orb %al, %cl
jne 0x208c
movq %r15, %r14
movq (%r14), %r14
cmpq %r15, %r14
je 0x2067
movl 0x20(%r14), %edi
cmpl 0x10(%rbp), %edi
jne 0x1ed4
callq 0x23e3
movzwl 0x3303(%rip), %eax      # 0x51f8
movzwl %ax, %ecx
leal -0x23(%rcx), %edx
cmpl $0x2, %edx
jae 0x1fa3
movzwl 0x32f6(%rip), %ecx      # 0x5201
orw 0x32ed(%rip), %cx       # 0x51ff
jne 0x1f8d
leaq 0x10(%r14), %rcx
movb 0x32e8(%rip), %dl       # 0x5206
movb 0x32e2(%rip), %sil      # 0x5207
movq %rcx, %rbx
movq (%rbx), %rbx
cmpq %rcx, %rbx
je 0x1f8d
cmpb 0x10(%rbx), %dl
jne 0x1f28
cmpb 0x11(%rbx), %sil
jne 0x1f28
movq 0x308e(%rip), %rax      # 0x4fd0
movq (%rax), %r13
callq 0x133c
movl 0x20(%r14), %ecx
movzbl 0x10(%rbx), %r8d
movzbl 0x11(%rbx), %r9d
movq %r13, %rdi
leaq 0x1487(%rip), %rsi      # 0x33e9
leaq 0x31e7(%rip), %rdx      # 0x5150
xorl %eax, %eax
callq 0x1120
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movq %rcx, 0x8(%rax)
movq %rax, (%rcx)
movq %rbx, %rdi
callq 0x1030
movzwl 0x326b(%rip), %eax      # 0x51f8
cmpw $0x24, %ax
jne 0x1ed4
movb $0x1, 0x3242(%rip)      # 0x51e0
jmp 0x1ed4
cmpl $0x22, %ecx
jne 0x1ed4
movzwl 0x324e(%rip), %eax      # 0x5201
orw 0x3245(%rip), %ax       # 0x51ff
jne 0x1ed4
pushq $0x18
popq %rdi
callq 0x1140
movq %rax, %rbx
movq %rax, (%rax)
movq %rax, 0x8(%rax)
movb 0x322e(%rip), %al       # 0x5206
movb %al, 0x10(%rbx)
movb 0x3226(%rip), %al       # 0x5207
movb %al, 0x11(%rbx)
andl $0x0, 0x14(%rbx)
movq 0x2fe1(%rip), %rax      # 0x4fd0
movq (%rax), %r13
callq 0x133c
movl 0x20(%r14), %ecx
movzbl 0x10(%rbx), %r8d
movzbl 0x11(%rbx), %r9d
movq %r13, %rdi
leaq 0x13af(%rip), %rsi      # 0x33be
leaq 0x313a(%rip), %rdx      # 0x5150
xorl %eax, %eax
callq 0x1120
leaq 0x10(%r14), %rax
movq %rax, (%rbx)
movq 0x18(%r14), %rax
movq %rax, 0x8(%rbx)
movq %rbx, (%rax)
movq %rbx, 0x18(%r14)
jmp 0x1ed4
cmpw $0x27, 0x31b8(%rip)     # 0x51f8
jne 0x20bb
movq (%r14), %rbx
callq 0x133c
movq %rbx, %rdi
leaq 0x13c0(%rip), %rsi      # 0x3414
leaq 0x30f5(%rip), %rdx      # 0x5150
xorl %eax, %eax
callq 0x1120
jmp 0x1ea7
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq %rax, (%rcx)
movq %rbp, %rdi
callq 0x1030
movq 0x417b(%rip), %rbp      # 0x6200
leaq 0x4174(%rip), %rdx      # 0x6200
cmpq %rdx, %rbp
je 0x1e8a
leaq 0x14(%rbp), %rbx
movq %r15, %r14
movq (%r14), %r14
cmpq %r15, %r14
je 0x1e8a
movl 0x20(%r14), %eax
cmpl 0x10(%rbp), %eax
jne 0x209c
movq %rbx, %rdi
callq 0x2463
jmp 0x209c
leaq 0x413e(%rip), %rax      # 0x6200
cmpq %rax, 0x4137(%rip)      # 0x6200
jne 0x20d7
leaq 0x311e(%rip), %rdi      # 0x51f0
callq 0x2463
movq %rbx, %rdi
addq $0x18, %rdi
callq 0x1140
movq %rax, %r13
movl %ebp, 0x10(%rax)
leaq 0x14(%rax), %rdi
leaq 0x30fc(%rip), %rsi      # 0x51f0
movq %rbx, %rdx
callq 0x1130
leaq 0x40fd(%rip), %rax      # 0x6200
movq %rax, (%r13)
movq 0x40fa(%rip), %rax      # 0x6208
movq %rax, 0x8(%r13)
movq %r13, (%rax)
movq %r13, 0x40ec(%rip)      # 0x6208
jmp 0x1ea7
movq (%r14), %r12
callq 0x133c
callq 0x1060
movl (%rax), %r13d
movl %r13d, %edi
callq 0x1200
movq %rax, (%rsp)
movq %r12, %rdi
leaq 0x11dc(%rip), %rsi      # 0x3323
leaq 0x3002(%rip), %rdx      # 0x5150
leaq 0x1130(%rip), %rcx      # 0x3285
movl %ebx, %r8d
movl %r13d, %r9d
xorl %eax, %eax
callq 0x1120
movl %ebp, %edi
callq 0x225c
jmp 0x1b43
movq 0x2e5b(%rip), %r15      # 0x4fd0
movq (%r15), %r14
callq 0x133c
callq 0x1060
movl (%rax), %ebp
movl %ebp, %edi
callq 0x1200
movq %rax, (%rsp)
leaq 0x10fe(%rip), %rsi      # 0x3294
leaq 0x2fb3(%rip), %rdx      # 0x5150
leaq 0x10e1(%rip), %rcx      # 0x3285
movq %r14, %rdi
movq %r15, %r14
movl %ebx, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x1120
jmp 0x21c1
movl 0x20(%rax), %edi
callq 0x225c
movq 0x4028(%rip), %rax      # 0x61f0
leaq 0x4021(%rip), %rcx      # 0x61f0
cmpq %rcx, %rax
jne 0x21b9
movq (%r14), %rbx
callq 0x133c
leaq 0x115d(%rip), %rsi      # 0x3340
leaq 0x2f66(%rip), %rdx      # 0x5150
leaq 0x1094(%rip), %rcx      # 0x3285
movq %rbx, %rdi
movq 0x10(%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
movq 0x2db5(%rip), %rax      # 0x4fd0
movq (%rax), %r14
callq 0x133c
callq 0x1060
movl (%rax), %ebp
movl %ebp, %edi
callq 0x1200
movq %rax, (%rsp)
leaq 0x109b(%rip), %rsi      # 0x32d7
leaq 0x2f0d(%rip), %rdx      # 0x5150
leaq 0x103b(%rip), %rcx      # 0x3285
movq %r14, %rdi
movq 0x2d7c(%rip), %r14      # 0x4fd0
movl %ebx, %r8d
jmp 0x21ad