Skip to content

Commit

Permalink
net/coap: add generic get block function
Browse files Browse the repository at this point in the history
  • Loading branch information
kb2ma committed Feb 24, 2019
1 parent d8ccda5 commit 0ff2856
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
34 changes: 30 additions & 4 deletions sys/include/net/nanocoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,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
*
Expand All @@ -643,23 +663,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
Expand Down
25 changes: 6 additions & 19 deletions sys/net/application_layer/nanocoap/nanocoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -599,30 +599,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)
Expand Down

0 comments on commit 0ff2856

Please sign in to comment.