blob: a275e4d22d4aad262a0ac7dc0fcd20af070bf2bf [file] [log] [blame]
Matt Spinlerc8705e22019-09-11 12:36:07 -05001#pragma once
2
Matt Spinler2a28c932020-02-03 14:23:40 -06003#include "dbus_types.hpp"
4#include "dbus_watcher.hpp"
5
Matt Spinler4dcd3f42020-01-22 14:55:07 -06006#include <filesystem>
Matt Spinlera7d9d962019-11-06 15:01:25 -06007#include <phosphor-logging/log.hpp>
Matt Spinlerc8705e22019-09-11 12:36:07 -05008#include <sdbusplus/bus.hpp>
9#include <sdbusplus/bus/match.hpp>
10
11namespace openpower
12{
13namespace pels
14{
15
Matt Spinlerc8705e22019-09-11 12:36:07 -050016/**
17 * @class DataInterface
18 *
Matt Spinler19e89ce2019-11-06 13:02:23 -060019 * A base class for gathering data about the system for use
20 * in PELs. Implemented this way to facilitate mocking.
Matt Spinlerc8705e22019-09-11 12:36:07 -050021 */
22class DataInterfaceBase
23{
24 public:
25 DataInterfaceBase() = default;
26 virtual ~DataInterfaceBase() = default;
27 DataInterfaceBase(const DataInterfaceBase&) = default;
28 DataInterfaceBase& operator=(const DataInterfaceBase&) = default;
29 DataInterfaceBase(DataInterfaceBase&&) = default;
30 DataInterfaceBase& operator=(DataInterfaceBase&&) = default;
31
32 /**
Matt Spinler19e89ce2019-11-06 13:02:23 -060033 * @brief Returns the machine Type/Model
Matt Spinlerc8705e22019-09-11 12:36:07 -050034 *
35 * @return string - The machine Type/Model string
36 */
Matt Spinler19e89ce2019-11-06 13:02:23 -060037 virtual std::string getMachineTypeModel() const
38 {
39 return _machineTypeModel;
40 }
Matt Spinlerc8705e22019-09-11 12:36:07 -050041
42 /**
Matt Spinler19e89ce2019-11-06 13:02:23 -060043 * @brief Returns the machine serial number
Matt Spinlerc8705e22019-09-11 12:36:07 -050044 *
45 * @return string - The machine serial number
46 */
Matt Spinler19e89ce2019-11-06 13:02:23 -060047 virtual std::string getMachineSerialNumber() const
48 {
49 return _machineSerialNumber;
50 }
51
Matt Spinlera7d9d962019-11-06 15:01:25 -060052 /**
Matt Spinlercce14112019-12-11 14:20:36 -060053 * @brief Says if the system is managed by a hardware
54 * management console.
55 * @return bool - If the system is HMC managed
56 */
57 virtual bool isHMCManaged() const
58 {
59 return _hmcManaged;
60 }
61
62 /**
Matt Spinlera7d9d962019-11-06 15:01:25 -060063 * @brief Says if the host is up and running
64 *
65 * @return bool - If the host is running
66 */
67 virtual bool isHostUp() const
68 {
69 return _hostUp;
70 }
71
Matt Spinlerb3f51862019-12-09 13:55:10 -060072 /**
73 * @brief Returns the PLDM instance ID to use for PLDM commands
74 *
75 * The base class implementation just returns zero.
76 *
77 * @param[in] eid - The PLDM EID
78 *
79 * @return uint8_t - The instance ID
80 */
81 virtual uint8_t getPLDMInstanceID(uint8_t eid) const
82 {
83 return 0;
84 }
85
Matt Spinlera7d9d962019-11-06 15:01:25 -060086 using HostStateChangeFunc = std::function<void(bool)>;
87
88 /**
89 * @brief Register a callback function that will get
90 * called on all host on/off transitions.
91 *
92 * The void(bool) function will get passed the new
93 * value of the host state.
94 *
95 * @param[in] name - The subscription name
96 * @param[in] func - The function to run
97 */
98 void subscribeToHostStateChange(const std::string& name,
99 HostStateChangeFunc func)
100 {
101 _hostChangeCallbacks[name] = func;
102 }
103
104 /**
105 * @brief Unsubscribe from host state changes.
106 *
107 * @param[in] name - The subscription name
108 */
109 void unsubscribeFromHostStateChange(const std::string& name)
110 {
111 _hostChangeCallbacks.erase(name);
112 }
113
Matt Spinlercad9c2b2019-12-02 15:42:01 -0600114 /**
115 * @brief Returns the BMC firmware version
116 *
117 * @return std::string - The BMC version
118 */
119 virtual std::string getBMCFWVersion() const
120 {
121 return _bmcFWVersion;
122 }
123
124 /**
125 * @brief Returns the server firmware version
126 *
127 * @return std::string - The server firmware version
128 */
129 virtual std::string getServerFWVersion() const
130 {
131 return _serverFWVersion;
132 }
133
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600134 /**
Matt Spinler677381b2020-01-23 10:04:29 -0600135 * @brief Returns the BMC FW version ID
136 *
137 * @return std::string - The BMC FW version ID
138 */
139 virtual std::string getBMCFWVersionID() const
140 {
141 return _bmcFWVersionID;
142 }
143
144 /**
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600145 * @brief Returns the process name given its PID.
146 *
147 * @param[in] pid - The PID value as a string
148 *
149 * @return std::optional<std::string> - The name, or std::nullopt
150 */
151 std::optional<std::string> getProcessName(const std::string& pid) const
152 {
153 namespace fs = std::filesystem;
154
155 fs::path path{"/proc"};
156 path /= fs::path{pid} / "exe";
157
158 if (fs::exists(path))
159 {
160 return fs::read_symlink(path);
161 }
162
163 return std::nullopt;
164 }
165
Matt Spinler19e89ce2019-11-06 13:02:23 -0600166 protected:
167 /**
Matt Spinlera7d9d962019-11-06 15:01:25 -0600168 * @brief Sets the host on/off state and runs any
169 * callback functions (if there was a change).
170 */
171 void setHostState(bool newState)
172 {
173 if (_hostUp != newState)
174 {
175 _hostUp = newState;
176
177 for (auto& [name, func] : _hostChangeCallbacks)
178 {
179 try
180 {
181 func(_hostUp);
182 }
183 catch (std::exception& e)
184 {
185 using namespace phosphor::logging;
186 log<level::ERR>("A host state change callback threw "
187 "an exception");
188 }
189 }
190 }
191 }
192
193 /**
Matt Spinler19e89ce2019-11-06 13:02:23 -0600194 * @brief The machine type-model. Always kept up to date
195 */
196 std::string _machineTypeModel;
197
198 /**
199 * @brief The machine serial number. Always kept up to date
200 */
201 std::string _machineSerialNumber;
Matt Spinlera7d9d962019-11-06 15:01:25 -0600202
203 /**
Matt Spinlercce14112019-12-11 14:20:36 -0600204 * @brief The hardware management console status. Always kept
205 * up to date.
206 */
207 bool _hmcManaged = false;
208
209 /**
Matt Spinlera7d9d962019-11-06 15:01:25 -0600210 * @brief The host up status. Always kept up to date.
211 */
212 bool _hostUp = false;
213
214 /**
215 * @brief The map of host state change subscriber
216 * names to callback functions.
217 */
218 std::map<std::string, HostStateChangeFunc> _hostChangeCallbacks;
Matt Spinlercad9c2b2019-12-02 15:42:01 -0600219
220 /**
221 * @brief The BMC firmware version string
222 */
223 std::string _bmcFWVersion;
224
225 /**
226 * @brief The server firmware version string
227 */
228 std::string _serverFWVersion;
Matt Spinler677381b2020-01-23 10:04:29 -0600229
230 /**
231 * @brief The BMC firmware version ID string
232 */
233 std::string _bmcFWVersionID;
Matt Spinlerc8705e22019-09-11 12:36:07 -0500234};
235
236/**
237 * @class DataInterface
238 *
239 * Concrete implementation of DataInterfaceBase.
240 */
241class DataInterface : public DataInterfaceBase
242{
243 public:
244 DataInterface() = delete;
245 ~DataInterface() = default;
246 DataInterface(const DataInterface&) = default;
247 DataInterface& operator=(const DataInterface&) = default;
248 DataInterface(DataInterface&&) = default;
249 DataInterface& operator=(DataInterface&&) = default;
250
251 /**
252 * @brief Constructor
253 *
254 * @param[in] bus - The sdbusplus bus object
255 */
256 explicit DataInterface(sdbusplus::bus::bus& bus);
257
Matt Spinlerb3f51862019-12-09 13:55:10 -0600258 /**
259 * @brief Returns the PLDM instance ID to use for PLDM commands
260 *
261 * @param[in] eid - The PLDM EID
262 *
263 * @return uint8_t - The instance ID
264 */
265 uint8_t getPLDMInstanceID(uint8_t eid) const override;
266
Matt Spinler677381b2020-01-23 10:04:29 -0600267 /**
Matt Spinlerc8705e22019-09-11 12:36:07 -0500268 * @brief Finds the D-Bus service name that hosts the
269 * passed in path and interface.
270 *
271 * @param[in] objectPath - The D-Bus object path
272 * @param[in] interface - The D-Bus interface
273 */
274 DBusService getService(const std::string& objectPath,
Matt Spinlerb3f51862019-12-09 13:55:10 -0600275 const std::string& interface) const;
Matt Spinlerc8705e22019-09-11 12:36:07 -0500276 /**
277 * @brief Wrapper for the 'GetAll' properties method call
278 *
279 * @param[in] service - The D-Bus service to call it on
280 * @param[in] objectPath - The D-Bus object path
281 * @param[in] interface - The interface to get the props on
282 *
283 * @return DBusPropertyMap - The property results
284 */
285 DBusPropertyMap getAllProperties(const std::string& service,
286 const std::string& objectPath,
Matt Spinler2a28c932020-02-03 14:23:40 -0600287 const std::string& interface) const;
Matt Spinlerc8705e22019-09-11 12:36:07 -0500288
289 /**
Matt Spinlera7d9d962019-11-06 15:01:25 -0600290 * @brief Wrapper for the 'Get' properties method call
291 *
292 * @param[in] service - The D-Bus service to call it on
293 * @param[in] objectPath - The D-Bus object path
294 * @param[in] interface - The interface to get the property on
295 * @param[in] property - The property name
296 * @param[out] value - Filled in with the property value.
297 */
298 void getProperty(const std::string& service, const std::string& objectPath,
299 const std::string& interface, const std::string& property,
Matt Spinler2a28c932020-02-03 14:23:40 -0600300 DBusValue& value) const;
301
302 private:
303 /**
304 * @brief Reads the BMC firmware version string and puts it into
305 * _bmcFWVersion.
306 */
307 void readBMCFWVersion();
Matt Spinlera7d9d962019-11-06 15:01:25 -0600308
309 /**
Matt Spinler2a28c932020-02-03 14:23:40 -0600310 * @brief Reads the server firmware version string and puts it into
311 * _serverFWVersion.
Matt Spinlerc8705e22019-09-11 12:36:07 -0500312 */
Matt Spinler2a28c932020-02-03 14:23:40 -0600313 void readServerFWVersion();
Matt Spinlerc8705e22019-09-11 12:36:07 -0500314
315 /**
Matt Spinler2a28c932020-02-03 14:23:40 -0600316 * @brief Reads the BMC firmware version ID and puts it into
317 * _bmcFWVersionID.
Matt Spinlera7d9d962019-11-06 15:01:25 -0600318 */
Matt Spinler2a28c932020-02-03 14:23:40 -0600319 void readBMCFWVersionID();
Matt Spinlera7d9d962019-11-06 15:01:25 -0600320
321 /**
Matt Spinler2a28c932020-02-03 14:23:40 -0600322 * @brief The D-Bus property or interface watchers that have callbacks
323 * registered that will set members in this class when
324 * they change.
Matt Spinlerc8705e22019-09-11 12:36:07 -0500325 */
Matt Spinler2a28c932020-02-03 14:23:40 -0600326 std::vector<std::unique_ptr<DBusWatcher>> _properties;
Matt Spinlera7d9d962019-11-06 15:01:25 -0600327
328 /**
Matt Spinlerc8705e22019-09-11 12:36:07 -0500329 * @brief The sdbusplus bus object for making D-Bus calls.
330 */
331 sdbusplus::bus::bus& _bus;
332};
333
334} // namespace pels
335} // namespace openpower