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 * Simon Bernard - Please refer to git log
terencez 0:c2dff8cbb91a 17 * Toby Jaffey - Please refer to git log
terencez 0:c2dff8cbb91a 18 * Pascal Rieux - Please refer to git log
terencez 0:c2dff8cbb91a 19 *
terencez 0:c2dff8cbb91a 20 *******************************************************************************/
terencez 0:c2dff8cbb91a 21
terencez 0:c2dff8cbb91a 22 /*
terencez 0:c2dff8cbb91a 23 Copyright (c) 2013, 2014 Intel Corporation
terencez 0:c2dff8cbb91a 24
terencez 0:c2dff8cbb91a 25 Redistribution and use in source and binary forms, with or without modification,
terencez 0:c2dff8cbb91a 26 are permitted provided that the following conditions are met:
terencez 0:c2dff8cbb91a 27
terencez 0:c2dff8cbb91a 28 * Redistributions of source code must retain the above copyright notice,
terencez 0:c2dff8cbb91a 29 this list of conditions and the following disclaimer.
terencez 0:c2dff8cbb91a 30 * Redistributions in binary form must reproduce the above copyright notice,
terencez 0:c2dff8cbb91a 31 this list of conditions and the following disclaimer in the documentation
terencez 0:c2dff8cbb91a 32 and/or other materials provided with the distribution.
terencez 0:c2dff8cbb91a 33 * Neither the name of Intel Corporation nor the names of its contributors
terencez 0:c2dff8cbb91a 34 may be used to endorse or promote products derived from this software
terencez 0:c2dff8cbb91a 35 without specific prior written permission.
terencez 0:c2dff8cbb91a 36
terencez 0:c2dff8cbb91a 37 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
terencez 0:c2dff8cbb91a 38 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
terencez 0:c2dff8cbb91a 39 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
terencez 0:c2dff8cbb91a 40 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
terencez 0:c2dff8cbb91a 41 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
terencez 0:c2dff8cbb91a 42 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
terencez 0:c2dff8cbb91a 43 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
terencez 0:c2dff8cbb91a 44 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
terencez 0:c2dff8cbb91a 45 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
terencez 0:c2dff8cbb91a 46 THE POSSIBILITY OF SUCH DAMAGE.
terencez 0:c2dff8cbb91a 47
terencez 0:c2dff8cbb91a 48 David Navarro <david.navarro@intel.com>
terencez 0:c2dff8cbb91a 49
terencez 0:c2dff8cbb91a 50 */
terencez 0:c2dff8cbb91a 51
terencez 0:c2dff8cbb91a 52 #include "internals.h"
terencez 0:c2dff8cbb91a 53
terencez 0:c2dff8cbb91a 54 #include <stdlib.h>
terencez 0:c2dff8cbb91a 55 #include <string.h>
terencez 0:c2dff8cbb91a 56
terencez 0:c2dff8cbb91a 57 #include <stdio.h>
terencez 0:c2dff8cbb91a 58
terencez 0:c2dff8cbb91a 59
terencez 0:c2dff8cbb91a 60 lwm2m_context_t * lwm2m_init(void * userData)
terencez 0:c2dff8cbb91a 61 {
terencez 0:c2dff8cbb91a 62 lwm2m_context_t * contextP;
terencez 0:c2dff8cbb91a 63
terencez 0:c2dff8cbb91a 64 LOG("Entering");
terencez 0:c2dff8cbb91a 65 contextP = (lwm2m_context_t *)lwm2m_malloc(sizeof(lwm2m_context_t));
terencez 0:c2dff8cbb91a 66 if (NULL != contextP)
terencez 0:c2dff8cbb91a 67 {
terencez 0:c2dff8cbb91a 68 memset(contextP, 0, sizeof(lwm2m_context_t));
terencez 0:c2dff8cbb91a 69 contextP->userData = userData;
terencez 0:c2dff8cbb91a 70 srand((int)lwm2m_gettime());
terencez 0:c2dff8cbb91a 71 contextP->nextMID = rand();
terencez 0:c2dff8cbb91a 72 }
terencez 0:c2dff8cbb91a 73
terencez 0:c2dff8cbb91a 74 return contextP;
terencez 0:c2dff8cbb91a 75 }
terencez 0:c2dff8cbb91a 76
terencez 0:c2dff8cbb91a 77 #ifdef LWM2M_CLIENT_MODE
terencez 0:c2dff8cbb91a 78 void lwm2m_deregister(lwm2m_context_t * context)
terencez 0:c2dff8cbb91a 79 {
terencez 0:c2dff8cbb91a 80 lwm2m_server_t * server = context->serverList;
terencez 0:c2dff8cbb91a 81
terencez 0:c2dff8cbb91a 82 LOG("Entering");
terencez 0:c2dff8cbb91a 83 while (NULL != server)
terencez 0:c2dff8cbb91a 84 {
terencez 0:c2dff8cbb91a 85 registration_deregister(context, server);
terencez 0:c2dff8cbb91a 86 server = server->next;
terencez 0:c2dff8cbb91a 87 }
terencez 0:c2dff8cbb91a 88 }
terencez 0:c2dff8cbb91a 89
terencez 0:c2dff8cbb91a 90 static void prv_deleteServer(lwm2m_server_t * serverP)
terencez 0:c2dff8cbb91a 91 {
terencez 0:c2dff8cbb91a 92 // TODO parse transaction and observation to remove the ones related to this server
terencez 0:c2dff8cbb91a 93 if (NULL != serverP->location)
terencez 0:c2dff8cbb91a 94 {
terencez 0:c2dff8cbb91a 95 lwm2m_free(serverP->location);
terencez 0:c2dff8cbb91a 96 }
terencez 0:c2dff8cbb91a 97 free_block1_buffer(serverP->block1Data);
terencez 0:c2dff8cbb91a 98 lwm2m_free(serverP);
terencez 0:c2dff8cbb91a 99 }
terencez 0:c2dff8cbb91a 100
terencez 0:c2dff8cbb91a 101 static void prv_deleteServerList(lwm2m_context_t * context)
terencez 0:c2dff8cbb91a 102 {
terencez 0:c2dff8cbb91a 103 while (NULL != context->serverList)
terencez 0:c2dff8cbb91a 104 {
terencez 0:c2dff8cbb91a 105 lwm2m_server_t * server;
terencez 0:c2dff8cbb91a 106 server = context->serverList;
terencez 0:c2dff8cbb91a 107 context->serverList = server->next;
terencez 0:c2dff8cbb91a 108 prv_deleteServer(server);
terencez 0:c2dff8cbb91a 109 }
terencez 0:c2dff8cbb91a 110 }
terencez 0:c2dff8cbb91a 111
terencez 0:c2dff8cbb91a 112 static void prv_deleteBootstrapServer(lwm2m_server_t * serverP)
terencez 0:c2dff8cbb91a 113 {
terencez 0:c2dff8cbb91a 114 // TODO should we free location as in prv_deleteServer ?
terencez 0:c2dff8cbb91a 115 // TODO should we parse transaction and observation to remove the ones related to this server ?
terencez 0:c2dff8cbb91a 116 free_block1_buffer(serverP->block1Data);
terencez 0:c2dff8cbb91a 117 lwm2m_free(serverP);
terencez 0:c2dff8cbb91a 118 }
terencez 0:c2dff8cbb91a 119
terencez 0:c2dff8cbb91a 120 static void prv_deleteBootstrapServerList(lwm2m_context_t * context)
terencez 0:c2dff8cbb91a 121 {
terencez 0:c2dff8cbb91a 122 while (NULL != context->bootstrapServerList)
terencez 0:c2dff8cbb91a 123 {
terencez 0:c2dff8cbb91a 124 lwm2m_server_t * server;
terencez 0:c2dff8cbb91a 125 server = context->bootstrapServerList;
terencez 0:c2dff8cbb91a 126 context->bootstrapServerList = server->next;
terencez 0:c2dff8cbb91a 127 prv_deleteBootstrapServer(server);
terencez 0:c2dff8cbb91a 128 }
terencez 0:c2dff8cbb91a 129 }
terencez 0:c2dff8cbb91a 130
terencez 0:c2dff8cbb91a 131 static void prv_deleteObservedList(lwm2m_context_t * contextP)
terencez 0:c2dff8cbb91a 132 {
terencez 0:c2dff8cbb91a 133 while (NULL != contextP->observedList)
terencez 0:c2dff8cbb91a 134 {
terencez 0:c2dff8cbb91a 135 lwm2m_observed_t * targetP;
terencez 0:c2dff8cbb91a 136 lwm2m_watcher_t * watcherP;
terencez 0:c2dff8cbb91a 137
terencez 0:c2dff8cbb91a 138 targetP = contextP->observedList;
terencez 0:c2dff8cbb91a 139 contextP->observedList = contextP->observedList->next;
terencez 0:c2dff8cbb91a 140
terencez 0:c2dff8cbb91a 141 for (watcherP = targetP->watcherList ; watcherP != NULL ; watcherP = watcherP->next)
terencez 0:c2dff8cbb91a 142 {
terencez 0:c2dff8cbb91a 143 if (watcherP->parameters != NULL) lwm2m_free(watcherP->parameters);
terencez 0:c2dff8cbb91a 144 }
terencez 0:c2dff8cbb91a 145 LWM2M_LIST_FREE(targetP->watcherList);
terencez 0:c2dff8cbb91a 146
terencez 0:c2dff8cbb91a 147 lwm2m_free(targetP);
terencez 0:c2dff8cbb91a 148 }
terencez 0:c2dff8cbb91a 149 }
terencez 0:c2dff8cbb91a 150 #endif
terencez 0:c2dff8cbb91a 151
terencez 0:c2dff8cbb91a 152 void prv_deleteTransactionList(lwm2m_context_t * context)
terencez 0:c2dff8cbb91a 153 {
terencez 0:c2dff8cbb91a 154 while (NULL != context->transactionList)
terencez 0:c2dff8cbb91a 155 {
terencez 0:c2dff8cbb91a 156 lwm2m_transaction_t * transaction;
terencez 0:c2dff8cbb91a 157
terencez 0:c2dff8cbb91a 158 transaction = context->transactionList;
terencez 0:c2dff8cbb91a 159 context->transactionList = context->transactionList->next;
terencez 0:c2dff8cbb91a 160 transaction_free(transaction);
terencez 0:c2dff8cbb91a 161 }
terencez 0:c2dff8cbb91a 162 }
terencez 0:c2dff8cbb91a 163
terencez 0:c2dff8cbb91a 164 void lwm2m_close(lwm2m_context_t * contextP)
terencez 0:c2dff8cbb91a 165 {
terencez 0:c2dff8cbb91a 166 #ifdef LWM2M_CLIENT_MODE
terencez 0:c2dff8cbb91a 167
terencez 0:c2dff8cbb91a 168 LOG("Entering");
terencez 0:c2dff8cbb91a 169 lwm2m_deregister(contextP);
terencez 0:c2dff8cbb91a 170 prv_deleteServerList(contextP);
terencez 0:c2dff8cbb91a 171 prv_deleteBootstrapServerList(contextP);
terencez 0:c2dff8cbb91a 172 prv_deleteObservedList(contextP);
terencez 0:c2dff8cbb91a 173 lwm2m_free(contextP->endpointName);
terencez 0:c2dff8cbb91a 174 if (contextP->msisdn != NULL)
terencez 0:c2dff8cbb91a 175 {
terencez 0:c2dff8cbb91a 176 lwm2m_free(contextP->msisdn);
terencez 0:c2dff8cbb91a 177 }
terencez 0:c2dff8cbb91a 178 if (contextP->altPath != NULL)
terencez 0:c2dff8cbb91a 179 {
terencez 0:c2dff8cbb91a 180 lwm2m_free(contextP->altPath);
terencez 0:c2dff8cbb91a 181 }
terencez 0:c2dff8cbb91a 182
terencez 0:c2dff8cbb91a 183 #endif
terencez 0:c2dff8cbb91a 184
terencez 0:c2dff8cbb91a 185 #ifdef LWM2M_SERVER_MODE
terencez 0:c2dff8cbb91a 186 while (NULL != contextP->clientList)
terencez 0:c2dff8cbb91a 187 {
terencez 0:c2dff8cbb91a 188 lwm2m_client_t * clientP;
terencez 0:c2dff8cbb91a 189
terencez 0:c2dff8cbb91a 190 clientP = contextP->clientList;
terencez 0:c2dff8cbb91a 191 contextP->clientList = contextP->clientList->next;
terencez 0:c2dff8cbb91a 192
terencez 0:c2dff8cbb91a 193 registration_freeClient(clientP);
terencez 0:c2dff8cbb91a 194 }
terencez 0:c2dff8cbb91a 195 #endif
terencez 0:c2dff8cbb91a 196
terencez 0:c2dff8cbb91a 197 prv_deleteTransactionList(contextP);
terencez 0:c2dff8cbb91a 198 lwm2m_free(contextP);
terencez 0:c2dff8cbb91a 199 }
terencez 0:c2dff8cbb91a 200
terencez 0:c2dff8cbb91a 201 #ifdef LWM2M_CLIENT_MODE
terencez 0:c2dff8cbb91a 202 static int prv_refreshServerList(lwm2m_context_t * contextP)
terencez 0:c2dff8cbb91a 203 {
terencez 0:c2dff8cbb91a 204 lwm2m_server_t * targetP;
terencez 0:c2dff8cbb91a 205 lwm2m_server_t * nextP;
terencez 0:c2dff8cbb91a 206
terencez 0:c2dff8cbb91a 207 // Remove all servers marked as dirty
terencez 0:c2dff8cbb91a 208 targetP = contextP->bootstrapServerList;
terencez 0:c2dff8cbb91a 209 contextP->bootstrapServerList = NULL;
terencez 0:c2dff8cbb91a 210 while (targetP != NULL)
terencez 0:c2dff8cbb91a 211 {
terencez 0:c2dff8cbb91a 212 nextP = targetP->next;
terencez 0:c2dff8cbb91a 213 targetP->next = NULL;
terencez 0:c2dff8cbb91a 214 if (!targetP->dirty)
terencez 0:c2dff8cbb91a 215 {
terencez 0:c2dff8cbb91a 216 targetP->status = STATE_DEREGISTERED;
terencez 0:c2dff8cbb91a 217 contextP->bootstrapServerList = (lwm2m_server_t *)LWM2M_LIST_ADD(contextP->bootstrapServerList, targetP);
terencez 0:c2dff8cbb91a 218 }
terencez 0:c2dff8cbb91a 219 else
terencez 0:c2dff8cbb91a 220 {
terencez 0:c2dff8cbb91a 221 prv_deleteServer(targetP);
terencez 0:c2dff8cbb91a 222 }
terencez 0:c2dff8cbb91a 223 targetP = nextP;
terencez 0:c2dff8cbb91a 224 }
terencez 0:c2dff8cbb91a 225 targetP = contextP->serverList;
terencez 0:c2dff8cbb91a 226 contextP->serverList = NULL;
terencez 0:c2dff8cbb91a 227 while (targetP != NULL)
terencez 0:c2dff8cbb91a 228 {
terencez 0:c2dff8cbb91a 229 nextP = targetP->next;
terencez 0:c2dff8cbb91a 230 targetP->next = NULL;
terencez 0:c2dff8cbb91a 231 if (!targetP->dirty)
terencez 0:c2dff8cbb91a 232 {
terencez 0:c2dff8cbb91a 233 // TODO: Should we revert the status to STATE_DEREGISTERED ?
terencez 0:c2dff8cbb91a 234 contextP->serverList = (lwm2m_server_t *)LWM2M_LIST_ADD(contextP->serverList, targetP);
terencez 0:c2dff8cbb91a 235 }
terencez 0:c2dff8cbb91a 236 else
terencez 0:c2dff8cbb91a 237 {
terencez 0:c2dff8cbb91a 238 prv_deleteServer(targetP);
terencez 0:c2dff8cbb91a 239 }
terencez 0:c2dff8cbb91a 240 targetP = nextP;
terencez 0:c2dff8cbb91a 241 }
terencez 0:c2dff8cbb91a 242
terencez 0:c2dff8cbb91a 243 return object_getServers(contextP);
terencez 0:c2dff8cbb91a 244 }
terencez 0:c2dff8cbb91a 245
terencez 0:c2dff8cbb91a 246 int lwm2m_configure(lwm2m_context_t * contextP,
terencez 0:c2dff8cbb91a 247 const char * endpointName,
terencez 0:c2dff8cbb91a 248 const char * msisdn,
terencez 0:c2dff8cbb91a 249 const char * altPath,
terencez 0:c2dff8cbb91a 250 uint16_t numObject,
terencez 0:c2dff8cbb91a 251 lwm2m_object_t * objectList[])
terencez 0:c2dff8cbb91a 252 {
terencez 0:c2dff8cbb91a 253 int i;
terencez 0:c2dff8cbb91a 254 uint8_t found;
terencez 0:c2dff8cbb91a 255
terencez 0:c2dff8cbb91a 256 LOG_ARG("endpointName: \"%s\", msisdn: \"%s\", altPath: \"%s\", numObject: %d", endpointName, msisdn, altPath, numObject);
terencez 0:c2dff8cbb91a 257 // This API can be called only once for now
terencez 0:c2dff8cbb91a 258 if (contextP->endpointName != NULL || contextP->objectList != NULL) return COAP_400_BAD_REQUEST;
terencez 0:c2dff8cbb91a 259
terencez 0:c2dff8cbb91a 260 if (endpointName == NULL) return COAP_400_BAD_REQUEST;
terencez 0:c2dff8cbb91a 261 if (numObject < 3) return COAP_400_BAD_REQUEST;
terencez 0:c2dff8cbb91a 262 // Check that mandatory objects are present
terencez 0:c2dff8cbb91a 263 found = 0;
terencez 0:c2dff8cbb91a 264 for (i = 0 ; i < numObject ; i++)
terencez 0:c2dff8cbb91a 265 {
terencez 0:c2dff8cbb91a 266 if (objectList[i]->objID == LWM2M_SECURITY_OBJECT_ID) found |= 0x01;
terencez 0:c2dff8cbb91a 267 if (objectList[i]->objID == LWM2M_SERVER_OBJECT_ID) found |= 0x02;
terencez 0:c2dff8cbb91a 268 if (objectList[i]->objID == LWM2M_DEVICE_OBJECT_ID) found |= 0x04;
terencez 0:c2dff8cbb91a 269 }
terencez 0:c2dff8cbb91a 270 if (found != 0x07) return COAP_400_BAD_REQUEST;
terencez 0:c2dff8cbb91a 271 if (altPath != NULL)
terencez 0:c2dff8cbb91a 272 {
terencez 0:c2dff8cbb91a 273 if (0 == utils_isAltPathValid(altPath))
terencez 0:c2dff8cbb91a 274 {
terencez 0:c2dff8cbb91a 275 return COAP_400_BAD_REQUEST;
terencez 0:c2dff8cbb91a 276 }
terencez 0:c2dff8cbb91a 277 if (altPath[1] == 0)
terencez 0:c2dff8cbb91a 278 {
terencez 0:c2dff8cbb91a 279 altPath = NULL;
terencez 0:c2dff8cbb91a 280 }
terencez 0:c2dff8cbb91a 281 }
terencez 0:c2dff8cbb91a 282 contextP->endpointName = lwm2m_strdup(endpointName);
terencez 0:c2dff8cbb91a 283 if (contextP->endpointName == NULL)
terencez 0:c2dff8cbb91a 284 {
terencez 0:c2dff8cbb91a 285 return COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:c2dff8cbb91a 286 }
terencez 0:c2dff8cbb91a 287
terencez 0:c2dff8cbb91a 288 if (msisdn != NULL)
terencez 0:c2dff8cbb91a 289 {
terencez 0:c2dff8cbb91a 290 contextP->msisdn = lwm2m_strdup(msisdn);
terencez 0:c2dff8cbb91a 291 if (contextP->msisdn == NULL)
terencez 0:c2dff8cbb91a 292 {
terencez 0:c2dff8cbb91a 293 return COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:c2dff8cbb91a 294 }
terencez 0:c2dff8cbb91a 295 }
terencez 0:c2dff8cbb91a 296
terencez 0:c2dff8cbb91a 297 if (altPath != NULL)
terencez 0:c2dff8cbb91a 298 {
terencez 0:c2dff8cbb91a 299 contextP->altPath = lwm2m_strdup(altPath);
terencez 0:c2dff8cbb91a 300 if (contextP->altPath == NULL)
terencez 0:c2dff8cbb91a 301 {
terencez 0:c2dff8cbb91a 302 return COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:c2dff8cbb91a 303 }
terencez 0:c2dff8cbb91a 304 }
terencez 0:c2dff8cbb91a 305
terencez 0:c2dff8cbb91a 306 for (i = 0; i < numObject; i++)
terencez 0:c2dff8cbb91a 307 {
terencez 0:c2dff8cbb91a 308 objectList[i]->next = NULL;
terencez 0:c2dff8cbb91a 309 contextP->objectList = (lwm2m_object_t *)LWM2M_LIST_ADD(contextP->objectList, objectList[i]);
terencez 0:c2dff8cbb91a 310 }
terencez 0:c2dff8cbb91a 311
terencez 0:c2dff8cbb91a 312 return COAP_NO_ERROR;
terencez 0:c2dff8cbb91a 313 }
terencez 0:c2dff8cbb91a 314
terencez 0:c2dff8cbb91a 315 int lwm2m_add_object(lwm2m_context_t * contextP,
terencez 0:c2dff8cbb91a 316 lwm2m_object_t * objectP)
terencez 0:c2dff8cbb91a 317 {
terencez 0:c2dff8cbb91a 318 lwm2m_object_t * targetP;
terencez 0:c2dff8cbb91a 319
terencez 0:c2dff8cbb91a 320 LOG_ARG("ID: %d", objectP->objID);
terencez 0:c2dff8cbb91a 321 targetP = (lwm2m_object_t *)LWM2M_LIST_FIND(contextP->objectList, objectP->objID);
terencez 0:c2dff8cbb91a 322 if (targetP != NULL) return COAP_406_NOT_ACCEPTABLE;
terencez 0:c2dff8cbb91a 323 objectP->next = NULL;
terencez 0:c2dff8cbb91a 324
terencez 0:c2dff8cbb91a 325 contextP->objectList = (lwm2m_object_t *)LWM2M_LIST_ADD(contextP->objectList, objectP);
terencez 0:c2dff8cbb91a 326
terencez 0:c2dff8cbb91a 327 if (contextP->state == STATE_READY)
terencez 0:c2dff8cbb91a 328 {
terencez 0:c2dff8cbb91a 329 return lwm2m_update_registration(contextP, 0, true);
terencez 0:c2dff8cbb91a 330 }
terencez 0:c2dff8cbb91a 331
terencez 0:c2dff8cbb91a 332 return COAP_NO_ERROR;
terencez 0:c2dff8cbb91a 333 }
terencez 0:c2dff8cbb91a 334
terencez 0:c2dff8cbb91a 335 int lwm2m_remove_object(lwm2m_context_t * contextP,
terencez 0:c2dff8cbb91a 336 uint16_t id)
terencez 0:c2dff8cbb91a 337 {
terencez 0:c2dff8cbb91a 338 lwm2m_object_t * targetP;
terencez 0:c2dff8cbb91a 339
terencez 0:c2dff8cbb91a 340 LOG_ARG("ID: %d", id);
terencez 0:c2dff8cbb91a 341 contextP->objectList = (lwm2m_object_t *)LWM2M_LIST_RM(contextP->objectList, id, &targetP);
terencez 0:c2dff8cbb91a 342
terencez 0:c2dff8cbb91a 343 if (targetP == NULL) return COAP_404_NOT_FOUND;
terencez 0:c2dff8cbb91a 344
terencez 0:c2dff8cbb91a 345 if (contextP->state == STATE_READY)
terencez 0:c2dff8cbb91a 346 {
terencez 0:c2dff8cbb91a 347 return lwm2m_update_registration(contextP, 0, true);
terencez 0:c2dff8cbb91a 348 }
terencez 0:c2dff8cbb91a 349
terencez 0:c2dff8cbb91a 350 return 0;
terencez 0:c2dff8cbb91a 351 }
terencez 0:c2dff8cbb91a 352
terencez 0:c2dff8cbb91a 353 #endif
terencez 0:c2dff8cbb91a 354
terencez 0:c2dff8cbb91a 355
terencez 0:c2dff8cbb91a 356 int lwm2m_step(lwm2m_context_t * contextP,
terencez 0:c2dff8cbb91a 357 time_t * timeoutP)
terencez 0:c2dff8cbb91a 358 {
terencez 0:c2dff8cbb91a 359 time_t tv_sec;
terencez 0:c2dff8cbb91a 360 int result;
terencez 0:c2dff8cbb91a 361
terencez 0:c2dff8cbb91a 362 LOG_ARG("timeoutP: %" PRId64, *timeoutP);
terencez 0:c2dff8cbb91a 363 tv_sec = lwm2m_gettime();
terencez 0:c2dff8cbb91a 364 if (tv_sec < 0) return COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:c2dff8cbb91a 365
terencez 0:c2dff8cbb91a 366 #ifdef LWM2M_CLIENT_MODE
terencez 0:c2dff8cbb91a 367 LOG_ARG("State: %s", STR_STATE(contextP->state));
terencez 0:c2dff8cbb91a 368 // state can also be modified in bootstrap_handleCommand().
terencez 0:c2dff8cbb91a 369
terencez 0:c2dff8cbb91a 370 next_step:
terencez 0:c2dff8cbb91a 371 switch (contextP->state)
terencez 0:c2dff8cbb91a 372 {
terencez 0:c2dff8cbb91a 373 case STATE_INITIAL:
terencez 0:c2dff8cbb91a 374 if (0 != prv_refreshServerList(contextP)) return COAP_503_SERVICE_UNAVAILABLE;
terencez 0:c2dff8cbb91a 375 if (contextP->serverList != NULL)
terencez 0:c2dff8cbb91a 376 {
terencez 0:c2dff8cbb91a 377 contextP->state = STATE_REGISTER_REQUIRED;
terencez 0:c2dff8cbb91a 378 }
terencez 0:c2dff8cbb91a 379 else
terencez 0:c2dff8cbb91a 380 {
terencez 0:c2dff8cbb91a 381 // Bootstrapping
terencez 0:c2dff8cbb91a 382 contextP->state = STATE_BOOTSTRAP_REQUIRED;
terencez 0:c2dff8cbb91a 383 }
terencez 0:c2dff8cbb91a 384 goto next_step;
terencez 0:c2dff8cbb91a 385 break;
terencez 0:c2dff8cbb91a 386
terencez 0:c2dff8cbb91a 387 case STATE_BOOTSTRAP_REQUIRED:
terencez 0:c2dff8cbb91a 388 #ifdef LWM2M_BOOTSTRAP
terencez 0:c2dff8cbb91a 389 if (contextP->bootstrapServerList != NULL)
terencez 0:c2dff8cbb91a 390 {
terencez 0:c2dff8cbb91a 391 bootstrap_start(contextP);
terencez 0:c2dff8cbb91a 392 contextP->state = STATE_BOOTSTRAPPING;
terencez 0:c2dff8cbb91a 393 bootstrap_step(contextP, tv_sec, timeoutP);
terencez 0:c2dff8cbb91a 394 }
terencez 0:c2dff8cbb91a 395 else
terencez 0:c2dff8cbb91a 396 #endif
terencez 0:c2dff8cbb91a 397 {
terencez 0:c2dff8cbb91a 398 return COAP_503_SERVICE_UNAVAILABLE;
terencez 0:c2dff8cbb91a 399 }
terencez 0:c2dff8cbb91a 400 break;
terencez 0:c2dff8cbb91a 401
terencez 0:c2dff8cbb91a 402 #ifdef LWM2M_BOOTSTRAP
terencez 0:c2dff8cbb91a 403 case STATE_BOOTSTRAPPING:
terencez 0:c2dff8cbb91a 404 switch (bootstrap_getStatus(contextP))
terencez 0:c2dff8cbb91a 405 {
terencez 0:c2dff8cbb91a 406 case STATE_BS_FINISHED:
terencez 0:c2dff8cbb91a 407 contextP->state = STATE_INITIAL;
terencez 0:c2dff8cbb91a 408 goto next_step;
terencez 0:c2dff8cbb91a 409 break;
terencez 0:c2dff8cbb91a 410
terencez 0:c2dff8cbb91a 411 case STATE_BS_FAILED:
terencez 0:c2dff8cbb91a 412 return COAP_503_SERVICE_UNAVAILABLE;
terencez 0:c2dff8cbb91a 413
terencez 0:c2dff8cbb91a 414 default:
terencez 0:c2dff8cbb91a 415 // keep on waiting
terencez 0:c2dff8cbb91a 416 bootstrap_step(contextP, tv_sec, timeoutP);
terencez 0:c2dff8cbb91a 417 break;
terencez 0:c2dff8cbb91a 418 }
terencez 0:c2dff8cbb91a 419 break;
terencez 0:c2dff8cbb91a 420 #endif
terencez 0:c2dff8cbb91a 421 case STATE_REGISTER_REQUIRED:
terencez 0:c2dff8cbb91a 422 result = registration_start(contextP);
terencez 0:c2dff8cbb91a 423 if (COAP_NO_ERROR != result) return result;
terencez 0:c2dff8cbb91a 424 contextP->state = STATE_REGISTERING;
terencez 0:c2dff8cbb91a 425 break;
terencez 0:c2dff8cbb91a 426
terencez 0:c2dff8cbb91a 427 case STATE_REGISTERING:
terencez 0:c2dff8cbb91a 428 {
terencez 0:c2dff8cbb91a 429 switch (registration_getStatus(contextP))
terencez 0:c2dff8cbb91a 430 {
terencez 0:c2dff8cbb91a 431 case STATE_REGISTERED:
terencez 0:c2dff8cbb91a 432 contextP->state = STATE_READY;
terencez 0:c2dff8cbb91a 433 break;
terencez 0:c2dff8cbb91a 434
terencez 0:c2dff8cbb91a 435 case STATE_REG_FAILED:
terencez 0:c2dff8cbb91a 436 // TODO avoid infinite loop by checking the bootstrap info is different
terencez 0:c2dff8cbb91a 437 contextP->state = STATE_BOOTSTRAP_REQUIRED;
terencez 0:c2dff8cbb91a 438 goto next_step;
terencez 0:c2dff8cbb91a 439 break;
terencez 0:c2dff8cbb91a 440
terencez 0:c2dff8cbb91a 441 case STATE_REG_PENDING:
terencez 0:c2dff8cbb91a 442 default:
terencez 0:c2dff8cbb91a 443 // keep on waiting
terencez 0:c2dff8cbb91a 444 break;
terencez 0:c2dff8cbb91a 445 }
terencez 0:c2dff8cbb91a 446 }
terencez 0:c2dff8cbb91a 447 break;
terencez 0:c2dff8cbb91a 448
terencez 0:c2dff8cbb91a 449 case STATE_READY:
terencez 0:c2dff8cbb91a 450 if (registration_getStatus(contextP) == STATE_REG_FAILED)
terencez 0:c2dff8cbb91a 451 {
terencez 0:c2dff8cbb91a 452 // TODO avoid infinite loop by checking the bootstrap info is different
terencez 0:c2dff8cbb91a 453 contextP->state = STATE_BOOTSTRAP_REQUIRED;
terencez 0:c2dff8cbb91a 454 goto next_step;
terencez 0:c2dff8cbb91a 455 break;
terencez 0:c2dff8cbb91a 456 }
terencez 0:c2dff8cbb91a 457 break;
terencez 0:c2dff8cbb91a 458
terencez 0:c2dff8cbb91a 459 default:
terencez 0:c2dff8cbb91a 460 // do nothing
terencez 0:c2dff8cbb91a 461 break;
terencez 0:c2dff8cbb91a 462 }
terencez 0:c2dff8cbb91a 463
terencez 0:c2dff8cbb91a 464 observe_step(contextP, tv_sec, timeoutP);
terencez 0:c2dff8cbb91a 465 #endif
terencez 0:c2dff8cbb91a 466
terencez 0:c2dff8cbb91a 467 registration_step(contextP, tv_sec, timeoutP);
terencez 0:c2dff8cbb91a 468 transaction_step(contextP, tv_sec, timeoutP);
terencez 0:c2dff8cbb91a 469
terencez 0:c2dff8cbb91a 470 LOG_ARG("Final timeoutP: %" PRId64, *timeoutP);
terencez 0:c2dff8cbb91a 471 #ifdef LWM2M_CLIENT_MODE
terencez 0:c2dff8cbb91a 472 LOG_ARG("Final state: %s", STR_STATE(contextP->state));
terencez 0:c2dff8cbb91a 473 #endif
terencez 0:c2dff8cbb91a 474 return 0;
terencez 0:c2dff8cbb91a 475 }