blob: 5f6f121907aeb4b482b19abc36dd1c4dbe74eb0a [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 Spinlerf9bae182019-10-09 13:37:38 -050016#include "src.hpp"
17
Matt Spinler717de422020-06-04 13:10:14 -050018#include "device_callouts.hpp"
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080019#include "json_utils.hpp"
20#include "paths.hpp"
21#include "pel_values.hpp"
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080022#ifdef PELTOOL
23#include <Python.h>
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080024
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080025#include <fifo_map.hpp>
26#include <nlohmann/json.hpp>
27#include <sstream>
28#endif
Matt Spinler5a90a952020-08-27 09:39:03 -050029#include <fmt/format.h>
30
Matt Spinlerf9bae182019-10-09 13:37:38 -050031#include <phosphor-logging/log.hpp>
32
33namespace openpower
34{
35namespace pels
36{
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080037namespace pv = openpower::pels::pel_values;
38namespace rg = openpower::pels::message;
Matt Spinlerf9bae182019-10-09 13:37:38 -050039using namespace phosphor::logging;
Matt Spinler85f61a62020-06-03 16:28:55 -050040using namespace std::string_literals;
Matt Spinlerf9bae182019-10-09 13:37:38 -050041
Matt Spinler075e5ba2020-02-21 15:46:00 -060042constexpr size_t ccinSize = 4;
43
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +080044#ifdef PELTOOL
45// Use fifo_map as nlohmann::json's map. We are just ignoring the 'less'
46// compare. With this map the keys are kept in FIFO order.
47template <class K, class V, class dummy_compare, class A>
48using fifoMap = nlohmann::fifo_map<K, V, nlohmann::fifo_map_compare<K>, A>;
49using fifoJSON = nlohmann::basic_json<fifoMap>;
50
51void pyDecRef(PyObject* pyObj)
52{
53 Py_XDECREF(pyObj);
54}
55
56/**
57 * @brief Returns a JSON string to append to SRC section.
58 *
59 * The returning string will contain a JSON object, but without
60 * the outer {}. If the input JSON isn't a JSON object (dict), then
61 * one will be created with the input added to a 'SRC Details' key.
62 *
63 * @param[in] json - The JSON to convert to a string
64 *
65 * @return std::string - The JSON string
66 */
67std::string prettyJSON(const fifoJSON& json)
68{
69 fifoJSON output;
70 if (!json.is_object())
71 {
72 output["SRC Details"] = json;
73 }
74 else
75 {
76 for (const auto& [key, value] : json.items())
77 {
78 output[key] = value;
79 }
80 }
81
82 // Let nlohmann do the pretty printing.
83 std::stringstream stream;
84 stream << std::setw(4) << output;
85
86 auto jsonString = stream.str();
87
88 // Now it looks like:
89 // {
90 // "Key": "Value",
91 // ...
92 // }
93
94 // Replace the { and the following newline, and the } and its
95 // preceeding newline.
96 jsonString.erase(0, 2);
97
98 auto pos = jsonString.find_last_of('}');
99 jsonString.erase(pos - 1);
100
101 return jsonString;
102}
103
104/**
105 * @brief Call Python modules to parse the data into a JSON string
106 *
107 * The module to call is based on the Creator Subsystem ID under the namespace
108 * "srcparsers". For example: "srcparsers.xsrc.xsrc" where "x" is the Creator
109 * Subsystem ID in ASCII lowercase.
110 *
111 * All modules must provide the following:
112 * Function: parseSRCToJson
113 * Argument list:
114 * 1. (str) ASCII string (Hex Word 1)
115 * 2. (str) Hex Word 2
116 * 3. (str) Hex Word 3
117 * 4. (str) Hex Word 4
118 * 5. (str) Hex Word 5
119 * 6. (str) Hex Word 6
120 * 7. (str) Hex Word 7
121 * 8. (str) Hex Word 8
122 * 9. (str) Hex Word 9
123 *-Return data:
124 * 1. (str) JSON string
125 *
126 * @param[in] hexwords - Vector of strings of Hexwords 1-9
127 * @param[in] creatorID - The creatorID from the Private Header section
128 * @return std::optional<std::string> - The JSON string if it could be created,
129 * else std::nullopt
130 */
131std::optional<std::string> getPythonJSON(std::vector<std::string>& hexwords,
132 uint8_t creatorID)
133{
134 PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pResult, *pBytes,
135 *eType, *eValue, *eTraceback;
136 std::string pErrStr;
137 std::string module = getNumberString("%c", tolower(creatorID)) + "src";
138 pName = PyUnicode_FromString(
139 std::string("srcparsers." + module + "." + module).c_str());
140 std::unique_ptr<PyObject, decltype(&pyDecRef)> modNamePtr(pName, &pyDecRef);
141 pModule = PyImport_Import(pName);
142 std::unique_ptr<PyObject, decltype(&pyDecRef)> modPtr(pModule, &pyDecRef);
143 if (pModule == NULL)
144 {
145 pErrStr = "No error string found";
146 PyErr_Fetch(&eType, &eValue, &eTraceback);
147 if (eValue)
148 {
149 PyObject* pStr = PyObject_Str(eValue);
150 if (pStr)
151 {
152 pErrStr = PyUnicode_AsUTF8(pStr);
153 }
154 Py_XDECREF(pStr);
155 }
156 }
157 else
158 {
159 pDict = PyModule_GetDict(pModule);
160 pFunc = PyDict_GetItemString(pDict, "parseSRCToJson");
161 if (PyCallable_Check(pFunc))
162 {
163 pArgs = PyTuple_New(9);
164 std::unique_ptr<PyObject, decltype(&pyDecRef)> argPtr(pArgs,
165 &pyDecRef);
166 for (size_t i = 0; i < 9; i++)
167 {
168 if (i < hexwords.size())
169 {
170 auto arg = hexwords[i];
171 PyTuple_SetItem(pArgs, i,
172 Py_BuildValue("s#", arg.c_str(), 8));
173 }
174 else
175 {
176 PyTuple_SetItem(pArgs, i, Py_BuildValue("s", "00000000"));
177 }
178 }
179 pResult = PyObject_CallObject(pFunc, pArgs);
180 std::unique_ptr<PyObject, decltype(&pyDecRef)> resPtr(pResult,
181 &pyDecRef);
182 if (pResult)
183 {
184 pBytes = PyUnicode_AsEncodedString(pResult, "utf-8", "~E~");
185 std::unique_ptr<PyObject, decltype(&pyDecRef)> pyBytePtr(
186 pBytes, &pyDecRef);
187 const char* output = PyBytes_AS_STRING(pBytes);
188 try
189 {
190 fifoJSON json = nlohmann::json::parse(output);
191 return prettyJSON(json);
192 }
193 catch (std::exception& e)
194 {
195 log<level::ERR>("Bad JSON from parser",
196 entry("ERROR=%s", e.what()),
197 entry("SRC=%s", hexwords.front().c_str()),
198 entry("PARSER_MODULE=%s", module.c_str()));
199 return std::nullopt;
200 }
201 }
202 else
203 {
204 pErrStr = "No error string found";
205 PyErr_Fetch(&eType, &eValue, &eTraceback);
206 if (eValue)
207 {
208 PyObject* pStr = PyObject_Str(eValue);
209 if (pStr)
210 {
211 pErrStr = PyUnicode_AsUTF8(pStr);
212 }
213 Py_XDECREF(pStr);
214 }
215 }
216 }
217 }
218 if (!pErrStr.empty())
219 {
220 log<level::ERR>("Python exception thrown by parser",
221 entry("ERROR=%s", pErrStr.c_str()),
222 entry("SRC=%s", hexwords.front().c_str()),
223 entry("PARSER_MODULE=%s", module.c_str()));
224 }
225 Py_XDECREF(eType);
226 Py_XDECREF(eValue);
227 Py_XDECREF(eTraceback);
228 return std::nullopt;
229}
230#endif
231
Matt Spinlerf9bae182019-10-09 13:37:38 -0500232void SRC::unflatten(Stream& stream)
233{
234 stream >> _header >> _version >> _flags >> _reserved1B >> _wordCount >>
235 _reserved2B >> _size;
236
237 for (auto& word : _hexData)
238 {
239 stream >> word;
240 }
241
242 _asciiString = std::make_unique<src::AsciiString>(stream);
243
244 if (hasAdditionalSections())
245 {
246 // The callouts section is currently the only extra subsection type
247 _callouts = std::make_unique<src::Callouts>(stream);
248 }
249}
250
Matt Spinler06885452019-11-06 10:35:42 -0600251void SRC::flatten(Stream& stream) const
Matt Spinlerf9bae182019-10-09 13:37:38 -0500252{
253 stream << _header << _version << _flags << _reserved1B << _wordCount
254 << _reserved2B << _size;
255
256 for (auto& word : _hexData)
257 {
258 stream << word;
259 }
260
261 _asciiString->flatten(stream);
262
263 if (_callouts)
264 {
265 _callouts->flatten(stream);
266 }
267}
268
269SRC::SRC(Stream& pel)
270{
271 try
272 {
273 unflatten(pel);
274 validate();
275 }
276 catch (const std::exception& e)
277 {
278 log<level::ERR>("Cannot unflatten SRC", entry("ERROR=%s", e.what()));
279 _valid = false;
280 }
281}
282
Matt Spinler075e5ba2020-02-21 15:46:00 -0600283SRC::SRC(const message::Entry& regEntry, const AdditionalData& additionalData,
Matt Spinler5a90a952020-08-27 09:39:03 -0500284 const nlohmann::json& jsonCallouts, const DataInterfaceBase& dataIface)
Matt Spinlerbd716f02019-10-15 10:54:11 -0500285{
286 _header.id = static_cast<uint16_t>(SectionID::primarySRC);
287 _header.version = srcSectionVersion;
288 _header.subType = srcSectionSubtype;
289 _header.componentID = regEntry.componentID;
290
291 _version = srcVersion;
292
293 _flags = 0;
294 if (regEntry.src.powerFault.value_or(false))
295 {
296 _flags |= powerFaultEvent;
297 }
298
299 _reserved1B = 0;
300
301 _wordCount = numSRCHexDataWords + 1;
302
303 _reserved2B = 0;
304
305 // There are multiple fields encoded in the hex data words.
306 std::for_each(_hexData.begin(), _hexData.end(),
307 [](auto& word) { word = 0; });
Matt Spinler7c619182020-07-27 15:15:11 -0500308
309 // Hex Word 2 Nibbles:
310 // MIGVEPFF
311 // M: Partition dump status = 0
312 // I: System boot state = TODO
313 // G: Partition Boot type = 0
314 // V: BMC dump status = TODO
315 // E: Platform boot mode = 0 (side = temporary, speed = fast)
316 // P: Platform dump status = TODO
317 // FF: SRC format, set below
318
Matt Spinlerbd716f02019-10-15 10:54:11 -0500319 setBMCFormat();
320 setBMCPosition();
Matt Spinler075e5ba2020-02-21 15:46:00 -0600321 setMotherboardCCIN(dataIface);
322
Matt Spinlerbd716f02019-10-15 10:54:11 -0500323 // Fill in the last 4 words from the AdditionalData property contents.
324 setUserDefinedHexWords(regEntry, additionalData);
325
326 _asciiString = std::make_unique<src::AsciiString>(regEntry);
327
Matt Spinler5a90a952020-08-27 09:39:03 -0500328 addCallouts(regEntry, additionalData, jsonCallouts, dataIface);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500329
330 _size = baseSRCSize;
331 _size += _callouts ? _callouts->flattenedSize() : 0;
332 _header.size = Section::flattenedSize() + _size;
333
334 _valid = true;
335}
336
337void SRC::setUserDefinedHexWords(const message::Entry& regEntry,
338 const AdditionalData& ad)
339{
340 if (!regEntry.src.hexwordADFields)
341 {
342 return;
343 }
344
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800345 // Save the AdditionalData value corresponding to the first element of
346 // adName tuple into _hexData[wordNum].
Matt Spinlerbd716f02019-10-15 10:54:11 -0500347 for (const auto& [wordNum, adName] : *regEntry.src.hexwordADFields)
348 {
349 // Can only set words 6 - 9
350 if (!isUserDefinedWord(wordNum))
351 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500352 std::string msg =
353 "SRC user data word out of range: " + std::to_string(wordNum);
354 addDebugData(msg);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500355 continue;
356 }
357
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800358 auto value = ad.getValue(std::get<0>(adName));
Matt Spinlerbd716f02019-10-15 10:54:11 -0500359 if (value)
360 {
361 _hexData[getWordIndexFromWordNum(wordNum)] =
362 std::strtoul(value.value().c_str(), nullptr, 0);
363 }
364 else
365 {
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800366 std::string msg = "Source for user data SRC word not found: " +
367 std::get<0>(adName);
Matt Spinler85f61a62020-06-03 16:28:55 -0500368 addDebugData(msg);
Matt Spinlerbd716f02019-10-15 10:54:11 -0500369 }
370 }
371}
372
Matt Spinler075e5ba2020-02-21 15:46:00 -0600373void SRC::setMotherboardCCIN(const DataInterfaceBase& dataIface)
374{
375 uint32_t ccin = 0;
376 auto ccinString = dataIface.getMotherboardCCIN();
377
378 try
379 {
380 if (ccinString.size() == ccinSize)
381 {
382 ccin = std::stoi(ccinString, 0, 16);
383 }
384 }
385 catch (std::exception& e)
386 {
387 log<level::WARNING>("Could not convert motherboard CCIN to a number",
388 entry("CCIN=%s", ccinString.c_str()));
389 return;
390 }
391
392 // Set the first 2 bytes
393 _hexData[1] |= ccin << 16;
394}
395
Matt Spinlerf9bae182019-10-09 13:37:38 -0500396void SRC::validate()
397{
398 bool failed = false;
399
400 if ((header().id != static_cast<uint16_t>(SectionID::primarySRC)) &&
401 (header().id != static_cast<uint16_t>(SectionID::secondarySRC)))
402 {
403 log<level::ERR>("Invalid SRC section ID",
404 entry("ID=0x%X", header().id));
405 failed = true;
406 }
407
408 // Check the version in the SRC, not in the header
Matt Spinlerbd716f02019-10-15 10:54:11 -0500409 if (_version != srcVersion)
Matt Spinlerf9bae182019-10-09 13:37:38 -0500410 {
Matt Spinlerbd716f02019-10-15 10:54:11 -0500411 log<level::ERR>("Invalid SRC version", entry("VERSION=0x%X", _version));
Matt Spinlerf9bae182019-10-09 13:37:38 -0500412 failed = true;
413 }
414
415 _valid = failed ? false : true;
416}
417
Matt Spinler075e5ba2020-02-21 15:46:00 -0600418bool SRC::isBMCSRC() const
419{
420 auto as = asciiString();
421 if (as.length() >= 2)
422 {
423 uint8_t errorType = strtoul(as.substr(0, 2).c_str(), nullptr, 16);
424 return (errorType == static_cast<uint8_t>(SRCType::bmcError) ||
425 errorType == static_cast<uint8_t>(SRCType::powerError));
426 }
427 return false;
428}
429
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800430std::optional<std::string> SRC::getErrorDetails(message::Registry& registry,
431 DetailLevel type,
432 bool toCache) const
433{
434 const std::string jsonIndent(indentLevel, 0x20);
435 std::string errorOut;
Matt Spinler075e5ba2020-02-21 15:46:00 -0600436 if (isBMCSRC())
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800437 {
438 auto entry = registry.lookup("0x" + asciiString().substr(4, 4),
439 rg::LookupType::reasonCode, toCache);
440 if (entry)
441 {
442 errorOut.append(jsonIndent + "\"Error Details\": {\n");
443 auto errorMsg = getErrorMessage(*entry);
444 if (errorMsg)
445 {
446 if (type == DetailLevel::message)
447 {
448 return errorMsg.value();
449 }
450 else
451 {
452 jsonInsert(errorOut, "Message", errorMsg.value(), 2);
453 }
454 }
455 if (entry->src.hexwordADFields)
456 {
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800457 std::map<size_t, std::tuple<std::string, std::string>>
458 adFields = entry->src.hexwordADFields.value();
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800459 for (const auto& hexwordMap : adFields)
460 {
Harisuddin Mohamed Isa1a1b0df2020-11-23 16:34:36 +0800461 std::vector<std::string> valueDescr;
462 valueDescr.push_back(getNumberString(
463 "0x%X",
464 _hexData[getWordIndexFromWordNum(hexwordMap.first)]));
465 valueDescr.push_back(std::get<1>(hexwordMap.second));
466 jsonInsertArray(errorOut, std::get<0>(hexwordMap.second),
467 valueDescr, 2);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800468 }
469 }
470 errorOut.erase(errorOut.size() - 2);
471 errorOut.append("\n");
472 errorOut.append(jsonIndent + "},\n");
473 return errorOut;
474 }
475 }
476 return std::nullopt;
477}
478
479std::optional<std::string>
480 SRC::getErrorMessage(const message::Entry& regEntry) const
481{
482 try
483 {
484 if (regEntry.doc.messageArgSources)
485 {
486 size_t msgLen = regEntry.doc.message.length();
487 char msg[msgLen + 1];
488 strcpy(msg, regEntry.doc.message.c_str());
489 std::vector<uint32_t> argSourceVals;
490 std::string message;
491 const auto& argValues = regEntry.doc.messageArgSources.value();
492 for (size_t i = 0; i < argValues.size(); ++i)
493 {
494 argSourceVals.push_back(_hexData[getWordIndexFromWordNum(
495 argValues[i].back() - '0')]);
496 }
497 const char* msgPointer = msg;
498 while (*msgPointer)
499 {
500 if (*msgPointer == '%')
501 {
502 msgPointer++;
503 size_t wordIndex = *msgPointer - '0';
504 if (isdigit(*msgPointer) && wordIndex >= 1 &&
505 static_cast<uint16_t>(wordIndex) <=
506 argSourceVals.size())
507 {
508 message.append(getNumberString(
509 "0x%X", argSourceVals[wordIndex - 1]));
510 }
511 else
512 {
513 message.append("%" + std::string(1, *msgPointer));
514 }
515 }
516 else
517 {
518 message.push_back(*msgPointer);
519 }
520 msgPointer++;
521 }
522 return message;
523 }
524 else
525 {
526 return regEntry.doc.message;
527 }
528 }
529 catch (const std::exception& e)
530 {
531 log<level::ERR>("Cannot get error message from registry entry",
532 entry("ERROR=%s", e.what()));
533 }
534 return std::nullopt;
535}
536
537std::optional<std::string> SRC::getCallouts() const
538{
539 if (!_callouts)
540 {
541 return std::nullopt;
542 }
543 std::string printOut;
544 const std::string jsonIndent(indentLevel, 0x20);
545 const auto& callout = _callouts->callouts();
546 const auto& compDescrp = pv::failingComponentType;
547 printOut.append(jsonIndent + "\"Callout Section\": {\n");
548 jsonInsert(printOut, "Callout Count", std::to_string(callout.size()), 2);
549 printOut.append(jsonIndent + jsonIndent + "\"Callouts\": [");
550 for (auto& entry : callout)
551 {
552 printOut.append("{\n");
553 if (entry->fruIdentity())
554 {
555 jsonInsert(
556 printOut, "FRU Type",
557 compDescrp.at(entry->fruIdentity()->failingComponentType()), 3);
558 jsonInsert(printOut, "Priority",
559 pv::getValue(entry->priority(),
560 pel_values::calloutPriorityValues),
561 3);
562 if (!entry->locationCode().empty())
563 {
564 jsonInsert(printOut, "Location Code", entry->locationCode(), 3);
565 }
566 if (entry->fruIdentity()->getPN().has_value())
567 {
568 jsonInsert(printOut, "Part Number",
569 entry->fruIdentity()->getPN().value(), 3);
570 }
571 if (entry->fruIdentity()->getMaintProc().has_value())
572 {
Matt Spinler9e8b49e2020-09-10 13:15:26 -0500573 jsonInsert(printOut, "Procedure",
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800574 entry->fruIdentity()->getMaintProc().value(), 3);
575 if (pv::procedureDesc.find(
576 entry->fruIdentity()->getMaintProc().value()) !=
577 pv::procedureDesc.end())
578 {
579 jsonInsert(
580 printOut, "Description",
581 pv::procedureDesc.at(
582 entry->fruIdentity()->getMaintProc().value()),
583 3);
584 }
585 }
586 if (entry->fruIdentity()->getCCIN().has_value())
587 {
588 jsonInsert(printOut, "CCIN",
589 entry->fruIdentity()->getCCIN().value(), 3);
590 }
591 if (entry->fruIdentity()->getSN().has_value())
592 {
593 jsonInsert(printOut, "Serial Number",
594 entry->fruIdentity()->getSN().value(), 3);
595 }
596 }
597 if (entry->pceIdentity())
598 {
599 const auto& pceIdentMtms = entry->pceIdentity()->mtms();
600 if (!pceIdentMtms.machineTypeAndModel().empty())
601 {
602 jsonInsert(printOut, "PCE MTMS",
603 pceIdentMtms.machineTypeAndModel() + "_" +
604 pceIdentMtms.machineSerialNumber(),
605 3);
606 }
607 if (!entry->pceIdentity()->enclosureName().empty())
608 {
609 jsonInsert(printOut, "PCE Name",
610 entry->pceIdentity()->enclosureName(), 3);
611 }
612 }
613 if (entry->mru())
614 {
615 const auto& mruCallouts = entry->mru()->mrus();
616 std::string mruId;
617 for (auto& element : mruCallouts)
618 {
619 if (!mruId.empty())
620 {
621 mruId.append(", " + getNumberString("%08X", element.id));
622 }
623 else
624 {
625 mruId.append(getNumberString("%08X", element.id));
626 }
627 }
628 jsonInsert(printOut, "MRU Id", mruId, 3);
629 }
630 printOut.erase(printOut.size() - 2);
631 printOut.append("\n" + jsonIndent + jsonIndent + "}, ");
632 };
633 printOut.erase(printOut.size() - 2);
634 printOut.append("]\n" + jsonIndent + "}");
635 return printOut;
636}
637
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800638std::optional<std::string> SRC::getJSON(message::Registry& registry,
639 const std::vector<std::string>& plugins,
640 uint8_t creatorID) const
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800641{
642 std::string ps;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800643 std::vector<std::string> hexwords;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +0800644 jsonInsert(ps, pv::sectionVer, getNumberString("%d", _header.version), 1);
645 jsonInsert(ps, pv::subSection, getNumberString("%d", _header.subType), 1);
646 jsonInsert(ps, pv::createdBy, getNumberString("0x%X", _header.componentID),
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800647 1);
648 jsonInsert(ps, "SRC Version", getNumberString("0x%02X", _version), 1);
Harisuddin Mohamed Isac32e5512020-02-06 18:05:21 +0800649 jsonInsert(ps, "SRC Format", getNumberString("0x%02X", _hexData[0] & 0xFF),
650 1);
651 jsonInsert(ps, "Virtual Progress SRC",
652 pv::boolString.at(_flags & virtualProgressSRC), 1);
653 jsonInsert(ps, "I5/OS Service Event Bit",
654 pv::boolString.at(_flags & i5OSServiceEventBit), 1);
655 jsonInsert(ps, "Hypervisor Dump Initiated",
656 pv::boolString.at(_flags & hypDumpInit), 1);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800657 jsonInsert(ps, "Power Control Net Fault",
658 pv::boolString.at(isPowerFaultEvent()), 1);
Matt Spinler075e5ba2020-02-21 15:46:00 -0600659
660 if (isBMCSRC())
661 {
662 std::string ccinString;
663 uint32_t ccin = _hexData[1] >> 16;
664
665 if (ccin)
666 {
667 ccinString = getNumberString("%04X", ccin);
668 }
669 // The PEL spec calls it a backplane, so call it that here.
670 jsonInsert(ps, "Backplane CCIN", ccinString, 1);
Matt Spinlerafa2c792020-08-27 11:01:39 -0500671
672 jsonInsert(ps, "Deconfigured",
673 pv::boolString.at(
674 _hexData[3] &
675 static_cast<uint32_t>(ErrorStatusFlags::deconfigured)),
676 1);
677
678 jsonInsert(
679 ps, "Guarded",
680 pv::boolString.at(_hexData[3] &
681 static_cast<uint32_t>(ErrorStatusFlags::guarded)),
682 1);
Matt Spinler075e5ba2020-02-21 15:46:00 -0600683 }
684
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800685 auto errorDetails = getErrorDetails(registry, DetailLevel::json, true);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800686 if (errorDetails)
687 {
688 ps.append(errorDetails.value());
689 }
690 jsonInsert(ps, "Valid Word Count", getNumberString("0x%02X", _wordCount),
691 1);
692 std::string refcode = asciiString();
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800693 hexwords.push_back(refcode);
Harisuddin Mohamed Isafecaa572020-03-11 16:04:50 +0800694 std::string extRefcode;
695 size_t pos = refcode.find(0x20);
696 if (pos != std::string::npos)
697 {
698 size_t nextPos = refcode.find_first_not_of(0x20, pos);
699 if (nextPos != std::string::npos)
700 {
701 extRefcode = trimEnd(refcode.substr(nextPos));
702 }
703 refcode.erase(pos);
704 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800705 jsonInsert(ps, "Reference Code", refcode, 1);
Harisuddin Mohamed Isafecaa572020-03-11 16:04:50 +0800706 if (!extRefcode.empty())
707 {
708 jsonInsert(ps, "Extended Reference Code", extRefcode, 1);
709 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800710 for (size_t i = 2; i <= _wordCount; i++)
711 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800712 std::string tmpWord =
713 getNumberString("%08X", _hexData[getWordIndexFromWordNum(i)]);
714 jsonInsert(ps, "Hex Word " + std::to_string(i), tmpWord, 1);
715 hexwords.push_back(tmpWord);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800716 }
717 auto calloutJson = getCallouts();
718 if (calloutJson)
719 {
720 ps.append(calloutJson.value());
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800721 ps.append(",\n");
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800722 }
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800723 std::string subsystem = getNumberString("%c", tolower(creatorID));
724 bool srcDetailExists = false;
725#ifdef PELTOOL
726 if (std::find(plugins.begin(), plugins.end(), subsystem + "src") !=
727 plugins.end())
728 {
729 auto pyJson = getPythonJSON(hexwords, creatorID);
730 if (pyJson)
731 {
732 ps.append(pyJson.value());
733 srcDetailExists = true;
734 }
735 }
736#endif
737 if (!srcDetailExists)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800738 {
739 ps.erase(ps.size() - 2);
740 }
741 return ps;
742}
743
Matt Spinler03984582020-04-09 13:17:58 -0500744void SRC::addCallouts(const message::Entry& regEntry,
745 const AdditionalData& additionalData,
Matt Spinler5a90a952020-08-27 09:39:03 -0500746 const nlohmann::json& jsonCallouts,
Matt Spinlered046852020-03-13 13:58:15 -0500747 const DataInterfaceBase& dataIface)
748{
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500749 auto registryCallouts =
750 getRegistryCallouts(regEntry, additionalData, dataIface);
751
Matt Spinlered046852020-03-13 13:58:15 -0500752 auto item = additionalData.getValue("CALLOUT_INVENTORY_PATH");
Miguel Gomez53ef1552020-10-14 21:16:32 +0000753 auto priority = additionalData.getValue("CALLOUT_PRIORITY");
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500754
Miguel Gomez53ef1552020-10-14 21:16:32 +0000755 std::optional<CalloutPriority> calloutPriority;
756
757 // Only H, M or L priority values.
758 if (priority && !(*priority).empty())
759 {
760 uint8_t p = (*priority)[0];
761 if (p == 'H' || p == 'M' || p == 'L')
762 {
763 calloutPriority = static_cast<CalloutPriority>(p);
764 }
765 }
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500766 // If the first registry callout says to use the passed in inventory
767 // path to get the location code for a symbolic FRU callout with a
768 // trusted location code, then do not add the inventory path as a
769 // normal FRU callout.
770 bool useInvForSymbolicFRULocCode =
771 !registryCallouts.empty() && registryCallouts[0].useInventoryLocCode &&
772 !registryCallouts[0].symbolicFRUTrusted.empty();
773
774 if (item && !useInvForSymbolicFRULocCode)
Matt Spinlered046852020-03-13 13:58:15 -0500775 {
Miguel Gomez53ef1552020-10-14 21:16:32 +0000776 addInventoryCallout(*item, calloutPriority, std::nullopt, dataIface);
Matt Spinlered046852020-03-13 13:58:15 -0500777 }
778
Matt Spinler717de422020-06-04 13:10:14 -0500779 addDevicePathCallouts(additionalData, dataIface);
Matt Spinler03984582020-04-09 13:17:58 -0500780
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500781 addRegistryCallouts(registryCallouts, dataIface,
782 (useInvForSymbolicFRULocCode) ? item : std::nullopt);
Matt Spinler5a90a952020-08-27 09:39:03 -0500783
784 if (!jsonCallouts.empty())
785 {
786 addJSONCallouts(jsonCallouts, dataIface);
787 }
Matt Spinlered046852020-03-13 13:58:15 -0500788}
789
790void SRC::addInventoryCallout(const std::string& inventoryPath,
Matt Spinleraf191c72020-06-04 11:35:13 -0500791 const std::optional<CalloutPriority>& priority,
792 const std::optional<std::string>& locationCode,
Matt Spinlerb8cb60f2020-08-27 10:55:55 -0500793 const DataInterfaceBase& dataIface,
794 const std::vector<src::MRU::MRUCallout>& mrus)
Matt Spinlered046852020-03-13 13:58:15 -0500795{
796 std::string locCode;
797 std::string fn;
798 std::string ccin;
799 std::string sn;
800 std::unique_ptr<src::Callout> callout;
801
Matt Spinlered046852020-03-13 13:58:15 -0500802 try
803 {
Matt Spinleraf191c72020-06-04 11:35:13 -0500804 // Use the passed in location code if there otherwise look it up
805 if (locationCode)
806 {
807 locCode = *locationCode;
808 }
809 else
810 {
811 locCode = dataIface.getLocationCode(inventoryPath);
812 }
Matt Spinlered046852020-03-13 13:58:15 -0500813
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500814 try
815 {
816 dataIface.getHWCalloutFields(inventoryPath, fn, ccin, sn);
817
Matt Spinleraf191c72020-06-04 11:35:13 -0500818 CalloutPriority p =
819 priority ? priority.value() : CalloutPriority::high;
820
Matt Spinlerb8cb60f2020-08-27 10:55:55 -0500821 callout =
822 std::make_unique<src::Callout>(p, locCode, fn, ccin, sn, mrus);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500823 }
824 catch (const SdBusError& e)
825 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500826 std::string msg =
827 "No VPD found for " + inventoryPath + ": " + e.what();
828 addDebugData(msg);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500829
830 // Just create the callout with empty FRU fields
Matt Spinlerb8cb60f2020-08-27 10:55:55 -0500831 callout = std::make_unique<src::Callout>(
832 CalloutPriority::high, locCode, fn, ccin, sn, mrus);
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500833 }
Matt Spinlered046852020-03-13 13:58:15 -0500834 }
835 catch (const SdBusError& e)
836 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500837 std::string msg = "Could not get location code for " + inventoryPath +
838 ": " + e.what();
839 addDebugData(msg);
Matt Spinlered046852020-03-13 13:58:15 -0500840
Matt Spinlered046852020-03-13 13:58:15 -0500841 callout = std::make_unique<src::Callout>(CalloutPriority::high,
Matt Spinlera27e2e52020-04-09 11:06:11 -0500842 "no_vpd_for_fru");
Matt Spinlered046852020-03-13 13:58:15 -0500843 }
844
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500845 createCalloutsObject();
Matt Spinlered046852020-03-13 13:58:15 -0500846 _callouts->addCallout(std::move(callout));
Matt Spinler03984582020-04-09 13:17:58 -0500847}
Matt Spinlered046852020-03-13 13:58:15 -0500848
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500849std::vector<message::RegistryCallout>
850 SRC::getRegistryCallouts(const message::Entry& regEntry,
851 const AdditionalData& additionalData,
852 const DataInterfaceBase& dataIface)
853{
854 std::vector<message::RegistryCallout> registryCallouts;
855
856 if (regEntry.callouts)
857 {
858 try
859 {
860 auto systemNames = dataIface.getSystemNames();
861
862 registryCallouts = message::Registry::getCallouts(
863 regEntry.callouts.value(), systemNames, additionalData);
864 }
865 catch (const std::exception& e)
866 {
867 addDebugData(fmt::format(
868 "Error parsing PEL message registry callout JSON: {}",
869 e.what()));
870 }
871 }
872
873 return registryCallouts;
874}
875
876void SRC::addRegistryCallouts(
877 const std::vector<message::RegistryCallout>& callouts,
878 const DataInterfaceBase& dataIface,
879 std::optional<std::string> trustedSymbolicFRUInvPath)
Matt Spinler03984582020-04-09 13:17:58 -0500880{
881 try
882 {
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500883 for (const auto& callout : callouts)
Matt Spinler03984582020-04-09 13:17:58 -0500884 {
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500885 addRegistryCallout(callout, dataIface, trustedSymbolicFRUInvPath);
886
887 // Only the first callout gets the inventory path
888 if (trustedSymbolicFRUInvPath)
889 {
890 trustedSymbolicFRUInvPath = std::nullopt;
891 }
Matt Spinler03984582020-04-09 13:17:58 -0500892 }
893 }
894 catch (std::exception& e)
895 {
Matt Spinler85f61a62020-06-03 16:28:55 -0500896 std::string msg =
897 "Error parsing PEL message registry callout JSON: "s + e.what();
898 addDebugData(msg);
Matt Spinler03984582020-04-09 13:17:58 -0500899 }
900}
901
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500902void SRC::addRegistryCallout(
903 const message::RegistryCallout& regCallout,
904 const DataInterfaceBase& dataIface,
905 const std::optional<std::string>& trustedSymbolicFRUInvPath)
Matt Spinler03984582020-04-09 13:17:58 -0500906{
907 std::unique_ptr<src::Callout> callout;
Matt Spinler03984582020-04-09 13:17:58 -0500908 auto locCode = regCallout.locCode;
909
Matt Spinleraf191c72020-06-04 11:35:13 -0500910 if (!locCode.empty())
911 {
912 try
913 {
914 locCode = dataIface.expandLocationCode(locCode, 0);
915 }
916 catch (const std::exception& e)
917 {
918 auto msg =
919 "Unable to expand location code " + locCode + ": " + e.what();
920 addDebugData(msg);
921 return;
922 }
923 }
924
Matt Spinler03984582020-04-09 13:17:58 -0500925 // Via the PEL values table, get the priority enum.
926 // The schema will have validated the priority was a valid value.
927 auto priorityIt =
928 pv::findByName(regCallout.priority, pv::calloutPriorityValues);
929 assert(priorityIt != pv::calloutPriorityValues.end());
930 auto priority =
931 static_cast<CalloutPriority>(std::get<pv::fieldValuePos>(*priorityIt));
932
933 if (!regCallout.procedure.empty())
934 {
935 // Procedure callout
936 callout =
937 std::make_unique<src::Callout>(priority, regCallout.procedure);
938 }
939 else if (!regCallout.symbolicFRU.empty())
940 {
941 // Symbolic FRU callout
942 callout = std::make_unique<src::Callout>(
943 priority, regCallout.symbolicFRU, locCode, false);
944 }
945 else if (!regCallout.symbolicFRUTrusted.empty())
946 {
947 // Symbolic FRU with trusted location code callout
948
Matt Spinlerf00f9d02020-10-23 09:14:22 -0500949 // Use the location code from the inventory path if there is one.
950 if (trustedSymbolicFRUInvPath)
951 {
952 try
953 {
954 locCode = dataIface.getLocationCode(*trustedSymbolicFRUInvPath);
955 }
956 catch (const std::exception& e)
957 {
958 addDebugData(
959 fmt::format("Could not get location code for {}: {}",
960 *trustedSymbolicFRUInvPath, e.what()));
961 locCode.clear();
962 }
963 }
964
Matt Spinler03984582020-04-09 13:17:58 -0500965 // The registry wants it to be trusted, but that requires a valid
966 // location code for it to actually be.
967 callout = std::make_unique<src::Callout>(
968 priority, regCallout.symbolicFRUTrusted, locCode, !locCode.empty());
969 }
970 else
971 {
Matt Spinleraf191c72020-06-04 11:35:13 -0500972 // A hardware callout
973 std::string inventoryPath;
974
975 try
976 {
977 // Get the inventory item from the unexpanded location code
978 inventoryPath =
Matt Spinler2f9225a2020-08-05 12:58:49 -0500979 dataIface.getInventoryFromLocCode(regCallout.locCode, 0, false);
Matt Spinleraf191c72020-06-04 11:35:13 -0500980 }
981 catch (const std::exception& e)
982 {
983 std::string msg =
984 "Unable to get inventory path from location code: " + locCode +
985 ": " + e.what();
986 addDebugData(msg);
987 return;
988 }
989
990 addInventoryCallout(inventoryPath, priority, locCode, dataIface);
Matt Spinler03984582020-04-09 13:17:58 -0500991 }
992
993 if (callout)
994 {
995 createCalloutsObject();
996 _callouts->addCallout(std::move(callout));
997 }
998}
Matt Spinlered046852020-03-13 13:58:15 -0500999
Matt Spinler717de422020-06-04 13:10:14 -05001000void SRC::addDevicePathCallouts(const AdditionalData& additionalData,
1001 const DataInterfaceBase& dataIface)
1002{
1003 std::vector<device_callouts::Callout> callouts;
1004 auto i2cBus = additionalData.getValue("CALLOUT_IIC_BUS");
1005 auto i2cAddr = additionalData.getValue("CALLOUT_IIC_ADDR");
1006 auto devPath = additionalData.getValue("CALLOUT_DEVICE_PATH");
1007
1008 // A device callout contains either:
1009 // * CALLOUT_ERRNO, CALLOUT_DEVICE_PATH
1010 // * CALLOUT_ERRNO, CALLOUT_IIC_BUS, CALLOUT_IIC_ADDR
1011 // We don't care about the errno.
1012
1013 if (devPath)
1014 {
1015 try
1016 {
1017 callouts = device_callouts::getCallouts(*devPath,
1018 dataIface.getSystemNames());
1019 }
1020 catch (const std::exception& e)
1021 {
1022 addDebugData(e.what());
1023 callouts.clear();
1024 }
1025 }
1026 else if (i2cBus && i2cAddr)
1027 {
1028 size_t bus;
1029 uint8_t address;
1030
1031 try
1032 {
1033 // If /dev/i2c- is prepended, remove it
1034 if (i2cBus->find("/dev/i2c-") != std::string::npos)
1035 {
1036 *i2cBus = i2cBus->substr(9);
1037 }
1038
1039 bus = stoul(*i2cBus, nullptr, 0);
1040 address = stoul(*i2cAddr, nullptr, 0);
1041 }
1042 catch (const std::exception& e)
1043 {
1044 std::string msg = "Invalid CALLOUT_IIC_BUS " + *i2cBus +
1045 " or CALLOUT_IIC_ADDR " + *i2cAddr +
1046 " in AdditionalData property";
1047 addDebugData(msg);
1048 return;
1049 }
1050
1051 try
1052 {
1053 callouts = device_callouts::getI2CCallouts(
1054 bus, address, dataIface.getSystemNames());
1055 }
1056 catch (const std::exception& e)
1057 {
1058 addDebugData(e.what());
1059 callouts.clear();
1060 }
1061 }
1062
1063 for (const auto& callout : callouts)
1064 {
1065 // The priority shouldn't be invalid, but check just in case.
1066 CalloutPriority priority = CalloutPriority::high;
1067
1068 if (!callout.priority.empty())
1069 {
1070 auto p = pel_values::findByValue(
1071 static_cast<uint32_t>(callout.priority[0]),
1072 pel_values::calloutPriorityValues);
1073
1074 if (p != pel_values::calloutPriorityValues.end())
1075 {
1076 priority = static_cast<CalloutPriority>(callout.priority[0]);
1077 }
1078 else
1079 {
1080 std::string msg =
1081 "Invalid priority found in dev callout JSON: " +
1082 callout.priority[0];
1083 addDebugData(msg);
1084 }
1085 }
1086
1087 try
1088 {
Matt Spinler2f9225a2020-08-05 12:58:49 -05001089 auto inventoryPath = dataIface.getInventoryFromLocCode(
1090 callout.locationCode, 0, false);
Matt Spinler717de422020-06-04 13:10:14 -05001091
1092 addInventoryCallout(inventoryPath, priority, std::nullopt,
1093 dataIface);
1094 }
1095 catch (const std::exception& e)
1096 {
1097 std::string msg =
1098 "Unable to get inventory path from location code: " +
1099 callout.locationCode + ": " + e.what();
1100 addDebugData(msg);
1101 }
1102
1103 // Until the code is there to convert these MRU value strings to
1104 // the official MRU values in the callout objects, just store
1105 // the MRU name in the debug UserData section.
1106 if (!callout.mru.empty())
1107 {
1108 std::string msg = "MRU: " + callout.mru;
1109 addDebugData(msg);
1110 }
1111
1112 // getCallouts() may have generated some debug data it stored
1113 // in a callout object. Save it as well.
1114 if (!callout.debug.empty())
1115 {
1116 addDebugData(callout.debug);
1117 }
1118 }
1119}
1120
Matt Spinler5a90a952020-08-27 09:39:03 -05001121void SRC::addJSONCallouts(const nlohmann::json& jsonCallouts,
1122 const DataInterfaceBase& dataIface)
1123{
1124 if (jsonCallouts.empty())
1125 {
1126 return;
1127 }
1128
1129 if (!jsonCallouts.is_array())
1130 {
1131 addDebugData("Callout JSON isn't an array");
1132 return;
1133 }
1134
1135 for (const auto& callout : jsonCallouts)
1136 {
1137 try
1138 {
1139 addJSONCallout(callout, dataIface);
1140 }
1141 catch (const std::exception& e)
1142 {
1143 addDebugData(fmt::format(
1144 "Failed extracting callout data from JSON: {}", e.what()));
1145 }
1146 }
1147}
1148
1149void SRC::addJSONCallout(const nlohmann::json& jsonCallout,
1150 const DataInterfaceBase& dataIface)
1151{
Matt Spinler3bdd0112020-08-27 10:24:34 -05001152 auto priority = getPriorityFromJSON(jsonCallout);
1153 std::string locCode;
1154 std::string unexpandedLocCode;
1155 std::unique_ptr<src::Callout> callout;
1156
1157 // Expand the location code if it's there
1158 if (jsonCallout.contains("LocationCode"))
1159 {
1160 unexpandedLocCode = jsonCallout.at("LocationCode").get<std::string>();
1161
1162 try
1163 {
1164 locCode = dataIface.expandLocationCode(unexpandedLocCode, 0);
1165 }
1166 catch (const std::exception& e)
1167 {
1168 addDebugData(fmt::format("Unable to expand location code {}: {}",
1169 unexpandedLocCode, e.what()));
1170 // Use the value from the JSON so at least there's something
1171 locCode = unexpandedLocCode;
1172 }
1173 }
1174
1175 // Create either a procedure, symbolic FRU, or normal FRU callout.
1176 if (jsonCallout.contains("Procedure"))
1177 {
1178 auto procedure = jsonCallout.at("Procedure").get<std::string>();
1179
1180 callout = std::make_unique<src::Callout>(
1181 static_cast<CalloutPriority>(priority), procedure,
1182 src::CalloutValueType::raw);
1183 }
1184 else if (jsonCallout.contains("SymbolicFRU"))
1185 {
1186 auto fru = jsonCallout.at("SymbolicFRU").get<std::string>();
1187
1188 bool trusted = false;
1189 if (jsonCallout.contains("TrustedLocationCode") && !locCode.empty())
1190 {
1191 trusted = jsonCallout.at("TrustedLocationCode").get<bool>();
1192 }
1193
1194 callout = std::make_unique<src::Callout>(
1195 static_cast<CalloutPriority>(priority), fru,
1196 src::CalloutValueType::raw, locCode, trusted);
1197 }
1198 else
1199 {
1200 // A hardware FRU
1201 std::string inventoryPath;
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001202 std::vector<src::MRU::MRUCallout> mrus;
Matt Spinler3bdd0112020-08-27 10:24:34 -05001203
1204 if (jsonCallout.contains("InventoryPath"))
1205 {
1206 inventoryPath = jsonCallout.at("InventoryPath").get<std::string>();
1207 }
1208 else
1209 {
1210 if (unexpandedLocCode.empty())
1211 {
1212 throw std::runtime_error{"JSON callout needs either an "
1213 "inventory path or location code"};
1214 }
1215
1216 try
1217 {
1218 inventoryPath = dataIface.getInventoryFromLocCode(
1219 unexpandedLocCode, 0, false);
1220 }
1221 catch (const std::exception& e)
1222 {
1223 throw std::runtime_error{
1224 fmt::format("Unable to get inventory path from "
1225 "location code: {}: {}",
1226 unexpandedLocCode, e.what())};
1227 }
1228 }
1229
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001230 if (jsonCallout.contains("MRUs"))
1231 {
1232 mrus = getMRUsFromJSON(jsonCallout.at("MRUs"));
1233 }
1234
Matt Spinler3bdd0112020-08-27 10:24:34 -05001235 // If the location code was also passed in, use that here too
1236 // so addInventoryCallout doesn't have to look it up.
1237 std::optional<std::string> lc;
1238 if (!locCode.empty())
1239 {
1240 lc = locCode;
1241 }
1242
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001243 addInventoryCallout(inventoryPath, priority, lc, dataIface, mrus);
Matt Spinlerafa2c792020-08-27 11:01:39 -05001244
1245 if (jsonCallout.contains("Deconfigured"))
1246 {
1247 if (jsonCallout.at("Deconfigured").get<bool>())
1248 {
1249 setErrorStatusFlag(ErrorStatusFlags::deconfigured);
1250 }
1251 }
1252
1253 if (jsonCallout.contains("Guarded"))
1254 {
1255 if (jsonCallout.at("Guarded").get<bool>())
1256 {
1257 setErrorStatusFlag(ErrorStatusFlags::guarded);
1258 }
1259 }
Matt Spinler3bdd0112020-08-27 10:24:34 -05001260 }
1261
1262 if (callout)
1263 {
1264 createCalloutsObject();
1265 _callouts->addCallout(std::move(callout));
1266 }
1267}
1268
1269CalloutPriority SRC::getPriorityFromJSON(const nlohmann::json& json)
1270{
1271 // Looks like:
1272 // {
1273 // "Priority": "H"
1274 // }
1275 auto p = json.at("Priority").get<std::string>();
1276 if (p.empty())
1277 {
1278 throw std::runtime_error{"Priority field in callout is empty"};
1279 }
1280
1281 auto priority = static_cast<CalloutPriority>(p.front());
1282
1283 // Validate it
1284 auto priorityIt = pv::findByValue(static_cast<uint32_t>(priority),
1285 pv::calloutPriorityValues);
1286 if (priorityIt == pv::calloutPriorityValues.end())
1287 {
1288 throw std::runtime_error{
1289 fmt::format("Invalid priority '{}' found in JSON callout", p)};
1290 }
1291
1292 return priority;
Matt Spinler5a90a952020-08-27 09:39:03 -05001293}
1294
Matt Spinlerb8cb60f2020-08-27 10:55:55 -05001295std::vector<src::MRU::MRUCallout>
1296 SRC::getMRUsFromJSON(const nlohmann::json& mruJSON)
1297{
1298 std::vector<src::MRU::MRUCallout> mrus;
1299
1300 // Looks like:
1301 // [
1302 // {
1303 // "ID": 100,
1304 // "Priority": "H"
1305 // }
1306 // ]
1307 if (!mruJSON.is_array())
1308 {
1309 addDebugData("MRU callout JSON is not an array");
1310 return mrus;
1311 }
1312
1313 for (const auto& mruCallout : mruJSON)
1314 {
1315 try
1316 {
1317 auto priority = getPriorityFromJSON(mruCallout);
1318 auto id = mruCallout.at("ID").get<uint32_t>();
1319
1320 src::MRU::MRUCallout mru{static_cast<uint32_t>(priority), id};
1321 mrus.push_back(std::move(mru));
1322 }
1323 catch (const std::exception& e)
1324 {
1325 addDebugData(fmt::format("Invalid MRU entry in JSON: {}: {}",
1326 mruCallout.dump(), e.what()));
1327 }
1328 }
1329
1330 return mrus;
1331}
1332
Matt Spinlerf9bae182019-10-09 13:37:38 -05001333} // namespace pels
1334} // namespace openpower