From 2abcbc0d2047351dbc5aeb61bf4e623dddda6b86 Mon Sep 17 00:00:00 2001 From: Ken Bannister Date: Sun, 24 Feb 2019 06:57:00 -0500 Subject: [PATCH] net/coap: add generic get block function --- sys/include/net/nanocoap.h | 34 ++++++++++++++++--- sys/net/application_layer/nanocoap/nanocoap.c | 25 ++++---------- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index 0ade92629f..cf53564acc 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -717,6 +717,26 @@ size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, uint8_t *bufpos, */ size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, uint8_t *bufpos, char c); +/** + * @brief Block option getter + * + * This function gets a CoAP packet's block option and parses it into a helper + * structure. + * + * If no block option is present in @p pkt, the values in @p block will be + * initialized with zero. That implies both block->offset and block->more are + * also valid in that case, as packet with offset==0 and more==0 means it contains + * all the payload for the corresponding request. + * + * @param[in] pkt pkt to work on + * @param[out] block ptr to preallocated coap_block1_t structure + * @param[in] option block1 or block2 + * + * @returns 0 if block option not present + * @returns 1 if structure has been filled + */ +int coap_get_block(coap_pkt_t *pkt, coap_block1_t *block, uint16_t option); + /** * @brief Block1 option getter * @@ -729,23 +749,29 @@ size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, uint8_t *bufpos, cha * all the payload for the corresponding request. * * @param[in] pkt pkt to work on - * @param[out] block1 ptr to preallocated coap_block1_t structure + * @param[out] block ptr to preallocated coap_block1_t structure * * @returns 0 if block1 option not present * @returns 1 if structure has been filled */ -int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block1); +static inline int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block) +{ + return coap_get_block(pkt, block, COAP_OPT_BLOCK1); +} /** * @brief Block2 option getter * * @param[in] pkt pkt to work on - * @param[out] block2 ptr to preallocated coap_block1_t structure + * @param[out] block ptr to preallocated coap_block1_t structure * * @returns 0 if block2 option not present * @returns 1 if structure has been filled */ -int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block2); +static inline int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block) +{ + return coap_get_block(pkt, block, COAP_OPT_BLOCK2); +} /** * @brief Generic block option getter diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index dbe2ce4b0c..2b8eec2287 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -681,30 +681,17 @@ static unsigned _slicer2blkopt(coap_block_slicer_t *slicer, bool more) return (blknum << 4) | _size2szx(blksize) | (more ? 0x8 : 0); } -int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block1) +int coap_get_block(coap_pkt_t *pkt, coap_block1_t *block, uint16_t option) { - uint32_t blknum; - unsigned szx; - - block1->more = coap_get_blockopt(pkt, COAP_OPT_BLOCK1, &blknum, &szx); - if (block1->more >= 0) { - block1->offset = blknum << (szx + 4); + block->more = coap_get_blockopt(pkt, option, &block->blknum, &block->szx); + if (block->more >= 0) { + block->offset = block->blknum << (block->szx + 4); } else { - block1->offset = 0; + block->offset = 0; } - block1->blknum = blknum; - block1->szx = szx; - - return (block1->more >= 0); -} - -int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block2) -{ - block2->more = coap_get_blockopt(pkt, COAP_OPT_BLOCK2, &block2->blknum, - &block2->szx); - return (block2->more >= 0); + return (block->more >= 0); } size_t coap_put_block1_ok(uint8_t *pkt_pos, coap_block1_t *block1, uint16_t lastonum)