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