cleanup: Move probe and scan into their own files
Moving the code into separate files and grouping them by namespace
improves 'separation of concern'. Increases maintainability.
Tested:
Changes have been tested in QEMU/Yosemite35 machine with Yosemite4
Image and adapted configfile, where Probe statement set to true.
Service runs and places the information in the inventory
```
root@yosemite4:~# busctl tree xyz.openbmc_project.EntityManager
`- /xyz
`- /xyz/openbmc_project
|- /xyz/openbmc_project/EntityManager
`- /xyz/openbmc_project/inventory
`- /xyz/openbmc_project/inventory/system
`- /xyz/openbmc_project/inventory/system/board
`- /xyz/openbmc_project/inventory/system/board/Yosemite_4_Management_Board
|- /xyz/openbmc_project/inventory/system/board/Yosemite_4_Management_Board/All_Fan
|- /xyz/openbmc_project/inventory/system/board/Yosemite_4_Management_Board/MGNT_ADC_P0V6_VOLT_V
|- /xyz/openbmc_project/inventory/system/board/Yosemite_4_Management_Board/MGNT_ADC_P12V_VOLT_V
|- /xyz/openbmc_project/inventory/system/board/Yosemite_4_Management_Board/MGNT_ADC_P1V0_VOLT_V
|- /xyz/openbmc_project/inventory/system/board/Yosemite_4_Management_Board/MGNT_ADC_P1V2_VOLT_V
|- /xyz/openbmc_project/inventory/system/board/Yosemite_4_Management_Board/MGNT_ADC_P1V8_VOLT_V
|- /xyz/openbmc_project/inventory/system/board/Yosemite_4_Management_Board/MGNT_ADC_P2V5_VOLT_V
|- /xyz/openbmc_project/inventory/system/board/Yosemite_4_Management_Board/MGNT_ADC_P3V3_RGM_VOLT_V
|- /xyz/openbmc_project/inventory/system/board/Yosemite_4_Management_Board/MGNT_ADC_P3V3_VOLT_V
|- /xyz/openbmc_project/inventory/system/board/Yosemite_4_Management_Board/MGNT_ADC_P3V_BAT_VOLT_V
|- /xyz/openbmc_project/inventory/system/board/Yosemite_4_Management_Board/MGNT_ADC_P5V_USB_VOLT_V
|- /xyz/openbmc_project/inventory/system/board/Yosemite_4_Management_Board/MGNT_ADC_P5V_VOLT_V
|- /xyz/openbmc_project/inventory/system/board/Yosemite_4_Management_Board/MGNT_TEMP_C
|- /xyz/openbmc_project/inventory/system/board/Yosemite_4_Management_Board/PID_NIC_TEMP
|- /xyz/openbmc_project/inventory/system/board/Yosemite_4_Management_Board/Stepwise_MGNT_TEMP
|- /xyz/openbmc_project/inventory/system/board/Yosemite_4_Management_Board/Stepwise_NIC_TEMP
|- /xyz/openbmc_project/inventory/system/board/Yosemite_4_Management_Board/Stepwise_SENTINEL_DOME_SLOT_PRESENT_PERCENTAGE
`- /xyz/openbmc_project/inventory/system/board/Yosemite_4_Management_Board/Zone_1
```
Change-Id: I0ae0cb14b054ac1a964c5a8e1164811384a4f31b
Signed-off-by: Christopher Meis <christopher.meis@9elements.com>
diff --git a/src/entity_manager.cpp b/src/entity_manager.cpp
index 84d131b..187373a 100644
--- a/src/entity_manager.cpp
+++ b/src/entity_manager.cpp
@@ -18,6 +18,8 @@
#include "entity_manager.hpp"
#include "overlay.hpp"
+#include "perform_probe.hpp"
+#include "perform_scan.hpp"
#include "topology.hpp"
#include "utils.hpp"
#include "variant_visitors.hpp"
@@ -53,14 +55,6 @@
constexpr const char* currentConfiguration = "/var/configuration/system.json";
constexpr const char* globalSchema = "global.json";
-const boost::container::flat_map<const char*, probe_type_codes, CmpStr>
- probeTypes{{{"FALSE", probe_type_codes::FALSE_T},
- {"TRUE", probe_type_codes::TRUE_T},
- {"AND", probe_type_codes::AND},
- {"OR", probe_type_codes::OR},
- {"FOUND", probe_type_codes::FOUND},
- {"MATCH_ONE", probe_type_codes::MATCH_ONE}}};
-
static constexpr std::array<const char*, 6> settableInterfaces = {
"FanProfile", "Pid", "Pid.Zone", "Stepwise", "Thresholds", "Polling"};
using JsonVariantType =
@@ -100,22 +94,6 @@
}
}
-FoundProbeTypeT findProbeType(const std::string& probe)
-{
- boost::container::flat_map<const char*, probe_type_codes,
- CmpStr>::const_iterator probeType;
- for (probeType = probeTypes.begin(); probeType != probeTypes.end();
- ++probeType)
- {
- if (probe.find(probeType->first) != std::string::npos)
- {
- return probeType;
- }
- }
-
- return std::nullopt;
-}
-
static std::shared_ptr<sdbusplus::asio::dbus_interface> createInterface(
sdbusplus::asio::object_server& objServer, const std::string& path,
const std::string& interface, const std::string& parent,
@@ -1101,7 +1079,7 @@
return;
}
- auto perfScan = std::make_shared<PerformScan>(
+ auto perfScan = std::make_shared<scan::PerformScan>(
systemConfiguration, *missingConfigurations, configurations,
objServer,
[&systemConfiguration, &objServer, count, oldConfiguration,
@@ -1177,7 +1155,7 @@
continue;
}
// Skip it if the probe cmd doesn't contain an interface.
- if (findProbeType(*probe))
+ if (probe::findProbeType(*probe))
{
continue;
}
diff --git a/src/entity_manager.hpp b/src/entity_manager.hpp
index db7804e..4d8d69d 100644
--- a/src/entity_manager.hpp
+++ b/src/entity_manager.hpp
@@ -25,78 +25,8 @@
#include <nlohmann/json.hpp>
#include <sdbusplus/asio/object_server.hpp>
-#include <iostream>
-#include <list>
-#include <optional>
#include <string>
-struct DBusDeviceDescriptor
-{
- DBusInterface interface;
- std::string path;
-};
-
-using FoundDevices = std::vector<DBusDeviceDescriptor>;
-
-struct CmpStr
-{
- bool operator()(const char* a, const char* b) const
- {
- return std::strcmp(a, b) < 0;
- }
-};
-
-// underscore T for collison with dbus c api
-enum class probe_type_codes
-{
- FALSE_T,
- TRUE_T,
- AND,
- OR,
- FOUND,
- MATCH_ONE
-};
-
-using FoundProbeTypeT = std::optional<boost::container::flat_map<
- const char*, probe_type_codes, CmpStr>::const_iterator>;
-FoundProbeTypeT findProbeType(const std::string& probe);
-
-struct PerformScan : std::enable_shared_from_this<PerformScan>
-{
- PerformScan(nlohmann::json& systemConfiguration,
- nlohmann::json& missingConfigurations,
- 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);
- void run();
- virtual ~PerformScan();
- nlohmann::json& _systemConfiguration;
- nlohmann::json& _missingConfigurations;
- std::list<nlohmann::json> _configurations;
- sdbusplus::asio::object_server& objServer;
- std::function<void()> _callback;
- bool _passed = false;
- MapperGetSubTreeResponse dbusProbeObjects;
- std::vector<std::string> passedProbes;
-};
-
-// this class finds the needed dbus fields and on destruction runs the probe
-struct PerformProbe : std::enable_shared_from_this<PerformProbe>
-{
- PerformProbe(nlohmann::json& recordRef,
- const std::vector<std::string>& probeCommand,
- std::string probeName, std::shared_ptr<PerformScan>& scanPtr);
- virtual ~PerformProbe();
-
- nlohmann::json& recordRef;
- std::vector<std::string> _probeCommand;
- std::string probeName;
- std::shared_ptr<PerformScan> scan;
-};
-
inline void logDeviceAdded(const nlohmann::json& record)
{
if (!deviceHasLogging(record))
diff --git a/src/perform_probe.cpp b/src/perform_probe.cpp
index c88ed5a..d7dcd8a 100644
--- a/src/perform_probe.cpp
+++ b/src/perform_probe.cpp
@@ -14,7 +14,10 @@
// limitations under the License.
*/
/// \file perform_probe.cpp
+#include "perform_probe.hpp"
+
#include "entity_manager.hpp"
+#include "perform_scan.hpp"
#include <boost/algorithm/string/replace.hpp>
#include <phosphor-logging/lg2.hpp>
@@ -26,8 +29,8 @@
// When an interface passes a probe, also save its D-Bus path with it.
bool probeDbus(const std::string& interfaceName,
const std::map<std::string, nlohmann::json>& matches,
- FoundDevices& devices, const std::shared_ptr<PerformScan>& scan,
- bool& foundProbe)
+ scan::FoundDevices& devices,
+ const std::shared_ptr<scan::PerformScan>& scan, bool& foundProbe)
{
bool foundMatch = false;
foundProbe = false;
@@ -73,35 +76,36 @@
// default probe entry point, iterates a list looking for specific types to
// call specific probe functions
-bool probe(const std::vector<std::string>& probeCommand,
- const std::shared_ptr<PerformScan>& scan, FoundDevices& foundDevs)
+bool doProbe(const std::vector<std::string>& probeCommand,
+ const std::shared_ptr<scan::PerformScan>& scan,
+ scan::FoundDevices& foundDevs)
{
const static std::regex command(R"(\((.*)\))");
std::smatch match;
bool ret = false;
bool matchOne = false;
bool cur = true;
- probe_type_codes lastCommand = probe_type_codes::FALSE_T;
+ probe::probe_type_codes lastCommand = probe::probe_type_codes::FALSE_T;
bool first = true;
for (const auto& probe : probeCommand)
{
- FoundProbeTypeT probeType = findProbeType(probe);
+ probe::FoundProbeTypeT probeType = probe::findProbeType(probe);
if (probeType)
{
switch ((*probeType)->second)
{
- case probe_type_codes::FALSE_T:
+ case probe::probe_type_codes::FALSE_T:
{
cur = false;
break;
}
- case probe_type_codes::TRUE_T:
+ case probe::probe_type_codes::TRUE_T:
{
cur = true;
break;
}
- case probe_type_codes::MATCH_ONE:
+ case probe::probe_type_codes::MATCH_ONE:
{
// set current value to last, this probe type shouldn't
// affect the outcome
@@ -109,13 +113,13 @@
matchOne = true;
break;
}
- /*case probe_type_codes::AND:
+ /*case probe::probe_type_codes::AND:
break;
- case probe_type_codes::OR:
+ case probe::probe_type_codes::OR:
break;
// these are no-ops until the last command switch
*/
- case probe_type_codes::FOUND:
+ case probe::probe_type_codes::FOUND:
{
if (!std::regex_search(probe, match, command))
{
@@ -171,11 +175,11 @@
// some functions like AND and OR only take affect after the
// fact
- if (lastCommand == probe_type_codes::AND)
+ if (lastCommand == probe::probe_type_codes::AND)
{
ret = cur && ret;
}
- else if (lastCommand == probe_type_codes::OR)
+ else if (lastCommand == probe::probe_type_codes::OR)
{
ret = cur || ret;
}
@@ -186,7 +190,7 @@
first = false;
}
lastCommand = probeType ? (*probeType)->second
- : probe_type_codes::FALSE_T;
+ : probe::probe_type_codes::FALSE_T;
}
// probe passed, but empty device
@@ -207,18 +211,48 @@
return ret;
}
+namespace probe
+{
+
PerformProbe::PerformProbe(nlohmann::json& recordRef,
const std::vector<std::string>& probeCommand,
std::string probeName,
- std::shared_ptr<PerformScan>& scanPtr) :
+ std::shared_ptr<scan::PerformScan>& scanPtr) :
recordRef(recordRef), _probeCommand(probeCommand),
probeName(std::move(probeName)), scan(scanPtr)
{}
+
PerformProbe::~PerformProbe()
{
- FoundDevices foundDevs;
- if (probe(_probeCommand, scan, foundDevs))
+ scan::FoundDevices foundDevs;
+ if (doProbe(_probeCommand, scan, foundDevs))
{
scan->updateSystemConfiguration(recordRef, probeName, foundDevs);
}
}
+
+FoundProbeTypeT findProbeType(const std::string& probe)
+{
+ const boost::container::flat_map<const char*, probe_type_codes, CmpStr>
+ probeTypes{{{"FALSE", probe_type_codes::FALSE_T},
+ {"TRUE", probe_type_codes::TRUE_T},
+ {"AND", probe_type_codes::AND},
+ {"OR", probe_type_codes::OR},
+ {"FOUND", probe_type_codes::FOUND},
+ {"MATCH_ONE", probe_type_codes::MATCH_ONE}}};
+
+ boost::container::flat_map<const char*, probe_type_codes,
+ CmpStr>::const_iterator probeType;
+ for (probeType = probeTypes.begin(); probeType != probeTypes.end();
+ ++probeType)
+ {
+ if (probe.find(probeType->first) != std::string::npos)
+ {
+ return probeType;
+ }
+ }
+
+ return std::nullopt;
+}
+
+} // namespace probe
diff --git a/src/perform_probe.hpp b/src/perform_probe.hpp
new file mode 100644
index 0000000..f979c52
--- /dev/null
+++ b/src/perform_probe.hpp
@@ -0,0 +1,52 @@
+#pragma once
+
+#include "perform_scan.hpp"
+
+#include <boost/container/flat_map.hpp>
+
+#include <memory>
+#include <string>
+#include <vector>
+
+namespace probe
+{
+struct CmpStr
+{
+ bool operator()(const char* a, const char* b) const
+ {
+ return std::strcmp(a, b) < 0;
+ }
+};
+
+// underscore T for collison with dbus c api
+enum class probe_type_codes
+{
+ FALSE_T,
+ TRUE_T,
+ AND,
+ OR,
+ FOUND,
+ MATCH_ONE
+};
+
+using FoundProbeTypeT = std::optional<boost::container::flat_map<
+ const char*, probe_type_codes, CmpStr>::const_iterator>;
+
+FoundProbeTypeT findProbeType(const std::string& probe);
+
+// this class finds the needed dbus fields and on destruction runs the probe
+struct PerformProbe : std::enable_shared_from_this<PerformProbe>
+{
+ PerformProbe(nlohmann::json& recordRef,
+ const std::vector<std::string>& probeCommand,
+ std::string probeName,
+ std::shared_ptr<scan::PerformScan>& scanPtr);
+ virtual ~PerformProbe();
+
+ nlohmann::json& recordRef;
+ std::vector<std::string> _probeCommand;
+ std::string probeName;
+ std::shared_ptr<scan::PerformScan> scan;
+};
+
+} // namespace probe
diff --git a/src/perform_scan.cpp b/src/perform_scan.cpp
index 73df57d..d05312d 100644
--- a/src/perform_scan.cpp
+++ b/src/perform_scan.cpp
@@ -14,7 +14,10 @@
// limitations under the License.
*/
/// \file perform_scan.cpp
+#include "perform_scan.hpp"
+
#include "entity_manager.hpp"
+#include "perform_probe.hpp"
#include <boost/algorithm/string/predicate.hpp>
#include <boost/asio/steady_timer.hpp>
@@ -48,8 +51,8 @@
void getInterfaces(
const DBusInterfaceInstance& instance,
- const std::vector<std::shared_ptr<PerformProbe>>& probeVector,
- const std::shared_ptr<PerformScan>& scan, size_t retries = 5)
+ const std::vector<std::shared_ptr<probe::PerformProbe>>& probeVector,
+ const std::shared_ptr<scan::PerformScan>& scan, size_t retries = 5)
{
if (retries == 0U)
{
@@ -109,8 +112,8 @@
}
static void processDbusObjects(
- std::vector<std::shared_ptr<PerformProbe>>& probeVector,
- const std::shared_ptr<PerformScan>& scan,
+ std::vector<std::shared_ptr<probe::PerformProbe>>& probeVector,
+ const std::shared_ptr<scan::PerformScan>& scan,
const GetSubTreeType& interfaceSubtree)
{
for (const auto& [path, object] : interfaceSubtree)
@@ -137,10 +140,10 @@
// Populates scan->dbusProbeObjects with all interfaces and properties
// for the paths that own the interfaces passed in.
-void findDbusObjects(std::vector<std::shared_ptr<PerformProbe>>&& probeVector,
- boost::container::flat_set<std::string>&& interfaces,
- const std::shared_ptr<PerformScan>& scan,
- size_t retries = 5)
+void findDbusObjects(
+ std::vector<std::shared_ptr<probe::PerformProbe>>&& probeVector,
+ boost::container::flat_set<std::string>&& interfaces,
+ const std::shared_ptr<scan::PerformScan>& scan, size_t retries = 5)
{
// Filter out interfaces already obtained.
for (const auto& [path, probeInterfaces] : scan->dbusProbeObjects)
@@ -221,11 +224,11 @@
return std::to_string(std::hash<std::string>{}(probeName + device.dump()));
}
-PerformScan::PerformScan(nlohmann::json& systemConfiguration,
- nlohmann::json& missingConfigurations,
- std::list<nlohmann::json>& configurations,
- sdbusplus::asio::object_server& objServerIn,
- std::function<void()>&& callback) :
+scan::PerformScan::PerformScan(nlohmann::json& systemConfiguration,
+ nlohmann::json& missingConfigurations,
+ std::list<nlohmann::json>& configurations,
+ sdbusplus::asio::object_server& objServerIn,
+ std::function<void()>&& callback) :
_systemConfiguration(systemConfiguration),
_missingConfigurations(missingConfigurations),
_configurations(configurations), objServer(objServerIn),
@@ -449,9 +452,9 @@
return copyIt.value();
}
-void PerformScan::updateSystemConfiguration(const nlohmann::json& recordRef,
- const std::string& probeName,
- FoundDevices& foundDevices)
+void scan::PerformScan::updateSystemConfiguration(
+ const nlohmann::json& recordRef, const std::string& probeName,
+ FoundDevices& foundDevices)
{
_passed = true;
passedProbes.push_back(probeName);
@@ -564,10 +567,10 @@
}
}
-void PerformScan::run()
+void scan::PerformScan::run()
{
boost::container::flat_set<std::string> dbusProbeInterfaces;
- std::vector<std::shared_ptr<PerformProbe>> dbusProbePointers;
+ std::vector<std::shared_ptr<probe::PerformProbe>> dbusProbePointers;
for (auto it = _configurations.begin(); it != _configurations.end();)
{
@@ -611,7 +614,7 @@
// store reference to this to children to makes sure we don't get
// destroyed too early
auto thisRef = shared_from_this();
- auto probePointer = std::make_shared<PerformProbe>(
+ auto probePointer = std::make_shared<probe::PerformProbe>(
recordRef, probeCommand, probeName, thisRef);
// parse out dbus probes by discarding other probe types, store in a
@@ -624,7 +627,7 @@
std::cerr << "Probe statement wasn't a string, can't parse";
continue;
}
- if (findProbeType(*probe))
+ if (probe::findProbeType(*probe))
{
continue;
}
@@ -643,7 +646,7 @@
std::move(dbusProbeInterfaces), shared_from_this());
}
-PerformScan::~PerformScan()
+scan::PerformScan::~PerformScan()
{
if (_passed)
{
diff --git a/src/perform_scan.hpp b/src/perform_scan.hpp
new file mode 100644
index 0000000..300ffb1
--- /dev/null
+++ b/src/perform_scan.hpp
@@ -0,0 +1,48 @@
+#pragma once
+
+#include "utils.hpp"
+
+#include <systemd/sd-journal.h>
+
+#include <boost/container/flat_map.hpp>
+#include <nlohmann/json.hpp>
+#include <sdbusplus/asio/object_server.hpp>
+
+#include <functional>
+#include <list>
+#include <vector>
+
+namespace scan
+{
+struct DBusDeviceDescriptor
+{
+ DBusInterface interface;
+ std::string path;
+};
+
+using FoundDevices = std::vector<DBusDeviceDescriptor>;
+
+struct PerformScan : std::enable_shared_from_this<PerformScan>
+{
+ PerformScan(nlohmann::json& systemConfiguration,
+ nlohmann::json& missingConfigurations,
+ 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);
+ void run();
+ virtual ~PerformScan();
+ nlohmann::json& _systemConfiguration;
+ nlohmann::json& _missingConfigurations;
+ std::list<nlohmann::json> _configurations;
+ sdbusplus::asio::object_server& objServer;
+ std::function<void()> _callback;
+ bool _passed = false;
+ MapperGetSubTreeResponse dbusProbeObjects;
+ std::vector<std::string> passedProbes;
+};
+
+} // namespace scan