xref: /openbmc/phosphor-logging/extensions/openpower-pels/failing_mtms.cpp (revision 40fb54935ce7367636a7156039396ee91cc4d5e2)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright 2019 IBM Corporation
3 
4 #include "failing_mtms.hpp"
5 
6 #include "json_utils.hpp"
7 #include "pel_types.hpp"
8 #include "pel_values.hpp"
9 
10 #include <phosphor-logging/lg2.hpp>
11 
12 namespace openpower
13 {
14 namespace pels
15 {
16 
17 namespace pv = openpower::pels::pel_values;
18 
19 static constexpr uint8_t failingMTMSVersion = 0x01;
20 
FailingMTMS(const DataInterfaceBase & dataIface)21 FailingMTMS::FailingMTMS(const DataInterfaceBase& dataIface) :
22     _mtms(dataIface.getMachineTypeModel(), dataIface.getMachineSerialNumber())
23 {
24     _header.id = static_cast<uint16_t>(SectionID::failingMTMS);
25     _header.size = FailingMTMS::flattenedSize();
26     _header.version = failingMTMSVersion;
27     _header.subType = 0;
28     _header.componentID = static_cast<uint16_t>(ComponentID::phosphorLogging);
29 
30     _valid = true;
31 }
32 
FailingMTMS(Stream & pel)33 FailingMTMS::FailingMTMS(Stream& pel)
34 {
35     try
36     {
37         unflatten(pel);
38         validate();
39     }
40     catch (const std::exception& e)
41     {
42         lg2::error("Cannot unflatten failing MTM section: {EXCEPTION}",
43                    "EXCEPTION", e);
44         _valid = false;
45     }
46 }
47 
validate()48 void FailingMTMS::validate()
49 {
50     bool failed = false;
51 
52     if (header().id != static_cast<uint16_t>(SectionID::failingMTMS))
53     {
54         lg2::error("Invalid failing MTMS section ID: {HEADER_ID}", "HEADER_ID",
55                    lg2::hex, header().id);
56         failed = true;
57     }
58 
59     if (header().version != failingMTMSVersion)
60     {
61         lg2::error("Invalid failing MTMS version: {HEADER_VERSION}",
62                    "HEADER_VERSION", lg2::hex, header().version);
63         failed = true;
64     }
65 
66     _valid = (failed) ? false : true;
67 }
68 
flatten(Stream & stream) const69 void FailingMTMS::flatten(Stream& stream) const
70 {
71     stream << _header << _mtms;
72 }
73 
unflatten(Stream & stream)74 void FailingMTMS::unflatten(Stream& stream)
75 {
76     stream >> _header >> _mtms;
77 }
78 
getJSON(uint8_t creatorID) const79 std::optional<std::string> FailingMTMS::getJSON(uint8_t creatorID) const
80 {
81     std::string json;
82     jsonInsert(json, pv::sectionVer, getNumberString("%d", _header.version), 1);
83     jsonInsert(json, pv::subSection, getNumberString("%d", _header.subType), 1);
84     jsonInsert(json, pv::createdBy,
85                getComponentName(_header.componentID, creatorID), 1);
86     jsonInsert(json, "Machine Type Model", _mtms.machineTypeAndModel(), 1);
87     jsonInsert(json, "Serial Number", trimEnd(_mtms.machineSerialNumber()), 1);
88     json.erase(json.size() - 2);
89     return json;
90 }
91 } // namespace pels
92 } // namespace openpower
93