entity-manager: create class PowerStatusMonitor
Removing following global variables
```
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static bool powerStatusOn = false;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static std::unique_ptr<sdbusplus::bus::match_t> powerMatch = nullptr;
```
and placing them in a new class PowerStatusMonitor.
class PowerStatusMonitor is a member of class EntityManager and
inherits its lifetime. So there should be no issue with the lifetime
compared to the global variable.
Tested: Inspection only.
Change-Id: Ibdd9eb3adb6655d88c90c2da8435912887ff547c
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/src/entity_manager/entity_manager.cpp b/src/entity_manager/entity_manager.cpp
index ff9a5d5..d55105c 100644
--- a/src/entity_manager/entity_manager.cpp
+++ b/src/entity_manager/entity_manager.cpp
@@ -385,7 +385,7 @@
return;
}
- if (!em_utils::isPowerOn() && scannedPowerOff)
+ if (!powerStatus.isPowerOn() && scannedPowerOff)
{
return;
}
@@ -398,7 +398,7 @@
return;
}
- bool powerOff = !em_utils::isPowerOn();
+ bool powerOff = !powerStatus.isPowerOn();
for (const auto& [name, device] : lastJson.items())
{
pruneDevice(systemConfiguration, powerOff, scannedPowerOff,
@@ -505,7 +505,7 @@
[this, count, oldConfiguration, missingConfigurations]() {
// this is something that since ac has been applied to the
// bmc we saw, and we no longer see it
- bool powerOff = !em_utils::isPowerOn();
+ bool powerOff = !powerStatus.isPowerOn();
for (const auto& [name, device] :
missingConfigurations->items())
{
@@ -693,7 +693,7 @@
// some boards only show up after power is on, we want to not say they are
// removed until the same state happens
- em_utils::setupPowerMatch(em.systemBus);
+ em.powerStatus.setupPowerMatch(em.systemBus);
io.run();
diff --git a/src/entity_manager/entity_manager.hpp b/src/entity_manager/entity_manager.hpp
index c244ad3..8aa8935 100644
--- a/src/entity_manager/entity_manager.hpp
+++ b/src/entity_manager/entity_manager.hpp
@@ -20,6 +20,7 @@
#include "../utils.hpp"
#include "configuration.hpp"
#include "dbus_interface.hpp"
+#include "power_status_monitor.hpp"
#include "topology.hpp"
#include <systemd/sd-journal.h>
@@ -59,6 +60,8 @@
dbus_interface::EMDBusInterface dbus_interface;
+ power::PowerStatusMonitor powerStatus;
+
void propertiesChangedCallback();
void registerCallback(const std::string& path);
void publishNewConfiguration(const size_t& instance, size_t count,
diff --git a/src/entity_manager/power_status_monitor.hpp b/src/entity_manager/power_status_monitor.hpp
new file mode 100644
index 0000000..29840e2
--- /dev/null
+++ b/src/entity_manager/power_status_monitor.hpp
@@ -0,0 +1,26 @@
+#pragma once
+
+#include <sdbusplus/asio/connection.hpp>
+#include <sdbusplus/bus/match.hpp>
+
+namespace power
+{
+
+const static constexpr char* busname = "xyz.openbmc_project.State.Host";
+const static constexpr char* interface = "xyz.openbmc_project.State.Host";
+const static constexpr char* path = "/xyz/openbmc_project/state/host0";
+const static constexpr char* property = "CurrentHostState";
+
+class PowerStatusMonitor
+{
+ public:
+ bool isPowerOn();
+ void setupPowerMatch(
+ const std::shared_ptr<sdbusplus::asio::connection>& conn);
+
+ private:
+ bool powerStatusOn = false;
+ std::unique_ptr<sdbusplus::bus::match_t> powerMatch = nullptr;
+};
+
+} // namespace power
diff --git a/src/entity_manager/utils.cpp b/src/entity_manager/utils.cpp
index b169bce..92dba87 100644
--- a/src/entity_manager/utils.cpp
+++ b/src/entity_manager/utils.cpp
@@ -2,6 +2,7 @@
#include "../variant_visitors.hpp"
#include "expression.hpp"
+#include "power_status_monitor.hpp"
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/find.hpp>
@@ -13,17 +14,7 @@
#include <fstream>
#include <iostream>
-namespace em_utils
-{
-
-// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
-static bool powerStatusOn = false;
-// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
-static std::unique_ptr<sdbusplus::bus::match_t> powerMatch = nullptr;
-
-constexpr const char* templateChar = "$";
-
-bool isPowerOn()
+bool power::PowerStatusMonitor::isPowerOn()
{
if (!powerMatch)
{
@@ -32,14 +23,16 @@
return powerStatusOn;
}
-void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn)
+void power::PowerStatusMonitor::setupPowerMatch(
+ const std::shared_ptr<sdbusplus::asio::connection>& conn)
{
powerMatch = std::make_unique<sdbusplus::bus::match_t>(
static_cast<sdbusplus::bus_t&>(*conn),
- "type='signal',interface='" + std::string(properties::interface) +
- "',path='" + std::string(power::path) + "',arg0='" +
+ "type='signal',interface='" +
+ std::string(em_utils::properties::interface) + "',path='" +
+ std::string(power::path) + "',arg0='" +
std::string(power::interface) + "'",
- [](sdbusplus::message_t& message) {
+ [this](sdbusplus::message_t& message) {
std::string objectName;
boost::container::flat_map<std::string, std::variant<std::string>>
values;
@@ -53,8 +46,8 @@
});
conn->async_method_call(
- [](boost::system::error_code ec,
- const std::variant<std::string>& state) {
+ [this](boost::system::error_code ec,
+ const std::variant<std::string>& state) {
if (ec)
{
return;
@@ -62,10 +55,15 @@
powerStatusOn =
boost::ends_with(std::get<std::string>(state), "Running");
},
- power::busname, power::path, properties::interface, properties::get,
- power::interface, power::property);
+ power::busname, power::path, em_utils::properties::interface,
+ em_utils::properties::get, power::interface, power::property);
}
+namespace em_utils
+{
+
+constexpr const char* templateChar = "$";
+
bool fwVersionIsSame()
{
std::ifstream version(versionFile);
diff --git a/src/entity_manager/utils.hpp b/src/entity_manager/utils.hpp
index fb25b24..3bf20f2 100644
--- a/src/entity_manager/utils.hpp
+++ b/src/entity_manager/utils.hpp
@@ -24,16 +24,6 @@
constexpr const char* get = "Get";
} // namespace properties
-namespace power
-{
-const static constexpr char* busname = "xyz.openbmc_project.State.Host";
-const static constexpr char* interface = "xyz.openbmc_project.State.Host";
-const static constexpr char* path = "/xyz/openbmc_project/state/host0";
-const static constexpr char* property = "CurrentHostState";
-} // namespace power
-
-bool isPowerOn();
-void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
bool fwVersionIsSame();
std::optional<std::string> templateCharReplace(