xref: /openbmc/entity-manager/src/entity_manager/power_status_monitor.cpp (revision e1646276a815471e4b3510825c21293095fc322f)
1 #include "power_status_monitor.hpp"
2 
3 #include "phosphor-logging/lg2.hpp"
4 #include "utils.hpp"
5 
6 #include <sdbusplus/bus/match.hpp>
7 
8 #include <flat_map>
9 
10 namespace power
11 {
12 
13 const static constexpr char* busname = "xyz.openbmc_project.State.Host";
14 const static constexpr char* interface = "xyz.openbmc_project.State.Host";
15 const static constexpr char* path = "/xyz/openbmc_project/state/host0";
16 const static constexpr char* property = "CurrentHostState";
17 
PowerStatusMonitor(sdbusplus::asio::connection & conn)18 PowerStatusMonitor::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 
27 {
28     getInitialPowerStatus(conn);
29 }
30 
isPowerOn() const31 bool PowerStatusMonitor::isPowerOn() const
32 {
33     return powerStatusOn;
34 }
35 
handlePowerMatch(sdbusplus::message_t & message)36 void 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 
getInitialPowerStatus(sdbusplus::asio::connection & conn)51 void PowerStatusMonitor::getInitialPowerStatus(
52     sdbusplus::asio::connection& conn)
53 {
54     conn.async_method_call(
55         [this](boost::system::error_code ec,
56                const std::variant<std::string>& state) {
57             if (ec)
58             {
59                 return;
60             }
61             powerStatusOn = std::get<std::string>(state).ends_with("Running");
62         },
63         power::busname, power::path, em_utils::properties::interface,
64         em_utils::properties::get, power::interface, power::property);
65 }
66 
67 } // namespace power
68