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 19*3ccb3adbSEd Tanous #include "app.hpp" 207a1dbc48SGeorge Liu #include "dbus_utility.hpp" 21*3ccb3adbSEd Tanous #include "query.hpp" 22*3ccb3adbSEd 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 { 424bb3dc34SCarol Wang BMCWEB_LOG_ERROR << "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 { 508d1b46d7Szhanghch05 BMCWEB_LOG_ERROR << "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", 821e1e598dSJonathan Doman [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( 1018d1b46d7Szhanghch05 [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 { 1242474adfaSEd Tanous 1257e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/") 126ed398213SEd Tanous .privileges(redfish::privileges::getPower) 127002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 128002d39b4SEd Tanous [&app](const crow::Request& req, 12945ca1b86SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1307e860f15SJohn Edward Broadbent const std::string& chassisName) { 1313ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 13245ca1b86SEd Tanous { 13345ca1b86SEd Tanous return; 13445ca1b86SEd Tanous } 135168e20c1SEd Tanous asyncResp->res.jsonValue["PowerControl"] = nlohmann::json::array(); 136c5d03ff4SJennifer Lee 1372474adfaSEd Tanous auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>( 13802da7c5aSEd Tanous asyncResp, chassisName, sensors::dbus::powerPaths, 139a0ec28b6SAdrian Ambrożewicz sensors::node::power); 140028f7ebcSEddie James 1412474adfaSEd Tanous getChassisData(sensorAsyncResp); 142028f7ebcSEddie James 1437e860f15SJohn Edward Broadbent // This callback verifies that the power limit is only provided 1447e860f15SJohn Edward Broadbent // for the chassis that implements the Chassis inventory item. 1457e860f15SJohn Edward Broadbent // This prevents things like power supplies providing the 1467e860f15SJohn Edward Broadbent // chassis power limit 147b9d36b47SEd Tanous 148b9d36b47SEd Tanous using Mapper = dbus::utility::MapperGetSubTreePathsResponse; 149002d39b4SEd Tanous auto chassisHandler = 1507a1dbc48SGeorge Liu [sensorAsyncResp](const boost::system::error_code& e, 151b9d36b47SEd Tanous const Mapper& chassisPaths) { 152271584abSEd Tanous if (e) 153028f7ebcSEddie James { 154028f7ebcSEddie James BMCWEB_LOG_ERROR 155002d39b4SEd Tanous << "Power Limit GetSubTreePaths handler Dbus error " << e; 156028f7ebcSEddie James return; 157028f7ebcSEddie James } 158028f7ebcSEddie James 159028f7ebcSEddie James bool found = false; 160028f7ebcSEddie James for (const std::string& chassis : chassisPaths) 161028f7ebcSEddie James { 162028f7ebcSEddie James size_t len = std::string::npos; 163f23b7296SEd Tanous size_t lastPos = chassis.rfind('/'); 164028f7ebcSEddie James if (lastPos == std::string::npos) 165028f7ebcSEddie James { 166028f7ebcSEddie James continue; 167028f7ebcSEddie James } 168028f7ebcSEddie James 169028f7ebcSEddie James if (lastPos == chassis.size() - 1) 170028f7ebcSEddie James { 171028f7ebcSEddie James size_t end = lastPos; 172f23b7296SEd Tanous lastPos = chassis.rfind('/', lastPos - 1); 173028f7ebcSEddie James if (lastPos == std::string::npos) 174028f7ebcSEddie James { 175028f7ebcSEddie James continue; 176028f7ebcSEddie James } 177028f7ebcSEddie James 178028f7ebcSEddie James len = end - (lastPos + 1); 179028f7ebcSEddie James } 180028f7ebcSEddie James 181028f7ebcSEddie James std::string interfaceChassisName = 182028f7ebcSEddie James chassis.substr(lastPos + 1, len); 18355f79e6fSEd Tanous if (interfaceChassisName == sensorAsyncResp->chassisId) 184028f7ebcSEddie James { 185028f7ebcSEddie James found = true; 186028f7ebcSEddie James break; 187028f7ebcSEddie James } 188028f7ebcSEddie James } 189028f7ebcSEddie James 190028f7ebcSEddie James if (!found) 191028f7ebcSEddie James { 192028f7ebcSEddie James BMCWEB_LOG_DEBUG << "Power Limit not present for " 193028f7ebcSEddie James << sensorAsyncResp->chassisId; 194028f7ebcSEddie James return; 195028f7ebcSEddie James } 196028f7ebcSEddie James 197168e20c1SEd Tanous auto valueHandler = 198168e20c1SEd Tanous [sensorAsyncResp]( 199028f7ebcSEddie James const boost::system::error_code ec, 200b9d36b47SEd Tanous const dbus::utility::DBusPropertiesMap& properties) { 201028f7ebcSEddie James if (ec) 202028f7ebcSEddie James { 203002d39b4SEd Tanous messages::internalError(sensorAsyncResp->asyncResp->res); 204028f7ebcSEddie James BMCWEB_LOG_ERROR 205002d39b4SEd Tanous << "Power Limit GetAll handler: Dbus error " << ec; 206028f7ebcSEddie James return; 207028f7ebcSEddie James } 208028f7ebcSEddie James 2097e860f15SJohn Edward Broadbent nlohmann::json& tempArray = 210002d39b4SEd Tanous sensorAsyncResp->asyncResp->res.jsonValue["PowerControl"]; 211028f7ebcSEddie James 2127e860f15SJohn Edward Broadbent // Put multiple "sensors" into a single PowerControl, 0, 2137e860f15SJohn Edward Broadbent // so only create the first one 214028f7ebcSEddie James if (tempArray.empty()) 215028f7ebcSEddie James { 2167ab06f49SGunnar Mills // Mandatory properties odata.id and MemberId 2177ab06f49SGunnar Mills // A warning without a odata.type 2181476687dSEd Tanous nlohmann::json::object_t powerControl; 219002d39b4SEd Tanous powerControl["@odata.type"] = "#Power.v1_0_0.PowerControl"; 220002d39b4SEd Tanous powerControl["@odata.id"] = "/redfish/v1/Chassis/" + 2217ab06f49SGunnar Mills sensorAsyncResp->chassisId + 2221476687dSEd Tanous "/Power#/PowerControl/0"; 2231476687dSEd Tanous powerControl["Name"] = "Chassis Power Control"; 2241476687dSEd Tanous powerControl["MemberId"] = "0"; 2251476687dSEd Tanous tempArray.push_back(std::move(powerControl)); 226028f7ebcSEddie James } 227028f7ebcSEddie James 228028f7ebcSEddie James nlohmann::json& sensorJson = tempArray.back(); 229028f7ebcSEddie James bool enabled = false; 230028f7ebcSEddie James double powerCap = 0.0; 231028f7ebcSEddie James int64_t scale = 0; 232028f7ebcSEddie James 233168e20c1SEd Tanous for (const std::pair<std::string, 234002d39b4SEd Tanous dbus::utility::DbusVariantType>& property : 235002d39b4SEd Tanous properties) 236028f7ebcSEddie James { 23755f79e6fSEd Tanous if (property.first == "Scale") 238028f7ebcSEddie James { 239028f7ebcSEddie James const int64_t* i = 2408d78b7a9SPatrick Williams std::get_if<int64_t>(&property.second); 241028f7ebcSEddie James 242e662eae8SEd Tanous if (i != nullptr) 243028f7ebcSEddie James { 244028f7ebcSEddie James scale = *i; 245028f7ebcSEddie James } 246028f7ebcSEddie James } 24755f79e6fSEd Tanous else if (property.first == "PowerCap") 248028f7ebcSEddie James { 249002d39b4SEd Tanous const double* d = std::get_if<double>(&property.second); 250028f7ebcSEddie James const int64_t* i = 2518d78b7a9SPatrick Williams std::get_if<int64_t>(&property.second); 252028f7ebcSEddie James const uint32_t* u = 2538d78b7a9SPatrick Williams std::get_if<uint32_t>(&property.second); 254028f7ebcSEddie James 255e662eae8SEd Tanous if (d != nullptr) 256028f7ebcSEddie James { 257028f7ebcSEddie James powerCap = *d; 258028f7ebcSEddie James } 259e662eae8SEd Tanous else if (i != nullptr) 260028f7ebcSEddie James { 261271584abSEd Tanous powerCap = static_cast<double>(*i); 262028f7ebcSEddie James } 263e662eae8SEd Tanous else if (u != nullptr) 264028f7ebcSEddie James { 265028f7ebcSEddie James powerCap = *u; 266028f7ebcSEddie James } 267028f7ebcSEddie James } 26855f79e6fSEd Tanous else if (property.first == "PowerCapEnable") 269028f7ebcSEddie James { 270002d39b4SEd Tanous const bool* b = std::get_if<bool>(&property.second); 271028f7ebcSEddie James 272e662eae8SEd Tanous if (b != nullptr) 273028f7ebcSEddie James { 274028f7ebcSEddie James enabled = *b; 275028f7ebcSEddie James } 276028f7ebcSEddie James } 277028f7ebcSEddie James } 278028f7ebcSEddie James 2797a1dbc48SGeorge Liu nlohmann::json& value = 2807a1dbc48SGeorge Liu sensorJson["PowerLimit"]["LimitInWatts"]; 2817a1dbc48SGeorge Liu 2827e860f15SJohn Edward Broadbent // LimitException is Mandatory attribute as per OCP 2837e860f15SJohn Edward Broadbent // Baseline Profile – v1.0.0, so currently making it 2847e860f15SJohn Edward Broadbent // "NoAction" as default value to make it OCP Compliant. 2855a64a6f3SJoshi-Mansi sensorJson["PowerLimit"]["LimitException"] = "NoAction"; 2865a64a6f3SJoshi-Mansi 287028f7ebcSEddie James if (enabled) 288028f7ebcSEddie James { 2897e860f15SJohn Edward Broadbent // Redfish specification indicates PowerLimit should 2907e860f15SJohn Edward Broadbent // be null if the limit is not enabled. 2917a1dbc48SGeorge Liu value = powerCap * std::pow(10, scale); 292028f7ebcSEddie James } 293028f7ebcSEddie James }; 294028f7ebcSEddie James 295d1bde9e5SKrzysztof Grobelny sdbusplus::asio::getAllProperties( 296d1bde9e5SKrzysztof Grobelny *crow::connections::systemBus, "xyz.openbmc_project.Settings", 297028f7ebcSEddie James "/xyz/openbmc_project/control/host0/power_cap", 298d1bde9e5SKrzysztof Grobelny "xyz.openbmc_project.Control.Power.Cap", 299d1bde9e5SKrzysztof Grobelny std::move(valueHandler)); 300028f7ebcSEddie James }; 301028f7ebcSEddie James 3027a1dbc48SGeorge Liu constexpr std::array<std::string_view, 2> interfaces = { 303f857e9aeSAppaRao Puli "xyz.openbmc_project.Inventory.Item.Board", 3047a1dbc48SGeorge Liu "xyz.openbmc_project.Inventory.Item.Chassis"}; 3057a1dbc48SGeorge Liu 3067a1dbc48SGeorge Liu dbus::utility::getSubTreePaths("/xyz/openbmc_project/inventory", 0, 3077a1dbc48SGeorge Liu interfaces, std::move(chassisHandler)); 3087e860f15SJohn Edward Broadbent }); 3094bb3dc34SCarol Wang 3107e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/") 311ed398213SEd Tanous .privileges(redfish::privileges::patchPower) 3127e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::patch)( 31345ca1b86SEd Tanous [&app](const crow::Request& req, 3147e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 3157e860f15SJohn Edward Broadbent const std::string& chassisName) { 3163ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 31745ca1b86SEd Tanous { 31845ca1b86SEd Tanous return; 31945ca1b86SEd Tanous } 3208d1b46d7Szhanghch05 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>( 32102da7c5aSEd Tanous asyncResp, chassisName, sensors::dbus::powerPaths, 322a0ec28b6SAdrian Ambrożewicz sensors::node::power); 3234bb3dc34SCarol Wang 3244bb3dc34SCarol Wang std::optional<std::vector<nlohmann::json>> voltageCollections; 3254bb3dc34SCarol Wang std::optional<std::vector<nlohmann::json>> powerCtlCollections; 3264bb3dc34SCarol Wang 327002d39b4SEd Tanous if (!json_util::readJsonPatch(req, sensorAsyncResp->asyncResp->res, 328002d39b4SEd Tanous "PowerControl", powerCtlCollections, 329002d39b4SEd Tanous "Voltages", voltageCollections)) 3304bb3dc34SCarol Wang { 3314bb3dc34SCarol Wang return; 3324bb3dc34SCarol Wang } 3334bb3dc34SCarol Wang 3344bb3dc34SCarol Wang if (powerCtlCollections) 3354bb3dc34SCarol Wang { 3368d1b46d7Szhanghch05 setPowerCapOverride(sensorAsyncResp, *powerCtlCollections); 3374bb3dc34SCarol Wang } 3384bb3dc34SCarol Wang if (voltageCollections) 3394bb3dc34SCarol Wang { 3404bb3dc34SCarol Wang std::unordered_map<std::string, std::vector<nlohmann::json>> 3414bb3dc34SCarol Wang allCollections; 342002d39b4SEd Tanous allCollections.emplace("Voltages", *std::move(voltageCollections)); 34380ac4024SBruce Lee setSensorsOverride(sensorAsyncResp, allCollections); 3444bb3dc34SCarol Wang } 3457e860f15SJohn Edward Broadbent }); 346413961deSRichard Marian Thomaiyar } 3472474adfaSEd Tanous 3482474adfaSEd Tanous } // namespace redfish 349