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) 2015 Sierra Wireless 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 * Pascal Rieux - Please refer to git log
terencez 0:c2dff8cbb91a 15 * Bosch Software Innovations GmbH - Please refer to git log
terencez 0:c2dff8cbb91a 16 * David Navarro, Intel Corporation - Please refer to git log
terencez 0:c2dff8cbb91a 17 *
terencez 0:c2dff8cbb91a 18 *******************************************************************************/
terencez 0:c2dff8cbb91a 19
terencez 0:c2dff8cbb91a 20 #include "internals.h"
terencez 0:c2dff8cbb91a 21
terencez 0:c2dff8cbb91a 22 #include <stdlib.h>
terencez 0:c2dff8cbb91a 23 #include <string.h>
terencez 0:c2dff8cbb91a 24 #include <stdio.h>
terencez 0:c2dff8cbb91a 25
terencez 0:c2dff8cbb91a 26 #ifdef LWM2M_CLIENT_MODE
terencez 0:c2dff8cbb91a 27 #ifdef LWM2M_BOOTSTRAP
terencez 0:c2dff8cbb91a 28
terencez 0:c2dff8cbb91a 29 #define PRV_QUERY_BUFFER_LENGTH 200
terencez 0:c2dff8cbb91a 30
terencez 0:c2dff8cbb91a 31
terencez 0:c2dff8cbb91a 32 static void prv_handleResponse(lwm2m_server_t * bootstrapServer,
terencez 0:c2dff8cbb91a 33 coap_packet_t * message)
terencez 0:c2dff8cbb91a 34 {
terencez 0:c2dff8cbb91a 35 if (COAP_204_CHANGED == message->code)
terencez 0:c2dff8cbb91a 36 {
terencez 0:c2dff8cbb91a 37 LOG("Received ACK/2.04, Bootstrap pending, waiting for DEL/PUT from BS server...");
terencez 0:c2dff8cbb91a 38 bootstrapServer->status = STATE_BS_PENDING;
terencez 0:c2dff8cbb91a 39 }
terencez 0:c2dff8cbb91a 40 else
terencez 0:c2dff8cbb91a 41 {
terencez 0:c2dff8cbb91a 42 bootstrapServer->status = STATE_BS_FAILING;
terencez 0:c2dff8cbb91a 43 }
terencez 0:c2dff8cbb91a 44 }
terencez 0:c2dff8cbb91a 45
terencez 0:c2dff8cbb91a 46 static void prv_handleBootstrapReply(lwm2m_transaction_t * transaction,
terencez 0:c2dff8cbb91a 47 void * message)
terencez 0:c2dff8cbb91a 48 {
terencez 0:c2dff8cbb91a 49 lwm2m_server_t * bootstrapServer = (lwm2m_server_t *)transaction->userData;
terencez 0:c2dff8cbb91a 50 coap_packet_t * coapMessage = (coap_packet_t *)message;
terencez 0:c2dff8cbb91a 51
terencez 0:c2dff8cbb91a 52 LOG("Entering");
terencez 0:c2dff8cbb91a 53
terencez 0:c2dff8cbb91a 54 if (bootstrapServer->status == STATE_BS_INITIATED)
terencez 0:c2dff8cbb91a 55 {
terencez 0:c2dff8cbb91a 56 if (NULL != coapMessage && COAP_TYPE_RST != coapMessage->type)
terencez 0:c2dff8cbb91a 57 {
terencez 0:c2dff8cbb91a 58 prv_handleResponse(bootstrapServer, coapMessage);
terencez 0:c2dff8cbb91a 59 }
terencez 0:c2dff8cbb91a 60 else
terencez 0:c2dff8cbb91a 61 {
terencez 0:c2dff8cbb91a 62 bootstrapServer->status = STATE_BS_FAILING;
terencez 0:c2dff8cbb91a 63 }
terencez 0:c2dff8cbb91a 64 }
terencez 0:c2dff8cbb91a 65 }
terencez 0:c2dff8cbb91a 66
terencez 0:c2dff8cbb91a 67 // start a device initiated bootstrap
terencez 0:c2dff8cbb91a 68 static void prv_requestBootstrap(lwm2m_context_t * context,
terencez 0:c2dff8cbb91a 69 lwm2m_server_t * bootstrapServer)
terencez 0:c2dff8cbb91a 70 {
terencez 0:c2dff8cbb91a 71 char query[PRV_QUERY_BUFFER_LENGTH];
terencez 0:c2dff8cbb91a 72 int query_length = 0;
terencez 0:c2dff8cbb91a 73 int res;
terencez 0:c2dff8cbb91a 74
terencez 0:c2dff8cbb91a 75 LOG("Entering");
terencez 0:c2dff8cbb91a 76
terencez 0:c2dff8cbb91a 77 query_length = utils_stringCopy(query, PRV_QUERY_BUFFER_LENGTH, QUERY_STARTER QUERY_NAME);
terencez 0:c2dff8cbb91a 78 if (query_length < 0)
terencez 0:c2dff8cbb91a 79 {
terencez 0:c2dff8cbb91a 80 bootstrapServer->status = STATE_BS_FAILING;
terencez 0:c2dff8cbb91a 81 return;
terencez 0:c2dff8cbb91a 82 }
terencez 0:c2dff8cbb91a 83 res = utils_stringCopy(query + query_length, PRV_QUERY_BUFFER_LENGTH - query_length, context->endpointName);
terencez 0:c2dff8cbb91a 84 if (res < 0)
terencez 0:c2dff8cbb91a 85 {
terencez 0:c2dff8cbb91a 86 bootstrapServer->status = STATE_BS_FAILING;
terencez 0:c2dff8cbb91a 87 return;
terencez 0:c2dff8cbb91a 88 }
terencez 0:c2dff8cbb91a 89 query_length += res;
terencez 0:c2dff8cbb91a 90
terencez 0:c2dff8cbb91a 91 if (bootstrapServer->sessionH == NULL)
terencez 0:c2dff8cbb91a 92 {
terencez 0:c2dff8cbb91a 93 bootstrapServer->sessionH = lwm2m_connect_server(bootstrapServer->secObjInstID, context->userData);
terencez 0:c2dff8cbb91a 94 }
terencez 0:c2dff8cbb91a 95
terencez 0:c2dff8cbb91a 96 if (bootstrapServer->sessionH != NULL)
terencez 0:c2dff8cbb91a 97 {
terencez 0:c2dff8cbb91a 98 lwm2m_transaction_t * transaction = NULL;
terencez 0:c2dff8cbb91a 99
terencez 0:c2dff8cbb91a 100 LOG("Bootstrap server connection opened");
terencez 0:c2dff8cbb91a 101
terencez 0:c2dff8cbb91a 102 transaction = transaction_new(bootstrapServer->sessionH, COAP_POST, NULL, NULL, context->nextMID++, 4, NULL);
terencez 0:c2dff8cbb91a 103 if (transaction == NULL)
terencez 0:c2dff8cbb91a 104 {
terencez 0:c2dff8cbb91a 105 bootstrapServer->status = STATE_BS_FAILING;
terencez 0:c2dff8cbb91a 106 return;
terencez 0:c2dff8cbb91a 107 }
terencez 0:c2dff8cbb91a 108
terencez 0:c2dff8cbb91a 109 coap_set_header_uri_path(transaction->message, "/"URI_BOOTSTRAP_SEGMENT);
terencez 0:c2dff8cbb91a 110 coap_set_header_uri_query(transaction->message, query);
terencez 0:c2dff8cbb91a 111 transaction->callback = prv_handleBootstrapReply;
terencez 0:c2dff8cbb91a 112 transaction->userData = (void *)bootstrapServer;
terencez 0:c2dff8cbb91a 113 context->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(context->transactionList, transaction);
terencez 0:c2dff8cbb91a 114 if (transaction_send(context, transaction) == 0)
terencez 0:c2dff8cbb91a 115 {
terencez 0:c2dff8cbb91a 116 LOG("CI bootstrap requested to BS server");
terencez 0:c2dff8cbb91a 117 bootstrapServer->status = STATE_BS_INITIATED;
terencez 0:c2dff8cbb91a 118 }
terencez 0:c2dff8cbb91a 119 }
terencez 0:c2dff8cbb91a 120 else
terencez 0:c2dff8cbb91a 121 {
terencez 0:c2dff8cbb91a 122 LOG("Connecting bootstrap server failed");
terencez 0:c2dff8cbb91a 123 bootstrapServer->status = STATE_BS_FAILED;
terencez 0:c2dff8cbb91a 124 }
terencez 0:c2dff8cbb91a 125 }
terencez 0:c2dff8cbb91a 126
terencez 0:c2dff8cbb91a 127 void bootstrap_step(lwm2m_context_t * contextP,
terencez 0:c2dff8cbb91a 128 uint32_t currentTime,
terencez 0:c2dff8cbb91a 129 time_t * timeoutP)
terencez 0:c2dff8cbb91a 130 {
terencez 0:c2dff8cbb91a 131 lwm2m_server_t * targetP;
terencez 0:c2dff8cbb91a 132
terencez 0:c2dff8cbb91a 133 LOG("entering");
terencez 0:c2dff8cbb91a 134 targetP = contextP->bootstrapServerList;
terencez 0:c2dff8cbb91a 135 while (targetP != NULL)
terencez 0:c2dff8cbb91a 136 {
terencez 0:c2dff8cbb91a 137 LOG_ARG("Initial status: %s", STR_STATUS(targetP->status));
terencez 0:c2dff8cbb91a 138 switch (targetP->status)
terencez 0:c2dff8cbb91a 139 {
terencez 0:c2dff8cbb91a 140 case STATE_DEREGISTERED:
terencez 0:c2dff8cbb91a 141 targetP->registration = currentTime + targetP->lifetime;
terencez 0:c2dff8cbb91a 142 targetP->status = STATE_BS_HOLD_OFF;
terencez 0:c2dff8cbb91a 143 if (*timeoutP > targetP->lifetime)
terencez 0:c2dff8cbb91a 144 {
terencez 0:c2dff8cbb91a 145 *timeoutP = targetP->lifetime;
terencez 0:c2dff8cbb91a 146 }
terencez 0:c2dff8cbb91a 147 break;
terencez 0:c2dff8cbb91a 148
terencez 0:c2dff8cbb91a 149 case STATE_BS_HOLD_OFF:
terencez 0:c2dff8cbb91a 150 if (targetP->registration <= currentTime)
terencez 0:c2dff8cbb91a 151 {
terencez 0:c2dff8cbb91a 152 prv_requestBootstrap(contextP, targetP);
terencez 0:c2dff8cbb91a 153 }
terencez 0:c2dff8cbb91a 154 else if (*timeoutP > targetP->registration - currentTime)
terencez 0:c2dff8cbb91a 155 {
terencez 0:c2dff8cbb91a 156 *timeoutP = targetP->registration - currentTime;
terencez 0:c2dff8cbb91a 157 }
terencez 0:c2dff8cbb91a 158 break;
terencez 0:c2dff8cbb91a 159
terencez 0:c2dff8cbb91a 160 case STATE_BS_INITIATED:
terencez 0:c2dff8cbb91a 161 // waiting
terencez 0:c2dff8cbb91a 162 break;
terencez 0:c2dff8cbb91a 163
terencez 0:c2dff8cbb91a 164 case STATE_BS_PENDING:
terencez 0:c2dff8cbb91a 165 // waiting
terencez 0:c2dff8cbb91a 166 break;
terencez 0:c2dff8cbb91a 167
terencez 0:c2dff8cbb91a 168 case STATE_BS_FINISHING:
terencez 0:c2dff8cbb91a 169 if (targetP->sessionH != NULL)
terencez 0:c2dff8cbb91a 170 {
terencez 0:c2dff8cbb91a 171 lwm2m_close_connection(targetP->sessionH, contextP->userData);
terencez 0:c2dff8cbb91a 172 targetP->sessionH = NULL;
terencez 0:c2dff8cbb91a 173 }
terencez 0:c2dff8cbb91a 174 targetP->status = STATE_BS_FINISHED;
terencez 0:c2dff8cbb91a 175 *timeoutP = 0;
terencez 0:c2dff8cbb91a 176 break;
terencez 0:c2dff8cbb91a 177
terencez 0:c2dff8cbb91a 178 case STATE_BS_FAILING:
terencez 0:c2dff8cbb91a 179 if (targetP->sessionH != NULL)
terencez 0:c2dff8cbb91a 180 {
terencez 0:c2dff8cbb91a 181 lwm2m_close_connection(targetP->sessionH, contextP->userData);
terencez 0:c2dff8cbb91a 182 targetP->sessionH = NULL;
terencez 0:c2dff8cbb91a 183 }
terencez 0:c2dff8cbb91a 184 targetP->status = STATE_BS_FAILED;
terencez 0:c2dff8cbb91a 185 *timeoutP = 0;
terencez 0:c2dff8cbb91a 186 break;
terencez 0:c2dff8cbb91a 187
terencez 0:c2dff8cbb91a 188 default:
terencez 0:c2dff8cbb91a 189 break;
terencez 0:c2dff8cbb91a 190 }
terencez 0:c2dff8cbb91a 191 LOG_ARG("Finalal status: %s", STR_STATUS(targetP->status));
terencez 0:c2dff8cbb91a 192 targetP = targetP->next;
terencez 0:c2dff8cbb91a 193 }
terencez 0:c2dff8cbb91a 194 }
terencez 0:c2dff8cbb91a 195
terencez 0:c2dff8cbb91a 196 coap_status_t bootstrap_handleFinish(lwm2m_context_t * context,
terencez 0:c2dff8cbb91a 197 void * fromSessionH)
terencez 0:c2dff8cbb91a 198 {
terencez 0:c2dff8cbb91a 199 lwm2m_server_t * bootstrapServer;
terencez 0:c2dff8cbb91a 200
terencez 0:c2dff8cbb91a 201 LOG("Entering");
terencez 0:c2dff8cbb91a 202 bootstrapServer = utils_findBootstrapServer(context, fromSessionH);
terencez 0:c2dff8cbb91a 203 if (bootstrapServer != NULL
terencez 0:c2dff8cbb91a 204 && bootstrapServer->status == STATE_BS_PENDING)
terencez 0:c2dff8cbb91a 205 {
terencez 0:c2dff8cbb91a 206 LOG("Bootstrap server status changed to STATE_BS_FINISHING");
terencez 0:c2dff8cbb91a 207 bootstrapServer->status = STATE_BS_FINISHING;
terencez 0:c2dff8cbb91a 208 return COAP_204_CHANGED;
terencez 0:c2dff8cbb91a 209 }
terencez 0:c2dff8cbb91a 210
terencez 0:c2dff8cbb91a 211 return COAP_IGNORE;
terencez 0:c2dff8cbb91a 212 }
terencez 0:c2dff8cbb91a 213
terencez 0:c2dff8cbb91a 214 /*
terencez 0:c2dff8cbb91a 215 * Reset the bootstrap servers statuses
terencez 0:c2dff8cbb91a 216 *
terencez 0:c2dff8cbb91a 217 * TODO: handle LWM2M Servers the client is registered to ?
terencez 0:c2dff8cbb91a 218 *
terencez 0:c2dff8cbb91a 219 */
terencez 0:c2dff8cbb91a 220 void bootstrap_start(lwm2m_context_t * contextP)
terencez 0:c2dff8cbb91a 221 {
terencez 0:c2dff8cbb91a 222 lwm2m_server_t * targetP;
terencez 0:c2dff8cbb91a 223
terencez 0:c2dff8cbb91a 224 LOG("Entering");
terencez 0:c2dff8cbb91a 225 targetP = contextP->bootstrapServerList;
terencez 0:c2dff8cbb91a 226 while (targetP != NULL)
terencez 0:c2dff8cbb91a 227 {
terencez 0:c2dff8cbb91a 228 targetP->status = STATE_DEREGISTERED;
terencez 0:c2dff8cbb91a 229 if (targetP->sessionH == NULL)
terencez 0:c2dff8cbb91a 230 {
terencez 0:c2dff8cbb91a 231 targetP->sessionH = lwm2m_connect_server(targetP->secObjInstID, contextP->userData);
terencez 0:c2dff8cbb91a 232 }
terencez 0:c2dff8cbb91a 233 targetP = targetP->next;
terencez 0:c2dff8cbb91a 234 }
terencez 0:c2dff8cbb91a 235 }
terencez 0:c2dff8cbb91a 236
terencez 0:c2dff8cbb91a 237 /*
terencez 0:c2dff8cbb91a 238 * Returns STATE_BS_PENDING if at least one bootstrap is still pending
terencez 0:c2dff8cbb91a 239 * Returns STATE_BS_FINISHED if at least one bootstrap succeeded and no bootstrap is pending
terencez 0:c2dff8cbb91a 240 * Returns STATE_BS_FAILED if all bootstrap failed.
terencez 0:c2dff8cbb91a 241 */
terencez 0:c2dff8cbb91a 242 lwm2m_status_t bootstrap_getStatus(lwm2m_context_t * contextP)
terencez 0:c2dff8cbb91a 243 {
terencez 0:c2dff8cbb91a 244 lwm2m_server_t * targetP;
terencez 0:c2dff8cbb91a 245 lwm2m_status_t bs_status;
terencez 0:c2dff8cbb91a 246
terencez 0:c2dff8cbb91a 247 LOG("Entering");
terencez 0:c2dff8cbb91a 248 targetP = contextP->bootstrapServerList;
terencez 0:c2dff8cbb91a 249 bs_status = STATE_BS_FAILED;
terencez 0:c2dff8cbb91a 250
terencez 0:c2dff8cbb91a 251 while (targetP != NULL)
terencez 0:c2dff8cbb91a 252 {
terencez 0:c2dff8cbb91a 253 switch (targetP->status)
terencez 0:c2dff8cbb91a 254 {
terencez 0:c2dff8cbb91a 255 case STATE_BS_FINISHED:
terencez 0:c2dff8cbb91a 256 if (bs_status == STATE_BS_FAILED)
terencez 0:c2dff8cbb91a 257 {
terencez 0:c2dff8cbb91a 258 bs_status = STATE_BS_FINISHED;
terencez 0:c2dff8cbb91a 259 }
terencez 0:c2dff8cbb91a 260 break;
terencez 0:c2dff8cbb91a 261
terencez 0:c2dff8cbb91a 262 case STATE_BS_HOLD_OFF:
terencez 0:c2dff8cbb91a 263 case STATE_BS_INITIATED:
terencez 0:c2dff8cbb91a 264 case STATE_BS_PENDING:
terencez 0:c2dff8cbb91a 265 case STATE_BS_FINISHING:
terencez 0:c2dff8cbb91a 266 bs_status = STATE_BS_PENDING;
terencez 0:c2dff8cbb91a 267 break;
terencez 0:c2dff8cbb91a 268
terencez 0:c2dff8cbb91a 269 default:
terencez 0:c2dff8cbb91a 270 break;
terencez 0:c2dff8cbb91a 271 }
terencez 0:c2dff8cbb91a 272 targetP = targetP->next;
terencez 0:c2dff8cbb91a 273 }
terencez 0:c2dff8cbb91a 274
terencez 0:c2dff8cbb91a 275 LOG_ARG("Returned status: %s", STR_STATUS(bs_status));
terencez 0:c2dff8cbb91a 276
terencez 0:c2dff8cbb91a 277 return bs_status;
terencez 0:c2dff8cbb91a 278 }
terencez 0:c2dff8cbb91a 279
terencez 0:c2dff8cbb91a 280 static coap_status_t prv_checkServerStatus(lwm2m_server_t * serverP)
terencez 0:c2dff8cbb91a 281 {
terencez 0:c2dff8cbb91a 282 LOG_ARG("Initial status: %s", STR_STATUS(serverP->status));
terencez 0:c2dff8cbb91a 283
terencez 0:c2dff8cbb91a 284 switch (serverP->status)
terencez 0:c2dff8cbb91a 285 {
terencez 0:c2dff8cbb91a 286 case STATE_BS_HOLD_OFF:
terencez 0:c2dff8cbb91a 287 serverP->status = STATE_BS_PENDING;
terencez 0:c2dff8cbb91a 288 LOG_ARG("Status changed to: %s", STR_STATUS(serverP->status));
terencez 0:c2dff8cbb91a 289 break;
terencez 0:c2dff8cbb91a 290
terencez 0:c2dff8cbb91a 291 case STATE_BS_INITIATED:
terencez 0:c2dff8cbb91a 292 // The ACK was probably lost
terencez 0:c2dff8cbb91a 293 serverP->status = STATE_BS_PENDING;
terencez 0:c2dff8cbb91a 294 LOG_ARG("Status changed to: %s", STR_STATUS(serverP->status));
terencez 0:c2dff8cbb91a 295 break;
terencez 0:c2dff8cbb91a 296
terencez 0:c2dff8cbb91a 297 case STATE_DEREGISTERED:
terencez 0:c2dff8cbb91a 298 // server initiated bootstrap
terencez 0:c2dff8cbb91a 299 case STATE_BS_PENDING:
terencez 0:c2dff8cbb91a 300 // do nothing
terencez 0:c2dff8cbb91a 301 break;
terencez 0:c2dff8cbb91a 302
terencez 0:c2dff8cbb91a 303 case STATE_BS_FINISHED:
terencez 0:c2dff8cbb91a 304 case STATE_BS_FINISHING:
terencez 0:c2dff8cbb91a 305 case STATE_BS_FAILING:
terencez 0:c2dff8cbb91a 306 case STATE_BS_FAILED:
terencez 0:c2dff8cbb91a 307 default:
terencez 0:c2dff8cbb91a 308 LOG("Returning COAP_IGNORE");
terencez 0:c2dff8cbb91a 309 return COAP_IGNORE;
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 static void prv_tagServer(lwm2m_context_t * contextP,
terencez 0:c2dff8cbb91a 316 uint16_t id)
terencez 0:c2dff8cbb91a 317 {
terencez 0:c2dff8cbb91a 318 lwm2m_server_t * targetP;
terencez 0:c2dff8cbb91a 319
terencez 0:c2dff8cbb91a 320 targetP = (lwm2m_server_t *)LWM2M_LIST_FIND(contextP->bootstrapServerList, id);
terencez 0:c2dff8cbb91a 321 if (targetP == NULL)
terencez 0:c2dff8cbb91a 322 {
terencez 0:c2dff8cbb91a 323 targetP = (lwm2m_server_t *)LWM2M_LIST_FIND(contextP->serverList, id);
terencez 0:c2dff8cbb91a 324 }
terencez 0:c2dff8cbb91a 325 if (targetP != NULL)
terencez 0:c2dff8cbb91a 326 {
terencez 0:c2dff8cbb91a 327 targetP->dirty = true;
terencez 0:c2dff8cbb91a 328 }
terencez 0:c2dff8cbb91a 329 }
terencez 0:c2dff8cbb91a 330
terencez 0:c2dff8cbb91a 331 static void prv_tagAllServer(lwm2m_context_t * contextP,
terencez 0:c2dff8cbb91a 332 lwm2m_server_t * serverP)
terencez 0:c2dff8cbb91a 333 {
terencez 0:c2dff8cbb91a 334 lwm2m_server_t * targetP;
terencez 0:c2dff8cbb91a 335
terencez 0:c2dff8cbb91a 336 targetP = contextP->bootstrapServerList;
terencez 0:c2dff8cbb91a 337 while (targetP != NULL)
terencez 0:c2dff8cbb91a 338 {
terencez 0:c2dff8cbb91a 339 if (targetP != serverP)
terencez 0:c2dff8cbb91a 340 {
terencez 0:c2dff8cbb91a 341 targetP->dirty = true;
terencez 0:c2dff8cbb91a 342 }
terencez 0:c2dff8cbb91a 343 targetP = targetP->next;
terencez 0:c2dff8cbb91a 344 }
terencez 0:c2dff8cbb91a 345 targetP = contextP->serverList;
terencez 0:c2dff8cbb91a 346 while (targetP != NULL)
terencez 0:c2dff8cbb91a 347 {
terencez 0:c2dff8cbb91a 348 targetP->dirty = true;
terencez 0:c2dff8cbb91a 349 targetP = targetP->next;
terencez 0:c2dff8cbb91a 350 }
terencez 0:c2dff8cbb91a 351 }
terencez 0:c2dff8cbb91a 352
terencez 0:c2dff8cbb91a 353 coap_status_t bootstrap_handleCommand(lwm2m_context_t * contextP,
terencez 0:c2dff8cbb91a 354 lwm2m_uri_t * uriP,
terencez 0:c2dff8cbb91a 355 lwm2m_server_t * serverP,
terencez 0:c2dff8cbb91a 356 coap_packet_t * message,
terencez 0:c2dff8cbb91a 357 coap_packet_t * response)
terencez 0:c2dff8cbb91a 358 {
terencez 0:c2dff8cbb91a 359 coap_status_t result;
terencez 0:c2dff8cbb91a 360 lwm2m_media_type_t format;
terencez 0:c2dff8cbb91a 361
terencez 0:c2dff8cbb91a 362 LOG_ARG("Code: %02X", message->code);
terencez 0:c2dff8cbb91a 363 LOG_URI(uriP);
terencez 0:c2dff8cbb91a 364 format = utils_convertMediaType(message->content_type);
terencez 0:c2dff8cbb91a 365
terencez 0:c2dff8cbb91a 366 result = prv_checkServerStatus(serverP);
terencez 0:c2dff8cbb91a 367 if (result != COAP_NO_ERROR) return result;
terencez 0:c2dff8cbb91a 368
terencez 0:c2dff8cbb91a 369 switch (message->code)
terencez 0:c2dff8cbb91a 370 {
terencez 0:c2dff8cbb91a 371 case COAP_PUT:
terencez 0:c2dff8cbb91a 372 {
terencez 0:c2dff8cbb91a 373 if (LWM2M_URI_IS_SET_INSTANCE(uriP))
terencez 0:c2dff8cbb91a 374 {
terencez 0:c2dff8cbb91a 375 if (object_isInstanceNew(contextP, uriP->objectId, uriP->instanceId))
terencez 0:c2dff8cbb91a 376 {
terencez 0:c2dff8cbb91a 377 result = object_create(contextP, uriP, format, message->payload, message->payload_len);
terencez 0:c2dff8cbb91a 378 if (COAP_201_CREATED == result)
terencez 0:c2dff8cbb91a 379 {
terencez 0:c2dff8cbb91a 380 result = COAP_204_CHANGED;
terencez 0:c2dff8cbb91a 381 }
terencez 0:c2dff8cbb91a 382 }
terencez 0:c2dff8cbb91a 383 else
terencez 0:c2dff8cbb91a 384 {
terencez 0:c2dff8cbb91a 385 result = object_write(contextP, uriP, format, message->payload, message->payload_len);
terencez 0:c2dff8cbb91a 386 if (uriP->objectId == LWM2M_SECURITY_OBJECT_ID
terencez 0:c2dff8cbb91a 387 && result == COAP_204_CHANGED)
terencez 0:c2dff8cbb91a 388 {
terencez 0:c2dff8cbb91a 389 prv_tagServer(contextP, uriP->instanceId);
terencez 0:c2dff8cbb91a 390 }
terencez 0:c2dff8cbb91a 391 }
terencez 0:c2dff8cbb91a 392 }
terencez 0:c2dff8cbb91a 393 else
terencez 0:c2dff8cbb91a 394 {
terencez 0:c2dff8cbb91a 395 lwm2m_data_t * dataP = NULL;
terencez 0:c2dff8cbb91a 396 int size = 0;
terencez 0:c2dff8cbb91a 397 int i;
terencez 0:c2dff8cbb91a 398
terencez 0:c2dff8cbb91a 399 if (message->payload_len == 0 || message->payload == 0)
terencez 0:c2dff8cbb91a 400 {
terencez 0:c2dff8cbb91a 401 result = COAP_400_BAD_REQUEST;
terencez 0:c2dff8cbb91a 402 }
terencez 0:c2dff8cbb91a 403 else
terencez 0:c2dff8cbb91a 404 {
terencez 0:c2dff8cbb91a 405 size = lwm2m_data_parse(uriP, message->payload, message->payload_len, format, &dataP);
terencez 0:c2dff8cbb91a 406 if (size == 0)
terencez 0:c2dff8cbb91a 407 {
terencez 0:c2dff8cbb91a 408 result = COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:c2dff8cbb91a 409 break;
terencez 0:c2dff8cbb91a 410 }
terencez 0:c2dff8cbb91a 411
terencez 0:c2dff8cbb91a 412 for (i = 0 ; i < size ; i++)
terencez 0:c2dff8cbb91a 413 {
terencez 0:c2dff8cbb91a 414 if(dataP[i].type == LWM2M_TYPE_OBJECT_INSTANCE)
terencez 0:c2dff8cbb91a 415 {
terencez 0:c2dff8cbb91a 416 if (object_isInstanceNew(contextP, uriP->objectId, dataP[i].id))
terencez 0:c2dff8cbb91a 417 {
terencez 0:c2dff8cbb91a 418 result = object_createInstance(contextP, uriP, &dataP[i]);
terencez 0:c2dff8cbb91a 419 if (COAP_201_CREATED == result)
terencez 0:c2dff8cbb91a 420 {
terencez 0:c2dff8cbb91a 421 result = COAP_204_CHANGED;
terencez 0:c2dff8cbb91a 422 }
terencez 0:c2dff8cbb91a 423 }
terencez 0:c2dff8cbb91a 424 else
terencez 0:c2dff8cbb91a 425 {
terencez 0:c2dff8cbb91a 426 result = object_writeInstance(contextP, uriP, &dataP[i]);
terencez 0:c2dff8cbb91a 427 if (uriP->objectId == LWM2M_SECURITY_OBJECT_ID
terencez 0:c2dff8cbb91a 428 && result == COAP_204_CHANGED)
terencez 0:c2dff8cbb91a 429 {
terencez 0:c2dff8cbb91a 430 prv_tagServer(contextP, dataP[i].id);
terencez 0:c2dff8cbb91a 431 }
terencez 0:c2dff8cbb91a 432 }
terencez 0:c2dff8cbb91a 433
terencez 0:c2dff8cbb91a 434 if(result != COAP_204_CHANGED) // Stop object create or write when result is error
terencez 0:c2dff8cbb91a 435 {
terencez 0:c2dff8cbb91a 436 break;
terencez 0:c2dff8cbb91a 437 }
terencez 0:c2dff8cbb91a 438 }
terencez 0:c2dff8cbb91a 439 else
terencez 0:c2dff8cbb91a 440 {
terencez 0:c2dff8cbb91a 441 result = COAP_400_BAD_REQUEST;
terencez 0:c2dff8cbb91a 442 }
terencez 0:c2dff8cbb91a 443 }
terencez 0:c2dff8cbb91a 444 lwm2m_data_free(size, dataP);
terencez 0:c2dff8cbb91a 445 }
terencez 0:c2dff8cbb91a 446 }
terencez 0:c2dff8cbb91a 447 }
terencez 0:c2dff8cbb91a 448 break;
terencez 0:c2dff8cbb91a 449
terencez 0:c2dff8cbb91a 450 case COAP_DELETE:
terencez 0:c2dff8cbb91a 451 {
terencez 0:c2dff8cbb91a 452 if (LWM2M_URI_IS_SET_RESOURCE(uriP))
terencez 0:c2dff8cbb91a 453 {
terencez 0:c2dff8cbb91a 454 result = COAP_400_BAD_REQUEST;
terencez 0:c2dff8cbb91a 455 }
terencez 0:c2dff8cbb91a 456 else
terencez 0:c2dff8cbb91a 457 {
terencez 0:c2dff8cbb91a 458 result = object_delete(contextP, uriP);
terencez 0:c2dff8cbb91a 459 if (uriP->objectId == LWM2M_SECURITY_OBJECT_ID
terencez 0:c2dff8cbb91a 460 && result == COAP_202_DELETED)
terencez 0:c2dff8cbb91a 461 {
terencez 0:c2dff8cbb91a 462 if (LWM2M_URI_IS_SET_INSTANCE(uriP))
terencez 0:c2dff8cbb91a 463 {
terencez 0:c2dff8cbb91a 464 prv_tagServer(contextP, uriP->instanceId);
terencez 0:c2dff8cbb91a 465 }
terencez 0:c2dff8cbb91a 466 else
terencez 0:c2dff8cbb91a 467 {
terencez 0:c2dff8cbb91a 468 prv_tagAllServer(contextP, NULL);
terencez 0:c2dff8cbb91a 469 }
terencez 0:c2dff8cbb91a 470 }
terencez 0:c2dff8cbb91a 471 }
terencez 0:c2dff8cbb91a 472 }
terencez 0:c2dff8cbb91a 473 break;
terencez 0:c2dff8cbb91a 474
terencez 0:c2dff8cbb91a 475 case COAP_GET:
terencez 0:c2dff8cbb91a 476 case COAP_POST:
terencez 0:c2dff8cbb91a 477 default:
terencez 0:c2dff8cbb91a 478 result = COAP_400_BAD_REQUEST;
terencez 0:c2dff8cbb91a 479 break;
terencez 0:c2dff8cbb91a 480 }
terencez 0:c2dff8cbb91a 481
terencez 0:c2dff8cbb91a 482 if (result == COAP_202_DELETED
terencez 0:c2dff8cbb91a 483 || result == COAP_204_CHANGED)
terencez 0:c2dff8cbb91a 484 {
terencez 0:c2dff8cbb91a 485 if (serverP->status != STATE_BS_PENDING)
terencez 0:c2dff8cbb91a 486 {
terencez 0:c2dff8cbb91a 487 serverP->status = STATE_BS_PENDING;
terencez 0:c2dff8cbb91a 488 contextP->state = STATE_BOOTSTRAPPING;
terencez 0:c2dff8cbb91a 489 }
terencez 0:c2dff8cbb91a 490 }
terencez 0:c2dff8cbb91a 491 LOG_ARG("Server status: %s", STR_STATUS(serverP->status));
terencez 0:c2dff8cbb91a 492
terencez 0:c2dff8cbb91a 493 return result;
terencez 0:c2dff8cbb91a 494 }
terencez 0:c2dff8cbb91a 495
terencez 0:c2dff8cbb91a 496 coap_status_t bootstrap_handleDeleteAll(lwm2m_context_t * contextP,
terencez 0:c2dff8cbb91a 497 void * fromSessionH)
terencez 0:c2dff8cbb91a 498 {
terencez 0:c2dff8cbb91a 499 lwm2m_server_t * serverP;
terencez 0:c2dff8cbb91a 500 coap_status_t result;
terencez 0:c2dff8cbb91a 501 lwm2m_object_t * objectP;
terencez 0:c2dff8cbb91a 502
terencez 0:c2dff8cbb91a 503 LOG("Entering");
terencez 0:c2dff8cbb91a 504 serverP = utils_findBootstrapServer(contextP, fromSessionH);
terencez 0:c2dff8cbb91a 505 if (serverP == NULL) return COAP_IGNORE;
terencez 0:c2dff8cbb91a 506 result = prv_checkServerStatus(serverP);
terencez 0:c2dff8cbb91a 507 if (result != COAP_NO_ERROR) return result;
terencez 0:c2dff8cbb91a 508
terencez 0:c2dff8cbb91a 509 result = COAP_202_DELETED;
terencez 0:c2dff8cbb91a 510 for (objectP = contextP->objectList; objectP != NULL; objectP = objectP->next)
terencez 0:c2dff8cbb91a 511 {
terencez 0:c2dff8cbb91a 512 lwm2m_uri_t uri;
terencez 0:c2dff8cbb91a 513
terencez 0:c2dff8cbb91a 514 memset(&uri, 0, sizeof(lwm2m_uri_t));
terencez 0:c2dff8cbb91a 515 uri.flag = LWM2M_URI_FLAG_OBJECT_ID;
terencez 0:c2dff8cbb91a 516 uri.objectId = objectP->objID;
terencez 0:c2dff8cbb91a 517
terencez 0:c2dff8cbb91a 518 if (objectP->objID == LWM2M_SECURITY_OBJECT_ID)
terencez 0:c2dff8cbb91a 519 {
terencez 0:c2dff8cbb91a 520 lwm2m_list_t * instanceP;
terencez 0:c2dff8cbb91a 521
terencez 0:c2dff8cbb91a 522 instanceP = objectP->instanceList;
terencez 0:c2dff8cbb91a 523 while (NULL != instanceP
terencez 0:c2dff8cbb91a 524 && result == COAP_202_DELETED)
terencez 0:c2dff8cbb91a 525 {
terencez 0:c2dff8cbb91a 526 if (instanceP->id == serverP->secObjInstID)
terencez 0:c2dff8cbb91a 527 {
terencez 0:c2dff8cbb91a 528 instanceP = instanceP->next;
terencez 0:c2dff8cbb91a 529 }
terencez 0:c2dff8cbb91a 530 else
terencez 0:c2dff8cbb91a 531 {
terencez 0:c2dff8cbb91a 532 uri.flag = LWM2M_URI_FLAG_OBJECT_ID | LWM2M_URI_FLAG_INSTANCE_ID;
terencez 0:c2dff8cbb91a 533 uri.instanceId = instanceP->id;
terencez 0:c2dff8cbb91a 534 result = object_delete(contextP, &uri);
terencez 0:c2dff8cbb91a 535 instanceP = objectP->instanceList;
terencez 0:c2dff8cbb91a 536 }
terencez 0:c2dff8cbb91a 537 }
terencez 0:c2dff8cbb91a 538 if (result == COAP_202_DELETED)
terencez 0:c2dff8cbb91a 539 {
terencez 0:c2dff8cbb91a 540 prv_tagAllServer(contextP, serverP);
terencez 0:c2dff8cbb91a 541 }
terencez 0:c2dff8cbb91a 542 }
terencez 0:c2dff8cbb91a 543 else
terencez 0:c2dff8cbb91a 544 {
terencez 0:c2dff8cbb91a 545 result = object_delete(contextP, &uri);
terencez 0:c2dff8cbb91a 546 if (result == COAP_405_METHOD_NOT_ALLOWED)
terencez 0:c2dff8cbb91a 547 {
terencez 0:c2dff8cbb91a 548 // Fake a successful deletion for static objects like the Device object.
terencez 0:c2dff8cbb91a 549 result = COAP_202_DELETED;
terencez 0:c2dff8cbb91a 550 }
terencez 0:c2dff8cbb91a 551 }
terencez 0:c2dff8cbb91a 552 }
terencez 0:c2dff8cbb91a 553
terencez 0:c2dff8cbb91a 554 return result;
terencez 0:c2dff8cbb91a 555 }
terencez 0:c2dff8cbb91a 556 #endif
terencez 0:c2dff8cbb91a 557 #endif
terencez 0:c2dff8cbb91a 558
terencez 0:c2dff8cbb91a 559 #ifdef LWM2M_BOOTSTRAP_SERVER_MODE
terencez 0:c2dff8cbb91a 560 uint8_t bootstrap_handleRequest(lwm2m_context_t * contextP,
terencez 0:c2dff8cbb91a 561 lwm2m_uri_t * uriP,
terencez 0:c2dff8cbb91a 562 void * fromSessionH,
terencez 0:c2dff8cbb91a 563 coap_packet_t * message,
terencez 0:c2dff8cbb91a 564 coap_packet_t * response)
terencez 0:c2dff8cbb91a 565 {
terencez 0:c2dff8cbb91a 566 uint8_t result;
terencez 0:c2dff8cbb91a 567 char * name;
terencez 0:c2dff8cbb91a 568
terencez 0:c2dff8cbb91a 569 LOG_URI(uriP);
terencez 0:c2dff8cbb91a 570 if (contextP->bootstrapCallback == NULL) return COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:c2dff8cbb91a 571 if (message->code != COAP_POST) return COAP_400_BAD_REQUEST;
terencez 0:c2dff8cbb91a 572 if (message->uri_query == NULL) return COAP_400_BAD_REQUEST;
terencez 0:c2dff8cbb91a 573 if (message->payload != NULL) return COAP_400_BAD_REQUEST;
terencez 0:c2dff8cbb91a 574
terencez 0:c2dff8cbb91a 575 if (lwm2m_strncmp((char *)message->uri_query->data, QUERY_NAME, QUERY_NAME_LEN) != 0)
terencez 0:c2dff8cbb91a 576 {
terencez 0:c2dff8cbb91a 577 return COAP_400_BAD_REQUEST;
terencez 0:c2dff8cbb91a 578 }
terencez 0:c2dff8cbb91a 579
terencez 0:c2dff8cbb91a 580 if (message->uri_query->len == QUERY_NAME_LEN) return COAP_400_BAD_REQUEST;
terencez 0:c2dff8cbb91a 581 if (message->uri_query->next != NULL) return COAP_400_BAD_REQUEST;
terencez 0:c2dff8cbb91a 582
terencez 0:c2dff8cbb91a 583 name = (char *)lwm2m_malloc(message->uri_query->len - QUERY_NAME_LEN + 1);
terencez 0:c2dff8cbb91a 584 if (name == NULL) return COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:c2dff8cbb91a 585
terencez 0:c2dff8cbb91a 586 memcpy(name, message->uri_query->data + QUERY_NAME_LEN, message->uri_query->len - QUERY_NAME_LEN);
terencez 0:c2dff8cbb91a 587 name[message->uri_query->len - QUERY_NAME_LEN] = 0;
terencez 0:c2dff8cbb91a 588
terencez 0:c2dff8cbb91a 589 result = contextP->bootstrapCallback(fromSessionH, COAP_NO_ERROR, NULL, name, contextP->bootstrapUserData);
terencez 0:c2dff8cbb91a 590
terencez 0:c2dff8cbb91a 591 lwm2m_free(name);
terencez 0:c2dff8cbb91a 592
terencez 0:c2dff8cbb91a 593 return result;
terencez 0:c2dff8cbb91a 594 }
terencez 0:c2dff8cbb91a 595
terencez 0:c2dff8cbb91a 596 void lwm2m_set_bootstrap_callback(lwm2m_context_t * contextP,
terencez 0:c2dff8cbb91a 597 lwm2m_bootstrap_callback_t callback,
terencez 0:c2dff8cbb91a 598 void * userData)
terencez 0:c2dff8cbb91a 599 {
terencez 0:c2dff8cbb91a 600 LOG("Entering");
terencez 0:c2dff8cbb91a 601 contextP->bootstrapCallback = callback;
terencez 0:c2dff8cbb91a 602 contextP->bootstrapUserData = userData;
terencez 0:c2dff8cbb91a 603 }
terencez 0:c2dff8cbb91a 604
terencez 0:c2dff8cbb91a 605 static void prv_resultCallback(lwm2m_transaction_t * transacP,
terencez 0:c2dff8cbb91a 606 void * message)
terencez 0:c2dff8cbb91a 607 {
terencez 0:c2dff8cbb91a 608 bs_data_t * dataP = (bs_data_t *)transacP->userData;
terencez 0:c2dff8cbb91a 609 lwm2m_uri_t * uriP;
terencez 0:c2dff8cbb91a 610
terencez 0:c2dff8cbb91a 611 if (dataP->isUri == true)
terencez 0:c2dff8cbb91a 612 {
terencez 0:c2dff8cbb91a 613 uriP = &dataP->uri;
terencez 0:c2dff8cbb91a 614 }
terencez 0:c2dff8cbb91a 615 else
terencez 0:c2dff8cbb91a 616 {
terencez 0:c2dff8cbb91a 617 uriP = NULL;
terencez 0:c2dff8cbb91a 618 }
terencez 0:c2dff8cbb91a 619
terencez 0:c2dff8cbb91a 620 if (message == NULL)
terencez 0:c2dff8cbb91a 621 {
terencez 0:c2dff8cbb91a 622 dataP->callback(transacP->peerH,
terencez 0:c2dff8cbb91a 623 COAP_503_SERVICE_UNAVAILABLE,
terencez 0:c2dff8cbb91a 624 uriP,
terencez 0:c2dff8cbb91a 625 NULL,
terencez 0:c2dff8cbb91a 626 dataP->userData);
terencez 0:c2dff8cbb91a 627 }
terencez 0:c2dff8cbb91a 628 else
terencez 0:c2dff8cbb91a 629 {
terencez 0:c2dff8cbb91a 630 coap_packet_t * packet = (coap_packet_t *)message;
terencez 0:c2dff8cbb91a 631
terencez 0:c2dff8cbb91a 632 dataP->callback(transacP->peerH,
terencez 0:c2dff8cbb91a 633 packet->code,
terencez 0:c2dff8cbb91a 634 uriP,
terencez 0:c2dff8cbb91a 635 NULL,
terencez 0:c2dff8cbb91a 636 dataP->userData);
terencez 0:c2dff8cbb91a 637 }
terencez 0:c2dff8cbb91a 638 lwm2m_free(dataP);
terencez 0:c2dff8cbb91a 639 }
terencez 0:c2dff8cbb91a 640
terencez 0:c2dff8cbb91a 641 int lwm2m_bootstrap_delete(lwm2m_context_t * contextP,
terencez 0:c2dff8cbb91a 642 void * sessionH,
terencez 0:c2dff8cbb91a 643 lwm2m_uri_t * uriP)
terencez 0:c2dff8cbb91a 644 {
terencez 0:c2dff8cbb91a 645 lwm2m_transaction_t * transaction;
terencez 0:c2dff8cbb91a 646 bs_data_t * dataP;
terencez 0:c2dff8cbb91a 647
terencez 0:c2dff8cbb91a 648 LOG_URI(uriP);
terencez 0:c2dff8cbb91a 649 transaction = transaction_new(sessionH, COAP_DELETE, NULL, uriP, contextP->nextMID++, 4, NULL);
terencez 0:c2dff8cbb91a 650 if (transaction == NULL) return COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:c2dff8cbb91a 651
terencez 0:c2dff8cbb91a 652 dataP = (bs_data_t *)lwm2m_malloc(sizeof(bs_data_t));
terencez 0:c2dff8cbb91a 653 if (dataP == NULL)
terencez 0:c2dff8cbb91a 654 {
terencez 0:c2dff8cbb91a 655 transaction_free(transaction);
terencez 0:c2dff8cbb91a 656 return COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:c2dff8cbb91a 657 }
terencez 0:c2dff8cbb91a 658 if (uriP == NULL)
terencez 0:c2dff8cbb91a 659 {
terencez 0:c2dff8cbb91a 660 dataP->isUri = false;
terencez 0:c2dff8cbb91a 661 }
terencez 0:c2dff8cbb91a 662 else
terencez 0:c2dff8cbb91a 663 {
terencez 0:c2dff8cbb91a 664 dataP->isUri = true;
terencez 0:c2dff8cbb91a 665 memcpy(&dataP->uri, uriP, sizeof(lwm2m_uri_t));
terencez 0:c2dff8cbb91a 666 }
terencez 0:c2dff8cbb91a 667 dataP->callback = contextP->bootstrapCallback;
terencez 0:c2dff8cbb91a 668 dataP->userData = contextP->bootstrapUserData;
terencez 0:c2dff8cbb91a 669
terencez 0:c2dff8cbb91a 670 transaction->callback = prv_resultCallback;
terencez 0:c2dff8cbb91a 671 transaction->userData = (void *)dataP;
terencez 0:c2dff8cbb91a 672
terencez 0:c2dff8cbb91a 673 contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction);
terencez 0:c2dff8cbb91a 674
terencez 0:c2dff8cbb91a 675 return transaction_send(contextP, transaction);
terencez 0:c2dff8cbb91a 676 }
terencez 0:c2dff8cbb91a 677
terencez 0:c2dff8cbb91a 678 int lwm2m_bootstrap_write(lwm2m_context_t * contextP,
terencez 0:c2dff8cbb91a 679 void * sessionH,
terencez 0:c2dff8cbb91a 680 lwm2m_uri_t * uriP,
terencez 0:c2dff8cbb91a 681 lwm2m_media_type_t format,
terencez 0:c2dff8cbb91a 682 uint8_t * buffer,
terencez 0:c2dff8cbb91a 683 size_t length)
terencez 0:c2dff8cbb91a 684 {
terencez 0:c2dff8cbb91a 685 lwm2m_transaction_t * transaction;
terencez 0:c2dff8cbb91a 686 bs_data_t * dataP;
terencez 0:c2dff8cbb91a 687
terencez 0:c2dff8cbb91a 688 LOG_URI(uriP);
terencez 0:c2dff8cbb91a 689 if (uriP == NULL
terencez 0:c2dff8cbb91a 690 || buffer == NULL
terencez 0:c2dff8cbb91a 691 || length == 0)
terencez 0:c2dff8cbb91a 692 {
terencez 0:c2dff8cbb91a 693 return COAP_400_BAD_REQUEST;
terencez 0:c2dff8cbb91a 694 }
terencez 0:c2dff8cbb91a 695
terencez 0:c2dff8cbb91a 696 transaction = transaction_new(sessionH, COAP_PUT, NULL, uriP, contextP->nextMID++, 4, NULL);
terencez 0:c2dff8cbb91a 697 if (transaction == NULL) return COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:c2dff8cbb91a 698
terencez 0:c2dff8cbb91a 699 coap_set_header_content_type(transaction->message, format);
terencez 0:c2dff8cbb91a 700 coap_set_payload(transaction->message, buffer, length);
terencez 0:c2dff8cbb91a 701
terencez 0:c2dff8cbb91a 702 dataP = (bs_data_t *)lwm2m_malloc(sizeof(bs_data_t));
terencez 0:c2dff8cbb91a 703 if (dataP == NULL)
terencez 0:c2dff8cbb91a 704 {
terencez 0:c2dff8cbb91a 705 transaction_free(transaction);
terencez 0:c2dff8cbb91a 706 return COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:c2dff8cbb91a 707 }
terencez 0:c2dff8cbb91a 708 dataP->isUri = true;
terencez 0:c2dff8cbb91a 709 memcpy(&dataP->uri, uriP, sizeof(lwm2m_uri_t));
terencez 0:c2dff8cbb91a 710 dataP->callback = contextP->bootstrapCallback;
terencez 0:c2dff8cbb91a 711 dataP->userData = contextP->bootstrapUserData;
terencez 0:c2dff8cbb91a 712
terencez 0:c2dff8cbb91a 713 transaction->callback = prv_resultCallback;
terencez 0:c2dff8cbb91a 714 transaction->userData = (void *)dataP;
terencez 0:c2dff8cbb91a 715
terencez 0:c2dff8cbb91a 716 contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction);
terencez 0:c2dff8cbb91a 717
terencez 0:c2dff8cbb91a 718 return transaction_send(contextP, transaction);
terencez 0:c2dff8cbb91a 719 }
terencez 0:c2dff8cbb91a 720
terencez 0:c2dff8cbb91a 721 int lwm2m_bootstrap_finish(lwm2m_context_t * contextP,
terencez 0:c2dff8cbb91a 722 void * sessionH)
terencez 0:c2dff8cbb91a 723 {
terencez 0:c2dff8cbb91a 724 lwm2m_transaction_t * transaction;
terencez 0:c2dff8cbb91a 725 bs_data_t * dataP;
terencez 0:c2dff8cbb91a 726
terencez 0:c2dff8cbb91a 727 LOG("Entering");
terencez 0:c2dff8cbb91a 728 transaction = transaction_new(sessionH, COAP_POST, NULL, NULL, contextP->nextMID++, 4, NULL);
terencez 0:c2dff8cbb91a 729 if (transaction == NULL) return COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:c2dff8cbb91a 730
terencez 0:c2dff8cbb91a 731 coap_set_header_uri_path(transaction->message, "/"URI_BOOTSTRAP_SEGMENT);
terencez 0:c2dff8cbb91a 732
terencez 0:c2dff8cbb91a 733 dataP = (bs_data_t *)lwm2m_malloc(sizeof(bs_data_t));
terencez 0:c2dff8cbb91a 734 if (dataP == NULL)
terencez 0:c2dff8cbb91a 735 {
terencez 0:c2dff8cbb91a 736 transaction_free(transaction);
terencez 0:c2dff8cbb91a 737 return COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:c2dff8cbb91a 738 }
terencez 0:c2dff8cbb91a 739 dataP->isUri = false;
terencez 0:c2dff8cbb91a 740 dataP->callback = contextP->bootstrapCallback;
terencez 0:c2dff8cbb91a 741 dataP->userData = contextP->bootstrapUserData;
terencez 0:c2dff8cbb91a 742
terencez 0:c2dff8cbb91a 743 transaction->callback = prv_resultCallback;
terencez 0:c2dff8cbb91a 744 transaction->userData = (void *)dataP;
terencez 0:c2dff8cbb91a 745
terencez 0:c2dff8cbb91a 746 contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction);
terencez 0:c2dff8cbb91a 747
terencez 0:c2dff8cbb91a 748 return transaction_send(contextP, transaction);
terencez 0:c2dff8cbb91a 749 }
terencez 0:c2dff8cbb91a 750
terencez 0:c2dff8cbb91a 751 #endif