1 #include "read_fru_data.hpp"
2 
3 #include "fruread.hpp"
4 
5 #include <ipmid/api.hpp>
6 #include <ipmid/types.hpp>
7 #include <ipmid/utils.hpp>
8 #include <phosphor-logging/elog-errors.hpp>
9 #include <phosphor-logging/lg2.hpp>
10 #include <sdbusplus/message/types.hpp>
11 #include <xyz/openbmc_project/Common/error.hpp>
12 
13 #include <algorithm>
14 #include <map>
15 
16 extern const FruMap frus;
17 namespace ipmi
18 {
19 namespace fru
20 {
21 
22 using namespace phosphor::logging;
23 using InternalFailure =
24     sdbusplus::error::xyz::openbmc_project::common::InternalFailure;
25 std::unique_ptr<sdbusplus::bus::match_t> matchPtr
26     __attribute__((init_priority(101)));
27 
28 namespace cache
29 {
30 // User initiate read FRU info area command followed by
31 // FRU read command. Also data is read in small chunks of
32 // the specified offset and count.
33 // Caching the data which will be invalidated when ever there
34 // is a change in FRU properties.
35 FRUAreaMap fruMap;
36 } // namespace cache
37 /**
38  * @brief Read all the property value's for the specified interface
39  *  from Inventory.
40  *
41  * @param[in] intf Interface
42  * @param[in] path Object path
43  * @return map of properties
44  */
45 ipmi::PropertyMap readAllProperties(const std::string& intf,
46                                     const std::string& path)
47 {
48     ipmi::PropertyMap properties;
49     sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
50     std::string service;
51     std::string objPath;
52 
53     // Is the path the full dbus path?
54     if (path.find(xyzPrefix) != std::string::npos)
55     {
56         service = ipmi::getService(bus, intf, path);
57         objPath = path;
58     }
59     else
60     {
61         service = ipmi::getService(bus, invMgrInterface, invObjPath);
62         objPath = invObjPath + path;
63     }
64 
65     auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
66                                       propInterface, "GetAll");
67     method.append(intf);
68     try
69     {
70         auto reply = bus.call(method);
71         reply.read(properties);
72     }
73     catch (const sdbusplus::exception_t& e)
74     {
75         // If property is not found simply return empty value
76         lg2::error("Error in reading property values: {ERROR}, path: {PATH}, "
77                    "interface: {INTERFACE}",
78                    "ERROR", e, "PATH", objPath, "INTERFACE", intf);
79     }
80 
81     return properties;
82 }
83 
84 void processFruPropChange(sdbusplus::message_t& msg)
85 {
86     if (cache::fruMap.empty())
87     {
88         return;
89     }
90     std::string path = msg.get_path();
91     // trim the object base path, if found at the beginning
92     if (path.compare(0, strlen(invObjPath), invObjPath) == 0)
93     {
94         path.erase(0, strlen(invObjPath));
95     }
96     for (const auto& [fruId, instanceList] : frus)
97     {
98         auto found = std::find_if(
99             instanceList.begin(), instanceList.end(),
100             [&path](const auto& iter) { return (iter.path == path); });
101 
102         if (found != instanceList.end())
103         {
104             cache::fruMap.erase(fruId);
105             break;
106         }
107     }
108 }
109 
110 // register for fru property change
111 int registerCallbackHandler()
112 {
113     if (matchPtr == nullptr)
114     {
115         using namespace sdbusplus::bus::match::rules;
116         sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
117         matchPtr = std::make_unique<sdbusplus::bus::match_t>(
118             bus,
119             path_namespace(invObjPath) + type::signal() +
120                 member("PropertiesChanged") + interface(propInterface),
121             std::bind(processFruPropChange, std::placeholders::_1));
122     }
123     return 0;
124 }
125 
126 /**
127  * @brief Read FRU property values from Inventory
128  *
129  * @param[in] fruNum  FRU id
130  * @return populate FRU Inventory data
131  */
132 FruInventoryData readDataFromInventory(const FRUId& fruNum)
133 {
134     auto iter = frus.find(fruNum);
135     if (iter == frus.end())
136     {
137         lg2::error("Unsupported FRU ID: {FRUID}", "FRUID", fruNum);
138         elog<InternalFailure>();
139     }
140 
141     FruInventoryData data;
142     auto& instanceList = iter->second;
143     for (auto& instance : instanceList)
144     {
145         for (auto& intf : instance.interfaces)
146         {
147             ipmi::PropertyMap allProp = readAllProperties(intf.first,
148                                                           instance.path);
149             for (auto& properties : intf.second)
150             {
151                 auto iter = allProp.find(properties.first);
152                 if (iter != allProp.end())
153                 {
154                     data[properties.second.section].emplace(
155                         properties.second.property,
156                         std::move(
157                             std::get<std::string>(allProp[properties.first])));
158                 }
159             }
160         }
161     }
162     return data;
163 }
164 
165 const FruAreaData& getFruAreaData(const FRUId& fruNum)
166 {
167     auto iter = cache::fruMap.find(fruNum);
168     if (iter != cache::fruMap.end())
169     {
170         return iter->second;
171     }
172     auto invData = readDataFromInventory(fruNum);
173 
174     // Build area info based on inventory data
175     FruAreaData newdata = buildFruAreaData(std::move(invData));
176     cache::fruMap.emplace(fruNum, std::move(newdata));
177     return cache::fruMap.at(fruNum);
178 }
179 } // namespace fru
180 } // namespace ipmi
181