2 * AWS IoT Shadow V2.1.0
\r
3 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\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
24 * @file aws_iot_shadow_parser.c
\r
25 * @brief Implements JSON parsing functions of the Shadow library.
\r
28 /* The config header is always included first. */
\r
29 #include "iot_config.h"
\r
31 /* Standard includes. */
\r
35 /* Shadow internal include. */
\r
36 #include "private/aws_iot_shadow_internal.h"
\r
38 /* Error handling include. */
\r
39 #include "iot_error.h"
\r
41 /* AWS Parser include. */
\r
42 #include "aws_iot_doc_parser.h"
\r
44 /*-----------------------------------------------------------*/
\r
47 * @brief The JSON key for the error code in a Shadow error document.
\r
49 #define ERROR_DOCUMENT_CODE_KEY "code"
\r
52 * @brief The length of #ERROR_DOCUMENT_CODE_KEY.
\r
54 #define ERROR_DOCUMENT_CODE_KEY_LENGTH ( sizeof( ERROR_DOCUMENT_CODE_KEY ) - 1 )
\r
57 * @brief The JSON key for the error message in a Shadow error document.
\r
59 #define ERROR_DOCUMENT_MESSAGE_KEY "message"
\r
62 * @brief The length of #ERROR_DOCUMENT_MESSAGE_KEY.
\r
64 #define ERROR_DOCUMENT_MESSAGE_KEY_LENGTH ( sizeof( ERROR_DOCUMENT_MESSAGE_KEY ) - 1 )
\r
66 /*-----------------------------------------------------------*/
\r
69 * @brief Converts a `unsigned long` to an `AwsIotShadowError_t`.
\r
71 * @param[in] code A value between 400 and 500 to convert.
\r
73 * @return A corresponding #AwsIotShadowError_t; #AWS_IOT_SHADOW_BAD_RESPONSE
\r
74 * if `code` is unknown.
\r
76 static AwsIotShadowError_t _codeToShadowStatus( uint32_t code );
\r
78 /*-----------------------------------------------------------*/
\r
80 static AwsIotShadowError_t _codeToShadowStatus( uint32_t code )
\r
82 AwsIotShadowError_t errorCode = AWS_IOT_SHADOW_STATUS_PENDING;
\r
84 /* Convert the Shadow response code to an AwsIotShadowError_t. */
\r
88 errorCode = AWS_IOT_SHADOW_BAD_REQUEST;
\r
92 errorCode = AWS_IOT_SHADOW_UNAUTHORIZED;
\r
96 errorCode = AWS_IOT_SHADOW_FORBIDDEN;
\r
100 errorCode = AWS_IOT_SHADOW_NOT_FOUND;
\r
104 errorCode = AWS_IOT_SHADOW_CONFLICT;
\r
108 errorCode = AWS_IOT_SHADOW_TOO_LARGE;
\r
112 errorCode = AWS_IOT_SHADOW_UNSUPPORTED;
\r
116 errorCode = AWS_IOT_SHADOW_TOO_MANY_REQUESTS;
\r
120 errorCode = AWS_IOT_SHADOW_SERVER_ERROR;
\r
124 errorCode = AWS_IOT_SHADOW_BAD_RESPONSE;
\r
131 /*-----------------------------------------------------------*/
\r
133 AwsIotShadowError_t _AwsIotShadow_ParseErrorDocument( const char * pErrorDocument,
\r
134 size_t errorDocumentLength )
\r
136 IOT_FUNCTION_ENTRY( AwsIotShadowError_t, AWS_IOT_SHADOW_STATUS_PENDING );
\r
137 const char * pCode = NULL, * pMessage = NULL;
\r
138 size_t codeLength = 0, messageLength = 0;
\r
141 /* Parse the code from the error document. */
\r
142 if( AwsIotDocParser_FindValue( pErrorDocument,
\r
143 errorDocumentLength,
\r
144 ERROR_DOCUMENT_CODE_KEY,
\r
145 ERROR_DOCUMENT_CODE_KEY_LENGTH,
\r
147 &codeLength ) == false )
\r
149 /* Error parsing JSON document, or no "code" key was found. */
\r
150 IotLogWarn( "Failed to parse code from error document.\n%.*s",
\r
151 errorDocumentLength,
\r
154 IOT_SET_AND_GOTO_CLEANUP( AWS_IOT_SHADOW_BAD_RESPONSE );
\r
157 /* Code must be in error document. */
\r
158 AwsIotShadow_Assert( ( pCode > pErrorDocument ) &&
\r
159 ( pCode + codeLength < pErrorDocument + errorDocumentLength ) );
\r
161 /* Convert the code to an unsigned integer value. */
\r
162 code = ( uint32_t ) strtoul( pCode, NULL, 10 );
\r
164 /* Parse the error message and print it. An error document must always contain
\r
166 if( AwsIotDocParser_FindValue( pErrorDocument,
\r
167 errorDocumentLength,
\r
168 ERROR_DOCUMENT_MESSAGE_KEY,
\r
169 ERROR_DOCUMENT_MESSAGE_KEY_LENGTH,
\r
171 &messageLength ) == true )
\r
173 IotLogWarn( "Code %lu: %.*s.",
\r
180 IotLogWarn( "Code %lu; failed to parse message from error document.\n%.*s",
\r
182 errorDocumentLength,
\r
185 /* An error document must contain a message; if it does not, then it is invalid. */
\r
186 IOT_SET_AND_GOTO_CLEANUP( AWS_IOT_SHADOW_BAD_RESPONSE );
\r
189 /* Convert a successfully parsed JSON code to a Shadow status. */
\r
190 status = _codeToShadowStatus( code );
\r
192 IOT_FUNCTION_EXIT_NO_CLEANUP();
\r
195 /*-----------------------------------------------------------*/
\r