blob: d05312de2360d5a61a1ce86c035cb40c230136b4 [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
Brad Bishope45d8c72022-05-25 15:12:53 -040019#include "entity_manager.hpp"
Christopher Meis26fbbd52025-03-26 14:55:06 +010020#include "perform_probe.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>
29
Brad Bishope45d8c72022-05-25 15:12:53 -040030/* Hacks from splitting entity_manager.cpp */
Ed Tanousfc171422024-04-04 17:18:16 -070031// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103032extern std::shared_ptr<sdbusplus::asio::connection> systemBus;
33extern nlohmann::json lastJson;
Patrick Williams5a807032025-03-03 11:20:39 -050034extern void propertiesChangedCallback(
35 nlohmann::json& systemConfiguration,
36 sdbusplus::asio::object_server& objServer);
Ed Tanousfc171422024-04-04 17:18:16 -070037// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103038
Andrew Jeffery47af65a2021-12-01 14:16:31 +103039using GetSubTreeType = std::vector<
40 std::pair<std::string,
41 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
42
43constexpr const int32_t maxMapperDepth = 0;
44
Andrew Jefferyd9b67842022-04-05 16:44:20 +093045struct DBusInterfaceInstance
46{
47 std::string busName;
48 std::string path;
49 std::string interface;
50};
51
Patrick Williams5a807032025-03-03 11:20:39 -050052void getInterfaces(
53 const DBusInterfaceInstance& instance,
Christopher Meis26fbbd52025-03-26 14:55:06 +010054 const std::vector<std::shared_ptr<probe::PerformProbe>>& probeVector,
55 const std::shared_ptr<scan::PerformScan>& scan, size_t retries = 5)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103056{
Ed Tanous3013fb42022-07-09 08:27:06 -070057 if (retries == 0U)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103058 {
Andrew Jefferyd9b67842022-04-05 16:44:20 +093059 std::cerr << "retries exhausted on " << instance.busName << " "
60 << instance.path << " " << instance.interface << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +103061 return;
62 }
63
64 systemBus->async_method_call(
Patrick Williamsb7077432024-08-16 15:22:21 -040065 [instance, scan, probeVector,
66 retries](boost::system::error_code& errc, const DBusInterface& resp) {
67 if (errc)
68 {
69 std::cerr << "error calling getall on " << instance.busName
70 << " " << instance.path << " "
71 << instance.interface << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +103072
Patrick Williamsb7077432024-08-16 15:22:21 -040073 auto timer = std::make_shared<boost::asio::steady_timer>(io);
74 timer->expires_after(std::chrono::seconds(2));
Andrew Jeffery47af65a2021-12-01 14:16:31 +103075
Patrick Williamsb7077432024-08-16 15:22:21 -040076 timer->async_wait([timer, instance, scan, probeVector,
77 retries](const boost::system::error_code&) {
78 getInterfaces(instance, probeVector, scan, retries - 1);
79 });
80 return;
81 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +103082
Patrick Williamsb7077432024-08-16 15:22:21 -040083 scan->dbusProbeObjects[instance.path][instance.interface] = resp;
84 },
Andrew Jefferyd9b67842022-04-05 16:44:20 +093085 instance.busName, instance.path, "org.freedesktop.DBus.Properties",
86 "GetAll", instance.interface);
Andrew Jeffery47af65a2021-12-01 14:16:31 +103087}
88
Andrew Jeffery4dcc55d2022-04-05 16:49:42 +093089static void registerCallback(nlohmann::json& systemConfiguration,
90 sdbusplus::asio::object_server& objServer,
91 const std::string& path)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103092{
Patrick Williams2af39222022-07-22 19:26:56 -050093 static boost::container::flat_map<std::string, sdbusplus::bus::match_t>
Andrew Jeffery47af65a2021-12-01 14:16:31 +103094 dbusMatches;
95
96 auto find = dbusMatches.find(path);
97 if (find != dbusMatches.end())
98 {
99 return;
100 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030101
Patrick Williams2af39222022-07-22 19:26:56 -0500102 std::function<void(sdbusplus::message_t & message)> eventHandler =
103 [&](sdbusplus::message_t&) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400104 propertiesChangedCallback(systemConfiguration, objServer);
105 };
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030106
Patrick Williams2af39222022-07-22 19:26:56 -0500107 sdbusplus::bus::match_t match(
108 static_cast<sdbusplus::bus_t&>(*systemBus),
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030109 "type='signal',member='PropertiesChanged',path='" + path + "'",
110 eventHandler);
111 dbusMatches.emplace(path, std::move(match));
112}
113
Patrick Williams5a807032025-03-03 11:20:39 -0500114static void processDbusObjects(
Christopher Meis26fbbd52025-03-26 14:55:06 +0100115 std::vector<std::shared_ptr<probe::PerformProbe>>& probeVector,
116 const std::shared_ptr<scan::PerformScan>& scan,
Patrick Williams5a807032025-03-03 11:20:39 -0500117 const GetSubTreeType& interfaceSubtree)
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930118{
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930119 for (const auto& [path, object] : interfaceSubtree)
120 {
Andrew Jeffery47321832022-04-05 16:30:29 +0930121 // Get a PropertiesChanged callback for all interfaces on this path.
122 registerCallback(scan->_systemConfiguration, scan->objServer, path);
123
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930124 for (const auto& [busname, ifaces] : object)
125 {
126 for (const std::string& iface : ifaces)
127 {
128 // The 3 default org.freedeskstop interfaces (Peer,
129 // Introspectable, and Properties) are returned by
130 // the mapper but don't have properties, so don't bother
131 // with the GetAll call to save some cycles.
132 if (!boost::algorithm::starts_with(iface, "org.freedesktop"))
133 {
Andrew Jeffery47321832022-04-05 16:30:29 +0930134 getInterfaces({busname, path, iface}, probeVector, scan);
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930135 }
136 }
137 }
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930138 }
139}
140
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030141// Populates scan->dbusProbeObjects with all interfaces and properties
142// for the paths that own the interfaces passed in.
Christopher Meis26fbbd52025-03-26 14:55:06 +0100143void findDbusObjects(
144 std::vector<std::shared_ptr<probe::PerformProbe>>&& probeVector,
145 boost::container::flat_set<std::string>&& interfaces,
146 const std::shared_ptr<scan::PerformScan>& scan, size_t retries = 5)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030147{
148 // Filter out interfaces already obtained.
149 for (const auto& [path, probeInterfaces] : scan->dbusProbeObjects)
150 {
151 for (const auto& [interface, _] : probeInterfaces)
152 {
153 interfaces.erase(interface);
154 }
155 }
156 if (interfaces.empty())
157 {
158 return;
159 }
160
161 // find all connections in the mapper that expose a specific type
162 systemBus->async_method_call(
163 [interfaces, probeVector{std::move(probeVector)}, scan,
164 retries](boost::system::error_code& ec,
165 const GetSubTreeType& interfaceSubtree) mutable {
Patrick Williamsb7077432024-08-16 15:22:21 -0400166 if (ec)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030167 {
Patrick Williamsb7077432024-08-16 15:22:21 -0400168 if (ec.value() == ENOENT)
169 {
170 return; // wasn't found by mapper
171 }
172 std::cerr << "Error communicating to mapper.\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030173
Patrick Williamsb7077432024-08-16 15:22:21 -0400174 if (retries == 0U)
175 {
176 // if we can't communicate to the mapper something is very
177 // wrong
178 std::exit(EXIT_FAILURE);
179 }
180
181 auto timer = std::make_shared<boost::asio::steady_timer>(io);
182 timer->expires_after(std::chrono::seconds(10));
183
184 timer->async_wait(
185 [timer, interfaces{std::move(interfaces)}, scan,
186 probeVector{std::move(probeVector)},
187 retries](const boost::system::error_code&) mutable {
188 findDbusObjects(std::move(probeVector),
189 std::move(interfaces), scan,
190 retries - 1);
191 });
192 return;
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030193 }
194
Patrick Williamsb7077432024-08-16 15:22:21 -0400195 processDbusObjects(probeVector, scan, interfaceSubtree);
196 },
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030197 "xyz.openbmc_project.ObjectMapper",
198 "/xyz/openbmc_project/object_mapper",
199 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", maxMapperDepth,
200 interfaces);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030201}
202
Andrew Jeffery09a09a62022-04-12 22:49:57 +0930203static std::string getRecordName(const DBusInterface& probe,
204 const std::string& probeName)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030205{
206 if (probe.empty())
207 {
208 return probeName;
209 }
210
Andrew Jeffery928c5b22022-04-05 16:57:51 +0930211 // use an array so alphabetical order from the flat_map is maintained
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030212 auto device = nlohmann::json::array();
Ed Tanous3013fb42022-07-09 08:27:06 -0700213 for (const auto& devPair : probe)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030214 {
215 device.push_back(devPair.first);
216 std::visit([&device](auto&& v) { device.push_back(v); },
217 devPair.second);
218 }
Andrew Jeffery86501872022-04-05 16:58:22 +0930219
Andrew Jeffery928c5b22022-04-05 16:57:51 +0930220 // hashes are hard to distinguish, use the non-hashed version if we want
221 // debug
Alexander Hansenc3db2c32024-08-20 15:01:38 +0200222 // return probeName + device.dump();
Andrew Jeffery1ae4ed62022-04-05 16:55:43 +0930223
Andrew Jeffery86501872022-04-05 16:58:22 +0930224 return std::to_string(std::hash<std::string>{}(probeName + device.dump()));
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030225}
226
Christopher Meis26fbbd52025-03-26 14:55:06 +0100227scan::PerformScan::PerformScan(nlohmann::json& systemConfiguration,
228 nlohmann::json& missingConfigurations,
229 std::list<nlohmann::json>& configurations,
230 sdbusplus::asio::object_server& objServerIn,
231 std::function<void()>&& callback) :
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030232 _systemConfiguration(systemConfiguration),
233 _missingConfigurations(missingConfigurations),
234 _configurations(configurations), objServer(objServerIn),
235 _callback(std::move(callback))
236{}
Andrew Jefferydb451482022-04-13 16:53:22 +0930237
Andrew Jeffery7a7faed2022-04-13 17:24:56 +0930238static void pruneRecordExposes(nlohmann::json& record)
Andrew Jefferydb451482022-04-13 16:53:22 +0930239{
Andrew Jeffery7a7faed2022-04-13 17:24:56 +0930240 auto findExposes = record.find("Exposes");
Andrew Jeffery5f051452022-04-13 17:11:37 +0930241 if (findExposes == record.end())
Andrew Jefferydb451482022-04-13 16:53:22 +0930242 {
Andrew Jeffery5f051452022-04-13 17:11:37 +0930243 return;
Andrew Jefferydb451482022-04-13 16:53:22 +0930244 }
Andrew Jeffery5f051452022-04-13 17:11:37 +0930245
246 auto copy = nlohmann::json::array();
247 for (auto& expose : *findExposes)
248 {
Andrew Jeffery742a7eb2022-04-13 17:14:46 +0930249 if (!expose.is_null())
Andrew Jeffery5f051452022-04-13 17:11:37 +0930250 {
Andrew Jeffery742a7eb2022-04-13 17:14:46 +0930251 copy.emplace_back(expose);
Andrew Jeffery5f051452022-04-13 17:11:37 +0930252 }
Andrew Jeffery5f051452022-04-13 17:11:37 +0930253 }
254 *findExposes = copy;
Andrew Jefferydb451482022-04-13 16:53:22 +0930255}
256
Patrick Williamsb7077432024-08-16 15:22:21 -0400257static void recordDiscoveredIdentifiers(
258 std::set<nlohmann::json>& usedNames, std::list<size_t>& indexes,
259 const std::string& probeName, const nlohmann::json& record)
Andrew Jeffery6addc022022-04-13 18:08:58 +0930260{
261 size_t indexIdx = probeName.find('$');
Andrew Jefferyba41b622022-04-13 18:13:28 +0930262 if (indexIdx == std::string::npos)
Andrew Jeffery6addc022022-04-13 18:08:58 +0930263 {
264 return;
265 }
266
Andrew Jefferyc0da0cc2022-04-13 18:15:09 +0930267 auto nameIt = record.find("Name");
268 if (nameIt == record.end())
Andrew Jeffery6addc022022-04-13 18:08:58 +0930269 {
270 std::cerr << "Last JSON Illegal\n";
271 return;
272 }
273
274 int index = 0;
275 auto str = nameIt->get<std::string>().substr(indexIdx);
Ed Tanous3013fb42022-07-09 08:27:06 -0700276 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
277 const char* endPtr = str.data() + str.size();
278 auto [p, ec] = std::from_chars(str.data(), endPtr, index);
Andrew Jeffery6addc022022-04-13 18:08:58 +0930279 if (ec != std::errc())
280 {
281 return; // non-numeric replacement
282 }
283
284 usedNames.insert(nameIt.value());
285
286 auto usedIt = std::find(indexes.begin(), indexes.end(), index);
287 if (usedIt != indexes.end())
288 {
289 indexes.erase(usedIt);
290 }
291}
292
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930293static bool extractExposeActionRecordNames(std::vector<std::string>& matches,
294 nlohmann::json::iterator& keyPair)
295{
Andrew Jefferya2449842022-04-14 15:35:51 +0930296 if (keyPair.value().is_string())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930297 {
298 matches.emplace_back(keyPair.value());
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930299 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930300 }
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930301
Andrew Jefferya2449842022-04-14 15:35:51 +0930302 if (keyPair.value().is_array())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930303 {
304 for (const auto& value : keyPair.value())
305 {
Andrew Jefferya2449842022-04-14 15:35:51 +0930306 if (!value.is_string())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930307 {
308 std::cerr << "Value is invalid type " << value << "\n";
309 break;
310 }
311 matches.emplace_back(value);
312 }
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930313
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930314 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930315 }
316
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930317 std::cerr << "Value is invalid type " << keyPair.key() << "\n";
318
319 return false;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930320}
321
Patrick Williamsb7077432024-08-16 15:22:21 -0400322static std::optional<std::vector<std::string>::iterator> findExposeActionRecord(
323 std::vector<std::string>& matches, const nlohmann::json& record)
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930324{
Andrew Jeffery1a02da82022-04-14 20:39:06 +0930325 const auto& name = (record)["Name"].get_ref<const std::string&>();
326 auto compare = [&name](const std::string& s) { return s == name; };
327 auto matchIt = std::find_if(matches.begin(), matches.end(), compare);
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930328
329 if (matchIt == matches.end())
330 {
331 return std::nullopt;
332 }
333
334 return matchIt;
335}
336
Andrew Jefferycf11bd32022-04-14 18:34:31 +0930337static void applyBindExposeAction(nlohmann::json& exposedObject,
338 nlohmann::json& expose,
339 const std::string& propertyName)
340{
341 if (boost::starts_with(propertyName, "Bind"))
342 {
343 std::string bind = propertyName.substr(sizeof("Bind") - 1);
344 exposedObject["Status"] = "okay";
345 expose[bind] = exposedObject;
346 }
347}
348
349static void applyDisableExposeAction(nlohmann::json& exposedObject,
350 const std::string& propertyName)
351{
352 if (propertyName == "DisableNode")
353 {
354 exposedObject["Status"] = "disabled";
355 }
356}
357
Patrick Williamsb7077432024-08-16 15:22:21 -0400358static void applyConfigExposeActions(
359 std::vector<std::string>& matches, nlohmann::json& expose,
360 const std::string& propertyName, nlohmann::json& configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930361{
Andrew Jefferyda45e5e2022-04-14 21:12:02 +0930362 for (auto& exposedObject : configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930363 {
364 auto match = findExposeActionRecord(matches, exposedObject);
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930365 if (match)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930366 {
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930367 matches.erase(*match);
368 applyBindExposeAction(exposedObject, expose, propertyName);
369 applyDisableExposeAction(exposedObject, propertyName);
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930370 }
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930371 }
372}
373
Patrick Williamsb7077432024-08-16 15:22:21 -0400374static void applyExposeActions(
375 nlohmann::json& systemConfiguration, const std::string& recordName,
376 nlohmann::json& expose, nlohmann::json::iterator& keyPair)
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930377{
378 bool isBind = boost::starts_with(keyPair.key(), "Bind");
379 bool isDisable = keyPair.key() == "DisableNode";
380 bool isExposeAction = isBind || isDisable;
381
382 if (!isExposeAction)
383 {
384 return;
385 }
386
387 std::vector<std::string> matches;
388
389 if (!extractExposeActionRecordNames(matches, keyPair))
390 {
391 return;
392 }
393
Patrick Williams2594d362022-09-28 06:46:24 -0500394 for (const auto& [configId, config] : systemConfiguration.items())
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930395 {
396 // don't disable ourselves
397 if (isDisable && configId == recordName)
398 {
399 continue;
400 }
401
402 auto configListFind = config.find("Exposes");
403 if (configListFind == config.end())
404 {
405 continue;
406 }
407
408 if (!configListFind->is_array())
409 {
410 continue;
411 }
412
413 applyConfigExposeActions(matches, expose, keyPair.key(),
414 *configListFind);
415 }
416
417 if (!matches.empty())
418 {
419 std::cerr << "configuration file dependency error, could not find "
420 << keyPair.key() << " " << keyPair.value() << "\n";
421 }
422}
423
Patrick Williamsb7077432024-08-16 15:22:21 -0400424static std::string generateDeviceName(
425 const std::set<nlohmann::json>& usedNames, const DBusObject& dbusObject,
426 size_t foundDeviceIdx, const std::string& nameTemplate,
427 std::optional<std::string>& replaceStr)
Andrew Jefferydabee982022-04-19 13:27:24 +0930428{
429 nlohmann::json copyForName = {{"Name", nameTemplate}};
430 nlohmann::json::iterator copyIt = copyForName.begin();
431 std::optional<std::string> replaceVal =
432 templateCharReplace(copyIt, dbusObject, foundDeviceIdx, replaceStr);
433
434 if (!replaceStr && replaceVal)
435 {
436 if (usedNames.find(copyIt.value()) != usedNames.end())
437 {
438 replaceStr = replaceVal;
439 copyForName = {{"Name", nameTemplate}};
440 copyIt = copyForName.begin();
441 templateCharReplace(copyIt, dbusObject, foundDeviceIdx, replaceStr);
442 }
443 }
444
445 if (replaceStr)
446 {
447 std::cerr << "Duplicates found, replacing " << *replaceStr
448 << " with found device index.\n Consider "
449 "fixing template to not have duplicates\n";
450 }
451
452 return copyIt.value();
453}
454
Christopher Meis26fbbd52025-03-26 14:55:06 +0100455void scan::PerformScan::updateSystemConfiguration(
456 const nlohmann::json& recordRef, const std::string& probeName,
457 FoundDevices& foundDevices)
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930458{
459 _passed = true;
460 passedProbes.push_back(probeName);
461
462 std::set<nlohmann::json> usedNames;
463 std::list<size_t> indexes(foundDevices.size());
464 std::iota(indexes.begin(), indexes.end(), 1);
465
466 // copy over persisted configurations and make sure we remove
467 // indexes that are already used
468 for (auto itr = foundDevices.begin(); itr != foundDevices.end();)
469 {
470 std::string recordName = getRecordName(itr->interface, probeName);
471
Zhikui Ren1f77d9c2022-08-29 16:02:37 -0700472 auto record = _systemConfiguration.find(recordName);
473 if (record == _systemConfiguration.end())
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930474 {
Zhikui Ren1f77d9c2022-08-29 16:02:37 -0700475 record = lastJson.find(recordName);
476 if (record == lastJson.end())
477 {
478 itr++;
479 continue;
480 }
481
482 pruneRecordExposes(*record);
483
Zhikui Ren1f77d9c2022-08-29 16:02:37 -0700484 _systemConfiguration[recordName] = *record;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930485 }
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930486 _missingConfigurations.erase(recordName);
487
488 // We've processed the device, remove it and advance the
489 // iterator
490 itr = foundDevices.erase(itr);
JinFuLin8f05a3a2022-11-21 15:33:24 +0800491 recordDiscoveredIdentifiers(usedNames, indexes, probeName, *record);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930492 }
493
494 std::optional<std::string> replaceStr;
495
496 DBusObject emptyObject;
497 DBusInterface emptyInterface;
498 emptyObject.emplace(std::string{}, emptyInterface);
499
500 for (const auto& [foundDevice, path] : foundDevices)
501 {
502 // Need all interfaces on this path so that template
503 // substitutions can be done with any of the contained
504 // properties. If the probe that passed didn't use an
505 // interface, such as if it was just TRUE, then
506 // templateCharReplace will just get passed in an empty
507 // map.
Andrew Jefferyaf9b46b2022-04-21 12:34:43 +0930508 auto objectIt = dbusProbeObjects.find(path);
509 const DBusObject& dbusObject = (objectIt == dbusProbeObjects.end())
510 ? emptyObject
511 : objectIt->second;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930512
513 nlohmann::json record = recordRef;
514 std::string recordName = getRecordName(foundDevice, probeName);
515 size_t foundDeviceIdx = indexes.front();
516 indexes.pop_front();
517
518 // check name first so we have no duplicate names
519 auto getName = record.find("Name");
520 if (getName == record.end())
521 {
522 std::cerr << "Record Missing Name! " << record.dump();
523 continue; // this should be impossible at this level
524 }
525
526 std::string deviceName = generateDeviceName(
527 usedNames, dbusObject, foundDeviceIdx, getName.value(), replaceStr);
528 getName.value() = deviceName;
529 usedNames.insert(deviceName);
530
531 for (auto keyPair = record.begin(); keyPair != record.end(); keyPair++)
532 {
533 if (keyPair.key() != "Name")
534 {
535 templateCharReplace(keyPair, dbusObject, foundDeviceIdx,
536 replaceStr);
537 }
538 }
539
540 // insert into configuration temporarily to be able to
541 // reference ourselves
542
543 _systemConfiguration[recordName] = record;
544
545 auto findExpose = record.find("Exposes");
546 if (findExpose == record.end())
547 {
548 continue;
549 }
550
551 for (auto& expose : *findExpose)
552 {
553 for (auto keyPair = expose.begin(); keyPair != expose.end();
554 keyPair++)
555 {
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930556 templateCharReplace(keyPair, dbusObject, foundDeviceIdx,
557 replaceStr);
558
559 applyExposeActions(_systemConfiguration, recordName, expose,
560 keyPair);
561 }
562 }
563
564 // overwrite ourselves with cleaned up version
565 _systemConfiguration[recordName] = record;
566 _missingConfigurations.erase(recordName);
567 }
568}
569
Christopher Meis26fbbd52025-03-26 14:55:06 +0100570void scan::PerformScan::run()
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030571{
572 boost::container::flat_set<std::string> dbusProbeInterfaces;
Christopher Meis26fbbd52025-03-26 14:55:06 +0100573 std::vector<std::shared_ptr<probe::PerformProbe>> dbusProbePointers;
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030574
575 for (auto it = _configurations.begin(); it != _configurations.end();)
576 {
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030577 // check for poorly formatted fields, probe must be an array
Andrew Jeffery38834872022-04-19 15:15:57 +0930578 auto findProbe = it->find("Probe");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030579 if (findProbe == it->end())
580 {
581 std::cerr << "configuration file missing probe:\n " << *it << "\n";
582 it = _configurations.erase(it);
583 continue;
584 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030585
Andrew Jeffery38834872022-04-19 15:15:57 +0930586 auto findName = it->find("Name");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030587 if (findName == it->end())
588 {
589 std::cerr << "configuration file missing name:\n " << *it << "\n";
590 it = _configurations.erase(it);
591 continue;
592 }
593 std::string probeName = *findName;
594
595 if (std::find(passedProbes.begin(), passedProbes.end(), probeName) !=
596 passedProbes.end())
597 {
598 it = _configurations.erase(it);
599 continue;
600 }
Andrew Jeffery38834872022-04-19 15:15:57 +0930601
Andrew Jeffery98f3d532022-04-19 15:29:22 +0930602 nlohmann::json& recordRef = *it;
Andrew Jeffery38834872022-04-19 15:15:57 +0930603 nlohmann::json probeCommand;
604 if ((*findProbe).type() != nlohmann::json::value_t::array)
605 {
606 probeCommand = nlohmann::json::array();
607 probeCommand.push_back(*findProbe);
608 }
609 else
610 {
611 probeCommand = *findProbe;
612 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030613
614 // store reference to this to children to makes sure we don't get
615 // destroyed too early
616 auto thisRef = shared_from_this();
Christopher Meis26fbbd52025-03-26 14:55:06 +0100617 auto probePointer = std::make_shared<probe::PerformProbe>(
Andrew Jefferyea4ff022022-04-21 12:31:40 +0930618 recordRef, probeCommand, probeName, thisRef);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030619
620 // parse out dbus probes by discarding other probe types, store in a
621 // map
622 for (const nlohmann::json& probeJson : probeCommand)
623 {
624 const std::string* probe = probeJson.get_ptr<const std::string*>();
625 if (probe == nullptr)
626 {
627 std::cerr << "Probe statement wasn't a string, can't parse";
628 continue;
629 }
Christopher Meis26fbbd52025-03-26 14:55:06 +0100630 if (probe::findProbeType(*probe))
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030631 {
632 continue;
633 }
634 // syntax requires probe before first open brace
635 auto findStart = probe->find('(');
636 std::string interface = probe->substr(0, findStart);
637 dbusProbeInterfaces.emplace(interface);
638 dbusProbePointers.emplace_back(probePointer);
639 }
640 it++;
641 }
642
643 // probe vector stores a shared_ptr to each PerformProbe that cares
644 // about a dbus interface
645 findDbusObjects(std::move(dbusProbePointers),
646 std::move(dbusProbeInterfaces), shared_from_this());
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030647}
648
Christopher Meis26fbbd52025-03-26 14:55:06 +0100649scan::PerformScan::~PerformScan()
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030650{
651 if (_passed)
652 {
653 auto nextScan = std::make_shared<PerformScan>(
654 _systemConfiguration, _missingConfigurations, _configurations,
655 objServer, std::move(_callback));
656 nextScan->passedProbes = std::move(passedProbes);
657 nextScan->dbusProbeObjects = std::move(dbusProbeObjects);
658 nextScan->run();
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030659 }
660 else
661 {
662 _callback();
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030663 }
664}