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 | { |
George Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame] | 12 | static constexpr auto mapperBusName = "xyz.openbmc_project.ObjectMapper"; |
| 13 | static constexpr auto mapperObjPath = "/xyz/openbmc_project/object_mapper"; |
| 14 | static constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper"; |
| 15 | static constexpr auto proIntf = "org.freedesktop.DBus.Properties"; |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 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 | */ |
George Liu | f059255 | 2024-08-23 09:46:17 +0800 | [diff] [blame] | 55 | static std::string getService(const std::string& path, |
| 56 | const std::string& interface); |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 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 | 3e073ba | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 65 | * @throw sdbusplus::exception_t when it fails |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 66 | */ |
George Liu | f059255 | 2024-08-23 09:46:17 +0800 | [diff] [blame] | 67 | static PropertyMap getAllProperties(const std::string& objectPath, |
| 68 | const std::string& interface); |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 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 | 3e073ba | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 78 | * @throw sdbusplus::exception_t when it fails |
George Liu | 4c5f533 | 2020-10-10 17:04:28 +0800 | [diff] [blame] | 79 | */ |
George Liu | f059255 | 2024-08-23 09:46:17 +0800 | [diff] [blame] | 80 | static PropertyValue getProperty(const std::string& objectPath, |
| 81 | const std::string& interface, |
| 82 | const std::string& propertyName); |
George Liu | 4c5f533 | 2020-10-10 17:04:28 +0800 | [diff] [blame] | 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 | 3e073ba | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 91 | * @throw sdbusplus::exception_t when it fails |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 92 | */ |
George Liu | f059255 | 2024-08-23 09:46:17 +0800 | [diff] [blame] | 93 | static void setProperty(const std::string& objectPath, |
| 94 | const std::string& interface, |
| 95 | const std::string& propertyName, |
| 96 | const PropertyValue& value); |
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 | */ |
George Liu | f059255 | 2024-08-23 09:46:17 +0800 | [diff] [blame] | 105 | static std::vector<std::string> getSubTreePaths( |
| 106 | const std::string& objectPath, const std::string& interface); |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | } // namespace utils |
| 110 | } // namespace led |
| 111 | } // namespace phosphor |