// File: /workspace/github_c_makefile_star2_O0/back1127[P]linkedge-thing-access-websocket_client_sdk/demo/linux/demo.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;
    
    if (argc < 3) {
        printf("usage: ./demo [$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, 32, "wss://%s:17682", argv[2]);
    } else {
        snprintf(url, 32, "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;
    conn.conn_devices_cb.report_reply_cb = NULL;
    conn.conn_devices_cb.usr_data_report_reply = NULL;
    conn.conn_devices_cb.service_output_max_count = 0;
    
    if (0 != pthread_create(&g_thread_online, NULL, thread_online, NULL)) {
        goto end;
    }

    while (leda_wsc_init(&conn) != LE_SUCCESS) {
        leda_wsc_exit();
        sleep(5);
        continue;
    }
    
    if (0 != pthread_create(&g_thread_report, NULL, thread_report, NULL)) {
        goto end;
    }
    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 $0x1170, %rsp           # imm = 0x1170
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
cmpl $0x3, -0x8(%rbp)
jge 0x1a95
leaq 0x1609(%rip), %rdi      # 0x308b
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1d51
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x1840
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x1ac4
leaq 0x16c5(%rip), %rdi      # 0x3176
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1d51
leaq -0x128(%rbp), %rdi
addq $0x8, %rdi
callq 0x1210
leaq -0x2fb(%rip), %rax      # 0x17e0
movq %rax, -0x128(%rbp)
movl $0x2, %edi
leaq -0x128(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x1150
cmpl $0x4, -0x8(%rbp)
jne 0x1b33
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x1170
cmpl $0x1, %eax
jne 0x1b33
leaq -0x1150(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
movl $0x20, %esi
leaq 0x1665(%rip), %rdx      # 0x318f
movb $0x0, %al
callq 0x1050
jmp 0x1b55
leaq -0x1150(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
movl $0x20, %esi
leaq 0x1650(%rip), %rdx      # 0x319e
movb $0x0, %al
callq 0x1050
leaq -0x1130(%rbp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x11c0
leaq -0x1130(%rbp), %rdi
leaq 0x1638(%rip), %rsi      # 0x31ac
callq 0x1160
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 0x1ad(%rip), %rax       # 0x1d60
movq %rax, -0x68(%rbp)
movq $0x0, -0x60(%rbp)
leaq 0x22a(%rip), %rax       # 0x1df0
movq %rax, -0x58(%rbp)
movq $0x0, -0x50(%rbp)
leaq 0x377(%rip), %rax       # 0x1f50
movq %rax, -0x48(%rbp)
movq $0x0, -0x40(%rbp)
leaq 0x4a4(%rip), %rax       # 0x2090
movq %rax, -0x38(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x20(%rbp)
movl $0x0, -0x18(%rbp)
leaq 0x3592(%rip), %rdi      # 0x51a8
xorl %eax, %eax
movl %eax, %ecx
leaq -0x721(%rip), %rdx      # 0x1500
movq %rcx, %rsi
callq 0x10e0
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x1c33
jmp 0x1c94
jmp 0x1c35
leaq -0x90(%rbp), %rdi
callq 0x1200
cmpl $0x0, %eax
je 0x1c57
callq 0x1110
movl $0x5, %edi
callq 0x1120
jmp 0x1c35
leaq 0x3552(%rip), %rdi      # 0x51b0
xorl %eax, %eax
movl %eax, %ecx
leaq -0x669(%rip), %rdx      # 0x1600
movq %rcx, %rsi
callq 0x10e0
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x1c7b
jmp 0x1c94
jmp 0x1c7d
cmpl $0x0, 0x34ac(%rip)      # 0x5130
je 0x1c92
movl $0x3, %edi
callq 0x1120
jmp 0x1c7d
jmp 0x1c94
movl $0x0, -0x1154(%rbp)
movl -0x1154(%rbp), %eax
cmpl 0x34ee(%rip), %eax      # 0x5198
jge 0x1d40
cmpl $0x0, 0x34e5(%rip)      # 0x519c
je 0x1d2a
movq 0x34e0(%rip), %rax      # 0x51a0
movslq -0x1154(%rbp), %rcx
imulq $0x3c, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x30(%rax)
je 0x1d2a
movq 0x34c5(%rip), %rdi      # 0x51a0
movslq -0x1154(%rbp), %rax
imulq $0x3c, %rax, %rax
addq %rax, %rdi
movq 0x34b0(%rip), %rsi      # 0x51a0
movslq -0x1154(%rbp), %rax
imulq $0x3c, %rax, %rax
addq %rax, %rsi
addq $0x10, %rsi
callq 0x11b0
cmpl $0x0, %eax
jne 0x1d28
movq 0x348d(%rip), %rax      # 0x51a0
movslq -0x1154(%rbp), %rcx
imulq $0x3c, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x30(%rax)
jmp 0x1d2a
jmp 0x1d2c
movl -0x1154(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1154(%rbp)
jmp 0x1c9e
callq 0x1110
callq 0x19d0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1170, %rsp           # imm = 0x1170
popq %rbp
retq
nopl (%rax)