GCC Code Coverage Report


Directory: /home/runner/work/fprime/fprime/Svc/ComAggregator/
File: ComAggregator.cpp
Date: 2026-09-23 22:12:50
Exec Total Coverage
Lines: 117 164 71.3%
Functions: 23 26 88.5%
Branches: 31 66 47.0%

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 1 ComAggregator ::ComAggregator(const char* const compName)
23 : ComAggregatorComponentBase(compName),
24 1 m_bufferState(Fw::Buffer::OwnershipState::OWNED),
25
1/1
✓ Branch 1 taken 1 times.
1 m_frameBuffer(m_frameBufferStore, sizeof(m_frameBufferStore)),
26
1/1
✓ Branch 1 taken 1 times.
1 m_frameSerializer(m_frameBuffer.getSerializer()),
27 1 m_allow_timeout(false),
28 1 m_spanning(false),
29 1 m_capacity(NON_SPANNING_CAPACITY),
30 1 m_heldOffset(0),
31 1 m_fhp(FHP_UNSET),
32 1 m_pendingIdleCount(0),
33 1 m_leadingIdleCount(0),
34
2/2
✓ Branch 3 taken 1 times.
✓ Branch 6 taken 1 times.
3 m_lastFrameLost(false) {}
35
36 2 ComAggregator ::~ComAggregator() {}
37
38 1 void ComAggregator ::configure(bool spanningEnabled) {
39 // Configuration must happen before any data is aggregated
40 1 FW_ASSERT(this->m_frameSerializer.getSize() == 0, static_cast<FwAssertArgType>(this->m_frameSerializer.getSize()));
41
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 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 ✗ const FwSizeType aggregationSize = static_cast<FwSizeType>(ComCfg::AggregationSize);
45 ✗ const FwSizeType fhpRange = static_cast<FwSizeType>(Ccsds::TMSubfields::FHP_IDLE_DATA_ONLY);
46 FW_ASSERT(aggregationSize <= fhpRange, static_cast<FwAssertArgType>(aggregationSize));
47 }
48 1 this->m_spanning = spanningEnabled;
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 this->m_capacity = spanningEnabled ? static_cast<FwSizeType>(ComCfg::AggregationSize) : NON_SPANNING_CAPACITY;
50 1 }
51
52 1 void ComAggregator ::preamble() {
53
1/1
✓ Branch 1 taken 1 times.
1 Fw::Success good = Fw::Success::SUCCESS;
54
1/1
✓ Branch 1 taken 1 times.
1 this->comStatusOut_out(0, good);
55 1 }
56
57 // ----------------------------------------------------------------------
58 // Handler implementations for typed input ports
59 // ----------------------------------------------------------------------
60
61 240 void ComAggregator ::comStatusIn_handler(FwIndexType portNum, Fw::Success& condition) {
62 240 this->aggregationMachine_sendSignal_status(condition);
63 240 }
64
65 703 void ComAggregator ::dataIn_handler(FwIndexType portNum, Fw::Buffer& data, const ComCfg::FrameContext& context) {
66 703 FW_ASSERT(this->m_spanning || data.getSize() <= this->m_capacity, static_cast<FwAssertArgType>(data.getSize()));
67
1/1
✓ Branch 1 taken 703 times.
703 Svc::ComDataContextPair pair(data, context);
68
1/1
✓ Branch 1 taken 703 times.
703 this->aggregationMachine_sendSignal_fill(pair);
69 703 }
70
71 239 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 239 const Fw::Buffer::OwnershipState previousState = this->m_bufferState.exchange(Fw::Buffer::OwnershipState::OWNED);
74 239 FW_ASSERT(previousState == Fw::Buffer::OwnershipState::NOT_OWNED, static_cast<FwAssertArgType>(previousState));
75 239 }
76
77 241 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 1 taken 240 times.
✓ Branch 2 taken 1 times.
241 if (this->m_allow_timeout) {
89 240 this->aggregationMachine_sendSignal_timeout();
90 }
91 241 }
92
93 // ----------------------------------------------------------------------
94 // Implementations for internal state machine actions
95 // ----------------------------------------------------------------------
96
97 240 void ComAggregator ::Svc_AggregationMachine_action_doClear(SmId smId, Svc_AggregationMachine::Signal signal) {
98 240 this->m_allow_timeout = true; // Allow timeout messages in FILL state
99 240 this->m_frameSerializer.resetSer();
100 240 this->m_frameBuffer.setSize(sizeof(this->m_frameBufferStore));
101
2/2
✓ Branch 1 taken 240 times.
✓ Branch 4 taken 240 times.
240 this->m_lastContext = ComCfg::FrameContext();
102 240 this->m_fhp = FHP_UNSET;
103 240 this->dropLostFrameState();
104 240 this->m_leadingIdleCount = this->m_pendingIdleCount;
105 // Write out any idle packet bytes spanning over from the previous aggregate
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 240 times.
240 if (this->m_pendingIdleCount > 0) {
107 ✗ Fw::SerializeStatus status = this->m_frameSerializer.serializeFrom(
108 ✗ this->m_pendingIdle, this->m_pendingIdleCount, Fw::Serialization::OMIT_LENGTH);
109 ✗ FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
110 ✗ this->m_pendingIdleCount = 0;
111 }
112 // Fill from the held buffer (whole packet, or remainder of a spanned packet)
113 240 this->fillFromHeld();
114 240 }
115
116 696 void ComAggregator ::Svc_AggregationMachine_action_doFill(SmId smId,
117 Svc_AggregationMachine::Signal signal,
118 const Svc::ComDataContextPair& value) {
119 696 this->markFirstHeaderIfUnset();
120 696 Fw::SerializeStatus status = this->m_frameSerializer.serializeFrom(
121 1392 value.get_data().getData(), value.get_data().getSize(), Fw::Serialization::OMIT_LENGTH);
122 696 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
123 696 this->m_lastContext = value.get_context();
124 696 this->returnAndSignalReady(value);
125 696 }
126
127 239 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 1 taken 239 times.
✗ Branch 2 not taken.
239 if (this->m_frameSerializer.getSize() > 0) {
130 239 FW_ASSERT(this->m_frameSerializer.getSize() <= this->m_capacity,
131 static_cast<FwAssertArgType>(this->m_frameSerializer.getSize()));
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 239 times.
239 if (this->m_spanning) {
133 ✗ this->fillResidualWithIdle();
134 ✗ this->m_lastContext.set_firstHeaderPointer(
135 ✗ (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 239 this->m_bufferState.exchange(Fw::Buffer::OwnershipState::NOT_OWNED);
139 239 FW_ASSERT(previousState == Fw::Buffer::OwnershipState::OWNED, static_cast<FwAssertArgType>(previousState));
140 239 this->m_frameBuffer.setSize(this->m_frameSerializer.getSize());
141 239 this->m_allow_timeout = false; // Timeout messages should be discarded in WAIT_STATUS state
142 239 this->dataOut_out(0, this->m_frameBuffer, this->m_lastContext);
143 }
144 239 }
145
146 7 void ComAggregator ::Svc_AggregationMachine_action_doHold(SmId smId,
147 Svc_AggregationMachine::Signal signal,
148 const Svc::ComDataContextPair& value) {
149 7 FW_ASSERT(not this->m_held.get_data().isValid());
150 7 this->m_held = value;
151 7 this->m_heldOffset = 0;
152 7 }
153
154 2 void ComAggregator ::Svc_AggregationMachine_action_doSplitHold(SmId smId,
155 Svc_AggregationMachine::Signal signal,
156 const Svc::ComDataContextPair& value) {
157 2 this->Svc_AggregationMachine_action_doHold(smId, signal, value);
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (this->m_spanning) {
159 // Split the leading bytes of the held packet into the remaining aggregation space
160 ✗ const FwSizeType remaining = this->remainingCapacity();
161 ✗ if (remaining > 0) {
162 // The held packet's header starts at the current fill offset of this aggregate
163 ✗ this->markFirstHeaderIfUnset();
164 ✗ Fw::SerializeStatus status = this->m_frameSerializer.serializeFrom(value.get_data().getData(), remaining,
165 Fw::Serialization::OMIT_LENGTH);
166 ✗ FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
167 ✗ this->m_heldOffset = remaining;
168 ✗ this->m_lastContext = value.get_context();
169 }
170 }
171 2 }
172
173 ✗ void ComAggregator ::Svc_AggregationMachine_action_doNoteFailure(SmId smId, Svc_AggregationMachine::Signal signal) {
174 ✗ this->m_lastFrameLost = true;
175 ✗ }
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 698 bool ComAggregator ::Svc_AggregationMachine_guard_isFull(SmId smId,
187 Svc_AggregationMachine::Signal signal,
188 const Svc::ComDataContextPair& value) const {
189 698 return (this->remainingCapacity() < value.get_data().getSize());
190 }
191
192 696 bool ComAggregator ::Svc_AggregationMachine_guard_willFill(SmId smId,
193 Svc_AggregationMachine::Signal signal,
194 const Svc::ComDataContextPair& value) const {
195 696 return (this->remainingCapacity() == value.get_data().getSize());
196 }
197
198 240 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 240 return this->m_frameSerializer.getSize() > this->m_leadingIdleCount;
201 }
202
203 240 bool ComAggregator ::Svc_AggregationMachine_guard_isGood(SmId smId,
204 Svc_AggregationMachine::Signal signal,
205 const Fw::Success& value) const {
206 240 return value == Fw::Success::SUCCESS;
207 }
208
209 240 bool ComAggregator ::Svc_AggregationMachine_guard_isSpanFull(SmId smId, Svc_AggregationMachine::Signal signal) const {
210
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 240 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
240 return this->m_spanning && (this->m_frameSerializer.getSize() == this->m_capacity);
211 }
212
213 // ----------------------------------------------------------------------
214 // Helper functions
215 // ----------------------------------------------------------------------
216
217 1401 FwSizeType ComAggregator ::remainingCapacity() const {
218 1401 FW_ASSERT(this->m_frameSerializer.getSize() <= this->m_capacity,
219 static_cast<FwAssertArgType>(this->m_frameSerializer.getSize()));
220 1401 return this->m_capacity - this->m_frameSerializer.getSize();
221 }
222
223 703 void ComAggregator ::markFirstHeaderIfUnset() {
224
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 703 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
703 if (this->m_spanning && this->m_fhp == FHP_UNSET) {
225 ✗ this->m_fhp = static_cast<U16>(this->m_frameSerializer.getSize());
226 }
227 703 }
228
229 240 void ComAggregator ::fillFromHeld() {
230
2/2
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 233 times.
240 if (this->m_held.get_data().isValid()) {
231 7 const Fw::Buffer& held = this->m_held.get_data();
232 7 const FwSizeType heldRemaining = held.getSize() - this->m_heldOffset;
233
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 const FwSizeType fillSize = FW_MIN(this->remainingCapacity(), heldRemaining);
234
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (this->m_heldOffset == 0) {
235 // The held packet's header starts at the current fill offset of this aggregate
236 7 this->markFirstHeaderIfUnset();
237 }
238 7 Fw::SerializeStatus status = this->m_frameSerializer.serializeFrom(held.getData() + this->m_heldOffset,
239 fillSize, Fw::Serialization::OMIT_LENGTH);
240 7 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
241 7 this->m_lastContext = this->m_held.get_context();
242 7 this->m_heldOffset += fillSize;
243
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 if (this->m_heldOffset == held.getSize()) {
244 // Held buffer fully consumed: return it and request more data
245 7 this->returnAndSignalReady(this->m_held);
246
2/2
✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
7 this->m_held = Svc::ComDataContextPair();
247 7 this->m_heldOffset = 0;
248 }
249 }
250 240 }
251
252 ✗ void ComAggregator ::fillResidualWithIdle() {
253 ✗ const FwSizeType residual = this->remainingCapacity();
254 ✗ if (residual == 0) {
255 ✗ return;
256 }
257 // The idle packet's header starts at the current fill offset of this aggregate
258 ✗ 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 ✗ Fw::SerializeStatus status = Fw::SerializeStatus::FW_SERIALIZE_OK;
262 ✗ if (residual >= Ccsds::Utils::IdlePacket::MIN_SIZE) {
263 // Idle packet fits entirely within this aggregate
264 ✗ status = Ccsds::Utils::IdlePacket::serialize(this->m_frameSerializer, residual);
265 ✗ 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 ✗ U8 staging[Ccsds::Utils::IdlePacket::MIN_SIZE] = {};
269 ✗ Fw::ExternalSerializeBuffer stager(staging, sizeof(staging));
270 ✗ status = Ccsds::Utils::IdlePacket::serialize(stager, Ccsds::Utils::IdlePacket::MIN_SIZE);
271 ✗ FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
272 ✗ status = this->m_frameSerializer.serializeFrom(staging, residual, Fw::Serialization::OMIT_LENGTH);
273 ✗ FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK);
274 ✗ this->m_pendingIdleCount = Ccsds::Utils::IdlePacket::MIN_SIZE - residual;
275 ✗ (void)memcpy(this->m_pendingIdle, &staging[residual], this->m_pendingIdleCount);
276 ✗ }
277 }
278
279 240 void ComAggregator ::dropLostFrameState() {
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 240 times.
240 if (this->m_lastFrameLost) {
281 ✗ this->m_pendingIdleCount = 0;
282 ✗ if (this->m_held.get_data().isValid() && this->m_heldOffset > 0) {
283 ✗ this->returnAndSignalReady(this->m_held);
284 ✗ this->m_held = Svc::ComDataContextPair();
285 ✗ this->m_heldOffset = 0;
286 }
287 ✗ this->m_lastFrameLost = false;
288 }
289 240 }
290
291 703 void ComAggregator ::returnAndSignalReady(const Svc::ComDataContextPair& pair) {
292 // Return port does not alter data and thus const-cast is safe
293
1/1
✓ Branch 3 taken 703 times.
703 this->dataReturnOut_out(0, const_cast<Fw::Buffer&>(pair.get_data()), pair.get_context());
294
1/1
✓ Branch 1 taken 703 times.
703 Fw::Success good = Fw::Success::SUCCESS;
295
1/1
✓ Branch 1 taken 703 times.
703 this->comStatusOut_out(0, good);
296 703 }
297
298 } // namespace Svc
299