diff --git a/examples/nanocoap_server/coap_handler.c b/examples/nanocoap_server/coap_handler.c index ef38b19e78c7..156747c839bf 100644 --- a/examples/nanocoap_server/coap_handler.c +++ b/examples/nanocoap_server/coap_handler.c @@ -59,7 +59,7 @@ static ssize_t _riot_block2_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, v unsigned payload_len = bufpos - payload; return coap_block2_build_reply(pkt, COAP_CODE_205, - buf, len, payload_len, &slicer); + buf, len, payload_len, &slicer); } static ssize_t _riot_value_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context) @@ -146,8 +146,8 @@ ssize_t _sha256_handler(coap_pkt_t* pkt, uint8_t *buf, size_t len, void *context const coap_resource_t coap_resources[] = { COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER, { "/riot/board", COAP_GET, _riot_board_handler, NULL }, - { "/riot/description", COAP_GET, _riot_block2_handler, NULL }, { "/riot/value", COAP_GET | COAP_PUT | COAP_POST, _riot_value_handler, NULL }, + { "/riot/ver", COAP_GET, _riot_block2_handler, NULL }, { "/sha256", COAP_POST, _sha256_handler, NULL }, }; diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index 16be251ab35c..b2ba50e2222c 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -547,7 +547,7 @@ ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags); * * @param[out] buf buffer to write to * @param[in] lastonum number of previous option (for delta calculation), - * must be < 27 + * must be < 23 * @param[in] slicer coap blockwise slicer helper struct * @param[in] more more flag (1 or 0) * @@ -711,8 +711,8 @@ void coap_block2_finish(coap_block_slicer_t *slicer); * @returns <0 on error */ ssize_t coap_block2_build_reply(coap_pkt_t *pkt, unsigned code, - uint8_t *rbuf, unsigned rlen, unsigned payload_len, - coap_block_slicer_t *slicer); + uint8_t *rbuf, unsigned rlen, unsigned payload_len, + coap_block_slicer_t *slicer); /** * @brief Add a single character to a block2 reply. diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index 848e788db591..0c9ebb25eef8 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -550,7 +550,8 @@ static unsigned _size2szx(size_t size) { unsigned szx = 0; assert(size <= 1024); - while(size) { + + while (size) { size = size >> 1; szx++; } @@ -564,7 +565,8 @@ static unsigned _slicer_blknum(coap_block_slicer_t *slicer) size_t blksize = slicer->end - slicer->start; size_t start = slicer->start; unsigned blknum = 0; - while(start > 0) { + + while (start > 0) { start -= blksize; blknum++; } @@ -575,7 +577,8 @@ static size_t coap_put_option_block(uint8_t *buf, uint16_t lastonum, unsigned bl { 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); + + 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) @@ -587,6 +590,7 @@ int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block1) { 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); @@ -604,8 +608,8 @@ int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block1) 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; + &block2->szx); + return (block2->more >= 0); } size_t coap_put_block1_ok(uint8_t *pkt_pos, coap_block1_t *block1, uint16_t lastonum) @@ -622,6 +626,7 @@ size_t coap_opt_put_block2(uint8_t *buf, uint16_t lastonum, coap_block_slicer_t { unsigned szx = _size2szx(slicer->end - slicer->start); unsigned blknum = _slicer_blknum(slicer); + slicer->opt = buf; return coap_put_option_block(buf, lastonum, blknum, szx, more, COAP_OPT_BLOCK2); } @@ -748,6 +753,7 @@ void coap_block2_init(coap_pkt_t *pkt, coap_block_slicer_t *slicer) { uint32_t blknum; unsigned szx; + /* Retrieve the block2 option from the client request */ if (coap_get_blockopt(pkt, COAP_OPT_BLOCK2, &blknum, &szx) >= 0) { /* Use the client requested block size if it is smaller than our own @@ -770,14 +776,14 @@ 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) ? 0x80 : 0; + int more = (slicer->cur > slicer->end) ? 1 : 0; coap_opt_put_block2(slicer->opt, COAP_OPT_BLOCK2 - delta, slicer, more); } ssize_t coap_block2_build_reply(coap_pkt_t *pkt, unsigned code, - uint8_t *rbuf, unsigned rlen, unsigned payload_len, - coap_block_slicer_t *slicer) + uint8_t *rbuf, unsigned rlen, unsigned payload_len, + coap_block_slicer_t *slicer) { /* Check if the generated data filled the requested block */ if (slicer->cur < slicer->start) { @@ -800,14 +806,14 @@ size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, uint8_t *bufpos, cha } size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, uint8_t *bufpos, - const uint8_t *c, size_t len) + const uint8_t *c, size_t len) { size_t str_len = 0; /* Length of the string to copy */ /* Calculate start offset of the supplied string */ size_t str_offset = (slicer->start > slicer->cur) - ? slicer->start - slicer->cur - : 0; + ? slicer->start - slicer->cur + : 0; /* Check for string before or beyond window */ if ((slicer->cur >= slicer->end) || (str_offset > len)) { @@ -854,7 +860,7 @@ ssize_t coap_well_known_core_default_handler(coap_pkt_t *pkt, uint8_t *buf, \ unsigned payload_len = bufpos - payload; return coap_block2_build_reply(pkt, COAP_CODE_205, buf, len, payload_len, - &slicer); + &slicer); } unsigned coap_get_len(coap_pkt_t *pkt)