xref: /openbmc/bmcweb/features/redfish/lib/power.hpp (revision 4f48d5f67f293e50340e7f4bf866435e03a6fc62)
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>
22ed398213SEd Tanous #include <registries/privilege_registry.hpp>
237e860f15SJohn Edward Broadbent 
242474adfaSEd Tanous namespace redfish
252474adfaSEd Tanous {
26*4f48d5f6SEd Tanous inline void setPowerCapOverride(
278d1b46d7Szhanghch05     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
284bb3dc34SCarol Wang     std::vector<nlohmann::json>& powerControlCollections)
294bb3dc34SCarol Wang {
307e860f15SJohn Edward Broadbent     auto getChassisPath =
317e860f15SJohn Edward Broadbent         [sensorsAsyncResp, powerControlCollections](
327e860f15SJohn Edward Broadbent             const std::optional<std::string>& chassisPath) mutable {
334bb3dc34SCarol Wang             if (!chassisPath)
344bb3dc34SCarol Wang             {
354bb3dc34SCarol Wang                 BMCWEB_LOG_ERROR << "Don't find valid chassis path ";
368d1b46d7Szhanghch05                 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
378d1b46d7Szhanghch05                                            "Chassis",
388d1b46d7Szhanghch05                                            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;
638d1b46d7Szhanghch05             if (!json_util::readJson(*powerLimit,
648d1b46d7Szhanghch05                                      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             }
738d1b46d7Szhanghch05             auto valueHandler = [value, sensorsAsyncResp](
744bb3dc34SCarol Wang                                     const boost::system::error_code ec,
754bb3dc34SCarol Wang                                     const SensorVariant& powerCapEnable) {
764bb3dc34SCarol Wang                 if (ec)
774bb3dc34SCarol Wang                 {
788d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
794bb3dc34SCarol Wang                     BMCWEB_LOG_ERROR
804bb3dc34SCarol Wang                         << "powerCapEnable Get handler: Dbus error " << ec;
814bb3dc34SCarol Wang                     return;
824bb3dc34SCarol Wang                 }
834bb3dc34SCarol Wang                 // Check PowerCapEnable
848d78b7a9SPatrick Williams                 const bool* b = std::get_if<bool>(&powerCapEnable);
854bb3dc34SCarol Wang                 if (b == nullptr)
864bb3dc34SCarol Wang                 {
878d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
888d1b46d7Szhanghch05                     BMCWEB_LOG_ERROR << "Fail to get PowerCapEnable status ";
894bb3dc34SCarol Wang                     return;
904bb3dc34SCarol Wang                 }
914bb3dc34SCarol Wang                 if (!(*b))
924bb3dc34SCarol Wang                 {
934bb3dc34SCarol Wang                     messages::actionNotSupported(
948d1b46d7Szhanghch05                         sensorsAsyncResp->asyncResp->res,
954bb3dc34SCarol Wang                         "Setting LimitInWatts when PowerLimit "
964bb3dc34SCarol Wang                         "feature is disabled");
974bb3dc34SCarol Wang                     BMCWEB_LOG_ERROR << "PowerLimit feature is disabled ";
984bb3dc34SCarol Wang                     return;
994bb3dc34SCarol Wang                 }
1004bb3dc34SCarol Wang 
1014bb3dc34SCarol Wang                 crow::connections::systemBus->async_method_call(
1028d1b46d7Szhanghch05                     [sensorsAsyncResp](const boost::system::error_code ec2) {
10323a21a1cSEd Tanous                         if (ec2)
1044bb3dc34SCarol Wang                         {
1058d1b46d7Szhanghch05                             BMCWEB_LOG_DEBUG << "Power Limit Set: Dbus error: "
1068d1b46d7Szhanghch05                                              << ec2;
1078d1b46d7Szhanghch05                             messages::internalError(
1088d1b46d7Szhanghch05                                 sensorsAsyncResp->asyncResp->res);
1094bb3dc34SCarol Wang                             return;
1104bb3dc34SCarol Wang                         }
1118d1b46d7Szhanghch05                         sensorsAsyncResp->asyncResp->res.result(
1124bb3dc34SCarol Wang                             boost::beast::http::status::no_content);
1134bb3dc34SCarol Wang                     },
1144bb3dc34SCarol Wang                     "xyz.openbmc_project.Settings",
1154bb3dc34SCarol Wang                     "/xyz/openbmc_project/control/host0/power_cap",
1164bb3dc34SCarol Wang                     "org.freedesktop.DBus.Properties", "Set",
1174bb3dc34SCarol Wang                     "xyz.openbmc_project.Control.Power.Cap", "PowerCap",
11819bd78d9SPatrick Williams                     std::variant<uint32_t>(*value));
1194bb3dc34SCarol Wang             };
1204bb3dc34SCarol Wang             crow::connections::systemBus->async_method_call(
1214bb3dc34SCarol Wang                 std::move(valueHandler), "xyz.openbmc_project.Settings",
1224bb3dc34SCarol Wang                 "/xyz/openbmc_project/control/host0/power_cap",
1234bb3dc34SCarol Wang                 "org.freedesktop.DBus.Properties", "Get",
1244bb3dc34SCarol Wang                 "xyz.openbmc_project.Control.Power.Cap", "PowerCapEnable");
1254bb3dc34SCarol Wang         };
1268d1b46d7Szhanghch05     getValidChassisPath(sensorsAsyncResp, std::move(getChassisPath));
1274bb3dc34SCarol Wang }
1287e860f15SJohn Edward Broadbent inline void requestRoutesPower(App& app)
1292474adfaSEd Tanous {
1302474adfaSEd Tanous 
1317e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
132ed398213SEd Tanous         .privileges(redfish::privileges::getPower)
1337e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
1347e860f15SJohn Edward Broadbent             [](const crow::Request&,
1357e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1367e860f15SJohn Edward Broadbent                const std::string& chassisName) {
1377e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["PowerControl"] =
1387e860f15SJohn Edward Broadbent                     nlohmann::json::array();
139c5d03ff4SJennifer Lee 
1402474adfaSEd Tanous                 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
1418d1b46d7Szhanghch05                     asyncResp, chassisName,
1428d1b46d7Szhanghch05                     sensors::dbus::paths.at(sensors::node::power),
143a0ec28b6SAdrian Ambrożewicz                     sensors::node::power);
144028f7ebcSEddie James 
1452474adfaSEd Tanous                 getChassisData(sensorAsyncResp);
146028f7ebcSEddie James 
1477e860f15SJohn Edward Broadbent                 // This callback verifies that the power limit is only provided
1487e860f15SJohn Edward Broadbent                 // for the chassis that implements the Chassis inventory item.
1497e860f15SJohn Edward Broadbent                 // This prevents things like power supplies providing the
1507e860f15SJohn Edward Broadbent                 // chassis power limit
151028f7ebcSEddie James                 auto chassisHandler = [sensorAsyncResp](
152271584abSEd Tanous                                           const boost::system::error_code e,
153028f7ebcSEddie James                                           const std::vector<std::string>&
154028f7ebcSEddie James                                               chassisPaths) {
155271584abSEd Tanous                     if (e)
156028f7ebcSEddie James                     {
157028f7ebcSEddie James                         BMCWEB_LOG_ERROR
1587e860f15SJohn Edward Broadbent                             << "Power Limit GetSubTreePaths handler Dbus error "
1597e860f15SJohn Edward Broadbent                             << e;
160028f7ebcSEddie James                         return;
161028f7ebcSEddie James                     }
162028f7ebcSEddie James 
163028f7ebcSEddie James                     bool found = false;
164028f7ebcSEddie James                     for (const std::string& chassis : chassisPaths)
165028f7ebcSEddie James                     {
166028f7ebcSEddie James                         size_t len = std::string::npos;
167f23b7296SEd Tanous                         size_t lastPos = chassis.rfind('/');
168028f7ebcSEddie James                         if (lastPos == std::string::npos)
169028f7ebcSEddie James                         {
170028f7ebcSEddie James                             continue;
171028f7ebcSEddie James                         }
172028f7ebcSEddie James 
173028f7ebcSEddie James                         if (lastPos == chassis.size() - 1)
174028f7ebcSEddie James                         {
175028f7ebcSEddie James                             size_t end = lastPos;
176f23b7296SEd Tanous                             lastPos = chassis.rfind('/', lastPos - 1);
177028f7ebcSEddie James                             if (lastPos == std::string::npos)
178028f7ebcSEddie James                             {
179028f7ebcSEddie James                                 continue;
180028f7ebcSEddie James                             }
181028f7ebcSEddie James 
182028f7ebcSEddie James                             len = end - (lastPos + 1);
183028f7ebcSEddie James                         }
184028f7ebcSEddie James 
185028f7ebcSEddie James                         std::string interfaceChassisName =
186028f7ebcSEddie James                             chassis.substr(lastPos + 1, len);
1877e860f15SJohn Edward Broadbent                         if (!interfaceChassisName.compare(
1887e860f15SJohn Edward Broadbent                                 sensorAsyncResp->chassisId))
189028f7ebcSEddie James                         {
190028f7ebcSEddie James                             found = true;
191028f7ebcSEddie James                             break;
192028f7ebcSEddie James                         }
193028f7ebcSEddie James                     }
194028f7ebcSEddie James 
195028f7ebcSEddie James                     if (!found)
196028f7ebcSEddie James                     {
197028f7ebcSEddie James                         BMCWEB_LOG_DEBUG << "Power Limit not present for "
198028f7ebcSEddie James                                          << sensorAsyncResp->chassisId;
199028f7ebcSEddie James                         return;
200028f7ebcSEddie James                     }
201028f7ebcSEddie James 
2027e860f15SJohn Edward Broadbent                     auto valueHandler = [sensorAsyncResp](
203028f7ebcSEddie James                                             const boost::system::error_code ec,
2047e860f15SJohn Edward Broadbent                                             const std::vector<std::pair<
2057e860f15SJohn Edward Broadbent                                                 std::string, SensorVariant>>&
206028f7ebcSEddie James                                                 properties) {
207028f7ebcSEddie James                         if (ec)
208028f7ebcSEddie James                         {
2098d1b46d7Szhanghch05                             messages::internalError(
2108d1b46d7Szhanghch05                                 sensorAsyncResp->asyncResp->res);
211028f7ebcSEddie James                             BMCWEB_LOG_ERROR
2127e860f15SJohn Edward Broadbent                                 << "Power Limit GetAll handler: Dbus error "
2137e860f15SJohn Edward Broadbent                                 << ec;
214028f7ebcSEddie James                             return;
215028f7ebcSEddie James                         }
216028f7ebcSEddie James 
2177e860f15SJohn Edward Broadbent                         nlohmann::json& tempArray =
2187e860f15SJohn Edward Broadbent                             sensorAsyncResp->asyncResp->res
2198d1b46d7Szhanghch05                                 .jsonValue["PowerControl"];
220028f7ebcSEddie James 
2217e860f15SJohn Edward Broadbent                         // Put multiple "sensors" into a single PowerControl, 0,
2227e860f15SJohn Edward Broadbent                         // so only create the first one
223028f7ebcSEddie James                         if (tempArray.empty())
224028f7ebcSEddie James                         {
2257ab06f49SGunnar Mills                             // Mandatory properties odata.id and MemberId
2267ab06f49SGunnar Mills                             // A warning without a odata.type
2277ab06f49SGunnar Mills                             tempArray.push_back(
2287ab06f49SGunnar Mills                                 {{"@odata.type", "#Power.v1_0_0.PowerControl"},
2297ab06f49SGunnar Mills                                  {"@odata.id", "/redfish/v1/Chassis/" +
2307ab06f49SGunnar Mills                                                    sensorAsyncResp->chassisId +
2317ab06f49SGunnar Mills                                                    "/Power#/PowerControl/0"},
2327ab06f49SGunnar Mills                                  {"Name", "Chassis Power Control"},
2337ab06f49SGunnar Mills                                  {"MemberId", "0"}});
234028f7ebcSEddie James                         }
235028f7ebcSEddie James 
236028f7ebcSEddie James                         nlohmann::json& sensorJson = tempArray.back();
237028f7ebcSEddie James                         bool enabled = false;
238028f7ebcSEddie James                         double powerCap = 0.0;
239028f7ebcSEddie James                         int64_t scale = 0;
240028f7ebcSEddie James 
2417e860f15SJohn Edward Broadbent                         for (const std::pair<std::string, SensorVariant>&
2427e860f15SJohn Edward Broadbent                                  property : properties)
243028f7ebcSEddie James                         {
244028f7ebcSEddie James                             if (!property.first.compare("Scale"))
245028f7ebcSEddie James                             {
246028f7ebcSEddie James                                 const int64_t* i =
2478d78b7a9SPatrick Williams                                     std::get_if<int64_t>(&property.second);
248028f7ebcSEddie James 
249028f7ebcSEddie James                                 if (i)
250028f7ebcSEddie James                                 {
251028f7ebcSEddie James                                     scale = *i;
252028f7ebcSEddie James                                 }
253028f7ebcSEddie James                             }
254028f7ebcSEddie James                             else if (!property.first.compare("PowerCap"))
255028f7ebcSEddie James                             {
256028f7ebcSEddie James                                 const double* d =
2578d78b7a9SPatrick Williams                                     std::get_if<double>(&property.second);
258028f7ebcSEddie James                                 const int64_t* i =
2598d78b7a9SPatrick Williams                                     std::get_if<int64_t>(&property.second);
260028f7ebcSEddie James                                 const uint32_t* u =
2618d78b7a9SPatrick Williams                                     std::get_if<uint32_t>(&property.second);
262028f7ebcSEddie James 
263028f7ebcSEddie James                                 if (d)
264028f7ebcSEddie James                                 {
265028f7ebcSEddie James                                     powerCap = *d;
266028f7ebcSEddie James                                 }
267028f7ebcSEddie James                                 else if (i)
268028f7ebcSEddie James                                 {
269271584abSEd Tanous                                     powerCap = static_cast<double>(*i);
270028f7ebcSEddie James                                 }
271028f7ebcSEddie James                                 else if (u)
272028f7ebcSEddie James                                 {
273028f7ebcSEddie James                                     powerCap = *u;
274028f7ebcSEddie James                                 }
275028f7ebcSEddie James                             }
276028f7ebcSEddie James                             else if (!property.first.compare("PowerCapEnable"))
277028f7ebcSEddie James                             {
2787e860f15SJohn Edward Broadbent                                 const bool* b =
2797e860f15SJohn Edward Broadbent                                     std::get_if<bool>(&property.second);
280028f7ebcSEddie James 
281028f7ebcSEddie James                                 if (b)
282028f7ebcSEddie James                                 {
283028f7ebcSEddie James                                     enabled = *b;
284028f7ebcSEddie James                                 }
285028f7ebcSEddie James                             }
286028f7ebcSEddie James                         }
287028f7ebcSEddie James 
2887ab06f49SGunnar Mills                         nlohmann::json& value =
2897ab06f49SGunnar Mills                             sensorJson["PowerLimit"]["LimitInWatts"];
290028f7ebcSEddie James 
2917e860f15SJohn Edward Broadbent                         // LimitException is Mandatory attribute as per OCP
2927e860f15SJohn Edward Broadbent                         // Baseline Profile – v1.0.0, so currently making it
2937e860f15SJohn Edward Broadbent                         // "NoAction" as default value to make it OCP Compliant.
2945a64a6f3SJoshi-Mansi                         sensorJson["PowerLimit"]["LimitException"] = "NoAction";
2955a64a6f3SJoshi-Mansi 
296028f7ebcSEddie James                         if (enabled)
297028f7ebcSEddie James                         {
2987e860f15SJohn Edward Broadbent                             // Redfish specification indicates PowerLimit should
2997e860f15SJohn Edward Broadbent                             // be null if the limit is not enabled.
300028f7ebcSEddie James                             value = powerCap * std::pow(10, scale);
301028f7ebcSEddie James                         }
302028f7ebcSEddie James                     };
303028f7ebcSEddie James 
304028f7ebcSEddie James                     crow::connections::systemBus->async_method_call(
305028f7ebcSEddie James                         std::move(valueHandler), "xyz.openbmc_project.Settings",
306028f7ebcSEddie James                         "/xyz/openbmc_project/control/host0/power_cap",
307028f7ebcSEddie James                         "org.freedesktop.DBus.Properties", "GetAll",
308028f7ebcSEddie James                         "xyz.openbmc_project.Control.Power.Cap");
309028f7ebcSEddie James                 };
310028f7ebcSEddie James 
311028f7ebcSEddie James                 crow::connections::systemBus->async_method_call(
3127e860f15SJohn Edward Broadbent                     std::move(chassisHandler),
3137e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.ObjectMapper",
314028f7ebcSEddie James                     "/xyz/openbmc_project/object_mapper",
315028f7ebcSEddie James                     "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
316271584abSEd Tanous                     "/xyz/openbmc_project/inventory", 0,
317f857e9aeSAppaRao Puli                     std::array<const char*, 2>{
318f857e9aeSAppaRao Puli                         "xyz.openbmc_project.Inventory.Item.Board",
319028f7ebcSEddie James                         "xyz.openbmc_project.Inventory.Item.Chassis"});
3207e860f15SJohn Edward Broadbent             });
3214bb3dc34SCarol Wang 
3227e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
323ed398213SEd Tanous         .privileges(redfish::privileges::patchPower)
3247e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
3257e860f15SJohn Edward Broadbent             [](const crow::Request& req,
3267e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3277e860f15SJohn Edward Broadbent                const std::string& chassisName) {
3288d1b46d7Szhanghch05                 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
3298d1b46d7Szhanghch05                     asyncResp, chassisName,
3308d1b46d7Szhanghch05                     sensors::dbus::paths.at(sensors::node::power),
331a0ec28b6SAdrian Ambrożewicz                     sensors::node::power);
3324bb3dc34SCarol Wang 
3334bb3dc34SCarol Wang                 std::optional<std::vector<nlohmann::json>> voltageCollections;
3344bb3dc34SCarol Wang                 std::optional<std::vector<nlohmann::json>> powerCtlCollections;
3354bb3dc34SCarol Wang 
3368d1b46d7Szhanghch05                 if (!json_util::readJson(req, sensorAsyncResp->asyncResp->res,
3378d1b46d7Szhanghch05                                          "PowerControl", powerCtlCollections,
3388d1b46d7Szhanghch05                                          "Voltages", voltageCollections))
3394bb3dc34SCarol Wang                 {
3404bb3dc34SCarol Wang                     return;
3414bb3dc34SCarol Wang                 }
3424bb3dc34SCarol Wang 
3434bb3dc34SCarol Wang                 if (powerCtlCollections)
3444bb3dc34SCarol Wang                 {
3458d1b46d7Szhanghch05                     setPowerCapOverride(sensorAsyncResp, *powerCtlCollections);
3464bb3dc34SCarol Wang                 }
3474bb3dc34SCarol Wang                 if (voltageCollections)
3484bb3dc34SCarol Wang                 {
3494bb3dc34SCarol Wang                     std::unordered_map<std::string, std::vector<nlohmann::json>>
3504bb3dc34SCarol Wang                         allCollections;
3517e860f15SJohn Edward Broadbent                     allCollections.emplace("Voltages",
3527e860f15SJohn Edward Broadbent                                            *std::move(voltageCollections));
35380ac4024SBruce Lee                     setSensorsOverride(sensorAsyncResp, allCollections);
3544bb3dc34SCarol Wang                 }
3557e860f15SJohn Edward Broadbent             });
356413961deSRichard Marian Thomaiyar }
3572474adfaSEd Tanous 
3582474adfaSEd Tanous } // namespace redfish
359