blob: 0ad65099f1b808a8c9df02258739c947e58a7428 [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
17#include "entity_manager.hpp"
Andrew Jeffery47af65a2021-12-01 14:16:31 +103018
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
Brad Bishope45d8c72022-05-25 15:12:53 -040026/* Hacks from splitting entity_manager.cpp */
Andrew Jeffery47af65a2021-12-01 14:16:31 +103027extern 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{
Ed Tanous3013fb42022-07-09 08:27:06 -070053 if (retries == 0U)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103054 {
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 {
Ed Tanous3013fb42022-07-09 08:27:06 -070086 std::cerr << __LINE__ << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +103087 }
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{
Patrick Williams2af39222022-07-22 19:26:56 -050094 static boost::container::flat_map<std::string, sdbusplus::bus::match_t>
Andrew Jeffery47af65a2021-12-01 14:16:31 +103095 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
Patrick Williams2af39222022-07-22 19:26:56 -0500103 std::function<void(sdbusplus::message_t & message)> eventHandler =
104 [&](sdbusplus::message_t&) {
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030105 propertiesChangedCallback(systemConfiguration, objServer);
106 };
107
Patrick Williams2af39222022-07-22 19:26:56 -0500108 sdbusplus::bus::match_t match(
109 static_cast<sdbusplus::bus_t&>(*systemBus),
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030110 "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
Ed Tanous3013fb42022-07-09 08:27:06 -0700175 if (retries == 0U)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030176 {
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 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700205 std::cerr << __LINE__ << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030206 }
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();
Ed Tanous3013fb42022-07-09 08:27:06 -0700219 for (const auto& devPair : probe)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030220 {
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);
Ed Tanous3013fb42022-07-09 08:27:06 -0700286 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
287 const char* endPtr = str.data() + str.size();
288 auto [p, ec] = std::from_chars(str.data(), endPtr, index);
Andrew Jeffery6addc022022-04-13 18:08:58 +0930289 if (ec != std::errc())
290 {
291 return; // non-numeric replacement
292 }
293
294 usedNames.insert(nameIt.value());
295
296 auto usedIt = std::find(indexes.begin(), indexes.end(), index);
297 if (usedIt != indexes.end())
298 {
299 indexes.erase(usedIt);
300 }
301}
302
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930303static bool extractExposeActionRecordNames(std::vector<std::string>& matches,
304 nlohmann::json::iterator& keyPair)
305{
Andrew Jefferya2449842022-04-14 15:35:51 +0930306 if (keyPair.value().is_string())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930307 {
308 matches.emplace_back(keyPair.value());
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930309 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930310 }
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930311
Andrew Jefferya2449842022-04-14 15:35:51 +0930312 if (keyPair.value().is_array())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930313 {
314 for (const auto& value : keyPair.value())
315 {
Andrew Jefferya2449842022-04-14 15:35:51 +0930316 if (!value.is_string())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930317 {
318 std::cerr << "Value is invalid type " << value << "\n";
319 break;
320 }
321 matches.emplace_back(value);
322 }
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930323
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930324 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930325 }
326
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930327 std::cerr << "Value is invalid type " << keyPair.key() << "\n";
328
329 return false;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930330}
331
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930332static std::optional<std::vector<std::string>::iterator>
333 findExposeActionRecord(std::vector<std::string>& matches,
Andrew Jefferyd8213b92022-04-14 15:51:56 +0930334 const nlohmann::json& record)
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930335{
Andrew Jeffery1a02da82022-04-14 20:39:06 +0930336 const auto& name = (record)["Name"].get_ref<const std::string&>();
337 auto compare = [&name](const std::string& s) { return s == name; };
338 auto matchIt = std::find_if(matches.begin(), matches.end(), compare);
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930339
340 if (matchIt == matches.end())
341 {
342 return std::nullopt;
343 }
344
345 return matchIt;
346}
347
Andrew Jefferycf11bd32022-04-14 18:34:31 +0930348static void applyBindExposeAction(nlohmann::json& exposedObject,
349 nlohmann::json& expose,
350 const std::string& propertyName)
351{
352 if (boost::starts_with(propertyName, "Bind"))
353 {
354 std::string bind = propertyName.substr(sizeof("Bind") - 1);
355 exposedObject["Status"] = "okay";
356 expose[bind] = exposedObject;
357 }
358}
359
360static void applyDisableExposeAction(nlohmann::json& exposedObject,
361 const std::string& propertyName)
362{
363 if (propertyName == "DisableNode")
364 {
365 exposedObject["Status"] = "disabled";
366 }
367}
368
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930369static void applyConfigExposeActions(std::vector<std::string>& matches,
370 nlohmann::json& expose,
371 const std::string& propertyName,
Andrew Jefferyda45e5e2022-04-14 21:12:02 +0930372 nlohmann::json& configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930373{
Andrew Jefferyda45e5e2022-04-14 21:12:02 +0930374 for (auto& exposedObject : configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930375 {
376 auto match = findExposeActionRecord(matches, exposedObject);
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930377 if (match)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930378 {
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930379 matches.erase(*match);
380 applyBindExposeAction(exposedObject, expose, propertyName);
381 applyDisableExposeAction(exposedObject, propertyName);
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930382 }
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930383 }
384}
385
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930386static void applyExposeActions(nlohmann::json& systemConfiguration,
387 const std::string& recordName,
388 nlohmann::json& expose,
389 nlohmann::json::iterator& keyPair)
390{
391 bool isBind = boost::starts_with(keyPair.key(), "Bind");
392 bool isDisable = keyPair.key() == "DisableNode";
393 bool isExposeAction = isBind || isDisable;
394
395 if (!isExposeAction)
396 {
397 return;
398 }
399
400 std::vector<std::string> matches;
401
402 if (!extractExposeActionRecordNames(matches, keyPair))
403 {
404 return;
405 }
406
Patrick Williams2594d362022-09-28 06:46:24 -0500407 for (const auto& [configId, config] : systemConfiguration.items())
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930408 {
409 // don't disable ourselves
410 if (isDisable && configId == recordName)
411 {
412 continue;
413 }
414
415 auto configListFind = config.find("Exposes");
416 if (configListFind == config.end())
417 {
418 continue;
419 }
420
421 if (!configListFind->is_array())
422 {
423 continue;
424 }
425
426 applyConfigExposeActions(matches, expose, keyPair.key(),
427 *configListFind);
428 }
429
430 if (!matches.empty())
431 {
432 std::cerr << "configuration file dependency error, could not find "
433 << keyPair.key() << " " << keyPair.value() << "\n";
434 }
435}
436
Andrew Jefferydabee982022-04-19 13:27:24 +0930437static std::string generateDeviceName(const std::set<nlohmann::json>& usedNames,
438 const DBusObject& dbusObject,
439 size_t foundDeviceIdx,
440 const std::string& nameTemplate,
441 std::optional<std::string>& replaceStr)
442{
443 nlohmann::json copyForName = {{"Name", nameTemplate}};
444 nlohmann::json::iterator copyIt = copyForName.begin();
445 std::optional<std::string> replaceVal =
446 templateCharReplace(copyIt, dbusObject, foundDeviceIdx, replaceStr);
447
448 if (!replaceStr && replaceVal)
449 {
450 if (usedNames.find(copyIt.value()) != usedNames.end())
451 {
452 replaceStr = replaceVal;
453 copyForName = {{"Name", nameTemplate}};
454 copyIt = copyForName.begin();
455 templateCharReplace(copyIt, dbusObject, foundDeviceIdx, replaceStr);
456 }
457 }
458
459 if (replaceStr)
460 {
461 std::cerr << "Duplicates found, replacing " << *replaceStr
462 << " with found device index.\n Consider "
463 "fixing template to not have duplicates\n";
464 }
465
466 return copyIt.value();
467}
468
Andrew Jefferyaf9b46b2022-04-21 12:34:43 +0930469void PerformScan::updateSystemConfiguration(const nlohmann::json& recordRef,
470 const std::string& probeName,
471 FoundDevices& foundDevices)
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930472{
473 _passed = true;
474 passedProbes.push_back(probeName);
475
476 std::set<nlohmann::json> usedNames;
477 std::list<size_t> indexes(foundDevices.size());
478 std::iota(indexes.begin(), indexes.end(), 1);
479
480 // copy over persisted configurations and make sure we remove
481 // indexes that are already used
482 for (auto itr = foundDevices.begin(); itr != foundDevices.end();)
483 {
484 std::string recordName = getRecordName(itr->interface, probeName);
485
Zhikui Ren1f77d9c2022-08-29 16:02:37 -0700486 auto record = _systemConfiguration.find(recordName);
487 if (record == _systemConfiguration.end())
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930488 {
Zhikui Ren1f77d9c2022-08-29 16:02:37 -0700489 record = lastJson.find(recordName);
490 if (record == lastJson.end())
491 {
492 itr++;
493 continue;
494 }
495
496 pruneRecordExposes(*record);
497
Zhikui Ren1f77d9c2022-08-29 16:02:37 -0700498 _systemConfiguration[recordName] = *record;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930499 }
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930500 _missingConfigurations.erase(recordName);
501
502 // We've processed the device, remove it and advance the
503 // iterator
504 itr = foundDevices.erase(itr);
JinFuLin8f05a3a2022-11-21 15:33:24 +0800505 recordDiscoveredIdentifiers(usedNames, indexes, probeName, *record);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930506 }
507
508 std::optional<std::string> replaceStr;
509
510 DBusObject emptyObject;
511 DBusInterface emptyInterface;
512 emptyObject.emplace(std::string{}, emptyInterface);
513
514 for (const auto& [foundDevice, path] : foundDevices)
515 {
516 // Need all interfaces on this path so that template
517 // substitutions can be done with any of the contained
518 // properties. If the probe that passed didn't use an
519 // interface, such as if it was just TRUE, then
520 // templateCharReplace will just get passed in an empty
521 // map.
Andrew Jefferyaf9b46b2022-04-21 12:34:43 +0930522 auto objectIt = dbusProbeObjects.find(path);
523 const DBusObject& dbusObject = (objectIt == dbusProbeObjects.end())
524 ? emptyObject
525 : objectIt->second;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930526
527 nlohmann::json record = recordRef;
528 std::string recordName = getRecordName(foundDevice, probeName);
529 size_t foundDeviceIdx = indexes.front();
530 indexes.pop_front();
531
532 // check name first so we have no duplicate names
533 auto getName = record.find("Name");
534 if (getName == record.end())
535 {
536 std::cerr << "Record Missing Name! " << record.dump();
537 continue; // this should be impossible at this level
538 }
539
540 std::string deviceName = generateDeviceName(
541 usedNames, dbusObject, foundDeviceIdx, getName.value(), replaceStr);
542 getName.value() = deviceName;
543 usedNames.insert(deviceName);
544
545 for (auto keyPair = record.begin(); keyPair != record.end(); keyPair++)
546 {
547 if (keyPair.key() != "Name")
548 {
549 templateCharReplace(keyPair, dbusObject, foundDeviceIdx,
550 replaceStr);
551 }
552 }
553
554 // insert into configuration temporarily to be able to
555 // reference ourselves
556
557 _systemConfiguration[recordName] = record;
558
559 auto findExpose = record.find("Exposes");
560 if (findExpose == record.end())
561 {
562 continue;
563 }
564
565 for (auto& expose : *findExpose)
566 {
567 for (auto keyPair = expose.begin(); keyPair != expose.end();
568 keyPair++)
569 {
570
571 templateCharReplace(keyPair, dbusObject, foundDeviceIdx,
572 replaceStr);
573
574 applyExposeActions(_systemConfiguration, recordName, expose,
575 keyPair);
576 }
577 }
578
579 // overwrite ourselves with cleaned up version
580 _systemConfiguration[recordName] = record;
581 _missingConfigurations.erase(recordName);
582 }
583}
584
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030585void PerformScan::run()
586{
587 boost::container::flat_set<std::string> dbusProbeInterfaces;
588 std::vector<std::shared_ptr<PerformProbe>> dbusProbePointers;
589
590 for (auto it = _configurations.begin(); it != _configurations.end();)
591 {
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030592 // check for poorly formatted fields, probe must be an array
Andrew Jeffery38834872022-04-19 15:15:57 +0930593 auto findProbe = it->find("Probe");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030594 if (findProbe == it->end())
595 {
596 std::cerr << "configuration file missing probe:\n " << *it << "\n";
597 it = _configurations.erase(it);
598 continue;
599 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030600
Andrew Jeffery38834872022-04-19 15:15:57 +0930601 auto findName = it->find("Name");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030602 if (findName == it->end())
603 {
604 std::cerr << "configuration file missing name:\n " << *it << "\n";
605 it = _configurations.erase(it);
606 continue;
607 }
608 std::string probeName = *findName;
609
610 if (std::find(passedProbes.begin(), passedProbes.end(), probeName) !=
611 passedProbes.end())
612 {
613 it = _configurations.erase(it);
614 continue;
615 }
Andrew Jeffery38834872022-04-19 15:15:57 +0930616
Andrew Jeffery98f3d532022-04-19 15:29:22 +0930617 nlohmann::json& recordRef = *it;
Andrew Jeffery38834872022-04-19 15:15:57 +0930618 nlohmann::json probeCommand;
619 if ((*findProbe).type() != nlohmann::json::value_t::array)
620 {
621 probeCommand = nlohmann::json::array();
622 probeCommand.push_back(*findProbe);
623 }
624 else
625 {
626 probeCommand = *findProbe;
627 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030628
629 // store reference to this to children to makes sure we don't get
630 // destroyed too early
631 auto thisRef = shared_from_this();
632 auto probePointer = std::make_shared<PerformProbe>(
Andrew Jefferyea4ff022022-04-21 12:31:40 +0930633 recordRef, probeCommand, probeName, thisRef);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030634
635 // parse out dbus probes by discarding other probe types, store in a
636 // map
637 for (const nlohmann::json& probeJson : probeCommand)
638 {
639 const std::string* probe = probeJson.get_ptr<const std::string*>();
640 if (probe == nullptr)
641 {
642 std::cerr << "Probe statement wasn't a string, can't parse";
643 continue;
644 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700645 if (findProbeType(*probe))
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030646 {
647 continue;
648 }
649 // syntax requires probe before first open brace
650 auto findStart = probe->find('(');
651 std::string interface = probe->substr(0, findStart);
652 dbusProbeInterfaces.emplace(interface);
653 dbusProbePointers.emplace_back(probePointer);
654 }
655 it++;
656 }
657
658 // probe vector stores a shared_ptr to each PerformProbe that cares
659 // about a dbus interface
660 findDbusObjects(std::move(dbusProbePointers),
661 std::move(dbusProbeInterfaces), shared_from_this());
662 if constexpr (debug)
663 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700664 std::cerr << __LINE__ << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030665 }
666}
667
668PerformScan::~PerformScan()
669{
670 if (_passed)
671 {
672 auto nextScan = std::make_shared<PerformScan>(
673 _systemConfiguration, _missingConfigurations, _configurations,
674 objServer, std::move(_callback));
675 nextScan->passedProbes = std::move(passedProbes);
676 nextScan->dbusProbeObjects = std::move(dbusProbeObjects);
677 nextScan->run();
678
679 if constexpr (debug)
680 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700681 std::cerr << __LINE__ << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030682 }
683 }
684 else
685 {
686 _callback();
687
688 if constexpr (debug)
689 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700690 std::cerr << __LINE__ << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030691 }
692 }
693}