http_basic_demo.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * 这个例程适用于`RTOS`这类支持Task的设备, 它演示了用SDK配置HTTP参数并成功鉴权, 然后
  3. *
  4. * + 如果放开被注释掉的 demo_http_post_lightswitch() 函数调用, 会向平台上报1条HTTP消息
  5. *
  6. * 需要用户关注或修改的部分, 已经用 TODO 在注释中标明
  7. *
  8. */
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "aiot_http_api.h"
  13. #include "core_http.h"
  14. #include "aiot_sysdep_api.h"
  15. #include "aiot_state_api.h"
  16. #include "ali_demo.h"
  17. #include "ql_api_osi.h"
  18. #include "ql_api_nw.h"
  19. #include "ali_demo.h"
  20. #include "ql_log.h"
  21. #include "ql_api_datacall.h"
  22. #include "sockets.h"
  23. #include "lwip/ip_addr.h"
  24. #include "lwip/ip6_addr.h"
  25. #include "lwip/netdb.h"
  26. #include "lwip/netif.h"
  27. #include "lwip/inet.h"
  28. #include "lwip/tcp.h"
  29. #define QL_SOCKET_LOG_LEVEL QL_LOG_LEVEL_INFO
  30. #define uart_printf(msg, ...) QL_LOG(QL_SOCKET_LOG_LEVEL, "linkSDK ", msg, ##__VA_ARGS__)
  31. /* 位于portfiles/aiot_port文件夹下的系统适配函数集合 */
  32. extern aiot_sysdep_portfile_t g_aiot_sysdep_portfile;
  33. /* 位于external/ali_ca_cert.c中的服务器证书 */
  34. extern const char *ali_ca_cert;
  35. ql_task_t ql_alisdk_http_client_task_ref;
  36. #define http_profile_idx 1
  37. static int datacall_satrt(void)
  38. {
  39. int ret = 0;
  40. int i = 0;
  41. ql_data_call_info_s info;
  42. char ip4_addr_str[16] = {0};
  43. uint8_t nSim = 0;
  44. ql_rtos_task_sleep_s(10);
  45. uart_printf("========== socket demo start ==========");
  46. uart_printf("wait for network register done");
  47. while ((ret = ql_network_register_wait(nSim, 120)) != 0 && i < 10)
  48. {
  49. i++;
  50. ql_rtos_task_sleep_s(1);
  51. }
  52. if (ret == 0)
  53. {
  54. i = 0;
  55. uart_printf("====network registered!!!!====");
  56. }
  57. else
  58. {
  59. uart_printf("====network register failure!!!!!====");
  60. goto exit;
  61. }
  62. ql_set_data_call_asyn_mode(nSim, http_profile_idx, 0);
  63. uart_printf("===start data call====");
  64. ret = ql_start_data_call(nSim, http_profile_idx, QL_PDP_TYPE_IP, "uninet", NULL, NULL, 0);
  65. uart_printf("===data call result:%d", ret);
  66. if (ret != 0)
  67. {
  68. uart_printf("====data call failure!!!!=====");
  69. }
  70. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  71. ret = ql_get_data_call_info(nSim, http_profile_idx, &info);
  72. if (ret != 0)
  73. {
  74. uart_printf("ql_get_data_call_info ret: %d", ret);
  75. ql_stop_data_call(nSim, http_profile_idx);
  76. goto exit;
  77. }
  78. uart_printf("info->mqtt_profile_idx: %d", info.profile_idx);
  79. uart_printf("info->ip_version: %d", info.ip_version);
  80. uart_printf("info->v4.state: %d", info.v4.state);
  81. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  82. uart_printf("info.v4.addr.ip: %s\r\n", ip4_addr_str);
  83. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  84. uart_printf("info.v4.addr.pri_dns: %s\r\n", ip4_addr_str);
  85. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  86. uart_printf("info.v4.addr.sec_dns: %s\r\n", ip4_addr_str);
  87. return 0;
  88. exit:
  89. return -1;
  90. }
  91. /* TODO: 如果要关闭日志, 就把这个函数实现为空, 如果要减少日志, 可根据code选择不打印
  92. *
  93. * 例如: [1578127395.399][LK-0409] > POST /auth HTTP/1.1
  94. *
  95. * 上面这条日志的code就是0409(十六进制), code值的定义见core/aiot_state_api.h
  96. *
  97. */
  98. /* 日志回调函数, SDK的日志会从这里输出 */
  99. static int32_t demo_state_logcb(int32_t code, char *message)
  100. {
  101. uart_printf("%s", message);
  102. return 0;
  103. }
  104. /* HTTP事件回调函数, 当内部Token变化时被触发, 事件定义见core/aiot_http_api.h */
  105. static void demo_http_event_handler(void *handle, const aiot_http_event_t *event, void *user_data)
  106. {
  107. int32_t res;
  108. /* token失效事件处理 */
  109. if (event->type == AIOT_HTTPEVT_TOKEN_INVALID) {
  110. uart_printf("token invalid, invoke iot_http_auth to get new token\n");
  111. res = aiot_http_auth(handle);
  112. uart_printf("aiot_http_auth in callback, res = -0x%04x\r\n", -res);
  113. }
  114. }
  115. /* HTTP事件回调函数, 当SDK读取到网络报文时被触发, 报文描述类型见core/aiot_http_api.h */
  116. static void demo_http_recv_handler(void *handle, const aiot_http_recv_t *packet, void *userdata)
  117. {
  118. switch (packet->type) {
  119. case AIOT_HTTPRECV_STATUS_CODE: {
  120. /* TODO: 以下代码如果不被注释, SDK收到HTTP报文时, 会通过这个用户回调打印HTTP状态码, 如404, 200, 302等 */
  121. /* uart_printf("status code: %d\n", packet->data.status_code.code); */
  122. uart_printf("status code: %d\n", packet->data.status_code.code);
  123. }
  124. break;
  125. case AIOT_HTTPRECV_HEADER: {
  126. /* TODO: 以下代码如果不被注释, SDK收到HTTP报文时, 会通过这个用户回调打印HTTP首部, 如Content-Length等 */
  127. /* uart_printf("key: %s, value: %s\n", packet->data.header.key, packet->data.header.value); */
  128. uart_printf("key: %s, value: %s\n", packet->data.header.key, packet->data.header.value);
  129. }
  130. break;
  131. /* TODO: 如果需要处理云平台的HTTP回应报文, 修改这里, 现在只是将回应打印出来 */
  132. case AIOT_HTTPRECV_BODY: {
  133. uart_printf("%d %s\r\n", packet->data.body.len, packet->data.body.buffer);
  134. }
  135. break;
  136. default: {
  137. }
  138. break;
  139. }
  140. }
  141. /* 用HTTP通道上报业务数据给云平台, 例如: 灯已关闭 */
  142. static int32_t demo_http_post_lightswitch(void *handle)
  143. {
  144. /* TODO: 如果要修改上报的数据内容, 就修改 data[] 数组 */
  145. char data[] = "{\"id\":\"1\",\"version\":\"1.0\",\"params\":{\"LightSwitch\":0}}";
  146. int32_t res;
  147. /* 用HTTP通道向云平台上报1条业务数据, 接口是aiot_http_send() */
  148. res = aiot_http_send(handle,
  149. "/sys/a1E6c7I1GoN/product1_test_device1/thing/event/property/post",
  150. (uint8_t *)data,
  151. strlen(data));
  152. if (res < 0) {
  153. uart_printf("aiot_http_send res = -0x%04X\r\n", -res);
  154. return res;
  155. }
  156. res = aiot_http_recv(handle);
  157. if (res >= 0) {
  158. /* 成功接收到服务器应答, 且业务应答码为0, 说明数据上报成功 */
  159. return 0;
  160. } else {
  161. uart_printf("aiot_http_recv res = -0x%04X\r\n", -res);
  162. return -1;
  163. }
  164. }
  165. static void ql_ali_sdk_http_client_test(void *arg)
  166. {
  167. void *http_handle = NULL;
  168. int32_t res;
  169. char *url = "iot-as-http.cn-shanghai.aliyuncs.com";
  170. uint16_t port = 443;
  171. aiot_sysdep_network_cred_t cred;
  172. /* TODO: 替换为自己设备的三元组 */
  173. char *product_key = "a1TSu2F29co";
  174. char *device_name = "000001";
  175. char *device_secret = "bd220c3a9c6a5b3f5921485d091497e1";
  176. /*执行拨号操作,设备联网*/
  177. if(datacall_satrt()!=0)
  178. goto exit;
  179. /* 配置SDK的底层依赖 */
  180. aiot_sysdep_set_portfile(&g_aiot_sysdep_portfile);
  181. /* 配置SDK的日志输出 */
  182. aiot_state_set_logcb(demo_state_logcb);
  183. /* 创建SDK的安全凭据, 用于建立TLS连接 */
  184. memset(&cred, 0, sizeof(aiot_sysdep_network_cred_t));
  185. cred.max_tls_fragment = 16384;
  186. cred.x509_server_cert = ali_ca_cert;
  187. cred.x509_server_cert_len = strlen(ali_ca_cert);
  188. cred.option = AIOT_SYSDEP_NETWORK_CRED_SVRCERT_CA;
  189. /* 创建1个HTTP客户端实例并内部初始化默认参数 */
  190. http_handle = aiot_http_init();
  191. if(!http_handle)
  192. {
  193. uart_printf("aiot_http_init failed !!\n");
  194. goto exit;
  195. }
  196. /* 配置服务器域名 */
  197. aiot_http_setopt(http_handle, AIOT_HTTPOPT_HOST, (void *)url);
  198. /* 配置服务器端口 */
  199. aiot_http_setopt(http_handle, AIOT_HTTPOPT_PORT, (void *)&port);
  200. /* 配置连接超时时间 */
  201. // int timeout = 5000;
  202. // aiot_http_setopt(http_handle, AIOT_HTTPOPT_CONNECT_TIMEOUT_MS, (void *)&timeout);
  203. // uart_printf("http_handle AIOT_HTTPOPT_CONNECT_TIMEOUT_MS : %d \r\n", http_handle->connect_timeout_ms);
  204. /* 配置设备安全凭证 */
  205. aiot_http_setopt(http_handle, AIOT_HTTPOPT_NETWORK_CRED, (void *)&cred);
  206. /* 配置设备三元组 */
  207. aiot_http_setopt(http_handle, AIOT_HTTPOPT_PRODUCT_KEY, (void *)product_key);
  208. aiot_http_setopt(http_handle, AIOT_HTTPOPT_DEVICE_NAME, (void *)device_name);
  209. aiot_http_setopt(http_handle, AIOT_HTTPOPT_DEVICE_SECRET, (void *)device_secret);
  210. /* 配置数据到达时, SDK应调用的用户回调函数 */
  211. aiot_http_setopt(http_handle, AIOT_HTTPOPT_RECV_HANDLER, demo_http_recv_handler);
  212. /* 配置内部状态变化时, SDK应调用的用户回调函数 */
  213. aiot_http_setopt(http_handle, AIOT_HTTPOPT_EVENT_HANDLER, demo_http_event_handler);
  214. /* 配置拨号profile_idx信息(quectel平台必须执行此操作)*/
  215. uint16_t pdp = http_profile_idx;
  216. aiot_http_setopt(http_handle, AIOT_HTTPOPT_PROFILE_IDX, (void *)&pdp);
  217. uint32_t timeout = 5000;
  218. aiot_http_setopt(http_handle, AIOT_HTTPOPT_CONNECT_TIMEOUT_MS, (void *)&timeout);
  219. /*
  220. * TODO: 以下代码如果不被注释, 会演示用TCP(而不是TLS)连接云平台
  221. */
  222. /*
  223. {
  224. int32_t tcp_port = 80;
  225. memset(&cred, 0, sizeof(aiot_sysdep_network_cred_t));
  226. cred.option = AIOT_SYSDEP_NETWORK_CRED_NONE;
  227. aiot_http_setopt(http_handle, AIOT_HTTPOPT_NETWORK_CRED, &cred);
  228. aiot_http_setopt(http_handle, AIOT_HTTPOPT_PORT, &tcp_port);
  229. }
  230. */
  231. /* 设备认证, 获取token */
  232. uart_printf(" aiot_http_auth !!! \r\n");
  233. res = aiot_http_auth(http_handle);
  234. if (res == 0) {
  235. uart_printf("aiot_http_auth succeed\r\n\r\n");
  236. } else {
  237. /* 如果认证失败, 就销毁实例, 回收资源, 结束程序退出 */
  238. uart_printf("aiot_http_auth failed, res = -0x%04X\r\n\r\n", -res);
  239. aiot_http_deinit(&http_handle);
  240. goto exit;
  241. }
  242. /* TODO: 以下代码如果不被注释, 会演示上报 1 次数据到物联网平台 */
  243. /*
  244. demo_http_post_lightswitch(http_handle);
  245. uart_printf("\r\n");
  246. */
  247. uart_printf(" demo_http_post_lightswitch !!! \r\n");
  248. demo_http_post_lightswitch(http_handle);
  249. uart_printf("\r\n");
  250. exit:
  251. /* 销毁HTTP实例 */
  252. aiot_http_deinit(&http_handle);
  253. uart_printf("program exit as normal return\r\n");
  254. uart_printf("\r\n");
  255. ql_rtos_task_delete(NULL);
  256. return ;
  257. }
  258. #define QL_ALI_TASK_STACK_SIZE 10240
  259. #define QL_ALI_TASK_PRIO APP_PRIORITY_NORMAL
  260. #define QL_ALI_TASK_EVENT_CNT 5
  261. int ql_ali_sdk_http_client_test_init(void)
  262. {
  263. QlOSStatus status;
  264. int count = 0;
  265. /* 主循环进入休眠 */
  266. while (count < 10)
  267. {
  268. count++;
  269. ql_rtos_task_sleep_s(1);
  270. uart_printf("ql_ali_sdk_mqtt_client_test_init -0x%04X\n", count);
  271. }
  272. status =ql_rtos_task_create(&ql_alisdk_http_client_task_ref, QL_ALI_TASK_STACK_SIZE, APP_PRIORITY_NORMAL, "http_test", ql_ali_sdk_http_client_test, NULL, QL_ALI_TASK_EVENT_CNT);
  273. if (status != QL_OSI_SUCCESS)
  274. {
  275. uart_printf("ql_ali_sdk_devinfo_test_init error");
  276. return -1;
  277. }
  278. return 0;
  279. }