devinfo_posix_demo.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * 这个例程适用于`RTOS`这类支持Task的设备, 它演示了用SDK配置MQTT参数并建立连接, 之后创建2个线程
  3. *
  4. * + 一个线程用于保活长连接
  5. * + 一个线程用于接收消息, 并在有消息到达时进入默认的数据回调, 在连接状态变化时进入事件回调
  6. *
  7. * 接着在MQTT连接上发送设备标签更新或删除请求, 如果云平台的回应报文到达, 从接收线程会调用devinfo消息处理的回调函数, 把上报结果打印出来
  8. *
  9. * 需要用户关注或修改的部分, 已经用 TODO 在注释中标明
  10. *
  11. */
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "aiot_state_api.h"
  15. #include "aiot_sysdep_api.h"
  16. #include "aiot_mqtt_api.h"
  17. #include "aiot_devinfo_api.h"
  18. #include "ali_demo.h"
  19. #include "ql_api_osi.h"
  20. #include "ql_api_nw.h"
  21. #include "ali_demo.h"
  22. #include "ql_log.h"
  23. #include "ql_api_datacall.h"
  24. #include "sockets.h"
  25. #include "lwip/ip_addr.h"
  26. #include "lwip/ip6_addr.h"
  27. #include "lwip/netdb.h"
  28. #include "lwip/netif.h"
  29. #include "lwip/inet.h"
  30. #include "lwip/tcp.h"
  31. #define QL_SOCKET_LOG_LEVEL QL_LOG_LEVEL_INFO
  32. #define uart_printf(msg, ...) QL_LOG(QL_SOCKET_LOG_LEVEL, "linkSDK ", msg, ##__VA_ARGS__)
  33. #define devinfo_profile_idx 1
  34. /* 位于portfiles/aiot_port文件夹下的系统适配函数集合 */
  35. extern aiot_sysdep_portfile_t g_aiot_sysdep_portfile;
  36. /* 位于external/ali_ca_cert.c中的服务器证书 */
  37. extern const char *ali_ca_cert;
  38. ql_task_t ql_alisdk_devinfo_task_ref;
  39. ql_task_t ql_alisdk_devinfo_mqtt_process_task_ref;
  40. ql_task_t ql_alisdk_devinfo_mqtt_recv_task_ref;
  41. static uint8_t g_devinfo_mqtt_process_task_running = 0;
  42. static uint8_t g_devinfo_mqtt_recv_task_running = 0;
  43. static int datacall_satrt(void)
  44. {
  45. int ret = 0;
  46. int i = 0;
  47. ql_data_call_info_s info;
  48. char ip4_addr_str[16] = {0};
  49. uint8_t nSim = 0;
  50. ql_rtos_task_sleep_s(10);
  51. uart_printf("========== socket demo start ==========");
  52. uart_printf("wait for network register done");
  53. while ((ret = ql_network_register_wait(nSim, 120)) != 0 && i < 10)
  54. {
  55. i++;
  56. ql_rtos_task_sleep_s(1);
  57. }
  58. if (ret == 0)
  59. {
  60. i = 0;
  61. uart_printf("====network registered!!!!====");
  62. }
  63. else
  64. {
  65. uart_printf("====network register failure!!!!!====");
  66. goto exit;
  67. }
  68. ql_set_data_call_asyn_mode(nSim, devinfo_profile_idx, 0);
  69. uart_printf("===start data call====");
  70. ret = ql_start_data_call(nSim, devinfo_profile_idx, QL_PDP_TYPE_IP, "uninet", NULL, NULL, 0);
  71. uart_printf("===data call result:%d", ret);
  72. if (ret != 0)
  73. {
  74. uart_printf("====data call failure!!!!=====");
  75. }
  76. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  77. ret = ql_get_data_call_info(nSim, devinfo_profile_idx, &info);
  78. if (ret != 0)
  79. {
  80. uart_printf("ql_get_data_call_info ret: %d", ret);
  81. ql_stop_data_call(nSim, devinfo_profile_idx);
  82. goto exit;
  83. }
  84. uart_printf("info->mqtt_profile_idx: %d", info.profile_idx);
  85. uart_printf("info->ip_version: %d", info.ip_version);
  86. uart_printf("info->v4.state: %d", info.v4.state);
  87. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  88. uart_printf("info.v4.addr.ip: %s\r\n", ip4_addr_str);
  89. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  90. uart_printf("info.v4.addr.pri_dns: %s\r\n", ip4_addr_str);
  91. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  92. uart_printf("info.v4.addr.sec_dns: %s\r\n", ip4_addr_str);
  93. return 0;
  94. exit:
  95. return -1;
  96. }
  97. /* TODO: 如果要关闭日志, 就把这个函数实现为空, 如果要减少日志, 可根据code选择不打印
  98. *
  99. * 例如: [1581501698.455][LK-0309] pub: /sys/a13FN5TplKq/devinfo_basic_demo/thing/deviceinfo/update
  100. *
  101. * 上面这条日志的code就是0309(十六进制), code值的定义见core/aiot_state_api.h
  102. *
  103. */
  104. /* 日志回调函数, SDK的日志会从这里输出 */
  105. static int32_t demo_state_logcb(int32_t code, char *message)
  106. {
  107. uart_printf("%s", message);
  108. return 0;
  109. }
  110. /* MQTT事件回调函数, 当网络连接/重连/断开时被触发, 事件定义见core/aiot_mqtt_api.h */
  111. static void demo_mqtt_event_handler(void *handle, const aiot_mqtt_event_t *event, void *userdata)
  112. {
  113. switch (event->type) {
  114. /* SDK因为用户调用了aiot_mqtt_connect()接口, 与mqtt服务器建立连接已成功 */
  115. case AIOT_MQTTEVT_CONNECT: {
  116. uart_printf("AIOT_MQTTEVT_CONNECT\n");
  117. }
  118. break;
  119. /* SDK因为网络状况被动断连后, 自动发起重连已成功 */
  120. case AIOT_MQTTEVT_RECONNECT: {
  121. uart_printf("AIOT_MQTTEVT_RECONNECT\n");
  122. }
  123. break;
  124. /* SDK因为网络的状况而被动断开了连接, network是底层读写失败, heartbeat是没有按预期得到服务端心跳应答 */
  125. case AIOT_MQTTEVT_DISCONNECT: {
  126. char *cause = (event->data.disconnect == AIOT_MQTTDISCONNEVT_NETWORK_DISCONNECT) ? ("network disconnect") :
  127. ("heartbeat disconnect");
  128. uart_printf("AIOT_MQTTEVT_DISCONNECT: %s\n", cause);
  129. }
  130. break;
  131. default: {
  132. }
  133. }
  134. }
  135. /* MQTT默认消息处理回调, 当SDK从服务器收到MQTT消息时, 且无对应用户回调处理时被调用 */
  136. static void demo_mqtt_default_recv_handler(void *handle, const aiot_mqtt_recv_t *packet, void *userdata)
  137. {
  138. switch (packet->type) {
  139. case AIOT_MQTTRECV_HEARTBEAT_RESPONSE: {
  140. uart_printf("heartbeat response\n");
  141. }
  142. break;
  143. case AIOT_MQTTRECV_SUB_ACK: {
  144. uart_printf("suback, res: -0x%04X, packet id: %d, max qos: %d\n",
  145. -packet->data.sub_ack.res, packet->data.sub_ack.packet_id, packet->data.sub_ack.max_qos);
  146. }
  147. break;
  148. case AIOT_MQTTRECV_PUB: {
  149. uart_printf("pub, qos: %d, topic: %.*s\n", packet->data.pub.qos, packet->data.pub.topic_len, packet->data.pub.topic);
  150. uart_printf("pub, payload: %.*s\n", packet->data.pub.payload_len, packet->data.pub.payload);
  151. }
  152. break;
  153. case AIOT_MQTTRECV_PUB_ACK: {
  154. uart_printf("puback, packet id: %d\n", packet->data.pub_ack.packet_id);
  155. }
  156. break;
  157. default: {
  158. }
  159. }
  160. }
  161. /* 执行aiot_mqtt_process的线程, 包含心跳发送和QoS1消息重发 */
  162. static void demo_mqtt_process_thread(void *args)
  163. {
  164. int32_t res = STATE_SUCCESS;
  165. while (g_devinfo_mqtt_process_task_running) {
  166. res = aiot_mqtt_process(args);
  167. if (res == STATE_USER_INPUT_EXEC_DISABLED) {
  168. break;
  169. }
  170. ql_rtos_task_sleep_s(1);
  171. }
  172. return;
  173. }
  174. /* 执行aiot_mqtt_recv的线程, 包含网络自动重连和从服务器收取MQTT消息 */
  175. static void demo_mqtt_recv_thread(void *args)
  176. {
  177. int32_t res = STATE_SUCCESS;
  178. while (g_devinfo_mqtt_recv_task_running) {
  179. res = aiot_mqtt_recv(args);
  180. if (res < STATE_SUCCESS) {
  181. if (res == STATE_USER_INPUT_EXEC_DISABLED) {
  182. break;
  183. }
  184. ql_rtos_task_sleep_s(1);
  185. }
  186. }
  187. return;
  188. }
  189. static int32_t demo_mqtt_start(void **handle, char *product_key, char *device_name, char *device_secret)
  190. {
  191. int32_t res = STATE_SUCCESS;
  192. void *mqtt_handle = NULL;
  193. char *url = "iot-as-mqtt.cn-shanghai.aliyuncs.com"; /* 阿里云平台上海站点的域名后缀 */
  194. char host[100] = {0}; /* 用这个数组拼接设备连接的云平台站点全地址, 规则是 ${productKey}.iot-as-mqtt.cn-shanghai.aliyuncs.com */
  195. uint16_t port = 443; /* 无论设备是否使用TLS连接阿里云平台, 目的端口都是443 */
  196. aiot_sysdep_network_cred_t cred; /* 安全凭据结构体, 如果要用TLS, 这个结构体中配置CA证书等参数 */
  197. /* 配置SDK的底层依赖 */
  198. aiot_sysdep_set_portfile(&g_aiot_sysdep_portfile);
  199. /* 配置SDK的日志输出 */
  200. aiot_state_set_logcb(demo_state_logcb);
  201. /* 创建SDK的安全凭据, 用于建立TLS连接 */
  202. memset(&cred, 0, sizeof(aiot_sysdep_network_cred_t));
  203. cred.option = AIOT_SYSDEP_NETWORK_CRED_SVRCERT_CA; /* 使用RSA证书校验MQTT服务端 */
  204. cred.max_tls_fragment = 16384; /* 最大的分片长度为16K, 其它可选值还有4K, 2K, 1K, 0.5K */
  205. cred.sni_enabled = 1; /* TLS建连时, 支持Server Name Indicator */
  206. cred.x509_server_cert = ali_ca_cert; /* 用来验证MQTT服务端的RSA根证书 */
  207. cred.x509_server_cert_len = strlen(ali_ca_cert); /* 用来验证MQTT服务端的RSA根证书长度 */
  208. /* 创建1个MQTT客户端实例并内部初始化默认参数 */
  209. mqtt_handle = aiot_mqtt_init();
  210. if (mqtt_handle == NULL) {
  211. uart_printf("aiot_mqtt_init failed\n");
  212. return -1;
  213. }
  214. /* 如果以下代码不被注释, 则例程会用TCP而不是TLS连接云平台 */
  215. /*
  216. {
  217. memset(&cred, 0, sizeof(aiot_sysdep_network_cred_t));
  218. cred.option = AIOT_SYSDEP_NETWORK_CRED_NONE;
  219. }
  220. */
  221. snprintf(host, 100, "%s.%s", product_key, url);
  222. /* 配置MQTT服务器地址 */
  223. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_HOST, (void *)host);
  224. /* 配置MQTT服务器端口 */
  225. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_PORT, (void *)&port);
  226. /* 配置设备productKey */
  227. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_PRODUCT_KEY, (void *)product_key);
  228. /* 配置设备deviceName */
  229. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_DEVICE_NAME, (void *)device_name);
  230. /* 配置设备deviceSecret */
  231. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_DEVICE_SECRET, (void *)device_secret);
  232. /* 配置网络连接的安全凭据, 上面已经创建好了 */
  233. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_NETWORK_CRED, (void *)&cred);
  234. /* 配置MQTT默认消息接收回调函数 */
  235. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_RECV_HANDLER, (void *)demo_mqtt_default_recv_handler);
  236. /* 配置MQTT事件回调函数 */
  237. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_EVENT_HANDLER, (void *)demo_mqtt_event_handler);
  238. /* 配置MQTT连接超时时间 */
  239. int time_delay_ms = 5000;
  240. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_CONNECT_TIMEOUT_MS, (void *)&time_delay_ms);
  241. /* 配置拨号profile_idx信息(quectel平台必须执行此操作)*/
  242. int pdp = devinfo_profile_idx;
  243. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_PROFILE_IDX, (void *)&pdp);
  244. /* 与服务器建立MQTT连接 */
  245. res = aiot_mqtt_connect(mqtt_handle);
  246. if (res < STATE_SUCCESS) {
  247. /* 尝试建立连接失败, 销毁MQTT实例, 回收资源 */
  248. aiot_mqtt_deinit(&mqtt_handle);
  249. uart_printf("aiot_mqtt_connect failed: -0x%04X\n", -res);
  250. return -1;
  251. }
  252. /* 创建一个单独的线程, 专用于执行aiot_mqtt_process, 它会自动发送心跳保活, 以及重发QoS1的未应答报文 */
  253. g_devinfo_mqtt_process_task_running = 1;
  254. res = ql_rtos_task_create(&ql_alisdk_devinfo_mqtt_process_task_ref,27576,24,"client_proc", demo_mqtt_process_thread, (void *)mqtt_handle, 5);
  255. if (res < 0) {
  256. uart_printf("pthread_create demo_mqtt_process_thread failed: %d\n", res);
  257. g_devinfo_mqtt_process_task_running = 0;
  258. aiot_mqtt_deinit(&mqtt_handle);
  259. return -1;
  260. }
  261. /* 创建一个单独的线程用于执行aiot_mqtt_recv, 它会循环收取服务器下发的MQTT消息, 并在断线时自动重连 */
  262. g_devinfo_mqtt_recv_task_running = 1;
  263. res = ql_rtos_task_create(&ql_alisdk_devinfo_mqtt_recv_task_ref, 24576, 25, "recv_test", demo_mqtt_recv_thread, (void *)mqtt_handle, 5);
  264. if (res < 0)
  265. {
  266. uart_printf("pthread_create demo_mqtt_recv_thread failed: %d\n", res);
  267. g_devinfo_mqtt_process_task_running = 0;
  268. ql_rtos_task_delete(ql_alisdk_devinfo_mqtt_process_task_ref);
  269. aiot_mqtt_deinit(&mqtt_handle);
  270. return -1;
  271. }
  272. *handle = mqtt_handle;
  273. return 0;
  274. }
  275. static int32_t demo_mqtt_stop(void **handle)
  276. {
  277. int32_t res = STATE_SUCCESS;
  278. void *mqtt_handle = NULL;
  279. mqtt_handle = *handle;
  280. g_devinfo_mqtt_process_task_running = 0;
  281. g_devinfo_mqtt_recv_task_running = 0;
  282. ql_rtos_task_delete(ql_alisdk_devinfo_mqtt_process_task_ref);
  283. ql_rtos_task_delete(ql_alisdk_devinfo_mqtt_recv_task_ref);
  284. /* 断开MQTT连接 */
  285. res = aiot_mqtt_disconnect(mqtt_handle);
  286. if (res < STATE_SUCCESS) {
  287. aiot_mqtt_deinit(&mqtt_handle);
  288. uart_printf("aiot_mqtt_disconnect failed: -0x%04X\n", -res);
  289. return -1;
  290. }
  291. /* 销毁MQTT实例 */
  292. res = aiot_mqtt_deinit(&mqtt_handle);
  293. if (res < STATE_SUCCESS) {
  294. uart_printf("aiot_mqtt_deinit failed: -0x%04X\n", -res);
  295. return -1;
  296. }
  297. return 0;
  298. }
  299. /* 事件处理回调, */
  300. static void demo_devinfo_event_handler(void *handle, const aiot_devinfo_event_t *event, void *userdata)
  301. {
  302. switch (event->type) {
  303. case AIOT_DEVINFOEVT_INVALID_DEVINFO: {
  304. uart_printf("AIOT_DEVINFOEVT_INVALID_DEVINFO\n");
  305. }
  306. break;
  307. case AIOT_DEVINFOEVT_INVALID_RESPONSE: {
  308. uart_printf("AIOT_DEVINFOEVT_INVALID_RESPONSE\n");
  309. }
  310. break;
  311. case AIOT_DEVINFOEVT_INVALID_RESPONSE_FORMAT: {
  312. uart_printf("AIOT_DEVINFOEVT_INVALID_RESPONSE_FORMAT\n");
  313. }
  314. break;
  315. default: {
  316. }
  317. }
  318. }
  319. /* TODO: 数据处理回调, 当SDK从网络上收到devinfo消息时被调用 */
  320. static void demo_devinfo_recv_handler(void *handle, const aiot_devinfo_recv_t *packet, void *userdata)
  321. {
  322. switch (packet->type) {
  323. /* 这是云端对devinfo消息的应答报文 */
  324. case AIOT_DEVINFORECV_GENERIC_REPLY: {
  325. uart_printf("pk: %s, dn: %s, code: %d, msg id: %d, data: %.*s, message: %.*s\n", packet->product_key, packet->device_name,
  326. packet->data.generic_reply.code, packet->data.generic_reply.msg_id, packet->data.generic_reply.data_len,
  327. packet->data.generic_reply.data, packet->data.generic_reply.message_len, packet->data.generic_reply.message);
  328. }
  329. break;
  330. default: {
  331. }
  332. }
  333. }
  334. static void ql_alisdk_devinfo_task(void *arg)
  335. {
  336. int32_t res = STATE_SUCCESS;
  337. /* TODO: 替换为自己设备的三元组 */
  338. char *product_key = "a1TSu2F29co";
  339. char *device_name = "000001";
  340. char *device_secret = "bd220c3a9c6a5b3f5921485d091497e1";
  341. void *mqtt_handle = NULL, *devinfo_handle = NULL;
  342. /*执行拨号操作,设备联网*/
  343. if(datacall_satrt() != 0)
  344. goto exit;
  345. /* 建立MQTT连接, 并开启保活线程和接收线程 */
  346. res = demo_mqtt_start(&mqtt_handle, product_key, device_name, device_secret);
  347. if (res < 0) {
  348. uart_printf("demo_mqtt_start failed\n");
  349. goto exit;
  350. }
  351. /* 创建1个devinfo客户端实例并内部初始化默认参数 */
  352. devinfo_handle = aiot_devinfo_init();
  353. if (devinfo_handle == NULL) {
  354. demo_mqtt_stop(&mqtt_handle);
  355. uart_printf("aiot_devinfo_init failed\n");
  356. goto exit;
  357. }
  358. /* 配置devinfo会话, 把它和MQTT会话的句柄关联起来 */
  359. res = aiot_devinfo_setopt(devinfo_handle, AIOT_DEVINFOOPT_MQTT_HANDLE, mqtt_handle);
  360. if (res < STATE_SUCCESS) {
  361. uart_printf("aiot_devinfo_setopt AIOT_DEVINFOOPT_MQTT_HANDLE failed, res: -0x%04X\n", -res);
  362. aiot_devinfo_deinit(&devinfo_handle);
  363. demo_mqtt_stop(&mqtt_handle);
  364. goto exit;
  365. }
  366. /* TODO: DEVINFO消息回应从云端到达设备时, 会进入此处设置的回调函数 */
  367. res = aiot_devinfo_setopt(devinfo_handle, AIOT_DEVINFOOPT_RECV_HANDLER, (void *)demo_devinfo_recv_handler);
  368. if (res < STATE_SUCCESS) {
  369. uart_printf("aiot_devinfo_setopt AIOT_DEVINFOOPT_RECV_HANDLER failed, res: -0x%04X\n", -res);
  370. aiot_devinfo_deinit(&devinfo_handle);
  371. demo_mqtt_stop(&mqtt_handle);
  372. goto exit;
  373. }
  374. res = aiot_devinfo_setopt(devinfo_handle, AIOT_DEVINFOOPT_EVENT_HANDLER, (void *)demo_devinfo_event_handler);
  375. if (res < STATE_SUCCESS) {
  376. uart_printf("aiot_devinfo_setopt AIOT_DEVINFOOPT_EVENT_HANDLER failed, res: -0x%04X\n", -res);
  377. aiot_devinfo_deinit(&devinfo_handle);
  378. demo_mqtt_stop(&mqtt_handle);
  379. goto exit;
  380. }
  381. /* 示例: 发送delete删除请求给云平台 */
  382. /* TODO: 替换示例JSON中的 testKey 部分, 效果就会变成删除其他Key标识的设备标签 */
  383. {
  384. aiot_devinfo_msg_t devinfo_delete;
  385. char *delete = "[{\"attrKey\":\"testKey\"}]";
  386. memset(&devinfo_delete, 0, sizeof(aiot_devinfo_msg_t));
  387. devinfo_delete.product_key = product_key;
  388. devinfo_delete.device_name = device_name;
  389. devinfo_delete.type = AIOT_DEVINFO_MSG_DELETE;
  390. devinfo_delete.data.delete.params = delete;
  391. res = aiot_devinfo_send(devinfo_handle, &devinfo_delete);
  392. if (res < STATE_SUCCESS) {
  393. aiot_devinfo_deinit(&devinfo_handle);
  394. demo_mqtt_stop(&mqtt_handle);
  395. return;
  396. }
  397. printf("aiot_devinfo_send delete msg id: %d\n", res);
  398. }
  399. /* 示例: 发送update请求给云平台 */
  400. /* TODO: 替换示例JSON中的 testKey 部分, 效果就会变成更新其他Key标识的设备标签 */
  401. /* TODO: 替换示例JSON中的 testValue 部分, 效果就会变成更新其他Value到设备标签 */
  402. {
  403. aiot_devinfo_msg_t devinfo_update;
  404. char *update = "[{\"attrKey\":\"testKey\",\"attrValue\":\"testValue\"}]";
  405. memset(&devinfo_update, 0, sizeof(aiot_devinfo_msg_t));
  406. devinfo_update.product_key = product_key;
  407. devinfo_update.device_name = device_name;
  408. devinfo_update.type = AIOT_DEVINFO_MSG_UPDATE;
  409. devinfo_update.data.update.params = update;
  410. res = aiot_devinfo_send(devinfo_handle, &devinfo_update);
  411. if (res < STATE_SUCCESS) {
  412. aiot_devinfo_deinit(&devinfo_handle);
  413. demo_mqtt_stop(&mqtt_handle);
  414. return;
  415. }
  416. printf("aiot_devinfo_send update msg id: %d\n", res);
  417. }
  418. /* 主线程进入休眠, 等云平台的DEVINFO回应到达时, 接收线程会调用 demo_devinfo_recv_handler() */
  419. while (1) {
  420. ql_rtos_task_sleep_s(1);
  421. }
  422. /* 销毁DEVINFO实例, 一般不会运行到这里 */
  423. res = aiot_devinfo_deinit(&devinfo_handle);
  424. if (res < STATE_SUCCESS) {
  425. demo_mqtt_stop(&mqtt_handle);
  426. uart_printf("aiot_devinfo_deinit failed: -0x%04X\n", -res);
  427. goto exit;
  428. }
  429. /* 销毁MQTT实例, 退出线程, 一般不会运行到这里 */
  430. res = demo_mqtt_stop(&mqtt_handle);
  431. if (res < 0) {
  432. uart_printf("demo_start_stop failed\n");
  433. goto exit;
  434. }
  435. return;
  436. exit:
  437. ql_rtos_task_delete(NULL);
  438. }
  439. #define QL_ALI_TASK_STACK_SIZE 10240
  440. #define QL_ALI_TASK_PRIO APP_PRIORITY_NORMAL
  441. #define QL_ALI_TASK_EVENT_CNT 5
  442. int ql_ali_sdk_devinfo_test_init(void)
  443. {
  444. QlOSStatus status;
  445. int count = 0;
  446. /* 主体进入休眠 */
  447. while (count < 10)
  448. {
  449. count++;
  450. ql_rtos_task_sleep_s(1);
  451. uart_printf("ql_ali_sdk_mqtt_client_test_init -0x%04X\n", count);
  452. }
  453. status =ql_rtos_task_create(&ql_alisdk_devinfo_task_ref, QL_ALI_TASK_STACK_SIZE, APP_PRIORITY_NORMAL, "devinfo_task", ql_alisdk_devinfo_task, NULL, QL_ALI_TASK_EVENT_CNT);
  454. if (status != QL_OSI_SUCCESS)
  455. {
  456. uart_printf("ql_ali_sdk_devinfo_test_init error");
  457. return -1;
  458. }
  459. return 0;
  460. }