LCOV - code coverage report
Current view: top level - zephyr - net_buf.h Coverage Total Hit
Test: new.info Lines: 100.0 % 195 195
Test Date: 2025-09-05 16:43:28

            Line data    Source code
       1            1 : /** @file
       2              :  *  @brief Buffer management.
       3              :  */
       4              : 
       5              : /*
       6              :  * Copyright (c) 2015 Intel Corporation
       7              :  *
       8              :  * SPDX-License-Identifier: Apache-2.0
       9              :  */
      10              : #ifndef ZEPHYR_INCLUDE_NET_BUF_H_
      11              : #define ZEPHYR_INCLUDE_NET_BUF_H_
      12              : 
      13              : #include <stddef.h>
      14              : #include <zephyr/types.h>
      15              : #include <zephyr/sys/util.h>
      16              : #include <zephyr/kernel.h>
      17              : #include <zephyr/sys/iterable_sections.h>
      18              : 
      19              : #ifdef __cplusplus
      20              : extern "C" {
      21              : #endif
      22              : 
      23              : /**
      24              :  * @brief Network buffer library
      25              :  * @defgroup net_buf Network Buffer Library
      26              :  * @since 1.0
      27              :  * @version 1.0.0
      28              :  * @ingroup os_services
      29              :  * @{
      30              :  */
      31              : 
      32              : /* Alignment needed for various parts of the buffer definition */
      33              : #if CONFIG_NET_BUF_ALIGNMENT == 0
      34              : #define __net_buf_align __aligned(sizeof(void *))
      35              : #else
      36              : #define __net_buf_align __aligned(CONFIG_NET_BUF_ALIGNMENT)
      37              : #endif
      38              : 
      39              : /**
      40              :  *  @brief Define a net_buf_simple stack variable.
      41              :  *
      42              :  *  This is a helper macro which is used to define a net_buf_simple object
      43              :  *  on the stack.
      44              :  *
      45              :  *  @param _name Name of the net_buf_simple object.
      46              :  *  @param _size Maximum data storage for the buffer.
      47              :  */
      48            1 : #define NET_BUF_SIMPLE_DEFINE(_name, _size)     \
      49              :         uint8_t net_buf_data_##_name[_size];       \
      50              :         struct net_buf_simple _name = {         \
      51              :                 .data   = net_buf_data_##_name, \
      52              :                 .len    = 0,                    \
      53              :                 .size   = _size,                \
      54              :                 .__buf  = net_buf_data_##_name, \
      55              :         }
      56              : 
      57              : /**
      58              :  *
      59              :  * @brief Define a static net_buf_simple variable.
      60              :  *
      61              :  * This is a helper macro which is used to define a static net_buf_simple
      62              :  * object.
      63              :  *
      64              :  * @param _name Name of the net_buf_simple object.
      65              :  * @param _size Maximum data storage for the buffer.
      66              :  */
      67            1 : #define NET_BUF_SIMPLE_DEFINE_STATIC(_name, _size)        \
      68              :         static __noinit uint8_t net_buf_data_##_name[_size]; \
      69              :         static struct net_buf_simple _name = {            \
      70              :                 .data   = net_buf_data_##_name,           \
      71              :                 .len    = 0,                              \
      72              :                 .size   = _size,                          \
      73              :                 .__buf  = net_buf_data_##_name,           \
      74              :         }
      75              : 
      76              : /**
      77              :  * @brief Simple network buffer representation.
      78              :  *
      79              :  * This is a simpler variant of the net_buf object (in fact net_buf uses
      80              :  * net_buf_simple internally). It doesn't provide any kind of reference
      81              :  * counting, user data, dynamic allocation, or in general the ability to
      82              :  * pass through kernel objects such as FIFOs.
      83              :  *
      84              :  * The main use of this is for scenarios where the meta-data of the normal
      85              :  * net_buf isn't needed and causes too much overhead. This could be e.g.
      86              :  * when the buffer only needs to be allocated on the stack or when the
      87              :  * access to and lifetime of the buffer is well controlled and constrained.
      88              :  */
      89            1 : struct net_buf_simple {
      90              :         /** Pointer to the start of data in the buffer. */
      91            1 :         uint8_t *data;
      92              : 
      93              :         /**
      94              :          * Length of the data behind the data pointer.
      95              :          *
      96              :          * To determine the max length, use net_buf_simple_max_len(), not #size!
      97              :          */
      98            1 :         uint16_t len;
      99              : 
     100              :         /** Amount of data that net_buf_simple#__buf can store. */
     101            1 :         uint16_t size;
     102              : 
     103              :         /** Start of the data storage. Not to be accessed directly
     104              :          *  (the data pointer should be used instead).
     105              :          */
     106              :         uint8_t *__buf;
     107              : };
     108              : 
     109              : /**
     110              :  *
     111              :  * @brief Define a net_buf_simple stack variable and get a pointer to it.
     112              :  *
     113              :  * This is a helper macro which is used to define a net_buf_simple object on
     114              :  * the stack and the get a pointer to it as follows:
     115              :  *
     116              :  * struct net_buf_simple *my_buf = NET_BUF_SIMPLE(10);
     117              :  *
     118              :  * After creating the object it needs to be initialized by calling
     119              :  * net_buf_simple_init().
     120              :  *
     121              :  * @param _size Maximum data storage for the buffer.
     122              :  *
     123              :  * @return Pointer to stack-allocated net_buf_simple object.
     124              :  */
     125            1 : #define NET_BUF_SIMPLE(_size)                        \
     126              :         ((struct net_buf_simple *)(&(struct {        \
     127              :                 struct net_buf_simple buf;           \
     128              :                 uint8_t data[_size];                 \
     129              :         }) {                                         \
     130              :                 .buf.size = _size,                   \
     131              :         }))
     132              : 
     133              : /**
     134              :  * @brief Initialize a net_buf_simple object.
     135              :  *
     136              :  * This needs to be called after creating a net_buf_simple object using
     137              :  * the NET_BUF_SIMPLE macro.
     138              :  *
     139              :  * @param buf Buffer to initialize.
     140              :  * @param reserve_head Headroom to reserve.
     141              :  */
     142            1 : static inline void net_buf_simple_init(struct net_buf_simple *buf,
     143              :                                        size_t reserve_head)
     144              : {
     145              :         if (!buf->__buf) {
     146              :                 buf->__buf = (uint8_t *)buf + sizeof(*buf);
     147              :         }
     148              : 
     149              :         buf->data = buf->__buf + reserve_head;
     150              :         buf->len = 0U;
     151              : }
     152              : 
     153              : /**
     154              :  * @brief Initialize a net_buf_simple object with data.
     155              :  *
     156              :  * Initialized buffer object with external data.
     157              :  *
     158              :  * @param buf Buffer to initialize.
     159              :  * @param data External data pointer
     160              :  * @param size Amount of data the pointed data buffer if able to fit.
     161              :  */
     162            1 : void net_buf_simple_init_with_data(struct net_buf_simple *buf,
     163              :                                    void *data, size_t size);
     164              : 
     165              : /**
     166              :  * @brief Reset buffer
     167              :  *
     168              :  * Reset buffer data so it can be reused for other purposes.
     169              :  *
     170              :  * @param buf Buffer to reset.
     171              :  */
     172            1 : static inline void net_buf_simple_reset(struct net_buf_simple *buf)
     173              : {
     174              :         buf->len  = 0U;
     175              :         buf->data = buf->__buf;
     176              : }
     177              : 
     178              : /**
     179              :  * Clone buffer state, using the same data buffer.
     180              :  *
     181              :  * Initializes a buffer to point to the same data as an existing buffer.
     182              :  * Allows operations on the same data without altering the length and
     183              :  * offset of the original.
     184              :  *
     185              :  * @param original Buffer to clone.
     186              :  * @param clone The new clone.
     187              :  */
     188            1 : void net_buf_simple_clone(const struct net_buf_simple *original,
     189              :                           struct net_buf_simple *clone);
     190              : 
     191              : /**
     192              :  * @brief Prepare data to be added at the end of the buffer
     193              :  *
     194              :  * Increments the data length of a buffer to account for more data
     195              :  * at the end.
     196              :  *
     197              :  * @param buf Buffer to update.
     198              :  * @param len Number of bytes to increment the length with.
     199              :  *
     200              :  * @return The original tail of the buffer.
     201              :  */
     202            1 : void *net_buf_simple_add(struct net_buf_simple *buf, size_t len);
     203              : 
     204              : /**
     205              :  * @brief Copy given number of bytes from memory to the end of the buffer
     206              :  *
     207              :  * Increments the data length of the  buffer to account for more data at the
     208              :  * end.
     209              :  *
     210              :  * @param buf Buffer to update.
     211              :  * @param mem Location of data to be added.
     212              :  * @param len Length of data to be added
     213              :  *
     214              :  * @return The original tail of the buffer.
     215              :  */
     216            1 : void *net_buf_simple_add_mem(struct net_buf_simple *buf, const void *mem,
     217              :                              size_t len);
     218              : 
     219              : /**
     220              :  * @brief Add (8-bit) byte at the end of the buffer
     221              :  *
     222              :  * Increments the data length of the  buffer to account for more data at the
     223              :  * end.
     224              :  *
     225              :  * @param buf Buffer to update.
     226              :  * @param val byte value to be added.
     227              :  *
     228              :  * @return Pointer to the value added
     229              :  */
     230            1 : uint8_t *net_buf_simple_add_u8(struct net_buf_simple *buf, uint8_t val);
     231              : 
     232              : /**
     233              :  * @brief Add 16-bit value at the end of the buffer
     234              :  *
     235              :  * Adds 16-bit value in little endian format at the end of buffer.
     236              :  * Increments the data length of a buffer to account for more data
     237              :  * at the end.
     238              :  *
     239              :  * @param buf Buffer to update.
     240              :  * @param val 16-bit value to be added.
     241              :  */
     242            1 : void net_buf_simple_add_le16(struct net_buf_simple *buf, uint16_t val);
     243              : 
     244              : /**
     245              :  * @brief Add 16-bit value at the end of the buffer
     246              :  *
     247              :  * Adds 16-bit value in big endian format at the end of buffer.
     248              :  * Increments the data length of a buffer to account for more data
     249              :  * at the end.
     250              :  *
     251              :  * @param buf Buffer to update.
     252              :  * @param val 16-bit value to be added.
     253              :  */
     254            1 : void net_buf_simple_add_be16(struct net_buf_simple *buf, uint16_t val);
     255              : 
     256              : /**
     257              :  * @brief Add 24-bit value at the end of the buffer
     258              :  *
     259              :  * Adds 24-bit value in little endian format at the end of buffer.
     260              :  * Increments the data length of a buffer to account for more data
     261              :  * at the end.
     262              :  *
     263              :  * @param buf Buffer to update.
     264              :  * @param val 24-bit value to be added.
     265              :  */
     266            1 : void net_buf_simple_add_le24(struct net_buf_simple *buf, uint32_t val);
     267              : 
     268              : /**
     269              :  * @brief Add 24-bit value at the end of the buffer
     270              :  *
     271              :  * Adds 24-bit value in big endian format at the end of buffer.
     272              :  * Increments the data length of a buffer to account for more data
     273              :  * at the end.
     274              :  *
     275              :  * @param buf Buffer to update.
     276              :  * @param val 24-bit value to be added.
     277              :  */
     278            1 : void net_buf_simple_add_be24(struct net_buf_simple *buf, uint32_t val);
     279              : 
     280              : /**
     281              :  * @brief Add 32-bit value at the end of the buffer
     282              :  *
     283              :  * Adds 32-bit value in little endian format at the end of buffer.
     284              :  * Increments the data length of a buffer to account for more data
     285              :  * at the end.
     286              :  *
     287              :  * @param buf Buffer to update.
     288              :  * @param val 32-bit value to be added.
     289              :  */
     290            1 : void net_buf_simple_add_le32(struct net_buf_simple *buf, uint32_t val);
     291              : 
     292              : /**
     293              :  * @brief Add 32-bit value at the end of the buffer
     294              :  *
     295              :  * Adds 32-bit value in big endian format at the end of buffer.
     296              :  * Increments the data length of a buffer to account for more data
     297              :  * at the end.
     298              :  *
     299              :  * @param buf Buffer to update.
     300              :  * @param val 32-bit value to be added.
     301              :  */
     302            1 : void net_buf_simple_add_be32(struct net_buf_simple *buf, uint32_t val);
     303              : 
     304              : /**
     305              :  * @brief Add 40-bit value at the end of the buffer
     306              :  *
     307              :  * Adds 40-bit value in little endian format at the end of buffer.
     308              :  * Increments the data length of a buffer to account for more data
     309              :  * at the end.
     310              :  *
     311              :  * @param buf Buffer to update.
     312              :  * @param val 40-bit value to be added.
     313              :  */
     314            1 : void net_buf_simple_add_le40(struct net_buf_simple *buf, uint64_t val);
     315              : 
     316              : /**
     317              :  * @brief Add 40-bit value at the end of the buffer
     318              :  *
     319              :  * Adds 40-bit value in big endian format at the end of buffer.
     320              :  * Increments the data length of a buffer to account for more data
     321              :  * at the end.
     322              :  *
     323              :  * @param buf Buffer to update.
     324              :  * @param val 40-bit value to be added.
     325              :  */
     326            1 : void net_buf_simple_add_be40(struct net_buf_simple *buf, uint64_t val);
     327              : 
     328              : /**
     329              :  * @brief Add 48-bit value at the end of the buffer
     330              :  *
     331              :  * Adds 48-bit value in little endian format at the end of buffer.
     332              :  * Increments the data length of a buffer to account for more data
     333              :  * at the end.
     334              :  *
     335              :  * @param buf Buffer to update.
     336              :  * @param val 48-bit value to be added.
     337              :  */
     338            1 : void net_buf_simple_add_le48(struct net_buf_simple *buf, uint64_t val);
     339              : 
     340              : /**
     341              :  * @brief Add 48-bit value at the end of the buffer
     342              :  *
     343              :  * Adds 48-bit value in big endian format at the end of buffer.
     344              :  * Increments the data length of a buffer to account for more data
     345              :  * at the end.
     346              :  *
     347              :  * @param buf Buffer to update.
     348              :  * @param val 48-bit value to be added.
     349              :  */
     350            1 : void net_buf_simple_add_be48(struct net_buf_simple *buf, uint64_t val);
     351              : 
     352              : /**
     353              :  * @brief Add 64-bit value at the end of the buffer
     354              :  *
     355              :  * Adds 64-bit value in little endian format at the end of buffer.
     356              :  * Increments the data length of a buffer to account for more data
     357              :  * at the end.
     358              :  *
     359              :  * @param buf Buffer to update.
     360              :  * @param val 64-bit value to be added.
     361              :  */
     362            1 : void net_buf_simple_add_le64(struct net_buf_simple *buf, uint64_t val);
     363              : 
     364              : /**
     365              :  * @brief Add 64-bit value at the end of the buffer
     366              :  *
     367              :  * Adds 64-bit value in big endian format at the end of buffer.
     368              :  * Increments the data length of a buffer to account for more data
     369              :  * at the end.
     370              :  *
     371              :  * @param buf Buffer to update.
     372              :  * @param val 64-bit value to be added.
     373              :  */
     374            1 : void net_buf_simple_add_be64(struct net_buf_simple *buf, uint64_t val);
     375              : 
     376              : /**
     377              :  * @brief Remove data from the end of the buffer.
     378              :  *
     379              :  * Removes data from the end of the buffer by modifying the buffer length.
     380              :  *
     381              :  * @param buf Buffer to update.
     382              :  * @param len Number of bytes to remove.
     383              :  *
     384              :  * @return New end of the buffer data.
     385              :  */
     386            1 : void *net_buf_simple_remove_mem(struct net_buf_simple *buf, size_t len);
     387              : 
     388              : /**
     389              :  * @brief Remove a 8-bit value from the end of the buffer
     390              :  *
     391              :  * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
     392              :  * on 8-bit values.
     393              :  *
     394              :  * @param buf A valid pointer on a buffer.
     395              :  *
     396              :  * @return The 8-bit removed value
     397              :  */
     398            1 : uint8_t net_buf_simple_remove_u8(struct net_buf_simple *buf);
     399              : 
     400              : /**
     401              :  * @brief Remove and convert 16 bits from the end of the buffer.
     402              :  *
     403              :  * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
     404              :  * on 16-bit little endian data.
     405              :  *
     406              :  * @param buf A valid pointer on a buffer.
     407              :  *
     408              :  * @return 16-bit value converted from little endian to host endian.
     409              :  */
     410            1 : uint16_t net_buf_simple_remove_le16(struct net_buf_simple *buf);
     411              : 
     412              : /**
     413              :  * @brief Remove and convert 16 bits from the end of the buffer.
     414              :  *
     415              :  * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
     416              :  * on 16-bit big endian data.
     417              :  *
     418              :  * @param buf A valid pointer on a buffer.
     419              :  *
     420              :  * @return 16-bit value converted from big endian to host endian.
     421              :  */
     422            1 : uint16_t net_buf_simple_remove_be16(struct net_buf_simple *buf);
     423              : 
     424              : /**
     425              :  * @brief Remove and convert 24 bits from the end of the buffer.
     426              :  *
     427              :  * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
     428              :  * on 24-bit little endian data.
     429              :  *
     430              :  * @param buf A valid pointer on a buffer.
     431              :  *
     432              :  * @return 24-bit value converted from little endian to host endian.
     433              :  */
     434            1 : uint32_t net_buf_simple_remove_le24(struct net_buf_simple *buf);
     435              : 
     436              : /**
     437              :  * @brief Remove and convert 24 bits from the end of the buffer.
     438              :  *
     439              :  * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
     440              :  * on 24-bit big endian data.
     441              :  *
     442              :  * @param buf A valid pointer on a buffer.
     443              :  *
     444              :  * @return 24-bit value converted from big endian to host endian.
     445              :  */
     446            1 : uint32_t net_buf_simple_remove_be24(struct net_buf_simple *buf);
     447              : 
     448              : /**
     449              :  * @brief Remove and convert 32 bits from the end of the buffer.
     450              :  *
     451              :  * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
     452              :  * on 32-bit little endian data.
     453              :  *
     454              :  * @param buf A valid pointer on a buffer.
     455              :  *
     456              :  * @return 32-bit value converted from little endian to host endian.
     457              :  */
     458            1 : uint32_t net_buf_simple_remove_le32(struct net_buf_simple *buf);
     459              : 
     460              : /**
     461              :  * @brief Remove and convert 32 bits from the end of the buffer.
     462              :  *
     463              :  * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
     464              :  * on 32-bit big endian data.
     465              :  *
     466              :  * @param buf A valid pointer on a buffer.
     467              :  *
     468              :  * @return 32-bit value converted from big endian to host endian.
     469              :  */
     470            1 : uint32_t net_buf_simple_remove_be32(struct net_buf_simple *buf);
     471              : 
     472              : /**
     473              :  * @brief Remove and convert 40 bits from the end of the buffer.
     474              :  *
     475              :  * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
     476              :  * on 40-bit little endian data.
     477              :  *
     478              :  * @param buf A valid pointer on a buffer.
     479              :  *
     480              :  * @return 40-bit value converted from little endian to host endian.
     481              :  */
     482            1 : uint64_t net_buf_simple_remove_le40(struct net_buf_simple *buf);
     483              : 
     484              : /**
     485              :  * @brief Remove and convert 40 bits from the end of the buffer.
     486              :  *
     487              :  * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
     488              :  * on 40-bit big endian data.
     489              :  *
     490              :  * @param buf A valid pointer on a buffer.
     491              :  *
     492              :  * @return 40-bit value converted from big endian to host endian.
     493              :  */
     494            1 : uint64_t net_buf_simple_remove_be40(struct net_buf_simple *buf);
     495              : 
     496              : /**
     497              :  * @brief Remove and convert 48 bits from the end of the buffer.
     498              :  *
     499              :  * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
     500              :  * on 48-bit little endian data.
     501              :  *
     502              :  * @param buf A valid pointer on a buffer.
     503              :  *
     504              :  * @return 48-bit value converted from little endian to host endian.
     505              :  */
     506            1 : uint64_t net_buf_simple_remove_le48(struct net_buf_simple *buf);
     507              : 
     508              : /**
     509              :  * @brief Remove and convert 48 bits from the end of the buffer.
     510              :  *
     511              :  * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
     512              :  * on 48-bit big endian data.
     513              :  *
     514              :  * @param buf A valid pointer on a buffer.
     515              :  *
     516              :  * @return 48-bit value converted from big endian to host endian.
     517              :  */
     518            1 : uint64_t net_buf_simple_remove_be48(struct net_buf_simple *buf);
     519              : 
     520              : /**
     521              :  * @brief Remove and convert 64 bits from the end of the buffer.
     522              :  *
     523              :  * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
     524              :  * on 64-bit little endian data.
     525              :  *
     526              :  * @param buf A valid pointer on a buffer.
     527              :  *
     528              :  * @return 64-bit value converted from little endian to host endian.
     529              :  */
     530            1 : uint64_t net_buf_simple_remove_le64(struct net_buf_simple *buf);
     531              : 
     532              : /**
     533              :  * @brief Remove and convert 64 bits from the end of the buffer.
     534              :  *
     535              :  * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
     536              :  * on 64-bit big endian data.
     537              :  *
     538              :  * @param buf A valid pointer on a buffer.
     539              :  *
     540              :  * @return 64-bit value converted from big endian to host endian.
     541              :  */
     542            1 : uint64_t net_buf_simple_remove_be64(struct net_buf_simple *buf);
     543              : 
     544              : /**
     545              :  * @brief Prepare data to be added to the start of the buffer
     546              :  *
     547              :  * Modifies the data pointer and buffer length to account for more data
     548              :  * in the beginning of the buffer.
     549              :  *
     550              :  * @param buf Buffer to update.
     551              :  * @param len Number of bytes to add to the beginning.
     552              :  *
     553              :  * @return The new beginning of the buffer data.
     554              :  */
     555            1 : void *net_buf_simple_push(struct net_buf_simple *buf, size_t len);
     556              : 
     557              : /**
     558              :  * @brief Copy given number of bytes from memory to the start of the buffer.
     559              :  *
     560              :  * Modifies the data pointer and buffer length to account for more data
     561              :  * in the beginning of the buffer.
     562              :  *
     563              :  * @param buf Buffer to update.
     564              :  * @param mem Location of data to be added.
     565              :  * @param len Length of data to be added.
     566              :  *
     567              :  * @return The new beginning of the buffer data.
     568              :  */
     569            1 : void *net_buf_simple_push_mem(struct net_buf_simple *buf, const void *mem,
     570              :                               size_t len);
     571              : 
     572              : /**
     573              :  * @brief Push 16-bit value to the beginning of the buffer
     574              :  *
     575              :  * Adds 16-bit value in little endian format to the beginning of the
     576              :  * buffer.
     577              :  *
     578              :  * @param buf Buffer to update.
     579              :  * @param val 16-bit value to be pushed to the buffer.
     580              :  */
     581            1 : void net_buf_simple_push_le16(struct net_buf_simple *buf, uint16_t val);
     582              : 
     583              : /**
     584              :  * @brief Push 16-bit value to the beginning of the buffer
     585              :  *
     586              :  * Adds 16-bit value in big endian format to the beginning of the
     587              :  * buffer.
     588              :  *
     589              :  * @param buf Buffer to update.
     590              :  * @param val 16-bit value to be pushed to the buffer.
     591              :  */
     592            1 : void net_buf_simple_push_be16(struct net_buf_simple *buf, uint16_t val);
     593              : 
     594              : /**
     595              :  * @brief Push 8-bit value to the beginning of the buffer
     596              :  *
     597              :  * Adds 8-bit value the beginning of the buffer.
     598              :  *
     599              :  * @param buf Buffer to update.
     600              :  * @param val 8-bit value to be pushed to the buffer.
     601              :  */
     602            1 : void net_buf_simple_push_u8(struct net_buf_simple *buf, uint8_t val);
     603              : 
     604              : /**
     605              :  * @brief Push 24-bit value to the beginning of the buffer
     606              :  *
     607              :  * Adds 24-bit value in little endian format to the beginning of the
     608              :  * buffer.
     609              :  *
     610              :  * @param buf Buffer to update.
     611              :  * @param val 24-bit value to be pushed to the buffer.
     612              :  */
     613            1 : void net_buf_simple_push_le24(struct net_buf_simple *buf, uint32_t val);
     614              : 
     615              : /**
     616              :  * @brief Push 24-bit value to the beginning of the buffer
     617              :  *
     618              :  * Adds 24-bit value in big endian format to the beginning of the
     619              :  * buffer.
     620              :  *
     621              :  * @param buf Buffer to update.
     622              :  * @param val 24-bit value to be pushed to the buffer.
     623              :  */
     624            1 : void net_buf_simple_push_be24(struct net_buf_simple *buf, uint32_t val);
     625              : 
     626              : /**
     627              :  * @brief Push 32-bit value to the beginning of the buffer
     628              :  *
     629              :  * Adds 32-bit value in little endian format to the beginning of the
     630              :  * buffer.
     631              :  *
     632              :  * @param buf Buffer to update.
     633              :  * @param val 32-bit value to be pushed to the buffer.
     634              :  */
     635            1 : void net_buf_simple_push_le32(struct net_buf_simple *buf, uint32_t val);
     636              : 
     637              : /**
     638              :  * @brief Push 32-bit value to the beginning of the buffer
     639              :  *
     640              :  * Adds 32-bit value in big endian format to the beginning of the
     641              :  * buffer.
     642              :  *
     643              :  * @param buf Buffer to update.
     644              :  * @param val 32-bit value to be pushed to the buffer.
     645              :  */
     646            1 : void net_buf_simple_push_be32(struct net_buf_simple *buf, uint32_t val);
     647              : 
     648              : /**
     649              :  * @brief Push 40-bit value to the beginning of the buffer
     650              :  *
     651              :  * Adds 40-bit value in little endian format to the beginning of the
     652              :  * buffer.
     653              :  *
     654              :  * @param buf Buffer to update.
     655              :  * @param val 40-bit value to be pushed to the buffer.
     656              :  */
     657            1 : void net_buf_simple_push_le40(struct net_buf_simple *buf, uint64_t val);
     658              : 
     659              : /**
     660              :  * @brief Push 40-bit value to the beginning of the buffer
     661              :  *
     662              :  * Adds 40-bit value in big endian format to the beginning of the
     663              :  * buffer.
     664              :  *
     665              :  * @param buf Buffer to update.
     666              :  * @param val 40-bit value to be pushed to the buffer.
     667              :  */
     668            1 : void net_buf_simple_push_be40(struct net_buf_simple *buf, uint64_t val);
     669              : 
     670              : /**
     671              :  * @brief Push 48-bit value to the beginning of the buffer
     672              :  *
     673              :  * Adds 48-bit value in little endian format to the beginning of the
     674              :  * buffer.
     675              :  *
     676              :  * @param buf Buffer to update.
     677              :  * @param val 48-bit value to be pushed to the buffer.
     678              :  */
     679            1 : void net_buf_simple_push_le48(struct net_buf_simple *buf, uint64_t val);
     680              : 
     681              : /**
     682              :  * @brief Push 48-bit value to the beginning of the buffer
     683              :  *
     684              :  * Adds 48-bit value in big endian format to the beginning of the
     685              :  * buffer.
     686              :  *
     687              :  * @param buf Buffer to update.
     688              :  * @param val 48-bit value to be pushed to the buffer.
     689              :  */
     690            1 : void net_buf_simple_push_be48(struct net_buf_simple *buf, uint64_t val);
     691              : 
     692              : /**
     693              :  * @brief Push 64-bit value to the beginning of the buffer
     694              :  *
     695              :  * Adds 64-bit value in little endian format to the beginning of the
     696              :  * buffer.
     697              :  *
     698              :  * @param buf Buffer to update.
     699              :  * @param val 64-bit value to be pushed to the buffer.
     700              :  */
     701            1 : void net_buf_simple_push_le64(struct net_buf_simple *buf, uint64_t val);
     702              : 
     703              : /**
     704              :  * @brief Push 64-bit value to the beginning of the buffer
     705              :  *
     706              :  * Adds 64-bit value in big endian format to the beginning of the
     707              :  * buffer.
     708              :  *
     709              :  * @param buf Buffer to update.
     710              :  * @param val 64-bit value to be pushed to the buffer.
     711              :  */
     712            1 : void net_buf_simple_push_be64(struct net_buf_simple *buf, uint64_t val);
     713              : 
     714              : /**
     715              :  * @brief Remove data from the beginning of the buffer.
     716              :  *
     717              :  * Removes data from the beginning of the buffer by modifying the data
     718              :  * pointer and buffer length.
     719              :  *
     720              :  * @param buf Buffer to update.
     721              :  * @param len Number of bytes to remove.
     722              :  *
     723              :  * @return New beginning of the buffer data.
     724              :  */
     725            1 : void *net_buf_simple_pull(struct net_buf_simple *buf, size_t len);
     726              : 
     727              : /**
     728              :  * @brief Remove data from the beginning of the buffer.
     729              :  *
     730              :  * Removes data from the beginning of the buffer by modifying the data
     731              :  * pointer and buffer length.
     732              :  *
     733              :  * @param buf Buffer to update.
     734              :  * @param len Number of bytes to remove.
     735              :  *
     736              :  * @return Pointer to the old location of the buffer data.
     737              :  */
     738            1 : void *net_buf_simple_pull_mem(struct net_buf_simple *buf, size_t len);
     739              : 
     740              : /**
     741              :  * @brief Remove a 8-bit value from the beginning of the buffer
     742              :  *
     743              :  * Same idea as with net_buf_simple_pull(), but a helper for operating
     744              :  * on 8-bit values.
     745              :  *
     746              :  * @param buf A valid pointer on a buffer.
     747              :  *
     748              :  * @return The 8-bit removed value
     749              :  */
     750            1 : uint8_t net_buf_simple_pull_u8(struct net_buf_simple *buf);
     751              : 
     752              : /**
     753              :  * @brief Remove and convert 16 bits from the beginning of the buffer.
     754              :  *
     755              :  * Same idea as with net_buf_simple_pull(), but a helper for operating
     756              :  * on 16-bit little endian data.
     757              :  *
     758              :  * @param buf A valid pointer on a buffer.
     759              :  *
     760              :  * @return 16-bit value converted from little endian to host endian.
     761              :  */
     762            1 : uint16_t net_buf_simple_pull_le16(struct net_buf_simple *buf);
     763              : 
     764              : /**
     765              :  * @brief Remove and convert 16 bits from the beginning of the buffer.
     766              :  *
     767              :  * Same idea as with net_buf_simple_pull(), but a helper for operating
     768              :  * on 16-bit big endian data.
     769              :  *
     770              :  * @param buf A valid pointer on a buffer.
     771              :  *
     772              :  * @return 16-bit value converted from big endian to host endian.
     773              :  */
     774            1 : uint16_t net_buf_simple_pull_be16(struct net_buf_simple *buf);
     775              : 
     776              : /**
     777              :  * @brief Remove and convert 24 bits from the beginning of the buffer.
     778              :  *
     779              :  * Same idea as with net_buf_simple_pull(), but a helper for operating
     780              :  * on 24-bit little endian data.
     781              :  *
     782              :  * @param buf A valid pointer on a buffer.
     783              :  *
     784              :  * @return 24-bit value converted from little endian to host endian.
     785              :  */
     786            1 : uint32_t net_buf_simple_pull_le24(struct net_buf_simple *buf);
     787              : 
     788              : /**
     789              :  * @brief Remove and convert 24 bits from the beginning of the buffer.
     790              :  *
     791              :  * Same idea as with net_buf_simple_pull(), but a helper for operating
     792              :  * on 24-bit big endian data.
     793              :  *
     794              :  * @param buf A valid pointer on a buffer.
     795              :  *
     796              :  * @return 24-bit value converted from big endian to host endian.
     797              :  */
     798            1 : uint32_t net_buf_simple_pull_be24(struct net_buf_simple *buf);
     799              : 
     800              : /**
     801              :  * @brief Remove and convert 32 bits from the beginning of the buffer.
     802              :  *
     803              :  * Same idea as with net_buf_simple_pull(), but a helper for operating
     804              :  * on 32-bit little endian data.
     805              :  *
     806              :  * @param buf A valid pointer on a buffer.
     807              :  *
     808              :  * @return 32-bit value converted from little endian to host endian.
     809              :  */
     810            1 : uint32_t net_buf_simple_pull_le32(struct net_buf_simple *buf);
     811              : 
     812              : /**
     813              :  * @brief Remove and convert 32 bits from the beginning of the buffer.
     814              :  *
     815              :  * Same idea as with net_buf_simple_pull(), but a helper for operating
     816              :  * on 32-bit big endian data.
     817              :  *
     818              :  * @param buf A valid pointer on a buffer.
     819              :  *
     820              :  * @return 32-bit value converted from big endian to host endian.
     821              :  */
     822            1 : uint32_t net_buf_simple_pull_be32(struct net_buf_simple *buf);
     823              : 
     824              : /**
     825              :  * @brief Remove and convert 40 bits from the beginning of the buffer.
     826              :  *
     827              :  * Same idea as with net_buf_simple_pull(), but a helper for operating
     828              :  * on 40-bit little endian data.
     829              :  *
     830              :  * @param buf A valid pointer on a buffer.
     831              :  *
     832              :  * @return 40-bit value converted from little endian to host endian.
     833              :  */
     834            1 : uint64_t net_buf_simple_pull_le40(struct net_buf_simple *buf);
     835              : 
     836              : /**
     837              :  * @brief Remove and convert 40 bits from the beginning of the buffer.
     838              :  *
     839              :  * Same idea as with net_buf_simple_pull(), but a helper for operating
     840              :  * on 40-bit big endian data.
     841              :  *
     842              :  * @param buf A valid pointer on a buffer.
     843              :  *
     844              :  * @return 40-bit value converted from big endian to host endian.
     845              :  */
     846            1 : uint64_t net_buf_simple_pull_be40(struct net_buf_simple *buf);
     847              : 
     848              : /**
     849              :  * @brief Remove and convert 48 bits from the beginning of the buffer.
     850              :  *
     851              :  * Same idea as with net_buf_simple_pull(), but a helper for operating
     852              :  * on 48-bit little endian data.
     853              :  *
     854              :  * @param buf A valid pointer on a buffer.
     855              :  *
     856              :  * @return 48-bit value converted from little endian to host endian.
     857              :  */
     858            1 : uint64_t net_buf_simple_pull_le48(struct net_buf_simple *buf);
     859              : 
     860              : /**
     861              :  * @brief Remove and convert 48 bits from the beginning of the buffer.
     862              :  *
     863              :  * Same idea as with net_buf_simple_pull(), but a helper for operating
     864              :  * on 48-bit big endian data.
     865              :  *
     866              :  * @param buf A valid pointer on a buffer.
     867              :  *
     868              :  * @return 48-bit value converted from big endian to host endian.
     869              :  */
     870            1 : uint64_t net_buf_simple_pull_be48(struct net_buf_simple *buf);
     871              : 
     872              : /**
     873              :  * @brief Remove and convert 64 bits from the beginning of the buffer.
     874              :  *
     875              :  * Same idea as with net_buf_simple_pull(), but a helper for operating
     876              :  * on 64-bit little endian data.
     877              :  *
     878              :  * @param buf A valid pointer on a buffer.
     879              :  *
     880              :  * @return 64-bit value converted from little endian to host endian.
     881              :  */
     882            1 : uint64_t net_buf_simple_pull_le64(struct net_buf_simple *buf);
     883              : 
     884              : /**
     885              :  * @brief Remove and convert 64 bits from the beginning of the buffer.
     886              :  *
     887              :  * Same idea as with net_buf_simple_pull(), but a helper for operating
     888              :  * on 64-bit big endian data.
     889              :  *
     890              :  * @param buf A valid pointer on a buffer.
     891              :  *
     892              :  * @return 64-bit value converted from big endian to host endian.
     893              :  */
     894            1 : uint64_t net_buf_simple_pull_be64(struct net_buf_simple *buf);
     895              : 
     896              : /**
     897              :  * @brief Get the tail pointer for a buffer.
     898              :  *
     899              :  * Get a pointer to the end of the data in a buffer.
     900              :  *
     901              :  * @param buf Buffer.
     902              :  *
     903              :  * @return Tail pointer for the buffer.
     904              :  */
     905            1 : static inline uint8_t *net_buf_simple_tail(const struct net_buf_simple *buf)
     906              : {
     907              :         return buf->data + buf->len;
     908              : }
     909              : 
     910              : /**
     911              :  * @brief Check buffer headroom.
     912              :  *
     913              :  * Check how much free space there is in the beginning of the buffer.
     914              :  *
     915              :  * buf A valid pointer on a buffer
     916              :  *
     917              :  * @return Number of bytes available in the beginning of the buffer.
     918              :  */
     919            1 : size_t net_buf_simple_headroom(const struct net_buf_simple *buf);
     920              : 
     921              : /**
     922              :  * @brief Check buffer tailroom.
     923              :  *
     924              :  * Check how much free space there is at the end of the buffer.
     925              :  *
     926              :  * @param buf A valid pointer on a buffer
     927              :  *
     928              :  * @return Number of bytes available at the end of the buffer.
     929              :  */
     930            1 : size_t net_buf_simple_tailroom(const struct net_buf_simple *buf);
     931              : 
     932              : /**
     933              :  * @brief Check maximum net_buf_simple::len value.
     934              :  *
     935              :  * This value is depending on the number of bytes being reserved as headroom.
     936              :  *
     937              :  * @param buf A valid pointer on a buffer
     938              :  *
     939              :  * @return Number of bytes usable behind the net_buf_simple::data pointer.
     940              :  */
     941            1 : uint16_t net_buf_simple_max_len(const struct net_buf_simple *buf);
     942              : 
     943              : /**
     944              :  * @brief Parsing state of a buffer.
     945              :  *
     946              :  * This is used for temporarily storing the parsing state of a buffer
     947              :  * while giving control of the parsing to a routine which we don't
     948              :  * control.
     949              :  */
     950            1 : struct net_buf_simple_state {
     951              :         /** Offset of the data pointer from the beginning of the storage */
     952            1 :         uint16_t offset;
     953              :         /** Length of data */
     954            1 :         uint16_t len;
     955              : };
     956              : 
     957              : /**
     958              :  * @brief Save the parsing state of a buffer.
     959              :  *
     960              :  * Saves the parsing state of a buffer so it can be restored later.
     961              :  *
     962              :  * @param buf Buffer from which the state should be saved.
     963              :  * @param state Storage for the state.
     964              :  */
     965            1 : static inline void net_buf_simple_save(const struct net_buf_simple *buf,
     966              :                                        struct net_buf_simple_state *state)
     967              : {
     968              :         state->offset = (uint16_t)net_buf_simple_headroom(buf);
     969              :         state->len = buf->len;
     970              : }
     971              : 
     972              : /**
     973              :  * @brief Restore the parsing state of a buffer.
     974              :  *
     975              :  * Restores the parsing state of a buffer from a state previously stored
     976              :  * by net_buf_simple_save().
     977              :  *
     978              :  * @param buf Buffer to which the state should be restored.
     979              :  * @param state Stored state.
     980              :  */
     981            1 : static inline void net_buf_simple_restore(struct net_buf_simple *buf,
     982              :                                           struct net_buf_simple_state *state)
     983              : {
     984              :         buf->data = buf->__buf + state->offset;
     985              :         buf->len = state->len;
     986              : }
     987              : 
     988              : /**
     989              :  * Flag indicating that the buffer's associated data pointer, points to
     990              :  * externally allocated memory. Therefore once ref goes down to zero, the
     991              :  * pointed data will not need to be deallocated. This never needs to be
     992              :  * explicitly set or unset by the net_buf API user. Such net_buf is
     993              :  * exclusively instantiated via net_buf_alloc_with_data() function.
     994              :  * Reference count mechanism however will behave the same way, and ref
     995              :  * count going to 0 will free the net_buf but no the data pointer in it.
     996              :  */
     997            1 : #define NET_BUF_EXTERNAL_DATA  BIT(0)
     998              : 
     999              : /**
    1000              :  * @brief Network buffer representation.
    1001              :  *
    1002              :  * This struct is used to represent network buffers. Such buffers are
    1003              :  * normally defined through the NET_BUF_POOL_*_DEFINE() APIs and allocated
    1004              :  * using the net_buf_alloc() API.
    1005              :  */
    1006            1 : struct net_buf {
    1007              :         /** Allow placing the buffer into sys_slist_t */
    1008            1 :         sys_snode_t node;
    1009              : 
    1010              :         /** Fragments associated with this buffer. */
    1011            1 :         struct net_buf *frags;
    1012              : 
    1013              :         /** Reference count. */
    1014            1 :         uint8_t ref;
    1015              : 
    1016              :         /** Bit-field of buffer flags. */
    1017            1 :         uint8_t flags;
    1018              : 
    1019              :         /** Where the buffer should go when freed up. */
    1020            1 :         uint8_t pool_id;
    1021              : 
    1022              :         /** Size of user data on this buffer */
    1023            1 :         uint8_t user_data_size;
    1024              : 
    1025              :         /** Union for convenience access to the net_buf_simple members, also
    1026              :          * preserving the old API.
    1027              :          */
    1028              :         union {
    1029              :                 /* The ABI of this struct must match net_buf_simple */
    1030              :                 struct {
    1031              :                         /** Pointer to the start of data in the buffer. */
    1032            1 :                         uint8_t *data;
    1033              : 
    1034              :                         /** Length of the data behind the data pointer. */
    1035            1 :                         uint16_t len;
    1036              : 
    1037              :                         /** Amount of data that this buffer can store. */
    1038            1 :                         uint16_t size;
    1039              : 
    1040              :                         /** Start of the data storage. Not to be accessed
    1041              :                          *  directly (the data pointer should be used
    1042              :                          *  instead).
    1043              :                          */
    1044              :                         uint8_t *__buf;
    1045              :                 };
    1046              : 
    1047              :                 /** @cond INTERNAL_HIDDEN */
    1048              :                 struct net_buf_simple b;
    1049              :                 /** @endcond */
    1050            1 :         };
    1051              : 
    1052              :         /** System metadata for this buffer. Cleared on allocation. */
    1053            1 :         uint8_t user_data[] __net_buf_align;
    1054              : };
    1055              : 
    1056              : /** @cond INTERNAL_HIDDEN */
    1057              : 
    1058              : struct net_buf_data_cb {
    1059              :         uint8_t * __must_check (*alloc)(struct net_buf *buf, size_t *size,
    1060              :                            k_timeout_t timeout);
    1061              :         uint8_t * __must_check (*ref)(struct net_buf *buf, uint8_t *data);
    1062              :         void   (*unref)(struct net_buf *buf, uint8_t *data);
    1063              : };
    1064              : 
    1065              : struct net_buf_data_alloc {
    1066              :         const struct net_buf_data_cb *cb;
    1067              :         void *alloc_data;
    1068              :         size_t max_alloc_size;
    1069              :         size_t alignment;
    1070              : };
    1071              : 
    1072              : /** @endcond */
    1073              : 
    1074              : /**
    1075              :  * @brief Network buffer pool representation.
    1076              :  *
    1077              :  * This struct is used to represent a pool of network buffers.
    1078              :  */
    1079            1 : struct net_buf_pool {
    1080              :         /** LIFO to place the buffer into when free */
    1081            1 :         struct k_lifo free;
    1082              : 
    1083              :         /** To prevent concurrent access/modifications */
    1084            1 :         struct k_spinlock lock;
    1085              : 
    1086              :         /** Number of buffers in pool */
    1087            1 :         const uint16_t buf_count;
    1088              : 
    1089              :         /** Number of uninitialized buffers */
    1090            1 :         uint16_t uninit_count;
    1091              : 
    1092              :         /** Size of user data allocated to this pool */
    1093            1 :         uint8_t user_data_size;
    1094              : 
    1095              : #if defined(CONFIG_NET_BUF_POOL_USAGE)
    1096              :         /** Amount of available buffers in the pool. */
    1097              :         atomic_t avail_count;
    1098              : 
    1099              :         /** Total size of the pool. */
    1100              :         const uint16_t pool_size;
    1101              : 
    1102              :         /** Maximum count of used buffers. */
    1103              :         uint16_t max_used;
    1104              : 
    1105              :         /** Name of the pool. Used when printing pool information. */
    1106              :         const char *name;
    1107              : #endif /* CONFIG_NET_BUF_POOL_USAGE */
    1108              : 
    1109              :         /** Optional destroy callback when buffer is freed. */
    1110            1 :         void (*const destroy)(struct net_buf *buf);
    1111              : 
    1112              :         /** Data allocation handlers. */
    1113            1 :         const struct net_buf_data_alloc *alloc;
    1114              : 
    1115              :         /** Start of buffer storage array */
    1116              :         struct net_buf * const __bufs;
    1117              : };
    1118              : 
    1119              : /** @cond INTERNAL_HIDDEN */
    1120              : #define NET_BUF_POOL_USAGE_INIT(_pool, _count) \
    1121              :         IF_ENABLED(CONFIG_NET_BUF_POOL_USAGE, (.avail_count = ATOMIC_INIT(_count),)) \
    1122              :         IF_ENABLED(CONFIG_NET_BUF_POOL_USAGE, (.max_used = 0,)) \
    1123              :         IF_ENABLED(CONFIG_NET_BUF_POOL_USAGE, (.name = STRINGIFY(_pool),))
    1124              : 
    1125              : #define NET_BUF_POOL_INITIALIZER(_pool, _alloc, _bufs, _count, _ud_size, _destroy) \
    1126              :         {                                                                          \
    1127              :                 .free = Z_LIFO_INITIALIZER(_pool.free),                            \
    1128              :                 .lock = { },                                                       \
    1129              :                 .buf_count = _count,                                               \
    1130              :                 .uninit_count = _count,                                            \
    1131              :                 .user_data_size = _ud_size,                                        \
    1132              :                 NET_BUF_POOL_USAGE_INIT(_pool, _count)                             \
    1133              :                 .destroy = _destroy,                                               \
    1134              :                 .alloc = _alloc,                                                   \
    1135              :                 .__bufs = (struct net_buf *)_bufs,                                 \
    1136              :         }
    1137              : 
    1138              : #define _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size)                                         \
    1139              :         struct _net_buf_##_name { uint8_t b[sizeof(struct net_buf)];                           \
    1140              :                                   uint8_t ud[_ud_size]; } __net_buf_align;                     \
    1141              :         BUILD_ASSERT(_ud_size <= UINT8_MAX);                                                \
    1142              :         BUILD_ASSERT(offsetof(struct net_buf, user_data) ==                                    \
    1143              :                      offsetof(struct _net_buf_##_name, ud), "Invalid offset");                       \
    1144              :         BUILD_ASSERT(__alignof__(struct net_buf) ==                                            \
    1145              :                      __alignof__(struct _net_buf_##_name), "Invalid alignment");             \
    1146              :         BUILD_ASSERT(sizeof(struct _net_buf_##_name) ==                                        \
    1147              :                      ROUND_UP(sizeof(struct net_buf) + _ud_size, __alignof__(struct net_buf)), \
    1148              :                      "Size cannot be determined");                                           \
    1149              :         static struct _net_buf_##_name _net_buf_##_name[_count] __noinit
    1150              : 
    1151              : extern const struct net_buf_data_alloc net_buf_heap_alloc;
    1152              : /** @endcond */
    1153              : 
    1154              : /**
    1155              :  *
    1156              :  * @brief Define a new pool for buffers using the heap for the data.
    1157              :  *
    1158              :  * Defines a net_buf_pool struct and the necessary memory storage (array of
    1159              :  * structs) for the needed amount of buffers. After this, the buffers can be
    1160              :  * accessed from the pool through net_buf_alloc. The pool is defined as a
    1161              :  * static variable, so if it needs to be exported outside the current module
    1162              :  * this needs to happen with the help of a separate pointer rather than an
    1163              :  * extern declaration.
    1164              :  *
    1165              :  * The data payload of the buffers will be allocated from the heap using
    1166              :  * k_malloc, so CONFIG_HEAP_MEM_POOL_SIZE must be set to a positive value.
    1167              :  * This kind of pool does not support blocking on the data allocation, so
    1168              :  * the timeout passed to net_buf_alloc will be always treated as K_NO_WAIT
    1169              :  * when trying to allocate the data. This means that allocation failures,
    1170              :  * i.e. NULL returns, must always be handled cleanly.
    1171              :  *
    1172              :  * If provided with a custom destroy callback, this callback is
    1173              :  * responsible for eventually calling net_buf_destroy() to complete the
    1174              :  * process of returning the buffer to the pool.
    1175              :  *
    1176              :  * @param _name      Name of the pool variable.
    1177              :  * @param _count     Number of buffers in the pool.
    1178              :  * @param _ud_size   User data space to reserve per buffer.
    1179              :  * @param _destroy   Optional destroy callback when buffer is freed.
    1180              :  */
    1181            1 : #define NET_BUF_POOL_HEAP_DEFINE(_name, _count, _ud_size, _destroy)          \
    1182              :         _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size);                      \
    1183              :         static STRUCT_SECTION_ITERABLE(net_buf_pool, _name) =                \
    1184              :                 NET_BUF_POOL_INITIALIZER(_name, &net_buf_heap_alloc,         \
    1185              :                                          _net_buf_##_name, _count, _ud_size, \
    1186              :                                          _destroy)
    1187              : 
    1188              : /** @cond INTERNAL_HIDDEN */
    1189              : 
    1190              : struct net_buf_pool_fixed {
    1191              :         uint8_t *data_pool;
    1192              : };
    1193              : 
    1194              : extern const struct net_buf_data_cb net_buf_fixed_cb;
    1195              : 
    1196              : /** @endcond */
    1197              : 
    1198              : /**
    1199              :  *
    1200              :  * @brief Define a new pool for buffers based on fixed-size data
    1201              :  *
    1202              :  * Defines a net_buf_pool struct and the necessary memory storage (array of
    1203              :  * structs) for the needed amount of buffers. After this, the buffers can be
    1204              :  * accessed from the pool through net_buf_alloc. The pool is defined as a
    1205              :  * static variable, so if it needs to be exported outside the current module
    1206              :  * this needs to happen with the help of a separate pointer rather than an
    1207              :  * extern declaration.
    1208              :  *
    1209              :  * The data payload of the buffers will be allocated from a byte array
    1210              :  * of fixed sized chunks. This kind of pool does not support blocking on
    1211              :  * the data allocation, so the timeout passed to net_buf_alloc will be
    1212              :  * always treated as K_NO_WAIT when trying to allocate the data. This means
    1213              :  * that allocation failures, i.e. NULL returns, must always be handled
    1214              :  * cleanly.
    1215              :  *
    1216              :  * If provided with a custom destroy callback, this callback is
    1217              :  * responsible for eventually calling net_buf_destroy() to complete the
    1218              :  * process of returning the buffer to the pool.
    1219              :  *
    1220              :  * @param _name      Name of the pool variable.
    1221              :  * @param _count     Number of buffers in the pool.
    1222              :  * @param _data_size Maximum data payload per buffer.
    1223              :  * @param _ud_size   User data space to reserve per buffer.
    1224              :  * @param _destroy   Optional destroy callback when buffer is freed.
    1225              :  */
    1226            1 : #define NET_BUF_POOL_FIXED_DEFINE(_name, _count, _data_size, _ud_size, _destroy) \
    1227              :         _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size);                        \
    1228              :         static uint8_t __noinit net_buf_data_##_name[_count][_data_size] __net_buf_align; \
    1229              :         static const struct net_buf_pool_fixed net_buf_fixed_##_name = {       \
    1230              :                 .data_pool = (uint8_t *)net_buf_data_##_name,                  \
    1231              :         };                                                                     \
    1232              :         static const struct net_buf_data_alloc net_buf_fixed_alloc_##_name = { \
    1233              :                 .cb = &net_buf_fixed_cb,                                       \
    1234              :                 .alloc_data = (void *)&net_buf_fixed_##_name,                  \
    1235              :                 .max_alloc_size = _data_size,                                  \
    1236              :         };                                                                     \
    1237              :         static STRUCT_SECTION_ITERABLE(net_buf_pool, _name) =                  \
    1238              :                 NET_BUF_POOL_INITIALIZER(_name, &net_buf_fixed_alloc_##_name,  \
    1239              :                                          _net_buf_##_name, _count, _ud_size,   \
    1240              :                                          _destroy)
    1241              : 
    1242              : /** @cond INTERNAL_HIDDEN */
    1243              : extern const struct net_buf_data_cb net_buf_var_cb;
    1244              : /** @endcond */
    1245              : 
    1246              : /**
    1247              :  *
    1248              :  * @brief Define a new pool for buffers with variable size payloads
    1249              :  *
    1250              :  * Defines a net_buf_pool struct and the necessary memory storage (array of
    1251              :  * structs) for the needed amount of buffers. After this, the buffers can be
    1252              :  * accessed from the pool through net_buf_alloc. The pool is defined as a
    1253              :  * static variable, so if it needs to be exported outside the current module
    1254              :  * this needs to happen with the help of a separate pointer rather than an
    1255              :  * extern declaration.
    1256              :  *
    1257              :  * The data payload of the buffers will be based on a memory pool from which
    1258              :  * variable size payloads may be allocated.
    1259              :  *
    1260              :  * If provided with a custom destroy callback, this callback is
    1261              :  * responsible for eventually calling net_buf_destroy() to complete the
    1262              :  * process of returning the buffer to the pool.
    1263              :  *
    1264              :  * @param _name      Name of the pool variable.
    1265              :  * @param _count     Number of buffers in the pool.
    1266              :  * @param _data_size Total amount of memory available for data payloads.
    1267              :  * @param _ud_size   User data space to reserve per buffer.
    1268              :  * @param _destroy   Optional destroy callback when buffer is freed.
    1269              :  */
    1270            1 : #define NET_BUF_POOL_VAR_DEFINE(_name, _count, _data_size, _ud_size, _destroy) \
    1271              :         _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size);                        \
    1272              :         K_HEAP_DEFINE(net_buf_mem_pool_##_name, _data_size);                   \
    1273              :         static const struct net_buf_data_alloc net_buf_data_alloc_##_name = {  \
    1274              :                 .cb = &net_buf_var_cb,                                         \
    1275              :                 .alloc_data = &net_buf_mem_pool_##_name,                       \
    1276              :                 .max_alloc_size = 0,                                           \
    1277              :         };                                                                     \
    1278              :         static STRUCT_SECTION_ITERABLE(net_buf_pool, _name) =                  \
    1279              :                 NET_BUF_POOL_INITIALIZER(_name, &net_buf_data_alloc_##_name,   \
    1280              :                                          _net_buf_##_name, _count, _ud_size,   \
    1281              :                                          _destroy)
    1282              : 
    1283              : /**
    1284              :  *
    1285              :  * @brief Define a new pool for buffers with variable size payloads. Align the
    1286              :  *        length and start of the buffer to the specified alignment.
    1287              :  *
    1288              :  * Defines a net_buf_pool struct and the necessary memory storage (array of
    1289              :  * structs) for the needed amount of buffers. After this, the buffers can be
    1290              :  * accessed from the pool through net_buf_alloc. The pool is defined as a
    1291              :  * static variable, so if it needs to be exported outside the current module
    1292              :  * this needs to happen with the help of a separate pointer rather than an
    1293              :  * extern declaration.
    1294              :  *
    1295              :  * The data payload of the buffers will be based on a memory pool from which
    1296              :  * variable size payloads may be allocated.
    1297              :  *
    1298              :  * If provided with a custom destroy callback, this callback is
    1299              :  * responsible for eventually calling net_buf_destroy() to complete the
    1300              :  * process of returning the buffer to the pool.
    1301              :  *
    1302              :  * Both the length and start of the buffer will be aligned to the specified
    1303              :  * alignment. The alignment must be a power of 2 and the size of the
    1304              :  * alignment must be less than or equal to the size of the data payload.
    1305              :  *
    1306              :  * @param _name      Name of the pool variable.
    1307              :  * @param _count     Number of buffers in the pool.
    1308              :  * @param _data_size Total amount of memory available for data payloads.
    1309              :  * @param _ud_size   User data space to reserve per buffer.
    1310              :  * @param _destroy   Optional destroy callback when buffer is freed.
    1311              :  * @param _align     Alignment of the length and start of the buffer.
    1312              :  */
    1313              : #define NET_BUF_POOL_VAR_ALIGN_DEFINE(_name, _count, _data_size, _ud_size,     \
    1314            1 :                                       _destroy, _align)                        \
    1315              :         _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size);                        \
    1316              :         K_HEAP_DEFINE(net_buf_mem_pool_##_name, _data_size);                   \
    1317              :         static const struct net_buf_data_alloc net_buf_data_alloc_##_name = {  \
    1318              :                 .cb = &net_buf_var_cb,                                             \
    1319              :                 .alloc_data = &net_buf_mem_pool_##_name,                       \
    1320              :                 .max_alloc_size = 0,                                           \
    1321              :                 .alignment = _align,                                           \
    1322              :         };                                                                     \
    1323              :         static STRUCT_SECTION_ITERABLE(net_buf_pool, _name) =                  \
    1324              :                 NET_BUF_POOL_INITIALIZER(_name, &net_buf_data_alloc_##_name,   \
    1325              :                                          _net_buf_##_name, _count, _ud_size,   \
    1326              :                                          _destroy)
    1327              : 
    1328              : /**
    1329              :  *
    1330              :  * @brief Define a new pool for buffers
    1331              :  *
    1332              :  * Defines a net_buf_pool struct and the necessary memory storage (array of
    1333              :  * structs) for the needed amount of buffers. After this,the buffers can be
    1334              :  * accessed from the pool through net_buf_alloc. The pool is defined as a
    1335              :  * static variable, so if it needs to be exported outside the current module
    1336              :  * this needs to happen with the help of a separate pointer rather than an
    1337              :  * extern declaration.
    1338              :  *
    1339              :  * If provided with a custom destroy callback this callback is
    1340              :  * responsible for eventually calling net_buf_destroy() to complete the
    1341              :  * process of returning the buffer to the pool.
    1342              :  *
    1343              :  * @param _name     Name of the pool variable.
    1344              :  * @param _count    Number of buffers in the pool.
    1345              :  * @param _size     Maximum data size for each buffer.
    1346              :  * @param _ud_size  Amount of user data space to reserve.
    1347              :  * @param _destroy  Optional destroy callback when buffer is freed.
    1348              :  */
    1349            1 : #define NET_BUF_POOL_DEFINE(_name, _count, _size, _ud_size, _destroy)        \
    1350              :         NET_BUF_POOL_FIXED_DEFINE(_name, _count, _size, _ud_size, _destroy)
    1351              : 
    1352              : /**
    1353              :  * @brief Looks up a pool based on its ID.
    1354              :  *
    1355              :  * @param id Pool ID (e.g. from buf->pool_id).
    1356              :  *
    1357              :  * @return Pointer to pool.
    1358              :  */
    1359            1 : struct net_buf_pool *net_buf_pool_get(int id);
    1360              : 
    1361              : /**
    1362              :  * @brief Get a zero-based index for a buffer.
    1363              :  *
    1364              :  * This function will translate a buffer into a zero-based index,
    1365              :  * based on its placement in its buffer pool. This can be useful if you
    1366              :  * want to associate an external array of meta-data contexts with the
    1367              :  * buffers of a pool.
    1368              :  *
    1369              :  * @param buf  Network buffer.
    1370              :  *
    1371              :  * @return Zero-based index for the buffer.
    1372              :  */
    1373            1 : int net_buf_id(const struct net_buf *buf);
    1374              : 
    1375              : /**
    1376              :  * @brief Allocate a new fixed buffer from a pool.
    1377              :  *
    1378              :  * @param pool Which pool to allocate the buffer from.
    1379              :  * @param timeout Affects the action taken should the pool be empty.
    1380              :  *        If K_NO_WAIT, then return immediately. If K_FOREVER, then
    1381              :  *        wait as long as necessary. Otherwise, wait until the specified
    1382              :  *        timeout. Note that some types of data allocators do not support
    1383              :  *        blocking (such as the HEAP type). In this case it's still possible
    1384              :  *        for net_buf_alloc() to fail (return NULL) even if it was given
    1385              :  *        K_FOREVER.
    1386              :  *
    1387              :  * @return New buffer or NULL if out of buffers.
    1388              :  */
    1389              : #if defined(CONFIG_NET_BUF_LOG)
    1390              : struct net_buf * __must_check net_buf_alloc_fixed_debug(struct net_buf_pool *pool,
    1391              :                                                         k_timeout_t timeout,
    1392              :                                                         const char *func,
    1393              :                                                         int line);
    1394              : #define net_buf_alloc_fixed(_pool, _timeout) \
    1395              :         net_buf_alloc_fixed_debug(_pool, _timeout, __func__, __LINE__)
    1396              : #else
    1397            1 : struct net_buf * __must_check net_buf_alloc_fixed(struct net_buf_pool *pool,
    1398              :                                                   k_timeout_t timeout);
    1399              : #endif
    1400              : 
    1401              : /**
    1402              :  * @copydetails net_buf_alloc_fixed
    1403              :  */
    1404            1 : static inline struct net_buf * __must_check net_buf_alloc(struct net_buf_pool *pool,
    1405              :                                                           k_timeout_t timeout)
    1406              : {
    1407              :         return net_buf_alloc_fixed(pool, timeout);
    1408              : }
    1409              : 
    1410              : /**
    1411              :  * @brief Allocate a new variable length buffer from a pool.
    1412              :  *
    1413              :  * @param pool Which pool to allocate the buffer from.
    1414              :  * @param size Amount of data the buffer must be able to fit.
    1415              :  * @param timeout Affects the action taken should the pool be empty.
    1416              :  *        If K_NO_WAIT, then return immediately. If K_FOREVER, then
    1417              :  *        wait as long as necessary. Otherwise, wait until the specified
    1418              :  *        timeout. Note that some types of data allocators do not support
    1419              :  *        blocking (such as the HEAP type). In this case it's still possible
    1420              :  *        for net_buf_alloc() to fail (return NULL) even if it was given
    1421              :  *        K_FOREVER.
    1422              :  *
    1423              :  * @return New buffer or NULL if out of buffers.
    1424              :  */
    1425              : #if defined(CONFIG_NET_BUF_LOG)
    1426              : struct net_buf * __must_check net_buf_alloc_len_debug(struct net_buf_pool *pool,
    1427              :                                                       size_t size,
    1428              :                                                       k_timeout_t timeout,
    1429              :                                                       const char *func,
    1430              :                                                       int line);
    1431              : #define net_buf_alloc_len(_pool, _size, _timeout) \
    1432              :         net_buf_alloc_len_debug(_pool, _size, _timeout, __func__, __LINE__)
    1433              : #else
    1434            1 : struct net_buf * __must_check net_buf_alloc_len(struct net_buf_pool *pool,
    1435              :                                                 size_t size,
    1436              :                                                 k_timeout_t timeout);
    1437              : #endif
    1438              : 
    1439              : /**
    1440              :  * @brief Allocate a new buffer from a pool but with external data pointer.
    1441              :  *
    1442              :  * Allocate a new buffer from a pool, where the data pointer comes from the
    1443              :  * user and not from the pool.
    1444              :  *
    1445              :  * @param pool Which pool to allocate the buffer from.
    1446              :  * @param data External data pointer
    1447              :  * @param size Amount of data the pointed data buffer if able to fit.
    1448              :  * @param timeout Affects the action taken should the pool be empty.
    1449              :  *        If K_NO_WAIT, then return immediately. If K_FOREVER, then
    1450              :  *        wait as long as necessary. Otherwise, wait until the specified
    1451              :  *        timeout. Note that some types of data allocators do not support
    1452              :  *        blocking (such as the HEAP type). In this case it's still possible
    1453              :  *        for net_buf_alloc() to fail (return NULL) even if it was given
    1454              :  *        K_FOREVER.
    1455              :  *
    1456              :  * @return New buffer or NULL if out of buffers.
    1457              :  */
    1458              : #if defined(CONFIG_NET_BUF_LOG)
    1459              : struct net_buf * __must_check net_buf_alloc_with_data_debug(struct net_buf_pool *pool,
    1460              :                                                             void *data, size_t size,
    1461              :                                                             k_timeout_t timeout,
    1462              :                                                             const char *func, int line);
    1463              : #define net_buf_alloc_with_data(_pool, _data_, _size, _timeout)         \
    1464              :         net_buf_alloc_with_data_debug(_pool, _data_, _size, _timeout,   \
    1465              :                                       __func__, __LINE__)
    1466              : #else
    1467            1 : struct net_buf * __must_check net_buf_alloc_with_data(struct net_buf_pool *pool,
    1468              :                                                       void *data, size_t size,
    1469              :                                                       k_timeout_t timeout);
    1470              : #endif
    1471              : 
    1472              : /**
    1473              :  * @brief Destroy buffer from custom destroy callback
    1474              :  *
    1475              :  * This helper is only intended to be used from custom destroy callbacks.
    1476              :  * If no custom destroy callback is given to NET_BUF_POOL_*_DEFINE() then
    1477              :  * there is no need to use this API.
    1478              :  *
    1479              :  * @param buf Buffer to destroy.
    1480              :  */
    1481            1 : static inline void net_buf_destroy(struct net_buf *buf)
    1482              : {
    1483              :         struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
    1484              : 
    1485              :         if (buf->__buf) {
    1486              :                 if (!(buf->flags & NET_BUF_EXTERNAL_DATA)) {
    1487              :                         pool->alloc->cb->unref(buf, buf->__buf);
    1488              :                 }
    1489              :                 buf->__buf = NULL;
    1490              :         }
    1491              : 
    1492              :         k_lifo_put(&pool->free, buf);
    1493              : }
    1494              : 
    1495              : /**
    1496              :  * @brief Reset buffer
    1497              :  *
    1498              :  * Reset buffer data and flags so it can be reused for other purposes.
    1499              :  *
    1500              :  * @param buf Buffer to reset.
    1501              :  */
    1502            1 : void net_buf_reset(struct net_buf *buf);
    1503              : 
    1504              : /**
    1505              :  * @brief Initialize buffer with the given headroom.
    1506              :  *
    1507              :  * The buffer is not expected to contain any data when this API is called.
    1508              :  *
    1509              :  * @param buf Buffer to initialize.
    1510              :  * @param reserve How much headroom to reserve.
    1511              :  */
    1512            1 : void net_buf_simple_reserve(struct net_buf_simple *buf, size_t reserve);
    1513              : 
    1514              : /**
    1515              :  * @brief Put a buffer into a list
    1516              :  *
    1517              :  * @param list Which list to append the buffer to.
    1518              :  * @param buf Buffer.
    1519              :  */
    1520            1 : void net_buf_slist_put(sys_slist_t *list, struct net_buf *buf);
    1521              : 
    1522              : /**
    1523              :  * @brief Get a buffer from a list.
    1524              :  *
    1525              :  * @param list Which list to take the buffer from.
    1526              :  *
    1527              :  * @return New buffer or NULL if the FIFO is empty.
    1528              :  */
    1529            1 : struct net_buf * __must_check net_buf_slist_get(sys_slist_t *list);
    1530              : 
    1531              : /**
    1532              :  * @brief Decrements the reference count of a buffer.
    1533              :  *
    1534              :  * The buffer is put back into the pool if the reference count reaches zero.
    1535              :  *
    1536              :  * @param buf A valid pointer on a buffer
    1537              :  */
    1538              : #if defined(CONFIG_NET_BUF_LOG)
    1539              : void net_buf_unref_debug(struct net_buf *buf, const char *func, int line);
    1540              : #define net_buf_unref(_buf) \
    1541              :         net_buf_unref_debug(_buf, __func__, __LINE__)
    1542              : #else
    1543            1 : void net_buf_unref(struct net_buf *buf);
    1544              : #endif
    1545              : 
    1546              : /**
    1547              :  * @brief Increment the reference count of a buffer.
    1548              :  *
    1549              :  * @param buf A valid pointer on a buffer
    1550              :  *
    1551              :  * @return the buffer newly referenced
    1552              :  */
    1553            1 : struct net_buf * __must_check net_buf_ref(struct net_buf *buf);
    1554              : 
    1555              : /**
    1556              :  * @brief Clone buffer
    1557              :  *
    1558              :  * Duplicate given buffer including any (user) data and headers currently stored.
    1559              :  *
    1560              :  * @param buf A valid pointer on a buffer
    1561              :  * @param timeout Affects the action taken should the pool be empty.
    1562              :  *        If K_NO_WAIT, then return immediately. If K_FOREVER, then
    1563              :  *        wait as long as necessary. Otherwise, wait until the specified
    1564              :  *        timeout.
    1565              :  *
    1566              :  * @return Cloned buffer or NULL if out of buffers.
    1567              :  */
    1568            1 : struct net_buf * __must_check net_buf_clone(struct net_buf *buf,
    1569              :                                             k_timeout_t timeout);
    1570              : 
    1571              : /**
    1572              :  * @brief Get a pointer to the user data of a buffer.
    1573              :  *
    1574              :  * @param buf A valid pointer on a buffer
    1575              :  *
    1576              :  * @return Pointer to the user data of the buffer.
    1577              :  */
    1578            1 : static inline void * __must_check net_buf_user_data(const struct net_buf *buf)
    1579              : {
    1580              :         return (void *)buf->user_data;
    1581              : }
    1582              : 
    1583              : /**
    1584              :  * @brief Copy user data from one to another buffer.
    1585              :  *
    1586              :  * @param dst A valid pointer to a buffer gettings its user data overwritten.
    1587              :  * @param src A valid pointer to a buffer gettings its user data copied. User data size must be
    1588              :  *            equal to or exceed @a dst.
    1589              :  *
    1590              :  * @return 0 on success or negative error number on failure.
    1591              :  */
    1592            1 : int net_buf_user_data_copy(struct net_buf *dst, const struct net_buf *src);
    1593              : 
    1594              : /**
    1595              :  * @brief Initialize buffer with the given headroom.
    1596              :  *
    1597              :  * The buffer is not expected to contain any data when this API is called.
    1598              :  *
    1599              :  * @param buf Buffer to initialize.
    1600              :  * @param reserve How much headroom to reserve.
    1601              :  */
    1602            1 : static inline void net_buf_reserve(struct net_buf *buf, size_t reserve)
    1603              : {
    1604              :         net_buf_simple_reserve(&buf->b, reserve);
    1605              : }
    1606              : 
    1607              : /**
    1608              :  * @brief Prepare data to be added at the end of the buffer
    1609              :  *
    1610              :  * Increments the data length of a buffer to account for more data
    1611              :  * at the end.
    1612              :  *
    1613              :  * @param buf Buffer to update.
    1614              :  * @param len Number of bytes to increment the length with.
    1615              :  *
    1616              :  * @return The original tail of the buffer.
    1617              :  */
    1618            1 : static inline void *net_buf_add(struct net_buf *buf, size_t len)
    1619              : {
    1620              :         return net_buf_simple_add(&buf->b, len);
    1621              : }
    1622              : 
    1623              : /**
    1624              :  * @brief Copies the given number of bytes to the end of the buffer
    1625              :  *
    1626              :  * Increments the data length of the  buffer to account for more data at
    1627              :  * the end.
    1628              :  *
    1629              :  * @param buf Buffer to update.
    1630              :  * @param mem Location of data to be added.
    1631              :  * @param len Length of data to be added
    1632              :  *
    1633              :  * @return The original tail of the buffer.
    1634              :  */
    1635            1 : static inline void *net_buf_add_mem(struct net_buf *buf, const void *mem,
    1636              :                                     size_t len)
    1637              : {
    1638              :         return net_buf_simple_add_mem(&buf->b, mem, len);
    1639              : }
    1640              : 
    1641              : /**
    1642              :  * @brief Add (8-bit) byte at the end of the buffer
    1643              :  *
    1644              :  * Increments the data length of the  buffer to account for more data at
    1645              :  * the end.
    1646              :  *
    1647              :  * @param buf Buffer to update.
    1648              :  * @param val byte value to be added.
    1649              :  *
    1650              :  * @return Pointer to the value added
    1651              :  */
    1652            1 : static inline uint8_t *net_buf_add_u8(struct net_buf *buf, uint8_t val)
    1653              : {
    1654              :         return net_buf_simple_add_u8(&buf->b, val);
    1655              : }
    1656              : 
    1657              : /**
    1658              :  * @brief Add 16-bit value at the end of the buffer
    1659              :  *
    1660              :  * Adds 16-bit value in little endian format at the end of buffer.
    1661              :  * Increments the data length of a buffer to account for more data
    1662              :  * at the end.
    1663              :  *
    1664              :  * @param buf Buffer to update.
    1665              :  * @param val 16-bit value to be added.
    1666              :  */
    1667            1 : static inline void net_buf_add_le16(struct net_buf *buf, uint16_t val)
    1668              : {
    1669              :         net_buf_simple_add_le16(&buf->b, val);
    1670              : }
    1671              : 
    1672              : /**
    1673              :  * @brief Add 16-bit value at the end of the buffer
    1674              :  *
    1675              :  * Adds 16-bit value in big endian format at the end of buffer.
    1676              :  * Increments the data length of a buffer to account for more data
    1677              :  * at the end.
    1678              :  *
    1679              :  * @param buf Buffer to update.
    1680              :  * @param val 16-bit value to be added.
    1681              :  */
    1682            1 : static inline void net_buf_add_be16(struct net_buf *buf, uint16_t val)
    1683              : {
    1684              :         net_buf_simple_add_be16(&buf->b, val);
    1685              : }
    1686              : 
    1687              : /**
    1688              :  * @brief Add 24-bit value at the end of the buffer
    1689              :  *
    1690              :  * Adds 24-bit value in little endian format at the end of buffer.
    1691              :  * Increments the data length of a buffer to account for more data
    1692              :  * at the end.
    1693              :  *
    1694              :  * @param buf Buffer to update.
    1695              :  * @param val 24-bit value to be added.
    1696              :  */
    1697            1 : static inline void net_buf_add_le24(struct net_buf *buf, uint32_t val)
    1698              : {
    1699              :         net_buf_simple_add_le24(&buf->b, val);
    1700              : }
    1701              : 
    1702              : /**
    1703              :  * @brief Add 24-bit value at the end of the buffer
    1704              :  *
    1705              :  * Adds 24-bit value in big endian format at the end of buffer.
    1706              :  * Increments the data length of a buffer to account for more data
    1707              :  * at the end.
    1708              :  *
    1709              :  * @param buf Buffer to update.
    1710              :  * @param val 24-bit value to be added.
    1711              :  */
    1712            1 : static inline void net_buf_add_be24(struct net_buf *buf, uint32_t val)
    1713              : {
    1714              :         net_buf_simple_add_be24(&buf->b, val);
    1715              : }
    1716              : 
    1717              : /**
    1718              :  * @brief Add 32-bit value at the end of the buffer
    1719              :  *
    1720              :  * Adds 32-bit value in little endian format at the end of buffer.
    1721              :  * Increments the data length of a buffer to account for more data
    1722              :  * at the end.
    1723              :  *
    1724              :  * @param buf Buffer to update.
    1725              :  * @param val 32-bit value to be added.
    1726              :  */
    1727            1 : static inline void net_buf_add_le32(struct net_buf *buf, uint32_t val)
    1728              : {
    1729              :         net_buf_simple_add_le32(&buf->b, val);
    1730              : }
    1731              : 
    1732              : /**
    1733              :  * @brief Add 32-bit value at the end of the buffer
    1734              :  *
    1735              :  * Adds 32-bit value in big endian format at the end of buffer.
    1736              :  * Increments the data length of a buffer to account for more data
    1737              :  * at the end.
    1738              :  *
    1739              :  * @param buf Buffer to update.
    1740              :  * @param val 32-bit value to be added.
    1741              :  */
    1742            1 : static inline void net_buf_add_be32(struct net_buf *buf, uint32_t val)
    1743              : {
    1744              :         net_buf_simple_add_be32(&buf->b, val);
    1745              : }
    1746              : 
    1747              : /**
    1748              :  * @brief Add 40-bit value at the end of the buffer
    1749              :  *
    1750              :  * Adds 40-bit value in little endian format at the end of buffer.
    1751              :  * Increments the data length of a buffer to account for more data
    1752              :  * at the end.
    1753              :  *
    1754              :  * @param buf Buffer to update.
    1755              :  * @param val 40-bit value to be added.
    1756              :  */
    1757            1 : static inline void net_buf_add_le40(struct net_buf *buf, uint64_t val)
    1758              : {
    1759              :         net_buf_simple_add_le40(&buf->b, val);
    1760              : }
    1761              : 
    1762              : /**
    1763              :  * @brief Add 40-bit value at the end of the buffer
    1764              :  *
    1765              :  * Adds 40-bit value in big endian format at the end of buffer.
    1766              :  * Increments the data length of a buffer to account for more data
    1767              :  * at the end.
    1768              :  *
    1769              :  * @param buf Buffer to update.
    1770              :  * @param val 40-bit value to be added.
    1771              :  */
    1772            1 : static inline void net_buf_add_be40(struct net_buf *buf, uint64_t val)
    1773              : {
    1774              :         net_buf_simple_add_be40(&buf->b, val);
    1775              : }
    1776              : 
    1777              : /**
    1778              :  * @brief Add 48-bit value at the end of the buffer
    1779              :  *
    1780              :  * Adds 48-bit value in little endian format at the end of buffer.
    1781              :  * Increments the data length of a buffer to account for more data
    1782              :  * at the end.
    1783              :  *
    1784              :  * @param buf Buffer to update.
    1785              :  * @param val 48-bit value to be added.
    1786              :  */
    1787            1 : static inline void net_buf_add_le48(struct net_buf *buf, uint64_t val)
    1788              : {
    1789              :         net_buf_simple_add_le48(&buf->b, val);
    1790              : }
    1791              : 
    1792              : /**
    1793              :  * @brief Add 48-bit value at the end of the buffer
    1794              :  *
    1795              :  * Adds 48-bit value in big endian format at the end of buffer.
    1796              :  * Increments the data length of a buffer to account for more data
    1797              :  * at the end.
    1798              :  *
    1799              :  * @param buf Buffer to update.
    1800              :  * @param val 48-bit value to be added.
    1801              :  */
    1802            1 : static inline void net_buf_add_be48(struct net_buf *buf, uint64_t val)
    1803              : {
    1804              :         net_buf_simple_add_be48(&buf->b, val);
    1805              : }
    1806              : 
    1807              : /**
    1808              :  * @brief Add 64-bit value at the end of the buffer
    1809              :  *
    1810              :  * Adds 64-bit value in little endian format at the end of buffer.
    1811              :  * Increments the data length of a buffer to account for more data
    1812              :  * at the end.
    1813              :  *
    1814              :  * @param buf Buffer to update.
    1815              :  * @param val 64-bit value to be added.
    1816              :  */
    1817            1 : static inline void net_buf_add_le64(struct net_buf *buf, uint64_t val)
    1818              : {
    1819              :         net_buf_simple_add_le64(&buf->b, val);
    1820              : }
    1821              : 
    1822              : /**
    1823              :  * @brief Add 64-bit value at the end of the buffer
    1824              :  *
    1825              :  * Adds 64-bit value in big endian format at the end of buffer.
    1826              :  * Increments the data length of a buffer to account for more data
    1827              :  * at the end.
    1828              :  *
    1829              :  * @param buf Buffer to update.
    1830              :  * @param val 64-bit value to be added.
    1831              :  */
    1832            1 : static inline void net_buf_add_be64(struct net_buf *buf, uint64_t val)
    1833              : {
    1834              :         net_buf_simple_add_be64(&buf->b, val);
    1835              : }
    1836              : 
    1837              : /**
    1838              :  * @brief Remove data from the end of the buffer.
    1839              :  *
    1840              :  * Removes data from the end of the buffer by modifying the buffer length.
    1841              :  *
    1842              :  * @param buf Buffer to update.
    1843              :  * @param len Number of bytes to remove.
    1844              :  *
    1845              :  * @return New end of the buffer data.
    1846              :  */
    1847            1 : static inline void *net_buf_remove_mem(struct net_buf *buf, size_t len)
    1848              : {
    1849              :         return net_buf_simple_remove_mem(&buf->b, len);
    1850              : }
    1851              : 
    1852              : /**
    1853              :  * @brief Remove a 8-bit value from the end of the buffer
    1854              :  *
    1855              :  * Same idea as with net_buf_remove_mem(), but a helper for operating on
    1856              :  * 8-bit values.
    1857              :  *
    1858              :  * @param buf A valid pointer on a buffer.
    1859              :  *
    1860              :  * @return The 8-bit removed value
    1861              :  */
    1862            1 : static inline uint8_t net_buf_remove_u8(struct net_buf *buf)
    1863              : {
    1864              :         return net_buf_simple_remove_u8(&buf->b);
    1865              : }
    1866              : 
    1867              : /**
    1868              :  * @brief Remove and convert 16 bits from the end of the buffer.
    1869              :  *
    1870              :  * Same idea as with net_buf_remove_mem(), but a helper for operating on
    1871              :  * 16-bit little endian data.
    1872              :  *
    1873              :  * @param buf A valid pointer on a buffer.
    1874              :  *
    1875              :  * @return 16-bit value converted from little endian to host endian.
    1876              :  */
    1877            1 : static inline uint16_t net_buf_remove_le16(struct net_buf *buf)
    1878              : {
    1879              :         return net_buf_simple_remove_le16(&buf->b);
    1880              : }
    1881              : 
    1882              : /**
    1883              :  * @brief Remove and convert 16 bits from the end of the buffer.
    1884              :  *
    1885              :  * Same idea as with net_buf_remove_mem(), but a helper for operating on
    1886              :  * 16-bit big endian data.
    1887              :  *
    1888              :  * @param buf A valid pointer on a buffer.
    1889              :  *
    1890              :  * @return 16-bit value converted from big endian to host endian.
    1891              :  */
    1892            1 : static inline uint16_t net_buf_remove_be16(struct net_buf *buf)
    1893              : {
    1894              :         return net_buf_simple_remove_be16(&buf->b);
    1895              : }
    1896              : 
    1897              : /**
    1898              :  * @brief Remove and convert 24 bits from the end of the buffer.
    1899              :  *
    1900              :  * Same idea as with net_buf_remove_mem(), but a helper for operating on
    1901              :  * 24-bit big endian data.
    1902              :  *
    1903              :  * @param buf A valid pointer on a buffer.
    1904              :  *
    1905              :  * @return 24-bit value converted from big endian to host endian.
    1906              :  */
    1907            1 : static inline uint32_t net_buf_remove_be24(struct net_buf *buf)
    1908              : {
    1909              :         return net_buf_simple_remove_be24(&buf->b);
    1910              : }
    1911              : 
    1912              : /**
    1913              :  * @brief Remove and convert 24 bits from the end of the buffer.
    1914              :  *
    1915              :  * Same idea as with net_buf_remove_mem(), but a helper for operating on
    1916              :  * 24-bit little endian data.
    1917              :  *
    1918              :  * @param buf A valid pointer on a buffer.
    1919              :  *
    1920              :  * @return 24-bit value converted from little endian to host endian.
    1921              :  */
    1922            1 : static inline uint32_t net_buf_remove_le24(struct net_buf *buf)
    1923              : {
    1924              :         return net_buf_simple_remove_le24(&buf->b);
    1925              : }
    1926              : 
    1927              : /**
    1928              :  * @brief Remove and convert 32 bits from the end of the buffer.
    1929              :  *
    1930              :  * Same idea as with net_buf_remove_mem(), but a helper for operating on
    1931              :  * 32-bit little endian data.
    1932              :  *
    1933              :  * @param buf A valid pointer on a buffer.
    1934              :  *
    1935              :  * @return 32-bit value converted from little endian to host endian.
    1936              :  */
    1937            1 : static inline uint32_t net_buf_remove_le32(struct net_buf *buf)
    1938              : {
    1939              :         return net_buf_simple_remove_le32(&buf->b);
    1940              : }
    1941              : 
    1942              : /**
    1943              :  * @brief Remove and convert 32 bits from the end of the buffer.
    1944              :  *
    1945              :  * Same idea as with net_buf_remove_mem(), but a helper for operating on
    1946              :  * 32-bit big endian data.
    1947              :  *
    1948              :  * @param buf A valid pointer on a buffer
    1949              :  *
    1950              :  * @return 32-bit value converted from big endian to host endian.
    1951              :  */
    1952            1 : static inline uint32_t net_buf_remove_be32(struct net_buf *buf)
    1953              : {
    1954              :         return net_buf_simple_remove_be32(&buf->b);
    1955              : }
    1956              : 
    1957              : /**
    1958              :  * @brief Remove and convert 40 bits from the end of the buffer.
    1959              :  *
    1960              :  * Same idea as with net_buf_remove_mem(), but a helper for operating on
    1961              :  * 40-bit little endian data.
    1962              :  *
    1963              :  * @param buf A valid pointer on a buffer.
    1964              :  *
    1965              :  * @return 40-bit value converted from little endian to host endian.
    1966              :  */
    1967            1 : static inline uint64_t net_buf_remove_le40(struct net_buf *buf)
    1968              : {
    1969              :         return net_buf_simple_remove_le40(&buf->b);
    1970              : }
    1971              : 
    1972              : /**
    1973              :  * @brief Remove and convert 40 bits from the end of the buffer.
    1974              :  *
    1975              :  * Same idea as with net_buf_remove_mem(), but a helper for operating on
    1976              :  * 40-bit big endian data.
    1977              :  *
    1978              :  * @param buf A valid pointer on a buffer
    1979              :  *
    1980              :  * @return 40-bit value converted from big endian to host endian.
    1981              :  */
    1982            1 : static inline uint64_t net_buf_remove_be40(struct net_buf *buf)
    1983              : {
    1984              :         return net_buf_simple_remove_be40(&buf->b);
    1985              : }
    1986              : 
    1987              : /**
    1988              :  * @brief Remove and convert 48 bits from the end of the buffer.
    1989              :  *
    1990              :  * Same idea as with net_buf_remove_mem(), but a helper for operating on
    1991              :  * 48-bit little endian data.
    1992              :  *
    1993              :  * @param buf A valid pointer on a buffer.
    1994              :  *
    1995              :  * @return 48-bit value converted from little endian to host endian.
    1996              :  */
    1997            1 : static inline uint64_t net_buf_remove_le48(struct net_buf *buf)
    1998              : {
    1999              :         return net_buf_simple_remove_le48(&buf->b);
    2000              : }
    2001              : 
    2002              : /**
    2003              :  * @brief Remove and convert 48 bits from the end of the buffer.
    2004              :  *
    2005              :  * Same idea as with net_buf_remove_mem(), but a helper for operating on
    2006              :  * 48-bit big endian data.
    2007              :  *
    2008              :  * @param buf A valid pointer on a buffer
    2009              :  *
    2010              :  * @return 48-bit value converted from big endian to host endian.
    2011              :  */
    2012            1 : static inline uint64_t net_buf_remove_be48(struct net_buf *buf)
    2013              : {
    2014              :         return net_buf_simple_remove_be48(&buf->b);
    2015              : }
    2016              : 
    2017              : /**
    2018              :  * @brief Remove and convert 64 bits from the end of the buffer.
    2019              :  *
    2020              :  * Same idea as with net_buf_remove_mem(), but a helper for operating on
    2021              :  * 64-bit little endian data.
    2022              :  *
    2023              :  * @param buf A valid pointer on a buffer.
    2024              :  *
    2025              :  * @return 64-bit value converted from little endian to host endian.
    2026              :  */
    2027            1 : static inline uint64_t net_buf_remove_le64(struct net_buf *buf)
    2028              : {
    2029              :         return net_buf_simple_remove_le64(&buf->b);
    2030              : }
    2031              : 
    2032              : /**
    2033              :  * @brief Remove and convert 64 bits from the end of the buffer.
    2034              :  *
    2035              :  * Same idea as with net_buf_remove_mem(), but a helper for operating on
    2036              :  * 64-bit big endian data.
    2037              :  *
    2038              :  * @param buf A valid pointer on a buffer
    2039              :  *
    2040              :  * @return 64-bit value converted from big endian to host endian.
    2041              :  */
    2042            1 : static inline uint64_t net_buf_remove_be64(struct net_buf *buf)
    2043              : {
    2044              :         return net_buf_simple_remove_be64(&buf->b);
    2045              : }
    2046              : 
    2047              : /**
    2048              :  * @brief Prepare data to be added at the start of the buffer
    2049              :  *
    2050              :  * Modifies the data pointer and buffer length to account for more data
    2051              :  * in the beginning of the buffer.
    2052              :  *
    2053              :  * @param buf Buffer to update.
    2054              :  * @param len Number of bytes to add to the beginning.
    2055              :  *
    2056              :  * @return The new beginning of the buffer data.
    2057              :  */
    2058            1 : static inline void *net_buf_push(struct net_buf *buf, size_t len)
    2059              : {
    2060              :         return net_buf_simple_push(&buf->b, len);
    2061              : }
    2062              : 
    2063              : /**
    2064              :  * @brief Copies the given number of bytes to the start of the buffer
    2065              :  *
    2066              :  * Modifies the data pointer and buffer length to account for more data
    2067              :  * in the beginning of the buffer.
    2068              :  *
    2069              :  * @param buf Buffer to update.
    2070              :  * @param mem Location of data to be added.
    2071              :  * @param len Length of data to be added.
    2072              :  *
    2073              :  * @return The new beginning of the buffer data.
    2074              :  */
    2075            1 : static inline void *net_buf_push_mem(struct net_buf *buf, const void *mem,
    2076              :                                      size_t len)
    2077              : {
    2078              :         return net_buf_simple_push_mem(&buf->b, mem, len);
    2079              : }
    2080              : 
    2081              : /**
    2082              :  * @brief Push 8-bit value to the beginning of the buffer
    2083              :  *
    2084              :  * Adds 8-bit value the beginning of the buffer.
    2085              :  *
    2086              :  * @param buf Buffer to update.
    2087              :  * @param val 8-bit value to be pushed to the buffer.
    2088              :  */
    2089            1 : static inline void net_buf_push_u8(struct net_buf *buf, uint8_t val)
    2090              : {
    2091              :         net_buf_simple_push_u8(&buf->b, val);
    2092              : }
    2093              : 
    2094              : /**
    2095              :  * @brief Push 16-bit value to the beginning of the buffer
    2096              :  *
    2097              :  * Adds 16-bit value in little endian format to the beginning of the
    2098              :  * buffer.
    2099              :  *
    2100              :  * @param buf Buffer to update.
    2101              :  * @param val 16-bit value to be pushed to the buffer.
    2102              :  */
    2103            1 : static inline void net_buf_push_le16(struct net_buf *buf, uint16_t val)
    2104              : {
    2105              :         net_buf_simple_push_le16(&buf->b, val);
    2106              : }
    2107              : 
    2108              : /**
    2109              :  * @brief Push 16-bit value to the beginning of the buffer
    2110              :  *
    2111              :  * Adds 16-bit value in big endian format to the beginning of the
    2112              :  * buffer.
    2113              :  *
    2114              :  * @param buf Buffer to update.
    2115              :  * @param val 16-bit value to be pushed to the buffer.
    2116              :  */
    2117            1 : static inline void net_buf_push_be16(struct net_buf *buf, uint16_t val)
    2118              : {
    2119              :         net_buf_simple_push_be16(&buf->b, val);
    2120              : }
    2121              : 
    2122              : /**
    2123              :  * @brief Push 24-bit value to the beginning of the buffer
    2124              :  *
    2125              :  * Adds 24-bit value in little endian format to the beginning of the
    2126              :  * buffer.
    2127              :  *
    2128              :  * @param buf Buffer to update.
    2129              :  * @param val 24-bit value to be pushed to the buffer.
    2130              :  */
    2131            1 : static inline void net_buf_push_le24(struct net_buf *buf, uint32_t val)
    2132              : {
    2133              :         net_buf_simple_push_le24(&buf->b, val);
    2134              : }
    2135              : 
    2136              : /**
    2137              :  * @brief Push 24-bit value to the beginning of the buffer
    2138              :  *
    2139              :  * Adds 24-bit value in big endian format to the beginning of the
    2140              :  * buffer.
    2141              :  *
    2142              :  * @param buf Buffer to update.
    2143              :  * @param val 24-bit value to be pushed to the buffer.
    2144              :  */
    2145            1 : static inline void net_buf_push_be24(struct net_buf *buf, uint32_t val)
    2146              : {
    2147              :         net_buf_simple_push_be24(&buf->b, val);
    2148              : }
    2149              : 
    2150              : /**
    2151              :  * @brief Push 32-bit value to the beginning of the buffer
    2152              :  *
    2153              :  * Adds 32-bit value in little endian format to the beginning of the
    2154              :  * buffer.
    2155              :  *
    2156              :  * @param buf Buffer to update.
    2157              :  * @param val 32-bit value to be pushed to the buffer.
    2158              :  */
    2159            1 : static inline void net_buf_push_le32(struct net_buf *buf, uint32_t val)
    2160              : {
    2161              :         net_buf_simple_push_le32(&buf->b, val);
    2162              : }
    2163              : 
    2164              : /**
    2165              :  * @brief Push 32-bit value to the beginning of the buffer
    2166              :  *
    2167              :  * Adds 32-bit value in big endian format to the beginning of the
    2168              :  * buffer.
    2169              :  *
    2170              :  * @param buf Buffer to update.
    2171              :  * @param val 32-bit value to be pushed to the buffer.
    2172              :  */
    2173            1 : static inline void net_buf_push_be32(struct net_buf *buf, uint32_t val)
    2174              : {
    2175              :         net_buf_simple_push_be32(&buf->b, val);
    2176              : }
    2177              : 
    2178              : /**
    2179              :  * @brief Push 40-bit value to the beginning of the buffer
    2180              :  *
    2181              :  * Adds 40-bit value in little endian format to the beginning of the
    2182              :  * buffer.
    2183              :  *
    2184              :  * @param buf Buffer to update.
    2185              :  * @param val 40-bit value to be pushed to the buffer.
    2186              :  */
    2187            1 : static inline void net_buf_push_le40(struct net_buf *buf, uint64_t val)
    2188              : {
    2189              :         net_buf_simple_push_le40(&buf->b, val);
    2190              : }
    2191              : 
    2192              : /**
    2193              :  * @brief Push 40-bit value to the beginning of the buffer
    2194              :  *
    2195              :  * Adds 40-bit value in big endian format to the beginning of the
    2196              :  * buffer.
    2197              :  *
    2198              :  * @param buf Buffer to update.
    2199              :  * @param val 40-bit value to be pushed to the buffer.
    2200              :  */
    2201            1 : static inline void net_buf_push_be40(struct net_buf *buf, uint64_t val)
    2202              : {
    2203              :         net_buf_simple_push_be40(&buf->b, val);
    2204              : }
    2205              : 
    2206              : /**
    2207              :  * @brief Push 48-bit value to the beginning of the buffer
    2208              :  *
    2209              :  * Adds 48-bit value in little endian format to the beginning of the
    2210              :  * buffer.
    2211              :  *
    2212              :  * @param buf Buffer to update.
    2213              :  * @param val 48-bit value to be pushed to the buffer.
    2214              :  */
    2215            1 : static inline void net_buf_push_le48(struct net_buf *buf, uint64_t val)
    2216              : {
    2217              :         net_buf_simple_push_le48(&buf->b, val);
    2218              : }
    2219              : 
    2220              : /**
    2221              :  * @brief Push 48-bit value to the beginning of the buffer
    2222              :  *
    2223              :  * Adds 48-bit value in big endian format to the beginning of the
    2224              :  * buffer.
    2225              :  *
    2226              :  * @param buf Buffer to update.
    2227              :  * @param val 48-bit value to be pushed to the buffer.
    2228              :  */
    2229            1 : static inline void net_buf_push_be48(struct net_buf *buf, uint64_t val)
    2230              : {
    2231              :         net_buf_simple_push_be48(&buf->b, val);
    2232              : }
    2233              : 
    2234              : /**
    2235              :  * @brief Push 64-bit value to the beginning of the buffer
    2236              :  *
    2237              :  * Adds 64-bit value in little endian format to the beginning of the
    2238              :  * buffer.
    2239              :  *
    2240              :  * @param buf Buffer to update.
    2241              :  * @param val 64-bit value to be pushed to the buffer.
    2242              :  */
    2243            1 : static inline void net_buf_push_le64(struct net_buf *buf, uint64_t val)
    2244              : {
    2245              :         net_buf_simple_push_le64(&buf->b, val);
    2246              : }
    2247              : 
    2248              : /**
    2249              :  * @brief Push 64-bit value to the beginning of the buffer
    2250              :  *
    2251              :  * Adds 64-bit value in big endian format to the beginning of the
    2252              :  * buffer.
    2253              :  *
    2254              :  * @param buf Buffer to update.
    2255              :  * @param val 64-bit value to be pushed to the buffer.
    2256              :  */
    2257            1 : static inline void net_buf_push_be64(struct net_buf *buf, uint64_t val)
    2258              : {
    2259              :         net_buf_simple_push_be64(&buf->b, val);
    2260              : }
    2261              : 
    2262              : /**
    2263              :  * @brief Remove data from the beginning of the buffer.
    2264              :  *
    2265              :  * Removes data from the beginning of the buffer by modifying the data
    2266              :  * pointer and buffer length.
    2267              :  *
    2268              :  * @param buf Buffer to update.
    2269              :  * @param len Number of bytes to remove.
    2270              :  *
    2271              :  * @return New beginning of the buffer data.
    2272              :  */
    2273            1 : static inline void *net_buf_pull(struct net_buf *buf, size_t len)
    2274              : {
    2275              :         return net_buf_simple_pull(&buf->b, len);
    2276              : }
    2277              : 
    2278              : /**
    2279              :  * @brief Remove data from the beginning of the buffer.
    2280              :  *
    2281              :  * Removes data from the beginning of the buffer by modifying the data
    2282              :  * pointer and buffer length.
    2283              :  *
    2284              :  * @param buf Buffer to update.
    2285              :  * @param len Number of bytes to remove.
    2286              :  *
    2287              :  * @return Pointer to the old beginning of the buffer data.
    2288              :  */
    2289            1 : static inline void *net_buf_pull_mem(struct net_buf *buf, size_t len)
    2290              : {
    2291              :         return net_buf_simple_pull_mem(&buf->b, len);
    2292              : }
    2293              : 
    2294              : /**
    2295              :  * @brief Remove a 8-bit value from the beginning of the buffer
    2296              :  *
    2297              :  * Same idea as with net_buf_pull(), but a helper for operating on
    2298              :  * 8-bit values.
    2299              :  *
    2300              :  * @param buf A valid pointer on a buffer.
    2301              :  *
    2302              :  * @return The 8-bit removed value
    2303              :  */
    2304            1 : static inline uint8_t net_buf_pull_u8(struct net_buf *buf)
    2305              : {
    2306              :         return net_buf_simple_pull_u8(&buf->b);
    2307              : }
    2308              : 
    2309              : /**
    2310              :  * @brief Remove and convert 16 bits from the beginning of the buffer.
    2311              :  *
    2312              :  * Same idea as with net_buf_pull(), but a helper for operating on
    2313              :  * 16-bit little endian data.
    2314              :  *
    2315              :  * @param buf A valid pointer on a buffer.
    2316              :  *
    2317              :  * @return 16-bit value converted from little endian to host endian.
    2318              :  */
    2319            1 : static inline uint16_t net_buf_pull_le16(struct net_buf *buf)
    2320              : {
    2321              :         return net_buf_simple_pull_le16(&buf->b);
    2322              : }
    2323              : 
    2324              : /**
    2325              :  * @brief Remove and convert 16 bits from the beginning of the buffer.
    2326              :  *
    2327              :  * Same idea as with net_buf_pull(), but a helper for operating on
    2328              :  * 16-bit big endian data.
    2329              :  *
    2330              :  * @param buf A valid pointer on a buffer.
    2331              :  *
    2332              :  * @return 16-bit value converted from big endian to host endian.
    2333              :  */
    2334            1 : static inline uint16_t net_buf_pull_be16(struct net_buf *buf)
    2335              : {
    2336              :         return net_buf_simple_pull_be16(&buf->b);
    2337              : }
    2338              : 
    2339              : /**
    2340              :  * @brief Remove and convert 24 bits from the beginning of the buffer.
    2341              :  *
    2342              :  * Same idea as with net_buf_pull(), but a helper for operating on
    2343              :  * 24-bit little endian data.
    2344              :  *
    2345              :  * @param buf A valid pointer on a buffer.
    2346              :  *
    2347              :  * @return 24-bit value converted from little endian to host endian.
    2348              :  */
    2349            1 : static inline uint32_t net_buf_pull_le24(struct net_buf *buf)
    2350              : {
    2351              :         return net_buf_simple_pull_le24(&buf->b);
    2352              : }
    2353              : 
    2354              : /**
    2355              :  * @brief Remove and convert 24 bits from the beginning of the buffer.
    2356              :  *
    2357              :  * Same idea as with net_buf_pull(), but a helper for operating on
    2358              :  * 24-bit big endian data.
    2359              :  *
    2360              :  * @param buf A valid pointer on a buffer.
    2361              :  *
    2362              :  * @return 24-bit value converted from big endian to host endian.
    2363              :  */
    2364            1 : static inline uint32_t net_buf_pull_be24(struct net_buf *buf)
    2365              : {
    2366              :         return net_buf_simple_pull_be24(&buf->b);
    2367              : }
    2368              : 
    2369              : /**
    2370              :  * @brief Remove and convert 32 bits from the beginning of the buffer.
    2371              :  *
    2372              :  * Same idea as with net_buf_pull(), but a helper for operating on
    2373              :  * 32-bit little endian data.
    2374              :  *
    2375              :  * @param buf A valid pointer on a buffer.
    2376              :  *
    2377              :  * @return 32-bit value converted from little endian to host endian.
    2378              :  */
    2379            1 : static inline uint32_t net_buf_pull_le32(struct net_buf *buf)
    2380              : {
    2381              :         return net_buf_simple_pull_le32(&buf->b);
    2382              : }
    2383              : 
    2384              : /**
    2385              :  * @brief Remove and convert 32 bits from the beginning of the buffer.
    2386              :  *
    2387              :  * Same idea as with net_buf_pull(), but a helper for operating on
    2388              :  * 32-bit big endian data.
    2389              :  *
    2390              :  * @param buf A valid pointer on a buffer
    2391              :  *
    2392              :  * @return 32-bit value converted from big endian to host endian.
    2393              :  */
    2394            1 : static inline uint32_t net_buf_pull_be32(struct net_buf *buf)
    2395              : {
    2396              :         return net_buf_simple_pull_be32(&buf->b);
    2397              : }
    2398              : 
    2399              : /**
    2400              :  * @brief Remove and convert 40 bits from the beginning of the buffer.
    2401              :  *
    2402              :  * Same idea as with net_buf_pull(), but a helper for operating on
    2403              :  * 40-bit little endian data.
    2404              :  *
    2405              :  * @param buf A valid pointer on a buffer.
    2406              :  *
    2407              :  * @return 40-bit value converted from little endian to host endian.
    2408              :  */
    2409            1 : static inline uint64_t net_buf_pull_le40(struct net_buf *buf)
    2410              : {
    2411              :         return net_buf_simple_pull_le40(&buf->b);
    2412              : }
    2413              : 
    2414              : /**
    2415              :  * @brief Remove and convert 40 bits from the beginning of the buffer.
    2416              :  *
    2417              :  * Same idea as with net_buf_pull(), but a helper for operating on
    2418              :  * 40-bit big endian data.
    2419              :  *
    2420              :  * @param buf A valid pointer on a buffer
    2421              :  *
    2422              :  * @return 40-bit value converted from big endian to host endian.
    2423              :  */
    2424            1 : static inline uint64_t net_buf_pull_be40(struct net_buf *buf)
    2425              : {
    2426              :         return net_buf_simple_pull_be40(&buf->b);
    2427              : }
    2428              : 
    2429              : /**
    2430              :  * @brief Remove and convert 48 bits from the beginning of the buffer.
    2431              :  *
    2432              :  * Same idea as with net_buf_pull(), but a helper for operating on
    2433              :  * 48-bit little endian data.
    2434              :  *
    2435              :  * @param buf A valid pointer on a buffer.
    2436              :  *
    2437              :  * @return 48-bit value converted from little endian to host endian.
    2438              :  */
    2439            1 : static inline uint64_t net_buf_pull_le48(struct net_buf *buf)
    2440              : {
    2441              :         return net_buf_simple_pull_le48(&buf->b);
    2442              : }
    2443              : 
    2444              : /**
    2445              :  * @brief Remove and convert 48 bits from the beginning of the buffer.
    2446              :  *
    2447              :  * Same idea as with net_buf_pull(), but a helper for operating on
    2448              :  * 48-bit big endian data.
    2449              :  *
    2450              :  * @param buf A valid pointer on a buffer
    2451              :  *
    2452              :  * @return 48-bit value converted from big endian to host endian.
    2453              :  */
    2454            1 : static inline uint64_t net_buf_pull_be48(struct net_buf *buf)
    2455              : {
    2456              :         return net_buf_simple_pull_be48(&buf->b);
    2457              : }
    2458              : 
    2459              : /**
    2460              :  * @brief Remove and convert 64 bits from the beginning of the buffer.
    2461              :  *
    2462              :  * Same idea as with net_buf_pull(), but a helper for operating on
    2463              :  * 64-bit little endian data.
    2464              :  *
    2465              :  * @param buf A valid pointer on a buffer.
    2466              :  *
    2467              :  * @return 64-bit value converted from little endian to host endian.
    2468              :  */
    2469            1 : static inline uint64_t net_buf_pull_le64(struct net_buf *buf)
    2470              : {
    2471              :         return net_buf_simple_pull_le64(&buf->b);
    2472              : }
    2473              : 
    2474              : /**
    2475              :  * @brief Remove and convert 64 bits from the beginning of the buffer.
    2476              :  *
    2477              :  * Same idea as with net_buf_pull(), but a helper for operating on
    2478              :  * 64-bit big endian data.
    2479              :  *
    2480              :  * @param buf A valid pointer on a buffer
    2481              :  *
    2482              :  * @return 64-bit value converted from big endian to host endian.
    2483              :  */
    2484            1 : static inline uint64_t net_buf_pull_be64(struct net_buf *buf)
    2485              : {
    2486              :         return net_buf_simple_pull_be64(&buf->b);
    2487              : }
    2488              : 
    2489              : /**
    2490              :  * @brief Check buffer tailroom.
    2491              :  *
    2492              :  * Check how much free space there is at the end of the buffer.
    2493              :  *
    2494              :  * @param buf A valid pointer on a buffer
    2495              :  *
    2496              :  * @return Number of bytes available at the end of the buffer.
    2497              :  */
    2498            1 : static inline size_t net_buf_tailroom(const struct net_buf *buf)
    2499              : {
    2500              :         return net_buf_simple_tailroom(&buf->b);
    2501              : }
    2502              : 
    2503              : /**
    2504              :  * @brief Check buffer headroom.
    2505              :  *
    2506              :  * Check how much free space there is in the beginning of the buffer.
    2507              :  *
    2508              :  * buf A valid pointer on a buffer
    2509              :  *
    2510              :  * @return Number of bytes available in the beginning of the buffer.
    2511              :  */
    2512            1 : static inline size_t net_buf_headroom(const struct net_buf *buf)
    2513              : {
    2514              :         return net_buf_simple_headroom(&buf->b);
    2515              : }
    2516              : 
    2517              : /**
    2518              :  * @brief Check maximum net_buf::len value.
    2519              :  *
    2520              :  * This value is depending on the number of bytes being reserved as headroom.
    2521              :  *
    2522              :  * @param buf A valid pointer on a buffer
    2523              :  *
    2524              :  * @return Number of bytes usable behind the net_buf::data pointer.
    2525              :  */
    2526            1 : static inline uint16_t net_buf_max_len(const struct net_buf *buf)
    2527              : {
    2528              :         return net_buf_simple_max_len(&buf->b);
    2529              : }
    2530              : 
    2531              : /**
    2532              :  * @brief Get the tail pointer for a buffer.
    2533              :  *
    2534              :  * Get a pointer to the end of the data in a buffer.
    2535              :  *
    2536              :  * @param buf Buffer.
    2537              :  *
    2538              :  * @return Tail pointer for the buffer.
    2539              :  */
    2540            1 : static inline uint8_t *net_buf_tail(const struct net_buf *buf)
    2541              : {
    2542              :         return net_buf_simple_tail(&buf->b);
    2543              : }
    2544              : 
    2545              : /**
    2546              :  * @brief Find the last fragment in the fragment list.
    2547              :  *
    2548              :  * @return Pointer to last fragment in the list.
    2549              :  */
    2550            1 : struct net_buf *net_buf_frag_last(struct net_buf *frags);
    2551              : 
    2552              : /**
    2553              :  * @brief Insert a new fragment to a chain of bufs.
    2554              :  *
    2555              :  * Insert a new fragment into the buffer fragments list after the parent.
    2556              :  *
    2557              :  * Note: This function takes ownership of the fragment reference so the
    2558              :  * caller is not required to unref.
    2559              :  *
    2560              :  * @param parent Parent buffer/fragment.
    2561              :  * @param frag Fragment to insert.
    2562              :  */
    2563            1 : void net_buf_frag_insert(struct net_buf *parent, struct net_buf *frag);
    2564              : 
    2565              : /**
    2566              :  * @brief Add a new fragment to the end of a chain of bufs.
    2567              :  *
    2568              :  * Append a new fragment into the buffer fragments list.
    2569              :  *
    2570              :  * Note: This function takes ownership of the fragment reference so the
    2571              :  * caller is not required to unref.
    2572              :  *
    2573              :  * @param head Head of the fragment chain.
    2574              :  * @param frag Fragment to add.
    2575              :  *
    2576              :  * @return New head of the fragment chain. Either head (if head
    2577              :  *         was non-NULL) or frag (if head was NULL).
    2578              :  */
    2579            1 : struct net_buf *net_buf_frag_add(struct net_buf *head, struct net_buf *frag);
    2580              : 
    2581              : /**
    2582              :  * @brief Delete existing fragment from a chain of bufs.
    2583              :  *
    2584              :  * @param parent Parent buffer/fragment, or NULL if there is no parent.
    2585              :  * @param frag Fragment to delete.
    2586              :  *
    2587              :  * @return Pointer to the buffer following the fragment, or NULL if it
    2588              :  *         had no further fragments.
    2589              :  */
    2590              : #if defined(CONFIG_NET_BUF_LOG)
    2591              : struct net_buf *net_buf_frag_del_debug(struct net_buf *parent,
    2592              :                                        struct net_buf *frag,
    2593              :                                        const char *func, int line);
    2594              : #define net_buf_frag_del(_parent, _frag) \
    2595              :         net_buf_frag_del_debug(_parent, _frag, __func__, __LINE__)
    2596              : #else
    2597            1 : struct net_buf *net_buf_frag_del(struct net_buf *parent, struct net_buf *frag);
    2598              : #endif
    2599              : 
    2600              : /**
    2601              :  * @brief Copy bytes from net_buf chain starting at offset to linear buffer
    2602              :  *
    2603              :  * Copy (extract) @a len bytes from @a src net_buf chain, starting from @a
    2604              :  * offset in it, to a linear buffer @a dst. Return number of bytes actually
    2605              :  * copied, which may be less than requested, if net_buf chain doesn't have
    2606              :  * enough data, or destination buffer is too small.
    2607              :  *
    2608              :  * @param dst Destination buffer
    2609              :  * @param dst_len Destination buffer length
    2610              :  * @param src Source net_buf chain
    2611              :  * @param offset Starting offset to copy from
    2612              :  * @param len Number of bytes to copy
    2613              :  * @return number of bytes actually copied
    2614              :  */
    2615            1 : size_t net_buf_linearize(void *dst, size_t dst_len,
    2616              :                          const struct net_buf *src, size_t offset, size_t len);
    2617              : 
    2618              : /**
    2619              :  * @typedef net_buf_allocator_cb
    2620              :  * @brief Network buffer allocator callback.
    2621              :  *
    2622              :  * @details The allocator callback is called when net_buf_append_bytes
    2623              :  * needs to allocate a new net_buf.
    2624              :  *
    2625              :  * @param timeout Affects the action taken should the net buf pool be empty.
    2626              :  *        If K_NO_WAIT, then return immediately. If K_FOREVER, then
    2627              :  *        wait as long as necessary. Otherwise, wait until the specified
    2628              :  *        timeout.
    2629              :  * @param user_data The user data given in net_buf_append_bytes call.
    2630              :  * @return pointer to allocated net_buf or NULL on error.
    2631              :  */
    2632              : typedef struct net_buf * __must_check (*net_buf_allocator_cb)(k_timeout_t timeout,
    2633              :                                                               void *user_data);
    2634              : 
    2635              : /**
    2636              :  * @brief Append data to a list of net_buf
    2637              :  *
    2638              :  * @details Append data to a net_buf. If there is not enough space in the
    2639              :  * net_buf then more net_buf will be added, unless there are no free net_buf
    2640              :  * and timeout occurs. If not allocator is provided it attempts to allocate from
    2641              :  * the same pool as the original buffer.
    2642              :  *
    2643              :  * @param buf Network buffer.
    2644              :  * @param len Total length of input data
    2645              :  * @param value Data to be added
    2646              :  * @param timeout Timeout is passed to the net_buf allocator callback.
    2647              :  * @param allocate_cb When a new net_buf is required, use this callback.
    2648              :  * @param user_data A user data pointer to be supplied to the allocate_cb.
    2649              :  *        This pointer is can be anything from a mem_pool or a net_pkt, the
    2650              :  *        logic is left up to the allocate_cb function.
    2651              :  *
    2652              :  * @return Length of data actually added. This may be less than input
    2653              :  *         length if other timeout than K_FOREVER was used, and there
    2654              :  *         were no free fragments in a pool to accommodate all data.
    2655              :  */
    2656            1 : size_t net_buf_append_bytes(struct net_buf *buf, size_t len,
    2657              :                             const void *value, k_timeout_t timeout,
    2658              :                             net_buf_allocator_cb allocate_cb, void *user_data);
    2659              : 
    2660              : /**
    2661              :  * @brief Match data with a net_buf's content
    2662              :  *
    2663              :  * @details Compare data with a content of a net_buf. Provide information about
    2664              :  * the number of bytes matching between both. If needed, traverse
    2665              :  * through multiple buffer fragments.
    2666              :  *
    2667              :  * @param buf Network buffer
    2668              :  * @param offset Starting offset to compare from
    2669              :  * @param data Data buffer for comparison
    2670              :  * @param len Number of bytes to compare
    2671              :  *
    2672              :  * @return The number of bytes compared before the first difference.
    2673              :  */
    2674            1 : size_t net_buf_data_match(const struct net_buf *buf, size_t offset, const void *data, size_t len);
    2675              : 
    2676              : /**
    2677              :  * @brief Skip N number of bytes in a net_buf
    2678              :  *
    2679              :  * @details Skip N number of bytes starting from fragment's offset. If the total
    2680              :  * length of data is placed in multiple fragments, this function will skip from
    2681              :  * all fragments until it reaches N number of bytes.  Any fully skipped buffers
    2682              :  * are removed from the net_buf list.
    2683              :  *
    2684              :  * @param buf Network buffer.
    2685              :  * @param len Total length of data to be skipped.
    2686              :  *
    2687              :  * @return Pointer to the fragment or
    2688              :  *         NULL and pos is 0 after successful skip,
    2689              :  *         NULL and pos is 0xffff otherwise.
    2690              :  */
    2691            1 : static inline struct net_buf *net_buf_skip(struct net_buf *buf, size_t len)
    2692              : {
    2693              :         while (buf && len--) {
    2694              :                 net_buf_pull_u8(buf);
    2695              :                 if (!buf->len) {
    2696              :                         buf = net_buf_frag_del(NULL, buf);
    2697              :                 }
    2698              :         }
    2699              : 
    2700              :         return buf;
    2701              : }
    2702              : 
    2703              : /**
    2704              :  * @brief Calculate amount of bytes stored in fragments.
    2705              :  *
    2706              :  * Calculates the total amount of data stored in the given buffer and the
    2707              :  * fragments linked to it.
    2708              :  *
    2709              :  * @param buf Buffer to start off with.
    2710              :  *
    2711              :  * @return Number of bytes in the buffer and its fragments.
    2712              :  */
    2713            1 : static inline size_t net_buf_frags_len(const struct net_buf *buf)
    2714              : {
    2715              :         size_t bytes = 0;
    2716              : 
    2717              :         while (buf) {
    2718              :                 bytes += buf->len;
    2719              :                 buf = buf->frags;
    2720              :         }
    2721              : 
    2722              :         return bytes;
    2723              : }
    2724              : 
    2725              : /**
    2726              :  * @}
    2727              :  */
    2728              : 
    2729              : #ifdef __cplusplus
    2730              : }
    2731              : #endif
    2732              : 
    2733              : #endif /* ZEPHYR_INCLUDE_NET_BUF_H_ */
        

Generated by: LCOV version 2.0-1