GCC Code Coverage Report


Directory: ./
File: Svc/ComAggregator/ComAggregator.cpp
Date: 2026-11-03 21:15:54
Exec Total Coverage
Lines: 197 200 98.5%
Functions: 27 28 96.4%
Branches: 86 97 88.7%

Line Branch Exec Source
1 // ======================================================================
2 // \title ComAggregator.cpp
3 // \author lestarch
4 // \brief cpp file for ComAggregator component implementation class
5 // ======================================================================
6
7 #include "Svc/ComAggregator/ComAggregator.hpp"
8 #include <cstring>
9 #include "Fw/FPrimeBasicTypes.hpp"
10 #include "Svc/Ccsds/Utils/IdlePacket.hpp"
11
12 namespace Svc {
13
14 // Definition for ODR-use of static constexpr member (required until C++17)
15 constexpr U16 ComAggregator::FHP_UNSET;
16 constexpr FwSizeType ComAggregator::MIN_NON_SPANNING_AGGREGATION_SIZE;
17
18 // ----------------------------------------------------------------------
19 // Component construction and destruction
20 // ----------------------------------------------------------------------
21
22 37 ComAggregator ::ComAggregator(const char* const compName)
23 : ComAggregatorComponentBase(compName),
24 37 m_allocator(nullptr),
25 37 m_allocationId(static_cast<FwEnumStoreType>(-1)),
26 37 m_allocation(nullptr),
27 37 m_bufferState(Fw::Buffer::OwnershipState::OWNED),
28
1/1
✓ Branch 5 taken 37 times.
37 m_frameBuffer(),
29
1/1
✓ Branch 5 taken 37 times.
37 m_frameSerializer(),
30 37 m_allow_timeout(false),
31 37 m_spanning(false),
32 37 m_aggregationSize(0),
33 37 m_heldOffset(0),
34 37 m_fhp(FHP_UNSET),
35 37 m_pendingIdleCount(0),
36 37 m_leadingIdleCount(0),
37
2/2
✓ Branch 10 taken 37 times.
✓ Branch 16 taken 37 times.
148 m_lastFrameLost(false) {}
38
39 74 ComAggregator ::~ComAggregator() {}
40
41 35 void ComAggregator ::configure(FwSizeType aggregationSize,
42 bool spanningEnabled,
43 FwEnumStoreType allocationId,
44 Fw::MemAllocator& allocator) {
45 // Storage is configured once, before any data is aggregated; cleanup() must run before reconfiguring
46 35 FW_ASSERT(this->m_allocation == nullptr);
47 // An aggregate must have room for data beyond the minimum idle packet
48 35 FW_ASSERT(aggregationSize > Ccsds::Utils::IdlePacket::MIN_SIZE, static_cast<FwAssertArgType>(aggregationSize));
49
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 26 times.
35 if (spanningEnabled) {
50 // Every packet header offset in an aggregate must be representable as an 11-bit First Header Pointer
51 // and distinct from the reserved values (CCSDS 132.0-B-3 4.1.2.7.6)
52 9 const FwSizeType fhpRange = static_cast<FwSizeType>(Ccsds::TMSubfields::FHP_IDLE_DATA_ONLY);
53 9 FW_ASSERT(aggregationSize <= fhpRange, static_cast<FwAssertArgType>(aggregationSize));
54 } else {
55 // Without spanning, a full com buffer or file buffer Space Packet must fit next to a minimum idle packet
56 26 FW_ASSERT(aggregationSize >= MIN_NON_SPANNING_AGGREGATION_SIZE, static_cast<FwAssertArgType>(aggregationSize));
57 // The residual idle packet, up to aggregationSize - 1 bytes, must have a representable SPP length field
58 26 FW_ASSERT((aggregationSize - 1) <= Ccsds::Utils::IdlePacket::MAX_SIZE,
59 static_cast<FwAssertArgType>(aggregationSize));
60 }
61 35 this->m_spanning = spanningEnabled;
62 35 this->m_aggregationSize = aggregationSize;
63
64 35 this->m_allocator = &allocator;
65 35 this->m_allocationId = allocationId;
66 35 FwSizeType allocatedSize = aggregationSize;
67
1/1
✓ Branch 6 taken 35 times.
35 this->m_allocation = allocator.checkedAllocate(allocationId, allocatedSize);
68
1/1
✓ Branch 10 taken 35 times.
35 this->m_frameBuffer.set(static_cast<U8*>(this->m_allocation), aggregationSize);
69
1/1
✓ Branch 10 taken 35 times.
35 this->m_frameSerializer.setExtBuffer(static_cast<U8*>(this->m_allocation), aggregationSize);
70 35 }
71
72 41 void ComAggregator ::cleanup() {
73
3/4
✓ Branch 4 taken 35 times.
✓ Branch 5 taken 6 times.
✓ Branch 10 taken 35 times.
✗ Branch 11 not taken.
41 if ((this->m_allocator != nullptr) && (this->m_allocation != nullptr)) {
74 // The aggregate must not be held downstream when its storage is released
75 35 FW_ASSERT(this->m_bufferState == Fw::Buffer::OwnershipState::OWNED,
76 static_cast<FwAssertArgType>(this->m_bufferState.load()));
77 // Drop the per-aggregate state so a later configure() starts clean
78
2/2
✓ Branch 2 taken 35 times.
✓ Branch 11 taken 35 times.
35 this->m_held = Svc::ComDataContextPair();
79 35 this->m_heldOffset = 0;
80 35 this->m_fhp = FHP_UNSET;
81 35 this->m_pendingIdleCount = 0;
82 35 this->m_leadingIdleCount = 0;
83 35 this->m_lastFrameLost = false;
84 35 this->m_frameSerializer.setExtBuffer(nullptr, 0);
85 35 this->m_frameBuffer.set(nullptr, 0);
86 35 this->m_aggregationSize = 0;
87 35 this->m_allocator->deallocate(this->m_allocationId, this->m_allocation);
88 35 this->m_allocation = nullptr;
89 35 this->m_allocator = nullptr;
90 }
91 41 }
92
93 25 void ComAggregator ::preamble() {
94
1/1
✓ Branch 2 taken 25 times.
25 Fw::Success good = Fw::Success::SUCCESS;
95
1/1
✓ Branch 5 taken 25 times.
25 this->comStatusOut_out(0, good);
96 50 }
97
98 // ----------------------------------------------------------------------
99 // Handler implementations for typed input ports
100 // ----------------------------------------------------------------------
101
102 10950 void ComAggregator ::comStatusIn_handler(FwIndexType portNum, Fw::Success& condition) {
103 10950 this->aggregationMachine_sendSignal_status(condition);
104 10950 }
105
106 5098 void ComAggregator ::dataIn_handler(FwIndexType portNum, Fw::Buffer& data, const ComCfg::FrameContext& context) {
107 5098 FW_ASSERT(this->m_allocation != nullptr);
108 // Without spanning, any packet must fit in an empty aggregate next to a minimum idle packet
109 5098 FW_ASSERT(this->m_spanning || data.getSize() <= (this->m_aggregationSize - Ccsds::Utils::IdlePacket::MIN_SIZE),
110 static_cast<FwAssertArgType>(data.getSize()));
111
1/1
✓ Branch 3 taken 5098 times.
5098 Svc::ComDataContextPair pair(data, context);
112
1/1
✓ Branch 5 taken 5098 times.
5098 this->aggregationMachine_sendSignal_fill(pair);
113 10196 }
114
115 5570 void ComAggregator ::dataReturnIn_handler(FwIndexType portNum, Fw::Buffer& data, const ComCfg::FrameContext& context) {
116 // This handler runs on the returning caller's thread: take ownership atomically
117 5570 const Fw::Buffer::OwnershipState previousState = this->m_bufferState.exchange(Fw::Buffer::OwnershipState::OWNED);
118 5570 FW_ASSERT(previousState == Fw::Buffer::OwnershipState::NOT_OWNED, static_cast<FwAssertArgType>(previousState));
119 5570 }
120
121 4178 void ComAggregator ::timeout_handler(FwIndexType portNum, U32 context) {
122 // Timeout is ignored in WAIT_STATUS state. However, the queue may not process timeout messages until the wait
123 // status is returned because the port chain may be synchronous and downstream components (radio, retry, etc) may
124 // take a long time to complete the transmission of data. This can cause the queue to overflow with messages that
125 // will soon be discarded.
126 //
127 // Therefore, to fix the risk of queue overflow we only queue timeout messages when they would be processed by the
128 // state machine (i.e. in the FILL state). Otherwise, these messages are not queued.
129 //
130 // Behaviorally, this solution will work exactly like the naive implementation with an infinite queue depth, but
131 // prevents queue overflow when using finite queues.
132
2/2
✓ Branch 4 taken 4151 times.
✓ Branch 5 taken 27 times.
4178 if (this->m_allow_timeout) {
133 4151 this->aggregationMachine_sendSignal_timeout();
134 }
135 4178 }
136
137 // ----------------------------------------------------------------------
138 // Implementations for internal state machine actions
139 // ----------------------------------------------------------------------
140
141 5591 void ComAggregator ::Svc_AggregationMachine_action_doClear(SmId smId, Svc_AggregationMachine::Signal signal) {
142 5591 this->m_allow_timeout = true; // Allow timeout messages in FILL state
143 5591 this->m_frameSerializer.resetSer();
144 5591 this->m_frameBuffer.setSize(this->m_aggregationSize);
145
2/2
✓ Branch 2 taken 5591 times.
✓ Branch 11 taken 5591 times.
5591 this->m_lastContext = ComCfg::FrameContext();
146 5591 this->m_fhp = FHP_UNSET;
147 5591 this->dropLostFrameState();
148 5591 this->m_leadingIdleCount = this->m_pendingIdleCount;
149 // Write out any idle packet bytes spanning over from the previous aggregate
150
2/2
✓ Branch 4 taken 40 times.
✓ Branch 5 taken 5551 times.
5591 if (this->m_pendingIdleCount > 0) {
151 80 Fw::SerializeStatus status = this->m_frameSerializer.serializeFrom(
152 80 this->m_pendingIdle, this->m_pendingIdleCount, Fw::Serialization::OMIT_LENGTH);
153 40 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
154 40 this->m_pendingIdleCount = 0;
155 }
156 // Fill from the held buffer (whole packet, or remainder of a spanned packet)
157 5591 this->fillFromHeld();
158 5591 }
159
160 1481 void ComAggregator ::Svc_AggregationMachine_action_doFill(SmId smId,
161 Svc_AggregationMachine::Signal signal,
162 const Svc::ComDataContextPair& value) {
163 1481 this->markFirstHeaderIfUnset();
164 2962 Fw::SerializeStatus status = this->m_frameSerializer.serializeFrom(
165 2962 value.get_data().getData(), value.get_data().getSize(), Fw::Serialization::OMIT_LENGTH);
166 1481 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
167 1481 this->m_lastContext = value.get_context();
168 1481 this->returnAndSignalReady(value);
169 1481 }
170
171 5570 void ComAggregator ::Svc_AggregationMachine_action_doSend(SmId smId, Svc_AggregationMachine::Signal signal) {
172 // Send only when the buffer will be valid
173
1/2
✓ Branch 6 taken 5570 times.
✗ Branch 7 not taken.
5570 if (this->m_frameSerializer.getSize() > 0) {
174 5570 this->fillResidualWithIdle();
175 5570 FW_ASSERT(this->m_frameSerializer.getSize() == this->m_aggregationSize,
176 static_cast<FwAssertArgType>(this->m_frameSerializer.getSize()));
177
3/4
✗ Branch 3 not taken.
✓ Branch 4 taken 5570 times.
✓ Branch 5 taken 5546 times.
✓ Branch 6 taken 24 times.
5570 if (this->m_spanning) {
178 11092 this->m_lastContext.set_firstHeaderPointer(
179
2/2
✓ Branch 4 taken 4772 times.
✓ Branch 5 taken 774 times.
5546 (this->m_fhp == FHP_UNSET) ? static_cast<U16>(Ccsds::TMSubfields::FHP_NO_PACKET_START) : this->m_fhp);
180 } else {
181 // Packets are never split: the first packet header is always at the start of the aggregate
182 24 this->m_lastContext.set_firstHeaderPointer(0);
183 }
184 const Fw::Buffer::OwnershipState previousState =
185 5570 this->m_bufferState.exchange(Fw::Buffer::OwnershipState::NOT_OWNED);
186 5570 FW_ASSERT(previousState == Fw::Buffer::OwnershipState::OWNED, static_cast<FwAssertArgType>(previousState));
187 5570 this->m_allow_timeout = false; // Timeout messages should be discarded in WAIT_STATUS state
188 5570 this->dataOut_out(0, this->m_frameBuffer, this->m_lastContext);
189 }
190 5570 }
191
192 3617 void ComAggregator ::Svc_AggregationMachine_action_doHold(SmId smId,
193 Svc_AggregationMachine::Signal signal,
194 const Svc::ComDataContextPair& value) {
195 3617 FW_ASSERT(not this->m_held.get_data().isValid());
196 3617 this->m_held = value;
197 3617 this->m_heldOffset = 0;
198 3617 }
199
200 2908 void ComAggregator ::Svc_AggregationMachine_action_doSplitHold(SmId smId,
201 Svc_AggregationMachine::Signal signal,
202 const Svc::ComDataContextPair& value) {
203 2908 this->Svc_AggregationMachine_action_doHold(smId, signal, value);
204
3/4
✗ Branch 3 not taken.
✓ Branch 4 taken 2908 times.
✓ Branch 5 taken 2898 times.
✓ Branch 6 taken 10 times.
2908 if (this->m_spanning) {
205 // Split the leading bytes of the held packet into the remaining aggregation space
206 2898 const FwSizeType remaining = this->remainingCapacity();
207
1/2
✓ Branch 0 taken 2898 times.
✗ Branch 1 not taken.
2898 if (remaining > 0) {
208 // The held packet's header starts at the current fill offset of this aggregate
209 2898 this->markFirstHeaderIfUnset();
210 2898 Fw::SerializeStatus status = this->m_frameSerializer.serializeFrom(value.get_data().getData(), remaining,
211 Fw::Serialization::OMIT_LENGTH);
212 2898 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
213 2898 this->m_heldOffset = remaining;
214 2898 this->m_lastContext = value.get_context();
215 }
216 }
217 2908 }
218
219 5359 void ComAggregator ::Svc_AggregationMachine_action_doNoteFailure(SmId smId, Svc_AggregationMachine::Signal signal) {
220 5359 this->m_lastFrameLost = true;
221 5359 }
222
223 ✗ void ComAggregator ::Svc_AggregationMachine_action_assertNoStatus(SmId smId, Svc_AggregationMachine::Signal signal) {
224 // Status is not possible in this state, confirm by assertion
225 ✗ FW_ASSERT(false);
226 ✗ }
227
228 // ----------------------------------------------------------------------
229 // Implementations for internal state machine guards
230 // ----------------------------------------------------------------------
231
232 4389 bool ComAggregator ::Svc_AggregationMachine_guard_isFull(SmId smId,
233 Svc_AggregationMachine::Signal signal,
234 const Svc::ComDataContextPair& value) const {
235 4389 return not this->accepts(value.get_data().getSize());
236 }
237
238 1481 bool ComAggregator ::Svc_AggregationMachine_guard_willFill(SmId smId,
239 Svc_AggregationMachine::Signal signal,
240 const Svc::ComDataContextPair& value) const {
241 1481 return (this->remainingCapacity() == value.get_data().getSize());
242 }
243
244 4151 bool ComAggregator ::Svc_AggregationMachine_guard_isNotEmpty(SmId smId, Svc_AggregationMachine::Signal signal) const {
245 // Carried-over idle bytes are not payload: an aggregate holding only those is empty for timeout purposes
246 4151 return this->m_frameSerializer.getSize() > this->m_leadingIdleCount;
247 }
248
249 10950 bool ComAggregator ::Svc_AggregationMachine_guard_isGood(SmId smId,
250 Svc_AggregationMachine::Signal signal,
251 const Fw::Success& value) const {
252 10950 return value == Fw::Success::SUCCESS;
253 }
254
255 5591 bool ComAggregator ::Svc_AggregationMachine_guard_isSpanFull(SmId smId, Svc_AggregationMachine::Signal signal) const {
256
5/6
✗ Branch 3 not taken.
✓ Branch 4 taken 5591 times.
✓ Branch 5 taken 5552 times.
✓ Branch 6 taken 39 times.
✓ Branch 22 taken 1194 times.
✓ Branch 23 taken 4358 times.
5591 return this->m_spanning && (this->m_frameSerializer.getSize() == this->m_aggregationSize);
257 }
258
259 // ----------------------------------------------------------------------
260 // Helper functions
261 // ----------------------------------------------------------------------
262
263 18287 FwSizeType ComAggregator ::remainingCapacity() const {
264 18287 FW_ASSERT(this->m_frameSerializer.getSize() <= this->m_aggregationSize,
265 static_cast<FwAssertArgType>(this->m_frameSerializer.getSize()));
266 18287 return this->m_aggregationSize - this->m_frameSerializer.getSize();
267 }
268
269 4389 bool ComAggregator ::accepts(FwSizeType size) const {
270 4389 const FwSizeType remaining = this->remainingCapacity();
271
2/2
✓ Branch 0 taken 2904 times.
✓ Branch 1 taken 1485 times.
4389 if (size > remaining) {
272 2904 return false;
273 }
274 // Without spanning, an idle packet cannot continue into the next aggregate: the packet must either complete
275 // the aggregate or leave room for a whole minimum idle packet
276 1485 const FwSizeType residual = remaining - size;
277
7/8
✗ Branch 3 not taken.
✓ Branch 4 taken 1485 times.
✓ Branch 5 taken 64 times.
✓ Branch 6 taken 1421 times.
✓ Branch 7 taken 61 times.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 57 times.
✓ Branch 10 taken 4 times.
1485 return this->m_spanning || (residual == 0) || (residual >= Ccsds::Utils::IdlePacket::MIN_SIZE);
278 }
279
280 6546 void ComAggregator ::markFirstHeaderIfUnset() {
281
5/6
✗ Branch 3 not taken.
✓ Branch 4 taken 6546 times.
✓ Branch 5 taken 6455 times.
✓ Branch 6 taken 91 times.
✓ Branch 11 taken 4772 times.
✓ Branch 12 taken 1683 times.
6546 if (this->m_spanning && this->m_fhp == FHP_UNSET) {
282 4772 this->m_fhp = static_cast<U16>(this->m_frameSerializer.getSize());
283 }
284 6546 }
285
286 5591 void ComAggregator ::fillFromHeld() {
287
2/2
✓ Branch 10 taken 2776 times.
✓ Branch 11 taken 2815 times.
5591 if (this->m_held.get_data().isValid()) {
288 2776 const Fw::Buffer& held = this->m_held.get_data();
289 2776 const FwSizeType heldRemaining = held.getSize() - this->m_heldOffset;
290
2/2
✓ Branch 4 taken 1173 times.
✓ Branch 5 taken 1603 times.
2776 const FwSizeType fillSize = FW_MIN(this->remainingCapacity(), heldRemaining);
291
2/2
✓ Branch 4 taken 718 times.
✓ Branch 5 taken 2058 times.
2776 if (this->m_heldOffset == 0) {
292 // The held packet's header starts at the current fill offset of this aggregate
293 718 this->markFirstHeaderIfUnset();
294 }
295 2776 Fw::SerializeStatus status = this->m_frameSerializer.serializeFrom(held.getData() + this->m_heldOffset,
296 fillSize, Fw::Serialization::OMIT_LENGTH);
297 2776 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
298 2776 this->m_lastContext = this->m_held.get_context();
299 2776 this->m_heldOffset += fillSize;
300
2/2
✓ Branch 8 taken 1603 times.
✓ Branch 9 taken 1173 times.
2776 if (this->m_heldOffset == held.getSize()) {
301 // Held buffer fully consumed: return it and request more data
302 1603 this->returnAndSignalReady(this->m_held);
303
2/2
✓ Branch 2 taken 1603 times.
✓ Branch 11 taken 1603 times.
1603 this->m_held = Svc::ComDataContextPair();
304 1603 this->m_heldOffset = 0;
305 }
306 }
307 5591 }
308
309 5570 void ComAggregator ::fillResidualWithIdle() {
310 5570 const FwSizeType residual = this->remainingCapacity();
311
2/2
✓ Branch 0 taken 4121 times.
✓ Branch 1 taken 1449 times.
5570 if (residual == 0) {
312 4121 return;
313 }
314 // The idle packet's header starts at the current fill offset of this aggregate
315 1449 this->markFirstHeaderIfUnset();
316 // Idle packet size: fill the residual space exactly, spanning a minimum-size idle packet
317 // into the next aggregate when the residual space is too small (CCSDS 132.0-B-3 4.1.4)
318 1449 Fw::SerializeStatus status = Fw::SerializeStatus::FW_SERIALIZE_OK;
319
2/2
✓ Branch 0 taken 1375 times.
✓ Branch 1 taken 74 times.
1449 if (residual >= Ccsds::Utils::IdlePacket::MIN_SIZE) {
320 // Idle packet fits entirely within this aggregate
321 1375 status = Ccsds::Utils::IdlePacket::serialize(this->m_frameSerializer, residual);
322 1375 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
323 } else {
324 // Without spanning, accepts() keeps the residual at zero or at least a minimum idle packet
325 74 FW_ASSERT(this->m_spanning);
326 // Stage a minimum-size idle packet, emit the leading bytes now and span the rest
327 74 U8 staging[Ccsds::Utils::IdlePacket::MIN_SIZE] = {};
328
1/1
✓ Branch 2 taken 74 times.
74 Fw::ExternalSerializeBuffer stager(staging, sizeof(staging));
329
1/1
✓ Branch 1 taken 74 times.
74 status = Ccsds::Utils::IdlePacket::serialize(stager, Ccsds::Utils::IdlePacket::MIN_SIZE);
330 74 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
331
1/1
✓ Branch 6 taken 74 times.
74 status = this->m_frameSerializer.serializeFrom(staging, residual, Fw::Serialization::OMIT_LENGTH);
332 74 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
333 74 this->m_pendingIdleCount = Ccsds::Utils::IdlePacket::MIN_SIZE - residual;
334
2/4
✗ Branch 8 not taken.
✓ Branch 9 taken 74 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 74 times.
74 (void)memcpy(this->m_pendingIdle, &staging[residual], this->m_pendingIdleCount);
335 74 }
336 }
337
338 5591 void ComAggregator ::dropLostFrameState() {
339
3/4
✗ Branch 3 not taken.
✓ Branch 4 taken 5591 times.
✓ Branch 5 taken 2745 times.
✓ Branch 6 taken 2846 times.
5591 if (this->m_lastFrameLost) {
340 2745 this->m_pendingIdleCount = 0;
341
6/6
✓ Branch 10 taken 2479 times.
✓ Branch 11 taken 266 times.
✓ Branch 16 taken 2012 times.
✓ Branch 17 taken 467 times.
✓ Branch 18 taken 2012 times.
✓ Branch 19 taken 733 times.
2745 if (this->m_held.get_data().isValid() && this->m_heldOffset > 0) {
342 2012 this->returnAndSignalReady(this->m_held);
343
2/2
✓ Branch 2 taken 2012 times.
✓ Branch 11 taken 2012 times.
2012 this->m_held = Svc::ComDataContextPair();
344 2012 this->m_heldOffset = 0;
345 }
346 2745 this->m_lastFrameLost = false;
347 }
348 5591 }
349
350 5096 void ComAggregator ::returnAndSignalReady(const Svc::ComDataContextPair& pair) {
351 // Return port does not alter data and thus const-cast is safe
352
1/1
✓ Branch 14 taken 5096 times.
5096 this->dataReturnOut_out(0, const_cast<Fw::Buffer&>(pair.get_data()), pair.get_context());
353
1/1
✓ Branch 2 taken 5096 times.
5096 Fw::Success good = Fw::Success::SUCCESS;
354
1/1
✓ Branch 5 taken 5096 times.
5096 this->comStatusOut_out(0, good);
355 10192 }
356
357 } // namespace Svc
358