xref: /openbmc/bmcweb/features/redfish/lib/power.hpp (revision d1bde9e590f165f28d948fda93e48d51b30bb463)
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"
200d7702c0SZhenwei Chen #include "utils/chassis_utils.hpp"
212474adfaSEd Tanous 
227e860f15SJohn Edward Broadbent #include <app.hpp>
23168e20c1SEd Tanous #include <dbus_utility.hpp>
2445ca1b86SEd Tanous #include <query.hpp>
25ed398213SEd Tanous #include <registries/privilege_registry.hpp>
26*d1bde9e5SKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
277e860f15SJohn Edward Broadbent 
282474adfaSEd Tanous namespace redfish
292474adfaSEd Tanous {
304f48d5f6SEd Tanous inline void setPowerCapOverride(
318d1b46d7Szhanghch05     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
324bb3dc34SCarol Wang     std::vector<nlohmann::json>& powerControlCollections)
334bb3dc34SCarol Wang {
34002d39b4SEd Tanous     auto getChassisPath =
35002d39b4SEd Tanous         [sensorsAsyncResp, powerControlCollections](
36002d39b4SEd Tanous             const std::optional<std::string>& chassisPath) mutable {
374bb3dc34SCarol Wang         if (!chassisPath)
384bb3dc34SCarol Wang         {
394bb3dc34SCarol Wang             BMCWEB_LOG_ERROR << "Don't find valid chassis path ";
408d1b46d7Szhanghch05             messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
410fda0f12SGeorge Liu                                        "Chassis", sensorsAsyncResp->chassisId);
424bb3dc34SCarol Wang             return;
434bb3dc34SCarol Wang         }
444bb3dc34SCarol Wang 
454bb3dc34SCarol Wang         if (powerControlCollections.size() != 1)
464bb3dc34SCarol Wang         {
478d1b46d7Szhanghch05             BMCWEB_LOG_ERROR << "Don't support multiple hosts at present ";
488d1b46d7Szhanghch05             messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
498d1b46d7Szhanghch05                                        "Power", "PowerControl");
504bb3dc34SCarol Wang             return;
514bb3dc34SCarol Wang         }
524bb3dc34SCarol Wang 
534bb3dc34SCarol Wang         auto& item = powerControlCollections[0];
544bb3dc34SCarol Wang 
554bb3dc34SCarol Wang         std::optional<nlohmann::json> powerLimit;
568d1b46d7Szhanghch05         if (!json_util::readJson(item, sensorsAsyncResp->asyncResp->res,
578d1b46d7Szhanghch05                                  "PowerLimit", powerLimit))
584bb3dc34SCarol Wang         {
594bb3dc34SCarol Wang             return;
604bb3dc34SCarol Wang         }
614bb3dc34SCarol Wang         if (!powerLimit)
624bb3dc34SCarol Wang         {
634bb3dc34SCarol Wang             return;
644bb3dc34SCarol Wang         }
654bb3dc34SCarol Wang         std::optional<uint32_t> value;
660fda0f12SGeorge Liu         if (!json_util::readJson(*powerLimit, sensorsAsyncResp->asyncResp->res,
674bb3dc34SCarol Wang                                  "LimitInWatts", value))
684bb3dc34SCarol Wang         {
694bb3dc34SCarol Wang             return;
704bb3dc34SCarol Wang         }
714bb3dc34SCarol Wang         if (!value)
724bb3dc34SCarol Wang         {
734bb3dc34SCarol Wang             return;
744bb3dc34SCarol Wang         }
751e1e598dSJonathan Doman         sdbusplus::asio::getProperty<bool>(
761e1e598dSJonathan Doman             *crow::connections::systemBus, "xyz.openbmc_project.Settings",
771e1e598dSJonathan Doman             "/xyz/openbmc_project/control/host0/power_cap",
781e1e598dSJonathan Doman             "xyz.openbmc_project.Control.Power.Cap", "PowerCapEnable",
791e1e598dSJonathan Doman             [value, sensorsAsyncResp](const boost::system::error_code ec,
801e1e598dSJonathan Doman                                       bool powerCapEnable) {
814bb3dc34SCarol Wang             if (ec)
824bb3dc34SCarol Wang             {
838d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
84002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "powerCapEnable Get handler: Dbus error "
85002d39b4SEd Tanous                                  << ec;
864bb3dc34SCarol Wang                 return;
874bb3dc34SCarol Wang             }
881e1e598dSJonathan Doman             if (!powerCapEnable)
894bb3dc34SCarol Wang             {
904bb3dc34SCarol Wang                 messages::actionNotSupported(
918d1b46d7Szhanghch05                     sensorsAsyncResp->asyncResp->res,
920fda0f12SGeorge Liu                     "Setting LimitInWatts when PowerLimit feature is disabled");
934bb3dc34SCarol Wang                 BMCWEB_LOG_ERROR << "PowerLimit feature is disabled ";
944bb3dc34SCarol Wang                 return;
954bb3dc34SCarol Wang             }
964bb3dc34SCarol Wang 
974bb3dc34SCarol Wang             crow::connections::systemBus->async_method_call(
988d1b46d7Szhanghch05                 [sensorsAsyncResp](const boost::system::error_code ec2) {
9923a21a1cSEd Tanous                 if (ec2)
1004bb3dc34SCarol Wang                 {
101002d39b4SEd Tanous                     BMCWEB_LOG_DEBUG << "Power Limit Set: Dbus error: " << ec2;
102002d39b4SEd Tanous                     messages::internalError(sensorsAsyncResp->asyncResp->res);
1034bb3dc34SCarol Wang                     return;
1044bb3dc34SCarol Wang                 }
1058d1b46d7Szhanghch05                 sensorsAsyncResp->asyncResp->res.result(
1064bb3dc34SCarol Wang                     boost::beast::http::status::no_content);
1074bb3dc34SCarol Wang                 },
1084bb3dc34SCarol Wang                 "xyz.openbmc_project.Settings",
1094bb3dc34SCarol Wang                 "/xyz/openbmc_project/control/host0/power_cap",
1104bb3dc34SCarol Wang                 "org.freedesktop.DBus.Properties", "Set",
1114bb3dc34SCarol Wang                 "xyz.openbmc_project.Control.Power.Cap", "PowerCap",
1121e1e598dSJonathan Doman                 std::variant<uint32_t>(*value));
1131e1e598dSJonathan Doman             });
1144bb3dc34SCarol Wang     };
1150d7702c0SZhenwei Chen     redfish::chassis_utils::getValidChassisPath(sensorsAsyncResp->asyncResp,
1160d7702c0SZhenwei Chen                                                 sensorsAsyncResp->chassisId,
1170d7702c0SZhenwei Chen                                                 std::move(getChassisPath));
1184bb3dc34SCarol Wang }
1197e860f15SJohn Edward Broadbent inline void requestRoutesPower(App& app)
1202474adfaSEd Tanous {
1212474adfaSEd Tanous 
1227e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
123ed398213SEd Tanous         .privileges(redfish::privileges::getPower)
124002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
125002d39b4SEd Tanous             [&app](const crow::Request& req,
12645ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1277e860f15SJohn Edward Broadbent                    const std::string& chassisName) {
1283ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
12945ca1b86SEd Tanous         {
13045ca1b86SEd Tanous             return;
13145ca1b86SEd Tanous         }
132168e20c1SEd Tanous         asyncResp->res.jsonValue["PowerControl"] = nlohmann::json::array();
133c5d03ff4SJennifer Lee 
1342474adfaSEd Tanous         auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
13502da7c5aSEd Tanous             asyncResp, chassisName, sensors::dbus::powerPaths,
136a0ec28b6SAdrian Ambrożewicz             sensors::node::power);
137028f7ebcSEddie James 
1382474adfaSEd Tanous         getChassisData(sensorAsyncResp);
139028f7ebcSEddie James 
1407e860f15SJohn Edward Broadbent         // This callback verifies that the power limit is only provided
1417e860f15SJohn Edward Broadbent         // for the chassis that implements the Chassis inventory item.
1427e860f15SJohn Edward Broadbent         // This prevents things like power supplies providing the
1437e860f15SJohn Edward Broadbent         // chassis power limit
144b9d36b47SEd Tanous 
145b9d36b47SEd Tanous         using Mapper = dbus::utility::MapperGetSubTreePathsResponse;
146002d39b4SEd Tanous         auto chassisHandler =
147002d39b4SEd Tanous             [sensorAsyncResp](const boost::system::error_code e,
148b9d36b47SEd Tanous                               const Mapper& chassisPaths) {
149271584abSEd Tanous             if (e)
150028f7ebcSEddie James             {
151028f7ebcSEddie James                 BMCWEB_LOG_ERROR
152002d39b4SEd Tanous                     << "Power Limit GetSubTreePaths handler Dbus error " << e;
153028f7ebcSEddie James                 return;
154028f7ebcSEddie James             }
155028f7ebcSEddie James 
156028f7ebcSEddie James             bool found = false;
157028f7ebcSEddie James             for (const std::string& chassis : chassisPaths)
158028f7ebcSEddie James             {
159028f7ebcSEddie James                 size_t len = std::string::npos;
160f23b7296SEd Tanous                 size_t lastPos = chassis.rfind('/');
161028f7ebcSEddie James                 if (lastPos == std::string::npos)
162028f7ebcSEddie James                 {
163028f7ebcSEddie James                     continue;
164028f7ebcSEddie James                 }
165028f7ebcSEddie James 
166028f7ebcSEddie James                 if (lastPos == chassis.size() - 1)
167028f7ebcSEddie James                 {
168028f7ebcSEddie James                     size_t end = lastPos;
169f23b7296SEd Tanous                     lastPos = chassis.rfind('/', lastPos - 1);
170028f7ebcSEddie James                     if (lastPos == std::string::npos)
171028f7ebcSEddie James                     {
172028f7ebcSEddie James                         continue;
173028f7ebcSEddie James                     }
174028f7ebcSEddie James 
175028f7ebcSEddie James                     len = end - (lastPos + 1);
176028f7ebcSEddie James                 }
177028f7ebcSEddie James 
178028f7ebcSEddie James                 std::string interfaceChassisName =
179028f7ebcSEddie James                     chassis.substr(lastPos + 1, len);
18055f79e6fSEd Tanous                 if (interfaceChassisName == sensorAsyncResp->chassisId)
181028f7ebcSEddie James                 {
182028f7ebcSEddie James                     found = true;
183028f7ebcSEddie James                     break;
184028f7ebcSEddie James                 }
185028f7ebcSEddie James             }
186028f7ebcSEddie James 
187028f7ebcSEddie James             if (!found)
188028f7ebcSEddie James             {
189028f7ebcSEddie James                 BMCWEB_LOG_DEBUG << "Power Limit not present for "
190028f7ebcSEddie James                                  << sensorAsyncResp->chassisId;
191028f7ebcSEddie James                 return;
192028f7ebcSEddie James             }
193028f7ebcSEddie James 
194168e20c1SEd Tanous             auto valueHandler =
195168e20c1SEd Tanous                 [sensorAsyncResp](
196028f7ebcSEddie James                     const boost::system::error_code ec,
197b9d36b47SEd Tanous                     const dbus::utility::DBusPropertiesMap& properties) {
198028f7ebcSEddie James                 if (ec)
199028f7ebcSEddie James                 {
200002d39b4SEd Tanous                     messages::internalError(sensorAsyncResp->asyncResp->res);
201028f7ebcSEddie James                     BMCWEB_LOG_ERROR
202002d39b4SEd Tanous                         << "Power Limit GetAll handler: Dbus error " << ec;
203028f7ebcSEddie James                     return;
204028f7ebcSEddie James                 }
205028f7ebcSEddie James 
2067e860f15SJohn Edward Broadbent                 nlohmann::json& tempArray =
207002d39b4SEd Tanous                     sensorAsyncResp->asyncResp->res.jsonValue["PowerControl"];
208028f7ebcSEddie James 
2097e860f15SJohn Edward Broadbent                 // Put multiple "sensors" into a single PowerControl, 0,
2107e860f15SJohn Edward Broadbent                 // so only create the first one
211028f7ebcSEddie James                 if (tempArray.empty())
212028f7ebcSEddie James                 {
2137ab06f49SGunnar Mills                     // Mandatory properties odata.id and MemberId
2147ab06f49SGunnar Mills                     // A warning without a odata.type
2151476687dSEd Tanous                     nlohmann::json::object_t powerControl;
216002d39b4SEd Tanous                     powerControl["@odata.type"] = "#Power.v1_0_0.PowerControl";
217002d39b4SEd Tanous                     powerControl["@odata.id"] = "/redfish/v1/Chassis/" +
2187ab06f49SGunnar Mills                                                 sensorAsyncResp->chassisId +
2191476687dSEd Tanous                                                 "/Power#/PowerControl/0";
2201476687dSEd Tanous                     powerControl["Name"] = "Chassis Power Control";
2211476687dSEd Tanous                     powerControl["MemberId"] = "0";
2221476687dSEd Tanous                     tempArray.push_back(std::move(powerControl));
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,
231002d39b4SEd Tanous                                      dbus::utility::DbusVariantType>& property :
232002d39b4SEd Tanous                      properties)
233028f7ebcSEddie James                 {
23455f79e6fSEd Tanous                     if (property.first == "Scale")
235028f7ebcSEddie James                     {
236028f7ebcSEddie James                         const int64_t* i =
2378d78b7a9SPatrick Williams                             std::get_if<int64_t>(&property.second);
238028f7ebcSEddie James 
239e662eae8SEd Tanous                         if (i != nullptr)
240028f7ebcSEddie James                         {
241028f7ebcSEddie James                             scale = *i;
242028f7ebcSEddie James                         }
243028f7ebcSEddie James                     }
24455f79e6fSEd Tanous                     else if (property.first == "PowerCap")
245028f7ebcSEddie James                     {
246002d39b4SEd Tanous                         const double* d = std::get_if<double>(&property.second);
247028f7ebcSEddie James                         const int64_t* i =
2488d78b7a9SPatrick Williams                             std::get_if<int64_t>(&property.second);
249028f7ebcSEddie James                         const uint32_t* u =
2508d78b7a9SPatrick Williams                             std::get_if<uint32_t>(&property.second);
251028f7ebcSEddie James 
252e662eae8SEd Tanous                         if (d != nullptr)
253028f7ebcSEddie James                         {
254028f7ebcSEddie James                             powerCap = *d;
255028f7ebcSEddie James                         }
256e662eae8SEd Tanous                         else if (i != nullptr)
257028f7ebcSEddie James                         {
258271584abSEd Tanous                             powerCap = static_cast<double>(*i);
259028f7ebcSEddie James                         }
260e662eae8SEd Tanous                         else if (u != nullptr)
261028f7ebcSEddie James                         {
262028f7ebcSEddie James                             powerCap = *u;
263028f7ebcSEddie James                         }
264028f7ebcSEddie James                     }
26555f79e6fSEd Tanous                     else if (property.first == "PowerCapEnable")
266028f7ebcSEddie James                     {
267002d39b4SEd Tanous                         const bool* b = std::get_if<bool>(&property.second);
268028f7ebcSEddie James 
269e662eae8SEd Tanous                         if (b != nullptr)
270028f7ebcSEddie James                         {
271028f7ebcSEddie James                             enabled = *b;
272028f7ebcSEddie James                         }
273028f7ebcSEddie James                     }
274028f7ebcSEddie James                 }
275028f7ebcSEddie James 
2767ab06f49SGunnar Mills                 nlohmann::json& value =
2777ab06f49SGunnar Mills                     sensorJson["PowerLimit"]["LimitInWatts"];
278028f7ebcSEddie James 
2797e860f15SJohn Edward Broadbent                 // LimitException is Mandatory attribute as per OCP
2807e860f15SJohn Edward Broadbent                 // Baseline Profile – v1.0.0, so currently making it
2817e860f15SJohn Edward Broadbent                 // "NoAction" as default value to make it OCP Compliant.
2825a64a6f3SJoshi-Mansi                 sensorJson["PowerLimit"]["LimitException"] = "NoAction";
2835a64a6f3SJoshi-Mansi 
284028f7ebcSEddie James                 if (enabled)
285028f7ebcSEddie James                 {
2867e860f15SJohn Edward Broadbent                     // Redfish specification indicates PowerLimit should
2877e860f15SJohn Edward Broadbent                     // be null if the limit is not enabled.
288028f7ebcSEddie James                     value = powerCap * std::pow(10, scale);
289028f7ebcSEddie James                 }
290028f7ebcSEddie James             };
291028f7ebcSEddie James 
292*d1bde9e5SKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
293*d1bde9e5SKrzysztof Grobelny                 *crow::connections::systemBus, "xyz.openbmc_project.Settings",
294028f7ebcSEddie James                 "/xyz/openbmc_project/control/host0/power_cap",
295*d1bde9e5SKrzysztof Grobelny                 "xyz.openbmc_project.Control.Power.Cap",
296*d1bde9e5SKrzysztof Grobelny                 std::move(valueHandler));
297028f7ebcSEddie James         };
298028f7ebcSEddie James 
299028f7ebcSEddie James         crow::connections::systemBus->async_method_call(
300168e20c1SEd Tanous             std::move(chassisHandler), "xyz.openbmc_project.ObjectMapper",
301028f7ebcSEddie James             "/xyz/openbmc_project/object_mapper",
302028f7ebcSEddie James             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
303271584abSEd Tanous             "/xyz/openbmc_project/inventory", 0,
304f857e9aeSAppaRao Puli             std::array<const char*, 2>{
305f857e9aeSAppaRao Puli                 "xyz.openbmc_project.Inventory.Item.Board",
306028f7ebcSEddie James                 "xyz.openbmc_project.Inventory.Item.Chassis"});
3077e860f15SJohn Edward Broadbent         });
3084bb3dc34SCarol Wang 
3097e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
310ed398213SEd Tanous         .privileges(redfish::privileges::patchPower)
3117e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
31245ca1b86SEd Tanous             [&app](const crow::Request& req,
3137e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3147e860f15SJohn Edward Broadbent                    const std::string& chassisName) {
3153ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
31645ca1b86SEd Tanous         {
31745ca1b86SEd Tanous             return;
31845ca1b86SEd Tanous         }
3198d1b46d7Szhanghch05         auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
32002da7c5aSEd Tanous             asyncResp, chassisName, sensors::dbus::powerPaths,
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 
326002d39b4SEd Tanous         if (!json_util::readJsonPatch(req, sensorAsyncResp->asyncResp->res,
327002d39b4SEd Tanous                                       "PowerControl", powerCtlCollections,
328002d39b4SEd Tanous                                       "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;
341002d39b4SEd Tanous             allCollections.emplace("Voltages", *std::move(voltageCollections));
34280ac4024SBruce Lee             setSensorsOverride(sensorAsyncResp, allCollections);
3434bb3dc34SCarol Wang         }
3447e860f15SJohn Edward Broadbent         });
345413961deSRichard Marian Thomaiyar }
3462474adfaSEd Tanous 
3472474adfaSEd Tanous } // namespace redfish
348