blob: 13c7b6292edd7d8b66dba27fe835d1b8979e39bf [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
Patrick Williamsb5f9b822022-06-16 17:26:17 -05005#include <any>
Lei YU5e0dcb32019-08-02 18:04:34 +08006#include <sdbusplus/bus.hpp>
Lei YU65207482019-10-11 16:39:36 +08007#include <set>
Lei YU5e0dcb32019-08-02 18:04:34 +08008#include <string>
9#include <vector>
10
11namespace utils
12{
13
Lei YUf77189f2019-08-07 14:26:30 +080014class UtilsInterface;
15
Lei YU4b9ac392019-10-12 11:02:26 +080016using AssociationList = phosphor::software::updater::AssociationList;
Patrick Williamsb5f9b822022-06-16 17:26:17 -050017using std::any;
18using std::any_cast;
Lei YUf77189f2019-08-07 14:26:30 +080019
20/**
21 * @brief Get the implementation of UtilsInterface
22 */
23const UtilsInterface& getUtils();
Lei YUad90ad52019-08-06 11:19:28 +080024
Lei YU5e0dcb32019-08-02 18:04:34 +080025/**
26 * @brief Get PSU inventory object path from DBus
27 */
Patrick Williams374fae52022-07-22 19:26:55 -050028std::vector<std::string> getPSUInventoryPath(sdbusplus::bus_t& bus);
Lei YU5e0dcb32019-08-02 18:04:34 +080029
Lei YUad90ad52019-08-06 11:19:28 +080030/** @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 Williams374fae52022-07-22 19:26:55 -050038std::string getService(sdbusplus::bus_t& bus, const char* path,
Lei YUad90ad52019-08-06 11:19:28 +080039 const char* interface);
40
Lei YUd0bbfa92019-09-11 16:10:54 +080041/** @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 Williams374fae52022-07-22 19:26:55 -050049std::vector<std::string> getServices(sdbusplus::bus_t& bus, const char* path,
Lei YUd0bbfa92019-09-11 16:10:54 +080050 const char* interface);
51
Lei YUad90ad52019-08-06 11:19:28 +080052/** @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 */
62template <typename T>
Patrick Williams374fae52022-07-22 19:26:55 -050063T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path,
Lei YUf77189f2019-08-07 14:26:30 +080064 const char* interface, const char* propertyName);
Lei YUad90ad52019-08-06 11:19:28 +080065
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 */
76std::string getVersionId(const std::string& version);
77
Lei YU5f3584d2019-08-27 16:28:53 +080078/** @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 */
84std::string getVersion(const std::string& inventoryPath);
85
Lei YU65207482019-10-11 16:39:36 +080086/** @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 */
92std::string getLatestVersion(const std::set<std::string>& versions);
93
Lei YU4b9ac392019-10-12 11:02:26 +080094/** @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 */
101bool isAssociated(const std::string& psuInventoryPath,
102 const AssociationList& assocs);
103
Lei YUf77189f2019-08-07 14:26:30 +0800104/**
105 * @brief The interface for utils
106 */
107class UtilsInterface
108{
109 public:
110 // For now the code needs to get property for Present and Version
Patrick Williamsbfa8d162020-05-13 18:00:02 -0500111 using PropertyType = std::variant<std::string, bool>;
Lei YUf77189f2019-08-07 14:26:30 +0800112
113 virtual ~UtilsInterface() = default;
114
115 virtual std::vector<std::string>
Patrick Williams374fae52022-07-22 19:26:55 -0500116 getPSUInventoryPath(sdbusplus::bus_t& bus) const = 0;
Lei YUf77189f2019-08-07 14:26:30 +0800117
Patrick Williams374fae52022-07-22 19:26:55 -0500118 virtual std::string getService(sdbusplus::bus_t& bus, const char* path,
Lei YUf77189f2019-08-07 14:26:30 +0800119 const char* interface) const = 0;
120
Lei YUd0bbfa92019-09-11 16:10:54 +0800121 virtual std::vector<std::string>
Patrick Williams374fae52022-07-22 19:26:55 -0500122 getServices(sdbusplus::bus_t& bus, const char* path,
Lei YUd0bbfa92019-09-11 16:10:54 +0800123 const char* interface) const = 0;
124
Lei YUf77189f2019-08-07 14:26:30 +0800125 virtual std::string getVersionId(const std::string& version) const = 0;
126
Lei YU5f3584d2019-08-27 16:28:53 +0800127 virtual std::string getVersion(const std::string& inventoryPath) const = 0;
128
Lei YU65207482019-10-11 16:39:36 +0800129 virtual std::string
130 getLatestVersion(const std::set<std::string>& versions) const = 0;
131
Lei YU4b9ac392019-10-12 11:02:26 +0800132 virtual bool isAssociated(const std::string& psuInventoryPath,
133 const AssociationList& assocs) const = 0;
134
Patrick Williams374fae52022-07-22 19:26:55 -0500135 virtual any getPropertyImpl(sdbusplus::bus_t& bus, const char* service,
Lei YUf77189f2019-08-07 14:26:30 +0800136 const char* path, const char* interface,
137 const char* propertyName) const = 0;
138
139 template <typename T>
Patrick Williams374fae52022-07-22 19:26:55 -0500140 T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path,
141 const char* interface, const char* propertyName) const
Lei YUf77189f2019-08-07 14:26:30 +0800142 {
143 any result =
144 getPropertyImpl(bus, service, path, interface, propertyName);
145 auto value = any_cast<PropertyType>(result);
Patrick Williams2e9a3b22020-05-13 12:23:54 -0500146 return std::get<T>(value);
Lei YUf77189f2019-08-07 14:26:30 +0800147 }
148};
149
150class Utils : public UtilsInterface
151{
152 public:
153 std::vector<std::string>
Patrick Williams374fae52022-07-22 19:26:55 -0500154 getPSUInventoryPath(sdbusplus::bus_t& bus) const override;
Lei YUf77189f2019-08-07 14:26:30 +0800155
Patrick Williams374fae52022-07-22 19:26:55 -0500156 std::string getService(sdbusplus::bus_t& bus, const char* path,
Lei YUf77189f2019-08-07 14:26:30 +0800157 const char* interface) const override;
158
Patrick Williams374fae52022-07-22 19:26:55 -0500159 std::vector<std::string> getServices(sdbusplus::bus_t& bus,
Lei YUd0bbfa92019-09-11 16:10:54 +0800160 const char* path,
161 const char* interface) const override;
162
Lei YUf77189f2019-08-07 14:26:30 +0800163 std::string getVersionId(const std::string& version) const override;
164
Lei YU5f3584d2019-08-27 16:28:53 +0800165 std::string getVersion(const std::string& inventoryPath) const override;
166
Lei YU65207482019-10-11 16:39:36 +0800167 std::string
168 getLatestVersion(const std::set<std::string>& versions) const override;
169
Lei YU4b9ac392019-10-12 11:02:26 +0800170 bool isAssociated(const std::string& psuInventoryPath,
171 const AssociationList& assocs) const override;
172
Patrick Williams374fae52022-07-22 19:26:55 -0500173 any getPropertyImpl(sdbusplus::bus_t& bus, const char* service,
Lei YUf77189f2019-08-07 14:26:30 +0800174 const char* path, const char* interface,
175 const char* propertyName) const override;
176};
177
Patrick Williams374fae52022-07-22 19:26:55 -0500178inline std::string getService(sdbusplus::bus_t& bus, const char* path,
Lei YUf77189f2019-08-07 14:26:30 +0800179 const char* interface)
180{
181 return getUtils().getService(bus, path, interface);
182}
183
Patrick Williams374fae52022-07-22 19:26:55 -0500184inline std::vector<std::string>
185 getServices(sdbusplus::bus_t& bus, const char* path, const char* interface)
Lei YUd0bbfa92019-09-11 16:10:54 +0800186{
187 return getUtils().getServices(bus, path, interface);
188}
189
Patrick Williams374fae52022-07-22 19:26:55 -0500190inline std::vector<std::string> getPSUInventoryPath(sdbusplus::bus_t& bus)
Lei YUf77189f2019-08-07 14:26:30 +0800191{
192 return getUtils().getPSUInventoryPath(bus);
193}
194
195inline std::string getVersionId(const std::string& version)
196{
197 return getUtils().getVersionId(version);
198}
199
Lei YU5f3584d2019-08-27 16:28:53 +0800200inline std::string getVersion(const std::string& inventoryPath)
201{
202 return getUtils().getVersion(inventoryPath);
203}
204
Lei YU65207482019-10-11 16:39:36 +0800205inline std::string getLatestVersion(const std::set<std::string>& versions)
206{
207 return getUtils().getLatestVersion(versions);
208}
209
Lei YU4b9ac392019-10-12 11:02:26 +0800210inline bool isAssociated(const std::string& psuInventoryPath,
211 const AssociationList& assocs)
212{
213 return getUtils().isAssociated(psuInventoryPath, assocs);
214}
215
Lei YUf77189f2019-08-07 14:26:30 +0800216template <typename T>
Patrick Williams374fae52022-07-22 19:26:55 -0500217T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path,
Lei YUf77189f2019-08-07 14:26:30 +0800218 const char* interface, const char* propertyName)
219{
220 return getUtils().getProperty<T>(bus, service, path, interface,
221 propertyName);
222}
223
Lei YU5e0dcb32019-08-02 18:04:34 +0800224} // namespace utils