diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index dea21512c0..1b5d773382 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -78,6 +78,7 @@ #define NET_NANOCOAP_H #include +#include #include #include #include @@ -1511,6 +1512,21 @@ static inline void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len) */ ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len); +/** + * @brief Add a single character to the payload data of the CoAP request + * + * This function is used to add single characters to a CoAP payload data. It + * checks whether the character can be added to the buffer and ignores if the + * payload area is already exhausted. + * + * @param[out] pkt pkt to add payload to + * @param[in] c character to write + * + * @returns number of payload bytes added on success (always one) + * @returns < 0 on error + */ +ssize_t coap_payload_put_char(coap_pkt_t *pkt, char c); + /** * @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 1d05a24e19..d3c5d3f1c4 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -934,6 +934,18 @@ ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len) return len; } +ssize_t coap_payload_put_char(coap_pkt_t *pkt, char c) +{ + if (pkt->payload_len < 1) { + return -ENOSPC; + } + + *pkt->payload++ = c; + pkt->payload_len--; + + return 1; +} + void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize, int more) {