blob: 3d8462c8218634e08d354a704cd13afd6b9e5127 [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"
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
83 fruLocationCode.emplace(
84 itemEEPROM["extraInterfaces"][LOCATION_CODE_INF]["LocationCode"]
85 .get_ref<const nlohmann::json::string_t&>(),
86 itemEEPROM["inventoryPath"]
87 .get_ref<const nlohmann::json::string_t&>());
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060088 }
89 }
90}
91
92void Manager::writeKeyword(const sdbusplus::message::object_path path,
93 const std::string recordName,
94 const std::string keyword, const Binary value)
95{
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060096 try
97 {
98 if (frus.find(path) == frus.end())
99 {
100 throw std::runtime_error("Inventory path not found");
101 }
102
SunnySrivastava198443306542020-04-01 02:50:20 -0500103 inventory::Path vpdFilePath = frus.find(path)->second.first;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600104
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500105 // instantiate editor class to update the data
SunnySrivastava1984b421bfd2020-03-02 08:59:32 -0600106 EditorImpl edit(vpdFilePath, jsonFile, recordName, keyword);
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500107 edit.updateKeyword(value);
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600108
SunnySrivastava198443306542020-04-01 02:50:20 -0500109 // if it is a motehrboard FRU need to check for location expansion
110 if (frus.find(path)->second.second)
111 {
112 if (recordName == "VCEN" && (keyword == "FC" || keyword == "SE"))
113 {
114 edit.expandLocationCode("fcs");
115 }
116 else if (recordName == "VSYS" &&
117 (keyword == "TM" || keyword == "SE"))
118 {
119 edit.expandLocationCode("mts");
120 }
121 }
122
SunnySrivastava19846d8314d2020-05-15 09:34:58 -0500123 return;
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -0600124 }
125 catch (const std::exception& e)
126 {
127 std::cerr << e.what() << std::endl;
128 }
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600129}
130
SunnySrivastava19841356d7e2020-04-24 04:29:35 -0500131ListOfPaths
SunnySrivastava1984fb5815a2020-04-24 08:03:52 -0500132 Manager::getFRUsByUnexpandedLocationCode(const LocationCode locationCode,
133 const NodeNumber nodeNumber)
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600134{
SunnySrivastava19841356d7e2020-04-24 04:29:35 -0500135 ReaderImpl read;
136 return read.getFrusAtLocation(locationCode, nodeNumber, fruLocationCode);
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600137}
138
SunnySrivastava19841356d7e2020-04-24 04:29:35 -0500139ListOfPaths
SunnySrivastava1984fb5815a2020-04-24 08:03:52 -0500140 Manager::getFRUsByExpandedLocationCode(const LocationCode locationCode)
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600141{
SunnySrivastava1984fb5815a2020-04-24 08:03:52 -0500142 ReaderImpl read;
143 return read.getFRUsByExpandedLocationCode(locationCode, fruLocationCode);
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600144}
145
SunnySrivastava1984fb5815a2020-04-24 08:03:52 -0500146LocationCode Manager::getExpandedLocationCode(const LocationCode locationCode,
147 const NodeNumber nodeNumber)
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600148{
SunnySrivastava1984bca5aaa2020-04-21 05:31:04 -0500149 ReaderImpl read;
150 return read.getExpandedLocationCode(locationCode, nodeNumber,
151 fruLocationCode);
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600152}
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -0600153
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600154} // namespace manager
155} // namespace vpd
156} // namespace openpower