GCC Code Coverage Report


Directory: ./
File: Svc/ComAggregator/ComAggregator.cpp
Date: 2026-10-14 21:15:02
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 10639 void ComAggregator ::comStatusIn_handler(FwIndexType portNum, Fw::Success& condition) {
103 10639 this->aggregationMachine_sendSignal_status(condition);
104 10639 }
105
106 5198 void ComAggregator ::dataIn_handler(FwIndexType portNum, Fw::Buffer& data, const ComCfg::FrameContext& context) {
107 5198 FW_ASSERT(this->m_allocation != nullptr);
108 // Without spanning, any packet must fit in an empty aggregate next to a minimum idle packet
109 5198 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 5198 times.
5198 Svc::ComDataContextPair pair(data, context);
112
1/1
✓ Branch 5 taken 5198 times.
5198 this->aggregationMachine_sendSignal_fill(pair);
113 10396 }
114
115 5381 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 5381 const Fw::Buffer::OwnershipState previousState = this->m_bufferState.exchange(Fw::Buffer::OwnershipState::OWNED);
118 5381 FW_ASSERT(previousState == Fw::Buffer::OwnershipState::NOT_OWNED, static_cast<FwAssertArgType>(previousState));
119 5381 }
120
121 4396 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 4368 times.
✓ Branch 5 taken 28 times.
4396 if (this->m_allow_timeout) {
133 4368 this->aggregationMachine_sendSignal_timeout();
134 }
135 4396 }
136
137 // ----------------------------------------------------------------------
138 // Implementations for internal state machine actions
139 // ----------------------------------------------------------------------
140
141 5403 void ComAggregator ::Svc_AggregationMachine_action_doClear(SmId smId, Svc_AggregationMachine::Signal signal) {
142 5403 this->m_allow_timeout = true; // Allow timeout messages in FILL state
143 5403 this->m_frameSerializer.resetSer();
144 5403 this->m_frameBuffer.setSize(this->m_aggregationSize);
145
2/2
✓ Branch 2 taken 5403 times.
✓ Branch 11 taken 5403 times.
5403 this->m_lastContext = ComCfg::FrameContext();
146 5403 this->m_fhp = FHP_UNSET;
147 5403 this->dropLostFrameState();
148 5403 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 30 times.
✓ Branch 5 taken 5373 times.
5403 if (this->m_pendingIdleCount > 0) {
151 60 Fw::SerializeStatus status = this->m_frameSerializer.serializeFrom(
152 60 this->m_pendingIdle, this->m_pendingIdleCount, Fw::Serialization::OMIT_LENGTH);
153 30 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
154 30 this->m_pendingIdleCount = 0;
155 }
156 // Fill from the held buffer (whole packet, or remainder of a spanned packet)
157 5403 this->fillFromHeld();
158 5403 }
159
160 1607 void ComAggregator ::Svc_AggregationMachine_action_doFill(SmId smId,
161 Svc_AggregationMachine::Signal signal,
162 const Svc::ComDataContextPair& value) {
163 1607 this->markFirstHeaderIfUnset();
164 3214 Fw::SerializeStatus status = this->m_frameSerializer.serializeFrom(
165 3214 value.get_data().getData(), value.get_data().getSize(), Fw::Serialization::OMIT_LENGTH);
166 1607 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
167 1607 this->m_lastContext = value.get_context();
168 1607 this->returnAndSignalReady(value);
169 1607 }
170
171 5381 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 5381 times.
✗ Branch 7 not taken.
5381 if (this->m_frameSerializer.getSize() > 0) {
174 5381 this->fillResidualWithIdle();
175 5381 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 5381 times.
✓ Branch 5 taken 5357 times.
✓ Branch 6 taken 24 times.
5381 if (this->m_spanning) {
178 10714 this->m_lastContext.set_firstHeaderPointer(
179
2/2
✓ Branch 4 taken 4816 times.
✓ Branch 5 taken 541 times.
5357 (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 5381 this->m_bufferState.exchange(Fw::Buffer::OwnershipState::NOT_OWNED);
186 5381 FW_ASSERT(previousState == Fw::Buffer::OwnershipState::OWNED, static_cast<FwAssertArgType>(previousState));
187 5381 this->m_allow_timeout = false; // Timeout messages should be discarded in WAIT_STATUS state
188 5381 this->dataOut_out(0, this->m_frameBuffer, this->m_lastContext);
189 }
190 5381 }
191
192 3591 void ComAggregator ::Svc_AggregationMachine_action_doHold(SmId smId,
193 Svc_AggregationMachine::Signal signal,
194 const Svc::ComDataContextPair& value) {
195 3591 FW_ASSERT(not this->m_held.get_data().isValid());
196 3591 this->m_held = value;
197 3591 this->m_heldOffset = 0;
198 3591 }
199
200 2834 void ComAggregator ::Svc_AggregationMachine_action_doSplitHold(SmId smId,
201 Svc_AggregationMachine::Signal signal,
202 const Svc::ComDataContextPair& value) {
203 2834 this->Svc_AggregationMachine_action_doHold(smId, signal, value);
204
3/4
✗ Branch 3 not taken.
✓ Branch 4 taken 2834 times.
✓ Branch 5 taken 2824 times.
✓ Branch 6 taken 10 times.
2834 if (this->m_spanning) {
205 // Split the leading bytes of the held packet into the remaining aggregation space
206 2824 const FwSizeType remaining = this->remainingCapacity();
207
1/2
✓ Branch 0 taken 2824 times.
✗ Branch 1 not taken.
2824 if (remaining > 0) {
208 // The held packet's header starts at the current fill offset of this aggregate
209 2824 this->markFirstHeaderIfUnset();
210 2824 Fw::SerializeStatus status = this->m_frameSerializer.serializeFrom(value.get_data().getData(), remaining,
211 Fw::Serialization::OMIT_LENGTH);
212 2824 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
213 2824 this->m_heldOffset = remaining;
214 2824 this->m_lastContext = value.get_context();
215 }
216 }
217 2834 }
218
219 5236 void ComAggregator ::Svc_AggregationMachine_action_doNoteFailure(SmId smId, Svc_AggregationMachine::Signal signal) {
220 5236 this->m_lastFrameLost = true;
221 5236 }
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 4441 bool ComAggregator ::Svc_AggregationMachine_guard_isFull(SmId smId,
233 Svc_AggregationMachine::Signal signal,
234 const Svc::ComDataContextPair& value) const {
235 4441 return not this->accepts(value.get_data().getSize());
236 }
237
238 1607 bool ComAggregator ::Svc_AggregationMachine_guard_willFill(SmId smId,
239 Svc_AggregationMachine::Signal signal,
240 const Svc::ComDataContextPair& value) const {
241 1607 return (this->remainingCapacity() == value.get_data().getSize());
242 }
243
244 4368 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 4368 return this->m_frameSerializer.getSize() > this->m_leadingIdleCount;
247 }
248
249 10639 bool ComAggregator ::Svc_AggregationMachine_guard_isGood(SmId smId,
250 Svc_AggregationMachine::Signal signal,
251 const Fw::Success& value) const {
252 10639 return value == Fw::Success::SUCCESS;
253 }
254
255 5403 bool ComAggregator ::Svc_AggregationMachine_guard_isSpanFull(SmId smId, Svc_AggregationMachine::Signal signal) const {
256
5/6
✗ Branch 3 not taken.
✓ Branch 4 taken 5403 times.
✓ Branch 5 taken 5364 times.
✓ Branch 6 taken 39 times.
✓ Branch 22 taken 957 times.
✓ Branch 23 taken 4407 times.
5403 return this->m_spanning && (this->m_frameSerializer.getSize() == this->m_aggregationSize);
257 }
258
259 // ----------------------------------------------------------------------
260 // Helper functions
261 // ----------------------------------------------------------------------
262
263 17859 FwSizeType ComAggregator ::remainingCapacity() const {
264 17859 FW_ASSERT(this->m_frameSerializer.getSize() <= this->m_aggregationSize,
265 static_cast<FwAssertArgType>(this->m_frameSerializer.getSize()));
266 17859 return this->m_aggregationSize - this->m_frameSerializer.getSize();
267 }
268
269 4441 bool ComAggregator ::accepts(FwSizeType size) const {
270 4441 const FwSizeType remaining = this->remainingCapacity();
271
2/2
✓ Branch 0 taken 2830 times.
✓ Branch 1 taken 1611 times.
4441 if (size > remaining) {
272 2830 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 1611 const FwSizeType residual = remaining - size;
277
7/8
✗ Branch 3 not taken.
✓ Branch 4 taken 1611 times.
✓ Branch 5 taken 68 times.
✓ Branch 6 taken 1543 times.
✓ Branch 7 taken 65 times.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 61 times.
✓ Branch 10 taken 4 times.
1611 return this->m_spanning || (residual == 0) || (residual >= Ccsds::Utils::IdlePacket::MIN_SIZE);
278 }
279
280 6785 void ComAggregator ::markFirstHeaderIfUnset() {
281
5/6
✗ Branch 3 not taken.
✓ Branch 4 taken 6785 times.
✓ Branch 5 taken 6690 times.
✓ Branch 6 taken 95 times.
✓ Branch 11 taken 4817 times.
✓ Branch 12 taken 1873 times.
6785 if (this->m_spanning && this->m_fhp == FHP_UNSET) {
282 4817 this->m_fhp = static_cast<U16>(this->m_frameSerializer.getSize());
283 }
284 6785 }
285
286 5403 void ComAggregator ::fillFromHeld() {
287
2/2
✓ Branch 10 taken 2662 times.
✓ Branch 11 taken 2741 times.
5403 if (this->m_held.get_data().isValid()) {
288 2662 const Fw::Buffer& held = this->m_held.get_data();
289 2662 const FwSizeType heldRemaining = held.getSize() - this->m_heldOffset;
290
2/2
✓ Branch 4 taken 944 times.
✓ Branch 5 taken 1718 times.
2662 const FwSizeType fillSize = FW_MIN(this->remainingCapacity(), heldRemaining);
291
2/2
✓ Branch 4 taken 766 times.
✓ Branch 5 taken 1896 times.
2662 if (this->m_heldOffset == 0) {
292 // The held packet's header starts at the current fill offset of this aggregate
293 766 this->markFirstHeaderIfUnset();
294 }
295 2662 Fw::SerializeStatus status = this->m_frameSerializer.serializeFrom(held.getData() + this->m_heldOffset,
296 fillSize, Fw::Serialization::OMIT_LENGTH);
297 2662 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
298 2662 this->m_lastContext = this->m_held.get_context();
299 2662 this->m_heldOffset += fillSize;
300
2/2
✓ Branch 8 taken 1718 times.
✓ Branch 9 taken 944 times.
2662 if (this->m_heldOffset == held.getSize()) {
301 // Held buffer fully consumed: return it and request more data
302 1718 this->returnAndSignalReady(this->m_held);
303
2/2
✓ Branch 2 taken 1718 times.
✓ Branch 11 taken 1718 times.
1718 this->m_held = Svc::ComDataContextPair();
304 1718 this->m_heldOffset = 0;
305 }
306 }
307 5403 }
308
309 5381 void ComAggregator ::fillResidualWithIdle() {
310 5381 const FwSizeType residual = this->remainingCapacity();
311
2/2
✓ Branch 0 taken 3793 times.
✓ Branch 1 taken 1588 times.
5381 if (residual == 0) {
312 3793 return;
313 }
314 // The idle packet's header starts at the current fill offset of this aggregate
315 1588 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 1588 Fw::SerializeStatus status = Fw::SerializeStatus::FW_SERIALIZE_OK;
319
2/2
✓ Branch 0 taken 1534 times.
✓ Branch 1 taken 54 times.
1588 if (residual >= Ccsds::Utils::IdlePacket::MIN_SIZE) {
320 // Idle packet fits entirely within this aggregate
321 1534 status = Ccsds::Utils::IdlePacket::serialize(this->m_frameSerializer, residual);
322 1534 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 54 FW_ASSERT(this->m_spanning);
326 // Stage a minimum-size idle packet, emit the leading bytes now and span the rest
327 54 U8 staging[Ccsds::Utils::IdlePacket::MIN_SIZE] = {};
328
1/1
✓ Branch 2 taken 54 times.
54 Fw::ExternalSerializeBuffer stager(staging, sizeof(staging));
329
1/1
✓ Branch 1 taken 54 times.
54 status = Ccsds::Utils::IdlePacket::serialize(stager, Ccsds::Utils::IdlePacket::MIN_SIZE);
330 54 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
331
1/1
✓ Branch 6 taken 54 times.
54 status = this->m_frameSerializer.serializeFrom(staging, residual, Fw::Serialization::OMIT_LENGTH);
332 54 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
333 54 this->m_pendingIdleCount = Ccsds::Utils::IdlePacket::MIN_SIZE - residual;
334
2/4
✗ Branch 8 not taken.
✓ Branch 9 taken 54 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 54 times.
54 (void)memcpy(this->m_pendingIdle, &staging[residual], this->m_pendingIdleCount);
335 54 }
336 }
337
338 5403 void ComAggregator ::dropLostFrameState() {
339
3/4
✗ Branch 3 not taken.
✓ Branch 4 taken 5403 times.
✓ Branch 5 taken 2672 times.
✓ Branch 6 taken 2731 times.
5403 if (this->m_lastFrameLost) {
340 2672 this->m_pendingIdleCount = 0;
341
6/6
✓ Branch 10 taken 2386 times.
✓ Branch 11 taken 286 times.
✓ Branch 16 taken 1872 times.
✓ Branch 17 taken 514 times.
✓ Branch 18 taken 1872 times.
✓ Branch 19 taken 800 times.
2672 if (this->m_held.get_data().isValid() && this->m_heldOffset > 0) {
342 1872 this->returnAndSignalReady(this->m_held);
343
2/2
✓ Branch 2 taken 1872 times.
✓ Branch 11 taken 1872 times.
1872 this->m_held = Svc::ComDataContextPair();
344 1872 this->m_heldOffset = 0;
345 }
346 2672 this->m_lastFrameLost = false;
347 }
348 5403 }
349
350 5197 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 5197 times.
5197 this->dataReturnOut_out(0, const_cast<Fw::Buffer&>(pair.get_data()), pair.get_context());
353
1/1
✓ Branch 2 taken 5197 times.
5197 Fw::Success good = Fw::Success::SUCCESS;
354
1/1
✓ Branch 5 taken 5197 times.
5197 this->comStatusOut_out(0, good);
355 10394 }
356
357 } // namespace Svc
358