AWS IoT Embedded C Device SDK
Data Structures | Typedefs | Enumerations | Functions
aws_iot_shadow_interface.h File Reference

Interface for thing shadow. More...

#include "aws_iot_mqtt_client_interface.h"
#include "aws_iot_shadow_json_data.h"

Go to the source code of this file.

Data Structures

struct  ShadowInitParameters_t
 Shadow Initialization parameters. More...
 
struct  ShadowConnectParameters_t
 Shadow Connect parameters. More...
 

Typedefs

typedef void(* fpActionCallback_t) (const char *pThingName, ShadowActions_t action, Shadow_Ack_Status_t status, const char *pReceivedJsonDocument, void *pContextData)
 Function Pointer typedef used as the callback for every action. More...
 

Enumerations

enum  Shadow_Ack_Status_t { SHADOW_ACK_TIMEOUT, SHADOW_ACK_REJECTED, SHADOW_ACK_ACCEPTED }
 Thing Shadow Acknowledgment enum. More...
 
enum  ShadowActions_t { SHADOW_GET, SHADOW_UPDATE, SHADOW_DELETE }
 Thing Shadow Action type enum. More...
 

Functions

IoT_Error_t aws_iot_shadow_init (AWS_IoT_Client *pClient, ShadowInitParameters_t *pParams)
 Initialize the Thing Shadow before use. More...
 
IoT_Error_t aws_iot_shadow_connect (AWS_IoT_Client *pClient, ShadowConnectParameters_t *pParams)
 Connect to the AWS IoT Thing Shadow service over MQTT. More...
 
IoT_Error_t aws_iot_shadow_yield (AWS_IoT_Client *pClient, uint32_t timeout)
 Yield function to let the background tasks of MQTT and Shadow. More...
 
IoT_Error_t aws_iot_shadow_disconnect (AWS_IoT_Client *pClient)
 Disconnect from the AWS IoT Thing Shadow service over MQTT. More...
 
IoT_Error_t aws_iot_shadow_update (AWS_IoT_Client *pClient, const char *pThingName, char *pJsonString, fpActionCallback_t callback, void *pContextData, uint8_t timeout_seconds, bool isPersistentSubscribe)
 This function is the one used to perform an Update action to a Thing Name's Shadow. More...
 
IoT_Error_t aws_iot_shadow_get (AWS_IoT_Client *pClient, const char *pThingName, fpActionCallback_t callback, void *pContextData, uint8_t timeout_seconds, bool isPersistentSubscribe)
 This function is the one used to perform an Get action to a Thing Name's Shadow. More...
 
IoT_Error_t aws_iot_shadow_delete (AWS_IoT_Client *pClient, const char *pThingName, fpActionCallback_t callback, void *pContextData, uint8_t timeout_seconds, bool isPersistentSubscriptions)
 This function is the one used to perform an Delete action to a Thing Name's Shadow. More...
 
IoT_Error_t aws_iot_shadow_register_delta (AWS_IoT_Client *pClient, jsonStruct_t *pStruct)
 This function is used to listen on the delta topic of #AWS_IOT_MY_THING_NAME mentioned in the aws_iot_config.h file. More...
 
void aws_iot_shadow_reset_last_received_version (void)
 Reset the last received version number to zero. This will be useful if the Thing Shadow is deleted and would like to to reset the local version. More...
 
uint32_t aws_iot_shadow_get_last_received_version (void)
 Version of a document is received with every accepted/rejected and the SDK keeps track of the last received version of the JSON document of #AWS_IOT_MY_THING_NAME shadow. More...
 
void aws_iot_shadow_enable_discard_old_delta_msgs (void)
 Enable the ignoring of delta messages with old version number. More...
 
void aws_iot_shadow_disable_discard_old_delta_msgs (void)
 Disable the ignoring of delta messages with old version number.
 
IoT_Error_t aws_iot_shadow_set_autoreconnect_status (AWS_IoT_Client *pClient, bool newStatus)
 This function is used to enable or disable autoreconnect. More...
 

Detailed Description

These are the functions and structs to manage/interact the Thing Shadow(in the cloud). This SDK will let you interact with your own thing shadow or any other shadow using its Thing Name. There are totally 3 actions a device can perform on the shadow - Get, Update and Delete.

Currently the device should use MQTT/S underneath. In the future this will also support other protocols. As it supports MQTT, the shadow needs to connect and disconnect. It will also work on the pub/sub model. On performing any action, the acknowledgment will be received in either accepted or rejected. For Example: If we want to perform a GET on the thing shadow the following messages will be sent and received:

  1. A MQTT Publish on the topic - $aws/things/{thingName}/shadow/get
  2. Subscribe to MQTT topics - $aws/things/{thingName}/shadow/get/accepted and $aws/things/{thingName}/shadow/get/rejected. If the request was successful we will receive the things json document in the accepted topic.

Typedef Documentation

typedef void(* fpActionCallback_t) (const char *pThingName, ShadowActions_t action, Shadow_Ack_Status_t status, const char *pReceivedJsonDocument, void *pContextData)

This function will be called from the context of aws_iot_shadow_yield() context

Parameters
pThingNameThing Name of the response received
actionThe response of the action
statusInforms if the action was Accepted/Rejected or Timed out
pReceivedJsonDocumentReceived JSON document
pContextDatathe void* data passed in during the action call(update, get or delete)

Enumeration Type Documentation

This enum type is use in the callback for the action response

This enum type is use in the callback for the action response

Function Documentation

IoT_Error_t aws_iot_shadow_connect ( AWS_IoT_Client pClient,
ShadowConnectParameters_t pParams 
)

This function does the TLSv1.2 handshake and establishes the MQTT connection

Parameters
pClientMQTT Client used as the protocol layer
pParamsShadow Conenction parameters like TLS cert location
Returns
An IoT Error Type defining successful/failed Connection
IoT_Error_t aws_iot_shadow_delete ( AWS_IoT_Client pClient,
const char *  pThingName,
fpActionCallback_t  callback,
void *  pContextData,
uint8_t  timeout_seconds,
bool  isPersistentSubscriptions 
)

This is not a very common use case for device. It is generally the responsibility of the accompanying app to do the delete. It is similar to the Update function internally except it does not take a JSON document as the input. The Thing Shadow referred by the ThingName will be deleted.

Parameters
pClientMQTT Client used as the protocol layer
pThingNameThing Name of the Shadow that should be deleted
callbackThis is the callback that will be used to inform the caller of the response from the AWS IoT Shadow service.Callback could be set to NULL if response is not important
pContextDataThis is an extra parameter that could be passed along with the callback. It should be set to NULL if not used
timeout_secondsIt is the time the SDK will wait for the response on either accepted/rejected before declaring timeout on the action
isPersistentSubscribeAs mentioned above, every time if a device deletes the same Shadow (JSON document) then this should be set to true to avoid repeated subscription and un-subscription. If the Thing Name is one off delete then this should be set to false
Returns
An IoT Error Type defining successful/failed delete action
IoT_Error_t aws_iot_shadow_disconnect ( AWS_IoT_Client pClient)

This will close the underlying TCP connection, MQTT connection will also be closed

Parameters
pClientMQTT Client used as the protocol layer
Returns
An IoT Error Type defining successful/failed disconnect status
void aws_iot_shadow_enable_discard_old_delta_msgs ( void  )

As we use MQTT underneath, there could be more than 1 of the same message if we use QoS 0. To avoid getting called for the same message, this functionality should be enabled. All the old message will be ignored

IoT_Error_t aws_iot_shadow_get ( AWS_IoT_Client pClient,
const char *  pThingName,
fpActionCallback_t  callback,
void *  pContextData,
uint8_t  timeout_seconds,
bool  isPersistentSubscribe 
)

One use of this function is usually to get the config of a device at boot up. It is similar to the Update function internally except it does not take a JSON document as the input. The entire JSON document will be sent over the accepted topic

Parameters
pClientMQTT Client used as the protocol layer
pThingNameThing Name of the JSON document that is needed
callbackThis is the callback that will be used to inform the caller of the response from the AWS IoT Shadow service.Callback could be set to NULL if response is not important
pContextDataThis is an extra parameter that could be passed along with the callback. It should be set to NULL if not used
timeout_secondsIt is the time the SDK will wait for the response on either accepted/rejected before declaring timeout on the action
isPersistentSubscribeAs mentioned above, every time if a device gets the same Sahdow (JSON document) then this should be set to true to avoid repeated subscription and un-subscription. If the Thing Name is one off get then this should be set to false
Returns
An IoT Error Type defining successful/failed get action
uint32_t aws_iot_shadow_get_last_received_version ( void  )

One exception to this version tracking is that, the SDK will ignore the version from update/accepted topic. Rest of the responses will be scanned to update the version number. Accepting version change for update/accepted may cause version conflicts for delta message if the update message is received before the delta.

Returns
version number of the last received response
IoT_Error_t aws_iot_shadow_init ( AWS_IoT_Client pClient,
ShadowInitParameters_t pParams 
)

This function takes care of initializing the internal book-keeping data structures

Parameters
pClientMQTT Client used as the protocol layer
Returns
An IoT Error Type defining successful/failed Initialization
IoT_Error_t aws_iot_shadow_register_delta ( AWS_IoT_Client pClient,
jsonStruct_t pStruct 
)

Any time a delta is published the Json document will be delivered to the pStruct->cb. If you don't want the parsing done by the SDK then use the jsonStruct_t key set to "state". A good example of this is displayed in the sample_apps/shadow_console_echo.c

Parameters
pClientMQTT Client used as the protocol layer
pStructThe struct used to parse JSON value
Returns
An IoT Error Type defining successful/failed delta registering
void aws_iot_shadow_reset_last_received_version ( void  )
Returns
no return values
IoT_Error_t aws_iot_shadow_set_autoreconnect_status ( AWS_IoT_Client pClient,
bool  newStatus 
)

Any time a disconnect happens the underlying MQTT client attempts to reconnect if this is set to true

Parameters
pClientMQTT Client used as the protocol layer
newStatusThe new status to set the autoreconnect option to
Returns
An IoT Error Type defining successful/failed operation
IoT_Error_t aws_iot_shadow_update ( AWS_IoT_Client pClient,
const char *  pThingName,
char *  pJsonString,
fpActionCallback_t  callback,
void *  pContextData,
uint8_t  timeout_seconds,
bool  isPersistentSubscribe 
)

update is one of the most frequently used functionality by a device. In most cases the device may be just reporting few params to update the thing shadow in the cloud Update Action if no callback or if the JSON document does not have a client token then will just publish the update and not track it.

Note
The update has to subscribe to two topics update/accepted and update/rejected. This function waits 2 seconds to ensure the subscriptions are registered before publishing the update message. The following steps are performed on using this function:
  1. Subscribe to Shadow topics - $aws/things/{thingName}/shadow/update/accepted and $aws/things/{thingName}/shadow/update/rejected
  2. wait for 2 seconds for the subscription to take effect
  3. Publish on the update topic - $aws/things/{thingName}/shadow/update
  4. In the aws_iot_shadow_yield() function the response will be handled. In case of timeout or if the response is received, the subscription to shadow response topics are un-subscribed from. On the contrary if the persistent subscription is set to true then the un-subscribe will not be done. The topics will always be listened to.
Parameters
pClientMQTT Client used as the protocol layer
pThingNameThing Name of the shadow that needs to be Updated
pJsonStringThe update action expects a JSON document to send. The JSON String should be a null terminated string. This JSON document should adhere to the AWS IoT Thing Shadow specification. To help in the process of creating this document- SDK provides apis in aws_iot_shadow_json_data.h
callbackThis is the callback that will be used to inform the caller of the response from the AWS IoT Shadow service.Callback could be set to NULL if response is not important
pContextDataThis is an extra parameter that could be passed along with the callback. It should be set to NULL if not used
timeout_secondsIt is the time the SDK will wait for the response on either accepted/rejected before declaring timeout on the action
isPersistentSubscribeAs mentioned above, every time if a device updates the same shadow then this should be set to true to avoid repeated subscription and unsubscription. If the Thing Name is one off update then this should be set to false
Returns
An IoT Error Type defining successful/failed update action
IoT_Error_t aws_iot_shadow_yield ( AWS_IoT_Client pClient,
uint32_t  timeout 
)

This function could be use in a separate thread waiting for the incoming messages, ensuring the connection is kept alive with the AWS Service. It also ensures the expired requests of Shadow actions are cleared and Timeout callback is executed.

Note
All callbacks ever used in the SDK will be executed in the context of this function.
Parameters
pClientMQTT Client used as the protocol layer
timeoutin milliseconds, This is the maximum time the yield function will wait for a message and/or read the messages from the TLS buffer
Returns
An IoT Error Type defining successful/failed Yield