blob: 270accf94a04fd974867f5f9aff2e787b111e363 [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 Venturec26cc712019-07-25 15:11:59 -070026static constexpr auto XYZ_PREFIX = "/xyz/openbmc_project/";
Patrick Venture0b02be92018-08-31 11:55:55 -070027static constexpr auto INV_INTF = "xyz.openbmc_project.Inventory.Manager";
28static constexpr auto OBJ_PATH = "/xyz/openbmc_project/inventory";
Marri Devender Rao0acf0572017-07-03 12:25:47 -050029static constexpr auto PROP_INTF = "org.freedesktop.DBus.Properties";
30
31namespace cache
32{
Patrick Venture0b02be92018-08-31 11:55:55 -070033// User initiate read FRU info area command followed by
34// FRU read command. Also data is read in small chunks of
35// the specified offset and count.
36// Caching the data which will be invalidated when ever there
37// is a change in FRU properties.
38FRUAreaMap fruMap;
39} // namespace cache
Marri Devender Rao0acf0572017-07-03 12:25:47 -050040/**
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050041 * @brief Read all the property value's for the specified interface
42 * from Inventory.
Marri Devender Rao0acf0572017-07-03 12:25:47 -050043 *
Marri Devender Rao0acf0572017-07-03 12:25:47 -050044 * @param[in] intf Interface
Marri Devender Rao0acf0572017-07-03 12:25:47 -050045 * @param[in] path Object path
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050046 * @return map of properties
Marri Devender Rao0acf0572017-07-03 12:25:47 -050047 */
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050048ipmi::PropertyMap readAllProperties(const std::string& intf,
Patrick Venture0b02be92018-08-31 11:55:55 -070049 const std::string& path)
Marri Devender Rao0acf0572017-07-03 12:25:47 -050050{
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050051 ipmi::PropertyMap properties;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050052 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Patrick Venturec26cc712019-07-25 15:11:59 -070053 std::string service;
54 std::string objPath;
55
56 // Is the path the full dbus path?
57 if (path.find(XYZ_PREFIX) != std::string::npos)
58 {
59 service = ipmi::getService(bus, intf, path);
60 objPath = path;
61 }
62 else
63 {
64 service = ipmi::getService(bus, INV_INTF, OBJ_PATH);
65 objPath = OBJ_PATH + path;
66 }
67
Patrick Venture0b02be92018-08-31 11:55:55 -070068 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
69 PROP_INTF, "GetAll");
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050070 method.append(intf);
Patrick Venturec26cc712019-07-25 15:11:59 -070071 try
72 {
73 auto reply = bus.call(method);
74 reply.read(properties);
75 }
76 catch (const sdbusplus::exception::SdBusError& e)
Marri Devender Rao0acf0572017-07-03 12:25:47 -050077 {
Patrick Venture0b02be92018-08-31 11:55:55 -070078 // If property is not found simply return empty value
Patrick Venturec26cc712019-07-25 15:11:59 -070079 log<level::ERR>("Error in reading property values",
80 entry("EXCEPTION=%s", e.what()),
Joseph Reynolds510eb9c2018-05-30 11:51:28 -050081 entry("INTERFACE=%s", intf.c_str()),
82 entry("PATH=%s", objPath.c_str()));
Marri Devender Rao0acf0572017-07-03 12:25:47 -050083 }
Patrick Venturec26cc712019-07-25 15:11:59 -070084
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050085 return properties;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050086}
87
Marri Devender Rao908f7502017-07-10 01:49:54 -050088void processFruPropChange(sdbusplus::message::message& msg)
89{
Patrick Venture0b02be92018-08-31 11:55:55 -070090 if (cache::fruMap.empty())
Marri Devender Rao908f7502017-07-10 01:49:54 -050091 {
92 return;
93 }
94 std::string path = msg.get_path();
Patrick Venture0b02be92018-08-31 11:55:55 -070095 // trim the object base path, if found at the beginning
Marri Devender Rao908f7502017-07-10 01:49:54 -050096 if (path.compare(0, strlen(OBJ_PATH), OBJ_PATH) == 0)
97 {
98 path.erase(0, strlen(OBJ_PATH));
99 }
Patrick Venture438fb792018-10-23 19:51:10 -0700100 for (const auto& [fruId, instanceList] : frus)
Marri Devender Rao908f7502017-07-10 01:49:54 -0500101 {
Patrick Ventureb0431c72018-10-22 14:57:36 -0700102 auto found = std::find_if(
103 instanceList.begin(), instanceList.end(),
104 [&path](const auto& iter) { return (iter.path == path); });
105
106 if (found != instanceList.end())
Marri Devender Rao908f7502017-07-10 01:49:54 -0500107 {
108 cache::fruMap.erase(fruId);
109 break;
110 }
111 }
112}
113
Patrick Venture0b02be92018-08-31 11:55:55 -0700114// register for fru property change
Marri Devender Rao908f7502017-07-10 01:49:54 -0500115int registerCallbackHandler()
116{
Patrick Venture0b02be92018-08-31 11:55:55 -0700117 if (matchPtr == nullptr)
Marri Devender Rao908f7502017-07-10 01:49:54 -0500118 {
119 using namespace sdbusplus::bus::match::rules;
120 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
121 matchPtr = std::make_unique<sdbusplus::bus::match_t>(
122 bus,
Patrick Venture0b02be92018-08-31 11:55:55 -0700123 path_namespace(OBJ_PATH) + type::signal() +
124 member("PropertiesChanged") + interface(PROP_INTF),
Marri Devender Rao908f7502017-07-10 01:49:54 -0500125 std::bind(processFruPropChange, std::placeholders::_1));
126 }
127 return 0;
128}
129
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500130/**
131 * @brief Read FRU property values from Inventory
132 *
133 * @param[in] fruNum FRU id
134 * @return populate FRU Inventory data
135 */
136FruInventoryData readDataFromInventory(const FRUId& fruNum)
137{
138 auto iter = frus.find(fruNum);
139 if (iter == frus.end())
140 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700141 log<level::ERR>("Unsupported FRU ID ", entry("FRUID=%d", fruNum));
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500142 elog<InternalFailure>();
143 }
144
145 FruInventoryData data;
146 auto& instanceList = iter->second;
147 for (auto& instance : instanceList)
148 {
Ratan Gupta00330972018-01-19 16:23:10 +0530149 for (auto& intf : instance.interfaces)
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500150 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700151 ipmi::PropertyMap allProp =
152 readAllProperties(intf.first, instance.path);
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500153 for (auto& properties : intf.second)
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500154 {
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500155 auto iter = allProp.find(properties.first);
156 if (iter != allProp.end())
157 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700158 data[properties.second.section].emplace(
Patrick Venture45e93cb2019-07-25 15:03:19 -0700159 properties.second.property,
160 std::move(
161 std::get<std::string>(allProp[properties.first])));
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500162 }
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500163 }
164 }
165 }
166 return data;
167}
168
169const FruAreaData& getFruAreaData(const FRUId& fruNum)
170{
171 auto iter = cache::fruMap.find(fruNum);
172 if (iter != cache::fruMap.end())
173 {
174 return iter->second;
175 }
176 auto invData = readDataFromInventory(fruNum);
177
Patrick Venture0b02be92018-08-31 11:55:55 -0700178 // Build area info based on inventory data
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500179 FruAreaData newdata = buildFruAreaData(std::move(invData));
180 cache::fruMap.emplace(fruNum, std::move(newdata));
181 return cache::fruMap.at(fruNum);
182}
Patrick Venture0b02be92018-08-31 11:55:55 -0700183} // namespace fru
184} // namespace ipmi