blob: a313e8636f04472ff7af609140cf6c657dead4d9 [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"
SunnySrivastava1984e12b1812020-05-26 02:23:11 -05006#include "ipz_parser.hpp"
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -05007#include "reader_impl.hpp"
SunnySrivastava19841356d7e2020-04-24 04:29:35 -05008#include "utils.hpp"
SunnySrivastava1984b59fd092020-02-03 09:58:56 -06009
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060010using namespace openpower::vpd::constants;
SunnySrivastava19841356d7e2020-04-24 04:29:35 -050011using namespace openpower::vpd::inventory;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060012using namespace openpower::vpd::manager::editor;
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -050013using namespace openpower::vpd::manager::reader;
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060014
15namespace openpower
16{
17namespace vpd
18{
19namespace manager
20{
21Manager::Manager(sdbusplus::bus::bus&& bus, const char* busName,
22 const char* objPath, const char* iFace) :
23 ServerObject<ManagerIface>(bus, objPath),
24 _bus(std::move(bus)), _manager(_bus, objPath)
25{
26 _bus.request_name(busName);
27}
28
29void Manager::run()
30{
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -060031 try
32 {
33 processJSON();
34 }
35 catch (const std::exception& e)
36 {
37 std::cerr << e.what() << "\n";
38 }
39
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060040 while (true)
41 {
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -060042 _bus.process_discard();
43
44 // wait for event
45 _bus.wait();
46 }
47}
48
49void Manager::processJSON()
50{
51 std::ifstream json(INVENTORY_JSON, std::ios::binary);
52
53 if (!json)
54 {
55 throw std::runtime_error("json file not found");
56 }
57
58 jsonFile = nlohmann::json::parse(json);
59 if (jsonFile.find("frus") == jsonFile.end())
60 {
61 throw std::runtime_error("frus group not found in json");
62 }
63
64 const nlohmann::json& groupFRUS =
65 jsonFile["frus"].get_ref<const nlohmann::json::object_t&>();
66 for (const auto& itemFRUS : groupFRUS.items())
67 {
68 const std::vector<nlohmann::json>& groupEEPROM =
69 itemFRUS.value().get_ref<const nlohmann::json::array_t&>();
70 for (const auto& itemEEPROM : groupEEPROM)
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060071 {
SunnySrivastava198443306542020-04-01 02:50:20 -050072 bool isMotherboard = false;
73 if (itemEEPROM["extraInterfaces"].find(
74 "xyz.openbmc_project.Inventory.Item.Board.Motherboard") !=
75 itemEEPROM["extraInterfaces"].end())
76 {
77 isMotherboard = true;
78 }
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -060079 frus.emplace(itemEEPROM["inventoryPath"]
80 .get_ref<const nlohmann::json::string_t&>(),
SunnySrivastava198443306542020-04-01 02:50:20 -050081 std::make_pair(itemFRUS.key(), isMotherboard));
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -050082
Alpana Kumari920408d2020-05-14 00:07:03 -050083 if (itemEEPROM["extraInterfaces"].find(LOCATION_CODE_INF) !=
84 itemEEPROM["extraInterfaces"].end())
85 {
86 fruLocationCode.emplace(
87 itemEEPROM["extraInterfaces"][LOCATION_CODE_INF]
88 ["LocationCode"]
89 .get_ref<const nlohmann::json::string_t&>(),
90 itemEEPROM["inventoryPath"]
91 .get_ref<const nlohmann::json::string_t&>());
92 }
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060093 }
94 }
95}
96
97void Manager::writeKeyword(const sdbusplus::message::object_path path,
98 const std::string recordName,
99 const std::string keyword, const Binary value)
100{
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600101 try
102 {
103 if (frus.find(path) == frus.end())
104 {
105 throw std::runtime_error("Inventory path not found");
106 }
107
SunnySrivastava198443306542020-04-01 02:50:20 -0500108 inventory::Path vpdFilePath = frus.find(path)->second.first;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600109
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500110 // instantiate editor class to update the data
Alpana Kumari920408d2020-05-14 00:07:03 -0500111 EditorImpl edit(vpdFilePath, jsonFile, recordName, keyword, path);
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500112 edit.updateKeyword(value);
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600113
SunnySrivastava198443306542020-04-01 02:50:20 -0500114 // if it is a motehrboard FRU need to check for location expansion
115 if (frus.find(path)->second.second)
116 {
117 if (recordName == "VCEN" && (keyword == "FC" || keyword == "SE"))
118 {
119 edit.expandLocationCode("fcs");
120 }
121 else if (recordName == "VSYS" &&
122 (keyword == "TM" || keyword == "SE"))
123 {
124 edit.expandLocationCode("mts");
125 }
126 }
127
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500128 return;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600129 }
130 catch (const std::exception& e)
131 {
132 std::cerr << e.what() << std::endl;
133 }
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600134}
135
SunnySrivastava19841356d7e2020-04-24 04:29:35 -0500136ListOfPaths
SunnySrivastava1984fb5815a2020-04-24 08:03:52 -0500137 Manager::getFRUsByUnexpandedLocationCode(const LocationCode locationCode,
138 const NodeNumber nodeNumber)
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600139{
SunnySrivastava19841356d7e2020-04-24 04:29:35 -0500140 ReaderImpl read;
141 return read.getFrusAtLocation(locationCode, nodeNumber, fruLocationCode);
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600142}
143
SunnySrivastava19841356d7e2020-04-24 04:29:35 -0500144ListOfPaths
SunnySrivastava1984fb5815a2020-04-24 08:03:52 -0500145 Manager::getFRUsByExpandedLocationCode(const LocationCode locationCode)
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600146{
SunnySrivastava1984fb5815a2020-04-24 08:03:52 -0500147 ReaderImpl read;
148 return read.getFRUsByExpandedLocationCode(locationCode, fruLocationCode);
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600149}
150
SunnySrivastava1984fb5815a2020-04-24 08:03:52 -0500151LocationCode Manager::getExpandedLocationCode(const LocationCode locationCode,
152 const NodeNumber nodeNumber)
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600153{
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -0500154 ReaderImpl read;
155 return read.getExpandedLocationCode(locationCode, nodeNumber,
156 fruLocationCode);
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600157}
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -0600158
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600159} // namespace manager
160} // namespace vpd
161} // namespace openpower