From 233dd31e173a706c997d233acd9a44f79a52cf30 Mon Sep 17 00:00:00 2001 From: Ken Bannister Date: Sun, 24 Feb 2019 06:06:30 -0500 Subject: [PATCH] net/nanocoap: add generic uint block option --- sys/include/net/nanocoap.h | 14 ++++++++++++++ sys/net/application_layer/nanocoap/nanocoap.c | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index 6f4daa40f0..7eabe5689a 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -1038,6 +1038,20 @@ static inline size_t coap_opt_put_block2(uint8_t *buf, uint16_t lastonum, return coap_opt_put_block(buf, lastonum, slicer, more, COAP_OPT_BLOCK2); } +/** + * @brief Encode the given uint option into buffer + * + * @param[out] buf buffer to write to + * @param[in] lastonum number of previous option (for delta calculation), + * or 0 for first option + * @param[in] onum number of option + * @param[in] value value to encode + * + * @returns amount of bytes written to @p buf + */ +size_t coap_opt_put_uint(uint8_t *buf, uint16_t lastonum, uint16_t onum, + uint32_t value); + /** * @brief Insert block option into buffer from block struct * diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index f5090e2a77..695165d52d 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -810,6 +810,14 @@ size_t coap_opt_put_string(uint8_t *buf, uint16_t lastonum, uint16_t optnum, return bufpos - buf; } +size_t coap_opt_put_uint(uint8_t *buf, uint16_t lastonum, uint16_t onum, + uint32_t value) +{ + unsigned uint_len = _encode_uint(&value); + + return coap_put_option(buf, lastonum, onum, (uint8_t *)&value, uint_len); +} + /* Common functionality for addition of an option */ static ssize_t _add_opt_pkt(coap_pkt_t *pkt, uint16_t optnum, const uint8_t *val, size_t val_len)