Skip to content

Commit d90f022

Browse files
committed
net/coap: improve slicer to block helper
1 parent 1420c8e commit d90f022

1 file changed

Lines changed: 6 additions & 19 deletions

File tree

sys/net/application_layer/nanocoap/nanocoap.c

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ static unsigned _size2szx(size_t size)
585585
return szx - 5;
586586
}
587587

588-
static unsigned _slicer_blknum(coap_block_slicer_t *slicer)
588+
static unsigned _slicer2blkopt(coap_block_slicer_t *slicer, bool more)
589589
{
590590
size_t blksize = slicer->end - slicer->start;
591591
size_t start = slicer->start;
@@ -595,7 +595,8 @@ static unsigned _slicer_blknum(coap_block_slicer_t *slicer)
595595
start -= blksize;
596596
blknum++;
597597
}
598-
return blknum;
598+
599+
return (blknum << 4) | _size2szx(blksize) | (more ? 0x8 : 0);
599600
}
600601

601602
int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block1)
@@ -637,14 +638,9 @@ size_t coap_put_block1_ok(uint8_t *pkt_pos, coap_block1_t *block1, uint16_t last
637638
size_t coap_opt_put_block(uint8_t *buf, uint16_t lastonum, coap_block_slicer_t *slicer,
638639
bool more, uint16_t option)
639640
{
640-
coap_block1_t block;
641-
642-
coap_block_object_init(&block, _slicer_blknum(slicer),
643-
slicer->end - slicer->start, more);
644-
645641
slicer->opt = buf;
646642

647-
return coap_opt_put_block_object(buf, lastonum, &block, option);
643+
return coap_opt_put_uint(buf, lastonum, option, _slicer2blkopt(slicer, more));
648644
}
649645

650646
size_t coap_opt_put_block_object(uint8_t *buf, uint16_t lastonum,
@@ -792,13 +788,9 @@ ssize_t coap_opt_add_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t value)
792788
ssize_t coap_opt_add_block(coap_pkt_t *pkt, coap_block_slicer_t *slicer,
793789
bool more, uint16_t option)
794790
{
795-
uint32_t blkopt = (_slicer_blknum(slicer) << 4);
796-
blkopt |= _size2szx(slicer->end - slicer->start);
797-
blkopt |= (more ? 0x8 : 0);
798-
799791
slicer->opt = pkt->payload;
800792

801-
return coap_opt_add_uint(pkt, option, blkopt);
793+
return coap_opt_add_uint(pkt, option, _slicer2blkopt(slicer, more));
802794
}
803795

804796
ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags)
@@ -861,12 +853,7 @@ void coap_block_finish(coap_block_slicer_t *slicer, uint16_t option)
861853
uint8_t *pos = slicer->opt + 1;
862854
uint16_t delta = _decode_value(*slicer->opt >> 4, &pos, slicer->opt + 3);
863855

864-
/* Calculate the block uint value inline here rather than through
865-
* coap_opt_put_block(). Conserves stack and avoids importing Buffer API
866-
* functions when using Packet API. */
867-
uint32_t blkopt = (_slicer_blknum(slicer) << 4);
868-
blkopt |= _size2szx(slicer->end - slicer->start);
869-
blkopt |= ((slicer->cur > slicer->end) ? 0x8 : 0);
856+
uint32_t blkopt = _slicer2blkopt(slicer, slicer->cur > slicer->end);
870857
size_t olen = _encode_uint(&blkopt);
871858

872859
coap_put_option(slicer->opt, option - delta, option, (uint8_t *)&blkopt, olen);

0 commit comments

Comments
 (0)