blob: e29ffdf57fd30c805258c1cb269313e277d5c42b [file] [log] [blame]
Matt Spinler711d51d2019-11-06 09:36:51 -06001/**
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 Spinler03c1d912019-07-10 14:12:15 -050016#include "user_header.hpp"
17
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080018#include "json_utils.hpp"
Matt Spinler1a94cc32019-09-11 13:32:12 -050019#include "pel_types.hpp"
Aatirc1489352019-12-09 13:13:20 -060020#include "pel_values.hpp"
Matt Spinlerfdb6a202019-09-20 14:09:20 -050021#include "severity.hpp"
Matt Spinler1a94cc32019-09-11 13:32:12 -050022
Aatir Manzurad0e0472019-10-07 13:18:37 -050023#include <iostream>
Matt Spinler03c1d912019-07-10 14:12:15 -050024#include <phosphor-logging/log.hpp>
25
26namespace openpower
27{
28namespace pels
29{
30
Aatirc1489352019-12-09 13:13:20 -060031namespace pv = openpower::pels::pel_values;
Matt Spinler03c1d912019-07-10 14:12:15 -050032using namespace phosphor::logging;
33
Matt Spinlercf5a8d02019-09-05 12:58:53 -050034void UserHeader::unflatten(Stream& stream)
Matt Spinler03c1d912019-07-10 14:12:15 -050035{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050036 stream >> _header >> _eventSubsystem >> _eventScope >> _eventSeverity >>
37 _eventType >> _reserved4Byte1 >> _problemDomain >> _problemVector >>
Matt Spinlereb111442019-11-07 13:05:36 -060038 _actionFlags >> _states;
Matt Spinler03c1d912019-07-10 14:12:15 -050039}
40
Matt Spinler06885452019-11-06 10:35:42 -060041void UserHeader::flatten(Stream& stream) const
Matt Spinler03c1d912019-07-10 14:12:15 -050042{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050043 stream << _header << _eventSubsystem << _eventScope << _eventSeverity
44 << _eventType << _reserved4Byte1 << _problemDomain << _problemVector
Matt Spinlereb111442019-11-07 13:05:36 -060045 << _actionFlags << _states;
Matt Spinler03c1d912019-07-10 14:12:15 -050046}
47
Matt Spinlerfdb6a202019-09-20 14:09:20 -050048UserHeader::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 Spinlerf1e85e22019-11-01 11:31:31 -050069 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 Spinlerfdb6a202019-09-20 14:09:20 -050083
84 _reserved4Byte1 = 0;
85
86 // No uses for problem domain or vector
87 _problemDomain = 0;
88 _problemVector = 0;
89
Matt Spinlerf1e85e22019-11-01 11:31:31 -050090 // These will be cleaned up later in pel_rules::check()
Matt Spinlere07f9152019-11-01 10:48:36 -050091 _actionFlags = entry.actionFlags.value_or(0);
Matt Spinlerfdb6a202019-09-20 14:09:20 -050092
Matt Spinlereb111442019-11-07 13:05:36 -060093 _states = 0;
Matt Spinlerfdb6a202019-09-20 14:09:20 -050094
95 _valid = true;
96}
97
Matt Spinler03c1d912019-07-10 14:12:15 -050098UserHeader::UserHeader(Stream& pel)
99{
100 try
101 {
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500102 unflatten(pel);
Matt Spinler03c1d912019-07-10 14:12:15 -0500103 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
113void UserHeader::validate()
114{
115 bool failed = false;
Matt Spinler1a94cc32019-09-11 13:32:12 -0500116 if (header().id != static_cast<uint16_t>(SectionID::userHeader))
Matt Spinler03c1d912019-07-10 14:12:15 -0500117 {
118 log<level::ERR>("Invalid user header section ID",
119 entry("ID=0x%X", header().id));
120 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500121 }
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 Spinler03c1d912019-07-10 14:12:15 -0500128 }
129
130 _valid = (failed) ? false : true;
131}
132
Aatir Manzurad0e0472019-10-07 13:18:37 -0500133std::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 Isa600d15a2019-12-20 12:42:26 +0800139 std::vector<std::string> actionFlags;
Aatirc1489352019-12-09 13:13:20 -0600140 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 Isa600d15a2019-12-20 12:42:26 +0800144 actionFlags =
145 pv::getValuesBitwise(_actionFlags, pel_values::actionFlagsValues);
Matt Spinler455587e2020-01-15 14:31:52 -0600146
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 Manzurad0e0472019-10-07 13:18:37 -0500155 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 Isa600d15a2019-12-20 12:42:26 +0800163 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 Spinler455587e2020-01-15 14:31:52 -0600172 jsonInsert(uh, "Host Transmission", hostState, 1);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800173 uh.erase(uh.size() - 2);
Aatir Manzurad0e0472019-10-07 13:18:37 -0500174 return uh;
175}
Matt Spinler03c1d912019-07-10 14:12:15 -0500176} // namespace pels
177} // namespace openpower