pick up wakaama files from https://github.com/eclipse/wakaama
core/data.c@0:1fa43ab66921, 2017-04-19 (annotated)
- Committer:
- terencez
- Date:
- Wed Apr 19 11:30:02 2017 +0000
- Revision:
- 0:1fa43ab66921
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
terencez | 0:1fa43ab66921 | 1 | /******************************************************************************* |
terencez | 0:1fa43ab66921 | 2 | * |
terencez | 0:1fa43ab66921 | 3 | * Copyright (c) 2013, 2014 Intel Corporation and others. |
terencez | 0:1fa43ab66921 | 4 | * All rights reserved. This program and the accompanying materials |
terencez | 0:1fa43ab66921 | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
terencez | 0:1fa43ab66921 | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
terencez | 0:1fa43ab66921 | 7 | * |
terencez | 0:1fa43ab66921 | 8 | * The Eclipse Public License is available at |
terencez | 0:1fa43ab66921 | 9 | * http://www.eclipse.org/legal/epl-v10.html |
terencez | 0:1fa43ab66921 | 10 | * The Eclipse Distribution License is available at |
terencez | 0:1fa43ab66921 | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. |
terencez | 0:1fa43ab66921 | 12 | * |
terencez | 0:1fa43ab66921 | 13 | * Contributors: |
terencez | 0:1fa43ab66921 | 14 | * David Navarro, Intel Corporation - initial API and implementation |
terencez | 0:1fa43ab66921 | 15 | * Fabien Fleutot - Please refer to git log |
terencez | 0:1fa43ab66921 | 16 | * Bosch Software Innovations GmbH - Please refer to git log |
terencez | 0:1fa43ab66921 | 17 | * |
terencez | 0:1fa43ab66921 | 18 | *******************************************************************************/ |
terencez | 0:1fa43ab66921 | 19 | |
terencez | 0:1fa43ab66921 | 20 | #include "internals.h" |
terencez | 0:1fa43ab66921 | 21 | #include <float.h> |
terencez | 0:1fa43ab66921 | 22 | |
terencez | 0:1fa43ab66921 | 23 | // dataP array length is assumed to be 1. |
terencez | 0:1fa43ab66921 | 24 | static int prv_textSerialize(lwm2m_data_t * dataP, |
terencez | 0:1fa43ab66921 | 25 | uint8_t ** bufferP) |
terencez | 0:1fa43ab66921 | 26 | { |
terencez | 0:1fa43ab66921 | 27 | size_t res; |
terencez | 0:1fa43ab66921 | 28 | |
terencez | 0:1fa43ab66921 | 29 | switch (dataP->type) |
terencez | 0:1fa43ab66921 | 30 | { |
terencez | 0:1fa43ab66921 | 31 | case LWM2M_TYPE_STRING: |
terencez | 0:1fa43ab66921 | 32 | *bufferP = (uint8_t *)lwm2m_malloc(dataP->value.asBuffer.length); |
terencez | 0:1fa43ab66921 | 33 | if (*bufferP == NULL) return 0; |
terencez | 0:1fa43ab66921 | 34 | memcpy(*bufferP, dataP->value.asBuffer.buffer, dataP->value.asBuffer.length); |
terencez | 0:1fa43ab66921 | 35 | return (int)dataP->value.asBuffer.length; |
terencez | 0:1fa43ab66921 | 36 | |
terencez | 0:1fa43ab66921 | 37 | case LWM2M_TYPE_INTEGER: |
terencez | 0:1fa43ab66921 | 38 | res = utils_int64ToPlainText(dataP->value.asInteger, bufferP); |
terencez | 0:1fa43ab66921 | 39 | if (res == 0) return -1; |
terencez | 0:1fa43ab66921 | 40 | return (int)res; |
terencez | 0:1fa43ab66921 | 41 | |
terencez | 0:1fa43ab66921 | 42 | case LWM2M_TYPE_FLOAT: |
terencez | 0:1fa43ab66921 | 43 | res = utils_float64ToPlainText(dataP->value.asFloat, bufferP); |
terencez | 0:1fa43ab66921 | 44 | if (res == 0) return -1; |
terencez | 0:1fa43ab66921 | 45 | return (int)res; |
terencez | 0:1fa43ab66921 | 46 | |
terencez | 0:1fa43ab66921 | 47 | case LWM2M_TYPE_BOOLEAN: |
terencez | 0:1fa43ab66921 | 48 | res = utils_boolToPlainText(dataP->value.asBoolean, bufferP); |
terencez | 0:1fa43ab66921 | 49 | if (res == 0) return -1; |
terencez | 0:1fa43ab66921 | 50 | return (int)res; |
terencez | 0:1fa43ab66921 | 51 | |
terencez | 0:1fa43ab66921 | 52 | case LWM2M_TYPE_OBJECT_LINK: |
terencez | 0:1fa43ab66921 | 53 | { |
terencez | 0:1fa43ab66921 | 54 | char stringBuffer[20]; |
terencez | 0:1fa43ab66921 | 55 | int len = snprintf(stringBuffer, 20, "%d:%d", |
terencez | 0:1fa43ab66921 | 56 | dataP->value.asObjLink.objectId, |
terencez | 0:1fa43ab66921 | 57 | dataP->value.asObjLink.objectInstanceId); |
terencez | 0:1fa43ab66921 | 58 | *bufferP = (uint8_t *)lwm2m_malloc(len); |
terencez | 0:1fa43ab66921 | 59 | if (*bufferP == NULL) return -1; |
terencez | 0:1fa43ab66921 | 60 | memcpy(*bufferP, stringBuffer, len); |
terencez | 0:1fa43ab66921 | 61 | return len; |
terencez | 0:1fa43ab66921 | 62 | } |
terencez | 0:1fa43ab66921 | 63 | case LWM2M_TYPE_OPAQUE: |
terencez | 0:1fa43ab66921 | 64 | case LWM2M_TYPE_UNDEFINED: |
terencez | 0:1fa43ab66921 | 65 | default: |
terencez | 0:1fa43ab66921 | 66 | return -1; |
terencez | 0:1fa43ab66921 | 67 | } |
terencez | 0:1fa43ab66921 | 68 | } |
terencez | 0:1fa43ab66921 | 69 | |
terencez | 0:1fa43ab66921 | 70 | static int prv_setBuffer(lwm2m_data_t * dataP, |
terencez | 0:1fa43ab66921 | 71 | uint8_t * buffer, |
terencez | 0:1fa43ab66921 | 72 | size_t bufferLen) |
terencez | 0:1fa43ab66921 | 73 | { |
terencez | 0:1fa43ab66921 | 74 | dataP->value.asBuffer.buffer = (uint8_t *)lwm2m_malloc(bufferLen); |
terencez | 0:1fa43ab66921 | 75 | if (dataP->value.asBuffer.buffer == NULL) |
terencez | 0:1fa43ab66921 | 76 | { |
terencez | 0:1fa43ab66921 | 77 | lwm2m_data_free(1, dataP); |
terencez | 0:1fa43ab66921 | 78 | return 0; |
terencez | 0:1fa43ab66921 | 79 | } |
terencez | 0:1fa43ab66921 | 80 | dataP->value.asBuffer.length = bufferLen; |
terencez | 0:1fa43ab66921 | 81 | memcpy(dataP->value.asBuffer.buffer, buffer, bufferLen); |
terencez | 0:1fa43ab66921 | 82 | |
terencez | 0:1fa43ab66921 | 83 | return 1; |
terencez | 0:1fa43ab66921 | 84 | } |
terencez | 0:1fa43ab66921 | 85 | |
terencez | 0:1fa43ab66921 | 86 | lwm2m_data_t * lwm2m_data_new(int size) |
terencez | 0:1fa43ab66921 | 87 | { |
terencez | 0:1fa43ab66921 | 88 | lwm2m_data_t * dataP; |
terencez | 0:1fa43ab66921 | 89 | |
terencez | 0:1fa43ab66921 | 90 | LOG_ARG("size: %d", size); |
terencez | 0:1fa43ab66921 | 91 | if (size <= 0) return NULL; |
terencez | 0:1fa43ab66921 | 92 | |
terencez | 0:1fa43ab66921 | 93 | dataP = (lwm2m_data_t *)lwm2m_malloc(size * sizeof(lwm2m_data_t)); |
terencez | 0:1fa43ab66921 | 94 | |
terencez | 0:1fa43ab66921 | 95 | if (dataP != NULL) |
terencez | 0:1fa43ab66921 | 96 | { |
terencez | 0:1fa43ab66921 | 97 | memset(dataP, 0, size * sizeof(lwm2m_data_t)); |
terencez | 0:1fa43ab66921 | 98 | } |
terencez | 0:1fa43ab66921 | 99 | |
terencez | 0:1fa43ab66921 | 100 | return dataP; |
terencez | 0:1fa43ab66921 | 101 | } |
terencez | 0:1fa43ab66921 | 102 | |
terencez | 0:1fa43ab66921 | 103 | void lwm2m_data_free(int size, |
terencez | 0:1fa43ab66921 | 104 | lwm2m_data_t * dataP) |
terencez | 0:1fa43ab66921 | 105 | { |
terencez | 0:1fa43ab66921 | 106 | int i; |
terencez | 0:1fa43ab66921 | 107 | |
terencez | 0:1fa43ab66921 | 108 | LOG_ARG("size: %d", size); |
terencez | 0:1fa43ab66921 | 109 | if (size == 0 || dataP == NULL) return; |
terencez | 0:1fa43ab66921 | 110 | |
terencez | 0:1fa43ab66921 | 111 | for (i = 0; i < size; i++) |
terencez | 0:1fa43ab66921 | 112 | { |
terencez | 0:1fa43ab66921 | 113 | switch (dataP[i].type) |
terencez | 0:1fa43ab66921 | 114 | { |
terencez | 0:1fa43ab66921 | 115 | case LWM2M_TYPE_MULTIPLE_RESOURCE: |
terencez | 0:1fa43ab66921 | 116 | case LWM2M_TYPE_OBJECT_INSTANCE: |
terencez | 0:1fa43ab66921 | 117 | case LWM2M_TYPE_OBJECT: |
terencez | 0:1fa43ab66921 | 118 | lwm2m_data_free(dataP[i].value.asChildren.count, dataP[i].value.asChildren.array); |
terencez | 0:1fa43ab66921 | 119 | break; |
terencez | 0:1fa43ab66921 | 120 | |
terencez | 0:1fa43ab66921 | 121 | case LWM2M_TYPE_STRING: |
terencez | 0:1fa43ab66921 | 122 | case LWM2M_TYPE_OPAQUE: |
terencez | 0:1fa43ab66921 | 123 | if (dataP[i].value.asBuffer.buffer != NULL) |
terencez | 0:1fa43ab66921 | 124 | { |
terencez | 0:1fa43ab66921 | 125 | lwm2m_free(dataP[i].value.asBuffer.buffer); |
terencez | 0:1fa43ab66921 | 126 | } |
terencez | 0:1fa43ab66921 | 127 | |
terencez | 0:1fa43ab66921 | 128 | default: |
terencez | 0:1fa43ab66921 | 129 | // do nothing |
terencez | 0:1fa43ab66921 | 130 | break; |
terencez | 0:1fa43ab66921 | 131 | } |
terencez | 0:1fa43ab66921 | 132 | } |
terencez | 0:1fa43ab66921 | 133 | lwm2m_free(dataP); |
terencez | 0:1fa43ab66921 | 134 | } |
terencez | 0:1fa43ab66921 | 135 | |
terencez | 0:1fa43ab66921 | 136 | void lwm2m_data_encode_string(const char * string, |
terencez | 0:1fa43ab66921 | 137 | lwm2m_data_t * dataP) |
terencez | 0:1fa43ab66921 | 138 | { |
terencez | 0:1fa43ab66921 | 139 | size_t len; |
terencez | 0:1fa43ab66921 | 140 | int res; |
terencez | 0:1fa43ab66921 | 141 | |
terencez | 0:1fa43ab66921 | 142 | LOG_ARG("\"%s\"", string); |
terencez | 0:1fa43ab66921 | 143 | if (string == NULL) |
terencez | 0:1fa43ab66921 | 144 | { |
terencez | 0:1fa43ab66921 | 145 | len = 0; |
terencez | 0:1fa43ab66921 | 146 | } |
terencez | 0:1fa43ab66921 | 147 | else |
terencez | 0:1fa43ab66921 | 148 | { |
terencez | 0:1fa43ab66921 | 149 | for (len = 0; string[len] != 0; len++); |
terencez | 0:1fa43ab66921 | 150 | } |
terencez | 0:1fa43ab66921 | 151 | |
terencez | 0:1fa43ab66921 | 152 | if (len == 0) |
terencez | 0:1fa43ab66921 | 153 | { |
terencez | 0:1fa43ab66921 | 154 | dataP->value.asBuffer.length = 0; |
terencez | 0:1fa43ab66921 | 155 | dataP->value.asBuffer.buffer = NULL; |
terencez | 0:1fa43ab66921 | 156 | res = 1; |
terencez | 0:1fa43ab66921 | 157 | } |
terencez | 0:1fa43ab66921 | 158 | else |
terencez | 0:1fa43ab66921 | 159 | { |
terencez | 0:1fa43ab66921 | 160 | res = prv_setBuffer(dataP, (uint8_t *)string, len); |
terencez | 0:1fa43ab66921 | 161 | } |
terencez | 0:1fa43ab66921 | 162 | |
terencez | 0:1fa43ab66921 | 163 | if (res == 1) |
terencez | 0:1fa43ab66921 | 164 | { |
terencez | 0:1fa43ab66921 | 165 | dataP->type = LWM2M_TYPE_STRING; |
terencez | 0:1fa43ab66921 | 166 | } |
terencez | 0:1fa43ab66921 | 167 | else |
terencez | 0:1fa43ab66921 | 168 | { |
terencez | 0:1fa43ab66921 | 169 | dataP->type = LWM2M_TYPE_UNDEFINED; |
terencez | 0:1fa43ab66921 | 170 | } |
terencez | 0:1fa43ab66921 | 171 | } |
terencez | 0:1fa43ab66921 | 172 | |
terencez | 0:1fa43ab66921 | 173 | void lwm2m_data_encode_opaque(uint8_t * buffer, |
terencez | 0:1fa43ab66921 | 174 | size_t length, |
terencez | 0:1fa43ab66921 | 175 | lwm2m_data_t * dataP) |
terencez | 0:1fa43ab66921 | 176 | { |
terencez | 0:1fa43ab66921 | 177 | int res; |
terencez | 0:1fa43ab66921 | 178 | |
terencez | 0:1fa43ab66921 | 179 | LOG_ARG("length: %d", length); |
terencez | 0:1fa43ab66921 | 180 | if (length == 0) |
terencez | 0:1fa43ab66921 | 181 | { |
terencez | 0:1fa43ab66921 | 182 | dataP->value.asBuffer.length = 0; |
terencez | 0:1fa43ab66921 | 183 | dataP->value.asBuffer.buffer = NULL; |
terencez | 0:1fa43ab66921 | 184 | res = 1; |
terencez | 0:1fa43ab66921 | 185 | } |
terencez | 0:1fa43ab66921 | 186 | else |
terencez | 0:1fa43ab66921 | 187 | { |
terencez | 0:1fa43ab66921 | 188 | res = prv_setBuffer(dataP, buffer, length); |
terencez | 0:1fa43ab66921 | 189 | } |
terencez | 0:1fa43ab66921 | 190 | |
terencez | 0:1fa43ab66921 | 191 | if (res == 1) |
terencez | 0:1fa43ab66921 | 192 | { |
terencez | 0:1fa43ab66921 | 193 | dataP->type = LWM2M_TYPE_OPAQUE; |
terencez | 0:1fa43ab66921 | 194 | } |
terencez | 0:1fa43ab66921 | 195 | else |
terencez | 0:1fa43ab66921 | 196 | { |
terencez | 0:1fa43ab66921 | 197 | dataP->type = LWM2M_TYPE_UNDEFINED; |
terencez | 0:1fa43ab66921 | 198 | } |
terencez | 0:1fa43ab66921 | 199 | } |
terencez | 0:1fa43ab66921 | 200 | |
terencez | 0:1fa43ab66921 | 201 | void lwm2m_data_encode_nstring(const char * string, |
terencez | 0:1fa43ab66921 | 202 | size_t length, |
terencez | 0:1fa43ab66921 | 203 | lwm2m_data_t * dataP) |
terencez | 0:1fa43ab66921 | 204 | { |
terencez | 0:1fa43ab66921 | 205 | LOG_ARG("length: %d, string: \"%s\"", length, string); |
terencez | 0:1fa43ab66921 | 206 | lwm2m_data_encode_opaque((uint8_t *)string, length, dataP); |
terencez | 0:1fa43ab66921 | 207 | |
terencez | 0:1fa43ab66921 | 208 | if (dataP->type == LWM2M_TYPE_OPAQUE) |
terencez | 0:1fa43ab66921 | 209 | { |
terencez | 0:1fa43ab66921 | 210 | dataP->type = LWM2M_TYPE_STRING; |
terencez | 0:1fa43ab66921 | 211 | } |
terencez | 0:1fa43ab66921 | 212 | } |
terencez | 0:1fa43ab66921 | 213 | |
terencez | 0:1fa43ab66921 | 214 | void lwm2m_data_encode_int(int64_t value, |
terencez | 0:1fa43ab66921 | 215 | lwm2m_data_t * dataP) |
terencez | 0:1fa43ab66921 | 216 | { |
terencez | 0:1fa43ab66921 | 217 | LOG_ARG("value: %" PRId64 "", value); |
terencez | 0:1fa43ab66921 | 218 | dataP->type = LWM2M_TYPE_INTEGER; |
terencez | 0:1fa43ab66921 | 219 | dataP->value.asInteger = value; |
terencez | 0:1fa43ab66921 | 220 | } |
terencez | 0:1fa43ab66921 | 221 | |
terencez | 0:1fa43ab66921 | 222 | int lwm2m_data_decode_int(const lwm2m_data_t * dataP, |
terencez | 0:1fa43ab66921 | 223 | int64_t * valueP) |
terencez | 0:1fa43ab66921 | 224 | { |
terencez | 0:1fa43ab66921 | 225 | int result; |
terencez | 0:1fa43ab66921 | 226 | |
terencez | 0:1fa43ab66921 | 227 | LOG("Entering"); |
terencez | 0:1fa43ab66921 | 228 | switch (dataP->type) |
terencez | 0:1fa43ab66921 | 229 | { |
terencez | 0:1fa43ab66921 | 230 | case LWM2M_TYPE_INTEGER: |
terencez | 0:1fa43ab66921 | 231 | *valueP = dataP->value.asInteger; |
terencez | 0:1fa43ab66921 | 232 | result = 1; |
terencez | 0:1fa43ab66921 | 233 | break; |
terencez | 0:1fa43ab66921 | 234 | |
terencez | 0:1fa43ab66921 | 235 | case LWM2M_TYPE_STRING: |
terencez | 0:1fa43ab66921 | 236 | result = utils_plainTextToInt64(dataP->value.asBuffer.buffer, dataP->value.asBuffer.length, valueP); |
terencez | 0:1fa43ab66921 | 237 | break; |
terencez | 0:1fa43ab66921 | 238 | |
terencez | 0:1fa43ab66921 | 239 | case LWM2M_TYPE_OPAQUE: |
terencez | 0:1fa43ab66921 | 240 | result = utils_opaqueToInt(dataP->value.asBuffer.buffer, dataP->value.asBuffer.length, valueP); |
terencez | 0:1fa43ab66921 | 241 | if (result == (int)dataP->value.asBuffer.length) |
terencez | 0:1fa43ab66921 | 242 | { |
terencez | 0:1fa43ab66921 | 243 | result = 1; |
terencez | 0:1fa43ab66921 | 244 | } |
terencez | 0:1fa43ab66921 | 245 | else |
terencez | 0:1fa43ab66921 | 246 | { |
terencez | 0:1fa43ab66921 | 247 | result = 0; |
terencez | 0:1fa43ab66921 | 248 | } |
terencez | 0:1fa43ab66921 | 249 | break; |
terencez | 0:1fa43ab66921 | 250 | |
terencez | 0:1fa43ab66921 | 251 | default: |
terencez | 0:1fa43ab66921 | 252 | return 0; |
terencez | 0:1fa43ab66921 | 253 | } |
terencez | 0:1fa43ab66921 | 254 | LOG_ARG("result: %d, value: %" PRId64, result, *valueP); |
terencez | 0:1fa43ab66921 | 255 | |
terencez | 0:1fa43ab66921 | 256 | return result; |
terencez | 0:1fa43ab66921 | 257 | } |
terencez | 0:1fa43ab66921 | 258 | |
terencez | 0:1fa43ab66921 | 259 | void lwm2m_data_encode_float(double value, |
terencez | 0:1fa43ab66921 | 260 | lwm2m_data_t * dataP) |
terencez | 0:1fa43ab66921 | 261 | { |
terencez | 0:1fa43ab66921 | 262 | LOG_ARG("value: %f", value); |
terencez | 0:1fa43ab66921 | 263 | dataP->type = LWM2M_TYPE_FLOAT; |
terencez | 0:1fa43ab66921 | 264 | dataP->value.asFloat = value; |
terencez | 0:1fa43ab66921 | 265 | } |
terencez | 0:1fa43ab66921 | 266 | |
terencez | 0:1fa43ab66921 | 267 | int lwm2m_data_decode_float(const lwm2m_data_t * dataP, |
terencez | 0:1fa43ab66921 | 268 | double * valueP) |
terencez | 0:1fa43ab66921 | 269 | { |
terencez | 0:1fa43ab66921 | 270 | int result; |
terencez | 0:1fa43ab66921 | 271 | |
terencez | 0:1fa43ab66921 | 272 | LOG("Entering"); |
terencez | 0:1fa43ab66921 | 273 | switch (dataP->type) |
terencez | 0:1fa43ab66921 | 274 | { |
terencez | 0:1fa43ab66921 | 275 | case LWM2M_TYPE_FLOAT: |
terencez | 0:1fa43ab66921 | 276 | *valueP = dataP->value.asFloat; |
terencez | 0:1fa43ab66921 | 277 | result = 1; |
terencez | 0:1fa43ab66921 | 278 | break; |
terencez | 0:1fa43ab66921 | 279 | |
terencez | 0:1fa43ab66921 | 280 | case LWM2M_TYPE_INTEGER: |
terencez | 0:1fa43ab66921 | 281 | *valueP = (double)dataP->value.asInteger; |
terencez | 0:1fa43ab66921 | 282 | result = 1; |
terencez | 0:1fa43ab66921 | 283 | break; |
terencez | 0:1fa43ab66921 | 284 | |
terencez | 0:1fa43ab66921 | 285 | case LWM2M_TYPE_STRING: |
terencez | 0:1fa43ab66921 | 286 | result = utils_plainTextToFloat64(dataP->value.asBuffer.buffer, dataP->value.asBuffer.length, valueP); |
terencez | 0:1fa43ab66921 | 287 | break; |
terencez | 0:1fa43ab66921 | 288 | |
terencez | 0:1fa43ab66921 | 289 | case LWM2M_TYPE_OPAQUE: |
terencez | 0:1fa43ab66921 | 290 | result = utils_opaqueToFloat(dataP->value.asBuffer.buffer, dataP->value.asBuffer.length, valueP); |
terencez | 0:1fa43ab66921 | 291 | if (result == (int)dataP->value.asBuffer.length) |
terencez | 0:1fa43ab66921 | 292 | { |
terencez | 0:1fa43ab66921 | 293 | result = 1; |
terencez | 0:1fa43ab66921 | 294 | } |
terencez | 0:1fa43ab66921 | 295 | else |
terencez | 0:1fa43ab66921 | 296 | { |
terencez | 0:1fa43ab66921 | 297 | result = 0; |
terencez | 0:1fa43ab66921 | 298 | } |
terencez | 0:1fa43ab66921 | 299 | break; |
terencez | 0:1fa43ab66921 | 300 | |
terencez | 0:1fa43ab66921 | 301 | default: |
terencez | 0:1fa43ab66921 | 302 | return 0; |
terencez | 0:1fa43ab66921 | 303 | } |
terencez | 0:1fa43ab66921 | 304 | |
terencez | 0:1fa43ab66921 | 305 | LOG_ARG("result: %d, value: %f", result, *valueP); |
terencez | 0:1fa43ab66921 | 306 | |
terencez | 0:1fa43ab66921 | 307 | return result; |
terencez | 0:1fa43ab66921 | 308 | } |
terencez | 0:1fa43ab66921 | 309 | |
terencez | 0:1fa43ab66921 | 310 | void lwm2m_data_encode_bool(bool value, |
terencez | 0:1fa43ab66921 | 311 | lwm2m_data_t * dataP) |
terencez | 0:1fa43ab66921 | 312 | { |
terencez | 0:1fa43ab66921 | 313 | LOG_ARG("value: %s", value?"true":"false"); |
terencez | 0:1fa43ab66921 | 314 | dataP->type = LWM2M_TYPE_BOOLEAN; |
terencez | 0:1fa43ab66921 | 315 | dataP->value.asBoolean = value; |
terencez | 0:1fa43ab66921 | 316 | } |
terencez | 0:1fa43ab66921 | 317 | |
terencez | 0:1fa43ab66921 | 318 | int lwm2m_data_decode_bool(const lwm2m_data_t * dataP, |
terencez | 0:1fa43ab66921 | 319 | bool * valueP) |
terencez | 0:1fa43ab66921 | 320 | { |
terencez | 0:1fa43ab66921 | 321 | int result; |
terencez | 0:1fa43ab66921 | 322 | |
terencez | 0:1fa43ab66921 | 323 | LOG("Entering"); |
terencez | 0:1fa43ab66921 | 324 | switch (dataP->type) |
terencez | 0:1fa43ab66921 | 325 | { |
terencez | 0:1fa43ab66921 | 326 | case LWM2M_TYPE_BOOLEAN: |
terencez | 0:1fa43ab66921 | 327 | *valueP = dataP->value.asBoolean; |
terencez | 0:1fa43ab66921 | 328 | result = 1; |
terencez | 0:1fa43ab66921 | 329 | break; |
terencez | 0:1fa43ab66921 | 330 | |
terencez | 0:1fa43ab66921 | 331 | case LWM2M_TYPE_STRING: |
terencez | 0:1fa43ab66921 | 332 | if (dataP->value.asBuffer.length != 1) return 0; |
terencez | 0:1fa43ab66921 | 333 | |
terencez | 0:1fa43ab66921 | 334 | switch (dataP->value.asBuffer.buffer[0]) |
terencez | 0:1fa43ab66921 | 335 | { |
terencez | 0:1fa43ab66921 | 336 | case '0': |
terencez | 0:1fa43ab66921 | 337 | *valueP = false; |
terencez | 0:1fa43ab66921 | 338 | result = 1; |
terencez | 0:1fa43ab66921 | 339 | break; |
terencez | 0:1fa43ab66921 | 340 | case '1': |
terencez | 0:1fa43ab66921 | 341 | *valueP = true; |
terencez | 0:1fa43ab66921 | 342 | result = 1; |
terencez | 0:1fa43ab66921 | 343 | break; |
terencez | 0:1fa43ab66921 | 344 | default: |
terencez | 0:1fa43ab66921 | 345 | result = 0; |
terencez | 0:1fa43ab66921 | 346 | break; |
terencez | 0:1fa43ab66921 | 347 | } |
terencez | 0:1fa43ab66921 | 348 | break; |
terencez | 0:1fa43ab66921 | 349 | |
terencez | 0:1fa43ab66921 | 350 | case LWM2M_TYPE_OPAQUE: |
terencez | 0:1fa43ab66921 | 351 | if (dataP->value.asBuffer.length != 1) return 0; |
terencez | 0:1fa43ab66921 | 352 | |
terencez | 0:1fa43ab66921 | 353 | switch (dataP->value.asBuffer.buffer[0]) |
terencez | 0:1fa43ab66921 | 354 | { |
terencez | 0:1fa43ab66921 | 355 | case 0: |
terencez | 0:1fa43ab66921 | 356 | *valueP = false; |
terencez | 0:1fa43ab66921 | 357 | result = 1; |
terencez | 0:1fa43ab66921 | 358 | break; |
terencez | 0:1fa43ab66921 | 359 | case 1: |
terencez | 0:1fa43ab66921 | 360 | *valueP = true; |
terencez | 0:1fa43ab66921 | 361 | result = 1; |
terencez | 0:1fa43ab66921 | 362 | break; |
terencez | 0:1fa43ab66921 | 363 | default: |
terencez | 0:1fa43ab66921 | 364 | result = 0; |
terencez | 0:1fa43ab66921 | 365 | break; |
terencez | 0:1fa43ab66921 | 366 | } |
terencez | 0:1fa43ab66921 | 367 | break; |
terencez | 0:1fa43ab66921 | 368 | |
terencez | 0:1fa43ab66921 | 369 | default: |
terencez | 0:1fa43ab66921 | 370 | result = 0; |
terencez | 0:1fa43ab66921 | 371 | break; |
terencez | 0:1fa43ab66921 | 372 | } |
terencez | 0:1fa43ab66921 | 373 | |
terencez | 0:1fa43ab66921 | 374 | LOG_ARG("result: %d, value: %s", result, *valueP ? "true" : "false"); |
terencez | 0:1fa43ab66921 | 375 | |
terencez | 0:1fa43ab66921 | 376 | return result; |
terencez | 0:1fa43ab66921 | 377 | } |
terencez | 0:1fa43ab66921 | 378 | |
terencez | 0:1fa43ab66921 | 379 | void lwm2m_data_encode_objlink(uint16_t objectId, |
terencez | 0:1fa43ab66921 | 380 | uint16_t objectInstanceId, |
terencez | 0:1fa43ab66921 | 381 | lwm2m_data_t * dataP) |
terencez | 0:1fa43ab66921 | 382 | { |
terencez | 0:1fa43ab66921 | 383 | LOG_ARG("value: %d/%d", objectId, objectInstanceId); |
terencez | 0:1fa43ab66921 | 384 | dataP->type = LWM2M_TYPE_OBJECT_LINK; |
terencez | 0:1fa43ab66921 | 385 | dataP->value.asObjLink.objectId = objectId; |
terencez | 0:1fa43ab66921 | 386 | dataP->value.asObjLink.objectInstanceId = objectInstanceId; |
terencez | 0:1fa43ab66921 | 387 | } |
terencez | 0:1fa43ab66921 | 388 | |
terencez | 0:1fa43ab66921 | 389 | void lwm2m_data_include(lwm2m_data_t * subDataP, |
terencez | 0:1fa43ab66921 | 390 | size_t count, |
terencez | 0:1fa43ab66921 | 391 | lwm2m_data_t * dataP) |
terencez | 0:1fa43ab66921 | 392 | { |
terencez | 0:1fa43ab66921 | 393 | LOG_ARG("count: %d", count); |
terencez | 0:1fa43ab66921 | 394 | if (subDataP == NULL || count == 0) return; |
terencez | 0:1fa43ab66921 | 395 | |
terencez | 0:1fa43ab66921 | 396 | switch (subDataP[0].type) |
terencez | 0:1fa43ab66921 | 397 | { |
terencez | 0:1fa43ab66921 | 398 | case LWM2M_TYPE_STRING: |
terencez | 0:1fa43ab66921 | 399 | case LWM2M_TYPE_OPAQUE: |
terencez | 0:1fa43ab66921 | 400 | case LWM2M_TYPE_INTEGER: |
terencez | 0:1fa43ab66921 | 401 | case LWM2M_TYPE_FLOAT: |
terencez | 0:1fa43ab66921 | 402 | case LWM2M_TYPE_BOOLEAN: |
terencez | 0:1fa43ab66921 | 403 | case LWM2M_TYPE_OBJECT_LINK: |
terencez | 0:1fa43ab66921 | 404 | case LWM2M_TYPE_MULTIPLE_RESOURCE: |
terencez | 0:1fa43ab66921 | 405 | dataP->type = LWM2M_TYPE_OBJECT_INSTANCE; |
terencez | 0:1fa43ab66921 | 406 | break; |
terencez | 0:1fa43ab66921 | 407 | case LWM2M_TYPE_OBJECT_INSTANCE: |
terencez | 0:1fa43ab66921 | 408 | dataP->type = LWM2M_TYPE_OBJECT; |
terencez | 0:1fa43ab66921 | 409 | break; |
terencez | 0:1fa43ab66921 | 410 | default: |
terencez | 0:1fa43ab66921 | 411 | return; |
terencez | 0:1fa43ab66921 | 412 | } |
terencez | 0:1fa43ab66921 | 413 | dataP->value.asChildren.count = count; |
terencez | 0:1fa43ab66921 | 414 | dataP->value.asChildren.array = subDataP; |
terencez | 0:1fa43ab66921 | 415 | } |
terencez | 0:1fa43ab66921 | 416 | |
terencez | 0:1fa43ab66921 | 417 | void lwm2m_data_encode_instances(lwm2m_data_t * subDataP, |
terencez | 0:1fa43ab66921 | 418 | size_t count, |
terencez | 0:1fa43ab66921 | 419 | lwm2m_data_t * dataP) |
terencez | 0:1fa43ab66921 | 420 | { |
terencez | 0:1fa43ab66921 | 421 | LOG_ARG("count: %d", count); |
terencez | 0:1fa43ab66921 | 422 | lwm2m_data_include(subDataP, count, dataP); |
terencez | 0:1fa43ab66921 | 423 | dataP->type = LWM2M_TYPE_MULTIPLE_RESOURCE; |
terencez | 0:1fa43ab66921 | 424 | } |
terencez | 0:1fa43ab66921 | 425 | |
terencez | 0:1fa43ab66921 | 426 | int lwm2m_data_parse(lwm2m_uri_t * uriP, |
terencez | 0:1fa43ab66921 | 427 | uint8_t * buffer, |
terencez | 0:1fa43ab66921 | 428 | size_t bufferLen, |
terencez | 0:1fa43ab66921 | 429 | lwm2m_media_type_t format, |
terencez | 0:1fa43ab66921 | 430 | lwm2m_data_t ** dataP) |
terencez | 0:1fa43ab66921 | 431 | { |
terencez | 0:1fa43ab66921 | 432 | LOG_ARG("format: %s, bufferLen: %d", STR_MEDIA_TYPE(format), bufferLen); |
terencez | 0:1fa43ab66921 | 433 | LOG_URI(uriP); |
terencez | 0:1fa43ab66921 | 434 | switch (format) |
terencez | 0:1fa43ab66921 | 435 | { |
terencez | 0:1fa43ab66921 | 436 | case LWM2M_CONTENT_TEXT: |
terencez | 0:1fa43ab66921 | 437 | if (!LWM2M_URI_IS_SET_RESOURCE(uriP)) return 0; |
terencez | 0:1fa43ab66921 | 438 | *dataP = lwm2m_data_new(1); |
terencez | 0:1fa43ab66921 | 439 | if (*dataP == NULL) return 0; |
terencez | 0:1fa43ab66921 | 440 | (*dataP)->id = uriP->resourceId; |
terencez | 0:1fa43ab66921 | 441 | (*dataP)->type = LWM2M_TYPE_STRING; |
terencez | 0:1fa43ab66921 | 442 | return prv_setBuffer(*dataP, buffer, bufferLen); |
terencez | 0:1fa43ab66921 | 443 | |
terencez | 0:1fa43ab66921 | 444 | case LWM2M_CONTENT_OPAQUE: |
terencez | 0:1fa43ab66921 | 445 | if (!LWM2M_URI_IS_SET_RESOURCE(uriP)) return 0; |
terencez | 0:1fa43ab66921 | 446 | *dataP = lwm2m_data_new(1); |
terencez | 0:1fa43ab66921 | 447 | if (*dataP == NULL) return 0; |
terencez | 0:1fa43ab66921 | 448 | (*dataP)->id = uriP->resourceId; |
terencez | 0:1fa43ab66921 | 449 | (*dataP)->type = LWM2M_TYPE_OPAQUE; |
terencez | 0:1fa43ab66921 | 450 | return prv_setBuffer(*dataP, buffer, bufferLen); |
terencez | 0:1fa43ab66921 | 451 | |
terencez | 0:1fa43ab66921 | 452 | #ifdef LWM2M_OLD_CONTENT_FORMAT_SUPPORT |
terencez | 0:1fa43ab66921 | 453 | case LWM2M_CONTENT_TLV_OLD: |
terencez | 0:1fa43ab66921 | 454 | #endif |
terencez | 0:1fa43ab66921 | 455 | case LWM2M_CONTENT_TLV: |
terencez | 0:1fa43ab66921 | 456 | return tlv_parse(buffer, bufferLen, dataP); |
terencez | 0:1fa43ab66921 | 457 | |
terencez | 0:1fa43ab66921 | 458 | #ifdef LWM2M_SUPPORT_JSON |
terencez | 0:1fa43ab66921 | 459 | #ifdef LWM2M_OLD_CONTENT_FORMAT_SUPPORT |
terencez | 0:1fa43ab66921 | 460 | case LWM2M_CONTENT_JSON_OLD: |
terencez | 0:1fa43ab66921 | 461 | #endif |
terencez | 0:1fa43ab66921 | 462 | case LWM2M_CONTENT_JSON: |
terencez | 0:1fa43ab66921 | 463 | return json_parse(uriP, buffer, bufferLen, dataP); |
terencez | 0:1fa43ab66921 | 464 | #endif |
terencez | 0:1fa43ab66921 | 465 | |
terencez | 0:1fa43ab66921 | 466 | default: |
terencez | 0:1fa43ab66921 | 467 | return 0; |
terencez | 0:1fa43ab66921 | 468 | } |
terencez | 0:1fa43ab66921 | 469 | } |
terencez | 0:1fa43ab66921 | 470 | |
terencez | 0:1fa43ab66921 | 471 | int lwm2m_data_serialize(lwm2m_uri_t * uriP, |
terencez | 0:1fa43ab66921 | 472 | int size, |
terencez | 0:1fa43ab66921 | 473 | lwm2m_data_t * dataP, |
terencez | 0:1fa43ab66921 | 474 | lwm2m_media_type_t * formatP, |
terencez | 0:1fa43ab66921 | 475 | uint8_t ** bufferP) |
terencez | 0:1fa43ab66921 | 476 | { |
terencez | 0:1fa43ab66921 | 477 | LOG_URI(uriP); |
terencez | 0:1fa43ab66921 | 478 | LOG_ARG("size: %d, formatP: %s", size, STR_MEDIA_TYPE(*formatP)); |
terencez | 0:1fa43ab66921 | 479 | |
terencez | 0:1fa43ab66921 | 480 | // Check format |
terencez | 0:1fa43ab66921 | 481 | if (*formatP == LWM2M_CONTENT_TEXT |
terencez | 0:1fa43ab66921 | 482 | || *formatP == LWM2M_CONTENT_OPAQUE) |
terencez | 0:1fa43ab66921 | 483 | { |
terencez | 0:1fa43ab66921 | 484 | if (size != 1 |
terencez | 0:1fa43ab66921 | 485 | || (uriP != NULL && !LWM2M_URI_IS_SET_RESOURCE(uriP)) |
terencez | 0:1fa43ab66921 | 486 | || dataP->type == LWM2M_TYPE_OBJECT |
terencez | 0:1fa43ab66921 | 487 | || dataP->type == LWM2M_TYPE_OBJECT_INSTANCE |
terencez | 0:1fa43ab66921 | 488 | || dataP->type == LWM2M_TYPE_MULTIPLE_RESOURCE) |
terencez | 0:1fa43ab66921 | 489 | { |
terencez | 0:1fa43ab66921 | 490 | #ifdef LWM2M_SUPPORT_JSON |
terencez | 0:1fa43ab66921 | 491 | *formatP = LWM2M_CONTENT_JSON; |
terencez | 0:1fa43ab66921 | 492 | #else |
terencez | 0:1fa43ab66921 | 493 | *formatP = LWM2M_CONTENT_TLV; |
terencez | 0:1fa43ab66921 | 494 | #endif |
terencez | 0:1fa43ab66921 | 495 | } |
terencez | 0:1fa43ab66921 | 496 | } |
terencez | 0:1fa43ab66921 | 497 | |
terencez | 0:1fa43ab66921 | 498 | if (*formatP == LWM2M_CONTENT_TEXT |
terencez | 0:1fa43ab66921 | 499 | && dataP->type == LWM2M_TYPE_OPAQUE) |
terencez | 0:1fa43ab66921 | 500 | { |
terencez | 0:1fa43ab66921 | 501 | *formatP = LWM2M_CONTENT_OPAQUE; |
terencez | 0:1fa43ab66921 | 502 | } |
terencez | 0:1fa43ab66921 | 503 | LOG_ARG("Final format: %s", STR_MEDIA_TYPE(*formatP)); |
terencez | 0:1fa43ab66921 | 504 | |
terencez | 0:1fa43ab66921 | 505 | switch (*formatP) |
terencez | 0:1fa43ab66921 | 506 | { |
terencez | 0:1fa43ab66921 | 507 | case LWM2M_CONTENT_TEXT: |
terencez | 0:1fa43ab66921 | 508 | return prv_textSerialize(dataP, bufferP); |
terencez | 0:1fa43ab66921 | 509 | |
terencez | 0:1fa43ab66921 | 510 | case LWM2M_CONTENT_OPAQUE: |
terencez | 0:1fa43ab66921 | 511 | *bufferP = (uint8_t *)lwm2m_malloc(dataP->value.asBuffer.length); |
terencez | 0:1fa43ab66921 | 512 | if (*bufferP == NULL) return -1; |
terencez | 0:1fa43ab66921 | 513 | memcpy(*bufferP, dataP->value.asBuffer.buffer, dataP->value.asBuffer.length); |
terencez | 0:1fa43ab66921 | 514 | return (int)dataP->value.asBuffer.length; |
terencez | 0:1fa43ab66921 | 515 | |
terencez | 0:1fa43ab66921 | 516 | case LWM2M_CONTENT_TLV: |
terencez | 0:1fa43ab66921 | 517 | { |
terencez | 0:1fa43ab66921 | 518 | bool isResourceInstance; |
terencez | 0:1fa43ab66921 | 519 | |
terencez | 0:1fa43ab66921 | 520 | if (uriP != NULL && LWM2M_URI_IS_SET_RESOURCE(uriP) |
terencez | 0:1fa43ab66921 | 521 | && (size != 1 || dataP->id != uriP->resourceId)) |
terencez | 0:1fa43ab66921 | 522 | { |
terencez | 0:1fa43ab66921 | 523 | isResourceInstance = true; |
terencez | 0:1fa43ab66921 | 524 | } |
terencez | 0:1fa43ab66921 | 525 | else |
terencez | 0:1fa43ab66921 | 526 | { |
terencez | 0:1fa43ab66921 | 527 | isResourceInstance = false; |
terencez | 0:1fa43ab66921 | 528 | } |
terencez | 0:1fa43ab66921 | 529 | return tlv_serialize(isResourceInstance, size, dataP, bufferP); |
terencez | 0:1fa43ab66921 | 530 | } |
terencez | 0:1fa43ab66921 | 531 | |
terencez | 0:1fa43ab66921 | 532 | #ifdef LWM2M_CLIENT_MODE |
terencez | 0:1fa43ab66921 | 533 | case LWM2M_CONTENT_LINK: |
terencez | 0:1fa43ab66921 | 534 | return discover_serialize(NULL, uriP, NULL, size, dataP, bufferP); |
terencez | 0:1fa43ab66921 | 535 | #endif |
terencez | 0:1fa43ab66921 | 536 | #ifdef LWM2M_SUPPORT_JSON |
terencez | 0:1fa43ab66921 | 537 | case LWM2M_CONTENT_JSON: |
terencez | 0:1fa43ab66921 | 538 | return json_serialize(uriP, size, dataP, bufferP); |
terencez | 0:1fa43ab66921 | 539 | #endif |
terencez | 0:1fa43ab66921 | 540 | |
terencez | 0:1fa43ab66921 | 541 | default: |
terencez | 0:1fa43ab66921 | 542 | return -1; |
terencez | 0:1fa43ab66921 | 543 | } |
terencez | 0:1fa43ab66921 | 544 | } |
terencez | 0:1fa43ab66921 | 545 |