nrf_queue.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /**
  2. * Copyright (c) 2016 - 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. /**
  41. * @defgroup nrf_queue Queue module
  42. * @{
  43. * @ingroup app_common
  44. * @brief Functions that handle the queue instances.
  45. */
  46. #ifndef NRF_QUEUE_H__
  47. #define NRF_QUEUE_H__
  48. #include <stdint.h>
  49. #include <stdint.h>
  50. #include <string.h>
  51. #include "nrf_assert.h"
  52. #include "sdk_errors.h"
  53. #include "app_util.h"
  54. #include "app_util_platform.h"
  55. #include "nrf_log_instance.h"
  56. #include "nrf_section.h"
  57. #ifdef __cplusplus
  58. extern "C" {
  59. #endif
  60. /** @brief Name of the module used for logger messaging.
  61. */
  62. #define NRF_QUEUE_LOG_NAME queue
  63. /**@brief Queue control block. */
  64. typedef struct
  65. {
  66. volatile size_t front; //!< Queue front index.
  67. volatile size_t back; //!< Queue back index.
  68. size_t max_utilization; //!< Maximum utilization of the queue.
  69. } nrf_queue_cb_t;
  70. /**@brief Supported queue modes. */
  71. typedef enum
  72. {
  73. NRF_QUEUE_MODE_OVERFLOW, //!< If the queue is full, new element will overwrite the oldest.
  74. NRF_QUEUE_MODE_NO_OVERFLOW, //!< If the queue is full, new element will not be accepted.
  75. } nrf_queue_mode_t;
  76. /**@brief Instance of the queue. */
  77. typedef struct
  78. {
  79. nrf_queue_cb_t * p_cb; //!< Pointer to the instance control block.
  80. void * p_buffer; //!< Pointer to the memory that is used as storage.
  81. size_t size; //!< Size of the queue.
  82. size_t element_size; //!< Size of one element.
  83. nrf_queue_mode_t mode; //!< Mode of the queue.
  84. #if NRF_QUEUE_CLI_CMDS
  85. const char * p_name; //!< Pointer to string with queue name.
  86. #endif
  87. NRF_LOG_INSTANCE_PTR_DECLARE(p_log) //!< Pointer to instance of the logger object (Conditionally compiled).
  88. } nrf_queue_t;
  89. #if NRF_QUEUE_CLI_CMDS
  90. #define __NRF_QUEUE_ASSIGN_POOL_NAME(_name) .p_name = STRINGIFY(_name),
  91. #else
  92. #define __NRF_QUEUE_ASSIGN_POOL_NAME(_name)
  93. #endif
  94. /**@brief Create a queue instance.
  95. *
  96. * @note This macro reserves memory for the given queue instance.
  97. *
  98. * @param[in] _type Type which is stored.
  99. * @param[in] _name Name of the queue.
  100. * @param[in] _size Size of the queue.
  101. * @param[in] _mode Mode of the queue.
  102. */
  103. #define NRF_QUEUE_DEF(_type, _name, _size, _mode) \
  104. static _type CONCAT_2(_name, _nrf_queue_buffer[(_size) + 1]); \
  105. static nrf_queue_cb_t CONCAT_2(_name, _nrf_queue_cb); \
  106. NRF_LOG_INSTANCE_REGISTER(NRF_QUEUE_LOG_NAME, _name, \
  107. NRF_QUEUE_CONFIG_INFO_COLOR, \
  108. NRF_QUEUE_CONFIG_DEBUG_COLOR, \
  109. NRF_QUEUE_CONFIG_LOG_INIT_FILTER_LEVEL, \
  110. NRF_QUEUE_CONFIG_LOG_ENABLED ? \
  111. NRF_QUEUE_CONFIG_LOG_LEVEL : NRF_LOG_SEVERITY_NONE); \
  112. NRF_SECTION_ITEM_REGISTER(nrf_queue, const nrf_queue_t _name) = \
  113. { \
  114. .p_cb = &CONCAT_2(_name, _nrf_queue_cb), \
  115. .p_buffer = CONCAT_2(_name,_nrf_queue_buffer), \
  116. .size = (_size), \
  117. .element_size = sizeof(_type), \
  118. .mode = _mode, \
  119. __NRF_QUEUE_ASSIGN_POOL_NAME(_name) \
  120. NRF_LOG_INSTANCE_PTR_INIT(p_log, NRF_QUEUE_LOG_NAME, _name) \
  121. }
  122. /**@brief Declare a queue interface.
  123. *
  124. * @param[in] _type Type which is stored.
  125. * @param[in] _name Name of the queue.
  126. */
  127. #define NRF_QUEUE_INTERFACE_DEC(_type, _name) \
  128. ret_code_t _name##_push(_type const * p_element); \
  129. ret_code_t _name##_pop(_type * p_element); \
  130. ret_code_t _name##_peek(_type * p_element); \
  131. ret_code_t _name##_write(_type const * p_data, \
  132. size_t element_count); \
  133. ret_code_t _name##_read(_type * p_data, \
  134. size_t element_count); \
  135. size_t _name##_out(_type * p_data, \
  136. size_t element_count); \
  137. size_t _name##_in(_type const * p_data, \
  138. size_t element_count); \
  139. bool _name##_is_full(void); \
  140. bool _name##_is_empty(void); \
  141. size_t _name##_utilization_get(void); \
  142. size_t _name##_available_get(void); \
  143. size_t _name##_max_utilization_get(void); \
  144. void _name##_reset(void)
  145. /**@brief Define a queue interface.
  146. *
  147. * @param[in] _type Type which is stored.
  148. * @param[in] _name Name of the queue.
  149. * @param[in] _p_queue Queue instance.
  150. */
  151. #define NRF_QUEUE_INTERFACE_DEF(_type, _name, _p_queue) \
  152. ret_code_t _name##_push(_type const * p_element) \
  153. { \
  154. GCC_PRAGMA("GCC diagnostic push") \
  155. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  156. ASSERT((_p_queue) != NULL); \
  157. ASSERT((_p_queue)->element_size == sizeof(_type)); \
  158. GCC_PRAGMA("GCC diagnostic pop") \
  159. return nrf_queue_push((_p_queue), p_element); \
  160. } \
  161. ret_code_t _name##_pop(_type * p_element) \
  162. { \
  163. GCC_PRAGMA("GCC diagnostic push") \
  164. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  165. ASSERT((_p_queue) != NULL); \
  166. ASSERT((_p_queue)->element_size == sizeof(_type)); \
  167. GCC_PRAGMA("GCC diagnostic pop") \
  168. return nrf_queue_pop((_p_queue), p_element); \
  169. } \
  170. ret_code_t _name##_peek(_type * p_element) \
  171. { \
  172. GCC_PRAGMA("GCC diagnostic push") \
  173. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  174. ASSERT((_p_queue) != NULL); \
  175. ASSERT((_p_queue)->element_size == sizeof(_type)); \
  176. GCC_PRAGMA("GCC diagnostic pop") \
  177. return nrf_queue_peek((_p_queue), p_element); \
  178. } \
  179. ret_code_t _name##_write(_type const * p_data, \
  180. size_t element_count) \
  181. { \
  182. GCC_PRAGMA("GCC diagnostic push") \
  183. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  184. ASSERT((_p_queue) != NULL); \
  185. ASSERT((_p_queue)->element_size == sizeof(_type)); \
  186. GCC_PRAGMA("GCC diagnostic pop") \
  187. return nrf_queue_write((_p_queue), p_data, element_count); \
  188. } \
  189. ret_code_t _name##_read(_type * p_data, \
  190. size_t element_count) \
  191. { \
  192. GCC_PRAGMA("GCC diagnostic push") \
  193. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  194. ASSERT((_p_queue) != NULL); \
  195. ASSERT((_p_queue)->element_size == sizeof(_type)); \
  196. GCC_PRAGMA("GCC diagnostic pop") \
  197. return nrf_queue_read((_p_queue), p_data, element_count); \
  198. } \
  199. size_t _name##_in(_type const * p_data, \
  200. size_t element_count) \
  201. { \
  202. GCC_PRAGMA("GCC diagnostic push") \
  203. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  204. ASSERT((_p_queue) != NULL); \
  205. ASSERT((_p_queue)->element_size == sizeof(_type)); \
  206. GCC_PRAGMA("GCC diagnostic pop") \
  207. return nrf_queue_in((_p_queue), p_data, element_count); \
  208. } \
  209. size_t _name##_out(_type * p_data, \
  210. size_t element_count) \
  211. { \
  212. GCC_PRAGMA("GCC diagnostic push") \
  213. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  214. ASSERT((_p_queue) != NULL); \
  215. ASSERT((_p_queue)->element_size == sizeof(_type)); \
  216. GCC_PRAGMA("GCC diagnostic pop") \
  217. return nrf_queue_out((_p_queue), p_data, element_count); \
  218. } \
  219. bool _name##_is_full(void) \
  220. { \
  221. GCC_PRAGMA("GCC diagnostic push") \
  222. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  223. ASSERT((_p_queue) != NULL); \
  224. return nrf_queue_is_full(_p_queue); \
  225. GCC_PRAGMA("GCC diagnostic pop") \
  226. } \
  227. bool _name##_is_empty(void) \
  228. { \
  229. GCC_PRAGMA("GCC diagnostic push") \
  230. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  231. ASSERT((_p_queue) != NULL); \
  232. GCC_PRAGMA("GCC diagnostic pop") \
  233. return nrf_queue_is_empty(_p_queue); \
  234. } \
  235. size_t _name##_utilization_get(void) \
  236. { \
  237. GCC_PRAGMA("GCC diagnostic push") \
  238. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  239. ASSERT((_p_queue) != NULL); \
  240. GCC_PRAGMA("GCC diagnostic pop") \
  241. return nrf_queue_utilization_get(_p_queue); \
  242. } \
  243. size_t _name##_available_get(void) \
  244. { \
  245. GCC_PRAGMA("GCC diagnostic push") \
  246. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  247. ASSERT((_p_queue) != NULL); \
  248. GCC_PRAGMA("GCC diagnostic pop") \
  249. return nrf_queue_available_get(_p_queue); \
  250. } \
  251. size_t _name##_max_utilization_get(void) \
  252. { \
  253. GCC_PRAGMA("GCC diagnostic push") \
  254. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  255. ASSERT((_p_queue) != NULL); \
  256. GCC_PRAGMA("GCC diagnostic pop") \
  257. return nrf_queue_max_utilization_get(_p_queue); \
  258. } \
  259. void _name##_reset(void) \
  260. { \
  261. GCC_PRAGMA("GCC diagnostic push") \
  262. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  263. ASSERT((_p_queue) != NULL); \
  264. GCC_PRAGMA("GCC diagnostic pop") \
  265. nrf_queue_reset(_p_queue); \
  266. }
  267. /**@brief Function for pushing an element to the end of queue.
  268. *
  269. * @param[in] p_queue Pointer to the nrf_queue_t instance.
  270. * @param[in] p_element Pointer to the element that will be stored in the queue.
  271. *
  272. * @return NRF_SUCCESS If an element has been successfully added.
  273. * @return NRF_ERROR_NO_MEM If the queue is full (only in @ref NRF_QUEUE_MODE_NO_OVERFLOW).
  274. */
  275. ret_code_t nrf_queue_push(nrf_queue_t const * p_queue, void const * p_element);
  276. /**@brief Generic pop implementation.
  277. *
  278. * @param[in] p_queue Pointer to the nrf_queue_t instance.
  279. * @param[out] p_element Pointer where the element will be copied.
  280. * @param[out] just_peek If true, the returned element will not be removed from queue.
  281. *
  282. * @return NRF_SUCCESS If an element was returned.
  283. * @return NRF_ERROR_NOT_FOUND If there are no more elements in the queue.
  284. */
  285. ret_code_t nrf_queue_generic_pop(nrf_queue_t const * p_queue,
  286. void * p_element,
  287. bool just_peek);
  288. /**@brief Pop element from the front of the queue.
  289. *
  290. * @param[in] _p_queue Pointer to the nrf_queue_t instance.
  291. * @param[out] _p_element Pointer where the element will be copied.
  292. *
  293. * @return NRF_SUCCESS If an element was returned.
  294. * @return NRF_ERROR_NOT_FOUND If there are no more elements in the queue.
  295. */
  296. #define nrf_queue_pop(_p_queue, _p_element) nrf_queue_generic_pop((_p_queue), (_p_element), false)
  297. /**@brief Peek element from the front of the queue.
  298. *
  299. * @param[in] _p_queue Pointer to the nrf_queue_t instance.
  300. * @param[out] _p_element Pointer where the element will be copied.
  301. *
  302. * @return NRF_SUCCESS If an element was returned.
  303. * @return NRF_ERROR_NOT_FOUND If there are no more elements in the queue.
  304. */
  305. #define nrf_queue_peek(_p_queue, _p_element) nrf_queue_generic_pop((_p_queue), (_p_element), true)
  306. /**@brief Function for writing elements to the queue.
  307. *
  308. * @param[in] p_queue Pointer to the nrf_queue_t instance.
  309. * @param[in] p_data Pointer to the buffer with elements to write.
  310. * @param[in] element_count Number of elements to write.
  311. *
  312. * @return NRF_SUCCESS If an element was written.
  313. * @return NRF_ERROR_NO_MEM There is not enough space in the queue. No element was written.
  314. */
  315. ret_code_t nrf_queue_write(nrf_queue_t const * p_queue,
  316. void const * p_data,
  317. size_t element_count);
  318. /**@brief Function for writing a portion of elements to the queue.
  319. *
  320. * @param[in] p_queue Pointer to the nrf_queue_t instance.
  321. * @param[in] p_data Pointer to the buffer with elements to write.
  322. * @param[in] element_count Number of elements to write.
  323. *
  324. * @return The number of added elements.
  325. */
  326. size_t nrf_queue_in(nrf_queue_t const * p_queue,
  327. void const * p_data,
  328. size_t element_count);
  329. /**@brief Function for reading elements from the queue.
  330. *
  331. * @param[in] p_queue Pointer to the nrf_queue_t instance.
  332. * @param[out] p_data Pointer to the buffer where elements will be copied.
  333. * @param[in] element_count Number of elements to read.
  334. *
  335. * @return NRF_SUCCESS If an element was returned.
  336. * @return NRF_ERROR_NOT_FOUND There is not enough elements in the queue.
  337. */
  338. ret_code_t nrf_queue_read(nrf_queue_t const * p_queue,
  339. void * p_data,
  340. size_t element_count);
  341. /**@brief Function for reading a portion of elements from the queue.
  342. *
  343. * @param[in] p_queue Pointer to the nrf_queue_t instance.
  344. * @param[out] p_data Pointer to the buffer where elements will be copied.
  345. * @param[in] element_count Number of elements to read.
  346. *
  347. * @return The number of read elements.
  348. */
  349. size_t nrf_queue_out(nrf_queue_t const * p_queue,
  350. void * p_data,
  351. size_t element_count);
  352. /**@brief Function for checking if the queue is full.
  353. *
  354. * @param[in] p_queue Pointer to the queue instance.
  355. *
  356. * @return True if the queue is full.
  357. */
  358. bool nrf_queue_is_full(nrf_queue_t const * p_queue);
  359. /**@brief Function for checking if the queue is empty.
  360. *
  361. * @param[in] p_queue Pointer to the queue instance.
  362. *
  363. * @return True if the queue is empty.
  364. */
  365. bool nrf_queue_is_empty(nrf_queue_t const * p_queue);
  366. /**@brief Function for getting the current queue utilization.
  367. *
  368. * @param[in] p_queue Pointer to the queue instance.
  369. *
  370. * @return Current queue utilization.
  371. */
  372. size_t nrf_queue_utilization_get(nrf_queue_t const * p_queue);
  373. /**@brief Function for getting the size of available space.
  374. *
  375. * @param[in] p_queue Pointer to the queue instance.
  376. *
  377. * @return Size of available space.
  378. */
  379. size_t nrf_queue_available_get(nrf_queue_t const * p_queue);
  380. /**@brief Function for getting the maximal queue utilization.
  381. *
  382. * @param[in] p_queue Pointer to the queue instance.
  383. *
  384. * @return Maximal queue utilization.
  385. */
  386. size_t nrf_queue_max_utilization_get(nrf_queue_t const * p_queue);
  387. /**@brief Function for resetting the maximal queue utilization.
  388. *
  389. * @param[in] p_queue Pointer to the queue instance.
  390. *
  391. */
  392. void nrf_queue_max_utilization_reset(nrf_queue_t const * p_queue);
  393. /**@brief Function for resetting the queue state.
  394. *
  395. * @param[in] p_queue Pointer to the queue instance.
  396. */
  397. void nrf_queue_reset(nrf_queue_t const * p_queue);
  398. #ifdef __cplusplus
  399. }
  400. #endif
  401. #endif // NRF_QUEUE_H__
  402. /** @} */