dev_model_sample.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 <unistd.h>
  18. #include <limits.h>
  19. #include <string.h>
  20. #include "uiot_export.h"
  21. #include "uiot_import.h"
  22. #include "uiot_export_dm.h"
  23. static int running_state = 0;
  24. static void event_handler(void *pClient, void *handle_context, MQTTEventMsg *msg)
  25. {
  26. switch(msg->event_type) {
  27. case MQTT_EVENT_UNDEF:
  28. LOG_INFO("undefined event occur.\n");
  29. break;
  30. case MQTT_EVENT_DISCONNECT:
  31. LOG_INFO("MQTT disconnect.\n");
  32. break;
  33. case MQTT_EVENT_RECONNECT:
  34. LOG_INFO("MQTT reconnect.\n");
  35. break;
  36. case MQTT_EVENT_SUBSCRIBE_SUCCESS:
  37. LOG_INFO("subscribe success.\n");
  38. break;
  39. case MQTT_EVENT_SUBSCRIBE_TIMEOUT:
  40. LOG_INFO("subscribe wait ack timeout.\n");
  41. break;
  42. case MQTT_EVENT_SUBSCRIBE_NACK:
  43. LOG_INFO("subscribe nack.\n");
  44. break;
  45. case MQTT_EVENT_PUBLISH_SUCCESS:
  46. LOG_INFO("publish success.\n");
  47. break;
  48. case MQTT_EVENT_PUBLISH_TIMEOUT:
  49. LOG_INFO("publish timeout.\n");
  50. break;
  51. case MQTT_EVENT_PUBLISH_NACK:
  52. LOG_INFO("publish nack.\n");
  53. break;
  54. default:
  55. LOG_INFO("Should NOT arrive here.\n");
  56. break;
  57. }
  58. }
  59. int event_post_cb(const char *request_id, const int ret_code){
  60. LOG_INFO("event_post_cb; request_id: %s; ret_code: %d", request_id, ret_code);
  61. return SUCCESS_RET;
  62. }
  63. int property_post_cb(const char *request_id, const int ret_code){
  64. LOG_INFO("property_post_cb; request_id: %s; ret_code: %d", request_id, ret_code);
  65. return SUCCESS_RET;
  66. }
  67. int command_cb(const char *request_id, const char *identifier, const char *input, char *output){
  68. LOG_INFO("command_cb; request_id: %s; identifier: %s; input: %s", request_id, identifier, input);
  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. initParams->device_secret = PKG_USING_UCLOUD_IOT_SDK_DEVICE_SECRET;
  81. initParams->command_timeout = UIOT_MQTT_COMMAND_TIMEOUT;
  82. initParams->keep_alive_interval = UIOT_MQTT_KEEP_ALIVE_INTERNAL;
  83. initParams->auto_connect_enable = 1;
  84. initParams->event_handler.h_fp = event_handler;
  85. return SUCCESS_RET;
  86. }
  87. static void mqtt_devmodel_thread(void)
  88. {
  89. int rc;
  90. int i = 0;
  91. MQTTInitParams init_params = DEFAULT_MQTT_INIT_PARAMS;
  92. rc = _setup_connect_init_params(&init_params);
  93. if (rc != SUCCESS_RET) {
  94. return;
  95. }
  96. void *client = IOT_MQTT_Construct(&init_params);
  97. if (client != NULL) {
  98. LOG_INFO("Cloud Device Construct Success");
  99. } else {
  100. LOG_ERROR("Cloud Device Construct Failed");
  101. return;
  102. }
  103. IOT_MQTT_Yield(client, 50);
  104. void *h_dm = IOT_DM_Init(PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SN, PKG_USING_UCLOUD_IOT_SDK_DEVICE_SN, client);
  105. if (NULL == h_dm) {
  106. IOT_MQTT_Destroy(&client);
  107. LOG_ERROR("initialize device model failed");
  108. return;
  109. }
  110. IOT_DM_Yield(h_dm, 50);
  111. IOT_DM_RegisterCallback(EVENT_POST, h_dm, event_post_cb);
  112. IOT_DM_RegisterCallback(COMMAND , h_dm, command_cb);
  113. IOT_DM_RegisterCallback(PROPERTY_POST , h_dm, property_post_cb);
  114. IOT_DM_RegisterCallback(PROPERTY_SET , h_dm, property_set_cb);
  115. IOT_DM_Yield(h_dm, 200);
  116. for (i = 0; i < 10; i++) {
  117. IOT_DM_Property_Report(h_dm, PROPERTY_POST, i * 2, "\"volume\": {\"Value\":50}");
  118. IOT_DM_TriggerEvent(h_dm, i * 2 + 1, "low_power_alert", "\"power\": 5");
  119. IOT_DM_Yield(h_dm, 200);
  120. HAL_SleepMs(2000);
  121. }
  122. //等待属性设置及命令下发
  123. IOT_DM_Yield(h_dm, 60000);
  124. IOT_DM_Destroy(h_dm);
  125. IOT_MQTT_Destroy(&client);
  126. return;
  127. }
  128. static int devmodel_test_example(int argc, char **argv)
  129. {
  130. rt_thread_t tid;
  131. int stack_size = 8192;
  132. if (2 == argc)
  133. {
  134. if (!strcmp("start", argv[1]))
  135. {
  136. if (1 == running_state)
  137. {
  138. HAL_Printf("devmodel_test_example is already running\n");
  139. return 0;
  140. }
  141. }
  142. else if (!strcmp("stop", argv[1]))
  143. {
  144. if (0 == running_state)
  145. {
  146. HAL_Printf("devmodel_test_example is already stopped\n");
  147. return 0;
  148. }
  149. running_state = 0;
  150. return 0;
  151. }
  152. else
  153. {
  154. HAL_Printf("Usage: devmodel_test_example start/stop");
  155. return 0;
  156. }
  157. }
  158. else
  159. {
  160. HAL_Printf("Para err, usage: devmodel_test_example start/stop");
  161. return 0;
  162. }
  163. tid = rt_thread_create("devmodel_test", (void (*)(void *))mqtt_devmodel_thread,
  164. NULL, stack_size, RT_THREAD_PRIORITY_MAX / 2 - 1, 100);
  165. if (tid != RT_NULL)
  166. {
  167. rt_thread_startup(tid);
  168. }
  169. return 0;
  170. }
  171. #ifdef RT_USING_FINSH
  172. #include <finsh.h>
  173. FINSH_FUNCTION_EXPORT(devmodel_test_example, startup mqtt devmodel example);
  174. #endif
  175. #ifdef FINSH_USING_MSH
  176. MSH_CMD_EXPORT(devmodel_test_example, startup mqtt devmodel example);
  177. #endif