blob: a0ba76802dfd5950e8a3a7649078adae3b1097de [file] [log] [blame]
Andrew Geissler36529022016-11-29 15:23:54 -06001#pragma once
2
Michael Tritz206a8332017-02-06 16:01:23 -06003#include <string>
Patrick Williamsd22706f2017-05-04 05:42:49 -05004#include <functional>
Andrew Geissler033fc3b2017-08-30 15:11:44 -05005#include <experimental/filesystem>
6#include <cereal/access.hpp>
Andrew Geissler36529022016-11-29 15:23:54 -06007#include <sdbusplus/bus.hpp>
Andrew Geissler7b90a622017-08-08 11:41:08 -05008#include <phosphor-logging/log.hpp>
Dhruvaraj Subhashchandran2710e732017-06-19 06:43:22 -05009#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 Geissler36529022016-11-29 15:23:54 -060012#include "xyz/openbmc_project/State/Host/server.hpp"
Deepak Kodihalli3dd08a52017-07-25 07:34:44 -050013#include "settings.hpp"
Dhruvaraj Subhashchandran2710e732017-06-19 06:43:22 -050014#include "config.h"
Andrew Geissler36529022016-11-29 15:23:54 -060015
16namespace phosphor
17{
18namespace state
19{
20namespace manager
21{
22
Patrick Williamsd22706f2017-05-04 05:42:49 -050023using HostInherit = sdbusplus::server::object::object<
Dhruvaraj Subhashchandran2710e732017-06-19 06:43:22 -050024 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 Geissler7b90a622017-08-08 11:41:08 -050029using namespace phosphor::logging;
30
Patrick Williamsd22706f2017-05-04 05:42:49 -050031namespace sdbusRule = sdbusplus::bus::match::rules;
Andrew Geissler033fc3b2017-08-30 15:11:44 -050032namespace fs = std::experimental::filesystem;
Patrick Williamsd22706f2017-05-04 05:42:49 -050033
Andrew Geissler36529022016-11-29 15:23:54 -060034/** @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 Williamsd22706f2017-05-04 05:42:49 -050039class Host : public HostInherit
Andrew Geissler36529022016-11-29 15:23:54 -060040{
41 public:
42 /** @brief Constructs Host State Manager
43 *
Andrew Geissleref3c1842016-12-01 12:33:09 -060044 * @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 Geissler36529022016-11-29 15:23:54 -060048 * @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 Williamsd22706f2017-05-04 05:42:49 -050053 const char* busName,
54 const char* objPath) :
55 HostInherit(bus, objPath, true),
Andrew Geissleref621162016-12-08 12:56:21 -060056 bus(bus),
Patrick Williamsd22706f2017-05-04 05:42:49 -050057 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 Kodihalli3dd08a52017-07-25 07:34:44 -050065 this, std::placeholders::_1)),
66 settings(bus)
Andrew Geissleref3c1842016-12-01 12:33:09 -060067 {
Andrew Geissler4da7e002017-01-24 15:21:40 -060068 // Enable systemd signals
69 subscribeToSystemdSignals();
70
Andrew Geissleref3c1842016-12-01 12:33:09 -060071 // Will throw exception on fail
72 determineInitialState();
73
Dhruvaraj Subhashchandran2710e732017-06-19 06:43:22 -050074 attemptsLeft(BOOT_COUNT_MAX_ALLOWED);
75
Andrew Geissleref3c1842016-12-01 12:33:09 -060076 // We deferred this until we could get our property correct
77 this->emit_object_added();
78 }
79
Andrew Geissleref3c1842016-12-01 12:33:09 -060080 /** @brief Set value of HostTransition */
81 Transition requestedHostTransition(Transition value) override;
82
83 /** @brief Set value of CurrentHostState */
84 HostState currentHostState(HostState value) override;
Andrew Geissler36529022016-11-29 15:23:54 -060085
Andrew Geissler7b90a622017-08-08 11:41:08 -050086 /**
87 * @brief Set host reboot count to default
88 *
89 * OpenBMC software controls the number of allowed reboot attempts so
90 * any external set request of this property will be overridden by
91 * this function and set to the default.
92 *
93 * The only code responsible for decrementing the boot count resides
94 * within this process and that will use the sub class interface
95 * directly
96 *
97 * @param[in] value - Reboot count value, will be ignored
98 *
99 * @return Default number of reboot attempts left
100 */
101 uint32_t attemptsLeft(uint32_t value) override
102 {
103 log<level::DEBUG>("External request to reset reboot count");
104 return (sdbusplus::xyz::openbmc_project::Control::Boot::server::
105 RebootAttempts::attemptsLeft(BOOT_COUNT_MAX_ALLOWED));
106 }
107
Andrew Geissler36529022016-11-29 15:23:54 -0600108 private:
Andrew Geissler4da7e002017-01-24 15:21:40 -0600109 /**
110 * @brief subscribe to the systemd signals
111 *
112 * This object needs to capture when it's systemd targets complete
113 * so it can keep it's state updated
114 *
115 **/
116 void subscribeToSystemdSignals();
117
118 /**
119 * @brief Determine initial host state and set internally
120 *
121 * @return Will throw exceptions on failure
122 **/
123 void determineInitialState();
124
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600125 /** @brief Execute the transition request
126 *
127 * This function assumes the state has been validated and the host
128 * is in an appropriate state for the transition to be started.
129 *
130 * @param[in] tranReq - Transition requested
131 */
132 void executeTransition(Transition tranReq);
133
Michael Tritz206a8332017-02-06 16:01:23 -0600134 /**
Josh D. King929ef702017-03-02 10:58:11 -0600135 * @brief Determine if target is active
136 *
137 * This function determines if the target is active and
138 * helps prevent misleading log recorded states.
139 *
140 * @param[in] target - Target string to check on
141 *
142 * @return boolean corresponding to state active
143 **/
144 bool stateActive(const std::string& target);
145
146 /**
Michael Tritz206a8332017-02-06 16:01:23 -0600147 * @brief Determine if auto reboot flag is set
148 *
149 * @return boolean corresponding to current auto_reboot setting
150 **/
151 bool isAutoReboot();
152
Andrew Geissler4da7e002017-01-24 15:21:40 -0600153 /** @brief Check if systemd state change is relevant to this object
154 *
155 * Instance specific interface to handle the detected systemd state
156 * change
157 *
158 * @param[in] msg - Data associated with subscribed signal
Andrew Geissler4da7e002017-01-24 15:21:40 -0600159 *
160 */
Patrick Williamsd22706f2017-05-04 05:42:49 -0500161 void sysStateChange(sdbusplus::message::message& msg);
Andrew Geissler4da7e002017-01-24 15:21:40 -0600162
Andrew Geissler7b90a622017-08-08 11:41:08 -0500163 /** @brief Decrement reboot count
164 *
165 * This is used internally to this application to decrement the boot
166 * count on each boot attempt. The host will use the external
167 * attemptsLeft() interface to reset the count when a boot is successful
168 *
169 * @return number of reboot count attempts left
170 */
171 uint32_t decrementRebootCount();
172
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500173 // Allow cereal class access to allow these next two function to be
174 // private
175 friend class cereal::access;
176
177 /** @brief Function required by Cereal to perform serialization.
178 *
179 * @tparam Archive - Cereal archive type (binary in our case).
180 * @param[in] archive - reference to Cereal archive.
181 */
182 template<class Archive>
183 void save(Archive& archive) const
184 {
185 archive(convertForMessage(sdbusplus::xyz::openbmc_project::
186 State::server::Host::
187 requestedHostTransition()));
188 }
189
190 /** @brief Function required by Cereal to perform deserialization.
191 *
192 * @tparam Archive - Cereal archive type (binary in our case).
193 * @param[in] archive - reference to Cereal archive.
194 */
195 template<class Archive>
196 void load(Archive& archive)
197 {
198 std::string str;
199 archive(str);
200 auto reqTran = Host::convertTransitionFromString(str);
201 // When restoring, set the requested state with persistent value
202 // but don't call the override which would execute it
203 sdbusplus::xyz::openbmc_project::State::server::Host::
204 requestedHostTransition(reqTran);
205 }
206
207 /** @brief Serialize and persist requested host state
208 *
209 * @param[in] dir - pathname of file where the serialized host state will
210 * be placed.
211 *
212 * @return fs::path - pathname of persisted requested host state.
213 */
214 fs::path serialize(const fs::path& dir =
215 fs::path(HOST_STATE_PERSIST_PATH));
216
217 /** @brief Deserialze a persisted requested host state.
218 *
219 * @param[in] path - pathname of persisted host state file
220 *
221 * @return bool - true if the deserialization was successful, false
222 * otherwise.
223 */
224 bool deserialize(const fs::path& path);
225
Andrew Geissleref3c1842016-12-01 12:33:09 -0600226 /** @brief Persistent sdbusplus DBus bus connection. */
227 sdbusplus::bus::bus& bus;
Andrew Geissleref621162016-12-08 12:56:21 -0600228
Andrew Geissler4da7e002017-01-24 15:21:40 -0600229 /** @brief Used to subscribe to dbus systemd signals **/
Patrick Williamsd22706f2017-05-04 05:42:49 -0500230 sdbusplus::bus::match_t systemdSignals;
Deepak Kodihalli3dd08a52017-07-25 07:34:44 -0500231
232 // Settings objects of interest
233 settings::Objects settings;
Andrew Geissler36529022016-11-29 15:23:54 -0600234};
235
236} // namespace manager
237} // namespace state
238} // namespace phosphor