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