blob: c52d4b593443efbac967c70f690eb3dc75580471 [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"
Matt Spinlerfdb6a202019-09-20 14:09:20 -050019#include "severity.hpp"
Matt Spinler1a94cc32019-09-11 13:32:12 -050020
Aatir Manzurad0e0472019-10-07 13:18:37 -050021#include <iostream>
Matt Spinler03c1d912019-07-10 14:12:15 -050022#include <phosphor-logging/log.hpp>
23
24namespace openpower
25{
26namespace pels
27{
28
29using namespace phosphor::logging;
30
Matt Spinlercf5a8d02019-09-05 12:58:53 -050031void UserHeader::unflatten(Stream& stream)
Matt Spinler03c1d912019-07-10 14:12:15 -050032{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050033 stream >> _header >> _eventSubsystem >> _eventScope >> _eventSeverity >>
34 _eventType >> _reserved4Byte1 >> _problemDomain >> _problemVector >>
35 _actionFlags >> _reserved4Byte2;
Matt Spinler03c1d912019-07-10 14:12:15 -050036}
37
Matt Spinler06885452019-11-06 10:35:42 -060038void UserHeader::flatten(Stream& stream) const
Matt Spinler03c1d912019-07-10 14:12:15 -050039{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050040 stream << _header << _eventSubsystem << _eventScope << _eventSeverity
41 << _eventType << _reserved4Byte1 << _problemDomain << _problemVector
42 << _actionFlags << _reserved4Byte2;
Matt Spinler03c1d912019-07-10 14:12:15 -050043}
44
Matt Spinlerfdb6a202019-09-20 14:09:20 -050045UserHeader::UserHeader(const message::Entry& entry,
46 phosphor::logging::Entry::Level severity)
47{
48 _header.id = static_cast<uint16_t>(SectionID::userHeader);
49 _header.size = UserHeader::flattenedSize();
50 _header.version = userHeaderVersion;
51 _header.subType = 0;
52 _header.componentID = static_cast<uint16_t>(ComponentID::phosphorLogging);
53
54 _eventSubsystem = entry.subsystem;
55
56 _eventScope = entry.eventScope.value_or(
57 static_cast<uint8_t>(EventScope::entirePlatform));
58
59 // Get the severity from the registry if it's there, otherwise get it
60 // from the OpenBMC event log severity value.
61 _eventSeverity =
62 entry.severity.value_or(convertOBMCSeverityToPEL(severity));
63
64 // TODO: ibm-dev/dev/#1144 Handle manufacturing sev & action flags
65
Matt Spinlerf1e85e22019-11-01 11:31:31 -050066 if (entry.eventType)
67 {
68 _eventType = *entry.eventType;
69 }
70 else
71 {
72 // There are different default event types for info errors
73 // vs non info ones.
74 auto sevType = static_cast<SeverityType>(_eventSeverity & 0xF0);
75
76 _eventType = (sevType == SeverityType::nonError)
77 ? static_cast<uint8_t>(EventType::miscInformational)
78 : static_cast<uint8_t>(EventType::notApplicable);
79 }
Matt Spinlerfdb6a202019-09-20 14:09:20 -050080
81 _reserved4Byte1 = 0;
82
83 // No uses for problem domain or vector
84 _problemDomain = 0;
85 _problemVector = 0;
86
Matt Spinlerf1e85e22019-11-01 11:31:31 -050087 // These will be cleaned up later in pel_rules::check()
Matt Spinlere07f9152019-11-01 10:48:36 -050088 _actionFlags = entry.actionFlags.value_or(0);
Matt Spinlerfdb6a202019-09-20 14:09:20 -050089
90 _reserved4Byte2 = 0;
91
92 _valid = true;
93}
94
Matt Spinler03c1d912019-07-10 14:12:15 -050095UserHeader::UserHeader(Stream& pel)
96{
97 try
98 {
Matt Spinlercf5a8d02019-09-05 12:58:53 -050099 unflatten(pel);
Matt Spinler03c1d912019-07-10 14:12:15 -0500100 validate();
101 }
102 catch (const std::exception& e)
103 {
104 log<level::ERR>("Cannot unflatten user header",
105 entry("ERROR=%s", e.what()));
106 _valid = false;
107 }
108}
109
110void UserHeader::validate()
111{
112 bool failed = false;
Matt Spinler1a94cc32019-09-11 13:32:12 -0500113 if (header().id != static_cast<uint16_t>(SectionID::userHeader))
Matt Spinler03c1d912019-07-10 14:12:15 -0500114 {
115 log<level::ERR>("Invalid user header section ID",
116 entry("ID=0x%X", header().id));
117 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500118 }
119
120 if (header().version != userHeaderVersion)
121 {
122 log<level::ERR>("Invalid user header version",
123 entry("VERSION=0x%X", header().version));
124 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500125 }
126
127 _valid = (failed) ? false : true;
128}
129
Aatir Manzurad0e0472019-10-07 13:18:37 -0500130std::string UserHeader::getValue(const uint8_t field,
131 const pel_values::PELValues& values) const
132{
133
134 auto tmp = pel_values::findByValue(field, values);
135 if (tmp != values.end())
136 {
137 return std::get<pel_values::registryNamePos>(*tmp);
138 }
139 else
140 {
141 return "invalid";
142 }
143}
144std::optional<std::string> UserHeader::getJSON() const
145{
146 std::string severity;
147 std::string subsystem;
148 std::string eventScope;
149 std::string eventType;
150 severity = getValue(_eventSeverity, pel_values::severityValues);
151 subsystem = getValue(_eventSubsystem, pel_values::subsystemValues);
152 eventScope = getValue(_eventScope, pel_values::eventScopeValues);
153 eventType = getValue(_eventType, pel_values::eventTypeValues);
154 char tmpUhVal[8];
155 sprintf(tmpUhVal, "%d", userHeaderVersion);
156 std::string uhVerStr(tmpUhVal);
157 sprintf(tmpUhVal, "0x%X", _header.componentID);
158 std::string uhCbStr(tmpUhVal);
159 sprintf(tmpUhVal, "%d", _header.subType);
160 std::string uhStStr(tmpUhVal);
161
162 std::string uh = "{\"Section Version\": \"" + uhVerStr +
163 "\"}, \n {\"Sub-section type\": \"" + uhStStr +
164 "\"}, \n "
165 "{\"Log Committed by\": \"" +
166 uhCbStr + "\"}, \n {\"Subsystem\": \"" + subsystem +
167 "\"},\n "
168 "{\"Event Scope\": \"" +
169 eventScope + "\"}, \n {\"Event Severity\":\"" + severity +
170 "\"},\n {\"Event Type\": \"" + eventType + "\"}";
171 return uh;
172}
Matt Spinler03c1d912019-07-10 14:12:15 -0500173} // namespace pels
174} // namespace openpower