F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
FpySequencer.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title FpySequencer.hpp
3 // \author zimri.leisher
4 // \brief hpp file for FpySequencer component implementation class
5 // ======================================================================
6 
7 #ifndef FpySequencer_HPP
8 #define FpySequencer_HPP
9 
10 #include "FppConstantsAc.hpp"
12 #include "Fw/Types/StringBase.hpp"
13 #include "Fw/Types/WaitEnumAc.hpp"
15 #include "Os/File.hpp"
23 
24 static_assert(Svc::Fpy::MAX_SEQUENCE_ARG_COUNT <= std::numeric_limits<U8>::max(), "Sequence arg count must be below U8 max");
25 static_assert(Svc::Fpy::MAX_SEQUENCE_STATEMENT_COUNT <= std::numeric_limits<U16>::max(), "Sequence statement count must be below U16 max");
26 
27 namespace Svc {
28 
31 
33  public:
34  // ----------------------------------------------------------------------
35  // Construction, initialization, and destruction
36  // ----------------------------------------------------------------------
37 
40  FpySequencer(const char* const compName
41  );
42 
45  ~FpySequencer();
46 
47  PRIVATE :
48 
52  void
53  RUN_cmdHandler(FwOpcodeType opCode,
54  U32 cmdSeq,
55  const Fw::CmdStringArg& fileName,
57  ) override;
58 
62  void VALIDATE_cmdHandler(FwOpcodeType opCode,
63  U32 cmdSeq,
64  const Fw::CmdStringArg& fileName
65  ) override;
66 
70  void RUN_VALIDATED_cmdHandler(FwOpcodeType opCode,
71  U32 cmdSeq,
73  ) override;
74 
78  void CANCEL_cmdHandler(FwOpcodeType opCode,
79  U32 cmdSeq
80  ) override;
81 
82  // ----------------------------------------------------------------------
83  // Functions to implement for internal state machine actions
84  // ----------------------------------------------------------------------
85 
89  void Svc_FpySequencer_SequencerStateMachine_action_signalEntered(
90  SmId smId,
92  ) override;
93 
97  void Svc_FpySequencer_SequencerStateMachine_action_setSequenceFilePath(
98  SmId smId,
101  ) override;
102 
106  void Svc_FpySequencer_SequencerStateMachine_action_setSequenceBlockState(
107  SmId smId,
110  ) override;
111 
115  void Svc_FpySequencer_SequencerStateMachine_action_validate(
116  SmId smId,
118  ) override;
119 
123  void Svc_FpySequencer_SequencerStateMachine_action_report_seqSucceeded(
124  SmId smId,
126  ) override;
127 
131  void Svc_FpySequencer_SequencerStateMachine_action_report_seqCancelled(
132  SmId smId,
134  ) override;
135 
139  void Svc_FpySequencer_SequencerStateMachine_action_setGoalState_RUNNING(
140  SmId smId,
142  ) override;
143 
147  void Svc_FpySequencer_SequencerStateMachine_action_setGoalState_VALID(
148  SmId smId,
150  ) override;
151 
155  void Svc_FpySequencer_SequencerStateMachine_action_setGoalState_IDLE(
156  SmId smId,
158  ) override;
159 
163  void Svc_FpySequencer_SequencerStateMachine_action_sendCmdResponse_OK(
164  SmId smId,
166  ) override;
167 
172  void Svc_FpySequencer_SequencerStateMachine_action_sendCmdResponse_EXECUTION_ERROR(
173  SmId smId,
175  ) override;
176 
180  void Svc_FpySequencer_SequencerStateMachine_action_dispatchStatement(
181  SmId smId,
183  ) override;
184 
188  void Svc_FpySequencer_SequencerStateMachine_action_clearSequenceFile(
189  SmId smId,
191  ) override;
192 
196  void Svc_FpySequencer_SequencerStateMachine_action_checkShouldWake(
197  SmId smId,
199  ) override;
200 
204  void Svc_FpySequencer_SequencerStateMachine_action_resetRuntime(
205  SmId smId,
207  ) override;
208 
212  void Svc_FpySequencer_SequencerStateMachine_action_checkStatementTimeout(
213  SmId smId,
215  ) override;
216 
220  void Svc_FpySequencer_SequencerStateMachine_action_incrementSequenceCounter(
221  SmId smId,
223  ) override;
224 
225  PROTECTED :
226 
227  // ----------------------------------------------------------------------
228  // Functions to implement for internal state machine guards
229  // ----------------------------------------------------------------------
230 
234  bool
236  SmId smId,
238  ) const override;
239 
240  // ----------------------------------------------------------------------
241  // Handlers to implement for typed input ports
242  // ----------------------------------------------------------------------
243 
245  void checkTimers_handler(FwIndexType portNum,
246  U32 context
247  ) override;
248 
250  void cmdResponseIn_handler(FwIndexType portNum,
251  FwOpcodeType opCode,
252  U32 cmdSeq,
253  const Fw::CmdResponse& response
254  ) override;
255 
257  void pingIn_handler(FwIndexType portNum,
258  U32 key
259  ) override;
260 
262  void tlmWrite_handler(FwIndexType portNum,
263  U32 context
264  ) override;
265 
268 
271 
272  void parametersLoaded() override;
273  void parameterUpdated(FwPrmIdType id) override;
274 
275  public:
276  void allocateBuffer(FwEnumStoreType identifier, Fw::MemAllocator& allocator, FwSizeType bytes);
277 
278  void deallocateBuffer(Fw::MemAllocator& allocator);
279  PRIVATE :
280 
281  static constexpr U32 CRC_INITIAL_VALUE = 0xFFFFFFFFU;
282 
283  // allocated at startup
284  Fw::ExternalSerializeBuffer m_sequenceBuffer;
285  // id of allocator that gave us m_sequenceBuffer
286  FwEnumStoreType m_allocatorId;
287 
288  // assigned by the user via cmd
289  Fw::String m_sequenceFilePath;
290  // the sequence, loaded in memory
291  Fpy::Sequence m_sequenceObj;
292  // live running computation of CRC (updated as we read)
293  U32 m_computedCRC;
294 
295  // whether or not the sequence we're about to run should return immediately or
296  // block on completion
297  FpySequencer_BlockState m_sequenceBlockState;
298  // if we are to block on completion, save the opCode and cmdSeq we should
299  // return
300  FwOpcodeType m_savedOpCode;
301  U32 m_savedCmdSeq;
302 
303  // the goal state is the state that we're trying to reach in the sequencer
304  // if it's RUNNING, then we should promptly go to RUNNING once we validate the
305  // sequence. if it's VALID, we should wait after VALIDATING
306  FpySequencer_GoalState m_goalState;
307 
308  // the total number of sequences this sequencer has started since construction
309  U64 m_sequencesStarted;
310  // the total number of statements this sequencer has dispatched, successfully or
311  // otherwise, since construction
312  U64 m_statementsDispatched;
313 
314  // the runtime state of the sequence. encapsulates all state
315  // needed to run the sequence.
316  // this is distinct from the state of the sequencer. the
317  // sequencer and all its state is really just a shell to load
318  // and execute this runtime.
319  struct Runtime {
320  // the index of the next statement to be executed
321  U32 nextStatementIndex = 0;
322 
323  // the opcode of the statement that is currently executing
324  FwOpcodeType currentStatementOpcode = Fpy::DirectiveId::INVALID;
325  // the stmt type of the stmt currently executing (cmd or directive)
326  Fpy::StatementType currentStatementType = Fpy::StatementType::DIRECTIVE;
327  // the time we dispatched the statement that is currently executing
328  Fw::Time currentStatementDispatchTime = Fw::Time();
329 
330  // the absolute time we should wait for until returning
331  // a statement response
332  Fw::Time wakeupTime = Fw::Time();
333  } m_runtime;
334 
335  struct Telemetry {
336  // the number of statements that failed to execute
337  U64 statementsFailed = 0;
338 
339  // the number of sequences successfully completed
340  U64 sequencesSucceeded = 0;
341 
342  // the number of sequences that failed to validate or execute
343  U64 sequencesFailed = 0;
344 
345  // the number of sequences that have been cancelled
346  U64 sequencesCancelled = 0;
347  } m_tlm;
348 
349  // ----------------------------------------------------------------------
350  // Validation state
351  // ----------------------------------------------------------------------
352 
353  void updateComputedCRC(const U8* buffer,
354  FwSizeType bufferSize
355  );
356 
357  // loads the sequence in memory, and does header/crc/integrity checks.
358  // return success if sequence is valid
359  Fw::Success validate();
360 
361  // reads some bytes from the open file into the m_sequenceBuffer.
362  // updates the CRC by default, but can be turned off if the contents
363  // aren't included in CRC.
364  // return success if successful
365  Fw::Success readBytes(Os::File& file, FwSizeType readLen, bool updateCRC = true);
366 
367  // ----------------------------------------------------------------------
368  // Run state
369  // ----------------------------------------------------------------------
370 
371  // dispatches the next statement
372  Signal dispatchStatement();
373 
374  // dispatches a command out via port.
375  // return success if successfully dispatched.
376  Fw::Success dispatchCommand(const Fpy::Statement& stmt);
377 
378  // dispatches a sequencer directive to the right handler.
379  // return success if successfully handled.
380  Fw::Success dispatchDirective(const Fpy::Statement& stmt);
381 
382  // checks whether the currently executing statement timed out
383  Signal checkStatementTimeout();
384  // checks whether the sequencer should wake from sleeping
385  Signal checkShouldWake();
386 
387  // ----------------------------------------------------------------------
388  // Directives
389  // ----------------------------------------------------------------------
390 
391  // sends a signal based on a signal id
392  void sendSignal(Signal signal);
393 
394  // we split these functions up into the internalInterfaceInvoke and these custom member funcs
395  // so that we can unit test them easier
396  Signal waitRel_directiveHandler(const FpySequencer_WaitRelDirective& directive);
397  Signal waitAbs_directiveHandler(const FpySequencer_WaitAbsDirective& directive);
398 };
399 
400 } // namespace Svc
401 
402 #endif
Definition: Time.hpp:9
PlatformSizeType FwSizeType
I32 FwEnumStoreType
bool Svc_FpySequencer_SequencerStateMachine_guard_goalStateIs_RUNNING(SmId smId, Svc_FpySequencer_SequencerStateMachine::Signal signal) const override
void pingIn_handler(FwIndexType portNum, U32 key) override
Handler for input port pingIn.
Enum representing a command response.
void directive_waitRel_internalInterfaceHandler(const FpySequencer_WaitRelDirective &directive) override
Internal interface handler for directive_waitRel.
void parametersLoaded() override
Called whenever parameters are loaded.
U32 FwOpcodeType
The type of a command opcode.
void tlmWrite_handler(FwIndexType portNum, U32 context) override
Handler for input port tlmWrite.
External serialize buffer with no copy semantics.
void directive_waitAbs_internalInterfaceHandler(const FpySequencer_WaitAbsDirective &directive) override
Internal interface handler for directive_waitAbs.
void parameterUpdated(FwPrmIdType id) override
Called whenever a parameter is updated.
void allocateBuffer(FwEnumStoreType identifier, Fw::MemAllocator &allocator, FwSizeType bytes)
U32 FwPrmIdType
The type of a parameter identifier.
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:56
void cmdResponseIn_handler(FwIndexType portNum, FwOpcodeType opCode, U32 cmdSeq, const Fw::CmdResponse &response) override
Handler for input port cmdResponseIn.
void checkTimers_handler(FwIndexType portNum, U32 context) override
Handler for input port checkTimers.
PlatformIndexType FwIndexType
RateGroupDivider component implementation.
Defines a base class for a memory allocator for classes.
Declares F Prime string base class.
void deallocateBuffer(Fw::MemAllocator &allocator)
Auto-generated base for FpySequencer component.
Success/Failure.
FpySequencer(const char *const compName)
#define U64(C)
Definition: sha.h:180