blob: 026cb3fa7879df30d4ed4ca27a9f6506dd8b5c31 [file] [log] [blame]
Josh D. Kingbdd9cb72016-12-19 11:13:43 -06001#pragma once
2
Josh D. Kingbdd9cb72016-12-19 11:13:43 -06003#include "xyz/openbmc_project/State/BMC/server.hpp"
4
Tim Lee2bfb1ef2021-03-17 09:50:35 +08005#include <linux/watchdog.h>
6
Andrew Geisslere426b582020-05-28 12:40:55 -05007#include <sdbusplus/bus.hpp>
8
9#include <functional>
10
Josh D. Kingbdd9cb72016-12-19 11:13:43 -060011namespace phosphor
12{
13namespace state
14{
15namespace manager
16{
17
Patrick Williamsf053e6f2022-07-22 19:26:54 -050018using BMCInherit = sdbusplus::server::object_t<
Andrew Geissler58a18012018-01-19 19:36:05 -080019 sdbusplus::xyz::openbmc_project::State::server::BMC>;
Patrick Williamsd32f8182017-05-05 15:55:24 -050020namespace sdbusRule = sdbusplus::bus::match::rules;
21
Josh D. Kingbdd9cb72016-12-19 11:13:43 -060022/** @class BMC
23 * @brief OpenBMC BMC state management implementation.
24 * @details A concrete implementation for xyz.openbmc_project.State.BMC
25 * DBus API.
26 */
Patrick Williamsd32f8182017-05-05 15:55:24 -050027class BMC : public BMCInherit
Josh D. Kingbdd9cb72016-12-19 11:13:43 -060028{
Andrew Geissler58a18012018-01-19 19:36:05 -080029 public:
30 /** @brief Constructs BMC State Manager
31 *
32 * @param[in] bus - The Dbus bus object
33 * @param[in] busName - The Dbus name to own
34 * @param[in] objPath - The Dbus object path
35 */
Patrick Williamsf053e6f2022-07-22 19:26:54 -050036 BMC(sdbusplus::bus_t& bus, const char* objPath) :
Patrick Williams76070742022-04-05 15:20:12 -050037 BMCInherit(bus, objPath, BMCInherit::action::defer_emit), bus(bus),
Andrew Geissler58a18012018-01-19 19:36:05 -080038 stateSignal(std::make_unique<decltype(stateSignal)::element_type>(
39 bus,
40 sdbusRule::type::signal() + sdbusRule::member("JobRemoved") +
41 sdbusRule::path("/org/freedesktop/systemd1") +
42 sdbusRule::interface("org.freedesktop.systemd1.Manager"),
43 std::bind(std::mem_fn(&BMC::bmcStateChange), this,
44 std::placeholders::_1)))
45 {
46 subscribeToSystemdSignals();
47 discoverInitialState();
Tim Lee2bfb1ef2021-03-17 09:50:35 +080048 discoverLastRebootCause();
Andrew Geissler58a18012018-01-19 19:36:05 -080049 this->emit_object_added();
50 };
Josh D. King6db38222016-12-19 14:52:40 -060051
Andrew Geissler58a18012018-01-19 19:36:05 -080052 /** @brief Set value of BMCTransition **/
53 Transition requestedBMCTransition(Transition value) override;
Josh D. King6db38222016-12-19 14:52:40 -060054
Andrew Geissler58a18012018-01-19 19:36:05 -080055 /** @brief Set value of CurrentBMCState **/
56 BMCState currentBMCState(BMCState value) override;
Josh D. King6db38222016-12-19 14:52:40 -060057
Matt Spinlere6710b72018-07-12 16:05:55 -050058 /** @brief Returns the last time the BMC was rebooted
59 *
60 * @details Uses uptime information to determine when
61 * the BMC was last rebooted.
62 *
63 * @return uint64_t - Epoch time, in milliseconds, of the
64 * last reboot.
65 */
66 uint64_t lastRebootTime() const override;
67
Tim Lee2bfb1ef2021-03-17 09:50:35 +080068 /** @brief Set value of LastRebootCause **/
69 RebootCause lastRebootCause(RebootCause value) override;
70
Andrew Geissler58a18012018-01-19 19:36:05 -080071 private:
72 /**
Andrew Geissler2774c782022-02-17 16:57:14 -060073 * @brief Retrieve input systemd unit state
74 **/
75 std::string getUnitState(const std::string& unitToCheck);
76 /**
Andrew Geissler58a18012018-01-19 19:36:05 -080077 * @brief discover the state of the bmc
78 **/
79 void discoverInitialState();
Josh D. Kingd3e58472017-02-02 11:09:11 -060080
Andrew Geissler58a18012018-01-19 19:36:05 -080081 /**
82 * @brief subscribe to the systemd signals
83 **/
84 void subscribeToSystemdSignals();
Josh D. King6db38222016-12-19 14:52:40 -060085
Andrew Geissler58a18012018-01-19 19:36:05 -080086 /** @brief Execute the transition request
87 *
88 * @param[in] tranReq - Transition requested
89 */
90 void executeTransition(Transition tranReq);
Josh D. King5162a7b2016-12-19 16:15:00 -060091
Andrew Geissler58a18012018-01-19 19:36:05 -080092 /** @brief Callback function on bmc state change
93 *
94 * Check if the state is relevant to the BMC and if so, update
95 * corresponding BMC object's state
96 *
97 * @param[in] msg - Data associated with subscribed signal
98 *
99 */
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500100 int bmcStateChange(sdbusplus::message_t& msg);
Josh D. Kingd613b812016-12-19 16:47:45 -0600101
Andrew Geissler58a18012018-01-19 19:36:05 -0800102 /** @brief Persistent sdbusplus DBus bus connection. **/
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500103 sdbusplus::bus_t& bus;
Josh D. King6db38222016-12-19 14:52:40 -0600104
Andrew Geissler58a18012018-01-19 19:36:05 -0800105 /** @brief Used to subscribe to dbus system state changes **/
106 std::unique_ptr<sdbusplus::bus::match_t> stateSignal;
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800107
108 /**
109 * @brief discover the last reboot cause of the bmc
110 **/
111 void discoverLastRebootCause();
Josh D. Kingbdd9cb72016-12-19 11:13:43 -0600112};
113
114} // namespace manager
115} // namespace state
116} // namespace phosphor