pick up wakaama files from https://github.com/eclipse/wakaama
core/liblwm2m.c@0:1fa43ab66921, 2017-04-19 (annotated)
- Committer:
- terencez
- Date:
- Wed Apr 19 11:30:02 2017 +0000
- Revision:
- 0:1fa43ab66921
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
terencez | 0:1fa43ab66921 | 1 | /******************************************************************************* |
terencez | 0:1fa43ab66921 | 2 | * |
terencez | 0:1fa43ab66921 | 3 | * Copyright (c) 2013, 2014 Intel Corporation and others. |
terencez | 0:1fa43ab66921 | 4 | * All rights reserved. This program and the accompanying materials |
terencez | 0:1fa43ab66921 | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
terencez | 0:1fa43ab66921 | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
terencez | 0:1fa43ab66921 | 7 | * |
terencez | 0:1fa43ab66921 | 8 | * The Eclipse Public License is available at |
terencez | 0:1fa43ab66921 | 9 | * http://www.eclipse.org/legal/epl-v10.html |
terencez | 0:1fa43ab66921 | 10 | * The Eclipse Distribution License is available at |
terencez | 0:1fa43ab66921 | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. |
terencez | 0:1fa43ab66921 | 12 | * |
terencez | 0:1fa43ab66921 | 13 | * Contributors: |
terencez | 0:1fa43ab66921 | 14 | * David Navarro, Intel Corporation - initial API and implementation |
terencez | 0:1fa43ab66921 | 15 | * Fabien Fleutot - Please refer to git log |
terencez | 0:1fa43ab66921 | 16 | * Simon Bernard - Please refer to git log |
terencez | 0:1fa43ab66921 | 17 | * Toby Jaffey - 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 | |
terencez | 0:1fa43ab66921 | 54 | #include <stdlib.h> |
terencez | 0:1fa43ab66921 | 55 | #include <string.h> |
terencez | 0:1fa43ab66921 | 56 | |
terencez | 0:1fa43ab66921 | 57 | #include <stdio.h> |
terencez | 0:1fa43ab66921 | 58 | |
terencez | 0:1fa43ab66921 | 59 | |
terencez | 0:1fa43ab66921 | 60 | lwm2m_context_t * lwm2m_init(void * userData) |
terencez | 0:1fa43ab66921 | 61 | { |
terencez | 0:1fa43ab66921 | 62 | lwm2m_context_t * contextP; |
terencez | 0:1fa43ab66921 | 63 | |
terencez | 0:1fa43ab66921 | 64 | LOG("Entering"); |
terencez | 0:1fa43ab66921 | 65 | contextP = (lwm2m_context_t *)lwm2m_malloc(sizeof(lwm2m_context_t)); |
terencez | 0:1fa43ab66921 | 66 | if (NULL != contextP) |
terencez | 0:1fa43ab66921 | 67 | { |
terencez | 0:1fa43ab66921 | 68 | memset(contextP, 0, sizeof(lwm2m_context_t)); |
terencez | 0:1fa43ab66921 | 69 | contextP->userData = userData; |
terencez | 0:1fa43ab66921 | 70 | srand((int)lwm2m_gettime()); |
terencez | 0:1fa43ab66921 | 71 | contextP->nextMID = rand(); |
terencez | 0:1fa43ab66921 | 72 | } |
terencez | 0:1fa43ab66921 | 73 | |
terencez | 0:1fa43ab66921 | 74 | return contextP; |
terencez | 0:1fa43ab66921 | 75 | } |
terencez | 0:1fa43ab66921 | 76 | |
terencez | 0:1fa43ab66921 | 77 | #ifdef LWM2M_CLIENT_MODE |
terencez | 0:1fa43ab66921 | 78 | void lwm2m_deregister(lwm2m_context_t * context) |
terencez | 0:1fa43ab66921 | 79 | { |
terencez | 0:1fa43ab66921 | 80 | lwm2m_server_t * server = context->serverList; |
terencez | 0:1fa43ab66921 | 81 | |
terencez | 0:1fa43ab66921 | 82 | LOG("Entering"); |
terencez | 0:1fa43ab66921 | 83 | while (NULL != server) |
terencez | 0:1fa43ab66921 | 84 | { |
terencez | 0:1fa43ab66921 | 85 | registration_deregister(context, server); |
terencez | 0:1fa43ab66921 | 86 | server = server->next; |
terencez | 0:1fa43ab66921 | 87 | } |
terencez | 0:1fa43ab66921 | 88 | } |
terencez | 0:1fa43ab66921 | 89 | |
terencez | 0:1fa43ab66921 | 90 | static void prv_deleteServer(lwm2m_server_t * serverP) |
terencez | 0:1fa43ab66921 | 91 | { |
terencez | 0:1fa43ab66921 | 92 | // TODO parse transaction and observation to remove the ones related to this server |
terencez | 0:1fa43ab66921 | 93 | if (NULL != serverP->location) |
terencez | 0:1fa43ab66921 | 94 | { |
terencez | 0:1fa43ab66921 | 95 | lwm2m_free(serverP->location); |
terencez | 0:1fa43ab66921 | 96 | } |
terencez | 0:1fa43ab66921 | 97 | free_block1_buffer(serverP->block1Data); |
terencez | 0:1fa43ab66921 | 98 | lwm2m_free(serverP); |
terencez | 0:1fa43ab66921 | 99 | } |
terencez | 0:1fa43ab66921 | 100 | |
terencez | 0:1fa43ab66921 | 101 | static void prv_deleteServerList(lwm2m_context_t * context) |
terencez | 0:1fa43ab66921 | 102 | { |
terencez | 0:1fa43ab66921 | 103 | while (NULL != context->serverList) |
terencez | 0:1fa43ab66921 | 104 | { |
terencez | 0:1fa43ab66921 | 105 | lwm2m_server_t * server; |
terencez | 0:1fa43ab66921 | 106 | server = context->serverList; |
terencez | 0:1fa43ab66921 | 107 | context->serverList = server->next; |
terencez | 0:1fa43ab66921 | 108 | prv_deleteServer(server); |
terencez | 0:1fa43ab66921 | 109 | } |
terencez | 0:1fa43ab66921 | 110 | } |
terencez | 0:1fa43ab66921 | 111 | |
terencez | 0:1fa43ab66921 | 112 | static void prv_deleteBootstrapServer(lwm2m_server_t * serverP) |
terencez | 0:1fa43ab66921 | 113 | { |
terencez | 0:1fa43ab66921 | 114 | // TODO should we free location as in prv_deleteServer ? |
terencez | 0:1fa43ab66921 | 115 | // TODO should we parse transaction and observation to remove the ones related to this server ? |
terencez | 0:1fa43ab66921 | 116 | free_block1_buffer(serverP->block1Data); |
terencez | 0:1fa43ab66921 | 117 | lwm2m_free(serverP); |
terencez | 0:1fa43ab66921 | 118 | } |
terencez | 0:1fa43ab66921 | 119 | |
terencez | 0:1fa43ab66921 | 120 | static void prv_deleteBootstrapServerList(lwm2m_context_t * context) |
terencez | 0:1fa43ab66921 | 121 | { |
terencez | 0:1fa43ab66921 | 122 | while (NULL != context->bootstrapServerList) |
terencez | 0:1fa43ab66921 | 123 | { |
terencez | 0:1fa43ab66921 | 124 | lwm2m_server_t * server; |
terencez | 0:1fa43ab66921 | 125 | server = context->bootstrapServerList; |
terencez | 0:1fa43ab66921 | 126 | context->bootstrapServerList = server->next; |
terencez | 0:1fa43ab66921 | 127 | prv_deleteBootstrapServer(server); |
terencez | 0:1fa43ab66921 | 128 | } |
terencez | 0:1fa43ab66921 | 129 | } |
terencez | 0:1fa43ab66921 | 130 | |
terencez | 0:1fa43ab66921 | 131 | static void prv_deleteObservedList(lwm2m_context_t * contextP) |
terencez | 0:1fa43ab66921 | 132 | { |
terencez | 0:1fa43ab66921 | 133 | while (NULL != contextP->observedList) |
terencez | 0:1fa43ab66921 | 134 | { |
terencez | 0:1fa43ab66921 | 135 | lwm2m_observed_t * targetP; |
terencez | 0:1fa43ab66921 | 136 | lwm2m_watcher_t * watcherP; |
terencez | 0:1fa43ab66921 | 137 | |
terencez | 0:1fa43ab66921 | 138 | targetP = contextP->observedList; |
terencez | 0:1fa43ab66921 | 139 | contextP->observedList = contextP->observedList->next; |
terencez | 0:1fa43ab66921 | 140 | |
terencez | 0:1fa43ab66921 | 141 | for (watcherP = targetP->watcherList ; watcherP != NULL ; watcherP = watcherP->next) |
terencez | 0:1fa43ab66921 | 142 | { |
terencez | 0:1fa43ab66921 | 143 | if (watcherP->parameters != NULL) lwm2m_free(watcherP->parameters); |
terencez | 0:1fa43ab66921 | 144 | } |
terencez | 0:1fa43ab66921 | 145 | LWM2M_LIST_FREE(targetP->watcherList); |
terencez | 0:1fa43ab66921 | 146 | |
terencez | 0:1fa43ab66921 | 147 | lwm2m_free(targetP); |
terencez | 0:1fa43ab66921 | 148 | } |
terencez | 0:1fa43ab66921 | 149 | } |
terencez | 0:1fa43ab66921 | 150 | #endif |
terencez | 0:1fa43ab66921 | 151 | |
terencez | 0:1fa43ab66921 | 152 | void prv_deleteTransactionList(lwm2m_context_t * context) |
terencez | 0:1fa43ab66921 | 153 | { |
terencez | 0:1fa43ab66921 | 154 | while (NULL != context->transactionList) |
terencez | 0:1fa43ab66921 | 155 | { |
terencez | 0:1fa43ab66921 | 156 | lwm2m_transaction_t * transaction; |
terencez | 0:1fa43ab66921 | 157 | |
terencez | 0:1fa43ab66921 | 158 | transaction = context->transactionList; |
terencez | 0:1fa43ab66921 | 159 | context->transactionList = context->transactionList->next; |
terencez | 0:1fa43ab66921 | 160 | transaction_free(transaction); |
terencez | 0:1fa43ab66921 | 161 | } |
terencez | 0:1fa43ab66921 | 162 | } |
terencez | 0:1fa43ab66921 | 163 | |
terencez | 0:1fa43ab66921 | 164 | void lwm2m_close(lwm2m_context_t * contextP) |
terencez | 0:1fa43ab66921 | 165 | { |
terencez | 0:1fa43ab66921 | 166 | #ifdef LWM2M_CLIENT_MODE |
terencez | 0:1fa43ab66921 | 167 | |
terencez | 0:1fa43ab66921 | 168 | LOG("Entering"); |
terencez | 0:1fa43ab66921 | 169 | lwm2m_deregister(contextP); |
terencez | 0:1fa43ab66921 | 170 | prv_deleteServerList(contextP); |
terencez | 0:1fa43ab66921 | 171 | prv_deleteBootstrapServerList(contextP); |
terencez | 0:1fa43ab66921 | 172 | prv_deleteObservedList(contextP); |
terencez | 0:1fa43ab66921 | 173 | lwm2m_free(contextP->endpointName); |
terencez | 0:1fa43ab66921 | 174 | if (contextP->msisdn != NULL) |
terencez | 0:1fa43ab66921 | 175 | { |
terencez | 0:1fa43ab66921 | 176 | lwm2m_free(contextP->msisdn); |
terencez | 0:1fa43ab66921 | 177 | } |
terencez | 0:1fa43ab66921 | 178 | if (contextP->altPath != NULL) |
terencez | 0:1fa43ab66921 | 179 | { |
terencez | 0:1fa43ab66921 | 180 | lwm2m_free(contextP->altPath); |
terencez | 0:1fa43ab66921 | 181 | } |
terencez | 0:1fa43ab66921 | 182 | |
terencez | 0:1fa43ab66921 | 183 | #endif |
terencez | 0:1fa43ab66921 | 184 | |
terencez | 0:1fa43ab66921 | 185 | #ifdef LWM2M_SERVER_MODE |
terencez | 0:1fa43ab66921 | 186 | while (NULL != contextP->clientList) |
terencez | 0:1fa43ab66921 | 187 | { |
terencez | 0:1fa43ab66921 | 188 | lwm2m_client_t * clientP; |
terencez | 0:1fa43ab66921 | 189 | |
terencez | 0:1fa43ab66921 | 190 | clientP = contextP->clientList; |
terencez | 0:1fa43ab66921 | 191 | contextP->clientList = contextP->clientList->next; |
terencez | 0:1fa43ab66921 | 192 | |
terencez | 0:1fa43ab66921 | 193 | registration_freeClient(clientP); |
terencez | 0:1fa43ab66921 | 194 | } |
terencez | 0:1fa43ab66921 | 195 | #endif |
terencez | 0:1fa43ab66921 | 196 | |
terencez | 0:1fa43ab66921 | 197 | prv_deleteTransactionList(contextP); |
terencez | 0:1fa43ab66921 | 198 | lwm2m_free(contextP); |
terencez | 0:1fa43ab66921 | 199 | } |
terencez | 0:1fa43ab66921 | 200 | |
terencez | 0:1fa43ab66921 | 201 | #ifdef LWM2M_CLIENT_MODE |
terencez | 0:1fa43ab66921 | 202 | static int prv_refreshServerList(lwm2m_context_t * contextP) |
terencez | 0:1fa43ab66921 | 203 | { |
terencez | 0:1fa43ab66921 | 204 | lwm2m_server_t * targetP; |
terencez | 0:1fa43ab66921 | 205 | lwm2m_server_t * nextP; |
terencez | 0:1fa43ab66921 | 206 | |
terencez | 0:1fa43ab66921 | 207 | // Remove all servers marked as dirty |
terencez | 0:1fa43ab66921 | 208 | targetP = contextP->bootstrapServerList; |
terencez | 0:1fa43ab66921 | 209 | contextP->bootstrapServerList = NULL; |
terencez | 0:1fa43ab66921 | 210 | while (targetP != NULL) |
terencez | 0:1fa43ab66921 | 211 | { |
terencez | 0:1fa43ab66921 | 212 | nextP = targetP->next; |
terencez | 0:1fa43ab66921 | 213 | targetP->next = NULL; |
terencez | 0:1fa43ab66921 | 214 | if (!targetP->dirty) |
terencez | 0:1fa43ab66921 | 215 | { |
terencez | 0:1fa43ab66921 | 216 | targetP->status = STATE_DEREGISTERED; |
terencez | 0:1fa43ab66921 | 217 | contextP->bootstrapServerList = (lwm2m_server_t *)LWM2M_LIST_ADD(contextP->bootstrapServerList, targetP); |
terencez | 0:1fa43ab66921 | 218 | } |
terencez | 0:1fa43ab66921 | 219 | else |
terencez | 0:1fa43ab66921 | 220 | { |
terencez | 0:1fa43ab66921 | 221 | prv_deleteServer(targetP); |
terencez | 0:1fa43ab66921 | 222 | } |
terencez | 0:1fa43ab66921 | 223 | targetP = nextP; |
terencez | 0:1fa43ab66921 | 224 | } |
terencez | 0:1fa43ab66921 | 225 | targetP = contextP->serverList; |
terencez | 0:1fa43ab66921 | 226 | contextP->serverList = NULL; |
terencez | 0:1fa43ab66921 | 227 | while (targetP != NULL) |
terencez | 0:1fa43ab66921 | 228 | { |
terencez | 0:1fa43ab66921 | 229 | nextP = targetP->next; |
terencez | 0:1fa43ab66921 | 230 | targetP->next = NULL; |
terencez | 0:1fa43ab66921 | 231 | if (!targetP->dirty) |
terencez | 0:1fa43ab66921 | 232 | { |
terencez | 0:1fa43ab66921 | 233 | // TODO: Should we revert the status to STATE_DEREGISTERED ? |
terencez | 0:1fa43ab66921 | 234 | contextP->serverList = (lwm2m_server_t *)LWM2M_LIST_ADD(contextP->serverList, targetP); |
terencez | 0:1fa43ab66921 | 235 | } |
terencez | 0:1fa43ab66921 | 236 | else |
terencez | 0:1fa43ab66921 | 237 | { |
terencez | 0:1fa43ab66921 | 238 | prv_deleteServer(targetP); |
terencez | 0:1fa43ab66921 | 239 | } |
terencez | 0:1fa43ab66921 | 240 | targetP = nextP; |
terencez | 0:1fa43ab66921 | 241 | } |
terencez | 0:1fa43ab66921 | 242 | |
terencez | 0:1fa43ab66921 | 243 | return object_getServers(contextP); |
terencez | 0:1fa43ab66921 | 244 | } |
terencez | 0:1fa43ab66921 | 245 | |
terencez | 0:1fa43ab66921 | 246 | int lwm2m_configure(lwm2m_context_t * contextP, |
terencez | 0:1fa43ab66921 | 247 | const char * endpointName, |
terencez | 0:1fa43ab66921 | 248 | const char * msisdn, |
terencez | 0:1fa43ab66921 | 249 | const char * altPath, |
terencez | 0:1fa43ab66921 | 250 | uint16_t numObject, |
terencez | 0:1fa43ab66921 | 251 | lwm2m_object_t * objectList[]) |
terencez | 0:1fa43ab66921 | 252 | { |
terencez | 0:1fa43ab66921 | 253 | int i; |
terencez | 0:1fa43ab66921 | 254 | uint8_t found; |
terencez | 0:1fa43ab66921 | 255 | |
terencez | 0:1fa43ab66921 | 256 | LOG_ARG("endpointName: \"%s\", msisdn: \"%s\", altPath: \"%s\", numObject: %d", endpointName, msisdn, altPath, numObject); |
terencez | 0:1fa43ab66921 | 257 | // This API can be called only once for now |
terencez | 0:1fa43ab66921 | 258 | if (contextP->endpointName != NULL || contextP->objectList != NULL) return COAP_400_BAD_REQUEST; |
terencez | 0:1fa43ab66921 | 259 | |
terencez | 0:1fa43ab66921 | 260 | if (endpointName == NULL) return COAP_400_BAD_REQUEST; |
terencez | 0:1fa43ab66921 | 261 | if (numObject < 3) return COAP_400_BAD_REQUEST; |
terencez | 0:1fa43ab66921 | 262 | // Check that mandatory objects are present |
terencez | 0:1fa43ab66921 | 263 | found = 0; |
terencez | 0:1fa43ab66921 | 264 | for (i = 0 ; i < numObject ; i++) |
terencez | 0:1fa43ab66921 | 265 | { |
terencez | 0:1fa43ab66921 | 266 | if (objectList[i]->objID == LWM2M_SECURITY_OBJECT_ID) found |= 0x01; |
terencez | 0:1fa43ab66921 | 267 | if (objectList[i]->objID == LWM2M_SERVER_OBJECT_ID) found |= 0x02; |
terencez | 0:1fa43ab66921 | 268 | if (objectList[i]->objID == LWM2M_DEVICE_OBJECT_ID) found |= 0x04; |
terencez | 0:1fa43ab66921 | 269 | } |
terencez | 0:1fa43ab66921 | 270 | if (found != 0x07) return COAP_400_BAD_REQUEST; |
terencez | 0:1fa43ab66921 | 271 | if (altPath != NULL) |
terencez | 0:1fa43ab66921 | 272 | { |
terencez | 0:1fa43ab66921 | 273 | if (0 == utils_isAltPathValid(altPath)) |
terencez | 0:1fa43ab66921 | 274 | { |
terencez | 0:1fa43ab66921 | 275 | return COAP_400_BAD_REQUEST; |
terencez | 0:1fa43ab66921 | 276 | } |
terencez | 0:1fa43ab66921 | 277 | if (altPath[1] == 0) |
terencez | 0:1fa43ab66921 | 278 | { |
terencez | 0:1fa43ab66921 | 279 | altPath = NULL; |
terencez | 0:1fa43ab66921 | 280 | } |
terencez | 0:1fa43ab66921 | 281 | } |
terencez | 0:1fa43ab66921 | 282 | contextP->endpointName = lwm2m_strdup(endpointName); |
terencez | 0:1fa43ab66921 | 283 | if (contextP->endpointName == NULL) |
terencez | 0:1fa43ab66921 | 284 | { |
terencez | 0:1fa43ab66921 | 285 | return COAP_500_INTERNAL_SERVER_ERROR; |
terencez | 0:1fa43ab66921 | 286 | } |
terencez | 0:1fa43ab66921 | 287 | |
terencez | 0:1fa43ab66921 | 288 | if (msisdn != NULL) |
terencez | 0:1fa43ab66921 | 289 | { |
terencez | 0:1fa43ab66921 | 290 | contextP->msisdn = lwm2m_strdup(msisdn); |
terencez | 0:1fa43ab66921 | 291 | if (contextP->msisdn == NULL) |
terencez | 0:1fa43ab66921 | 292 | { |
terencez | 0:1fa43ab66921 | 293 | return COAP_500_INTERNAL_SERVER_ERROR; |
terencez | 0:1fa43ab66921 | 294 | } |
terencez | 0:1fa43ab66921 | 295 | } |
terencez | 0:1fa43ab66921 | 296 | |
terencez | 0:1fa43ab66921 | 297 | if (altPath != NULL) |
terencez | 0:1fa43ab66921 | 298 | { |
terencez | 0:1fa43ab66921 | 299 | contextP->altPath = lwm2m_strdup(altPath); |
terencez | 0:1fa43ab66921 | 300 | if (contextP->altPath == NULL) |
terencez | 0:1fa43ab66921 | 301 | { |
terencez | 0:1fa43ab66921 | 302 | return COAP_500_INTERNAL_SERVER_ERROR; |
terencez | 0:1fa43ab66921 | 303 | } |
terencez | 0:1fa43ab66921 | 304 | } |
terencez | 0:1fa43ab66921 | 305 | |
terencez | 0:1fa43ab66921 | 306 | for (i = 0; i < numObject; i++) |
terencez | 0:1fa43ab66921 | 307 | { |
terencez | 0:1fa43ab66921 | 308 | objectList[i]->next = NULL; |
terencez | 0:1fa43ab66921 | 309 | contextP->objectList = (lwm2m_object_t *)LWM2M_LIST_ADD(contextP->objectList, objectList[i]); |
terencez | 0:1fa43ab66921 | 310 | } |
terencez | 0:1fa43ab66921 | 311 | |
terencez | 0:1fa43ab66921 | 312 | return COAP_NO_ERROR; |
terencez | 0:1fa43ab66921 | 313 | } |
terencez | 0:1fa43ab66921 | 314 | |
terencez | 0:1fa43ab66921 | 315 | int lwm2m_add_object(lwm2m_context_t * contextP, |
terencez | 0:1fa43ab66921 | 316 | lwm2m_object_t * objectP) |
terencez | 0:1fa43ab66921 | 317 | { |
terencez | 0:1fa43ab66921 | 318 | lwm2m_object_t * targetP; |
terencez | 0:1fa43ab66921 | 319 | |
terencez | 0:1fa43ab66921 | 320 | LOG_ARG("ID: %d", objectP->objID); |
terencez | 0:1fa43ab66921 | 321 | targetP = (lwm2m_object_t *)LWM2M_LIST_FIND(contextP->objectList, objectP->objID); |
terencez | 0:1fa43ab66921 | 322 | if (targetP != NULL) return COAP_406_NOT_ACCEPTABLE; |
terencez | 0:1fa43ab66921 | 323 | objectP->next = NULL; |
terencez | 0:1fa43ab66921 | 324 | |
terencez | 0:1fa43ab66921 | 325 | contextP->objectList = (lwm2m_object_t *)LWM2M_LIST_ADD(contextP->objectList, objectP); |
terencez | 0:1fa43ab66921 | 326 | |
terencez | 0:1fa43ab66921 | 327 | if (contextP->state == STATE_READY) |
terencez | 0:1fa43ab66921 | 328 | { |
terencez | 0:1fa43ab66921 | 329 | return lwm2m_update_registration(contextP, 0, true); |
terencez | 0:1fa43ab66921 | 330 | } |
terencez | 0:1fa43ab66921 | 331 | |
terencez | 0:1fa43ab66921 | 332 | return COAP_NO_ERROR; |
terencez | 0:1fa43ab66921 | 333 | } |
terencez | 0:1fa43ab66921 | 334 | |
terencez | 0:1fa43ab66921 | 335 | int lwm2m_remove_object(lwm2m_context_t * contextP, |
terencez | 0:1fa43ab66921 | 336 | uint16_t id) |
terencez | 0:1fa43ab66921 | 337 | { |
terencez | 0:1fa43ab66921 | 338 | lwm2m_object_t * targetP; |
terencez | 0:1fa43ab66921 | 339 | |
terencez | 0:1fa43ab66921 | 340 | LOG_ARG("ID: %d", id); |
terencez | 0:1fa43ab66921 | 341 | contextP->objectList = (lwm2m_object_t *)LWM2M_LIST_RM(contextP->objectList, id, &targetP); |
terencez | 0:1fa43ab66921 | 342 | |
terencez | 0:1fa43ab66921 | 343 | if (targetP == NULL) return COAP_404_NOT_FOUND; |
terencez | 0:1fa43ab66921 | 344 | |
terencez | 0:1fa43ab66921 | 345 | if (contextP->state == STATE_READY) |
terencez | 0:1fa43ab66921 | 346 | { |
terencez | 0:1fa43ab66921 | 347 | return lwm2m_update_registration(contextP, 0, true); |
terencez | 0:1fa43ab66921 | 348 | } |
terencez | 0:1fa43ab66921 | 349 | |
terencez | 0:1fa43ab66921 | 350 | return 0; |
terencez | 0:1fa43ab66921 | 351 | } |
terencez | 0:1fa43ab66921 | 352 | |
terencez | 0:1fa43ab66921 | 353 | #endif |
terencez | 0:1fa43ab66921 | 354 | |
terencez | 0:1fa43ab66921 | 355 | |
terencez | 0:1fa43ab66921 | 356 | int lwm2m_step(lwm2m_context_t * contextP, |
terencez | 0:1fa43ab66921 | 357 | time_t * timeoutP) |
terencez | 0:1fa43ab66921 | 358 | { |
terencez | 0:1fa43ab66921 | 359 | time_t tv_sec; |
terencez | 0:1fa43ab66921 | 360 | int result; |
terencez | 0:1fa43ab66921 | 361 | |
terencez | 0:1fa43ab66921 | 362 | LOG_ARG("timeoutP: %" PRId64, *timeoutP); |
terencez | 0:1fa43ab66921 | 363 | tv_sec = lwm2m_gettime(); |
terencez | 0:1fa43ab66921 | 364 | if (tv_sec < 0) return COAP_500_INTERNAL_SERVER_ERROR; |
terencez | 0:1fa43ab66921 | 365 | |
terencez | 0:1fa43ab66921 | 366 | #ifdef LWM2M_CLIENT_MODE |
terencez | 0:1fa43ab66921 | 367 | LOG_ARG("State: %s", STR_STATE(contextP->state)); |
terencez | 0:1fa43ab66921 | 368 | // state can also be modified in bootstrap_handleCommand(). |
terencez | 0:1fa43ab66921 | 369 | |
terencez | 0:1fa43ab66921 | 370 | next_step: |
terencez | 0:1fa43ab66921 | 371 | switch (contextP->state) |
terencez | 0:1fa43ab66921 | 372 | { |
terencez | 0:1fa43ab66921 | 373 | case STATE_INITIAL: |
terencez | 0:1fa43ab66921 | 374 | if (0 != prv_refreshServerList(contextP)) return COAP_503_SERVICE_UNAVAILABLE; |
terencez | 0:1fa43ab66921 | 375 | if (contextP->serverList != NULL) |
terencez | 0:1fa43ab66921 | 376 | { |
terencez | 0:1fa43ab66921 | 377 | contextP->state = STATE_REGISTER_REQUIRED; |
terencez | 0:1fa43ab66921 | 378 | } |
terencez | 0:1fa43ab66921 | 379 | else |
terencez | 0:1fa43ab66921 | 380 | { |
terencez | 0:1fa43ab66921 | 381 | // Bootstrapping |
terencez | 0:1fa43ab66921 | 382 | contextP->state = STATE_BOOTSTRAP_REQUIRED; |
terencez | 0:1fa43ab66921 | 383 | } |
terencez | 0:1fa43ab66921 | 384 | goto next_step; |
terencez | 0:1fa43ab66921 | 385 | break; |
terencez | 0:1fa43ab66921 | 386 | |
terencez | 0:1fa43ab66921 | 387 | case STATE_BOOTSTRAP_REQUIRED: |
terencez | 0:1fa43ab66921 | 388 | #ifdef LWM2M_BOOTSTRAP |
terencez | 0:1fa43ab66921 | 389 | if (contextP->bootstrapServerList != NULL) |
terencez | 0:1fa43ab66921 | 390 | { |
terencez | 0:1fa43ab66921 | 391 | bootstrap_start(contextP); |
terencez | 0:1fa43ab66921 | 392 | contextP->state = STATE_BOOTSTRAPPING; |
terencez | 0:1fa43ab66921 | 393 | bootstrap_step(contextP, tv_sec, timeoutP); |
terencez | 0:1fa43ab66921 | 394 | } |
terencez | 0:1fa43ab66921 | 395 | else |
terencez | 0:1fa43ab66921 | 396 | #endif |
terencez | 0:1fa43ab66921 | 397 | { |
terencez | 0:1fa43ab66921 | 398 | return COAP_503_SERVICE_UNAVAILABLE; |
terencez | 0:1fa43ab66921 | 399 | } |
terencez | 0:1fa43ab66921 | 400 | break; |
terencez | 0:1fa43ab66921 | 401 | |
terencez | 0:1fa43ab66921 | 402 | #ifdef LWM2M_BOOTSTRAP |
terencez | 0:1fa43ab66921 | 403 | case STATE_BOOTSTRAPPING: |
terencez | 0:1fa43ab66921 | 404 | switch (bootstrap_getStatus(contextP)) |
terencez | 0:1fa43ab66921 | 405 | { |
terencez | 0:1fa43ab66921 | 406 | case STATE_BS_FINISHED: |
terencez | 0:1fa43ab66921 | 407 | contextP->state = STATE_INITIAL; |
terencez | 0:1fa43ab66921 | 408 | goto next_step; |
terencez | 0:1fa43ab66921 | 409 | break; |
terencez | 0:1fa43ab66921 | 410 | |
terencez | 0:1fa43ab66921 | 411 | case STATE_BS_FAILED: |
terencez | 0:1fa43ab66921 | 412 | return COAP_503_SERVICE_UNAVAILABLE; |
terencez | 0:1fa43ab66921 | 413 | |
terencez | 0:1fa43ab66921 | 414 | default: |
terencez | 0:1fa43ab66921 | 415 | // keep on waiting |
terencez | 0:1fa43ab66921 | 416 | bootstrap_step(contextP, tv_sec, timeoutP); |
terencez | 0:1fa43ab66921 | 417 | break; |
terencez | 0:1fa43ab66921 | 418 | } |
terencez | 0:1fa43ab66921 | 419 | break; |
terencez | 0:1fa43ab66921 | 420 | #endif |
terencez | 0:1fa43ab66921 | 421 | case STATE_REGISTER_REQUIRED: |
terencez | 0:1fa43ab66921 | 422 | result = registration_start(contextP); |
terencez | 0:1fa43ab66921 | 423 | if (COAP_NO_ERROR != result) return result; |
terencez | 0:1fa43ab66921 | 424 | contextP->state = STATE_REGISTERING; |
terencez | 0:1fa43ab66921 | 425 | break; |
terencez | 0:1fa43ab66921 | 426 | |
terencez | 0:1fa43ab66921 | 427 | case STATE_REGISTERING: |
terencez | 0:1fa43ab66921 | 428 | { |
terencez | 0:1fa43ab66921 | 429 | switch (registration_getStatus(contextP)) |
terencez | 0:1fa43ab66921 | 430 | { |
terencez | 0:1fa43ab66921 | 431 | case STATE_REGISTERED: |
terencez | 0:1fa43ab66921 | 432 | contextP->state = STATE_READY; |
terencez | 0:1fa43ab66921 | 433 | break; |
terencez | 0:1fa43ab66921 | 434 | |
terencez | 0:1fa43ab66921 | 435 | case STATE_REG_FAILED: |
terencez | 0:1fa43ab66921 | 436 | // TODO avoid infinite loop by checking the bootstrap info is different |
terencez | 0:1fa43ab66921 | 437 | contextP->state = STATE_BOOTSTRAP_REQUIRED; |
terencez | 0:1fa43ab66921 | 438 | goto next_step; |
terencez | 0:1fa43ab66921 | 439 | break; |
terencez | 0:1fa43ab66921 | 440 | |
terencez | 0:1fa43ab66921 | 441 | case STATE_REG_PENDING: |
terencez | 0:1fa43ab66921 | 442 | default: |
terencez | 0:1fa43ab66921 | 443 | // keep on waiting |
terencez | 0:1fa43ab66921 | 444 | break; |
terencez | 0:1fa43ab66921 | 445 | } |
terencez | 0:1fa43ab66921 | 446 | } |
terencez | 0:1fa43ab66921 | 447 | break; |
terencez | 0:1fa43ab66921 | 448 | |
terencez | 0:1fa43ab66921 | 449 | case STATE_READY: |
terencez | 0:1fa43ab66921 | 450 | if (registration_getStatus(contextP) == STATE_REG_FAILED) |
terencez | 0:1fa43ab66921 | 451 | { |
terencez | 0:1fa43ab66921 | 452 | // TODO avoid infinite loop by checking the bootstrap info is different |
terencez | 0:1fa43ab66921 | 453 | contextP->state = STATE_BOOTSTRAP_REQUIRED; |
terencez | 0:1fa43ab66921 | 454 | goto next_step; |
terencez | 0:1fa43ab66921 | 455 | break; |
terencez | 0:1fa43ab66921 | 456 | } |
terencez | 0:1fa43ab66921 | 457 | break; |
terencez | 0:1fa43ab66921 | 458 | |
terencez | 0:1fa43ab66921 | 459 | default: |
terencez | 0:1fa43ab66921 | 460 | // do nothing |
terencez | 0:1fa43ab66921 | 461 | break; |
terencez | 0:1fa43ab66921 | 462 | } |
terencez | 0:1fa43ab66921 | 463 | |
terencez | 0:1fa43ab66921 | 464 | observe_step(contextP, tv_sec, timeoutP); |
terencez | 0:1fa43ab66921 | 465 | #endif |
terencez | 0:1fa43ab66921 | 466 | |
terencez | 0:1fa43ab66921 | 467 | registration_step(contextP, tv_sec, timeoutP); |
terencez | 0:1fa43ab66921 | 468 | transaction_step(contextP, tv_sec, timeoutP); |
terencez | 0:1fa43ab66921 | 469 | |
terencez | 0:1fa43ab66921 | 470 | LOG_ARG("Final timeoutP: %" PRId64, *timeoutP); |
terencez | 0:1fa43ab66921 | 471 | #ifdef LWM2M_CLIENT_MODE |
terencez | 0:1fa43ab66921 | 472 | LOG_ARG("Final state: %s", STR_STATE(contextP->state)); |
terencez | 0:1fa43ab66921 | 473 | #endif |
terencez | 0:1fa43ab66921 | 474 | return 0; |
terencez | 0:1fa43ab66921 | 475 | } |