pm_buffer.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #ifndef BUFFER_H__
  13. #define BUFFER_H__
  14. #include <stdint.h>
  15. #include "sdk_errors.h"
  16. #include "pm_mutex.h"
  17. /**
  18. * @cond NO_DOXYGEN
  19. * @defgroup pm_buffer Buffer
  20. * @ingroup peer_manager
  21. * @{
  22. * @brief An internal module of @ref peer_manager. This module provides a simple buffer.
  23. */
  24. #define BUFFER_INVALID_ID 0xFF
  25. #define PM_BUFFER_INIT(p_buffer, n_blocks, block_size, err_code) \
  26. do \
  27. { \
  28. static uint8_t buffer_memory[(n_blocks) * (block_size)]; \
  29. static uint8_t mutex_memory[MUTEX_STORAGE_SIZE(n_blocks)]; \
  30. err_code = pm_buffer_init((p_buffer), \
  31. buffer_memory, \
  32. (n_blocks) * (block_size), \
  33. mutex_memory, \
  34. MUTEX_STORAGE_SIZE(n_blocks), \
  35. (n_blocks), \
  36. (block_size)); \
  37. } while(0)
  38. typedef struct
  39. {
  40. uint8_t * p_memory; /**< The storage for all buffer entries. The size of the buffer must be n_blocks*block_size. */
  41. uint8_t * p_mutex; /**< A mutex group with one mutex for each buffer entry. */
  42. uint32_t n_blocks; /**< The number of allocatable blocks in the buffer. */
  43. uint32_t block_size; /**< The size of each block in the buffer. */
  44. } pm_buffer_t;
  45. /**@brief Function for initializing a buffer instance.
  46. *
  47. * @param[out] p_buffer The buffer instance to initialize.
  48. * @param[in] p_buffer_memory The memory this buffer will use.
  49. * @param[in] buffer_memory_size The size of p_buffer_memory. This must be at least
  50. * n_blocks*block_size.
  51. * @param[in] p_mutex_memory The memory for the mutexes. This must be at least
  52. * @ref MUTEX_STORAGE_SIZE(n_blocks).
  53. * @param[in] mutex_memory_size The size of p_mutex_memory.
  54. * @param[in] n_blocks The number of blocks in the buffer.
  55. * @param[in] block_size The size of each block.
  56. *
  57. * @retval NRF_SUCCESS Successfully initialized buffer instance.
  58. * @retval NRF_ERROR_INVALID_PARAM A parameter was 0 or NULL or a size was too small.
  59. */
  60. ret_code_t pm_buffer_init(pm_buffer_t * p_buffer,
  61. uint8_t * p_buffer_memory,
  62. uint32_t buffer_memory_size,
  63. uint8_t * p_mutex_memory,
  64. uint32_t mutex_memory_size,
  65. uint32_t n_blocks,
  66. uint32_t block_size);
  67. /**@brief Function for acquiring a buffer block in a buffer.
  68. *
  69. * @param[in] p_buffer The buffer instance acquire from.
  70. * @param[in] n_blocks The number of contiguous blocks to acquire.
  71. *
  72. * @return The id of the acquired block, if successful.
  73. * @retval BUFFER_INVALID_ID If unsuccessful.
  74. */
  75. uint8_t pm_buffer_block_acquire(pm_buffer_t * p_buffer, uint32_t n_blocks);
  76. /**@brief Function for getting a pointer to a specific buffer block.
  77. *
  78. * @param[in] p_buffer The buffer instance get from.
  79. * @param[in] id The id of the buffer to get the pointer for.
  80. *
  81. * @return A pointer to the buffer for the specified id, if the id is valid.
  82. * @retval NULL If the id is invalid.
  83. */
  84. uint8_t * pm_buffer_ptr_get(pm_buffer_t * p_buffer, uint8_t id);
  85. /**@brief Function for releasing a buffer block.
  86. *
  87. * @param[in] p_buffer The buffer instance containing the block to release.
  88. * @param[in] id The id of the block to release.
  89. */
  90. void pm_buffer_release(pm_buffer_t * p_buffer, uint8_t id);
  91. #endif // BUFFER_H__
  92. /**
  93. * @}
  94. * @endcond
  95. */