blob: 04d3c675329b910dfc6ae1282fe03abd2ec4cbbe [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,
47 const std::shared_ptr<scan::PerformScan>& scan, size_t retries = 5)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103048{
Ed Tanous3013fb42022-07-09 08:27:06 -070049 if (retries == 0U)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103050 {
Andrew Jefferyd9b67842022-04-05 16:44:20 +093051 std::cerr << "retries exhausted on " << instance.busName << " "
52 << instance.path << " " << instance.interface << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +103053 return;
54 }
55
Christopher Meiscf6a75b2025-06-03 07:53:50 +020056 scan->_em.systemBus->async_method_call(
Patrick Williamsb7077432024-08-16 15:22:21 -040057 [instance, scan, probeVector,
58 retries](boost::system::error_code& errc, const DBusInterface& resp) {
59 if (errc)
60 {
61 std::cerr << "error calling getall on " << instance.busName
62 << " " << instance.path << " "
63 << instance.interface << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +103064
Patrick Williamsb7077432024-08-16 15:22:21 -040065 auto timer = std::make_shared<boost::asio::steady_timer>(io);
66 timer->expires_after(std::chrono::seconds(2));
Andrew Jeffery47af65a2021-12-01 14:16:31 +103067
Patrick Williamsb7077432024-08-16 15:22:21 -040068 timer->async_wait([timer, instance, scan, probeVector,
69 retries](const boost::system::error_code&) {
70 getInterfaces(instance, probeVector, scan, retries - 1);
71 });
72 return;
73 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +103074
Patrick Williamsb7077432024-08-16 15:22:21 -040075 scan->dbusProbeObjects[instance.path][instance.interface] = resp;
76 },
Andrew Jefferyd9b67842022-04-05 16:44:20 +093077 instance.busName, instance.path, "org.freedesktop.DBus.Properties",
78 "GetAll", instance.interface);
Andrew Jeffery47af65a2021-12-01 14:16:31 +103079}
80
Patrick Williams5a807032025-03-03 11:20:39 -050081static void processDbusObjects(
Christopher Meis26fbbd52025-03-26 14:55:06 +010082 std::vector<std::shared_ptr<probe::PerformProbe>>& probeVector,
83 const std::shared_ptr<scan::PerformScan>& scan,
Patrick Williams5a807032025-03-03 11:20:39 -050084 const GetSubTreeType& interfaceSubtree)
Andrew Jeffery8d2761e2022-04-05 16:26:28 +093085{
Andrew Jeffery8d2761e2022-04-05 16:26:28 +093086 for (const auto& [path, object] : interfaceSubtree)
87 {
Andrew Jeffery47321832022-04-05 16:30:29 +093088 // Get a PropertiesChanged callback for all interfaces on this path.
Christopher Meiscf6a75b2025-06-03 07:53:50 +020089 scan->_em.registerCallback(path);
Andrew Jeffery47321832022-04-05 16:30:29 +093090
Andrew Jeffery8d2761e2022-04-05 16:26:28 +093091 for (const auto& [busname, ifaces] : object)
92 {
93 for (const std::string& iface : ifaces)
94 {
95 // The 3 default org.freedeskstop interfaces (Peer,
96 // Introspectable, and Properties) are returned by
97 // the mapper but don't have properties, so don't bother
98 // with the GetAll call to save some cycles.
99 if (!boost::algorithm::starts_with(iface, "org.freedesktop"))
100 {
Andrew Jeffery47321832022-04-05 16:30:29 +0930101 getInterfaces({busname, path, iface}, probeVector, scan);
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930102 }
103 }
104 }
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930105 }
106}
107
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030108// Populates scan->dbusProbeObjects with all interfaces and properties
109// for the paths that own the interfaces passed in.
Christopher Meis26fbbd52025-03-26 14:55:06 +0100110void findDbusObjects(
111 std::vector<std::shared_ptr<probe::PerformProbe>>&& probeVector,
112 boost::container::flat_set<std::string>&& interfaces,
113 const std::shared_ptr<scan::PerformScan>& scan, size_t retries = 5)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030114{
115 // Filter out interfaces already obtained.
116 for (const auto& [path, probeInterfaces] : scan->dbusProbeObjects)
117 {
118 for (const auto& [interface, _] : probeInterfaces)
119 {
120 interfaces.erase(interface);
121 }
122 }
123 if (interfaces.empty())
124 {
125 return;
126 }
127
128 // find all connections in the mapper that expose a specific type
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200129 scan->_em.systemBus->async_method_call(
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030130 [interfaces, probeVector{std::move(probeVector)}, scan,
131 retries](boost::system::error_code& ec,
132 const GetSubTreeType& interfaceSubtree) mutable {
Patrick Williamsb7077432024-08-16 15:22:21 -0400133 if (ec)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030134 {
Patrick Williamsb7077432024-08-16 15:22:21 -0400135 if (ec.value() == ENOENT)
136 {
137 return; // wasn't found by mapper
138 }
139 std::cerr << "Error communicating to mapper.\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030140
Patrick Williamsb7077432024-08-16 15:22:21 -0400141 if (retries == 0U)
142 {
143 // if we can't communicate to the mapper something is very
144 // wrong
145 std::exit(EXIT_FAILURE);
146 }
147
148 auto timer = std::make_shared<boost::asio::steady_timer>(io);
149 timer->expires_after(std::chrono::seconds(10));
150
151 timer->async_wait(
152 [timer, interfaces{std::move(interfaces)}, scan,
153 probeVector{std::move(probeVector)},
154 retries](const boost::system::error_code&) mutable {
155 findDbusObjects(std::move(probeVector),
156 std::move(interfaces), scan,
157 retries - 1);
158 });
159 return;
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030160 }
161
Patrick Williamsb7077432024-08-16 15:22:21 -0400162 processDbusObjects(probeVector, scan, interfaceSubtree);
163 },
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030164 "xyz.openbmc_project.ObjectMapper",
165 "/xyz/openbmc_project/object_mapper",
166 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", maxMapperDepth,
167 interfaces);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030168}
169
Andrew Jeffery09a09a62022-04-12 22:49:57 +0930170static std::string getRecordName(const DBusInterface& probe,
171 const std::string& probeName)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030172{
173 if (probe.empty())
174 {
175 return probeName;
176 }
177
Andrew Jeffery928c5b22022-04-05 16:57:51 +0930178 // use an array so alphabetical order from the flat_map is maintained
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030179 auto device = nlohmann::json::array();
Ed Tanous3013fb42022-07-09 08:27:06 -0700180 for (const auto& devPair : probe)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030181 {
182 device.push_back(devPair.first);
183 std::visit([&device](auto&& v) { device.push_back(v); },
184 devPair.second);
185 }
Andrew Jeffery86501872022-04-05 16:58:22 +0930186
Andrew Jeffery928c5b22022-04-05 16:57:51 +0930187 // hashes are hard to distinguish, use the non-hashed version if we want
188 // debug
Alexander Hansenc3db2c32024-08-20 15:01:38 +0200189 // return probeName + device.dump();
Andrew Jeffery1ae4ed62022-04-05 16:55:43 +0930190
Andrew Jeffery86501872022-04-05 16:58:22 +0930191 return std::to_string(std::hash<std::string>{}(probeName + device.dump()));
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030192}
193
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200194scan::PerformScan::PerformScan(EntityManager& em,
Christopher Meis26fbbd52025-03-26 14:55:06 +0100195 nlohmann::json& missingConfigurations,
196 std::list<nlohmann::json>& configurations,
Christopher Meis26fbbd52025-03-26 14:55:06 +0100197 std::function<void()>&& callback) :
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200198 _em(em), _missingConfigurations(missingConfigurations),
199 _configurations(configurations), _callback(std::move(callback))
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030200{}
Andrew Jefferydb451482022-04-13 16:53:22 +0930201
Andrew Jeffery7a7faed2022-04-13 17:24:56 +0930202static void pruneRecordExposes(nlohmann::json& record)
Andrew Jefferydb451482022-04-13 16:53:22 +0930203{
Andrew Jeffery7a7faed2022-04-13 17:24:56 +0930204 auto findExposes = record.find("Exposes");
Andrew Jeffery5f051452022-04-13 17:11:37 +0930205 if (findExposes == record.end())
Andrew Jefferydb451482022-04-13 16:53:22 +0930206 {
Andrew Jeffery5f051452022-04-13 17:11:37 +0930207 return;
Andrew Jefferydb451482022-04-13 16:53:22 +0930208 }
Andrew Jeffery5f051452022-04-13 17:11:37 +0930209
210 auto copy = nlohmann::json::array();
211 for (auto& expose : *findExposes)
212 {
Andrew Jeffery742a7eb2022-04-13 17:14:46 +0930213 if (!expose.is_null())
Andrew Jeffery5f051452022-04-13 17:11:37 +0930214 {
Andrew Jeffery742a7eb2022-04-13 17:14:46 +0930215 copy.emplace_back(expose);
Andrew Jeffery5f051452022-04-13 17:11:37 +0930216 }
Andrew Jeffery5f051452022-04-13 17:11:37 +0930217 }
218 *findExposes = copy;
Andrew Jefferydb451482022-04-13 16:53:22 +0930219}
220
Patrick Williamsb7077432024-08-16 15:22:21 -0400221static void recordDiscoveredIdentifiers(
222 std::set<nlohmann::json>& usedNames, std::list<size_t>& indexes,
223 const std::string& probeName, const nlohmann::json& record)
Andrew Jeffery6addc022022-04-13 18:08:58 +0930224{
225 size_t indexIdx = probeName.find('$');
Andrew Jefferyba41b622022-04-13 18:13:28 +0930226 if (indexIdx == std::string::npos)
Andrew Jeffery6addc022022-04-13 18:08:58 +0930227 {
228 return;
229 }
230
Andrew Jefferyc0da0cc2022-04-13 18:15:09 +0930231 auto nameIt = record.find("Name");
232 if (nameIt == record.end())
Andrew Jeffery6addc022022-04-13 18:08:58 +0930233 {
234 std::cerr << "Last JSON Illegal\n";
235 return;
236 }
237
238 int index = 0;
239 auto str = nameIt->get<std::string>().substr(indexIdx);
Ed Tanous3013fb42022-07-09 08:27:06 -0700240 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
241 const char* endPtr = str.data() + str.size();
242 auto [p, ec] = std::from_chars(str.data(), endPtr, index);
Andrew Jeffery6addc022022-04-13 18:08:58 +0930243 if (ec != std::errc())
244 {
245 return; // non-numeric replacement
246 }
247
248 usedNames.insert(nameIt.value());
249
250 auto usedIt = std::find(indexes.begin(), indexes.end(), index);
251 if (usedIt != indexes.end())
252 {
253 indexes.erase(usedIt);
254 }
255}
256
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930257static bool extractExposeActionRecordNames(std::vector<std::string>& matches,
258 nlohmann::json::iterator& keyPair)
259{
Andrew Jefferya2449842022-04-14 15:35:51 +0930260 if (keyPair.value().is_string())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930261 {
262 matches.emplace_back(keyPair.value());
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930263 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930264 }
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930265
Andrew Jefferya2449842022-04-14 15:35:51 +0930266 if (keyPair.value().is_array())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930267 {
268 for (const auto& value : keyPair.value())
269 {
Andrew Jefferya2449842022-04-14 15:35:51 +0930270 if (!value.is_string())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930271 {
272 std::cerr << "Value is invalid type " << value << "\n";
273 break;
274 }
275 matches.emplace_back(value);
276 }
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930277
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930278 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930279 }
280
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930281 std::cerr << "Value is invalid type " << keyPair.key() << "\n";
282
283 return false;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930284}
285
Patrick Williamsb7077432024-08-16 15:22:21 -0400286static std::optional<std::vector<std::string>::iterator> findExposeActionRecord(
287 std::vector<std::string>& matches, const nlohmann::json& record)
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930288{
Andrew Jeffery1a02da82022-04-14 20:39:06 +0930289 const auto& name = (record)["Name"].get_ref<const std::string&>();
290 auto compare = [&name](const std::string& s) { return s == name; };
291 auto matchIt = std::find_if(matches.begin(), matches.end(), compare);
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930292
293 if (matchIt == matches.end())
294 {
295 return std::nullopt;
296 }
297
298 return matchIt;
299}
300
Andrew Jefferycf11bd32022-04-14 18:34:31 +0930301static void applyBindExposeAction(nlohmann::json& exposedObject,
302 nlohmann::json& expose,
303 const std::string& propertyName)
304{
305 if (boost::starts_with(propertyName, "Bind"))
306 {
307 std::string bind = propertyName.substr(sizeof("Bind") - 1);
308 exposedObject["Status"] = "okay";
309 expose[bind] = exposedObject;
310 }
311}
312
313static void applyDisableExposeAction(nlohmann::json& exposedObject,
314 const std::string& propertyName)
315{
316 if (propertyName == "DisableNode")
317 {
318 exposedObject["Status"] = "disabled";
319 }
320}
321
Patrick Williamsb7077432024-08-16 15:22:21 -0400322static void applyConfigExposeActions(
323 std::vector<std::string>& matches, nlohmann::json& expose,
324 const std::string& propertyName, nlohmann::json& configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930325{
Andrew Jefferyda45e5e2022-04-14 21:12:02 +0930326 for (auto& exposedObject : configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930327 {
328 auto match = findExposeActionRecord(matches, exposedObject);
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930329 if (match)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930330 {
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930331 matches.erase(*match);
332 applyBindExposeAction(exposedObject, expose, propertyName);
333 applyDisableExposeAction(exposedObject, propertyName);
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930334 }
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930335 }
336}
337
Patrick Williamsb7077432024-08-16 15:22:21 -0400338static void applyExposeActions(
339 nlohmann::json& systemConfiguration, const std::string& recordName,
340 nlohmann::json& expose, nlohmann::json::iterator& keyPair)
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930341{
342 bool isBind = boost::starts_with(keyPair.key(), "Bind");
343 bool isDisable = keyPair.key() == "DisableNode";
344 bool isExposeAction = isBind || isDisable;
345
346 if (!isExposeAction)
347 {
348 return;
349 }
350
351 std::vector<std::string> matches;
352
353 if (!extractExposeActionRecordNames(matches, keyPair))
354 {
355 return;
356 }
357
Patrick Williams2594d362022-09-28 06:46:24 -0500358 for (const auto& [configId, config] : systemConfiguration.items())
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930359 {
360 // don't disable ourselves
361 if (isDisable && configId == recordName)
362 {
363 continue;
364 }
365
366 auto configListFind = config.find("Exposes");
367 if (configListFind == config.end())
368 {
369 continue;
370 }
371
372 if (!configListFind->is_array())
373 {
374 continue;
375 }
376
377 applyConfigExposeActions(matches, expose, keyPair.key(),
378 *configListFind);
379 }
380
381 if (!matches.empty())
382 {
383 std::cerr << "configuration file dependency error, could not find "
384 << keyPair.key() << " " << keyPair.value() << "\n";
385 }
386}
387
Patrick Williamsb7077432024-08-16 15:22:21 -0400388static std::string generateDeviceName(
389 const std::set<nlohmann::json>& usedNames, const DBusObject& dbusObject,
390 size_t foundDeviceIdx, const std::string& nameTemplate,
391 std::optional<std::string>& replaceStr)
Andrew Jefferydabee982022-04-19 13:27:24 +0930392{
393 nlohmann::json copyForName = {{"Name", nameTemplate}};
394 nlohmann::json::iterator copyIt = copyForName.begin();
Christopher Meis59ef1e72025-04-16 08:53:25 +0200395 std::optional<std::string> replaceVal = em_utils::templateCharReplace(
396 copyIt, dbusObject, foundDeviceIdx, replaceStr);
Andrew Jefferydabee982022-04-19 13:27:24 +0930397
398 if (!replaceStr && replaceVal)
399 {
Patrick Williams64599932025-05-14 07:24:20 -0400400 if (usedNames.contains(copyIt.value()))
Andrew Jefferydabee982022-04-19 13:27:24 +0930401 {
402 replaceStr = replaceVal;
403 copyForName = {{"Name", nameTemplate}};
404 copyIt = copyForName.begin();
Christopher Meis59ef1e72025-04-16 08:53:25 +0200405 em_utils::templateCharReplace(copyIt, dbusObject, foundDeviceIdx,
406 replaceStr);
Andrew Jefferydabee982022-04-19 13:27:24 +0930407 }
408 }
409
410 if (replaceStr)
411 {
412 std::cerr << "Duplicates found, replacing " << *replaceStr
413 << " with found device index.\n Consider "
414 "fixing template to not have duplicates\n";
415 }
416
417 return copyIt.value();
418}
419
Christopher Meis26fbbd52025-03-26 14:55:06 +0100420void scan::PerformScan::updateSystemConfiguration(
421 const nlohmann::json& recordRef, const std::string& probeName,
422 FoundDevices& foundDevices)
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930423{
424 _passed = true;
425 passedProbes.push_back(probeName);
426
427 std::set<nlohmann::json> usedNames;
428 std::list<size_t> indexes(foundDevices.size());
429 std::iota(indexes.begin(), indexes.end(), 1);
430
431 // copy over persisted configurations and make sure we remove
432 // indexes that are already used
433 for (auto itr = foundDevices.begin(); itr != foundDevices.end();)
434 {
435 std::string recordName = getRecordName(itr->interface, probeName);
436
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200437 auto record = _em.systemConfiguration.find(recordName);
438 if (record == _em.systemConfiguration.end())
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930439 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200440 record = _em.lastJson.find(recordName);
441 if (record == _em.lastJson.end())
Zhikui Ren1f77d9c2022-08-29 16:02:37 -0700442 {
443 itr++;
444 continue;
445 }
446
447 pruneRecordExposes(*record);
448
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200449 _em.systemConfiguration[recordName] = *record;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930450 }
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930451 _missingConfigurations.erase(recordName);
452
453 // We've processed the device, remove it and advance the
454 // iterator
455 itr = foundDevices.erase(itr);
JinFuLin8f05a3a2022-11-21 15:33:24 +0800456 recordDiscoveredIdentifiers(usedNames, indexes, probeName, *record);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930457 }
458
459 std::optional<std::string> replaceStr;
460
461 DBusObject emptyObject;
462 DBusInterface emptyInterface;
463 emptyObject.emplace(std::string{}, emptyInterface);
464
465 for (const auto& [foundDevice, path] : foundDevices)
466 {
467 // Need all interfaces on this path so that template
468 // substitutions can be done with any of the contained
469 // properties. If the probe that passed didn't use an
470 // interface, such as if it was just TRUE, then
471 // templateCharReplace will just get passed in an empty
472 // map.
Andrew Jefferyaf9b46b2022-04-21 12:34:43 +0930473 auto objectIt = dbusProbeObjects.find(path);
474 const DBusObject& dbusObject = (objectIt == dbusProbeObjects.end())
475 ? emptyObject
476 : objectIt->second;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930477
478 nlohmann::json record = recordRef;
479 std::string recordName = getRecordName(foundDevice, probeName);
480 size_t foundDeviceIdx = indexes.front();
481 indexes.pop_front();
482
483 // check name first so we have no duplicate names
484 auto getName = record.find("Name");
485 if (getName == record.end())
486 {
487 std::cerr << "Record Missing Name! " << record.dump();
488 continue; // this should be impossible at this level
489 }
490
491 std::string deviceName = generateDeviceName(
492 usedNames, dbusObject, foundDeviceIdx, getName.value(), replaceStr);
493 getName.value() = deviceName;
494 usedNames.insert(deviceName);
495
496 for (auto keyPair = record.begin(); keyPair != record.end(); keyPair++)
497 {
498 if (keyPair.key() != "Name")
499 {
Christopher Meis59ef1e72025-04-16 08:53:25 +0200500 em_utils::templateCharReplace(keyPair, dbusObject,
501 foundDeviceIdx, replaceStr);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930502 }
503 }
504
505 // insert into configuration temporarily to be able to
506 // reference ourselves
507
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200508 _em.systemConfiguration[recordName] = record;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930509
510 auto findExpose = record.find("Exposes");
511 if (findExpose == record.end())
512 {
513 continue;
514 }
515
516 for (auto& expose : *findExpose)
517 {
518 for (auto keyPair = expose.begin(); keyPair != expose.end();
519 keyPair++)
520 {
Christopher Meis59ef1e72025-04-16 08:53:25 +0200521 em_utils::templateCharReplace(keyPair, dbusObject,
522 foundDeviceIdx, replaceStr);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930523
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200524 applyExposeActions(_em.systemConfiguration, recordName, expose,
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930525 keyPair);
526 }
527 }
528
529 // overwrite ourselves with cleaned up version
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200530 _em.systemConfiguration[recordName] = record;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930531 _missingConfigurations.erase(recordName);
532 }
533}
534
Christopher Meis26fbbd52025-03-26 14:55:06 +0100535void scan::PerformScan::run()
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030536{
537 boost::container::flat_set<std::string> dbusProbeInterfaces;
Christopher Meis26fbbd52025-03-26 14:55:06 +0100538 std::vector<std::shared_ptr<probe::PerformProbe>> dbusProbePointers;
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030539
540 for (auto it = _configurations.begin(); it != _configurations.end();)
541 {
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030542 // check for poorly formatted fields, probe must be an array
Andrew Jeffery38834872022-04-19 15:15:57 +0930543 auto findProbe = it->find("Probe");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030544 if (findProbe == it->end())
545 {
546 std::cerr << "configuration file missing probe:\n " << *it << "\n";
547 it = _configurations.erase(it);
548 continue;
549 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030550
Andrew Jeffery38834872022-04-19 15:15:57 +0930551 auto findName = it->find("Name");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030552 if (findName == it->end())
553 {
554 std::cerr << "configuration file missing name:\n " << *it << "\n";
555 it = _configurations.erase(it);
556 continue;
557 }
558 std::string probeName = *findName;
559
560 if (std::find(passedProbes.begin(), passedProbes.end(), probeName) !=
561 passedProbes.end())
562 {
563 it = _configurations.erase(it);
564 continue;
565 }
Andrew Jeffery38834872022-04-19 15:15:57 +0930566
Andrew Jeffery98f3d532022-04-19 15:29:22 +0930567 nlohmann::json& recordRef = *it;
Andrew Jeffery38834872022-04-19 15:15:57 +0930568 nlohmann::json probeCommand;
569 if ((*findProbe).type() != nlohmann::json::value_t::array)
570 {
571 probeCommand = nlohmann::json::array();
572 probeCommand.push_back(*findProbe);
573 }
574 else
575 {
576 probeCommand = *findProbe;
577 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030578
579 // store reference to this to children to makes sure we don't get
580 // destroyed too early
581 auto thisRef = shared_from_this();
Christopher Meis26fbbd52025-03-26 14:55:06 +0100582 auto probePointer = std::make_shared<probe::PerformProbe>(
Andrew Jefferyea4ff022022-04-21 12:31:40 +0930583 recordRef, probeCommand, probeName, thisRef);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030584
585 // parse out dbus probes by discarding other probe types, store in a
586 // map
587 for (const nlohmann::json& probeJson : probeCommand)
588 {
589 const std::string* probe = probeJson.get_ptr<const std::string*>();
590 if (probe == nullptr)
591 {
592 std::cerr << "Probe statement wasn't a string, can't parse";
593 continue;
594 }
Christopher Meis26fbbd52025-03-26 14:55:06 +0100595 if (probe::findProbeType(*probe))
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030596 {
597 continue;
598 }
599 // syntax requires probe before first open brace
600 auto findStart = probe->find('(');
601 std::string interface = probe->substr(0, findStart);
602 dbusProbeInterfaces.emplace(interface);
603 dbusProbePointers.emplace_back(probePointer);
604 }
605 it++;
606 }
607
608 // probe vector stores a shared_ptr to each PerformProbe that cares
609 // about a dbus interface
610 findDbusObjects(std::move(dbusProbePointers),
611 std::move(dbusProbeInterfaces), shared_from_this());
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030612}
613
Christopher Meis26fbbd52025-03-26 14:55:06 +0100614scan::PerformScan::~PerformScan()
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030615{
616 if (_passed)
617 {
618 auto nextScan = std::make_shared<PerformScan>(
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200619 _em, _missingConfigurations, _configurations, std::move(_callback));
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030620 nextScan->passedProbes = std::move(passedProbes);
621 nextScan->dbusProbeObjects = std::move(dbusProbeObjects);
622 nextScan->run();
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030623 }
624 else
625 {
626 _callback();
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030627 }
628}