Contiki 3.x
er-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).
35  * \author
36  * Matthias Kovatsch <kovatsch@inf.ethz.ch>
37  */
38 
39 #ifndef ER_COAP_H_
40 #define ER_COAP_H_
41 
42 #include <stddef.h> /* for size_t */
43 #include "contiki-net.h"
44 #include "er-coap-constants.h"
45 #include "er-coap-conf.h"
46 
47 /* sanity check for configured values */
48 #define COAP_MAX_PACKET_SIZE (COAP_MAX_HEADER_SIZE + REST_MAX_CHUNK_SIZE)
49 #if COAP_MAX_PACKET_SIZE > (UIP_BUFSIZE - UIP_IPH_LEN - UIP_UDPH_LEN)
50 #error "UIP_CONF_BUFFER_SIZE too small for REST_MAX_CHUNK_SIZE"
51 #endif
52 
53 /* use Erbium CoAP for the REST Engine. Must come before include of rest-engine.h. */
54 #define REST coap_rest_implementation
55 #include "rest-engine.h"
56 
57 /* REST_MAX_CHUNK_SIZE can be different from 2^x so we need to get next lower 2^x for COAP_MAX_BLOCK_SIZE */
58 #ifndef COAP_MAX_BLOCK_SIZE
59 #define COAP_MAX_BLOCK_SIZE (REST_MAX_CHUNK_SIZE < 32 ? 16 : \
60  (REST_MAX_CHUNK_SIZE < 64 ? 32 : \
61  (REST_MAX_CHUNK_SIZE < 128 ? 64 : \
62  (REST_MAX_CHUNK_SIZE < 256 ? 128 : \
63  (REST_MAX_CHUNK_SIZE < 512 ? 256 : \
64  (REST_MAX_CHUNK_SIZE < 1024 ? 512 : \
65  (REST_MAX_CHUNK_SIZE < 2048 ? 1024 : 2048)))))))
66 #endif /* COAP_MAX_BLOCK_SIZE */
67 
68 /* direct access into the buffer */
69 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
70 #if UIP_CONF_IPV6
71 #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
72 #else
73 #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN])
74 #endif
75 
76 /* bitmap for set options */
77 enum { OPTION_MAP_SIZE = sizeof(uint8_t) * 8 };
78 
79 #define SET_OPTION(packet, opt) ((packet)->options[opt / OPTION_MAP_SIZE] |= 1 << (opt % OPTION_MAP_SIZE))
80 #define IS_OPTION(packet, opt) ((packet)->options[opt / OPTION_MAP_SIZE] & (1 << (opt % OPTION_MAP_SIZE)))
81 
82 #ifndef MIN
83 #define MIN(a, b) ((a) < (b) ? (a) : (b))
84 #endif /* MIN */
85 
86 /* parsed message struct */
87 typedef struct {
88  uint8_t *buffer; /* pointer to CoAP header / incoming packet buffer / memory to serialize packet */
89 
90  uint8_t version;
91  coap_message_type_t type;
92  uint8_t code;
93  uint16_t mid;
94 
95  uint8_t token_len;
96  uint8_t token[COAP_TOKEN_LEN];
97 
98  uint8_t options[COAP_OPTION_SIZE1 / OPTION_MAP_SIZE + 1]; /* bitmap to check if option is set */
99 
100  coap_content_format_t content_format; /* parse options once and store; allows setting options in random order */
101  uint32_t max_age;
102  uint8_t etag_len;
103  uint8_t etag[COAP_ETAG_LEN];
104  size_t proxy_uri_len;
105  const char *proxy_uri;
106  size_t proxy_scheme_len;
107  const char *proxy_scheme;
108  size_t uri_host_len;
109  const char *uri_host;
110  size_t location_path_len;
111  const char *location_path;
112  uint16_t uri_port;
113  size_t location_query_len;
114  const char *location_query;
115  size_t uri_path_len;
116  const char *uri_path;
117  int32_t observe;
118  coap_content_format_t accept;
119  uint8_t if_match_len;
120  uint8_t if_match[COAP_ETAG_LEN];
121  uint32_t block2_num;
122  uint8_t block2_more;
123  uint16_t block2_size;
124  uint32_t block2_offset;
125  uint32_t block1_num;
126  uint8_t block1_more;
127  uint16_t block1_size;
128  uint32_t block1_offset;
129  uint32_t size2;
130  uint32_t size1;
131  size_t uri_query_len;
132  const char *uri_query;
133  uint8_t if_none_match;
134 
135  uint16_t payload_len;
136  uint8_t *payload;
137 } coap_packet_t;
138 
139 /* option format serialization */
140 #define COAP_SERIALIZE_INT_OPTION(number, field, text) \
141  if(IS_OPTION(coap_pkt, number)) { \
142  PRINTF(text " [%u]\n", coap_pkt->field); \
143  option += coap_serialize_int_option(number, current_number, option, coap_pkt->field); \
144  current_number = number; \
145  }
146 #define COAP_SERIALIZE_BYTE_OPTION(number, field, text) \
147  if(IS_OPTION(coap_pkt, number)) { \
148  PRINTF(text " %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", coap_pkt->field##_len, \
149  coap_pkt->field[0], \
150  coap_pkt->field[1], \
151  coap_pkt->field[2], \
152  coap_pkt->field[3], \
153  coap_pkt->field[4], \
154  coap_pkt->field[5], \
155  coap_pkt->field[6], \
156  coap_pkt->field[7] \
157  ); /* FIXME always prints 8 bytes */ \
158  option += coap_serialize_array_option(number, current_number, option, coap_pkt->field, coap_pkt->field##_len, '\0'); \
159  current_number = number; \
160  }
161 #define COAP_SERIALIZE_STRING_OPTION(number, field, splitter, text) \
162  if(IS_OPTION(coap_pkt, number)) { \
163  PRINTF(text " [%.*s]\n", coap_pkt->field##_len, coap_pkt->field); \
164  option += coap_serialize_array_option(number, current_number, option, (uint8_t *)coap_pkt->field, coap_pkt->field##_len, splitter); \
165  current_number = number; \
166  }
167 #define COAP_SERIALIZE_BLOCK_OPTION(number, field, text) \
168  if(IS_OPTION(coap_pkt, number)) \
169  { \
170  PRINTF(text " [%lu%s (%u B/blk)]\n", coap_pkt->field##_num, coap_pkt->field##_more ? "+" : "", coap_pkt->field##_size); \
171  uint32_t block = coap_pkt->field##_num << 4; \
172  if(coap_pkt->field##_more) { block |= 0x8; } \
173  block |= 0xF & coap_log_2(coap_pkt->field##_size / 16); \
174  PRINTF(text " encoded: 0x%lX\n", block); \
175  option += coap_serialize_int_option(number, current_number, option, block); \
176  current_number = number; \
177  }
178 
179 /* to store error code and human-readable payload */
180 extern coap_status_t erbium_status_code;
181 extern char *coap_error_message;
182 
183 void coap_init_connection(uint16_t port);
184 uint16_t coap_get_mid(void);
185 
186 void coap_init_message(void *packet, coap_message_type_t type, uint8_t code,
187  uint16_t mid);
188 size_t coap_serialize_message(void *packet, uint8_t *buffer);
189 void coap_send_message(uip_ipaddr_t *addr, uint16_t port, uint8_t *data,
190  uint16_t length);
191 coap_status_t coap_parse_message(void *request, uint8_t *data,
192  uint16_t data_len);
193 
194 int coap_get_query_variable(void *packet, const char *name,
195  const char **output);
196 int coap_get_post_variable(void *packet, const char *name,
197  const char **output);
198 
199 /*---------------------------------------------------------------------------*/
200 
201 int coap_set_status_code(void *packet, unsigned int code);
202 
203 int coap_set_token(void *packet, const uint8_t *token, size_t token_len);
204 
205 int coap_get_header_content_format(void *packet, unsigned int *format);
206 int coap_set_header_content_format(void *packet, unsigned int format);
207 
208 int coap_get_header_accept(void *packet, unsigned int *accept);
209 int coap_set_header_accept(void *packet, unsigned int accept);
210 
211 int coap_get_header_max_age(void *packet, uint32_t *age);
212 int coap_set_header_max_age(void *packet, uint32_t age);
213 
214 int coap_get_header_etag(void *packet, const uint8_t **etag);
215 int coap_set_header_etag(void *packet, const uint8_t *etag, size_t etag_len);
216 
217 int coap_get_header_if_match(void *packet, const uint8_t **etag);
218 int coap_set_header_if_match(void *packet, const uint8_t *etag,
219  size_t etag_len);
220 
221 int coap_get_header_if_none_match(void *packet);
222 int coap_set_header_if_none_match(void *packet);
223 
224 int coap_get_header_proxy_uri(void *packet, const char **uri); /* in-place string might not be 0-terminated. */
225 int coap_set_header_proxy_uri(void *packet, const char *uri);
226 
227 int coap_get_header_proxy_scheme(void *packet, const char **scheme); /* in-place string might not be 0-terminated. */
228 int coap_set_header_proxy_scheme(void *packet, const char *scheme);
229 
230 int coap_get_header_uri_host(void *packet, const char **host); /* in-place string might not be 0-terminated. */
231 int coap_set_header_uri_host(void *packet, const char *host);
232 
233 int coap_get_header_uri_path(void *packet, const char **path); /* in-place string might not be 0-terminated. */
234 int coap_set_header_uri_path(void *packet, const char *path);
235 
236 int coap_get_header_uri_query(void *packet, const char **query); /* in-place string might not be 0-terminated. */
237 int coap_set_header_uri_query(void *packet, const char *query);
238 
239 int coap_get_header_location_path(void *packet, const char **path); /* in-place string might not be 0-terminated. */
240 int coap_set_header_location_path(void *packet, const char *path); /* also splits optional query into Location-Query option. */
241 
242 int coap_get_header_location_query(void *packet, const char **query); /* in-place string might not be 0-terminated. */
243 int coap_set_header_location_query(void *packet, const char *query);
244 
245 int coap_get_header_observe(void *packet, uint32_t *observe);
246 int coap_set_header_observe(void *packet, uint32_t observe);
247 
248 int coap_get_header_block2(void *packet, uint32_t *num, uint8_t *more,
249  uint16_t *size, uint32_t *offset);
250 int coap_set_header_block2(void *packet, uint32_t num, uint8_t more,
251  uint16_t size);
252 
253 int coap_get_header_block1(void *packet, uint32_t *num, uint8_t *more,
254  uint16_t *size, uint32_t *offset);
255 int coap_set_header_block1(void *packet, uint32_t num, uint8_t more,
256  uint16_t size);
257 
258 int coap_get_header_size2(void *packet, uint32_t *size);
259 int coap_set_header_size2(void *packet, uint32_t size);
260 
261 int coap_get_header_size1(void *packet, uint32_t *size);
262 int coap_set_header_size1(void *packet, uint32_t size);
263 
264 int coap_get_payload(void *packet, const uint8_t **payload);
265 int coap_set_payload(void *packet, const void *payload, size_t length);
266 
267 #endif /* ER_COAP_H_ */
Collection of constants specified in the CoAP standard.
An abstraction layer for RESTful Web services (Erbium).
Collection of default configuration values.