blob: c3e531006fb9996acdf7119ab976e2af0119b509 [file] [log] [blame]
Andrew Jeffery47af65a2021-12-01 14:16:31 +10301/*
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 Bishope45d8c72022-05-25 15:12:53 -040016/// \file perform_scan.cpp
Christopher Meis26fbbd52025-03-26 14:55:06 +010017#include "perform_scan.hpp"
18
Christopher Meis26fbbd52025-03-26 14:55:06 +010019#include "perform_probe.hpp"
Christopher Meis59ef1e72025-04-16 08:53:25 +020020#include "utils.hpp"
Andrew Jeffery47af65a2021-12-01 14:16:31 +103021
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 Hansenc3db2c32024-08-20 15:01:38 +020026#include <phosphor-logging/lg2.hpp>
Andrew Jeffery47af65a2021-12-01 14:16:31 +103027
28#include <charconv>
Christopher Meis59ef1e72025-04-16 08:53:25 +020029#include <iostream>
Andrew Jeffery47af65a2021-12-01 14:16:31 +103030
Andrew Jeffery47af65a2021-12-01 14:16:31 +103031using GetSubTreeType = std::vector<
32 std::pair<std::string,
33 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
34
35constexpr const int32_t maxMapperDepth = 0;
36
Andrew Jefferyd9b67842022-04-05 16:44:20 +093037struct DBusInterfaceInstance
38{
39 std::string busName;
40 std::string path;
41 std::string interface;
42};
43
Patrick Williams5a807032025-03-03 11:20:39 -050044void getInterfaces(
45 const DBusInterfaceInstance& instance,
Christopher Meis26fbbd52025-03-26 14:55:06 +010046 const std::vector<std::shared_ptr<probe::PerformProbe>>& probeVector,
Alexander Hansena555acf2025-06-27 11:59:10 +020047 const std::shared_ptr<scan::PerformScan>& scan, boost::asio::io_context& io,
48 size_t retries = 5)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103049{
Ed Tanous3013fb42022-07-09 08:27:06 -070050 if (retries == 0U)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103051 {
Andrew Jefferyd9b67842022-04-05 16:44:20 +093052 std::cerr << "retries exhausted on " << instance.busName << " "
53 << instance.path << " " << instance.interface << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +103054 return;
55 }
56
Christopher Meiscf6a75b2025-06-03 07:53:50 +020057 scan->_em.systemBus->async_method_call(
Alexander Hansena555acf2025-06-27 11:59:10 +020058 [instance, scan, probeVector, retries,
59 &io](boost::system::error_code& errc, const DBusInterface& resp) {
Patrick Williamsb7077432024-08-16 15:22:21 -040060 if (errc)
61 {
62 std::cerr << "error calling getall on " << instance.busName
63 << " " << instance.path << " "
64 << instance.interface << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +103065
Patrick Williamsb7077432024-08-16 15:22:21 -040066 auto timer = std::make_shared<boost::asio::steady_timer>(io);
67 timer->expires_after(std::chrono::seconds(2));
Andrew Jeffery47af65a2021-12-01 14:16:31 +103068
Alexander Hansena555acf2025-06-27 11:59:10 +020069 timer->async_wait([timer, instance, scan, probeVector, retries,
70 &io](const boost::system::error_code&) {
71 getInterfaces(instance, probeVector, scan, io, retries - 1);
Patrick Williamsb7077432024-08-16 15:22:21 -040072 });
73 return;
74 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +103075
Patrick Williamsb7077432024-08-16 15:22:21 -040076 scan->dbusProbeObjects[instance.path][instance.interface] = resp;
77 },
Andrew Jefferyd9b67842022-04-05 16:44:20 +093078 instance.busName, instance.path, "org.freedesktop.DBus.Properties",
79 "GetAll", instance.interface);
Andrew Jeffery47af65a2021-12-01 14:16:31 +103080}
81
Patrick Williams5a807032025-03-03 11:20:39 -050082static void processDbusObjects(
Christopher Meis26fbbd52025-03-26 14:55:06 +010083 std::vector<std::shared_ptr<probe::PerformProbe>>& probeVector,
84 const std::shared_ptr<scan::PerformScan>& scan,
Alexander Hansena555acf2025-06-27 11:59:10 +020085 const GetSubTreeType& interfaceSubtree, boost::asio::io_context& io)
Andrew Jeffery8d2761e2022-04-05 16:26:28 +093086{
Andrew Jeffery8d2761e2022-04-05 16:26:28 +093087 for (const auto& [path, object] : interfaceSubtree)
88 {
Andrew Jeffery47321832022-04-05 16:30:29 +093089 // Get a PropertiesChanged callback for all interfaces on this path.
Christopher Meiscf6a75b2025-06-03 07:53:50 +020090 scan->_em.registerCallback(path);
Andrew Jeffery47321832022-04-05 16:30:29 +093091
Andrew Jeffery8d2761e2022-04-05 16:26:28 +093092 for (const auto& [busname, ifaces] : object)
93 {
94 for (const std::string& iface : ifaces)
95 {
96 // The 3 default org.freedeskstop interfaces (Peer,
97 // Introspectable, and Properties) are returned by
98 // the mapper but don't have properties, so don't bother
99 // with the GetAll call to save some cycles.
100 if (!boost::algorithm::starts_with(iface, "org.freedesktop"))
101 {
Alexander Hansena555acf2025-06-27 11:59:10 +0200102 getInterfaces({busname, path, iface}, probeVector, scan,
103 io);
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930104 }
105 }
106 }
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930107 }
108}
109
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030110// Populates scan->dbusProbeObjects with all interfaces and properties
111// for the paths that own the interfaces passed in.
Christopher Meis26fbbd52025-03-26 14:55:06 +0100112void findDbusObjects(
113 std::vector<std::shared_ptr<probe::PerformProbe>>&& probeVector,
114 boost::container::flat_set<std::string>&& interfaces,
Alexander Hansena555acf2025-06-27 11:59:10 +0200115 const std::shared_ptr<scan::PerformScan>& scan, boost::asio::io_context& io,
116 size_t retries = 5)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030117{
118 // Filter out interfaces already obtained.
119 for (const auto& [path, probeInterfaces] : scan->dbusProbeObjects)
120 {
121 for (const auto& [interface, _] : probeInterfaces)
122 {
123 interfaces.erase(interface);
124 }
125 }
126 if (interfaces.empty())
127 {
128 return;
129 }
130
131 // find all connections in the mapper that expose a specific type
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200132 scan->_em.systemBus->async_method_call(
Alexander Hansena555acf2025-06-27 11:59:10 +0200133 [interfaces, probeVector{std::move(probeVector)}, scan, retries,
134 &io](boost::system::error_code& ec,
135 const GetSubTreeType& interfaceSubtree) mutable {
Patrick Williamsb7077432024-08-16 15:22:21 -0400136 if (ec)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030137 {
Patrick Williamsb7077432024-08-16 15:22:21 -0400138 if (ec.value() == ENOENT)
139 {
140 return; // wasn't found by mapper
141 }
142 std::cerr << "Error communicating to mapper.\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030143
Patrick Williamsb7077432024-08-16 15:22:21 -0400144 if (retries == 0U)
145 {
146 // if we can't communicate to the mapper something is very
147 // wrong
148 std::exit(EXIT_FAILURE);
149 }
150
151 auto timer = std::make_shared<boost::asio::steady_timer>(io);
152 timer->expires_after(std::chrono::seconds(10));
153
154 timer->async_wait(
155 [timer, interfaces{std::move(interfaces)}, scan,
Alexander Hansena555acf2025-06-27 11:59:10 +0200156 probeVector{std::move(probeVector)}, retries,
157 &io](const boost::system::error_code&) mutable {
Patrick Williamsb7077432024-08-16 15:22:21 -0400158 findDbusObjects(std::move(probeVector),
Alexander Hansena555acf2025-06-27 11:59:10 +0200159 std::move(interfaces), scan, io,
Patrick Williamsb7077432024-08-16 15:22:21 -0400160 retries - 1);
161 });
162 return;
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030163 }
164
Alexander Hansena555acf2025-06-27 11:59:10 +0200165 processDbusObjects(probeVector, scan, interfaceSubtree, io);
Patrick Williamsb7077432024-08-16 15:22:21 -0400166 },
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030167 "xyz.openbmc_project.ObjectMapper",
168 "/xyz/openbmc_project/object_mapper",
169 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", maxMapperDepth,
170 interfaces);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030171}
172
Andrew Jeffery09a09a62022-04-12 22:49:57 +0930173static std::string getRecordName(const DBusInterface& probe,
174 const std::string& probeName)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030175{
176 if (probe.empty())
177 {
178 return probeName;
179 }
180
Andrew Jeffery928c5b22022-04-05 16:57:51 +0930181 // use an array so alphabetical order from the flat_map is maintained
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030182 auto device = nlohmann::json::array();
Ed Tanous3013fb42022-07-09 08:27:06 -0700183 for (const auto& devPair : probe)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030184 {
185 device.push_back(devPair.first);
186 std::visit([&device](auto&& v) { device.push_back(v); },
187 devPair.second);
188 }
Andrew Jeffery86501872022-04-05 16:58:22 +0930189
Andrew Jeffery928c5b22022-04-05 16:57:51 +0930190 // hashes are hard to distinguish, use the non-hashed version if we want
191 // debug
Alexander Hansenc3db2c32024-08-20 15:01:38 +0200192 // return probeName + device.dump();
Andrew Jeffery1ae4ed62022-04-05 16:55:43 +0930193
Andrew Jeffery86501872022-04-05 16:58:22 +0930194 return std::to_string(std::hash<std::string>{}(probeName + device.dump()));
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030195}
196
Alexander Hansena555acf2025-06-27 11:59:10 +0200197scan::PerformScan::PerformScan(
198 EntityManager& em, nlohmann::json& missingConfigurations,
Christopher Meisf7252572025-06-11 13:22:05 +0200199 std::vector<nlohmann::json>& configurations, boost::asio::io_context& io,
Alexander Hansena555acf2025-06-27 11:59:10 +0200200 std::function<void()>&& callback) :
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200201 _em(em), _missingConfigurations(missingConfigurations),
Alexander Hansena555acf2025-06-27 11:59:10 +0200202 _configurations(configurations), _callback(std::move(callback)), io(io)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030203{}
Andrew Jefferydb451482022-04-13 16:53:22 +0930204
Andrew Jeffery7a7faed2022-04-13 17:24:56 +0930205static void pruneRecordExposes(nlohmann::json& record)
Andrew Jefferydb451482022-04-13 16:53:22 +0930206{
Andrew Jeffery7a7faed2022-04-13 17:24:56 +0930207 auto findExposes = record.find("Exposes");
Andrew Jeffery5f051452022-04-13 17:11:37 +0930208 if (findExposes == record.end())
Andrew Jefferydb451482022-04-13 16:53:22 +0930209 {
Andrew Jeffery5f051452022-04-13 17:11:37 +0930210 return;
Andrew Jefferydb451482022-04-13 16:53:22 +0930211 }
Andrew Jeffery5f051452022-04-13 17:11:37 +0930212
213 auto copy = nlohmann::json::array();
214 for (auto& expose : *findExposes)
215 {
Andrew Jeffery742a7eb2022-04-13 17:14:46 +0930216 if (!expose.is_null())
Andrew Jeffery5f051452022-04-13 17:11:37 +0930217 {
Andrew Jeffery742a7eb2022-04-13 17:14:46 +0930218 copy.emplace_back(expose);
Andrew Jeffery5f051452022-04-13 17:11:37 +0930219 }
Andrew Jeffery5f051452022-04-13 17:11:37 +0930220 }
221 *findExposes = copy;
Andrew Jefferydb451482022-04-13 16:53:22 +0930222}
223
Patrick Williamsb7077432024-08-16 15:22:21 -0400224static void recordDiscoveredIdentifiers(
225 std::set<nlohmann::json>& usedNames, std::list<size_t>& indexes,
226 const std::string& probeName, const nlohmann::json& record)
Andrew Jeffery6addc022022-04-13 18:08:58 +0930227{
228 size_t indexIdx = probeName.find('$');
Andrew Jefferyba41b622022-04-13 18:13:28 +0930229 if (indexIdx == std::string::npos)
Andrew Jeffery6addc022022-04-13 18:08:58 +0930230 {
231 return;
232 }
233
Andrew Jefferyc0da0cc2022-04-13 18:15:09 +0930234 auto nameIt = record.find("Name");
235 if (nameIt == record.end())
Andrew Jeffery6addc022022-04-13 18:08:58 +0930236 {
237 std::cerr << "Last JSON Illegal\n";
238 return;
239 }
240
241 int index = 0;
242 auto str = nameIt->get<std::string>().substr(indexIdx);
Ed Tanous3013fb42022-07-09 08:27:06 -0700243 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
244 const char* endPtr = str.data() + str.size();
245 auto [p, ec] = std::from_chars(str.data(), endPtr, index);
Andrew Jeffery6addc022022-04-13 18:08:58 +0930246 if (ec != std::errc())
247 {
248 return; // non-numeric replacement
249 }
250
251 usedNames.insert(nameIt.value());
252
253 auto usedIt = std::find(indexes.begin(), indexes.end(), index);
254 if (usedIt != indexes.end())
255 {
256 indexes.erase(usedIt);
257 }
258}
259
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930260static bool extractExposeActionRecordNames(std::vector<std::string>& matches,
261 nlohmann::json::iterator& keyPair)
262{
Andrew Jefferya2449842022-04-14 15:35:51 +0930263 if (keyPair.value().is_string())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930264 {
265 matches.emplace_back(keyPair.value());
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930266 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930267 }
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930268
Andrew Jefferya2449842022-04-14 15:35:51 +0930269 if (keyPair.value().is_array())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930270 {
271 for (const auto& value : keyPair.value())
272 {
Andrew Jefferya2449842022-04-14 15:35:51 +0930273 if (!value.is_string())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930274 {
275 std::cerr << "Value is invalid type " << value << "\n";
276 break;
277 }
278 matches.emplace_back(value);
279 }
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930280
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930281 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930282 }
283
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930284 std::cerr << "Value is invalid type " << keyPair.key() << "\n";
285
286 return false;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930287}
288
Patrick Williamsb7077432024-08-16 15:22:21 -0400289static std::optional<std::vector<std::string>::iterator> findExposeActionRecord(
290 std::vector<std::string>& matches, const nlohmann::json& record)
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930291{
Andrew Jeffery1a02da82022-04-14 20:39:06 +0930292 const auto& name = (record)["Name"].get_ref<const std::string&>();
293 auto compare = [&name](const std::string& s) { return s == name; };
294 auto matchIt = std::find_if(matches.begin(), matches.end(), compare);
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930295
296 if (matchIt == matches.end())
297 {
298 return std::nullopt;
299 }
300
301 return matchIt;
302}
303
Andrew Jefferycf11bd32022-04-14 18:34:31 +0930304static void applyBindExposeAction(nlohmann::json& exposedObject,
305 nlohmann::json& expose,
306 const std::string& propertyName)
307{
308 if (boost::starts_with(propertyName, "Bind"))
309 {
310 std::string bind = propertyName.substr(sizeof("Bind") - 1);
311 exposedObject["Status"] = "okay";
312 expose[bind] = exposedObject;
313 }
314}
315
316static void applyDisableExposeAction(nlohmann::json& exposedObject,
317 const std::string& propertyName)
318{
319 if (propertyName == "DisableNode")
320 {
321 exposedObject["Status"] = "disabled";
322 }
323}
324
Patrick Williamsb7077432024-08-16 15:22:21 -0400325static void applyConfigExposeActions(
326 std::vector<std::string>& matches, nlohmann::json& expose,
327 const std::string& propertyName, nlohmann::json& configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930328{
Andrew Jefferyda45e5e2022-04-14 21:12:02 +0930329 for (auto& exposedObject : configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930330 {
331 auto match = findExposeActionRecord(matches, exposedObject);
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930332 if (match)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930333 {
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930334 matches.erase(*match);
335 applyBindExposeAction(exposedObject, expose, propertyName);
336 applyDisableExposeAction(exposedObject, propertyName);
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930337 }
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930338 }
339}
340
Patrick Williamsb7077432024-08-16 15:22:21 -0400341static void applyExposeActions(
342 nlohmann::json& systemConfiguration, const std::string& recordName,
343 nlohmann::json& expose, nlohmann::json::iterator& keyPair)
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930344{
345 bool isBind = boost::starts_with(keyPair.key(), "Bind");
346 bool isDisable = keyPair.key() == "DisableNode";
347 bool isExposeAction = isBind || isDisable;
348
349 if (!isExposeAction)
350 {
351 return;
352 }
353
354 std::vector<std::string> matches;
355
356 if (!extractExposeActionRecordNames(matches, keyPair))
357 {
358 return;
359 }
360
Patrick Williams2594d362022-09-28 06:46:24 -0500361 for (const auto& [configId, config] : systemConfiguration.items())
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930362 {
363 // don't disable ourselves
364 if (isDisable && configId == recordName)
365 {
366 continue;
367 }
368
369 auto configListFind = config.find("Exposes");
370 if (configListFind == config.end())
371 {
372 continue;
373 }
374
375 if (!configListFind->is_array())
376 {
377 continue;
378 }
379
380 applyConfigExposeActions(matches, expose, keyPair.key(),
381 *configListFind);
382 }
383
384 if (!matches.empty())
385 {
386 std::cerr << "configuration file dependency error, could not find "
387 << keyPair.key() << " " << keyPair.value() << "\n";
388 }
389}
390
Patrick Williamsb7077432024-08-16 15:22:21 -0400391static std::string generateDeviceName(
392 const std::set<nlohmann::json>& usedNames, const DBusObject& dbusObject,
393 size_t foundDeviceIdx, const std::string& nameTemplate,
394 std::optional<std::string>& replaceStr)
Andrew Jefferydabee982022-04-19 13:27:24 +0930395{
396 nlohmann::json copyForName = {{"Name", nameTemplate}};
397 nlohmann::json::iterator copyIt = copyForName.begin();
Christopher Meis59ef1e72025-04-16 08:53:25 +0200398 std::optional<std::string> replaceVal = em_utils::templateCharReplace(
399 copyIt, dbusObject, foundDeviceIdx, replaceStr);
Andrew Jefferydabee982022-04-19 13:27:24 +0930400
401 if (!replaceStr && replaceVal)
402 {
Patrick Williams64599932025-05-14 07:24:20 -0400403 if (usedNames.contains(copyIt.value()))
Andrew Jefferydabee982022-04-19 13:27:24 +0930404 {
405 replaceStr = replaceVal;
406 copyForName = {{"Name", nameTemplate}};
407 copyIt = copyForName.begin();
Christopher Meis59ef1e72025-04-16 08:53:25 +0200408 em_utils::templateCharReplace(copyIt, dbusObject, foundDeviceIdx,
409 replaceStr);
Andrew Jefferydabee982022-04-19 13:27:24 +0930410 }
411 }
412
413 if (replaceStr)
414 {
415 std::cerr << "Duplicates found, replacing " << *replaceStr
416 << " with found device index.\n Consider "
417 "fixing template to not have duplicates\n";
418 }
419
420 return copyIt.value();
421}
422
Christopher Meis26fbbd52025-03-26 14:55:06 +0100423void scan::PerformScan::updateSystemConfiguration(
424 const nlohmann::json& recordRef, const std::string& probeName,
425 FoundDevices& foundDevices)
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930426{
427 _passed = true;
428 passedProbes.push_back(probeName);
429
430 std::set<nlohmann::json> usedNames;
431 std::list<size_t> indexes(foundDevices.size());
432 std::iota(indexes.begin(), indexes.end(), 1);
433
434 // copy over persisted configurations and make sure we remove
435 // indexes that are already used
436 for (auto itr = foundDevices.begin(); itr != foundDevices.end();)
437 {
438 std::string recordName = getRecordName(itr->interface, probeName);
439
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200440 auto record = _em.systemConfiguration.find(recordName);
441 if (record == _em.systemConfiguration.end())
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930442 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200443 record = _em.lastJson.find(recordName);
444 if (record == _em.lastJson.end())
Zhikui Ren1f77d9c2022-08-29 16:02:37 -0700445 {
446 itr++;
447 continue;
448 }
449
450 pruneRecordExposes(*record);
451
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200452 _em.systemConfiguration[recordName] = *record;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930453 }
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930454 _missingConfigurations.erase(recordName);
455
456 // We've processed the device, remove it and advance the
457 // iterator
458 itr = foundDevices.erase(itr);
JinFuLin8f05a3a2022-11-21 15:33:24 +0800459 recordDiscoveredIdentifiers(usedNames, indexes, probeName, *record);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930460 }
461
462 std::optional<std::string> replaceStr;
463
464 DBusObject emptyObject;
465 DBusInterface emptyInterface;
466 emptyObject.emplace(std::string{}, emptyInterface);
467
468 for (const auto& [foundDevice, path] : foundDevices)
469 {
470 // Need all interfaces on this path so that template
471 // substitutions can be done with any of the contained
472 // properties. If the probe that passed didn't use an
473 // interface, such as if it was just TRUE, then
474 // templateCharReplace will just get passed in an empty
475 // map.
Andrew Jefferyaf9b46b2022-04-21 12:34:43 +0930476 auto objectIt = dbusProbeObjects.find(path);
477 const DBusObject& dbusObject = (objectIt == dbusProbeObjects.end())
478 ? emptyObject
479 : objectIt->second;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930480
481 nlohmann::json record = recordRef;
482 std::string recordName = getRecordName(foundDevice, probeName);
483 size_t foundDeviceIdx = indexes.front();
484 indexes.pop_front();
485
486 // check name first so we have no duplicate names
487 auto getName = record.find("Name");
488 if (getName == record.end())
489 {
490 std::cerr << "Record Missing Name! " << record.dump();
491 continue; // this should be impossible at this level
492 }
493
494 std::string deviceName = generateDeviceName(
495 usedNames, dbusObject, foundDeviceIdx, getName.value(), replaceStr);
496 getName.value() = deviceName;
497 usedNames.insert(deviceName);
498
499 for (auto keyPair = record.begin(); keyPair != record.end(); keyPair++)
500 {
501 if (keyPair.key() != "Name")
502 {
Christopher Meis59ef1e72025-04-16 08:53:25 +0200503 em_utils::templateCharReplace(keyPair, dbusObject,
504 foundDeviceIdx, replaceStr);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930505 }
506 }
507
508 // insert into configuration temporarily to be able to
509 // reference ourselves
510
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200511 _em.systemConfiguration[recordName] = record;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930512
513 auto findExpose = record.find("Exposes");
514 if (findExpose == record.end())
515 {
516 continue;
517 }
518
519 for (auto& expose : *findExpose)
520 {
521 for (auto keyPair = expose.begin(); keyPair != expose.end();
522 keyPair++)
523 {
Christopher Meis59ef1e72025-04-16 08:53:25 +0200524 em_utils::templateCharReplace(keyPair, dbusObject,
525 foundDeviceIdx, replaceStr);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930526
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200527 applyExposeActions(_em.systemConfiguration, recordName, expose,
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930528 keyPair);
529 }
530 }
531
532 // overwrite ourselves with cleaned up version
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200533 _em.systemConfiguration[recordName] = record;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930534 _missingConfigurations.erase(recordName);
535 }
536}
537
Christopher Meis26fbbd52025-03-26 14:55:06 +0100538void scan::PerformScan::run()
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030539{
540 boost::container::flat_set<std::string> dbusProbeInterfaces;
Christopher Meis26fbbd52025-03-26 14:55:06 +0100541 std::vector<std::shared_ptr<probe::PerformProbe>> dbusProbePointers;
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030542
543 for (auto it = _configurations.begin(); it != _configurations.end();)
544 {
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030545 // check for poorly formatted fields, probe must be an array
Andrew Jeffery38834872022-04-19 15:15:57 +0930546 auto findProbe = it->find("Probe");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030547 if (findProbe == it->end())
548 {
549 std::cerr << "configuration file missing probe:\n " << *it << "\n";
550 it = _configurations.erase(it);
551 continue;
552 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030553
Andrew Jeffery38834872022-04-19 15:15:57 +0930554 auto findName = it->find("Name");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030555 if (findName == it->end())
556 {
557 std::cerr << "configuration file missing name:\n " << *it << "\n";
558 it = _configurations.erase(it);
559 continue;
560 }
561 std::string probeName = *findName;
562
563 if (std::find(passedProbes.begin(), passedProbes.end(), probeName) !=
564 passedProbes.end())
565 {
566 it = _configurations.erase(it);
567 continue;
568 }
Andrew Jeffery38834872022-04-19 15:15:57 +0930569
Andrew Jeffery98f3d532022-04-19 15:29:22 +0930570 nlohmann::json& recordRef = *it;
Andrew Jeffery38834872022-04-19 15:15:57 +0930571 nlohmann::json probeCommand;
572 if ((*findProbe).type() != nlohmann::json::value_t::array)
573 {
574 probeCommand = nlohmann::json::array();
575 probeCommand.push_back(*findProbe);
576 }
577 else
578 {
579 probeCommand = *findProbe;
580 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030581
582 // store reference to this to children to makes sure we don't get
583 // destroyed too early
584 auto thisRef = shared_from_this();
Christopher Meis26fbbd52025-03-26 14:55:06 +0100585 auto probePointer = std::make_shared<probe::PerformProbe>(
Andrew Jefferyea4ff022022-04-21 12:31:40 +0930586 recordRef, probeCommand, probeName, thisRef);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030587
588 // parse out dbus probes by discarding other probe types, store in a
589 // map
590 for (const nlohmann::json& probeJson : probeCommand)
591 {
592 const std::string* probe = probeJson.get_ptr<const std::string*>();
593 if (probe == nullptr)
594 {
595 std::cerr << "Probe statement wasn't a string, can't parse";
596 continue;
597 }
Christopher Meis26fbbd52025-03-26 14:55:06 +0100598 if (probe::findProbeType(*probe))
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030599 {
600 continue;
601 }
602 // syntax requires probe before first open brace
603 auto findStart = probe->find('(');
604 std::string interface = probe->substr(0, findStart);
605 dbusProbeInterfaces.emplace(interface);
606 dbusProbePointers.emplace_back(probePointer);
607 }
608 it++;
609 }
610
611 // probe vector stores a shared_ptr to each PerformProbe that cares
612 // about a dbus interface
613 findDbusObjects(std::move(dbusProbePointers),
Alexander Hansena555acf2025-06-27 11:59:10 +0200614 std::move(dbusProbeInterfaces), shared_from_this(), io);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030615}
616
Christopher Meis26fbbd52025-03-26 14:55:06 +0100617scan::PerformScan::~PerformScan()
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030618{
619 if (_passed)
620 {
621 auto nextScan = std::make_shared<PerformScan>(
Alexander Hansena555acf2025-06-27 11:59:10 +0200622 _em, _missingConfigurations, _configurations, io,
623 std::move(_callback));
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030624 nextScan->passedProbes = std::move(passedProbes);
625 nextScan->dbusProbeObjects = std::move(dbusProbeObjects);
626 nextScan->run();
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030627 }
628 else
629 {
630 _callback();
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030631 }
632}