![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
DIYmall 0.96" Inch I2c IIC Serial 128x64 Oled LCD LED White Display Module
Dependencies: Adafruit_GFX SDFileSystem
Fork of ATT_AWS_IoT_demo by
AWS_openssl/aws_iot_src/utils/aws_iot_json_utils.cpp@28:4650c541b029, 2018-10-09 (annotated)
- Committer:
- afmiee
- Date:
- Tue Oct 09 20:57:34 2018 +0000
- Revision:
- 28:4650c541b029
- Parent:
- 15:6f2798e45099
DIYmall 0.96" Inch I2c IIC Serial 128x64 Oled LCD LED White Display Module
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ampembeng | 15:6f2798e45099 | 1 | /* |
ampembeng | 15:6f2798e45099 | 2 | * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
ampembeng | 15:6f2798e45099 | 3 | * |
ampembeng | 15:6f2798e45099 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"). |
ampembeng | 15:6f2798e45099 | 5 | * You may not use this file except in compliance with the License. |
ampembeng | 15:6f2798e45099 | 6 | * A copy of the License is located at |
ampembeng | 15:6f2798e45099 | 7 | * |
ampembeng | 15:6f2798e45099 | 8 | * http://aws.amazon.com/apache2.0 |
ampembeng | 15:6f2798e45099 | 9 | * |
ampembeng | 15:6f2798e45099 | 10 | * or in the "license" file accompanying this file. This file is distributed |
ampembeng | 15:6f2798e45099 | 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
ampembeng | 15:6f2798e45099 | 12 | * express or implied. See the License for the specific language governing |
ampembeng | 15:6f2798e45099 | 13 | * permissions and limitations under the License. |
ampembeng | 15:6f2798e45099 | 14 | */ |
ampembeng | 15:6f2798e45099 | 15 | |
ampembeng | 15:6f2798e45099 | 16 | /** |
ampembeng | 15:6f2798e45099 | 17 | * @file aws_json_utils.c |
ampembeng | 15:6f2798e45099 | 18 | * @brief Utilities for manipulating JSON |
ampembeng | 15:6f2798e45099 | 19 | * |
ampembeng | 15:6f2798e45099 | 20 | * json_utils provides JSON parsing utilities for use with the IoT SDK. |
ampembeng | 15:6f2798e45099 | 21 | * Underlying JSON parsing relies on the Jasmine JSON parser. |
ampembeng | 15:6f2798e45099 | 22 | * |
ampembeng | 15:6f2798e45099 | 23 | */ |
ampembeng | 15:6f2798e45099 | 24 | |
ampembeng | 15:6f2798e45099 | 25 | #include "aws_iot_json_utils.h" |
ampembeng | 15:6f2798e45099 | 26 | |
ampembeng | 15:6f2798e45099 | 27 | #include <stdio.h> |
ampembeng | 15:6f2798e45099 | 28 | #include <stdint.h> |
ampembeng | 15:6f2798e45099 | 29 | #include <inttypes.h> |
ampembeng | 15:6f2798e45099 | 30 | #include <string.h> |
ampembeng | 15:6f2798e45099 | 31 | #include "aws_iot_log.h" |
ampembeng | 15:6f2798e45099 | 32 | |
ampembeng | 15:6f2798e45099 | 33 | int8_t jsoneq(const char *json, jsmntok_t *tok, const char *s) { |
ampembeng | 15:6f2798e45099 | 34 | if (tok->type == JSMN_STRING) { |
ampembeng | 15:6f2798e45099 | 35 | if ((int) strlen(s) == tok->end - tok->start) { |
ampembeng | 15:6f2798e45099 | 36 | if (strncmp(json + tok->start, s, tok->end - tok->start) == 0) { |
ampembeng | 15:6f2798e45099 | 37 | return 0; |
ampembeng | 15:6f2798e45099 | 38 | } |
ampembeng | 15:6f2798e45099 | 39 | } |
ampembeng | 15:6f2798e45099 | 40 | } |
ampembeng | 15:6f2798e45099 | 41 | return -1; |
ampembeng | 15:6f2798e45099 | 42 | } |
ampembeng | 15:6f2798e45099 | 43 | |
ampembeng | 15:6f2798e45099 | 44 | IoT_Error_t parseUnsignedInteger32Value(uint32_t *i, const char *jsonString, jsmntok_t *token) { |
ampembeng | 15:6f2798e45099 | 45 | if (token->type != JSMN_PRIMITIVE) { |
ampembeng | 15:6f2798e45099 | 46 | WARN("Token was not an integer"); |
ampembeng | 15:6f2798e45099 | 47 | return JSON_PARSE_ERROR; |
ampembeng | 15:6f2798e45099 | 48 | } |
ampembeng | 15:6f2798e45099 | 49 | |
ampembeng | 15:6f2798e45099 | 50 | //if (1 != sscanf(jsonString + token->start, "%"PRIu32, i)) { |
ampembeng | 15:6f2798e45099 | 51 | if (1 != sscanf(jsonString + token->start, "%lu", i)) { |
ampembeng | 15:6f2798e45099 | 52 | WARN("Token was not an integer."); |
ampembeng | 15:6f2798e45099 | 53 | return JSON_PARSE_ERROR; |
ampembeng | 15:6f2798e45099 | 54 | } |
ampembeng | 15:6f2798e45099 | 55 | |
ampembeng | 15:6f2798e45099 | 56 | return NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 57 | } |
ampembeng | 15:6f2798e45099 | 58 | |
ampembeng | 15:6f2798e45099 | 59 | IoT_Error_t parseUnsignedInteger16Value(uint16_t *i, const char *jsonString, jsmntok_t *token) { |
ampembeng | 15:6f2798e45099 | 60 | if (token->type != JSMN_PRIMITIVE) { |
ampembeng | 15:6f2798e45099 | 61 | WARN("Token was not an integer"); |
ampembeng | 15:6f2798e45099 | 62 | return JSON_PARSE_ERROR; |
ampembeng | 15:6f2798e45099 | 63 | } |
ampembeng | 15:6f2798e45099 | 64 | |
ampembeng | 15:6f2798e45099 | 65 | //if (1 != sscanf(jsonString + token->start, "%"PRIu16, i)) { |
ampembeng | 15:6f2798e45099 | 66 | if (1 != sscanf(jsonString + token->start, "%u", i)) { |
ampembeng | 15:6f2798e45099 | 67 | WARN("Token was not an integer."); |
ampembeng | 15:6f2798e45099 | 68 | return JSON_PARSE_ERROR; |
ampembeng | 15:6f2798e45099 | 69 | } |
ampembeng | 15:6f2798e45099 | 70 | |
ampembeng | 15:6f2798e45099 | 71 | return NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 72 | } |
ampembeng | 15:6f2798e45099 | 73 | |
ampembeng | 15:6f2798e45099 | 74 | IoT_Error_t parseUnsignedInteger8Value(uint8_t *i, const char *jsonString, jsmntok_t *token) { |
ampembeng | 15:6f2798e45099 | 75 | if (token->type != JSMN_PRIMITIVE) { |
ampembeng | 15:6f2798e45099 | 76 | WARN("Token was not an integer"); |
ampembeng | 15:6f2798e45099 | 77 | return JSON_PARSE_ERROR; |
ampembeng | 15:6f2798e45099 | 78 | } |
ampembeng | 15:6f2798e45099 | 79 | |
ampembeng | 15:6f2798e45099 | 80 | //if (1 != sscanf(jsonString + token->start, "%"PRIu8, i)) { |
ampembeng | 15:6f2798e45099 | 81 | if (1 != sscanf(jsonString + token->start, "%u", i)) { |
ampembeng | 15:6f2798e45099 | 82 | WARN("Token was not an integer."); |
ampembeng | 15:6f2798e45099 | 83 | return JSON_PARSE_ERROR; |
ampembeng | 15:6f2798e45099 | 84 | } |
ampembeng | 15:6f2798e45099 | 85 | |
ampembeng | 15:6f2798e45099 | 86 | return NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 87 | } |
ampembeng | 15:6f2798e45099 | 88 | |
ampembeng | 15:6f2798e45099 | 89 | IoT_Error_t parseInteger32Value(int32_t *i, const char *jsonString, jsmntok_t *token) { |
ampembeng | 15:6f2798e45099 | 90 | if (token->type != JSMN_PRIMITIVE) { |
ampembeng | 15:6f2798e45099 | 91 | WARN("Token was not an integer"); |
ampembeng | 15:6f2798e45099 | 92 | return JSON_PARSE_ERROR; |
ampembeng | 15:6f2798e45099 | 93 | } |
ampembeng | 15:6f2798e45099 | 94 | |
ampembeng | 15:6f2798e45099 | 95 | //if (1 != sscanf(jsonString + token->start, "%"PRIi32, i)) { |
ampembeng | 15:6f2798e45099 | 96 | if (1 != sscanf(jsonString + token->start, "%li", i)) { |
ampembeng | 15:6f2798e45099 | 97 | WARN("Token was not an integer."); |
ampembeng | 15:6f2798e45099 | 98 | return JSON_PARSE_ERROR; |
ampembeng | 15:6f2798e45099 | 99 | } |
ampembeng | 15:6f2798e45099 | 100 | |
ampembeng | 15:6f2798e45099 | 101 | return NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 102 | } |
ampembeng | 15:6f2798e45099 | 103 | |
ampembeng | 15:6f2798e45099 | 104 | IoT_Error_t parseInteger16Value(int16_t *i, const char *jsonString, jsmntok_t *token) { |
ampembeng | 15:6f2798e45099 | 105 | if (token->type != JSMN_PRIMITIVE) { |
ampembeng | 15:6f2798e45099 | 106 | WARN("Token was not an integer"); |
ampembeng | 15:6f2798e45099 | 107 | return JSON_PARSE_ERROR; |
ampembeng | 15:6f2798e45099 | 108 | } |
ampembeng | 15:6f2798e45099 | 109 | |
ampembeng | 15:6f2798e45099 | 110 | //if (1 != sscanf(jsonString + token->start, "%"PRIi16, i)) { |
ampembeng | 15:6f2798e45099 | 111 | if (1 != sscanf(jsonString + token->start, "%i", i)) { |
ampembeng | 15:6f2798e45099 | 112 | WARN("Token was not an integer."); |
ampembeng | 15:6f2798e45099 | 113 | return JSON_PARSE_ERROR; |
ampembeng | 15:6f2798e45099 | 114 | } |
ampembeng | 15:6f2798e45099 | 115 | |
ampembeng | 15:6f2798e45099 | 116 | return NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 117 | } |
ampembeng | 15:6f2798e45099 | 118 | |
ampembeng | 15:6f2798e45099 | 119 | IoT_Error_t parseInteger8Value(int8_t *i, const char *jsonString, jsmntok_t *token) { |
ampembeng | 15:6f2798e45099 | 120 | if (token->type != JSMN_PRIMITIVE) { |
ampembeng | 15:6f2798e45099 | 121 | WARN("Token was not an integer"); |
ampembeng | 15:6f2798e45099 | 122 | return JSON_PARSE_ERROR; |
ampembeng | 15:6f2798e45099 | 123 | } |
ampembeng | 15:6f2798e45099 | 124 | |
ampembeng | 15:6f2798e45099 | 125 | //if (1 != sscanf(jsonString + token->start, "%"PRIi8, i)) { |
ampembeng | 15:6f2798e45099 | 126 | if (1 != sscanf(jsonString + token->start, "%i", i)) { |
ampembeng | 15:6f2798e45099 | 127 | WARN("Token was not an integer."); |
ampembeng | 15:6f2798e45099 | 128 | return JSON_PARSE_ERROR; |
ampembeng | 15:6f2798e45099 | 129 | } |
ampembeng | 15:6f2798e45099 | 130 | |
ampembeng | 15:6f2798e45099 | 131 | return NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 132 | } |
ampembeng | 15:6f2798e45099 | 133 | |
ampembeng | 15:6f2798e45099 | 134 | IoT_Error_t parseFloatValue(float *f, const char *jsonString, jsmntok_t *token) { |
ampembeng | 15:6f2798e45099 | 135 | if (token->type != JSMN_PRIMITIVE) { |
ampembeng | 15:6f2798e45099 | 136 | WARN("Token was not a float."); |
ampembeng | 15:6f2798e45099 | 137 | return JSON_PARSE_ERROR; |
ampembeng | 15:6f2798e45099 | 138 | } |
ampembeng | 15:6f2798e45099 | 139 | |
ampembeng | 15:6f2798e45099 | 140 | if (1 != sscanf(jsonString + token->start, "%f", f)) { |
ampembeng | 15:6f2798e45099 | 141 | WARN("Token was not a float."); |
ampembeng | 15:6f2798e45099 | 142 | return JSON_PARSE_ERROR; |
ampembeng | 15:6f2798e45099 | 143 | } |
ampembeng | 15:6f2798e45099 | 144 | |
ampembeng | 15:6f2798e45099 | 145 | return NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 146 | } |
ampembeng | 15:6f2798e45099 | 147 | |
ampembeng | 15:6f2798e45099 | 148 | IoT_Error_t parseDoubleValue(double *d, const char *jsonString, jsmntok_t *token) { |
ampembeng | 15:6f2798e45099 | 149 | if (token->type != JSMN_PRIMITIVE) { |
ampembeng | 15:6f2798e45099 | 150 | WARN("Token was not a double."); |
ampembeng | 15:6f2798e45099 | 151 | return JSON_PARSE_ERROR; |
ampembeng | 15:6f2798e45099 | 152 | } |
ampembeng | 15:6f2798e45099 | 153 | |
ampembeng | 15:6f2798e45099 | 154 | if (1 != sscanf(jsonString + token->start, "%lf", d)) { |
ampembeng | 15:6f2798e45099 | 155 | WARN("Token was not a double."); |
ampembeng | 15:6f2798e45099 | 156 | return JSON_PARSE_ERROR; |
ampembeng | 15:6f2798e45099 | 157 | } |
ampembeng | 15:6f2798e45099 | 158 | |
ampembeng | 15:6f2798e45099 | 159 | return NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 160 | } |
ampembeng | 15:6f2798e45099 | 161 | |
ampembeng | 15:6f2798e45099 | 162 | IoT_Error_t parseBooleanValue(bool *b, const char *jsonString, jsmntok_t *token) { |
ampembeng | 15:6f2798e45099 | 163 | if (token->type != JSMN_PRIMITIVE) { |
ampembeng | 15:6f2798e45099 | 164 | WARN("Token was not a primitive."); |
ampembeng | 15:6f2798e45099 | 165 | return JSON_PARSE_ERROR; |
ampembeng | 15:6f2798e45099 | 166 | } |
ampembeng | 15:6f2798e45099 | 167 | if (jsonString[token->start] == 't' && jsonString[token->start + 1] == 'r' && jsonString[token->start + 2] == 'u' |
ampembeng | 15:6f2798e45099 | 168 | && jsonString[token->start + 3] == 'e') { |
ampembeng | 15:6f2798e45099 | 169 | *b = true; |
ampembeng | 15:6f2798e45099 | 170 | } else if (jsonString[token->start] == 'f' && jsonString[token->start + 1] == 'a' |
ampembeng | 15:6f2798e45099 | 171 | && jsonString[token->start + 2] == 'l' && jsonString[token->start + 3] == 's' |
ampembeng | 15:6f2798e45099 | 172 | && jsonString[token->start + 4] == 'e') { |
ampembeng | 15:6f2798e45099 | 173 | *b = false; |
ampembeng | 15:6f2798e45099 | 174 | } else { |
ampembeng | 15:6f2798e45099 | 175 | WARN("Token was not a bool."); |
ampembeng | 15:6f2798e45099 | 176 | return JSON_PARSE_ERROR; |
ampembeng | 15:6f2798e45099 | 177 | } |
ampembeng | 15:6f2798e45099 | 178 | return NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 179 | } |
ampembeng | 15:6f2798e45099 | 180 | |
ampembeng | 15:6f2798e45099 | 181 | IoT_Error_t parseStringValue(char *buf, const char *jsonString, jsmntok_t *token) { |
ampembeng | 15:6f2798e45099 | 182 | uint16_t size = 0; |
ampembeng | 15:6f2798e45099 | 183 | if (token->type != JSMN_STRING) { |
ampembeng | 15:6f2798e45099 | 184 | WARN("Token was not a string."); |
ampembeng | 15:6f2798e45099 | 185 | return JSON_PARSE_ERROR; |
ampembeng | 15:6f2798e45099 | 186 | } |
ampembeng | 15:6f2798e45099 | 187 | size = token->end - token->start; |
ampembeng | 15:6f2798e45099 | 188 | memcpy(buf, jsonString + token->start, size); |
ampembeng | 15:6f2798e45099 | 189 | buf[size] = '\0'; |
ampembeng | 15:6f2798e45099 | 190 | return NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 191 | } |
ampembeng | 15:6f2798e45099 | 192 |