Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
wakaama/packet.c@0:f9d13e09cf11, 2017-04-24 (annotated)
- Committer:
- terencez
- Date:
- Mon Apr 24 23:03:31 2017 +0000
- Revision:
- 0:f9d13e09cf11
- Child:
- 3:a280069151ac
The first compiled submit.
Who changed what in which revision?
User | Revision | Line number | New 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 |
terencez | 0:f9d13e09cf11 | 20 | * |
terencez | 0:f9d13e09cf11 | 21 | *******************************************************************************/ |
terencez | 0:f9d13e09cf11 | 22 | |
terencez | 0:f9d13e09cf11 | 23 | /* |
terencez | 0:f9d13e09cf11 | 24 | Copyright (c) 2013, 2014 Intel Corporation |
terencez | 0:f9d13e09cf11 | 25 | |
terencez | 0:f9d13e09cf11 | 26 | Redistribution and use in source and binary forms, with or without modification, |
terencez | 0:f9d13e09cf11 | 27 | are permitted provided that the following conditions are met: |
terencez | 0:f9d13e09cf11 | 28 | |
terencez | 0:f9d13e09cf11 | 29 | * Redistributions of source code must retain the above copyright notice, |
terencez | 0:f9d13e09cf11 | 30 | this list of conditions and the following disclaimer. |
terencez | 0:f9d13e09cf11 | 31 | * Redistributions in binary form must reproduce the above copyright notice, |
terencez | 0:f9d13e09cf11 | 32 | this list of conditions and the following disclaimer in the documentation |
terencez | 0:f9d13e09cf11 | 33 | and/or other materials provided with the distribution. |
terencez | 0:f9d13e09cf11 | 34 | * Neither the name of Intel Corporation nor the names of its contributors |
terencez | 0:f9d13e09cf11 | 35 | may be used to endorse or promote products derived from this software |
terencez | 0:f9d13e09cf11 | 36 | without specific prior written permission. |
terencez | 0:f9d13e09cf11 | 37 | |
terencez | 0:f9d13e09cf11 | 38 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
terencez | 0:f9d13e09cf11 | 39 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
terencez | 0:f9d13e09cf11 | 40 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
terencez | 0:f9d13e09cf11 | 41 | IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
terencez | 0:f9d13e09cf11 | 42 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
terencez | 0:f9d13e09cf11 | 43 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
terencez | 0:f9d13e09cf11 | 44 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
terencez | 0:f9d13e09cf11 | 45 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
terencez | 0:f9d13e09cf11 | 46 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
terencez | 0:f9d13e09cf11 | 47 | THE POSSIBILITY OF SUCH DAMAGE. |
terencez | 0:f9d13e09cf11 | 48 | |
terencez | 0:f9d13e09cf11 | 49 | David Navarro <david.navarro@intel.com> |
terencez | 0:f9d13e09cf11 | 50 | |
terencez | 0:f9d13e09cf11 | 51 | */ |
terencez | 0:f9d13e09cf11 | 52 | |
terencez | 0:f9d13e09cf11 | 53 | /* |
terencez | 0:f9d13e09cf11 | 54 | Contains code snippets which are: |
terencez | 0:f9d13e09cf11 | 55 | |
terencez | 0:f9d13e09cf11 | 56 | * Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich |
terencez | 0:f9d13e09cf11 | 57 | * All rights reserved. |
terencez | 0:f9d13e09cf11 | 58 | * |
terencez | 0:f9d13e09cf11 | 59 | * Redistribution and use in source and binary forms, with or without |
terencez | 0:f9d13e09cf11 | 60 | * modification, are permitted provided that the following conditions |
terencez | 0:f9d13e09cf11 | 61 | * are met: |
terencez | 0:f9d13e09cf11 | 62 | * 1. Redistributions of source code must retain the above copyright |
terencez | 0:f9d13e09cf11 | 63 | * notice, this list of conditions and the following disclaimer. |
terencez | 0:f9d13e09cf11 | 64 | * 2. Redistributions in binary form must reproduce the above copyright |
terencez | 0:f9d13e09cf11 | 65 | * notice, this list of conditions and the following disclaimer in the |
terencez | 0:f9d13e09cf11 | 66 | * documentation and/or other materials provided with the distribution. |
terencez | 0:f9d13e09cf11 | 67 | * 3. Neither the name of the Institute nor the names of its contributors |
terencez | 0:f9d13e09cf11 | 68 | * may be used to endorse or promote products derived from this software |
terencez | 0:f9d13e09cf11 | 69 | * without specific prior written permission. |
terencez | 0:f9d13e09cf11 | 70 | * |
terencez | 0:f9d13e09cf11 | 71 | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
terencez | 0:f9d13e09cf11 | 72 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
terencez | 0:f9d13e09cf11 | 73 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
terencez | 0:f9d13e09cf11 | 74 | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
terencez | 0:f9d13e09cf11 | 75 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
terencez | 0:f9d13e09cf11 | 76 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
terencez | 0:f9d13e09cf11 | 77 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
terencez | 0:f9d13e09cf11 | 78 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
terencez | 0:f9d13e09cf11 | 79 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
terencez | 0:f9d13e09cf11 | 80 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
terencez | 0:f9d13e09cf11 | 81 | * SUCH DAMAGE. |
terencez | 0:f9d13e09cf11 | 82 | |
terencez | 0:f9d13e09cf11 | 83 | */ |
terencez | 0:f9d13e09cf11 | 84 | |
terencez | 0:f9d13e09cf11 | 85 | |
terencez | 0:f9d13e09cf11 | 86 | #include "internals.h" |
terencez | 0:f9d13e09cf11 | 87 | |
terencez | 0:f9d13e09cf11 | 88 | #include <stdlib.h> |
terencez | 0:f9d13e09cf11 | 89 | #include <string.h> |
terencez | 0:f9d13e09cf11 | 90 | |
terencez | 0:f9d13e09cf11 | 91 | #include <stdio.h> |
terencez | 0:f9d13e09cf11 | 92 | |
terencez | 0:f9d13e09cf11 | 93 | |
terencez | 0:f9d13e09cf11 | 94 | static void handle_reset(lwm2m_context_t * contextP, |
terencez | 0:f9d13e09cf11 | 95 | void * fromSessionH, |
terencez | 0:f9d13e09cf11 | 96 | coap_packet_t * message) |
terencez | 0:f9d13e09cf11 | 97 | { |
terencez | 0:f9d13e09cf11 | 98 | #ifdef LWM2M_CLIENT_MODE |
terencez | 0:f9d13e09cf11 | 99 | cancel_observe(contextP, message->mid, fromSessionH); |
terencez | 0:f9d13e09cf11 | 100 | #endif |
terencez | 0:f9d13e09cf11 | 101 | } |
terencez | 0:f9d13e09cf11 | 102 | |
terencez | 0:f9d13e09cf11 | 103 | static coap_status_t handle_request(lwm2m_context_t * contextP, |
terencez | 0:f9d13e09cf11 | 104 | void * fromSessionH, |
terencez | 0:f9d13e09cf11 | 105 | coap_packet_t * message, |
terencez | 0:f9d13e09cf11 | 106 | coap_packet_t * response) |
terencez | 0:f9d13e09cf11 | 107 | { |
terencez | 0:f9d13e09cf11 | 108 | lwm2m_uri_t * uriP; |
terencez | 0:f9d13e09cf11 | 109 | coap_status_t result = NOT_FOUND_4_04; |
terencez | 0:f9d13e09cf11 | 110 | |
terencez | 0:f9d13e09cf11 | 111 | uriP = lwm2m_decode_uri(message->uri_path); |
terencez | 0:f9d13e09cf11 | 112 | if (uriP == NULL) return BAD_REQUEST_4_00; |
terencez | 0:f9d13e09cf11 | 113 | |
terencez | 0:f9d13e09cf11 | 114 | switch(uriP->flag & LWM2M_URI_MASK_TYPE) |
terencez | 0:f9d13e09cf11 | 115 | { |
terencez | 0:f9d13e09cf11 | 116 | #ifdef LWM2M_CLIENT_MODE |
terencez | 0:f9d13e09cf11 | 117 | case LWM2M_URI_FLAG_DM: |
terencez | 0:f9d13e09cf11 | 118 | // TODO: Authentify server |
terencez | 0:f9d13e09cf11 | 119 | result = handle_dm_request(contextP, uriP, fromSessionH, message, response); |
terencez | 0:f9d13e09cf11 | 120 | break; |
terencez | 0:f9d13e09cf11 | 121 | |
terencez | 0:f9d13e09cf11 | 122 | case LWM2M_URI_FLAG_BOOTSTRAP: |
terencez | 0:f9d13e09cf11 | 123 | result = NOT_IMPLEMENTED_5_01; |
terencez | 0:f9d13e09cf11 | 124 | break; |
terencez | 0:f9d13e09cf11 | 125 | #endif |
terencez | 0:f9d13e09cf11 | 126 | |
terencez | 0:f9d13e09cf11 | 127 | #ifdef LWM2M_SERVER_MODE |
terencez | 0:f9d13e09cf11 | 128 | case LWM2M_URI_FLAG_REGISTRATION: |
terencez | 0:f9d13e09cf11 | 129 | result = handle_registration_request(contextP, uriP, fromSessionH, message, response); |
terencez | 0:f9d13e09cf11 | 130 | break; |
terencez | 0:f9d13e09cf11 | 131 | #endif |
terencez | 0:f9d13e09cf11 | 132 | default: |
terencez | 0:f9d13e09cf11 | 133 | result = BAD_REQUEST_4_00; |
terencez | 0:f9d13e09cf11 | 134 | break; |
terencez | 0:f9d13e09cf11 | 135 | } |
terencez | 0:f9d13e09cf11 | 136 | |
terencez | 0:f9d13e09cf11 | 137 | coap_set_status_code(response, result); |
terencez | 0:f9d13e09cf11 | 138 | |
terencez | 0:f9d13e09cf11 | 139 | if (result < BAD_REQUEST_4_00) |
terencez | 0:f9d13e09cf11 | 140 | { |
terencez | 0:f9d13e09cf11 | 141 | result = NO_ERROR; |
terencez | 0:f9d13e09cf11 | 142 | } |
terencez | 0:f9d13e09cf11 | 143 | |
terencez | 0:f9d13e09cf11 | 144 | lwm2m_free( uriP); |
terencez | 0:f9d13e09cf11 | 145 | return result; |
terencez | 0:f9d13e09cf11 | 146 | } |
terencez | 0:f9d13e09cf11 | 147 | |
terencez | 0:f9d13e09cf11 | 148 | /* This function is an adaptation of function coap_receive() from Erbium's er-coap-13-engine.c. |
terencez | 0:f9d13e09cf11 | 149 | * Erbium is Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich |
terencez | 0:f9d13e09cf11 | 150 | * All rights reserved. |
terencez | 0:f9d13e09cf11 | 151 | */ |
terencez | 0:f9d13e09cf11 | 152 | void lwm2m_handle_packet(lwm2m_context_t * contextP, |
terencez | 0:f9d13e09cf11 | 153 | uint8_t * buffer, |
terencez | 0:f9d13e09cf11 | 154 | int length, |
terencez | 0:f9d13e09cf11 | 155 | void * fromSessionH) |
terencez | 0:f9d13e09cf11 | 156 | { |
terencez | 0:f9d13e09cf11 | 157 | coap_status_t coap_error_code = NO_ERROR; |
terencez | 0:f9d13e09cf11 | 158 | static coap_packet_t message[1]; |
terencez | 0:f9d13e09cf11 | 159 | static coap_packet_t response[1]; |
terencez | 0:f9d13e09cf11 | 160 | |
terencez | 0:f9d13e09cf11 | 161 | coap_error_code = coap_parse_message(message, buffer, (uint16_t)length); |
terencez | 0:f9d13e09cf11 | 162 | if (coap_error_code==NO_ERROR) |
terencez | 0:f9d13e09cf11 | 163 | { |
terencez | 0:f9d13e09cf11 | 164 | LOG(" Parsed: ver %u, type %u, tkl %u, code %u, mid %u\r\n", message->version, message->type, message->token_len, message->code, message->mid); |
terencez | 0:f9d13e09cf11 | 165 | LOG(" Payload: %.*s\r\n\n", message->payload_len, message->payload); |
terencez | 0:f9d13e09cf11 | 166 | |
terencez | 0:f9d13e09cf11 | 167 | if (message->code >= COAP_GET && message->code <= COAP_DELETE) |
terencez | 0:f9d13e09cf11 | 168 | { |
terencez | 0:f9d13e09cf11 | 169 | uint32_t block_num = 0; |
terencez | 0:f9d13e09cf11 | 170 | uint16_t block_size = REST_MAX_CHUNK_SIZE; |
terencez | 0:f9d13e09cf11 | 171 | uint32_t block_offset = 0; |
terencez | 0:f9d13e09cf11 | 172 | int32_t new_offset = 0; |
terencez | 0:f9d13e09cf11 | 173 | |
terencez | 0:f9d13e09cf11 | 174 | /* prepare response */ |
terencez | 0:f9d13e09cf11 | 175 | if (message->type==COAP_TYPE_CON) |
terencez | 0:f9d13e09cf11 | 176 | { |
terencez | 0:f9d13e09cf11 | 177 | /* Reliable CON requests are answered with an ACK. */ |
terencez | 0:f9d13e09cf11 | 178 | coap_init_message(response, COAP_TYPE_ACK, CONTENT_2_05, message->mid); |
terencez | 0:f9d13e09cf11 | 179 | } |
terencez | 0:f9d13e09cf11 | 180 | else |
terencez | 0:f9d13e09cf11 | 181 | { |
terencez | 0:f9d13e09cf11 | 182 | /* Unreliable NON requests are answered with a NON as well. */ |
terencez | 0:f9d13e09cf11 | 183 | coap_init_message(response, COAP_TYPE_NON, CONTENT_2_05, contextP->nextMID++); |
terencez | 0:f9d13e09cf11 | 184 | } |
terencez | 0:f9d13e09cf11 | 185 | |
terencez | 0:f9d13e09cf11 | 186 | /* mirror token */ |
terencez | 0:f9d13e09cf11 | 187 | if (message->token_len) |
terencez | 0:f9d13e09cf11 | 188 | { |
terencez | 0:f9d13e09cf11 | 189 | coap_set_header_token(response, message->token, message->token_len); |
terencez | 0:f9d13e09cf11 | 190 | } |
terencez | 0:f9d13e09cf11 | 191 | |
terencez | 0:f9d13e09cf11 | 192 | /* get offset for blockwise transfers */ |
terencez | 0:f9d13e09cf11 | 193 | if (coap_get_header_block2(message, &block_num, NULL, &block_size, &block_offset)) |
terencez | 0:f9d13e09cf11 | 194 | { |
terencez | 0:f9d13e09cf11 | 195 | LOG("Blockwise: block request %u (%u/%u) @ %u bytes\n", block_num, block_size, REST_MAX_CHUNK_SIZE, block_offset); |
terencez | 0:f9d13e09cf11 | 196 | block_size = MIN(block_size, REST_MAX_CHUNK_SIZE); |
terencez | 0:f9d13e09cf11 | 197 | new_offset = block_offset; |
terencez | 0:f9d13e09cf11 | 198 | } |
terencez | 0:f9d13e09cf11 | 199 | |
terencez | 0:f9d13e09cf11 | 200 | coap_error_code = handle_request(contextP, fromSessionH, message, response); |
terencez | 0:f9d13e09cf11 | 201 | if (coap_error_code==NO_ERROR) |
terencez | 0:f9d13e09cf11 | 202 | { |
terencez | 0:f9d13e09cf11 | 203 | /* Apply blockwise transfers. */ |
terencez | 0:f9d13e09cf11 | 204 | if ( IS_OPTION(message, COAP_OPTION_BLOCK1) && response->code<BAD_REQUEST_4_00 && !IS_OPTION(response, COAP_OPTION_BLOCK1) ) |
terencez | 0:f9d13e09cf11 | 205 | { |
terencez | 0:f9d13e09cf11 | 206 | LOG("Block1 NOT IMPLEMENTED\n"); |
terencez | 0:f9d13e09cf11 | 207 | |
terencez | 0:f9d13e09cf11 | 208 | coap_error_code = NOT_IMPLEMENTED_5_01; |
terencez | 0:f9d13e09cf11 | 209 | coap_error_message = "NoBlock1Support"; |
terencez | 0:f9d13e09cf11 | 210 | } |
terencez | 0:f9d13e09cf11 | 211 | else if ( IS_OPTION(message, COAP_OPTION_BLOCK2) ) |
terencez | 0:f9d13e09cf11 | 212 | { |
terencez | 0:f9d13e09cf11 | 213 | /* unchanged new_offset indicates that resource is unaware of blockwise transfer */ |
terencez | 0:f9d13e09cf11 | 214 | if (new_offset==block_offset) |
terencez | 0:f9d13e09cf11 | 215 | { |
terencez | 0:f9d13e09cf11 | 216 | LOG("Blockwise: unaware resource with payload length %u/%u\n", response->payload_len, block_size); |
terencez | 0:f9d13e09cf11 | 217 | if (block_offset >= response->payload_len) |
terencez | 0:f9d13e09cf11 | 218 | { |
terencez | 0:f9d13e09cf11 | 219 | LOG("handle_incoming_data(): block_offset >= response->payload_len\n"); |
terencez | 0:f9d13e09cf11 | 220 | |
terencez | 0:f9d13e09cf11 | 221 | response->code = BAD_OPTION_4_02; |
terencez | 0:f9d13e09cf11 | 222 | coap_set_payload(response, "BlockOutOfScope", 15); /* a const char str[] and sizeof(str) produces larger code size */ |
terencez | 0:f9d13e09cf11 | 223 | } |
terencez | 0:f9d13e09cf11 | 224 | else |
terencez | 0:f9d13e09cf11 | 225 | { |
terencez | 0:f9d13e09cf11 | 226 | coap_set_header_block2(response, block_num, response->payload_len - block_offset > block_size, block_size); |
terencez | 0:f9d13e09cf11 | 227 | coap_set_payload(response, response->payload+block_offset, MIN(response->payload_len - block_offset, block_size)); |
terencez | 0:f9d13e09cf11 | 228 | } /* if (valid offset) */ |
terencez | 0:f9d13e09cf11 | 229 | } |
terencez | 0:f9d13e09cf11 | 230 | else |
terencez | 0:f9d13e09cf11 | 231 | { |
terencez | 0:f9d13e09cf11 | 232 | /* resource provides chunk-wise data */ |
terencez | 0:f9d13e09cf11 | 233 | LOG("Blockwise: blockwise resource, new offset %d\n", new_offset); |
terencez | 0:f9d13e09cf11 | 234 | coap_set_header_block2(response, block_num, new_offset!=-1 || response->payload_len > block_size, block_size); |
terencez | 0:f9d13e09cf11 | 235 | if (response->payload_len > block_size) coap_set_payload(response, response->payload, block_size); |
terencez | 0:f9d13e09cf11 | 236 | } /* if (resource aware of blockwise) */ |
terencez | 0:f9d13e09cf11 | 237 | } |
terencez | 0:f9d13e09cf11 | 238 | else if (new_offset!=0) |
terencez | 0:f9d13e09cf11 | 239 | { |
terencez | 0:f9d13e09cf11 | 240 | LOG("Blockwise: no block option for blockwise resource, using block size %u\n", REST_MAX_CHUNK_SIZE); |
terencez | 0:f9d13e09cf11 | 241 | |
terencez | 0:f9d13e09cf11 | 242 | coap_set_header_block2(response, 0, new_offset!=-1, REST_MAX_CHUNK_SIZE); |
terencez | 0:f9d13e09cf11 | 243 | coap_set_payload(response, response->payload, MIN(response->payload_len, REST_MAX_CHUNK_SIZE)); |
terencez | 0:f9d13e09cf11 | 244 | } /* if (blockwise request) */ |
terencez | 0:f9d13e09cf11 | 245 | |
terencez | 0:f9d13e09cf11 | 246 | coap_error_code = message_send(contextP, response, fromSessionH); |
terencez | 0:f9d13e09cf11 | 247 | |
terencez | 0:f9d13e09cf11 | 248 | lwm2m_free(response->payload); |
terencez | 0:f9d13e09cf11 | 249 | response->payload = NULL; |
terencez | 0:f9d13e09cf11 | 250 | response->payload_len = 0; |
terencez | 0:f9d13e09cf11 | 251 | } |
terencez | 0:f9d13e09cf11 | 252 | else |
terencez | 0:f9d13e09cf11 | 253 | { |
terencez | 0:f9d13e09cf11 | 254 | coap_error_code = message_send(contextP, response, fromSessionH); |
terencez | 0:f9d13e09cf11 | 255 | } |
terencez | 0:f9d13e09cf11 | 256 | } |
terencez | 0:f9d13e09cf11 | 257 | else |
terencez | 0:f9d13e09cf11 | 258 | { |
terencez | 0:f9d13e09cf11 | 259 | /* Responses */ |
terencez | 0:f9d13e09cf11 | 260 | lwm2m_transaction_t * transaction; |
terencez | 0:f9d13e09cf11 | 261 | |
terencez | 0:f9d13e09cf11 | 262 | if (message->type==COAP_TYPE_ACK) |
terencez | 0:f9d13e09cf11 | 263 | { |
terencez | 0:f9d13e09cf11 | 264 | LOG("Received ACK\n"); |
terencez | 0:f9d13e09cf11 | 265 | } |
terencez | 0:f9d13e09cf11 | 266 | else if (message->type==COAP_TYPE_RST) |
terencez | 0:f9d13e09cf11 | 267 | { |
terencez | 0:f9d13e09cf11 | 268 | LOG("Received RST\n"); |
terencez | 0:f9d13e09cf11 | 269 | /* Cancel possible subscriptions. */ |
terencez | 0:f9d13e09cf11 | 270 | handle_reset(contextP, fromSessionH, message); |
terencez | 0:f9d13e09cf11 | 271 | } |
terencez | 0:f9d13e09cf11 | 272 | |
terencez | 0:f9d13e09cf11 | 273 | #ifdef LWM2M_SERVER_MODE |
terencez | 0:f9d13e09cf11 | 274 | if (message->code == COAP_204_CHANGED |
terencez | 0:f9d13e09cf11 | 275 | && IS_OPTION(message, COAP_OPTION_OBSERVE)) |
terencez | 0:f9d13e09cf11 | 276 | { |
terencez | 0:f9d13e09cf11 | 277 | handle_observe_notify(contextP, fromSessionH, message); |
terencez | 0:f9d13e09cf11 | 278 | } |
terencez | 0:f9d13e09cf11 | 279 | else |
terencez | 0:f9d13e09cf11 | 280 | #endif |
terencez | 0:f9d13e09cf11 | 281 | { |
terencez | 0:f9d13e09cf11 | 282 | transaction_handle_response(contextP, fromSessionH, message); |
terencez | 0:f9d13e09cf11 | 283 | } |
terencez | 0:f9d13e09cf11 | 284 | } /* Request or Response */ |
terencez | 0:f9d13e09cf11 | 285 | |
terencez | 0:f9d13e09cf11 | 286 | coap_free_header(message); |
terencez | 0:f9d13e09cf11 | 287 | |
terencez | 0:f9d13e09cf11 | 288 | } /* if (parsed correctly) */ |
terencez | 0:f9d13e09cf11 | 289 | else |
terencez | 0:f9d13e09cf11 | 290 | { |
terencez | 0:f9d13e09cf11 | 291 | LOG("Message parsing failed %d\r\n", coap_error_code); |
terencez | 0:f9d13e09cf11 | 292 | } |
terencez | 0:f9d13e09cf11 | 293 | |
terencez | 0:f9d13e09cf11 | 294 | if (coap_error_code != NO_ERROR) |
terencez | 0:f9d13e09cf11 | 295 | { |
terencez | 0:f9d13e09cf11 | 296 | LOG("ERROR %u: %s\n", coap_error_code, coap_error_message); |
terencez | 0:f9d13e09cf11 | 297 | |
terencez | 0:f9d13e09cf11 | 298 | /* Set to sendable error code. */ |
terencez | 0:f9d13e09cf11 | 299 | if (coap_error_code >= 192) |
terencez | 0:f9d13e09cf11 | 300 | { |
terencez | 0:f9d13e09cf11 | 301 | coap_error_code = INTERNAL_SERVER_ERROR_5_00; |
terencez | 0:f9d13e09cf11 | 302 | } |
terencez | 0:f9d13e09cf11 | 303 | /* Reuse input buffer for error message. */ |
terencez | 0:f9d13e09cf11 | 304 | coap_init_message(message, COAP_TYPE_ACK, coap_error_code, message->mid); |
terencez | 0:f9d13e09cf11 | 305 | coap_set_payload(message, coap_error_message, strlen(coap_error_message)); |
terencez | 0:f9d13e09cf11 | 306 | message_send(contextP, message, fromSessionH); |
terencez | 0:f9d13e09cf11 | 307 | } |
terencez | 0:f9d13e09cf11 | 308 | } |
terencez | 0:f9d13e09cf11 | 309 | |
terencez | 0:f9d13e09cf11 | 310 | |
terencez | 0:f9d13e09cf11 | 311 | coap_status_t message_send(lwm2m_context_t * contextP, |
terencez | 0:f9d13e09cf11 | 312 | coap_packet_t * message, |
terencez | 0:f9d13e09cf11 | 313 | void * sessionH) |
terencez | 0:f9d13e09cf11 | 314 | { |
terencez | 0:f9d13e09cf11 | 315 | coap_status_t result = INTERNAL_SERVER_ERROR_5_00; |
terencez | 0:f9d13e09cf11 | 316 | uint8_t pktBuffer[COAP_MAX_PACKET_SIZE+1]; |
terencez | 0:f9d13e09cf11 | 317 | size_t pktBufferLen = 0; |
terencez | 0:f9d13e09cf11 | 318 | |
terencez | 0:f9d13e09cf11 | 319 | pktBufferLen = coap_serialize_message(message, pktBuffer); |
terencez | 0:f9d13e09cf11 | 320 | if (0 != pktBufferLen) |
terencez | 0:f9d13e09cf11 | 321 | { |
terencez | 0:f9d13e09cf11 | 322 | result = contextP->bufferSendCallback(sessionH, pktBuffer, pktBufferLen, contextP->bufferSendUserData); |
terencez | 0:f9d13e09cf11 | 323 | } |
terencez | 0:f9d13e09cf11 | 324 | |
terencez | 0:f9d13e09cf11 | 325 | return result; |
terencez | 0:f9d13e09cf11 | 326 | } |
terencez | 0:f9d13e09cf11 | 327 |