blob: 167490b23f935975a59745eeb25e07922337abc0 [file] [log] [blame]
Andrew Jeffery47af65a2021-12-01 14:16:31 +10301/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
Brad Bishope45d8c72022-05-25 15:12:53 -040016/// \file perform_scan.cpp
Christopher Meis26fbbd52025-03-26 14:55:06 +010017#include "perform_scan.hpp"
18
Christopher Meis26fbbd52025-03-26 14:55:06 +010019#include "perform_probe.hpp"
Christopher Meis59ef1e72025-04-16 08:53:25 +020020#include "utils.hpp"
Andrew Jeffery47af65a2021-12-01 14:16:31 +103021
22#include <boost/algorithm/string/predicate.hpp>
23#include <boost/asio/steady_timer.hpp>
24#include <boost/container/flat_map.hpp>
25#include <boost/container/flat_set.hpp>
Alexander Hansenc3db2c32024-08-20 15:01:38 +020026#include <phosphor-logging/lg2.hpp>
Andrew Jeffery47af65a2021-12-01 14:16:31 +103027
28#include <charconv>
Christopher Meis59ef1e72025-04-16 08:53:25 +020029#include <iostream>
Andrew Jeffery47af65a2021-12-01 14:16:31 +103030
Brad Bishope45d8c72022-05-25 15:12:53 -040031/* Hacks from splitting entity_manager.cpp */
Ed Tanousfc171422024-04-04 17:18:16 -070032// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103033extern std::shared_ptr<sdbusplus::asio::connection> systemBus;
34extern nlohmann::json lastJson;
Patrick Williams5a807032025-03-03 11:20:39 -050035extern void propertiesChangedCallback(
36 nlohmann::json& systemConfiguration,
37 sdbusplus::asio::object_server& objServer);
Ed Tanousfc171422024-04-04 17:18:16 -070038// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103039
Andrew Jeffery47af65a2021-12-01 14:16:31 +103040using GetSubTreeType = std::vector<
41 std::pair<std::string,
42 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
43
44constexpr const int32_t maxMapperDepth = 0;
45
Andrew Jefferyd9b67842022-04-05 16:44:20 +093046struct DBusInterfaceInstance
47{
48 std::string busName;
49 std::string path;
50 std::string interface;
51};
52
Patrick Williams5a807032025-03-03 11:20:39 -050053void getInterfaces(
54 const DBusInterfaceInstance& instance,
Christopher Meis26fbbd52025-03-26 14:55:06 +010055 const std::vector<std::shared_ptr<probe::PerformProbe>>& probeVector,
56 const std::shared_ptr<scan::PerformScan>& scan, size_t retries = 5)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103057{
Ed Tanous3013fb42022-07-09 08:27:06 -070058 if (retries == 0U)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103059 {
Andrew Jefferyd9b67842022-04-05 16:44:20 +093060 std::cerr << "retries exhausted on " << instance.busName << " "
61 << instance.path << " " << instance.interface << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +103062 return;
63 }
64
65 systemBus->async_method_call(
Patrick Williamsb7077432024-08-16 15:22:21 -040066 [instance, scan, probeVector,
67 retries](boost::system::error_code& errc, const DBusInterface& resp) {
68 if (errc)
69 {
70 std::cerr << "error calling getall on " << instance.busName
71 << " " << instance.path << " "
72 << instance.interface << "\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +103073
Patrick Williamsb7077432024-08-16 15:22:21 -040074 auto timer = std::make_shared<boost::asio::steady_timer>(io);
75 timer->expires_after(std::chrono::seconds(2));
Andrew Jeffery47af65a2021-12-01 14:16:31 +103076
Patrick Williamsb7077432024-08-16 15:22:21 -040077 timer->async_wait([timer, instance, scan, probeVector,
78 retries](const boost::system::error_code&) {
79 getInterfaces(instance, probeVector, scan, retries - 1);
80 });
81 return;
82 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +103083
Patrick Williamsb7077432024-08-16 15:22:21 -040084 scan->dbusProbeObjects[instance.path][instance.interface] = resp;
85 },
Andrew Jefferyd9b67842022-04-05 16:44:20 +093086 instance.busName, instance.path, "org.freedesktop.DBus.Properties",
87 "GetAll", instance.interface);
Andrew Jeffery47af65a2021-12-01 14:16:31 +103088}
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&) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400105 propertiesChangedCallback(systemConfiguration, objServer);
106 };
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030107
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
Patrick Williams5a807032025-03-03 11:20:39 -0500115static void processDbusObjects(
Christopher Meis26fbbd52025-03-26 14:55:06 +0100116 std::vector<std::shared_ptr<probe::PerformProbe>>& probeVector,
117 const std::shared_ptr<scan::PerformScan>& scan,
Patrick Williams5a807032025-03-03 11:20:39 -0500118 const GetSubTreeType& interfaceSubtree)
Andrew Jeffery8d2761e2022-04-05 16:26:28 +0930119{
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.
Christopher Meis26fbbd52025-03-26 14:55:06 +0100144void findDbusObjects(
145 std::vector<std::shared_ptr<probe::PerformProbe>>&& probeVector,
146 boost::container::flat_set<std::string>&& interfaces,
147 const std::shared_ptr<scan::PerformScan>& scan, size_t retries = 5)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030148{
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 {
Patrick Williamsb7077432024-08-16 15:22:21 -0400167 if (ec)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030168 {
Patrick Williamsb7077432024-08-16 15:22:21 -0400169 if (ec.value() == ENOENT)
170 {
171 return; // wasn't found by mapper
172 }
173 std::cerr << "Error communicating to mapper.\n";
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030174
Patrick Williamsb7077432024-08-16 15:22:21 -0400175 if (retries == 0U)
176 {
177 // if we can't communicate to the mapper something is very
178 // wrong
179 std::exit(EXIT_FAILURE);
180 }
181
182 auto timer = std::make_shared<boost::asio::steady_timer>(io);
183 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;
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030194 }
195
Patrick Williamsb7077432024-08-16 15:22:21 -0400196 processDbusObjects(probeVector, scan, interfaceSubtree);
197 },
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030198 "xyz.openbmc_project.ObjectMapper",
199 "/xyz/openbmc_project/object_mapper",
200 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", maxMapperDepth,
201 interfaces);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030202}
203
Andrew Jeffery09a09a62022-04-12 22:49:57 +0930204static std::string getRecordName(const DBusInterface& probe,
205 const std::string& probeName)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030206{
207 if (probe.empty())
208 {
209 return probeName;
210 }
211
Andrew Jeffery928c5b22022-04-05 16:57:51 +0930212 // use an array so alphabetical order from the flat_map is maintained
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030213 auto device = nlohmann::json::array();
Ed Tanous3013fb42022-07-09 08:27:06 -0700214 for (const auto& devPair : probe)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030215 {
216 device.push_back(devPair.first);
217 std::visit([&device](auto&& v) { device.push_back(v); },
218 devPair.second);
219 }
Andrew Jeffery86501872022-04-05 16:58:22 +0930220
Andrew Jeffery928c5b22022-04-05 16:57:51 +0930221 // hashes are hard to distinguish, use the non-hashed version if we want
222 // debug
Alexander Hansenc3db2c32024-08-20 15:01:38 +0200223 // return probeName + device.dump();
Andrew Jeffery1ae4ed62022-04-05 16:55:43 +0930224
Andrew Jeffery86501872022-04-05 16:58:22 +0930225 return std::to_string(std::hash<std::string>{}(probeName + device.dump()));
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030226}
227
Christopher Meis26fbbd52025-03-26 14:55:06 +0100228scan::PerformScan::PerformScan(nlohmann::json& systemConfiguration,
229 nlohmann::json& missingConfigurations,
230 std::list<nlohmann::json>& configurations,
231 sdbusplus::asio::object_server& objServerIn,
232 std::function<void()>&& callback) :
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030233 _systemConfiguration(systemConfiguration),
234 _missingConfigurations(missingConfigurations),
235 _configurations(configurations), objServer(objServerIn),
236 _callback(std::move(callback))
237{}
Andrew Jefferydb451482022-04-13 16:53:22 +0930238
Andrew Jeffery7a7faed2022-04-13 17:24:56 +0930239static void pruneRecordExposes(nlohmann::json& record)
Andrew Jefferydb451482022-04-13 16:53:22 +0930240{
Andrew Jeffery7a7faed2022-04-13 17:24:56 +0930241 auto findExposes = record.find("Exposes");
Andrew Jeffery5f051452022-04-13 17:11:37 +0930242 if (findExposes == record.end())
Andrew Jefferydb451482022-04-13 16:53:22 +0930243 {
Andrew Jeffery5f051452022-04-13 17:11:37 +0930244 return;
Andrew Jefferydb451482022-04-13 16:53:22 +0930245 }
Andrew Jeffery5f051452022-04-13 17:11:37 +0930246
247 auto copy = nlohmann::json::array();
248 for (auto& expose : *findExposes)
249 {
Andrew Jeffery742a7eb2022-04-13 17:14:46 +0930250 if (!expose.is_null())
Andrew Jeffery5f051452022-04-13 17:11:37 +0930251 {
Andrew Jeffery742a7eb2022-04-13 17:14:46 +0930252 copy.emplace_back(expose);
Andrew Jeffery5f051452022-04-13 17:11:37 +0930253 }
Andrew Jeffery5f051452022-04-13 17:11:37 +0930254 }
255 *findExposes = copy;
Andrew Jefferydb451482022-04-13 16:53:22 +0930256}
257
Patrick Williamsb7077432024-08-16 15:22:21 -0400258static void recordDiscoveredIdentifiers(
259 std::set<nlohmann::json>& usedNames, std::list<size_t>& indexes,
260 const std::string& probeName, const nlohmann::json& record)
Andrew Jeffery6addc022022-04-13 18:08:58 +0930261{
262 size_t indexIdx = probeName.find('$');
Andrew Jefferyba41b622022-04-13 18:13:28 +0930263 if (indexIdx == std::string::npos)
Andrew Jeffery6addc022022-04-13 18:08:58 +0930264 {
265 return;
266 }
267
Andrew Jefferyc0da0cc2022-04-13 18:15:09 +0930268 auto nameIt = record.find("Name");
269 if (nameIt == record.end())
Andrew Jeffery6addc022022-04-13 18:08:58 +0930270 {
271 std::cerr << "Last JSON Illegal\n";
272 return;
273 }
274
275 int index = 0;
276 auto str = nameIt->get<std::string>().substr(indexIdx);
Ed Tanous3013fb42022-07-09 08:27:06 -0700277 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
278 const char* endPtr = str.data() + str.size();
279 auto [p, ec] = std::from_chars(str.data(), endPtr, index);
Andrew Jeffery6addc022022-04-13 18:08:58 +0930280 if (ec != std::errc())
281 {
282 return; // non-numeric replacement
283 }
284
285 usedNames.insert(nameIt.value());
286
287 auto usedIt = std::find(indexes.begin(), indexes.end(), index);
288 if (usedIt != indexes.end())
289 {
290 indexes.erase(usedIt);
291 }
292}
293
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930294static bool extractExposeActionRecordNames(std::vector<std::string>& matches,
295 nlohmann::json::iterator& keyPair)
296{
Andrew Jefferya2449842022-04-14 15:35:51 +0930297 if (keyPair.value().is_string())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930298 {
299 matches.emplace_back(keyPair.value());
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930300 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930301 }
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930302
Andrew Jefferya2449842022-04-14 15:35:51 +0930303 if (keyPair.value().is_array())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930304 {
305 for (const auto& value : keyPair.value())
306 {
Andrew Jefferya2449842022-04-14 15:35:51 +0930307 if (!value.is_string())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930308 {
309 std::cerr << "Value is invalid type " << value << "\n";
310 break;
311 }
312 matches.emplace_back(value);
313 }
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930314
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930315 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930316 }
317
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930318 std::cerr << "Value is invalid type " << keyPair.key() << "\n";
319
320 return false;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930321}
322
Patrick Williamsb7077432024-08-16 15:22:21 -0400323static std::optional<std::vector<std::string>::iterator> findExposeActionRecord(
324 std::vector<std::string>& matches, const nlohmann::json& record)
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930325{
Andrew Jeffery1a02da82022-04-14 20:39:06 +0930326 const auto& name = (record)["Name"].get_ref<const std::string&>();
327 auto compare = [&name](const std::string& s) { return s == name; };
328 auto matchIt = std::find_if(matches.begin(), matches.end(), compare);
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930329
330 if (matchIt == matches.end())
331 {
332 return std::nullopt;
333 }
334
335 return matchIt;
336}
337
Andrew Jefferycf11bd32022-04-14 18:34:31 +0930338static void applyBindExposeAction(nlohmann::json& exposedObject,
339 nlohmann::json& expose,
340 const std::string& propertyName)
341{
342 if (boost::starts_with(propertyName, "Bind"))
343 {
344 std::string bind = propertyName.substr(sizeof("Bind") - 1);
345 exposedObject["Status"] = "okay";
346 expose[bind] = exposedObject;
347 }
348}
349
350static void applyDisableExposeAction(nlohmann::json& exposedObject,
351 const std::string& propertyName)
352{
353 if (propertyName == "DisableNode")
354 {
355 exposedObject["Status"] = "disabled";
356 }
357}
358
Patrick Williamsb7077432024-08-16 15:22:21 -0400359static void applyConfigExposeActions(
360 std::vector<std::string>& matches, nlohmann::json& expose,
361 const std::string& propertyName, nlohmann::json& configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930362{
Andrew Jefferyda45e5e2022-04-14 21:12:02 +0930363 for (auto& exposedObject : configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930364 {
365 auto match = findExposeActionRecord(matches, exposedObject);
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930366 if (match)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930367 {
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930368 matches.erase(*match);
369 applyBindExposeAction(exposedObject, expose, propertyName);
370 applyDisableExposeAction(exposedObject, propertyName);
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930371 }
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930372 }
373}
374
Patrick Williamsb7077432024-08-16 15:22:21 -0400375static void applyExposeActions(
376 nlohmann::json& systemConfiguration, const std::string& recordName,
377 nlohmann::json& expose, nlohmann::json::iterator& keyPair)
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930378{
379 bool isBind = boost::starts_with(keyPair.key(), "Bind");
380 bool isDisable = keyPair.key() == "DisableNode";
381 bool isExposeAction = isBind || isDisable;
382
383 if (!isExposeAction)
384 {
385 return;
386 }
387
388 std::vector<std::string> matches;
389
390 if (!extractExposeActionRecordNames(matches, keyPair))
391 {
392 return;
393 }
394
Patrick Williams2594d362022-09-28 06:46:24 -0500395 for (const auto& [configId, config] : systemConfiguration.items())
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930396 {
397 // don't disable ourselves
398 if (isDisable && configId == recordName)
399 {
400 continue;
401 }
402
403 auto configListFind = config.find("Exposes");
404 if (configListFind == config.end())
405 {
406 continue;
407 }
408
409 if (!configListFind->is_array())
410 {
411 continue;
412 }
413
414 applyConfigExposeActions(matches, expose, keyPair.key(),
415 *configListFind);
416 }
417
418 if (!matches.empty())
419 {
420 std::cerr << "configuration file dependency error, could not find "
421 << keyPair.key() << " " << keyPair.value() << "\n";
422 }
423}
424
Patrick Williamsb7077432024-08-16 15:22:21 -0400425static std::string generateDeviceName(
426 const std::set<nlohmann::json>& usedNames, const DBusObject& dbusObject,
427 size_t foundDeviceIdx, const std::string& nameTemplate,
428 std::optional<std::string>& replaceStr)
Andrew Jefferydabee982022-04-19 13:27:24 +0930429{
430 nlohmann::json copyForName = {{"Name", nameTemplate}};
431 nlohmann::json::iterator copyIt = copyForName.begin();
Christopher Meis59ef1e72025-04-16 08:53:25 +0200432 std::optional<std::string> replaceVal = em_utils::templateCharReplace(
433 copyIt, dbusObject, foundDeviceIdx, replaceStr);
Andrew Jefferydabee982022-04-19 13:27:24 +0930434
435 if (!replaceStr && replaceVal)
436 {
437 if (usedNames.find(copyIt.value()) != usedNames.end())
438 {
439 replaceStr = replaceVal;
440 copyForName = {{"Name", nameTemplate}};
441 copyIt = copyForName.begin();
Christopher Meis59ef1e72025-04-16 08:53:25 +0200442 em_utils::templateCharReplace(copyIt, dbusObject, foundDeviceIdx,
443 replaceStr);
Andrew Jefferydabee982022-04-19 13:27:24 +0930444 }
445 }
446
447 if (replaceStr)
448 {
449 std::cerr << "Duplicates found, replacing " << *replaceStr
450 << " with found device index.\n Consider "
451 "fixing template to not have duplicates\n";
452 }
453
454 return copyIt.value();
455}
456
Christopher Meis26fbbd52025-03-26 14:55:06 +0100457void scan::PerformScan::updateSystemConfiguration(
458 const nlohmann::json& recordRef, const std::string& probeName,
459 FoundDevices& foundDevices)
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930460{
461 _passed = true;
462 passedProbes.push_back(probeName);
463
464 std::set<nlohmann::json> usedNames;
465 std::list<size_t> indexes(foundDevices.size());
466 std::iota(indexes.begin(), indexes.end(), 1);
467
468 // copy over persisted configurations and make sure we remove
469 // indexes that are already used
470 for (auto itr = foundDevices.begin(); itr != foundDevices.end();)
471 {
472 std::string recordName = getRecordName(itr->interface, probeName);
473
Zhikui Ren1f77d9c2022-08-29 16:02:37 -0700474 auto record = _systemConfiguration.find(recordName);
475 if (record == _systemConfiguration.end())
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930476 {
Zhikui Ren1f77d9c2022-08-29 16:02:37 -0700477 record = lastJson.find(recordName);
478 if (record == lastJson.end())
479 {
480 itr++;
481 continue;
482 }
483
484 pruneRecordExposes(*record);
485
Zhikui Ren1f77d9c2022-08-29 16:02:37 -0700486 _systemConfiguration[recordName] = *record;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930487 }
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930488 _missingConfigurations.erase(recordName);
489
490 // We've processed the device, remove it and advance the
491 // iterator
492 itr = foundDevices.erase(itr);
JinFuLin8f05a3a2022-11-21 15:33:24 +0800493 recordDiscoveredIdentifiers(usedNames, indexes, probeName, *record);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930494 }
495
496 std::optional<std::string> replaceStr;
497
498 DBusObject emptyObject;
499 DBusInterface emptyInterface;
500 emptyObject.emplace(std::string{}, emptyInterface);
501
502 for (const auto& [foundDevice, path] : foundDevices)
503 {
504 // Need all interfaces on this path so that template
505 // substitutions can be done with any of the contained
506 // properties. If the probe that passed didn't use an
507 // interface, such as if it was just TRUE, then
508 // templateCharReplace will just get passed in an empty
509 // map.
Andrew Jefferyaf9b46b2022-04-21 12:34:43 +0930510 auto objectIt = dbusProbeObjects.find(path);
511 const DBusObject& dbusObject = (objectIt == dbusProbeObjects.end())
512 ? emptyObject
513 : objectIt->second;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930514
515 nlohmann::json record = recordRef;
516 std::string recordName = getRecordName(foundDevice, probeName);
517 size_t foundDeviceIdx = indexes.front();
518 indexes.pop_front();
519
520 // check name first so we have no duplicate names
521 auto getName = record.find("Name");
522 if (getName == record.end())
523 {
524 std::cerr << "Record Missing Name! " << record.dump();
525 continue; // this should be impossible at this level
526 }
527
528 std::string deviceName = generateDeviceName(
529 usedNames, dbusObject, foundDeviceIdx, getName.value(), replaceStr);
530 getName.value() = deviceName;
531 usedNames.insert(deviceName);
532
533 for (auto keyPair = record.begin(); keyPair != record.end(); keyPair++)
534 {
535 if (keyPair.key() != "Name")
536 {
Christopher Meis59ef1e72025-04-16 08:53:25 +0200537 em_utils::templateCharReplace(keyPair, dbusObject,
538 foundDeviceIdx, replaceStr);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930539 }
540 }
541
542 // insert into configuration temporarily to be able to
543 // reference ourselves
544
545 _systemConfiguration[recordName] = record;
546
547 auto findExpose = record.find("Exposes");
548 if (findExpose == record.end())
549 {
550 continue;
551 }
552
553 for (auto& expose : *findExpose)
554 {
555 for (auto keyPair = expose.begin(); keyPair != expose.end();
556 keyPair++)
557 {
Christopher Meis59ef1e72025-04-16 08:53:25 +0200558 em_utils::templateCharReplace(keyPair, dbusObject,
559 foundDeviceIdx, replaceStr);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930560
561 applyExposeActions(_systemConfiguration, recordName, expose,
562 keyPair);
563 }
564 }
565
566 // overwrite ourselves with cleaned up version
567 _systemConfiguration[recordName] = record;
568 _missingConfigurations.erase(recordName);
569 }
570}
571
Christopher Meis26fbbd52025-03-26 14:55:06 +0100572void scan::PerformScan::run()
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030573{
574 boost::container::flat_set<std::string> dbusProbeInterfaces;
Christopher Meis26fbbd52025-03-26 14:55:06 +0100575 std::vector<std::shared_ptr<probe::PerformProbe>> dbusProbePointers;
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030576
577 for (auto it = _configurations.begin(); it != _configurations.end();)
578 {
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030579 // check for poorly formatted fields, probe must be an array
Andrew Jeffery38834872022-04-19 15:15:57 +0930580 auto findProbe = it->find("Probe");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030581 if (findProbe == it->end())
582 {
583 std::cerr << "configuration file missing probe:\n " << *it << "\n";
584 it = _configurations.erase(it);
585 continue;
586 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030587
Andrew Jeffery38834872022-04-19 15:15:57 +0930588 auto findName = it->find("Name");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030589 if (findName == it->end())
590 {
591 std::cerr << "configuration file missing name:\n " << *it << "\n";
592 it = _configurations.erase(it);
593 continue;
594 }
595 std::string probeName = *findName;
596
597 if (std::find(passedProbes.begin(), passedProbes.end(), probeName) !=
598 passedProbes.end())
599 {
600 it = _configurations.erase(it);
601 continue;
602 }
Andrew Jeffery38834872022-04-19 15:15:57 +0930603
Andrew Jeffery98f3d532022-04-19 15:29:22 +0930604 nlohmann::json& recordRef = *it;
Andrew Jeffery38834872022-04-19 15:15:57 +0930605 nlohmann::json probeCommand;
606 if ((*findProbe).type() != nlohmann::json::value_t::array)
607 {
608 probeCommand = nlohmann::json::array();
609 probeCommand.push_back(*findProbe);
610 }
611 else
612 {
613 probeCommand = *findProbe;
614 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030615
616 // store reference to this to children to makes sure we don't get
617 // destroyed too early
618 auto thisRef = shared_from_this();
Christopher Meis26fbbd52025-03-26 14:55:06 +0100619 auto probePointer = std::make_shared<probe::PerformProbe>(
Andrew Jefferyea4ff022022-04-21 12:31:40 +0930620 recordRef, probeCommand, probeName, thisRef);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030621
622 // parse out dbus probes by discarding other probe types, store in a
623 // map
624 for (const nlohmann::json& probeJson : probeCommand)
625 {
626 const std::string* probe = probeJson.get_ptr<const std::string*>();
627 if (probe == nullptr)
628 {
629 std::cerr << "Probe statement wasn't a string, can't parse";
630 continue;
631 }
Christopher Meis26fbbd52025-03-26 14:55:06 +0100632 if (probe::findProbeType(*probe))
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030633 {
634 continue;
635 }
636 // syntax requires probe before first open brace
637 auto findStart = probe->find('(');
638 std::string interface = probe->substr(0, findStart);
639 dbusProbeInterfaces.emplace(interface);
640 dbusProbePointers.emplace_back(probePointer);
641 }
642 it++;
643 }
644
645 // probe vector stores a shared_ptr to each PerformProbe that cares
646 // about a dbus interface
647 findDbusObjects(std::move(dbusProbePointers),
648 std::move(dbusProbeInterfaces), shared_from_this());
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030649}
650
Christopher Meis26fbbd52025-03-26 14:55:06 +0100651scan::PerformScan::~PerformScan()
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030652{
653 if (_passed)
654 {
655 auto nextScan = std::make_shared<PerformScan>(
656 _systemConfiguration, _missingConfigurations, _configurations,
657 objServer, std::move(_callback));
658 nextScan->passedProbes = std::move(passedProbes);
659 nextScan->dbusProbeObjects = std::move(dbusProbeObjects);
660 nextScan->run();
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030661 }
662 else
663 {
664 _callback();
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030665 }
666}