blob: 1f978bcf6c1d9b421de9c303939f2f02cd2e7449 [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
Ed Tanousdbf95b22025-10-13 11:38:35 -070011#include <flat_map>
Christopher Meis12bea9b2025-04-03 10:14:42 +020012#include <vector>
13
14namespace dbus_interface
15{
Alexander Hansen57604ed2025-06-27 13:22:28 +020016
Alexander Hansena182cb72025-10-14 15:22:34 +020017using JsonVariantType =
18 std::variant<std::vector<std::string>, std::vector<double>, std::string,
19 int64_t, uint64_t, double, int32_t, uint32_t, int16_t,
20 uint16_t, uint8_t, bool>;
21
Alexander Hansen57604ed2025-06-27 13:22:28 +020022class EMDBusInterface
23{
24 public:
Alexander Hansen89737252025-08-04 15:15:13 +020025 EMDBusInterface(boost::asio::io_context& io,
26 sdbusplus::asio::object_server& objServer);
27
Alexander Hansen57604ed2025-06-27 13:22:28 +020028 std::shared_ptr<sdbusplus::asio::dbus_interface> createInterface(
Alexander Hansen89737252025-08-04 15:15:13 +020029 const std::string& path, const std::string& interface,
30 const std::string& parent, bool checkNull = false);
Alexander Hansen57604ed2025-06-27 13:22:28 +020031
32 std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>&
33 getDeviceInterfaces(const nlohmann::json& device);
34
Alexander Hansen89737252025-08-04 15:15:13 +020035 void createAddObjectMethod(const std::string& jsonPointerPath,
36 const std::string& path,
37 nlohmann::json& systemConfiguration,
38 const std::string& board);
39
40 void populateInterfaceFromJson(
41 nlohmann::json& systemConfiguration, const std::string& jsonPointerPath,
42 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface,
43 nlohmann::json& dict,
44 sdbusplus::asio::PropertyPermission permission =
45 sdbusplus::asio::PropertyPermission::readOnly);
46
47 void createDeleteObjectMethod(
48 const std::string& jsonPointerPath,
49 const std::shared_ptr<sdbusplus::asio::dbus_interface>& iface,
50 nlohmann::json& systemConfiguration);
Alexander Hansen57604ed2025-06-27 13:22:28 +020051
52 private:
Alexander Hansena182cb72025-10-14 15:22:34 +020053 void addObject(
54 const std::flat_map<std::string, JsonVariantType, std::less<>>& data,
55 nlohmann::json& systemConfiguration, const std::string& jsonPointerPath,
56 const std::string& path, const std::string& board);
57
Alexander Hansen7d5f2052025-10-14 15:57:24 +020058 // @brief: same as 'addObject', but operates on json
59 void addObjectJson(nlohmann::json& newData,
60 nlohmann::json& systemConfiguration,
61 const std::string& jsonPointerPath,
62 const std::string& path, const std::string& board);
63
Alexander Hansen89737252025-08-04 15:15:13 +020064 boost::asio::io_context& io;
65 sdbusplus::asio::object_server& objServer;
66
Alexander Hansen57604ed2025-06-27 13:22:28 +020067 boost::container::flat_map<
68 std::string,
69 std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>>
70 inventory;
71};
72
Christopher Meis12bea9b2025-04-03 10:14:42 +020073void tryIfaceInitialize(
74 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface);
75
Christopher Meis12bea9b2025-04-03 10:14:42 +020076template <typename PropertyType>
77void addArrayToDbus(const std::string& name, const nlohmann::json& array,
78 sdbusplus::asio::dbus_interface* iface,
79 sdbusplus::asio::PropertyPermission permission,
80 nlohmann::json& systemConfiguration,
81 const std::string& jsonPointerString)
82{
83 std::vector<PropertyType> values;
84 for (const auto& property : array)
85 {
86 auto ptr = property.get_ptr<const PropertyType*>();
87 if (ptr != nullptr)
88 {
89 values.emplace_back(*ptr);
90 }
91 }
92
93 if (permission == sdbusplus::asio::PropertyPermission::readOnly)
94 {
95 iface->register_property(name, values);
96 }
97 else
98 {
99 iface->register_property(
100 name, values,
101 [&systemConfiguration,
102 jsonPointerString{std::string(jsonPointerString)}](
103 const std::vector<PropertyType>& newVal,
104 std::vector<PropertyType>& val) {
105 val = newVal;
Christopher Meisf7252572025-06-11 13:22:05 +0200106 if (!setJsonFromPointer(jsonPointerString, val,
107 systemConfiguration))
Christopher Meis12bea9b2025-04-03 10:14:42 +0200108 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200109 lg2::error("error setting json field");
Christopher Meis12bea9b2025-04-03 10:14:42 +0200110 return -1;
111 }
Christopher Meisf7252572025-06-11 13:22:05 +0200112 if (!writeJsonFiles(systemConfiguration))
Christopher Meis12bea9b2025-04-03 10:14:42 +0200113 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200114 lg2::error("error setting json file");
Christopher Meis12bea9b2025-04-03 10:14:42 +0200115 return -1;
116 }
117 return 1;
118 });
119 }
120}
121
122template <typename PropertyType>
123void addProperty(const std::string& name, const PropertyType& value,
124 sdbusplus::asio::dbus_interface* iface,
125 nlohmann::json& systemConfiguration,
126 const std::string& jsonPointerString,
127 sdbusplus::asio::PropertyPermission permission)
128{
129 if (permission == sdbusplus::asio::PropertyPermission::readOnly)
130 {
131 iface->register_property(name, value);
132 return;
133 }
134 iface->register_property(
135 name, value,
136 [&systemConfiguration,
137 jsonPointerString{std::string(jsonPointerString)}](
138 const PropertyType& newVal, PropertyType& val) {
139 val = newVal;
Christopher Meisf7252572025-06-11 13:22:05 +0200140 if (!setJsonFromPointer(jsonPointerString, val,
141 systemConfiguration))
Christopher Meis12bea9b2025-04-03 10:14:42 +0200142 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200143 lg2::error("error setting json field");
Christopher Meis12bea9b2025-04-03 10:14:42 +0200144 return -1;
145 }
Christopher Meisf7252572025-06-11 13:22:05 +0200146 if (!writeJsonFiles(systemConfiguration))
Christopher Meis12bea9b2025-04-03 10:14:42 +0200147 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200148 lg2::error("error setting json file");
Christopher Meis12bea9b2025-04-03 10:14:42 +0200149 return -1;
150 }
151 return 1;
152 });
153}
154
Alexander Hansen5531eea2025-08-22 11:03:09 +0200155template <typename PropertyType>
Alexander Hansend7908692025-06-10 11:14:20 +0200156void addValueToDBus(const std::string& key, const nlohmann::json& value,
157 sdbusplus::asio::dbus_interface& iface,
158 sdbusplus::asio::PropertyPermission permission,
159 nlohmann::json& systemConfiguration,
160 const std::string& path)
161{
162 if (value.is_array())
163 {
164 addArrayToDbus<PropertyType>(key, value, &iface, permission,
165 systemConfiguration, path);
166 }
167 else
168 {
Alexander Hansen5531eea2025-08-22 11:03:09 +0200169 addProperty(key, value.get<PropertyType>(), &iface, systemConfiguration,
170 path, sdbusplus::asio::PropertyPermission::readOnly);
Alexander Hansend7908692025-06-10 11:14:20 +0200171 }
172}
173
Christopher Meis12bea9b2025-04-03 10:14:42 +0200174} // namespace dbus_interface