entity-manager: create class EMDBusInterface
class EMDBusInterface is created to remove global var 'inventory'.
The one instance of this class is a member of class EntityManager and
has the same lifetime. Since the EntityManager class instance is created
at the start of the main function, the 'inventory' member should always
be in scope from the perspective of the rest of the code, as before.
The class was created with a small amount of changes.
It does not aim to be a good abstraction for entity-manager's dbus
interface, the primary goal is to get rid of the global variable and
avoid passing it through a bunch of functions.
Tested: On Tyan S8030
```
Jul 01 09:59:26 s8030-bmc-30303035c0c1 entity-manager[4204]: Inventory Added: Supermicro PWS 920P SQ 0
Jul 01 09:59:26 s8030-bmc-30303035c0c1 entity-manager[4204]: Inventory Added: Supermicro PWS 920P SQ 1
Jul 01 09:59:26 s8030-bmc-30303035c0c1 entity-manager[4204]: Inventory Added: chassis
Jul 01 09:59:26 s8030-bmc-30303035c0c1 entity-manager[4204]: Inventory Added: MBX 1.57 Chassis
```
busctl tree output as before
Change-Id: I5f682311358836f88fe861d778210cf38efe08fa
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/src/entity_manager/dbus_interface.cpp b/src/entity_manager/dbus_interface.cpp
index ff8094e..1efa0c3 100644
--- a/src/entity_manager/dbus_interface.cpp
+++ b/src/entity_manager/dbus_interface.cpp
@@ -22,13 +22,6 @@
const std::regex illegalDbusPathRegex("[^A-Za-z0-9_.]");
const std::regex illegalDbusMemberRegex("[^A-Za-z0-9_]");
-// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
-// store reference to all interfaces so we can destroy them later
-boost::container::flat_map<
- std::string, std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>>
- inventory;
-// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
-
void tryIfaceInitialize(std::shared_ptr<sdbusplus::asio::dbus_interface>& iface)
{
try
@@ -44,9 +37,10 @@
}
}
-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, bool checkNull)
+std::shared_ptr<sdbusplus::asio::dbus_interface>
+ EMDBusInterface::createInterface(
+ sdbusplus::asio::object_server& objServer, const std::string& path,
+ const std::string& interface, const std::string& parent, bool checkNull)
{
// on first add we have no reason to check for null before add, as there
// won't be any. For dynamically added interfaces, we check for null so that
@@ -244,7 +238,7 @@
tryIfaceInitialize(iface);
}
-void createAddObjectMethod(
+void EMDBusInterface::createAddObjectMethod(
boost::asio::io_context& io, const std::string& jsonPointerPath,
const std::string& path, nlohmann::json& systemConfiguration,
sdbusplus::asio::object_server& objServer, const std::string& board)
@@ -256,9 +250,9 @@
"AddObject",
[&systemConfiguration, &objServer,
jsonPointerPath{std::string(jsonPointerPath)}, path{std::string(path)},
- board,
- &io](const boost::container::flat_map<std::string, JsonVariantType>&
- data) {
+ board, &io,
+ this](const boost::container::flat_map<std::string, JsonVariantType>&
+ data) {
nlohmann::json::json_pointer ptr(jsonPointerPath);
nlohmann::json& base = systemConfiguration[ptr];
auto findExposes = base.find("Exposes");
@@ -374,7 +368,7 @@
}
std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>&
- getDeviceInterfaces(const nlohmann::json& device)
+ EMDBusInterface::getDeviceInterfaces(const nlohmann::json& device)
{
return inventory[device["Name"].get<std::string>()];
}
diff --git a/src/entity_manager/dbus_interface.hpp b/src/entity_manager/dbus_interface.hpp
index 84cfbba..51d967e 100644
--- a/src/entity_manager/dbus_interface.hpp
+++ b/src/entity_manager/dbus_interface.hpp
@@ -2,24 +2,43 @@
#include "configuration.hpp"
+#include <boost/container/flat_map.hpp>
#include <nlohmann/json.hpp>
#include <sdbusplus/asio/connection.hpp>
#include <sdbusplus/asio/object_server.hpp>
#include <iostream>
-#include <set>
#include <vector>
namespace dbus_interface
{
+
+class EMDBusInterface
+{
+ public:
+ 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,
+ bool checkNull = false);
+
+ std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>&
+ getDeviceInterfaces(const nlohmann::json& device);
+
+ void createAddObjectMethod(
+ boost::asio::io_context& io, const std::string& jsonPointerPath,
+ const std::string& path, nlohmann::json& systemConfiguration,
+ sdbusplus::asio::object_server& objServer, const std::string& board);
+
+ private:
+ boost::container::flat_map<
+ std::string,
+ std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>>
+ inventory;
+};
+
void tryIfaceInitialize(
std::shared_ptr<sdbusplus::asio::dbus_interface>& iface);
-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,
- bool checkNull = false);
-
template <typename PropertyType>
void addArrayToDbus(const std::string& name, const nlohmann::json& array,
sdbusplus::asio::dbus_interface* iface,
@@ -133,12 +152,4 @@
sdbusplus::asio::PropertyPermission permission =
sdbusplus::asio::PropertyPermission::readOnly);
-void createAddObjectMethod(
- boost::asio::io_context& io, const std::string& jsonPointerPath,
- const std::string& path, nlohmann::json& systemConfiguration,
- sdbusplus::asio::object_server& objServer, const std::string& board);
-
-std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>&
- getDeviceInterfaces(const nlohmann::json& device);
-
} // namespace dbus_interface
diff --git a/src/entity_manager/entity_manager.cpp b/src/entity_manager/entity_manager.cpp
index 5a6955d..e4cd71a 100644
--- a/src/entity_manager/entity_manager.cpp
+++ b/src/entity_manager/entity_manager.cpp
@@ -124,17 +124,17 @@
boardPath += boardName;
std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface =
- dbus_interface::createInterface(
- objServer, boardPath, "xyz.openbmc_project.Inventory.Item",
- boardName);
+ dbus_interface.createInterface(objServer, boardPath,
+ "xyz.openbmc_project.Inventory.Item",
+ boardName);
std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface =
- dbus_interface::createInterface(
+ dbus_interface.createInterface(
objServer, boardPath,
"xyz.openbmc_project.Inventory.Item." + boardType,
boardNameOrig);
- dbus_interface::createAddObjectMethod(
+ dbus_interface.createAddObjectMethod(
io, jsonPointerPath, boardPath, systemConfiguration, objServer,
boardNameOrig);
@@ -148,8 +148,8 @@
if (propValue.type() == nlohmann::json::value_t::object)
{
std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
- dbus_interface::createInterface(objServer, boardPath,
- propName, boardNameOrig);
+ dbus_interface.createInterface(objServer, boardPath,
+ propName, boardNameOrig);
dbus_interface::populateInterfaceFromJson(
io, systemConfiguration, jsonPointerPath + propName, iface,
@@ -211,7 +211,7 @@
if (itemType == "BMC")
{
std::shared_ptr<sdbusplus::asio::dbus_interface> bmcIface =
- dbus_interface::createInterface(
+ dbus_interface.createInterface(
objServer, ifacePath,
"xyz.openbmc_project.Inventory.Item.Bmc",
boardNameOrig);
@@ -222,7 +222,7 @@
else if (itemType == "System")
{
std::shared_ptr<sdbusplus::asio::dbus_interface> systemIface =
- dbus_interface::createInterface(
+ dbus_interface.createInterface(
objServer, ifacePath,
"xyz.openbmc_project.Inventory.Item.System",
boardNameOrig);
@@ -244,7 +244,7 @@
ifaceName.append(itemType).append(".").append(name);
std::shared_ptr<sdbusplus::asio::dbus_interface>
- objectIface = dbus_interface::createInterface(
+ objectIface = dbus_interface.createInterface(
objServer, ifacePath, ifaceName, boardNameOrig);
dbus_interface::populateInterfaceFromJson(
@@ -288,7 +288,7 @@
ifaceName.append(std::to_string(index));
std::shared_ptr<sdbusplus::asio::dbus_interface>
- objectIface = dbus_interface::createInterface(
+ objectIface = dbus_interface.createInterface(
objServer, ifacePath, ifaceName, boardNameOrig);
dbus_interface::populateInterfaceFromJson(
@@ -302,7 +302,7 @@
}
std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface =
- dbus_interface::createInterface(
+ dbus_interface.createInterface(
objServer, ifacePath,
"xyz.openbmc_project.Configuration." + itemType,
boardNameOrig);
@@ -326,7 +326,7 @@
continue;
}
- auto ifacePtr = dbus_interface::createInterface(
+ auto ifacePtr = dbus_interface.createInterface(
objServer, assocPath, "xyz.openbmc_project.Association.Definitions",
findBoard->second);
@@ -422,7 +422,7 @@
return;
}
- auto& ifaces = dbus_interface::getDeviceInterfaces(device);
+ auto& ifaces = dbus_interface.getDeviceInterfaces(device);
for (auto& iface : ifaces)
{
auto sharedPtr = iface.lock();
diff --git a/src/entity_manager/entity_manager.hpp b/src/entity_manager/entity_manager.hpp
index ba3914f..f499320 100644
--- a/src/entity_manager/entity_manager.hpp
+++ b/src/entity_manager/entity_manager.hpp
@@ -18,6 +18,7 @@
#pragma once
#include "../utils.hpp"
+#include "dbus_interface.hpp"
#include "topology.hpp"
#include <systemd/sd-journal.h>
@@ -44,6 +45,8 @@
Topology topology;
boost::asio::io_context& io;
+ dbus_interface::EMDBusInterface dbus_interface;
+
void propertiesChangedCallback();
void registerCallback(const std::string& path);
void publishNewConfiguration(const size_t& instance, size_t count,