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