blob: c71fa487b400a5ea03f794e00080293d84b05f73 [file] [log] [blame]
Alexander Hansen4e1142d2025-07-25 17:07:27 +02001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
3
Christopher Meis26fbbd52025-03-26 14:55:06 +01004#include "perform_scan.hpp"
5
Christopher Meis26fbbd52025-03-26 14:55:06 +01006#include "perform_probe.hpp"
Christopher Meis59ef1e72025-04-16 08:53:25 +02007#include "utils.hpp"
Andrew Jeffery47af65a2021-12-01 14:16:31 +10308
Andrew Jeffery47af65a2021-12-01 14:16:31 +10309#include <boost/asio/steady_timer.hpp>
10#include <boost/container/flat_map.hpp>
11#include <boost/container/flat_set.hpp>
Alexander Hansenc3db2c32024-08-20 15:01:38 +020012#include <phosphor-logging/lg2.hpp>
Andrew Jeffery47af65a2021-12-01 14:16:31 +103013
14#include <charconv>
Christopher Meis59ef1e72025-04-16 08:53:25 +020015#include <iostream>
Andrew Jeffery47af65a2021-12-01 14:16:31 +103016
Andrew Jeffery47af65a2021-12-01 14:16:31 +103017using GetSubTreeType = std::vector<
18 std::pair<std::string,
19 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
20
21constexpr const int32_t maxMapperDepth = 0;
22
Andrew Jefferyd9b67842022-04-05 16:44:20 +093023struct DBusInterfaceInstance
24{
25 std::string busName;
26 std::string path;
27 std::string interface;
28};
29
Patrick Williams5a807032025-03-03 11:20:39 -050030void getInterfaces(
31 const DBusInterfaceInstance& instance,
Christopher Meis26fbbd52025-03-26 14:55:06 +010032 const std::vector<std::shared_ptr<probe::PerformProbe>>& probeVector,
Alexander Hansena555acf2025-06-27 11:59:10 +020033 const std::shared_ptr<scan::PerformScan>& scan, boost::asio::io_context& io,
34 size_t retries = 5)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103035{
Ed Tanous3013fb42022-07-09 08:27:06 -070036 if (retries == 0U)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103037 {
Andrew Jefferyd9b67842022-04-05 16:44:20 +093038 std::cerr << "retries exhausted on " << instance.busName << " "
39 << instance.path << " " << instance.interface << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +103040 return;
41 }
42
Christopher Meiscf6a75b2025-06-03 07:53:50 +020043 scan->_em.systemBus->async_method_call(
Alexander Hansena555acf2025-06-27 11:59:10 +020044 [instance, scan, probeVector, retries,
45 &io](boost::system::error_code& errc, const DBusInterface& resp) {
Patrick Williamsb7077432024-08-16 15:22:21 -040046 if (errc)
47 {
48 std::cerr << "error calling getall on " << instance.busName
49 << " " << instance.path << " "
50 << instance.interface << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +103051
Patrick Williamsb7077432024-08-16 15:22:21 -040052 auto timer = std::make_shared<boost::asio::steady_timer>(io);
53 timer->expires_after(std::chrono::seconds(2));
Andrew Jeffery47af65a2021-12-01 14:16:31 +103054
Alexander Hansena555acf2025-06-27 11:59:10 +020055 timer->async_wait([timer, instance, scan, probeVector, retries,
56 &io](const boost::system::error_code&) {
57 getInterfaces(instance, probeVector, scan, io, retries - 1);
Patrick Williamsb7077432024-08-16 15:22:21 -040058 });
59 return;
60 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +103061
Patrick Williamsb7077432024-08-16 15:22:21 -040062 scan->dbusProbeObjects[instance.path][instance.interface] = resp;
63 },
Andrew Jefferyd9b67842022-04-05 16:44:20 +093064 instance.busName, instance.path, "org.freedesktop.DBus.Properties",
65 "GetAll", instance.interface);
Andrew Jeffery47af65a2021-12-01 14:16:31 +103066}
67
Patrick Williams5a807032025-03-03 11:20:39 -050068static void processDbusObjects(
Christopher Meis26fbbd52025-03-26 14:55:06 +010069 std::vector<std::shared_ptr<probe::PerformProbe>>& probeVector,
70 const std::shared_ptr<scan::PerformScan>& scan,
Alexander Hansena555acf2025-06-27 11:59:10 +020071 const GetSubTreeType& interfaceSubtree, boost::asio::io_context& io)
Andrew Jeffery8d2761e2022-04-05 16:26:28 +093072{
Andrew Jeffery8d2761e2022-04-05 16:26:28 +093073 for (const auto& [path, object] : interfaceSubtree)
74 {
Andrew Jeffery47321832022-04-05 16:30:29 +093075 // Get a PropertiesChanged callback for all interfaces on this path.
Christopher Meiscf6a75b2025-06-03 07:53:50 +020076 scan->_em.registerCallback(path);
Andrew Jeffery47321832022-04-05 16:30:29 +093077
Andrew Jeffery8d2761e2022-04-05 16:26:28 +093078 for (const auto& [busname, ifaces] : object)
79 {
80 for (const std::string& iface : ifaces)
81 {
82 // The 3 default org.freedeskstop interfaces (Peer,
83 // Introspectable, and Properties) are returned by
84 // the mapper but don't have properties, so don't bother
85 // with the GetAll call to save some cycles.
George Liu164af2f2025-08-21 17:16:31 +080086 if (!iface.starts_with("org.freedesktop"))
Andrew Jeffery8d2761e2022-04-05 16:26:28 +093087 {
Alexander Hansena555acf2025-06-27 11:59:10 +020088 getInterfaces({busname, path, iface}, probeVector, scan,
89 io);
Andrew Jeffery8d2761e2022-04-05 16:26:28 +093090 }
91 }
92 }
Andrew Jeffery8d2761e2022-04-05 16:26:28 +093093 }
94}
95
Andrew Jeffery47af65a2021-12-01 14:16:31 +103096// Populates scan->dbusProbeObjects with all interfaces and properties
97// for the paths that own the interfaces passed in.
Christopher Meis26fbbd52025-03-26 14:55:06 +010098void findDbusObjects(
99 std::vector<std::shared_ptr<probe::PerformProbe>>&& probeVector,
100 boost::container::flat_set<std::string>&& interfaces,
Alexander Hansena555acf2025-06-27 11:59:10 +0200101 const std::shared_ptr<scan::PerformScan>& scan, boost::asio::io_context& io,
102 size_t retries = 5)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030103{
104 // Filter out interfaces already obtained.
105 for (const auto& [path, probeInterfaces] : scan->dbusProbeObjects)
106 {
107 for (const auto& [interface, _] : probeInterfaces)
108 {
109 interfaces.erase(interface);
110 }
111 }
112 if (interfaces.empty())
113 {
114 return;
115 }
116
117 // find all connections in the mapper that expose a specific type
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200118 scan->_em.systemBus->async_method_call(
Alexander Hansena555acf2025-06-27 11:59:10 +0200119 [interfaces, probeVector{std::move(probeVector)}, scan, retries,
120 &io](boost::system::error_code& ec,
121 const GetSubTreeType& interfaceSubtree) mutable {
Patrick Williamsb7077432024-08-16 15:22:21 -0400122 if (ec)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030123 {
Patrick Williamsb7077432024-08-16 15:22:21 -0400124 if (ec.value() == ENOENT)
125 {
126 return; // wasn't found by mapper
127 }
128 std::cerr << "Error communicating to mapper.\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030129
Patrick Williamsb7077432024-08-16 15:22:21 -0400130 if (retries == 0U)
131 {
132 // if we can't communicate to the mapper something is very
133 // wrong
134 std::exit(EXIT_FAILURE);
135 }
136
137 auto timer = std::make_shared<boost::asio::steady_timer>(io);
138 timer->expires_after(std::chrono::seconds(10));
139
140 timer->async_wait(
141 [timer, interfaces{std::move(interfaces)}, scan,
Alexander Hansena555acf2025-06-27 11:59:10 +0200142 probeVector{std::move(probeVector)}, retries,
143 &io](const boost::system::error_code&) mutable {
Patrick Williamsb7077432024-08-16 15:22:21 -0400144 findDbusObjects(std::move(probeVector),
Alexander Hansena555acf2025-06-27 11:59:10 +0200145 std::move(interfaces), scan, io,
Patrick Williamsb7077432024-08-16 15:22:21 -0400146 retries - 1);
147 });
148 return;
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030149 }
150
Alexander Hansena555acf2025-06-27 11:59:10 +0200151 processDbusObjects(probeVector, scan, interfaceSubtree, io);
Patrick Williamsb7077432024-08-16 15:22:21 -0400152 },
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030153 "xyz.openbmc_project.ObjectMapper",
154 "/xyz/openbmc_project/object_mapper",
155 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", maxMapperDepth,
156 interfaces);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030157}
158
Andrew Jeffery09a09a62022-04-12 22:49:57 +0930159static std::string getRecordName(const DBusInterface& probe,
160 const std::string& probeName)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030161{
162 if (probe.empty())
163 {
164 return probeName;
165 }
166
Andrew Jeffery928c5b22022-04-05 16:57:51 +0930167 // use an array so alphabetical order from the flat_map is maintained
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030168 auto device = nlohmann::json::array();
Ed Tanous3013fb42022-07-09 08:27:06 -0700169 for (const auto& devPair : probe)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030170 {
171 device.push_back(devPair.first);
172 std::visit([&device](auto&& v) { device.push_back(v); },
173 devPair.second);
174 }
Andrew Jeffery86501872022-04-05 16:58:22 +0930175
Andrew Jeffery928c5b22022-04-05 16:57:51 +0930176 // hashes are hard to distinguish, use the non-hashed version if we want
177 // debug
Alexander Hansenc3db2c32024-08-20 15:01:38 +0200178 // return probeName + device.dump();
Andrew Jeffery1ae4ed62022-04-05 16:55:43 +0930179
Andrew Jeffery86501872022-04-05 16:58:22 +0930180 return std::to_string(std::hash<std::string>{}(probeName + device.dump()));
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030181}
182
Alexander Hansena555acf2025-06-27 11:59:10 +0200183scan::PerformScan::PerformScan(
184 EntityManager& em, nlohmann::json& missingConfigurations,
Christopher Meisf7252572025-06-11 13:22:05 +0200185 std::vector<nlohmann::json>& configurations, boost::asio::io_context& io,
Alexander Hansena555acf2025-06-27 11:59:10 +0200186 std::function<void()>&& callback) :
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200187 _em(em), _missingConfigurations(missingConfigurations),
Alexander Hansena555acf2025-06-27 11:59:10 +0200188 _configurations(configurations), _callback(std::move(callback)), io(io)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030189{}
Andrew Jefferydb451482022-04-13 16:53:22 +0930190
Andrew Jeffery7a7faed2022-04-13 17:24:56 +0930191static void pruneRecordExposes(nlohmann::json& record)
Andrew Jefferydb451482022-04-13 16:53:22 +0930192{
Andrew Jeffery7a7faed2022-04-13 17:24:56 +0930193 auto findExposes = record.find("Exposes");
Andrew Jeffery5f051452022-04-13 17:11:37 +0930194 if (findExposes == record.end())
Andrew Jefferydb451482022-04-13 16:53:22 +0930195 {
Andrew Jeffery5f051452022-04-13 17:11:37 +0930196 return;
Andrew Jefferydb451482022-04-13 16:53:22 +0930197 }
Andrew Jeffery5f051452022-04-13 17:11:37 +0930198
199 auto copy = nlohmann::json::array();
200 for (auto& expose : *findExposes)
201 {
Andrew Jeffery742a7eb2022-04-13 17:14:46 +0930202 if (!expose.is_null())
Andrew Jeffery5f051452022-04-13 17:11:37 +0930203 {
Andrew Jeffery742a7eb2022-04-13 17:14:46 +0930204 copy.emplace_back(expose);
Andrew Jeffery5f051452022-04-13 17:11:37 +0930205 }
Andrew Jeffery5f051452022-04-13 17:11:37 +0930206 }
207 *findExposes = copy;
Andrew Jefferydb451482022-04-13 16:53:22 +0930208}
209
Patrick Williamsb7077432024-08-16 15:22:21 -0400210static void recordDiscoveredIdentifiers(
211 std::set<nlohmann::json>& usedNames, std::list<size_t>& indexes,
212 const std::string& probeName, const nlohmann::json& record)
Andrew Jeffery6addc022022-04-13 18:08:58 +0930213{
214 size_t indexIdx = probeName.find('$');
Andrew Jefferyba41b622022-04-13 18:13:28 +0930215 if (indexIdx == std::string::npos)
Andrew Jeffery6addc022022-04-13 18:08:58 +0930216 {
217 return;
218 }
219
Andrew Jefferyc0da0cc2022-04-13 18:15:09 +0930220 auto nameIt = record.find("Name");
221 if (nameIt == record.end())
Andrew Jeffery6addc022022-04-13 18:08:58 +0930222 {
223 std::cerr << "Last JSON Illegal\n";
224 return;
225 }
226
227 int index = 0;
228 auto str = nameIt->get<std::string>().substr(indexIdx);
Ed Tanous3013fb42022-07-09 08:27:06 -0700229 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
230 const char* endPtr = str.data() + str.size();
231 auto [p, ec] = std::from_chars(str.data(), endPtr, index);
Andrew Jeffery6addc022022-04-13 18:08:58 +0930232 if (ec != std::errc())
233 {
234 return; // non-numeric replacement
235 }
236
237 usedNames.insert(nameIt.value());
238
239 auto usedIt = std::find(indexes.begin(), indexes.end(), index);
240 if (usedIt != indexes.end())
241 {
242 indexes.erase(usedIt);
243 }
244}
245
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930246static bool extractExposeActionRecordNames(std::vector<std::string>& matches,
247 nlohmann::json::iterator& keyPair)
248{
Andrew Jefferya2449842022-04-14 15:35:51 +0930249 if (keyPair.value().is_string())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930250 {
251 matches.emplace_back(keyPair.value());
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930252 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930253 }
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930254
Andrew Jefferya2449842022-04-14 15:35:51 +0930255 if (keyPair.value().is_array())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930256 {
257 for (const auto& value : keyPair.value())
258 {
Andrew Jefferya2449842022-04-14 15:35:51 +0930259 if (!value.is_string())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930260 {
261 std::cerr << "Value is invalid type " << value << "\n";
262 break;
263 }
264 matches.emplace_back(value);
265 }
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930266
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930267 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930268 }
269
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930270 std::cerr << "Value is invalid type " << keyPair.key() << "\n";
271
272 return false;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930273}
274
Patrick Williamsb7077432024-08-16 15:22:21 -0400275static std::optional<std::vector<std::string>::iterator> findExposeActionRecord(
276 std::vector<std::string>& matches, const nlohmann::json& record)
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930277{
Andrew Jeffery1a02da82022-04-14 20:39:06 +0930278 const auto& name = (record)["Name"].get_ref<const std::string&>();
279 auto compare = [&name](const std::string& s) { return s == name; };
280 auto matchIt = std::find_if(matches.begin(), matches.end(), compare);
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930281
282 if (matchIt == matches.end())
283 {
284 return std::nullopt;
285 }
286
287 return matchIt;
288}
289
Andrew Jefferycf11bd32022-04-14 18:34:31 +0930290static void applyBindExposeAction(nlohmann::json& exposedObject,
291 nlohmann::json& expose,
292 const std::string& propertyName)
293{
George Liu164af2f2025-08-21 17:16:31 +0800294 if (propertyName.starts_with("Bind"))
Andrew Jefferycf11bd32022-04-14 18:34:31 +0930295 {
296 std::string bind = propertyName.substr(sizeof("Bind") - 1);
297 exposedObject["Status"] = "okay";
298 expose[bind] = exposedObject;
299 }
300}
301
302static void applyDisableExposeAction(nlohmann::json& exposedObject,
303 const std::string& propertyName)
304{
305 if (propertyName == "DisableNode")
306 {
307 exposedObject["Status"] = "disabled";
308 }
309}
310
Patrick Williamsb7077432024-08-16 15:22:21 -0400311static void applyConfigExposeActions(
312 std::vector<std::string>& matches, nlohmann::json& expose,
313 const std::string& propertyName, nlohmann::json& configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930314{
Andrew Jefferyda45e5e2022-04-14 21:12:02 +0930315 for (auto& exposedObject : configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930316 {
317 auto match = findExposeActionRecord(matches, exposedObject);
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930318 if (match)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930319 {
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930320 matches.erase(*match);
321 applyBindExposeAction(exposedObject, expose, propertyName);
322 applyDisableExposeAction(exposedObject, propertyName);
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930323 }
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930324 }
325}
326
Patrick Williamsb7077432024-08-16 15:22:21 -0400327static void applyExposeActions(
328 nlohmann::json& systemConfiguration, const std::string& recordName,
329 nlohmann::json& expose, nlohmann::json::iterator& keyPair)
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930330{
George Liu164af2f2025-08-21 17:16:31 +0800331 bool isBind = keyPair.key().starts_with("Bind");
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930332 bool isDisable = keyPair.key() == "DisableNode";
333 bool isExposeAction = isBind || isDisable;
334
335 if (!isExposeAction)
336 {
337 return;
338 }
339
340 std::vector<std::string> matches;
341
342 if (!extractExposeActionRecordNames(matches, keyPair))
343 {
344 return;
345 }
346
Patrick Williams2594d362022-09-28 06:46:24 -0500347 for (const auto& [configId, config] : systemConfiguration.items())
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930348 {
349 // don't disable ourselves
350 if (isDisable && configId == recordName)
351 {
352 continue;
353 }
354
355 auto configListFind = config.find("Exposes");
356 if (configListFind == config.end())
357 {
358 continue;
359 }
360
361 if (!configListFind->is_array())
362 {
363 continue;
364 }
365
366 applyConfigExposeActions(matches, expose, keyPair.key(),
367 *configListFind);
368 }
369
370 if (!matches.empty())
371 {
372 std::cerr << "configuration file dependency error, could not find "
373 << keyPair.key() << " " << keyPair.value() << "\n";
374 }
375}
376
Patrick Williamsb7077432024-08-16 15:22:21 -0400377static std::string generateDeviceName(
378 const std::set<nlohmann::json>& usedNames, const DBusObject& dbusObject,
379 size_t foundDeviceIdx, const std::string& nameTemplate,
380 std::optional<std::string>& replaceStr)
Andrew Jefferydabee982022-04-19 13:27:24 +0930381{
382 nlohmann::json copyForName = {{"Name", nameTemplate}};
383 nlohmann::json::iterator copyIt = copyForName.begin();
Christopher Meis59ef1e72025-04-16 08:53:25 +0200384 std::optional<std::string> replaceVal = em_utils::templateCharReplace(
385 copyIt, dbusObject, foundDeviceIdx, replaceStr);
Andrew Jefferydabee982022-04-19 13:27:24 +0930386
387 if (!replaceStr && replaceVal)
388 {
Patrick Williams64599932025-05-14 07:24:20 -0400389 if (usedNames.contains(copyIt.value()))
Andrew Jefferydabee982022-04-19 13:27:24 +0930390 {
391 replaceStr = replaceVal;
392 copyForName = {{"Name", nameTemplate}};
393 copyIt = copyForName.begin();
Christopher Meis59ef1e72025-04-16 08:53:25 +0200394 em_utils::templateCharReplace(copyIt, dbusObject, foundDeviceIdx,
395 replaceStr);
Andrew Jefferydabee982022-04-19 13:27:24 +0930396 }
397 }
398
399 if (replaceStr)
400 {
401 std::cerr << "Duplicates found, replacing " << *replaceStr
402 << " with found device index.\n Consider "
403 "fixing template to not have duplicates\n";
404 }
405
406 return copyIt.value();
407}
408
Christopher Meis26fbbd52025-03-26 14:55:06 +0100409void scan::PerformScan::updateSystemConfiguration(
410 const nlohmann::json& recordRef, const std::string& probeName,
411 FoundDevices& foundDevices)
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930412{
413 _passed = true;
414 passedProbes.push_back(probeName);
415
416 std::set<nlohmann::json> usedNames;
417 std::list<size_t> indexes(foundDevices.size());
418 std::iota(indexes.begin(), indexes.end(), 1);
419
420 // copy over persisted configurations and make sure we remove
421 // indexes that are already used
422 for (auto itr = foundDevices.begin(); itr != foundDevices.end();)
423 {
424 std::string recordName = getRecordName(itr->interface, probeName);
425
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200426 auto record = _em.systemConfiguration.find(recordName);
427 if (record == _em.systemConfiguration.end())
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930428 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200429 record = _em.lastJson.find(recordName);
430 if (record == _em.lastJson.end())
Zhikui Ren1f77d9c2022-08-29 16:02:37 -0700431 {
432 itr++;
433 continue;
434 }
435
436 pruneRecordExposes(*record);
437
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200438 _em.systemConfiguration[recordName] = *record;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930439 }
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930440 _missingConfigurations.erase(recordName);
441
442 // We've processed the device, remove it and advance the
443 // iterator
444 itr = foundDevices.erase(itr);
JinFuLin8f05a3a2022-11-21 15:33:24 +0800445 recordDiscoveredIdentifiers(usedNames, indexes, probeName, *record);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930446 }
447
448 std::optional<std::string> replaceStr;
449
450 DBusObject emptyObject;
451 DBusInterface emptyInterface;
452 emptyObject.emplace(std::string{}, emptyInterface);
453
454 for (const auto& [foundDevice, path] : foundDevices)
455 {
456 // Need all interfaces on this path so that template
457 // substitutions can be done with any of the contained
458 // properties. If the probe that passed didn't use an
459 // interface, such as if it was just TRUE, then
460 // templateCharReplace will just get passed in an empty
461 // map.
Andrew Jefferyaf9b46b2022-04-21 12:34:43 +0930462 auto objectIt = dbusProbeObjects.find(path);
463 const DBusObject& dbusObject = (objectIt == dbusProbeObjects.end())
464 ? emptyObject
465 : objectIt->second;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930466
467 nlohmann::json record = recordRef;
468 std::string recordName = getRecordName(foundDevice, probeName);
469 size_t foundDeviceIdx = indexes.front();
470 indexes.pop_front();
471
472 // check name first so we have no duplicate names
473 auto getName = record.find("Name");
474 if (getName == record.end())
475 {
476 std::cerr << "Record Missing Name! " << record.dump();
477 continue; // this should be impossible at this level
478 }
479
480 std::string deviceName = generateDeviceName(
481 usedNames, dbusObject, foundDeviceIdx, getName.value(), replaceStr);
482 getName.value() = deviceName;
483 usedNames.insert(deviceName);
484
485 for (auto keyPair = record.begin(); keyPair != record.end(); keyPair++)
486 {
487 if (keyPair.key() != "Name")
488 {
Christopher Meis59ef1e72025-04-16 08:53:25 +0200489 em_utils::templateCharReplace(keyPair, dbusObject,
490 foundDeviceIdx, replaceStr);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930491 }
492 }
493
494 // insert into configuration temporarily to be able to
495 // reference ourselves
496
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200497 _em.systemConfiguration[recordName] = record;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930498
499 auto findExpose = record.find("Exposes");
500 if (findExpose == record.end())
501 {
502 continue;
503 }
504
505 for (auto& expose : *findExpose)
506 {
507 for (auto keyPair = expose.begin(); keyPair != expose.end();
508 keyPair++)
509 {
Christopher Meis59ef1e72025-04-16 08:53:25 +0200510 em_utils::templateCharReplace(keyPair, dbusObject,
511 foundDeviceIdx, replaceStr);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930512
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200513 applyExposeActions(_em.systemConfiguration, recordName, expose,
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930514 keyPair);
515 }
516 }
517
518 // overwrite ourselves with cleaned up version
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200519 _em.systemConfiguration[recordName] = record;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930520 _missingConfigurations.erase(recordName);
521 }
522}
523
Christopher Meis26fbbd52025-03-26 14:55:06 +0100524void scan::PerformScan::run()
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030525{
526 boost::container::flat_set<std::string> dbusProbeInterfaces;
Christopher Meis26fbbd52025-03-26 14:55:06 +0100527 std::vector<std::shared_ptr<probe::PerformProbe>> dbusProbePointers;
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030528
529 for (auto it = _configurations.begin(); it != _configurations.end();)
530 {
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030531 // check for poorly formatted fields, probe must be an array
Andrew Jeffery38834872022-04-19 15:15:57 +0930532 auto findProbe = it->find("Probe");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030533 if (findProbe == it->end())
534 {
535 std::cerr << "configuration file missing probe:\n " << *it << "\n";
536 it = _configurations.erase(it);
537 continue;
538 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030539
Andrew Jeffery38834872022-04-19 15:15:57 +0930540 auto findName = it->find("Name");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030541 if (findName == it->end())
542 {
543 std::cerr << "configuration file missing name:\n " << *it << "\n";
544 it = _configurations.erase(it);
545 continue;
546 }
547 std::string probeName = *findName;
548
549 if (std::find(passedProbes.begin(), passedProbes.end(), probeName) !=
550 passedProbes.end())
551 {
552 it = _configurations.erase(it);
553 continue;
554 }
Andrew Jeffery38834872022-04-19 15:15:57 +0930555
Andrew Jeffery98f3d532022-04-19 15:29:22 +0930556 nlohmann::json& recordRef = *it;
Andrew Jeffery38834872022-04-19 15:15:57 +0930557 nlohmann::json probeCommand;
558 if ((*findProbe).type() != nlohmann::json::value_t::array)
559 {
560 probeCommand = nlohmann::json::array();
561 probeCommand.push_back(*findProbe);
562 }
563 else
564 {
565 probeCommand = *findProbe;
566 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030567
568 // store reference to this to children to makes sure we don't get
569 // destroyed too early
570 auto thisRef = shared_from_this();
Christopher Meis26fbbd52025-03-26 14:55:06 +0100571 auto probePointer = std::make_shared<probe::PerformProbe>(
Andrew Jefferyea4ff022022-04-21 12:31:40 +0930572 recordRef, probeCommand, probeName, thisRef);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030573
574 // parse out dbus probes by discarding other probe types, store in a
575 // map
576 for (const nlohmann::json& probeJson : probeCommand)
577 {
578 const std::string* probe = probeJson.get_ptr<const std::string*>();
579 if (probe == nullptr)
580 {
581 std::cerr << "Probe statement wasn't a string, can't parse";
582 continue;
583 }
Christopher Meis26fbbd52025-03-26 14:55:06 +0100584 if (probe::findProbeType(*probe))
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030585 {
586 continue;
587 }
588 // syntax requires probe before first open brace
589 auto findStart = probe->find('(');
590 std::string interface = probe->substr(0, findStart);
591 dbusProbeInterfaces.emplace(interface);
592 dbusProbePointers.emplace_back(probePointer);
593 }
594 it++;
595 }
596
597 // probe vector stores a shared_ptr to each PerformProbe that cares
598 // about a dbus interface
599 findDbusObjects(std::move(dbusProbePointers),
Alexander Hansena555acf2025-06-27 11:59:10 +0200600 std::move(dbusProbeInterfaces), shared_from_this(), io);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030601}
602
Christopher Meis26fbbd52025-03-26 14:55:06 +0100603scan::PerformScan::~PerformScan()
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030604{
605 if (_passed)
606 {
607 auto nextScan = std::make_shared<PerformScan>(
Alexander Hansena555acf2025-06-27 11:59:10 +0200608 _em, _missingConfigurations, _configurations, io,
609 std::move(_callback));
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030610 nextScan->passedProbes = std::move(passedProbes);
611 nextScan->dbusProbeObjects = std::move(dbusProbeObjects);
612 nextScan->run();
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030613 }
614 else
615 {
616 _callback();
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030617 }
618}