From 54d3898295a30bd6984f68d8d296115da9c71991 Mon Sep 17 00:00:00 2001 From: Ken Bannister Date: Thu, 14 Feb 2019 12:16:03 -0500 Subject: [PATCH] net/nanocoap: add block slicer init function --- sys/include/net/nanocoap.h | 12 ++++++++++++ sys/net/application_layer/nanocoap/nanocoap.c | 13 ++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index 04a7348feaa7e..fa8dd5104672a 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -610,6 +610,18 @@ static inline void coap_block2_finish(coap_block_slicer_t *slicer) */ void coap_block2_init(coap_pkt_t *pkt, coap_block_slicer_t *slicer); +/** + * @brief Initialize a block slicer struct from content information + * + * @param[out] slicer slicer struct to initialize + * @param[in] blknum offset from the beginning of content, in terms of + @p blksize byte blocks + * @param[in] blksize size of each block; must be a power of 2 between 16 + * and 2 raised to #NANOCOAP_BLOCK_SIZE_EXP_MAX + */ +void coap_block_slicer_init(coap_block_slicer_t *slicer, size_t blknum, + size_t blksize); + /** * @brief Add a byte array to a block2 reply. * diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index 3d2110518198c..e5f23a412658d 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -883,6 +883,14 @@ void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize, block->more = more; } +void coap_block_slicer_init(coap_block_slicer_t *slicer, size_t blknum, + size_t blksize) +{ + slicer->start = blknum * blksize; + slicer->end = slicer->start + blksize; + slicer->cur = 0; +} + void coap_block2_init(coap_pkt_t *pkt, coap_block_slicer_t *slicer) { uint32_t blknum; @@ -896,9 +904,8 @@ void coap_block2_init(coap_pkt_t *pkt, coap_block_slicer_t *slicer) szx = NANOCOAP_BLOCK_SIZE_EXP_MAX - 4; } } - slicer->start = blknum * coap_szx2size(szx); - slicer->end = slicer->start + coap_szx2size(szx); - slicer->cur = 0; + + coap_block_slicer_init(slicer, blknum, coap_szx2size(szx)); } void coap_block_finish(coap_block_slicer_t *slicer, uint16_t option)