Contiki 3.x
er-coap.c
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 #include <string.h>
40 #include <stdio.h>
41 #include "contiki.h"
42 #include "contiki-net.h"
43 
44 #include "er-coap.h"
45 #include "er-coap-transactions.h"
46 
47 #define DEBUG 0
48 #if DEBUG
49 #include <stdio.h>
50 #define PRINTF(...) printf(__VA_ARGS__)
51 #define PRINT6ADDR(addr) PRINTF("[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
52 #define PRINTLLADDR(lladdr) PRINTF("[%02x:%02x:%02x:%02x:%02x:%02x]", (lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3], (lladdr)->addr[4], (lladdr)->addr[5])
53 #else
54 #define PRINTF(...)
55 #define PRINT6ADDR(addr)
56 #define PRINTLLADDR(addr)
57 #endif
58 
59 /*---------------------------------------------------------------------------*/
60 /*- Variables ---------------------------------------------------------------*/
61 /*---------------------------------------------------------------------------*/
62 static struct uip_udp_conn *udp_conn = NULL;
63 static uint16_t current_mid = 0;
64 
65 coap_status_t erbium_status_code = NO_ERROR;
66 char *coap_error_message = "";
67 /*---------------------------------------------------------------------------*/
68 /*- Local helper functions --------------------------------------------------*/
69 /*---------------------------------------------------------------------------*/
70 static uint16_t
71 coap_log_2(uint16_t value)
72 {
73  uint16_t result = 0;
74 
75  do {
76  value = value >> 1;
77  result++;
78  } while(value);
79 
80  return result ? result - 1 : result;
81 }
82 /*---------------------------------------------------------------------------*/
83 static uint32_t
84 coap_parse_int_option(uint8_t *bytes, size_t length)
85 {
86  uint32_t var = 0;
87  int i = 0;
88 
89  while(i < length) {
90  var <<= 8;
91  var |= bytes[i++];
92  }
93  return var;
94 }
95 /*---------------------------------------------------------------------------*/
96 static uint8_t
97 coap_option_nibble(unsigned int value)
98 {
99  if(value < 13) {
100  return value;
101  } else if(value <= 0xFF + 13) {
102  return 13;
103  } else {
104  return 14;
105  }
106 }
107 /*---------------------------------------------------------------------------*/
108 static size_t
109 coap_set_option_header(unsigned int delta, size_t length, uint8_t *buffer)
110 {
111  size_t written = 0;
112 
113  buffer[0] = coap_option_nibble(delta) << 4 | coap_option_nibble(length);
114 
115  /* avoids code duplication without function overhead */
116  unsigned int *x = &delta;
117 
118  do {
119  if(*x > 268) {
120  buffer[++written] = (*x - 269) >> 8;
121  buffer[++written] = (*x - 269);
122  } else if(*x > 12) {
123  buffer[++written] = (*x - 13);
124  }
125  } while(x != &length && (x = &length));
126 
127  PRINTF("WRITTEN %u B opt header\n", 1 + written);
128 
129  return ++written;
130 }
131 /*---------------------------------------------------------------------------*/
132 static size_t
133 coap_serialize_int_option(unsigned int number, unsigned int current_number,
134  uint8_t *buffer, uint32_t value)
135 {
136  size_t i = 0;
137 
138  if(0xFF000000 & value) {
139  ++i;
140  }
141  if(0xFFFF0000 & value) {
142  ++i;
143  }
144  if(0xFFFFFF00 & value) {
145  ++i;
146  }
147  if(0xFFFFFFFF & value) {
148  ++i;
149  }
150  PRINTF("OPTION %u (delta %u, len %u)\n", number, number - current_number,
151  i);
152 
153  i = coap_set_option_header(number - current_number, i, buffer);
154 
155  if(0xFF000000 & value) {
156  buffer[i++] = (uint8_t)(value >> 24);
157  }
158  if(0xFFFF0000 & value) {
159  buffer[i++] = (uint8_t)(value >> 16);
160  }
161  if(0xFFFFFF00 & value) {
162  buffer[i++] = (uint8_t)(value >> 8);
163  }
164  if(0xFFFFFFFF & value) {
165  buffer[i++] = (uint8_t)(value);
166  }
167  return i;
168 }
169 /*---------------------------------------------------------------------------*/
170 static size_t
171 coap_serialize_array_option(unsigned int number, unsigned int current_number,
172  uint8_t *buffer, uint8_t *array, size_t length,
173  char split_char)
174 {
175  size_t i = 0;
176 
177  PRINTF("ARRAY type %u, len %u, full [%.*s]\n", number, length, length,
178  array);
179 
180  if(split_char != '\0') {
181  int j;
182  uint8_t *part_start = array;
183  uint8_t *part_end = NULL;
184  size_t temp_length;
185 
186  for(j = 0; j <= length + 1; ++j) {
187  PRINTF("STEP %u/%u (%c)\n", j, length, array[j]);
188  if(array[j] == split_char || j == length) {
189  part_end = array + j;
190  temp_length = part_end - part_start;
191 
192  i += coap_set_option_header(number - current_number, temp_length,
193  &buffer[i]);
194  memcpy(&buffer[i], part_start, temp_length);
195  i += temp_length;
196 
197  PRINTF("OPTION type %u, delta %u, len %u, part [%.*s]\n", number,
198  number - current_number, i, temp_length, part_start);
199 
200  ++j; /* skip the splitter */
201  current_number = number;
202  part_start = array + j;
203  }
204  } /* for */
205  } else {
206  i += coap_set_option_header(number - current_number, length, &buffer[i]);
207  memcpy(&buffer[i], array, length);
208  i += length;
209 
210  PRINTF("OPTION type %u, delta %u, len %u\n", number,
211  number - current_number, length);
212  }
213 
214  return i;
215 }
216 /*---------------------------------------------------------------------------*/
217 static void
218 coap_merge_multi_option(char **dst, size_t *dst_len, uint8_t *option,
219  size_t option_len, char separator)
220 {
221  /* merge multiple options */
222  if(*dst_len > 0) {
223  /* dst already contains an option: concatenate */
224  (*dst)[*dst_len] = separator;
225  *dst_len += 1;
226 
227  /* memmove handles 2-byte option headers */
228  memmove((*dst) + (*dst_len), option, option_len);
229 
230  *dst_len += option_len;
231  } else {
232  /* dst is empty: set to option */
233  *dst = (char *)option;
234  *dst_len = option_len;
235  }
236 }
237 /*---------------------------------------------------------------------------*/
238 static int
239 coap_get_variable(const char *buffer, size_t length, const char *name,
240  const char **output)
241 {
242  const char *start = NULL;
243  const char *end = NULL;
244  const char *value_end = NULL;
245  size_t name_len = 0;
246 
247  /*initialize the output buffer first */
248  *output = 0;
249 
250  name_len = strlen(name);
251  end = buffer + length;
252 
253  for(start = buffer; start + name_len < end; ++start) {
254  if((start == buffer || start[-1] == '&') && start[name_len] == '='
255  && strncmp(name, start, name_len) == 0) {
256 
257  /* Point start to variable value */
258  start += name_len + 1;
259 
260  /* Point end to the end of the value */
261  value_end = (const char *)memchr(start, '&', end - start);
262  if(value_end == NULL) {
263  value_end = end;
264  }
265  *output = start;
266 
267  return value_end - start;
268  }
269  }
270  return 0;
271 }
272 /*---------------------------------------------------------------------------*/
273 /*- Internal API ------------------------------------------------------------*/
274 /*---------------------------------------------------------------------------*/
275 void
276 coap_init_connection(uint16_t port)
277 {
278  /* new connection with remote host */
279  udp_conn = udp_new(NULL, 0, NULL);
280  udp_bind(udp_conn, port);
281  PRINTF("Listening on port %u\n", uip_ntohs(udp_conn->lport));
282 
283  /* initialize transaction ID */
284  current_mid = random_rand();
285 }
286 /*---------------------------------------------------------------------------*/
287 uint16_t
288 coap_get_mid()
289 {
290  return ++current_mid;
291 }
292 /*---------------------------------------------------------------------------*/
293 void
294 coap_init_message(void *packet, coap_message_type_t type, uint8_t code,
295  uint16_t mid)
296 {
297  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
298 
299  /* Important thing */
300  memset(coap_pkt, 0, sizeof(coap_packet_t));
301 
302  coap_pkt->type = type;
303  coap_pkt->code = code;
304  coap_pkt->mid = mid;
305 }
306 /*---------------------------------------------------------------------------*/
307 size_t
308 coap_serialize_message(void *packet, uint8_t *buffer)
309 {
310  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
311  uint8_t *option;
312  unsigned int current_number = 0;
313 
314  /* Initialize */
315  coap_pkt->buffer = buffer;
316  coap_pkt->version = 1;
317 
318  PRINTF("-Serializing MID %u to %p, ", coap_pkt->mid, coap_pkt->buffer);
319 
320  /* set header fields */
321  coap_pkt->buffer[0] = 0x00;
322  coap_pkt->buffer[0] |= COAP_HEADER_VERSION_MASK
323  & (coap_pkt->version) << COAP_HEADER_VERSION_POSITION;
324  coap_pkt->buffer[0] |= COAP_HEADER_TYPE_MASK
325  & (coap_pkt->type) << COAP_HEADER_TYPE_POSITION;
326  coap_pkt->buffer[0] |= COAP_HEADER_TOKEN_LEN_MASK
327  & (coap_pkt->token_len) << COAP_HEADER_TOKEN_LEN_POSITION;
328  coap_pkt->buffer[1] = coap_pkt->code;
329  coap_pkt->buffer[2] = (uint8_t)((coap_pkt->mid) >> 8);
330  coap_pkt->buffer[3] = (uint8_t)(coap_pkt->mid);
331 
332  /* empty packet, dont need to do more stuff */
333  if(!coap_pkt->code) {
334  PRINTF("-Done serializing empty message at %p-\n", option);
335  return 4;
336  }
337 
338  /* set Token */
339  PRINTF("Token (len %u)", coap_pkt->token_len);
340  option = coap_pkt->buffer + COAP_HEADER_LEN;
341  for(current_number = 0; current_number < coap_pkt->token_len;
342  ++current_number) {
343  PRINTF(" %02X", coap_pkt->token[current_number]);
344  *option = coap_pkt->token[current_number];
345  ++option;
346  }
347  PRINTF("-\n");
348 
349  /* Serialize options */
350  current_number = 0;
351 
352  PRINTF("-Serializing options at %p-\n", option);
353 
354  /* The options must be serialized in the order of their number */
355  COAP_SERIALIZE_BYTE_OPTION(COAP_OPTION_IF_MATCH, if_match, "If-Match");
356  COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_HOST, uri_host, '\0',
357  "Uri-Host");
358  COAP_SERIALIZE_BYTE_OPTION(COAP_OPTION_ETAG, etag, "ETag");
359  COAP_SERIALIZE_INT_OPTION(COAP_OPTION_IF_NONE_MATCH,
360  content_format -
361  coap_pkt->
362  content_format /* hack to get a zero field */,
363  "If-None-Match");
364  COAP_SERIALIZE_INT_OPTION(COAP_OPTION_OBSERVE, observe, "Observe");
365  COAP_SERIALIZE_INT_OPTION(COAP_OPTION_URI_PORT, uri_port, "Uri-Port");
366  COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_LOCATION_PATH, location_path, '/',
367  "Location-Path");
368  COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_PATH, uri_path, '/',
369  "Uri-Path");
370  COAP_SERIALIZE_INT_OPTION(COAP_OPTION_CONTENT_FORMAT, content_format,
371  "Content-Format");
372  COAP_SERIALIZE_INT_OPTION(COAP_OPTION_MAX_AGE, max_age, "Max-Age");
373  COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_QUERY, uri_query, '&',
374  "Uri-Query");
375  COAP_SERIALIZE_INT_OPTION(COAP_OPTION_ACCEPT, accept, "Accept");
376  COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_LOCATION_QUERY, location_query,
377  '&', "Location-Query");
378  COAP_SERIALIZE_BLOCK_OPTION(COAP_OPTION_BLOCK2, block2, "Block2");
379  COAP_SERIALIZE_BLOCK_OPTION(COAP_OPTION_BLOCK1, block1, "Block1");
380  COAP_SERIALIZE_INT_OPTION(COAP_OPTION_SIZE2, size2, "Size2");
381  COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_PROXY_URI, proxy_uri, '\0',
382  "Proxy-Uri");
383  COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_PROXY_SCHEME, proxy_scheme, '\0',
384  "Proxy-Scheme");
385  COAP_SERIALIZE_INT_OPTION(COAP_OPTION_SIZE1, size1, "Size1");
386 
387  PRINTF("-Done serializing at %p----\n", option);
388 
389  /* Pack payload */
390  if((option - coap_pkt->buffer) <= COAP_MAX_HEADER_SIZE) {
391  /* Payload marker */
392  if(coap_pkt->payload_len) {
393  *option = 0xFF;
394  ++option;
395  }
396  memmove(option, coap_pkt->payload, coap_pkt->payload_len);
397  } else {
398  /* an error occurred: caller must check for !=0 */
399  coap_pkt->buffer = NULL;
400  coap_error_message = "Serialized header exceeds COAP_MAX_HEADER_SIZE";
401  return 0;
402  }
403 
404  PRINTF("-Done %u B (header len %u, payload len %u)-\n",
405  coap_pkt->payload_len + option - buffer, option - buffer,
406  coap_pkt->payload_len);
407 
408  PRINTF("Dump [0x%02X %02X %02X %02X %02X %02X %02X %02X]\n",
409  coap_pkt->buffer[0],
410  coap_pkt->buffer[1],
411  coap_pkt->buffer[2],
412  coap_pkt->buffer[3],
413  coap_pkt->buffer[4],
414  coap_pkt->buffer[5], coap_pkt->buffer[6], coap_pkt->buffer[7]
415  );
416 
417  return (option - buffer) + coap_pkt->payload_len; /* packet length */
418 }
419 /*---------------------------------------------------------------------------*/
420 void
421 coap_send_message(uip_ipaddr_t *addr, uint16_t port, uint8_t *data,
422  uint16_t length)
423 {
424  /* configure connection to reply to client */
425  uip_ipaddr_copy(&udp_conn->ripaddr, addr);
426  udp_conn->rport = port;
427 
428  uip_udp_packet_send(udp_conn, data, length);
429 
430  PRINTF("-sent UDP datagram (%u)-\n", length);
431 
432  /* restore server socket to allow data from any node */
433  memset(&udp_conn->ripaddr, 0, sizeof(udp_conn->ripaddr));
434  udp_conn->rport = 0;
435 }
436 /*---------------------------------------------------------------------------*/
437 coap_status_t
438 coap_parse_message(void *packet, uint8_t *data, uint16_t data_len)
439 {
440  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
441 
442  /* initialize packet */
443  memset(coap_pkt, 0, sizeof(coap_packet_t));
444 
445  /* pointer to packet bytes */
446  coap_pkt->buffer = data;
447 
448  /* parse header fields */
449  coap_pkt->version = (COAP_HEADER_VERSION_MASK & coap_pkt->buffer[0])
450  >> COAP_HEADER_VERSION_POSITION;
451  coap_pkt->type = (COAP_HEADER_TYPE_MASK & coap_pkt->buffer[0])
452  >> COAP_HEADER_TYPE_POSITION;
453  coap_pkt->token_len =
454  MIN(COAP_TOKEN_LEN,
455  (COAP_HEADER_TOKEN_LEN_MASK & coap_pkt->
456  buffer[0]) >> COAP_HEADER_TOKEN_LEN_POSITION);
457  coap_pkt->code = coap_pkt->buffer[1];
458  coap_pkt->mid = coap_pkt->buffer[2] << 8 | coap_pkt->buffer[3];
459 
460  if(coap_pkt->version != 1) {
461  coap_error_message = "CoAP version must be 1";
462  return BAD_REQUEST_4_00;
463  }
464 
465  uint8_t *current_option = data + COAP_HEADER_LEN;
466 
467  memcpy(coap_pkt->token, current_option, coap_pkt->token_len);
468  PRINTF("Token (len %u) [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n",
469  coap_pkt->token_len, coap_pkt->token[0], coap_pkt->token[1],
470  coap_pkt->token[2], coap_pkt->token[3], coap_pkt->token[4],
471  coap_pkt->token[5], coap_pkt->token[6], coap_pkt->token[7]
472  ); /*FIXME always prints 8 bytes */
473 
474  /* parse options */
475  memset(coap_pkt->options, 0, sizeof(coap_pkt->options));
476  current_option += coap_pkt->token_len;
477 
478  unsigned int option_number = 0;
479  unsigned int option_delta = 0;
480  size_t option_length = 0;
481 
482  while(current_option < data + data_len) {
483  /* payload marker 0xFF, currently only checking for 0xF* because rest is reserved */
484  if((current_option[0] & 0xF0) == 0xF0) {
485  coap_pkt->payload = ++current_option;
486  coap_pkt->payload_len = data_len - (coap_pkt->payload - data);
487 
488  /* also for receiving, the Erbium upper bound is REST_MAX_CHUNK_SIZE */
489  if(coap_pkt->payload_len > REST_MAX_CHUNK_SIZE) {
490  coap_pkt->payload_len = REST_MAX_CHUNK_SIZE;
491  /* null-terminate payload */
492  }
493  coap_pkt->payload[coap_pkt->payload_len] = '\0';
494 
495  break;
496  }
497 
498  option_delta = current_option[0] >> 4;
499  option_length = current_option[0] & 0x0F;
500  ++current_option;
501 
502  /* avoids code duplication without function overhead */
503  unsigned int *x = &option_delta;
504 
505  do {
506  if(*x == 13) {
507  *x += current_option[0];
508  ++current_option;
509  } else if(*x == 14) {
510  *x += 255;
511  *x += current_option[0] << 8;
512  ++current_option;
513  *x += current_option[0];
514  ++current_option;
515  }
516  } while(x != &option_length && (x = &option_length));
517 
518  option_number += option_delta;
519 
520  PRINTF("OPTION %u (delta %u, len %u): ", option_number, option_delta,
521  option_length);
522 
523  SET_OPTION(coap_pkt, option_number);
524 
525  switch(option_number) {
526  case COAP_OPTION_CONTENT_FORMAT:
527  coap_pkt->content_format = coap_parse_int_option(current_option,
528  option_length);
529  PRINTF("Content-Format [%u]\n", coap_pkt->content_format);
530  break;
531  case COAP_OPTION_MAX_AGE:
532  coap_pkt->max_age = coap_parse_int_option(current_option,
533  option_length);
534  PRINTF("Max-Age [%lu]\n", coap_pkt->max_age);
535  break;
536  case COAP_OPTION_ETAG:
537  coap_pkt->etag_len = MIN(COAP_ETAG_LEN, option_length);
538  memcpy(coap_pkt->etag, current_option, coap_pkt->etag_len);
539  PRINTF("ETag %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n",
540  coap_pkt->etag_len, coap_pkt->etag[0], coap_pkt->etag[1],
541  coap_pkt->etag[2], coap_pkt->etag[3], coap_pkt->etag[4],
542  coap_pkt->etag[5], coap_pkt->etag[6], coap_pkt->etag[7]
543  ); /*FIXME always prints 8 bytes */
544  break;
545  case COAP_OPTION_ACCEPT:
546  coap_pkt->accept = coap_parse_int_option(current_option, option_length);
547  PRINTF("Accept [%u]\n", coap_pkt->accept);
548  break;
549  case COAP_OPTION_IF_MATCH:
550  /* TODO support multiple ETags */
551  coap_pkt->if_match_len = MIN(COAP_ETAG_LEN, option_length);
552  memcpy(coap_pkt->if_match, current_option, coap_pkt->if_match_len);
553  PRINTF("If-Match %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n",
554  coap_pkt->if_match_len, coap_pkt->if_match[0],
555  coap_pkt->if_match[1], coap_pkt->if_match[2],
556  coap_pkt->if_match[3], coap_pkt->if_match[4],
557  coap_pkt->if_match[5], coap_pkt->if_match[6],
558  coap_pkt->if_match[7]
559  ); /* FIXME always prints 8 bytes */
560  break;
561  case COAP_OPTION_IF_NONE_MATCH:
562  coap_pkt->if_none_match = 1;
563  PRINTF("If-None-Match\n");
564  break;
565 
566  case COAP_OPTION_PROXY_URI:
567 #if COAP_PROXY_OPTION_PROCESSING
568  coap_pkt->proxy_uri = (char *)current_option;
569  coap_pkt->proxy_uri_len = option_length;
570 #endif
571  PRINTF("Proxy-Uri NOT IMPLEMENTED [%.*s]\n", coap_pkt->proxy_uri_len,
572  coap_pkt->proxy_uri);
573  coap_error_message = "This is a constrained server (Contiki)";
574  return PROXYING_NOT_SUPPORTED_5_05;
575  break;
576  case COAP_OPTION_PROXY_SCHEME:
577 #if COAP_PROXY_OPTION_PROCESSING
578  coap_pkt->proxy_scheme = (char *)current_option;
579  coap_pkt->proxy_scheme_len = option_length;
580 #endif
581  PRINTF("Proxy-Scheme NOT IMPLEMENTED [%.*s]\n",
582  coap_pkt->proxy_scheme_len, coap_pkt->proxy_scheme);
583  coap_error_message = "This is a constrained server (Contiki)";
584  return PROXYING_NOT_SUPPORTED_5_05;
585  break;
586 
587  case COAP_OPTION_URI_HOST:
588  coap_pkt->uri_host = (char *)current_option;
589  coap_pkt->uri_host_len = option_length;
590  PRINTF("Uri-Host [%.*s]\n", coap_pkt->uri_host_len, coap_pkt->uri_host);
591  break;
592  case COAP_OPTION_URI_PORT:
593  coap_pkt->uri_port = coap_parse_int_option(current_option,
594  option_length);
595  PRINTF("Uri-Port [%u]\n", coap_pkt->uri_port);
596  break;
597  case COAP_OPTION_URI_PATH:
598  /* coap_merge_multi_option() operates in-place on the IPBUF, but final packet field should be const string -> cast to string */
599  coap_merge_multi_option((char **)&(coap_pkt->uri_path),
600  &(coap_pkt->uri_path_len), current_option,
601  option_length, '/');
602  PRINTF("Uri-Path [%.*s]\n", coap_pkt->uri_path_len, coap_pkt->uri_path);
603  break;
604  case COAP_OPTION_URI_QUERY:
605  /* coap_merge_multi_option() operates in-place on the IPBUF, but final packet field should be const string -> cast to string */
606  coap_merge_multi_option((char **)&(coap_pkt->uri_query),
607  &(coap_pkt->uri_query_len), current_option,
608  option_length, '&');
609  PRINTF("Uri-Query [%.*s]\n", coap_pkt->uri_query_len,
610  coap_pkt->uri_query);
611  break;
612 
613  case COAP_OPTION_LOCATION_PATH:
614  /* coap_merge_multi_option() operates in-place on the IPBUF, but final packet field should be const string -> cast to string */
615  coap_merge_multi_option((char **)&(coap_pkt->location_path),
616  &(coap_pkt->location_path_len), current_option,
617  option_length, '/');
618  PRINTF("Location-Path [%.*s]\n", coap_pkt->location_path_len,
619  coap_pkt->location_path);
620  break;
621  case COAP_OPTION_LOCATION_QUERY:
622  /* coap_merge_multi_option() operates in-place on the IPBUF, but final packet field should be const string -> cast to string */
623  coap_merge_multi_option((char **)&(coap_pkt->location_query),
624  &(coap_pkt->location_query_len), current_option,
625  option_length, '&');
626  PRINTF("Location-Query [%.*s]\n", coap_pkt->location_query_len,
627  coap_pkt->location_query);
628  break;
629 
630  case COAP_OPTION_OBSERVE:
631  coap_pkt->observe = coap_parse_int_option(current_option,
632  option_length);
633  PRINTF("Observe [%lu]\n", coap_pkt->observe);
634  break;
635  case COAP_OPTION_BLOCK2:
636  coap_pkt->block2_num = coap_parse_int_option(current_option,
637  option_length);
638  coap_pkt->block2_more = (coap_pkt->block2_num & 0x08) >> 3;
639  coap_pkt->block2_size = 16 << (coap_pkt->block2_num & 0x07);
640  coap_pkt->block2_offset = (coap_pkt->block2_num & ~0x0000000F)
641  << (coap_pkt->block2_num & 0x07);
642  coap_pkt->block2_num >>= 4;
643  PRINTF("Block2 [%lu%s (%u B/blk)]\n", coap_pkt->block2_num,
644  coap_pkt->block2_more ? "+" : "", coap_pkt->block2_size);
645  break;
646  case COAP_OPTION_BLOCK1:
647  coap_pkt->block1_num = coap_parse_int_option(current_option,
648  option_length);
649  coap_pkt->block1_more = (coap_pkt->block1_num & 0x08) >> 3;
650  coap_pkt->block1_size = 16 << (coap_pkt->block1_num & 0x07);
651  coap_pkt->block1_offset = (coap_pkt->block1_num & ~0x0000000F)
652  << (coap_pkt->block1_num & 0x07);
653  coap_pkt->block1_num >>= 4;
654  PRINTF("Block1 [%lu%s (%u B/blk)]\n", coap_pkt->block1_num,
655  coap_pkt->block1_more ? "+" : "", coap_pkt->block1_size);
656  break;
657  case COAP_OPTION_SIZE2:
658  coap_pkt->size2 = coap_parse_int_option(current_option, option_length);
659  PRINTF("Size2 [%lu]\n", coap_pkt->size2);
660  break;
661  case COAP_OPTION_SIZE1:
662  coap_pkt->size1 = coap_parse_int_option(current_option, option_length);
663  PRINTF("Size1 [%lu]\n", coap_pkt->size1);
664  break;
665  default:
666  PRINTF("unknown (%u)\n", option_number);
667  /* check if critical (odd) */
668  if(option_number & 1) {
669  coap_error_message = "Unsupported critical option";
670  return BAD_OPTION_4_02;
671  }
672  }
673 
674  current_option += option_length;
675  } /* for */
676  PRINTF("-Done parsing-------\n");
677 
678  return NO_ERROR;
679 }
680 /*---------------------------------------------------------------------------*/
681 /*- REST Engine API ---------------------------------------------------------*/
682 /*---------------------------------------------------------------------------*/
683 int
684 coap_get_query_variable(void *packet, const char *name, const char **output)
685 {
686  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
687 
688  if(IS_OPTION(coap_pkt, COAP_OPTION_URI_QUERY)) {
689  return coap_get_variable(coap_pkt->uri_query, coap_pkt->uri_query_len,
690  name, output);
691  }
692  return 0;
693 }
694 int
695 coap_get_post_variable(void *packet, const char *name, const char **output)
696 {
697  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
698 
699  if(coap_pkt->payload_len) {
700  return coap_get_variable((const char *)coap_pkt->payload,
701  coap_pkt->payload_len, name, output);
702  }
703  return 0;
704 }
705 /*---------------------------------------------------------------------------*/
706 int
707 coap_set_status_code(void *packet, unsigned int code)
708 {
709  if(code <= 0xFF) {
710  ((coap_packet_t *)packet)->code = (uint8_t)code;
711  return 1;
712  } else {
713  return 0;
714  }
715 }
716 /*---------------------------------------------------------------------------*/
717 int
718 coap_set_token(void *packet, const uint8_t *token, size_t token_len)
719 {
720  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
721 
722  coap_pkt->token_len = MIN(COAP_TOKEN_LEN, token_len);
723  memcpy(coap_pkt->token, token, coap_pkt->token_len);
724 
725  return coap_pkt->token_len;
726 }
727 /*---------------------------------------------------------------------------*/
728 /*- CoAP REST Implementation API --------------------------------------------*/
729 /*---------------------------------------------------------------------------*/
730 int
731 coap_get_header_content_format(void *packet, unsigned int *format)
732 {
733  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
734 
735  if(!IS_OPTION(coap_pkt, COAP_OPTION_CONTENT_FORMAT)) {
736  return 0;
737  }
738  *format = coap_pkt->content_format;
739  return 1;
740 }
741 int
742 coap_set_header_content_format(void *packet, unsigned int format)
743 {
744  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
745 
746  coap_pkt->content_format = (coap_content_format_t)format;
747  SET_OPTION(coap_pkt, COAP_OPTION_CONTENT_FORMAT);
748  return 1;
749 }
750 /*---------------------------------------------------------------------------*/
751 int
752 coap_get_header_accept(void *packet, unsigned int *accept)
753 {
754  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
755 
756  if(!IS_OPTION(coap_pkt, COAP_OPTION_ACCEPT)) {
757  return 0;
758  }
759  *accept = coap_pkt->accept;
760  return 1;
761 }
762 int
763 coap_set_header_accept(void *packet, unsigned int accept)
764 {
765  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
766 
767  coap_pkt->accept = (coap_content_format_t)accept;
768  SET_OPTION(coap_pkt, COAP_OPTION_ACCEPT);
769  return 1;
770 }
771 /*---------------------------------------------------------------------------*/
772 int
773 coap_get_header_max_age(void *packet, uint32_t *age)
774 {
775  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
776 
777  if(!IS_OPTION(coap_pkt, COAP_OPTION_MAX_AGE)) {
778  *age = COAP_DEFAULT_MAX_AGE;
779  } else {
780  *age = coap_pkt->max_age;
781  } return 1;
782 }
783 int
784 coap_set_header_max_age(void *packet, uint32_t age)
785 {
786  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
787 
788  coap_pkt->max_age = age;
789  SET_OPTION(coap_pkt, COAP_OPTION_MAX_AGE);
790  return 1;
791 }
792 /*---------------------------------------------------------------------------*/
793 int
794 coap_get_header_etag(void *packet, const uint8_t **etag)
795 {
796  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
797 
798  if(!IS_OPTION(coap_pkt, COAP_OPTION_ETAG)) {
799  return 0;
800  }
801  *etag = coap_pkt->etag;
802  return coap_pkt->etag_len;
803 }
804 int
805 coap_set_header_etag(void *packet, const uint8_t *etag, size_t etag_len)
806 {
807  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
808 
809  coap_pkt->etag_len = MIN(COAP_ETAG_LEN, etag_len);
810  memcpy(coap_pkt->etag, etag, coap_pkt->etag_len);
811 
812  SET_OPTION(coap_pkt, COAP_OPTION_ETAG);
813  return coap_pkt->etag_len;
814 }
815 /*---------------------------------------------------------------------------*/
816 /*FIXME support multiple ETags */
817 int
818 coap_get_header_if_match(void *packet, const uint8_t **etag)
819 {
820  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
821 
822  if(!IS_OPTION(coap_pkt, COAP_OPTION_IF_MATCH)) {
823  return 0;
824  }
825  *etag = coap_pkt->if_match;
826  return coap_pkt->if_match_len;
827 }
828 int
829 coap_set_header_if_match(void *packet, const uint8_t *etag, size_t etag_len)
830 {
831  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
832 
833  coap_pkt->if_match_len = MIN(COAP_ETAG_LEN, etag_len);
834  memcpy(coap_pkt->if_match, etag, coap_pkt->if_match_len);
835 
836  SET_OPTION(coap_pkt, COAP_OPTION_IF_MATCH);
837  return coap_pkt->if_match_len;
838 }
839 /*---------------------------------------------------------------------------*/
840 int
841 coap_get_header_if_none_match(void *packet)
842 {
843  return IS_OPTION((coap_packet_t *)packet,
844  COAP_OPTION_IF_NONE_MATCH) ? 1 : 0;
845 }
846 int
847 coap_set_header_if_none_match(void *packet)
848 {
849  SET_OPTION((coap_packet_t *)packet, COAP_OPTION_IF_NONE_MATCH);
850  return 1;
851 }
852 /*---------------------------------------------------------------------------*/
853 int
854 coap_get_header_proxy_uri(void *packet, const char **uri)
855 {
856  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
857 
858  if(!IS_OPTION(coap_pkt, COAP_OPTION_PROXY_URI)) {
859  return 0;
860  }
861  *uri = coap_pkt->proxy_uri;
862  return coap_pkt->proxy_uri_len;
863 }
864 int
865 coap_set_header_proxy_uri(void *packet, const char *uri)
866 {
867  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
868 
869  /*TODO Provide alternative that sets Proxy-Scheme and Uri-* options and provide er-coap-conf define */
870 
871  coap_pkt->proxy_uri = uri;
872  coap_pkt->proxy_uri_len = strlen(uri);
873 
874  SET_OPTION(coap_pkt, COAP_OPTION_PROXY_URI);
875  return coap_pkt->proxy_uri_len;
876 }
877 /*---------------------------------------------------------------------------*/
878 int
879 coap_get_header_uri_host(void *packet, const char **host)
880 {
881  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
882 
883  if(!IS_OPTION(coap_pkt, COAP_OPTION_URI_HOST)) {
884  return 0;
885  }
886  *host = coap_pkt->uri_host;
887  return coap_pkt->uri_host_len;
888 }
889 int
890 coap_set_header_uri_host(void *packet, const char *host)
891 {
892  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
893 
894  coap_pkt->uri_host = host;
895  coap_pkt->uri_host_len = strlen(host);
896 
897  SET_OPTION(coap_pkt, COAP_OPTION_URI_HOST);
898  return coap_pkt->uri_host_len;
899 }
900 /*---------------------------------------------------------------------------*/
901 int
902 coap_get_header_uri_path(void *packet, const char **path)
903 {
904  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
905 
906  if(!IS_OPTION(coap_pkt, COAP_OPTION_URI_PATH)) {
907  return 0;
908  }
909  *path = coap_pkt->uri_path;
910  return coap_pkt->uri_path_len;
911 }
912 int
913 coap_set_header_uri_path(void *packet, const char *path)
914 {
915  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
916 
917  while(path[0] == '/')
918  ++path;
919 
920  coap_pkt->uri_path = path;
921  coap_pkt->uri_path_len = strlen(path);
922 
923  SET_OPTION(coap_pkt, COAP_OPTION_URI_PATH);
924  return coap_pkt->uri_path_len;
925 }
926 /*---------------------------------------------------------------------------*/
927 int
928 coap_get_header_uri_query(void *packet, const char **query)
929 {
930  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
931 
932  if(!IS_OPTION(coap_pkt, COAP_OPTION_URI_QUERY)) {
933  return 0;
934  }
935  *query = coap_pkt->uri_query;
936  return coap_pkt->uri_query_len;
937 }
938 int
939 coap_set_header_uri_query(void *packet, const char *query)
940 {
941  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
942 
943  while(query[0] == '?')
944  ++query;
945 
946  coap_pkt->uri_query = query;
947  coap_pkt->uri_query_len = strlen(query);
948 
949  SET_OPTION(coap_pkt, COAP_OPTION_URI_QUERY);
950  return coap_pkt->uri_query_len;
951 }
952 /*---------------------------------------------------------------------------*/
953 int
954 coap_get_header_location_path(void *packet, const char **path)
955 {
956  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
957 
958  if(!IS_OPTION(coap_pkt, COAP_OPTION_LOCATION_PATH)) {
959  return 0;
960  }
961  *path = coap_pkt->location_path;
962  return coap_pkt->location_path_len;
963 }
964 int
965 coap_set_header_location_path(void *packet, const char *path)
966 {
967  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
968 
969  char *query;
970 
971  while(path[0] == '/')
972  ++path;
973 
974  if((query = strchr(path, '?'))) {
975  coap_set_header_location_query(packet, query + 1);
976  coap_pkt->location_path_len = query - path;
977  } else {
978  coap_pkt->location_path_len = strlen(path);
979  } coap_pkt->location_path = path;
980 
981  if(coap_pkt->location_path_len > 0) {
982  SET_OPTION(coap_pkt, COAP_OPTION_LOCATION_PATH);
983  }
984  return coap_pkt->location_path_len;
985 }
986 /*---------------------------------------------------------------------------*/
987 int
988 coap_get_header_location_query(void *packet, const char **query)
989 {
990  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
991 
992  if(!IS_OPTION(coap_pkt, COAP_OPTION_LOCATION_QUERY)) {
993  return 0;
994  }
995  *query = coap_pkt->location_query;
996  return coap_pkt->location_query_len;
997 }
998 int
999 coap_set_header_location_query(void *packet, const char *query)
1000 {
1001  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
1002 
1003  while(query[0] == '?')
1004  ++query;
1005 
1006  coap_pkt->location_query = query;
1007  coap_pkt->location_query_len = strlen(query);
1008 
1009  SET_OPTION(coap_pkt, COAP_OPTION_LOCATION_QUERY);
1010  return coap_pkt->location_query_len;
1011 }
1012 /*---------------------------------------------------------------------------*/
1013 int
1014 coap_get_header_observe(void *packet, uint32_t *observe)
1015 {
1016  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
1017 
1018  if(!IS_OPTION(coap_pkt, COAP_OPTION_OBSERVE)) {
1019  return 0;
1020  }
1021  *observe = coap_pkt->observe;
1022  return 1;
1023 }
1024 int
1025 coap_set_header_observe(void *packet, uint32_t observe)
1026 {
1027  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
1028 
1029  coap_pkt->observe = observe;
1030  SET_OPTION(coap_pkt, COAP_OPTION_OBSERVE);
1031  return 1;
1032 }
1033 /*---------------------------------------------------------------------------*/
1034 int
1035 coap_get_header_block2(void *packet, uint32_t *num, uint8_t *more,
1036  uint16_t *size, uint32_t *offset)
1037 {
1038  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
1039 
1040  if(!IS_OPTION(coap_pkt, COAP_OPTION_BLOCK2)) {
1041  return 0;
1042  }
1043  /* pointers may be NULL to get only specific block parameters */
1044  if(num != NULL) {
1045  *num = coap_pkt->block2_num;
1046  }
1047  if(more != NULL) {
1048  *more = coap_pkt->block2_more;
1049  }
1050  if(size != NULL) {
1051  *size = coap_pkt->block2_size;
1052  }
1053  if(offset != NULL) {
1054  *offset = coap_pkt->block2_offset;
1055  }
1056  return 1;
1057 }
1058 int
1059 coap_set_header_block2(void *packet, uint32_t num, uint8_t more,
1060  uint16_t size)
1061 {
1062  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
1063 
1064  if(size < 16) {
1065  return 0;
1066  }
1067  if(size > 2048) {
1068  return 0;
1069  }
1070  if(num > 0x0FFFFF) {
1071  return 0;
1072  }
1073  coap_pkt->block2_num = num;
1074  coap_pkt->block2_more = more ? 1 : 0;
1075  coap_pkt->block2_size = size;
1076 
1077  SET_OPTION(coap_pkt, COAP_OPTION_BLOCK2);
1078  return 1;
1079 }
1080 /*---------------------------------------------------------------------------*/
1081 int
1082 coap_get_header_block1(void *packet, uint32_t *num, uint8_t *more,
1083  uint16_t *size, uint32_t *offset)
1084 {
1085  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
1086 
1087  if(!IS_OPTION(coap_pkt, COAP_OPTION_BLOCK1)) {
1088  return 0;
1089  }
1090  /* pointers may be NULL to get only specific block parameters */
1091  if(num != NULL) {
1092  *num = coap_pkt->block1_num;
1093  }
1094  if(more != NULL) {
1095  *more = coap_pkt->block1_more;
1096  }
1097  if(size != NULL) {
1098  *size = coap_pkt->block1_size;
1099  }
1100  if(offset != NULL) {
1101  *offset = coap_pkt->block1_offset;
1102  }
1103  return 1;
1104 }
1105 int
1106 coap_set_header_block1(void *packet, uint32_t num, uint8_t more,
1107  uint16_t size)
1108 {
1109  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
1110 
1111  if(size < 16) {
1112  return 0;
1113  }
1114  if(size > 2048) {
1115  return 0;
1116  }
1117  if(num > 0x0FFFFF) {
1118  return 0;
1119  }
1120  coap_pkt->block1_num = num;
1121  coap_pkt->block1_more = more;
1122  coap_pkt->block1_size = size;
1123 
1124  SET_OPTION(coap_pkt, COAP_OPTION_BLOCK1);
1125  return 1;
1126 }
1127 /*---------------------------------------------------------------------------*/
1128 int
1129 coap_get_header_size2(void *packet, uint32_t *size)
1130 {
1131  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
1132 
1133  if(!IS_OPTION(coap_pkt, COAP_OPTION_SIZE2)) {
1134  return 0;
1135  }
1136  *size = coap_pkt->size2;
1137  return 1;
1138 }
1139 int
1140 coap_set_header_size2(void *packet, uint32_t size)
1141 {
1142  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
1143 
1144  coap_pkt->size2 = size;
1145  SET_OPTION(coap_pkt, COAP_OPTION_SIZE2);
1146  return 1;
1147 }
1148 /*---------------------------------------------------------------------------*/
1149 int
1150 coap_get_header_size1(void *packet, uint32_t *size)
1151 {
1152  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
1153 
1154  if(!IS_OPTION(coap_pkt, COAP_OPTION_SIZE1)) {
1155  return 0;
1156  }
1157  *size = coap_pkt->size1;
1158  return 1;
1159 }
1160 int
1161 coap_set_header_size1(void *packet, uint32_t size)
1162 {
1163  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
1164 
1165  coap_pkt->size1 = size;
1166  SET_OPTION(coap_pkt, COAP_OPTION_SIZE1);
1167  return 1;
1168 }
1169 /*---------------------------------------------------------------------------*/
1170 int
1171 coap_get_payload(void *packet, const uint8_t **payload)
1172 {
1173  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
1174 
1175  if(coap_pkt->payload) {
1176  *payload = coap_pkt->payload;
1177  return coap_pkt->payload_len;
1178  } else {
1179  *payload = NULL;
1180  return 0;
1181  }
1182 }
1183 int
1184 coap_set_payload(void *packet, const void *payload, size_t length)
1185 {
1186  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
1187 
1188  coap_pkt->payload = (uint8_t *)payload;
1189  coap_pkt->payload_len = MIN(REST_MAX_CHUNK_SIZE, length);
1190 
1191  return coap_pkt->payload_len;
1192 }
1193 /*---------------------------------------------------------------------------*/
uint16_t rport
The remote port number in network byte order.
Definition: uip.h:1397
CCIF struct uip_udp_conn * udp_new(const uip_ipaddr_t *ripaddr, uint16_t port, void *appstate)
Create a new UDP connection.
#define NULL
The null pointer.
uint16_t lport
The local port number in network byte order.
Definition: uip.h:1396
An implementation of the Constrained Application Protocol (RFC).
uip_ipaddr_t ripaddr
The IP address of the remote peer.
Definition: uip.h:1395
CoAP module for reliable transport
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1026
#define udp_bind(conn, port)
Bind a UDP connection to a local port.
Definition: tcpip.h:262
Representation of a uIP UDP connection.
Definition: uip.h:1394
unsigned short random_rand(void)
Generate the next state and return the upper part of it.
Definition: random.c:47