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