GCC Code Coverage Report


Directory: ./
File: Svc/ComQueue/ComQueue.cpp
Date: 2026-09-23 21:11:01
Exec Total Coverage
Lines: 268 273 98.2%
Functions: 23 23 100.0%
Branches: 190 208 91.3%

Line Branch Exec Source
1 // ======================================================================
2 // \title ComQueue.cpp
3 // \author vbai
4 // \brief cpp file for ComQueue component implementation class
5 // ======================================================================
6
7 #include <Fw/Com/ComPacket.hpp>
8 #include <Fw/Types/Assert.hpp>
9 #include <Svc/ComQueue/ComQueue.hpp>
10 #include <type_traits>
11 #include "Fw/Types/BasicTypes.hpp"
12
13 namespace Svc {
14
15 // ----------------------------------------------------------------------
16 // Construction, initialization, and destruction
17 // ----------------------------------------------------------------------
18
19 using FwUnsignedIndexType = std::make_unsigned<FwIndexType>::type;
20
21 25 ComQueue ::QueueConfigurationTable ::QueueConfigurationTable() {
22 static_assert(static_cast<FwUnsignedIndexType>(std::numeric_limits<FwIndexType>::max()) >=
23 FW_NUM_ARRAY_ELEMENTS(this->entries),
24 "Number of entries must fit into FwIndexType");
25
2/2
✓ Branch 1 taken 75 times.
✓ Branch 2 taken 25 times.
100 for (FwIndexType i = 0; i < static_cast<FwIndexType>(FW_NUM_ARRAY_ELEMENTS(this->entries)); i++) {
26 75 this->entries[i].priority = 0;
27 75 this->entries[i].depth = 0;
28 75 this->entries[i].mode = Types::QUEUE_FIFO;
29 75 this->entries[i].overflowMode = Types::QUEUE_DROP_NEWEST;
30 }
31 25 }
32
33 26 ComQueue ::ComQueue(const char* const compName)
34 : ComQueueComponentBase(compName),
35 26 m_state(WAITING),
36 26 m_buffer_state(OWNED),
37 26 m_allocationId(static_cast<FwEnumStoreType>(-1)),
38 26 m_allocator(nullptr),
39
4/4
✓ Branch 9 taken 26 times.
✓ Branch 15 taken 78 times.
✓ Branch 18 taken 78 times.
✓ Branch 19 taken 26 times.
130 m_allocation(nullptr) {
40 // Initialize throttles to "off"
41
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 26 times.
104 for (FwIndexType i = 0; i < TOTAL_PORT_COUNT; i++) {
42 78 this->m_throttle[i] = false;
43 }
44
45 static_assert(TOTAL_PORT_COUNT >= 1, "ComQueue must have more than one port");
46 26 }
47
48 52 ComQueue ::~ComQueue() {}
49
50 25 void ComQueue ::cleanup() {
51 // Deallocate memory ignoring error conditions
52
3/4
✓ Branch 4 taken 24 times.
✓ Branch 5 taken 1 times.
✓ Branch 10 taken 24 times.
✗ Branch 11 not taken.
25 if ((this->m_allocator != nullptr) && (this->m_allocation != nullptr)) {
53 24 this->m_allocator->deallocate(this->m_allocationId, this->m_allocation);
54 }
55 25 }
56
57 24 void ComQueue::configure(const QueueConfigurationTable& queueConfig,
58 FwEnumStoreType allocationId,
59 Fw::MemAllocator& allocator) {
60 24 FwIndexType currentPriorityIndex = 0;
61 24 FwSizeType totalAllocation = 0;
62
63 // Store/initialize allocator members
64 24 this->m_allocator = &allocator;
65 24 this->m_allocationId = allocationId;
66 24 this->m_allocation = nullptr;
67
68 // Initializes the sorted queue metadata list in priority (sorted) order. This is accomplished by walking the
69 // priority values in priority order from 0 to TOTAL_PORT_COUNT. At each priory value, the supplied queue
70 // configuration table is walked and any entry matching the current priority values is used to add queue metadata to
71 // the prioritized list. This results in priority-sorted queue metadata objects that index back into the unsorted
72 // queue data structures.
73 //
74 // The total allocation size is tracked for passing to the allocation call and is a summation of
75 // (depth * message size) for each prioritized metadata object of (depth * message size)
76
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 24 times.
96 for (FwIndexType currentPriority = 0; currentPriority < TOTAL_PORT_COUNT; currentPriority++) {
77 // Walk each queue configuration entry and add them into the prioritized metadata list when matching the current
78 // priority value
79 288 for (FwIndexType entryIndex = 0;
80
2/2
✓ Branch 1 taken 216 times.
✓ Branch 2 taken 72 times.
288 entryIndex < static_cast<FwIndexType>(FW_NUM_ARRAY_ELEMENTS(queueConfig.entries)); entryIndex++) {
81 // Check for valid configuration entry
82 216 FW_ASSERT(queueConfig.entries[entryIndex].priority < TOTAL_PORT_COUNT,
83 static_cast<FwAssertArgType>(queueConfig.entries[entryIndex].priority),
84 static_cast<FwAssertArgType>(TOTAL_PORT_COUNT), static_cast<FwAssertArgType>(entryIndex));
85
2/2
✓ Branch 2 taken 72 times.
✓ Branch 3 taken 144 times.
216 if (currentPriority == queueConfig.entries[entryIndex].priority) {
86 // Set up the queue metadata object in order to track priority, depth, index into the queue list of the
87 // backing queue object, and message size. Both index and message size are calculated where priority and
88 // depth are copied from the configuration object.
89 72 QueueMetadata& entry = this->m_prioritizedList[currentPriorityIndex];
90 72 entry.priority = queueConfig.entries[entryIndex].priority;
91 72 entry.depth = queueConfig.entries[entryIndex].depth;
92
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
72 entry.mode = queueConfig.entries[entryIndex].mode;
93
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
72 entry.overflowMode = queueConfig.entries[entryIndex].overflowMode;
94 72 entry.index = entryIndex;
95 // Message size is determined by the type of object being stored, which in turn is determined by the
96 // index of the entry. Those lower than COM_PORT_COUNT are Fw::ComBuffers and those larger Fw::Buffer.
97
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
72 entry.msgSize = (entryIndex < COM_PORT_COUNT) ? static_cast<FwSizeType>(Fw::ComBuffer::SERIALIZED_SIZE)
98 : static_cast<FwSizeType>(Fw::Buffer::SERIALIZED_SIZE);
99 // Overflow checks. A depth of 0 disables the queue and contributes no storage.
100
2/2
✓ Branch 1 taken 70 times.
✓ Branch 2 taken 2 times.
72 if (entry.depth > 0) {
101 70 FW_ASSERT((std::numeric_limits<FwSizeType>::max() / entry.depth) >= entry.msgSize,
102 static_cast<FwAssertArgType>(entry.depth), static_cast<FwAssertArgType>(entry.msgSize));
103 70 FW_ASSERT(std::numeric_limits<FwSizeType>::max() - (entry.depth * entry.msgSize) >=
104 totalAllocation);
105 70 totalAllocation += entry.depth * entry.msgSize;
106 }
107 72 currentPriorityIndex++;
108 }
109 }
110 }
111 // At least one queue must be enabled; an all-zero table is the default-constructed (unconfigured) table
112 24 FW_ASSERT(totalAllocation > 0);
113 // Allocate a single chunk of memory from the memory allocator. Memory recover is neither needed nor used.
114 24 bool recoverable = false;
115 24 FwSizeType actualAllocation = totalAllocation;
116
1/1
✓ Branch 28 taken 24 times.
24 this->m_allocation = this->m_allocator->allocate(this->m_allocationId, actualAllocation, recoverable);
117 24 FW_ASSERT(this->m_allocation != nullptr);
118 24 FW_ASSERT(actualAllocation >= totalAllocation, static_cast<FwAssertArgType>(actualAllocation),
119 static_cast<FwAssertArgType>(totalAllocation));
120
121 // Each of the backing queue objects must be supplied memory to store the queued messages. These data regions are
122 // sub-portions of the total allocated data. This memory is passed out by looping through each queue in prioritized
123 // order and passing out the memory to each queue's setup method.
124 24 FwSizeType allocationOffset = 0;
125
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 24 times.
96 for (FwIndexType i = 0; i < TOTAL_PORT_COUNT; i++) {
126 // Get current queue's allocation size and safety check the values
127 72 FwSizeType allocationSize = this->m_prioritizedList[i].depth * this->m_prioritizedList[i].msgSize;
128 72 FW_ASSERT(this->m_prioritizedList[i].index < static_cast<FwIndexType>(FW_NUM_ARRAY_ELEMENTS(this->m_queues)),
129 static_cast<FwAssertArgType>(this->m_prioritizedList[i].index));
130 72 FW_ASSERT((allocationSize + allocationOffset) <= totalAllocation, static_cast<FwAssertArgType>(allocationSize),
131 static_cast<FwAssertArgType>(allocationOffset), static_cast<FwAssertArgType>(totalAllocation));
132
133 // Setup queue's memory allocation, depth, and message size. Setup is skipped for a disabled (depth 0) queue
134
2/2
✓ Branch 5 taken 70 times.
✓ Branch 6 taken 2 times.
72 if (this->m_prioritizedList[i].depth > 0) {
135
3/5
✗ Branch 10 not taken.
✓ Branch 11 taken 70 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 70 times.
✓ Branch 20 taken 70 times.
420 this->m_queues[this->m_prioritizedList[i].index].setup(
136 70 reinterpret_cast<U8*>(this->m_allocation) + allocationOffset, allocationSize,
137 210 this->m_prioritizedList[i].depth, this->m_prioritizedList[i].msgSize, this->m_prioritizedList[i].mode,
138 70 this->m_prioritizedList[i].overflowMode);
139 }
140 72 allocationOffset += allocationSize;
141 }
142 // Safety check that all memory was used as expected
143 24 FW_ASSERT(allocationOffset == totalAllocation, static_cast<FwAssertArgType>(allocationOffset),
144 static_cast<FwAssertArgType>(totalAllocation));
145 24 }
146
147 // ----------------------------------------------------------------------
148 // Handler implementations for commands
149 // ----------------------------------------------------------------------
150
151 12 void ComQueue ::FLUSH_QUEUE_cmdHandler(FwOpcodeType opCode,
152 U32 cmdSeq,
153 const Svc::QueueType& queueType,
154 FwIndexType index) {
155 // Acquire the queue that we need to drain
156
1/1
✓ Branch 5 taken 12 times.
12 FwIndexType queueIndex = this->getQueueNum(queueType, index);
157
158 // Validate queue index
159
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
12 if (queueIndex < 0 || queueIndex >= TOTAL_PORT_COUNT) {
160
2/2
✓ Branch 6 taken 6 times.
✓ Branch 9 taken 6 times.
6 this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::VALIDATION_ERROR);
161 6 return;
162 }
163 6 FW_ASSERT(queueIndex >= 0 && queueIndex < TOTAL_PORT_COUNT, static_cast<FwAssertArgType>(queueIndex));
164
165 6 this->drainQueue(queueIndex);
166
2/2
✓ Branch 6 taken 6 times.
✓ Branch 9 taken 6 times.
6 this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
167 }
168
169 2 void ComQueue ::FLUSH_ALL_QUEUES_cmdHandler(FwOpcodeType opCode, U32 cmdSeq) {
170
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (FwIndexType i = 0; i < TOTAL_PORT_COUNT; i++) {
171 6 this->drainQueue(i);
172 }
173
2/2
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
2 this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
174 2 }
175
176 9 void ComQueue::SET_QUEUE_PRIORITY_cmdHandler(FwOpcodeType opCode,
177 U32 cmdSeq,
178 const Svc::QueueType& queueType,
179 FwIndexType index,
180 FwIndexType newPriority) {
181 // Acquire the queue we are to reprioritize
182
1/1
✓ Branch 5 taken 9 times.
9 FwIndexType queueIndex = this->getQueueNum(queueType, index);
183
184 // Validate queue index
185
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
9 if (queueIndex < 0 || queueIndex >= TOTAL_PORT_COUNT) {
186
2/2
✓ Branch 6 taken 6 times.
✓ Branch 9 taken 6 times.
6 this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::VALIDATION_ERROR);
187 6 return;
188 }
189
190 // Validate priority range
191
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
3 if (newPriority < 0 || newPriority >= TOTAL_PORT_COUNT) {
192
2/2
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
2 this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::VALIDATION_ERROR);
193 2 return;
194 }
195
196 // Find our queue in the prioritized list & update the priority
197
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 for (FwIndexType prioIndex = 0; prioIndex < TOTAL_PORT_COUNT; prioIndex++) {
198 // Each entry must reference a valid queue index
199 1 FW_ASSERT(m_prioritizedList[prioIndex].index >= 0 && m_prioritizedList[prioIndex].index < TOTAL_PORT_COUNT,
200 static_cast<FwAssertArgType>(m_prioritizedList[prioIndex].index));
201 // If the port based index matches, then update
202
1/2
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
1 if (m_prioritizedList[prioIndex].index == queueIndex) {
203 1 m_prioritizedList[prioIndex].priority = newPriority;
204 1 break; // Since we shouldn't find more than one queue at this port index
205 }
206 }
207
208 // Re-sort the prioritized list to maintain priority ordering
209 // Using simple bubble sort since TOTAL_PORT_COUNT is typically small
210
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (FwIndexType i = 0; i < TOTAL_PORT_COUNT - 1; i++) {
211
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
5 for (FwIndexType j = 0; (j < TOTAL_PORT_COUNT - i - 1) && (j < TOTAL_PORT_COUNT - 1); j++) {
212
2/2
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 2 times.
3 if (m_prioritizedList[j].priority > m_prioritizedList[j + 1].priority) {
213 // Swap metadata
214 1 QueueMetadata temp = m_prioritizedList[j];
215 1 m_prioritizedList[j] = m_prioritizedList[j + 1];
216 1 m_prioritizedList[j + 1] = temp;
217 }
218 }
219 }
220
221 // Emit event for successful priority change
222 1 this->log_ACTIVITY_HI_QueuePriorityChanged(queueType, index, newPriority);
223
224 // Send command response
225
2/2
✓ Branch 6 taken 1 times.
✓ Branch 9 taken 1 times.
1 this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
226 }
227
228 // ----------------------------------------------------------------------
229 // Handler implementations for user-defined typed input ports
230 // ----------------------------------------------------------------------
231
232 44 void ComQueue::comPacketQueueIn_handler(const FwIndexType portNum, Fw::ComBuffer& data, U32 context) {
233 // Ensure that the port number of comPacketQueueIn is consistent with the expectation
234 44 FW_ASSERT(portNum >= 0 && portNum < COM_PORT_COUNT, static_cast<FwAssertArgType>(portNum));
235 44 (void)this->enqueue(portNum, data);
236 44 }
237
238 24 void ComQueue::bufferQueueIn_handler(const FwIndexType portNum, Fw::Buffer& fwBuffer) {
239 24 FW_ASSERT(std::numeric_limits<FwIndexType>::max() - COM_PORT_COUNT > portNum);
240 24 const FwIndexType queueNum = static_cast<FwIndexType>(portNum + COM_PORT_COUNT);
241 // Ensure that the port number of bufferQueueIn is consistent with the expectation
242 24 FW_ASSERT(portNum >= 0 && portNum < BUFFER_PORT_COUNT, static_cast<FwAssertArgType>(portNum));
243 24 FW_ASSERT(queueNum < TOTAL_PORT_COUNT);
244 24 bool success = this->enqueue(queueNum, fwBuffer);
245
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 20 times.
24 if (!success) {
246 4 this->bufferReturnOut_out(portNum, fwBuffer);
247 }
248 24 }
249
250 57 void ComQueue::comStatusIn_handler(const FwIndexType portNum, Fw::Success& condition) {
251
2/4
✗ Branch 3 not taken.
✓ Branch 4 taken 57 times.
✓ Branch 5 taken 57 times.
✗ Branch 6 not taken.
57 switch (this->m_state) {
252 // On success, the queue should be processed. On failure, the component should still wait.
253 57 case WAITING:
254
3/4
✗ Branch 3 not taken.
✓ Branch 4 taken 57 times.
✓ Branch 5 taken 48 times.
✓ Branch 6 taken 9 times.
57 if (condition.e == Fw::Success::SUCCESS) {
255 48 this->m_state = READY;
256 48 this->processQueue();
257 // A message may or may not be sent. Thus, READY or WAITING are acceptable final states.
258 48 FW_ASSERT((this->m_state == WAITING || this->m_state == READY),
259 static_cast<FwAssertArgType>(this->m_state));
260 } else {
261 9 this->m_state = WAITING;
262 }
263 57 break;
264 // Both READY and unknown states should not be possible at this point. To receive a status message we must be
265 // one of the WAITING or RETRY states.
266 ✗ default:
267 ✗ FW_ASSERT(false, static_cast<FwAssertArgType>(this->m_state));
268 ✗ break;
269 }
270 57 }
271
272 2 void ComQueue::run_handler(const FwIndexType portNum, U32 context) {
273 // Downlink the high-water marks for the Fw::ComBuffer array types. Disabled (depth 0) queues report 0.
274
1/1
✓ Branch 2 taken 2 times.
2 ComQueueDepth comQueueDepth;
275 FW_ASSERT(comQueueDepth.SIZE <= COM_PORT_COUNT, static_cast<FwAssertArgType>(comQueueDepth.SIZE));
276
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (U32 i = 0; i < comQueueDepth.SIZE; i++) {
277 4 const FwIndexType queueNum = static_cast<FwIndexType>(i);
278
1/1
✓ Branch 2 taken 4 times.
4 comQueueDepth[i] = 0;
279
3/3
✓ Branch 4 taken 4 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 1 times.
4 if (this->getQueueDepth(queueNum) > 0) {
280
2/2
✓ Branch 5 taken 3 times.
✓ Branch 9 taken 3 times.
3 comQueueDepth[i] = static_cast<U32>(this->m_queues[queueNum].get_high_water_mark());
281
1/1
✓ Branch 5 taken 3 times.
3 this->m_queues[queueNum].clear_high_water_mark();
282 }
283 }
284
2/2
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
2 this->tlmWrite_comQueueDepth(comQueueDepth);
285
286 // Downlink the high-water marks for the Fw::Buffer array types
287
1/1
✓ Branch 2 taken 2 times.
2 BuffQueueDepth buffQueueDepth;
288 FW_ASSERT((buffQueueDepth.SIZE + COM_PORT_COUNT) <= TOTAL_PORT_COUNT,
289 static_cast<FwAssertArgType>(buffQueueDepth.SIZE));
290
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (U32 i = 0; i < buffQueueDepth.SIZE; i++) {
291 2 const FwIndexType queueNum = static_cast<FwIndexType>(i + COM_PORT_COUNT);
292
1/1
✓ Branch 2 taken 2 times.
2 buffQueueDepth[i] = 0;
293
3/3
✓ Branch 4 taken 2 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 1 times.
2 if (this->getQueueDepth(queueNum) > 0) {
294
2/2
✓ Branch 5 taken 1 times.
✓ Branch 9 taken 1 times.
1 buffQueueDepth[i] = static_cast<U32>(this->m_queues[queueNum].get_high_water_mark());
295
1/1
✓ Branch 5 taken 1 times.
1 this->m_queues[queueNum].clear_high_water_mark();
296 }
297 }
298
2/2
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
2 this->tlmWrite_buffQueueDepth(buffQueueDepth);
299 4 }
300
301 45 void ComQueue ::dataReturnIn_handler(FwIndexType portNum, Fw::Buffer& data, const ComCfg::FrameContext& context) {
302 static_assert(std::numeric_limits<FwIndexType>::is_signed, "FwIndexType must be signed");
303 // This handler runs on the returning caller's thread: take ownership atomically
304 45 const BufferState previousState = this->m_buffer_state.exchange(OWNED);
305 45 FW_ASSERT(previousState == UNOWNED, static_cast<FwAssertArgType>(previousState));
306 // For the buffer queues, the index of the queue is portNum offset by COM_PORT_COUNT since
307 // the first COM_PORT_COUNT queues are for ComBuffer. So we have for buffer queues:
308 // queueNum = portNum + COM_PORT_COUNT
309 // Since queueNum is used as APID, we can retrieve the original portNum like such:
310 45 FwIndexType bufferReturnPortNum = static_cast<FwIndexType>(context.get_comQueueIndex() - ComQueue::COM_PORT_COUNT);
311 // Failing this assert means that context.apid was modified since ComQueue set it, which should not happen
312 45 FW_ASSERT(bufferReturnPortNum < BUFFER_PORT_COUNT, static_cast<FwAssertArgType>(bufferReturnPortNum));
313
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 29 times.
45 if (bufferReturnPortNum >= 0) {
314 // It is a coding error not to connect the associated bufferReturnOut port for each dataReturnIn port
315 16 FW_ASSERT(this->isConnected_bufferReturnOut_OutputPort(bufferReturnPortNum),
316 static_cast<FwAssertArgType>(bufferReturnPortNum));
317 // If this is a buffer port, return the buffer to the BufferDownlink
318 16 this->bufferReturnOut_out(bufferReturnPortNum, data);
319 }
320 45 }
321
322 // ----------------------------------------------------------------------
323 // Hook implementations for typed async input ports
324 // ----------------------------------------------------------------------
325
326 2 void ComQueue::bufferQueueIn_overflowHook(FwIndexType portNum, Fw::Buffer& fwBuffer) {
327 2 FW_ASSERT(portNum >= 0 && portNum < BUFFER_PORT_COUNT, static_cast<FwAssertArgType>(portNum));
328 2 this->bufferReturnOut_out(portNum, fwBuffer);
329 2 }
330
331 // ----------------------------------------------------------------------
332 // Private helper methods
333 // ----------------------------------------------------------------------
334
335 44 bool ComQueue::enqueue(const FwIndexType queueNum, const Fw::ComBuffer& data) {
336 // Enqueue the given message onto the matching queue. When no space is available then emit the queue overflow event,
337 // set the appropriate throttle, and move on. A disabled (depth 0) queue has no space and always overflows.
338 44 FW_ASSERT(queueNum >= 0 && queueNum < COM_PORT_COUNT, static_cast<FwAssertArgType>(queueNum));
339
2/2
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 42 times.
44 if (this->getQueueDepth(queueNum) == 0) {
340
2/2
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
4 return this->handleEnqueueStatus(queueNum, QueueType::COM_QUEUE, queueNum, false,
341 2 Fw::FW_SERIALIZE_NO_ROOM_LEFT);
342 }
343
344 42 const Fw::SerializeStatus status = this->m_queues[queueNum].enqueue(data);
345
2/2
✓ Branch 5 taken 42 times.
✓ Branch 8 taken 42 times.
42 return this->handleEnqueueStatus(queueNum, QueueType::COM_QUEUE, queueNum, false, status);
346 }
347
348 24 bool ComQueue::enqueue(const FwIndexType queueNum, const Fw::Buffer& data) {
349 // Enqueue the given message onto the matching queue. When no space is available then emit the queue overflow event,
350 // set the appropriate throttle, and move on. A disabled (depth 0) queue has no space and always overflows.
351 24 FW_ASSERT(queueNum >= COM_PORT_COUNT && queueNum < TOTAL_PORT_COUNT, static_cast<FwAssertArgType>(queueNum));
352 24 const FwIndexType portNum = static_cast<FwIndexType>(queueNum - COM_PORT_COUNT);
353
2/2
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 23 times.
24 if (this->getQueueDepth(queueNum) == 0) {
354
2/2
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
2 return this->handleEnqueueStatus(queueNum, QueueType::BUFFER_QUEUE, portNum, false,
355 1 Fw::FW_SERIALIZE_NO_ROOM_LEFT);
356 }
357
358 // For buffer queues with DROP_OLDEST, check if the queue is full before enqueuing.
359 // If full, dequeue the oldest entry first so we can return buffer ownership before
360 // Queue::enqueue() silently discards it via rotate. This prevents buffer-pool leaks.
361 23 bool preEmptiveOverflow = false;
362 23 Types::Queue& queue = this->m_queues[queueNum];
363
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 22 times.
89 for (FwIndexType i = 0; i < TOTAL_PORT_COUNT; i++) {
364 67 if (this->m_prioritizedList[i].index == queueNum &&
365
7/8
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 44 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 23 times.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 20 times.
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 66 times.
70 this->m_prioritizedList[i].overflowMode == Types::QUEUE_DROP_OLDEST &&
366
2/2
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 2 times.
3 queue.getQueueSize() >= this->m_prioritizedList[i].depth) {
367 // Queue is full and will drop oldest; remove the front entry to return ownership.
368 // popFront() always removes from the front (oldest) regardless of queue mode,
369 // matching the rotate-based removal that Queue::enqueue() uses for DROP_OLDEST.
370
1/1
✓ Branch 2 taken 1 times.
1 Fw::Buffer droppedBuffer;
371
1/1
✓ Branch 2 taken 1 times.
1 Fw::SerializeStatus dequeueStatus = queue.popFront(droppedBuffer);
372 1 FW_ASSERT(dequeueStatus == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(dequeueStatus));
373
1/1
✓ Branch 5 taken 1 times.
1 this->bufferReturnOut_out(portNum, droppedBuffer);
374 1 preEmptiveOverflow = true;
375 1 break;
376 1 }
377 }
378
379 23 const Fw::SerializeStatus status = this->m_queues[queueNum].enqueue(data);
380
2/2
✓ Branch 5 taken 23 times.
✓ Branch 8 taken 23 times.
23 return this->handleEnqueueStatus(queueNum, QueueType::BUFFER_QUEUE, portNum, preEmptiveOverflow, status);
381 }
382
383 68 bool ComQueue::handleEnqueueStatus(const FwIndexType queueNum,
384 QueueType queueType,
385 const FwIndexType portNum,
386 const bool preEmptiveOverflow,
387 const Fw::SerializeStatus status) {
388
6/6
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 54 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 52 times.
68 if (preEmptiveOverflow || status == Fw::FW_SERIALIZE_NO_ROOM_LEFT ||
389 status == Fw::FW_SERIALIZE_DISCARDED_EXISTING) {
390
3/4
✗ Branch 4 not taken.
✓ Branch 5 taken 16 times.
✓ Branch 6 taken 12 times.
✓ Branch 7 taken 4 times.
16 if (!this->m_throttle[queueNum]) {
391 12 this->log_WARNING_HI_QueueOverflow(queueType, portNum);
392 12 this->m_throttle[queueNum] = true;
393 }
394 }
395
396 // When the component is already in READY state process the queue to send out the next available message immediately
397
3/4
✗ Branch 3 not taken.
✓ Branch 4 taken 68 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 64 times.
68 if (this->m_state == READY) {
398 4 this->processQueue();
399 }
400
401 // Check if the buffer was accepted or must be returned
402 68 return status != Fw::FW_SERIALIZE_NO_ROOM_LEFT;
403 }
404
405 29 void ComQueue::sendComBuffer(Fw::ComBuffer& comBuffer, FwIndexType queueIndex) {
406 29 FW_ASSERT(this->m_state == READY);
407
2/2
✓ Branch 6 taken 29 times.
✓ Branch 14 taken 29 times.
29 Fw::Buffer outBuffer(comBuffer.getBuffAddr(), static_cast<Fw::Buffer::SizeType>(comBuffer.getSize()));
408
409 // Context value is used to determine what to do when the buffer returns on the dataReturnIn port
410
1/1
✓ Branch 2 taken 29 times.
29 ComCfg::FrameContext context;
411 29 FwPacketDescriptorType descriptor = 0;
412
1/1
✓ Branch 5 taken 29 times.
29 Fw::SerializeStatus status = comBuffer.deserializeTo(descriptor);
413 29 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
414
1/1
✓ Branch 2 taken 29 times.
29 context.set_apid(static_cast<ComCfg::Apid::T>(descriptor));
415
1/1
✓ Branch 2 taken 29 times.
29 context.set_comQueueIndex(queueIndex);
416 29 const BufferState previousState = this->m_buffer_state.exchange(UNOWNED);
417 29 FW_ASSERT(previousState == OWNED, static_cast<FwAssertArgType>(previousState));
418
1/1
✓ Branch 5 taken 29 times.
29 this->dataOut_out(0, outBuffer, context);
419 // Set state to WAITING for the status to come back
420 29 this->m_state = WAITING;
421 58 }
422
423 16 void ComQueue::sendBuffer(Fw::Buffer& buffer, FwIndexType queueIndex) {
424 // Retry buffer expected to be cleared as we are either transferring ownership or have already deallocated it.
425 16 FW_ASSERT(this->m_state == READY);
426
427 // Context value is used to determine what to do when the buffer returns on the dataReturnIn port
428
1/1
✓ Branch 2 taken 16 times.
16 ComCfg::FrameContext context;
429 16 FwPacketDescriptorType descriptor;
430
2/2
✓ Branch 2 taken 16 times.
✓ Branch 8 taken 16 times.
16 Fw::SerializeStatus status = buffer.getDeserializer().deserializeTo(descriptor);
431 16 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
432
1/1
✓ Branch 2 taken 16 times.
16 context.set_apid(static_cast<ComCfg::Apid::T>(descriptor));
433
1/1
✓ Branch 2 taken 16 times.
16 context.set_comQueueIndex(queueIndex);
434 16 const BufferState previousState = this->m_buffer_state.exchange(UNOWNED);
435 16 FW_ASSERT(previousState == OWNED, static_cast<FwAssertArgType>(previousState));
436
1/1
✓ Branch 5 taken 16 times.
16 this->dataOut_out(0, buffer, context);
437 // Set state to WAITING for the status to come back
438 16 this->m_state = WAITING;
439 32 }
440
441 12 void ComQueue::drainQueue(FwIndexType index) {
442 12 FW_ASSERT(index >= 0 && index < TOTAL_PORT_COUNT, static_cast<FwAssertArgType>(index));
443 // A disabled (depth 0) queue holds no messages and has no backing storage to drain
444
2/2
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 8 times.
12 if (this->getQueueDepth(index) == 0) {
445 4 return;
446 }
447 8 Types::Queue& queue = this->m_queues[index];
448
449 // Read all messages from the queue and discard them
450 8 Fw::SerializeStatus status = Fw::FW_SERIALIZE_OK;
451 8 const FwSizeType available = queue.getQueueSize();
452
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
15 for (FwSizeType i = 0; (i < available) && (status == Fw::FW_SERIALIZE_OK); i++) {
453
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
7 if (index < COM_PORT_COUNT) {
454 // Dequeueing deserializes the persisted Fw::ComBuffer from the queue's storage
455
1/1
✓ Branch 2 taken 4 times.
4 Fw::ComBuffer comBuffer;
456
1/1
✓ Branch 2 taken 4 times.
4 status = queue.dequeue(comBuffer);
457 4 } else {
458 // For buffer queues, if the buffer requires ownership return, return it via the bufferReturnOut port
459 // Dequeueing deserializes the persisted Fw::Buffer from the queue's storage
460
1/1
✓ Branch 2 taken 3 times.
3 Fw::Buffer buffer;
461
1/1
✓ Branch 2 taken 3 times.
3 status = queue.dequeue(buffer);
462
1/1
✓ Branch 5 taken 3 times.
3 this->bufferReturnOut_out(static_cast<FwIndexType>(index - COM_PORT_COUNT), buffer);
463 3 }
464 }
465 }
466
467 52 void ComQueue::processQueue() {
468 52 FwIndexType priorityIndex = 0;
469 52 FwIndexType sendPriority = 0;
470 // Check that we are in the appropriate state
471 52 FW_ASSERT(this->m_state == READY);
472
473 // Walk all the queues in priority order. Send the first message that is available in priority order. No balancing
474 // is done within this loop.
475
2/2
✓ Branch 0 taken 89 times.
✓ Branch 1 taken 7 times.
96 for (priorityIndex = 0; priorityIndex < TOTAL_PORT_COUNT; priorityIndex++) {
476 89 QueueMetadata& entry = this->m_prioritizedList[priorityIndex];
477 89 Types::Queue& queue = this->m_queues[entry.index];
478
479 // Continue onto next prioritized queue if the current queue is disabled (depth 0) or holds no items
480
6/6
✓ Branch 1 taken 85 times.
✓ Branch 2 taken 4 times.
✓ Branch 5 taken 40 times.
✓ Branch 6 taken 45 times.
✓ Branch 7 taken 44 times.
✓ Branch 8 taken 45 times.
89 if ((entry.depth == 0) || (queue.getQueueSize() == 0)) {
481 44 continue;
482 }
483
484 // Send out the message based on the type
485
2/2
✓ Branch 2 taken 29 times.
✓ Branch 3 taken 16 times.
45 if (entry.index < COM_PORT_COUNT) {
486 // Dequeue deserializes the persisted Fw::ComBuffer from the queue's storage
487 29 FW_ASSERT(this->m_buffer_state.load() == OWNED);
488 29 auto dequeue_status = queue.dequeue(this->m_dequeued_com_buffer);
489 29 FW_ASSERT(dequeue_status == Fw::SerializeStatus::FW_SERIALIZE_OK,
490 static_cast<FwAssertArgType>(dequeue_status));
491 29 this->sendComBuffer(this->m_dequeued_com_buffer, entry.index);
492 } else {
493
1/1
✓ Branch 2 taken 16 times.
16 Fw::Buffer buffer;
494
1/1
✓ Branch 2 taken 16 times.
16 auto dequeue_status = queue.dequeue(buffer);
495 16 FW_ASSERT(dequeue_status == Fw::SerializeStatus::FW_SERIALIZE_OK,
496 static_cast<FwAssertArgType>(dequeue_status));
497
1/1
✓ Branch 6 taken 16 times.
16 this->sendBuffer(buffer, entry.index);
498 16 }
499
500 // Update the throttle and the index that was just sent
501 45 this->m_throttle[entry.index] = false;
502
503 // Priority used in the next loop
504 45 sendPriority = entry.priority;
505 45 break;
506 }
507
508 // Starting on the priority entry after the one dispatched and continuing through the end of the set of entries that
509 // share the same priority, rotate those entries such that the currently dispatched queue is last and the rest are
510 // shifted up by one. This effectively round-robins the queues of the same priority.
511 54 for (priorityIndex++;
512
4/4
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 15 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 37 times.
54 priorityIndex < TOTAL_PORT_COUNT && (this->m_prioritizedList[priorityIndex].priority == sendPriority);
513 priorityIndex++) {
514 // Swap the previous entry with this one.
515 2 QueueMetadata temp = this->m_prioritizedList[priorityIndex];
516 2 this->m_prioritizedList[priorityIndex] = this->m_prioritizedList[priorityIndex - 1];
517 2 this->m_prioritizedList[priorityIndex - 1] = temp;
518 }
519 52 }
520
521 21 FwIndexType ComQueue::getQueueNum(Svc::QueueType queueType, FwIndexType portNum) {
522 // Validate against the per-type port count before folding so an invalid index cannot alias another queue type
523
2/2
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 10 times.
21 const FwIndexType portCount = (queueType == QueueType::COM_QUEUE) ? COM_PORT_COUNT : BUFFER_PORT_COUNT;
524
4/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 9 times.
21 if (portNum < 0 || portNum >= portCount) {
525 12 return -1;
526 }
527
2/2
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 4 times.
9 return static_cast<FwIndexType>(portNum + ((queueType == QueueType::COM_QUEUE) ? 0 : COM_PORT_COUNT));
528 }
529
530 86 FwSizeType ComQueue::getQueueDepth(const FwIndexType queueNum) const {
531 86 FW_ASSERT(queueNum >= 0 && queueNum < TOTAL_PORT_COUNT, static_cast<FwAssertArgType>(queueNum));
532
1/2
✓ Branch 0 taken 151 times.
✗ Branch 1 not taken.
151 for (FwIndexType i = 0; i < TOTAL_PORT_COUNT; i++) {
533
2/2
✓ Branch 5 taken 86 times.
✓ Branch 6 taken 65 times.
151 if (this->m_prioritizedList[i].index == queueNum) {
534 86 return this->m_prioritizedList[i].depth;
535 }
536 }
537 // configure() places exactly one metadata entry per queue in the prioritized list
538 ✗ FW_ASSERT(false, static_cast<FwAssertArgType>(queueNum));
539 ✗ return 0;
540 }
541 } // end namespace Svc
542