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)
287 current_mid = random_rand();
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;
488 uint32_t option_number = 0;
489 uint32_t option_delta = 0;
490 uint32_t option_length = 0;
492 while(current_option < data + data_len) {
497 if(current_option[0] == 0xFF) {
499 if(current_option >= data + data_len) {
500 LOG_WARN(
"BAD REQUEST: payload marker with zero-length payload\n");
501 return BAD_REQUEST_4_00;
503 coap_pkt->payload = current_option;
504 coap_pkt->payload_len = data_len - (coap_pkt->payload - data);
507 if(coap_pkt->payload_len > COAP_MAX_CHUNK_SIZE) {
508 coap_pkt->payload_len = COAP_MAX_CHUNK_SIZE;
511 coap_pkt->payload[coap_pkt->payload_len] =
'\0';
516 option_delta = current_option[0] >> 4;
517 option_length = current_option[0] & 0x0F;
520 if(option_delta == 15 || option_length == 15) {
522 LOG_WARN(
"BAD REQUEST: reserved option delta/length nibble 15\n");
523 return BAD_REQUEST_4_00;
526 if(option_delta == 13) {
527 CHECK_OPTION_BOUNDARY(1);
528 option_delta += current_option[0];
530 }
else if(option_delta == 14) {
531 CHECK_OPTION_BOUNDARY(2);
533 option_delta += (uint32_t)current_option[0] << 8;
535 option_delta += current_option[0];
539 if(option_length == 13) {
540 CHECK_OPTION_BOUNDARY(1);
541 option_length += current_option[0];
543 }
else if(option_length == 14) {
544 CHECK_OPTION_BOUNDARY(2);
545 option_length += 255;
546 option_length += (uint32_t)current_option[0] << 8;
548 option_length += current_option[0];
552 CHECK_OPTION_BOUNDARY(option_length);
553 option_number += option_delta;
555 if(option_number > UINT16_MAX) {
557 LOG_WARN(
"BAD REQUEST: option number out of range: %"PRIu32
"\n",
559 return BAD_REQUEST_4_00;
562 LOG_DBG(
"OPTION %"PRIu32
" (delta %"PRIu32
", len %"PRIu32
"): ",
563 option_number, option_delta, option_length);
565 coap_set_option(coap_pkt, option_number);
567 switch(option_number) {
568 case COAP_OPTION_CONTENT_FORMAT:
569 coap_pkt->content_format = coap_parse_int_option(current_option,
571 LOG_DBG_(
"Content-Format [%u]\n", coap_pkt->content_format);
573 case COAP_OPTION_MAX_AGE:
574 coap_pkt->max_age = coap_parse_int_option(current_option,
576 LOG_DBG_(
"Max-Age [%"PRIu32
"]\n", coap_pkt->max_age);
578 case COAP_OPTION_ETAG:
579 coap_pkt->etag_len = MIN(COAP_ETAG_LEN, option_length);
580 memcpy(coap_pkt->etag, current_option, coap_pkt->etag_len);
581 LOG_DBG_(
"ETag %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n",
582 coap_pkt->etag_len, coap_pkt->etag[0], coap_pkt->etag[1],
583 coap_pkt->etag[2], coap_pkt->etag[3], coap_pkt->etag[4],
584 coap_pkt->etag[5], coap_pkt->etag[6], coap_pkt->etag[7]
587 case COAP_OPTION_ACCEPT:
588 coap_pkt->accept = coap_parse_int_option(current_option, option_length);
589 LOG_DBG_(
"Accept [%u]\n", coap_pkt->accept);
591 case COAP_OPTION_IF_MATCH:
593 coap_pkt->if_match_len = MIN(COAP_ETAG_LEN, option_length);
594 memcpy(coap_pkt->if_match, current_option, coap_pkt->if_match_len);
595 LOG_DBG_(
"If-Match %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n",
596 coap_pkt->if_match_len, coap_pkt->if_match[0],
597 coap_pkt->if_match[1], coap_pkt->if_match[2],
598 coap_pkt->if_match[3], coap_pkt->if_match[4],
599 coap_pkt->if_match[5], coap_pkt->if_match[6],
600 coap_pkt->if_match[7]
603 case COAP_OPTION_IF_NONE_MATCH:
604 coap_pkt->if_none_match = 1;
605 LOG_DBG_(
"If-None-Match\n");
608 case COAP_OPTION_PROXY_URI:
609#if COAP_PROXY_OPTION_PROCESSING
610 coap_pkt->proxy_uri = (
char *)current_option;
611 coap_pkt->proxy_uri_len = option_length;
612 LOG_DBG_(
"Proxy-Uri NOT IMPLEMENTED [");
613 LOG_DBG_COAP_STRING(coap_pkt->proxy_uri, coap_pkt->proxy_uri_len);
616#if COAP_MESSAGE_ON_ERROR
617 coap_error_message =
"This is a constrained server (Contiki-NG)";
619 return PROXYING_NOT_SUPPORTED_5_05;
621 case COAP_OPTION_PROXY_SCHEME:
622#if COAP_PROXY_OPTION_PROCESSING
623 coap_pkt->proxy_scheme = (
char *)current_option;
624 coap_pkt->proxy_scheme_len = option_length;
625 LOG_DBG_(
"Proxy-Scheme NOT IMPLEMENTED [");
626 LOG_DBG_COAP_STRING(coap_pkt->proxy_scheme, coap_pkt->proxy_scheme_len);
629#if COAP_MESSAGE_ON_ERROR
630 coap_error_message =
"This is a constrained server (Contiki-NG)";
632 return PROXYING_NOT_SUPPORTED_5_05;
635 case COAP_OPTION_URI_HOST:
636 coap_pkt->uri_host = (
char *)current_option;
637 coap_pkt->uri_host_len = option_length;
638 LOG_DBG_(
"Uri-Host [");
639 LOG_DBG_COAP_STRING(coap_pkt->uri_host, coap_pkt->uri_host_len);
642 case COAP_OPTION_URI_PORT:
643 coap_pkt->uri_port = coap_parse_int_option(current_option,
645 LOG_DBG_(
"Uri-Port [%u]\n", coap_pkt->uri_port);
647 case COAP_OPTION_URI_PATH:
649 coap_merge_multi_option((
char **)&(coap_pkt->uri_path),
650 &(coap_pkt->uri_path_len), current_option,
652 LOG_DBG_(
"Uri-Path [");
653 LOG_DBG_COAP_STRING(coap_pkt->uri_path, coap_pkt->uri_path_len);
656 case COAP_OPTION_URI_QUERY:
658 coap_merge_multi_option((
char **)&(coap_pkt->uri_query),
659 &(coap_pkt->uri_query_len), current_option,
661 LOG_DBG_(
"Uri-Query[");
662 LOG_DBG_COAP_STRING(coap_pkt->uri_query, coap_pkt->uri_query_len);
666 case COAP_OPTION_LOCATION_PATH:
668 coap_merge_multi_option((
char **)&(coap_pkt->location_path),
669 &(coap_pkt->location_path_len), current_option,
672 LOG_DBG_(
"Location-Path [");
673 LOG_DBG_COAP_STRING(coap_pkt->location_path, coap_pkt->location_path_len);
676 case COAP_OPTION_LOCATION_QUERY:
678 coap_merge_multi_option((
char **)&(coap_pkt->location_query),
679 &(coap_pkt->location_query_len), current_option,
681 LOG_DBG_(
"Location-Query [");
682 LOG_DBG_COAP_STRING(coap_pkt->location_query, coap_pkt->location_query_len);
686 case COAP_OPTION_OBSERVE:
687 coap_pkt->observe = coap_parse_int_option(current_option,
689 LOG_DBG_(
"Observe [%"PRId32
"]\n", coap_pkt->observe);
691 case COAP_OPTION_BLOCK2:
692 coap_pkt->block2_num = coap_parse_int_option(current_option,
694 coap_pkt->block2_more = (coap_pkt->block2_num & 0x08) >> 3;
695 coap_pkt->block2_size = 16 << (coap_pkt->block2_num & 0x07);
696 coap_pkt->block2_offset = (coap_pkt->block2_num & ~0x0000000F)
697 << (coap_pkt->block2_num & 0x07);
698 coap_pkt->block2_num >>= 4;
699 LOG_DBG_(
"Block2 [%lu%s (%u B/blk)]\n",
700 (
unsigned long)coap_pkt->block2_num,
701 coap_pkt->block2_more ?
"+" :
"", coap_pkt->block2_size);
703 case COAP_OPTION_BLOCK1:
704 coap_pkt->block1_num = coap_parse_int_option(current_option,
706 coap_pkt->block1_more = (coap_pkt->block1_num & 0x08) >> 3;
707 coap_pkt->block1_size = 16 << (coap_pkt->block1_num & 0x07);
708 coap_pkt->block1_offset = (coap_pkt->block1_num & ~0x0000000F)
709 << (coap_pkt->block1_num & 0x07);
710 coap_pkt->block1_num >>= 4;
711 LOG_DBG_(
"Block1 [%lu%s (%u B/blk)]\n",
712 (
unsigned long)coap_pkt->block1_num,
713 coap_pkt->block1_more ?
"+" :
"", coap_pkt->block1_size);
715 case COAP_OPTION_SIZE2:
716 coap_pkt->size2 = coap_parse_int_option(current_option, option_length);
717 LOG_DBG_(
"Size2 [%"PRIu32
"]\n", coap_pkt->size2);
719 case COAP_OPTION_SIZE1:
720 coap_pkt->size1 = coap_parse_int_option(current_option, option_length);
721 LOG_DBG_(
"Size1 [%"PRIu32
"]\n", coap_pkt->size1);
724 LOG_DBG_(
"unknown (%"PRIu32
")\n", option_number);
726 if(option_number & 1) {
727#if COAP_MESSAGE_ON_ERROR
728 coap_error_message =
"Unsupported critical option";
730 return BAD_OPTION_4_02;
734 current_option += option_length;
736 LOG_DBG(
"-Done parsing-------\n");
744coap_get_query_variable(
const coap_message_t *coap_pkt,
745 const char *name,
const char **output)
747 if(coap_is_option(coap_pkt, COAP_OPTION_URI_QUERY)) {
748 return coap_get_variable(coap_pkt->uri_query, coap_pkt->uri_query_len,
754coap_get_post_variable(
const coap_message_t *coap_pkt,
755 const char *name,
const char **output)
757 if(coap_pkt->payload_len) {
758 return coap_get_variable((
const char *)coap_pkt->payload,
759 coap_pkt->payload_len, name, output);
765coap_set_status_code(coap_message_t *message,
unsigned int code)
768 message->code = (uint8_t)code;
776coap_set_token(coap_message_t *coap_pkt,
const uint8_t *token,
size_t token_len)
778 coap_pkt->token_len = MIN(COAP_TOKEN_LEN, token_len);
779 memcpy(coap_pkt->token, token, coap_pkt->token_len);
781 return coap_pkt->token_len;
787coap_get_header_content_format(
const coap_message_t *coap_pkt,
788 unsigned int *format)
790 if(!coap_is_option(coap_pkt, COAP_OPTION_CONTENT_FORMAT)) {
793 *format = coap_pkt->content_format;
797coap_set_header_content_format(coap_message_t *coap_pkt,
unsigned int format)
799 coap_pkt->content_format = format;
800 coap_set_option(coap_pkt, COAP_OPTION_CONTENT_FORMAT);
805coap_get_header_accept(
const coap_message_t *coap_pkt,
unsigned int *
accept)
807 if(!coap_is_option(coap_pkt, COAP_OPTION_ACCEPT)) {
810 *
accept = coap_pkt->accept;
814coap_set_header_accept(coap_message_t *coap_pkt,
unsigned int accept)
816 coap_pkt->accept =
accept;
817 coap_set_option(coap_pkt, COAP_OPTION_ACCEPT);
822coap_get_header_max_age(
const coap_message_t *coap_pkt, uint32_t *age)
824 if(!coap_is_option(coap_pkt, COAP_OPTION_MAX_AGE)) {
825 *age = COAP_DEFAULT_MAX_AGE;
827 *age = coap_pkt->max_age;
831coap_set_header_max_age(coap_message_t *coap_pkt, uint32_t age)
833 coap_pkt->max_age = age;
834 coap_set_option(coap_pkt, COAP_OPTION_MAX_AGE);
839coap_get_header_etag(
const coap_message_t *coap_pkt,
const uint8_t **etag)
841 if(!coap_is_option(coap_pkt, COAP_OPTION_ETAG)) {
844 *etag = coap_pkt->etag;
845 return coap_pkt->etag_len;
848coap_set_header_etag(coap_message_t *coap_pkt,
const uint8_t *etag,
size_t etag_len)
850 coap_pkt->etag_len = MIN(COAP_ETAG_LEN, etag_len);
851 memcpy(coap_pkt->etag, etag, coap_pkt->etag_len);
853 coap_set_option(coap_pkt, COAP_OPTION_ETAG);
854 return coap_pkt->etag_len;
859coap_get_header_if_match(
const coap_message_t *coap_pkt,
const uint8_t **etag)
861 if(!coap_is_option(coap_pkt, COAP_OPTION_IF_MATCH)) {
864 *etag = coap_pkt->if_match;
865 return coap_pkt->if_match_len;
868coap_set_header_if_match(coap_message_t *coap_pkt,
const uint8_t *etag,
size_t etag_len)
870 coap_pkt->if_match_len = MIN(COAP_ETAG_LEN, etag_len);
871 memcpy(coap_pkt->if_match, etag, coap_pkt->if_match_len);
873 coap_set_option(coap_pkt, COAP_OPTION_IF_MATCH);
874 return coap_pkt->if_match_len;
878coap_get_header_if_none_match(
const coap_message_t *message)
880 return coap_is_option(message, COAP_OPTION_IF_NONE_MATCH) ? 1 : 0;
883coap_set_header_if_none_match(coap_message_t *message)
885 coap_set_option(message, COAP_OPTION_IF_NONE_MATCH);
890coap_get_header_proxy_uri(
const coap_message_t *coap_pkt,
const char **uri)
892 if(!coap_is_option(coap_pkt, COAP_OPTION_PROXY_URI)) {
895 *uri = coap_pkt->proxy_uri;
896 return coap_pkt->proxy_uri_len;
899coap_set_header_proxy_uri(coap_message_t *coap_pkt,
const char *uri)
903 coap_pkt->proxy_uri = uri;
904 coap_pkt->proxy_uri_len = strlen(uri);
906 coap_set_option(coap_pkt, COAP_OPTION_PROXY_URI);
907 return coap_pkt->proxy_uri_len;
911coap_get_header_uri_host(
const coap_message_t *coap_pkt,
const char **host)
913 if(!coap_is_option(coap_pkt, COAP_OPTION_URI_HOST)) {
916 *host = coap_pkt->uri_host;
917 return coap_pkt->uri_host_len;
920coap_set_header_uri_host(coap_message_t *coap_pkt,
const char *host)
922 coap_pkt->uri_host = host;
923 coap_pkt->uri_host_len = strlen(host);
925 coap_set_option(coap_pkt, COAP_OPTION_URI_HOST);
926 return coap_pkt->uri_host_len;
930coap_get_header_uri_path(
const coap_message_t *coap_pkt,
const char **path)
932 if(!coap_is_option(coap_pkt, COAP_OPTION_URI_PATH)) {
935 *path = coap_pkt->uri_path;
936 return coap_pkt->uri_path_len;
939coap_set_header_uri_path(coap_message_t *coap_pkt,
const char *path)
941 while(path[0] ==
'/') {
945 coap_pkt->uri_path = path;
946 coap_pkt->uri_path_len = strlen(path);
948 coap_set_option(coap_pkt, COAP_OPTION_URI_PATH);
949 return coap_pkt->uri_path_len;
953coap_get_header_uri_query(
const coap_message_t *coap_pkt,
956 if(!coap_is_option(coap_pkt, COAP_OPTION_URI_QUERY)) {
959 *query = coap_pkt->uri_query;
960 return coap_pkt->uri_query_len;
963coap_set_header_uri_query(coap_message_t *coap_pkt,
const char *query)
965 while(query[0] ==
'?') {
969 coap_pkt->uri_query = query;
970 coap_pkt->uri_query_len = strlen(query);
972 coap_set_option(coap_pkt, COAP_OPTION_URI_QUERY);
973 return coap_pkt->uri_query_len;
977coap_get_header_location_path(
const coap_message_t *coap_pkt,
980 if(!coap_is_option(coap_pkt, COAP_OPTION_LOCATION_PATH)) {
983 *path = coap_pkt->location_path;
984 return coap_pkt->location_path_len;
987coap_set_header_location_path(coap_message_t *coap_pkt,
const char *path)
991 while(path[0] ==
'/') {
995 if((query = strchr(path,
'?'))) {
996 coap_set_header_location_query(coap_pkt, query + 1);
997 coap_pkt->location_path_len = query - path;
999 coap_pkt->location_path_len = strlen(path);
1000 } coap_pkt->location_path = path;
1002 if(coap_pkt->location_path_len > 0) {
1003 coap_set_option(coap_pkt, COAP_OPTION_LOCATION_PATH);
1005 return coap_pkt->location_path_len;
1009coap_get_header_location_query(
const coap_message_t *coap_pkt,
1012 if(!coap_is_option(coap_pkt, COAP_OPTION_LOCATION_QUERY)) {
1015 *query = coap_pkt->location_query;
1016 return coap_pkt->location_query_len;
1019coap_set_header_location_query(coap_message_t *coap_pkt,
const char *query)
1021 while(query[0] ==
'?') {
1025 coap_pkt->location_query = query;
1026 coap_pkt->location_query_len = strlen(query);
1028 coap_set_option(coap_pkt, COAP_OPTION_LOCATION_QUERY);
1029 return coap_pkt->location_query_len;
1033coap_get_header_observe(
const coap_message_t *coap_pkt, uint32_t *observe)
1035 if(!coap_is_option(coap_pkt, COAP_OPTION_OBSERVE)) {
1038 *observe = coap_pkt->observe;
1042coap_set_header_observe(coap_message_t *coap_pkt, uint32_t observe)
1044 coap_pkt->observe = observe;
1045 coap_set_option(coap_pkt, COAP_OPTION_OBSERVE);
1050coap_get_header_block2(
const coap_message_t *coap_pkt, uint32_t *num,
1051 uint8_t *more, uint16_t *size, uint32_t *offset)
1053 if(!coap_is_option(coap_pkt, COAP_OPTION_BLOCK2)) {
1058 *num = coap_pkt->block2_num;
1061 *more = coap_pkt->block2_more;
1064 *size = coap_pkt->block2_size;
1066 if(offset != NULL) {
1067 *offset = coap_pkt->block2_offset;
1072coap_set_header_block2(coap_message_t *coap_pkt, uint32_t num, uint8_t more,
1081 if(num > 0x0FFFFF) {
1084 coap_pkt->block2_num = num;
1085 coap_pkt->block2_more = more ? 1 : 0;
1086 coap_pkt->block2_size = size;
1088 coap_set_option(coap_pkt, COAP_OPTION_BLOCK2);
1093coap_get_header_block1(
const coap_message_t *coap_pkt, uint32_t *num,
1094 uint8_t *more, uint16_t *size, uint32_t *offset)
1096 if(!coap_is_option(coap_pkt, COAP_OPTION_BLOCK1)) {
1101 *num = coap_pkt->block1_num;
1104 *more = coap_pkt->block1_more;
1107 *size = coap_pkt->block1_size;
1109 if(offset != NULL) {
1110 *offset = coap_pkt->block1_offset;
1115coap_set_header_block1(coap_message_t *coap_pkt, uint32_t num, uint8_t more,
1124 if(num > 0x0FFFFF) {
1127 coap_pkt->block1_num = num;
1128 coap_pkt->block1_more = more;
1129 coap_pkt->block1_size = size;
1131 coap_set_option(coap_pkt, COAP_OPTION_BLOCK1);
1136coap_get_header_size2(
const coap_message_t *coap_pkt, uint32_t *size)
1138 if(!coap_is_option(coap_pkt, COAP_OPTION_SIZE2)) {
1141 *size = coap_pkt->size2;
1145coap_set_header_size2(coap_message_t *coap_pkt, uint32_t size)
1147 coap_pkt->size2 = size;
1148 coap_set_option(coap_pkt, COAP_OPTION_SIZE2);
1153coap_get_header_size1(
const coap_message_t *coap_pkt, uint32_t *size)
1155 if(!coap_is_option(coap_pkt, COAP_OPTION_SIZE1)) {
1158 *size = coap_pkt->size1;
1162coap_set_header_size1(coap_message_t *coap_pkt, uint32_t size)
1164 coap_pkt->size1 = size;
1165 coap_set_option(coap_pkt, COAP_OPTION_SIZE1);
1170coap_get_payload(
const coap_message_t *coap_pkt,
const uint8_t **payload)
1172 if(payload != NULL) {
1173 *payload = coap_pkt->payload;
1175 return coap_pkt->payload != NULL ? coap_pkt->payload_len : 0;
1178coap_set_payload(coap_message_t *coap_pkt,
const void *payload,
size_t length)
1180 coap_pkt->payload = (uint8_t *)payload;
1181 coap_pkt->payload_len = MIN(COAP_MAX_CHUNK_SIZE, length);
1183 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).
static uint8_t accept(uint8_t in)
static void start(void)
Start measurement.
Header file for generating non-cryptographic random numbers.