Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Andrew Geissler | 928bbf1 | 2023-02-14 13:30:14 -0600 | [diff] [blame] | 3 | #include "utils.hpp" |
Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 4 | #include "xyz/openbmc_project/State/BMC/server.hpp" |
| 5 | |
Tim Lee | 2bfb1ef | 2021-03-17 09:50:35 +0800 | [diff] [blame] | 6 | #include <linux/watchdog.h> |
Willy Tu | 564eb4f | 2023-09-07 16:02:46 -0700 | [diff] [blame] | 7 | #include <sys/sysinfo.h> |
Tim Lee | 2bfb1ef | 2021-03-17 09:50:35 +0800 | [diff] [blame] | 8 | |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 9 | #include <sdbusplus/bus.hpp> |
| 10 | |
Willy Tu | 564eb4f | 2023-09-07 16:02:46 -0700 | [diff] [blame] | 11 | #include <cassert> |
| 12 | #include <chrono> |
| 13 | |
Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 14 | namespace phosphor |
| 15 | { |
| 16 | namespace state |
| 17 | { |
| 18 | namespace manager |
| 19 | { |
| 20 | |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 21 | using BMCInherit = sdbusplus::server::object_t< |
Patrick Williams | 7e969cb | 2023-08-23 16:24:23 -0500 | [diff] [blame] | 22 | sdbusplus::server::xyz::openbmc_project::state::BMC>; |
Patrick Williams | d32f818 | 2017-05-05 15:55:24 -0500 | [diff] [blame] | 23 | namespace sdbusRule = sdbusplus::bus::match::rules; |
| 24 | |
Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 25 | /** @class BMC |
| 26 | * @brief OpenBMC BMC state management implementation. |
| 27 | * @details A concrete implementation for xyz.openbmc_project.State.BMC |
| 28 | * DBus API. |
| 29 | */ |
Patrick Williams | d32f818 | 2017-05-05 15:55:24 -0500 | [diff] [blame] | 30 | class BMC : public BMCInherit |
Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 31 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 32 | public: |
| 33 | /** @brief Constructs BMC State Manager |
| 34 | * |
| 35 | * @param[in] bus - The Dbus bus object |
| 36 | * @param[in] busName - The Dbus name to own |
| 37 | * @param[in] objPath - The Dbus object path |
| 38 | */ |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 39 | BMC(sdbusplus::bus_t& bus, const char* objPath) : |
Patrick Williams | 7607074 | 2022-04-05 15:20:12 -0500 | [diff] [blame] | 40 | BMCInherit(bus, objPath, BMCInherit::action::defer_emit), bus(bus), |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 41 | stateSignal(std::make_unique<decltype(stateSignal)::element_type>( |
| 42 | bus, |
| 43 | sdbusRule::type::signal() + sdbusRule::member("JobRemoved") + |
| 44 | sdbusRule::path("/org/freedesktop/systemd1") + |
| 45 | sdbusRule::interface("org.freedesktop.systemd1.Manager"), |
Willy Tu | bd1eebd | 2023-10-05 12:14:20 -0700 | [diff] [blame] | 46 | [this](sdbusplus::message_t& m) { bmcStateChange(m); })), |
| 47 | |
| 48 | timeSyncSignal(std::make_unique<decltype(timeSyncSignal)::element_type>( |
| 49 | bus, |
| 50 | sdbusRule::propertiesChanged( |
| 51 | "/org/freedesktop/systemd1/unit/time_2dsync_2etarget", |
| 52 | "org.freedesktop.systemd1.Unit"), |
| 53 | [this](sdbusplus::message_t& m) { |
Patrick Williams | 1b2c3c0 | 2024-08-16 15:20:29 -0400 | [diff] [blame^] | 54 | std::string interface; |
| 55 | std::unordered_map<std::string, std::variant<std::string>> |
| 56 | propertyChanged; |
| 57 | m.read(interface, propertyChanged); |
Willy Tu | bd1eebd | 2023-10-05 12:14:20 -0700 | [diff] [blame] | 58 | |
Patrick Williams | 1b2c3c0 | 2024-08-16 15:20:29 -0400 | [diff] [blame^] | 59 | for (const auto& [key, value] : propertyChanged) |
| 60 | { |
| 61 | if (key == "ActiveState" && |
| 62 | std::holds_alternative<std::string>(value) && |
| 63 | std::get<std::string>(value) == "active") |
| 64 | { |
| 65 | updateLastRebootTime(); |
| 66 | timeSyncSignal.reset(); |
| 67 | } |
| 68 | } |
| 69 | })) |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 70 | { |
Andrew Geissler | 928bbf1 | 2023-02-14 13:30:14 -0600 | [diff] [blame] | 71 | utils::subscribeToSystemdSignals(bus); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 72 | discoverInitialState(); |
Tim Lee | 2bfb1ef | 2021-03-17 09:50:35 +0800 | [diff] [blame] | 73 | discoverLastRebootCause(); |
Willy Tu | bd1eebd | 2023-10-05 12:14:20 -0700 | [diff] [blame] | 74 | updateLastRebootTime(); |
Willy Tu | 564eb4f | 2023-09-07 16:02:46 -0700 | [diff] [blame] | 75 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 76 | this->emit_object_added(); |
| 77 | }; |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 78 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 79 | /** @brief Set value of BMCTransition **/ |
| 80 | Transition requestedBMCTransition(Transition value) override; |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 81 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 82 | /** @brief Set value of CurrentBMCState **/ |
| 83 | BMCState currentBMCState(BMCState value) override; |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 84 | |
Matt Spinler | e6710b7 | 2018-07-12 16:05:55 -0500 | [diff] [blame] | 85 | /** @brief Returns the last time the BMC was rebooted |
| 86 | * |
| 87 | * @details Uses uptime information to determine when |
| 88 | * the BMC was last rebooted. |
| 89 | * |
| 90 | * @return uint64_t - Epoch time, in milliseconds, of the |
| 91 | * last reboot. |
| 92 | */ |
| 93 | uint64_t lastRebootTime() const override; |
| 94 | |
Tim Lee | 2bfb1ef | 2021-03-17 09:50:35 +0800 | [diff] [blame] | 95 | /** @brief Set value of LastRebootCause **/ |
| 96 | RebootCause lastRebootCause(RebootCause value) override; |
| 97 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 98 | private: |
| 99 | /** |
Andrew Geissler | 2774c78 | 2022-02-17 16:57:14 -0600 | [diff] [blame] | 100 | * @brief Retrieve input systemd unit state |
| 101 | **/ |
| 102 | std::string getUnitState(const std::string& unitToCheck); |
| 103 | /** |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 104 | * @brief discover the state of the bmc |
| 105 | **/ |
| 106 | void discoverInitialState(); |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 107 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 108 | /** @brief Execute the transition request |
| 109 | * |
| 110 | * @param[in] tranReq - Transition requested |
| 111 | */ |
| 112 | void executeTransition(Transition tranReq); |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 113 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 114 | /** @brief Callback function on bmc state change |
| 115 | * |
| 116 | * Check if the state is relevant to the BMC and if so, update |
| 117 | * corresponding BMC object's state |
| 118 | * |
| 119 | * @param[in] msg - Data associated with subscribed signal |
| 120 | * |
| 121 | */ |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 122 | int bmcStateChange(sdbusplus::message_t& msg); |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 123 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 124 | /** @brief Persistent sdbusplus DBus bus connection. **/ |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 125 | sdbusplus::bus_t& bus; |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 126 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 127 | /** @brief Used to subscribe to dbus system state changes **/ |
| 128 | std::unique_ptr<sdbusplus::bus::match_t> stateSignal; |
Tim Lee | 2bfb1ef | 2021-03-17 09:50:35 +0800 | [diff] [blame] | 129 | |
Willy Tu | bd1eebd | 2023-10-05 12:14:20 -0700 | [diff] [blame] | 130 | /** @brief Used to subscribe to timesync **/ |
| 131 | std::unique_ptr<sdbusplus::bus::match_t> timeSyncSignal; |
| 132 | |
Tim Lee | 2bfb1ef | 2021-03-17 09:50:35 +0800 | [diff] [blame] | 133 | /** |
| 134 | * @brief discover the last reboot cause of the bmc |
| 135 | **/ |
| 136 | void discoverLastRebootCause(); |
Willy Tu | 564eb4f | 2023-09-07 16:02:46 -0700 | [diff] [blame] | 137 | |
| 138 | /** |
Willy Tu | bd1eebd | 2023-10-05 12:14:20 -0700 | [diff] [blame] | 139 | * @brief update the last reboot time of the bmc |
| 140 | **/ |
| 141 | void updateLastRebootTime(); |
| 142 | |
| 143 | /** |
Manojkiran Eda | 3ff5a36 | 2024-06-17 10:59:07 +0530 | [diff] [blame] | 144 | * @brief the lastRebootTime calculated at startup. |
Willy Tu | 564eb4f | 2023-09-07 16:02:46 -0700 | [diff] [blame] | 145 | **/ |
| 146 | uint64_t rebootTime; |
Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | } // namespace manager |
| 150 | } // namespace state |
| 151 | } // namespace phosphor |