| 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> | 
|  | 7 | #include <sdbusplus/asio/connection.hpp> | 
|  | 8 | #include <sdbusplus/asio/object_server.hpp> | 
|  | 9 |  | 
|  | 10 | #include <iostream> | 
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 11 | #include <vector> | 
|  | 12 |  | 
|  | 13 | namespace dbus_interface | 
|  | 14 | { | 
| Alexander Hansen | 57604ed | 2025-06-27 13:22:28 +0200 | [diff] [blame] | 15 |  | 
|  | 16 | class EMDBusInterface | 
|  | 17 | { | 
|  | 18 | public: | 
|  | 19 | std::shared_ptr<sdbusplus::asio::dbus_interface> createInterface( | 
|  | 20 | sdbusplus::asio::object_server& objServer, const std::string& path, | 
|  | 21 | const std::string& interface, const std::string& parent, | 
|  | 22 | bool checkNull = false); | 
|  | 23 |  | 
|  | 24 | std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>& | 
|  | 25 | getDeviceInterfaces(const nlohmann::json& device); | 
|  | 26 |  | 
|  | 27 | void createAddObjectMethod( | 
|  | 28 | boost::asio::io_context& io, const std::string& jsonPointerPath, | 
|  | 29 | const std::string& path, nlohmann::json& systemConfiguration, | 
|  | 30 | sdbusplus::asio::object_server& objServer, const std::string& board); | 
|  | 31 |  | 
|  | 32 | private: | 
|  | 33 | boost::container::flat_map< | 
|  | 34 | std::string, | 
|  | 35 | std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>> | 
|  | 36 | inventory; | 
|  | 37 | }; | 
|  | 38 |  | 
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 39 | void tryIfaceInitialize( | 
|  | 40 | std::shared_ptr<sdbusplus::asio::dbus_interface>& iface); | 
|  | 41 |  | 
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 42 | template <typename PropertyType> | 
|  | 43 | void addArrayToDbus(const std::string& name, const nlohmann::json& array, | 
|  | 44 | sdbusplus::asio::dbus_interface* iface, | 
|  | 45 | sdbusplus::asio::PropertyPermission permission, | 
|  | 46 | nlohmann::json& systemConfiguration, | 
|  | 47 | const std::string& jsonPointerString) | 
|  | 48 | { | 
|  | 49 | std::vector<PropertyType> values; | 
|  | 50 | for (const auto& property : array) | 
|  | 51 | { | 
|  | 52 | auto ptr = property.get_ptr<const PropertyType*>(); | 
|  | 53 | if (ptr != nullptr) | 
|  | 54 | { | 
|  | 55 | values.emplace_back(*ptr); | 
|  | 56 | } | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | if (permission == sdbusplus::asio::PropertyPermission::readOnly) | 
|  | 60 | { | 
|  | 61 | iface->register_property(name, values); | 
|  | 62 | } | 
|  | 63 | else | 
|  | 64 | { | 
|  | 65 | iface->register_property( | 
|  | 66 | name, values, | 
|  | 67 | [&systemConfiguration, | 
|  | 68 | jsonPointerString{std::string(jsonPointerString)}]( | 
|  | 69 | const std::vector<PropertyType>& newVal, | 
|  | 70 | std::vector<PropertyType>& val) { | 
|  | 71 | val = newVal; | 
| Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 72 | if (!setJsonFromPointer(jsonPointerString, val, | 
|  | 73 | systemConfiguration)) | 
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 74 | { | 
|  | 75 | std::cerr << "error setting json field\n"; | 
|  | 76 | return -1; | 
|  | 77 | } | 
| Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 78 | if (!writeJsonFiles(systemConfiguration)) | 
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 79 | { | 
|  | 80 | std::cerr << "error setting json file\n"; | 
|  | 81 | return -1; | 
|  | 82 | } | 
|  | 83 | return 1; | 
|  | 84 | }); | 
|  | 85 | } | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | template <typename PropertyType> | 
|  | 89 | void addProperty(const std::string& name, const PropertyType& value, | 
|  | 90 | sdbusplus::asio::dbus_interface* iface, | 
|  | 91 | nlohmann::json& systemConfiguration, | 
|  | 92 | const std::string& jsonPointerString, | 
|  | 93 | sdbusplus::asio::PropertyPermission permission) | 
|  | 94 | { | 
|  | 95 | if (permission == sdbusplus::asio::PropertyPermission::readOnly) | 
|  | 96 | { | 
|  | 97 | iface->register_property(name, value); | 
|  | 98 | return; | 
|  | 99 | } | 
|  | 100 | iface->register_property( | 
|  | 101 | name, value, | 
|  | 102 | [&systemConfiguration, | 
|  | 103 | jsonPointerString{std::string(jsonPointerString)}]( | 
|  | 104 | const PropertyType& newVal, PropertyType& val) { | 
|  | 105 | val = newVal; | 
| Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 106 | if (!setJsonFromPointer(jsonPointerString, val, | 
|  | 107 | systemConfiguration)) | 
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 108 | { | 
|  | 109 | std::cerr << "error setting json field\n"; | 
|  | 110 | return -1; | 
|  | 111 | } | 
| Christopher Meis | f725257 | 2025-06-11 13:22:05 +0200 | [diff] [blame] | 112 | if (!writeJsonFiles(systemConfiguration)) | 
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 113 | { | 
|  | 114 | std::cerr << "error setting json file\n"; | 
|  | 115 | return -1; | 
|  | 116 | } | 
|  | 117 | return 1; | 
|  | 118 | }); | 
|  | 119 | } | 
|  | 120 |  | 
| Alexander Hansen | 0123f8a | 2025-06-27 12:11:08 +0200 | [diff] [blame] | 121 | template <typename PropertyType, typename SinglePropertyType = PropertyType> | 
| Alexander Hansen | d790869 | 2025-06-10 11:14:20 +0200 | [diff] [blame] | 122 | void addValueToDBus(const std::string& key, const nlohmann::json& value, | 
|  | 123 | sdbusplus::asio::dbus_interface& iface, | 
|  | 124 | sdbusplus::asio::PropertyPermission permission, | 
|  | 125 | nlohmann::json& systemConfiguration, | 
|  | 126 | const std::string& path) | 
|  | 127 | { | 
|  | 128 | if (value.is_array()) | 
|  | 129 | { | 
|  | 130 | addArrayToDbus<PropertyType>(key, value, &iface, permission, | 
|  | 131 | systemConfiguration, path); | 
|  | 132 | } | 
|  | 133 | else | 
|  | 134 | { | 
| Alexander Hansen | 0123f8a | 2025-06-27 12:11:08 +0200 | [diff] [blame] | 135 | addProperty(key, value.get<SinglePropertyType>(), &iface, | 
|  | 136 | systemConfiguration, path, | 
|  | 137 | sdbusplus::asio::PropertyPermission::readOnly); | 
| Alexander Hansen | d790869 | 2025-06-10 11:14:20 +0200 | [diff] [blame] | 138 | } | 
|  | 139 | } | 
|  | 140 |  | 
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 141 | void createDeleteObjectMethod( | 
|  | 142 | const std::string& jsonPointerPath, | 
|  | 143 | const std::shared_ptr<sdbusplus::asio::dbus_interface>& iface, | 
|  | 144 | sdbusplus::asio::object_server& objServer, | 
| Alexander Hansen | a555acf | 2025-06-27 11:59:10 +0200 | [diff] [blame] | 145 | nlohmann::json& systemConfiguration, boost::asio::io_context& io); | 
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 146 |  | 
|  | 147 | void populateInterfaceFromJson( | 
| Alexander Hansen | a555acf | 2025-06-27 11:59:10 +0200 | [diff] [blame] | 148 | boost::asio::io_context& io, nlohmann::json& systemConfiguration, | 
|  | 149 | const std::string& jsonPointerPath, | 
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 150 | std::shared_ptr<sdbusplus::asio::dbus_interface>& iface, | 
|  | 151 | nlohmann::json& dict, sdbusplus::asio::object_server& objServer, | 
|  | 152 | sdbusplus::asio::PropertyPermission permission = | 
|  | 153 | sdbusplus::asio::PropertyPermission::readOnly); | 
|  | 154 |  | 
| Christopher Meis | 12bea9b | 2025-04-03 10:14:42 +0200 | [diff] [blame] | 155 | } // namespace dbus_interface |