2 * AWS IoT Jobs 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_jobs_static_memory.c
\r
25 * @brief Implementation of Jobs static memory functions.
\r
28 /* The config header is always included first. */
\r
29 #include "iot_config.h"
\r
31 /* This file should only be compiled if dynamic memory allocation is forbidden. */
\r
32 #if IOT_STATIC_MEMORY_ONLY == 1
\r
34 /* Standard includes. */
\r
35 #include <stdbool.h>
\r
39 /* Static memory include. */
\r
40 #include "iot_static_memory.h"
\r
42 /* Jobs internal include. */
\r
43 #include "private/aws_iot_jobs_internal.h"
\r
45 /*-----------------------------------------------------------*/
\r
48 * @cond DOXYGEN_IGNORE
\r
49 * Doxygen should ignore this section.
\r
51 * Provide default values for undefined configuration constants.
\r
53 #ifndef AWS_IOT_JOBS_MAX_IN_PROGRESS_OPERATIONS
\r
54 #define AWS_IOT_JOBS_MAX_IN_PROGRESS_OPERATIONS ( 10 )
\r
56 #ifndef AWS_IOT_JOBS_SUBSCRIPTIONS
\r
57 #define AWS_IOT_JOBS_SUBSCRIPTIONS ( 2 )
\r
61 /* Validate static memory configuration settings. */
\r
62 #if AWS_IOT_JOBS_MAX_IN_PROGRESS_OPERATIONS <= 0
\r
63 #error "AWS_IOT_JOBS_MAX_IN_PROGRESS_OPERATIONS cannot be 0 or negative."
\r
65 #if AWS_IOT_JOBS_SUBSCRIPTIONS <= 0
\r
66 #error "AWS_IOT_JOBS_SUBSCRIPTIONS cannot be 0 or negative."
\r
70 * @brief The size of a static memory Jobs operation.
\r
72 * Since the pJobId member of #_jobsOperation_t is variable-length,
\r
73 * the constant `JOBS_MAX_ID_LENGTH` is used for the length of
\r
74 * #_jobsOperation_t.pJobId.
\r
76 #define JOBS_OPERATION_SIZE ( sizeof( _jobsOperation_t ) + JOBS_MAX_ID_LENGTH )
\r
79 * @brief The size of a static memory Jobs subscription.
\r
81 * Since the pThingName member of #_jobsSubscription_t is variable-length,
\r
82 * the constant `AWS_IOT_MAX_THING_NAME_LENGTH` is used for the length of
\r
83 * #_jobsSubscription_t.pThingName.
\r
85 #define JOBS_SUBSCRIPTION_SIZE ( sizeof( _jobsSubscription_t ) + AWS_IOT_MAX_THING_NAME_LENGTH )
\r
87 /*-----------------------------------------------------------*/
\r
90 * Static memory buffers and flags, allocated and zeroed at compile-time.
\r
92 static uint32_t _pInUseJobsOperations[ AWS_IOT_JOBS_MAX_IN_PROGRESS_OPERATIONS ] = { 0U }; /**< @brief Jobs operation in-use flags. */
\r
93 static char _pJobsOperations[ AWS_IOT_JOBS_MAX_IN_PROGRESS_OPERATIONS ][ JOBS_OPERATION_SIZE ] = { { 0 } }; /**< @brief Jobs operations. */
\r
95 static uint32_t _pInUseJobsSubscriptions[ AWS_IOT_JOBS_SUBSCRIPTIONS ] = { 0U }; /**< @brief Jobs subscription in-use flags. */
\r
96 static char _pJobsSubscriptions[ AWS_IOT_JOBS_SUBSCRIPTIONS ][ JOBS_SUBSCRIPTION_SIZE ] = { { 0 } }; /**< @brief Jobs subscriptions. */
\r
98 /*-----------------------------------------------------------*/
\r
100 void * AwsIotJobs_MallocOperation( size_t size )
\r
102 int32_t freeIndex = -1;
\r
103 void * pNewOperation = NULL;
\r
105 /* Check size argument. */
\r
106 if( size <= JOBS_OPERATION_SIZE )
\r
108 /* Find a free Jobs operation. */
\r
109 freeIndex = IotStaticMemory_FindFree( _pInUseJobsOperations,
\r
110 AWS_IOT_JOBS_MAX_IN_PROGRESS_OPERATIONS );
\r
112 if( freeIndex != -1 )
\r
114 pNewOperation = &( _pJobsOperations[ freeIndex ] );
\r
118 return pNewOperation;
\r
121 /*-----------------------------------------------------------*/
\r
123 void AwsIotJobs_FreeOperation( void * ptr )
\r
125 /* Return the in-use Jobs operation. */
\r
126 IotStaticMemory_ReturnInUse( ptr,
\r
128 _pInUseJobsOperations,
\r
129 AWS_IOT_JOBS_MAX_IN_PROGRESS_OPERATIONS,
\r
130 JOBS_OPERATION_SIZE );
\r
133 /*-----------------------------------------------------------*/
\r
135 void * AwsIotJobs_MallocSubscription( size_t size )
\r
137 int32_t freeIndex = -1;
\r
138 void * pNewSubscription = NULL;
\r
140 if( size <= JOBS_SUBSCRIPTION_SIZE )
\r
142 /* Get the index of a free Jobs subscription. */
\r
143 freeIndex = IotStaticMemory_FindFree( _pInUseJobsSubscriptions,
\r
144 AWS_IOT_JOBS_SUBSCRIPTIONS );
\r
146 if( freeIndex != -1 )
\r
148 pNewSubscription = &( _pJobsSubscriptions[ freeIndex ][ 0 ] );
\r
152 return pNewSubscription;
\r
155 /*-----------------------------------------------------------*/
\r
157 void AwsIotJobs_FreeSubscription( void * ptr )
\r
159 /* Return the in-use Jobs subscription. */
\r
160 IotStaticMemory_ReturnInUse( ptr,
\r
161 _pJobsSubscriptions,
\r
162 _pInUseJobsSubscriptions,
\r
163 AWS_IOT_JOBS_SUBSCRIPTIONS,
\r
164 JOBS_SUBSCRIPTION_SIZE );
\r
167 /*-----------------------------------------------------------*/
\r