xref: /openbmc/bmcweb/features/redfish/lib/power.hpp (revision bd79bce8c3f1deb1fb2773868b9ece25233cf27b)
12474adfaSEd Tanous /*
22474adfaSEd Tanous // Copyright (c) 2018 Intel Corporation
32474adfaSEd Tanous // Copyright (c) 2018 Ampere Computing LLC
42474adfaSEd Tanous /
52474adfaSEd Tanous // Licensed under the Apache License, Version 2.0 (the "License");
62474adfaSEd Tanous // you may not use this file except in compliance with the License.
72474adfaSEd Tanous // You may obtain a copy of the License at
82474adfaSEd Tanous //
92474adfaSEd Tanous //      http://www.apache.org/licenses/LICENSE-2.0
102474adfaSEd Tanous //
112474adfaSEd Tanous // Unless required by applicable law or agreed to in writing, software
122474adfaSEd Tanous // distributed under the License is distributed on an "AS IS" BASIS,
132474adfaSEd Tanous // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
142474adfaSEd Tanous // See the License for the specific language governing permissions and
152474adfaSEd Tanous // limitations under the License.
162474adfaSEd Tanous */
172474adfaSEd Tanous #pragma once
182474adfaSEd Tanous 
193ccb3adbSEd Tanous #include "app.hpp"
207a1dbc48SGeorge Liu #include "dbus_utility.hpp"
21539d8c6bSEd Tanous #include "generated/enums/power.hpp"
223ccb3adbSEd Tanous #include "query.hpp"
233ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
242474adfaSEd Tanous #include "sensors.hpp"
250d7702c0SZhenwei Chen #include "utils/chassis_utils.hpp"
265b90429aSEd Tanous #include "utils/json_utils.hpp"
272474adfaSEd Tanous 
28d1bde9e5SKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
297e860f15SJohn Edward Broadbent 
307a1dbc48SGeorge Liu #include <array>
310885057cSEd Tanous #include <string>
327a1dbc48SGeorge Liu #include <string_view>
330885057cSEd Tanous #include <vector>
347a1dbc48SGeorge Liu 
352474adfaSEd Tanous namespace redfish
362474adfaSEd Tanous {
3753b00f5dSEd Tanous 
3853b00f5dSEd Tanous inline void afterGetPowerCapEnable(
398d1b46d7Szhanghch05     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
4053b00f5dSEd Tanous     uint32_t valueToSet, const boost::system::error_code& ec,
4153b00f5dSEd Tanous     bool powerCapEnable)
424bb3dc34SCarol Wang {
4353b00f5dSEd Tanous     if (ec)
4453b00f5dSEd Tanous     {
4553b00f5dSEd Tanous         messages::internalError(sensorsAsyncResp->asyncResp->res);
4653b00f5dSEd Tanous         BMCWEB_LOG_ERROR("powerCapEnable Get handler: Dbus error {}", ec);
4753b00f5dSEd Tanous         return;
4853b00f5dSEd Tanous     }
4953b00f5dSEd Tanous     if (!powerCapEnable)
5053b00f5dSEd Tanous     {
5153b00f5dSEd Tanous         messages::actionNotSupported(
5253b00f5dSEd Tanous             sensorsAsyncResp->asyncResp->res,
5353b00f5dSEd Tanous             "Setting LimitInWatts when PowerLimit feature is disabled");
5453b00f5dSEd Tanous         BMCWEB_LOG_ERROR("PowerLimit feature is disabled ");
5553b00f5dSEd Tanous         return;
5653b00f5dSEd Tanous     }
5753b00f5dSEd Tanous 
58e93abac6SGinu George     setDbusProperty(sensorsAsyncResp->asyncResp, "PowerControl",
59e93abac6SGinu George                     "xyz.openbmc_project.Settings",
6087c44966SAsmitha Karunanithi                     sdbusplus::message::object_path(
6187c44966SAsmitha Karunanithi                         "/xyz/openbmc_project/control/host0/power_cap"),
6287c44966SAsmitha Karunanithi                     "xyz.openbmc_project.Control.Power.Cap", "PowerCap",
63e93abac6SGinu George                     valueToSet);
6453b00f5dSEd Tanous }
6553b00f5dSEd Tanous 
6653b00f5dSEd Tanous inline void afterGetChassisPath(
6753b00f5dSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
680885057cSEd Tanous     std::vector<nlohmann::json::object_t>& powerControlCollections,
6953b00f5dSEd Tanous     const std::optional<std::string>& chassisPath)
7053b00f5dSEd Tanous {
714bb3dc34SCarol Wang     if (!chassisPath)
724bb3dc34SCarol Wang     {
7362598e31SEd Tanous         BMCWEB_LOG_WARNING("Don't find valid chassis path ");
7453b00f5dSEd Tanous         messages::resourceNotFound(sensorsAsyncResp->asyncResp->res, "Chassis",
7553b00f5dSEd Tanous                                    sensorsAsyncResp->chassisId);
764bb3dc34SCarol Wang         return;
774bb3dc34SCarol Wang     }
784bb3dc34SCarol Wang 
794bb3dc34SCarol Wang     if (powerControlCollections.size() != 1)
804bb3dc34SCarol Wang     {
8162598e31SEd Tanous         BMCWEB_LOG_WARNING("Don't support multiple hosts at present ");
8253b00f5dSEd Tanous         messages::resourceNotFound(sensorsAsyncResp->asyncResp->res, "Power",
8353b00f5dSEd Tanous                                    "PowerControl");
844bb3dc34SCarol Wang         return;
854bb3dc34SCarol Wang     }
864bb3dc34SCarol Wang 
874bb3dc34SCarol Wang     auto& item = powerControlCollections[0];
884bb3dc34SCarol Wang 
894bb3dc34SCarol Wang     std::optional<uint32_t> value;
900885057cSEd Tanous     if (!json_util::readJsonObject(item, sensorsAsyncResp->asyncResp->res,
910885057cSEd Tanous                                    "PowerLimit/LimitInWatts", value))
924bb3dc34SCarol Wang     {
934bb3dc34SCarol Wang         return;
944bb3dc34SCarol Wang     }
954bb3dc34SCarol Wang     if (!value)
964bb3dc34SCarol Wang     {
974bb3dc34SCarol Wang         return;
984bb3dc34SCarol Wang     }
991e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
1001e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
1011e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/power_cap",
1021e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Power.Cap", "PowerCapEnable",
10353b00f5dSEd Tanous         std::bind_front(afterGetPowerCapEnable, sensorsAsyncResp, *value));
1044bb3dc34SCarol Wang }
1054bb3dc34SCarol Wang 
10653b00f5dSEd Tanous inline void afterPowerCapSettingGet(
10753b00f5dSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
1085e7e2dc5SEd Tanous     const boost::system::error_code& ec,
10953b00f5dSEd Tanous     const dbus::utility::DBusPropertiesMap& properties)
11053b00f5dSEd Tanous {
111028f7ebcSEddie James     if (ec)
112028f7ebcSEddie James     {
113002d39b4SEd Tanous         messages::internalError(sensorAsyncResp->asyncResp->res);
11453b00f5dSEd Tanous         BMCWEB_LOG_ERROR("Power Limit GetAll handler: Dbus error {}", ec);
115028f7ebcSEddie James         return;
116028f7ebcSEddie James     }
117028f7ebcSEddie James 
1187e860f15SJohn Edward Broadbent     nlohmann::json& tempArray =
119002d39b4SEd Tanous         sensorAsyncResp->asyncResp->res.jsonValue["PowerControl"];
120028f7ebcSEddie James 
1217e860f15SJohn Edward Broadbent     // Put multiple "sensors" into a single PowerControl, 0,
1227e860f15SJohn Edward Broadbent     // so only create the first one
123028f7ebcSEddie James     if (tempArray.empty())
124028f7ebcSEddie James     {
1257ab06f49SGunnar Mills         // Mandatory properties odata.id and MemberId
1267ab06f49SGunnar Mills         // A warning without a odata.type
1271476687dSEd Tanous         nlohmann::json::object_t powerControl;
128002d39b4SEd Tanous         powerControl["@odata.type"] = "#Power.v1_0_0.PowerControl";
129*bd79bce8SPatrick Williams         powerControl["@odata.id"] =
130*bd79bce8SPatrick Williams             "/redfish/v1/Chassis/" + sensorAsyncResp->chassisId +
1311476687dSEd Tanous             "/Power#/PowerControl/0";
1321476687dSEd Tanous         powerControl["Name"] = "Chassis Power Control";
1331476687dSEd Tanous         powerControl["MemberId"] = "0";
134b2ba3072SPatrick Williams         tempArray.emplace_back(std::move(powerControl));
135028f7ebcSEddie James     }
136028f7ebcSEddie James 
137028f7ebcSEddie James     nlohmann::json& sensorJson = tempArray.back();
138028f7ebcSEddie James     bool enabled = false;
139028f7ebcSEddie James     double powerCap = 0.0;
140028f7ebcSEddie James     int64_t scale = 0;
141028f7ebcSEddie James 
14253b00f5dSEd Tanous     for (const std::pair<std::string, dbus::utility::DbusVariantType>&
14353b00f5dSEd Tanous              property : properties)
144028f7ebcSEddie James     {
14555f79e6fSEd Tanous         if (property.first == "Scale")
146028f7ebcSEddie James         {
14753b00f5dSEd Tanous             const int64_t* i = std::get_if<int64_t>(&property.second);
148028f7ebcSEddie James 
149e662eae8SEd Tanous             if (i != nullptr)
150028f7ebcSEddie James             {
151028f7ebcSEddie James                 scale = *i;
152028f7ebcSEddie James             }
153028f7ebcSEddie James         }
15455f79e6fSEd Tanous         else if (property.first == "PowerCap")
155028f7ebcSEddie James         {
156002d39b4SEd Tanous             const double* d = std::get_if<double>(&property.second);
15753b00f5dSEd Tanous             const int64_t* i = std::get_if<int64_t>(&property.second);
15853b00f5dSEd Tanous             const uint32_t* u = std::get_if<uint32_t>(&property.second);
159028f7ebcSEddie James 
160e662eae8SEd Tanous             if (d != nullptr)
161028f7ebcSEddie James             {
162028f7ebcSEddie James                 powerCap = *d;
163028f7ebcSEddie James             }
164e662eae8SEd Tanous             else if (i != nullptr)
165028f7ebcSEddie James             {
166271584abSEd Tanous                 powerCap = static_cast<double>(*i);
167028f7ebcSEddie James             }
168e662eae8SEd Tanous             else if (u != nullptr)
169028f7ebcSEddie James             {
170028f7ebcSEddie James                 powerCap = *u;
171028f7ebcSEddie James             }
172028f7ebcSEddie James         }
17355f79e6fSEd Tanous         else if (property.first == "PowerCapEnable")
174028f7ebcSEddie James         {
175002d39b4SEd Tanous             const bool* b = std::get_if<bool>(&property.second);
176028f7ebcSEddie James 
177e662eae8SEd Tanous             if (b != nullptr)
178028f7ebcSEddie James             {
179028f7ebcSEddie James                 enabled = *b;
180028f7ebcSEddie James             }
181028f7ebcSEddie James         }
182028f7ebcSEddie James     }
183028f7ebcSEddie James 
1847e860f15SJohn Edward Broadbent     // LimitException is Mandatory attribute as per OCP
1857e860f15SJohn Edward Broadbent     // Baseline Profile – v1.0.0, so currently making it
1867e860f15SJohn Edward Broadbent     // "NoAction" as default value to make it OCP Compliant.
187539d8c6bSEd Tanous     sensorJson["PowerLimit"]["LimitException"] =
188539d8c6bSEd Tanous         power::PowerLimitException::NoAction;
1895a64a6f3SJoshi-Mansi 
190028f7ebcSEddie James     if (enabled)
191028f7ebcSEddie James     {
1927e860f15SJohn Edward Broadbent         // Redfish specification indicates PowerLimit should
1937e860f15SJohn Edward Broadbent         // be null if the limit is not enabled.
194*bd79bce8SPatrick Williams         sensorJson["PowerLimit"]["LimitInWatts"] =
195*bd79bce8SPatrick Williams             powerCap * std::pow(10, scale);
196028f7ebcSEddie James     }
19753b00f5dSEd Tanous }
19853b00f5dSEd Tanous 
19953b00f5dSEd Tanous using Mapper = dbus::utility::MapperGetSubTreePathsResponse;
200*bd79bce8SPatrick Williams inline void afterGetChassis(
201*bd79bce8SPatrick Williams     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
202*bd79bce8SPatrick Williams     const boost::system::error_code& ec2, const Mapper& chassisPaths)
20353b00f5dSEd Tanous {
20453b00f5dSEd Tanous     if (ec2)
20553b00f5dSEd Tanous     {
20653b00f5dSEd Tanous         BMCWEB_LOG_ERROR("Power Limit GetSubTreePaths handler Dbus error {}",
20753b00f5dSEd Tanous                          ec2);
20853b00f5dSEd Tanous         return;
20953b00f5dSEd Tanous     }
21053b00f5dSEd Tanous 
21153b00f5dSEd Tanous     bool found = false;
21253b00f5dSEd Tanous     for (const std::string& chassis : chassisPaths)
21353b00f5dSEd Tanous     {
21453b00f5dSEd Tanous         size_t len = std::string::npos;
21553b00f5dSEd Tanous         size_t lastPos = chassis.rfind('/');
21653b00f5dSEd Tanous         if (lastPos == std::string::npos)
21753b00f5dSEd Tanous         {
21853b00f5dSEd Tanous             continue;
21953b00f5dSEd Tanous         }
22053b00f5dSEd Tanous 
22153b00f5dSEd Tanous         if (lastPos == chassis.size() - 1)
22253b00f5dSEd Tanous         {
22353b00f5dSEd Tanous             size_t end = lastPos;
22453b00f5dSEd Tanous             lastPos = chassis.rfind('/', lastPos - 1);
22553b00f5dSEd Tanous             if (lastPos == std::string::npos)
22653b00f5dSEd Tanous             {
22753b00f5dSEd Tanous                 continue;
22853b00f5dSEd Tanous             }
22953b00f5dSEd Tanous 
23053b00f5dSEd Tanous             len = end - (lastPos + 1);
23153b00f5dSEd Tanous         }
23253b00f5dSEd Tanous 
23353b00f5dSEd Tanous         std::string interfaceChassisName = chassis.substr(lastPos + 1, len);
23453b00f5dSEd Tanous         if (interfaceChassisName == sensorAsyncResp->chassisId)
23553b00f5dSEd Tanous         {
23653b00f5dSEd Tanous             found = true;
23753b00f5dSEd Tanous             break;
23853b00f5dSEd Tanous         }
23953b00f5dSEd Tanous     }
24053b00f5dSEd Tanous 
24153b00f5dSEd Tanous     if (!found)
24253b00f5dSEd Tanous     {
24353b00f5dSEd Tanous         BMCWEB_LOG_DEBUG("Power Limit not present for {}",
24453b00f5dSEd Tanous                          sensorAsyncResp->chassisId);
24553b00f5dSEd Tanous         return;
24653b00f5dSEd Tanous     }
247028f7ebcSEddie James 
248d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
249d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
250028f7ebcSEddie James         "/xyz/openbmc_project/control/host0/power_cap",
251d1bde9e5SKrzysztof Grobelny         "xyz.openbmc_project.Control.Power.Cap",
25253b00f5dSEd Tanous         [sensorAsyncResp](const boost::system::error_code& ec,
25353b00f5dSEd Tanous                           const dbus::utility::DBusPropertiesMap& properties
25453b00f5dSEd Tanous 
25553b00f5dSEd Tanous         ) { afterPowerCapSettingGet(sensorAsyncResp, ec, properties); });
25653b00f5dSEd Tanous }
25753b00f5dSEd Tanous 
25853b00f5dSEd Tanous inline void
25953b00f5dSEd Tanous     handleChassisPowerGet(App& app, const crow::Request& req,
26053b00f5dSEd Tanous                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
26153b00f5dSEd Tanous                           const std::string& chassisName)
26253b00f5dSEd Tanous {
26353b00f5dSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
26453b00f5dSEd Tanous     {
26553b00f5dSEd Tanous         return;
26653b00f5dSEd Tanous     }
26753b00f5dSEd Tanous     asyncResp->res.jsonValue["PowerControl"] = nlohmann::json::array();
26853b00f5dSEd Tanous 
26953b00f5dSEd Tanous     auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
27053b00f5dSEd Tanous         asyncResp, chassisName, sensors::dbus::powerPaths,
27153b00f5dSEd Tanous         sensors::node::power);
27253b00f5dSEd Tanous 
27353b00f5dSEd Tanous     getChassisData(sensorAsyncResp);
27453b00f5dSEd Tanous 
27553b00f5dSEd Tanous     // This callback verifies that the power limit is only provided
27653b00f5dSEd Tanous     // for the chassis that implements the Chassis inventory item.
27753b00f5dSEd Tanous     // This prevents things like power supplies providing the
27853b00f5dSEd Tanous     // chassis power limit
279028f7ebcSEddie James 
2807a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
281f857e9aeSAppaRao Puli         "xyz.openbmc_project.Inventory.Item.Board",
2827a1dbc48SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Chassis"};
2837a1dbc48SGeorge Liu 
28453b00f5dSEd Tanous     dbus::utility::getSubTreePaths(
28553b00f5dSEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces,
28653b00f5dSEd Tanous         std::bind_front(afterGetChassis, sensorAsyncResp));
28753b00f5dSEd Tanous }
2884bb3dc34SCarol Wang 
28953b00f5dSEd Tanous inline void
29053b00f5dSEd Tanous     handleChassisPowerPatch(App& app, const crow::Request& req,
2917e860f15SJohn Edward Broadbent                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
29253b00f5dSEd Tanous                             const std::string& chassisName)
29353b00f5dSEd Tanous {
2943ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
29545ca1b86SEd Tanous     {
29645ca1b86SEd Tanous         return;
29745ca1b86SEd Tanous     }
2988d1b46d7Szhanghch05     auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
29902da7c5aSEd Tanous         asyncResp, chassisName, sensors::dbus::powerPaths,
300a0ec28b6SAdrian Ambrożewicz         sensors::node::power);
3014bb3dc34SCarol Wang 
3020885057cSEd Tanous     std::optional<std::vector<nlohmann::json::object_t>> voltageCollections;
3030885057cSEd Tanous     std::optional<std::vector<nlohmann::json::object_t>> powerCtlCollections;
3044bb3dc34SCarol Wang 
305002d39b4SEd Tanous     if (!json_util::readJsonPatch(req, sensorAsyncResp->asyncResp->res,
306002d39b4SEd Tanous                                   "PowerControl", powerCtlCollections,
307002d39b4SEd Tanous                                   "Voltages", voltageCollections))
3084bb3dc34SCarol Wang     {
3094bb3dc34SCarol Wang         return;
3104bb3dc34SCarol Wang     }
3114bb3dc34SCarol Wang 
3124bb3dc34SCarol Wang     if (powerCtlCollections)
3134bb3dc34SCarol Wang     {
31453b00f5dSEd Tanous         redfish::chassis_utils::getValidChassisPath(
31553b00f5dSEd Tanous             sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
31653b00f5dSEd Tanous             std::bind_front(afterGetChassisPath, sensorAsyncResp,
31753b00f5dSEd Tanous                             *powerCtlCollections));
3184bb3dc34SCarol Wang     }
3194bb3dc34SCarol Wang     if (voltageCollections)
3204bb3dc34SCarol Wang     {
3210885057cSEd Tanous         std::unordered_map<std::string, std::vector<nlohmann::json::object_t>>
3224bb3dc34SCarol Wang             allCollections;
3230885057cSEd Tanous         allCollections.emplace("Voltages", std::move(*voltageCollections));
32480ac4024SBruce Lee         setSensorsOverride(sensorAsyncResp, allCollections);
3254bb3dc34SCarol Wang     }
32653b00f5dSEd Tanous }
32753b00f5dSEd Tanous 
32853b00f5dSEd Tanous inline void requestRoutesPower(App& app)
32953b00f5dSEd Tanous {
33053b00f5dSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
33153b00f5dSEd Tanous         .privileges(redfish::privileges::getPower)
33253b00f5dSEd Tanous         .methods(boost::beast::http::verb::get)(
33353b00f5dSEd Tanous             std::bind_front(handleChassisPowerGet, std::ref(app)));
33453b00f5dSEd Tanous 
33553b00f5dSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
33653b00f5dSEd Tanous         .privileges(redfish::privileges::patchPower)
33753b00f5dSEd Tanous         .methods(boost::beast::http::verb::patch)(
33853b00f5dSEd Tanous             std::bind_front(handleChassisPowerPatch, std::ref(app)));
339413961deSRichard Marian Thomaiyar }
3402474adfaSEd Tanous 
3412474adfaSEd Tanous } // namespace redfish
342