blob: 25be92258c2c69dc92f0f4b47dcf1c17577cfd91 [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
Vernon Mauerye08fbff2019-04-03 09:19:34 -07005#include <ipmid/api.hpp>
Vernon Mauery33250242019-03-12 16:49:26 -07006#include <ipmid/types.hpp>
Vernon Mauery6a98fe72019-03-11 15:57:48 -07007#include <ipmid/utils.hpp>
Marri Devender Rao0acf0572017-07-03 12:25:47 -05008#include <phosphor-logging/elog-errors.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -07009#include <sdbusplus/message/types.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070010#include <xyz/openbmc_project/Common/error.hpp>
11
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050012#include <algorithm>
13#include <map>
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
Marri Devender Rao0acf0572017-07-03 12:25:47 -050021using namespace phosphor::logging;
22using InternalFailure =
Patrick Venture0b02be92018-08-31 11:55:55 -070023 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Lei YU4b0ddb62019-01-25 16:43:50 +080024std::unique_ptr<sdbusplus::bus::match_t> matchPtr
25 __attribute__((init_priority(101)));
Marri Devender Rao0acf0572017-07-03 12:25:47 -050026
Marri Devender Rao0acf0572017-07-03 12:25:47 -050027namespace cache
28{
Patrick Venture0b02be92018-08-31 11:55:55 -070029// User initiate read FRU info area command followed by
30// FRU read command. Also data is read in small chunks of
31// the specified offset and count.
32// Caching the data which will be invalidated when ever there
33// is a change in FRU properties.
34FRUAreaMap fruMap;
35} // namespace cache
Marri Devender Rao0acf0572017-07-03 12:25:47 -050036/**
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050037 * @brief Read all the property value's for the specified interface
38 * from Inventory.
Marri Devender Rao0acf0572017-07-03 12:25:47 -050039 *
Marri Devender Rao0acf0572017-07-03 12:25:47 -050040 * @param[in] intf Interface
Marri Devender Rao0acf0572017-07-03 12:25:47 -050041 * @param[in] path Object path
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050042 * @return map of properties
Marri Devender Rao0acf0572017-07-03 12:25:47 -050043 */
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050044ipmi::PropertyMap readAllProperties(const std::string& intf,
Patrick Venture0b02be92018-08-31 11:55:55 -070045 const std::string& path)
Marri Devender Rao0acf0572017-07-03 12:25:47 -050046{
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050047 ipmi::PropertyMap properties;
Patrick Williams5d82f472022-07-22 19:26:53 -050048 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Patrick Venturec26cc712019-07-25 15:11:59 -070049 std::string service;
50 std::string objPath;
51
52 // Is the path the full dbus path?
Kirill Pakhomovb118c4b2020-05-08 18:38:26 +030053 if (path.find(xyzPrefix) != std::string::npos)
Patrick Venturec26cc712019-07-25 15:11:59 -070054 {
55 service = ipmi::getService(bus, intf, path);
56 objPath = path;
57 }
58 else
59 {
Kirill Pakhomovb118c4b2020-05-08 18:38:26 +030060 service = ipmi::getService(bus, invMgrInterface, invObjPath);
61 objPath = invObjPath + path;
Patrick Venturec26cc712019-07-25 15:11:59 -070062 }
63
Patrick Venture0b02be92018-08-31 11:55:55 -070064 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
Kirill Pakhomovb118c4b2020-05-08 18:38:26 +030065 propInterface, "GetAll");
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050066 method.append(intf);
Patrick Venturec26cc712019-07-25 15:11:59 -070067 try
68 {
69 auto reply = bus.call(method);
70 reply.read(properties);
71 }
Patrick Williams5d82f472022-07-22 19:26:53 -050072 catch (const sdbusplus::exception_t& e)
Marri Devender Rao0acf0572017-07-03 12:25:47 -050073 {
Patrick Venture0b02be92018-08-31 11:55:55 -070074 // If property is not found simply return empty value
Patrick Venturec26cc712019-07-25 15:11:59 -070075 log<level::ERR>("Error in reading property values",
76 entry("EXCEPTION=%s", e.what()),
Joseph Reynolds510eb9c2018-05-30 11:51:28 -050077 entry("INTERFACE=%s", intf.c_str()),
78 entry("PATH=%s", objPath.c_str()));
Marri Devender Rao0acf0572017-07-03 12:25:47 -050079 }
Patrick Venturec26cc712019-07-25 15:11:59 -070080
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050081 return properties;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050082}
83
Patrick Williams5d82f472022-07-22 19:26:53 -050084void processFruPropChange(sdbusplus::message_t& msg)
Marri Devender Rao908f7502017-07-10 01:49:54 -050085{
Patrick Venture0b02be92018-08-31 11:55:55 -070086 if (cache::fruMap.empty())
Marri Devender Rao908f7502017-07-10 01:49:54 -050087 {
88 return;
89 }
90 std::string path = msg.get_path();
Patrick Venture0b02be92018-08-31 11:55:55 -070091 // trim the object base path, if found at the beginning
Kirill Pakhomovb118c4b2020-05-08 18:38:26 +030092 if (path.compare(0, strlen(invObjPath), invObjPath) == 0)
Marri Devender Rao908f7502017-07-10 01:49:54 -050093 {
Kirill Pakhomovb118c4b2020-05-08 18:38:26 +030094 path.erase(0, strlen(invObjPath));
Marri Devender Rao908f7502017-07-10 01:49:54 -050095 }
Patrick Venture438fb792018-10-23 19:51:10 -070096 for (const auto& [fruId, instanceList] : frus)
Marri Devender Rao908f7502017-07-10 01:49:54 -050097 {
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050098 auto found = std::find_if(instanceList.begin(), instanceList.end(),
99 [&path](const auto& iter) {
100 return (iter.path == path);
101 });
Patrick Ventureb0431c72018-10-22 14:57:36 -0700102
103 if (found != instanceList.end())
Marri Devender Rao908f7502017-07-10 01:49:54 -0500104 {
105 cache::fruMap.erase(fruId);
106 break;
107 }
108 }
109}
110
Patrick Venture0b02be92018-08-31 11:55:55 -0700111// register for fru property change
Marri Devender Rao908f7502017-07-10 01:49:54 -0500112int registerCallbackHandler()
113{
Patrick Venture0b02be92018-08-31 11:55:55 -0700114 if (matchPtr == nullptr)
Marri Devender Rao908f7502017-07-10 01:49:54 -0500115 {
116 using namespace sdbusplus::bus::match::rules;
Patrick Williams5d82f472022-07-22 19:26:53 -0500117 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Marri Devender Rao908f7502017-07-10 01:49:54 -0500118 matchPtr = std::make_unique<sdbusplus::bus::match_t>(
119 bus,
Kirill Pakhomovb118c4b2020-05-08 18:38:26 +0300120 path_namespace(invObjPath) + type::signal() +
121 member("PropertiesChanged") + interface(propInterface),
Marri Devender Rao908f7502017-07-10 01:49:54 -0500122 std::bind(processFruPropChange, std::placeholders::_1));
123 }
124 return 0;
125}
126
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500127/**
128 * @brief Read FRU property values from Inventory
129 *
130 * @param[in] fruNum FRU id
131 * @return populate FRU Inventory data
132 */
133FruInventoryData readDataFromInventory(const FRUId& fruNum)
134{
135 auto iter = frus.find(fruNum);
136 if (iter == frus.end())
137 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700138 log<level::ERR>("Unsupported FRU ID ", entry("FRUID=%d", fruNum));
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500139 elog<InternalFailure>();
140 }
141
142 FruInventoryData data;
143 auto& instanceList = iter->second;
144 for (auto& instance : instanceList)
145 {
Ratan Gupta00330972018-01-19 16:23:10 +0530146 for (auto& intf : instance.interfaces)
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500147 {
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500148 ipmi::PropertyMap allProp = readAllProperties(intf.first,
149 instance.path);
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500150 for (auto& properties : intf.second)
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500151 {
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500152 auto iter = allProp.find(properties.first);
153 if (iter != allProp.end())
154 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700155 data[properties.second.section].emplace(
Patrick Venture45e93cb2019-07-25 15:03:19 -0700156 properties.second.property,
157 std::move(
158 std::get<std::string>(allProp[properties.first])));
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500159 }
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500160 }
161 }
162 }
163 return data;
164}
165
166const FruAreaData& getFruAreaData(const FRUId& fruNum)
167{
168 auto iter = cache::fruMap.find(fruNum);
169 if (iter != cache::fruMap.end())
170 {
171 return iter->second;
172 }
173 auto invData = readDataFromInventory(fruNum);
174
Patrick Venture0b02be92018-08-31 11:55:55 -0700175 // Build area info based on inventory data
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500176 FruAreaData newdata = buildFruAreaData(std::move(invData));
177 cache::fruMap.emplace(fruNum, std::move(newdata));
178 return cache::fruMap.at(fruNum);
179}
Patrick Venture0b02be92018-08-31 11:55:55 -0700180} // namespace fru
181} // namespace ipmi