| Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 1 | #pragma once | 
|  | 2 |  | 
| Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 3 | #include <phosphor-logging/log.hpp> | 
| Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 4 | #include <sdbusplus/bus.hpp> | 
|  | 5 | #include <sdbusplus/bus/match.hpp> | 
|  | 6 |  | 
|  | 7 | namespace openpower | 
|  | 8 | { | 
|  | 9 | namespace pels | 
|  | 10 | { | 
|  | 11 |  | 
|  | 12 | using DBusValue = sdbusplus::message::variant<std::string>; | 
|  | 13 | using DBusProperty = std::string; | 
|  | 14 | using DBusInterface = std::string; | 
|  | 15 | using DBusService = std::string; | 
|  | 16 | using DBusPath = std::string; | 
|  | 17 | using DBusInterfaceList = std::vector<DBusInterface>; | 
|  | 18 | using DBusPropertyMap = std::map<DBusProperty, DBusValue>; | 
|  | 19 |  | 
|  | 20 | /** | 
|  | 21 | * @class DataInterface | 
|  | 22 | * | 
| Matt Spinler | 19e89ce | 2019-11-06 13:02:23 -0600 | [diff] [blame] | 23 | * A base class for gathering data about the system for use | 
|  | 24 | * in PELs. Implemented this way to facilitate mocking. | 
| Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 25 | */ | 
|  | 26 | class DataInterfaceBase | 
|  | 27 | { | 
|  | 28 | public: | 
|  | 29 | DataInterfaceBase() = default; | 
|  | 30 | virtual ~DataInterfaceBase() = default; | 
|  | 31 | DataInterfaceBase(const DataInterfaceBase&) = default; | 
|  | 32 | DataInterfaceBase& operator=(const DataInterfaceBase&) = default; | 
|  | 33 | DataInterfaceBase(DataInterfaceBase&&) = default; | 
|  | 34 | DataInterfaceBase& operator=(DataInterfaceBase&&) = default; | 
|  | 35 |  | 
|  | 36 | /** | 
| Matt Spinler | 19e89ce | 2019-11-06 13:02:23 -0600 | [diff] [blame] | 37 | * @brief Returns the machine Type/Model | 
| Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 38 | * | 
|  | 39 | * @return string - The machine Type/Model string | 
|  | 40 | */ | 
| Matt Spinler | 19e89ce | 2019-11-06 13:02:23 -0600 | [diff] [blame] | 41 | virtual std::string getMachineTypeModel() const | 
|  | 42 | { | 
|  | 43 | return _machineTypeModel; | 
|  | 44 | } | 
| Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 45 |  | 
|  | 46 | /** | 
| Matt Spinler | 19e89ce | 2019-11-06 13:02:23 -0600 | [diff] [blame] | 47 | * @brief Returns the machine serial number | 
| Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 48 | * | 
|  | 49 | * @return string - The machine serial number | 
|  | 50 | */ | 
| Matt Spinler | 19e89ce | 2019-11-06 13:02:23 -0600 | [diff] [blame] | 51 | virtual std::string getMachineSerialNumber() const | 
|  | 52 | { | 
|  | 53 | return _machineSerialNumber; | 
|  | 54 | } | 
|  | 55 |  | 
| Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 56 | /** | 
|  | 57 | * @brief Says if the host is up and running | 
|  | 58 | * | 
|  | 59 | * @return bool - If the host is running | 
|  | 60 | */ | 
|  | 61 | virtual bool isHostUp() const | 
|  | 62 | { | 
|  | 63 | return _hostUp; | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | using HostStateChangeFunc = std::function<void(bool)>; | 
|  | 67 |  | 
|  | 68 | /** | 
|  | 69 | * @brief Register a callback function that will get | 
|  | 70 | *        called on all host on/off transitions. | 
|  | 71 | * | 
|  | 72 | * The void(bool) function will get passed the new | 
|  | 73 | * value of the host state. | 
|  | 74 | * | 
|  | 75 | * @param[in] name - The subscription name | 
|  | 76 | * @param[in] func - The function to run | 
|  | 77 | */ | 
|  | 78 | void subscribeToHostStateChange(const std::string& name, | 
|  | 79 | HostStateChangeFunc func) | 
|  | 80 | { | 
|  | 81 | _hostChangeCallbacks[name] = func; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | /** | 
|  | 85 | * @brief Unsubscribe from host state changes. | 
|  | 86 | * | 
|  | 87 | * @param[in] name - The subscription name | 
|  | 88 | */ | 
|  | 89 | void unsubscribeFromHostStateChange(const std::string& name) | 
|  | 90 | { | 
|  | 91 | _hostChangeCallbacks.erase(name); | 
|  | 92 | } | 
|  | 93 |  | 
| Matt Spinler | 19e89ce | 2019-11-06 13:02:23 -0600 | [diff] [blame] | 94 | protected: | 
|  | 95 | /** | 
| Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 96 | * @brief Sets the host on/off state and runs any | 
|  | 97 | *        callback functions (if there was a change). | 
|  | 98 | */ | 
|  | 99 | void setHostState(bool newState) | 
|  | 100 | { | 
|  | 101 | if (_hostUp != newState) | 
|  | 102 | { | 
|  | 103 | _hostUp = newState; | 
|  | 104 |  | 
|  | 105 | for (auto& [name, func] : _hostChangeCallbacks) | 
|  | 106 | { | 
|  | 107 | try | 
|  | 108 | { | 
|  | 109 | func(_hostUp); | 
|  | 110 | } | 
|  | 111 | catch (std::exception& e) | 
|  | 112 | { | 
|  | 113 | using namespace phosphor::logging; | 
|  | 114 | log<level::ERR>("A host state change callback threw " | 
|  | 115 | "an exception"); | 
|  | 116 | } | 
|  | 117 | } | 
|  | 118 | } | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | /** | 
| Matt Spinler | 19e89ce | 2019-11-06 13:02:23 -0600 | [diff] [blame] | 122 | * @brief The machine type-model.  Always kept up to date | 
|  | 123 | */ | 
|  | 124 | std::string _machineTypeModel; | 
|  | 125 |  | 
|  | 126 | /** | 
|  | 127 | * @brief The machine serial number.  Always kept up to date | 
|  | 128 | */ | 
|  | 129 | std::string _machineSerialNumber; | 
| Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 130 |  | 
|  | 131 | /** | 
|  | 132 | * @brief The host up status.  Always kept up to date. | 
|  | 133 | */ | 
|  | 134 | bool _hostUp = false; | 
|  | 135 |  | 
|  | 136 | /** | 
|  | 137 | * @brief The map of host state change subscriber | 
|  | 138 | *        names to callback functions. | 
|  | 139 | */ | 
|  | 140 | std::map<std::string, HostStateChangeFunc> _hostChangeCallbacks; | 
| Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 141 | }; | 
|  | 142 |  | 
|  | 143 | /** | 
|  | 144 | * @class DataInterface | 
|  | 145 | * | 
|  | 146 | * Concrete implementation of DataInterfaceBase. | 
|  | 147 | */ | 
|  | 148 | class DataInterface : public DataInterfaceBase | 
|  | 149 | { | 
|  | 150 | public: | 
|  | 151 | DataInterface() = delete; | 
|  | 152 | ~DataInterface() = default; | 
|  | 153 | DataInterface(const DataInterface&) = default; | 
|  | 154 | DataInterface& operator=(const DataInterface&) = default; | 
|  | 155 | DataInterface(DataInterface&&) = default; | 
|  | 156 | DataInterface& operator=(DataInterface&&) = default; | 
|  | 157 |  | 
|  | 158 | /** | 
|  | 159 | * @brief Constructor | 
|  | 160 | * | 
|  | 161 | * @param[in] bus - The sdbusplus bus object | 
|  | 162 | */ | 
|  | 163 | explicit DataInterface(sdbusplus::bus::bus& bus); | 
|  | 164 |  | 
| Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 165 | private: | 
|  | 166 | /** | 
|  | 167 | * @brief Reads the machine type/model and SN from D-Bus. | 
|  | 168 | * | 
|  | 169 | * Looks for them on the 'system' inventory object, and also | 
|  | 170 | * places a properties changed watch on them to obtain any changes | 
|  | 171 | * (or read them for the first time if the inventory isn't ready | 
|  | 172 | * when this function runs.) | 
|  | 173 | */ | 
|  | 174 | void readMTMS(); | 
|  | 175 |  | 
|  | 176 | /** | 
| Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 177 | * @brief Reads the host state from D-Bus. | 
|  | 178 | * | 
|  | 179 | * For host on, looks for the values of 'BootComplete' or 'Standby' | 
|  | 180 | * in the OperatingSystemState property on the | 
|  | 181 | * 'xyz.openbmc_project.State.OperatingSystem.Status' interface | 
|  | 182 | * on the '/xyz/openbmc_project/state/host0' path. | 
|  | 183 | * | 
|  | 184 | * Also adds a properties changed watch on it so the code can be | 
|  | 185 | * kept up to date on changes. | 
|  | 186 | */ | 
|  | 187 | void readHostState(); | 
|  | 188 |  | 
|  | 189 | /** | 
| Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 190 | * @brief Finds the D-Bus service name that hosts the | 
|  | 191 | *        passed in path and interface. | 
|  | 192 | * | 
|  | 193 | * @param[in] objectPath - The D-Bus object path | 
|  | 194 | * @param[in] interface - The D-Bus interface | 
|  | 195 | */ | 
|  | 196 | DBusService getService(const std::string& objectPath, | 
|  | 197 | const std::string& interface); | 
|  | 198 | /** | 
|  | 199 | * @brief Wrapper for the 'GetAll' properties method call | 
|  | 200 | * | 
|  | 201 | * @param[in] service - The D-Bus service to call it on | 
|  | 202 | * @param[in] objectPath - The D-Bus object path | 
|  | 203 | * @param[in] interface - The interface to get the props on | 
|  | 204 | * | 
|  | 205 | * @return DBusPropertyMap - The property results | 
|  | 206 | */ | 
|  | 207 | DBusPropertyMap getAllProperties(const std::string& service, | 
|  | 208 | const std::string& objectPath, | 
|  | 209 | const std::string& interface); | 
|  | 210 |  | 
|  | 211 | /** | 
| Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 212 | * @brief Wrapper for the 'Get' properties method call | 
|  | 213 | * | 
|  | 214 | * @param[in] service - The D-Bus service to call it on | 
|  | 215 | * @param[in] objectPath - The D-Bus object path | 
|  | 216 | * @param[in] interface - The interface to get the property on | 
|  | 217 | * @param[in] property - The property name | 
|  | 218 | * @param[out] value - Filled in with the property value. | 
|  | 219 | */ | 
|  | 220 | void getProperty(const std::string& service, const std::string& objectPath, | 
|  | 221 | const std::string& interface, const std::string& property, | 
|  | 222 | DBusValue& value); | 
|  | 223 |  | 
|  | 224 | /** | 
| Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 225 | * @brief The properties changed callback for the Asset iface | 
|  | 226 | *        on the system inventory object. | 
|  | 227 | * | 
|  | 228 | * @param[in] msg - The sdbusplus message of the signal | 
|  | 229 | */ | 
|  | 230 | void sysAssetPropChanged(sdbusplus::message::message& msg); | 
|  | 231 |  | 
|  | 232 | /** | 
| Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 233 | * @brief The properties changed callback for the OperatingSystemStatus | 
|  | 234 | *        interface on the host state object. | 
|  | 235 | * | 
|  | 236 | * @param[in] msg - The sdbusplus message of the signal | 
|  | 237 | */ | 
|  | 238 | void osStatePropChanged(sdbusplus::message::message& msg); | 
|  | 239 |  | 
|  | 240 | /** | 
| Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 241 | * @brief The match object for the system path's properties | 
|  | 242 | */ | 
|  | 243 | std::unique_ptr<sdbusplus::bus::match_t> _sysInventoryPropMatch; | 
|  | 244 |  | 
|  | 245 | /** | 
| Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 246 | * @brief The match object for the operating system status. | 
|  | 247 | */ | 
|  | 248 | std::unique_ptr<sdbusplus::bus::match_t> _osStateMatch; | 
|  | 249 |  | 
|  | 250 | /** | 
| Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 251 | * @brief The sdbusplus bus object for making D-Bus calls. | 
|  | 252 | */ | 
|  | 253 | sdbusplus::bus::bus& _bus; | 
|  | 254 | }; | 
|  | 255 |  | 
|  | 256 | } // namespace pels | 
|  | 257 | } // namespace openpower |