Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [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 perform_scan.cpp |
| 17 | #include "entity_manager.hpp" |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 18 | |
| 19 | #include <boost/algorithm/string/predicate.hpp> |
| 20 | #include <boost/asio/steady_timer.hpp> |
| 21 | #include <boost/container/flat_map.hpp> |
| 22 | #include <boost/container/flat_set.hpp> |
| 23 | |
| 24 | #include <charconv> |
| 25 | |
Brad Bishop | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 26 | /* Hacks from splitting entity_manager.cpp */ |
Ed Tanous | fc17142 | 2024-04-04 17:18:16 -0700 | [diff] [blame] | 27 | // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 28 | extern std::shared_ptr<sdbusplus::asio::connection> systemBus; |
| 29 | extern nlohmann::json lastJson; |
| 30 | extern void |
| 31 | propertiesChangedCallback(nlohmann::json& systemConfiguration, |
| 32 | sdbusplus::asio::object_server& objServer); |
Ed Tanous | fc17142 | 2024-04-04 17:18:16 -0700 | [diff] [blame] | 33 | // NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables) |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 34 | |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 35 | using GetSubTreeType = std::vector< |
| 36 | std::pair<std::string, |
| 37 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
| 38 | |
| 39 | constexpr const int32_t maxMapperDepth = 0; |
| 40 | |
| 41 | constexpr const bool debug = false; |
| 42 | |
Andrew Jeffery | d9b6784 | 2022-04-05 16:44:20 +0930 | [diff] [blame] | 43 | struct DBusInterfaceInstance |
| 44 | { |
| 45 | std::string busName; |
| 46 | std::string path; |
| 47 | std::string interface; |
| 48 | }; |
| 49 | |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 50 | void getInterfaces( |
Andrew Jeffery | d9b6784 | 2022-04-05 16:44:20 +0930 | [diff] [blame] | 51 | const DBusInterfaceInstance& instance, |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 52 | const std::vector<std::shared_ptr<PerformProbe>>& probeVector, |
| 53 | const std::shared_ptr<PerformScan>& scan, size_t retries = 5) |
| 54 | { |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 55 | if (retries == 0U) |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 56 | { |
Andrew Jeffery | d9b6784 | 2022-04-05 16:44:20 +0930 | [diff] [blame] | 57 | std::cerr << "retries exhausted on " << instance.busName << " " |
| 58 | << instance.path << " " << instance.interface << "\n"; |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 59 | return; |
| 60 | } |
| 61 | |
| 62 | systemBus->async_method_call( |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame^] | 63 | [instance, scan, probeVector, |
| 64 | retries](boost::system::error_code& errc, const DBusInterface& resp) { |
| 65 | if (errc) |
| 66 | { |
| 67 | std::cerr << "error calling getall on " << instance.busName |
| 68 | << " " << instance.path << " " |
| 69 | << instance.interface << "\n"; |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 70 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame^] | 71 | auto timer = std::make_shared<boost::asio::steady_timer>(io); |
| 72 | timer->expires_after(std::chrono::seconds(2)); |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 73 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame^] | 74 | timer->async_wait([timer, instance, scan, probeVector, |
| 75 | retries](const boost::system::error_code&) { |
| 76 | getInterfaces(instance, probeVector, scan, retries - 1); |
| 77 | }); |
| 78 | return; |
| 79 | } |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 80 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame^] | 81 | scan->dbusProbeObjects[instance.path][instance.interface] = resp; |
| 82 | }, |
Andrew Jeffery | d9b6784 | 2022-04-05 16:44:20 +0930 | [diff] [blame] | 83 | instance.busName, instance.path, "org.freedesktop.DBus.Properties", |
| 84 | "GetAll", instance.interface); |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 85 | |
| 86 | if constexpr (debug) |
| 87 | { |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 88 | std::cerr << __LINE__ << "\n"; |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Andrew Jeffery | 4dcc55d | 2022-04-05 16:49:42 +0930 | [diff] [blame] | 92 | static void registerCallback(nlohmann::json& systemConfiguration, |
| 93 | sdbusplus::asio::object_server& objServer, |
| 94 | const std::string& path) |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 95 | { |
Patrick Williams | 2af3922 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 96 | static boost::container::flat_map<std::string, sdbusplus::bus::match_t> |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 97 | dbusMatches; |
| 98 | |
| 99 | auto find = dbusMatches.find(path); |
| 100 | if (find != dbusMatches.end()) |
| 101 | { |
| 102 | return; |
| 103 | } |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 104 | |
Patrick Williams | 2af3922 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 105 | std::function<void(sdbusplus::message_t & message)> eventHandler = |
| 106 | [&](sdbusplus::message_t&) { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame^] | 107 | propertiesChangedCallback(systemConfiguration, objServer); |
| 108 | }; |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 109 | |
Patrick Williams | 2af3922 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 110 | sdbusplus::bus::match_t match( |
| 111 | static_cast<sdbusplus::bus_t&>(*systemBus), |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 112 | "type='signal',member='PropertiesChanged',path='" + path + "'", |
| 113 | eventHandler); |
| 114 | dbusMatches.emplace(path, std::move(match)); |
| 115 | } |
| 116 | |
Andrew Jeffery | 8d2761e | 2022-04-05 16:26:28 +0930 | [diff] [blame] | 117 | static void |
| 118 | processDbusObjects(std::vector<std::shared_ptr<PerformProbe>>& probeVector, |
| 119 | const std::shared_ptr<PerformScan>& scan, |
| 120 | const GetSubTreeType& interfaceSubtree) |
| 121 | { |
Andrew Jeffery | 8d2761e | 2022-04-05 16:26:28 +0930 | [diff] [blame] | 122 | for (const auto& [path, object] : interfaceSubtree) |
| 123 | { |
Andrew Jeffery | 4732183 | 2022-04-05 16:30:29 +0930 | [diff] [blame] | 124 | // Get a PropertiesChanged callback for all interfaces on this path. |
| 125 | registerCallback(scan->_systemConfiguration, scan->objServer, path); |
| 126 | |
Andrew Jeffery | 8d2761e | 2022-04-05 16:26:28 +0930 | [diff] [blame] | 127 | for (const auto& [busname, ifaces] : object) |
| 128 | { |
| 129 | for (const std::string& iface : ifaces) |
| 130 | { |
| 131 | // The 3 default org.freedeskstop interfaces (Peer, |
| 132 | // Introspectable, and Properties) are returned by |
| 133 | // the mapper but don't have properties, so don't bother |
| 134 | // with the GetAll call to save some cycles. |
| 135 | if (!boost::algorithm::starts_with(iface, "org.freedesktop")) |
| 136 | { |
Andrew Jeffery | 4732183 | 2022-04-05 16:30:29 +0930 | [diff] [blame] | 137 | getInterfaces({busname, path, iface}, probeVector, scan); |
Andrew Jeffery | 8d2761e | 2022-04-05 16:26:28 +0930 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | } |
Andrew Jeffery | 8d2761e | 2022-04-05 16:26:28 +0930 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 144 | // Populates scan->dbusProbeObjects with all interfaces and properties |
| 145 | // for the paths that own the interfaces passed in. |
| 146 | void findDbusObjects(std::vector<std::shared_ptr<PerformProbe>>&& probeVector, |
| 147 | boost::container::flat_set<std::string>&& interfaces, |
| 148 | const std::shared_ptr<PerformScan>& scan, |
| 149 | size_t retries = 5) |
| 150 | { |
| 151 | // Filter out interfaces already obtained. |
| 152 | for (const auto& [path, probeInterfaces] : scan->dbusProbeObjects) |
| 153 | { |
| 154 | for (const auto& [interface, _] : probeInterfaces) |
| 155 | { |
| 156 | interfaces.erase(interface); |
| 157 | } |
| 158 | } |
| 159 | if (interfaces.empty()) |
| 160 | { |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | // find all connections in the mapper that expose a specific type |
| 165 | systemBus->async_method_call( |
| 166 | [interfaces, probeVector{std::move(probeVector)}, scan, |
| 167 | retries](boost::system::error_code& ec, |
| 168 | const GetSubTreeType& interfaceSubtree) mutable { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame^] | 169 | if (ec) |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 170 | { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame^] | 171 | if (ec.value() == ENOENT) |
| 172 | { |
| 173 | return; // wasn't found by mapper |
| 174 | } |
| 175 | std::cerr << "Error communicating to mapper.\n"; |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 176 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame^] | 177 | if (retries == 0U) |
| 178 | { |
| 179 | // if we can't communicate to the mapper something is very |
| 180 | // wrong |
| 181 | std::exit(EXIT_FAILURE); |
| 182 | } |
| 183 | |
| 184 | auto timer = std::make_shared<boost::asio::steady_timer>(io); |
| 185 | timer->expires_after(std::chrono::seconds(10)); |
| 186 | |
| 187 | timer->async_wait( |
| 188 | [timer, interfaces{std::move(interfaces)}, scan, |
| 189 | probeVector{std::move(probeVector)}, |
| 190 | retries](const boost::system::error_code&) mutable { |
| 191 | findDbusObjects(std::move(probeVector), |
| 192 | std::move(interfaces), scan, |
| 193 | retries - 1); |
| 194 | }); |
| 195 | return; |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 196 | } |
| 197 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame^] | 198 | processDbusObjects(probeVector, scan, interfaceSubtree); |
| 199 | }, |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 200 | "xyz.openbmc_project.ObjectMapper", |
| 201 | "/xyz/openbmc_project/object_mapper", |
| 202 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", maxMapperDepth, |
| 203 | interfaces); |
| 204 | |
| 205 | if constexpr (debug) |
| 206 | { |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 207 | std::cerr << __LINE__ << "\n"; |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
Andrew Jeffery | 09a09a6 | 2022-04-12 22:49:57 +0930 | [diff] [blame] | 211 | static std::string getRecordName(const DBusInterface& probe, |
| 212 | const std::string& probeName) |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 213 | { |
| 214 | if (probe.empty()) |
| 215 | { |
| 216 | return probeName; |
| 217 | } |
| 218 | |
Andrew Jeffery | 928c5b2 | 2022-04-05 16:57:51 +0930 | [diff] [blame] | 219 | // use an array so alphabetical order from the flat_map is maintained |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 220 | auto device = nlohmann::json::array(); |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 221 | for (const auto& devPair : probe) |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 222 | { |
| 223 | device.push_back(devPair.first); |
| 224 | std::visit([&device](auto&& v) { device.push_back(v); }, |
| 225 | devPair.second); |
| 226 | } |
Andrew Jeffery | 8650187 | 2022-04-05 16:58:22 +0930 | [diff] [blame] | 227 | |
Andrew Jeffery | 928c5b2 | 2022-04-05 16:57:51 +0930 | [diff] [blame] | 228 | // hashes are hard to distinguish, use the non-hashed version if we want |
| 229 | // debug |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 230 | if constexpr (debug) |
| 231 | { |
| 232 | return probeName + device.dump(); |
| 233 | } |
Andrew Jeffery | 1ae4ed6 | 2022-04-05 16:55:43 +0930 | [diff] [blame] | 234 | |
Andrew Jeffery | 8650187 | 2022-04-05 16:58:22 +0930 | [diff] [blame] | 235 | return std::to_string(std::hash<std::string>{}(probeName + device.dump())); |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | PerformScan::PerformScan(nlohmann::json& systemConfiguration, |
| 239 | nlohmann::json& missingConfigurations, |
| 240 | std::list<nlohmann::json>& configurations, |
| 241 | sdbusplus::asio::object_server& objServerIn, |
| 242 | std::function<void()>&& callback) : |
| 243 | _systemConfiguration(systemConfiguration), |
| 244 | _missingConfigurations(missingConfigurations), |
| 245 | _configurations(configurations), objServer(objServerIn), |
| 246 | _callback(std::move(callback)) |
| 247 | {} |
Andrew Jeffery | db45148 | 2022-04-13 16:53:22 +0930 | [diff] [blame] | 248 | |
Andrew Jeffery | 7a7faed | 2022-04-13 17:24:56 +0930 | [diff] [blame] | 249 | static void pruneRecordExposes(nlohmann::json& record) |
Andrew Jeffery | db45148 | 2022-04-13 16:53:22 +0930 | [diff] [blame] | 250 | { |
Andrew Jeffery | 7a7faed | 2022-04-13 17:24:56 +0930 | [diff] [blame] | 251 | auto findExposes = record.find("Exposes"); |
Andrew Jeffery | 5f05145 | 2022-04-13 17:11:37 +0930 | [diff] [blame] | 252 | if (findExposes == record.end()) |
Andrew Jeffery | db45148 | 2022-04-13 16:53:22 +0930 | [diff] [blame] | 253 | { |
Andrew Jeffery | 5f05145 | 2022-04-13 17:11:37 +0930 | [diff] [blame] | 254 | return; |
Andrew Jeffery | db45148 | 2022-04-13 16:53:22 +0930 | [diff] [blame] | 255 | } |
Andrew Jeffery | 5f05145 | 2022-04-13 17:11:37 +0930 | [diff] [blame] | 256 | |
| 257 | auto copy = nlohmann::json::array(); |
| 258 | for (auto& expose : *findExposes) |
| 259 | { |
Andrew Jeffery | 742a7eb | 2022-04-13 17:14:46 +0930 | [diff] [blame] | 260 | if (!expose.is_null()) |
Andrew Jeffery | 5f05145 | 2022-04-13 17:11:37 +0930 | [diff] [blame] | 261 | { |
Andrew Jeffery | 742a7eb | 2022-04-13 17:14:46 +0930 | [diff] [blame] | 262 | copy.emplace_back(expose); |
Andrew Jeffery | 5f05145 | 2022-04-13 17:11:37 +0930 | [diff] [blame] | 263 | } |
Andrew Jeffery | 5f05145 | 2022-04-13 17:11:37 +0930 | [diff] [blame] | 264 | } |
| 265 | *findExposes = copy; |
Andrew Jeffery | db45148 | 2022-04-13 16:53:22 +0930 | [diff] [blame] | 266 | } |
| 267 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame^] | 268 | static void recordDiscoveredIdentifiers( |
| 269 | std::set<nlohmann::json>& usedNames, std::list<size_t>& indexes, |
| 270 | const std::string& probeName, const nlohmann::json& record) |
Andrew Jeffery | 6addc02 | 2022-04-13 18:08:58 +0930 | [diff] [blame] | 271 | { |
| 272 | size_t indexIdx = probeName.find('$'); |
Andrew Jeffery | ba41b62 | 2022-04-13 18:13:28 +0930 | [diff] [blame] | 273 | if (indexIdx == std::string::npos) |
Andrew Jeffery | 6addc02 | 2022-04-13 18:08:58 +0930 | [diff] [blame] | 274 | { |
| 275 | return; |
| 276 | } |
| 277 | |
Andrew Jeffery | c0da0cc | 2022-04-13 18:15:09 +0930 | [diff] [blame] | 278 | auto nameIt = record.find("Name"); |
| 279 | if (nameIt == record.end()) |
Andrew Jeffery | 6addc02 | 2022-04-13 18:08:58 +0930 | [diff] [blame] | 280 | { |
| 281 | std::cerr << "Last JSON Illegal\n"; |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | int index = 0; |
| 286 | auto str = nameIt->get<std::string>().substr(indexIdx); |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 287 | // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) |
| 288 | const char* endPtr = str.data() + str.size(); |
| 289 | auto [p, ec] = std::from_chars(str.data(), endPtr, index); |
Andrew Jeffery | 6addc02 | 2022-04-13 18:08:58 +0930 | [diff] [blame] | 290 | if (ec != std::errc()) |
| 291 | { |
| 292 | return; // non-numeric replacement |
| 293 | } |
| 294 | |
| 295 | usedNames.insert(nameIt.value()); |
| 296 | |
| 297 | auto usedIt = std::find(indexes.begin(), indexes.end(), index); |
| 298 | if (usedIt != indexes.end()) |
| 299 | { |
| 300 | indexes.erase(usedIt); |
| 301 | } |
| 302 | } |
| 303 | |
Andrew Jeffery | 6890ae2 | 2022-04-14 15:33:01 +0930 | [diff] [blame] | 304 | static bool extractExposeActionRecordNames(std::vector<std::string>& matches, |
| 305 | nlohmann::json::iterator& keyPair) |
| 306 | { |
Andrew Jeffery | a244984 | 2022-04-14 15:35:51 +0930 | [diff] [blame] | 307 | if (keyPair.value().is_string()) |
Andrew Jeffery | 6890ae2 | 2022-04-14 15:33:01 +0930 | [diff] [blame] | 308 | { |
| 309 | matches.emplace_back(keyPair.value()); |
Andrew Jeffery | ba3c50c | 2022-04-14 15:34:38 +0930 | [diff] [blame] | 310 | return true; |
Andrew Jeffery | 6890ae2 | 2022-04-14 15:33:01 +0930 | [diff] [blame] | 311 | } |
Andrew Jeffery | ba3c50c | 2022-04-14 15:34:38 +0930 | [diff] [blame] | 312 | |
Andrew Jeffery | a244984 | 2022-04-14 15:35:51 +0930 | [diff] [blame] | 313 | if (keyPair.value().is_array()) |
Andrew Jeffery | 6890ae2 | 2022-04-14 15:33:01 +0930 | [diff] [blame] | 314 | { |
| 315 | for (const auto& value : keyPair.value()) |
| 316 | { |
Andrew Jeffery | a244984 | 2022-04-14 15:35:51 +0930 | [diff] [blame] | 317 | if (!value.is_string()) |
Andrew Jeffery | 6890ae2 | 2022-04-14 15:33:01 +0930 | [diff] [blame] | 318 | { |
| 319 | std::cerr << "Value is invalid type " << value << "\n"; |
| 320 | break; |
| 321 | } |
| 322 | matches.emplace_back(value); |
| 323 | } |
Andrew Jeffery | 6890ae2 | 2022-04-14 15:33:01 +0930 | [diff] [blame] | 324 | |
Andrew Jeffery | ba3c50c | 2022-04-14 15:34:38 +0930 | [diff] [blame] | 325 | return true; |
Andrew Jeffery | 6890ae2 | 2022-04-14 15:33:01 +0930 | [diff] [blame] | 326 | } |
| 327 | |
Andrew Jeffery | ba3c50c | 2022-04-14 15:34:38 +0930 | [diff] [blame] | 328 | std::cerr << "Value is invalid type " << keyPair.key() << "\n"; |
| 329 | |
| 330 | return false; |
Andrew Jeffery | 6890ae2 | 2022-04-14 15:33:01 +0930 | [diff] [blame] | 331 | } |
| 332 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame^] | 333 | static std::optional<std::vector<std::string>::iterator> findExposeActionRecord( |
| 334 | std::vector<std::string>& matches, const nlohmann::json& record) |
Andrew Jeffery | 6de53fb | 2022-04-14 15:45:38 +0930 | [diff] [blame] | 335 | { |
Andrew Jeffery | 1a02da8 | 2022-04-14 20:39:06 +0930 | [diff] [blame] | 336 | const auto& name = (record)["Name"].get_ref<const std::string&>(); |
| 337 | auto compare = [&name](const std::string& s) { return s == name; }; |
| 338 | auto matchIt = std::find_if(matches.begin(), matches.end(), compare); |
Andrew Jeffery | 6de53fb | 2022-04-14 15:45:38 +0930 | [diff] [blame] | 339 | |
| 340 | if (matchIt == matches.end()) |
| 341 | { |
| 342 | return std::nullopt; |
| 343 | } |
| 344 | |
| 345 | return matchIt; |
| 346 | } |
| 347 | |
Andrew Jeffery | cf11bd3 | 2022-04-14 18:34:31 +0930 | [diff] [blame] | 348 | static void applyBindExposeAction(nlohmann::json& exposedObject, |
| 349 | nlohmann::json& expose, |
| 350 | const std::string& propertyName) |
| 351 | { |
| 352 | if (boost::starts_with(propertyName, "Bind")) |
| 353 | { |
| 354 | std::string bind = propertyName.substr(sizeof("Bind") - 1); |
| 355 | exposedObject["Status"] = "okay"; |
| 356 | expose[bind] = exposedObject; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | static void applyDisableExposeAction(nlohmann::json& exposedObject, |
| 361 | const std::string& propertyName) |
| 362 | { |
| 363 | if (propertyName == "DisableNode") |
| 364 | { |
| 365 | exposedObject["Status"] = "disabled"; |
| 366 | } |
| 367 | } |
| 368 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame^] | 369 | static void applyConfigExposeActions( |
| 370 | std::vector<std::string>& matches, nlohmann::json& expose, |
| 371 | const std::string& propertyName, nlohmann::json& configExposes) |
Andrew Jeffery | 5468f8e | 2022-04-14 21:08:31 +0930 | [diff] [blame] | 372 | { |
Andrew Jeffery | da45e5e | 2022-04-14 21:12:02 +0930 | [diff] [blame] | 373 | for (auto& exposedObject : configExposes) |
Andrew Jeffery | 5468f8e | 2022-04-14 21:08:31 +0930 | [diff] [blame] | 374 | { |
| 375 | auto match = findExposeActionRecord(matches, exposedObject); |
Andrew Jeffery | 060ac9c | 2022-04-14 21:11:18 +0930 | [diff] [blame] | 376 | if (match) |
Andrew Jeffery | 5468f8e | 2022-04-14 21:08:31 +0930 | [diff] [blame] | 377 | { |
Andrew Jeffery | 060ac9c | 2022-04-14 21:11:18 +0930 | [diff] [blame] | 378 | matches.erase(*match); |
| 379 | applyBindExposeAction(exposedObject, expose, propertyName); |
| 380 | applyDisableExposeAction(exposedObject, propertyName); |
Andrew Jeffery | 5468f8e | 2022-04-14 21:08:31 +0930 | [diff] [blame] | 381 | } |
Andrew Jeffery | 5468f8e | 2022-04-14 21:08:31 +0930 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame^] | 385 | static void applyExposeActions( |
| 386 | nlohmann::json& systemConfiguration, const std::string& recordName, |
| 387 | nlohmann::json& expose, nlohmann::json::iterator& keyPair) |
Andrew Jeffery | b48779d | 2022-04-14 21:40:31 +0930 | [diff] [blame] | 388 | { |
| 389 | bool isBind = boost::starts_with(keyPair.key(), "Bind"); |
| 390 | bool isDisable = keyPair.key() == "DisableNode"; |
| 391 | bool isExposeAction = isBind || isDisable; |
| 392 | |
| 393 | if (!isExposeAction) |
| 394 | { |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | std::vector<std::string> matches; |
| 399 | |
| 400 | if (!extractExposeActionRecordNames(matches, keyPair)) |
| 401 | { |
| 402 | return; |
| 403 | } |
| 404 | |
Patrick Williams | 2594d36 | 2022-09-28 06:46:24 -0500 | [diff] [blame] | 405 | for (const auto& [configId, config] : systemConfiguration.items()) |
Andrew Jeffery | b48779d | 2022-04-14 21:40:31 +0930 | [diff] [blame] | 406 | { |
| 407 | // don't disable ourselves |
| 408 | if (isDisable && configId == recordName) |
| 409 | { |
| 410 | continue; |
| 411 | } |
| 412 | |
| 413 | auto configListFind = config.find("Exposes"); |
| 414 | if (configListFind == config.end()) |
| 415 | { |
| 416 | continue; |
| 417 | } |
| 418 | |
| 419 | if (!configListFind->is_array()) |
| 420 | { |
| 421 | continue; |
| 422 | } |
| 423 | |
| 424 | applyConfigExposeActions(matches, expose, keyPair.key(), |
| 425 | *configListFind); |
| 426 | } |
| 427 | |
| 428 | if (!matches.empty()) |
| 429 | { |
| 430 | std::cerr << "configuration file dependency error, could not find " |
| 431 | << keyPair.key() << " " << keyPair.value() << "\n"; |
| 432 | } |
| 433 | } |
| 434 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame^] | 435 | static std::string generateDeviceName( |
| 436 | const std::set<nlohmann::json>& usedNames, const DBusObject& dbusObject, |
| 437 | size_t foundDeviceIdx, const std::string& nameTemplate, |
| 438 | std::optional<std::string>& replaceStr) |
Andrew Jeffery | dabee98 | 2022-04-19 13:27:24 +0930 | [diff] [blame] | 439 | { |
| 440 | nlohmann::json copyForName = {{"Name", nameTemplate}}; |
| 441 | nlohmann::json::iterator copyIt = copyForName.begin(); |
| 442 | std::optional<std::string> replaceVal = |
| 443 | templateCharReplace(copyIt, dbusObject, foundDeviceIdx, replaceStr); |
| 444 | |
| 445 | if (!replaceStr && replaceVal) |
| 446 | { |
| 447 | if (usedNames.find(copyIt.value()) != usedNames.end()) |
| 448 | { |
| 449 | replaceStr = replaceVal; |
| 450 | copyForName = {{"Name", nameTemplate}}; |
| 451 | copyIt = copyForName.begin(); |
| 452 | templateCharReplace(copyIt, dbusObject, foundDeviceIdx, replaceStr); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | if (replaceStr) |
| 457 | { |
| 458 | std::cerr << "Duplicates found, replacing " << *replaceStr |
| 459 | << " with found device index.\n Consider " |
| 460 | "fixing template to not have duplicates\n"; |
| 461 | } |
| 462 | |
| 463 | return copyIt.value(); |
| 464 | } |
| 465 | |
Andrew Jeffery | af9b46b | 2022-04-21 12:34:43 +0930 | [diff] [blame] | 466 | void PerformScan::updateSystemConfiguration(const nlohmann::json& recordRef, |
| 467 | const std::string& probeName, |
| 468 | FoundDevices& foundDevices) |
Andrew Jeffery | ae6c1a4 | 2022-04-19 15:44:42 +0930 | [diff] [blame] | 469 | { |
| 470 | _passed = true; |
| 471 | passedProbes.push_back(probeName); |
| 472 | |
| 473 | std::set<nlohmann::json> usedNames; |
| 474 | std::list<size_t> indexes(foundDevices.size()); |
| 475 | std::iota(indexes.begin(), indexes.end(), 1); |
| 476 | |
| 477 | // copy over persisted configurations and make sure we remove |
| 478 | // indexes that are already used |
| 479 | for (auto itr = foundDevices.begin(); itr != foundDevices.end();) |
| 480 | { |
| 481 | std::string recordName = getRecordName(itr->interface, probeName); |
| 482 | |
Zhikui Ren | 1f77d9c | 2022-08-29 16:02:37 -0700 | [diff] [blame] | 483 | auto record = _systemConfiguration.find(recordName); |
| 484 | if (record == _systemConfiguration.end()) |
Andrew Jeffery | ae6c1a4 | 2022-04-19 15:44:42 +0930 | [diff] [blame] | 485 | { |
Zhikui Ren | 1f77d9c | 2022-08-29 16:02:37 -0700 | [diff] [blame] | 486 | record = lastJson.find(recordName); |
| 487 | if (record == lastJson.end()) |
| 488 | { |
| 489 | itr++; |
| 490 | continue; |
| 491 | } |
| 492 | |
| 493 | pruneRecordExposes(*record); |
| 494 | |
Zhikui Ren | 1f77d9c | 2022-08-29 16:02:37 -0700 | [diff] [blame] | 495 | _systemConfiguration[recordName] = *record; |
Andrew Jeffery | ae6c1a4 | 2022-04-19 15:44:42 +0930 | [diff] [blame] | 496 | } |
Andrew Jeffery | ae6c1a4 | 2022-04-19 15:44:42 +0930 | [diff] [blame] | 497 | _missingConfigurations.erase(recordName); |
| 498 | |
| 499 | // We've processed the device, remove it and advance the |
| 500 | // iterator |
| 501 | itr = foundDevices.erase(itr); |
JinFuLin | 8f05a3a | 2022-11-21 15:33:24 +0800 | [diff] [blame] | 502 | recordDiscoveredIdentifiers(usedNames, indexes, probeName, *record); |
Andrew Jeffery | ae6c1a4 | 2022-04-19 15:44:42 +0930 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | std::optional<std::string> replaceStr; |
| 506 | |
| 507 | DBusObject emptyObject; |
| 508 | DBusInterface emptyInterface; |
| 509 | emptyObject.emplace(std::string{}, emptyInterface); |
| 510 | |
| 511 | for (const auto& [foundDevice, path] : foundDevices) |
| 512 | { |
| 513 | // Need all interfaces on this path so that template |
| 514 | // substitutions can be done with any of the contained |
| 515 | // properties. If the probe that passed didn't use an |
| 516 | // interface, such as if it was just TRUE, then |
| 517 | // templateCharReplace will just get passed in an empty |
| 518 | // map. |
Andrew Jeffery | af9b46b | 2022-04-21 12:34:43 +0930 | [diff] [blame] | 519 | auto objectIt = dbusProbeObjects.find(path); |
| 520 | const DBusObject& dbusObject = (objectIt == dbusProbeObjects.end()) |
| 521 | ? emptyObject |
| 522 | : objectIt->second; |
Andrew Jeffery | ae6c1a4 | 2022-04-19 15:44:42 +0930 | [diff] [blame] | 523 | |
| 524 | nlohmann::json record = recordRef; |
| 525 | std::string recordName = getRecordName(foundDevice, probeName); |
| 526 | size_t foundDeviceIdx = indexes.front(); |
| 527 | indexes.pop_front(); |
| 528 | |
| 529 | // check name first so we have no duplicate names |
| 530 | auto getName = record.find("Name"); |
| 531 | if (getName == record.end()) |
| 532 | { |
| 533 | std::cerr << "Record Missing Name! " << record.dump(); |
| 534 | continue; // this should be impossible at this level |
| 535 | } |
| 536 | |
| 537 | std::string deviceName = generateDeviceName( |
| 538 | usedNames, dbusObject, foundDeviceIdx, getName.value(), replaceStr); |
| 539 | getName.value() = deviceName; |
| 540 | usedNames.insert(deviceName); |
| 541 | |
| 542 | for (auto keyPair = record.begin(); keyPair != record.end(); keyPair++) |
| 543 | { |
| 544 | if (keyPair.key() != "Name") |
| 545 | { |
| 546 | templateCharReplace(keyPair, dbusObject, foundDeviceIdx, |
| 547 | replaceStr); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | // insert into configuration temporarily to be able to |
| 552 | // reference ourselves |
| 553 | |
| 554 | _systemConfiguration[recordName] = record; |
| 555 | |
| 556 | auto findExpose = record.find("Exposes"); |
| 557 | if (findExpose == record.end()) |
| 558 | { |
| 559 | continue; |
| 560 | } |
| 561 | |
| 562 | for (auto& expose : *findExpose) |
| 563 | { |
| 564 | for (auto keyPair = expose.begin(); keyPair != expose.end(); |
| 565 | keyPair++) |
| 566 | { |
Andrew Jeffery | ae6c1a4 | 2022-04-19 15:44:42 +0930 | [diff] [blame] | 567 | templateCharReplace(keyPair, dbusObject, foundDeviceIdx, |
| 568 | replaceStr); |
| 569 | |
| 570 | applyExposeActions(_systemConfiguration, recordName, expose, |
| 571 | keyPair); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | // overwrite ourselves with cleaned up version |
| 576 | _systemConfiguration[recordName] = record; |
| 577 | _missingConfigurations.erase(recordName); |
| 578 | } |
| 579 | } |
| 580 | |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 581 | void PerformScan::run() |
| 582 | { |
| 583 | boost::container::flat_set<std::string> dbusProbeInterfaces; |
| 584 | std::vector<std::shared_ptr<PerformProbe>> dbusProbePointers; |
| 585 | |
| 586 | for (auto it = _configurations.begin(); it != _configurations.end();) |
| 587 | { |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 588 | // check for poorly formatted fields, probe must be an array |
Andrew Jeffery | 3883487 | 2022-04-19 15:15:57 +0930 | [diff] [blame] | 589 | auto findProbe = it->find("Probe"); |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 590 | if (findProbe == it->end()) |
| 591 | { |
| 592 | std::cerr << "configuration file missing probe:\n " << *it << "\n"; |
| 593 | it = _configurations.erase(it); |
| 594 | continue; |
| 595 | } |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 596 | |
Andrew Jeffery | 3883487 | 2022-04-19 15:15:57 +0930 | [diff] [blame] | 597 | auto findName = it->find("Name"); |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 598 | if (findName == it->end()) |
| 599 | { |
| 600 | std::cerr << "configuration file missing name:\n " << *it << "\n"; |
| 601 | it = _configurations.erase(it); |
| 602 | continue; |
| 603 | } |
| 604 | std::string probeName = *findName; |
| 605 | |
| 606 | if (std::find(passedProbes.begin(), passedProbes.end(), probeName) != |
| 607 | passedProbes.end()) |
| 608 | { |
| 609 | it = _configurations.erase(it); |
| 610 | continue; |
| 611 | } |
Andrew Jeffery | 3883487 | 2022-04-19 15:15:57 +0930 | [diff] [blame] | 612 | |
Andrew Jeffery | 98f3d53 | 2022-04-19 15:29:22 +0930 | [diff] [blame] | 613 | nlohmann::json& recordRef = *it; |
Andrew Jeffery | 3883487 | 2022-04-19 15:15:57 +0930 | [diff] [blame] | 614 | nlohmann::json probeCommand; |
| 615 | if ((*findProbe).type() != nlohmann::json::value_t::array) |
| 616 | { |
| 617 | probeCommand = nlohmann::json::array(); |
| 618 | probeCommand.push_back(*findProbe); |
| 619 | } |
| 620 | else |
| 621 | { |
| 622 | probeCommand = *findProbe; |
| 623 | } |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 624 | |
| 625 | // store reference to this to children to makes sure we don't get |
| 626 | // destroyed too early |
| 627 | auto thisRef = shared_from_this(); |
| 628 | auto probePointer = std::make_shared<PerformProbe>( |
Andrew Jeffery | ea4ff02 | 2022-04-21 12:31:40 +0930 | [diff] [blame] | 629 | recordRef, probeCommand, probeName, thisRef); |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 630 | |
| 631 | // parse out dbus probes by discarding other probe types, store in a |
| 632 | // map |
| 633 | for (const nlohmann::json& probeJson : probeCommand) |
| 634 | { |
| 635 | const std::string* probe = probeJson.get_ptr<const std::string*>(); |
| 636 | if (probe == nullptr) |
| 637 | { |
| 638 | std::cerr << "Probe statement wasn't a string, can't parse"; |
| 639 | continue; |
| 640 | } |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 641 | if (findProbeType(*probe)) |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 642 | { |
| 643 | continue; |
| 644 | } |
| 645 | // syntax requires probe before first open brace |
| 646 | auto findStart = probe->find('('); |
| 647 | std::string interface = probe->substr(0, findStart); |
| 648 | dbusProbeInterfaces.emplace(interface); |
| 649 | dbusProbePointers.emplace_back(probePointer); |
| 650 | } |
| 651 | it++; |
| 652 | } |
| 653 | |
| 654 | // probe vector stores a shared_ptr to each PerformProbe that cares |
| 655 | // about a dbus interface |
| 656 | findDbusObjects(std::move(dbusProbePointers), |
| 657 | std::move(dbusProbeInterfaces), shared_from_this()); |
| 658 | if constexpr (debug) |
| 659 | { |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 660 | std::cerr << __LINE__ << "\n"; |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | |
| 664 | PerformScan::~PerformScan() |
| 665 | { |
| 666 | if (_passed) |
| 667 | { |
| 668 | auto nextScan = std::make_shared<PerformScan>( |
| 669 | _systemConfiguration, _missingConfigurations, _configurations, |
| 670 | objServer, std::move(_callback)); |
| 671 | nextScan->passedProbes = std::move(passedProbes); |
| 672 | nextScan->dbusProbeObjects = std::move(dbusProbeObjects); |
| 673 | nextScan->run(); |
| 674 | |
| 675 | if constexpr (debug) |
| 676 | { |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 677 | std::cerr << __LINE__ << "\n"; |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | else |
| 681 | { |
| 682 | _callback(); |
| 683 | |
| 684 | if constexpr (debug) |
| 685 | { |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 686 | std::cerr << __LINE__ << "\n"; |
Andrew Jeffery | 47af65a | 2021-12-01 14:16:31 +1030 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | } |