]> git.sur5r.net Git - freertos/blob - FreeRTOS-Labs/Source/FreeRTOS-IoT-Libraries/c_sdk/aws/jobs/src/aws_iot_jobs_static_memory.c
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Source / FreeRTOS-IoT-Libraries / c_sdk / aws / jobs / src / aws_iot_jobs_static_memory.c
1 /*\r
2  * AWS IoT Jobs 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_jobs_static_memory.c\r
25  * @brief Implementation of Jobs static memory functions.\r
26  */\r
27 \r
28 /* The config header is always included first. */\r
29 #include "iot_config.h"\r
30 \r
31 /* This file should only be compiled if dynamic memory allocation is forbidden. */\r
32 #if IOT_STATIC_MEMORY_ONLY == 1\r
33 \r
34 /* Standard includes. */\r
35 #include <stdbool.h>\r
36 #include <stddef.h>\r
37 #include <string.h>\r
38 \r
39 /* Static memory include. */\r
40 #include "iot_static_memory.h"\r
41 \r
42 /* Jobs internal include. */\r
43 #include "private/aws_iot_jobs_internal.h"\r
44 \r
45 /*-----------------------------------------------------------*/\r
46 \r
47 /**\r
48  * @cond DOXYGEN_IGNORE\r
49  * Doxygen should ignore this section.\r
50  *\r
51  * Provide default values for undefined configuration constants.\r
52  */\r
53 #ifndef AWS_IOT_JOBS_MAX_IN_PROGRESS_OPERATIONS\r
54     #define AWS_IOT_JOBS_MAX_IN_PROGRESS_OPERATIONS    ( 10 )\r
55 #endif\r
56 #ifndef AWS_IOT_JOBS_SUBSCRIPTIONS\r
57     #define AWS_IOT_JOBS_SUBSCRIPTIONS                 ( 2 )\r
58 #endif\r
59 /** @endcond */\r
60 \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
64 #endif\r
65 #if AWS_IOT_JOBS_SUBSCRIPTIONS <= 0\r
66     #error "AWS_IOT_JOBS_SUBSCRIPTIONS cannot be 0 or negative."\r
67 #endif\r
68 \r
69 /**\r
70  * @brief The size of a static memory Jobs operation.\r
71  *\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
75  */\r
76 #define JOBS_OPERATION_SIZE    ( sizeof( _jobsOperation_t ) + JOBS_MAX_ID_LENGTH )\r
77 \r
78 /**\r
79  * @brief The size of a static memory Jobs subscription.\r
80  *\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
84  */\r
85 #define JOBS_SUBSCRIPTION_SIZE    ( sizeof( _jobsSubscription_t ) + AWS_IOT_MAX_THING_NAME_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 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
94 \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
97 \r
98 /*-----------------------------------------------------------*/\r
99 \r
100 void * AwsIotJobs_MallocOperation( size_t size )\r
101 {\r
102     int32_t freeIndex = -1;\r
103     void * pNewOperation = NULL;\r
104 \r
105     /* Check size argument. */\r
106     if( size <= JOBS_OPERATION_SIZE )\r
107     {\r
108         /* Find a free Jobs operation. */\r
109         freeIndex = IotStaticMemory_FindFree( _pInUseJobsOperations,\r
110                                               AWS_IOT_JOBS_MAX_IN_PROGRESS_OPERATIONS );\r
111 \r
112         if( freeIndex != -1 )\r
113         {\r
114             pNewOperation = &( _pJobsOperations[ freeIndex ] );\r
115         }\r
116     }\r
117 \r
118     return pNewOperation;\r
119 }\r
120 \r
121 /*-----------------------------------------------------------*/\r
122 \r
123 void AwsIotJobs_FreeOperation( void * ptr )\r
124 {\r
125     /* Return the in-use Jobs operation. */\r
126     IotStaticMemory_ReturnInUse( ptr,\r
127                                  _pJobsOperations,\r
128                                  _pInUseJobsOperations,\r
129                                  AWS_IOT_JOBS_MAX_IN_PROGRESS_OPERATIONS,\r
130                                  JOBS_OPERATION_SIZE );\r
131 }\r
132 \r
133 /*-----------------------------------------------------------*/\r
134 \r
135 void * AwsIotJobs_MallocSubscription( size_t size )\r
136 {\r
137     int32_t freeIndex = -1;\r
138     void * pNewSubscription = NULL;\r
139 \r
140     if( size <= JOBS_SUBSCRIPTION_SIZE )\r
141     {\r
142         /* Get the index of a free Jobs subscription. */\r
143         freeIndex = IotStaticMemory_FindFree( _pInUseJobsSubscriptions,\r
144                                               AWS_IOT_JOBS_SUBSCRIPTIONS );\r
145 \r
146         if( freeIndex != -1 )\r
147         {\r
148             pNewSubscription = &( _pJobsSubscriptions[ freeIndex ][ 0 ] );\r
149         }\r
150     }\r
151 \r
152     return pNewSubscription;\r
153 }\r
154 \r
155 /*-----------------------------------------------------------*/\r
156 \r
157 void AwsIotJobs_FreeSubscription( void * ptr )\r
158 {\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
165 }\r
166 \r
167 /*-----------------------------------------------------------*/\r
168 \r
169 #endif\r