Skip to content

Commit 6b9ac4f

Browse files
committed
Systems: Add support to read AllowedPowerModes
Instead of hardcodeing the AllowedPowerModes property, the data will be read from dbus if it exists. If data is empty/not found, the property will be set to the default value: [ "MaximumPerformance", "PowerSaving", "Static" ] Tested on Rainier hardware and Validator passed When dbus property is empty, it will show default modes: ''' xyz.openbmc_project.Control.Power.Mode interface - - - .AllowedPowerModes property as 0 const .PowerMode property s "xyz.openbmc_project.Control.Power.Mode.PowerMode.MaximumPerformance" emits-change writable "PowerMode": "MaximumPerformance", "PowerMode@Redfish.AllowableValues": [ "MaximumPerformance", "PowerSaving", "Static" ], "PowerRestorePolicy": "AlwaysOff", ''' When dbus property populated with 6 modes: ''' - xyz.openbmc_project.Control.Power.Mode interface - - - .AllowedPowerModes property as 6 "xyz.openbmc_project.Control.Power.Mode.PowerMode.BalancedPerformance" "xyz.openbmc_project.Control.Power.Mode.PowerMode.EfficiencyFavorPerformance" "xyz.openbmc_project.Control.Power.Mode.PowerMode.EfficiencyFavorPower" "xyz.openbmc_project.Control.Power.Mode.PowerMode.MaximumPerformance" "xyz.openbmc_project.Control.Power.Mode.PowerMode.PowerSaving" "xyz.openbmc_project.Control.Power.Mode.PowerMode.Static" const .PowerMode property s "xyz.openbmc_project.Control.Power.Mode.PowerMode.MaximumPerformance" emits-change writable "PowerMode": "MaximumPerformance", "PowerMode@Redfish.AllowableValues": [ "BalancedPerformance", "EfficiencyFavorPerformance", "EfficiencyFavorPower", "MaximumPerformance", "PowerSaving", "Static" ], "PowerRestorePolicy": "AlwaysOff", ''' When dbus property not defined it will show default modes: ''' xyz.openbmc_project.Control.Power.Mode interface - - - .PowerMode property s "xyz.openbmc_project.Control.Power.Mode.PowerMode.MaximumPerformance" emits-change writable "PowerMode": "MaximumPerformance", "PowerMode@Redfish.AllowableValues": [ "MaximumPerformance", "PowerSaving", "Static" ], "PowerRestorePolicy": "AlwaysOff", ''' Signed-off-by: Chris Cain <cjcain@us.ibm.com> Change-Id: Ic9882d2760a39dd1a0ea353624eb3c8575f4c6a0
1 parent e610b31 commit 6b9ac4f

File tree

1 file changed

+85
-48
lines changed

1 file changed

+85
-48
lines changed

redfish-core/lib/systems.hpp

Lines changed: 85 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@
4343
#include <sdbusplus/unpack_properties.hpp>
4444

4545
#include <array>
46+
#include <string>
4647
#include <string_view>
4748
#include <variant>
49+
#include <vector>
4850

4951
namespace redfish
5052
{
@@ -2166,66 +2168,112 @@ inline void getProvisioningStatus(std::shared_ptr<bmcweb::AsyncResp> asyncResp)
21662168
#endif
21672169

21682170
/**
2169-
* @brief Translate the PowerMode to a response message.
2171+
* @brief Translate the PowerMode string to enum value
21702172
*
2171-
* @param[in] asyncResp Shared pointer for generating response message.
2172-
* @param[in] modeValue PowerMode value to be translated
2173+
* @param[in] modeString PowerMode string to be translated
21732174
*
2174-
* @return None.
2175+
* @return PowerMode enum
21752176
*/
2176-
inline void
2177-
translatePowerMode(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2178-
const std::string& modeValue)
2177+
inline computer_system::PowerMode
2178+
translatePowerModeString(const std::string& modeString)
21792179
{
21802180
using PowerMode = computer_system::PowerMode;
21812181

2182-
if (modeValue == "xyz.openbmc_project.Control.Power.Mode.PowerMode.Static")
2182+
if (modeString == "xyz.openbmc_project.Control.Power.Mode.PowerMode.Static")
21832183
{
2184-
asyncResp->res.jsonValue["PowerMode"] = PowerMode::Static;
2184+
return PowerMode::Static;
21852185
}
2186-
else if (
2187-
modeValue ==
2186+
if (modeString ==
21882187
"xyz.openbmc_project.Control.Power.Mode.PowerMode.MaximumPerformance")
21892188
{
2190-
asyncResp->res.jsonValue["PowerMode"] = PowerMode::MaximumPerformance;
2189+
return PowerMode::MaximumPerformance;
21912190
}
2192-
else if (modeValue ==
2193-
"xyz.openbmc_project.Control.Power.Mode.PowerMode.PowerSaving")
2191+
if (modeString ==
2192+
"xyz.openbmc_project.Control.Power.Mode.PowerMode.PowerSaving")
21942193
{
2195-
asyncResp->res.jsonValue["PowerMode"] = PowerMode::PowerSaving;
2194+
return PowerMode::PowerSaving;
21962195
}
2197-
else if (
2198-
modeValue ==
2196+
if (modeString ==
21992197
"xyz.openbmc_project.Control.Power.Mode.PowerMode.BalancedPerformance")
22002198
{
2201-
asyncResp->res.jsonValue["PowerMode"] = PowerMode::BalancedPerformance;
2199+
return PowerMode::BalancedPerformance;
22022200
}
2203-
else if (
2204-
modeValue ==
2201+
if (modeString ==
22052202
"xyz.openbmc_project.Control.Power.Mode.PowerMode.EfficiencyFavorPerformance")
22062203
{
2207-
asyncResp->res.jsonValue["PowerMode"] =
2208-
PowerMode::EfficiencyFavorPerformance;
2204+
return PowerMode::EfficiencyFavorPerformance;
22092205
}
2210-
else if (
2211-
modeValue ==
2206+
if (modeString ==
22122207
"xyz.openbmc_project.Control.Power.Mode.PowerMode.EfficiencyFavorPower")
22132208
{
2214-
asyncResp->res.jsonValue["PowerMode"] = PowerMode::EfficiencyFavorPower;
2209+
return PowerMode::EfficiencyFavorPower;
22152210
}
2216-
else if (modeValue ==
2217-
"xyz.openbmc_project.Control.Power.Mode.PowerMode.OEM")
2211+
if (modeString == "xyz.openbmc_project.Control.Power.Mode.PowerMode.OEM")
22182212
{
2219-
asyncResp->res.jsonValue["PowerMode"] = PowerMode::OEM;
2213+
return PowerMode::OEM;
2214+
}
2215+
// Any other values would be invalid
2216+
BMCWEB_LOG_ERROR("PowerMode value was not valid: {}", modeString);
2217+
return PowerMode::Invalid;
2218+
}
2219+
2220+
inline void
2221+
afterGetPowerMode(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2222+
const boost::system::error_code& ec,
2223+
const dbus::utility::DBusPropertiesMap& properties)
2224+
{
2225+
if (ec)
2226+
{
2227+
BMCWEB_LOG_ERROR("DBUS response error on PowerMode GetAll: {}", ec);
2228+
messages::internalError(asyncResp->res);
2229+
return;
2230+
}
2231+
2232+
std::string powerMode;
2233+
const std::vector<std::string>* allowedModes = nullptr;
2234+
const bool success = sdbusplus::unpackPropertiesNoThrow(
2235+
dbus_utils::UnpackErrorPrinter(), properties, "PowerMode", powerMode,
2236+
"AllowedPowerModes", allowedModes);
2237+
2238+
if (!success)
2239+
{
2240+
messages::internalError(asyncResp->res);
2241+
return;
2242+
}
2243+
2244+
nlohmann::json::array_t modeList;
2245+
if (allowedModes == nullptr)
2246+
{
2247+
modeList.emplace_back("Static");
2248+
modeList.emplace_back("MaximumPerformance");
2249+
modeList.emplace_back("PowerSaving");
22202250
}
22212251
else
22222252
{
2223-
// Any other values would be invalid
2224-
BMCWEB_LOG_DEBUG("PowerMode value was not valid: {}", modeValue);
2253+
for (const auto& aMode : *allowedModes)
2254+
{
2255+
computer_system::PowerMode modeValue =
2256+
translatePowerModeString(aMode);
2257+
if (modeValue == computer_system::PowerMode::Invalid)
2258+
{
2259+
messages::internalError(asyncResp->res);
2260+
continue;
2261+
}
2262+
modeList.emplace_back(modeValue);
2263+
}
2264+
}
2265+
asyncResp->res.jsonValue["PowerMode@Redfish.AllowableValues"] = modeList;
2266+
2267+
BMCWEB_LOG_DEBUG("Current power mode: {}", powerMode);
2268+
const computer_system::PowerMode modeValue =
2269+
translatePowerModeString(powerMode);
2270+
if (modeValue == computer_system::PowerMode::Invalid)
2271+
{
22252272
messages::internalError(asyncResp->res);
2273+
return;
22262274
}
2275+
asyncResp->res.jsonValue["PowerMode"] = modeValue;
22272276
}
2228-
22292277
/**
22302278
* @brief Retrieves system power mode
22312279
*
@@ -2282,25 +2330,14 @@ inline void getPowerMode(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
22822330
messages::internalError(asyncResp->res);
22832331
return;
22842332
}
2285-
// Valid Power Mode object found, now read the current value
2286-
sdbusplus::asio::getProperty<std::string>(
2333+
2334+
// Valid Power Mode object found, now read the mode properties
2335+
sdbusplus::asio::getAllProperties(
22872336
*crow::connections::systemBus, service, path,
2288-
"xyz.openbmc_project.Control.Power.Mode", "PowerMode",
2337+
"xyz.openbmc_project.Control.Power.Mode",
22892338
[asyncResp](const boost::system::error_code& ec2,
2290-
const std::string& pmode) {
2291-
if (ec2)
2292-
{
2293-
BMCWEB_LOG_ERROR("DBUS response error on PowerMode Get: {}",
2294-
ec2);
2295-
messages::internalError(asyncResp->res);
2296-
return;
2297-
}
2298-
2299-
asyncResp->res.jsonValue["PowerMode@Redfish.AllowableValues"] = {
2300-
"Static", "MaximumPerformance", "PowerSaving"};
2301-
2302-
BMCWEB_LOG_DEBUG("Current power mode: {}", pmode);
2303-
translatePowerMode(asyncResp, pmode);
2339+
const dbus::utility::DBusPropertiesMap& properties) {
2340+
afterGetPowerMode(asyncResp, ec2, properties);
23042341
});
23052342
});
23062343
}

0 commit comments

Comments
 (0)