F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
Queue.cpp
Go to the documentation of this file.
1 /*
2  * Queue.cpp:
3  *
4  * Implementation of the queue data type.
5  *
6  * Created on: July 5th, 2022
7  * Author: lestarch
8  *
9  */
10 #include "Queue.hpp"
11 #include <Fw/Types/Assert.hpp>
12 
13 namespace Types {
14 
15 Queue::Queue() : m_internal(), m_message_size(0) {}
16 
17 void Queue::setup(U8* const storage, const FwSizeType storage_size, const FwSizeType depth, const FwSizeType message_size) {
18  // Ensure that enough storage was supplied
19  const FwSizeType total_needed_size = depth * message_size;
20  FW_ASSERT(
21  storage_size >= total_needed_size,
22  static_cast<FwAssertArgType>(storage_size),
23  static_cast<FwAssertArgType>(depth),
24  static_cast<FwAssertArgType>(message_size));
25  m_internal.setup(storage, static_cast<NATIVE_UINT_TYPE>(total_needed_size));
26  m_message_size = message_size;
27 }
28 
29 Fw::SerializeStatus Queue::enqueue(const U8* const message, const FwSizeType size) {
30  FW_ASSERT(m_message_size > 0, static_cast<FwAssertArgType>(m_message_size)); // Ensure initialization
31  FW_ASSERT(
32  m_message_size == size,
33  static_cast<FwAssertArgType>(size),
34  static_cast<FwAssertArgType>(m_message_size)); // Message size is as expected
35  return m_internal.serialize(message, static_cast<NATIVE_UINT_TYPE>(m_message_size));
36 }
37 
38 Fw::SerializeStatus Queue::dequeue(U8* const message, const FwSizeType size) {
39  FW_ASSERT(m_message_size > 0); // Ensure initialization
40  FW_ASSERT(
41  m_message_size <= size,
42  static_cast<FwAssertArgType>(size),
43  static_cast<FwAssertArgType>(m_message_size)); // Sufficient storage space for read message
44  Fw::SerializeStatus result = m_internal.peek(message, static_cast<NATIVE_UINT_TYPE>(m_message_size), 0);
45  if (result != Fw::FW_SERIALIZE_OK) {
46  return result;
47  }
48  return m_internal.rotate(static_cast<NATIVE_UINT_TYPE>(m_message_size));
49 }
50 
52  FW_ASSERT(m_message_size > 0, static_cast<FwAssertArgType>(m_message_size));
53  return static_cast<NATIVE_UINT_TYPE>(m_internal.get_high_water_mark() / m_message_size);
54 }
55 
57  m_internal.clear_high_water_mark();
58 }
59 
61  FW_ASSERT(m_message_size > 0, static_cast<FwAssertArgType>(m_message_size));
62  return static_cast<NATIVE_UINT_TYPE>(m_internal.get_allocated_size() / m_message_size);
63 }
64 
65 
66 } // namespace Types
#define FW_ASSERT(...)
Definition: Assert.hpp:14
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:26
PlatformUIntType NATIVE_UINT_TYPE
Definition: BasicTypes.h:52
PlatformAssertArgType FwAssertArgType
Definition: FpConfig.h:34
PlatformSizeType FwSizeType
Definition: FpConfig.h:30
Fw::SerializeStatus serialize(const U8 *const buffer, const NATIVE_UINT_TYPE size)
NATIVE_UINT_TYPE get_allocated_size() const
NATIVE_UINT_TYPE get_high_water_mark() const
void setup(U8 *const buffer, const NATIVE_UINT_TYPE size)
Fw::SerializeStatus peek(char &value, NATIVE_UINT_TYPE offset=0) const
Fw::SerializeStatus rotate(NATIVE_UINT_TYPE amount)
NATIVE_UINT_TYPE getQueueSize() const
Definition: Queue.cpp:60
void setup(U8 *const storage, const FwSizeType storage_size, const FwSizeType depth, const FwSizeType message_size)
setup the queue object to setup storage
Definition: Queue.cpp:17
Fw::SerializeStatus dequeue(U8 *const message, const FwSizeType size)
pops a fixed-size message off the front of the queue
Definition: Queue.cpp:38
Queue()
constructs an uninitialized queue
Definition: Queue.cpp:15
Fw::SerializeStatus enqueue(const U8 *const message, const FwSizeType size)
pushes a fixed-size message onto the back of the queue
Definition: Queue.cpp:29
NATIVE_UINT_TYPE get_high_water_mark() const
Definition: Queue.cpp:51
void clear_high_water_mark()
Definition: Queue.cpp:56
SerializeStatus
forward declaration for string
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.