GCC Code Coverage Report


Directory: Svc/CmdDispatcher/
File: CommandDispatcherImpl.cpp
Date: 2026-11-03 21:18:06
Exec Total Coverage
Lines: 147 153 96.1%
Functions: 15 17 88.2%
Branches: 136 149 91.3%

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