// File: /workspace/github_c_makefile_star2_O2/Carton32[P]quectel-CM/quectel-qmi-proxy.c
// Function: main
int main(int argc, char *argv[]) {
    int opt;
    char cdc_wdm[32+1] = "/dev/cdc-wdm0";
    int retry_times = 0;
    char servername[64] = {0};

    optind = 1;

    signal(SIGINT, sig_action);

    while ( -1 != (opt = getopt(argc, argv, "d:i:vh"))) {
        switch (opt) {
            case 'd':
                strcpy(cdc_wdm, optarg);
                break;
            case 'v':
                verbose_debug = 1;
                break;
            default:
                usage();
                return 0;
        }
    }

    if (access(cdc_wdm, R_OK | W_OK)) {
        dprintf("Fail to access %s, errno: %d (%s). break\n", cdc_wdm, errno, strerror(errno));
        return -1;
    }

    sprintf(servername, "quectel-qmi-proxy%c", cdc_wdm[strlen(cdc_wdm)-1]);
    dprintf("Will use cdc-wdm='%s', proxy='%s'\n", cdc_wdm, servername);

    while (qmi_proxy_quit == 0) {
        if (access(cdc_wdm, R_OK | W_OK)) {
            dprintf("Fail to access %s, errno: %d (%s). continue\n", cdc_wdm, errno, strerror(errno));
            // wait device
            sleep(3);
            continue;
        }

        cdc_wdm_fd = open(cdc_wdm, O_RDWR | O_NONBLOCK | O_NOCTTY);
        if (cdc_wdm_fd == -1) {
            dprintf("Failed to open %s, errno: %d (%s). break\n", cdc_wdm, errno, strerror(errno));
            return -1;
        }
        cfmakenoblock(cdc_wdm_fd);
        
        /* no qmi_proxy_loop lives, create one */
        pthread_create(&thread_id, NULL, qmi_proxy_loop, NULL);
        /* try to redo init if failed, init function must be successfully */
        while (qmi_proxy_init() != 0) {
            if (retry_times < 5) {
                dprintf("fail to init proxy, try again in 2 seconds.\n");
                sleep(2);
                retry_times++;
            } else {
                dprintf("has failed too much times, restart the modem and have a try...\n");
                break;
            }
            /* break loop if modem is detached */
            if (access(cdc_wdm, F_OK|R_OK|W_OK))
                break;
        }
        retry_times = 0;
        qmi_start_server(servername);
        if (qmi_proxy_server_fd == -1)
            pthread_cancel(thread_id); 
        pthread_join(thread_id, NULL);

        /* close local server at last */
        qmi_close_server(servername);
        close(cdc_wdm_fd);
        /* DO RESTART IN 20s IF MODEM RESET ITSELF */
        if (modem_reset_flag) {
            unsigned int time_to_wait = 20;
            while (time_to_wait) {
                time_to_wait = sleep(time_to_wait);
            }
            modem_reset_flag = 0;
        }
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rsi, %rbx
movl %edi, %ebp
movaps 0x2184(%rip), %xmm0     # 0x3570
leaq 0x10(%rsp), %r14
movaps %xmm0, 0x10(%r14)
movaps 0x2163(%rip), %xmm0     # 0x3560
movaps %xmm0, (%r14)
movb $0x0, 0x20(%r14)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq 0x3bb4(%rip), %rax      # 0x4fd8
movl $0x1, (%rax)
leaq 0x679(%rip), %rsi       # 0x1aaa
pushq $0x2
popq %rdi
callq 0x1110
leaq 0x1be4(%rip), %r15      # 0x3024
movq 0x3b99(%rip), %r12      # 0x4fe0
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x11c0
cmpl $0x64, %eax
je 0x1467
cmpl $0x76, %eax
jne 0x1475
movb $0x1, 0x3d6b(%rip)      # 0x51d0
jmp 0x1447
movq (%r12), %rsi
movq %r14, %rdi
callq 0x1070
jmp 0x1447
cmpl $-0x1, %eax
jne 0x14d7
leaq 0x10(%rsp), %r14
pushq $0x6
popq %rsi
movq %r14, %rdi
callq 0x11b0
testl %eax, %eax
je 0x1506
pushq $-0x1
popq %r14
movq 0x3b37(%rip), %rax      # 0x4fd0
movq (%rax), %rbx
callq 0x133c
callq 0x1060
movl (%rax), %ebp
movl %ebp, %edi
callq 0x1200
leaq 0x1b75(%rip), %rsi      # 0x302b
leaq 0x3c93(%rip), %rdx      # 0x5150
leaq 0x10(%rsp), %rcx
movq %rbx, %rdi
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x1120
jmp 0x1a95
movq 0x3af2(%rip), %rax      # 0x4fd0
movq (%rax), %rbx
callq 0x133c
leaq 0x1c75(%rip), %rsi      # 0x3162
leaq 0x3c5c(%rip), %rdx      # 0x5150
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1120
jmp 0x1a95
movq %r14, %rdi
callq 0x10b0
movsbl -0x1(%rax,%r14), %edx
leaq 0x1b3d(%rip), %rsi      # 0x3058
leaq 0x40(%rsp), %r15
movq %r15, %rdi
xorl %eax, %eax
callq 0x11e0
movq 0x3a9f(%rip), %rax      # 0x4fd0
movq (%rax), %rbx
callq 0x133c
leaq 0x1b2c(%rip), %rsi      # 0x306c
leaq 0x3c09(%rip), %rdx      # 0x5150
movq %rbx, %rdi
movq %r14, %rcx
movq %r15, %r8
xorl %eax, %eax
callq 0x1120
pushq $0x3
popq %r12
pushq $0x1
popq %rbx
leaq 0x8a(%rsp), %r13
cmpb $0x0, 0x3c67(%rip)      # 0x51d4
jne 0x1a4c
leaq 0x10(%rsp), %r15
movq %r15, %rdi
pushq $0x6
popq %rsi
callq 0x11b0
testl %eax, %eax
je 0x15cf
movq 0x3a42(%rip), %rax      # 0x4fd0
movq (%rax), %r14
callq 0x133c
callq 0x1060
movl (%rax), %ebp
movl %ebp, %edi
callq 0x1200
movq %r14, %rdi
leaq 0x1ae4(%rip), %rsi      # 0x3092
leaq 0x3b9b(%rip), %rdx      # 0x5150
movq %r15, %rcx
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x1120
movl %r12d, %edi
callq 0x1220
jmp 0x1566
movq %r15, %rdi
movl $0x902, %esi            # imm = 0x902
xorl %eax, %eax
callq 0x11a0
movl %eax, 0x3b4c(%rip)      # 0x5130
cmpl $-0x1, %eax
je 0x1a51
movl %eax, %ebp
xorl %r15d, %r15d
movl %eax, %edi
movl %r12d, %esi
xorl %eax, %eax
callq 0x1090
orl $0x800, %eax            # imm = 0x800
movl %ebp, %edi
pushq $0x4
popq %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x1090
leaq 0x3bc0(%rip), %rdi      # 0x51d8
xorl %esi, %esi
leaq 0x4ac(%rip), %rdx       # 0x1acd
xorl %ecx, %ecx
callq 0x1180
movq 0x39a1(%rip), %rax      # 0x4fd0
leaq 0x3b1a(%rip), %r12      # 0x5150
movq (%rax), %r14
callq 0x133c
movq %r14, %rdi
leaq 0x1df9(%rip), %rsi      # 0x3441
movq %r12, %rbp
movq %r12, %rdx
leaq 0x1df9(%rip), %rcx      # 0x344e
xorl %eax, %eax
callq 0x1120
movb $0x1, 0x8a(%rsp)
andl $0x0, 0x8d(%rsp)
movb $0x0, 0x4b9d(%rip)      # 0x6210
pushq $-0xa
popq %rax
movl %eax, %r14d
testl %r14d, %r14d
je 0x16bd
leal 0xb(%r14), %eax
movb %al, 0x91(%rsp)
movl $0x27, 0x92(%rsp)
movw $0xb, 0x8b(%rsp)
movq %r13, %rdi
callq 0x2463
testl %eax, %eax
jle 0x16bd
movl %ebx, %edi
callq 0x1220
incl %r14d
cmpb $0x1, 0x4b55(%rip)      # 0x6210
jne 0x1679
movq 0x390c(%rip), %r12      # 0x4fd0
movq (%r12), %r14
callq 0x133c
cmpb $0x0, 0x4b3c(%rip)      # 0x6210
leaq 0x1d94(%rip), %r8       # 0x346f
leaq 0x1d85(%rip), %rax      # 0x3467
cmovneq %rax, %r8
movq %r14, %rdi
leaq 0x1d6d(%rip), %rsi      # 0x345d
movq %rbp, %rdx
leaq 0x1d54(%rip), %rcx      # 0x344e
xorl %eax, %eax
callq 0x1120
cmpb $0x0, 0x4b08(%rip)      # 0x6210
jne 0x176a
movq (%r12), %r14
callq 0x133c
movq %r14, %rdi
cmpl $0x5, %r15d
je 0x1759
leaq 0x19cc(%rip), %rsi      # 0x30ef
movq %rbp, %r12
movq %rbp, %rdx
xorl %eax, %eax
callq 0x1120
pushq $0x2
popq %rdi
callq 0x1220
incl %r15d
leaq 0x10(%rsp), %rdi
pushq $0x6
popq %rsi
callq 0x11b0
testl %eax, %eax
movq 0x387f(%rip), %rax      # 0x4fd0
je 0x1636
jmp 0x176a
leaq 0x19bf(%rip), %rsi      # 0x311f
movq %rbp, %rdx
xorl %eax, %eax
callq 0x1120
movl $0x1, 0xc(%rsp)
movl %ebx, %edi
movl %ebx, %esi
xorl %edx, %edx
callq 0x1230
testl %eax, %eax
js 0x186e
movl %eax, %r12d
leaq 0x8c(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x5c(%rax)
movups %xmm0, 0x50(%rax)
movups %xmm0, 0x40(%rax)
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movw $0x1, 0x8a(%rsp)
leaq 0x40(%rsp), %r15
movq %r15, %rdi
callq 0x10b0
movq %rax, %r14
leaq 0x8d(%rsp), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x1130
movl %r12d, %edi
movl %ebx, %esi
pushq $0x2
popq %rdx
leaq 0xc(%rsp), %rcx
pushq $0x4
popq %r8
callq 0x1080
testl %eax, %eax
js 0x18af
addl $0x3, %r14d
movl %r12d, %edi
movq %r13, %rsi
movl %r14d, %edx
callq 0x1170
testl %eax, %eax
movq 0x37bc(%rip), %r15      # 0x4fd0
js 0x18f5
movq (%r15), %r14
callq 0x133c
movq %r14, %rdi
leaq 0x1cdd(%rip), %rsi      # 0x3509
movq %rbp, %rdx
leaq 0x40(%rsp), %rcx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x1120
movl %r12d, %edi
pushq $0x3
popq %rsi
xorl %eax, %eax
callq 0x1090
orl $0x800, %eax            # imm = 0x800
movl %r12d, %edi
pushq $0x4
popq %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x1090
movl %r12d, %edi
movl %ebx, %esi
callq 0x1150
jmp 0x193c
movq 0x375b(%rip), %r15      # 0x4fd0
movq (%r15), %r14
callq 0x133c
callq 0x1060
movq %rax, %r12
movl (%rax), %edi
callq 0x1200
movl (%r12), %ecx
movl %ecx, (%rsp)
movq %r14, %rdi
leaq 0x1c1c(%rip), %rsi      # 0x34b9
movq %rbp, %rdx
leaq 0x1c33(%rip), %rcx      # 0x34da
movl $0x1e8, %r8d            # imm = 0x1E8
jmp 0x192e
movq 0x371a(%rip), %r15      # 0x4fd0
movq (%r15), %r14
callq 0x133c
callq 0x1060
movq %rbp, %r12
movq %rax, %rbp
movl (%rax), %edi
callq 0x1200
movl (%rbp), %ecx
movq %r12, %rbp
movl %ecx, (%rsp)
movq %r14, %rdi
leaq 0x1bd6(%rip), %rsi      # 0x34b9
movq %r12, %rdx
leaq 0x1bed(%rip), %rcx      # 0x34da
movl $0x1f0, %r8d            # imm = 0x1F0
jmp 0x192e
movl %r12d, %edi
callq 0x10e0
movq (%r15), %r14
callq 0x133c
callq 0x1060
movq %rbp, %r12
movl (%rax), %ebp
movl %ebp, %edi
callq 0x1200
movq %r14, %rdi
leaq 0x1bce(%rip), %rsi      # 0x34ee
movq %r12, %rdx
leaq 0x40(%rsp), %rcx
movl %ebp, %r8d
movq %r12, %rbp
movq %rax, %r9
xorl %eax, %eax
callq 0x1120
pushq $-0x1
popq %r12
movl %r12d, 0x37f1(%rip)     # 0x5134
movq (%r15), %r14
callq 0x133c
movl 0x37e3(%rip), %ecx      # 0x5134
movq %r14, %rdi
leaq 0x1b19(%rip), %rsi      # 0x3474
movq %rbp, %rdx
xorl %eax, %eax
callq 0x1120
cmpl $-0x1, 0x37c8(%rip)     # 0x5134
jne 0x19c3
movq (%r15), %r14
callq 0x133c
callq 0x1060
movl (%rax), %ebp
movl %ebp, %edi
callq 0x1200
movq %r14, %rdi
leaq 0x1b03(%rip), %rsi      # 0x3491
leaq 0x37bb(%rip), %rdx      # 0x5150
leaq 0x40(%rsp), %rcx
movl %ebp, %r8d
leaq 0x37ac(%rip), %rbp      # 0x5150
movq %rax, %r9
xorl %eax, %eax
callq 0x1120
cmpl $-0x1, 0x377f(%rip)     # 0x5134
jne 0x19c3
movq 0x381a(%rip), %rdi      # 0x51d8
callq 0x1210
movq 0x380e(%rip), %rdi      # 0x51d8
xorl %esi, %esi
callq 0x11f0
cmpl $-0x1, 0x375c(%rip)     # 0x5134
pushq $0x3
popq %r12
je 0x1a18
movq (%r15), %r14
callq 0x133c
movq %r14, %rdi
leaq 0x1b3a(%rip), %rsi      # 0x352a
movq %rbp, %rdx
leaq 0x1b47(%rip), %rcx      # 0x3541
leaq 0x40(%rsp), %r8
xorl %eax, %eax
callq 0x1120
movl 0x3728(%rip), %edi      # 0x5134
callq 0x10e0
orl $-0x1, 0x371c(%rip)     # 0x5134
movl 0x3712(%rip), %edi      # 0x5130
callq 0x10e0
cmpb $0x1, 0x37b6(%rip)      # 0x51e0
jne 0x1566
pushq $0x14
popq %rax
testl %eax, %eax
je 0x1a40
movl %eax, %edi
callq 0x1220
jmp 0x1a33
movb $0x0, 0x3799(%rip)      # 0x51e0
jmp 0x1566
xorl %r14d, %r14d
jmp 0x1a95
movq 0x3578(%rip), %rax      # 0x4fd0
movq (%rax), %rbx
callq 0x133c
callq 0x1060
movl (%rax), %ebp
movl %ebp, %edi
callq 0x1200
leaq 0x164d(%rip), %rsi      # 0x30c2
leaq 0x36d4(%rip), %rdx      # 0x5150
leaq 0x10(%rsp), %rcx
movq %rbx, %rdi
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x1120
pushq $-0x1
popq %r14
movl %r14d, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq