Microchip® Advanced Software Framework

aws_iot_shadow_interface.h File Reference

Interface for thing shadow.

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.

Data Structures

struct  ShadowParameters_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_connect (MQTTClient_t *pClient, ShadowParameters_t *pParams)
 Connect to the AWS IoT Thing Shadow service over MQTT. More...
 
IoT_Error_t aws_iot_shadow_delete (MQTTClient_t *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...
 
void aws_iot_shadow_disable_discard_old_delta_msgs (void)
 Disable the ignoring of delta messages with old version number. More...
 
IoT_Error_t aws_iot_shadow_disconnect (MQTTClient_t *pClient)
 Disconnect from the AWS IoT Thing Shadow service over MQTT. More...
 
void aws_iot_shadow_enable_discard_old_delta_msgs (void)
 Enable the ignoring of delta messages with old version number. More...
 
IoT_Error_t aws_iot_shadow_get (MQTTClient_t *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...
 
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...
 
IoT_Error_t aws_iot_shadow_init (MQTTClient_t *pClient)
 Initialize the Thing Shadow before use. More...
 
IoT_Error_t aws_iot_shadow_register_delta (MQTTClient_t *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. More...
 
IoT_Error_t aws_iot_shadow_update (MQTTClient_t *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_yield (MQTTClient_t *pClient, int timeout)
 Yield function to let the background tasks of MQTT and Shadow. More...
 

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.

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)

Thing Shadow Acknowledgment enum.

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

Enumerator
SHADOW_ACK_TIMEOUT 
SHADOW_ACK_REJECTED 
SHADOW_ACK_ACCEPTED 

Thing Shadow Action type enum.

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

Enumerator
SHADOW_GET 
SHADOW_UPDATE 
SHADOW_DELETE 

IoT_Error_t aws_iot_shadow_delete ( MQTTClient_t 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.

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 Sahdow (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

References CONNECTION_ERROR, iot_shadow_action(), iot_shadow_delete_request_json(), MQTTClient_t::isConnected, NONE_ERROR, and SHADOW_DELETE.

void aws_iot_shadow_disable_discard_old_delta_msgs ( void  )

Disable the ignoring of delta messages with old version number.

References shadowDiscardOldDeltaFlag.

IoT_Error_t aws_iot_shadow_disconnect ( MQTTClient_t pClient)

Disconnect from the AWS IoT Thing Shadow service over MQTT.

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

References MQTTClient_t::disconnect.

void aws_iot_shadow_enable_discard_old_delta_msgs ( void  )

Enable the ignoring of delta messages with old version number.

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

References shadowDiscardOldDeltaFlag.

IoT_Error_t aws_iot_shadow_get ( MQTTClient_t 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.

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

References CONNECTION_ERROR, iot_shadow_action(), iot_shadow_get_request_json(), MQTTClient_t::isConnected, NONE_ERROR, and SHADOW_GET.

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.

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

References shadowJsonVersionNum.

IoT_Error_t aws_iot_shadow_init ( MQTTClient_t pClient)

Initialize the Thing Shadow before use.

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

References aws_iot_shadow_reset_last_received_version(), initDeltaTokens(), NONE_ERROR, NULL, NULL_VALUE_ERROR, and resetClientTokenSequenceNum().

IoT_Error_t aws_iot_shadow_register_delta ( MQTTClient_t 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.

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

References CONNECTION_ERROR, MQTTClient_t::isConnected, NONE_ERROR, and registerJsonTokenOnDelta().

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

Returns
no return values

References shadowJsonVersionNum.

Referenced by aws_iot_shadow_init().

IoT_Error_t aws_iot_shadow_update ( MQTTClient_t 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.

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 JSO 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

References CONNECTION_ERROR, iot_shadow_action(), MQTTClient_t::isConnected, NONE_ERROR, and SHADOW_UPDATE.

IoT_Error_t aws_iot_shadow_yield ( MQTTClient_t pClient,
int  timeout 
)

Yield function to let the background tasks of MQTT and Shadow.

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

References HandleExpiredResponseCallbacks(), and MQTTClient_t::yield.