blob: 386f4964c00f76ec2bb1352f1ab299e681dedecd [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 Spinler09d64002019-09-11 14:29:46 -050016#include "failing_mtms.hpp"
17
Harisuddin Mohamed Isae2d1bf32020-02-06 17:32:38 +080018#include "json_utils.hpp"
Matt Spinler09d64002019-09-11 14:29:46 -050019#include "pel_types.hpp"
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +080020#include "pel_values.hpp"
Matt Spinler09d64002019-09-11 14:29:46 -050021
22#include <phosphor-logging/log.hpp>
23
24namespace openpower
25{
26namespace pels
27{
28
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +080029namespace pv = openpower::pels::pel_values;
Matt Spinler09d64002019-09-11 14:29:46 -050030using namespace phosphor::logging;
31static constexpr uint8_t failingMTMSVersion = 0x01;
32
33FailingMTMS::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);
Matt Spinleraa659472019-10-23 09:26:48 -050041
42 _valid = true;
Matt Spinler09d64002019-09-11 14:29:46 -050043}
44
45FailingMTMS::FailingMTMS(Stream& pel)
46{
47 try
48 {
49 unflatten(pel);
50 validate();
51 }
52 catch (std::exception& e)
53 {
54 log<level::ERR>("Cannot unflatten failing MTM section",
55 entry("ERROR=%s", e.what()));
56 _valid = false;
57 }
58}
59
60void FailingMTMS::validate()
61{
62 bool failed = false;
63
64 if (header().id != static_cast<uint16_t>(SectionID::failingMTMS))
65 {
66 log<level::ERR>("Invalid failing MTMS section ID",
67 entry("ID=0x%X", header().id));
68 failed = true;
69 }
70
71 if (header().version != failingMTMSVersion)
72 {
73 log<level::ERR>("Invalid failing MTMS version",
74 entry("VERSION=0x%X", header().version));
75 failed = true;
76 }
77
78 _valid = (failed) ? false : true;
79}
80
Matt Spinler06885452019-11-06 10:35:42 -060081void FailingMTMS::flatten(Stream& stream) const
Matt Spinler09d64002019-09-11 14:29:46 -050082{
83 stream << _header << _mtms;
84}
85
86void FailingMTMS::unflatten(Stream& stream)
87{
88 stream >> _header >> _mtms;
89}
90
Harisuddin Mohamed Isae2d1bf32020-02-06 17:32:38 +080091std::optional<std::string> FailingMTMS::getJSON() const
92{
93 std::string json;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +080094 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 getNumberString("0x%X", _header.componentID), 1);
Harisuddin Mohamed Isae2d1bf32020-02-06 17:32:38 +080098 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}
Matt Spinler09d64002019-09-11 14:29:46 -0500103} // namespace pels
104} // namespace openpower