From a6f919ef3e1ed1f17d9f454a7f3d22b752278731 Mon Sep 17 00:00:00 2001 From: Ken Bannister Date: Thu, 14 Feb 2019 12:10:16 -0500 Subject: [PATCH] net/nanocoap: refactor block2_finish() --- sys/include/net/nanocoap.h | 41 +++++++++++++++++-- sys/net/application_layer/nanocoap/nanocoap.c | 13 ++++-- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index a9b2e46d36..100ea2742d 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -553,18 +553,51 @@ static inline ssize_t coap_get_uri_query(const coap_pkt_t *pkt, uint8_t *target) void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize, int more); +/** + * @brief Finish a block request (block1 or block2) + * + * This function finalizes the block response header + * + * Checks whether the `more` bit should be set in the block option and + * sets/clears it if required. Doesn't return the number of bytes, as this + * function overwrites bytes in the packet rather than adding new. + * + * @param[in] slicer Preallocated slicer struct to use + * @param[in] option option number (block1 or block2) + */ +void coap_block_finish(coap_block_slicer_t *slicer, uint16_t option); + +/** + * @brief Finish a block1 request + * + * This function finalizes the block1 response header + * + * Checks whether the `more` bit should be set in the block1 option and + * sets/clears it if required. Doesn't return the number of bytes, as this + * function overwrites bytes in the packet rather than adding new. + * + * @param[in] slicer Preallocated slicer struct to use + */ +static inline void coap_block1_finish(coap_block_slicer_t *slicer) +{ + coap_block_finish(slicer, COAP_OPT_BLOCK1); +} + /** * @brief Finish a block2 response * * This function finalizes the block2 response header * * Checks whether the `more` bit should be set in the block2 option and - * sets/clears it if required. Doesn't return the number of bytes as this - * overwrites bytes in the packet, it doesn't add new bytes to the packet. + * sets/clears it if required. Doesn't return the number of bytes, as this + * function overwrites bytes in the packet rather than adding new. * - * @param[inout] slicer Preallocated slicer struct to use + * @param[in] slicer Preallocated slicer struct to use */ -void coap_block2_finish(coap_block_slicer_t *slicer); +static inline void coap_block2_finish(coap_block_slicer_t *slicer) +{ + coap_block_finish(slicer, COAP_OPT_BLOCK2); +} /** * @brief Initialize a block2 slicer struct for writing the payload diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index e90478930d..3d21105181 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -901,7 +901,7 @@ void coap_block2_init(coap_pkt_t *pkt, coap_block_slicer_t *slicer) slicer->cur = 0; } -void coap_block2_finish(coap_block_slicer_t *slicer) +void coap_block_finish(coap_block_slicer_t *slicer, uint16_t option) { assert(slicer->opt); @@ -910,9 +910,16 @@ void coap_block2_finish(coap_block_slicer_t *slicer) * it's already in the buffer. So just point past the option. */ uint8_t *pos = slicer->opt + 1; uint16_t delta = _decode_value(*slicer->opt >> 4, &pos, slicer->opt + 3); - int more = (slicer->cur > slicer->end) ? 1 : 0; - coap_opt_put_block2(slicer->opt, COAP_OPT_BLOCK2 - delta, slicer, more); + /* Calculate the block uint value inline here rather than through + * coap_opt_put_block(). Conserves stack and avoids importing Buffer API + * functions when using Packet API. */ + uint32_t blkopt = (_slicer_blknum(slicer) << 4); + blkopt |= _size2szx(slicer->end - slicer->start); + blkopt |= ((slicer->cur > slicer->end) ? 0x8 : 0); + size_t olen = _encode_uint(&blkopt); + + coap_put_option(slicer->opt, option - delta, option, (uint8_t *)&blkopt, olen); } ssize_t coap_block2_build_reply(coap_pkt_t *pkt, unsigned code,