xref: /openbmc/entity-manager/src/entity_manager/dbus_interface.hpp (revision d7908697f33dd4904a14ab128512528a7e8372c3)
1fc9e7fdaSChristopher Meis #pragma once
2fc9e7fdaSChristopher Meis 
3fc9e7fdaSChristopher Meis #include "configuration.hpp"
4fc9e7fdaSChristopher Meis 
5fc9e7fdaSChristopher Meis #include <nlohmann/json.hpp>
6fc9e7fdaSChristopher Meis #include <sdbusplus/asio/connection.hpp>
7fc9e7fdaSChristopher Meis #include <sdbusplus/asio/object_server.hpp>
8fc9e7fdaSChristopher Meis 
9fc9e7fdaSChristopher Meis #include <iostream>
10fc9e7fdaSChristopher Meis #include <set>
11fc9e7fdaSChristopher Meis #include <vector>
12fc9e7fdaSChristopher Meis 
13fc9e7fdaSChristopher Meis namespace dbus_interface
14fc9e7fdaSChristopher Meis {
15fc9e7fdaSChristopher Meis void tryIfaceInitialize(
16fc9e7fdaSChristopher Meis     std::shared_ptr<sdbusplus::asio::dbus_interface>& iface);
17fc9e7fdaSChristopher Meis 
18fc9e7fdaSChristopher Meis std::shared_ptr<sdbusplus::asio::dbus_interface> createInterface(
19fc9e7fdaSChristopher Meis     sdbusplus::asio::object_server& objServer, const std::string& path,
20fc9e7fdaSChristopher Meis     const std::string& interface, const std::string& parent,
21fc9e7fdaSChristopher Meis     bool checkNull = false);
22fc9e7fdaSChristopher Meis 
23fc9e7fdaSChristopher Meis template <typename PropertyType>
addArrayToDbus(const std::string & name,const nlohmann::json & array,sdbusplus::asio::dbus_interface * iface,sdbusplus::asio::PropertyPermission permission,nlohmann::json & systemConfiguration,const std::string & jsonPointerString)24fc9e7fdaSChristopher Meis void addArrayToDbus(const std::string& name, const nlohmann::json& array,
25fc9e7fdaSChristopher Meis                     sdbusplus::asio::dbus_interface* iface,
26fc9e7fdaSChristopher Meis                     sdbusplus::asio::PropertyPermission permission,
27fc9e7fdaSChristopher Meis                     nlohmann::json& systemConfiguration,
28fc9e7fdaSChristopher Meis                     const std::string& jsonPointerString)
29fc9e7fdaSChristopher Meis {
30fc9e7fdaSChristopher Meis     std::vector<PropertyType> values;
31fc9e7fdaSChristopher Meis     for (const auto& property : array)
32fc9e7fdaSChristopher Meis     {
33fc9e7fdaSChristopher Meis         auto ptr = property.get_ptr<const PropertyType*>();
34fc9e7fdaSChristopher Meis         if (ptr != nullptr)
35fc9e7fdaSChristopher Meis         {
36fc9e7fdaSChristopher Meis             values.emplace_back(*ptr);
37fc9e7fdaSChristopher Meis         }
38fc9e7fdaSChristopher Meis     }
39fc9e7fdaSChristopher Meis 
40fc9e7fdaSChristopher Meis     if (permission == sdbusplus::asio::PropertyPermission::readOnly)
41fc9e7fdaSChristopher Meis     {
42fc9e7fdaSChristopher Meis         iface->register_property(name, values);
43fc9e7fdaSChristopher Meis     }
44fc9e7fdaSChristopher Meis     else
45fc9e7fdaSChristopher Meis     {
46fc9e7fdaSChristopher Meis         iface->register_property(
47fc9e7fdaSChristopher Meis             name, values,
48fc9e7fdaSChristopher Meis             [&systemConfiguration,
49fc9e7fdaSChristopher Meis              jsonPointerString{std::string(jsonPointerString)}](
50fc9e7fdaSChristopher Meis                 const std::vector<PropertyType>& newVal,
51fc9e7fdaSChristopher Meis                 std::vector<PropertyType>& val) {
52fc9e7fdaSChristopher Meis                 val = newVal;
53fc9e7fdaSChristopher Meis                 if (!configuration::setJsonFromPointer(jsonPointerString, val,
54fc9e7fdaSChristopher Meis                                                        systemConfiguration))
55fc9e7fdaSChristopher Meis                 {
56fc9e7fdaSChristopher Meis                     std::cerr << "error setting json field\n";
57fc9e7fdaSChristopher Meis                     return -1;
58fc9e7fdaSChristopher Meis                 }
59fc9e7fdaSChristopher Meis                 if (!configuration::writeJsonFiles(systemConfiguration))
60fc9e7fdaSChristopher Meis                 {
61fc9e7fdaSChristopher Meis                     std::cerr << "error setting json file\n";
62fc9e7fdaSChristopher Meis                     return -1;
63fc9e7fdaSChristopher Meis                 }
64fc9e7fdaSChristopher Meis                 return 1;
65fc9e7fdaSChristopher Meis             });
66fc9e7fdaSChristopher Meis     }
67fc9e7fdaSChristopher Meis }
68fc9e7fdaSChristopher Meis 
69fc9e7fdaSChristopher Meis template <typename PropertyType>
addProperty(const std::string & name,const PropertyType & value,sdbusplus::asio::dbus_interface * iface,nlohmann::json & systemConfiguration,const std::string & jsonPointerString,sdbusplus::asio::PropertyPermission permission)70fc9e7fdaSChristopher Meis void addProperty(const std::string& name, const PropertyType& value,
71fc9e7fdaSChristopher Meis                  sdbusplus::asio::dbus_interface* iface,
72fc9e7fdaSChristopher Meis                  nlohmann::json& systemConfiguration,
73fc9e7fdaSChristopher Meis                  const std::string& jsonPointerString,
74fc9e7fdaSChristopher Meis                  sdbusplus::asio::PropertyPermission permission)
75fc9e7fdaSChristopher Meis {
76fc9e7fdaSChristopher Meis     if (permission == sdbusplus::asio::PropertyPermission::readOnly)
77fc9e7fdaSChristopher Meis     {
78fc9e7fdaSChristopher Meis         iface->register_property(name, value);
79fc9e7fdaSChristopher Meis         return;
80fc9e7fdaSChristopher Meis     }
81fc9e7fdaSChristopher Meis     iface->register_property(
82fc9e7fdaSChristopher Meis         name, value,
83fc9e7fdaSChristopher Meis         [&systemConfiguration,
84fc9e7fdaSChristopher Meis          jsonPointerString{std::string(jsonPointerString)}](
85fc9e7fdaSChristopher Meis             const PropertyType& newVal, PropertyType& val) {
86fc9e7fdaSChristopher Meis             val = newVal;
87fc9e7fdaSChristopher Meis             if (!configuration::setJsonFromPointer(jsonPointerString, val,
88fc9e7fdaSChristopher Meis                                                    systemConfiguration))
89fc9e7fdaSChristopher Meis             {
90fc9e7fdaSChristopher Meis                 std::cerr << "error setting json field\n";
91fc9e7fdaSChristopher Meis                 return -1;
92fc9e7fdaSChristopher Meis             }
93fc9e7fdaSChristopher Meis             if (!configuration::writeJsonFiles(systemConfiguration))
94fc9e7fdaSChristopher Meis             {
95fc9e7fdaSChristopher Meis                 std::cerr << "error setting json file\n";
96fc9e7fdaSChristopher Meis                 return -1;
97fc9e7fdaSChristopher Meis             }
98fc9e7fdaSChristopher Meis             return 1;
99fc9e7fdaSChristopher Meis         });
100fc9e7fdaSChristopher Meis }
101fc9e7fdaSChristopher Meis 
102*d7908697SAlexander Hansen template <typename PropertyType>
103*d7908697SAlexander Hansen void addValueToDBus(const std::string& key, const nlohmann::json& value,
104*d7908697SAlexander Hansen                     sdbusplus::asio::dbus_interface& iface,
105*d7908697SAlexander Hansen                     sdbusplus::asio::PropertyPermission permission,
106*d7908697SAlexander Hansen                     nlohmann::json& systemConfiguration,
107*d7908697SAlexander Hansen                     const std::string& path)
108*d7908697SAlexander Hansen {
109*d7908697SAlexander Hansen     if (value.is_array())
110*d7908697SAlexander Hansen     {
111*d7908697SAlexander Hansen         addArrayToDbus<PropertyType>(key, value, &iface, permission,
112*d7908697SAlexander Hansen                                      systemConfiguration, path);
113*d7908697SAlexander Hansen     }
114*d7908697SAlexander Hansen     else
115*d7908697SAlexander Hansen     {
116*d7908697SAlexander Hansen         addProperty(key, value.get<PropertyType>(), &iface, systemConfiguration,
117*d7908697SAlexander Hansen                     path, sdbusplus::asio::PropertyPermission::readOnly);
118*d7908697SAlexander Hansen     }
119*d7908697SAlexander Hansen }
120*d7908697SAlexander Hansen 
121fc9e7fdaSChristopher Meis void createDeleteObjectMethod(
122fc9e7fdaSChristopher Meis     const std::string& jsonPointerPath,
123fc9e7fdaSChristopher Meis     const std::shared_ptr<sdbusplus::asio::dbus_interface>& iface,
124fc9e7fdaSChristopher Meis     sdbusplus::asio::object_server& objServer,
125fc9e7fdaSChristopher Meis     nlohmann::json& systemConfiguration);
126fc9e7fdaSChristopher Meis 
127fc9e7fdaSChristopher Meis void populateInterfaceFromJson(
128fc9e7fdaSChristopher Meis     nlohmann::json& systemConfiguration, const std::string& jsonPointerPath,
129fc9e7fdaSChristopher Meis     std::shared_ptr<sdbusplus::asio::dbus_interface>& iface,
130fc9e7fdaSChristopher Meis     nlohmann::json& dict, sdbusplus::asio::object_server& objServer,
131fc9e7fdaSChristopher Meis     sdbusplus::asio::PropertyPermission permission =
132fc9e7fdaSChristopher Meis         sdbusplus::asio::PropertyPermission::readOnly);
133fc9e7fdaSChristopher Meis 
134fc9e7fdaSChristopher Meis void createAddObjectMethod(
135fc9e7fdaSChristopher Meis     const std::string& jsonPointerPath, const std::string& path,
136fc9e7fdaSChristopher Meis     nlohmann::json& systemConfiguration,
137fc9e7fdaSChristopher Meis     sdbusplus::asio::object_server& objServer, const std::string& board);
138fc9e7fdaSChristopher Meis 
139fc9e7fdaSChristopher Meis std::vector<std::weak_ptr<sdbusplus::asio::dbus_interface>>&
140fc9e7fdaSChristopher Meis     getDeviceInterfaces(const nlohmann::json& device);
141fc9e7fdaSChristopher Meis 
142fc9e7fdaSChristopher Meis } // namespace dbus_interface
143