mbuf.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * Copyright (c) 2015 - 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. #ifndef MBUF_H__
  41. #define MBUF_H__
  42. /**
  43. * @file mbuf.h
  44. *
  45. * @defgroup iot_socket_mbuf Memory management for socket
  46. * @ingroup iot_sdk_socket
  47. * @{
  48. * @brief Memory management for socket.
  49. */
  50. #include <stdbool.h>
  51. #include "nrf_fifo.h"
  52. #ifdef __cplusplus
  53. extern "C" {
  54. #endif
  55. /**@brief Function for reading data from a buffer. */
  56. typedef uint32_t (*mbuf_read_fn)(void * p_ctx,
  57. uint32_t read_offset,
  58. uint8_t * p_dest,
  59. uint32_t dest_len);
  60. /**@brief Function for checking buffer length. */
  61. typedef uint32_t (*mbuf_buf_len_fn)(void * p_ctx);
  62. /**@brief Function for freeing a buffer. */
  63. typedef void (*mbuf_free_fn)(void * p_ctx);
  64. /**@brief Memory management structure. */
  65. typedef struct
  66. {
  67. nrf_fifo_t fifo; /**< FIFO for storing data buffers. */
  68. mbuf_read_fn read; /**< Function for reading data from a buffer. */
  69. mbuf_buf_len_fn buf_len; /**< Function for checking buffer length. */
  70. mbuf_free_fn free; /**< Function for freeing a buffer. */
  71. uint32_t read_pos; /**< Read position in the currently processed buffer. */
  72. void * p_current; /**< Pointer to the currently processed buffer. */
  73. } mbuf_t;
  74. /**
  75. * @brief Function for initializing the memory buffer manager.
  76. *
  77. * This function allocates resources for the mbuf FIFO and initializes the mbuf.
  78. *
  79. * @param[in, out] p_mbuf Pointer to the mbuf structure to initialize.
  80. * @param[in] read_fn Function for reading data from a buffer.
  81. * @param[in] buf_len_fn Function for checking buffer length.
  82. * @param[in] free_fn Function for freeing a buffer.
  83. * @param[in] nmemb Maximum number of data buffers.
  84. *
  85. * @return NRF_SUCCESS on success, otherwise error code is returned.
  86. */
  87. uint32_t mbuf_init(mbuf_t * p_mbuf,
  88. mbuf_read_fn read_fn,
  89. mbuf_buf_len_fn buf_len_fn,
  90. mbuf_free_fn free_fn,
  91. uint32_t nmemb);
  92. /**
  93. * @brief Function for deinitializing the memory buffer manager.
  94. *
  95. * This function releases any resources allocated for mbuf instance pointed by p_mbuf.
  96. *
  97. * @param[in, out] p_mbuf Pointer to the mbuf structure to deinitialize.
  98. */
  99. void mbuf_deinit(mbuf_t * p_mbuf);
  100. /**
  101. * @brief Function for putting a data buffer in the mbuf.
  102. *
  103. * @param[in, out] p_mbuf Pointer to the mbuf structure that shall store the buffer.
  104. * @param[in] p_ctx Pointer to the data buffer to store.
  105. *
  106. * @return NRF_SUCCESS on success, otherwise error code is returned.
  107. */
  108. uint32_t mbuf_write(mbuf_t * p_mbuf, void * p_ctx);
  109. /**
  110. * @brief Function for reading data from the mbuf.
  111. *
  112. * @param[in, out] p_mbuf Pointer to the mbuf structure to read data from.
  113. * @param[in] p_buf Pointer to the buffer where data shall be read from.
  114. * @param[out] buf_size Size of the buffer pointed by p_buf.
  115. *
  116. * @return NRF_SUCCESS on success, otherwise error code is returned.
  117. */
  118. uint32_t mbuf_read(mbuf_t * p_mbuf, void * p_buf, uint32_t buf_size);
  119. /**
  120. * @brief Function for checking if the mbuf is empty.
  121. *
  122. * @param[in] p_mbuf Pointer to the mbuf structure that shall be checked.
  123. *
  124. * @return True if mbuf is empty, false otherwise.
  125. */
  126. bool mbuf_empty(mbuf_t * p_mbuf);
  127. #ifdef __cplusplus
  128. }
  129. #endif
  130. /**@} */
  131. #endif // MBUF_H__