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