George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | #include <sdbusplus/server.hpp> |
| 3 | |
Patrick Williams | f204403 | 2022-03-17 05:12:30 -0500 | [diff] [blame] | 4 | #include <unordered_map> |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 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 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 23 | // The name of the property |
| 24 | using DbusProperty = std::string; |
| 25 | |
| 26 | // The Map to constructs all properties values of the interface |
Patrick Williams | f204403 | 2022-03-17 05:12:30 -0500 | [diff] [blame] | 27 | using PropertyMap = std::unordered_map<DbusProperty, PropertyValue>; |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 28 | |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 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 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 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 | * |
Patrick Williams | 7152edc | 2021-09-02 09:41:54 -0500 | [diff] [blame] | 65 | * @throw sdbusplus::exception::exception when it fails |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 66 | */ |
| 67 | const PropertyMap getAllProperties(const std::string& objectPath, |
| 68 | const std::string& interface) const; |
| 69 | |
George Liu | 4c5f533 | 2020-10-10 17:04:28 +0800 | [diff] [blame] | 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 | * |
Patrick Williams | 7152edc | 2021-09-02 09:41:54 -0500 | [diff] [blame] | 78 | * @throw sdbusplus::exception::exception when it fails |
George Liu | 4c5f533 | 2020-10-10 17:04:28 +0800 | [diff] [blame] | 79 | */ |
| 80 | const PropertyValue getProperty(const std::string& objectPath, |
| 81 | const std::string& interface, |
| 82 | const std::string& propertyName) const; |
| 83 | |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 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 | * |
Patrick Williams | 7152edc | 2021-09-02 09:41:54 -0500 | [diff] [blame] | 91 | * @throw sdbusplus::exception::exception when it fails |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 92 | */ |
| 93 | void setProperty(const std::string& objectPath, |
| 94 | const std::string& interface, |
| 95 | const std::string& propertyName, |
| 96 | const PropertyValue& value) const; |
George Liu | 616a071 | 2021-02-18 10:50:24 +0800 | [diff] [blame] | 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); |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | } // namespace utils |
| 111 | } // namespace led |
| 112 | } // namespace phosphor |