From dae9c4ab8927029f654fdc28145dbbe125cd586a Mon Sep 17 00:00:00 2001 From: Ken Bannister Date: Sun, 24 Feb 2019 06:31:58 -0500 Subject: [PATCH] net/nanocoap: use coap_opt_add_uint() and remove unused --- sys/include/net/nanocoap.h | 21 ++++++-- sys/net/application_layer/nanocoap/nanocoap.c | 54 +++++-------------- 2 files changed, 28 insertions(+), 47 deletions(-) diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index 7eabe5689a..635399518e 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -1077,7 +1077,8 @@ size_t coap_opt_put_block_object(uint8_t *buf, uint16_t lastonum, static inline size_t coap_opt_put_block1_control(uint8_t *buf, uint16_t lastonum, coap_block1_t *block) { - return coap_opt_put_block_object(buf, lastonum, block, COAP_OPT_BLOCK1); + return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK1, + (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0)); } /** @@ -1094,8 +1095,9 @@ static inline size_t coap_opt_put_block1_control(uint8_t *buf, uint16_t lastonum static inline size_t coap_opt_put_block2_control(uint8_t *buf, uint16_t lastonum, coap_block1_t *block) { - block->more = 0; - return coap_opt_put_block_object(buf, lastonum, block, COAP_OPT_BLOCK2); + /* block.more must be zero, so no need to 'or' it in */ + return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK2, + (block->blknum << 4) | block->szx); } /** @@ -1230,7 +1232,12 @@ size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum, const uin * * @returns amount of bytes written to @p buf */ -size_t coap_put_option_block1(uint8_t *buf, uint16_t lastonum, unsigned blknum, unsigned szx, int more); +static inline size_t coap_put_option_block1(uint8_t *buf, uint16_t lastonum, + unsigned blknum, unsigned szx, int more) +{ + return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK1, + (blknum << 4) | szx | (more ? 0x8 : 0)); +} /** * @brief Insert content type option into buffer @@ -1242,7 +1249,11 @@ size_t coap_put_option_block1(uint8_t *buf, uint16_t lastonum, unsigned blknum, * * @returns amount of bytes written to @p buf */ -size_t coap_put_option_ct(uint8_t *buf, uint16_t lastonum, uint16_t content_type); +static inline size_t coap_put_option_ct(uint8_t *buf, uint16_t lastonum, + uint16_t content_type) +{ + return coap_opt_put_uint(buf, lastonum, COAP_OPT_CONTENT_FORMAT, content_type); +} /**@}*/ diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index 695165d52d..c1ef9205b7 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -653,20 +653,6 @@ size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum, const uin return (size_t)n; } -size_t coap_put_option_ct(uint8_t *buf, uint16_t lastonum, uint16_t content_type) -{ - if (content_type == 0) { - return coap_put_option(buf, lastonum, COAP_OPT_CONTENT_FORMAT, NULL, 0); - } - else if (content_type <= 255) { - uint8_t tmp = content_type; - return coap_put_option(buf, lastonum, COAP_OPT_CONTENT_FORMAT, &tmp, sizeof(tmp)); - } - else { - return coap_put_option(buf, lastonum, COAP_OPT_CONTENT_FORMAT, (uint8_t *)&content_type, sizeof(content_type)); - } -} - static unsigned _size2szx(size_t size) { unsigned szx = 0; @@ -694,46 +680,30 @@ static unsigned _slicer_blknum(coap_block_slicer_t *slicer) return blknum; } -static size_t coap_put_option_block(uint8_t *buf, uint16_t lastonum, unsigned blknum, unsigned szx, int more, uint16_t option) -{ - uint32_t blkopt = (blknum << 4) | szx | (more ? 0x8 : 0); - size_t olen = _encode_uint(&blkopt); - - return coap_put_option(buf, lastonum, option, (uint8_t *)&blkopt, olen); -} - -size_t coap_put_option_block1(uint8_t *buf, uint16_t lastonum, unsigned blknum, unsigned szx, int more) -{ - return coap_put_option_block(buf, lastonum, blknum, szx, more, COAP_OPT_BLOCK1); -} - -int coap_get_block(coap_pkt_t *pkt, coap_block1_t *block, uint16_t blkopt) +int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block1) { uint32_t blknum; unsigned szx; - block->more = coap_get_blockopt(pkt, blkopt, &blknum, &szx); - if (block->more >= 0) { - block->offset = blknum << (szx + 4); + block1->more = coap_get_blockopt(pkt, COAP_OPT_BLOCK1, &blknum, &szx); + if (block1->more >= 0) { + block1->offset = blknum << (szx + 4); } else { - block->offset = 0; + block1->offset = 0; } - block->blknum = blknum; - block->szx = szx; + block1->blknum = blknum; + block1->szx = szx; - return (block->more >= 0); + return (block1->more >= 0); } -int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block) +int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block2) { - return coap_get_block(pkt, block, COAP_OPT_BLOCK1); -} - -int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block) -{ - return coap_get_block(pkt, block, COAP_OPT_BLOCK2); + block2->more = coap_get_blockopt(pkt, COAP_OPT_BLOCK2, &block2->blknum, + &block2->szx); + return (block2->more >= 0); } size_t coap_put_block1_ok(uint8_t *pkt_pos, coap_block1_t *block1, uint16_t lastonum)