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 * Benjamin Cabé - Please refer to git log
terencez 0:1fa43ab66921 18 * Bosch Software Innovations GmbH - Please refer to git log
terencez 0:1fa43ab66921 19 * Pascal Rieux - Please refer to git log
terencez 0:1fa43ab66921 20 *
terencez 0:1fa43ab66921 21 *******************************************************************************/
terencez 0:1fa43ab66921 22
terencez 0:1fa43ab66921 23 /*
terencez 0:1fa43ab66921 24 Copyright (c) 2013, 2014 Intel Corporation
terencez 0:1fa43ab66921 25
terencez 0:1fa43ab66921 26 Redistribution and use in source and binary forms, with or without modification,
terencez 0:1fa43ab66921 27 are permitted provided that the following conditions are met:
terencez 0:1fa43ab66921 28
terencez 0:1fa43ab66921 29 * Redistributions of source code must retain the above copyright notice,
terencez 0:1fa43ab66921 30 this list of conditions and the following disclaimer.
terencez 0:1fa43ab66921 31 * Redistributions in binary form must reproduce the above copyright notice,
terencez 0:1fa43ab66921 32 this list of conditions and the following disclaimer in the documentation
terencez 0:1fa43ab66921 33 and/or other materials provided with the distribution.
terencez 0:1fa43ab66921 34 * Neither the name of Intel Corporation nor the names of its contributors
terencez 0:1fa43ab66921 35 may be used to endorse or promote products derived from this software
terencez 0:1fa43ab66921 36 without specific prior written permission.
terencez 0:1fa43ab66921 37
terencez 0:1fa43ab66921 38 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
terencez 0:1fa43ab66921 39 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
terencez 0:1fa43ab66921 40 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
terencez 0:1fa43ab66921 41 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
terencez 0:1fa43ab66921 42 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
terencez 0:1fa43ab66921 43 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
terencez 0:1fa43ab66921 44 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
terencez 0:1fa43ab66921 45 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
terencez 0:1fa43ab66921 46 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
terencez 0:1fa43ab66921 47 THE POSSIBILITY OF SUCH DAMAGE.
terencez 0:1fa43ab66921 48
terencez 0:1fa43ab66921 49 David Navarro <david.navarro@intel.com>
terencez 0:1fa43ab66921 50
terencez 0:1fa43ab66921 51 */
terencez 0:1fa43ab66921 52 #include "internals.h"
terencez 0:1fa43ab66921 53
terencez 0:1fa43ab66921 54 #ifdef LWM2M_CLIENT_MODE
terencez 0:1fa43ab66921 55
terencez 0:1fa43ab66921 56
terencez 0:1fa43ab66921 57 #include <stdlib.h>
terencez 0:1fa43ab66921 58 #include <string.h>
terencez 0:1fa43ab66921 59 #include <stdio.h>
terencez 0:1fa43ab66921 60
terencez 0:1fa43ab66921 61
terencez 0:1fa43ab66921 62 uint8_t object_checkReadable(lwm2m_context_t * contextP,
terencez 0:1fa43ab66921 63 lwm2m_uri_t * uriP)
terencez 0:1fa43ab66921 64 {
terencez 0:1fa43ab66921 65 coap_status_t result;
terencez 0:1fa43ab66921 66 lwm2m_object_t * targetP;
terencez 0:1fa43ab66921 67 lwm2m_data_t * dataP = NULL;
terencez 0:1fa43ab66921 68 int size;
terencez 0:1fa43ab66921 69
terencez 0:1fa43ab66921 70 LOG_URI(uriP);
terencez 0:1fa43ab66921 71 targetP = (lwm2m_object_t *)LWM2M_LIST_FIND(contextP->objectList, uriP->objectId);
terencez 0:1fa43ab66921 72 if (NULL == targetP) return COAP_404_NOT_FOUND;
terencez 0:1fa43ab66921 73 if (NULL == targetP->readFunc) return COAP_405_METHOD_NOT_ALLOWED;
terencez 0:1fa43ab66921 74
terencez 0:1fa43ab66921 75 if (!LWM2M_URI_IS_SET_INSTANCE(uriP)) return COAP_205_CONTENT;
terencez 0:1fa43ab66921 76
terencez 0:1fa43ab66921 77 if (NULL == lwm2m_list_find(targetP->instanceList, uriP->instanceId)) return COAP_404_NOT_FOUND;
terencez 0:1fa43ab66921 78
terencez 0:1fa43ab66921 79 if (!LWM2M_URI_IS_SET_RESOURCE(uriP)) return COAP_205_CONTENT;
terencez 0:1fa43ab66921 80
terencez 0:1fa43ab66921 81 size = 1;
terencez 0:1fa43ab66921 82 dataP = lwm2m_data_new(1);
terencez 0:1fa43ab66921 83 if (dataP == NULL) return COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:1fa43ab66921 84
terencez 0:1fa43ab66921 85 dataP->id = uriP->resourceId;
terencez 0:1fa43ab66921 86
terencez 0:1fa43ab66921 87 result = targetP->readFunc(uriP->instanceId, &size, &dataP, targetP);
terencez 0:1fa43ab66921 88 lwm2m_data_free(1, dataP);
terencez 0:1fa43ab66921 89
terencez 0:1fa43ab66921 90 return result;
terencez 0:1fa43ab66921 91 }
terencez 0:1fa43ab66921 92
terencez 0:1fa43ab66921 93 uint8_t object_checkNumeric(lwm2m_context_t * contextP,
terencez 0:1fa43ab66921 94 lwm2m_uri_t * uriP)
terencez 0:1fa43ab66921 95 {
terencez 0:1fa43ab66921 96 coap_status_t result;
terencez 0:1fa43ab66921 97 lwm2m_object_t * targetP;
terencez 0:1fa43ab66921 98 lwm2m_data_t * dataP = NULL;
terencez 0:1fa43ab66921 99 int size;
terencez 0:1fa43ab66921 100
terencez 0:1fa43ab66921 101 LOG_URI(uriP);
terencez 0:1fa43ab66921 102 if (!LWM2M_URI_IS_SET_RESOURCE(uriP)) return COAP_405_METHOD_NOT_ALLOWED;
terencez 0:1fa43ab66921 103
terencez 0:1fa43ab66921 104 targetP = (lwm2m_object_t *)LWM2M_LIST_FIND(contextP->objectList, uriP->objectId);
terencez 0:1fa43ab66921 105 if (NULL == targetP) return COAP_404_NOT_FOUND;
terencez 0:1fa43ab66921 106 if (NULL == targetP->readFunc) return COAP_405_METHOD_NOT_ALLOWED;
terencez 0:1fa43ab66921 107
terencez 0:1fa43ab66921 108 size = 1;
terencez 0:1fa43ab66921 109 dataP = lwm2m_data_new(1);
terencez 0:1fa43ab66921 110 if (dataP == NULL) return COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:1fa43ab66921 111
terencez 0:1fa43ab66921 112 dataP->id = uriP->resourceId;
terencez 0:1fa43ab66921 113
terencez 0:1fa43ab66921 114 result = targetP->readFunc(uriP->instanceId, &size, &dataP, targetP);
terencez 0:1fa43ab66921 115 if (result == COAP_205_CONTENT)
terencez 0:1fa43ab66921 116 {
terencez 0:1fa43ab66921 117 switch (dataP->type)
terencez 0:1fa43ab66921 118 {
terencez 0:1fa43ab66921 119 case LWM2M_TYPE_INTEGER:
terencez 0:1fa43ab66921 120 case LWM2M_TYPE_FLOAT:
terencez 0:1fa43ab66921 121 break;
terencez 0:1fa43ab66921 122 default:
terencez 0:1fa43ab66921 123 result = COAP_405_METHOD_NOT_ALLOWED;
terencez 0:1fa43ab66921 124 }
terencez 0:1fa43ab66921 125 }
terencez 0:1fa43ab66921 126
terencez 0:1fa43ab66921 127 lwm2m_data_free(1, dataP);
terencez 0:1fa43ab66921 128
terencez 0:1fa43ab66921 129 return result;
terencez 0:1fa43ab66921 130 }
terencez 0:1fa43ab66921 131
terencez 0:1fa43ab66921 132 coap_status_t object_readData(lwm2m_context_t * contextP,
terencez 0:1fa43ab66921 133 lwm2m_uri_t * uriP,
terencez 0:1fa43ab66921 134 int * sizeP,
terencez 0:1fa43ab66921 135 lwm2m_data_t ** dataP)
terencez 0:1fa43ab66921 136 {
terencez 0:1fa43ab66921 137 coap_status_t result;
terencez 0:1fa43ab66921 138 lwm2m_object_t * targetP;
terencez 0:1fa43ab66921 139
terencez 0:1fa43ab66921 140 LOG_URI(uriP);
terencez 0:1fa43ab66921 141 targetP = (lwm2m_object_t *)LWM2M_LIST_FIND(contextP->objectList, uriP->objectId);
terencez 0:1fa43ab66921 142 if (NULL == targetP) return COAP_404_NOT_FOUND;
terencez 0:1fa43ab66921 143 if (NULL == targetP->readFunc) return COAP_405_METHOD_NOT_ALLOWED;
terencez 0:1fa43ab66921 144
terencez 0:1fa43ab66921 145 if (LWM2M_URI_IS_SET_INSTANCE(uriP))
terencez 0:1fa43ab66921 146 {
terencez 0:1fa43ab66921 147 if (NULL == lwm2m_list_find(targetP->instanceList, uriP->instanceId)) return COAP_404_NOT_FOUND;
terencez 0:1fa43ab66921 148
terencez 0:1fa43ab66921 149 // single instance read
terencez 0:1fa43ab66921 150 if (LWM2M_URI_IS_SET_RESOURCE(uriP))
terencez 0:1fa43ab66921 151 {
terencez 0:1fa43ab66921 152 *sizeP = 1;
terencez 0:1fa43ab66921 153 *dataP = lwm2m_data_new(*sizeP);
terencez 0:1fa43ab66921 154 if (*dataP == NULL) return COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:1fa43ab66921 155
terencez 0:1fa43ab66921 156 (*dataP)->id = uriP->resourceId;
terencez 0:1fa43ab66921 157 }
terencez 0:1fa43ab66921 158
terencez 0:1fa43ab66921 159 result = targetP->readFunc(uriP->instanceId, sizeP, dataP, targetP);
terencez 0:1fa43ab66921 160 }
terencez 0:1fa43ab66921 161 else
terencez 0:1fa43ab66921 162 {
terencez 0:1fa43ab66921 163 // multiple object instances read
terencez 0:1fa43ab66921 164 lwm2m_list_t * instanceP;
terencez 0:1fa43ab66921 165 int i;
terencez 0:1fa43ab66921 166
terencez 0:1fa43ab66921 167 result = COAP_205_CONTENT;
terencez 0:1fa43ab66921 168
terencez 0:1fa43ab66921 169 *sizeP = 0;
terencez 0:1fa43ab66921 170 for (instanceP = targetP->instanceList; instanceP != NULL ; instanceP = instanceP->next)
terencez 0:1fa43ab66921 171 {
terencez 0:1fa43ab66921 172 (*sizeP)++;
terencez 0:1fa43ab66921 173 }
terencez 0:1fa43ab66921 174
terencez 0:1fa43ab66921 175 if (*sizeP == 0)
terencez 0:1fa43ab66921 176 {
terencez 0:1fa43ab66921 177 *dataP = NULL;
terencez 0:1fa43ab66921 178 }
terencez 0:1fa43ab66921 179 else
terencez 0:1fa43ab66921 180 {
terencez 0:1fa43ab66921 181 *dataP = lwm2m_data_new(*sizeP);
terencez 0:1fa43ab66921 182 if (*dataP == NULL) return COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:1fa43ab66921 183
terencez 0:1fa43ab66921 184 instanceP = targetP->instanceList;
terencez 0:1fa43ab66921 185 i = 0;
terencez 0:1fa43ab66921 186 while (instanceP != NULL && result == COAP_205_CONTENT)
terencez 0:1fa43ab66921 187 {
terencez 0:1fa43ab66921 188 result = targetP->readFunc(instanceP->id, (int*)&((*dataP)[i].value.asChildren.count), &((*dataP)[i].value.asChildren.array), targetP);
terencez 0:1fa43ab66921 189 (*dataP)[i].type = LWM2M_TYPE_OBJECT_INSTANCE;
terencez 0:1fa43ab66921 190 (*dataP)[i].id = instanceP->id;
terencez 0:1fa43ab66921 191 i++;
terencez 0:1fa43ab66921 192 instanceP = instanceP->next;
terencez 0:1fa43ab66921 193 }
terencez 0:1fa43ab66921 194 }
terencez 0:1fa43ab66921 195 }
terencez 0:1fa43ab66921 196
terencez 0:1fa43ab66921 197 LOG_ARG("result: %u.%2u, size: %d", (result & 0xFF) >> 5, (result & 0x1F), *sizeP);
terencez 0:1fa43ab66921 198 return result;
terencez 0:1fa43ab66921 199 }
terencez 0:1fa43ab66921 200
terencez 0:1fa43ab66921 201 coap_status_t object_read(lwm2m_context_t * contextP,
terencez 0:1fa43ab66921 202 lwm2m_uri_t * uriP,
terencez 0:1fa43ab66921 203 lwm2m_media_type_t * formatP,
terencez 0:1fa43ab66921 204 uint8_t ** bufferP,
terencez 0:1fa43ab66921 205 size_t * lengthP)
terencez 0:1fa43ab66921 206 {
terencez 0:1fa43ab66921 207 coap_status_t result;
terencez 0:1fa43ab66921 208 lwm2m_data_t * dataP = NULL;
terencez 0:1fa43ab66921 209 int size = 0;
terencez 0:1fa43ab66921 210 int res;
terencez 0:1fa43ab66921 211
terencez 0:1fa43ab66921 212 LOG_URI(uriP);
terencez 0:1fa43ab66921 213 result = object_readData(contextP, uriP, &size, &dataP);
terencez 0:1fa43ab66921 214
terencez 0:1fa43ab66921 215 if (result == COAP_205_CONTENT)
terencez 0:1fa43ab66921 216 {
terencez 0:1fa43ab66921 217 res = lwm2m_data_serialize(uriP, size, dataP, formatP, bufferP);
terencez 0:1fa43ab66921 218 if (res < 0)
terencez 0:1fa43ab66921 219 {
terencez 0:1fa43ab66921 220 result = COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:1fa43ab66921 221 }
terencez 0:1fa43ab66921 222 else
terencez 0:1fa43ab66921 223 {
terencez 0:1fa43ab66921 224 *lengthP = (size_t)res;
terencez 0:1fa43ab66921 225 }
terencez 0:1fa43ab66921 226 }
terencez 0:1fa43ab66921 227 lwm2m_data_free(size, dataP);
terencez 0:1fa43ab66921 228
terencez 0:1fa43ab66921 229 LOG_ARG("result: %u.%2u, length: %d", (result & 0xFF) >> 5, (result & 0x1F), *lengthP);
terencez 0:1fa43ab66921 230
terencez 0:1fa43ab66921 231 return result;
terencez 0:1fa43ab66921 232 }
terencez 0:1fa43ab66921 233
terencez 0:1fa43ab66921 234 coap_status_t object_write(lwm2m_context_t * contextP,
terencez 0:1fa43ab66921 235 lwm2m_uri_t * uriP,
terencez 0:1fa43ab66921 236 lwm2m_media_type_t format,
terencez 0:1fa43ab66921 237 uint8_t * buffer,
terencez 0:1fa43ab66921 238 size_t length)
terencez 0:1fa43ab66921 239 {
terencez 0:1fa43ab66921 240 coap_status_t result = NO_ERROR;
terencez 0:1fa43ab66921 241 lwm2m_object_t * targetP;
terencez 0:1fa43ab66921 242 lwm2m_data_t * dataP = NULL;
terencez 0:1fa43ab66921 243 int size = 0;
terencez 0:1fa43ab66921 244
terencez 0:1fa43ab66921 245 LOG_URI(uriP);
terencez 0:1fa43ab66921 246 targetP = (lwm2m_object_t *)LWM2M_LIST_FIND(contextP->objectList, uriP->objectId);
terencez 0:1fa43ab66921 247 if (NULL == targetP)
terencez 0:1fa43ab66921 248 {
terencez 0:1fa43ab66921 249 result = COAP_404_NOT_FOUND;
terencez 0:1fa43ab66921 250 }
terencez 0:1fa43ab66921 251 else if (NULL == targetP->writeFunc)
terencez 0:1fa43ab66921 252 {
terencez 0:1fa43ab66921 253 result = COAP_405_METHOD_NOT_ALLOWED;
terencez 0:1fa43ab66921 254 }
terencez 0:1fa43ab66921 255 else
terencez 0:1fa43ab66921 256 {
terencez 0:1fa43ab66921 257 size = lwm2m_data_parse(uriP, buffer, length, format, &dataP);
terencez 0:1fa43ab66921 258 if (size == 0)
terencez 0:1fa43ab66921 259 {
terencez 0:1fa43ab66921 260 result = COAP_406_NOT_ACCEPTABLE;
terencez 0:1fa43ab66921 261 }
terencez 0:1fa43ab66921 262 }
terencez 0:1fa43ab66921 263 if (result == NO_ERROR)
terencez 0:1fa43ab66921 264 {
terencez 0:1fa43ab66921 265 result = targetP->writeFunc(uriP->instanceId, size, dataP, targetP);
terencez 0:1fa43ab66921 266 lwm2m_data_free(size, dataP);
terencez 0:1fa43ab66921 267 }
terencez 0:1fa43ab66921 268
terencez 0:1fa43ab66921 269 LOG_ARG("result: %u.%2u", (result & 0xFF) >> 5, (result & 0x1F));
terencez 0:1fa43ab66921 270
terencez 0:1fa43ab66921 271 return result;
terencez 0:1fa43ab66921 272 }
terencez 0:1fa43ab66921 273
terencez 0:1fa43ab66921 274 coap_status_t object_execute(lwm2m_context_t * contextP,
terencez 0:1fa43ab66921 275 lwm2m_uri_t * uriP,
terencez 0:1fa43ab66921 276 uint8_t * buffer,
terencez 0:1fa43ab66921 277 size_t length)
terencez 0:1fa43ab66921 278 {
terencez 0:1fa43ab66921 279 lwm2m_object_t * targetP;
terencez 0:1fa43ab66921 280
terencez 0:1fa43ab66921 281 LOG_URI(uriP);
terencez 0:1fa43ab66921 282 targetP = (lwm2m_object_t *)LWM2M_LIST_FIND(contextP->objectList, uriP->objectId);
terencez 0:1fa43ab66921 283 if (NULL == targetP) return COAP_404_NOT_FOUND;
terencez 0:1fa43ab66921 284 if (NULL == targetP->executeFunc) return COAP_405_METHOD_NOT_ALLOWED;
terencez 0:1fa43ab66921 285 if (NULL == lwm2m_list_find(targetP->instanceList, uriP->instanceId)) return COAP_404_NOT_FOUND;
terencez 0:1fa43ab66921 286
terencez 0:1fa43ab66921 287 return targetP->executeFunc(uriP->instanceId, uriP->resourceId, buffer, length, targetP);
terencez 0:1fa43ab66921 288 }
terencez 0:1fa43ab66921 289
terencez 0:1fa43ab66921 290 coap_status_t object_create(lwm2m_context_t * contextP,
terencez 0:1fa43ab66921 291 lwm2m_uri_t * uriP,
terencez 0:1fa43ab66921 292 lwm2m_media_type_t format,
terencez 0:1fa43ab66921 293 uint8_t * buffer,
terencez 0:1fa43ab66921 294 size_t length)
terencez 0:1fa43ab66921 295 {
terencez 0:1fa43ab66921 296 lwm2m_object_t * targetP;
terencez 0:1fa43ab66921 297 lwm2m_data_t * dataP = NULL;
terencez 0:1fa43ab66921 298 int size = 0;
terencez 0:1fa43ab66921 299 uint8_t result;
terencez 0:1fa43ab66921 300
terencez 0:1fa43ab66921 301 LOG_URI(uriP);
terencez 0:1fa43ab66921 302
terencez 0:1fa43ab66921 303 if (length == 0 || buffer == 0)
terencez 0:1fa43ab66921 304 {
terencez 0:1fa43ab66921 305 return COAP_400_BAD_REQUEST;
terencez 0:1fa43ab66921 306 }
terencez 0:1fa43ab66921 307
terencez 0:1fa43ab66921 308 targetP = (lwm2m_object_t *)LWM2M_LIST_FIND(contextP->objectList, uriP->objectId);
terencez 0:1fa43ab66921 309 if (NULL == targetP) return COAP_404_NOT_FOUND;
terencez 0:1fa43ab66921 310 if (NULL == targetP->createFunc) return COAP_405_METHOD_NOT_ALLOWED;
terencez 0:1fa43ab66921 311
terencez 0:1fa43ab66921 312 size = lwm2m_data_parse(uriP, buffer, length, format, &dataP);
terencez 0:1fa43ab66921 313 if (size <= 0) return COAP_400_BAD_REQUEST;
terencez 0:1fa43ab66921 314
terencez 0:1fa43ab66921 315 switch (dataP[0].type)
terencez 0:1fa43ab66921 316 {
terencez 0:1fa43ab66921 317 case LWM2M_TYPE_OBJECT:
terencez 0:1fa43ab66921 318 result = COAP_400_BAD_REQUEST;
terencez 0:1fa43ab66921 319 goto exit;
terencez 0:1fa43ab66921 320
terencez 0:1fa43ab66921 321 case LWM2M_TYPE_OBJECT_INSTANCE:
terencez 0:1fa43ab66921 322 if (size != 1)
terencez 0:1fa43ab66921 323 {
terencez 0:1fa43ab66921 324 result = COAP_400_BAD_REQUEST;
terencez 0:1fa43ab66921 325 goto exit;
terencez 0:1fa43ab66921 326 }
terencez 0:1fa43ab66921 327 if (NULL != lwm2m_list_find(targetP->instanceList, dataP[0].id))
terencez 0:1fa43ab66921 328 {
terencez 0:1fa43ab66921 329 // Instance already exists
terencez 0:1fa43ab66921 330 result = COAP_406_NOT_ACCEPTABLE;
terencez 0:1fa43ab66921 331 goto exit;
terencez 0:1fa43ab66921 332 }
terencez 0:1fa43ab66921 333 result = targetP->createFunc(dataP[0].id, dataP[0].value.asChildren.count, dataP[0].value.asChildren.array, targetP);
terencez 0:1fa43ab66921 334 uriP->instanceId = dataP[0].id;
terencez 0:1fa43ab66921 335 uriP->flag |= LWM2M_URI_FLAG_INSTANCE_ID;
terencez 0:1fa43ab66921 336 break;
terencez 0:1fa43ab66921 337
terencez 0:1fa43ab66921 338 default:
terencez 0:1fa43ab66921 339 if (!LWM2M_URI_IS_SET_INSTANCE(uriP))
terencez 0:1fa43ab66921 340 {
terencez 0:1fa43ab66921 341 uriP->instanceId = lwm2m_list_newId(targetP->instanceList);
terencez 0:1fa43ab66921 342 uriP->flag |= LWM2M_URI_FLAG_INSTANCE_ID;
terencez 0:1fa43ab66921 343 }
terencez 0:1fa43ab66921 344 result = targetP->createFunc(uriP->instanceId, size, dataP, targetP);
terencez 0:1fa43ab66921 345 break;
terencez 0:1fa43ab66921 346 }
terencez 0:1fa43ab66921 347
terencez 0:1fa43ab66921 348 exit:
terencez 0:1fa43ab66921 349 lwm2m_data_free(size, dataP);
terencez 0:1fa43ab66921 350
terencez 0:1fa43ab66921 351 LOG_ARG("result: %u.%2u", (result & 0xFF) >> 5, (result & 0x1F));
terencez 0:1fa43ab66921 352
terencez 0:1fa43ab66921 353 return result;
terencez 0:1fa43ab66921 354 }
terencez 0:1fa43ab66921 355
terencez 0:1fa43ab66921 356 coap_status_t object_delete(lwm2m_context_t * contextP,
terencez 0:1fa43ab66921 357 lwm2m_uri_t * uriP)
terencez 0:1fa43ab66921 358 {
terencez 0:1fa43ab66921 359 lwm2m_object_t * objectP;
terencez 0:1fa43ab66921 360 coap_status_t result;
terencez 0:1fa43ab66921 361
terencez 0:1fa43ab66921 362 LOG_URI(uriP);
terencez 0:1fa43ab66921 363 objectP = (lwm2m_object_t *)LWM2M_LIST_FIND(contextP->objectList, uriP->objectId);
terencez 0:1fa43ab66921 364 if (NULL == objectP) return COAP_404_NOT_FOUND;
terencez 0:1fa43ab66921 365 if (NULL == objectP->deleteFunc) return COAP_405_METHOD_NOT_ALLOWED;
terencez 0:1fa43ab66921 366
terencez 0:1fa43ab66921 367 LOG("Entering");
terencez 0:1fa43ab66921 368
terencez 0:1fa43ab66921 369 if (LWM2M_URI_IS_SET_INSTANCE(uriP))
terencez 0:1fa43ab66921 370 {
terencez 0:1fa43ab66921 371 result = objectP->deleteFunc(uriP->instanceId, objectP);
terencez 0:1fa43ab66921 372 }
terencez 0:1fa43ab66921 373 else
terencez 0:1fa43ab66921 374 {
terencez 0:1fa43ab66921 375 lwm2m_list_t * instanceP;
terencez 0:1fa43ab66921 376
terencez 0:1fa43ab66921 377 result = COAP_202_DELETED;
terencez 0:1fa43ab66921 378 instanceP = objectP->instanceList;
terencez 0:1fa43ab66921 379 while (NULL != instanceP
terencez 0:1fa43ab66921 380 && result == COAP_202_DELETED)
terencez 0:1fa43ab66921 381 {
terencez 0:1fa43ab66921 382 result = objectP->deleteFunc(instanceP->id, objectP);
terencez 0:1fa43ab66921 383 instanceP = objectP->instanceList;
terencez 0:1fa43ab66921 384 }
terencez 0:1fa43ab66921 385 }
terencez 0:1fa43ab66921 386
terencez 0:1fa43ab66921 387 LOG_ARG("result: %u.%2u", (result & 0xFF) >> 5, (result & 0x1F));
terencez 0:1fa43ab66921 388
terencez 0:1fa43ab66921 389 return result;
terencez 0:1fa43ab66921 390 }
terencez 0:1fa43ab66921 391
terencez 0:1fa43ab66921 392 coap_status_t object_discover(lwm2m_context_t * contextP,
terencez 0:1fa43ab66921 393 lwm2m_uri_t * uriP,
terencez 0:1fa43ab66921 394 lwm2m_server_t * serverP,
terencez 0:1fa43ab66921 395 uint8_t ** bufferP,
terencez 0:1fa43ab66921 396 size_t * lengthP)
terencez 0:1fa43ab66921 397 {
terencez 0:1fa43ab66921 398 coap_status_t result;
terencez 0:1fa43ab66921 399 lwm2m_object_t * targetP;
terencez 0:1fa43ab66921 400 lwm2m_data_t * dataP = NULL;
terencez 0:1fa43ab66921 401 int size = 0;
terencez 0:1fa43ab66921 402
terencez 0:1fa43ab66921 403 LOG_URI(uriP);
terencez 0:1fa43ab66921 404 targetP = (lwm2m_object_t *)LWM2M_LIST_FIND(contextP->objectList, uriP->objectId);
terencez 0:1fa43ab66921 405 if (NULL == targetP) return COAP_404_NOT_FOUND;
terencez 0:1fa43ab66921 406 if (NULL == targetP->discoverFunc) return COAP_501_NOT_IMPLEMENTED;
terencez 0:1fa43ab66921 407
terencez 0:1fa43ab66921 408 if (LWM2M_URI_IS_SET_INSTANCE(uriP))
terencez 0:1fa43ab66921 409 {
terencez 0:1fa43ab66921 410 if (NULL == lwm2m_list_find(targetP->instanceList, uriP->instanceId)) return COAP_404_NOT_FOUND;
terencez 0:1fa43ab66921 411
terencez 0:1fa43ab66921 412 // single instance read
terencez 0:1fa43ab66921 413 if (LWM2M_URI_IS_SET_RESOURCE(uriP))
terencez 0:1fa43ab66921 414 {
terencez 0:1fa43ab66921 415 size = 1;
terencez 0:1fa43ab66921 416 dataP = lwm2m_data_new(size);
terencez 0:1fa43ab66921 417 if (dataP == NULL) return COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:1fa43ab66921 418
terencez 0:1fa43ab66921 419 dataP->id = uriP->resourceId;
terencez 0:1fa43ab66921 420 }
terencez 0:1fa43ab66921 421
terencez 0:1fa43ab66921 422 result = targetP->discoverFunc(uriP->instanceId, &size, &dataP, targetP);
terencez 0:1fa43ab66921 423 }
terencez 0:1fa43ab66921 424 else
terencez 0:1fa43ab66921 425 {
terencez 0:1fa43ab66921 426 // multiple object instances read
terencez 0:1fa43ab66921 427 lwm2m_list_t * instanceP;
terencez 0:1fa43ab66921 428 int i;
terencez 0:1fa43ab66921 429
terencez 0:1fa43ab66921 430 result = COAP_205_CONTENT;
terencez 0:1fa43ab66921 431
terencez 0:1fa43ab66921 432 size = 0;
terencez 0:1fa43ab66921 433 for (instanceP = targetP->instanceList; instanceP != NULL ; instanceP = instanceP->next)
terencez 0:1fa43ab66921 434 {
terencez 0:1fa43ab66921 435 size++;
terencez 0:1fa43ab66921 436 }
terencez 0:1fa43ab66921 437
terencez 0:1fa43ab66921 438 if (size != 0)
terencez 0:1fa43ab66921 439 {
terencez 0:1fa43ab66921 440 dataP = lwm2m_data_new(size);
terencez 0:1fa43ab66921 441 if (dataP == NULL) return COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:1fa43ab66921 442
terencez 0:1fa43ab66921 443 instanceP = targetP->instanceList;
terencez 0:1fa43ab66921 444 i = 0;
terencez 0:1fa43ab66921 445 while (instanceP != NULL && result == COAP_205_CONTENT)
terencez 0:1fa43ab66921 446 {
terencez 0:1fa43ab66921 447 result = targetP->discoverFunc(instanceP->id, (int*)&(dataP[i].value.asChildren.count), &(dataP[i].value.asChildren.array), targetP);
terencez 0:1fa43ab66921 448 dataP[i].type = LWM2M_TYPE_OBJECT_INSTANCE;
terencez 0:1fa43ab66921 449 dataP[i].id = instanceP->id;
terencez 0:1fa43ab66921 450 i++;
terencez 0:1fa43ab66921 451 instanceP = instanceP->next;
terencez 0:1fa43ab66921 452 }
terencez 0:1fa43ab66921 453 }
terencez 0:1fa43ab66921 454 }
terencez 0:1fa43ab66921 455
terencez 0:1fa43ab66921 456 if (result == COAP_205_CONTENT)
terencez 0:1fa43ab66921 457 {
terencez 0:1fa43ab66921 458 int len;
terencez 0:1fa43ab66921 459
terencez 0:1fa43ab66921 460 len = discover_serialize(contextP, uriP, serverP, size, dataP, bufferP);
terencez 0:1fa43ab66921 461 if (len <= 0) result = COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:1fa43ab66921 462 else *lengthP = len;
terencez 0:1fa43ab66921 463 }
terencez 0:1fa43ab66921 464 lwm2m_data_free(size, dataP);
terencez 0:1fa43ab66921 465
terencez 0:1fa43ab66921 466 LOG_ARG("result: %u.%2u", (result & 0xFF) >> 5, (result & 0x1F));
terencez 0:1fa43ab66921 467
terencez 0:1fa43ab66921 468 return result;
terencez 0:1fa43ab66921 469 }
terencez 0:1fa43ab66921 470
terencez 0:1fa43ab66921 471 bool object_isInstanceNew(lwm2m_context_t * contextP,
terencez 0:1fa43ab66921 472 uint16_t objectId,
terencez 0:1fa43ab66921 473 uint16_t instanceId)
terencez 0:1fa43ab66921 474 {
terencez 0:1fa43ab66921 475 lwm2m_object_t * targetP;
terencez 0:1fa43ab66921 476
terencez 0:1fa43ab66921 477 LOG("Entering");
terencez 0:1fa43ab66921 478 targetP = (lwm2m_object_t *)LWM2M_LIST_FIND(contextP->objectList, objectId);
terencez 0:1fa43ab66921 479 if (targetP != NULL)
terencez 0:1fa43ab66921 480 {
terencez 0:1fa43ab66921 481 if (NULL != lwm2m_list_find(targetP->instanceList, instanceId))
terencez 0:1fa43ab66921 482 {
terencez 0:1fa43ab66921 483 return false;
terencez 0:1fa43ab66921 484 }
terencez 0:1fa43ab66921 485 }
terencez 0:1fa43ab66921 486
terencez 0:1fa43ab66921 487 return true;
terencez 0:1fa43ab66921 488 }
terencez 0:1fa43ab66921 489
terencez 0:1fa43ab66921 490 static int prv_getObjectTemplate(uint8_t * buffer,
terencez 0:1fa43ab66921 491 size_t length,
terencez 0:1fa43ab66921 492 uint16_t id)
terencez 0:1fa43ab66921 493 {
terencez 0:1fa43ab66921 494 int index;
terencez 0:1fa43ab66921 495 int result;
terencez 0:1fa43ab66921 496
terencez 0:1fa43ab66921 497 if (length < REG_OBJECT_MIN_LEN) return -1;
terencez 0:1fa43ab66921 498
terencez 0:1fa43ab66921 499 buffer[0] = '<';
terencez 0:1fa43ab66921 500 buffer[1] = '/';
terencez 0:1fa43ab66921 501 index = 2;
terencez 0:1fa43ab66921 502
terencez 0:1fa43ab66921 503 result = utils_intCopy((char *)buffer + index, length - index, id);
terencez 0:1fa43ab66921 504 if (result < 0) return -1;
terencez 0:1fa43ab66921 505 index += result;
terencez 0:1fa43ab66921 506
terencez 0:1fa43ab66921 507 if (length - index < REG_OBJECT_MIN_LEN - 3) return -1;
terencez 0:1fa43ab66921 508 buffer[index] = '/';
terencez 0:1fa43ab66921 509 index++;
terencez 0:1fa43ab66921 510
terencez 0:1fa43ab66921 511 return index;
terencez 0:1fa43ab66921 512 }
terencez 0:1fa43ab66921 513
terencez 0:1fa43ab66921 514 int object_getRegisterPayload(lwm2m_context_t * contextP,
terencez 0:1fa43ab66921 515 uint8_t * buffer,
terencez 0:1fa43ab66921 516 size_t bufferLen)
terencez 0:1fa43ab66921 517 {
terencez 0:1fa43ab66921 518 size_t index;
terencez 0:1fa43ab66921 519 int result;
terencez 0:1fa43ab66921 520 lwm2m_object_t * objectP;
terencez 0:1fa43ab66921 521
terencez 0:1fa43ab66921 522 LOG("Entering");
terencez 0:1fa43ab66921 523 // index can not be greater than bufferLen
terencez 0:1fa43ab66921 524 index = 0;
terencez 0:1fa43ab66921 525
terencez 0:1fa43ab66921 526 result = utils_stringCopy((char *)buffer, bufferLen, REG_START);
terencez 0:1fa43ab66921 527 if (result < 0) return 0;
terencez 0:1fa43ab66921 528 index += result;
terencez 0:1fa43ab66921 529
terencez 0:1fa43ab66921 530 if ((contextP->altPath != NULL)
terencez 0:1fa43ab66921 531 && (contextP->altPath[0] != 0))
terencez 0:1fa43ab66921 532 {
terencez 0:1fa43ab66921 533 result = utils_stringCopy((char *)buffer + index, bufferLen - index, contextP->altPath);
terencez 0:1fa43ab66921 534 }
terencez 0:1fa43ab66921 535 else
terencez 0:1fa43ab66921 536 {
terencez 0:1fa43ab66921 537 result = utils_stringCopy((char *)buffer + index, bufferLen - index, REG_DEFAULT_PATH);
terencez 0:1fa43ab66921 538 }
terencez 0:1fa43ab66921 539 if (result < 0) return 0;
terencez 0:1fa43ab66921 540 index += result;
terencez 0:1fa43ab66921 541
terencez 0:1fa43ab66921 542 result = utils_stringCopy((char *)buffer + index, bufferLen - index, REG_LWM2M_RESOURCE_TYPE);
terencez 0:1fa43ab66921 543 if (result < 0) return 0;
terencez 0:1fa43ab66921 544 index += result;
terencez 0:1fa43ab66921 545
terencez 0:1fa43ab66921 546 for (objectP = contextP->objectList; objectP != NULL; objectP = objectP->next)
terencez 0:1fa43ab66921 547 {
terencez 0:1fa43ab66921 548 size_t start;
terencez 0:1fa43ab66921 549 size_t length;
terencez 0:1fa43ab66921 550
terencez 0:1fa43ab66921 551 if (objectP->objID == LWM2M_SECURITY_OBJECT_ID) continue;
terencez 0:1fa43ab66921 552
terencez 0:1fa43ab66921 553 start = index;
terencez 0:1fa43ab66921 554 result = prv_getObjectTemplate(buffer + index, bufferLen - index, objectP->objID);
terencez 0:1fa43ab66921 555 if (result < 0) return 0;
terencez 0:1fa43ab66921 556 length = result;
terencez 0:1fa43ab66921 557 index += length;
terencez 0:1fa43ab66921 558
terencez 0:1fa43ab66921 559 if (objectP->instanceList == NULL)
terencez 0:1fa43ab66921 560 {
terencez 0:1fa43ab66921 561 index--;
terencez 0:1fa43ab66921 562 result = utils_stringCopy((char *)buffer + index, bufferLen - index, REG_PATH_END);
terencez 0:1fa43ab66921 563 if (result < 0) return 0;
terencez 0:1fa43ab66921 564 index += result;
terencez 0:1fa43ab66921 565 }
terencez 0:1fa43ab66921 566 else
terencez 0:1fa43ab66921 567 {
terencez 0:1fa43ab66921 568 lwm2m_list_t * targetP;
terencez 0:1fa43ab66921 569 for (targetP = objectP->instanceList ; targetP != NULL ; targetP = targetP->next)
terencez 0:1fa43ab66921 570 {
terencez 0:1fa43ab66921 571 if (bufferLen - index <= length) return 0;
terencez 0:1fa43ab66921 572
terencez 0:1fa43ab66921 573 if (index != start + length)
terencez 0:1fa43ab66921 574 {
terencez 0:1fa43ab66921 575 memcpy(buffer + index, buffer + start, length);
terencez 0:1fa43ab66921 576 index += length;
terencez 0:1fa43ab66921 577 }
terencez 0:1fa43ab66921 578
terencez 0:1fa43ab66921 579 result = utils_intCopy((char *)buffer + index, bufferLen - index, targetP->id);
terencez 0:1fa43ab66921 580 if (result < 0) return 0;
terencez 0:1fa43ab66921 581 index += result;
terencez 0:1fa43ab66921 582
terencez 0:1fa43ab66921 583 result = utils_stringCopy((char *)buffer + index, bufferLen - index, REG_PATH_END);
terencez 0:1fa43ab66921 584 if (result < 0) return 0;
terencez 0:1fa43ab66921 585 index += result;
terencez 0:1fa43ab66921 586 }
terencez 0:1fa43ab66921 587 }
terencez 0:1fa43ab66921 588 }
terencez 0:1fa43ab66921 589
terencez 0:1fa43ab66921 590 if (index > 0)
terencez 0:1fa43ab66921 591 {
terencez 0:1fa43ab66921 592 index = index - 1; // remove trailing ','
terencez 0:1fa43ab66921 593 }
terencez 0:1fa43ab66921 594
terencez 0:1fa43ab66921 595 buffer[index] = 0;
terencez 0:1fa43ab66921 596
terencez 0:1fa43ab66921 597 return index;
terencez 0:1fa43ab66921 598 }
terencez 0:1fa43ab66921 599
terencez 0:1fa43ab66921 600 static lwm2m_list_t * prv_findServerInstance(lwm2m_object_t * objectP,
terencez 0:1fa43ab66921 601 uint16_t shortID)
terencez 0:1fa43ab66921 602 {
terencez 0:1fa43ab66921 603 lwm2m_list_t * instanceP;
terencez 0:1fa43ab66921 604
terencez 0:1fa43ab66921 605 instanceP = objectP->instanceList;
terencez 0:1fa43ab66921 606 while (NULL != instanceP)
terencez 0:1fa43ab66921 607 {
terencez 0:1fa43ab66921 608 int64_t value;
terencez 0:1fa43ab66921 609 lwm2m_data_t * dataP;
terencez 0:1fa43ab66921 610 int size;
terencez 0:1fa43ab66921 611
terencez 0:1fa43ab66921 612 size = 1;
terencez 0:1fa43ab66921 613 dataP = lwm2m_data_new(size);
terencez 0:1fa43ab66921 614 if (dataP == NULL) return NULL;
terencez 0:1fa43ab66921 615 dataP->id = LWM2M_SERVER_SHORT_ID_ID;
terencez 0:1fa43ab66921 616
terencez 0:1fa43ab66921 617 if (objectP->readFunc(instanceP->id, &size, &dataP, objectP) != COAP_205_CONTENT)
terencez 0:1fa43ab66921 618 {
terencez 0:1fa43ab66921 619 lwm2m_data_free(size, dataP);
terencez 0:1fa43ab66921 620 return NULL;
terencez 0:1fa43ab66921 621 }
terencez 0:1fa43ab66921 622
terencez 0:1fa43ab66921 623 if (1 == lwm2m_data_decode_int(dataP, &value))
terencez 0:1fa43ab66921 624 {
terencez 0:1fa43ab66921 625 if (value == shortID)
terencez 0:1fa43ab66921 626 {
terencez 0:1fa43ab66921 627 lwm2m_data_free(size, dataP);
terencez 0:1fa43ab66921 628 break;
terencez 0:1fa43ab66921 629 }
terencez 0:1fa43ab66921 630 }
terencez 0:1fa43ab66921 631 lwm2m_data_free(size, dataP);
terencez 0:1fa43ab66921 632 instanceP = instanceP->next;
terencez 0:1fa43ab66921 633 }
terencez 0:1fa43ab66921 634
terencez 0:1fa43ab66921 635 return instanceP;
terencez 0:1fa43ab66921 636 }
terencez 0:1fa43ab66921 637
terencez 0:1fa43ab66921 638 static int prv_getMandatoryInfo(lwm2m_object_t * objectP,
terencez 0:1fa43ab66921 639 uint16_t instanceID,
terencez 0:1fa43ab66921 640 lwm2m_server_t * targetP)
terencez 0:1fa43ab66921 641 {
terencez 0:1fa43ab66921 642 lwm2m_data_t * dataP;
terencez 0:1fa43ab66921 643 int size;
terencez 0:1fa43ab66921 644 int64_t value;
terencez 0:1fa43ab66921 645
terencez 0:1fa43ab66921 646 size = 2;
terencez 0:1fa43ab66921 647 dataP = lwm2m_data_new(size);
terencez 0:1fa43ab66921 648 if (dataP == NULL) return -1;
terencez 0:1fa43ab66921 649 dataP[0].id = LWM2M_SERVER_LIFETIME_ID;
terencez 0:1fa43ab66921 650 dataP[1].id = LWM2M_SERVER_BINDING_ID;
terencez 0:1fa43ab66921 651
terencez 0:1fa43ab66921 652 if (objectP->readFunc(instanceID, &size, &dataP, objectP) != COAP_205_CONTENT)
terencez 0:1fa43ab66921 653 {
terencez 0:1fa43ab66921 654 lwm2m_data_free(size, dataP);
terencez 0:1fa43ab66921 655 return -1;
terencez 0:1fa43ab66921 656 }
terencez 0:1fa43ab66921 657
terencez 0:1fa43ab66921 658 if (0 == lwm2m_data_decode_int(dataP, &value)
terencez 0:1fa43ab66921 659 || value < 0 || value >0xFFFFFFFF) // This is an implementation limit
terencez 0:1fa43ab66921 660 {
terencez 0:1fa43ab66921 661 lwm2m_data_free(size, dataP);
terencez 0:1fa43ab66921 662 return -1;
terencez 0:1fa43ab66921 663 }
terencez 0:1fa43ab66921 664 targetP->lifetime = value;
terencez 0:1fa43ab66921 665
terencez 0:1fa43ab66921 666 targetP->binding = utils_stringToBinding(dataP[1].value.asBuffer.buffer, dataP[1].value.asBuffer.length);
terencez 0:1fa43ab66921 667
terencez 0:1fa43ab66921 668 lwm2m_data_free(size, dataP);
terencez 0:1fa43ab66921 669
terencez 0:1fa43ab66921 670 if (targetP->binding == BINDING_UNKNOWN)
terencez 0:1fa43ab66921 671 {
terencez 0:1fa43ab66921 672 return -1;
terencez 0:1fa43ab66921 673 }
terencez 0:1fa43ab66921 674
terencez 0:1fa43ab66921 675 return 0;
terencez 0:1fa43ab66921 676 }
terencez 0:1fa43ab66921 677
terencez 0:1fa43ab66921 678 int object_getServers(lwm2m_context_t * contextP)
terencez 0:1fa43ab66921 679 {
terencez 0:1fa43ab66921 680 lwm2m_object_t * objectP;
terencez 0:1fa43ab66921 681 lwm2m_object_t * securityObjP = NULL;
terencez 0:1fa43ab66921 682 lwm2m_object_t * serverObjP = NULL;
terencez 0:1fa43ab66921 683 lwm2m_list_t * securityInstP; // instanceID of the server in the LWM2M Security Object
terencez 0:1fa43ab66921 684
terencez 0:1fa43ab66921 685 LOG("Entering");
terencez 0:1fa43ab66921 686
terencez 0:1fa43ab66921 687 for (objectP = contextP->objectList; objectP != NULL; objectP = objectP->next)
terencez 0:1fa43ab66921 688 {
terencez 0:1fa43ab66921 689 if (objectP->objID == LWM2M_SECURITY_OBJECT_ID)
terencez 0:1fa43ab66921 690 {
terencez 0:1fa43ab66921 691 securityObjP = objectP;
terencez 0:1fa43ab66921 692 }
terencez 0:1fa43ab66921 693 else if (objectP->objID == LWM2M_SERVER_OBJECT_ID)
terencez 0:1fa43ab66921 694 {
terencez 0:1fa43ab66921 695 serverObjP = objectP;
terencez 0:1fa43ab66921 696 }
terencez 0:1fa43ab66921 697 }
terencez 0:1fa43ab66921 698
terencez 0:1fa43ab66921 699 if (NULL == securityObjP) return -1;
terencez 0:1fa43ab66921 700
terencez 0:1fa43ab66921 701 securityInstP = securityObjP->instanceList;
terencez 0:1fa43ab66921 702 while (securityInstP != NULL)
terencez 0:1fa43ab66921 703 {
terencez 0:1fa43ab66921 704 if (LWM2M_LIST_FIND(contextP->bootstrapServerList, securityInstP->id) == NULL
terencez 0:1fa43ab66921 705 && LWM2M_LIST_FIND(contextP->serverList, securityInstP->id) == NULL)
terencez 0:1fa43ab66921 706 {
terencez 0:1fa43ab66921 707 // This server is new. eg created by last bootstrap
terencez 0:1fa43ab66921 708
terencez 0:1fa43ab66921 709 lwm2m_data_t * dataP;
terencez 0:1fa43ab66921 710 int size;
terencez 0:1fa43ab66921 711 lwm2m_server_t * targetP;
terencez 0:1fa43ab66921 712 bool isBootstrap;
terencez 0:1fa43ab66921 713 int64_t value = 0;
terencez 0:1fa43ab66921 714
terencez 0:1fa43ab66921 715 size = 3;
terencez 0:1fa43ab66921 716 dataP = lwm2m_data_new(size);
terencez 0:1fa43ab66921 717 if (dataP == NULL) return -1;
terencez 0:1fa43ab66921 718 dataP[0].id = LWM2M_SECURITY_BOOTSTRAP_ID;
terencez 0:1fa43ab66921 719 dataP[1].id = LWM2M_SECURITY_SHORT_SERVER_ID;
terencez 0:1fa43ab66921 720 dataP[2].id = LWM2M_SECURITY_HOLD_OFF_ID;
terencez 0:1fa43ab66921 721
terencez 0:1fa43ab66921 722 if (securityObjP->readFunc(securityInstP->id, &size, &dataP, securityObjP) != COAP_205_CONTENT)
terencez 0:1fa43ab66921 723 {
terencez 0:1fa43ab66921 724 lwm2m_data_free(size, dataP);
terencez 0:1fa43ab66921 725 return -1;
terencez 0:1fa43ab66921 726 }
terencez 0:1fa43ab66921 727
terencez 0:1fa43ab66921 728 targetP = (lwm2m_server_t *)lwm2m_malloc(sizeof(lwm2m_server_t));
terencez 0:1fa43ab66921 729 if (targetP == NULL) {
terencez 0:1fa43ab66921 730 lwm2m_data_free(size, dataP);
terencez 0:1fa43ab66921 731 return -1;
terencez 0:1fa43ab66921 732 }
terencez 0:1fa43ab66921 733 memset(targetP, 0, sizeof(lwm2m_server_t));
terencez 0:1fa43ab66921 734 targetP->secObjInstID = securityInstP->id;
terencez 0:1fa43ab66921 735
terencez 0:1fa43ab66921 736 if (0 == lwm2m_data_decode_bool(dataP + 0, &isBootstrap))
terencez 0:1fa43ab66921 737 {
terencez 0:1fa43ab66921 738 lwm2m_free(targetP);
terencez 0:1fa43ab66921 739 lwm2m_data_free(size, dataP);
terencez 0:1fa43ab66921 740 return -1;
terencez 0:1fa43ab66921 741 }
terencez 0:1fa43ab66921 742
terencez 0:1fa43ab66921 743 if (0 == lwm2m_data_decode_int(dataP + 1, &value)
terencez 0:1fa43ab66921 744 || value < (isBootstrap ? 0 : 1) || value > 0xFFFF) // 0 is forbidden as a Short Server ID
terencez 0:1fa43ab66921 745 {
terencez 0:1fa43ab66921 746 lwm2m_free(targetP);
terencez 0:1fa43ab66921 747 lwm2m_data_free(size, dataP);
terencez 0:1fa43ab66921 748 return -1;
terencez 0:1fa43ab66921 749 }
terencez 0:1fa43ab66921 750 targetP->shortID = value;
terencez 0:1fa43ab66921 751
terencez 0:1fa43ab66921 752 if (isBootstrap == true)
terencez 0:1fa43ab66921 753 {
terencez 0:1fa43ab66921 754 if (0 == lwm2m_data_decode_int(dataP + 2, &value)
terencez 0:1fa43ab66921 755 || value < 0 || value > 0xFFFFFFFF) // This is an implementation limit
terencez 0:1fa43ab66921 756 {
terencez 0:1fa43ab66921 757 lwm2m_free(targetP);
terencez 0:1fa43ab66921 758 lwm2m_data_free(size, dataP);
terencez 0:1fa43ab66921 759 return -1;
terencez 0:1fa43ab66921 760 }
terencez 0:1fa43ab66921 761 // lifetime of a bootstrap server is set to ClientHoldOffTime
terencez 0:1fa43ab66921 762 targetP->lifetime = value;
terencez 0:1fa43ab66921 763
terencez 0:1fa43ab66921 764 contextP->bootstrapServerList = (lwm2m_server_t*)LWM2M_LIST_ADD(contextP->bootstrapServerList, targetP);
terencez 0:1fa43ab66921 765 }
terencez 0:1fa43ab66921 766 else
terencez 0:1fa43ab66921 767 {
terencez 0:1fa43ab66921 768 lwm2m_list_t * serverInstP; // instanceID of the server in the LWM2M Server Object
terencez 0:1fa43ab66921 769
terencez 0:1fa43ab66921 770 serverInstP = prv_findServerInstance(serverObjP, targetP->shortID);
terencez 0:1fa43ab66921 771 if (serverInstP == NULL)
terencez 0:1fa43ab66921 772 {
terencez 0:1fa43ab66921 773 lwm2m_free(targetP);
terencez 0:1fa43ab66921 774 lwm2m_data_free(size, dataP);
terencez 0:1fa43ab66921 775 return -1;
terencez 0:1fa43ab66921 776 }
terencez 0:1fa43ab66921 777 if (0 != prv_getMandatoryInfo(serverObjP, serverInstP->id, targetP))
terencez 0:1fa43ab66921 778 {
terencez 0:1fa43ab66921 779 lwm2m_free(targetP);
terencez 0:1fa43ab66921 780 lwm2m_data_free(size, dataP);
terencez 0:1fa43ab66921 781 return -1;
terencez 0:1fa43ab66921 782 }
terencez 0:1fa43ab66921 783 targetP->status = STATE_DEREGISTERED;
terencez 0:1fa43ab66921 784 contextP->serverList = (lwm2m_server_t*)LWM2M_LIST_ADD(contextP->serverList, targetP);
terencez 0:1fa43ab66921 785 }
terencez 0:1fa43ab66921 786 lwm2m_data_free(size, dataP);
terencez 0:1fa43ab66921 787 }
terencez 0:1fa43ab66921 788 securityInstP = securityInstP->next;
terencez 0:1fa43ab66921 789 }
terencez 0:1fa43ab66921 790
terencez 0:1fa43ab66921 791 return 0;
terencez 0:1fa43ab66921 792 }
terencez 0:1fa43ab66921 793
terencez 0:1fa43ab66921 794 coap_status_t object_createInstance(lwm2m_context_t * contextP,
terencez 0:1fa43ab66921 795 lwm2m_uri_t * uriP,
terencez 0:1fa43ab66921 796 lwm2m_data_t * dataP)
terencez 0:1fa43ab66921 797 {
terencez 0:1fa43ab66921 798 lwm2m_object_t * targetP;
terencez 0:1fa43ab66921 799
terencez 0:1fa43ab66921 800 LOG_URI(uriP);
terencez 0:1fa43ab66921 801 targetP = (lwm2m_object_t *)LWM2M_LIST_FIND(contextP->objectList, uriP->objectId);
terencez 0:1fa43ab66921 802 if (NULL == targetP) return COAP_404_NOT_FOUND;
terencez 0:1fa43ab66921 803
terencez 0:1fa43ab66921 804 if (NULL == targetP->createFunc)
terencez 0:1fa43ab66921 805 {
terencez 0:1fa43ab66921 806 return COAP_405_METHOD_NOT_ALLOWED;
terencez 0:1fa43ab66921 807 }
terencez 0:1fa43ab66921 808
terencez 0:1fa43ab66921 809 return targetP->createFunc(lwm2m_list_newId(targetP->instanceList), dataP->value.asChildren.count, dataP->value.asChildren.array, targetP);
terencez 0:1fa43ab66921 810 }
terencez 0:1fa43ab66921 811
terencez 0:1fa43ab66921 812 coap_status_t object_writeInstance(lwm2m_context_t * contextP,
terencez 0:1fa43ab66921 813 lwm2m_uri_t * uriP,
terencez 0:1fa43ab66921 814 lwm2m_data_t * dataP)
terencez 0:1fa43ab66921 815 {
terencez 0:1fa43ab66921 816 lwm2m_object_t * targetP;
terencez 0:1fa43ab66921 817
terencez 0:1fa43ab66921 818 LOG_URI(uriP);
terencez 0:1fa43ab66921 819 targetP = (lwm2m_object_t *)LWM2M_LIST_FIND(contextP->objectList, uriP->objectId);
terencez 0:1fa43ab66921 820 if (NULL == targetP) return COAP_404_NOT_FOUND;
terencez 0:1fa43ab66921 821
terencez 0:1fa43ab66921 822 if (NULL == targetP->writeFunc)
terencez 0:1fa43ab66921 823 {
terencez 0:1fa43ab66921 824 return COAP_405_METHOD_NOT_ALLOWED;
terencez 0:1fa43ab66921 825 }
terencez 0:1fa43ab66921 826
terencez 0:1fa43ab66921 827 return targetP->writeFunc(dataP->id, dataP->value.asChildren.count, dataP->value.asChildren.array, targetP);
terencez 0:1fa43ab66921 828 }
terencez 0:1fa43ab66921 829
terencez 0:1fa43ab66921 830 #endif