blob: 3e24768d354bf2dfa83e7ff542e642e74698673a [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 Hansen89737252025-08-04 15:15:13 +020058 boost::asio::io_context& io;
59 sdbusplus::asio::object_server& objServer;
60
Alexander Hansen57604ed2025-06-27 13:22:28 +020061 boost::container::flat_map<
62 std::string,
63 std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>>
64 inventory;
65};
66
Christopher Meis12bea9b2025-04-03 10:14:42 +020067void tryIfaceInitialize(
68 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface);
69
Christopher Meis12bea9b2025-04-03 10:14:42 +020070template <typename PropertyType>
71void addArrayToDbus(const std::string& name, const nlohmann::json& array,
72 sdbusplus::asio::dbus_interface* iface,
73 sdbusplus::asio::PropertyPermission permission,
74 nlohmann::json& systemConfiguration,
75 const std::string& jsonPointerString)
76{
77 std::vector<PropertyType> values;
78 for (const auto& property : array)
79 {
80 auto ptr = property.get_ptr<const PropertyType*>();
81 if (ptr != nullptr)
82 {
83 values.emplace_back(*ptr);
84 }
85 }
86
87 if (permission == sdbusplus::asio::PropertyPermission::readOnly)
88 {
89 iface->register_property(name, values);
90 }
91 else
92 {
93 iface->register_property(
94 name, values,
95 [&systemConfiguration,
96 jsonPointerString{std::string(jsonPointerString)}](
97 const std::vector<PropertyType>& newVal,
98 std::vector<PropertyType>& val) {
99 val = newVal;
Christopher Meisf7252572025-06-11 13:22:05 +0200100 if (!setJsonFromPointer(jsonPointerString, val,
101 systemConfiguration))
Christopher Meis12bea9b2025-04-03 10:14:42 +0200102 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200103 lg2::error("error setting json field");
Christopher Meis12bea9b2025-04-03 10:14:42 +0200104 return -1;
105 }
Christopher Meisf7252572025-06-11 13:22:05 +0200106 if (!writeJsonFiles(systemConfiguration))
Christopher Meis12bea9b2025-04-03 10:14:42 +0200107 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200108 lg2::error("error setting json file");
Christopher Meis12bea9b2025-04-03 10:14:42 +0200109 return -1;
110 }
111 return 1;
112 });
113 }
114}
115
116template <typename PropertyType>
117void addProperty(const std::string& name, const PropertyType& value,
118 sdbusplus::asio::dbus_interface* iface,
119 nlohmann::json& systemConfiguration,
120 const std::string& jsonPointerString,
121 sdbusplus::asio::PropertyPermission permission)
122{
123 if (permission == sdbusplus::asio::PropertyPermission::readOnly)
124 {
125 iface->register_property(name, value);
126 return;
127 }
128 iface->register_property(
129 name, value,
130 [&systemConfiguration,
131 jsonPointerString{std::string(jsonPointerString)}](
132 const PropertyType& newVal, PropertyType& val) {
133 val = newVal;
Christopher Meisf7252572025-06-11 13:22:05 +0200134 if (!setJsonFromPointer(jsonPointerString, val,
135 systemConfiguration))
Christopher Meis12bea9b2025-04-03 10:14:42 +0200136 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200137 lg2::error("error setting json field");
Christopher Meis12bea9b2025-04-03 10:14:42 +0200138 return -1;
139 }
Christopher Meisf7252572025-06-11 13:22:05 +0200140 if (!writeJsonFiles(systemConfiguration))
Christopher Meis12bea9b2025-04-03 10:14:42 +0200141 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200142 lg2::error("error setting json file");
Christopher Meis12bea9b2025-04-03 10:14:42 +0200143 return -1;
144 }
145 return 1;
146 });
147}
148
Alexander Hansen5531eea2025-08-22 11:03:09 +0200149template <typename PropertyType>
Alexander Hansend7908692025-06-10 11:14:20 +0200150void addValueToDBus(const std::string& key, const nlohmann::json& value,
151 sdbusplus::asio::dbus_interface& iface,
152 sdbusplus::asio::PropertyPermission permission,
153 nlohmann::json& systemConfiguration,
154 const std::string& path)
155{
156 if (value.is_array())
157 {
158 addArrayToDbus<PropertyType>(key, value, &iface, permission,
159 systemConfiguration, path);
160 }
161 else
162 {
Alexander Hansen5531eea2025-08-22 11:03:09 +0200163 addProperty(key, value.get<PropertyType>(), &iface, systemConfiguration,
164 path, sdbusplus::asio::PropertyPermission::readOnly);
Alexander Hansend7908692025-06-10 11:14:20 +0200165 }
166}
167
Christopher Meis12bea9b2025-04-03 10:14:42 +0200168} // namespace dbus_interface