// File: /workspace/github_c_makefile_star2_O0/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
subq $0x19c0, %rsp           # imm = 0x19C0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x1040
leaq -0x128(%rbp), %rdi
xorl %esi, %esi
movl $0x98, %edx
callq 0x1040
leaq -0x1130(%rbp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x1040
leaq -0x1150(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x1040
leaq -0x19b0(%rbp), %rdi
xorl %esi, %esi
movl $0x844, %edx            # imm = 0x844
callq 0x1040
cmpl $0x3, -0x8(%rbp)
jge 0x2a48
leaq 0x1719(%rip), %rdi      # 0x414e
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2d22
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2800
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x2a77
leaq 0x17e1(%rip), %rdi      # 0x4245
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2d22
leaq -0x128(%rbp), %rdi
addq $0x8, %rdi
callq 0x1250
leaq -0x55e(%rip), %rax      # 0x2530
movq %rax, -0x128(%rbp)
movl $0x2, %edi
leaq -0x128(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x1190
cmpl $0x4, -0x8(%rbp)
jne 0x2ae6
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x11b0
cmpl $0x1, %eax
jne 0x2ae6
leaq -0x1150(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
movl $0x20, %esi
leaq 0x1781(%rip), %rdx      # 0x425e
movb $0x0, %al
callq 0x1050
jmp 0x2b08
leaq -0x1150(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
movl $0x20, %esi
leaq 0x176c(%rip), %rdx      # 0x426d
movb $0x0, %al
callq 0x1050
leaq -0x1130(%rbp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x1200
leaq -0x1130(%rbp), %rdi
leaq 0x1754(%rip), %rsi      # 0x427b
callq 0x11a0
leaq -0x1150(%rbp), %rax
movq %rax, -0x90(%rbp)
movl $0x4b0, -0x88(%rbp)     # imm = 0x4B0
leaq -0x1130(%rbp), %rax
movq %rax, -0x80(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x70(%rbp)
leaq 0x1ca(%rip), %rax       # 0x2d30
movq %rax, -0x68(%rbp)
movq $0x0, -0x60(%rbp)
leaq 0x247(%rip), %rax       # 0x2dc0
movq %rax, -0x58(%rbp)
movq $0x0, -0x50(%rbp)
leaq 0xa14(%rip), %rax       # 0x35a0
movq %rax, -0x48(%rbp)
movq $0x0, -0x40(%rbp)
leaq 0xd61(%rip), %rax       # 0x3900
movq %rax, -0x38(%rbp)
movq $0x0, -0x30(%rbp)
leaq 0x37d6(%rip), %rdi      # 0x6388
xorl %eax, %eax
movl %eax, %ecx
leaq -0x62d(%rip), %rdx      # 0x2590
movq %rcx, %rsi
callq 0x1110
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x2bd2
jmp 0x2c59
jmp 0x2bd4
leaq -0x90(%rbp), %rdi
callq 0x1240
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x19b1(%rbp)
je 0x2bff
cmpl $0x0, 0x3686(%rip)      # 0x627c
setne %al
movb %al, -0x19b1(%rbp)
movb -0x19b1(%rbp), %al
testb $0x1, %al
jne 0x2c0b
jmp 0x2c1c
callq 0x1150
movl $0x5, %edi
callq 0x1160
jmp 0x2bd4
leaq 0x376d(%rip), %rdi      # 0x6390
xorl %eax, %eax
movl %eax, %ecx
leaq -0x56e(%rip), %rdx      # 0x26c0
movq %rcx, %rsi
callq 0x1110
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x2c40
jmp 0x2c59
jmp 0x2c42
cmpl $0x0, 0x3633(%rip)      # 0x627c
je 0x2c57
movl $0x3, %edi
callq 0x1160
jmp 0x2c42
jmp 0x2c59
movl $0x0, -0x1154(%rbp)
movl -0x1154(%rbp), %eax
cmpl 0x3629(%rip), %eax      # 0x6298
jge 0x2d11
cmpl $0x0, 0x3704(%rip)      # 0x6380
je 0x2cfb
movq 0x360b(%rip), %rax      # 0x6290
movslq -0x1154(%rbp), %rcx
imulq $0x10a8, %rcx, %rcx     # imm = 0x10A8
addq %rcx, %rax
cmpl $0x0, 0x30(%rax)
je 0x2cfb
movq 0x35ed(%rip), %rdi      # 0x6290
movslq -0x1154(%rbp), %rax
imulq $0x10a8, %rax, %rax     # imm = 0x10A8
addq %rax, %rdi
movq 0x35d5(%rip), %rsi      # 0x6290
movslq -0x1154(%rbp), %rax
imulq $0x10a8, %rax, %rax     # imm = 0x10A8
addq %rax, %rsi
addq $0x10, %rsi
callq 0x11f0
cmpl $0x0, %eax
jne 0x2cf9
movq 0x35af(%rip), %rax      # 0x6290
movslq -0x1154(%rbp), %rcx
imulq $0x10a8, %rcx, %rcx     # imm = 0x10A8
addq %rcx, %rax
movl $0x0, 0x30(%rax)
jmp 0x2cfb
jmp 0x2cfd
movl -0x1154(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1154(%rbp)
jmp 0x2c63
callq 0x1150
callq 0x27c0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x19c0, %rsp           # imm = 0x19C0
popq %rbp
retq
nop