GCC Code Coverage Report


Directory: ./
File: Svc/ComAggregator/ComAggregator.cpp
Date: 2026-10-14 22:11:56
Exec Total Coverage
Lines: 161 200 80.5%
Functions: 26 28 92.9%
Branches: 44 81 54.3%

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 1 ComAggregator ::ComAggregator(const char* const compName)
23 : ComAggregatorComponentBase(compName),
24 1 m_allocator(nullptr),
25 1 m_allocationId(static_cast<FwEnumStoreType>(-1)),
26 1 m_allocation(nullptr),
27 1 m_bufferState(Fw::Buffer::OwnershipState::OWNED),
28
1/1
✓ Branch 1 taken 1 times.
1 m_frameBuffer(),
29
1/1
✓ Branch 1 taken 1 times.
1 m_frameSerializer(),
30 1 m_allow_timeout(false),
31 1 m_spanning(false),
32 1 m_aggregationSize(0),
33 1 m_heldOffset(0),
34 1 m_fhp(FHP_UNSET),
35 1 m_pendingIdleCount(0),
36 1 m_leadingIdleCount(0),
37
2/2
✓ Branch 3 taken 1 times.
✓ Branch 6 taken 1 times.
3 m_lastFrameLost(false) {}
38
39 2 ComAggregator ::~ComAggregator() {}
40
41 1 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 1 FW_ASSERT(this->m_allocation == nullptr);
47 // An aggregate must have room for data beyond the minimum idle packet
48 1 FW_ASSERT(aggregationSize > Ccsds::Utils::IdlePacket::MIN_SIZE, static_cast<FwAssertArgType>(aggregationSize));
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 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 ✗ const FwSizeType fhpRange = static_cast<FwSizeType>(Ccsds::TMSubfields::FHP_IDLE_DATA_ONLY);
53 ✗ 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 1 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 1 FW_ASSERT((aggregationSize - 1) <= Ccsds::Utils::IdlePacket::MAX_SIZE,
59 static_cast<FwAssertArgType>(aggregationSize));
60 }
61 1 this->m_spanning = spanningEnabled;
62 1 this->m_aggregationSize = aggregationSize;
63
64 1 this->m_allocator = &allocator;
65 1 this->m_allocationId = allocationId;
66 1 FwSizeType allocatedSize = aggregationSize;
67
1/1
✓ Branch 1 taken 1 times.
1 this->m_allocation = allocator.checkedAllocate(allocationId, allocatedSize);
68
1/1
✓ Branch 1 taken 1 times.
1 this->m_frameBuffer.set(static_cast<U8*>(this->m_allocation), aggregationSize);
69
1/1
✓ Branch 1 taken 1 times.
1 this->m_frameSerializer.setExtBuffer(static_cast<U8*>(this->m_allocation), aggregationSize);
70 1 }
71
72 1 void ComAggregator ::cleanup() {
73
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if ((this->m_allocator != nullptr) && (this->m_allocation != nullptr)) {
74 // The aggregate must not be held downstream when its storage is released
75 1 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 1 taken 1 times.
✓ Branch 4 taken 1 times.
1 this->m_held = Svc::ComDataContextPair();
79 1 this->m_heldOffset = 0;
80 1 this->m_fhp = FHP_UNSET;
81 1 this->m_pendingIdleCount = 0;
82 1 this->m_leadingIdleCount = 0;
83 1 this->m_lastFrameLost = false;
84 1 this->m_frameSerializer.setExtBuffer(nullptr, 0);
85 1 this->m_frameBuffer.set(nullptr, 0);
86 1 this->m_aggregationSize = 0;
87 1 this->m_allocator->deallocate(this->m_allocationId, this->m_allocation);
88 1 this->m_allocation = nullptr;
89 1 this->m_allocator = nullptr;
90 }
91 1 }
92
93 1 void ComAggregator ::preamble() {
94
1/1
✓ Branch 1 taken 1 times.
1 Fw::Success good = Fw::Success::SUCCESS;
95
1/1
✓ Branch 1 taken 1 times.
1 this->comStatusOut_out(0, good);
96 1 }
97
98 // ----------------------------------------------------------------------
99 // Handler implementations for typed input ports
100 // ----------------------------------------------------------------------
101
102 232 void ComAggregator ::comStatusIn_handler(FwIndexType portNum, Fw::Success& condition) {
103 232 this->aggregationMachine_sendSignal_status(condition);
104 232 }
105
106 705 void ComAggregator ::dataIn_handler(FwIndexType portNum, Fw::Buffer& data, const ComCfg::FrameContext& context) {
107 705 FW_ASSERT(this->m_allocation != nullptr);
108 // Without spanning, any packet must fit in an empty aggregate next to a minimum idle packet
109 705 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 1 taken 705 times.
705 Svc::ComDataContextPair pair(data, context);
112
1/1
✓ Branch 1 taken 705 times.
705 this->aggregationMachine_sendSignal_fill(pair);
113 705 }
114
115 231 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 231 const Fw::Buffer::OwnershipState previousState = this->m_bufferState.exchange(Fw::Buffer::OwnershipState::OWNED);
118 231 FW_ASSERT(previousState == Fw::Buffer::OwnershipState::NOT_OWNED, static_cast<FwAssertArgType>(previousState));
119 231 }
120
121 246 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 1 taken 245 times.
✓ Branch 2 taken 1 times.
246 if (this->m_allow_timeout) {
133 245 this->aggregationMachine_sendSignal_timeout();
134 }
135 246 }
136
137 // ----------------------------------------------------------------------
138 // Implementations for internal state machine actions
139 // ----------------------------------------------------------------------
140
141 232 void ComAggregator ::Svc_AggregationMachine_action_doClear(SmId smId, Svc_AggregationMachine::Signal signal) {
142 232 this->m_allow_timeout = true; // Allow timeout messages in FILL state
143 232 this->m_frameSerializer.resetSer();
144 232 this->m_frameBuffer.setSize(this->m_aggregationSize);
145
2/2
✓ Branch 1 taken 232 times.
✓ Branch 4 taken 232 times.
232 this->m_lastContext = ComCfg::FrameContext();
146 232 this->m_fhp = FHP_UNSET;
147 232 this->dropLostFrameState();
148 232 this->m_leadingIdleCount = this->m_pendingIdleCount;
149 // Write out any idle packet bytes spanning over from the previous aggregate
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 232 times.
232 if (this->m_pendingIdleCount > 0) {
151 ✗ Fw::SerializeStatus status = this->m_frameSerializer.serializeFrom(
152 ✗ this->m_pendingIdle, this->m_pendingIdleCount, Fw::Serialization::OMIT_LENGTH);
153 ✗ FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
154 ✗ this->m_pendingIdleCount = 0;
155 }
156 // Fill from the held buffer (whole packet, or remainder of a spanned packet)
157 232 this->fillFromHeld();
158 232 }
159
160 679 void ComAggregator ::Svc_AggregationMachine_action_doFill(SmId smId,
161 Svc_AggregationMachine::Signal signal,
162 const Svc::ComDataContextPair& value) {
163 679 this->markFirstHeaderIfUnset();
164 679 Fw::SerializeStatus status = this->m_frameSerializer.serializeFrom(
165 1358 value.get_data().getData(), value.get_data().getSize(), Fw::Serialization::OMIT_LENGTH);
166 679 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
167 679 this->m_lastContext = value.get_context();
168 679 this->returnAndSignalReady(value);
169 679 }
170
171 231 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 1 taken 231 times.
✗ Branch 2 not taken.
231 if (this->m_frameSerializer.getSize() > 0) {
174 231 this->fillResidualWithIdle();
175 231 FW_ASSERT(this->m_frameSerializer.getSize() == this->m_aggregationSize,
176 static_cast<FwAssertArgType>(this->m_frameSerializer.getSize()));
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 231 times.
231 if (this->m_spanning) {
178 ✗ this->m_lastContext.set_firstHeaderPointer(
179 ✗ (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 231 this->m_lastContext.set_firstHeaderPointer(0);
183 }
184 const Fw::Buffer::OwnershipState previousState =
185 231 this->m_bufferState.exchange(Fw::Buffer::OwnershipState::NOT_OWNED);
186 231 FW_ASSERT(previousState == Fw::Buffer::OwnershipState::OWNED, static_cast<FwAssertArgType>(previousState));
187 231 this->m_allow_timeout = false; // Timeout messages should be discarded in WAIT_STATUS state
188 231 this->dataOut_out(0, this->m_frameBuffer, this->m_lastContext);
189 }
190 231 }
191
192 26 void ComAggregator ::Svc_AggregationMachine_action_doHold(SmId smId,
193 Svc_AggregationMachine::Signal signal,
194 const Svc::ComDataContextPair& value) {
195 26 FW_ASSERT(not this->m_held.get_data().isValid());
196 26 this->m_held = value;
197 26 this->m_heldOffset = 0;
198 26 }
199
200 1 void ComAggregator ::Svc_AggregationMachine_action_doSplitHold(SmId smId,
201 Svc_AggregationMachine::Signal signal,
202 const Svc::ComDataContextPair& value) {
203 1 this->Svc_AggregationMachine_action_doHold(smId, signal, value);
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (this->m_spanning) {
205 // Split the leading bytes of the held packet into the remaining aggregation space
206 ✗ const FwSizeType remaining = this->remainingCapacity();
207 ✗ if (remaining > 0) {
208 // The held packet's header starts at the current fill offset of this aggregate
209 ✗ this->markFirstHeaderIfUnset();
210 ✗ Fw::SerializeStatus status = this->m_frameSerializer.serializeFrom(value.get_data().getData(), remaining,
211 Fw::Serialization::OMIT_LENGTH);
212 ✗ FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
213 ✗ this->m_heldOffset = remaining;
214 ✗ this->m_lastContext = value.get_context();
215 }
216 }
217 1 }
218
219 ✗ void ComAggregator ::Svc_AggregationMachine_action_doNoteFailure(SmId smId, Svc_AggregationMachine::Signal signal) {
220 ✗ this->m_lastFrameLost = true;
221 ✗ }
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 680 bool ComAggregator ::Svc_AggregationMachine_guard_isFull(SmId smId,
233 Svc_AggregationMachine::Signal signal,
234 const Svc::ComDataContextPair& value) const {
235 680 return not this->accepts(value.get_data().getSize());
236 }
237
238 679 bool ComAggregator ::Svc_AggregationMachine_guard_willFill(SmId smId,
239 Svc_AggregationMachine::Signal signal,
240 const Svc::ComDataContextPair& value) const {
241 679 return (this->remainingCapacity() == value.get_data().getSize());
242 }
243
244 245 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 245 return this->m_frameSerializer.getSize() > this->m_leadingIdleCount;
247 }
248
249 232 bool ComAggregator ::Svc_AggregationMachine_guard_isGood(SmId smId,
250 Svc_AggregationMachine::Signal signal,
251 const Fw::Success& value) const {
252 232 return value == Fw::Success::SUCCESS;
253 }
254
255 232 bool ComAggregator ::Svc_AggregationMachine_guard_isSpanFull(SmId smId, Svc_AggregationMachine::Signal signal) const {
256
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 232 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
232 return this->m_spanning && (this->m_frameSerializer.getSize() == this->m_aggregationSize);
257 }
258
259 // ----------------------------------------------------------------------
260 // Helper functions
261 // ----------------------------------------------------------------------
262
263 1616 FwSizeType ComAggregator ::remainingCapacity() const {
264 1616 FW_ASSERT(this->m_frameSerializer.getSize() <= this->m_aggregationSize,
265 static_cast<FwAssertArgType>(this->m_frameSerializer.getSize()));
266 1616 return this->m_aggregationSize - this->m_frameSerializer.getSize();
267 }
268
269 680 bool ComAggregator ::accepts(FwSizeType size) const {
270 680 const FwSizeType remaining = this->remainingCapacity();
271
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 679 times.
680 if (size > remaining) {
272 1 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 679 const FwSizeType residual = remaining - size;
277
3/6
✓ Branch 0 taken 679 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 679 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 679 times.
✗ Branch 5 not taken.
679 return this->m_spanning || (residual == 0) || (residual >= Ccsds::Utils::IdlePacket::MIN_SIZE);
278 }
279
280 936 void ComAggregator ::markFirstHeaderIfUnset() {
281
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 936 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
936 if (this->m_spanning && this->m_fhp == FHP_UNSET) {
282 ✗ this->m_fhp = static_cast<U16>(this->m_frameSerializer.getSize());
283 }
284 936 }
285
286 232 void ComAggregator ::fillFromHeld() {
287
2/2
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 206 times.
232 if (this->m_held.get_data().isValid()) {
288 26 const Fw::Buffer& held = this->m_held.get_data();
289 26 const FwSizeType heldRemaining = held.getSize() - this->m_heldOffset;
290
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
26 const FwSizeType fillSize = FW_MIN(this->remainingCapacity(), heldRemaining);
291
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 if (this->m_heldOffset == 0) {
292 // The held packet's header starts at the current fill offset of this aggregate
293 26 this->markFirstHeaderIfUnset();
294 }
295 26 Fw::SerializeStatus status = this->m_frameSerializer.serializeFrom(held.getData() + this->m_heldOffset,
296 fillSize, Fw::Serialization::OMIT_LENGTH);
297 26 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
298 26 this->m_lastContext = this->m_held.get_context();
299 26 this->m_heldOffset += fillSize;
300
1/2
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
26 if (this->m_heldOffset == held.getSize()) {
301 // Held buffer fully consumed: return it and request more data
302 26 this->returnAndSignalReady(this->m_held);
303
2/2
✓ Branch 1 taken 26 times.
✓ Branch 4 taken 26 times.
26 this->m_held = Svc::ComDataContextPair();
304 26 this->m_heldOffset = 0;
305 }
306 }
307 232 }
308
309 231 void ComAggregator ::fillResidualWithIdle() {
310 231 const FwSizeType residual = this->remainingCapacity();
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 231 times.
231 if (residual == 0) {
312 ✗ return;
313 }
314 // The idle packet's header starts at the current fill offset of this aggregate
315 231 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 231 Fw::SerializeStatus status = Fw::SerializeStatus::FW_SERIALIZE_OK;
319
1/2
✓ Branch 0 taken 231 times.
✗ Branch 1 not taken.
231 if (residual >= Ccsds::Utils::IdlePacket::MIN_SIZE) {
320 // Idle packet fits entirely within this aggregate
321 231 status = Ccsds::Utils::IdlePacket::serialize(this->m_frameSerializer, residual);
322 231 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 ✗ FW_ASSERT(this->m_spanning);
326 // Stage a minimum-size idle packet, emit the leading bytes now and span the rest
327 ✗ U8 staging[Ccsds::Utils::IdlePacket::MIN_SIZE] = {};
328 ✗ Fw::ExternalSerializeBuffer stager(staging, sizeof(staging));
329 ✗ status = Ccsds::Utils::IdlePacket::serialize(stager, Ccsds::Utils::IdlePacket::MIN_SIZE);
330 ✗ FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
331 ✗ status = this->m_frameSerializer.serializeFrom(staging, residual, Fw::Serialization::OMIT_LENGTH);
332 ✗ FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
333 ✗ this->m_pendingIdleCount = Ccsds::Utils::IdlePacket::MIN_SIZE - residual;
334 ✗ (void)memcpy(this->m_pendingIdle, &staging[residual], this->m_pendingIdleCount);
335 ✗ }
336 }
337
338 232 void ComAggregator ::dropLostFrameState() {
339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 232 times.
232 if (this->m_lastFrameLost) {
340 ✗ this->m_pendingIdleCount = 0;
341 ✗ if (this->m_held.get_data().isValid() && this->m_heldOffset > 0) {
342 ✗ this->returnAndSignalReady(this->m_held);
343 ✗ this->m_held = Svc::ComDataContextPair();
344 ✗ this->m_heldOffset = 0;
345 }
346 ✗ this->m_lastFrameLost = false;
347 }
348 232 }
349
350 705 void ComAggregator ::returnAndSignalReady(const Svc::ComDataContextPair& pair) {
351 // Return port does not alter data and thus const-cast is safe
352
1/1
✓ Branch 3 taken 705 times.
705 this->dataReturnOut_out(0, const_cast<Fw::Buffer&>(pair.get_data()), pair.get_context());
353
1/1
✓ Branch 1 taken 705 times.
705 Fw::Success good = Fw::Success::SUCCESS;
354
1/1
✓ Branch 1 taken 705 times.
705 this->comStatusOut_out(0, good);
355 705 }
356
357 } // namespace Svc
358