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 <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 namespace variant_ns = sdbusplus::message::variant_ns; 21 22 using namespace phosphor::logging; 23 using InternalFailure = 24 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; 25 std::unique_ptr<sdbusplus::bus::match_t> matchPtr(nullptr); 26 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 auto service = ipmi::getService(bus, INV_INTF, OBJ_PATH); 54 std::string objPath = OBJ_PATH + path; 55 auto method = bus.new_method_call(service.c_str(), objPath.c_str(), 56 PROP_INTF, "GetAll"); 57 method.append(intf); 58 auto reply = bus.call(method); 59 if (reply.is_method_error()) 60 { 61 // If property is not found simply return empty value 62 log<level::ERR>("Error in reading property values from inventory", 63 entry("INTERFACE=%s", intf.c_str()), 64 entry("PATH=%s", objPath.c_str())); 65 return properties; 66 } 67 reply.read(properties); 68 return properties; 69 } 70 71 void processFruPropChange(sdbusplus::message::message& msg) 72 { 73 if (cache::fruMap.empty()) 74 { 75 return; 76 } 77 std::string path = msg.get_path(); 78 // trim the object base path, if found at the beginning 79 if (path.compare(0, strlen(OBJ_PATH), OBJ_PATH) == 0) 80 { 81 path.erase(0, strlen(OBJ_PATH)); 82 } 83 for (auto& fru : frus) 84 { 85 bool found = false; 86 auto& fruId = fru.first; 87 auto& instanceList = fru.second; 88 for (auto& instance : instanceList) 89 { 90 if (instance.path == path) 91 { 92 found = true; 93 break; 94 } 95 } 96 if (found) 97 { 98 cache::fruMap.erase(fruId); 99 break; 100 } 101 } 102 } 103 104 // register for fru property change 105 int registerCallbackHandler() 106 { 107 if (matchPtr == nullptr) 108 { 109 using namespace sdbusplus::bus::match::rules; 110 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; 111 matchPtr = std::make_unique<sdbusplus::bus::match_t>( 112 bus, 113 path_namespace(OBJ_PATH) + type::signal() + 114 member("PropertiesChanged") + interface(PROP_INTF), 115 std::bind(processFruPropChange, std::placeholders::_1)); 116 } 117 return 0; 118 } 119 120 /** 121 * @brief Read FRU property values from Inventory 122 * 123 * @param[in] fruNum FRU id 124 * @return populate FRU Inventory data 125 */ 126 FruInventoryData readDataFromInventory(const FRUId& fruNum) 127 { 128 auto iter = frus.find(fruNum); 129 if (iter == frus.end()) 130 { 131 log<level::ERR>("Unsupported FRU ID ", entry("FRUID=%d", fruNum)); 132 elog<InternalFailure>(); 133 } 134 135 FruInventoryData data; 136 auto& instanceList = iter->second; 137 for (auto& instance : instanceList) 138 { 139 for (auto& intf : instance.interfaces) 140 { 141 ipmi::PropertyMap allProp = 142 readAllProperties(intf.first, instance.path); 143 for (auto& properties : intf.second) 144 { 145 auto iter = allProp.find(properties.first); 146 if (iter != allProp.end()) 147 { 148 data[properties.second.section].emplace( 149 properties.first, 150 std::move(variant_ns::get<std::string>( 151 allProp[properties.first]))); 152 } 153 } 154 } 155 } 156 return data; 157 } 158 159 const FruAreaData& getFruAreaData(const FRUId& fruNum) 160 { 161 auto iter = cache::fruMap.find(fruNum); 162 if (iter != cache::fruMap.end()) 163 { 164 return iter->second; 165 } 166 auto invData = readDataFromInventory(fruNum); 167 168 // Build area info based on inventory data 169 FruAreaData newdata = buildFruAreaData(std::move(invData)); 170 cache::fruMap.emplace(fruNum, std::move(newdata)); 171 return cache::fruMap.at(fruNum); 172 } 173 } // namespace fru 174 } // namespace ipmi 175