blob: 79ab5a5645963babbe17afddd2e0095e2482005e [file] [log] [blame]
SunnySrivastava1984b59fd092020-02-03 09:58:56 -06001#include "config.h"
2
3#include "manager.hpp"
4
SunnySrivastava198419be6d32020-03-03 07:21:45 -06005#include "const.hpp"
SunnySrivastava1984b59fd092020-02-03 09:58:56 -06006#include "parser.hpp"
7
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -06008using namespace openpower::vpd::constants;
9using namespace openpower::vpd::manager::editor;
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060010
11namespace openpower
12{
13namespace vpd
14{
15namespace manager
16{
17Manager::Manager(sdbusplus::bus::bus&& bus, const char* busName,
18 const char* objPath, const char* iFace) :
19 ServerObject<ManagerIface>(bus, objPath),
20 _bus(std::move(bus)), _manager(_bus, objPath)
21{
22 _bus.request_name(busName);
23}
24
25void Manager::run()
26{
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -060027 try
28 {
29 processJSON();
30 }
31 catch (const std::exception& e)
32 {
33 std::cerr << e.what() << "\n";
34 }
35
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060036 while (true)
37 {
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -060038 _bus.process_discard();
39
40 // wait for event
41 _bus.wait();
42 }
43}
44
45void Manager::processJSON()
46{
47 std::ifstream json(INVENTORY_JSON, std::ios::binary);
48
49 if (!json)
50 {
51 throw std::runtime_error("json file not found");
52 }
53
54 jsonFile = nlohmann::json::parse(json);
55 if (jsonFile.find("frus") == jsonFile.end())
56 {
57 throw std::runtime_error("frus group not found in json");
58 }
59
60 const nlohmann::json& groupFRUS =
61 jsonFile["frus"].get_ref<const nlohmann::json::object_t&>();
62 for (const auto& itemFRUS : groupFRUS.items())
63 {
64 const std::vector<nlohmann::json>& groupEEPROM =
65 itemFRUS.value().get_ref<const nlohmann::json::array_t&>();
66 for (const auto& itemEEPROM : groupEEPROM)
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060067 {
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -060068 frus.emplace(itemEEPROM["inventoryPath"]
69 .get_ref<const nlohmann::json::string_t&>(),
70 itemFRUS.key());
SunnySrivastava1984b59fd092020-02-03 09:58:56 -060071 }
72 }
73}
74
75void Manager::writeKeyword(const sdbusplus::message::object_path path,
76 const std::string recordName,
77 const std::string keyword, const Binary value)
78{
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060079 try
80 {
81 if (frus.find(path) == frus.end())
82 {
83 throw std::runtime_error("Inventory path not found");
84 }
85
86 inventory::Path vpdFilePath = frus.find(path)->second;
87 std::ifstream vpdStream(vpdFilePath, std::ios::binary);
88 if (!vpdStream)
89 {
90 throw std::runtime_error("file not found");
91 }
92
93 Byte data;
94 vpdStream.seekg(IPZ_DATA_START, std::ios::beg);
95 vpdStream.get(*(reinterpret_cast<char*>(&data)));
96
97 // implies it is IPZ VPD
98 if (data == KW_VAL_PAIR_START_TAG)
99 {
100 Binary vpdHeader(lengths::VHDR_RECORD_LENGTH +
101 lengths::VHDR_ECC_LENGTH);
102 vpdStream.seekg(0);
103 vpdStream.read(reinterpret_cast<char*>(vpdHeader.data()),
104 vpdHeader.capacity());
105
106 // check if header is valid
107 openpower::vpd::keyword::editor::processHeader(
108 std::move(vpdHeader));
109
110 // instantiate editor class to update the data
111 EditorImpl edit(vpdFilePath, recordName, keyword);
112 edit.updateKeyword(value);
113
114 return;
115 }
116 throw std::runtime_error("Invalid VPD file type");
117 }
118 catch (const std::exception& e)
119 {
120 std::cerr << e.what() << std::endl;
121 }
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600122}
123
124std::vector<sdbusplus::message::object_path>
125 Manager::getFRUsByUnexpandedLocationCode(const std::string locationCode,
126 const uint16_t nodeNumber)
127{
128 // implement the interface
129}
130
131std::vector<sdbusplus::message::object_path>
132 Manager::getFRUsByExpandedLocationCode(const std::string locationCode)
133{
134 // implement the interface
135}
136
137std::string Manager::getExpandedLocationCode(const std::string locationCode,
138 const uint16_t nodeNumber)
139{
140 // implement the interface
141}
SunnySrivastava1984de3c60d2020-02-03 10:34:33 -0600142
SunnySrivastava1984b59fd092020-02-03 09:58:56 -0600143} // namespace manager
144} // namespace vpd
145} // namespace openpower