data_model_basic_demo.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. * 这个例程适用于`RTOS`这类支持Task的POSIX设备, 它演示了用SDK配置MQTT参数并建立连接, 之后创建2个任务
  3. *
  4. * + 一个任务用于保活长连接
  5. * + 一个任务用于接收消息, 并在有消息到达时进入默认的数据回调, 在连接状态变化时进入事件回调
  6. *
  7. * 接着演示了在MQTT连接上进行属性上报, 事件上报, 以及处理收到的属性设置, 服务调用, 取消这些代码段落的注释即可观察运行效果
  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_dm_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 data_model_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_data_model_task_ref;
  39. ql_task_t ql_alisdk_data_model_process_task_ref;
  40. ql_task_t ql_alisdk_data_model_recv_task_ref;
  41. static uint8_t g_data_model_process_task_running = 0;
  42. static uint8_t g_data_model_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, data_model_profile_idx, 0);
  69. uart_printf("===start data call====");
  70. ret = ql_start_data_call(nSim, data_model_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, data_model_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, data_model_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. * 例如: [1577589489.033][LK-0317] mqtt_basic_demo&a13FN5TplKq
  100. *
  101. * 上面这条日志的code就是0317(十六进制), 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. /* 执行aiot_mqtt_process的任务, 包含心跳发送和QoS1消息重发 */
  136. static void demo_mqtt_process_thread(void *args)
  137. {
  138. int32_t res = STATE_SUCCESS;
  139. while (g_data_model_process_task_running) {
  140. res = aiot_mqtt_process(args);
  141. if (res == STATE_USER_INPUT_EXEC_DISABLED) {
  142. break;
  143. }
  144. ql_rtos_task_sleep_s(1);
  145. }
  146. return;
  147. }
  148. /* 执行aiot_mqtt_recv的任务, 包含网络自动重连和从服务器收取MQTT消息 */
  149. static void demo_mqtt_recv_thread(void *args)
  150. {
  151. int32_t res = STATE_SUCCESS;
  152. while (g_data_model_recv_task_running) {
  153. res = aiot_mqtt_recv(args);
  154. if (res < STATE_SUCCESS) {
  155. if (res == STATE_USER_INPUT_EXEC_DISABLED) {
  156. break;
  157. }
  158. ql_rtos_task_sleep_s(1);
  159. }
  160. }
  161. return;
  162. }
  163. /* 用户数据接收处理回调函数 */
  164. static void demo_dm_recv_handler(void *dm_handle, const aiot_dm_recv_t *recv, void *userdata)
  165. {
  166. uart_printf("demo_dm_recv_handler, type = %d\r\n", recv->type);
  167. switch (recv->type) {
  168. /* 属性上报, 事件上报, 获取期望属性值或者删除期望属性值的应答 */
  169. case AIOT_DMRECV_GENERIC_REPLY: {
  170. uart_printf("msg_id = %d, code = %d, data = %.*s, message = %.*s\r\n",
  171. recv->data.generic_reply.msg_id,
  172. recv->data.generic_reply.code,
  173. recv->data.generic_reply.data_len,
  174. recv->data.generic_reply.data,
  175. recv->data.generic_reply.message_len,
  176. recv->data.generic_reply.message);
  177. }
  178. break;
  179. /* 属性设置 */
  180. case AIOT_DMRECV_PROPERTY_SET: {
  181. uart_printf("msg_id = %ld, params = %.*s\r\n",
  182. (unsigned long)recv->data.property_set.msg_id,
  183. recv->data.property_set.params_len,
  184. recv->data.property_set.params);
  185. /* TODO: 以下代码演示如何对来自云平台的属性设置指令进行应答, 用户可取消注释查看演示效果 */
  186. {
  187. aiot_dm_msg_t msg;
  188. memset(&msg, 0, sizeof(aiot_dm_msg_t));
  189. msg.type = AIOT_DMMSG_PROPERTY_SET_REPLY;
  190. msg.data.property_set_reply.msg_id = recv->data.property_set.msg_id;
  191. msg.data.property_set_reply.code = 200;
  192. msg.data.property_set_reply.data = "{}";
  193. int32_t res = aiot_dm_send(dm_handle, &msg);
  194. if (res < 0) {
  195. uart_printf("aiot_dm_send failed\r\n");
  196. }
  197. }
  198. }
  199. break;
  200. /* 异步服务调用 */
  201. case AIOT_DMRECV_ASYNC_SERVICE_INVOKE: {
  202. uart_printf("msg_id = %ld, service_id = %s, params = %.*s\r\n",
  203. (unsigned long)recv->data.async_service_invoke.msg_id,
  204. recv->data.async_service_invoke.service_id,
  205. recv->data.async_service_invoke.params_len,
  206. recv->data.async_service_invoke.params);
  207. /* TODO: 以下代码演示如何对来自云平台的异步服务调用进行应答, 用户可取消注释查看演示效果
  208. *
  209. * 注意: 如果用户在回调函数外进行应答, 需要自行保存msg_id, 因为回调函数入参在退出回调函数后将被SDK销毁, 不可以再访问到
  210. */
  211. {
  212. aiot_dm_msg_t msg;
  213. memset(&msg, 0, sizeof(aiot_dm_msg_t));
  214. msg.type = AIOT_DMMSG_ASYNC_SERVICE_REPLY;
  215. msg.data.async_service_reply.msg_id = recv->data.async_service_invoke.msg_id;
  216. msg.data.async_service_reply.code = 200;
  217. msg.data.async_service_reply.service_id = "ToggleLightSwitch";
  218. msg.data.async_service_reply.data = "{\"dataA\": 20}";
  219. int32_t res = aiot_dm_send(dm_handle, &msg);
  220. if (res < 0) {
  221. uart_printf("aiot_dm_send failed\r\n");
  222. }
  223. }
  224. }
  225. break;
  226. /* 同步服务调用 */
  227. case AIOT_DMRECV_SYNC_SERVICE_INVOKE: {
  228. uart_printf("msg_id = %ld, rrpc_id = %s, service_id = %s, params = %.*s\r\n",
  229. (unsigned long)recv->data.sync_service_invoke.msg_id,
  230. recv->data.sync_service_invoke.rrpc_id,
  231. recv->data.sync_service_invoke.service_id,
  232. recv->data.sync_service_invoke.params_len,
  233. recv->data.sync_service_invoke.params);
  234. /* TODO: 以下代码演示如何对来自云平台的同步服务调用进行应答, 用户可取消注释查看演示效果
  235. *
  236. * 注意: 如果用户在回调函数外进行应答, 需要自行保存msg_id和rrpc_id字符串, 因为回调函数入参在退出回调函数后将被SDK销毁, 不可以再访问到
  237. */
  238. {
  239. aiot_dm_msg_t msg;
  240. memset(&msg, 0, sizeof(aiot_dm_msg_t));
  241. msg.type = AIOT_DMMSG_SYNC_SERVICE_REPLY;
  242. msg.data.sync_service_reply.rrpc_id = recv->data.sync_service_invoke.rrpc_id;
  243. msg.data.sync_service_reply.msg_id = recv->data.sync_service_invoke.msg_id;
  244. msg.data.sync_service_reply.code = 200;
  245. msg.data.sync_service_reply.service_id = "SetLightSwitchTimer";
  246. msg.data.sync_service_reply.data = "{}";
  247. int32_t res = aiot_dm_send(dm_handle, &msg);
  248. if (res < 0) {
  249. uart_printf("aiot_dm_send failed\r\n");
  250. }
  251. }
  252. }
  253. break;
  254. /* 下行二进制数据 */
  255. case AIOT_DMRECV_RAW_DATA: {
  256. uart_printf("raw data len = %d\r\n", recv->data.raw_data.data_len);
  257. /* TODO: 以下代码演示如何发送二进制格式数据, 若使用需要有相应的数据透传脚本部署在云端 */
  258. {
  259. aiot_dm_msg_t msg;
  260. uint8_t raw_data[] = {0x01, 0x02};
  261. memset(&msg, 0, sizeof(aiot_dm_msg_t));
  262. msg.type = AIOT_DMMSG_RAW_DATA;
  263. msg.data.raw_data.data = raw_data;
  264. msg.data.raw_data.data_len = sizeof(raw_data);
  265. aiot_dm_send(dm_handle, &msg);
  266. }
  267. }
  268. break;
  269. /* 二进制格式的同步服务调用, 比单纯的二进制数据消息多了个rrpc_id */
  270. case AIOT_DMRECV_RAW_SYNC_SERVICE_INVOKE: {
  271. uart_printf("raw sync service rrpc_id = %s, data_len = %d\r\n",
  272. recv->data.raw_service_invoke.rrpc_id,
  273. recv->data.raw_service_invoke.data_len);
  274. }
  275. break;
  276. default:
  277. break;
  278. }
  279. }
  280. /* 属性上报函数演示 */
  281. static int32_t demo_send_property_post(void *dm_handle, char *params)
  282. {
  283. aiot_dm_msg_t msg;
  284. memset(&msg, 0, sizeof(aiot_dm_msg_t));
  285. msg.type = AIOT_DMMSG_PROPERTY_POST;
  286. msg.data.property_post.params = params;
  287. return aiot_dm_send(dm_handle, &msg);
  288. }
  289. /* 事件上报函数演示 */
  290. static int32_t demo_send_event_post(void *dm_handle, char *event_id, char *params)
  291. {
  292. aiot_dm_msg_t msg;
  293. memset(&msg, 0, sizeof(aiot_dm_msg_t));
  294. msg.type = AIOT_DMMSG_EVENT_POST;
  295. msg.data.event_post.event_id = event_id;
  296. msg.data.event_post.params = params;
  297. return aiot_dm_send(dm_handle, &msg);
  298. }
  299. #if 0
  300. /* 演示了获取属性LightSwitch的期望值, 用户可将此函数加入到main函数中运行演示 */
  301. static int32_t demo_send_get_desred_requset(void *dm_handle)
  302. {
  303. aiot_dm_msg_t msg;
  304. memset(&msg, 0, sizeof(aiot_dm_msg_t));
  305. msg.type = AIOT_DMMSG_GET_DESIRED;
  306. msg.data.get_desired.params = "[\"LightSwitch\"]";
  307. return aiot_dm_send(dm_handle, &msg);
  308. }
  309. /* 演示了删除属性LightSwitch的期望值, 用户可将此函数加入到main函数中运行演示 */
  310. static int32_t demo_send_delete_desred_requset(void *dm_handle)
  311. {
  312. aiot_dm_msg_t msg;
  313. memset(&msg, 0, sizeof(aiot_dm_msg_t));
  314. msg.type = AIOT_DMMSG_DELETE_DESIRED;
  315. msg.data.get_desired.params = "{\"LightSwitch\":{}}";
  316. return aiot_dm_send(dm_handle, &msg);
  317. }
  318. #endif
  319. static void ql_alisdk_data_model_task(void *arg)
  320. {
  321. int32_t res = STATE_SUCCESS;
  322. void *dm_handle = NULL;
  323. void *mqtt_handle = NULL;
  324. char *url = "iot-as-mqtt.cn-shanghai.aliyuncs.com"; /* 阿里云平台上海站点的域名后缀 */
  325. char host[100] = {0}; /* 用这个数组拼接设备连接的云平台站点全地址, 规则是 ${productKey}.iot-as-mqtt.cn-shanghai.aliyuncs.com */
  326. uint16_t port = 443; /* 无论设备是否使用TLS连接阿里云平台, 目的端口都是443 */
  327. aiot_sysdep_network_cred_t cred; /* 安全凭据结构体, 如果要用TLS, 这个结构体中配置CA证书等参数 */
  328. /* TODO: 替换为自己设备的三元组 */
  329. char *product_key = "a1TSu2F29co";
  330. char *device_name = "000001";
  331. char *device_secret = "bd220c3a9c6a5b3f5921485d091497e1";
  332. /*执行拨号操作,设备联网*/
  333. if(datacall_satrt() != 0)
  334. goto exit;
  335. /* 配置SDK的底层依赖 */
  336. aiot_sysdep_set_portfile(&g_aiot_sysdep_portfile);
  337. /* 配置SDK的日志输出 */
  338. aiot_state_set_logcb(demo_state_logcb);
  339. /* 创建SDK的安全凭据, 用于建立TLS连接 */
  340. memset(&cred, 0, sizeof(aiot_sysdep_network_cred_t));
  341. cred.option = AIOT_SYSDEP_NETWORK_CRED_SVRCERT_CA; /* 使用RSA证书校验MQTT服务端 */
  342. cred.max_tls_fragment = 16384; /* 最大的分片长度为16K, 其它可选值还有4K, 2K, 1K, 0.5K */
  343. cred.sni_enabled = 1; /* TLS建连时, 支持Server Name Indicator */
  344. cred.x509_server_cert = ali_ca_cert; /* 用来验证MQTT服务端的RSA根证书 */
  345. cred.x509_server_cert_len = strlen(ali_ca_cert); /* 用来验证MQTT服务端的RSA根证书长度 */
  346. /* 如果以下代码不被注释, 则例程会用TCP而不是TLS连接云平台 */
  347. /*
  348. {
  349. memset(&cred, 0, sizeof(aiot_sysdep_network_cred_t));
  350. cred.option = AIOT_SYSDEP_NETWORK_CRED_NONE;
  351. }
  352. */
  353. /* 创建1个MQTT客户端实例并内部初始化默认参数 */
  354. mqtt_handle = aiot_mqtt_init();
  355. if (mqtt_handle == NULL) {
  356. uart_printf("aiot_mqtt_init failed\n");
  357. goto exit;
  358. }
  359. snprintf(host, 100, "%s.%s", product_key, url);
  360. /* 配置MQTT服务器地址 */
  361. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_HOST, (void *)host);
  362. /* 配置MQTT服务器端口 */
  363. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_PORT, (void *)&port);
  364. /* 配置设备productKey */
  365. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_PRODUCT_KEY, (void *)product_key);
  366. /* 配置设备deviceName */
  367. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_DEVICE_NAME, (void *)device_name);
  368. /* 配置设备deviceSecret */
  369. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_DEVICE_SECRET, (void *)device_secret);
  370. /* 配置网络连接的安全凭据, 上面已经创建好了 */
  371. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_NETWORK_CRED, (void *)&cred);
  372. /* 配置MQTT事件回调函数 */
  373. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_EVENT_HANDLER, (void *)demo_mqtt_event_handler);
  374. /* 配置MQTT连接超时时间 */
  375. int time_delay_ms = 5000;
  376. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_CONNECT_TIMEOUT_MS, (void *)&time_delay_ms);
  377. /* 配置拨号profile_idx信息(quectel平台必须执行此操作)*/
  378. int pdp = data_model_profile_idx;
  379. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_PROFILE_IDX, (void *)&pdp);
  380. /* 创建DATA-MODEL实例 */
  381. dm_handle = aiot_dm_init();
  382. if (dm_handle == NULL) {
  383. uart_printf("aiot_dm_init failed");
  384. goto exit;
  385. }
  386. /* 配置MQTT实例句柄 */
  387. aiot_dm_setopt(dm_handle, AIOT_DMOPT_MQTT_HANDLE, mqtt_handle);
  388. /* 配置消息接收处理回调函数 */
  389. aiot_dm_setopt(dm_handle, AIOT_DMOPT_RECV_HANDLER, (void *)demo_dm_recv_handler);
  390. /* 与服务器建立MQTT连接 */
  391. res = aiot_mqtt_connect(mqtt_handle);
  392. if (res < STATE_SUCCESS) {
  393. /* 尝试建立连接失败, 销毁MQTT实例, 回收资源 */
  394. aiot_mqtt_deinit(&mqtt_handle);
  395. uart_printf("aiot_mqtt_connect failed: -0x%04X\n", -res);
  396. goto exit;
  397. }
  398. /* 创建一个单独的任务, 专用于执行aiot_mqtt_process, 它会自动发送心跳保活, 以及重发QoS1的未应答报文 */
  399. g_data_model_process_task_running = 1;
  400. res = ql_rtos_task_create(&ql_alisdk_data_model_process_task_ref, 24576, 24, "client_proc", demo_mqtt_process_thread, (void*)mqtt_handle, 5);
  401. if (res < 0) {
  402. uart_printf("pthread_create demo_mqtt_process_thread failed: %d\n", res);
  403. goto exit;
  404. }
  405. /* 创建一个单独的任务用于执行aiot_mqtt_recv, 它会循环收取服务器下发的MQTT消息, 并在断线时自动重连 */
  406. g_data_model_recv_task_running = 1;
  407. res = ql_rtos_task_create(&ql_alisdk_data_model_recv_task_ref, 24576, 25, "rect_test", demo_mqtt_recv_thread, (void *)mqtt_handle, 5);
  408. if (res < 0) {
  409. uart_printf("pthread_create demo_mqtt_recv_thread failed: %d\n", res);
  410. goto exit;
  411. }
  412. /* 主循环进入休眠 */
  413. while (1) {
  414. /* TODO: 以下代码演示了简单的属性上报和事件上报, 用户可取消注释观察演示效果 */
  415. ql_rtos_task_sleep_s(10);
  416. demo_send_property_post(dm_handle, "{\"LightSwitch\": 0}");
  417. if(res)
  418. {
  419. uart_printf("demo_send_property_post failed!!\r\n");
  420. }
  421. res = demo_send_event_post(dm_handle, "Error", "{\"ErrorCode\": 0}");
  422. if(res)
  423. {
  424. uart_printf("demo_send_event_post failed!!\r\n");
  425. }
  426. }
  427. /* 断开MQTT连接, 一般不会运行到这里 */
  428. res = aiot_mqtt_disconnect(mqtt_handle);
  429. if (res < STATE_SUCCESS) {
  430. aiot_mqtt_deinit(&mqtt_handle);
  431. uart_printf("aiot_mqtt_disconnect failed: -0x%04X\n", -res);
  432. goto exit;
  433. }
  434. /* 销毁DATA-MODEL实例, 一般不会运行到这里 */
  435. res = aiot_dm_deinit(&dm_handle);
  436. if (res < STATE_SUCCESS) {
  437. uart_printf("aiot_dm_deinit failed: -0x%04X\n", -res);
  438. return;
  439. }
  440. /* 销毁MQTT实例, 一般不会运行到这里 */
  441. res = aiot_mqtt_deinit(&mqtt_handle);
  442. if (res < STATE_SUCCESS) {
  443. uart_printf("aiot_mqtt_deinit failed: -0x%04X\n", -res);
  444. goto exit;
  445. }
  446. g_data_model_process_task_running = 0;
  447. g_data_model_recv_task_running = 0;
  448. ql_rtos_task_delete(ql_alisdk_data_model_process_task_ref);
  449. ql_rtos_task_delete(ql_alisdk_data_model_recv_task_ref);
  450. return;
  451. exit:
  452. ql_rtos_task_delete(NULL);
  453. }
  454. #define QL_ALI_TASK_STACK_SIZE 10240
  455. #define QL_ALI_TASK_PRIO APP_PRIORITY_NORMAL
  456. #define QL_ALI_TASK_EVENT_CNT 5
  457. int ql_ali_sdk_data_model_test_init(void)
  458. {
  459. QlOSStatus status;
  460. int count = 0;
  461. /* 主体进入休眠 */
  462. while (count < 10)
  463. {
  464. count++;
  465. ql_rtos_task_sleep_s(1);
  466. uart_printf("ql_ali_sdk_mqtt_client_test_init -0x%04X\n", count);
  467. }
  468. status =ql_rtos_task_create(&ql_alisdk_data_model_task_ref, QL_ALI_TASK_STACK_SIZE, APP_PRIORITY_NORMAL, "model_task", ql_alisdk_data_model_task, NULL, QL_ALI_TASK_EVENT_CNT);
  469. if (status != QL_OSI_SUCCESS)
  470. {
  471. uart_printf("ql_ali_sdk_data_model_test_init error");
  472. return -1;
  473. }
  474. return 0;
  475. }