shadow_sample.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 <unistd.h>
  16. #include <limits.h>
  17. #include <stdbool.h>
  18. #include <string.h>
  19. #include <signal.h>
  20. #include "uiot_export_shadow.h"
  21. #include "uiot_import.h"
  22. #include "shadow_client.h"
  23. #define MAX_SIZE_OF_TOPIC_CONTENT 100
  24. #define SIZE_OF_JSON_BUFFER 256
  25. static int running_state = 0;
  26. static UIoT_Shadow *sg_pshadow;
  27. static MQTTInitParams sg_initParams = DEFAULT_MQTT_INIT_PARAMS;
  28. //当设备直接按照desired字段中的属性值更新时不需要上报
  29. void RegCallback_update(void *pClient, RequestParams *pParams, char *pJsonValueBuffer, uint32_t valueLength, DeviceProperty *pProperty)
  30. {
  31. LOG_DEBUG("key:%s val:%s\n",pProperty->key, pJsonValueBuffer);
  32. IOT_Shadow_Direct_Update_Value(pJsonValueBuffer, pProperty);
  33. return;
  34. }
  35. //当设备没有完全按照desired字段中的属性更新时,需要将当前真实值上报
  36. void RegCallback_hold(void *pClient, RequestParams *pParams, char *pJsonValueBuffer, uint32_t valueLength, DeviceProperty *pProperty)
  37. {
  38. LOG_DEBUG("key:%s val:%s\n",pProperty->key, pJsonValueBuffer);
  39. int num = 10;
  40. pProperty->data = &num;
  41. IOT_Shadow_Request_Add_Delta_Property(pClient, pParams,pProperty);
  42. return;
  43. }
  44. static void _update_ack_cb(void *pClient, Method method, RequestAck requestAck, const char *pReceivedJsonDocument, void *pUserdata)
  45. {
  46. LOG_DEBUG("requestAck=%d\n", requestAck);
  47. if (NULL != pReceivedJsonDocument) {
  48. LOG_DEBUG("Received Json Document=%s\n", pReceivedJsonDocument);
  49. } else {
  50. LOG_DEBUG("Received Json Document is NULL\n");
  51. }
  52. *((RequestAck *)pUserdata) = requestAck;
  53. return;
  54. }
  55. /**
  56. * 设置MQTT connet初始化参数
  57. *
  58. * @param initParams MQTT connet初始化参数
  59. *
  60. * @return 0: 参数初始化成功 非0: 失败
  61. */
  62. static int _setup_connect_init_params(MQTTInitParams* initParams)
  63. {
  64. int ret = SUCCESS_RET;
  65. initParams->device_sn = (char *)PKG_USING_UCLOUD_IOT_DEVICE_SN;
  66. initParams->product_sn = (char *)PKG_USING_UCLOUD_IOT_PRODUCT_SN;
  67. initParams->device_secret = (char *)PKG_USING_UCLOUD_IOT_DEVICE_SECRET;
  68. initParams->command_timeout = UIOT_MQTT_COMMAND_TIMEOUT;
  69. initParams->keep_alive_interval = UIOT_MQTT_KEEP_ALIVE_INTERNAL;
  70. initParams->auto_connect_enable = 1;
  71. return ret;
  72. }
  73. static void shadow_test_thread(void)
  74. {
  75. int ret = SUCCESS_RET;
  76. ret = _setup_connect_init_params(&sg_initParams);
  77. if(ret != SUCCESS_RET)
  78. {
  79. HAL_Printf("_setup_connect_init_params fail:%d\n", ret);
  80. return;
  81. }
  82. void *mqtt_client = IOT_MQTT_Construct(&sg_initParams);
  83. if(mqtt_client == NULL)
  84. {
  85. HAL_Printf("IOT_MQTT_Construct fail\n");
  86. return;
  87. }
  88. void *shadow_client = IOT_Shadow_Construct(PKG_USING_UCLOUD_IOT_PRODUCT_SN, PKG_USING_UCLOUD_IOT_DEVICE_SN, mqtt_client);
  89. if(shadow_client == NULL)
  90. {
  91. HAL_Printf("IOT_Shadow_Construct fail\n");
  92. return;
  93. }
  94. sg_pshadow = (UIoT_Shadow *)shadow_client;
  95. bool isConnected = IOT_MQTT_IsConnected(sg_pshadow->mqtt);
  96. if(isConnected != true)
  97. {
  98. HAL_Printf("IOT_MQTT_IsConnected fail\n");
  99. return;
  100. }
  101. int time_sec = MAX_WAIT_TIME_SEC;
  102. RequestAck ack_update = ACK_NONE;
  103. DeviceProperty *Property1 = (DeviceProperty *)HAL_Malloc(sizeof(DeviceProperty));
  104. int32_t num1 = 18;
  105. char str1[6] = "data1";
  106. Property1->key= str1;
  107. Property1->data = &num1;
  108. Property1->type = JINT32;
  109. ret = IOT_Shadow_Register_Property(sg_pshadow, Property1, RegCallback_hold);
  110. if(SUCCESS_RET != ret)
  111. {
  112. HAL_Printf("Register Property1 fail:%d\n", ret);
  113. return;
  114. }
  115. DeviceProperty *Property2 = (DeviceProperty *)HAL_Malloc(sizeof(DeviceProperty));
  116. float num2 = 20.2;
  117. char str2[6] = "data2";
  118. Property2->key= str2;
  119. Property2->data = &num2;
  120. Property2->type = JFLOAT;
  121. ret = IOT_Shadow_Register_Property(sg_pshadow, Property2, RegCallback_update);
  122. if(SUCCESS_RET != ret)
  123. {
  124. HAL_Printf("Register Property2 fail:%d\n", ret);
  125. return;
  126. }
  127. DeviceProperty *Property3 = (DeviceProperty *)HAL_Malloc(sizeof(DeviceProperty));
  128. double num3 = 22.9;
  129. char str3[6] = "data3";
  130. Property3->key= str3;
  131. Property3->data = &num3;
  132. Property3->type = JDOUBLE;
  133. ret = IOT_Shadow_Register_Property(sg_pshadow, Property3, RegCallback_update);
  134. if(SUCCESS_RET != ret)
  135. {
  136. HAL_Printf("Register Property3 fail:%d\n", ret);
  137. return;
  138. }
  139. DeviceProperty *Property4 = (DeviceProperty *)HAL_Malloc(sizeof(DeviceProperty));
  140. char num4[5] = "num4";
  141. char str4[6] = "data4";
  142. Property4->key= str4;
  143. Property4->data = num4;
  144. Property4->type = JSTRING;
  145. ret = IOT_Shadow_Register_Property(sg_pshadow, Property4, RegCallback_update);
  146. if(SUCCESS_RET != ret)
  147. {
  148. HAL_Printf("Register Property4 fail:%d\n", ret);
  149. return;
  150. }
  151. DeviceProperty *Property5 = (DeviceProperty *)HAL_Malloc(sizeof(DeviceProperty));
  152. bool num5 = false;
  153. char str5[6] = "data5";
  154. Property5->key= str5;
  155. Property5->data = &num5;
  156. Property5->type = JBOOL;
  157. ret = IOT_Shadow_Register_Property(sg_pshadow, Property5, RegCallback_update);
  158. if(SUCCESS_RET != ret)
  159. {
  160. HAL_Printf("Register Property5 fail:%d\n", ret);
  161. return;
  162. }
  163. DeviceProperty *Property6 = (DeviceProperty *)HAL_Malloc(sizeof(DeviceProperty));
  164. char num6[20] = "{\"temp\":25}";
  165. char str6[6] = "data6";
  166. Property6->key= str6;
  167. Property6->data = num6;
  168. Property6->type = JOBJECT;
  169. ret = IOT_Shadow_Register_Property(sg_pshadow, Property6, RegCallback_update);
  170. if(SUCCESS_RET != ret)
  171. {
  172. HAL_Printf("Register Property6 fail:%d\n", ret);
  173. return;
  174. }
  175. /* 先同步一下版本号和设备掉电期间更新的属性 */
  176. ret = IOT_Shadow_Get_Sync(sg_pshadow, _update_ack_cb, time_sec, &ack_update);
  177. if(SUCCESS_RET != ret)
  178. {
  179. HAL_Printf("Get Sync fail:%d\n", ret);
  180. return;
  181. }
  182. while (ACK_NONE == ack_update) {
  183. IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS);
  184. }
  185. /* update */
  186. ack_update = ACK_NONE;
  187. ret = IOT_Shadow_Update(sg_pshadow, _update_ack_cb, time_sec, &ack_update, 6, Property1, Property2, Property3, Property4, Property5, Property6);
  188. if(SUCCESS_RET != ret)
  189. {
  190. HAL_Printf("Update Property1 Property2 Property3 Property4 Property5 Property6 fail:%d\n", ret);
  191. return;
  192. }
  193. while (ACK_NONE == ack_update) {
  194. IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS);
  195. }
  196. ack_update = ACK_NONE;
  197. ret = IOT_Shadow_Get_Sync(sg_pshadow, _update_ack_cb, time_sec, &ack_update);
  198. while (ACK_NONE == ack_update) {
  199. IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS);
  200. }
  201. #if 0
  202. /* update */
  203. num1 = 123;
  204. Property1->data = &num1;
  205. char num9[5] = "num9";
  206. Property4->data = num9;
  207. ack_update = ACK_NONE;
  208. ret = IOT_Shadow_Update(sg_pshadow, _update_ack_cb, time_sec, &ack_update, 2, Property1, Property4);
  209. if(SUCCESS_RET != ret)
  210. {
  211. HAL_Printf("Update Property1 Property4 fail:%d\n", ret);
  212. return ret;
  213. }
  214. while (ACK_NONE == ack_update) {
  215. IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS);
  216. }
  217. /* delete */
  218. ack_update = ACK_NONE;
  219. ret = IOT_Shadow_Delete(sg_pshadow, _update_ack_cb, time_sec, &ack_update, 2, Property1, Property2);
  220. if(SUCCESS_RET != ret)
  221. {
  222. HAL_Printf("Delete Property1 Property2 fail:%d\n", ret);
  223. return ret;
  224. }
  225. while (ACK_NONE == ack_update) {
  226. IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS);
  227. }
  228. ack_update = ACK_NONE;
  229. ret = IOT_Shadow_Get_Sync(sg_pshadow, _update_ack_cb, time_sec, &ack_update);
  230. while (ACK_NONE == ack_update) {
  231. IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS);
  232. }
  233. /* delete all */
  234. ack_update = ACK_NONE;
  235. ret = IOT_Shadow_Delete_All(sg_pshadow, _update_ack_cb, time_sec, &ack_update);
  236. if(SUCCESS_RET != ret)
  237. {
  238. HAL_Printf("Delete All fail:%d\n", ret);
  239. return ret;
  240. }
  241. while (ACK_NONE == ack_update) {
  242. IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS);
  243. }
  244. ack_update = ACK_NONE;
  245. ret = IOT_Shadow_Get_Sync(sg_pshadow, _update_ack_cb, time_sec, &ack_update);
  246. while (ACK_NONE == ack_update) {
  247. IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS);
  248. }
  249. Property1->data = &num1;
  250. Property4->data = num4;
  251. Property5->data = &num5;
  252. Property6->data = num6;
  253. /* update */
  254. ack_update = ACK_NONE;
  255. ret = IOT_Shadow_Update_And_Reset_Version(sg_pshadow, _update_ack_cb, time_sec, &ack_update, 4, Property1, Property4, Property5, Property6);
  256. if(SUCCESS_RET != ret)
  257. {
  258. HAL_Printf("Update and Reset Ver fail:%d\n", ret);
  259. return ret;
  260. }
  261. while (ACK_NONE == ack_update) {
  262. IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS);
  263. }
  264. ack_update = ACK_NONE;
  265. ret = IOT_Shadow_Get_Sync(sg_pshadow, _update_ack_cb, time_sec, &ack_update);
  266. while (ACK_NONE == ack_update) {
  267. IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS);
  268. }
  269. #endif
  270. HAL_Free(Property1);
  271. HAL_Free(Property2);
  272. HAL_Free(Property3);
  273. HAL_Free(Property4);
  274. HAL_Free(Property5);
  275. HAL_Free(Property6);
  276. IOT_Shadow_Destroy(sg_pshadow);
  277. return;
  278. }
  279. static int shadow_test_example(int argc, char **argv)
  280. {
  281. rt_thread_t tid;
  282. int stack_size = 8192;
  283. if (2 == argc)
  284. {
  285. if (!strcmp("start", argv[1]))
  286. {
  287. if (1 == running_state)
  288. {
  289. HAL_Printf("shadow_test_example is already running\n");
  290. return 0;
  291. }
  292. }
  293. else if (!strcmp("stop", argv[1]))
  294. {
  295. if (0 == running_state)
  296. {
  297. HAL_Printf("shadow_test_example is already stopped\n");
  298. return 0;
  299. }
  300. running_state = 0;
  301. return 0;
  302. }
  303. else
  304. {
  305. HAL_Printf("Usage: shadow_test_example start/stop");
  306. return 0;
  307. }
  308. }
  309. else
  310. {
  311. HAL_Printf("Para err, usage: shadow_test_example start/stop");
  312. return 0;
  313. }
  314. tid = rt_thread_create("shadow_test", (void (*)(void *))shadow_test_thread,
  315. NULL, stack_size, RT_THREAD_PRIORITY_MAX / 2 - 1, 100);
  316. if (tid != RT_NULL)
  317. {
  318. rt_thread_startup(tid);
  319. }
  320. return 0;
  321. }
  322. #ifdef RT_USING_FINSH
  323. #include <finsh.h>
  324. FINSH_FUNCTION_EXPORT(shadow_test_example, startup mqtt shadow example);
  325. #endif
  326. #ifdef FINSH_USING_MSH
  327. MSH_CMD_EXPORT(shadow_test_example, startup mqtt shadow example);
  328. #endif