F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
FpySequencerValidationState.cpp
Go to the documentation of this file.
1 #include <Utils/Hash/Hash.hpp>
4 
5 namespace Svc {
6 
8  // if this assertion fails, you aren't allocating enough bytes for the
9  // FpySequencer. this is because you must have a buffer big enough to fit the
10  // header of a sequence
11  FW_ASSERT(bytes >= Fpy::Header::SERIALIZED_SIZE, static_cast<FwAssertArgType>(bytes));
12  FwSizeType originalBytes = bytes;
13  bool recoverable = false;
14  this->m_allocatorId = identifier;
15  U8* allocatedMemory = static_cast<U8*>(allocator.allocate(identifier, bytes, recoverable));
16  // if this fails, unable to allocate the requested amount of money
17  FW_ASSERT(bytes >= originalBytes, static_cast<FwAssertArgType>(bytes));
18  this->m_sequenceBuffer.setExtBuffer(allocatedMemory, bytes);
19 }
20 
22  allocator.deallocate(this->m_allocatorId, this->m_sequenceBuffer.getBuffAddr());
23  this->m_sequenceBuffer.clear();
24 }
25 
26 // loads the sequence in memory, and does header/crc/integrity checks.
27 // return SUCCESS if sequence is valid, FAILURE otherwise
28 Fw::Success FpySequencer::validate() {
29  FW_ASSERT(this->m_sequenceFilePath.length() > 0);
30 
31  // crc needs to be initialized with a particular value
32  // for the calculation to work
33  this->m_computedCRC.init();
34 
35  Os::File sequenceFile;
36  Os::File::Status openStatus = sequenceFile.open(this->m_sequenceFilePath.toChar(), Os::File::OPEN_READ);
37 
38  if (openStatus != Os::File::Status::OP_OK) {
39  this->log_WARNING_HI_FileOpenError(this->m_sequenceFilePath, static_cast<I32>(openStatus));
40  return Fw::Success::FAILURE;
41  }
42 
43  Fw::Success readStatus =
45 
46  if (readStatus != Fw::Success::SUCCESS) {
47  return Fw::Success::FAILURE;
48  }
49 
50  readStatus = this->readHeader();
51 
52  if (readStatus != Fw::Success::SUCCESS) {
53  return Fw::Success::FAILURE;
54  }
55 
56  readStatus =
57  readBytes(sequenceFile, this->m_sequenceObj.get_header().get_bodySize(), FpySequencer_FileReadStage::BODY);
58 
59  if (readStatus != Fw::Success::SUCCESS) {
60  return Fw::Success::FAILURE;
61  }
62 
63  readStatus = this->readBody();
64 
65  if (readStatus != Fw::Success::SUCCESS) {
66  return Fw::Success::FAILURE;
67  }
68 
69  // read footer bytes but don't include in CRC
70  readStatus = this->readBytes(sequenceFile, Fpy::Footer::SERIALIZED_SIZE, FpySequencer_FileReadStage::FOOTER, false);
71 
72  if (readStatus != Fw::Success::SUCCESS) {
73  return Fw::Success::FAILURE;
74  }
75 
76  readStatus = this->readFooter();
77 
78  if (readStatus != Fw::Success::SUCCESS) {
79  return Fw::Success::FAILURE;
80  }
81 
82  // make sure we're at EOF. The size() and position() OS calls can fail
83  // on filesystem errors or if the file was concurrently modified by
84  // another FileManager command (e.g. RemoveFile or AppendFile) between
85  // the file open and this point. Treat the failure as a validation
86  // failure rather than aborting the FSW process.
87  FwSizeType sequenceFileSize;
88  Os::File::Status sizeStatus = sequenceFile.size(sequenceFileSize);
89  if (sizeStatus != Os::File::Status::OP_OK) {
90  this->log_WARNING_HI_FileApiError(this->m_sequenceFilePath, static_cast<I32>(sizeStatus));
91  return Fw::Success::FAILURE;
92  }
93 
94  FwSizeType sequenceFilePosition;
95  Os::File::Status positionStatus = sequenceFile.position(sequenceFilePosition);
96  if (positionStatus != Os::File::Status::OP_OK) {
97  this->log_WARNING_HI_FileApiError(this->m_sequenceFilePath, static_cast<I32>(positionStatus));
98  return Fw::Success::FAILURE;
99  }
100 
101  if (sequenceFileSize != sequenceFilePosition) {
102  this->log_WARNING_HI_ExtraBytesInSequence(static_cast<FwSizeType>(sequenceFileSize - sequenceFilePosition));
103  return Fw::Success::FAILURE;
104  }
105 
106  Fpy::StackSizeType availableSpace = Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size;
107 
108  if (this->m_sequenceArgs.get_size() > availableSpace) {
109  return Fw::Success::FAILURE;
110  }
111 
112  return Fw::Success::SUCCESS;
113 }
114 
115 // reads and validates the header from the m_sequenceBuffer
116 // return SUCCESS if sequence is valid, FAILURE otherwise
117 Fw::Success FpySequencer::readHeader() {
118  // deser header
119  Fw::SerializeStatus deserStatus = this->m_sequenceBuffer.deserializeTo(this->m_sequenceObj.get_header());
120  if (deserStatus != Fw::SerializeStatus::FW_SERIALIZE_OK) {
122  FpySequencer_FileReadStage::HEADER, this->m_sequenceFilePath, static_cast<I32>(deserStatus),
123  this->m_sequenceBuffer.getDeserializeSizeLeft(), this->m_sequenceBuffer.getSize());
124  return Fw::Success::FAILURE;
125  }
126 
127  // check matching schema version
128  if (this->m_sequenceObj.get_header().get_schemaVersion() != Fpy::SCHEMA_VERSION) {
130  this->m_sequenceObj.get_header().get_schemaVersion());
131  return Fw::Success::FAILURE;
132  }
133 
134  if (this->m_sequenceObj.get_header().get_argumentCount() > Fpy::MAX_SEQUENCE_ARG_COUNT) {
137  return Fw::Success::FAILURE;
138  }
139 
140  if (this->m_sequenceObj.get_header().get_statementCount() > Fpy::MAX_SEQUENCE_STATEMENT_COUNT) {
143  return Fw::Success::FAILURE;
144  }
145  return Fw::Success::SUCCESS;
146 }
147 
148 // reads and validates the body from the m_sequenceBuffer
149 // return SUCCESS if sequence is valid, FAILURE otherwise
150 Fw::Success FpySequencer::readBody() {
151  Fw::SerializeStatus deserStatus;
152 
153  const U8 argumentCount = this->m_sequenceObj.get_header().get_argumentCount();
154  this->m_totalExpectedArgSize = 0;
155 
156  // deser arguments
157  // Read and deserialize each arg_spec incrementally since they're variable-length
158  for (U8 i = 0; i < argumentCount; i++) {
159  Fpy::ArgSpec& argSpec = this->m_sequenceObj.get_args()[i];
160  deserStatus = this->m_sequenceBuffer.deserializeTo(argSpec);
161  if (deserStatus != Fw::SerializeStatus::FW_SERIALIZE_OK) {
163  FpySequencer_FileReadStage::BODY, this->m_sequenceFilePath, static_cast<I32>(deserStatus),
164  this->m_sequenceBuffer.getDeserializeSizeLeft(), this->m_sequenceBuffer.getSize());
165  return Fw::Success::FAILURE;
166  }
167 
168  m_totalExpectedArgSize += argSpec.get_argSize();
169  }
170 
171  // Check for overflow
172  if (m_totalExpectedArgSize > Fpy::MAX_STACK_SIZE) {
173  this->log_WARNING_HI_ArgTotalSizeExceedsStackLimit(m_totalExpectedArgSize);
174  return Fw::Success::FAILURE;
175  }
176 
177  // Validate total argument size
178  if (this->m_totalExpectedArgSize != this->m_sequenceArgs.get_size()) {
179  this->log_WARNING_HI_ArgSizeMismatch(this->m_totalExpectedArgSize, this->m_sequenceArgs.get_size(),
180  this->m_sequenceFilePath);
181  return Fw::Success::FAILURE;
182  }
183 
184  // deser statements
185  for (U16 statementIdx = 0; statementIdx < this->m_sequenceObj.get_header().get_statementCount(); statementIdx++) {
186  // deser statement
187  deserStatus = this->m_sequenceBuffer.deserializeTo(this->m_sequenceObj.get_statements()[statementIdx]);
188  if (deserStatus != Fw::FW_SERIALIZE_OK) {
190  FpySequencer_FileReadStage::BODY, this->m_sequenceFilePath, static_cast<I32>(deserStatus),
191  this->m_sequenceBuffer.getDeserializeSizeLeft(), this->m_sequenceBuffer.getSize());
192  return Fw::Success::FAILURE;
193  }
194  }
195  return Fw::Success::SUCCESS;
196 }
197 
198 // reads and validates the footer from the m_sequenceBuffer
199 // return SUCCESS if sequence is valid, FAILURE otherwise
200 Fw::Success FpySequencer::readFooter() {
201  Fw::SerializeStatus deserStatus = this->m_sequenceBuffer.deserializeTo(this->m_sequenceObj.get_footer());
202  if (deserStatus != Fw::FW_SERIALIZE_OK) {
204  FpySequencer_FileReadStage::FOOTER, this->m_sequenceFilePath, static_cast<I32>(deserStatus),
205  this->m_sequenceBuffer.getDeserializeSizeLeft(), this->m_sequenceBuffer.getSize());
206  return Fw::Success::FAILURE;
207  }
208 
209  // need this for some reason to "finalize" the crc TODO get an explanation on this
210  U32 computedCRC = 0;
211  this->m_computedCRC.finalize(computedCRC);
212 
213  if (computedCRC != this->m_sequenceObj.get_footer().get_crc()) {
214  this->log_WARNING_HI_WrongCRC(this->m_sequenceObj.get_footer().get_crc(), computedCRC);
215  return Fw::Success::FAILURE;
216  }
217 
218  return Fw::Success::SUCCESS;
219 }
220 
221 // reads some bytes from the open file into the m_sequenceBuffer.
222 // return success if successful
223 Fw::Success FpySequencer::readBytes(Os::File& file,
224  FwSizeType expectedReadLen,
225  const FpySequencer_FileReadStage& readStage,
226  bool updateCrc) {
227  FW_ASSERT(file.isOpen());
228  // this has to be declared a var because file.read must take a ref
229  FwSizeType actualReadLen = expectedReadLen;
230 
231  const FwSizeType capacity = this->m_sequenceBuffer.getCapacity();
232 
233  // if this fails, then you need to give the sequencer more buffer memory. pass in a bigger number
234  // to fpySeq.allocateBuffer(). This is usually done in topology setup CPP
235  if (expectedReadLen > capacity) {
236  this->log_WARNING_HI_InsufficientBufferSpace(static_cast<U64>(capacity), this->m_sequenceFilePath);
237  return Fw::Success::FAILURE;
238  }
239 
240  Os::File::Status fileStatus = file.read(this->m_sequenceBuffer.getBuffAddr(), actualReadLen);
241 
242  if (fileStatus != Os::File::OP_OK) {
243  this->log_WARNING_HI_FileReadError(readStage, this->m_sequenceFilePath, static_cast<I32>(fileStatus));
244  return Fw::Success::FAILURE;
245  }
246 
247  if (actualReadLen < expectedReadLen) {
248  this->log_WARNING_HI_EndOfFileError(readStage, this->m_sequenceFilePath);
249  return Fw::Success::FAILURE;
250  }
251 
252  // should probably fail if we read in MORE bytes than we ask for
253  FW_ASSERT(expectedReadLen == actualReadLen, static_cast<FwAssertArgType>(expectedReadLen),
254  static_cast<FwAssertArgType>(actualReadLen));
255 
256  Fw::SerializeStatus serializeStatus =
257  this->m_sequenceBuffer.setBuffLen(static_cast<Fw::Serializable::SizeType>(expectedReadLen));
258  FW_ASSERT(serializeStatus == Fw::FW_SERIALIZE_OK, serializeStatus);
259 
260  if (updateCrc) {
261  this->m_computedCRC.update(this->m_sequenceBuffer.getBuffAddr(), expectedReadLen);
262  }
263 
264  return Fw::Success::SUCCESS;
265 }
266 
267 } // namespace Svc
void update(const void *const data, const FwSizeType len)
Definition: HashImpl.cpp:34
Serialization/Deserialization operation was successful.
void clear()
Clear external buffer.
virtual void * allocate(const FwEnumStoreType identifier, FwSizeType &size, bool &recoverable, FwSizeType alignment=alignof(std::max_align_t))=0
Operation succeeded.
Definition: Os.hpp:27
U16 get_statementCount() const
Get member statementCount.
Type_of_args & get_args()
Get member args.
Representing success.
void log_WARNING_HI_InsufficientBufferSpace(U64 bufferSize, const Fw::StringBase &filePath) const
Log event InsufficientBufferSpace.
PlatformSizeType FwSizeType
I32 FwEnumStoreType
void log_WARNING_HI_WrongCRC(U32 expected, U32 actual) const
Log event WrongCRC.
Serializable::SizeType getSize() const override
Get current buffer size.
Status position(FwSizeType &position_result) override
get file pointer position of the currently open file
Definition: File.cpp:113
Status size(FwSizeType &size_result) override
get size of currently open file
Definition: File.cpp:104
void log_WARNING_HI_FileReadError(Svc::FpySequencer_FileReadStage readStage, const Fw::StringBase &filePath, I32 errorCode) const
Log event FileReadError.
The size of the serial representation.
void log_WARNING_HI_FileApiError(const Fw::StringBase &filePath, I32 errorCode) const
Log event FileApiError.
void log_WARNING_HI_ExtraBytesInSequence(FwSizeType remaining) const
Log event ExtraBytesInSequence.
void log_WARNING_HI_TooManySequenceDirectives(U16 count, U16 max) const
Log event TooManySequenceDirectives.
void init()
Definition: HashImpl.cpp:30
SerializeStatus
forward declaration for string
Os::FileInterface::Status open(const char *path, Mode mode)
open file with supplied path and mode
Definition: File.cpp:43
U8 get_schemaVersion() const
Get member schemaVersion.
Serializable::SizeType getCapacity() const
Get buffer capacity.
Svc::Fpy::Header & get_header()
Get member header.
Serializable::SizeType getDeserializeSizeLeft() const override
Get remaining deserialization buffer size.
void log_WARNING_HI_EndOfFileError(Svc::FpySequencer_FileReadStage readStage, const Fw::StringBase &filePath) const
Log event EndOfFileError.
U32 get_bodySize() const
Get member bodySize.
Type_of_statements & get_statements()
Get member statements.
void allocateBuffer(FwEnumStoreType identifier, Fw::MemAllocator &allocator, FwSizeType bytes)
const char * toChar() const
Convert to a C-style char*.
FwSizeType get_size() const
Get member size.
Representing failure.
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:53
Status read(U8 *buffer, FwSizeType &size)
read data from this file into supplied buffer bounded by size
Definition: File.cpp:187
void log_WARNING_HI_FileOpenError(const Fw::StringBase &filePath, I32 errorCode) const
Log event FileOpenError.
void setExtBuffer(U8 *buffPtr, Serializable::SizeType size)
Set the external buffer.
Memory Allocation base class.
Svc::Fpy::Footer & get_footer()
Get member footer.
Operation was successful.
Definition: File.hpp:42
void log_WARNING_HI_ArgTotalSizeExceedsStackLimit(Svc::Fpy::StackSizeType argSize) const
Log event ArgTotalSizeExceedsStackLimit.
void log_WARNING_HI_FileReadDeserializeError(Svc::FpySequencer_FileReadStage readStage, const Fw::StringBase &filePath, I32 errorCode, U64 buffLeft, U64 buffLength) const
Log event FileReadDeserializeError.
Open file for reading.
Definition: File.hpp:33
U8 get_argumentCount() const
Get member argumentCount.
void log_WARNING_HI_WrongSchemaVersion(U8 expected, U8 actual) const
Log event WrongSchemaVersion.
RateGroupDivider component implementation.
virtual SizeType length() const
Get the length of the string.
SerializeStatus deserializeTo(U8 &val, Endianness mode=Endianness::BIG) override
Deserialize an 8-bit unsigned integer value.
void log_WARNING_HI_TooManySequenceArgs(U8 count, U8 max) const
Log event TooManySequenceArgs.
void log_WARNING_HI_ArgSizeMismatch(Svc::Fpy::StackSizeType expected, FwSizeType actual, const Fw::StringBase &filePath) const
Log event ArgSizeMismatch.
virtual void deallocate(const FwEnumStoreType identifier, void *ptr)=0
void deallocateBuffer(Fw::MemAllocator &allocator)
SerializeStatus setBuffLen(Serializable::SizeType length) override
Set buffer length manually.
void finalize(HashBuffer &buffer) const
Definition: HashImpl.cpp:40
#define FW_ASSERT(...)
Definition: Assert.hpp:14
Success/Failure.
bool isOpen() const
determine if the file is open
Definition: File.cpp:98
U32 StackSizeType
the type which everything referencing a size or offset on the stack is represented in ...
U8 * getBuffAddr()
Get buffer address for data filling (non-const version)