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 #include "nrf_802154_assert.h"
40 
41 #include "nrf_802154_queue.h"
42 
increment_modulo(uint8_t v,uint8_t wrap_at_value)43 static inline uint8_t increment_modulo(uint8_t v, uint8_t wrap_at_value)
44 {
45     v++;
46 
47     if (v >= wrap_at_value)
48     {
49         v = 0U;
50     }
51 
52     return v;
53 }
54 
idx2ptr(const nrf_802154_queue_t * p_queue,size_t idx)55 static inline void * idx2ptr(const nrf_802154_queue_t * p_queue, size_t idx)
56 {
57     return ((uint8_t *)(p_queue->p_memory)) + idx * p_queue->item_size;
58 }
59 
nrf_802154_queue_init(nrf_802154_queue_t * p_queue,void * p_memory,size_t memory_size,size_t item_size)60 void nrf_802154_queue_init(nrf_802154_queue_t * p_queue,
61                            void               * p_memory,
62                            size_t               memory_size,
63                            size_t               item_size)
64 {
65     NRF_802154_ASSERT(p_queue != NULL);
66     NRF_802154_ASSERT(p_memory != NULL);
67     NRF_802154_ASSERT(item_size != 0U);
68 
69     /* Due uint8_t type of nrf_802154_queue_t::item_size */
70     NRF_802154_ASSERT(item_size <= UINT8_MAX);
71 
72     size_t capacity = memory_size / item_size;
73 
74     /* Due simplified design, one entry in p_memory array is lost,
75      * see nrf_802154_queue_is_empty and nrf_802154_queue_is_full */
76     NRF_802154_ASSERT(capacity >= 2U);
77 
78     /* Due uint8_t type of nrf_802154_queue_t::capacity */
79     NRF_802154_ASSERT(capacity <= UINT8_MAX);
80 
81     p_queue->p_memory  = p_memory;
82     p_queue->capacity  = capacity;
83     p_queue->item_size = item_size;
84     p_queue->wridx     = 0U;
85     p_queue->rdidx     = 0U;
86 }
87 
nrf_802154_queue_push_begin(const nrf_802154_queue_t * p_queue)88 void * nrf_802154_queue_push_begin(const nrf_802154_queue_t * p_queue)
89 {
90     return idx2ptr(p_queue, p_queue->wridx);
91 }
92 
nrf_802154_queue_push_commit(nrf_802154_queue_t * p_queue)93 void nrf_802154_queue_push_commit(nrf_802154_queue_t * p_queue)
94 {
95     p_queue->wridx = increment_modulo(p_queue->wridx, p_queue->capacity);
96 }
97 
nrf_802154_queue_pop_begin(const nrf_802154_queue_t * p_queue)98 void * nrf_802154_queue_pop_begin(const nrf_802154_queue_t * p_queue)
99 {
100     return idx2ptr(p_queue, p_queue->rdidx);
101 }
102 
nrf_802154_queue_pop_commit(nrf_802154_queue_t * p_queue)103 void nrf_802154_queue_pop_commit(nrf_802154_queue_t * p_queue)
104 {
105     p_queue->rdidx = increment_modulo(p_queue->rdidx, p_queue->capacity);
106 }
107 
nrf_802154_queue_is_full(const nrf_802154_queue_t * p_queue)108 bool nrf_802154_queue_is_full(const nrf_802154_queue_t * p_queue)
109 {
110     size_t wridx;
111 
112     wridx = increment_modulo(p_queue->wridx, p_queue->capacity);
113 
114     return (p_queue->rdidx == wridx);
115 }
116