Lei YU | fb19d99 | 2020-09-25 17:10:49 +0800 | [diff] [blame^] | 1 | #pragma once |
| 2 | |
| 3 | #include <sdbusplus/bus.hpp> |
| 4 | |
| 5 | #include <experimental/any> |
| 6 | #include <string> |
| 7 | #include <vector> |
| 8 | |
| 9 | namespace utils |
| 10 | { |
| 11 | |
| 12 | class UtilsInterface; |
| 13 | |
| 14 | // Due to a libstdc++ bug, we got compile error using std::any with gmock. |
| 15 | // A temporary workaround is to use std::experimental::any. |
| 16 | // See details in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90415 |
| 17 | using std::experimental::any; |
| 18 | using std::experimental::any_cast; |
| 19 | |
| 20 | /** |
| 21 | * @brief Get the implementation of UtilsInterface |
| 22 | */ |
| 23 | const UtilsInterface& getUtils(); |
| 24 | |
| 25 | /** @brief Get service name from object path and interface |
| 26 | * |
| 27 | * @param[in] bus - The Dbus bus object |
| 28 | * @param[in] path - The Dbus object path |
| 29 | * @param[in] interface - The Dbus interface |
| 30 | * |
| 31 | * @return The name of the service |
| 32 | */ |
| 33 | std::string getService(sdbusplus::bus::bus& bus, const char* path, |
| 34 | const char* interface); |
| 35 | |
| 36 | /** @brief Get all the service names from object path and interface |
| 37 | * |
| 38 | * @param[in] bus - The Dbus bus object |
| 39 | * @param[in] path - The Dbus object path |
| 40 | * @param[in] interface - The Dbus interface |
| 41 | * |
| 42 | * @return The name of the services |
| 43 | */ |
| 44 | std::vector<std::string> getServices(sdbusplus::bus::bus& bus, const char* path, |
| 45 | const char* interface); |
| 46 | |
| 47 | /** @brief The template function to get property from the requested dbus path |
| 48 | * |
| 49 | * @param[in] bus - The Dbus bus object |
| 50 | * @param[in] service - The Dbus service name |
| 51 | * @param[in] path - The Dbus object path |
| 52 | * @param[in] interface - The Dbus interface |
| 53 | * @param[in] propertyName - The property name to get |
| 54 | * |
| 55 | * @return The value of the property |
| 56 | */ |
| 57 | template <typename T> |
| 58 | T getProperty(sdbusplus::bus::bus& bus, const char* service, const char* path, |
| 59 | const char* interface, const char* propertyName); |
| 60 | |
| 61 | /** |
| 62 | * @brief The interface for utils |
| 63 | */ |
| 64 | class UtilsInterface |
| 65 | { |
| 66 | public: |
| 67 | // For now the code needs to get property for Present and Version |
| 68 | using PropertyType = std::variant<std::string, bool>; |
| 69 | |
| 70 | virtual ~UtilsInterface() = default; |
| 71 | |
| 72 | virtual std::string getService(sdbusplus::bus::bus& bus, const char* path, |
| 73 | const char* interface) const = 0; |
| 74 | |
| 75 | virtual std::vector<std::string> |
| 76 | getServices(sdbusplus::bus::bus& bus, const char* path, |
| 77 | const char* interface) const = 0; |
| 78 | |
| 79 | virtual any getPropertyImpl(sdbusplus::bus::bus& bus, const char* service, |
| 80 | const char* path, const char* interface, |
| 81 | const char* propertyName) const = 0; |
| 82 | |
| 83 | template <typename T> |
| 84 | T getProperty(sdbusplus::bus::bus& bus, const char* service, |
| 85 | const char* path, const char* interface, |
| 86 | const char* propertyName) const |
| 87 | { |
| 88 | any result = |
| 89 | getPropertyImpl(bus, service, path, interface, propertyName); |
| 90 | auto value = any_cast<PropertyType>(result); |
| 91 | return std::get<T>(value); |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | class Utils : public UtilsInterface |
| 96 | { |
| 97 | public: |
| 98 | std::string getService(sdbusplus::bus::bus& bus, const char* path, |
| 99 | const char* interface) const override; |
| 100 | |
| 101 | std::vector<std::string> getServices(sdbusplus::bus::bus& bus, |
| 102 | const char* path, |
| 103 | const char* interface) const override; |
| 104 | |
| 105 | any getPropertyImpl(sdbusplus::bus::bus& bus, const char* service, |
| 106 | const char* path, const char* interface, |
| 107 | const char* propertyName) const override; |
| 108 | }; |
| 109 | |
| 110 | inline std::string getService(sdbusplus::bus::bus& bus, const char* path, |
| 111 | const char* interface) |
| 112 | { |
| 113 | return getUtils().getService(bus, path, interface); |
| 114 | } |
| 115 | |
| 116 | inline std::vector<std::string> getServices(sdbusplus::bus::bus& bus, |
| 117 | const char* path, |
| 118 | const char* interface) |
| 119 | { |
| 120 | return getUtils().getServices(bus, path, interface); |
| 121 | } |
| 122 | |
| 123 | template <typename T> |
| 124 | T getProperty(sdbusplus::bus::bus& bus, const char* service, const char* path, |
| 125 | const char* interface, const char* propertyName) |
| 126 | { |
| 127 | return getUtils().getProperty<T>(bus, service, path, interface, |
| 128 | propertyName); |
| 129 | } |
| 130 | |
| 131 | } // namespace utils |