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

Dependencies:   C12832 LM75B

Committer:
terencez
Date:
Thu May 11 12:40:25 2017 +0000
Revision:
16:31c387e94b6d
Parent:
15:d0f20339c1ad
Added a demo temperature object based on sensor LM75B.

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