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