Matt Spinler | 711d51d | 2019-11-06 09:36:51 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2019 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Aatir Manzur | 51c9263 | 2019-09-06 13:30:48 -0500 | [diff] [blame] | 16 | #include "user_data.hpp" |
| 17 | |
| 18 | #include "pel_types.hpp" |
| 19 | |
| 20 | #include <phosphor-logging/log.hpp> |
| 21 | |
| 22 | namespace openpower |
| 23 | { |
| 24 | namespace pels |
| 25 | { |
| 26 | |
| 27 | using namespace phosphor::logging; |
| 28 | |
| 29 | void UserData::unflatten(Stream& stream) |
| 30 | { |
| 31 | stream >> _header; |
| 32 | |
| 33 | if (_header.size <= SectionHeader::flattenedSize()) |
| 34 | { |
| 35 | throw std::out_of_range( |
| 36 | "UserData::unflatten: SectionHeader::size field too small"); |
| 37 | } |
| 38 | |
| 39 | size_t dataLength = _header.size - SectionHeader::flattenedSize(); |
| 40 | _data.resize(dataLength); |
| 41 | |
| 42 | stream >> _data; |
| 43 | } |
| 44 | |
Matt Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 45 | void UserData::flatten(Stream& stream) const |
Aatir Manzur | 51c9263 | 2019-09-06 13:30:48 -0500 | [diff] [blame] | 46 | { |
| 47 | stream << _header << _data; |
| 48 | } |
| 49 | |
| 50 | UserData::UserData(Stream& pel) |
| 51 | { |
| 52 | try |
| 53 | { |
| 54 | unflatten(pel); |
| 55 | validate(); |
| 56 | } |
| 57 | catch (const std::exception& e) |
| 58 | { |
| 59 | log<level::ERR>("Cannot unflatten user data", |
| 60 | entry("ERROR=%s", e.what())); |
| 61 | _valid = false; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | UserData::UserData(uint16_t componentID, uint8_t subType, uint8_t version, |
| 66 | const std::vector<uint8_t>& data) |
| 67 | { |
| 68 | _header.id = static_cast<uint16_t>(SectionID::userData); |
| 69 | _header.size = Section::flattenedSize() + data.size(); |
| 70 | _header.version = version; |
| 71 | _header.subType = subType; |
| 72 | _header.componentID = componentID; |
| 73 | |
| 74 | _data = data; |
| 75 | |
| 76 | _valid = true; |
| 77 | } |
| 78 | |
| 79 | void UserData::validate() |
| 80 | { |
| 81 | if (header().id != static_cast<uint16_t>(SectionID::userData)) |
| 82 | { |
| 83 | log<level::ERR>("Invalid user data section ID", |
| 84 | entry("ID=0x%X", header().id)); |
| 85 | _valid = false; |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | _valid = true; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | } // namespace pels |
| 94 | } // namespace openpower |