blob: af748f5d7ea4bbb22f6683978cd56357e403a5eb [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 Geissler36529022016-11-29 15:23:54 -06005#include <sdbusplus/bus.hpp>
Andrew Geissler7b90a622017-08-08 11:41:08 -05006#include <phosphor-logging/log.hpp>
Dhruvaraj Subhashchandran2710e732017-06-19 06:43:22 -05007#include <xyz/openbmc_project/State/Boot/Progress/server.hpp>
8#include <xyz/openbmc_project/Control/Boot/RebootAttempts/server.hpp>
9#include <xyz/openbmc_project/State/OperatingSystem/Status/server.hpp>
Andrew Geissler36529022016-11-29 15:23:54 -060010#include "xyz/openbmc_project/State/Host/server.hpp"
Deepak Kodihalli3dd08a52017-07-25 07:34:44 -050011#include "settings.hpp"
Dhruvaraj Subhashchandran2710e732017-06-19 06:43:22 -050012#include "config.h"
Andrew Geissler36529022016-11-29 15:23:54 -060013
14namespace phosphor
15{
16namespace state
17{
18namespace manager
19{
20
Patrick Williamsd22706f2017-05-04 05:42:49 -050021using HostInherit = sdbusplus::server::object::object<
Dhruvaraj Subhashchandran2710e732017-06-19 06:43:22 -050022 sdbusplus::xyz::openbmc_project::State::server::Host,
23 sdbusplus::xyz::openbmc_project::State::Boot::server::Progress,
24 sdbusplus::xyz::openbmc_project::Control::Boot::server::RebootAttempts,
25 sdbusplus::xyz::openbmc_project::State::OperatingSystem::server::Status>;
26
Andrew Geissler7b90a622017-08-08 11:41:08 -050027using namespace phosphor::logging;
28
Patrick Williamsd22706f2017-05-04 05:42:49 -050029namespace sdbusRule = sdbusplus::bus::match::rules;
30
Andrew Geissler36529022016-11-29 15:23:54 -060031/** @class Host
32 * @brief OpenBMC host state management implementation.
33 * @details A concrete implementation for xyz.openbmc_project.State.Host
34 * DBus API.
35 */
Patrick Williamsd22706f2017-05-04 05:42:49 -050036class Host : public HostInherit
Andrew Geissler36529022016-11-29 15:23:54 -060037{
38 public:
39 /** @brief Constructs Host State Manager
40 *
Andrew Geissleref3c1842016-12-01 12:33:09 -060041 * @note This constructor passes 'true' to the base class in order to
42 * defer dbus object registration until we can run
43 * determineInitialState() and set our properties
44 *
Andrew Geissler36529022016-11-29 15:23:54 -060045 * @param[in] bus - The Dbus bus object
46 * @param[in] busName - The Dbus name to own
47 * @param[in] objPath - The Dbus object path
48 */
49 Host(sdbusplus::bus::bus& bus,
Patrick Williamsd22706f2017-05-04 05:42:49 -050050 const char* busName,
51 const char* objPath) :
52 HostInherit(bus, objPath, true),
Andrew Geissleref621162016-12-08 12:56:21 -060053 bus(bus),
Patrick Williamsd22706f2017-05-04 05:42:49 -050054 systemdSignals(
55 bus,
56 sdbusRule::type::signal() +
57 sdbusRule::member("JobRemoved") +
58 sdbusRule::path("/org/freedesktop/systemd1") +
59 sdbusRule::interface(
60 "org.freedesktop.systemd1.Manager"),
61 std::bind(std::mem_fn(&Host::sysStateChange),
Deepak Kodihalli3dd08a52017-07-25 07:34:44 -050062 this, std::placeholders::_1)),
63 settings(bus)
Andrew Geissleref3c1842016-12-01 12:33:09 -060064 {
Andrew Geissler4da7e002017-01-24 15:21:40 -060065 // Enable systemd signals
66 subscribeToSystemdSignals();
67
Andrew Geissleref3c1842016-12-01 12:33:09 -060068 // Will throw exception on fail
69 determineInitialState();
70
Dhruvaraj Subhashchandran2710e732017-06-19 06:43:22 -050071 attemptsLeft(BOOT_COUNT_MAX_ALLOWED);
72
Andrew Geissleref3c1842016-12-01 12:33:09 -060073 // We deferred this until we could get our property correct
74 this->emit_object_added();
75 }
76
Andrew Geissleref3c1842016-12-01 12:33:09 -060077 /** @brief Set value of HostTransition */
78 Transition requestedHostTransition(Transition value) override;
79
80 /** @brief Set value of CurrentHostState */
81 HostState currentHostState(HostState value) override;
Andrew Geissler36529022016-11-29 15:23:54 -060082
Andrew Geissler7b90a622017-08-08 11:41:08 -050083 /**
84 * @brief Set host reboot count to default
85 *
86 * OpenBMC software controls the number of allowed reboot attempts so
87 * any external set request of this property will be overridden by
88 * this function and set to the default.
89 *
90 * The only code responsible for decrementing the boot count resides
91 * within this process and that will use the sub class interface
92 * directly
93 *
94 * @param[in] value - Reboot count value, will be ignored
95 *
96 * @return Default number of reboot attempts left
97 */
98 uint32_t attemptsLeft(uint32_t value) override
99 {
100 log<level::DEBUG>("External request to reset reboot count");
101 return (sdbusplus::xyz::openbmc_project::Control::Boot::server::
102 RebootAttempts::attemptsLeft(BOOT_COUNT_MAX_ALLOWED));
103 }
104
Andrew Geissler36529022016-11-29 15:23:54 -0600105 private:
Andrew Geissler4da7e002017-01-24 15:21:40 -0600106 /**
107 * @brief subscribe to the systemd signals
108 *
109 * This object needs to capture when it's systemd targets complete
110 * so it can keep it's state updated
111 *
112 **/
113 void subscribeToSystemdSignals();
114
115 /**
116 * @brief Determine initial host state and set internally
117 *
118 * @return Will throw exceptions on failure
119 **/
120 void determineInitialState();
121
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600122 /** @brief Execute the transition request
123 *
124 * This function assumes the state has been validated and the host
125 * is in an appropriate state for the transition to be started.
126 *
127 * @param[in] tranReq - Transition requested
128 */
129 void executeTransition(Transition tranReq);
130
Michael Tritz206a8332017-02-06 16:01:23 -0600131 /**
Josh D. King929ef702017-03-02 10:58:11 -0600132 * @brief Determine if target is active
133 *
134 * This function determines if the target is active and
135 * helps prevent misleading log recorded states.
136 *
137 * @param[in] target - Target string to check on
138 *
139 * @return boolean corresponding to state active
140 **/
141 bool stateActive(const std::string& target);
142
143 /**
Michael Tritz206a8332017-02-06 16:01:23 -0600144 * @brief Determine if auto reboot flag is set
145 *
146 * @return boolean corresponding to current auto_reboot setting
147 **/
148 bool isAutoReboot();
149
Andrew Geissler4da7e002017-01-24 15:21:40 -0600150 /** @brief Check if systemd state change is relevant to this object
151 *
152 * Instance specific interface to handle the detected systemd state
153 * change
154 *
155 * @param[in] msg - Data associated with subscribed signal
Andrew Geissler4da7e002017-01-24 15:21:40 -0600156 *
157 */
Patrick Williamsd22706f2017-05-04 05:42:49 -0500158 void sysStateChange(sdbusplus::message::message& msg);
Andrew Geissler4da7e002017-01-24 15:21:40 -0600159
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -0500160 /** @brief Determine whether restoring of host requested state is enabled
161 *
162 * @return boolean corresponding to restore setting
163 */
164 bool getStateRestoreSetting() const;
165
Andrew Geissler7b90a622017-08-08 11:41:08 -0500166 /** @brief Decrement reboot count
167 *
168 * This is used internally to this application to decrement the boot
169 * count on each boot attempt. The host will use the external
170 * attemptsLeft() interface to reset the count when a boot is successful
171 *
172 * @return number of reboot count attempts left
173 */
174 uint32_t decrementRebootCount();
175
Andrew Geissleref3c1842016-12-01 12:33:09 -0600176 /** @brief Persistent sdbusplus DBus bus connection. */
177 sdbusplus::bus::bus& bus;
Andrew Geissleref621162016-12-08 12:56:21 -0600178
Andrew Geissler4da7e002017-01-24 15:21:40 -0600179 /** @brief Used to subscribe to dbus systemd signals **/
Patrick Williamsd22706f2017-05-04 05:42:49 -0500180 sdbusplus::bus::match_t systemdSignals;
Deepak Kodihalli3dd08a52017-07-25 07:34:44 -0500181
182 // Settings objects of interest
183 settings::Objects settings;
Andrew Geissler36529022016-11-29 15:23:54 -0600184};
185
186} // namespace manager
187} // namespace state
188} // namespace phosphor