| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 13 | #include "Fw/SerializableFile/SerializableFile.hpp" | ||
| 14 | #include "Fw/Types/Assert.hpp" | ||
| 15 | #include "Os/File.hpp" | ||
| 16 | |||
| 17 | namespace Fw { | ||
| 18 | |||
| 19 | 1 | SerializableFile::SerializableFile(MemAllocator* allocator, FwSizeType maxSerializedSize) | |
| 20 | 1 | : m_allocator(allocator), | |
| 21 | 1 | m_recoverable(false), // for compiler; not used | |
| 22 | 1 | m_actualSize(maxSerializedSize), | |
| 23 | 1 | m_buffer(static_cast<U8*>(this->m_allocator->allocate(0, m_actualSize, m_recoverable)), m_actualSize) { | |
| 24 | // assert if allocator returns smaller size | ||
| 25 | 1 | FW_ASSERT(maxSerializedSize == m_actualSize, static_cast<FwAssertArgType>(maxSerializedSize), | |
| 26 | static_cast<FwAssertArgType>(m_actualSize)); | ||
| 27 | 1 | FW_ASSERT(nullptr != m_buffer.getBuffAddr()); | |
| 28 | 1 | } | |
| 29 | |||
| 30 | 1 | SerializableFile::~SerializableFile() { | |
| 31 | 1 | this->m_allocator->deallocate(0, this->m_buffer.getBuffAddr()); | |
| 32 | 1 | } | |
| 33 | |||
| 34 | 2 | SerializableFile::Status SerializableFile::load(const char* fileName, Serializable& serializable) { | |
| 35 |
1/1✓ Branch 2 taken 2 times.
|
2 | Os::File file; |
| 36 | Os::File::Status status; | ||
| 37 |
1/1✓ Branch 2 taken 2 times.
|
2 | status = file.open(fileName, Os::File::OPEN_READ); |
| 38 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (Os::File::OP_OK != status) { |
| 39 | 1 | return FILE_OPEN_ERROR; | |
| 40 | } | ||
| 41 | |||
| 42 | 1 | FwSizeType length = this->m_buffer.getCapacity(); | |
| 43 |
1/1✓ Branch 8 taken 1 times.
|
1 | status = file.read(this->m_buffer.getBuffAddr(), length, Os::File::WaitType::NO_WAIT); |
| 44 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (Os::File::OP_OK != status) { |
| 45 | ✗ | file.close(); | |
| 46 | ✗ | return FILE_READ_ERROR; | |
| 47 | } | ||
| 48 |
1/1✓ Branch 2 taken 1 times.
|
1 | file.close(); |
| 49 | |||
| 50 |
1/1✓ Branch 2 taken 1 times.
|
1 | this->reset(); |
| 51 | SerializeStatus serStatus; | ||
| 52 |
1/1✓ Branch 6 taken 1 times.
|
1 | serStatus = this->m_buffer.setBuffLen(length); |
| 53 | 1 | FW_ASSERT(FW_SERIALIZE_OK == serStatus, serStatus); | |
| 54 |
1/1✓ Branch 10 taken 1 times.
|
1 | serStatus = serializable.deserializeFrom(this->m_buffer); |
| 55 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (FW_SERIALIZE_OK != serStatus) { |
| 56 | ✗ | return DESERIALIZATION_ERROR; | |
| 57 | } | ||
| 58 | |||
| 59 | 1 | return SerializableFile::OP_OK; | |
| 60 | 2 | } | |
| 61 | |||
| 62 | 2 | SerializableFile::Status SerializableFile::save(const char* fileName, Serializable& serializable) { | |
| 63 |
1/1✓ Branch 2 taken 2 times.
|
2 | this->reset(); |
| 64 |
1/1✓ Branch 9 taken 2 times.
|
2 | SerializeStatus serStatus = serializable.serializeTo(this->m_buffer); |
| 65 | 2 | FW_ASSERT(FW_SERIALIZE_OK == serStatus, serStatus); | |
| 66 | |||
| 67 |
1/1✓ Branch 2 taken 2 times.
|
2 | Os::File file; |
| 68 | Os::File::Status status; | ||
| 69 |
1/1✓ Branch 2 taken 2 times.
|
2 | status = file.open(fileName, Os::File::OPEN_WRITE); |
| 70 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (Os::File::OP_OK != status) { |
| 71 | 1 | return FILE_OPEN_ERROR; | |
| 72 | } | ||
| 73 | |||
| 74 |
1/1✓ Branch 7 taken 1 times.
|
1 | FwSizeType length = this->m_buffer.getSize(); |
| 75 |
1/1✓ Branch 8 taken 1 times.
|
1 | status = file.write(this->m_buffer.getBuffAddr(), length); |
| 76 |
4/7✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 times.
|
1 | if ((Os::File::OP_OK != status) || (length != this->m_buffer.getSize())) { |
| 77 | ✗ | file.close(); | |
| 78 | ✗ | return FILE_WRITE_ERROR; | |
| 79 | } | ||
| 80 | |||
| 81 |
1/1✓ Branch 2 taken 1 times.
|
1 | file.close(); |
| 82 | |||
| 83 | 1 | return SerializableFile::OP_OK; | |
| 84 | 2 | } | |
| 85 | |||
| 86 | 3 | void SerializableFile::reset() { | |
| 87 | 3 | this->m_buffer.resetSer(); //!< reset to beginning of buffer to reuse for serialization | |
| 88 | 3 | this->m_buffer.resetDeser(); //!< reset deserialization to beginning | |
| 89 | 3 | } | |
| 90 | } // namespace Fw | ||
| 91 |