blob: babc8541c2491c6ef4283d7478fd9bcec4676ff1 [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
17class EMDBusInterface
18{
19 public:
Alexander Hansen89737252025-08-04 15:15:13 +020020 EMDBusInterface(boost::asio::io_context& io,
21 sdbusplus::asio::object_server& objServer);
22
Alexander Hansen57604ed2025-06-27 13:22:28 +020023 std::shared_ptr<sdbusplus::asio::dbus_interface> createInterface(
Alexander Hansen89737252025-08-04 15:15:13 +020024 const std::string& path, const std::string& interface,
25 const std::string& parent, bool checkNull = false);
Alexander Hansen57604ed2025-06-27 13:22:28 +020026
27 std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>&
28 getDeviceInterfaces(const nlohmann::json& device);
29
Alexander Hansen89737252025-08-04 15:15:13 +020030 void createAddObjectMethod(const std::string& jsonPointerPath,
31 const std::string& path,
32 nlohmann::json& systemConfiguration,
33 const std::string& board);
34
35 void populateInterfaceFromJson(
36 nlohmann::json& systemConfiguration, const std::string& jsonPointerPath,
37 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface,
38 nlohmann::json& dict,
39 sdbusplus::asio::PropertyPermission permission =
40 sdbusplus::asio::PropertyPermission::readOnly);
41
42 void createDeleteObjectMethod(
43 const std::string& jsonPointerPath,
44 const std::shared_ptr<sdbusplus::asio::dbus_interface>& iface,
45 nlohmann::json& systemConfiguration);
Alexander Hansen57604ed2025-06-27 13:22:28 +020046
47 private:
Alexander Hansen89737252025-08-04 15:15:13 +020048 boost::asio::io_context& io;
49 sdbusplus::asio::object_server& objServer;
50
Alexander Hansen57604ed2025-06-27 13:22:28 +020051 boost::container::flat_map<
52 std::string,
53 std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>>
54 inventory;
55};
56
Christopher Meis12bea9b2025-04-03 10:14:42 +020057void tryIfaceInitialize(
58 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface);
59
Christopher Meis12bea9b2025-04-03 10:14:42 +020060template <typename PropertyType>
61void addArrayToDbus(const std::string& name, const nlohmann::json& array,
62 sdbusplus::asio::dbus_interface* iface,
63 sdbusplus::asio::PropertyPermission permission,
64 nlohmann::json& systemConfiguration,
65 const std::string& jsonPointerString)
66{
67 std::vector<PropertyType> values;
68 for (const auto& property : array)
69 {
70 auto ptr = property.get_ptr<const PropertyType*>();
71 if (ptr != nullptr)
72 {
73 values.emplace_back(*ptr);
74 }
75 }
76
77 if (permission == sdbusplus::asio::PropertyPermission::readOnly)
78 {
79 iface->register_property(name, values);
80 }
81 else
82 {
83 iface->register_property(
84 name, values,
85 [&systemConfiguration,
86 jsonPointerString{std::string(jsonPointerString)}](
87 const std::vector<PropertyType>& newVal,
88 std::vector<PropertyType>& val) {
89 val = newVal;
Christopher Meisf7252572025-06-11 13:22:05 +020090 if (!setJsonFromPointer(jsonPointerString, val,
91 systemConfiguration))
Christopher Meis12bea9b2025-04-03 10:14:42 +020092 {
Alexander Hansen8feb0452025-09-15 14:29:20 +020093 lg2::error("error setting json field");
Christopher Meis12bea9b2025-04-03 10:14:42 +020094 return -1;
95 }
Christopher Meisf7252572025-06-11 13:22:05 +020096 if (!writeJsonFiles(systemConfiguration))
Christopher Meis12bea9b2025-04-03 10:14:42 +020097 {
Alexander Hansen8feb0452025-09-15 14:29:20 +020098 lg2::error("error setting json file");
Christopher Meis12bea9b2025-04-03 10:14:42 +020099 return -1;
100 }
101 return 1;
102 });
103 }
104}
105
106template <typename PropertyType>
107void addProperty(const std::string& name, const PropertyType& value,
108 sdbusplus::asio::dbus_interface* iface,
109 nlohmann::json& systemConfiguration,
110 const std::string& jsonPointerString,
111 sdbusplus::asio::PropertyPermission permission)
112{
113 if (permission == sdbusplus::asio::PropertyPermission::readOnly)
114 {
115 iface->register_property(name, value);
116 return;
117 }
118 iface->register_property(
119 name, value,
120 [&systemConfiguration,
121 jsonPointerString{std::string(jsonPointerString)}](
122 const PropertyType& newVal, PropertyType& val) {
123 val = newVal;
Christopher Meisf7252572025-06-11 13:22:05 +0200124 if (!setJsonFromPointer(jsonPointerString, val,
125 systemConfiguration))
Christopher Meis12bea9b2025-04-03 10:14:42 +0200126 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200127 lg2::error("error setting json field");
Christopher Meis12bea9b2025-04-03 10:14:42 +0200128 return -1;
129 }
Christopher Meisf7252572025-06-11 13:22:05 +0200130 if (!writeJsonFiles(systemConfiguration))
Christopher Meis12bea9b2025-04-03 10:14:42 +0200131 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200132 lg2::error("error setting json file");
Christopher Meis12bea9b2025-04-03 10:14:42 +0200133 return -1;
134 }
135 return 1;
136 });
137}
138
Alexander Hansen5531eea2025-08-22 11:03:09 +0200139template <typename PropertyType>
Alexander Hansend7908692025-06-10 11:14:20 +0200140void addValueToDBus(const std::string& key, const nlohmann::json& value,
141 sdbusplus::asio::dbus_interface& iface,
142 sdbusplus::asio::PropertyPermission permission,
143 nlohmann::json& systemConfiguration,
144 const std::string& path)
145{
146 if (value.is_array())
147 {
148 addArrayToDbus<PropertyType>(key, value, &iface, permission,
149 systemConfiguration, path);
150 }
151 else
152 {
Alexander Hansen5531eea2025-08-22 11:03:09 +0200153 addProperty(key, value.get<PropertyType>(), &iface, systemConfiguration,
154 path, 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