Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pr/nanocoap/block2 fixes 2 #4

Merged
merged 5 commits into from
Oct 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/nanocoap_server/coap_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 },
};

Expand Down
6 changes: 3 additions & 3 deletions sys/include/net/nanocoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*
Expand Down Expand Up @@ -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.
Expand Down
30 changes: 18 additions & 12 deletions sys/net/application_layer/nanocoap/nanocoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,8 @@ static unsigned _size2szx(size_t size)
{
unsigned szx = 0;
assert(size <= 1024);
while(size) {

while (size) {
size = size >> 1;
szx++;
}
Expand All @@ -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++;
}
Expand All @@ -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)
Expand All @@ -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);
Expand All @@ -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)
Expand All @@ -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);
}
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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)
Expand Down