blob: fe376a8431bfcbad2b4ed20faedd4f1fbc1b772a [file] [log] [blame]
Lei YU5e0dcb32019-08-02 18:04:34 +08001#pragma once
2
Lei YUf77189f2019-08-07 14:26:30 +08003#include <experimental/any>
Lei YU5e0dcb32019-08-02 18:04:34 +08004#include <sdbusplus/bus.hpp>
5#include <string>
6#include <vector>
7
8namespace utils
9{
10
Lei YUf77189f2019-08-07 14:26:30 +080011class UtilsInterface;
12
13// Due to a libstdc++ bug, we got compile error using std::any with gmock.
14// A temporary workaround is to use std::experimental::any.
15// See details in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90415
16using std::experimental::any;
17using std::experimental::any_cast;
18
19/**
20 * @brief Get the implementation of UtilsInterface
21 */
22const UtilsInterface& getUtils();
Lei YUad90ad52019-08-06 11:19:28 +080023
Lei YU5e0dcb32019-08-02 18:04:34 +080024/**
25 * @brief Get PSU inventory object path from DBus
26 */
27std::vector<std::string> getPSUInventoryPath(sdbusplus::bus::bus& bus);
28
Lei YUad90ad52019-08-06 11:19:28 +080029/** @brief Get service name from object path and interface
30 *
31 * @param[in] bus - The Dbus bus object
32 * @param[in] path - The Dbus object path
33 * @param[in] interface - The Dbus interface
34 *
35 * @return The name of the service
36 */
37std::string getService(sdbusplus::bus::bus& bus, const char* path,
38 const char* interface);
39
Lei YUd0bbfa92019-09-11 16:10:54 +080040/** @brief Get all the service names from object path and interface
41 *
42 * @param[in] bus - The Dbus bus object
43 * @param[in] path - The Dbus object path
44 * @param[in] interface - The Dbus interface
45 *
46 * @return The name of the services
47 */
48std::vector<std::string> getServices(sdbusplus::bus::bus& bus, const char* path,
49 const char* interface);
50
Lei YUad90ad52019-08-06 11:19:28 +080051/** @brief The template function to get property from the requested dbus path
52 *
53 * @param[in] bus - The Dbus bus object
54 * @param[in] service - The Dbus service name
55 * @param[in] path - The Dbus object path
56 * @param[in] interface - The Dbus interface
57 * @param[in] propertyName - The property name to get
58 *
59 * @return The value of the property
60 */
61template <typename T>
62T getProperty(sdbusplus::bus::bus& bus, const char* service, const char* path,
Lei YUf77189f2019-08-07 14:26:30 +080063 const char* interface, const char* propertyName);
Lei YUad90ad52019-08-06 11:19:28 +080064
65/**
66 * @brief Calculate the version id from the version string.
67 *
68 * @details The version id is a unique 8 hexadecimal digit id
69 * calculated from the version string.
70 *
71 * @param[in] version - The image version string (e.g. v1.99.10-19).
72 *
73 * @return The id.
74 */
75std::string getVersionId(const std::string& version);
76
Lei YU5f3584d2019-08-27 16:28:53 +080077/** @brief Get version of PSU specified by the inventory path
78 *
79 * @param[in] inventoryPath - The PSU inventory object path
80 *
81 * @return The version string, or empry string if it fails to get the version
82 */
83std::string getVersion(const std::string& inventoryPath);
84
Lei YUf77189f2019-08-07 14:26:30 +080085/**
86 * @brief The interface for utils
87 */
88class UtilsInterface
89{
90 public:
91 // For now the code needs to get property for Present and Version
92 using PropertyType = sdbusplus::message::variant<std::string, bool>;
93
94 virtual ~UtilsInterface() = default;
95
96 virtual std::vector<std::string>
97 getPSUInventoryPath(sdbusplus::bus::bus& bus) const = 0;
98
99 virtual std::string getService(sdbusplus::bus::bus& bus, const char* path,
100 const char* interface) const = 0;
101
Lei YUd0bbfa92019-09-11 16:10:54 +0800102 virtual std::vector<std::string>
103 getServices(sdbusplus::bus::bus& bus, const char* path,
104 const char* interface) const = 0;
105
Lei YUf77189f2019-08-07 14:26:30 +0800106 virtual std::string getVersionId(const std::string& version) const = 0;
107
Lei YU5f3584d2019-08-27 16:28:53 +0800108 virtual std::string getVersion(const std::string& inventoryPath) const = 0;
109
Lei YUf77189f2019-08-07 14:26:30 +0800110 virtual any getPropertyImpl(sdbusplus::bus::bus& bus, const char* service,
111 const char* path, const char* interface,
112 const char* propertyName) const = 0;
113
114 template <typename T>
115 T getProperty(sdbusplus::bus::bus& bus, const char* service,
116 const char* path, const char* interface,
117 const char* propertyName) const
118 {
119 any result =
120 getPropertyImpl(bus, service, path, interface, propertyName);
121 auto value = any_cast<PropertyType>(result);
122 return sdbusplus::message::variant_ns::get<T>(value);
123 }
124};
125
126class Utils : public UtilsInterface
127{
128 public:
129 std::vector<std::string>
130 getPSUInventoryPath(sdbusplus::bus::bus& bus) const override;
131
132 std::string getService(sdbusplus::bus::bus& bus, const char* path,
133 const char* interface) const override;
134
Lei YUd0bbfa92019-09-11 16:10:54 +0800135 std::vector<std::string> getServices(sdbusplus::bus::bus& bus,
136 const char* path,
137 const char* interface) const override;
138
Lei YUf77189f2019-08-07 14:26:30 +0800139 std::string getVersionId(const std::string& version) const override;
140
Lei YU5f3584d2019-08-27 16:28:53 +0800141 std::string getVersion(const std::string& inventoryPath) const override;
142
Lei YUf77189f2019-08-07 14:26:30 +0800143 any getPropertyImpl(sdbusplus::bus::bus& bus, const char* service,
144 const char* path, const char* interface,
145 const char* propertyName) const override;
146};
147
148inline std::string getService(sdbusplus::bus::bus& bus, const char* path,
149 const char* interface)
150{
151 return getUtils().getService(bus, path, interface);
152}
153
Lei YUd0bbfa92019-09-11 16:10:54 +0800154inline std::vector<std::string> getServices(sdbusplus::bus::bus& bus,
155 const char* path,
156 const char* interface)
157{
158 return getUtils().getServices(bus, path, interface);
159}
160
Lei YUf77189f2019-08-07 14:26:30 +0800161inline std::vector<std::string> getPSUInventoryPath(sdbusplus::bus::bus& bus)
162{
163 return getUtils().getPSUInventoryPath(bus);
164}
165
166inline std::string getVersionId(const std::string& version)
167{
168 return getUtils().getVersionId(version);
169}
170
Lei YU5f3584d2019-08-27 16:28:53 +0800171inline std::string getVersion(const std::string& inventoryPath)
172{
173 return getUtils().getVersion(inventoryPath);
174}
175
Lei YUf77189f2019-08-07 14:26:30 +0800176template <typename T>
177T getProperty(sdbusplus::bus::bus& bus, const char* service, const char* path,
178 const char* interface, const char* propertyName)
179{
180 return getUtils().getProperty<T>(bus, service, path, interface,
181 propertyName);
182}
183
Lei YU5e0dcb32019-08-02 18:04:34 +0800184} // namespace utils