F´ Flight Software - C/C++ Documentation  NASA-v1.6.0
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CircularBuffer.cpp
Go to the documentation of this file.
1 /*
2  * CircularBuffer.cpp:
3  *
4  * Buffer used to efficiently store data in ring data structure. Uses an externally supplied
5  * data store as the backing for this buffer. Thus it is dependent on receiving sole ownership
6  * of the supplied buffer.
7  *
8  * This implementation file contains the function definitions.
9  *
10  * Created on: Apr 4, 2019
11  * Author: lestarch
12  * Revised March 2022
13  * Author: bocchino
14  */
15 #include <Fw/Types/BasicTypes.hpp>
16 #include <Fw/Types/Assert.hpp>
18 
19 #ifdef CIRCULAR_DEBUG
20  #include <Os/Log.hpp>
21 #endif
22 
23 namespace Types {
24 
26  m_store(buffer),
27  m_store_size(size),
28  m_head_idx(0),
29  m_allocated_size(0)
30 {
31  FW_ASSERT(m_store_size > 0);
32 }
33 
35  return m_allocated_size;
36 }
37 
39  FW_ASSERT(m_allocated_size <= m_store_size, m_allocated_size);
40  return m_store_size - m_allocated_size;
41 }
42 
43 NATIVE_UINT_TYPE CircularBuffer :: advance_idx(NATIVE_UINT_TYPE idx, NATIVE_UINT_TYPE amount) const {
44  FW_ASSERT(idx < m_store_size, idx);
45  return (idx + amount) % m_store_size;
46 }
47 
49  FW_ASSERT(buffer != nullptr);
50  // Check there is sufficient space
51  if (size > get_free_size()) {
53  }
54  // Copy in all the supplied data
55  NATIVE_UINT_TYPE idx = advance_idx(m_head_idx, m_allocated_size);
56  for (U32 i = 0; i < size; i++) {
57  FW_ASSERT(idx < m_store_size, idx);
58  m_store[idx] = buffer[i];
59  idx = advance_idx(idx);
60  }
61  m_allocated_size += size;
62  FW_ASSERT(m_allocated_size <= this->get_capacity(), m_allocated_size);
63  return Fw::FW_SERIALIZE_OK;
64 }
65 
67  return peek(reinterpret_cast<U8&>(value), offset);
68 }
69 
71  // Check there is sufficient data
72  if ((sizeof(U8) + offset) > m_allocated_size) {
74  }
75  const NATIVE_UINT_TYPE idx = advance_idx(m_head_idx, offset);
76  FW_ASSERT(idx < m_store_size, idx);
77  value = m_store[idx];
78  return Fw::FW_SERIALIZE_OK;
79 }
80 
82  // Check there is sufficient data
83  if ((sizeof(U32) + offset) > m_allocated_size) {
85  }
86  value = 0;
87  NATIVE_UINT_TYPE idx = advance_idx(m_head_idx, offset);
88 
89  // Deserialize all the bytes from network format
90  for (NATIVE_UINT_TYPE i = 0; i < sizeof(U32); i++) {
91  FW_ASSERT(idx < m_store_size, idx);
92  value = (value << 8) | static_cast<U32>(m_store[idx]);
93  idx = advance_idx(idx);
94  }
95  return Fw::FW_SERIALIZE_OK;
96 }
97 
99  FW_ASSERT(buffer != nullptr);
100  // Check there is sufficient data
101  if ((size + offset) > m_allocated_size) {
103  }
104  NATIVE_UINT_TYPE idx = advance_idx(m_head_idx, offset);
105  // Deserialize all the bytes from network format
106  for (NATIVE_UINT_TYPE i = 0; i < size; i++) {
107  FW_ASSERT(idx < m_store_size, idx);
108  buffer[i] = m_store[idx];
109  idx = advance_idx(idx);
110  }
111  return Fw::FW_SERIALIZE_OK;
112 }
113 
115  // Check there is sufficient data
116  if (amount > m_allocated_size) {
118  }
119  m_head_idx = advance_idx(m_head_idx, amount);
120  m_allocated_size -= amount;
121  return Fw::FW_SERIALIZE_OK;
122 }
123 
125  return m_store_size;
126 }
127 
128 #ifdef CIRCULAR_DEBUG
129 void CircularBuffer :: print() {
130  NATIVE_UINT_TYPE idx = m_head_idx;
131  Os::Log::logMsg("Ring: ", 0, 0, 0, 0, 0, 0);
132  for (NATIVE_UINT_TYPE i = 0; i < m_allocated_size; ++i) {
133  Os::Log::logMsg("%c", m_store[idx], 0, 0, 0, 0, 0);
134  idx = advance_idx(idx);
135  }
136  Os::Log::logMsg("\n", 0, 0, 0, 0, 0, 0);
137 }
138 #endif
139 } //End Namespace Types
Types::CircularBuffer::serialize
Fw::SerializeStatus serialize(const U8 *const buffer, const NATIVE_UINT_TYPE size)
Definition: CircularBuffer.cpp:48
Fw::SerializeStatus
SerializeStatus
forward declaration for string
Definition: Serializable.hpp:14
U8
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.hpp:73
NATIVE_UINT_TYPE
unsigned int NATIVE_UINT_TYPE
native unsigned integer type declaration
Definition: BasicTypes.hpp:28
Types::CircularBuffer::get_allocated_size
NATIVE_UINT_TYPE get_allocated_size() const
Definition: CircularBuffer.cpp:34
Types::CircularBuffer::get_capacity
NATIVE_UINT_TYPE get_capacity() const
Definition: CircularBuffer.cpp:124
CircularBuffer.hpp
Types::CircularBuffer::peek
Fw::SerializeStatus peek(char &value, NATIVE_UINT_TYPE offset=0) const
Definition: CircularBuffer.cpp:66
Types::CircularBuffer::rotate
Fw::SerializeStatus rotate(NATIVE_UINT_TYPE amount)
Definition: CircularBuffer.cpp:114
FW_ASSERT
#define FW_ASSERT(...)
Definition: Assert.hpp:9
Types
Definition: CircularBuffer.cpp:23
Fw::Logger::logMsg
static void logMsg(const char *fmt, POINTER_CAST a0=0, POINTER_CAST a1=0, POINTER_CAST a2=0, POINTER_CAST a3=0, POINTER_CAST a4=0, POINTER_CAST a5=0, POINTER_CAST a6=0, POINTER_CAST a7=0, POINTER_CAST a8=0, POINTER_CAST a9=0)
Definition: Logger.cpp:18
Types::CircularBuffer::CircularBuffer
CircularBuffer(U8 *const buffer, const NATIVE_UINT_TYPE size)
Definition: CircularBuffer.cpp:25
Fw::FW_SERIALIZE_NO_ROOM_LEFT
@ FW_SERIALIZE_NO_ROOM_LEFT
No room left in the buffer to serialize data.
Definition: Serializable.hpp:17
Fw::FW_DESERIALIZE_BUFFER_EMPTY
@ FW_DESERIALIZE_BUFFER_EMPTY
Deserialization buffer was empty when trying to read more data.
Definition: Serializable.hpp:18
Types::CircularBuffer::get_free_size
NATIVE_UINT_TYPE get_free_size() const
Definition: CircularBuffer.cpp:38
Fw::FW_SERIALIZE_OK
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.
Definition: Serializable.hpp:15
Log.hpp