Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Matt Spinler | 2a28c93 | 2020-02-03 14:23:40 -0600 | [diff] [blame] | 3 | #include "dbus_types.hpp" |
| 4 | #include "dbus_watcher.hpp" |
| 5 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 6 | #include <filesystem> |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 7 | #include <phosphor-logging/log.hpp> |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 8 | #include <sdbusplus/bus.hpp> |
| 9 | #include <sdbusplus/bus/match.hpp> |
| 10 | |
| 11 | namespace openpower |
| 12 | { |
| 13 | namespace pels |
| 14 | { |
| 15 | |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 16 | /** |
| 17 | * @class DataInterface |
| 18 | * |
Matt Spinler | 19e89ce | 2019-11-06 13:02:23 -0600 | [diff] [blame] | 19 | * A base class for gathering data about the system for use |
| 20 | * in PELs. Implemented this way to facilitate mocking. |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 21 | */ |
| 22 | class 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 Spinler | 19e89ce | 2019-11-06 13:02:23 -0600 | [diff] [blame] | 33 | * @brief Returns the machine Type/Model |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 34 | * |
| 35 | * @return string - The machine Type/Model string |
| 36 | */ |
Matt Spinler | 19e89ce | 2019-11-06 13:02:23 -0600 | [diff] [blame] | 37 | virtual std::string getMachineTypeModel() const |
| 38 | { |
| 39 | return _machineTypeModel; |
| 40 | } |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 41 | |
| 42 | /** |
Matt Spinler | 19e89ce | 2019-11-06 13:02:23 -0600 | [diff] [blame] | 43 | * @brief Returns the machine serial number |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 44 | * |
| 45 | * @return string - The machine serial number |
| 46 | */ |
Matt Spinler | 19e89ce | 2019-11-06 13:02:23 -0600 | [diff] [blame] | 47 | virtual std::string getMachineSerialNumber() const |
| 48 | { |
| 49 | return _machineSerialNumber; |
| 50 | } |
| 51 | |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 52 | /** |
Matt Spinler | cce1411 | 2019-12-11 14:20:36 -0600 | [diff] [blame] | 53 | * @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 Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 63 | * @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 Spinler | b3f5186 | 2019-12-09 13:55:10 -0600 | [diff] [blame] | 72 | /** |
| 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 Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 86 | 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 Spinler | cad9c2b | 2019-12-02 15:42:01 -0600 | [diff] [blame] | 114 | /** |
| 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 Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 134 | /** |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 135 | * @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 Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 145 | * @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 Spinler | 9cf3cfd | 2020-02-03 14:41:55 -0600 | [diff] [blame] | 166 | /** |
| 167 | * @brief Returns the 'send event logs to host' setting. |
| 168 | * |
| 169 | * @return bool - If sending PELs to the host is enabled. |
| 170 | */ |
| 171 | virtual bool getHostPELEnablement() const |
| 172 | { |
| 173 | return _sendPELsToHost; |
| 174 | } |
| 175 | |
Matt Spinler | 19e89ce | 2019-11-06 13:02:23 -0600 | [diff] [blame] | 176 | protected: |
| 177 | /** |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 178 | * @brief Sets the host on/off state and runs any |
| 179 | * callback functions (if there was a change). |
| 180 | */ |
| 181 | void setHostState(bool newState) |
| 182 | { |
| 183 | if (_hostUp != newState) |
| 184 | { |
| 185 | _hostUp = newState; |
| 186 | |
| 187 | for (auto& [name, func] : _hostChangeCallbacks) |
| 188 | { |
| 189 | try |
| 190 | { |
| 191 | func(_hostUp); |
| 192 | } |
| 193 | catch (std::exception& e) |
| 194 | { |
| 195 | using namespace phosphor::logging; |
| 196 | log<level::ERR>("A host state change callback threw " |
| 197 | "an exception"); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /** |
Matt Spinler | 19e89ce | 2019-11-06 13:02:23 -0600 | [diff] [blame] | 204 | * @brief The machine type-model. Always kept up to date |
| 205 | */ |
| 206 | std::string _machineTypeModel; |
| 207 | |
| 208 | /** |
| 209 | * @brief The machine serial number. Always kept up to date |
| 210 | */ |
| 211 | std::string _machineSerialNumber; |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 212 | |
| 213 | /** |
Matt Spinler | cce1411 | 2019-12-11 14:20:36 -0600 | [diff] [blame] | 214 | * @brief The hardware management console status. Always kept |
| 215 | * up to date. |
| 216 | */ |
| 217 | bool _hmcManaged = false; |
| 218 | |
| 219 | /** |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 220 | * @brief The host up status. Always kept up to date. |
| 221 | */ |
| 222 | bool _hostUp = false; |
| 223 | |
| 224 | /** |
| 225 | * @brief The map of host state change subscriber |
| 226 | * names to callback functions. |
| 227 | */ |
| 228 | std::map<std::string, HostStateChangeFunc> _hostChangeCallbacks; |
Matt Spinler | cad9c2b | 2019-12-02 15:42:01 -0600 | [diff] [blame] | 229 | |
| 230 | /** |
| 231 | * @brief The BMC firmware version string |
| 232 | */ |
| 233 | std::string _bmcFWVersion; |
| 234 | |
| 235 | /** |
| 236 | * @brief The server firmware version string |
| 237 | */ |
| 238 | std::string _serverFWVersion; |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 239 | |
| 240 | /** |
| 241 | * @brief The BMC firmware version ID string |
| 242 | */ |
| 243 | std::string _bmcFWVersionID; |
Matt Spinler | 9cf3cfd | 2020-02-03 14:41:55 -0600 | [diff] [blame] | 244 | |
| 245 | /** |
| 246 | * @brief If sending PELs is enabled. |
| 247 | * |
| 248 | * This is usually set to false in manufacturing test. |
| 249 | */ |
| 250 | bool _sendPELsToHost = true; |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | /** |
| 254 | * @class DataInterface |
| 255 | * |
| 256 | * Concrete implementation of DataInterfaceBase. |
| 257 | */ |
| 258 | class DataInterface : public DataInterfaceBase |
| 259 | { |
| 260 | public: |
| 261 | DataInterface() = delete; |
| 262 | ~DataInterface() = default; |
| 263 | DataInterface(const DataInterface&) = default; |
| 264 | DataInterface& operator=(const DataInterface&) = default; |
| 265 | DataInterface(DataInterface&&) = default; |
| 266 | DataInterface& operator=(DataInterface&&) = default; |
| 267 | |
| 268 | /** |
| 269 | * @brief Constructor |
| 270 | * |
| 271 | * @param[in] bus - The sdbusplus bus object |
| 272 | */ |
| 273 | explicit DataInterface(sdbusplus::bus::bus& bus); |
| 274 | |
Matt Spinler | b3f5186 | 2019-12-09 13:55:10 -0600 | [diff] [blame] | 275 | /** |
| 276 | * @brief Returns the PLDM instance ID to use for PLDM commands |
| 277 | * |
| 278 | * @param[in] eid - The PLDM EID |
| 279 | * |
| 280 | * @return uint8_t - The instance ID |
| 281 | */ |
| 282 | uint8_t getPLDMInstanceID(uint8_t eid) const override; |
| 283 | |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 284 | /** |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 285 | * @brief Finds the D-Bus service name that hosts the |
| 286 | * passed in path and interface. |
| 287 | * |
| 288 | * @param[in] objectPath - The D-Bus object path |
| 289 | * @param[in] interface - The D-Bus interface |
| 290 | */ |
| 291 | DBusService getService(const std::string& objectPath, |
Matt Spinler | b3f5186 | 2019-12-09 13:55:10 -0600 | [diff] [blame] | 292 | const std::string& interface) const; |
Matt Spinler | 9cf3cfd | 2020-02-03 14:41:55 -0600 | [diff] [blame] | 293 | |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 294 | /** |
| 295 | * @brief Wrapper for the 'GetAll' properties method call |
| 296 | * |
| 297 | * @param[in] service - The D-Bus service to call it on |
| 298 | * @param[in] objectPath - The D-Bus object path |
| 299 | * @param[in] interface - The interface to get the props on |
| 300 | * |
| 301 | * @return DBusPropertyMap - The property results |
| 302 | */ |
| 303 | DBusPropertyMap getAllProperties(const std::string& service, |
| 304 | const std::string& objectPath, |
Matt Spinler | 2a28c93 | 2020-02-03 14:23:40 -0600 | [diff] [blame] | 305 | const std::string& interface) const; |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 306 | /** |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 307 | * @brief Wrapper for the 'Get' properties method call |
| 308 | * |
| 309 | * @param[in] service - The D-Bus service to call it on |
| 310 | * @param[in] objectPath - The D-Bus object path |
| 311 | * @param[in] interface - The interface to get the property on |
| 312 | * @param[in] property - The property name |
| 313 | * @param[out] value - Filled in with the property value. |
| 314 | */ |
| 315 | void getProperty(const std::string& service, const std::string& objectPath, |
| 316 | const std::string& interface, const std::string& property, |
Matt Spinler | 2a28c93 | 2020-02-03 14:23:40 -0600 | [diff] [blame] | 317 | DBusValue& value) const; |
| 318 | |
| 319 | private: |
| 320 | /** |
| 321 | * @brief Reads the BMC firmware version string and puts it into |
| 322 | * _bmcFWVersion. |
| 323 | */ |
| 324 | void readBMCFWVersion(); |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 325 | |
| 326 | /** |
Matt Spinler | 2a28c93 | 2020-02-03 14:23:40 -0600 | [diff] [blame] | 327 | * @brief Reads the server firmware version string and puts it into |
| 328 | * _serverFWVersion. |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 329 | */ |
Matt Spinler | 2a28c93 | 2020-02-03 14:23:40 -0600 | [diff] [blame] | 330 | void readServerFWVersion(); |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 331 | |
| 332 | /** |
Matt Spinler | 2a28c93 | 2020-02-03 14:23:40 -0600 | [diff] [blame] | 333 | * @brief Reads the BMC firmware version ID and puts it into |
| 334 | * _bmcFWVersionID. |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 335 | */ |
Matt Spinler | 2a28c93 | 2020-02-03 14:23:40 -0600 | [diff] [blame] | 336 | void readBMCFWVersionID(); |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 337 | |
| 338 | /** |
Matt Spinler | 2a28c93 | 2020-02-03 14:23:40 -0600 | [diff] [blame] | 339 | * @brief The D-Bus property or interface watchers that have callbacks |
| 340 | * registered that will set members in this class when |
| 341 | * they change. |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 342 | */ |
Matt Spinler | 2a28c93 | 2020-02-03 14:23:40 -0600 | [diff] [blame] | 343 | std::vector<std::unique_ptr<DBusWatcher>> _properties; |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 344 | |
| 345 | /** |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 346 | * @brief The sdbusplus bus object for making D-Bus calls. |
| 347 | */ |
| 348 | sdbusplus::bus::bus& _bus; |
| 349 | }; |
| 350 | |
| 351 | } // namespace pels |
| 352 | } // namespace openpower |