iot_pbuffer.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * Copyright (c) 2014 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. *
  42. * @defgroup iot_pbuffer Packet Buffer
  43. * @{
  44. * @ingroup iot_sdk_stack
  45. * @brief Packet buffer management for IPv6 stack layers to minimize data copy across stack layers.
  46. *
  47. * @details This module interfaces with the Memory Manager to allocate packet buffers
  48. * for the IPv6 stack layers, without each layer having to ensure
  49. * sufficient header space for layers below.
  50. */
  51. #ifndef IOT_PBUFFER__
  52. #define IOT_PBUFFER__
  53. #include <stdint.h>
  54. #include <stdbool.h>
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58. /**@brief IPv6 packet type identifiers that are needed to ensure that enough
  59. * space is reserved for headers from layers below during memory allocation.
  60. */
  61. typedef enum
  62. {
  63. UNASSIGNED_TYPE = 0, /**< Indicates that the packet buffer is unassigned and not in use. */
  64. RAW_PACKET_TYPE = 1, /**< Raw packet, with no room made for headers of any lower layer. */
  65. IPV6_PACKET_TYPE = 2, /**< Indicates that the packet buffer is requested for an entire IPv6 packet; pbuffer provisions 40 bytes of IPv6 header. */
  66. ICMP6_PACKET_TYPE = 3, /**< Indicates that the packet buffer is requested for an ICMPv6 packet, and provision for 40 bytes of IPv6 header is made by pbuffer. */
  67. UDP6_PACKET_TYPE = 4, /**< Indicates that the packet buffer is requested for a UDP packet, and provision for 40 bytes of IPv6 header and UDP header is made by pbuffer. */
  68. COAP_PACKET_TYPE = 5 /**< Indicates that the packet buffer is requested for a CoAP packet, and provision for 4 bytes of CoAP header, 8 bytes of UDP header, and 40 bytes of IPv6 header is made. */
  69. }iot_pbuffer_type_t;
  70. /**@brief Additional information that must be provided to the module during allocation
  71. * or reallocation to ensure optimal utilization of memory and avoid unnecessary data
  72. * copies.
  73. */
  74. typedef enum
  75. {
  76. PBUFFER_FLAG_DEFAULT = 0, /**< Default behavior with respect to memory allocation when allocating packet buffer. Memory will be allocated for the length indicated by this default.*/
  77. PBUFFER_FLAG_NO_MEM_ALLOCATION = 1, /**< Only allocate packet buffer, not memory. This is needed when a packet already exists and the packet buffer is needed only to feed it to the IPv6 stack.*/
  78. }iot_pbuffer_flags_t;
  79. /**@brief Packet buffer used for exchanging IPv6 payload across layers in both receive and transmit
  80. * paths.
  81. */
  82. typedef struct
  83. {
  84. iot_pbuffer_type_t type; /**< Determines if any offset for lower layers must be provisioned for in the stack. */
  85. uint8_t * p_memory; /**< Pointer to actual memory allocated for the buffer. */
  86. uint8_t * p_payload; /**< Pointer to memory where the payload for the layer that allocates the packet buffer should be contained. */
  87. uint32_t length; /**< Length of the payload of the layer processing it. This value can be modified by each layer of the IPv6 stack based on the required header information added by each.*/
  88. }iot_pbuffer_t;
  89. /**@brief Parameters required to allocate the packet buffer. */
  90. typedef struct
  91. {
  92. iot_pbuffer_type_t type; /**< Payload type for which the packet buffer is requested to be allocated or reallocated. */
  93. iot_pbuffer_flags_t flags; /**< Flags that indicate if memory allocation is needed or not. */
  94. uint32_t length; /**< Length of payload for which the packet buffer is requested. */
  95. }iot_pbuffer_alloc_param_t;
  96. /**@brief Function for initializing the module.
  97. *
  98. * @retval NRF_SUCCESS If the module was successfully initialized. Otherwise, an error code that indicates the reason for the failure is returned.
  99. */
  100. uint32_t iot_pbuffer_init(void);
  101. /**@brief Function for allocating a packet buffer.
  102. *
  103. * @param[in] p_param Pointer to allocation parameters that indicate the length of the payload requested,
  104. * the type of payload, and additional information using the flags. This
  105. * parameter cannot be NULL.
  106. * @param[out] pp_pbuffer Pointer to allocated packet buffer. This parameter shall
  107. * not be NULL.
  108. *
  109. * @retval NRF_SUCCESS If the packet buffer was successfully allocated. Otherwise, an error code that indicates the reason for the failure is returned.
  110. */
  111. uint32_t iot_pbuffer_allocate(iot_pbuffer_alloc_param_t * p_param,
  112. iot_pbuffer_t ** pp_pbuffer);
  113. /**@brief Function for reallocating a packet buffer.
  114. *
  115. * Reallocation requests are treated as follows:
  116. * - If the requested reallocation is less than or equal to the allocated size,
  117. * no data is moved, and the function returns NRF_SUCCESS.
  118. * - If the requested reallocation is more than what is allocated, the function
  119. * requests new memory, backs up existing data, and then frees the previously
  120. * allocated memory.
  121. * - If reallocation is requested with the PBUFFER_FLAG_NO_MEM_ALLOCATION flag,
  122. * the function does not free previously allocated memory or copy it to the
  123. * new location. In this case, the application that uses the pbuffer must
  124. * decide when to move previously allocated memory and when to free it and
  125. * handle this.
  126. *
  127. * @param[in] p_param Pointer to reallocation parameters that indicate the length of the payload requested,
  128. * the type of payload, and additional information using the flags. This
  129. * parameter cannot be NULL.
  130. * @param[in] p_pbuffer Pointer to the packet buffer being reallocated. This parameter shall
  131. * not be NULL.
  132. *
  133. * @retval NRF_SUCCESS If the packet buffer was successfully reallocated. Otherwise, an error code that indicates the reason for the failure is returned.
  134. */
  135. uint32_t iot_pbuffer_reallocate(iot_pbuffer_alloc_param_t * p_param,
  136. iot_pbuffer_t * p_pbuffer);
  137. /**@brief Function for freeing a packet buffer.
  138. *
  139. * This function frees the packet buffer. If the parameter free_flag is set, the
  140. * function tries to free the memory allocated as well. This action is performed
  141. * irrespective of whether the memory was allocated using the PBUFFER_FLAG_DEFAULT or
  142. * the PBUFFER_FLAG_NO_MEM_ALLOCATION flag.
  143. *
  144. * @param[in] p_pbuffer Pointer to the packet buffer requested to be freed. This parameter shall
  145. * not be NULL.
  146. * @param[in] free_flag Indicates if the allocated memory should be freed or not when freeing the
  147. * packet buffer.
  148. *
  149. * @retval NRF_SUCCESS If the packet buffer was successfully freed. Otherwise, an error code that indicates the reason for the failure is returned.
  150. *
  151. */
  152. uint32_t iot_pbuffer_free(iot_pbuffer_t * p_pbuffer, bool free_flag);
  153. #ifdef __cplusplus
  154. }
  155. #endif
  156. #endif // IOT_PBUFFER__
  157. /**@} */