xref: /openbmc/phosphor-led-manager/utils.hpp (revision 6d254ee0)
1 #pragma once
2 #include <sdbusplus/server.hpp>
3 
4 #include <unordered_map>
5 #include <vector>
6 namespace phosphor
7 {
8 namespace led
9 {
10 namespace utils
11 {
12 constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
13 constexpr auto MAPPER_OBJ_PATH = "/xyz/openbmc_project/object_mapper";
14 constexpr auto MAPPER_IFACE = "xyz.openbmc_project.ObjectMapper";
15 constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
16 
17 // The value of the property(type: variant, contains some basic types)
18 // Eg: uint8_t : dutyOn, uint16_t : Period, std::string : Name,
19 // std::vector<std::string> : endpoints, bool : Functional
20 using PropertyValue = std::variant<uint8_t, uint16_t, std::string,
21                                    std::vector<std::string>, bool>;
22 
23 // The name of the property
24 using DbusProperty = std::string;
25 
26 // The Map to constructs all properties values of the interface
27 using PropertyMap = std::unordered_map<DbusProperty, PropertyValue>;
28 
29 /**
30  *  @class DBusHandler
31  *
32  *  Wrapper class to handle the D-Bus calls
33  *
34  *  This class contains the APIs to handle the D-Bus calls.
35  */
36 class DBusHandler
37 {
38   public:
39     /** @brief Get the bus connection. */
40     static auto& getBus()
41     {
42         static auto bus = sdbusplus::bus::new_default();
43         return bus;
44     }
45 
46     /**
47      *  @brief Get service name by the path and interface of the DBus.
48      *
49      *  @param[in] path      -  D-Bus object path
50      *  @param[in] interface -  D-Bus Interface
51      *
52      *  @return std::string  -  the D-Bus service name
53      *
54      */
55     const std::string getService(const std::string& path,
56                                  const std::string& interface) const;
57 
58     /** @brief Get All properties
59      *
60      *  @param[in] objectPath       -   D-Bus object path
61      *  @param[in] interface        -   D-Bus interface
62      *
63      *  @return The Map to constructs all properties values
64      *
65      *  @throw sdbusplus::exception_t when it fails
66      */
67     const PropertyMap getAllProperties(const std::string& objectPath,
68                                        const std::string& interface) const;
69 
70     /** @brief Get property(type: variant)
71      *
72      *  @param[in] objectPath       -   D-Bus object path
73      *  @param[in] interface        -   D-Bus interface
74      *  @param[in] propertyName     -   D-Bus property name
75      *
76      *  @return The value of the property(type: variant)
77      *
78      *  @throw sdbusplus::exception_t when it fails
79      */
80     const PropertyValue getProperty(const std::string& objectPath,
81                                     const std::string& interface,
82                                     const std::string& propertyName) const;
83 
84     /** @brief Set D-Bus property
85      *
86      *  @param[in] objectPath       -   D-Bus object path
87      *  @param[in] interface        -   D-Bus interface
88      *  @param[in] propertyName     -   D-Bus property name
89      *  @param[in] value            -   The value to be set
90      *
91      *  @throw sdbusplus::exception_t when it fails
92      */
93     void setProperty(const std::string& objectPath,
94                      const std::string& interface,
95                      const std::string& propertyName,
96                      const PropertyValue& value) const;
97 
98     /** @brief Get sub tree paths by the path and interface of the DBus.
99      *
100      *  @param[in]  objectPath   -  D-Bus object path
101      *  @param[in]  interface    -  D-Bus object interface
102      *
103      *  @return std::vector<std::string> vector of subtree paths
104      */
105     const std::vector<std::string>
106         getSubTreePaths(const std::string& objectPath,
107                         const std::string& interface);
108 };
109 
110 } // namespace utils
111 } // namespace led
112 } // namespace phosphor
113