blob: 17bcfe4002892e04d453c7fdee6a3ed02f95fd4a [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
William A. Kennington III194375f2018-12-14 02:14:33 -08005#include <ipmid/api.h>
Patrick Venture46470a32018-09-07 19:26:25 -07006
Patrick Ventureb0431c72018-10-22 14:57:36 -07007#include <algorithm>
Vernon Mauery33250242019-03-12 16:49:26 -07008#include <ipmid/types.hpp>
Vernon Mauery6a98fe72019-03-11 15:57:48 -07009#include <ipmid/utils.hpp>
Marri Devender Rao0acf0572017-07-03 12:25:47 -050010#include <map>
11#include <phosphor-logging/elog-errors.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -070012#include <sdbusplus/message/types.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070013#include <xyz/openbmc_project/Common/error.hpp>
14
Marri Devender Rao0acf0572017-07-03 12:25:47 -050015extern const FruMap frus;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050016namespace ipmi
17{
18namespace fru
19{
William A. Kennington III4c008022018-10-12 17:18:14 -070020
21namespace variant_ns = sdbusplus::message::variant_ns;
22
Marri Devender Rao0acf0572017-07-03 12:25:47 -050023using namespace phosphor::logging;
24using InternalFailure =
Patrick Venture0b02be92018-08-31 11:55:55 -070025 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Lei YU4b0ddb62019-01-25 16:43:50 +080026std::unique_ptr<sdbusplus::bus::match_t> matchPtr
27 __attribute__((init_priority(101)));
Marri Devender Rao0acf0572017-07-03 12:25:47 -050028
Patrick Venture0b02be92018-08-31 11:55:55 -070029static constexpr auto INV_INTF = "xyz.openbmc_project.Inventory.Manager";
30static constexpr auto OBJ_PATH = "/xyz/openbmc_project/inventory";
Marri Devender Rao0acf0572017-07-03 12:25:47 -050031static constexpr auto PROP_INTF = "org.freedesktop.DBus.Properties";
32
33namespace cache
34{
Patrick Venture0b02be92018-08-31 11:55:55 -070035// User initiate read FRU info area command followed by
36// FRU read command. Also data is read in small chunks of
37// the specified offset and count.
38// Caching the data which will be invalidated when ever there
39// is a change in FRU properties.
40FRUAreaMap fruMap;
41} // namespace cache
Marri Devender Rao0acf0572017-07-03 12:25:47 -050042/**
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050043 * @brief Read all the property value's for the specified interface
44 * from Inventory.
Marri Devender Rao0acf0572017-07-03 12:25:47 -050045 *
Marri Devender Rao0acf0572017-07-03 12:25:47 -050046 * @param[in] intf Interface
Marri Devender Rao0acf0572017-07-03 12:25:47 -050047 * @param[in] path Object path
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050048 * @return map of properties
Marri Devender Rao0acf0572017-07-03 12:25:47 -050049 */
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050050ipmi::PropertyMap readAllProperties(const std::string& intf,
Patrick Venture0b02be92018-08-31 11:55:55 -070051 const std::string& path)
Marri Devender Rao0acf0572017-07-03 12:25:47 -050052{
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050053 ipmi::PropertyMap properties;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050054 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
55 auto service = ipmi::getService(bus, INV_INTF, OBJ_PATH);
56 std::string objPath = OBJ_PATH + path;
Patrick Venture0b02be92018-08-31 11:55:55 -070057 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
58 PROP_INTF, "GetAll");
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050059 method.append(intf);
Marri Devender Rao0acf0572017-07-03 12:25:47 -050060 auto reply = bus.call(method);
61 if (reply.is_method_error())
62 {
Patrick Venture0b02be92018-08-31 11:55:55 -070063 // If property is not found simply return empty value
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050064 log<level::ERR>("Error in reading property values from inventory",
Joseph Reynolds510eb9c2018-05-30 11:51:28 -050065 entry("INTERFACE=%s", intf.c_str()),
66 entry("PATH=%s", objPath.c_str()));
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050067 return properties;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050068 }
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050069 reply.read(properties);
70 return properties;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050071}
72
Marri Devender Rao908f7502017-07-10 01:49:54 -050073void processFruPropChange(sdbusplus::message::message& msg)
74{
Patrick Venture0b02be92018-08-31 11:55:55 -070075 if (cache::fruMap.empty())
Marri Devender Rao908f7502017-07-10 01:49:54 -050076 {
77 return;
78 }
79 std::string path = msg.get_path();
Patrick Venture0b02be92018-08-31 11:55:55 -070080 // trim the object base path, if found at the beginning
Marri Devender Rao908f7502017-07-10 01:49:54 -050081 if (path.compare(0, strlen(OBJ_PATH), OBJ_PATH) == 0)
82 {
83 path.erase(0, strlen(OBJ_PATH));
84 }
Patrick Venture438fb792018-10-23 19:51:10 -070085 for (const auto& [fruId, instanceList] : frus)
Marri Devender Rao908f7502017-07-10 01:49:54 -050086 {
Patrick Ventureb0431c72018-10-22 14:57:36 -070087 auto found = std::find_if(
88 instanceList.begin(), instanceList.end(),
89 [&path](const auto& iter) { return (iter.path == path); });
90
91 if (found != instanceList.end())
Marri Devender Rao908f7502017-07-10 01:49:54 -050092 {
93 cache::fruMap.erase(fruId);
94 break;
95 }
96 }
97}
98
Patrick Venture0b02be92018-08-31 11:55:55 -070099// register for fru property change
Marri Devender Rao908f7502017-07-10 01:49:54 -0500100int registerCallbackHandler()
101{
Patrick Venture0b02be92018-08-31 11:55:55 -0700102 if (matchPtr == nullptr)
Marri Devender Rao908f7502017-07-10 01:49:54 -0500103 {
104 using namespace sdbusplus::bus::match::rules;
105 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
106 matchPtr = std::make_unique<sdbusplus::bus::match_t>(
107 bus,
Patrick Venture0b02be92018-08-31 11:55:55 -0700108 path_namespace(OBJ_PATH) + type::signal() +
109 member("PropertiesChanged") + interface(PROP_INTF),
Marri Devender Rao908f7502017-07-10 01:49:54 -0500110 std::bind(processFruPropChange, std::placeholders::_1));
111 }
112 return 0;
113}
114
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500115/**
116 * @brief Read FRU property values from Inventory
117 *
118 * @param[in] fruNum FRU id
119 * @return populate FRU Inventory data
120 */
121FruInventoryData readDataFromInventory(const FRUId& fruNum)
122{
123 auto iter = frus.find(fruNum);
124 if (iter == frus.end())
125 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700126 log<level::ERR>("Unsupported FRU ID ", entry("FRUID=%d", fruNum));
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500127 elog<InternalFailure>();
128 }
129
130 FruInventoryData data;
131 auto& instanceList = iter->second;
132 for (auto& instance : instanceList)
133 {
Ratan Gupta00330972018-01-19 16:23:10 +0530134 for (auto& intf : instance.interfaces)
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500135 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700136 ipmi::PropertyMap allProp =
137 readAllProperties(intf.first, instance.path);
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500138 for (auto& properties : intf.second)
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500139 {
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500140 auto iter = allProp.find(properties.first);
141 if (iter != allProp.end())
142 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700143 data[properties.second.section].emplace(
144 properties.first,
William A. Kennington III4c008022018-10-12 17:18:14 -0700145 std::move(variant_ns::get<std::string>(
146 allProp[properties.first])));
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500147 }
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500148 }
149 }
150 }
151 return data;
152}
153
154const FruAreaData& getFruAreaData(const FRUId& fruNum)
155{
156 auto iter = cache::fruMap.find(fruNum);
157 if (iter != cache::fruMap.end())
158 {
159 return iter->second;
160 }
161 auto invData = readDataFromInventory(fruNum);
162
Patrick Venture0b02be92018-08-31 11:55:55 -0700163 // Build area info based on inventory data
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500164 FruAreaData newdata = buildFruAreaData(std::move(invData));
165 cache::fruMap.emplace(fruNum, std::move(newdata));
166 return cache::fruMap.at(fruNum);
167}
Patrick Venture0b02be92018-08-31 11:55:55 -0700168} // namespace fru
169} // namespace ipmi