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 <sdbusplus/message/types.hpp> 10 #include <xyz/openbmc_project/Common/error.hpp> 11 12 #include <algorithm> 13 #include <map> 14 15 extern const FruMap frus; 16 namespace ipmi 17 { 18 namespace fru 19 { 20 21 using namespace phosphor::logging; 22 using InternalFailure = 23 sdbusplus::error::xyz::openbmc_project::common::InternalFailure; 24 std::unique_ptr<sdbusplus::bus::match_t> matchPtr 25 __attribute__((init_priority(101))); 26 27 namespace cache 28 { 29 // User initiate read FRU info area command followed by 30 // FRU read command. Also data is read in small chunks of 31 // the specified offset and count. 32 // Caching the data which will be invalidated when ever there 33 // is a change in FRU properties. 34 FRUAreaMap fruMap; 35 } // namespace cache 36 /** 37 * @brief Read all the property value's for the specified interface 38 * from Inventory. 39 * 40 * @param[in] intf Interface 41 * @param[in] path Object path 42 * @return map of properties 43 */ 44 ipmi::PropertyMap readAllProperties(const std::string& intf, 45 const std::string& path) 46 { 47 ipmi::PropertyMap properties; 48 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()}; 49 std::string service; 50 std::string objPath; 51 52 // Is the path the full dbus path? 53 if (path.find(xyzPrefix) != std::string::npos) 54 { 55 service = ipmi::getService(bus, intf, path); 56 objPath = path; 57 } 58 else 59 { 60 service = ipmi::getService(bus, invMgrInterface, invObjPath); 61 objPath = invObjPath + path; 62 } 63 64 auto method = bus.new_method_call(service.c_str(), objPath.c_str(), 65 propInterface, "GetAll"); 66 method.append(intf); 67 try 68 { 69 auto reply = bus.call(method); 70 reply.read(properties); 71 } 72 catch (const sdbusplus::exception_t& e) 73 { 74 // If property is not found simply return empty value 75 log<level::ERR>("Error in reading property values", 76 entry("EXCEPTION=%s", e.what()), 77 entry("INTERFACE=%s", intf.c_str()), 78 entry("PATH=%s", objPath.c_str())); 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 log<level::ERR>("Unsupported FRU ID ", entry("FRUID=%d", 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