Business process overview
This topic uses the ./demos/tunnel_switch_demo.c demo file from the C Link software development kit (SDK) as an example to describe the business process and its logs when the business process is isolated from the remote logon process.
Business process flowchart

Procedure
Initialize the remote logon tunnel switch module.
/* Create an RA instance and initialize the default parameters internally. */ void *tunnel_switch_handle = aiot_tunnel_switch_init(); if (tunnel_switch_handle == NULL) { printf( "aiot_tunnel_switch_init failed\n"); return -1; }Configure features.
Associate the MQTT module. Ensure that the MQTT module is connected. For more information, see MQTT connection.
Configure the message callback function.
This function parses tunnel-related messages and synchronizes them with the O&M process.
/* Configure the MQTT handle. The RA module subscribes to MQTT messages internally. */ aiot_tunnel_switch_setopt(tunnel_switch_handle, AIOT_TSOPT_MQTT_HANDLE, mqtt_handle);/* Configure the message callback function. */ aiot_tunnel_switch_setopt(tunnel_switch_handle, AIOT_TSOPT_RECV_HANDLER, demo_tunnel_switch_recv_handle);Request tunnel connection information.
NoteThe requested tunnel information expires after seven days. For more information, see Remote logon to IoT Platform.
/* The device actively requests to establish a tunnel. This tunnel is used only for remote logon. */ aiot_tunnel_switch_request(tunnel_switch_handle);Send information to the O&M process using inter-process communication.
The following example uses Linux message queues for inter-process communication. You can also use other inter-process communication methods.
static void demo_tunnel_switch_recv_handle(void *handle, const aiot_tunnel_switch_recv_data_t *recv, void *userdata) { demo_tunnel_info_t info; memset(&info, 0, sizeof(info)); if(recv->operation != AIOT_TUNNEL_OPERATOPN_CLOSE) { /* Print the expiration time of the tunnel authentication. If the tunnel is about to expire, send a request in advance. */ printf("tunnel %s, create time %"PRIu64" , will expired after %"PRIu64" seconds \r\n", recv->tunnel_id, recv->created_time, recv->expired_time); } else { printf("tunnel %s close , close reason %s \r\n", recv->tunnel_id, recv->close_reason); } info.operation = recv->operation; if(recv->tunnel_id != NULL) { memcpy(info.tunnel_id, recv->tunnel_id, strlen(recv->tunnel_id)); } if(recv->host != NULL) { memcpy(info.host, recv->host, strlen(recv->host)); } if(recv->port != NULL) { memcpy(info.port, recv->port, strlen(recv->port)); } if(recv->path != NULL) { memcpy(info.path, recv->path, strlen(recv->path)); } if(recv->token != NULL) { memcpy(info.token, recv->token, strlen(recv->token)); } /* Send the tunnel operation and connection information to the tunnel business process. Inter-process communication can also be implemented by storing the information in a file. */ demo_posix_sysmsg_send((char *)&info, sizeof(info)); }
Operational logs
Establishing an MQTT connection.
[1644818817.044][LK-0313] MQTT user calls aiot_mqtt_connect api, connect [1644818817.044][LK-032A] mqtt host: iot-test-*************.unify.aliyuncs.com [1644818817.044][LK-0317] user name: tunnel_test_00**********LXZe [1644818817.044][LK-0318] password: 89EF89F414F1369*******************B18B39A6B63A3EFB319A0C408 success to establish tcp, fd=3 local port: 35024 [1644818817.133][LK-1000] establish mbedtls connection with server(host='iot-test-************.unify.aliyuncs.com', port=[443]) [1644818817.144][LK-1000] success to establish mbedtls connection, (cost 45247 bytes in total, max used 47983 bytes) [1644818817.611][LK-0313] MQTT connect success in 567 ms AIOT_MQTTEVT_CONNECTThe tunnel switch module has been initialized.
[1644818817.611][LK-0309] sub: /sys/a1******XZe/tunnel_test_001/secure_tunnel/notify [1644818817.611][LK-0309] sub: /sys/a1******XZe/tunnel_test_001/secure_tunnel/proxy/request_reply heartbeat response suback, res: -0x0000, packet id: 2, max qos: 1 suback, res: -0x0000, packet id: 1, max qos: 1Requesting tunnel connection information.
[1644818817.611][LK-0309] pub: /sys/a1W*******XZe/tunnel_test_001/secure_tunnel/proxy/request [LK-030A] > 7B 22 69 64 22 3A 31 30 30 30 2C 22 70 61 72 61 | {"id":1000,"para [LK-030A] > 6D 73 22 3A 7B 7D 7D | ms":{}}Replying with tunnel connection information.
[1644818818.122][LK-0309] pub: /sys/a1******XZe/tunnel_test_001/secure_tunnel/proxy/request_reply [LK-030A] < 7B 22 63 6F 64 65 22 3A 32 30 30 2C 22 64 61 74 | {"code":200,"dat [LK-030A] < 61 22 3A 7B 22 73 63 68 65 6D 61 22 3A 22 77 73 | a":{"schema":"ws [LK-030A] < 73 22 2C 22 70 61 74 68 22 3A 22 2F 74 75 6E 6E | s","path":"/tunn [LK-030A] < 65 6C 2F 34 61 32 66 63 39 39 62 2D 38 32 66 39 | el/4a2f********9 [LK-030A] < 2D 34 36 65 65 2D 62 34 31 63 2D 34 63 36 30 31 | -46ee-b41c-4c601 [LK-030A] < 36 33 30 65 35 31 61 2F 64 65 73 74 22 2C 22 74 | 630e51a/dest","t [LK-030A] < 6F 6B 65 6E 5F 65 78 70 69 72 65 22 3A 36 30 34 | oken_expire":604 [LK-030A] < 37 39 39 2C 22 74 75 6E 6E 65 6C 5F 69 64 22 3A | 799,"tunnel_id": [LK-030A] < 22 34 61 32 66 63 39 39 62 2D 38 32 66 39 2D 34 | "4a2fc********** [LK-030A] < 36 65 65 2D 62 34 31 63 2D 34 63 36 30 31 36 33 | 6ee-b41c-4c60163 [LK-030A] < 30 65 35 31 61 22 2C 22 70 61 79 6C 6F 61 64 5F | 0e51a","payload_ [LK-030A] < 6D 6F 64 65 22 3A 22 61 70 70 2D 70 72 6F 78 79 | mode":"app-proxy [LK-030A] < 22 2C 22 70 6F 72 74 22 3A 34 34 33 2C 22 68 6F | ","port":443,"ho [LK-030A] < 73 74 22 3A 22 69 6F 74 2D 73 65 63 75 72 65 2D | st":"iot-secure- [LK-030A] < 74 75 6E 6E 65 6C 2D 68 7A 2D 70 72 65 2E 61 6C | tunnel-hz-pre.al [LK-030A] < 69 79 75 6E 63 73 2E 63 6F 6D 22 2C 22 75 72 69 | iyuncs.com","uri [LK-030A] < 22 3A 22 77 73 73 3A 2F 2F 69 6F 74 2D 73 65 63 | ":"wss://iot-sec [LK-030A] < 75 72 65 2D 74 75 6E 6E 65 6C 2D 68 7A 2D 70 72 | ure-tunnel-hz-pr [LK-030A] < 65 2E 61 6C 69 79 75 6E 63 73 2E 63 6F 6D 2F 74 | e.aliyuncs.com/t [LK-030A] < 75 6E 6E 65 6C 2F 34 61 32 66 63 39 39 62 2D 38 | unnel/4a2******* [LK-030A] < 32 66 39 2D 34 36 65 65 2D 62 34 31 63 2D 34 63 | 2f9-46ee-b41c-4c [LK-030A] < 36 30 31 36 33 30 65 35 31 61 2F 64 65 73 74 22 | 601630e51a/dest" [LK-030A] < 2C 22 74 6F 6B 65 6E 22 3A 22 32 30 39 34 65 61 | ,"token":"2094ea [LK-030A] < 37 37 2D 34 32 61 38 2D 34 32 37 32 2D 38 63 38 | 77-42a8-4272-8c8 [LK-030A] < 65 2D 62 37 61 65 36 64 37 39 65 62 30 36 22 7D | e-b7**********6"} [LK-030A] < 2C 22 69 64 22 3A 22 31 30 30 30 22 2C 22 6D 65 | ,"id":"1000","me [LK-030A] < 73 73 61 67 65 22 3A 22 73 75 63 63 65 73 73 22 | ssage":"success" [LK-030A] < 7D | } [1644818818.122][LK-0000] tunnel_switch_topic_handler payload:{"code":200,"data":{"schema":"wss","path":"/tunnel/4a2f************ee-b41c-4c601630e51a/dest","tok tunnel 4a2f***********46ee-b41c-4c601630e51a, create time 1644818818127 , will expired after 604799 secondsSending instructions using inter-process communication.
send msgsnd 644