FIFO protection (#1285)
[ExpressLRS.git] / src / lib / FIFO / FIFO.cpp
blobfc9876abcb1fdec82131187300f67ba2acddb2da
1 /*
2 * FIFO Buffer
3 * Implementation uses arrays to conserve memory
5 * The MIT License (MIT)
7 * Copyright (c) 2015 Daniel Eisterhold
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in all
17 * copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
28 * Modified/Ammended by Alessandro Carcione 2020
30 #include "FIFO.h"
31 #include "logging.h"
33 FIFO::FIFO()
35 head = 0;
36 tail = 0;
37 numElements = 0;
40 FIFO::~FIFO()
44 void ICACHE_RAM_ATTR FIFO::push(const uint8_t data)
46 if (numElements == FIFO_SIZE)
48 ERRLN("Buffer full, will flush");
49 flush();
50 return;
52 else
54 numElements++;
55 buffer[tail] = data;
56 tail = (tail + 1) % FIFO_SIZE;
60 void ICACHE_RAM_ATTR FIFO::pushBytes(const uint8_t *data, uint8_t len)
62 if (numElements + len > FIFO_SIZE)
64 ERRLN("Buffer full, will flush");
65 flush();
66 return;
68 for (int i = 0; i < len; i++)
70 buffer[tail] = data[i];
71 tail = (tail + 1) % FIFO_SIZE;
73 numElements += len;
76 uint8_t ICACHE_RAM_ATTR FIFO::pop()
78 if (numElements == 0)
80 // DBGLN(F("Buffer empty"));
81 return 0;
83 else
85 numElements--;
86 uint8_t data = buffer[head];
87 head = (head + 1) % FIFO_SIZE;
88 return data;
92 void ICACHE_RAM_ATTR FIFO::popBytes(uint8_t *data, uint8_t len)
94 if (numElements < len)
96 // DBGLN(F("Buffer underrun"));
97 flush();
99 else
101 numElements -= len;
102 for (int i = 0; i < len; i++)
104 data[i] = buffer[head];
105 head = (head + 1) % FIFO_SIZE;
110 uint8_t ICACHE_RAM_ATTR FIFO::peek()
112 if (numElements == 0)
114 // DBGLN(F("Buffer empty"));
115 return 0;
117 else
119 uint8_t data = buffer[head];
120 return data;
124 uint16_t ICACHE_RAM_ATTR FIFO::size()
126 return numElements;
129 void ICACHE_RAM_ATTR FIFO::flush()
131 head = 0;
132 tail = 0;
133 numElements = 0;
136 bool ICACHE_RAM_ATTR FIFO::ensure(uint8_t requiredSize)
138 if(requiredSize > FIFO_SIZE)
139 return false;
140 while(!available(requiredSize)) {
141 uint8_t len = pop();
142 head = (head + len) % FIFO_SIZE;
143 numElements -= len;
145 return true;