Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1 | #include "read_fru_data.hpp" |
| 2 | |
| 3 | #include "fruread.hpp" |
| 4 | #include "types.hpp" |
| 5 | #include "utils.hpp" |
| 6 | |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 7 | #include <map> |
| 8 | #include <phosphor-logging/elog-errors.hpp> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 9 | #include <xyz/openbmc_project/Common/error.hpp> |
| 10 | |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 11 | #include "host-ipmid/ipmid-api.h" |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 12 | |
| 13 | extern const FruMap frus; |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 14 | namespace ipmi |
| 15 | { |
| 16 | namespace fru |
| 17 | { |
| 18 | using namespace phosphor::logging; |
| 19 | using InternalFailure = |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 20 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 21 | std::unique_ptr<sdbusplus::bus::match_t> matchPtr(nullptr); |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 22 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 23 | static constexpr auto INV_INTF = "xyz.openbmc_project.Inventory.Manager"; |
| 24 | static constexpr auto OBJ_PATH = "/xyz/openbmc_project/inventory"; |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 25 | static constexpr auto PROP_INTF = "org.freedesktop.DBus.Properties"; |
| 26 | |
| 27 | namespace cache |
| 28 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 29 | // User initiate read FRU info area command followed by |
| 30 | // FRU read command. Also data is read in small chunks of |
| 31 | // the specified offset and count. |
| 32 | // Caching the data which will be invalidated when ever there |
| 33 | // is a change in FRU properties. |
| 34 | FRUAreaMap fruMap; |
| 35 | } // namespace cache |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 36 | /** |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 37 | * @brief Read all the property value's for the specified interface |
| 38 | * from Inventory. |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 39 | * |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 40 | * @param[in] intf Interface |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 41 | * @param[in] path Object path |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 42 | * @return map of properties |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 43 | */ |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 44 | ipmi::PropertyMap readAllProperties(const std::string& intf, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 45 | const std::string& path) |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 46 | { |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 47 | ipmi::PropertyMap properties; |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 48 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 49 | auto service = ipmi::getService(bus, INV_INTF, OBJ_PATH); |
| 50 | std::string objPath = OBJ_PATH + path; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 51 | auto method = bus.new_method_call(service.c_str(), objPath.c_str(), |
| 52 | PROP_INTF, "GetAll"); |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 53 | method.append(intf); |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 54 | auto reply = bus.call(method); |
| 55 | if (reply.is_method_error()) |
| 56 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 57 | // If property is not found simply return empty value |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 58 | log<level::ERR>("Error in reading property values from inventory", |
Joseph Reynolds | 510eb9c | 2018-05-30 11:51:28 -0500 | [diff] [blame] | 59 | entry("INTERFACE=%s", intf.c_str()), |
| 60 | entry("PATH=%s", objPath.c_str())); |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 61 | return properties; |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 62 | } |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 63 | reply.read(properties); |
| 64 | return properties; |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 65 | } |
| 66 | |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 67 | void processFruPropChange(sdbusplus::message::message& msg) |
| 68 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 69 | if (cache::fruMap.empty()) |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 70 | { |
| 71 | return; |
| 72 | } |
| 73 | std::string path = msg.get_path(); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 74 | // trim the object base path, if found at the beginning |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 75 | if (path.compare(0, strlen(OBJ_PATH), OBJ_PATH) == 0) |
| 76 | { |
| 77 | path.erase(0, strlen(OBJ_PATH)); |
| 78 | } |
| 79 | for (auto& fru : frus) |
| 80 | { |
| 81 | bool found = false; |
| 82 | auto& fruId = fru.first; |
| 83 | auto& instanceList = fru.second; |
| 84 | for (auto& instance : instanceList) |
| 85 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 86 | if (instance.path == path) |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 87 | { |
| 88 | found = true; |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | if (found) |
| 93 | { |
| 94 | cache::fruMap.erase(fruId); |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 100 | // register for fru property change |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 101 | int registerCallbackHandler() |
| 102 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 103 | if (matchPtr == nullptr) |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 104 | { |
| 105 | using namespace sdbusplus::bus::match::rules; |
| 106 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 107 | matchPtr = std::make_unique<sdbusplus::bus::match_t>( |
| 108 | bus, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 109 | path_namespace(OBJ_PATH) + type::signal() + |
| 110 | member("PropertiesChanged") + interface(PROP_INTF), |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 111 | std::bind(processFruPropChange, std::placeholders::_1)); |
| 112 | } |
| 113 | return 0; |
| 114 | } |
| 115 | |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 116 | /** |
| 117 | * @brief Read FRU property values from Inventory |
| 118 | * |
| 119 | * @param[in] fruNum FRU id |
| 120 | * @return populate FRU Inventory data |
| 121 | */ |
| 122 | FruInventoryData readDataFromInventory(const FRUId& fruNum) |
| 123 | { |
| 124 | auto iter = frus.find(fruNum); |
| 125 | if (iter == frus.end()) |
| 126 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 127 | log<level::ERR>("Unsupported FRU ID ", entry("FRUID=%d", fruNum)); |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 128 | elog<InternalFailure>(); |
| 129 | } |
| 130 | |
| 131 | FruInventoryData data; |
| 132 | auto& instanceList = iter->second; |
| 133 | for (auto& instance : instanceList) |
| 134 | { |
Ratan Gupta | 0033097 | 2018-01-19 16:23:10 +0530 | [diff] [blame] | 135 | for (auto& intf : instance.interfaces) |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 136 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 137 | ipmi::PropertyMap allProp = |
| 138 | readAllProperties(intf.first, instance.path); |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 139 | for (auto& properties : intf.second) |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 140 | { |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 141 | auto iter = allProp.find(properties.first); |
| 142 | if (iter != allProp.end()) |
| 143 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 144 | data[properties.second.section].emplace( |
| 145 | properties.first, |
| 146 | std::move( |
| 147 | allProp[properties.first].get<std::string>())); |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 148 | } |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | } |
| 152 | return data; |
| 153 | } |
| 154 | |
| 155 | const FruAreaData& getFruAreaData(const FRUId& fruNum) |
| 156 | { |
| 157 | auto iter = cache::fruMap.find(fruNum); |
| 158 | if (iter != cache::fruMap.end()) |
| 159 | { |
| 160 | return iter->second; |
| 161 | } |
| 162 | auto invData = readDataFromInventory(fruNum); |
| 163 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 164 | // Build area info based on inventory data |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 165 | FruAreaData newdata = buildFruAreaData(std::move(invData)); |
| 166 | cache::fruMap.emplace(fruNum, std::move(newdata)); |
| 167 | return cache::fruMap.at(fruNum); |
| 168 | } |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 169 | } // namespace fru |
| 170 | } // namespace ipmi |