blob: e69054295b279c571ae52a30c31c65ef8f3b11fe [file] [log] [blame]
Matt Spinlerc8705e22019-09-11 12:36:07 -05001#pragma once
2
Matt Spinler4dcd3f42020-01-22 14:55:07 -06003#include <filesystem>
Matt Spinlera7d9d962019-11-06 15:01:25 -06004#include <phosphor-logging/log.hpp>
Matt Spinlerc8705e22019-09-11 12:36:07 -05005#include <sdbusplus/bus.hpp>
6#include <sdbusplus/bus/match.hpp>
7
8namespace openpower
9{
10namespace pels
11{
12
13using DBusValue = sdbusplus::message::variant<std::string>;
14using DBusProperty = std::string;
15using DBusInterface = std::string;
16using DBusService = std::string;
17using DBusPath = std::string;
18using DBusInterfaceList = std::vector<DBusInterface>;
19using DBusPropertyMap = std::map<DBusProperty, DBusValue>;
20
21/**
22 * @class DataInterface
23 *
Matt Spinler19e89ce2019-11-06 13:02:23 -060024 * A base class for gathering data about the system for use
25 * in PELs. Implemented this way to facilitate mocking.
Matt Spinlerc8705e22019-09-11 12:36:07 -050026 */
27class DataInterfaceBase
28{
29 public:
30 DataInterfaceBase() = default;
31 virtual ~DataInterfaceBase() = default;
32 DataInterfaceBase(const DataInterfaceBase&) = default;
33 DataInterfaceBase& operator=(const DataInterfaceBase&) = default;
34 DataInterfaceBase(DataInterfaceBase&&) = default;
35 DataInterfaceBase& operator=(DataInterfaceBase&&) = default;
36
37 /**
Matt Spinler19e89ce2019-11-06 13:02:23 -060038 * @brief Returns the machine Type/Model
Matt Spinlerc8705e22019-09-11 12:36:07 -050039 *
40 * @return string - The machine Type/Model string
41 */
Matt Spinler19e89ce2019-11-06 13:02:23 -060042 virtual std::string getMachineTypeModel() const
43 {
44 return _machineTypeModel;
45 }
Matt Spinlerc8705e22019-09-11 12:36:07 -050046
47 /**
Matt Spinler19e89ce2019-11-06 13:02:23 -060048 * @brief Returns the machine serial number
Matt Spinlerc8705e22019-09-11 12:36:07 -050049 *
50 * @return string - The machine serial number
51 */
Matt Spinler19e89ce2019-11-06 13:02:23 -060052 virtual std::string getMachineSerialNumber() const
53 {
54 return _machineSerialNumber;
55 }
56
Matt Spinlera7d9d962019-11-06 15:01:25 -060057 /**
Matt Spinlercce14112019-12-11 14:20:36 -060058 * @brief Says if the system is managed by a hardware
59 * management console.
60 * @return bool - If the system is HMC managed
61 */
62 virtual bool isHMCManaged() const
63 {
64 return _hmcManaged;
65 }
66
67 /**
Matt Spinlera7d9d962019-11-06 15:01:25 -060068 * @brief Says if the host is up and running
69 *
70 * @return bool - If the host is running
71 */
72 virtual bool isHostUp() const
73 {
74 return _hostUp;
75 }
76
Matt Spinlerb3f51862019-12-09 13:55:10 -060077 /**
78 * @brief Returns the PLDM instance ID to use for PLDM commands
79 *
80 * The base class implementation just returns zero.
81 *
82 * @param[in] eid - The PLDM EID
83 *
84 * @return uint8_t - The instance ID
85 */
86 virtual uint8_t getPLDMInstanceID(uint8_t eid) const
87 {
88 return 0;
89 }
90
Matt Spinlera7d9d962019-11-06 15:01:25 -060091 using HostStateChangeFunc = std::function<void(bool)>;
92
93 /**
94 * @brief Register a callback function that will get
95 * called on all host on/off transitions.
96 *
97 * The void(bool) function will get passed the new
98 * value of the host state.
99 *
100 * @param[in] name - The subscription name
101 * @param[in] func - The function to run
102 */
103 void subscribeToHostStateChange(const std::string& name,
104 HostStateChangeFunc func)
105 {
106 _hostChangeCallbacks[name] = func;
107 }
108
109 /**
110 * @brief Unsubscribe from host state changes.
111 *
112 * @param[in] name - The subscription name
113 */
114 void unsubscribeFromHostStateChange(const std::string& name)
115 {
116 _hostChangeCallbacks.erase(name);
117 }
118
Matt Spinlercad9c2b2019-12-02 15:42:01 -0600119 /**
120 * @brief Returns the BMC firmware version
121 *
122 * @return std::string - The BMC version
123 */
124 virtual std::string getBMCFWVersion() const
125 {
126 return _bmcFWVersion;
127 }
128
129 /**
130 * @brief Returns the server firmware version
131 *
132 * @return std::string - The server firmware version
133 */
134 virtual std::string getServerFWVersion() const
135 {
136 return _serverFWVersion;
137 }
138
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600139 /**
140 * @brief Returns the process name given its PID.
141 *
142 * @param[in] pid - The PID value as a string
143 *
144 * @return std::optional<std::string> - The name, or std::nullopt
145 */
146 std::optional<std::string> getProcessName(const std::string& pid) const
147 {
148 namespace fs = std::filesystem;
149
150 fs::path path{"/proc"};
151 path /= fs::path{pid} / "exe";
152
153 if (fs::exists(path))
154 {
155 return fs::read_symlink(path);
156 }
157
158 return std::nullopt;
159 }
160
Matt Spinler19e89ce2019-11-06 13:02:23 -0600161 protected:
162 /**
Matt Spinlera7d9d962019-11-06 15:01:25 -0600163 * @brief Sets the host on/off state and runs any
164 * callback functions (if there was a change).
165 */
166 void setHostState(bool newState)
167 {
168 if (_hostUp != newState)
169 {
170 _hostUp = newState;
171
172 for (auto& [name, func] : _hostChangeCallbacks)
173 {
174 try
175 {
176 func(_hostUp);
177 }
178 catch (std::exception& e)
179 {
180 using namespace phosphor::logging;
181 log<level::ERR>("A host state change callback threw "
182 "an exception");
183 }
184 }
185 }
186 }
187
188 /**
Matt Spinler19e89ce2019-11-06 13:02:23 -0600189 * @brief The machine type-model. Always kept up to date
190 */
191 std::string _machineTypeModel;
192
193 /**
194 * @brief The machine serial number. Always kept up to date
195 */
196 std::string _machineSerialNumber;
Matt Spinlera7d9d962019-11-06 15:01:25 -0600197
198 /**
Matt Spinlercce14112019-12-11 14:20:36 -0600199 * @brief The hardware management console status. Always kept
200 * up to date.
201 */
202 bool _hmcManaged = false;
203
204 /**
Matt Spinlera7d9d962019-11-06 15:01:25 -0600205 * @brief The host up status. Always kept up to date.
206 */
207 bool _hostUp = false;
208
209 /**
210 * @brief The map of host state change subscriber
211 * names to callback functions.
212 */
213 std::map<std::string, HostStateChangeFunc> _hostChangeCallbacks;
Matt Spinlercad9c2b2019-12-02 15:42:01 -0600214
215 /**
216 * @brief The BMC firmware version string
217 */
218 std::string _bmcFWVersion;
219
220 /**
221 * @brief The server firmware version string
222 */
223 std::string _serverFWVersion;
Matt Spinlerc8705e22019-09-11 12:36:07 -0500224};
225
226/**
227 * @class DataInterface
228 *
229 * Concrete implementation of DataInterfaceBase.
230 */
231class DataInterface : public DataInterfaceBase
232{
233 public:
234 DataInterface() = delete;
235 ~DataInterface() = default;
236 DataInterface(const DataInterface&) = default;
237 DataInterface& operator=(const DataInterface&) = default;
238 DataInterface(DataInterface&&) = default;
239 DataInterface& operator=(DataInterface&&) = default;
240
241 /**
242 * @brief Constructor
243 *
244 * @param[in] bus - The sdbusplus bus object
245 */
246 explicit DataInterface(sdbusplus::bus::bus& bus);
247
Matt Spinlerb3f51862019-12-09 13:55:10 -0600248 /**
249 * @brief Returns the PLDM instance ID to use for PLDM commands
250 *
251 * @param[in] eid - The PLDM EID
252 *
253 * @return uint8_t - The instance ID
254 */
255 uint8_t getPLDMInstanceID(uint8_t eid) const override;
256
Matt Spinlerc8705e22019-09-11 12:36:07 -0500257 private:
258 /**
259 * @brief Reads the machine type/model and SN from D-Bus.
260 *
261 * Looks for them on the 'system' inventory object, and also
262 * places a properties changed watch on them to obtain any changes
263 * (or read them for the first time if the inventory isn't ready
264 * when this function runs.)
265 */
266 void readMTMS();
267
268 /**
Matt Spinlera7d9d962019-11-06 15:01:25 -0600269 * @brief Reads the host state from D-Bus.
270 *
271 * For host on, looks for the values of 'BootComplete' or 'Standby'
272 * in the OperatingSystemState property on the
273 * 'xyz.openbmc_project.State.OperatingSystem.Status' interface
274 * on the '/xyz/openbmc_project/state/host0' path.
275 *
276 * Also adds a properties changed watch on it so the code can be
277 * kept up to date on changes.
278 */
279 void readHostState();
280
281 /**
Matt Spinlercad9c2b2019-12-02 15:42:01 -0600282 * @brief Reads the BMC firmware version string and puts it into
283 * _bmcFWVersion.
284 */
285 void readBMCFWVersion();
286
287 /**
288 * @brief Reads the server firmware version string and puts it into
289 * _serverFWVersion.
290 */
291 void readServerFWVersion();
292
293 /**
Matt Spinlerc8705e22019-09-11 12:36:07 -0500294 * @brief Finds the D-Bus service name that hosts the
295 * passed in path and interface.
296 *
297 * @param[in] objectPath - The D-Bus object path
298 * @param[in] interface - The D-Bus interface
299 */
300 DBusService getService(const std::string& objectPath,
Matt Spinlerb3f51862019-12-09 13:55:10 -0600301 const std::string& interface) const;
Matt Spinlerc8705e22019-09-11 12:36:07 -0500302 /**
303 * @brief Wrapper for the 'GetAll' properties method call
304 *
305 * @param[in] service - The D-Bus service to call it on
306 * @param[in] objectPath - The D-Bus object path
307 * @param[in] interface - The interface to get the props on
308 *
309 * @return DBusPropertyMap - The property results
310 */
311 DBusPropertyMap getAllProperties(const std::string& service,
312 const std::string& objectPath,
313 const std::string& interface);
314
315 /**
Matt Spinlera7d9d962019-11-06 15:01:25 -0600316 * @brief Wrapper for the 'Get' properties method call
317 *
318 * @param[in] service - The D-Bus service to call it on
319 * @param[in] objectPath - The D-Bus object path
320 * @param[in] interface - The interface to get the property on
321 * @param[in] property - The property name
322 * @param[out] value - Filled in with the property value.
323 */
324 void getProperty(const std::string& service, const std::string& objectPath,
325 const std::string& interface, const std::string& property,
326 DBusValue& value);
327
328 /**
Matt Spinlerc8705e22019-09-11 12:36:07 -0500329 * @brief The properties changed callback for the Asset iface
330 * on the system inventory object.
331 *
332 * @param[in] msg - The sdbusplus message of the signal
333 */
334 void sysAssetPropChanged(sdbusplus::message::message& msg);
335
336 /**
Matt Spinlera7d9d962019-11-06 15:01:25 -0600337 * @brief The properties changed callback for the OperatingSystemStatus
338 * interface on the host state object.
339 *
340 * @param[in] msg - The sdbusplus message of the signal
341 */
342 void osStatePropChanged(sdbusplus::message::message& msg);
343
344 /**
Matt Spinlerc8705e22019-09-11 12:36:07 -0500345 * @brief The match object for the system path's properties
346 */
347 std::unique_ptr<sdbusplus::bus::match_t> _sysInventoryPropMatch;
348
349 /**
Matt Spinlera7d9d962019-11-06 15:01:25 -0600350 * @brief The match object for the operating system status.
351 */
352 std::unique_ptr<sdbusplus::bus::match_t> _osStateMatch;
353
354 /**
Matt Spinlerc8705e22019-09-11 12:36:07 -0500355 * @brief The sdbusplus bus object for making D-Bus calls.
356 */
357 sdbusplus::bus::bus& _bus;
358};
359
360} // namespace pels
361} // namespace openpower