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