Aatir Manzur | 51c9263 | 2019-09-06 13:30:48 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "section.hpp" |
| 4 | #include "stream.hpp" |
| 5 | |
| 6 | namespace openpower |
| 7 | { |
| 8 | namespace pels |
| 9 | { |
| 10 | |
| 11 | /** |
| 12 | * @class UserData |
| 13 | * |
| 14 | * This represents the User Data section in a PEL. It is free form data |
| 15 | * that the creator knows the contents of. The component ID, version, |
| 16 | * and sub-type fields in the section header are used to identify the |
| 17 | * format. |
| 18 | * |
| 19 | * The Section base class handles the section header structure that every |
| 20 | * PEL section has at offset zero. |
| 21 | */ |
| 22 | class UserData : public Section |
| 23 | { |
| 24 | public: |
| 25 | UserData() = delete; |
| 26 | ~UserData() = default; |
| 27 | UserData(const UserData&) = default; |
| 28 | UserData& operator=(const UserData&) = default; |
| 29 | UserData(UserData&&) = default; |
| 30 | UserData& operator=(UserData&&) = default; |
| 31 | |
| 32 | /** |
| 33 | * @brief Constructor |
| 34 | * |
| 35 | * Fills in this class's data fields from the stream. |
| 36 | * |
| 37 | * @param[in] pel - the PEL data stream |
| 38 | */ |
| 39 | explicit UserData(Stream& pel); |
| 40 | |
| 41 | /** |
| 42 | * @brief Constructor |
| 43 | * |
| 44 | * Create a valid UserData object with the passed in data. |
| 45 | * |
| 46 | * The component ID, subtype, and version are used to identify |
| 47 | * the data to know which parser to call. |
| 48 | * |
| 49 | * @param[in] componentID - Component ID of the creator |
| 50 | * @param[in] subType - The type of user data |
| 51 | * @param[in] version - The version of the data |
| 52 | */ |
| 53 | UserData(uint16_t componentID, uint8_t subType, uint8_t version, |
| 54 | const std::vector<uint8_t>& data); |
| 55 | |
| 56 | /** |
| 57 | * @brief Flatten the section into the stream |
| 58 | * |
| 59 | * @param[in] stream - The stream to write to |
| 60 | */ |
Matt Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 61 | void flatten(Stream& stream) const override; |
Aatir Manzur | 51c9263 | 2019-09-06 13:30:48 -0500 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * @brief Returns the size of this section when flattened into a PEL |
| 65 | * |
| 66 | * @return size_t - the size of the section |
| 67 | */ |
| 68 | size_t flattenedSize() |
| 69 | { |
| 70 | return Section::flattenedSize() + _data.size(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @brief Returns the raw section data |
| 75 | * |
| 76 | * @return std::vector<uint8_t>& |
| 77 | */ |
| 78 | const std::vector<uint8_t>& data() const |
| 79 | { |
| 80 | return _data; |
| 81 | } |
| 82 | |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 83 | /** |
| 84 | * @brief Get the section contents in JSON |
| 85 | * |
| 86 | * @return The JSON as a string if a parser was found, |
| 87 | * otherwise std::nullopt. |
| 88 | */ |
| 89 | std::optional<std::string> getJSON() const override; |
| 90 | |
Matt Spinler | cbf3c4d | 2020-03-24 15:34:52 -0500 | [diff] [blame] | 91 | /** |
| 92 | * @brief Shrink the section |
| 93 | * |
| 94 | * The new size must be between the current size and the minimum |
| 95 | * size, which is 12 bytes. If it isn't a 4 byte aligned value |
| 96 | * the code will do the aligning before the resize takes place. |
| 97 | * |
| 98 | * @param[in] newSize - The new size in bytes |
| 99 | * |
| 100 | * @return bool - true if successful, false else. |
| 101 | */ |
| 102 | bool shrink(size_t newSize) override; |
| 103 | |
Aatir Manzur | 51c9263 | 2019-09-06 13:30:48 -0500 | [diff] [blame] | 104 | private: |
| 105 | /** |
| 106 | * @brief Fills in the object from the stream data |
| 107 | * |
| 108 | * @param[in] stream - The stream to read from |
| 109 | */ |
| 110 | void unflatten(Stream& stream); |
| 111 | |
| 112 | /** |
| 113 | * @brief Validates the section contents |
| 114 | * |
| 115 | * Updates _valid (in Section) with the results. |
| 116 | */ |
| 117 | void validate() override; |
| 118 | |
| 119 | /** |
| 120 | * @brief The section data |
| 121 | */ |
| 122 | std::vector<uint8_t> _data; |
| 123 | }; |
| 124 | |
| 125 | } // namespace pels |
| 126 | } // namespace openpower |