AWS IoT Device SDK C: Shadow
AWS IoT Device Shadow library
Return to main page ↑
aws_iot_shadow_internal.h
Go to the documentation of this file.
1 /*
2  * AWS IoT Shadow V2.1.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 
29 #ifndef AWS_IOT_SHADOW_INTERNAL_H_
30 #define AWS_IOT_SHADOW_INTERNAL_H_
31 
32 /* The config header is always included first. */
33 #include "iot_config.h"
34 
35 /* Linear containers (lists and queues) include. */
36 #include "iot_linear_containers.h"
37 
38 /* Platform layer types include. */
40 
41 /* Shadow include. */
42 #include "aws_iot_shadow.h"
43 
44 /* AWS IoT include. */
45 #include "aws_iot.h"
46 
56 #if AWS_IOT_SHADOW_ENABLE_ASSERTS == 1
57  #ifndef AwsIotShadow_Assert
58  #ifdef Iot_DefaultAssert
59  #define AwsIotShadow_Assert( expression ) Iot_DefaultAssert( expression )
60  #else
61  #error "Asserts are enabled for Shadow, but AwsIotShadow_Assert is not defined"
62  #endif
63  #endif
64 #else /* if AWS_IOT_SHADOW_ENABLE_ASSERTS == 1 */
65  #define AwsIotShadow_Assert( expression )
66 #endif /* if AWS_IOT_SHADOW_ENABLE_ASSERTS == 1 */
67 
68 /* Configure logs for Shadow functions. */
69 #ifdef AWS_IOT_LOG_LEVEL_SHADOW
70  #define LIBRARY_LOG_LEVEL AWS_IOT_LOG_LEVEL_SHADOW
71 #else
72  #ifdef IOT_LOG_LEVEL_GLOBAL
73  #define LIBRARY_LOG_LEVEL IOT_LOG_LEVEL_GLOBAL
74  #else
75  #define LIBRARY_LOG_LEVEL IOT_LOG_NONE
76  #endif
77 #endif
78 
79 #define LIBRARY_LOG_NAME ( "Shadow" )
80 #include "iot_logging_setup.h"
81 
82 /*
83  * Provide default values for undefined memory allocation functions based on
84  * the usage of dynamic memory allocation.
85  */
86 #if IOT_STATIC_MEMORY_ONLY == 1
87  #include "iot_static_memory.h"
88 
94  void * AwsIotShadow_MallocOperation( size_t size );
95 
101  void AwsIotShadow_FreeOperation( void * ptr );
102 
108  #define AwsIotShadow_MallocString Iot_MallocMessageBuffer
109 
115  #define AwsIotShadow_FreeString Iot_FreeMessageBuffer
116 
122  void * AwsIotShadow_MallocSubscription( size_t size );
123 
129  void AwsIotShadow_FreeSubscription( void * ptr );
130 #else /* if IOT_STATIC_MEMORY_ONLY == 1 */
131  #ifndef AwsIotShadow_MallocOperation
132  #ifdef Iot_DefaultMalloc
133  #define AwsIotShadow_MallocOperation Iot_DefaultMalloc
134  #else
135  #error "No malloc function defined for AwsIotShadow_MallocOperation"
136  #endif
137  #endif
138 
139  #ifndef AwsIotShadow_FreeOperation
140  #ifdef Iot_DefaultFree
141  #define AwsIotShadow_FreeOperation Iot_DefaultFree
142  #else
143  #error "No free function defined for AwsIotShadow_FreeOperation"
144  #endif
145  #endif
146 
147  #ifndef AwsIotShadow_MallocString
148  #ifdef Iot_DefaultMalloc
149  #define AwsIotShadow_MallocString Iot_DefaultMalloc
150  #else
151  #error "No malloc function defined for AwsIotShadow_MallocString"
152  #endif
153  #endif
154 
155  #ifndef AwsIotShadow_FreeString
156  #ifdef Iot_DefaultFree
157  #define AwsIotShadow_FreeString Iot_DefaultFree
158  #else
159  #error "No free function defined for AwsIotShadow_FreeString"
160  #endif
161  #endif
162 
163  #ifndef AwsIotShadow_MallocSubscription
164  #ifdef Iot_DefaultMalloc
165  #define AwsIotShadow_MallocSubscription Iot_DefaultMalloc
166  #else
167  #error "No malloc function defined for AwsIotShadow_MallocSubscription"
168  #endif
169  #endif
170 
171  #ifndef AwsIotShadow_FreeSubscription
172  #ifdef Iot_DefaultFree
173  #define AwsIotShadow_FreeSubscription Iot_DefaultFree
174  #else
175  #error "No free function defined for AwsIotShadow_FreeSubscription"
176  #endif
177  #endif
178 #endif /* if IOT_STATIC_MEMORY_ONLY == 1 */
179 
186 #ifndef AWS_IOT_SHADOW_DEFAULT_MQTT_TIMEOUT_MS
187  #define AWS_IOT_SHADOW_DEFAULT_MQTT_TIMEOUT_MS ( 5000 )
188 #endif
189 
196 #define SHADOW_OPERATION_COUNT ( 3U )
197 
204 #define SHADOW_CALLBACK_COUNT ( 2 )
205 
209 #define SHADOW_DELETE_OPERATION_STRING "/shadow/delete"
210 
214 #define SHADOW_DELETE_OPERATION_STRING_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_DELETE_OPERATION_STRING ) - 1U ) )
215 
219 #define SHADOW_GET_OPERATION_STRING "/shadow/get"
220 
224 #define SHADOW_GET_OPERATION_STRING_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_GET_OPERATION_STRING ) - 1U ) )
225 
229 #define SHADOW_UPDATE_OPERATION_STRING "/shadow/update"
230 
234 #define SHADOW_UPDATE_OPERATION_STRING_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_UPDATE_OPERATION_STRING ) - 1U ) )
235 
239 #define SHADOW_DELTA_SUFFIX "/delta"
240 
244 #define SHADOW_DELTA_SUFFIX_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_DELTA_SUFFIX ) - 1U ) )
245 
249 #define SHADOW_UPDATED_SUFFIX "/documents"
250 
254 #define SHADOW_UPDATED_SUFFIX_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_UPDATED_SUFFIX ) - 1U ) )
255 
259 #define SHADOW_LONGEST_SUFFIX_LENGTH SHADOW_UPDATED_SUFFIX_LENGTH
260 
268 #define SHADOW_CONVERT_STATUS_CODE_MQTT_TO_SHADOW( X ) \
269  ( ( X ) == IOT_MQTT_SUCCESS ) ? AWS_IOT_SHADOW_SUCCESS : \
270  ( ( X ) == IOT_MQTT_NO_MEMORY ) ? AWS_IOT_SHADOW_NO_MEMORY : \
271  AWS_IOT_SHADOW_MQTT_ERROR
272 
273 /*----------------------- Shadow internal data types ------------------------*/
274 
278 typedef enum _shadowOperationType
279 {
280  /* Shadow operations. */
285  /* Shadow callbacks. */
289 
293 typedef enum _shadowCallbackType
294 {
298 
304 typedef struct _shadowSubscription
305 {
308  int32_t references[ SHADOW_OPERATION_COUNT ];
317  char * pTopicBuffer;
318 
320  char pThingName[];
322 
329 typedef struct _shadowOperation
330 {
333  /* Basic operation information. */
335  uint32_t flags;
341  union
342  {
343  /* Members valid only for a GET operation. */
344  struct
345  {
351  void *( *mallocDocument )( size_t );
352 
353  const char * pDocument;
354  size_t documentLength;
355  } get;
356 
357  /* Members valid only for an UPDATE operation. */
358  struct
359  {
360  const char * pClientToken;
362  } update;
363  } u;
365  /* How to notify of an operation's completion. */
366  union
367  {
370  } notify;
372 
373 /* Declarations of names printed in logs. */
374 #if LIBRARY_LOG_LEVEL > IOT_LOG_NONE
375  extern const char * const _pAwsIotShadowOperationNames[];
376  extern const char * const _pAwsIotShadowCallbackNames[];
377 #endif
378 
379 /* Declarations of variables for internal Shadow files. */
380 extern uint32_t _AwsIotShadowMqttTimeoutMs;
385 
386 /*----------------------- Shadow operation functions ------------------------*/
387 
400  uint32_t flags,
401  const AwsIotShadowCallbackInfo_t * pCallbackInfo );
402 
411 void _AwsIotShadow_DestroyOperation( void * pData );
412 
433  const char * pThingName,
434  size_t thingNameLength,
435  char ** pTopicBuffer,
436  uint16_t * pOperationTopicLength );
437 
452  const char * pThingName,
453  size_t thingNameLength,
454  _shadowOperation_t * pOperation,
455  const AwsIotShadowDocumentInfo_t * pDocumentInfo );
456 
457 
458 
467 
476 
477 /*---------------------- Shadow subscription functions ----------------------*/
478 
494 _shadowSubscription_t * _AwsIotShadow_FindSubscription( const char * pThingName,
495  size_t thingNameLength,
496  bool createIfNotFound );
497 
511  _shadowSubscription_t ** pRemovedSubscription );
512 
520 void _AwsIotShadow_DestroySubscription( void * pData );
521 
544  char * pTopicBuffer,
545  uint16_t operationTopicLength,
546  AwsIotMqttCallbackFunction_t callback );
547 
568  char * pTopicBuffer,
569  _shadowSubscription_t ** pRemovedSubscription );
570 
571 /*------------------------- Shadow parser functions -------------------------*/
572 
582 AwsIotShadowError_t _AwsIotShadow_ParseErrorDocument( const char * pErrorDocument,
583  size_t errorDocumentLength );
584 
585 #endif /* ifndef AWS_IOT_SHADOW_INTERNAL_H_ */
Represents a Shadow subscriptions object.
Definition: aws_iot_shadow_internal.h:304
size_t clientTokenLength
Length of client token.
Definition: aws_iot_shadow_internal.h:361
Definition: aws_iot_shadow_internal.h:287
IotSemaphore_t waitSemaphore
Semaphore to be used with AwsIotShadow_Wait.
Definition: aws_iot_shadow_internal.h:368
void AwsIotShadow_FreeOperation(void *ptr)
Free a _shadowOperation_t. This function should have the same signature as free.
Definition: aws_iot_shadow_static_memory.c:114
AwsIotShadowError_t _AwsIotShadow_IncrementReferences(_shadowOperation_t *pOperation, char *pTopicBuffer, uint16_t operationTopicLength, AwsIotMqttCallbackFunction_t callback)
Increment the reference count of a Shadow subscriptions object.
Definition: aws_iot_shadow_subscription.c:270
Definition: aws_iot_shadow_internal.h:281
void _AwsIotShadow_DecrementReferences(_shadowOperation_t *pOperation, char *pTopicBuffer, _shadowSubscription_t **pRemovedSubscription)
Decrement the reference count of a Shadow subscriptions object.
Definition: aws_iot_shadow_subscription.c:345
void * AwsIotShadow_MallocSubscription(size_t size)
Allocate a _shadowSubscription_t. This function should have the same signature as malloc...
Definition: aws_iot_shadow_static_memory.c:126
_shadowOperationType_t type
Operation type.
Definition: aws_iot_shadow_internal.h:334
IotMutex_t _AwsIotShadowPendingOperationsMutex
Protects _AwsIotShadowPendingOperations from concurrent access.
Definition: aws_iot_shadow_operation.c:183
IotLink_t link
List link member.
Definition: aws_iot_shadow_internal.h:331
char * pTopicBuffer
Buffer allocated for removing Shadow topics.
Definition: aws_iot_shadow_internal.h:317
Definition: aws_iot_shadow_internal.h:295
_shadowSubscription_t * _AwsIotShadow_FindSubscription(const char *pThingName, size_t thingNameLength, bool createIfNotFound)
Find a Shadow subscription object. May create a new subscription object and adds it to the subscripti...
Definition: aws_iot_shadow_subscription.c:99
_shadowOperationType_t
Enumerations representing each of the Shadow library's API functions.
Definition: aws_iot_shadow_internal.h:278
const char * pDocument
Retrieved Shadow document.
Definition: aws_iot_shadow_internal.h:353
AwsIotShadowError_t _AwsIotShadow_ParseErrorDocument(const char *pErrorDocument, size_t errorDocumentLength)
Parse a Shadow error document.
Definition: aws_iot_shadow_parser.c:130
AwsIotShadowError_t
Return codes of Shadow functions.
Definition: aws_iot_shadow_types.h:88
_shadowSubscription_t * pSubscription
Shadow subscriptions object associated with this operation.
Definition: aws_iot_shadow_internal.h:339
Definition: aws_iot_shadow_internal.h:296
uint32_t _AwsIotShadowMqttTimeoutMs
Timeout used for MQTT operations.
Definition: aws_iot_shadow_api.c:168
IotListDouble_t _AwsIotShadowPendingOperations
List of active Shadow operations awaiting a response from the Shadow service.
Definition: aws_iot_shadow_operation.c:178
struct _mqttConnection * IotMqttConnection_t
User-facing functions of the Shadow library.
uint32_t flags
Flags passed to operation API function.
Definition: aws_iot_shadow_internal.h:335
Information on a user-provided Shadow callback function.
Definition: aws_iot_shadow_types.h:436
void _AwsIotShadow_DestroyOperation(void *pData)
Free resources used to record a Shadow operation. This is called when the operation completes...
Definition: aws_iot_shadow_operation.c:736
AwsIotShadowError_t _AwsIotShadow_CreateOperation(_shadowOperation_t **pNewOperation, _shadowOperationType_t type, uint32_t flags, const AwsIotShadowCallbackInfo_t *pCallbackInfo)
Create a record for a new in-progress Shadow operation.
Definition: aws_iot_shadow_operation.c:672
Definition: aws_iot_shadow_internal.h:283
Internal structure representing a single Shadow operation (DELETE, GET, or UPDATE).
Definition: aws_iot_shadow_internal.h:329
IotListDouble_t _AwsIotShadowSubscriptions
List of active Shadow subscriptions objects.
Definition: aws_iot_shadow_subscription.c:63
IotMutex_t _AwsIotShadowSubscriptionsMutex
Protects _AwsIotShadowSubscriptions from concurrent access.
Definition: aws_iot_shadow_subscription.c:68
const char * pClientToken
Client token in update document.
Definition: aws_iot_shadow_internal.h:360
#define SHADOW_OPERATION_COUNT
The number of currently available Shadow operations.
Definition: aws_iot_shadow_internal.h:196
Information on a Shadow document for AwsIotShadow_GetAsync or AwsIotShadow_UpdateAsync.
Definition: aws_iot_shadow_types.h:469
_IotSystemSemaphore_t IotSemaphore_t
AwsIotShadowError_t status
Status of operation.
Definition: aws_iot_shadow_internal.h:336
AwsIotShadowError_t _AwsIotShadow_GenerateShadowTopic(_shadowOperationType_t type, const char *pThingName, size_t thingNameLength, char **pTopicBuffer, uint16_t *pOperationTopicLength)
Fill a buffer with a Shadow topic.
Definition: aws_iot_shadow_operation.c:768
Definition: aws_iot_shadow_internal.h:282
size_t documentLength
Length of retrieved Shadow document.
Definition: aws_iot_shadow_internal.h:354
void _AwsIotShadow_DestroySubscription(void *pData)
Free resources used for a Shadow subscription object.
Definition: aws_iot_shadow_subscription.c:254
_IotSystemMutex_t IotMutex_t
AwsIotShadowError_t _AwsIotShadow_ProcessOperation(IotMqttConnection_t mqttConnection, const char *pThingName, size_t thingNameLength, _shadowOperation_t *pOperation, const AwsIotShadowDocumentInfo_t *pDocumentInfo)
Process a Shadow operation by sending the necessary MQTT packets.
Definition: aws_iot_shadow_operation.c:819
size_t thingNameLength
Length of Thing Name.
Definition: aws_iot_shadow_internal.h:319
Definition: aws_iot_shadow_internal.h:286
AwsIotShadowCallbackInfo_t callback
User-provided callback function and parameter.
Definition: aws_iot_shadow_internal.h:369
_shadowCallbackType_t
Enumerations representing each of the Shadow callback functions.
Definition: aws_iot_shadow_internal.h:293
IotMqttConnection_t mqttConnection
MQTT connection associated with this operation.
Definition: aws_iot_shadow_internal.h:338
_shadowOperationType_t _AwsIotShadow_IntToShadowOperationType(uint32_t n)
Convert an integer to the shadow operation type.
Definition: aws_iot_shadow_operation.c:187
AwsIotShadowCallbackType_t _AwsIotShadow_IntToShadowCallbackType(uint32_t n)
Convert an integer to the shadow callback type.
Definition: aws_iot_shadow_operation.c:223
void AwsIotShadow_FreeSubscription(void *ptr)
Free a _shadowSubscription_t. This function should have the same signature as free.
Definition: aws_iot_shadow_static_memory.c:148
#define SHADOW_CALLBACK_COUNT
The number of currently available Shadow callbacks.
Definition: aws_iot_shadow_internal.h:204
AwsIotShadowCallbackType_t
Types of Shadow library callbacks.
Definition: aws_iot_shadow_types.h:345
void _AwsIotShadow_RemoveSubscription(_shadowSubscription_t *pSubscription, _shadowSubscription_t **pRemovedSubscription)
Remove a Shadow subscription object from the subscription list if unreferenced.
Definition: aws_iot_shadow_subscription.c:163
IotLink_t link
List link member.
Definition: aws_iot_shadow_internal.h:306
void * AwsIotShadow_MallocOperation(size_t size)
Allocate a _shadowOperation_t. This function should have the same signature as malloc.
Definition: aws_iot_shadow_static_memory.c:91