SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "manager.hpp" |
| 4 | |
SunnySrivastava1984 | 19be6d3 | 2020-03-03 07:21:45 -0600 | [diff] [blame] | 5 | #include "const.hpp" |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 6 | #include "parser.hpp" |
| 7 | |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame^] | 8 | using namespace openpower::vpd::constants; |
| 9 | using namespace openpower::vpd::manager::editor; |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 10 | |
| 11 | namespace openpower |
| 12 | { |
| 13 | namespace vpd |
| 14 | { |
| 15 | namespace manager |
| 16 | { |
| 17 | Manager::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 | |
| 25 | void Manager::run() |
| 26 | { |
SunnySrivastava1984 | de3c60d | 2020-02-03 10:34:33 -0600 | [diff] [blame] | 27 | try |
| 28 | { |
| 29 | processJSON(); |
| 30 | } |
| 31 | catch (const std::exception& e) |
| 32 | { |
| 33 | std::cerr << e.what() << "\n"; |
| 34 | } |
| 35 | |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 36 | while (true) |
| 37 | { |
SunnySrivastava1984 | de3c60d | 2020-02-03 10:34:33 -0600 | [diff] [blame] | 38 | _bus.process_discard(); |
| 39 | |
| 40 | // wait for event |
| 41 | _bus.wait(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void 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) |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 67 | { |
SunnySrivastava1984 | de3c60d | 2020-02-03 10:34:33 -0600 | [diff] [blame] | 68 | frus.emplace(itemEEPROM["inventoryPath"] |
| 69 | .get_ref<const nlohmann::json::string_t&>(), |
| 70 | itemFRUS.key()); |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void Manager::writeKeyword(const sdbusplus::message::object_path path, |
| 76 | const std::string recordName, |
| 77 | const std::string keyword, const Binary value) |
| 78 | { |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame^] | 79 | 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 | } |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | std::vector<sdbusplus::message::object_path> |
| 125 | Manager::getFRUsByUnexpandedLocationCode(const std::string locationCode, |
| 126 | const uint16_t nodeNumber) |
| 127 | { |
| 128 | // implement the interface |
| 129 | } |
| 130 | |
| 131 | std::vector<sdbusplus::message::object_path> |
| 132 | Manager::getFRUsByExpandedLocationCode(const std::string locationCode) |
| 133 | { |
| 134 | // implement the interface |
| 135 | } |
| 136 | |
| 137 | std::string Manager::getExpandedLocationCode(const std::string locationCode, |
| 138 | const uint16_t nodeNumber) |
| 139 | { |
| 140 | // implement the interface |
| 141 | } |
SunnySrivastava1984 | de3c60d | 2020-02-03 10:34:33 -0600 | [diff] [blame] | 142 | |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 143 | } // namespace manager |
| 144 | } // namespace vpd |
| 145 | } // namespace openpower |