blob: 7355e0865054dd19ac8920307540787e433ebf73 [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
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -050024#include <format>
25
Matt Spinler09d64002019-09-11 14:29:46 -050026namespace openpower
27{
28namespace pels
29{
30
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +080031namespace pv = openpower::pels::pel_values;
Matt Spinler09d64002019-09-11 14:29:46 -050032using namespace phosphor::logging;
33static constexpr uint8_t failingMTMSVersion = 0x01;
34
35FailingMTMS::FailingMTMS(const DataInterfaceBase& dataIface) :
36 _mtms(dataIface.getMachineTypeModel(), dataIface.getMachineSerialNumber())
37{
38 _header.id = static_cast<uint16_t>(SectionID::failingMTMS);
39 _header.size = FailingMTMS::flattenedSize();
40 _header.version = failingMTMSVersion;
41 _header.subType = 0;
42 _header.componentID = static_cast<uint16_t>(ComponentID::phosphorLogging);
Matt Spinleraa659472019-10-23 09:26:48 -050043
44 _valid = true;
Matt Spinler09d64002019-09-11 14:29:46 -050045}
46
47FailingMTMS::FailingMTMS(Stream& pel)
48{
49 try
50 {
51 unflatten(pel);
52 validate();
53 }
Patrick Williams66491c62021-10-06 12:23:37 -050054 catch (const std::exception& e)
Matt Spinler09d64002019-09-11 14:29:46 -050055 {
Matt Spinler8ac20502023-01-04 11:03:58 -060056 log<level::ERR>(
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -050057 std::format("Cannot unflatten failing MTM section: {}", e.what())
Matt Spinler8ac20502023-01-04 11:03:58 -060058 .c_str());
Matt Spinler09d64002019-09-11 14:29:46 -050059 _valid = false;
60 }
61}
62
63void FailingMTMS::validate()
64{
65 bool failed = false;
66
67 if (header().id != static_cast<uint16_t>(SectionID::failingMTMS))
68 {
Matt Spinler8ac20502023-01-04 11:03:58 -060069 log<level::ERR>(
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -050070 std::format("Invalid failing MTMS section ID: {0:#x}", header().id)
Matt Spinler8ac20502023-01-04 11:03:58 -060071 .c_str());
Matt Spinler09d64002019-09-11 14:29:46 -050072 failed = true;
73 }
74
75 if (header().version != failingMTMSVersion)
76 {
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -050077 log<level::ERR>(std::format("Invalid failing MTMS version: {0:#x}",
Matt Spinler8ac20502023-01-04 11:03:58 -060078 header().version)
79 .c_str());
Matt Spinler09d64002019-09-11 14:29:46 -050080 failed = true;
81 }
82
83 _valid = (failed) ? false : true;
84}
85
Matt Spinler06885452019-11-06 10:35:42 -060086void FailingMTMS::flatten(Stream& stream) const
Matt Spinler09d64002019-09-11 14:29:46 -050087{
88 stream << _header << _mtms;
89}
90
91void FailingMTMS::unflatten(Stream& stream)
92{
93 stream >> _header >> _mtms;
94}
95
Matt Spinlerb832aa52023-03-21 15:32:34 -050096std::optional<std::string> FailingMTMS::getJSON(uint8_t creatorID) const
Harisuddin Mohamed Isae2d1bf32020-02-06 17:32:38 +080097{
98 std::string json;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +080099 jsonInsert(json, pv::sectionVer, getNumberString("%d", _header.version), 1);
100 jsonInsert(json, pv::subSection, getNumberString("%d", _header.subType), 1);
101 jsonInsert(json, pv::createdBy,
Matt Spinlerb832aa52023-03-21 15:32:34 -0500102 getComponentName(_header.componentID, creatorID), 1);
Harisuddin Mohamed Isae2d1bf32020-02-06 17:32:38 +0800103 jsonInsert(json, "Machine Type Model", _mtms.machineTypeAndModel(), 1);
104 jsonInsert(json, "Serial Number", trimEnd(_mtms.machineSerialNumber()), 1);
105 json.erase(json.size() - 2);
106 return json;
107}
Matt Spinler09d64002019-09-11 14:29:46 -0500108} // namespace pels
109} // namespace openpower