| 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 | |
| Alexander Hansen | a182cb7 | 2025-10-14 15:22:34 +0200 | [diff] [blame] | 17 | using 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 Hansen | 57604ed | 2025-06-27 13:22:28 +0200 | [diff] [blame] | 22 | class EMDBusInterface |
| 23 | { |
| 24 | public: |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 25 | EMDBusInterface(boost::asio::io_context& io, |
| 26 | sdbusplus::asio::object_server& objServer); |
| 27 | |
| Alexander Hansen | 57604ed | 2025-06-27 13:22:28 +0200 | [diff] [blame] | 28 | std::shared_ptr<sdbusplus::asio::dbus_interface> createInterface( |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 29 | const std::string& path, const std::string& interface, |
| 30 | const std::string& parent, bool checkNull = false); |
| Alexander Hansen | 57604ed | 2025-06-27 13:22:28 +0200 | [diff] [blame] | 31 | |
| 32 | std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>& |
| 33 | getDeviceInterfaces(const nlohmann::json& device); |
| 34 | |
| Alexander Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 35 | 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 Hansen | 57604ed | 2025-06-27 13:22:28 +0200 | [diff] [blame] | 51 | |
| 52 | private: |
| Alexander Hansen | a182cb7 | 2025-10-14 15:22:34 +0200 | [diff] [blame] | 53 | 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 Hansen | 8973725 | 2025-08-04 15:15:13 +0200 | [diff] [blame] | 58 | boost::asio::io_context& io; |
| 59 | sdbusplus::asio::object_server& objServer; |
| 60 | |
| Alexander Hansen | 57604ed | 2025-06-27 13:22:28 +0200 | [diff] [blame] | 61 | boost::container::flat_map< |
| 62 | std::string, |
| 63 | std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>> |
| 64 | inventory; |
| 65 | }; |
| 66 | |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 67 | void tryIfaceInitialize( |
| 68 | std::shared_ptr<sdbusplus::asio::dbus_interface>& iface); |
| 69 | |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 70 | template <typename PropertyType> |
| 71 | void 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 Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 100 | if (!setJsonFromPointer(jsonPointerString, val, |
| 101 | systemConfiguration)) |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 102 | { |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 103 | lg2::error("error setting json field"); |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 104 | return -1; |
| 105 | } |
| Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 106 | if (!writeJsonFiles(systemConfiguration)) |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 107 | { |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 108 | lg2::error("error setting json file"); |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 109 | return -1; |
| 110 | } |
| 111 | return 1; |
| 112 | }); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | template <typename PropertyType> |
| 117 | void 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 Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 134 | if (!setJsonFromPointer(jsonPointerString, val, |
| 135 | systemConfiguration)) |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 136 | { |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 137 | lg2::error("error setting json field"); |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 138 | return -1; |
| 139 | } |
| Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 140 | if (!writeJsonFiles(systemConfiguration)) |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 141 | { |
| Alexander Hansen | 8feb045 | 2025-09-15 14:29:20 +0200 | [diff] [blame] | 142 | lg2::error("error setting json file"); |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 143 | return -1; |
| 144 | } |
| 145 | return 1; |
| 146 | }); |
| 147 | } |
| 148 | |
| Alexander Hansen | 5531eea | 2025-08-22 11:03:09 +0200 | [diff] [blame] | 149 | template <typename PropertyType> |
| Alexander Hansen | d790869 | 2025-06-10 11:14:20 +0200 | [diff] [blame] | 150 | void 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 Hansen | 5531eea | 2025-08-22 11:03:09 +0200 | [diff] [blame] | 163 | addProperty(key, value.get<PropertyType>(), &iface, systemConfiguration, |
| 164 | path, sdbusplus::asio::PropertyPermission::readOnly); |
| Alexander Hansen | d790869 | 2025-06-10 11:14:20 +0200 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 168 | } // namespace dbus_interface |