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 /** 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 * @deprecated Use @a k_fifo_get() instead.
1441 *
1442 * @param fifo Which FIFO to take the buffer from.
1443 * @param timeout Affects the action taken should the FIFO be empty.
1444 * If K_NO_WAIT, then return immediately. If K_FOREVER, then wait as
1445 * long as necessary. Otherwise, wait until the specified timeout.
1446 *
1447 * @return New buffer or NULL if the FIFO is empty.
1448 */
1449 #if defined(CONFIG_NET_BUF_LOG)
1450 __deprecated struct net_buf * __must_check net_buf_get_debug(struct k_fifo *fifo,
1451 k_timeout_t timeout,
1452 const char *func, int line);
1453 #define net_buf_get(_fifo, _timeout) \
1454 net_buf_get_debug(_fifo, _timeout, __func__, __LINE__)
1455 #else
1456 __deprecated struct net_buf * __must_check net_buf_get(struct k_fifo *fifo,
1457 k_timeout_t timeout);
1458 #endif
1459
1460 /**
1461 * @brief Destroy buffer from custom destroy callback
1462 *
1463 * This helper is only intended to be used from custom destroy callbacks.
1464 * If no custom destroy callback is given to NET_BUF_POOL_*_DEFINE() then
1465 * there is no need to use this API.
1466 *
1467 * @param buf Buffer to destroy.
1468 */
net_buf_destroy(struct net_buf * buf)1469 static inline void net_buf_destroy(struct net_buf *buf)
1470 {
1471 struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
1472
1473 if (buf->__buf) {
1474 if (!(buf->flags & NET_BUF_EXTERNAL_DATA)) {
1475 pool->alloc->cb->unref(buf, buf->__buf);
1476 }
1477 buf->__buf = NULL;
1478 }
1479
1480 k_lifo_put(&pool->free, buf);
1481 }
1482
1483 /**
1484 * @brief Reset buffer
1485 *
1486 * Reset buffer data and flags so it can be reused for other purposes.
1487 *
1488 * @param buf Buffer to reset.
1489 */
1490 void net_buf_reset(struct net_buf *buf);
1491
1492 /**
1493 * @brief Initialize buffer with the given headroom.
1494 *
1495 * The buffer is not expected to contain any data when this API is called.
1496 *
1497 * @param buf Buffer to initialize.
1498 * @param reserve How much headroom to reserve.
1499 */
1500 void net_buf_simple_reserve(struct net_buf_simple *buf, size_t reserve);
1501
1502 /**
1503 * @brief Put a buffer into a list
1504 *
1505 * @param list Which list to append the buffer to.
1506 * @param buf Buffer.
1507 */
1508 void net_buf_slist_put(sys_slist_t *list, struct net_buf *buf);
1509
1510 /**
1511 * @brief Get a buffer from a list.
1512 *
1513 * @param list Which list to take the buffer from.
1514 *
1515 * @return New buffer or NULL if the FIFO is empty.
1516 */
1517 struct net_buf * __must_check net_buf_slist_get(sys_slist_t *list);
1518
1519 /**
1520 * @brief Put a buffer to the end of a FIFO.
1521 *
1522 * @deprecated Use @a k_fifo_put() instead.
1523 *
1524 * @param fifo Which FIFO to put the buffer to.
1525 * @param buf Buffer.
1526 */
1527 __deprecated void net_buf_put(struct k_fifo *fifo, struct net_buf *buf);
1528
1529 /**
1530 * @brief Decrements the reference count of a buffer.
1531 *
1532 * The buffer is put back into the pool if the reference count reaches zero.
1533 *
1534 * @param buf A valid pointer on a buffer
1535 */
1536 #if defined(CONFIG_NET_BUF_LOG)
1537 void net_buf_unref_debug(struct net_buf *buf, const char *func, int line);
1538 #define net_buf_unref(_buf) \
1539 net_buf_unref_debug(_buf, __func__, __LINE__)
1540 #else
1541 void net_buf_unref(struct net_buf *buf);
1542 #endif
1543
1544 /**
1545 * @brief Increment the reference count of a buffer.
1546 *
1547 * @param buf A valid pointer on a buffer
1548 *
1549 * @return the buffer newly referenced
1550 */
1551 struct net_buf * __must_check net_buf_ref(struct net_buf *buf);
1552
1553 /**
1554 * @brief Clone buffer
1555 *
1556 * Duplicate given buffer including any (user) data and headers currently stored.
1557 *
1558 * @param buf A valid pointer on a buffer
1559 * @param timeout Affects the action taken should the pool be empty.
1560 * If K_NO_WAIT, then return immediately. If K_FOREVER, then
1561 * wait as long as necessary. Otherwise, wait until the specified
1562 * timeout.
1563 *
1564 * @return Cloned buffer or NULL if out of buffers.
1565 */
1566 struct net_buf * __must_check net_buf_clone(struct net_buf *buf,
1567 k_timeout_t timeout);
1568
1569 /**
1570 * @brief Get a pointer to the user data of a buffer.
1571 *
1572 * @param buf A valid pointer on a buffer
1573 *
1574 * @return Pointer to the user data of the buffer.
1575 */
net_buf_user_data(const struct net_buf * buf)1576 static inline void * __must_check net_buf_user_data(const struct net_buf *buf)
1577 {
1578 return (void *)buf->user_data;
1579 }
1580
1581 /**
1582 * @brief Copy user data from one to another buffer.
1583 *
1584 * @param dst A valid pointer to a buffer gettings its user data overwritten.
1585 * @param src A valid pointer to a buffer gettings its user data copied. User data size must be
1586 * equal to or exceed @a dst.
1587 *
1588 * @return 0 on success or negative error number on failure.
1589 */
1590 int net_buf_user_data_copy(struct net_buf *dst, const struct net_buf *src);
1591
1592 /**
1593 * @brief Initialize buffer with the given headroom.
1594 *
1595 * The buffer is not expected to contain any data when this API is called.
1596 *
1597 * @param buf Buffer to initialize.
1598 * @param reserve How much headroom to reserve.
1599 */
net_buf_reserve(struct net_buf * buf,size_t reserve)1600 static inline void net_buf_reserve(struct net_buf *buf, size_t reserve)
1601 {
1602 net_buf_simple_reserve(&buf->b, reserve);
1603 }
1604
1605 /**
1606 * @brief Prepare data to be added at the end of the buffer
1607 *
1608 * Increments the data length of a buffer to account for more data
1609 * at the end.
1610 *
1611 * @param buf Buffer to update.
1612 * @param len Number of bytes to increment the length with.
1613 *
1614 * @return The original tail of the buffer.
1615 */
net_buf_add(struct net_buf * buf,size_t len)1616 static inline void *net_buf_add(struct net_buf *buf, size_t len)
1617 {
1618 return net_buf_simple_add(&buf->b, len);
1619 }
1620
1621 /**
1622 * @brief Copies the given number of bytes to the end of the buffer
1623 *
1624 * Increments the data length of the buffer to account for more data at
1625 * the end.
1626 *
1627 * @param buf Buffer to update.
1628 * @param mem Location of data to be added.
1629 * @param len Length of data to be added
1630 *
1631 * @return The original tail of the buffer.
1632 */
net_buf_add_mem(struct net_buf * buf,const void * mem,size_t len)1633 static inline void *net_buf_add_mem(struct net_buf *buf, const void *mem,
1634 size_t len)
1635 {
1636 return net_buf_simple_add_mem(&buf->b, mem, len);
1637 }
1638
1639 /**
1640 * @brief Add (8-bit) byte at the end of the buffer
1641 *
1642 * Increments the data length of the buffer to account for more data at
1643 * the end.
1644 *
1645 * @param buf Buffer to update.
1646 * @param val byte value to be added.
1647 *
1648 * @return Pointer to the value added
1649 */
net_buf_add_u8(struct net_buf * buf,uint8_t val)1650 static inline uint8_t *net_buf_add_u8(struct net_buf *buf, uint8_t val)
1651 {
1652 return net_buf_simple_add_u8(&buf->b, val);
1653 }
1654
1655 /**
1656 * @brief Add 16-bit value at the end of the buffer
1657 *
1658 * Adds 16-bit value in little endian format at the end of buffer.
1659 * Increments the data length of a buffer to account for more data
1660 * at the end.
1661 *
1662 * @param buf Buffer to update.
1663 * @param val 16-bit value to be added.
1664 */
net_buf_add_le16(struct net_buf * buf,uint16_t val)1665 static inline void net_buf_add_le16(struct net_buf *buf, uint16_t val)
1666 {
1667 net_buf_simple_add_le16(&buf->b, val);
1668 }
1669
1670 /**
1671 * @brief Add 16-bit value at the end of the buffer
1672 *
1673 * Adds 16-bit value in big endian format at the end of buffer.
1674 * Increments the data length of a buffer to account for more data
1675 * at the end.
1676 *
1677 * @param buf Buffer to update.
1678 * @param val 16-bit value to be added.
1679 */
net_buf_add_be16(struct net_buf * buf,uint16_t val)1680 static inline void net_buf_add_be16(struct net_buf *buf, uint16_t val)
1681 {
1682 net_buf_simple_add_be16(&buf->b, val);
1683 }
1684
1685 /**
1686 * @brief Add 24-bit value at the end of the buffer
1687 *
1688 * Adds 24-bit value in little endian format at the end of buffer.
1689 * Increments the data length of a buffer to account for more data
1690 * at the end.
1691 *
1692 * @param buf Buffer to update.
1693 * @param val 24-bit value to be added.
1694 */
net_buf_add_le24(struct net_buf * buf,uint32_t val)1695 static inline void net_buf_add_le24(struct net_buf *buf, uint32_t val)
1696 {
1697 net_buf_simple_add_le24(&buf->b, val);
1698 }
1699
1700 /**
1701 * @brief Add 24-bit value at the end of the buffer
1702 *
1703 * Adds 24-bit value in big endian format at the end of buffer.
1704 * Increments the data length of a buffer to account for more data
1705 * at the end.
1706 *
1707 * @param buf Buffer to update.
1708 * @param val 24-bit value to be added.
1709 */
net_buf_add_be24(struct net_buf * buf,uint32_t val)1710 static inline void net_buf_add_be24(struct net_buf *buf, uint32_t val)
1711 {
1712 net_buf_simple_add_be24(&buf->b, val);
1713 }
1714
1715 /**
1716 * @brief Add 32-bit value at the end of the buffer
1717 *
1718 * Adds 32-bit value in little endian format at the end of buffer.
1719 * Increments the data length of a buffer to account for more data
1720 * at the end.
1721 *
1722 * @param buf Buffer to update.
1723 * @param val 32-bit value to be added.
1724 */
net_buf_add_le32(struct net_buf * buf,uint32_t val)1725 static inline void net_buf_add_le32(struct net_buf *buf, uint32_t val)
1726 {
1727 net_buf_simple_add_le32(&buf->b, val);
1728 }
1729
1730 /**
1731 * @brief Add 32-bit value at the end of the buffer
1732 *
1733 * Adds 32-bit value in big endian format at the end of buffer.
1734 * Increments the data length of a buffer to account for more data
1735 * at the end.
1736 *
1737 * @param buf Buffer to update.
1738 * @param val 32-bit value to be added.
1739 */
net_buf_add_be32(struct net_buf * buf,uint32_t val)1740 static inline void net_buf_add_be32(struct net_buf *buf, uint32_t val)
1741 {
1742 net_buf_simple_add_be32(&buf->b, val);
1743 }
1744
1745 /**
1746 * @brief Add 40-bit value at the end of the buffer
1747 *
1748 * Adds 40-bit value in little endian format at the end of buffer.
1749 * Increments the data length of a buffer to account for more data
1750 * at the end.
1751 *
1752 * @param buf Buffer to update.
1753 * @param val 40-bit value to be added.
1754 */
net_buf_add_le40(struct net_buf * buf,uint64_t val)1755 static inline void net_buf_add_le40(struct net_buf *buf, uint64_t val)
1756 {
1757 net_buf_simple_add_le40(&buf->b, val);
1758 }
1759
1760 /**
1761 * @brief Add 40-bit value at the end of the buffer
1762 *
1763 * Adds 40-bit value in big endian format at the end of buffer.
1764 * Increments the data length of a buffer to account for more data
1765 * at the end.
1766 *
1767 * @param buf Buffer to update.
1768 * @param val 40-bit value to be added.
1769 */
net_buf_add_be40(struct net_buf * buf,uint64_t val)1770 static inline void net_buf_add_be40(struct net_buf *buf, uint64_t val)
1771 {
1772 net_buf_simple_add_be40(&buf->b, val);
1773 }
1774
1775 /**
1776 * @brief Add 48-bit value at the end of the buffer
1777 *
1778 * Adds 48-bit value in little endian format at the end of buffer.
1779 * Increments the data length of a buffer to account for more data
1780 * at the end.
1781 *
1782 * @param buf Buffer to update.
1783 * @param val 48-bit value to be added.
1784 */
net_buf_add_le48(struct net_buf * buf,uint64_t val)1785 static inline void net_buf_add_le48(struct net_buf *buf, uint64_t val)
1786 {
1787 net_buf_simple_add_le48(&buf->b, val);
1788 }
1789
1790 /**
1791 * @brief Add 48-bit value at the end of the buffer
1792 *
1793 * Adds 48-bit value in big endian format at the end of buffer.
1794 * Increments the data length of a buffer to account for more data
1795 * at the end.
1796 *
1797 * @param buf Buffer to update.
1798 * @param val 48-bit value to be added.
1799 */
net_buf_add_be48(struct net_buf * buf,uint64_t val)1800 static inline void net_buf_add_be48(struct net_buf *buf, uint64_t val)
1801 {
1802 net_buf_simple_add_be48(&buf->b, val);
1803 }
1804
1805 /**
1806 * @brief Add 64-bit value at the end of the buffer
1807 *
1808 * Adds 64-bit value in little endian format at the end of buffer.
1809 * Increments the data length of a buffer to account for more data
1810 * at the end.
1811 *
1812 * @param buf Buffer to update.
1813 * @param val 64-bit value to be added.
1814 */
net_buf_add_le64(struct net_buf * buf,uint64_t val)1815 static inline void net_buf_add_le64(struct net_buf *buf, uint64_t val)
1816 {
1817 net_buf_simple_add_le64(&buf->b, val);
1818 }
1819
1820 /**
1821 * @brief Add 64-bit value at the end of the buffer
1822 *
1823 * Adds 64-bit value in big endian format at the end of buffer.
1824 * Increments the data length of a buffer to account for more data
1825 * at the end.
1826 *
1827 * @param buf Buffer to update.
1828 * @param val 64-bit value to be added.
1829 */
net_buf_add_be64(struct net_buf * buf,uint64_t val)1830 static inline void net_buf_add_be64(struct net_buf *buf, uint64_t val)
1831 {
1832 net_buf_simple_add_be64(&buf->b, val);
1833 }
1834
1835 /**
1836 * @brief Remove data from the end of the buffer.
1837 *
1838 * Removes data from the end of the buffer by modifying the buffer length.
1839 *
1840 * @param buf Buffer to update.
1841 * @param len Number of bytes to remove.
1842 *
1843 * @return New end of the buffer data.
1844 */
net_buf_remove_mem(struct net_buf * buf,size_t len)1845 static inline void *net_buf_remove_mem(struct net_buf *buf, size_t len)
1846 {
1847 return net_buf_simple_remove_mem(&buf->b, len);
1848 }
1849
1850 /**
1851 * @brief Remove a 8-bit value from the end of the buffer
1852 *
1853 * Same idea as with net_buf_remove_mem(), but a helper for operating on
1854 * 8-bit values.
1855 *
1856 * @param buf A valid pointer on a buffer.
1857 *
1858 * @return The 8-bit removed value
1859 */
net_buf_remove_u8(struct net_buf * buf)1860 static inline uint8_t net_buf_remove_u8(struct net_buf *buf)
1861 {
1862 return net_buf_simple_remove_u8(&buf->b);
1863 }
1864
1865 /**
1866 * @brief Remove and convert 16 bits from the end of the buffer.
1867 *
1868 * Same idea as with net_buf_remove_mem(), but a helper for operating on
1869 * 16-bit little endian data.
1870 *
1871 * @param buf A valid pointer on a buffer.
1872 *
1873 * @return 16-bit value converted from little endian to host endian.
1874 */
net_buf_remove_le16(struct net_buf * buf)1875 static inline uint16_t net_buf_remove_le16(struct net_buf *buf)
1876 {
1877 return net_buf_simple_remove_le16(&buf->b);
1878 }
1879
1880 /**
1881 * @brief Remove and convert 16 bits from the end of the buffer.
1882 *
1883 * Same idea as with net_buf_remove_mem(), but a helper for operating on
1884 * 16-bit big endian data.
1885 *
1886 * @param buf A valid pointer on a buffer.
1887 *
1888 * @return 16-bit value converted from big endian to host endian.
1889 */
net_buf_remove_be16(struct net_buf * buf)1890 static inline uint16_t net_buf_remove_be16(struct net_buf *buf)
1891 {
1892 return net_buf_simple_remove_be16(&buf->b);
1893 }
1894
1895 /**
1896 * @brief Remove and convert 24 bits from the end of the buffer.
1897 *
1898 * Same idea as with net_buf_remove_mem(), but a helper for operating on
1899 * 24-bit big endian data.
1900 *
1901 * @param buf A valid pointer on a buffer.
1902 *
1903 * @return 24-bit value converted from big endian to host endian.
1904 */
net_buf_remove_be24(struct net_buf * buf)1905 static inline uint32_t net_buf_remove_be24(struct net_buf *buf)
1906 {
1907 return net_buf_simple_remove_be24(&buf->b);
1908 }
1909
1910 /**
1911 * @brief Remove and convert 24 bits from the end of the buffer.
1912 *
1913 * Same idea as with net_buf_remove_mem(), but a helper for operating on
1914 * 24-bit little endian data.
1915 *
1916 * @param buf A valid pointer on a buffer.
1917 *
1918 * @return 24-bit value converted from little endian to host endian.
1919 */
net_buf_remove_le24(struct net_buf * buf)1920 static inline uint32_t net_buf_remove_le24(struct net_buf *buf)
1921 {
1922 return net_buf_simple_remove_le24(&buf->b);
1923 }
1924
1925 /**
1926 * @brief Remove and convert 32 bits from the end of the buffer.
1927 *
1928 * Same idea as with net_buf_remove_mem(), but a helper for operating on
1929 * 32-bit little endian data.
1930 *
1931 * @param buf A valid pointer on a buffer.
1932 *
1933 * @return 32-bit value converted from little endian to host endian.
1934 */
net_buf_remove_le32(struct net_buf * buf)1935 static inline uint32_t net_buf_remove_le32(struct net_buf *buf)
1936 {
1937 return net_buf_simple_remove_le32(&buf->b);
1938 }
1939
1940 /**
1941 * @brief Remove and convert 32 bits from the end of the buffer.
1942 *
1943 * Same idea as with net_buf_remove_mem(), but a helper for operating on
1944 * 32-bit big endian data.
1945 *
1946 * @param buf A valid pointer on a buffer
1947 *
1948 * @return 32-bit value converted from big endian to host endian.
1949 */
net_buf_remove_be32(struct net_buf * buf)1950 static inline uint32_t net_buf_remove_be32(struct net_buf *buf)
1951 {
1952 return net_buf_simple_remove_be32(&buf->b);
1953 }
1954
1955 /**
1956 * @brief Remove and convert 40 bits from the end of the buffer.
1957 *
1958 * Same idea as with net_buf_remove_mem(), but a helper for operating on
1959 * 40-bit little endian data.
1960 *
1961 * @param buf A valid pointer on a buffer.
1962 *
1963 * @return 40-bit value converted from little endian to host endian.
1964 */
net_buf_remove_le40(struct net_buf * buf)1965 static inline uint64_t net_buf_remove_le40(struct net_buf *buf)
1966 {
1967 return net_buf_simple_remove_le40(&buf->b);
1968 }
1969
1970 /**
1971 * @brief Remove and convert 40 bits from the end of the buffer.
1972 *
1973 * Same idea as with net_buf_remove_mem(), but a helper for operating on
1974 * 40-bit big endian data.
1975 *
1976 * @param buf A valid pointer on a buffer
1977 *
1978 * @return 40-bit value converted from big endian to host endian.
1979 */
net_buf_remove_be40(struct net_buf * buf)1980 static inline uint64_t net_buf_remove_be40(struct net_buf *buf)
1981 {
1982 return net_buf_simple_remove_be40(&buf->b);
1983 }
1984
1985 /**
1986 * @brief Remove and convert 48 bits from the end of the buffer.
1987 *
1988 * Same idea as with net_buf_remove_mem(), but a helper for operating on
1989 * 48-bit little endian data.
1990 *
1991 * @param buf A valid pointer on a buffer.
1992 *
1993 * @return 48-bit value converted from little endian to host endian.
1994 */
net_buf_remove_le48(struct net_buf * buf)1995 static inline uint64_t net_buf_remove_le48(struct net_buf *buf)
1996 {
1997 return net_buf_simple_remove_le48(&buf->b);
1998 }
1999
2000 /**
2001 * @brief Remove and convert 48 bits from the end of the buffer.
2002 *
2003 * Same idea as with net_buf_remove_mem(), but a helper for operating on
2004 * 48-bit big endian data.
2005 *
2006 * @param buf A valid pointer on a buffer
2007 *
2008 * @return 48-bit value converted from big endian to host endian.
2009 */
net_buf_remove_be48(struct net_buf * buf)2010 static inline uint64_t net_buf_remove_be48(struct net_buf *buf)
2011 {
2012 return net_buf_simple_remove_be48(&buf->b);
2013 }
2014
2015 /**
2016 * @brief Remove and convert 64 bits from the end of the buffer.
2017 *
2018 * Same idea as with net_buf_remove_mem(), but a helper for operating on
2019 * 64-bit little endian data.
2020 *
2021 * @param buf A valid pointer on a buffer.
2022 *
2023 * @return 64-bit value converted from little endian to host endian.
2024 */
net_buf_remove_le64(struct net_buf * buf)2025 static inline uint64_t net_buf_remove_le64(struct net_buf *buf)
2026 {
2027 return net_buf_simple_remove_le64(&buf->b);
2028 }
2029
2030 /**
2031 * @brief Remove and convert 64 bits from the end of the buffer.
2032 *
2033 * Same idea as with net_buf_remove_mem(), but a helper for operating on
2034 * 64-bit big endian data.
2035 *
2036 * @param buf A valid pointer on a buffer
2037 *
2038 * @return 64-bit value converted from big endian to host endian.
2039 */
net_buf_remove_be64(struct net_buf * buf)2040 static inline uint64_t net_buf_remove_be64(struct net_buf *buf)
2041 {
2042 return net_buf_simple_remove_be64(&buf->b);
2043 }
2044
2045 /**
2046 * @brief Prepare data to be added at the start of the buffer
2047 *
2048 * Modifies the data pointer and buffer length to account for more data
2049 * in the beginning of the buffer.
2050 *
2051 * @param buf Buffer to update.
2052 * @param len Number of bytes to add to the beginning.
2053 *
2054 * @return The new beginning of the buffer data.
2055 */
net_buf_push(struct net_buf * buf,size_t len)2056 static inline void *net_buf_push(struct net_buf *buf, size_t len)
2057 {
2058 return net_buf_simple_push(&buf->b, len);
2059 }
2060
2061 /**
2062 * @brief Copies the given number of bytes to the start of the buffer
2063 *
2064 * Modifies the data pointer and buffer length to account for more data
2065 * in the beginning of the buffer.
2066 *
2067 * @param buf Buffer to update.
2068 * @param mem Location of data to be added.
2069 * @param len Length of data to be added.
2070 *
2071 * @return The new beginning of the buffer data.
2072 */
net_buf_push_mem(struct net_buf * buf,const void * mem,size_t len)2073 static inline void *net_buf_push_mem(struct net_buf *buf, const void *mem,
2074 size_t len)
2075 {
2076 return net_buf_simple_push_mem(&buf->b, mem, len);
2077 }
2078
2079 /**
2080 * @brief Push 8-bit value to the beginning of the buffer
2081 *
2082 * Adds 8-bit value the beginning of the buffer.
2083 *
2084 * @param buf Buffer to update.
2085 * @param val 8-bit value to be pushed to the buffer.
2086 */
net_buf_push_u8(struct net_buf * buf,uint8_t val)2087 static inline void net_buf_push_u8(struct net_buf *buf, uint8_t val)
2088 {
2089 net_buf_simple_push_u8(&buf->b, val);
2090 }
2091
2092 /**
2093 * @brief Push 16-bit value to the beginning of the buffer
2094 *
2095 * Adds 16-bit value in little endian format to the beginning of the
2096 * buffer.
2097 *
2098 * @param buf Buffer to update.
2099 * @param val 16-bit value to be pushed to the buffer.
2100 */
net_buf_push_le16(struct net_buf * buf,uint16_t val)2101 static inline void net_buf_push_le16(struct net_buf *buf, uint16_t val)
2102 {
2103 net_buf_simple_push_le16(&buf->b, val);
2104 }
2105
2106 /**
2107 * @brief Push 16-bit value to the beginning of the buffer
2108 *
2109 * Adds 16-bit value in big endian format to the beginning of the
2110 * buffer.
2111 *
2112 * @param buf Buffer to update.
2113 * @param val 16-bit value to be pushed to the buffer.
2114 */
net_buf_push_be16(struct net_buf * buf,uint16_t val)2115 static inline void net_buf_push_be16(struct net_buf *buf, uint16_t val)
2116 {
2117 net_buf_simple_push_be16(&buf->b, val);
2118 }
2119
2120 /**
2121 * @brief Push 24-bit value to the beginning of the buffer
2122 *
2123 * Adds 24-bit value in little endian format to the beginning of the
2124 * buffer.
2125 *
2126 * @param buf Buffer to update.
2127 * @param val 24-bit value to be pushed to the buffer.
2128 */
net_buf_push_le24(struct net_buf * buf,uint32_t val)2129 static inline void net_buf_push_le24(struct net_buf *buf, uint32_t val)
2130 {
2131 net_buf_simple_push_le24(&buf->b, val);
2132 }
2133
2134 /**
2135 * @brief Push 24-bit value to the beginning of the buffer
2136 *
2137 * Adds 24-bit value in big endian format to the beginning of the
2138 * buffer.
2139 *
2140 * @param buf Buffer to update.
2141 * @param val 24-bit value to be pushed to the buffer.
2142 */
net_buf_push_be24(struct net_buf * buf,uint32_t val)2143 static inline void net_buf_push_be24(struct net_buf *buf, uint32_t val)
2144 {
2145 net_buf_simple_push_be24(&buf->b, val);
2146 }
2147
2148 /**
2149 * @brief Push 32-bit value to the beginning of the buffer
2150 *
2151 * Adds 32-bit value in little endian format to the beginning of the
2152 * buffer.
2153 *
2154 * @param buf Buffer to update.
2155 * @param val 32-bit value to be pushed to the buffer.
2156 */
net_buf_push_le32(struct net_buf * buf,uint32_t val)2157 static inline void net_buf_push_le32(struct net_buf *buf, uint32_t val)
2158 {
2159 net_buf_simple_push_le32(&buf->b, val);
2160 }
2161
2162 /**
2163 * @brief Push 32-bit value to the beginning of the buffer
2164 *
2165 * Adds 32-bit value in big endian format to the beginning of the
2166 * buffer.
2167 *
2168 * @param buf Buffer to update.
2169 * @param val 32-bit value to be pushed to the buffer.
2170 */
net_buf_push_be32(struct net_buf * buf,uint32_t val)2171 static inline void net_buf_push_be32(struct net_buf *buf, uint32_t val)
2172 {
2173 net_buf_simple_push_be32(&buf->b, val);
2174 }
2175
2176 /**
2177 * @brief Push 40-bit value to the beginning of the buffer
2178 *
2179 * Adds 40-bit value in little endian format to the beginning of the
2180 * buffer.
2181 *
2182 * @param buf Buffer to update.
2183 * @param val 40-bit value to be pushed to the buffer.
2184 */
net_buf_push_le40(struct net_buf * buf,uint64_t val)2185 static inline void net_buf_push_le40(struct net_buf *buf, uint64_t val)
2186 {
2187 net_buf_simple_push_le40(&buf->b, val);
2188 }
2189
2190 /**
2191 * @brief Push 40-bit value to the beginning of the buffer
2192 *
2193 * Adds 40-bit value in big endian format to the beginning of the
2194 * buffer.
2195 *
2196 * @param buf Buffer to update.
2197 * @param val 40-bit value to be pushed to the buffer.
2198 */
net_buf_push_be40(struct net_buf * buf,uint64_t val)2199 static inline void net_buf_push_be40(struct net_buf *buf, uint64_t val)
2200 {
2201 net_buf_simple_push_be40(&buf->b, val);
2202 }
2203
2204 /**
2205 * @brief Push 48-bit value to the beginning of the buffer
2206 *
2207 * Adds 48-bit value in little endian format to the beginning of the
2208 * buffer.
2209 *
2210 * @param buf Buffer to update.
2211 * @param val 48-bit value to be pushed to the buffer.
2212 */
net_buf_push_le48(struct net_buf * buf,uint64_t val)2213 static inline void net_buf_push_le48(struct net_buf *buf, uint64_t val)
2214 {
2215 net_buf_simple_push_le48(&buf->b, val);
2216 }
2217
2218 /**
2219 * @brief Push 48-bit value to the beginning of the buffer
2220 *
2221 * Adds 48-bit value in big endian format to the beginning of the
2222 * buffer.
2223 *
2224 * @param buf Buffer to update.
2225 * @param val 48-bit value to be pushed to the buffer.
2226 */
net_buf_push_be48(struct net_buf * buf,uint64_t val)2227 static inline void net_buf_push_be48(struct net_buf *buf, uint64_t val)
2228 {
2229 net_buf_simple_push_be48(&buf->b, val);
2230 }
2231
2232 /**
2233 * @brief Push 64-bit value to the beginning of the buffer
2234 *
2235 * Adds 64-bit value in little endian format to the beginning of the
2236 * buffer.
2237 *
2238 * @param buf Buffer to update.
2239 * @param val 64-bit value to be pushed to the buffer.
2240 */
net_buf_push_le64(struct net_buf * buf,uint64_t val)2241 static inline void net_buf_push_le64(struct net_buf *buf, uint64_t val)
2242 {
2243 net_buf_simple_push_le64(&buf->b, val);
2244 }
2245
2246 /**
2247 * @brief Push 64-bit value to the beginning of the buffer
2248 *
2249 * Adds 64-bit value in big endian format to the beginning of the
2250 * buffer.
2251 *
2252 * @param buf Buffer to update.
2253 * @param val 64-bit value to be pushed to the buffer.
2254 */
net_buf_push_be64(struct net_buf * buf,uint64_t val)2255 static inline void net_buf_push_be64(struct net_buf *buf, uint64_t val)
2256 {
2257 net_buf_simple_push_be64(&buf->b, val);
2258 }
2259
2260 /**
2261 * @brief Remove data from the beginning of the buffer.
2262 *
2263 * Removes data from the beginning of the buffer by modifying the data
2264 * pointer and buffer length.
2265 *
2266 * @param buf Buffer to update.
2267 * @param len Number of bytes to remove.
2268 *
2269 * @return New beginning of the buffer data.
2270 */
net_buf_pull(struct net_buf * buf,size_t len)2271 static inline void *net_buf_pull(struct net_buf *buf, size_t len)
2272 {
2273 return net_buf_simple_pull(&buf->b, len);
2274 }
2275
2276 /**
2277 * @brief Remove data from the beginning of the buffer.
2278 *
2279 * Removes data from the beginning of the buffer by modifying the data
2280 * pointer and buffer length.
2281 *
2282 * @param buf Buffer to update.
2283 * @param len Number of bytes to remove.
2284 *
2285 * @return Pointer to the old beginning of the buffer data.
2286 */
net_buf_pull_mem(struct net_buf * buf,size_t len)2287 static inline void *net_buf_pull_mem(struct net_buf *buf, size_t len)
2288 {
2289 return net_buf_simple_pull_mem(&buf->b, len);
2290 }
2291
2292 /**
2293 * @brief Remove a 8-bit value from the beginning of the buffer
2294 *
2295 * Same idea as with net_buf_pull(), but a helper for operating on
2296 * 8-bit values.
2297 *
2298 * @param buf A valid pointer on a buffer.
2299 *
2300 * @return The 8-bit removed value
2301 */
net_buf_pull_u8(struct net_buf * buf)2302 static inline uint8_t net_buf_pull_u8(struct net_buf *buf)
2303 {
2304 return net_buf_simple_pull_u8(&buf->b);
2305 }
2306
2307 /**
2308 * @brief Remove and convert 16 bits from the beginning of the buffer.
2309 *
2310 * Same idea as with net_buf_pull(), but a helper for operating on
2311 * 16-bit little endian data.
2312 *
2313 * @param buf A valid pointer on a buffer.
2314 *
2315 * @return 16-bit value converted from little endian to host endian.
2316 */
net_buf_pull_le16(struct net_buf * buf)2317 static inline uint16_t net_buf_pull_le16(struct net_buf *buf)
2318 {
2319 return net_buf_simple_pull_le16(&buf->b);
2320 }
2321
2322 /**
2323 * @brief Remove and convert 16 bits from the beginning of the buffer.
2324 *
2325 * Same idea as with net_buf_pull(), but a helper for operating on
2326 * 16-bit big endian data.
2327 *
2328 * @param buf A valid pointer on a buffer.
2329 *
2330 * @return 16-bit value converted from big endian to host endian.
2331 */
net_buf_pull_be16(struct net_buf * buf)2332 static inline uint16_t net_buf_pull_be16(struct net_buf *buf)
2333 {
2334 return net_buf_simple_pull_be16(&buf->b);
2335 }
2336
2337 /**
2338 * @brief Remove and convert 24 bits from the beginning of the buffer.
2339 *
2340 * Same idea as with net_buf_pull(), but a helper for operating on
2341 * 24-bit little endian data.
2342 *
2343 * @param buf A valid pointer on a buffer.
2344 *
2345 * @return 24-bit value converted from little endian to host endian.
2346 */
net_buf_pull_le24(struct net_buf * buf)2347 static inline uint32_t net_buf_pull_le24(struct net_buf *buf)
2348 {
2349 return net_buf_simple_pull_le24(&buf->b);
2350 }
2351
2352 /**
2353 * @brief Remove and convert 24 bits from the beginning of the buffer.
2354 *
2355 * Same idea as with net_buf_pull(), but a helper for operating on
2356 * 24-bit big endian data.
2357 *
2358 * @param buf A valid pointer on a buffer.
2359 *
2360 * @return 24-bit value converted from big endian to host endian.
2361 */
net_buf_pull_be24(struct net_buf * buf)2362 static inline uint32_t net_buf_pull_be24(struct net_buf *buf)
2363 {
2364 return net_buf_simple_pull_be24(&buf->b);
2365 }
2366
2367 /**
2368 * @brief Remove and convert 32 bits from the beginning of the buffer.
2369 *
2370 * Same idea as with net_buf_pull(), but a helper for operating on
2371 * 32-bit little endian data.
2372 *
2373 * @param buf A valid pointer on a buffer.
2374 *
2375 * @return 32-bit value converted from little endian to host endian.
2376 */
net_buf_pull_le32(struct net_buf * buf)2377 static inline uint32_t net_buf_pull_le32(struct net_buf *buf)
2378 {
2379 return net_buf_simple_pull_le32(&buf->b);
2380 }
2381
2382 /**
2383 * @brief Remove and convert 32 bits from the beginning of the buffer.
2384 *
2385 * Same idea as with net_buf_pull(), but a helper for operating on
2386 * 32-bit big endian data.
2387 *
2388 * @param buf A valid pointer on a buffer
2389 *
2390 * @return 32-bit value converted from big endian to host endian.
2391 */
net_buf_pull_be32(struct net_buf * buf)2392 static inline uint32_t net_buf_pull_be32(struct net_buf *buf)
2393 {
2394 return net_buf_simple_pull_be32(&buf->b);
2395 }
2396
2397 /**
2398 * @brief Remove and convert 40 bits from the beginning of the buffer.
2399 *
2400 * Same idea as with net_buf_pull(), but a helper for operating on
2401 * 40-bit little endian data.
2402 *
2403 * @param buf A valid pointer on a buffer.
2404 *
2405 * @return 40-bit value converted from little endian to host endian.
2406 */
net_buf_pull_le40(struct net_buf * buf)2407 static inline uint64_t net_buf_pull_le40(struct net_buf *buf)
2408 {
2409 return net_buf_simple_pull_le40(&buf->b);
2410 }
2411
2412 /**
2413 * @brief Remove and convert 40 bits from the beginning of the buffer.
2414 *
2415 * Same idea as with net_buf_pull(), but a helper for operating on
2416 * 40-bit big endian data.
2417 *
2418 * @param buf A valid pointer on a buffer
2419 *
2420 * @return 40-bit value converted from big endian to host endian.
2421 */
net_buf_pull_be40(struct net_buf * buf)2422 static inline uint64_t net_buf_pull_be40(struct net_buf *buf)
2423 {
2424 return net_buf_simple_pull_be40(&buf->b);
2425 }
2426
2427 /**
2428 * @brief Remove and convert 48 bits from the beginning of the buffer.
2429 *
2430 * Same idea as with net_buf_pull(), but a helper for operating on
2431 * 48-bit little endian data.
2432 *
2433 * @param buf A valid pointer on a buffer.
2434 *
2435 * @return 48-bit value converted from little endian to host endian.
2436 */
net_buf_pull_le48(struct net_buf * buf)2437 static inline uint64_t net_buf_pull_le48(struct net_buf *buf)
2438 {
2439 return net_buf_simple_pull_le48(&buf->b);
2440 }
2441
2442 /**
2443 * @brief Remove and convert 48 bits from the beginning of the buffer.
2444 *
2445 * Same idea as with net_buf_pull(), but a helper for operating on
2446 * 48-bit big endian data.
2447 *
2448 * @param buf A valid pointer on a buffer
2449 *
2450 * @return 48-bit value converted from big endian to host endian.
2451 */
net_buf_pull_be48(struct net_buf * buf)2452 static inline uint64_t net_buf_pull_be48(struct net_buf *buf)
2453 {
2454 return net_buf_simple_pull_be48(&buf->b);
2455 }
2456
2457 /**
2458 * @brief Remove and convert 64 bits from the beginning of the buffer.
2459 *
2460 * Same idea as with net_buf_pull(), but a helper for operating on
2461 * 64-bit little endian data.
2462 *
2463 * @param buf A valid pointer on a buffer.
2464 *
2465 * @return 64-bit value converted from little endian to host endian.
2466 */
net_buf_pull_le64(struct net_buf * buf)2467 static inline uint64_t net_buf_pull_le64(struct net_buf *buf)
2468 {
2469 return net_buf_simple_pull_le64(&buf->b);
2470 }
2471
2472 /**
2473 * @brief Remove and convert 64 bits from the beginning of the buffer.
2474 *
2475 * Same idea as with net_buf_pull(), but a helper for operating on
2476 * 64-bit big endian data.
2477 *
2478 * @param buf A valid pointer on a buffer
2479 *
2480 * @return 64-bit value converted from big endian to host endian.
2481 */
net_buf_pull_be64(struct net_buf * buf)2482 static inline uint64_t net_buf_pull_be64(struct net_buf *buf)
2483 {
2484 return net_buf_simple_pull_be64(&buf->b);
2485 }
2486
2487 /**
2488 * @brief Check buffer tailroom.
2489 *
2490 * Check how much free space there is at the end of the buffer.
2491 *
2492 * @param buf A valid pointer on a buffer
2493 *
2494 * @return Number of bytes available at the end of the buffer.
2495 */
net_buf_tailroom(const struct net_buf * buf)2496 static inline size_t net_buf_tailroom(const struct net_buf *buf)
2497 {
2498 return net_buf_simple_tailroom(&buf->b);
2499 }
2500
2501 /**
2502 * @brief Check buffer headroom.
2503 *
2504 * Check how much free space there is in the beginning of the buffer.
2505 *
2506 * buf A valid pointer on a buffer
2507 *
2508 * @return Number of bytes available in the beginning of the buffer.
2509 */
net_buf_headroom(const struct net_buf * buf)2510 static inline size_t net_buf_headroom(const struct net_buf *buf)
2511 {
2512 return net_buf_simple_headroom(&buf->b);
2513 }
2514
2515 /**
2516 * @brief Check maximum net_buf::len value.
2517 *
2518 * This value is depending on the number of bytes being reserved as headroom.
2519 *
2520 * @param buf A valid pointer on a buffer
2521 *
2522 * @return Number of bytes usable behind the net_buf::data pointer.
2523 */
net_buf_max_len(const struct net_buf * buf)2524 static inline uint16_t net_buf_max_len(const struct net_buf *buf)
2525 {
2526 return net_buf_simple_max_len(&buf->b);
2527 }
2528
2529 /**
2530 * @brief Get the tail pointer for a buffer.
2531 *
2532 * Get a pointer to the end of the data in a buffer.
2533 *
2534 * @param buf Buffer.
2535 *
2536 * @return Tail pointer for the buffer.
2537 */
net_buf_tail(const struct net_buf * buf)2538 static inline uint8_t *net_buf_tail(const struct net_buf *buf)
2539 {
2540 return net_buf_simple_tail(&buf->b);
2541 }
2542
2543 /**
2544 * @brief Find the last fragment in the fragment list.
2545 *
2546 * @return Pointer to last fragment in the list.
2547 */
2548 struct net_buf *net_buf_frag_last(struct net_buf *frags);
2549
2550 /**
2551 * @brief Insert a new fragment to a chain of bufs.
2552 *
2553 * Insert a new fragment into the buffer fragments list after the parent.
2554 *
2555 * Note: This function takes ownership of the fragment reference so the
2556 * caller is not required to unref.
2557 *
2558 * @param parent Parent buffer/fragment.
2559 * @param frag Fragment to insert.
2560 */
2561 void net_buf_frag_insert(struct net_buf *parent, struct net_buf *frag);
2562
2563 /**
2564 * @brief Add a new fragment to the end of a chain of bufs.
2565 *
2566 * Append a new fragment into the buffer fragments list.
2567 *
2568 * Note: This function takes ownership of the fragment reference so the
2569 * caller is not required to unref.
2570 *
2571 * @param head Head of the fragment chain.
2572 * @param frag Fragment to add.
2573 *
2574 * @return New head of the fragment chain. Either head (if head
2575 * was non-NULL) or frag (if head was NULL).
2576 */
2577 struct net_buf *net_buf_frag_add(struct net_buf *head, struct net_buf *frag);
2578
2579 /**
2580 * @brief Delete existing fragment from a chain of bufs.
2581 *
2582 * @param parent Parent buffer/fragment, or NULL if there is no parent.
2583 * @param frag Fragment to delete.
2584 *
2585 * @return Pointer to the buffer following the fragment, or NULL if it
2586 * had no further fragments.
2587 */
2588 #if defined(CONFIG_NET_BUF_LOG)
2589 struct net_buf *net_buf_frag_del_debug(struct net_buf *parent,
2590 struct net_buf *frag,
2591 const char *func, int line);
2592 #define net_buf_frag_del(_parent, _frag) \
2593 net_buf_frag_del_debug(_parent, _frag, __func__, __LINE__)
2594 #else
2595 struct net_buf *net_buf_frag_del(struct net_buf *parent, struct net_buf *frag);
2596 #endif
2597
2598 /**
2599 * @brief Copy bytes from net_buf chain starting at offset to linear buffer
2600 *
2601 * Copy (extract) @a len bytes from @a src net_buf chain, starting from @a
2602 * offset in it, to a linear buffer @a dst. Return number of bytes actually
2603 * copied, which may be less than requested, if net_buf chain doesn't have
2604 * enough data, or destination buffer is too small.
2605 *
2606 * @param dst Destination buffer
2607 * @param dst_len Destination buffer length
2608 * @param src Source net_buf chain
2609 * @param offset Starting offset to copy from
2610 * @param len Number of bytes to copy
2611 * @return number of bytes actually copied
2612 */
2613 size_t net_buf_linearize(void *dst, size_t dst_len,
2614 const struct net_buf *src, size_t offset, size_t len);
2615
2616 /**
2617 * @typedef net_buf_allocator_cb
2618 * @brief Network buffer allocator callback.
2619 *
2620 * @details The allocator callback is called when net_buf_append_bytes
2621 * needs to allocate a new net_buf.
2622 *
2623 * @param timeout Affects the action taken should the net buf pool be empty.
2624 * If K_NO_WAIT, then return immediately. If K_FOREVER, then
2625 * wait as long as necessary. Otherwise, wait until the specified
2626 * timeout.
2627 * @param user_data The user data given in net_buf_append_bytes call.
2628 * @return pointer to allocated net_buf or NULL on error.
2629 */
2630 typedef struct net_buf * __must_check (*net_buf_allocator_cb)(k_timeout_t timeout,
2631 void *user_data);
2632
2633 /**
2634 * @brief Append data to a list of net_buf
2635 *
2636 * @details Append data to a net_buf. If there is not enough space in the
2637 * net_buf then more net_buf will be added, unless there are no free net_buf
2638 * and timeout occurs. If not allocator is provided it attempts to allocate from
2639 * the same pool as the original buffer.
2640 *
2641 * @param buf Network buffer.
2642 * @param len Total length of input data
2643 * @param value Data to be added
2644 * @param timeout Timeout is passed to the net_buf allocator callback.
2645 * @param allocate_cb When a new net_buf is required, use this callback.
2646 * @param user_data A user data pointer to be supplied to the allocate_cb.
2647 * This pointer is can be anything from a mem_pool or a net_pkt, the
2648 * logic is left up to the allocate_cb function.
2649 *
2650 * @return Length of data actually added. This may be less than input
2651 * length if other timeout than K_FOREVER was used, and there
2652 * were no free fragments in a pool to accommodate all data.
2653 */
2654 size_t net_buf_append_bytes(struct net_buf *buf, size_t len,
2655 const void *value, k_timeout_t timeout,
2656 net_buf_allocator_cb allocate_cb, void *user_data);
2657
2658 /**
2659 * @brief Match data with a net_buf's content
2660 *
2661 * @details Compare data with a content of a net_buf. Provide information about
2662 * the number of bytes matching between both. If needed, traverse
2663 * through multiple buffer fragments.
2664 *
2665 * @param buf Network buffer
2666 * @param offset Starting offset to compare from
2667 * @param data Data buffer for comparison
2668 * @param len Number of bytes to compare
2669 *
2670 * @return The number of bytes compared before the first difference.
2671 */
2672 size_t net_buf_data_match(const struct net_buf *buf, size_t offset, const void *data, size_t len);
2673
2674 /**
2675 * @brief Skip N number of bytes in a net_buf
2676 *
2677 * @details Skip N number of bytes starting from fragment's offset. If the total
2678 * length of data is placed in multiple fragments, this function will skip from
2679 * all fragments until it reaches N number of bytes. Any fully skipped buffers
2680 * are removed from the net_buf list.
2681 *
2682 * @param buf Network buffer.
2683 * @param len Total length of data to be skipped.
2684 *
2685 * @return Pointer to the fragment or
2686 * NULL and pos is 0 after successful skip,
2687 * NULL and pos is 0xffff otherwise.
2688 */
net_buf_skip(struct net_buf * buf,size_t len)2689 static inline struct net_buf *net_buf_skip(struct net_buf *buf, size_t len)
2690 {
2691 while (buf && len--) {
2692 net_buf_pull_u8(buf);
2693 if (!buf->len) {
2694 buf = net_buf_frag_del(NULL, buf);
2695 }
2696 }
2697
2698 return buf;
2699 }
2700
2701 /**
2702 * @brief Calculate amount of bytes stored in fragments.
2703 *
2704 * Calculates the total amount of data stored in the given buffer and the
2705 * fragments linked to it.
2706 *
2707 * @param buf Buffer to start off with.
2708 *
2709 * @return Number of bytes in the buffer and its fragments.
2710 */
net_buf_frags_len(const struct net_buf * buf)2711 static inline size_t net_buf_frags_len(const struct net_buf *buf)
2712 {
2713 size_t bytes = 0;
2714
2715 while (buf) {
2716 bytes += buf->len;
2717 buf = buf->frags;
2718 }
2719
2720 return bytes;
2721 }
2722
2723 /**
2724 * @}
2725 */
2726
2727 #ifdef __cplusplus
2728 }
2729 #endif
2730
2731 #endif /* ZEPHYR_INCLUDE_NET_BUF_H_ */
2732