blob: 1f99e2038f513b5db1a43e4470c3f32de73b3b0a [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
20namespace variant_ns = sdbusplus::message::variant_ns;
21
Marri Devender Rao0acf0572017-07-03 12:25:47 -050022using namespace phosphor::logging;
23using InternalFailure =
Patrick Venture0b02be92018-08-31 11:55:55 -070024 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Lei YU4b0ddb62019-01-25 16:43:50 +080025std::unique_ptr<sdbusplus::bus::match_t> matchPtr
26 __attribute__((init_priority(101)));
Marri Devender Rao0acf0572017-07-03 12:25:47 -050027
Patrick Venture0b02be92018-08-31 11:55:55 -070028static constexpr auto INV_INTF = "xyz.openbmc_project.Inventory.Manager";
29static constexpr auto OBJ_PATH = "/xyz/openbmc_project/inventory";
Marri Devender Rao0acf0572017-07-03 12:25:47 -050030static constexpr auto PROP_INTF = "org.freedesktop.DBus.Properties";
31
32namespace cache
33{
Patrick Venture0b02be92018-08-31 11:55:55 -070034// User initiate read FRU info area command followed by
35// FRU read command. Also data is read in small chunks of
36// the specified offset and count.
37// Caching the data which will be invalidated when ever there
38// is a change in FRU properties.
39FRUAreaMap fruMap;
40} // namespace cache
Marri Devender Rao0acf0572017-07-03 12:25:47 -050041/**
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050042 * @brief Read all the property value's for the specified interface
43 * from Inventory.
Marri Devender Rao0acf0572017-07-03 12:25:47 -050044 *
Marri Devender Rao0acf0572017-07-03 12:25:47 -050045 * @param[in] intf Interface
Marri Devender Rao0acf0572017-07-03 12:25:47 -050046 * @param[in] path Object path
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050047 * @return map of properties
Marri Devender Rao0acf0572017-07-03 12:25:47 -050048 */
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050049ipmi::PropertyMap readAllProperties(const std::string& intf,
Patrick Venture0b02be92018-08-31 11:55:55 -070050 const std::string& path)
Marri Devender Rao0acf0572017-07-03 12:25:47 -050051{
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050052 ipmi::PropertyMap properties;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050053 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
54 auto service = ipmi::getService(bus, INV_INTF, OBJ_PATH);
55 std::string objPath = OBJ_PATH + path;
Patrick Venture0b02be92018-08-31 11:55:55 -070056 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
57 PROP_INTF, "GetAll");
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050058 method.append(intf);
Marri Devender Rao0acf0572017-07-03 12:25:47 -050059 auto reply = bus.call(method);
60 if (reply.is_method_error())
61 {
Patrick Venture0b02be92018-08-31 11:55:55 -070062 // If property is not found simply return empty value
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050063 log<level::ERR>("Error in reading property values from inventory",
Joseph Reynolds510eb9c2018-05-30 11:51:28 -050064 entry("INTERFACE=%s", intf.c_str()),
65 entry("PATH=%s", objPath.c_str()));
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050066 return properties;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050067 }
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050068 reply.read(properties);
69 return properties;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050070}
71
Marri Devender Rao908f7502017-07-10 01:49:54 -050072void processFruPropChange(sdbusplus::message::message& msg)
73{
Patrick Venture0b02be92018-08-31 11:55:55 -070074 if (cache::fruMap.empty())
Marri Devender Rao908f7502017-07-10 01:49:54 -050075 {
76 return;
77 }
78 std::string path = msg.get_path();
Patrick Venture0b02be92018-08-31 11:55:55 -070079 // trim the object base path, if found at the beginning
Marri Devender Rao908f7502017-07-10 01:49:54 -050080 if (path.compare(0, strlen(OBJ_PATH), OBJ_PATH) == 0)
81 {
82 path.erase(0, strlen(OBJ_PATH));
83 }
Patrick Venture438fb792018-10-23 19:51:10 -070084 for (const auto& [fruId, instanceList] : frus)
Marri Devender Rao908f7502017-07-10 01:49:54 -050085 {
Patrick Ventureb0431c72018-10-22 14:57:36 -070086 auto found = std::find_if(
87 instanceList.begin(), instanceList.end(),
88 [&path](const auto& iter) { return (iter.path == path); });
89
90 if (found != instanceList.end())
Marri Devender Rao908f7502017-07-10 01:49:54 -050091 {
92 cache::fruMap.erase(fruId);
93 break;
94 }
95 }
96}
97
Patrick Venture0b02be92018-08-31 11:55:55 -070098// register for fru property change
Marri Devender Rao908f7502017-07-10 01:49:54 -050099int registerCallbackHandler()
100{
Patrick Venture0b02be92018-08-31 11:55:55 -0700101 if (matchPtr == nullptr)
Marri Devender Rao908f7502017-07-10 01:49:54 -0500102 {
103 using namespace sdbusplus::bus::match::rules;
104 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
105 matchPtr = std::make_unique<sdbusplus::bus::match_t>(
106 bus,
Patrick Venture0b02be92018-08-31 11:55:55 -0700107 path_namespace(OBJ_PATH) + type::signal() +
108 member("PropertiesChanged") + interface(PROP_INTF),
Marri Devender Rao908f7502017-07-10 01:49:54 -0500109 std::bind(processFruPropChange, std::placeholders::_1));
110 }
111 return 0;
112}
113
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500114/**
115 * @brief Read FRU property values from Inventory
116 *
117 * @param[in] fruNum FRU id
118 * @return populate FRU Inventory data
119 */
120FruInventoryData readDataFromInventory(const FRUId& fruNum)
121{
122 auto iter = frus.find(fruNum);
123 if (iter == frus.end())
124 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700125 log<level::ERR>("Unsupported FRU ID ", entry("FRUID=%d", fruNum));
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500126 elog<InternalFailure>();
127 }
128
129 FruInventoryData data;
130 auto& instanceList = iter->second;
131 for (auto& instance : instanceList)
132 {
Ratan Gupta00330972018-01-19 16:23:10 +0530133 for (auto& intf : instance.interfaces)
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500134 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700135 ipmi::PropertyMap allProp =
136 readAllProperties(intf.first, instance.path);
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500137 for (auto& properties : intf.second)
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500138 {
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500139 auto iter = allProp.find(properties.first);
140 if (iter != allProp.end())
141 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700142 data[properties.second.section].emplace(
143 properties.first,
William A. Kennington III4c008022018-10-12 17:18:14 -0700144 std::move(variant_ns::get<std::string>(
145 allProp[properties.first])));
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500146 }
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500147 }
148 }
149 }
150 return data;
151}
152
153const FruAreaData& getFruAreaData(const FRUId& fruNum)
154{
155 auto iter = cache::fruMap.find(fruNum);
156 if (iter != cache::fruMap.end())
157 {
158 return iter->second;
159 }
160 auto invData = readDataFromInventory(fruNum);
161
Patrick Venture0b02be92018-08-31 11:55:55 -0700162 // Build area info based on inventory data
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500163 FruAreaData newdata = buildFruAreaData(std::move(invData));
164 cache::fruMap.emplace(fruNum, std::move(newdata));
165 return cache::fruMap.at(fruNum);
166}
Patrick Venture0b02be92018-08-31 11:55:55 -0700167} // namespace fru
168} // namespace ipmi