xref: /openbmc/phosphor-led-manager/utils.hpp (revision f05925599f7ad2b07e326eb3413a488ce21bd1c6)
11c737afcSGeorge Liu #pragma once
21c737afcSGeorge Liu #include <sdbusplus/server.hpp>
31c737afcSGeorge Liu 
4f2044037SPatrick Williams #include <unordered_map>
51c737afcSGeorge Liu #include <vector>
61c737afcSGeorge Liu namespace phosphor
71c737afcSGeorge Liu {
81c737afcSGeorge Liu namespace led
91c737afcSGeorge Liu {
101c737afcSGeorge Liu namespace utils
111c737afcSGeorge Liu {
121f0b715aSGeorge Liu static constexpr auto mapperBusName = "xyz.openbmc_project.ObjectMapper";
131f0b715aSGeorge Liu static constexpr auto mapperObjPath = "/xyz/openbmc_project/object_mapper";
141f0b715aSGeorge Liu static constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
151f0b715aSGeorge Liu static constexpr auto proIntf = "org.freedesktop.DBus.Properties";
161c737afcSGeorge Liu 
171c737afcSGeorge Liu // The value of the property(type: variant, contains some basic types)
184c5f5337SGeorge Liu // Eg: uint8_t : dutyOn, uint16_t : Period, std::string : Name,
194c5f5337SGeorge Liu // std::vector<std::string> : endpoints, bool : Functional
204c5f5337SGeorge Liu using PropertyValue = std::variant<uint8_t, uint16_t, std::string,
214c5f5337SGeorge Liu                                    std::vector<std::string>, bool>;
221c737afcSGeorge Liu 
23b6151623SGeorge Liu // The name of the property
24b6151623SGeorge Liu using DbusProperty = std::string;
25b6151623SGeorge Liu 
26b6151623SGeorge Liu // The Map to constructs all properties values of the interface
27f2044037SPatrick Williams using PropertyMap = std::unordered_map<DbusProperty, PropertyValue>;
28b6151623SGeorge Liu 
291c737afcSGeorge Liu /**
301c737afcSGeorge Liu  *  @class DBusHandler
311c737afcSGeorge Liu  *
321c737afcSGeorge Liu  *  Wrapper class to handle the D-Bus calls
331c737afcSGeorge Liu  *
341c737afcSGeorge Liu  *  This class contains the APIs to handle the D-Bus calls.
351c737afcSGeorge Liu  */
361c737afcSGeorge Liu class DBusHandler
371c737afcSGeorge Liu {
381c737afcSGeorge Liu   public:
391c737afcSGeorge Liu     /** @brief Get the bus connection. */
getBus()401c737afcSGeorge Liu     static auto& getBus()
411c737afcSGeorge Liu     {
421c737afcSGeorge Liu         static auto bus = sdbusplus::bus::new_default();
431c737afcSGeorge Liu         return bus;
441c737afcSGeorge Liu     }
451c737afcSGeorge Liu 
461c737afcSGeorge Liu     /**
471c737afcSGeorge Liu      *  @brief Get service name by the path and interface of the DBus.
481c737afcSGeorge Liu      *
491c737afcSGeorge Liu      *  @param[in] path      -  D-Bus object path
501c737afcSGeorge Liu      *  @param[in] interface -  D-Bus Interface
511c737afcSGeorge Liu      *
521c737afcSGeorge Liu      *  @return std::string  -  the D-Bus service name
531c737afcSGeorge Liu      *
541c737afcSGeorge Liu      */
55*f0592559SGeorge Liu     static std::string getService(const std::string& path,
56*f0592559SGeorge Liu                                   const std::string& interface);
571c737afcSGeorge Liu 
58b6151623SGeorge Liu     /** @brief Get All properties
59b6151623SGeorge Liu      *
60b6151623SGeorge Liu      *  @param[in] objectPath       -   D-Bus object path
61b6151623SGeorge Liu      *  @param[in] interface        -   D-Bus interface
62b6151623SGeorge Liu      *
63b6151623SGeorge Liu      *  @return The Map to constructs all properties values
64b6151623SGeorge Liu      *
653e073ba6SPatrick Williams      *  @throw sdbusplus::exception_t when it fails
66b6151623SGeorge Liu      */
67*f0592559SGeorge Liu     static PropertyMap getAllProperties(const std::string& objectPath,
68*f0592559SGeorge Liu                                         const std::string& interface);
69b6151623SGeorge Liu 
704c5f5337SGeorge Liu     /** @brief Get property(type: variant)
714c5f5337SGeorge Liu      *
724c5f5337SGeorge Liu      *  @param[in] objectPath       -   D-Bus object path
734c5f5337SGeorge Liu      *  @param[in] interface        -   D-Bus interface
744c5f5337SGeorge Liu      *  @param[in] propertyName     -   D-Bus property name
754c5f5337SGeorge Liu      *
764c5f5337SGeorge Liu      *  @return The value of the property(type: variant)
774c5f5337SGeorge Liu      *
783e073ba6SPatrick Williams      *  @throw sdbusplus::exception_t when it fails
794c5f5337SGeorge Liu      */
80*f0592559SGeorge Liu     static PropertyValue getProperty(const std::string& objectPath,
814c5f5337SGeorge Liu                                      const std::string& interface,
82*f0592559SGeorge Liu                                      const std::string& propertyName);
834c5f5337SGeorge Liu 
841c737afcSGeorge Liu     /** @brief Set D-Bus property
851c737afcSGeorge Liu      *
861c737afcSGeorge Liu      *  @param[in] objectPath       -   D-Bus object path
871c737afcSGeorge Liu      *  @param[in] interface        -   D-Bus interface
881c737afcSGeorge Liu      *  @param[in] propertyName     -   D-Bus property name
891c737afcSGeorge Liu      *  @param[in] value            -   The value to be set
901c737afcSGeorge Liu      *
913e073ba6SPatrick Williams      *  @throw sdbusplus::exception_t when it fails
921c737afcSGeorge Liu      */
93*f0592559SGeorge Liu     static void setProperty(const std::string& objectPath,
941c737afcSGeorge Liu                             const std::string& interface,
951c737afcSGeorge Liu                             const std::string& propertyName,
96*f0592559SGeorge Liu                             const PropertyValue& value);
97616a0716SGeorge Liu 
98616a0716SGeorge Liu     /** @brief Get sub tree paths by the path and interface of the DBus.
99616a0716SGeorge Liu      *
100616a0716SGeorge Liu      *  @param[in]  objectPath   -  D-Bus object path
101616a0716SGeorge Liu      *  @param[in]  interface    -  D-Bus object interface
102616a0716SGeorge Liu      *
103616a0716SGeorge Liu      *  @return std::vector<std::string> vector of subtree paths
104616a0716SGeorge Liu      */
105*f0592559SGeorge Liu     static std::vector<std::string> getSubTreePaths(
106*f0592559SGeorge Liu         const std::string& objectPath, const std::string& interface);
1071c737afcSGeorge Liu };
1081c737afcSGeorge Liu 
1091c737afcSGeorge Liu } // namespace utils
1101c737afcSGeorge Liu } // namespace led
1111c737afcSGeorge Liu } // namespace phosphor
112