xref: /openbmc/bmcweb/features/redfish/lib/power.hpp (revision 3ba0007367777144f474fdf99439ae8c03633486)
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 
192474adfaSEd Tanous #include "sensors.hpp"
202474adfaSEd Tanous 
217e860f15SJohn Edward Broadbent #include <app.hpp>
22168e20c1SEd Tanous #include <dbus_utility.hpp>
2345ca1b86SEd Tanous #include <query.hpp>
24ed398213SEd Tanous #include <registries/privilege_registry.hpp>
257e860f15SJohn Edward Broadbent 
262474adfaSEd Tanous namespace redfish
272474adfaSEd Tanous {
284f48d5f6SEd Tanous inline void setPowerCapOverride(
298d1b46d7Szhanghch05     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
304bb3dc34SCarol Wang     std::vector<nlohmann::json>& powerControlCollections)
314bb3dc34SCarol Wang {
32002d39b4SEd Tanous     auto getChassisPath =
33002d39b4SEd Tanous         [sensorsAsyncResp, powerControlCollections](
34002d39b4SEd Tanous             const std::optional<std::string>& chassisPath) mutable {
354bb3dc34SCarol Wang         if (!chassisPath)
364bb3dc34SCarol Wang         {
374bb3dc34SCarol Wang             BMCWEB_LOG_ERROR << "Don't find valid chassis path ";
388d1b46d7Szhanghch05             messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
390fda0f12SGeorge Liu                                        "Chassis", sensorsAsyncResp->chassisId);
404bb3dc34SCarol Wang             return;
414bb3dc34SCarol Wang         }
424bb3dc34SCarol Wang 
434bb3dc34SCarol Wang         if (powerControlCollections.size() != 1)
444bb3dc34SCarol Wang         {
458d1b46d7Szhanghch05             BMCWEB_LOG_ERROR << "Don't support multiple hosts at present ";
468d1b46d7Szhanghch05             messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
478d1b46d7Szhanghch05                                        "Power", "PowerControl");
484bb3dc34SCarol Wang             return;
494bb3dc34SCarol Wang         }
504bb3dc34SCarol Wang 
514bb3dc34SCarol Wang         auto& item = powerControlCollections[0];
524bb3dc34SCarol Wang 
534bb3dc34SCarol Wang         std::optional<nlohmann::json> powerLimit;
548d1b46d7Szhanghch05         if (!json_util::readJson(item, sensorsAsyncResp->asyncResp->res,
558d1b46d7Szhanghch05                                  "PowerLimit", powerLimit))
564bb3dc34SCarol Wang         {
574bb3dc34SCarol Wang             return;
584bb3dc34SCarol Wang         }
594bb3dc34SCarol Wang         if (!powerLimit)
604bb3dc34SCarol Wang         {
614bb3dc34SCarol Wang             return;
624bb3dc34SCarol Wang         }
634bb3dc34SCarol Wang         std::optional<uint32_t> value;
640fda0f12SGeorge Liu         if (!json_util::readJson(*powerLimit, sensorsAsyncResp->asyncResp->res,
654bb3dc34SCarol Wang                                  "LimitInWatts", value))
664bb3dc34SCarol Wang         {
674bb3dc34SCarol Wang             return;
684bb3dc34SCarol Wang         }
694bb3dc34SCarol Wang         if (!value)
704bb3dc34SCarol Wang         {
714bb3dc34SCarol Wang             return;
724bb3dc34SCarol Wang         }
731e1e598dSJonathan Doman         sdbusplus::asio::getProperty<bool>(
741e1e598dSJonathan Doman             *crow::connections::systemBus, "xyz.openbmc_project.Settings",
751e1e598dSJonathan Doman             "/xyz/openbmc_project/control/host0/power_cap",
761e1e598dSJonathan Doman             "xyz.openbmc_project.Control.Power.Cap", "PowerCapEnable",
771e1e598dSJonathan Doman             [value, sensorsAsyncResp](const boost::system::error_code ec,
781e1e598dSJonathan Doman                                       bool powerCapEnable) {
794bb3dc34SCarol Wang             if (ec)
804bb3dc34SCarol Wang             {
818d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
82002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "powerCapEnable Get handler: Dbus error "
83002d39b4SEd Tanous                                  << ec;
844bb3dc34SCarol Wang                 return;
854bb3dc34SCarol Wang             }
861e1e598dSJonathan Doman             if (!powerCapEnable)
874bb3dc34SCarol Wang             {
884bb3dc34SCarol Wang                 messages::actionNotSupported(
898d1b46d7Szhanghch05                     sensorsAsyncResp->asyncResp->res,
900fda0f12SGeorge Liu                     "Setting LimitInWatts when PowerLimit feature is disabled");
914bb3dc34SCarol Wang                 BMCWEB_LOG_ERROR << "PowerLimit feature is disabled ";
924bb3dc34SCarol Wang                 return;
934bb3dc34SCarol Wang             }
944bb3dc34SCarol Wang 
954bb3dc34SCarol Wang             crow::connections::systemBus->async_method_call(
968d1b46d7Szhanghch05                 [sensorsAsyncResp](const boost::system::error_code ec2) {
9723a21a1cSEd Tanous                 if (ec2)
984bb3dc34SCarol Wang                 {
99002d39b4SEd Tanous                     BMCWEB_LOG_DEBUG << "Power Limit Set: Dbus error: " << ec2;
100002d39b4SEd Tanous                     messages::internalError(sensorsAsyncResp->asyncResp->res);
1014bb3dc34SCarol Wang                     return;
1024bb3dc34SCarol Wang                 }
1038d1b46d7Szhanghch05                 sensorsAsyncResp->asyncResp->res.result(
1044bb3dc34SCarol Wang                     boost::beast::http::status::no_content);
1054bb3dc34SCarol Wang                 },
1064bb3dc34SCarol Wang                 "xyz.openbmc_project.Settings",
1074bb3dc34SCarol Wang                 "/xyz/openbmc_project/control/host0/power_cap",
1084bb3dc34SCarol Wang                 "org.freedesktop.DBus.Properties", "Set",
1094bb3dc34SCarol Wang                 "xyz.openbmc_project.Control.Power.Cap", "PowerCap",
1101e1e598dSJonathan Doman                 std::variant<uint32_t>(*value));
1111e1e598dSJonathan Doman             });
1124bb3dc34SCarol Wang     };
1138d1b46d7Szhanghch05     getValidChassisPath(sensorsAsyncResp, std::move(getChassisPath));
1144bb3dc34SCarol Wang }
1157e860f15SJohn Edward Broadbent inline void requestRoutesPower(App& app)
1162474adfaSEd Tanous {
1172474adfaSEd Tanous 
1187e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
119ed398213SEd Tanous         .privileges(redfish::privileges::getPower)
120002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
121002d39b4SEd Tanous             [&app](const crow::Request& req,
12245ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1237e860f15SJohn Edward Broadbent                    const std::string& chassisName) {
124*3ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
12545ca1b86SEd Tanous         {
12645ca1b86SEd Tanous             return;
12745ca1b86SEd Tanous         }
128168e20c1SEd Tanous         asyncResp->res.jsonValue["PowerControl"] = nlohmann::json::array();
129c5d03ff4SJennifer Lee 
1302474adfaSEd Tanous         auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
13102da7c5aSEd Tanous             asyncResp, chassisName, sensors::dbus::powerPaths,
132a0ec28b6SAdrian Ambrożewicz             sensors::node::power);
133028f7ebcSEddie James 
1342474adfaSEd Tanous         getChassisData(sensorAsyncResp);
135028f7ebcSEddie James 
1367e860f15SJohn Edward Broadbent         // This callback verifies that the power limit is only provided
1377e860f15SJohn Edward Broadbent         // for the chassis that implements the Chassis inventory item.
1387e860f15SJohn Edward Broadbent         // This prevents things like power supplies providing the
1397e860f15SJohn Edward Broadbent         // chassis power limit
140b9d36b47SEd Tanous 
141b9d36b47SEd Tanous         using Mapper = dbus::utility::MapperGetSubTreePathsResponse;
142002d39b4SEd Tanous         auto chassisHandler =
143002d39b4SEd Tanous             [sensorAsyncResp](const boost::system::error_code e,
144b9d36b47SEd Tanous                               const Mapper& chassisPaths) {
145271584abSEd Tanous             if (e)
146028f7ebcSEddie James             {
147028f7ebcSEddie James                 BMCWEB_LOG_ERROR
148002d39b4SEd Tanous                     << "Power Limit GetSubTreePaths handler Dbus error " << e;
149028f7ebcSEddie James                 return;
150028f7ebcSEddie James             }
151028f7ebcSEddie James 
152028f7ebcSEddie James             bool found = false;
153028f7ebcSEddie James             for (const std::string& chassis : chassisPaths)
154028f7ebcSEddie James             {
155028f7ebcSEddie James                 size_t len = std::string::npos;
156f23b7296SEd Tanous                 size_t lastPos = chassis.rfind('/');
157028f7ebcSEddie James                 if (lastPos == std::string::npos)
158028f7ebcSEddie James                 {
159028f7ebcSEddie James                     continue;
160028f7ebcSEddie James                 }
161028f7ebcSEddie James 
162028f7ebcSEddie James                 if (lastPos == chassis.size() - 1)
163028f7ebcSEddie James                 {
164028f7ebcSEddie James                     size_t end = lastPos;
165f23b7296SEd Tanous                     lastPos = chassis.rfind('/', lastPos - 1);
166028f7ebcSEddie James                     if (lastPos == std::string::npos)
167028f7ebcSEddie James                     {
168028f7ebcSEddie James                         continue;
169028f7ebcSEddie James                     }
170028f7ebcSEddie James 
171028f7ebcSEddie James                     len = end - (lastPos + 1);
172028f7ebcSEddie James                 }
173028f7ebcSEddie James 
174028f7ebcSEddie James                 std::string interfaceChassisName =
175028f7ebcSEddie James                     chassis.substr(lastPos + 1, len);
17655f79e6fSEd Tanous                 if (interfaceChassisName == sensorAsyncResp->chassisId)
177028f7ebcSEddie James                 {
178028f7ebcSEddie James                     found = true;
179028f7ebcSEddie James                     break;
180028f7ebcSEddie James                 }
181028f7ebcSEddie James             }
182028f7ebcSEddie James 
183028f7ebcSEddie James             if (!found)
184028f7ebcSEddie James             {
185028f7ebcSEddie James                 BMCWEB_LOG_DEBUG << "Power Limit not present for "
186028f7ebcSEddie James                                  << sensorAsyncResp->chassisId;
187028f7ebcSEddie James                 return;
188028f7ebcSEddie James             }
189028f7ebcSEddie James 
190168e20c1SEd Tanous             auto valueHandler =
191168e20c1SEd Tanous                 [sensorAsyncResp](
192028f7ebcSEddie James                     const boost::system::error_code ec,
193b9d36b47SEd Tanous                     const dbus::utility::DBusPropertiesMap& properties) {
194028f7ebcSEddie James                 if (ec)
195028f7ebcSEddie James                 {
196002d39b4SEd Tanous                     messages::internalError(sensorAsyncResp->asyncResp->res);
197028f7ebcSEddie James                     BMCWEB_LOG_ERROR
198002d39b4SEd Tanous                         << "Power Limit GetAll handler: Dbus error " << ec;
199028f7ebcSEddie James                     return;
200028f7ebcSEddie James                 }
201028f7ebcSEddie James 
2027e860f15SJohn Edward Broadbent                 nlohmann::json& tempArray =
203002d39b4SEd Tanous                     sensorAsyncResp->asyncResp->res.jsonValue["PowerControl"];
204028f7ebcSEddie James 
2057e860f15SJohn Edward Broadbent                 // Put multiple "sensors" into a single PowerControl, 0,
2067e860f15SJohn Edward Broadbent                 // so only create the first one
207028f7ebcSEddie James                 if (tempArray.empty())
208028f7ebcSEddie James                 {
2097ab06f49SGunnar Mills                     // Mandatory properties odata.id and MemberId
2107ab06f49SGunnar Mills                     // A warning without a odata.type
2111476687dSEd Tanous                     nlohmann::json::object_t powerControl;
212002d39b4SEd Tanous                     powerControl["@odata.type"] = "#Power.v1_0_0.PowerControl";
213002d39b4SEd Tanous                     powerControl["@odata.id"] = "/redfish/v1/Chassis/" +
2147ab06f49SGunnar Mills                                                 sensorAsyncResp->chassisId +
2151476687dSEd Tanous                                                 "/Power#/PowerControl/0";
2161476687dSEd Tanous                     powerControl["Name"] = "Chassis Power Control";
2171476687dSEd Tanous                     powerControl["MemberId"] = "0";
2181476687dSEd Tanous                     tempArray.push_back(std::move(powerControl));
219028f7ebcSEddie James                 }
220028f7ebcSEddie James 
221028f7ebcSEddie James                 nlohmann::json& sensorJson = tempArray.back();
222028f7ebcSEddie James                 bool enabled = false;
223028f7ebcSEddie James                 double powerCap = 0.0;
224028f7ebcSEddie James                 int64_t scale = 0;
225028f7ebcSEddie James 
226168e20c1SEd Tanous                 for (const std::pair<std::string,
227002d39b4SEd Tanous                                      dbus::utility::DbusVariantType>& property :
228002d39b4SEd Tanous                      properties)
229028f7ebcSEddie James                 {
23055f79e6fSEd Tanous                     if (property.first == "Scale")
231028f7ebcSEddie James                     {
232028f7ebcSEddie James                         const int64_t* i =
2338d78b7a9SPatrick Williams                             std::get_if<int64_t>(&property.second);
234028f7ebcSEddie James 
235e662eae8SEd Tanous                         if (i != nullptr)
236028f7ebcSEddie James                         {
237028f7ebcSEddie James                             scale = *i;
238028f7ebcSEddie James                         }
239028f7ebcSEddie James                     }
24055f79e6fSEd Tanous                     else if (property.first == "PowerCap")
241028f7ebcSEddie James                     {
242002d39b4SEd Tanous                         const double* d = std::get_if<double>(&property.second);
243028f7ebcSEddie James                         const int64_t* i =
2448d78b7a9SPatrick Williams                             std::get_if<int64_t>(&property.second);
245028f7ebcSEddie James                         const uint32_t* u =
2468d78b7a9SPatrick Williams                             std::get_if<uint32_t>(&property.second);
247028f7ebcSEddie James 
248e662eae8SEd Tanous                         if (d != nullptr)
249028f7ebcSEddie James                         {
250028f7ebcSEddie James                             powerCap = *d;
251028f7ebcSEddie James                         }
252e662eae8SEd Tanous                         else if (i != nullptr)
253028f7ebcSEddie James                         {
254271584abSEd Tanous                             powerCap = static_cast<double>(*i);
255028f7ebcSEddie James                         }
256e662eae8SEd Tanous                         else if (u != nullptr)
257028f7ebcSEddie James                         {
258028f7ebcSEddie James                             powerCap = *u;
259028f7ebcSEddie James                         }
260028f7ebcSEddie James                     }
26155f79e6fSEd Tanous                     else if (property.first == "PowerCapEnable")
262028f7ebcSEddie James                     {
263002d39b4SEd Tanous                         const bool* b = std::get_if<bool>(&property.second);
264028f7ebcSEddie James 
265e662eae8SEd Tanous                         if (b != nullptr)
266028f7ebcSEddie James                         {
267028f7ebcSEddie James                             enabled = *b;
268028f7ebcSEddie James                         }
269028f7ebcSEddie James                     }
270028f7ebcSEddie James                 }
271028f7ebcSEddie James 
2727ab06f49SGunnar Mills                 nlohmann::json& value =
2737ab06f49SGunnar Mills                     sensorJson["PowerLimit"]["LimitInWatts"];
274028f7ebcSEddie James 
2757e860f15SJohn Edward Broadbent                 // LimitException is Mandatory attribute as per OCP
2767e860f15SJohn Edward Broadbent                 // Baseline Profile – v1.0.0, so currently making it
2777e860f15SJohn Edward Broadbent                 // "NoAction" as default value to make it OCP Compliant.
2785a64a6f3SJoshi-Mansi                 sensorJson["PowerLimit"]["LimitException"] = "NoAction";
2795a64a6f3SJoshi-Mansi 
280028f7ebcSEddie James                 if (enabled)
281028f7ebcSEddie James                 {
2827e860f15SJohn Edward Broadbent                     // Redfish specification indicates PowerLimit should
2837e860f15SJohn Edward Broadbent                     // be null if the limit is not enabled.
284028f7ebcSEddie James                     value = powerCap * std::pow(10, scale);
285028f7ebcSEddie James                 }
286028f7ebcSEddie James             };
287028f7ebcSEddie James 
288028f7ebcSEddie James             crow::connections::systemBus->async_method_call(
289028f7ebcSEddie James                 std::move(valueHandler), "xyz.openbmc_project.Settings",
290028f7ebcSEddie James                 "/xyz/openbmc_project/control/host0/power_cap",
291028f7ebcSEddie James                 "org.freedesktop.DBus.Properties", "GetAll",
292028f7ebcSEddie James                 "xyz.openbmc_project.Control.Power.Cap");
293028f7ebcSEddie James         };
294028f7ebcSEddie James 
295028f7ebcSEddie James         crow::connections::systemBus->async_method_call(
296168e20c1SEd Tanous             std::move(chassisHandler), "xyz.openbmc_project.ObjectMapper",
297028f7ebcSEddie James             "/xyz/openbmc_project/object_mapper",
298028f7ebcSEddie James             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
299271584abSEd Tanous             "/xyz/openbmc_project/inventory", 0,
300f857e9aeSAppaRao Puli             std::array<const char*, 2>{
301f857e9aeSAppaRao Puli                 "xyz.openbmc_project.Inventory.Item.Board",
302028f7ebcSEddie James                 "xyz.openbmc_project.Inventory.Item.Chassis"});
3037e860f15SJohn Edward Broadbent         });
3044bb3dc34SCarol Wang 
3057e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
306ed398213SEd Tanous         .privileges(redfish::privileges::patchPower)
3077e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
30845ca1b86SEd Tanous             [&app](const crow::Request& req,
3097e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3107e860f15SJohn Edward Broadbent                    const std::string& chassisName) {
311*3ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
31245ca1b86SEd Tanous         {
31345ca1b86SEd Tanous             return;
31445ca1b86SEd Tanous         }
3158d1b46d7Szhanghch05         auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
31602da7c5aSEd Tanous             asyncResp, chassisName, sensors::dbus::powerPaths,
317a0ec28b6SAdrian Ambrożewicz             sensors::node::power);
3184bb3dc34SCarol Wang 
3194bb3dc34SCarol Wang         std::optional<std::vector<nlohmann::json>> voltageCollections;
3204bb3dc34SCarol Wang         std::optional<std::vector<nlohmann::json>> powerCtlCollections;
3214bb3dc34SCarol Wang 
322002d39b4SEd Tanous         if (!json_util::readJsonPatch(req, sensorAsyncResp->asyncResp->res,
323002d39b4SEd Tanous                                       "PowerControl", powerCtlCollections,
324002d39b4SEd Tanous                                       "Voltages", voltageCollections))
3254bb3dc34SCarol Wang         {
3264bb3dc34SCarol Wang             return;
3274bb3dc34SCarol Wang         }
3284bb3dc34SCarol Wang 
3294bb3dc34SCarol Wang         if (powerCtlCollections)
3304bb3dc34SCarol Wang         {
3318d1b46d7Szhanghch05             setPowerCapOverride(sensorAsyncResp, *powerCtlCollections);
3324bb3dc34SCarol Wang         }
3334bb3dc34SCarol Wang         if (voltageCollections)
3344bb3dc34SCarol Wang         {
3354bb3dc34SCarol Wang             std::unordered_map<std::string, std::vector<nlohmann::json>>
3364bb3dc34SCarol Wang                 allCollections;
337002d39b4SEd Tanous             allCollections.emplace("Voltages", *std::move(voltageCollections));
33880ac4024SBruce Lee             setSensorsOverride(sensorAsyncResp, allCollections);
3394bb3dc34SCarol Wang         }
3407e860f15SJohn Edward Broadbent         });
341413961deSRichard Marian Thomaiyar }
3422474adfaSEd Tanous 
3432474adfaSEd Tanous } // namespace redfish
344