blob: a52d184ab75452c5636dc56d1ad43724e314204d [file] [log] [blame]
Josh D. Kingbdd9cb72016-12-19 11:13:43 -06001#pragma once
2
Andrew Geissler928bbf12023-02-14 13:30:14 -06003#include "utils.hpp"
Josh D. Kingbdd9cb72016-12-19 11:13:43 -06004#include "xyz/openbmc_project/State/BMC/server.hpp"
5
Tim Lee2bfb1ef2021-03-17 09:50:35 +08006#include <linux/watchdog.h>
7
Andrew Geisslere426b582020-05-28 12:40:55 -05008#include <sdbusplus/bus.hpp>
9
Josh D. Kingbdd9cb72016-12-19 11:13:43 -060010namespace phosphor
11{
12namespace state
13{
14namespace manager
15{
16
Patrick Williamsf053e6f2022-07-22 19:26:54 -050017using BMCInherit = sdbusplus::server::object_t<
Andrew Geissler58a18012018-01-19 19:36:05 -080018 sdbusplus::xyz::openbmc_project::State::server::BMC>;
Patrick Williamsd32f8182017-05-05 15:55:24 -050019namespace sdbusRule = sdbusplus::bus::match::rules;
20
Josh D. Kingbdd9cb72016-12-19 11:13:43 -060021/** @class BMC
22 * @brief OpenBMC BMC state management implementation.
23 * @details A concrete implementation for xyz.openbmc_project.State.BMC
24 * DBus API.
25 */
Patrick Williamsd32f8182017-05-05 15:55:24 -050026class BMC : public BMCInherit
Josh D. Kingbdd9cb72016-12-19 11:13:43 -060027{
Andrew Geissler58a18012018-01-19 19:36:05 -080028 public:
29 /** @brief Constructs BMC State Manager
30 *
31 * @param[in] bus - The Dbus bus object
32 * @param[in] busName - The Dbus name to own
33 * @param[in] objPath - The Dbus object path
34 */
Patrick Williamsf053e6f2022-07-22 19:26:54 -050035 BMC(sdbusplus::bus_t& bus, const char* objPath) :
Patrick Williams76070742022-04-05 15:20:12 -050036 BMCInherit(bus, objPath, BMCInherit::action::defer_emit), bus(bus),
Andrew Geissler58a18012018-01-19 19:36:05 -080037 stateSignal(std::make_unique<decltype(stateSignal)::element_type>(
38 bus,
39 sdbusRule::type::signal() + sdbusRule::member("JobRemoved") +
40 sdbusRule::path("/org/freedesktop/systemd1") +
41 sdbusRule::interface("org.freedesktop.systemd1.Manager"),
William A. Kennington IIIbd28f022022-11-22 17:11:49 -080042 [this](sdbusplus::message_t& m) { bmcStateChange(m); }))
Andrew Geissler58a18012018-01-19 19:36:05 -080043 {
Andrew Geissler928bbf12023-02-14 13:30:14 -060044 utils::subscribeToSystemdSignals(bus);
Andrew Geissler58a18012018-01-19 19:36:05 -080045 discoverInitialState();
Tim Lee2bfb1ef2021-03-17 09:50:35 +080046 discoverLastRebootCause();
Andrew Geissler58a18012018-01-19 19:36:05 -080047 this->emit_object_added();
48 };
Josh D. King6db38222016-12-19 14:52:40 -060049
Andrew Geissler58a18012018-01-19 19:36:05 -080050 /** @brief Set value of BMCTransition **/
51 Transition requestedBMCTransition(Transition value) override;
Josh D. King6db38222016-12-19 14:52:40 -060052
Andrew Geissler58a18012018-01-19 19:36:05 -080053 /** @brief Set value of CurrentBMCState **/
54 BMCState currentBMCState(BMCState value) override;
Josh D. King6db38222016-12-19 14:52:40 -060055
Matt Spinlere6710b72018-07-12 16:05:55 -050056 /** @brief Returns the last time the BMC was rebooted
57 *
58 * @details Uses uptime information to determine when
59 * the BMC was last rebooted.
60 *
61 * @return uint64_t - Epoch time, in milliseconds, of the
62 * last reboot.
63 */
64 uint64_t lastRebootTime() const override;
65
Tim Lee2bfb1ef2021-03-17 09:50:35 +080066 /** @brief Set value of LastRebootCause **/
67 RebootCause lastRebootCause(RebootCause value) override;
68
Andrew Geissler58a18012018-01-19 19:36:05 -080069 private:
70 /**
Andrew Geissler2774c782022-02-17 16:57:14 -060071 * @brief Retrieve input systemd unit state
72 **/
73 std::string getUnitState(const std::string& unitToCheck);
74 /**
Andrew Geissler58a18012018-01-19 19:36:05 -080075 * @brief discover the state of the bmc
76 **/
77 void discoverInitialState();
Josh D. Kingd3e58472017-02-02 11:09:11 -060078
Andrew Geissler58a18012018-01-19 19:36:05 -080079 /** @brief Execute the transition request
80 *
81 * @param[in] tranReq - Transition requested
82 */
83 void executeTransition(Transition tranReq);
Josh D. King5162a7b2016-12-19 16:15:00 -060084
Andrew Geissler58a18012018-01-19 19:36:05 -080085 /** @brief Callback function on bmc state change
86 *
87 * Check if the state is relevant to the BMC and if so, update
88 * corresponding BMC object's state
89 *
90 * @param[in] msg - Data associated with subscribed signal
91 *
92 */
Patrick Williamsf053e6f2022-07-22 19:26:54 -050093 int bmcStateChange(sdbusplus::message_t& msg);
Josh D. Kingd613b812016-12-19 16:47:45 -060094
Andrew Geissler58a18012018-01-19 19:36:05 -080095 /** @brief Persistent sdbusplus DBus bus connection. **/
Patrick Williamsf053e6f2022-07-22 19:26:54 -050096 sdbusplus::bus_t& bus;
Josh D. King6db38222016-12-19 14:52:40 -060097
Andrew Geissler58a18012018-01-19 19:36:05 -080098 /** @brief Used to subscribe to dbus system state changes **/
99 std::unique_ptr<sdbusplus::bus::match_t> stateSignal;
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800100
101 /**
102 * @brief discover the last reboot cause of the bmc
103 **/
104 void discoverLastRebootCause();
Josh D. Kingbdd9cb72016-12-19 11:13:43 -0600105};
106
107} // namespace manager
108} // namespace state
109} // namespace phosphor