F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
ComQueue.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title ComQueue.cpp
3 // \author vbai
4 // \brief cpp file for ComQueue component implementation class
5 // ======================================================================
6 
7 #include <Fw/Types/Assert.hpp>
10 
11 namespace Svc {
12 
13 // ----------------------------------------------------------------------
14 // Construction, initialization, and destruction
15 // ----------------------------------------------------------------------
16 
17 ComQueue ::QueueConfigurationTable ::QueueConfigurationTable() {
18  for (NATIVE_UINT_TYPE i = 0; i < FW_NUM_ARRAY_ELEMENTS(this->entries); i++) {
19  this->entries[i].priority = 0;
20  this->entries[i].depth = 0;
21  }
22 }
23 
24 ComQueue ::ComQueue(const char* const compName)
25  : ComQueueComponentBase(compName),
26  m_state(WAITING),
27  m_allocationId(static_cast<NATIVE_UINT_TYPE>(-1)),
28  m_allocator(nullptr),
29  m_allocation(nullptr) {
30  // Initialize throttles to "off"
31  for (NATIVE_UINT_TYPE i = 0; i < TOTAL_PORT_COUNT; i++) {
32  this->m_throttle[i] = false;
33  }
34 }
35 
37 
38 void ComQueue ::init(const NATIVE_INT_TYPE queueDepth, const NATIVE_INT_TYPE instance) {
39  ComQueueComponentBase::init(queueDepth, instance);
40 }
41 
43  // Deallocate memory ignoring error conditions
44  if ((this->m_allocator != nullptr) && (this->m_allocation != nullptr)) {
45  this->m_allocator->deallocate(this->m_allocationId, this->m_allocation);
46  }
47 }
48 
50  NATIVE_UINT_TYPE allocationId,
51  Fw::MemAllocator& allocator) {
52  FwIndexType currentPriorityIndex = 0;
53  NATIVE_UINT_TYPE totalAllocation = 0;
54 
55  // Store/initialize allocator members
56  this->m_allocator = &allocator;
57  this->m_allocationId = allocationId;
58  this->m_allocation = nullptr;
59 
60  // Initializes the sorted queue metadata list in priority (sorted) order. This is accomplished by walking the
61  // priority values in priority order from 0 to TOTAL_PORT_COUNT. At each priory value, the supplied queue
62  // configuration table is walked and any entry matching the current priority values is used to add queue metadata to
63  // the prioritized list. This results in priority-sorted queue metadata objects that index back into the unsorted
64  // queue data structures.
65  //
66  // The total allocation size is tracked for passing to the allocation call and is a summation of
67  // (depth * message size) for each prioritized metadata object of (depth * message size)
68  for (FwIndexType currentPriority = 0; currentPriority < TOTAL_PORT_COUNT; currentPriority++) {
69  // Walk each queue configuration entry and add them into the prioritized metadata list when matching the current
70  // priority value
71  for (FwIndexType entryIndex = 0; entryIndex < static_cast<FwIndexType>(FW_NUM_ARRAY_ELEMENTS(queueConfig.entries)); entryIndex++) {
72  // Check for valid configuration entry
73  FW_ASSERT(queueConfig.entries[entryIndex].priority < TOTAL_PORT_COUNT,
74  static_cast<FwAssertArgType>(queueConfig.entries[entryIndex].priority),
75  static_cast<FwAssertArgType>(TOTAL_PORT_COUNT),
76  static_cast<FwAssertArgType>(entryIndex));
77 
78  if (currentPriority == queueConfig.entries[entryIndex].priority) {
79  // Set up the queue metadata object in order to track priority, depth, index into the queue list of the
80  // backing queue object, and message size. Both index and message size are calculated where priority and
81  // depth are copied from the configuration object.
82  QueueMetadata& entry = this->m_prioritizedList[currentPriorityIndex];
83  entry.priority = queueConfig.entries[entryIndex].priority;
84  entry.depth = queueConfig.entries[entryIndex].depth;
85  entry.index = entryIndex;
86  // Message size is determined by the type of object being stored, which in turn is determined by the
87  // index of the entry. Those lower than COM_PORT_COUNT are Fw::ComBuffers and those larger Fw::Buffer.
88  entry.msgSize = (entryIndex < COM_PORT_COUNT) ? sizeof(Fw::ComBuffer) : sizeof(Fw::Buffer);
89  totalAllocation += static_cast<NATIVE_UINT_TYPE>(entry.depth * entry.msgSize);
90  currentPriorityIndex++;
91  }
92  }
93  }
94  // Allocate a single chunk of memory from the memory allocator. Memory recover is neither needed nor used.
95  bool recoverable = false;
96  this->m_allocation = this->m_allocator->allocate(this->m_allocationId, totalAllocation, recoverable);
97 
98  // Each of the backing queue objects must be supplied memory to store the queued messages. These data regions are
99  // sub-portions of the total allocated data. This memory is passed out by looping through each queue in prioritized
100  // order and passing out the memory to each queue's setup method.
101  FwSizeType allocationOffset = 0;
102  for (FwIndexType i = 0; i < TOTAL_PORT_COUNT; i++) {
103  // Get current queue's allocation size and safety check the values
104  FwSizeType allocationSize = this->m_prioritizedList[i].depth * this->m_prioritizedList[i].msgSize;
105  FW_ASSERT(this->m_prioritizedList[i].index < static_cast<FwIndexType>(FW_NUM_ARRAY_ELEMENTS(this->m_queues)),
106  this->m_prioritizedList[i].index);
107  FW_ASSERT(
108  (allocationSize + allocationOffset) <= totalAllocation,
109  static_cast<FwAssertArgType>(allocationSize),
110  static_cast<FwAssertArgType>(allocationOffset),
111  static_cast<FwAssertArgType>(totalAllocation));
112 
113  // Setup queue's memory allocation, depth, and message size. Setup is skipped for a depth 0 queue
114  if (allocationSize > 0) {
115  this->m_queues[this->m_prioritizedList[i].index].setup(
116  reinterpret_cast<U8*>(this->m_allocation) + allocationOffset, allocationSize,
117  this->m_prioritizedList[i].depth, this->m_prioritizedList[i].msgSize);
118  }
119  allocationOffset += allocationSize;
120  }
121  // Safety check that all memory was used as expected
122  FW_ASSERT(
123  allocationOffset == totalAllocation,
124  static_cast<FwAssertArgType>(allocationOffset),
125  static_cast<FwAssertArgType>(totalAllocation));
126 }
127 // ----------------------------------------------------------------------
128 // Handler implementations for user-defined typed input ports
129 // ----------------------------------------------------------------------
130 
131 void ComQueue::comQueueIn_handler(const NATIVE_INT_TYPE portNum, Fw::ComBuffer& data, U32 context) {
132  // Ensure that the port number of comQueueIn is consistent with the expectation
133  FW_ASSERT(portNum >= 0 && portNum < COM_PORT_COUNT, portNum);
134  this->enqueue(portNum, QueueType::COM_QUEUE, reinterpret_cast<const U8*>(&data), sizeof(Fw::ComBuffer));
135 }
136 
137 void ComQueue::buffQueueIn_handler(const NATIVE_INT_TYPE portNum, Fw::Buffer& fwBuffer) {
138  const NATIVE_INT_TYPE queueNum = portNum + COM_PORT_COUNT;
139  // Ensure that the port number of buffQueueIn is consistent with the expectation
140  FW_ASSERT(portNum >= 0 && portNum < BUFFER_PORT_COUNT, portNum);
141  FW_ASSERT(queueNum < TOTAL_PORT_COUNT);
142  this->enqueue(queueNum, QueueType::BUFFER_QUEUE, reinterpret_cast<const U8*>(&fwBuffer), sizeof(Fw::Buffer));
143 }
144 
145 void ComQueue::comStatusIn_handler(const NATIVE_INT_TYPE portNum, Fw::Success& condition) {
146  switch (this->m_state) {
147  // On success, the queue should be processed. On failure, the component should still wait.
148  case WAITING:
149  if (condition.e == Fw::Success::SUCCESS) {
150  this->m_state = READY;
151  this->processQueue();
152  // A message may or may not be sent. Thus, READY or WAITING are acceptable final states.
153  FW_ASSERT((this->m_state == WAITING || this->m_state == READY), this->m_state);
154  } else {
155  this->m_state = WAITING;
156  }
157  break;
158  // Both READY and unknown states should not be possible at this point. To receive a status message we must be
159  // one of the WAITING or RETRY states.
160  default:
161  FW_ASSERT(0, this->m_state);
162  break;
163  }
164 }
165 
166 void ComQueue::run_handler(const NATIVE_INT_TYPE portNum, U32 context) {
167  // Downlink the high-water marks for the Fw::ComBuffer array types
168  ComQueueDepth comQueueDepth;
169  for (U32 i = 0; i < comQueueDepth.SIZE; i++) {
170  comQueueDepth[i] = this->m_queues[i].get_high_water_mark();
171  this->m_queues[i].clear_high_water_mark();
172  }
173  this->tlmWrite_comQueueDepth(comQueueDepth);
174 
175  // Downlink the high-water marks for the Fw::Buffer array types
176  BuffQueueDepth buffQueueDepth;
177  for (U32 i = 0; i < buffQueueDepth.SIZE; i++) {
178  buffQueueDepth[i] = this->m_queues[i + COM_PORT_COUNT].get_high_water_mark();
179  this->m_queues[i + COM_PORT_COUNT].clear_high_water_mark();
180  }
181  this->tlmWrite_buffQueueDepth(buffQueueDepth);
182 }
183 
184 // ----------------------------------------------------------------------
185 // Private helper methods
186 // ----------------------------------------------------------------------
187 
188 void ComQueue::enqueue(const FwIndexType queueNum, QueueType queueType, const U8* data, const FwSizeType size) {
189  // Enqueue the given message onto the matching queue. When no space is available then emit the queue overflow event,
190  // set the appropriate throttle, and move on. Will assert if passed a message for a depth 0 queue.
191  const FwSizeType expectedSize = (queueType == QueueType::COM_QUEUE) ? sizeof(Fw::ComBuffer) : sizeof(Fw::Buffer);
192  const FwIndexType portNum = queueNum - ((queueType == QueueType::COM_QUEUE) ? 0 : COM_PORT_COUNT);
193  FW_ASSERT(
194  expectedSize == size,
195  static_cast<FwAssertArgType>(size),
196  static_cast<FwAssertArgType>(expectedSize));
197  FW_ASSERT(portNum >= 0, portNum);
198  Fw::SerializeStatus status = this->m_queues[queueNum].enqueue(data, size);
199  if (status == Fw::FW_SERIALIZE_NO_ROOM_LEFT && !this->m_throttle[queueNum]) {
200  this->log_WARNING_HI_QueueOverflow(queueType, static_cast<U32>(portNum));
201  this->m_throttle[queueNum] = true;
202  }
203  // When the component is already in READY state process the queue to send out the next available message immediately
204  if (this->m_state == READY) {
205  this->processQueue();
206  }
207 }
208 
209 void ComQueue::sendComBuffer(Fw::ComBuffer& comBuffer) {
210  FW_ASSERT(this->m_state == READY);
211  this->comQueueSend_out(0, comBuffer, 0);
212  this->m_state = WAITING;
213 }
214 
215 void ComQueue::sendBuffer(Fw::Buffer& buffer) {
216  // Retry buffer expected to be cleared as we are either transferring ownership or have already deallocated it.
217  FW_ASSERT(this->m_state == READY);
218  this->buffQueueSend_out(0, buffer);
219  this->m_state = WAITING;
220 }
221 
222 void ComQueue::processQueue() {
223  FwIndexType priorityIndex = 0;
224  FwIndexType sendPriority = 0;
225  // Check that we are in the appropriate state
226  FW_ASSERT(this->m_state == READY);
227 
228  // Walk all the queues in priority order. Send the first message that is available in priority order. No balancing
229  // is done within this loop.
230  for (priorityIndex = 0; priorityIndex < TOTAL_PORT_COUNT; priorityIndex++) {
231  QueueMetadata& entry = this->m_prioritizedList[priorityIndex];
232  Types::Queue& queue = this->m_queues[entry.index];
233 
234  // Continue onto next prioritized queue if there is no items in the current queue
235  if (queue.getQueueSize() == 0) {
236  continue;
237  }
238 
239  // Send out the message based on the type
240  if (entry.index < COM_PORT_COUNT) {
241  Fw::ComBuffer comBuffer;
242  queue.dequeue(reinterpret_cast<U8*>(&comBuffer), sizeof(comBuffer));
243  this->sendComBuffer(comBuffer);
244  } else {
245  Fw::Buffer buffer;
246  queue.dequeue(reinterpret_cast<U8*>(&buffer), sizeof(buffer));
247  this->sendBuffer(buffer);
248  }
249 
250  // Update the throttle and the index that was just sent
251  this->m_throttle[entry.index] = false;
252 
253  // Priority used in the next loop
254  sendPriority = entry.priority;
255  break;
256  }
257 
258  // Starting on the priority entry after the one dispatched and continuing through the end of the set of entries that
259  // share the same priority, rotate those entries such that the currently dispatched queue is last and the rest are
260  // shifted up by one. This effectively round-robins the queues of the same priority.
261  for (priorityIndex++;
262  priorityIndex < TOTAL_PORT_COUNT && (this->m_prioritizedList[priorityIndex].priority == sendPriority);
263  priorityIndex++) {
264  // Swap the previous entry with this one.
265  QueueMetadata temp = this->m_prioritizedList[priorityIndex];
266  this->m_prioritizedList[priorityIndex] = this->m_prioritizedList[priorityIndex - 1];
267  this->m_prioritizedList[priorityIndex - 1] = temp;
268  }
269 }
270 } // end namespace Svc
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:51
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:26
#define FW_NUM_ARRAY_ELEMENTS(a)
number of elements in an array
Definition: BasicTypes.h:66
PlatformUIntType NATIVE_UINT_TYPE
Definition: BasicTypes.h:52
C++ header for working with basic fprime types.
PlatformAssertArgType FwAssertArgType
Definition: FpConfig.h:34
PlatformSizeType FwSizeType
Definition: FpConfig.h:30
PlatformIndexType FwIndexType
Definition: FpConfig.h:20
virtual void deallocate(const NATIVE_UINT_TYPE identifier, void *ptr)=0
Deallocate memory.
virtual void * allocate(const NATIVE_UINT_TYPE identifier, NATIVE_UINT_TYPE &size, bool &recoverable)=0
Allocate memory.
void init()
Object initializer.
Definition: ObjBase.cpp:27
Success/Failure.
T e
The raw enum value.
@ SUCCESS
Representing success.
Auto-generated base for ComQueue component.
void tlmWrite_comQueueDepth(const Svc::ComQueueDepth &arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_buffQueueDepth(const Svc::BuffQueueDepth &arg, Fw::Time _tlmTime=Fw::Time())
void buffQueueSend_out(FwIndexType portNum, Fw::Buffer &fwBuffer)
Invoke output port buffQueueSend.
void comQueueSend_out(FwIndexType portNum, Fw::ComBuffer &data, U32 context)
Invoke output port comQueueSend.
void log_WARNING_HI_QueueOverflow(Svc::QueueType queueType, U32 index)
ComQueue(const char *const compName)
Definition: ComQueue.cpp:24
void configure(QueueConfigurationTable queueConfig, NATIVE_UINT_TYPE allocationId, Fw::MemAllocator &allocator)
Definition: ComQueue.cpp:49
void cleanup()
Definition: ComQueue.cpp:42
static const FwIndexType BUFFER_PORT_COUNT
Total count of input buffer ports and thus total queues.
Definition: ComQueue.hpp:29
static const FwIndexType COM_PORT_COUNT
< Count of Fw::Com input ports and thus Fw::Com queues
Definition: ComQueue.hpp:26
static const FwIndexType TOTAL_PORT_COUNT
Definition: ComQueue.hpp:32
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
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_NO_ROOM_LEFT
No room left in the buffer to serialize data.
FwIndexType priority
Priority of the queue [0, TOTAL_PORT_COUNT)
Definition: ComQueue.hpp:47
FwSizeType depth
Depth of the queue [0, infinity)
Definition: ComQueue.hpp:46
configuration table for each queue
Definition: ComQueue.hpp:59
QueueConfigurationEntry entries[TOTAL_PORT_COUNT]
Definition: ComQueue.hpp:60