blob: 079c3207deda77c3320a7919d41388d98e83a410 [file] [log] [blame]
Alexander Hansen8c99fcf2025-07-24 12:27:05 +02001#include "power_status_monitor.hpp"
2
3#include "utils.hpp"
4
Ed Tanousdd5732d2025-10-13 10:50:26 -07005#include <phosphor-logging/lg2.hpp>
Alexander Hansen8c99fcf2025-07-24 12:27:05 +02006#include <sdbusplus/bus/match.hpp>
Alexander Hansenfa88c5e2025-09-12 12:33:16 +02007#include <xyz/openbmc_project/State/Host/client.hpp>
Alexander Hansen8c99fcf2025-07-24 12:27:05 +02008
Alexander Hansen0a4bb2a2025-07-25 11:34:48 +02009#include <flat_map>
10
Alexander Hansen8c99fcf2025-07-24 12:27:05 +020011namespace power
12{
13
Alexander Hansenfa88c5e2025-09-12 12:33:16 +020014using HostState = sdbusplus::common::xyz::openbmc_project::state::Host;
15
16const static std::string path =
17 std::format("{}/{}{}", HostState::namespace_path::value,
18 HostState::namespace_path::host, 0);
Alexander Hansen8c99fcf2025-07-24 12:27:05 +020019const static constexpr char* property = "CurrentHostState";
20
Alexander Hansene1646272025-07-25 11:47:17 +020021PowerStatusMonitor::PowerStatusMonitor(sdbusplus::asio::connection& conn) :
22
23 powerMatch(static_cast<sdbusplus::bus_t&>(conn),
24 "type='signal',interface='" +
25 std::string(em_utils::properties::interface) + "',path='" +
26 std::string(power::path) + "',arg0='" +
Alexander Hansenfa88c5e2025-09-12 12:33:16 +020027 std::string(HostState::interface) + "'",
Alexander Hansene1646272025-07-25 11:47:17 +020028 std::bind_front(&PowerStatusMonitor::handlePowerMatch, this))
29
Alexander Hansen8c99fcf2025-07-24 12:27:05 +020030{
Alexander Hansene1646272025-07-25 11:47:17 +020031 getInitialPowerStatus(conn);
32}
33
34bool PowerStatusMonitor::isPowerOn() const
35{
Alexander Hansen8c99fcf2025-07-24 12:27:05 +020036 return powerStatusOn;
37}
38
Alexander Hansen0a4bb2a2025-07-25 11:34:48 +020039void PowerStatusMonitor::handlePowerMatch(sdbusplus::message_t& message)
40{
41 lg2::debug("power match triggered");
42
43 std::string objectName;
44 std::flat_map<std::string, std::variant<std::string>> values;
45 message.read(objectName, values);
46 auto findState = values.find(power::property);
47 if (findState != values.end())
48 {
49 powerStatusOn =
50 std::get<std::string>(findState->second).ends_with("Running");
51 }
52}
53
Alexander Hansene1646272025-07-25 11:47:17 +020054void PowerStatusMonitor::getInitialPowerStatus(
55 sdbusplus::asio::connection& conn)
Alexander Hansen8c99fcf2025-07-24 12:27:05 +020056{
Alexander Hansenfa88c5e2025-09-12 12:33:16 +020057 lg2::debug("querying initial power state");
58
Alexander Hansene1646272025-07-25 11:47:17 +020059 conn.async_method_call(
Alexander Hansen8c99fcf2025-07-24 12:27:05 +020060 [this](boost::system::error_code ec,
61 const std::variant<std::string>& state) {
62 if (ec)
63 {
64 return;
65 }
Alexander Hansen0a4bb2a2025-07-25 11:34:48 +020066 powerStatusOn = std::get<std::string>(state).ends_with("Running");
Alexander Hansen8c99fcf2025-07-24 12:27:05 +020067 },
Alexander Hansenfa88c5e2025-09-12 12:33:16 +020068 HostState::interface, power::path, em_utils::properties::interface,
69 em_utils::properties::get, HostState::interface, power::property);
Alexander Hansen8c99fcf2025-07-24 12:27:05 +020070}
71
72} // namespace power