11 #if defined(HAVE_ASSERT_H) && !defined(assert)
18 #ifdef HAVE_ARPA_INET_H
19 #include <arpa/inet.h>
32 MEMB(pdu_storage, _pdu, COAP_PDU_MAXCNT);
35 coap_pdu_resources_init() {
36 memb_init(&pdu_storage);
57 coap_pdu_from_pbuf(
struct pbuf *pbuf)
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);
62 void *data = pbuf->payload;
65 u8_t header_error = pbuf_header(pbuf,
sizeof(
coap_pdu_t));
66 LWIP_ASSERT(
"CoAP PDU header does not fit in existing header space", header_error == 0);
83 unsigned short id,
size_t size) {
102 p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);
104 u8_t header_error = pbuf_header(p,
sizeof(
coap_pdu_t));
107 LWIP_ASSERT(
"CoAP PDU header does not fit in transport header", header_error == 0);
137 coap_log(LOG_CRIT,
"coap_new_pdu: cannot allocate memory for new PDU\n");
149 pbuf_free(pdu->pbuf);
152 memb_free(&pdu_storage, pdu);
158 const size_t HEADERLENGTH = len + 4;
160 if (!pdu || len > 8 || pdu->
max_size < HEADERLENGTH)
167 pdu->
length = HEADERLENGTH;
182 if (type < pdu->max_delta) {
183 warn(
"coap_add_option: options are not in correct order\n");
187 opt = (
unsigned char *)pdu->
hdr + pdu->
length;
194 warn(
"coap_add_option: cannot add option\n");
214 if (type < pdu->max_delta) {
215 warn(
"coap_add_option: options are not in correct order\n");
219 opt = (
unsigned char *)pdu->
hdr + pdu->
length;
226 warn(
"coap_add_option: cannot add option\n");
234 return ((
unsigned char*)opt) + optsize - len;
240 assert(pdu->
data == NULL);
246 warn(
"coap_add_data: cannot add: data too large for PDU\n");
247 assert(pdu->
data == NULL);
255 memcpy(pdu->
data, data, len);
274 return *data != NULL;
277 #ifndef SHORT_ERROR_RESPONSE
312 for (i = 0; coap_error[i].
code; ++i) {
313 if (coap_error[i].code == code)
314 return coap_error[i].
phrase;
330 assert(optp); assert(*optp);
335 assert(optsize <= *length);
352 debug(
"insufficient space to store parsed PDU\n");
357 debug(
"discarded invalid PDU\n");
361 pdu->
hdr->
type = (data[0] >> 4) & 0x03;
369 debug(
"coap_pdu_parse: empty message is not empty\n");
376 debug(
"coap_pdu_parse: invalid Token\n");
382 memcpy(&pdu->
hdr->
id, data + 2, 2);
398 debug(
"coap_pdu_parse: drop\n");
409 debug(
"coap_pdu_parse: message ending in payload start marker\n");
413 debug(
"set data to %p (pdu ends at %p)\n", (
unsigned char *)opt,
414 (
unsigned char *)pdu->
hdr + pdu->
length);
415 pdu->
data = (
unsigned char *)opt;
#define COAP_RESPONSE_CODE(N)
unsigned char * coap_add_option_later(coap_pdu_t *pdu, unsigned short type, unsigned int len)
de-duplicate code with coap_add_option
static size_t next_option_safe(coap_opt_t **optp, size_t *length)
Advances *optp to next option if still in PDU.
unsigned short length
PDU length (including header, options, data)
int coap_add_token(coap_pdu_t *pdu, size_t len, const unsigned char *data)
Adds token of length len to pdu.
int coap_pdu_parse(unsigned char *data, size_t length, coap_pdu_t *pdu)
Parses data into the CoAP PDU structure given in result.
int coap_get_data(coap_pdu_t *pdu, size_t *len, unsigned char **data)
Retrieves the length and data pointer of specified PDU.
#define coap_malloc(size)
helpers for handling options in CoAP PDUs
void coap_pdu_clear(coap_pdu_t *pdu, size_t size)
Clears any contents from pdu and resets version field, length and data pointers.
unsigned short max_delta
highest option number
#define COAP_DEFAULT_VERSION
Header structure for CoAP PDUs.
error_desc_t coap_error[]
Representation of CoAP options.
size_t max_size
allocated storage for options and data
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.
size_t coap_add_option(coap_pdu_t *pdu, unsigned short type, unsigned int len, const unsigned char *data)
de-duplicate code with coap_add_option_later
#define COAP_MAX_PDU_SIZE
int coap_add_data(coap_pdu_t *pdu, unsigned int len, const unsigned char *data)
Adds given data to the pdu that is passed as first parameter.
void coap_delete_pdu(coap_pdu_t *pdu)
coap_pdu_t * coap_pdu_init(unsigned char type, unsigned char code, unsigned short id, size_t size)
Creates a new CoAP PDU of given size (must be large enough to hold the basic CoAP message header (coa...
#define COAP_PAYLOAD_START
unsigned int token_length
unsigned char coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
coap_pdu_t * coap_new_pdu()
Creates a new CoAP PDU.
unsigned char * data
payload
size_t coap_opt_encode(coap_opt_t *opt, size_t maxlen, unsigned short delta, const unsigned char *val, size_t length)
Encodes option with given delta into opt.