AWS IoT Device Shadow  v1.0.2
AWS IoT Device Shadow client library
shadow.c File Reference

Implements the user-facing functions of the Shadow library. More...

#include <string.h>
#include "shadow.h"

Macros

#define SHADOW_OP_UPDATE_ACCEPTED   SHADOW_OP_UPDATE SHADOW_SUFFIX_ACCEPTED
 The string representing "/shadow/update/accepted".
 
#define SHADOW_OP_UPDATE_REJECTED   SHADOW_OP_UPDATE SHADOW_SUFFIX_REJECTED
 The string representing "/shadow/update/rejected".
 
#define SHADOW_OP_UPDATE_DELTA   SHADOW_OP_UPDATE SHADOW_SUFFIX_DELTA
 The string representing "/shadow/update/delta".
 
#define SHADOW_OP_UPDATE_DOCUMENTS   SHADOW_OP_UPDATE SHADOW_SUFFIX_DOCUMENTS
 The string representing "/shadow/update/document".
 
#define SHADOW_OP_DELETE_ACCEPTED   SHADOW_OP_DELETE SHADOW_SUFFIX_ACCEPTED
 The string representing "/shadow/delete/accepted".
 
#define SHADOW_OP_DELETE_REJECTED   SHADOW_OP_DELETE SHADOW_SUFFIX_REJECTED
 The string representing "/shadow/delete/accepted".
 
#define SHADOW_OP_GET_ACCEPTED   SHADOW_OP_GET SHADOW_SUFFIX_ACCEPTED
 The string representing "/shadow/get/accepted".
 
#define SHADOW_OP_GET_REJECTED   SHADOW_OP_GET SHADOW_SUFFIX_REJECTED
 The string representing "/shadow/get/accepted".
 
#define SHADOW_OP_UPDATE_ACCEPTED_LENGTH   ( SHADOW_OP_UPDATE_LENGTH + SHADOW_SUFFIX_ACCEPTED_LENGTH )
 The length of "/shadow/update/accepted".
 
#define SHADOW_OP_UPDATE_REJECTED_LENGTH   ( SHADOW_OP_UPDATE_LENGTH + SHADOW_SUFFIX_REJECTED_LENGTH )
 The length of "/shadow/update/rejected".
 
#define SHADOW_OP_UPDATE_DOCUMENTS_LENGTH   ( SHADOW_OP_UPDATE_LENGTH + SHADOW_SUFFIX_DOCUMENTS_LENGTH )
 The length of "/shadow/update/document".
 
#define SHADOW_OP_UPDATE_DELTA_LENGTH   ( SHADOW_OP_UPDATE_LENGTH + SHADOW_SUFFIX_DELTA_LENGTH )
 The length of "/shadow/update/rejected".
 
#define SHADOW_OP_GET_ACCEPTED_LENGTH   ( SHADOW_OP_GET_LENGTH + SHADOW_SUFFIX_ACCEPTED_LENGTH )
 The length of "/shadow/get/accepted".
 
#define SHADOW_OP_GET_REJECTED_LENGTH   ( SHADOW_OP_GET_LENGTH + SHADOW_SUFFIX_REJECTED_LENGTH )
 The length of "/shadow/get/rejected".
 
#define SHADOW_OP_DELETE_ACCEPTED_LENGTH   ( SHADOW_OP_DELETE_LENGTH + SHADOW_SUFFIX_ACCEPTED_LENGTH )
 The length of "/shadow/get/accepted".
 
#define SHADOW_OP_DELETE_REJECTED_LENGTH   ( SHADOW_OP_DELETE_LENGTH + SHADOW_SUFFIX_REJECTED_LENGTH )
 The length of "/shadow/delete/rejected".
 

Functions

static ShadowStatus_t containsSubString (const char *pString, uint16_t stringLength, const char *pSubString, uint16_t subStringLength)
 Determine if the string contains the substring. More...
 
static ShadowStatus_t validateThingName (const char *pString, uint16_t stringLength, uint16_t *pThingNameLength)
 Check if the Thing Name is valid. More...
 
static ShadowStatus_t extractShadowMessageType (const char *pString, uint16_t stringLength, ShadowMessageType_t *pMessageType)
 Extract the Shadow message type from a string. More...
 
static const char * getShadowOperationString (ShadowTopicStringType_t topicType)
 Get the shadow operation string for a given shadow topic type. More...
 
static uint16_t getShadowOperationLength (ShadowTopicStringType_t topicType)
 Get the shadow operation string length for a given shadow topic type. More...
 
ShadowStatus_t Shadow_MatchTopic (const char *pTopic, uint16_t topicLength, ShadowMessageType_t *pMessageType, const char **pThingName, uint16_t *pThingNameLength)
 Given the topic string of an incoming message, determine whether it is related to a device shadow; if it is, return information about the type of device shadow message, and a pointer to the Thing Name inside of the topic string. See ShadowMessageType_t for the list of message types. Those types correspond to Device Shadow Topics. More...
 
ShadowStatus_t Shadow_GetTopicString (ShadowTopicStringType_t topicType, const char *pThingName, uint8_t thingNameLength, char *pTopicBuffer, uint16_t bufferSize, uint16_t *pOutLength)
 Assemble shadow topic string when Thing Name is only known at run time. If the Thing Name is known at compile time, use SHADOW_TOPIC_STRING_* macros instead. More...
 

Detailed Description

Implements the user-facing functions of the Shadow library.

Function Documentation

◆ containsSubString()

static ShadowStatus_t containsSubString ( const char *  pString,
uint16_t  stringLength,
const char *  pSubString,
uint16_t  subStringLength 
)
static

Determine if the string contains the substring.

Parameters
[in]pStringPointer to the string.
[in]stringLengthLength of pString.
[in]pSubStringPointer to the substring.
[in]subStringLengthLength of pSubString.
Returns
Return SHADOW_SUCCESS if it contains; return SHADOW_FAIL if not.

◆ validateThingName()

static ShadowStatus_t validateThingName ( const char *  pString,
uint16_t  stringLength,
uint16_t *  pThingNameLength 
)
static

Check if the Thing Name is valid.

Parameters
[in]pStringPointer to the starting of thing name.
[in]stringLengthLength of pString.
[out]pThingNameLengthPointer to caller-supplied memory for returning the length of the Thing Name.
Returns
Return SHADOW_SUCCESS if it is valid; return SHADOW_THINGNAME_PARSE_FAILED if it is not.

◆ extractShadowMessageType()

static ShadowStatus_t extractShadowMessageType ( const char *  pString,
uint16_t  stringLength,
ShadowMessageType_t pMessageType 
)
static

Extract the Shadow message type from a string.

Parameters
[in]pStringPointer to the string.
[in]stringLengthLength of pString.
[out]pMessageTypePointer to call-supplied memory for returning the type of the shadow message.
Returns
Return SHADOW_SUCCESS if successfully extracted; return SHADOW_SHADOW_MESSAGE_TYPE_PARSE_FAILED if failed.

◆ getShadowOperationString()

static const char * getShadowOperationString ( ShadowTopicStringType_t  topicType)
static

Get the shadow operation string for a given shadow topic type.

Parameters
[in]topicTypeThe given shadow topic type.
Returns
The shadow operation string for the given shadow type.

◆ getShadowOperationLength()

static uint16_t getShadowOperationLength ( ShadowTopicStringType_t  topicType)
static

Get the shadow operation string length for a given shadow topic type.

Parameters
[in]topicTypeThe given shadow topic type.
Returns
The shadow operation string length for the given shadow type.

◆ Shadow_MatchTopic()

ShadowStatus_t Shadow_MatchTopic ( const char *  pTopic,
uint16_t  topicLength,
ShadowMessageType_t pMessageType,
const char **  pThingName,
uint16_t *  pThingNameLength 
)

Given the topic string of an incoming message, determine whether it is related to a device shadow; if it is, return information about the type of device shadow message, and a pointer to the Thing Name inside of the topic string. See ShadowMessageType_t for the list of message types. Those types correspond to Device Shadow Topics.

Note
When this function returns, the pointer pThingName points at the first character of the <thingName> segment inside of the topic string. Caller is responsible for keeping the memory holding the topic string around while accessing the Thing Name through pThingName.
Parameters
[in]pTopicPointer to the MQTT topic string. Does not have to be null-terminated.
[in]topicLengthLength of the MQTT topic string.
[out]pMessageTypePointer to caller-supplied memory for returning the type of the shadow message.
[out]pThingNamePoints to the 1st character of Thing Name inside of the topic string, and can be null if caller does not need to know the Thing Name contained in the topic string.
[out]pThingNameLengthPointer to caller-supplied memory for returning the length of the Thing Name, and can be null if caller does not need to know the Thing Name contained in the topic string.
Returns
One of the following:
  • SHADOW_SUCCESS if the message is related to a device shadow;
  • An error code defined in ShadowStatus_t if the message is not related to a device shadow, if any input parameter is invalid, or if the function fails to parse the topic string.

Example

// Variables used in this example.
#define SHADOW_TOPIC_MAX_LENGTH ( 256U )
ShadowStatus_te shadowStatus = SHADOW_SUCCESS;
char * pTopicName; //usually supplied by MQTT stack
uint16_t topicNameLength; //usually supplied by MQTT stack
ShadowMessageType_t messageType;
shadowStatus = Shadow_MatchTopic( pTopicName,
topicNameLength,
& messageType,
NULL,
NULL );
if( shadowStatus == SHADOW_SUCCESS )
{
// It is a device shadow message. And the type of the message has been returned in messageType.
// Now we can act on the message.
}

◆ Shadow_GetTopicString()

ShadowStatus_t Shadow_GetTopicString ( ShadowTopicStringType_t  topicType,
const char *  pThingName,
uint8_t  thingNameLength,
char *  pTopicBuffer,
uint16_t  bufferSize,
uint16_t *  pOutLength 
)

Assemble shadow topic string when Thing Name is only known at run time. If the Thing Name is known at compile time, use SHADOW_TOPIC_STRING_* macros instead.

Parameters
[in]topicTypeIndicates what topic will be written into the buffer pointed to by pTopicBuffer. can be one of:
  • ShadowTopicStringTypeGet
  • ShadowTopicStringTypeGetAccepted
  • ShadowTopicStringTypeGetRejected
  • ShadowTopicStringTypeDelete
  • ShadowTopicStringTypeDeleteAccepted
  • ShadowTopicStringTypeDeleteRejected
  • ShadowTopicStringTypeUpdate
  • ShadowTopicStringTypeUpdateAccepted
  • ShadowTopicStringTypeUpdateRejected
  • ShadowTopicStringTypeUpdateDocuments
  • ShadowTopicStringTypeUpdateDelta
[in]pThingNameThing Name string. No need to be null terminated. Must not be NULL.
[in]thingNameLengthLength of Thing Name string pointed to by pThingName. Must not be zero.
[out]pTopicBufferPointer to buffer for returning the topic string. Caller is responsible for supplying memory pointed to by pTopicBuffer. This function does not fill in the terminating null character. The app can supply a buffer that does not have space for holding the null character.
[in]bufferSizeLength of pTopicBuffer. This function will return error if bufferSize is less than the length of the assembled topic string.
[out]pOutLengthPointer to caller-supplied memory for returning the length of the topic string.
Returns
One of the following:
  • SHADOW_SUCCESS if successful.
  • An error code if failed to assemble.

Example

// Variables used in this example.
#define SHADOW_TOPIC_MAX_LENGTH ( 256U )
char topicBuffer[ SHADOW_TOPIC_MAX_LENGTH ] = { 0 };
uint16_t bufferSize = SHADOW_TOPIC_MAX_LENGTH;
uint16_t outLength = 0;
const char * pThingName = "TestThingName";
uint16_t thingNameLength = ( sizeof( thingName ) - 1U );
shadowStatus = Shadow_GetTopicString( ShadowTopicStringTypeUpdateDelta,
pThingName,
thingNameLength,
& ( topicBuffer[ 0 ] ),
bufferSize,
& outLength );
if( shadowStatus == SHADOW_SUCCESS )
{
// The assembled topic string is put in pTopicBuffer with the length outLength.
}
ShadowMessageType_t
ShadowMessageType_t
Each of these values describes the type of a shadow message. https://docs.aws.amazon....
Definition: shadow.h:54
ShadowStatus_t
ShadowStatus_t
Return codes from Shadow functions.
Definition: shadow.h:94
Shadow_GetTopicString
ShadowStatus_t Shadow_GetTopicString(ShadowTopicStringType_t topicType, const char *pThingName, uint8_t thingNameLength, char *pTopicBuffer, uint16_t bufferSize, uint16_t *pOutLength)
Assemble shadow topic string when Thing Name is only known at run time. If the Thing Name is known at...
Definition: shadow.c:536
Shadow_MatchTopic
ShadowStatus_t Shadow_MatchTopic(const char *pTopic, uint16_t topicLength, ShadowMessageType_t *pMessageType, const char **pThingName, uint16_t *pThingNameLength)
Given the topic string of an incoming message, determine whether it is related to a device shadow; if...
Definition: shadow.c:430
SHADOW_SUCCESS
@ SHADOW_SUCCESS
Shadow function success.
Definition: shadow.h:95