]> git.sur5r.net Git - freertos/blob - FreeRTOS-Labs/Source/FreeRTOS-IoT-Libraries/c_sdk/aws/common/src/aws_iot_operation.c
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Source / FreeRTOS-IoT-Libraries / c_sdk / aws / common / src / aws_iot_operation.c
1 /*\r
2  * AWS IoT Common V1.0.0\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
14  *\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
21  */\r
22 \r
23 /**\r
24  * @file aws_iot_operation.c\r
25  * @brief Functions for common AWS IoT operations.\r
26  */\r
27 \r
28 /* The config header is always included first. */\r
29 #include "iot_config.h"\r
30 \r
31 /* Standard includes. */\r
32 #include <string.h>\r
33 \r
34 /* Platform threads include. */\r
35 #include "platform/iot_threads.h"\r
36 \r
37 /* AWS IoT include. */\r
38 #include "aws_iot.h"\r
39 \r
40 /* Error handling include. */\r
41 #include "iot_error.h"\r
42 \r
43 /*-----------------------------------------------------------*/\r
44 \r
45 bool AwsIot_InitLists( IotListDouble_t * pPendingOperationsList,\r
46                        IotListDouble_t * pSubscriptionsList,\r
47                        IotMutex_t * pPendingOperationsMutex,\r
48                        IotMutex_t * pSubscriptionsMutex )\r
49 {\r
50     IOT_FUNCTION_ENTRY( bool, true );\r
51 \r
52     /* Flags to track cleanup. */\r
53     bool operationsMutexCreated = false;\r
54     bool subscriptionsMutexCreated = false;\r
55 \r
56     /* Create the mutex guarding the pending operations list. */\r
57     operationsMutexCreated = IotMutex_Create( pPendingOperationsMutex, false );\r
58 \r
59     if( operationsMutexCreated == false )\r
60     {\r
61         IOT_SET_AND_GOTO_CLEANUP( false );\r
62     }\r
63 \r
64     /* Create the mutex guarding the subscriptions list. */\r
65     subscriptionsMutexCreated = IotMutex_Create( pSubscriptionsMutex, false );\r
66 \r
67     if( subscriptionsMutexCreated == false )\r
68     {\r
69         IOT_SET_AND_GOTO_CLEANUP( false );\r
70     }\r
71 \r
72     /* Initialize lists. */\r
73     IotListDouble_Create( pPendingOperationsList );\r
74     IotListDouble_Create( pSubscriptionsList );\r
75 \r
76     IOT_FUNCTION_CLEANUP_BEGIN();\r
77 \r
78     /* Clean up on error. */\r
79     if( status == false )\r
80     {\r
81         if( operationsMutexCreated == true )\r
82         {\r
83             IotMutex_Destroy( pPendingOperationsMutex );\r
84         }\r
85     }\r
86 \r
87     IOT_FUNCTION_CLEANUP_END();\r
88 }\r
89 \r
90 /*-----------------------------------------------------------*/\r
91 \r
92 bool AwsIot_GenerateOperationTopic( const AwsIotTopicInfo_t * pTopicInfo,\r
93                                     char ** pTopicBuffer,\r
94                                     uint16_t * pOperationTopicLength )\r
95 {\r
96     bool status = true;\r
97     uint16_t bufferLength = 0;\r
98     uint16_t operationTopicLength = 0;\r
99     char * pBuffer = NULL;\r
100 \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
106 \r
107     /* Allocate memory for the topic buffer if no topic buffer is given. */\r
108     if( *pTopicBuffer == NULL )\r
109     {\r
110         pBuffer = pTopicInfo->mallocString( ( size_t ) bufferLength );\r
111 \r
112         if( pBuffer == NULL )\r
113         {\r
114             status = false;\r
115         }\r
116     }\r
117     /* Otherwise, use the given topic buffer. */\r
118     else\r
119     {\r
120         pBuffer = *pTopicBuffer;\r
121     }\r
122 \r
123     if( status == true )\r
124     {\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
128 \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
134 \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
140 \r
141         /* Set the output parameters. */\r
142         if( *pTopicBuffer == NULL )\r
143         {\r
144             *pTopicBuffer = pBuffer;\r
145         }\r
146 \r
147         *pOperationTopicLength = operationTopicLength;\r
148     }\r
149 \r
150     return status;\r
151 }\r
152 \r
153 /*-----------------------------------------------------------*/\r