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