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