1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

net/nanocoap: refactor block2_finish()

This commit is contained in:
Ken Bannister 2019-02-14 12:10:16 -05:00
parent 86edea81be
commit a6f919ef3e
2 changed files with 47 additions and 7 deletions

View File

@ -553,18 +553,51 @@ static inline ssize_t coap_get_uri_query(const coap_pkt_t *pkt, uint8_t *target)
void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize,
int more);
/**
* @brief Finish a block request (block1 or block2)
*
* This function finalizes the block response header
*
* Checks whether the `more` bit should be set in the block option and
* sets/clears it if required. Doesn't return the number of bytes, as this
* function overwrites bytes in the packet rather than adding new.
*
* @param[in] slicer Preallocated slicer struct to use
* @param[in] option option number (block1 or block2)
*/
void coap_block_finish(coap_block_slicer_t *slicer, uint16_t option);
/**
* @brief Finish a block1 request
*
* This function finalizes the block1 response header
*
* Checks whether the `more` bit should be set in the block1 option and
* sets/clears it if required. Doesn't return the number of bytes, as this
* function overwrites bytes in the packet rather than adding new.
*
* @param[in] slicer Preallocated slicer struct to use
*/
static inline void coap_block1_finish(coap_block_slicer_t *slicer)
{
coap_block_finish(slicer, COAP_OPT_BLOCK1);
}
/**
* @brief Finish a block2 response
*
* This function finalizes the block2 response header
*
* Checks whether the `more` bit should be set in the block2 option and
* sets/clears it if required. Doesn't return the number of bytes as this
* overwrites bytes in the packet, it doesn't add new bytes to the packet.
* sets/clears it if required. Doesn't return the number of bytes, as this
* function overwrites bytes in the packet rather than adding new.
*
* @param[inout] slicer Preallocated slicer struct to use
* @param[in] slicer Preallocated slicer struct to use
*/
void coap_block2_finish(coap_block_slicer_t *slicer);
static inline void coap_block2_finish(coap_block_slicer_t *slicer)
{
coap_block_finish(slicer, COAP_OPT_BLOCK2);
}
/**
* @brief Initialize a block2 slicer struct for writing the payload

View File

@ -901,7 +901,7 @@ void coap_block2_init(coap_pkt_t *pkt, coap_block_slicer_t *slicer)
slicer->cur = 0;
}
void coap_block2_finish(coap_block_slicer_t *slicer)
void coap_block_finish(coap_block_slicer_t *slicer, uint16_t option)
{
assert(slicer->opt);
@ -910,9 +910,16 @@ 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) ? 1 : 0;
coap_opt_put_block2(slicer->opt, COAP_OPT_BLOCK2 - delta, slicer, more);
/* Calculate the block uint value inline here rather than through
* coap_opt_put_block(). Conserves stack and avoids importing Buffer API
* functions when using Packet API. */
uint32_t blkopt = (_slicer_blknum(slicer) << 4);
blkopt |= _size2szx(slicer->end - slicer->start);
blkopt |= ((slicer->cur > slicer->end) ? 0x8 : 0);
size_t olen = _encode_uint(&blkopt);
coap_put_option(slicer->opt, option - delta, option, (uint8_t *)&blkopt, olen);
}
ssize_t coap_block2_build_reply(coap_pkt_t *pkt, unsigned code,