dynregmq_basic_demo.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* 这个例程演示了用SDK配置动态注册会话实例的参数, 并发起请求和接收应答, 之后
  2. *
  3. * + 如果接收应答失败了, 销毁实例, 回收资源, 结束程序退出
  4. * + 如果接收应答成功, 在`demo_dynregmq_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_dynregmq_api.h"
  15. #include "ali_demo.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 dynregmq_mqtt_profile_idx 1
  31. /* 白名单模式下用于保存deviceSecret的结构体定义 */
  32. typedef struct {
  33. char device_secret[64];
  34. } demo_devinfo_wl_t;
  35. /* 免白名单模式下用于保存mqtt建连信息clientid, username和password的结构体定义 */
  36. typedef struct {
  37. char conn_clientid[128];
  38. char conn_username[128];
  39. char conn_password[64];
  40. } demo_devinfo_nwl_t;
  41. /* 位于portfiles/aiot_port文件夹下的系统适配函数集合 */
  42. extern aiot_sysdep_portfile_t g_aiot_sysdep_portfile;
  43. /* 位于external/ali_ca_cert.c中的服务器证书 */
  44. extern const char *ali_ca_cert;
  45. /* 用户保存白名单模式动态注册, 服务器返回的deviceSecret */
  46. static demo_devinfo_wl_t demo_devinfo_wl;
  47. /* 用户保存免白名单模式动态注册, 服务器返回的mqtt建连信息 */
  48. static demo_devinfo_nwl_t demo_devinfo_nwl;
  49. ql_task_t ql_alisdk_dynregmq_task_ref;
  50. static int datacall_satrt(void)
  51. {
  52. int ret = 0;
  53. int i = 0;
  54. ql_data_call_info_s info;
  55. char ip4_addr_str[16] = {0};
  56. uint8_t nSim = 0;
  57. ql_rtos_task_sleep_s(10);
  58. uart_printf("========== socket demo start ==========");
  59. uart_printf("wait for network register done");
  60. while ((ret = ql_network_register_wait(nSim, 120)) != 0 && i < 10)
  61. {
  62. i++;
  63. ql_rtos_task_sleep_s(1);
  64. }
  65. if (ret == 0)
  66. {
  67. i = 0;
  68. uart_printf("====network registered!!!!====");
  69. }
  70. else
  71. {
  72. uart_printf("====network register failure!!!!!====");
  73. goto exit;
  74. }
  75. ql_set_data_call_asyn_mode(nSim, dynregmq_mqtt_profile_idx, 0);
  76. uart_printf("===start data call====");
  77. ret = ql_start_data_call(nSim, dynregmq_mqtt_profile_idx, QL_PDP_TYPE_IP, "uninet", NULL, NULL, 0);
  78. uart_printf("===data call result:%d", ret);
  79. if (ret != 0)
  80. {
  81. uart_printf("====data call failure!!!!=====");
  82. }
  83. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  84. ret = ql_get_data_call_info(nSim, dynregmq_mqtt_profile_idx, &info);
  85. if (ret != 0)
  86. {
  87. uart_printf("ql_get_data_call_info ret: %d", ret);
  88. ql_stop_data_call(nSim, dynregmq_mqtt_profile_idx);
  89. goto exit;
  90. }
  91. uart_printf("info->mqtt_profile_idx: %d", info.profile_idx);
  92. uart_printf("info->ip_version: %d", info.ip_version);
  93. uart_printf("info->v4.state: %d", info.v4.state);
  94. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  95. uart_printf("info.v4.addr.ip: %s\r\n", ip4_addr_str);
  96. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  97. uart_printf("info.v4.addr.pri_dns: %s\r\n", ip4_addr_str);
  98. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  99. uart_printf("info.v4.addr.sec_dns: %s\r\n", ip4_addr_str);
  100. return 0;
  101. exit:
  102. return -1;
  103. }
  104. /* TODO: 如果要关闭日志, 就把这个函数实现为空, 如果要减少日志, 可根据code选择不打印
  105. *
  106. * 例如: [1580995015.811][LK-040B] > POST /auth/register/device HTTP/1.1
  107. *
  108. * 上面这条日志的code就是040B(十六进制), code值的定义见components/dynregmq/aiot_dynregmq_api.h
  109. *
  110. */
  111. /* 日志回调函数, SDK的日志会从这里输出 */
  112. static int32_t demo_state_logcb(int32_t code, char *message)
  113. {
  114. uart_printf("%s", message);
  115. return 0;
  116. }
  117. /* 数据处理回调, 当SDK从网络上收到dynregmq消息时被调用 */
  118. static void demo_dynregmq_recv_handler(void *handle, const aiot_dynregmq_recv_t *packet, void *userdata)
  119. {
  120. switch (packet->type) {
  121. /* TODO: 回调中需要将packet指向的空间内容复制保存好, 因为回调返回后, 这些空间就会被SDK释放 */
  122. case AIOT_DYNREGMQRECV_DEVICEINFO_WL: {
  123. if (strlen(packet->data.deviceinfo_wl.device_secret) >= sizeof(demo_devinfo_wl.device_secret)) {
  124. break;
  125. }
  126. /* 白名单模式, 用户务必要对device_secret进行持久化保存 */
  127. memset(&demo_devinfo_wl, 0, sizeof(demo_devinfo_wl_t));
  128. memcpy(demo_devinfo_wl.device_secret, packet->data.deviceinfo_wl.device_secret,
  129. strlen(packet->data.deviceinfo_wl.device_secret));
  130. }
  131. break;
  132. /* TODO: 回调中需要将packet指向的空间内容复制保存好, 因为回调返回后, 这些空间就会被SDK释放 */
  133. case AIOT_DYNREGMQRECV_DEVICEINFO_NWL: {
  134. if (strlen(packet->data.deviceinfo_nwl.clientid) >= sizeof(demo_devinfo_nwl.conn_clientid) ||
  135. strlen(packet->data.deviceinfo_nwl.username) >= sizeof(demo_devinfo_nwl.conn_username) ||
  136. strlen(packet->data.deviceinfo_nwl.password) >= sizeof(demo_devinfo_nwl.conn_password)) {
  137. break;
  138. }
  139. /* 免白名单模式, 用户务必要对MQTT的建连信息clientid, username和password进行持久化保存 */
  140. memset(&demo_devinfo_nwl, 0, sizeof(demo_devinfo_nwl_t));
  141. memcpy(demo_devinfo_nwl.conn_clientid, packet->data.deviceinfo_nwl.clientid,
  142. strlen(packet->data.deviceinfo_nwl.clientid));
  143. memcpy(demo_devinfo_nwl.conn_username, packet->data.deviceinfo_nwl.username,
  144. strlen(packet->data.deviceinfo_nwl.username));
  145. memcpy(demo_devinfo_nwl.conn_password, packet->data.deviceinfo_nwl.password,
  146. strlen(packet->data.deviceinfo_nwl.password));
  147. }
  148. break;
  149. default: {
  150. }
  151. break;
  152. }
  153. }
  154. static void ali_dynregmq_test(void *arg)
  155. {
  156. int32_t res = STATE_SUCCESS;
  157. void *dynregmq_handle = NULL;
  158. char *host = "a18wPzZJzNG.iot-as-mqtt.cn-shanghai.aliyuncs.com"; /* 阿里云平台动态注册国内站点, TODO: 前缀为设备的productKey */
  159. uint16_t port = 443; /* 无论设备是否使用TLS连接阿里云平台, 目的端口都是443 */
  160. aiot_sysdep_network_cred_t
  161. cred; /* 安全凭据结构体, 如果要用TLS, 这个结构体中配置CA证书等参数 */
  162. uint8_t nowhitelist = 1; /* 是否使用免白名单功能 */
  163. /* TODO: 替换为自己设备的productKey, productSecret和deviceName */
  164. char *product_key = "a1ATIg2Dgt3";
  165. char *device_name = "product1_test_device1";
  166. char *product_secret = "sRwayrNWiGwWYgEr";
  167. /*执行拨号操作,设备联网*/
  168. if(datacall_satrt()!=0)
  169. goto exit ;
  170. /* 配置SDK的底层依赖 */
  171. aiot_sysdep_set_portfile(&g_aiot_sysdep_portfile);
  172. /* 配置SDK的日志输出 */
  173. aiot_state_set_logcb(demo_state_logcb);
  174. /* 创建SDK的安全凭据, 用于建立TLS连接 */
  175. memset(&cred, 0, sizeof(aiot_sysdep_network_cred_t));
  176. cred.option = AIOT_SYSDEP_NETWORK_CRED_SVRCERT_CA; /* 使用RSA证书校验DYNREGMQ服务端 */
  177. cred.max_tls_fragment = 16384; /* 最大的分片长度为16K, 其它可选值还有4K, 2K, 1K, 0.5K */
  178. cred.sni_enabled = 1; /* TLS建连时, 支持Server Name Indicator */
  179. cred.x509_server_cert = ali_ca_cert; /* 用来验证服务端的RSA根证书 */
  180. cred.x509_server_cert_len = strlen(ali_ca_cert); /* 用来验证服务端的RSA根证书长度 */
  181. /* 创建1个dynregmq客户端实例并内部初始化默认参数 */
  182. dynregmq_handle = aiot_dynregmq_init();
  183. if (dynregmq_handle == NULL) {
  184. uart_printf("aiot_dynregmq_init failed\n");
  185. goto exit ;
  186. }
  187. /* 配置连接的服务器地址 */
  188. res = aiot_dynregmq_setopt(dynregmq_handle, AIOT_DYNREGMQOPT_HOST, (void *)host);
  189. if (res < STATE_SUCCESS) {
  190. uart_printf("aiot_dynregmq_setopt AIOT_DYNREGMQOPT_HOST failed, res: -0x%04X\n", -res);
  191. //aiot_dynregmq_deinit(&dynregmq_handle);
  192. goto exit ;
  193. }
  194. /* 配置连接的服务器端口 */
  195. res = aiot_dynregmq_setopt(dynregmq_handle, AIOT_DYNREGMQOPT_PORT, (void *)&port);
  196. if (res < STATE_SUCCESS) {
  197. uart_printf("aiot_dynregmq_setopt AIOT_DYNREGMQOPT_PORT failed, res: -0x%04X\n", -res);
  198. //aiot_dynregmq_deinit(&dynregmq_handle);
  199. goto exit ;
  200. }
  201. /* 配置拨号profile_idx信息(quectel平台必须执行此操作)*/
  202. int pdp = dynregmq_mqtt_profile_idx;
  203. res = aiot_dynregmq_setopt(dynregmq_handle, AIOT_DYNREGMQOPT_PROFILE_INDEX, (void *)&pdp);
  204. if (res < STATE_SUCCESS) {
  205. uart_printf("aiot_dynregmq_setopt AIOT_DYNREGMQOPT_PORT failed, res: -0x%04X\n", -res);
  206. //aiot_dynregmq_deinit(&dynregmq_handle);
  207. goto exit ;
  208. }
  209. /* 配置设备productKey */
  210. res = aiot_dynregmq_setopt(dynregmq_handle, AIOT_DYNREGMQOPT_PRODUCT_KEY, (void *)product_key);
  211. if (res < STATE_SUCCESS) {
  212. uart_printf("aiot_dynregmq_setopt AIOT_DYNREGMQOPT_PRODUCT_KEY failed, res: -0x%04X\n", -res);
  213. //aiot_dynregmq_deinit(&dynregmq_handle);
  214. goto exit ;
  215. }
  216. /* 配置设备productSecret */
  217. res = aiot_dynregmq_setopt(dynregmq_handle, AIOT_DYNREGMQOPT_PRODUCT_SECRET, (void *)product_secret);
  218. if (res < STATE_SUCCESS) {
  219. uart_printf("aiot_dynregmq_setopt AIOT_DYNREGMQOPT_PRODUCT_SECRET failed, res: -0x%04X\n", -res);
  220. //aiot_dynregmq_deinit(&dynregmq_handle);
  221. goto exit ;
  222. }
  223. /* 配置设备deviceName */
  224. res = aiot_dynregmq_setopt(dynregmq_handle, AIOT_DYNREGMQOPT_DEVICE_NAME, (void *)device_name);
  225. if (res < STATE_SUCCESS) {
  226. uart_printf("aiot_dynregmq_setopt AIOT_DYNREGMQOPT_DEVICE_NAME failed, res: -0x%04X\n", -res);
  227. //aiot_dynregmq_deinit(&dynregmq_handle);
  228. goto exit ;
  229. }
  230. /* 配置网络连接的安全凭据, 上面已经创建好了 */
  231. res = aiot_dynregmq_setopt(dynregmq_handle, AIOT_DYNREGMQOPT_NETWORK_CRED, (void *)&cred);
  232. if (res < STATE_SUCCESS) {
  233. uart_printf("aiot_dynregmq_setopt AIOT_DYNREGMQOPT_NETWORK_CRED failed, res: -0x%04X\n", -res);
  234. //aiot_dynregmq_deinit(&dynregmq_handle);
  235. goto exit ;
  236. }
  237. /* 配置DYNREGMQ默认消息接收回调函数 */
  238. res = aiot_dynregmq_setopt(dynregmq_handle, AIOT_DYNREGMQOPT_RECV_HANDLER, (void *)demo_dynregmq_recv_handler);
  239. if (res < STATE_SUCCESS) {
  240. uart_printf("aiot_dynregmq_setopt AIOT_DYNREGMQOPT_RECV_HANDLER failed, res: -0x%04X\n", -res);
  241. //aiot_dynregmq_deinit(&dynregmq_handle);
  242. goto exit ;
  243. }
  244. int time_delay_ms = 5000;
  245. res = aiot_dynregmq_setopt(dynregmq_handle, AIOT_DYNREGMQOPT_CONNECT_TIMEOUT_MS, (void *)&time_delay_ms);
  246. /* 配置DYNREGMQ动态注册模式,
  247. 1. 配置为0则为白名单模式, 用户必须提前在控制台录入deviceName, 动态注册完成后服务会返回deviceSecret, 用户可通过
  248. AIOT_DYNREGMQRECV_DEVICEINFO_WL类型数据回调获取到deviceSecret.
  249. 2. 配置为1则为免白名单模式, 用户无需提前在控制台录入deviceName, 动态注册完成后服务会返回MQTT建连信息, 用户可通过
  250. AIOT_DYNREGMQRECV_DEVICEINFO_NWL类型数据回调获取到clientid, username, password. 用户需要将这三个参数通过
  251. aiot_mqtt_setopt接口以AIOT_MQTTOPT_CLIENTID, AIOT_MQTTOPT_USERNAME, AIOT_MQTTOPT_PASSWORD配置选项
  252. 配置到MQTT句柄中。
  253. */
  254. res = aiot_dynregmq_setopt(dynregmq_handle, AIOT_DYNREGMQOPT_NO_WHITELIST, (void *)&nowhitelist);
  255. if (res < STATE_SUCCESS) {
  256. uart_printf("aiot_dynregmq_setopt AIOT_DYNREGMQOPT_NO_WHITELIST failed, res: -0x%04X\n", -res);
  257. //aiot_dynregmq_deinit(&dynregmq_handle);
  258. goto exit ;
  259. }
  260. /* 发送动态注册请求 */
  261. res = aiot_dynregmq_send_request(dynregmq_handle);
  262. if (res < STATE_SUCCESS) {
  263. uart_printf("aiot_dynregmq_send_request failed: -0x%04X\n", -res);
  264. //aiot_dynregmq_deinit(&dynregmq_handle);
  265. goto exit ;
  266. }
  267. /* 接收动态注册请求 */
  268. res = aiot_dynregmq_recv(dynregmq_handle);
  269. if (res < STATE_SUCCESS) {
  270. uart_printf("aiot_dynregmq_recv failed: -0x%04X\n", -res);
  271. //aiot_dynregmq_deinit(&dynregmq_handle);
  272. goto exit ;
  273. }
  274. /* 把服务应答中的信息打印出来 */
  275. if (nowhitelist == 0) {
  276. uart_printf("device secret: %s\n", demo_devinfo_wl.device_secret);
  277. } else {
  278. uart_printf("clientid: %s\n", demo_devinfo_nwl.conn_clientid);
  279. uart_printf("username: %s\n", demo_devinfo_nwl.conn_username);
  280. uart_printf("password: %s\n", demo_devinfo_nwl.conn_password);
  281. }
  282. exit:
  283. /* 销毁动态注册会话实例 */
  284. res = aiot_dynregmq_deinit(&dynregmq_handle);
  285. if (res < STATE_SUCCESS) {
  286. uart_printf("aiot_dynregmq_deinit failed: -0x%04X\n", -res);
  287. //return ;
  288. }
  289. ql_rtos_task_delete(NULL);
  290. return;
  291. }
  292. #define QL_ALI_TASK_STACK_SIZE 10240
  293. #define QL_ALI_TASK_PRIO APP_PRIORITY_NORMAL
  294. #define QL_ALI_TASK_EVENT_CNT 5
  295. int ql_ali_sdk_dynregmq_test_init(void)
  296. {
  297. QlOSStatus status;
  298. int count = 0;
  299. /* 主循环进入休眠 */
  300. while (count < 10)
  301. {
  302. count++;
  303. ql_rtos_task_sleep_s(1);
  304. uart_printf("ql_ali_sdk_dynregmq_test_init -0x%04X\n", count);
  305. }
  306. status =ql_rtos_task_create(&ql_alisdk_dynregmq_task_ref, QL_ALI_TASK_STACK_SIZE, APP_PRIORITY_NORMAL, "dynregmq_test", ali_dynregmq_test, NULL, QL_ALI_TASK_EVENT_CNT);
  307. if (status != QL_OSI_SUCCESS)
  308. {
  309. uart_printf("ql_ali_sdk_dynregmq_test_init error");
  310. return -1;
  311. }
  312. return 0;
  313. }