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