| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "configuration.hpp" |
| 4 | |
| Alexander Hansen | 57604ed | 2025-06-27 13:22:28 +0200 | [diff] [blame] | 5 | #include <boost/container/flat_map.hpp> |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 6 | #include <nlohmann/json.hpp> |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 7 | #include <phosphor-logging/lg2.hpp> |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 8 | #include <sdbusplus/asio/connection.hpp> |
| 9 | #include <sdbusplus/asio/object_server.hpp> |
| 10 | |
| Ed Tanous | dbf95b2 | 2025-10-13 11:38:35 -0700 | [diff] [blame^] | 11 | #include <flat_map> |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
| 14 | namespace dbus_interface |
| 15 | { |
| Alexander Hansen | 57604ed | 2025-06-27 13:22:28 +0200 | [diff] [blame] | 16 | |
| 17 | class EMDBusInterface |
| 18 | { |
| 19 | public: |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 20 | EMDBusInterface(boost::asio::io_context& io, |
| 21 | sdbusplus::asio::object_server& objServer); |
| 22 | |
| Alexander Hansen | 57604ed | 2025-06-27 13:22:28 +0200 | [diff] [blame] | 23 | std::shared_ptr<sdbusplus::asio::dbus_interface> createInterface( |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 24 | const std::string& path, const std::string& interface, |
| 25 | const std::string& parent, bool checkNull = false); |
| Alexander Hansen | 57604ed | 2025-06-27 13:22:28 +0200 | [diff] [blame] | 26 | |
| 27 | std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>& |
| 28 | getDeviceInterfaces(const nlohmann::json& device); |
| 29 | |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 30 | 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 Hansen | 57604ed | 2025-06-27 13:22:28 +0200 | [diff] [blame] | 46 | |
| 47 | private: |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 48 | boost::asio::io_context& io; |
| 49 | sdbusplus::asio::object_server& objServer; |
| 50 | |
| Alexander Hansen | 57604ed | 2025-06-27 13:22:28 +0200 | [diff] [blame] | 51 | boost::container::flat_map< |
| 52 | std::string, |
| 53 | std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>> |
| 54 | inventory; |
| 55 | }; |
| 56 | |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 57 | void tryIfaceInitialize( |
| 58 | std::shared_ptr<sdbusplus::asio::dbus_interface>& iface); |
| 59 | |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 60 | template <typename PropertyType> |
| 61 | void 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 Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 90 | if (!setJsonFromPointer(jsonPointerString, val, |
| 91 | systemConfiguration)) |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 92 | { |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 93 | lg2::error("error setting json field"); |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 94 | return -1; |
| 95 | } |
| Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 96 | if (!writeJsonFiles(systemConfiguration)) |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 97 | { |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 98 | lg2::error("error setting json file"); |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 99 | return -1; |
| 100 | } |
| 101 | return 1; |
| 102 | }); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | template <typename PropertyType> |
| 107 | void 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 Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 124 | if (!setJsonFromPointer(jsonPointerString, val, |
| 125 | systemConfiguration)) |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 126 | { |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 127 | lg2::error("error setting json field"); |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 128 | return -1; |
| 129 | } |
| Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 130 | if (!writeJsonFiles(systemConfiguration)) |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 131 | { |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 132 | lg2::error("error setting json file"); |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 133 | return -1; |
| 134 | } |
| 135 | return 1; |
| 136 | }); |
| 137 | } |
| 138 | |
| Alexander Hansen | 5531eea | 2025-08-22 11:03:09 +0200 | [diff] [blame] | 139 | template <typename PropertyType> |
| Alexander Hansen | d790869 | 2025-06-10 11:14:20 +0200 | [diff] [blame] | 140 | void 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 Hansen | 5531eea | 2025-08-22 11:03:09 +0200 | [diff] [blame] | 153 | addProperty(key, value.get<PropertyType>(), &iface, systemConfiguration, |
| 154 | path, sdbusplus::asio::PropertyPermission::readOnly); |
| Alexander Hansen | d790869 | 2025-06-10 11:14:20 +0200 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 158 | } // namespace dbus_interface |