43 #include "contiki-net.h"
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"
54 #define REST coap_rest_implementation
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)))))))
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])
73 #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLH_LEN + UIP_IPH_LEN])
77 enum { OPTION_MAP_SIZE =
sizeof(uint8_t) * 8 };
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)))
87 coap_message_type_t type;
92 uint8_t token[COAP_TOKEN_LEN];
94 uint8_t options[COAP_OPTION_SIZE1 / OPTION_MAP_SIZE + 1];
96 uint16_t content_format;
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;
105 const char *uri_host;
106 size_t location_path_len;
107 const char *location_path;
109 size_t location_query_len;
110 const char *location_query;
112 const char *uri_path;
115 uint8_t if_match_len;
116 uint8_t if_match[COAP_ETAG_LEN];
119 uint16_t block2_size;
120 uint32_t block2_offset;
123 uint16_t block1_size;
124 uint32_t block1_offset;
127 size_t uri_query_len;
128 const char *uri_query;
129 uint8_t if_none_match;
131 uint16_t payload_len;
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; \
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], \
154 option += coap_serialize_array_option(number, current_number, option, coap_pkt->field, coap_pkt->field##_len, '\0'); \
155 current_number = number; \
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; \
163 #define COAP_SERIALIZE_BLOCK_OPTION(number, field, text) \
164 if(IS_OPTION(coap_pkt, number)) \
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; \
176 extern coap_status_t erbium_status_code;
177 extern char *coap_error_message;
179 void coap_init_connection(uint16_t port);
180 uint16_t coap_get_mid(
void);
182 void coap_init_message(
void *packet, coap_message_type_t type, uint8_t code,
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,
187 coap_status_t coap_parse_message(
void *request, uint8_t *data,
190 int coap_get_query_variable(
void *packet,
const char *name,
192 int coap_get_post_variable(
void *packet,
const char *name,
197 int coap_set_status_code(
void *packet,
unsigned int code);
199 int coap_set_token(
void *packet,
const uint8_t *token,
size_t token_len);
201 int coap_get_header_content_format(
void *packet,
unsigned int *format);
202 int coap_set_header_content_format(
void *packet,
unsigned int format);
204 int coap_get_header_accept(
void *packet,
unsigned int *
accept);
205 int coap_set_header_accept(
void *packet,
unsigned int accept);
207 int coap_get_header_max_age(
void *packet, uint32_t *age);
208 int coap_set_header_max_age(
void *packet, uint32_t age);
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);
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,
217 int coap_get_header_if_none_match(
void *packet);
218 int coap_set_header_if_none_match(
void *packet);
220 int coap_get_header_proxy_uri(
void *packet,
const char **uri);
221 int coap_set_header_proxy_uri(
void *packet,
const char *uri);
223 int coap_get_header_proxy_scheme(
void *packet,
const char **scheme);
224 int coap_set_header_proxy_scheme(
void *packet,
const char *scheme);
226 int coap_get_header_uri_host(
void *packet,
const char **host);
227 int coap_set_header_uri_host(
void *packet,
const char *host);
229 int coap_get_header_uri_path(
void *packet,
const char **path);
230 int coap_set_header_uri_path(
void *packet,
const char *path);
232 int coap_get_header_uri_query(
void *packet,
const char **query);
233 int coap_set_header_uri_query(
void *packet,
const char *query);
235 int coap_get_header_location_path(
void *packet,
const char **path);
236 int coap_set_header_location_path(
void *packet,
const char *path);
238 int coap_get_header_location_query(
void *packet,
const char **query);
239 int coap_set_header_location_query(
void *packet,
const char *query);
241 int coap_get_header_observe(
void *packet, uint32_t *observe);
242 int coap_set_header_observe(
void *packet, uint32_t observe);
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,
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,
254 int coap_get_header_size2(
void *packet, uint32_t *size);
255 int coap_set_header_size2(
void *packet, uint32_t size);
257 int coap_get_header_size1(
void *packet, uint32_t *size);
258 int coap_set_header_size1(
void *packet, uint32_t size);
260 int coap_get_payload(
void *packet,
const uint8_t **payload);
261 int coap_set_payload(
void *packet,
const void *payload,
size_t length);
static uip_ds6_addr_t * addr
Pointer to a router list entry.
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.
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...