diff --git a/pdu.c b/pdu.c index 37d5f8f7..1089be76 100644 --- a/pdu.c +++ b/pdu.c @@ -120,3 +120,21 @@ coap_add_data(coap_pdu_t *pdu, unsigned int len, const unsigned char *data) { pdu->length += len; return 1; } + +int +coap_get_data(coap_pdu_t *pdu, unsigned int *len, unsigned char **data) { + if ( !pdu ) + return 0; + + if ( pdu->data < (unsigned char *)pdu->hdr + pdu->length ) { + /* pdu contains data */ + + *len = (unsigned char *)pdu->hdr + pdu->length - pdu->data; + *data = pdu->data; + } else { /* no data, clear everything */ + *len = 0; + *data = NULL; + } + + return 1; +} diff --git a/pdu.h b/pdu.h index 2b08bd2f..5e8b699b 100644 --- a/pdu.h +++ b/pdu.h @@ -228,4 +228,11 @@ coap_opt_t *coap_check_option(coap_pdu_t *pdu, unsigned char type); int coap_add_data(coap_pdu_t *pdu, unsigned int len, const unsigned char *data); +/** + * Retrieves the length and data pointer of specified PDU. Returns 0 on error + * or 1 if *len and *data have correct values. Note that these values are + * destroyed with the pdu. + */ +int coap_get_data(coap_pdu_t *pdu, unsigned int *len, unsigned char **data); + #endif /* _PDU_H_ */