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