blob: 86ee5ddb4b5f92cff5dc01451fb32a3ae4a84da6 [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
40/** @brief The template function to get property from the requested dbus path
41 *
42 * @param[in] bus - The Dbus bus object
43 * @param[in] service - The Dbus service name
44 * @param[in] path - The Dbus object path
45 * @param[in] interface - The Dbus interface
46 * @param[in] propertyName - The property name to get
47 *
48 * @return The value of the property
49 */
50template <typename T>
51T getProperty(sdbusplus::bus::bus& bus, const char* service, const char* path,
Lei YUf77189f2019-08-07 14:26:30 +080052 const char* interface, const char* propertyName);
Lei YUad90ad52019-08-06 11:19:28 +080053
54/**
55 * @brief Calculate the version id from the version string.
56 *
57 * @details The version id is a unique 8 hexadecimal digit id
58 * calculated from the version string.
59 *
60 * @param[in] version - The image version string (e.g. v1.99.10-19).
61 *
62 * @return The id.
63 */
64std::string getVersionId(const std::string& version);
65
Lei YU5f3584d2019-08-27 16:28:53 +080066/** @brief Get version of PSU specified by the inventory path
67 *
68 * @param[in] inventoryPath - The PSU inventory object path
69 *
70 * @return The version string, or empry string if it fails to get the version
71 */
72std::string getVersion(const std::string& inventoryPath);
73
Lei YUf77189f2019-08-07 14:26:30 +080074/**
75 * @brief The interface for utils
76 */
77class UtilsInterface
78{
79 public:
80 // For now the code needs to get property for Present and Version
81 using PropertyType = sdbusplus::message::variant<std::string, bool>;
82
83 virtual ~UtilsInterface() = default;
84
85 virtual std::vector<std::string>
86 getPSUInventoryPath(sdbusplus::bus::bus& bus) const = 0;
87
88 virtual std::string getService(sdbusplus::bus::bus& bus, const char* path,
89 const char* interface) const = 0;
90
91 virtual std::string getVersionId(const std::string& version) const = 0;
92
Lei YU5f3584d2019-08-27 16:28:53 +080093 virtual std::string getVersion(const std::string& inventoryPath) const = 0;
94
Lei YUf77189f2019-08-07 14:26:30 +080095 virtual any getPropertyImpl(sdbusplus::bus::bus& bus, const char* service,
96 const char* path, const char* interface,
97 const char* propertyName) const = 0;
98
99 template <typename T>
100 T getProperty(sdbusplus::bus::bus& bus, const char* service,
101 const char* path, const char* interface,
102 const char* propertyName) const
103 {
104 any result =
105 getPropertyImpl(bus, service, path, interface, propertyName);
106 auto value = any_cast<PropertyType>(result);
107 return sdbusplus::message::variant_ns::get<T>(value);
108 }
109};
110
111class Utils : public UtilsInterface
112{
113 public:
114 std::vector<std::string>
115 getPSUInventoryPath(sdbusplus::bus::bus& bus) const override;
116
117 std::string getService(sdbusplus::bus::bus& bus, const char* path,
118 const char* interface) const override;
119
120 std::string getVersionId(const std::string& version) const override;
121
Lei YU5f3584d2019-08-27 16:28:53 +0800122 std::string getVersion(const std::string& inventoryPath) const override;
123
Lei YUf77189f2019-08-07 14:26:30 +0800124 any getPropertyImpl(sdbusplus::bus::bus& bus, const char* service,
125 const char* path, const char* interface,
126 const char* propertyName) const override;
127};
128
129inline std::string getService(sdbusplus::bus::bus& bus, const char* path,
130 const char* interface)
131{
132 return getUtils().getService(bus, path, interface);
133}
134
135inline std::vector<std::string> getPSUInventoryPath(sdbusplus::bus::bus& bus)
136{
137 return getUtils().getPSUInventoryPath(bus);
138}
139
140inline std::string getVersionId(const std::string& version)
141{
142 return getUtils().getVersionId(version);
143}
144
Lei YU5f3584d2019-08-27 16:28:53 +0800145inline std::string getVersion(const std::string& inventoryPath)
146{
147 return getUtils().getVersion(inventoryPath);
148}
149
Lei YUf77189f2019-08-07 14:26:30 +0800150template <typename T>
151T getProperty(sdbusplus::bus::bus& bus, const char* service, const char* path,
152 const char* interface, const char* propertyName)
153{
154 return getUtils().getProperty<T>(bus, service, path, interface,
155 propertyName);
156}
157
Lei YU5e0dcb32019-08-02 18:04:34 +0800158} // namespace utils