blob: 30423e6223afbef9b8b184879333a3be66cb3535 [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>
7#include <sdbusplus/asio/connection.hpp>
8#include <sdbusplus/asio/object_server.hpp>
9
10#include <iostream>
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 {
92 std::cerr << "error setting json field\n";
93 return -1;
94 }
Christopher Meisf7252572025-06-11 13:22:05 +020095 if (!writeJsonFiles(systemConfiguration))
Christopher Meis12bea9b2025-04-03 10:14:42 +020096 {
97 std::cerr << "error setting json file\n";
98 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 {
126 std::cerr << "error setting json field\n";
127 return -1;
128 }
Christopher Meisf7252572025-06-11 13:22:05 +0200129 if (!writeJsonFiles(systemConfiguration))
Christopher Meis12bea9b2025-04-03 10:14:42 +0200130 {
131 std::cerr << "error setting json file\n";
132 return -1;
133 }
134 return 1;
135 });
136}
137
Alexander Hansen0123f8a2025-06-27 12:11:08 +0200138template <typename PropertyType, typename SinglePropertyType = 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 Hansen0123f8a2025-06-27 12:11:08 +0200152 addProperty(key, value.get<SinglePropertyType>(), &iface,
153 systemConfiguration, path,
154 sdbusplus::asio::PropertyPermission::readOnly);
Alexander Hansend7908692025-06-10 11:14:20 +0200155 }
156}
157
Christopher Meis12bea9b2025-04-03 10:14:42 +0200158} // namespace dbus_interface