From e68a016b657777c6cd3125fb63ae93b99661f496 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Fri, 11 Jan 2013 17:45:12 +0100 Subject: [PATCH] payload data --- pdu.c | 26 ++++++++++++++------------ pdu.h | 9 +++++---- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/pdu.c b/pdu.c index 43540bd7..52c7e0b2 100644 --- a/pdu.c +++ b/pdu.c @@ -51,7 +51,6 @@ coap_pdu_clear(coap_pdu_t *pdu, size_t size) { /* data points after the header; when options are added, the data pointer is moved to the back */ pdu->length = sizeof(coap_hdr_t); - pdu->data = (unsigned char *)pdu->hdr + pdu->length; } coap_pdu_t * @@ -155,27 +154,30 @@ coap_add_option(coap_pdu_t *pdu, unsigned short type, unsigned int len, const un int coap_add_data(coap_pdu_t *pdu, unsigned int len, const unsigned char *data) { assert(pdu); + assert(pdu->data == NULL); - if ( pdu->length + len > pdu->max_size ) { -#ifndef NDEBUG - coap_log(LOG_WARN, "coap_add_data: cannot add: data too large for PDU\n"); -#endif + if (len == 0) + return 1; + + if (pdu->length + len + 1 > pdu->max_size) { + warn("coap_add_data: cannot add: data too large for PDU\n"); return 0; } - memcpy( (unsigned char *)pdu->hdr + pdu->length, data, len ); - pdu->length += len; + pdu->data = (unsigned char *)pdu->hdr + pdu->length; + *pdu->data = COAP_PAYLOAD_START; + pdu->data++; + + memcpy(pdu->data, data, len); + pdu->length += len + 1; return 1; } int coap_get_data(coap_pdu_t *pdu, size_t *len, unsigned char **data) { - if ( !pdu ) - return 0; - - if ( pdu->data < (unsigned char *)pdu->hdr + pdu->length ) { - /* pdu contains data */ + assert(pdu); + if (pdu->data) { *len = (unsigned char *)pdu->hdr + pdu->length - pdu->data; *data = pdu->data; } else { /* no data, clear everything */ diff --git a/pdu.h b/pdu.h index 2c6e69e6..99291add 100644 --- a/pdu.h +++ b/pdu.h @@ -62,9 +62,9 @@ #define COAP_OPTION_MAXAGE 14 /* E, uint, 0--4 B, 60 Seconds */ #define COAP_OPTION_URI_QUERY 15 /* C, String, 1-255 B, (none) */ #define COAP_OPTION_ACCEPT 16 /* E, uint, 0-2 B, (none) */ -#define COAP_OPTION_TOKEN 19 /* C, opaque, 1-8 B, empty */ #define COAP_OPTION_LOCATION_QUERY 20 /* E, String, 0-255 B, (none) */ #define COAP_OPTION_PROXY_URI 35 /* C, String, 1-1034 B, (none) */ +#define COAP_OPTION_PROXY_SCHEME 39 /* C, String, 1-255 B, (none) */ /* option types from draft-hartke-coap-observe-07 */ @@ -75,7 +75,7 @@ #define COAP_OPTION_BLOCK2 23 /* C, uint, 0--3 B, (none) */ #define COAP_OPTION_BLOCK1 27 /* C, uint, 0--3 B, (none) */ -#define COAP_MAX_OPT 35 /**< the highest option number we know */ +#define COAP_MAX_OPT 39 /**< the highest option number we know */ /* CoAP result codes (HTTP-Code / 100 * 40 + HTTP-Code % 100) */ @@ -287,8 +287,9 @@ size_t coap_add_option(coap_pdu_t *pdu, unsigned short type, unsigned int len, const unsigned char *data); /** - * Adds given data to the pdu that is passed as first parameter. Note that the PDU's - * data is destroyed by coap_add_option(). + * Adds given data to the pdu that is passed as first parameter. Note + * that the PDU's data is destroyed by coap_add_option(). coap_add_data() + * must be called only once per PDU, otherwise the result is undefined. */ int coap_add_data(coap_pdu_t *pdu, unsigned int len, const unsigned char *data);