AWS IoT Device Shadow  v1.1.0
AWS IoT Device Shadow client library
shadow.h
Go to the documentation of this file.
1 /*
2  * AWS IoT Device Shadow v1.1.0
3  * Copyright (C) 2020 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 SHADOW_H_
29 #define SHADOW_H_
30 
31 /* Standard includes. */
32 #include <stdint.h>
33 
34 /* SHADOW_DO_NOT_USE_CUSTOM_CONFIG allows building the Shadow library
35  * without a custom config. If a custom config is provided, the
36  * SHADOW_DO_NOT_USE_CUSTOM_CONFIG macro should not be defined. */
37 #ifndef SHADOW_DO_NOT_USE_CUSTOM_CONFIG
38  /* Include custom config file before other headers. */
39  #include "shadow_config.h"
40 #endif
41 
42 /* Include config defaults header to get default values of configs not
43  * defined in shadow_config_defaults.h file. */
44 #include "shadow_config_defaults.h"
45 
46 /*--------------------------- Shadow types ---------------------------*/
47 
53 typedef enum ShadowMessageType
54 {
55  ShadowMessageTypeGetAccepted = 0,
56  ShadowMessageTypeGetRejected,
57  ShadowMessageTypeDeleteAccepted,
58  ShadowMessageTypeDeleteRejected,
59  ShadowMessageTypeUpdateAccepted,
60  ShadowMessageTypeUpdateRejected,
61  ShadowMessageTypeUpdateDocuments,
62  ShadowMessageTypeUpdateDelta,
63  ShadowMessageTypeMaxNum
65 
73 typedef enum ShadowTopicStringType
74 {
75  ShadowTopicStringTypeGet = 0,
76  ShadowTopicStringTypeGetAccepted,
77  ShadowTopicStringTypeGetRejected,
78  ShadowTopicStringTypeDelete,
79  ShadowTopicStringTypeDeleteAccepted,
80  ShadowTopicStringTypeDeleteRejected,
81  ShadowTopicStringTypeUpdate,
82  ShadowTopicStringTypeUpdateAccepted,
83  ShadowTopicStringTypeUpdateRejected,
84  ShadowTopicStringTypeUpdateDocuments,
85  ShadowTopicStringTypeUpdateDelta,
86  ShadowTopicStringTypeMaxNum
88 
93 typedef enum ShadowStatus
94 {
104 
105 /*------------------------ Shadow library constants -------------------------*/
106 
112 #define SHADOW_PREFIX "$aws/things/"
113 
118 #define SHADOW_PREFIX_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_PREFIX ) - 1U ) )
119 
125 #define SHADOW_CLASSIC_ROOT "/shadow"
126 
131 #define SHADOW_CLASSIC_ROOT_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_CLASSIC_ROOT ) - 1U ) )
132 
138 #define SHADOW_NAMED_ROOT "/shadow/name/"
139 
144 #define SHADOW_NAMED_ROOT_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_NAMED_ROOT ) - 1U ) )
145 
150 #define SHADOW_OP_DELETE "/delete"
151 
156 #define SHADOW_OP_DELETE_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_OP_DELETE ) - 1U ) )
157 
162 #define SHADOW_OP_GET "/get"
163 
168 #define SHADOW_OP_GET_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_OP_GET ) - 1U ) )
169 
174 #define SHADOW_OP_UPDATE "/update"
175 
180 #define SHADOW_OP_UPDATE_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_OP_UPDATE ) - 1U ) )
181 
186 #define SHADOW_SUFFIX_ACCEPTED "/accepted"
187 
192 #define SHADOW_SUFFIX_ACCEPTED_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_SUFFIX_ACCEPTED ) - 1U ) )
193 
198 #define SHADOW_SUFFIX_REJECTED "/rejected"
199 
204 #define SHADOW_SUFFIX_REJECTED_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_SUFFIX_REJECTED ) - 1U ) )
205 
210 #define SHADOW_SUFFIX_DELTA "/delta"
211 
216 #define SHADOW_SUFFIX_DELTA_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_SUFFIX_DELTA ) - 1U ) )
217 
222 #define SHADOW_SUFFIX_DOCUMENTS "/documents"
223 
228 #define SHADOW_SUFFIX_DOCUMENTS_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_SUFFIX_DOCUMENTS ) - 1U ) )
229 
234 #define SHADOW_SUFFIX_NULL
235 
240 #define SHADOW_SUFFIX_NULL_LENGTH ( 0U )
241 
246 #define SHADOW_THINGNAME_LENGTH_MAX ( 128U )
247 
252 #define SHADOW_NAME_LENGTH_MAX ( 64U )
253 
258 #define SHADOW_NAME_CLASSIC ""
259 
264 #define SHADOW_NAME_CLASSIC_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_NAME_CLASSIC ) - 1U ) )
265 
313 #define SHADOW_TOPIC_LEN( operationLength, suffixLength, thingNameLength, shadowNameLength ) \
314  ( operationLength + suffixLength + thingNameLength + shadowNameLength + \
315  SHADOW_PREFIX_LENGTH + \
316  ( ( shadowNameLength > 0 ) ? SHADOW_NAMED_ROOT_LENGTH : SHADOW_CLASSIC_ROOT_LENGTH ) )
317 
328 #define SHADOW_TOPIC_LEN_UPDATE( thingNameLength, shadowNameLength ) \
329  SHADOW_TOPIC_LEN( SHADOW_OP_UPDATE_LENGTH, SHADOW_SUFFIX_NULL_LENGTH, thingNameLength, shadowNameLength )
330 
341 #define SHADOW_TOPIC_LEN_UPDATE_ACC( thingNameLength, shadowNameLength ) \
342  SHADOW_TOPIC_LEN( SHADOW_OP_UPDATE_LENGTH, SHADOW_SUFFIX_ACCEPTED_LENGTH, thingNameLength, shadowNameLength )
343 
354 #define SHADOW_TOPIC_LEN_UPDATE_REJ( thingNameLength, shadowNameLength ) \
355  SHADOW_TOPIC_LEN( SHADOW_OP_UPDATE_LENGTH, SHADOW_SUFFIX_REJECTED_LENGTH, thingNameLength, shadowNameLength )
356 
367 #define SHADOW_TOPIC_LEN_UPDATE_DOCS( thingNameLength, shadowNameLength ) \
368  SHADOW_TOPIC_LEN( SHADOW_OP_UPDATE_LENGTH, SHADOW_SUFFIX_DOCUMENTS_LENGTH, thingNameLength, shadowNameLength )
369 
380 #define SHADOW_TOPIC_LEN_UPDATE_DELTA( thingNameLength, shadowNameLength ) \
381  SHADOW_TOPIC_LEN( SHADOW_OP_UPDATE_LENGTH, SHADOW_SUFFIX_DELTA_LENGTH, thingNameLength, shadowNameLength )
382 
393 #define SHADOW_TOPIC_LEN_GET( thingNameLength, shadowNameLength ) \
394  SHADOW_TOPIC_LEN( SHADOW_OP_GET_LENGTH, SHADOW_SUFFIX_NULL_LENGTH, thingNameLength, shadowNameLength )
395 
406 #define SHADOW_TOPIC_LEN_GET_ACC( thingNameLength, shadowNameLength ) \
407  SHADOW_TOPIC_LEN( SHADOW_OP_GET_LENGTH, SHADOW_SUFFIX_ACCEPTED_LENGTH, thingNameLength, shadowNameLength )
408 
419 #define SHADOW_TOPIC_LEN_GET_REJ( thingNameLength, shadowNameLength ) \
420  SHADOW_TOPIC_LEN( SHADOW_OP_GET_LENGTH, SHADOW_SUFFIX_REJECTED_LENGTH, thingNameLength, shadowNameLength )
421 
432 #define SHADOW_TOPIC_LEN_DELETE( thingNameLength, shadowNameLength ) \
433  SHADOW_TOPIC_LEN( SHADOW_OP_DELETE_LENGTH, SHADOW_SUFFIX_NULL_LENGTH, thingNameLength, shadowNameLength )
434 
445 #define SHADOW_TOPIC_LEN_DELETE_ACC( thingNameLength, shadowNameLength ) \
446  SHADOW_TOPIC_LEN( SHADOW_OP_DELETE_LENGTH, SHADOW_SUFFIX_ACCEPTED_LENGTH, thingNameLength, shadowNameLength )
447 
458 #define SHADOW_TOPIC_LEN_DELETE_REJ( thingNameLength, shadowNameLength ) \
459  SHADOW_TOPIC_LEN( SHADOW_OP_DELETE_LENGTH, SHADOW_SUFFIX_REJECTED_LENGTH, thingNameLength, shadowNameLength )
460 
470 #define SHADOW_TOPIC_LEN_MAX( thingNameLength, shadowNameLength ) \
471  SHADOW_TOPIC_LEN( SHADOW_OP_UPDATE_LENGTH, SHADOW_SUFFIX_DOCUMENTS_LENGTH, thingNameLength, shadowNameLength )
472 
500 #define SHADOW_TOPIC_STR( thingName, shadowName, operation, suffix ) \
501  ( ( sizeof( shadowName ) > 1 ) ? \
502  ( SHADOW_PREFIX thingName SHADOW_NAMED_ROOT shadowName operation suffix ) : \
503  ( SHADOW_PREFIX thingName SHADOW_CLASSIC_ROOT operation suffix ) )
504 
515 #define SHADOW_TOPIC_STR_UPDATE( thingName, shadowName ) \
516  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_UPDATE, SHADOW_SUFFIX_NULL )
517 
528 #define SHADOW_TOPIC_STR_UPDATE_ACC( thingName, shadowName ) \
529  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_UPDATE, SHADOW_SUFFIX_ACCEPTED )
530 
541 #define SHADOW_TOPIC_STR_UPDATE_REJ( thingName, shadowName ) \
542  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_UPDATE, SHADOW_SUFFIX_REJECTED )
543 
554 #define SHADOW_TOPIC_STR_UPDATE_DOCS( thingName, shadowName ) \
555  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_UPDATE, SHADOW_SUFFIX_DOCUMENTS )
556 
567 #define SHADOW_TOPIC_STR_UPDATE_DELTA( thingName, shadowName ) \
568  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_UPDATE, SHADOW_SUFFIX_DELTA )
569 
580 #define SHADOW_TOPIC_STR_GET( thingName, shadowName ) \
581  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_GET, SHADOW_SUFFIX_NULL )
582 
593 #define SHADOW_TOPIC_STR_GET_ACC( thingName, shadowName ) \
594  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_GET, SHADOW_SUFFIX_ACCEPTED )
595 
606 #define SHADOW_TOPIC_STR_GET_REJ( thingName, shadowName ) \
607  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_GET, SHADOW_SUFFIX_REJECTED )
608 
619 #define SHADOW_TOPIC_STR_DELETE( thingName, shadowName ) \
620  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_DELETE, SHADOW_SUFFIX_NULL )
621 
632 #define SHADOW_TOPIC_STR_DELETE_ACC( thingName, shadowName ) \
633  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_DELETE, SHADOW_SUFFIX_ACCEPTED )
634 
645 #define SHADOW_TOPIC_STR_DELETE_REJ( thingName, shadowName ) \
646  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_DELETE, SHADOW_SUFFIX_REJECTED )
647 
648 /*------------------------ Shadow library functions -------------------------*/
649 
715 /* @[declare_shadow_assembletopicstring] */
717  const char * pThingName,
718  uint8_t thingNameLength,
719  const char * pShadowName,
720  uint8_t shadowNameLength,
721  char * pTopicBuffer,
722  uint16_t bufferSize,
723  uint16_t * pOutLength );
724 /* @[declare_shadow_assembletopicstring] */
725 
787 /* @[declare_shadow_matchtopicstring] */
788 ShadowStatus_t Shadow_MatchTopicString( const char * pTopic,
789  uint16_t topicLength,
790  ShadowMessageType_t * pMessageType,
791  const char ** pThingName,
792  uint8_t * pThingNameLength,
793  const char ** pShadowName,
794  uint8_t * pShadowNameLength );
795 /* @[declare_shadow_matchtopicstring] */
796 
797 /*------------- Shadow library backwardly-compatible constants -------------*/
798 
805 #define SHADOW_TOPIC_LENGTH( operationLength, suffixLength, thingNameLength ) \
806  SHADOW_TOPIC_LEN( operationLength, suffixLength, thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
807 
814 #define SHADOW_TOPIC_LENGTH_UPDATE( thingNameLength ) \
815  SHADOW_TOPIC_LEN_UPDATE( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
816 
823 #define SHADOW_TOPIC_LENGTH_UPDATE_ACCEPTED( thingNameLength ) \
824  SHADOW_TOPIC_LEN_UPDATE_ACC( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
825 
832 #define SHADOW_TOPIC_LENGTH_UPDATE_REJECTED( thingNameLength ) \
833  SHADOW_TOPIC_LEN_UPDATE_REJ( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
834 
841 #define SHADOW_TOPIC_LENGTH_UPDATE_DOCUMENTS( thingNameLength ) \
842  SHADOW_TOPIC_LEN_UPDATE_DOCS( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
843 
850 #define SHADOW_TOPIC_LENGTH_UPDATE_DELTA( thingNameLength ) \
851  SHADOW_TOPIC_LEN_UPDATE_DELTA( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
852 
859 #define SHADOW_TOPIC_LENGTH_GET( thingNameLength ) \
860  SHADOW_TOPIC_LEN_GET( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
861 
868 #define SHADOW_TOPIC_LENGTH_GET_ACCEPTED( thingNameLength ) \
869  SHADOW_TOPIC_LEN_GET_ACC( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
870 
877 #define SHADOW_TOPIC_LENGTH_GET_REJECTED( thingNameLength ) \
878  SHADOW_TOPIC_LEN_GET_REJ( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
879 
886 #define SHADOW_TOPIC_LENGTH_DELETE( thingNameLength ) \
887  SHADOW_TOPIC_LEN_DELETE( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
888 
895 #define SHADOW_TOPIC_LENGTH_DELETE_ACCEPTED( thingNameLength ) \
896  SHADOW_TOPIC_LEN_DELETE_ACC( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
897 
904 #define SHADOW_TOPIC_LENGTH_DELETE_REJECTED( thingNameLength ) \
905  SHADOW_TOPIC_LEN_DELETE_REJ( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
906 
913 #define SHADOW_TOPIC_LENGTH_MAX( thingNameLength ) \
914  SHADOW_TOPIC_LEN_MAX( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
915 
922 #define SHADOW_TOPIC_STRING( thingName, operation, suffix ) \
923  SHADOW_TOPIC_STR( thingName, SHADOW_NAME_CLASSIC, operation, suffix )
924 
931 #define SHADOW_TOPIC_STRING_UPDATE( thingName ) \
932  SHADOW_TOPIC_STR_UPDATE( thingName, SHADOW_NAME_CLASSIC )
933 
940 #define SHADOW_TOPIC_STRING_UPDATE_ACCEPTED( thingName ) \
941  SHADOW_TOPIC_STR_UPDATE_ACC( thingName, SHADOW_NAME_CLASSIC )
942 
949 #define SHADOW_TOPIC_STRING_UPDATE_REJECTED( thingName ) \
950  SHADOW_TOPIC_STR_UPDATE_REJ( thingName, SHADOW_NAME_CLASSIC )
951 
958 #define SHADOW_TOPIC_STRING_UPDATE_DOCUMENTS( thingName ) \
959  SHADOW_TOPIC_STR_UPDATE_DOCS( thingName, SHADOW_NAME_CLASSIC )
960 
967 #define SHADOW_TOPIC_STRING_UPDATE_DELTA( thingName ) \
968  SHADOW_TOPIC_STR_UPDATE_DELTA( thingName, SHADOW_NAME_CLASSIC )
969 
976 #define SHADOW_TOPIC_STRING_GET( thingName ) \
977  SHADOW_TOPIC_STR_GET( thingName, SHADOW_NAME_CLASSIC )
978 
985 #define SHADOW_TOPIC_STRING_GET_ACCEPTED( thingName ) \
986  SHADOW_TOPIC_STR_GET_ACC( thingName, SHADOW_NAME_CLASSIC )
987 
994 #define SHADOW_TOPIC_STRING_GET_REJECTED( thingName ) \
995  SHADOW_TOPIC_STR_GET_REJ( thingName, SHADOW_NAME_CLASSIC )
996 
1003 #define SHADOW_TOPIC_STRING_DELETE( thingName ) \
1004  SHADOW_TOPIC_STR_DELETE( thingName, SHADOW_NAME_CLASSIC )
1005 
1012 #define SHADOW_TOPIC_STRING_DELETE_ACCEPTED( thingName ) \
1013  SHADOW_TOPIC_STR_DELETE_ACC( thingName, SHADOW_NAME_CLASSIC )
1014 
1021 #define SHADOW_TOPIC_STRING_DELETE_REJECTED( thingName ) \
1022  SHADOW_TOPIC_STR_DELETE_REJ( thingName, SHADOW_NAME_CLASSIC )
1023 
1033 /* @[declare_shadow_gettopicstring] */
1034 #define Shadow_GetTopicString( topicType, pThingName, thingNameLength, pTopicBuffer, bufferSize, pOutLength ) \
1035  Shadow_AssembleTopicString( topicType, pThingName, thingNameLength, SHADOW_NAME_CLASSIC, 0, \
1036  pTopicBuffer, bufferSize, pOutLength )
1037 /* @[declare_shadow_gettopicstring] */
1038 
1050 /* @[declare_shadow_matchtopic] */
1051 ShadowStatus_t Shadow_MatchTopic( const char * pTopic,
1052  uint16_t topicLength,
1053  ShadowMessageType_t * pMessageType,
1054  const char ** pThingName,
1055  uint16_t * pThingNameLength );
1056 /* @[declare_shadow_matchtopic] */
1057 
1058 #endif /* ifndef SHADOW_H_ */
Shadow_AssembleTopicString
ShadowStatus_t Shadow_AssembleTopicString(ShadowTopicStringType_t topicType, const char *pThingName, uint8_t thingNameLength, const char *pShadowName, uint8_t shadowNameLength, char *pTopicBuffer, uint16_t bufferSize, uint16_t *pOutLength)
Assemble shadow topic string when Thing Name or Shadow Name is only known at run time....
Definition: shadow.c:873
shadow_config_defaults.h
This represents the default values for the configuration macros for the Shadow library.
SHADOW_SHADOWNAME_PARSE_FAILED
@ SHADOW_SHADOWNAME_PARSE_FAILED
Could not parse the shadow name (in the case of a named shadow topic).
Definition: shadow.h:102
SHADOW_THINGNAME_PARSE_FAILED
@ SHADOW_THINGNAME_PARSE_FAILED
Could not parse the thing name.
Definition: shadow.h:99
SHADOW_ROOT_PARSE_FAILED
@ SHADOW_ROOT_PARSE_FAILED
Could not parse the classic or named shadow root.
Definition: shadow.h:101
ShadowMessageType_t
ShadowMessageType_t
Each of these values describes the type of a shadow message. https://docs.aws.amazon....
Definition: shadow.h:54
SHADOW_BUFFER_TOO_SMALL
@ SHADOW_BUFFER_TOO_SMALL
The provided buffer is too small.
Definition: shadow.h:98
ShadowTopicStringType_t
ShadowTopicStringType_t
Each of these values describes the type of a shadow topic string.
Definition: shadow.h:74
ShadowStatus_t
ShadowStatus_t
Return codes from Shadow functions.
Definition: shadow.h:94
Shadow_MatchTopicString
ShadowStatus_t Shadow_MatchTopicString(const char *pTopic, uint16_t topicLength, ShadowMessageType_t *pMessageType, const char **pThingName, uint8_t *pThingNameLength, const char **pShadowName, uint8_t *pShadowNameLength)
Given the topic string of an incoming message, determine whether it is related to a device shadow; if...
Definition: shadow.c:764
SHADOW_MESSAGE_TYPE_PARSE_FAILED
@ SHADOW_MESSAGE_TYPE_PARSE_FAILED
Could not parse the shadow type.
Definition: shadow.h:100
SHADOW_BAD_PARAMETER
@ SHADOW_BAD_PARAMETER
Input parameter is invalid.
Definition: shadow.h:97
Shadow_MatchTopic
ShadowStatus_t Shadow_MatchTopic(const char *pTopic, uint16_t topicLength, ShadowMessageType_t *pMessageType, const char **pThingName, uint16_t *pThingNameLength)
Given the topic string of an incoming message, determine whether it is related to an unnamed ("Classi...
Definition: shadow.c:928
SHADOW_FAIL
@ SHADOW_FAIL
Shadow function encountered error.
Definition: shadow.h:96
SHADOW_SUCCESS
@ SHADOW_SUCCESS
Shadow function success.
Definition: shadow.h:95