| Alexander Hansen | 4e1142d | 2025-07-25 17:07:27 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation |
| James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 3 | |
| Brad Bishop | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 4 | #include "entity_manager.hpp" |
| James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 5 | |
| Christopher Meis | fc9e7fd | 2025-04-03 13:13:35 +0200 | [diff] [blame] | 6 | #include "../utils.hpp" |
| 7 | #include "../variant_visitors.hpp" |
| Christopher Meis | bdaa6b2 | 2025-04-02 10:49:02 +0200 | [diff] [blame] | 8 | #include "configuration.hpp" |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 9 | #include "dbus_interface.hpp" |
| Alexander Hansen | f57a259 | 2025-06-27 15:07:07 +0200 | [diff] [blame] | 10 | #include "log_device_inventory.hpp" |
| Brad Bishop | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 11 | #include "overlay.hpp" |
| Christopher Meis | 26fbbd5 | 2025-03-26 14:55:06 +0100 | [diff] [blame] | 12 | #include "perform_scan.hpp" |
| Benjamin Fair | ca2eb04 | 2022-09-13 06:40:42 +0000 | [diff] [blame] | 13 | #include "topology.hpp" |
| Christopher Meis | 59ef1e7 | 2025-04-16 08:53:25 +0200 | [diff] [blame] | 14 | #include "utils.hpp" |
| James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 15 | |
| James Feist | 11be667 | 2018-04-06 14:05:32 -0700 | [diff] [blame] | 16 | #include <boost/algorithm/string/case_conv.hpp> |
| James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 17 | #include <boost/algorithm/string/classification.hpp> |
| James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 18 | #include <boost/algorithm/string/replace.hpp> |
| James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 19 | #include <boost/algorithm/string/split.hpp> |
| James Feist | 02d2b93 | 2020-02-06 16:28:48 -0800 | [diff] [blame] | 20 | #include <boost/asio/io_context.hpp> |
| Ed Tanous | 49a888c | 2023-03-06 13:44:51 -0800 | [diff] [blame] | 21 | #include <boost/asio/post.hpp> |
| James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 22 | #include <boost/asio/steady_timer.hpp> |
| James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 23 | #include <boost/container/flat_map.hpp> |
| 24 | #include <boost/container/flat_set.hpp> |
| James Feist | f5125b0 | 2019-06-06 11:27:43 -0700 | [diff] [blame] | 25 | #include <boost/range/iterator_range.hpp> |
| James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 26 | #include <nlohmann/json.hpp> |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 27 | #include <phosphor-logging/lg2.hpp> |
| James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 28 | #include <sdbusplus/asio/connection.hpp> |
| 29 | #include <sdbusplus/asio/object_server.hpp> |
| 30 | |
| James Feist | 637b3ef | 2019-04-15 16:35:30 -0700 | [diff] [blame] | 31 | #include <filesystem> |
| James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 32 | #include <fstream> |
| Andrew Jeffery | e35d0ac | 2022-03-24 15:53:13 +1030 | [diff] [blame] | 33 | #include <functional> |
| Andrew Jeffery | 666583b | 2021-12-01 15:50:12 +1030 | [diff] [blame] | 34 | #include <map> |
| James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 35 | #include <regex> |
| James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 36 | constexpr const char* tempConfigDir = "/tmp/configuration/"; |
| 37 | constexpr const char* lastConfiguration = "/tmp/configuration/last.json"; |
| James Feist | f1b1414 | 2019-04-10 15:22:09 -0700 | [diff] [blame] | 38 | |
| Adrian Ambrożewicz | c789fca | 2020-05-14 15:50:05 +0200 | [diff] [blame] | 39 | static constexpr std::array<const char*, 6> settableInterfaces = { |
| 40 | "FanProfile", "Pid", "Pid.Zone", "Stepwise", "Thresholds", "Polling"}; |
| James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 41 | |
| Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 42 | const std::regex illegalDbusPathRegex("[^A-Za-z0-9_.]"); |
| 43 | const std::regex illegalDbusMemberRegex("[^A-Za-z0-9_]"); |
| James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 44 | |
| James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 45 | sdbusplus::asio::PropertyPermission getPermission(const std::string& interface) |
| James Feist | c6248a5 | 2018-08-14 10:09:45 -0700 | [diff] [blame] | 46 | { |
| 47 | return std::find(settableInterfaces.begin(), settableInterfaces.end(), |
| 48 | interface) != settableInterfaces.end() |
| 49 | ? sdbusplus::asio::PropertyPermission::readWrite |
| 50 | : sdbusplus::asio::PropertyPermission::readOnly; |
| 51 | } |
| 52 | |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 53 | EntityManager::EntityManager( |
| Alexander Hansen | a555acf | 2025-06-27 11:59:10 +0200 | [diff] [blame] | 54 | std::shared_ptr<sdbusplus::asio::connection>& systemBus, |
| 55 | boost::asio::io_context& io) : |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 56 | systemBus(systemBus), |
| 57 | objServer(sdbusplus::asio::object_server(systemBus, /*skipManager=*/true)), |
| 58 | lastJson(nlohmann::json::object()), |
| Alexander Hansen | b1340da | 2025-06-27 14:29:13 +0200 | [diff] [blame] | 59 | systemConfiguration(nlohmann::json::object()), io(io), |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 60 | dbus_interface(io, objServer), powerStatus(*systemBus), |
| 61 | propertiesChangedTimer(io) |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 62 | { |
| 63 | // All other objects that EntityManager currently support are under the |
| 64 | // inventory subtree. |
| 65 | // See the discussion at |
| 66 | // https://discord.com/channels/775381525260664832/1018929092009144380 |
| 67 | objServer.add_manager("/xyz/openbmc_project/inventory"); |
| James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 68 | |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 69 | entityIface = objServer.add_interface("/xyz/openbmc_project/EntityManager", |
| 70 | "xyz.openbmc_project.EntityManager"); |
| 71 | entityIface->register_method("ReScan", [this]() { |
| 72 | propertiesChangedCallback(); |
| 73 | }); |
| 74 | dbus_interface::tryIfaceInitialize(entityIface); |
| Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 75 | |
| 76 | initFilters(configuration.probeInterfaces); |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void EntityManager::postToDbus(const nlohmann::json& newConfiguration) |
| James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 80 | { |
| Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 81 | std::map<std::string, std::string> newBoards; // path -> name |
| Benjamin Fair | ca2eb04 | 2022-09-13 06:40:42 +0000 | [diff] [blame] | 82 | |
| James Feist | 97a63f1 | 2018-05-17 13:50:57 -0700 | [diff] [blame] | 83 | // iterate through boards |
| Patrick Williams | 2594d36 | 2022-09-28 06:46:24 -0500 | [diff] [blame] | 84 | for (const auto& [boardId, boardConfig] : newConfiguration.items()) |
| James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 85 | { |
| Alexander Hansen | 4fcd946 | 2025-07-30 17:44:44 +0200 | [diff] [blame] | 86 | postBoardToDBus(boardId, boardConfig, newBoards); |
| James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 87 | } |
| Benjamin Fair | ca2eb04 | 2022-09-13 06:40:42 +0000 | [diff] [blame] | 88 | |
| Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 89 | for (const auto& [assocPath, assocPropValue] : |
| Alexander Hansen | 32e7418 | 2025-08-20 16:16:00 +0200 | [diff] [blame] | 90 | topology.getAssocs(std::views::keys(newBoards))) |
| Benjamin Fair | ca2eb04 | 2022-09-13 06:40:42 +0000 | [diff] [blame] | 91 | { |
| Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 92 | auto findBoard = newBoards.find(assocPath); |
| 93 | if (findBoard == newBoards.end()) |
| 94 | { |
| 95 | continue; |
| 96 | } |
| Benjamin Fair | ca2eb04 | 2022-09-13 06:40:42 +0000 | [diff] [blame] | 97 | |
| Alexander Hansen | 57604ed | 2025-06-27 13:22:28 +0200 | [diff] [blame] | 98 | auto ifacePtr = dbus_interface.createInterface( |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 99 | assocPath, "xyz.openbmc_project.Association.Definitions", |
| Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 100 | findBoard->second); |
| 101 | |
| 102 | ifacePtr->register_property("Associations", assocPropValue); |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 103 | dbus_interface::tryIfaceInitialize(ifacePtr); |
| Benjamin Fair | ca2eb04 | 2022-09-13 06:40:42 +0000 | [diff] [blame] | 104 | } |
| James Feist | 1b2e224 | 2018-01-30 13:45:19 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| Alexander Hansen | 4fcd946 | 2025-07-30 17:44:44 +0200 | [diff] [blame] | 107 | void EntityManager::postBoardToDBus( |
| 108 | const std::string& boardId, const nlohmann::json& boardConfig, |
| 109 | std::map<std::string, std::string>& newBoards) |
| 110 | { |
| 111 | std::string boardName = boardConfig["Name"]; |
| 112 | std::string boardNameOrig = boardConfig["Name"]; |
| 113 | std::string jsonPointerPath = "/" + boardId; |
| 114 | // loop through newConfiguration, but use values from system |
| 115 | // configuration to be able to modify via dbus later |
| 116 | auto boardValues = systemConfiguration[boardId]; |
| 117 | auto findBoardType = boardValues.find("Type"); |
| 118 | std::string boardType; |
| 119 | if (findBoardType != boardValues.end() && |
| 120 | findBoardType->type() == nlohmann::json::value_t::string) |
| 121 | { |
| 122 | boardType = findBoardType->get<std::string>(); |
| 123 | std::regex_replace(boardType.begin(), boardType.begin(), |
| 124 | boardType.end(), illegalDbusMemberRegex, "_"); |
| 125 | } |
| 126 | else |
| 127 | { |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 128 | lg2::error("Unable to find type for {BOARD} reverting to Chassis.", |
| 129 | "BOARD", boardName); |
| Alexander Hansen | 4fcd946 | 2025-07-30 17:44:44 +0200 | [diff] [blame] | 130 | boardType = "Chassis"; |
| 131 | } |
| Alexander Hansen | 4fcd946 | 2025-07-30 17:44:44 +0200 | [diff] [blame] | 132 | |
| Christopher Meis | 811160e | 2025-08-08 08:48:37 +0200 | [diff] [blame] | 133 | const std::string boardPath = |
| 134 | em_utils::buildInventorySystemPath(boardName, boardType); |
| Alexander Hansen | 4fcd946 | 2025-07-30 17:44:44 +0200 | [diff] [blame] | 135 | |
| 136 | std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface = |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 137 | dbus_interface.createInterface( |
| 138 | boardPath, "xyz.openbmc_project.Inventory.Item", boardName); |
| Alexander Hansen | 4fcd946 | 2025-07-30 17:44:44 +0200 | [diff] [blame] | 139 | |
| 140 | std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface = |
| 141 | dbus_interface.createInterface( |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 142 | boardPath, "xyz.openbmc_project.Inventory.Item." + boardType, |
| 143 | boardNameOrig); |
| Alexander Hansen | 4fcd946 | 2025-07-30 17:44:44 +0200 | [diff] [blame] | 144 | |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 145 | dbus_interface.createAddObjectMethod(jsonPointerPath, boardPath, |
| 146 | systemConfiguration, boardNameOrig); |
| Alexander Hansen | 4fcd946 | 2025-07-30 17:44:44 +0200 | [diff] [blame] | 147 | |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 148 | dbus_interface.populateInterfaceFromJson( |
| 149 | systemConfiguration, jsonPointerPath, boardIface, boardValues); |
| Alexander Hansen | 4fcd946 | 2025-07-30 17:44:44 +0200 | [diff] [blame] | 150 | jsonPointerPath += "/"; |
| 151 | // iterate through board properties |
| 152 | for (const auto& [propName, propValue] : boardValues.items()) |
| 153 | { |
| 154 | if (propValue.type() == nlohmann::json::value_t::object) |
| 155 | { |
| 156 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 157 | dbus_interface.createInterface(boardPath, propName, |
| Alexander Hansen | 4fcd946 | 2025-07-30 17:44:44 +0200 | [diff] [blame] | 158 | boardNameOrig); |
| 159 | |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 160 | dbus_interface.populateInterfaceFromJson( |
| 161 | systemConfiguration, jsonPointerPath + propName, iface, |
| 162 | propValue); |
| Alexander Hansen | 4fcd946 | 2025-07-30 17:44:44 +0200 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| Alexander Hansen | 4fa4012 | 2025-07-30 18:26:35 +0200 | [diff] [blame] | 166 | nlohmann::json::iterator exposes = boardValues.find("Exposes"); |
| Alexander Hansen | 4fcd946 | 2025-07-30 17:44:44 +0200 | [diff] [blame] | 167 | if (exposes == boardValues.end()) |
| 168 | { |
| 169 | return; |
| 170 | } |
| 171 | // iterate through exposes |
| 172 | jsonPointerPath += "Exposes/"; |
| 173 | |
| 174 | // store the board level pointer so we can modify it on the way down |
| 175 | std::string jsonPointerPathBoard = jsonPointerPath; |
| 176 | size_t exposesIndex = -1; |
| Alexander Hansen | 4fa4012 | 2025-07-30 18:26:35 +0200 | [diff] [blame] | 177 | for (nlohmann::json& item : *exposes) |
| Alexander Hansen | 4fcd946 | 2025-07-30 17:44:44 +0200 | [diff] [blame] | 178 | { |
| Alexander Hansen | 4fa4012 | 2025-07-30 18:26:35 +0200 | [diff] [blame] | 179 | postExposesRecordsToDBus(item, exposesIndex, boardNameOrig, |
| 180 | jsonPointerPath, jsonPointerPathBoard, |
| 181 | boardPath, boardType); |
| 182 | } |
| Alexander Hansen | 4fcd946 | 2025-07-30 17:44:44 +0200 | [diff] [blame] | 183 | |
| Alexander Hansen | 4fa4012 | 2025-07-30 18:26:35 +0200 | [diff] [blame] | 184 | newBoards.emplace(boardPath, boardNameOrig); |
| 185 | } |
| 186 | |
| 187 | void EntityManager::postExposesRecordsToDBus( |
| 188 | nlohmann::json& item, size_t& exposesIndex, |
| 189 | const std::string& boardNameOrig, std::string jsonPointerPath, |
| 190 | const std::string& jsonPointerPathBoard, const std::string& boardPath, |
| 191 | const std::string& boardType) |
| 192 | { |
| 193 | exposesIndex++; |
| 194 | jsonPointerPath = jsonPointerPathBoard; |
| 195 | jsonPointerPath += std::to_string(exposesIndex); |
| 196 | |
| 197 | auto findName = item.find("Name"); |
| 198 | if (findName == item.end()) |
| 199 | { |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 200 | lg2::error("cannot find name in field {ITEM}", "ITEM", item); |
| Alexander Hansen | 4fa4012 | 2025-07-30 18:26:35 +0200 | [diff] [blame] | 201 | return; |
| 202 | } |
| 203 | auto findStatus = item.find("Status"); |
| 204 | // if status is not found it is assumed to be status = 'okay' |
| 205 | if (findStatus != item.end()) |
| 206 | { |
| 207 | if (*findStatus == "disabled") |
| Alexander Hansen | 4fcd946 | 2025-07-30 17:44:44 +0200 | [diff] [blame] | 208 | { |
| Alexander Hansen | 4fa4012 | 2025-07-30 18:26:35 +0200 | [diff] [blame] | 209 | return; |
| Alexander Hansen | 4fcd946 | 2025-07-30 17:44:44 +0200 | [diff] [blame] | 210 | } |
| Alexander Hansen | 4fa4012 | 2025-07-30 18:26:35 +0200 | [diff] [blame] | 211 | } |
| 212 | auto findType = item.find("Type"); |
| 213 | std::string itemType; |
| 214 | if (findType != item.end()) |
| 215 | { |
| 216 | itemType = findType->get<std::string>(); |
| 217 | std::regex_replace(itemType.begin(), itemType.begin(), itemType.end(), |
| 218 | illegalDbusPathRegex, "_"); |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | itemType = "unknown"; |
| 223 | } |
| 224 | std::string itemName = findName->get<std::string>(); |
| 225 | std::regex_replace(itemName.begin(), itemName.begin(), itemName.end(), |
| 226 | illegalDbusMemberRegex, "_"); |
| 227 | std::string ifacePath = boardPath; |
| 228 | ifacePath += "/"; |
| 229 | ifacePath += itemName; |
| 230 | |
| 231 | if (itemType == "BMC") |
| 232 | { |
| 233 | std::shared_ptr<sdbusplus::asio::dbus_interface> bmcIface = |
| 234 | dbus_interface.createInterface( |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 235 | ifacePath, "xyz.openbmc_project.Inventory.Item.Bmc", |
| Alexander Hansen | 4fa4012 | 2025-07-30 18:26:35 +0200 | [diff] [blame] | 236 | boardNameOrig); |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 237 | dbus_interface.populateInterfaceFromJson( |
| 238 | systemConfiguration, jsonPointerPath, bmcIface, item, |
| Alexander Hansen | 4fa4012 | 2025-07-30 18:26:35 +0200 | [diff] [blame] | 239 | getPermission(itemType)); |
| 240 | } |
| 241 | else if (itemType == "System") |
| 242 | { |
| 243 | std::shared_ptr<sdbusplus::asio::dbus_interface> systemIface = |
| 244 | dbus_interface.createInterface( |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 245 | ifacePath, "xyz.openbmc_project.Inventory.Item.System", |
| 246 | boardNameOrig); |
| 247 | dbus_interface.populateInterfaceFromJson( |
| 248 | systemConfiguration, jsonPointerPath, systemIface, item, |
| 249 | getPermission(itemType)); |
| Alexander Hansen | 4fa4012 | 2025-07-30 18:26:35 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | for (const auto& [name, config] : item.items()) |
| 253 | { |
| 254 | jsonPointerPath = jsonPointerPathBoard; |
| 255 | jsonPointerPath.append(std::to_string(exposesIndex)) |
| 256 | .append("/") |
| 257 | .append(name); |
| Alexander Hansen | 5aa65d8 | 2025-07-31 14:08:42 +0200 | [diff] [blame] | 258 | |
| 259 | if (!postConfigurationRecord(name, config, boardNameOrig, itemType, |
| 260 | jsonPointerPath, ifacePath)) |
| Alexander Hansen | 4fcd946 | 2025-07-30 17:44:44 +0200 | [diff] [blame] | 261 | { |
| Alexander Hansen | 5aa65d8 | 2025-07-31 14:08:42 +0200 | [diff] [blame] | 262 | break; |
| Alexander Hansen | 4fcd946 | 2025-07-30 17:44:44 +0200 | [diff] [blame] | 263 | } |
| Alexander Hansen | 4fcd946 | 2025-07-30 17:44:44 +0200 | [diff] [blame] | 264 | } |
| 265 | |
| Alexander Hansen | 4fa4012 | 2025-07-30 18:26:35 +0200 | [diff] [blame] | 266 | std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface = |
| 267 | dbus_interface.createInterface( |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 268 | ifacePath, "xyz.openbmc_project.Configuration." + itemType, |
| 269 | boardNameOrig); |
| Alexander Hansen | 4fa4012 | 2025-07-30 18:26:35 +0200 | [diff] [blame] | 270 | |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 271 | dbus_interface.populateInterfaceFromJson( |
| 272 | systemConfiguration, jsonPointerPath, itemIface, item, |
| Alexander Hansen | 4fa4012 | 2025-07-30 18:26:35 +0200 | [diff] [blame] | 273 | getPermission(itemType)); |
| 274 | |
| 275 | topology.addBoard(boardPath, boardType, boardNameOrig, item); |
| Alexander Hansen | 4fcd946 | 2025-07-30 17:44:44 +0200 | [diff] [blame] | 276 | } |
| 277 | |
| Alexander Hansen | 5aa65d8 | 2025-07-31 14:08:42 +0200 | [diff] [blame] | 278 | bool EntityManager::postConfigurationRecord( |
| 279 | const std::string& name, nlohmann::json& config, |
| 280 | const std::string& boardNameOrig, const std::string& itemType, |
| 281 | const std::string& jsonPointerPath, const std::string& ifacePath) |
| 282 | { |
| 283 | if (config.type() == nlohmann::json::value_t::object) |
| 284 | { |
| 285 | std::string ifaceName = "xyz.openbmc_project.Configuration."; |
| 286 | ifaceName.append(itemType).append(".").append(name); |
| 287 | |
| 288 | std::shared_ptr<sdbusplus::asio::dbus_interface> objectIface = |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 289 | dbus_interface.createInterface(ifacePath, ifaceName, boardNameOrig); |
| Alexander Hansen | 5aa65d8 | 2025-07-31 14:08:42 +0200 | [diff] [blame] | 290 | |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 291 | dbus_interface.populateInterfaceFromJson( |
| 292 | systemConfiguration, jsonPointerPath, objectIface, config, |
| 293 | getPermission(name)); |
| Alexander Hansen | 5aa65d8 | 2025-07-31 14:08:42 +0200 | [diff] [blame] | 294 | } |
| 295 | else if (config.type() == nlohmann::json::value_t::array) |
| 296 | { |
| 297 | size_t index = 0; |
| 298 | if (config.empty()) |
| 299 | { |
| 300 | return true; |
| 301 | } |
| 302 | bool isLegal = true; |
| 303 | auto type = config[0].type(); |
| 304 | if (type != nlohmann::json::value_t::object) |
| 305 | { |
| 306 | return true; |
| 307 | } |
| 308 | |
| 309 | // verify legal json |
| 310 | for (const auto& arrayItem : config) |
| 311 | { |
| 312 | if (arrayItem.type() != type) |
| 313 | { |
| 314 | isLegal = false; |
| 315 | break; |
| 316 | } |
| 317 | } |
| 318 | if (!isLegal) |
| 319 | { |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 320 | lg2::error("dbus format error {JSON}", "JSON", config); |
| Alexander Hansen | 5aa65d8 | 2025-07-31 14:08:42 +0200 | [diff] [blame] | 321 | return false; |
| 322 | } |
| 323 | |
| 324 | for (auto& arrayItem : config) |
| 325 | { |
| 326 | std::string ifaceName = "xyz.openbmc_project.Configuration."; |
| 327 | ifaceName.append(itemType).append(".").append(name); |
| 328 | ifaceName.append(std::to_string(index)); |
| 329 | |
| 330 | std::shared_ptr<sdbusplus::asio::dbus_interface> objectIface = |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 331 | dbus_interface.createInterface(ifacePath, ifaceName, |
| Alexander Hansen | 5aa65d8 | 2025-07-31 14:08:42 +0200 | [diff] [blame] | 332 | boardNameOrig); |
| 333 | |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 334 | dbus_interface.populateInterfaceFromJson( |
| 335 | systemConfiguration, |
| Alexander Hansen | 5aa65d8 | 2025-07-31 14:08:42 +0200 | [diff] [blame] | 336 | jsonPointerPath + "/" + std::to_string(index), objectIface, |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 337 | arrayItem, getPermission(name)); |
| Alexander Hansen | 5aa65d8 | 2025-07-31 14:08:42 +0200 | [diff] [blame] | 338 | index++; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | return true; |
| 343 | } |
| 344 | |
| Andrew Jeffery | 5519293 | 2022-03-24 12:29:27 +1030 | [diff] [blame] | 345 | static bool deviceRequiresPowerOn(const nlohmann::json& entity) |
| 346 | { |
| 347 | auto powerState = entity.find("PowerState"); |
| Andrew Jeffery | b620944 | 2022-03-24 12:36:20 +1030 | [diff] [blame] | 348 | if (powerState == entity.end()) |
| Andrew Jeffery | 5519293 | 2022-03-24 12:29:27 +1030 | [diff] [blame] | 349 | { |
| Andrew Jeffery | b620944 | 2022-03-24 12:36:20 +1030 | [diff] [blame] | 350 | return false; |
| Andrew Jeffery | 5519293 | 2022-03-24 12:29:27 +1030 | [diff] [blame] | 351 | } |
| 352 | |
| Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 353 | const auto* ptr = powerState->get_ptr<const std::string*>(); |
| 354 | if (ptr == nullptr) |
| Andrew Jeffery | b620944 | 2022-03-24 12:36:20 +1030 | [diff] [blame] | 355 | { |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | return *ptr == "On" || *ptr == "BiosPost"; |
| Andrew Jeffery | 5519293 | 2022-03-24 12:29:27 +1030 | [diff] [blame] | 360 | } |
| 361 | |
| Andrew Jeffery | 89ec352 | 2022-03-24 13:30:41 +1030 | [diff] [blame] | 362 | static void pruneDevice(const nlohmann::json& systemConfiguration, |
| 363 | const bool powerOff, const bool scannedPowerOff, |
| 364 | const std::string& name, const nlohmann::json& device) |
| 365 | { |
| 366 | if (systemConfiguration.contains(name)) |
| 367 | { |
| 368 | return; |
| 369 | } |
| 370 | |
| Andrew Jeffery | 4db38bc | 2022-03-24 13:42:41 +1030 | [diff] [blame] | 371 | if (deviceRequiresPowerOn(device) && (powerOff || scannedPowerOff)) |
| Andrew Jeffery | 89ec352 | 2022-03-24 13:30:41 +1030 | [diff] [blame] | 372 | { |
| Andrew Jeffery | 89ec352 | 2022-03-24 13:30:41 +1030 | [diff] [blame] | 373 | return; |
| 374 | } |
| 375 | |
| 376 | logDeviceRemoved(device); |
| 377 | } |
| 378 | |
| Alexander Hansen | 95ab18f | 2025-06-27 13:58:10 +0200 | [diff] [blame] | 379 | void EntityManager::startRemovedTimer(boost::asio::steady_timer& timer, |
| 380 | nlohmann::json& systemConfiguration) |
| James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 381 | { |
| James Feist | fb00f39 | 2019-06-25 14:16:48 -0700 | [diff] [blame] | 382 | if (systemConfiguration.empty() || lastJson.empty()) |
| 383 | { |
| 384 | return; // not ready yet |
| 385 | } |
| James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 386 | if (scannedPowerOn) |
| 387 | { |
| 388 | return; |
| 389 | } |
| 390 | |
| Alexander Hansen | 0ab32b3 | 2025-06-27 14:50:33 +0200 | [diff] [blame] | 391 | if (!powerStatus.isPowerOn() && scannedPowerOff) |
| James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 392 | { |
| 393 | return; |
| 394 | } |
| 395 | |
| James Feist | b1728ca | 2020-04-30 15:40:55 -0700 | [diff] [blame] | 396 | timer.expires_after(std::chrono::seconds(10)); |
| Andrew Jeffery | 27a1cfb | 2022-03-24 12:31:53 +1030 | [diff] [blame] | 397 | timer.async_wait( |
| Alexander Hansen | 95ab18f | 2025-06-27 13:58:10 +0200 | [diff] [blame] | 398 | [&systemConfiguration, this](const boost::system::error_code& ec) { |
| Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 399 | if (ec == boost::asio::error::operation_aborted) |
| 400 | { |
| 401 | return; |
| 402 | } |
| Andrew Jeffery | 27a1cfb | 2022-03-24 12:31:53 +1030 | [diff] [blame] | 403 | |
| Alexander Hansen | 0ab32b3 | 2025-06-27 14:50:33 +0200 | [diff] [blame] | 404 | bool powerOff = !powerStatus.isPowerOn(); |
| Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 405 | for (const auto& [name, device] : lastJson.items()) |
| 406 | { |
| 407 | pruneDevice(systemConfiguration, powerOff, scannedPowerOff, |
| 408 | name, device); |
| 409 | } |
| Andrew Jeffery | 89ec352 | 2022-03-24 13:30:41 +1030 | [diff] [blame] | 410 | |
| Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 411 | scannedPowerOff = true; |
| 412 | if (!powerOff) |
| 413 | { |
| 414 | scannedPowerOn = true; |
| 415 | } |
| 416 | }); |
| James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 419 | void EntityManager::pruneConfiguration(bool powerOff, const std::string& name, |
| 420 | const nlohmann::json& device) |
| Andrew Jeffery | 0d0c146 | 2022-03-24 15:26:39 +1030 | [diff] [blame] | 421 | { |
| 422 | if (powerOff && deviceRequiresPowerOn(device)) |
| 423 | { |
| 424 | // power not on yet, don't know if it's there or not |
| 425 | return; |
| 426 | } |
| 427 | |
| Alexander Hansen | 57604ed | 2025-06-27 13:22:28 +0200 | [diff] [blame] | 428 | auto& ifaces = dbus_interface.getDeviceInterfaces(device); |
| Andrew Jeffery | 0d0c146 | 2022-03-24 15:26:39 +1030 | [diff] [blame] | 429 | for (auto& iface : ifaces) |
| 430 | { |
| 431 | auto sharedPtr = iface.lock(); |
| 432 | if (!!sharedPtr) |
| 433 | { |
| 434 | objServer.remove_interface(sharedPtr); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | ifaces.clear(); |
| 439 | systemConfiguration.erase(name); |
| Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 440 | topology.remove(device["Name"].get<std::string>()); |
| Andrew Jeffery | 0d0c146 | 2022-03-24 15:26:39 +1030 | [diff] [blame] | 441 | logDeviceRemoved(device); |
| 442 | } |
| 443 | |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 444 | void EntityManager::publishNewConfiguration( |
| Andrew Jeffery | e35d0ac | 2022-03-24 15:53:13 +1030 | [diff] [blame] | 445 | const size_t& instance, const size_t count, |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 446 | boost::asio::steady_timer& timer, // Gerrit discussion: |
| Andrew Jeffery | e35d0ac | 2022-03-24 15:53:13 +1030 | [diff] [blame] | 447 | // https://gerrit.openbmc-project.xyz/c/openbmc/entity-manager/+/52316/6 |
| 448 | // |
| 449 | // Discord discussion: |
| 450 | // https://discord.com/channels/775381525260664832/867820390406422538/958048437729910854 |
| 451 | // |
| 452 | // NOLINTNEXTLINE(performance-unnecessary-value-param) |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 453 | const nlohmann::json newConfiguration) |
| Andrew Jeffery | e35d0ac | 2022-03-24 15:53:13 +1030 | [diff] [blame] | 454 | { |
| Alexander Hansen | a555acf | 2025-06-27 11:59:10 +0200 | [diff] [blame] | 455 | loadOverlays(newConfiguration, io); |
| Andrew Jeffery | e35d0ac | 2022-03-24 15:53:13 +1030 | [diff] [blame] | 456 | |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 457 | boost::asio::post(io, [this]() { |
| Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 458 | if (!writeJsonFiles(systemConfiguration)) |
| Andrew Jeffery | e35d0ac | 2022-03-24 15:53:13 +1030 | [diff] [blame] | 459 | { |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 460 | lg2::error("Error writing json files"); |
| Andrew Jeffery | e35d0ac | 2022-03-24 15:53:13 +1030 | [diff] [blame] | 461 | } |
| 462 | }); |
| 463 | |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 464 | boost::asio::post(io, [this, &instance, count, &timer, newConfiguration]() { |
| 465 | postToDbus(newConfiguration); |
| Andrew Jeffery | e35d0ac | 2022-03-24 15:53:13 +1030 | [diff] [blame] | 466 | if (count == instance) |
| 467 | { |
| Alexander Hansen | 95ab18f | 2025-06-27 13:58:10 +0200 | [diff] [blame] | 468 | startRemovedTimer(timer, systemConfiguration); |
| Andrew Jeffery | e35d0ac | 2022-03-24 15:53:13 +1030 | [diff] [blame] | 469 | } |
| 470 | }); |
| 471 | } |
| 472 | |
| James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 473 | // main properties changed entry |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 474 | void EntityManager::propertiesChangedCallback() |
| James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 475 | { |
| Alexander Hansen | fc1b1e2 | 2025-06-27 14:34:11 +0200 | [diff] [blame] | 476 | propertiesChangedInstance++; |
| 477 | size_t count = propertiesChangedInstance; |
| James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 478 | |
| Alexander Hansen | b1340da | 2025-06-27 14:29:13 +0200 | [diff] [blame] | 479 | propertiesChangedTimer.expires_after(std::chrono::milliseconds(500)); |
| James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 480 | |
| 481 | // setup an async wait as we normally get flooded with new requests |
| Alexander Hansen | b1340da | 2025-06-27 14:29:13 +0200 | [diff] [blame] | 482 | propertiesChangedTimer.async_wait( |
| 483 | [this, count](const boost::system::error_code& ec) { |
| 484 | if (ec == boost::asio::error::operation_aborted) |
| 485 | { |
| 486 | // we were cancelled |
| 487 | return; |
| 488 | } |
| 489 | if (ec) |
| 490 | { |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 491 | lg2::error("async wait error {ERR}", "ERR", ec.message()); |
| Alexander Hansen | b1340da | 2025-06-27 14:29:13 +0200 | [diff] [blame] | 492 | return; |
| 493 | } |
| James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 494 | |
| Alexander Hansen | b1340da | 2025-06-27 14:29:13 +0200 | [diff] [blame] | 495 | if (propertiesChangedInProgress) |
| 496 | { |
| 497 | propertiesChangedCallback(); |
| 498 | return; |
| 499 | } |
| 500 | propertiesChangedInProgress = true; |
| James Feist | 2539ccd | 2020-05-01 16:15:08 -0700 | [diff] [blame] | 501 | |
| Alexander Hansen | b1340da | 2025-06-27 14:29:13 +0200 | [diff] [blame] | 502 | nlohmann::json oldConfiguration = systemConfiguration; |
| 503 | auto missingConfigurations = std::make_shared<nlohmann::json>(); |
| 504 | *missingConfigurations = systemConfiguration; |
| James Feist | 899e17f | 2019-09-13 11:46:29 -0700 | [diff] [blame] | 505 | |
| Alexander Hansen | b1340da | 2025-06-27 14:29:13 +0200 | [diff] [blame] | 506 | auto perfScan = std::make_shared<scan::PerformScan>( |
| Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 507 | *this, *missingConfigurations, configuration.configurations, io, |
| Alexander Hansen | b1340da | 2025-06-27 14:29:13 +0200 | [diff] [blame] | 508 | [this, count, oldConfiguration, missingConfigurations]() { |
| 509 | // this is something that since ac has been applied to the |
| 510 | // bmc we saw, and we no longer see it |
| Alexander Hansen | 0ab32b3 | 2025-06-27 14:50:33 +0200 | [diff] [blame] | 511 | bool powerOff = !powerStatus.isPowerOn(); |
| Alexander Hansen | b1340da | 2025-06-27 14:29:13 +0200 | [diff] [blame] | 512 | for (const auto& [name, device] : |
| 513 | missingConfigurations->items()) |
| 514 | { |
| 515 | pruneConfiguration(powerOff, name, device); |
| 516 | } |
| Alexander Hansen | b1340da | 2025-06-27 14:29:13 +0200 | [diff] [blame] | 517 | nlohmann::json newConfiguration = systemConfiguration; |
| 518 | |
| Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 519 | deriveNewConfiguration(oldConfiguration, newConfiguration); |
| Alexander Hansen | b1340da | 2025-06-27 14:29:13 +0200 | [diff] [blame] | 520 | |
| 521 | for (const auto& [_, device] : newConfiguration.items()) |
| 522 | { |
| 523 | logDeviceAdded(device); |
| 524 | } |
| 525 | |
| 526 | propertiesChangedInProgress = false; |
| 527 | |
| 528 | boost::asio::post(io, [this, newConfiguration, count] { |
| 529 | publishNewConfiguration( |
| Alexander Hansen | fc1b1e2 | 2025-06-27 14:34:11 +0200 | [diff] [blame] | 530 | std::ref(propertiesChangedInstance), count, |
| Alexander Hansen | b1340da | 2025-06-27 14:29:13 +0200 | [diff] [blame] | 531 | std::ref(propertiesChangedTimer), newConfiguration); |
| 532 | }); |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 533 | }); |
| Alexander Hansen | b1340da | 2025-06-27 14:29:13 +0200 | [diff] [blame] | 534 | perfScan->run(); |
| 535 | }); |
| James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 536 | } |
| 537 | |
| Matt Spinler | 8d1ac3a | 2023-02-02 09:48:04 -0600 | [diff] [blame] | 538 | // Check if InterfacesAdded payload contains an iface that needs probing. |
| Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 539 | static bool iaContainsProbeInterface( |
| Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 540 | sdbusplus::message_t& msg, |
| 541 | const std::unordered_set<std::string>& probeInterfaces) |
| Matt Spinler | 8d1ac3a | 2023-02-02 09:48:04 -0600 | [diff] [blame] | 542 | { |
| 543 | sdbusplus::message::object_path path; |
| 544 | DBusObject interfaces; |
| Matt Spinler | 8d1ac3a | 2023-02-02 09:48:04 -0600 | [diff] [blame] | 545 | msg.read(path, interfaces); |
| Eldin Lee | 802cbf2 | 2025-08-22 14:44:04 +0800 | [diff] [blame] | 546 | return std::ranges::any_of(interfaces | std::views::keys, |
| 547 | [&probeInterfaces](const auto& ifaceName) { |
| 548 | return probeInterfaces.contains(ifaceName); |
| 549 | }); |
| Matt Spinler | 8d1ac3a | 2023-02-02 09:48:04 -0600 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | // Check if InterfacesRemoved payload contains an iface that needs probing. |
| Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 553 | static bool irContainsProbeInterface( |
| Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 554 | sdbusplus::message_t& msg, |
| 555 | const std::unordered_set<std::string>& probeInterfaces) |
| Matt Spinler | 8d1ac3a | 2023-02-02 09:48:04 -0600 | [diff] [blame] | 556 | { |
| 557 | sdbusplus::message::object_path path; |
| Eldin Lee | 802cbf2 | 2025-08-22 14:44:04 +0800 | [diff] [blame] | 558 | std::vector<std::string> interfaces; |
| Matt Spinler | 8d1ac3a | 2023-02-02 09:48:04 -0600 | [diff] [blame] | 559 | msg.read(path, interfaces); |
| Eldin Lee | 802cbf2 | 2025-08-22 14:44:04 +0800 | [diff] [blame] | 560 | return std::ranges::any_of(interfaces, |
| 561 | [&probeInterfaces](const auto& ifaceName) { |
| 562 | return probeInterfaces.contains(ifaceName); |
| 563 | }); |
| Matt Spinler | 8d1ac3a | 2023-02-02 09:48:04 -0600 | [diff] [blame] | 564 | } |
| 565 | |
| Alexander Hansen | ad28e40 | 2025-06-25 12:38:20 +0200 | [diff] [blame] | 566 | void EntityManager::handleCurrentConfigurationJson() |
| 567 | { |
| Alexander Hansen | e665185 | 2025-01-21 16:22:05 +0100 | [diff] [blame] | 568 | if (EM_CACHE_CONFIGURATION && em_utils::fwVersionIsSame()) |
| Alexander Hansen | ad28e40 | 2025-06-25 12:38:20 +0200 | [diff] [blame] | 569 | { |
| Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 570 | if (std::filesystem::is_regular_file(currentConfiguration)) |
| Alexander Hansen | ad28e40 | 2025-06-25 12:38:20 +0200 | [diff] [blame] | 571 | { |
| 572 | // this file could just be deleted, but it's nice for debug |
| 573 | std::filesystem::create_directory(tempConfigDir); |
| 574 | std::filesystem::remove(lastConfiguration); |
| Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 575 | std::filesystem::copy(currentConfiguration, lastConfiguration); |
| 576 | std::filesystem::remove(currentConfiguration); |
| Alexander Hansen | ad28e40 | 2025-06-25 12:38:20 +0200 | [diff] [blame] | 577 | |
| 578 | std::ifstream jsonStream(lastConfiguration); |
| 579 | if (jsonStream.good()) |
| 580 | { |
| 581 | auto data = nlohmann::json::parse(jsonStream, nullptr, false); |
| 582 | if (data.is_discarded()) |
| 583 | { |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 584 | lg2::error("syntax error in {PATH}", "PATH", |
| 585 | lastConfiguration); |
| Alexander Hansen | ad28e40 | 2025-06-25 12:38:20 +0200 | [diff] [blame] | 586 | } |
| 587 | else |
| 588 | { |
| 589 | lastJson = std::move(data); |
| 590 | } |
| 591 | } |
| 592 | else |
| 593 | { |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 594 | lg2::error("unable to open {PATH}", "PATH", lastConfiguration); |
| Alexander Hansen | ad28e40 | 2025-06-25 12:38:20 +0200 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | } |
| 598 | else |
| 599 | { |
| 600 | // not an error, just logging at this level to make it in the journal |
| Marc Olberding | b2f8282 | 2025-09-25 13:56:41 -0700 | [diff] [blame] | 601 | std::error_code ec; |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 602 | lg2::error("Clearing previous configuration"); |
| Marc Olberding | b2f8282 | 2025-09-25 13:56:41 -0700 | [diff] [blame] | 603 | std::filesystem::remove(currentConfiguration, ec); |
| Alexander Hansen | ad28e40 | 2025-06-25 12:38:20 +0200 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 607 | void EntityManager::registerCallback(const std::string& path) |
| James Feist | 75fdeeb | 2018-02-20 14:26:16 -0800 | [diff] [blame] | 608 | { |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 609 | if (dbusMatches.contains(path)) |
| 610 | { |
| 611 | return; |
| 612 | } |
| Nan Zhou | a331567 | 2022-09-20 19:48:14 +0000 | [diff] [blame] | 613 | |
| Alexander Hansen | 6080318 | 2025-06-27 14:41:28 +0200 | [diff] [blame] | 614 | lg2::debug("creating PropertiesChanged match on {PATH}", "PATH", path); |
| 615 | |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 616 | std::function<void(sdbusplus::message_t & message)> eventHandler = |
| 617 | [&](sdbusplus::message_t&) { propertiesChangedCallback(); }; |
| James Feist | fd1264a | 2018-05-03 12:10:00 -0700 | [diff] [blame] | 618 | |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 619 | sdbusplus::bus::match_t match( |
| 620 | static_cast<sdbusplus::bus_t&>(*systemBus), |
| 621 | "type='signal',member='PropertiesChanged',path='" + path + "'", |
| 622 | eventHandler); |
| 623 | dbusMatches.emplace(path, std::move(match)); |
| 624 | } |
| James Feist | fd1264a | 2018-05-03 12:10:00 -0700 | [diff] [blame] | 625 | |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 626 | // We need a poke from DBus for static providers that create all their |
| 627 | // objects prior to claiming a well-known name, and thus don't emit any |
| 628 | // org.freedesktop.DBus.Properties signals. Similarly if a process exits |
| 629 | // for any reason, expected or otherwise, we'll need a poke to remove |
| 630 | // entities from DBus. |
| Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 631 | void EntityManager::initFilters( |
| 632 | const std::unordered_set<std::string>& probeInterfaces) |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 633 | { |
| Alexander Hansen | 0f7bd78 | 2025-06-27 13:39:53 +0200 | [diff] [blame] | 634 | nameOwnerChangedMatch = std::make_unique<sdbusplus::bus::match_t>( |
| Patrick Williams | 2af3922 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 635 | static_cast<sdbusplus::bus_t&>(*systemBus), |
| Brad Bishop | c76af0f | 2020-12-04 13:50:23 -0500 | [diff] [blame] | 636 | sdbusplus::bus::match::rules::nameOwnerChanged(), |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 637 | [this](sdbusplus::message_t& m) { |
| Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 638 | auto [name, oldOwner, |
| 639 | newOwner] = m.unpack<std::string, std::string, std::string>(); |
| Patrick Williams | 7b8786f | 2022-10-10 10:23:37 -0500 | [diff] [blame] | 640 | |
| Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 641 | if (name.starts_with(':')) |
| 642 | { |
| 643 | // We should do nothing with unique-name connections. |
| 644 | return; |
| 645 | } |
| Patrick Williams | 7b8786f | 2022-10-10 10:23:37 -0500 | [diff] [blame] | 646 | |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 647 | propertiesChangedCallback(); |
| Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 648 | }); |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 649 | |
| Brad Bishop | 10a8c5f | 2020-12-07 21:40:07 -0500 | [diff] [blame] | 650 | // We also need a poke from DBus when new interfaces are created or |
| 651 | // destroyed. |
| Alexander Hansen | 0f7bd78 | 2025-06-27 13:39:53 +0200 | [diff] [blame] | 652 | interfacesAddedMatch = std::make_unique<sdbusplus::bus::match_t>( |
| Patrick Williams | 2af3922 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 653 | static_cast<sdbusplus::bus_t&>(*systemBus), |
| Brad Bishop | 10a8c5f | 2020-12-07 21:40:07 -0500 | [diff] [blame] | 654 | sdbusplus::bus::match::rules::interfacesAdded(), |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 655 | [this, probeInterfaces](sdbusplus::message_t& msg) { |
| Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 656 | if (iaContainsProbeInterface(msg, probeInterfaces)) |
| 657 | { |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 658 | propertiesChangedCallback(); |
| Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 659 | } |
| 660 | }); |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 661 | |
| Alexander Hansen | 0f7bd78 | 2025-06-27 13:39:53 +0200 | [diff] [blame] | 662 | interfacesRemovedMatch = std::make_unique<sdbusplus::bus::match_t>( |
| Patrick Williams | 2af3922 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 663 | static_cast<sdbusplus::bus_t&>(*systemBus), |
| Brad Bishop | 10a8c5f | 2020-12-07 21:40:07 -0500 | [diff] [blame] | 664 | sdbusplus::bus::match::rules::interfacesRemoved(), |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 665 | [this, probeInterfaces](sdbusplus::message_t& msg) { |
| Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 666 | if (irContainsProbeInterface(msg, probeInterfaces)) |
| 667 | { |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 668 | propertiesChangedCallback(); |
| Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 669 | } |
| 670 | }); |
| Christopher Meis | cf6a75b | 2025-06-03 07:53:50 +0200 | [diff] [blame] | 671 | } |