Jeon byungchul
/
aws-iot-example
this is fork and i will modify for STM32
Fork of AWS-test by
Embed:
(wiki syntax)
Show/hide line numbers
aws_iot_json_utils.cpp
00001 /* 00002 * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"). 00005 * You may not use this file except in compliance with the License. 00006 * A copy of the License is located at 00007 * 00008 * http://aws.amazon.com/apache2.0 00009 * 00010 * or in the "license" file accompanying this file. This file is distributed 00011 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 00012 * express or implied. See the License for the specific language governing 00013 * permissions and limitations under the License. 00014 */ 00015 00016 /** 00017 * @file aws_json_utils.c 00018 * @brief Utilities for manipulating JSON 00019 * 00020 * json_utils provides JSON parsing utilities for use with the IoT SDK. 00021 * Underlying JSON parsing relies on the Jasmine JSON parser. 00022 * 00023 */ 00024 00025 #ifdef __cplusplus 00026 extern "C" { 00027 #endif 00028 00029 #include "aws_iot_json_utils.h" 00030 00031 #include <stdio.h> 00032 #include <stdint.h> 00033 #include <string.h> 00034 00035 #ifdef __cplusplus 00036 #include <cinttypes> 00037 #else 00038 00039 #include <inttypes.h> 00040 00041 #endif 00042 00043 #include "aws_iot_log.h" 00044 00045 int8_t jsoneq(const char *json, jsmntok_t *tok, const char *s) { 00046 if(tok->type == JSMN_STRING) { 00047 if((int) strlen(s) == tok->end - tok->start) { 00048 if(strncmp(json + tok->start, s, (size_t) (tok->end - tok->start)) == 0) { 00049 return 0; 00050 } 00051 } 00052 } 00053 return -1; 00054 } 00055 00056 IoT_Error_t parseUnsignedInteger32Value(uint32_t *i, const char *jsonString, jsmntok_t *token) { 00057 if(token->type != JSMN_PRIMITIVE) { 00058 IOT_WARN("Token was not an integer"); 00059 return JSON_PARSE_ERROR; 00060 } 00061 00062 if(('-' == (char) (jsonString[token->start])) || (1 != sscanf(jsonString + token->start, "%lu", i))) { 00063 IOT_WARN("Token was not an unsigned integer."); 00064 return JSON_PARSE_ERROR; 00065 } 00066 00067 return IOT_SUCCESS; 00068 } 00069 00070 IoT_Error_t parseUnsignedInteger16Value(uint16_t *i, const char *jsonString, jsmntok_t *token) { 00071 if(token->type != JSMN_PRIMITIVE) { 00072 IOT_WARN("Token was not an integer"); 00073 return JSON_PARSE_ERROR; 00074 } 00075 00076 //if(('-' == (char) (jsonString[token->start])) || (1 != sscanf(jsonString + token->start, "%" SCNu16, i))) { 00077 if(('-' == (char) (jsonString[token->start])) || (1 != sscanf(jsonString + token->start, "%hu", i))) { 00078 IOT_WARN("Token was not an unsigned integer."); 00079 return JSON_PARSE_ERROR; 00080 } 00081 00082 return IOT_SUCCESS; 00083 } 00084 00085 IoT_Error_t parseUnsignedInteger8Value(uint8_t *i, const char *jsonString, jsmntok_t *token) { 00086 if(token->type != JSMN_PRIMITIVE) { 00087 IOT_WARN("Token was not an integer"); 00088 return JSON_PARSE_ERROR; 00089 } 00090 00091 //if(('-' == (char) (jsonString[token->start])) || (1 != sscanf(jsonString + token->start, "%" SCNu8, i))) { 00092 if(('-' == (char) (jsonString[token->start])) || (1 != sscanf(jsonString + token->start, "%hhu", i))) { 00093 IOT_WARN("Token was not an unsigned integer."); 00094 return JSON_PARSE_ERROR; 00095 } 00096 00097 return IOT_SUCCESS; 00098 } 00099 00100 IoT_Error_t parseInteger32Value(int32_t *i, const char *jsonString, jsmntok_t *token) { 00101 if(token->type != JSMN_PRIMITIVE) { 00102 IOT_WARN("Token was not an integer"); 00103 return JSON_PARSE_ERROR; 00104 } 00105 00106 if(1 != sscanf(jsonString + token->start, "%li", i)) { 00107 IOT_WARN("Token was not an integer."); 00108 return JSON_PARSE_ERROR; 00109 } 00110 00111 return IOT_SUCCESS; 00112 } 00113 00114 IoT_Error_t parseInteger16Value(int16_t *i, const char *jsonString, jsmntok_t *token) { 00115 if(token->type != JSMN_PRIMITIVE) { 00116 IOT_WARN("Token was not an integer"); 00117 return JSON_PARSE_ERROR; 00118 } 00119 00120 if(1 != sscanf(jsonString + token->start, "%i", i)) { 00121 IOT_WARN("Token was not an integer."); 00122 return JSON_PARSE_ERROR; 00123 } 00124 00125 return IOT_SUCCESS; 00126 } 00127 00128 IoT_Error_t parseInteger8Value(int8_t *i, const char *jsonString, jsmntok_t *token) { 00129 if(token->type != JSMN_PRIMITIVE) { 00130 IOT_WARN("Token was not an integer"); 00131 return JSON_PARSE_ERROR; 00132 } 00133 00134 //if(1 != sscanf(jsonString + token->start, "%" SCNi8, i)) { 00135 if(1 != sscanf(jsonString + token->start, "%hhi", i)) { 00136 IOT_WARN("Token was not an integer."); 00137 return JSON_PARSE_ERROR; 00138 } 00139 00140 return IOT_SUCCESS; 00141 } 00142 00143 IoT_Error_t parseFloatValue(float *f, const char *jsonString, jsmntok_t *token) { 00144 if(token->type != JSMN_PRIMITIVE) { 00145 IOT_WARN("Token was not a float."); 00146 return JSON_PARSE_ERROR; 00147 } 00148 00149 if(1 != sscanf(jsonString + token->start, "%f", f)) { 00150 IOT_WARN("Token was not a float."); 00151 return JSON_PARSE_ERROR; 00152 } 00153 00154 return IOT_SUCCESS; 00155 } 00156 00157 IoT_Error_t parseDoubleValue(double *d, const char *jsonString, jsmntok_t *token) { 00158 if(token->type != JSMN_PRIMITIVE) { 00159 IOT_WARN("Token was not a double."); 00160 return JSON_PARSE_ERROR; 00161 } 00162 00163 if(1 != sscanf(jsonString + token->start, "%lf", d)) { 00164 IOT_WARN("Token was not a double."); 00165 return JSON_PARSE_ERROR; 00166 } 00167 00168 return IOT_SUCCESS; 00169 } 00170 00171 IoT_Error_t parseBooleanValue(bool *b, const char *jsonString, jsmntok_t *token) { 00172 if(token->type != JSMN_PRIMITIVE) { 00173 IOT_WARN("Token was not a primitive."); 00174 return JSON_PARSE_ERROR; 00175 } 00176 if(jsonString[token->start] == 't' && jsonString[token->start + 1] == 'r' && jsonString[token->start + 2] == 'u' 00177 && jsonString[token->start + 3] == 'e') { 00178 *b = true; 00179 } else if(jsonString[token->start] == 'f' && jsonString[token->start + 1] == 'a' 00180 && jsonString[token->start + 2] == 'l' && jsonString[token->start + 3] == 's' 00181 && jsonString[token->start + 4] == 'e') { 00182 *b = false; 00183 } else { 00184 IOT_WARN("Token was not a bool."); 00185 return JSON_PARSE_ERROR; 00186 } 00187 return IOT_SUCCESS; 00188 } 00189 00190 IoT_Error_t parseStringValue(char *buf, const char *jsonString, jsmntok_t *token) { 00191 uint16_t size = 0; 00192 if(token->type != JSMN_STRING) { 00193 IOT_WARN("Token was not a string."); 00194 return JSON_PARSE_ERROR; 00195 } 00196 size = (uint16_t) (token->end - token->start); 00197 memcpy(buf, jsonString + token->start, size); 00198 buf[size] = '\0'; 00199 return IOT_SUCCESS; 00200 } 00201 00202 #ifdef __cplusplus 00203 } 00204 #endif
Generated on Tue Jul 12 2022 11:16:36 by 1.7.2