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 NETSTACK_CONF_WITH_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 /* parsed message struct */
83 typedef struct {
84  uint8_t *buffer; /* pointer to CoAP header / incoming packet buffer / memory to serialize packet */
85 
86  uint8_t version;
87  coap_message_type_t type;
88  uint8_t code;
89  uint16_t mid;
90 
91  uint8_t token_len;
92  uint8_t token[COAP_TOKEN_LEN];
93 
94  uint8_t options[COAP_OPTION_SIZE1 / OPTION_MAP_SIZE + 1]; /* bitmap to check if option is set */
95 
96  uint16_t content_format; /* parse options once and store; allows setting options in random order */
97  uint32_t max_age;
98  uint8_t etag_len;
99  uint8_t etag[COAP_ETAG_LEN];
100  size_t proxy_uri_len;
101  const char *proxy_uri;
102  size_t proxy_scheme_len;
103  const char *proxy_scheme;
104  size_t uri_host_len;
105  const char *uri_host;
106  size_t location_path_len;
107  const char *location_path;
108  uint16_t uri_port;
109  size_t location_query_len;
110  const char *location_query;
111  size_t uri_path_len;
112  const char *uri_path;
113  int32_t observe;
114  uint16_t accept;
115  uint8_t if_match_len;
116  uint8_t if_match[COAP_ETAG_LEN];
117  uint32_t block2_num;
118  uint8_t block2_more;
119  uint16_t block2_size;
120  uint32_t block2_offset;
121  uint32_t block1_num;
122  uint8_t block1_more;
123  uint16_t block1_size;
124  uint32_t block1_offset;
125  uint32_t size2;
126  uint32_t size1;
127  size_t uri_query_len;
128  const char *uri_query;
129  uint8_t if_none_match;
130 
131  uint16_t payload_len;
132  uint8_t *payload;
133 } coap_packet_t;
134 
135 /* option format serialization */
136 #define COAP_SERIALIZE_INT_OPTION(number, field, text) \
137  if(IS_OPTION(coap_pkt, number)) { \
138  PRINTF(text " [%u]\n", (unsigned int)coap_pkt->field); \
139  option += coap_serialize_int_option(number, current_number, option, coap_pkt->field); \
140  current_number = number; \
141  }
142 #define COAP_SERIALIZE_BYTE_OPTION(number, field, text) \
143  if(IS_OPTION(coap_pkt, number)) { \
144  PRINTF(text " %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n", (unsigned int)coap_pkt->field##_len, \
145  coap_pkt->field[0], \
146  coap_pkt->field[1], \
147  coap_pkt->field[2], \
148  coap_pkt->field[3], \
149  coap_pkt->field[4], \
150  coap_pkt->field[5], \
151  coap_pkt->field[6], \
152  coap_pkt->field[7] \
153  ); /* FIXME always prints 8 bytes */ \
154  option += coap_serialize_array_option(number, current_number, option, coap_pkt->field, coap_pkt->field##_len, '\0'); \
155  current_number = number; \
156  }
157 #define COAP_SERIALIZE_STRING_OPTION(number, field, splitter, text) \
158  if(IS_OPTION(coap_pkt, number)) { \
159  PRINTF(text " [%.*s]\n", (int)coap_pkt->field##_len, coap_pkt->field); \
160  option += coap_serialize_array_option(number, current_number, option, (uint8_t *)coap_pkt->field, coap_pkt->field##_len, splitter); \
161  current_number = number; \
162  }
163 #define COAP_SERIALIZE_BLOCK_OPTION(number, field, text) \
164  if(IS_OPTION(coap_pkt, number)) \
165  { \
166  PRINTF(text " [%lu%s (%u B/blk)]\n", (unsigned long)coap_pkt->field##_num, coap_pkt->field##_more ? "+" : "", coap_pkt->field##_size); \
167  uint32_t block = coap_pkt->field##_num << 4; \
168  if(coap_pkt->field##_more) { block |= 0x8; } \
169  block |= 0xF & coap_log_2(coap_pkt->field##_size / 16); \
170  PRINTF(text " encoded: 0x%lX\n", (unsigned long)block); \
171  option += coap_serialize_int_option(number, current_number, option, block); \
172  current_number = number; \
173  }
174 
175 /* to store error code and human-readable payload */
176 extern coap_status_t erbium_status_code;
177 extern char *coap_error_message;
178 
179 void coap_init_connection(uint16_t port);
180 uint16_t coap_get_mid(void);
181 
182 void coap_init_message(void *packet, coap_message_type_t type, uint8_t code,
183  uint16_t mid);
184 size_t coap_serialize_message(void *packet, uint8_t *buffer);
185 void coap_send_message(uip_ipaddr_t *addr, uint16_t port, uint8_t *data,
186  uint16_t length);
187 coap_status_t coap_parse_message(void *request, uint8_t *data,
188  uint16_t data_len);
189 
190 int coap_get_query_variable(void *packet, const char *name,
191  const char **output);
192 int coap_get_post_variable(void *packet, const char *name,
193  const char **output);
194 
195 /*---------------------------------------------------------------------------*/
196 
197 int coap_set_status_code(void *packet, unsigned int code);
198 
199 int coap_set_token(void *packet, const uint8_t *token, size_t token_len);
200 
201 int coap_get_header_content_format(void *packet, unsigned int *format);
202 int coap_set_header_content_format(void *packet, unsigned int format);
203 
204 int coap_get_header_accept(void *packet, unsigned int *accept);
205 int coap_set_header_accept(void *packet, unsigned int accept);
206 
207 int coap_get_header_max_age(void *packet, uint32_t *age);
208 int coap_set_header_max_age(void *packet, uint32_t age);
209 
210 int coap_get_header_etag(void *packet, const uint8_t **etag);
211 int coap_set_header_etag(void *packet, const uint8_t *etag, size_t etag_len);
212 
213 int coap_get_header_if_match(void *packet, const uint8_t **etag);
214 int coap_set_header_if_match(void *packet, const uint8_t *etag,
215  size_t etag_len);
216 
217 int coap_get_header_if_none_match(void *packet);
218 int coap_set_header_if_none_match(void *packet);
219 
220 int coap_get_header_proxy_uri(void *packet, const char **uri); /* in-place string might not be 0-terminated. */
221 int coap_set_header_proxy_uri(void *packet, const char *uri);
222 
223 int coap_get_header_proxy_scheme(void *packet, const char **scheme); /* in-place string might not be 0-terminated. */
224 int coap_set_header_proxy_scheme(void *packet, const char *scheme);
225 
226 int coap_get_header_uri_host(void *packet, const char **host); /* in-place string might not be 0-terminated. */
227 int coap_set_header_uri_host(void *packet, const char *host);
228 
229 int coap_get_header_uri_path(void *packet, const char **path); /* in-place string might not be 0-terminated. */
230 int coap_set_header_uri_path(void *packet, const char *path);
231 
232 int coap_get_header_uri_query(void *packet, const char **query); /* in-place string might not be 0-terminated. */
233 int coap_set_header_uri_query(void *packet, const char *query);
234 
235 int coap_get_header_location_path(void *packet, const char **path); /* in-place string might not be 0-terminated. */
236 int coap_set_header_location_path(void *packet, const char *path); /* also splits optional query into Location-Query option. */
237 
238 int coap_get_header_location_query(void *packet, const char **query); /* in-place string might not be 0-terminated. */
239 int coap_set_header_location_query(void *packet, const char *query);
240 
241 int coap_get_header_observe(void *packet, uint32_t *observe);
242 int coap_set_header_observe(void *packet, uint32_t observe);
243 
244 int coap_get_header_block2(void *packet, uint32_t *num, uint8_t *more,
245  uint16_t *size, uint32_t *offset);
246 int coap_set_header_block2(void *packet, uint32_t num, uint8_t more,
247  uint16_t size);
248 
249 int coap_get_header_block1(void *packet, uint32_t *num, uint8_t *more,
250  uint16_t *size, uint32_t *offset);
251 int coap_set_header_block1(void *packet, uint32_t num, uint8_t more,
252  uint16_t size);
253 
254 int coap_get_header_size2(void *packet, uint32_t *size);
255 int coap_set_header_size2(void *packet, uint32_t size);
256 
257 int coap_get_header_size1(void *packet, uint32_t *size);
258 int coap_set_header_size1(void *packet, uint32_t size);
259 
260 int coap_get_payload(void *packet, const uint8_t **payload);
261 int coap_set_payload(void *packet, const void *payload, size_t length);
262 
263 #endif /* ER_COAP_H_ */
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Definition: uip-nd6.c:124
An abstraction layer for RESTful Web services (Erbium).
Collection of constants specified in the CoAP standard.
static uint8_t output(const uip_lladdr_t *localdest)
Take an IP packet and format it to be sent on an 802.15.4 network using 6lowpan.
Definition: sicslowpan.c:1275
Collection of default configuration values.
static uint8_t accept(uint8_t in)
Processes an incoming or outgoing multicast message and determines whether it should be dropped or ac...
Definition: roll-tm.c:894