cleanup: move all dbus interface functions in own files and namespace
Improving maintainability by 'separation of concern'.
Change-Id: I2797813c44ca0d70d03c8115035adc2c5efae4fe
Signed-off-by: Christopher Meis <christopher.meis@9elements.com>
diff --git a/src/dbus_interface.hpp b/src/dbus_interface.hpp
new file mode 100644
index 0000000..35ea742
--- /dev/null
+++ b/src/dbus_interface.hpp
@@ -0,0 +1,123 @@
+#pragma once
+
+#include "configuration.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
+{
+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,
+ sdbusplus::asio::PropertyPermission permission,
+ nlohmann::json& systemConfiguration,
+ const std::string& jsonPointerString)
+{
+ std::vector<PropertyType> values;
+ for (const auto& property : array)
+ {
+ auto ptr = property.get_ptr<const PropertyType*>();
+ if (ptr != nullptr)
+ {
+ values.emplace_back(*ptr);
+ }
+ }
+
+ if (permission == sdbusplus::asio::PropertyPermission::readOnly)
+ {
+ iface->register_property(name, values);
+ }
+ else
+ {
+ iface->register_property(
+ name, values,
+ [&systemConfiguration,
+ jsonPointerString{std::string(jsonPointerString)}](
+ const std::vector<PropertyType>& newVal,
+ std::vector<PropertyType>& val) {
+ val = newVal;
+ if (!configuration::setJsonFromPointer(jsonPointerString, val,
+ systemConfiguration))
+ {
+ std::cerr << "error setting json field\n";
+ return -1;
+ }
+ if (!configuration::writeJsonFiles(systemConfiguration))
+ {
+ std::cerr << "error setting json file\n";
+ return -1;
+ }
+ return 1;
+ });
+ }
+}
+
+template <typename PropertyType>
+void addProperty(const std::string& name, const PropertyType& value,
+ sdbusplus::asio::dbus_interface* iface,
+ nlohmann::json& systemConfiguration,
+ const std::string& jsonPointerString,
+ sdbusplus::asio::PropertyPermission permission)
+{
+ if (permission == sdbusplus::asio::PropertyPermission::readOnly)
+ {
+ iface->register_property(name, value);
+ return;
+ }
+ iface->register_property(
+ name, value,
+ [&systemConfiguration,
+ jsonPointerString{std::string(jsonPointerString)}](
+ const PropertyType& newVal, PropertyType& val) {
+ val = newVal;
+ if (!configuration::setJsonFromPointer(jsonPointerString, val,
+ systemConfiguration))
+ {
+ std::cerr << "error setting json field\n";
+ return -1;
+ }
+ if (!configuration::writeJsonFiles(systemConfiguration))
+ {
+ std::cerr << "error setting json file\n";
+ return -1;
+ }
+ return 1;
+ });
+}
+
+void createDeleteObjectMethod(
+ const std::string& jsonPointerPath,
+ const std::shared_ptr<sdbusplus::asio::dbus_interface>& iface,
+ sdbusplus::asio::object_server& objServer,
+ nlohmann::json& systemConfiguration);
+
+void populateInterfaceFromJson(
+ nlohmann::json& systemConfiguration, const std::string& jsonPointerPath,
+ std::shared_ptr<sdbusplus::asio::dbus_interface>& iface,
+ nlohmann::json& dict, sdbusplus::asio::object_server& objServer,
+ sdbusplus::asio::PropertyPermission permission =
+ sdbusplus::asio::PropertyPermission::readOnly);
+
+void createAddObjectMethod(
+ 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