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::xyz::openbmc_project::Common::Error::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(instanceList.begin(), instanceList.end(), 99 [&path](const auto& iter) { 100 return (iter.path == path); 101 }); 102 103 if (found != instanceList.end()) 104 { 105 cache::fruMap.erase(fruId); 106 break; 107 } 108 } 109 } 110 111 // register for fru property change 112 int registerCallbackHandler() 113 { 114 if (matchPtr == nullptr) 115 { 116 using namespace sdbusplus::bus::match::rules; 117 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()}; 118 matchPtr = std::make_unique<sdbusplus::bus::match_t>( 119 bus, 120 path_namespace(invObjPath) + type::signal() + 121 member("PropertiesChanged") + interface(propInterface), 122 std::bind(processFruPropChange, std::placeholders::_1)); 123 } 124 return 0; 125 } 126 127 /** 128 * @brief Read FRU property values from Inventory 129 * 130 * @param[in] fruNum FRU id 131 * @return populate FRU Inventory data 132 */ 133 FruInventoryData readDataFromInventory(const FRUId& fruNum) 134 { 135 auto iter = frus.find(fruNum); 136 if (iter == frus.end()) 137 { 138 log<level::ERR>("Unsupported FRU ID ", entry("FRUID=%d", fruNum)); 139 elog<InternalFailure>(); 140 } 141 142 FruInventoryData data; 143 auto& instanceList = iter->second; 144 for (auto& instance : instanceList) 145 { 146 for (auto& intf : instance.interfaces) 147 { 148 ipmi::PropertyMap allProp = readAllProperties(intf.first, 149 instance.path); 150 for (auto& properties : intf.second) 151 { 152 auto iter = allProp.find(properties.first); 153 if (iter != allProp.end()) 154 { 155 data[properties.second.section].emplace( 156 properties.second.property, 157 std::move( 158 std::get<std::string>(allProp[properties.first]))); 159 } 160 } 161 } 162 } 163 return data; 164 } 165 166 const FruAreaData& getFruAreaData(const FRUId& fruNum) 167 { 168 auto iter = cache::fruMap.find(fruNum); 169 if (iter != cache::fruMap.end()) 170 { 171 return iter->second; 172 } 173 auto invData = readDataFromInventory(fruNum); 174 175 // Build area info based on inventory data 176 FruAreaData newdata = buildFruAreaData(std::move(invData)); 177 cache::fruMap.emplace(fruNum, std::move(newdata)); 178 return cache::fruMap.at(fruNum); 179 } 180 } // namespace fru 181 } // namespace ipmi 182