18#if defined(HAVE_LIMITS_H)
25#ifdef HAVE_ARPA_INET_H
40#define min(a,b) ((a) < (b) ? (a) : (b))
44#define max(a,b) ((a) > (b) ? (a) : (b))
73coap_pdu_from_pbuf(
struct pbuf *pbuf )
77 if (pbuf == NULL)
return NULL;
79 LWIP_ASSERT(
"Can only deal with contiguous PBUFs (increase PBUF_POOL_BUFSIZE)", pbuf->tot_len == pbuf->len);
80 LWIP_ASSERT(
"coap_io_do_io needs to receive an exclusive copy of the incoming pbuf", pbuf->ref == 1);
104 assert(code <= 0xff);
105 assert(mid >= 0 && mid <= 0xffff);
110 if (memp_pools[MEMP_COAP_PDU]->stats->used + 1 >=
111 memp_pools[MEMP_COAP_PDU]->stats->avail) {
112 memp_pools[MEMP_COAP_PDU]->stats->err++;
118 if (!pdu)
return NULL;
120#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
130 pdu->pbuf = pbuf_alloc(PBUF_TRANSPORT, size + pdu->
max_hdr_size, PBUF_RAM);
131 if (pdu->pbuf == NULL) {
159 coap_log_crit(
"coap_new_pdu: cannot allocate memory for new PDU\n");
167 pbuf_free(pdu->pbuf);
169 if (pdu->
token != NULL)
180 const uint8_t *token,
193 if (drop_options == NULL) {
235#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
243#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
244 if (pdu->
data != NULL) {
252 if (new_hdr == NULL) {
271 while (size > new_size)
292 "coap_add_token: The token must defined first. Token ignored\n");
299 memcpy(pdu->
token, data, len);
337 memcpy(pdu->
token, data, len);
354 if (opt_iter.
number == number) {
372 opt_delta = decode_this.
delta + decode_next.
delta;
373 if (opt_delta <= 12) {
375 next_option[0] = (next_option[0] & 0x0f) + (
coap_opt_t)(opt_delta << 4);
377 else if (opt_delta <= 269 && decode_next.
delta <= 12) {
380 next_option[0] = (next_option[1] & 0x0f) + (13 << 4);
381 next_option[1] = (
coap_opt_t)(opt_delta - 13);
383 else if (opt_delta <= 269) {
385 next_option[1] = (
coap_opt_t)(opt_delta - 13);
387 else if (decode_next.
delta <= 12) {
389 if (next_option - option < 2) {
397 if (opt_iter.
number == number) {
403 assert(option != NULL);
404 assert(next_option != NULL);
405 memmove(&next_option[1], next_option,
413 next_option[0] = (next_option[2] & 0x0f) + (14 << 4);
414 next_option[1] = (
coap_opt_t)((opt_delta - 269) >> 8);
415 next_option[2] = (opt_delta - 269) & 0xff;
417 else if (decode_next.
delta <= 269) {
420 next_option[0] = (next_option[1] & 0x0f) + (14 << 4);
421 next_option[1] = (
coap_opt_t)((opt_delta - 269) >> 8);
422 next_option[2] = (opt_delta - 269) & 0xff;
425 next_option[1] = (
coap_opt_t)((opt_delta - 269) >> 8);
426 next_option[2] = (opt_delta - 269) & 0xff;
435 memmove(option, next_option, pdu->
used_size - (next_option - pdu->
token));
438 pdu->
data -= next_option - option;
474 "Option number %d is not defined as repeatable - dropped\n",
478 coap_log_info(
"Option number %d is not defined as repeatable\n",
488 const uint8_t *data) {
491 uint16_t prev_number = 0;
503 if (opt_iter.
number > number) {
507 prev_number = opt_iter.
number;
509 assert(option != NULL);
516 opt_delta = opt_iter.
number - number;
517 if (opt_delta == 0) {
530 if (opt_iter.
number > number) {
535 assert(option != NULL);
537 if (decode.
delta < 13) {
539 option[0] = (option[0] & 0x0f) + (
coap_opt_t)(opt_delta << 4);
540 }
else if (decode.
delta < 269 && opt_delta < 13) {
542 option[1] = (option[0] & 0x0f) + (
coap_opt_t)(opt_delta << 4);
544 }
else if (decode.
delta < 269 && opt_delta < 269) {
547 }
else if (opt_delta < 13) {
549 option[2] = (option[0] & 0x0f) + (
coap_opt_t)(opt_delta << 4);
551 }
else if (opt_delta < 269) {
553 option[1] = (option[0] & 0x0f) + 0xd0;
558 option[1] = (
coap_opt_t)((opt_delta - 269) >> 8);
559 option[2] = (opt_delta - 269) & 0xff;
562 memmove(&option[shift], &option[shrink],
565 number - prev_number, data, len))
570 pdu->
data += shift - shrink;
576 const uint8_t *data) {
580 size_t new_length = 0;
581 size_t old_length = 0;
592 if (new_length > old_length) {
594 pdu->
used_size + new_length - old_length))
600 if (new_length != old_length)
601 memmove(&option[new_length], &option[old_length],
605 decode.
delta, data, len))
608 pdu->
used_size += new_length - old_length;
610 pdu->
data += new_length - old_length;
616 const uint8_t *data) {
618 coap_log_warn(
"coap_add_optlist_pdu: PDU already contains data\n");
626 const uint8_t *data) {
653 if (number < pdu->max_opt) {
655 "coap_add_option: options are not in correct order\n");
666 memmove (&pdu->
data[optsize-1], &pdu->
data[-1],
669 pdu->
data += optsize;
677 number - pdu->
max_opt, data, len);
698 memcpy(payload, data, len);
699 return payload != NULL;
732 size_t *offset,
size_t *total) {
745 if(pdu->
data == NULL) {
758#ifndef SHORT_ERROR_RESPONSE
818 assert(optp); assert(*optp);
823 assert(optsize <= *length);
830 *max_opt += option.
delta;
840 const uint8_t *data) {
842 size_t header_size = 0;
845 uint8_t len = *data >> 4;
872 uint8_t len = *data >> 4;
875 }
else if (length >= 2) {
878 }
else if (length >= 3) {
881 }
else if (length >= 5) {
882 size = ((size_t)data[1] << 24) + ((size_t)data[2] << 16)
887 size += data[0] & 0x0f;
902 pdu->
type = (hdr[0] >> 4) & 0x03;
905 pdu->
mid = (uint16_t)hdr[2] << 8 | hdr[3];
931 if (len > 4)
goto bad;
934 if (len > 0)
goto bad;
944 if (len > 0)
goto bad;
953 if (len < 1 || len > 255)
goto bad;
956 if (len > 3)
goto bad;
965 if (len > 2)
goto bad;
1017 if (*len < prflen +1) {
1021 memcpy(*obp, prf, prflen);
1035 const uint8_t hex[] =
"0123456789abcdef";
1036 (*obp)[0] = hex[(c & 0xf0) >> 4];
1037 (*obp)[1] = hex[c & 0x0f];
1039 (*obp)[0] = isprint(c) ? c :
'.';
1052 if (pdu->
code == 0) {
1065 if (pdu->
code == 0) {
1077 const uint32_t len =
1081 "coap_pdu_parse: %d.%02d: offset %u malformed option\n",
1091 "coap_pdu_parse: %d.%02d: offset %u option %u has bad length %" PRIu32 "\n",
1105 static char outbuf[COAP_DEBUG_BUF_SIZE];
1112 for (i = 0; i < 2; i++) {
1117 outbuflen =
sizeof(outbuf);
1123 const uint32_t len =
1138 tlen = opt - opt_last;
1140 ok = ok &&
write_char(&obp, &outbuflen, *opt_last, i);
1145 ok = ok &&
write_char(&obp, &outbuflen, *opt, i);
1159 "coap_pdu_parse: message ending in payload start marker\n");
1164 pdu->
data = (uint8_t*)opt;
1174 const uint8_t *data,
1183 if (!hdr_size || hdr_size > length)
1189 if (pdu->
token - hdr_size != data)
1190 memcpy(pdu->
token - hdr_size, data, length);
1202 "coap_pdu_encode_header: not enough space for UDP-style header\n");
1209 pdu->
token[-2] = (uint8_t)(pdu->
mid >> 8);
1210 pdu->
token[-1] = (uint8_t)(pdu->
mid);
1224 "coap_pdu_encode_header: not enough space for TCP0 header\n");
1227 pdu->
token[-2] = (uint8_t)len << 4
1235 "coap_pdu_encode_header: not enough space for TCP8 header\n");
1246 "coap_pdu_encode_header: not enough space for TCP16 header\n");
1258 "coap_pdu_encode_header: not enough space for TCP32 header\n");
1270 coap_log_warn(
"coap_pdu_encode_header: unsupported protocol\n");
1282 assert(code <= 0xff);
1291 assert(type <= 0x3);
1308 assert(mid >= 0 && mid <= 0xffff);
Pulls together all the internal only header files.
size_t coap_opt_parse(const coap_opt_t *opt, size_t length, coap_option_t *result)
Parses the option pointed to by opt into result.
uint16_t coap_option_num_t
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
uint16_t coap_new_message_id(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
#define coap_log_debug(...)
#define coap_log_info(...)
#define coap_log_warn(...)
#define coap_log_crit(...)
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
size_t coap_opt_encode(coap_opt_t *opt, size_t maxlen, uint16_t delta, const uint8_t *val, size_t length)
Encodes option with given delta into opt.
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
coap_opt_iterator_t * coap_option_iterator_init(const coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t *filter)
Initializes the given option iterator oi to point to the beginning of the pdu's option list.
size_t coap_opt_encode_size(uint16_t delta, size_t length)
Compute storage bytes needed for an option with given delta and length.
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
coap_opt_t * coap_check_option(const coap_pdu_t *pdu, coap_option_num_t number, coap_opt_iterator_t *oi)
Retrieves the first option of number number from pdu.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
int coap_option_filter_get(coap_opt_filter_t *filter, coap_option_num_t option)
Checks if number is contained in filter.
#define COAP_MESSAGE_SIZE_OFFSET_TCP8
size_t coap_insert_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Inserts option of given number in the pdu with the appropriate data.
int coap_remove_option(coap_pdu_t *pdu, coap_option_num_t number)
Removes (first) option of given number from the pdu.
int coap_update_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Updates token in pdu with length len and data.
#define COAP_PDU_MAX_UDP_HEADER_SIZE
int coap_pdu_parse_header(coap_pdu_t *pdu, coap_proto_t proto)
Decode the protocol specific header for the specified PDU.
size_t coap_pdu_parse_header_size(coap_proto_t proto, const uint8_t *data)
Interprets data to determine the number of bytes in the header.
#define COAP_PDU_MAX_TCP_HEADER_SIZE
#define COAP_MAX_MESSAGE_SIZE_TCP8
#define COAP_PDU_IS_SIGNALING(pdu)
#define COAP_MAX_MESSAGE_SIZE_TCP0
int coap_option_check_repeatable(coap_option_num_t number)
Check whether the option is allowed to be repeated or not.
#define COAP_MESSAGE_SIZE_OFFSET_TCP16
void coap_pdu_clear(coap_pdu_t *pdu, size_t size)
Clears any contents from pdu and resets used_size, and data pointers.
#define COAP_MESSAGE_SIZE_OFFSET_TCP32
int coap_pdu_parse_opt(coap_pdu_t *pdu)
Verify consistency in the given CoAP PDU structure and locate the data.
size_t coap_update_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Updates existing first option of given number in the pdu with the new data.
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto)
Compose the protocol specific header for the specified PDU.
#define COAP_DEFAULT_VERSION
#define COAP_PAYLOAD_START
int coap_pdu_check_resize(coap_pdu_t *pdu, size_t size)
Dynamically grows the size of pdu to new_size if needed.
size_t coap_pdu_parse_size(coap_proto_t proto, const uint8_t *data, size_t length)
Parses data to extract the message size.
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size)
Dynamically grows the size of pdu to new_size.
#define COAP_PDU_IS_REQUEST(pdu)
#define COAP_MAX_MESSAGE_SIZE_TCP16
size_t coap_add_option_internal(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Adds option of given number to pdu that is passed as first parameter.
#define COAP_OPTION_HOP_LIMIT
#define COAP_OPTION_NORESPONSE
#define COAP_OPTION_URI_HOST
#define COAP_OPTION_IF_MATCH
coap_pdu_code_t coap_pdu_get_code(const coap_pdu_t *pdu)
Gets the PDU code associated with pdu.
#define COAP_OPTION_BLOCK2
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
#define COAP_OPTION_CONTENT_FORMAT
#define COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS
#define COAP_OPTION_SIZE2
#define COAP_OPTION_BLOCK1
#define COAP_OPTION_PROXY_SCHEME
uint8_t * coap_add_data_after(coap_pdu_t *pdu, size_t len)
Adds given data to the pdu that is passed as first parameter but does not.
#define COAP_OPTION_URI_QUERY
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
void coap_pdu_set_code(coap_pdu_t *pdu, coap_pdu_code_t code)
Sets the PDU code in the pdu.
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
#define COAP_OPTION_IF_NONE_MATCH
#define COAP_OPTION_LOCATION_PATH
#define COAP_OPTION_URI_PATH
#define COAP_RESPONSE_CODE(N)
coap_proto_t
CoAP protocol types.
coap_pdu_t * coap_new_pdu(coap_pdu_type_t type, coap_pdu_code_t code, coap_session_t *session)
Creates a new CoAP PDU.
coap_pdu_code_t
Set of codes available for a PDU.
#define COAP_OPTION_OSCORE
#define COAP_OPTION_SIZE1
coap_pdu_type_t
CoAP PDU message type definitions.
#define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
#define COAP_OPTION_LOCATION_QUERY
void coap_pdu_set_type(coap_pdu_t *pdu, coap_pdu_type_t type)
Sets the PDU type in the pdu.
size_t coap_add_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Adds option of given number to pdu that is passed as first parameter.
#define COAP_SIGNALING_OPTION_CUSTODY
coap_pdu_signaling_proto_t
coap_pdu_type_t coap_pdu_get_type(const coap_pdu_t *pdu)
Gets the PDU type associated with pdu.
int coap_get_data(const coap_pdu_t *pdu, size_t *len, const uint8_t **data)
Retrieves the length and data pointer of specified PDU.
int coap_pdu_parse(coap_proto_t proto, const uint8_t *data, size_t length, coap_pdu_t *pdu)
Parses data into the CoAP PDU structure given in result.
#define COAP_MAX_OPT
the highest option number we know
void coap_pdu_set_mid(coap_pdu_t *pdu, coap_mid_t mid)
Sets the message id in the pdu.
coap_pdu_t * coap_pdu_duplicate(const coap_pdu_t *old_pdu, coap_session_t *session, size_t token_length, const uint8_t *token, coap_opt_filter_t *drop_options)
Duplicate an existing PDU.
#define COAP_OPTION_URI_PORT
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
#define COAP_OPTION_ACCEPT
int coap_get_data_large(const coap_pdu_t *pdu, size_t *len, const uint8_t **data, size_t *offset, size_t *total)
Retrieves the data from a PDU, with support for large bodies of data that spans multiple PDUs.
coap_mid_t coap_pdu_get_mid(const coap_pdu_t *pdu)
Gets the message id associated with pdu.
#define COAP_OPTION_MAXAGE
#define COAP_OPTION_PROXY_URI
#define COAP_OPTION_OBSERVE
#define COAP_SIGNALING_OPTION_HOLD_OFF
#define COAP_SIGNALING_OPTION_BAD_CSM_OPTION
#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE
int coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds given data to the pdu that is passed as first parameter.
coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu)
Gets the token associated with pdu.
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
static size_t next_option_safe(coap_opt_t **optp, size_t *length, uint16_t *max_opt)
Advances *optp to next option if still in PDU.
static int write_char(char **obp, size_t *len, char c, int printable)
static int coap_pdu_parse_opt_csm(coap_pdu_t *pdu, uint16_t len)
error_desc_t coap_error[]
static int write_prefix(char **obp, size_t *len, const char *prf, size_t prflen)
static int coap_pdu_parse_opt_base(coap_pdu_t *pdu, uint16_t len)
CoAP binary data definition with const data.
size_t length
length of binary data
const uint8_t * s
read-only binary data
Iterator to run through PDU options.
coap_option_num_t number
decoded option number
Representation of CoAP options.
uint8_t max_hdr_size
space reserved for protocol-specific header
uint16_t max_opt
highest option number in PDU
uint8_t * token
first byte of token, if any, or options
coap_lg_xmit_t * lg_xmit
Holds ptr to lg_xmit if sending a set of blocks.
size_t body_length
Holds body data length.
size_t max_size
maximum size for token, options and payload, or zero for variable size pdu
const uint8_t * body_data
Holds ptr to re-assembled data or NULL.
size_t body_offset
Holds body data offset.
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
uint8_t token_length
length of Token
uint8_t hdr_size
actual size used for protocol-specific header (0 until header is encoded)
uint8_t * data
first byte of payload, if any
coap_mid_t mid
message id, if any, in regular host byte order
size_t used_size
used bytes of storage for token, options and payload
uint8_t crit_opt
Set if unknown critical option for proxy.
size_t alloc_size
allocated storage for token, options and payload
size_t body_total
Holds body data total size.
coap_pdu_type_t type
message type
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...