diag_basic_demo.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /*
  2. * 这个例程适用于`rtos`这类支持task的设备, 它演示了......
  3. *
  4. * 需要用户关注或修改的部分, 已经用 TODO 在注释中标明
  5. *
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "aiot_state_api.h"
  10. #include "aiot_sysdep_api.h"
  11. #include "aiot_mqtt_api.h"
  12. #include "aiot_dm_api.h"
  13. #include "aiot_diag_api.h"
  14. #include "ali_demo.h"
  15. #include "ql_api_osi.h"
  16. #include "ql_api_nw.h"
  17. #include "ali_demo.h"
  18. #include "ql_log.h"
  19. #include "ql_api_datacall.h"
  20. #include "sockets.h"
  21. #include "lwip/ip_addr.h"
  22. #include "lwip/ip6_addr.h"
  23. #include "lwip/netdb.h"
  24. #include "lwip/netif.h"
  25. #include "lwip/inet.h"
  26. #include "lwip/tcp.h"
  27. #define QL_SOCKET_LOG_LEVEL QL_LOG_LEVEL_INFO
  28. #define uart_printf(msg, ...) QL_LOG(QL_SOCKET_LOG_LEVEL, "linkSDK ", msg, ##__VA_ARGS__)
  29. #define diag_mqtt_profile_idx 1
  30. /* 位于portfiles/aiot_port文件夹下的系统适配函数集合 */
  31. extern aiot_sysdep_portfile_t g_aiot_sysdep_portfile;
  32. /* 位于external/ali_ca_cert.c中的服务器证书 */
  33. extern const char *ali_ca_cert;
  34. ql_task_t ql_alisdk_diag_task_ref;
  35. ql_task_t g_diag_mqtt_process_task_ref;
  36. ql_task_t g_diag_mqtt_recv_task_ref;
  37. static uint8_t g_diag_mqtt_process_task_ref_running = 0;
  38. static uint8_t g_diag_mqtt_recv_task_ref_running = 0;
  39. static int datacall_satrt(void)
  40. {
  41. int ret = 0;
  42. int i = 0;
  43. ql_data_call_info_s info;
  44. char ip4_addr_str[16] = {0};
  45. uint8_t nSim = 0;
  46. ql_rtos_task_sleep_s(10);
  47. uart_printf("========== socket demo start ==========");
  48. uart_printf("wait for network register done");
  49. while ((ret = ql_network_register_wait(nSim, 120)) != 0 && i < 10)
  50. {
  51. i++;
  52. ql_rtos_task_sleep_s(1);
  53. }
  54. if (ret == 0)
  55. {
  56. i = 0;
  57. uart_printf("====network registered!!!!====");
  58. }
  59. else
  60. {
  61. uart_printf("====network register failure!!!!!====");
  62. goto exit;
  63. }
  64. ql_set_data_call_asyn_mode(nSim, diag_mqtt_profile_idx, 0);
  65. uart_printf("===start data call====");
  66. ret = ql_start_data_call(nSim, diag_mqtt_profile_idx, QL_PDP_TYPE_IP, "uninet", NULL, NULL, 0);
  67. uart_printf("===data call result:%d", ret);
  68. if (ret != 0)
  69. {
  70. uart_printf("====data call failure!!!!=====");
  71. }
  72. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  73. ret = ql_get_data_call_info(nSim, diag_mqtt_profile_idx, &info);
  74. if (ret != 0)
  75. {
  76. uart_printf("ql_get_data_call_info ret: %d", ret);
  77. ql_stop_data_call(nSim, diag_mqtt_profile_idx);
  78. goto exit;
  79. }
  80. uart_printf("info->mqtt_profile_idx: %d", info.profile_idx);
  81. uart_printf("info->ip_version: %d", info.ip_version);
  82. uart_printf("info->v4.state: %d", info.v4.state);
  83. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  84. uart_printf("info.v4.addr.ip: %s\r\n", ip4_addr_str);
  85. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  86. uart_printf("info.v4.addr.pri_dns: %s\r\n", ip4_addr_str);
  87. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  88. uart_printf("info.v4.addr.sec_dns: %s\r\n", ip4_addr_str);
  89. return 0;
  90. exit:
  91. return -1;
  92. }
  93. /* TODO: 如果要关闭日志, 就把这个函数实现为空, 如果要减少日志, 可根据code选择不打印
  94. *
  95. * 例如: [1577589489.033][LK-0317] diag_basic_demo&a13FN5TplKq
  96. *
  97. * 上面这条日志的code就是0317(十六进制), code值的定义见core/aiot_state_api.h
  98. *
  99. */
  100. /* 日志回调函数, SDK的日志会从这里输出 */
  101. static int32_t demo_state_logcb(int32_t code, char *message)
  102. {
  103. uart_printf("%s", message);
  104. return 0;
  105. }
  106. /* MQTT事件回调函数, 当网络连接/重连/断开时被触发, 事件定义见core/aiot_mqtt_api.h */
  107. static void demo_mqtt_event_handler(void *handle, const aiot_mqtt_event_t *event, void *userdata)
  108. {
  109. switch (event->type) {
  110. /* SDK因为用户调用了aiot_mqtt_connect()接口, 与mqtt服务器建立连接已成功 */
  111. case AIOT_MQTTEVT_CONNECT: {
  112. uart_printf("AIOT_MQTTEVT_CONNECT\n");
  113. }
  114. break;
  115. /* SDK因为网络状况被动断连后, 自动发起重连已成功 */
  116. case AIOT_MQTTEVT_RECONNECT: {
  117. uart_printf("AIOT_MQTTEVT_RECONNECT\n");
  118. }
  119. break;
  120. /* SDK因为网络的状况而被动断开了连接, network是底层读写失败, heartbeat是没有按预期得到服务端心跳应答 */
  121. case AIOT_MQTTEVT_DISCONNECT: {
  122. char *cause = (event->data.disconnect == AIOT_MQTTDISCONNEVT_NETWORK_DISCONNECT) ? ("network disconnect") :
  123. ("heartbeat disconnect");
  124. uart_printf("AIOT_MQTTEVT_DISCONNECT: %s\n", cause);
  125. }
  126. break;
  127. default: {
  128. }
  129. }
  130. }
  131. /* MQTT默认消息处理回调, 当SDK从服务器收到MQTT消息时, 且无对应用户回调处理时被调用 */
  132. static void demo_mqtt_default_recv_handler(void *handle, const aiot_mqtt_recv_t *packet, void *userdata)
  133. {
  134. switch (packet->type) {
  135. case AIOT_MQTTRECV_HEARTBEAT_RESPONSE: {
  136. uart_printf("heartbeat response\n");
  137. }
  138. break;
  139. case AIOT_MQTTRECV_SUB_ACK: {
  140. uart_printf("suback, res: -0x%04X, packet id: %d, max qos: %d\n",
  141. -packet->data.sub_ack.res, packet->data.sub_ack.packet_id, packet->data.sub_ack.max_qos);
  142. }
  143. break;
  144. case AIOT_MQTTRECV_PUB: {
  145. uart_printf("pub, qos: %d, topic: %.*s\n", packet->data.pub.qos, packet->data.pub.topic_len, packet->data.pub.topic);
  146. uart_printf("pub, payload: %.*s\n", packet->data.pub.payload_len, packet->data.pub.payload);
  147. }
  148. break;
  149. case AIOT_MQTTRECV_PUB_ACK: {
  150. uart_printf("puback, packet id: %d\n", packet->data.pub_ack.packet_id);
  151. }
  152. break;
  153. default: {
  154. }
  155. }
  156. }
  157. /* 执行aiot_mqtt_process的线程, 包含心跳发送和QoS1消息重发 */
  158. static void demo_mqtt_process_thread(void *args)
  159. {
  160. int32_t res = STATE_SUCCESS;
  161. while (g_diag_mqtt_process_task_ref_running) {
  162. res = aiot_mqtt_process(args);
  163. if (res == STATE_USER_INPUT_EXEC_DISABLED) {
  164. break;
  165. }
  166. ql_rtos_task_sleep_s(1);
  167. }
  168. return;
  169. }
  170. /* 执行aiot_mqtt_recv的线程, 包含网络自动重连和从服务器收取MQTT消息 */
  171. static void demo_mqtt_recv_thread(void *args)
  172. {
  173. int32_t res = STATE_SUCCESS;
  174. while (g_diag_mqtt_recv_task_ref_running) {
  175. res = aiot_mqtt_recv(args);
  176. if (res < STATE_SUCCESS) {
  177. if (res == STATE_USER_INPUT_EXEC_DISABLED) {
  178. break;
  179. }
  180. ql_rtos_task_sleep_s(1);
  181. }
  182. }
  183. return;
  184. }
  185. static int32_t demo_mqtt_start(void *handle, char *product_key, char *device_name, char *device_secret)
  186. {
  187. int32_t res = STATE_SUCCESS;
  188. void *mqtt_handle = handle;
  189. char *url = "iot-as-mqtt.cn-shanghai.aliyuncs.com"; /* 阿里云平台上海站点的域名后缀 */
  190. char host[100] = {0}; /* 用这个数组拼接设备连接的云平台站点全地址, 规则是 ${productKey}.iot-as-mqtt.cn-shanghai.aliyuncs.com */
  191. uint16_t port = 443; /* 无论设备是否使用TLS连接阿里云平台, 目的端口都是443 */
  192. aiot_sysdep_network_cred_t cred; /* 安全凭据结构体, 如果要用TLS, 这个结构体中配置CA证书等参数 */
  193. /* 创建SDK的安全凭据, 用于建立TLS连接 */
  194. memset(&cred, 0, sizeof(aiot_sysdep_network_cred_t));
  195. cred.option = AIOT_SYSDEP_NETWORK_CRED_SVRCERT_CA; /* 使用RSA证书校验MQTT服务端 */
  196. cred.max_tls_fragment = 16384; /* 最大的分片长度为16K, 其它可选值还有4K, 2K, 1K, 0.5K */
  197. cred.sni_enabled = 1; /* TLS建连时, 支持Server Name Indicator */
  198. cred.x509_server_cert = ali_ca_cert; /* 用来验证MQTT服务端的RSA根证书 */
  199. cred.x509_server_cert_len = strlen(ali_ca_cert); /* 用来验证MQTT服务端的RSA根证书长度 */
  200. /* TODO: 如果以下代码不被注释, 则例程会用TCP而不是TLS连接云平台 */
  201. /*
  202. {
  203. memset(&cred, 0, sizeof(aiot_sysdep_network_cred_t));
  204. cred.option = AIOT_SYSDEP_NETWORK_CRED_NONE;
  205. }
  206. */
  207. snprintf(host, 100, "%s.%s", product_key, url);
  208. /* 配置MQTT服务器地址 */
  209. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_HOST, (void *)host);
  210. /* 配置MQTT服务器端口 */
  211. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_PORT, (void *)&port);
  212. int pdp = diag_mqtt_profile_idx;
  213. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_PROFILE_IDX, (void *)&pdp);
  214. /* 配置设备productKey */
  215. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_PRODUCT_KEY, (void *)product_key);
  216. /* 配置设备deviceName */
  217. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_DEVICE_NAME, (void *)device_name);
  218. /* 配置设备deviceSecret */
  219. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_DEVICE_SECRET, (void *)device_secret);
  220. /* 配置网络连接的安全凭据, 上面已经创建好了 */
  221. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_NETWORK_CRED, (void *)&cred);
  222. /* 配置MQTT默认消息接收回调函数 */
  223. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_RECV_HANDLER, (void *)demo_mqtt_default_recv_handler);
  224. /* 配置MQTT事件回调函数 */
  225. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_EVENT_HANDLER, (void *)demo_mqtt_event_handler);
  226. /* 配置MQTT连接超时时间 */
  227. int time_delay_ms = 5000;
  228. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_CONNECT_TIMEOUT_MS, (void *)&time_delay_ms);
  229. /* 与服务器建立MQTT连接 */
  230. res = aiot_mqtt_connect(mqtt_handle);
  231. if (res < STATE_SUCCESS) {
  232. /* 尝试建立连接失败, 销毁MQTT实例, 回收资源 */
  233. aiot_mqtt_deinit(&mqtt_handle);
  234. uart_printf("aiot_mqtt_connect failed: -0x%04X\n", -res);
  235. return -1;
  236. }
  237. /* 创建一个单独的线程, 专用于执行aiot_mqtt_process, 它会自动发送心跳保活, 以及重发QoS1的未应答报文 */
  238. g_diag_mqtt_process_task_ref_running = 1;
  239. res = ql_rtos_task_create(&g_diag_mqtt_process_task_ref, 24000, 24, "client_proc", demo_mqtt_process_thread, (void*)mqtt_handle, 5);
  240. if (res < 0) {
  241. uart_printf("pthread_create demo_mqtt_process_thread failed: %d\n", res);
  242. aiot_mqtt_deinit(&mqtt_handle);
  243. return -1;
  244. }
  245. /* 创建一个单独的线程用于执行aiot_mqtt_recv, 它会循环收取服务器下发的MQTT消息, 并在断线时自动重连 */
  246. g_diag_mqtt_recv_task_ref_running = 1;
  247. res = ql_rtos_task_create(&g_diag_mqtt_recv_task_ref, 24000, 25, "recv_test", demo_mqtt_recv_thread, (void*)mqtt_handle, 5);
  248. if (res < 0) {
  249. uart_printf("pthread_create demo_mqtt_recv_thread failed: %d\n", res);
  250. g_diag_mqtt_process_task_ref_running = 0;
  251. aiot_mqtt_deinit(&mqtt_handle);
  252. return -1;
  253. }
  254. return 0;
  255. }
  256. static int32_t demo_mqtt_stop(void **handle)
  257. {
  258. int32_t res = STATE_SUCCESS;
  259. void *mqtt_handle = NULL;
  260. mqtt_handle = *handle;
  261. g_diag_mqtt_process_task_ref_running = 0;
  262. g_diag_mqtt_recv_task_ref_running = 0;
  263. // pthread_join(g_diag_mqtt_process_task_ref, NULL);
  264. // pthread_join(g_diag_mqtt_recv_task_ref, NULL);
  265. ql_rtos_task_delete(g_diag_mqtt_process_task_ref);
  266. ql_rtos_task_delete(g_diag_mqtt_recv_task_ref);
  267. /* 断开MQTT连接 */
  268. res = aiot_mqtt_disconnect(mqtt_handle);
  269. if (res < STATE_SUCCESS) {
  270. aiot_mqtt_deinit(&mqtt_handle);
  271. uart_printf("aiot_mqtt_disconnect failed: -0x%04X\n", -res);
  272. return -1;
  273. }
  274. /* 销毁MQTT实例 */
  275. res = aiot_mqtt_deinit(&mqtt_handle);
  276. if (res < STATE_SUCCESS) {
  277. uart_printf("aiot_mqtt_deinit failed: -0x%04X\n", -res);
  278. return -1;
  279. }
  280. return 0;
  281. }
  282. /* 用户数据接收处理回调函数 */
  283. static void demo_dm_recv_handler(void *dm_handle, const aiot_dm_recv_t *recv, void *userdata)
  284. {
  285. uart_printf("demo_dm_recv_handler, type = %d\r\n", recv->type);
  286. switch (recv->type) {
  287. /* 属性上报, 事件上报, 获取期望属性值或者删除期望属性值的应答 */
  288. case AIOT_DMRECV_GENERIC_REPLY: {
  289. uart_printf("msg_id = %d, code = %d, data = %.*s, message = %.*s\r\n",
  290. recv->data.generic_reply.msg_id,
  291. recv->data.generic_reply.code,
  292. recv->data.generic_reply.data_len,
  293. recv->data.generic_reply.data,
  294. recv->data.generic_reply.message_len,
  295. recv->data.generic_reply.message);
  296. }
  297. break;
  298. /* 属性设置 */
  299. case AIOT_DMRECV_PROPERTY_SET: {
  300. uart_printf("msg_id = %ld, params = %.*s\r\n",
  301. (unsigned long)recv->data.property_set.msg_id,
  302. recv->data.property_set.params_len,
  303. recv->data.property_set.params);
  304. /* TODO: 以下代码演示如何对来自云平台的属性设置指令进行应答, 用户可取消注释查看演示效果 */
  305. /*
  306. {
  307. aiot_dm_msg_t msg;
  308. memset(&msg, 0, sizeof(aiot_dm_msg_t));
  309. msg.type = AIOT_DMMSG_PROPERTY_SET_REPLY;
  310. msg.data.property_set_reply.msg_id = recv->data.property_set.msg_id;
  311. msg.data.property_set_reply.code = 200;
  312. msg.data.property_set_reply.data = "{}";
  313. int32_t res = aiot_dm_send(dm_handle, &msg);
  314. if (res < 0) {
  315. uart_printf("aiot_dm_send failed\r\n");
  316. }
  317. }
  318. */
  319. }
  320. break;
  321. /* 异步服务调用 */
  322. case AIOT_DMRECV_ASYNC_SERVICE_INVOKE: {
  323. uart_printf("msg_id = %ld, service_id = %s, params = %.*s\r\n",
  324. (unsigned long)recv->data.async_service_invoke.msg_id,
  325. recv->data.async_service_invoke.service_id,
  326. recv->data.async_service_invoke.params_len,
  327. recv->data.async_service_invoke.params);
  328. /* TODO: 以下代码演示如何对来自云平台的异步服务调用进行应答, 用户可取消注释查看演示效果
  329. *
  330. * 注意: 如果用户在回调函数外进行应答, 需要自行保存msg_id, 因为回调函数入参在退出回调函数后将被SDK销毁, 不可以再访问到
  331. */
  332. /*
  333. {
  334. aiot_dm_msg_t msg;
  335. memset(&msg, 0, sizeof(aiot_dm_msg_t));
  336. msg.type = AIOT_DMMSG_ASYNC_SERVICE_REPLY;
  337. msg.data.async_service_reply.msg_id = recv->data.async_service_invoke.msg_id;
  338. msg.data.async_service_reply.code = 200;
  339. msg.data.async_service_reply.service_id = "ToggleLightSwitch";
  340. msg.data.async_service_reply.data = "{\"dataA\": 20}";
  341. int32_t res = aiot_dm_send(dm_handle, &msg);
  342. if (res < 0) {
  343. uart_printf("aiot_dm_send failed\r\n");
  344. }
  345. }
  346. */
  347. }
  348. break;
  349. /* 同步服务调用 */
  350. case AIOT_DMRECV_SYNC_SERVICE_INVOKE: {
  351. uart_printf("msg_id = %ld, rrpc_id = %s, service_id = %s, params = %.*s\r\n",
  352. (unsigned long)recv->data.sync_service_invoke.msg_id,
  353. recv->data.sync_service_invoke.rrpc_id,
  354. recv->data.sync_service_invoke.service_id,
  355. recv->data.sync_service_invoke.params_len,
  356. recv->data.sync_service_invoke.params);
  357. /* TODO: 以下代码演示如何对来自云平台的同步服务调用进行应答, 用户可取消注释查看演示效果
  358. *
  359. * 注意: 如果用户在回调函数外进行应答, 需要自行保存msg_id和rrpc_id字符串, 因为回调函数入参在退出回调函数后将被SDK销毁, 不可以再访问到
  360. */
  361. /*
  362. {
  363. aiot_dm_msg_t msg;
  364. memset(&msg, 0, sizeof(aiot_dm_msg_t));
  365. msg.type = AIOT_DMMSG_SYNC_SERVICE_REPLY;
  366. msg.data.sync_service_reply.rrpc_id = recv->data.sync_service_invoke.rrpc_id;
  367. msg.data.sync_service_reply.msg_id = recv->data.sync_service_invoke.msg_id;
  368. msg.data.sync_service_reply.code = 200;
  369. msg.data.sync_service_reply.service_id = "SetLightSwitchTimer";
  370. msg.data.sync_service_reply.data = "{}";
  371. int32_t res = aiot_dm_send(dm_handle, &msg);
  372. if (res < 0) {
  373. uart_printf("aiot_dm_send failed\r\n");
  374. }
  375. }
  376. */
  377. }
  378. break;
  379. /* 下行二进制数据 */
  380. case AIOT_DMRECV_RAW_DATA: {
  381. uart_printf("raw data len = %d\r\n", recv->data.raw_data.data_len);
  382. /* TODO: 以下代码演示如何发送二进制格式数据, 若使用需要有相应的数据透传脚本部署在云端 */
  383. /*
  384. {
  385. aiot_dm_msg_t msg;
  386. uint8_t raw_data[] = {0x01, 0x02};
  387. memset(&msg, 0, sizeof(aiot_dm_msg_t));
  388. msg.type = AIOT_DMMSG_RAW_DATA;
  389. msg.data.raw_data.data = raw_data;
  390. msg.data.raw_data.data_len = sizeof(raw_data);
  391. aiot_dm_send(dm_handle, &msg);
  392. }
  393. */
  394. }
  395. break;
  396. /* 二进制格式的同步服务调用, 比单纯的二进制数据消息多了个rrpc_id */
  397. case AIOT_DMRECV_RAW_SYNC_SERVICE_INVOKE: {
  398. uart_printf("raw sync service rrpc_id = %s, data_len = %d\r\n",
  399. recv->data.raw_service_invoke.rrpc_id,
  400. recv->data.raw_service_invoke.data_len);
  401. }
  402. break;
  403. default:
  404. break;
  405. }
  406. }
  407. static int32_t demo_dm_start(void **handle, void *mqtt_handle, char *product_key, char *device_name)
  408. {
  409. void *dm_handle = NULL;
  410. dm_handle = aiot_dm_init();
  411. if (dm_handle == NULL) {
  412. return -1;
  413. }
  414. /* 配置MQTT实例句柄 */
  415. aiot_dm_setopt(dm_handle, AIOT_DMOPT_MQTT_HANDLE, mqtt_handle);
  416. /* 配置消息接收处理回调函数 */
  417. aiot_dm_setopt(dm_handle, AIOT_DMOPT_RECV_HANDLER, (void *)demo_dm_recv_handler);
  418. *handle = dm_handle;
  419. return STATE_SUCCESS;
  420. }
  421. static int32_t demo_dm_stop(void **handle)
  422. {
  423. int32_t res = STATE_SUCCESS;
  424. /* 销毁DATA-MODEL实例, 一般不会运行到这里 */
  425. res = aiot_dm_deinit(handle);
  426. if (res < STATE_SUCCESS) {
  427. uart_printf("aiot_dm_deinit failed: -0x%04X\n", -res);
  428. return -1;
  429. }
  430. return STATE_SUCCESS;
  431. }
  432. /* 事件回调函数, 当网络连接/重连/断开时被触发, 事件定义见core/aiot_diag_api.h */
  433. static void demo_diag_event_handler(void *handle, const aiot_diag_event_t *event, void *userdata)
  434. {
  435. switch (event->type) {
  436. case AIOT_DIAGEVT_ALERT: {
  437. uart_printf("AIOT_DIAGEVT_ALERT, module name: %s, level: %s, desc: %s\n",
  438. event->data.alert.module_name, event->data.alert.level, event->data.alert.desc);
  439. }
  440. break;
  441. default: {
  442. }
  443. }
  444. }
  445. /* 数据处理回调, 当SDK从网络上收到diag消息时被调用 */
  446. // static void demo_diag_recv_handler(void *handle, const aiot_diag_recv_t *packet, void *userdata)
  447. // {
  448. // switch (packet->type) {
  449. // case AIOT_DIAGRECV_DIAG_CONTROL: {
  450. // }
  451. // break;
  452. // default: {
  453. //
  454. // }
  455. // }
  456. // }
  457. static void ali_diag_test(void *arg)
  458. {
  459. int32_t res = STATE_SUCCESS;
  460. void *mqtt_handle = NULL, *diag_handle = NULL, *dm_handle = NULL;
  461. /* TODO: 替换为自己设备的三元组 */
  462. char *product_key = "a1TSu2F29co";
  463. char *device_name = "000001";
  464. char *device_secret = "bd220c3a9c6a5b3f5921485d091497e1";
  465. /*执行拨号操作,设备联网*/
  466. if(datacall_satrt()!=0)
  467. goto exit;
  468. //return;
  469. /* 配置SDK的底层依赖 */
  470. aiot_sysdep_set_portfile(&g_aiot_sysdep_portfile);
  471. /* 配置SDK的日志输出 */
  472. aiot_state_set_logcb(demo_state_logcb);
  473. /* 创建1个MQTT客户端实例并内部初始化默认参数 */
  474. mqtt_handle = aiot_mqtt_init();
  475. if (mqtt_handle == NULL) {
  476. uart_printf("aiot_mqtt_init failed\n");
  477. goto exit;
  478. //return;
  479. }
  480. diag_handle = aiot_diag_init();
  481. if (diag_handle == NULL) {
  482. uart_printf("aiot_diag_init failed\n");
  483. goto exit;
  484. //return;
  485. }
  486. aiot_diag_setopt(diag_handle, AIOT_DIAGOPT_MQTT_HANDLE, mqtt_handle);
  487. aiot_diag_setopt(diag_handle, AIOT_DIAGOPT_EVENT_HANDLER, demo_diag_event_handler);
  488. aiot_diag_start(diag_handle);
  489. /* 建立MQTT连接, 并开启保活线程和接收线程 */
  490. res = demo_mqtt_start(mqtt_handle, product_key, device_name, device_secret);
  491. if (res < 0) {
  492. uart_printf("demo_mqtt_start failed\n");
  493. goto exit;
  494. //return;
  495. }
  496. res = demo_dm_start(&dm_handle, mqtt_handle, product_key, device_name);
  497. if (res < 0) {
  498. uart_printf("demo_dm_start failed\n");
  499. goto exit;
  500. //return;
  501. }
  502. /* {
  503. aiot_dm_msg_t msg;
  504. memset(&msg, 0, sizeof(aiot_dm_msg_t));
  505. msg.type = AIOT_DMMSG_PROPERTY_POST;
  506. msg.data.property_post.params = "{\"LightSwitch\":1}";
  507. res = aiot_dm_send(dm_handle, &msg);
  508. } */
  509. while(1) {
  510. ql_rtos_task_sleep_s(1);
  511. }
  512. exit:
  513. /* 销毁dm实例, 一般不会运行到这里 */
  514. demo_dm_stop(&dm_handle);
  515. /* 销毁diag实例, 一般不会运行到这里 */
  516. aiot_diag_deinit(&diag_handle);
  517. /* 销毁MQTT实例, 退出线程, 一般不会运行到这里 */
  518. demo_mqtt_stop(&mqtt_handle);
  519. return;
  520. }
  521. #define QL_ALI_TASK_STACK_SIZE 10240
  522. #define QL_ALI_TASK_PRIO APP_PRIORITY_NORMAL
  523. #define QL_ALI_TASK_EVENT_CNT 5
  524. int ql_ali_sdk_diag_test_init(void)
  525. {
  526. QlOSStatus status;
  527. int count = 0;
  528. /* 主体进入休眠 */
  529. while (count < 10)
  530. {
  531. count++;
  532. ql_rtos_task_sleep_s(1);
  533. uart_printf("ql_ali_sdk_mqtt_client_test_init -0x%04X\n", count);
  534. }
  535. status =ql_rtos_task_create(&ql_alisdk_diag_task_ref, QL_ALI_TASK_STACK_SIZE, APP_PRIORITY_NORMAL, "diag_test", ali_diag_test, NULL, QL_ALI_TASK_EVENT_CNT);
  536. if (status != QL_OSI_SUCCESS)
  537. {
  538. uart_printf("ql_ali_sdk_diag_test_init error");
  539. return -1;
  540. }
  541. return 0;
  542. }