terence zhang / Mbed OS mbed-os-example-wakaama

Dependencies:   C12832 LM75B

Committer:
terence zhang
Date:
Fri Apr 28 18:13:27 2017 +0800
Revision:
3:a280069151ac
Parent:
0:f9d13e09cf11
Child:
14:ec9e195830ff
update and merge wakaama to the latest version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
terencez 0:f9d13e09cf11 1 /*******************************************************************************
terencez 0:f9d13e09cf11 2 *
terencez 0:f9d13e09cf11 3 * Copyright (c) 2013, 2014 Intel Corporation and others.
terencez 0:f9d13e09cf11 4 * All rights reserved. This program and the accompanying materials
terencez 0:f9d13e09cf11 5 * are made available under the terms of the Eclipse Public License v1.0
terencez 0:f9d13e09cf11 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
terencez 0:f9d13e09cf11 7 *
terencez 0:f9d13e09cf11 8 * The Eclipse Public License is available at
terencez 0:f9d13e09cf11 9 * http://www.eclipse.org/legal/epl-v10.html
terencez 0:f9d13e09cf11 10 * The Eclipse Distribution License is available at
terencez 0:f9d13e09cf11 11 * http://www.eclipse.org/org/documents/edl-v10.php.
terencez 0:f9d13e09cf11 12 *
terencez 0:f9d13e09cf11 13 * Contributors:
terencez 0:f9d13e09cf11 14 * David Navarro, Intel Corporation - initial API and implementation
terencez 0:f9d13e09cf11 15 * domedambrosio - Please refer to git log
terencez 0:f9d13e09cf11 16 * Fabien Fleutot - Please refer to git log
terencez 0:f9d13e09cf11 17 * Fabien Fleutot - Please refer to git log
terencez 0:f9d13e09cf11 18 * Simon Bernard - Please refer to git log
terencez 0:f9d13e09cf11 19 * Toby Jaffey - Please refer to git log
terence zhang 3:a280069151ac 20 * Pascal Rieux - Please refer to git log
terence zhang 3:a280069151ac 21 * Bosch Software Innovations GmbH - Please refer to git log
terencez 0:f9d13e09cf11 22 *
terencez 0:f9d13e09cf11 23 *******************************************************************************/
terencez 0:f9d13e09cf11 24
terencez 0:f9d13e09cf11 25 /*
terencez 0:f9d13e09cf11 26 Copyright (c) 2013, 2014 Intel Corporation
terencez 0:f9d13e09cf11 27
terencez 0:f9d13e09cf11 28 Redistribution and use in source and binary forms, with or without modification,
terencez 0:f9d13e09cf11 29 are permitted provided that the following conditions are met:
terencez 0:f9d13e09cf11 30
terencez 0:f9d13e09cf11 31 * Redistributions of source code must retain the above copyright notice,
terencez 0:f9d13e09cf11 32 this list of conditions and the following disclaimer.
terencez 0:f9d13e09cf11 33 * Redistributions in binary form must reproduce the above copyright notice,
terencez 0:f9d13e09cf11 34 this list of conditions and the following disclaimer in the documentation
terencez 0:f9d13e09cf11 35 and/or other materials provided with the distribution.
terencez 0:f9d13e09cf11 36 * Neither the name of Intel Corporation nor the names of its contributors
terencez 0:f9d13e09cf11 37 may be used to endorse or promote products derived from this software
terencez 0:f9d13e09cf11 38 without specific prior written permission.
terencez 0:f9d13e09cf11 39
terencez 0:f9d13e09cf11 40 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
terencez 0:f9d13e09cf11 41 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
terencez 0:f9d13e09cf11 42 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
terencez 0:f9d13e09cf11 43 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
terencez 0:f9d13e09cf11 44 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
terencez 0:f9d13e09cf11 45 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
terencez 0:f9d13e09cf11 46 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
terencez 0:f9d13e09cf11 47 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
terencez 0:f9d13e09cf11 48 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
terencez 0:f9d13e09cf11 49 THE POSSIBILITY OF SUCH DAMAGE.
terencez 0:f9d13e09cf11 50
terencez 0:f9d13e09cf11 51 David Navarro <david.navarro@intel.com>
terencez 0:f9d13e09cf11 52
terencez 0:f9d13e09cf11 53 */
terencez 0:f9d13e09cf11 54
terencez 0:f9d13e09cf11 55 /*
terencez 0:f9d13e09cf11 56 Contains code snippets which are:
terencez 0:f9d13e09cf11 57
terencez 0:f9d13e09cf11 58 * Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
terencez 0:f9d13e09cf11 59 * All rights reserved.
terencez 0:f9d13e09cf11 60 *
terencez 0:f9d13e09cf11 61 * Redistribution and use in source and binary forms, with or without
terencez 0:f9d13e09cf11 62 * modification, are permitted provided that the following conditions
terencez 0:f9d13e09cf11 63 * are met:
terencez 0:f9d13e09cf11 64 * 1. Redistributions of source code must retain the above copyright
terencez 0:f9d13e09cf11 65 * notice, this list of conditions and the following disclaimer.
terencez 0:f9d13e09cf11 66 * 2. Redistributions in binary form must reproduce the above copyright
terencez 0:f9d13e09cf11 67 * notice, this list of conditions and the following disclaimer in the
terencez 0:f9d13e09cf11 68 * documentation and/or other materials provided with the distribution.
terencez 0:f9d13e09cf11 69 * 3. Neither the name of the Institute nor the names of its contributors
terencez 0:f9d13e09cf11 70 * may be used to endorse or promote products derived from this software
terencez 0:f9d13e09cf11 71 * without specific prior written permission.
terencez 0:f9d13e09cf11 72 *
terencez 0:f9d13e09cf11 73 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
terencez 0:f9d13e09cf11 74 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
terencez 0:f9d13e09cf11 75 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
terencez 0:f9d13e09cf11 76 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
terencez 0:f9d13e09cf11 77 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
terencez 0:f9d13e09cf11 78 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
terencez 0:f9d13e09cf11 79 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
terencez 0:f9d13e09cf11 80 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
terencez 0:f9d13e09cf11 81 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
terencez 0:f9d13e09cf11 82 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
terencez 0:f9d13e09cf11 83 * SUCH DAMAGE.
terencez 0:f9d13e09cf11 84
terencez 0:f9d13e09cf11 85 */
terencez 0:f9d13e09cf11 86
terencez 0:f9d13e09cf11 87
terencez 0:f9d13e09cf11 88 #include "internals.h"
terencez 0:f9d13e09cf11 89
terencez 0:f9d13e09cf11 90 #include <stdlib.h>
terencez 0:f9d13e09cf11 91 #include <string.h>
terencez 0:f9d13e09cf11 92
terencez 0:f9d13e09cf11 93 #include <stdio.h>
terencez 0:f9d13e09cf11 94
terencez 0:f9d13e09cf11 95
terencez 0:f9d13e09cf11 96 static void handle_reset(lwm2m_context_t * contextP,
terencez 0:f9d13e09cf11 97 void * fromSessionH,
terencez 0:f9d13e09cf11 98 coap_packet_t * message)
terencez 0:f9d13e09cf11 99 {
terencez 0:f9d13e09cf11 100 #ifdef LWM2M_CLIENT_MODE
terence zhang 3:a280069151ac 101 LOG("Entering");
terence zhang 3:a280069151ac 102 observe_cancel(contextP, message->mid, fromSessionH);
terencez 0:f9d13e09cf11 103 #endif
terencez 0:f9d13e09cf11 104 }
terencez 0:f9d13e09cf11 105
terencez 0:f9d13e09cf11 106 static coap_status_t handle_request(lwm2m_context_t * contextP,
terencez 0:f9d13e09cf11 107 void * fromSessionH,
terencez 0:f9d13e09cf11 108 coap_packet_t * message,
terencez 0:f9d13e09cf11 109 coap_packet_t * response)
terencez 0:f9d13e09cf11 110 {
terencez 0:f9d13e09cf11 111 lwm2m_uri_t * uriP;
terence zhang 3:a280069151ac 112 coap_status_t result = COAP_IGNORE;
terencez 0:f9d13e09cf11 113
terence zhang 3:a280069151ac 114 LOG("Entering");
terence zhang 3:a280069151ac 115
terence zhang 3:a280069151ac 116 #ifdef LWM2M_CLIENT_MODE
terence zhang 3:a280069151ac 117 uriP = uri_decode(contextP->altPath, message->uri_path);
terence zhang 3:a280069151ac 118 #else
terence zhang 3:a280069151ac 119 uriP = uri_decode(NULL, message->uri_path);
terence zhang 3:a280069151ac 120 #endif
terence zhang 3:a280069151ac 121
terence zhang 3:a280069151ac 122 if (uriP == NULL) return COAP_400_BAD_REQUEST;
terencez 0:f9d13e09cf11 123
terencez 0:f9d13e09cf11 124 switch(uriP->flag & LWM2M_URI_MASK_TYPE)
terencez 0:f9d13e09cf11 125 {
terencez 0:f9d13e09cf11 126 #ifdef LWM2M_CLIENT_MODE
terencez 0:f9d13e09cf11 127 case LWM2M_URI_FLAG_DM:
terence zhang 3:a280069151ac 128 {
terence zhang 3:a280069151ac 129 lwm2m_server_t * serverP;
terence zhang 3:a280069151ac 130
terence zhang 3:a280069151ac 131 serverP = utils_findServer(contextP, fromSessionH);
terence zhang 3:a280069151ac 132 if (serverP != NULL)
terence zhang 3:a280069151ac 133 {
terence zhang 3:a280069151ac 134 result = dm_handleRequest(contextP, uriP, serverP, message, response);
terence zhang 3:a280069151ac 135 }
terence zhang 3:a280069151ac 136 #ifdef LWM2M_BOOTSTRAP
terence zhang 3:a280069151ac 137 else
terence zhang 3:a280069151ac 138 {
terence zhang 3:a280069151ac 139 serverP = utils_findBootstrapServer(contextP, fromSessionH);
terence zhang 3:a280069151ac 140 if (serverP != NULL)
terence zhang 3:a280069151ac 141 {
terence zhang 3:a280069151ac 142 result = bootstrap_handleCommand(contextP, uriP, serverP, message, response);
terence zhang 3:a280069151ac 143 }
terence zhang 3:a280069151ac 144 }
terence zhang 3:a280069151ac 145 #endif
terence zhang 3:a280069151ac 146 }
terence zhang 3:a280069151ac 147 break;
terence zhang 3:a280069151ac 148
terence zhang 3:a280069151ac 149 #ifdef LWM2M_BOOTSTRAP
terence zhang 3:a280069151ac 150 case LWM2M_URI_FLAG_DELETE_ALL:
terence zhang 3:a280069151ac 151 if (COAP_DELETE != message->code)
terence zhang 3:a280069151ac 152 {
terence zhang 3:a280069151ac 153 result = COAP_400_BAD_REQUEST;
terence zhang 3:a280069151ac 154 }
terence zhang 3:a280069151ac 155 else
terence zhang 3:a280069151ac 156 {
terence zhang 3:a280069151ac 157 result = bootstrap_handleDeleteAll(contextP, fromSessionH);
terence zhang 3:a280069151ac 158 }
terencez 0:f9d13e09cf11 159 break;
terencez 0:f9d13e09cf11 160
terencez 0:f9d13e09cf11 161 case LWM2M_URI_FLAG_BOOTSTRAP:
terence zhang 3:a280069151ac 162 if (message->code == COAP_POST)
terence zhang 3:a280069151ac 163 {
terence zhang 3:a280069151ac 164 result = bootstrap_handleFinish(contextP, fromSessionH);
terence zhang 3:a280069151ac 165 }
terencez 0:f9d13e09cf11 166 break;
terencez 0:f9d13e09cf11 167 #endif
terence zhang 3:a280069151ac 168 #endif
terencez 0:f9d13e09cf11 169
terencez 0:f9d13e09cf11 170 #ifdef LWM2M_SERVER_MODE
terence zhang 3:a280069151ac 171 case LWM2M_URI_FLAG_REGISTRATION:
terence zhang 3:a280069151ac 172 result = registration_handleRequest(contextP, uriP, fromSessionH, message, response);
terence zhang 3:a280069151ac 173 break;
terence zhang 3:a280069151ac 174 #endif
terence zhang 3:a280069151ac 175 #ifdef LWM2M_BOOTSTRAP_SERVER_MODE
terence zhang 3:a280069151ac 176 case LWM2M_URI_FLAG_BOOTSTRAP:
terence zhang 3:a280069151ac 177 result = bootstrap_handleRequest(contextP, uriP, fromSessionH, message, response);
terencez 0:f9d13e09cf11 178 break;
terencez 0:f9d13e09cf11 179 #endif
terencez 0:f9d13e09cf11 180 default:
terence zhang 3:a280069151ac 181 result = COAP_IGNORE;
terencez 0:f9d13e09cf11 182 break;
terencez 0:f9d13e09cf11 183 }
terencez 0:f9d13e09cf11 184
terencez 0:f9d13e09cf11 185 coap_set_status_code(response, result);
terencez 0:f9d13e09cf11 186
terence zhang 3:a280069151ac 187 if (COAP_IGNORE < result && result < COAP_400_BAD_REQUEST)
terencez 0:f9d13e09cf11 188 {
terencez 0:f9d13e09cf11 189 result = NO_ERROR;
terencez 0:f9d13e09cf11 190 }
terencez 0:f9d13e09cf11 191
terence zhang 3:a280069151ac 192 lwm2m_free(uriP);
terencez 0:f9d13e09cf11 193 return result;
terencez 0:f9d13e09cf11 194 }
terencez 0:f9d13e09cf11 195
terencez 0:f9d13e09cf11 196 /* This function is an adaptation of function coap_receive() from Erbium's er-coap-13-engine.c.
terencez 0:f9d13e09cf11 197 * Erbium is Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
terencez 0:f9d13e09cf11 198 * All rights reserved.
terencez 0:f9d13e09cf11 199 */
terencez 0:f9d13e09cf11 200 void lwm2m_handle_packet(lwm2m_context_t * contextP,
terencez 0:f9d13e09cf11 201 uint8_t * buffer,
terencez 0:f9d13e09cf11 202 int length,
terencez 0:f9d13e09cf11 203 void * fromSessionH)
terencez 0:f9d13e09cf11 204 {
terencez 0:f9d13e09cf11 205 coap_status_t coap_error_code = NO_ERROR;
terencez 0:f9d13e09cf11 206 static coap_packet_t message[1];
terencez 0:f9d13e09cf11 207 static coap_packet_t response[1];
terencez 0:f9d13e09cf11 208
terence zhang 3:a280069151ac 209 LOG("Entering");
terencez 0:f9d13e09cf11 210 coap_error_code = coap_parse_message(message, buffer, (uint16_t)length);
terence zhang 3:a280069151ac 211 if (coap_error_code == NO_ERROR)
terencez 0:f9d13e09cf11 212 {
terence zhang 3:a280069151ac 213 LOG_ARG("Parsed: ver %u, type %u, tkl %u, code %u.%.2u, mid %u, Content type: %d",
terence zhang 3:a280069151ac 214 message->version, message->type, message->token_len, message->code >> 5, message->code & 0x1F, message->mid, message->content_type);
terence zhang 3:a280069151ac 215 LOG_ARG("Payload: %.*s", message->payload_len, message->payload);
terencez 0:f9d13e09cf11 216 if (message->code >= COAP_GET && message->code <= COAP_DELETE)
terencez 0:f9d13e09cf11 217 {
terencez 0:f9d13e09cf11 218 uint32_t block_num = 0;
terencez 0:f9d13e09cf11 219 uint16_t block_size = REST_MAX_CHUNK_SIZE;
terencez 0:f9d13e09cf11 220 uint32_t block_offset = 0;
terence zhang 3:a280069151ac 221 int64_t new_offset = 0;
terencez 0:f9d13e09cf11 222
terencez 0:f9d13e09cf11 223 /* prepare response */
terence zhang 3:a280069151ac 224 if (message->type == COAP_TYPE_CON)
terencez 0:f9d13e09cf11 225 {
terencez 0:f9d13e09cf11 226 /* Reliable CON requests are answered with an ACK. */
terence zhang 3:a280069151ac 227 coap_init_message(response, COAP_TYPE_ACK, COAP_205_CONTENT, message->mid);
terencez 0:f9d13e09cf11 228 }
terencez 0:f9d13e09cf11 229 else
terencez 0:f9d13e09cf11 230 {
terencez 0:f9d13e09cf11 231 /* Unreliable NON requests are answered with a NON as well. */
terence zhang 3:a280069151ac 232 coap_init_message(response, COAP_TYPE_NON, COAP_205_CONTENT, contextP->nextMID++);
terencez 0:f9d13e09cf11 233 }
terencez 0:f9d13e09cf11 234
terencez 0:f9d13e09cf11 235 /* mirror token */
terencez 0:f9d13e09cf11 236 if (message->token_len)
terencez 0:f9d13e09cf11 237 {
terencez 0:f9d13e09cf11 238 coap_set_header_token(response, message->token, message->token_len);
terencez 0:f9d13e09cf11 239 }
terencez 0:f9d13e09cf11 240
terencez 0:f9d13e09cf11 241 /* get offset for blockwise transfers */
terencez 0:f9d13e09cf11 242 if (coap_get_header_block2(message, &block_num, NULL, &block_size, &block_offset))
terencez 0:f9d13e09cf11 243 {
terence zhang 3:a280069151ac 244 LOG_ARG("Blockwise: block request %u (%u/%u) @ %u bytes", block_num, block_size, REST_MAX_CHUNK_SIZE, block_offset);
terencez 0:f9d13e09cf11 245 block_size = MIN(block_size, REST_MAX_CHUNK_SIZE);
terencez 0:f9d13e09cf11 246 new_offset = block_offset;
terencez 0:f9d13e09cf11 247 }
terencez 0:f9d13e09cf11 248
terence zhang 3:a280069151ac 249 /* handle block1 option */
terence zhang 3:a280069151ac 250 if (IS_OPTION(message, COAP_OPTION_BLOCK1))
terence zhang 3:a280069151ac 251 {
terence zhang 3:a280069151ac 252 #ifdef LWM2M_CLIENT_MODE
terence zhang 3:a280069151ac 253 // get server
terence zhang 3:a280069151ac 254 lwm2m_server_t * serverP;
terence zhang 3:a280069151ac 255 serverP = utils_findServer(contextP, fromSessionH);
terence zhang 3:a280069151ac 256 #ifdef LWM2M_BOOTSTRAP
terence zhang 3:a280069151ac 257 if (serverP == NULL)
terence zhang 3:a280069151ac 258 {
terence zhang 3:a280069151ac 259 serverP = utils_findBootstrapServer(contextP, fromSessionH);
terence zhang 3:a280069151ac 260 }
terence zhang 3:a280069151ac 261 #endif
terence zhang 3:a280069151ac 262 if (serverP == NULL)
terence zhang 3:a280069151ac 263 {
terence zhang 3:a280069151ac 264 coap_error_code = COAP_500_INTERNAL_SERVER_ERROR;
terence zhang 3:a280069151ac 265 }
terence zhang 3:a280069151ac 266 else
terence zhang 3:a280069151ac 267 {
terence zhang 3:a280069151ac 268 uint32_t block1_num;
terence zhang 3:a280069151ac 269 uint8_t block1_more;
terence zhang 3:a280069151ac 270 uint16_t block1_size;
terence zhang 3:a280069151ac 271 uint8_t * complete_buffer = NULL;
terence zhang 3:a280069151ac 272 size_t complete_buffer_size;
terence zhang 3:a280069151ac 273
terence zhang 3:a280069151ac 274 // parse block1 header
terence zhang 3:a280069151ac 275 coap_get_header_block1(message, &block1_num, &block1_more, &block1_size, NULL);
terence zhang 3:a280069151ac 276 LOG_ARG("Blockwise: block1 request NUM %u (SZX %u/ SZX Max%u) MORE %u", block1_num, block1_size, REST_MAX_CHUNK_SIZE, block1_more);
terence zhang 3:a280069151ac 277
terence zhang 3:a280069151ac 278 // handle block 1
terence zhang 3:a280069151ac 279 coap_error_code = coap_block1_handler(&serverP->block1Data, message->mid, message->payload, message->payload_len, block1_size, block1_num, block1_more, &complete_buffer, &complete_buffer_size);
terence zhang 3:a280069151ac 280
terence zhang 3:a280069151ac 281 // if payload is complete, replace it in the coap message.
terence zhang 3:a280069151ac 282 if (coap_error_code == NO_ERROR)
terence zhang 3:a280069151ac 283 {
terence zhang 3:a280069151ac 284 message->payload = complete_buffer;
terence zhang 3:a280069151ac 285 message->payload_len = complete_buffer_size;
terence zhang 3:a280069151ac 286 }
terence zhang 3:a280069151ac 287 else if (coap_error_code == COAP_231_CONTINUE)
terence zhang 3:a280069151ac 288 {
terence zhang 3:a280069151ac 289 block1_size = MIN(block1_size, REST_MAX_CHUNK_SIZE);
terence zhang 3:a280069151ac 290 coap_set_header_block1(response,block1_num, block1_more,block1_size);
terence zhang 3:a280069151ac 291 }
terence zhang 3:a280069151ac 292 }
terence zhang 3:a280069151ac 293 #else
terence zhang 3:a280069151ac 294 coap_error_code = COAP_501_NOT_IMPLEMENTED;
terence zhang 3:a280069151ac 295 #endif
terence zhang 3:a280069151ac 296 }
terence zhang 3:a280069151ac 297 if (coap_error_code == NO_ERROR)
terence zhang 3:a280069151ac 298 {
terence zhang 3:a280069151ac 299 coap_error_code = handle_request(contextP, fromSessionH, message, response);
terence zhang 3:a280069151ac 300 }
terencez 0:f9d13e09cf11 301 if (coap_error_code==NO_ERROR)
terencez 0:f9d13e09cf11 302 {
terence zhang 3:a280069151ac 303 if ( IS_OPTION(message, COAP_OPTION_BLOCK2) )
terencez 0:f9d13e09cf11 304 {
terencez 0:f9d13e09cf11 305 /* unchanged new_offset indicates that resource is unaware of blockwise transfer */
terencez 0:f9d13e09cf11 306 if (new_offset==block_offset)
terencez 0:f9d13e09cf11 307 {
terence zhang 3:a280069151ac 308 LOG_ARG("Blockwise: unaware resource with payload length %u/%u", response->payload_len, block_size);
terencez 0:f9d13e09cf11 309 if (block_offset >= response->payload_len)
terencez 0:f9d13e09cf11 310 {
terence zhang 3:a280069151ac 311 LOG("handle_incoming_data(): block_offset >= response->payload_len");
terencez 0:f9d13e09cf11 312
terence zhang 3:a280069151ac 313 response->code = COAP_402_BAD_OPTION;
terencez 0:f9d13e09cf11 314 coap_set_payload(response, "BlockOutOfScope", 15); /* a const char str[] and sizeof(str) produces larger code size */
terencez 0:f9d13e09cf11 315 }
terencez 0:f9d13e09cf11 316 else
terencez 0:f9d13e09cf11 317 {
terencez 0:f9d13e09cf11 318 coap_set_header_block2(response, block_num, response->payload_len - block_offset > block_size, block_size);
terencez 0:f9d13e09cf11 319 coap_set_payload(response, response->payload+block_offset, MIN(response->payload_len - block_offset, block_size));
terencez 0:f9d13e09cf11 320 } /* if (valid offset) */
terencez 0:f9d13e09cf11 321 }
terencez 0:f9d13e09cf11 322 else
terencez 0:f9d13e09cf11 323 {
terencez 0:f9d13e09cf11 324 /* resource provides chunk-wise data */
terence zhang 3:a280069151ac 325 LOG_ARG("Blockwise: blockwise resource, new offset %d", (int) new_offset);
terencez 0:f9d13e09cf11 326 coap_set_header_block2(response, block_num, new_offset!=-1 || response->payload_len > block_size, block_size);
terencez 0:f9d13e09cf11 327 if (response->payload_len > block_size) coap_set_payload(response, response->payload, block_size);
terencez 0:f9d13e09cf11 328 } /* if (resource aware of blockwise) */
terencez 0:f9d13e09cf11 329 }
terencez 0:f9d13e09cf11 330 else if (new_offset!=0)
terencez 0:f9d13e09cf11 331 {
terence zhang 3:a280069151ac 332 LOG_ARG("Blockwise: no block option for blockwise resource, using block size %u", REST_MAX_CHUNK_SIZE);
terencez 0:f9d13e09cf11 333
terencez 0:f9d13e09cf11 334 coap_set_header_block2(response, 0, new_offset!=-1, REST_MAX_CHUNK_SIZE);
terencez 0:f9d13e09cf11 335 coap_set_payload(response, response->payload, MIN(response->payload_len, REST_MAX_CHUNK_SIZE));
terencez 0:f9d13e09cf11 336 } /* if (blockwise request) */
terencez 0:f9d13e09cf11 337
terencez 0:f9d13e09cf11 338 coap_error_code = message_send(contextP, response, fromSessionH);
terencez 0:f9d13e09cf11 339
terencez 0:f9d13e09cf11 340 lwm2m_free(response->payload);
terencez 0:f9d13e09cf11 341 response->payload = NULL;
terencez 0:f9d13e09cf11 342 response->payload_len = 0;
terencez 0:f9d13e09cf11 343 }
terence zhang 3:a280069151ac 344 else if (coap_error_code != COAP_IGNORE)
terencez 0:f9d13e09cf11 345 {
terence zhang 3:a280069151ac 346 if (1 == coap_set_status_code(response, coap_error_code))
terence zhang 3:a280069151ac 347 {
terence zhang 3:a280069151ac 348 coap_error_code = message_send(contextP, response, fromSessionH);
terence zhang 3:a280069151ac 349 }
terencez 0:f9d13e09cf11 350 }
terencez 0:f9d13e09cf11 351 }
terencez 0:f9d13e09cf11 352 else
terencez 0:f9d13e09cf11 353 {
terencez 0:f9d13e09cf11 354 /* Responses */
terence zhang 3:a280069151ac 355 switch (message->type)
terencez 0:f9d13e09cf11 356 {
terence zhang 3:a280069151ac 357 case COAP_TYPE_NON:
terence zhang 3:a280069151ac 358 case COAP_TYPE_CON:
terence zhang 3:a280069151ac 359 {
terence zhang 3:a280069151ac 360 bool done = transaction_handleResponse(contextP, fromSessionH, message, response);
terencez 0:f9d13e09cf11 361
terencez 0:f9d13e09cf11 362 #ifdef LWM2M_SERVER_MODE
terence zhang 3:a280069151ac 363 if (!done && IS_OPTION(message, COAP_OPTION_OBSERVE) &&
terence zhang 3:a280069151ac 364 ((message->code == COAP_204_CHANGED) || (message->code == COAP_205_CONTENT)))
terence zhang 3:a280069151ac 365 {
terence zhang 3:a280069151ac 366 done = observe_handleNotify(contextP, fromSessionH, message, response);
terence zhang 3:a280069151ac 367 }
terencez 0:f9d13e09cf11 368 #endif
terence zhang 3:a280069151ac 369 if (!done && message->type == COAP_TYPE_CON )
terence zhang 3:a280069151ac 370 {
terence zhang 3:a280069151ac 371 coap_init_message(response, COAP_TYPE_ACK, 0, message->mid);
terence zhang 3:a280069151ac 372 coap_error_code = message_send(contextP, response, fromSessionH);
terence zhang 3:a280069151ac 373 }
terence zhang 3:a280069151ac 374 }
terence zhang 3:a280069151ac 375 break;
terence zhang 3:a280069151ac 376
terence zhang 3:a280069151ac 377 case COAP_TYPE_RST:
terence zhang 3:a280069151ac 378 /* Cancel possible subscriptions. */
terence zhang 3:a280069151ac 379 handle_reset(contextP, fromSessionH, message);
terence zhang 3:a280069151ac 380 transaction_handleResponse(contextP, fromSessionH, message, NULL);
terence zhang 3:a280069151ac 381 break;
terence zhang 3:a280069151ac 382
terence zhang 3:a280069151ac 383 case COAP_TYPE_ACK:
terence zhang 3:a280069151ac 384 transaction_handleResponse(contextP, fromSessionH, message, NULL);
terence zhang 3:a280069151ac 385 break;
terence zhang 3:a280069151ac 386
terence zhang 3:a280069151ac 387 default:
terence zhang 3:a280069151ac 388 break;
terencez 0:f9d13e09cf11 389 }
terencez 0:f9d13e09cf11 390 } /* Request or Response */
terencez 0:f9d13e09cf11 391 coap_free_header(message);
terencez 0:f9d13e09cf11 392 } /* if (parsed correctly) */
terencez 0:f9d13e09cf11 393 else
terencez 0:f9d13e09cf11 394 {
terence zhang 3:a280069151ac 395 LOG_ARG("Message parsing failed %u.%2u", coap_error_code >> 5, coap_error_code & 0x1F);
terencez 0:f9d13e09cf11 396 }
terencez 0:f9d13e09cf11 397
terence zhang 3:a280069151ac 398 if (coap_error_code != NO_ERROR && coap_error_code != COAP_IGNORE)
terencez 0:f9d13e09cf11 399 {
terence zhang 3:a280069151ac 400 LOG_ARG("ERROR %u: %s", coap_error_code, coap_error_message);
terencez 0:f9d13e09cf11 401
terencez 0:f9d13e09cf11 402 /* Set to sendable error code. */
terencez 0:f9d13e09cf11 403 if (coap_error_code >= 192)
terencez 0:f9d13e09cf11 404 {
terence zhang 3:a280069151ac 405 coap_error_code = COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:f9d13e09cf11 406 }
terencez 0:f9d13e09cf11 407 /* Reuse input buffer for error message. */
terencez 0:f9d13e09cf11 408 coap_init_message(message, COAP_TYPE_ACK, coap_error_code, message->mid);
terencez 0:f9d13e09cf11 409 coap_set_payload(message, coap_error_message, strlen(coap_error_message));
terencez 0:f9d13e09cf11 410 message_send(contextP, message, fromSessionH);
terencez 0:f9d13e09cf11 411 }
terencez 0:f9d13e09cf11 412 }
terencez 0:f9d13e09cf11 413
terencez 0:f9d13e09cf11 414
terencez 0:f9d13e09cf11 415 coap_status_t message_send(lwm2m_context_t * contextP,
terencez 0:f9d13e09cf11 416 coap_packet_t * message,
terencez 0:f9d13e09cf11 417 void * sessionH)
terencez 0:f9d13e09cf11 418 {
terence zhang 3:a280069151ac 419 coap_status_t result = COAP_500_INTERNAL_SERVER_ERROR;
terence zhang 3:a280069151ac 420 uint8_t * pktBuffer;
terencez 0:f9d13e09cf11 421 size_t pktBufferLen = 0;
terence zhang 3:a280069151ac 422 size_t allocLen;
terence zhang 3:a280069151ac 423
terence zhang 3:a280069151ac 424 LOG("Entering");
terence zhang 3:a280069151ac 425 allocLen = coap_serialize_get_size(message);
terence zhang 3:a280069151ac 426 LOG_ARG("Size to allocate: %d", allocLen);
terence zhang 3:a280069151ac 427 if (allocLen == 0) return COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:f9d13e09cf11 428
terence zhang 3:a280069151ac 429 pktBuffer = (uint8_t *)lwm2m_malloc(allocLen);
terence zhang 3:a280069151ac 430 if (pktBuffer != NULL)
terencez 0:f9d13e09cf11 431 {
terence zhang 3:a280069151ac 432 pktBufferLen = coap_serialize_message(message, pktBuffer);
terence zhang 3:a280069151ac 433 LOG_ARG("coap_serialize_message() returned %d", pktBufferLen);
terence zhang 3:a280069151ac 434 if (0 != pktBufferLen)
terence zhang 3:a280069151ac 435 {
terence zhang 3:a280069151ac 436 result = lwm2m_buffer_send(sessionH, pktBuffer, pktBufferLen, contextP->userData);
terence zhang 3:a280069151ac 437 }
terence zhang 3:a280069151ac 438 lwm2m_free(pktBuffer);
terencez 0:f9d13e09cf11 439 }
terencez 0:f9d13e09cf11 440
terencez 0:f9d13e09cf11 441 return result;
terencez 0:f9d13e09cf11 442 }
terencez 0:f9d13e09cf11 443