blob: 327f552763e2617d304c67f149bbe01ae9bdd2a1 [file] [log] [blame]
Patrick Venture0b02be92018-08-31 11:55:55 -07001#include "read_fru_data.hpp"
2
3#include "fruread.hpp"
Patrick Venture0b02be92018-08-31 11:55:55 -07004
Patrick Ventureb0431c72018-10-22 14:57:36 -07005#include <algorithm>
Vernon Mauerye08fbff2019-04-03 09:19:34 -07006#include <ipmid/api.hpp>
Vernon Mauery33250242019-03-12 16:49:26 -07007#include <ipmid/types.hpp>
Vernon Mauery6a98fe72019-03-11 15:57:48 -07008#include <ipmid/utils.hpp>
Marri Devender Rao0acf0572017-07-03 12:25:47 -05009#include <map>
10#include <phosphor-logging/elog-errors.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -070011#include <sdbusplus/message/types.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070012#include <xyz/openbmc_project/Common/error.hpp>
13
Marri Devender Rao0acf0572017-07-03 12:25:47 -050014extern const FruMap frus;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050015namespace ipmi
16{
17namespace fru
18{
William A. Kennington III4c008022018-10-12 17:18:14 -070019
Marri Devender Rao0acf0572017-07-03 12:25:47 -050020using namespace phosphor::logging;
21using InternalFailure =
Patrick Venture0b02be92018-08-31 11:55:55 -070022 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Lei YU4b0ddb62019-01-25 16:43:50 +080023std::unique_ptr<sdbusplus::bus::match_t> matchPtr
24 __attribute__((init_priority(101)));
Marri Devender Rao0acf0572017-07-03 12:25:47 -050025
Patrick Venture0b02be92018-08-31 11:55:55 -070026static constexpr auto INV_INTF = "xyz.openbmc_project.Inventory.Manager";
27static constexpr auto OBJ_PATH = "/xyz/openbmc_project/inventory";
Marri Devender Rao0acf0572017-07-03 12:25:47 -050028static constexpr auto PROP_INTF = "org.freedesktop.DBus.Properties";
29
30namespace cache
31{
Patrick Venture0b02be92018-08-31 11:55:55 -070032// User initiate read FRU info area command followed by
33// FRU read command. Also data is read in small chunks of
34// the specified offset and count.
35// Caching the data which will be invalidated when ever there
36// is a change in FRU properties.
37FRUAreaMap fruMap;
38} // namespace cache
Marri Devender Rao0acf0572017-07-03 12:25:47 -050039/**
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050040 * @brief Read all the property value's for the specified interface
41 * from Inventory.
Marri Devender Rao0acf0572017-07-03 12:25:47 -050042 *
Marri Devender Rao0acf0572017-07-03 12:25:47 -050043 * @param[in] intf Interface
Marri Devender Rao0acf0572017-07-03 12:25:47 -050044 * @param[in] path Object path
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050045 * @return map of properties
Marri Devender Rao0acf0572017-07-03 12:25:47 -050046 */
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050047ipmi::PropertyMap readAllProperties(const std::string& intf,
Patrick Venture0b02be92018-08-31 11:55:55 -070048 const std::string& path)
Marri Devender Rao0acf0572017-07-03 12:25:47 -050049{
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050050 ipmi::PropertyMap properties;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050051 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
52 auto service = ipmi::getService(bus, INV_INTF, OBJ_PATH);
53 std::string objPath = OBJ_PATH + path;
Patrick Venture0b02be92018-08-31 11:55:55 -070054 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
55 PROP_INTF, "GetAll");
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050056 method.append(intf);
Marri Devender Rao0acf0572017-07-03 12:25:47 -050057 auto reply = bus.call(method);
58 if (reply.is_method_error())
59 {
Patrick Venture0b02be92018-08-31 11:55:55 -070060 // If property is not found simply return empty value
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050061 log<level::ERR>("Error in reading property values from inventory",
Joseph Reynolds510eb9c2018-05-30 11:51:28 -050062 entry("INTERFACE=%s", intf.c_str()),
63 entry("PATH=%s", objPath.c_str()));
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050064 return properties;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050065 }
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050066 reply.read(properties);
67 return properties;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050068}
69
Marri Devender Rao908f7502017-07-10 01:49:54 -050070void processFruPropChange(sdbusplus::message::message& msg)
71{
Patrick Venture0b02be92018-08-31 11:55:55 -070072 if (cache::fruMap.empty())
Marri Devender Rao908f7502017-07-10 01:49:54 -050073 {
74 return;
75 }
76 std::string path = msg.get_path();
Patrick Venture0b02be92018-08-31 11:55:55 -070077 // trim the object base path, if found at the beginning
Marri Devender Rao908f7502017-07-10 01:49:54 -050078 if (path.compare(0, strlen(OBJ_PATH), OBJ_PATH) == 0)
79 {
80 path.erase(0, strlen(OBJ_PATH));
81 }
Patrick Venture438fb792018-10-23 19:51:10 -070082 for (const auto& [fruId, instanceList] : frus)
Marri Devender Rao908f7502017-07-10 01:49:54 -050083 {
Patrick Ventureb0431c72018-10-22 14:57:36 -070084 auto found = std::find_if(
85 instanceList.begin(), instanceList.end(),
86 [&path](const auto& iter) { return (iter.path == path); });
87
88 if (found != instanceList.end())
Marri Devender Rao908f7502017-07-10 01:49:54 -050089 {
90 cache::fruMap.erase(fruId);
91 break;
92 }
93 }
94}
95
Patrick Venture0b02be92018-08-31 11:55:55 -070096// register for fru property change
Marri Devender Rao908f7502017-07-10 01:49:54 -050097int registerCallbackHandler()
98{
Patrick Venture0b02be92018-08-31 11:55:55 -070099 if (matchPtr == nullptr)
Marri Devender Rao908f7502017-07-10 01:49:54 -0500100 {
101 using namespace sdbusplus::bus::match::rules;
102 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
103 matchPtr = std::make_unique<sdbusplus::bus::match_t>(
104 bus,
Patrick Venture0b02be92018-08-31 11:55:55 -0700105 path_namespace(OBJ_PATH) + type::signal() +
106 member("PropertiesChanged") + interface(PROP_INTF),
Marri Devender Rao908f7502017-07-10 01:49:54 -0500107 std::bind(processFruPropChange, std::placeholders::_1));
108 }
109 return 0;
110}
111
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500112/**
113 * @brief Read FRU property values from Inventory
114 *
115 * @param[in] fruNum FRU id
116 * @return populate FRU Inventory data
117 */
118FruInventoryData readDataFromInventory(const FRUId& fruNum)
119{
120 auto iter = frus.find(fruNum);
121 if (iter == frus.end())
122 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700123 log<level::ERR>("Unsupported FRU ID ", entry("FRUID=%d", fruNum));
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500124 elog<InternalFailure>();
125 }
126
127 FruInventoryData data;
128 auto& instanceList = iter->second;
129 for (auto& instance : instanceList)
130 {
Ratan Gupta00330972018-01-19 16:23:10 +0530131 for (auto& intf : instance.interfaces)
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500132 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700133 ipmi::PropertyMap allProp =
134 readAllProperties(intf.first, instance.path);
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500135 for (auto& properties : intf.second)
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500136 {
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500137 auto iter = allProp.find(properties.first);
138 if (iter != allProp.end())
139 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700140 data[properties.second.section].emplace(
Patrick Venture45e93cb2019-07-25 15:03:19 -0700141 properties.second.property,
142 std::move(
143 std::get<std::string>(allProp[properties.first])));
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500144 }
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500145 }
146 }
147 }
148 return data;
149}
150
151const FruAreaData& getFruAreaData(const FRUId& fruNum)
152{
153 auto iter = cache::fruMap.find(fruNum);
154 if (iter != cache::fruMap.end())
155 {
156 return iter->second;
157 }
158 auto invData = readDataFromInventory(fruNum);
159
Patrick Venture0b02be92018-08-31 11:55:55 -0700160 // Build area info based on inventory data
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500161 FruAreaData newdata = buildFruAreaData(std::move(invData));
162 cache::fruMap.emplace(fruNum, std::move(newdata));
163 return cache::fruMap.at(fruNum);
164}
Patrick Venture0b02be92018-08-31 11:55:55 -0700165} // namespace fru
166} // namespace ipmi