blob: 93617d29810709d9f0eb23edacc7afeea475a5a6 [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
18bool PowerStatusMonitor::isPowerOn()
19{
20 if (!powerMatch)
21 {
22 throw std::runtime_error("Power Match Not Created");
23 }
24 return powerStatusOn;
25}
26
Alexander Hansen0a4bb2a2025-07-25 11:34:48 +020027void PowerStatusMonitor::handlePowerMatch(sdbusplus::message_t& message)
28{
29 lg2::debug("power match triggered");
30
31 std::string objectName;
32 std::flat_map<std::string, std::variant<std::string>> values;
33 message.read(objectName, values);
34 auto findState = values.find(power::property);
35 if (findState != values.end())
36 {
37 powerStatusOn =
38 std::get<std::string>(findState->second).ends_with("Running");
39 }
40}
41
Alexander Hansen8c99fcf2025-07-24 12:27:05 +020042void PowerStatusMonitor::setupPowerMatch(
43 const std::shared_ptr<sdbusplus::asio::connection>& conn)
44{
45 powerMatch = std::make_unique<sdbusplus::bus::match_t>(
46 static_cast<sdbusplus::bus_t&>(*conn),
47 "type='signal',interface='" +
48 std::string(em_utils::properties::interface) + "',path='" +
49 std::string(power::path) + "',arg0='" +
50 std::string(power::interface) + "'",
Alexander Hansen0a4bb2a2025-07-25 11:34:48 +020051 std::bind_front(&PowerStatusMonitor::handlePowerMatch, this));
Alexander Hansen8c99fcf2025-07-24 12:27:05 +020052
53 conn->async_method_call(
54 [this](boost::system::error_code ec,
55 const std::variant<std::string>& state) {
56 if (ec)
57 {
58 return;
59 }
Alexander Hansen0a4bb2a2025-07-25 11:34:48 +020060 powerStatusOn = std::get<std::string>(state).ends_with("Running");
Alexander Hansen8c99fcf2025-07-24 12:27:05 +020061 },
62 power::busname, power::path, em_utils::properties::interface,
63 em_utils::properties::get, power::interface, power::property);
64}
65
66} // namespace power