nrf_queue.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /**
  2. * Copyright (c) 2016 - 2020, 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. #if !(defined(__LINT__))
  123. /**@brief Create multiple queue instances.
  124. *
  125. * @note This macro reserves memory for array of queue instances.
  126. *
  127. * @param[in] _type Type which is stored.
  128. * @param[in] _name Name of the array with queue instances.
  129. * @param[in] _size Size of single queue instance.
  130. * @param[in] _mode Mode of single queue instance.
  131. * @param[in] _num Number of queue instances within array.
  132. */
  133. #define NRF_QUEUE_ARRAY_DEF(_type, _name, _size, _mode, _num) \
  134. MACRO_REPEAT_FOR(_num, NRF_QUEUE_ARRAY_INSTANCE_ELEMS_DEC, _type, _name, _size, _mode) \
  135. static const nrf_queue_t _name[] = \
  136. { \
  137. MACRO_REPEAT_FOR(_num, NRF_QUEUE_ARRAY_INSTANCE_INIT, _type, _name, _size, _mode) \
  138. }; \
  139. STATIC_ASSERT(ARRAY_SIZE(_name) == _num)
  140. #else
  141. #define NRF_QUEUE_ARRAY_DEF(_type, _name, _size, _mode, _num) \
  142. static const nrf_queue_t _name[_num];
  143. #endif // !(defined(__LINT__))
  144. /**@brief Helping macro used to declare elements for nrf_queue_t instance.
  145. * Used in @ref NRF_QUEUE_ARRAY_DEF.
  146. */
  147. #define NRF_QUEUE_ARRAY_INSTANCE_ELEMS_DEC(_num, _type, _name, _size, _mode) \
  148. static _type CONCAT_3(_name, _num, _nrf_queue_buffer[(_size) + 1]); \
  149. static nrf_queue_cb_t CONCAT_3(_name, _num, _nrf_queue_cb);
  150. /**@brief Helping macro used to initialize nrf_queue_t instance in an array fashion.
  151. * Used in @ref NRF_QUEUE_ARRAY_DEF.
  152. */
  153. #define NRF_QUEUE_ARRAY_INSTANCE_INIT(_num, _type, _name, _size, _mode) \
  154. { \
  155. .p_cb = &CONCAT_3(_name, _num, _nrf_queue_cb), \
  156. .p_buffer = CONCAT_3(_name, _num, _nrf_queue_buffer), \
  157. .size = (_size), \
  158. .element_size = sizeof(_type), \
  159. .mode = _mode, \
  160. },
  161. /**@brief Declare a queue interface.
  162. *
  163. * @param[in] _type Type which is stored.
  164. * @param[in] _name Name of the queue.
  165. */
  166. #define NRF_QUEUE_INTERFACE_DEC(_type, _name) \
  167. ret_code_t _name##_push(_type const * p_element); \
  168. ret_code_t _name##_pop(_type * p_element); \
  169. ret_code_t _name##_peek(_type * p_element); \
  170. ret_code_t _name##_write(_type const * p_data, \
  171. size_t element_count); \
  172. ret_code_t _name##_read(_type * p_data, \
  173. size_t element_count); \
  174. size_t _name##_out(_type * p_data, \
  175. size_t element_count); \
  176. size_t _name##_in(_type const * p_data, \
  177. size_t element_count); \
  178. bool _name##_is_full(void); \
  179. bool _name##_is_empty(void); \
  180. size_t _name##_utilization_get(void); \
  181. size_t _name##_available_get(void); \
  182. size_t _name##_max_utilization_get(void); \
  183. void _name##_reset(void)
  184. /**@brief Define a queue interface.
  185. *
  186. * @param[in] _type Type which is stored.
  187. * @param[in] _name Name of the queue.
  188. * @param[in] _p_queue Queue instance.
  189. */
  190. #define NRF_QUEUE_INTERFACE_DEF(_type, _name, _p_queue) \
  191. ret_code_t _name##_push(_type const * p_element) \
  192. { \
  193. GCC_PRAGMA("GCC diagnostic push") \
  194. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  195. ASSERT((_p_queue) != NULL); \
  196. ASSERT((_p_queue)->element_size == sizeof(_type)); \
  197. GCC_PRAGMA("GCC diagnostic pop") \
  198. return nrf_queue_push((_p_queue), p_element); \
  199. } \
  200. ret_code_t _name##_pop(_type * p_element) \
  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_pop((_p_queue), p_element); \
  208. } \
  209. ret_code_t _name##_peek(_type * p_element) \
  210. { \
  211. GCC_PRAGMA("GCC diagnostic push") \
  212. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  213. ASSERT((_p_queue) != NULL); \
  214. ASSERT((_p_queue)->element_size == sizeof(_type)); \
  215. GCC_PRAGMA("GCC diagnostic pop") \
  216. return nrf_queue_peek((_p_queue), p_element); \
  217. } \
  218. ret_code_t _name##_write(_type const * p_data, \
  219. size_t element_count) \
  220. { \
  221. GCC_PRAGMA("GCC diagnostic push") \
  222. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  223. ASSERT((_p_queue) != NULL); \
  224. ASSERT((_p_queue)->element_size == sizeof(_type)); \
  225. GCC_PRAGMA("GCC diagnostic pop") \
  226. return nrf_queue_write((_p_queue), p_data, element_count); \
  227. } \
  228. ret_code_t _name##_read(_type * p_data, \
  229. size_t element_count) \
  230. { \
  231. GCC_PRAGMA("GCC diagnostic push") \
  232. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  233. ASSERT((_p_queue) != NULL); \
  234. ASSERT((_p_queue)->element_size == sizeof(_type)); \
  235. GCC_PRAGMA("GCC diagnostic pop") \
  236. return nrf_queue_read((_p_queue), p_data, element_count); \
  237. } \
  238. size_t _name##_in(_type const * p_data, \
  239. size_t element_count) \
  240. { \
  241. GCC_PRAGMA("GCC diagnostic push") \
  242. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  243. ASSERT((_p_queue) != NULL); \
  244. ASSERT((_p_queue)->element_size == sizeof(_type)); \
  245. GCC_PRAGMA("GCC diagnostic pop") \
  246. return nrf_queue_in((_p_queue), p_data, element_count); \
  247. } \
  248. size_t _name##_out(_type * p_data, \
  249. size_t element_count) \
  250. { \
  251. GCC_PRAGMA("GCC diagnostic push") \
  252. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  253. ASSERT((_p_queue) != NULL); \
  254. ASSERT((_p_queue)->element_size == sizeof(_type)); \
  255. GCC_PRAGMA("GCC diagnostic pop") \
  256. return nrf_queue_out((_p_queue), p_data, element_count); \
  257. } \
  258. bool _name##_is_full(void) \
  259. { \
  260. GCC_PRAGMA("GCC diagnostic push") \
  261. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  262. ASSERT((_p_queue) != NULL); \
  263. return nrf_queue_is_full(_p_queue); \
  264. GCC_PRAGMA("GCC diagnostic pop") \
  265. } \
  266. bool _name##_is_empty(void) \
  267. { \
  268. GCC_PRAGMA("GCC diagnostic push") \
  269. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  270. ASSERT((_p_queue) != NULL); \
  271. GCC_PRAGMA("GCC diagnostic pop") \
  272. return nrf_queue_is_empty(_p_queue); \
  273. } \
  274. size_t _name##_utilization_get(void) \
  275. { \
  276. GCC_PRAGMA("GCC diagnostic push") \
  277. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  278. ASSERT((_p_queue) != NULL); \
  279. GCC_PRAGMA("GCC diagnostic pop") \
  280. return nrf_queue_utilization_get(_p_queue); \
  281. } \
  282. size_t _name##_available_get(void) \
  283. { \
  284. GCC_PRAGMA("GCC diagnostic push") \
  285. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  286. ASSERT((_p_queue) != NULL); \
  287. GCC_PRAGMA("GCC diagnostic pop") \
  288. return nrf_queue_available_get(_p_queue); \
  289. } \
  290. size_t _name##_max_utilization_get(void) \
  291. { \
  292. GCC_PRAGMA("GCC diagnostic push") \
  293. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  294. ASSERT((_p_queue) != NULL); \
  295. GCC_PRAGMA("GCC diagnostic pop") \
  296. return nrf_queue_max_utilization_get(_p_queue); \
  297. } \
  298. void _name##_reset(void) \
  299. { \
  300. GCC_PRAGMA("GCC diagnostic push") \
  301. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  302. ASSERT((_p_queue) != NULL); \
  303. GCC_PRAGMA("GCC diagnostic pop") \
  304. nrf_queue_reset(_p_queue); \
  305. }
  306. /**@brief Function for pushing an element to the end of queue.
  307. *
  308. * @param[in] p_queue Pointer to the nrf_queue_t instance.
  309. * @param[in] p_element Pointer to the element that will be stored in the queue.
  310. *
  311. * @return NRF_SUCCESS If an element has been successfully added.
  312. * @return NRF_ERROR_NO_MEM If the queue is full (only in @ref NRF_QUEUE_MODE_NO_OVERFLOW).
  313. */
  314. ret_code_t nrf_queue_push(nrf_queue_t const * p_queue, void const * p_element);
  315. /**@brief Generic pop implementation.
  316. *
  317. * @param[in] p_queue Pointer to the nrf_queue_t instance.
  318. * @param[out] p_element Pointer where the element will be copied.
  319. * @param[out] just_peek If true, the returned element will not be removed from queue.
  320. *
  321. * @return NRF_SUCCESS If an element was returned.
  322. * @return NRF_ERROR_NOT_FOUND If there are no more elements in the queue.
  323. */
  324. ret_code_t nrf_queue_generic_pop(nrf_queue_t const * p_queue,
  325. void * p_element,
  326. bool just_peek);
  327. /**@brief Pop element from the front of the queue.
  328. *
  329. * @param[in] _p_queue Pointer to the nrf_queue_t instance.
  330. * @param[out] _p_element Pointer where the element will be copied.
  331. *
  332. * @return NRF_SUCCESS If an element was returned.
  333. * @return NRF_ERROR_NOT_FOUND If there are no more elements in the queue.
  334. */
  335. #define nrf_queue_pop(_p_queue, _p_element) nrf_queue_generic_pop((_p_queue), (_p_element), false)
  336. /**@brief Peek element from the front of the queue.
  337. *
  338. * @param[in] _p_queue Pointer to the nrf_queue_t instance.
  339. * @param[out] _p_element Pointer where the element will be copied.
  340. *
  341. * @return NRF_SUCCESS If an element was returned.
  342. * @return NRF_ERROR_NOT_FOUND If there are no more elements in the queue.
  343. */
  344. #define nrf_queue_peek(_p_queue, _p_element) nrf_queue_generic_pop((_p_queue), (_p_element), true)
  345. /**@brief Function for writing elements to the queue.
  346. *
  347. * @param[in] p_queue Pointer to the nrf_queue_t instance.
  348. * @param[in] p_data Pointer to the buffer with elements to write.
  349. * @param[in] element_count Number of elements to write.
  350. *
  351. * @return NRF_SUCCESS If an element was written.
  352. * @return NRF_ERROR_NO_MEM There is not enough space in the queue. No element was written.
  353. */
  354. ret_code_t nrf_queue_write(nrf_queue_t const * p_queue,
  355. void const * p_data,
  356. size_t element_count);
  357. /**@brief Function for writing a portion of elements to the queue.
  358. *
  359. * @param[in] p_queue Pointer to the nrf_queue_t instance.
  360. * @param[in] p_data Pointer to the buffer with elements to write.
  361. * @param[in] element_count Number of elements to write.
  362. *
  363. * @return The number of added elements.
  364. */
  365. size_t nrf_queue_in(nrf_queue_t const * p_queue,
  366. void const * p_data,
  367. size_t element_count);
  368. /**@brief Function for reading elements from the queue.
  369. *
  370. * @param[in] p_queue Pointer to the nrf_queue_t instance.
  371. * @param[out] p_data Pointer to the buffer where elements will be copied.
  372. * @param[in] element_count Number of elements to read.
  373. *
  374. * @return NRF_SUCCESS If an element was returned.
  375. * @return NRF_ERROR_NOT_FOUND There is not enough elements in the queue.
  376. */
  377. ret_code_t nrf_queue_read(nrf_queue_t const * p_queue,
  378. void * p_data,
  379. size_t element_count);
  380. /**@brief Function for reading a portion of elements from the queue.
  381. *
  382. * @param[in] p_queue Pointer to the nrf_queue_t instance.
  383. * @param[out] p_data Pointer to the buffer where elements will be copied.
  384. * @param[in] element_count Number of elements to read.
  385. *
  386. * @return The number of read elements.
  387. */
  388. size_t nrf_queue_out(nrf_queue_t const * p_queue,
  389. void * p_data,
  390. size_t element_count);
  391. /**@brief Function for checking if the queue is full.
  392. *
  393. * @param[in] p_queue Pointer to the queue instance.
  394. *
  395. * @return True if the queue is full.
  396. */
  397. bool nrf_queue_is_full(nrf_queue_t const * p_queue);
  398. /**@brief Function for checking if the queue is empty.
  399. *
  400. * @param[in] p_queue Pointer to the queue instance.
  401. *
  402. * @return True if the queue is empty.
  403. */
  404. bool nrf_queue_is_empty(nrf_queue_t const * p_queue);
  405. /**@brief Function for getting the current queue utilization.
  406. *
  407. * @param[in] p_queue Pointer to the queue instance.
  408. *
  409. * @return Current queue utilization.
  410. */
  411. size_t nrf_queue_utilization_get(nrf_queue_t const * p_queue);
  412. /**@brief Function for getting the size of available space.
  413. *
  414. * @param[in] p_queue Pointer to the queue instance.
  415. *
  416. * @return Size of available space.
  417. */
  418. size_t nrf_queue_available_get(nrf_queue_t const * p_queue);
  419. /**@brief Function for getting the maximal queue utilization.
  420. *
  421. * @param[in] p_queue Pointer to the queue instance.
  422. *
  423. * @return Maximal queue utilization.
  424. */
  425. size_t nrf_queue_max_utilization_get(nrf_queue_t const * p_queue);
  426. /**@brief Function for resetting the maximal queue utilization.
  427. *
  428. * @param[in] p_queue Pointer to the queue instance.
  429. *
  430. */
  431. void nrf_queue_max_utilization_reset(nrf_queue_t const * p_queue);
  432. /**@brief Function for resetting the queue state.
  433. *
  434. * @param[in] p_queue Pointer to the queue instance.
  435. */
  436. void nrf_queue_reset(nrf_queue_t const * p_queue);
  437. #ifdef __cplusplus
  438. }
  439. #endif
  440. #endif // NRF_QUEUE_H__
  441. /** @} */