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 193ccb3adbSEd Tanous #include "app.hpp" 207a1dbc48SGeorge Liu #include "dbus_utility.hpp" 213ccb3adbSEd Tanous #include "query.hpp" 223ccb3adbSEd Tanous #include "registries/privilege_registry.hpp" 232474adfaSEd Tanous #include "sensors.hpp" 240d7702c0SZhenwei Chen #include "utils/chassis_utils.hpp" 252474adfaSEd Tanous 26d1bde9e5SKrzysztof Grobelny #include <sdbusplus/asio/property.hpp> 277e860f15SJohn Edward Broadbent 287a1dbc48SGeorge Liu #include <array> 297a1dbc48SGeorge Liu #include <string_view> 307a1dbc48SGeorge Liu 312474adfaSEd Tanous namespace redfish 322474adfaSEd Tanous { 334f48d5f6SEd Tanous inline void setPowerCapOverride( 348d1b46d7Szhanghch05 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, 354bb3dc34SCarol Wang std::vector<nlohmann::json>& powerControlCollections) 364bb3dc34SCarol Wang { 37002d39b4SEd Tanous auto getChassisPath = 38002d39b4SEd Tanous [sensorsAsyncResp, powerControlCollections]( 39002d39b4SEd Tanous const std::optional<std::string>& chassisPath) mutable { 404bb3dc34SCarol Wang if (!chassisPath) 414bb3dc34SCarol Wang { 42a7405d5fSGunnar Mills BMCWEB_LOG_WARNING << "Don't find valid chassis path "; 438d1b46d7Szhanghch05 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res, 440fda0f12SGeorge Liu "Chassis", sensorsAsyncResp->chassisId); 454bb3dc34SCarol Wang return; 464bb3dc34SCarol Wang } 474bb3dc34SCarol Wang 484bb3dc34SCarol Wang if (powerControlCollections.size() != 1) 494bb3dc34SCarol Wang { 50a7405d5fSGunnar Mills BMCWEB_LOG_WARNING << "Don't support multiple hosts at present "; 518d1b46d7Szhanghch05 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res, 528d1b46d7Szhanghch05 "Power", "PowerControl"); 534bb3dc34SCarol Wang return; 544bb3dc34SCarol Wang } 554bb3dc34SCarol Wang 564bb3dc34SCarol Wang auto& item = powerControlCollections[0]; 574bb3dc34SCarol Wang 584bb3dc34SCarol Wang std::optional<nlohmann::json> powerLimit; 598d1b46d7Szhanghch05 if (!json_util::readJson(item, sensorsAsyncResp->asyncResp->res, 608d1b46d7Szhanghch05 "PowerLimit", powerLimit)) 614bb3dc34SCarol Wang { 624bb3dc34SCarol Wang return; 634bb3dc34SCarol Wang } 644bb3dc34SCarol Wang if (!powerLimit) 654bb3dc34SCarol Wang { 664bb3dc34SCarol Wang return; 674bb3dc34SCarol Wang } 684bb3dc34SCarol Wang std::optional<uint32_t> value; 690fda0f12SGeorge Liu if (!json_util::readJson(*powerLimit, sensorsAsyncResp->asyncResp->res, 704bb3dc34SCarol Wang "LimitInWatts", value)) 714bb3dc34SCarol Wang { 724bb3dc34SCarol Wang return; 734bb3dc34SCarol Wang } 744bb3dc34SCarol Wang if (!value) 754bb3dc34SCarol Wang { 764bb3dc34SCarol Wang return; 774bb3dc34SCarol Wang } 781e1e598dSJonathan Doman sdbusplus::asio::getProperty<bool>( 791e1e598dSJonathan Doman *crow::connections::systemBus, "xyz.openbmc_project.Settings", 801e1e598dSJonathan Doman "/xyz/openbmc_project/control/host0/power_cap", 811e1e598dSJonathan Doman "xyz.openbmc_project.Control.Power.Cap", "PowerCapEnable", 825e7e2dc5SEd Tanous [value, sensorsAsyncResp](const boost::system::error_code& ec, 831e1e598dSJonathan Doman bool powerCapEnable) { 844bb3dc34SCarol Wang if (ec) 854bb3dc34SCarol Wang { 868d1b46d7Szhanghch05 messages::internalError(sensorsAsyncResp->asyncResp->res); 87002d39b4SEd Tanous BMCWEB_LOG_ERROR << "powerCapEnable Get handler: Dbus error " 88002d39b4SEd Tanous << ec; 894bb3dc34SCarol Wang return; 904bb3dc34SCarol Wang } 911e1e598dSJonathan Doman if (!powerCapEnable) 924bb3dc34SCarol Wang { 934bb3dc34SCarol Wang messages::actionNotSupported( 948d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res, 950fda0f12SGeorge Liu "Setting LimitInWatts when PowerLimit 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( 1015e7e2dc5SEd Tanous [sensorsAsyncResp](const boost::system::error_code& ec2) { 10223a21a1cSEd Tanous if (ec2) 1034bb3dc34SCarol Wang { 104002d39b4SEd Tanous BMCWEB_LOG_DEBUG << "Power Limit Set: Dbus error: " << ec2; 105002d39b4SEd Tanous messages::internalError(sensorsAsyncResp->asyncResp->res); 1064bb3dc34SCarol Wang return; 1074bb3dc34SCarol Wang } 1088d1b46d7Szhanghch05 sensorsAsyncResp->asyncResp->res.result( 1094bb3dc34SCarol Wang boost::beast::http::status::no_content); 1104bb3dc34SCarol Wang }, 1114bb3dc34SCarol Wang "xyz.openbmc_project.Settings", 1124bb3dc34SCarol Wang "/xyz/openbmc_project/control/host0/power_cap", 1134bb3dc34SCarol Wang "org.freedesktop.DBus.Properties", "Set", 1144bb3dc34SCarol Wang "xyz.openbmc_project.Control.Power.Cap", "PowerCap", 1151e1e598dSJonathan Doman std::variant<uint32_t>(*value)); 1161e1e598dSJonathan Doman }); 1174bb3dc34SCarol Wang }; 1180d7702c0SZhenwei Chen redfish::chassis_utils::getValidChassisPath(sensorsAsyncResp->asyncResp, 1190d7702c0SZhenwei Chen sensorsAsyncResp->chassisId, 1200d7702c0SZhenwei Chen std::move(getChassisPath)); 1214bb3dc34SCarol Wang } 1227e860f15SJohn Edward Broadbent inline void requestRoutesPower(App& app) 1232474adfaSEd Tanous { 1247e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/") 125ed398213SEd Tanous .privileges(redfish::privileges::getPower) 126002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 127002d39b4SEd Tanous [&app](const crow::Request& req, 12845ca1b86SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1297e860f15SJohn Edward Broadbent const std::string& chassisName) { 1303ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 13145ca1b86SEd Tanous { 13245ca1b86SEd Tanous return; 13345ca1b86SEd Tanous } 134168e20c1SEd Tanous asyncResp->res.jsonValue["PowerControl"] = nlohmann::json::array(); 135c5d03ff4SJennifer Lee 1362474adfaSEd Tanous auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>( 13702da7c5aSEd Tanous asyncResp, chassisName, sensors::dbus::powerPaths, 138a0ec28b6SAdrian Ambrożewicz sensors::node::power); 139028f7ebcSEddie James 1402474adfaSEd Tanous getChassisData(sensorAsyncResp); 141028f7ebcSEddie James 1427e860f15SJohn Edward Broadbent // This callback verifies that the power limit is only provided 1437e860f15SJohn Edward Broadbent // for the chassis that implements the Chassis inventory item. 1447e860f15SJohn Edward Broadbent // This prevents things like power supplies providing the 1457e860f15SJohn Edward Broadbent // chassis power limit 146b9d36b47SEd Tanous 147b9d36b47SEd Tanous using Mapper = dbus::utility::MapperGetSubTreePathsResponse; 148002d39b4SEd Tanous auto chassisHandler = 1497a1dbc48SGeorge Liu [sensorAsyncResp](const boost::system::error_code& e, 150b9d36b47SEd Tanous const Mapper& chassisPaths) { 151271584abSEd Tanous if (e) 152028f7ebcSEddie James { 153028f7ebcSEddie James BMCWEB_LOG_ERROR 154002d39b4SEd Tanous << "Power Limit GetSubTreePaths handler Dbus error " << e; 155028f7ebcSEddie James return; 156028f7ebcSEddie James } 157028f7ebcSEddie James 158028f7ebcSEddie James bool found = false; 159028f7ebcSEddie James for (const std::string& chassis : chassisPaths) 160028f7ebcSEddie James { 161028f7ebcSEddie James size_t len = std::string::npos; 162f23b7296SEd Tanous size_t lastPos = chassis.rfind('/'); 163028f7ebcSEddie James if (lastPos == std::string::npos) 164028f7ebcSEddie James { 165028f7ebcSEddie James continue; 166028f7ebcSEddie James } 167028f7ebcSEddie James 168028f7ebcSEddie James if (lastPos == chassis.size() - 1) 169028f7ebcSEddie James { 170028f7ebcSEddie James size_t end = lastPos; 171f23b7296SEd Tanous lastPos = chassis.rfind('/', lastPos - 1); 172028f7ebcSEddie James if (lastPos == std::string::npos) 173028f7ebcSEddie James { 174028f7ebcSEddie James continue; 175028f7ebcSEddie James } 176028f7ebcSEddie James 177028f7ebcSEddie James len = end - (lastPos + 1); 178028f7ebcSEddie James } 179028f7ebcSEddie James 18089492a15SPatrick Williams std::string interfaceChassisName = chassis.substr(lastPos + 1, 18189492a15SPatrick Williams len); 18255f79e6fSEd Tanous if (interfaceChassisName == sensorAsyncResp->chassisId) 183028f7ebcSEddie James { 184028f7ebcSEddie James found = true; 185028f7ebcSEddie James break; 186028f7ebcSEddie James } 187028f7ebcSEddie James } 188028f7ebcSEddie James 189028f7ebcSEddie James if (!found) 190028f7ebcSEddie James { 191028f7ebcSEddie James BMCWEB_LOG_DEBUG << "Power Limit not present for " 192028f7ebcSEddie James << sensorAsyncResp->chassisId; 193028f7ebcSEddie James return; 194028f7ebcSEddie James } 195028f7ebcSEddie James 196168e20c1SEd Tanous auto valueHandler = 197168e20c1SEd Tanous [sensorAsyncResp]( 1985e7e2dc5SEd Tanous const boost::system::error_code& ec, 199b9d36b47SEd Tanous const dbus::utility::DBusPropertiesMap& properties) { 200028f7ebcSEddie James if (ec) 201028f7ebcSEddie James { 202002d39b4SEd Tanous messages::internalError(sensorAsyncResp->asyncResp->res); 203028f7ebcSEddie James BMCWEB_LOG_ERROR 204002d39b4SEd Tanous << "Power Limit GetAll handler: Dbus error " << ec; 205028f7ebcSEddie James return; 206028f7ebcSEddie James } 207028f7ebcSEddie James 2087e860f15SJohn Edward Broadbent nlohmann::json& tempArray = 209002d39b4SEd Tanous sensorAsyncResp->asyncResp->res.jsonValue["PowerControl"]; 210028f7ebcSEddie James 2117e860f15SJohn Edward Broadbent // Put multiple "sensors" into a single PowerControl, 0, 2127e860f15SJohn Edward Broadbent // so only create the first one 213028f7ebcSEddie James if (tempArray.empty()) 214028f7ebcSEddie James { 2157ab06f49SGunnar Mills // Mandatory properties odata.id and MemberId 2167ab06f49SGunnar Mills // A warning without a odata.type 2171476687dSEd Tanous nlohmann::json::object_t powerControl; 218002d39b4SEd Tanous powerControl["@odata.type"] = "#Power.v1_0_0.PowerControl"; 219002d39b4SEd Tanous powerControl["@odata.id"] = "/redfish/v1/Chassis/" + 2207ab06f49SGunnar Mills sensorAsyncResp->chassisId + 2211476687dSEd Tanous "/Power#/PowerControl/0"; 2221476687dSEd Tanous powerControl["Name"] = "Chassis Power Control"; 2231476687dSEd Tanous powerControl["MemberId"] = "0"; 224*b2ba3072SPatrick Williams tempArray.emplace_back(std::move(powerControl)); 225028f7ebcSEddie James } 226028f7ebcSEddie James 227028f7ebcSEddie James nlohmann::json& sensorJson = tempArray.back(); 228028f7ebcSEddie James bool enabled = false; 229028f7ebcSEddie James double powerCap = 0.0; 230028f7ebcSEddie James int64_t scale = 0; 231028f7ebcSEddie James 232168e20c1SEd Tanous for (const std::pair<std::string, 233002d39b4SEd Tanous dbus::utility::DbusVariantType>& property : 234002d39b4SEd Tanous properties) 235028f7ebcSEddie James { 23655f79e6fSEd Tanous if (property.first == "Scale") 237028f7ebcSEddie James { 238028f7ebcSEddie James const int64_t* i = 2398d78b7a9SPatrick Williams std::get_if<int64_t>(&property.second); 240028f7ebcSEddie James 241e662eae8SEd Tanous if (i != nullptr) 242028f7ebcSEddie James { 243028f7ebcSEddie James scale = *i; 244028f7ebcSEddie James } 245028f7ebcSEddie James } 24655f79e6fSEd Tanous else if (property.first == "PowerCap") 247028f7ebcSEddie James { 248002d39b4SEd Tanous const double* d = std::get_if<double>(&property.second); 249028f7ebcSEddie James const int64_t* i = 2508d78b7a9SPatrick Williams std::get_if<int64_t>(&property.second); 251028f7ebcSEddie James const uint32_t* u = 2528d78b7a9SPatrick Williams std::get_if<uint32_t>(&property.second); 253028f7ebcSEddie James 254e662eae8SEd Tanous if (d != nullptr) 255028f7ebcSEddie James { 256028f7ebcSEddie James powerCap = *d; 257028f7ebcSEddie James } 258e662eae8SEd Tanous else if (i != nullptr) 259028f7ebcSEddie James { 260271584abSEd Tanous powerCap = static_cast<double>(*i); 261028f7ebcSEddie James } 262e662eae8SEd Tanous else if (u != nullptr) 263028f7ebcSEddie James { 264028f7ebcSEddie James powerCap = *u; 265028f7ebcSEddie James } 266028f7ebcSEddie James } 26755f79e6fSEd Tanous else if (property.first == "PowerCapEnable") 268028f7ebcSEddie James { 269002d39b4SEd Tanous const bool* b = std::get_if<bool>(&property.second); 270028f7ebcSEddie James 271e662eae8SEd Tanous if (b != nullptr) 272028f7ebcSEddie James { 273028f7ebcSEddie James enabled = *b; 274028f7ebcSEddie James } 275028f7ebcSEddie James } 276028f7ebcSEddie James } 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. 287d4413c5bSGeorge Liu sensorJson["PowerLimit"]["LimitInWatts"] = 288d4413c5bSGeorge Liu powerCap * std::pow(10, scale); 289028f7ebcSEddie James } 290028f7ebcSEddie James }; 291028f7ebcSEddie James 292d1bde9e5SKrzysztof Grobelny sdbusplus::asio::getAllProperties( 293d1bde9e5SKrzysztof Grobelny *crow::connections::systemBus, "xyz.openbmc_project.Settings", 294028f7ebcSEddie James "/xyz/openbmc_project/control/host0/power_cap", 295d1bde9e5SKrzysztof Grobelny "xyz.openbmc_project.Control.Power.Cap", 296d1bde9e5SKrzysztof Grobelny std::move(valueHandler)); 297028f7ebcSEddie James }; 298028f7ebcSEddie James 2997a1dbc48SGeorge Liu constexpr std::array<std::string_view, 2> interfaces = { 300f857e9aeSAppaRao Puli "xyz.openbmc_project.Inventory.Item.Board", 3017a1dbc48SGeorge Liu "xyz.openbmc_project.Inventory.Item.Chassis"}; 3027a1dbc48SGeorge Liu 3037a1dbc48SGeorge Liu dbus::utility::getSubTreePaths("/xyz/openbmc_project/inventory", 0, 3047a1dbc48SGeorge Liu interfaces, std::move(chassisHandler)); 3057e860f15SJohn Edward Broadbent }); 3064bb3dc34SCarol Wang 3077e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/") 308ed398213SEd Tanous .privileges(redfish::privileges::patchPower) 3097e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::patch)( 31045ca1b86SEd Tanous [&app](const crow::Request& req, 3117e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 3127e860f15SJohn Edward Broadbent const std::string& chassisName) { 3133ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 31445ca1b86SEd Tanous { 31545ca1b86SEd Tanous return; 31645ca1b86SEd Tanous } 3178d1b46d7Szhanghch05 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>( 31802da7c5aSEd Tanous asyncResp, chassisName, sensors::dbus::powerPaths, 319a0ec28b6SAdrian Ambrożewicz sensors::node::power); 3204bb3dc34SCarol Wang 3214bb3dc34SCarol Wang std::optional<std::vector<nlohmann::json>> voltageCollections; 3224bb3dc34SCarol Wang std::optional<std::vector<nlohmann::json>> powerCtlCollections; 3234bb3dc34SCarol Wang 324002d39b4SEd Tanous if (!json_util::readJsonPatch(req, sensorAsyncResp->asyncResp->res, 325002d39b4SEd Tanous "PowerControl", powerCtlCollections, 326002d39b4SEd Tanous "Voltages", voltageCollections)) 3274bb3dc34SCarol Wang { 3284bb3dc34SCarol Wang return; 3294bb3dc34SCarol Wang } 3304bb3dc34SCarol Wang 3314bb3dc34SCarol Wang if (powerCtlCollections) 3324bb3dc34SCarol Wang { 3338d1b46d7Szhanghch05 setPowerCapOverride(sensorAsyncResp, *powerCtlCollections); 3344bb3dc34SCarol Wang } 3354bb3dc34SCarol Wang if (voltageCollections) 3364bb3dc34SCarol Wang { 3374bb3dc34SCarol Wang std::unordered_map<std::string, std::vector<nlohmann::json>> 3384bb3dc34SCarol Wang allCollections; 339002d39b4SEd Tanous allCollections.emplace("Voltages", *std::move(voltageCollections)); 34080ac4024SBruce Lee setSensorsOverride(sensorAsyncResp, allCollections); 3414bb3dc34SCarol Wang } 3427e860f15SJohn Edward Broadbent }); 343413961deSRichard Marian Thomaiyar } 3442474adfaSEd Tanous 3452474adfaSEd Tanous } // namespace redfish 346