blob: 3c5160d648c4f1dc987092f8e50649ffebd1b505 [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*/
16/// \file PerformScan.cpp
17#include "EntityManager.hpp"
18
19#include <boost/algorithm/string/predicate.hpp>
20#include <boost/asio/steady_timer.hpp>
21#include <boost/container/flat_map.hpp>
22#include <boost/container/flat_set.hpp>
23
24#include <charconv>
25
26/* Hacks from splitting EntityManager.cpp */
27extern std::shared_ptr<sdbusplus::asio::connection> systemBus;
28extern nlohmann::json lastJson;
29extern void
30 propertiesChangedCallback(nlohmann::json& systemConfiguration,
31 sdbusplus::asio::object_server& objServer);
32
Andrew Jeffery47af65a2021-12-01 14:16:31 +103033using GetSubTreeType = std::vector<
34 std::pair<std::string,
35 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
36
37constexpr const int32_t maxMapperDepth = 0;
38
39constexpr const bool debug = false;
40
Andrew Jefferyd9b67842022-04-05 16:44:20 +093041struct DBusInterfaceInstance
42{
43 std::string busName;
44 std::string path;
45 std::string interface;
46};
47
Andrew Jeffery47af65a2021-12-01 14:16:31 +103048void getInterfaces(
Andrew Jefferyd9b67842022-04-05 16:44:20 +093049 const DBusInterfaceInstance& instance,
Andrew Jeffery47af65a2021-12-01 14:16:31 +103050 const std::vector<std::shared_ptr<PerformProbe>>& probeVector,
51 const std::shared_ptr<PerformScan>& scan, size_t retries = 5)
52{
53 if (!retries)
54 {
Andrew Jefferyd9b67842022-04-05 16:44:20 +093055 std::cerr << "retries exhausted on " << instance.busName << " "
56 << instance.path << " " << instance.interface << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +103057 return;
58 }
59
60 systemBus->async_method_call(
Andrew Jefferyd9b67842022-04-05 16:44:20 +093061 [instance, scan, probeVector, retries](boost::system::error_code& errc,
62 const DBusInterface& resp) {
Andrew Jeffery47af65a2021-12-01 14:16:31 +103063 if (errc)
64 {
Andrew Jefferyd9b67842022-04-05 16:44:20 +093065 std::cerr << "error calling getall on " << instance.busName
66 << " " << instance.path << " "
67 << instance.interface << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +103068
Andrew Jeffery91c5eaa2022-04-05 16:45:52 +093069 auto timer = std::make_shared<boost::asio::steady_timer>(io);
Andrew Jeffery47af65a2021-12-01 14:16:31 +103070 timer->expires_after(std::chrono::seconds(2));
71
Andrew Jefferyd9b67842022-04-05 16:44:20 +093072 timer->async_wait([timer, instance, scan, probeVector,
Andrew Jeffery47af65a2021-12-01 14:16:31 +103073 retries](const boost::system::error_code&) {
Andrew Jefferyd9b67842022-04-05 16:44:20 +093074 getInterfaces(instance, probeVector, scan, retries - 1);
Andrew Jeffery47af65a2021-12-01 14:16:31 +103075 });
76 return;
77 }
78
Andrew Jefferyd9b67842022-04-05 16:44:20 +093079 scan->dbusProbeObjects[instance.path][instance.interface] = resp;
Andrew Jeffery47af65a2021-12-01 14:16:31 +103080 },
Andrew Jefferyd9b67842022-04-05 16:44:20 +093081 instance.busName, instance.path, "org.freedesktop.DBus.Properties",
82 "GetAll", instance.interface);
Andrew Jeffery47af65a2021-12-01 14:16:31 +103083
84 if constexpr (debug)
85 {
86 std::cerr << __func__ << " " << __LINE__ << "\n";
87 }
88}
89
Andrew Jeffery4dcc55d2022-04-05 16:49:42 +093090static void registerCallback(nlohmann::json& systemConfiguration,
91 sdbusplus::asio::object_server& objServer,
92 const std::string& path)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103093{
94 static boost::container::flat_map<std::string, sdbusplus::bus::match::match>
95 dbusMatches;
96
97 auto find = dbusMatches.find(path);
98 if (find != dbusMatches.end())
99 {
100 return;
101 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030102
Andrew Jeffery26db5752022-04-05 16:53:36 +0930103 std::function<void(sdbusplus::message::message & message)> eventHandler =
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030104 [&](sdbusplus::message::message&) {
105 propertiesChangedCallback(systemConfiguration, objServer);
106 };
107
108 sdbusplus::bus::match::match match(
109 static_cast<sdbusplus::bus::bus&>(*systemBus),
110 "type='signal',member='PropertiesChanged',path='" + path + "'",
111 eventHandler);
112 dbusMatches.emplace(path, std::move(match));
113}
114
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930115static void
116 processDbusObjects(std::vector<std::shared_ptr<PerformProbe>>& probeVector,
117 const std::shared_ptr<PerformScan>& scan,
118 const GetSubTreeType& interfaceSubtree)
119{
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930120 for (const auto& [path, object] : interfaceSubtree)
121 {
Andrew Jeffery47321832022-04-05 16:30:29 +0930122 // Get a PropertiesChanged callback for all interfaces on this path.
123 registerCallback(scan->_systemConfiguration, scan->objServer, path);
124
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930125 for (const auto& [busname, ifaces] : object)
126 {
127 for (const std::string& iface : ifaces)
128 {
129 // The 3 default org.freedeskstop interfaces (Peer,
130 // Introspectable, and Properties) are returned by
131 // the mapper but don't have properties, so don't bother
132 // with the GetAll call to save some cycles.
133 if (!boost::algorithm::starts_with(iface, "org.freedesktop"))
134 {
Andrew Jeffery47321832022-04-05 16:30:29 +0930135 getInterfaces({busname, path, iface}, probeVector, scan);
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930136 }
137 }
138 }
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930139 }
140}
141
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030142// Populates scan->dbusProbeObjects with all interfaces and properties
143// for the paths that own the interfaces passed in.
144void findDbusObjects(std::vector<std::shared_ptr<PerformProbe>>&& probeVector,
145 boost::container::flat_set<std::string>&& interfaces,
146 const std::shared_ptr<PerformScan>& scan,
147 size_t retries = 5)
148{
149 // Filter out interfaces already obtained.
150 for (const auto& [path, probeInterfaces] : scan->dbusProbeObjects)
151 {
152 for (const auto& [interface, _] : probeInterfaces)
153 {
154 interfaces.erase(interface);
155 }
156 }
157 if (interfaces.empty())
158 {
159 return;
160 }
161
162 // find all connections in the mapper that expose a specific type
163 systemBus->async_method_call(
164 [interfaces, probeVector{std::move(probeVector)}, scan,
165 retries](boost::system::error_code& ec,
166 const GetSubTreeType& interfaceSubtree) mutable {
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030167 if (ec)
168 {
169 if (ec.value() == ENOENT)
170 {
171 return; // wasn't found by mapper
172 }
173 std::cerr << "Error communicating to mapper.\n";
174
175 if (!retries)
176 {
177 // if we can't communicate to the mapper something is very
178 // wrong
179 std::exit(EXIT_FAILURE);
180 }
Andrew Jeffery91c5eaa2022-04-05 16:45:52 +0930181
182 auto timer = std::make_shared<boost::asio::steady_timer>(io);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030183 timer->expires_after(std::chrono::seconds(10));
184
185 timer->async_wait(
186 [timer, interfaces{std::move(interfaces)}, scan,
187 probeVector{std::move(probeVector)},
188 retries](const boost::system::error_code&) mutable {
189 findDbusObjects(std::move(probeVector),
190 std::move(interfaces), scan,
191 retries - 1);
192 });
193 return;
194 }
195
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930196 processDbusObjects(probeVector, scan, interfaceSubtree);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030197 },
198 "xyz.openbmc_project.ObjectMapper",
199 "/xyz/openbmc_project/object_mapper",
200 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", maxMapperDepth,
201 interfaces);
202
203 if constexpr (debug)
204 {
205 std::cerr << __func__ << " " << __LINE__ << "\n";
206 }
207}
208
Andrew Jeffery09a09a62022-04-12 22:49:57 +0930209static std::string getRecordName(const DBusInterface& probe,
210 const std::string& probeName)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030211{
212 if (probe.empty())
213 {
214 return probeName;
215 }
216
Andrew Jeffery928c5b22022-04-05 16:57:51 +0930217 // use an array so alphabetical order from the flat_map is maintained
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030218 auto device = nlohmann::json::array();
219 for (auto& devPair : probe)
220 {
221 device.push_back(devPair.first);
222 std::visit([&device](auto&& v) { device.push_back(v); },
223 devPair.second);
224 }
Andrew Jeffery86501872022-04-05 16:58:22 +0930225
Andrew Jeffery928c5b22022-04-05 16:57:51 +0930226 // hashes are hard to distinguish, use the non-hashed version if we want
227 // debug
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030228 if constexpr (debug)
229 {
230 return probeName + device.dump();
231 }
Andrew Jeffery1ae4ed62022-04-05 16:55:43 +0930232
Andrew Jeffery86501872022-04-05 16:58:22 +0930233 return std::to_string(std::hash<std::string>{}(probeName + device.dump()));
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030234}
235
236PerformScan::PerformScan(nlohmann::json& systemConfiguration,
237 nlohmann::json& missingConfigurations,
238 std::list<nlohmann::json>& configurations,
239 sdbusplus::asio::object_server& objServerIn,
240 std::function<void()>&& callback) :
241 _systemConfiguration(systemConfiguration),
242 _missingConfigurations(missingConfigurations),
243 _configurations(configurations), objServer(objServerIn),
244 _callback(std::move(callback))
245{}
Andrew Jefferydb451482022-04-13 16:53:22 +0930246
Andrew Jeffery7a7faed2022-04-13 17:24:56 +0930247static void pruneRecordExposes(nlohmann::json& record)
Andrew Jefferydb451482022-04-13 16:53:22 +0930248{
Andrew Jeffery7a7faed2022-04-13 17:24:56 +0930249 auto findExposes = record.find("Exposes");
Andrew Jeffery5f051452022-04-13 17:11:37 +0930250 if (findExposes == record.end())
Andrew Jefferydb451482022-04-13 16:53:22 +0930251 {
Andrew Jeffery5f051452022-04-13 17:11:37 +0930252 return;
Andrew Jefferydb451482022-04-13 16:53:22 +0930253 }
Andrew Jeffery5f051452022-04-13 17:11:37 +0930254
255 auto copy = nlohmann::json::array();
256 for (auto& expose : *findExposes)
257 {
Andrew Jeffery742a7eb2022-04-13 17:14:46 +0930258 if (!expose.is_null())
Andrew Jeffery5f051452022-04-13 17:11:37 +0930259 {
Andrew Jeffery742a7eb2022-04-13 17:14:46 +0930260 copy.emplace_back(expose);
Andrew Jeffery5f051452022-04-13 17:11:37 +0930261 }
Andrew Jeffery5f051452022-04-13 17:11:37 +0930262 }
263 *findExposes = copy;
Andrew Jefferydb451482022-04-13 16:53:22 +0930264}
265
Andrew Jeffery6addc022022-04-13 18:08:58 +0930266static void recordDiscoveredIdentifiers(std::set<nlohmann::json>& usedNames,
267 std::list<size_t>& indexes,
268 const std::string& probeName,
Andrew Jefferyc0da0cc2022-04-13 18:15:09 +0930269 const nlohmann::json& record)
Andrew Jeffery6addc022022-04-13 18:08:58 +0930270{
271 size_t indexIdx = probeName.find('$');
Andrew Jefferyba41b622022-04-13 18:13:28 +0930272 if (indexIdx == std::string::npos)
Andrew Jeffery6addc022022-04-13 18:08:58 +0930273 {
274 return;
275 }
276
Andrew Jefferyc0da0cc2022-04-13 18:15:09 +0930277 auto nameIt = record.find("Name");
278 if (nameIt == record.end())
Andrew Jeffery6addc022022-04-13 18:08:58 +0930279 {
280 std::cerr << "Last JSON Illegal\n";
281 return;
282 }
283
284 int index = 0;
285 auto str = nameIt->get<std::string>().substr(indexIdx);
286 auto [p, ec] = std::from_chars(str.data(), str.data() + str.size(), index);
287 if (ec != std::errc())
288 {
289 return; // non-numeric replacement
290 }
291
292 usedNames.insert(nameIt.value());
293
294 auto usedIt = std::find(indexes.begin(), indexes.end(), index);
295 if (usedIt != indexes.end())
296 {
297 indexes.erase(usedIt);
298 }
299}
300
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930301static bool extractExposeActionRecordNames(std::vector<std::string>& matches,
302 nlohmann::json::iterator& keyPair)
303{
Andrew Jefferya2449842022-04-14 15:35:51 +0930304 if (keyPair.value().is_string())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930305 {
306 matches.emplace_back(keyPair.value());
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930307 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930308 }
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930309
Andrew Jefferya2449842022-04-14 15:35:51 +0930310 if (keyPair.value().is_array())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930311 {
312 for (const auto& value : keyPair.value())
313 {
Andrew Jefferya2449842022-04-14 15:35:51 +0930314 if (!value.is_string())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930315 {
316 std::cerr << "Value is invalid type " << value << "\n";
317 break;
318 }
319 matches.emplace_back(value);
320 }
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930321
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930322 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930323 }
324
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930325 std::cerr << "Value is invalid type " << keyPair.key() << "\n";
326
327 return false;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930328}
329
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930330static std::optional<std::vector<std::string>::iterator>
331 findExposeActionRecord(std::vector<std::string>& matches,
Andrew Jefferyd8213b92022-04-14 15:51:56 +0930332 const nlohmann::json& record)
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930333{
Andrew Jeffery1a02da82022-04-14 20:39:06 +0930334 const auto& name = (record)["Name"].get_ref<const std::string&>();
335 auto compare = [&name](const std::string& s) { return s == name; };
336 auto matchIt = std::find_if(matches.begin(), matches.end(), compare);
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930337
338 if (matchIt == matches.end())
339 {
340 return std::nullopt;
341 }
342
343 return matchIt;
344}
345
Andrew Jefferycf11bd32022-04-14 18:34:31 +0930346static void applyBindExposeAction(nlohmann::json& exposedObject,
347 nlohmann::json& expose,
348 const std::string& propertyName)
349{
350 if (boost::starts_with(propertyName, "Bind"))
351 {
352 std::string bind = propertyName.substr(sizeof("Bind") - 1);
353 exposedObject["Status"] = "okay";
354 expose[bind] = exposedObject;
355 }
356}
357
358static void applyDisableExposeAction(nlohmann::json& exposedObject,
359 const std::string& propertyName)
360{
361 if (propertyName == "DisableNode")
362 {
363 exposedObject["Status"] = "disabled";
364 }
365}
366
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930367static void applyConfigExposeActions(std::vector<std::string>& matches,
368 nlohmann::json& expose,
369 const std::string& propertyName,
Andrew Jefferyda45e5e2022-04-14 21:12:02 +0930370 nlohmann::json& configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930371{
Andrew Jefferyda45e5e2022-04-14 21:12:02 +0930372 for (auto& exposedObject : configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930373 {
374 auto match = findExposeActionRecord(matches, exposedObject);
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930375 if (match)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930376 {
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930377 matches.erase(*match);
378 applyBindExposeAction(exposedObject, expose, propertyName);
379 applyDisableExposeAction(exposedObject, propertyName);
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930380 }
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930381 }
382}
383
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930384static void applyExposeActions(nlohmann::json& systemConfiguration,
385 const std::string& recordName,
386 nlohmann::json& expose,
387 nlohmann::json::iterator& keyPair)
388{
389 bool isBind = boost::starts_with(keyPair.key(), "Bind");
390 bool isDisable = keyPair.key() == "DisableNode";
391 bool isExposeAction = isBind || isDisable;
392
393 if (!isExposeAction)
394 {
395 return;
396 }
397
398 std::vector<std::string> matches;
399
400 if (!extractExposeActionRecordNames(matches, keyPair))
401 {
402 return;
403 }
404
405 for (auto& [configId, config] : systemConfiguration.items())
406 {
407 // don't disable ourselves
408 if (isDisable && configId == recordName)
409 {
410 continue;
411 }
412
413 auto configListFind = config.find("Exposes");
414 if (configListFind == config.end())
415 {
416 continue;
417 }
418
419 if (!configListFind->is_array())
420 {
421 continue;
422 }
423
424 applyConfigExposeActions(matches, expose, keyPair.key(),
425 *configListFind);
426 }
427
428 if (!matches.empty())
429 {
430 std::cerr << "configuration file dependency error, could not find "
431 << keyPair.key() << " " << keyPair.value() << "\n";
432 }
433}
434
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030435void PerformScan::run()
436{
437 boost::container::flat_set<std::string> dbusProbeInterfaces;
438 std::vector<std::shared_ptr<PerformProbe>> dbusProbePointers;
439
440 for (auto it = _configurations.begin(); it != _configurations.end();)
441 {
442 auto findProbe = it->find("Probe");
443 auto findName = it->find("Name");
444
445 nlohmann::json probeCommand;
446 // check for poorly formatted fields, probe must be an array
447 if (findProbe == it->end())
448 {
449 std::cerr << "configuration file missing probe:\n " << *it << "\n";
450 it = _configurations.erase(it);
451 continue;
452 }
453 if ((*findProbe).type() != nlohmann::json::value_t::array)
454 {
455 probeCommand = nlohmann::json::array();
456 probeCommand.push_back(*findProbe);
457 }
458 else
459 {
460 probeCommand = *findProbe;
461 }
462
463 if (findName == it->end())
464 {
465 std::cerr << "configuration file missing name:\n " << *it << "\n";
466 it = _configurations.erase(it);
467 continue;
468 }
469 std::string probeName = *findName;
470
471 if (std::find(passedProbes.begin(), passedProbes.end(), probeName) !=
472 passedProbes.end())
473 {
474 it = _configurations.erase(it);
475 continue;
476 }
477 nlohmann::json* recordPtr = &(*it);
478
479 // store reference to this to children to makes sure we don't get
480 // destroyed too early
481 auto thisRef = shared_from_this();
482 auto probePointer = std::make_shared<PerformProbe>(
483 probeCommand, thisRef,
Andrew Jefferyac20bd92022-04-05 11:11:40 +0930484 [&, recordPtr,
Andrew Jefferyf5772d22022-04-12 22:05:30 +0930485 probeName](FoundDevices& foundDevices,
Andrew Jefferyef5c8432022-04-12 22:34:37 +0930486 const MapperGetSubTreeResponse& dbusSubtree) {
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030487 _passed = true;
488 std::set<nlohmann::json> usedNames;
489 passedProbes.push_back(probeName);
490 std::list<size_t> indexes(foundDevices.size());
491 std::iota(indexes.begin(), indexes.end(), 1);
492
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030493 // copy over persisted configurations and make sure we remove
494 // indexes that are already used
495 for (auto itr = foundDevices.begin();
496 itr != foundDevices.end();)
497 {
498 std::string recordName =
Andrew Jefferyf5772d22022-04-12 22:05:30 +0930499 getRecordName(itr->interface, probeName);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030500
Andrew Jefferyc1c769d2022-04-14 13:28:28 +0930501 auto record = lastJson.find(recordName);
502 if (record == lastJson.end())
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030503 {
Andrew Jeffery1d221752022-04-13 17:34:10 +0930504 itr++;
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030505 continue;
506 }
Andrew Jeffery1d221752022-04-13 17:34:10 +0930507
Andrew Jefferyc1c769d2022-04-14 13:28:28 +0930508 pruneRecordExposes(*record);
Andrew Jeffery1d221752022-04-13 17:34:10 +0930509
Andrew Jeffery42c9ea62022-04-13 18:25:39 +0930510 recordDiscoveredIdentifiers(usedNames, indexes, probeName,
Andrew Jefferyc1c769d2022-04-14 13:28:28 +0930511 *record);
Andrew Jeffery42c9ea62022-04-13 18:25:39 +0930512
Andrew Jeffery1d221752022-04-13 17:34:10 +0930513 // keep user changes
Andrew Jefferyc1c769d2022-04-14 13:28:28 +0930514 _systemConfiguration[recordName] = *record;
Andrew Jeffery1d221752022-04-13 17:34:10 +0930515 _missingConfigurations.erase(recordName);
516
517 // We've processed the device, remove it and advance the
518 // iterator
519 itr = foundDevices.erase(itr);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030520 }
521
522 std::optional<std::string> replaceStr;
523
Andrew Jeffery0a8de742022-04-12 22:32:01 +0930524 DBusObject emptyObject;
Andrew Jeffery1983d2f2022-04-05 14:55:13 +0930525 DBusInterface emptyInterface;
Andrew Jeffery0a8de742022-04-12 22:32:01 +0930526 emptyObject.emplace(std::string{}, emptyInterface);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030527
Andrew Jeffery920a2812022-04-12 22:17:32 +0930528 for (const auto& [foundDevice, path] : foundDevices)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030529 {
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030530 // Need all interfaces on this path so that template
531 // substitutions can be done with any of the contained
532 // properties. If the probe that passed didn't use an
533 // interface, such as if it was just TRUE, then
534 // templateCharReplace will just get passed in an empty
535 // map.
Andrew Jefferyd60b1472022-04-12 23:21:25 +0930536 auto objectIt = dbusSubtree.find(path);
Andrew Jefferyd45b1e72022-04-12 22:47:14 +0930537 const DBusObject& dbusObject =
Andrew Jefferyd60b1472022-04-12 23:21:25 +0930538 (objectIt == dbusSubtree.end()) ? emptyObject
539 : objectIt->second;
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030540
541 nlohmann::json record = *recordPtr;
542 std::string recordName =
543 getRecordName(foundDevice, probeName);
544 size_t foundDeviceIdx = indexes.front();
545 indexes.pop_front();
546
547 // check name first so we have no duplicate names
548 auto getName = record.find("Name");
549 if (getName == record.end())
550 {
551 std::cerr << "Record Missing Name! " << record.dump();
552 continue; // this should be impossible at this level
553 }
554
555 nlohmann::json copyForName = {{"Name", getName.value()}};
556 nlohmann::json::iterator copyIt = copyForName.begin();
Andrew Jefferyc6558f62022-04-05 15:39:31 +0930557 std::optional<std::string> replaceVal = templateCharReplace(
Andrew Jefferyd45b1e72022-04-12 22:47:14 +0930558 copyIt, dbusObject, foundDeviceIdx, replaceStr);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030559
560 if (!replaceStr && replaceVal)
561 {
562 if (usedNames.find(copyIt.value()) != usedNames.end())
563 {
564 replaceStr = replaceVal;
565 copyForName = {{"Name", getName.value()}};
566 copyIt = copyForName.begin();
Andrew Jefferyd45b1e72022-04-12 22:47:14 +0930567 templateCharReplace(copyIt, dbusObject,
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030568 foundDeviceIdx, replaceStr);
569 }
570 }
571
572 if (replaceStr)
573 {
574 std::cerr << "Duplicates found, replacing "
575 << *replaceStr
576 << " with found device index.\n Consider "
577 "fixing template to not have duplicates\n";
578 }
579
Andrew Jefferyd53ee412022-04-12 23:11:59 +0930580 getName.value() = copyIt.value();
581 usedNames.insert(copyIt.value());
582
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030583 for (auto keyPair = record.begin(); keyPair != record.end();
584 keyPair++)
585 {
Andrew Jefferyd53ee412022-04-12 23:11:59 +0930586 if (keyPair.key() != "Name")
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030587 {
Andrew Jefferyd53ee412022-04-12 23:11:59 +0930588 templateCharReplace(keyPair, dbusObject,
589 foundDeviceIdx, replaceStr);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030590 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030591 }
592
593 // insert into configuration temporarily to be able to
594 // reference ourselves
595
596 _systemConfiguration[recordName] = record;
597
598 auto findExpose = record.find("Exposes");
599 if (findExpose == record.end())
600 {
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030601 continue;
602 }
603
604 for (auto& expose : *findExpose)
605 {
606 for (auto keyPair = expose.begin();
607 keyPair != expose.end(); keyPair++)
608 {
609
Andrew Jefferyd45b1e72022-04-12 22:47:14 +0930610 templateCharReplace(keyPair, dbusObject,
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030611 foundDeviceIdx, replaceStr);
612
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930613 applyExposeActions(_systemConfiguration, recordName,
614 expose, keyPair);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030615 }
616 }
617 // overwrite ourselves with cleaned up version
618 _systemConfiguration[recordName] = record;
619 _missingConfigurations.erase(recordName);
620 }
621 });
622
623 // parse out dbus probes by discarding other probe types, store in a
624 // map
625 for (const nlohmann::json& probeJson : probeCommand)
626 {
627 const std::string* probe = probeJson.get_ptr<const std::string*>();
628 if (probe == nullptr)
629 {
630 std::cerr << "Probe statement wasn't a string, can't parse";
631 continue;
632 }
Andrew Jeffery666583b2021-12-01 15:50:12 +1030633 if (findProbeType(probe->c_str()))
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030634 {
635 continue;
636 }
637 // syntax requires probe before first open brace
638 auto findStart = probe->find('(');
639 std::string interface = probe->substr(0, findStart);
640 dbusProbeInterfaces.emplace(interface);
641 dbusProbePointers.emplace_back(probePointer);
642 }
643 it++;
644 }
645
646 // probe vector stores a shared_ptr to each PerformProbe that cares
647 // about a dbus interface
648 findDbusObjects(std::move(dbusProbePointers),
649 std::move(dbusProbeInterfaces), shared_from_this());
650 if constexpr (debug)
651 {
652 std::cerr << __func__ << " " << __LINE__ << "\n";
653 }
654}
655
656PerformScan::~PerformScan()
657{
658 if (_passed)
659 {
660 auto nextScan = std::make_shared<PerformScan>(
661 _systemConfiguration, _missingConfigurations, _configurations,
662 objServer, std::move(_callback));
663 nextScan->passedProbes = std::move(passedProbes);
664 nextScan->dbusProbeObjects = std::move(dbusProbeObjects);
665 nextScan->run();
666
667 if constexpr (debug)
668 {
669 std::cerr << __func__ << " " << __LINE__ << "\n";
670 }
671 }
672 else
673 {
674 _callback();
675
676 if constexpr (debug)
677 {
678 std::cerr << __func__ << " " << __LINE__ << "\n";
679 }
680 }
681}