F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SerializableFile.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title SerializableFile.cpp
3 // \author dinkel
4 // \brief cpp file for SerializableFile
5 //
6 // \copyright
7 // Copyright 2009-2016, by the California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 
14 #include "Fw/Types/Assert.hpp"
15 #include "Os/File.hpp"
16 
17 namespace Fw {
18 
20  m_allocator(allocator),
21  m_recoverable(false), // for compiler; not used
22  m_actualSize(maxSerializedSize),
23  m_buffer(static_cast<U8*>(this->m_allocator->allocate(0, m_actualSize, m_recoverable)), m_actualSize)
24  {
25  // assert if allocator returns smaller size
26  FW_ASSERT(maxSerializedSize == m_actualSize, static_cast<FwAssertArgType>(maxSerializedSize), static_cast<FwAssertArgType>(m_actualSize));
27  FW_ASSERT(nullptr != m_buffer.getBuffAddr());
28  }
29 
31  this->m_allocator->deallocate(0, this->m_buffer.getBuffAddr());
32  }
33 
34  SerializableFile::Status SerializableFile::load(const char* fileName, Serializable& serializable) {
35  Os::File file;
36  Os::File::Status status;
37  status = file.open(fileName, Os::File::OPEN_READ);
38  if( Os::File::OP_OK != status ) {
39  return FILE_OPEN_ERROR;
40  }
41 
42  FwSignedSizeType capacity = static_cast<FwSignedSizeType>(this->m_buffer.getBuffCapacity());
43  FwSignedSizeType length = static_cast<FwSignedSizeType>(capacity);
44  status = file.read(this->m_buffer.getBuffAddr(), length, Os::File::WaitType::NO_WAIT);
45  if( Os::File::OP_OK != status ) {
46  file.close();
47  return FILE_READ_ERROR;
48  }
49  file.close();
50 
51  this->reset();
52  SerializeStatus serStatus;
53  serStatus = this->m_buffer.setBuffLen(static_cast<NATIVE_UINT_TYPE>(length));
54  FW_ASSERT(FW_SERIALIZE_OK == serStatus, serStatus);
55  serStatus = serializable.deserialize(this->m_buffer);
56  if(FW_SERIALIZE_OK != serStatus) {
57  return DESERIALIZATION_ERROR;
58  }
59 
61  }
62 
63  SerializableFile::Status SerializableFile::save(const char* fileName, Serializable& serializable) {
64  this->reset();
65  SerializeStatus serStatus = serializable.serialize(this->m_buffer);
66  FW_ASSERT(FW_SERIALIZE_OK == serStatus, serStatus);
67 
68  Os::File file;
69  Os::File::Status status;
70  status = file.open(fileName, Os::File::OPEN_WRITE);
71  if( Os::File::OP_OK != status ) {
72  return FILE_OPEN_ERROR;
73  }
74 
75  FwSignedSizeType length = static_cast<FwSignedSizeType>(this->m_buffer.getBuffLength());
76  FwSignedSizeType size = length;
77  status = file.write(this->m_buffer.getBuffAddr(), length);
78  if( (Os::File::OP_OK != status) || (length != size)) {
79  file.close();
80  return FILE_WRITE_ERROR;
81  }
82 
83  file.close();
84 
86  }
87 
88  void SerializableFile::reset() {
89  this->m_buffer.resetSer();
90  this->m_buffer.resetDeser();
91  }
92 }
#define FW_ASSERT(...)
Definition: Assert.hpp:14
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:30
PlatformUIntType NATIVE_UINT_TYPE
Definition: BasicTypes.h:56
PlatformAssertArgType FwAssertArgType
Definition: FpConfig.h:39
PlatformSignedSizeType FwSignedSizeType
Definition: FpConfig.h:30
virtual void deallocate(const NATIVE_UINT_TYPE identifier, void *ptr)=0
Deallocate memory.
NATIVE_UINT_TYPE getBuffCapacity() const
returns capacity, not current size, of buffer
U8 * getBuffAddr()
gets buffer address for data filling
SerializableFile(MemAllocator *allocator, NATIVE_UINT_TYPE maxSerializedSize)
Status save(const char *fileName, Serializable &serializable)
Status load(const char *fileName, Serializable &serializable)
forward declaration
virtual SerializeStatus deserialize(SerializeBufferBase &buffer)=0
deserialize to contents
virtual SerializeStatus serialize(SerializeBufferBase &buffer) const =0
serialize contents
SerializeStatus setBuffLen(Serializable::SizeType length)
sets buffer length manually after filling with data
void resetDeser()
reset deserialization to beginning
void resetSer()
reset to beginning of buffer to reuse for serialization
Serializable::SizeType getBuffLength() const
returns current buffer size
Status read(U8 *buffer, FwSignedSizeType &size)
read data from this file into supplied buffer bounded by size
Definition: File.cpp:143
void close() override
close the file, if not opened then do nothing
Definition: File.cpp:70
Os::FileInterface::Status open(const char *path, Mode mode)
open file with supplied path and mode
Definition: File.cpp:45
Status write(const U8 *buffer, FwSignedSizeType &size)
write data to this file from the supplied buffer bounded by size
Definition: File.cpp:163
@ OP_OK
Operation was successful.
Definition: File.hpp:30
@ OPEN_WRITE
Open file for writing.
Definition: File.hpp:23
@ OPEN_READ
Open file for reading.
Definition: File.hpp:21
SerializeStatus
forward declaration for string
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.