Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 3 | #include <string> |
Patrick Williams | d22706f | 2017-05-04 05:42:49 -0500 | [diff] [blame] | 4 | #include <functional> |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 5 | #include <experimental/filesystem> |
| 6 | #include <cereal/access.hpp> |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 7 | #include <sdbusplus/bus.hpp> |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/log.hpp> |
Dhruvaraj Subhashchandran | 2710e73 | 2017-06-19 06:43:22 -0500 | [diff] [blame] | 9 | #include <xyz/openbmc_project/State/Boot/Progress/server.hpp> |
| 10 | #include <xyz/openbmc_project/Control/Boot/RebootAttempts/server.hpp> |
| 11 | #include <xyz/openbmc_project/State/OperatingSystem/Status/server.hpp> |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 12 | #include "xyz/openbmc_project/State/Host/server.hpp" |
Deepak Kodihalli | 3dd08a5 | 2017-07-25 07:34:44 -0500 | [diff] [blame] | 13 | #include "settings.hpp" |
Dhruvaraj Subhashchandran | 2710e73 | 2017-06-19 06:43:22 -0500 | [diff] [blame] | 14 | #include "config.h" |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 15 | |
| 16 | namespace phosphor |
| 17 | { |
| 18 | namespace state |
| 19 | { |
| 20 | namespace manager |
| 21 | { |
| 22 | |
Patrick Williams | d22706f | 2017-05-04 05:42:49 -0500 | [diff] [blame] | 23 | using HostInherit = sdbusplus::server::object::object< |
Dhruvaraj Subhashchandran | 2710e73 | 2017-06-19 06:43:22 -0500 | [diff] [blame] | 24 | sdbusplus::xyz::openbmc_project::State::server::Host, |
| 25 | sdbusplus::xyz::openbmc_project::State::Boot::server::Progress, |
| 26 | sdbusplus::xyz::openbmc_project::Control::Boot::server::RebootAttempts, |
| 27 | sdbusplus::xyz::openbmc_project::State::OperatingSystem::server::Status>; |
| 28 | |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 29 | using namespace phosphor::logging; |
| 30 | |
Patrick Williams | d22706f | 2017-05-04 05:42:49 -0500 | [diff] [blame] | 31 | namespace sdbusRule = sdbusplus::bus::match::rules; |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 32 | namespace fs = std::experimental::filesystem; |
Patrick Williams | d22706f | 2017-05-04 05:42:49 -0500 | [diff] [blame] | 33 | |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 34 | /** @class Host |
| 35 | * @brief OpenBMC host state management implementation. |
| 36 | * @details A concrete implementation for xyz.openbmc_project.State.Host |
| 37 | * DBus API. |
| 38 | */ |
Patrick Williams | d22706f | 2017-05-04 05:42:49 -0500 | [diff] [blame] | 39 | class Host : public HostInherit |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 40 | { |
| 41 | public: |
| 42 | /** @brief Constructs Host State Manager |
| 43 | * |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 44 | * @note This constructor passes 'true' to the base class in order to |
| 45 | * defer dbus object registration until we can run |
| 46 | * determineInitialState() and set our properties |
| 47 | * |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 48 | * @param[in] bus - The Dbus bus object |
| 49 | * @param[in] busName - The Dbus name to own |
| 50 | * @param[in] objPath - The Dbus object path |
| 51 | */ |
| 52 | Host(sdbusplus::bus::bus& bus, |
Patrick Williams | d22706f | 2017-05-04 05:42:49 -0500 | [diff] [blame] | 53 | const char* busName, |
| 54 | const char* objPath) : |
| 55 | HostInherit(bus, objPath, true), |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 56 | bus(bus), |
Patrick Williams | d22706f | 2017-05-04 05:42:49 -0500 | [diff] [blame] | 57 | systemdSignals( |
| 58 | bus, |
| 59 | sdbusRule::type::signal() + |
| 60 | sdbusRule::member("JobRemoved") + |
| 61 | sdbusRule::path("/org/freedesktop/systemd1") + |
| 62 | sdbusRule::interface( |
| 63 | "org.freedesktop.systemd1.Manager"), |
| 64 | std::bind(std::mem_fn(&Host::sysStateChange), |
Deepak Kodihalli | 3dd08a5 | 2017-07-25 07:34:44 -0500 | [diff] [blame] | 65 | this, std::placeholders::_1)), |
| 66 | settings(bus) |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 67 | { |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 68 | // Enable systemd signals |
| 69 | subscribeToSystemdSignals(); |
| 70 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 71 | // Will throw exception on fail |
| 72 | determineInitialState(); |
| 73 | |
Dhruvaraj Subhashchandran | 2710e73 | 2017-06-19 06:43:22 -0500 | [diff] [blame] | 74 | attemptsLeft(BOOT_COUNT_MAX_ALLOWED); |
| 75 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 76 | // We deferred this until we could get our property correct |
| 77 | this->emit_object_added(); |
| 78 | } |
| 79 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 80 | /** @brief Set value of HostTransition */ |
| 81 | Transition requestedHostTransition(Transition value) override; |
| 82 | |
Dhruvaraj Subhashchandran | 4e6534f | 2017-09-19 06:13:20 -0500 | [diff] [blame^] | 83 | /** @brief Set Value for boot progress */ |
| 84 | ProgressStages bootProgress(ProgressStages value) override; |
| 85 | |
| 86 | /** @brief Set Value for Operating System Status */ |
| 87 | OSStatus operatingSystemState(OSStatus value) override; |
| 88 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 89 | /** @brief Set value of CurrentHostState */ |
| 90 | HostState currentHostState(HostState value) override; |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 91 | |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 92 | /** |
| 93 | * @brief Set host reboot count to default |
| 94 | * |
| 95 | * OpenBMC software controls the number of allowed reboot attempts so |
| 96 | * any external set request of this property will be overridden by |
| 97 | * this function and set to the default. |
| 98 | * |
| 99 | * The only code responsible for decrementing the boot count resides |
| 100 | * within this process and that will use the sub class interface |
| 101 | * directly |
| 102 | * |
| 103 | * @param[in] value - Reboot count value, will be ignored |
| 104 | * |
| 105 | * @return Default number of reboot attempts left |
| 106 | */ |
| 107 | uint32_t attemptsLeft(uint32_t value) override |
| 108 | { |
| 109 | log<level::DEBUG>("External request to reset reboot count"); |
| 110 | return (sdbusplus::xyz::openbmc_project::Control::Boot::server:: |
| 111 | RebootAttempts::attemptsLeft(BOOT_COUNT_MAX_ALLOWED)); |
| 112 | } |
| 113 | |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 114 | private: |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 115 | /** |
| 116 | * @brief subscribe to the systemd signals |
| 117 | * |
| 118 | * This object needs to capture when it's systemd targets complete |
| 119 | * so it can keep it's state updated |
| 120 | * |
| 121 | **/ |
| 122 | void subscribeToSystemdSignals(); |
| 123 | |
| 124 | /** |
| 125 | * @brief Determine initial host state and set internally |
| 126 | * |
| 127 | * @return Will throw exceptions on failure |
| 128 | **/ |
| 129 | void determineInitialState(); |
| 130 | |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 131 | /** @brief Execute the transition request |
| 132 | * |
| 133 | * This function assumes the state has been validated and the host |
| 134 | * is in an appropriate state for the transition to be started. |
| 135 | * |
| 136 | * @param[in] tranReq - Transition requested |
| 137 | */ |
| 138 | void executeTransition(Transition tranReq); |
| 139 | |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 140 | /** |
Josh D. King | 929ef70 | 2017-03-02 10:58:11 -0600 | [diff] [blame] | 141 | * @brief Determine if target is active |
| 142 | * |
| 143 | * This function determines if the target is active and |
| 144 | * helps prevent misleading log recorded states. |
| 145 | * |
| 146 | * @param[in] target - Target string to check on |
| 147 | * |
| 148 | * @return boolean corresponding to state active |
| 149 | **/ |
| 150 | bool stateActive(const std::string& target); |
| 151 | |
| 152 | /** |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 153 | * @brief Determine if auto reboot flag is set |
| 154 | * |
| 155 | * @return boolean corresponding to current auto_reboot setting |
| 156 | **/ |
| 157 | bool isAutoReboot(); |
| 158 | |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 159 | /** @brief Check if systemd state change is relevant to this object |
| 160 | * |
| 161 | * Instance specific interface to handle the detected systemd state |
| 162 | * change |
| 163 | * |
| 164 | * @param[in] msg - Data associated with subscribed signal |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 165 | * |
| 166 | */ |
Patrick Williams | d22706f | 2017-05-04 05:42:49 -0500 | [diff] [blame] | 167 | void sysStateChange(sdbusplus::message::message& msg); |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 168 | |
Andrew Geissler | 7b90a62 | 2017-08-08 11:41:08 -0500 | [diff] [blame] | 169 | /** @brief Decrement reboot count |
| 170 | * |
| 171 | * This is used internally to this application to decrement the boot |
| 172 | * count on each boot attempt. The host will use the external |
| 173 | * attemptsLeft() interface to reset the count when a boot is successful |
| 174 | * |
| 175 | * @return number of reboot count attempts left |
| 176 | */ |
| 177 | uint32_t decrementRebootCount(); |
| 178 | |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 179 | // Allow cereal class access to allow these next two function to be |
| 180 | // private |
| 181 | friend class cereal::access; |
| 182 | |
| 183 | /** @brief Function required by Cereal to perform serialization. |
| 184 | * |
| 185 | * @tparam Archive - Cereal archive type (binary in our case). |
| 186 | * @param[in] archive - reference to Cereal archive. |
| 187 | */ |
| 188 | template<class Archive> |
| 189 | void save(Archive& archive) const |
| 190 | { |
| 191 | archive(convertForMessage(sdbusplus::xyz::openbmc_project:: |
| 192 | State::server::Host:: |
Dhruvaraj Subhashchandran | 4e6534f | 2017-09-19 06:13:20 -0500 | [diff] [blame^] | 193 | requestedHostTransition()), |
| 194 | convertForMessage(sdbusplus::xyz::openbmc_project:: |
| 195 | State::Boot::server::Progress:: |
| 196 | bootProgress()), |
| 197 | convertForMessage(sdbusplus::xyz::openbmc_project:: |
| 198 | State::OperatingSystem::server::Status:: |
| 199 | operatingSystemState())); |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /** @brief Function required by Cereal to perform deserialization. |
| 203 | * |
| 204 | * @tparam Archive - Cereal archive type (binary in our case). |
| 205 | * @param[in] archive - reference to Cereal archive. |
| 206 | */ |
| 207 | template<class Archive> |
| 208 | void load(Archive& archive) |
| 209 | { |
Dhruvaraj Subhashchandran | 4e6534f | 2017-09-19 06:13:20 -0500 | [diff] [blame^] | 210 | std::string reqTranState; |
| 211 | std::string bootProgress; |
| 212 | std::string osState; |
| 213 | archive(reqTranState, bootProgress, osState); |
| 214 | auto reqTran = Host::convertTransitionFromString(reqTranState); |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 215 | // When restoring, set the requested state with persistent value |
| 216 | // but don't call the override which would execute it |
| 217 | sdbusplus::xyz::openbmc_project::State::server::Host:: |
| 218 | requestedHostTransition(reqTran); |
Dhruvaraj Subhashchandran | 4e6534f | 2017-09-19 06:13:20 -0500 | [diff] [blame^] | 219 | sdbusplus::xyz::openbmc_project::State::Boot::server::Progress:: |
| 220 | bootProgress( |
| 221 | Host::convertProgressStagesFromString(bootProgress)); |
| 222 | sdbusplus::xyz::openbmc_project::State::OperatingSystem::server:: |
| 223 | Status::operatingSystemState( |
| 224 | Host::convertOSStatusFromString(osState)); |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /** @brief Serialize and persist requested host state |
| 228 | * |
| 229 | * @param[in] dir - pathname of file where the serialized host state will |
| 230 | * be placed. |
| 231 | * |
| 232 | * @return fs::path - pathname of persisted requested host state. |
| 233 | */ |
| 234 | fs::path serialize(const fs::path& dir = |
| 235 | fs::path(HOST_STATE_PERSIST_PATH)); |
| 236 | |
| 237 | /** @brief Deserialze a persisted requested host state. |
| 238 | * |
| 239 | * @param[in] path - pathname of persisted host state file |
| 240 | * |
| 241 | * @return bool - true if the deserialization was successful, false |
| 242 | * otherwise. |
| 243 | */ |
| 244 | bool deserialize(const fs::path& path); |
| 245 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 246 | /** @brief Persistent sdbusplus DBus bus connection. */ |
| 247 | sdbusplus::bus::bus& bus; |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 248 | |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 249 | /** @brief Used to subscribe to dbus systemd signals **/ |
Patrick Williams | d22706f | 2017-05-04 05:42:49 -0500 | [diff] [blame] | 250 | sdbusplus::bus::match_t systemdSignals; |
Deepak Kodihalli | 3dd08a5 | 2017-07-25 07:34:44 -0500 | [diff] [blame] | 251 | |
| 252 | // Settings objects of interest |
| 253 | settings::Objects settings; |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 254 | }; |
| 255 | |
| 256 | } // namespace manager |
| 257 | } // namespace state |
| 258 | } // namespace phosphor |