blob: 446f0247f4883d8cb6b6c86a0b3ae25ae350d042 [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/**
27 * @brief Get PSU inventory object path from DBus
28 */
Patrick Williams374fae52022-07-22 19:26:55 -050029std::vector<std::string> getPSUInventoryPath(sdbusplus::bus_t& bus);
Lei YU5e0dcb32019-08-02 18:04:34 +080030
Lei YUad90ad52019-08-06 11:19:28 +080031/** @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 Williams374fae52022-07-22 19:26:55 -050039std::string getService(sdbusplus::bus_t& bus, const char* path,
Lei YUad90ad52019-08-06 11:19:28 +080040 const char* interface);
41
Lei YUd0bbfa92019-09-11 16:10:54 +080042/** @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 Williams374fae52022-07-22 19:26:55 -050050std::vector<std::string> getServices(sdbusplus::bus_t& bus, const char* path,
Lei YUd0bbfa92019-09-11 16:10:54 +080051 const char* interface);
52
Lei YUad90ad52019-08-06 11:19:28 +080053/** @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 */
63template <typename T>
Patrick Williams374fae52022-07-22 19:26:55 -050064T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path,
Lei YUf77189f2019-08-07 14:26:30 +080065 const char* interface, const char* propertyName);
Lei YUad90ad52019-08-06 11:19:28 +080066
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 */
77std::string getVersionId(const std::string& version);
78
Lei YU5f3584d2019-08-27 16:28:53 +080079/** @brief Get version of PSU specified by the inventory path
80 *
81 * @param[in] inventoryPath - The PSU inventory object path
82 *
83 * @return The version string, or empry string if it fails to get the version
84 */
85std::string getVersion(const std::string& inventoryPath);
86
Lei YU65207482019-10-11 16:39:36 +080087/** @brief Get latest version from the PSU versions
88 *
89 * @param[in] versions - The list of the versions
90 *
91 * @return The latest version string
92 */
93std::string getLatestVersion(const std::set<std::string>& versions);
94
Lei YU4b9ac392019-10-12 11:02:26 +080095/** @brief Check if the PSU is associated
96 *
97 * @param[in] psuInventoryPath - The PSU inventory path
98 * @param[in] assocs - The list of associations
99 *
100 * @return true if the psu is in the association list
101 */
102bool isAssociated(const std::string& psuInventoryPath,
103 const AssociationList& assocs);
104
Lei YUf77189f2019-08-07 14:26:30 +0800105/**
106 * @brief The interface for utils
107 */
108class UtilsInterface
109{
110 public:
111 // For now the code needs to get property for Present and Version
Patrick Williamsbfa8d162020-05-13 18:00:02 -0500112 using PropertyType = std::variant<std::string, bool>;
Lei YUf77189f2019-08-07 14:26:30 +0800113
114 virtual ~UtilsInterface() = default;
115
116 virtual std::vector<std::string>
Patrick Williams374fae52022-07-22 19:26:55 -0500117 getPSUInventoryPath(sdbusplus::bus_t& bus) const = 0;
Lei YUf77189f2019-08-07 14:26:30 +0800118
Patrick Williams374fae52022-07-22 19:26:55 -0500119 virtual std::string getService(sdbusplus::bus_t& bus, const char* path,
Lei YUf77189f2019-08-07 14:26:30 +0800120 const char* interface) const = 0;
121
Lei YUd0bbfa92019-09-11 16:10:54 +0800122 virtual std::vector<std::string>
Patrick Williams374fae52022-07-22 19:26:55 -0500123 getServices(sdbusplus::bus_t& bus, const char* path,
Lei YUd0bbfa92019-09-11 16:10:54 +0800124 const char* interface) const = 0;
125
Lei YUf77189f2019-08-07 14:26:30 +0800126 virtual std::string getVersionId(const std::string& version) const = 0;
127
Lei YU5f3584d2019-08-27 16:28:53 +0800128 virtual std::string getVersion(const std::string& inventoryPath) const = 0;
129
Lei YU65207482019-10-11 16:39:36 +0800130 virtual std::string
131 getLatestVersion(const std::set<std::string>& versions) const = 0;
132
Lei YU4b9ac392019-10-12 11:02:26 +0800133 virtual bool isAssociated(const std::string& psuInventoryPath,
134 const AssociationList& assocs) const = 0;
135
Patrick Williams374fae52022-07-22 19:26:55 -0500136 virtual any getPropertyImpl(sdbusplus::bus_t& bus, const char* service,
Lei YUf77189f2019-08-07 14:26:30 +0800137 const char* path, const char* interface,
138 const char* propertyName) const = 0;
139
140 template <typename T>
Patrick Williams374fae52022-07-22 19:26:55 -0500141 T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path,
142 const char* interface, const char* propertyName) const
Lei YUf77189f2019-08-07 14:26:30 +0800143 {
Patrick Williams5670b182023-05-10 07:50:50 -0500144 any result = getPropertyImpl(bus, service, path, interface,
145 propertyName);
Lei YUf77189f2019-08-07 14:26:30 +0800146 auto value = any_cast<PropertyType>(result);
Patrick Williams2e9a3b22020-05-13 12:23:54 -0500147 return std::get<T>(value);
Lei YUf77189f2019-08-07 14:26:30 +0800148 }
149};
150
151class Utils : public UtilsInterface
152{
153 public:
154 std::vector<std::string>
Patrick Williams374fae52022-07-22 19:26:55 -0500155 getPSUInventoryPath(sdbusplus::bus_t& bus) const override;
Lei YUf77189f2019-08-07 14:26:30 +0800156
Patrick Williams374fae52022-07-22 19:26:55 -0500157 std::string getService(sdbusplus::bus_t& bus, const char* path,
Lei YUf77189f2019-08-07 14:26:30 +0800158 const char* interface) const override;
159
Patrick Williams374fae52022-07-22 19:26:55 -0500160 std::vector<std::string> getServices(sdbusplus::bus_t& bus,
Lei YUd0bbfa92019-09-11 16:10:54 +0800161 const char* path,
162 const char* interface) const override;
163
Lei YUf77189f2019-08-07 14:26:30 +0800164 std::string getVersionId(const std::string& version) const override;
165
Lei YU5f3584d2019-08-27 16:28:53 +0800166 std::string getVersion(const std::string& inventoryPath) const override;
167
Lei YU65207482019-10-11 16:39:36 +0800168 std::string
169 getLatestVersion(const std::set<std::string>& versions) const override;
170
Lei YU4b9ac392019-10-12 11:02:26 +0800171 bool isAssociated(const std::string& psuInventoryPath,
172 const AssociationList& assocs) const override;
173
Patrick Williams374fae52022-07-22 19:26:55 -0500174 any getPropertyImpl(sdbusplus::bus_t& bus, const char* service,
Lei YUf77189f2019-08-07 14:26:30 +0800175 const char* path, const char* interface,
176 const char* propertyName) const override;
177};
178
Patrick Williams374fae52022-07-22 19:26:55 -0500179inline std::string getService(sdbusplus::bus_t& bus, const char* path,
Lei YUf77189f2019-08-07 14:26:30 +0800180 const char* interface)
181{
182 return getUtils().getService(bus, path, interface);
183}
184
Patrick Williams374fae52022-07-22 19:26:55 -0500185inline std::vector<std::string>
186 getServices(sdbusplus::bus_t& bus, const char* path, const char* interface)
Lei YUd0bbfa92019-09-11 16:10:54 +0800187{
188 return getUtils().getServices(bus, path, interface);
189}
190
Patrick Williams374fae52022-07-22 19:26:55 -0500191inline std::vector<std::string> getPSUInventoryPath(sdbusplus::bus_t& bus)
Lei YUf77189f2019-08-07 14:26:30 +0800192{
193 return getUtils().getPSUInventoryPath(bus);
194}
195
196inline std::string getVersionId(const std::string& version)
197{
198 return getUtils().getVersionId(version);
199}
200
Lei YU5f3584d2019-08-27 16:28:53 +0800201inline std::string getVersion(const std::string& inventoryPath)
202{
203 return getUtils().getVersion(inventoryPath);
204}
205
Lei YU65207482019-10-11 16:39:36 +0800206inline std::string getLatestVersion(const std::set<std::string>& versions)
207{
208 return getUtils().getLatestVersion(versions);
209}
210
Lei YU4b9ac392019-10-12 11:02:26 +0800211inline bool isAssociated(const std::string& psuInventoryPath,
212 const AssociationList& assocs)
213{
214 return getUtils().isAssociated(psuInventoryPath, assocs);
215}
216
Lei YUf77189f2019-08-07 14:26:30 +0800217template <typename T>
Patrick Williams374fae52022-07-22 19:26:55 -0500218T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path,
Lei YUf77189f2019-08-07 14:26:30 +0800219 const char* interface, const char* propertyName)
220{
221 return getUtils().getProperty<T>(bus, service, path, interface,
222 propertyName);
223}
224
Lei YU5e0dcb32019-08-02 18:04:34 +0800225} // namespace utils