]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-IoT-Libraries/c_sdk/standard/mqtt/src/iot_mqtt_static_memory.c
Correct an err in queue.c introduced when previously updating behaviour when queue...
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-IoT-Libraries / c_sdk / standard / mqtt / src / iot_mqtt_static_memory.c
1 /*\r
2  * Amazon FreeRTOS MQTT V2.0.0\r
3  * Copyright (C) 2018 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  * http://aws.amazon.com/freertos\r
23  * http://www.FreeRTOS.org\r
24  */\r
25 \r
26 /**\r
27  * @file iot_mqtt_static_memory.c\r
28  * @brief Implementation of MQTT static memory functions.\r
29  */\r
30 \r
31 /* The config header is always included first. */\r
32 #include "iot_config.h"\r
33 \r
34 /* This file should only be compiled if dynamic memory allocation is forbidden. */\r
35 #if IOT_STATIC_MEMORY_ONLY == 1\r
36 \r
37 /* Standard includes. */\r
38 #include <stdbool.h>\r
39 #include <stddef.h>\r
40 #include <string.h>\r
41 \r
42 /* Static memory include. */\r
43 #include "private/iot_static_memory.h"\r
44 \r
45 /* MQTT internal include. */\r
46 #include "private/iot_mqtt_internal.h"\r
47 \r
48 /*-----------------------------------------------------------*/\r
49 \r
50 /**\r
51  * @cond DOXYGEN_IGNORE\r
52  * Doxygen should ignore this section.\r
53  *\r
54  * Provide default values for undefined configuration constants.\r
55  */\r
56 #ifndef IOT_MQTT_CONNECTIONS\r
57     #define IOT_MQTT_CONNECTIONS                   ( 1 )\r
58 #endif\r
59 #ifndef IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS\r
60     #define IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS    ( 10 )\r
61 #endif\r
62 #ifndef IOT_MQTT_SUBSCRIPTIONS\r
63     #define IOT_MQTT_SUBSCRIPTIONS                 ( 8 )\r
64 #endif\r
65 /** @endcond */\r
66 \r
67 /* Validate static memory configuration settings. */\r
68 #if IOT_MQTT_CONNECTIONS <= 0\r
69     #error "IOT_MQTT_CONNECTIONS cannot be 0 or negative."\r
70 #endif\r
71 #if IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS <= 0\r
72     #error "IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS cannot be 0 or negative."\r
73 #endif\r
74 #if IOT_MQTT_SUBSCRIPTIONS <= 0\r
75     #error "IOT_MQTT_SUBSCRIPTIONS cannot be 0 or negative."\r
76 #endif\r
77 \r
78 /**\r
79  * @brief The size of a static memory MQTT subscription.\r
80  *\r
81  * Since the pTopic member of #_mqttSubscription_t is variable-length, the constant\r
82  * #AWS_IOT_MQTT_SERVER_MAX_TOPIC_LENGTH is used for the length of\r
83  * #_mqttSubscription_t.pTopicFilter.\r
84  */\r
85 #define MQTT_SUBSCRIPTION_SIZE    ( sizeof( _mqttSubscription_t ) + AWS_IOT_MQTT_SERVER_MAX_TOPIC_LENGTH )\r
86 \r
87 /*-----------------------------------------------------------*/\r
88 \r
89 /*\r
90  * Static memory buffers and flags, allocated and zeroed at compile-time.\r
91  */\r
92 static bool _pInUseMqttConnections[ IOT_MQTT_CONNECTIONS ] = { 0 };                               /**< @brief MQTT connection in-use flags. */\r
93 static _mqttConnection_t _pMqttConnections[ IOT_MQTT_CONNECTIONS ] = { { 0 } };                   /**< @brief MQTT connections. */\r
94 \r
95 static bool _pInUseMqttOperations[ IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS ] = { 0 };                        /**< @brief MQTT operation in-use flags. */\r
96 static _mqttOperation_t _pMqttOperations[ IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS ] = { { .link = { 0 } } }; /**< @brief MQTT operations. */\r
97 \r
98 static bool _pInUseMqttSubscriptions[ IOT_MQTT_SUBSCRIPTIONS ] = { 0 };                           /**< @brief MQTT subscription in-use flags. */\r
99 static char _pMqttSubscriptions[ IOT_MQTT_SUBSCRIPTIONS ][ MQTT_SUBSCRIPTION_SIZE ] = { { 0 } };  /**< @brief MQTT subscriptions. */\r
100 \r
101 /*-----------------------------------------------------------*/\r
102 \r
103 void * IotMqtt_MallocConnection( size_t size )\r
104 {\r
105     int32_t freeIndex = -1;\r
106     void * pNewConnection = NULL;\r
107 \r
108     /* Check size argument. */\r
109     if( size == sizeof( _mqttConnection_t ) )\r
110     {\r
111         /* Find a free MQTT connection. */\r
112         freeIndex = IotStaticMemory_FindFree( _pInUseMqttConnections,\r
113                                               IOT_MQTT_CONNECTIONS );\r
114 \r
115         if( freeIndex != -1 )\r
116         {\r
117             pNewConnection = &( _pMqttConnections[ freeIndex ] );\r
118         }\r
119     }\r
120 \r
121     return pNewConnection;\r
122 }\r
123 \r
124 /*-----------------------------------------------------------*/\r
125 \r
126 void IotMqtt_FreeConnection( void * ptr )\r
127 {\r
128     /* Return the in-use MQTT connection. */\r
129     IotStaticMemory_ReturnInUse( ptr,\r
130                                  _pMqttConnections,\r
131                                  _pInUseMqttConnections,\r
132                                  IOT_MQTT_CONNECTIONS,\r
133                                  sizeof( _mqttConnection_t ) );\r
134 }\r
135 \r
136 /*-----------------------------------------------------------*/\r
137 \r
138 void * IotMqtt_MallocOperation( size_t size )\r
139 {\r
140     int32_t freeIndex = -1;\r
141     void * pNewOperation = NULL;\r
142 \r
143     /* Check size argument. */\r
144     if( size == sizeof( _mqttOperation_t ) )\r
145     {\r
146         /* Find a free MQTT operation. */\r
147         freeIndex = IotStaticMemory_FindFree( _pInUseMqttOperations,\r
148                                               IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS );\r
149 \r
150         if( freeIndex != -1 )\r
151         {\r
152             pNewOperation = &( _pMqttOperations[ freeIndex ] );\r
153         }\r
154     }\r
155 \r
156     return pNewOperation;\r
157 }\r
158 \r
159 /*-----------------------------------------------------------*/\r
160 \r
161 void IotMqtt_FreeOperation( void * ptr )\r
162 {\r
163     /* Return the in-use MQTT operation. */\r
164     IotStaticMemory_ReturnInUse( ptr,\r
165                                  _pMqttOperations,\r
166                                  _pInUseMqttOperations,\r
167                                  IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS,\r
168                                  sizeof( _mqttOperation_t ) );\r
169 }\r
170 \r
171 /*-----------------------------------------------------------*/\r
172 \r
173 void * IotMqtt_MallocSubscription( size_t size )\r
174 {\r
175     int32_t freeIndex = -1;\r
176     void * pNewSubscription = NULL;\r
177 \r
178     if( size <= MQTT_SUBSCRIPTION_SIZE )\r
179     {\r
180         /* Get the index of a free MQTT subscription. */\r
181         freeIndex = IotStaticMemory_FindFree( _pInUseMqttSubscriptions,\r
182                                               IOT_MQTT_SUBSCRIPTIONS );\r
183 \r
184         if( freeIndex != -1 )\r
185         {\r
186             pNewSubscription = &( _pMqttSubscriptions[ freeIndex ][ 0 ] );\r
187         }\r
188     }\r
189 \r
190     return pNewSubscription;\r
191 }\r
192 \r
193 /*-----------------------------------------------------------*/\r
194 \r
195 void IotMqtt_FreeSubscription( void * ptr )\r
196 {\r
197     /* Return the in-use MQTT subscription. */\r
198     IotStaticMemory_ReturnInUse( ptr,\r
199                                  _pMqttSubscriptions,\r
200                                  _pInUseMqttSubscriptions,\r
201                                  IOT_MQTT_SUBSCRIPTIONS,\r
202                                  MQTT_SUBSCRIPTION_SIZE );\r
203 }\r
204 \r
205 /*-----------------------------------------------------------*/\r
206 \r
207 #endif\r