payload data

This commit is contained in:
Olaf Bergmann committed 2013-01-11 17:45:12 +01:00
1 parent f48ecd9f2d
commit e68a016b65
2 files changed
+19 -16

No files matched your search

+14 -12
View File
@@ -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 */
+5 -4
View File
@@ -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);