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