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 "node.hpp" 202474adfaSEd Tanous #include "sensors.hpp" 212474adfaSEd Tanous 222474adfaSEd Tanous namespace redfish 232474adfaSEd Tanous { 242474adfaSEd Tanous 252474adfaSEd Tanous class Power : public Node 262474adfaSEd Tanous { 272474adfaSEd Tanous public: 2852cc112dSEd Tanous Power(App& app) : 292474adfaSEd Tanous Node((app), "/redfish/v1/Chassis/<str>/Power/", std::string()) 302474adfaSEd Tanous { 312474adfaSEd Tanous entityPrivileges = { 322474adfaSEd Tanous {boost::beast::http::verb::get, {{"Login"}}}, 332474adfaSEd Tanous {boost::beast::http::verb::head, {{"Login"}}}, 341b1b43f2Sjayaprakash Mutyala {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, 352474adfaSEd Tanous {boost::beast::http::verb::put, {{"ConfigureManager"}}}, 362474adfaSEd Tanous {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, 372474adfaSEd Tanous {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; 382474adfaSEd Tanous } 392474adfaSEd Tanous 402474adfaSEd Tanous private: 414bb3dc34SCarol Wang void setPowerCapOverride( 424bb3dc34SCarol Wang std::shared_ptr<SensorsAsyncResp> asyncResp, 434bb3dc34SCarol Wang std::vector<nlohmann::json>& powerControlCollections) 444bb3dc34SCarol Wang { 454bb3dc34SCarol Wang auto getChassisPath = 464bb3dc34SCarol Wang [asyncResp, powerControlCollections]( 474bb3dc34SCarol Wang const std::optional<std::string>& chassisPath) mutable { 484bb3dc34SCarol Wang if (!chassisPath) 494bb3dc34SCarol Wang { 504bb3dc34SCarol Wang BMCWEB_LOG_ERROR << "Don't find valid chassis path "; 514bb3dc34SCarol Wang messages::resourceNotFound(asyncResp->res, "Chassis", 524bb3dc34SCarol Wang asyncResp->chassisId); 534bb3dc34SCarol Wang return; 544bb3dc34SCarol Wang } 554bb3dc34SCarol Wang 564bb3dc34SCarol Wang if (powerControlCollections.size() != 1) 574bb3dc34SCarol Wang { 584bb3dc34SCarol Wang BMCWEB_LOG_ERROR 594bb3dc34SCarol Wang << "Don't support multiple hosts at present "; 604bb3dc34SCarol Wang messages::resourceNotFound(asyncResp->res, "Power", 614bb3dc34SCarol Wang "PowerControl"); 624bb3dc34SCarol Wang return; 634bb3dc34SCarol Wang } 644bb3dc34SCarol Wang 654bb3dc34SCarol Wang auto& item = powerControlCollections[0]; 664bb3dc34SCarol Wang 674bb3dc34SCarol Wang std::optional<nlohmann::json> powerLimit; 684bb3dc34SCarol Wang if (!json_util::readJson(item, asyncResp->res, "PowerLimit", 694bb3dc34SCarol Wang powerLimit)) 704bb3dc34SCarol Wang { 714bb3dc34SCarol Wang return; 724bb3dc34SCarol Wang } 734bb3dc34SCarol Wang if (!powerLimit) 744bb3dc34SCarol Wang { 754bb3dc34SCarol Wang return; 764bb3dc34SCarol Wang } 774bb3dc34SCarol Wang std::optional<uint32_t> value; 784bb3dc34SCarol Wang if (!json_util::readJson(*powerLimit, asyncResp->res, 794bb3dc34SCarol Wang "LimitInWatts", value)) 804bb3dc34SCarol Wang { 814bb3dc34SCarol Wang return; 824bb3dc34SCarol Wang } 834bb3dc34SCarol Wang if (!value) 844bb3dc34SCarol Wang { 854bb3dc34SCarol Wang return; 864bb3dc34SCarol Wang } 874bb3dc34SCarol Wang auto valueHandler = [value, asyncResp]( 884bb3dc34SCarol Wang const boost::system::error_code ec, 894bb3dc34SCarol Wang const SensorVariant& powerCapEnable) { 904bb3dc34SCarol Wang if (ec) 914bb3dc34SCarol Wang { 924bb3dc34SCarol Wang messages::internalError(asyncResp->res); 934bb3dc34SCarol Wang BMCWEB_LOG_ERROR 944bb3dc34SCarol Wang << "powerCapEnable Get handler: Dbus error " << ec; 954bb3dc34SCarol Wang return; 964bb3dc34SCarol Wang } 974bb3dc34SCarol Wang // Check PowerCapEnable 988d78b7a9SPatrick Williams const bool* b = std::get_if<bool>(&powerCapEnable); 994bb3dc34SCarol Wang if (b == nullptr) 1004bb3dc34SCarol Wang { 1014bb3dc34SCarol Wang messages::internalError(asyncResp->res); 1024bb3dc34SCarol Wang BMCWEB_LOG_ERROR 1034bb3dc34SCarol Wang << "Fail to get PowerCapEnable status "; 1044bb3dc34SCarol Wang return; 1054bb3dc34SCarol Wang } 1064bb3dc34SCarol Wang if (!(*b)) 1074bb3dc34SCarol Wang { 1084bb3dc34SCarol Wang messages::actionNotSupported( 1094bb3dc34SCarol Wang asyncResp->res, 1104bb3dc34SCarol Wang "Setting LimitInWatts when PowerLimit " 1114bb3dc34SCarol Wang "feature is disabled"); 1124bb3dc34SCarol Wang BMCWEB_LOG_ERROR << "PowerLimit feature is disabled "; 1134bb3dc34SCarol Wang return; 1144bb3dc34SCarol Wang } 1154bb3dc34SCarol Wang 1164bb3dc34SCarol Wang crow::connections::systemBus->async_method_call( 11723a21a1cSEd Tanous [asyncResp](const boost::system::error_code ec2) { 11823a21a1cSEd Tanous if (ec2) 1194bb3dc34SCarol Wang { 1204bb3dc34SCarol Wang BMCWEB_LOG_DEBUG 12123a21a1cSEd Tanous << "Power Limit Set: Dbus error: " << ec2; 1224bb3dc34SCarol Wang messages::internalError(asyncResp->res); 1234bb3dc34SCarol Wang return; 1244bb3dc34SCarol Wang } 1254bb3dc34SCarol Wang asyncResp->res.result( 1264bb3dc34SCarol Wang boost::beast::http::status::no_content); 1274bb3dc34SCarol Wang }, 1284bb3dc34SCarol Wang "xyz.openbmc_project.Settings", 1294bb3dc34SCarol Wang "/xyz/openbmc_project/control/host0/power_cap", 1304bb3dc34SCarol Wang "org.freedesktop.DBus.Properties", "Set", 1314bb3dc34SCarol Wang "xyz.openbmc_project.Control.Power.Cap", "PowerCap", 13219bd78d9SPatrick Williams std::variant<uint32_t>(*value)); 1334bb3dc34SCarol Wang }; 1344bb3dc34SCarol Wang crow::connections::systemBus->async_method_call( 1354bb3dc34SCarol Wang std::move(valueHandler), "xyz.openbmc_project.Settings", 1364bb3dc34SCarol Wang "/xyz/openbmc_project/control/host0/power_cap", 1374bb3dc34SCarol Wang "org.freedesktop.DBus.Properties", "Get", 1384bb3dc34SCarol Wang "xyz.openbmc_project.Control.Power.Cap", "PowerCapEnable"); 1394bb3dc34SCarol Wang }; 1404bb3dc34SCarol Wang getValidChassisPath(asyncResp, std::move(getChassisPath)); 1414bb3dc34SCarol Wang } 142cb13a392SEd Tanous void doGet(crow::Response& res, const crow::Request&, 1432474adfaSEd Tanous const std::vector<std::string>& params) override 1442474adfaSEd Tanous { 1452474adfaSEd Tanous if (params.size() != 1) 1462474adfaSEd Tanous { 1472474adfaSEd Tanous res.result(boost::beast::http::status::internal_server_error); 1482474adfaSEd Tanous res.end(); 1492474adfaSEd Tanous return; 1502474adfaSEd Tanous } 151*2c70f800SEd Tanous const std::string& chassisName = params[0]; 1522474adfaSEd Tanous 153c5d03ff4SJennifer Lee res.jsonValue["PowerControl"] = nlohmann::json::array(); 154c5d03ff4SJennifer Lee 1552474adfaSEd Tanous auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>( 156*2c70f800SEd Tanous res, chassisName, sensors::dbus::types.at(sensors::node::power), 157a0ec28b6SAdrian Ambrożewicz sensors::node::power); 158028f7ebcSEddie James 1592474adfaSEd Tanous getChassisData(sensorAsyncResp); 160028f7ebcSEddie James 161028f7ebcSEddie James // This callback verifies that the power limit is only provided for the 162028f7ebcSEddie James // chassis that implements the Chassis inventory item. This prevents 163028f7ebcSEddie James // things like power supplies providing the chassis power limit 164028f7ebcSEddie James auto chassisHandler = [sensorAsyncResp]( 165271584abSEd Tanous const boost::system::error_code e, 166028f7ebcSEddie James const std::vector<std::string>& 167028f7ebcSEddie James chassisPaths) { 168271584abSEd Tanous if (e) 169028f7ebcSEddie James { 170028f7ebcSEddie James BMCWEB_LOG_ERROR 171271584abSEd Tanous << "Power Limit GetSubTreePaths handler Dbus error " << e; 172028f7ebcSEddie James return; 173028f7ebcSEddie James } 174028f7ebcSEddie James 175028f7ebcSEddie James bool found = false; 176028f7ebcSEddie James for (const std::string& chassis : chassisPaths) 177028f7ebcSEddie James { 178028f7ebcSEddie James size_t len = std::string::npos; 179028f7ebcSEddie James size_t lastPos = chassis.rfind("/"); 180028f7ebcSEddie James if (lastPos == std::string::npos) 181028f7ebcSEddie James { 182028f7ebcSEddie James continue; 183028f7ebcSEddie James } 184028f7ebcSEddie James 185028f7ebcSEddie James if (lastPos == chassis.size() - 1) 186028f7ebcSEddie James { 187028f7ebcSEddie James size_t end = lastPos; 188028f7ebcSEddie James lastPos = chassis.rfind("/", lastPos - 1); 189028f7ebcSEddie James if (lastPos == std::string::npos) 190028f7ebcSEddie James { 191028f7ebcSEddie James continue; 192028f7ebcSEddie James } 193028f7ebcSEddie James 194028f7ebcSEddie James len = end - (lastPos + 1); 195028f7ebcSEddie James } 196028f7ebcSEddie James 197028f7ebcSEddie James std::string interfaceChassisName = 198028f7ebcSEddie James chassis.substr(lastPos + 1, len); 199028f7ebcSEddie James if (!interfaceChassisName.compare(sensorAsyncResp->chassisId)) 200028f7ebcSEddie James { 201028f7ebcSEddie James found = true; 202028f7ebcSEddie James break; 203028f7ebcSEddie James } 204028f7ebcSEddie James } 205028f7ebcSEddie James 206028f7ebcSEddie James if (!found) 207028f7ebcSEddie James { 208028f7ebcSEddie James BMCWEB_LOG_DEBUG << "Power Limit not present for " 209028f7ebcSEddie James << sensorAsyncResp->chassisId; 210028f7ebcSEddie James return; 211028f7ebcSEddie James } 212028f7ebcSEddie James 213028f7ebcSEddie James auto valueHandler = 214028f7ebcSEddie James [sensorAsyncResp]( 215028f7ebcSEddie James const boost::system::error_code ec, 216028f7ebcSEddie James const std::vector<std::pair<std::string, SensorVariant>>& 217028f7ebcSEddie James properties) { 218028f7ebcSEddie James if (ec) 219028f7ebcSEddie James { 220028f7ebcSEddie James messages::internalError(sensorAsyncResp->res); 221028f7ebcSEddie James BMCWEB_LOG_ERROR 222028f7ebcSEddie James << "Power Limit GetAll handler: Dbus error " << ec; 223028f7ebcSEddie James return; 224028f7ebcSEddie James } 225028f7ebcSEddie James 226028f7ebcSEddie James nlohmann::json& tempArray = 2277ab06f49SGunnar Mills sensorAsyncResp->res.jsonValue["PowerControl"]; 228028f7ebcSEddie James 2297ab06f49SGunnar Mills // Put multiple "sensors" into a single PowerControl, 0, so 2307ab06f49SGunnar Mills // only create the first one 231028f7ebcSEddie James if (tempArray.empty()) 232028f7ebcSEddie James { 2337ab06f49SGunnar Mills // Mandatory properties odata.id and MemberId 2347ab06f49SGunnar Mills // A warning without a odata.type 2357ab06f49SGunnar Mills tempArray.push_back( 2367ab06f49SGunnar Mills {{"@odata.type", "#Power.v1_0_0.PowerControl"}, 2377ab06f49SGunnar Mills {"@odata.id", "/redfish/v1/Chassis/" + 2387ab06f49SGunnar Mills sensorAsyncResp->chassisId + 2397ab06f49SGunnar Mills "/Power#/PowerControl/0"}, 2407ab06f49SGunnar Mills {"Name", "Chassis Power Control"}, 2417ab06f49SGunnar Mills {"MemberId", "0"}}); 242028f7ebcSEddie James } 243028f7ebcSEddie James 244028f7ebcSEddie James nlohmann::json& sensorJson = tempArray.back(); 245028f7ebcSEddie James bool enabled = false; 246028f7ebcSEddie James double powerCap = 0.0; 247028f7ebcSEddie James int64_t scale = 0; 248028f7ebcSEddie James 249028f7ebcSEddie James for (const std::pair<std::string, SensorVariant>& property : 250028f7ebcSEddie James properties) 251028f7ebcSEddie James { 252028f7ebcSEddie James if (!property.first.compare("Scale")) 253028f7ebcSEddie James { 254028f7ebcSEddie James const int64_t* i = 2558d78b7a9SPatrick Williams std::get_if<int64_t>(&property.second); 256028f7ebcSEddie James 257028f7ebcSEddie James if (i) 258028f7ebcSEddie James { 259028f7ebcSEddie James scale = *i; 260028f7ebcSEddie James } 261028f7ebcSEddie James } 262028f7ebcSEddie James else if (!property.first.compare("PowerCap")) 263028f7ebcSEddie James { 264028f7ebcSEddie James const double* d = 2658d78b7a9SPatrick Williams std::get_if<double>(&property.second); 266028f7ebcSEddie James const int64_t* i = 2678d78b7a9SPatrick Williams std::get_if<int64_t>(&property.second); 268028f7ebcSEddie James const uint32_t* u = 2698d78b7a9SPatrick Williams std::get_if<uint32_t>(&property.second); 270028f7ebcSEddie James 271028f7ebcSEddie James if (d) 272028f7ebcSEddie James { 273028f7ebcSEddie James powerCap = *d; 274028f7ebcSEddie James } 275028f7ebcSEddie James else if (i) 276028f7ebcSEddie James { 277271584abSEd Tanous powerCap = static_cast<double>(*i); 278028f7ebcSEddie James } 279028f7ebcSEddie James else if (u) 280028f7ebcSEddie James { 281028f7ebcSEddie James powerCap = *u; 282028f7ebcSEddie James } 283028f7ebcSEddie James } 284028f7ebcSEddie James else if (!property.first.compare("PowerCapEnable")) 285028f7ebcSEddie James { 2868d78b7a9SPatrick Williams const bool* b = std::get_if<bool>(&property.second); 287028f7ebcSEddie James 288028f7ebcSEddie James if (b) 289028f7ebcSEddie James { 290028f7ebcSEddie James enabled = *b; 291028f7ebcSEddie James } 292028f7ebcSEddie James } 293028f7ebcSEddie James } 294028f7ebcSEddie James 2957ab06f49SGunnar Mills nlohmann::json& value = 2967ab06f49SGunnar Mills sensorJson["PowerLimit"]["LimitInWatts"]; 297028f7ebcSEddie James 2985a64a6f3SJoshi-Mansi // LimitException is Mandatory attribute as per OCP Baseline 2995a64a6f3SJoshi-Mansi // Profile – v1.0.0, so currently making it "NoAction" 3005a64a6f3SJoshi-Mansi // as default value to make it OCP Compliant. 3015a64a6f3SJoshi-Mansi sensorJson["PowerLimit"]["LimitException"] = "NoAction"; 3025a64a6f3SJoshi-Mansi 303028f7ebcSEddie James if (enabled) 304028f7ebcSEddie James { 305028f7ebcSEddie James // Redfish specification indicates PowerLimit should be 306028f7ebcSEddie James // null if the limit is not enabled. 307028f7ebcSEddie James value = powerCap * std::pow(10, scale); 308028f7ebcSEddie James } 309028f7ebcSEddie James }; 310028f7ebcSEddie James 311028f7ebcSEddie James crow::connections::systemBus->async_method_call( 312028f7ebcSEddie James std::move(valueHandler), "xyz.openbmc_project.Settings", 313028f7ebcSEddie James "/xyz/openbmc_project/control/host0/power_cap", 314028f7ebcSEddie James "org.freedesktop.DBus.Properties", "GetAll", 315028f7ebcSEddie James "xyz.openbmc_project.Control.Power.Cap"); 316028f7ebcSEddie James }; 317028f7ebcSEddie James 318028f7ebcSEddie James crow::connections::systemBus->async_method_call( 319028f7ebcSEddie James std::move(chassisHandler), "xyz.openbmc_project.ObjectMapper", 320028f7ebcSEddie James "/xyz/openbmc_project/object_mapper", 321028f7ebcSEddie James "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", 322271584abSEd Tanous "/xyz/openbmc_project/inventory", 0, 323f857e9aeSAppaRao Puli std::array<const char*, 2>{ 324f857e9aeSAppaRao Puli "xyz.openbmc_project.Inventory.Item.Board", 325028f7ebcSEddie James "xyz.openbmc_project.Inventory.Item.Chassis"}); 3262474adfaSEd Tanous } 327413961deSRichard Marian Thomaiyar void doPatch(crow::Response& res, const crow::Request& req, 328413961deSRichard Marian Thomaiyar const std::vector<std::string>& params) override 329413961deSRichard Marian Thomaiyar { 3304bb3dc34SCarol Wang if (params.size() != 1) 3314bb3dc34SCarol Wang { 3324bb3dc34SCarol Wang messages::internalError(res); 3334bb3dc34SCarol Wang res.end(); 3344bb3dc34SCarol Wang return; 3354bb3dc34SCarol Wang } 3364bb3dc34SCarol Wang 3374bb3dc34SCarol Wang const std::string& chassisName = params[0]; 338a0ec28b6SAdrian Ambrożewicz auto asyncResp = std::make_shared<SensorsAsyncResp>( 339a0ec28b6SAdrian Ambrożewicz res, chassisName, sensors::dbus::types.at(sensors::node::power), 340a0ec28b6SAdrian Ambrożewicz sensors::node::power); 3414bb3dc34SCarol Wang 3424bb3dc34SCarol Wang std::optional<std::vector<nlohmann::json>> voltageCollections; 3434bb3dc34SCarol Wang std::optional<std::vector<nlohmann::json>> powerCtlCollections; 3444bb3dc34SCarol Wang 3454bb3dc34SCarol Wang if (!json_util::readJson(req, asyncResp->res, "PowerControl", 3464bb3dc34SCarol Wang powerCtlCollections, "Voltages", 3474bb3dc34SCarol Wang voltageCollections)) 3484bb3dc34SCarol Wang { 3494bb3dc34SCarol Wang return; 3504bb3dc34SCarol Wang } 3514bb3dc34SCarol Wang 3524bb3dc34SCarol Wang if (powerCtlCollections) 3534bb3dc34SCarol Wang { 3544bb3dc34SCarol Wang setPowerCapOverride(asyncResp, *powerCtlCollections); 3554bb3dc34SCarol Wang } 3564bb3dc34SCarol Wang if (voltageCollections) 3574bb3dc34SCarol Wang { 3584bb3dc34SCarol Wang std::unordered_map<std::string, std::vector<nlohmann::json>> 3594bb3dc34SCarol Wang allCollections; 3604bb3dc34SCarol Wang allCollections.emplace("Voltages", *std::move(voltageCollections)); 361397fd61fSjayaprakash Mutyala checkAndDoSensorsOverride(asyncResp, allCollections); 3624bb3dc34SCarol Wang } 363413961deSRichard Marian Thomaiyar } 3642474adfaSEd Tanous }; 3652474adfaSEd Tanous 3662474adfaSEd Tanous } // namespace redfish 367