All Products
Search
Document Center

Device Shadow

Last Updated: May 13, 2020

Background

A device shadow is a persistent virtual shadow of a device built on the IoT Platform based on a special topic for the related device. This device synchronizes its status to the cloud using this device shadow. The cloud can quickly obtain the device status from the device shadow even when the device is not connected to the IoT Platform.

Note: For products that support device modelling, reported information is kept in the cloud, so the device shadow is only applicable to the Basic Edition of the Alibaba Cloud IoT Platform.

Procedure

  • The C SDK provides the IOT_Shadow_Construct function to create the device shadow. The function is declared as follows:
  1. /**
  2. * @brief Construct the Device Shadow.
  3. * This function initializes the data structures, and establishes the MQTT connection.
  4. * and subscribe the topic: "/shadow/get/${YourProduct_key}/${YourDevice_name}".
  5. *
  6. * @param [in] pparam: The specific initial parameter.
  7. * @retval NULL : Construct shadow failed.
  8. * @retval NOT_NULL : Construct success.
  9. * @see None.
  10. */
  11. void *IOT_Shadow_Construct(iotx_shadow_para_pt pparam);
  • Use the IOT_Shadow_RegisterAttribute function to register the properties of the device shadow. The function is declared as follows:
  1. /**
  2. * @brief Create a data type registered to the server.
  3. *
  4. * @param [in] handle: The handle of device shadow.
  5. * @param [in] pattr: The parameter registered to the server.
  6. * @retval SUCCESS_RETURN : Success.
  7. * @retval other : See iotx_err_t.
  8. * @see None.
  9. */
  10. iotx_err_t IOT_Shadow_RegisterAttribute(void *handle, iotx_shadow_attr_pt pattr);
  • You can use the IOT_Shadow_Pull interface in the C-SDK to synchronize device status from the cloud whenever the device shadow starts. The function is declared as follows:
  1. /**
  2. * @brief Synchronize device shadow data from cloud.
  3. * It is a synchronous interface.
  4. * @param [in] handle: The handle of device shadow.
  5. * @retval SUCCESS_RETURN : Success.
  6. * @retval other : See iotx_err_t.
  7. * @see None.
  8. */
  9. iotx_err_t IOT_Shadow_Pull(void *handle);
  • When the device updates its status, you can use IOT_Shadow_PushFormat_Init, IOT_Shadow_PushFormat_Add, and IOT_Shadow_PushFormat_Finalize in the C SDK to update the device status, and use IOT_Shadow_Push in the C SDK to synchronize the status to the cloud. The function is declared as follows:
  1. /**
  2. * @brief Start to process the structure of the data type format.
  3. *
  4. * @param [in] handle: The handle of device shadow.
  5. * @param [out] pformat: The format struct of device shadow.
  6. * @param [in] buf: The buf which store device shadow.
  7. * @param [in] size: Maximum length of device shadow attribute.
  8. * @retval SUCCESS_RETURN : Success.
  9. * @retval other : See iotx_err_t.
  10. * @see None.
  11. */
  12. iotx_err_t IOT_Shadow_PushFormat_Init(
  13. void *handle,
  14. format_data_pt pformat,
  15. char *buf,
  16. uint16_t size);
  17. /**
  18. * @brief Format the attribute name and value for update.
  19. *
  20. * @param [in] handle: The handle of device shadow.
  21. * @param [in] pformat: The format struct of device shadow.
  22. * @param [in] pattr: To have created the data type of the format in the add member attributes.
  23. * @retval SUCCESS_RETURN : Success.
  24. * @retval other : See iotx_err_t.
  25. * @see None.
  26. */
  27. iotx_err_t IOT_Shadow_PushFormat_Add(
  28. void *handle,
  29. format_data_pt pformat,
  30. iotx_shadow_attr_pt pattr);
  31. /**
  32. * @brief Complete processing of the structure of the data type format.
  33. *
  34. * @param [in] handle: The handle of device shadow.
  35. * @param [in] pformat: The format struct of device shadow.
  36. * @retval SUCCESS_RETURN : Success.
  37. * @retval other : See iotx_err_t.
  38. * @see None.
  39. */
  40. iotx_err_t IOT_Shadow_PushFormat_Finalize(void *handle, format_data_pt pformat);
  • To disconnect the device from IoT Platform, use IOT_Shadow_DeleteAttribute and IOT_Shadow_Destroy in the C SDK to delete all properties that have been created for this device on IoT Platform, and release the device shadow. The function is declared as follows:
  1. /**
  2. * @brief Deconstruct the specific device shadow.
  3. *
  4. * @param [in] handle: The handle of device shadow.
  5. * @retval SUCCESS_RETURN : Success.
  6. * @retval other : See iotx_err_t.
  7. * @see None.
  8. */
  9. iotx_err_t IOT_Shadow_Destroy(void *handle);