dynreg_basic_demo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /* 这个例程演示了用SDK配置动态注册会话实例的参数, 并发起请求和接收应答, 之后
  2. *
  3. * + 如果接收应答失败了, 销毁实例, 回收资源, 结束程序退出
  4. * + 如果接收应答成功, 在`demo_dynreg_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_dynreg_api.h"
  15. #include "aiot_http_api.h"
  16. #include "core_http.h"
  17. #include "ali_demo.h"
  18. #include "ql_api_osi.h"
  19. #include "ql_api_nw.h"
  20. #include "ali_demo.h"
  21. #include "ql_log.h"
  22. #include "ql_api_datacall.h"
  23. #include "sockets.h"
  24. #include "lwip/ip_addr.h"
  25. #include "lwip/ip6_addr.h"
  26. #include "lwip/netdb.h"
  27. #include "lwip/netif.h"
  28. #include "lwip/inet.h"
  29. #include "lwip/tcp.h"
  30. #define QL_SOCKET_LOG_LEVEL QL_LOG_LEVEL_INFO
  31. #define uart_printf(msg, ...) QL_LOG(QL_SOCKET_LOG_LEVEL, "linkSDK ", msg, ##__VA_ARGS__)
  32. #define dynreg_profile_idx 1
  33. typedef struct {
  34. uint32_t code;
  35. char *device_secret;
  36. } demo_info_dynreg_t;
  37. /* 位于portfiles/aiot_port文件夹下的系统适配函数集合 */
  38. extern aiot_sysdep_portfile_t g_aiot_sysdep_portfile;
  39. /* 位于external/ali_ca_cert.c中的服务器证书 */
  40. extern const char *ali_ca_cert;
  41. ql_task_t ql_alisdk_dynreg_task_ref;
  42. static int datacall_satrt(void)
  43. {
  44. int ret = 0;
  45. int i = 0;
  46. ql_data_call_info_s info;
  47. char ip4_addr_str[16] = {0};
  48. uint8_t nSim = 0;
  49. ql_rtos_task_sleep_s(10);
  50. uart_printf("========== socket demo start ==========");
  51. uart_printf("wait for network register done");
  52. while ((ret = ql_network_register_wait(nSim, 120)) != 0 && i < 10)
  53. {
  54. i++;
  55. ql_rtos_task_sleep_s(1);
  56. }
  57. if (ret == 0)
  58. {
  59. i = 0;
  60. uart_printf("====network registered!!!!====");
  61. }
  62. else
  63. {
  64. uart_printf("====network register failure!!!!!====");
  65. goto exit;
  66. }
  67. ql_set_data_call_asyn_mode(nSim, dynreg_profile_idx, 0);
  68. uart_printf("===start data call====");
  69. ret = ql_start_data_call(nSim, dynreg_profile_idx, QL_PDP_TYPE_IP, "uninet", NULL, NULL, 0);
  70. uart_printf("===data call result:%d", ret);
  71. if (ret != 0)
  72. {
  73. uart_printf("====data call failure!!!!=====");
  74. }
  75. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  76. ret = ql_get_data_call_info(nSim, dynreg_profile_idx, &info);
  77. if (ret != 0)
  78. {
  79. uart_printf("ql_get_data_call_info ret: %d", ret);
  80. ql_stop_data_call(nSim, dynreg_profile_idx);
  81. goto exit;
  82. }
  83. uart_printf("info->mqtt_profile_idx: %d", info.profile_idx);
  84. uart_printf("info->ip_version: %d", info.ip_version);
  85. uart_printf("info->v4.state: %d", info.v4.state);
  86. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  87. uart_printf("info.v4.addr.ip: %s\r\n", ip4_addr_str);
  88. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  89. uart_printf("info.v4.addr.pri_dns: %s\r\n", ip4_addr_str);
  90. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  91. uart_printf("info.v4.addr.sec_dns: %s\r\n", ip4_addr_str);
  92. return 0;
  93. exit:
  94. return -1;
  95. }
  96. /* TODO: 如果要关闭日志, 就把这个函数实现为空, 如果要减少日志, 可根据code选择不打印
  97. *
  98. * 例如: [1580995015.811][LK-040B] > POST /auth/register/device HTTP/1.1
  99. *
  100. * 上面这条日志的code就是040B(十六进制), code值的定义见components/dynreg/aiot_dynreg_api.h
  101. *
  102. */
  103. /* 日志回调函数, SDK的日志会从这里输出 */
  104. static int32_t demo_state_logcb(int32_t code, char *message)
  105. {
  106. uart_printf("%s", message);
  107. return 0;
  108. }
  109. /* 数据处理回调, 当SDK从网络上收到dynreg消息时被调用 */
  110. static void demo_dynreg_recv_handler(void *handle, const aiot_dynreg_recv_t *packet, void *userdata)
  111. {
  112. demo_info_dynreg_t *demo_info = (demo_info_dynreg_t *)userdata;
  113. switch (packet->type) {
  114. case AIOT_DYNREGRECV_STATUS_CODE: {
  115. demo_info->code = packet->data.status_code.code;
  116. uart_printf("func:%s line:%d AIOT_DYNREGRECV_STATUS_CODE \r\n",__func__,__LINE__);
  117. }
  118. break;
  119. /* TODO: 回调中需要将packet指向的空间内容复制保存好, 因为回调返回后, 这些空间就会被SDK释放 */
  120. case AIOT_DYNREGRECV_DEVICE_INFO: {
  121. uart_printf("func:%s line:%d AIOT_DYNREGRECV_DEVICE_INFO \r\n",__func__,__LINE__);
  122. demo_info->device_secret = malloc(strlen(packet->data.device_info.device_secret) + 1);
  123. if (demo_info->device_secret != NULL) {
  124. memset(demo_info->device_secret, 0, strlen(packet->data.device_info.device_secret) + 1);
  125. memcpy(demo_info->device_secret, packet->data.device_info.device_secret,
  126. strlen(packet->data.device_info.device_secret));
  127. }
  128. }
  129. break;
  130. default: {
  131. }
  132. break;
  133. }
  134. }
  135. static void ali_dynreg_test(void *arg)
  136. {
  137. int32_t res = STATE_SUCCESS;
  138. void *dynreg_handle = NULL;
  139. char *host = "iot-auth.cn-shanghai.aliyuncs.com"; /* 阿里云平台动态注册国内站点 */
  140. uint16_t port = 443; /* 无论设备是否使用TLS连接阿里云平台, 目的端口都是443 */
  141. aiot_sysdep_network_cred_t cred; /* 安全凭据结构体, 如果要用TLS, 这个结构体中配置CA证书等参数 */
  142. demo_info_dynreg_t demo_info;
  143. uint16_t pdp = dynreg_profile_idx;
  144. uint32_t timeout = 5000;
  145. /* TODO: 替换为自己设备的productKey, productSecret和deviceName */
  146. char *product_key = "a1ATIg2Dgt3";
  147. char *device_name = "product1_test_device1";
  148. char *product_secret = "sRwayrNWiGwWYgEr";
  149. /*执行拨号操作,设备联网*/
  150. if(datacall_satrt()!=0)
  151. goto exit;
  152. uart_printf("==========kernel ali_dynreg_test entry!!=====\n");
  153. memset(&demo_info, 0, sizeof(demo_info_dynreg_t));
  154. /* 配置SDK的底层依赖 */
  155. aiot_sysdep_set_portfile(&g_aiot_sysdep_portfile);
  156. /* 配置SDK的日志输出 */
  157. aiot_state_set_logcb(demo_state_logcb);
  158. /* 创建SDK的安全凭据, 用于建立TLS连接 */
  159. memset(&cred, 0, sizeof(aiot_sysdep_network_cred_t));
  160. cred.option = AIOT_SYSDEP_NETWORK_CRED_SVRCERT_CA; /* 使用RSA证书校验DYNREG服务端 */
  161. cred.max_tls_fragment = 16384; /* 最大的分片长度为16K, 其它可选值还有4K, 2K, 1K, 0.5K */
  162. cred.sni_enabled = 1; /* TLS建连时, 支持Server Name Indicator */
  163. cred.x509_server_cert = ali_ca_cert; /* 用来验证服务端的RSA根证书 */
  164. cred.x509_server_cert_len = strlen(ali_ca_cert); /* 用来验证服务端的RSA根证书长度 */
  165. /* 创建1个dynreg客户端实例并内部初始化默认参数 */
  166. dynreg_handle = aiot_dynreg_init();
  167. if (dynreg_handle == NULL) {
  168. uart_printf("aiot_dynreg_init failed\n");
  169. goto exit;
  170. }
  171. /* 配置连接的服务器地址 */
  172. res = aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_HOST, (void *)host);
  173. if (res < STATE_SUCCESS) {
  174. uart_printf("aiot_dynreg_setopt AIOT_DYNREGOPT_HOST failed, res: -0x%04X\n", -res);
  175. aiot_dynreg_deinit(&dynreg_handle);
  176. goto exit;
  177. }
  178. /* 配置连接的服务器端口 */
  179. res = aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_PORT, (void *)&port);
  180. if (res < STATE_SUCCESS) {
  181. uart_printf("aiot_dynreg_setopt AIOT_DYNREGOPT_PORT failed, res: -0x%04X\n", -res);
  182. aiot_dynreg_deinit(&dynreg_handle);
  183. goto exit;
  184. }
  185. /* 配置拨号profile_idx信息(quectel平台必须执行此操作) */
  186. res = aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_PROFILE_IDX, (void *)&pdp);
  187. if (res < STATE_SUCCESS) {
  188. uart_printf("aiot_dynreg_setopt AIOT_DYNREGOPT_PROFILE_IDX failed, res: -0x%04X\n", -res);
  189. aiot_dynreg_deinit(&dynreg_handle);
  190. goto exit;
  191. }
  192. /* 配置设备productKey */
  193. res = aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_PRODUCT_KEY, (void *)product_key);
  194. if (res < STATE_SUCCESS) {
  195. uart_printf("aiot_dynreg_setopt AIOT_DYNREGOPT_PRODUCT_KEY failed, res: -0x%04X\n", -res);
  196. aiot_dynreg_deinit(&dynreg_handle);
  197. goto exit;
  198. }
  199. /* 配置设备productSecret */
  200. res = aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_PRODUCT_SECRET, (void *)product_secret);
  201. if (res < STATE_SUCCESS) {
  202. uart_printf("aiot_dynreg_setopt AIOT_DYNREGOPT_PRODUCT_SECRET failed, res: -0x%04X\n", -res);
  203. aiot_dynreg_deinit(&dynreg_handle);
  204. goto exit;
  205. }
  206. /* 配置设备deviceName */
  207. res = aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_DEVICE_NAME, (void *)device_name);
  208. if (res < STATE_SUCCESS) {
  209. uart_printf("aiot_dynreg_setopt AIOT_DYNREGOPT_DEVICE_NAME failed, res: -0x%04X\n", -res);
  210. aiot_dynreg_deinit(&dynreg_handle);
  211. goto exit;
  212. }
  213. /* 配置网络连接的安全凭据, 上面已经创建好了 */
  214. res = aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_NETWORK_CRED, (void *)&cred);
  215. if (res < STATE_SUCCESS) {
  216. uart_printf("aiot_dynreg_setopt AIOT_DYNREGOPT_NETWORK_CRED failed, res: -0x%04X\n", -res);
  217. aiot_dynreg_deinit(&dynreg_handle);
  218. goto exit;
  219. }
  220. /* 配置DYNREG默认消息接收回调函数 */
  221. res = aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_RECV_HANDLER, (void *)demo_dynreg_recv_handler);
  222. if (res < STATE_SUCCESS) {
  223. uart_printf("aiot_dynreg_setopt AIOT_DYNREGOPT_RECV_HANDLER failed, res: -0x%04X\n", -res);
  224. aiot_dynreg_deinit(&dynreg_handle);
  225. goto exit;
  226. }
  227. /* 配置HTTP连接超时时间 */
  228. res = aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_CONNECT_TIMEOUT_MS, (void *)&timeout);
  229. if (res < STATE_SUCCESS) {
  230. uart_printf("aiot_dynreg_setopt AIOT_DYNREGOPT_TIMEOUT_MS failed, res: -0x%04X\n", -res);
  231. aiot_dynreg_deinit(&dynreg_handle);
  232. goto exit;
  233. }
  234. /* 设置用户上下文,该上下文会在 demo_dynreg_recv_handler 被调用时传回 */
  235. res = aiot_dynreg_setopt(dynreg_handle, AIOT_DYNREGOPT_USERDATA, (void *)&demo_info);
  236. if (res < STATE_SUCCESS) {
  237. uart_printf("aiot_dynreg_setopt AIOT_DYNREGOPT_USERDATA failed, res: -0x%04X\n", -res);
  238. aiot_dynreg_deinit(&dynreg_handle);
  239. goto exit;
  240. }
  241. /* 发送动态注册请求 */
  242. res = aiot_dynreg_send_request(dynreg_handle);
  243. if (res < STATE_SUCCESS) {
  244. uart_printf("aiot_dynreg_send_request failed: -0x%04X\n", -res);
  245. goto exit;
  246. }
  247. /* 接收动态注册请求 */
  248. res = aiot_dynreg_recv(dynreg_handle);
  249. if (res < STATE_SUCCESS) {
  250. uart_printf("aiot_dynreg_recv failed: -0x%04X\n", -res);
  251. goto exit;
  252. }
  253. uart_printf("status code: %d\n", demo_info.code);
  254. /* 把服务应答中的 deviceSecret 打印出来 */
  255. if (demo_info.device_secret != NULL) {
  256. uart_printf("device secret: %s\n", demo_info.device_secret);
  257. free(demo_info.device_secret);
  258. }
  259. /* 销毁动态注册会话实例 */
  260. res = aiot_dynreg_deinit(&dynreg_handle);
  261. if (res < STATE_SUCCESS) {
  262. uart_printf("aiot_dynreg_deinit failed: -0x%04X\n", -res);
  263. goto exit;
  264. }
  265. return ;
  266. exit:
  267. ql_rtos_task_delete(NULL);
  268. }
  269. #define QL_ALI_TASK_STACK_SIZE 10240
  270. #define QL_ALI_TASK_PRIO APP_PRIORITY_NORMAL
  271. #define QL_ALI_TASK_EVENT_CNT 5
  272. int ql_ali_sdk_dynreg_test_init(void)
  273. {
  274. QlOSStatus status;
  275. int count = 0;
  276. /* 主循环进入休眠 */
  277. while (count < 10)
  278. {
  279. count++;
  280. ql_rtos_task_sleep_s(1);
  281. uart_printf("ql_ali_sdk_dynreg_test_init -0x%04X\n", count);
  282. }
  283. status =ql_rtos_task_create(&ql_alisdk_dynreg_task_ref, QL_ALI_TASK_STACK_SIZE, APP_PRIORITY_NORMAL, "dynreg_test", ali_dynreg_test, NULL, QL_ALI_TASK_EVENT_CNT);
  284. if(status < 0)
  285. return -1;
  286. return 0;
  287. }