Matt Spinler | 711d51d | 2019-11-06 09:36:51 -0600 | [diff] [blame] | 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 | */ |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 16 | #include "pel.hpp" |
| 17 | |
| 18 | #include "bcd_time.hpp" |
Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 19 | #include "extended_user_header.hpp" |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 20 | #include "failing_mtms.hpp" |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 21 | #include "json_utils.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 22 | #include "log_id.hpp" |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 23 | #include "pel_rules.hpp" |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 24 | #include "pel_values.hpp" |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 25 | #include "section_factory.hpp" |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 26 | #include "src.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 27 | #include "stream.hpp" |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 28 | #include "user_data_formats.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 29 | |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 30 | #include <iostream> |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 31 | #include <phosphor-logging/log.hpp> |
| 32 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 33 | namespace openpower |
| 34 | { |
| 35 | namespace pels |
| 36 | { |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 37 | namespace message = openpower::pels::message; |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 38 | namespace pv = openpower::pels::pel_values; |
Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 39 | using namespace phosphor::logging; |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 40 | |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 41 | constexpr auto unknownValue = "Unknown"; |
| 42 | |
Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 43 | PEL::PEL(const message::Entry& regEntry, uint32_t obmcLogID, uint64_t timestamp, |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 44 | phosphor::logging::Entry::Level severity, |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 45 | const AdditionalData& additionalData, |
| 46 | const DataInterfaceBase& dataIface) |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 47 | { |
Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 48 | _ph = std::make_unique<PrivateHeader>(regEntry.componentID, obmcLogID, |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 49 | timestamp); |
Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 50 | _uh = std::make_unique<UserHeader>(regEntry, severity); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 51 | |
Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 52 | auto src = std::make_unique<SRC>(regEntry, additionalData, dataIface); |
Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 53 | |
Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 54 | auto euh = std::make_unique<ExtendedUserHeader>(dataIface, regEntry, *src); |
Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 55 | |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 56 | _optionalSections.push_back(std::move(src)); |
Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 57 | _optionalSections.push_back(std::move(euh)); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 58 | |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 59 | auto mtms = std::make_unique<FailingMTMS>(dataIface); |
| 60 | _optionalSections.push_back(std::move(mtms)); |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 61 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 62 | auto ud = util::makeSysInfoUserDataSection(additionalData, dataIface); |
| 63 | _optionalSections.push_back(std::move(ud)); |
| 64 | |
Matt Spinler | 9b7e94f | 2020-03-24 15:44:41 -0500 | [diff] [blame] | 65 | // Create a UserData section from AdditionalData. |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 66 | if (!additionalData.empty()) |
| 67 | { |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 68 | ud = util::makeADUserDataSection(additionalData); |
Matt Spinler | 6d66382 | 2020-01-22 14:50:46 -0600 | [diff] [blame] | 69 | |
Matt Spinler | 9b7e94f | 2020-03-24 15:44:41 -0500 | [diff] [blame] | 70 | // Shrink the section if necessary. |
| 71 | if (size() + ud->header().size > _maxPELSize) |
| 72 | { |
| 73 | if (ud->shrink(_maxPELSize - size())) |
| 74 | { |
| 75 | _optionalSections.push_back(std::move(ud)); |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | log<level::WARNING>( |
| 80 | "Dropping AdditionalData UserData section", |
| 81 | entry("SECTION_SIZE=%d\n", ud->header().size)); |
| 82 | } |
| 83 | } |
| 84 | else |
Matt Spinler | 6d66382 | 2020-01-22 14:50:46 -0600 | [diff] [blame] | 85 | { |
| 86 | _optionalSections.push_back(std::move(ud)); |
| 87 | } |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 88 | } |
| 89 | |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 90 | _ph->setSectionCount(2 + _optionalSections.size()); |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 91 | |
| 92 | checkRulesAndFix(); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 93 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 94 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 95 | PEL::PEL(std::vector<uint8_t>& data) : PEL(data, 0) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 96 | { |
| 97 | } |
| 98 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 99 | PEL::PEL(std::vector<uint8_t>& data, uint32_t obmcLogID) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 100 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 101 | populateFromRawData(data, obmcLogID); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 102 | } |
| 103 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 104 | void PEL::populateFromRawData(std::vector<uint8_t>& data, uint32_t obmcLogID) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 105 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 106 | Stream pelData{data}; |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 107 | _ph = std::make_unique<PrivateHeader>(pelData); |
| 108 | if (obmcLogID != 0) |
| 109 | { |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 110 | _ph->setOBMCLogID(obmcLogID); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | _uh = std::make_unique<UserHeader>(pelData); |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 114 | |
| 115 | // Use the section factory to create the rest of the objects |
| 116 | for (size_t i = 2; i < _ph->sectionCount(); i++) |
| 117 | { |
| 118 | auto section = section_factory::create(pelData); |
| 119 | _optionalSections.push_back(std::move(section)); |
| 120 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | bool PEL::valid() const |
| 124 | { |
| 125 | bool valid = _ph->valid(); |
| 126 | |
| 127 | if (valid) |
| 128 | { |
| 129 | valid = _uh->valid(); |
| 130 | } |
| 131 | |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 132 | if (valid) |
| 133 | { |
| 134 | if (!std::all_of(_optionalSections.begin(), _optionalSections.end(), |
| 135 | [](const auto& section) { return section->valid(); })) |
| 136 | { |
| 137 | valid = false; |
| 138 | } |
| 139 | } |
| 140 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 141 | return valid; |
| 142 | } |
| 143 | |
| 144 | void PEL::setCommitTime() |
| 145 | { |
| 146 | auto now = std::chrono::system_clock::now(); |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 147 | _ph->setCommitTimestamp(getBCDTime(now)); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void PEL::assignID() |
| 151 | { |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 152 | _ph->setID(generatePELID()); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 153 | } |
| 154 | |
Matt Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 155 | void PEL::flatten(std::vector<uint8_t>& pelBuffer) const |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 156 | { |
| 157 | Stream pelData{pelBuffer}; |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 158 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 159 | if (!valid()) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 160 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 161 | log<level::WARNING>("Unflattening an invalid PEL"); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 162 | } |
| 163 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 164 | _ph->flatten(pelData); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 165 | _uh->flatten(pelData); |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 166 | |
| 167 | for (auto& section : _optionalSections) |
| 168 | { |
| 169 | section->flatten(pelData); |
| 170 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 171 | } |
| 172 | |
Matt Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 173 | std::vector<uint8_t> PEL::data() const |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 174 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 175 | std::vector<uint8_t> pelData; |
| 176 | flatten(pelData); |
| 177 | return pelData; |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 178 | } |
| 179 | |
Matt Spinler | f1b46ff | 2020-01-22 14:10:04 -0600 | [diff] [blame] | 180 | size_t PEL::size() const |
| 181 | { |
| 182 | size_t size = 0; |
| 183 | |
| 184 | if (_ph) |
| 185 | { |
| 186 | size += _ph->header().size; |
| 187 | } |
| 188 | |
| 189 | if (_uh) |
| 190 | { |
| 191 | size += _uh->header().size; |
| 192 | } |
| 193 | |
| 194 | for (const auto& section : _optionalSections) |
| 195 | { |
| 196 | size += section->header().size; |
| 197 | } |
| 198 | |
| 199 | return size; |
| 200 | } |
| 201 | |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 202 | std::optional<SRC*> PEL::primarySRC() const |
| 203 | { |
| 204 | auto src = std::find_if( |
| 205 | _optionalSections.begin(), _optionalSections.end(), [](auto& section) { |
| 206 | return section->header().id == |
| 207 | static_cast<uint16_t>(SectionID::primarySRC); |
| 208 | }); |
| 209 | if (src != _optionalSections.end()) |
| 210 | { |
| 211 | return static_cast<SRC*>(src->get()); |
| 212 | } |
| 213 | |
| 214 | return std::nullopt; |
| 215 | } |
| 216 | |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 217 | void PEL::checkRulesAndFix() |
| 218 | { |
| 219 | auto [actionFlags, eventType] = |
| 220 | pel_rules::check(_uh->actionFlags(), _uh->eventType(), _uh->severity()); |
| 221 | |
| 222 | _uh->setActionFlags(actionFlags); |
| 223 | _uh->setEventType(eventType); |
| 224 | } |
| 225 | |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 226 | void PEL::printSectionInJSON(const Section& section, std::string& buf, |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 227 | std::map<uint16_t, size_t>& pluralSections, |
| 228 | message::Registry& registry) const |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 229 | { |
| 230 | char tmpB[5]; |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 231 | uint8_t id[] = {static_cast<uint8_t>(section.header().id >> 8), |
| 232 | static_cast<uint8_t>(section.header().id)}; |
| 233 | sprintf(tmpB, "%c%c", id[0], id[1]); |
| 234 | std::string sectionID(tmpB); |
| 235 | std::string sectionName = pv::sectionTitles.count(sectionID) |
| 236 | ? pv::sectionTitles.at(sectionID) |
| 237 | : "Unknown Section"; |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 238 | |
| 239 | // Add a count if there are multiple of this type of section |
| 240 | auto count = pluralSections.find(section.header().id); |
| 241 | if (count != pluralSections.end()) |
| 242 | { |
| 243 | sectionName += " " + std::to_string(count->second); |
| 244 | count->second++; |
| 245 | } |
| 246 | |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 247 | if (section.valid()) |
| 248 | { |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 249 | auto json = (sectionID == "PS" || sectionID == "SS") |
| 250 | ? section.getJSON(registry) |
| 251 | : section.getJSON(); |
Matt Spinler | 4220a15 | 2020-03-26 10:18:09 -0500 | [diff] [blame] | 252 | |
| 253 | buf += "\"" + sectionName + "\": {\n"; |
| 254 | |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 255 | if (json) |
| 256 | { |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 257 | buf += *json + "\n},\n"; |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 258 | } |
| 259 | else |
| 260 | { |
Matt Spinler | 4220a15 | 2020-03-26 10:18:09 -0500 | [diff] [blame] | 261 | jsonInsert(buf, pv::sectionVer, |
| 262 | getNumberString("%d", section.header().version), 1); |
| 263 | jsonInsert(buf, pv::subSection, |
| 264 | getNumberString("%d", section.header().subType), 1); |
| 265 | jsonInsert(buf, pv::createdBy, |
| 266 | getNumberString("0x%X", section.header().componentID), |
| 267 | 1); |
| 268 | |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 269 | std::vector<uint8_t> data; |
| 270 | Stream s{data}; |
| 271 | section.flatten(s); |
Matt Spinler | 4220a15 | 2020-03-26 10:18:09 -0500 | [diff] [blame] | 272 | std::string dstr = |
| 273 | dumpHex(std::data(data) + SectionHeader::flattenedSize(), |
| 274 | data.size(), 2); |
| 275 | |
| 276 | std::string jsonIndent(indentLevel, 0x20); |
| 277 | buf += jsonIndent + "\"Data\": [\n"; |
| 278 | buf += dstr; |
| 279 | buf += jsonIndent + "]\n"; |
| 280 | buf += "},\n"; |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 281 | } |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 282 | } |
| 283 | else |
| 284 | { |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 285 | buf += "\n\"Invalid Section\": [\n \"invalid\"\n],\n"; |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 289 | std::map<uint16_t, size_t> PEL::getPluralSections() const |
| 290 | { |
| 291 | std::map<uint16_t, size_t> sectionCounts; |
| 292 | |
| 293 | for (const auto& section : optionalSections()) |
| 294 | { |
| 295 | if (sectionCounts.find(section->header().id) == sectionCounts.end()) |
| 296 | { |
| 297 | sectionCounts[section->header().id] = 1; |
| 298 | } |
| 299 | else |
| 300 | { |
| 301 | sectionCounts[section->header().id]++; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | std::map<uint16_t, size_t> sections; |
| 306 | for (const auto& [id, count] : sectionCounts) |
| 307 | { |
| 308 | if (count > 1) |
| 309 | { |
| 310 | // Start with 0 here and printSectionInJSON() |
| 311 | // will increment it as it goes. |
| 312 | sections.emplace(id, 0); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return sections; |
| 317 | } |
| 318 | |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 319 | void PEL::toJSON(message::Registry& registry) const |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 320 | { |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 321 | auto sections = getPluralSections(); |
| 322 | |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 323 | std::string buf = "{\n"; |
| 324 | printSectionInJSON(*(_ph.get()), buf, sections, registry); |
| 325 | printSectionInJSON(*(_uh.get()), buf, sections, registry); |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 326 | for (auto& section : this->optionalSections()) |
| 327 | { |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 328 | printSectionInJSON(*(section.get()), buf, sections, registry); |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 329 | } |
| 330 | buf += "}"; |
| 331 | std::size_t found = buf.rfind(","); |
| 332 | if (found != std::string::npos) |
| 333 | buf.replace(found, 1, ""); |
| 334 | std::cout << buf << std::endl; |
| 335 | } |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 336 | |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 337 | namespace util |
| 338 | { |
| 339 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 340 | std::unique_ptr<UserData> makeJSONUserDataSection(const nlohmann::json& json) |
| 341 | { |
| 342 | auto jsonString = json.dump(); |
| 343 | std::vector<uint8_t> jsonData(jsonString.begin(), jsonString.end()); |
| 344 | |
| 345 | // Pad to a 4 byte boundary |
| 346 | while ((jsonData.size() % 4) != 0) |
| 347 | { |
| 348 | jsonData.push_back(0); |
| 349 | } |
| 350 | |
| 351 | return std::make_unique<UserData>( |
| 352 | static_cast<uint16_t>(ComponentID::phosphorLogging), |
| 353 | static_cast<uint8_t>(UserDataFormat::json), |
| 354 | static_cast<uint8_t>(UserDataFormatVersion::json), jsonData); |
| 355 | } |
| 356 | |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 357 | std::unique_ptr<UserData> makeADUserDataSection(const AdditionalData& ad) |
| 358 | { |
| 359 | assert(!ad.empty()); |
| 360 | nlohmann::json json; |
| 361 | |
| 362 | // Remove the 'ESEL' entry, as it contains a full PEL in the value. |
| 363 | if (ad.getValue("ESEL")) |
| 364 | { |
| 365 | auto newAD = ad; |
| 366 | newAD.remove("ESEL"); |
| 367 | json = newAD.toJSON(); |
| 368 | } |
| 369 | else |
| 370 | { |
| 371 | json = ad.toJSON(); |
| 372 | } |
| 373 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 374 | return makeJSONUserDataSection(json); |
| 375 | } |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 376 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 377 | void addProcessNameToJSON(nlohmann::json& json, |
| 378 | const std::optional<std::string>& pid, |
| 379 | const DataInterfaceBase& dataIface) |
| 380 | { |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 381 | std::string name{unknownValue}; |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 382 | |
| 383 | try |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 384 | { |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 385 | if (pid) |
| 386 | { |
| 387 | auto n = dataIface.getProcessName(*pid); |
| 388 | if (n) |
| 389 | { |
| 390 | name = *n; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | catch (std::exception& e) |
| 395 | { |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 396 | } |
| 397 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 398 | json["Process Name"] = std::move(name); |
| 399 | } |
| 400 | |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 401 | void addBMCFWVersionIDToJSON(nlohmann::json& json, |
| 402 | const DataInterfaceBase& dataIface) |
| 403 | { |
| 404 | auto id = dataIface.getBMCFWVersionID(); |
| 405 | if (id.empty()) |
| 406 | { |
| 407 | id = unknownValue; |
| 408 | } |
| 409 | |
| 410 | json["BMC Version ID"] = std::move(id); |
| 411 | } |
| 412 | |
Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame] | 413 | std::string lastSegment(char separator, std::string data) |
| 414 | { |
| 415 | auto pos = data.find_last_of(separator); |
| 416 | if (pos != std::string::npos) |
| 417 | { |
| 418 | data = data.substr(pos + 1); |
| 419 | } |
| 420 | |
| 421 | return data; |
| 422 | } |
| 423 | |
| 424 | void addStatesToJSON(nlohmann::json& json, const DataInterfaceBase& dataIface) |
| 425 | { |
| 426 | json["BMCState"] = lastSegment('.', dataIface.getBMCState()); |
| 427 | json["ChassisState"] = lastSegment('.', dataIface.getChassisState()); |
| 428 | json["HostState"] = lastSegment('.', dataIface.getHostState()); |
| 429 | } |
| 430 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 431 | std::unique_ptr<UserData> |
| 432 | makeSysInfoUserDataSection(const AdditionalData& ad, |
| 433 | const DataInterfaceBase& dataIface) |
| 434 | { |
| 435 | nlohmann::json json; |
| 436 | |
| 437 | addProcessNameToJSON(json, ad.getValue("_PID"), dataIface); |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 438 | addBMCFWVersionIDToJSON(json, dataIface); |
Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame] | 439 | addStatesToJSON(json, dataIface); |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 440 | |
| 441 | return makeJSONUserDataSection(json); |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | } // namespace util |
| 445 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 446 | } // namespace pels |
| 447 | } // namespace openpower |