in source/demo-tasks/shadow_device_task.c [402:511]
static void prvIncomingPublishUpdateDeltaCallback( void * pxSubscriptionContext,
MQTTPublishInfo_t * pxPublishInfo )
{
static uint32_t ulCurrentVersion = 0; /* Remember the latest version number we've received */
uint32_t ulVersion = 0UL;
uint32_t ulNewState = 0UL;
char * pcOutValue = NULL;
uint32_t ulOutValueLength = 0UL;
JSONStatus_t result = JSONSuccess;
/* Remove compiler warnings about unused parameters. */
( void ) pxSubscriptionContext;
configASSERT( pxPublishInfo != NULL );
configASSERT( pxPublishInfo->pPayload != NULL );
LogDebug( ( "/update/delta json payload:%.*s.",
pxPublishInfo->payloadLength,
( const char * ) pxPublishInfo->pPayload ) );
/* The payload will look similar to this:
* {
* "state": {
* "powerOn": 1
* },
* "metadata": {
* "powerOn": {
* "timestamp": 1595437367
* }
* },
* "timestamp": 1595437367,
* "clientToken": "388062",
* "version": 12
* }
*/
/* Make sure the payload is a valid json document. */
result = JSON_Validate( pxPublishInfo->pPayload,
pxPublishInfo->payloadLength );
if( result != JSONSuccess )
{
LogError( ( "Invalid JSON document recieved!" ) );
}
else
{
/* Obtain the version value. */
result = JSON_Search( ( char * ) pxPublishInfo->pPayload,
pxPublishInfo->payloadLength,
"version",
sizeof( "version" ) - 1,
&pcOutValue,
( size_t * ) &ulOutValueLength );
if( result != JSONSuccess )
{
LogError( ( "Version field not found in JSON document!" ) );
}
else
{
/* Convert the extracted value to an unsigned integer value. */
ulVersion = ( uint32_t ) strtoul( pcOutValue, NULL, 10 );
/* Make sure the version is newer than the last one we received. */
if( ulVersion <= ulCurrentVersion )
{
/* In this demo, we discard the incoming message
* if the version number is not newer than the latest
* that we've received before. Your application may use a
* different approach.
*/
LogWarn( ( "Recieved unexpected delta update with version %u. Current version is %u",
( unsigned int ) ulVersion,
( unsigned int ) ulCurrentVersion ) );
}
else
{
LogInfo( ( "Recieved delta update with version %.*s.",
ulOutValueLength,
pcOutValue ) );
/* Set received version as the current version. */
ulCurrentVersion = ulVersion;
/* Get powerOn state from json documents. */
result = JSON_Search( ( char * ) pxPublishInfo->pPayload,
pxPublishInfo->payloadLength,
"state.powerOn",
sizeof( "state.powerOn" ) - 1,
&pcOutValue,
( size_t * ) &ulOutValueLength );
if( result != JSONSuccess )
{
LogError( ( "powerOn field not found in JSON document!" ) );
}
else
{
/* Convert the powerOn state value to an unsigned integer value. */
ulNewState = ( uint32_t ) strtoul( pcOutValue, NULL, 10 );
LogInfo( ( "Setting powerOn state to %u.",
( unsigned int ) ulNewState ) );
/* Set the new powerOn state. */
ulCurrentPowerOnState = ulNewState;
}
}
}
}
}