app_mailbox.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. /** @file
  13. *
  14. * @defgroup app_mailbox Mailbox library
  15. * @{
  16. * @ingroup app_common
  17. *
  18. * @brief Mailbox for safely queuing items.
  19. *
  20. */
  21. #ifndef _APP_MAILBOX_H
  22. #define _APP_MAILBOX_H
  23. #include <stdint.h>
  24. #include "sdk_errors.h"
  25. #include "app_util.h"
  26. #include "nordic_common.h"
  27. /**
  28. * @brief Supported overflow modes.
  29. */
  30. typedef enum
  31. {
  32. APP_MAILBOX_MODE_NO_OVERFLOW, //!< If the mailbox is full, @ref app_mailbox_put does not add a new element.
  33. APP_MAILBOX_MODE_OVERFLOW //!< If the mailbox is full, the oldest element is lost and a new one is added.
  34. } app_mailbox_overflow_mode_t;
  35. #include "app_mailbox_local.h"
  36. /**
  37. * @brief Mailbox definition structure.
  38. */
  39. typedef struct
  40. {
  41. void * p_pool; /**< Memory array for mail. */
  42. app_mailbox_cb_t * p_cb; /**< Mailbox handle. */
  43. uint16_t item_sz; /**< Size of a single item. */
  44. uint8_t queue_sz; /**< Capacity of the queue. */
  45. } app_mailbox_t;
  46. /**
  47. * @brief Macro to statically allocate memory for a given mailbox queue.
  48. */
  49. #define APP_MAILBOX_DEF(name, QUEUE_SZ, ITEM_SZ) \
  50. static uint32_t STRING_CONCATENATE(mailbox_items_,name)[(1+CEIL_DIV((ITEM_SZ),4))*(QUEUE_SZ)];\
  51. static app_mailbox_cb_t STRING_CONCATENATE(mailbox_cb_,name); \
  52. const app_mailbox_t name = \
  53. { \
  54. .p_pool = STRING_CONCATENATE(mailbox_items_,name), \
  55. .p_cb = STRING_CONCATENATE(&mailbox_cb_,name), \
  56. .queue_sz = (uint8_t)(QUEUE_SZ), \
  57. .item_sz = (uint16_t)(ITEM_SZ), \
  58. }
  59. /**
  60. * @brief Function for creating a mailbox queue.
  61. *
  62. * This function creates and initializes a mailbox queue.
  63. *
  64. * @param[in] p_mailbox Pointer to the mailbox.
  65. *
  66. * @retval NRF_SUCCESS If the queue was successfully created.
  67. */
  68. ret_code_t app_mailbox_create(const app_mailbox_t * p_mailbox);
  69. /**
  70. * @brief Function for putting an item in the mailbox queue.
  71. *
  72. * @param[in] p_mailbox Pointer to the mailbox.
  73. * @param[in] p_item Pointer to the item to be queued.
  74. *
  75. * @retval NRF_SUCCESS If the item was enqueued.
  76. * @retval NRF_ERROR_NO_MEM If the queue is full.
  77. */
  78. ret_code_t app_mailbox_put (const app_mailbox_t * p_mailbox, void * p_item);
  79. /**
  80. * @brief Function for putting an item with a specified size in the mailbox queue.
  81. *
  82. * @param[in] p_mailbox Pointer to the mailbox.
  83. * @param[in] p_item Pointer to the item to be queued.
  84. * @param[in] size Size of the item.
  85. *
  86. * @retval NRF_SUCCESS If the item was enqueued.
  87. * @retval NRF_ERROR_NO_MEM If the queue is full.
  88. */
  89. ret_code_t app_mailbox_sized_put (const app_mailbox_t * p_mailbox, void * p_item, uint16_t size);
  90. /**
  91. * @brief Function for getting an item from the mailbox queue.
  92. *
  93. * @param[in] p_mailbox Pointer to the mailbox.
  94. * @param[out] p_item Pointer to the output location for the dequeued item.
  95. *
  96. * @retval NRF_SUCCESS If the item was retrieved successfully.
  97. * @retval NRF_ERROR_NO_MEM If the queue is empty.
  98. */
  99. ret_code_t app_mailbox_get (const app_mailbox_t * p_mailbox, void * p_item);
  100. /**
  101. * @brief Function for getting an item and its size from the mailbox queue.
  102. *
  103. * @param[in] p_mailbox Pointer to the mailbox.
  104. * @param[out] p_item Pointer to the output location for the dequeued item.
  105. * @param[out] p_size Pointer to the item size.
  106. *
  107. * @retval NRF_SUCCESS If the item was retrieved successfully.
  108. * @retval NRF_ERROR_NO_MEM If the queue is empty.
  109. */
  110. ret_code_t app_mailbox_sized_get (const app_mailbox_t * p_mailbox, void * p_item, uint16_t * p_size);
  111. /**
  112. * @brief Function for getting the current length of the mailbox queue.
  113. *
  114. * @param[in] p_mailbox Pointer to the mailbox.
  115. *
  116. * @return Current number of elements in the queue.
  117. *
  118. */
  119. uint32_t app_mailbox_length_get (const app_mailbox_t * p_mailbox);
  120. /**
  121. * @brief Function for changing the mode of overflow handling.
  122. *
  123. * @param[in] p_mailbox Pointer to the mailbox.
  124. * @param mode New mode to set.
  125. */
  126. void app_mailbox_mode_set(const app_mailbox_t * p_mailbox, app_mailbox_overflow_mode_t mode);
  127. #endif //_APP_MAILBOX_H
  128. /** @} */