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

Committer:
terencez
Date:
Wed Apr 19 11:30:02 2017 +0000
Revision:
0:1fa43ab66921
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew 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 * Toby Jaffey - Please refer to git log
terencez 0:1fa43ab66921 17 * Bosch Software Innovations GmbH - Please refer to git log
terencez 0:1fa43ab66921 18 * Pascal Rieux - Please refer to git log
terencez 0:1fa43ab66921 19 *
terencez 0:1fa43ab66921 20 *******************************************************************************/
terencez 0:1fa43ab66921 21
terencez 0:1fa43ab66921 22 /*
terencez 0:1fa43ab66921 23 Copyright (c) 2013, 2014 Intel Corporation
terencez 0:1fa43ab66921 24
terencez 0:1fa43ab66921 25 Redistribution and use in source and binary forms, with or without modification,
terencez 0:1fa43ab66921 26 are permitted provided that the following conditions are met:
terencez 0:1fa43ab66921 27
terencez 0:1fa43ab66921 28 * Redistributions of source code must retain the above copyright notice,
terencez 0:1fa43ab66921 29 this list of conditions and the following disclaimer.
terencez 0:1fa43ab66921 30 * Redistributions in binary form must reproduce the above copyright notice,
terencez 0:1fa43ab66921 31 this list of conditions and the following disclaimer in the documentation
terencez 0:1fa43ab66921 32 and/or other materials provided with the distribution.
terencez 0:1fa43ab66921 33 * Neither the name of Intel Corporation nor the names of its contributors
terencez 0:1fa43ab66921 34 may be used to endorse or promote products derived from this software
terencez 0:1fa43ab66921 35 without specific prior written permission.
terencez 0:1fa43ab66921 36
terencez 0:1fa43ab66921 37 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
terencez 0:1fa43ab66921 38 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
terencez 0:1fa43ab66921 39 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
terencez 0:1fa43ab66921 40 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
terencez 0:1fa43ab66921 41 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
terencez 0:1fa43ab66921 42 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
terencez 0:1fa43ab66921 43 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
terencez 0:1fa43ab66921 44 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
terencez 0:1fa43ab66921 45 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
terencez 0:1fa43ab66921 46 THE POSSIBILITY OF SUCH DAMAGE.
terencez 0:1fa43ab66921 47
terencez 0:1fa43ab66921 48 David Navarro <david.navarro@intel.com>
terencez 0:1fa43ab66921 49
terencez 0:1fa43ab66921 50 */
terencez 0:1fa43ab66921 51
terencez 0:1fa43ab66921 52 #include "internals.h"
terencez 0:1fa43ab66921 53 #include <stdlib.h>
terencez 0:1fa43ab66921 54 #include <string.h>
terencez 0:1fa43ab66921 55 #include <ctype.h>
terencez 0:1fa43ab66921 56
terencez 0:1fa43ab66921 57
terencez 0:1fa43ab66921 58 static int prv_parseNumber(uint8_t * uriString,
terencez 0:1fa43ab66921 59 size_t uriLength,
terencez 0:1fa43ab66921 60 size_t * headP)
terencez 0:1fa43ab66921 61 {
terencez 0:1fa43ab66921 62 int result = 0;
terencez 0:1fa43ab66921 63
terencez 0:1fa43ab66921 64 if (uriString[*headP] == '/')
terencez 0:1fa43ab66921 65 {
terencez 0:1fa43ab66921 66 // empty Object Instance ID with resource ID is not allowed
terencez 0:1fa43ab66921 67 return -1;
terencez 0:1fa43ab66921 68 }
terencez 0:1fa43ab66921 69 while (*headP < uriLength && uriString[*headP] != '/')
terencez 0:1fa43ab66921 70 {
terencez 0:1fa43ab66921 71 if ('0' <= uriString[*headP] && uriString[*headP] <= '9')
terencez 0:1fa43ab66921 72 {
terencez 0:1fa43ab66921 73 result += uriString[*headP] - '0';
terencez 0:1fa43ab66921 74 result *= 10;
terencez 0:1fa43ab66921 75 }
terencez 0:1fa43ab66921 76 else
terencez 0:1fa43ab66921 77 {
terencez 0:1fa43ab66921 78 return -1;
terencez 0:1fa43ab66921 79 }
terencez 0:1fa43ab66921 80 *headP += 1;
terencez 0:1fa43ab66921 81 }
terencez 0:1fa43ab66921 82
terencez 0:1fa43ab66921 83 result /= 10;
terencez 0:1fa43ab66921 84 return result;
terencez 0:1fa43ab66921 85 }
terencez 0:1fa43ab66921 86
terencez 0:1fa43ab66921 87
terencez 0:1fa43ab66921 88 int uri_getNumber(uint8_t * uriString,
terencez 0:1fa43ab66921 89 size_t uriLength)
terencez 0:1fa43ab66921 90 {
terencez 0:1fa43ab66921 91 size_t index = 0;
terencez 0:1fa43ab66921 92
terencez 0:1fa43ab66921 93 return prv_parseNumber(uriString, uriLength, &index);
terencez 0:1fa43ab66921 94 }
terencez 0:1fa43ab66921 95
terencez 0:1fa43ab66921 96
terencez 0:1fa43ab66921 97 lwm2m_uri_t * uri_decode(char * altPath,
terencez 0:1fa43ab66921 98 multi_option_t *uriPath)
terencez 0:1fa43ab66921 99 {
terencez 0:1fa43ab66921 100 lwm2m_uri_t * uriP;
terencez 0:1fa43ab66921 101 int readNum;
terencez 0:1fa43ab66921 102
terencez 0:1fa43ab66921 103 LOG_ARG("altPath: \"%s\"", altPath);
terencez 0:1fa43ab66921 104
terencez 0:1fa43ab66921 105 uriP = (lwm2m_uri_t *)lwm2m_malloc(sizeof(lwm2m_uri_t));
terencez 0:1fa43ab66921 106 if (NULL == uriP) return NULL;
terencez 0:1fa43ab66921 107
terencez 0:1fa43ab66921 108 memset(uriP, 0, sizeof(lwm2m_uri_t));
terencez 0:1fa43ab66921 109
terencez 0:1fa43ab66921 110 // Read object ID
terencez 0:1fa43ab66921 111 if (NULL != uriPath
terencez 0:1fa43ab66921 112 && URI_REGISTRATION_SEGMENT_LEN == uriPath->len
terencez 0:1fa43ab66921 113 && 0 == strncmp(URI_REGISTRATION_SEGMENT, (char *)uriPath->data, uriPath->len))
terencez 0:1fa43ab66921 114 {
terencez 0:1fa43ab66921 115 uriP->flag |= LWM2M_URI_FLAG_REGISTRATION;
terencez 0:1fa43ab66921 116 uriPath = uriPath->next;
terencez 0:1fa43ab66921 117 if (uriPath == NULL) return uriP;
terencez 0:1fa43ab66921 118 }
terencez 0:1fa43ab66921 119 else if (NULL != uriPath
terencez 0:1fa43ab66921 120 && URI_BOOTSTRAP_SEGMENT_LEN == uriPath->len
terencez 0:1fa43ab66921 121 && 0 == strncmp(URI_BOOTSTRAP_SEGMENT, (char *)uriPath->data, uriPath->len))
terencez 0:1fa43ab66921 122 {
terencez 0:1fa43ab66921 123 uriP->flag |= LWM2M_URI_FLAG_BOOTSTRAP;
terencez 0:1fa43ab66921 124 uriPath = uriPath->next;
terencez 0:1fa43ab66921 125 if (uriPath != NULL) goto error;
terencez 0:1fa43ab66921 126 return uriP;
terencez 0:1fa43ab66921 127 }
terencez 0:1fa43ab66921 128
terencez 0:1fa43ab66921 129 if ((uriP->flag & LWM2M_URI_MASK_TYPE) != LWM2M_URI_FLAG_REGISTRATION)
terencez 0:1fa43ab66921 130 {
terencez 0:1fa43ab66921 131 // Read altPath if any
terencez 0:1fa43ab66921 132 if (altPath != NULL)
terencez 0:1fa43ab66921 133 {
terencez 0:1fa43ab66921 134 int i;
terencez 0:1fa43ab66921 135 if (NULL == uriPath)
terencez 0:1fa43ab66921 136 {
terencez 0:1fa43ab66921 137 lwm2m_free(uriP);
terencez 0:1fa43ab66921 138 return NULL;
terencez 0:1fa43ab66921 139 }
terencez 0:1fa43ab66921 140 for (i = 0 ; i < uriPath->len ; i++)
terencez 0:1fa43ab66921 141 {
terencez 0:1fa43ab66921 142 if (uriPath->data[i] != altPath[i+1])
terencez 0:1fa43ab66921 143 {
terencez 0:1fa43ab66921 144 lwm2m_free(uriP);
terencez 0:1fa43ab66921 145 return NULL;
terencez 0:1fa43ab66921 146 }
terencez 0:1fa43ab66921 147 }
terencez 0:1fa43ab66921 148 uriPath = uriPath->next;
terencez 0:1fa43ab66921 149 }
terencez 0:1fa43ab66921 150 if (NULL == uriPath || uriPath->len == 0)
terencez 0:1fa43ab66921 151 {
terencez 0:1fa43ab66921 152 uriP->flag |= LWM2M_URI_FLAG_DELETE_ALL;
terencez 0:1fa43ab66921 153 return uriP;
terencez 0:1fa43ab66921 154 }
terencez 0:1fa43ab66921 155 }
terencez 0:1fa43ab66921 156
terencez 0:1fa43ab66921 157 readNum = uri_getNumber(uriPath->data, uriPath->len);
terencez 0:1fa43ab66921 158 if (readNum < 0 || readNum > LWM2M_MAX_ID) goto error;
terencez 0:1fa43ab66921 159 uriP->objectId = (uint16_t)readNum;
terencez 0:1fa43ab66921 160 uriP->flag |= LWM2M_URI_FLAG_OBJECT_ID;
terencez 0:1fa43ab66921 161 uriPath = uriPath->next;
terencez 0:1fa43ab66921 162
terencez 0:1fa43ab66921 163 if ((uriP->flag & LWM2M_URI_MASK_TYPE) == LWM2M_URI_FLAG_REGISTRATION)
terencez 0:1fa43ab66921 164 {
terencez 0:1fa43ab66921 165 if (uriPath != NULL) goto error;
terencez 0:1fa43ab66921 166 return uriP;
terencez 0:1fa43ab66921 167 }
terencez 0:1fa43ab66921 168 uriP->flag |= LWM2M_URI_FLAG_DM;
terencez 0:1fa43ab66921 169
terencez 0:1fa43ab66921 170 if (uriPath == NULL) return uriP;
terencez 0:1fa43ab66921 171
terencez 0:1fa43ab66921 172 // Read object instance
terencez 0:1fa43ab66921 173 if (uriPath->len != 0)
terencez 0:1fa43ab66921 174 {
terencez 0:1fa43ab66921 175 readNum = uri_getNumber(uriPath->data, uriPath->len);
terencez 0:1fa43ab66921 176 if (readNum < 0 || readNum >= LWM2M_MAX_ID) goto error;
terencez 0:1fa43ab66921 177 uriP->instanceId = (uint16_t)readNum;
terencez 0:1fa43ab66921 178 uriP->flag |= LWM2M_URI_FLAG_INSTANCE_ID;
terencez 0:1fa43ab66921 179 }
terencez 0:1fa43ab66921 180 uriPath = uriPath->next;
terencez 0:1fa43ab66921 181
terencez 0:1fa43ab66921 182 if (uriPath == NULL) return uriP;
terencez 0:1fa43ab66921 183
terencez 0:1fa43ab66921 184 // Read resource ID
terencez 0:1fa43ab66921 185 if (uriPath->len != 0)
terencez 0:1fa43ab66921 186 {
terencez 0:1fa43ab66921 187 // resource ID without an instance ID is not allowed
terencez 0:1fa43ab66921 188 if ((uriP->flag & LWM2M_URI_FLAG_INSTANCE_ID) == 0) goto error;
terencez 0:1fa43ab66921 189
terencez 0:1fa43ab66921 190 readNum = uri_getNumber(uriPath->data, uriPath->len);
terencez 0:1fa43ab66921 191 if (readNum < 0 || readNum > LWM2M_MAX_ID) goto error;
terencez 0:1fa43ab66921 192 uriP->resourceId = (uint16_t)readNum;
terencez 0:1fa43ab66921 193 uriP->flag |= LWM2M_URI_FLAG_RESOURCE_ID;
terencez 0:1fa43ab66921 194 }
terencez 0:1fa43ab66921 195
terencez 0:1fa43ab66921 196 // must be the last segment
terencez 0:1fa43ab66921 197 if (NULL == uriPath->next)
terencez 0:1fa43ab66921 198 {
terencez 0:1fa43ab66921 199 LOG_URI(uriP);
terencez 0:1fa43ab66921 200 return uriP;
terencez 0:1fa43ab66921 201 }
terencez 0:1fa43ab66921 202
terencez 0:1fa43ab66921 203 error:
terencez 0:1fa43ab66921 204 LOG("Exiting on error");
terencez 0:1fa43ab66921 205 lwm2m_free(uriP);
terencez 0:1fa43ab66921 206 return NULL;
terencez 0:1fa43ab66921 207 }
terencez 0:1fa43ab66921 208
terencez 0:1fa43ab66921 209 int lwm2m_stringToUri(const char * buffer,
terencez 0:1fa43ab66921 210 size_t buffer_len,
terencez 0:1fa43ab66921 211 lwm2m_uri_t * uriP)
terencez 0:1fa43ab66921 212 {
terencez 0:1fa43ab66921 213 size_t head;
terencez 0:1fa43ab66921 214 int readNum;
terencez 0:1fa43ab66921 215
terencez 0:1fa43ab66921 216 LOG_ARG("buffer_len: %u, buffer: \"%.*s\"", buffer_len, buffer_len, buffer);
terencez 0:1fa43ab66921 217
terencez 0:1fa43ab66921 218 if (buffer == NULL || buffer_len == 0 || uriP == NULL) return 0;
terencez 0:1fa43ab66921 219
terencez 0:1fa43ab66921 220 memset(uriP, 0, sizeof(lwm2m_uri_t));
terencez 0:1fa43ab66921 221
terencez 0:1fa43ab66921 222 // Skip any white space
terencez 0:1fa43ab66921 223 head = 0;
terencez 0:1fa43ab66921 224 while (head < buffer_len && isspace(buffer[head]&0xFF))
terencez 0:1fa43ab66921 225 {
terencez 0:1fa43ab66921 226 head++;
terencez 0:1fa43ab66921 227 }
terencez 0:1fa43ab66921 228 if (head == buffer_len) return 0;
terencez 0:1fa43ab66921 229
terencez 0:1fa43ab66921 230 // Check the URI start with a '/'
terencez 0:1fa43ab66921 231 if (buffer[head] != '/') return 0;
terencez 0:1fa43ab66921 232 head++;
terencez 0:1fa43ab66921 233 if (head == buffer_len) return 0;
terencez 0:1fa43ab66921 234
terencez 0:1fa43ab66921 235 // Read object ID
terencez 0:1fa43ab66921 236 readNum = prv_parseNumber((uint8_t *)buffer, buffer_len, &head);
terencez 0:1fa43ab66921 237 if (readNum < 0 || readNum > LWM2M_MAX_ID) return 0;
terencez 0:1fa43ab66921 238 uriP->objectId = (uint16_t)readNum;
terencez 0:1fa43ab66921 239 uriP->flag |= LWM2M_URI_FLAG_OBJECT_ID;
terencez 0:1fa43ab66921 240
terencez 0:1fa43ab66921 241 if (buffer[head] == '/') head += 1;
terencez 0:1fa43ab66921 242 if (head >= buffer_len)
terencez 0:1fa43ab66921 243 {
terencez 0:1fa43ab66921 244 LOG_ARG("Parsed characters: %u", head);
terencez 0:1fa43ab66921 245 LOG_URI(uriP);
terencez 0:1fa43ab66921 246 return head;
terencez 0:1fa43ab66921 247 }
terencez 0:1fa43ab66921 248
terencez 0:1fa43ab66921 249 readNum = prv_parseNumber((uint8_t *)buffer, buffer_len, &head);
terencez 0:1fa43ab66921 250 if (readNum < 0 || readNum >= LWM2M_MAX_ID) return 0;
terencez 0:1fa43ab66921 251 uriP->instanceId = (uint16_t)readNum;
terencez 0:1fa43ab66921 252 uriP->flag |= LWM2M_URI_FLAG_INSTANCE_ID;
terencez 0:1fa43ab66921 253
terencez 0:1fa43ab66921 254 if (buffer[head] == '/') head += 1;
terencez 0:1fa43ab66921 255 if (head >= buffer_len)
terencez 0:1fa43ab66921 256 {
terencez 0:1fa43ab66921 257 LOG_ARG("Parsed characters: %u", head);
terencez 0:1fa43ab66921 258 LOG_URI(uriP);
terencez 0:1fa43ab66921 259 return head;
terencez 0:1fa43ab66921 260 }
terencez 0:1fa43ab66921 261
terencez 0:1fa43ab66921 262 readNum = prv_parseNumber((uint8_t *)buffer, buffer_len, &head);
terencez 0:1fa43ab66921 263 if (readNum < 0 || readNum >= LWM2M_MAX_ID) return 0;
terencez 0:1fa43ab66921 264 uriP->resourceId = (uint16_t)readNum;
terencez 0:1fa43ab66921 265 uriP->flag |= LWM2M_URI_FLAG_RESOURCE_ID;
terencez 0:1fa43ab66921 266
terencez 0:1fa43ab66921 267 if (head != buffer_len) return 0;
terencez 0:1fa43ab66921 268
terencez 0:1fa43ab66921 269 LOG_ARG("Parsed characters: %u", head);
terencez 0:1fa43ab66921 270 LOG_URI(uriP);
terencez 0:1fa43ab66921 271
terencez 0:1fa43ab66921 272 return head;
terencez 0:1fa43ab66921 273 }
terencez 0:1fa43ab66921 274
terencez 0:1fa43ab66921 275 int uri_toString(lwm2m_uri_t * uriP,
terencez 0:1fa43ab66921 276 uint8_t * buffer,
terencez 0:1fa43ab66921 277 size_t bufferLen,
terencez 0:1fa43ab66921 278 uri_depth_t * depthP)
terencez 0:1fa43ab66921 279 {
terencez 0:1fa43ab66921 280 size_t head;
terencez 0:1fa43ab66921 281 int res;
terencez 0:1fa43ab66921 282
terencez 0:1fa43ab66921 283 LOG_ARG("bufferLen: %u", bufferLen);
terencez 0:1fa43ab66921 284 LOG_URI(uriP);
terencez 0:1fa43ab66921 285
terencez 0:1fa43ab66921 286 buffer[0] = '/';
terencez 0:1fa43ab66921 287
terencez 0:1fa43ab66921 288 if (uriP == NULL)
terencez 0:1fa43ab66921 289 {
terencez 0:1fa43ab66921 290 if (depthP) *depthP = URI_DEPTH_OBJECT;
terencez 0:1fa43ab66921 291 return 1;
terencez 0:1fa43ab66921 292 }
terencez 0:1fa43ab66921 293
terencez 0:1fa43ab66921 294 head = 1;
terencez 0:1fa43ab66921 295
terencez 0:1fa43ab66921 296 res = utils_intToText(uriP->objectId, buffer + head, bufferLen - head);
terencez 0:1fa43ab66921 297 if (res <= 0) return -1;
terencez 0:1fa43ab66921 298 head += res;
terencez 0:1fa43ab66921 299 if (head >= bufferLen - 1) return -1;
terencez 0:1fa43ab66921 300 if (depthP) *depthP = URI_DEPTH_OBJECT_INSTANCE;
terencez 0:1fa43ab66921 301
terencez 0:1fa43ab66921 302 if (LWM2M_URI_IS_SET_INSTANCE(uriP))
terencez 0:1fa43ab66921 303 {
terencez 0:1fa43ab66921 304 buffer[head] = '/';
terencez 0:1fa43ab66921 305 head++;
terencez 0:1fa43ab66921 306 res = utils_intToText(uriP->instanceId, buffer + head, bufferLen - head);
terencez 0:1fa43ab66921 307 if (res <= 0) return -1;
terencez 0:1fa43ab66921 308 head += res;
terencez 0:1fa43ab66921 309 if (head >= bufferLen - 1) return -1;
terencez 0:1fa43ab66921 310 if (depthP) *depthP = URI_DEPTH_RESOURCE;
terencez 0:1fa43ab66921 311 if (LWM2M_URI_IS_SET_RESOURCE(uriP))
terencez 0:1fa43ab66921 312 {
terencez 0:1fa43ab66921 313 buffer[head] = '/';
terencez 0:1fa43ab66921 314 head++;
terencez 0:1fa43ab66921 315 res = utils_intToText(uriP->resourceId, buffer + head, bufferLen - head);
terencez 0:1fa43ab66921 316 if (res <= 0) return -1;
terencez 0:1fa43ab66921 317 head += res;
terencez 0:1fa43ab66921 318 if (head >= bufferLen - 1) return -1;
terencez 0:1fa43ab66921 319 if (depthP) *depthP = URI_DEPTH_RESOURCE_INSTANCE;
terencez 0:1fa43ab66921 320 }
terencez 0:1fa43ab66921 321 }
terencez 0:1fa43ab66921 322
terencez 0:1fa43ab66921 323 buffer[head] = '/';
terencez 0:1fa43ab66921 324 head++;
terencez 0:1fa43ab66921 325
terencez 0:1fa43ab66921 326 LOG_ARG("length: %u, buffer: \"%.*s\"", head, head, buffer);
terencez 0:1fa43ab66921 327
terencez 0:1fa43ab66921 328 return head;
terencez 0:1fa43ab66921 329 }