GCC Code Coverage Report


Directory: ./
File: Svc/CmdDispatcher/CommandDispatcherImpl.cpp
Date: 2026-09-23 21:11:01
Exec Total Coverage
Lines: 140 143 97.9%
Functions: 14 15 93.3%
Branches: 134 146 91.8%

Line Branch Exec Source
1 /*
2 * CommandDispatcherImpl.cpp
3 *
4 * Created on: May 13, 2014
5 * Author: Timothy Canham
6 */
7
8 #include <Fw/Cmd/CmdPacket.hpp>
9 #include <Fw/Types/Assert.hpp>
10 #include <Svc/CmdDispatcher/CommandDispatcherImpl.hpp>
11 #include <cstdio>
12 #include <cstring>
13 #include <limits>
14
15 // Check the CMD_DISPATCHER_DISPATCH_TABLE_SIZE and CMD_DISPATCHER_SEQUENCER_TABLE_SIZE for overflow
16 static_assert(CMD_DISPATCHER_DISPATCH_TABLE_SIZE <= std::numeric_limits<FwOpcodeType>::max(),
17 "Opcode table limited to opcode range");
18 static_assert(CMD_DISPATCHER_SEQUENCER_TABLE_SIZE <= std::numeric_limits<U32>::max(),
19 "Sequencer table limited to range of U32");
20
21 namespace Svc {
22 15 CommandDispatcherImpl::CommandDispatcherImpl(const char* name)
23 : CommandDispatcherComponentBase(name),
24 15 m_seq(0),
25 15 m_seqWrapped(false),
26 15 m_executeWhenSequenceTableFull(CmdDispatcherCfg::EXECUTE_WHEN_SEQUENCE_TABLE_FULL_DEFAULT),
27 15 m_numCmdsDispatched(0),
28 15 m_numCmdErrors(0),
29
2/2
✓ Branch 9 taken 15 times.
✓ Branch 15 taken 15 times.
30 m_numCmdsDropped(0) {}
30
31 30 CommandDispatcherImpl::~CommandDispatcherImpl() {}
32
33 2 void CommandDispatcherImpl::configure(bool executeWhenSequenceTableFull) {
34 2 this->m_executeWhenSequenceTableFull = executeWhenSequenceTableFull;
35 2 }
36
37 81 void CommandDispatcherImpl::advanceSequenceNumber() {
38
2/2
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 79 times.
81 if (this->m_seq == std::numeric_limits<U32>::max()) {
39 2 this->m_seqWrapped = true;
40 }
41 81 ++this->m_seq;
42 81 }
43
44 78 U32 CommandDispatcherImpl::allocateSequenceNumber() {
45 // Before the first wrap, m_seq is monotonic and cannot collide with a tracked key
46
3/4
✗ Branch 3 not taken.
✓ Branch 4 taken 78 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 77 times.
78 if (this->m_seqWrapped) {
47 1 SequenceTrackerEntry trackedCmd;
48
1/1
✓ Branch 6 taken 1 times.
1 const FwSizeType numTrackedCommands = this->m_sequenceTracker.getSize();
49
50 // At most numTrackedCommands keys can collide, so the loop is bounded by the table size
51
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 for (FwSizeType i = 0; i < numTrackedCommands; ++i) {
52
3/3
✓ Branch 7 taken 2 times.
✓ Branch 16 taken 1 times.
✓ Branch 17 taken 1 times.
2 if (this->m_sequenceTracker.find(this->m_seq, trackedCmd) != Fw::Success::SUCCESS) {
53 1 break;
54 }
55 1 this->advanceSequenceNumber();
56 }
57 }
58
59 78 const U32 sequenceNumber = this->m_seq;
60 78 this->advanceSequenceNumber();
61 78 return sequenceNumber;
62 }
63
64 55 void CommandDispatcherImpl::compCmdReg_handler(FwIndexType portNum, FwOpcodeType opCode) {
65 55 FwIndexType existingPort;
66
3/3
✓ Branch 4 taken 55 times.
✓ Branch 13 taken 1 times.
✓ Branch 14 taken 54 times.
55 if (this->m_entryTable.find(opCode, existingPort) == Fw::Success::SUCCESS) {
67 // Opcode already present — must be the same port (re-registration)
68 1 FW_ASSERT(existingPort == portNum, static_cast<FwAssertArgType>(opCode));
69
1/1
✓ Branch 6 taken 1 times.
1 this->log_DIAGNOSTIC_OpCodeReregistered(CmdDispatcherCfg::getEventOpcode(opCode), portNum);
70 } else {
71
1/1
✓ Branch 6 taken 54 times.
54 const I32 slot = static_cast<I32>(this->m_entryTable.getSize());
72
1/1
✓ Branch 4 taken 54 times.
54 const Fw::Success status = this->m_entryTable.insert(opCode, portNum);
73 54 FW_ASSERT(status == Fw::Success::SUCCESS, static_cast<FwAssertArgType>(opCode));
74
1/1
✓ Branch 6 taken 54 times.
54 this->log_DIAGNOSTIC_OpCodeRegistered(CmdDispatcherCfg::getEventOpcode(opCode), portNum, slot);
75 54 }
76 55 }
77
78 20 void CommandDispatcherImpl::compCmdStat_handler(FwIndexType portNum,
79 FwOpcodeType opCode,
80 U32 cmdSeq,
81 const Fw::CmdResponse& response) {
82 // check response and log
83
3/4
✗ Branch 3 not taken.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 17 times.
✓ Branch 6 taken 3 times.
20 if (Fw::CmdResponse::OK == response.e) {
84
1/1
✓ Branch 6 taken 17 times.
17 this->log_COMMAND_OpCodeCompleted(CmdDispatcherCfg::getEventOpcode(opCode));
85 } else {
86 3 this->m_numCmdErrors++;
87 3 FW_ASSERT(response.e != Fw::CmdResponse::OK);
88
1/1
✓ Branch 6 taken 3 times.
3 this->log_COMMAND_OpCodeError(CmdDispatcherCfg::getEventOpcode(opCode), response);
89 }
90 // look for command source
91 20 SequenceTrackerEntry trackedCmd;
92
1/1
✓ Branch 4 taken 20 times.
20 const Fw::Success removeStatus = this->m_sequenceTracker.remove(cmdSeq, trackedCmd);
93
2/2
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 1 times.
20 if (removeStatus == Fw::Success::SUCCESS) {
94 19 const FwIndexType portToCall = trackedCmd.callerPort;
95 19 const U32 context = trackedCmd.context;
96 19 FW_ASSERT(opCode == trackedCmd.opCode);
97 19 FW_ASSERT(portToCall < this->getNum_seqCmdStatus_OutputPorts());
98
99 // call port to report status
100
2/3
✓ Branch 5 taken 19 times.
✓ Branch 7 taken 19 times.
✗ Branch 8 not taken.
19 if (this->isConnected_seqCmdStatus_OutputPort(portToCall)) {
101 // NOTE: seqCmdStatus port forwards three arguments: (opCode, cmdSeq, response).
102 // However, the cmdSeq value has no meaning for the calling sequencer.
103 // Instead, the context value is forwarded to allow the caller to utilize it if needed.
104
1/1
✓ Branch 5 taken 19 times.
19 this->seqCmdStatus_out(portToCall, opCode, context, response);
105 }
106 }
107 40 }
108
109 78 void CommandDispatcherImpl::seqCmdBuff_handler(FwIndexType portNum, Fw::ComBuffer& data, U32 context) {
110
1/1
✓ Branch 2 taken 78 times.
78 Fw::CmdPacket cmdPkt;
111
1/1
✓ Branch 4 taken 78 times.
78 Fw::SerializeStatus stat = cmdPkt.deserializeFrom(data);
112
1/1
✓ Branch 5 taken 78 times.
78 const bool portIsConnected = this->isConnected_seqCmdStatus_OutputPort(portNum);
113
114
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 77 times.
78 if (stat != Fw::FW_SERIALIZE_OK) {
115
1/1
✓ Branch 2 taken 1 times.
1 Fw::DeserialStatus serErr(static_cast<Fw::DeserialStatus::t>(stat));
116
1/1
✓ Branch 5 taken 1 times.
1 this->log_WARNING_HI_MalformedCommand(serErr);
117
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (portIsConnected) {
118
3/3
✓ Branch 6 taken 1 times.
✓ Branch 11 taken 1 times.
✓ Branch 14 taken 1 times.
1 this->seqCmdStatus_out(portNum, cmdPkt.getOpCode(), context, Fw::CmdResponse::VALIDATION_ERROR);
119 }
120 1 return;
121 1 }
122
123 // look up opcode in dispatch map
124 77 FwIndexType entryPort;
125
2/2
✓ Branch 6 taken 77 times.
✓ Branch 10 taken 77 times.
77 Fw::Success findStatus = this->m_entryTable.find(cmdPkt.getOpCode(), entryPort);
126
6/7
✓ Branch 2 taken 75 times.
✓ Branch 3 taken 2 times.
✓ Branch 9 taken 75 times.
✓ Branch 11 taken 75 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 75 times.
✓ Branch 14 taken 2 times.
77 if (findStatus == Fw::Success::SUCCESS and this->isConnected_compCmdSend_OutputPort(entryPort)) {
127
1/1
✓ Branch 2 taken 75 times.
75 Fw::Success pendingInsertStatus = Fw::Success::SUCCESS;
128
1/1
✓ Branch 5 taken 75 times.
75 const U32 sequenceNumber = this->allocateSequenceNumber();
129
130 // register command in command tracker only if response port is connect
131
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
75 if (portIsConnected) {
132 75 SequenceTrackerEntry pendingCmd;
133
1/1
✓ Branch 2 taken 75 times.
75 pendingCmd.opCode = cmdPkt.getOpCode();
134 75 pendingCmd.context = context;
135 75 pendingCmd.callerPort = portNum;
136
137
2/2
✓ Branch 4 taken 75 times.
✓ Branch 9 taken 75 times.
75 pendingInsertStatus = this->m_sequenceTracker.insert(sequenceNumber, pendingCmd);
138
139 // if sequence table is full, reject here unless configured to dispatch untracked
140
7/8
✗ Branch 3 not taken.
✓ Branch 4 taken 75 times.
✓ Branch 5 taken 49 times.
✓ Branch 6 taken 26 times.
✓ Branch 9 taken 1 times.
✓ Branch 10 taken 48 times.
✓ Branch 11 taken 1 times.
✓ Branch 12 taken 74 times.
75 if (not this->m_executeWhenSequenceTableFull and pendingInsertStatus != Fw::Success::SUCCESS) {
141
2/2
✓ Branch 6 taken 1 times.
✓ Branch 10 taken 1 times.
1 this->log_WARNING_HI_TooManyCommands(CmdDispatcherCfg::getEventOpcode(cmdPkt.getOpCode()));
142
3/3
✓ Branch 6 taken 1 times.
✓ Branch 11 taken 1 times.
✓ Branch 14 taken 1 times.
1 this->seqCmdStatus_out(portNum, cmdPkt.getOpCode(), context, Fw::CmdResponse::EXECUTION_ERROR);
143 1 return;
144 }
145 } // end if status port connected
146 // pass arguments to argument buffer
147
3/3
✓ Branch 6 taken 74 times.
✓ Branch 10 taken 74 times.
✓ Branch 13 taken 74 times.
74 this->compCmdSend_out(entryPort, cmdPkt.getOpCode(), sequenceNumber, cmdPkt.getArgBuffer());
148 // log dispatched command
149
2/2
✓ Branch 6 taken 74 times.
✓ Branch 10 taken 74 times.
74 this->log_COMMAND_OpCodeDispatched(CmdDispatcherCfg::getEventOpcode(cmdPkt.getOpCode()), entryPort);
150
151 // increment command count
152 74 this->m_numCmdsDispatched++;
153
154 // pendingInsertStatus is only non-SUCCESS for a connected caller whose insert failed (see check above)
155
7/8
✗ Branch 3 not taken.
✓ Branch 4 taken 74 times.
✓ Branch 5 taken 26 times.
✓ Branch 6 taken 48 times.
✓ Branch 9 taken 1 times.
✓ Branch 10 taken 25 times.
✓ Branch 11 taken 1 times.
✓ Branch 12 taken 73 times.
74 if (this->m_executeWhenSequenceTableFull and pendingInsertStatus != Fw::Success::SUCCESS) {
156
2/2
✓ Branch 6 taken 1 times.
✓ Branch 10 taken 1 times.
1 this->log_WARNING_HI_TooManyCommands(CmdDispatcherCfg::getEventOpcode(cmdPkt.getOpCode()));
157
3/3
✓ Branch 6 taken 1 times.
✓ Branch 11 taken 1 times.
✓ Branch 14 taken 1 times.
1 this->seqCmdStatus_out(portNum, cmdPkt.getOpCode(), context, Fw::CmdResponse::DISPATCHED_UNTRACKED);
158 }
159
2/2
✓ Branch 2 taken 74 times.
✓ Branch 3 taken 1 times.
75 } else {
160
2/2
✓ Branch 6 taken 2 times.
✓ Branch 10 taken 2 times.
2 this->log_WARNING_HI_InvalidCommand(CmdDispatcherCfg::getEventOpcode(cmdPkt.getOpCode()));
161 2 this->m_numCmdErrors++;
162 // Fail command back to port, if connected
163
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (portIsConnected) {
164
3/3
✓ Branch 6 taken 2 times.
✓ Branch 11 taken 2 times.
✓ Branch 14 taken 2 times.
2 this->seqCmdStatus_out(portNum, cmdPkt.getOpCode(), context, Fw::CmdResponse::INVALID_OPCODE);
165 }
166 // Preserve the existing behavior of consuming a sequence number for an invalid opcode.
167 2 this->advanceSequenceNumber();
168 }
169 79 }
170
171 4 void CommandDispatcherImpl ::run_handler(FwIndexType portNum, U32 context) {
172
2/2
✓ Branch 6 taken 4 times.
✓ Branch 12 taken 4 times.
8 this->tlmWrite_CommandsDropped(this->m_numCmdsDropped.load(std::memory_order_relaxed));
173
2/2
✓ Branch 6 taken 4 times.
✓ Branch 13 taken 4 times.
4 this->tlmWrite_CommandErrors(this->m_numCmdErrors);
174
2/2
✓ Branch 6 taken 4 times.
✓ Branch 13 taken 4 times.
4 this->tlmWrite_CommandsDispatched(this->m_numCmdsDispatched);
175 4 }
176
177 14 void CommandDispatcherImpl::CMD_NO_OP_cmdHandler(FwOpcodeType opCode, U32 cmdSeq) {
178
1/1
✓ Branch 2 taken 14 times.
14 Fw::LogStringArg no_op_string("Hello, World!");
179 // Log event for NO_OP here.
180
1/1
✓ Branch 5 taken 14 times.
14 this->log_ACTIVITY_HI_NoOpReceived();
181
2/2
✓ Branch 6 taken 14 times.
✓ Branch 9 taken 14 times.
14 this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
182 28 }
183
184 1 void CommandDispatcherImpl::CMD_NO_OP_STRING_cmdHandler(FwOpcodeType opCode, U32 cmdSeq, const Fw::CmdStringArg& arg1) {
185
1/1
✓ Branch 6 taken 1 times.
1 Fw::LogStringArg msg(arg1.toChar());
186 // Echo the NO_OP_STRING args here.
187
1/1
✓ Branch 5 taken 1 times.
1 this->log_ACTIVITY_HI_NoOpStringReceived(msg);
188
2/2
✓ Branch 6 taken 1 times.
✓ Branch 9 taken 1 times.
1 this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
189 2 }
190
191 1 void CommandDispatcherImpl::CMD_TEST_CMD_1_cmdHandler(FwOpcodeType opCode, U32 cmdSeq, I32 arg1, F32 arg2, U8 arg3) {
192 1 this->log_ACTIVITY_HI_TestCmd1Args(arg1, arg2, arg3);
193
2/2
✓ Branch 6 taken 1 times.
✓ Branch 9 taken 1 times.
1 this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
194 1 }
195
196 1 void CommandDispatcherImpl::CMD_CLEAR_TRACKING_cmdHandler(FwOpcodeType opCode, U32 cmdSeq) {
197 // Preserve this command's own entry so its OK status still reaches the caller
198 1 SequenceTrackerEntry selfEntry = {};
199
1/1
✓ Branch 4 taken 1 times.
1 const bool selfTracked = (this->m_sequenceTracker.find(cmdSeq, selfEntry) == Fw::Success::SUCCESS);
200
201 // Notify every other caller that its pending status will never arrive
202
7/7
✓ Branch 5 taken 1 times.
✓ Branch 9 taken 1 times.
✓ Branch 12 taken 2 times.
✓ Branch 16 taken 2 times.
✓ Branch 19 taken 3 times.
✓ Branch 21 taken 2 times.
✓ Branch 22 taken 1 times.
5 for (const auto& entry : this->m_sequenceTracker) {
203
3/3
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 1 times.
2 if (entry.getKey() == cmdSeq) {
204 1 continue;
205 }
206
1/1
✓ Branch 7 taken 1 times.
1 const SequenceTrackerEntry& trackedCmd = entry.getValue();
207 1 FW_ASSERT(trackedCmd.callerPort < this->getNum_seqCmdStatus_OutputPorts());
208
2/3
✓ Branch 7 taken 1 times.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
1 if (this->isConnected_seqCmdStatus_OutputPort(trackedCmd.callerPort)) {
209
2/2
✓ Branch 6 taken 1 times.
✓ Branch 15 taken 1 times.
1 this->seqCmdStatus_out(trackedCmd.callerPort, trackedCmd.opCode, trackedCmd.context,
210 Fw::CmdResponse::CLEARED);
211 }
212 1 }
213
214
1/1
✓ Branch 6 taken 1 times.
1 this->m_sequenceTracker.clear();
215
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (selfTracked) {
216
1/1
✓ Branch 4 taken 1 times.
1 const Fw::Success status = this->m_sequenceTracker.insert(cmdSeq, selfEntry);
217 1 FW_ASSERT(status == Fw::Success::SUCCESS);
218 1 }
219
2/2
✓ Branch 6 taken 1 times.
✓ Branch 9 taken 1 times.
1 this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
220 1 }
221
222 ✗ void CommandDispatcherImpl::pingIn_handler(FwIndexType portNum, U32 key) {
223 // respond to ping
224 ✗ this->pingOut_out(0, key);
225 ✗ }
226
227 74702 void CommandDispatcherImpl::seqCmdBuff_overflowHook(FwIndexType portNum, Fw::ComBuffer& data, U32 context) {
228 74702 FwOpcodeType opcode = 0; // Note: 0 = Reserved opcode
229 if (CmdDispatcherCfg::IncludeCommandOpcodesInEvents) {
230
1/1
✓ Branch 2 taken 75038 times.
74702 Fw::CmdPacket cmdPkt;
231
1/1
✓ Branch 4 taken 75761 times.
75038 const Fw::SerializeStatus stat = cmdPkt.deserializeFrom(data);
232
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 75755 times.
75761 if (stat == Fw::FW_SERIALIZE_OK) {
233
1/1
✓ Branch 2 taken 6 times.
6 opcode = cmdPkt.getOpCode();
234 }
235 75761 }
236
237 72295 this->log_WARNING_HI_CommandDroppedQueueOverflow(CmdDispatcherCfg::getEventOpcode(opcode), context);
238 // This hook runs on the caller's thread; the counter is atomic so no lock is needed
239 69662 this->m_numCmdsDropped.fetch_add(1, std::memory_order_relaxed);
240 76803 }
241
242 } // namespace Svc
243