dev_model_sample.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (C) 2012-2019 UCloud. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <limits.h>
  18. #include <string.h>
  19. #include "uiot_export.h"
  20. #include "uiot_import.h"
  21. #include "uiot_export_dm.h"
  22. static int running_state = 0;
  23. static void event_handler(void *pClient, void *handle_context, MQTTEventMsg *msg)
  24. {
  25. switch(msg->event_type) {
  26. case MQTT_EVENT_UNDEF:
  27. LOG_INFO("undefined event occur.\n");
  28. break;
  29. case MQTT_EVENT_DISCONNECT:
  30. LOG_INFO("MQTT disconnect.\n");
  31. break;
  32. case MQTT_EVENT_RECONNECT:
  33. LOG_INFO("MQTT reconnect.\n");
  34. break;
  35. case MQTT_EVENT_SUBSCRIBE_SUCCESS:
  36. LOG_INFO("subscribe success.\n");
  37. break;
  38. case MQTT_EVENT_SUBSCRIBE_TIMEOUT:
  39. LOG_INFO("subscribe wait ack timeout.\n");
  40. break;
  41. case MQTT_EVENT_SUBSCRIBE_NACK:
  42. LOG_INFO("subscribe nack.\n");
  43. break;
  44. case MQTT_EVENT_PUBLISH_SUCCESS:
  45. LOG_INFO("publish success.\n");
  46. break;
  47. case MQTT_EVENT_PUBLISH_TIMEOUT:
  48. LOG_INFO("publish timeout.\n");
  49. break;
  50. case MQTT_EVENT_PUBLISH_NACK:
  51. LOG_INFO("publish nack.\n");
  52. break;
  53. default:
  54. LOG_INFO("Should NOT arrive here.\n");
  55. break;
  56. }
  57. }
  58. int event_post_cb(const char *request_id, const int ret_code){
  59. LOG_INFO("event_post_cb; request_id: %s; ret_code: %d", request_id, ret_code);
  60. return SUCCESS_RET;
  61. }
  62. int property_post_cb(const char *request_id, const int ret_code){
  63. LOG_INFO("property_post_cb; request_id: %s; ret_code: %d", request_id, ret_code);
  64. return SUCCESS_RET;
  65. }
  66. int command_cb(const char *request_id, const char *identifier, const char *input, char **output){
  67. LOG_INFO("command_cb; request_id: %s; identifier: %s; input: %s", request_id, identifier, input);
  68. *output = (char *)HAL_Malloc(100);
  69. HAL_Snprintf(*output, 1000, "{\"result\":%d}", 1);
  70. return SUCCESS_RET;
  71. }
  72. int property_set_cb(const char *request_id, const char *property){
  73. LOG_INFO("property_set_cb; request_id: %s; property: %s", request_id, property);
  74. return SUCCESS_RET;
  75. }
  76. static int _setup_connect_init_params(MQTTInitParams* initParams)
  77. {
  78. initParams->device_sn = PKG_USING_UCLOUD_IOT_SDK_DEVICE_SN;
  79. initParams->product_sn = PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SN;
  80. #ifdef PKG_USING_UCLOUD_MQTT_DYNAMIC_AUTH
  81. initParams->device_secret = (char *) malloc(IOT_DEVICE_SN_LEN + 1);
  82. HAL_GetDeviceSecret(initParams->device_secret);
  83. #else
  84. initParams->device_secret = PKG_USING_UCLOUD_IOT_SDK_DEVICE_SECRET;
  85. #endif
  86. initParams->command_timeout = UIOT_MQTT_COMMAND_TIMEOUT;
  87. initParams->keep_alive_interval = UIOT_MQTT_KEEP_ALIVE_INTERNAL;
  88. initParams->auto_connect_enable = 1;
  89. initParams->event_handler.h_fp = event_handler;
  90. return SUCCESS_RET;
  91. }
  92. static void mqtt_devmodel_thread(void)
  93. {
  94. int rc;
  95. MQTTInitParams init_params = DEFAULT_MQTT_INIT_PARAMS;
  96. rc = _setup_connect_init_params(&init_params);
  97. if (rc != SUCCESS_RET) {
  98. return;
  99. }
  100. void *client = IOT_MQTT_Construct(&init_params);
  101. if (client != NULL) {
  102. LOG_INFO("Cloud Device Construct Success");
  103. } else {
  104. LOG_ERROR("Cloud Device Construct Failed");
  105. return;
  106. }
  107. IOT_MQTT_Yield(client, 50);
  108. void *h_dm = IOT_DM_Init(PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SN, PKG_USING_UCLOUD_IOT_SDK_DEVICE_SN, client);
  109. if (NULL == h_dm) {
  110. LOG_ERROR("initialize device model failed");
  111. return;
  112. }
  113. IOT_DM_Yield(h_dm, 50);
  114. IOT_DM_RegisterCallback(EVENT_POST, h_dm, event_post_cb);
  115. IOT_DM_RegisterCallback(COMMAND , h_dm, command_cb);
  116. IOT_DM_RegisterCallback(PROPERTY_POST , h_dm, property_post_cb);
  117. IOT_DM_RegisterCallback(PROPERTY_SET , h_dm, property_set_cb);
  118. for (int i = 0; i < 10; i++) {
  119. IOT_DM_Property_Report(h_dm, PROPERTY_POST, i * 2, "{\"volume\": {\"Value\":50}}");
  120. IOT_DM_TriggerEvent(h_dm, i * 2 + 1, "low_power_alert", "{\"power\": 5}");
  121. IOT_DM_Yield(h_dm, 200);
  122. HAL_SleepMs(2000);
  123. }
  124. //等待属性设置及命令下发
  125. IOT_DM_Yield(h_dm, 60000);
  126. IOT_DM_Destroy(h_dm);
  127. IOT_MQTT_Destroy(&client);
  128. return;
  129. }
  130. static int devmodel_test_example(int argc, char **argv)
  131. {
  132. rt_thread_t tid;
  133. int stack_size = 8192;
  134. if (2 == argc)
  135. {
  136. if (!strcmp("start", argv[1]))
  137. {
  138. if (1 == running_state)
  139. {
  140. HAL_Printf("devmodel_test_example is already running\n");
  141. return 0;
  142. }
  143. }
  144. else if (!strcmp("stop", argv[1]))
  145. {
  146. if (0 == running_state)
  147. {
  148. HAL_Printf("devmodel_test_example is already stopped\n");
  149. return 0;
  150. }
  151. running_state = 0;
  152. return 0;
  153. }
  154. else
  155. {
  156. HAL_Printf("Usage: devmodel_test_example start/stop");
  157. return 0;
  158. }
  159. }
  160. else
  161. {
  162. HAL_Printf("Para err, usage: devmodel_test_example start/stop");
  163. return 0;
  164. }
  165. tid = rt_thread_create("devmodel_test", (void (*)(void *))mqtt_devmodel_thread,
  166. NULL, stack_size, RT_THREAD_PRIORITY_MAX / 2 - 1, 100);
  167. if (tid != RT_NULL)
  168. {
  169. rt_thread_startup(tid);
  170. }
  171. return 0;
  172. }
  173. #ifdef RT_USING_FINSH
  174. #include <finsh.h>
  175. FINSH_FUNCTION_EXPORT(devmodel_test_example, startup mqtt devmodel example);
  176. #endif
  177. #ifdef FINSH_USING_MSH
  178. MSH_CMD_EXPORT(devmodel_test_example, startup mqtt devmodel example);
  179. #endif