summaryrefslogtreecommitdiff
path: root/FreeRTOS-Plus/Source/FreeRTOS-IoT-Libraries/c_sdk/standard/mqtt/src/iot_mqtt_static_memory.c
blob: 000fcbadce906378c416a123a34dbcc21c1f778c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * Amazon FreeRTOS MQTT V2.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.
 *
 * http://aws.amazon.com/freertos
 * http://www.FreeRTOS.org
 */

/**
 * @file iot_mqtt_static_memory.c
 * @brief Implementation of MQTT static memory functions.
 */

/* The config header is always included first. */
#include "iot_config.h"

/* This file should only be compiled if dynamic memory allocation is forbidden. */
#if IOT_STATIC_MEMORY_ONLY == 1

/* Standard includes. */
#include <stdbool.h>
#include <stddef.h>
#include <string.h>

/* Static memory include. */
#include "private/iot_static_memory.h"

/* MQTT internal include. */
#include "private/iot_mqtt_internal.h"

/*-----------------------------------------------------------*/

/**
 * @cond DOXYGEN_IGNORE
 * Doxygen should ignore this section.
 *
 * Provide default values for undefined configuration constants.
 */
#ifndef IOT_MQTT_CONNECTIONS
    #define IOT_MQTT_CONNECTIONS                   ( 1 )
#endif
#ifndef IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS
    #define IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS    ( 10 )
#endif
#ifndef IOT_MQTT_SUBSCRIPTIONS
    #define IOT_MQTT_SUBSCRIPTIONS                 ( 8 )
#endif
/** @endcond */

/* Validate static memory configuration settings. */
#if IOT_MQTT_CONNECTIONS <= 0
    #error "IOT_MQTT_CONNECTIONS cannot be 0 or negative."
#endif
#if IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS <= 0
    #error "IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS cannot be 0 or negative."
#endif
#if IOT_MQTT_SUBSCRIPTIONS <= 0
    #error "IOT_MQTT_SUBSCRIPTIONS cannot be 0 or negative."
#endif

/**
 * @brief The size of a static memory MQTT subscription.
 *
 * Since the pTopic member of #_mqttSubscription_t is variable-length, the constant
 * #AWS_IOT_MQTT_SERVER_MAX_TOPIC_LENGTH is used for the length of
 * #_mqttSubscription_t.pTopicFilter.
 */
#define MQTT_SUBSCRIPTION_SIZE    ( sizeof( _mqttSubscription_t ) + AWS_IOT_MQTT_SERVER_MAX_TOPIC_LENGTH )

/*-----------------------------------------------------------*/

/*
 * Static memory buffers and flags, allocated and zeroed at compile-time.
 */
static bool _pInUseMqttConnections[ IOT_MQTT_CONNECTIONS ] = { 0 };                               /**< @brief MQTT connection in-use flags. */
static _mqttConnection_t _pMqttConnections[ IOT_MQTT_CONNECTIONS ] = { { 0 } };                   /**< @brief MQTT connections. */

static bool _pInUseMqttOperations[ IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS ] = { 0 };                        /**< @brief MQTT operation in-use flags. */
static _mqttOperation_t _pMqttOperations[ IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS ] = { { .link = { 0 } } }; /**< @brief MQTT operations. */

static bool _pInUseMqttSubscriptions[ IOT_MQTT_SUBSCRIPTIONS ] = { 0 };                           /**< @brief MQTT subscription in-use flags. */
static char _pMqttSubscriptions[ IOT_MQTT_SUBSCRIPTIONS ][ MQTT_SUBSCRIPTION_SIZE ] = { { 0 } };  /**< @brief MQTT subscriptions. */

/*-----------------------------------------------------------*/

void * IotMqtt_MallocConnection( size_t size )
{
    int32_t freeIndex = -1;
    void * pNewConnection = NULL;

    /* Check size argument. */
    if( size == sizeof( _mqttConnection_t ) )
    {
        /* Find a free MQTT connection. */
        freeIndex = IotStaticMemory_FindFree( _pInUseMqttConnections,
                                              IOT_MQTT_CONNECTIONS );

        if( freeIndex != -1 )
        {
            pNewConnection = &( _pMqttConnections[ freeIndex ] );
        }
    }

    return pNewConnection;
}

/*-----------------------------------------------------------*/

void IotMqtt_FreeConnection( void * ptr )
{
    /* Return the in-use MQTT connection. */
    IotStaticMemory_ReturnInUse( ptr,
                                 _pMqttConnections,
                                 _pInUseMqttConnections,
                                 IOT_MQTT_CONNECTIONS,
                                 sizeof( _mqttConnection_t ) );
}

/*-----------------------------------------------------------*/

void * IotMqtt_MallocOperation( size_t size )
{
    int32_t freeIndex = -1;
    void * pNewOperation = NULL;

    /* Check size argument. */
    if( size == sizeof( _mqttOperation_t ) )
    {
        /* Find a free MQTT operation. */
        freeIndex = IotStaticMemory_FindFree( _pInUseMqttOperations,
                                              IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS );

        if( freeIndex != -1 )
        {
            pNewOperation = &( _pMqttOperations[ freeIndex ] );
        }
    }

    return pNewOperation;
}

/*-----------------------------------------------------------*/

void IotMqtt_FreeOperation( void * ptr )
{
    /* Return the in-use MQTT operation. */
    IotStaticMemory_ReturnInUse( ptr,
                                 _pMqttOperations,
                                 _pInUseMqttOperations,
                                 IOT_MQTT_MAX_IN_PROGRESS_OPERATIONS,
                                 sizeof( _mqttOperation_t ) );
}

/*-----------------------------------------------------------*/

void * IotMqtt_MallocSubscription( size_t size )
{
    int32_t freeIndex = -1;
    void * pNewSubscription = NULL;

    if( size <= MQTT_SUBSCRIPTION_SIZE )
    {
        /* Get the index of a free MQTT subscription. */
        freeIndex = IotStaticMemory_FindFree( _pInUseMqttSubscriptions,
                                              IOT_MQTT_SUBSCRIPTIONS );

        if( freeIndex != -1 )
        {
            pNewSubscription = &( _pMqttSubscriptions[ freeIndex ][ 0 ] );
        }
    }

    return pNewSubscription;
}

/*-----------------------------------------------------------*/

void IotMqtt_FreeSubscription( void * ptr )
{
    /* Return the in-use MQTT subscription. */
    IotStaticMemory_ReturnInUse( ptr,
                                 _pMqttSubscriptions,
                                 _pInUseMqttSubscriptions,
                                 IOT_MQTT_SUBSCRIPTIONS,
                                 MQTT_SUBSCRIPTION_SIZE );
}

/*-----------------------------------------------------------*/

#endif