coap-example.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) 2014-2016 Alibaba Group. All rights reserved.
  3. * License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. * not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include "iot_import.h"
  23. #include "iot_export.h"
  24. #define IOTX_DAILY_DTLS_SERVER_URI "coaps://iot-as-coap.alibaba.net:5684"
  25. #define IOTX_PRE_DTLS_SERVER_URI "coaps://pre.iot-as-coap.cn-shanghai.aliyuncs.com:5684"
  26. #define IOTX_PRE_NOSEC_SERVER_URI "coap://pre.iot-as-coap.cn-shanghai.aliyuncs.com:5683"
  27. #define IOTX_ONLINE_DTLS_SERVER_URL "coaps://%s.iot-as-coap.cn-shanghai.aliyuncs.com:5684"
  28. char m_coap_client_running = 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. #ifdef TEST_COAP_DAILY
  40. #define IOTX_PRODUCT_KEY "zPygj0yP3UF"
  41. #define IOTX_DEVICE_NAME "device_2"
  42. #define IOTX_DEVICE_SECRET "5FQbVOPWNwhEuCvnVcP1Mvyjmvt8ECQi"
  43. #define IOTX_DEVICE_ID "device_2"
  44. #else
  45. #define IOTX_PRODUCT_KEY "vtkkbrpmxmF"
  46. #define IOTX_DEVICE_NAME "IoTxCoAPTestDev"
  47. #define IOTX_DEVICE_SECRET "Stk4IUErQUBc1tWRWEKWb5ACra4hFDYF"
  48. #define IOTX_DEVICE_ID "IoTxCoAPTestDev.1"
  49. #endif
  50. int iotx_set_devinfo(iotx_deviceinfo_t *p_devinfo)
  51. {
  52. if (NULL == p_devinfo) {
  53. return IOTX_ERR_INVALID_PARAM;
  54. }
  55. memset(p_devinfo, 0x00, sizeof(iotx_deviceinfo_t));
  56. /**< get device info*/
  57. HAL_GetProductKey(p_devinfo->product_key);
  58. HAL_GetDeviceName(p_devinfo->device_name);
  59. HAL_GetDeviceSecret(p_devinfo->device_secret);
  60. HAL_GetDeviceID(p_devinfo->device_id);
  61. /**< end*/
  62. fprintf(stderr, "*****The Product Key : %s *****\r\n", p_devinfo->product_key);
  63. fprintf(stderr, "*****The Device Name : %s *****\r\n", p_devinfo->device_name);
  64. //fprintf(stderr, "*****The Device Secret: %s *****\r\n", p_devinfo->device_secret);
  65. fprintf(stderr, "*****The Device ID : %s *****\r\n", p_devinfo->device_id);
  66. return IOTX_SUCCESS;
  67. }
  68. static void iotx_post_data_to_server(void *param)
  69. {
  70. char path[IOTX_URI_MAX_LEN + 1] = {0};
  71. iotx_message_t message;
  72. iotx_deviceinfo_t devinfo;
  73. message.p_payload = (unsigned char *)"{\"name\":\"hello world\"}";
  74. message.payload_len = strlen("{\"name\":\"hello world\"}");
  75. message.resp_callback = iotx_response_handler;
  76. message.msg_type = IOTX_MESSAGE_CON;
  77. message.content_type = IOTX_CONTENT_TYPE_JSON;
  78. iotx_coap_context_t *p_ctx = (iotx_coap_context_t *)param;
  79. iotx_set_devinfo(&devinfo);
  80. snprintf(path, IOTX_URI_MAX_LEN, "/topic/%s/%s/update/", (char *)devinfo.product_key,
  81. (char *)devinfo.device_name);
  82. IOT_CoAP_SendMessage(p_ctx, path, &message);
  83. }
  84. int 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*/
  93. HAL_SetProductKey(IOTX_PRODUCT_KEY);
  94. HAL_SetDeviceName(IOTX_DEVICE_NAME);
  95. HAL_SetDeviceSecret(IOTX_DEVICE_SECRET);
  96. /**< end*/
  97. IOT_OpenLog("coap");
  98. IOT_SetLogLevel(IOT_LOG_DEBUG);
  99. HAL_Printf("[COAP-Client]: Enter Coap Client\r\n");
  100. while ((opt = getopt(argc, argv, "e:s:lh")) != -1) {
  101. switch (opt) {
  102. case 's':
  103. strncpy(secur, optarg, strlen(optarg));
  104. break;
  105. case 'e':
  106. strncpy(env, optarg, strlen(optarg));
  107. break;
  108. case 'l':
  109. m_coap_client_running = 1;
  110. break;
  111. case 'h':
  112. /* TODO: */
  113. break;
  114. default:
  115. break;
  116. }
  117. }
  118. memset(&config, 0x00, sizeof(iotx_coap_config_t));
  119. if (0 == strncmp(env, "pre", strlen("pre"))) {
  120. if (0 == strncmp(secur, "dtls", strlen("dtls"))) {
  121. config.p_url = IOTX_PRE_DTLS_SERVER_URI;
  122. } else {
  123. config.p_url = IOTX_PRE_NOSEC_SERVER_URI;
  124. }
  125. } else if (0 == strncmp(env, "online", strlen("online"))) {
  126. if (0 == strncmp(secur, "dtls", strlen("dtls"))) {
  127. char url[256] = {0};
  128. snprintf(url, sizeof(url), IOTX_ONLINE_DTLS_SERVER_URL, IOTX_PRODUCT_KEY);
  129. config.p_url = url;
  130. } else {
  131. HAL_Printf("Online environment must access with DTLS\r\n");
  132. IOT_CloseLog();
  133. return -1;
  134. }
  135. }
  136. #ifdef TEST_COAP_DAILY
  137. config.p_url = IOTX_DAILY_DTLS_SERVER_URI;
  138. #endif
  139. iotx_set_devinfo(&deviceinfo);
  140. config.p_devinfo = (iotx_device_info_t*)&deviceinfo;
  141. config.wait_time_ms = 3000;
  142. iotx_coap_context_t *p_ctx = NULL;
  143. p_ctx = IOT_CoAP_Init(&config);
  144. if (NULL != p_ctx) {
  145. IOT_CoAP_DeviceNameAuth(p_ctx);
  146. do {
  147. count ++;
  148. if (count == 11) {
  149. iotx_post_data_to_server((void *)p_ctx);
  150. count = 1;
  151. }
  152. IOT_CoAP_Yield(p_ctx);
  153. } while (m_coap_client_running);
  154. IOT_CoAP_Deinit(&p_ctx);
  155. } else {
  156. HAL_Printf("IoTx CoAP init failed\r\n");
  157. }
  158. IOT_CloseLog();
  159. return 0;
  160. }