1 /*
2  * Copyright (c) 2019, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  *    list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
18  *    contributors may be used to endorse or promote products derived from this
19  *    software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 /**
36  * @brief Module implementing simple FIFO queue.
37  */
38 
39 #ifndef NRF_802154_QUEUE_H__
40 #define NRF_802154_QUEUE_H__
41 
42 #include <stdbool.h>
43 #include <stdint.h>
44 #include <stddef.h>
45 
46 /**@brief Type representing a FIFO queue. */
47 typedef struct
48 {
49     /**@brief Pointer to items memory of the queue.
50      * @details Memory pointed by this pointer has size @c item_size * @c capacity. */
51     void           * p_memory;
52 
53     /**@brief Size of an item in the queue. */
54     uint8_t          item_size;
55 
56     /**@brief Maximum number of items that can be stored in the memory of the queue */
57     uint8_t          capacity;
58 
59     /**@brief Index in the items memory of the queue where next item is written. */
60     volatile uint8_t wridx;
61 
62     /**@brief Index in the items memory of the queue where next item is read. */
63     volatile uint8_t rdidx;
64 } nrf_802154_queue_t;
65 
66 /**@brief Initializes a queue.
67  *
68  * @param[in] p_queue       Pointer to the queue instance to be initialized. Must not be NULL.
69  * @param[in] p_memory      Pointer to a memory that will be used to store items of the queue.
70  *                          Must not be NULL.
71  * @param[in] memory_size   Size of the memory pointed by @p p_memory.
72  *                          This parameter must be no less than 2 * @p item_size
73  * @param[in] item_size     Size of an item of the queue. Must not be 0.
74  */
75 void nrf_802154_queue_init(nrf_802154_queue_t * p_queue,
76                            void               * p_memory,
77                            size_t               memory_size,
78                            size_t               item_size);
79 
80 /**@brief Returns pointer to the next item to be written to the queue.
81  *
82  * This function is to be used when writing data to the queue directly (no copy).
83  * Returned pointer is valid when the queue is not full (@ref nrf_802154_queue_is_full returned false).
84  * To write an item to the queue perform following.
85  * @code
86  * if (!nrf_802154_queue_is_full(&queue))
87  * {
88  *     my_item_t * p_item = (my_item_t *)nrf_802154_queue_push_begin(&queue);
89  *     ... p_item is now direct pointer into memory of the queue, fill all data at p_item pointer
90  *     p_item->some_field = some_value;
91  *     ... fill all data at p_item pointer
92  *     nrf_802154_queue_push_commit(&queue)
93  * }
94  * @endcode
95  *
96  * To ensure thread-safety external locking is required.
97  *
98  * @param[in] p_queue        Pointer to the queue instance.
99  *
100  * @return Pointer to the next item to be written.
101  */
102 void * nrf_802154_queue_push_begin(const nrf_802154_queue_t * p_queue);
103 
104 /**@brief Increments write pointer of the queue.
105  *
106  * @param[in] p_queue       Pointer to the queue instance.
107  */
108 void nrf_802154_queue_push_commit(nrf_802154_queue_t * p_queue);
109 
110 /**@brief Returns pointer to the next item to be read from the queue.
111  *
112  * This function is to be used when reading data from the queue directly (no copy).
113  * Returned pointer is valid when the queue is not empty (@ref nrf_802154_queue_is_empty returned false).
114  * To ensure thread-safety external locking is required.
115  *
116  * To read an item from the queue perform following.
117  * @code
118  * if (!nrf_802154_queue_is_empty(&queue))
119  * {
120  *     my_item_t * p_item = (my_item_t *)nrf_802154_queue_pop_begin(&queue);
121  *     ... read & process data pointed by p_item
122  *     nrf_802154_queue_pop_commit(&queue)
123  * }
124  * @endcode
125  *
126  *
127  * @param[in] p_queue        Pointer to the queue instance.
128  *
129  * @return Pointer to the next item to be written.
130  */
131 void * nrf_802154_queue_pop_begin(const nrf_802154_queue_t * p_queue);
132 
133 /**@brief Increments read pointer of the queue.
134  *
135  * @param[in] p_queue       Pointer to the queue instance.
136  */
137 void nrf_802154_queue_pop_commit(nrf_802154_queue_t * p_queue);
138 
139 /**@brief Checks if the queue is empty.
140  *
141  * @param[in] p_queue       Pointer to the queue instance.
142  *
143  * @retval true             When the queue is empty
144  * @retval false            When the queue is not empty.
145  */
nrf_802154_queue_is_empty(const nrf_802154_queue_t * p_queue)146 static inline bool nrf_802154_queue_is_empty(const nrf_802154_queue_t * p_queue)
147 {
148     return (p_queue->wridx == p_queue->rdidx);
149 }
150 
151 /**@brief Checks if the queue is full.
152  *
153  * @param[in] p_queue       Pointer to the queue instance.
154  *
155  * @retval true             When the queue is full.
156  * @retval false            When the queue is not full.
157  */
158 bool nrf_802154_queue_is_full(const nrf_802154_queue_t * p_queue);
159 
160 #endif /* NRF_802154_QUEUE_H__ */
161