Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 | /** @file * @brief Buffer management. */ /* * Copyright (c) 2015 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #ifndef __NET_BUF_H #define __NET_BUF_H #include <stddef.h> #include <stdint.h> #include <misc/util.h> #include <zephyr.h> #ifdef __cplusplus extern "C" { #endif /* Alignment needed for various parts of the buffer definition */ #define __net_buf_align __aligned(sizeof(int)) /** @def NET_BUF_SIMPLE * @brief Define a net_buf_simple stack variable and get a pointer to it. * * This is a helper macro which is used to define a net_buf_simple object on * the stack and the get a pointer to it as follows: * * struct net_buf_simple *my_buf = NET_BUF_SIMPLE(10); * * After creating the object it needs to be initialized by calling * net_buf_simple_init(). * * @param _size Maximum data storage for the buffer. * * @return Pointer to stack-allocated net_buf_simple object. */ #define NET_BUF_SIMPLE(_size) \ ((struct net_buf_simple *)(&(struct { \ struct net_buf_simple buf; \ uint8_t data[_size] __net_buf_align; \ }) { \ .buf.size = _size, \ })) /** @brief Simple network buffer representation. * * This is a simpler variant of the net_buf object (in fact net_buf uses * net_buf_simple internally). It doesn't provide any kind of reference * counting, user data, dynamic allocation, or in general the ability to * pass through nano-kernel objects such as FIFOs. * * The main use of this is for scenarios where the meta-data of the normal * net_buf isn't needed and causes too much overhead. This could be e.g. * when the buffer only needs to be allocated on the stack or when the * access to and lifetime of the buffer is well controlled and constrained. * */ struct net_buf_simple { /** Pointer to the start of data in the buffer. */ uint8_t *data; /** Length of the data behind the data pointer. */ uint16_t len; /** Amount of data that this buffer can store. */ uint16_t size; /** Start of the data storage. Not to be accessed directly * (the data pointer should be used instead). */ uint8_t __buf[0] __net_buf_align; }; /** @brief Initialize a net_buf_simple object. * * This needs to be called after creating a net_buf_simple object e.g. using * the NET_BUF_SIMPLE macro. * * @param buf Buffer to initialize. * @param reserve_head Headroom to reserve. */ static inline void net_buf_simple_init(struct net_buf_simple *buf, size_t reserve_head) { buf->data = buf->__buf + reserve_head; buf->len = 0; } /** * @brief Prepare data to be added at the end of the buffer * * Increments the data length of a buffer to account for more data * at the end. * * @param buf Buffer to update. * @param len Number of bytes to increment the length with. * * @return The original tail of the buffer. */ void *net_buf_simple_add(struct net_buf_simple *buf, size_t len); /** * @brief Copy bytes from memory to the end of the buffer * * Copies the given number of bytes to the end of the buffer. Increments the * data length of the buffer to account for more data at the end. * * @param buf Buffer to update. * @param mem Location of data to be added. * @param len Length of data to be added * * @return The original tail of the buffer. */ void *net_buf_simple_add_mem(struct net_buf_simple *buf, const void *mem, size_t len); /** * @brief Add (8-bit) byte at the end of the buffer * * Adds a byte at the end of the buffer. Increments the data length of * the buffer to account for more data at the end. * * @param buf Buffer to update. * @param val byte value to be added. * * @return Pointer to the value added */ uint8_t *net_buf_simple_add_u8(struct net_buf_simple *buf, uint8_t val); /** * @brief Add 16-bit value at the end of the buffer * * Adds 16-bit value in little endian format at the end of buffer. * Increments the data length of a buffer to account for more data * at the end. * * @param buf Buffer to update. * @param val 16-bit value to be added. */ void net_buf_simple_add_le16(struct net_buf_simple *buf, uint16_t val); /** * @brief Add 16-bit value at the end of the buffer * * Adds 16-bit value in big endian format at the end of buffer. * Increments the data length of a buffer to account for more data * at the end. * * @param buf Buffer to update. * @param val 16-bit value to be added. */ void net_buf_simple_add_be16(struct net_buf_simple *buf, uint16_t val); /** * @brief Add 32-bit value at the end of the buffer * * Adds 32-bit value in little endian format at the end of buffer. * Increments the data length of a buffer to account for more data * at the end. * * @param buf Buffer to update. * @param val 32-bit value to be added. */ void net_buf_simple_add_le32(struct net_buf_simple *buf, uint32_t val); /** * @brief Add 32-bit value at the end of the buffer * * Adds 32-bit value in big endian format at the end of buffer. * Increments the data length of a buffer to account for more data * at the end. * * @param buf Buffer to update. * @param val 32-bit value to be added. */ void net_buf_simple_add_be32(struct net_buf_simple *buf, uint32_t val); /** * @brief Push data to the beginning of the buffer. * * Modifies the data pointer and buffer length to account for more data * in the beginning of the buffer. * * @param buf Buffer to update. * @param len Number of bytes to add to the beginning. * * @return The new beginning of the buffer data. */ void *net_buf_simple_push(struct net_buf_simple *buf, size_t len); /** * @brief Push 16-bit value to the beginning of the buffer * * Adds 16-bit value in little endian format to the beginning of the * buffer. * * @param buf Buffer to update. * @param val 16-bit value to be pushed to the buffer. */ void net_buf_simple_push_le16(struct net_buf_simple *buf, uint16_t val); /** * @brief Push 16-bit value to the beginning of the buffer * * Adds 16-bit value in big endian format to the beginning of the * buffer. * * @param buf Buffer to update. * @param val 16-bit value to be pushed to the buffer. */ void net_buf_simple_push_be16(struct net_buf_simple *buf, uint16_t val); /** * @brief Push 8-bit value to the beginning of the buffer * * Adds 8-bit value the beginning of the buffer. * * @param buf Buffer to update. * @param val 8-bit value to be pushed to the buffer. */ void net_buf_simple_push_u8(struct net_buf_simple *buf, uint8_t val); /** * @brief Remove data from the beginning of the buffer. * * Removes data from the beginnig of the buffer by modifying the data * pointer and buffer length. * * @param buf Buffer to update. * @param len Number of bytes to remove. * * @return New beginning of the buffer data. */ void *net_buf_simple_pull(struct net_buf_simple *buf, size_t len); /** * @brief Remove a 8-bit value from the beginning of the buffer * * Same idea as with net_buf_simple_pull(), but a helper for operating * on 8-bit values. * * @param buf A valid pointer on a buffer. * * @return The 8-bit removed value */ uint8_t net_buf_simple_pull_u8(struct net_buf_simple *buf); /** * @brief Remove and convert 16 bits from the beginning of the buffer. * * Same idea as with net_buf_simple_pull(), but a helper for operating * on 16-bit little endian data. * * @param buf A valid pointer on a buffer. * * @return 16-bit value converted from little endian to host endian. */ uint16_t net_buf_simple_pull_le16(struct net_buf_simple *buf); /** * @brief Remove and convert 16 bits from the beginning of the buffer. * * Same idea as with net_buf_simple_pull(), but a helper for operating * on 16-bit big endian data. * * @param buf A valid pointer on a buffer. * * @return 16-bit value converted from big endian to host endian. */ uint16_t net_buf_simple_pull_be16(struct net_buf_simple *buf); /** * @brief Remove and convert 32 bits from the beginning of the buffer. * * Same idea as with net_buf_simple_pull(), but a helper for operating * on 32-bit little endian data. * * @param buf A valid pointer on a buffer. * * @return 32-bit value converted from little endian to host endian. */ uint32_t net_buf_simple_pull_le32(struct net_buf_simple *buf); /** * @brief Remove and convert 32 bits from the beginning of the buffer. * * Same idea as with net_buf_simple_pull(), but a helper for operating * on 32-bit big endian data. * * @param buf A valid pointer on a buffer. * * @return 32-bit value converted from big endian to host endian. */ uint32_t net_buf_simple_pull_be32(struct net_buf_simple *buf); /** * @brief Get the tail pointer for a buffer. * * Get a pointer to the end of the data in a buffer. * * @param buf Buffer. * * @return Tail pointer for the buffer. */ static inline uint8_t *net_buf_simple_tail(struct net_buf_simple *buf) { return buf->data + buf->len; } /** * @brief Check buffer headroom. * * Check how much free space there is in the beginning of the buffer. * * buf A valid pointer on a buffer * * @return Number of bytes available in the beginning of the buffer. */ size_t net_buf_simple_headroom(struct net_buf_simple *buf); /** * @brief Check buffer tailroom. * * Check how much free space there is at the end of the buffer. * * @param buf A valid pointer on a buffer * * @return Number of bytes available at the end of the buffer. */ size_t net_buf_simple_tailroom(struct net_buf_simple *buf); /** * @brief Parsing state of a buffer. * * This is used for temporarily storing the parsing state of a buffer * while giving control of the parsing to a routine which we don't * control. */ struct net_buf_simple_state { /** Offset of the data pointer from the beginning of the storage */ uint16_t offset; /** Length of data */ uint16_t len; }; /** * @brief Save the parsing state of a buffer. * * Saves the parsing state of a buffer so it can be restored later. * * @param buf Buffer from which the state should be saved. * @param state Storage for the state. */ static inline void net_buf_simple_save(struct net_buf_simple *buf, struct net_buf_simple_state *state) { state->offset = net_buf_simple_headroom(buf); state->len = buf->len; } /** * @brief Restore the parsing state of a buffer. * * Restores the parsing state of a buffer from a state previously stored * by net_buf_simple_save(). * * @param buf Buffer to which the state should be restored. * @param state Stored state. */ static inline void net_buf_simple_restore(struct net_buf_simple *buf, struct net_buf_simple_state *state) { buf->data = buf->__buf + state->offset; buf->len = state->len; } /** Flag indicating that the buffer has associated fragments. Only used * internally by the buffer handling code while the buffer is inside a * FIFO, meaning this never needs to be explicitly set or unset by the * net_buf API user. As long as the buffer is outside of a FIFO, i.e. * in practice always for the user for this API, the buf->frags pointer * should be used instead. */ #define NET_BUF_FRAGS BIT(0) /** @brief Network buffer representation. * * This struct is used to represent network buffers. Such buffers are * normally defined through the NET_BUF_POOL_DEFINE() API and allocated * using the net_buf_alloc() API. */ struct net_buf { union { /** FIFO uses first 4 bytes itself, reserve space */ int _unused; /** Fragments associated with this buffer. */ struct net_buf *frags; }; #if defined(CONFIG_NET_TCP) /** List pointer used for TCP retransmit buffering */ sys_snode_t sent_list; #endif /* CONFIG_NET_TCP */ /** Reference count. */ uint8_t ref; /** Bit-field of buffer flags. */ uint8_t flags; /** Where the buffer should go when freed up. */ struct net_buf_pool *pool; /* Union for convenience access to the net_buf_simple members, also * preserving the old API. */ union { /* The ABI of this struct must match net_buf_simple */ struct { /** Pointer to the start of data in the buffer. */ uint8_t *data; /** Length of the data behind the data pointer. */ uint16_t len; /** Amount of data that this buffer can store. */ uint16_t size; }; struct net_buf_simple b; }; /** Start of the data storage. Not to be accessed directly * (the data pointer should be used instead). */ uint8_t __buf[0] __net_buf_align; }; struct net_buf_pool { /** LIFO to place the buffer into when free */ struct k_lifo free; /** Number of buffers in pool */ const uint16_t buf_count; /** Number of uninitialized buffers */ uint16_t uninit_count; /** Data size of each buffer in the pool */ const uint16_t buf_size; /** Size of the user data associated with each buffer. */ const uint16_t user_data_size; /** Optional destroy callback when buffer is freed. */ void (*const destroy)(struct net_buf *buf); /** Helper to access the start of storage (for net_buf_pool_init) */ struct net_buf * const __bufs; }; #define NET_BUF_POOL_INITIALIZER(_pool, _bufs, _count, _size, _ud_size, \ _destroy) \ { \ .free = K_LIFO_INITIALIZER(_pool.free), \ .__bufs = (struct net_buf *)_bufs, \ .buf_count = _count, \ .uninit_count = _count, \ .buf_size = _size, \ .user_data_size = _ud_size, \ .destroy = _destroy, \ } /** @def NET_BUF_POOL_DEFINE * @brief Define a new pool for buffers * * Defines a net_buf_pool struct and the necessary memory storage (array of * structs) for the needed amount of buffers. After this,the buffers can be * accessed from the pool through net_buf_alloc. The pool is defined as a * static variable, so if it needs to be exported outside the current module * this needs to happen with the help of a separate pointer rather than an * extern declaration. * * If provided with a custom destroy callback this callback is * responsible for eventually calling net_buf_destroy() to complete the * process of returning the buffer to the pool. * * @param _name Name of the pool variable. * @param _count Number of buffers in the pool. * @param _size Maximum data size for each buffer. * @param _ud_size Amount of user data space to reserve. * @param _destroy Optional destroy callback when buffer is freed. */ #define NET_BUF_POOL_DEFINE(_name, _count, _size, _ud_size, _destroy) \ static struct { \ struct net_buf buf; \ uint8_t data[_size] __net_buf_align; \ uint8_t ud[ROUND_UP(_ud_size, 4)] __net_buf_align; \ } _net_buf_pool_##_name[_count] __noinit; \ static struct net_buf_pool _name = \ NET_BUF_POOL_INITIALIZER(_name, _net_buf_pool_##_name, \ _count, _size, _ud_size, _destroy) /** * @brief Allocate a new buffer from a pool. * * Allocate a new buffer from a pool. * * @param pool Which pool to allocate the buffer from. * @param timeout Affects the action taken should the pool be empty. * If K_NO_WAIT, then return immediately. If K_FOREVER, then * wait as long as necessary. Otherwise, wait up to the specified * number of milliseconds before timing out. * * @return New buffer or NULL if out of buffers. */ #if defined(CONFIG_NET_BUF_LOG) struct net_buf *net_buf_alloc_debug(struct net_buf_pool *pool, int32_t timeout, const char *func, int line); #define net_buf_alloc(_pool, _timeout) \ net_buf_alloc_debug(_pool, _timeout, __func__, __LINE__) #else struct net_buf *net_buf_alloc(struct net_buf_pool *pool, int32_t timeout); #endif /** * @brief Get a buffer from a FIFO. * * Get buffer from a FIFO. * * @param fifo Which FIFO to take the buffer from. * @param timeout Affects the action taken should the FIFO be empty. * If K_NO_WAIT, then return immediately. If K_FOREVER, then wait as * long as necessary. Otherwise, wait up to the specified number of * milliseconds before timing out. * * @return New buffer or NULL if the FIFO is empty. */ #if defined(CONFIG_NET_BUF_LOG) struct net_buf *net_buf_get_debug(struct k_fifo *fifo, int32_t timeout, const char *func, int line); #define net_buf_get(_fifo, _timeout) \ net_buf_get_debug(_fifo, _timeout, __func__, __LINE__) #else struct net_buf *net_buf_get(struct k_fifo *fifo, int32_t timeout); #endif /** * @brief Destroy buffer from custom destroy callback * * This helper is only intended to be used from custom destroy callbacks. * If no custom destroy callback is given to NET_BUF_POOL_DEFINE() then * there is no need to use this API. * * @param buf Buffer to destroy. */ static inline void net_buf_destroy(struct net_buf *buf) { k_lifo_put(&buf->pool->free, buf); } /** * @brief Initialize buffer with the given headroom. * * Initializes a buffer with a given headroom. The buffer is not expected to * contain any data when this API is called. * * @param buf Buffer to initialize. * @param reserve How much headroom to reserve. */ void net_buf_reserve(struct net_buf *buf, size_t reserve); /** * @brief Put a buffer into a FIFO * * Put a buffer to the end of a FIFO. If the buffer contains follow-up * fragments this function will take care of inserting them as well * into the FIFO. * * @param fifo Which FIFO to put the buffer to. * @param buf Buffer. */ void net_buf_put(struct k_fifo *fifo, struct net_buf *buf); /** * @brief Decrements the reference count of a buffer. * * Decrements the reference count of a buffer and puts it back into the * pool if the count reaches zero. * * @param buf A valid pointer on a buffer */ #if defined(CONFIG_NET_BUF_LOG) void net_buf_unref_debug(struct net_buf *buf, const char *func, int line); #define net_buf_unref(_buf) \ net_buf_unref_debug(_buf, __func__, __LINE__) #else void net_buf_unref(struct net_buf *buf); #endif /** * @brief Increment the reference count of a buffer. * * @param buf A valid pointer on a buffer * * @return the buffer newly referenced */ struct net_buf *net_buf_ref(struct net_buf *buf); /** * @brief Duplicate buffer * * Duplicate given buffer including any data and headers currently stored. * * @param buf A valid pointer on a buffer * @param timeout Affects the action taken should the pool be empty. * If K_NO_WAIT, then return immediately. If K_FOREVER, then * wait as long as necessary. Otherwise, wait up to the specified * number of milliseconds before timing out. * * @return Duplicated buffer or NULL if out of buffers. */ struct net_buf *net_buf_clone(struct net_buf *buf, int32_t timeout); /** * @brief Get a pointer to the user data of a buffer. * * @param buf A valid pointer on a buffer * * @return Pointer to the user data of the buffer. */ static inline void *net_buf_user_data(struct net_buf *buf) { return (void *)ROUND_UP((buf->__buf + buf->size), sizeof(int)); } /** * @def net_buf_add * @brief Prepare data to be added at the end of the buffer * * Increments the data length of a buffer to account for more data * at the end. * * @param buf Buffer to update. * @param len Number of bytes to increment the length with. * * @return The original tail of the buffer. */ #define net_buf_add(buf, len) net_buf_simple_add(&(buf)->b, len) /** * @def net_buf_add_mem * @brief Copy bytes from memory to the end of the buffer * * Copies the given number of bytes to the end of the buffer. Increments the * data length of the buffer to account for more data at the end. * * @param buf Buffer to update. * @param mem Location of data to be added. * @param len Length of data to be added * * @return The original tail of the buffer. */ #define net_buf_add_mem(buf, mem, len) net_buf_simple_add_mem(&(buf)->b, \ mem, len) /** * @def net_buf_add_u8 * @brief Add (8-bit) byte at the end of the buffer * * Adds a byte at the end of the buffer. Increments the data length of * the buffer to account for more data at the end. * * @param buf Buffer to update. * @param val byte value to be added. * * @return Pointer to the value added */ #define net_buf_add_u8(buf, val) net_buf_simple_add_u8(&(buf)->b, val) /** * @def net_buf_add_le16 * @brief Add 16-bit value at the end of the buffer * * Adds 16-bit value in little endian format at the end of buffer. * Increments the data length of a buffer to account for more data * at the end. * * @param buf Buffer to update. * @param val 16-bit value to be added. */ #define net_buf_add_le16(buf, val) net_buf_simple_add_le16(&(buf)->b, val) /** * @def net_buf_add_be16 * @brief Add 16-bit value at the end of the buffer * * Adds 16-bit value in big endian format at the end of buffer. * Increments the data length of a buffer to account for more data * at the end. * * @param buf Buffer to update. * @param val 16-bit value to be added. */ #define net_buf_add_be16(buf, val) net_buf_simple_add_be16(&(buf)->b, val) /** * @def net_buf_add_le32 * @brief Add 32-bit value at the end of the buffer * * Adds 32-bit value in little endian format at the end of buffer. * Increments the data length of a buffer to account for more data * at the end. * * @param buf Buffer to update. * @param val 32-bit value to be added. */ #define net_buf_add_le32(buf, val) net_buf_simple_add_le32(&(buf)->b, val) /** * @def net_buf_add_be32 * @brief Add 32-bit value at the end of the buffer * * Adds 32-bit value in big endian format at the end of buffer. * Increments the data length of a buffer to account for more data * at the end. * * @param buf Buffer to update. * @param val 32-bit value to be added. */ #define net_buf_add_be32(buf, val) net_buf_simple_add_be32(&(buf)->b, val) /** * @def net_buf_push * @brief Push data to the beginning of the buffer. * * Modifies the data pointer and buffer length to account for more data * in the beginning of the buffer. * * @param buf Buffer to update. * @param len Number of bytes to add to the beginning. * * @return The new beginning of the buffer data. */ #define net_buf_push(buf, len) net_buf_simple_push(&(buf)->b, len) /** * @def net_buf_push_le16 * @brief Push 16-bit value to the beginning of the buffer * * Adds 16-bit value in little endian format to the beginning of the * buffer. * * @param buf Buffer to update. * @param val 16-bit value to be pushed to the buffer. */ #define net_buf_push_le16(buf, val) net_buf_simple_push_le16(&(buf)->b, val) /** * @def net_buf_push_be16 * @brief Push 16-bit value to the beginning of the buffer * * Adds 16-bit value in little endian format to the beginning of the * buffer. * * @param buf Buffer to update. * @param val 16-bit value to be pushed to the buffer. */ #define net_buf_push_be16(buf, val) net_buf_simple_push_be16(&(buf)->b, val) /** * @def net_buf_push_u8 * @brief Push 8-bit value to the beginning of the buffer * * Adds 8-bit value the beginning of the buffer. * * @param buf Buffer to update. * @param val 8-bit value to be pushed to the buffer. */ #define net_buf_push_u8(buf, val) net_buf_simple_push_u8(&(buf)->b, val) /** * @def net_buf_pull * @brief Remove data from the beginning of the buffer. * * Removes data from the beginnig of the buffer by modifying the data * pointer and buffer length. * * @param buf Buffer to update. * @param len Number of bytes to remove. * * @return New beginning of the buffer data. */ #define net_buf_pull(buf, len) net_buf_simple_pull(&(buf)->b, len) /** * @def net_buf_pull_u8 * @brief Remove a 8-bit value from the beginning of the buffer * * Same idea as with net_buf_pull(), but a helper for operating on * 8-bit values. * * @param buf A valid pointer on a buffer. * * @return The 8-bit removed value */ #define net_buf_pull_u8(buf) net_buf_simple_pull_u8(&(buf)->b) /** * @def net_buf_pull_le16 * @brief Remove and convert 16 bits from the beginning of the buffer. * * Same idea as with net_buf_pull(), but a helper for operating on * 16-bit little endian data. * * @param buf A valid pointer on a buffer. * * @return 16-bit value converted from little endian to host endian. */ #define net_buf_pull_le16(buf) net_buf_simple_pull_le16(&(buf)->b) /** * @def net_buf_pull_be16 * @brief Remove and convert 16 bits from the beginning of the buffer. * * Same idea as with net_buf_pull(), but a helper for operating on * 16-bit big endian data. * * @param buf A valid pointer on a buffer. * * @return 16-bit value converted from big endian to host endian. */ #define net_buf_pull_be16(buf) net_buf_simple_pull_be16(&(buf)->b) /** * @def net_buf_pull_le32 * @brief Remove and convert 32 bits from the beginning of the buffer. * * Same idea as with net_buf_pull(), but a helper for operating on * 32-bit little endian data. * * @param buf A valid pointer on a buffer. * * @return 32-bit value converted from little endian to host endian. */ #define net_buf_pull_le32(buf) net_buf_simple_pull_le32(&(buf)->b) /** * @def net_buf_pull_be32 * @brief Remove and convert 32 bits from the beginning of the buffer. * * Same idea as with net_buf_pull(), but a helper for operating on * 32-bit big endian data. * * @param buf A valid pointer on a buffer * * @return 32-bit value converted from big endian to host endian. */ #define net_buf_pull_be32(buf) net_buf_simple_pull_be32(&(buf)->b) /** * @def net_buf_tailroom * @brief Check buffer tailroom. * * Check how much free space there is at the end of the buffer. * * @param buf A valid pointer on a buffer * * @return Number of bytes available at the end of the buffer. */ #define net_buf_tailroom(buf) net_buf_simple_tailroom(&(buf)->b) /** * @def net_buf_headroom * @brief Check buffer headroom. * * Check how much free space there is in the beginning of the buffer. * * buf A valid pointer on a buffer * * @return Number of bytes available in the beginning of the buffer. */ #define net_buf_headroom(buf) net_buf_simple_headroom(&(buf)->b) /** * @def net_buf_tail * @brief Get the tail pointer for a buffer. * * Get a pointer to the end of the data in a buffer. * * @param buf Buffer. * * @return Tail pointer for the buffer. */ #define net_buf_tail(buf) net_buf_simple_tail(&(buf)->b) /** @brief Find the last fragment in the fragment list. * * @return Pointer to last fragment in the list. */ struct net_buf *net_buf_frag_last(struct net_buf *frags); /** @brief Insert a new fragment to a chain of bufs. * * Insert a new fragment into the buffer fragments list after the parent. * * Note: This function takes ownership of the fragment reference so the * caller is not required to unref. * * @param parent Parent buffer/fragment. * @param frag Fragment to insert. */ void net_buf_frag_insert(struct net_buf *parent, struct net_buf *frag); /** @brief Add a new fragment to the end of a chain of bufs. * * Append a new fragment into the buffer fragments list. * * Note: This function takes ownership of the fragment reference so the * caller is not required to unref. * * @param head Head of the fragment chain. * @param frag Fragment to add. * * @return New head of the fragment chain. Either head (if head * was non-NULL) or frag (if head was NULL). */ struct net_buf *net_buf_frag_add(struct net_buf *head, struct net_buf *frag); /** @brief Delete existing fragment from a chain of bufs. * * @param parent Parent buffer/fragment, or NULL if there is no parent. * @param frag Fragment to delete. * * @return Pointer to the buffer following the fragment, or NULL if it * had no further fragments. */ #if defined(CONFIG_NET_BUF_LOG) struct net_buf *net_buf_frag_del_debug(struct net_buf *parent, struct net_buf *frag, const char *func, int line); #define net_buf_frag_del(_parent, _frag) \ net_buf_frag_del_debug(_parent, _frag, __func__, __LINE__) #else struct net_buf *net_buf_frag_del(struct net_buf *parent, struct net_buf *frag); #endif /** @brief Calculate amount of bytes stored in fragments. * * Calculates the total amount of data stored in the given buffer and the * fragments linked to it. * * @param buf Buffer to start off with. * * @return Number of bytes in the buffer and its fragments. */ static inline size_t net_buf_frags_len(struct net_buf *buf) { size_t bytes = 0; while (buf) { bytes += buf->len; buf = buf->frags; } return bytes; } #ifdef __cplusplus } #endif #endif /* __NET_BUF_H */ |