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 | |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 40 | /** @brief Get all the service names from object path and interface |
| 41 | * |
| 42 | * @param[in] bus - The Dbus bus object |
| 43 | * @param[in] path - The Dbus object path |
| 44 | * @param[in] interface - The Dbus interface |
| 45 | * |
| 46 | * @return The name of the services |
| 47 | */ |
| 48 | std::vector<std::string> getServices(sdbusplus::bus::bus& bus, const char* path, |
| 49 | const char* interface); |
| 50 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 51 | /** @brief The template function to get property from the requested dbus path |
| 52 | * |
| 53 | * @param[in] bus - The Dbus bus object |
| 54 | * @param[in] service - The Dbus service name |
| 55 | * @param[in] path - The Dbus object path |
| 56 | * @param[in] interface - The Dbus interface |
| 57 | * @param[in] propertyName - The property name to get |
| 58 | * |
| 59 | * @return The value of the property |
| 60 | */ |
| 61 | template <typename T> |
| 62 | T getProperty(sdbusplus::bus::bus& bus, const char* service, const char* path, |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 63 | const char* interface, const char* propertyName); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * @brief Calculate the version id from the version string. |
| 67 | * |
| 68 | * @details The version id is a unique 8 hexadecimal digit id |
| 69 | * calculated from the version string. |
| 70 | * |
| 71 | * @param[in] version - The image version string (e.g. v1.99.10-19). |
| 72 | * |
| 73 | * @return The id. |
| 74 | */ |
| 75 | std::string getVersionId(const std::string& version); |
| 76 | |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 77 | /** @brief Get version of PSU specified by the inventory path |
| 78 | * |
| 79 | * @param[in] inventoryPath - The PSU inventory object path |
| 80 | * |
| 81 | * @return The version string, or empry string if it fails to get the version |
| 82 | */ |
| 83 | std::string getVersion(const std::string& inventoryPath); |
| 84 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 85 | /** |
| 86 | * @brief The interface for utils |
| 87 | */ |
| 88 | class UtilsInterface |
| 89 | { |
| 90 | public: |
| 91 | // For now the code needs to get property for Present and Version |
| 92 | using PropertyType = sdbusplus::message::variant<std::string, bool>; |
| 93 | |
| 94 | virtual ~UtilsInterface() = default; |
| 95 | |
| 96 | virtual std::vector<std::string> |
| 97 | getPSUInventoryPath(sdbusplus::bus::bus& bus) const = 0; |
| 98 | |
| 99 | virtual std::string getService(sdbusplus::bus::bus& bus, const char* path, |
| 100 | const char* interface) const = 0; |
| 101 | |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 102 | virtual std::vector<std::string> |
| 103 | getServices(sdbusplus::bus::bus& bus, const char* path, |
| 104 | const char* interface) const = 0; |
| 105 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 106 | virtual std::string getVersionId(const std::string& version) const = 0; |
| 107 | |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 108 | virtual std::string getVersion(const std::string& inventoryPath) const = 0; |
| 109 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 110 | virtual any getPropertyImpl(sdbusplus::bus::bus& bus, const char* service, |
| 111 | const char* path, const char* interface, |
| 112 | const char* propertyName) const = 0; |
| 113 | |
| 114 | template <typename T> |
| 115 | T getProperty(sdbusplus::bus::bus& bus, const char* service, |
| 116 | const char* path, const char* interface, |
| 117 | const char* propertyName) const |
| 118 | { |
| 119 | any result = |
| 120 | getPropertyImpl(bus, service, path, interface, propertyName); |
| 121 | auto value = any_cast<PropertyType>(result); |
| 122 | return sdbusplus::message::variant_ns::get<T>(value); |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | class Utils : public UtilsInterface |
| 127 | { |
| 128 | public: |
| 129 | std::vector<std::string> |
| 130 | getPSUInventoryPath(sdbusplus::bus::bus& bus) const override; |
| 131 | |
| 132 | std::string getService(sdbusplus::bus::bus& bus, const char* path, |
| 133 | const char* interface) const override; |
| 134 | |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 135 | std::vector<std::string> getServices(sdbusplus::bus::bus& bus, |
| 136 | const char* path, |
| 137 | const char* interface) const override; |
| 138 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 139 | std::string getVersionId(const std::string& version) const override; |
| 140 | |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 141 | std::string getVersion(const std::string& inventoryPath) const override; |
| 142 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 143 | any getPropertyImpl(sdbusplus::bus::bus& bus, const char* service, |
| 144 | const char* path, const char* interface, |
| 145 | const char* propertyName) const override; |
| 146 | }; |
| 147 | |
| 148 | inline std::string getService(sdbusplus::bus::bus& bus, const char* path, |
| 149 | const char* interface) |
| 150 | { |
| 151 | return getUtils().getService(bus, path, interface); |
| 152 | } |
| 153 | |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 154 | inline std::vector<std::string> getServices(sdbusplus::bus::bus& bus, |
| 155 | const char* path, |
| 156 | const char* interface) |
| 157 | { |
| 158 | return getUtils().getServices(bus, path, interface); |
| 159 | } |
| 160 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 161 | inline std::vector<std::string> getPSUInventoryPath(sdbusplus::bus::bus& bus) |
| 162 | { |
| 163 | return getUtils().getPSUInventoryPath(bus); |
| 164 | } |
| 165 | |
| 166 | inline std::string getVersionId(const std::string& version) |
| 167 | { |
| 168 | return getUtils().getVersionId(version); |
| 169 | } |
| 170 | |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 171 | inline std::string getVersion(const std::string& inventoryPath) |
| 172 | { |
| 173 | return getUtils().getVersion(inventoryPath); |
| 174 | } |
| 175 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 176 | template <typename T> |
| 177 | T getProperty(sdbusplus::bus::bus& bus, const char* service, const char* path, |
| 178 | const char* interface, const char* propertyName) |
| 179 | { |
| 180 | return getUtils().getProperty<T>(bus, service, path, interface, |
| 181 | propertyName); |
| 182 | } |
| 183 | |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 184 | } // namespace utils |