# FIFO Buffer (Circular/ring buffer) for packet storage

I am developing a simple radio transmission network using B-L072Z-LRWAN1 boards. The network structure is formed by:

1. One board acting as a gateway (where data is received).
2. Multiple boards acting as nodes (which send data to the gateway).

The packet payload sent by nodes is N bytes long and I want to store this packet's information in case gateway stops working and nodes cannot communicate with it. I want to keep data that could not be sent by the node and send it once the gateway is active again.

Let's say I want to keep the last M messages that node could not send to the gateway. The buffer size should be M*N bytes long, but how do I implement this type of buffer in C?

    //pseudo algorithm First In First Out
int FIFO[16]; // size of 2^N
int WR_Ptr, RD_Ptr; // Write and read pointer
int Input, Output;

// On write event
FIFO[WR_Ptr] = Input;
WR_Ptr++; //increment WR pointer
WR_Ptr &= 15 // apply mask 2^N-1, turn over in circular manner
if WR_Ptr==RD_Ptr //buffer overrun?
{
//handle error
}

if WR_Ptr<>RD_Ptr //check if FIFO is not empty
{
Output = FIFO[RD_Ptr];
RD_Ptr++; //increment RD pointer
RD_Ptr &= 15 // apply mask 2^N-1, turn over in circular manner
}

//clear FIFO
RD_Ptr = 0;
WR_Ptr = 0;

• Could you change the types of FIFO, Input and Output to something other then int? As it is this pseudocode is a bit confusing. – Jan Dorniak Aug 29 '18 at 18:21
• It's a pseudo code only, what thype would be more demonstrative? – Marko Buršič Aug 29 '18 at 18:23
• Something suggesting that it stores those messages - something implying a structure, or honestly - anything other than int to differentiate from WR_Ptr and RD_Ptr – Jan Dorniak Aug 29 '18 at 20:33
• @JanDorniak Don't be afraid to post your answer if you have a better solution. – Marko Buršič Aug 30 '18 at 10:41
• not really. And I'm not afraid - mostly I use SE on mobile which isn't conductive to posting good answers. Although I might suggest an edit to yours since it's more of a clarification of your answer then separate answer (FIFOs are fairly standard anyway) – Jan Dorniak Aug 30 '18 at 10:58