blob: 1ce1cd17477d7ef39c02de707340cf9856658131 [file] [log] [blame]
SunnySrivastava1984b59fd092020-02-03 09:58:56 -06001#include "config.h"
2
3#include "manager.hpp"
4
SunnySrivastava1984d076da82020-03-05 05:33:35 -06005#include "editor_impl.hpp"
SunnySrivastava1984b59fd092020-02-03 09:58:56 -06006#include "parser.hpp"
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -05007#include "reader_impl.hpp"
SunnySrivastava1984b59fd092020-02-03 09:58:56 -06008
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -06009using namespace openpower::vpd::constants;
10using namespace openpower::vpd::manager::editor;
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -050011using namespace openpower::vpd::manager::reader;
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060012
13namespace openpower
14{
15namespace vpd
16{
17namespace manager
18{
19Manager::Manager(sdbusplus::bus::bus&& bus, const char* busName,
20 const char* objPath, const char* iFace) :
21 ServerObject<ManagerIface>(bus, objPath),
22 _bus(std::move(bus)), _manager(_bus, objPath)
23{
24 _bus.request_name(busName);
25}
26
27void Manager::run()
28{
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -060029 try
30 {
31 processJSON();
32 }
33 catch (const std::exception& e)
34 {
35 std::cerr << e.what() << "\n";
36 }
37
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060038 while (true)
39 {
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -060040 _bus.process_discard();
41
42 // wait for event
43 _bus.wait();
44 }
45}
46
47void Manager::processJSON()
48{
49 std::ifstream json(INVENTORY_JSON, std::ios::binary);
50
51 if (!json)
52 {
53 throw std::runtime_error("json file not found");
54 }
55
56 jsonFile = nlohmann::json::parse(json);
57 if (jsonFile.find("frus") == jsonFile.end())
58 {
59 throw std::runtime_error("frus group not found in json");
60 }
61
62 const nlohmann::json& groupFRUS =
63 jsonFile["frus"].get_ref<const nlohmann::json::object_t&>();
64 for (const auto& itemFRUS : groupFRUS.items())
65 {
66 const std::vector<nlohmann::json>& groupEEPROM =
67 itemFRUS.value().get_ref<const nlohmann::json::array_t&>();
68 for (const auto& itemEEPROM : groupEEPROM)
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060069 {
SunnySrivastava198443306542020-04-01 02:50:20 -050070 bool isMotherboard = false;
71 if (itemEEPROM["extraInterfaces"].find(
72 "xyz.openbmc_project.Inventory.Item.Board.Motherboard") !=
73 itemEEPROM["extraInterfaces"].end())
74 {
75 isMotherboard = true;
76 }
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -060077 frus.emplace(itemEEPROM["inventoryPath"]
78 .get_ref<const nlohmann::json::string_t&>(),
SunnySrivastava198443306542020-04-01 02:50:20 -050079 std::make_pair(itemFRUS.key(), isMotherboard));
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -050080
81 fruLocationCode.emplace(
82 itemEEPROM["extraInterfaces"][LOCATION_CODE_INF]["LocationCode"]
83 .get_ref<const nlohmann::json::string_t&>(),
84 itemEEPROM["inventoryPath"]
85 .get_ref<const nlohmann::json::string_t&>());
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060086 }
87 }
88}
89
90void Manager::writeKeyword(const sdbusplus::message::object_path path,
91 const std::string recordName,
92 const std::string keyword, const Binary value)
93{
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060094 try
95 {
96 if (frus.find(path) == frus.end())
97 {
98 throw std::runtime_error("Inventory path not found");
99 }
100
SunnySrivastava198443306542020-04-01 02:50:20 -0500101 inventory::Path vpdFilePath = frus.find(path)->second.first;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600102
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500103 // instantiate editor class to update the data
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600104 EditorImpl edit(vpdFilePath, jsonFile, recordName, keyword);
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500105 edit.updateKeyword(value);
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600106
SunnySrivastava198443306542020-04-01 02:50:20 -0500107 // if it is a motehrboard FRU need to check for location expansion
108 if (frus.find(path)->second.second)
109 {
110 if (recordName == "VCEN" && (keyword == "FC" || keyword == "SE"))
111 {
112 edit.expandLocationCode("fcs");
113 }
114 else if (recordName == "VSYS" &&
115 (keyword == "TM" || keyword == "SE"))
116 {
117 edit.expandLocationCode("mts");
118 }
119 }
120
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500121 return;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600122 }
123 catch (const std::exception& e)
124 {
125 std::cerr << e.what() << std::endl;
126 }
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600127}
128
129std::vector<sdbusplus::message::object_path>
130 Manager::getFRUsByUnexpandedLocationCode(const std::string locationCode,
131 const uint16_t nodeNumber)
132{
133 // implement the interface
134}
135
136std::vector<sdbusplus::message::object_path>
137 Manager::getFRUsByExpandedLocationCode(const std::string locationCode)
138{
139 // implement the interface
140}
141
142std::string Manager::getExpandedLocationCode(const std::string locationCode,
143 const uint16_t nodeNumber)
144{
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -0500145 ReaderImpl read;
146 return read.getExpandedLocationCode(locationCode, nodeNumber,
147 fruLocationCode);
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600148}
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -0600149
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600150} // namespace manager
151} // namespace vpd
152} // namespace openpower