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