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