F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Queue.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Os/Queue.cpp
3 // \brief common function implementation for Os::Queue
4 // ======================================================================
5 #include "Os/Queue.hpp"
6 #include "Fw/Types/Assert.hpp"
8 
9 namespace Os {
10 
11 FwSizeType Queue::s_queueCount = 0;
12 #if FW_QUEUE_REGISTRATION
13 QueueRegistry* Queue::s_queueRegistry = nullptr;
14 #endif
15 
16 Queue::Queue() : m_name(""), m_depth(0), m_size(0), m_delegate(*QueueInterface::getDelegate(m_handle_storage)) {}
17 
19  m_delegate.~QueueInterface();
20 }
21 
23  const Fw::ConstStringBase& name,
24  FwSizeType depth,
25  FwSizeType messageSize) {
26  FW_ASSERT(&this->m_delegate == reinterpret_cast<QueueInterface*>(&this->m_handle_storage[0]));
27  FW_ASSERT(depth > 0);
28  FW_ASSERT(messageSize > 0);
29  // Check for previous creation call
30  if (this->m_depth > 0 || this->m_size > 0) {
31  return QueueInterface::Status::ALREADY_CREATED;
32  }
33  QueueInterface::Status status = this->m_delegate.create(id, name, depth, messageSize);
34  if (status == QueueInterface::Status::OP_OK) {
35  this->m_name = name;
36  this->m_depth = depth;
37  this->m_size = messageSize;
39  Queue::s_queueCount++;
40 #if FW_QUEUE_REGISTRATION
41  if (Queue::s_queueRegistry != nullptr) {
42  Queue::s_queueRegistry->registerQueue(this);
43  }
44 #endif
45  }
46  return status;
47 }
48 
50  FW_ASSERT(&this->m_delegate == reinterpret_cast<QueueInterface*>(&this->m_handle_storage[0]));
51  return this->m_delegate.teardown();
52 }
53 
55  FwSizeType size,
56  FwQueuePriorityType priority,
57  QueueInterface::BlockingType blockType) {
58  FW_ASSERT(&this->m_delegate == reinterpret_cast<QueueInterface*>(&this->m_handle_storage[0]));
59  FW_ASSERT(buffer != nullptr);
60  // Check if initialized
61  if (this->m_depth == 0 || this->m_size == 0) {
62  return QueueInterface::Status::UNINITIALIZED;
63  }
64  // Check size before proceeding
65  else if (size > this->getMessageSize()) {
66  return QueueInterface::Status::SIZE_MISMATCH;
67  }
68  return this->m_delegate.send(buffer, size, priority, blockType);
69 }
70 
72  FwSizeType capacity,
74  FwSizeType& actualSize,
75  FwQueuePriorityType& priority) {
76  FW_ASSERT(&this->m_delegate == reinterpret_cast<QueueInterface*>(&this->m_handle_storage[0]));
77  FW_ASSERT(destination != nullptr);
78  // Check if initialized
79  if (this->m_depth == 0 || this->m_size == 0) {
80  return QueueInterface::Status::UNINITIALIZED;
81  }
82  // Check capacity before proceeding
83  else if (capacity < this->getMessageSize()) {
84  return QueueInterface::Status::SIZE_MISMATCH;
85  }
86  return this->m_delegate.receive(destination, capacity, blockType, actualSize, priority);
87 }
88 
90  FW_ASSERT(&this->m_delegate == reinterpret_cast<const QueueInterface*>(&this->m_handle_storage[0]));
91  return this->m_delegate.getMessagesAvailable();
92 }
93 
95  FW_ASSERT(&this->m_delegate == reinterpret_cast<const QueueInterface*>(&this->m_handle_storage[0]));
96  return this->m_delegate.getMessageHighWaterMark();
97 }
98 
100  FW_ASSERT(&this->m_delegate == reinterpret_cast<const QueueInterface*>(&this->m_handle_storage[0]));
101  return this->m_delegate.getHandle();
102 }
103 
105  FwQueuePriorityType priority,
106  QueueInterface::BlockingType blockType) {
107  return this->send(message.getBuffAddr(), message.getSize(), priority, blockType);
108 }
109 
112  FwQueuePriorityType& priority) {
113  FwSizeType actualSize = 0;
114  destination.resetSer(); // Reset the buffer
115  QueueInterface::Status status =
116  this->receive(destination.getBuffAddrSer(), destination.getCapacity(), blockType, actualSize, priority);
117  if (status == QueueInterface::Status::OP_OK) {
118  Fw::SerializeStatus serializeStatus =
119  destination.setBuffLen(static_cast<Fw::Serializable::SizeType>(actualSize));
120  if (serializeStatus != Fw::SerializeStatus::FW_SERIALIZE_OK) {
121  status = QueueInterface::Status::SIZE_MISMATCH;
122  }
123  }
124  return status;
125 }
126 
128  return this->m_depth;
129 }
130 
132  return this->m_size;
133 }
134 
135 const QueueString& Queue::getName() const {
136  return this->m_name;
137 }
138 
141  return Queue::s_queueCount;
142 }
143 
145  static Os::Mutex s_mutex;
146  return s_mutex;
147 }
148 
149 } // namespace Os
Serialization/Deserialization operation was successful.
Status create(FwEnumStoreType id, const Fw::ConstStringBase &name, FwSizeType depth, FwSizeType messageSize) override
create queue storage through delegate
Definition: Queue.cpp:22
Operation succeeded.
Definition: Os.hpp:26
PlatformSizeType FwSizeType
Status receive(U8 *destination, FwSizeType capacity, BlockingType blockType, FwSizeType &actualSize, FwQueuePriorityType &priority) override
receive a message from the queue through delegate
Definition: Queue.cpp:71
I32 FwEnumStoreType
Status
status returned from the queue send function
Definition: Queue.hpp:30
Serializable::SizeType getSize() const override
Get current buffer size.
QueueHandle parent class.
Definition: Queue.hpp:19
static FwSizeType getNumQueues()
get number of queues system-wide
Definition: Queue.cpp:139
virtual U8 * getBuffAddr()=0
Get buffer address for data filling (non-const version)
base queue interface
Definition: Queue.hpp:27
virtual Status receive(U8 *destination, FwSizeType capacity, BlockingType blockType, FwSizeType &actualSize, FwQueuePriorityType &priority)=0
receive a message from the queue
FwSizeType getDepth() const
get the queue&#39;s depth in messages
Definition: Queue.cpp:127
virtual void teardown()
teardown the queue
Definition: Queue.hpp:88
Queue()
queue constructor
Definition: Queue.cpp:16
virtual ~QueueInterface()=default
default queue destructor
virtual ~Queue()
default queue destructor
Definition: Queue.cpp:18
SerializeStatus
forward declaration for string
virtual FwSizeType getMessagesAvailable() const =0
get number of messages available
virtual Status create(FwEnumStoreType id, const Fw::ConstStringBase &name, FwSizeType depth, FwSizeType messageSize)=0
create queue storage
QueueHandle * getHandle() override
return the underlying queue handle (implementation specific). Delegates to implementation.
Definition: Queue.cpp:99
virtual QueueHandle * getHandle()=0
return the underlying queue handle (implementation specific)
const QueueString & getName() const
get the queue&#39;s name
Definition: Queue.cpp:135
FwSizeType getMessageSize() const
get the queue&#39;s message maximum size
Definition: Queue.cpp:131
Status send(const U8 *buffer, FwSizeType size, FwQueuePriorityType priority, BlockingType blockType) override
send a message into the queue through delegate
Definition: Queue.cpp:54
void resetSer() override
Reset serialization pointer to beginning of buffer.
Serializable::SizeType getCapacity() const override=0
Get buffer capacity.
void teardown() override
teardown the queue
Definition: Queue.cpp:49
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:53
BlockingType
message type
Definition: Queue.hpp:46
static Os::Mutex & getStaticMutex()
get static mutex
Definition: Queue.cpp:144
FwSizeType getMessagesAvailable() const override
get number of messages available
Definition: Queue.cpp:89
PlatformQueuePriorityType FwQueuePriorityType
The type of queue priorities used.
A read-only abstract superclass for StringBase.
locks a mutex within the current scope
Definition: Mutex.hpp:80
virtual FwSizeType getMessageHighWaterMark() const =0
get maximum messages stored at any given time
FwSizeType getMessageHighWaterMark() const override
get maximum messages stored at any given time through delegate
Definition: Queue.cpp:94
SerializeStatus setBuffLen(Serializable::SizeType length) override
Set buffer length manually.
virtual Status send(const U8 *buffer, FwSizeType size, FwQueuePriorityType priority, BlockingType blockType)=0
send a message into the queue
#define FW_ASSERT(...)
Definition: Assert.hpp:14
U8 * getBuffAddrSer()
Get address of end of serialization (DANGEROUS!)