Microchip® Advanced Software Framework

aws_iot_shadow_json.c File Reference
#include "aws_iot_shadow_json.h"
#include <string.h>
#include <stdbool.h>
#include <inttypes.h>
#include "AWS_SDK/aws_iot_src/utils/aws_iot_json_utils.h"
#include "AWS_SDK/aws_iot_src/utils/aws_iot_log.h"
#include "aws_iot_shadow_key.h"
#include "aws_iot_config.h"

Functions

IoT_Error_t aws_iot_fill_with_client_token (char *pBufferToBeUpdatedWithClientToken, size_t maxSizeOfJsonDocument)
 Fill the given buffer with client token for tracking the Repsonse. More...
 
IoT_Error_t aws_iot_finalize_json_document (char *pJsonDocument, size_t maxSizeOfJsonDocument)
 Finalize the JSON document with Shadow expected client Token. More...
 
IoT_Error_t aws_iot_shadow_add_desired (char *pJsonDocument, size_t maxSizeOfJsonDocument, uint8_t count,...)
 Add the desired section of the JSON document of jsonStruct_t. More...
 
IoT_Error_t aws_iot_shadow_add_reported (char *pJsonDocument, size_t maxSizeOfJsonDocument, uint8_t count,...)
 Add the reported section of the JSON document of jsonStruct_t. More...
 
IoT_Error_t aws_iot_shadow_init_json_document (char *pJsonDocument, size_t maxSizeOfJsonDocument)
 Initialize the JSON document with Shadow expected name/value. More...
 
static IoT_Error_t checkReturnValueOfSnPrintf (int32_t snPrintfReturn, size_t maxSizeOfJsonDocument)
 
static IoT_Error_t convertDataToString (char *pStringBuffer, size_t maxSizoStringBuffer, JsonPrimitiveType type, void *pData)
 
static void emptyJsonWithClientToken (char *pJsonDocument)
 
bool extractClientToken (const char *pJsonDocument, char *pExtractedClientToken)
 
bool extractVersionNumber (const char *pJsonDocument, void *pJsonHandler, int32_t tokenCount, uint32_t *pVersionNumber)
 
void FillWithClientToken (char *pBufferToBeUpdatedWithClientToken)
 
int32_t FillWithClientTokenSize (char *pBufferToBeUpdatedWithClientToken, size_t maxSizeOfJsonDocument)
 
void iot_shadow_delete_request_json (char *pJsonDocument)
 
void iot_shadow_get_request_json (char *pJsonDocument)
 
bool isJsonKeyMatchingAndUpdateValue (const char *pJsonDocument, void *pJsonHandler, int32_t tokenCount, jsonStruct_t *pDataStruct, uint32_t *pDataLength, int32_t *pDataPosition)
 
bool isJsonValidAndParse (const char *pJsonDocument, void *pJsonHandler, int32_t *pTokenCount)
 
bool isReceivedJsonValid (const char *pJsonDocument)
 
void resetClientTokenSequenceNum (void)
 
static IoT_Error_t UpdateValueIfNoObject (const char *pJsonString, jsonStruct_t *pDataStruct, jsmntok_t token)
 

Variables

static uint32_t clientTokenNum = 0
 
static jsmntok_t jsonTokenStruct [MAX_JSON_TOKEN_EXPECTED]
 
char mqttClientID [MAX_SIZE_OF_UNIQUE_CLIENT_ID_BYTES]
 
static jsmn_parser shadowJsonParser
 

IoT_Error_t aws_iot_fill_with_client_token ( char *  pBufferToBeUpdatedWithClientToken,
size_t  maxSizeOfJsonDocument 
)

Fill the given buffer with client token for tracking the Repsonse.

This function will add the AWS_IOT_MQTT_CLIENT_ID with a sequence number. Every time this function is used the sequence number gets incremented

Parameters
pBufferToBeUpdatedWithClientTokenbuffer to be updated with the client token string
maxSizeOfJsonDocumentmaximum size of the pBufferToBeUpdatedWithClientToken that can be used
Returns
An IoT Error Type defining if the buffer was null or the entire string was not filled up

References checkReturnValueOfSnPrintf(), and FillWithClientTokenSize().

IoT_Error_t aws_iot_finalize_json_document ( char *  pJsonDocument,
size_t  maxSizeOfJsonDocument 
)

Finalize the JSON document with Shadow expected client Token.

This function will automatically increment the client token every time this function is called.

Note
Ensure the size of the Buffer is enough to hold the entire JSON Document. If the finalized section is not invoked then the JSON doucment will not be valid
Parameters
pJsonDocumentThe JSON Document filled in this char buffer
maxSizeOfJsonDocumentmaximum size of the pJsonDocument that can be used to fill the JSON document
Returns
An IoT Error Type defining if the buffer was null or the entire string was not filled up

References checkReturnValueOfSnPrintf(), FillWithClientTokenSize(), NONE_ERROR, NULL, NULL_VALUE_ERROR, SHADOW_CLIENT_TOKEN_STRING, and SHADOW_JSON_ERROR.

IoT_Error_t aws_iot_shadow_add_desired ( char *  pJsonDocument,
size_t  maxSizeOfJsonDocument,
uint8_t  count,
  ... 
)

Add the desired section of the JSON document of jsonStruct_t.

This is a variadic function and please be careful with the usage. count is the number of jsonStruct_t types that you would like to add in the reported section This function will add "desired":{<all the="" values="" that="" needs="" to="" be="" added>="">}

Note
Ensure the size of the Buffer is enough to hold the reported section + the init section. Always use the same JSON document buffer used in the iot_shadow_init_json_document function. This function will accommodate the size of previous null terminated string, so pass the max size of the buffer
Parameters
pJsonDocumentThe JSON Document filled in this char buffer
maxSizeOfJsonDocumentmaximum size of the pJsonDocument that can be used to fill the JSON document
counttotal number of arguments(jsonStruct_t object) passed in the arguments
Returns
An IoT Error Type defining if the buffer was null or the entire string was not filled up

References checkReturnValueOfSnPrintf(), convertDataToString(), count, i, NONE_ERROR, NULL, NULL_VALUE_ERROR, jsonStruct::pData, jsonStruct::pKey, SHADOW_JSON_ERROR, and jsonStruct::type.

IoT_Error_t aws_iot_shadow_add_reported ( char *  pJsonDocument,
size_t  maxSizeOfJsonDocument,
uint8_t  count,
  ... 
)

Add the reported section of the JSON document of jsonStruct_t.

This is a variadic function and please be careful with the usage. count is the number of jsonStruct_t types that you would like to add in the reported section This function will add "reported":{<all the="" values="" that="" needs="" to="" be="" added>="">}

Note
Ensure the size of the Buffer is enough to hold the reported section + the init section. Always use the same JSON document buffer used in the iot_shadow_init_json_document function. This function will accommodate the size of previous null terminated string, so pass teh max size of the buffer
Parameters
pJsonDocumentThe JSON Document filled in this char buffer
maxSizeOfJsonDocumentmaximum size of the pJsonDocument that can be used to fill the JSON document
counttotal number of arguments(jsonStruct_t object) passed in the arguments
Returns
An IoT Error Type defining if the buffer was null or the entire string was not filled up

References checkReturnValueOfSnPrintf(), convertDataToString(), count, i, NONE_ERROR, NULL, NULL_VALUE_ERROR, jsonStruct::pData, jsonStruct::pKey, SHADOW_JSON_ERROR, and jsonStruct::type.

IoT_Error_t aws_iot_shadow_init_json_document ( char *  pJsonDocument,
size_t  maxSizeOfJsonDocument 
)

Initialize the JSON document with Shadow expected name/value.

This Function will fill the JSON Buffer with a null terminated string. Internally it uses snprintf This function should always be used First, followed by iot_shadow_add_reported and/or iot_shadow_add_desired. Always finish the call sequence with iot_finalize_json_document

Note
Ensure the size of the Buffer is enough to hold the entire JSON Document.
Parameters
pJsonDocumentThe JSON Document filled in this char buffer
maxSizeOfJsonDocumentmaximum size of the pJsonDocument that can be used to fill the JSON document
Returns
An IoT Error Type defining if the buffer was null or the entire string was not filled up

References checkReturnValueOfSnPrintf(), NONE_ERROR, NULL, and NULL_VALUE_ERROR.

static IoT_Error_t checkReturnValueOfSnPrintf ( int32_t  snPrintfReturn,
size_t  maxSizeOfJsonDocument 
)
inlinestatic
static void emptyJsonWithClientToken ( char *  pJsonDocument)
static
bool extractClientToken ( const char *  pJsonDocument,
char *  pExtractedClientToken 
)
bool extractVersionNumber ( const char *  pJsonDocument,
void *  pJsonHandler,
int32_t  tokenCount,
uint32_t *  pVersionNumber 
)
void FillWithClientToken ( char *  pBufferToBeUpdatedWithClientToken)

References clientTokenNum, and mqttClientID.

Referenced by emptyJsonWithClientToken().

int32_t FillWithClientTokenSize ( char *  pBufferToBeUpdatedWithClientToken,
size_t  maxSizeOfJsonDocument 
)
void iot_shadow_delete_request_json ( char *  pJsonDocument)
void iot_shadow_get_request_json ( char *  pJsonDocument)
bool isJsonKeyMatchingAndUpdateValue ( const char *  pJsonDocument,
void *  pJsonHandler,
int32_t  tokenCount,
jsonStruct_t pDataStruct,
uint32_t *  pDataLength,
int32_t *  pDataPosition 
)
bool isJsonValidAndParse ( const char *  pJsonDocument,
void *  pJsonHandler,
int32_t *  pTokenCount 
)
bool isReceivedJsonValid ( const char *  pJsonDocument)
void resetClientTokenSequenceNum ( void  )

References clientTokenNum.

Referenced by aws_iot_shadow_init().

uint32_t clientTokenNum = 0
static
jsmntok_t jsonTokenStruct[MAX_JSON_TOKEN_EXPECTED]
static
char mqttClientID[MAX_SIZE_OF_UNIQUE_CLIENT_ID_BYTES]
jsmn_parser shadowJsonParser
static