blob: dd1317f430413677aff114b75b5c75ae34235b7d [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 YUf77189f2019-08-07 14:26:30 +080066/**
67 * @brief The interface for utils
68 */
69class UtilsInterface
70{
71 public:
72 // For now the code needs to get property for Present and Version
73 using PropertyType = sdbusplus::message::variant<std::string, bool>;
74
75 virtual ~UtilsInterface() = default;
76
77 virtual std::vector<std::string>
78 getPSUInventoryPath(sdbusplus::bus::bus& bus) const = 0;
79
80 virtual std::string getService(sdbusplus::bus::bus& bus, const char* path,
81 const char* interface) const = 0;
82
83 virtual std::string getVersionId(const std::string& version) const = 0;
84
85 virtual any getPropertyImpl(sdbusplus::bus::bus& bus, const char* service,
86 const char* path, const char* interface,
87 const char* propertyName) const = 0;
88
89 template <typename T>
90 T getProperty(sdbusplus::bus::bus& bus, const char* service,
91 const char* path, const char* interface,
92 const char* propertyName) const
93 {
94 any result =
95 getPropertyImpl(bus, service, path, interface, propertyName);
96 auto value = any_cast<PropertyType>(result);
97 return sdbusplus::message::variant_ns::get<T>(value);
98 }
99};
100
101class Utils : public UtilsInterface
102{
103 public:
104 std::vector<std::string>
105 getPSUInventoryPath(sdbusplus::bus::bus& bus) const override;
106
107 std::string getService(sdbusplus::bus::bus& bus, const char* path,
108 const char* interface) const override;
109
110 std::string getVersionId(const std::string& version) const override;
111
112 any getPropertyImpl(sdbusplus::bus::bus& bus, const char* service,
113 const char* path, const char* interface,
114 const char* propertyName) const override;
115};
116
117inline std::string getService(sdbusplus::bus::bus& bus, const char* path,
118 const char* interface)
119{
120 return getUtils().getService(bus, path, interface);
121}
122
123inline std::vector<std::string> getPSUInventoryPath(sdbusplus::bus::bus& bus)
124{
125 return getUtils().getPSUInventoryPath(bus);
126}
127
128inline std::string getVersionId(const std::string& version)
129{
130 return getUtils().getVersionId(version);
131}
132
133template <typename T>
134T getProperty(sdbusplus::bus::bus& bus, const char* service, const char* path,
135 const char* interface, const char* propertyName)
136{
137 return getUtils().getProperty<T>(bus, service, path, interface,
138 propertyName);
139}
140
Lei YU5e0dcb32019-08-02 18:04:34 +0800141} // namespace utils