blob: 5aef28dbb6ffcf511d0c5d86130708559737dfc1 [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
Matt Spinler1a94cc32019-09-11 13:32:12 -050018#include "pel_types.hpp"
Aatirc1489352019-12-09 13:13:20 -060019#include "pel_values.hpp"
Matt Spinlerfdb6a202019-09-20 14:09:20 -050020#include "severity.hpp"
Matt Spinler1a94cc32019-09-11 13:32:12 -050021
Aatir Manzurad0e0472019-10-07 13:18:37 -050022#include <iostream>
Matt Spinler03c1d912019-07-10 14:12:15 -050023#include <phosphor-logging/log.hpp>
24
25namespace openpower
26{
27namespace pels
28{
29
Aatirc1489352019-12-09 13:13:20 -060030namespace pv = openpower::pels::pel_values;
Matt Spinler03c1d912019-07-10 14:12:15 -050031using namespace phosphor::logging;
32
Matt Spinlercf5a8d02019-09-05 12:58:53 -050033void UserHeader::unflatten(Stream& stream)
Matt Spinler03c1d912019-07-10 14:12:15 -050034{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050035 stream >> _header >> _eventSubsystem >> _eventScope >> _eventSeverity >>
36 _eventType >> _reserved4Byte1 >> _problemDomain >> _problemVector >>
Matt Spinlereb111442019-11-07 13:05:36 -060037 _actionFlags >> _states;
Matt Spinler03c1d912019-07-10 14:12:15 -050038}
39
Matt Spinler06885452019-11-06 10:35:42 -060040void UserHeader::flatten(Stream& stream) const
Matt Spinler03c1d912019-07-10 14:12:15 -050041{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050042 stream << _header << _eventSubsystem << _eventScope << _eventSeverity
43 << _eventType << _reserved4Byte1 << _problemDomain << _problemVector
Matt Spinlereb111442019-11-07 13:05:36 -060044 << _actionFlags << _states;
Matt Spinler03c1d912019-07-10 14:12:15 -050045}
46
Matt Spinlerfdb6a202019-09-20 14:09:20 -050047UserHeader::UserHeader(const message::Entry& entry,
48 phosphor::logging::Entry::Level severity)
49{
50 _header.id = static_cast<uint16_t>(SectionID::userHeader);
51 _header.size = UserHeader::flattenedSize();
52 _header.version = userHeaderVersion;
53 _header.subType = 0;
54 _header.componentID = static_cast<uint16_t>(ComponentID::phosphorLogging);
55
56 _eventSubsystem = entry.subsystem;
57
58 _eventScope = entry.eventScope.value_or(
59 static_cast<uint8_t>(EventScope::entirePlatform));
60
61 // Get the severity from the registry if it's there, otherwise get it
62 // from the OpenBMC event log severity value.
63 _eventSeverity =
64 entry.severity.value_or(convertOBMCSeverityToPEL(severity));
65
66 // TODO: ibm-dev/dev/#1144 Handle manufacturing sev & action flags
67
Matt Spinlerf1e85e22019-11-01 11:31:31 -050068 if (entry.eventType)
69 {
70 _eventType = *entry.eventType;
71 }
72 else
73 {
74 // There are different default event types for info errors
75 // vs non info ones.
76 auto sevType = static_cast<SeverityType>(_eventSeverity & 0xF0);
77
78 _eventType = (sevType == SeverityType::nonError)
79 ? static_cast<uint8_t>(EventType::miscInformational)
80 : static_cast<uint8_t>(EventType::notApplicable);
81 }
Matt Spinlerfdb6a202019-09-20 14:09:20 -050082
83 _reserved4Byte1 = 0;
84
85 // No uses for problem domain or vector
86 _problemDomain = 0;
87 _problemVector = 0;
88
Matt Spinlerf1e85e22019-11-01 11:31:31 -050089 // These will be cleaned up later in pel_rules::check()
Matt Spinlere07f9152019-11-01 10:48:36 -050090 _actionFlags = entry.actionFlags.value_or(0);
Matt Spinlerfdb6a202019-09-20 14:09:20 -050091
Matt Spinlereb111442019-11-07 13:05:36 -060092 _states = 0;
Matt Spinlerfdb6a202019-09-20 14:09:20 -050093
94 _valid = true;
95}
96
Matt Spinler03c1d912019-07-10 14:12:15 -050097UserHeader::UserHeader(Stream& pel)
98{
99 try
100 {
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500101 unflatten(pel);
Matt Spinler03c1d912019-07-10 14:12:15 -0500102 validate();
103 }
104 catch (const std::exception& e)
105 {
106 log<level::ERR>("Cannot unflatten user header",
107 entry("ERROR=%s", e.what()));
108 _valid = false;
109 }
110}
111
112void UserHeader::validate()
113{
114 bool failed = false;
Matt Spinler1a94cc32019-09-11 13:32:12 -0500115 if (header().id != static_cast<uint16_t>(SectionID::userHeader))
Matt Spinler03c1d912019-07-10 14:12:15 -0500116 {
117 log<level::ERR>("Invalid user header section ID",
118 entry("ID=0x%X", header().id));
119 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500120 }
121
122 if (header().version != userHeaderVersion)
123 {
124 log<level::ERR>("Invalid user header version",
125 entry("VERSION=0x%X", header().version));
126 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500127 }
128
129 _valid = (failed) ? false : true;
130}
131
Aatir Manzurad0e0472019-10-07 13:18:37 -0500132std::optional<std::string> UserHeader::getJSON() const
133{
134 std::string severity;
135 std::string subsystem;
136 std::string eventScope;
137 std::string eventType;
Aatirc1489352019-12-09 13:13:20 -0600138 severity = pv::getValue(_eventSeverity, pel_values::severityValues);
139 subsystem = pv::getValue(_eventSubsystem, pel_values::subsystemValues);
140 eventScope = pv::getValue(_eventScope, pel_values::eventScopeValues);
141 eventType = pv::getValue(_eventType, pel_values::eventTypeValues);
Aatir Manzurad0e0472019-10-07 13:18:37 -0500142 char tmpUhVal[8];
143 sprintf(tmpUhVal, "%d", userHeaderVersion);
144 std::string uhVerStr(tmpUhVal);
145 sprintf(tmpUhVal, "0x%X", _header.componentID);
146 std::string uhCbStr(tmpUhVal);
147 sprintf(tmpUhVal, "%d", _header.subType);
148 std::string uhStStr(tmpUhVal);
149
150 std::string uh = "{\"Section Version\": \"" + uhVerStr +
151 "\"}, \n {\"Sub-section type\": \"" + uhStStr +
152 "\"}, \n "
153 "{\"Log Committed by\": \"" +
154 uhCbStr + "\"}, \n {\"Subsystem\": \"" + subsystem +
155 "\"},\n "
156 "{\"Event Scope\": \"" +
157 eventScope + "\"}, \n {\"Event Severity\":\"" + severity +
158 "\"},\n {\"Event Type\": \"" + eventType + "\"}";
159 return uh;
160}
Matt Spinler03c1d912019-07-10 14:12:15 -0500161} // namespace pels
162} // namespace openpower