Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 1 | #include <fmt/core.h> |
| 2 | |
| 3 | #include <cassert> |
| 4 | #include <phosphor-logging/log.hpp> |
| 5 | #include <powermode.hpp> |
| 6 | #include <regex> |
| 7 | #include <xyz/openbmc_project/Control/Power/Mode/server.hpp> |
| 8 | |
| 9 | namespace open_power |
| 10 | { |
| 11 | namespace occ |
| 12 | { |
| 13 | namespace powermode |
| 14 | { |
| 15 | |
| 16 | using namespace phosphor::logging; |
| 17 | using Mode = sdbusplus::xyz::openbmc_project::Control::Power::server::Mode; |
| 18 | |
| 19 | void PowerMode::modeChanged(sdbusplus::message::message& msg) |
| 20 | { |
| 21 | if (!occStatus.occActive()) |
| 22 | { |
| 23 | // Nothing to do |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | SysPwrMode pmode = SysPwrMode::NO_CHANGE; |
| 28 | |
| 29 | std::map<std::string, std::variant<std::string>> properties{}; |
| 30 | std::string interface; |
| 31 | std::string propVal; |
| 32 | msg.read(interface, properties); |
| 33 | const auto modeEntry = properties.find(POWER_MODE_PROP); |
| 34 | if (modeEntry != properties.end()) |
| 35 | { |
| 36 | auto modeEntryValue = modeEntry->second; |
| 37 | propVal = std::get<std::string>(modeEntryValue); |
| 38 | pmode = convertStringToMode(propVal); |
| 39 | |
| 40 | if (pmode != SysPwrMode::NO_CHANGE) |
| 41 | { |
| 42 | log<level::INFO>( |
| 43 | fmt::format("Power Mode Change Requested: {}", propVal) |
| 44 | .c_str()); |
| 45 | |
| 46 | // Trigger mode change to OCC |
| 47 | occStatus.sendModeChange(); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | // Convert PowerMode string to OCC SysPwrMode |
| 55 | SysPwrMode convertStringToMode(const std::string& i_modeString) |
| 56 | { |
| 57 | SysPwrMode pmode = SysPwrMode::NO_CHANGE; |
| 58 | |
| 59 | Mode::PowerMode mode = Mode::convertPowerModeFromString(i_modeString); |
| 60 | if (mode == Mode::PowerMode::MaximumPerformance) |
| 61 | { |
| 62 | pmode = SysPwrMode::MAX_PERF; |
| 63 | } |
| 64 | else if (mode == Mode::PowerMode::PowerSaving) |
| 65 | { |
| 66 | pmode = SysPwrMode::POWER_SAVING; |
| 67 | } |
| 68 | else if (mode == Mode::PowerMode::Static) |
| 69 | { |
| 70 | pmode = SysPwrMode::DISABLE; |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | log<level::ERR>( |
| 75 | fmt::format("convertStringToMode: Invalid Power Mode specified: {}", |
| 76 | i_modeString) |
| 77 | .c_str()); |
| 78 | } |
| 79 | |
| 80 | return pmode; |
| 81 | } |
| 82 | |
| 83 | } // namespace powermode |
| 84 | |
| 85 | } // namespace occ |
| 86 | |
| 87 | } // namespace open_power |