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" 20*0d7702c0SZhenwei 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> 267e860f15SJohn Edward Broadbent 272474adfaSEd Tanous namespace redfish 282474adfaSEd Tanous { 294f48d5f6SEd Tanous inline void setPowerCapOverride( 308d1b46d7Szhanghch05 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 314bb3dc34SCarol Wang std::vector<nlohmann::json>& powerControlCollections) 324bb3dc34SCarol Wang { 33002d39b4SEd Tanous auto getChassisPath = 34002d39b4SEd Tanous [sensorsAsyncResp, powerControlCollections]( 35002d39b4SEd Tanous const std::optional<std::string>& chassisPath) mutable { 364bb3dc34SCarol Wang if (!chassisPath) 374bb3dc34SCarol Wang { 384bb3dc34SCarol Wang BMCWEB_LOG_ERROR << "Don't find valid chassis path "; 398d1b46d7Szhanghch05 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res, 400fda0f12SGeorge Liu "Chassis", sensorsAsyncResp->chassisId); 414bb3dc34SCarol Wang return; 424bb3dc34SCarol Wang } 434bb3dc34SCarol Wang 444bb3dc34SCarol Wang if (powerControlCollections.size() != 1) 454bb3dc34SCarol Wang { 468d1b46d7Szhanghch05 BMCWEB_LOG_ERROR << "Don't support multiple hosts at present "; 478d1b46d7Szhanghch05 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res, 488d1b46d7Szhanghch05 "Power", "PowerControl"); 494bb3dc34SCarol Wang return; 504bb3dc34SCarol Wang } 514bb3dc34SCarol Wang 524bb3dc34SCarol Wang auto& item = powerControlCollections[0]; 534bb3dc34SCarol Wang 544bb3dc34SCarol Wang std::optional<nlohmann::json> powerLimit; 558d1b46d7Szhanghch05 if (!json_util::readJson(item, sensorsAsyncResp->asyncResp->res, 568d1b46d7Szhanghch05 "PowerLimit", powerLimit)) 574bb3dc34SCarol Wang { 584bb3dc34SCarol Wang return; 594bb3dc34SCarol Wang } 604bb3dc34SCarol Wang if (!powerLimit) 614bb3dc34SCarol Wang { 624bb3dc34SCarol Wang return; 634bb3dc34SCarol Wang } 644bb3dc34SCarol Wang std::optional<uint32_t> value; 650fda0f12SGeorge Liu if (!json_util::readJson(*powerLimit, sensorsAsyncResp->asyncResp->res, 664bb3dc34SCarol Wang "LimitInWatts", value)) 674bb3dc34SCarol Wang { 684bb3dc34SCarol Wang return; 694bb3dc34SCarol Wang } 704bb3dc34SCarol Wang if (!value) 714bb3dc34SCarol Wang { 724bb3dc34SCarol Wang return; 734bb3dc34SCarol Wang } 741e1e598dSJonathan Doman sdbusplus::asio::getProperty<bool>( 751e1e598dSJonathan Doman *crow::connections::systemBus, "xyz.openbmc_project.Settings", 761e1e598dSJonathan Doman "/xyz/openbmc_project/control/host0/power_cap", 771e1e598dSJonathan Doman "xyz.openbmc_project.Control.Power.Cap", "PowerCapEnable", 781e1e598dSJonathan Doman [value, sensorsAsyncResp](const boost::system::error_code ec, 791e1e598dSJonathan Doman bool powerCapEnable) { 804bb3dc34SCarol Wang if (ec) 814bb3dc34SCarol Wang { 828d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 83002d39b4SEd Tanous BMCWEB_LOG_ERROR << "powerCapEnable Get handler: Dbus error " 84002d39b4SEd Tanous << ec; 854bb3dc34SCarol Wang return; 864bb3dc34SCarol Wang } 871e1e598dSJonathan Doman if (!powerCapEnable) 884bb3dc34SCarol Wang { 894bb3dc34SCarol Wang messages::actionNotSupported( 908d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res, 910fda0f12SGeorge Liu "Setting LimitInWatts when PowerLimit feature is disabled"); 924bb3dc34SCarol Wang BMCWEB_LOG_ERROR << "PowerLimit feature is disabled "; 934bb3dc34SCarol Wang return; 944bb3dc34SCarol Wang } 954bb3dc34SCarol Wang 964bb3dc34SCarol Wang crow::connections::systemBus->async_method_call( 978d1b46d7Szhanghch05 [sensorsAsyncResp](const boost::system::error_code ec2) { 9823a21a1cSEd Tanous if (ec2) 994bb3dc34SCarol Wang { 100002d39b4SEd Tanous BMCWEB_LOG_DEBUG << "Power Limit Set: Dbus error: " << ec2; 101002d39b4SEd Tanous messages::internalError(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 }; 114*0d7702c0SZhenwei Chen redfish::chassis_utils::getValidChassisPath(sensorsAsyncResp->asyncResp, 115*0d7702c0SZhenwei Chen sensorsAsyncResp->chassisId, 116*0d7702c0SZhenwei Chen std::move(getChassisPath)); 1174bb3dc34SCarol Wang } 1187e860f15SJohn Edward Broadbent inline void requestRoutesPower(App& app) 1192474adfaSEd Tanous { 1202474adfaSEd Tanous 1217e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/") 122ed398213SEd Tanous .privileges(redfish::privileges::getPower) 123002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 124002d39b4SEd Tanous [&app](const crow::Request& req, 12545ca1b86SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1267e860f15SJohn Edward Broadbent const std::string& chassisName) { 1273ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 12845ca1b86SEd Tanous { 12945ca1b86SEd Tanous return; 13045ca1b86SEd Tanous } 131168e20c1SEd Tanous asyncResp->res.jsonValue["PowerControl"] = nlohmann::json::array(); 132c5d03ff4SJennifer Lee 1332474adfaSEd Tanous auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>( 13402da7c5aSEd Tanous asyncResp, chassisName, sensors::dbus::powerPaths, 135a0ec28b6SAdrian Ambrożewicz sensors::node::power); 136028f7ebcSEddie James 1372474adfaSEd Tanous getChassisData(sensorAsyncResp); 138028f7ebcSEddie James 1397e860f15SJohn Edward Broadbent // This callback verifies that the power limit is only provided 1407e860f15SJohn Edward Broadbent // for the chassis that implements the Chassis inventory item. 1417e860f15SJohn Edward Broadbent // This prevents things like power supplies providing the 1427e860f15SJohn Edward Broadbent // chassis power limit 143b9d36b47SEd Tanous 144b9d36b47SEd Tanous using Mapper = dbus::utility::MapperGetSubTreePathsResponse; 145002d39b4SEd Tanous auto chassisHandler = 146002d39b4SEd Tanous [sensorAsyncResp](const boost::system::error_code e, 147b9d36b47SEd Tanous const Mapper& chassisPaths) { 148271584abSEd Tanous if (e) 149028f7ebcSEddie James { 150028f7ebcSEddie James BMCWEB_LOG_ERROR 151002d39b4SEd Tanous << "Power Limit GetSubTreePaths handler Dbus error " << e; 152028f7ebcSEddie James return; 153028f7ebcSEddie James } 154028f7ebcSEddie James 155028f7ebcSEddie James bool found = false; 156028f7ebcSEddie James for (const std::string& chassis : chassisPaths) 157028f7ebcSEddie James { 158028f7ebcSEddie James size_t len = std::string::npos; 159f23b7296SEd Tanous size_t lastPos = chassis.rfind('/'); 160028f7ebcSEddie James if (lastPos == std::string::npos) 161028f7ebcSEddie James { 162028f7ebcSEddie James continue; 163028f7ebcSEddie James } 164028f7ebcSEddie James 165028f7ebcSEddie James if (lastPos == chassis.size() - 1) 166028f7ebcSEddie James { 167028f7ebcSEddie James size_t end = lastPos; 168f23b7296SEd Tanous lastPos = chassis.rfind('/', lastPos - 1); 169028f7ebcSEddie James if (lastPos == std::string::npos) 170028f7ebcSEddie James { 171028f7ebcSEddie James continue; 172028f7ebcSEddie James } 173028f7ebcSEddie James 174028f7ebcSEddie James len = end - (lastPos + 1); 175028f7ebcSEddie James } 176028f7ebcSEddie James 177028f7ebcSEddie James std::string interfaceChassisName = 178028f7ebcSEddie James chassis.substr(lastPos + 1, len); 17955f79e6fSEd Tanous if (interfaceChassisName == sensorAsyncResp->chassisId) 180028f7ebcSEddie James { 181028f7ebcSEddie James found = true; 182028f7ebcSEddie James break; 183028f7ebcSEddie James } 184028f7ebcSEddie James } 185028f7ebcSEddie James 186028f7ebcSEddie James if (!found) 187028f7ebcSEddie James { 188028f7ebcSEddie James BMCWEB_LOG_DEBUG << "Power Limit not present for " 189028f7ebcSEddie James << sensorAsyncResp->chassisId; 190028f7ebcSEddie James return; 191028f7ebcSEddie James } 192028f7ebcSEddie James 193168e20c1SEd Tanous auto valueHandler = 194168e20c1SEd Tanous [sensorAsyncResp]( 195028f7ebcSEddie James const boost::system::error_code ec, 196b9d36b47SEd Tanous const dbus::utility::DBusPropertiesMap& properties) { 197028f7ebcSEddie James if (ec) 198028f7ebcSEddie James { 199002d39b4SEd Tanous messages::internalError(sensorAsyncResp->asyncResp->res); 200028f7ebcSEddie James BMCWEB_LOG_ERROR 201002d39b4SEd Tanous << "Power Limit GetAll handler: Dbus error " << ec; 202028f7ebcSEddie James return; 203028f7ebcSEddie James } 204028f7ebcSEddie James 2057e860f15SJohn Edward Broadbent nlohmann::json& tempArray = 206002d39b4SEd Tanous sensorAsyncResp->asyncResp->res.jsonValue["PowerControl"]; 207028f7ebcSEddie James 2087e860f15SJohn Edward Broadbent // Put multiple "sensors" into a single PowerControl, 0, 2097e860f15SJohn Edward Broadbent // so only create the first one 210028f7ebcSEddie James if (tempArray.empty()) 211028f7ebcSEddie James { 2127ab06f49SGunnar Mills // Mandatory properties odata.id and MemberId 2137ab06f49SGunnar Mills // A warning without a odata.type 2141476687dSEd Tanous nlohmann::json::object_t powerControl; 215002d39b4SEd Tanous powerControl["@odata.type"] = "#Power.v1_0_0.PowerControl"; 216002d39b4SEd Tanous powerControl["@odata.id"] = "/redfish/v1/Chassis/" + 2177ab06f49SGunnar Mills sensorAsyncResp->chassisId + 2181476687dSEd Tanous "/Power#/PowerControl/0"; 2191476687dSEd Tanous powerControl["Name"] = "Chassis Power Control"; 2201476687dSEd Tanous powerControl["MemberId"] = "0"; 2211476687dSEd Tanous tempArray.push_back(std::move(powerControl)); 222028f7ebcSEddie James } 223028f7ebcSEddie James 224028f7ebcSEddie James nlohmann::json& sensorJson = tempArray.back(); 225028f7ebcSEddie James bool enabled = false; 226028f7ebcSEddie James double powerCap = 0.0; 227028f7ebcSEddie James int64_t scale = 0; 228028f7ebcSEddie James 229168e20c1SEd Tanous for (const std::pair<std::string, 230002d39b4SEd Tanous dbus::utility::DbusVariantType>& property : 231002d39b4SEd Tanous properties) 232028f7ebcSEddie James { 23355f79e6fSEd Tanous if (property.first == "Scale") 234028f7ebcSEddie James { 235028f7ebcSEddie James const int64_t* i = 2368d78b7a9SPatrick Williams std::get_if<int64_t>(&property.second); 237028f7ebcSEddie James 238e662eae8SEd Tanous if (i != nullptr) 239028f7ebcSEddie James { 240028f7ebcSEddie James scale = *i; 241028f7ebcSEddie James } 242028f7ebcSEddie James } 24355f79e6fSEd Tanous else if (property.first == "PowerCap") 244028f7ebcSEddie James { 245002d39b4SEd Tanous const double* d = std::get_if<double>(&property.second); 246028f7ebcSEddie James const int64_t* i = 2478d78b7a9SPatrick Williams std::get_if<int64_t>(&property.second); 248028f7ebcSEddie James const uint32_t* u = 2498d78b7a9SPatrick Williams std::get_if<uint32_t>(&property.second); 250028f7ebcSEddie James 251e662eae8SEd Tanous if (d != nullptr) 252028f7ebcSEddie James { 253028f7ebcSEddie James powerCap = *d; 254028f7ebcSEddie James } 255e662eae8SEd Tanous else if (i != nullptr) 256028f7ebcSEddie James { 257271584abSEd Tanous powerCap = static_cast<double>(*i); 258028f7ebcSEddie James } 259e662eae8SEd Tanous else if (u != nullptr) 260028f7ebcSEddie James { 261028f7ebcSEddie James powerCap = *u; 262028f7ebcSEddie James } 263028f7ebcSEddie James } 26455f79e6fSEd Tanous else if (property.first == "PowerCapEnable") 265028f7ebcSEddie James { 266002d39b4SEd Tanous const bool* b = std::get_if<bool>(&property.second); 267028f7ebcSEddie James 268e662eae8SEd Tanous if (b != nullptr) 269028f7ebcSEddie James { 270028f7ebcSEddie James enabled = *b; 271028f7ebcSEddie James } 272028f7ebcSEddie James } 273028f7ebcSEddie James } 274028f7ebcSEddie James 2757ab06f49SGunnar Mills nlohmann::json& value = 2767ab06f49SGunnar Mills sensorJson["PowerLimit"]["LimitInWatts"]; 277028f7ebcSEddie James 2787e860f15SJohn Edward Broadbent // LimitException is Mandatory attribute as per OCP 2797e860f15SJohn Edward Broadbent // Baseline Profile – v1.0.0, so currently making it 2807e860f15SJohn Edward Broadbent // "NoAction" as default value to make it OCP Compliant. 2815a64a6f3SJoshi-Mansi sensorJson["PowerLimit"]["LimitException"] = "NoAction"; 2825a64a6f3SJoshi-Mansi 283028f7ebcSEddie James if (enabled) 284028f7ebcSEddie James { 2857e860f15SJohn Edward Broadbent // Redfish specification indicates PowerLimit should 2867e860f15SJohn Edward Broadbent // be null if the limit is not enabled. 287028f7ebcSEddie James value = powerCap * std::pow(10, scale); 288028f7ebcSEddie James } 289028f7ebcSEddie James }; 290028f7ebcSEddie James 291028f7ebcSEddie James crow::connections::systemBus->async_method_call( 292028f7ebcSEddie James std::move(valueHandler), "xyz.openbmc_project.Settings", 293028f7ebcSEddie James "/xyz/openbmc_project/control/host0/power_cap", 294028f7ebcSEddie James "org.freedesktop.DBus.Properties", "GetAll", 295028f7ebcSEddie James "xyz.openbmc_project.Control.Power.Cap"); 296028f7ebcSEddie James }; 297028f7ebcSEddie James 298028f7ebcSEddie James crow::connections::systemBus->async_method_call( 299168e20c1SEd Tanous std::move(chassisHandler), "xyz.openbmc_project.ObjectMapper", 300028f7ebcSEddie James "/xyz/openbmc_project/object_mapper", 301028f7ebcSEddie James "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", 302271584abSEd Tanous "/xyz/openbmc_project/inventory", 0, 303f857e9aeSAppaRao Puli std::array<const char*, 2>{ 304f857e9aeSAppaRao Puli "xyz.openbmc_project.Inventory.Item.Board", 305028f7ebcSEddie James "xyz.openbmc_project.Inventory.Item.Chassis"}); 3067e860f15SJohn Edward Broadbent }); 3074bb3dc34SCarol Wang 3087e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/") 309ed398213SEd Tanous .privileges(redfish::privileges::patchPower) 3107e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::patch)( 31145ca1b86SEd Tanous [&app](const crow::Request& req, 3127e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 3137e860f15SJohn Edward Broadbent const std::string& chassisName) { 3143ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 31545ca1b86SEd Tanous { 31645ca1b86SEd Tanous return; 31745ca1b86SEd Tanous } 3188d1b46d7Szhanghch05 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>( 31902da7c5aSEd Tanous asyncResp, chassisName, sensors::dbus::powerPaths, 320a0ec28b6SAdrian Ambrożewicz sensors::node::power); 3214bb3dc34SCarol Wang 3224bb3dc34SCarol Wang std::optional<std::vector<nlohmann::json>> voltageCollections; 3234bb3dc34SCarol Wang std::optional<std::vector<nlohmann::json>> powerCtlCollections; 3244bb3dc34SCarol Wang 325002d39b4SEd Tanous if (!json_util::readJsonPatch(req, sensorAsyncResp->asyncResp->res, 326002d39b4SEd Tanous "PowerControl", powerCtlCollections, 327002d39b4SEd Tanous "Voltages", voltageCollections)) 3284bb3dc34SCarol Wang { 3294bb3dc34SCarol Wang return; 3304bb3dc34SCarol Wang } 3314bb3dc34SCarol Wang 3324bb3dc34SCarol Wang if (powerCtlCollections) 3334bb3dc34SCarol Wang { 3348d1b46d7Szhanghch05 setPowerCapOverride(sensorAsyncResp, *powerCtlCollections); 3354bb3dc34SCarol Wang } 3364bb3dc34SCarol Wang if (voltageCollections) 3374bb3dc34SCarol Wang { 3384bb3dc34SCarol Wang std::unordered_map<std::string, std::vector<nlohmann::json>> 3394bb3dc34SCarol Wang allCollections; 340002d39b4SEd Tanous allCollections.emplace("Voltages", *std::move(voltageCollections)); 34180ac4024SBruce Lee setSensorsOverride(sensorAsyncResp, allCollections); 3424bb3dc34SCarol Wang } 3437e860f15SJohn Edward Broadbent }); 344413961deSRichard Marian Thomaiyar } 3452474adfaSEd Tanous 3462474adfaSEd Tanous } // namespace redfish 347