xref: /openbmc/bmcweb/features/redfish/lib/power.hpp (revision e662eae819f545ef07193f216b42e161b7ff7a46)
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>
23ed398213SEd Tanous #include <registries/privilege_registry.hpp>
247e860f15SJohn Edward Broadbent 
252474adfaSEd Tanous namespace redfish
262474adfaSEd Tanous {
274f48d5f6SEd Tanous inline void setPowerCapOverride(
288d1b46d7Szhanghch05     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
294bb3dc34SCarol Wang     std::vector<nlohmann::json>& powerControlCollections)
304bb3dc34SCarol Wang {
310fda0f12SGeorge Liu     auto getChassisPath = [sensorsAsyncResp, powerControlCollections](
320fda0f12SGeorge Liu                               const std::optional<std::string>&
330fda0f12SGeorge Liu                                   chassisPath) mutable {
344bb3dc34SCarol Wang         if (!chassisPath)
354bb3dc34SCarol Wang         {
364bb3dc34SCarol Wang             BMCWEB_LOG_ERROR << "Don't find valid chassis path ";
378d1b46d7Szhanghch05             messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
380fda0f12SGeorge Liu                                        "Chassis", sensorsAsyncResp->chassisId);
394bb3dc34SCarol Wang             return;
404bb3dc34SCarol Wang         }
414bb3dc34SCarol Wang 
424bb3dc34SCarol Wang         if (powerControlCollections.size() != 1)
434bb3dc34SCarol Wang         {
448d1b46d7Szhanghch05             BMCWEB_LOG_ERROR << "Don't support multiple hosts at present ";
458d1b46d7Szhanghch05             messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
468d1b46d7Szhanghch05                                        "Power", "PowerControl");
474bb3dc34SCarol Wang             return;
484bb3dc34SCarol Wang         }
494bb3dc34SCarol Wang 
504bb3dc34SCarol Wang         auto& item = powerControlCollections[0];
514bb3dc34SCarol Wang 
524bb3dc34SCarol Wang         std::optional<nlohmann::json> powerLimit;
538d1b46d7Szhanghch05         if (!json_util::readJson(item, sensorsAsyncResp->asyncResp->res,
548d1b46d7Szhanghch05                                  "PowerLimit", powerLimit))
554bb3dc34SCarol Wang         {
564bb3dc34SCarol Wang             return;
574bb3dc34SCarol Wang         }
584bb3dc34SCarol Wang         if (!powerLimit)
594bb3dc34SCarol Wang         {
604bb3dc34SCarol Wang             return;
614bb3dc34SCarol Wang         }
624bb3dc34SCarol Wang         std::optional<uint32_t> value;
630fda0f12SGeorge Liu         if (!json_util::readJson(*powerLimit, sensorsAsyncResp->asyncResp->res,
644bb3dc34SCarol Wang                                  "LimitInWatts", value))
654bb3dc34SCarol Wang         {
664bb3dc34SCarol Wang             return;
674bb3dc34SCarol Wang         }
684bb3dc34SCarol Wang         if (!value)
694bb3dc34SCarol Wang         {
704bb3dc34SCarol Wang             return;
714bb3dc34SCarol Wang         }
721e1e598dSJonathan Doman         sdbusplus::asio::getProperty<bool>(
731e1e598dSJonathan Doman             *crow::connections::systemBus, "xyz.openbmc_project.Settings",
741e1e598dSJonathan Doman             "/xyz/openbmc_project/control/host0/power_cap",
751e1e598dSJonathan Doman             "xyz.openbmc_project.Control.Power.Cap", "PowerCapEnable",
761e1e598dSJonathan Doman             [value, sensorsAsyncResp](const boost::system::error_code ec,
771e1e598dSJonathan Doman                                       bool powerCapEnable) {
784bb3dc34SCarol Wang                 if (ec)
794bb3dc34SCarol Wang                 {
808d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
811e1e598dSJonathan Doman                     BMCWEB_LOG_ERROR
821e1e598dSJonathan Doman                         << "powerCapEnable Get handler: Dbus error " << ec;
834bb3dc34SCarol Wang                     return;
844bb3dc34SCarol Wang                 }
851e1e598dSJonathan Doman                 if (!powerCapEnable)
864bb3dc34SCarol Wang                 {
874bb3dc34SCarol Wang                     messages::actionNotSupported(
888d1b46d7Szhanghch05                         sensorsAsyncResp->asyncResp->res,
890fda0f12SGeorge Liu                         "Setting LimitInWatts when PowerLimit feature is disabled");
904bb3dc34SCarol Wang                     BMCWEB_LOG_ERROR << "PowerLimit feature is disabled ";
914bb3dc34SCarol Wang                     return;
924bb3dc34SCarol Wang                 }
934bb3dc34SCarol Wang 
944bb3dc34SCarol Wang                 crow::connections::systemBus->async_method_call(
958d1b46d7Szhanghch05                     [sensorsAsyncResp](const boost::system::error_code ec2) {
9623a21a1cSEd Tanous                         if (ec2)
974bb3dc34SCarol Wang                         {
988d1b46d7Szhanghch05                             BMCWEB_LOG_DEBUG << "Power Limit Set: Dbus error: "
998d1b46d7Szhanghch05                                              << ec2;
1008d1b46d7Szhanghch05                             messages::internalError(
1018d1b46d7Szhanghch05                                 sensorsAsyncResp->asyncResp->res);
1024bb3dc34SCarol Wang                             return;
1034bb3dc34SCarol Wang                         }
1048d1b46d7Szhanghch05                         sensorsAsyncResp->asyncResp->res.result(
1054bb3dc34SCarol Wang                             boost::beast::http::status::no_content);
1064bb3dc34SCarol Wang                     },
1074bb3dc34SCarol Wang                     "xyz.openbmc_project.Settings",
1084bb3dc34SCarol Wang                     "/xyz/openbmc_project/control/host0/power_cap",
1094bb3dc34SCarol Wang                     "org.freedesktop.DBus.Properties", "Set",
1104bb3dc34SCarol Wang                     "xyz.openbmc_project.Control.Power.Cap", "PowerCap",
1111e1e598dSJonathan Doman                     std::variant<uint32_t>(*value));
1121e1e598dSJonathan Doman             });
1134bb3dc34SCarol Wang     };
1148d1b46d7Szhanghch05     getValidChassisPath(sensorsAsyncResp, std::move(getChassisPath));
1154bb3dc34SCarol Wang }
1167e860f15SJohn Edward Broadbent inline void requestRoutesPower(App& app)
1172474adfaSEd Tanous {
1182474adfaSEd Tanous 
1197e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
120ed398213SEd Tanous         .privileges(redfish::privileges::getPower)
121168e20c1SEd Tanous         .methods(
122168e20c1SEd Tanous             boost::beast::http::verb::get)([](const crow::Request&,
123168e20c1SEd Tanous                                               const std::shared_ptr<
124168e20c1SEd Tanous                                                   bmcweb::AsyncResp>& asyncResp,
1257e860f15SJohn Edward Broadbent                                               const std::string& chassisName) {
126168e20c1SEd Tanous             asyncResp->res.jsonValue["PowerControl"] = nlohmann::json::array();
127c5d03ff4SJennifer Lee 
1282474adfaSEd Tanous             auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
1298d1b46d7Szhanghch05                 asyncResp, chassisName,
1308d1b46d7Szhanghch05                 sensors::dbus::paths.at(sensors::node::power),
131a0ec28b6SAdrian Ambrożewicz                 sensors::node::power);
132028f7ebcSEddie James 
1332474adfaSEd Tanous             getChassisData(sensorAsyncResp);
134028f7ebcSEddie James 
1357e860f15SJohn Edward Broadbent             // This callback verifies that the power limit is only provided
1367e860f15SJohn Edward Broadbent             // for the chassis that implements the Chassis inventory item.
1377e860f15SJohn Edward Broadbent             // This prevents things like power supplies providing the
1387e860f15SJohn Edward Broadbent             // chassis power limit
139028f7ebcSEddie James             auto chassisHandler = [sensorAsyncResp](
140271584abSEd Tanous                                       const boost::system::error_code e,
141028f7ebcSEddie James                                       const std::vector<std::string>&
142028f7ebcSEddie James                                           chassisPaths) {
143271584abSEd Tanous                 if (e)
144028f7ebcSEddie James                 {
145028f7ebcSEddie James                     BMCWEB_LOG_ERROR
1467e860f15SJohn Edward Broadbent                         << "Power Limit GetSubTreePaths handler Dbus error "
1477e860f15SJohn Edward Broadbent                         << e;
148028f7ebcSEddie James                     return;
149028f7ebcSEddie James                 }
150028f7ebcSEddie James 
151028f7ebcSEddie James                 bool found = false;
152028f7ebcSEddie James                 for (const std::string& chassis : chassisPaths)
153028f7ebcSEddie James                 {
154028f7ebcSEddie James                     size_t len = std::string::npos;
155f23b7296SEd Tanous                     size_t lastPos = chassis.rfind('/');
156028f7ebcSEddie James                     if (lastPos == std::string::npos)
157028f7ebcSEddie James                     {
158028f7ebcSEddie James                         continue;
159028f7ebcSEddie James                     }
160028f7ebcSEddie James 
161028f7ebcSEddie James                     if (lastPos == chassis.size() - 1)
162028f7ebcSEddie James                     {
163028f7ebcSEddie James                         size_t end = lastPos;
164f23b7296SEd Tanous                         lastPos = chassis.rfind('/', lastPos - 1);
165028f7ebcSEddie James                         if (lastPos == std::string::npos)
166028f7ebcSEddie James                         {
167028f7ebcSEddie James                             continue;
168028f7ebcSEddie James                         }
169028f7ebcSEddie James 
170028f7ebcSEddie James                         len = end - (lastPos + 1);
171028f7ebcSEddie James                     }
172028f7ebcSEddie James 
173028f7ebcSEddie James                     std::string interfaceChassisName =
174028f7ebcSEddie James                         chassis.substr(lastPos + 1, len);
175*e662eae8SEd Tanous                     if (interfaceChassisName.compare(
176*e662eae8SEd Tanous                             sensorAsyncResp->chassisId) == 0)
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,
1937e860f15SJohn Edward Broadbent                         const std::vector<std::pair<
194168e20c1SEd Tanous                             std::string, dbus::utility::DbusVariantType>>&
195028f7ebcSEddie James                             properties) {
196028f7ebcSEddie James                         if (ec)
197028f7ebcSEddie James                         {
1988d1b46d7Szhanghch05                             messages::internalError(
1998d1b46d7Szhanghch05                                 sensorAsyncResp->asyncResp->res);
200028f7ebcSEddie James                             BMCWEB_LOG_ERROR
2017e860f15SJohn Edward Broadbent                                 << "Power Limit GetAll handler: Dbus error "
2027e860f15SJohn Edward Broadbent                                 << ec;
203028f7ebcSEddie James                             return;
204028f7ebcSEddie James                         }
205028f7ebcSEddie James 
2067e860f15SJohn Edward Broadbent                         nlohmann::json& tempArray =
2077e860f15SJohn Edward Broadbent                             sensorAsyncResp->asyncResp->res
2088d1b46d7Szhanghch05                                 .jsonValue["PowerControl"];
209028f7ebcSEddie James 
2107e860f15SJohn Edward Broadbent                         // Put multiple "sensors" into a single PowerControl, 0,
2117e860f15SJohn Edward Broadbent                         // so only create the first one
212028f7ebcSEddie James                         if (tempArray.empty())
213028f7ebcSEddie James                         {
2147ab06f49SGunnar Mills                             // Mandatory properties odata.id and MemberId
2157ab06f49SGunnar Mills                             // A warning without a odata.type
2167ab06f49SGunnar Mills                             tempArray.push_back(
2177ab06f49SGunnar Mills                                 {{"@odata.type", "#Power.v1_0_0.PowerControl"},
2187ab06f49SGunnar Mills                                  {"@odata.id", "/redfish/v1/Chassis/" +
2197ab06f49SGunnar Mills                                                    sensorAsyncResp->chassisId +
2207ab06f49SGunnar Mills                                                    "/Power#/PowerControl/0"},
2217ab06f49SGunnar Mills                                  {"Name", "Chassis Power Control"},
2227ab06f49SGunnar Mills                                  {"MemberId", "0"}});
223028f7ebcSEddie James                         }
224028f7ebcSEddie James 
225028f7ebcSEddie James                         nlohmann::json& sensorJson = tempArray.back();
226028f7ebcSEddie James                         bool enabled = false;
227028f7ebcSEddie James                         double powerCap = 0.0;
228028f7ebcSEddie James                         int64_t scale = 0;
229028f7ebcSEddie James 
230168e20c1SEd Tanous                         for (const std::pair<std::string,
231168e20c1SEd Tanous                                              dbus::utility::DbusVariantType>&
2327e860f15SJohn Edward Broadbent                                  property : properties)
233028f7ebcSEddie James                         {
234*e662eae8SEd Tanous                             if (property.first.compare("Scale") == 0)
235028f7ebcSEddie James                             {
236028f7ebcSEddie James                                 const int64_t* i =
2378d78b7a9SPatrick Williams                                     std::get_if<int64_t>(&property.second);
238028f7ebcSEddie James 
239*e662eae8SEd Tanous                                 if (i != nullptr)
240028f7ebcSEddie James                                 {
241028f7ebcSEddie James                                     scale = *i;
242028f7ebcSEddie James                                 }
243028f7ebcSEddie James                             }
244*e662eae8SEd Tanous                             else if (property.first.compare("PowerCap") == 0)
245028f7ebcSEddie James                             {
246028f7ebcSEddie James                                 const double* d =
2478d78b7a9SPatrick Williams                                     std::get_if<double>(&property.second);
248028f7ebcSEddie James                                 const int64_t* i =
2498d78b7a9SPatrick Williams                                     std::get_if<int64_t>(&property.second);
250028f7ebcSEddie James                                 const uint32_t* u =
2518d78b7a9SPatrick Williams                                     std::get_if<uint32_t>(&property.second);
252028f7ebcSEddie James 
253*e662eae8SEd Tanous                                 if (d != nullptr)
254028f7ebcSEddie James                                 {
255028f7ebcSEddie James                                     powerCap = *d;
256028f7ebcSEddie James                                 }
257*e662eae8SEd Tanous                                 else if (i != nullptr)
258028f7ebcSEddie James                                 {
259271584abSEd Tanous                                     powerCap = static_cast<double>(*i);
260028f7ebcSEddie James                                 }
261*e662eae8SEd Tanous                                 else if (u != nullptr)
262028f7ebcSEddie James                                 {
263028f7ebcSEddie James                                     powerCap = *u;
264028f7ebcSEddie James                                 }
265028f7ebcSEddie James                             }
266*e662eae8SEd Tanous                             else if (property.first.compare("PowerCapEnable") ==
267*e662eae8SEd Tanous                                      0)
268028f7ebcSEddie James                             {
2697e860f15SJohn Edward Broadbent                                 const bool* b =
2707e860f15SJohn Edward Broadbent                                     std::get_if<bool>(&property.second);
271028f7ebcSEddie James 
272*e662eae8SEd Tanous                                 if (b != nullptr)
273028f7ebcSEddie James                                 {
274028f7ebcSEddie James                                     enabled = *b;
275028f7ebcSEddie James                                 }
276028f7ebcSEddie James                             }
277028f7ebcSEddie James                         }
278028f7ebcSEddie James 
2797ab06f49SGunnar Mills                         nlohmann::json& value =
2807ab06f49SGunnar Mills                             sensorJson["PowerLimit"]["LimitInWatts"];
281028f7ebcSEddie James 
2827e860f15SJohn Edward Broadbent                         // LimitException is Mandatory attribute as per OCP
2837e860f15SJohn Edward Broadbent                         // Baseline Profile – v1.0.0, so currently making it
2847e860f15SJohn Edward Broadbent                         // "NoAction" as default value to make it OCP Compliant.
2855a64a6f3SJoshi-Mansi                         sensorJson["PowerLimit"]["LimitException"] = "NoAction";
2865a64a6f3SJoshi-Mansi 
287028f7ebcSEddie James                         if (enabled)
288028f7ebcSEddie James                         {
2897e860f15SJohn Edward Broadbent                             // Redfish specification indicates PowerLimit should
2907e860f15SJohn Edward Broadbent                             // be null if the limit is not enabled.
291028f7ebcSEddie James                             value = powerCap * std::pow(10, scale);
292028f7ebcSEddie James                         }
293028f7ebcSEddie James                     };
294028f7ebcSEddie James 
295028f7ebcSEddie James                 crow::connections::systemBus->async_method_call(
296028f7ebcSEddie James                     std::move(valueHandler), "xyz.openbmc_project.Settings",
297028f7ebcSEddie James                     "/xyz/openbmc_project/control/host0/power_cap",
298028f7ebcSEddie James                     "org.freedesktop.DBus.Properties", "GetAll",
299028f7ebcSEddie James                     "xyz.openbmc_project.Control.Power.Cap");
300028f7ebcSEddie James             };
301028f7ebcSEddie James 
302028f7ebcSEddie James             crow::connections::systemBus->async_method_call(
303168e20c1SEd Tanous                 std::move(chassisHandler), "xyz.openbmc_project.ObjectMapper",
304028f7ebcSEddie James                 "/xyz/openbmc_project/object_mapper",
305028f7ebcSEddie James                 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
306271584abSEd Tanous                 "/xyz/openbmc_project/inventory", 0,
307f857e9aeSAppaRao Puli                 std::array<const char*, 2>{
308f857e9aeSAppaRao Puli                     "xyz.openbmc_project.Inventory.Item.Board",
309028f7ebcSEddie James                     "xyz.openbmc_project.Inventory.Item.Chassis"});
3107e860f15SJohn Edward Broadbent         });
3114bb3dc34SCarol Wang 
3127e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
313ed398213SEd Tanous         .privileges(redfish::privileges::patchPower)
3147e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
3157e860f15SJohn Edward Broadbent             [](const crow::Request& req,
3167e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3177e860f15SJohn Edward Broadbent                const std::string& chassisName) {
3188d1b46d7Szhanghch05                 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
3198d1b46d7Szhanghch05                     asyncResp, chassisName,
3208d1b46d7Szhanghch05                     sensors::dbus::paths.at(sensors::node::power),
321a0ec28b6SAdrian Ambrożewicz                     sensors::node::power);
3224bb3dc34SCarol Wang 
3234bb3dc34SCarol Wang                 std::optional<std::vector<nlohmann::json>> voltageCollections;
3244bb3dc34SCarol Wang                 std::optional<std::vector<nlohmann::json>> powerCtlCollections;
3254bb3dc34SCarol Wang 
3268d1b46d7Szhanghch05                 if (!json_util::readJson(req, sensorAsyncResp->asyncResp->res,
3278d1b46d7Szhanghch05                                          "PowerControl", powerCtlCollections,
3288d1b46d7Szhanghch05                                          "Voltages", voltageCollections))
3294bb3dc34SCarol Wang                 {
3304bb3dc34SCarol Wang                     return;
3314bb3dc34SCarol Wang                 }
3324bb3dc34SCarol Wang 
3334bb3dc34SCarol Wang                 if (powerCtlCollections)
3344bb3dc34SCarol Wang                 {
3358d1b46d7Szhanghch05                     setPowerCapOverride(sensorAsyncResp, *powerCtlCollections);
3364bb3dc34SCarol Wang                 }
3374bb3dc34SCarol Wang                 if (voltageCollections)
3384bb3dc34SCarol Wang                 {
3394bb3dc34SCarol Wang                     std::unordered_map<std::string, std::vector<nlohmann::json>>
3404bb3dc34SCarol Wang                         allCollections;
3417e860f15SJohn Edward Broadbent                     allCollections.emplace("Voltages",
3427e860f15SJohn Edward Broadbent                                            *std::move(voltageCollections));
34380ac4024SBruce Lee                     setSensorsOverride(sensorAsyncResp, allCollections);
3444bb3dc34SCarol Wang                 }
3457e860f15SJohn Edward Broadbent             });
346413961deSRichard Marian Thomaiyar }
3472474adfaSEd Tanous 
3482474adfaSEd Tanous } // namespace redfish
349