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 <johan.hedberg@intel.com>
This commit is contained in:
parent
b008914e42
commit
db8a5d9b80
2 changed files with 39 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue