diff --git a/include/net/buf.h b/include/net/buf.h index dab7499ccd1..28dc23f9b82 100644 --- a/include/net/buf.h +++ b/include/net/buf.h @@ -112,6 +112,21 @@ static inline void net_buf_simple_init(struct net_buf_simple *buf, */ void *net_buf_simple_add(struct net_buf_simple *buf, size_t len); +/** + * @brief Copy bytes from memory to the end of the buffer + * + * Copies the given number of bytes to the end of the buffer. Increments the + * data length of the buffer to account for more data at the end. + * + * @param buf Buffer to update. + * @param mem Location of data to be added. + * @param len Length of data to be added + * + * @return The original tail of the buffer. + */ +void *net_buf_simple_add_mem(struct net_buf_simple *buf, const void *mem, + size_t len); + /** * @brief Add (8-bit) byte at the end of the buffer * @@ -645,6 +660,22 @@ static inline void *net_buf_user_data(struct net_buf *buf) */ #define net_buf_add(buf, len) net_buf_simple_add(&(buf)->b, len) +/** + * @def net_buf_add_mem + * @brief Copy bytes from memory to the end of the buffer + * + * Copies the given number of bytes to the end of the buffer. Increments the + * data length of the buffer to account for more data at the end. + * + * @param buf Buffer to update. + * @param mem Location of data to be added. + * @param len Length of data to be added + * + * @return The original tail of the buffer. + */ +#define net_buf_add_mem(buf, mem, len) net_buf_simple_add_mem(&(buf)->b, \ + mem, len) + /** * @def net_buf_add_u8 * @brief Add (8-bit) byte at the end of the buffer diff --git a/subsys/net/buf.c b/subsys/net/buf.c index e0e9feb85c3..9828aa24e1d 100644 --- a/subsys/net/buf.c +++ b/subsys/net/buf.c @@ -350,6 +350,14 @@ void *net_buf_simple_add(struct net_buf_simple *buf, size_t len) return tail; } +void *net_buf_simple_add_mem(struct net_buf_simple *buf, const void *mem, + size_t len) +{ + NET_BUF_SIMPLE_DBG("buf %p len %zu", buf, len); + + return memcpy(net_buf_simple_add(buf, len), mem, len); +} + uint8_t *net_buf_simple_add_u8(struct net_buf_simple *buf, uint8_t val) { uint8_t *u8;