shadow_sample.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Tencent is pleased to support the open source community by making IoT Hub available.
  3. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  4. * Licensed under the MIT License (the "License"); you may not use this file except in
  5. * compliance with the License. You may obtain a copy of the License at
  6. * http://opensource.org/licenses/MIT
  7. * Unless required by applicable law or agreed to in writing, software distributed under the License is
  8. * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. * either express or implied. See the License for the specific language governing permissions and
  10. * limitations under the License.
  11. *
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <limits.h>
  16. #include <stdbool.h>
  17. #include <string.h>
  18. #include <signal.h>
  19. #include "uiot_export_shadow.h"
  20. #include "uiot_import.h"
  21. #include "shadow_client.h"
  22. #define MAX_SIZE_OF_TOPIC_CONTENT 100
  23. #define SIZE_OF_JSON_BUFFER 256
  24. static int running_state = 0;
  25. static UIoT_Shadow *sg_pshadow;
  26. static MQTTInitParams sg_initParams = DEFAULT_MQTT_INIT_PARAMS;
  27. //当设备直接按照desired字段中的属性值更新时不需要上报
  28. void RegCallback_update(void *pClient, RequestParams *pParams, char *pJsonValueBuffer, uint32_t valueLength, DeviceProperty *pProperty)
  29. {
  30. LOG_DEBUG("key:%s val:%s\n",pProperty->key, pJsonValueBuffer);
  31. IOT_Shadow_Direct_Update_Value(pJsonValueBuffer, pProperty);
  32. return;
  33. }
  34. //当设备没有完全按照desired字段中的属性更新时,需要将当前真实值上报
  35. void RegCallback_hold(void *pClient, RequestParams *pParams, char *pJsonValueBuffer, uint32_t valueLength, DeviceProperty *pProperty)
  36. {
  37. LOG_DEBUG("key:%s val:%s\n",pProperty->key, pJsonValueBuffer);
  38. int num = 10;
  39. pProperty->data = &num;
  40. IOT_Shadow_Request_Add_Delta_Property(pClient, pParams,pProperty);
  41. return;
  42. }
  43. static void _update_ack_cb(void *pClient, Method method, RequestAck requestAck, const char *pReceivedJsonDocument, void *pUserdata)
  44. {
  45. LOG_DEBUG("requestAck=%d\n", requestAck);
  46. if (NULL != pReceivedJsonDocument) {
  47. LOG_DEBUG("Received Json Document=%s\n", pReceivedJsonDocument);
  48. } else {
  49. LOG_DEBUG("Received Json Document is NULL\n");
  50. }
  51. *((RequestAck *)pUserdata) = requestAck;
  52. return;
  53. }
  54. /**
  55. * 设置MQTT connet初始化参数
  56. *
  57. * @param initParams MQTT connet初始化参数
  58. *
  59. * @return 0: 参数初始化成功 非0: 失败
  60. */
  61. static int _setup_connect_init_params(MQTTInitParams* initParams)
  62. {
  63. int ret = SUCCESS_RET;
  64. initParams->device_sn = (char *)PKG_USING_UCLOUD_IOT_SDK_DEVICE_SN;
  65. initParams->product_sn = (char *)PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SN;
  66. #ifdef PKG_USING_UCLOUD_MQTT_DYNAMIC_AUTH
  67. initParams->device_secret = (char *) malloc(IOT_DEVICE_SN_LEN + 1);
  68. HAL_GetDeviceSecret(initParams->device_secret);
  69. #else
  70. initParams->device_secret = (char *)PKG_USING_UCLOUD_IOT_SDK_DEVICE_SECRET;
  71. #endif
  72. initParams->command_timeout = UIOT_MQTT_COMMAND_TIMEOUT;
  73. initParams->keep_alive_interval = UIOT_MQTT_KEEP_ALIVE_INTERNAL;
  74. initParams->auto_connect_enable = 1;
  75. return ret;
  76. }
  77. static void shadow_test_thread(void)
  78. {
  79. int ret = SUCCESS_RET;
  80. ret = _setup_connect_init_params(&sg_initParams);
  81. if(ret != SUCCESS_RET)
  82. {
  83. HAL_Printf("_setup_connect_init_params fail:%d\n", ret);
  84. return;
  85. }
  86. void *mqtt_client = IOT_MQTT_Construct(&sg_initParams);
  87. if(mqtt_client == NULL)
  88. {
  89. HAL_Printf("IOT_MQTT_Construct fail\n");
  90. return;
  91. }
  92. void *shadow_client = IOT_Shadow_Construct(PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SN, PKG_USING_UCLOUD_IOT_SDK_DEVICE_SN, mqtt_client);
  93. if(shadow_client == NULL)
  94. {
  95. HAL_Printf("IOT_Shadow_Construct fail\n");
  96. return;
  97. }
  98. sg_pshadow = (UIoT_Shadow *)shadow_client;
  99. bool isConnected = IOT_MQTT_IsConnected(sg_pshadow->mqtt);
  100. if(isConnected != true)
  101. {
  102. HAL_Printf("IOT_MQTT_IsConnected fail\n");
  103. return;
  104. }
  105. int time_sec = MAX_WAIT_TIME_SEC;
  106. RequestAck ack_update = ACK_NONE;
  107. DeviceProperty *Property1 = (DeviceProperty *)HAL_Malloc(sizeof(DeviceProperty));
  108. int32_t num1 = 18;
  109. char str1[6] = "data1";
  110. Property1->key= str1;
  111. Property1->data = &num1;
  112. Property1->type = JINT32;
  113. ret = IOT_Shadow_Register_Property(sg_pshadow, Property1, RegCallback_hold);
  114. if(SUCCESS_RET != ret)
  115. {
  116. HAL_Printf("Register Property1 fail:%d\n", ret);
  117. return;
  118. }
  119. DeviceProperty *Property2 = (DeviceProperty *)HAL_Malloc(sizeof(DeviceProperty));
  120. float num2 = 20.2;
  121. char str2[6] = "data2";
  122. Property2->key= str2;
  123. Property2->data = &num2;
  124. Property2->type = JFLOAT;
  125. ret = IOT_Shadow_Register_Property(sg_pshadow, Property2, RegCallback_update);
  126. if(SUCCESS_RET != ret)
  127. {
  128. HAL_Printf("Register Property2 fail:%d\n", ret);
  129. return;
  130. }
  131. DeviceProperty *Property3 = (DeviceProperty *)HAL_Malloc(sizeof(DeviceProperty));
  132. double num3 = 22.9;
  133. char str3[6] = "data3";
  134. Property3->key= str3;
  135. Property3->data = &num3;
  136. Property3->type = JDOUBLE;
  137. ret = IOT_Shadow_Register_Property(sg_pshadow, Property3, RegCallback_update);
  138. if(SUCCESS_RET != ret)
  139. {
  140. HAL_Printf("Register Property3 fail:%d\n", ret);
  141. return;
  142. }
  143. DeviceProperty *Property4 = (DeviceProperty *)HAL_Malloc(sizeof(DeviceProperty));
  144. char num4[5] = "num4";
  145. char str4[6] = "data4";
  146. Property4->key= str4;
  147. Property4->data = num4;
  148. Property4->type = JSTRING;
  149. ret = IOT_Shadow_Register_Property(sg_pshadow, Property4, RegCallback_update);
  150. if(SUCCESS_RET != ret)
  151. {
  152. HAL_Printf("Register Property4 fail:%d\n", ret);
  153. return;
  154. }
  155. DeviceProperty *Property5 = (DeviceProperty *)HAL_Malloc(sizeof(DeviceProperty));
  156. bool num5 = false;
  157. char str5[6] = "data5";
  158. Property5->key= str5;
  159. Property5->data = &num5;
  160. Property5->type = JBOOL;
  161. ret = IOT_Shadow_Register_Property(sg_pshadow, Property5, RegCallback_update);
  162. if(SUCCESS_RET != ret)
  163. {
  164. HAL_Printf("Register Property5 fail:%d\n", ret);
  165. return;
  166. }
  167. DeviceProperty *Property6 = (DeviceProperty *)HAL_Malloc(sizeof(DeviceProperty));
  168. char num6[20] = "{\"temp\":25}";
  169. char str6[6] = "data6";
  170. Property6->key= str6;
  171. Property6->data = num6;
  172. Property6->type = JOBJECT;
  173. ret = IOT_Shadow_Register_Property(sg_pshadow, Property6, RegCallback_update);
  174. if(SUCCESS_RET != ret)
  175. {
  176. HAL_Printf("Register Property6 fail:%d\n", ret);
  177. return;
  178. }
  179. /* 先同步一下版本号和设备掉电期间更新的属性 */
  180. ret = IOT_Shadow_Get_Sync(sg_pshadow, _update_ack_cb, time_sec, &ack_update);
  181. if(SUCCESS_RET != ret)
  182. {
  183. HAL_Printf("Get Sync fail:%d\n", ret);
  184. return;
  185. }
  186. while (ACK_NONE == ack_update) {
  187. IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS);
  188. }
  189. /* update */
  190. ack_update = ACK_NONE;
  191. ret = IOT_Shadow_Update(sg_pshadow, _update_ack_cb, time_sec, &ack_update, 6, Property1, Property2, Property3, Property4, Property5, Property6);
  192. if(SUCCESS_RET != ret)
  193. {
  194. HAL_Printf("Update Property1 Property2 Property3 Property4 Property5 Property6 fail:%d\n", ret);
  195. return;
  196. }
  197. while (ACK_NONE == ack_update) {
  198. IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS);
  199. }
  200. ack_update = ACK_NONE;
  201. ret = IOT_Shadow_Get_Sync(sg_pshadow, _update_ack_cb, time_sec, &ack_update);
  202. while (ACK_NONE == ack_update) {
  203. IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS);
  204. }
  205. HAL_Free(Property1);
  206. HAL_Free(Property2);
  207. HAL_Free(Property3);
  208. HAL_Free(Property4);
  209. HAL_Free(Property5);
  210. HAL_Free(Property6);
  211. IOT_Shadow_Destroy(sg_pshadow);
  212. IOT_MQTT_Destroy(&mqtt_client);
  213. return;
  214. }
  215. static int shadow_test_example(int argc, char **argv)
  216. {
  217. rt_thread_t tid;
  218. int stack_size = 8192;
  219. if (2 == argc)
  220. {
  221. if (!strcmp("start", argv[1]))
  222. {
  223. if (1 == running_state)
  224. {
  225. HAL_Printf("shadow_test_example is already running\n");
  226. return 0;
  227. }
  228. }
  229. else if (!strcmp("stop", argv[1]))
  230. {
  231. if (0 == running_state)
  232. {
  233. HAL_Printf("shadow_test_example is already stopped\n");
  234. return 0;
  235. }
  236. running_state = 0;
  237. return 0;
  238. }
  239. else
  240. {
  241. HAL_Printf("Usage: shadow_test_example start/stop");
  242. return 0;
  243. }
  244. }
  245. else
  246. {
  247. HAL_Printf("Para err, usage: shadow_test_example start/stop");
  248. return 0;
  249. }
  250. tid = rt_thread_create("shadow_test", (void (*)(void *))shadow_test_thread,
  251. NULL, stack_size, RT_THREAD_PRIORITY_MAX / 2 - 1, 100);
  252. if (tid != RT_NULL)
  253. {
  254. rt_thread_startup(tid);
  255. }
  256. return 0;
  257. }
  258. #ifdef RT_USING_FINSH
  259. #include <finsh.h>
  260. FINSH_FUNCTION_EXPORT(shadow_test_example, startup mqtt shadow example);
  261. #endif
  262. #ifdef FINSH_USING_MSH
  263. MSH_CMD_EXPORT(shadow_test_example, startup mqtt shadow example);
  264. #endif