1 #include "read_fru_data.hpp"
2 
3 #include "fruread.hpp"
4 #include "types.hpp"
5 
6 #include <ipmid/api.h>
7 
8 #include <algorithm>
9 #include <ipmid/utils.hpp>
10 #include <map>
11 #include <phosphor-logging/elog-errors.hpp>
12 #include <sdbusplus/message/types.hpp>
13 #include <xyz/openbmc_project/Common/error.hpp>
14 
15 extern const FruMap frus;
16 namespace ipmi
17 {
18 namespace fru
19 {
20 
21 namespace variant_ns = sdbusplus::message::variant_ns;
22 
23 using namespace phosphor::logging;
24 using InternalFailure =
25     sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
26 std::unique_ptr<sdbusplus::bus::match_t> matchPtr
27     __attribute__((init_priority(101)));
28 
29 static constexpr auto INV_INTF = "xyz.openbmc_project.Inventory.Manager";
30 static constexpr auto OBJ_PATH = "/xyz/openbmc_project/inventory";
31 static constexpr auto PROP_INTF = "org.freedesktop.DBus.Properties";
32 
33 namespace cache
34 {
35 // 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.
40 FRUAreaMap fruMap;
41 } // namespace cache
42 /**
43  * @brief Read all the property value's for the specified interface
44  *  from Inventory.
45  *
46  * @param[in] intf Interface
47  * @param[in] path Object path
48  * @return map of properties
49  */
50 ipmi::PropertyMap readAllProperties(const std::string& intf,
51                                     const std::string& path)
52 {
53     ipmi::PropertyMap properties;
54     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;
57     auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
58                                       PROP_INTF, "GetAll");
59     method.append(intf);
60     auto reply = bus.call(method);
61     if (reply.is_method_error())
62     {
63         // If property is not found simply return empty value
64         log<level::ERR>("Error in reading property values from inventory",
65                         entry("INTERFACE=%s", intf.c_str()),
66                         entry("PATH=%s", objPath.c_str()));
67         return properties;
68     }
69     reply.read(properties);
70     return properties;
71 }
72 
73 void processFruPropChange(sdbusplus::message::message& msg)
74 {
75     if (cache::fruMap.empty())
76     {
77         return;
78     }
79     std::string path = msg.get_path();
80     // trim the object base path, if found at the beginning
81     if (path.compare(0, strlen(OBJ_PATH), OBJ_PATH) == 0)
82     {
83         path.erase(0, strlen(OBJ_PATH));
84     }
85     for (const auto& [fruId, instanceList] : frus)
86     {
87         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())
92         {
93             cache::fruMap.erase(fruId);
94             break;
95         }
96     }
97 }
98 
99 // register for fru property change
100 int 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) + type::signal() +
109                 member("PropertiesChanged") + interface(PROP_INTF),
110             std::bind(processFruPropChange, std::placeholders::_1));
111     }
112     return 0;
113 }
114 
115 /**
116  * @brief Read FRU property values from Inventory
117  *
118  * @param[in] fruNum  FRU id
119  * @return populate FRU Inventory data
120  */
121 FruInventoryData readDataFromInventory(const FRUId& fruNum)
122 {
123     auto iter = frus.find(fruNum);
124     if (iter == frus.end())
125     {
126         log<level::ERR>("Unsupported FRU ID ", entry("FRUID=%d", fruNum));
127         elog<InternalFailure>();
128     }
129 
130     FruInventoryData data;
131     auto& instanceList = iter->second;
132     for (auto& instance : instanceList)
133     {
134         for (auto& intf : instance.interfaces)
135         {
136             ipmi::PropertyMap allProp =
137                 readAllProperties(intf.first, instance.path);
138             for (auto& properties : intf.second)
139             {
140                 auto iter = allProp.find(properties.first);
141                 if (iter != allProp.end())
142                 {
143                     data[properties.second.section].emplace(
144                         properties.first,
145                         std::move(variant_ns::get<std::string>(
146                             allProp[properties.first])));
147                 }
148             }
149         }
150     }
151     return data;
152 }
153 
154 const 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 } // namespace fru
169 } // namespace ipmi
170