bootstrap_posix_demo.c 20 KB

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