diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index 23899341e3..d781af8c56 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -811,6 +811,52 @@ static inline size_t coap_opt_put_block2(uint8_t *buf, uint16_t lastonum, return coap_opt_put_block(buf, lastonum, slicer, more, COAP_OPT_BLOCK2); } +/** + * @brief Insert block option into buffer from block struct + * + * @param[in] buf buffer to write to + * @param[in] lastonum last option number (must be < @p option) + * @param[in] block block option attribute struct + * @param[in] option option number (block1 or block2) + * + * @returns amount of bytes written to @p buf + */ +size_t coap_opt_put_block_object(uint8_t *buf, uint16_t lastonum, + coap_block1_t *block, uint16_t option); + +/** + * @brief Insert block1 option into buffer in control usage + * + * @param[in] buf buffer to write to + * @param[in] lastonum last option number (must be < 27) + * @param[in] block block option attribute struct + * + * @returns amount of bytes written to @p buf + */ +static inline size_t coap_opt_put_block1_control(uint8_t *buf, uint16_t lastonum, + coap_block1_t *block) +{ + return coap_opt_put_block_object(buf, lastonum, block, COAP_OPT_BLOCK1); +} + +/** + * @brief Insert block2 option into buffer in control usage + * + * Forces value of block 'more' attribute to zero, per spec. + * + * @param[in] buf buffer to write to + * @param[in] lastonum last option number (must be < 27) + * @param[in] block block option attribute struct + * + * @returns amount of bytes written to @p buf + */ +static inline size_t coap_opt_put_block2_control(uint8_t *buf, uint16_t lastonum, + coap_block1_t *block) +{ + block->more = 0; + return coap_opt_put_block_object(buf, lastonum, block, COAP_OPT_BLOCK2); +} + /** * @brief Encode the given string as multi-part option into buffer * diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index 57695fb042..ad6b3027ee 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -720,6 +720,15 @@ size_t coap_opt_put_block(uint8_t *buf, uint16_t lastonum, coap_block_slicer_t * return coap_put_option(buf, lastonum, option, (uint8_t *)&blkopt, olen); } +size_t coap_opt_put_block_object(uint8_t *buf, uint16_t lastonum, + coap_block1_t *block, uint16_t option) +{ + uint32_t blkopt = (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0); + size_t olen = _encode_uint(&blkopt); + + 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, const char *string, char separator) {