F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
CommandDispatcherImpl.cpp
Go to the documentation of this file.
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>
11 #include <cstdio>
12 
13 // Check the CMD_DISPATCHER_DISPATCH_TABLE_SIZE and CMD_DISPATCHER_SEQUENCER_TABLE_SIZE for overflow
14 static_assert(CMD_DISPATCHER_DISPATCH_TABLE_SIZE <= std::numeric_limits<FwOpcodeType>::max(),
15  "Opcode table limited to opcode range");
16 static_assert(CMD_DISPATCHER_SEQUENCER_TABLE_SIZE <= std::numeric_limits<U32>::max(),
17  "Sequencer table limited to range of U32");
18 
19 namespace Svc {
21  : CommandDispatcherComponentBase(name), m_seq(0), m_numCmdsDispatched(0), m_numCmdErrors(0) {
22  memset(this->m_entryTable, 0, sizeof(this->m_entryTable));
23  memset(this->m_sequenceTracker, 0, sizeof(this->m_sequenceTracker));
24 }
25 
27 
28 void CommandDispatcherImpl::compCmdReg_handler(FwIndexType portNum, FwOpcodeType opCode) {
29  // search for an empty slot
30  bool slotFound = false;
31  for (FwOpcodeType slot = 0; slot < FW_NUM_ARRAY_ELEMENTS(this->m_entryTable); slot++) {
32  if ((not this->m_entryTable[slot].used) and (not slotFound)) {
33  this->m_entryTable[slot].opcode = opCode;
34  this->m_entryTable[slot].port = portNum;
35  this->m_entryTable[slot].used = true;
36  this->log_DIAGNOSTIC_OpCodeRegistered(opCode, portNum, static_cast<I32>(slot));
37  slotFound = true;
38  } else if ((this->m_entryTable[slot].used) && (this->m_entryTable[slot].opcode == opCode) &&
39  (this->m_entryTable[slot].port == portNum) && (not slotFound)) {
40  slotFound = true;
41  this->log_DIAGNOSTIC_OpCodeReregistered(opCode, portNum);
42  } else if (this->m_entryTable[slot].used) { // make sure no duplicates
43  FW_ASSERT(this->m_entryTable[slot].opcode != opCode, static_cast<FwAssertArgType>(opCode));
44  }
45  }
46  FW_ASSERT(slotFound, static_cast<FwAssertArgType>(opCode));
47 }
48 
49 void CommandDispatcherImpl::compCmdStat_handler(FwIndexType portNum,
50  FwOpcodeType opCode,
51  U32 cmdSeq,
52  const Fw::CmdResponse& response) {
53  // check response and log
54  if (Fw::CmdResponse::OK == response.e) {
55  this->log_COMMAND_OpCodeCompleted(opCode);
56  } else {
57  this->m_numCmdErrors++;
58  this->tlmWrite_CommandErrors(this->m_numCmdErrors);
59  FW_ASSERT(response.e != Fw::CmdResponse::OK);
60  this->log_COMMAND_OpCodeError(opCode, response);
61  }
62  // look for command source
63  FwIndexType portToCall = -1;
64  U32 context;
65  for (U32 pending = 0; pending < FW_NUM_ARRAY_ELEMENTS(this->m_sequenceTracker); pending++) {
66  if ((this->m_sequenceTracker[pending].seq == cmdSeq) && (this->m_sequenceTracker[pending].used)) {
67  portToCall = this->m_sequenceTracker[pending].callerPort;
68  context = this->m_sequenceTracker[pending].context;
69  FW_ASSERT(opCode == this->m_sequenceTracker[pending].opCode);
70  FW_ASSERT(portToCall < this->getNum_seqCmdStatus_OutputPorts());
71  this->m_sequenceTracker[pending].used = false;
72  break;
73  }
74  }
75 
76  if (portToCall != -1) {
77  // call port to report status
78  if (this->isConnected_seqCmdStatus_OutputPort(portToCall)) {
79  // NOTE: seqCmdStatus port forwards three arguments: (opCode, cmdSeq, response).
80  // However, the cmdSeq value has no meaning for the calling sequencer.
81  // Instead, the context value is forwarded to allow the caller to utilize it if needed.
82  this->seqCmdStatus_out(portToCall, opCode, context, response);
83  }
84  }
85 }
86 
87 void CommandDispatcherImpl::seqCmdBuff_handler(FwIndexType portNum, Fw::ComBuffer& data, U32 context) {
88  Fw::CmdPacket cmdPkt;
89  Fw::SerializeStatus stat = cmdPkt.deserialize(data);
90 
91  if (stat != Fw::FW_SERIALIZE_OK) {
92  Fw::DeserialStatus serErr(static_cast<Fw::DeserialStatus::t>(stat));
93  this->log_WARNING_HI_MalformedCommand(serErr);
94  if (this->isConnected_seqCmdStatus_OutputPort(portNum)) {
95  this->seqCmdStatus_out(portNum, cmdPkt.getOpCode(), context, Fw::CmdResponse::VALIDATION_ERROR);
96  }
97  return;
98  }
99 
100  // search for opcode in dispatch table
101  FwOpcodeType entry;
102  bool entryFound = false;
103 
104  for (entry = 0; entry < FW_NUM_ARRAY_ELEMENTS(this->m_entryTable); entry++) {
105  if ((this->m_entryTable[entry].used) and (cmdPkt.getOpCode() == this->m_entryTable[entry].opcode)) {
106  entryFound = true;
107  break;
108  }
109  }
110  if (entryFound and this->isConnected_compCmdSend_OutputPort(this->m_entryTable[entry].port)) {
111  // register command in command tracker only if response port is connect
112  if (this->isConnected_seqCmdStatus_OutputPort(portNum)) {
113  bool pendingFound = false;
114 
115  for (U32 pending = 0; pending < FW_NUM_ARRAY_ELEMENTS(this->m_sequenceTracker); pending++) {
116  if (not this->m_sequenceTracker[pending].used) {
117  pendingFound = true;
118  this->m_sequenceTracker[pending].used = true;
119  this->m_sequenceTracker[pending].opCode = cmdPkt.getOpCode();
120  this->m_sequenceTracker[pending].seq = this->m_seq;
121  this->m_sequenceTracker[pending].context = context;
122  this->m_sequenceTracker[pending].callerPort = portNum;
123  break;
124  }
125  }
126 
127  // if we couldn't find a slot to track the command, quit
128  if (not pendingFound) {
130  if (this->isConnected_seqCmdStatus_OutputPort(portNum)) {
131  this->seqCmdStatus_out(portNum, cmdPkt.getOpCode(), context, Fw::CmdResponse::EXECUTION_ERROR);
132  }
133  return;
134  }
135  } // end if status port connected
136  // pass arguments to argument buffer
137  this->compCmdSend_out(this->m_entryTable[entry].port, cmdPkt.getOpCode(), this->m_seq, cmdPkt.getArgBuffer());
138  // log dispatched command
139  this->log_COMMAND_OpCodeDispatched(cmdPkt.getOpCode(), this->m_entryTable[entry].port);
140 
141  // increment command count
142  this->m_numCmdsDispatched++;
143  // write telemetry channel for dispatched commands
144  this->tlmWrite_CommandsDispatched(this->m_numCmdsDispatched);
145  } else {
147  this->m_numCmdErrors++;
148  // Fail command back to port, if connected
149  if (this->isConnected_seqCmdStatus_OutputPort(portNum)) {
150  this->seqCmdStatus_out(portNum, cmdPkt.getOpCode(), context, Fw::CmdResponse::INVALID_OPCODE);
151  }
152  this->tlmWrite_CommandErrors(this->m_numCmdErrors);
153  }
154 
155  // increment sequence number
156  this->m_seq++;
157 }
158 
159 void CommandDispatcherImpl::CMD_NO_OP_cmdHandler(FwOpcodeType opCode, U32 cmdSeq) {
160  Fw::LogStringArg no_op_string("Hello, World!");
161  // Log event for NO_OP here.
163  this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
164 }
165 
166 void CommandDispatcherImpl::CMD_NO_OP_STRING_cmdHandler(FwOpcodeType opCode, U32 cmdSeq, const Fw::CmdStringArg& arg1) {
167  Fw::LogStringArg msg(arg1.toChar());
168  // Echo the NO_OP_STRING args here.
170  this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
171 }
172 
173 void CommandDispatcherImpl::CMD_TEST_CMD_1_cmdHandler(FwOpcodeType opCode, U32 cmdSeq, I32 arg1, F32 arg2, U8 arg3) {
174  this->log_ACTIVITY_HI_TestCmd1Args(arg1, arg2, arg3);
175  this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
176 }
177 
178 void CommandDispatcherImpl::CMD_CLEAR_TRACKING_cmdHandler(FwOpcodeType opCode, U32 cmdSeq) {
179  // clear tracking table
180  for (FwOpcodeType entry = 0; entry < CMD_DISPATCHER_SEQUENCER_TABLE_SIZE; entry++) {
181  this->m_sequenceTracker[entry].used = false;
182  }
183  this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
184 }
185 
186 void CommandDispatcherImpl::pingIn_handler(FwIndexType portNum, U32 key) {
187  // respond to ping
188  this->pingOut_out(0, key);
189 }
190 
191 } // namespace Svc
Serialization/Deserialization operation was successful.
SerializeStatus deserialize(SerializeBufferBase &buffer)
FwIdType FwOpcodeType
The type of a command opcode.
Invalid opcode dispatched.
Deserialization status.
void log_ACTIVITY_HI_TestCmd1Args(I32 arg1, F32 arg2, U8 arg3) const
void pingOut_out(FwIndexType portNum, U32 key)
Invoke output port pingOut.
Enum representing a command response.
void compCmdSend_out(FwIndexType portNum, FwOpcodeType opCode, U32 cmdSeq, Fw::CmdArgBuffer &args)
Invoke output port compCmdSend.
void log_DIAGNOSTIC_OpCodeReregistered(FwOpcodeType Opcode, I32 port) const
SerializeStatus
forward declaration for string
float F32
32-bit floating point
Definition: BasicTypes.h:83
void log_COMMAND_OpCodeCompleted(FwOpcodeType Opcode) const
void log_ACTIVITY_HI_NoOpStringReceived(const Fw::StringBase &message) const
void tlmWrite_CommandsDispatched(U32 arg, Fw::Time _tlmTime=Fw::Time())
void log_WARNING_HI_MalformedCommand(Fw::DeserialStatus Status) const
T e
The raw enum value.
Component responsible for dispatching incoming commands to registered components. ...
virtual ~CommandDispatcherImpl()
Component destructor.
void tlmWrite_CommandErrors(U32 arg, Fw::Time _tlmTime=Fw::Time())
CommandDispatcherImpl(const char *name)
Command Dispatcher constructor.
FwOpcodeType getOpCode() const
Definition: CmdPacket.cpp:52
void log_WARNING_HI_InvalidCommand(FwOpcodeType Opcode) const
Command successfully executed.
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:53
void log_COMMAND_OpCodeError(FwOpcodeType Opcode, Fw::CmdResponse error) const
CmdArgBuffer & getArgBuffer()
Definition: CmdPacket.cpp:56
Command had execution error.
const char * toChar() const
Definition: CmdString.hpp:50
void seqCmdStatus_out(FwIndexType portNum, FwOpcodeType opCode, U32 cmdSeq, const Fw::CmdResponse &response)
Invoke output port seqCmdStatus.
PlatformIndexType FwIndexType
void log_DIAGNOSTIC_OpCodeRegistered(FwOpcodeType Opcode, I32 port, I32 slot) const
Log event OpCodeRegistered.
#define FW_NUM_ARRAY_ELEMENTS(a)
number of elements in an array
Definition: BasicTypes.h:90
Auto-generated base for CommandDispatcher component.
bool isConnected_seqCmdStatus_OutputPort(FwIndexType portNum)
Command failed validation.
RateGroupDivider component implementation.
void log_WARNING_HI_TooManyCommands(FwOpcodeType Opcode) const
bool isConnected_compCmdSend_OutputPort(FwIndexType portNum)
#define FW_ASSERT(...)
Definition: Assert.hpp:14
void cmdResponse_out(FwOpcodeType opCode, U32 cmdSeq, Fw::CmdResponse response)
Emit command response.
void log_COMMAND_OpCodeDispatched(FwOpcodeType Opcode, I32 port) const