xref: /openbmc/bmcweb/features/redfish/lib/power.hpp (revision 7e860f1550c8686eec42f7a75bc5f2ef51e756ad)
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 
21*7e860f15SJohn Edward Broadbent #include <app.hpp>
22*7e860f15SJohn Edward Broadbent 
232474adfaSEd Tanous namespace redfish
242474adfaSEd Tanous {
254bb3dc34SCarol Wang void setPowerCapOverride(
268d1b46d7Szhanghch05     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
274bb3dc34SCarol Wang     std::vector<nlohmann::json>& powerControlCollections)
284bb3dc34SCarol Wang {
29*7e860f15SJohn Edward Broadbent     auto getChassisPath =
30*7e860f15SJohn Edward Broadbent         [sensorsAsyncResp, powerControlCollections](
31*7e860f15SJohn Edward Broadbent             const std::optional<std::string>& chassisPath) mutable {
324bb3dc34SCarol Wang             if (!chassisPath)
334bb3dc34SCarol Wang             {
344bb3dc34SCarol Wang                 BMCWEB_LOG_ERROR << "Don't find valid chassis path ";
358d1b46d7Szhanghch05                 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
368d1b46d7Szhanghch05                                            "Chassis",
378d1b46d7Szhanghch05                                            sensorsAsyncResp->chassisId);
384bb3dc34SCarol Wang                 return;
394bb3dc34SCarol Wang             }
404bb3dc34SCarol Wang 
414bb3dc34SCarol Wang             if (powerControlCollections.size() != 1)
424bb3dc34SCarol Wang             {
438d1b46d7Szhanghch05                 BMCWEB_LOG_ERROR << "Don't support multiple hosts at present ";
448d1b46d7Szhanghch05                 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
458d1b46d7Szhanghch05                                            "Power", "PowerControl");
464bb3dc34SCarol Wang                 return;
474bb3dc34SCarol Wang             }
484bb3dc34SCarol Wang 
494bb3dc34SCarol Wang             auto& item = powerControlCollections[0];
504bb3dc34SCarol Wang 
514bb3dc34SCarol Wang             std::optional<nlohmann::json> powerLimit;
528d1b46d7Szhanghch05             if (!json_util::readJson(item, sensorsAsyncResp->asyncResp->res,
538d1b46d7Szhanghch05                                      "PowerLimit", powerLimit))
544bb3dc34SCarol Wang             {
554bb3dc34SCarol Wang                 return;
564bb3dc34SCarol Wang             }
574bb3dc34SCarol Wang             if (!powerLimit)
584bb3dc34SCarol Wang             {
594bb3dc34SCarol Wang                 return;
604bb3dc34SCarol Wang             }
614bb3dc34SCarol Wang             std::optional<uint32_t> value;
628d1b46d7Szhanghch05             if (!json_util::readJson(*powerLimit,
638d1b46d7Szhanghch05                                      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             }
728d1b46d7Szhanghch05             auto valueHandler = [value, sensorsAsyncResp](
734bb3dc34SCarol Wang                                     const boost::system::error_code ec,
744bb3dc34SCarol Wang                                     const SensorVariant& powerCapEnable) {
754bb3dc34SCarol Wang                 if (ec)
764bb3dc34SCarol Wang                 {
778d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
784bb3dc34SCarol Wang                     BMCWEB_LOG_ERROR
794bb3dc34SCarol Wang                         << "powerCapEnable Get handler: Dbus error " << ec;
804bb3dc34SCarol Wang                     return;
814bb3dc34SCarol Wang                 }
824bb3dc34SCarol Wang                 // Check PowerCapEnable
838d78b7a9SPatrick Williams                 const bool* b = std::get_if<bool>(&powerCapEnable);
844bb3dc34SCarol Wang                 if (b == nullptr)
854bb3dc34SCarol Wang                 {
868d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
878d1b46d7Szhanghch05                     BMCWEB_LOG_ERROR << "Fail to get PowerCapEnable status ";
884bb3dc34SCarol Wang                     return;
894bb3dc34SCarol Wang                 }
904bb3dc34SCarol Wang                 if (!(*b))
914bb3dc34SCarol Wang                 {
924bb3dc34SCarol Wang                     messages::actionNotSupported(
938d1b46d7Szhanghch05                         sensorsAsyncResp->asyncResp->res,
944bb3dc34SCarol Wang                         "Setting LimitInWatts when PowerLimit "
954bb3dc34SCarol Wang                         "feature is disabled");
964bb3dc34SCarol Wang                     BMCWEB_LOG_ERROR << "PowerLimit feature is disabled ";
974bb3dc34SCarol Wang                     return;
984bb3dc34SCarol Wang                 }
994bb3dc34SCarol Wang 
1004bb3dc34SCarol Wang                 crow::connections::systemBus->async_method_call(
1018d1b46d7Szhanghch05                     [sensorsAsyncResp](const boost::system::error_code ec2) {
10223a21a1cSEd Tanous                         if (ec2)
1034bb3dc34SCarol Wang                         {
1048d1b46d7Szhanghch05                             BMCWEB_LOG_DEBUG << "Power Limit Set: Dbus error: "
1058d1b46d7Szhanghch05                                              << ec2;
1068d1b46d7Szhanghch05                             messages::internalError(
1078d1b46d7Szhanghch05                                 sensorsAsyncResp->asyncResp->res);
1084bb3dc34SCarol Wang                             return;
1094bb3dc34SCarol Wang                         }
1108d1b46d7Szhanghch05                         sensorsAsyncResp->asyncResp->res.result(
1114bb3dc34SCarol Wang                             boost::beast::http::status::no_content);
1124bb3dc34SCarol Wang                     },
1134bb3dc34SCarol Wang                     "xyz.openbmc_project.Settings",
1144bb3dc34SCarol Wang                     "/xyz/openbmc_project/control/host0/power_cap",
1154bb3dc34SCarol Wang                     "org.freedesktop.DBus.Properties", "Set",
1164bb3dc34SCarol Wang                     "xyz.openbmc_project.Control.Power.Cap", "PowerCap",
11719bd78d9SPatrick Williams                     std::variant<uint32_t>(*value));
1184bb3dc34SCarol Wang             };
1194bb3dc34SCarol Wang             crow::connections::systemBus->async_method_call(
1204bb3dc34SCarol Wang                 std::move(valueHandler), "xyz.openbmc_project.Settings",
1214bb3dc34SCarol Wang                 "/xyz/openbmc_project/control/host0/power_cap",
1224bb3dc34SCarol Wang                 "org.freedesktop.DBus.Properties", "Get",
1234bb3dc34SCarol Wang                 "xyz.openbmc_project.Control.Power.Cap", "PowerCapEnable");
1244bb3dc34SCarol Wang         };
1258d1b46d7Szhanghch05     getValidChassisPath(sensorsAsyncResp, std::move(getChassisPath));
1264bb3dc34SCarol Wang }
127*7e860f15SJohn Edward Broadbent inline void requestRoutesPower(App& app)
1282474adfaSEd Tanous {
1292474adfaSEd Tanous 
130*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
131*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
132*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
133*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
134*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
135*7e860f15SJohn Edward Broadbent                const std::string& chassisName) {
136*7e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["PowerControl"] =
137*7e860f15SJohn Edward Broadbent                     nlohmann::json::array();
138c5d03ff4SJennifer Lee 
1392474adfaSEd Tanous                 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
1408d1b46d7Szhanghch05                     asyncResp, chassisName,
1418d1b46d7Szhanghch05                     sensors::dbus::paths.at(sensors::node::power),
142a0ec28b6SAdrian Ambrożewicz                     sensors::node::power);
143028f7ebcSEddie James 
1442474adfaSEd Tanous                 getChassisData(sensorAsyncResp);
145028f7ebcSEddie James 
146*7e860f15SJohn Edward Broadbent                 // This callback verifies that the power limit is only provided
147*7e860f15SJohn Edward Broadbent                 // for the chassis that implements the Chassis inventory item.
148*7e860f15SJohn Edward Broadbent                 // This prevents things like power supplies providing the
149*7e860f15SJohn Edward Broadbent                 // chassis power limit
150028f7ebcSEddie James                 auto chassisHandler = [sensorAsyncResp](
151271584abSEd Tanous                                           const boost::system::error_code e,
152028f7ebcSEddie James                                           const std::vector<std::string>&
153028f7ebcSEddie James                                               chassisPaths) {
154271584abSEd Tanous                     if (e)
155028f7ebcSEddie James                     {
156028f7ebcSEddie James                         BMCWEB_LOG_ERROR
157*7e860f15SJohn Edward Broadbent                             << "Power Limit GetSubTreePaths handler Dbus error "
158*7e860f15SJohn Edward Broadbent                             << e;
159028f7ebcSEddie James                         return;
160028f7ebcSEddie James                     }
161028f7ebcSEddie James 
162028f7ebcSEddie James                     bool found = false;
163028f7ebcSEddie James                     for (const std::string& chassis : chassisPaths)
164028f7ebcSEddie James                     {
165028f7ebcSEddie James                         size_t len = std::string::npos;
166f23b7296SEd Tanous                         size_t lastPos = chassis.rfind('/');
167028f7ebcSEddie James                         if (lastPos == std::string::npos)
168028f7ebcSEddie James                         {
169028f7ebcSEddie James                             continue;
170028f7ebcSEddie James                         }
171028f7ebcSEddie James 
172028f7ebcSEddie James                         if (lastPos == chassis.size() - 1)
173028f7ebcSEddie James                         {
174028f7ebcSEddie James                             size_t end = lastPos;
175f23b7296SEd Tanous                             lastPos = chassis.rfind('/', lastPos - 1);
176028f7ebcSEddie James                             if (lastPos == std::string::npos)
177028f7ebcSEddie James                             {
178028f7ebcSEddie James                                 continue;
179028f7ebcSEddie James                             }
180028f7ebcSEddie James 
181028f7ebcSEddie James                             len = end - (lastPos + 1);
182028f7ebcSEddie James                         }
183028f7ebcSEddie James 
184028f7ebcSEddie James                         std::string interfaceChassisName =
185028f7ebcSEddie James                             chassis.substr(lastPos + 1, len);
186*7e860f15SJohn Edward Broadbent                         if (!interfaceChassisName.compare(
187*7e860f15SJohn Edward Broadbent                                 sensorAsyncResp->chassisId))
188028f7ebcSEddie James                         {
189028f7ebcSEddie James                             found = true;
190028f7ebcSEddie James                             break;
191028f7ebcSEddie James                         }
192028f7ebcSEddie James                     }
193028f7ebcSEddie James 
194028f7ebcSEddie James                     if (!found)
195028f7ebcSEddie James                     {
196028f7ebcSEddie James                         BMCWEB_LOG_DEBUG << "Power Limit not present for "
197028f7ebcSEddie James                                          << sensorAsyncResp->chassisId;
198028f7ebcSEddie James                         return;
199028f7ebcSEddie James                     }
200028f7ebcSEddie James 
201*7e860f15SJohn Edward Broadbent                     auto valueHandler = [sensorAsyncResp](
202028f7ebcSEddie James                                             const boost::system::error_code ec,
203*7e860f15SJohn Edward Broadbent                                             const std::vector<std::pair<
204*7e860f15SJohn Edward Broadbent                                                 std::string, SensorVariant>>&
205028f7ebcSEddie James                                                 properties) {
206028f7ebcSEddie James                         if (ec)
207028f7ebcSEddie James                         {
2088d1b46d7Szhanghch05                             messages::internalError(
2098d1b46d7Szhanghch05                                 sensorAsyncResp->asyncResp->res);
210028f7ebcSEddie James                             BMCWEB_LOG_ERROR
211*7e860f15SJohn Edward Broadbent                                 << "Power Limit GetAll handler: Dbus error "
212*7e860f15SJohn Edward Broadbent                                 << ec;
213028f7ebcSEddie James                             return;
214028f7ebcSEddie James                         }
215028f7ebcSEddie James 
216*7e860f15SJohn Edward Broadbent                         nlohmann::json& tempArray =
217*7e860f15SJohn Edward Broadbent                             sensorAsyncResp->asyncResp->res
2188d1b46d7Szhanghch05                                 .jsonValue["PowerControl"];
219028f7ebcSEddie James 
220*7e860f15SJohn Edward Broadbent                         // Put multiple "sensors" into a single PowerControl, 0,
221*7e860f15SJohn Edward Broadbent                         // so only create the first one
222028f7ebcSEddie James                         if (tempArray.empty())
223028f7ebcSEddie James                         {
2247ab06f49SGunnar Mills                             // Mandatory properties odata.id and MemberId
2257ab06f49SGunnar Mills                             // A warning without a odata.type
2267ab06f49SGunnar Mills                             tempArray.push_back(
2277ab06f49SGunnar Mills                                 {{"@odata.type", "#Power.v1_0_0.PowerControl"},
2287ab06f49SGunnar Mills                                  {"@odata.id", "/redfish/v1/Chassis/" +
2297ab06f49SGunnar Mills                                                    sensorAsyncResp->chassisId +
2307ab06f49SGunnar Mills                                                    "/Power#/PowerControl/0"},
2317ab06f49SGunnar Mills                                  {"Name", "Chassis Power Control"},
2327ab06f49SGunnar Mills                                  {"MemberId", "0"}});
233028f7ebcSEddie James                         }
234028f7ebcSEddie James 
235028f7ebcSEddie James                         nlohmann::json& sensorJson = tempArray.back();
236028f7ebcSEddie James                         bool enabled = false;
237028f7ebcSEddie James                         double powerCap = 0.0;
238028f7ebcSEddie James                         int64_t scale = 0;
239028f7ebcSEddie James 
240*7e860f15SJohn Edward Broadbent                         for (const std::pair<std::string, SensorVariant>&
241*7e860f15SJohn Edward Broadbent                                  property : properties)
242028f7ebcSEddie James                         {
243028f7ebcSEddie James                             if (!property.first.compare("Scale"))
244028f7ebcSEddie James                             {
245028f7ebcSEddie James                                 const int64_t* i =
2468d78b7a9SPatrick Williams                                     std::get_if<int64_t>(&property.second);
247028f7ebcSEddie James 
248028f7ebcSEddie James                                 if (i)
249028f7ebcSEddie James                                 {
250028f7ebcSEddie James                                     scale = *i;
251028f7ebcSEddie James                                 }
252028f7ebcSEddie James                             }
253028f7ebcSEddie James                             else if (!property.first.compare("PowerCap"))
254028f7ebcSEddie James                             {
255028f7ebcSEddie James                                 const double* d =
2568d78b7a9SPatrick Williams                                     std::get_if<double>(&property.second);
257028f7ebcSEddie James                                 const int64_t* i =
2588d78b7a9SPatrick Williams                                     std::get_if<int64_t>(&property.second);
259028f7ebcSEddie James                                 const uint32_t* u =
2608d78b7a9SPatrick Williams                                     std::get_if<uint32_t>(&property.second);
261028f7ebcSEddie James 
262028f7ebcSEddie James                                 if (d)
263028f7ebcSEddie James                                 {
264028f7ebcSEddie James                                     powerCap = *d;
265028f7ebcSEddie James                                 }
266028f7ebcSEddie James                                 else if (i)
267028f7ebcSEddie James                                 {
268271584abSEd Tanous                                     powerCap = static_cast<double>(*i);
269028f7ebcSEddie James                                 }
270028f7ebcSEddie James                                 else if (u)
271028f7ebcSEddie James                                 {
272028f7ebcSEddie James                                     powerCap = *u;
273028f7ebcSEddie James                                 }
274028f7ebcSEddie James                             }
275028f7ebcSEddie James                             else if (!property.first.compare("PowerCapEnable"))
276028f7ebcSEddie James                             {
277*7e860f15SJohn Edward Broadbent                                 const bool* b =
278*7e860f15SJohn Edward Broadbent                                     std::get_if<bool>(&property.second);
279028f7ebcSEddie James 
280028f7ebcSEddie James                                 if (b)
281028f7ebcSEddie James                                 {
282028f7ebcSEddie James                                     enabled = *b;
283028f7ebcSEddie James                                 }
284028f7ebcSEddie James                             }
285028f7ebcSEddie James                         }
286028f7ebcSEddie James 
2877ab06f49SGunnar Mills                         nlohmann::json& value =
2887ab06f49SGunnar Mills                             sensorJson["PowerLimit"]["LimitInWatts"];
289028f7ebcSEddie James 
290*7e860f15SJohn Edward Broadbent                         // LimitException is Mandatory attribute as per OCP
291*7e860f15SJohn Edward Broadbent                         // Baseline Profile – v1.0.0, so currently making it
292*7e860f15SJohn Edward Broadbent                         // "NoAction" as default value to make it OCP Compliant.
2935a64a6f3SJoshi-Mansi                         sensorJson["PowerLimit"]["LimitException"] = "NoAction";
2945a64a6f3SJoshi-Mansi 
295028f7ebcSEddie James                         if (enabled)
296028f7ebcSEddie James                         {
297*7e860f15SJohn Edward Broadbent                             // Redfish specification indicates PowerLimit should
298*7e860f15SJohn Edward Broadbent                             // be null if the limit is not enabled.
299028f7ebcSEddie James                             value = powerCap * std::pow(10, scale);
300028f7ebcSEddie James                         }
301028f7ebcSEddie James                     };
302028f7ebcSEddie James 
303028f7ebcSEddie James                     crow::connections::systemBus->async_method_call(
304028f7ebcSEddie James                         std::move(valueHandler), "xyz.openbmc_project.Settings",
305028f7ebcSEddie James                         "/xyz/openbmc_project/control/host0/power_cap",
306028f7ebcSEddie James                         "org.freedesktop.DBus.Properties", "GetAll",
307028f7ebcSEddie James                         "xyz.openbmc_project.Control.Power.Cap");
308028f7ebcSEddie James                 };
309028f7ebcSEddie James 
310028f7ebcSEddie James                 crow::connections::systemBus->async_method_call(
311*7e860f15SJohn Edward Broadbent                     std::move(chassisHandler),
312*7e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.ObjectMapper",
313028f7ebcSEddie James                     "/xyz/openbmc_project/object_mapper",
314028f7ebcSEddie James                     "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
315271584abSEd Tanous                     "/xyz/openbmc_project/inventory", 0,
316f857e9aeSAppaRao Puli                     std::array<const char*, 2>{
317f857e9aeSAppaRao Puli                         "xyz.openbmc_project.Inventory.Item.Board",
318028f7ebcSEddie James                         "xyz.openbmc_project.Inventory.Item.Chassis"});
319*7e860f15SJohn Edward Broadbent             });
3204bb3dc34SCarol Wang 
321*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
322*7e860f15SJohn Edward Broadbent         .privileges({"ConfigureManager"})
323*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
324*7e860f15SJohn Edward Broadbent             [](const crow::Request& req,
325*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
326*7e860f15SJohn Edward Broadbent                const std::string& chassisName) {
3278d1b46d7Szhanghch05                 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
3288d1b46d7Szhanghch05                     asyncResp, chassisName,
3298d1b46d7Szhanghch05                     sensors::dbus::paths.at(sensors::node::power),
330a0ec28b6SAdrian Ambrożewicz                     sensors::node::power);
3314bb3dc34SCarol Wang 
3324bb3dc34SCarol Wang                 std::optional<std::vector<nlohmann::json>> voltageCollections;
3334bb3dc34SCarol Wang                 std::optional<std::vector<nlohmann::json>> powerCtlCollections;
3344bb3dc34SCarol Wang 
3358d1b46d7Szhanghch05                 if (!json_util::readJson(req, sensorAsyncResp->asyncResp->res,
3368d1b46d7Szhanghch05                                          "PowerControl", powerCtlCollections,
3378d1b46d7Szhanghch05                                          "Voltages", voltageCollections))
3384bb3dc34SCarol Wang                 {
3394bb3dc34SCarol Wang                     return;
3404bb3dc34SCarol Wang                 }
3414bb3dc34SCarol Wang 
3424bb3dc34SCarol Wang                 if (powerCtlCollections)
3434bb3dc34SCarol Wang                 {
3448d1b46d7Szhanghch05                     setPowerCapOverride(sensorAsyncResp, *powerCtlCollections);
3454bb3dc34SCarol Wang                 }
3464bb3dc34SCarol Wang                 if (voltageCollections)
3474bb3dc34SCarol Wang                 {
3484bb3dc34SCarol Wang                     std::unordered_map<std::string, std::vector<nlohmann::json>>
3494bb3dc34SCarol Wang                         allCollections;
350*7e860f15SJohn Edward Broadbent                     allCollections.emplace("Voltages",
351*7e860f15SJohn Edward Broadbent                                            *std::move(voltageCollections));
3528d1b46d7Szhanghch05                     checkAndDoSensorsOverride(sensorAsyncResp, allCollections);
3534bb3dc34SCarol Wang                 }
354*7e860f15SJohn Edward Broadbent             });
355413961deSRichard Marian Thomaiyar }
3562474adfaSEd Tanous 
3572474adfaSEd Tanous } // namespace redfish
358