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