diff --git a/examples/nanocoap_server/coap_handler.c b/examples/nanocoap_server/coap_handler.c index 1e827561c953..af1ac111b8b8 100644 --- a/examples/nanocoap_server/coap_handler.c +++ b/examples/nanocoap_server/coap_handler.c @@ -55,19 +55,19 @@ static ssize_t _riot_block2_handler(coap_pkt_t *pkt, coap_build_pkt_t *response, *response->cur++ = 0xff; /* Add actual content */ - response->cur += coap_blockwise_put_bytes(&slicer, response->cur, block2_intro, sizeof(block2_intro)-1); - response->cur += coap_blockwise_put_bytes(&slicer, response->cur, (uint8_t*)RIOT_VERSION, strlen(RIOT_VERSION)); - response->cur += coap_blockwise_put_char(&slicer, response->cur, ')'); - response->cur += coap_blockwise_put_bytes(&slicer, response->cur, block2_board, sizeof(block2_board)-1); - response->cur += coap_blockwise_put_bytes(&slicer, response->cur, (uint8_t*)RIOT_BOARD, strlen(RIOT_BOARD)); - response->cur += coap_blockwise_put_bytes(&slicer, response->cur, block2_mcu, sizeof(block2_mcu)-1); - response->cur += coap_blockwise_put_bytes(&slicer, response->cur, (uint8_t*)RIOT_MCU, strlen(RIOT_MCU)); + coap_blockwise_put_bytes(&slicer, response, block2_intro, sizeof(block2_intro)-1); + coap_blockwise_put_bytes(&slicer, response, (uint8_t*)RIOT_VERSION, strlen(RIOT_VERSION)); + coap_blockwise_put_char(&slicer, response, ')'); + coap_blockwise_put_bytes(&slicer, response, block2_board, sizeof(block2_board)-1); + coap_blockwise_put_bytes(&slicer, response, (uint8_t*)RIOT_BOARD, strlen(RIOT_BOARD)); + coap_blockwise_put_bytes(&slicer, response, block2_mcu, sizeof(block2_mcu)-1); + coap_blockwise_put_bytes(&slicer, response, (uint8_t*)RIOT_MCU, strlen(RIOT_MCU)); /* To demonstrate individual chars */ - response->cur += coap_blockwise_put_char(&slicer, response->cur, ' '); - response->cur += coap_blockwise_put_char(&slicer, response->cur, 'M'); - response->cur += coap_blockwise_put_char(&slicer, response->cur, 'C'); - response->cur += coap_blockwise_put_char(&slicer, response->cur, 'U'); - response->cur += coap_blockwise_put_char(&slicer, response->cur, '.'); + coap_blockwise_put_char(&slicer, response, ' '); + coap_blockwise_put_char(&slicer, response, 'M'); + coap_blockwise_put_char(&slicer, response, 'C'); + coap_blockwise_put_char(&slicer, response, 'U'); + coap_blockwise_put_char(&slicer, response, '.'); return coap_block2_build_reply(pkt, COAP_CODE_205, response, &slicer); } diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index 32a8e841750f..ea6db6438c64 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -823,14 +823,14 @@ void coap_block_slicer_init(coap_block_slicer_t *slicer, size_t blknum, * parts that are outside the current block2 request. * * @param[in] slicer slicer to use - * @param[in] bufpos pointer to the current payload buffer position + * @param[out] pkt destination packet * @param[in] c byte array to copy * @param[in] len length of the byte array * * @returns Number of bytes written to @p bufpos */ -size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, uint8_t *bufpos, - const uint8_t *c, size_t len); +size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, coap_build_pkt_t *pkt, + const void *c, size_t len); /** * @brief Add a single character to a block2 reply. @@ -840,12 +840,12 @@ size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, uint8_t *bufpos, * when the character is outside the current block2 request. * * @param[in] slicer slicer to use - * @param[in] bufpos pointer to the current payload buffer position + * @param[out] pkt destination packet * @param[in] c character to write * * @returns Number of bytes written to @p bufpos */ -size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, uint8_t *bufpos, char c); +size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, coap_build_pkt_t *pkt, char c); /** * @brief Block option getter diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index ccc3864f710b..2fb907c840f6 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -1061,11 +1061,11 @@ ssize_t coap_block2_build_reply(coap_pkt_t *pkt, unsigned code, return coap_build_reply(pkt, code, response); } -size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, uint8_t *bufpos, char c) +size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, coap_build_pkt_t *pkt, char c) { /* Only copy the char if it is within the window */ if ((slicer->start <= slicer->cur) && (slicer->cur < slicer->end)) { - *bufpos = c; + *pkt->cur++ = c; slicer->cur++; return 1; } @@ -1073,8 +1073,8 @@ size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, uint8_t *bufpos, cha return 0; } -size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, uint8_t *bufpos, - const uint8_t *c, size_t len) +size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, coap_build_pkt_t *pkt, + const void *c, size_t len) { size_t str_len = 0; /* Length of the string to copy */ @@ -1097,8 +1097,10 @@ size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, uint8_t *bufpos, } /* Only copy the relevant part of the string to the buffer */ - memcpy(bufpos, c + str_offset, str_len); + memcpy(pkt->cur, (const uint8_t *)c + str_offset, str_len); slicer->cur += len; + pkt->cur += str_len; + return str_len; } @@ -1115,13 +1117,12 @@ ssize_t coap_well_known_core_default_handler(coap_pkt_t *pkt, coap_build_pkt_t * for (unsigned i = 0; i < coap_resources_numof; i++) { if (i) { - response->cur += coap_blockwise_put_char(&slicer, response->cur, ','); + coap_blockwise_put_char(&slicer, response, ','); } - response->cur += coap_blockwise_put_char(&slicer, response->cur, '<'); unsigned url_len = strlen(coap_resources[i].path); - response->cur += coap_blockwise_put_bytes(&slicer, response->cur, - (uint8_t*)coap_resources[i].path, url_len); - response->cur += coap_blockwise_put_char(&slicer, response->cur, '>'); + coap_blockwise_put_char(&slicer, response, '<'); + coap_blockwise_put_bytes(&slicer, response, coap_resources[i].path, url_len); + coap_blockwise_put_char(&slicer, response, '>'); } return coap_block2_build_reply(pkt, COAP_CODE_205, response, &slicer);