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