blob: c126c0f5e2b19f30786fc8ea690752154cd9000a [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,
Ed Tanous77192692025-06-24 11:32:12 -0700249 const std::string& exposeKey,
250 nlohmann::json& exposeValue)
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930251{
Ed Tanous77192692025-06-24 11:32:12 -0700252 const std::string* exposeValueStr =
253 exposeValue.get_ptr<const std::string*>();
254 if (exposeValueStr != nullptr)
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930255 {
Ed Tanous77192692025-06-24 11:32:12 -0700256 matches.emplace_back(*exposeValueStr);
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930257 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930258 }
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930259
Ed Tanous77192692025-06-24 11:32:12 -0700260 const nlohmann::json::array_t* exarr =
261 exposeValue.get_ptr<const nlohmann::json::array_t*>();
262 if (exarr != nullptr)
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930263 {
Ed Tanous77192692025-06-24 11:32:12 -0700264 for (const auto& value : *exarr)
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930265 {
Andrew Jefferya2449842022-04-14 15:35:51 +0930266 if (!value.is_string())
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930267 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200268 lg2::error("Value is invalid type {VALUE}", "VALUE", value);
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930269 break;
270 }
271 matches.emplace_back(value);
272 }
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930273
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930274 return true;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930275 }
276
Ed Tanous77192692025-06-24 11:32:12 -0700277 lg2::error("Value is invalid type {KEY}", "KEY", exposeKey);
Andrew Jefferyba3c50c2022-04-14 15:34:38 +0930278 return false;
Andrew Jeffery6890ae22022-04-14 15:33:01 +0930279}
280
Patrick Williamsb7077432024-08-16 15:22:21 -0400281static std::optional<std::vector<std::string>::iterator> findExposeActionRecord(
282 std::vector<std::string>& matches, const nlohmann::json& record)
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930283{
Andrew Jeffery1a02da82022-04-14 20:39:06 +0930284 const auto& name = (record)["Name"].get_ref<const std::string&>();
285 auto compare = [&name](const std::string& s) { return s == name; };
286 auto matchIt = std::find_if(matches.begin(), matches.end(), compare);
Andrew Jeffery6de53fb2022-04-14 15:45:38 +0930287
288 if (matchIt == matches.end())
289 {
290 return std::nullopt;
291 }
292
293 return matchIt;
294}
295
Ed Tanous77192692025-06-24 11:32:12 -0700296static void applyBindExposeAction(nlohmann::json::object_t& exposedObject,
297 nlohmann::json::object_t& expose,
Andrew Jefferycf11bd32022-04-14 18:34:31 +0930298 const std::string& propertyName)
299{
George Liu164af2f2025-08-21 17:16:31 +0800300 if (propertyName.starts_with("Bind"))
Andrew Jefferycf11bd32022-04-14 18:34:31 +0930301 {
302 std::string bind = propertyName.substr(sizeof("Bind") - 1);
303 exposedObject["Status"] = "okay";
304 expose[bind] = exposedObject;
305 }
306}
307
Ed Tanous77192692025-06-24 11:32:12 -0700308static void applyDisableExposeAction(nlohmann::json::object_t& exposedObject,
Andrew Jefferycf11bd32022-04-14 18:34:31 +0930309 const std::string& propertyName)
310{
311 if (propertyName == "DisableNode")
312 {
313 exposedObject["Status"] = "disabled";
314 }
315}
316
Patrick Williamsb7077432024-08-16 15:22:21 -0400317static void applyConfigExposeActions(
Ed Tanous77192692025-06-24 11:32:12 -0700318 std::vector<std::string>& matches, nlohmann::json::object_t& expose,
319 const std::string& propertyName, nlohmann::json::array_t& configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930320{
Andrew Jefferyda45e5e2022-04-14 21:12:02 +0930321 for (auto& exposedObject : configExposes)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930322 {
323 auto match = findExposeActionRecord(matches, exposedObject);
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930324 if (match)
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930325 {
Andrew Jeffery060ac9c2022-04-14 21:11:18 +0930326 matches.erase(*match);
Ed Tanous77192692025-06-24 11:32:12 -0700327 nlohmann::json::object_t* exposedObjectObj =
328 exposedObject.get_ptr<nlohmann::json::object_t*>();
329 if (exposedObjectObj == nullptr)
330 {
331 lg2::error("Exposed object wasn't a object: {JSON}", "JSON",
332 exposedObject.dump());
333 continue;
334 }
335
336 applyBindExposeAction(*exposedObjectObj, expose, propertyName);
337 applyDisableExposeAction(*exposedObjectObj, propertyName);
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930338 }
Andrew Jeffery5468f8e2022-04-14 21:08:31 +0930339 }
340}
341
Patrick Williamsb7077432024-08-16 15:22:21 -0400342static void applyExposeActions(
343 nlohmann::json& systemConfiguration, const std::string& recordName,
Ed Tanous77192692025-06-24 11:32:12 -0700344 nlohmann::json::object_t& expose, const std::string& exposeKey,
345 nlohmann::json& exposeValue)
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930346{
Ed Tanous77192692025-06-24 11:32:12 -0700347 bool isBind = exposeKey.starts_with("Bind");
348 bool isDisable = exposeKey == "DisableNode";
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930349 bool isExposeAction = isBind || isDisable;
350
351 if (!isExposeAction)
352 {
353 return;
354 }
355
356 std::vector<std::string> matches;
357
Ed Tanous77192692025-06-24 11:32:12 -0700358 if (!extractExposeActionRecordNames(matches, exposeKey, exposeValue))
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930359 {
360 return;
361 }
362
Patrick Williams2594d362022-09-28 06:46:24 -0500363 for (const auto& [configId, config] : systemConfiguration.items())
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930364 {
365 // don't disable ourselves
366 if (isDisable && configId == recordName)
367 {
368 continue;
369 }
370
371 auto configListFind = config.find("Exposes");
372 if (configListFind == config.end())
373 {
374 continue;
375 }
376
Ed Tanous77192692025-06-24 11:32:12 -0700377 nlohmann::json::array_t* configList =
378 configListFind->get_ptr<nlohmann::json::array_t*>();
379 if (configList == nullptr)
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930380 {
381 continue;
382 }
Ed Tanous77192692025-06-24 11:32:12 -0700383 applyConfigExposeActions(matches, expose, exposeKey, *configList);
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930384 }
385
386 if (!matches.empty())
387 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200388 lg2::error(
389 "configuration file dependency error, could not find {KEY} {VALUE}",
Ed Tanous77192692025-06-24 11:32:12 -0700390 "KEY", exposeKey, "VALUE", exposeValue);
Andrew Jefferyb48779d2022-04-14 21:40:31 +0930391 }
392}
393
Patrick Williamsb7077432024-08-16 15:22:21 -0400394static std::string generateDeviceName(
395 const std::set<nlohmann::json>& usedNames, const DBusObject& dbusObject,
396 size_t foundDeviceIdx, const std::string& nameTemplate,
397 std::optional<std::string>& replaceStr)
Andrew Jefferydabee982022-04-19 13:27:24 +0930398{
Ed Tanous77192692025-06-24 11:32:12 -0700399 nlohmann::json copyForName = nameTemplate;
Christopher Meis59ef1e72025-04-16 08:53:25 +0200400 std::optional<std::string> replaceVal = em_utils::templateCharReplace(
Ed Tanous77192692025-06-24 11:32:12 -0700401 copyForName, dbusObject, foundDeviceIdx, replaceStr);
Andrew Jefferydabee982022-04-19 13:27:24 +0930402
403 if (!replaceStr && replaceVal)
404 {
Ed Tanous77192692025-06-24 11:32:12 -0700405 if (usedNames.contains(nameTemplate))
Andrew Jefferydabee982022-04-19 13:27:24 +0930406 {
407 replaceStr = replaceVal;
Ed Tanous77192692025-06-24 11:32:12 -0700408 em_utils::templateCharReplace(copyForName, dbusObject,
409 foundDeviceIdx, replaceStr);
Andrew Jefferydabee982022-04-19 13:27:24 +0930410 }
411 }
412
413 if (replaceStr)
414 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200415 lg2::error(
416 "Duplicates found, replacing {STR} with found device index. Consider fixing template to not have duplicates",
417 "STR", *replaceStr);
Andrew Jefferydabee982022-04-19 13:27:24 +0930418 }
Ed Tanous77192692025-06-24 11:32:12 -0700419 const std::string* ret = copyForName.get_ptr<const std::string*>();
420 if (ret == nullptr)
421 {
422 lg2::error("Device name wasn't a string: ${JSON}", "JSON",
423 copyForName.dump());
424 return "";
425 }
426 return *ret;
Andrew Jefferydabee982022-04-19 13:27:24 +0930427}
Ed Tanous77192692025-06-24 11:32:12 -0700428static void applyTemplateAndExposeActions(
429 const std::string& recordName, const DBusObject& dbusObject,
430 size_t foundDeviceIdx, const std::optional<std::string>& replaceStr,
431 nlohmann::json& value, nlohmann::json& systemConfiguration)
432{
433 nlohmann::json::object_t* exposeObj =
434 value.get_ptr<nlohmann::json::object_t*>();
435 if (exposeObj != nullptr)
436 {
437 return;
438 }
439 for (auto& [key, value] : *exposeObj)
440 {
441 em_utils::templateCharReplace(value, dbusObject, foundDeviceIdx,
442 replaceStr);
443
444 applyExposeActions(systemConfiguration, recordName, *exposeObj, key,
445 value);
446 }
447};
Andrew Jefferydabee982022-04-19 13:27:24 +0930448
Christopher Meis26fbbd52025-03-26 14:55:06 +0100449void scan::PerformScan::updateSystemConfiguration(
450 const nlohmann::json& recordRef, const std::string& probeName,
451 FoundDevices& foundDevices)
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930452{
453 _passed = true;
454 passedProbes.push_back(probeName);
455
456 std::set<nlohmann::json> usedNames;
457 std::list<size_t> indexes(foundDevices.size());
458 std::iota(indexes.begin(), indexes.end(), 1);
459
460 // copy over persisted configurations and make sure we remove
461 // indexes that are already used
462 for (auto itr = foundDevices.begin(); itr != foundDevices.end();)
463 {
464 std::string recordName = getRecordName(itr->interface, probeName);
465
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200466 auto record = _em.systemConfiguration.find(recordName);
467 if (record == _em.systemConfiguration.end())
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930468 {
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200469 record = _em.lastJson.find(recordName);
470 if (record == _em.lastJson.end())
Zhikui Ren1f77d9c2022-08-29 16:02:37 -0700471 {
472 itr++;
473 continue;
474 }
475
476 pruneRecordExposes(*record);
477
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200478 _em.systemConfiguration[recordName] = *record;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930479 }
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930480 _missingConfigurations.erase(recordName);
481
482 // We've processed the device, remove it and advance the
483 // iterator
484 itr = foundDevices.erase(itr);
JinFuLin8f05a3a2022-11-21 15:33:24 +0800485 recordDiscoveredIdentifiers(usedNames, indexes, probeName, *record);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930486 }
487
488 std::optional<std::string> replaceStr;
489
490 DBusObject emptyObject;
491 DBusInterface emptyInterface;
492 emptyObject.emplace(std::string{}, emptyInterface);
493
494 for (const auto& [foundDevice, path] : foundDevices)
495 {
496 // Need all interfaces on this path so that template
497 // substitutions can be done with any of the contained
498 // properties. If the probe that passed didn't use an
499 // interface, such as if it was just TRUE, then
500 // templateCharReplace will just get passed in an empty
501 // map.
Andrew Jefferyaf9b46b2022-04-21 12:34:43 +0930502 auto objectIt = dbusProbeObjects.find(path);
503 const DBusObject& dbusObject = (objectIt == dbusProbeObjects.end())
504 ? emptyObject
505 : objectIt->second;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930506
Ed Tanous77192692025-06-24 11:32:12 -0700507 const nlohmann::json::object_t* recordPtr =
508 recordRef.get_ptr<const nlohmann::json::object_t*>();
509 if (recordPtr == nullptr)
510 {
511 lg2::error("Failed to parse record {JSON}", "JSON",
512 recordRef.dump());
513 continue;
514 }
515 nlohmann::json::object_t record = *recordPtr;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930516 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 {
Ed Tanous77192692025-06-24 11:32:12 -0700524 lg2::error("Record Missing Name! {JSON}", "JSON", recordRef.dump());
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930525 continue; // this should be impossible at this level
526 }
527
Ed Tanous77192692025-06-24 11:32:12 -0700528 const std::string* name = getName->second.get_ptr<const std::string*>();
529 if (name == nullptr)
530 {
531 lg2::error("Name wasn't a string: {JSON}", "JSON",
532 recordRef.dump());
533 continue;
534 }
535
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930536 std::string deviceName = generateDeviceName(
Ed Tanous77192692025-06-24 11:32:12 -0700537 usedNames, dbusObject, foundDeviceIdx, *name, replaceStr);
538
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930539 usedNames.insert(deviceName);
540
Ed Tanous77192692025-06-24 11:32:12 -0700541 for (auto& keyPair : record)
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930542 {
Ed Tanous77192692025-06-24 11:32:12 -0700543 if (keyPair.first != "Name")
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930544 {
Chau Ly8ee43692025-10-09 07:22:08 +0000545 // "Probe" string does not contain template variables
546 // Handle left-over variables for "Exposes" later below
547 const bool handleLeftOver =
Ed Tanous77192692025-06-24 11:32:12 -0700548 (keyPair.first != "Probe") && (keyPair.first != "Exposes");
549 em_utils::templateCharReplace(keyPair.second, dbusObject,
Chau Ly8ee43692025-10-09 07:22:08 +0000550 foundDeviceIdx, replaceStr,
551 handleLeftOver);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930552 }
553 }
554
555 // insert into configuration temporarily to be able to
556 // reference ourselves
557
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200558 _em.systemConfiguration[recordName] = record;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930559
560 auto findExpose = record.find("Exposes");
561 if (findExpose == record.end())
562 {
563 continue;
564 }
565
Ed Tanous77192692025-06-24 11:32:12 -0700566 nlohmann::json::array_t* exposeArr =
567 findExpose->second.get_ptr<nlohmann::json::array_t*>();
568 if (exposeArr != nullptr)
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930569 {
Ed Tanous77192692025-06-24 11:32:12 -0700570 for (auto& value : *exposeArr)
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930571 {
Ed Tanous77192692025-06-24 11:32:12 -0700572 applyTemplateAndExposeActions(recordName, dbusObject,
573 foundDeviceIdx, replaceStr, value,
574 _em.systemConfiguration);
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930575 }
576 }
Ed Tanous77192692025-06-24 11:32:12 -0700577 else
578 {
579 applyTemplateAndExposeActions(
580 recordName, dbusObject, foundDeviceIdx, replaceStr,
581 findExpose->second, _em.systemConfiguration);
582 }
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930583
Christopher Meis9a5eec92025-08-07 08:43:03 +0200584 // If we end up here and the path is empty, we have Probe: "True"
585 // and we dont want that to show up in the associations.
586 if (!path.empty())
587 {
Ed Tanous77192692025-06-24 11:32:12 -0700588 auto boardType = record.find("Type")->second.get<std::string>();
589 auto boardName = record.find("Name")->second.get<std::string>();
Christopher Meis9a5eec92025-08-07 08:43:03 +0200590 std::string boardInventoryPath =
591 em_utils::buildInventorySystemPath(boardName, boardType);
592 _em.topology.addProbePath(boardInventoryPath, path);
593 }
594
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930595 // overwrite ourselves with cleaned up version
Christopher Meiscf6a75b2025-06-03 07:53:50 +0200596 _em.systemConfiguration[recordName] = record;
Andrew Jefferyae6c1a42022-04-19 15:44:42 +0930597 _missingConfigurations.erase(recordName);
598 }
599}
600
Christopher Meis26fbbd52025-03-26 14:55:06 +0100601void scan::PerformScan::run()
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030602{
Ed Tanousdbf95b22025-10-13 11:38:35 -0700603 std::flat_set<std::string, std::less<>> dbusProbeInterfaces;
Christopher Meis26fbbd52025-03-26 14:55:06 +0100604 std::vector<std::shared_ptr<probe::PerformProbe>> dbusProbePointers;
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030605
606 for (auto it = _configurations.begin(); it != _configurations.end();)
607 {
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030608 // check for poorly formatted fields, probe must be an array
Andrew Jeffery38834872022-04-19 15:15:57 +0930609 auto findProbe = it->find("Probe");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030610 if (findProbe == it->end())
611 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200612 lg2::error("configuration file missing probe:\n {JSON}", "JSON",
613 *it);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030614 it = _configurations.erase(it);
615 continue;
616 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030617
Andrew Jeffery38834872022-04-19 15:15:57 +0930618 auto findName = it->find("Name");
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030619 if (findName == it->end())
620 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200621 lg2::error("configuration file missing name:\n {JSON}", "JSON",
622 *it);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030623 it = _configurations.erase(it);
624 continue;
625 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030626
Ed Tanous77192692025-06-24 11:32:12 -0700627 const std::string* probeName = findName->get_ptr<const std::string*>();
628 if (probeName == nullptr)
629 {
630 lg2::error("Name wasn't a string? {JSON}", "JSON", *it);
631 it = _configurations.erase(it);
632 continue;
633 }
634
635 if (std::find(passedProbes.begin(), passedProbes.end(), *probeName) !=
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030636 passedProbes.end())
637 {
638 it = _configurations.erase(it);
639 continue;
640 }
Andrew Jeffery38834872022-04-19 15:15:57 +0930641
Andrew Jeffery98f3d532022-04-19 15:29:22 +0930642 nlohmann::json& recordRef = *it;
Ed Tanous77192692025-06-24 11:32:12 -0700643 std::vector<std::string> probeCommand;
644 nlohmann::json::array_t* probeCommandArrayPtr =
645 findProbe->get_ptr<nlohmann::json::array_t*>();
646 if (probeCommandArrayPtr != nullptr)
Andrew Jeffery38834872022-04-19 15:15:57 +0930647 {
Ed Tanous77192692025-06-24 11:32:12 -0700648 for (const auto& probe : *probeCommandArrayPtr)
649 {
650 const std::string* probeStr =
651 probe.get_ptr<const std::string*>();
652 if (probeStr == nullptr)
653 {
654 lg2::error("Probe statement wasn't a string, can't parse");
655 return;
656 }
657 probeCommand.push_back(*probeStr);
658 }
Andrew Jeffery38834872022-04-19 15:15:57 +0930659 }
660 else
661 {
Ed Tanous77192692025-06-24 11:32:12 -0700662 const std::string* probeStr =
663 findProbe->get_ptr<const std::string*>();
664 if (probeStr == nullptr)
665 {
666 lg2::error("Probe statement wasn't a string, can't parse");
667 return;
668 }
669 probeCommand.push_back(*probeStr);
Andrew Jeffery38834872022-04-19 15:15:57 +0930670 }
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030671
672 // store reference to this to children to makes sure we don't get
673 // destroyed too early
674 auto thisRef = shared_from_this();
Christopher Meis26fbbd52025-03-26 14:55:06 +0100675 auto probePointer = std::make_shared<probe::PerformProbe>(
Ed Tanous77192692025-06-24 11:32:12 -0700676 recordRef, probeCommand, *probeName, thisRef);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030677
678 // parse out dbus probes by discarding other probe types, store in a
679 // map
Ed Tanous77192692025-06-24 11:32:12 -0700680 for (const std::string& probe : probeCommand)
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030681 {
Ed Tanous77192692025-06-24 11:32:12 -0700682 if (probe::findProbeType(probe))
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030683 {
684 continue;
685 }
686 // syntax requires probe before first open brace
Ed Tanous77192692025-06-24 11:32:12 -0700687 auto findStart = probe.find('(');
688 std::string interface = probe.substr(0, findStart);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030689 dbusProbeInterfaces.emplace(interface);
690 dbusProbePointers.emplace_back(probePointer);
691 }
692 it++;
693 }
694
695 // probe vector stores a shared_ptr to each PerformProbe that cares
696 // about a dbus interface
697 findDbusObjects(std::move(dbusProbePointers),
Alexander Hansena555acf2025-06-27 11:59:10 +0200698 std::move(dbusProbeInterfaces), shared_from_this(), io);
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030699}
700
Christopher Meis26fbbd52025-03-26 14:55:06 +0100701scan::PerformScan::~PerformScan()
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030702{
703 if (_passed)
704 {
705 auto nextScan = std::make_shared<PerformScan>(
Alexander Hansena555acf2025-06-27 11:59:10 +0200706 _em, _missingConfigurations, _configurations, io,
707 std::move(_callback));
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030708 nextScan->passedProbes = std::move(passedProbes);
709 nextScan->dbusProbeObjects = std::move(dbusProbeObjects);
Ed Tanous77192692025-06-24 11:32:12 -0700710 boost::asio::post(_em.io, [nextScan]() { nextScan->run(); });
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030711 }
712 else
713 {
714 _callback();
Andrew Jeffery47af65a2021-12-01 14:16:31 +1030715 }
716}