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 networking
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 #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 #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 struct net_buf_simple {
90 	/** Pointer to the start of data in the buffer. */
91 	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 	uint16_t len;
99 
100 	/** Amount of data that net_buf_simple#__buf can store. */
101 	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 #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  */
net_buf_simple_init(struct net_buf_simple * buf,size_t reserve_head)142 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 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  */
net_buf_simple_reset(struct net_buf_simple * buf)172 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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  */
net_buf_simple_tail(const struct net_buf_simple * buf)905 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 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 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 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 struct net_buf_simple_state {
951 	/** Offset of the data pointer from the beginning of the storage */
952 	uint16_t offset;
953 	/** Length of data */
954 	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  */
net_buf_simple_save(const struct net_buf_simple * buf,struct net_buf_simple_state * state)965 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  */
net_buf_simple_restore(struct net_buf_simple * buf,struct net_buf_simple_state * state)981 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 #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 struct net_buf {
1007 	/** Allow placing the buffer into sys_slist_t */
1008 	sys_snode_t node;
1009 
1010 	/** Fragments associated with this buffer. */
1011 	struct net_buf *frags;
1012 
1013 	/** Reference count. */
1014 	uint8_t ref;
1015 
1016 	/** Bit-field of buffer flags. */
1017 	uint8_t flags;
1018 
1019 	/** Where the buffer should go when freed up. */
1020 	uint8_t pool_id;
1021 
1022 	/** Size of user data on this buffer */
1023 	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 			uint8_t *data;
1033 
1034 			/** Length of the data behind the data pointer. */
1035 			uint16_t len;
1036 
1037 			/** Amount of data that this buffer can store. */
1038 			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 	};
1051 
1052 	/** System metadata for this buffer. */
1053 	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 };
1070 
1071 /** @endcond */
1072 
1073 /**
1074  * @brief Network buffer pool representation.
1075  *
1076  * This struct is used to represent a pool of network buffers.
1077  */
1078 struct net_buf_pool {
1079 	/** LIFO to place the buffer into when free */
1080 	struct k_lifo free;
1081 
1082 	/** To prevent concurrent access/modifications */
1083 	struct k_spinlock lock;
1084 
1085 	/** Number of buffers in pool */
1086 	const uint16_t buf_count;
1087 
1088 	/** Number of uninitialized buffers */
1089 	uint16_t uninit_count;
1090 
1091 	/** Size of user data allocated to this pool */
1092 	uint8_t user_data_size;
1093 
1094 #if defined(CONFIG_NET_BUF_POOL_USAGE)
1095 	/** Amount of available buffers in the pool. */
1096 	atomic_t avail_count;
1097 
1098 	/** Total size of the pool. */
1099 	const uint16_t pool_size;
1100 
1101 	/** Name of the pool. Used when printing pool information. */
1102 	const char *name;
1103 #endif /* CONFIG_NET_BUF_POOL_USAGE */
1104 
1105 	/** Optional destroy callback when buffer is freed. */
1106 	void (*const destroy)(struct net_buf *buf);
1107 
1108 	/** Data allocation handlers. */
1109 	const struct net_buf_data_alloc *alloc;
1110 
1111 	/** Start of buffer storage array */
1112 	struct net_buf * const __bufs;
1113 };
1114 
1115 /** @cond INTERNAL_HIDDEN */
1116 #define NET_BUF_POOL_USAGE_INIT(_pool, _count) \
1117 	IF_ENABLED(CONFIG_NET_BUF_POOL_USAGE, (.avail_count = ATOMIC_INIT(_count),)) \
1118 	IF_ENABLED(CONFIG_NET_BUF_POOL_USAGE, (.name = STRINGIFY(_pool),))
1119 
1120 #define NET_BUF_POOL_INITIALIZER(_pool, _alloc, _bufs, _count, _ud_size, _destroy) \
1121 	{                                                                          \
1122 		.free = Z_LIFO_INITIALIZER(_pool.free),                            \
1123 		.lock = { },                                                       \
1124 		.buf_count = _count,                                               \
1125 		.uninit_count = _count,                                            \
1126 		.user_data_size = _ud_size,                                        \
1127 		NET_BUF_POOL_USAGE_INIT(_pool, _count)                             \
1128 		.destroy = _destroy,                                               \
1129 		.alloc = _alloc,                                                   \
1130 		.__bufs = (struct net_buf *)_bufs,                                 \
1131 	}
1132 
1133 #define _NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size)					       \
1134 	struct _net_buf_##_name { uint8_t b[sizeof(struct net_buf)];			       \
1135 				  uint8_t ud[_ud_size]; } __net_buf_align;		       \
1136 	BUILD_ASSERT(_ud_size <= UINT8_MAX);						       \
1137 	BUILD_ASSERT(offsetof(struct net_buf, user_data) ==				       \
1138 		     offsetof(struct _net_buf_##_name, ud), "Invalid offset");		       \
1139 	BUILD_ASSERT(__alignof__(struct net_buf) ==					       \
1140 		     __alignof__(struct _net_buf_##_name), "Invalid alignment");	       \
1141 	BUILD_ASSERT(sizeof(struct _net_buf_##_name) ==					       \
1142 		     ROUND_UP(sizeof(struct net_buf) + _ud_size, __alignof__(struct net_buf)), \
1143 		     "Size cannot be determined");					       \
1144 	static struct _net_buf_##_name _net_buf_##_name[_count] __noinit
1145 
1146 extern const struct net_buf_data_alloc net_buf_heap_alloc;
1147 /** @endcond */
1148 
1149 /**
1150  *
1151  * @brief Define a new pool for buffers using the heap for the data.
1152  *
1153  * Defines a net_buf_pool struct and the necessary memory storage (array of
1154  * structs) for the needed amount of buffers. After this, the buffers can be
1155  * accessed from the pool through net_buf_alloc. The pool is defined as a
1156  * static variable, so if it needs to be exported outside the current module
1157  * this needs to happen with the help of a separate pointer rather than an
1158  * extern declaration.
1159  *
1160  * The data payload of the buffers will be allocated from the heap using
1161  * k_malloc, so CONFIG_HEAP_MEM_POOL_SIZE must be set to a positive value.
1162  * This kind of pool does not support blocking on the data allocation, so
1163  * the timeout passed to net_buf_alloc will be always treated as K_NO_WAIT
1164  * when trying to allocate the data. This means that allocation failures,
1165  * i.e. NULL returns, must always be handled cleanly.
1166  *
1167  * If provided with a custom destroy callback, this callback is
1168  * responsible for eventually calling net_buf_destroy() to complete the
1169  * process of returning the buffer to the pool.
1170  *
1171  * @param _name      Name of the pool variable.
1172  * @param _count     Number of buffers in the pool.
1173  * @param _ud_size   User data space to reserve per buffer.
1174  * @param _destroy   Optional destroy callback when buffer is freed.
1175  */
1176 #define NET_BUF_POOL_HEAP_DEFINE(_name, _count, _ud_size, _destroy)          \
1177 	_NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size);                      \
1178 	static STRUCT_SECTION_ITERABLE(net_buf_pool, _name) =                \
1179 		NET_BUF_POOL_INITIALIZER(_name, &net_buf_heap_alloc,         \
1180 					 _net_buf_##_name, _count, _ud_size, \
1181 					 _destroy)
1182 
1183 /** @cond INTERNAL_HIDDEN */
1184 
1185 struct net_buf_pool_fixed {
1186 	uint8_t *data_pool;
1187 };
1188 
1189 extern const struct net_buf_data_cb net_buf_fixed_cb;
1190 
1191 /** @endcond */
1192 
1193 /**
1194  *
1195  * @brief Define a new pool for buffers based on fixed-size data
1196  *
1197  * Defines a net_buf_pool struct and the necessary memory storage (array of
1198  * structs) for the needed amount of buffers. After this, the buffers can be
1199  * accessed from the pool through net_buf_alloc. The pool is defined as a
1200  * static variable, so if it needs to be exported outside the current module
1201  * this needs to happen with the help of a separate pointer rather than an
1202  * extern declaration.
1203  *
1204  * The data payload of the buffers will be allocated from a byte array
1205  * of fixed sized chunks. This kind of pool does not support blocking on
1206  * the data allocation, so the timeout passed to net_buf_alloc will be
1207  * always treated as K_NO_WAIT when trying to allocate the data. This means
1208  * that allocation failures, i.e. NULL returns, must always be handled
1209  * cleanly.
1210  *
1211  * If provided with a custom destroy callback, this callback is
1212  * responsible for eventually calling net_buf_destroy() to complete the
1213  * process of returning the buffer to the pool.
1214  *
1215  * @param _name      Name of the pool variable.
1216  * @param _count     Number of buffers in the pool.
1217  * @param _data_size Maximum data payload per buffer.
1218  * @param _ud_size   User data space to reserve per buffer.
1219  * @param _destroy   Optional destroy callback when buffer is freed.
1220  */
1221 #define NET_BUF_POOL_FIXED_DEFINE(_name, _count, _data_size, _ud_size, _destroy) \
1222 	_NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size);                        \
1223 	static uint8_t __noinit net_buf_data_##_name[_count][_data_size] __net_buf_align; \
1224 	static const struct net_buf_pool_fixed net_buf_fixed_##_name = {       \
1225 		.data_pool = (uint8_t *)net_buf_data_##_name,                  \
1226 	};                                                                     \
1227 	static const struct net_buf_data_alloc net_buf_fixed_alloc_##_name = { \
1228 		.cb = &net_buf_fixed_cb,                                       \
1229 		.alloc_data = (void *)&net_buf_fixed_##_name,                  \
1230 		.max_alloc_size = _data_size,                                  \
1231 	};                                                                     \
1232 	static STRUCT_SECTION_ITERABLE(net_buf_pool, _name) =                  \
1233 		NET_BUF_POOL_INITIALIZER(_name, &net_buf_fixed_alloc_##_name,  \
1234 					 _net_buf_##_name, _count, _ud_size,   \
1235 					 _destroy)
1236 
1237 /** @cond INTERNAL_HIDDEN */
1238 extern const struct net_buf_data_cb net_buf_var_cb;
1239 /** @endcond */
1240 
1241 /**
1242  *
1243  * @brief Define a new pool for buffers with variable size payloads
1244  *
1245  * Defines a net_buf_pool struct and the necessary memory storage (array of
1246  * structs) for the needed amount of buffers. After this, the buffers can be
1247  * accessed from the pool through net_buf_alloc. The pool is defined as a
1248  * static variable, so if it needs to be exported outside the current module
1249  * this needs to happen with the help of a separate pointer rather than an
1250  * extern declaration.
1251  *
1252  * The data payload of the buffers will be based on a memory pool from which
1253  * variable size payloads may be allocated.
1254  *
1255  * If provided with a custom destroy callback, this callback is
1256  * responsible for eventually calling net_buf_destroy() to complete the
1257  * process of returning the buffer to the pool.
1258  *
1259  * @param _name      Name of the pool variable.
1260  * @param _count     Number of buffers in the pool.
1261  * @param _data_size Total amount of memory available for data payloads.
1262  * @param _ud_size   User data space to reserve per buffer.
1263  * @param _destroy   Optional destroy callback when buffer is freed.
1264  */
1265 #define NET_BUF_POOL_VAR_DEFINE(_name, _count, _data_size, _ud_size, _destroy) \
1266 	_NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size);                        \
1267 	K_HEAP_DEFINE(net_buf_mem_pool_##_name, _data_size);                   \
1268 	static const struct net_buf_data_alloc net_buf_data_alloc_##_name = {  \
1269 		.cb = &net_buf_var_cb,                                         \
1270 		.alloc_data = &net_buf_mem_pool_##_name,                       \
1271 		.max_alloc_size = 0,                                           \
1272 	};                                                                     \
1273 	static STRUCT_SECTION_ITERABLE(net_buf_pool, _name) =                  \
1274 		NET_BUF_POOL_INITIALIZER(_name, &net_buf_data_alloc_##_name,   \
1275 					 _net_buf_##_name, _count, _ud_size,   \
1276 					 _destroy)
1277 
1278 /**
1279  *
1280  * @brief Define a new pool for buffers
1281  *
1282  * Defines a net_buf_pool struct and the necessary memory storage (array of
1283  * structs) for the needed amount of buffers. After this,the buffers can be
1284  * accessed from the pool through net_buf_alloc. The pool is defined as a
1285  * static variable, so if it needs to be exported outside the current module
1286  * this needs to happen with the help of a separate pointer rather than an
1287  * extern declaration.
1288  *
1289  * If provided with a custom destroy callback this callback is
1290  * responsible for eventually calling net_buf_destroy() to complete the
1291  * process of returning the buffer to the pool.
1292  *
1293  * @param _name     Name of the pool variable.
1294  * @param _count    Number of buffers in the pool.
1295  * @param _size     Maximum data size for each buffer.
1296  * @param _ud_size  Amount of user data space to reserve.
1297  * @param _destroy  Optional destroy callback when buffer is freed.
1298  */
1299 #define NET_BUF_POOL_DEFINE(_name, _count, _size, _ud_size, _destroy)        \
1300 	NET_BUF_POOL_FIXED_DEFINE(_name, _count, _size, _ud_size, _destroy)
1301 
1302 /**
1303  * @brief Looks up a pool based on its ID.
1304  *
1305  * @param id Pool ID (e.g. from buf->pool_id).
1306  *
1307  * @return Pointer to pool.
1308  */
1309 struct net_buf_pool *net_buf_pool_get(int id);
1310 
1311 /**
1312  * @brief Get a zero-based index for a buffer.
1313  *
1314  * This function will translate a buffer into a zero-based index,
1315  * based on its placement in its buffer pool. This can be useful if you
1316  * want to associate an external array of meta-data contexts with the
1317  * buffers of a pool.
1318  *
1319  * @param buf  Network buffer.
1320  *
1321  * @return Zero-based index for the buffer.
1322  */
1323 int net_buf_id(const struct net_buf *buf);
1324 
1325 /**
1326  * @brief Allocate a new fixed buffer from a pool.
1327  *
1328  * @note Some types of data allocators do not support
1329  *       blocking (such as the HEAP type). In this case it's still possible
1330  *       for net_buf_alloc() to fail (return NULL) even if it was given
1331  *       K_FOREVER.
1332  *
1333  * @note The timeout value will be overridden to K_NO_WAIT if called from the
1334  *       system workqueue.
1335  *
1336  * @param pool Which pool to allocate the buffer from.
1337  * @param timeout Affects the action taken should the pool be empty.
1338  *        If K_NO_WAIT, then return immediately. If K_FOREVER, then
1339  *        wait as long as necessary. Otherwise, wait until the specified
1340  *        timeout.
1341  *
1342  * @return New buffer or NULL if out of buffers.
1343  */
1344 #if defined(CONFIG_NET_BUF_LOG)
1345 struct net_buf * __must_check net_buf_alloc_fixed_debug(struct net_buf_pool *pool,
1346 							k_timeout_t timeout,
1347 							const char *func,
1348 							int line);
1349 #define net_buf_alloc_fixed(_pool, _timeout) \
1350 	net_buf_alloc_fixed_debug(_pool, _timeout, __func__, __LINE__)
1351 #else
1352 struct net_buf * __must_check net_buf_alloc_fixed(struct net_buf_pool *pool,
1353 						  k_timeout_t timeout);
1354 #endif
1355 
1356 /**
1357  * @copydetails net_buf_alloc_fixed
1358  */
net_buf_alloc(struct net_buf_pool * pool,k_timeout_t timeout)1359 static inline struct net_buf * __must_check net_buf_alloc(struct net_buf_pool *pool,
1360 							  k_timeout_t timeout)
1361 {
1362 	return net_buf_alloc_fixed(pool, timeout);
1363 }
1364 
1365 /**
1366  * @brief Allocate a new variable length buffer from a pool.
1367  *
1368  * @note Some types of data allocators do not support
1369  *       blocking (such as the HEAP type). In this case it's still possible
1370  *       for net_buf_alloc() to fail (return NULL) even if it was given
1371  *       K_FOREVER.
1372  *
1373  * @note The timeout value will be overridden to K_NO_WAIT if called from the
1374  *       system workqueue.
1375  *
1376  * @param pool Which pool to allocate the buffer from.
1377  * @param size Amount of data the buffer must be able to fit.
1378  * @param timeout Affects the action taken should the pool be empty.
1379  *        If K_NO_WAIT, then return immediately. If K_FOREVER, then
1380  *        wait as long as necessary. Otherwise, wait until the specified
1381  *        timeout.
1382  *
1383  * @return New buffer or NULL if out of buffers.
1384  */
1385 #if defined(CONFIG_NET_BUF_LOG)
1386 struct net_buf * __must_check net_buf_alloc_len_debug(struct net_buf_pool *pool,
1387 						      size_t size,
1388 						      k_timeout_t timeout,
1389 						      const char *func,
1390 						      int line);
1391 #define net_buf_alloc_len(_pool, _size, _timeout) \
1392 	net_buf_alloc_len_debug(_pool, _size, _timeout, __func__, __LINE__)
1393 #else
1394 struct net_buf * __must_check net_buf_alloc_len(struct net_buf_pool *pool,
1395 						size_t size,
1396 						k_timeout_t timeout);
1397 #endif
1398 
1399 /**
1400  * @brief Allocate a new buffer from a pool but with external data pointer.
1401  *
1402  * Allocate a new buffer from a pool, where the data pointer comes from the
1403  * user and not from the pool.
1404  *
1405  * @note Some types of data allocators do not support
1406  *       blocking (such as the HEAP type). In this case it's still possible
1407  *       for net_buf_alloc() to fail (return NULL) even if it was given
1408  *       K_FOREVER.
1409  *
1410  * @note The timeout value will be overridden to K_NO_WAIT if called from the
1411  *       system workqueue.
1412  *
1413  * @param pool Which pool to allocate the buffer from.
1414  * @param data External data pointer
1415  * @param size Amount of data the pointed data buffer if able to fit.
1416  * @param timeout Affects the action taken should the pool be empty.
1417  *        If K_NO_WAIT, then return immediately. If K_FOREVER, then
1418  *        wait as long as necessary. Otherwise, wait until the specified
1419  *        timeout.
1420  *
1421  * @return New buffer or NULL if out of buffers.
1422  */
1423 #if defined(CONFIG_NET_BUF_LOG)
1424 struct net_buf * __must_check net_buf_alloc_with_data_debug(struct net_buf_pool *pool,
1425 							    void *data, size_t size,
1426 							    k_timeout_t timeout,
1427 							    const char *func, int line);
1428 #define net_buf_alloc_with_data(_pool, _data_, _size, _timeout)		\
1429 	net_buf_alloc_with_data_debug(_pool, _data_, _size, _timeout,	\
1430 				      __func__, __LINE__)
1431 #else
1432 struct net_buf * __must_check net_buf_alloc_with_data(struct net_buf_pool *pool,
1433 						      void *data, size_t size,
1434 						      k_timeout_t timeout);
1435 #endif
1436 
1437 /**
1438  * @brief Get a buffer from a FIFO.
1439  *
1440  * This function is NOT thread-safe if the buffers in the FIFO contain
1441  * fragments.
1442  *
1443  * @param fifo Which FIFO to take the buffer from.
1444  * @param timeout Affects the action taken should the FIFO be empty.
1445  *        If K_NO_WAIT, then return immediately. If K_FOREVER, then wait as
1446  *        long as necessary. Otherwise, wait until the specified timeout.
1447  *
1448  * @return New buffer or NULL if the FIFO is empty.
1449  */
1450 #if defined(CONFIG_NET_BUF_LOG)
1451 struct net_buf * __must_check net_buf_get_debug(struct k_fifo *fifo,
1452 						k_timeout_t timeout,
1453 						const char *func, int line);
1454 #define	net_buf_get(_fifo, _timeout) \
1455 	net_buf_get_debug(_fifo, _timeout, __func__, __LINE__)
1456 #else
1457 struct net_buf * __must_check net_buf_get(struct k_fifo *fifo,
1458 					  k_timeout_t timeout);
1459 #endif
1460 
1461 /**
1462  * @brief Destroy buffer from custom destroy callback
1463  *
1464  * This helper is only intended to be used from custom destroy callbacks.
1465  * If no custom destroy callback is given to NET_BUF_POOL_*_DEFINE() then
1466  * there is no need to use this API.
1467  *
1468  * @param buf Buffer to destroy.
1469  */
net_buf_destroy(struct net_buf * buf)1470 static inline void net_buf_destroy(struct net_buf *buf)
1471 {
1472 	struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
1473 
1474 	if (buf->__buf) {
1475 		if (!(buf->flags & NET_BUF_EXTERNAL_DATA)) {
1476 			pool->alloc->cb->unref(buf, buf->__buf);
1477 		}
1478 		buf->__buf = NULL;
1479 	}
1480 
1481 	k_lifo_put(&pool->free, buf);
1482 }
1483 
1484 /**
1485  * @brief Reset buffer
1486  *
1487  * Reset buffer data and flags so it can be reused for other purposes.
1488  *
1489  * @param buf Buffer to reset.
1490  */
1491 void net_buf_reset(struct net_buf *buf);
1492 
1493 /**
1494  * @brief Initialize buffer with the given headroom.
1495  *
1496  * The buffer is not expected to contain any data when this API is called.
1497  *
1498  * @param buf Buffer to initialize.
1499  * @param reserve How much headroom to reserve.
1500  */
1501 void net_buf_simple_reserve(struct net_buf_simple *buf, size_t reserve);
1502 
1503 /**
1504  * @brief Put a buffer into a list
1505  *
1506  * If the buffer contains follow-up fragments this function will take care of
1507  * inserting them as well into the list.
1508  *
1509  * @param list Which list to append the buffer to.
1510  * @param buf Buffer.
1511  */
1512 void net_buf_slist_put(sys_slist_t *list, struct net_buf *buf);
1513 
1514 /**
1515  * @brief Get a buffer from a list.
1516  *
1517  * If the buffer had any fragments, these will automatically be recovered from
1518  * the list as well and be placed to the buffer's fragment list.
1519  *
1520  * @param list Which list to take the buffer from.
1521  *
1522  * @return New buffer or NULL if the FIFO is empty.
1523  */
1524 struct net_buf * __must_check net_buf_slist_get(sys_slist_t *list);
1525 
1526 /**
1527  * @brief Put a buffer to the end of a FIFO.
1528  *
1529  * If the buffer contains follow-up fragments this function will take care of
1530  * inserting them as well into the FIFO.
1531  *
1532  * @param fifo Which FIFO to put the buffer to.
1533  * @param buf Buffer.
1534  */
1535 void net_buf_put(struct k_fifo *fifo, struct net_buf *buf);
1536 
1537 /**
1538  * @brief Decrements the reference count of a buffer.
1539  *
1540  * The buffer is put back into the pool if the reference count reaches zero.
1541  *
1542  * @param buf A valid pointer on a buffer
1543  */
1544 #if defined(CONFIG_NET_BUF_LOG)
1545 void net_buf_unref_debug(struct net_buf *buf, const char *func, int line);
1546 #define	net_buf_unref(_buf) \
1547 	net_buf_unref_debug(_buf, __func__, __LINE__)
1548 #else
1549 void net_buf_unref(struct net_buf *buf);
1550 #endif
1551 
1552 /**
1553  * @brief Increment the reference count of a buffer.
1554  *
1555  * @param buf A valid pointer on a buffer
1556  *
1557  * @return the buffer newly referenced
1558  */
1559 struct net_buf * __must_check net_buf_ref(struct net_buf *buf);
1560 
1561 /**
1562  * @brief Clone buffer
1563  *
1564  * Duplicate given buffer including any (user) data and headers currently stored.
1565  *
1566  * @param buf A valid pointer on a buffer
1567  * @param timeout Affects the action taken should the pool be empty.
1568  *        If K_NO_WAIT, then return immediately. If K_FOREVER, then
1569  *        wait as long as necessary. Otherwise, wait until the specified
1570  *        timeout.
1571  *
1572  * @return Cloned buffer or NULL if out of buffers.
1573  */
1574 struct net_buf * __must_check net_buf_clone(struct net_buf *buf,
1575 					    k_timeout_t timeout);
1576 
1577 /**
1578  * @brief Get a pointer to the user data of a buffer.
1579  *
1580  * @param buf A valid pointer on a buffer
1581  *
1582  * @return Pointer to the user data of the buffer.
1583  */
net_buf_user_data(const struct net_buf * buf)1584 static inline void * __must_check net_buf_user_data(const struct net_buf *buf)
1585 {
1586 	return (void *)buf->user_data;
1587 }
1588 
1589 /**
1590  * @brief Copy user data from one to another buffer.
1591  *
1592  * @param dst A valid pointer to a buffer gettings its user data overwritten.
1593  * @param src A valid pointer to a buffer gettings its user data copied. User data size must be
1594  *            equal to or exceed @a dst.
1595  *
1596  * @return 0 on success or negative error number on failure.
1597  */
1598 int net_buf_user_data_copy(struct net_buf *dst, const struct net_buf *src);
1599 
1600 /**
1601  * @brief Initialize buffer with the given headroom.
1602  *
1603  * The buffer is not expected to contain any data when this API is called.
1604  *
1605  * @param buf Buffer to initialize.
1606  * @param reserve How much headroom to reserve.
1607  */
net_buf_reserve(struct net_buf * buf,size_t reserve)1608 static inline void net_buf_reserve(struct net_buf *buf, size_t reserve)
1609 {
1610 	net_buf_simple_reserve(&buf->b, reserve);
1611 }
1612 
1613 /**
1614  * @brief Prepare data to be added at the end of the buffer
1615  *
1616  * Increments the data length of a buffer to account for more data
1617  * at the end.
1618  *
1619  * @param buf Buffer to update.
1620  * @param len Number of bytes to increment the length with.
1621  *
1622  * @return The original tail of the buffer.
1623  */
net_buf_add(struct net_buf * buf,size_t len)1624 static inline void *net_buf_add(struct net_buf *buf, size_t len)
1625 {
1626 	return net_buf_simple_add(&buf->b, len);
1627 }
1628 
1629 /**
1630  * @brief Copies the given number of bytes to the end of the buffer
1631  *
1632  * Increments the data length of the  buffer to account for more data at
1633  * the end.
1634  *
1635  * @param buf Buffer to update.
1636  * @param mem Location of data to be added.
1637  * @param len Length of data to be added
1638  *
1639  * @return The original tail of the buffer.
1640  */
net_buf_add_mem(struct net_buf * buf,const void * mem,size_t len)1641 static inline void *net_buf_add_mem(struct net_buf *buf, const void *mem,
1642 				    size_t len)
1643 {
1644 	return net_buf_simple_add_mem(&buf->b, mem, len);
1645 }
1646 
1647 /**
1648  * @brief Add (8-bit) byte at the end of the buffer
1649  *
1650  * Increments the data length of the  buffer to account for more data at
1651  * the end.
1652  *
1653  * @param buf Buffer to update.
1654  * @param val byte value to be added.
1655  *
1656  * @return Pointer to the value added
1657  */
net_buf_add_u8(struct net_buf * buf,uint8_t val)1658 static inline uint8_t *net_buf_add_u8(struct net_buf *buf, uint8_t val)
1659 {
1660 	return net_buf_simple_add_u8(&buf->b, val);
1661 }
1662 
1663 /**
1664  * @brief Add 16-bit value at the end of the buffer
1665  *
1666  * Adds 16-bit value in little endian format at the end of buffer.
1667  * Increments the data length of a buffer to account for more data
1668  * at the end.
1669  *
1670  * @param buf Buffer to update.
1671  * @param val 16-bit value to be added.
1672  */
net_buf_add_le16(struct net_buf * buf,uint16_t val)1673 static inline void net_buf_add_le16(struct net_buf *buf, uint16_t val)
1674 {
1675 	net_buf_simple_add_le16(&buf->b, val);
1676 }
1677 
1678 /**
1679  * @brief Add 16-bit value at the end of the buffer
1680  *
1681  * Adds 16-bit value in big endian format at the end of buffer.
1682  * Increments the data length of a buffer to account for more data
1683  * at the end.
1684  *
1685  * @param buf Buffer to update.
1686  * @param val 16-bit value to be added.
1687  */
net_buf_add_be16(struct net_buf * buf,uint16_t val)1688 static inline void net_buf_add_be16(struct net_buf *buf, uint16_t val)
1689 {
1690 	net_buf_simple_add_be16(&buf->b, val);
1691 }
1692 
1693 /**
1694  * @brief Add 24-bit value at the end of the buffer
1695  *
1696  * Adds 24-bit value in little endian format at the end of buffer.
1697  * Increments the data length of a buffer to account for more data
1698  * at the end.
1699  *
1700  * @param buf Buffer to update.
1701  * @param val 24-bit value to be added.
1702  */
net_buf_add_le24(struct net_buf * buf,uint32_t val)1703 static inline void net_buf_add_le24(struct net_buf *buf, uint32_t val)
1704 {
1705 	net_buf_simple_add_le24(&buf->b, val);
1706 }
1707 
1708 /**
1709  * @brief Add 24-bit value at the end of the buffer
1710  *
1711  * Adds 24-bit value in big endian format at the end of buffer.
1712  * Increments the data length of a buffer to account for more data
1713  * at the end.
1714  *
1715  * @param buf Buffer to update.
1716  * @param val 24-bit value to be added.
1717  */
net_buf_add_be24(struct net_buf * buf,uint32_t val)1718 static inline void net_buf_add_be24(struct net_buf *buf, uint32_t val)
1719 {
1720 	net_buf_simple_add_be24(&buf->b, val);
1721 }
1722 
1723 /**
1724  * @brief Add 32-bit value at the end of the buffer
1725  *
1726  * Adds 32-bit value in little endian format at the end of buffer.
1727  * Increments the data length of a buffer to account for more data
1728  * at the end.
1729  *
1730  * @param buf Buffer to update.
1731  * @param val 32-bit value to be added.
1732  */
net_buf_add_le32(struct net_buf * buf,uint32_t val)1733 static inline void net_buf_add_le32(struct net_buf *buf, uint32_t val)
1734 {
1735 	net_buf_simple_add_le32(&buf->b, val);
1736 }
1737 
1738 /**
1739  * @brief Add 32-bit value at the end of the buffer
1740  *
1741  * Adds 32-bit value in big endian format at the end of buffer.
1742  * Increments the data length of a buffer to account for more data
1743  * at the end.
1744  *
1745  * @param buf Buffer to update.
1746  * @param val 32-bit value to be added.
1747  */
net_buf_add_be32(struct net_buf * buf,uint32_t val)1748 static inline void net_buf_add_be32(struct net_buf *buf, uint32_t val)
1749 {
1750 	net_buf_simple_add_be32(&buf->b, val);
1751 }
1752 
1753 /**
1754  * @brief Add 40-bit value at the end of the buffer
1755  *
1756  * Adds 40-bit value in little endian format at the end of buffer.
1757  * Increments the data length of a buffer to account for more data
1758  * at the end.
1759  *
1760  * @param buf Buffer to update.
1761  * @param val 40-bit value to be added.
1762  */
net_buf_add_le40(struct net_buf * buf,uint64_t val)1763 static inline void net_buf_add_le40(struct net_buf *buf, uint64_t val)
1764 {
1765 	net_buf_simple_add_le40(&buf->b, val);
1766 }
1767 
1768 /**
1769  * @brief Add 40-bit value at the end of the buffer
1770  *
1771  * Adds 40-bit value in big endian format at the end of buffer.
1772  * Increments the data length of a buffer to account for more data
1773  * at the end.
1774  *
1775  * @param buf Buffer to update.
1776  * @param val 40-bit value to be added.
1777  */
net_buf_add_be40(struct net_buf * buf,uint64_t val)1778 static inline void net_buf_add_be40(struct net_buf *buf, uint64_t val)
1779 {
1780 	net_buf_simple_add_be40(&buf->b, val);
1781 }
1782 
1783 /**
1784  * @brief Add 48-bit value at the end of the buffer
1785  *
1786  * Adds 48-bit value in little endian format at the end of buffer.
1787  * Increments the data length of a buffer to account for more data
1788  * at the end.
1789  *
1790  * @param buf Buffer to update.
1791  * @param val 48-bit value to be added.
1792  */
net_buf_add_le48(struct net_buf * buf,uint64_t val)1793 static inline void net_buf_add_le48(struct net_buf *buf, uint64_t val)
1794 {
1795 	net_buf_simple_add_le48(&buf->b, val);
1796 }
1797 
1798 /**
1799  * @brief Add 48-bit value at the end of the buffer
1800  *
1801  * Adds 48-bit value in big endian format at the end of buffer.
1802  * Increments the data length of a buffer to account for more data
1803  * at the end.
1804  *
1805  * @param buf Buffer to update.
1806  * @param val 48-bit value to be added.
1807  */
net_buf_add_be48(struct net_buf * buf,uint64_t val)1808 static inline void net_buf_add_be48(struct net_buf *buf, uint64_t val)
1809 {
1810 	net_buf_simple_add_be48(&buf->b, val);
1811 }
1812 
1813 /**
1814  * @brief Add 64-bit value at the end of the buffer
1815  *
1816  * Adds 64-bit value in little endian format at the end of buffer.
1817  * Increments the data length of a buffer to account for more data
1818  * at the end.
1819  *
1820  * @param buf Buffer to update.
1821  * @param val 64-bit value to be added.
1822  */
net_buf_add_le64(struct net_buf * buf,uint64_t val)1823 static inline void net_buf_add_le64(struct net_buf *buf, uint64_t val)
1824 {
1825 	net_buf_simple_add_le64(&buf->b, val);
1826 }
1827 
1828 /**
1829  * @brief Add 64-bit value at the end of the buffer
1830  *
1831  * Adds 64-bit value in big endian format at the end of buffer.
1832  * Increments the data length of a buffer to account for more data
1833  * at the end.
1834  *
1835  * @param buf Buffer to update.
1836  * @param val 64-bit value to be added.
1837  */
net_buf_add_be64(struct net_buf * buf,uint64_t val)1838 static inline void net_buf_add_be64(struct net_buf *buf, uint64_t val)
1839 {
1840 	net_buf_simple_add_be64(&buf->b, val);
1841 }
1842 
1843 /**
1844  * @brief Remove data from the end of the buffer.
1845  *
1846  * Removes data from the end of the buffer by modifying the buffer length.
1847  *
1848  * @param buf Buffer to update.
1849  * @param len Number of bytes to remove.
1850  *
1851  * @return New end of the buffer data.
1852  */
net_buf_remove_mem(struct net_buf * buf,size_t len)1853 static inline void *net_buf_remove_mem(struct net_buf *buf, size_t len)
1854 {
1855 	return net_buf_simple_remove_mem(&buf->b, len);
1856 }
1857 
1858 /**
1859  * @brief Remove a 8-bit value from the end of the buffer
1860  *
1861  * Same idea as with net_buf_remove_mem(), but a helper for operating on
1862  * 8-bit values.
1863  *
1864  * @param buf A valid pointer on a buffer.
1865  *
1866  * @return The 8-bit removed value
1867  */
net_buf_remove_u8(struct net_buf * buf)1868 static inline uint8_t net_buf_remove_u8(struct net_buf *buf)
1869 {
1870 	return net_buf_simple_remove_u8(&buf->b);
1871 }
1872 
1873 /**
1874  * @brief Remove and convert 16 bits from the end of the buffer.
1875  *
1876  * Same idea as with net_buf_remove_mem(), but a helper for operating on
1877  * 16-bit little endian data.
1878  *
1879  * @param buf A valid pointer on a buffer.
1880  *
1881  * @return 16-bit value converted from little endian to host endian.
1882  */
net_buf_remove_le16(struct net_buf * buf)1883 static inline uint16_t net_buf_remove_le16(struct net_buf *buf)
1884 {
1885 	return net_buf_simple_remove_le16(&buf->b);
1886 }
1887 
1888 /**
1889  * @brief Remove and convert 16 bits from the end of the buffer.
1890  *
1891  * Same idea as with net_buf_remove_mem(), but a helper for operating on
1892  * 16-bit big endian data.
1893  *
1894  * @param buf A valid pointer on a buffer.
1895  *
1896  * @return 16-bit value converted from big endian to host endian.
1897  */
net_buf_remove_be16(struct net_buf * buf)1898 static inline uint16_t net_buf_remove_be16(struct net_buf *buf)
1899 {
1900 	return net_buf_simple_remove_be16(&buf->b);
1901 }
1902 
1903 /**
1904  * @brief Remove and convert 24 bits from the end of the buffer.
1905  *
1906  * Same idea as with net_buf_remove_mem(), but a helper for operating on
1907  * 24-bit big endian data.
1908  *
1909  * @param buf A valid pointer on a buffer.
1910  *
1911  * @return 24-bit value converted from big endian to host endian.
1912  */
net_buf_remove_be24(struct net_buf * buf)1913 static inline uint32_t net_buf_remove_be24(struct net_buf *buf)
1914 {
1915 	return net_buf_simple_remove_be24(&buf->b);
1916 }
1917 
1918 /**
1919  * @brief Remove and convert 24 bits from the end of the buffer.
1920  *
1921  * Same idea as with net_buf_remove_mem(), but a helper for operating on
1922  * 24-bit little endian data.
1923  *
1924  * @param buf A valid pointer on a buffer.
1925  *
1926  * @return 24-bit value converted from little endian to host endian.
1927  */
net_buf_remove_le24(struct net_buf * buf)1928 static inline uint32_t net_buf_remove_le24(struct net_buf *buf)
1929 {
1930 	return net_buf_simple_remove_le24(&buf->b);
1931 }
1932 
1933 /**
1934  * @brief Remove and convert 32 bits from the end of the buffer.
1935  *
1936  * Same idea as with net_buf_remove_mem(), but a helper for operating on
1937  * 32-bit little endian data.
1938  *
1939  * @param buf A valid pointer on a buffer.
1940  *
1941  * @return 32-bit value converted from little endian to host endian.
1942  */
net_buf_remove_le32(struct net_buf * buf)1943 static inline uint32_t net_buf_remove_le32(struct net_buf *buf)
1944 {
1945 	return net_buf_simple_remove_le32(&buf->b);
1946 }
1947 
1948 /**
1949  * @brief Remove and convert 32 bits from the end of the buffer.
1950  *
1951  * Same idea as with net_buf_remove_mem(), but a helper for operating on
1952  * 32-bit big endian data.
1953  *
1954  * @param buf A valid pointer on a buffer
1955  *
1956  * @return 32-bit value converted from big endian to host endian.
1957  */
net_buf_remove_be32(struct net_buf * buf)1958 static inline uint32_t net_buf_remove_be32(struct net_buf *buf)
1959 {
1960 	return net_buf_simple_remove_be32(&buf->b);
1961 }
1962 
1963 /**
1964  * @brief Remove and convert 40 bits from the end of the buffer.
1965  *
1966  * Same idea as with net_buf_remove_mem(), but a helper for operating on
1967  * 40-bit little endian data.
1968  *
1969  * @param buf A valid pointer on a buffer.
1970  *
1971  * @return 40-bit value converted from little endian to host endian.
1972  */
net_buf_remove_le40(struct net_buf * buf)1973 static inline uint64_t net_buf_remove_le40(struct net_buf *buf)
1974 {
1975 	return net_buf_simple_remove_le40(&buf->b);
1976 }
1977 
1978 /**
1979  * @brief Remove and convert 40 bits from the end of the buffer.
1980  *
1981  * Same idea as with net_buf_remove_mem(), but a helper for operating on
1982  * 40-bit big endian data.
1983  *
1984  * @param buf A valid pointer on a buffer
1985  *
1986  * @return 40-bit value converted from big endian to host endian.
1987  */
net_buf_remove_be40(struct net_buf * buf)1988 static inline uint64_t net_buf_remove_be40(struct net_buf *buf)
1989 {
1990 	return net_buf_simple_remove_be40(&buf->b);
1991 }
1992 
1993 /**
1994  * @brief Remove and convert 48 bits from the end of the buffer.
1995  *
1996  * Same idea as with net_buf_remove_mem(), but a helper for operating on
1997  * 48-bit little endian data.
1998  *
1999  * @param buf A valid pointer on a buffer.
2000  *
2001  * @return 48-bit value converted from little endian to host endian.
2002  */
net_buf_remove_le48(struct net_buf * buf)2003 static inline uint64_t net_buf_remove_le48(struct net_buf *buf)
2004 {
2005 	return net_buf_simple_remove_le48(&buf->b);
2006 }
2007 
2008 /**
2009  * @brief Remove and convert 48 bits from the end of the buffer.
2010  *
2011  * Same idea as with net_buf_remove_mem(), but a helper for operating on
2012  * 48-bit big endian data.
2013  *
2014  * @param buf A valid pointer on a buffer
2015  *
2016  * @return 48-bit value converted from big endian to host endian.
2017  */
net_buf_remove_be48(struct net_buf * buf)2018 static inline uint64_t net_buf_remove_be48(struct net_buf *buf)
2019 {
2020 	return net_buf_simple_remove_be48(&buf->b);
2021 }
2022 
2023 /**
2024  * @brief Remove and convert 64 bits from the end of the buffer.
2025  *
2026  * Same idea as with net_buf_remove_mem(), but a helper for operating on
2027  * 64-bit little endian data.
2028  *
2029  * @param buf A valid pointer on a buffer.
2030  *
2031  * @return 64-bit value converted from little endian to host endian.
2032  */
net_buf_remove_le64(struct net_buf * buf)2033 static inline uint64_t net_buf_remove_le64(struct net_buf *buf)
2034 {
2035 	return net_buf_simple_remove_le64(&buf->b);
2036 }
2037 
2038 /**
2039  * @brief Remove and convert 64 bits from the end of the buffer.
2040  *
2041  * Same idea as with net_buf_remove_mem(), but a helper for operating on
2042  * 64-bit big endian data.
2043  *
2044  * @param buf A valid pointer on a buffer
2045  *
2046  * @return 64-bit value converted from big endian to host endian.
2047  */
net_buf_remove_be64(struct net_buf * buf)2048 static inline uint64_t net_buf_remove_be64(struct net_buf *buf)
2049 {
2050 	return net_buf_simple_remove_be64(&buf->b);
2051 }
2052 
2053 /**
2054  * @brief Prepare data to be added at the start of the buffer
2055  *
2056  * Modifies the data pointer and buffer length to account for more data
2057  * in the beginning of the buffer.
2058  *
2059  * @param buf Buffer to update.
2060  * @param len Number of bytes to add to the beginning.
2061  *
2062  * @return The new beginning of the buffer data.
2063  */
net_buf_push(struct net_buf * buf,size_t len)2064 static inline void *net_buf_push(struct net_buf *buf, size_t len)
2065 {
2066 	return net_buf_simple_push(&buf->b, len);
2067 }
2068 
2069 /**
2070  * @brief Copies the given number of bytes to the start of the buffer
2071  *
2072  * Modifies the data pointer and buffer length to account for more data
2073  * in the beginning of the buffer.
2074  *
2075  * @param buf Buffer to update.
2076  * @param mem Location of data to be added.
2077  * @param len Length of data to be added.
2078  *
2079  * @return The new beginning of the buffer data.
2080  */
net_buf_push_mem(struct net_buf * buf,const void * mem,size_t len)2081 static inline void *net_buf_push_mem(struct net_buf *buf, const void *mem,
2082 				     size_t len)
2083 {
2084 	return net_buf_simple_push_mem(&buf->b, mem, len);
2085 }
2086 
2087 /**
2088  * @brief Push 8-bit value to the beginning of the buffer
2089  *
2090  * Adds 8-bit value the beginning of the buffer.
2091  *
2092  * @param buf Buffer to update.
2093  * @param val 8-bit value to be pushed to the buffer.
2094  */
net_buf_push_u8(struct net_buf * buf,uint8_t val)2095 static inline void net_buf_push_u8(struct net_buf *buf, uint8_t val)
2096 {
2097 	net_buf_simple_push_u8(&buf->b, val);
2098 }
2099 
2100 /**
2101  * @brief Push 16-bit value to the beginning of the buffer
2102  *
2103  * Adds 16-bit value in little endian format to the beginning of the
2104  * buffer.
2105  *
2106  * @param buf Buffer to update.
2107  * @param val 16-bit value to be pushed to the buffer.
2108  */
net_buf_push_le16(struct net_buf * buf,uint16_t val)2109 static inline void net_buf_push_le16(struct net_buf *buf, uint16_t val)
2110 {
2111 	net_buf_simple_push_le16(&buf->b, val);
2112 }
2113 
2114 /**
2115  * @brief Push 16-bit value to the beginning of the buffer
2116  *
2117  * Adds 16-bit value in big endian format to the beginning of the
2118  * buffer.
2119  *
2120  * @param buf Buffer to update.
2121  * @param val 16-bit value to be pushed to the buffer.
2122  */
net_buf_push_be16(struct net_buf * buf,uint16_t val)2123 static inline void net_buf_push_be16(struct net_buf *buf, uint16_t val)
2124 {
2125 	net_buf_simple_push_be16(&buf->b, val);
2126 }
2127 
2128 /**
2129  * @brief Push 24-bit value to the beginning of the buffer
2130  *
2131  * Adds 24-bit value in little endian format to the beginning of the
2132  * buffer.
2133  *
2134  * @param buf Buffer to update.
2135  * @param val 24-bit value to be pushed to the buffer.
2136  */
net_buf_push_le24(struct net_buf * buf,uint32_t val)2137 static inline void net_buf_push_le24(struct net_buf *buf, uint32_t val)
2138 {
2139 	net_buf_simple_push_le24(&buf->b, val);
2140 }
2141 
2142 /**
2143  * @brief Push 24-bit value to the beginning of the buffer
2144  *
2145  * Adds 24-bit value in big endian format to the beginning of the
2146  * buffer.
2147  *
2148  * @param buf Buffer to update.
2149  * @param val 24-bit value to be pushed to the buffer.
2150  */
net_buf_push_be24(struct net_buf * buf,uint32_t val)2151 static inline void net_buf_push_be24(struct net_buf *buf, uint32_t val)
2152 {
2153 	net_buf_simple_push_be24(&buf->b, val);
2154 }
2155 
2156 /**
2157  * @brief Push 32-bit value to the beginning of the buffer
2158  *
2159  * Adds 32-bit value in little endian format to the beginning of the
2160  * buffer.
2161  *
2162  * @param buf Buffer to update.
2163  * @param val 32-bit value to be pushed to the buffer.
2164  */
net_buf_push_le32(struct net_buf * buf,uint32_t val)2165 static inline void net_buf_push_le32(struct net_buf *buf, uint32_t val)
2166 {
2167 	net_buf_simple_push_le32(&buf->b, val);
2168 }
2169 
2170 /**
2171  * @brief Push 32-bit value to the beginning of the buffer
2172  *
2173  * Adds 32-bit value in big endian format to the beginning of the
2174  * buffer.
2175  *
2176  * @param buf Buffer to update.
2177  * @param val 32-bit value to be pushed to the buffer.
2178  */
net_buf_push_be32(struct net_buf * buf,uint32_t val)2179 static inline void net_buf_push_be32(struct net_buf *buf, uint32_t val)
2180 {
2181 	net_buf_simple_push_be32(&buf->b, val);
2182 }
2183 
2184 /**
2185  * @brief Push 40-bit value to the beginning of the buffer
2186  *
2187  * Adds 40-bit value in little endian format to the beginning of the
2188  * buffer.
2189  *
2190  * @param buf Buffer to update.
2191  * @param val 40-bit value to be pushed to the buffer.
2192  */
net_buf_push_le40(struct net_buf * buf,uint64_t val)2193 static inline void net_buf_push_le40(struct net_buf *buf, uint64_t val)
2194 {
2195 	net_buf_simple_push_le40(&buf->b, val);
2196 }
2197 
2198 /**
2199  * @brief Push 40-bit value to the beginning of the buffer
2200  *
2201  * Adds 40-bit value in big endian format to the beginning of the
2202  * buffer.
2203  *
2204  * @param buf Buffer to update.
2205  * @param val 40-bit value to be pushed to the buffer.
2206  */
net_buf_push_be40(struct net_buf * buf,uint64_t val)2207 static inline void net_buf_push_be40(struct net_buf *buf, uint64_t val)
2208 {
2209 	net_buf_simple_push_be40(&buf->b, val);
2210 }
2211 
2212 /**
2213  * @brief Push 48-bit value to the beginning of the buffer
2214  *
2215  * Adds 48-bit value in little endian format to the beginning of the
2216  * buffer.
2217  *
2218  * @param buf Buffer to update.
2219  * @param val 48-bit value to be pushed to the buffer.
2220  */
net_buf_push_le48(struct net_buf * buf,uint64_t val)2221 static inline void net_buf_push_le48(struct net_buf *buf, uint64_t val)
2222 {
2223 	net_buf_simple_push_le48(&buf->b, val);
2224 }
2225 
2226 /**
2227  * @brief Push 48-bit value to the beginning of the buffer
2228  *
2229  * Adds 48-bit value in big endian format to the beginning of the
2230  * buffer.
2231  *
2232  * @param buf Buffer to update.
2233  * @param val 48-bit value to be pushed to the buffer.
2234  */
net_buf_push_be48(struct net_buf * buf,uint64_t val)2235 static inline void net_buf_push_be48(struct net_buf *buf, uint64_t val)
2236 {
2237 	net_buf_simple_push_be48(&buf->b, val);
2238 }
2239 
2240 /**
2241  * @brief Push 64-bit value to the beginning of the buffer
2242  *
2243  * Adds 64-bit value in little endian format to the beginning of the
2244  * buffer.
2245  *
2246  * @param buf Buffer to update.
2247  * @param val 64-bit value to be pushed to the buffer.
2248  */
net_buf_push_le64(struct net_buf * buf,uint64_t val)2249 static inline void net_buf_push_le64(struct net_buf *buf, uint64_t val)
2250 {
2251 	net_buf_simple_push_le64(&buf->b, val);
2252 }
2253 
2254 /**
2255  * @brief Push 64-bit value to the beginning of the buffer
2256  *
2257  * Adds 64-bit value in big endian format to the beginning of the
2258  * buffer.
2259  *
2260  * @param buf Buffer to update.
2261  * @param val 64-bit value to be pushed to the buffer.
2262  */
net_buf_push_be64(struct net_buf * buf,uint64_t val)2263 static inline void net_buf_push_be64(struct net_buf *buf, uint64_t val)
2264 {
2265 	net_buf_simple_push_be64(&buf->b, val);
2266 }
2267 
2268 /**
2269  * @brief Remove data from the beginning of the buffer.
2270  *
2271  * Removes data from the beginning of the buffer by modifying the data
2272  * pointer and buffer length.
2273  *
2274  * @param buf Buffer to update.
2275  * @param len Number of bytes to remove.
2276  *
2277  * @return New beginning of the buffer data.
2278  */
net_buf_pull(struct net_buf * buf,size_t len)2279 static inline void *net_buf_pull(struct net_buf *buf, size_t len)
2280 {
2281 	return net_buf_simple_pull(&buf->b, len);
2282 }
2283 
2284 /**
2285  * @brief Remove data from the beginning of the buffer.
2286  *
2287  * Removes data from the beginning of the buffer by modifying the data
2288  * pointer and buffer length.
2289  *
2290  * @param buf Buffer to update.
2291  * @param len Number of bytes to remove.
2292  *
2293  * @return Pointer to the old beginning of the buffer data.
2294  */
net_buf_pull_mem(struct net_buf * buf,size_t len)2295 static inline void *net_buf_pull_mem(struct net_buf *buf, size_t len)
2296 {
2297 	return net_buf_simple_pull_mem(&buf->b, len);
2298 }
2299 
2300 /**
2301  * @brief Remove a 8-bit value from the beginning of the buffer
2302  *
2303  * Same idea as with net_buf_pull(), but a helper for operating on
2304  * 8-bit values.
2305  *
2306  * @param buf A valid pointer on a buffer.
2307  *
2308  * @return The 8-bit removed value
2309  */
net_buf_pull_u8(struct net_buf * buf)2310 static inline uint8_t net_buf_pull_u8(struct net_buf *buf)
2311 {
2312 	return net_buf_simple_pull_u8(&buf->b);
2313 }
2314 
2315 /**
2316  * @brief Remove and convert 16 bits from the beginning of the buffer.
2317  *
2318  * Same idea as with net_buf_pull(), but a helper for operating on
2319  * 16-bit little endian data.
2320  *
2321  * @param buf A valid pointer on a buffer.
2322  *
2323  * @return 16-bit value converted from little endian to host endian.
2324  */
net_buf_pull_le16(struct net_buf * buf)2325 static inline uint16_t net_buf_pull_le16(struct net_buf *buf)
2326 {
2327 	return net_buf_simple_pull_le16(&buf->b);
2328 }
2329 
2330 /**
2331  * @brief Remove and convert 16 bits from the beginning of the buffer.
2332  *
2333  * Same idea as with net_buf_pull(), but a helper for operating on
2334  * 16-bit big endian data.
2335  *
2336  * @param buf A valid pointer on a buffer.
2337  *
2338  * @return 16-bit value converted from big endian to host endian.
2339  */
net_buf_pull_be16(struct net_buf * buf)2340 static inline uint16_t net_buf_pull_be16(struct net_buf *buf)
2341 {
2342 	return net_buf_simple_pull_be16(&buf->b);
2343 }
2344 
2345 /**
2346  * @brief Remove and convert 24 bits from the beginning of the buffer.
2347  *
2348  * Same idea as with net_buf_pull(), but a helper for operating on
2349  * 24-bit little endian data.
2350  *
2351  * @param buf A valid pointer on a buffer.
2352  *
2353  * @return 24-bit value converted from little endian to host endian.
2354  */
net_buf_pull_le24(struct net_buf * buf)2355 static inline uint32_t net_buf_pull_le24(struct net_buf *buf)
2356 {
2357 	return net_buf_simple_pull_le24(&buf->b);
2358 }
2359 
2360 /**
2361  * @brief Remove and convert 24 bits from the beginning of the buffer.
2362  *
2363  * Same idea as with net_buf_pull(), but a helper for operating on
2364  * 24-bit big endian data.
2365  *
2366  * @param buf A valid pointer on a buffer.
2367  *
2368  * @return 24-bit value converted from big endian to host endian.
2369  */
net_buf_pull_be24(struct net_buf * buf)2370 static inline uint32_t net_buf_pull_be24(struct net_buf *buf)
2371 {
2372 	return net_buf_simple_pull_be24(&buf->b);
2373 }
2374 
2375 /**
2376  * @brief Remove and convert 32 bits from the beginning of the buffer.
2377  *
2378  * Same idea as with net_buf_pull(), but a helper for operating on
2379  * 32-bit little endian data.
2380  *
2381  * @param buf A valid pointer on a buffer.
2382  *
2383  * @return 32-bit value converted from little endian to host endian.
2384  */
net_buf_pull_le32(struct net_buf * buf)2385 static inline uint32_t net_buf_pull_le32(struct net_buf *buf)
2386 {
2387 	return net_buf_simple_pull_le32(&buf->b);
2388 }
2389 
2390 /**
2391  * @brief Remove and convert 32 bits from the beginning of the buffer.
2392  *
2393  * Same idea as with net_buf_pull(), but a helper for operating on
2394  * 32-bit big endian data.
2395  *
2396  * @param buf A valid pointer on a buffer
2397  *
2398  * @return 32-bit value converted from big endian to host endian.
2399  */
net_buf_pull_be32(struct net_buf * buf)2400 static inline uint32_t net_buf_pull_be32(struct net_buf *buf)
2401 {
2402 	return net_buf_simple_pull_be32(&buf->b);
2403 }
2404 
2405 /**
2406  * @brief Remove and convert 40 bits from the beginning of the buffer.
2407  *
2408  * Same idea as with net_buf_pull(), but a helper for operating on
2409  * 40-bit little endian data.
2410  *
2411  * @param buf A valid pointer on a buffer.
2412  *
2413  * @return 40-bit value converted from little endian to host endian.
2414  */
net_buf_pull_le40(struct net_buf * buf)2415 static inline uint64_t net_buf_pull_le40(struct net_buf *buf)
2416 {
2417 	return net_buf_simple_pull_le40(&buf->b);
2418 }
2419 
2420 /**
2421  * @brief Remove and convert 40 bits from the beginning of the buffer.
2422  *
2423  * Same idea as with net_buf_pull(), but a helper for operating on
2424  * 40-bit big endian data.
2425  *
2426  * @param buf A valid pointer on a buffer
2427  *
2428  * @return 40-bit value converted from big endian to host endian.
2429  */
net_buf_pull_be40(struct net_buf * buf)2430 static inline uint64_t net_buf_pull_be40(struct net_buf *buf)
2431 {
2432 	return net_buf_simple_pull_be40(&buf->b);
2433 }
2434 
2435 /**
2436  * @brief Remove and convert 48 bits from the beginning of the buffer.
2437  *
2438  * Same idea as with net_buf_pull(), but a helper for operating on
2439  * 48-bit little endian data.
2440  *
2441  * @param buf A valid pointer on a buffer.
2442  *
2443  * @return 48-bit value converted from little endian to host endian.
2444  */
net_buf_pull_le48(struct net_buf * buf)2445 static inline uint64_t net_buf_pull_le48(struct net_buf *buf)
2446 {
2447 	return net_buf_simple_pull_le48(&buf->b);
2448 }
2449 
2450 /**
2451  * @brief Remove and convert 48 bits from the beginning of the buffer.
2452  *
2453  * Same idea as with net_buf_pull(), but a helper for operating on
2454  * 48-bit big endian data.
2455  *
2456  * @param buf A valid pointer on a buffer
2457  *
2458  * @return 48-bit value converted from big endian to host endian.
2459  */
net_buf_pull_be48(struct net_buf * buf)2460 static inline uint64_t net_buf_pull_be48(struct net_buf *buf)
2461 {
2462 	return net_buf_simple_pull_be48(&buf->b);
2463 }
2464 
2465 /**
2466  * @brief Remove and convert 64 bits from the beginning of the buffer.
2467  *
2468  * Same idea as with net_buf_pull(), but a helper for operating on
2469  * 64-bit little endian data.
2470  *
2471  * @param buf A valid pointer on a buffer.
2472  *
2473  * @return 64-bit value converted from little endian to host endian.
2474  */
net_buf_pull_le64(struct net_buf * buf)2475 static inline uint64_t net_buf_pull_le64(struct net_buf *buf)
2476 {
2477 	return net_buf_simple_pull_le64(&buf->b);
2478 }
2479 
2480 /**
2481  * @brief Remove and convert 64 bits from the beginning of the buffer.
2482  *
2483  * Same idea as with net_buf_pull(), but a helper for operating on
2484  * 64-bit big endian data.
2485  *
2486  * @param buf A valid pointer on a buffer
2487  *
2488  * @return 64-bit value converted from big endian to host endian.
2489  */
net_buf_pull_be64(struct net_buf * buf)2490 static inline uint64_t net_buf_pull_be64(struct net_buf *buf)
2491 {
2492 	return net_buf_simple_pull_be64(&buf->b);
2493 }
2494 
2495 /**
2496  * @brief Check buffer tailroom.
2497  *
2498  * Check how much free space there is at the end of the buffer.
2499  *
2500  * @param buf A valid pointer on a buffer
2501  *
2502  * @return Number of bytes available at the end of the buffer.
2503  */
net_buf_tailroom(const struct net_buf * buf)2504 static inline size_t net_buf_tailroom(const struct net_buf *buf)
2505 {
2506 	return net_buf_simple_tailroom(&buf->b);
2507 }
2508 
2509 /**
2510  * @brief Check buffer headroom.
2511  *
2512  * Check how much free space there is in the beginning of the buffer.
2513  *
2514  * buf A valid pointer on a buffer
2515  *
2516  * @return Number of bytes available in the beginning of the buffer.
2517  */
net_buf_headroom(const struct net_buf * buf)2518 static inline size_t net_buf_headroom(const struct net_buf *buf)
2519 {
2520 	return net_buf_simple_headroom(&buf->b);
2521 }
2522 
2523 /**
2524  * @brief Check maximum net_buf::len value.
2525  *
2526  * This value is depending on the number of bytes being reserved as headroom.
2527  *
2528  * @param buf A valid pointer on a buffer
2529  *
2530  * @return Number of bytes usable behind the net_buf::data pointer.
2531  */
net_buf_max_len(const struct net_buf * buf)2532 static inline uint16_t net_buf_max_len(const struct net_buf *buf)
2533 {
2534 	return net_buf_simple_max_len(&buf->b);
2535 }
2536 
2537 /**
2538  * @brief Get the tail pointer for a buffer.
2539  *
2540  * Get a pointer to the end of the data in a buffer.
2541  *
2542  * @param buf Buffer.
2543  *
2544  * @return Tail pointer for the buffer.
2545  */
net_buf_tail(const struct net_buf * buf)2546 static inline uint8_t *net_buf_tail(const struct net_buf *buf)
2547 {
2548 	return net_buf_simple_tail(&buf->b);
2549 }
2550 
2551 /**
2552  * @brief Find the last fragment in the fragment list.
2553  *
2554  * @return Pointer to last fragment in the list.
2555  */
2556 struct net_buf *net_buf_frag_last(struct net_buf *frags);
2557 
2558 /**
2559  * @brief Insert a new fragment to a chain of bufs.
2560  *
2561  * Insert a new fragment into the buffer fragments list after the parent.
2562  *
2563  * Note: This function takes ownership of the fragment reference so the
2564  * caller is not required to unref.
2565  *
2566  * @param parent Parent buffer/fragment.
2567  * @param frag Fragment to insert.
2568  */
2569 void net_buf_frag_insert(struct net_buf *parent, struct net_buf *frag);
2570 
2571 /**
2572  * @brief Add a new fragment to the end of a chain of bufs.
2573  *
2574  * Append a new fragment into the buffer fragments list.
2575  *
2576  * Note: This function takes ownership of the fragment reference so the
2577  * caller is not required to unref.
2578  *
2579  * @param head Head of the fragment chain.
2580  * @param frag Fragment to add.
2581  *
2582  * @return New head of the fragment chain. Either head (if head
2583  *         was non-NULL) or frag (if head was NULL).
2584  */
2585 struct net_buf *net_buf_frag_add(struct net_buf *head, struct net_buf *frag);
2586 
2587 /**
2588  * @brief Delete existing fragment from a chain of bufs.
2589  *
2590  * @param parent Parent buffer/fragment, or NULL if there is no parent.
2591  * @param frag Fragment to delete.
2592  *
2593  * @return Pointer to the buffer following the fragment, or NULL if it
2594  *         had no further fragments.
2595  */
2596 #if defined(CONFIG_NET_BUF_LOG)
2597 struct net_buf *net_buf_frag_del_debug(struct net_buf *parent,
2598 				       struct net_buf *frag,
2599 				       const char *func, int line);
2600 #define net_buf_frag_del(_parent, _frag) \
2601 	net_buf_frag_del_debug(_parent, _frag, __func__, __LINE__)
2602 #else
2603 struct net_buf *net_buf_frag_del(struct net_buf *parent, struct net_buf *frag);
2604 #endif
2605 
2606 /**
2607  * @brief Copy bytes from net_buf chain starting at offset to linear buffer
2608  *
2609  * Copy (extract) @a len bytes from @a src net_buf chain, starting from @a
2610  * offset in it, to a linear buffer @a dst. Return number of bytes actually
2611  * copied, which may be less than requested, if net_buf chain doesn't have
2612  * enough data, or destination buffer is too small.
2613  *
2614  * @param dst Destination buffer
2615  * @param dst_len Destination buffer length
2616  * @param src Source net_buf chain
2617  * @param offset Starting offset to copy from
2618  * @param len Number of bytes to copy
2619  * @return number of bytes actually copied
2620  */
2621 size_t net_buf_linearize(void *dst, size_t dst_len,
2622 			 const struct net_buf *src, size_t offset, size_t len);
2623 
2624 /**
2625  * @typedef net_buf_allocator_cb
2626  * @brief Network buffer allocator callback.
2627  *
2628  * @details The allocator callback is called when net_buf_append_bytes
2629  * needs to allocate a new net_buf.
2630  *
2631  * @param timeout Affects the action taken should the net buf pool be empty.
2632  *        If K_NO_WAIT, then return immediately. If K_FOREVER, then
2633  *        wait as long as necessary. Otherwise, wait until the specified
2634  *        timeout.
2635  * @param user_data The user data given in net_buf_append_bytes call.
2636  * @return pointer to allocated net_buf or NULL on error.
2637  */
2638 typedef struct net_buf * __must_check (*net_buf_allocator_cb)(k_timeout_t timeout,
2639 							      void *user_data);
2640 
2641 /**
2642  * @brief Append data to a list of net_buf
2643  *
2644  * @details Append data to a net_buf. If there is not enough space in the
2645  * net_buf then more net_buf will be added, unless there are no free net_buf
2646  * and timeout occurs. If not allocator is provided it attempts to allocate from
2647  * the same pool as the original buffer.
2648  *
2649  * @param buf Network buffer.
2650  * @param len Total length of input data
2651  * @param value Data to be added
2652  * @param timeout Timeout is passed to the net_buf allocator callback.
2653  * @param allocate_cb When a new net_buf is required, use this callback.
2654  * @param user_data A user data pointer to be supplied to the allocate_cb.
2655  *        This pointer is can be anything from a mem_pool or a net_pkt, the
2656  *        logic is left up to the allocate_cb function.
2657  *
2658  * @return Length of data actually added. This may be less than input
2659  *         length if other timeout than K_FOREVER was used, and there
2660  *         were no free fragments in a pool to accommodate all data.
2661  */
2662 size_t net_buf_append_bytes(struct net_buf *buf, size_t len,
2663 			    const void *value, k_timeout_t timeout,
2664 			    net_buf_allocator_cb allocate_cb, void *user_data);
2665 
2666 /**
2667  * @brief Match data with a net_buf's content
2668  *
2669  * @details Compare data with a content of a net_buf. Provide information about
2670  * the number of bytes matching between both. If needed, traverse
2671  * through multiple buffer fragments.
2672  *
2673  * @param buf Network buffer
2674  * @param offset Starting offset to compare from
2675  * @param data Data buffer for comparison
2676  * @param len Number of bytes to compare
2677  *
2678  * @return The number of bytes compared before the first difference.
2679  */
2680 size_t net_buf_data_match(const struct net_buf *buf, size_t offset, const void *data, size_t len);
2681 
2682 /**
2683  * @brief Skip N number of bytes in a net_buf
2684  *
2685  * @details Skip N number of bytes starting from fragment's offset. If the total
2686  * length of data is placed in multiple fragments, this function will skip from
2687  * all fragments until it reaches N number of bytes.  Any fully skipped buffers
2688  * are removed from the net_buf list.
2689  *
2690  * @param buf Network buffer.
2691  * @param len Total length of data to be skipped.
2692  *
2693  * @return Pointer to the fragment or
2694  *         NULL and pos is 0 after successful skip,
2695  *         NULL and pos is 0xffff otherwise.
2696  */
net_buf_skip(struct net_buf * buf,size_t len)2697 static inline struct net_buf *net_buf_skip(struct net_buf *buf, size_t len)
2698 {
2699 	while (buf && len--) {
2700 		net_buf_pull_u8(buf);
2701 		if (!buf->len) {
2702 			buf = net_buf_frag_del(NULL, buf);
2703 		}
2704 	}
2705 
2706 	return buf;
2707 }
2708 
2709 /**
2710  * @brief Calculate amount of bytes stored in fragments.
2711  *
2712  * Calculates the total amount of data stored in the given buffer and the
2713  * fragments linked to it.
2714  *
2715  * @param buf Buffer to start off with.
2716  *
2717  * @return Number of bytes in the buffer and its fragments.
2718  */
net_buf_frags_len(const struct net_buf * buf)2719 static inline size_t net_buf_frags_len(const struct net_buf *buf)
2720 {
2721 	size_t bytes = 0;
2722 
2723 	while (buf) {
2724 		bytes += buf->len;
2725 		buf = buf->frags;
2726 	}
2727 
2728 	return bytes;
2729 }
2730 
2731 /**
2732  * @}
2733  */
2734 
2735 #ifdef __cplusplus
2736 }
2737 #endif
2738 
2739 #endif /* ZEPHYR_INCLUDE_NET_BUF_H_ */
2740