blob: 67bbf9f0392bd861a8e1675881306123fc1f707c [file] [log] [blame]
Patrick Venture0b02be92018-08-31 11:55:55 -07001#include "read_fru_data.hpp"
2
3#include "fruread.hpp"
4#include "types.hpp"
5#include "utils.hpp"
6
Patrick Venture46470a32018-09-07 19:26:25 -07007#include <host-ipmid/ipmid-api.h>
8
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;
Marri Devender Rao908f7502017-07-10 01:49:54 -050025std::unique_ptr<sdbusplus::bus::match_t> matchPtr(nullptr);
Marri Devender Rao0acf0572017-07-03 12:25:47 -050026
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()};
53 auto service = ipmi::getService(bus, INV_INTF, OBJ_PATH);
54 std::string objPath = OBJ_PATH + path;
Patrick Venture0b02be92018-08-31 11:55:55 -070055 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
56 PROP_INTF, "GetAll");
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050057 method.append(intf);
Marri Devender Rao0acf0572017-07-03 12:25:47 -050058 auto reply = bus.call(method);
59 if (reply.is_method_error())
60 {
Patrick Venture0b02be92018-08-31 11:55:55 -070061 // If property is not found simply return empty value
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050062 log<level::ERR>("Error in reading property values from inventory",
Joseph Reynolds510eb9c2018-05-30 11:51:28 -050063 entry("INTERFACE=%s", intf.c_str()),
64 entry("PATH=%s", objPath.c_str()));
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050065 return properties;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050066 }
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050067 reply.read(properties);
68 return properties;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050069}
70
Marri Devender Rao908f7502017-07-10 01:49:54 -050071void processFruPropChange(sdbusplus::message::message& msg)
72{
Patrick Venture0b02be92018-08-31 11:55:55 -070073 if (cache::fruMap.empty())
Marri Devender Rao908f7502017-07-10 01:49:54 -050074 {
75 return;
76 }
77 std::string path = msg.get_path();
Patrick Venture0b02be92018-08-31 11:55:55 -070078 // trim the object base path, if found at the beginning
Marri Devender Rao908f7502017-07-10 01:49:54 -050079 if (path.compare(0, strlen(OBJ_PATH), OBJ_PATH) == 0)
80 {
81 path.erase(0, strlen(OBJ_PATH));
82 }
83 for (auto& fru : frus)
84 {
85 bool found = false;
86 auto& fruId = fru.first;
87 auto& instanceList = fru.second;
88 for (auto& instance : instanceList)
89 {
Patrick Venture0b02be92018-08-31 11:55:55 -070090 if (instance.path == path)
Marri Devender Rao908f7502017-07-10 01:49:54 -050091 {
92 found = true;
93 break;
94 }
95 }
96 if (found)
97 {
98 cache::fruMap.erase(fruId);
99 break;
100 }
101 }
102}
103
Patrick Venture0b02be92018-08-31 11:55:55 -0700104// register for fru property change
Marri Devender Rao908f7502017-07-10 01:49:54 -0500105int registerCallbackHandler()
106{
Patrick Venture0b02be92018-08-31 11:55:55 -0700107 if (matchPtr == nullptr)
Marri Devender Rao908f7502017-07-10 01:49:54 -0500108 {
109 using namespace sdbusplus::bus::match::rules;
110 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
111 matchPtr = std::make_unique<sdbusplus::bus::match_t>(
112 bus,
Patrick Venture0b02be92018-08-31 11:55:55 -0700113 path_namespace(OBJ_PATH) + type::signal() +
114 member("PropertiesChanged") + interface(PROP_INTF),
Marri Devender Rao908f7502017-07-10 01:49:54 -0500115 std::bind(processFruPropChange, std::placeholders::_1));
116 }
117 return 0;
118}
119
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500120/**
121 * @brief Read FRU property values from Inventory
122 *
123 * @param[in] fruNum FRU id
124 * @return populate FRU Inventory data
125 */
126FruInventoryData readDataFromInventory(const FRUId& fruNum)
127{
128 auto iter = frus.find(fruNum);
129 if (iter == frus.end())
130 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700131 log<level::ERR>("Unsupported FRU ID ", entry("FRUID=%d", fruNum));
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500132 elog<InternalFailure>();
133 }
134
135 FruInventoryData data;
136 auto& instanceList = iter->second;
137 for (auto& instance : instanceList)
138 {
Ratan Gupta00330972018-01-19 16:23:10 +0530139 for (auto& intf : instance.interfaces)
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500140 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700141 ipmi::PropertyMap allProp =
142 readAllProperties(intf.first, instance.path);
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500143 for (auto& properties : intf.second)
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500144 {
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500145 auto iter = allProp.find(properties.first);
146 if (iter != allProp.end())
147 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700148 data[properties.second.section].emplace(
149 properties.first,
William A. Kennington III4c008022018-10-12 17:18:14 -0700150 std::move(variant_ns::get<std::string>(
151 allProp[properties.first])));
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500152 }
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500153 }
154 }
155 }
156 return data;
157}
158
159const FruAreaData& getFruAreaData(const FRUId& fruNum)
160{
161 auto iter = cache::fruMap.find(fruNum);
162 if (iter != cache::fruMap.end())
163 {
164 return iter->second;
165 }
166 auto invData = readDataFromInventory(fruNum);
167
Patrick Venture0b02be92018-08-31 11:55:55 -0700168 // Build area info based on inventory data
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500169 FruAreaData newdata = buildFruAreaData(std::move(invData));
170 cache::fruMap.emplace(fruNum, std::move(newdata));
171 return cache::fruMap.at(fruNum);
172}
Patrick Venture0b02be92018-08-31 11:55:55 -0700173} // namespace fru
174} // namespace ipmi