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