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