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.
3 extern "C" {
5 }
6 namespace Svc {
7 
9  // if this assertion fails, you aren't allocating enough bytes for the
10  // FpySequencer. this is because you must have a buffer big enough to fit the
11  // header of a sequence
12  FW_ASSERT(bytes >= Fpy::Header::SERIALIZED_SIZE, static_cast<FwAssertArgType>(bytes));
13  FwSizeType originalBytes = bytes;
14  bool recoverable = false;
15  this->m_allocatorId = identifier;
16  U8* allocatedMemory = static_cast<U8*>(allocator.allocate(identifier, bytes, recoverable));
17  // if this fails, unable to allocate the requested amount of money
18  FW_ASSERT(bytes >= originalBytes, static_cast<FwAssertArgType>(bytes));
19  this->m_sequenceBuffer.setExtBuffer(allocatedMemory, bytes);
20 }
21 
23  allocator.deallocate(this->m_allocatorId, this->m_sequenceBuffer.getBuffAddr());
24  this->m_sequenceBuffer.clear();
25 }
26 
27 void FpySequencer::updateCrc(U32& crc, const U8* buffer, FwSizeType bufferSize) {
28  FW_ASSERT(buffer);
29  for (FwSizeType index = 0; index < bufferSize; index++) {
30  crc = static_cast<U32>(update_crc_32(crc, static_cast<char>(buffer[index])));
31  }
32 }
33 
34 // loads the sequence in memory, and does header/crc/integrity checks.
35 // return true if sequence is valid
36 Fw::Success FpySequencer::validate() {
37  FW_ASSERT(this->m_sequenceFilePath.length() > 0);
38 
39  // crc needs to be initialized with a particular value
40  // for the calculation to work
41  this->m_computedCRC = CRC_INITIAL_VALUE;
42 
43  Os::File sequenceFile;
44  Os::File::Status openStatus = sequenceFile.open(this->m_sequenceFilePath.toChar(), Os::File::OPEN_READ);
45 
46  if (openStatus != Os::File::Status::OP_OK) {
47  this->log_WARNING_HI_FileOpenError(this->m_sequenceFilePath, static_cast<I32>(openStatus));
48  return Fw::Success::FAILURE;
49  }
50 
51  Fw::Success readStatus =
53 
54  if (readStatus != Fw::Success::SUCCESS) {
55  return Fw::Success::FAILURE;
56  }
57 
58  readStatus = this->readHeader();
59 
60  if (readStatus != Fw::Success::SUCCESS) {
61  return Fw::Success::FAILURE;
62  }
63 
64  readStatus =
65  readBytes(sequenceFile, this->m_sequenceObj.get_header().get_bodySize(), FpySequencer_FileReadStage::BODY);
66 
67  if (readStatus != Fw::Success::SUCCESS) {
68  return Fw::Success::FAILURE;
69  }
70 
71  readStatus = this->readBody();
72 
73  if (readStatus != Fw::Success::SUCCESS) {
74  return Fw::Success::FAILURE;
75  }
76 
77  // read footer bytes but don't include in CRC
78  readStatus = this->readBytes(sequenceFile, Fpy::Footer::SERIALIZED_SIZE, FpySequencer_FileReadStage::FOOTER, false);
79 
80  if (readStatus != Fw::Success::SUCCESS) {
81  return Fw::Success::FAILURE;
82  }
83 
84  readStatus = this->readFooter();
85 
86  if (readStatus != Fw::Success::SUCCESS) {
87  return Fw::Success::FAILURE;
88  }
89 
90  // make sure we're at EOF
91  FwSizeType sequenceFileSize;
92  FW_ASSERT(sequenceFile.size(sequenceFileSize) == Os::File::Status::OP_OK);
93 
94  FwSizeType sequenceFilePosition;
95  FW_ASSERT(sequenceFile.position(sequenceFilePosition) == Os::File::Status::OP_OK);
96 
97  if (sequenceFileSize != sequenceFilePosition) {
98  this->log_WARNING_HI_ExtraBytesInSequence(static_cast<U32>(sequenceFileSize - sequenceFilePosition));
99  return Fw::Success::FAILURE;
100  }
101 
102  return Fw::Success::SUCCESS;
103 }
104 
105 Fw::Success FpySequencer::readHeader() {
106  // deser header
107  Fw::SerializeStatus deserStatus = this->m_sequenceBuffer.deserialize(this->m_sequenceObj.get_header());
108  if (deserStatus != Fw::SerializeStatus::FW_SERIALIZE_OK) {
110  FpySequencer_FileReadStage::HEADER, this->m_sequenceFilePath, static_cast<I32>(deserStatus),
111  this->m_sequenceBuffer.getBuffLeft(), this->m_sequenceBuffer.getBuffLength());
112  return Fw::Success::FAILURE;
113  }
114 
115  // check matching schema version
116  if (this->m_sequenceObj.get_header().get_schemaVersion() != Fpy::SCHEMA_VERSION) {
118  this->m_sequenceObj.get_header().get_schemaVersion());
119  return Fw::Success::FAILURE;
120  }
121 
122  if (this->m_sequenceObj.get_header().get_argumentCount() > Fpy::MAX_SEQUENCE_ARG_COUNT) {
125  return Fw::Success::FAILURE;
126  }
127 
128  if (this->m_sequenceObj.get_header().get_statementCount() > Fpy::MAX_SEQUENCE_STATEMENT_COUNT) {
131  return Fw::Success::FAILURE;
132  }
133  return Fw::Success::SUCCESS;
134 }
135 
136 Fw::Success FpySequencer::readBody() {
137  Fw::SerializeStatus deserStatus;
138  // deser body:
139  // deser arg mappings
140  for (U8 argMappingIdx = 0; argMappingIdx < this->m_sequenceObj.get_header().get_argumentCount(); argMappingIdx++) {
141  // serializable register index of arg $argMappingIdx
142  // TODO should probably check that this serReg is inside range
143  deserStatus = this->m_sequenceBuffer.deserialize(this->m_sequenceObj.get_args()[argMappingIdx]);
144  if (deserStatus != Fw::FW_SERIALIZE_OK) {
146  FpySequencer_FileReadStage::BODY, this->m_sequenceFilePath, static_cast<I32>(deserStatus),
147  this->m_sequenceBuffer.getBuffLeft(), this->m_sequenceBuffer.getBuffLength());
148  return Fw::Success::FAILURE;
149  }
150  }
151 
152  // deser statements
153  for (U16 statementIdx = 0; statementIdx < this->m_sequenceObj.get_header().get_statementCount(); statementIdx++) {
154  // deser statement
155  deserStatus = this->m_sequenceBuffer.deserialize(this->m_sequenceObj.get_statements()[statementIdx]);
156  if (deserStatus != Fw::FW_SERIALIZE_OK) {
158  FpySequencer_FileReadStage::BODY, this->m_sequenceFilePath, static_cast<I32>(deserStatus),
159  this->m_sequenceBuffer.getBuffLeft(), this->m_sequenceBuffer.getBuffLength());
160  return Fw::Success::FAILURE;
161  }
162  }
163  return Fw::Success::SUCCESS;
164 }
165 
166 Fw::Success FpySequencer::readFooter() {
167  Fw::SerializeStatus deserStatus = this->m_sequenceBuffer.deserialize(this->m_sequenceObj.get_footer());
168  if (deserStatus != Fw::FW_SERIALIZE_OK) {
170  FpySequencer_FileReadStage::FOOTER, this->m_sequenceFilePath, static_cast<I32>(deserStatus),
171  this->m_sequenceBuffer.getBuffLeft(), this->m_sequenceBuffer.getBuffLength());
172  return Fw::Success::FAILURE;
173  }
174 
175  // need this for some reason to "finalize" the crc TODO get an explanation on this
176  this->m_computedCRC = ~this->m_computedCRC;
177 
178  if (this->m_computedCRC != this->m_sequenceObj.get_footer().get_crc()) {
179  this->log_WARNING_HI_WrongCRC(this->m_sequenceObj.get_footer().get_crc(), this->m_computedCRC);
180  return Fw::Success::FAILURE;
181  }
182 
183  return Fw::Success::SUCCESS;
184 }
185 
186 // reads some bytes from the open file into the m_sequenceBuffer.
187 // return success if successful
188 Fw::Success FpySequencer::readBytes(Os::File& file,
189  FwSizeType expectedReadLen,
190  const FpySequencer_FileReadStage& readStage,
191  bool updateCrc) {
192  FW_ASSERT(file.isOpen());
193  // this has to be declared a var because file.read must take a ref
194  FwSizeType actualReadLen = expectedReadLen;
195 
196  const FwSizeType capacity = this->m_sequenceBuffer.getBuffCapacity();
197 
198  // if this fails, then you need to give the sequencer more buffer memory. pass in a bigger number
199  // to fpySeq.allocateBuffer(). This is usually done in topology setup CPP
200  if (expectedReadLen > capacity) {
201  this->log_WARNING_HI_InsufficientBufferSpace(static_cast<U64>(capacity), this->m_sequenceFilePath);
202  return Fw::Success::FAILURE;
203  }
204 
205  Os::File::Status fileStatus = file.read(this->m_sequenceBuffer.getBuffAddr(), actualReadLen);
206 
207  if (fileStatus != Os::File::OP_OK) {
208  this->log_WARNING_HI_FileReadError(readStage, this->m_sequenceFilePath, static_cast<I32>(fileStatus));
209  return Fw::Success::FAILURE;
210  }
211 
212  if (actualReadLen < expectedReadLen) {
213  this->log_WARNING_HI_EndOfFileError(readStage, this->m_sequenceFilePath);
214  return Fw::Success::FAILURE;
215  }
216 
217  // should probably fail if we read in MORE bytes than we ask for
218  FW_ASSERT(expectedReadLen == actualReadLen, static_cast<FwAssertArgType>(expectedReadLen),
219  static_cast<FwAssertArgType>(actualReadLen));
220 
221  Fw::SerializeStatus serializeStatus =
222  this->m_sequenceBuffer.setBuffLen(static_cast<Fw::Serializable::SizeType>(expectedReadLen));
223  FW_ASSERT(serializeStatus == Fw::FW_SERIALIZE_OK, serializeStatus);
224 
225  if (updateCrc) {
226  FpySequencer::updateCrc(this->m_computedCRC, this->m_sequenceBuffer.getBuffAddr(), expectedReadLen);
227  }
228 
229  return Fw::Success::SUCCESS;
230 }
231 
232 } // namespace Svc
Serialization/Deserialization operation was successful.
void clear()
clear external buffer
Operation succeeded.
Definition: Os.hpp:26
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.
virtual void * allocate(const FwEnumStoreType identifier, FwSizeType &size, bool &recoverable)=0
Allocate memory.
const char * toChar() const
Definition: String.hpp:50
Status position(FwSizeType &position_result) override
get file pointer position of the currently open file
Definition: File.cpp:94
Status size(FwSizeType &size_result) override
get size of currently open file
Definition: File.cpp:85
Os::FileInterface::Status open(const char *path, Mode mode)
open file with supplied path and mode
void log_WARNING_HI_FileReadError(Svc::FpySequencer_FileReadStage readStage, const Fw::StringBase &filePath, I32 errorCode) const
Log event FileReadError.
SerializeStatus
forward declaration for string
unsigned long update_crc_32(unsigned long crc, char c)
Definition: lib_crc.c:271
The size of the serial representation.
void log_WARNING_HI_ExtraBytesInSequence(U32 remaining) const
Log event ExtraBytesInSequence.
Serializable::SizeType getBuffLength() const
returns current buffer size
Serializable::SizeType getBuffLeft() const
returns how much deserialization buffer is left
U8 get_schemaVersion() const
Get member schemaVersion.
Svc::Fpy::Header & get_header()
Get member header.
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.
SizeType length() const
Get length of string.
Definition: StringBase.cpp:121
void allocateBuffer(FwEnumStoreType identifier, Fw::MemAllocator &allocator, FwSizeType bytes)
Representing failure.
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:53
void log_WARNING_HI_TooManySequenceStatements(U16 count, U16 max) const
Log event TooManySequenceStatements.
Status read(U8 *buffer, FwSizeType &size)
read data from this file into supplied buffer bounded by size
Definition: File.cpp:168
void log_WARNING_HI_FileOpenError(const Fw::StringBase &filePath, I32 errorCode) const
Log event FileOpenError.
void setExtBuffer(U8 *buffPtr, Serializable::SizeType size)
Svc::Fpy::Footer & get_footer()
Get member footer.
Operation was successful.
Definition: File.hpp:40
void log_WARNING_HI_FileReadDeserializeError(Svc::FpySequencer_FileReadStage readStage, const Fw::StringBase &filePath, I32 errorCode, U64 buffLeft, U64 buffLength) const
Log event FileReadDeserializeError.
SerializeStatus deserialize(U8 &val)
Open file for reading.
Definition: File.hpp:31
U8 get_argumentCount() const
Get member argumentCount.
void log_WARNING_HI_WrongSchemaVersion(U8 expected, U8 actual) const
Log event WrongSchemaVersion.
RateGroupDivider component implementation.
void log_WARNING_HI_TooManySequenceArgs(U8 count, U8 max) const
Log event TooManySequenceArgs.
Serializable::SizeType getBuffCapacity() const
returns capacity, not current size, of buffer
virtual void deallocate(const FwEnumStoreType identifier, void *ptr)=0
Deallocate memory.
void deallocateBuffer(Fw::MemAllocator &allocator)
#define FW_ASSERT(...)
Definition: Assert.hpp:14
Success/Failure.
bool isOpen() const
determine if the file is open
Definition: File.cpp:79
U8 * getBuffAddr()
gets buffer address for data filling
SerializeStatus setBuffLen(Serializable::SizeType length)
sets buffer length manually after filling with data