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_read 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);
222 if (type < pdu->max_delta) {
224 "coap_add_option: options are not in correct order\n");
259 if (type < pdu->max_delta) {
261 "coap_add_option: options are not in correct order\n");
284 return opt + optsize - len;
294 memcpy(payload, data, len);
295 return payload != NULL;
302 assert(pdu->
data == NULL);
324 if(pdu->
data == NULL) {
334 #ifndef SHORT_ERROR_RESPONSE 372 for (i = 0; coap_error[i].
code; ++i) {
373 if (coap_error[i].code == code)
374 return coap_error[i].
phrase;
390 assert(optp); assert(*optp);
395 assert(optsize <= *length);
408 size_t header_size = 0;
441 }
else if (length >= 2) {
444 }
else if (length >= 3) {
447 }
else if (length >= 5) {
448 size = ((size_t)data[1] << 24) + ((size_t)data[2] << 16)
453 size += data[0] & 0x0f;
468 pdu->
type = (hdr[0] >> 4) & 0x03;
471 pdu->
tid = (uint16_t)hdr[2] << 8 | hdr[3];
495 if (pdu->
code == 0) {
507 if (pdu->
code == 0) {
529 "coap_pdu_parse: message ending in payload start marker\n");
553 if (!hdr_size || hdr_size > length)
560 memcpy(pdu->
token - hdr_size, data, length);
573 "coap_pdu_encode_header: not enough space for UDP-style header\n");
595 "coap_pdu_encode_header: not enough space for TCP0 header\n");
606 "coap_pdu_encode_header: not enough space for TCP8 header\n");
617 "coap_pdu_encode_header: not enough space for TCP16 header\n");
629 "coap_pdu_encode_header: not enough space for TCP32 header\n");
uint8_t code
request method (value 1–10) or response code (value 40-255)
size_t length
the option value length
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
coap_pdu_t * coap_new_pdu(const struct coap_session_t *session)
Creates a new CoAP PDU.
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_RESPONSE_CODE(N)
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 copyt it.
static size_t next_option_safe(coap_opt_t **optp, size_t *length)
Advances *optp to next option if still in PDU.
#define COAP_MAX_MESSAGE_SIZE_TCP8
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_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
int coap_pdu_parse_opt(coap_pdu_t *pdu)
Verify consistency in the given CoAP PDU structure and locate the data.
#define COAP_MAX_MESSAGE_SIZE_TCP16
#define COAP_MESSAGE_SIZE_OFFSET_TCP16
static int coap_pdu_check_resize(coap_pdu_t *pdu, size_t size)
void coap_pdu_clear(coap_pdu_t *pdu, size_t size)
Clears any contents from pdu and resets used_size, and data pointers.
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size)
Dynamically grows the size of pdu to new_size.
#define COAP_MESSAGE_SIZE_OFFSET_TCP32
size_t coap_opt_encode_size(uint16_t delta, size_t length)
Compute storage bytes needed for an option with given delta and length.
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
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
uint8_t * data
first byte of payload, if any
error_desc_t coap_error[]
Representation of CoAP options.
uint8_t * data
the option data
size_t used_size
used bytes of storage for token, options and payload
size_t alloc_size
allocated storage for token, options and payload
#define COAP_MESSAGE_SIZE_OFFSET_TCP8
uint8_t * coap_add_option_later(coap_pdu_t *pdu, uint16_t type, size_t len)
Adds option of given type to pdu that is passed as first parameter, but does not write a value...
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
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.
int coap_get_data(const coap_pdu_t *pdu, size_t *len, uint8_t **data)
Retrieves the length and data pointer of specified PDU.
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.
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
uint16_t max_delta
highest option number
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
#define COAP_PAYLOAD_START
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.
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.
#define COAP_MAX_MESSAGE_SIZE_TCP0
uint8_t max_hdr_size
space reserved for protocol-specific header
#define COAP_PDU_MAX_TCP_HEADER_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.
uint8_t hdr_size
actaul size used for protocol-specific header
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_log(level,...)
Logging function.
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...
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
uint8_t token_length
length of Token
#define COAP_PDU_MAX_UDP_HEADER_SIZE
uint16_t tid
transaction id, if any, in regular host byte order
Pulls together all the internal only header files.
size_t coap_pdu_parse_size(coap_proto_t proto, const uint8_t *data, size_t length)
Parses data to extract the message size.