mirror of
https://github.com/FreeRTOS/FreeRTOS-Kernel.git
synced 2025-12-17 09:05:18 -05:00
Add the Labs projects provided in the V10.2.1_191129 zip file.
This commit is contained in:
parent
46e5937529
commit
e5708b38e9
801 changed files with 356576 additions and 0 deletions
|
|
@ -0,0 +1,294 @@
|
|||
/*
|
||||
* AWS IoT Common V1.0.0
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file aws_iot_doc_parser.c
|
||||
* @brief Implements the functions in aws_iot_doc_parser.h
|
||||
*/
|
||||
|
||||
/* The config header is always included first. */
|
||||
#include "iot_config.h"
|
||||
|
||||
/* Standard includes. */
|
||||
#include <string.h>
|
||||
|
||||
/* JSON utilities include. */
|
||||
#include "aws_iot_doc_parser.h"
|
||||
|
||||
#define IS_QUOTE( str, idx ) \
|
||||
( ( str )[ ( idx ) ] == '"' && ( ( idx ) == 0 || ( str )[ ( idx ) - 1 ] != '\\' ) )
|
||||
#define IS_WHITESPACE( str, idx ) \
|
||||
( ( str )[ ( idx ) ] == ' ' || ( str )[ ( idx ) ] == '\n' || ( str )[ ( idx ) ] == '\r' || ( str )[ ( idx ) ] == '\t' )
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
bool AwsIotDocParser_FindValue( const char * pAwsIotJsonDocument,
|
||||
size_t awsIotJsonDocumentLength,
|
||||
const char * pAwsIotJsonKey,
|
||||
size_t awsIotJsonKeyLength,
|
||||
const char ** pAwsIotJsonValue,
|
||||
size_t * pAwsIotJsonValueLength )
|
||||
{
|
||||
size_t i = 0;
|
||||
size_t jsonValueLength = 0;
|
||||
char openCharacter = '\0', closeCharacter = '\0';
|
||||
int nestingLevel = 0;
|
||||
bool isWithinQuotes = false;
|
||||
|
||||
/* Validate all the arguments.*/
|
||||
if( ( pAwsIotJsonDocument == NULL ) || ( pAwsIotJsonKey == NULL ) ||
|
||||
( awsIotJsonDocumentLength == 0 ) || ( awsIotJsonKeyLength == 0 ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Ensure the JSON document is long enough to contain the key/value pair. At
|
||||
* the very least, a JSON key/value pair must contain the key and the 6
|
||||
* characters {":""} */
|
||||
if( awsIotJsonDocumentLength < awsIotJsonKeyLength + 6 )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Search the characters in the JSON document for the key. The end of the JSON
|
||||
* document does not have to be searched once too few characters remain to hold a
|
||||
* value. */
|
||||
while( i < awsIotJsonDocumentLength - awsIotJsonKeyLength - 3 )
|
||||
{
|
||||
/* If the first character in the key is found and there's an unescaped double
|
||||
* quote after the key length, do a string compare for the key. */
|
||||
if( ( IS_QUOTE( pAwsIotJsonDocument, i ) ) &&
|
||||
( IS_QUOTE( pAwsIotJsonDocument, i + 1 + awsIotJsonKeyLength ) ) &&
|
||||
( pAwsIotJsonDocument[ i + 1 ] == pAwsIotJsonKey[ 0 ] ) &&
|
||||
( strncmp( pAwsIotJsonDocument + i + 1,
|
||||
pAwsIotJsonKey,
|
||||
awsIotJsonKeyLength ) == 0 ) )
|
||||
{
|
||||
/* Key found; this is a potential match. */
|
||||
|
||||
/* Skip the characters in the JSON key and closing double quote. */
|
||||
/* While loop guarantees that i < awsIotJsonDocumentLength - 1 */
|
||||
i += awsIotJsonKeyLength + 2;
|
||||
|
||||
/* Skip all whitespace characters between the closing " and the : */
|
||||
while( IS_WHITESPACE( pAwsIotJsonDocument, i ) )
|
||||
{
|
||||
i++;
|
||||
|
||||
/* If the end of the document is reached, this isn't a match. */
|
||||
if( i >= awsIotJsonDocumentLength )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/* The character immediately following a key (and any whitespace) must be a :
|
||||
* If it's another character, then this string is a JSON value; skip it. */
|
||||
if( pAwsIotJsonDocument[ i ] != ':' )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Skip the : */
|
||||
i++;
|
||||
}
|
||||
|
||||
/* If the end of the document is reached, this isn't a match. */
|
||||
if( i >= awsIotJsonDocumentLength )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Skip all whitespace characters between : and the first character in the value. */
|
||||
while( IS_WHITESPACE( pAwsIotJsonDocument, i ) )
|
||||
{
|
||||
i++;
|
||||
|
||||
/* If the end of the document is reached, this isn't a match. */
|
||||
if( i >= awsIotJsonDocumentLength )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/* Value found. Set the output parameter. */
|
||||
if( pAwsIotJsonValue != NULL )
|
||||
{
|
||||
*pAwsIotJsonValue = pAwsIotJsonDocument + i;
|
||||
}
|
||||
|
||||
/* Calculate the value's length. */
|
||||
switch( pAwsIotJsonDocument[ i ] )
|
||||
{
|
||||
/* Calculate length of a JSON string. */
|
||||
case '\"':
|
||||
/* Include the length of the opening and closing double quotes. */
|
||||
jsonValueLength = 2;
|
||||
|
||||
/* Skip the opening double quote. */
|
||||
i++;
|
||||
|
||||
/* If the end of the document is reached, this isn't a match. */
|
||||
if( i >= awsIotJsonDocumentLength )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Add the length of all characters in the JSON string. */
|
||||
while( pAwsIotJsonDocument[ i ] != '\"' )
|
||||
{
|
||||
/* Ignore escaped double quotes. */
|
||||
if( ( pAwsIotJsonDocument[ i ] == '\\' ) &&
|
||||
( i + 1 < awsIotJsonDocumentLength ) &&
|
||||
( pAwsIotJsonDocument[ i + 1 ] == '\"' ) )
|
||||
{
|
||||
/* Skip the characters \" */
|
||||
i += 2;
|
||||
jsonValueLength += 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Add the length of a single character. */
|
||||
i++;
|
||||
jsonValueLength++;
|
||||
}
|
||||
|
||||
/* If the end of the document is reached, this isn't a match. */
|
||||
if( i >= awsIotJsonDocumentLength )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
/* Set the matching opening and closing characters of a JSON object or array.
|
||||
* The length calculation is performed below. */
|
||||
case '{':
|
||||
openCharacter = '{';
|
||||
closeCharacter = '}';
|
||||
break;
|
||||
|
||||
case '[':
|
||||
openCharacter = '[';
|
||||
closeCharacter = ']';
|
||||
break;
|
||||
|
||||
/* Calculate the length of a JSON primitive. */
|
||||
default:
|
||||
|
||||
/* Skip the characters in the JSON value. The JSON value ends with a , or } */
|
||||
while( pAwsIotJsonDocument[ i ] != ',' &&
|
||||
pAwsIotJsonDocument[ i ] != '}' )
|
||||
{
|
||||
/* Any whitespace before a , or } means the JSON document is invalid. */
|
||||
if( IS_WHITESPACE( pAwsIotJsonDocument, i ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
i++;
|
||||
jsonValueLength++;
|
||||
|
||||
/* If the end of the document is reached, this isn't a match. */
|
||||
if( i >= awsIotJsonDocumentLength )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
/* Calculate the length of a JSON object or array. */
|
||||
if( ( openCharacter != '\0' ) && ( closeCharacter != '\0' ) )
|
||||
{
|
||||
/* Include the length of the opening and closing characters. */
|
||||
jsonValueLength = 2;
|
||||
|
||||
/* Skip the opening character. */
|
||||
i++;
|
||||
|
||||
/* If the end of the document is reached, this isn't a match. */
|
||||
if( i >= awsIotJsonDocumentLength )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Add the length of all characters in the JSON object or array. This
|
||||
* includes the length of nested objects. */
|
||||
while( pAwsIotJsonDocument[ i ] != closeCharacter ||
|
||||
( pAwsIotJsonDocument[ i ] == closeCharacter && ( nestingLevel != 0 || isWithinQuotes ) ) )
|
||||
{
|
||||
/* Check if its a quote so as to avoid considering the
|
||||
* nested levels for opening and closing characters within
|
||||
* quotes.
|
||||
*/
|
||||
if( IS_QUOTE( pAwsIotJsonDocument, i ) )
|
||||
{
|
||||
/*Toggle the flag*/
|
||||
isWithinQuotes = !isWithinQuotes;
|
||||
}
|
||||
|
||||
/* Calculate the nesting levels only if not in quotes. */
|
||||
if( !isWithinQuotes )
|
||||
{
|
||||
/* An opening character starts a nested object. */
|
||||
if( pAwsIotJsonDocument[ i ] == openCharacter )
|
||||
{
|
||||
nestingLevel++;
|
||||
}
|
||||
/* A closing character ends a nested object. */
|
||||
else if( pAwsIotJsonDocument[ i ] == closeCharacter )
|
||||
{
|
||||
nestingLevel--;
|
||||
}
|
||||
}
|
||||
|
||||
i++;
|
||||
jsonValueLength++;
|
||||
|
||||
/* If the end of the document is reached, this isn't a match. */
|
||||
if( i >= awsIotJsonDocumentLength )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* JSON value length calculated; set the output parameter. */
|
||||
if( pAwsIotJsonValueLength != NULL )
|
||||
{
|
||||
*pAwsIotJsonValueLength = jsonValueLength;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
|
@ -0,0 +1,153 @@
|
|||
/*
|
||||
* AWS IoT Common V1.0.0
|
||||
* Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file aws_iot_operation.c
|
||||
* @brief Functions for common AWS IoT operations.
|
||||
*/
|
||||
|
||||
/* The config header is always included first. */
|
||||
#include "iot_config.h"
|
||||
|
||||
/* Standard includes. */
|
||||
#include <string.h>
|
||||
|
||||
/* Platform threads include. */
|
||||
#include "platform/iot_threads.h"
|
||||
|
||||
/* AWS IoT include. */
|
||||
#include "aws_iot.h"
|
||||
|
||||
/* Error handling include. */
|
||||
#include "iot_error.h"
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
bool AwsIot_InitLists( IotListDouble_t * pPendingOperationsList,
|
||||
IotListDouble_t * pSubscriptionsList,
|
||||
IotMutex_t * pPendingOperationsMutex,
|
||||
IotMutex_t * pSubscriptionsMutex )
|
||||
{
|
||||
IOT_FUNCTION_ENTRY( bool, true );
|
||||
|
||||
/* Flags to track cleanup. */
|
||||
bool operationsMutexCreated = false;
|
||||
bool subscriptionsMutexCreated = false;
|
||||
|
||||
/* Create the mutex guarding the pending operations list. */
|
||||
operationsMutexCreated = IotMutex_Create( pPendingOperationsMutex, false );
|
||||
|
||||
if( operationsMutexCreated == false )
|
||||
{
|
||||
IOT_SET_AND_GOTO_CLEANUP( false );
|
||||
}
|
||||
|
||||
/* Create the mutex guarding the subscriptions list. */
|
||||
subscriptionsMutexCreated = IotMutex_Create( pSubscriptionsMutex, false );
|
||||
|
||||
if( subscriptionsMutexCreated == false )
|
||||
{
|
||||
IOT_SET_AND_GOTO_CLEANUP( false );
|
||||
}
|
||||
|
||||
/* Initialize lists. */
|
||||
IotListDouble_Create( pPendingOperationsList );
|
||||
IotListDouble_Create( pSubscriptionsList );
|
||||
|
||||
IOT_FUNCTION_CLEANUP_BEGIN();
|
||||
|
||||
/* Clean up on error. */
|
||||
if( status == false )
|
||||
{
|
||||
if( operationsMutexCreated == true )
|
||||
{
|
||||
IotMutex_Destroy( pPendingOperationsMutex );
|
||||
}
|
||||
}
|
||||
|
||||
IOT_FUNCTION_CLEANUP_END();
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
bool AwsIot_GenerateOperationTopic( const AwsIotTopicInfo_t * pTopicInfo,
|
||||
char ** pTopicBuffer,
|
||||
uint16_t * pOperationTopicLength )
|
||||
{
|
||||
bool status = true;
|
||||
uint16_t bufferLength = 0;
|
||||
uint16_t operationTopicLength = 0;
|
||||
char * pBuffer = NULL;
|
||||
|
||||
/* Calculate the required topic buffer length. */
|
||||
bufferLength = ( uint16_t ) ( AWS_IOT_TOPIC_PREFIX_LENGTH +
|
||||
pTopicInfo->thingNameLength +
|
||||
pTopicInfo->operationNameLength +
|
||||
pTopicInfo->longestSuffixLength );
|
||||
|
||||
/* Allocate memory for the topic buffer if no topic buffer is given. */
|
||||
if( *pTopicBuffer == NULL )
|
||||
{
|
||||
pBuffer = pTopicInfo->mallocString( ( size_t ) bufferLength );
|
||||
|
||||
if( pBuffer == NULL )
|
||||
{
|
||||
status = false;
|
||||
}
|
||||
}
|
||||
/* Otherwise, use the given topic buffer. */
|
||||
else
|
||||
{
|
||||
pBuffer = *pTopicBuffer;
|
||||
}
|
||||
|
||||
if( status == true )
|
||||
{
|
||||
/* Copy the AWS IoT topic prefix into the topic buffer. */
|
||||
( void ) memcpy( pBuffer, AWS_IOT_TOPIC_PREFIX, AWS_IOT_TOPIC_PREFIX_LENGTH );
|
||||
operationTopicLength = ( uint16_t ) ( operationTopicLength + AWS_IOT_TOPIC_PREFIX_LENGTH );
|
||||
|
||||
/* Copy the Thing Name into the topic buffer. */
|
||||
( void ) memcpy( pBuffer + operationTopicLength,
|
||||
pTopicInfo->pThingName,
|
||||
pTopicInfo->thingNameLength );
|
||||
operationTopicLength = ( uint16_t ) ( operationTopicLength + pTopicInfo->thingNameLength );
|
||||
|
||||
/* Copy the Shadow operation string into the topic buffer. */
|
||||
( void ) memcpy( pBuffer + operationTopicLength,
|
||||
pTopicInfo->pOperationName,
|
||||
pTopicInfo->operationNameLength );
|
||||
operationTopicLength = ( uint16_t ) ( operationTopicLength + pTopicInfo->operationNameLength );
|
||||
|
||||
/* Set the output parameters. */
|
||||
if( *pTopicBuffer == NULL )
|
||||
{
|
||||
*pTopicBuffer = pBuffer;
|
||||
}
|
||||
|
||||
*pOperationTopicLength = operationTopicLength;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
|
@ -0,0 +1,181 @@
|
|||
/*
|
||||
* AWS IoT Common V1.0.0
|
||||
* Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file aws_iot_parser.c
|
||||
* @brief Parses topics for Thing Name and status.
|
||||
*/
|
||||
|
||||
/* The config header is always included first. */
|
||||
#include "iot_config.h"
|
||||
|
||||
/* Standard includes. */
|
||||
#include <string.h>
|
||||
|
||||
/* AWS IoT include. */
|
||||
#include "aws_iot.h"
|
||||
|
||||
/* Error handling include. */
|
||||
#include "iot_error.h"
|
||||
|
||||
/* AWS Parser include. */
|
||||
#include "aws_iot_doc_parser.h"
|
||||
|
||||
/**
|
||||
* @brief Minimum allowed topic length for an AWS IoT status topic.
|
||||
*
|
||||
* Topics must contain at least:
|
||||
* - The common prefix
|
||||
* - The suffix "/accepted" or "/rejected"
|
||||
* - 1 character for the Thing Name
|
||||
* - 2 characters for the operation name and the enclosing slashes
|
||||
*/
|
||||
#define MINIMUM_TOPIC_NAME_LENGTH \
|
||||
( uint16_t ) ( AWS_IOT_TOPIC_PREFIX_LENGTH + \
|
||||
AWS_IOT_ACCEPTED_SUFFIX_LENGTH + \
|
||||
1 + 2 )
|
||||
|
||||
/**
|
||||
* @brief The longest client token accepted by AWS IoT service, per AWS IoT
|
||||
* service limits.
|
||||
*/
|
||||
#define MAX_CLIENT_TOKEN_LENGTH ( 64 )
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
bool AwsIot_GetClientToken( const char * pJsonDocument,
|
||||
size_t jsonDocumentLength,
|
||||
const char ** pClientToken,
|
||||
size_t * pClientTokenLength )
|
||||
{
|
||||
/* Extract the client token from the JSON document. */
|
||||
bool status = AwsIotDocParser_FindValue( pJsonDocument,
|
||||
jsonDocumentLength,
|
||||
AWS_IOT_CLIENT_TOKEN_KEY,
|
||||
AWS_IOT_CLIENT_TOKEN_KEY_LENGTH,
|
||||
pClientToken,
|
||||
pClientTokenLength );
|
||||
|
||||
if( status == true )
|
||||
{
|
||||
/* Check that the length of the client token is valid. */
|
||||
if( ( *pClientTokenLength < 2 ) ||
|
||||
( *pClientTokenLength > MAX_CLIENT_TOKEN_LENGTH ) )
|
||||
{
|
||||
status = false;
|
||||
}
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
bool AwsIot_ParseThingName( const char * pTopicName,
|
||||
uint16_t topicNameLength,
|
||||
const char ** pThingName,
|
||||
size_t * pThingNameLength )
|
||||
{
|
||||
IOT_FUNCTION_ENTRY( bool, true );
|
||||
const char * pThingNameStart = NULL;
|
||||
size_t thingNameLength = 0;
|
||||
|
||||
/* Check that the topic name is at least as long as the minimum allowed. */
|
||||
if( topicNameLength < MINIMUM_TOPIC_NAME_LENGTH )
|
||||
{
|
||||
IOT_SET_AND_GOTO_CLEANUP( false );
|
||||
}
|
||||
|
||||
/* Check that the given topic starts with the common prefix. */
|
||||
if( strncmp( AWS_IOT_TOPIC_PREFIX,
|
||||
pTopicName,
|
||||
AWS_IOT_TOPIC_PREFIX_LENGTH ) != 0 )
|
||||
{
|
||||
IOT_SET_AND_GOTO_CLEANUP( false );
|
||||
}
|
||||
|
||||
/* The Thing Name starts immediately after the topic prefix. */
|
||||
pThingNameStart = pTopicName + AWS_IOT_TOPIC_PREFIX_LENGTH;
|
||||
|
||||
/* Calculate the length of the Thing Name, which is terminated with a '/'. */
|
||||
while( ( thingNameLength + AWS_IOT_TOPIC_PREFIX_LENGTH < ( size_t ) topicNameLength ) &&
|
||||
( pThingNameStart[ thingNameLength ] != '/' ) )
|
||||
{
|
||||
thingNameLength++;
|
||||
}
|
||||
|
||||
/* The end of the topic name was reached without finding a '/'. The topic
|
||||
* name is invalid. */
|
||||
if( thingNameLength + AWS_IOT_TOPIC_PREFIX_LENGTH >= ( size_t ) topicNameLength )
|
||||
{
|
||||
IOT_SET_AND_GOTO_CLEANUP( false );
|
||||
}
|
||||
|
||||
/* Set the output parameters. */
|
||||
*pThingName = pThingNameStart;
|
||||
*pThingNameLength = thingNameLength;
|
||||
|
||||
IOT_FUNCTION_EXIT_NO_CLEANUP();
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
AwsIotStatus_t AwsIot_ParseStatus( const char * pTopicName,
|
||||
uint16_t topicNameLength )
|
||||
{
|
||||
IOT_FUNCTION_ENTRY( AwsIotStatus_t, AWS_IOT_UNKNOWN );
|
||||
const char * pSuffixStart = NULL;
|
||||
|
||||
/* Both 'accepted' and 'rejected' topics are of the same length
|
||||
* The below is a defensive check at run time to ensure that.
|
||||
*/
|
||||
Iot_DefaultAssert( AWS_IOT_ACCEPTED_SUFFIX_LENGTH == AWS_IOT_REJECTED_SUFFIX_LENGTH );
|
||||
|
||||
/* Check that the status topic name is at least as long as the
|
||||
* "accepted" suffix. This length check will be good for rejected also
|
||||
* as both are of 8 characters in length. */
|
||||
if( topicNameLength > AWS_IOT_ACCEPTED_SUFFIX_LENGTH )
|
||||
{
|
||||
/* Calculate where the "accepted" suffix should start. */
|
||||
pSuffixStart = pTopicName + topicNameLength - AWS_IOT_ACCEPTED_SUFFIX_LENGTH;
|
||||
|
||||
/* Check if the end of the status topic name is "/accepted". */
|
||||
if( strncmp( pSuffixStart,
|
||||
AWS_IOT_ACCEPTED_SUFFIX,
|
||||
AWS_IOT_ACCEPTED_SUFFIX_LENGTH ) == 0 )
|
||||
{
|
||||
IOT_SET_AND_GOTO_CLEANUP( AWS_IOT_ACCEPTED );
|
||||
}
|
||||
|
||||
/* Check if the end of the status topic name is "/rejected". */
|
||||
if( strncmp( pSuffixStart,
|
||||
AWS_IOT_REJECTED_SUFFIX,
|
||||
AWS_IOT_REJECTED_SUFFIX_LENGTH ) == 0 )
|
||||
{
|
||||
IOT_SET_AND_GOTO_CLEANUP( AWS_IOT_REJECTED );
|
||||
}
|
||||
}
|
||||
|
||||
IOT_FUNCTION_EXIT_NO_CLEANUP();
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
|
@ -0,0 +1,164 @@
|
|||
/*
|
||||
* AWS IoT Common V1.0.0
|
||||
* Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file aws_iot_subscription.c
|
||||
* @brief Functions for common AWS IoT subscriptions.
|
||||
*/
|
||||
|
||||
/* The config header is always included first. */
|
||||
#include "iot_config.h"
|
||||
|
||||
/* Standard includes. */
|
||||
#include <string.h>
|
||||
|
||||
/* AWS IoT include. */
|
||||
#include "aws_iot.h"
|
||||
|
||||
/* Error handling include. */
|
||||
#include "iot_error.h"
|
||||
|
||||
/* MQTT include. */
|
||||
#include "iot_mqtt.h"
|
||||
|
||||
/**
|
||||
* @brief Modify subscriptions, either by unsubscribing or subscribing.
|
||||
*
|
||||
* @param[in] mqttOperation Either @ref mqtt_function_subscribesync or @ref
|
||||
* mqtt_function_unsubscribesync.
|
||||
* @param[in] pSubscriptionInfo Information needed to process an MQTT
|
||||
* operation.
|
||||
* @param[in] pTopicFilter The topic filter to modify.
|
||||
* @param[in] topicFilterLength The length of `pTopicFilter`.
|
||||
*
|
||||
* @return See the return values of @ref mqtt_function_subscribesync or
|
||||
* @ref mqtt_function_unsubscribesync.
|
||||
*/
|
||||
static IotMqttError_t _modifySubscriptions( AwsIotMqttFunction_t mqttOperation,
|
||||
const AwsIotSubscriptionInfo_t * pSubscriptionInfo,
|
||||
const char * pTopicFilter,
|
||||
uint16_t topicFilterLength );
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static IotMqttError_t _modifySubscriptions( AwsIotMqttFunction_t mqttOperation,
|
||||
const AwsIotSubscriptionInfo_t * pSubscriptionInfo,
|
||||
const char * pTopicFilter,
|
||||
uint16_t topicFilterLength )
|
||||
{
|
||||
IotMqttError_t status = IOT_MQTT_STATUS_PENDING;
|
||||
IotMqttSubscription_t subscription = IOT_MQTT_SUBSCRIPTION_INITIALIZER;
|
||||
|
||||
/* Per the AWS IoT documentation, topic subscriptions are QoS 1. */
|
||||
subscription.qos = IOT_MQTT_QOS_1;
|
||||
|
||||
/* Set the members of the subscription parameter. */
|
||||
subscription.pTopicFilter = pTopicFilter;
|
||||
subscription.topicFilterLength = topicFilterLength;
|
||||
subscription.callback.pCallbackContext = NULL;
|
||||
subscription.callback.function = pSubscriptionInfo->callbackFunction;
|
||||
|
||||
/* Call the MQTT operation function.
|
||||
* Subscription count is 1 in this case.
|
||||
* None of the flags are set in this call. Hence 0 is passed for flags.
|
||||
* Please refer to documentation for AwsIotMqttFunction_t for more details.
|
||||
*/
|
||||
status = mqttOperation( pSubscriptionInfo->mqttConnection,
|
||||
&subscription,
|
||||
1,
|
||||
0,
|
||||
pSubscriptionInfo->timeout );
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
IotMqttError_t AwsIot_ModifySubscriptions( AwsIotMqttFunction_t mqttOperation,
|
||||
const AwsIotSubscriptionInfo_t * pSubscriptionInfo )
|
||||
{
|
||||
IOT_FUNCTION_ENTRY( IotMqttError_t, IOT_MQTT_STATUS_PENDING );
|
||||
IotMqttError_t acceptedStatus = IOT_MQTT_STATUS_PENDING;
|
||||
uint16_t topicFilterLength = 0;
|
||||
|
||||
/* Place the topic "accepted" suffix at the end of the topic buffer. */
|
||||
( void ) memcpy( pSubscriptionInfo->pTopicFilterBase
|
||||
+ pSubscriptionInfo->topicFilterBaseLength,
|
||||
AWS_IOT_ACCEPTED_SUFFIX,
|
||||
AWS_IOT_ACCEPTED_SUFFIX_LENGTH );
|
||||
topicFilterLength = ( uint16_t ) ( pSubscriptionInfo->topicFilterBaseLength
|
||||
+ AWS_IOT_ACCEPTED_SUFFIX_LENGTH );
|
||||
|
||||
/* Modify the subscription to the "accepted" topic. */
|
||||
acceptedStatus = _modifySubscriptions( mqttOperation,
|
||||
pSubscriptionInfo,
|
||||
pSubscriptionInfo->pTopicFilterBase,
|
||||
topicFilterLength );
|
||||
|
||||
if( acceptedStatus != IOT_MQTT_SUCCESS )
|
||||
{
|
||||
IOT_SET_AND_GOTO_CLEANUP( acceptedStatus );
|
||||
}
|
||||
|
||||
/* Place the topic "rejected" suffix at the end of the topic buffer. */
|
||||
( void ) memcpy( pSubscriptionInfo->pTopicFilterBase
|
||||
+ pSubscriptionInfo->topicFilterBaseLength,
|
||||
AWS_IOT_REJECTED_SUFFIX,
|
||||
AWS_IOT_REJECTED_SUFFIX_LENGTH );
|
||||
topicFilterLength = ( uint16_t ) ( pSubscriptionInfo->topicFilterBaseLength
|
||||
+ AWS_IOT_REJECTED_SUFFIX_LENGTH );
|
||||
|
||||
/* Modify the subscription to the "rejected" topic. */
|
||||
status = _modifySubscriptions( mqttOperation,
|
||||
pSubscriptionInfo,
|
||||
pSubscriptionInfo->pTopicFilterBase,
|
||||
topicFilterLength );
|
||||
|
||||
IOT_FUNCTION_CLEANUP_BEGIN();
|
||||
|
||||
/* Clean up on error. */
|
||||
if( status != IOT_MQTT_SUCCESS )
|
||||
{
|
||||
/* Remove the subscription to the "accepted" topic if the subscription
|
||||
* to the "rejected" topic failed. */
|
||||
if( ( mqttOperation == IotMqtt_SubscribeSync ) &&
|
||||
( acceptedStatus == IOT_MQTT_SUCCESS ) )
|
||||
{
|
||||
/* Place the topic "accepted" suffix at the end of the topic buffer. */
|
||||
( void ) memcpy( pSubscriptionInfo->pTopicFilterBase
|
||||
+ pSubscriptionInfo->topicFilterBaseLength,
|
||||
AWS_IOT_ACCEPTED_SUFFIX,
|
||||
AWS_IOT_ACCEPTED_SUFFIX_LENGTH );
|
||||
topicFilterLength = ( uint16_t ) ( pSubscriptionInfo->topicFilterBaseLength
|
||||
+ AWS_IOT_ACCEPTED_SUFFIX_LENGTH );
|
||||
|
||||
( void ) _modifySubscriptions( IotMqtt_UnsubscribeSync,
|
||||
pSubscriptionInfo,
|
||||
pSubscriptionInfo->pTopicFilterBase,
|
||||
topicFilterLength );
|
||||
}
|
||||
}
|
||||
|
||||
IOT_FUNCTION_CLEANUP_END();
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* AWS IoT Common V1.0.0
|
||||
* Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file aws_iot_validate.c
|
||||
* @brief Validates Thing Names and other parameters to AWS IoT.
|
||||
*/
|
||||
|
||||
/* The config header is always included first. */
|
||||
#include "iot_config.h"
|
||||
|
||||
/* AWS IoT include. */
|
||||
#include "aws_iot.h"
|
||||
|
||||
/* Error handling include. */
|
||||
#include "iot_error.h"
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
bool AwsIot_ValidateThingName( const char * pThingName,
|
||||
size_t thingNameLength )
|
||||
{
|
||||
IOT_FUNCTION_ENTRY( bool, true );
|
||||
|
||||
if( pThingName == NULL )
|
||||
{
|
||||
IOT_SET_AND_GOTO_CLEANUP( false );
|
||||
}
|
||||
|
||||
if( thingNameLength == 0 )
|
||||
{
|
||||
IOT_SET_AND_GOTO_CLEANUP( false );
|
||||
}
|
||||
|
||||
if( thingNameLength > AWS_IOT_MAX_THING_NAME_LENGTH )
|
||||
{
|
||||
IOT_SET_AND_GOTO_CLEANUP( false );
|
||||
}
|
||||
|
||||
IOT_FUNCTION_EXIT_NO_CLEANUP();
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue