blob: aa85dc8f544bab713eefe46c1aef72865050d6da [file] [log] [blame]
Tom Joseph151d5332019-11-17 22:21:45 +05301#include "fru_parser.hpp"
2
George Liu6492f522020-06-16 10:34:05 +08003#include <nlohmann/json.hpp>
4#include <xyz/openbmc_project/Common/error.hpp>
5
Tom Joseph151d5332019-11-17 22:21:45 +05306#include <filesystem>
7#include <fstream>
8#include <iostream>
Tom Joseph151d5332019-11-17 22:21:45 +05309
Brad Bishop5079ac42021-08-19 18:35:06 -040010using namespace pldm::responder::dbus;
11
Tom Joseph151d5332019-11-17 22:21:45 +053012namespace pldm
13{
14
15namespace responder
16{
17
18namespace fru_parser
19{
20
21using Json = nlohmann::json;
22using InternalFailure =
23 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
24
25const Json emptyJson{};
26const std::vector<Json> emptyJsonList{};
27const std::vector<std::string> emptyStringVec{};
28
Manojkiran Eda03b01ca2021-06-29 08:55:09 +053029FruParser::FruParser(const std::string& dirPath,
30 const fs::path& fruMasterJsonPath)
Tom Joseph151d5332019-11-17 22:21:45 +053031{
Manojkiran Eda03b01ca2021-06-29 08:55:09 +053032 if (fs::exists(fruMasterJsonPath))
33 {
34 setupDefaultDBusLookup(fruMasterJsonPath);
35 }
Deepak Kodihallida079602020-08-05 03:23:03 -050036 setupDefaultFruRecordMap();
37
Tom Joseph151d5332019-11-17 22:21:45 +053038 fs::path dir(dirPath);
Deepak Kodihallida079602020-08-05 03:23:03 -050039 if (fs::exists(dir) && !fs::is_empty(dir))
Tom Joseph151d5332019-11-17 22:21:45 +053040 {
Deepak Kodihallida079602020-08-05 03:23:03 -050041 setupFruRecordMap(dirPath);
Tom Joseph151d5332019-11-17 22:21:45 +053042 }
Tom Joseph151d5332019-11-17 22:21:45 +053043}
44
Manojkiran Eda03b01ca2021-06-29 08:55:09 +053045void FruParser::setupDefaultDBusLookup(const fs::path& masterJsonPath)
John Wang55732c22020-05-14 10:33:00 +080046{
47 constexpr auto service = "xyz.openbmc_project.Inventory.Manager";
48 constexpr auto rootPath = "/xyz/openbmc_project/inventory";
Manojkiran Eda03b01ca2021-06-29 08:55:09 +053049 std::ifstream jsonFile(masterJsonPath);
50 auto data = Json::parse(jsonFile, nullptr, false);
51 if (data.is_discarded())
52 {
Manojkiran Eda03b01ca2021-06-29 08:55:09 +053053 std::cerr << "Parsing FRU Dbus Lookup Map config file failed, FILE="
54 << masterJsonPath;
55 std::abort();
56 }
57 std::map<Interface, EntityType> defIntfToEntityType;
58 auto dbusMap = data.value("FruDBusLookupMap", emptyJson);
59 for (const auto& element : dbusMap.items())
60
61 {
62 try
63 {
64 defIntfToEntityType[static_cast<Interface>(element.key())] =
65 static_cast<EntityType>(element.value());
66 }
67 catch (const std::exception& e)
68 {
69 std::cerr << "FRU DBus lookup map format error\n";
70 throw InternalFailure();
71 }
72 }
John Wang55732c22020-05-14 10:33:00 +080073
74 Interfaces interfaces{};
75 for (auto [intf, entityType] : defIntfToEntityType)
76 {
77 intfToEntityType[intf] = entityType;
78 interfaces.emplace(intf);
79 }
80
81 lookupInfo.emplace(service, rootPath, std::move(interfaces));
82}
83
John Wang55732c22020-05-14 10:33:00 +080084void FruParser::setupDefaultFruRecordMap()
85{
86 const FruRecordInfo generalRecordInfo = {
87 1, // generalRecordType
88 1, // encodingTypeASCII
89 {
90 // DSP0257 Table 5 General FRU Record Field Type Definitions
91 {"xyz.openbmc_project.Inventory.Decorator.Asset", "Model", "string",
92 2},
93 {"xyz.openbmc_project.Inventory.Decorator.Asset", "PartNumber",
94 "string", 3},
95 {"xyz.openbmc_project.Inventory.Decorator.Asset", "SerialNumber",
96 "string", 4},
97 {"xyz.openbmc_project.Inventory.Decorator.Asset", "Manufacturer",
98 "string", 5},
99 {"xyz.openbmc_project.Inventory.Item", "PrettyName", "string", 8},
100 {"xyz.openbmc_project.Inventory.Decorator.AssetTag", "AssetTag",
101 "string", 11},
102 {"xyz.openbmc_project.Inventory.Decorator.Revision", "Version",
103 "string", 10},
104 }};
105
106 for (auto [intf, entityType] : intfToEntityType)
107 {
108 recordMap[intf] = {generalRecordInfo};
109 }
110}
111
Tom Joseph151d5332019-11-17 22:21:45 +0530112void FruParser::setupFruRecordMap(const std::string& dirPath)
113{
114 for (auto& file : fs::directory_iterator(dirPath))
115 {
116 auto fileName = file.path().filename().string();
Tom Joseph151d5332019-11-17 22:21:45 +0530117 std::ifstream jsonFile(file.path());
118 auto data = Json::parse(jsonFile, nullptr, false);
119 if (data.is_discarded())
120 {
Deepak Kodihallida079602020-08-05 03:23:03 -0500121 std::cerr << "Parsing FRU config file failed, FILE=" << file.path();
Tom Joseph151d5332019-11-17 22:21:45 +0530122 throw InternalFailure();
123 }
124
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500125 try
Tom Joseph151d5332019-11-17 22:21:45 +0530126 {
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500127 auto record = data.value("record_details", emptyJson);
128 auto recordType =
129 static_cast<uint8_t>(record.value("fru_record_type", 0));
130 auto encType =
131 static_cast<uint8_t>(record.value("fru_encoding_type", 0));
132 auto dbusIntfName = record.value("dbus_interface_name", "");
133 auto entries = data.value("fru_fields", emptyJsonList);
134 std::vector<FieldInfo> fieldInfo;
135
136 for (const auto& entry : entries)
137 {
138 auto fieldType =
139 static_cast<uint8_t>(entry.value("fru_field_type", 0));
140 auto dbus = entry.value("dbus", emptyJson);
141 auto interface = dbus.value("interface", "");
142 auto property = dbus.value("property_name", "");
143 auto propType = dbus.value("property_type", "");
144 fieldInfo.emplace_back(
145 std::make_tuple(std::move(interface), std::move(property),
146 std::move(propType), std::move(fieldType)));
147 }
148
149 FruRecordInfo fruInfo;
150 fruInfo =
151 std::make_tuple(recordType, encType, std::move(fieldInfo));
152
153 auto search = recordMap.find(dbusIntfName);
154
155 // PLDM FRU can have multiple records for the same FRU like General
156 // FRU record and multiple OEM FRU records. If the FRU item
157 // interface name is already in the map, that indicates a record
158 // info is already added for the FRU, so append the new record info
159 // to the same data.
160 if (search != recordMap.end())
161 {
162 search->second.emplace_back(std::move(fruInfo));
163 }
164 else
165 {
166 FruRecordInfos recordInfos{fruInfo};
167 recordMap.emplace(dbusIntfName, recordInfos);
168 }
Tom Joseph151d5332019-11-17 22:21:45 +0530169 }
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500170 catch (const std::exception& e)
Tom Joseph151d5332019-11-17 22:21:45 +0530171 {
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500172 continue;
Tom Joseph151d5332019-11-17 22:21:45 +0530173 }
174 }
175}
176
177} // namespace fru_parser
178
179} // namespace responder
180
181} // namespace pldm