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), m_numCmdsDropped(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  FW_ASSERT(response.e != Fw::CmdResponse::OK);
59  this->log_COMMAND_OpCodeError(opCode, response);
60  }
61  // look for command source
62  FwIndexType portToCall = -1;
63  U32 context;
64  for (U32 pending = 0; pending < FW_NUM_ARRAY_ELEMENTS(this->m_sequenceTracker); pending++) {
65  if ((this->m_sequenceTracker[pending].seq == cmdSeq) && (this->m_sequenceTracker[pending].used)) {
66  portToCall = this->m_sequenceTracker[pending].callerPort;
67  context = this->m_sequenceTracker[pending].context;
68  FW_ASSERT(opCode == this->m_sequenceTracker[pending].opCode);
69  FW_ASSERT(portToCall < this->getNum_seqCmdStatus_OutputPorts());
70  this->m_sequenceTracker[pending].used = false;
71  break;
72  }
73  }
74 
75  if (portToCall != -1) {
76  // call port to report status
77  if (this->isConnected_seqCmdStatus_OutputPort(portToCall)) {
78  // NOTE: seqCmdStatus port forwards three arguments: (opCode, cmdSeq, response).
79  // However, the cmdSeq value has no meaning for the calling sequencer.
80  // Instead, the context value is forwarded to allow the caller to utilize it if needed.
81  this->seqCmdStatus_out(portToCall, opCode, context, response);
82  }
83  }
84 }
85 
86 void CommandDispatcherImpl::seqCmdBuff_handler(FwIndexType portNum, Fw::ComBuffer& data, U32 context) {
87  Fw::CmdPacket cmdPkt;
88  Fw::SerializeStatus stat = cmdPkt.deserializeFrom(data);
89 
90  if (stat != Fw::FW_SERIALIZE_OK) {
91  Fw::DeserialStatus serErr(static_cast<Fw::DeserialStatus::t>(stat));
92  this->log_WARNING_HI_MalformedCommand(serErr);
93  if (this->isConnected_seqCmdStatus_OutputPort(portNum)) {
94  this->seqCmdStatus_out(portNum, cmdPkt.getOpCode(), context, Fw::CmdResponse::VALIDATION_ERROR);
95  }
96  return;
97  }
98 
99  // search for opcode in dispatch table
100  FwOpcodeType entry;
101  bool entryFound = false;
102 
103  for (entry = 0; entry < FW_NUM_ARRAY_ELEMENTS(this->m_entryTable); entry++) {
104  if ((this->m_entryTable[entry].used) and (cmdPkt.getOpCode() == this->m_entryTable[entry].opcode)) {
105  entryFound = true;
106  break;
107  }
108  }
109  if (entryFound and this->isConnected_compCmdSend_OutputPort(this->m_entryTable[entry].port)) {
110  // register command in command tracker only if response port is connect
111  if (this->isConnected_seqCmdStatus_OutputPort(portNum)) {
112  bool pendingFound = false;
113 
114  for (U32 pending = 0; pending < FW_NUM_ARRAY_ELEMENTS(this->m_sequenceTracker); pending++) {
115  if (not this->m_sequenceTracker[pending].used) {
116  pendingFound = true;
117  this->m_sequenceTracker[pending].used = true;
118  this->m_sequenceTracker[pending].opCode = cmdPkt.getOpCode();
119  this->m_sequenceTracker[pending].seq = this->m_seq;
120  this->m_sequenceTracker[pending].context = context;
121  this->m_sequenceTracker[pending].callerPort = portNum;
122  break;
123  }
124  }
125 
126  // if we couldn't find a slot to track the command, quit
127  if (not pendingFound) {
129  if (this->isConnected_seqCmdStatus_OutputPort(portNum)) {
130  this->seqCmdStatus_out(portNum, cmdPkt.getOpCode(), context, Fw::CmdResponse::EXECUTION_ERROR);
131  }
132  return;
133  }
134  } // end if status port connected
135  // pass arguments to argument buffer
136  this->compCmdSend_out(this->m_entryTable[entry].port, cmdPkt.getOpCode(), this->m_seq, cmdPkt.getArgBuffer());
137  // log dispatched command
138  this->log_COMMAND_OpCodeDispatched(cmdPkt.getOpCode(), this->m_entryTable[entry].port);
139 
140  // increment command count
141  this->m_numCmdsDispatched++;
142  } else {
144  this->m_numCmdErrors++;
145  // Fail command back to port, if connected
146  if (this->isConnected_seqCmdStatus_OutputPort(portNum)) {
147  this->seqCmdStatus_out(portNum, cmdPkt.getOpCode(), context, Fw::CmdResponse::INVALID_OPCODE);
148  }
149  }
150 
151  // increment sequence number
152  this->m_seq++;
153 }
154 
155 void CommandDispatcherImpl ::run_handler(FwIndexType portNum, U32 context) {
156  this->tlmWrite_CommandsDropped(this->m_numCmdsDropped);
157  this->tlmWrite_CommandErrors(this->m_numCmdErrors);
158  this->tlmWrite_CommandsDispatched(this->m_numCmdsDispatched);
159 }
160 
161 void CommandDispatcherImpl::CMD_NO_OP_cmdHandler(FwOpcodeType opCode, U32 cmdSeq) {
162  Fw::LogStringArg no_op_string("Hello, World!");
163  // Log event for NO_OP here.
165  this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
166 }
167 
168 void CommandDispatcherImpl::CMD_NO_OP_STRING_cmdHandler(FwOpcodeType opCode, U32 cmdSeq, const Fw::CmdStringArg& arg1) {
169  Fw::LogStringArg msg(arg1.toChar());
170  // Echo the NO_OP_STRING args here.
172  this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
173 }
174 
175 void CommandDispatcherImpl::CMD_TEST_CMD_1_cmdHandler(FwOpcodeType opCode, U32 cmdSeq, I32 arg1, F32 arg2, U8 arg3) {
176  this->log_ACTIVITY_HI_TestCmd1Args(arg1, arg2, arg3);
177  this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
178 }
179 
180 void CommandDispatcherImpl::CMD_CLEAR_TRACKING_cmdHandler(FwOpcodeType opCode, U32 cmdSeq) {
181  // clear tracking table
182  for (FwOpcodeType entry = 0; entry < CMD_DISPATCHER_SEQUENCER_TABLE_SIZE; entry++) {
183  this->m_sequenceTracker[entry].used = false;
184  }
185  this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
186 }
187 
188 void CommandDispatcherImpl::pingIn_handler(FwIndexType portNum, U32 key) {
189  // respond to ping
190  this->pingOut_out(0, key);
191 }
192 
193 void CommandDispatcherImpl::seqCmdBuff_overflowHook(FwIndexType portNum, Fw::ComBuffer& data, U32 context) {
194  // Extract command opcode
195  Fw::CmdPacket cmdPkt;
196  Fw::SerializeStatus stat = cmdPkt.deserializeFrom(data);
197  FwOpcodeType opcode = 0; // Note: 0 = Reserved opcode
198 
199  if (stat == Fw::FW_SERIALIZE_OK) {
200  opcode = cmdPkt.getOpCode();
201  }
202 
203  // Log Cmd Buffer Overflow and increment CommandsDroppedBufOverflow counter
204  this->m_numCmdsDropped++;
205  this->log_WARNING_HI_CommandDroppedQueueOverflow(opcode, context);
206 }
207 
208 } // namespace Svc
Serialization/Deserialization operation was successful.
FwIdType FwOpcodeType
The type of a command opcode.
void log_WARNING_HI_CommandDroppedQueueOverflow(FwOpcodeType OpCode, U32 Context)
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
void tlmWrite_CommandsDropped(U32 arg, Fw::Time _tlmTime=Fw::Time())
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.
SerializeStatus deserializeFrom(SerializeBufferBase &buffer)
deserialize contents from buffer
Definition: CmdPacket.cpp:27
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