Contiki-NG
Loading...
Searching...
No Matches
coap.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 */
31
32/**
33 * \file
34 * An implementation of the Constrained Application Protocol (RFC 7252).
35 * \author
36 * Matthias Kovatsch <kovatsch@inf.ethz.ch>
37 */
38
39/**
40 * \addtogroup apps
41 * @{
42 *
43 * \defgroup coap An implementation of CoAP
44 * @{
45 *
46 * This is an implementation of the Constrained Application Protocol
47 */
48
49#ifndef COAP_H_
50#define COAP_H_
51
52#include <stddef.h> /* for size_t */
53#include "coap-constants.h"
54#include "coap-conf.h"
55#include "coap-transport.h"
56
57/**
58 * \brief The max size of the CoAP messages
59 */
60#define COAP_MAX_PACKET_SIZE (COAP_MAX_HEADER_SIZE + COAP_MAX_CHUNK_SIZE)
61
62/* COAP_MAX_CHUNK_SIZE can be different from 2^x so we need to get next lower 2^x for COAP_MAX_BLOCK_SIZE */
63#ifndef COAP_MAX_BLOCK_SIZE
64#define COAP_MAX_BLOCK_SIZE (COAP_MAX_CHUNK_SIZE < 32 ? 16 : \
65 (COAP_MAX_CHUNK_SIZE < 64 ? 32 : \
66 (COAP_MAX_CHUNK_SIZE < 128 ? 64 : \
67 (COAP_MAX_CHUNK_SIZE < 256 ? 128 : \
68 (COAP_MAX_CHUNK_SIZE < 512 ? 256 : \
69 (COAP_MAX_CHUNK_SIZE < 1024 ? 512 : \
70 (COAP_MAX_CHUNK_SIZE < 2048 ? 1024 : 2048)))))))
71#endif /* COAP_MAX_BLOCK_SIZE */
72
73/* bitmap for set options */
74#define COAP_OPTION_MAP_SIZE (sizeof(uint8_t) * 8)
75
76/* parsed message struct */
77typedef struct {
78 uint8_t *buffer; /* pointer to CoAP header / incoming message buffer / memory to serialize message */
79
80 uint8_t version;
81 coap_message_type_t type;
82 uint8_t code;
83 uint16_t mid;
84
85 uint8_t token_len;
86 uint8_t token[COAP_TOKEN_LEN];
87
88 uint8_t options[COAP_OPTION_SIZE1 / COAP_OPTION_MAP_SIZE + 1]; /* bitmap to check if option is set */
89
90 uint16_t content_format; /* parse options once and store; allows setting options in random order */
91 uint32_t max_age;
92 uint8_t etag_len;
93 uint8_t etag[COAP_ETAG_LEN];
94 size_t proxy_uri_len;
95 const char *proxy_uri;
96 size_t proxy_scheme_len;
97 const char *proxy_scheme;
98 size_t uri_host_len;
99 const char *uri_host;
100 size_t location_path_len;
101 const char *location_path;
102 uint16_t uri_port;
103 size_t location_query_len;
104 const char *location_query;
105 size_t uri_path_len;
106 const char *uri_path;
107 int32_t observe;
108 uint16_t accept;
109 uint8_t if_match_len;
110 uint8_t if_match[COAP_ETAG_LEN];
111 uint32_t block2_num;
112 uint8_t block2_more;
113 uint16_t block2_size;
114 uint32_t block2_offset;
115 uint32_t block1_num;
116 uint8_t block1_more;
117 uint16_t block1_size;
118 uint32_t block1_offset;
119 uint32_t size2;
120 uint32_t size1;
121 size_t uri_query_len;
122 const char *uri_query;
123 uint8_t if_none_match;
124
125 const coap_endpoint_t *src_ep;
126
127 uint16_t payload_len;
128 uint8_t *payload;
129} coap_message_t;
130
131/*
132 * The bitmap currently spans 0..COAP_OPTION_SIZE1 (60). The parser
133 * accepts option numbers up to UINT16_MAX, so options beyond
134 * COAP_OPTION_SIZE1 (e.g., Echo=252, No-Response=258, Request-Tag=292)
135 * are parsed but not tracked here, and coap_is_option() will return 0
136 * for them. This is harmless for options handled solely via the
137 * default switch case in coap_parse_message(); extend the bitmap
138 * (and adjust the message->options array size) when adding accessor
139 * APIs for high-numbered options that need presence tracking.
140 */
141static inline int
142coap_set_option(coap_message_t *message, unsigned int opt)
143{
144 if(opt > COAP_OPTION_SIZE1) {
145 return 0;
146 }
147 message->options[opt / COAP_OPTION_MAP_SIZE] |= 1 << (opt % COAP_OPTION_MAP_SIZE);
148 return 1;
149}
150
151static inline int
152coap_is_option(const coap_message_t *message, unsigned int opt)
153{
154 return (opt <= COAP_OPTION_SIZE1) &&
155 (message->options[opt / COAP_OPTION_MAP_SIZE] & (1 << (opt % COAP_OPTION_MAP_SIZE))) != 0;
156}
157
158/* option format serialization */
159#define COAP_SERIALIZE_INT_OPTION(number, field, text) \
160 if(coap_is_option(coap_pkt, number)) { \
161 LOG_DBG(text " [%u]\n", (unsigned int)coap_pkt->field); \
162 option += coap_serialize_int_option(number, current_number, option, coap_pkt->field); \
163 current_number = number; \
164 }
165#define COAP_SERIALIZE_BYTE_OPTION(number, field, text) \
166 if(coap_is_option(coap_pkt, number)) { \
167 LOG_DBG(text " %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", (unsigned int)coap_pkt->field##_len, \
168 coap_pkt->field[0], \
169 coap_pkt->field[1], \
170 coap_pkt->field[2], \
171 coap_pkt->field[3], \
172 coap_pkt->field[4], \
173 coap_pkt->field[5], \
174 coap_pkt->field[6], \
175 coap_pkt->field[7] \
176 ); /* FIXME always prints 8 bytes */ \
177 option += coap_serialize_array_option(number, current_number, option, coap_pkt->field, coap_pkt->field##_len, '\0'); \
178 current_number = number; \
179 }
180#define COAP_SERIALIZE_STRING_OPTION(number, field, splitter, text) \
181 if(coap_is_option(coap_pkt, number)) { \
182 LOG_DBG(text " ["); \
183 LOG_DBG_COAP_STRING(coap_pkt->field, coap_pkt->field##_len); \
184 LOG_DBG_("]\n"); \
185 option += coap_serialize_array_option(number, current_number, option, (uint8_t *)coap_pkt->field, coap_pkt->field##_len, splitter); \
186 current_number = number; \
187 }
188#define COAP_SERIALIZE_BLOCK_OPTION(number, field, text) \
189 if(coap_is_option(coap_pkt, number)) { \
190 LOG_DBG(text " [%lu%s (%u B/blk)]\n", (unsigned long)coap_pkt->field##_num, coap_pkt->field##_more ? "+" : "", coap_pkt->field##_size); \
191 uint32_t block = coap_pkt->field##_num << 4; \
192 if(coap_pkt->field##_more) { block |= 0x8; } \
193 block |= 0xF & coap_log_2(coap_pkt->field##_size / 16); \
194 LOG_DBG(text " encoded: 0x%lX\n", (unsigned long)block); \
195 option += coap_serialize_int_option(number, current_number, option, block); \
196 current_number = number; \
197 }
198
199/* to store error code and human-readable payload */
200extern coap_status_t coap_status_code;
201#if COAP_MESSAGE_ON_ERROR
202extern const char *coap_error_message;
203#endif
204
205void coap_init_connection(void);
206uint16_t coap_get_mid(void);
207
208void coap_init_message(coap_message_t *message, coap_message_type_t type,
209 uint8_t code, uint16_t mid);
210size_t coap_serialize_message(coap_message_t *message, uint8_t *buffer);
211coap_status_t coap_parse_message(coap_message_t *request, uint8_t *data,
212 uint16_t data_len);
213
214int coap_get_query_variable(const coap_message_t *message, const char *name,
215 const char **output);
216int coap_get_post_variable(const coap_message_t *message, const char *name,
217 const char **output);
218
219static inline coap_resource_flags_t
220coap_get_method_type(const coap_message_t *message)
221{
222 return (coap_resource_flags_t)(1 << (message->code - 1));
223}
224
225static inline const coap_endpoint_t *
226coap_get_src_endpoint(const coap_message_t *request)
227{
228 return request->src_ep;
229}
230
231static inline void
232coap_set_src_endpoint(coap_message_t *request, const coap_endpoint_t *ep)
233{
234 request->src_ep = ep;
235}
236/*---------------------------------------------------------------------------*/
237
238int coap_set_status_code(coap_message_t *message, unsigned int code);
239
240int coap_set_token(coap_message_t *message, const uint8_t *token,
241 size_t token_len);
242
243int coap_get_header_content_format(const coap_message_t *message,
244 unsigned int *format);
245int coap_set_header_content_format(coap_message_t *message, unsigned int format);
246
247int coap_get_header_accept(const coap_message_t *message, unsigned int *accept);
248int coap_set_header_accept(coap_message_t *message, unsigned int accept);
249
250int coap_get_header_max_age(const coap_message_t *message, uint32_t *age);
251int coap_set_header_max_age(coap_message_t *message, uint32_t age);
252
253int coap_get_header_etag(const coap_message_t *message, const uint8_t **etag);
254int coap_set_header_etag(coap_message_t *message, const uint8_t *etag,
255 size_t etag_len);
256
257int coap_get_header_if_match(const coap_message_t *message,
258 const uint8_t **etag);
259int coap_set_header_if_match(coap_message_t *message, const uint8_t *etag,
260 size_t etag_len);
261
262int coap_get_header_if_none_match(const coap_message_t *message);
263int coap_set_header_if_none_match(coap_message_t *message);
264
265/* in-place string might not be 0-terminated. */
266int coap_get_header_proxy_uri(const coap_message_t *message, const char **uri);
267int coap_set_header_proxy_uri(coap_message_t *message, const char *uri);
268
269/* in-place string might not be 0-terminated. FIXME not supported */
270/* int coap_get_header_proxy_scheme(const coap_message_t *message,
271 const char **scheme); */
272/* int coap_set_header_proxy_scheme(coap_message_t *message,
273 const char *scheme); */
274
275/* in-place string might not be 0-terminated. */
276int coap_get_header_uri_host(const coap_message_t *message, const char **host);
277int coap_set_header_uri_host(coap_message_t *message, const char *host);
278
279/* in-place string might not be 0-terminated. */
280int coap_get_header_uri_path(const coap_message_t *message, const char **path);
281int coap_set_header_uri_path(coap_message_t *message, const char *path);
282
283/* in-place string might not be 0-terminated. */
284int coap_get_header_uri_query(const coap_message_t *message,
285 const char **query);
286int coap_set_header_uri_query(coap_message_t *message, const char *query);
287
288/* in-place string might not be 0-terminated. */
289int coap_get_header_location_path(const coap_message_t *message,
290 const char **path);
291/* also splits optional query into Location-Query option. */
292int coap_set_header_location_path(coap_message_t *message, const char *path);
293
294/* in-place string might not be 0-terminated. */
295int coap_get_header_location_query(const coap_message_t *message,
296 const char **query);
297int coap_set_header_location_query(coap_message_t *message, const char *query);
298
299int coap_get_header_observe(const coap_message_t *message, uint32_t *observe);
300int coap_set_header_observe(coap_message_t *message, uint32_t observe);
301
302int coap_get_header_block2(const coap_message_t *message, uint32_t *num,
303 uint8_t *more, uint16_t *size, uint32_t *offset);
304int coap_set_header_block2(coap_message_t *message, uint32_t num, uint8_t more,
305 uint16_t size);
306
307int coap_get_header_block1(const coap_message_t *message, uint32_t *num,
308 uint8_t *more, uint16_t *size, uint32_t *offset);
309int coap_set_header_block1(coap_message_t *message, uint32_t num, uint8_t more,
310 uint16_t size);
311
312int coap_get_header_size2(const coap_message_t *message, uint32_t *size);
313int coap_set_header_size2(coap_message_t *message, uint32_t size);
314
315int coap_get_header_size1(const coap_message_t *message, uint32_t *size);
316int coap_set_header_size1(coap_message_t *message, uint32_t size);
317
318int coap_get_payload(const coap_message_t *message, const uint8_t **payload);
319int coap_set_payload(coap_message_t *message, const void *payload,
320 size_t length);
321
322#endif /* COAP_H_ */
323/** @} */
324/** @} */
Collection of default configuration values.
Collection of constants specified in the CoAP standard.
API for CoAP transport.
coap_resource_flags_t
Resource flags for allowed methods and special functionalities.
static uint8_t accept(uint8_t in)
Definition mpl.c:1390