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