dev_model_sample.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. 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. MQTTInitParams init_params = DEFAULT_MQTT_INIT_PARAMS;
  91. rc = _setup_connect_init_params(&init_params);
  92. if (rc != SUCCESS_RET) {
  93. return;
  94. }
  95. void *client = IOT_MQTT_Construct(&init_params);
  96. if (client != NULL) {
  97. LOG_INFO("Cloud Device Construct Success");
  98. } else {
  99. LOG_ERROR("Cloud Device Construct Failed");
  100. return;
  101. }
  102. IOT_MQTT_Yield(client, 50);
  103. void *h_dm = IOT_DM_Init(PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SN, PKG_USING_UCLOUD_IOT_SDK_DEVICE_SN, client);
  104. if (NULL == h_dm) {
  105. IOT_MQTT_Destroy(&client);
  106. LOG_ERROR("initialize device model failed");
  107. return;
  108. }
  109. IOT_DM_Yield(h_dm, 50);
  110. IOT_DM_RegisterCallback(EVENT_POST, h_dm, event_post_cb);
  111. IOT_DM_RegisterCallback(COMMAND , h_dm, command_cb);
  112. IOT_DM_RegisterCallback(PROPERTY_POST , h_dm, property_post_cb);
  113. IOT_DM_RegisterCallback(PROPERTY_SET , h_dm, property_set_cb);
  114. IOT_DM_Yield(h_dm, 200);
  115. for (int i = 0; i < 10; i++) {
  116. IOT_DM_Property_Report(h_dm, PROPERTY_POST, i * 2, "{\"volume\": {\"Value\":50}}");
  117. IOT_DM_TriggerEvent(h_dm, i * 2 + 1, "low_power_alert", "{\"power\": 5}");
  118. IOT_DM_Yield(h_dm, 200);
  119. HAL_SleepMs(2000);
  120. }
  121. //等待属性设置及命令下发
  122. IOT_DM_Yield(h_dm, 60000);
  123. IOT_DM_Destroy(h_dm);
  124. IOT_MQTT_Destroy(&client);
  125. return;
  126. }
  127. static int devmodel_test_example(int argc, char **argv)
  128. {
  129. rt_thread_t tid;
  130. int stack_size = 8192;
  131. if (2 == argc)
  132. {
  133. if (!strcmp("start", argv[1]))
  134. {
  135. if (1 == running_state)
  136. {
  137. HAL_Printf("devmodel_test_example is already running\n");
  138. return 0;
  139. }
  140. }
  141. else if (!strcmp("stop", argv[1]))
  142. {
  143. if (0 == running_state)
  144. {
  145. HAL_Printf("devmodel_test_example is already stopped\n");
  146. return 0;
  147. }
  148. running_state = 0;
  149. return 0;
  150. }
  151. else
  152. {
  153. HAL_Printf("Usage: devmodel_test_example start/stop");
  154. return 0;
  155. }
  156. }
  157. else
  158. {
  159. HAL_Printf("Para err, usage: devmodel_test_example start/stop");
  160. return 0;
  161. }
  162. tid = rt_thread_create("devmodel_test", (void (*)(void *))mqtt_devmodel_thread,
  163. NULL, stack_size, RT_THREAD_PRIORITY_MAX / 2 - 1, 100);
  164. if (tid != RT_NULL)
  165. {
  166. rt_thread_startup(tid);
  167. }
  168. return 0;
  169. }
  170. #ifdef RT_USING_FINSH
  171. #include <finsh.h>
  172. FINSH_FUNCTION_EXPORT(devmodel_test_example, startup mqtt devmodel example);
  173. #endif
  174. #ifdef FINSH_USING_MSH
  175. MSH_CMD_EXPORT(devmodel_test_example, startup mqtt devmodel example);
  176. #endif