blob: 2ce64f4d22627dac728522cf54c3dc7fa80f7d59 [file] [log] [blame]
Lei YU5e0dcb32019-08-02 18:04:34 +08001#pragma once
2
Lei YU4b9ac392019-10-12 11:02:26 +08003#include "types.hpp"
4
Lei YU5e0dcb32019-08-02 18:04:34 +08005#include <sdbusplus/bus.hpp>
Patrick Williams5670b182023-05-10 07:50:50 -05006
7#include <any>
Lei YU65207482019-10-11 16:39:36 +08008#include <set>
Lei YU5e0dcb32019-08-02 18:04:34 +08009#include <string>
10#include <vector>
11
12namespace utils
13{
14
Lei YUf77189f2019-08-07 14:26:30 +080015class UtilsInterface;
16
Lei YU4b9ac392019-10-12 11:02:26 +080017using AssociationList = phosphor::software::updater::AssociationList;
Patrick Williamsb5f9b822022-06-16 17:26:17 -050018using std::any;
19using std::any_cast;
Lei YUf77189f2019-08-07 14:26:30 +080020
21/**
22 * @brief Get the implementation of UtilsInterface
23 */
24const UtilsInterface& getUtils();
Lei YUad90ad52019-08-06 11:19:28 +080025
Lei YU5e0dcb32019-08-02 18:04:34 +080026/**
Shawn McCarney487e2e12024-11-25 17:19:46 -060027 * @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 YU5e0dcb32019-08-02 18:04:34 +080035 */
Shawn McCarneyd57bd2f2024-12-02 18:40:28 -060036std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus_t& bus);
Lei YU5e0dcb32019-08-02 18:04:34 +080037
Lei YUad90ad52019-08-06 11:19:28 +080038/** @brief Get service name from object path and interface
39 *
Shawn McCarney487e2e12024-11-25 17:19:46 -060040 * @details Throws an exception if an error occurs or no service name was
41 * found.
42 *
Lei YUad90ad52019-08-06 11:19:28 +080043 * @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 Williams374fae52022-07-22 19:26:55 -050049std::string getService(sdbusplus::bus_t& bus, const char* path,
Lei YUad90ad52019-08-06 11:19:28 +080050 const char* interface);
51
Shawn McCarney487e2e12024-11-25 17:19:46 -060052/** @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 YUd0bbfa92019-09-11 16:10:54 +080056 *
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 McCarney487e2e12024-11-25 17:19:46 -060061 * @return The name of the services (if any)
Lei YUd0bbfa92019-09-11 16:10:54 +080062 */
Patrick Williams374fae52022-07-22 19:26:55 -050063std::vector<std::string> getServices(sdbusplus::bus_t& bus, const char* path,
Lei YUd0bbfa92019-09-11 16:10:54 +080064 const char* interface);
65
Lei YUad90ad52019-08-06 11:19:28 +080066/** @brief The template function to get property from the requested dbus path
67 *
Shawn McCarney487e2e12024-11-25 17:19:46 -060068 * @details Throws an exception if an error occurs
69 *
Lei YUad90ad52019-08-06 11:19:28 +080070 * @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 */
78template <typename T>
Patrick Williams374fae52022-07-22 19:26:55 -050079T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path,
Lei YUf77189f2019-08-07 14:26:30 +080080 const char* interface, const char* propertyName);
Lei YUad90ad52019-08-06 11:19:28 +080081
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 */
92std::string getVersionId(const std::string& version);
93
Lei YU5f3584d2019-08-27 16:28:53 +080094/** @brief Get version of PSU specified by the inventory path
95 *
96 * @param[in] inventoryPath - The PSU inventory object path
97 *
Shawn McCarney783406e2024-11-17 21:49:37 -060098 * @return The version string, or empty string if it fails to get the version
Lei YU5f3584d2019-08-27 16:28:53 +080099 */
100std::string getVersion(const std::string& inventoryPath);
101
Shawn McCarney783406e2024-11-17 21:49:37 -0600102/** @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 */
108std::string getModel(const std::string& inventoryPath);
109
Lei YU65207482019-10-11 16:39:36 +0800110/** @brief Get latest version from the PSU versions
111 *
112 * @param[in] versions - The list of the versions
113 *
Shawn McCarney487e2e12024-11-25 17:19:46 -0600114 * @return The latest version string, or empty string if it fails to get the
115 * latest version
Lei YU65207482019-10-11 16:39:36 +0800116 */
117std::string getLatestVersion(const std::set<std::string>& versions);
118
Lei YU4b9ac392019-10-12 11:02:26 +0800119/** @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 */
126bool isAssociated(const std::string& psuInventoryPath,
127 const AssociationList& assocs);
128
Lei YUf77189f2019-08-07 14:26:30 +0800129/**
130 * @brief The interface for utils
131 */
132class UtilsInterface
133{
134 public:
George Liu66a54ad2024-08-23 13:53:39 +0800135 UtilsInterface() = default;
136 UtilsInterface(const UtilsInterface&) = delete;
137 UtilsInterface& operator=(const UtilsInterface&) = delete;
138 UtilsInterface(UtilsInterface&&) = delete;
139 UtilsInterface& operator=(UtilsInterface&&) = delete;
140
Lei YUf77189f2019-08-07 14:26:30 +0800141 // For now the code needs to get property for Present and Version
Patrick Williamsbfa8d162020-05-13 18:00:02 -0500142 using PropertyType = std::variant<std::string, bool>;
Lei YUf77189f2019-08-07 14:26:30 +0800143
144 virtual ~UtilsInterface() = default;
145
146 virtual std::vector<std::string>
Shawn McCarneyd57bd2f2024-12-02 18:40:28 -0600147 getPSUInventoryPaths(sdbusplus::bus_t& bus) const = 0;
Lei YUf77189f2019-08-07 14:26:30 +0800148
Patrick Williams374fae52022-07-22 19:26:55 -0500149 virtual std::string getService(sdbusplus::bus_t& bus, const char* path,
Lei YUf77189f2019-08-07 14:26:30 +0800150 const char* interface) const = 0;
151
Lei YUd0bbfa92019-09-11 16:10:54 +0800152 virtual std::vector<std::string>
Patrick Williams374fae52022-07-22 19:26:55 -0500153 getServices(sdbusplus::bus_t& bus, const char* path,
Lei YUd0bbfa92019-09-11 16:10:54 +0800154 const char* interface) const = 0;
155
Lei YUf77189f2019-08-07 14:26:30 +0800156 virtual std::string getVersionId(const std::string& version) const = 0;
157
Lei YU5f3584d2019-08-27 16:28:53 +0800158 virtual std::string getVersion(const std::string& inventoryPath) const = 0;
159
Shawn McCarney783406e2024-11-17 21:49:37 -0600160 virtual std::string getModel(const std::string& inventoryPath) const = 0;
161
Lei YU65207482019-10-11 16:39:36 +0800162 virtual std::string
163 getLatestVersion(const std::set<std::string>& versions) const = 0;
164
Lei YU4b9ac392019-10-12 11:02:26 +0800165 virtual bool isAssociated(const std::string& psuInventoryPath,
166 const AssociationList& assocs) const = 0;
167
Patrick Williams374fae52022-07-22 19:26:55 -0500168 virtual any getPropertyImpl(sdbusplus::bus_t& bus, const char* service,
Lei YUf77189f2019-08-07 14:26:30 +0800169 const char* path, const char* interface,
170 const char* propertyName) const = 0;
171
172 template <typename T>
Patrick Williams374fae52022-07-22 19:26:55 -0500173 T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path,
174 const char* interface, const char* propertyName) const
Lei YUf77189f2019-08-07 14:26:30 +0800175 {
Patrick Williamsbab5ed92024-08-16 15:20:54 -0400176 any result =
177 getPropertyImpl(bus, service, path, interface, propertyName);
Lei YUf77189f2019-08-07 14:26:30 +0800178 auto value = any_cast<PropertyType>(result);
Patrick Williams2e9a3b22020-05-13 12:23:54 -0500179 return std::get<T>(value);
Lei YUf77189f2019-08-07 14:26:30 +0800180 }
181};
182
183class Utils : public UtilsInterface
184{
185 public:
186 std::vector<std::string>
Shawn McCarneyd57bd2f2024-12-02 18:40:28 -0600187 getPSUInventoryPaths(sdbusplus::bus_t& bus) const override;
Lei YUf77189f2019-08-07 14:26:30 +0800188
Patrick Williams374fae52022-07-22 19:26:55 -0500189 std::string getService(sdbusplus::bus_t& bus, const char* path,
Lei YUf77189f2019-08-07 14:26:30 +0800190 const char* interface) const override;
191
Patrick Williams374fae52022-07-22 19:26:55 -0500192 std::vector<std::string> getServices(sdbusplus::bus_t& bus,
Lei YUd0bbfa92019-09-11 16:10:54 +0800193 const char* path,
194 const char* interface) const override;
195
Lei YUf77189f2019-08-07 14:26:30 +0800196 std::string getVersionId(const std::string& version) const override;
197
Lei YU5f3584d2019-08-27 16:28:53 +0800198 std::string getVersion(const std::string& inventoryPath) const override;
199
Shawn McCarney783406e2024-11-17 21:49:37 -0600200 std::string getModel(const std::string& inventoryPath) const override;
201
Lei YU65207482019-10-11 16:39:36 +0800202 std::string
203 getLatestVersion(const std::set<std::string>& versions) const override;
204
Lei YU4b9ac392019-10-12 11:02:26 +0800205 bool isAssociated(const std::string& psuInventoryPath,
206 const AssociationList& assocs) const override;
207
Patrick Williams374fae52022-07-22 19:26:55 -0500208 any getPropertyImpl(sdbusplus::bus_t& bus, const char* service,
Lei YUf77189f2019-08-07 14:26:30 +0800209 const char* path, const char* interface,
210 const char* propertyName) const override;
211};
212
Patrick Williams374fae52022-07-22 19:26:55 -0500213inline std::string getService(sdbusplus::bus_t& bus, const char* path,
Lei YUf77189f2019-08-07 14:26:30 +0800214 const char* interface)
215{
216 return getUtils().getService(bus, path, interface);
217}
218
Patrick Williams374fae52022-07-22 19:26:55 -0500219inline std::vector<std::string>
220 getServices(sdbusplus::bus_t& bus, const char* path, const char* interface)
Lei YUd0bbfa92019-09-11 16:10:54 +0800221{
222 return getUtils().getServices(bus, path, interface);
223}
224
Shawn McCarneyd57bd2f2024-12-02 18:40:28 -0600225inline std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus_t& bus)
Lei YUf77189f2019-08-07 14:26:30 +0800226{
Shawn McCarneyd57bd2f2024-12-02 18:40:28 -0600227 return getUtils().getPSUInventoryPaths(bus);
Lei YUf77189f2019-08-07 14:26:30 +0800228}
229
230inline std::string getVersionId(const std::string& version)
231{
232 return getUtils().getVersionId(version);
233}
234
Lei YU5f3584d2019-08-27 16:28:53 +0800235inline std::string getVersion(const std::string& inventoryPath)
236{
237 return getUtils().getVersion(inventoryPath);
238}
239
Shawn McCarney783406e2024-11-17 21:49:37 -0600240inline std::string getModel(const std::string& inventoryPath)
241{
242 return getUtils().getModel(inventoryPath);
243}
244
Lei YU65207482019-10-11 16:39:36 +0800245inline std::string getLatestVersion(const std::set<std::string>& versions)
246{
247 return getUtils().getLatestVersion(versions);
248}
249
Lei YU4b9ac392019-10-12 11:02:26 +0800250inline bool isAssociated(const std::string& psuInventoryPath,
251 const AssociationList& assocs)
252{
253 return getUtils().isAssociated(psuInventoryPath, assocs);
254}
255
Lei YUf77189f2019-08-07 14:26:30 +0800256template <typename T>
Patrick Williams374fae52022-07-22 19:26:55 -0500257T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path,
Lei YUf77189f2019-08-07 14:26:30 +0800258 const char* interface, const char* propertyName)
259{
260 return getUtils().getProperty<T>(bus, service, path, interface,
261 propertyName);
262}
263
Lei YU5e0dcb32019-08-02 18:04:34 +0800264} // namespace utils