Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Lei YU | 4b9ac39 | 2019-10-12 11:02:26 +0800 | [diff] [blame] | 3 | #include "types.hpp" |
| 4 | |
Patrick Williams | b5f9b82 | 2022-06-16 17:26:17 -0500 | [diff] [blame] | 5 | #include <any> |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 6 | #include <sdbusplus/bus.hpp> |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 7 | #include <set> |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | namespace utils |
| 12 | { |
| 13 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 14 | class UtilsInterface; |
| 15 | |
Lei YU | 4b9ac39 | 2019-10-12 11:02:26 +0800 | [diff] [blame] | 16 | using AssociationList = phosphor::software::updater::AssociationList; |
Patrick Williams | b5f9b82 | 2022-06-16 17:26:17 -0500 | [diff] [blame] | 17 | using std::any; |
| 18 | using std::any_cast; |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * @brief Get the implementation of UtilsInterface |
| 22 | */ |
| 23 | const UtilsInterface& getUtils(); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 24 | |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 25 | /** |
| 26 | * @brief Get PSU inventory object path from DBus |
| 27 | */ |
| 28 | std::vector<std::string> getPSUInventoryPath(sdbusplus::bus::bus& bus); |
| 29 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 30 | /** @brief Get service name from object path and interface |
| 31 | * |
| 32 | * @param[in] bus - The Dbus bus object |
| 33 | * @param[in] path - The Dbus object path |
| 34 | * @param[in] interface - The Dbus interface |
| 35 | * |
| 36 | * @return The name of the service |
| 37 | */ |
| 38 | std::string getService(sdbusplus::bus::bus& bus, const char* path, |
| 39 | const char* interface); |
| 40 | |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 41 | /** @brief Get all the service names from object path and interface |
| 42 | * |
| 43 | * @param[in] bus - The Dbus bus object |
| 44 | * @param[in] path - The Dbus object path |
| 45 | * @param[in] interface - The Dbus interface |
| 46 | * |
| 47 | * @return The name of the services |
| 48 | */ |
| 49 | std::vector<std::string> getServices(sdbusplus::bus::bus& bus, const char* path, |
| 50 | const char* interface); |
| 51 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 52 | /** @brief The template function to get property from the requested dbus path |
| 53 | * |
| 54 | * @param[in] bus - The Dbus bus object |
| 55 | * @param[in] service - The Dbus service name |
| 56 | * @param[in] path - The Dbus object path |
| 57 | * @param[in] interface - The Dbus interface |
| 58 | * @param[in] propertyName - The property name to get |
| 59 | * |
| 60 | * @return The value of the property |
| 61 | */ |
| 62 | template <typename T> |
| 63 | T getProperty(sdbusplus::bus::bus& bus, const char* service, const char* path, |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 64 | const char* interface, const char* propertyName); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * @brief Calculate the version id from the version string. |
| 68 | * |
| 69 | * @details The version id is a unique 8 hexadecimal digit id |
| 70 | * calculated from the version string. |
| 71 | * |
| 72 | * @param[in] version - The image version string (e.g. v1.99.10-19). |
| 73 | * |
| 74 | * @return The id. |
| 75 | */ |
| 76 | std::string getVersionId(const std::string& version); |
| 77 | |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 78 | /** @brief Get version of PSU specified by the inventory path |
| 79 | * |
| 80 | * @param[in] inventoryPath - The PSU inventory object path |
| 81 | * |
| 82 | * @return The version string, or empry string if it fails to get the version |
| 83 | */ |
| 84 | std::string getVersion(const std::string& inventoryPath); |
| 85 | |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 86 | /** @brief Get latest version from the PSU versions |
| 87 | * |
| 88 | * @param[in] versions - The list of the versions |
| 89 | * |
| 90 | * @return The latest version string |
| 91 | */ |
| 92 | std::string getLatestVersion(const std::set<std::string>& versions); |
| 93 | |
Lei YU | 4b9ac39 | 2019-10-12 11:02:26 +0800 | [diff] [blame] | 94 | /** @brief Check if the PSU is associated |
| 95 | * |
| 96 | * @param[in] psuInventoryPath - The PSU inventory path |
| 97 | * @param[in] assocs - The list of associations |
| 98 | * |
| 99 | * @return true if the psu is in the association list |
| 100 | */ |
| 101 | bool isAssociated(const std::string& psuInventoryPath, |
| 102 | const AssociationList& assocs); |
| 103 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 104 | /** |
| 105 | * @brief The interface for utils |
| 106 | */ |
| 107 | class UtilsInterface |
| 108 | { |
| 109 | public: |
| 110 | // For now the code needs to get property for Present and Version |
Patrick Williams | bfa8d16 | 2020-05-13 18:00:02 -0500 | [diff] [blame] | 111 | using PropertyType = std::variant<std::string, bool>; |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 112 | |
| 113 | virtual ~UtilsInterface() = default; |
| 114 | |
| 115 | virtual std::vector<std::string> |
| 116 | getPSUInventoryPath(sdbusplus::bus::bus& bus) const = 0; |
| 117 | |
| 118 | virtual std::string getService(sdbusplus::bus::bus& bus, const char* path, |
| 119 | const char* interface) const = 0; |
| 120 | |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 121 | virtual std::vector<std::string> |
| 122 | getServices(sdbusplus::bus::bus& bus, const char* path, |
| 123 | const char* interface) const = 0; |
| 124 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 125 | virtual std::string getVersionId(const std::string& version) const = 0; |
| 126 | |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 127 | virtual std::string getVersion(const std::string& inventoryPath) const = 0; |
| 128 | |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 129 | virtual std::string |
| 130 | getLatestVersion(const std::set<std::string>& versions) const = 0; |
| 131 | |
Lei YU | 4b9ac39 | 2019-10-12 11:02:26 +0800 | [diff] [blame] | 132 | virtual bool isAssociated(const std::string& psuInventoryPath, |
| 133 | const AssociationList& assocs) const = 0; |
| 134 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 135 | virtual any getPropertyImpl(sdbusplus::bus::bus& bus, const char* service, |
| 136 | const char* path, const char* interface, |
| 137 | const char* propertyName) const = 0; |
| 138 | |
| 139 | template <typename T> |
| 140 | T getProperty(sdbusplus::bus::bus& bus, const char* service, |
| 141 | const char* path, const char* interface, |
| 142 | const char* propertyName) const |
| 143 | { |
| 144 | any result = |
| 145 | getPropertyImpl(bus, service, path, interface, propertyName); |
| 146 | auto value = any_cast<PropertyType>(result); |
Patrick Williams | 2e9a3b2 | 2020-05-13 12:23:54 -0500 | [diff] [blame] | 147 | return std::get<T>(value); |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 148 | } |
| 149 | }; |
| 150 | |
| 151 | class Utils : public UtilsInterface |
| 152 | { |
| 153 | public: |
| 154 | std::vector<std::string> |
| 155 | getPSUInventoryPath(sdbusplus::bus::bus& bus) const override; |
| 156 | |
| 157 | std::string getService(sdbusplus::bus::bus& bus, const char* path, |
| 158 | const char* interface) const override; |
| 159 | |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 160 | std::vector<std::string> getServices(sdbusplus::bus::bus& bus, |
| 161 | const char* path, |
| 162 | const char* interface) const override; |
| 163 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 164 | std::string getVersionId(const std::string& version) const override; |
| 165 | |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 166 | std::string getVersion(const std::string& inventoryPath) const override; |
| 167 | |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 168 | std::string |
| 169 | getLatestVersion(const std::set<std::string>& versions) const override; |
| 170 | |
Lei YU | 4b9ac39 | 2019-10-12 11:02:26 +0800 | [diff] [blame] | 171 | bool isAssociated(const std::string& psuInventoryPath, |
| 172 | const AssociationList& assocs) const override; |
| 173 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 174 | any getPropertyImpl(sdbusplus::bus::bus& bus, const char* service, |
| 175 | const char* path, const char* interface, |
| 176 | const char* propertyName) const override; |
| 177 | }; |
| 178 | |
| 179 | inline std::string getService(sdbusplus::bus::bus& bus, const char* path, |
| 180 | const char* interface) |
| 181 | { |
| 182 | return getUtils().getService(bus, path, interface); |
| 183 | } |
| 184 | |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 185 | inline std::vector<std::string> getServices(sdbusplus::bus::bus& bus, |
| 186 | const char* path, |
| 187 | const char* interface) |
| 188 | { |
| 189 | return getUtils().getServices(bus, path, interface); |
| 190 | } |
| 191 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 192 | inline std::vector<std::string> getPSUInventoryPath(sdbusplus::bus::bus& bus) |
| 193 | { |
| 194 | return getUtils().getPSUInventoryPath(bus); |
| 195 | } |
| 196 | |
| 197 | inline std::string getVersionId(const std::string& version) |
| 198 | { |
| 199 | return getUtils().getVersionId(version); |
| 200 | } |
| 201 | |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 202 | inline std::string getVersion(const std::string& inventoryPath) |
| 203 | { |
| 204 | return getUtils().getVersion(inventoryPath); |
| 205 | } |
| 206 | |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 207 | inline std::string getLatestVersion(const std::set<std::string>& versions) |
| 208 | { |
| 209 | return getUtils().getLatestVersion(versions); |
| 210 | } |
| 211 | |
Lei YU | 4b9ac39 | 2019-10-12 11:02:26 +0800 | [diff] [blame] | 212 | inline bool isAssociated(const std::string& psuInventoryPath, |
| 213 | const AssociationList& assocs) |
| 214 | { |
| 215 | return getUtils().isAssociated(psuInventoryPath, assocs); |
| 216 | } |
| 217 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 218 | template <typename T> |
| 219 | T getProperty(sdbusplus::bus::bus& bus, const char* service, const char* path, |
| 220 | const char* interface, const char* propertyName) |
| 221 | { |
| 222 | return getUtils().getProperty<T>(bus, service, path, interface, |
| 223 | propertyName); |
| 224 | } |
| 225 | |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 226 | } // namespace utils |