pm_buffer.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 BUFFER_H__
  41. #define BUFFER_H__
  42. #include <stdint.h>
  43. #include "compiler_abstraction.h"
  44. #include "sdk_errors.h"
  45. #include "nrf_atflags.h"
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. /**
  50. * @cond NO_DOXYGEN
  51. * @defgroup pm_buffer Buffer
  52. * @ingroup peer_manager
  53. * @{
  54. * @brief An internal module of @ref peer_manager. This module provides a simple buffer.
  55. */
  56. #define PM_BUFFER_INVALID_ID 0xFF //!< Invalid buffer block ID.
  57. /**@brief Convenience macro for declaring memory and initializing a buffer instance.
  58. *
  59. * @param[out] p_buffer The buffer instance to initialize.
  60. * @param[in] n_blocks The desired number of blocks in the buffer.
  61. * @param[in] block_size The desired block size of the buffer.
  62. * @param[out] err_code The return code from @ref pm_buffer_init.
  63. */
  64. #define PM_BUFFER_INIT(p_buffer, n_blocks, block_size, err_code) \
  65. do \
  66. { \
  67. __ALIGN(4) static uint8_t buffer_memory[(n_blocks) * (block_size)]; \
  68. static NRF_ATFLAGS_DEF(mutex_memory, n_blocks); \
  69. err_code = pm_buffer_init((p_buffer), \
  70. buffer_memory, \
  71. (n_blocks) * (block_size), \
  72. mutex_memory, \
  73. (n_blocks), \
  74. (block_size)); \
  75. } while (0)
  76. typedef struct
  77. {
  78. uint8_t * p_memory; /**< The storage for all buffer entries. The size of the buffer must be n_blocks*block_size. */
  79. nrf_atflags_t * p_mutex; /**< A mutex group with one mutex for each buffer entry. */
  80. uint32_t n_blocks; /**< The number of allocatable blocks in the buffer. */
  81. uint32_t block_size; /**< The size of each block in the buffer. */
  82. } pm_buffer_t;
  83. /**@brief Function for initializing a buffer instance.
  84. *
  85. * @param[out] p_buffer The buffer instance to initialize.
  86. * @param[in] p_buffer_memory The memory this buffer will use.
  87. * @param[in] buffer_memory_size The size of p_buffer_memory. This must be at least
  88. * n_blocks*block_size.
  89. * @param[in] p_mutex_memory The memory for the mutexes. This must be at least
  90. * @ref NRF_ATFLAGS_ARRAY_LEN(n_blocks).
  91. * @param[in] n_blocks The number of blocks in the buffer.
  92. * @param[in] block_size The size of each block.
  93. *
  94. * @retval NRF_SUCCESS Successfully initialized buffer instance.
  95. * @retval NRF_ERROR_INVALID_PARAM A parameter was 0 or NULL or a size was too small.
  96. */
  97. ret_code_t pm_buffer_init(pm_buffer_t * p_buffer,
  98. uint8_t * p_buffer_memory,
  99. uint32_t buffer_memory_size,
  100. nrf_atflags_t * p_mutex_memory,
  101. uint32_t n_blocks,
  102. uint32_t block_size);
  103. /**@brief Function for acquiring a buffer block in a buffer.
  104. *
  105. * @param[in] p_buffer The buffer instance acquire from.
  106. * @param[in] n_blocks The number of contiguous blocks to acquire.
  107. *
  108. * @return The id of the acquired block, if successful.
  109. * @retval PM_BUFFER_INVALID_ID If unsuccessful.
  110. */
  111. uint8_t pm_buffer_block_acquire(pm_buffer_t * p_buffer, uint32_t n_blocks);
  112. /**@brief Function for getting a pointer to a specific buffer block.
  113. *
  114. * @param[in] p_buffer The buffer instance get from.
  115. * @param[in] id The id of the buffer to get the pointer for.
  116. *
  117. * @return A pointer to the buffer for the specified id, if the id is valid.
  118. * @retval NULL If the id is invalid.
  119. */
  120. uint8_t * pm_buffer_ptr_get(pm_buffer_t * p_buffer, uint8_t id);
  121. /**@brief Function for releasing a buffer block.
  122. *
  123. * @param[in] p_buffer The buffer instance containing the block to release.
  124. * @param[in] id The id of the block to release.
  125. */
  126. void pm_buffer_release(pm_buffer_t * p_buffer, uint8_t id);
  127. #ifdef __cplusplus
  128. }
  129. #endif
  130. #endif // BUFFER_H__
  131. /**
  132. * @}
  133. * @endcond
  134. */