subdev_basic_demo.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * 这个例程适用于`Linux`这类支持pthread的POSIX设备, 它演示了......
  3. *
  4. * 需要用户关注或修改的部分, 已经用 TODO 在注释中标明
  5. *
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <pthread.h>
  11. #include "aiot_state_api.h"
  12. #include "aiot_sysdep_api.h"
  13. #include "aiot_mqtt_api.h"
  14. #include "aiot_subdev_api.h"
  15. /* 位于portfiles/aiot_port文件夹下的系统适配函数集合 */
  16. extern aiot_sysdep_portfile_t g_aiot_sysdep_portfile;
  17. /* 位于external/ali_ca_cert.c中的服务器证书 */
  18. extern const char *ali_ca_cert;
  19. static pthread_t g_mqtt_process_thread;
  20. static pthread_t g_mqtt_recv_thread;
  21. static uint8_t g_mqtt_process_thread_running = 0;
  22. static uint8_t g_mqtt_recv_thread_running = 0;
  23. aiot_subdev_dev_t g_subdev[] = {
  24. {
  25. "a13FN5TplKq",
  26. "subdev_basic_demo_01",
  27. "768XBgQwgOakz3K4uhOiLeeh9xjJQx6h",
  28. "y7GSILD480lBSsP8"
  29. },
  30. {
  31. "a13FN5TplKq",
  32. "subdev_basic_demo_02",
  33. "iwTZrbjbgNVChfuJkihjE5asekoyKoYv",
  34. "y7GSILD480lBSsP8"
  35. },
  36. {
  37. "a13FN5TplKq",
  38. "subdev_basic_demo_03",
  39. "fdutq35iKMYdcWWBuIINY26hsNhgFXWE",
  40. "y7GSILD480lBSsP8"
  41. },
  42. {
  43. "a13FN5TplKq",
  44. "subdev_basic_demo_04",
  45. "HCKv50YqgwdKhy5cE0Vz4aydmK2ojPvr",
  46. "y7GSILD480lBSsP8"
  47. }
  48. };
  49. /* TODO: 如果要关闭日志, 就把这个函数实现为空, 如果要减少日志, 可根据code选择不打印
  50. *
  51. * 例如: [1577589489.033][LK-0317] subdev_basic_demo&a13FN5TplKq
  52. *
  53. * 上面这条日志的code就是0317(十六进制), code值的定义见core/aiot_state_api.h
  54. *
  55. */
  56. /* 日志回调函数, SDK的日志会从这里输出 */
  57. static int32_t demo_state_logcb(int32_t code, char *message)
  58. {
  59. printf("%s", message);
  60. return 0;
  61. }
  62. /* MQTT事件回调函数, 当网络连接/重连/断开时被触发, 事件定义见core/aiot_mqtt_api.h */
  63. void demo_mqtt_event_handler(void *handle, const aiot_mqtt_event_t *event, void *userdata)
  64. {
  65. switch (event->type) {
  66. /* SDK因为用户调用了aiot_mqtt_connect()接口, 与mqtt服务器建立连接已成功 */
  67. case AIOT_MQTTEVT_CONNECT: {
  68. printf("AIOT_MQTTEVT_CONNECT\n");
  69. /* TODO: 处理SDK建连成功, 不可以在这里调用耗时较长的阻塞函数 */
  70. }
  71. break;
  72. /* SDK因为网络状况被动断连后, 自动发起重连已成功 */
  73. case AIOT_MQTTEVT_RECONNECT: {
  74. printf("AIOT_MQTTEVT_RECONNECT\n");
  75. /* TODO: 处理SDK重连成功, 不可以在这里调用耗时较长的阻塞函数 */
  76. }
  77. break;
  78. /* SDK因为网络的状况而被动断开了连接, network是底层读写失败, heartbeat是没有按预期得到服务端心跳应答 */
  79. case AIOT_MQTTEVT_DISCONNECT: {
  80. char *cause = (event->data.disconnect == AIOT_MQTTDISCONNEVT_NETWORK_DISCONNECT) ? ("network disconnect") :
  81. ("heartbeat disconnect");
  82. printf("AIOT_MQTTEVT_DISCONNECT: %s\n", cause);
  83. /* TODO: 处理SDK被动断连, 不可以在这里调用耗时较长的阻塞函数 */
  84. }
  85. break;
  86. default: {
  87. }
  88. }
  89. }
  90. /* MQTT默认消息处理回调, 当SDK从服务器收到MQTT消息时, 且无对应用户回调处理时被调用 */
  91. void demo_mqtt_default_recv_handler(void *handle, const aiot_mqtt_recv_t *packet, void *userdata)
  92. {
  93. switch (packet->type) {
  94. case AIOT_MQTTRECV_HEARTBEAT_RESPONSE: {
  95. printf("heartbeat response\n");
  96. /* TODO: 处理服务器对心跳的回应, 一般不处理 */
  97. }
  98. break;
  99. case AIOT_MQTTRECV_SUB_ACK: {
  100. printf("suback, res: -0x%04X, packet id: %d, max qos: %d\n",
  101. -packet->data.sub_ack.res, packet->data.sub_ack.packet_id, packet->data.sub_ack.max_qos);
  102. /* TODO: 处理服务器对订阅请求的回应, 一般不处理 */
  103. }
  104. break;
  105. case AIOT_MQTTRECV_PUB: {
  106. printf("pub, qos: %d, topic: %.*s\n", packet->data.pub.qos, packet->data.pub.topic_len, packet->data.pub.topic);
  107. printf("pub, payload: %.*s\n", packet->data.pub.payload_len, packet->data.pub.payload);
  108. /* TODO: 处理服务器下发的业务报文 */
  109. }
  110. break;
  111. case AIOT_MQTTRECV_PUB_ACK: {
  112. printf("puback, packet id: %d\n", packet->data.pub_ack.packet_id);
  113. /* TODO: 处理服务器对QoS1上报消息的回应, 一般不处理 */
  114. }
  115. break;
  116. default: {
  117. }
  118. }
  119. }
  120. /* 执行aiot_mqtt_process的线程, 包含心跳发送和QoS1消息重发 */
  121. void *demo_mqtt_process_thread(void *args)
  122. {
  123. int32_t res = STATE_SUCCESS;
  124. while (g_mqtt_process_thread_running) {
  125. res = aiot_mqtt_process(args);
  126. if (res == STATE_USER_INPUT_EXEC_DISABLED) {
  127. break;
  128. }
  129. sleep(1);
  130. }
  131. return NULL;
  132. }
  133. /* 执行aiot_mqtt_recv的线程, 包含网络自动重连和从服务器收取MQTT消息 */
  134. void *demo_mqtt_recv_thread(void *args)
  135. {
  136. int32_t res = STATE_SUCCESS;
  137. while (g_mqtt_recv_thread_running) {
  138. res = aiot_mqtt_recv(args);
  139. if (res < STATE_SUCCESS) {
  140. if (res == STATE_USER_INPUT_EXEC_DISABLED) {
  141. break;
  142. }
  143. sleep(1);
  144. }
  145. }
  146. return NULL;
  147. }
  148. int32_t demo_mqtt_start(void **handle)
  149. {
  150. int32_t res = STATE_SUCCESS;
  151. void *mqtt_handle = NULL;
  152. char *url = "iot-as-mqtt.cn-shanghai.aliyuncs.com"; /* 阿里云平台上海站点的域名后缀 */
  153. char host[100] = {0}; /* 用这个数组拼接设备连接的云平台站点全地址, 规则是 ${productKey}.iot-as-mqtt.cn-shanghai.aliyuncs.com */
  154. uint16_t port = 443; /* 无论设备是否使用TLS连接阿里云平台, 目的端口都是443 */
  155. aiot_sysdep_network_cred_t cred; /* 安全凭据结构体, 如果要用TLS, 这个结构体中配置CA证书等参数 */
  156. /* TODO: 替换为自己设备的三元组 */
  157. char *product_key = "a1Zv737gW43";
  158. char *device_name = "EC200U_QUECTEL_01";
  159. char *device_secret = "05e47a1c7f55b2d828b16733dd6457dd";
  160. /* 创建SDK的安全凭据, 用于建立TLS连接 */
  161. memset(&cred, 0, sizeof(aiot_sysdep_network_cred_t));
  162. cred.option = AIOT_SYSDEP_NETWORK_CRED_SVRCERT_CA; /* 使用RSA证书校验MQTT服务端 */
  163. cred.max_tls_fragment = 16384; /* 最大的分片长度为16K, 其它可选值还有4K, 2K, 1K, 0.5K */
  164. cred.sni_enabled = 1; /* TLS建连时, 支持Server Name Indicator */
  165. cred.x509_server_cert = ali_ca_cert; /* 用来验证MQTT服务端的RSA根证书 */
  166. cred.x509_server_cert_len = strlen(ali_ca_cert); /* 用来验证MQTT服务端的RSA根证书长度 */
  167. /* 创建1个MQTT客户端实例并内部初始化默认参数 */
  168. mqtt_handle = aiot_mqtt_init();
  169. if (mqtt_handle == NULL) {
  170. printf("aiot_mqtt_init failed\n");
  171. return -1;
  172. }
  173. /* TODO: 如果以下代码不被注释, 则例程会用TCP而不是TLS连接云平台 */
  174. /*
  175. {
  176. memset(&cred, 0, sizeof(aiot_sysdep_network_cred_t));
  177. cred.option = AIOT_SYSDEP_NETWORK_CRED_NONE;
  178. }
  179. */
  180. snprintf(host, 100, "%s.%s", product_key, url);
  181. /* 配置MQTT服务器地址 */
  182. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_HOST, (void *)host);
  183. /* 配置MQTT服务器端口 */
  184. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_PORT, (void *)&port);
  185. /* 配置设备productKey */
  186. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_PRODUCT_KEY, (void *)product_key);
  187. /* 配置设备deviceName */
  188. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_DEVICE_NAME, (void *)device_name);
  189. /* 配置设备deviceSecret */
  190. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_DEVICE_SECRET, (void *)device_secret);
  191. /* 配置网络连接的安全凭据, 上面已经创建好了 */
  192. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_NETWORK_CRED, (void *)&cred);
  193. /* 配置MQTT默认消息接收回调函数 */
  194. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_RECV_HANDLER, (void *)demo_mqtt_default_recv_handler);
  195. /* 配置MQTT事件回调函数 */
  196. aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_EVENT_HANDLER, (void *)demo_mqtt_event_handler);
  197. /* 与服务器建立MQTT连接 */
  198. res = aiot_mqtt_connect(mqtt_handle);
  199. if (res < STATE_SUCCESS) {
  200. /* 尝试建立连接失败, 销毁MQTT实例, 回收资源 */
  201. aiot_mqtt_deinit(&mqtt_handle);
  202. printf("aiot_mqtt_connect failed: -0x%04X\n", -res);
  203. return -1;
  204. }
  205. /* 创建一个单独的线程, 专用于执行aiot_mqtt_process, 它会自动发送心跳保活, 以及重发QoS1的未应答报文 */
  206. g_mqtt_process_thread_running = 1;
  207. res = pthread_create(&g_mqtt_process_thread, NULL, demo_mqtt_process_thread, mqtt_handle);
  208. if (res < 0) {
  209. printf("pthread_create demo_mqtt_process_thread failed: %d\n", res);
  210. aiot_mqtt_deinit(&mqtt_handle);
  211. return -1;
  212. }
  213. /* 创建一个单独的线程用于执行aiot_mqtt_recv, 它会循环收取服务器下发的MQTT消息, 并在断线时自动重连 */
  214. g_mqtt_recv_thread_running = 1;
  215. res = pthread_create(&g_mqtt_recv_thread, NULL, demo_mqtt_recv_thread, mqtt_handle);
  216. if (res < 0) {
  217. printf("pthread_create demo_mqtt_recv_thread failed: %d\n", res);
  218. g_mqtt_process_thread_running = 0;
  219. pthread_join(g_mqtt_process_thread, NULL);
  220. aiot_mqtt_deinit(&mqtt_handle);
  221. return -1;
  222. }
  223. *handle = mqtt_handle;
  224. return 0;
  225. }
  226. int32_t demo_mqtt_stop(void **handle)
  227. {
  228. int32_t res = STATE_SUCCESS;
  229. void *mqtt_handle = NULL;
  230. mqtt_handle = *handle;
  231. g_mqtt_process_thread_running = 0;
  232. g_mqtt_recv_thread_running = 0;
  233. pthread_join(g_mqtt_process_thread, NULL);
  234. pthread_join(g_mqtt_recv_thread, NULL);
  235. /* 断开MQTT连接 */
  236. res = aiot_mqtt_disconnect(mqtt_handle);
  237. if (res < STATE_SUCCESS) {
  238. aiot_mqtt_deinit(&mqtt_handle);
  239. printf("aiot_mqtt_disconnect failed: -0x%04X\n", -res);
  240. return -1;
  241. }
  242. /* 销毁MQTT实例 */
  243. res = aiot_mqtt_deinit(&mqtt_handle);
  244. if (res < STATE_SUCCESS) {
  245. printf("aiot_mqtt_deinit failed: -0x%04X\n", -res);
  246. return -1;
  247. }
  248. return 0;
  249. }
  250. void demo_subdev_recv_handler(void *handle, const aiot_subdev_recv_t *packet, void *user_data)
  251. {
  252. switch (packet->type) {
  253. case AIOT_SUBDEVRECV_TOPO_ADD_REPLY:
  254. case AIOT_SUBDEVRECV_TOPO_DELETE_REPLY:
  255. case AIOT_SUBDEVRECV_TOPO_GET_REPLY:
  256. case AIOT_SUBDEVRECV_BATCH_LOGIN_REPLY:
  257. case AIOT_SUBDEVRECV_BATCH_LOGOUT_REPLY:
  258. case AIOT_SUBDEVRECV_SUB_REGISTER_REPLY:
  259. case AIOT_SUBDEVRECV_PRODUCT_REGISTER_REPLY: {
  260. printf("msgid : %d\n", packet->data.generic_reply.msg_id);
  261. printf("code : %d\n", packet->data.generic_reply.code);
  262. printf("product key : %s\n", packet->data.generic_reply.product_key);
  263. printf("device name : %s\n", packet->data.generic_reply.device_name);
  264. printf("message : %s\n", (packet->data.generic_reply.message == NULL)?("NULL"):(packet->data.generic_reply.message));
  265. printf("data : %s\n", packet->data.generic_reply.data);
  266. }
  267. break;
  268. case AIOT_SUBDEVRECV_TOPO_CHANGE_NOTIFY: {
  269. printf("msgid : %d\n", packet->data.generic_notify.msg_id);
  270. printf("product key : %s\n", packet->data.generic_notify.product_key);
  271. printf("device name : %s\n", packet->data.generic_notify.device_name);
  272. printf("params : %s\n", packet->data.generic_notify.params);
  273. }
  274. break;
  275. default: {
  276. }
  277. }
  278. }
  279. int main(int argc, char *argv[])
  280. {
  281. int32_t res = STATE_SUCCESS;
  282. void *mqtt_handle = NULL, *subdev_handle = NULL;
  283. /* 配置SDK的底层依赖 */
  284. aiot_sysdep_set_portfile(&g_aiot_sysdep_portfile);
  285. /* 配置SDK的日志输出 */
  286. aiot_state_set_logcb(demo_state_logcb);
  287. res = demo_mqtt_start(&mqtt_handle);
  288. if (res < 0) {
  289. printf("demo_mqtt_start failed\n");
  290. return -1;
  291. }
  292. subdev_handle = aiot_subdev_init();
  293. if (subdev_handle == NULL) {
  294. printf("aiot_subdev_init failed\n");
  295. demo_mqtt_stop(&mqtt_handle);
  296. return -1;
  297. }
  298. aiot_subdev_setopt(subdev_handle, AIOT_SUBDEVOPT_MQTT_HANDLE, mqtt_handle);
  299. aiot_subdev_setopt(subdev_handle, AIOT_SUBDEVOPT_RECV_HANDLER, demo_subdev_recv_handler);
  300. res = aiot_subdev_send_topo_add(subdev_handle, g_subdev, sizeof(g_subdev)/sizeof(aiot_subdev_dev_t));
  301. if (res < STATE_SUCCESS) {
  302. printf("aiot_subdev_send_topo_add failed, res: -0x%04X\n", -res);
  303. aiot_subdev_deinit(&subdev_handle);
  304. demo_mqtt_stop(&mqtt_handle);
  305. return -1;
  306. }
  307. sleep(2);
  308. // aiot_subdev_send_topo_delete(subdev_handle, g_subdev, sizeof(g_subdev)/sizeof(aiot_subdev_dev_t));
  309. // if (res < STATE_SUCCESS) {
  310. // printf("aiot_subdev_send_topo_delete failed, res: -0x%04X\n", -res);
  311. // aiot_subdev_deinit(&subdev_handle);
  312. // demo_mqtt_stop(&mqtt_handle);
  313. // return -1;
  314. // }
  315. // sleep(2);
  316. // aiot_subdev_send_topo_get(subdev_handle);
  317. // if (res < STATE_SUCCESS) {
  318. // printf("aiot_subdev_send_topo_get failed, res: -0x%04X\n", -res);
  319. // aiot_subdev_deinit(&subdev_handle);
  320. // demo_mqtt_stop(&mqtt_handle);
  321. // return -1;
  322. // }
  323. // sleep(2);
  324. // aiot_subdev_send_sub_register(subdev_handle, g_subdev, sizeof(g_subdev)/sizeof(aiot_subdev_dev_t));
  325. // if (res < STATE_SUCCESS) {
  326. // printf("aiot_subdev_send_sub_register failed, res: -0x%04X\n", -res);
  327. // aiot_subdev_deinit(&subdev_handle);
  328. // demo_mqtt_stop(&mqtt_handle);
  329. // return -1;
  330. // }
  331. // sleep(2);
  332. // aiot_subdev_send_product_register(subdev_handle, g_subdev, sizeof(g_subdev)/sizeof(aiot_subdev_dev_t));
  333. // if (res < STATE_SUCCESS) {
  334. // printf("aiot_subdev_send_product_register failed, res: -0x%04X\n", -res);
  335. // aiot_subdev_deinit(&subdev_handle);
  336. // demo_mqtt_stop(&mqtt_handle);
  337. // return -1;
  338. // }
  339. // sleep(2);
  340. aiot_subdev_send_batch_login(subdev_handle, g_subdev, sizeof(g_subdev)/sizeof(aiot_subdev_dev_t));
  341. if (res < STATE_SUCCESS) {
  342. printf("aiot_subdev_send_batch_login failed, res: -0x%04X\n", -res);
  343. aiot_subdev_deinit(&subdev_handle);
  344. demo_mqtt_stop(&mqtt_handle);
  345. return -1;
  346. }
  347. sleep(2);
  348. // aiot_subdev_send_batch_logout(subdev_handle, g_subdev, sizeof(g_subdev)/sizeof(aiot_subdev_dev_t));
  349. // if (res < STATE_SUCCESS) {
  350. // printf("aiot_subdev_send_batch_logout failed, res: -0x%04X\n", -res);
  351. // aiot_subdev_deinit(&subdev_handle);
  352. // demo_mqtt_stop(&mqtt_handle);
  353. // return -1;
  354. // }
  355. while(1) {
  356. sleep(1);
  357. }
  358. res = aiot_subdev_deinit(&subdev_handle);
  359. if (res < STATE_SUCCESS) {
  360. printf("aiot_subdev_deinit failed: -0x%04X\n", res);
  361. }
  362. res = demo_mqtt_stop(&mqtt_handle);
  363. if (res < 0) {
  364. printf("demo_start_stop failed\n");
  365. return -1;
  366. }
  367. return 0;
  368. }