Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 1 | #include "pel.hpp" |
| 2 | |
| 3 | #include "bcd_time.hpp" |
| 4 | #include "log_id.hpp" |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 5 | #include "section_factory.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 6 | #include "stream.hpp" |
| 7 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/log.hpp> |
| 9 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 10 | namespace openpower |
| 11 | { |
| 12 | namespace pels |
| 13 | { |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 14 | namespace message = openpower::pels::message; |
| 15 | |
| 16 | PEL::PEL(const message::Entry& entry, uint32_t obmcLogID, uint64_t timestamp, |
| 17 | phosphor::logging::Entry::Level severity) |
| 18 | { |
| 19 | _ph = std::make_unique<PrivateHeader>(entry.componentID, obmcLogID, |
| 20 | timestamp); |
| 21 | _uh = std::make_unique<UserHeader>(entry, severity); |
| 22 | |
| 23 | // Add future sections here and update the count |
| 24 | |
| 25 | _ph->sectionCount() = 2; |
| 26 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 27 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 28 | PEL::PEL(std::vector<uint8_t>& data) : PEL(data, 0) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 29 | { |
| 30 | } |
| 31 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 32 | PEL::PEL(std::vector<uint8_t>& data, uint32_t obmcLogID) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 33 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 34 | populateFromRawData(data, obmcLogID); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 35 | } |
| 36 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 37 | void PEL::populateFromRawData(std::vector<uint8_t>& data, uint32_t obmcLogID) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 38 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 39 | Stream pelData{data}; |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 40 | _ph = std::make_unique<PrivateHeader>(pelData); |
| 41 | if (obmcLogID != 0) |
| 42 | { |
| 43 | _ph->obmcLogID() = obmcLogID; |
| 44 | } |
| 45 | |
| 46 | _uh = std::make_unique<UserHeader>(pelData); |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 47 | |
| 48 | // Use the section factory to create the rest of the objects |
| 49 | for (size_t i = 2; i < _ph->sectionCount(); i++) |
| 50 | { |
| 51 | auto section = section_factory::create(pelData); |
| 52 | _optionalSections.push_back(std::move(section)); |
| 53 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | bool PEL::valid() const |
| 57 | { |
| 58 | bool valid = _ph->valid(); |
| 59 | |
| 60 | if (valid) |
| 61 | { |
| 62 | valid = _uh->valid(); |
| 63 | } |
| 64 | |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 65 | if (valid) |
| 66 | { |
| 67 | if (!std::all_of(_optionalSections.begin(), _optionalSections.end(), |
| 68 | [](const auto& section) { return section->valid(); })) |
| 69 | { |
| 70 | valid = false; |
| 71 | } |
| 72 | } |
| 73 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 74 | return valid; |
| 75 | } |
| 76 | |
| 77 | void PEL::setCommitTime() |
| 78 | { |
| 79 | auto now = std::chrono::system_clock::now(); |
| 80 | _ph->commitTimestamp() = getBCDTime(now); |
| 81 | } |
| 82 | |
| 83 | void PEL::assignID() |
| 84 | { |
| 85 | _ph->id() = generatePELID(); |
| 86 | } |
| 87 | |
| 88 | void PEL::flatten(std::vector<uint8_t>& pelBuffer) |
| 89 | { |
| 90 | Stream pelData{pelBuffer}; |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 91 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 92 | if (!valid()) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 93 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 94 | using namespace phosphor::logging; |
| 95 | log<level::WARNING>("Unflattening an invalid PEL"); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 96 | } |
| 97 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 98 | _ph->flatten(pelData); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 99 | _uh->flatten(pelData); |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 100 | |
| 101 | for (auto& section : _optionalSections) |
| 102 | { |
| 103 | section->flatten(pelData); |
| 104 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | std::vector<uint8_t> PEL::data() |
| 108 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 109 | std::vector<uint8_t> pelData; |
| 110 | flatten(pelData); |
| 111 | return pelData; |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | } // namespace pels |
| 115 | } // namespace openpower |