net/nanocoap: refactor block option put

This commit is contained in:
Ken Bannister 2019-01-06 11:19:15 -05:00
parent 7dd00b79e9
commit 4311f17e81
2 changed files with 50 additions and 3 deletions

View File

@ -751,6 +751,45 @@ ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags);
* Write PDU Options directly to the array of bytes for a message.
*/
/**@{*/
/**
* @brief Insert block option into buffer
*
* When calling this function to initialize a packet with a block option, the
* more flag must be set to prevent the creation of an option with a length too
* small to contain the size bit.
*
* @param[out] buf buffer to write to
* @param[in] lastonum number of previous option, must be < @p option
* @param[in] slicer coap blockwise slicer helper struct
* @param[in] more more flag (1 or 0)
* @param[in] option option number (block1 or block2)
*
* @returns amount of bytes written to @p buf
*/
size_t coap_opt_put_block(uint8_t *buf, uint16_t lastonum, coap_block_slicer_t *slicer,
bool more, uint16_t option);
/**
* @brief Insert block1 option into buffer
*
* When calling this function to initialize a packet with a block1 option, the
* more flag must be set to prevent the creation of an option with a length too
* small to contain the size bit.
*
* @param[out] buf buffer to write to
* @param[in] lastonum number of previous option (for delta calculation),
* must be < 27
* @param[in] slicer coap blockwise slicer helper struct
* @param[in] more more flag (1 or 0)
*
* @returns amount of bytes written to @p buf
*/
static inline size_t coap_opt_put_block1(uint8_t *buf, uint16_t lastonum,
coap_block_slicer_t *slicer, bool more)
{
return coap_opt_put_block(buf, lastonum, slicer, more, COAP_OPT_BLOCK1);
}
/**
* @brief Insert block2 option into buffer
*
@ -766,7 +805,11 @@ ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags);
*
* @returns amount of bytes written to @p buf
*/
size_t coap_opt_put_block2(uint8_t *buf, uint16_t lastonum, coap_block_slicer_t *slicer, bool more);
static inline size_t coap_opt_put_block2(uint8_t *buf, uint16_t lastonum,
coap_block_slicer_t *slicer, bool more)
{
return coap_opt_put_block(buf, lastonum, slicer, more, COAP_OPT_BLOCK2);
}
/**
* @brief Encode the given string as multi-part option into buffer

View File

@ -707,13 +707,17 @@ size_t coap_put_block1_ok(uint8_t *pkt_pos, coap_block1_t *block1, uint16_t last
}
}
size_t coap_opt_put_block2(uint8_t *buf, uint16_t lastonum, coap_block_slicer_t *slicer, bool more)
size_t coap_opt_put_block(uint8_t *buf, uint16_t lastonum, coap_block_slicer_t *slicer,
bool more, uint16_t option)
{
unsigned szx = _size2szx(slicer->end - slicer->start);
unsigned blknum = _slicer_blknum(slicer);
uint32_t blkopt = (blknum << 4) | szx | (more ? 0x8 : 0);
size_t olen = _encode_uint(&blkopt);
slicer->opt = buf;
return coap_put_option_block(buf, lastonum, blknum, szx, more, COAP_OPT_BLOCK2);
return coap_put_option(buf, lastonum, option, (uint8_t *)&blkopt, olen);
}
size_t coap_opt_put_string(uint8_t *buf, uint16_t lastonum, uint16_t optnum,