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