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 */
16 #include "failing_mtms.hpp"
17
18 #include "json_utils.hpp"
19 #include "pel_types.hpp"
20 #include "pel_values.hpp"
21
22 #include <phosphor-logging/lg2.hpp>
23
24 namespace openpower
25 {
26 namespace pels
27 {
28
29 namespace pv = openpower::pels::pel_values;
30
31 static constexpr uint8_t failingMTMSVersion = 0x01;
32
FailingMTMS(const DataInterfaceBase & dataIface)33 FailingMTMS::FailingMTMS(const DataInterfaceBase& dataIface) :
34 _mtms(dataIface.getMachineTypeModel(), dataIface.getMachineSerialNumber())
35 {
36 _header.id = static_cast<uint16_t>(SectionID::failingMTMS);
37 _header.size = FailingMTMS::flattenedSize();
38 _header.version = failingMTMSVersion;
39 _header.subType = 0;
40 _header.componentID = static_cast<uint16_t>(ComponentID::phosphorLogging);
41
42 _valid = true;
43 }
44
FailingMTMS(Stream & pel)45 FailingMTMS::FailingMTMS(Stream& pel)
46 {
47 try
48 {
49 unflatten(pel);
50 validate();
51 }
52 catch (const std::exception& e)
53 {
54 lg2::error("Cannot unflatten failing MTM section: {EXCEPTION}",
55 "EXCEPTION", e);
56 _valid = false;
57 }
58 }
59
validate()60 void FailingMTMS::validate()
61 {
62 bool failed = false;
63
64 if (header().id != static_cast<uint16_t>(SectionID::failingMTMS))
65 {
66 lg2::error("Invalid failing MTMS section ID: {HEADER_ID}", "HEADER_ID",
67 lg2::hex, header().id);
68 failed = true;
69 }
70
71 if (header().version != failingMTMSVersion)
72 {
73 lg2::error("Invalid failing MTMS version: {HEADER_VERSION}",
74 "HEADER_VERSION", lg2::hex, header().version);
75 failed = true;
76 }
77
78 _valid = (failed) ? false : true;
79 }
80
flatten(Stream & stream) const81 void FailingMTMS::flatten(Stream& stream) const
82 {
83 stream << _header << _mtms;
84 }
85
unflatten(Stream & stream)86 void FailingMTMS::unflatten(Stream& stream)
87 {
88 stream >> _header >> _mtms;
89 }
90
getJSON(uint8_t creatorID) const91 std::optional<std::string> FailingMTMS::getJSON(uint8_t creatorID) const
92 {
93 std::string json;
94 jsonInsert(json, pv::sectionVer, getNumberString("%d", _header.version), 1);
95 jsonInsert(json, pv::subSection, getNumberString("%d", _header.subType), 1);
96 jsonInsert(json, pv::createdBy,
97 getComponentName(_header.componentID, creatorID), 1);
98 jsonInsert(json, "Machine Type Model", _mtms.machineTypeAndModel(), 1);
99 jsonInsert(json, "Serial Number", trimEnd(_mtms.machineSerialNumber()), 1);
100 json.erase(json.size() - 2);
101 return json;
102 }
103 } // namespace pels
104 } // namespace openpower
105