9 #include "coap_config.h" 11 #if defined(HAVE_ASSERT_H) && !defined(assert) 18 #ifdef HAVE_ARPA_INET_H 19 #include <arpa/inet.h> 35 pdu->
hdr = pdu->pbuf->payload;
40 memset(pdu->
hdr, 0, size);
50 coap_pdu_from_pbuf(
struct pbuf *pbuf)
52 if (pbuf == NULL)
return NULL;
54 LWIP_ASSERT(
"Can only deal with contiguous PBUFs", pbuf->tot_len == pbuf->len);
55 LWIP_ASSERT(
"coap_read needs to receive an exclusive copy of the incoming pbuf", pbuf->ref == 1);
66 result->
length = pbuf->tot_len;
67 result->
hdr = pbuf->payload;
76 unsigned short id,
size_t size) {
88 #if defined(WITH_POSIX) || defined(WITH_CONTIKI) 90 if (!pdu)
return NULL;
92 if (pdu->
hdr == NULL) {
99 if (!pdu)
return NULL;
100 p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);
137 #if defined(WITH_POSIX) || defined(WITH_CONTIKI) 139 if (pdu->
hdr != NULL) {
147 pbuf_free(pdu->pbuf);
154 const size_t HEADERLENGTH = len + 4;
156 if (!pdu || len > 8 || pdu->
max_size < HEADERLENGTH)
163 pdu->
length = HEADERLENGTH;
178 if (type < pdu->max_delta) {
179 warn(
"coap_add_option: options are not in correct order\n");
183 opt = (
unsigned char *)pdu->
hdr + pdu->
length;
190 warn(
"coap_add_option: cannot add option\n");
210 if (type < pdu->max_delta) {
211 warn(
"coap_add_option: options are not in correct order\n");
215 opt = (
unsigned char *)pdu->
hdr + pdu->
length;
222 warn(
"coap_add_option: cannot add option\n");
230 return ((
unsigned char*)opt) + optsize - len;
242 warn(
"coap_add_data: cannot add: data too large for PDU\n");
251 memcpy(pdu->
data, data, len);
270 return *data != NULL;
273 #ifndef SHORT_ERROR_RESPONSE 311 for (i = 0; coap_error[i].
code; ++i) {
312 if (coap_error[i].code == code)
313 return coap_error[i].
phrase;
334 assert(optsize <= *length);
351 debug(
"insufficient space to store parsed PDU\n");
356 debug(
"discarded invalid PDU\n");
363 LWIP_ASSERT(
"coap_pdu_parse with unexpected addresses", data == (
void*)pdu->
hdr);
364 LWIP_ASSERT(
"coap_pdu_parse with unexpected length", length == pdu->
length);
368 pdu->
hdr->
type = (data[0] >> 4) & 0x03;
377 debug(
"coap_pdu_parse: empty message is not empty\n");
384 debug(
"coap_pdu_parse: invalid Token\n");
391 memcpy(&pdu->
hdr->
id, data + 2, 2);
409 debug(
"coap_pdu_parse: drop\n");
420 debug(
"coap_pdu_parse: message ending in payload start marker\n");
424 debug(
"set data to %p (pdu ends at %p)\n", (
unsigned char *)opt,
425 (
unsigned char *)pdu->
hdr + pdu->
length);
426 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.
Helpers for handling options in CoAP PDUs.
coap_hdr_t * hdr
Address of the first byte of the CoAP message.
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
#define COAP_INVALID_TID
Indicates an invalid transaction id.
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.
Pre-defined constants that reflect defaults for CoAP.
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
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.
coap_pdu_t * coap_new_pdu(void)
Creates a new CoAP PDU.
unsigned char coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
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.