2 * Amazon FreeRTOS MQTT V2.0.0
\r
3 * Copyright (C) 2018 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
22 * http://aws.amazon.com/freertos
\r
23 * http://www.FreeRTOS.org
\r
27 * @file iot_mqtt_static_memory.c
\r
28 * @brief Implementation of MQTT static memory functions.
\r
31 /* The config header is always included first. */
\r
32 #include "iot_config.h"
\r
34 /* This file should only be compiled if dynamic memory allocation is forbidden. */
\r
35 #if IOT_STATIC_MEMORY_ONLY == 1
\r
37 /* Standard includes. */
\r
38 #include <stdbool.h>
\r
42 /* Static memory include. */
\r
43 #include "private/iot_static_memory.h"
\r
45 /* MQTT internal include. */
\r
46 #include "private/iot_mqtt_internal.h"
\r
48 /*-----------------------------------------------------------*/
\r
51 * @cond DOXYGEN_IGNORE
\r
52 * Doxygen should ignore this section.
\r
54 * Provide default values for undefined configuration constants.
\r
56 #ifndef IOT_MQTT_CONNECTIONS
\r
57 #define IOT_MQTT_CONNECTIONS ( 1 )
\r
59 #ifndef IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS
\r
60 #define IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS ( 10 )
\r
62 #ifndef IOT_MQTT_SUBSCRIPTIONS
\r
63 #define IOT_MQTT_SUBSCRIPTIONS ( 8 )
\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
71 #if IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS <= 0
\r
72 #error "IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS cannot be 0 or negative."
\r
74 #if IOT_MQTT_SUBSCRIPTIONS <= 0
\r
75 #error "IOT_MQTT_SUBSCRIPTIONS cannot be 0 or negative."
\r
79 * @brief The size of a static memory MQTT subscription.
\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
85 #define MQTT_SUBSCRIPTION_SIZE ( sizeof( _mqttSubscription_t ) + AWS_IOT_MQTT_SERVER_MAX_TOPIC_LENGTH )
\r
87 /*-----------------------------------------------------------*/
\r
90 * Static memory buffers and flags, allocated and zeroed at compile-time.
\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
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
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
101 /*-----------------------------------------------------------*/
\r
103 void * IotMqtt_MallocConnection( size_t size )
\r
105 int32_t freeIndex = -1;
\r
106 void * pNewConnection = NULL;
\r
108 /* Check size argument. */
\r
109 if( size == sizeof( _mqttConnection_t ) )
\r
111 /* Find a free MQTT connection. */
\r
112 freeIndex = IotStaticMemory_FindFree( _pInUseMqttConnections,
\r
113 IOT_MQTT_CONNECTIONS );
\r
115 if( freeIndex != -1 )
\r
117 pNewConnection = &( _pMqttConnections[ freeIndex ] );
\r
121 return pNewConnection;
\r
124 /*-----------------------------------------------------------*/
\r
126 void IotMqtt_FreeConnection( void * ptr )
\r
128 /* Return the in-use MQTT connection. */
\r
129 IotStaticMemory_ReturnInUse( ptr,
\r
131 _pInUseMqttConnections,
\r
132 IOT_MQTT_CONNECTIONS,
\r
133 sizeof( _mqttConnection_t ) );
\r
136 /*-----------------------------------------------------------*/
\r
138 void * IotMqtt_MallocOperation( size_t size )
\r
140 int32_t freeIndex = -1;
\r
141 void * pNewOperation = NULL;
\r
143 /* Check size argument. */
\r
144 if( size == sizeof( _mqttOperation_t ) )
\r
146 /* Find a free MQTT operation. */
\r
147 freeIndex = IotStaticMemory_FindFree( _pInUseMqttOperations,
\r
148 IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS );
\r
150 if( freeIndex != -1 )
\r
152 pNewOperation = &( _pMqttOperations[ freeIndex ] );
\r
156 return pNewOperation;
\r
159 /*-----------------------------------------------------------*/
\r
161 void IotMqtt_FreeOperation( void * ptr )
\r
163 /* Return the in-use MQTT operation. */
\r
164 IotStaticMemory_ReturnInUse( ptr,
\r
166 _pInUseMqttOperations,
\r
167 IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS,
\r
168 sizeof( _mqttOperation_t ) );
\r
171 /*-----------------------------------------------------------*/
\r
173 void * IotMqtt_MallocSubscription( size_t size )
\r
175 int32_t freeIndex = -1;
\r
176 void * pNewSubscription = NULL;
\r
178 if( size <= MQTT_SUBSCRIPTION_SIZE )
\r
180 /* Get the index of a free MQTT subscription. */
\r
181 freeIndex = IotStaticMemory_FindFree( _pInUseMqttSubscriptions,
\r
182 IOT_MQTT_SUBSCRIPTIONS );
\r
184 if( freeIndex != -1 )
\r
186 pNewSubscription = &( _pMqttSubscriptions[ freeIndex ][ 0 ] );
\r
190 return pNewSubscription;
\r
193 /*-----------------------------------------------------------*/
\r
195 void IotMqtt_FreeSubscription( void * ptr )
\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
205 /*-----------------------------------------------------------*/
\r