Alexander Hansen | cc37235 | 2025-01-14 14:15:39 +0100 | [diff] [blame] | 1 | #include "software_manager.hpp" |
| 2 | |
Alexander Hansen | cec1475 | 2025-05-08 13:11:03 +0200 | [diff] [blame] | 3 | #include <boost/container/flat_map.hpp> |
Alexander Hansen | cc37235 | 2025-01-14 14:15:39 +0100 | [diff] [blame] | 4 | #include <phosphor-logging/lg2.hpp> |
| 5 | #include <sdbusplus/asio/object_server.hpp> |
| 6 | #include <sdbusplus/async.hpp> |
| 7 | #include <sdbusplus/async/context.hpp> |
| 8 | #include <sdbusplus/bus.hpp> |
Alexander Hansen | cec1475 | 2025-05-08 13:11:03 +0200 | [diff] [blame] | 9 | #include <sdbusplus/bus/match.hpp> |
Alexander Hansen | cc37235 | 2025-01-14 14:15:39 +0100 | [diff] [blame] | 10 | #include <xyz/openbmc_project/Association/Definitions/server.hpp> |
| 11 | #include <xyz/openbmc_project/ObjectMapper/client.hpp> |
Alexander Hansen | de5e76f | 2025-02-20 16:30:11 +0100 | [diff] [blame] | 12 | #include <xyz/openbmc_project/Software/Version/client.hpp> |
Alexander Hansen | cc37235 | 2025-01-14 14:15:39 +0100 | [diff] [blame] | 13 | #include <xyz/openbmc_project/State/Host/client.hpp> |
| 14 | |
| 15 | #include <cstdint> |
| 16 | |
| 17 | PHOSPHOR_LOG2_USING; |
| 18 | |
| 19 | using namespace phosphor::software::manager; |
| 20 | |
Alexander Hansen | cec1475 | 2025-05-08 13:11:03 +0200 | [diff] [blame] | 21 | using AsyncMatch = sdbusplus::async::match; |
| 22 | |
| 23 | namespace RulesIntf = sdbusplus::bus::match::rules; |
| 24 | static constexpr auto serviceNameEM = "xyz.openbmc_project.EntityManager"; |
| 25 | |
| 26 | const auto matchRuleSender = RulesIntf::sender(serviceNameEM); |
| 27 | const auto matchRulePath = RulesIntf::path("/xyz/openbmc_project/inventory"); |
| 28 | |
Alexander Hansen | cc37235 | 2025-01-14 14:15:39 +0100 | [diff] [blame] | 29 | SoftwareManager::SoftwareManager(sdbusplus::async::context& ctx, |
| 30 | const std::string& serviceNameSuffix) : |
Alexander Hansen | cec1475 | 2025-05-08 13:11:03 +0200 | [diff] [blame] | 31 | ctx(ctx), |
| 32 | configIntfAddedMatch(ctx, RulesIntf::interfacesAdded() + matchRuleSender), |
| 33 | configIntfRemovedMatch(ctx, RulesIntf::interfacesRemoved() + matchRulePath), |
| 34 | serviceNameSuffix(serviceNameSuffix), |
Alexander Hansen | de5e76f | 2025-02-20 16:30:11 +0100 | [diff] [blame] | 35 | manager(ctx, sdbusplus::client::xyz::openbmc_project::software::Version<>:: |
| 36 | namespace_path) |
Alexander Hansen | cc37235 | 2025-01-14 14:15:39 +0100 | [diff] [blame] | 37 | { |
| 38 | const std::string serviceNameFull = |
| 39 | "xyz.openbmc_project.Software." + serviceNameSuffix; |
| 40 | |
| 41 | debug("requesting dbus name {BUSNAME}", "BUSNAME", serviceNameFull); |
| 42 | |
Alexander Hansen | de5e76f | 2025-02-20 16:30:11 +0100 | [diff] [blame] | 43 | ctx.request_name(serviceNameFull.c_str()); |
Alexander Hansen | cc37235 | 2025-01-14 14:15:39 +0100 | [diff] [blame] | 44 | |
Alexander Hansen | de5e76f | 2025-02-20 16:30:11 +0100 | [diff] [blame] | 45 | debug("Initialized SoftwareManager"); |
Alexander Hansen | cc37235 | 2025-01-14 14:15:39 +0100 | [diff] [blame] | 46 | } |
| 47 | |
Alexander Hansen | f3d407b | 2025-05-08 09:52:31 +0200 | [diff] [blame] | 48 | static sdbusplus::async::task<std::optional<SoftwareConfig>> getConfig( |
| 49 | sdbusplus::async::context& ctx, const std::string& service, |
| 50 | const std::string& objectPath, const std::string& interfacePrefix) |
Alexander Hansen | f3d407b | 2025-05-08 09:52:31 +0200 | [diff] [blame] | 51 | { |
| 52 | auto client = sdbusplus::async::proxy() |
| 53 | .service(service) |
| 54 | .path(objectPath) |
| 55 | .interface("org.freedesktop.DBus.Properties"); |
| 56 | |
| 57 | uint64_t vendorIANA = 0; |
| 58 | std::string compatible{}; |
| 59 | std::string configType{}; |
| 60 | std::string configName{}; |
| 61 | |
| 62 | const std::string interfaceName = interfacePrefix + ".FirmwareInfo"; |
| 63 | |
| 64 | try |
| 65 | { |
| 66 | { |
| 67 | auto propVendorIANA = co_await client.call<std::variant<uint64_t>>( |
| 68 | ctx, "Get", interfaceName, "VendorIANA"); |
| 69 | |
| 70 | vendorIANA = std::get<uint64_t>(propVendorIANA); |
| 71 | } |
| 72 | { |
| 73 | auto propCompatible = |
| 74 | co_await client.call<std::variant<std::string>>( |
| 75 | ctx, "Get", interfaceName, "CompatibleHardware"); |
| 76 | |
| 77 | compatible = std::get<std::string>(propCompatible); |
| 78 | } |
| 79 | { |
| 80 | auto propConfigType = |
| 81 | co_await client.call<std::variant<std::string>>( |
| 82 | ctx, "Get", interfacePrefix, "Type"); |
| 83 | |
| 84 | configType = std::get<std::string>(propConfigType); |
| 85 | } |
| 86 | { |
| 87 | auto propConfigName = |
| 88 | co_await client.call<std::variant<std::string>>( |
| 89 | ctx, "Get", interfacePrefix, "Name"); |
| 90 | |
| 91 | configName = std::get<std::string>(propConfigName); |
| 92 | } |
| 93 | } |
| 94 | catch (std::exception& e) |
| 95 | { |
| 96 | error("Failed to get config with {ERROR}", "ERROR", e); |
| 97 | co_return std::nullopt; |
| 98 | } |
| 99 | |
| 100 | co_return SoftwareConfig(objectPath, vendorIANA, compatible, configType, |
| 101 | configName); |
| 102 | } |
| 103 | |
Alexander Hansen | cc37235 | 2025-01-14 14:15:39 +0100 | [diff] [blame] | 104 | sdbusplus::async::task<> SoftwareManager::initDevices( |
| 105 | const std::vector<std::string>& configurationInterfaces) |
Alexander Hansen | cc37235 | 2025-01-14 14:15:39 +0100 | [diff] [blame] | 106 | { |
Alexander Hansen | cec1475 | 2025-05-08 13:11:03 +0200 | [diff] [blame] | 107 | ctx.spawn(interfaceAddedMatch(configurationInterfaces)); |
| 108 | ctx.spawn(interfaceRemovedMatch(configurationInterfaces)); |
| 109 | |
Alexander Hansen | cc37235 | 2025-01-14 14:15:39 +0100 | [diff] [blame] | 110 | auto client = sdbusplus::client::xyz::openbmc_project::ObjectMapper<>(ctx) |
| 111 | .service("xyz.openbmc_project.ObjectMapper") |
| 112 | .path("/xyz/openbmc_project/object_mapper"); |
| 113 | |
Alexander Hansen | 0a457ff | 2025-02-25 16:13:47 +0100 | [diff] [blame] | 114 | auto res = co_await client.get_sub_tree("/xyz/openbmc_project/inventory", 0, |
| 115 | configurationInterfaces); |
Alexander Hansen | cc37235 | 2025-01-14 14:15:39 +0100 | [diff] [blame] | 116 | |
| 117 | for (auto& iface : configurationInterfaces) |
| 118 | { |
| 119 | debug("[config] looking for dbus interface {INTF}", "INTF", iface); |
| 120 | } |
| 121 | |
| 122 | for (auto& [path, v] : res) |
| 123 | { |
| 124 | for (auto& [service, interfaceNames] : v) |
| 125 | { |
| 126 | std::string interfaceFound; |
| 127 | |
| 128 | for (std::string& interfaceName : interfaceNames) |
| 129 | { |
| 130 | for (auto& iface : configurationInterfaces) |
| 131 | { |
| 132 | if (interfaceName == iface) |
| 133 | { |
| 134 | interfaceFound = interfaceName; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if (interfaceFound.empty()) |
| 140 | { |
| 141 | continue; |
| 142 | } |
| 143 | |
Alexander Hansen | 9017479 | 2025-05-08 10:14:54 +0200 | [diff] [blame] | 144 | co_await handleInterfaceAdded(service, path, interfaceFound); |
Alexander Hansen | cc37235 | 2025-01-14 14:15:39 +0100 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Alexander Hansen | f3d407b | 2025-05-08 09:52:31 +0200 | [diff] [blame] | 148 | debug("Done with initial configuration"); |
Alexander Hansen | cc37235 | 2025-01-14 14:15:39 +0100 | [diff] [blame] | 149 | } |
Alexander Hansen | 9017479 | 2025-05-08 10:14:54 +0200 | [diff] [blame] | 150 | |
Alexander Hansen | 9017479 | 2025-05-08 10:14:54 +0200 | [diff] [blame] | 151 | sdbusplus::async::task<void> SoftwareManager::handleInterfaceAdded( |
| 152 | const std::string& service, const std::string& path, |
| 153 | const std::string& interface) |
Alexander Hansen | 9017479 | 2025-05-08 10:14:54 +0200 | [diff] [blame] | 154 | { |
| 155 | debug("Found configuration interface at {SERVICE}, {PATH}", "SERVICE", |
| 156 | service, "PATH", path); |
| 157 | |
| 158 | auto optConfig = co_await getConfig(ctx, service, path, interface); |
| 159 | |
| 160 | if (!optConfig.has_value()) |
| 161 | { |
| 162 | error("Failed to get configuration from {PATH}", "PATH", path); |
| 163 | co_return; |
| 164 | } |
| 165 | |
Alexander Hansen | cec1475 | 2025-05-08 13:11:03 +0200 | [diff] [blame] | 166 | if (devices.contains(optConfig.value().objectPath)) |
| 167 | { |
| 168 | error("Device configured from {PATH} is already known", "PATH", |
| 169 | optConfig.value().objectPath); |
| 170 | co_return; |
| 171 | } |
| 172 | |
Alexander Hansen | 9017479 | 2025-05-08 10:14:54 +0200 | [diff] [blame] | 173 | co_await initDevice(service, path, optConfig.value()); |
| 174 | |
| 175 | co_return; |
| 176 | } |
Alexander Hansen | cec1475 | 2025-05-08 13:11:03 +0200 | [diff] [blame] | 177 | |
| 178 | using BasicVariantType = |
| 179 | std::variant<std::vector<std::string>, std::string, int64_t, uint64_t, |
| 180 | double, int32_t, uint32_t, int16_t, uint16_t, uint8_t, bool>; |
| 181 | using InterfacesMap = boost::container::flat_map<std::string, BasicVariantType>; |
| 182 | using ConfigMap = boost::container::flat_map<std::string, InterfacesMap>; |
| 183 | |
Alexander Hansen | cec1475 | 2025-05-08 13:11:03 +0200 | [diff] [blame] | 184 | sdbusplus::async::task<void> SoftwareManager::interfaceAddedMatch( |
| 185 | std::vector<std::string> interfaces) |
Alexander Hansen | cec1475 | 2025-05-08 13:11:03 +0200 | [diff] [blame] | 186 | { |
| 187 | while (!ctx.stop_requested()) |
| 188 | { |
| 189 | std::tuple<std::string, ConfigMap> nextResult("", {}); |
| 190 | nextResult = co_await configIntfAddedMatch |
| 191 | .next<sdbusplus::message::object_path, ConfigMap>(); |
| 192 | |
| 193 | auto& [objPath, interfacesMap] = nextResult; |
| 194 | |
| 195 | for (auto& interface : interfaces) |
| 196 | { |
| 197 | if (interfacesMap.contains(interface)) |
| 198 | { |
| 199 | debug("detected interface {INTF} added on {PATH}", "INTF", |
| 200 | interface, "PATH", objPath); |
| 201 | |
| 202 | co_await handleInterfaceAdded(serviceNameEM, objPath, |
| 203 | interface); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
Alexander Hansen | cec1475 | 2025-05-08 13:11:03 +0200 | [diff] [blame] | 209 | sdbusplus::async::task<void> SoftwareManager::interfaceRemovedMatch( |
| 210 | std::vector<std::string> interfaces) |
Alexander Hansen | cec1475 | 2025-05-08 13:11:03 +0200 | [diff] [blame] | 211 | { |
| 212 | while (!ctx.stop_requested()) |
| 213 | { |
| 214 | auto nextResult = co_await configIntfRemovedMatch.next< |
| 215 | sdbusplus::message::object_path, std::vector<std::string>>(); |
| 216 | |
| 217 | auto& [objPath, interfacesRemoved] = nextResult; |
| 218 | |
| 219 | debug("detected interface removed on {PATH}", "PATH", objPath); |
| 220 | |
| 221 | for (auto& interface : interfaces) |
| 222 | { |
| 223 | if (std::ranges::find(interfacesRemoved, interface) != |
| 224 | interfacesRemoved.end()) |
| 225 | { |
| 226 | debug("detected interface {INTF} removed on {PATH}", "INTF", |
| 227 | interface, "PATH", objPath); |
| 228 | co_await handleInterfaceRemoved(objPath); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | sdbusplus::async::task<void> SoftwareManager::handleInterfaceRemoved( |
| 235 | const sdbusplus::message::object_path& objPath) |
| 236 | { |
| 237 | if (!devices.contains(objPath)) |
| 238 | { |
| 239 | debug("could not find a device to remove"); |
| 240 | co_return; |
| 241 | } |
| 242 | |
| 243 | if (devices[objPath]->updateInProgress) |
| 244 | { |
| 245 | // TODO: This code path needs to be cleaned up in the future to |
| 246 | // eventually remove the device. |
| 247 | debug( |
| 248 | "removal of device at {PATH} ignored because of in-progress update", |
| 249 | "PATH", objPath.str); |
| 250 | co_return; |
| 251 | } |
| 252 | |
| 253 | debug("removing device at {PATH}", "PATH", objPath.str); |
| 254 | devices.erase(objPath); |
| 255 | } |