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