// File: /workspace/github_c_makefile_star2_O1/back1127[P]linkedge-thing-access-websocket_client_sdk/sdk/test/linux/simulated_device.c
// Function: main
int main(int argc, char **argv)
{
    leda_conn_info_t conn = {0};
    struct sigaction sig_int = {0};
    char path[PATH_MAX] = {0};
    char url[32] = {0};
    int i;
    char *pk, *dn;
    
    leda_device_data_t event_output[1] = {0};
    
    if (argc < 3) {
        printf("usage: ./simulated_device [$csv_path] [$ip] <$tls_switch>\n"\
                "    $csv_path is the path of csv file downloaded from IoT platform\n"\
                "    $ip is the ip address of Link IoT Edge\n"\
                "    $tls_switch is the tls switch, 0 close, 1 open, none is close for default\n");
        return -1;
    }

    if (0 != load_device_from_csv(argv[1])) {
        printf("load device list error!\n");
        return -1;
    }

    sigemptyset(&sig_int.sa_mask);
    sig_int.sa_handler = sigint_handler;
    sigaction(SIGINT, &sig_int, NULL);

    if (argc == 4 && atoi(argv[3]) == 1) {
        snprintf(url, sizeof(url), "wss://%s:17682", argv[2]);
    } else {
        snprintf(url, sizeof(url), "ws://%s:17682", argv[2]);
    }

    getcwd(path, PATH_MAX);
    strcat(path, "../cert/CA.cer");
    conn.url = url;
    conn.timeout = 20 * 60;
    conn.ca_path = path;
    conn.cert_path = NULL;
    conn.key_path = NULL;
    conn.ws_conn_cb.conn_state_change_cb = cb_state_changed;
    conn.ws_conn_cb.usr_data = NULL;
    conn.conn_devices_cb.get_properties_cb = cb_get_property;
    conn.conn_devices_cb.usr_data_get_property = NULL;
    conn.conn_devices_cb.set_properties_cb = cb_set_property;
    conn.conn_devices_cb.usr_data_set_property = NULL;
    conn.conn_devices_cb.call_service_cb = cb_call_service;
    conn.conn_devices_cb.usr_data_call_service = NULL;
    if (0 != pthread_create(&g_thread_online, NULL, thread_online, NULL)) {
        goto end;
    }

    while (leda_wsc_init(&conn) != LE_SUCCESS && g_running) {
        leda_wsc_exit();
        sleep(5);
        continue;
    }
#if 1
    if (0 != pthread_create(&g_thread_report, NULL, thread_report, NULL)) {
        goto end;
    }
#endif    
#if 0
    if (0 != pthread_create(&g_thread_property_monitor, NULL, property_monitor, NULL)) {
        goto end;
    }
#endif
    while (g_running) {
         sleep(3);
    }

end:    
    for (i = 0; i < g_devices_count; i++) {
        if (g_is_connected && g_devices[i].online) {
            if (leda_offline(g_devices[i].pk, g_devices[i].dn) == LE_SUCCESS) {
                g_devices[i].online = 0;
            }
        }
    }
    leda_wsc_exit();
    unload_device_list();

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x1140, %rsp           # imm = 0x1140
movq %rsi, %r14
movl %edi, %r15d
xorps %xmm0, %xmm0
movaps %xmm0, -0x30(%rbp)
movaps %xmm0, -0x40(%rbp)
movaps %xmm0, -0x50(%rbp)
movaps %xmm0, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
movaps %xmm0, -0xa0(%rbp)
leaq -0x158(%rbp), %rdi
movl $0x98, %edx
xorl %esi, %esi
callq 0x1040
leaq -0x1160(%rbp), %rbx
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1040
xorps %xmm0, %xmm0
movaps %xmm0, -0xb0(%rbp)
movaps %xmm0, -0xc0(%rbp)
cmpl $0x2, %r15d
jg 0x2433
leaq 0xdcc(%rip), %rdi       # 0x31fd
jmp 0x2447
movq 0x8(%r14), %rdi
callq 0x224c
testl %eax, %eax
je 0x2461
leaq 0xd9e(%rip), %rdi       # 0x31e5
callq 0x1080
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x1140, %rsp           # imm = 0x1140
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq -0x150(%rbp), %r12
movq %r12, %rdi
callq 0x1250
leaq -0x430(%rip), %rax      # 0x2047
movq %rax, -0x8(%r12)
leaq -0x158(%rbp), %rsi
movl $0x2, %edi
xorl %edx, %edx
callq 0x11a0
cmpl $0x4, %r15d
jne 0x24ac
movq 0x18(%r14), %rdi
callq 0x11b0
cmpl $0x1, %eax
jne 0x24ac
leaq 0xcb7(%rip), %rdx       # 0x3161
jmp 0x24b3
leaq 0xcbd(%rip), %rdx       # 0x3170
movq 0x10(%r14), %rcx
xorl %r15d, %r15d
leaq -0xc0(%rbp), %r14
movl $0x20, %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x1050
movl $0x1000, %esi           # imm = 0x1000
movq %rbx, %rdi
callq 0x1200
movq %rbx, %rdi
callq 0x1140
movabsq $0x2f747265632f2e2e, %rcx # imm = 0x2F747265632F2E2E
movq %rcx, -0x1160(%rbp,%rax)
movabsq $0x7265632e41432f, %rcx # imm = 0x7265632E41432F
movq %rcx, -0x1159(%rbp,%rax)
movq %r14, -0xa0(%rbp)
movl $0x4b0, -0x98(%rbp)     # imm = 0x4B0
movq %rbx, -0x90(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, -0x88(%rbp)
leaq 0x160(%rip), %rax       # 0x2692
movq %rax, -0x78(%rbp)
movq %r15, -0x70(%rbp)
leaq 0x1ad(%rip), %rax       # 0x26ee
movq %rax, -0x68(%rbp)
movq %r15, -0x60(%rbp)
leaq 0x5e6(%rip), %rax       # 0x2b36
movq %rax, -0x58(%rbp)
movq %r15, -0x50(%rbp)
leaq 0x85e(%rip), %rax       # 0x2dbd
movq %rax, -0x48(%rbp)
movq %r15, -0x40(%rbp)
leaq 0x2e1a(%rip), %rdi      # 0x5388
leaq -0x4ec(%rip), %rdx      # 0x2089
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x1120
testl %eax, %eax
je 0x2616
cmpl $0x0, 0x2d0b(%rip)      # 0x5298
jle 0x25e4
xorl %ebx, %ebx
xorl %r14d, %r14d
cmpl $0x0, 0x2de5(%rip)      # 0x5380
je 0x25ce
movq 0x2cec(%rip), %rdi      # 0x5290
cmpl $0x0, 0x30(%rdi,%rbx)
je 0x25ce
leaq (%rdi,%rbx), %rsi
addq $0x10, %rsi
addq %rbx, %rdi
callq 0x11f0
testl %eax, %eax
jne 0x25ce
movq 0x2cca(%rip), %rax      # 0x5290
movl $0x0, 0x30(%rax,%rbx)
incq %r14
movslq 0x2cc0(%rip), %rax      # 0x5298
addq $0x10a8, %rbx           # imm = 0x10A8
cmpq %rax, %r14
jl 0x2594
callq 0x1160
movq 0x2ca0(%rip), %rdi      # 0x5290
testq %rdi, %rdi
je 0x2605
callq 0x1130
movq $0x0, 0x2c8b(%rip) # 0x5290
movl $0x0, 0x2c89(%rip)      # 0x5298
xorl %eax, %eax
jmp 0x2451
leaq -0xa0(%rbp), %rdi
callq 0x1240
testl %eax, %eax
je 0x265a
cmpl $0x0, 0x2c4f(%rip)      # 0x527c
je 0x265a
leaq -0xa0(%rbp), %rbx
callq 0x1160
movl $0x5, %edi
callq 0x1170
movq %rbx, %rdi
callq 0x1240
testl %eax, %eax
je 0x265a
cmpl $0x0, 0x2c24(%rip)      # 0x527c
jne 0x2636
leaq 0x2d2f(%rip), %rdi      # 0x5390
leaq -0x501(%rip), %rdx      # 0x2167
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x1120
testl %eax, %eax
jne 0x2586
cmpl $0x0, 0x2bfc(%rip)      # 0x527c
je 0x2586
movl $0x3, %edi
callq 0x1170
jmp 0x2679