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