AWS IoT Device Shadow  v1.1.1
AWS IoT Device Shadow client library
shadow.h
Go to the documentation of this file.
1 /*
2  * AWS IoT Device Shadow v1.1.1
3  * Copyright (C) 2021 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 /* *INDENT-OFF* */
35 #ifdef __cplusplus
36  extern "C" {
37 #endif
38 /* *INDENT-ON* */
39 
40 /* SHADOW_DO_NOT_USE_CUSTOM_CONFIG allows building the Shadow library
41  * without a custom config. If a custom config is provided, the
42  * SHADOW_DO_NOT_USE_CUSTOM_CONFIG macro should not be defined. */
43 #ifndef SHADOW_DO_NOT_USE_CUSTOM_CONFIG
44  /* Include custom config file before other headers. */
45  #include "shadow_config.h"
46 #endif
47 
48 /* Include config defaults header to get default values of configs not
49  * defined in shadow_config_defaults.h file. */
50 #include "shadow_config_defaults.h"
51 
52 /*--------------------------- Shadow types ---------------------------*/
53 
59 typedef enum ShadowMessageType
60 {
61  ShadowMessageTypeGetAccepted = 0,
62  ShadowMessageTypeGetRejected,
63  ShadowMessageTypeDeleteAccepted,
64  ShadowMessageTypeDeleteRejected,
65  ShadowMessageTypeUpdateAccepted,
66  ShadowMessageTypeUpdateRejected,
67  ShadowMessageTypeUpdateDocuments,
68  ShadowMessageTypeUpdateDelta,
69  ShadowMessageTypeMaxNum
71 
79 typedef enum ShadowTopicStringType
80 {
81  ShadowTopicStringTypeGet = 0,
82  ShadowTopicStringTypeGetAccepted,
83  ShadowTopicStringTypeGetRejected,
84  ShadowTopicStringTypeDelete,
85  ShadowTopicStringTypeDeleteAccepted,
86  ShadowTopicStringTypeDeleteRejected,
87  ShadowTopicStringTypeUpdate,
88  ShadowTopicStringTypeUpdateAccepted,
89  ShadowTopicStringTypeUpdateRejected,
90  ShadowTopicStringTypeUpdateDocuments,
91  ShadowTopicStringTypeUpdateDelta,
92  ShadowTopicStringTypeMaxNum
94 
99 typedef enum ShadowStatus
100 {
110 
111 /*------------------------ Shadow library constants -------------------------*/
112 
118 #define SHADOW_PREFIX "$aws/things/"
119 
124 #define SHADOW_PREFIX_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_PREFIX ) - 1U ) )
125 
131 #define SHADOW_CLASSIC_ROOT "/shadow"
132 
137 #define SHADOW_CLASSIC_ROOT_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_CLASSIC_ROOT ) - 1U ) )
138 
144 #define SHADOW_NAMED_ROOT "/shadow/name/"
145 
150 #define SHADOW_NAMED_ROOT_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_NAMED_ROOT ) - 1U ) )
151 
156 #define SHADOW_OP_DELETE "/delete"
157 
162 #define SHADOW_OP_DELETE_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_OP_DELETE ) - 1U ) )
163 
168 #define SHADOW_OP_GET "/get"
169 
174 #define SHADOW_OP_GET_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_OP_GET ) - 1U ) )
175 
180 #define SHADOW_OP_UPDATE "/update"
181 
186 #define SHADOW_OP_UPDATE_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_OP_UPDATE ) - 1U ) )
187 
192 #define SHADOW_SUFFIX_ACCEPTED "/accepted"
193 
198 #define SHADOW_SUFFIX_ACCEPTED_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_SUFFIX_ACCEPTED ) - 1U ) )
199 
204 #define SHADOW_SUFFIX_REJECTED "/rejected"
205 
210 #define SHADOW_SUFFIX_REJECTED_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_SUFFIX_REJECTED ) - 1U ) )
211 
216 #define SHADOW_SUFFIX_DELTA "/delta"
217 
222 #define SHADOW_SUFFIX_DELTA_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_SUFFIX_DELTA ) - 1U ) )
223 
228 #define SHADOW_SUFFIX_DOCUMENTS "/documents"
229 
234 #define SHADOW_SUFFIX_DOCUMENTS_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_SUFFIX_DOCUMENTS ) - 1U ) )
235 
240 #define SHADOW_SUFFIX_NULL
241 
246 #define SHADOW_SUFFIX_NULL_LENGTH ( 0U )
247 
252 #define SHADOW_THINGNAME_LENGTH_MAX ( 128U )
253 
258 #define SHADOW_NAME_LENGTH_MAX ( 64U )
259 
264 #define SHADOW_NAME_CLASSIC ""
265 
270 #define SHADOW_NAME_CLASSIC_LENGTH ( ( uint16_t ) ( sizeof( SHADOW_NAME_CLASSIC ) - 1U ) )
271 
319 #define SHADOW_TOPIC_LEN( operationLength, suffixLength, thingNameLength, shadowNameLength ) \
320  ( operationLength + suffixLength + thingNameLength + shadowNameLength + \
321  SHADOW_PREFIX_LENGTH + \
322  ( ( shadowNameLength > 0 ) ? SHADOW_NAMED_ROOT_LENGTH : SHADOW_CLASSIC_ROOT_LENGTH ) )
323 
334 #define SHADOW_TOPIC_LEN_UPDATE( thingNameLength, shadowNameLength ) \
335  SHADOW_TOPIC_LEN( SHADOW_OP_UPDATE_LENGTH, SHADOW_SUFFIX_NULL_LENGTH, thingNameLength, shadowNameLength )
336 
347 #define SHADOW_TOPIC_LEN_UPDATE_ACC( thingNameLength, shadowNameLength ) \
348  SHADOW_TOPIC_LEN( SHADOW_OP_UPDATE_LENGTH, SHADOW_SUFFIX_ACCEPTED_LENGTH, thingNameLength, shadowNameLength )
349 
360 #define SHADOW_TOPIC_LEN_UPDATE_REJ( thingNameLength, shadowNameLength ) \
361  SHADOW_TOPIC_LEN( SHADOW_OP_UPDATE_LENGTH, SHADOW_SUFFIX_REJECTED_LENGTH, thingNameLength, shadowNameLength )
362 
373 #define SHADOW_TOPIC_LEN_UPDATE_DOCS( thingNameLength, shadowNameLength ) \
374  SHADOW_TOPIC_LEN( SHADOW_OP_UPDATE_LENGTH, SHADOW_SUFFIX_DOCUMENTS_LENGTH, thingNameLength, shadowNameLength )
375 
386 #define SHADOW_TOPIC_LEN_UPDATE_DELTA( thingNameLength, shadowNameLength ) \
387  SHADOW_TOPIC_LEN( SHADOW_OP_UPDATE_LENGTH, SHADOW_SUFFIX_DELTA_LENGTH, thingNameLength, shadowNameLength )
388 
399 #define SHADOW_TOPIC_LEN_GET( thingNameLength, shadowNameLength ) \
400  SHADOW_TOPIC_LEN( SHADOW_OP_GET_LENGTH, SHADOW_SUFFIX_NULL_LENGTH, thingNameLength, shadowNameLength )
401 
412 #define SHADOW_TOPIC_LEN_GET_ACC( thingNameLength, shadowNameLength ) \
413  SHADOW_TOPIC_LEN( SHADOW_OP_GET_LENGTH, SHADOW_SUFFIX_ACCEPTED_LENGTH, thingNameLength, shadowNameLength )
414 
425 #define SHADOW_TOPIC_LEN_GET_REJ( thingNameLength, shadowNameLength ) \
426  SHADOW_TOPIC_LEN( SHADOW_OP_GET_LENGTH, SHADOW_SUFFIX_REJECTED_LENGTH, thingNameLength, shadowNameLength )
427 
438 #define SHADOW_TOPIC_LEN_DELETE( thingNameLength, shadowNameLength ) \
439  SHADOW_TOPIC_LEN( SHADOW_OP_DELETE_LENGTH, SHADOW_SUFFIX_NULL_LENGTH, thingNameLength, shadowNameLength )
440 
451 #define SHADOW_TOPIC_LEN_DELETE_ACC( thingNameLength, shadowNameLength ) \
452  SHADOW_TOPIC_LEN( SHADOW_OP_DELETE_LENGTH, SHADOW_SUFFIX_ACCEPTED_LENGTH, thingNameLength, shadowNameLength )
453 
464 #define SHADOW_TOPIC_LEN_DELETE_REJ( thingNameLength, shadowNameLength ) \
465  SHADOW_TOPIC_LEN( SHADOW_OP_DELETE_LENGTH, SHADOW_SUFFIX_REJECTED_LENGTH, thingNameLength, shadowNameLength )
466 
476 #define SHADOW_TOPIC_LEN_MAX( thingNameLength, shadowNameLength ) \
477  SHADOW_TOPIC_LEN( SHADOW_OP_UPDATE_LENGTH, SHADOW_SUFFIX_DOCUMENTS_LENGTH, thingNameLength, shadowNameLength )
478 
506 #define SHADOW_TOPIC_STR( thingName, shadowName, operation, suffix ) \
507  ( ( sizeof( shadowName ) > 1 ) ? \
508  ( SHADOW_PREFIX thingName SHADOW_NAMED_ROOT shadowName operation suffix ) : \
509  ( SHADOW_PREFIX thingName SHADOW_CLASSIC_ROOT operation suffix ) )
510 
521 #define SHADOW_TOPIC_STR_UPDATE( thingName, shadowName ) \
522  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_UPDATE, SHADOW_SUFFIX_NULL )
523 
534 #define SHADOW_TOPIC_STR_UPDATE_ACC( thingName, shadowName ) \
535  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_UPDATE, SHADOW_SUFFIX_ACCEPTED )
536 
547 #define SHADOW_TOPIC_STR_UPDATE_REJ( thingName, shadowName ) \
548  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_UPDATE, SHADOW_SUFFIX_REJECTED )
549 
560 #define SHADOW_TOPIC_STR_UPDATE_DOCS( thingName, shadowName ) \
561  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_UPDATE, SHADOW_SUFFIX_DOCUMENTS )
562 
573 #define SHADOW_TOPIC_STR_UPDATE_DELTA( thingName, shadowName ) \
574  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_UPDATE, SHADOW_SUFFIX_DELTA )
575 
586 #define SHADOW_TOPIC_STR_GET( thingName, shadowName ) \
587  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_GET, SHADOW_SUFFIX_NULL )
588 
599 #define SHADOW_TOPIC_STR_GET_ACC( thingName, shadowName ) \
600  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_GET, SHADOW_SUFFIX_ACCEPTED )
601 
612 #define SHADOW_TOPIC_STR_GET_REJ( thingName, shadowName ) \
613  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_GET, SHADOW_SUFFIX_REJECTED )
614 
625 #define SHADOW_TOPIC_STR_DELETE( thingName, shadowName ) \
626  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_DELETE, SHADOW_SUFFIX_NULL )
627 
638 #define SHADOW_TOPIC_STR_DELETE_ACC( thingName, shadowName ) \
639  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_DELETE, SHADOW_SUFFIX_ACCEPTED )
640 
651 #define SHADOW_TOPIC_STR_DELETE_REJ( thingName, shadowName ) \
652  SHADOW_TOPIC_STR( thingName, shadowName, SHADOW_OP_DELETE, SHADOW_SUFFIX_REJECTED )
653 
654 /*------------------------ Shadow library functions -------------------------*/
655 
721 /* @[declare_shadow_assembletopicstring] */
723  const char * pThingName,
724  uint8_t thingNameLength,
725  const char * pShadowName,
726  uint8_t shadowNameLength,
727  char * pTopicBuffer,
728  uint16_t bufferSize,
729  uint16_t * pOutLength );
730 /* @[declare_shadow_assembletopicstring] */
731 
793 /* @[declare_shadow_matchtopicstring] */
794 ShadowStatus_t Shadow_MatchTopicString( const char * pTopic,
795  uint16_t topicLength,
796  ShadowMessageType_t * pMessageType,
797  const char ** pThingName,
798  uint8_t * pThingNameLength,
799  const char ** pShadowName,
800  uint8_t * pShadowNameLength );
801 /* @[declare_shadow_matchtopicstring] */
802 
803 /*------------- Shadow library backwardly-compatible constants -------------*/
804 
811 #define SHADOW_TOPIC_LENGTH( operationLength, suffixLength, thingNameLength ) \
812  SHADOW_TOPIC_LEN( operationLength, suffixLength, thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
813 
820 #define SHADOW_TOPIC_LENGTH_UPDATE( thingNameLength ) \
821  SHADOW_TOPIC_LEN_UPDATE( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
822 
829 #define SHADOW_TOPIC_LENGTH_UPDATE_ACCEPTED( thingNameLength ) \
830  SHADOW_TOPIC_LEN_UPDATE_ACC( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
831 
838 #define SHADOW_TOPIC_LENGTH_UPDATE_REJECTED( thingNameLength ) \
839  SHADOW_TOPIC_LEN_UPDATE_REJ( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
840 
847 #define SHADOW_TOPIC_LENGTH_UPDATE_DOCUMENTS( thingNameLength ) \
848  SHADOW_TOPIC_LEN_UPDATE_DOCS( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
849 
856 #define SHADOW_TOPIC_LENGTH_UPDATE_DELTA( thingNameLength ) \
857  SHADOW_TOPIC_LEN_UPDATE_DELTA( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
858 
865 #define SHADOW_TOPIC_LENGTH_GET( thingNameLength ) \
866  SHADOW_TOPIC_LEN_GET( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
867 
874 #define SHADOW_TOPIC_LENGTH_GET_ACCEPTED( thingNameLength ) \
875  SHADOW_TOPIC_LEN_GET_ACC( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
876 
883 #define SHADOW_TOPIC_LENGTH_GET_REJECTED( thingNameLength ) \
884  SHADOW_TOPIC_LEN_GET_REJ( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
885 
892 #define SHADOW_TOPIC_LENGTH_DELETE( thingNameLength ) \
893  SHADOW_TOPIC_LEN_DELETE( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
894 
901 #define SHADOW_TOPIC_LENGTH_DELETE_ACCEPTED( thingNameLength ) \
902  SHADOW_TOPIC_LEN_DELETE_ACC( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
903 
910 #define SHADOW_TOPIC_LENGTH_DELETE_REJECTED( thingNameLength ) \
911  SHADOW_TOPIC_LEN_DELETE_REJ( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
912 
919 #define SHADOW_TOPIC_LENGTH_MAX( thingNameLength ) \
920  SHADOW_TOPIC_LEN_MAX( thingNameLength, SHADOW_NAME_CLASSIC_LENGTH )
921 
928 #define SHADOW_TOPIC_STRING( thingName, operation, suffix ) \
929  SHADOW_TOPIC_STR( thingName, SHADOW_NAME_CLASSIC, operation, suffix )
930 
937 #define SHADOW_TOPIC_STRING_UPDATE( thingName ) \
938  SHADOW_TOPIC_STR_UPDATE( thingName, SHADOW_NAME_CLASSIC )
939 
946 #define SHADOW_TOPIC_STRING_UPDATE_ACCEPTED( thingName ) \
947  SHADOW_TOPIC_STR_UPDATE_ACC( thingName, SHADOW_NAME_CLASSIC )
948 
955 #define SHADOW_TOPIC_STRING_UPDATE_REJECTED( thingName ) \
956  SHADOW_TOPIC_STR_UPDATE_REJ( thingName, SHADOW_NAME_CLASSIC )
957 
964 #define SHADOW_TOPIC_STRING_UPDATE_DOCUMENTS( thingName ) \
965  SHADOW_TOPIC_STR_UPDATE_DOCS( thingName, SHADOW_NAME_CLASSIC )
966 
973 #define SHADOW_TOPIC_STRING_UPDATE_DELTA( thingName ) \
974  SHADOW_TOPIC_STR_UPDATE_DELTA( thingName, SHADOW_NAME_CLASSIC )
975 
982 #define SHADOW_TOPIC_STRING_GET( thingName ) \
983  SHADOW_TOPIC_STR_GET( thingName, SHADOW_NAME_CLASSIC )
984 
991 #define SHADOW_TOPIC_STRING_GET_ACCEPTED( thingName ) \
992  SHADOW_TOPIC_STR_GET_ACC( thingName, SHADOW_NAME_CLASSIC )
993 
1000 #define SHADOW_TOPIC_STRING_GET_REJECTED( thingName ) \
1001  SHADOW_TOPIC_STR_GET_REJ( thingName, SHADOW_NAME_CLASSIC )
1002 
1009 #define SHADOW_TOPIC_STRING_DELETE( thingName ) \
1010  SHADOW_TOPIC_STR_DELETE( thingName, SHADOW_NAME_CLASSIC )
1011 
1018 #define SHADOW_TOPIC_STRING_DELETE_ACCEPTED( thingName ) \
1019  SHADOW_TOPIC_STR_DELETE_ACC( thingName, SHADOW_NAME_CLASSIC )
1020 
1027 #define SHADOW_TOPIC_STRING_DELETE_REJECTED( thingName ) \
1028  SHADOW_TOPIC_STR_DELETE_REJ( thingName, SHADOW_NAME_CLASSIC )
1029 
1039 /* @[declare_shadow_gettopicstring] */
1040 #define Shadow_GetTopicString( topicType, pThingName, thingNameLength, pTopicBuffer, bufferSize, pOutLength ) \
1041  Shadow_AssembleTopicString( topicType, pThingName, thingNameLength, SHADOW_NAME_CLASSIC, 0, \
1042  pTopicBuffer, bufferSize, pOutLength )
1043 /* @[declare_shadow_gettopicstring] */
1044 
1056 /* @[declare_shadow_matchtopic] */
1057 ShadowStatus_t Shadow_MatchTopic( const char * pTopic,
1058  uint16_t topicLength,
1059  ShadowMessageType_t * pMessageType,
1060  const char ** pThingName,
1061  uint16_t * pThingNameLength );
1062 /* @[declare_shadow_matchtopic] */
1063 
1064 /* *INDENT-OFF* */
1065 #ifdef __cplusplus
1066  }
1067 #endif
1068 /* *INDENT-ON* */
1069 
1070 #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:108
SHADOW_THINGNAME_PARSE_FAILED
@ SHADOW_THINGNAME_PARSE_FAILED
Could not parse the thing name.
Definition: shadow.h:105
SHADOW_ROOT_PARSE_FAILED
@ SHADOW_ROOT_PARSE_FAILED
Could not parse the classic or named shadow root.
Definition: shadow.h:107
ShadowMessageType_t
ShadowMessageType_t
Each of these values describes the type of a shadow message. https://docs.aws.amazon....
Definition: shadow.h:60
SHADOW_BUFFER_TOO_SMALL
@ SHADOW_BUFFER_TOO_SMALL
The provided buffer is too small.
Definition: shadow.h:104
ShadowTopicStringType_t
ShadowTopicStringType_t
Each of these values describes the type of a shadow topic string.
Definition: shadow.h:80
ShadowStatus_t
ShadowStatus_t
Return codes from Shadow functions.
Definition: shadow.h:100
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:106
SHADOW_BAD_PARAMETER
@ SHADOW_BAD_PARAMETER
Input parameter is invalid.
Definition: shadow.h:103
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:102
SHADOW_SUCCESS
@ SHADOW_SUCCESS
Shadow function success.
Definition: shadow.h:101