coap_example.c 7.8 KB

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