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