AWS IoT Device SDK C: Shadow
AWS IoT Device Shadow library
Return to main page ↑
aws_iot_demo_shadow.c File Reference

Demonstrates usage of the Thing Shadow library. More...

#include "iot_config.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "iot_demo_logging.h"
#include "platform/iot_clock.h"
#include "platform/iot_threads.h"
#include "iot_mqtt.h"
#include "aws_iot_shadow.h"
#include "aws_iot_doc_parser.h"

Macros

#define KEEP_ALIVE_SECONDS   ( 60 )
 The keep-alive interval used for this demo. More...
 
#define TIMEOUT_MS   ( 5000 )
 The timeout for Shadow and MQTT operations in this demo.
 
#define SHADOW_DESIRED_JSON
 Format string representing a Shadow document with a "desired" state. More...
 
#define EXPECTED_DESIRED_JSON_SIZE   ( sizeof( SHADOW_DESIRED_JSON ) - 3 )
 The expected size of SHADOW_DESIRED_JSON. More...
 
#define SHADOW_REPORTED_JSON
 Format string representing a Shadow document with a "reported" state. More...
 
#define EXPECTED_REPORTED_JSON_SIZE   ( sizeof( SHADOW_REPORTED_JSON ) - 3 )
 The expected size of SHADOW_REPORTED_JSON. More...
 

Functions

int RunShadowDemo (bool awsIotMqttMode, const char *pIdentifier, void *pNetworkServerInfo, void *pNetworkCredentialInfo, const IotNetworkInterface_t *pNetworkInterface)
 The function that runs the Shadow demo, called by the demo runner. More...
 
static bool _getDelta (const char *pDeltaDocument, size_t deltaDocumentLength, const char *pDeltaKey, const char **pDelta, size_t *pDeltaLength)
 Parses a key in the "state" section of a Shadow delta document. More...
 
static bool _getUpdatedState (const char *pUpdatedDocument, size_t updatedDocumentLength, const char *pSectionKey, const char **pState, size_t *pStateLength)
 Parses the "state" key from the "previous" or "current" sections of a Shadow updated document. More...
 
static void _shadowDeltaCallback (void *pCallbackContext, AwsIotShadowCallbackParam_t *pCallbackParam)
 Shadow delta callback, invoked when the desired and updates Shadow states differ. More...
 
static void _shadowUpdatedCallback (void *pCallbackContext, AwsIotShadowCallbackParam_t *pCallbackParam)
 Shadow updated callback, invoked when the Shadow document changes. More...
 
static int _initializeDemo (void)
 Initialize the the MQTT library and the Shadow library. More...
 
static void _cleanupDemo (void)
 Clean up the the MQTT library and the Shadow library.
 
static int _establishMqttConnection (const char *pIdentifier, void *pNetworkServerInfo, void *pNetworkCredentialInfo, const IotNetworkInterface_t *pNetworkInterface, IotMqttConnection_t *pMqttConnection)
 Establish a new connection to the MQTT server for the Shadow demo. More...
 
static int _setShadowCallbacks (IotSemaphore_t *pDeltaSemaphore, IotMqttConnection_t mqttConnection, const char *pThingName, size_t thingNameLength)
 Set the Shadow callback functions used in this demo. More...
 
static void _clearShadowDocument (IotMqttConnection_t mqttConnection, const char *const pThingName, size_t thingNameLength)
 Try to delete any Shadow document in the cloud. More...
 
static int _sendShadowUpdates (IotSemaphore_t *pDeltaSemaphore, IotMqttConnection_t mqttConnection, const char *const pThingName, size_t thingNameLength)
 Send the Shadow updates that will trigger the Shadow callbacks. More...
 

Detailed Description

Demonstrates usage of the Thing Shadow library.

This program demonstrates the using Shadow documents to toggle a state called "powerOn" in a remote device.

Macro Definition Documentation

◆ KEEP_ALIVE_SECONDS

#define KEEP_ALIVE_SECONDS   ( 60 )

The keep-alive interval used for this demo.

An MQTT ping request will be sent periodically at this interval.

◆ SHADOW_DESIRED_JSON

#define SHADOW_DESIRED_JSON
Value:
"{" \
"\"state\":{" \
"\"desired\":{" \
"\"powerOn\":%01d" \
"}" \
"}," \
"\"clientToken\":\"%06lu\"" \
"}"

Format string representing a Shadow document with a "desired" state.

Note the client token, which is required for all Shadow updates. The client token must be unique at any given time, but may be reused once the update is completed. For this demo, a timestamp is used for a client token.

◆ EXPECTED_DESIRED_JSON_SIZE

#define EXPECTED_DESIRED_JSON_SIZE   ( sizeof( SHADOW_DESIRED_JSON ) - 3 )

The expected size of SHADOW_DESIRED_JSON.

Because all the format specifiers in SHADOW_DESIRED_JSON include a length, its full size is known at compile-time.

◆ SHADOW_REPORTED_JSON

#define SHADOW_REPORTED_JSON
Value:
"{" \
"\"state\":{" \
"\"reported\":{" \
"\"powerOn\":%01d" \
"}" \
"}," \
"\"clientToken\":\"%06lu\"" \
"}"

Format string representing a Shadow document with a "reported" state.

Note the client token, which is required for all Shadow updates. The client token must be unique at any given time, but may be reused once the update is completed. For this demo, a timestamp is used for a client token.

◆ EXPECTED_REPORTED_JSON_SIZE

#define EXPECTED_REPORTED_JSON_SIZE   ( sizeof( SHADOW_REPORTED_JSON ) - 3 )

The expected size of SHADOW_REPORTED_JSON.

Because all the format specifiers in SHADOW_REPORTED_JSON include a length, its full size is known at compile-time.

Function Documentation

◆ RunShadowDemo()

int RunShadowDemo ( bool  awsIotMqttMode,
const char *  pIdentifier,
void *  pNetworkServerInfo,
void *  pNetworkCredentialInfo,
const IotNetworkInterface_t pNetworkInterface 
)

The function that runs the Shadow demo, called by the demo runner.

Parameters
[in]awsIotMqttModeIgnored for the Shadow demo.
[in]pIdentifierNULL-terminated Shadow Thing Name.
[in]pNetworkServerInfoPassed to the MQTT connect function when establishing the MQTT connection for Shadows.
[in]pNetworkCredentialInfoPassed to the MQTT connect function when establishing the MQTT connection for Shadows.
[in]pNetworkInterfaceThe network interface to use for this demo.
Returns
EXIT_SUCCESS if the demo completes successfully; EXIT_FAILURE otherwise.

◆ _getDelta()

static bool _getDelta ( const char *  pDeltaDocument,
size_t  deltaDocumentLength,
const char *  pDeltaKey,
const char **  pDelta,
size_t *  pDeltaLength 
)
static

Parses a key in the "state" section of a Shadow delta document.

Parameters
[in]pDeltaDocumentThe Shadow delta document to parse.
[in]deltaDocumentLengthThe length of pDeltaDocument.
[in]pDeltaKeyThe key in the delta document to find. Must be NULL-terminated.
[out]pDeltaSet to the first character in the delta key.
[out]pDeltaLengthThe length of the delta key.
Returns
true if the given delta key is found; false otherwise.

◆ _getUpdatedState()

static bool _getUpdatedState ( const char *  pUpdatedDocument,
size_t  updatedDocumentLength,
const char *  pSectionKey,
const char **  pState,
size_t *  pStateLength 
)
static

Parses the "state" key from the "previous" or "current" sections of a Shadow updated document.

Parameters
[in]pUpdatedDocumentThe Shadow updated document to parse.
[in]updatedDocumentLengthThe length of pUpdatedDocument.
[in]pSectionKeyEither "previous" or "current". Must be NULL-terminated.
[out]pStateSet to the first character in "state".
[out]pStateLengthLength of the "state" section.
Returns
true if the "state" was found; false otherwise.

◆ _shadowDeltaCallback()

static void _shadowDeltaCallback ( void *  pCallbackContext,
AwsIotShadowCallbackParam_t pCallbackParam 
)
static

Shadow delta callback, invoked when the desired and updates Shadow states differ.

This function simulates a device updating its state in response to a Shadow.

Parameters
[in]pCallbackContextNot used.
[in]pCallbackParamThe received Shadow delta document.

◆ _shadowUpdatedCallback()

static void _shadowUpdatedCallback ( void *  pCallbackContext,
AwsIotShadowCallbackParam_t pCallbackParam 
)
static

Shadow updated callback, invoked when the Shadow document changes.

This function reports when a Shadow has been updated.

Parameters
[in]pCallbackContextNot used.
[in]pCallbackParamThe received Shadow updated document.

◆ _initializeDemo()

static int _initializeDemo ( void  )
static

Initialize the the MQTT library and the Shadow library.

Returns
EXIT_SUCCESS if all libraries were successfully initialized; EXIT_FAILURE otherwise.

◆ _establishMqttConnection()

static int _establishMqttConnection ( const char *  pIdentifier,
void *  pNetworkServerInfo,
void *  pNetworkCredentialInfo,
const IotNetworkInterface_t pNetworkInterface,
IotMqttConnection_t pMqttConnection 
)
static

Establish a new connection to the MQTT server for the Shadow demo.

Parameters
[in]pIdentifierNULL-terminated MQTT client identifier. The Shadow demo will use the Thing Name as the client identifier.
[in]pNetworkServerInfoPassed to the MQTT connect function when establishing the MQTT connection.
[in]pNetworkCredentialInfoPassed to the MQTT connect function when establishing the MQTT connection.
[in]pNetworkInterfaceThe network interface to use for this demo.
[out]pMqttConnectionSet to the handle to the new MQTT connection.
Returns
EXIT_SUCCESS if the connection is successfully established; EXIT_FAILURE otherwise.

◆ _setShadowCallbacks()

static int _setShadowCallbacks ( IotSemaphore_t pDeltaSemaphore,
IotMqttConnection_t  mqttConnection,
const char *  pThingName,
size_t  thingNameLength 
)
static

Set the Shadow callback functions used in this demo.

Parameters
[in]pDeltaSemaphoreUsed to synchronize Shadow updates with the delta callback.
[in]mqttConnectionThe MQTT connection used for Shadows.
[in]pThingNameThe Thing Name for Shadows in this demo.
[in]thingNameLengthThe length of pThingName.
Returns
EXIT_SUCCESS if all Shadow callbacks were set; EXIT_FAILURE otherwise.

◆ _clearShadowDocument()

static void _clearShadowDocument ( IotMqttConnection_t  mqttConnection,
const char *const  pThingName,
size_t  thingNameLength 
)
static

Try to delete any Shadow document in the cloud.

Parameters
[in]mqttConnectionThe MQTT connection used for Shadows.
[in]pThingNameThe Shadow Thing Name to delete.
[in]thingNameLengthThe length of pThingName.

◆ _sendShadowUpdates()

static int _sendShadowUpdates ( IotSemaphore_t pDeltaSemaphore,
IotMqttConnection_t  mqttConnection,
const char *const  pThingName,
size_t  thingNameLength 
)
static

Send the Shadow updates that will trigger the Shadow callbacks.

Parameters
[in]pDeltaSemaphoreUsed to synchronize Shadow updates with the delta callback.
[in]mqttConnectionThe MQTT connection used for Shadows.
[in]pThingNameThe Thing Name for Shadows in this demo.
[in]thingNameLengthThe length of pThingName.
Returns
EXIT_SUCCESS if all Shadow updates were sent; EXIT_FAILURE otherwise.