dev_model_sample.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. *output = (char *)HAL_Malloc(100);
  70. HAL_Snprintf(*output, 1000, "{\"result\":%d}", 1);
  71. return SUCCESS_RET;
  72. }
  73. int property_set_cb(const char *request_id, const char *property){
  74. LOG_INFO("property_set_cb; request_id: %s; property: %s", request_id, property);
  75. return SUCCESS_RET;
  76. }
  77. static int _setup_connect_init_params(MQTTInitParams* initParams)
  78. {
  79. initParams->device_sn = PKG_USING_UCLOUD_IOT_DEVICE_SN;
  80. initParams->product_sn = PKG_USING_UCLOUD_IOT_PRODUCT_SN;
  81. initParams->device_secret = PKG_USING_UCLOUD_IOT_DEVICE_SECRET;
  82. initParams->command_timeout = UIOT_MQTT_COMMAND_TIMEOUT;
  83. initParams->keep_alive_interval = UIOT_MQTT_KEEP_ALIVE_INTERNAL;
  84. initParams->auto_connect_enable = 1;
  85. initParams->event_handler.h_fp = event_handler;
  86. return SUCCESS_RET;
  87. }
  88. static void mqtt_devmodel_thread(void)
  89. {
  90. int rc;
  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_PRODUCT_SN, PKG_USING_UCLOUD_IOT_DEVICE_SN, client);
  105. if (NULL == h_dm) {
  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. for (int i = 0; i < 10; i++) {
  115. IOT_DM_Property_Report(h_dm, PROPERTY_POST, i * 2, "{\"volume\": {\"Value\":50}}");
  116. IOT_DM_TriggerEvent(h_dm, i * 2 + 1, "low_power_alert", "{\"power\": 5}");
  117. IOT_DM_Yield(h_dm, 200);
  118. HAL_SleepMs(2000);
  119. }
  120. //等待属性设置及命令下发
  121. IOT_DM_Yield(h_dm, 60000);
  122. IOT_DM_Destroy(h_dm);
  123. IOT_MQTT_Destroy(&client);
  124. return;
  125. }
  126. static int devmodel_test_example(int argc, char **argv)
  127. {
  128. rt_thread_t tid;
  129. int stack_size = 8192;
  130. if (2 == argc)
  131. {
  132. if (!strcmp("start", argv[1]))
  133. {
  134. if (1 == running_state)
  135. {
  136. HAL_Printf("devmodel_test_example is already running\n");
  137. return 0;
  138. }
  139. }
  140. else if (!strcmp("stop", argv[1]))
  141. {
  142. if (0 == running_state)
  143. {
  144. HAL_Printf("devmodel_test_example is already stopped\n");
  145. return 0;
  146. }
  147. running_state = 0;
  148. return 0;
  149. }
  150. else
  151. {
  152. HAL_Printf("Usage: devmodel_test_example start/stop");
  153. return 0;
  154. }
  155. }
  156. else
  157. {
  158. HAL_Printf("Para err, usage: devmodel_test_example start/stop");
  159. return 0;
  160. }
  161. tid = rt_thread_create("devmodel_test", (void (*)(void *))mqtt_devmodel_thread,
  162. NULL, stack_size, RT_THREAD_PRIORITY_MAX / 2 - 1, 100);
  163. if (tid != RT_NULL)
  164. {
  165. rt_thread_startup(tid);
  166. }
  167. return 0;
  168. }
  169. #ifdef RT_USING_FINSH
  170. #include <finsh.h>
  171. FINSH_FUNCTION_EXPORT(devmodel_test_example, startup mqtt devmodel example);
  172. #endif
  173. #ifdef FINSH_USING_MSH
  174. MSH_CMD_EXPORT(devmodel_test_example, startup mqtt devmodel example);
  175. #endif