Alexander Hansen | 8c99fcf | 2025-07-24 12:27:05 +0200 | [diff] [blame^] | 1 | #include "power_status_monitor.hpp" |
| 2 | |
| 3 | #include "utils.hpp" |
| 4 | |
| 5 | #include <boost/algorithm/string/predicate.hpp> |
| 6 | #include <sdbusplus/bus/match.hpp> |
| 7 | |
| 8 | namespace power |
| 9 | { |
| 10 | |
| 11 | const static constexpr char* busname = "xyz.openbmc_project.State.Host"; |
| 12 | const static constexpr char* interface = "xyz.openbmc_project.State.Host"; |
| 13 | const static constexpr char* path = "/xyz/openbmc_project/state/host0"; |
| 14 | const static constexpr char* property = "CurrentHostState"; |
| 15 | |
| 16 | bool PowerStatusMonitor::isPowerOn() |
| 17 | { |
| 18 | if (!powerMatch) |
| 19 | { |
| 20 | throw std::runtime_error("Power Match Not Created"); |
| 21 | } |
| 22 | return powerStatusOn; |
| 23 | } |
| 24 | |
| 25 | void PowerStatusMonitor::setupPowerMatch( |
| 26 | const std::shared_ptr<sdbusplus::asio::connection>& conn) |
| 27 | { |
| 28 | powerMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 29 | static_cast<sdbusplus::bus_t&>(*conn), |
| 30 | "type='signal',interface='" + |
| 31 | std::string(em_utils::properties::interface) + "',path='" + |
| 32 | std::string(power::path) + "',arg0='" + |
| 33 | std::string(power::interface) + "'", |
| 34 | [this](sdbusplus::message_t& message) { |
| 35 | std::string objectName; |
| 36 | boost::container::flat_map<std::string, std::variant<std::string>> |
| 37 | values; |
| 38 | message.read(objectName, values); |
| 39 | auto findState = values.find(power::property); |
| 40 | if (findState != values.end()) |
| 41 | { |
| 42 | powerStatusOn = boost::ends_with( |
| 43 | std::get<std::string>(findState->second), "Running"); |
| 44 | } |
| 45 | }); |
| 46 | |
| 47 | conn->async_method_call( |
| 48 | [this](boost::system::error_code ec, |
| 49 | const std::variant<std::string>& state) { |
| 50 | if (ec) |
| 51 | { |
| 52 | return; |
| 53 | } |
| 54 | powerStatusOn = |
| 55 | boost::ends_with(std::get<std::string>(state), "Running"); |
| 56 | }, |
| 57 | power::busname, power::path, em_utils::properties::interface, |
| 58 | em_utils::properties::get, power::interface, power::property); |
| 59 | } |
| 60 | |
| 61 | } // namespace power |