blob: 740a1400887fa80e8b1d0e94787cc3f55ff0ce7e [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
Marri Devender Rao0acf0572017-07-03 12:25:47 -050026namespace cache
27{
Patrick Venture0b02be92018-08-31 11:55:55 -070028// User initiate read FRU info area command followed by
29// FRU read command. Also data is read in small chunks of
30// the specified offset and count.
31// Caching the data which will be invalidated when ever there
32// is a change in FRU properties.
33FRUAreaMap fruMap;
34} // namespace cache
Marri Devender Rao0acf0572017-07-03 12:25:47 -050035/**
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050036 * @brief Read all the property value's for the specified interface
37 * from Inventory.
Marri Devender Rao0acf0572017-07-03 12:25:47 -050038 *
Marri Devender Rao0acf0572017-07-03 12:25:47 -050039 * @param[in] intf Interface
Marri Devender Rao0acf0572017-07-03 12:25:47 -050040 * @param[in] path Object path
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050041 * @return map of properties
Marri Devender Rao0acf0572017-07-03 12:25:47 -050042 */
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050043ipmi::PropertyMap readAllProperties(const std::string& intf,
Patrick Venture0b02be92018-08-31 11:55:55 -070044 const std::string& path)
Marri Devender Rao0acf0572017-07-03 12:25:47 -050045{
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050046 ipmi::PropertyMap properties;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050047 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Patrick Venturec26cc712019-07-25 15:11:59 -070048 std::string service;
49 std::string objPath;
50
51 // Is the path the full dbus path?
Kirill Pakhomovb118c4b2020-05-08 18:38:26 +030052 if (path.find(xyzPrefix) != std::string::npos)
Patrick Venturec26cc712019-07-25 15:11:59 -070053 {
54 service = ipmi::getService(bus, intf, path);
55 objPath = path;
56 }
57 else
58 {
Kirill Pakhomovb118c4b2020-05-08 18:38:26 +030059 service = ipmi::getService(bus, invMgrInterface, invObjPath);
60 objPath = invObjPath + path;
Patrick Venturec26cc712019-07-25 15:11:59 -070061 }
62
Patrick Venture0b02be92018-08-31 11:55:55 -070063 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
Kirill Pakhomovb118c4b2020-05-08 18:38:26 +030064 propInterface, "GetAll");
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050065 method.append(intf);
Patrick Venturec26cc712019-07-25 15:11:59 -070066 try
67 {
68 auto reply = bus.call(method);
69 reply.read(properties);
70 }
71 catch (const sdbusplus::exception::SdBusError& e)
Marri Devender Rao0acf0572017-07-03 12:25:47 -050072 {
Patrick Venture0b02be92018-08-31 11:55:55 -070073 // If property is not found simply return empty value
Patrick Venturec26cc712019-07-25 15:11:59 -070074 log<level::ERR>("Error in reading property values",
75 entry("EXCEPTION=%s", e.what()),
Joseph Reynolds510eb9c2018-05-30 11:51:28 -050076 entry("INTERFACE=%s", intf.c_str()),
77 entry("PATH=%s", objPath.c_str()));
Marri Devender Rao0acf0572017-07-03 12:25:47 -050078 }
Patrick Venturec26cc712019-07-25 15:11:59 -070079
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050080 return properties;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050081}
82
Marri Devender Rao908f7502017-07-10 01:49:54 -050083void processFruPropChange(sdbusplus::message::message& msg)
84{
Patrick Venture0b02be92018-08-31 11:55:55 -070085 if (cache::fruMap.empty())
Marri Devender Rao908f7502017-07-10 01:49:54 -050086 {
87 return;
88 }
89 std::string path = msg.get_path();
Patrick Venture0b02be92018-08-31 11:55:55 -070090 // trim the object base path, if found at the beginning
Kirill Pakhomovb118c4b2020-05-08 18:38:26 +030091 if (path.compare(0, strlen(invObjPath), invObjPath) == 0)
Marri Devender Rao908f7502017-07-10 01:49:54 -050092 {
Kirill Pakhomovb118c4b2020-05-08 18:38:26 +030093 path.erase(0, strlen(invObjPath));
Marri Devender Rao908f7502017-07-10 01:49:54 -050094 }
Patrick Venture438fb792018-10-23 19:51:10 -070095 for (const auto& [fruId, instanceList] : frus)
Marri Devender Rao908f7502017-07-10 01:49:54 -050096 {
Patrick Ventureb0431c72018-10-22 14:57:36 -070097 auto found = std::find_if(
98 instanceList.begin(), instanceList.end(),
99 [&path](const auto& iter) { return (iter.path == path); });
100
101 if (found != instanceList.end())
Marri Devender Rao908f7502017-07-10 01:49:54 -0500102 {
103 cache::fruMap.erase(fruId);
104 break;
105 }
106 }
107}
108
Patrick Venture0b02be92018-08-31 11:55:55 -0700109// register for fru property change
Marri Devender Rao908f7502017-07-10 01:49:54 -0500110int registerCallbackHandler()
111{
Patrick Venture0b02be92018-08-31 11:55:55 -0700112 if (matchPtr == nullptr)
Marri Devender Rao908f7502017-07-10 01:49:54 -0500113 {
114 using namespace sdbusplus::bus::match::rules;
115 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
116 matchPtr = std::make_unique<sdbusplus::bus::match_t>(
117 bus,
Kirill Pakhomovb118c4b2020-05-08 18:38:26 +0300118 path_namespace(invObjPath) + type::signal() +
119 member("PropertiesChanged") + interface(propInterface),
Marri Devender Rao908f7502017-07-10 01:49:54 -0500120 std::bind(processFruPropChange, std::placeholders::_1));
121 }
122 return 0;
123}
124
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500125/**
126 * @brief Read FRU property values from Inventory
127 *
128 * @param[in] fruNum FRU id
129 * @return populate FRU Inventory data
130 */
131FruInventoryData readDataFromInventory(const FRUId& fruNum)
132{
133 auto iter = frus.find(fruNum);
134 if (iter == frus.end())
135 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700136 log<level::ERR>("Unsupported FRU ID ", entry("FRUID=%d", fruNum));
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500137 elog<InternalFailure>();
138 }
139
140 FruInventoryData data;
141 auto& instanceList = iter->second;
142 for (auto& instance : instanceList)
143 {
Ratan Gupta00330972018-01-19 16:23:10 +0530144 for (auto& intf : instance.interfaces)
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500145 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700146 ipmi::PropertyMap allProp =
147 readAllProperties(intf.first, instance.path);
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500148 for (auto& properties : intf.second)
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500149 {
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500150 auto iter = allProp.find(properties.first);
151 if (iter != allProp.end())
152 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700153 data[properties.second.section].emplace(
Patrick Venture45e93cb2019-07-25 15:03:19 -0700154 properties.second.property,
155 std::move(
156 std::get<std::string>(allProp[properties.first])));
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500157 }
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500158 }
159 }
160 }
161 return data;
162}
163
164const FruAreaData& getFruAreaData(const FRUId& fruNum)
165{
166 auto iter = cache::fruMap.find(fruNum);
167 if (iter != cache::fruMap.end())
168 {
169 return iter->second;
170 }
171 auto invData = readDataFromInventory(fruNum);
172
Patrick Venture0b02be92018-08-31 11:55:55 -0700173 // Build area info based on inventory data
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500174 FruAreaData newdata = buildFruAreaData(std::move(invData));
175 cache::fruMap.emplace(fruNum, std::move(newdata));
176 return cache::fruMap.at(fruNum);
177}
Patrick Venture0b02be92018-08-31 11:55:55 -0700178} // namespace fru
179} // namespace ipmi