blob: b281602e4fa21066acd94cd6f8fd2320ecaf32f7 [file] [log] [blame]
Alexander Hansen8c99fcf2025-07-24 12:27:05 +02001#include "power_status_monitor.hpp"
2
Alexander Hansen0a4bb2a2025-07-25 11:34:48 +02003#include "phosphor-logging/lg2.hpp"
Alexander Hansen8c99fcf2025-07-24 12:27:05 +02004#include "utils.hpp"
5
Alexander Hansen8c99fcf2025-07-24 12:27:05 +02006#include <sdbusplus/bus/match.hpp>
7
Alexander Hansen0a4bb2a2025-07-25 11:34:48 +02008#include <flat_map>
9
Alexander Hansen8c99fcf2025-07-24 12:27:05 +020010namespace power
11{
12
13const static constexpr char* busname = "xyz.openbmc_project.State.Host";
14const static constexpr char* interface = "xyz.openbmc_project.State.Host";
15const static constexpr char* path = "/xyz/openbmc_project/state/host0";
16const static constexpr char* property = "CurrentHostState";
17
Alexander Hansene1646272025-07-25 11:47:17 +020018PowerStatusMonitor::PowerStatusMonitor(sdbusplus::asio::connection& conn) :
19
20 powerMatch(static_cast<sdbusplus::bus_t&>(conn),
21 "type='signal',interface='" +
22 std::string(em_utils::properties::interface) + "',path='" +
23 std::string(power::path) + "',arg0='" +
24 std::string(power::interface) + "'",
25 std::bind_front(&PowerStatusMonitor::handlePowerMatch, this))
26
Alexander Hansen8c99fcf2025-07-24 12:27:05 +020027{
Alexander Hansene1646272025-07-25 11:47:17 +020028 getInitialPowerStatus(conn);
29}
30
31bool PowerStatusMonitor::isPowerOn() const
32{
Alexander Hansen8c99fcf2025-07-24 12:27:05 +020033 return powerStatusOn;
34}
35
Alexander Hansen0a4bb2a2025-07-25 11:34:48 +020036void PowerStatusMonitor::handlePowerMatch(sdbusplus::message_t& message)
37{
38 lg2::debug("power match triggered");
39
40 std::string objectName;
41 std::flat_map<std::string, std::variant<std::string>> values;
42 message.read(objectName, values);
43 auto findState = values.find(power::property);
44 if (findState != values.end())
45 {
46 powerStatusOn =
47 std::get<std::string>(findState->second).ends_with("Running");
48 }
49}
50
Alexander Hansene1646272025-07-25 11:47:17 +020051void PowerStatusMonitor::getInitialPowerStatus(
52 sdbusplus::asio::connection& conn)
Alexander Hansen8c99fcf2025-07-24 12:27:05 +020053{
Alexander Hansene1646272025-07-25 11:47:17 +020054 conn.async_method_call(
Alexander Hansen8c99fcf2025-07-24 12:27:05 +020055 [this](boost::system::error_code ec,
56 const std::variant<std::string>& state) {
57 if (ec)
58 {
59 return;
60 }
Alexander Hansen0a4bb2a2025-07-25 11:34:48 +020061 powerStatusOn = std::get<std::string>(state).ends_with("Running");
Alexander Hansen8c99fcf2025-07-24 12:27:05 +020062 },
63 power::busname, power::path, em_utils::properties::interface,
64 em_utils::properties::get, power::interface, power::property);
65}
66
67} // namespace power