Aatir Manzur | 51c9263 | 2019-09-06 13:30:48 -0500 | [diff] [blame] | 1 | #include "user_data.hpp" |
| 2 | |
| 3 | #include "pel_types.hpp" |
| 4 | |
| 5 | #include <phosphor-logging/log.hpp> |
| 6 | |
| 7 | namespace openpower |
| 8 | { |
| 9 | namespace pels |
| 10 | { |
| 11 | |
| 12 | using namespace phosphor::logging; |
| 13 | |
| 14 | void UserData::unflatten(Stream& stream) |
| 15 | { |
| 16 | stream >> _header; |
| 17 | |
| 18 | if (_header.size <= SectionHeader::flattenedSize()) |
| 19 | { |
| 20 | throw std::out_of_range( |
| 21 | "UserData::unflatten: SectionHeader::size field too small"); |
| 22 | } |
| 23 | |
| 24 | size_t dataLength = _header.size - SectionHeader::flattenedSize(); |
| 25 | _data.resize(dataLength); |
| 26 | |
| 27 | stream >> _data; |
| 28 | } |
| 29 | |
| 30 | void UserData::flatten(Stream& stream) |
| 31 | { |
| 32 | stream << _header << _data; |
| 33 | } |
| 34 | |
| 35 | UserData::UserData(Stream& pel) |
| 36 | { |
| 37 | try |
| 38 | { |
| 39 | unflatten(pel); |
| 40 | validate(); |
| 41 | } |
| 42 | catch (const std::exception& e) |
| 43 | { |
| 44 | log<level::ERR>("Cannot unflatten user data", |
| 45 | entry("ERROR=%s", e.what())); |
| 46 | _valid = false; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | UserData::UserData(uint16_t componentID, uint8_t subType, uint8_t version, |
| 51 | const std::vector<uint8_t>& data) |
| 52 | { |
| 53 | _header.id = static_cast<uint16_t>(SectionID::userData); |
| 54 | _header.size = Section::flattenedSize() + data.size(); |
| 55 | _header.version = version; |
| 56 | _header.subType = subType; |
| 57 | _header.componentID = componentID; |
| 58 | |
| 59 | _data = data; |
| 60 | |
| 61 | _valid = true; |
| 62 | } |
| 63 | |
| 64 | void UserData::validate() |
| 65 | { |
| 66 | if (header().id != static_cast<uint16_t>(SectionID::userData)) |
| 67 | { |
| 68 | log<level::ERR>("Invalid user data section ID", |
| 69 | entry("ID=0x%X", header().id)); |
| 70 | _valid = false; |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | _valid = true; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | } // namespace pels |
| 79 | } // namespace openpower |