Skip to content

Commit

Permalink
sys/net/nanocoap: use coap_build_pkt_t in coap_blockwise_put_bytes()
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Mar 3, 2022
1 parent a2e2e2d commit 21bd79a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
24 changes: 12 additions & 12 deletions examples/nanocoap_server/coap_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
10 changes: 5 additions & 5 deletions sys/include/net/nanocoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
21 changes: 11 additions & 10 deletions sys/net/application_layer/nanocoap/nanocoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1061,20 +1061,20 @@ 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;
}
slicer->cur++;
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 */

Expand All @@ -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;
}

Expand All @@ -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);
Expand Down

0 comments on commit 21bd79a

Please sign in to comment.