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 | */ |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 16 | #include "user_header.hpp" |
| 17 | |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 18 | #include "json_utils.hpp" |
Matt Spinler | 1a94cc3 | 2019-09-11 13:32:12 -0500 | [diff] [blame] | 19 | #include "pel_types.hpp" |
Aatir | c148935 | 2019-12-09 13:13:20 -0600 | [diff] [blame] | 20 | #include "pel_values.hpp" |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 21 | #include "severity.hpp" |
Matt Spinler | 1a94cc3 | 2019-09-11 13:32:12 -0500 | [diff] [blame] | 22 | |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 23 | #include <iostream> |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 24 | #include <phosphor-logging/log.hpp> |
| 25 | |
| 26 | namespace openpower |
| 27 | { |
| 28 | namespace pels |
| 29 | { |
| 30 | |
Aatir | c148935 | 2019-12-09 13:13:20 -0600 | [diff] [blame] | 31 | namespace pv = openpower::pels::pel_values; |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 32 | using namespace phosphor::logging; |
| 33 | |
Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 34 | void UserHeader::unflatten(Stream& stream) |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 35 | { |
Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 36 | stream >> _header >> _eventSubsystem >> _eventScope >> _eventSeverity >> |
| 37 | _eventType >> _reserved4Byte1 >> _problemDomain >> _problemVector >> |
Matt Spinler | eb11144 | 2019-11-07 13:05:36 -0600 | [diff] [blame] | 38 | _actionFlags >> _states; |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 39 | } |
| 40 | |
Matt Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 41 | void UserHeader::flatten(Stream& stream) const |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 42 | { |
Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 43 | stream << _header << _eventSubsystem << _eventScope << _eventSeverity |
| 44 | << _eventType << _reserved4Byte1 << _problemDomain << _problemVector |
Matt Spinler | eb11144 | 2019-11-07 13:05:36 -0600 | [diff] [blame] | 45 | << _actionFlags << _states; |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 48 | UserHeader::UserHeader(const message::Entry& entry, |
| 49 | phosphor::logging::Entry::Level severity) |
| 50 | { |
| 51 | _header.id = static_cast<uint16_t>(SectionID::userHeader); |
| 52 | _header.size = UserHeader::flattenedSize(); |
| 53 | _header.version = userHeaderVersion; |
| 54 | _header.subType = 0; |
| 55 | _header.componentID = static_cast<uint16_t>(ComponentID::phosphorLogging); |
| 56 | |
| 57 | _eventSubsystem = entry.subsystem; |
| 58 | |
| 59 | _eventScope = entry.eventScope.value_or( |
| 60 | static_cast<uint8_t>(EventScope::entirePlatform)); |
| 61 | |
| 62 | // Get the severity from the registry if it's there, otherwise get it |
| 63 | // from the OpenBMC event log severity value. |
| 64 | _eventSeverity = |
| 65 | entry.severity.value_or(convertOBMCSeverityToPEL(severity)); |
| 66 | |
| 67 | // TODO: ibm-dev/dev/#1144 Handle manufacturing sev & action flags |
| 68 | |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 69 | if (entry.eventType) |
| 70 | { |
| 71 | _eventType = *entry.eventType; |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | // There are different default event types for info errors |
| 76 | // vs non info ones. |
| 77 | auto sevType = static_cast<SeverityType>(_eventSeverity & 0xF0); |
| 78 | |
| 79 | _eventType = (sevType == SeverityType::nonError) |
| 80 | ? static_cast<uint8_t>(EventType::miscInformational) |
| 81 | : static_cast<uint8_t>(EventType::notApplicable); |
| 82 | } |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 83 | |
| 84 | _reserved4Byte1 = 0; |
| 85 | |
| 86 | // No uses for problem domain or vector |
| 87 | _problemDomain = 0; |
| 88 | _problemVector = 0; |
| 89 | |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 90 | // These will be cleaned up later in pel_rules::check() |
Matt Spinler | e07f915 | 2019-11-01 10:48:36 -0500 | [diff] [blame] | 91 | _actionFlags = entry.actionFlags.value_or(0); |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 92 | |
Matt Spinler | eb11144 | 2019-11-07 13:05:36 -0600 | [diff] [blame] | 93 | _states = 0; |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 94 | |
| 95 | _valid = true; |
| 96 | } |
| 97 | |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 98 | UserHeader::UserHeader(Stream& pel) |
| 99 | { |
| 100 | try |
| 101 | { |
Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 102 | unflatten(pel); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 103 | validate(); |
| 104 | } |
| 105 | catch (const std::exception& e) |
| 106 | { |
| 107 | log<level::ERR>("Cannot unflatten user header", |
| 108 | entry("ERROR=%s", e.what())); |
| 109 | _valid = false; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void UserHeader::validate() |
| 114 | { |
| 115 | bool failed = false; |
Matt Spinler | 1a94cc3 | 2019-09-11 13:32:12 -0500 | [diff] [blame] | 116 | if (header().id != static_cast<uint16_t>(SectionID::userHeader)) |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 117 | { |
| 118 | log<level::ERR>("Invalid user header section ID", |
| 119 | entry("ID=0x%X", header().id)); |
| 120 | failed = true; |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | if (header().version != userHeaderVersion) |
| 124 | { |
| 125 | log<level::ERR>("Invalid user header version", |
| 126 | entry("VERSION=0x%X", header().version)); |
| 127 | failed = true; |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | _valid = (failed) ? false : true; |
| 131 | } |
| 132 | |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 133 | std::optional<std::string> UserHeader::getJSON() const |
| 134 | { |
| 135 | std::string severity; |
| 136 | std::string subsystem; |
| 137 | std::string eventScope; |
| 138 | std::string eventType; |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 139 | std::vector<std::string> actionFlags; |
Aatir | c148935 | 2019-12-09 13:13:20 -0600 | [diff] [blame] | 140 | severity = pv::getValue(_eventSeverity, pel_values::severityValues); |
| 141 | subsystem = pv::getValue(_eventSubsystem, pel_values::subsystemValues); |
| 142 | eventScope = pv::getValue(_eventScope, pel_values::eventScopeValues); |
| 143 | eventType = pv::getValue(_eventType, pel_values::eventTypeValues); |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 144 | actionFlags = |
| 145 | pv::getValuesBitwise(_actionFlags, pel_values::actionFlagsValues); |
Matt Spinler | 455587e | 2020-01-15 14:31:52 -0600 | [diff] [blame] | 146 | |
| 147 | std::string hostState{"Invalid"}; |
| 148 | auto iter = pv::transmissionStates.find( |
| 149 | static_cast<TransmissionState>(hostTransmissionState())); |
| 150 | if (iter != pv::transmissionStates.end()) |
| 151 | { |
| 152 | hostState = iter->second; |
| 153 | } |
| 154 | |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 155 | char tmpUhVal[8]; |
| 156 | sprintf(tmpUhVal, "%d", userHeaderVersion); |
| 157 | std::string uhVerStr(tmpUhVal); |
| 158 | sprintf(tmpUhVal, "0x%X", _header.componentID); |
| 159 | std::string uhCbStr(tmpUhVal); |
| 160 | sprintf(tmpUhVal, "%d", _header.subType); |
| 161 | std::string uhStStr(tmpUhVal); |
| 162 | |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 163 | std::string uh; |
| 164 | jsonInsert(uh, "Section Version", uhVerStr, 1); |
| 165 | jsonInsert(uh, "Sub-section type", uhStStr, 1); |
| 166 | jsonInsert(uh, "Log Committed by", uhCbStr, 1); |
| 167 | jsonInsert(uh, "Subsystem", subsystem, 1); |
| 168 | jsonInsert(uh, "Event Scope", eventScope, 1); |
| 169 | jsonInsert(uh, "Event Severity", severity, 1); |
| 170 | jsonInsert(uh, "Event Type", eventType, 1); |
| 171 | jsonInsertArray(uh, "Action Flags", actionFlags, 1); |
Matt Spinler | 455587e | 2020-01-15 14:31:52 -0600 | [diff] [blame] | 172 | jsonInsert(uh, "Host Transmission", hostState, 1); |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 173 | uh.erase(uh.size() - 2); |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 174 | return uh; |
| 175 | } |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 176 | } // namespace pels |
| 177 | } // namespace openpower |