pick up wakaama files from https://github.com/eclipse/wakaama

Committer:
terencez
Date:
Wed Apr 19 11:27:34 2017 +0000
Revision:
0:c2dff8cbb91a
Initial commit

Who changed what in which revision?

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