2 * AWS IoT Common V1.0.0
\r
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
24 * @file aws_iot_operation.c
\r
25 * @brief Functions for common AWS IoT operations.
\r
28 /* The config header is always included first. */
\r
29 #include "iot_config.h"
\r
31 /* Standard includes. */
\r
34 /* Platform threads include. */
\r
35 #include "platform/iot_threads.h"
\r
37 /* AWS IoT include. */
\r
38 #include "aws_iot.h"
\r
40 /* Error handling include. */
\r
41 #include "iot_error.h"
\r
43 /*-----------------------------------------------------------*/
\r
45 bool AwsIot_InitLists( IotListDouble_t * pPendingOperationsList,
\r
46 IotListDouble_t * pSubscriptionsList,
\r
47 IotMutex_t * pPendingOperationsMutex,
\r
48 IotMutex_t * pSubscriptionsMutex )
\r
50 IOT_FUNCTION_ENTRY( bool, true );
\r
52 /* Flags to track cleanup. */
\r
53 bool operationsMutexCreated = false;
\r
54 bool subscriptionsMutexCreated = false;
\r
56 /* Create the mutex guarding the pending operations list. */
\r
57 operationsMutexCreated = IotMutex_Create( pPendingOperationsMutex, false );
\r
59 if( operationsMutexCreated == false )
\r
61 IOT_SET_AND_GOTO_CLEANUP( false );
\r
64 /* Create the mutex guarding the subscriptions list. */
\r
65 subscriptionsMutexCreated = IotMutex_Create( pSubscriptionsMutex, false );
\r
67 if( subscriptionsMutexCreated == false )
\r
69 IOT_SET_AND_GOTO_CLEANUP( false );
\r
72 /* Initialize lists. */
\r
73 IotListDouble_Create( pPendingOperationsList );
\r
74 IotListDouble_Create( pSubscriptionsList );
\r
76 IOT_FUNCTION_CLEANUP_BEGIN();
\r
78 /* Clean up on error. */
\r
79 if( status == false )
\r
81 if( operationsMutexCreated == true )
\r
83 IotMutex_Destroy( pPendingOperationsMutex );
\r
87 IOT_FUNCTION_CLEANUP_END();
\r
90 /*-----------------------------------------------------------*/
\r
92 bool AwsIot_GenerateOperationTopic( const AwsIotTopicInfo_t * pTopicInfo,
\r
93 char ** pTopicBuffer,
\r
94 uint16_t * pOperationTopicLength )
\r
97 uint16_t bufferLength = 0;
\r
98 uint16_t operationTopicLength = 0;
\r
99 char * pBuffer = NULL;
\r
101 /* Calculate the required topic buffer length. */
\r
102 bufferLength = ( uint16_t ) ( AWS_IOT_TOPIC_PREFIX_LENGTH +
\r
103 pTopicInfo->thingNameLength +
\r
104 pTopicInfo->operationNameLength +
\r
105 pTopicInfo->longestSuffixLength );
\r
107 /* Allocate memory for the topic buffer if no topic buffer is given. */
\r
108 if( *pTopicBuffer == NULL )
\r
110 pBuffer = pTopicInfo->mallocString( ( size_t ) bufferLength );
\r
112 if( pBuffer == NULL )
\r
117 /* Otherwise, use the given topic buffer. */
\r
120 pBuffer = *pTopicBuffer;
\r
123 if( status == true )
\r
125 /* Copy the AWS IoT topic prefix into the topic buffer. */
\r
126 ( void ) memcpy( pBuffer, AWS_IOT_TOPIC_PREFIX, AWS_IOT_TOPIC_PREFIX_LENGTH );
\r
127 operationTopicLength = ( uint16_t ) ( operationTopicLength + AWS_IOT_TOPIC_PREFIX_LENGTH );
\r
129 /* Copy the Thing Name into the topic buffer. */
\r
130 ( void ) memcpy( pBuffer + operationTopicLength,
\r
131 pTopicInfo->pThingName,
\r
132 pTopicInfo->thingNameLength );
\r
133 operationTopicLength = ( uint16_t ) ( operationTopicLength + pTopicInfo->thingNameLength );
\r
135 /* Copy the Shadow operation string into the topic buffer. */
\r
136 ( void ) memcpy( pBuffer + operationTopicLength,
\r
137 pTopicInfo->pOperationName,
\r
138 pTopicInfo->operationNameLength );
\r
139 operationTopicLength = ( uint16_t ) ( operationTopicLength + pTopicInfo->operationNameLength );
\r
141 /* Set the output parameters. */
\r
142 if( *pTopicBuffer == NULL )
\r
144 *pTopicBuffer = pBuffer;
\r
147 *pOperationTopicLength = operationTopicLength;
\r
153 /*-----------------------------------------------------------*/
\r