Skip to content

Commit

Permalink
Merge pull request #20950 from maribu/sys/net/nanocoap/coap_get_respo…
Browse files Browse the repository at this point in the history
…nse_hdr_len

sys/net/nanocoap: add and use coap_get_response_hdr_len()
  • Loading branch information
maribu authored Nov 5, 2024
2 parents 2734366 + 68beb52 commit c70075b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/nanocoap_server/coap_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static ssize_t _riot_block2_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, c
(void)context;
coap_block_slicer_t slicer;
coap_block2_init(pkt, &slicer);
uint8_t *payload = buf + coap_get_total_hdr_len(pkt);
uint8_t *payload = buf + coap_get_response_hdr_len(pkt);

uint8_t *bufpos = payload;

Expand Down
19 changes: 19 additions & 0 deletions sys/include/net/nanocoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,22 @@ static inline unsigned coap_get_total_hdr_len(const coap_pkt_t *pkt)
coap_get_token_len(pkt);
}

/**
* @brief Get the header length a response to the given packet will have
*
* @param[in] pkt CoAP packet to reply to
* @return Length of the response header including token excluding
* CoAP options and any payload marker
*
* @note The main use case is the use of @ref coap_block2_build_reply, which
* is building the CoAP header of the response after options and
* payload have been added.
*/
static inline unsigned coap_get_response_hdr_len(const coap_pkt_t *pkt)
{
return coap_get_total_hdr_len(pkt);
}

/**
* @brief Write the given raw message code to given CoAP header
*
Expand Down Expand Up @@ -1939,6 +1955,9 @@ static inline size_t coap_put_option_ct(uint8_t *buf, uint16_t lastonum,
* @param[in] payload_len length of payload
* @param[in] slicer slicer to use
*
* @warning Use @ref coap_get_response_hdr_len to determine the size of the
* header this will write.
*
* @returns size of reply packet on success
* @returns <0 on error
*/
Expand Down
11 changes: 7 additions & 4 deletions sys/include/net/nanocoap_sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,13 @@
* Start with coap_block2_init() to read the client request and initialize a
* coap_slicer_t struct with the size and location for this slice of the
* overall payload. Then write the block2 option in the response with
* coap_opt_put_block2(). The option includes an indicator ("more") that a
* slice completes the overall payload transfer. You may not know the value for
* _more_ at this point, but you must initialize the space in the packet for
* the option before writing the payload. The option is rewritten later.
* coap_opt_put_block2(). Use @ref coap_get_response_hdr_len to get the length
* of the response header: This will be the offset in the buffer where you
* should start adding options. The Block2 option includes an indicator
* ("more") that a slice completes the overall payload transfer. You may not
* know the value for _more_ at this point, but you must initialize the space
* in the packet for the option before writing the payload. The option is
* rewritten later.
*
* Next, use the coap_blockwise_put_xxx() functions to write the payload
* content. These functions use the coap_block_slicer_t to enable or disable
Expand Down
2 changes: 1 addition & 1 deletion sys/net/application_layer/nanocoap/nanocoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,7 @@ ssize_t coap_well_known_core_default_handler(coap_pkt_t *pkt, uint8_t *buf, \
(void)context;
coap_block_slicer_t slicer;
coap_block2_init(pkt, &slicer);
uint8_t *payload = buf + coap_get_total_hdr_len(pkt);
uint8_t *payload = buf + coap_get_response_hdr_len(pkt);
uint8_t *bufpos = payload;
bufpos += coap_put_option_ct(bufpos, 0, COAP_FORMAT_LINK);
bufpos += coap_opt_put_block2(bufpos, COAP_OPT_CONTENT_FORMAT, &slicer, 1);
Expand Down

0 comments on commit c70075b

Please sign in to comment.