11 #if defined(HAVE_LIMITS_H)
18 #ifdef HAVE_ARPA_INET_H
19 #include <arpa/inet.h>
21 #ifdef HAVE_WINSOCK2_H
26 #define min(a,b) ((a) < (b) ? (a) : (b))
30 #define max(a,b) ((a) > (b) ? (a) : (b))
53 coap_pdu_from_pbuf(
struct pbuf *pbuf )
57 if (pbuf == NULL)
return NULL;
59 LWIP_ASSERT(
"Can only deal with contiguous PBUFs", pbuf->tot_len == pbuf->len);
60 LWIP_ASSERT(
"coap_io_do_io needs to receive an exclusive copy of the incoming pbuf", pbuf->ref == 1);
83 if (!pdu)
return NULL;
85 #if defined(WITH_CONTIKI) || defined(WITH_LWIP)
95 pdu->pbuf = pbuf_alloc(PBUF_TRANSPORT, size + pdu->
max_hdr_size, PBUF_RAM);
96 if (pdu->pbuf == NULL) {
130 pbuf_free(pdu->pbuf);
132 if (pdu->
token != NULL)
142 #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
150 #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
151 if (pdu->
data != NULL) {
158 if (new_hdr == NULL) {
177 while (size > new_size)
198 "coap_add_token: The token must defined first. Token ignored\n");
205 memcpy(pdu->
token, data, len);
218 uint16_t prev_type = 0;
230 if (opt_iter.
type > type) {
234 prev_type = opt_iter.
type;
236 assert(option != NULL);
243 opt_delta = opt_iter.
type - type;
253 if (opt_iter.
type > type) {
258 assert(option != NULL);
260 if (decode.
delta <= 12) {
262 option[0] = (option[0] & 0x0f) + (opt_delta << 4);
264 else if (decode.
delta <= 269 && opt_delta <= 12) {
266 option[1] = (option[0] & 0x0f) + (opt_delta << 4);
269 else if (decode.
delta <= 269 && opt_delta <= 269) {
271 option[1] = opt_delta - 13;
273 else if (opt_delta <= 12) {
275 option[2] = (option[0] & 0x0f) + (opt_delta << 4);
278 else if (opt_delta <= 269) {
280 option[1] = (option[0] & 0x0f) + 0xd0;
281 option[2] = opt_delta - 13;
286 option[1] = (opt_delta - 269) >> 8;
287 option[2] = (opt_delta - 269) & 0xff;
290 memmove(&option[shift], &option[shrink],
293 type - prev_type, data, len))
298 pdu->
data += shift - shrink;
308 size_t new_length = 0;
309 size_t old_length = 0;
320 if (new_length > old_length) {
322 pdu->
used_size + new_length - old_length))
328 if (new_length != old_length)
329 memmove(&option[new_length], &option[old_length],
333 decode.
delta, data, len))
336 pdu->
used_size += new_length - old_length;
338 pdu->
data += new_length - old_length;
385 if (type < pdu->max_opt) {
387 "coap_add_option: options are not in correct order\n");
399 type - pdu->
max_opt, data, len);
420 memcpy(payload, data, len);
421 return payload != NULL;
428 assert(pdu->
data == NULL);
450 if(pdu->
data == NULL) {
460 #ifndef SHORT_ERROR_RESPONSE
517 assert(optp); assert(*optp);
522 assert(optsize <= *length);
529 *max_opt += option.
delta;
541 size_t header_size = 0;
574 }
else if (length >= 2) {
577 }
else if (length >= 3) {
580 }
else if (length >= 5) {
581 size = ((size_t)data[1] << 24) + ((size_t)data[2] << 16)
586 size += data[0] & 0x0f;
601 pdu->
type = (hdr[0] >> 4) & 0x03;
604 pdu->
tid = (uint16_t)hdr[2] << 8 | hdr[3];
630 if (len > 4)
goto bad;
633 if (len > 0)
goto bad;
643 if (len > 0)
goto bad;
652 if (len < 1 || len > 255)
goto bad;
655 if (len > 3)
goto bad;
664 if (len > 2)
goto bad;
683 if (pdu->
code == 0) {
696 if (pdu->
code == 0) {
747 "coap_pdu_parse: %d.%02d: option %u has bad length %u\n",
758 "coap_pdu_parse: message ending in payload start marker\n");
782 if (!hdr_size || hdr_size > length)
789 memcpy(pdu->
token - hdr_size, data, length);
802 "coap_pdu_encode_header: not enough space for UDP-style header\n");
824 "coap_pdu_encode_header: not enough space for TCP0 header\n");
835 "coap_pdu_encode_header: not enough space for TCP8 header\n");
846 "coap_pdu_encode_header: not enough space for TCP16 header\n");
858 "coap_pdu_encode_header: not enough space for TCP32 header\n");
Pulls together all the internal only header files.
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
#define coap_log(level,...)
Logging function.
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.
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_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.
coap_opt_t * coap_check_option(coap_pdu_t *pdu, uint16_t type, coap_opt_iterator_t *oi)
Retrieves the first option of type type from pdu.
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().
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.
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
int coap_update_option(coap_pdu_t *pdu, uint16_t type, size_t len, const uint8_t *data)
Updates existing first option of given type in the pdu with the new data.
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.
int coap_pdu_check_resize(coap_pdu_t *pdu, size_t size)
Dynamically grows the size of pdu to new_size if needed.
static int coap_pdu_parse_opt_csm(coap_pdu_t *pdu, uint16_t len)
error_desc_t coap_error[]
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
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.
int coap_get_data(const coap_pdu_t *pdu, size_t *len, uint8_t **data)
Retrieves the length and data pointer of specified PDU.
coap_pdu_t * coap_pdu_init(uint8_t type, uint8_t code, uint16_t tid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
coap_pdu_t * coap_new_pdu(const struct coap_session_t *session)
Creates a new CoAP PDU.
void coap_pdu_clear(coap_pdu_t *pdu, size_t size)
Clears any contents from pdu and resets used_size, and data pointers.
size_t coap_add_option(coap_pdu_t *pdu, uint16_t type, size_t len, const uint8_t *data)
Adds option of given type to pdu that is passed as first parameter.
int coap_pdu_parse_opt(coap_pdu_t *pdu)
Verify consistency in the given CoAP PDU structure and locate the data.
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 copy it.
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto)
Compose the protocol specific header for the 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.
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
size_t coap_insert_option(coap_pdu_t *pdu, uint16_t type, size_t len, const uint8_t *data)
Inserts option of given type in the pdu with the appropriate data.
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.
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.
#define COAP_OPTION_HOP_LIMIT
#define COAP_OPTION_NORESPONSE
#define COAP_OPTION_URI_HOST
#define COAP_OPTION_IF_MATCH
#define COAP_MESSAGE_SIZE_OFFSET_TCP8
#define COAP_OPTION_BLOCK2
#define COAP_OPTION_CONTENT_FORMAT
#define COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS
#define COAP_OPTION_SIZE2
#define COAP_OPTION_BLOCK1
#define COAP_OPTION_PROXY_SCHEME
#define COAP_SIGNALING_PONG
#define COAP_PDU_MAX_UDP_HEADER_SIZE
#define COAP_OPTION_URI_QUERY
#define COAP_OPTION_IF_NONE_MATCH
#define COAP_OPTION_LOCATION_PATH
#define COAP_OPTION_URI_PATH
#define COAP_SIGNALING_CSM
#define COAP_RESPONSE_CODE(N)
#define COAP_PDU_MAX_TCP_HEADER_SIZE
#define COAP_OPTION_OSCORE
#define COAP_MAX_MESSAGE_SIZE_TCP8
#define COAP_PDU_IS_SIGNALING(pdu)
#define COAP_OPTION_SIZE1
#define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER
#define COAP_MAX_MESSAGE_SIZE_TCP0
#define COAP_OPTION_LOCATION_QUERY
#define COAP_MESSAGE_SIZE_OFFSET_TCP16
#define COAP_MESSAGE_SIZE_OFFSET_TCP32
#define COAP_SIGNALING_OPTION_CUSTODY
#define COAP_MAX_OPT
the highest option number we know
#define COAP_DEFAULT_VERSION
#define COAP_OPTION_URI_PORT
#define COAP_OPTION_ACCEPT
#define COAP_SIGNALING_RELEASE
#define COAP_PAYLOAD_START
#define COAP_OPTION_MAXAGE
#define COAP_OPTION_PROXY_URI
#define COAP_SIGNALING_PING
#define COAP_OPTION_OBSERVE
#define COAP_PDU_IS_REQUEST(pdu)
#define COAP_SIGNALING_OPTION_HOLD_OFF
#define COAP_MAX_MESSAGE_SIZE_TCP16
#define COAP_SIGNALING_OPTION_BAD_CSM_OPTION
#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE
#define COAP_SIGNALING_ABORT
Iterator to run through PDU options.
uint16_t type
decoded option type
size_t length
remaining length of PDU
Representation of CoAP options.
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
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
size_t max_size
maximum size for token, options and payload, or zero for variable size pdu
uint16_t tid
transaction id, if any, in regular host byte order
uint8_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
uint8_t * data
first byte of payload, if any
size_t used_size
used bytes of storage for token, options and payload
size_t alloc_size
allocated storage for token, options and payload