1 #include "power_status_monitor.hpp" 2 3 #include "utils.hpp" 4 5 #include <phosphor-logging/lg2.hpp> 6 #include <sdbusplus/bus/match.hpp> 7 #include <xyz/openbmc_project/State/Host/client.hpp> 8 9 #include <flat_map> 10 11 namespace power 12 { 13 14 using HostState = sdbusplus::common::xyz::openbmc_project::state::Host; 15 16 const static std::string path = 17 std::format("{}/{}{}", HostState::namespace_path::value, 18 HostState::namespace_path::host, 0); 19 const static constexpr char* property = "CurrentHostState"; 20 21 PowerStatusMonitor::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='" + 27 std::string(HostState::interface) + "'", 28 std::bind_front(&PowerStatusMonitor::handlePowerMatch, this)) 29 30 { 31 getInitialPowerStatus(conn); 32 } 33 34 bool PowerStatusMonitor::isPowerOn() const 35 { 36 return powerStatusOn; 37 } 38 39 void 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 54 void PowerStatusMonitor::getInitialPowerStatus( 55 sdbusplus::asio::connection& conn) 56 { 57 lg2::debug("querying initial power state"); 58 59 conn.async_method_call( 60 [this](boost::system::error_code ec, 61 const std::variant<std::string>& state) { 62 if (ec) 63 { 64 return; 65 } 66 powerStatusOn = std::get<std::string>(state).ends_with("Running"); 67 }, 68 HostState::interface, power::path, em_utils::properties::interface, 69 em_utils::properties::get, HostState::interface, power::property); 70 } 71 72 } // namespace power 73