PerformScan: Add updateSystemConfiguration() as a member function
Pull the guts out of the PerformProbe callback lambda and add it as a
member function on PerformScan.
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I30b815f92b303d4cd81b0b6a417c1982c11f7b1d
diff --git a/include/EntityManager.hpp b/include/EntityManager.hpp
index 22a8042..781d3a1 100644
--- a/include/EntityManager.hpp
+++ b/include/EntityManager.hpp
@@ -70,6 +70,10 @@
std::list<nlohmann::json>& configurations,
sdbusplus::asio::object_server& objServer,
std::function<void()>&& callback);
+ void updateSystemConfiguration(const nlohmann::json& recordRef,
+ const std::string& probeName,
+ FoundDevices& foundDevices,
+ const MapperGetSubTreeResponse& dbusSubtree);
void run(void);
virtual ~PerformScan();
nlohmann::json& _systemConfiguration;
diff --git a/src/PerformScan.cpp b/src/PerformScan.cpp
index 9ea8e27..47b96f8 100644
--- a/src/PerformScan.cpp
+++ b/src/PerformScan.cpp
@@ -464,6 +464,119 @@
return copyIt.value();
}
+void PerformScan::updateSystemConfiguration(
+ const nlohmann::json& recordRef, const std::string& probeName,
+ FoundDevices& foundDevices, const MapperGetSubTreeResponse& dbusSubtree)
+{
+ _passed = true;
+ passedProbes.push_back(probeName);
+
+ std::set<nlohmann::json> usedNames;
+ std::list<size_t> indexes(foundDevices.size());
+ std::iota(indexes.begin(), indexes.end(), 1);
+
+ // copy over persisted configurations and make sure we remove
+ // indexes that are already used
+ for (auto itr = foundDevices.begin(); itr != foundDevices.end();)
+ {
+ std::string recordName = getRecordName(itr->interface, probeName);
+
+ auto record = lastJson.find(recordName);
+ if (record == lastJson.end())
+ {
+ itr++;
+ continue;
+ }
+
+ pruneRecordExposes(*record);
+
+ recordDiscoveredIdentifiers(usedNames, indexes, probeName, *record);
+
+ // keep user changes
+ _systemConfiguration[recordName] = *record;
+ _missingConfigurations.erase(recordName);
+
+ // We've processed the device, remove it and advance the
+ // iterator
+ itr = foundDevices.erase(itr);
+ }
+
+ std::optional<std::string> replaceStr;
+
+ DBusObject emptyObject;
+ DBusInterface emptyInterface;
+ emptyObject.emplace(std::string{}, emptyInterface);
+
+ for (const auto& [foundDevice, path] : foundDevices)
+ {
+ // Need all interfaces on this path so that template
+ // substitutions can be done with any of the contained
+ // properties. If the probe that passed didn't use an
+ // interface, such as if it was just TRUE, then
+ // templateCharReplace will just get passed in an empty
+ // map.
+ auto objectIt = dbusSubtree.find(path);
+ const DBusObject& dbusObject =
+ (objectIt == dbusSubtree.end()) ? emptyObject : objectIt->second;
+
+ nlohmann::json record = recordRef;
+ std::string recordName = getRecordName(foundDevice, probeName);
+ size_t foundDeviceIdx = indexes.front();
+ indexes.pop_front();
+
+ // check name first so we have no duplicate names
+ auto getName = record.find("Name");
+ if (getName == record.end())
+ {
+ std::cerr << "Record Missing Name! " << record.dump();
+ continue; // this should be impossible at this level
+ }
+
+ std::string deviceName = generateDeviceName(
+ usedNames, dbusObject, foundDeviceIdx, getName.value(), replaceStr);
+ getName.value() = deviceName;
+ usedNames.insert(deviceName);
+
+ for (auto keyPair = record.begin(); keyPair != record.end(); keyPair++)
+ {
+ if (keyPair.key() != "Name")
+ {
+ templateCharReplace(keyPair, dbusObject, foundDeviceIdx,
+ replaceStr);
+ }
+ }
+
+ // insert into configuration temporarily to be able to
+ // reference ourselves
+
+ _systemConfiguration[recordName] = record;
+
+ auto findExpose = record.find("Exposes");
+ if (findExpose == record.end())
+ {
+ continue;
+ }
+
+ for (auto& expose : *findExpose)
+ {
+ for (auto keyPair = expose.begin(); keyPair != expose.end();
+ keyPair++)
+ {
+
+ templateCharReplace(keyPair, dbusObject, foundDeviceIdx,
+ replaceStr);
+
+ applyExposeActions(_systemConfiguration, recordName, expose,
+ keyPair);
+ }
+ }
+
+ // overwrite ourselves with cleaned up version
+ _systemConfiguration[recordName] = record;
+ _missingConfigurations.erase(recordName);
+ }
+}
+
void PerformScan::run()
{
boost::container::flat_set<std::string> dbusProbeInterfaces;
@@ -513,121 +626,11 @@
auto thisRef = shared_from_this();
auto probePointer = std::make_shared<PerformProbe>(
probeCommand, thisRef,
- [&, recordRef,
+ [thisRef, recordRef,
probeName](FoundDevices& foundDevices,
const MapperGetSubTreeResponse& dbusSubtree) {
- _passed = true;
- std::set<nlohmann::json> usedNames;
- passedProbes.push_back(probeName);
- std::list<size_t> indexes(foundDevices.size());
- std::iota(indexes.begin(), indexes.end(), 1);
-
- // copy over persisted configurations and make sure we remove
- // indexes that are already used
- for (auto itr = foundDevices.begin();
- itr != foundDevices.end();)
- {
- std::string recordName =
- getRecordName(itr->interface, probeName);
-
- auto record = lastJson.find(recordName);
- if (record == lastJson.end())
- {
- itr++;
- continue;
- }
-
- pruneRecordExposes(*record);
-
- recordDiscoveredIdentifiers(usedNames, indexes, probeName,
- *record);
-
- // keep user changes
- _systemConfiguration[recordName] = *record;
- _missingConfigurations.erase(recordName);
-
- // We've processed the device, remove it and advance the
- // iterator
- itr = foundDevices.erase(itr);
- }
-
- std::optional<std::string> replaceStr;
-
- DBusObject emptyObject;
- DBusInterface emptyInterface;
- emptyObject.emplace(std::string{}, emptyInterface);
-
- for (const auto& [foundDevice, path] : foundDevices)
- {
- // Need all interfaces on this path so that template
- // substitutions can be done with any of the contained
- // properties. If the probe that passed didn't use an
- // interface, such as if it was just TRUE, then
- // templateCharReplace will just get passed in an empty
- // map.
- auto objectIt = dbusSubtree.find(path);
- const DBusObject& dbusObject =
- (objectIt == dbusSubtree.end()) ? emptyObject
- : objectIt->second;
-
- nlohmann::json record = recordRef;
- std::string recordName =
- getRecordName(foundDevice, probeName);
- size_t foundDeviceIdx = indexes.front();
- indexes.pop_front();
-
- // check name first so we have no duplicate names
- auto getName = record.find("Name");
- if (getName == record.end())
- {
- std::cerr << "Record Missing Name! " << record.dump();
- continue; // this should be impossible at this level
- }
-
- std::string deviceName = generateDeviceName(
- usedNames, dbusObject, foundDeviceIdx, getName.value(),
- replaceStr);
- getName.value() = deviceName;
- usedNames.insert(deviceName);
-
- for (auto keyPair = record.begin(); keyPair != record.end();
- keyPair++)
- {
- if (keyPair.key() != "Name")
- {
- templateCharReplace(keyPair, dbusObject,
- foundDeviceIdx, replaceStr);
- }
- }
-
- // insert into configuration temporarily to be able to
- // reference ourselves
-
- _systemConfiguration[recordName] = record;
-
- auto findExpose = record.find("Exposes");
- if (findExpose == record.end())
- {
- continue;
- }
-
- for (auto& expose : *findExpose)
- {
- for (auto keyPair = expose.begin();
- keyPair != expose.end(); keyPair++)
- {
-
- templateCharReplace(keyPair, dbusObject,
- foundDeviceIdx, replaceStr);
-
- applyExposeActions(_systemConfiguration, recordName,
- expose, keyPair);
- }
- }
- // overwrite ourselves with cleaned up version
- _systemConfiguration[recordName] = record;
- _missingConfigurations.erase(recordName);
- }
+ thisRef->updateSystemConfiguration(recordRef, probeName,
+ foundDevices, dbusSubtree);
});
// parse out dbus probes by discarding other probe types, store in a