AWS IoT Device SDK C: Defender
AWS IoT Device Defender library
Return to main page ↑
aws_iot_defender_internal.h
1 /*
2  * AWS IoT Defender V3.0.0
3  * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
28 #ifndef AWS_IOT_DEFENDER_INTERNAL_H_
29 #define AWS_IOT_DEFENDER_INTERNAL_H_
30 
31 /* The config header is always included first. */
32 #include "iot_config.h"
33 
34 /* Defender include. */
35 #include "aws_iot_defender.h"
36 
37 /* Serializer include. */
38 #include "iot_serializer.h"
39 
40 /* Platform thread include. */
41 #include "platform/iot_threads.h"
42 
43 /* Double linked list include. */
44 #include "iot_linear_containers.h"
45 
55 #if AWS_IOT_DEFENDER_ENABLE_ASSERTS == 1
56  #ifndef AwsIotDefender_Assert
57  #ifdef Iot_DefaultAssert
58  #define AwsIotDefender_Assert( expression ) Iot_DefaultAssert( expression )
59  #else
60  #error "Asserts are enabled for Defender, but AwsIotDefender_Assert is not defined"
61  #endif
62  #endif
63 #else /* if AWS_IOT_DEFENDER_ENABLE_ASSERTS == 1 */
64  #define AwsIotDefender_Assert( expression )
65 #endif /* if AWS_IOT_DEFENDER_ENABLE_ASSERTS == 1 */
66 
67 /* Configure logs for Defender functions. */
68 #ifdef AWS_IOT_LOG_LEVEL_DEFENDER
69  #define LIBRARY_LOG_LEVEL AWS_IOT_LOG_LEVEL_DEFENDER
70 #else
71  #ifdef AWS_IOT_LOG_LEVEL_GLOBAL
72  #define LIBRARY_LOG_LEVEL IOT_LOG_LEVEL_GLOBAL
73  #else
74  #define LIBRARY_LOG_LEVEL IOT_LOG_NONE
75  #endif
76 #endif
77 
78 #define LIBRARY_LOG_NAME ( "Defender" )
79 #include "iot_logging_setup.h"
80 
81 /*
82  * Provide default values for undefined memory allocation functions based on
83  * the usage of dynamic memory allocation.
84  */
85 #if IOT_STATIC_MEMORY_ONLY == 1
86  #include "iot_static_memory.h"
87 
93  #define AwsIotDefender_MallocReport Iot_MallocMessageBuffer
94 
100  #define AwsIotDefender_FreeReport Iot_FreeMessageBuffer
101 
107  #define AwsIotDefender_MallocTopic Iot_MallocMessageBuffer
108 
114  #define AwsIotDefender_FreeTopic Iot_FreeMessageBuffer
115 #else /* if IOT_STATIC_MEMORY_ONLY */
116  #ifndef AwsIotDefender_MallocReport
117  #ifdef Iot_DefaultMalloc
118  #define AwsIotDefender_MallocReport Iot_DefaultMalloc
119  #else
120  #error "No malloc function defined for AwsIotDefender_MallocReport"
121  #endif
122  #endif
123 
124  #ifndef AwsIotDefender_FreeReport
125  #ifdef Iot_DefaultFree
126  #define AwsIotDefender_FreeReport Iot_DefaultFree
127  #else
128  #error "No free function defined for AwsIotDefender_FreeReport"
129  #endif
130  #endif
131 
132  #ifndef AwsIotDefender_MallocTopic
133  #ifdef Iot_DefaultMalloc
134  #define AwsIotDefender_MallocTopic Iot_DefaultMalloc
135  #else
136  #error "No malloc function defined for AwsIotDefender_MallocTopic"
137  #endif
138  #endif
139 
140  #ifndef AwsIotDefender_FreeTopic
141  #ifdef Iot_DefaultFree
142  #define AwsIotDefender_FreeTopic Iot_DefaultFree
143  #else
144  #error "No free function defined for AwsIotDefender_FreeTopic"
145  #endif
146  #endif
147 #endif /* if IOT_STATIC_MEMORY_ONLY */
148 
209 #ifndef AWS_IOT_DEFENDER_DEFAULT_PERIOD_SECONDS
210  #define AWS_IOT_DEFENDER_DEFAULT_PERIOD_SECONDS ( 300 )
211 #endif
212 
213 #ifndef AWS_IOT_DEFENDER_WAIT_SERVER_MAX_SECONDS
214  #define AWS_IOT_DEFENDER_WAIT_SERVER_MAX_SECONDS ( 3 )
215 #endif
216 
217 #ifndef AWS_IOT_DEFENDER_MQTT_CONNECT_TIMEOUT_SECONDS
218  #define AWS_IOT_DEFENDER_MQTT_CONNECT_TIMEOUT_SECONDS ( 10U )
219 #endif
220 
221 #ifndef AWS_IOT_DEFENDER_MQTT_SUBSCRIBE_TIMEOUT_SECONDS
222  #define AWS_IOT_DEFENDER_MQTT_SUBSCRIBE_TIMEOUT_SECONDS ( 10U )
223 #endif
224 
225 #ifndef AWS_IOT_DEFENDER_MQTT_PUBLISH_TIMEOUT_SECONDS
226  #define AWS_IOT_DEFENDER_MQTT_PUBLISH_TIMEOUT_SECONDS ( 10U )
227 #endif
228 
229 #ifndef AWS_IOT_DEFENDER_FORMAT
230  #define AWS_IOT_DEFENDER_FORMAT AWS_IOT_DEFENDER_FORMAT_CBOR
231 #endif
232 
233 /* In current release, JSON format is not supported. */
234 #if AWS_IOT_DEFENDER_FORMAT == AWS_IOT_DEFENDER_FORMAT_JSON
235  #error "AWS_IOT_DEFENDER_FORMAT_JSON is not supported."
236 #endif
237 
238 /* Default to short tag to save memory and network. */
239 #ifndef AWS_IOT_DEFENDER_USE_LONG_TAG
240  #define AWS_IOT_DEFENDER_USE_LONG_TAG ( 0 )
241 #endif
242 
243 /*----------------- Below this line is INTERNAL used only --------------------*/
244 
245 /* This MUST be consistent with enum AwsIotDefenderMetricsGroup_t. */
246 #define DEFENDER_METRICS_GROUP_COUNT 1
247 
251 #if AWS_IOT_DEFENDER_FORMAT == AWS_IOT_DEFENDER_FORMAT_CBOR
252  #define DEFENDER_FORMAT "cbor"
253 #else /* if AWS_IOT_DEFENDER_FORMAT == AWS_IOT_DEFENDER_FORMAT_CBOR */
254  #error "AWS_IOT_DEFENDER_FORMAT must be AWS_IOT_DEFENDER_FORMAT_CBOR."
255 #endif /* if AWS_IOT_DEFENDER_FORMAT == AWS_IOT_DEFENDER_FORMAT_CBOR */
256 
260 #if AWS_IOT_DEFENDER_USE_LONG_TAG == 1
261  #define AwsIotDefenderInternal_SelectTag( long_tag, short_tag ) ( long_tag )
262 #else
263  #define AwsIotDefenderInternal_SelectTag( long_tag, short_tag ) ( short_tag )
264 #endif
265 
269 #define _defenderToMilliseconds( secondValue ) ( secondValue ) * 1000
270 #define _defenderToSeconds( millisecondValue ) \
271  ( millisecondValue ) / 1000 \
272 
273 
277 typedef struct _defenderMetrics
278 {
282  uint32_t metricsFlag[ DEFENDER_METRICS_GROUP_COUNT ];
283 
291 
295 bool AwsIotDefenderInternal_CreateReport( void );
296 
300 uint8_t * AwsIotDefenderInternal_GetReportBuffer( void );
301 
305 size_t AwsIotDefenderInternal_GetReportBufferSize( void );
306 
310 void AwsIotDefenderInternal_DeleteReport( void );
311 
315 AwsIotDefenderError_t AwsIotDefenderInternal_BuildTopicsNames( void );
316 
320 void AwsIotDefenderInternal_DeleteTopicsNames( void );
321 
326 IotMqttError_t AwsIotDefenderInternal_MqttSubscribe( void );
327 
331 IotMqttError_t AwsIotDefenderInternal_MqttPublish( uint8_t * pData,
332  size_t dataLength );
333 
337 IotMqttError_t AwsIotDefenderInternal_MqttUnSubscribe( void );
338 
342 void AwsIotDefenderInternal_MqttDisconnect( void );
343 
344 /*----------------- Below this line are INTERNAL global variables --------------------*/
345 
346 extern _defenderMetrics_t _AwsIotDefenderMetrics;
347 
348 extern const IotSerializerEncodeInterface_t * _pAwsIotDefenderEncoder;
349 extern const IotSerializerDecodeInterface_t * _pAwsIotDefenderDecoder;
350 
351 #endif /* ifndef AWS_IOT_DEFENDER_INTERNAL_H_ */
IotMqttError_t
User-facing functions and structs of AWS IoT Device Defender library.
AwsIotDefenderError_t
Return codes of defender functions.
Definition: aws_iot_defender.h:139
IotMutex_t mutex
Definition: aws_iot_defender_internal.h:289
Definition: aws_iot_defender_internal.h:277
_IotSystemMutex_t IotMutex_t