diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index 0c69ff5e17..dea21512c0 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -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) * diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index 80c6ba359f..1d05a24e19 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -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) {