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