blob: 5e5b7153eafb1ebd811783fa64d2ba53d68a05e9 [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) {
Patrick Williamsdf190612023-05-10 07:51:34 -050063 if (errc)
64 {
65 std::cerr << "error calling getall on " << instance.busName << " "
66 << instance.path << " " << instance.interface << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +103067
Patrick Williamsdf190612023-05-10 07:51:34 -050068 auto timer = std::make_shared<boost::asio::steady_timer>(io);
69 timer->expires_after(std::chrono::seconds(2));
Andrew Jeffery47af65a2021-12-01 14:16:31 +103070
Patrick Williamsdf190612023-05-10 07:51:34 -050071 timer->async_wait([timer, instance, scan, probeVector,
72 retries](const boost::system::error_code&) {
73 getInterfaces(instance, probeVector, scan, retries - 1);
74 });
75 return;
76 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +103077
Patrick Williamsdf190612023-05-10 07:51:34 -050078 scan->dbusProbeObjects[instance.path][instance.interface] = resp;
Patrick Williamsb9dd7f82023-10-20 11:20:11 -050079 },
Andrew Jefferyd9b67842022-04-05 16:44:20 +093080 instance.busName, instance.path, "org.freedesktop.DBus.Properties",
81 "GetAll", instance.interface);
Andrew Jeffery47af65a2021-12-01 14:16:31 +103082
83 if constexpr (debug)
84 {
Ed Tanous3013fb42022-07-09 08:27:06 -070085 std::cerr << __LINE__ << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +103086 }
87}
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 Williamsdf190612023-05-10 07:51:34 -0500104 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
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930114static void
115 processDbusObjects(std::vector<std::shared_ptr<PerformProbe>>& probeVector,
116 const std::shared_ptr<PerformScan>& scan,
117 const GetSubTreeType& interfaceSubtree)
118{
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.
143void findDbusObjects(std::vector<std::shared_ptr<PerformProbe>>&& probeVector,
144 boost::container::flat_set<std::string>&& interfaces,
145 const std::shared_ptr<PerformScan>& scan,
146 size_t retries = 5)
147{
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 Williamsdf190612023-05-10 07:51:34 -0500166 if (ec)
167 {
168 if (ec.value() == ENOENT)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030169 {
Patrick Williamsdf190612023-05-10 07:51:34 -0500170 return; // wasn't found by mapper
171 }
172 std::cerr << "Error communicating to mapper.\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030173
Patrick Williamsdf190612023-05-10 07:51:34 -0500174 if (retries == 0U)
175 {
176 // if we can't communicate to the mapper something is very
177 // wrong
178 std::exit(EXIT_FAILURE);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030179 }
180
Patrick Williamsdf190612023-05-10 07:51:34 -0500181 auto timer = std::make_shared<boost::asio::steady_timer>(io);
182 timer->expires_after(std::chrono::seconds(10));
183
184 timer->async_wait([timer, interfaces{std::move(interfaces)}, scan,
185 probeVector{std::move(probeVector)}, retries](
186 const boost::system::error_code&) mutable {
187 findDbusObjects(std::move(probeVector), std::move(interfaces),
188 scan, retries - 1);
189 });
190 return;
191 }
192
193 processDbusObjects(probeVector, scan, interfaceSubtree);
Patrick Williamsb9dd7f82023-10-20 11:20:11 -0500194 },
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030195 "xyz.openbmc_project.ObjectMapper",
196 "/xyz/openbmc_project/object_mapper",
197 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", maxMapperDepth,
198 interfaces);
199
200 if constexpr (debug)
201 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700202 std::cerr << __LINE__ << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030203 }
204}
205
Andrew Jeffery09a09a62022-04-12 22:49:57 +0930206static std::string getRecordName(const DBusInterface& probe,
207 const std::string& probeName)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030208{
209 if (probe.empty())
210 {
211 return probeName;
212 }
213
Andrew Jeffery928c5b22022-04-05 16:57:51 +0930214 // use an array so alphabetical order from the flat_map is maintained
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030215 auto device = nlohmann::json::array();
Ed Tanous3013fb42022-07-09 08:27:06 -0700216 for (const auto& devPair : probe)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030217 {
218 device.push_back(devPair.first);
219 std::visit([&device](auto&& v) { device.push_back(v); },
220 devPair.second);
221 }
Andrew Jeffery86501872022-04-05 16:58:22 +0930222
Andrew Jeffery928c5b22022-04-05 16:57:51 +0930223 // hashes are hard to distinguish, use the non-hashed version if we want
224 // debug
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030225 if constexpr (debug)
226 {
227 return probeName + device.dump();
228 }
Andrew Jeffery1ae4ed62022-04-05 16:55:43 +0930229
Andrew Jeffery86501872022-04-05 16:58:22 +0930230 return std::to_string(std::hash<std::string>{}(probeName + device.dump()));
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030231}
232
233PerformScan::PerformScan(nlohmann::json& systemConfiguration,
234 nlohmann::json& missingConfigurations,
235 std::list<nlohmann::json>& configurations,
236 sdbusplus::asio::object_server& objServerIn,
237 std::function<void()>&& callback) :
238 _systemConfiguration(systemConfiguration),
239 _missingConfigurations(missingConfigurations),
240 _configurations(configurations), objServer(objServerIn),
241 _callback(std::move(callback))
242{}
Andrew Jefferydb451482022-04-13 16:53:22 +0930243
Andrew Jeffery7a7faed2022-04-13 17:24:56 +0930244static void pruneRecordExposes(nlohmann::json& record)
Andrew Jefferydb451482022-04-13 16:53:22 +0930245{
Andrew Jeffery7a7faed2022-04-13 17:24:56 +0930246 auto findExposes = record.find("Exposes");
Andrew Jeffery5f051452022-04-13 17:11:37 +0930247 if (findExposes == record.end())
Andrew Jefferydb451482022-04-13 16:53:22 +0930248 {
Andrew Jeffery5f051452022-04-13 17:11:37 +0930249 return;
Andrew Jefferydb451482022-04-13 16:53:22 +0930250 }
Andrew Jeffery5f051452022-04-13 17:11:37 +0930251
252 auto copy = nlohmann::json::array();
253 for (auto& expose : *findExposes)
254 {
Andrew Jeffery742a7eb2022-04-13 17:14:46 +0930255 if (!expose.is_null())
Andrew Jeffery5f051452022-04-13 17:11:37 +0930256 {
Andrew Jeffery742a7eb2022-04-13 17:14:46 +0930257 copy.emplace_back(expose);
Andrew Jeffery5f051452022-04-13 17:11:37 +0930258 }
Andrew Jeffery5f051452022-04-13 17:11:37 +0930259 }
260 *findExposes = copy;
Andrew Jefferydb451482022-04-13 16:53:22 +0930261}
262
Andrew Jeffery6addc022022-04-13 18:08:58 +0930263static void recordDiscoveredIdentifiers(std::set<nlohmann::json>& usedNames,
264 std::list<size_t>& indexes,
265 const std::string& probeName,
Andrew Jefferyc0da0cc2022-04-13 18:15:09 +0930266 const nlohmann::json& record)
Andrew Jeffery6addc022022-04-13 18:08:58 +0930267{
268 size_t indexIdx = probeName.find('$');
Andrew Jefferyba41b622022-04-13 18:13:28 +0930269 if (indexIdx == std::string::npos)
Andrew Jeffery6addc022022-04-13 18:08:58 +0930270 {
271 return;
272 }
273
Andrew Jefferyc0da0cc2022-04-13 18:15:09 +0930274 auto nameIt = record.find("Name");
275 if (nameIt == record.end())
Andrew Jeffery6addc022022-04-13 18:08:58 +0930276 {
277 std::cerr << "Last JSON Illegal\n";
278 return;
279 }
280
281 int index = 0;
282 auto str = nameIt->get<std::string>().substr(indexIdx);
Ed Tanous3013fb42022-07-09 08:27:06 -0700283 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
284 const char* endPtr = str.data() + str.size();
285 auto [p, ec] = std::from_chars(str.data(), endPtr, index);
Andrew Jeffery6addc022022-04-13 18:08:58 +0930286 if (ec != std::errc())
287 {
288 return; // non-numeric replacement
289 }
290
291 usedNames.insert(nameIt.value());
292
293 auto usedIt = std::find(indexes.begin(), indexes.end(), index);
294 if (usedIt != indexes.end())
295 {
296 indexes.erase(usedIt);
297 }
298}
299
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930300static bool extractExposeActionRecordNames(std::vector<std::string>& matches,
301 nlohmann::json::iterator& keyPair)
302{
Andrew Jefferya2449842022-04-14 15:35:51 +0930303 if (keyPair.value().is_string())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930304 {
305 matches.emplace_back(keyPair.value());
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930306 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930307 }
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930308
Andrew Jefferya2449842022-04-14 15:35:51 +0930309 if (keyPair.value().is_array())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930310 {
311 for (const auto& value : keyPair.value())
312 {
Andrew Jefferya2449842022-04-14 15:35:51 +0930313 if (!value.is_string())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930314 {
315 std::cerr << "Value is invalid type " << value << "\n";
316 break;
317 }
318 matches.emplace_back(value);
319 }
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930320
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930321 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930322 }
323
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930324 std::cerr << "Value is invalid type " << keyPair.key() << "\n";
325
326 return false;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930327}
328
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930329static std::optional<std::vector<std::string>::iterator>
330 findExposeActionRecord(std::vector<std::string>& matches,
Andrew Jefferyd8213b92022-04-14 15:51:56 +0930331 const nlohmann::json& record)
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930332{
Andrew Jeffery1a02da82022-04-14 20:39:06 +0930333 const auto& name = (record)["Name"].get_ref<const std::string&>();
334 auto compare = [&name](const std::string& s) { return s == name; };
335 auto matchIt = std::find_if(matches.begin(), matches.end(), compare);
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930336
337 if (matchIt == matches.end())
338 {
339 return std::nullopt;
340 }
341
342 return matchIt;
343}
344
Andrew Jefferycf11bd32022-04-14 18:34:31 +0930345static void applyBindExposeAction(nlohmann::json& exposedObject,
346 nlohmann::json& expose,
347 const std::string& propertyName)
348{
349 if (boost::starts_with(propertyName, "Bind"))
350 {
351 std::string bind = propertyName.substr(sizeof("Bind") - 1);
352 exposedObject["Status"] = "okay";
353 expose[bind] = exposedObject;
354 }
355}
356
357static void applyDisableExposeAction(nlohmann::json& exposedObject,
358 const std::string& propertyName)
359{
360 if (propertyName == "DisableNode")
361 {
362 exposedObject["Status"] = "disabled";
363 }
364}
365
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930366static void applyConfigExposeActions(std::vector<std::string>& matches,
367 nlohmann::json& expose,
368 const std::string& propertyName,
Andrew Jefferyda45e5e2022-04-14 21:12:02 +0930369 nlohmann::json& configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930370{
Andrew Jefferyda45e5e2022-04-14 21:12:02 +0930371 for (auto& exposedObject : configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930372 {
373 auto match = findExposeActionRecord(matches, exposedObject);
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930374 if (match)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930375 {
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930376 matches.erase(*match);
377 applyBindExposeAction(exposedObject, expose, propertyName);
378 applyDisableExposeAction(exposedObject, propertyName);
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930379 }
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930380 }
381}
382
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930383static void applyExposeActions(nlohmann::json& systemConfiguration,
384 const std::string& recordName,
385 nlohmann::json& expose,
386 nlohmann::json::iterator& keyPair)
387{
388 bool isBind = boost::starts_with(keyPair.key(), "Bind");
389 bool isDisable = keyPair.key() == "DisableNode";
390 bool isExposeAction = isBind || isDisable;
391
392 if (!isExposeAction)
393 {
394 return;
395 }
396
397 std::vector<std::string> matches;
398
399 if (!extractExposeActionRecordNames(matches, keyPair))
400 {
401 return;
402 }
403
Patrick Williams2594d362022-09-28 06:46:24 -0500404 for (const auto& [configId, config] : systemConfiguration.items())
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930405 {
406 // don't disable ourselves
407 if (isDisable && configId == recordName)
408 {
409 continue;
410 }
411
412 auto configListFind = config.find("Exposes");
413 if (configListFind == config.end())
414 {
415 continue;
416 }
417
418 if (!configListFind->is_array())
419 {
420 continue;
421 }
422
423 applyConfigExposeActions(matches, expose, keyPair.key(),
424 *configListFind);
425 }
426
427 if (!matches.empty())
428 {
429 std::cerr << "configuration file dependency error, could not find "
430 << keyPair.key() << " " << keyPair.value() << "\n";
431 }
432}
433
Andrew Jefferydabee982022-04-19 13:27:24 +0930434static std::string generateDeviceName(const std::set<nlohmann::json>& usedNames,
435 const DBusObject& dbusObject,
436 size_t foundDeviceIdx,
437 const std::string& nameTemplate,
438 std::optional<std::string>& replaceStr)
439{
440 nlohmann::json copyForName = {{"Name", nameTemplate}};
441 nlohmann::json::iterator copyIt = copyForName.begin();
442 std::optional<std::string> replaceVal =
443 templateCharReplace(copyIt, dbusObject, foundDeviceIdx, replaceStr);
444
445 if (!replaceStr && replaceVal)
446 {
447 if (usedNames.find(copyIt.value()) != usedNames.end())
448 {
449 replaceStr = replaceVal;
450 copyForName = {{"Name", nameTemplate}};
451 copyIt = copyForName.begin();
452 templateCharReplace(copyIt, dbusObject, foundDeviceIdx, replaceStr);
453 }
454 }
455
456 if (replaceStr)
457 {
458 std::cerr << "Duplicates found, replacing " << *replaceStr
459 << " with found device index.\n Consider "
460 "fixing template to not have duplicates\n";
461 }
462
463 return copyIt.value();
464}
465
Andrew Jefferyaf9b46b2022-04-21 12:34:43 +0930466void PerformScan::updateSystemConfiguration(const nlohmann::json& recordRef,
467 const std::string& probeName,
468 FoundDevices& foundDevices)
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930469{
470 _passed = true;
471 passedProbes.push_back(probeName);
472
473 std::set<nlohmann::json> usedNames;
474 std::list<size_t> indexes(foundDevices.size());
475 std::iota(indexes.begin(), indexes.end(), 1);
476
477 // copy over persisted configurations and make sure we remove
478 // indexes that are already used
479 for (auto itr = foundDevices.begin(); itr != foundDevices.end();)
480 {
481 std::string recordName = getRecordName(itr->interface, probeName);
482
Zhikui Ren1f77d9c2022-08-29 16:02:37 -0700483 auto record = _systemConfiguration.find(recordName);
484 if (record == _systemConfiguration.end())
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930485 {
Zhikui Ren1f77d9c2022-08-29 16:02:37 -0700486 record = lastJson.find(recordName);
487 if (record == lastJson.end())
488 {
489 itr++;
490 continue;
491 }
492
493 pruneRecordExposes(*record);
494
Zhikui Ren1f77d9c2022-08-29 16:02:37 -0700495 _systemConfiguration[recordName] = *record;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930496 }
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930497 _missingConfigurations.erase(recordName);
498
499 // We've processed the device, remove it and advance the
500 // iterator
501 itr = foundDevices.erase(itr);
JinFuLin8f05a3a2022-11-21 15:33:24 +0800502 recordDiscoveredIdentifiers(usedNames, indexes, probeName, *record);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930503 }
504
505 std::optional<std::string> replaceStr;
506
507 DBusObject emptyObject;
508 DBusInterface emptyInterface;
509 emptyObject.emplace(std::string{}, emptyInterface);
510
511 for (const auto& [foundDevice, path] : foundDevices)
512 {
513 // Need all interfaces on this path so that template
514 // substitutions can be done with any of the contained
515 // properties. If the probe that passed didn't use an
516 // interface, such as if it was just TRUE, then
517 // templateCharReplace will just get passed in an empty
518 // map.
Andrew Jefferyaf9b46b2022-04-21 12:34:43 +0930519 auto objectIt = dbusProbeObjects.find(path);
520 const DBusObject& dbusObject = (objectIt == dbusProbeObjects.end())
521 ? emptyObject
522 : objectIt->second;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930523
524 nlohmann::json record = recordRef;
525 std::string recordName = getRecordName(foundDevice, probeName);
526 size_t foundDeviceIdx = indexes.front();
527 indexes.pop_front();
528
529 // check name first so we have no duplicate names
530 auto getName = record.find("Name");
531 if (getName == record.end())
532 {
533 std::cerr << "Record Missing Name! " << record.dump();
534 continue; // this should be impossible at this level
535 }
536
537 std::string deviceName = generateDeviceName(
538 usedNames, dbusObject, foundDeviceIdx, getName.value(), replaceStr);
539 getName.value() = deviceName;
540 usedNames.insert(deviceName);
541
542 for (auto keyPair = record.begin(); keyPair != record.end(); keyPair++)
543 {
544 if (keyPair.key() != "Name")
545 {
546 templateCharReplace(keyPair, dbusObject, foundDeviceIdx,
547 replaceStr);
548 }
549 }
550
551 // insert into configuration temporarily to be able to
552 // reference ourselves
553
554 _systemConfiguration[recordName] = record;
555
556 auto findExpose = record.find("Exposes");
557 if (findExpose == record.end())
558 {
559 continue;
560 }
561
562 for (auto& expose : *findExpose)
563 {
564 for (auto keyPair = expose.begin(); keyPair != expose.end();
565 keyPair++)
566 {
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930567 templateCharReplace(keyPair, dbusObject, foundDeviceIdx,
568 replaceStr);
569
570 applyExposeActions(_systemConfiguration, recordName, expose,
571 keyPair);
572 }
573 }
574
575 // overwrite ourselves with cleaned up version
576 _systemConfiguration[recordName] = record;
577 _missingConfigurations.erase(recordName);
578 }
579}
580
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030581void PerformScan::run()
582{
583 boost::container::flat_set<std::string> dbusProbeInterfaces;
584 std::vector<std::shared_ptr<PerformProbe>> dbusProbePointers;
585
586 for (auto it = _configurations.begin(); it != _configurations.end();)
587 {
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030588 // check for poorly formatted fields, probe must be an array
Andrew Jeffery38834872022-04-19 15:15:57 +0930589 auto findProbe = it->find("Probe");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030590 if (findProbe == it->end())
591 {
592 std::cerr << "configuration file missing probe:\n " << *it << "\n";
593 it = _configurations.erase(it);
594 continue;
595 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030596
Andrew Jeffery38834872022-04-19 15:15:57 +0930597 auto findName = it->find("Name");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030598 if (findName == it->end())
599 {
600 std::cerr << "configuration file missing name:\n " << *it << "\n";
601 it = _configurations.erase(it);
602 continue;
603 }
604 std::string probeName = *findName;
605
606 if (std::find(passedProbes.begin(), passedProbes.end(), probeName) !=
607 passedProbes.end())
608 {
609 it = _configurations.erase(it);
610 continue;
611 }
Andrew Jeffery38834872022-04-19 15:15:57 +0930612
Andrew Jeffery98f3d532022-04-19 15:29:22 +0930613 nlohmann::json& recordRef = *it;
Andrew Jeffery38834872022-04-19 15:15:57 +0930614 nlohmann::json probeCommand;
615 if ((*findProbe).type() != nlohmann::json::value_t::array)
616 {
617 probeCommand = nlohmann::json::array();
618 probeCommand.push_back(*findProbe);
619 }
620 else
621 {
622 probeCommand = *findProbe;
623 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030624
625 // store reference to this to children to makes sure we don't get
626 // destroyed too early
627 auto thisRef = shared_from_this();
628 auto probePointer = std::make_shared<PerformProbe>(
Andrew Jefferyea4ff022022-04-21 12:31:40 +0930629 recordRef, probeCommand, probeName, thisRef);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030630
631 // parse out dbus probes by discarding other probe types, store in a
632 // map
633 for (const nlohmann::json& probeJson : probeCommand)
634 {
635 const std::string* probe = probeJson.get_ptr<const std::string*>();
636 if (probe == nullptr)
637 {
638 std::cerr << "Probe statement wasn't a string, can't parse";
639 continue;
640 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700641 if (findProbeType(*probe))
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030642 {
643 continue;
644 }
645 // syntax requires probe before first open brace
646 auto findStart = probe->find('(');
647 std::string interface = probe->substr(0, findStart);
648 dbusProbeInterfaces.emplace(interface);
649 dbusProbePointers.emplace_back(probePointer);
650 }
651 it++;
652 }
653
654 // probe vector stores a shared_ptr to each PerformProbe that cares
655 // about a dbus interface
656 findDbusObjects(std::move(dbusProbePointers),
657 std::move(dbusProbeInterfaces), shared_from_this());
658 if constexpr (debug)
659 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700660 std::cerr << __LINE__ << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030661 }
662}
663
664PerformScan::~PerformScan()
665{
666 if (_passed)
667 {
668 auto nextScan = std::make_shared<PerformScan>(
669 _systemConfiguration, _missingConfigurations, _configurations,
670 objServer, std::move(_callback));
671 nextScan->passedProbes = std::move(passedProbes);
672 nextScan->dbusProbeObjects = std::move(dbusProbeObjects);
673 nextScan->run();
674
675 if constexpr (debug)
676 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700677 std::cerr << __LINE__ << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030678 }
679 }
680 else
681 {
682 _callback();
683
684 if constexpr (debug)
685 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700686 std::cerr << __LINE__ << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030687 }
688 }
689}