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