GCC Code Coverage Report


Directory: Svc/ComAggregator/
File: ComAggregator.cpp
Date: 2026-09-23 21:13:32
Exec Total Coverage
Lines: 161 164 98.2%
Functions: 25 26 96.2%
Branches: 69 80 86.2%

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::NON_SPANNING_CAPACITY;
17
18 // ----------------------------------------------------------------------
19 // Component construction and destruction
20 // ----------------------------------------------------------------------
21
22 18 ComAggregator ::ComAggregator(const char* const compName)
23 : ComAggregatorComponentBase(compName),
24 18 m_bufferState(Fw::Buffer::OwnershipState::OWNED),
25
1/1
✓ Branch 6 taken 18 times.
18 m_frameBuffer(m_frameBufferStore, sizeof(m_frameBufferStore)),
26
1/1
✓ Branch 7 taken 18 times.
18 m_frameSerializer(m_frameBuffer.getSerializer()),
27 18 m_allow_timeout(false),
28 18 m_spanning(false),
29 18 m_capacity(NON_SPANNING_CAPACITY),
30 18 m_heldOffset(0),
31 18 m_fhp(FHP_UNSET),
32 18 m_pendingIdleCount(0),
33 18 m_leadingIdleCount(0),
34
2/2
✓ Branch 10 taken 18 times.
✓ Branch 16 taken 18 times.
72 m_lastFrameLost(false) {}
35
36 36 ComAggregator ::~ComAggregator() {}
37
38 5 void ComAggregator ::configure(bool spanningEnabled) {
39 // Configuration must happen before any data is aggregated
40 5 FW_ASSERT(this->m_frameSerializer.getSize() == 0, static_cast<FwAssertArgType>(this->m_frameSerializer.getSize()));
41
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (spanningEnabled) {
42 // Every packet header offset in an aggregate must be representable as an 11-bit First Header Pointer
43 // and distinct from the reserved values (CCSDS 132.0-B-3 4.1.2.7.6)
44 5 const FwSizeType aggregationSize = static_cast<FwSizeType>(ComCfg::AggregationSize);
45 5 const FwSizeType fhpRange = static_cast<FwSizeType>(Ccsds::TMSubfields::FHP_IDLE_DATA_ONLY);
46 FW_ASSERT(aggregationSize <= fhpRange, static_cast<FwAssertArgType>(aggregationSize));
47 }
48 5 this->m_spanning = spanningEnabled;
49
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 this->m_capacity = spanningEnabled ? static_cast<FwSizeType>(ComCfg::AggregationSize) : NON_SPANNING_CAPACITY;
50 5 }
51
52 18 void ComAggregator ::preamble() {
53
1/1
✓ Branch 2 taken 18 times.
18 Fw::Success good = Fw::Success::SUCCESS;
54
1/1
✓ Branch 5 taken 18 times.
18 this->comStatusOut_out(0, good);
55 36 }
56
57 // ----------------------------------------------------------------------
58 // Handler implementations for typed input ports
59 // ----------------------------------------------------------------------
60
61 5310 void ComAggregator ::comStatusIn_handler(FwIndexType portNum, Fw::Success& condition) {
62 5310 this->aggregationMachine_sendSignal_status(condition);
63 5310 }
64
65 2667 void ComAggregator ::dataIn_handler(FwIndexType portNum, Fw::Buffer& data, const ComCfg::FrameContext& context) {
66 2667 FW_ASSERT(this->m_spanning || data.getSize() <= this->m_capacity, static_cast<FwAssertArgType>(data.getSize()));
67
1/1
✓ Branch 3 taken 2667 times.
2667 Svc::ComDataContextPair pair(data, context);
68
1/1
✓ Branch 5 taken 2667 times.
2667 this->aggregationMachine_sendSignal_fill(pair);
69 5334 }
70
71 2629 void ComAggregator ::dataReturnIn_handler(FwIndexType portNum, Fw::Buffer& data, const ComCfg::FrameContext& context) {
72 // This handler runs on the returning caller's thread: take ownership atomically
73 2629 const Fw::Buffer::OwnershipState previousState = this->m_bufferState.exchange(Fw::Buffer::OwnershipState::OWNED);
74 2629 FW_ASSERT(previousState == Fw::Buffer::OwnershipState::NOT_OWNED, static_cast<FwAssertArgType>(previousState));
75 2629 }
76
77 2178 void ComAggregator ::timeout_handler(FwIndexType portNum, U32 context) {
78 // Timeout is ignored in WAIT_STATUS state. However, the queue may not process timeout messages until the wait
79 // status is returned because the port chain may be synchronous and downstream components (radio, retry, etc) may
80 // take a long time to complete the transmission of data. This can cause the queue to overflow with messages that
81 // will soon be discarded.
82 //
83 // Therefore, to fix the risk of queue overflow we only queue timeout messages when they would be processed by the
84 // state machine (i.e. in the FILL state). Otherwise, these messages are not queued.
85 //
86 // Behaviorally, this solution will work exactly like the naive implementation with an infinite queue depth, but
87 // prevents queue overflow when using finite queues.
88
2/2
✓ Branch 4 taken 2153 times.
✓ Branch 5 taken 25 times.
2178 if (this->m_allow_timeout) {
89 2153 this->aggregationMachine_sendSignal_timeout();
90 }
91 2178 }
92
93 // ----------------------------------------------------------------------
94 // Implementations for internal state machine actions
95 // ----------------------------------------------------------------------
96
97 2647 void ComAggregator ::Svc_AggregationMachine_action_doClear(SmId smId, Svc_AggregationMachine::Signal signal) {
98 2647 this->m_allow_timeout = true; // Allow timeout messages in FILL state
99 2647 this->m_frameSerializer.resetSer();
100 2647 this->m_frameBuffer.setSize(sizeof(this->m_frameBufferStore));
101
2/2
✓ Branch 2 taken 2647 times.
✓ Branch 11 taken 2647 times.
2647 this->m_lastContext = ComCfg::FrameContext();
102 2647 this->m_fhp = FHP_UNSET;
103 2647 this->dropLostFrameState();
104 2647 this->m_leadingIdleCount = this->m_pendingIdleCount;
105 // Write out any idle packet bytes spanning over from the previous aggregate
106
2/2
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 2643 times.
2647 if (this->m_pendingIdleCount > 0) {
107 8 Fw::SerializeStatus status = this->m_frameSerializer.serializeFrom(
108 8 this->m_pendingIdle, this->m_pendingIdleCount, Fw::Serialization::OMIT_LENGTH);
109 4 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
110 4 this->m_pendingIdleCount = 0;
111 }
112 // Fill from the held buffer (whole packet, or remainder of a spanned packet)
113 2647 this->fillFromHeld();
114 2647 }
115
116 883 void ComAggregator ::Svc_AggregationMachine_action_doFill(SmId smId,
117 Svc_AggregationMachine::Signal signal,
118 const Svc::ComDataContextPair& value) {
119 883 this->markFirstHeaderIfUnset();
120 1766 Fw::SerializeStatus status = this->m_frameSerializer.serializeFrom(
121 1766 value.get_data().getData(), value.get_data().getSize(), Fw::Serialization::OMIT_LENGTH);
122 883 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
123 883 this->m_lastContext = value.get_context();
124 883 this->returnAndSignalReady(value);
125 883 }
126
127 2631 void ComAggregator ::Svc_AggregationMachine_action_doSend(SmId smId, Svc_AggregationMachine::Signal signal) {
128 // Send only when the buffer will be valid
129
1/2
✓ Branch 6 taken 2631 times.
✗ Branch 7 not taken.
2631 if (this->m_frameSerializer.getSize() > 0) {
130 2631 FW_ASSERT(this->m_frameSerializer.getSize() <= this->m_capacity,
131 static_cast<FwAssertArgType>(this->m_frameSerializer.getSize()));
132
3/4
✗ Branch 3 not taken.
✓ Branch 4 taken 2631 times.
✓ Branch 5 taken 2620 times.
✓ Branch 6 taken 11 times.
2631 if (this->m_spanning) {
133 2620 this->fillResidualWithIdle();
134 5240 this->m_lastContext.set_firstHeaderPointer(
135
2/2
✓ Branch 4 taken 2467 times.
✓ Branch 5 taken 153 times.
2620 (this->m_fhp == FHP_UNSET) ? static_cast<U16>(Ccsds::TMSubfields::FHP_NO_PACKET_START) : this->m_fhp);
136 }
137 const Fw::Buffer::OwnershipState previousState =
138 2631 this->m_bufferState.exchange(Fw::Buffer::OwnershipState::NOT_OWNED);
139 2631 FW_ASSERT(previousState == Fw::Buffer::OwnershipState::OWNED, static_cast<FwAssertArgType>(previousState));
140 2631 this->m_frameBuffer.setSize(this->m_frameSerializer.getSize());
141 2631 this->m_allow_timeout = false; // Timeout messages should be discarded in WAIT_STATUS state
142 2631 this->dataOut_out(0, this->m_frameBuffer, this->m_lastContext);
143 }
144 2631 }
145
146 1784 void ComAggregator ::Svc_AggregationMachine_action_doHold(SmId smId,
147 Svc_AggregationMachine::Signal signal,
148 const Svc::ComDataContextPair& value) {
149 1784 FW_ASSERT(not this->m_held.get_data().isValid());
150 1784 this->m_held = value;
151 1784 this->m_heldOffset = 0;
152 1784 }
153
154 1345 void ComAggregator ::Svc_AggregationMachine_action_doSplitHold(SmId smId,
155 Svc_AggregationMachine::Signal signal,
156 const Svc::ComDataContextPair& value) {
157 1345 this->Svc_AggregationMachine_action_doHold(smId, signal, value);
158
3/4
✗ Branch 3 not taken.
✓ Branch 4 taken 1345 times.
✓ Branch 5 taken 1341 times.
✓ Branch 6 taken 4 times.
1345 if (this->m_spanning) {
159 // Split the leading bytes of the held packet into the remaining aggregation space
160 1341 const FwSizeType remaining = this->remainingCapacity();
161
1/2
✓ Branch 0 taken 1341 times.
✗ Branch 1 not taken.
1341 if (remaining > 0) {
162 // The held packet's header starts at the current fill offset of this aggregate
163 1341 this->markFirstHeaderIfUnset();
164 1341 Fw::SerializeStatus status = this->m_frameSerializer.serializeFrom(value.get_data().getData(), remaining,
165 Fw::Serialization::OMIT_LENGTH);
166 1341 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
167 1341 this->m_heldOffset = remaining;
168 1341 this->m_lastContext = value.get_context();
169 }
170 }
171 1345 }
172
173 2663 void ComAggregator ::Svc_AggregationMachine_action_doNoteFailure(SmId smId, Svc_AggregationMachine::Signal signal) {
174 2663 this->m_lastFrameLost = true;
175 2663 }
176
177 ✗ void ComAggregator ::Svc_AggregationMachine_action_assertNoStatus(SmId smId, Svc_AggregationMachine::Signal signal) {
178 // Status is not possible in this state, confirm by assertion
179 ✗ FW_ASSERT(false);
180 ✗ }
181
182 // ----------------------------------------------------------------------
183 // Implementations for internal state machine guards
184 // ----------------------------------------------------------------------
185
186 2228 bool ComAggregator ::Svc_AggregationMachine_guard_isFull(SmId smId,
187 Svc_AggregationMachine::Signal signal,
188 const Svc::ComDataContextPair& value) const {
189 2228 return (this->remainingCapacity() < value.get_data().getSize());
190 }
191
192 883 bool ComAggregator ::Svc_AggregationMachine_guard_willFill(SmId smId,
193 Svc_AggregationMachine::Signal signal,
194 const Svc::ComDataContextPair& value) const {
195 883 return (this->remainingCapacity() == value.get_data().getSize());
196 }
197
198 2153 bool ComAggregator ::Svc_AggregationMachine_guard_isNotEmpty(SmId smId, Svc_AggregationMachine::Signal signal) const {
199 // Carried-over idle bytes are not payload: an aggregate holding only those is empty for timeout purposes
200 2153 return this->m_frameSerializer.getSize() > this->m_leadingIdleCount;
201 }
202
203 5310 bool ComAggregator ::Svc_AggregationMachine_guard_isGood(SmId smId,
204 Svc_AggregationMachine::Signal signal,
205 const Fw::Success& value) const {
206 5310 return value == Fw::Success::SUCCESS;
207 }
208
209 2647 bool ComAggregator ::Svc_AggregationMachine_guard_isSpanFull(SmId smId, Svc_AggregationMachine::Signal signal) const {
210
5/6
✗ Branch 3 not taken.
✓ Branch 4 taken 2647 times.
✓ Branch 5 taken 2624 times.
✓ Branch 6 taken 23 times.
✓ Branch 22 taken 392 times.
✓ Branch 23 taken 2232 times.
2647 return this->m_spanning && (this->m_frameSerializer.getSize() == this->m_capacity);
211 }
212
213 // ----------------------------------------------------------------------
214 // Helper functions
215 // ----------------------------------------------------------------------
216
217 8783 FwSizeType ComAggregator ::remainingCapacity() const {
218 8783 FW_ASSERT(this->m_frameSerializer.getSize() <= this->m_capacity,
219 static_cast<FwAssertArgType>(this->m_frameSerializer.getSize()));
220 8783 return this->m_capacity - this->m_frameSerializer.getSize();
221 }
222
223 3554 void ComAggregator ::markFirstHeaderIfUnset() {
224
5/6
✗ Branch 3 not taken.
✓ Branch 4 taken 3554 times.
✓ Branch 5 taken 3511 times.
✓ Branch 6 taken 43 times.
✓ Branch 11 taken 2467 times.
✓ Branch 12 taken 1044 times.
3554 if (this->m_spanning && this->m_fhp == FHP_UNSET) {
225 2467 this->m_fhp = static_cast<U16>(this->m_frameSerializer.getSize());
226 }
227 3554 }
228
229 2647 void ComAggregator ::fillFromHeld() {
230
2/2
✓ Branch 10 taken 1319 times.
✓ Branch 11 taken 1328 times.
2647 if (this->m_held.get_data().isValid()) {
231 1319 const Fw::Buffer& held = this->m_held.get_data();
232 1319 const FwSizeType heldRemaining = held.getSize() - this->m_heldOffset;
233
2/2
✓ Branch 4 taken 392 times.
✓ Branch 5 taken 927 times.
1319 const FwSizeType fillSize = FW_MIN(this->remainingCapacity(), heldRemaining);
234
2/2
✓ Branch 4 taken 443 times.
✓ Branch 5 taken 876 times.
1319 if (this->m_heldOffset == 0) {
235 // The held packet's header starts at the current fill offset of this aggregate
236 443 this->markFirstHeaderIfUnset();
237 }
238 1319 Fw::SerializeStatus status = this->m_frameSerializer.serializeFrom(held.getData() + this->m_heldOffset,
239 fillSize, Fw::Serialization::OMIT_LENGTH);
240 1319 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
241 1319 this->m_lastContext = this->m_held.get_context();
242 1319 this->m_heldOffset += fillSize;
243
2/2
✓ Branch 8 taken 927 times.
✓ Branch 9 taken 392 times.
1319 if (this->m_heldOffset == held.getSize()) {
244 // Held buffer fully consumed: return it and request more data
245 927 this->returnAndSignalReady(this->m_held);
246
2/2
✓ Branch 2 taken 927 times.
✓ Branch 11 taken 927 times.
927 this->m_held = Svc::ComDataContextPair();
247 927 this->m_heldOffset = 0;
248 }
249 }
250 2647 }
251
252 2620 void ComAggregator ::fillResidualWithIdle() {
253 2620 const FwSizeType residual = this->remainingCapacity();
254
2/2
✓ Branch 0 taken 1733 times.
✓ Branch 1 taken 887 times.
2620 if (residual == 0) {
255 1733 return;
256 }
257 // The idle packet's header starts at the current fill offset of this aggregate
258 887 this->markFirstHeaderIfUnset();
259 // Idle packet size: fill the residual space exactly, spanning a minimum-size idle packet
260 // into the next aggregate when the residual space is too small (CCSDS 132.0-B-3 4.1.4)
261 887 Fw::SerializeStatus status = Fw::SerializeStatus::FW_SERIALIZE_OK;
262
2/2
✓ Branch 0 taken 877 times.
✓ Branch 1 taken 10 times.
887 if (residual >= Ccsds::Utils::IdlePacket::MIN_SIZE) {
263 // Idle packet fits entirely within this aggregate
264 877 status = Ccsds::Utils::IdlePacket::serialize(this->m_frameSerializer, residual);
265 877 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
266 } else {
267 // Stage a minimum-size idle packet, emit the leading bytes now and span the rest
268 10 U8 staging[Ccsds::Utils::IdlePacket::MIN_SIZE] = {};
269
1/1
✓ Branch 2 taken 10 times.
10 Fw::ExternalSerializeBuffer stager(staging, sizeof(staging));
270
1/1
✓ Branch 1 taken 10 times.
10 status = Ccsds::Utils::IdlePacket::serialize(stager, Ccsds::Utils::IdlePacket::MIN_SIZE);
271 10 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
272
1/1
✓ Branch 6 taken 10 times.
10 status = this->m_frameSerializer.serializeFrom(staging, residual, Fw::Serialization::OMIT_LENGTH);
273 10 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
274 10 this->m_pendingIdleCount = Ccsds::Utils::IdlePacket::MIN_SIZE - residual;
275
2/4
✗ Branch 8 not taken.
✓ Branch 9 taken 10 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 10 times.
10 (void)memcpy(this->m_pendingIdle, &staging[residual], this->m_pendingIdleCount);
276 10 }
277 }
278
279 2647 void ComAggregator ::dropLostFrameState() {
280
3/4
✗ Branch 3 not taken.
✓ Branch 4 taken 2647 times.
✓ Branch 5 taken 1304 times.
✓ Branch 6 taken 1343 times.
2647 if (this->m_lastFrameLost) {
281 1304 this->m_pendingIdleCount = 0;
282
6/6
✓ Branch 10 taken 1167 times.
✓ Branch 11 taken 137 times.
✓ Branch 16 taken 857 times.
✓ Branch 17 taken 310 times.
✓ Branch 18 taken 857 times.
✓ Branch 19 taken 447 times.
1304 if (this->m_held.get_data().isValid() && this->m_heldOffset > 0) {
283 857 this->returnAndSignalReady(this->m_held);
284
2/2
✓ Branch 2 taken 857 times.
✓ Branch 11 taken 857 times.
857 this->m_held = Svc::ComDataContextPair();
285 857 this->m_heldOffset = 0;
286 }
287 1304 this->m_lastFrameLost = false;
288 }
289 2647 }
290
291 2667 void ComAggregator ::returnAndSignalReady(const Svc::ComDataContextPair& pair) {
292 // Return port does not alter data and thus const-cast is safe
293
1/1
✓ Branch 14 taken 2667 times.
2667 this->dataReturnOut_out(0, const_cast<Fw::Buffer&>(pair.get_data()), pair.get_context());
294
1/1
✓ Branch 2 taken 2667 times.
2667 Fw::Success good = Fw::Success::SUCCESS;
295
1/1
✓ Branch 5 taken 2667 times.
2667 this->comStatusOut_out(0, good);
296 5334 }
297
298 } // namespace Svc
299