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