blob: 510f0afbbbc29403349b7bcac364ae557bcf213d [file] [log] [blame]
Marri Devender Rao0acf0572017-07-03 12:25:47 -05001#include <map>
2#include <phosphor-logging/elog-errors.hpp>
3#include "xyz/openbmc_project/Common/error.hpp"
4#include "read_fru_data.hpp"
5#include "fruread.hpp"
6#include "host-ipmid/ipmid-api.h"
7#include "utils.hpp"
Marri Devender Rao18aae1f2017-07-26 00:33:26 -05008#include "types.hpp"
Marri Devender Rao0acf0572017-07-03 12:25:47 -05009
10extern const FruMap frus;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050011namespace ipmi
12{
13namespace fru
14{
15using namespace phosphor::logging;
16using InternalFailure =
17 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Marri Devender Rao908f7502017-07-10 01:49:54 -050018std::unique_ptr<sdbusplus::bus::match_t> matchPtr(nullptr);
Marri Devender Rao0acf0572017-07-03 12:25:47 -050019
20static constexpr auto INV_INTF = "xyz.openbmc_project.Inventory.Manager";
21static constexpr auto OBJ_PATH = "/xyz/openbmc_project/inventory";
22static constexpr auto PROP_INTF = "org.freedesktop.DBus.Properties";
23
24namespace cache
25{
26 //User initiate read FRU info area command followed by
27 //FRU read command. Also data is read in small chunks of
28 //the specified offset and count.
29 //Caching the data which will be invalidated when ever there
30 //is a change in FRU properties.
31 FRUAreaMap fruMap;
32}
33/**
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050034 * @brief Read all the property value's for the specified interface
35 * from Inventory.
Marri Devender Rao0acf0572017-07-03 12:25:47 -050036 *
Marri Devender Rao0acf0572017-07-03 12:25:47 -050037 * @param[in] intf Interface
Marri Devender Rao0acf0572017-07-03 12:25:47 -050038 * @param[in] path Object path
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050039 * @return map of properties
Marri Devender Rao0acf0572017-07-03 12:25:47 -050040 */
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050041ipmi::PropertyMap readAllProperties(const std::string& intf,
42 const std::string& path)
Marri Devender Rao0acf0572017-07-03 12:25:47 -050043{
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050044 ipmi::PropertyMap properties;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050045 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
46 auto service = ipmi::getService(bus, INV_INTF, OBJ_PATH);
47 std::string objPath = OBJ_PATH + path;
48 auto method = bus.new_method_call(service.c_str(),
49 objPath.c_str(),
50 PROP_INTF,
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050051 "GetAll");
52 method.append(intf);
Marri Devender Rao0acf0572017-07-03 12:25:47 -050053 auto reply = bus.call(method);
54 if (reply.is_method_error())
55 {
56 //If property is not found simply return empty value
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050057 log<level::ERR>("Error in reading property values from inventory",
Gunnar Millse4050f22017-10-19 16:36:45 -050058 entry("INTERFACE=%s", intf),
59 entry("PATH=%s", objPath));
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050060 return properties;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050061 }
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050062 reply.read(properties);
63 return properties;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050064}
65
Marri Devender Rao908f7502017-07-10 01:49:54 -050066void processFruPropChange(sdbusplus::message::message& msg)
67{
68 if(cache::fruMap.empty())
69 {
70 return;
71 }
72 std::string path = msg.get_path();
73 //trim the object base path, if found at the beginning
74 if (path.compare(0, strlen(OBJ_PATH), OBJ_PATH) == 0)
75 {
76 path.erase(0, strlen(OBJ_PATH));
77 }
78 for (auto& fru : frus)
79 {
80 bool found = false;
81 auto& fruId = fru.first;
82 auto& instanceList = fru.second;
83 for (auto& instance : instanceList)
84 {
85 if(instance.first == path)
86 {
87 found = true;
88 break;
89 }
90 }
91 if (found)
92 {
93 cache::fruMap.erase(fruId);
94 break;
95 }
96 }
97}
98
99//register for fru property change
100int registerCallbackHandler()
101{
102 if(matchPtr == nullptr)
103 {
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,
108 path_namespace(OBJ_PATH) +
109 type::signal() +
110 member("PropertiesChanged") +
111 interface(PROP_INTF),
112 std::bind(processFruPropChange, std::placeholders::_1));
113 }
114 return 0;
115}
116
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500117/**
118 * @brief Read FRU property values from Inventory
119 *
120 * @param[in] fruNum FRU id
121 * @return populate FRU Inventory data
122 */
123FruInventoryData readDataFromInventory(const FRUId& fruNum)
124{
125 auto iter = frus.find(fruNum);
126 if (iter == frus.end())
127 {
128 log<level::ERR>("Unsupported FRU ID ",entry("FRUID=%d", fruNum));
129 elog<InternalFailure>();
130 }
131
132 FruInventoryData data;
133 auto& instanceList = iter->second;
134 for (auto& instance : instanceList)
135 {
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500136 for (auto& intf : instance.second)
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500137 {
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500138 ipmi::PropertyMap allProp = readAllProperties(
139 intf.first, instance.first);
140 for (auto& properties : intf.second)
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500141 {
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500142 auto iter = allProp.find(properties.first);
143 if (iter != allProp.end())
144 {
145 data[properties.second.section].emplace(properties.first,
146 std::move(allProp[properties.first].get<std::string>()));
147 }
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
163 //Build area info based on inventory data
164 FruAreaData newdata = buildFruAreaData(std::move(invData));
165 cache::fruMap.emplace(fruNum, std::move(newdata));
166 return cache::fruMap.at(fruNum);
167}
168} //fru
169} //ipmi