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])
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)))
83 #define MIN(a, b) ((a) < (b) ? (a) : (b))
91 coap_message_type_t type;
96 uint8_t token[COAP_TOKEN_LEN];
98 uint8_t options[COAP_OPTION_SIZE1 / OPTION_MAP_SIZE + 1];
100 coap_content_format_t content_format;
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;
109 const char *uri_host;
110 size_t location_path_len;
111 const char *location_path;
113 size_t location_query_len;
114 const char *location_query;
116 const char *uri_path;
118 coap_content_format_t accept;
119 uint8_t if_match_len;
120 uint8_t if_match[COAP_ETAG_LEN];
123 uint16_t block2_size;
124 uint32_t block2_offset;
127 uint16_t block1_size;
128 uint32_t block1_offset;
131 size_t uri_query_len;
132 const char *uri_query;
133 uint8_t if_none_match;
135 uint16_t payload_len;
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; \
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], \
158 option += coap_serialize_array_option(number, current_number, option, coap_pkt->field, coap_pkt->field##_len, '\0'); \
159 current_number = number; \
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; \
167 #define COAP_SERIALIZE_BLOCK_OPTION(number, field, text) \
168 if(IS_OPTION(coap_pkt, number)) \
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; \
180 extern coap_status_t erbium_status_code;
181 extern char *coap_error_message;
183 void coap_init_connection(uint16_t port);
184 uint16_t coap_get_mid(
void);
186 void coap_init_message(
void *packet, coap_message_type_t type, uint8_t code,
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,
191 coap_status_t coap_parse_message(
void *request, uint8_t *data,
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);
201 int coap_set_status_code(
void *packet,
unsigned int code);
203 int coap_set_token(
void *packet,
const uint8_t *token,
size_t token_len);
205 int coap_get_header_content_format(
void *packet,
unsigned int *format);
206 int coap_set_header_content_format(
void *packet,
unsigned int format);
208 int coap_get_header_accept(
void *packet,
unsigned int *accept);
209 int coap_set_header_accept(
void *packet,
unsigned int accept);
211 int coap_get_header_max_age(
void *packet, uint32_t *age);
212 int coap_set_header_max_age(
void *packet, uint32_t age);
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);
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,
221 int coap_get_header_if_none_match(
void *packet);
222 int coap_set_header_if_none_match(
void *packet);
224 int coap_get_header_proxy_uri(
void *packet,
const char **uri);
225 int coap_set_header_proxy_uri(
void *packet,
const char *uri);
227 int coap_get_header_proxy_scheme(
void *packet,
const char **scheme);
228 int coap_set_header_proxy_scheme(
void *packet,
const char *scheme);
230 int coap_get_header_uri_host(
void *packet,
const char **host);
231 int coap_set_header_uri_host(
void *packet,
const char *host);
233 int coap_get_header_uri_path(
void *packet,
const char **path);
234 int coap_set_header_uri_path(
void *packet,
const char *path);
236 int coap_get_header_uri_query(
void *packet,
const char **query);
237 int coap_set_header_uri_query(
void *packet,
const char *query);
239 int coap_get_header_location_path(
void *packet,
const char **path);
240 int coap_set_header_location_path(
void *packet,
const char *path);
242 int coap_get_header_location_query(
void *packet,
const char **query);
243 int coap_set_header_location_query(
void *packet,
const char *query);
245 int coap_get_header_observe(
void *packet, uint32_t *observe);
246 int coap_set_header_observe(
void *packet, uint32_t observe);
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,
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,
258 int coap_get_header_size2(
void *packet, uint32_t *size);
259 int coap_set_header_size2(
void *packet, uint32_t size);
261 int coap_get_header_size1(
void *packet, uint32_t *size);
262 int coap_set_header_size1(
void *packet, uint32_t size);
264 int coap_get_payload(
void *packet,
const uint8_t **payload);
265 int coap_set_payload(
void *packet,
const void *payload,
size_t length);
Collection of constants specified in the CoAP standard.
An abstraction layer for RESTful Web services (Erbium).
Collection of default configuration values.