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

Dependencies:   C12832 LM75B

Committer:
terencez
Date:
Sat May 06 11:21:27 2017 +0000
Revision:
14:ec9e195830ff
Parent:
3:a280069151ac
Child:
15:d0f20339c1ad
First compile version, registration success but could not handle request.

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 {
terencez 14:ec9e195830ff 129 #ifdef MBED_OS_EXAMPLE_WAKAAMA
terencez 14:ec9e195830ff 130 result = dm_handleRequest(contextP, uriP, fromSessionH, message, response);
terencez 14:ec9e195830ff 131 #else
terence zhang 3:a280069151ac 132 lwm2m_server_t * serverP;
terence zhang 3:a280069151ac 133
terence zhang 3:a280069151ac 134 serverP = utils_findServer(contextP, fromSessionH);
terence zhang 3:a280069151ac 135 if (serverP != NULL)
terence zhang 3:a280069151ac 136 {
terence zhang 3:a280069151ac 137 result = dm_handleRequest(contextP, uriP, serverP, message, response);
terence zhang 3:a280069151ac 138 }
terence zhang 3:a280069151ac 139 #ifdef LWM2M_BOOTSTRAP
terence zhang 3:a280069151ac 140 else
terence zhang 3:a280069151ac 141 {
terence zhang 3:a280069151ac 142 serverP = utils_findBootstrapServer(contextP, fromSessionH);
terence zhang 3:a280069151ac 143 if (serverP != NULL)
terence zhang 3:a280069151ac 144 {
terence zhang 3:a280069151ac 145 result = bootstrap_handleCommand(contextP, uriP, serverP, message, response);
terence zhang 3:a280069151ac 146 }
terence zhang 3:a280069151ac 147 }
terence zhang 3:a280069151ac 148 #endif
terencez 14:ec9e195830ff 149 #endif
terence zhang 3:a280069151ac 150 }
terence zhang 3:a280069151ac 151 break;
terence zhang 3:a280069151ac 152
terence zhang 3:a280069151ac 153 #ifdef LWM2M_BOOTSTRAP
terence zhang 3:a280069151ac 154 case LWM2M_URI_FLAG_DELETE_ALL:
terence zhang 3:a280069151ac 155 if (COAP_DELETE != message->code)
terence zhang 3:a280069151ac 156 {
terence zhang 3:a280069151ac 157 result = COAP_400_BAD_REQUEST;
terence zhang 3:a280069151ac 158 }
terence zhang 3:a280069151ac 159 else
terence zhang 3:a280069151ac 160 {
terence zhang 3:a280069151ac 161 result = bootstrap_handleDeleteAll(contextP, fromSessionH);
terence zhang 3:a280069151ac 162 }
terencez 0:f9d13e09cf11 163 break;
terencez 0:f9d13e09cf11 164
terencez 0:f9d13e09cf11 165 case LWM2M_URI_FLAG_BOOTSTRAP:
terence zhang 3:a280069151ac 166 if (message->code == COAP_POST)
terence zhang 3:a280069151ac 167 {
terence zhang 3:a280069151ac 168 result = bootstrap_handleFinish(contextP, fromSessionH);
terence zhang 3:a280069151ac 169 }
terencez 0:f9d13e09cf11 170 break;
terencez 0:f9d13e09cf11 171 #endif
terence zhang 3:a280069151ac 172 #endif
terencez 0:f9d13e09cf11 173
terencez 0:f9d13e09cf11 174 #ifdef LWM2M_SERVER_MODE
terence zhang 3:a280069151ac 175 case LWM2M_URI_FLAG_REGISTRATION:
terence zhang 3:a280069151ac 176 result = registration_handleRequest(contextP, uriP, fromSessionH, message, response);
terence zhang 3:a280069151ac 177 break;
terence zhang 3:a280069151ac 178 #endif
terence zhang 3:a280069151ac 179 #ifdef LWM2M_BOOTSTRAP_SERVER_MODE
terence zhang 3:a280069151ac 180 case LWM2M_URI_FLAG_BOOTSTRAP:
terence zhang 3:a280069151ac 181 result = bootstrap_handleRequest(contextP, uriP, fromSessionH, message, response);
terencez 0:f9d13e09cf11 182 break;
terencez 0:f9d13e09cf11 183 #endif
terencez 0:f9d13e09cf11 184 default:
terence zhang 3:a280069151ac 185 result = COAP_IGNORE;
terencez 0:f9d13e09cf11 186 break;
terencez 0:f9d13e09cf11 187 }
terencez 0:f9d13e09cf11 188
terencez 0:f9d13e09cf11 189 coap_set_status_code(response, result);
terencez 0:f9d13e09cf11 190
terence zhang 3:a280069151ac 191 if (COAP_IGNORE < result && result < COAP_400_BAD_REQUEST)
terencez 0:f9d13e09cf11 192 {
terencez 0:f9d13e09cf11 193 result = NO_ERROR;
terencez 0:f9d13e09cf11 194 }
terencez 0:f9d13e09cf11 195
terence zhang 3:a280069151ac 196 lwm2m_free(uriP);
terencez 0:f9d13e09cf11 197 return result;
terencez 0:f9d13e09cf11 198 }
terencez 0:f9d13e09cf11 199
terencez 0:f9d13e09cf11 200 /* This function is an adaptation of function coap_receive() from Erbium's er-coap-13-engine.c.
terencez 0:f9d13e09cf11 201 * Erbium is Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
terencez 0:f9d13e09cf11 202 * All rights reserved.
terencez 0:f9d13e09cf11 203 */
terencez 0:f9d13e09cf11 204 void lwm2m_handle_packet(lwm2m_context_t * contextP,
terencez 0:f9d13e09cf11 205 uint8_t * buffer,
terencez 0:f9d13e09cf11 206 int length,
terencez 0:f9d13e09cf11 207 void * fromSessionH)
terencez 0:f9d13e09cf11 208 {
terencez 0:f9d13e09cf11 209 coap_status_t coap_error_code = NO_ERROR;
terencez 0:f9d13e09cf11 210 static coap_packet_t message[1];
terencez 0:f9d13e09cf11 211 static coap_packet_t response[1];
terencez 0:f9d13e09cf11 212
terence zhang 3:a280069151ac 213 LOG("Entering");
terencez 0:f9d13e09cf11 214 coap_error_code = coap_parse_message(message, buffer, (uint16_t)length);
terence zhang 3:a280069151ac 215 if (coap_error_code == NO_ERROR)
terencez 0:f9d13e09cf11 216 {
terence zhang 3:a280069151ac 217 LOG_ARG("Parsed: ver %u, type %u, tkl %u, code %u.%.2u, mid %u, Content type: %d",
terence zhang 3:a280069151ac 218 message->version, message->type, message->token_len, message->code >> 5, message->code & 0x1F, message->mid, message->content_type);
terence zhang 3:a280069151ac 219 LOG_ARG("Payload: %.*s", message->payload_len, message->payload);
terencez 0:f9d13e09cf11 220 if (message->code >= COAP_GET && message->code <= COAP_DELETE)
terencez 0:f9d13e09cf11 221 {
terencez 0:f9d13e09cf11 222 uint32_t block_num = 0;
terencez 0:f9d13e09cf11 223 uint16_t block_size = REST_MAX_CHUNK_SIZE;
terencez 0:f9d13e09cf11 224 uint32_t block_offset = 0;
terence zhang 3:a280069151ac 225 int64_t new_offset = 0;
terencez 0:f9d13e09cf11 226
terencez 0:f9d13e09cf11 227 /* prepare response */
terence zhang 3:a280069151ac 228 if (message->type == COAP_TYPE_CON)
terencez 0:f9d13e09cf11 229 {
terencez 0:f9d13e09cf11 230 /* Reliable CON requests are answered with an ACK. */
terence zhang 3:a280069151ac 231 coap_init_message(response, COAP_TYPE_ACK, COAP_205_CONTENT, message->mid);
terencez 0:f9d13e09cf11 232 }
terencez 0:f9d13e09cf11 233 else
terencez 0:f9d13e09cf11 234 {
terencez 0:f9d13e09cf11 235 /* Unreliable NON requests are answered with a NON as well. */
terence zhang 3:a280069151ac 236 coap_init_message(response, COAP_TYPE_NON, COAP_205_CONTENT, contextP->nextMID++);
terencez 0:f9d13e09cf11 237 }
terencez 0:f9d13e09cf11 238
terencez 0:f9d13e09cf11 239 /* mirror token */
terencez 0:f9d13e09cf11 240 if (message->token_len)
terencez 0:f9d13e09cf11 241 {
terencez 0:f9d13e09cf11 242 coap_set_header_token(response, message->token, message->token_len);
terencez 0:f9d13e09cf11 243 }
terencez 0:f9d13e09cf11 244
terencez 0:f9d13e09cf11 245 /* get offset for blockwise transfers */
terencez 0:f9d13e09cf11 246 if (coap_get_header_block2(message, &block_num, NULL, &block_size, &block_offset))
terencez 0:f9d13e09cf11 247 {
terence zhang 3:a280069151ac 248 LOG_ARG("Blockwise: block request %u (%u/%u) @ %u bytes", block_num, block_size, REST_MAX_CHUNK_SIZE, block_offset);
terencez 0:f9d13e09cf11 249 block_size = MIN(block_size, REST_MAX_CHUNK_SIZE);
terencez 0:f9d13e09cf11 250 new_offset = block_offset;
terencez 0:f9d13e09cf11 251 }
terencez 0:f9d13e09cf11 252
terence zhang 3:a280069151ac 253 /* handle block1 option */
terence zhang 3:a280069151ac 254 if (IS_OPTION(message, COAP_OPTION_BLOCK1))
terence zhang 3:a280069151ac 255 {
terence zhang 3:a280069151ac 256 #ifdef LWM2M_CLIENT_MODE
terence zhang 3:a280069151ac 257 // get server
terence zhang 3:a280069151ac 258 lwm2m_server_t * serverP;
terence zhang 3:a280069151ac 259 serverP = utils_findServer(contextP, fromSessionH);
terence zhang 3:a280069151ac 260 #ifdef LWM2M_BOOTSTRAP
terence zhang 3:a280069151ac 261 if (serverP == NULL)
terence zhang 3:a280069151ac 262 {
terence zhang 3:a280069151ac 263 serverP = utils_findBootstrapServer(contextP, fromSessionH);
terence zhang 3:a280069151ac 264 }
terence zhang 3:a280069151ac 265 #endif
terence zhang 3:a280069151ac 266 if (serverP == NULL)
terence zhang 3:a280069151ac 267 {
terence zhang 3:a280069151ac 268 coap_error_code = COAP_500_INTERNAL_SERVER_ERROR;
terence zhang 3:a280069151ac 269 }
terence zhang 3:a280069151ac 270 else
terence zhang 3:a280069151ac 271 {
terence zhang 3:a280069151ac 272 uint32_t block1_num;
terence zhang 3:a280069151ac 273 uint8_t block1_more;
terence zhang 3:a280069151ac 274 uint16_t block1_size;
terence zhang 3:a280069151ac 275 uint8_t * complete_buffer = NULL;
terence zhang 3:a280069151ac 276 size_t complete_buffer_size;
terence zhang 3:a280069151ac 277
terence zhang 3:a280069151ac 278 // parse block1 header
terence zhang 3:a280069151ac 279 coap_get_header_block1(message, &block1_num, &block1_more, &block1_size, NULL);
terence zhang 3:a280069151ac 280 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 281
terence zhang 3:a280069151ac 282 // handle block 1
terence zhang 3:a280069151ac 283 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 284
terence zhang 3:a280069151ac 285 // if payload is complete, replace it in the coap message.
terence zhang 3:a280069151ac 286 if (coap_error_code == NO_ERROR)
terence zhang 3:a280069151ac 287 {
terence zhang 3:a280069151ac 288 message->payload = complete_buffer;
terence zhang 3:a280069151ac 289 message->payload_len = complete_buffer_size;
terence zhang 3:a280069151ac 290 }
terence zhang 3:a280069151ac 291 else if (coap_error_code == COAP_231_CONTINUE)
terence zhang 3:a280069151ac 292 {
terence zhang 3:a280069151ac 293 block1_size = MIN(block1_size, REST_MAX_CHUNK_SIZE);
terence zhang 3:a280069151ac 294 coap_set_header_block1(response,block1_num, block1_more,block1_size);
terence zhang 3:a280069151ac 295 }
terence zhang 3:a280069151ac 296 }
terence zhang 3:a280069151ac 297 #else
terence zhang 3:a280069151ac 298 coap_error_code = COAP_501_NOT_IMPLEMENTED;
terence zhang 3:a280069151ac 299 #endif
terence zhang 3:a280069151ac 300 }
terence zhang 3:a280069151ac 301 if (coap_error_code == NO_ERROR)
terence zhang 3:a280069151ac 302 {
terence zhang 3:a280069151ac 303 coap_error_code = handle_request(contextP, fromSessionH, message, response);
terence zhang 3:a280069151ac 304 }
terencez 0:f9d13e09cf11 305 if (coap_error_code==NO_ERROR)
terencez 0:f9d13e09cf11 306 {
terence zhang 3:a280069151ac 307 if ( IS_OPTION(message, COAP_OPTION_BLOCK2) )
terencez 0:f9d13e09cf11 308 {
terencez 0:f9d13e09cf11 309 /* unchanged new_offset indicates that resource is unaware of blockwise transfer */
terencez 0:f9d13e09cf11 310 if (new_offset==block_offset)
terencez 0:f9d13e09cf11 311 {
terence zhang 3:a280069151ac 312 LOG_ARG("Blockwise: unaware resource with payload length %u/%u", response->payload_len, block_size);
terencez 0:f9d13e09cf11 313 if (block_offset >= response->payload_len)
terencez 0:f9d13e09cf11 314 {
terence zhang 3:a280069151ac 315 LOG("handle_incoming_data(): block_offset >= response->payload_len");
terencez 0:f9d13e09cf11 316
terence zhang 3:a280069151ac 317 response->code = COAP_402_BAD_OPTION;
terencez 0:f9d13e09cf11 318 coap_set_payload(response, "BlockOutOfScope", 15); /* a const char str[] and sizeof(str) produces larger code size */
terencez 0:f9d13e09cf11 319 }
terencez 0:f9d13e09cf11 320 else
terencez 0:f9d13e09cf11 321 {
terencez 0:f9d13e09cf11 322 coap_set_header_block2(response, block_num, response->payload_len - block_offset > block_size, block_size);
terencez 0:f9d13e09cf11 323 coap_set_payload(response, response->payload+block_offset, MIN(response->payload_len - block_offset, block_size));
terencez 0:f9d13e09cf11 324 } /* if (valid offset) */
terencez 0:f9d13e09cf11 325 }
terencez 0:f9d13e09cf11 326 else
terencez 0:f9d13e09cf11 327 {
terencez 0:f9d13e09cf11 328 /* resource provides chunk-wise data */
terence zhang 3:a280069151ac 329 LOG_ARG("Blockwise: blockwise resource, new offset %d", (int) new_offset);
terencez 0:f9d13e09cf11 330 coap_set_header_block2(response, block_num, new_offset!=-1 || response->payload_len > block_size, block_size);
terencez 0:f9d13e09cf11 331 if (response->payload_len > block_size) coap_set_payload(response, response->payload, block_size);
terencez 0:f9d13e09cf11 332 } /* if (resource aware of blockwise) */
terencez 0:f9d13e09cf11 333 }
terencez 0:f9d13e09cf11 334 else if (new_offset!=0)
terencez 0:f9d13e09cf11 335 {
terence zhang 3:a280069151ac 336 LOG_ARG("Blockwise: no block option for blockwise resource, using block size %u", REST_MAX_CHUNK_SIZE);
terencez 0:f9d13e09cf11 337
terencez 0:f9d13e09cf11 338 coap_set_header_block2(response, 0, new_offset!=-1, REST_MAX_CHUNK_SIZE);
terencez 0:f9d13e09cf11 339 coap_set_payload(response, response->payload, MIN(response->payload_len, REST_MAX_CHUNK_SIZE));
terencez 0:f9d13e09cf11 340 } /* if (blockwise request) */
terencez 0:f9d13e09cf11 341
terencez 0:f9d13e09cf11 342 coap_error_code = message_send(contextP, response, fromSessionH);
terencez 0:f9d13e09cf11 343
terencez 0:f9d13e09cf11 344 lwm2m_free(response->payload);
terencez 0:f9d13e09cf11 345 response->payload = NULL;
terencez 0:f9d13e09cf11 346 response->payload_len = 0;
terencez 0:f9d13e09cf11 347 }
terence zhang 3:a280069151ac 348 else if (coap_error_code != COAP_IGNORE)
terencez 0:f9d13e09cf11 349 {
terence zhang 3:a280069151ac 350 if (1 == coap_set_status_code(response, coap_error_code))
terence zhang 3:a280069151ac 351 {
terence zhang 3:a280069151ac 352 coap_error_code = message_send(contextP, response, fromSessionH);
terence zhang 3:a280069151ac 353 }
terencez 0:f9d13e09cf11 354 }
terencez 0:f9d13e09cf11 355 }
terencez 0:f9d13e09cf11 356 else
terencez 0:f9d13e09cf11 357 {
terencez 0:f9d13e09cf11 358 /* Responses */
terence zhang 3:a280069151ac 359 switch (message->type)
terencez 0:f9d13e09cf11 360 {
terence zhang 3:a280069151ac 361 case COAP_TYPE_NON:
terence zhang 3:a280069151ac 362 case COAP_TYPE_CON:
terence zhang 3:a280069151ac 363 {
terence zhang 3:a280069151ac 364 bool done = transaction_handleResponse(contextP, fromSessionH, message, response);
terencez 0:f9d13e09cf11 365
terencez 0:f9d13e09cf11 366 #ifdef LWM2M_SERVER_MODE
terence zhang 3:a280069151ac 367 if (!done && IS_OPTION(message, COAP_OPTION_OBSERVE) &&
terence zhang 3:a280069151ac 368 ((message->code == COAP_204_CHANGED) || (message->code == COAP_205_CONTENT)))
terence zhang 3:a280069151ac 369 {
terence zhang 3:a280069151ac 370 done = observe_handleNotify(contextP, fromSessionH, message, response);
terence zhang 3:a280069151ac 371 }
terencez 0:f9d13e09cf11 372 #endif
terence zhang 3:a280069151ac 373 if (!done && message->type == COAP_TYPE_CON )
terence zhang 3:a280069151ac 374 {
terence zhang 3:a280069151ac 375 coap_init_message(response, COAP_TYPE_ACK, 0, message->mid);
terence zhang 3:a280069151ac 376 coap_error_code = message_send(contextP, response, fromSessionH);
terence zhang 3:a280069151ac 377 }
terence zhang 3:a280069151ac 378 }
terence zhang 3:a280069151ac 379 break;
terence zhang 3:a280069151ac 380
terence zhang 3:a280069151ac 381 case COAP_TYPE_RST:
terence zhang 3:a280069151ac 382 /* Cancel possible subscriptions. */
terence zhang 3:a280069151ac 383 handle_reset(contextP, fromSessionH, message);
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 case COAP_TYPE_ACK:
terence zhang 3:a280069151ac 388 transaction_handleResponse(contextP, fromSessionH, message, NULL);
terence zhang 3:a280069151ac 389 break;
terence zhang 3:a280069151ac 390
terence zhang 3:a280069151ac 391 default:
terence zhang 3:a280069151ac 392 break;
terencez 0:f9d13e09cf11 393 }
terencez 0:f9d13e09cf11 394 } /* Request or Response */
terencez 0:f9d13e09cf11 395 coap_free_header(message);
terencez 0:f9d13e09cf11 396 } /* if (parsed correctly) */
terencez 0:f9d13e09cf11 397 else
terencez 0:f9d13e09cf11 398 {
terence zhang 3:a280069151ac 399 LOG_ARG("Message parsing failed %u.%2u", coap_error_code >> 5, coap_error_code & 0x1F);
terencez 0:f9d13e09cf11 400 }
terencez 0:f9d13e09cf11 401
terence zhang 3:a280069151ac 402 if (coap_error_code != NO_ERROR && coap_error_code != COAP_IGNORE)
terencez 0:f9d13e09cf11 403 {
terence zhang 3:a280069151ac 404 LOG_ARG("ERROR %u: %s", coap_error_code, coap_error_message);
terencez 0:f9d13e09cf11 405
terencez 0:f9d13e09cf11 406 /* Set to sendable error code. */
terencez 0:f9d13e09cf11 407 if (coap_error_code >= 192)
terencez 0:f9d13e09cf11 408 {
terence zhang 3:a280069151ac 409 coap_error_code = COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:f9d13e09cf11 410 }
terencez 0:f9d13e09cf11 411 /* Reuse input buffer for error message. */
terencez 0:f9d13e09cf11 412 coap_init_message(message, COAP_TYPE_ACK, coap_error_code, message->mid);
terencez 0:f9d13e09cf11 413 coap_set_payload(message, coap_error_message, strlen(coap_error_message));
terencez 0:f9d13e09cf11 414 message_send(contextP, message, fromSessionH);
terencez 0:f9d13e09cf11 415 }
terencez 0:f9d13e09cf11 416 }
terencez 0:f9d13e09cf11 417
terencez 0:f9d13e09cf11 418
terencez 0:f9d13e09cf11 419 coap_status_t message_send(lwm2m_context_t * contextP,
terencez 0:f9d13e09cf11 420 coap_packet_t * message,
terencez 0:f9d13e09cf11 421 void * sessionH)
terencez 0:f9d13e09cf11 422 {
terence zhang 3:a280069151ac 423 coap_status_t result = COAP_500_INTERNAL_SERVER_ERROR;
terence zhang 3:a280069151ac 424 uint8_t * pktBuffer;
terencez 0:f9d13e09cf11 425 size_t pktBufferLen = 0;
terence zhang 3:a280069151ac 426 size_t allocLen;
terence zhang 3:a280069151ac 427
terence zhang 3:a280069151ac 428 LOG("Entering");
terence zhang 3:a280069151ac 429 allocLen = coap_serialize_get_size(message);
terence zhang 3:a280069151ac 430 LOG_ARG("Size to allocate: %d", allocLen);
terence zhang 3:a280069151ac 431 if (allocLen == 0) return COAP_500_INTERNAL_SERVER_ERROR;
terencez 0:f9d13e09cf11 432
terence zhang 3:a280069151ac 433 pktBuffer = (uint8_t *)lwm2m_malloc(allocLen);
terence zhang 3:a280069151ac 434 if (pktBuffer != NULL)
terencez 0:f9d13e09cf11 435 {
terence zhang 3:a280069151ac 436 pktBufferLen = coap_serialize_message(message, pktBuffer);
terence zhang 3:a280069151ac 437 LOG_ARG("coap_serialize_message() returned %d", pktBufferLen);
terence zhang 3:a280069151ac 438 if (0 != pktBufferLen)
terence zhang 3:a280069151ac 439 {
terence zhang 3:a280069151ac 440 result = lwm2m_buffer_send(sessionH, pktBuffer, pktBufferLen, contextP->userData);
terence zhang 3:a280069151ac 441 }
terence zhang 3:a280069151ac 442 lwm2m_free(pktBuffer);
terencez 0:f9d13e09cf11 443 }
terencez 0:f9d13e09cf11 444
terencez 0:f9d13e09cf11 445 return result;
terencez 0:f9d13e09cf11 446 }
terencez 0:f9d13e09cf11 447