pick up wakaama files from https://github.com/eclipse/wakaama

Committer:
terencez
Date:
Wed Apr 19 11:30:02 2017 +0000
Revision:
0:1fa43ab66921
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
terencez 0:1fa43ab66921 1 /*
terencez 0:1fa43ab66921 2 * Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
terencez 0:1fa43ab66921 3 * All rights reserved.
terencez 0:1fa43ab66921 4 *
terencez 0:1fa43ab66921 5 * Redistribution and use in source and binary forms, with or without
terencez 0:1fa43ab66921 6 * modification, are permitted provided that the following conditions
terencez 0:1fa43ab66921 7 * are met:
terencez 0:1fa43ab66921 8 * 1. Redistributions of source code must retain the above copyright
terencez 0:1fa43ab66921 9 * notice, this list of conditions and the following disclaimer.
terencez 0:1fa43ab66921 10 * 2. Redistributions in binary form must reproduce the above copyright
terencez 0:1fa43ab66921 11 * notice, this list of conditions and the following disclaimer in the
terencez 0:1fa43ab66921 12 * documentation and/or other materials provided with the distribution.
terencez 0:1fa43ab66921 13 * 3. Neither the name of the Institute nor the names of its contributors
terencez 0:1fa43ab66921 14 * may be used to endorse or promote products derived from this software
terencez 0:1fa43ab66921 15 * without specific prior written permission.
terencez 0:1fa43ab66921 16 *
terencez 0:1fa43ab66921 17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
terencez 0:1fa43ab66921 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
terencez 0:1fa43ab66921 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
terencez 0:1fa43ab66921 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
terencez 0:1fa43ab66921 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
terencez 0:1fa43ab66921 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
terencez 0:1fa43ab66921 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
terencez 0:1fa43ab66921 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
terencez 0:1fa43ab66921 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
terencez 0:1fa43ab66921 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
terencez 0:1fa43ab66921 27 * SUCH DAMAGE.
terencez 0:1fa43ab66921 28 *
terencez 0:1fa43ab66921 29 * This file is part of the Contiki operating system.
terencez 0:1fa43ab66921 30 */
terencez 0:1fa43ab66921 31
terencez 0:1fa43ab66921 32 /**
terencez 0:1fa43ab66921 33 * \file
terencez 0:1fa43ab66921 34 * An implementation of the Constrained Application Protocol (draft 12)
terencez 0:1fa43ab66921 35 * \author
terencez 0:1fa43ab66921 36 * Matthias Kovatsch <kovatsch@inf.ethz.ch>
terencez 0:1fa43ab66921 37 * \contributors
terencez 0:1fa43ab66921 38 * David Navarro, Intel Corporation - Adapt to usage in liblwm2m
terencez 0:1fa43ab66921 39 */
terencez 0:1fa43ab66921 40
terencez 0:1fa43ab66921 41
terencez 0:1fa43ab66921 42 #ifndef COAP_13_H_
terencez 0:1fa43ab66921 43 #define COAP_13_H_
terencez 0:1fa43ab66921 44
terencez 0:1fa43ab66921 45 #include <stdint.h>
terencez 0:1fa43ab66921 46 #include <stddef.h> /* for size_t */
terencez 0:1fa43ab66921 47
terencez 0:1fa43ab66921 48 /*
terencez 0:1fa43ab66921 49 * The maximum buffer size that is provided for resource responses and must be respected due to the limited IP buffer.
terencez 0:1fa43ab66921 50 * Larger data must be handled by the resource and will be sent chunk-wise through a TCP stream or CoAP blocks.
terencez 0:1fa43ab66921 51 */
terencez 0:1fa43ab66921 52 #ifndef REST_MAX_CHUNK_SIZE
terencez 0:1fa43ab66921 53 #define REST_MAX_CHUNK_SIZE 128
terencez 0:1fa43ab66921 54 #endif
terencez 0:1fa43ab66921 55
terencez 0:1fa43ab66921 56 #define COAP_DEFAULT_MAX_AGE 60
terencez 0:1fa43ab66921 57 #define COAP_RESPONSE_TIMEOUT 2
terencez 0:1fa43ab66921 58 #define COAP_MAX_RETRANSMIT 4
terencez 0:1fa43ab66921 59 #define COAP_ACK_RANDOM_FACTOR 1.5
terencez 0:1fa43ab66921 60
terencez 0:1fa43ab66921 61 #define COAP_MAX_TRANSMIT_WAIT ((COAP_RESPONSE_TIMEOUT * ( (1 << (COAP_MAX_RETRANSMIT + 1) ) - 1) * COAP_ACK_RANDOM_FACTOR))
terencez 0:1fa43ab66921 62
terencez 0:1fa43ab66921 63 #define COAP_HEADER_LEN 4 /* | version:0x03 type:0x0C tkl:0xF0 | code | mid:0x00FF | mid:0xFF00 | */
terencez 0:1fa43ab66921 64 #define COAP_ETAG_LEN 8 /* The maximum number of bytes for the ETag */
terencez 0:1fa43ab66921 65 #define COAP_TOKEN_LEN 8 /* The maximum number of bytes for the Token */
terencez 0:1fa43ab66921 66 #define COAP_MAX_ACCEPT_NUM 2 /* The maximum number of accept preferences to parse/store */
terencez 0:1fa43ab66921 67
terencez 0:1fa43ab66921 68 #define COAP_MAX_OPTION_HEADER_LEN 5
terencez 0:1fa43ab66921 69
terencez 0:1fa43ab66921 70 #define COAP_HEADER_VERSION_MASK 0xC0
terencez 0:1fa43ab66921 71 #define COAP_HEADER_VERSION_POSITION 6
terencez 0:1fa43ab66921 72 #define COAP_HEADER_TYPE_MASK 0x30
terencez 0:1fa43ab66921 73 #define COAP_HEADER_TYPE_POSITION 4
terencez 0:1fa43ab66921 74 #define COAP_HEADER_TOKEN_LEN_MASK 0x0F
terencez 0:1fa43ab66921 75 #define COAP_HEADER_TOKEN_LEN_POSITION 0
terencez 0:1fa43ab66921 76
terencez 0:1fa43ab66921 77 #define COAP_HEADER_OPTION_DELTA_MASK 0xF0
terencez 0:1fa43ab66921 78 #define COAP_HEADER_OPTION_SHORT_LENGTH_MASK 0x0F
terencez 0:1fa43ab66921 79
terencez 0:1fa43ab66921 80 /*
terencez 0:1fa43ab66921 81 * Conservative size limit, as not all options have to be set at the same time.
terencez 0:1fa43ab66921 82 */
terencez 0:1fa43ab66921 83 #ifndef COAP_MAX_HEADER_SIZE
terencez 0:1fa43ab66921 84 /* Hdr CoT Age Tag Obs Tok Blo strings */
terencez 0:1fa43ab66921 85 #define COAP_MAX_HEADER_SIZE (4 + 3 + 5 + 1+COAP_ETAG_LEN + 3 + 1+COAP_TOKEN_LEN + 4 + 30) /* 70 */
terencez 0:1fa43ab66921 86 #endif /* COAP_MAX_HEADER_SIZE */
terencez 0:1fa43ab66921 87
terencez 0:1fa43ab66921 88 #define COAP_MAX_PACKET_SIZE (COAP_MAX_HEADER_SIZE + REST_MAX_CHUNK_SIZE)
terencez 0:1fa43ab66921 89 /* 0/14 48 for IPv6 (28 for IPv4) */
terencez 0:1fa43ab66921 90 #if COAP_MAX_PACKET_SIZE > (UIP_BUFSIZE - UIP_LLH_LEN - UIP_IPUDPH_LEN)
terencez 0:1fa43ab66921 91 //#error "UIP_CONF_BUFFER_SIZE too small for REST_MAX_CHUNK_SIZE"
terencez 0:1fa43ab66921 92 #endif
terencez 0:1fa43ab66921 93
terencez 0:1fa43ab66921 94
terencez 0:1fa43ab66921 95 /* Bitmap for set options */
terencez 0:1fa43ab66921 96 enum { OPTION_MAP_SIZE = sizeof(uint8_t) * 8 };
terencez 0:1fa43ab66921 97 #define SET_OPTION(packet, opt) ((packet)->options[opt / OPTION_MAP_SIZE] |= 1 << (opt % OPTION_MAP_SIZE))
terencez 0:1fa43ab66921 98 #define IS_OPTION(packet, opt) ((packet)->options[opt / OPTION_MAP_SIZE] & (1 << (opt % OPTION_MAP_SIZE)))
terencez 0:1fa43ab66921 99
terencez 0:1fa43ab66921 100 #ifndef MIN
terencez 0:1fa43ab66921 101 #define MIN(a, b) ((a) < (b)? (a) : (b))
terencez 0:1fa43ab66921 102 #endif /* MIN */
terencez 0:1fa43ab66921 103
terencez 0:1fa43ab66921 104 /* CoAP message types */
terencez 0:1fa43ab66921 105 typedef enum {
terencez 0:1fa43ab66921 106 COAP_TYPE_CON, /* confirmables */
terencez 0:1fa43ab66921 107 COAP_TYPE_NON, /* non-confirmables */
terencez 0:1fa43ab66921 108 COAP_TYPE_ACK, /* acknowledgements */
terencez 0:1fa43ab66921 109 COAP_TYPE_RST /* reset */
terencez 0:1fa43ab66921 110 } coap_message_type_t;
terencez 0:1fa43ab66921 111
terencez 0:1fa43ab66921 112 /* CoAP request method codes */
terencez 0:1fa43ab66921 113 typedef enum {
terencez 0:1fa43ab66921 114 COAP_GET = 1,
terencez 0:1fa43ab66921 115 COAP_POST,
terencez 0:1fa43ab66921 116 COAP_PUT,
terencez 0:1fa43ab66921 117 COAP_DELETE
terencez 0:1fa43ab66921 118 } coap_method_t;
terencez 0:1fa43ab66921 119
terencez 0:1fa43ab66921 120 /* CoAP response codes */
terencez 0:1fa43ab66921 121 typedef enum {
terencez 0:1fa43ab66921 122 NO_ERROR = 0,
terencez 0:1fa43ab66921 123
terencez 0:1fa43ab66921 124 CREATED_2_01 = 65, /* CREATED */
terencez 0:1fa43ab66921 125 DELETED_2_02 = 66, /* DELETED */
terencez 0:1fa43ab66921 126 VALID_2_03 = 67, /* NOT_MODIFIED */
terencez 0:1fa43ab66921 127 CHANGED_2_04 = 68, /* CHANGED */
terencez 0:1fa43ab66921 128 CONTENT_2_05 = 69, /* OK */
terencez 0:1fa43ab66921 129
terencez 0:1fa43ab66921 130 BAD_REQUEST_4_00 = 128, /* BAD_REQUEST */
terencez 0:1fa43ab66921 131 UNAUTHORIZED_4_01 = 129, /* UNAUTHORIZED */
terencez 0:1fa43ab66921 132 BAD_OPTION_4_02 = 130, /* BAD_OPTION */
terencez 0:1fa43ab66921 133 FORBIDDEN_4_03 = 131, /* FORBIDDEN */
terencez 0:1fa43ab66921 134 NOT_FOUND_4_04 = 132, /* NOT_FOUND */
terencez 0:1fa43ab66921 135 METHOD_NOT_ALLOWED_4_05 = 133, /* METHOD_NOT_ALLOWED */
terencez 0:1fa43ab66921 136 NOT_ACCEPTABLE_4_06 = 134, /* NOT_ACCEPTABLE */
terencez 0:1fa43ab66921 137 PRECONDITION_FAILED_4_12 = 140, /* BAD_REQUEST */
terencez 0:1fa43ab66921 138 REQUEST_ENTITY_TOO_LARGE_4_13 = 141, /* REQUEST_ENTITY_TOO_LARGE */
terencez 0:1fa43ab66921 139 UNSUPPORTED_MEDIA_TYPE_4_15 = 143, /* UNSUPPORTED_MEDIA_TYPE */
terencez 0:1fa43ab66921 140
terencez 0:1fa43ab66921 141 INTERNAL_SERVER_ERROR_5_00 = 160, /* INTERNAL_SERVER_ERROR */
terencez 0:1fa43ab66921 142 NOT_IMPLEMENTED_5_01 = 161, /* NOT_IMPLEMENTED */
terencez 0:1fa43ab66921 143 BAD_GATEWAY_5_02 = 162, /* BAD_GATEWAY */
terencez 0:1fa43ab66921 144 SERVICE_UNAVAILABLE_5_03 = 163, /* SERVICE_UNAVAILABLE */
terencez 0:1fa43ab66921 145 GATEWAY_TIMEOUT_5_04 = 164, /* GATEWAY_TIMEOUT */
terencez 0:1fa43ab66921 146 PROXYING_NOT_SUPPORTED_5_05 = 165, /* PROXYING_NOT_SUPPORTED */
terencez 0:1fa43ab66921 147
terencez 0:1fa43ab66921 148 /* Erbium errors */
terencez 0:1fa43ab66921 149 MEMORY_ALLOCATION_ERROR = 192,
terencez 0:1fa43ab66921 150 PACKET_SERIALIZATION_ERROR,
terencez 0:1fa43ab66921 151
terencez 0:1fa43ab66921 152 /* Erbium hooks */
terencez 0:1fa43ab66921 153 MANUAL_RESPONSE
terencez 0:1fa43ab66921 154
terencez 0:1fa43ab66921 155 } coap_status_t;
terencez 0:1fa43ab66921 156
terencez 0:1fa43ab66921 157 /* CoAP header options */
terencez 0:1fa43ab66921 158 typedef enum {
terencez 0:1fa43ab66921 159 COAP_OPTION_IF_MATCH = 1, /* 0-8 B */
terencez 0:1fa43ab66921 160 COAP_OPTION_URI_HOST = 3, /* 1-255 B */
terencez 0:1fa43ab66921 161 COAP_OPTION_ETAG = 4, /* 1-8 B */
terencez 0:1fa43ab66921 162 COAP_OPTION_IF_NONE_MATCH = 5, /* 0 B */
terencez 0:1fa43ab66921 163 COAP_OPTION_OBSERVE = 6, /* 0-3 B */
terencez 0:1fa43ab66921 164 COAP_OPTION_URI_PORT = 7, /* 0-2 B */
terencez 0:1fa43ab66921 165 COAP_OPTION_LOCATION_PATH = 8, /* 0-255 B */
terencez 0:1fa43ab66921 166 COAP_OPTION_URI_PATH = 11, /* 0-255 B */
terencez 0:1fa43ab66921 167 COAP_OPTION_CONTENT_TYPE = 12, /* 0-2 B */
terencez 0:1fa43ab66921 168 COAP_OPTION_MAX_AGE = 14, /* 0-4 B */
terencez 0:1fa43ab66921 169 COAP_OPTION_URI_QUERY = 15, /* 0-270 B */
terencez 0:1fa43ab66921 170 COAP_OPTION_ACCEPT = 17, /* 0-2 B */
terencez 0:1fa43ab66921 171 COAP_OPTION_TOKEN = 19, /* 1-8 B */
terencez 0:1fa43ab66921 172 COAP_OPTION_LOCATION_QUERY = 20, /* 1-270 B */
terencez 0:1fa43ab66921 173 COAP_OPTION_BLOCK2 = 23, /* 1-3 B */
terencez 0:1fa43ab66921 174 COAP_OPTION_BLOCK1 = 27, /* 1-3 B */
terencez 0:1fa43ab66921 175 COAP_OPTION_SIZE = 28, /* 0-4 B */
terencez 0:1fa43ab66921 176 COAP_OPTION_PROXY_URI = 35, /* 1-270 B */
terencez 0:1fa43ab66921 177 OPTION_MAX_VALUE = 0xFFFF
terencez 0:1fa43ab66921 178 } coap_option_t;
terencez 0:1fa43ab66921 179
terencez 0:1fa43ab66921 180 /* CoAP Content-Types */
terencez 0:1fa43ab66921 181 typedef enum {
terencez 0:1fa43ab66921 182 TEXT_PLAIN = 0,
terencez 0:1fa43ab66921 183 TEXT_XML = 1, /* Indented types are not in the initial registry. */
terencez 0:1fa43ab66921 184 TEXT_CSV = 2,
terencez 0:1fa43ab66921 185 TEXT_HTML = 3,
terencez 0:1fa43ab66921 186 IMAGE_GIF = 21,
terencez 0:1fa43ab66921 187 IMAGE_JPEG = 22,
terencez 0:1fa43ab66921 188 IMAGE_PNG = 23,
terencez 0:1fa43ab66921 189 IMAGE_TIFF = 24,
terencez 0:1fa43ab66921 190 AUDIO_RAW = 25,
terencez 0:1fa43ab66921 191 VIDEO_RAW = 26,
terencez 0:1fa43ab66921 192 APPLICATION_LINK_FORMAT = 40,
terencez 0:1fa43ab66921 193 APPLICATION_XML = 41,
terencez 0:1fa43ab66921 194 APPLICATION_OCTET_STREAM = 42,
terencez 0:1fa43ab66921 195 APPLICATION_RDF_XML = 43,
terencez 0:1fa43ab66921 196 APPLICATION_SOAP_XML = 44,
terencez 0:1fa43ab66921 197 APPLICATION_ATOM_XML = 45,
terencez 0:1fa43ab66921 198 APPLICATION_XMPP_XML = 46,
terencez 0:1fa43ab66921 199 APPLICATION_EXI = 47,
terencez 0:1fa43ab66921 200 APPLICATION_FASTINFOSET = 48,
terencez 0:1fa43ab66921 201 APPLICATION_SOAP_FASTINFOSET = 49,
terencez 0:1fa43ab66921 202 APPLICATION_JSON = 50,
terencez 0:1fa43ab66921 203 APPLICATION_X_OBIX_BINARY = 51,
terencez 0:1fa43ab66921 204 CONTENT_MAX_VALUE = 0xFFFF
terencez 0:1fa43ab66921 205 } coap_content_type_t;
terencez 0:1fa43ab66921 206
terencez 0:1fa43ab66921 207 typedef struct _multi_option_t {
terencez 0:1fa43ab66921 208 struct _multi_option_t *next;
terencez 0:1fa43ab66921 209 uint8_t is_static;
terencez 0:1fa43ab66921 210 uint8_t len;
terencez 0:1fa43ab66921 211 uint8_t *data;
terencez 0:1fa43ab66921 212 } multi_option_t;
terencez 0:1fa43ab66921 213
terencez 0:1fa43ab66921 214 /* Parsed message struct */
terencez 0:1fa43ab66921 215 typedef struct {
terencez 0:1fa43ab66921 216 uint8_t *buffer; /* pointer to CoAP header / incoming packet buffer / memory to serialize packet */
terencez 0:1fa43ab66921 217
terencez 0:1fa43ab66921 218 uint8_t version;
terencez 0:1fa43ab66921 219 coap_message_type_t type;
terencez 0:1fa43ab66921 220 uint8_t code;
terencez 0:1fa43ab66921 221 uint16_t mid;
terencez 0:1fa43ab66921 222
terencez 0:1fa43ab66921 223 uint8_t options[COAP_OPTION_PROXY_URI / OPTION_MAP_SIZE + 1]; /* Bitmap to check if option is set */
terencez 0:1fa43ab66921 224
terencez 0:1fa43ab66921 225 coap_content_type_t content_type; /* Parse options once and store; allows setting options in random order */
terencez 0:1fa43ab66921 226 uint32_t max_age;
terencez 0:1fa43ab66921 227 size_t proxy_uri_len;
terencez 0:1fa43ab66921 228 const uint8_t *proxy_uri;
terencez 0:1fa43ab66921 229 uint8_t etag_len;
terencez 0:1fa43ab66921 230 uint8_t etag[COAP_ETAG_LEN];
terencez 0:1fa43ab66921 231 size_t uri_host_len;
terencez 0:1fa43ab66921 232 const uint8_t *uri_host;
terencez 0:1fa43ab66921 233 multi_option_t *location_path;
terencez 0:1fa43ab66921 234 uint16_t uri_port;
terencez 0:1fa43ab66921 235 size_t location_query_len;
terencez 0:1fa43ab66921 236 uint8_t *location_query;
terencez 0:1fa43ab66921 237 multi_option_t *uri_path;
terencez 0:1fa43ab66921 238 uint32_t observe;
terencez 0:1fa43ab66921 239 uint8_t token_len;
terencez 0:1fa43ab66921 240 uint8_t token[COAP_TOKEN_LEN];
terencez 0:1fa43ab66921 241 uint8_t accept_num;
terencez 0:1fa43ab66921 242 uint16_t accept[COAP_MAX_ACCEPT_NUM];
terencez 0:1fa43ab66921 243 uint8_t if_match_len;
terencez 0:1fa43ab66921 244 uint8_t if_match[COAP_ETAG_LEN];
terencez 0:1fa43ab66921 245 uint32_t block2_num;
terencez 0:1fa43ab66921 246 uint8_t block2_more;
terencez 0:1fa43ab66921 247 uint16_t block2_size;
terencez 0:1fa43ab66921 248 uint32_t block2_offset;
terencez 0:1fa43ab66921 249 uint32_t block1_num;
terencez 0:1fa43ab66921 250 uint8_t block1_more;
terencez 0:1fa43ab66921 251 uint16_t block1_size;
terencez 0:1fa43ab66921 252 uint32_t block1_offset;
terencez 0:1fa43ab66921 253 uint32_t size;
terencez 0:1fa43ab66921 254 multi_option_t *uri_query;
terencez 0:1fa43ab66921 255 uint8_t if_none_match;
terencez 0:1fa43ab66921 256
terencez 0:1fa43ab66921 257 uint16_t payload_len;
terencez 0:1fa43ab66921 258 uint8_t *payload;
terencez 0:1fa43ab66921 259
terencez 0:1fa43ab66921 260 } coap_packet_t;
terencez 0:1fa43ab66921 261
terencez 0:1fa43ab66921 262 /* Option format serialization*/
terencez 0:1fa43ab66921 263 #define COAP_SERIALIZE_INT_OPTION(number, field, text) \
terencez 0:1fa43ab66921 264 if (IS_OPTION(coap_pkt, number)) { \
terencez 0:1fa43ab66921 265 PRINTF(text" [%u]\n", coap_pkt->field); \
terencez 0:1fa43ab66921 266 option += coap_serialize_int_option(number, current_number, option, coap_pkt->field); \
terencez 0:1fa43ab66921 267 current_number = number; \
terencez 0:1fa43ab66921 268 }
terencez 0:1fa43ab66921 269 #define COAP_SERIALIZE_BYTE_OPTION(number, field, text) \
terencez 0:1fa43ab66921 270 if (IS_OPTION(coap_pkt, number)) { \
terencez 0:1fa43ab66921 271 PRINTF(text" %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", coap_pkt->field##_len, \
terencez 0:1fa43ab66921 272 coap_pkt->field[0], \
terencez 0:1fa43ab66921 273 coap_pkt->field[1], \
terencez 0:1fa43ab66921 274 coap_pkt->field[2], \
terencez 0:1fa43ab66921 275 coap_pkt->field[3], \
terencez 0:1fa43ab66921 276 coap_pkt->field[4], \
terencez 0:1fa43ab66921 277 coap_pkt->field[5], \
terencez 0:1fa43ab66921 278 coap_pkt->field[6], \
terencez 0:1fa43ab66921 279 coap_pkt->field[7] \
terencez 0:1fa43ab66921 280 ); /*FIXME always prints 8 bytes */ \
terencez 0:1fa43ab66921 281 option += coap_serialize_array_option(number, current_number, option, coap_pkt->field, coap_pkt->field##_len, '\0'); \
terencez 0:1fa43ab66921 282 current_number = number; \
terencez 0:1fa43ab66921 283 }
terencez 0:1fa43ab66921 284 #define COAP_SERIALIZE_STRING_OPTION(number, field, splitter, text) \
terencez 0:1fa43ab66921 285 if (IS_OPTION(coap_pkt, number)) { \
terencez 0:1fa43ab66921 286 PRINTF(text" [%.*s]\n", coap_pkt->field##_len, coap_pkt->field); \
terencez 0:1fa43ab66921 287 option += coap_serialize_array_option(number, current_number, option, (uint8_t *) coap_pkt->field, coap_pkt->field##_len, splitter); \
terencez 0:1fa43ab66921 288 current_number = number; \
terencez 0:1fa43ab66921 289 }
terencez 0:1fa43ab66921 290 #define COAP_SERIALIZE_MULTI_OPTION(number, field, text) \
terencez 0:1fa43ab66921 291 if (IS_OPTION(coap_pkt, number)) { \
terencez 0:1fa43ab66921 292 PRINTF(text); \
terencez 0:1fa43ab66921 293 option += coap_serialize_multi_option(number, current_number, option, coap_pkt->field); \
terencez 0:1fa43ab66921 294 current_number = number; \
terencez 0:1fa43ab66921 295 }
terencez 0:1fa43ab66921 296 #define COAP_SERIALIZE_ACCEPT_OPTION(number, field, text) \
terencez 0:1fa43ab66921 297 if (IS_OPTION(coap_pkt, number)) { \
terencez 0:1fa43ab66921 298 int i; \
terencez 0:1fa43ab66921 299 for (i=0; i<coap_pkt->field##_num; ++i) \
terencez 0:1fa43ab66921 300 { \
terencez 0:1fa43ab66921 301 PRINTF(text" [%u]\n", coap_pkt->field[i]); \
terencez 0:1fa43ab66921 302 option += coap_serialize_int_option(number, current_number, option, coap_pkt->field[i]); \
terencez 0:1fa43ab66921 303 current_number = number; \
terencez 0:1fa43ab66921 304 } \
terencez 0:1fa43ab66921 305 }
terencez 0:1fa43ab66921 306 #define COAP_SERIALIZE_BLOCK_OPTION(number, field, text) \
terencez 0:1fa43ab66921 307 if (IS_OPTION(coap_pkt, number)) \
terencez 0:1fa43ab66921 308 { \
terencez 0:1fa43ab66921 309 uint32_t block = coap_pkt->field##_num << 4; \
terencez 0:1fa43ab66921 310 PRINTF(text" [%lu%s (%u B/blk)]\n", coap_pkt->field##_num, coap_pkt->field##_more ? "+" : "", coap_pkt->field##_size); \
terencez 0:1fa43ab66921 311 if (coap_pkt->field##_more) block |= 0x8; \
terencez 0:1fa43ab66921 312 block |= 0xF & coap_log_2(coap_pkt->field##_size/16); \
terencez 0:1fa43ab66921 313 PRINTF(text" encoded: 0x%lX\n", block); \
terencez 0:1fa43ab66921 314 option += coap_serialize_int_option(number, current_number, option, block); \
terencez 0:1fa43ab66921 315 current_number = number; \
terencez 0:1fa43ab66921 316 }
terencez 0:1fa43ab66921 317
terencez 0:1fa43ab66921 318 /* To store error code and human-readable payload */
terencez 0:1fa43ab66921 319 extern const char *coap_error_message;
terencez 0:1fa43ab66921 320
terencez 0:1fa43ab66921 321 uint16_t coap_get_mid(void);
terencez 0:1fa43ab66921 322
terencez 0:1fa43ab66921 323 void coap_init_message(void *packet, coap_message_type_t type, uint8_t code, uint16_t mid);
terencez 0:1fa43ab66921 324 size_t coap_serialize_get_size(void *packet);
terencez 0:1fa43ab66921 325 size_t coap_serialize_message(void *packet, uint8_t *buffer);
terencez 0:1fa43ab66921 326 coap_status_t coap_parse_message(void *request, uint8_t *data, uint16_t data_len);
terencez 0:1fa43ab66921 327 void coap_free_header(void *packet);
terencez 0:1fa43ab66921 328
terencez 0:1fa43ab66921 329 char * coap_get_multi_option_as_string(multi_option_t * option);
terencez 0:1fa43ab66921 330 void coap_add_multi_option(multi_option_t **dst, uint8_t *option, size_t option_len, uint8_t is_static);
terencez 0:1fa43ab66921 331 void free_multi_option(multi_option_t *dst);
terencez 0:1fa43ab66921 332
terencez 0:1fa43ab66921 333 int coap_get_query_variable(void *packet, const char *name, const char **output);
terencez 0:1fa43ab66921 334 int coap_get_post_variable(void *packet, const char *name, const char **output);
terencez 0:1fa43ab66921 335
terencez 0:1fa43ab66921 336 /*-----------------------------------------------------------------------------------*/
terencez 0:1fa43ab66921 337
terencez 0:1fa43ab66921 338 int coap_set_status_code(void *packet, unsigned int code);
terencez 0:1fa43ab66921 339
terencez 0:1fa43ab66921 340 unsigned int coap_get_header_content_type(void *packet);
terencez 0:1fa43ab66921 341 int coap_set_header_content_type(void *packet, unsigned int content_type);
terencez 0:1fa43ab66921 342
terencez 0:1fa43ab66921 343 int coap_get_header_accept(void *packet, const uint16_t **accept);
terencez 0:1fa43ab66921 344 int coap_set_header_accept(void *packet, uint16_t accept);
terencez 0:1fa43ab66921 345
terencez 0:1fa43ab66921 346 int coap_get_header_max_age(void *packet, uint32_t *age);
terencez 0:1fa43ab66921 347 int coap_set_header_max_age(void *packet, uint32_t age);
terencez 0:1fa43ab66921 348
terencez 0:1fa43ab66921 349 int coap_get_header_etag(void *packet, const uint8_t **etag);
terencez 0:1fa43ab66921 350 int coap_set_header_etag(void *packet, const uint8_t *etag, size_t etag_len);
terencez 0:1fa43ab66921 351
terencez 0:1fa43ab66921 352 int coap_get_header_if_match(void *packet, const uint8_t **etag);
terencez 0:1fa43ab66921 353 int coap_set_header_if_match(void *packet, const uint8_t *etag, size_t etag_len);
terencez 0:1fa43ab66921 354
terencez 0:1fa43ab66921 355 int coap_get_header_if_none_match(void *packet);
terencez 0:1fa43ab66921 356 int coap_set_header_if_none_match(void *packet);
terencez 0:1fa43ab66921 357
terencez 0:1fa43ab66921 358 int coap_get_header_token(void *packet, const uint8_t **token);
terencez 0:1fa43ab66921 359 int coap_set_header_token(void *packet, const uint8_t *token, size_t token_len);
terencez 0:1fa43ab66921 360
terencez 0:1fa43ab66921 361 int coap_get_header_proxy_uri(void *packet, const char **uri); /* In-place string might not be 0-terminated. */
terencez 0:1fa43ab66921 362 int coap_set_header_proxy_uri(void *packet, const char *uri);
terencez 0:1fa43ab66921 363
terencez 0:1fa43ab66921 364 int coap_get_header_uri_host(void *packet, const char **host); /* In-place string might not be 0-terminated. */
terencez 0:1fa43ab66921 365 int coap_set_header_uri_host(void *packet, const char *host);
terencez 0:1fa43ab66921 366
terencez 0:1fa43ab66921 367 int coap_get_header_uri_path(void *packet, const char **path); /* In-place string might not be 0-terminated. */
terencez 0:1fa43ab66921 368 int coap_set_header_uri_path(void *packet, const char *path);
terencez 0:1fa43ab66921 369 int coap_set_header_uri_path_segment(void *packet, const char *path);
terencez 0:1fa43ab66921 370
terencez 0:1fa43ab66921 371 int coap_get_header_uri_query(void *packet, const char **query); /* In-place string might not be 0-terminated. */
terencez 0:1fa43ab66921 372 int coap_set_header_uri_query(void *packet, const char *query);
terencez 0:1fa43ab66921 373
terencez 0:1fa43ab66921 374 int coap_get_header_location_path(void *packet, const char **path); /* In-place string might not be 0-terminated. */
terencez 0:1fa43ab66921 375 int coap_set_header_location_path(void *packet, const char *path); /* Also splits optional query into Location-Query option. */
terencez 0:1fa43ab66921 376
terencez 0:1fa43ab66921 377 int coap_get_header_location_query(void *packet, const char **query); /* In-place string might not be 0-terminated. */
terencez 0:1fa43ab66921 378 int coap_set_header_location_query(void *packet, char *query);
terencez 0:1fa43ab66921 379
terencez 0:1fa43ab66921 380 int coap_get_header_observe(void *packet, uint32_t *observe);
terencez 0:1fa43ab66921 381 int coap_set_header_observe(void *packet, uint32_t observe);
terencez 0:1fa43ab66921 382
terencez 0:1fa43ab66921 383 int coap_get_header_block2(void *packet, uint32_t *num, uint8_t *more, uint16_t *size, uint32_t *offset);
terencez 0:1fa43ab66921 384 int coap_set_header_block2(void *packet, uint32_t num, uint8_t more, uint16_t size);
terencez 0:1fa43ab66921 385
terencez 0:1fa43ab66921 386 int coap_get_header_block1(void *packet, uint32_t *num, uint8_t *more, uint16_t *size, uint32_t *offset);
terencez 0:1fa43ab66921 387 int coap_set_header_block1(void *packet, uint32_t num, uint8_t more, uint16_t size);
terencez 0:1fa43ab66921 388
terencez 0:1fa43ab66921 389 int coap_get_header_size(void *packet, uint32_t *size);
terencez 0:1fa43ab66921 390 int coap_set_header_size(void *packet, uint32_t size);
terencez 0:1fa43ab66921 391
terencez 0:1fa43ab66921 392 int coap_get_payload(void *packet, const uint8_t **payload);
terencez 0:1fa43ab66921 393 int coap_set_payload(void *packet, const void *payload, size_t length);
terencez 0:1fa43ab66921 394
terencez 0:1fa43ab66921 395 #endif /* COAP_13_H_ */