xref: /openbmc/openpower-occ-control/powermode.cpp (revision b5ca101530dc5fa0992d00ab36edb292519cef94)
1 #include <fmt/core.h>
2 
3 #include <phosphor-logging/log.hpp>
4 #include <powermode.hpp>
5 #include <xyz/openbmc_project/Control/Power/Mode/server.hpp>
6 
7 #include <cassert>
8 #include <regex>
9 
10 namespace open_power
11 {
12 namespace occ
13 {
14 namespace powermode
15 {
16 
17 using namespace phosphor::logging;
18 using Mode = sdbusplus::xyz::openbmc_project::Control::Power::server::Mode;
19 
20 void 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
56 SysPwrMode 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
89