xref: /openbmc/bmcweb/features/redfish/lib/power.hpp (revision e93abac6d60c4b409ad72086dac153f23d793ac1)
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"
213ccb3adbSEd Tanous #include "query.hpp"
223ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
232474adfaSEd Tanous #include "sensors.hpp"
240d7702c0SZhenwei Chen #include "utils/chassis_utils.hpp"
255b90429aSEd Tanous #include "utils/json_utils.hpp"
262474adfaSEd Tanous 
27d1bde9e5SKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
287e860f15SJohn Edward Broadbent 
297a1dbc48SGeorge Liu #include <array>
300885057cSEd Tanous #include <string>
317a1dbc48SGeorge Liu #include <string_view>
320885057cSEd Tanous #include <vector>
337a1dbc48SGeorge Liu 
342474adfaSEd Tanous namespace redfish
352474adfaSEd Tanous {
3653b00f5dSEd Tanous 
3753b00f5dSEd Tanous inline void afterGetPowerCapEnable(
388d1b46d7Szhanghch05     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
3953b00f5dSEd Tanous     uint32_t valueToSet, const boost::system::error_code& ec,
4053b00f5dSEd Tanous     bool powerCapEnable)
414bb3dc34SCarol Wang {
4253b00f5dSEd Tanous     if (ec)
4353b00f5dSEd Tanous     {
4453b00f5dSEd Tanous         messages::internalError(sensorsAsyncResp->asyncResp->res);
4553b00f5dSEd Tanous         BMCWEB_LOG_ERROR("powerCapEnable Get handler: Dbus error {}", ec);
4653b00f5dSEd Tanous         return;
4753b00f5dSEd Tanous     }
4853b00f5dSEd Tanous     if (!powerCapEnable)
4953b00f5dSEd Tanous     {
5053b00f5dSEd Tanous         messages::actionNotSupported(
5153b00f5dSEd Tanous             sensorsAsyncResp->asyncResp->res,
5253b00f5dSEd Tanous             "Setting LimitInWatts when PowerLimit feature is disabled");
5353b00f5dSEd Tanous         BMCWEB_LOG_ERROR("PowerLimit feature is disabled ");
5453b00f5dSEd Tanous         return;
5553b00f5dSEd Tanous     }
5653b00f5dSEd Tanous 
57*e93abac6SGinu George     setDbusProperty(sensorsAsyncResp->asyncResp, "PowerControl",
58*e93abac6SGinu George                     "xyz.openbmc_project.Settings",
5987c44966SAsmitha Karunanithi                     sdbusplus::message::object_path(
6087c44966SAsmitha Karunanithi                         "/xyz/openbmc_project/control/host0/power_cap"),
6187c44966SAsmitha Karunanithi                     "xyz.openbmc_project.Control.Power.Cap", "PowerCap",
62*e93abac6SGinu George                     valueToSet);
6353b00f5dSEd Tanous }
6453b00f5dSEd Tanous 
6553b00f5dSEd Tanous inline void afterGetChassisPath(
6653b00f5dSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
670885057cSEd Tanous     std::vector<nlohmann::json::object_t>& powerControlCollections,
6853b00f5dSEd Tanous     const std::optional<std::string>& chassisPath)
6953b00f5dSEd Tanous {
704bb3dc34SCarol Wang     if (!chassisPath)
714bb3dc34SCarol Wang     {
7262598e31SEd Tanous         BMCWEB_LOG_WARNING("Don't find valid chassis path ");
7353b00f5dSEd Tanous         messages::resourceNotFound(sensorsAsyncResp->asyncResp->res, "Chassis",
7453b00f5dSEd Tanous                                    sensorsAsyncResp->chassisId);
754bb3dc34SCarol Wang         return;
764bb3dc34SCarol Wang     }
774bb3dc34SCarol Wang 
784bb3dc34SCarol Wang     if (powerControlCollections.size() != 1)
794bb3dc34SCarol Wang     {
8062598e31SEd Tanous         BMCWEB_LOG_WARNING("Don't support multiple hosts at present ");
8153b00f5dSEd Tanous         messages::resourceNotFound(sensorsAsyncResp->asyncResp->res, "Power",
8253b00f5dSEd Tanous                                    "PowerControl");
834bb3dc34SCarol Wang         return;
844bb3dc34SCarol Wang     }
854bb3dc34SCarol Wang 
864bb3dc34SCarol Wang     auto& item = powerControlCollections[0];
874bb3dc34SCarol Wang 
884bb3dc34SCarol Wang     std::optional<uint32_t> value;
890885057cSEd Tanous     if (!json_util::readJsonObject(item, sensorsAsyncResp->asyncResp->res,
900885057cSEd Tanous                                    "PowerLimit/LimitInWatts", value))
914bb3dc34SCarol Wang     {
924bb3dc34SCarol Wang         return;
934bb3dc34SCarol Wang     }
944bb3dc34SCarol Wang     if (!value)
954bb3dc34SCarol Wang     {
964bb3dc34SCarol Wang         return;
974bb3dc34SCarol Wang     }
981e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
991e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
1001e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/power_cap",
1011e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Power.Cap", "PowerCapEnable",
10253b00f5dSEd Tanous         std::bind_front(afterGetPowerCapEnable, sensorsAsyncResp, *value));
1034bb3dc34SCarol Wang }
1044bb3dc34SCarol Wang 
10553b00f5dSEd Tanous inline void afterPowerCapSettingGet(
10653b00f5dSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
1075e7e2dc5SEd Tanous     const boost::system::error_code& ec,
10853b00f5dSEd Tanous     const dbus::utility::DBusPropertiesMap& properties)
10953b00f5dSEd Tanous {
110028f7ebcSEddie James     if (ec)
111028f7ebcSEddie James     {
112002d39b4SEd Tanous         messages::internalError(sensorAsyncResp->asyncResp->res);
11353b00f5dSEd Tanous         BMCWEB_LOG_ERROR("Power Limit GetAll handler: Dbus error {}", ec);
114028f7ebcSEddie James         return;
115028f7ebcSEddie James     }
116028f7ebcSEddie James 
1177e860f15SJohn Edward Broadbent     nlohmann::json& tempArray =
118002d39b4SEd Tanous         sensorAsyncResp->asyncResp->res.jsonValue["PowerControl"];
119028f7ebcSEddie James 
1207e860f15SJohn Edward Broadbent     // Put multiple "sensors" into a single PowerControl, 0,
1217e860f15SJohn Edward Broadbent     // so only create the first one
122028f7ebcSEddie James     if (tempArray.empty())
123028f7ebcSEddie James     {
1247ab06f49SGunnar Mills         // Mandatory properties odata.id and MemberId
1257ab06f49SGunnar Mills         // A warning without a odata.type
1261476687dSEd Tanous         nlohmann::json::object_t powerControl;
127002d39b4SEd Tanous         powerControl["@odata.type"] = "#Power.v1_0_0.PowerControl";
128002d39b4SEd Tanous         powerControl["@odata.id"] = "/redfish/v1/Chassis/" +
1297ab06f49SGunnar Mills                                     sensorAsyncResp->chassisId +
1301476687dSEd Tanous                                     "/Power#/PowerControl/0";
1311476687dSEd Tanous         powerControl["Name"] = "Chassis Power Control";
1321476687dSEd Tanous         powerControl["MemberId"] = "0";
133b2ba3072SPatrick Williams         tempArray.emplace_back(std::move(powerControl));
134028f7ebcSEddie James     }
135028f7ebcSEddie James 
136028f7ebcSEddie James     nlohmann::json& sensorJson = tempArray.back();
137028f7ebcSEddie James     bool enabled = false;
138028f7ebcSEddie James     double powerCap = 0.0;
139028f7ebcSEddie James     int64_t scale = 0;
140028f7ebcSEddie James 
14153b00f5dSEd Tanous     for (const std::pair<std::string, dbus::utility::DbusVariantType>&
14253b00f5dSEd Tanous              property : properties)
143028f7ebcSEddie James     {
14455f79e6fSEd Tanous         if (property.first == "Scale")
145028f7ebcSEddie James         {
14653b00f5dSEd Tanous             const int64_t* i = std::get_if<int64_t>(&property.second);
147028f7ebcSEddie James 
148e662eae8SEd Tanous             if (i != nullptr)
149028f7ebcSEddie James             {
150028f7ebcSEddie James                 scale = *i;
151028f7ebcSEddie James             }
152028f7ebcSEddie James         }
15355f79e6fSEd Tanous         else if (property.first == "PowerCap")
154028f7ebcSEddie James         {
155002d39b4SEd Tanous             const double* d = std::get_if<double>(&property.second);
15653b00f5dSEd Tanous             const int64_t* i = std::get_if<int64_t>(&property.second);
15753b00f5dSEd Tanous             const uint32_t* u = std::get_if<uint32_t>(&property.second);
158028f7ebcSEddie James 
159e662eae8SEd Tanous             if (d != nullptr)
160028f7ebcSEddie James             {
161028f7ebcSEddie James                 powerCap = *d;
162028f7ebcSEddie James             }
163e662eae8SEd Tanous             else if (i != nullptr)
164028f7ebcSEddie James             {
165271584abSEd Tanous                 powerCap = static_cast<double>(*i);
166028f7ebcSEddie James             }
167e662eae8SEd Tanous             else if (u != nullptr)
168028f7ebcSEddie James             {
169028f7ebcSEddie James                 powerCap = *u;
170028f7ebcSEddie James             }
171028f7ebcSEddie James         }
17255f79e6fSEd Tanous         else if (property.first == "PowerCapEnable")
173028f7ebcSEddie James         {
174002d39b4SEd Tanous             const bool* b = std::get_if<bool>(&property.second);
175028f7ebcSEddie James 
176e662eae8SEd Tanous             if (b != nullptr)
177028f7ebcSEddie James             {
178028f7ebcSEddie James                 enabled = *b;
179028f7ebcSEddie James             }
180028f7ebcSEddie James         }
181028f7ebcSEddie James     }
182028f7ebcSEddie James 
1837e860f15SJohn Edward Broadbent     // LimitException is Mandatory attribute as per OCP
1847e860f15SJohn Edward Broadbent     // Baseline Profile – v1.0.0, so currently making it
1857e860f15SJohn Edward Broadbent     // "NoAction" as default value to make it OCP Compliant.
1865a64a6f3SJoshi-Mansi     sensorJson["PowerLimit"]["LimitException"] = "NoAction";
1875a64a6f3SJoshi-Mansi 
188028f7ebcSEddie James     if (enabled)
189028f7ebcSEddie James     {
1907e860f15SJohn Edward Broadbent         // Redfish specification indicates PowerLimit should
1917e860f15SJohn Edward Broadbent         // be null if the limit is not enabled.
19253b00f5dSEd Tanous         sensorJson["PowerLimit"]["LimitInWatts"] = powerCap *
19353b00f5dSEd Tanous                                                    std::pow(10, scale);
194028f7ebcSEddie James     }
19553b00f5dSEd Tanous }
19653b00f5dSEd Tanous 
19753b00f5dSEd Tanous using Mapper = dbus::utility::MapperGetSubTreePathsResponse;
19853b00f5dSEd Tanous inline void
19953b00f5dSEd Tanous     afterGetChassis(const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
20053b00f5dSEd Tanous                     const boost::system::error_code& ec2,
20153b00f5dSEd Tanous                     const Mapper& chassisPaths)
20253b00f5dSEd Tanous {
20353b00f5dSEd Tanous     if (ec2)
20453b00f5dSEd Tanous     {
20553b00f5dSEd Tanous         BMCWEB_LOG_ERROR("Power Limit GetSubTreePaths handler Dbus error {}",
20653b00f5dSEd Tanous                          ec2);
20753b00f5dSEd Tanous         return;
20853b00f5dSEd Tanous     }
20953b00f5dSEd Tanous 
21053b00f5dSEd Tanous     bool found = false;
21153b00f5dSEd Tanous     for (const std::string& chassis : chassisPaths)
21253b00f5dSEd Tanous     {
21353b00f5dSEd Tanous         size_t len = std::string::npos;
21453b00f5dSEd Tanous         size_t lastPos = chassis.rfind('/');
21553b00f5dSEd Tanous         if (lastPos == std::string::npos)
21653b00f5dSEd Tanous         {
21753b00f5dSEd Tanous             continue;
21853b00f5dSEd Tanous         }
21953b00f5dSEd Tanous 
22053b00f5dSEd Tanous         if (lastPos == chassis.size() - 1)
22153b00f5dSEd Tanous         {
22253b00f5dSEd Tanous             size_t end = lastPos;
22353b00f5dSEd Tanous             lastPos = chassis.rfind('/', lastPos - 1);
22453b00f5dSEd Tanous             if (lastPos == std::string::npos)
22553b00f5dSEd Tanous             {
22653b00f5dSEd Tanous                 continue;
22753b00f5dSEd Tanous             }
22853b00f5dSEd Tanous 
22953b00f5dSEd Tanous             len = end - (lastPos + 1);
23053b00f5dSEd Tanous         }
23153b00f5dSEd Tanous 
23253b00f5dSEd Tanous         std::string interfaceChassisName = chassis.substr(lastPos + 1, len);
23353b00f5dSEd Tanous         if (interfaceChassisName == sensorAsyncResp->chassisId)
23453b00f5dSEd Tanous         {
23553b00f5dSEd Tanous             found = true;
23653b00f5dSEd Tanous             break;
23753b00f5dSEd Tanous         }
23853b00f5dSEd Tanous     }
23953b00f5dSEd Tanous 
24053b00f5dSEd Tanous     if (!found)
24153b00f5dSEd Tanous     {
24253b00f5dSEd Tanous         BMCWEB_LOG_DEBUG("Power Limit not present for {}",
24353b00f5dSEd Tanous                          sensorAsyncResp->chassisId);
24453b00f5dSEd Tanous         return;
24553b00f5dSEd Tanous     }
246028f7ebcSEddie James 
247d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
248d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
249028f7ebcSEddie James         "/xyz/openbmc_project/control/host0/power_cap",
250d1bde9e5SKrzysztof Grobelny         "xyz.openbmc_project.Control.Power.Cap",
25153b00f5dSEd Tanous         [sensorAsyncResp](const boost::system::error_code& ec,
25253b00f5dSEd Tanous                           const dbus::utility::DBusPropertiesMap& properties
25353b00f5dSEd Tanous 
25453b00f5dSEd Tanous         ) { afterPowerCapSettingGet(sensorAsyncResp, ec, properties); });
25553b00f5dSEd Tanous }
25653b00f5dSEd Tanous 
25753b00f5dSEd Tanous inline void
25853b00f5dSEd Tanous     handleChassisPowerGet(App& app, const crow::Request& req,
25953b00f5dSEd Tanous                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
26053b00f5dSEd Tanous                           const std::string& chassisName)
26153b00f5dSEd Tanous {
26253b00f5dSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
26353b00f5dSEd Tanous     {
26453b00f5dSEd Tanous         return;
26553b00f5dSEd Tanous     }
26653b00f5dSEd Tanous     asyncResp->res.jsonValue["PowerControl"] = nlohmann::json::array();
26753b00f5dSEd Tanous 
26853b00f5dSEd Tanous     auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
26953b00f5dSEd Tanous         asyncResp, chassisName, sensors::dbus::powerPaths,
27053b00f5dSEd Tanous         sensors::node::power);
27153b00f5dSEd Tanous 
27253b00f5dSEd Tanous     getChassisData(sensorAsyncResp);
27353b00f5dSEd Tanous 
27453b00f5dSEd Tanous     // This callback verifies that the power limit is only provided
27553b00f5dSEd Tanous     // for the chassis that implements the Chassis inventory item.
27653b00f5dSEd Tanous     // This prevents things like power supplies providing the
27753b00f5dSEd Tanous     // chassis power limit
278028f7ebcSEddie James 
2797a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
280f857e9aeSAppaRao Puli         "xyz.openbmc_project.Inventory.Item.Board",
2817a1dbc48SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Chassis"};
2827a1dbc48SGeorge Liu 
28353b00f5dSEd Tanous     dbus::utility::getSubTreePaths(
28453b00f5dSEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces,
28553b00f5dSEd Tanous         std::bind_front(afterGetChassis, sensorAsyncResp));
28653b00f5dSEd Tanous }
2874bb3dc34SCarol Wang 
28853b00f5dSEd Tanous inline void
28953b00f5dSEd Tanous     handleChassisPowerPatch(App& app, const crow::Request& req,
2907e860f15SJohn Edward Broadbent                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
29153b00f5dSEd Tanous                             const std::string& chassisName)
29253b00f5dSEd Tanous {
2933ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
29445ca1b86SEd Tanous     {
29545ca1b86SEd Tanous         return;
29645ca1b86SEd Tanous     }
2978d1b46d7Szhanghch05     auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
29802da7c5aSEd Tanous         asyncResp, chassisName, sensors::dbus::powerPaths,
299a0ec28b6SAdrian Ambrożewicz         sensors::node::power);
3004bb3dc34SCarol Wang 
3010885057cSEd Tanous     std::optional<std::vector<nlohmann::json::object_t>> voltageCollections;
3020885057cSEd Tanous     std::optional<std::vector<nlohmann::json::object_t>> powerCtlCollections;
3034bb3dc34SCarol Wang 
304002d39b4SEd Tanous     if (!json_util::readJsonPatch(req, sensorAsyncResp->asyncResp->res,
305002d39b4SEd Tanous                                   "PowerControl", powerCtlCollections,
306002d39b4SEd Tanous                                   "Voltages", voltageCollections))
3074bb3dc34SCarol Wang     {
3084bb3dc34SCarol Wang         return;
3094bb3dc34SCarol Wang     }
3104bb3dc34SCarol Wang 
3114bb3dc34SCarol Wang     if (powerCtlCollections)
3124bb3dc34SCarol Wang     {
31353b00f5dSEd Tanous         redfish::chassis_utils::getValidChassisPath(
31453b00f5dSEd Tanous             sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
31553b00f5dSEd Tanous             std::bind_front(afterGetChassisPath, sensorAsyncResp,
31653b00f5dSEd Tanous                             *powerCtlCollections));
3174bb3dc34SCarol Wang     }
3184bb3dc34SCarol Wang     if (voltageCollections)
3194bb3dc34SCarol Wang     {
3200885057cSEd Tanous         std::unordered_map<std::string, std::vector<nlohmann::json::object_t>>
3214bb3dc34SCarol Wang             allCollections;
3220885057cSEd Tanous         allCollections.emplace("Voltages", std::move(*voltageCollections));
32380ac4024SBruce Lee         setSensorsOverride(sensorAsyncResp, allCollections);
3244bb3dc34SCarol Wang     }
32553b00f5dSEd Tanous }
32653b00f5dSEd Tanous 
32753b00f5dSEd Tanous inline void requestRoutesPower(App& app)
32853b00f5dSEd Tanous {
32953b00f5dSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
33053b00f5dSEd Tanous         .privileges(redfish::privileges::getPower)
33153b00f5dSEd Tanous         .methods(boost::beast::http::verb::get)(
33253b00f5dSEd Tanous             std::bind_front(handleChassisPowerGet, std::ref(app)));
33353b00f5dSEd Tanous 
33453b00f5dSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
33553b00f5dSEd Tanous         .privileges(redfish::privileges::patchPower)
33653b00f5dSEd Tanous         .methods(boost::beast::http::verb::patch)(
33753b00f5dSEd Tanous             std::bind_front(handleChassisPowerPatch, std::ref(app)));
338413961deSRichard Marian Thomaiyar }
3392474adfaSEd Tanous 
3402474adfaSEd Tanous } // namespace redfish
341