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