From db8a5d9b805251da73918c7daeddcd42aab59515 Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Thu, 22 Dec 2016 22:42:47 +0200 Subject: [PATCH] net: buf: Add net_buf_add_mem() API A very common pattern in code goes something like the following: memcpy(net_buf_add(buf, len), data, len); To avoid having to create this kind of complex constructions every time, this patch adds a new API which simplifies the call: net_buf_add_mem(buf, data, len); Change-Id: Ic1aeae4baf88b2295d139f672d5d265db2ddbe7b Signed-off-by: Johan Hedberg --- include/net/buf.h | 31 +++++++++++++++++++++++++++++++ subsys/net/buf.c | 8 ++++++++ 2 files changed, 39 insertions(+) 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;