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;