coap_example.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  3. *
  4. * Again edit by rt-thread group
  5. * Change Logs:
  6. * Date Author Notes
  7. * 2019-07-21 MurphyZhao first edit
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #if !defined(_WIN32)
  13. #include <unistd.h>
  14. #endif
  15. #include "coap_api.h"
  16. #include "coap_wrapper.h"
  17. #include "rtthread.h"
  18. #define IOTX_DAILY_DTLS_SERVER_URI "coaps://11.239.164.238:5684"
  19. #define IOTX_DAILY_PSK_SERVER_URI "coap-psk://10.101.83.159:5682"
  20. #define IOTX_PRE_DTLS_SERVER_URI "coaps://pre.coap.cn-shanghai.link.aliyuncs.com:5684"
  21. #define IOTX_PRE_NOSEC_SERVER_URI "coap://pre.coap.cn-shanghai.link.aliyuncs.com:5683"
  22. #define IOTX_PRE_PSK_SERVER_URI "coap-psk://pre.coap.cn-shanghai.link.aliyuncs.com:5683"
  23. /* online url */
  24. #define IOTX_ONLINE_DTLS_SERVER_URL "coaps://%s.coap.cn-shanghai.link.aliyuncs.com:5684"
  25. #define IOTX_ONLINE_NOSEC_SERVER_URI "coap://%s.coap.cn-shanghai.link.aliyuncs.com:5683"
  26. #define IOTX_ONLINE_PSK_SERVER_URL "coap-psk://%s.coap.cn-shanghai.link.aliyuncs.com:5682"
  27. char m_coap_client_running = 0;
  28. char m_coap_reconnect = 0;
  29. static void iotx_response_handler(void *arg, void *p_response)
  30. {
  31. int len = 0;
  32. unsigned char *p_payload = NULL;
  33. iotx_coap_resp_code_t resp_code;
  34. IOT_CoAP_GetMessageCode(p_response, &resp_code);
  35. IOT_CoAP_GetMessagePayload(p_response, &p_payload, &len);
  36. HAL_Printf("[APPL]: Message response code: 0x%x\r\n", resp_code);
  37. HAL_Printf("[APPL]: Len: %d, Payload: %s\r\n", len, p_payload);
  38. }
  39. char IOTX_PRODUCT_KEY[IOTX_PRODUCT_KEY_LEN + 1] = {0};
  40. char IOTX_DEVICE_NAME[IOTX_DEVICE_NAME_LEN + 1] = {0};
  41. char IOTX_DEVICE_SECRET[IOTX_DEVICE_SECRET_LEN + 1] = {0};
  42. int iotx_get_devinfo(iotx_deviceinfo_t *p_devinfo)
  43. {
  44. if (NULL == p_devinfo) {
  45. return IOTX_ERR_INVALID_PARAM;
  46. }
  47. memset(p_devinfo, 0x00, sizeof(iotx_deviceinfo_t));
  48. /**< get device info*/
  49. HAL_GetProductKey(p_devinfo->product_key);
  50. HAL_GetDeviceName(p_devinfo->device_name);
  51. HAL_GetDeviceSecret(p_devinfo->device_secret);
  52. memset(p_devinfo->device_id, 0, IOTX_PRODUCT_KEY_LEN + IOTX_DEVICE_NAME_LEN + 2);
  53. HAL_Snprintf(p_devinfo->device_id, IOTX_PRODUCT_KEY_LEN + IOTX_DEVICE_NAME_LEN + 2,
  54. "%s.%s", p_devinfo->product_key, p_devinfo->device_name);
  55. /**< end*/
  56. fprintf(stderr, "*****The Product Key : %s *****\r\n", p_devinfo->product_key);
  57. fprintf(stderr, "*****The Device Name : %s *****\r\n", p_devinfo->device_name);
  58. fprintf(stderr, "*****The Device Secret: %s *****\r\n", p_devinfo->device_secret);
  59. fprintf(stderr, "*****The Device ID : %s *****\r\n", p_devinfo->device_id);
  60. return IOTX_SUCCESS;
  61. }
  62. static void iotx_post_data_to_server(void *param)
  63. {
  64. char path[IOTX_URI_MAX_LEN + 1] = {0};
  65. iotx_message_t message;
  66. iotx_deviceinfo_t devinfo;
  67. memset(&message, 0, sizeof(iotx_message_t));
  68. memset(&devinfo, 0, sizeof(iotx_deviceinfo_t));
  69. message.p_payload = (unsigned char *)"{\"name\":\"hello world\"}";
  70. message.payload_len = strlen("{\"name\":\"hello world\"}");
  71. message.resp_callback = iotx_response_handler;
  72. message.msg_type = IOTX_MESSAGE_CON;
  73. message.content_type = IOTX_CONTENT_TYPE_JSON;
  74. iotx_coap_context_t *p_ctx = (iotx_coap_context_t *)param;
  75. iotx_get_devinfo(&devinfo);
  76. snprintf(path, IOTX_URI_MAX_LEN, "/topic/%s/%s/update/", (char *)devinfo.product_key,
  77. (char *)devinfo.device_name);
  78. IOT_CoAP_SendMessage(p_ctx, path, &message);
  79. }
  80. void show_usage()
  81. {
  82. HAL_Printf("\r\nusage: coap-example [OPTION]...\r\n");
  83. HAL_Printf("\t-e pre|online|daily\t\tSet the cloud environment.\r\n");
  84. HAL_Printf("\t-s nosec|dtls|psk \t\tSet the security setting.\r\n");
  85. HAL_Printf("\t-l \t\tSet the program run loop.\r\n");
  86. HAL_Printf("\t-r \t\tTesting the DTLS session ticket.\r\n");
  87. HAL_Printf("\t-h \t\tShow this usage.\r\n");
  88. }
  89. static int coap_example_main(int argc, char **argv)
  90. {
  91. int count = 0;
  92. char secur[32] = {0};
  93. char env[32] = {0};
  94. int opt;
  95. iotx_coap_config_t config;
  96. iotx_deviceinfo_t deviceinfo;
  97. /* set device info use HAL function */
  98. HAL_GetProductKey(IOTX_PRODUCT_KEY);
  99. HAL_GetDeviceName(IOTX_DEVICE_NAME);
  100. HAL_GetDeviceSecret(IOTX_DEVICE_SECRET);
  101. IOT_SetLogLevel(IOT_LOG_DEBUG);
  102. #if 0
  103. while ((opt = getopt(argc, argv, "e:s:lhr")) != -1) {
  104. switch (opt) {
  105. case 's': {
  106. if (strlen(optarg) > 31) {
  107. memcpy(secur, optarg, 31);
  108. } else {
  109. memcpy(secur, optarg, strlen(optarg));
  110. }
  111. }
  112. break;
  113. case 'e': {
  114. if (strlen(optarg) > 31) {
  115. memcpy(env, optarg, 31);
  116. } else {
  117. memcpy(env, optarg, strlen(optarg));
  118. }
  119. }
  120. break;
  121. case 'l':
  122. m_coap_client_running = 1;
  123. break;
  124. case 'r':
  125. m_coap_reconnect = 1;
  126. break;
  127. case 'h':
  128. show_usage();
  129. return 0;
  130. default:
  131. break;
  132. }
  133. }
  134. #else
  135. /* Just use psk security mode, online environment */
  136. (void)argc;
  137. (void)argv;
  138. (void)opt;
  139. memcpy(secur, "psk", 4);
  140. memcpy(env, "online", 7);
  141. m_coap_client_running = 1;
  142. m_coap_reconnect = 1;
  143. #endif
  144. HAL_Printf("[COAP-Client]: Enter Coap Client\r\n");
  145. memset(&config, 0x00, sizeof(iotx_coap_config_t));
  146. if (0 == strncmp(env, "pre", strlen("pre"))) {
  147. if (0 == strncmp(secur, "dtls", strlen("dtls"))) {
  148. config.p_url = IOTX_PRE_DTLS_SERVER_URI;
  149. } else if (0 == strncmp(secur, "psk", strlen("psk"))) {
  150. config.p_url = IOTX_PRE_PSK_SERVER_URI;
  151. } else {
  152. config.p_url = IOTX_PRE_NOSEC_SERVER_URI;
  153. }
  154. } else if (0 == strncmp(env, "online", strlen("online"))) {
  155. if (0 == strncmp(secur, "dtls", strlen("dtls"))) {
  156. char url[256] = {0};
  157. snprintf(url, sizeof(url), IOTX_ONLINE_DTLS_SERVER_URL, IOTX_PRODUCT_KEY);
  158. config.p_url = url;
  159. } else if (0 == strncmp(secur, "psk", strlen("psk"))) {
  160. char url[256] = {0};
  161. snprintf(url, sizeof(url), IOTX_ONLINE_PSK_SERVER_URL, IOTX_PRODUCT_KEY);
  162. config.p_url = url;
  163. } else {
  164. HAL_Printf("Online environment must access with DTLS/PSK\r\n");
  165. IOT_SetLogLevel(IOT_LOG_NONE);
  166. return -1;
  167. }
  168. } else if (0 == strncmp(env, "daily", strlen("daily"))) {
  169. if (0 == strncmp(secur, "dtls", strlen("dtls"))) {
  170. config.p_url = IOTX_DAILY_DTLS_SERVER_URI;
  171. } else if (0 == strncmp(secur, "psk", strlen("psk"))) {
  172. config.p_url = IOTX_DAILY_PSK_SERVER_URI;
  173. }
  174. }
  175. iotx_get_devinfo(&deviceinfo);
  176. config.p_devinfo = (iotx_device_info_t *)&deviceinfo;
  177. config.wait_time_ms = 3000;
  178. iotx_coap_context_t *p_ctx = NULL;
  179. reconnect:
  180. p_ctx = IOT_CoAP_Init(&config);
  181. if (NULL != p_ctx) {
  182. IOT_CoAP_DeviceNameAuth(p_ctx);
  183. do {
  184. if (count == 11 || 0 == count) {
  185. iotx_post_data_to_server((void *)p_ctx);
  186. count = 1;
  187. }
  188. count ++;
  189. IOT_CoAP_Yield(p_ctx);
  190. } while (m_coap_client_running);
  191. IOT_CoAP_Deinit(&p_ctx);
  192. } else {
  193. HAL_Printf("IoTx CoAP init failed\r\n");
  194. }
  195. if (m_coap_reconnect) {
  196. m_coap_reconnect = 0;
  197. goto reconnect;
  198. }
  199. IOT_DumpMemoryStats(IOT_LOG_DEBUG);
  200. IOT_SetLogLevel(IOT_LOG_NONE);
  201. HAL_Printf("[COAP-Client]: Exit Coap Client\r\n");
  202. return 0;
  203. }
  204. #ifdef FINSH_USING_MSH
  205. MSH_CMD_EXPORT_ALIAS(coap_example_main, ali_coap_sample, ali coap sample);
  206. #endif