blob: 9c20d8f7c6a25d4efabd31c2e7898f315b84d721 [file] [log] [blame]
Christopher Meis12bea9b2025-04-03 10:14:42 +02001#pragma once
2
3#include "configuration.hpp"
4
Alexander Hansen57604ed2025-06-27 13:22:28 +02005#include <boost/container/flat_map.hpp>
Christopher Meis12bea9b2025-04-03 10:14:42 +02006#include <nlohmann/json.hpp>
Alexander Hansen8feb0452025-09-15 14:29:20 +02007#include <phosphor-logging/lg2.hpp>
Christopher Meis12bea9b2025-04-03 10:14:42 +02008#include <sdbusplus/asio/connection.hpp>
9#include <sdbusplus/asio/object_server.hpp>
10
Christopher Meis12bea9b2025-04-03 10:14:42 +020011#include <vector>
12
13namespace dbus_interface
14{
Alexander Hansen57604ed2025-06-27 13:22:28 +020015
16class EMDBusInterface
17{
18 public:
Alexander Hansen89737252025-08-04 15:15:13 +020019 EMDBusInterface(boost::asio::io_context& io,
20 sdbusplus::asio::object_server& objServer);
21
Alexander Hansen57604ed2025-06-27 13:22:28 +020022 std::shared_ptr<sdbusplus::asio::dbus_interface> createInterface(
Alexander Hansen89737252025-08-04 15:15:13 +020023 const std::string& path, const std::string& interface,
24 const std::string& parent, bool checkNull = false);
Alexander Hansen57604ed2025-06-27 13:22:28 +020025
26 std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>&
27 getDeviceInterfaces(const nlohmann::json& device);
28
Alexander Hansen89737252025-08-04 15:15:13 +020029 void createAddObjectMethod(const std::string& jsonPointerPath,
30 const std::string& path,
31 nlohmann::json& systemConfiguration,
32 const std::string& board);
33
34 void populateInterfaceFromJson(
35 nlohmann::json& systemConfiguration, const std::string& jsonPointerPath,
36 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface,
37 nlohmann::json& dict,
38 sdbusplus::asio::PropertyPermission permission =
39 sdbusplus::asio::PropertyPermission::readOnly);
40
41 void createDeleteObjectMethod(
42 const std::string& jsonPointerPath,
43 const std::shared_ptr<sdbusplus::asio::dbus_interface>& iface,
44 nlohmann::json& systemConfiguration);
Alexander Hansen57604ed2025-06-27 13:22:28 +020045
46 private:
Alexander Hansen89737252025-08-04 15:15:13 +020047 boost::asio::io_context& io;
48 sdbusplus::asio::object_server& objServer;
49
Alexander Hansen57604ed2025-06-27 13:22:28 +020050 boost::container::flat_map<
51 std::string,
52 std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>>
53 inventory;
54};
55
Christopher Meis12bea9b2025-04-03 10:14:42 +020056void tryIfaceInitialize(
57 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface);
58
Christopher Meis12bea9b2025-04-03 10:14:42 +020059template <typename PropertyType>
60void addArrayToDbus(const std::string& name, const nlohmann::json& array,
61 sdbusplus::asio::dbus_interface* iface,
62 sdbusplus::asio::PropertyPermission permission,
63 nlohmann::json& systemConfiguration,
64 const std::string& jsonPointerString)
65{
66 std::vector<PropertyType> values;
67 for (const auto& property : array)
68 {
69 auto ptr = property.get_ptr<const PropertyType*>();
70 if (ptr != nullptr)
71 {
72 values.emplace_back(*ptr);
73 }
74 }
75
76 if (permission == sdbusplus::asio::PropertyPermission::readOnly)
77 {
78 iface->register_property(name, values);
79 }
80 else
81 {
82 iface->register_property(
83 name, values,
84 [&systemConfiguration,
85 jsonPointerString{std::string(jsonPointerString)}](
86 const std::vector<PropertyType>& newVal,
87 std::vector<PropertyType>& val) {
88 val = newVal;
Christopher Meisf7252572025-06-11 13:22:05 +020089 if (!setJsonFromPointer(jsonPointerString, val,
90 systemConfiguration))
Christopher Meis12bea9b2025-04-03 10:14:42 +020091 {
Alexander Hansen8feb0452025-09-15 14:29:20 +020092 lg2::error("error setting json field");
Christopher Meis12bea9b2025-04-03 10:14:42 +020093 return -1;
94 }
Christopher Meisf7252572025-06-11 13:22:05 +020095 if (!writeJsonFiles(systemConfiguration))
Christopher Meis12bea9b2025-04-03 10:14:42 +020096 {
Alexander Hansen8feb0452025-09-15 14:29:20 +020097 lg2::error("error setting json file");
Christopher Meis12bea9b2025-04-03 10:14:42 +020098 return -1;
99 }
100 return 1;
101 });
102 }
103}
104
105template <typename PropertyType>
106void addProperty(const std::string& name, const PropertyType& value,
107 sdbusplus::asio::dbus_interface* iface,
108 nlohmann::json& systemConfiguration,
109 const std::string& jsonPointerString,
110 sdbusplus::asio::PropertyPermission permission)
111{
112 if (permission == sdbusplus::asio::PropertyPermission::readOnly)
113 {
114 iface->register_property(name, value);
115 return;
116 }
117 iface->register_property(
118 name, value,
119 [&systemConfiguration,
120 jsonPointerString{std::string(jsonPointerString)}](
121 const PropertyType& newVal, PropertyType& val) {
122 val = newVal;
Christopher Meisf7252572025-06-11 13:22:05 +0200123 if (!setJsonFromPointer(jsonPointerString, val,
124 systemConfiguration))
Christopher Meis12bea9b2025-04-03 10:14:42 +0200125 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200126 lg2::error("error setting json field");
Christopher Meis12bea9b2025-04-03 10:14:42 +0200127 return -1;
128 }
Christopher Meisf7252572025-06-11 13:22:05 +0200129 if (!writeJsonFiles(systemConfiguration))
Christopher Meis12bea9b2025-04-03 10:14:42 +0200130 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200131 lg2::error("error setting json file");
Christopher Meis12bea9b2025-04-03 10:14:42 +0200132 return -1;
133 }
134 return 1;
135 });
136}
137
Alexander Hansen5531eea2025-08-22 11:03:09 +0200138template <typename PropertyType>
Alexander Hansend7908692025-06-10 11:14:20 +0200139void addValueToDBus(const std::string& key, const nlohmann::json& value,
140 sdbusplus::asio::dbus_interface& iface,
141 sdbusplus::asio::PropertyPermission permission,
142 nlohmann::json& systemConfiguration,
143 const std::string& path)
144{
145 if (value.is_array())
146 {
147 addArrayToDbus<PropertyType>(key, value, &iface, permission,
148 systemConfiguration, path);
149 }
150 else
151 {
Alexander Hansen5531eea2025-08-22 11:03:09 +0200152 addProperty(key, value.get<PropertyType>(), &iface, systemConfiguration,
153 path, sdbusplus::asio::PropertyPermission::readOnly);
Alexander Hansend7908692025-06-10 11:14:20 +0200154 }
155}
156
Christopher Meis12bea9b2025-04-03 10:14:42 +0200157} // namespace dbus_interface