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 | */ |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 28 | std::vector<std::string> getPSUInventoryPath(sdbusplus::bus_t& bus); |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 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 | */ |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 38 | std::string getService(sdbusplus::bus_t& bus, const char* path, |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 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 | */ |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 49 | std::vector<std::string> getServices(sdbusplus::bus_t& bus, const char* path, |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 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> |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 63 | T getProperty(sdbusplus::bus_t& 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> |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 116 | getPSUInventoryPath(sdbusplus::bus_t& bus) const = 0; |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 117 | |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 118 | virtual std::string getService(sdbusplus::bus_t& bus, const char* path, |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 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> |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 122 | getServices(sdbusplus::bus_t& bus, const char* path, |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 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 | |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 135 | virtual any getPropertyImpl(sdbusplus::bus_t& bus, const char* service, |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 136 | const char* path, const char* interface, |
| 137 | const char* propertyName) const = 0; |
| 138 | |
| 139 | template <typename T> |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 140 | T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path, |
| 141 | const char* interface, const char* propertyName) const |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 142 | { |
| 143 | any result = |
| 144 | getPropertyImpl(bus, service, path, interface, propertyName); |
| 145 | auto value = any_cast<PropertyType>(result); |
Patrick Williams | 2e9a3b2 | 2020-05-13 12:23:54 -0500 | [diff] [blame] | 146 | return std::get<T>(value); |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 147 | } |
| 148 | }; |
| 149 | |
| 150 | class Utils : public UtilsInterface |
| 151 | { |
| 152 | public: |
| 153 | std::vector<std::string> |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 154 | getPSUInventoryPath(sdbusplus::bus_t& bus) const override; |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 155 | |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 156 | std::string getService(sdbusplus::bus_t& bus, const char* path, |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 157 | const char* interface) const override; |
| 158 | |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 159 | std::vector<std::string> getServices(sdbusplus::bus_t& bus, |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 160 | const char* path, |
| 161 | const char* interface) const override; |
| 162 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 163 | std::string getVersionId(const std::string& version) const override; |
| 164 | |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 165 | std::string getVersion(const std::string& inventoryPath) const override; |
| 166 | |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 167 | std::string |
| 168 | getLatestVersion(const std::set<std::string>& versions) const override; |
| 169 | |
Lei YU | 4b9ac39 | 2019-10-12 11:02:26 +0800 | [diff] [blame] | 170 | bool isAssociated(const std::string& psuInventoryPath, |
| 171 | const AssociationList& assocs) const override; |
| 172 | |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 173 | any getPropertyImpl(sdbusplus::bus_t& bus, const char* service, |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 174 | const char* path, const char* interface, |
| 175 | const char* propertyName) const override; |
| 176 | }; |
| 177 | |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 178 | inline std::string getService(sdbusplus::bus_t& bus, const char* path, |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 179 | const char* interface) |
| 180 | { |
| 181 | return getUtils().getService(bus, path, interface); |
| 182 | } |
| 183 | |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 184 | inline std::vector<std::string> |
| 185 | getServices(sdbusplus::bus_t& bus, const char* path, const char* interface) |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 186 | { |
| 187 | return getUtils().getServices(bus, path, interface); |
| 188 | } |
| 189 | |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 190 | inline std::vector<std::string> getPSUInventoryPath(sdbusplus::bus_t& bus) |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 191 | { |
| 192 | return getUtils().getPSUInventoryPath(bus); |
| 193 | } |
| 194 | |
| 195 | inline std::string getVersionId(const std::string& version) |
| 196 | { |
| 197 | return getUtils().getVersionId(version); |
| 198 | } |
| 199 | |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 200 | inline std::string getVersion(const std::string& inventoryPath) |
| 201 | { |
| 202 | return getUtils().getVersion(inventoryPath); |
| 203 | } |
| 204 | |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 205 | inline std::string getLatestVersion(const std::set<std::string>& versions) |
| 206 | { |
| 207 | return getUtils().getLatestVersion(versions); |
| 208 | } |
| 209 | |
Lei YU | 4b9ac39 | 2019-10-12 11:02:26 +0800 | [diff] [blame] | 210 | inline bool isAssociated(const std::string& psuInventoryPath, |
| 211 | const AssociationList& assocs) |
| 212 | { |
| 213 | return getUtils().isAssociated(psuInventoryPath, assocs); |
| 214 | } |
| 215 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 216 | template <typename T> |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 217 | T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path, |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 218 | const char* interface, const char* propertyName) |
| 219 | { |
| 220 | return getUtils().getProperty<T>(bus, service, path, interface, |
| 221 | propertyName); |
| 222 | } |
| 223 | |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 224 | } // namespace utils |