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  if (this->m_sequenceFilePath.length() == 0) {
30  this->log_WARNING_HI_FileOpenError(this->m_sequenceFilePath, static_cast<I32>(Os::File::INVALID_ARGUMENT));
31  return Fw::Success::FAILURE;
32  }
33 
34  // crc needs to be initialized with a particular value
35  // for the calculation to work
36  this->m_computedCRC.init();
37 
38  Os::File sequenceFile;
39  Os::File::Status openStatus = sequenceFile.open(this->m_sequenceFilePath.toChar(), Os::File::OPEN_READ);
40 
41  if (openStatus != Os::File::Status::OP_OK) {
42  this->log_WARNING_HI_FileOpenError(this->m_sequenceFilePath, static_cast<I32>(openStatus));
43  return Fw::Success::FAILURE;
44  }
45 
46  Fw::Success readStatus =
48 
49  if (readStatus != Fw::Success::SUCCESS) {
50  return Fw::Success::FAILURE;
51  }
52 
53  readStatus = this->readHeader();
54 
55  if (readStatus != Fw::Success::SUCCESS) {
56  return Fw::Success::FAILURE;
57  }
58 
59  readStatus =
60  readBytes(sequenceFile, this->m_sequenceObj.get_header().get_bodySize(), FpySequencer_FileReadStage::BODY);
61 
62  if (readStatus != Fw::Success::SUCCESS) {
63  return Fw::Success::FAILURE;
64  }
65 
66  readStatus = this->readBody();
67 
68  if (readStatus != Fw::Success::SUCCESS) {
69  return Fw::Success::FAILURE;
70  }
71 
72  // read footer bytes but don't include in CRC
73  readStatus = this->readBytes(sequenceFile, Fpy::Footer::SERIALIZED_SIZE, FpySequencer_FileReadStage::FOOTER, false);
74 
75  if (readStatus != Fw::Success::SUCCESS) {
76  return Fw::Success::FAILURE;
77  }
78 
79  readStatus = this->readFooter();
80 
81  if (readStatus != Fw::Success::SUCCESS) {
82  return Fw::Success::FAILURE;
83  }
84 
85  // make sure we're at EOF. The size() and position() OS calls can fail
86  // on filesystem errors or if the file was concurrently modified by
87  // another FileManager command (e.g. RemoveFile or AppendFile) between
88  // the file open and this point. Treat the failure as a validation
89  // failure rather than aborting the FSW process.
90  FwSizeType sequenceFileSize;
91  Os::File::Status sizeStatus = sequenceFile.size(sequenceFileSize);
92  if (sizeStatus != Os::File::Status::OP_OK) {
93  this->log_WARNING_HI_FileApiError(this->m_sequenceFilePath, static_cast<I32>(sizeStatus));
94  return Fw::Success::FAILURE;
95  }
96 
97  FwSizeType sequenceFilePosition;
98  Os::File::Status positionStatus = sequenceFile.position(sequenceFilePosition);
99  if (positionStatus != Os::File::Status::OP_OK) {
100  this->log_WARNING_HI_FileApiError(this->m_sequenceFilePath, static_cast<I32>(positionStatus));
101  return Fw::Success::FAILURE;
102  }
103 
104  if (sequenceFileSize != sequenceFilePosition) {
105  this->log_WARNING_HI_ExtraBytesInSequence(static_cast<FwSizeType>(sequenceFileSize - sequenceFilePosition));
106  return Fw::Success::FAILURE;
107  }
108 
109  Fpy::StackSizeType availableSpace = Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size;
110 
111  if (this->m_sequenceArgs.get_size() > availableSpace) {
112  return Fw::Success::FAILURE;
113  }
114 
115  // The argument size arrives separately from the argument buffer, so it can describe more bytes
116  // than that buffer holds. Reject before anything reads the buffer by that size.
117  const FwSizeType argCapacity = static_cast<FwSizeType>(sizeof(this->m_sequenceArgs.get_buffer()));
118  if (this->m_sequenceArgs.get_size() > argCapacity) {
119  this->log_WARNING_HI_ArgSizeExceedsCapacity(this->m_sequenceArgs.get_size(), argCapacity);
120  return Fw::Success::FAILURE;
121  }
122 
123  return Fw::Success::SUCCESS;
124 }
125 
126 // reads and validates the header from the m_sequenceBuffer
127 // return SUCCESS if sequence is valid, FAILURE otherwise
128 Fw::Success FpySequencer::readHeader() {
129  // deser header
130  Fw::SerializeStatus deserStatus = this->m_sequenceBuffer.deserializeTo(this->m_sequenceObj.get_header());
131  if (deserStatus != Fw::SerializeStatus::FW_SERIALIZE_OK) {
133  FpySequencer_FileReadStage::HEADER, this->m_sequenceFilePath, static_cast<I32>(deserStatus),
134  this->m_sequenceBuffer.getDeserializeSizeLeft(), this->m_sequenceBuffer.getSize());
135  return Fw::Success::FAILURE;
136  }
137 
138  // check matching schema version
139  if (this->m_sequenceObj.get_header().get_schemaVersion() != Fpy::SCHEMA_VERSION) {
141  this->m_sequenceObj.get_header().get_schemaVersion());
142  return Fw::Success::FAILURE;
143  }
144 
145  if (this->m_sequenceObj.get_header().get_argumentCount() > Fpy::MAX_SEQUENCE_ARG_COUNT) {
148  return Fw::Success::FAILURE;
149  }
150 
151  if (this->m_sequenceObj.get_header().get_statementCount() > Fpy::MAX_SEQUENCE_STATEMENT_COUNT) {
154  return Fw::Success::FAILURE;
155  }
156  return Fw::Success::SUCCESS;
157 }
158 
159 // reads and validates the body from the m_sequenceBuffer
160 // return SUCCESS if sequence is valid, FAILURE otherwise
161 Fw::Success FpySequencer::readBody() {
162  Fw::SerializeStatus deserStatus;
163 
164  const U8 argumentCount = this->m_sequenceObj.get_header().get_argumentCount();
165  this->m_totalExpectedArgSize = 0;
166 
167  // deser arguments
168  // Read and deserialize each arg_spec incrementally since they're variable-length
169  for (U8 i = 0; i < argumentCount; i++) {
170  Fpy::ArgSpec& argSpec = this->m_sequenceObj.get_args()[i];
171  deserStatus = this->m_sequenceBuffer.deserializeTo(argSpec);
172  if (deserStatus != Fw::SerializeStatus::FW_SERIALIZE_OK) {
174  FpySequencer_FileReadStage::BODY, this->m_sequenceFilePath, static_cast<I32>(deserStatus),
175  this->m_sequenceBuffer.getDeserializeSizeLeft(), this->m_sequenceBuffer.getSize());
176  return Fw::Success::FAILURE;
177  }
178 
179  m_totalExpectedArgSize += argSpec.get_argSize();
180  }
181 
182  // Check for overflow
183  if (m_totalExpectedArgSize > Fpy::MAX_STACK_SIZE) {
184  this->log_WARNING_HI_ArgTotalSizeExceedsStackLimit(m_totalExpectedArgSize);
185  return Fw::Success::FAILURE;
186  }
187 
188  // Validate total argument size
189  if (this->m_totalExpectedArgSize != this->m_sequenceArgs.get_size()) {
190  this->log_WARNING_HI_ArgSizeMismatch(this->m_totalExpectedArgSize, this->m_sequenceArgs.get_size(),
191  this->m_sequenceFilePath);
192  return Fw::Success::FAILURE;
193  }
194 
195  // deser statements
196  const U16 statementCount = this->m_sequenceObj.get_header().get_statementCount();
197  for (U16 statementIdx = 0; statementIdx < statementCount; statementIdx++) {
198  // deser statement
199  deserStatus = this->m_sequenceBuffer.deserializeTo(this->m_sequenceObj.get_statements()[statementIdx]);
200  if (deserStatus != Fw::FW_SERIALIZE_OK) {
202  FpySequencer_FileReadStage::BODY, this->m_sequenceFilePath, static_cast<I32>(deserStatus),
203  this->m_sequenceBuffer.getDeserializeSizeLeft(), this->m_sequenceBuffer.getSize());
204  return Fw::Success::FAILURE;
205  }
206  }
207  return Fw::Success::SUCCESS;
208 }
209 
210 // reads and validates the footer from the m_sequenceBuffer
211 // return SUCCESS if sequence is valid, FAILURE otherwise
212 Fw::Success FpySequencer::readFooter() {
213  Fw::SerializeStatus deserStatus = this->m_sequenceBuffer.deserializeTo(this->m_sequenceObj.get_footer());
214  if (deserStatus != Fw::FW_SERIALIZE_OK) {
216  FpySequencer_FileReadStage::FOOTER, this->m_sequenceFilePath, static_cast<I32>(deserStatus),
217  this->m_sequenceBuffer.getDeserializeSizeLeft(), this->m_sequenceBuffer.getSize());
218  return Fw::Success::FAILURE;
219  }
220 
221  // need this for some reason to "finalize" the crc TODO get an explanation on this
222  U32 computedCRC = 0;
223  this->m_computedCRC.finalize(computedCRC);
224 
225  if (computedCRC != this->m_sequenceObj.get_footer().get_crc()) {
226  this->log_WARNING_HI_WrongCRC(this->m_sequenceObj.get_footer().get_crc(), computedCRC);
227  return Fw::Success::FAILURE;
228  }
229 
230  return Fw::Success::SUCCESS;
231 }
232 
233 // reads some bytes from the open file into the m_sequenceBuffer.
234 // return success if successful
235 Fw::Success FpySequencer::readBytes(Os::File& file,
236  FwSizeType expectedReadLen,
237  const FpySequencer_FileReadStage& readStage,
238  bool updateCrc) {
239  FW_ASSERT(file.isOpen());
240  // this has to be declared a var because file.read must take a ref
241  FwSizeType actualReadLen = expectedReadLen;
242 
243  const FwSizeType capacity = this->m_sequenceBuffer.getCapacity();
244 
245  // if this fails, then you need to give the sequencer more buffer memory. pass in a bigger number
246  // to fpySeq.allocateBuffer(). This is usually done in topology setup CPP
247  if (expectedReadLen > capacity) {
248  this->log_WARNING_HI_InsufficientBufferSpace(static_cast<U64>(capacity), this->m_sequenceFilePath);
249  return Fw::Success::FAILURE;
250  }
251 
252  Os::File::Status fileStatus = file.read(this->m_sequenceBuffer.getBuffAddr(), actualReadLen);
253 
254  if (fileStatus != Os::File::OP_OK) {
255  this->log_WARNING_HI_FileReadError(readStage, this->m_sequenceFilePath, static_cast<I32>(fileStatus));
256  return Fw::Success::FAILURE;
257  }
258 
259  if (actualReadLen < expectedReadLen) {
260  this->log_WARNING_HI_EndOfFileError(readStage, this->m_sequenceFilePath);
261  return Fw::Success::FAILURE;
262  }
263 
264  // should probably fail if we read in MORE bytes than we ask for
265  FW_ASSERT(expectedReadLen == actualReadLen, static_cast<FwAssertArgType>(expectedReadLen),
266  static_cast<FwAssertArgType>(actualReadLen));
267 
268  Fw::SerializeStatus serializeStatus =
269  this->m_sequenceBuffer.setBuffLen(static_cast<Fw::Serializable::SizeType>(expectedReadLen));
270  FW_ASSERT(serializeStatus == Fw::FW_SERIALIZE_OK, serializeStatus);
271 
272  if (updateCrc) {
273  this->m_computedCRC.update(this->m_sequenceBuffer.getBuffAddr(), expectedReadLen);
274  }
275 
276  return Fw::Success::SUCCESS;
277 }
278 
279 } // 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.
void log_WARNING_HI_ArgSizeExceedsCapacity(FwSizeType actual, FwSizeType capacity) const
Log event ArgSizeExceedsCapacity.
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_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
void log_WARNING_HI_EndOfFileError(const Svc::FpySequencer_FileReadStage &readStage, const Fw::StringBase &filePath) const
Log event EndOfFileError.
U8 get_schemaVersion() const
Get member schemaVersion.
Serializable::SizeType getCapacity() const
Get buffer capacity.
Svc::Fpy::Header & get_header()
Get member header.
Invalid argument passed in.
Definition: File.hpp:51
Type_of_buffer & get_buffer()
Get member buffer.
Serializable::SizeType getDeserializeSizeLeft() const override
Get remaining deserialization buffer size.
void log_WARNING_HI_FileReadError(const Svc::FpySequencer_FileReadStage &readStage, const Fw::StringBase &filePath, I32 errorCode) const
Log event FileReadError.
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:54
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.
The size of the serial representation.
Operation was successful.
Definition: File.hpp:42
void log_WARNING_HI_ArgTotalSizeExceedsStackLimit(Svc::Fpy::StackSizeType argSize) const
Log event ArgTotalSizeExceedsStackLimit.
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 log_WARNING_HI_FileReadDeserializeError(const Svc::FpySequencer_FileReadStage &readStage, const Fw::StringBase &filePath, I32 errorCode, U64 buffLeft, U64 buffLength) const
Log event FileReadDeserializeError.
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)