1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

nanocoap: add payload helper functions

This adds two functions `coap_payload_add()` and `coap_payload_advance()`.

 - `coap_payload_add()` will add n bytes to the payload buffer and advance
    payload pointer accordingly.

    const char hello[] = "Hello CoAP!";
    coap_payload_add(pkt, hello, sizeof(hello));

 - `coap_payload_advance()` will advance the payload buffer after data
    has been added to it.

    int len = snprintf(pkt->payload, pkt->payload_len, "%s %s!", "Hello", "CoAP");
    coap_payload_advance(pkt, len);

I considered adding an additional parameter to keep track of the total request size
(returned size from coap_opt_finish() incremented by each added payload fragment),
but decided against it to keep consistency with the existing API.
This commit is contained in:
Benjamin Valentin 2020-03-26 14:32:12 +01:00
parent cba1a2da0c
commit 2751708341
2 changed files with 54 additions and 0 deletions

View File

@ -1469,6 +1469,48 @@ int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len);
*/
void coap_pkt_init(coap_pkt_t *pkt, uint8_t *buf, size_t len, size_t header_len);
/**
* @brief Advance the payload pointer.
*
* @pre You added @p len bytes of data to `pkt->payload`.
*
* You can add payload to a CoAP request by writing data directly to
* `pkt->payload`.
* This convenience function takes care of advancing the payload pointer
* afterwards.
*
* @param[out] pkt pkt to which payload was added
* @param[in] len length of payload
*/
static inline void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
{
pkt->payload += len;
pkt->payload_len -= len;
}
/**
* @brief Add payload data to the CoAP request.
*
* @pre @ref coap_opt_finish must have been called before with
* the @ref COAP_OPT_FINISH_PAYLOAD option.
*
* The function copies @p data into the payload buffer of @p pkt and
* advances the payload pointer.
*
* This is just a convenience function, you can also directly write
* to `pkt->payload` if you have a function that outputs payload to
* a buffer.
* In this case you should instead call @ref coap_payload_advance_bytes.
*
* @param[out] pkt pkt to add payload to
* @param[in] data payload data
* @param[in] len length of payload
*
* @returns number of payload bytes added on success
* @returns < 0 on error
*/
ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len);
/**
* @brief Create CoAP reply (convenience function)
*

View File

@ -922,6 +922,18 @@ ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags)
return pkt->payload - (uint8_t *)pkt->hdr;
}
ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len)
{
if (pkt->payload_len < len) {
return -ENOSPC;
}
memcpy(pkt->payload, data, len);
coap_payload_advance_bytes(pkt, len);
return len;
}
void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize,
int more)
{