blob: c503d99b257eee8db62677a0319beb0a2a6643ee [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 */
Ed Tanousfc171422024-04-04 17:18:16 -070027// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103028extern std::shared_ptr<sdbusplus::asio::connection> systemBus;
29extern nlohmann::json lastJson;
30extern void
31 propertiesChangedCallback(nlohmann::json& systemConfiguration,
32 sdbusplus::asio::object_server& objServer);
Ed Tanousfc171422024-04-04 17:18:16 -070033// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103034
Andrew Jeffery47af65a2021-12-01 14:16:31 +103035using GetSubTreeType = std::vector<
36 std::pair<std::string,
37 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
38
39constexpr const int32_t maxMapperDepth = 0;
40
41constexpr const bool debug = false;
42
Andrew Jefferyd9b67842022-04-05 16:44:20 +093043struct DBusInterfaceInstance
44{
45 std::string busName;
46 std::string path;
47 std::string interface;
48};
49
Andrew Jeffery47af65a2021-12-01 14:16:31 +103050void getInterfaces(
Andrew Jefferyd9b67842022-04-05 16:44:20 +093051 const DBusInterfaceInstance& instance,
Andrew Jeffery47af65a2021-12-01 14:16:31 +103052 const std::vector<std::shared_ptr<PerformProbe>>& probeVector,
53 const std::shared_ptr<PerformScan>& scan, size_t retries = 5)
54{
Ed Tanous3013fb42022-07-09 08:27:06 -070055 if (retries == 0U)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103056 {
Andrew Jefferyd9b67842022-04-05 16:44:20 +093057 std::cerr << "retries exhausted on " << instance.busName << " "
58 << instance.path << " " << instance.interface << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +103059 return;
60 }
61
62 systemBus->async_method_call(
Andrew Jefferyd9b67842022-04-05 16:44:20 +093063 [instance, scan, probeVector, retries](boost::system::error_code& errc,
64 const DBusInterface& resp) {
Patrick Williamsdf190612023-05-10 07:51:34 -050065 if (errc)
66 {
67 std::cerr << "error calling getall on " << instance.busName << " "
68 << instance.path << " " << instance.interface << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +103069
Patrick Williamsdf190612023-05-10 07:51:34 -050070 auto timer = std::make_shared<boost::asio::steady_timer>(io);
71 timer->expires_after(std::chrono::seconds(2));
Andrew Jeffery47af65a2021-12-01 14:16:31 +103072
Patrick Williamsdf190612023-05-10 07:51:34 -050073 timer->async_wait([timer, instance, scan, probeVector,
74 retries](const boost::system::error_code&) {
75 getInterfaces(instance, probeVector, scan, retries - 1);
76 });
77 return;
78 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +103079
Patrick Williamsdf190612023-05-10 07:51:34 -050080 scan->dbusProbeObjects[instance.path][instance.interface] = resp;
Patrick Williamsb9dd7f82023-10-20 11:20:11 -050081 },
Andrew Jefferyd9b67842022-04-05 16:44:20 +093082 instance.busName, instance.path, "org.freedesktop.DBus.Properties",
83 "GetAll", instance.interface);
Andrew Jeffery47af65a2021-12-01 14:16:31 +103084
85 if constexpr (debug)
86 {
Ed Tanous3013fb42022-07-09 08:27:06 -070087 std::cerr << __LINE__ << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +103088 }
89}
90
Andrew Jeffery4dcc55d2022-04-05 16:49:42 +093091static void registerCallback(nlohmann::json& systemConfiguration,
92 sdbusplus::asio::object_server& objServer,
93 const std::string& path)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103094{
Patrick Williams2af39222022-07-22 19:26:56 -050095 static boost::container::flat_map<std::string, sdbusplus::bus::match_t>
Andrew Jeffery47af65a2021-12-01 14:16:31 +103096 dbusMatches;
97
98 auto find = dbusMatches.find(path);
99 if (find != dbusMatches.end())
100 {
101 return;
102 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030103
Patrick Williams2af39222022-07-22 19:26:56 -0500104 std::function<void(sdbusplus::message_t & message)> eventHandler =
105 [&](sdbusplus::message_t&) {
Patrick Williamsdf190612023-05-10 07:51:34 -0500106 propertiesChangedCallback(systemConfiguration, objServer);
107 };
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030108
Patrick Williams2af39222022-07-22 19:26:56 -0500109 sdbusplus::bus::match_t match(
110 static_cast<sdbusplus::bus_t&>(*systemBus),
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030111 "type='signal',member='PropertiesChanged',path='" + path + "'",
112 eventHandler);
113 dbusMatches.emplace(path, std::move(match));
114}
115
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930116static void
117 processDbusObjects(std::vector<std::shared_ptr<PerformProbe>>& probeVector,
118 const std::shared_ptr<PerformScan>& scan,
119 const GetSubTreeType& interfaceSubtree)
120{
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930121 for (const auto& [path, object] : interfaceSubtree)
122 {
Andrew Jeffery47321832022-04-05 16:30:29 +0930123 // Get a PropertiesChanged callback for all interfaces on this path.
124 registerCallback(scan->_systemConfiguration, scan->objServer, path);
125
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930126 for (const auto& [busname, ifaces] : object)
127 {
128 for (const std::string& iface : ifaces)
129 {
130 // The 3 default org.freedeskstop interfaces (Peer,
131 // Introspectable, and Properties) are returned by
132 // the mapper but don't have properties, so don't bother
133 // with the GetAll call to save some cycles.
134 if (!boost::algorithm::starts_with(iface, "org.freedesktop"))
135 {
Andrew Jeffery47321832022-04-05 16:30:29 +0930136 getInterfaces({busname, path, iface}, probeVector, scan);
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930137 }
138 }
139 }
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930140 }
141}
142
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030143// Populates scan->dbusProbeObjects with all interfaces and properties
144// for the paths that own the interfaces passed in.
145void findDbusObjects(std::vector<std::shared_ptr<PerformProbe>>&& probeVector,
146 boost::container::flat_set<std::string>&& interfaces,
147 const std::shared_ptr<PerformScan>& scan,
148 size_t retries = 5)
149{
150 // Filter out interfaces already obtained.
151 for (const auto& [path, probeInterfaces] : scan->dbusProbeObjects)
152 {
153 for (const auto& [interface, _] : probeInterfaces)
154 {
155 interfaces.erase(interface);
156 }
157 }
158 if (interfaces.empty())
159 {
160 return;
161 }
162
163 // find all connections in the mapper that expose a specific type
164 systemBus->async_method_call(
165 [interfaces, probeVector{std::move(probeVector)}, scan,
166 retries](boost::system::error_code& ec,
167 const GetSubTreeType& interfaceSubtree) mutable {
Patrick Williamsdf190612023-05-10 07:51:34 -0500168 if (ec)
169 {
170 if (ec.value() == ENOENT)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030171 {
Patrick Williamsdf190612023-05-10 07:51:34 -0500172 return; // wasn't found by mapper
173 }
174 std::cerr << "Error communicating to mapper.\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030175
Patrick Williamsdf190612023-05-10 07:51:34 -0500176 if (retries == 0U)
177 {
178 // if we can't communicate to the mapper something is very
179 // wrong
180 std::exit(EXIT_FAILURE);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030181 }
182
Patrick Williamsdf190612023-05-10 07:51:34 -0500183 auto timer = std::make_shared<boost::asio::steady_timer>(io);
184 timer->expires_after(std::chrono::seconds(10));
185
186 timer->async_wait([timer, interfaces{std::move(interfaces)}, scan,
187 probeVector{std::move(probeVector)}, retries](
188 const boost::system::error_code&) mutable {
189 findDbusObjects(std::move(probeVector), std::move(interfaces),
190 scan, retries - 1);
191 });
192 return;
193 }
194
195 processDbusObjects(probeVector, scan, interfaceSubtree);
Patrick Williamsb9dd7f82023-10-20 11:20:11 -0500196 },
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);
201
202 if constexpr (debug)
203 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700204 std::cerr << __LINE__ << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030205 }
206}
207
Andrew Jeffery09a09a62022-04-12 22:49:57 +0930208static std::string getRecordName(const DBusInterface& probe,
209 const std::string& probeName)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030210{
211 if (probe.empty())
212 {
213 return probeName;
214 }
215
Andrew Jeffery928c5b22022-04-05 16:57:51 +0930216 // use an array so alphabetical order from the flat_map is maintained
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030217 auto device = nlohmann::json::array();
Ed Tanous3013fb42022-07-09 08:27:06 -0700218 for (const auto& devPair : probe)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030219 {
220 device.push_back(devPair.first);
221 std::visit([&device](auto&& v) { device.push_back(v); },
222 devPair.second);
223 }
Andrew Jeffery86501872022-04-05 16:58:22 +0930224
Andrew Jeffery928c5b22022-04-05 16:57:51 +0930225 // hashes are hard to distinguish, use the non-hashed version if we want
226 // debug
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030227 if constexpr (debug)
228 {
229 return probeName + device.dump();
230 }
Andrew Jeffery1ae4ed62022-04-05 16:55:43 +0930231
Andrew Jeffery86501872022-04-05 16:58:22 +0930232 return std::to_string(std::hash<std::string>{}(probeName + device.dump()));
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030233}
234
235PerformScan::PerformScan(nlohmann::json& systemConfiguration,
236 nlohmann::json& missingConfigurations,
237 std::list<nlohmann::json>& configurations,
238 sdbusplus::asio::object_server& objServerIn,
239 std::function<void()>&& callback) :
240 _systemConfiguration(systemConfiguration),
241 _missingConfigurations(missingConfigurations),
242 _configurations(configurations), objServer(objServerIn),
243 _callback(std::move(callback))
244{}
Andrew Jefferydb451482022-04-13 16:53:22 +0930245
Andrew Jeffery7a7faed2022-04-13 17:24:56 +0930246static void pruneRecordExposes(nlohmann::json& record)
Andrew Jefferydb451482022-04-13 16:53:22 +0930247{
Andrew Jeffery7a7faed2022-04-13 17:24:56 +0930248 auto findExposes = record.find("Exposes");
Andrew Jeffery5f051452022-04-13 17:11:37 +0930249 if (findExposes == record.end())
Andrew Jefferydb451482022-04-13 16:53:22 +0930250 {
Andrew Jeffery5f051452022-04-13 17:11:37 +0930251 return;
Andrew Jefferydb451482022-04-13 16:53:22 +0930252 }
Andrew Jeffery5f051452022-04-13 17:11:37 +0930253
254 auto copy = nlohmann::json::array();
255 for (auto& expose : *findExposes)
256 {
Andrew Jeffery742a7eb2022-04-13 17:14:46 +0930257 if (!expose.is_null())
Andrew Jeffery5f051452022-04-13 17:11:37 +0930258 {
Andrew Jeffery742a7eb2022-04-13 17:14:46 +0930259 copy.emplace_back(expose);
Andrew Jeffery5f051452022-04-13 17:11:37 +0930260 }
Andrew Jeffery5f051452022-04-13 17:11:37 +0930261 }
262 *findExposes = copy;
Andrew Jefferydb451482022-04-13 16:53:22 +0930263}
264
Andrew Jeffery6addc022022-04-13 18:08:58 +0930265static void recordDiscoveredIdentifiers(std::set<nlohmann::json>& usedNames,
266 std::list<size_t>& indexes,
267 const std::string& probeName,
Andrew Jefferyc0da0cc2022-04-13 18:15:09 +0930268 const nlohmann::json& record)
Andrew Jeffery6addc022022-04-13 18:08:58 +0930269{
270 size_t indexIdx = probeName.find('$');
Andrew Jefferyba41b622022-04-13 18:13:28 +0930271 if (indexIdx == std::string::npos)
Andrew Jeffery6addc022022-04-13 18:08:58 +0930272 {
273 return;
274 }
275
Andrew Jefferyc0da0cc2022-04-13 18:15:09 +0930276 auto nameIt = record.find("Name");
277 if (nameIt == record.end())
Andrew Jeffery6addc022022-04-13 18:08:58 +0930278 {
279 std::cerr << "Last JSON Illegal\n";
280 return;
281 }
282
283 int index = 0;
284 auto str = nameIt->get<std::string>().substr(indexIdx);
Ed Tanous3013fb42022-07-09 08:27:06 -0700285 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
286 const char* endPtr = str.data() + str.size();
287 auto [p, ec] = std::from_chars(str.data(), endPtr, index);
Andrew Jeffery6addc022022-04-13 18:08:58 +0930288 if (ec != std::errc())
289 {
290 return; // non-numeric replacement
291 }
292
293 usedNames.insert(nameIt.value());
294
295 auto usedIt = std::find(indexes.begin(), indexes.end(), index);
296 if (usedIt != indexes.end())
297 {
298 indexes.erase(usedIt);
299 }
300}
301
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930302static bool extractExposeActionRecordNames(std::vector<std::string>& matches,
303 nlohmann::json::iterator& keyPair)
304{
Andrew Jefferya2449842022-04-14 15:35:51 +0930305 if (keyPair.value().is_string())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930306 {
307 matches.emplace_back(keyPair.value());
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930308 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930309 }
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930310
Andrew Jefferya2449842022-04-14 15:35:51 +0930311 if (keyPair.value().is_array())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930312 {
313 for (const auto& value : keyPair.value())
314 {
Andrew Jefferya2449842022-04-14 15:35:51 +0930315 if (!value.is_string())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930316 {
317 std::cerr << "Value is invalid type " << value << "\n";
318 break;
319 }
320 matches.emplace_back(value);
321 }
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930322
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930323 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930324 }
325
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930326 std::cerr << "Value is invalid type " << keyPair.key() << "\n";
327
328 return false;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930329}
330
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930331static std::optional<std::vector<std::string>::iterator>
332 findExposeActionRecord(std::vector<std::string>& matches,
Andrew Jefferyd8213b92022-04-14 15:51:56 +0930333 const nlohmann::json& record)
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930334{
Andrew Jeffery1a02da82022-04-14 20:39:06 +0930335 const auto& name = (record)["Name"].get_ref<const std::string&>();
336 auto compare = [&name](const std::string& s) { return s == name; };
337 auto matchIt = std::find_if(matches.begin(), matches.end(), compare);
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930338
339 if (matchIt == matches.end())
340 {
341 return std::nullopt;
342 }
343
344 return matchIt;
345}
346
Andrew Jefferycf11bd32022-04-14 18:34:31 +0930347static void applyBindExposeAction(nlohmann::json& exposedObject,
348 nlohmann::json& expose,
349 const std::string& propertyName)
350{
351 if (boost::starts_with(propertyName, "Bind"))
352 {
353 std::string bind = propertyName.substr(sizeof("Bind") - 1);
354 exposedObject["Status"] = "okay";
355 expose[bind] = exposedObject;
356 }
357}
358
359static void applyDisableExposeAction(nlohmann::json& exposedObject,
360 const std::string& propertyName)
361{
362 if (propertyName == "DisableNode")
363 {
364 exposedObject["Status"] = "disabled";
365 }
366}
367
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930368static void applyConfigExposeActions(std::vector<std::string>& matches,
369 nlohmann::json& expose,
370 const std::string& propertyName,
Andrew Jefferyda45e5e2022-04-14 21:12:02 +0930371 nlohmann::json& configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930372{
Andrew Jefferyda45e5e2022-04-14 21:12:02 +0930373 for (auto& exposedObject : configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930374 {
375 auto match = findExposeActionRecord(matches, exposedObject);
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930376 if (match)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930377 {
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930378 matches.erase(*match);
379 applyBindExposeAction(exposedObject, expose, propertyName);
380 applyDisableExposeAction(exposedObject, propertyName);
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930381 }
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930382 }
383}
384
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930385static void applyExposeActions(nlohmann::json& systemConfiguration,
386 const std::string& recordName,
387 nlohmann::json& expose,
388 nlohmann::json::iterator& keyPair)
389{
390 bool isBind = boost::starts_with(keyPair.key(), "Bind");
391 bool isDisable = keyPair.key() == "DisableNode";
392 bool isExposeAction = isBind || isDisable;
393
394 if (!isExposeAction)
395 {
396 return;
397 }
398
399 std::vector<std::string> matches;
400
401 if (!extractExposeActionRecordNames(matches, keyPair))
402 {
403 return;
404 }
405
Patrick Williams2594d362022-09-28 06:46:24 -0500406 for (const auto& [configId, config] : systemConfiguration.items())
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930407 {
408 // don't disable ourselves
409 if (isDisable && configId == recordName)
410 {
411 continue;
412 }
413
414 auto configListFind = config.find("Exposes");
415 if (configListFind == config.end())
416 {
417 continue;
418 }
419
420 if (!configListFind->is_array())
421 {
422 continue;
423 }
424
425 applyConfigExposeActions(matches, expose, keyPair.key(),
426 *configListFind);
427 }
428
429 if (!matches.empty())
430 {
431 std::cerr << "configuration file dependency error, could not find "
432 << keyPair.key() << " " << keyPair.value() << "\n";
433 }
434}
435
Andrew Jefferydabee982022-04-19 13:27:24 +0930436static std::string generateDeviceName(const std::set<nlohmann::json>& usedNames,
437 const DBusObject& dbusObject,
438 size_t foundDeviceIdx,
439 const std::string& nameTemplate,
440 std::optional<std::string>& replaceStr)
441{
442 nlohmann::json copyForName = {{"Name", nameTemplate}};
443 nlohmann::json::iterator copyIt = copyForName.begin();
444 std::optional<std::string> replaceVal =
445 templateCharReplace(copyIt, dbusObject, foundDeviceIdx, replaceStr);
446
447 if (!replaceStr && replaceVal)
448 {
449 if (usedNames.find(copyIt.value()) != usedNames.end())
450 {
451 replaceStr = replaceVal;
452 copyForName = {{"Name", nameTemplate}};
453 copyIt = copyForName.begin();
454 templateCharReplace(copyIt, dbusObject, foundDeviceIdx, replaceStr);
455 }
456 }
457
458 if (replaceStr)
459 {
460 std::cerr << "Duplicates found, replacing " << *replaceStr
461 << " with found device index.\n Consider "
462 "fixing template to not have duplicates\n";
463 }
464
465 return copyIt.value();
466}
467
Andrew Jefferyaf9b46b2022-04-21 12:34:43 +0930468void PerformScan::updateSystemConfiguration(const nlohmann::json& recordRef,
469 const std::string& probeName,
470 FoundDevices& foundDevices)
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930471{
472 _passed = true;
473 passedProbes.push_back(probeName);
474
475 std::set<nlohmann::json> usedNames;
476 std::list<size_t> indexes(foundDevices.size());
477 std::iota(indexes.begin(), indexes.end(), 1);
478
479 // copy over persisted configurations and make sure we remove
480 // indexes that are already used
481 for (auto itr = foundDevices.begin(); itr != foundDevices.end();)
482 {
483 std::string recordName = getRecordName(itr->interface, probeName);
484
Zhikui Ren1f77d9c2022-08-29 16:02:37 -0700485 auto record = _systemConfiguration.find(recordName);
486 if (record == _systemConfiguration.end())
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930487 {
Zhikui Ren1f77d9c2022-08-29 16:02:37 -0700488 record = lastJson.find(recordName);
489 if (record == lastJson.end())
490 {
491 itr++;
492 continue;
493 }
494
495 pruneRecordExposes(*record);
496
Zhikui Ren1f77d9c2022-08-29 16:02:37 -0700497 _systemConfiguration[recordName] = *record;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930498 }
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930499 _missingConfigurations.erase(recordName);
500
501 // We've processed the device, remove it and advance the
502 // iterator
503 itr = foundDevices.erase(itr);
JinFuLin8f05a3a2022-11-21 15:33:24 +0800504 recordDiscoveredIdentifiers(usedNames, indexes, probeName, *record);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930505 }
506
507 std::optional<std::string> replaceStr;
508
509 DBusObject emptyObject;
510 DBusInterface emptyInterface;
511 emptyObject.emplace(std::string{}, emptyInterface);
512
513 for (const auto& [foundDevice, path] : foundDevices)
514 {
515 // Need all interfaces on this path so that template
516 // substitutions can be done with any of the contained
517 // properties. If the probe that passed didn't use an
518 // interface, such as if it was just TRUE, then
519 // templateCharReplace will just get passed in an empty
520 // map.
Andrew Jefferyaf9b46b2022-04-21 12:34:43 +0930521 auto objectIt = dbusProbeObjects.find(path);
522 const DBusObject& dbusObject = (objectIt == dbusProbeObjects.end())
523 ? emptyObject
524 : objectIt->second;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930525
526 nlohmann::json record = recordRef;
527 std::string recordName = getRecordName(foundDevice, probeName);
528 size_t foundDeviceIdx = indexes.front();
529 indexes.pop_front();
530
531 // check name first so we have no duplicate names
532 auto getName = record.find("Name");
533 if (getName == record.end())
534 {
535 std::cerr << "Record Missing Name! " << record.dump();
536 continue; // this should be impossible at this level
537 }
538
539 std::string deviceName = generateDeviceName(
540 usedNames, dbusObject, foundDeviceIdx, getName.value(), replaceStr);
541 getName.value() = deviceName;
542 usedNames.insert(deviceName);
543
544 for (auto keyPair = record.begin(); keyPair != record.end(); keyPair++)
545 {
546 if (keyPair.key() != "Name")
547 {
548 templateCharReplace(keyPair, dbusObject, foundDeviceIdx,
549 replaceStr);
550 }
551 }
552
553 // insert into configuration temporarily to be able to
554 // reference ourselves
555
556 _systemConfiguration[recordName] = record;
557
558 auto findExpose = record.find("Exposes");
559 if (findExpose == record.end())
560 {
561 continue;
562 }
563
564 for (auto& expose : *findExpose)
565 {
566 for (auto keyPair = expose.begin(); keyPair != expose.end();
567 keyPair++)
568 {
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930569 templateCharReplace(keyPair, dbusObject, foundDeviceIdx,
570 replaceStr);
571
572 applyExposeActions(_systemConfiguration, recordName, expose,
573 keyPair);
574 }
575 }
576
577 // overwrite ourselves with cleaned up version
578 _systemConfiguration[recordName] = record;
579 _missingConfigurations.erase(recordName);
580 }
581}
582
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030583void PerformScan::run()
584{
585 boost::container::flat_set<std::string> dbusProbeInterfaces;
586 std::vector<std::shared_ptr<PerformProbe>> dbusProbePointers;
587
588 for (auto it = _configurations.begin(); it != _configurations.end();)
589 {
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030590 // check for poorly formatted fields, probe must be an array
Andrew Jeffery38834872022-04-19 15:15:57 +0930591 auto findProbe = it->find("Probe");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030592 if (findProbe == it->end())
593 {
594 std::cerr << "configuration file missing probe:\n " << *it << "\n";
595 it = _configurations.erase(it);
596 continue;
597 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030598
Andrew Jeffery38834872022-04-19 15:15:57 +0930599 auto findName = it->find("Name");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030600 if (findName == it->end())
601 {
602 std::cerr << "configuration file missing name:\n " << *it << "\n";
603 it = _configurations.erase(it);
604 continue;
605 }
606 std::string probeName = *findName;
607
608 if (std::find(passedProbes.begin(), passedProbes.end(), probeName) !=
609 passedProbes.end())
610 {
611 it = _configurations.erase(it);
612 continue;
613 }
Andrew Jeffery38834872022-04-19 15:15:57 +0930614
Andrew Jeffery98f3d532022-04-19 15:29:22 +0930615 nlohmann::json& recordRef = *it;
Andrew Jeffery38834872022-04-19 15:15:57 +0930616 nlohmann::json probeCommand;
617 if ((*findProbe).type() != nlohmann::json::value_t::array)
618 {
619 probeCommand = nlohmann::json::array();
620 probeCommand.push_back(*findProbe);
621 }
622 else
623 {
624 probeCommand = *findProbe;
625 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030626
627 // store reference to this to children to makes sure we don't get
628 // destroyed too early
629 auto thisRef = shared_from_this();
630 auto probePointer = std::make_shared<PerformProbe>(
Andrew Jefferyea4ff022022-04-21 12:31:40 +0930631 recordRef, probeCommand, probeName, thisRef);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030632
633 // parse out dbus probes by discarding other probe types, store in a
634 // map
635 for (const nlohmann::json& probeJson : probeCommand)
636 {
637 const std::string* probe = probeJson.get_ptr<const std::string*>();
638 if (probe == nullptr)
639 {
640 std::cerr << "Probe statement wasn't a string, can't parse";
641 continue;
642 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700643 if (findProbeType(*probe))
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030644 {
645 continue;
646 }
647 // syntax requires probe before first open brace
648 auto findStart = probe->find('(');
649 std::string interface = probe->substr(0, findStart);
650 dbusProbeInterfaces.emplace(interface);
651 dbusProbePointers.emplace_back(probePointer);
652 }
653 it++;
654 }
655
656 // probe vector stores a shared_ptr to each PerformProbe that cares
657 // about a dbus interface
658 findDbusObjects(std::move(dbusProbePointers),
659 std::move(dbusProbeInterfaces), shared_from_this());
660 if constexpr (debug)
661 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700662 std::cerr << __LINE__ << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030663 }
664}
665
666PerformScan::~PerformScan()
667{
668 if (_passed)
669 {
670 auto nextScan = std::make_shared<PerformScan>(
671 _systemConfiguration, _missingConfigurations, _configurations,
672 objServer, std::move(_callback));
673 nextScan->passedProbes = std::move(passedProbes);
674 nextScan->dbusProbeObjects = std::move(dbusProbeObjects);
675 nextScan->run();
676
677 if constexpr (debug)
678 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700679 std::cerr << __LINE__ << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030680 }
681 }
682 else
683 {
684 _callback();
685
686 if constexpr (debug)
687 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700688 std::cerr << __LINE__ << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030689 }
690 }
691}