1 #pragma once
2 
3 #include "types.hpp"
4 
5 #include <sdbusplus/bus.hpp>
6 
7 #include <any>
8 #include <set>
9 #include <string>
10 #include <vector>
11 
12 namespace utils
13 {
14 
15 class UtilsInterface;
16 
17 using AssociationList = phosphor::software::updater::AssociationList;
18 using std::any;
19 using std::any_cast;
20 
21 /**
22  * @brief Get the implementation of UtilsInterface
23  */
24 const UtilsInterface& getUtils();
25 
26 /**
27  * @brief Get PSU inventory object path from DBus
28  */
29 std::vector<std::string> getPSUInventoryPath(sdbusplus::bus_t& bus);
30 
31 /** @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  */
39 std::string getService(sdbusplus::bus_t& bus, const char* path,
40                        const char* interface);
41 
42 /** @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  */
50 std::vector<std::string> getServices(sdbusplus::bus_t& bus, const char* path,
51                                      const char* interface);
52 
53 /** @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  */
63 template <typename T>
64 T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path,
65               const char* interface, const char* propertyName);
66 
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  */
77 std::string getVersionId(const std::string& version);
78 
79 /** @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  */
85 std::string getVersion(const std::string& inventoryPath);
86 
87 /** @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  */
93 std::string getLatestVersion(const std::set<std::string>& versions);
94 
95 /** @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  */
102 bool isAssociated(const std::string& psuInventoryPath,
103                   const AssociationList& assocs);
104 
105 /**
106  * @brief The interface for utils
107  */
108 class UtilsInterface
109 {
110   public:
111     // For now the code needs to get property for Present and Version
112     using PropertyType = std::variant<std::string, bool>;
113 
114     virtual ~UtilsInterface() = default;
115 
116     virtual std::vector<std::string>
117         getPSUInventoryPath(sdbusplus::bus_t& bus) const = 0;
118 
119     virtual std::string getService(sdbusplus::bus_t& bus, const char* path,
120                                    const char* interface) const = 0;
121 
122     virtual std::vector<std::string>
123         getServices(sdbusplus::bus_t& bus, const char* path,
124                     const char* interface) const = 0;
125 
126     virtual std::string getVersionId(const std::string& version) const = 0;
127 
128     virtual std::string getVersion(const std::string& inventoryPath) const = 0;
129 
130     virtual std::string
131         getLatestVersion(const std::set<std::string>& versions) const = 0;
132 
133     virtual bool isAssociated(const std::string& psuInventoryPath,
134                               const AssociationList& assocs) const = 0;
135 
136     virtual any getPropertyImpl(sdbusplus::bus_t& bus, const char* service,
137                                 const char* path, const char* interface,
138                                 const char* propertyName) const = 0;
139 
140     template <typename T>
141     T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path,
142                   const char* interface, const char* propertyName) const
143     {
144         any result = getPropertyImpl(bus, service, path, interface,
145                                      propertyName);
146         auto value = any_cast<PropertyType>(result);
147         return std::get<T>(value);
148     }
149 };
150 
151 class Utils : public UtilsInterface
152 {
153   public:
154     std::vector<std::string>
155         getPSUInventoryPath(sdbusplus::bus_t& bus) const override;
156 
157     std::string getService(sdbusplus::bus_t& bus, const char* path,
158                            const char* interface) const override;
159 
160     std::vector<std::string> getServices(sdbusplus::bus_t& bus,
161                                          const char* path,
162                                          const char* interface) const override;
163 
164     std::string getVersionId(const std::string& version) const override;
165 
166     std::string getVersion(const std::string& inventoryPath) const override;
167 
168     std::string
169         getLatestVersion(const std::set<std::string>& versions) const override;
170 
171     bool isAssociated(const std::string& psuInventoryPath,
172                       const AssociationList& assocs) const override;
173 
174     any getPropertyImpl(sdbusplus::bus_t& bus, const char* service,
175                         const char* path, const char* interface,
176                         const char* propertyName) const override;
177 };
178 
179 inline std::string getService(sdbusplus::bus_t& bus, const char* path,
180                               const char* interface)
181 {
182     return getUtils().getService(bus, path, interface);
183 }
184 
185 inline std::vector<std::string>
186     getServices(sdbusplus::bus_t& bus, const char* path, const char* interface)
187 {
188     return getUtils().getServices(bus, path, interface);
189 }
190 
191 inline std::vector<std::string> getPSUInventoryPath(sdbusplus::bus_t& bus)
192 {
193     return getUtils().getPSUInventoryPath(bus);
194 }
195 
196 inline std::string getVersionId(const std::string& version)
197 {
198     return getUtils().getVersionId(version);
199 }
200 
201 inline std::string getVersion(const std::string& inventoryPath)
202 {
203     return getUtils().getVersion(inventoryPath);
204 }
205 
206 inline std::string getLatestVersion(const std::set<std::string>& versions)
207 {
208     return getUtils().getLatestVersion(versions);
209 }
210 
211 inline bool isAssociated(const std::string& psuInventoryPath,
212                          const AssociationList& assocs)
213 {
214     return getUtils().isAssociated(psuInventoryPath, assocs);
215 }
216 
217 template <typename T>
218 T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path,
219               const char* interface, const char* propertyName)
220 {
221     return getUtils().getProperty<T>(bus, service, path, interface,
222                                      propertyName);
223 }
224 
225 } // namespace utils
226