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