F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
CmdSequencerImpl.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title CmdSequencerImpl.hpp
3 // \author Bocchino/Canham
4 // \brief hpp file for CmdSequencer component implementation class
5 //
6 // Copyright (C) 2009-2018 California Institute of Technology.
7 // ALL RIGHTS RESERVED. United States Government Sponsorship
8 // acknowledged.
9 // ======================================================================
10 
11 #ifndef Svc_CmdSequencerImpl_HPP
12 #define Svc_CmdSequencerImpl_HPP
13 
14 #include <Utils/Hash/Hash.hpp>
15 #include "Fw/Com/ComBuffer.hpp"
17 #include "Os/File.hpp"
18 #include "Os/ValidateFile.hpp"
21 
22 namespace Svc {
23 
24 // Forward declaration for UTs
25 namespace ImmediateBase {
26 class CmdSequencerTester;
27 }
28 namespace Immediate {
29 class CmdSequencerTester;
30 }
31 namespace ImmediateEOS {
32 class CmdSequencerTester;
33 }
34 namespace Mixed {
35 class CmdSequencerTester;
36 }
37 namespace MixedRelativeBase {
38 class CmdSequencerTester;
39 }
40 namespace Relative {
41 class CmdSequencerTester;
42 }
43 namespace JoinWait {
44 class CmdSequencerTester;
45 }
46 
48  friend class CmdSequencerTester;
56 
57  private:
58  // ----------------------------------------------------------------------
59  // Private enumerations
60  // ----------------------------------------------------------------------
61 
63  enum RunMode { STOPPED, RUNNING };
64 
66  enum StepMode { AUTO, MANUAL };
67 
68  public:
69  // ----------------------------------------------------------------------
70  // Public classes
71  // ----------------------------------------------------------------------
72 
75  class Sequence {
76  public:
79  class Events {
80  public:
82  Events(Sequence& sequence
83  );
84 
85  public:
87  void fileCRCFailure(const U32 storedCRC,
88  const U32 computedCRC
89  );
90 
93  const I32 error
94  );
95 
97  void fileNotFound();
98 
100  void fileReadError();
101 
103  void fileSizeError(const U32 size
104  );
105 
107  void recordInvalid(const U32 recordNumber,
108  const I32 error
109  );
110 
112  void recordMismatch(const U32 numRecords,
113  const U32 extraBytes
114  );
115 
117  void timeBaseMismatch(const TimeBase currTimeBase,
118  const TimeBase seqTimeBase
119  );
120 
122  void timeContextMismatch(const FwTimeContextStoreType currTimeContext,
123  const FwTimeContextStoreType seqTimeContext
124  );
125 
126  // No Records
127  void noRecords();
128 
129  private:
131  Sequence& m_sequence;
132  };
133 
134  public:
137  );
138 
140  virtual ~Sequence();
141 
142  public:
145  class Header {
146  public:
147  enum Constants {
150  sizeof(U32) + sizeof(U32) + sizeof(FwTimeBaseStoreType) + sizeof(FwTimeContextStoreType)
151  };
152 
153  public:
155  Header();
156 
157  public:
160  bool validateTime(CmdSequencerComponentImpl& component
161  );
162 
163  public:
166 
169 
172 
175  };
176 
177  public:
180  class Record {
181  public:
182  enum Descriptor {
186  };
187 
188  public:
191 
192  public:
195 
198 
201  };
202 
203  public:
205  void allocateBuffer(FwEnumStoreType identifier,
206  Fw::MemAllocator& allocator,
207  FwSizeType bytes
208  );
209 
211  void deallocateBuffer(Fw::MemAllocator& allocator
212  );
213 
215  void setFileName(const Fw::ConstStringBase& fileName);
216 
220 
224 
228 
230  const Header& getHeader() const;
231 
234  virtual bool loadFile(const Fw::ConstStringBase& fileName
235  ) = 0;
236 
239  virtual bool hasMoreRecords() const = 0;
240 
243  virtual void nextRecord(Record& record
244  ) = 0;
245 
249  virtual void reset() = 0;
250 
253  virtual void clear() = 0;
254 
255  protected:
258 
261 
264 
267 
270 
273 
276 
279  };
280 
283  class FPrimeSequence : public Sequence {
284  private:
285  enum Constants { INITIAL_COMPUTED_VALUE = 0xFFFFFFFFU };
286 
287  public:
290  struct CRC {
292  CRC();
293 
295  void init();
296 
298  void update(const BYTE* buffer,
299  FwSizeType bufferSize
300  );
301 
303  U32 finalize();
304 
307 
309  U32 m_stored;
310  };
311 
312  public:
315  );
316 
317  public:
320  bool loadFile(const Fw::ConstStringBase& fileName
321  );
322 
325  bool hasMoreRecords() const;
326 
329  void nextRecord(Record& record
330  );
331 
335  void reset();
336 
339  void clear();
340 
341  private:
344  bool readFile();
345 
348  bool readOpenFile();
349 
353  bool readHeader();
354 
357  bool deserializeHeader();
358 
361  bool readRecordsAndCRC();
362 
365  bool extractCRC();
366 
369  bool validateCRC();
370 
373  Fw::SerializeStatus deserializeRecord(Record& record
374  );
375 
378  Fw::SerializeStatus deserializeDescriptor(Record::Descriptor& descriptor
379  );
380 
383  Fw::SerializeStatus deserializeTimeTag(Fw::Time& timeTag
384  );
385 
388  Fw::SerializeStatus deserializeRecordSize(U32& recordSize
389  );
390 
393  Fw::SerializeStatus copyCommand(Fw::ComBuffer& comBuffer,
394  const U32 recordSize
395  );
396 
399  bool validateRecords();
400 
401  private:
403  CRC m_crc;
404 
406  Os::File m_sequenceFile;
407  };
408 
409  private:
410  // ----------------------------------------------------------------------
411  // Private classes
412  // ----------------------------------------------------------------------
413 
416  class Timer {
417  friend class CmdSequencerTester;
418  friend class Svc::ImmediateBase::CmdSequencerTester;
419  friend class Svc::Immediate::CmdSequencerTester;
420  friend class Svc::ImmediateEOS::CmdSequencerTester;
421  friend class Svc::Mixed::CmdSequencerTester;
422  friend class Svc::MixedRelativeBase::CmdSequencerTester;
423  friend class Svc::Relative::CmdSequencerTester;
424  friend class Svc::JoinWait::CmdSequencerTester;
425 
426  private:
428  typedef enum { SET, CLEAR } State;
429 
430  public:
432  Timer() : m_state(CLEAR) {}
433 
435  void set(Fw::Time time
436  ) {
437  this->m_state = SET;
438  this->expirationTime = time;
439  }
440 
442  void clear() { this->m_state = CLEAR; }
443 
446  bool isExpiredAt(Fw::Time time
447  ) {
448  if (this->m_state == CLEAR) {
449  return false;
450  } else if (Fw::Time::compare(this->expirationTime, time) == Fw::TimeComparison::GT) {
451  return false;
452  }
453  return true;
454  }
455 
456  private:
458  State m_state;
459 
461  Fw::Time expirationTime;
462  };
463 
464  public:
465  // ----------------------------------------------------------------------
466  // Construction, initialization, and destruction
467  // ----------------------------------------------------------------------
468 
470  CmdSequencerComponentImpl(const char* compName
471  );
472 
476  void setTimeout(const U32 seconds
477  );
478 
483  void setSequenceFormat(Sequence& sequence
484  );
485 
489  void allocateBuffer(const FwEnumStoreType identifier,
490  Fw::MemAllocator& allocator,
491  const FwSizeType bytes
492  );
493 
496  void loadSequence(const Fw::ConstStringBase& fileName
497  );
498 
500  void deallocateBuffer(Fw::MemAllocator& allocator
501  );
502 
505 
506  private:
507  // ----------------------------------------------------------------------
508  // Handler implementations for input ports
509  // ----------------------------------------------------------------------
510 
512  void cmdResponseIn_handler(FwIndexType portNum,
513  FwOpcodeType opcode,
514  U32 cmdSeq,
515  const Fw::CmdResponse& response
516  ) override;
517 
519  void schedIn_handler(FwIndexType portNum,
520  U32 order
521  ) override;
522 
524  void seqRunIn_handler(FwIndexType portNum,
525  const Fw::StringBase& filename,
526  const Svc::SeqArgs& args
527  ) override;
528 
532  void seqDispatchIn_handler(FwIndexType portNum,
533  Fw::StringBase& file_name
534  ) override;
535 
537  void pingIn_handler(FwIndexType portNum,
538  U32 key
539  ) override;
540 
543  void seqCancelIn_handler(const FwIndexType portNum
544  ) override;
545 
546  private:
547  // ----------------------------------------------------------------------
548  // Command handler implementations
549  // ----------------------------------------------------------------------
550 
553  void CS_AUTO_cmdHandler(FwOpcodeType opcode,
554  U32 cmdSeq
555  ) override;
556 
559  void CS_CANCEL_cmdHandler(FwOpcodeType opCode,
560  U32 cmdSeq
561  ) override;
562 
565  void CS_MANUAL_cmdHandler(FwOpcodeType opcode,
566  U32 cmdSeq
567  ) override;
568 
570  void CS_RUN_cmdHandler(FwOpcodeType opCode,
571  U32 cmdSeq,
572  const Fw::CmdStringArg& fileName,
573  Svc::BlockState block
574  ) override;
575 
578  void CS_START_cmdHandler(FwOpcodeType opcode,
579  U32 cmdSeq
580  ) override;
581 
585  void CS_STEP_cmdHandler(FwOpcodeType opcode,
586  U32 cmdSeq
587  ) override;
588 
591  void CS_VALIDATE_cmdHandler(FwOpcodeType opCode,
592  U32 cmdSeq,
593  const Fw::CmdStringArg& fileName
594  ) override;
595 
600  void CS_JOIN_WAIT_cmdHandler(const FwOpcodeType opCode,
601  const U32 cmdSeq
602  ) override;
603 
604  private:
605  // ----------------------------------------------------------------------
606  // Private helper methods
607  // ----------------------------------------------------------------------
608 
611  bool loadFile(const Fw::ConstStringBase& fileName
612  );
613 
615  void performCmd_Cancel();
616 
618  void performCmd_Step();
619 
621  void performCmd_Step_RELATIVE(Fw::Time& currentTime
622  );
623 
625  void performCmd_Step_ABSOLUTE(Fw::Time& currentTime
626  );
627 
629  void commandComplete(const FwOpcodeType opCode
630  );
631 
633  void sequenceComplete();
634 
636  void error();
637 
639  void commandError(const U32 number,
640  const FwOpcodeType opCode,
641  const U32 error
642  );
643 
646  bool requireRunMode(RunMode mode
647  );
648 
650  void setCmdTimeout(const Fw::Time& currentTime
651  );
652 
654  void doSequenceRun(const Fw::StringBase& fileName);
655 
656  private:
657  // ----------------------------------------------------------------------
658  // Private member variables
659  // ----------------------------------------------------------------------
660 
662  FPrimeSequence m_FPrimeSequence;
663 
665  Sequence* m_sequence;
666 
668  U32 m_loadCmdCount;
669 
671  U32 m_cancelCmdCount;
672 
674  U32 m_errorCount;
675 
677  RunMode m_runMode;
678 
680  StepMode m_stepMode;
681 
683  Sequence::Record m_record;
684 
686  Timer m_cmdTimer;
687 
689  U32 m_executedCount;
690 
692  U32 m_totalExecutedCount;
693 
695  U32 m_sequencesCompletedCount;
696 
698  U32 m_timeout;
699 
701  Timer m_cmdTimeoutTimer;
702 
704  Svc::BlockState::t m_blockState;
705  FwOpcodeType m_opCode;
706  U32 m_cmdSeq;
707  bool m_join_waiting;
708 };
709 
710 } // namespace Svc
711 
712 #endif
void loadSequence(const Fw::ConstStringBase &fileName)
friend class Svc::Mixed::CmdSequencerTester
void recordMismatch(const U32 numRecords, const U32 extraBytes)
Record mismatch.
Definition: Events.cpp:61
void setSequenceFormat(Sequence &sequence)
FwIdType FwOpcodeType
The type of a command opcode.
TimeBase m_timeBase
The time base of the sequence.
PlatformSizeType FwSizeType
Events(Sequence &sequence)
Construct an Events object.
Definition: Events.cpp:16
I32 FwEnumStoreType
CmdSequencerComponentImpl(const char *compName)
Construct a CmdSequencer.
friend class Svc::ImmediateBase::CmdSequencerTester
enum T t
For backwards compatibility.
U32 m_numRecords
The number of records in the sequence.
Fw::Time m_timeTag
The time tag. NOTE: timeBase and context not filled in.
Enum representing a command response.
friend class Svc::JoinWait::CmdSequencerTester
U8 FwTimeContextStoreType
The type used to serialize a time context value.
void recordInvalid(const U32 recordNumber, const I32 error)
Record invalid.
Definition: Events.cpp:54
void allocateBuffer(FwEnumStoreType identifier, Fw::MemAllocator &allocator, FwSizeType bytes)
Give the sequence representation a memory buffer.
Definition: Sequence.cpp:45
SerializeStatus
forward declaration for string
void timeContextMismatch(const FwTimeContextStoreType currTimeContext, const FwTimeContextStoreType seqTimeContext)
Time context mismatch.
Definition: Events.cpp:76
virtual bool hasMoreRecords() const =0
friend class Svc::Immediate::CmdSequencerTester
Fw::ExternalSerializeBuffer m_buffer
Serialize buffer to hold the binary sequence data.
~CmdSequencerComponentImpl()
Destroy a CmdDispatcherComponentBase.
Fw::CmdStringArg m_fileName
The sequence file name.
Container for computed and stored CRC values.
FwTimeContextStoreType m_timeContext
The context of the sequence.
Fw::LogStringArg m_logFileName
Copy of file name for events.
FwEnumStoreType m_allocatorId
The allocator ID.
void allocateBuffer(const FwEnumStoreType identifier, Fw::MemAllocator &allocator, const FwSizeType bytes)
A sequence with unspecified binary format.
void timeBaseMismatch(const TimeBase currTimeBase, const TimeBase seqTimeBase)
Time base mismatch.
Definition: Events.cpp:68
bool loadFile(const Fw::ConstStringBase &fileName)
void deallocateBuffer(Fw::MemAllocator &allocator)
Deallocate the buffer.
Definition: Sequence.cpp:55
void update(const BYTE *buffer, FwSizeType bufferSize)
Update computed CRC.
void fileInvalid(const CmdSequencer_FileReadStage::t stage, const I32 error)
File invalid.
Definition: Events.cpp:25
External serialize buffer with no copy semantics.
FpySequencer_SequencerStateMachineStateMachineBase::State State
Sequencer blocking state.
friend class Svc::ImmediateEOS::CmdSequencerTester
FPrimeSequence(CmdSequencerComponentImpl &component)
Construct an FPrimeSequence.
A generic interface for creating and comparing hash values.
Definition: Hash.hpp:24
void setTimeout(const U32 seconds)
void fileSizeError(const U32 size)
File size error.
Definition: Events.cpp:47
void setFileName(const Fw::ConstStringBase &fileName)
Set the file name. Also sets the log file name.
Definition: Sequence.cpp:64
virtual bool loadFile(const Fw::ConstStringBase &fileName)=0
Sequence(CmdSequencerComponentImpl &component)
Construct a Sequence object.
Definition: Sequence.cpp:16
friend class Svc::MixedRelativeBase::CmdSequencerTester
static TimeComparison compare(const Time &time1, const Time &time2)
Definition: Time.cpp:139
void deallocateBuffer(Fw::MemAllocator &allocator)
Return allocated buffer. Call during shutdown.
Memory Allocation base class.
A sequence that uses the F Prime binary format.
virtual ~Sequence()
Destroy a Sequence object.
Definition: Sequence.cpp:19
enum T t
For backwards compatibility.
Defines a file class to validate files or generate a file validator file.
A read-only abstract superclass for StringBase.
void fileCRCFailure(const U32 storedCRC, const U32 computedCRC)
File CRC failure.
Definition: Events.cpp:18
PlatformIndexType FwIndexType
CmdSequencerComponentImpl & m_component
The enclosing component.
Auto-generated base for CmdSequencer component.
RateGroupDivider component implementation.
virtual void nextRecord(Record &record)=0
friend class Svc::Relative::CmdSequencerTester
U8 BYTE
byte type
Definition: BasicTypes.h:57
Defines a base class for a memory allocator for classes.
U16 FwTimeBaseStoreType
The type used to serialize a time base value.
Define enumeration for Time base types.
bool validateTime(CmdSequencerComponentImpl &component)
Definition: Sequence.cpp:24
Fw::String m_stringFileName
Copy of file name for ports.
const Header & getHeader() const
Get the sequence header.
Definition: Sequence.cpp:60