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