shadow-example.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) 2014-2016 Alibaba Group. All rights reserved.
  3. * License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. * not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <stdarg.h>
  22. #include "iot_import.h"
  23. #include "iot_export.h"
  24. #define PRODUCT_KEY "yfTuLfBJTiL"
  25. #define DEVICE_NAME "TestDeviceForDemo"
  26. #define DEVICE_SECRET "fSCl9Ns5YPnYN8Ocg0VEel1kXFnRlV6c"
  27. #define SHADOW_MQTT_MSGLEN (1024)
  28. char g_product_key[PRODUCT_KEY_LEN + 1];
  29. char g_product_secret[PRODUCT_SECRET_LEN + 1];
  30. char g_device_name[DEVICE_NAME_LEN + 1];
  31. char g_device_secret[DEVICE_SECRET_LEN + 1];
  32. #define SHADOW_TRACE(fmt, ...) \
  33. do { \
  34. HAL_Printf("%s|%03d :: ", __func__, __LINE__); \
  35. HAL_Printf(fmt, ##__VA_ARGS__); \
  36. HAL_Printf("%s", "\r\n"); \
  37. } while(0)
  38. /**
  39. * @brief This is a callback function when a control value coming from server.
  40. *
  41. * @param [in] pattr: attribute structure pointer
  42. * @return none
  43. * @see none.
  44. * @note none.
  45. */
  46. static void _device_shadow_cb_light(iotx_shadow_attr_pt pattr)
  47. {
  48. /*
  49. * ****** Your Code ******
  50. */
  51. SHADOW_TRACE("----");
  52. SHADOW_TRACE("Attrbute Name: '%s'", pattr->pattr_name);
  53. SHADOW_TRACE("Attrbute Value: %d", *(int32_t *)pattr->pattr_data);
  54. SHADOW_TRACE("----");
  55. }
  56. /* Device shadow demo entry */
  57. int demo_device_shadow(char *msg_buf, char *msg_readbuf)
  58. {
  59. char buf[1024];
  60. iotx_err_t rc;
  61. iotx_conn_info_pt puser_info;
  62. void *h_shadow;
  63. iotx_shadow_para_t shadow_para;
  64. /**< get device info*/
  65. HAL_GetProductKey(g_product_key);
  66. HAL_GetDeviceName(g_device_name);
  67. HAL_GetDeviceSecret(g_device_secret);
  68. /**< end*/
  69. /* Device AUTH */
  70. // rc = IOT_SetupConnInfo(PRODUCT_KEY, DEVICE_NAME, DEVICE_SECRET, (void **)&puser_info);
  71. rc = IOT_SetupConnInfo(g_product_key, g_device_name, g_device_secret, (void **)&puser_info);
  72. if (SUCCESS_RETURN != rc) {
  73. SHADOW_TRACE("rc = IOT_SetupConnInfo() = %d", rc);
  74. return rc;
  75. }
  76. /* Construct a device shadow */
  77. memset(&shadow_para, 0, sizeof(iotx_shadow_para_t));
  78. shadow_para.mqtt.port = puser_info->port;
  79. shadow_para.mqtt.host = puser_info->host_name;
  80. shadow_para.mqtt.client_id = puser_info->client_id;
  81. shadow_para.mqtt.username = puser_info->username;
  82. shadow_para.mqtt.password = puser_info->password;
  83. shadow_para.mqtt.pub_key = puser_info->pub_key;
  84. shadow_para.mqtt.request_timeout_ms = 2000;
  85. shadow_para.mqtt.clean_session = 0;
  86. shadow_para.mqtt.keepalive_interval_ms = 60000;
  87. shadow_para.mqtt.pread_buf = msg_readbuf;
  88. shadow_para.mqtt.read_buf_size = SHADOW_MQTT_MSGLEN;
  89. shadow_para.mqtt.pwrite_buf = msg_buf;
  90. shadow_para.mqtt.write_buf_size = SHADOW_MQTT_MSGLEN;
  91. shadow_para.mqtt.handle_event.h_fp = NULL;
  92. shadow_para.mqtt.handle_event.pcontext = NULL;
  93. h_shadow = IOT_Shadow_Construct(&shadow_para);
  94. if (NULL == h_shadow) {
  95. SHADOW_TRACE("construct device shadow failed!");
  96. return rc;
  97. }
  98. /* Define and add two attribute */
  99. int32_t light = 1000, temperature = 1001;
  100. iotx_shadow_attr_t attr_light, attr_temperature;
  101. memset(&attr_light, 0, sizeof(iotx_shadow_attr_t));
  102. memset(&attr_temperature, 0, sizeof(iotx_shadow_attr_t));
  103. /* Initialize the @light attribute */
  104. attr_light.attr_type = IOTX_SHADOW_INT32;
  105. attr_light.mode = IOTX_SHADOW_RW;
  106. attr_light.pattr_name = "switch";
  107. attr_light.pattr_data = &light;
  108. attr_light.callback = _device_shadow_cb_light;
  109. /* Initialize the @temperature attribute */
  110. attr_temperature.attr_type = IOTX_SHADOW_INT32;
  111. attr_temperature.mode = IOTX_SHADOW_READONLY;
  112. attr_temperature.pattr_name = "temperature";
  113. attr_temperature.pattr_data = &temperature;
  114. attr_temperature.callback = NULL;
  115. /* Register the attribute */
  116. /* Note that you must register the attribute you want to synchronize with cloud
  117. * before calling IOT_Shadow_Pull() */
  118. IOT_Shadow_RegisterAttribute(h_shadow, &attr_light);
  119. IOT_Shadow_RegisterAttribute(h_shadow, &attr_temperature);
  120. /* synchronize the device shadow with device shadow cloud */
  121. IOT_Shadow_Pull(h_shadow);
  122. do {
  123. format_data_t format;
  124. /* Format the attribute data */
  125. IOT_Shadow_PushFormat_Init(h_shadow, &format, buf, 1024);
  126. IOT_Shadow_PushFormat_Add(h_shadow, &format, &attr_temperature);
  127. IOT_Shadow_PushFormat_Add(h_shadow, &format, &attr_light);
  128. IOT_Shadow_PushFormat_Finalize(h_shadow, &format);
  129. /* Update attribute data */
  130. IOT_Shadow_Push(h_shadow, format.buf, format.offset, 10);
  131. /* Sleep 1000 ms */
  132. HAL_SleepMs(1000);
  133. } while (0);
  134. /* Delete the two attributes */
  135. IOT_Shadow_DeleteAttribute(h_shadow, &attr_temperature);
  136. IOT_Shadow_DeleteAttribute(h_shadow, &attr_light);
  137. IOT_Shadow_Destroy(h_shadow);
  138. return 0;
  139. }
  140. int main()
  141. {
  142. IOT_OpenLog("shadow");
  143. IOT_SetLogLevel(IOT_LOG_DEBUG);
  144. /**< set device info*/
  145. HAL_SetProductKey(PRODUCT_KEY);
  146. HAL_SetDeviceName(DEVICE_NAME);
  147. HAL_SetDeviceSecret(DEVICE_SECRET);
  148. /**< end*/
  149. char *msg_buf = (char *)HAL_Malloc(SHADOW_MQTT_MSGLEN);
  150. char *msg_readbuf = (char *)HAL_Malloc(SHADOW_MQTT_MSGLEN);
  151. demo_device_shadow(msg_buf, msg_readbuf);
  152. HAL_Free(msg_buf);
  153. HAL_Free(msg_readbuf);
  154. SHADOW_TRACE("out of demo!");
  155. IOT_DumpMemoryStats(IOT_LOG_DEBUG);
  156. IOT_CloseLog();
  157. return 0;
  158. }