diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index abdf2409fd..d239a6a4f1 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -573,6 +573,23 @@ static inline ssize_t coap_get_uri_query(const coap_pkt_t *pkt, uint8_t *target) */ ssize_t coap_opt_get_next(const coap_pkt_t *pkt, coap_optpos_t *opt, uint8_t **value, bool init_opt); + +/** + * @brief Retrieve the value for an option as an opaque array of bytes + * + * Retrieves the location and length of the option value of any type. Useful for + * an opaque option, which essentially is an array of bytes. If more than one + * option for a given option number, retrieves the first option. To retrieve + * subsequent options, see coap_opt_get_next(). + * + * @param[in] pkt packet to read from + * @param[in] opt_num option number to retrieve + * @param[out] value start of the option value + * + * @return length of option; 0 if the option exists but is empty + * @return -ENOENT if option not found + */ +ssize_t coap_opt_get_opaque(coap_pkt_t *pkt, unsigned opt_num, uint8_t **value); /**@}*/ diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index 0759f11f41..cfae4cffeb 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -205,6 +205,20 @@ static uint8_t *_parse_option(const coap_pkt_t *pkt, return pkt_pos; } +ssize_t coap_opt_get_opaque(coap_pkt_t *pkt, unsigned opt_num, uint8_t **value) +{ + uint8_t *start = coap_find_option(pkt, opt_num); + if (!start) { + return -ENOENT; + } + + uint16_t delta; + int len; + + *value = _parse_option(pkt, start, &delta, &len); + return len; +} + int coap_get_option_uint(coap_pkt_t *pkt, unsigned opt_num, uint32_t *target) { assert(target);