cmp-example.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <stdlib.h>
  20. #include <string.h>
  21. #include <stdarg.h>
  22. #include "lite-utils.h"
  23. #include "iot_import.h"
  24. #include "iot_export.h"
  25. #ifdef ON_DAILY
  26. #define IOTX_PRODUCT_KEY "gsYfsxQJgeD"
  27. #define IOTX_DEVICE_NAME "DailyEnvDN"
  28. #define IOTX_DEVICE_SECRET "y1vzFkEgcuXnvkAfm627pwarx4HRNikX"
  29. #define IOTX_PRODUCT_SECRET ""
  30. #define IOTX_DEVICE_ID "IoTxHttpTestDev_001"
  31. #else
  32. #define IOTX_PRODUCT_KEY "a1grYGVCPWl"
  33. #define IOTX_DEVICE_NAME "0402_09"
  34. #define IOTX_DEVICE_SECRET "DocxiD5wS6x9HNZrbC2CAoi9UnYP74Xt"
  35. #define IOTX_PRODUCT_SECRET ""
  36. #define IOTX_DEVICE_ID "IoTxHttpTestDev_001"
  37. #endif
  38. /* These are pre-defined topics */
  39. #define TOPIC_UPDATE "/"IOTX_PRODUCT_KEY"/"IOTX_DEVICE_NAME"/update"
  40. #define TOPIC_DATA "/"IOTX_PRODUCT_KEY"/"IOTX_DEVICE_NAME"/data"
  41. #define MQTT_MSGLEN (1024)
  42. #define EXAMPLE_TRACE(fmt, ...) \
  43. do { \
  44. HAL_Printf("%s|%03d :: ", __func__, __LINE__); \
  45. HAL_Printf(fmt, ##__VA_ARGS__); \
  46. HAL_Printf("%s", "\r\n"); \
  47. } while(0)
  48. static void _register_func(iotx_cmp_send_peer_pt source, iotx_cmp_message_info_pt msg, void *user_data)
  49. {
  50. printf("source %s:%s\n", source->product_key, source->device_name);
  51. printf("type %d\n", msg->message_type);
  52. printf("URI %s\n", msg->URI);
  53. printf("URI_type %d\n", msg->URI_type);
  54. printf("code %d\n", msg->code);
  55. printf("id %d\n", msg->id);
  56. printf("method %s\n", msg->method);
  57. printf("parameter %s\n", (char*)msg->parameter);
  58. }
  59. static void _event_handle(void *pcontext, iotx_cmp_event_msg_pt msg, void *user_data)
  60. {
  61. printf("event %d\n", msg->event_id);
  62. if (IOTX_CMP_EVENT_REGISTER_RESULT == msg->event_id) {
  63. iotx_cmp_event_result_pt result = (iotx_cmp_event_result_pt)msg->msg;
  64. printf("register result\n");
  65. printf("result %d\n", result->result);
  66. printf("URI %s\n", result->URI);
  67. printf("URI_type %d\n", result->URI_type);
  68. } else if (IOTX_CMP_EVENT_UNREGISTER_RESULT == msg->event_id) {
  69. iotx_cmp_event_result_pt result = (iotx_cmp_event_result_pt)msg->msg;
  70. printf("unregister result\n");
  71. printf("result %d\n", result->result);
  72. printf("URI %s\n", result->URI);
  73. printf("URI_type %d\n", result->URI_type);
  74. } else if (IOTX_CMP_EVENT_NEW_DATA_RECEIVED == msg->event_id) {
  75. iotx_cmp_new_data_pt new_data = (iotx_cmp_new_data_pt)msg->msg;
  76. _register_func(new_data->peer, new_data->message_info, user_data);
  77. }
  78. }
  79. int cmp_client()
  80. {
  81. int rc = -1;
  82. int user_data = 10;
  83. iotx_cmp_init_param_t param = {0};
  84. iotx_cmp_register_param_t register_param = {0};
  85. iotx_cmp_send_peer_t cloud_peer;
  86. iotx_cmp_message_info_t message_info = {0};
  87. iotx_cmp_unregister_param_t unregister_param = {0};
  88. param.domain_type = IOTX_CMP_CLOUD_DOMAIN_SH;
  89. param.event_func = _event_handle;
  90. param.user_data = &user_data;
  91. #ifdef SUPPORT_PRODUCT_SECRET
  92. param.secret_type = IOTX_CMP_DEVICE_SECRET_PRODUCT;
  93. #else
  94. param.secret_type = IOTX_CMP_DEVICE_SECRET_DEVICE;
  95. #endif /* SUPPORT_PRODUCT_SECRET */
  96. printf("init\n");
  97. rc = IOT_CMP_Init(&param, NULL);
  98. if (FAIL_RETURN == rc) {
  99. printf("init fail\n");
  100. IOT_CMP_Deinit(NULL);
  101. return FAIL_RETURN;
  102. }
  103. printf("register\n");
  104. register_param.URI_type = IOTX_CMP_URI_UNDEFINE;
  105. register_param.URI = TOPIC_DATA;
  106. register_param.message_type = IOTX_CMP_MESSAGE_REQUEST;
  107. register_param.register_func = _register_func;
  108. register_param.user_data = &user_data;
  109. rc = IOT_CMP_Register(&register_param, NULL);
  110. if (FAIL_RETURN == rc) {
  111. printf("register fail\n");
  112. IOT_CMP_Deinit(NULL);
  113. return FAIL_RETURN;
  114. }
  115. printf("register success \n");
  116. #ifndef CMP_SUPPORT_MULTI_THREAD
  117. rc = IOT_CMP_Yield(200, NULL);
  118. if (FAIL_RETURN == rc) {
  119. printf("yield fail\n");
  120. IOT_CMP_Deinit(NULL);
  121. return FAIL_RETURN;
  122. }
  123. #else
  124. HAL_SleepMs(2000);
  125. #endif
  126. printf("send\n");
  127. memset(&cloud_peer, 0x0, sizeof(iotx_cmp_send_peer_t));
  128. strncpy(cloud_peer.product_key, IOTX_PRODUCT_KEY, strlen(IOTX_PRODUCT_KEY));
  129. strncpy(cloud_peer.device_name, IOTX_DEVICE_NAME, strlen(IOTX_DEVICE_NAME));
  130. message_info.id = 0;
  131. message_info.message_type = IOTX_CMP_MESSAGE_REQUEST;
  132. message_info.URI = LITE_malloc(strlen(TOPIC_DATA) + 1);
  133. memset(message_info.URI, 0x0, strlen(TOPIC_DATA) + 1);
  134. strncpy(message_info.URI, TOPIC_DATA, strlen(TOPIC_DATA));
  135. message_info.URI_type = IOTX_CMP_URI_UNDEFINE;
  136. message_info.method = LITE_malloc(strlen("thing.data") + 1);
  137. memset(message_info.method, 0x0, strlen("thing.data") + 1);
  138. strncpy(message_info.method, "thing.data", strlen("thing.data"));
  139. message_info.parameter = LITE_malloc(strlen("{hello world!}") + 1);
  140. memset(message_info.parameter, 0x0, strlen("{hello world!}") + 1);
  141. strncpy(message_info.parameter, "{hello world!}", strlen("{hello world!}"));
  142. message_info.parameter_length = strlen(message_info.parameter);
  143. rc = IOT_CMP_Send(&cloud_peer, &message_info, NULL);
  144. LITE_free(message_info.URI);
  145. LITE_free(message_info.method);
  146. LITE_free(message_info.parameter);
  147. if (FAIL_RETURN == rc) {
  148. printf("send fail\n");
  149. IOT_CMP_Deinit(NULL);
  150. return FAIL_RETURN;
  151. }
  152. #ifndef CMP_SUPPORT_MULTI_THREAD
  153. rc = IOT_CMP_Yield(200, NULL);
  154. if (FAIL_RETURN == rc) {
  155. printf("register fail\n");
  156. IOT_CMP_Deinit(NULL);
  157. return FAIL_RETURN;
  158. }
  159. #else
  160. HAL_SleepMs(2000);
  161. #endif
  162. printf("unregister\n");
  163. unregister_param.URI_type = IOTX_CMP_URI_UNDEFINE;
  164. unregister_param.URI = TOPIC_DATA;
  165. rc = IOT_CMP_Unregister(&unregister_param, NULL);
  166. if (FAIL_RETURN == rc) {
  167. printf("unregister fail\n");
  168. IOT_CMP_Deinit(NULL);
  169. return FAIL_RETURN;
  170. }
  171. #ifndef CMP_SUPPORT_MULTI_THREAD
  172. rc = IOT_CMP_Yield(200, NULL);
  173. if (FAIL_RETURN == rc) {
  174. printf("register fail\n");
  175. IOT_CMP_Deinit(NULL);
  176. return FAIL_RETURN;
  177. }
  178. #else
  179. HAL_SleepMs(2000);
  180. #endif
  181. IOT_CMP_Deinit(NULL);
  182. return SUCCESS_RETURN;
  183. }
  184. int main(int argc, char **argv)
  185. {
  186. IOT_OpenLog("cmp\n");
  187. IOT_SetLogLevel(IOT_LOG_DEBUG);
  188. /**< set device info*/
  189. HAL_SetProductKey(IOTX_PRODUCT_KEY);
  190. HAL_SetDeviceName(IOTX_DEVICE_NAME);
  191. #ifndef SUPPORT_PRODUCT_SECRET
  192. HAL_SetDeviceSecret(IOTX_DEVICE_SECRET);
  193. #else
  194. HAL_SetProductSecret(IOTX_PRODUCT_SECRET);
  195. #endif /**< SUPPORT_PRODUCT_SECRET*/
  196. /**< end*/
  197. EXAMPLE_TRACE("start!\n");
  198. cmp_client();
  199. IOT_DumpMemoryStats(IOT_LOG_DEBUG);
  200. IOT_CloseLog();
  201. EXAMPLE_TRACE("out of sample!\n");
  202. return 0;
  203. }