52#include "lib/random.h"
59#define LOG_MODULE "coap"
60#define LOG_LEVEL LOG_LEVEL_COAP
65static uint16_t current_mid = 0;
67coap_status_t coap_status_code = NO_ERROR;
68#if COAP_MESSAGE_ON_ERROR
69const char *coap_error_message =
"";
75coap_log_2(uint16_t value)
84 return result ? result - 1 : result;
88coap_parse_int_option(
const uint8_t *bytes,
size_t length)
101coap_option_nibble(
unsigned int value)
105 }
else if(value <= 0xFF + 13) {
113coap_set_option_header(
unsigned int delta,
size_t length, uint8_t *buffer)
117 buffer[0] = coap_option_nibble(delta) << 4 | coap_option_nibble(length);
120 buffer[++written] = ((delta - 269) >> 8) & 0xff;
121 buffer[++written] = (delta - 269) & 0xff;
122 }
else if(delta > 12) {
123 buffer[++written] = (delta - 13);
127 buffer[++written] = ((length - 269) >> 8) & 0xff;
128 buffer[++written] = (length - 269) & 0xff;
129 }
else if(length > 12) {
130 buffer[++written] = (length - 13);
133 LOG_DBG(
"WRITTEN %zu B opt header\n", 1 + written);
139coap_serialize_int_option(
unsigned int number,
unsigned int current_number,
140 uint8_t *buffer, uint32_t value)
144 if(0xFF000000 & value) {
147 if(0xFFFF0000 & value) {
150 if(0xFFFFFF00 & value) {
153 if(0xFFFFFFFF & value) {
156 LOG_DBG(
"OPTION %u (delta %u, len %zu)\n", number, number - current_number,
159 i = coap_set_option_header(number - current_number, i, buffer);
161 if(0xFF000000 & value) {
162 buffer[i++] = (uint8_t)(value >> 24);
164 if(0xFFFF0000 & value) {
165 buffer[i++] = (uint8_t)(value >> 16);
167 if(0xFFFFFF00 & value) {
168 buffer[i++] = (uint8_t)(value >> 8);
170 if(0xFFFFFFFF & value) {
171 buffer[i++] = (uint8_t)(value);
177coap_serialize_array_option(
unsigned int number,
unsigned int current_number,
178 uint8_t *buffer, uint8_t *array,
size_t length,
183 LOG_DBG(
"ARRAY type %u, len %zu, full [", number, length);
184 LOG_DBG_COAP_STRING((
const char *)array, length);
187 if(split_char !=
'\0') {
189 uint8_t *part_start = array;
190 uint8_t *part_end = NULL;
193 for(j = 0; j <= length + 1; ++j) {
194 LOG_DBG(
"STEP %u/%zu (%c)\n", j, length, array[j]);
195 if(array[j] == split_char || j == length) {
196 part_end = array + j;
197 temp_length = part_end - part_start;
199 i += coap_set_option_header(number - current_number, temp_length,
201 memcpy(&buffer[i], part_start, temp_length);
204 LOG_DBG(
"OPTION type %u, delta %u, len %zu, part [", number,
205 number - current_number, i);
206 LOG_DBG_COAP_STRING((
const char *)part_start, temp_length);
209 current_number = number;
210 part_start = array + j;
214 i += coap_set_option_header(number - current_number, length, &buffer[i]);
215 memcpy(&buffer[i], array, length);
218 LOG_DBG(
"OPTION type %u, delta %u, len %zu\n", number,
219 number - current_number, length);
226coap_merge_multi_option(
char **dst,
size_t *dst_len, uint8_t *option,
227 size_t option_len,
char separator)
232 (*dst)[*dst_len] = separator;
236 memmove((*dst) + (*dst_len), option, option_len);
238 *dst_len += option_len;
241 *dst = (
char *)option;
242 *dst_len = option_len;
247coap_get_variable(
const char *buffer,
size_t length,
const char *name,
250 const char *
start = NULL;
251 const char *end = NULL;
252 const char *value_end = NULL;
258 name_len = strlen(name);
259 end = buffer + length;
263 && strncmp(name,
start, name_len) == 0) {
266 start += name_len + 1;
269 value_end = (
const char *)memchr(
start,
'&', end -
start);
270 if(value_end == NULL) {
275 return value_end -
start;
284coap_init_connection(
void)
293 return ++current_mid;
297coap_init_message(coap_message_t *coap_pkt, coap_message_type_t type,
298 uint8_t code, uint16_t mid)
301 memset(coap_pkt, 0,
sizeof(coap_message_t));
303 coap_pkt->type = type;
304 coap_pkt->code = code;
309coap_serialize_message(coap_message_t *coap_pkt, uint8_t *buffer)
312 unsigned int current_number = 0;
315 coap_pkt->buffer = buffer;
316 coap_pkt->version = 1;
318 LOG_DBG(
"-Serializing MID %u to %p, ", coap_pkt->mid, coap_pkt->buffer);
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);
333 if(!coap_pkt->code) {
334 LOG_DBG_(
"-Done serializing empty message at %p-\n", coap_pkt->buffer);
339 LOG_DBG_(
"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;
343 LOG_DBG_(
" %02X", coap_pkt->token[current_number]);
344 *option = coap_pkt->token[current_number];
352 LOG_DBG(
"-Serializing options at %p-\n", option);
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',
358 COAP_SERIALIZE_BYTE_OPTION(COAP_OPTION_ETAG, etag,
"ETag");
359 COAP_SERIALIZE_INT_OPTION(COAP_OPTION_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,
'/',
368 COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_PATH, uri_path,
'/',
370 LOG_DBG(
"Serialize content format: %d\n", coap_pkt->content_format);
371 COAP_SERIALIZE_INT_OPTION(COAP_OPTION_CONTENT_FORMAT, content_format,
373 COAP_SERIALIZE_INT_OPTION(COAP_OPTION_MAX_AGE, max_age,
"Max-Age");
374 COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_QUERY, uri_query,
'&',
376 COAP_SERIALIZE_INT_OPTION(COAP_OPTION_ACCEPT,
accept,
"Accept");
377 COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_LOCATION_QUERY, location_query,
378 '&',
"Location-Query");
379 COAP_SERIALIZE_BLOCK_OPTION(COAP_OPTION_BLOCK2, block2,
"Block2");
380 COAP_SERIALIZE_BLOCK_OPTION(COAP_OPTION_BLOCK1, block1,
"Block1");
381 COAP_SERIALIZE_INT_OPTION(COAP_OPTION_SIZE2, size2,
"Size2");
382 COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_PROXY_URI, proxy_uri,
'\0',
384 COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_PROXY_SCHEME, proxy_scheme,
'\0',
386 COAP_SERIALIZE_INT_OPTION(COAP_OPTION_SIZE1, size1,
"Size1");
388 LOG_DBG(
"-Done serializing at %p----\n", option);
391 if((option - coap_pkt->buffer) <= COAP_MAX_HEADER_SIZE) {
393 if(coap_pkt->payload_len) {
397 memmove(option, coap_pkt->payload, coap_pkt->payload_len);
400 coap_pkt->buffer = NULL;
401#if COAP_MESSAGE_ON_ERROR
402 coap_error_message =
"Serialized header exceeds COAP_MAX_HEADER_SIZE";
407 LOG_DBG(
"-Done %u B (header len %u, payload len %u)-\n",
408 (
unsigned int)(coap_pkt->payload_len + option - buffer),
409 (
unsigned int)(option - buffer),
410 (
unsigned int)coap_pkt->payload_len);
412 LOG_DBG(
"Dump [0x%02X %02X %02X %02X %02X %02X %02X %02X]\n",
413 coap_pkt->buffer[0], coap_pkt->buffer[1], coap_pkt->buffer[2],
414 coap_pkt->buffer[3], coap_pkt->buffer[4], coap_pkt->buffer[5],
415 coap_pkt->buffer[6], coap_pkt->buffer[7]);
417 return (option - buffer) + coap_pkt->payload_len;
421coap_parse_message(coap_message_t *coap_pkt, uint8_t *data, uint16_t data_len)
423#define CHECK_OPTION_BOUNDARY(byte_count) \
425 if(current_option + (byte_count) > data + data_len) { \
426 LOG_WARN("BAD REQUEST: option delta outside buffer (%u > %u)\n", \
427 (unsigned)(current_option + (byte_count) - data), \
429 return BAD_REQUEST_4_00; \
433 if(data_len < COAP_HEADER_LEN) {
435 LOG_WARN(
"BAD REQUEST: message too short\n");
436 return BAD_REQUEST_4_00;
440 memset(coap_pkt, 0,
sizeof(coap_message_t));
443 coap_pkt->buffer = data;
446 coap_pkt->version = (COAP_HEADER_VERSION_MASK & coap_pkt->buffer[0])
447 >> COAP_HEADER_VERSION_POSITION;
448 coap_pkt->type = (COAP_HEADER_TYPE_MASK & coap_pkt->buffer[0])
449 >> COAP_HEADER_TYPE_POSITION;
450 coap_pkt->token_len = (COAP_HEADER_TOKEN_LEN_MASK & coap_pkt->buffer[0])
451 >> COAP_HEADER_TOKEN_LEN_POSITION;
452 coap_pkt->code = coap_pkt->buffer[1];
453 coap_pkt->mid = coap_pkt->buffer[2] << 8 | coap_pkt->buffer[3];
455 if(coap_pkt->version != 1) {
456#if COAP_MESSAGE_ON_ERROR
457 coap_error_message =
"CoAP version must be 1";
459 return BAD_REQUEST_4_00;
462 if(coap_pkt->token_len > COAP_TOKEN_LEN) {
463#if COAP_MESSAGE_ON_ERROR
464 coap_error_message =
"Token Length must not be more than 8";
466 return BAD_REQUEST_4_00;
469 uint8_t *current_option = data + COAP_HEADER_LEN;
470 CHECK_OPTION_BOUNDARY(coap_pkt->token_len);
472 memcpy(coap_pkt->token, current_option, coap_pkt->token_len);
473 LOG_DBG(
"Token (len %u) [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n",
474 coap_pkt->token_len, coap_pkt->token[0], coap_pkt->token[1],
475 coap_pkt->token[2], coap_pkt->token[3], coap_pkt->token[4],
476 coap_pkt->token[5], coap_pkt->token[6], coap_pkt->token[7]
480 memset(coap_pkt->options, 0,
sizeof(coap_pkt->options));
481 current_option += coap_pkt->token_len;
483 unsigned int option_number = 0;
484 unsigned int option_delta = 0;
485 size_t option_length = 0;
487 while(current_option < data + data_len) {
489 if((current_option[0] & 0xF0) == 0xF0) {
490 coap_pkt->payload = ++current_option;
491 coap_pkt->payload_len = data_len - (coap_pkt->payload - data);
494 if(coap_pkt->payload_len > COAP_MAX_CHUNK_SIZE) {
495 coap_pkt->payload_len = COAP_MAX_CHUNK_SIZE;
498 coap_pkt->payload[coap_pkt->payload_len] =
'\0';
503 option_delta = current_option[0] >> 4;
504 option_length = current_option[0] & 0x0F;
507 if(option_delta == 13) {
508 CHECK_OPTION_BOUNDARY(1);
509 option_delta += current_option[0];
511 }
else if(option_delta == 14) {
512 CHECK_OPTION_BOUNDARY(2);
514 option_delta += current_option[0] << 8;
516 option_delta += current_option[0];
520 if(option_length == 13) {
521 CHECK_OPTION_BOUNDARY(1);
522 option_length += current_option[0];
524 }
else if(option_length == 14) {
525 CHECK_OPTION_BOUNDARY(2);
526 option_length += 255;
527 option_length += current_option[0] << 8;
529 option_length += current_option[0];
533 CHECK_OPTION_BOUNDARY(option_length);
534 option_number += option_delta;
536 if(option_number > COAP_OPTION_SIZE1) {
538 LOG_WARN(
"BAD REQUEST: option number too large: %u\n", option_number);
539 return BAD_REQUEST_4_00;
542 LOG_DBG(
"OPTION %u (delta %u, len %zu): ", option_number, option_delta,
545 coap_set_option(coap_pkt, option_number);
547 switch(option_number) {
548 case COAP_OPTION_CONTENT_FORMAT:
549 coap_pkt->content_format = coap_parse_int_option(current_option,
551 LOG_DBG_(
"Content-Format [%u]\n", coap_pkt->content_format);
553 case COAP_OPTION_MAX_AGE:
554 coap_pkt->max_age = coap_parse_int_option(current_option,
556 LOG_DBG_(
"Max-Age [%"PRIu32
"]\n", coap_pkt->max_age);
558 case COAP_OPTION_ETAG:
559 coap_pkt->etag_len = MIN(COAP_ETAG_LEN, option_length);
560 memcpy(coap_pkt->etag, current_option, coap_pkt->etag_len);
561 LOG_DBG_(
"ETag %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n",
562 coap_pkt->etag_len, coap_pkt->etag[0], coap_pkt->etag[1],
563 coap_pkt->etag[2], coap_pkt->etag[3], coap_pkt->etag[4],
564 coap_pkt->etag[5], coap_pkt->etag[6], coap_pkt->etag[7]
567 case COAP_OPTION_ACCEPT:
568 coap_pkt->accept = coap_parse_int_option(current_option, option_length);
569 LOG_DBG_(
"Accept [%u]\n", coap_pkt->accept);
571 case COAP_OPTION_IF_MATCH:
573 coap_pkt->if_match_len = MIN(COAP_ETAG_LEN, option_length);
574 memcpy(coap_pkt->if_match, current_option, coap_pkt->if_match_len);
575 LOG_DBG_(
"If-Match %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n",
576 coap_pkt->if_match_len, coap_pkt->if_match[0],
577 coap_pkt->if_match[1], coap_pkt->if_match[2],
578 coap_pkt->if_match[3], coap_pkt->if_match[4],
579 coap_pkt->if_match[5], coap_pkt->if_match[6],
580 coap_pkt->if_match[7]
583 case COAP_OPTION_IF_NONE_MATCH:
584 coap_pkt->if_none_match = 1;
585 LOG_DBG_(
"If-None-Match\n");
588 case COAP_OPTION_PROXY_URI:
589#if COAP_PROXY_OPTION_PROCESSING
590 coap_pkt->proxy_uri = (
char *)current_option;
591 coap_pkt->proxy_uri_len = option_length;
592 LOG_DBG_(
"Proxy-Uri NOT IMPLEMENTED [");
593 LOG_DBG_COAP_STRING(coap_pkt->proxy_uri, coap_pkt->proxy_uri_len);
596#if COAP_MESSAGE_ON_ERROR
597 coap_error_message =
"This is a constrained server (Contiki-NG)";
599 return PROXYING_NOT_SUPPORTED_5_05;
601 case COAP_OPTION_PROXY_SCHEME:
602#if COAP_PROXY_OPTION_PROCESSING
603 coap_pkt->proxy_scheme = (
char *)current_option;
604 coap_pkt->proxy_scheme_len = option_length;
605 LOG_DBG_(
"Proxy-Scheme NOT IMPLEMENTED [");
606 LOG_DBG_COAP_STRING(coap_pkt->proxy_scheme, coap_pkt->proxy_scheme_len);
609#if COAP_MESSAGE_ON_ERROR
610 coap_error_message =
"This is a constrained server (Contiki-NG)";
612 return PROXYING_NOT_SUPPORTED_5_05;
615 case COAP_OPTION_URI_HOST:
616 coap_pkt->uri_host = (
char *)current_option;
617 coap_pkt->uri_host_len = option_length;
618 LOG_DBG_(
"Uri-Host [");
619 LOG_DBG_COAP_STRING(coap_pkt->uri_host, coap_pkt->uri_host_len);
622 case COAP_OPTION_URI_PORT:
623 coap_pkt->uri_port = coap_parse_int_option(current_option,
625 LOG_DBG_(
"Uri-Port [%u]\n", coap_pkt->uri_port);
627 case COAP_OPTION_URI_PATH:
629 coap_merge_multi_option((
char **)&(coap_pkt->uri_path),
630 &(coap_pkt->uri_path_len), current_option,
632 LOG_DBG_(
"Uri-Path [");
633 LOG_DBG_COAP_STRING(coap_pkt->uri_path, coap_pkt->uri_path_len);
636 case COAP_OPTION_URI_QUERY:
638 coap_merge_multi_option((
char **)&(coap_pkt->uri_query),
639 &(coap_pkt->uri_query_len), current_option,
641 LOG_DBG_(
"Uri-Query[");
642 LOG_DBG_COAP_STRING(coap_pkt->uri_query, coap_pkt->uri_query_len);
646 case COAP_OPTION_LOCATION_PATH:
648 coap_merge_multi_option((
char **)&(coap_pkt->location_path),
649 &(coap_pkt->location_path_len), current_option,
652 LOG_DBG_(
"Location-Path [");
653 LOG_DBG_COAP_STRING(coap_pkt->location_path, coap_pkt->location_path_len);
656 case COAP_OPTION_LOCATION_QUERY:
658 coap_merge_multi_option((
char **)&(coap_pkt->location_query),
659 &(coap_pkt->location_query_len), current_option,
661 LOG_DBG_(
"Location-Query [");
662 LOG_DBG_COAP_STRING(coap_pkt->location_query, coap_pkt->location_query_len);
666 case COAP_OPTION_OBSERVE:
667 coap_pkt->observe = coap_parse_int_option(current_option,
669 LOG_DBG_(
"Observe [%"PRId32
"]\n", coap_pkt->observe);
671 case COAP_OPTION_BLOCK2:
672 coap_pkt->block2_num = coap_parse_int_option(current_option,
674 coap_pkt->block2_more = (coap_pkt->block2_num & 0x08) >> 3;
675 coap_pkt->block2_size = 16 << (coap_pkt->block2_num & 0x07);
676 coap_pkt->block2_offset = (coap_pkt->block2_num & ~0x0000000F)
677 << (coap_pkt->block2_num & 0x07);
678 coap_pkt->block2_num >>= 4;
679 LOG_DBG_(
"Block2 [%lu%s (%u B/blk)]\n",
680 (
unsigned long)coap_pkt->block2_num,
681 coap_pkt->block2_more ?
"+" :
"", coap_pkt->block2_size);
683 case COAP_OPTION_BLOCK1:
684 coap_pkt->block1_num = coap_parse_int_option(current_option,
686 coap_pkt->block1_more = (coap_pkt->block1_num & 0x08) >> 3;
687 coap_pkt->block1_size = 16 << (coap_pkt->block1_num & 0x07);
688 coap_pkt->block1_offset = (coap_pkt->block1_num & ~0x0000000F)
689 << (coap_pkt->block1_num & 0x07);
690 coap_pkt->block1_num >>= 4;
691 LOG_DBG_(
"Block1 [%lu%s (%u B/blk)]\n",
692 (
unsigned long)coap_pkt->block1_num,
693 coap_pkt->block1_more ?
"+" :
"", coap_pkt->block1_size);
695 case COAP_OPTION_SIZE2:
696 coap_pkt->size2 = coap_parse_int_option(current_option, option_length);
697 LOG_DBG_(
"Size2 [%"PRIu32
"]\n", coap_pkt->size2);
699 case COAP_OPTION_SIZE1:
700 coap_pkt->size1 = coap_parse_int_option(current_option, option_length);
701 LOG_DBG_(
"Size1 [%"PRIu32
"]\n", coap_pkt->size1);
704 LOG_DBG_(
"unknown (%u)\n", option_number);
706 if(option_number & 1) {
707#if COAP_MESSAGE_ON_ERROR
708 coap_error_message =
"Unsupported critical option";
710 return BAD_OPTION_4_02;
714 current_option += option_length;
716 LOG_DBG(
"-Done parsing-------\n");
724coap_get_query_variable(
const coap_message_t *coap_pkt,
725 const char *name,
const char **output)
727 if(coap_is_option(coap_pkt, COAP_OPTION_URI_QUERY)) {
728 return coap_get_variable(coap_pkt->uri_query, coap_pkt->uri_query_len,
734coap_get_post_variable(
const coap_message_t *coap_pkt,
735 const char *name,
const char **output)
737 if(coap_pkt->payload_len) {
738 return coap_get_variable((
const char *)coap_pkt->payload,
739 coap_pkt->payload_len, name, output);
745coap_set_status_code(coap_message_t *message,
unsigned int code)
748 message->code = (uint8_t)code;
756coap_set_token(coap_message_t *coap_pkt,
const uint8_t *token,
size_t token_len)
758 coap_pkt->token_len = MIN(COAP_TOKEN_LEN, token_len);
759 memcpy(coap_pkt->token, token, coap_pkt->token_len);
761 return coap_pkt->token_len;
767coap_get_header_content_format(
const coap_message_t *coap_pkt,
768 unsigned int *format)
770 if(!coap_is_option(coap_pkt, COAP_OPTION_CONTENT_FORMAT)) {
773 *format = coap_pkt->content_format;
777coap_set_header_content_format(coap_message_t *coap_pkt,
unsigned int format)
779 coap_pkt->content_format = format;
780 coap_set_option(coap_pkt, COAP_OPTION_CONTENT_FORMAT);
785coap_get_header_accept(
const coap_message_t *coap_pkt,
unsigned int *
accept)
787 if(!coap_is_option(coap_pkt, COAP_OPTION_ACCEPT)) {
790 *
accept = coap_pkt->accept;
794coap_set_header_accept(coap_message_t *coap_pkt,
unsigned int accept)
796 coap_pkt->accept =
accept;
797 coap_set_option(coap_pkt, COAP_OPTION_ACCEPT);
802coap_get_header_max_age(
const coap_message_t *coap_pkt, uint32_t *age)
804 if(!coap_is_option(coap_pkt, COAP_OPTION_MAX_AGE)) {
805 *age = COAP_DEFAULT_MAX_AGE;
807 *age = coap_pkt->max_age;
811coap_set_header_max_age(coap_message_t *coap_pkt, uint32_t age)
813 coap_pkt->max_age = age;
814 coap_set_option(coap_pkt, COAP_OPTION_MAX_AGE);
819coap_get_header_etag(
const coap_message_t *coap_pkt,
const uint8_t **etag)
821 if(!coap_is_option(coap_pkt, COAP_OPTION_ETAG)) {
824 *etag = coap_pkt->etag;
825 return coap_pkt->etag_len;
828coap_set_header_etag(coap_message_t *coap_pkt,
const uint8_t *etag,
size_t etag_len)
830 coap_pkt->etag_len = MIN(COAP_ETAG_LEN, etag_len);
831 memcpy(coap_pkt->etag, etag, coap_pkt->etag_len);
833 coap_set_option(coap_pkt, COAP_OPTION_ETAG);
834 return coap_pkt->etag_len;
839coap_get_header_if_match(
const coap_message_t *coap_pkt,
const uint8_t **etag)
841 if(!coap_is_option(coap_pkt, COAP_OPTION_IF_MATCH)) {
844 *etag = coap_pkt->if_match;
845 return coap_pkt->if_match_len;
848coap_set_header_if_match(coap_message_t *coap_pkt,
const uint8_t *etag,
size_t etag_len)
850 coap_pkt->if_match_len = MIN(COAP_ETAG_LEN, etag_len);
851 memcpy(coap_pkt->if_match, etag, coap_pkt->if_match_len);
853 coap_set_option(coap_pkt, COAP_OPTION_IF_MATCH);
854 return coap_pkt->if_match_len;
858coap_get_header_if_none_match(
const coap_message_t *message)
860 return coap_is_option(message, COAP_OPTION_IF_NONE_MATCH) ? 1 : 0;
863coap_set_header_if_none_match(coap_message_t *message)
865 coap_set_option(message, COAP_OPTION_IF_NONE_MATCH);
870coap_get_header_proxy_uri(
const coap_message_t *coap_pkt,
const char **uri)
872 if(!coap_is_option(coap_pkt, COAP_OPTION_PROXY_URI)) {
875 *uri = coap_pkt->proxy_uri;
876 return coap_pkt->proxy_uri_len;
879coap_set_header_proxy_uri(coap_message_t *coap_pkt,
const char *uri)
883 coap_pkt->proxy_uri = uri;
884 coap_pkt->proxy_uri_len = strlen(uri);
886 coap_set_option(coap_pkt, COAP_OPTION_PROXY_URI);
887 return coap_pkt->proxy_uri_len;
891coap_get_header_uri_host(
const coap_message_t *coap_pkt,
const char **host)
893 if(!coap_is_option(coap_pkt, COAP_OPTION_URI_HOST)) {
896 *host = coap_pkt->uri_host;
897 return coap_pkt->uri_host_len;
900coap_set_header_uri_host(coap_message_t *coap_pkt,
const char *host)
902 coap_pkt->uri_host = host;
903 coap_pkt->uri_host_len = strlen(host);
905 coap_set_option(coap_pkt, COAP_OPTION_URI_HOST);
906 return coap_pkt->uri_host_len;
910coap_get_header_uri_path(
const coap_message_t *coap_pkt,
const char **path)
912 if(!coap_is_option(coap_pkt, COAP_OPTION_URI_PATH)) {
915 *path = coap_pkt->uri_path;
916 return coap_pkt->uri_path_len;
919coap_set_header_uri_path(coap_message_t *coap_pkt,
const char *path)
921 while(path[0] ==
'/') {
925 coap_pkt->uri_path = path;
926 coap_pkt->uri_path_len = strlen(path);
928 coap_set_option(coap_pkt, COAP_OPTION_URI_PATH);
929 return coap_pkt->uri_path_len;
933coap_get_header_uri_query(
const coap_message_t *coap_pkt,
936 if(!coap_is_option(coap_pkt, COAP_OPTION_URI_QUERY)) {
939 *query = coap_pkt->uri_query;
940 return coap_pkt->uri_query_len;
943coap_set_header_uri_query(coap_message_t *coap_pkt,
const char *query)
945 while(query[0] ==
'?') {
949 coap_pkt->uri_query = query;
950 coap_pkt->uri_query_len = strlen(query);
952 coap_set_option(coap_pkt, COAP_OPTION_URI_QUERY);
953 return coap_pkt->uri_query_len;
957coap_get_header_location_path(
const coap_message_t *coap_pkt,
960 if(!coap_is_option(coap_pkt, COAP_OPTION_LOCATION_PATH)) {
963 *path = coap_pkt->location_path;
964 return coap_pkt->location_path_len;
967coap_set_header_location_path(coap_message_t *coap_pkt,
const char *path)
971 while(path[0] ==
'/') {
975 if((query = strchr(path,
'?'))) {
976 coap_set_header_location_query(coap_pkt, query + 1);
977 coap_pkt->location_path_len = query - path;
979 coap_pkt->location_path_len = strlen(path);
980 } coap_pkt->location_path = path;
982 if(coap_pkt->location_path_len > 0) {
983 coap_set_option(coap_pkt, COAP_OPTION_LOCATION_PATH);
985 return coap_pkt->location_path_len;
989coap_get_header_location_query(
const coap_message_t *coap_pkt,
992 if(!coap_is_option(coap_pkt, COAP_OPTION_LOCATION_QUERY)) {
995 *query = coap_pkt->location_query;
996 return coap_pkt->location_query_len;
999coap_set_header_location_query(coap_message_t *coap_pkt,
const char *query)
1001 while(query[0] ==
'?') {
1005 coap_pkt->location_query = query;
1006 coap_pkt->location_query_len = strlen(query);
1008 coap_set_option(coap_pkt, COAP_OPTION_LOCATION_QUERY);
1009 return coap_pkt->location_query_len;
1013coap_get_header_observe(
const coap_message_t *coap_pkt, uint32_t *observe)
1015 if(!coap_is_option(coap_pkt, COAP_OPTION_OBSERVE)) {
1018 *observe = coap_pkt->observe;
1022coap_set_header_observe(coap_message_t *coap_pkt, uint32_t observe)
1024 coap_pkt->observe = observe;
1025 coap_set_option(coap_pkt, COAP_OPTION_OBSERVE);
1030coap_get_header_block2(
const coap_message_t *coap_pkt, uint32_t *num,
1031 uint8_t *more, uint16_t *size, uint32_t *offset)
1033 if(!coap_is_option(coap_pkt, COAP_OPTION_BLOCK2)) {
1038 *num = coap_pkt->block2_num;
1041 *more = coap_pkt->block2_more;
1044 *size = coap_pkt->block2_size;
1046 if(offset != NULL) {
1047 *offset = coap_pkt->block2_offset;
1052coap_set_header_block2(coap_message_t *coap_pkt, uint32_t num, uint8_t more,
1061 if(num > 0x0FFFFF) {
1064 coap_pkt->block2_num = num;
1065 coap_pkt->block2_more = more ? 1 : 0;
1066 coap_pkt->block2_size = size;
1068 coap_set_option(coap_pkt, COAP_OPTION_BLOCK2);
1073coap_get_header_block1(
const coap_message_t *coap_pkt, uint32_t *num,
1074 uint8_t *more, uint16_t *size, uint32_t *offset)
1076 if(!coap_is_option(coap_pkt, COAP_OPTION_BLOCK1)) {
1081 *num = coap_pkt->block1_num;
1084 *more = coap_pkt->block1_more;
1087 *size = coap_pkt->block1_size;
1089 if(offset != NULL) {
1090 *offset = coap_pkt->block1_offset;
1095coap_set_header_block1(coap_message_t *coap_pkt, uint32_t num, uint8_t more,
1104 if(num > 0x0FFFFF) {
1107 coap_pkt->block1_num = num;
1108 coap_pkt->block1_more = more;
1109 coap_pkt->block1_size = size;
1111 coap_set_option(coap_pkt, COAP_OPTION_BLOCK1);
1116coap_get_header_size2(
const coap_message_t *coap_pkt, uint32_t *size)
1118 if(!coap_is_option(coap_pkt, COAP_OPTION_SIZE2)) {
1121 *size = coap_pkt->size2;
1125coap_set_header_size2(coap_message_t *coap_pkt, uint32_t size)
1127 coap_pkt->size2 = size;
1128 coap_set_option(coap_pkt, COAP_OPTION_SIZE2);
1133coap_get_header_size1(
const coap_message_t *coap_pkt, uint32_t *size)
1135 if(!coap_is_option(coap_pkt, COAP_OPTION_SIZE1)) {
1138 *size = coap_pkt->size1;
1142coap_set_header_size1(coap_message_t *coap_pkt, uint32_t size)
1144 coap_pkt->size1 = size;
1145 coap_set_option(coap_pkt, COAP_OPTION_SIZE1);
1150coap_get_payload(
const coap_message_t *coap_pkt,
const uint8_t **payload)
1152 if(payload != NULL) {
1153 *payload = coap_pkt->payload;
1155 return coap_pkt->payload != NULL ? coap_pkt->payload_len : 0;
1158coap_set_payload(coap_message_t *coap_pkt,
const void *payload,
size_t length)
1160 coap_pkt->payload = (uint8_t *)payload;
1161 coap_pkt->payload_len = MIN(COAP_MAX_CHUNK_SIZE, length);
1163 return coap_pkt->payload_len;
Default definitions of C compiler quirk work-arounds.
CoAP module for reliable transport.
An implementation of the Constrained Application Protocol (RFC 7252).
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
static uint8_t accept(uint8_t in)
static void start(void)
Start measurement.