F´ Flight Software - C/C++ Documentation NASA-v1.6.0
A framework for building embedded system applications to NASA flight quality standards.
Loading...
Searching...
No Matches
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
17namespace Fw {
18
20 allocator(allocator),
21 recoverable(false), // for compiler; not used
22 actualSize(maxSerializedSize),
23 buffer(static_cast<U8*>(this->allocator->allocate(0, actualSize, recoverable)), actualSize)
24 {
25 // assert if allocator returns smaller size
26 FW_ASSERT(maxSerializedSize == actualSize,maxSerializedSize,actualSize);
27 FW_ASSERT(nullptr != buffer.getBuffAddr());
28 }
29
31 this->allocator->deallocate(0, this->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 NATIVE_INT_TYPE capacity = this->buffer.getBuffCapacity();
43 NATIVE_INT_TYPE length = capacity;
44 status = file.read(this->buffer.getBuffAddr(), length, false);
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->buffer.setBuffLen(static_cast<NATIVE_UINT_TYPE>(length));
54 FW_ASSERT(FW_SERIALIZE_OK == serStatus, serStatus);
55 serStatus = serializable.deserialize(this->buffer);
56 if(FW_SERIALIZE_OK != serStatus) {
58 }
59
61 }
62
63 SerializableFile::Status SerializableFile::save(const char* fileName, Serializable& serializable) {
64 this->reset();
65 SerializeStatus serStatus = serializable.serialize(this->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 NATIVE_INT_TYPE length = this->buffer.getBuffLength();
76 NATIVE_INT_TYPE size = length;
77 status = file.write(this->buffer.getBuffAddr(), length);
78 if( (Os::File::OP_OK != status) ||
79 (length != size) )
80 {
81 file.close();
82 return FILE_WRITE_ERROR;
83 }
84
85 file.close();
86
88 }
89
90 void SerializableFile::reset() {
91 this->buffer.resetSer();
92 this->buffer.resetDeser();
93 }
94}
#define FW_ASSERT(...)
Definition Assert.hpp:7
PlatformIntType NATIVE_INT_TYPE
Definition BasicTypes.h:51
uint8_t U8
8-bit unsigned integer
Definition BasicTypes.h:26
PlatformUIntType NATIVE_UINT_TYPE
Definition BasicTypes.h:52
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
void resetDeser()
reset deserialization to beginning
SerializeStatus setBuffLen(NATIVE_UINT_TYPE length)
sets buffer length manually after filling with data
void resetSer()
reset to beginning of buffer to reuse for serialization
NATIVE_UINT_TYPE getBuffLength() const
returns current buffer size
void close()
close file
Definition File.cpp:36
@ OP_OK
Operation was successful.
Definition File.hpp:24
Status write(const void *buffer, NATIVE_INT_TYPE &size, bool waitForDone=true)
write size; will return amount written or errno
Definition File.cpp:32
@ OPEN_WRITE
Open file for writing.
Definition File.hpp:16
@ OPEN_READ
Open file for reading.
Definition File.hpp:15
Status open(const char *fileName, Mode mode)
open file. Writing creates file if it doesn't exist
Definition File.cpp:12
Status read(void *buffer, NATIVE_INT_TYPE &size, bool waitForFull=true)
waitForFull = true to wait for all bytes to be read
Definition File.cpp:28
Definition Buffer.cpp:21
SerializeStatus
forward declaration for string
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.