xref: /openbmc/bmcweb/features/redfish/lib/power.hpp (revision 5b90429a75c58797ec29ac9a8ed18c2dcd7d4950)
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"
25*5b90429aSEd 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 
5787c44966SAsmitha Karunanithi     setDbusProperty(sensorsAsyncResp->asyncResp, "xyz.openbmc_project.Settings",
5887c44966SAsmitha Karunanithi                     sdbusplus::message::object_path(
5987c44966SAsmitha Karunanithi                         "/xyz/openbmc_project/control/host0/power_cap"),
6087c44966SAsmitha Karunanithi                     "xyz.openbmc_project.Control.Power.Cap", "PowerCap",
6187c44966SAsmitha Karunanithi                     "PowerControl", valueToSet);
6253b00f5dSEd Tanous }
6353b00f5dSEd Tanous 
6453b00f5dSEd Tanous inline void afterGetChassisPath(
6553b00f5dSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
660885057cSEd Tanous     std::vector<nlohmann::json::object_t>& powerControlCollections,
6753b00f5dSEd Tanous     const std::optional<std::string>& chassisPath)
6853b00f5dSEd Tanous {
694bb3dc34SCarol Wang     if (!chassisPath)
704bb3dc34SCarol Wang     {
7162598e31SEd Tanous         BMCWEB_LOG_WARNING("Don't find valid chassis path ");
7253b00f5dSEd Tanous         messages::resourceNotFound(sensorsAsyncResp->asyncResp->res, "Chassis",
7353b00f5dSEd Tanous                                    sensorsAsyncResp->chassisId);
744bb3dc34SCarol Wang         return;
754bb3dc34SCarol Wang     }
764bb3dc34SCarol Wang 
774bb3dc34SCarol Wang     if (powerControlCollections.size() != 1)
784bb3dc34SCarol Wang     {
7962598e31SEd Tanous         BMCWEB_LOG_WARNING("Don't support multiple hosts at present ");
8053b00f5dSEd Tanous         messages::resourceNotFound(sensorsAsyncResp->asyncResp->res, "Power",
8153b00f5dSEd Tanous                                    "PowerControl");
824bb3dc34SCarol Wang         return;
834bb3dc34SCarol Wang     }
844bb3dc34SCarol Wang 
854bb3dc34SCarol Wang     auto& item = powerControlCollections[0];
864bb3dc34SCarol Wang 
874bb3dc34SCarol Wang     std::optional<uint32_t> value;
880885057cSEd Tanous     if (!json_util::readJsonObject(item, sensorsAsyncResp->asyncResp->res,
890885057cSEd Tanous                                    "PowerLimit/LimitInWatts", value))
904bb3dc34SCarol Wang     {
914bb3dc34SCarol Wang         return;
924bb3dc34SCarol Wang     }
934bb3dc34SCarol Wang     if (!value)
944bb3dc34SCarol Wang     {
954bb3dc34SCarol Wang         return;
964bb3dc34SCarol Wang     }
971e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
981e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
991e1e598dSJonathan Doman         "/xyz/openbmc_project/control/host0/power_cap",
1001e1e598dSJonathan Doman         "xyz.openbmc_project.Control.Power.Cap", "PowerCapEnable",
10153b00f5dSEd Tanous         std::bind_front(afterGetPowerCapEnable, sensorsAsyncResp, *value));
1024bb3dc34SCarol Wang }
1034bb3dc34SCarol Wang 
10453b00f5dSEd Tanous inline void afterPowerCapSettingGet(
10553b00f5dSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
1065e7e2dc5SEd Tanous     const boost::system::error_code& ec,
10753b00f5dSEd Tanous     const dbus::utility::DBusPropertiesMap& properties)
10853b00f5dSEd Tanous {
109028f7ebcSEddie James     if (ec)
110028f7ebcSEddie James     {
111002d39b4SEd Tanous         messages::internalError(sensorAsyncResp->asyncResp->res);
11253b00f5dSEd Tanous         BMCWEB_LOG_ERROR("Power Limit GetAll handler: Dbus error {}", ec);
113028f7ebcSEddie James         return;
114028f7ebcSEddie James     }
115028f7ebcSEddie James 
1167e860f15SJohn Edward Broadbent     nlohmann::json& tempArray =
117002d39b4SEd Tanous         sensorAsyncResp->asyncResp->res.jsonValue["PowerControl"];
118028f7ebcSEddie James 
1197e860f15SJohn Edward Broadbent     // Put multiple "sensors" into a single PowerControl, 0,
1207e860f15SJohn Edward Broadbent     // so only create the first one
121028f7ebcSEddie James     if (tempArray.empty())
122028f7ebcSEddie James     {
1237ab06f49SGunnar Mills         // Mandatory properties odata.id and MemberId
1247ab06f49SGunnar Mills         // A warning without a odata.type
1251476687dSEd Tanous         nlohmann::json::object_t powerControl;
126002d39b4SEd Tanous         powerControl["@odata.type"] = "#Power.v1_0_0.PowerControl";
127002d39b4SEd Tanous         powerControl["@odata.id"] = "/redfish/v1/Chassis/" +
1287ab06f49SGunnar Mills                                     sensorAsyncResp->chassisId +
1291476687dSEd Tanous                                     "/Power#/PowerControl/0";
1301476687dSEd Tanous         powerControl["Name"] = "Chassis Power Control";
1311476687dSEd Tanous         powerControl["MemberId"] = "0";
132b2ba3072SPatrick Williams         tempArray.emplace_back(std::move(powerControl));
133028f7ebcSEddie James     }
134028f7ebcSEddie James 
135028f7ebcSEddie James     nlohmann::json& sensorJson = tempArray.back();
136028f7ebcSEddie James     bool enabled = false;
137028f7ebcSEddie James     double powerCap = 0.0;
138028f7ebcSEddie James     int64_t scale = 0;
139028f7ebcSEddie James 
14053b00f5dSEd Tanous     for (const std::pair<std::string, dbus::utility::DbusVariantType>&
14153b00f5dSEd Tanous              property : properties)
142028f7ebcSEddie James     {
14355f79e6fSEd Tanous         if (property.first == "Scale")
144028f7ebcSEddie James         {
14553b00f5dSEd Tanous             const int64_t* i = std::get_if<int64_t>(&property.second);
146028f7ebcSEddie James 
147e662eae8SEd Tanous             if (i != nullptr)
148028f7ebcSEddie James             {
149028f7ebcSEddie James                 scale = *i;
150028f7ebcSEddie James             }
151028f7ebcSEddie James         }
15255f79e6fSEd Tanous         else if (property.first == "PowerCap")
153028f7ebcSEddie James         {
154002d39b4SEd Tanous             const double* d = std::get_if<double>(&property.second);
15553b00f5dSEd Tanous             const int64_t* i = std::get_if<int64_t>(&property.second);
15653b00f5dSEd Tanous             const uint32_t* u = std::get_if<uint32_t>(&property.second);
157028f7ebcSEddie James 
158e662eae8SEd Tanous             if (d != nullptr)
159028f7ebcSEddie James             {
160028f7ebcSEddie James                 powerCap = *d;
161028f7ebcSEddie James             }
162e662eae8SEd Tanous             else if (i != nullptr)
163028f7ebcSEddie James             {
164271584abSEd Tanous                 powerCap = static_cast<double>(*i);
165028f7ebcSEddie James             }
166e662eae8SEd Tanous             else if (u != nullptr)
167028f7ebcSEddie James             {
168028f7ebcSEddie James                 powerCap = *u;
169028f7ebcSEddie James             }
170028f7ebcSEddie James         }
17155f79e6fSEd Tanous         else if (property.first == "PowerCapEnable")
172028f7ebcSEddie James         {
173002d39b4SEd Tanous             const bool* b = std::get_if<bool>(&property.second);
174028f7ebcSEddie James 
175e662eae8SEd Tanous             if (b != nullptr)
176028f7ebcSEddie James             {
177028f7ebcSEddie James                 enabled = *b;
178028f7ebcSEddie James             }
179028f7ebcSEddie James         }
180028f7ebcSEddie James     }
181028f7ebcSEddie James 
1827e860f15SJohn Edward Broadbent     // LimitException is Mandatory attribute as per OCP
1837e860f15SJohn Edward Broadbent     // Baseline Profile – v1.0.0, so currently making it
1847e860f15SJohn Edward Broadbent     // "NoAction" as default value to make it OCP Compliant.
1855a64a6f3SJoshi-Mansi     sensorJson["PowerLimit"]["LimitException"] = "NoAction";
1865a64a6f3SJoshi-Mansi 
187028f7ebcSEddie James     if (enabled)
188028f7ebcSEddie James     {
1897e860f15SJohn Edward Broadbent         // Redfish specification indicates PowerLimit should
1907e860f15SJohn Edward Broadbent         // be null if the limit is not enabled.
19153b00f5dSEd Tanous         sensorJson["PowerLimit"]["LimitInWatts"] = powerCap *
19253b00f5dSEd Tanous                                                    std::pow(10, scale);
193028f7ebcSEddie James     }
19453b00f5dSEd Tanous }
19553b00f5dSEd Tanous 
19653b00f5dSEd Tanous using Mapper = dbus::utility::MapperGetSubTreePathsResponse;
19753b00f5dSEd Tanous inline void
19853b00f5dSEd Tanous     afterGetChassis(const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
19953b00f5dSEd Tanous                     const boost::system::error_code& ec2,
20053b00f5dSEd Tanous                     const Mapper& chassisPaths)
20153b00f5dSEd Tanous {
20253b00f5dSEd Tanous     if (ec2)
20353b00f5dSEd Tanous     {
20453b00f5dSEd Tanous         BMCWEB_LOG_ERROR("Power Limit GetSubTreePaths handler Dbus error {}",
20553b00f5dSEd Tanous                          ec2);
20653b00f5dSEd Tanous         return;
20753b00f5dSEd Tanous     }
20853b00f5dSEd Tanous 
20953b00f5dSEd Tanous     bool found = false;
21053b00f5dSEd Tanous     for (const std::string& chassis : chassisPaths)
21153b00f5dSEd Tanous     {
21253b00f5dSEd Tanous         size_t len = std::string::npos;
21353b00f5dSEd Tanous         size_t lastPos = chassis.rfind('/');
21453b00f5dSEd Tanous         if (lastPos == std::string::npos)
21553b00f5dSEd Tanous         {
21653b00f5dSEd Tanous             continue;
21753b00f5dSEd Tanous         }
21853b00f5dSEd Tanous 
21953b00f5dSEd Tanous         if (lastPos == chassis.size() - 1)
22053b00f5dSEd Tanous         {
22153b00f5dSEd Tanous             size_t end = lastPos;
22253b00f5dSEd Tanous             lastPos = chassis.rfind('/', lastPos - 1);
22353b00f5dSEd Tanous             if (lastPos == std::string::npos)
22453b00f5dSEd Tanous             {
22553b00f5dSEd Tanous                 continue;
22653b00f5dSEd Tanous             }
22753b00f5dSEd Tanous 
22853b00f5dSEd Tanous             len = end - (lastPos + 1);
22953b00f5dSEd Tanous         }
23053b00f5dSEd Tanous 
23153b00f5dSEd Tanous         std::string interfaceChassisName = chassis.substr(lastPos + 1, len);
23253b00f5dSEd Tanous         if (interfaceChassisName == sensorAsyncResp->chassisId)
23353b00f5dSEd Tanous         {
23453b00f5dSEd Tanous             found = true;
23553b00f5dSEd Tanous             break;
23653b00f5dSEd Tanous         }
23753b00f5dSEd Tanous     }
23853b00f5dSEd Tanous 
23953b00f5dSEd Tanous     if (!found)
24053b00f5dSEd Tanous     {
24153b00f5dSEd Tanous         BMCWEB_LOG_DEBUG("Power Limit not present for {}",
24253b00f5dSEd Tanous                          sensorAsyncResp->chassisId);
24353b00f5dSEd Tanous         return;
24453b00f5dSEd Tanous     }
245028f7ebcSEddie James 
246d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
247d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
248028f7ebcSEddie James         "/xyz/openbmc_project/control/host0/power_cap",
249d1bde9e5SKrzysztof Grobelny         "xyz.openbmc_project.Control.Power.Cap",
25053b00f5dSEd Tanous         [sensorAsyncResp](const boost::system::error_code& ec,
25153b00f5dSEd Tanous                           const dbus::utility::DBusPropertiesMap& properties
25253b00f5dSEd Tanous 
25353b00f5dSEd Tanous         ) { afterPowerCapSettingGet(sensorAsyncResp, ec, properties); });
25453b00f5dSEd Tanous }
25553b00f5dSEd Tanous 
25653b00f5dSEd Tanous inline void
25753b00f5dSEd Tanous     handleChassisPowerGet(App& app, const crow::Request& req,
25853b00f5dSEd Tanous                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25953b00f5dSEd Tanous                           const std::string& chassisName)
26053b00f5dSEd Tanous {
26153b00f5dSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
26253b00f5dSEd Tanous     {
26353b00f5dSEd Tanous         return;
26453b00f5dSEd Tanous     }
26553b00f5dSEd Tanous     asyncResp->res.jsonValue["PowerControl"] = nlohmann::json::array();
26653b00f5dSEd Tanous 
26753b00f5dSEd Tanous     auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
26853b00f5dSEd Tanous         asyncResp, chassisName, sensors::dbus::powerPaths,
26953b00f5dSEd Tanous         sensors::node::power);
27053b00f5dSEd Tanous 
27153b00f5dSEd Tanous     getChassisData(sensorAsyncResp);
27253b00f5dSEd Tanous 
27353b00f5dSEd Tanous     // This callback verifies that the power limit is only provided
27453b00f5dSEd Tanous     // for the chassis that implements the Chassis inventory item.
27553b00f5dSEd Tanous     // This prevents things like power supplies providing the
27653b00f5dSEd Tanous     // chassis power limit
277028f7ebcSEddie James 
2787a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
279f857e9aeSAppaRao Puli         "xyz.openbmc_project.Inventory.Item.Board",
2807a1dbc48SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Chassis"};
2817a1dbc48SGeorge Liu 
28253b00f5dSEd Tanous     dbus::utility::getSubTreePaths(
28353b00f5dSEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces,
28453b00f5dSEd Tanous         std::bind_front(afterGetChassis, sensorAsyncResp));
28553b00f5dSEd Tanous }
2864bb3dc34SCarol Wang 
28753b00f5dSEd Tanous inline void
28853b00f5dSEd Tanous     handleChassisPowerPatch(App& app, const crow::Request& req,
2897e860f15SJohn Edward Broadbent                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
29053b00f5dSEd Tanous                             const std::string& chassisName)
29153b00f5dSEd Tanous {
2923ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
29345ca1b86SEd Tanous     {
29445ca1b86SEd Tanous         return;
29545ca1b86SEd Tanous     }
2968d1b46d7Szhanghch05     auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
29702da7c5aSEd Tanous         asyncResp, chassisName, sensors::dbus::powerPaths,
298a0ec28b6SAdrian Ambrożewicz         sensors::node::power);
2994bb3dc34SCarol Wang 
3000885057cSEd Tanous     std::optional<std::vector<nlohmann::json::object_t>> voltageCollections;
3010885057cSEd Tanous     std::optional<std::vector<nlohmann::json::object_t>> powerCtlCollections;
3024bb3dc34SCarol Wang 
303002d39b4SEd Tanous     if (!json_util::readJsonPatch(req, sensorAsyncResp->asyncResp->res,
304002d39b4SEd Tanous                                   "PowerControl", powerCtlCollections,
305002d39b4SEd Tanous                                   "Voltages", voltageCollections))
3064bb3dc34SCarol Wang     {
3074bb3dc34SCarol Wang         return;
3084bb3dc34SCarol Wang     }
3094bb3dc34SCarol Wang 
3104bb3dc34SCarol Wang     if (powerCtlCollections)
3114bb3dc34SCarol Wang     {
31253b00f5dSEd Tanous         redfish::chassis_utils::getValidChassisPath(
31353b00f5dSEd Tanous             sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
31453b00f5dSEd Tanous             std::bind_front(afterGetChassisPath, sensorAsyncResp,
31553b00f5dSEd Tanous                             *powerCtlCollections));
3164bb3dc34SCarol Wang     }
3174bb3dc34SCarol Wang     if (voltageCollections)
3184bb3dc34SCarol Wang     {
3190885057cSEd Tanous         std::unordered_map<std::string, std::vector<nlohmann::json::object_t>>
3204bb3dc34SCarol Wang             allCollections;
3210885057cSEd Tanous         allCollections.emplace("Voltages", std::move(*voltageCollections));
32280ac4024SBruce Lee         setSensorsOverride(sensorAsyncResp, allCollections);
3234bb3dc34SCarol Wang     }
32453b00f5dSEd Tanous }
32553b00f5dSEd Tanous 
32653b00f5dSEd Tanous inline void requestRoutesPower(App& app)
32753b00f5dSEd Tanous {
32853b00f5dSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
32953b00f5dSEd Tanous         .privileges(redfish::privileges::getPower)
33053b00f5dSEd Tanous         .methods(boost::beast::http::verb::get)(
33153b00f5dSEd Tanous             std::bind_front(handleChassisPowerGet, std::ref(app)));
33253b00f5dSEd Tanous 
33353b00f5dSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
33453b00f5dSEd Tanous         .privileges(redfish::privileges::patchPower)
33553b00f5dSEd Tanous         .methods(boost::beast::http::verb::patch)(
33653b00f5dSEd Tanous             std::bind_front(handleChassisPowerPatch, std::ref(app)));
337413961deSRichard Marian Thomaiyar }
3382474adfaSEd Tanous 
3392474adfaSEd Tanous } // namespace redfish
340