blob: 85eb723cef5adc63f430e7e2e7702d854781cf2b [file] [log] [blame]
Alexander Hansen4e1142d2025-07-25 17:07:27 +02001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
3
Christopher Meis26fbbd52025-03-26 14:55:06 +01004#include "perform_scan.hpp"
5
Christopher Meis26fbbd52025-03-26 14:55:06 +01006#include "perform_probe.hpp"
Christopher Meis59ef1e72025-04-16 08:53:25 +02007#include "utils.hpp"
Andrew Jeffery47af65a2021-12-01 14:16:31 +10308
Andrew Jeffery47af65a2021-12-01 14:16:31 +10309#include <boost/asio/steady_timer.hpp>
Alexander Hansenc3db2c32024-08-20 15:01:38 +020010#include <phosphor-logging/lg2.hpp>
Andrew Jeffery47af65a2021-12-01 14:16:31 +103011
12#include <charconv>
Ed Tanousdbf95b22025-10-13 11:38:35 -070013#include <flat_map>
14#include <flat_set>
Andrew Jeffery47af65a2021-12-01 14:16:31 +103015
Andrew Jeffery47af65a2021-12-01 14:16:31 +103016using GetSubTreeType = std::vector<
17 std::pair<std::string,
18 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
19
20constexpr const int32_t maxMapperDepth = 0;
21
Andrew Jefferyd9b67842022-04-05 16:44:20 +093022struct DBusInterfaceInstance
23{
24 std::string busName;
25 std::string path;
26 std::string interface;
27};
28
Patrick Williams5a807032025-03-03 11:20:39 -050029void getInterfaces(
30 const DBusInterfaceInstance& instance,
Christopher Meis26fbbd52025-03-26 14:55:06 +010031 const std::vector<std::shared_ptr<probe::PerformProbe>>& probeVector,
Alexander Hansena555acf2025-06-27 11:59:10 +020032 const std::shared_ptr<scan::PerformScan>& scan, boost::asio::io_context& io,
33 size_t retries = 5)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103034{
Ed Tanous3013fb42022-07-09 08:27:06 -070035 if (retries == 0U)
Andrew Jeffery47af65a2021-12-01 14:16:31 +103036 {
Alexander Hansen8feb0452025-09-15 14:29:20 +020037 lg2::error("retries exhausted on {BUSNAME} {PATH} {INTF}", "BUSNAME",
38 instance.busName, "PATH", instance.path, "INTF",
39 instance.interface);
Andrew Jeffery47af65a2021-12-01 14:16:31 +103040 return;
41 }
42
Christopher Meiscf6a75b2025-06-03 07:53:50 +020043 scan->_em.systemBus->async_method_call(
Alexander Hansena555acf2025-06-27 11:59:10 +020044 [instance, scan, probeVector, retries,
Ed Tanousdbf95b22025-10-13 11:38:35 -070045 &io](boost::system::error_code& errc,
46 const DBusInterface& resp) mutable {
Patrick Williamsb7077432024-08-16 15:22:21 -040047 if (errc)
48 {
Alexander Hansen8feb0452025-09-15 14:29:20 +020049 lg2::error("error calling getall on {BUSNAME} {PATH} {INTF}",
50 "BUSNAME", instance.busName, "PATH", instance.path,
51 "INTF", instance.interface);
Andrew Jeffery47af65a2021-12-01 14:16:31 +103052
Patrick Williamsb7077432024-08-16 15:22:21 -040053 auto timer = std::make_shared<boost::asio::steady_timer>(io);
54 timer->expires_after(std::chrono::seconds(2));
Andrew Jeffery47af65a2021-12-01 14:16:31 +103055
Alexander Hansena555acf2025-06-27 11:59:10 +020056 timer->async_wait([timer, instance, scan, probeVector, retries,
57 &io](const boost::system::error_code&) {
58 getInterfaces(instance, probeVector, scan, io, retries - 1);
Patrick Williamsb7077432024-08-16 15:22:21 -040059 });
60 return;
61 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +103062
Ed Tanousdbf95b22025-10-13 11:38:35 -070063 scan->dbusProbeObjects[std::string(instance.path)]
64 [std::string(instance.interface)] = resp;
Patrick Williamsb7077432024-08-16 15:22:21 -040065 },
Andrew Jefferyd9b67842022-04-05 16:44:20 +093066 instance.busName, instance.path, "org.freedesktop.DBus.Properties",
67 "GetAll", instance.interface);
Andrew Jeffery47af65a2021-12-01 14:16:31 +103068}
69
Patrick Williams5a807032025-03-03 11:20:39 -050070static void processDbusObjects(
Christopher Meis26fbbd52025-03-26 14:55:06 +010071 std::vector<std::shared_ptr<probe::PerformProbe>>& probeVector,
72 const std::shared_ptr<scan::PerformScan>& scan,
Alexander Hansena555acf2025-06-27 11:59:10 +020073 const GetSubTreeType& interfaceSubtree, boost::asio::io_context& io)
Andrew Jeffery8d2761e2022-04-05 16:26:28 +093074{
Andrew Jeffery8d2761e2022-04-05 16:26:28 +093075 for (const auto& [path, object] : interfaceSubtree)
76 {
Andrew Jeffery47321832022-04-05 16:30:29 +093077 // Get a PropertiesChanged callback for all interfaces on this path.
Christopher Meiscf6a75b2025-06-03 07:53:50 +020078 scan->_em.registerCallback(path);
Andrew Jeffery47321832022-04-05 16:30:29 +093079
Andrew Jeffery8d2761e2022-04-05 16:26:28 +093080 for (const auto& [busname, ifaces] : object)
81 {
82 for (const std::string& iface : ifaces)
83 {
84 // The 3 default org.freedeskstop interfaces (Peer,
85 // Introspectable, and Properties) are returned by
86 // the mapper but don't have properties, so don't bother
87 // with the GetAll call to save some cycles.
George Liu164af2f2025-08-21 17:16:31 +080088 if (!iface.starts_with("org.freedesktop"))
Andrew Jeffery8d2761e2022-04-05 16:26:28 +093089 {
Alexander Hansena555acf2025-06-27 11:59:10 +020090 getInterfaces({busname, path, iface}, probeVector, scan,
91 io);
Andrew Jeffery8d2761e2022-04-05 16:26:28 +093092 }
93 }
94 }
Andrew Jeffery8d2761e2022-04-05 16:26:28 +093095 }
96}
97
Andrew Jeffery47af65a2021-12-01 14:16:31 +103098// Populates scan->dbusProbeObjects with all interfaces and properties
99// for the paths that own the interfaces passed in.
Christopher Meis26fbbd52025-03-26 14:55:06 +0100100void findDbusObjects(
101 std::vector<std::shared_ptr<probe::PerformProbe>>&& probeVector,
Ed Tanousdbf95b22025-10-13 11:38:35 -0700102 std::flat_set<std::string, std::less<>>&& interfaces,
Alexander Hansena555acf2025-06-27 11:59:10 +0200103 const std::shared_ptr<scan::PerformScan>& scan, boost::asio::io_context& io,
104 size_t retries = 5)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030105{
106 // Filter out interfaces already obtained.
107 for (const auto& [path, probeInterfaces] : scan->dbusProbeObjects)
108 {
109 for (const auto& [interface, _] : probeInterfaces)
110 {
111 interfaces.erase(interface);
112 }
113 }
114 if (interfaces.empty())
115 {
116 return;
117 }
118
119 // find all connections in the mapper that expose a specific type
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200120 scan->_em.systemBus->async_method_call(
Alexander Hansena555acf2025-06-27 11:59:10 +0200121 [interfaces, probeVector{std::move(probeVector)}, scan, retries,
122 &io](boost::system::error_code& ec,
123 const GetSubTreeType& interfaceSubtree) mutable {
Patrick Williamsb7077432024-08-16 15:22:21 -0400124 if (ec)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030125 {
Patrick Williamsb7077432024-08-16 15:22:21 -0400126 if (ec.value() == ENOENT)
127 {
128 return; // wasn't found by mapper
129 }
Alexander Hansen8feb0452025-09-15 14:29:20 +0200130 lg2::error("Error communicating to mapper.");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030131
Patrick Williamsb7077432024-08-16 15:22:21 -0400132 if (retries == 0U)
133 {
134 // if we can't communicate to the mapper something is very
135 // wrong
136 std::exit(EXIT_FAILURE);
137 }
138
139 auto timer = std::make_shared<boost::asio::steady_timer>(io);
140 timer->expires_after(std::chrono::seconds(10));
141
142 timer->async_wait(
143 [timer, interfaces{std::move(interfaces)}, scan,
Alexander Hansena555acf2025-06-27 11:59:10 +0200144 probeVector{std::move(probeVector)}, retries,
145 &io](const boost::system::error_code&) mutable {
Patrick Williamsb7077432024-08-16 15:22:21 -0400146 findDbusObjects(std::move(probeVector),
Alexander Hansena555acf2025-06-27 11:59:10 +0200147 std::move(interfaces), scan, io,
Patrick Williamsb7077432024-08-16 15:22:21 -0400148 retries - 1);
149 });
150 return;
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030151 }
152
Alexander Hansena555acf2025-06-27 11:59:10 +0200153 processDbusObjects(probeVector, scan, interfaceSubtree, io);
Patrick Williamsb7077432024-08-16 15:22:21 -0400154 },
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030155 "xyz.openbmc_project.ObjectMapper",
156 "/xyz/openbmc_project/object_mapper",
157 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", maxMapperDepth,
158 interfaces);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030159}
160
Andrew Jeffery09a09a62022-04-12 22:49:57 +0930161static std::string getRecordName(const DBusInterface& probe,
162 const std::string& probeName)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030163{
164 if (probe.empty())
165 {
166 return probeName;
167 }
168
Andrew Jeffery928c5b22022-04-05 16:57:51 +0930169 // use an array so alphabetical order from the flat_map is maintained
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030170 auto device = nlohmann::json::array();
Ed Tanous3013fb42022-07-09 08:27:06 -0700171 for (const auto& devPair : probe)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030172 {
173 device.push_back(devPair.first);
174 std::visit([&device](auto&& v) { device.push_back(v); },
175 devPair.second);
176 }
Andrew Jeffery86501872022-04-05 16:58:22 +0930177
Andrew Jeffery928c5b22022-04-05 16:57:51 +0930178 // hashes are hard to distinguish, use the non-hashed version if we want
179 // debug
Alexander Hansenc3db2c32024-08-20 15:01:38 +0200180 // return probeName + device.dump();
Andrew Jeffery1ae4ed62022-04-05 16:55:43 +0930181
Andrew Jeffery86501872022-04-05 16:58:22 +0930182 return std::to_string(std::hash<std::string>{}(probeName + device.dump()));
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030183}
184
Alexander Hansena555acf2025-06-27 11:59:10 +0200185scan::PerformScan::PerformScan(
186 EntityManager& em, nlohmann::json& missingConfigurations,
Christopher Meisf7252572025-06-11 13:22:05 +0200187 std::vector<nlohmann::json>& configurations, boost::asio::io_context& io,
Alexander Hansena555acf2025-06-27 11:59:10 +0200188 std::function<void()>&& callback) :
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200189 _em(em), _missingConfigurations(missingConfigurations),
Alexander Hansena555acf2025-06-27 11:59:10 +0200190 _configurations(configurations), _callback(std::move(callback)), io(io)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030191{}
Andrew Jefferydb451482022-04-13 16:53:22 +0930192
Andrew Jeffery7a7faed2022-04-13 17:24:56 +0930193static void pruneRecordExposes(nlohmann::json& record)
Andrew Jefferydb451482022-04-13 16:53:22 +0930194{
Andrew Jeffery7a7faed2022-04-13 17:24:56 +0930195 auto findExposes = record.find("Exposes");
Andrew Jeffery5f051452022-04-13 17:11:37 +0930196 if (findExposes == record.end())
Andrew Jefferydb451482022-04-13 16:53:22 +0930197 {
Andrew Jeffery5f051452022-04-13 17:11:37 +0930198 return;
Andrew Jefferydb451482022-04-13 16:53:22 +0930199 }
Andrew Jeffery5f051452022-04-13 17:11:37 +0930200
201 auto copy = nlohmann::json::array();
202 for (auto& expose : *findExposes)
203 {
Andrew Jeffery742a7eb2022-04-13 17:14:46 +0930204 if (!expose.is_null())
Andrew Jeffery5f051452022-04-13 17:11:37 +0930205 {
Andrew Jeffery742a7eb2022-04-13 17:14:46 +0930206 copy.emplace_back(expose);
Andrew Jeffery5f051452022-04-13 17:11:37 +0930207 }
Andrew Jeffery5f051452022-04-13 17:11:37 +0930208 }
209 *findExposes = copy;
Andrew Jefferydb451482022-04-13 16:53:22 +0930210}
211
Patrick Williamsb7077432024-08-16 15:22:21 -0400212static void recordDiscoveredIdentifiers(
213 std::set<nlohmann::json>& usedNames, std::list<size_t>& indexes,
214 const std::string& probeName, const nlohmann::json& record)
Andrew Jeffery6addc022022-04-13 18:08:58 +0930215{
216 size_t indexIdx = probeName.find('$');
Andrew Jefferyba41b622022-04-13 18:13:28 +0930217 if (indexIdx == std::string::npos)
Andrew Jeffery6addc022022-04-13 18:08:58 +0930218 {
219 return;
220 }
221
Andrew Jefferyc0da0cc2022-04-13 18:15:09 +0930222 auto nameIt = record.find("Name");
223 if (nameIt == record.end())
Andrew Jeffery6addc022022-04-13 18:08:58 +0930224 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200225 lg2::error("Last JSON Illegal");
Andrew Jeffery6addc022022-04-13 18:08:58 +0930226 return;
227 }
228
229 int index = 0;
230 auto str = nameIt->get<std::string>().substr(indexIdx);
Ed Tanous3013fb42022-07-09 08:27:06 -0700231 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
232 const char* endPtr = str.data() + str.size();
233 auto [p, ec] = std::from_chars(str.data(), endPtr, index);
Andrew Jeffery6addc022022-04-13 18:08:58 +0930234 if (ec != std::errc())
235 {
236 return; // non-numeric replacement
237 }
238
239 usedNames.insert(nameIt.value());
240
241 auto usedIt = std::find(indexes.begin(), indexes.end(), index);
242 if (usedIt != indexes.end())
243 {
244 indexes.erase(usedIt);
245 }
246}
247
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930248static bool extractExposeActionRecordNames(std::vector<std::string>& matches,
249 nlohmann::json::iterator& keyPair)
250{
Andrew Jefferya2449842022-04-14 15:35:51 +0930251 if (keyPair.value().is_string())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930252 {
253 matches.emplace_back(keyPair.value());
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930254 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930255 }
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930256
Andrew Jefferya2449842022-04-14 15:35:51 +0930257 if (keyPair.value().is_array())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930258 {
259 for (const auto& value : keyPair.value())
260 {
Andrew Jefferya2449842022-04-14 15:35:51 +0930261 if (!value.is_string())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930262 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200263 lg2::error("Value is invalid type {VALUE}", "VALUE", value);
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930264 break;
265 }
266 matches.emplace_back(value);
267 }
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930268
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930269 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930270 }
271
Alexander Hansen8feb0452025-09-15 14:29:20 +0200272 lg2::error("Value is invalid type {KEY}", "KEY", keyPair.key());
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930273
274 return false;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930275}
276
Patrick Williamsb7077432024-08-16 15:22:21 -0400277static std::optional<std::vector<std::string>::iterator> findExposeActionRecord(
278 std::vector<std::string>& matches, const nlohmann::json& record)
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930279{
Andrew Jeffery1a02da82022-04-14 20:39:06 +0930280 const auto& name = (record)["Name"].get_ref<const std::string&>();
281 auto compare = [&name](const std::string& s) { return s == name; };
282 auto matchIt = std::find_if(matches.begin(), matches.end(), compare);
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930283
284 if (matchIt == matches.end())
285 {
286 return std::nullopt;
287 }
288
289 return matchIt;
290}
291
Andrew Jefferycf11bd32022-04-14 18:34:31 +0930292static void applyBindExposeAction(nlohmann::json& exposedObject,
293 nlohmann::json& expose,
294 const std::string& propertyName)
295{
George Liu164af2f2025-08-21 17:16:31 +0800296 if (propertyName.starts_with("Bind"))
Andrew Jefferycf11bd32022-04-14 18:34:31 +0930297 {
298 std::string bind = propertyName.substr(sizeof("Bind") - 1);
299 exposedObject["Status"] = "okay";
300 expose[bind] = exposedObject;
301 }
302}
303
304static void applyDisableExposeAction(nlohmann::json& exposedObject,
305 const std::string& propertyName)
306{
307 if (propertyName == "DisableNode")
308 {
309 exposedObject["Status"] = "disabled";
310 }
311}
312
Patrick Williamsb7077432024-08-16 15:22:21 -0400313static void applyConfigExposeActions(
314 std::vector<std::string>& matches, nlohmann::json& expose,
315 const std::string& propertyName, nlohmann::json& configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930316{
Andrew Jefferyda45e5e2022-04-14 21:12:02 +0930317 for (auto& exposedObject : configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930318 {
319 auto match = findExposeActionRecord(matches, exposedObject);
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930320 if (match)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930321 {
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930322 matches.erase(*match);
323 applyBindExposeAction(exposedObject, expose, propertyName);
324 applyDisableExposeAction(exposedObject, propertyName);
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930325 }
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930326 }
327}
328
Patrick Williamsb7077432024-08-16 15:22:21 -0400329static void applyExposeActions(
330 nlohmann::json& systemConfiguration, const std::string& recordName,
331 nlohmann::json& expose, nlohmann::json::iterator& keyPair)
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930332{
George Liu164af2f2025-08-21 17:16:31 +0800333 bool isBind = keyPair.key().starts_with("Bind");
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930334 bool isDisable = keyPair.key() == "DisableNode";
335 bool isExposeAction = isBind || isDisable;
336
337 if (!isExposeAction)
338 {
339 return;
340 }
341
342 std::vector<std::string> matches;
343
344 if (!extractExposeActionRecordNames(matches, keyPair))
345 {
346 return;
347 }
348
Patrick Williams2594d362022-09-28 06:46:24 -0500349 for (const auto& [configId, config] : systemConfiguration.items())
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930350 {
351 // don't disable ourselves
352 if (isDisable && configId == recordName)
353 {
354 continue;
355 }
356
357 auto configListFind = config.find("Exposes");
358 if (configListFind == config.end())
359 {
360 continue;
361 }
362
363 if (!configListFind->is_array())
364 {
365 continue;
366 }
367
368 applyConfigExposeActions(matches, expose, keyPair.key(),
369 *configListFind);
370 }
371
372 if (!matches.empty())
373 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200374 lg2::error(
375 "configuration file dependency error, could not find {KEY} {VALUE}",
376 "KEY", keyPair.key(), "VALUE", keyPair.value());
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930377 }
378}
379
Patrick Williamsb7077432024-08-16 15:22:21 -0400380static std::string generateDeviceName(
381 const std::set<nlohmann::json>& usedNames, const DBusObject& dbusObject,
382 size_t foundDeviceIdx, const std::string& nameTemplate,
383 std::optional<std::string>& replaceStr)
Andrew Jefferydabee982022-04-19 13:27:24 +0930384{
385 nlohmann::json copyForName = {{"Name", nameTemplate}};
386 nlohmann::json::iterator copyIt = copyForName.begin();
Christopher Meis59ef1e72025-04-16 08:53:25 +0200387 std::optional<std::string> replaceVal = em_utils::templateCharReplace(
388 copyIt, dbusObject, foundDeviceIdx, replaceStr);
Andrew Jefferydabee982022-04-19 13:27:24 +0930389
390 if (!replaceStr && replaceVal)
391 {
Patrick Williams64599932025-05-14 07:24:20 -0400392 if (usedNames.contains(copyIt.value()))
Andrew Jefferydabee982022-04-19 13:27:24 +0930393 {
394 replaceStr = replaceVal;
395 copyForName = {{"Name", nameTemplate}};
396 copyIt = copyForName.begin();
Christopher Meis59ef1e72025-04-16 08:53:25 +0200397 em_utils::templateCharReplace(copyIt, dbusObject, foundDeviceIdx,
398 replaceStr);
Andrew Jefferydabee982022-04-19 13:27:24 +0930399 }
400 }
401
402 if (replaceStr)
403 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200404 lg2::error(
405 "Duplicates found, replacing {STR} with found device index. Consider fixing template to not have duplicates",
406 "STR", *replaceStr);
Andrew Jefferydabee982022-04-19 13:27:24 +0930407 }
408
409 return copyIt.value();
410}
411
Christopher Meis26fbbd52025-03-26 14:55:06 +0100412void scan::PerformScan::updateSystemConfiguration(
413 const nlohmann::json& recordRef, const std::string& probeName,
414 FoundDevices& foundDevices)
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930415{
416 _passed = true;
417 passedProbes.push_back(probeName);
418
419 std::set<nlohmann::json> usedNames;
420 std::list<size_t> indexes(foundDevices.size());
421 std::iota(indexes.begin(), indexes.end(), 1);
422
423 // copy over persisted configurations and make sure we remove
424 // indexes that are already used
425 for (auto itr = foundDevices.begin(); itr != foundDevices.end();)
426 {
427 std::string recordName = getRecordName(itr->interface, probeName);
428
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200429 auto record = _em.systemConfiguration.find(recordName);
430 if (record == _em.systemConfiguration.end())
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930431 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200432 record = _em.lastJson.find(recordName);
433 if (record == _em.lastJson.end())
Zhikui Ren1f77d9c2022-08-29 16:02:37 -0700434 {
435 itr++;
436 continue;
437 }
438
439 pruneRecordExposes(*record);
440
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200441 _em.systemConfiguration[recordName] = *record;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930442 }
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930443 _missingConfigurations.erase(recordName);
444
445 // We've processed the device, remove it and advance the
446 // iterator
447 itr = foundDevices.erase(itr);
JinFuLin8f05a3a2022-11-21 15:33:24 +0800448 recordDiscoveredIdentifiers(usedNames, indexes, probeName, *record);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930449 }
450
451 std::optional<std::string> replaceStr;
452
453 DBusObject emptyObject;
454 DBusInterface emptyInterface;
455 emptyObject.emplace(std::string{}, emptyInterface);
456
457 for (const auto& [foundDevice, path] : foundDevices)
458 {
459 // Need all interfaces on this path so that template
460 // substitutions can be done with any of the contained
461 // properties. If the probe that passed didn't use an
462 // interface, such as if it was just TRUE, then
463 // templateCharReplace will just get passed in an empty
464 // map.
Andrew Jefferyaf9b46b2022-04-21 12:34:43 +0930465 auto objectIt = dbusProbeObjects.find(path);
466 const DBusObject& dbusObject = (objectIt == dbusProbeObjects.end())
467 ? emptyObject
468 : objectIt->second;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930469
470 nlohmann::json record = recordRef;
471 std::string recordName = getRecordName(foundDevice, probeName);
472 size_t foundDeviceIdx = indexes.front();
473 indexes.pop_front();
474
475 // check name first so we have no duplicate names
476 auto getName = record.find("Name");
477 if (getName == record.end())
478 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200479 lg2::error("Record Missing Name! {JSON}", "JSON", record.dump());
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930480 continue; // this should be impossible at this level
481 }
482
483 std::string deviceName = generateDeviceName(
484 usedNames, dbusObject, foundDeviceIdx, getName.value(), replaceStr);
485 getName.value() = deviceName;
486 usedNames.insert(deviceName);
487
488 for (auto keyPair = record.begin(); keyPair != record.end(); keyPair++)
489 {
490 if (keyPair.key() != "Name")
491 {
Chau Ly8ee43692025-10-09 07:22:08 +0000492 // "Probe" string does not contain template variables
493 // Handle left-over variables for "Exposes" later below
494 const bool handleLeftOver =
495 (keyPair.key() != "Probe") && (keyPair.key() != "Exposes");
Christopher Meis59ef1e72025-04-16 08:53:25 +0200496 em_utils::templateCharReplace(keyPair, dbusObject,
Chau Ly8ee43692025-10-09 07:22:08 +0000497 foundDeviceIdx, replaceStr,
498 handleLeftOver);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930499 }
500 }
501
502 // insert into configuration temporarily to be able to
503 // reference ourselves
504
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200505 _em.systemConfiguration[recordName] = record;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930506
507 auto findExpose = record.find("Exposes");
508 if (findExpose == record.end())
509 {
510 continue;
511 }
512
513 for (auto& expose : *findExpose)
514 {
515 for (auto keyPair = expose.begin(); keyPair != expose.end();
516 keyPair++)
517 {
Christopher Meis59ef1e72025-04-16 08:53:25 +0200518 em_utils::templateCharReplace(keyPair, dbusObject,
519 foundDeviceIdx, replaceStr);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930520
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200521 applyExposeActions(_em.systemConfiguration, recordName, expose,
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930522 keyPair);
523 }
524 }
525
Christopher Meis9a5eec92025-08-07 08:43:03 +0200526 // If we end up here and the path is empty, we have Probe: "True"
527 // and we dont want that to show up in the associations.
528 if (!path.empty())
529 {
530 auto boardType = record.find("Type")->get<std::string>();
531 auto boardName = record.find("Name")->get<std::string>();
532 std::string boardInventoryPath =
533 em_utils::buildInventorySystemPath(boardName, boardType);
534 _em.topology.addProbePath(boardInventoryPath, path);
535 }
536
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930537 // overwrite ourselves with cleaned up version
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200538 _em.systemConfiguration[recordName] = record;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930539 _missingConfigurations.erase(recordName);
540 }
541}
542
Christopher Meis26fbbd52025-03-26 14:55:06 +0100543void scan::PerformScan::run()
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030544{
Ed Tanousdbf95b22025-10-13 11:38:35 -0700545 std::flat_set<std::string, std::less<>> dbusProbeInterfaces;
Christopher Meis26fbbd52025-03-26 14:55:06 +0100546 std::vector<std::shared_ptr<probe::PerformProbe>> dbusProbePointers;
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030547
548 for (auto it = _configurations.begin(); it != _configurations.end();)
549 {
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030550 // check for poorly formatted fields, probe must be an array
Andrew Jeffery38834872022-04-19 15:15:57 +0930551 auto findProbe = it->find("Probe");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030552 if (findProbe == it->end())
553 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200554 lg2::error("configuration file missing probe:\n {JSON}", "JSON",
555 *it);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030556 it = _configurations.erase(it);
557 continue;
558 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030559
Andrew Jeffery38834872022-04-19 15:15:57 +0930560 auto findName = it->find("Name");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030561 if (findName == it->end())
562 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200563 lg2::error("configuration file missing name:\n {JSON}", "JSON",
564 *it);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030565 it = _configurations.erase(it);
566 continue;
567 }
568 std::string probeName = *findName;
569
570 if (std::find(passedProbes.begin(), passedProbes.end(), probeName) !=
571 passedProbes.end())
572 {
573 it = _configurations.erase(it);
574 continue;
575 }
Andrew Jeffery38834872022-04-19 15:15:57 +0930576
Andrew Jeffery98f3d532022-04-19 15:29:22 +0930577 nlohmann::json& recordRef = *it;
Andrew Jeffery38834872022-04-19 15:15:57 +0930578 nlohmann::json probeCommand;
579 if ((*findProbe).type() != nlohmann::json::value_t::array)
580 {
581 probeCommand = nlohmann::json::array();
582 probeCommand.push_back(*findProbe);
583 }
584 else
585 {
586 probeCommand = *findProbe;
587 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030588
589 // store reference to this to children to makes sure we don't get
590 // destroyed too early
591 auto thisRef = shared_from_this();
Christopher Meis26fbbd52025-03-26 14:55:06 +0100592 auto probePointer = std::make_shared<probe::PerformProbe>(
Andrew Jefferyea4ff022022-04-21 12:31:40 +0930593 recordRef, probeCommand, probeName, thisRef);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030594
595 // parse out dbus probes by discarding other probe types, store in a
596 // map
597 for (const nlohmann::json& probeJson : probeCommand)
598 {
599 const std::string* probe = probeJson.get_ptr<const std::string*>();
600 if (probe == nullptr)
601 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200602 lg2::error("Probe statement wasn't a string, can't parse");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030603 continue;
604 }
Christopher Meis26fbbd52025-03-26 14:55:06 +0100605 if (probe::findProbeType(*probe))
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030606 {
607 continue;
608 }
609 // syntax requires probe before first open brace
610 auto findStart = probe->find('(');
611 std::string interface = probe->substr(0, findStart);
612 dbusProbeInterfaces.emplace(interface);
613 dbusProbePointers.emplace_back(probePointer);
614 }
615 it++;
616 }
617
618 // probe vector stores a shared_ptr to each PerformProbe that cares
619 // about a dbus interface
620 findDbusObjects(std::move(dbusProbePointers),
Alexander Hansena555acf2025-06-27 11:59:10 +0200621 std::move(dbusProbeInterfaces), shared_from_this(), io);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030622}
623
Christopher Meis26fbbd52025-03-26 14:55:06 +0100624scan::PerformScan::~PerformScan()
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030625{
626 if (_passed)
627 {
628 auto nextScan = std::make_shared<PerformScan>(
Alexander Hansena555acf2025-06-27 11:59:10 +0200629 _em, _missingConfigurations, _configurations, io,
630 std::move(_callback));
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030631 nextScan->passedProbes = std::move(passedProbes);
632 nextScan->dbusProbeObjects = std::move(dbusProbeObjects);
633 nextScan->run();
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030634 }
635 else
636 {
637 _callback();
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030638 }
639}