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