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: 282474adfaSEd Tanous Power(CrowApp& 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: 4185e1424fSEd Tanous std::vector<const char*> typeList = {"/xyz/openbmc_project/sensors/voltage", 42413961deSRichard Marian Thomaiyar "/xyz/openbmc_project/sensors/power"}; 434bb3dc34SCarol Wang void setPowerCapOverride( 444bb3dc34SCarol Wang std::shared_ptr<SensorsAsyncResp> asyncResp, 454bb3dc34SCarol Wang std::vector<nlohmann::json>& powerControlCollections) 464bb3dc34SCarol Wang { 474bb3dc34SCarol Wang auto getChassisPath = 484bb3dc34SCarol Wang [asyncResp, powerControlCollections]( 494bb3dc34SCarol Wang const std::optional<std::string>& chassisPath) mutable { 504bb3dc34SCarol Wang if (!chassisPath) 514bb3dc34SCarol Wang { 524bb3dc34SCarol Wang BMCWEB_LOG_ERROR << "Don't find valid chassis path "; 534bb3dc34SCarol Wang messages::resourceNotFound(asyncResp->res, "Chassis", 544bb3dc34SCarol Wang asyncResp->chassisId); 554bb3dc34SCarol Wang return; 564bb3dc34SCarol Wang } 574bb3dc34SCarol Wang 584bb3dc34SCarol Wang if (powerControlCollections.size() != 1) 594bb3dc34SCarol Wang { 604bb3dc34SCarol Wang BMCWEB_LOG_ERROR 614bb3dc34SCarol Wang << "Don't support multiple hosts at present "; 624bb3dc34SCarol Wang messages::resourceNotFound(asyncResp->res, "Power", 634bb3dc34SCarol Wang "PowerControl"); 644bb3dc34SCarol Wang return; 654bb3dc34SCarol Wang } 664bb3dc34SCarol Wang 674bb3dc34SCarol Wang auto& item = powerControlCollections[0]; 684bb3dc34SCarol Wang 694bb3dc34SCarol Wang std::optional<nlohmann::json> powerLimit; 704bb3dc34SCarol Wang if (!json_util::readJson(item, asyncResp->res, "PowerLimit", 714bb3dc34SCarol Wang powerLimit)) 724bb3dc34SCarol Wang { 734bb3dc34SCarol Wang return; 744bb3dc34SCarol Wang } 754bb3dc34SCarol Wang if (!powerLimit) 764bb3dc34SCarol Wang { 774bb3dc34SCarol Wang return; 784bb3dc34SCarol Wang } 794bb3dc34SCarol Wang std::optional<uint32_t> value; 804bb3dc34SCarol Wang if (!json_util::readJson(*powerLimit, asyncResp->res, 814bb3dc34SCarol Wang "LimitInWatts", value)) 824bb3dc34SCarol Wang { 834bb3dc34SCarol Wang return; 844bb3dc34SCarol Wang } 854bb3dc34SCarol Wang if (!value) 864bb3dc34SCarol Wang { 874bb3dc34SCarol Wang return; 884bb3dc34SCarol Wang } 894bb3dc34SCarol Wang auto valueHandler = [value, asyncResp]( 904bb3dc34SCarol Wang const boost::system::error_code ec, 914bb3dc34SCarol Wang const SensorVariant& powerCapEnable) { 924bb3dc34SCarol Wang if (ec) 934bb3dc34SCarol Wang { 944bb3dc34SCarol Wang messages::internalError(asyncResp->res); 954bb3dc34SCarol Wang BMCWEB_LOG_ERROR 964bb3dc34SCarol Wang << "powerCapEnable Get handler: Dbus error " << ec; 974bb3dc34SCarol Wang return; 984bb3dc34SCarol Wang } 994bb3dc34SCarol Wang // Check PowerCapEnable 100*8d78b7a9SPatrick Williams const bool* b = std::get_if<bool>(&powerCapEnable); 1014bb3dc34SCarol Wang if (b == nullptr) 1024bb3dc34SCarol Wang { 1034bb3dc34SCarol Wang messages::internalError(asyncResp->res); 1044bb3dc34SCarol Wang BMCWEB_LOG_ERROR 1054bb3dc34SCarol Wang << "Fail to get PowerCapEnable status "; 1064bb3dc34SCarol Wang return; 1074bb3dc34SCarol Wang } 1084bb3dc34SCarol Wang if (!(*b)) 1094bb3dc34SCarol Wang { 1104bb3dc34SCarol Wang messages::actionNotSupported( 1114bb3dc34SCarol Wang asyncResp->res, 1124bb3dc34SCarol Wang "Setting LimitInWatts when PowerLimit " 1134bb3dc34SCarol Wang "feature is disabled"); 1144bb3dc34SCarol Wang BMCWEB_LOG_ERROR << "PowerLimit feature is disabled "; 1154bb3dc34SCarol Wang return; 1164bb3dc34SCarol Wang } 1174bb3dc34SCarol Wang 1184bb3dc34SCarol Wang crow::connections::systemBus->async_method_call( 1194bb3dc34SCarol Wang [asyncResp](const boost::system::error_code ec) { 1204bb3dc34SCarol Wang if (ec) 1214bb3dc34SCarol Wang { 1224bb3dc34SCarol Wang BMCWEB_LOG_DEBUG 1234bb3dc34SCarol Wang << "Power Limit Set: Dbus error: " << ec; 1244bb3dc34SCarol Wang messages::internalError(asyncResp->res); 1254bb3dc34SCarol Wang return; 1264bb3dc34SCarol Wang } 1274bb3dc34SCarol Wang asyncResp->res.result( 1284bb3dc34SCarol Wang boost::beast::http::status::no_content); 1294bb3dc34SCarol Wang }, 1304bb3dc34SCarol Wang "xyz.openbmc_project.Settings", 1314bb3dc34SCarol Wang "/xyz/openbmc_project/control/host0/power_cap", 1324bb3dc34SCarol Wang "org.freedesktop.DBus.Properties", "Set", 1334bb3dc34SCarol Wang "xyz.openbmc_project.Control.Power.Cap", "PowerCap", 1344bb3dc34SCarol Wang sdbusplus::message::variant<uint32_t>(*value)); 1354bb3dc34SCarol Wang }; 1364bb3dc34SCarol Wang crow::connections::systemBus->async_method_call( 1374bb3dc34SCarol Wang std::move(valueHandler), "xyz.openbmc_project.Settings", 1384bb3dc34SCarol Wang "/xyz/openbmc_project/control/host0/power_cap", 1394bb3dc34SCarol Wang "org.freedesktop.DBus.Properties", "Get", 1404bb3dc34SCarol Wang "xyz.openbmc_project.Control.Power.Cap", "PowerCapEnable"); 1414bb3dc34SCarol Wang }; 1424bb3dc34SCarol Wang getValidChassisPath(asyncResp, std::move(getChassisPath)); 1434bb3dc34SCarol Wang } 1442474adfaSEd Tanous void doGet(crow::Response& res, const crow::Request& req, 1452474adfaSEd Tanous const std::vector<std::string>& params) override 1462474adfaSEd Tanous { 1472474adfaSEd Tanous if (params.size() != 1) 1482474adfaSEd Tanous { 1492474adfaSEd Tanous res.result(boost::beast::http::status::internal_server_error); 1502474adfaSEd Tanous res.end(); 1512474adfaSEd Tanous return; 1522474adfaSEd Tanous } 1532474adfaSEd Tanous const std::string& chassis_name = params[0]; 1542474adfaSEd Tanous 155c5d03ff4SJennifer Lee res.jsonValue["PowerControl"] = nlohmann::json::array(); 156c5d03ff4SJennifer Lee 1572474adfaSEd Tanous auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>( 158413961deSRichard Marian Thomaiyar res, chassis_name, typeList, "Power"); 159028f7ebcSEddie James 1602474adfaSEd Tanous getChassisData(sensorAsyncResp); 161028f7ebcSEddie James 162028f7ebcSEddie James // This callback verifies that the power limit is only provided for the 163028f7ebcSEddie James // chassis that implements the Chassis inventory item. This prevents 164028f7ebcSEddie James // things like power supplies providing the chassis power limit 165028f7ebcSEddie James auto chassisHandler = [sensorAsyncResp]( 166271584abSEd Tanous const boost::system::error_code e, 167028f7ebcSEddie James const std::vector<std::string>& 168028f7ebcSEddie James chassisPaths) { 169271584abSEd Tanous if (e) 170028f7ebcSEddie James { 171028f7ebcSEddie James BMCWEB_LOG_ERROR 172271584abSEd Tanous << "Power Limit GetSubTreePaths handler Dbus error " << e; 173028f7ebcSEddie James return; 174028f7ebcSEddie James } 175028f7ebcSEddie James 176028f7ebcSEddie James bool found = false; 177028f7ebcSEddie James for (const std::string& chassis : chassisPaths) 178028f7ebcSEddie James { 179028f7ebcSEddie James size_t len = std::string::npos; 180028f7ebcSEddie James size_t lastPos = chassis.rfind("/"); 181028f7ebcSEddie James if (lastPos == std::string::npos) 182028f7ebcSEddie James { 183028f7ebcSEddie James continue; 184028f7ebcSEddie James } 185028f7ebcSEddie James 186028f7ebcSEddie James if (lastPos == chassis.size() - 1) 187028f7ebcSEddie James { 188028f7ebcSEddie James size_t end = lastPos; 189028f7ebcSEddie James lastPos = chassis.rfind("/", lastPos - 1); 190028f7ebcSEddie James if (lastPos == std::string::npos) 191028f7ebcSEddie James { 192028f7ebcSEddie James continue; 193028f7ebcSEddie James } 194028f7ebcSEddie James 195028f7ebcSEddie James len = end - (lastPos + 1); 196028f7ebcSEddie James } 197028f7ebcSEddie James 198028f7ebcSEddie James std::string interfaceChassisName = 199028f7ebcSEddie James chassis.substr(lastPos + 1, len); 200028f7ebcSEddie James if (!interfaceChassisName.compare(sensorAsyncResp->chassisId)) 201028f7ebcSEddie James { 202028f7ebcSEddie James found = true; 203028f7ebcSEddie James break; 204028f7ebcSEddie James } 205028f7ebcSEddie James } 206028f7ebcSEddie James 207028f7ebcSEddie James if (!found) 208028f7ebcSEddie James { 209028f7ebcSEddie James BMCWEB_LOG_DEBUG << "Power Limit not present for " 210028f7ebcSEddie James << sensorAsyncResp->chassisId; 211028f7ebcSEddie James return; 212028f7ebcSEddie James } 213028f7ebcSEddie James 214028f7ebcSEddie James auto valueHandler = 215028f7ebcSEddie James [sensorAsyncResp]( 216028f7ebcSEddie James const boost::system::error_code ec, 217028f7ebcSEddie James const std::vector<std::pair<std::string, SensorVariant>>& 218028f7ebcSEddie James properties) { 219028f7ebcSEddie James if (ec) 220028f7ebcSEddie James { 221028f7ebcSEddie James messages::internalError(sensorAsyncResp->res); 222028f7ebcSEddie James BMCWEB_LOG_ERROR 223028f7ebcSEddie James << "Power Limit GetAll handler: Dbus error " << ec; 224028f7ebcSEddie James return; 225028f7ebcSEddie James } 226028f7ebcSEddie James 227028f7ebcSEddie James nlohmann::json& tempArray = 2287ab06f49SGunnar Mills sensorAsyncResp->res.jsonValue["PowerControl"]; 229028f7ebcSEddie James 2307ab06f49SGunnar Mills // Put multiple "sensors" into a single PowerControl, 0, so 2317ab06f49SGunnar Mills // only create the first one 232028f7ebcSEddie James if (tempArray.empty()) 233028f7ebcSEddie James { 2347ab06f49SGunnar Mills // Mandatory properties odata.id and MemberId 2357ab06f49SGunnar Mills // A warning without a odata.type 2367ab06f49SGunnar Mills tempArray.push_back( 2377ab06f49SGunnar Mills {{"@odata.type", "#Power.v1_0_0.PowerControl"}, 2387ab06f49SGunnar Mills {"@odata.id", "/redfish/v1/Chassis/" + 2397ab06f49SGunnar Mills sensorAsyncResp->chassisId + 2407ab06f49SGunnar Mills "/Power#/PowerControl/0"}, 2417ab06f49SGunnar Mills {"Name", "Chassis Power Control"}, 2427ab06f49SGunnar Mills {"MemberId", "0"}}); 243028f7ebcSEddie James } 244028f7ebcSEddie James 245028f7ebcSEddie James nlohmann::json& sensorJson = tempArray.back(); 246028f7ebcSEddie James bool enabled = false; 247028f7ebcSEddie James double powerCap = 0.0; 248028f7ebcSEddie James int64_t scale = 0; 249028f7ebcSEddie James 250028f7ebcSEddie James for (const std::pair<std::string, SensorVariant>& property : 251028f7ebcSEddie James properties) 252028f7ebcSEddie James { 253028f7ebcSEddie James if (!property.first.compare("Scale")) 254028f7ebcSEddie James { 255028f7ebcSEddie James const int64_t* i = 256*8d78b7a9SPatrick Williams std::get_if<int64_t>(&property.second); 257028f7ebcSEddie James 258028f7ebcSEddie James if (i) 259028f7ebcSEddie James { 260028f7ebcSEddie James scale = *i; 261028f7ebcSEddie James } 262028f7ebcSEddie James } 263028f7ebcSEddie James else if (!property.first.compare("PowerCap")) 264028f7ebcSEddie James { 265028f7ebcSEddie James const double* d = 266*8d78b7a9SPatrick Williams std::get_if<double>(&property.second); 267028f7ebcSEddie James const int64_t* i = 268*8d78b7a9SPatrick Williams std::get_if<int64_t>(&property.second); 269028f7ebcSEddie James const uint32_t* u = 270*8d78b7a9SPatrick Williams std::get_if<uint32_t>(&property.second); 271028f7ebcSEddie James 272028f7ebcSEddie James if (d) 273028f7ebcSEddie James { 274028f7ebcSEddie James powerCap = *d; 275028f7ebcSEddie James } 276028f7ebcSEddie James else if (i) 277028f7ebcSEddie James { 278271584abSEd Tanous powerCap = static_cast<double>(*i); 279028f7ebcSEddie James } 280028f7ebcSEddie James else if (u) 281028f7ebcSEddie James { 282028f7ebcSEddie James powerCap = *u; 283028f7ebcSEddie James } 284028f7ebcSEddie James } 285028f7ebcSEddie James else if (!property.first.compare("PowerCapEnable")) 286028f7ebcSEddie James { 287*8d78b7a9SPatrick Williams const bool* b = std::get_if<bool>(&property.second); 288028f7ebcSEddie James 289028f7ebcSEddie James if (b) 290028f7ebcSEddie James { 291028f7ebcSEddie James enabled = *b; 292028f7ebcSEddie James } 293028f7ebcSEddie James } 294028f7ebcSEddie James } 295028f7ebcSEddie James 2967ab06f49SGunnar Mills nlohmann::json& value = 2977ab06f49SGunnar Mills sensorJson["PowerLimit"]["LimitInWatts"]; 298028f7ebcSEddie James 2995a64a6f3SJoshi-Mansi // LimitException is Mandatory attribute as per OCP Baseline 3005a64a6f3SJoshi-Mansi // Profile – v1.0.0, so currently making it "NoAction" 3015a64a6f3SJoshi-Mansi // as default value to make it OCP Compliant. 3025a64a6f3SJoshi-Mansi sensorJson["PowerLimit"]["LimitException"] = "NoAction"; 3035a64a6f3SJoshi-Mansi 304028f7ebcSEddie James if (enabled) 305028f7ebcSEddie James { 306028f7ebcSEddie James // Redfish specification indicates PowerLimit should be 307028f7ebcSEddie James // null if the limit is not enabled. 308028f7ebcSEddie James value = powerCap * std::pow(10, scale); 309028f7ebcSEddie James } 310028f7ebcSEddie James }; 311028f7ebcSEddie James 312028f7ebcSEddie James crow::connections::systemBus->async_method_call( 313028f7ebcSEddie James std::move(valueHandler), "xyz.openbmc_project.Settings", 314028f7ebcSEddie James "/xyz/openbmc_project/control/host0/power_cap", 315028f7ebcSEddie James "org.freedesktop.DBus.Properties", "GetAll", 316028f7ebcSEddie James "xyz.openbmc_project.Control.Power.Cap"); 317028f7ebcSEddie James }; 318028f7ebcSEddie James 319028f7ebcSEddie James crow::connections::systemBus->async_method_call( 320028f7ebcSEddie James std::move(chassisHandler), "xyz.openbmc_project.ObjectMapper", 321028f7ebcSEddie James "/xyz/openbmc_project/object_mapper", 322028f7ebcSEddie James "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", 323271584abSEd Tanous "/xyz/openbmc_project/inventory", 0, 324f857e9aeSAppaRao Puli std::array<const char*, 2>{ 325f857e9aeSAppaRao Puli "xyz.openbmc_project.Inventory.Item.Board", 326028f7ebcSEddie James "xyz.openbmc_project.Inventory.Item.Chassis"}); 3272474adfaSEd Tanous } 328413961deSRichard Marian Thomaiyar void doPatch(crow::Response& res, const crow::Request& req, 329413961deSRichard Marian Thomaiyar const std::vector<std::string>& params) override 330413961deSRichard Marian Thomaiyar { 3314bb3dc34SCarol Wang if (params.size() != 1) 3324bb3dc34SCarol Wang { 3334bb3dc34SCarol Wang messages::internalError(res); 3344bb3dc34SCarol Wang res.end(); 3354bb3dc34SCarol Wang return; 3364bb3dc34SCarol Wang } 3374bb3dc34SCarol Wang 3384bb3dc34SCarol Wang const std::string& chassisName = params[0]; 3394bb3dc34SCarol Wang auto asyncResp = std::make_shared<SensorsAsyncResp>(res, chassisName, 3404bb3dc34SCarol Wang typeList, "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