George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | #include <sdbusplus/server.hpp> |
| 3 | |
| 4 | #include <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) |
George Liu | 4c5f533 | 2020-10-10 17:04:28 +0800 | [diff] [blame] | 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>; |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * @class DBusHandler |
| 25 | * |
| 26 | * Wrapper class to handle the D-Bus calls |
| 27 | * |
| 28 | * This class contains the APIs to handle the D-Bus calls. |
| 29 | */ |
| 30 | class DBusHandler |
| 31 | { |
| 32 | public: |
| 33 | /** @brief Get the bus connection. */ |
| 34 | static auto& getBus() |
| 35 | { |
| 36 | static auto bus = sdbusplus::bus::new_default(); |
| 37 | return bus; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @brief Get service name by the path and interface of the DBus. |
| 42 | * |
| 43 | * @param[in] path - D-Bus object path |
| 44 | * @param[in] interface - D-Bus Interface |
| 45 | * |
| 46 | * @return std::string - the D-Bus service name |
| 47 | * |
| 48 | */ |
| 49 | const std::string getService(const std::string& path, |
| 50 | const std::string& interface) const; |
| 51 | |
George Liu | 4c5f533 | 2020-10-10 17:04:28 +0800 | [diff] [blame] | 52 | /** @brief Get property(type: variant) |
| 53 | * |
| 54 | * @param[in] objectPath - D-Bus object path |
| 55 | * @param[in] interface - D-Bus interface |
| 56 | * @param[in] propertyName - D-Bus property name |
| 57 | * |
| 58 | * @return The value of the property(type: variant) |
| 59 | * |
| 60 | * @throw sdbusplus::exception::SdBusError when it fails |
| 61 | */ |
| 62 | const PropertyValue getProperty(const std::string& objectPath, |
| 63 | const std::string& interface, |
| 64 | const std::string& propertyName) const; |
| 65 | |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 66 | /** @brief Set D-Bus property |
| 67 | * |
| 68 | * @param[in] objectPath - D-Bus object path |
| 69 | * @param[in] interface - D-Bus interface |
| 70 | * @param[in] propertyName - D-Bus property name |
| 71 | * @param[in] value - The value to be set |
| 72 | * |
| 73 | * @throw sdbusplus::exception::SdBusError when it fails |
| 74 | */ |
| 75 | void setProperty(const std::string& objectPath, |
| 76 | const std::string& interface, |
| 77 | const std::string& propertyName, |
| 78 | const PropertyValue& value) const; |
| 79 | }; |
| 80 | |
| 81 | } // namespace utils |
| 82 | } // namespace led |
| 83 | } // namespace phosphor |