Ed Tanous | 2474adf | 2018-09-05 16:31:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // Copyright (c) 2018 Ampere Computing LLC |
| 4 | / |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | */ |
| 17 | #pragma once |
| 18 | |
Ed Tanous | 2474adf | 2018-09-05 16:31:16 -0700 | [diff] [blame] | 19 | #include "sensors.hpp" |
| 20 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 21 | #include <app.hpp> |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 22 | #include <dbus_utility.hpp> |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 23 | #include <registries/privilege_registry.hpp> |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 24 | |
Ed Tanous | 2474adf | 2018-09-05 16:31:16 -0700 | [diff] [blame] | 25 | namespace redfish |
| 26 | { |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 27 | inline void setPowerCapOverride( |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 28 | const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp, |
| 29 | std::vector<nlohmann::json>& powerControlCollections) |
Ed Tanous | 2474adf | 2018-09-05 16:31:16 -0700 | [diff] [blame] | 30 | { |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 31 | auto getChassisPath = [sensorsAsyncResp, powerControlCollections]( |
| 32 | const std::optional<std::string>& |
| 33 | chassisPath) mutable { |
| 34 | if (!chassisPath) |
| 35 | { |
| 36 | BMCWEB_LOG_ERROR << "Don't find valid chassis path "; |
| 37 | messages::resourceNotFound(sensorsAsyncResp->asyncResp->res, |
| 38 | "Chassis", sensorsAsyncResp->chassisId); |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | if (powerControlCollections.size() != 1) |
| 43 | { |
| 44 | BMCWEB_LOG_ERROR << "Don't support multiple hosts at present "; |
| 45 | messages::resourceNotFound(sensorsAsyncResp->asyncResp->res, |
| 46 | "Power", "PowerControl"); |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | auto& item = powerControlCollections[0]; |
| 51 | |
| 52 | std::optional<nlohmann::json> powerLimit; |
| 53 | if (!json_util::readJson(item, sensorsAsyncResp->asyncResp->res, |
| 54 | "PowerLimit", powerLimit)) |
| 55 | { |
| 56 | return; |
| 57 | } |
| 58 | if (!powerLimit) |
| 59 | { |
| 60 | return; |
| 61 | } |
| 62 | std::optional<uint32_t> value; |
| 63 | if (!json_util::readJson(*powerLimit, sensorsAsyncResp->asyncResp->res, |
| 64 | "LimitInWatts", value)) |
| 65 | { |
| 66 | return; |
| 67 | } |
| 68 | if (!value) |
| 69 | { |
| 70 | return; |
| 71 | } |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 72 | sdbusplus::asio::getProperty<bool>( |
| 73 | *crow::connections::systemBus, "xyz.openbmc_project.Settings", |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 74 | "/xyz/openbmc_project/control/host0/power_cap", |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 75 | "xyz.openbmc_project.Control.Power.Cap", "PowerCapEnable", |
| 76 | [value, sensorsAsyncResp](const boost::system::error_code ec, |
| 77 | bool powerCapEnable) { |
| 78 | if (ec) |
| 79 | { |
| 80 | messages::internalError(sensorsAsyncResp->asyncResp->res); |
| 81 | BMCWEB_LOG_ERROR |
| 82 | << "powerCapEnable Get handler: Dbus error " << ec; |
| 83 | return; |
| 84 | } |
| 85 | if (!powerCapEnable) |
| 86 | { |
| 87 | messages::actionNotSupported( |
| 88 | sensorsAsyncResp->asyncResp->res, |
| 89 | "Setting LimitInWatts when PowerLimit feature is disabled"); |
| 90 | BMCWEB_LOG_ERROR << "PowerLimit feature is disabled "; |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | crow::connections::systemBus->async_method_call( |
| 95 | [sensorsAsyncResp](const boost::system::error_code ec2) { |
| 96 | if (ec2) |
| 97 | { |
| 98 | BMCWEB_LOG_DEBUG << "Power Limit Set: Dbus error: " |
| 99 | << ec2; |
| 100 | messages::internalError( |
| 101 | sensorsAsyncResp->asyncResp->res); |
| 102 | return; |
| 103 | } |
| 104 | sensorsAsyncResp->asyncResp->res.result( |
| 105 | boost::beast::http::status::no_content); |
| 106 | }, |
| 107 | "xyz.openbmc_project.Settings", |
| 108 | "/xyz/openbmc_project/control/host0/power_cap", |
| 109 | "org.freedesktop.DBus.Properties", "Set", |
| 110 | "xyz.openbmc_project.Control.Power.Cap", "PowerCap", |
| 111 | std::variant<uint32_t>(*value)); |
| 112 | }); |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 113 | }; |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 114 | getValidChassisPath(sensorsAsyncResp, std::move(getChassisPath)); |
| 115 | } |
| 116 | inline void requestRoutesPower(App& app) |
| 117 | { |
Ed Tanous | 2474adf | 2018-09-05 16:31:16 -0700 | [diff] [blame] | 118 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 119 | BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 120 | .privileges(redfish::privileges::getPower) |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 121 | .methods( |
| 122 | boost::beast::http::verb::get)([](const crow::Request&, |
| 123 | const std::shared_ptr< |
| 124 | bmcweb::AsyncResp>& asyncResp, |
| 125 | const std::string& chassisName) { |
| 126 | asyncResp->res.jsonValue["PowerControl"] = nlohmann::json::array(); |
Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 127 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 128 | auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>( |
| 129 | asyncResp, chassisName, |
| 130 | sensors::dbus::paths.at(sensors::node::power), |
| 131 | sensors::node::power); |
Eddie James | 028f7eb | 2019-05-17 21:24:36 +0000 | [diff] [blame] | 132 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 133 | getChassisData(sensorAsyncResp); |
Eddie James | 028f7eb | 2019-05-17 21:24:36 +0000 | [diff] [blame] | 134 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 135 | // This callback verifies that the power limit is only provided |
| 136 | // for the chassis that implements the Chassis inventory item. |
| 137 | // This prevents things like power supplies providing the |
| 138 | // chassis power limit |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 139 | |
| 140 | using Mapper = dbus::utility::MapperGetSubTreePathsResponse; |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 141 | auto chassisHandler = [sensorAsyncResp]( |
| 142 | const boost::system::error_code e, |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 143 | const Mapper& chassisPaths) { |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 144 | if (e) |
| 145 | { |
| 146 | BMCWEB_LOG_ERROR |
| 147 | << "Power Limit GetSubTreePaths handler Dbus error " |
| 148 | << e; |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | bool found = false; |
| 153 | for (const std::string& chassis : chassisPaths) |
| 154 | { |
| 155 | size_t len = std::string::npos; |
| 156 | size_t lastPos = chassis.rfind('/'); |
| 157 | if (lastPos == std::string::npos) |
Eddie James | 028f7eb | 2019-05-17 21:24:36 +0000 | [diff] [blame] | 158 | { |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 159 | continue; |
Eddie James | 028f7eb | 2019-05-17 21:24:36 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 162 | if (lastPos == chassis.size() - 1) |
Eddie James | 028f7eb | 2019-05-17 21:24:36 +0000 | [diff] [blame] | 163 | { |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 164 | size_t end = lastPos; |
| 165 | lastPos = chassis.rfind('/', lastPos - 1); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 166 | if (lastPos == std::string::npos) |
Eddie James | 028f7eb | 2019-05-17 21:24:36 +0000 | [diff] [blame] | 167 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 168 | continue; |
Eddie James | 028f7eb | 2019-05-17 21:24:36 +0000 | [diff] [blame] | 169 | } |
Eddie James | 028f7eb | 2019-05-17 21:24:36 +0000 | [diff] [blame] | 170 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 171 | len = end - (lastPos + 1); |
Eddie James | 028f7eb | 2019-05-17 21:24:36 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 174 | std::string interfaceChassisName = |
| 175 | chassis.substr(lastPos + 1, len); |
Ed Tanous | 55f79e6 | 2022-01-25 11:26:16 -0800 | [diff] [blame] | 176 | if (interfaceChassisName == sensorAsyncResp->chassisId) |
Eddie James | 028f7eb | 2019-05-17 21:24:36 +0000 | [diff] [blame] | 177 | { |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 178 | found = true; |
| 179 | break; |
Eddie James | 028f7eb | 2019-05-17 21:24:36 +0000 | [diff] [blame] | 180 | } |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 181 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 182 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 183 | if (!found) |
| 184 | { |
| 185 | BMCWEB_LOG_DEBUG << "Power Limit not present for " |
| 186 | << sensorAsyncResp->chassisId; |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | auto valueHandler = |
| 191 | [sensorAsyncResp]( |
| 192 | const boost::system::error_code ec, |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 193 | const dbus::utility::DBusPropertiesMap& properties) { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 194 | if (ec) |
| 195 | { |
| 196 | messages::internalError( |
| 197 | sensorAsyncResp->asyncResp->res); |
| 198 | BMCWEB_LOG_ERROR |
| 199 | << "Power Limit GetAll handler: Dbus error " |
| 200 | << ec; |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | nlohmann::json& tempArray = |
| 205 | sensorAsyncResp->asyncResp->res |
| 206 | .jsonValue["PowerControl"]; |
| 207 | |
| 208 | // Put multiple "sensors" into a single PowerControl, 0, |
| 209 | // so only create the first one |
| 210 | if (tempArray.empty()) |
| 211 | { |
| 212 | // Mandatory properties odata.id and MemberId |
| 213 | // A warning without a odata.type |
| 214 | tempArray.push_back( |
| 215 | {{"@odata.type", "#Power.v1_0_0.PowerControl"}, |
| 216 | {"@odata.id", "/redfish/v1/Chassis/" + |
| 217 | sensorAsyncResp->chassisId + |
| 218 | "/Power#/PowerControl/0"}, |
| 219 | {"Name", "Chassis Power Control"}, |
| 220 | {"MemberId", "0"}}); |
| 221 | } |
| 222 | |
| 223 | nlohmann::json& sensorJson = tempArray.back(); |
| 224 | bool enabled = false; |
| 225 | double powerCap = 0.0; |
| 226 | int64_t scale = 0; |
| 227 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 228 | for (const std::pair<std::string, |
| 229 | dbus::utility::DbusVariantType>& |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 230 | property : properties) |
| 231 | { |
Ed Tanous | 55f79e6 | 2022-01-25 11:26:16 -0800 | [diff] [blame] | 232 | if (property.first == "Scale") |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 233 | { |
| 234 | const int64_t* i = |
| 235 | std::get_if<int64_t>(&property.second); |
| 236 | |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 237 | if (i != nullptr) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 238 | { |
| 239 | scale = *i; |
| 240 | } |
| 241 | } |
Ed Tanous | 55f79e6 | 2022-01-25 11:26:16 -0800 | [diff] [blame] | 242 | else if (property.first == "PowerCap") |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 243 | { |
| 244 | const double* d = |
| 245 | std::get_if<double>(&property.second); |
| 246 | const int64_t* i = |
| 247 | std::get_if<int64_t>(&property.second); |
| 248 | const uint32_t* u = |
| 249 | std::get_if<uint32_t>(&property.second); |
| 250 | |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 251 | if (d != nullptr) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 252 | { |
| 253 | powerCap = *d; |
| 254 | } |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 255 | else if (i != nullptr) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 256 | { |
| 257 | powerCap = static_cast<double>(*i); |
| 258 | } |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 259 | else if (u != nullptr) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 260 | { |
| 261 | powerCap = *u; |
| 262 | } |
| 263 | } |
Ed Tanous | 55f79e6 | 2022-01-25 11:26:16 -0800 | [diff] [blame] | 264 | else if (property.first == "PowerCapEnable") |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 265 | { |
| 266 | const bool* b = |
| 267 | std::get_if<bool>(&property.second); |
| 268 | |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 269 | if (b != nullptr) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 270 | { |
| 271 | enabled = *b; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | nlohmann::json& value = |
| 277 | sensorJson["PowerLimit"]["LimitInWatts"]; |
| 278 | |
| 279 | // LimitException is Mandatory attribute as per OCP |
| 280 | // Baseline Profile – v1.0.0, so currently making it |
| 281 | // "NoAction" as default value to make it OCP Compliant. |
| 282 | sensorJson["PowerLimit"]["LimitException"] = "NoAction"; |
| 283 | |
| 284 | if (enabled) |
| 285 | { |
| 286 | // Redfish specification indicates PowerLimit should |
| 287 | // be null if the limit is not enabled. |
| 288 | value = powerCap * std::pow(10, scale); |
| 289 | } |
| 290 | }; |
| 291 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 292 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 293 | std::move(valueHandler), "xyz.openbmc_project.Settings", |
| 294 | "/xyz/openbmc_project/control/host0/power_cap", |
| 295 | "org.freedesktop.DBus.Properties", "GetAll", |
| 296 | "xyz.openbmc_project.Control.Power.Cap"); |
| 297 | }; |
| 298 | |
| 299 | crow::connections::systemBus->async_method_call( |
| 300 | std::move(chassisHandler), "xyz.openbmc_project.ObjectMapper", |
| 301 | "/xyz/openbmc_project/object_mapper", |
| 302 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", |
| 303 | "/xyz/openbmc_project/inventory", 0, |
| 304 | std::array<const char*, 2>{ |
| 305 | "xyz.openbmc_project.Inventory.Item.Board", |
| 306 | "xyz.openbmc_project.Inventory.Item.Chassis"}); |
| 307 | }); |
Eddie James | 028f7eb | 2019-05-17 21:24:36 +0000 | [diff] [blame] | 308 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 309 | BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 310 | .privileges(redfish::privileges::patchPower) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 311 | .methods(boost::beast::http::verb::patch)( |
| 312 | [](const crow::Request& req, |
| 313 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 314 | const std::string& chassisName) { |
| 315 | auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>( |
| 316 | asyncResp, chassisName, |
| 317 | sensors::dbus::paths.at(sensors::node::power), |
| 318 | sensors::node::power); |
Carol Wang | 4bb3dc3 | 2019-10-17 18:15:02 +0800 | [diff] [blame] | 319 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 320 | std::optional<std::vector<nlohmann::json>> voltageCollections; |
| 321 | std::optional<std::vector<nlohmann::json>> powerCtlCollections; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 322 | |
Willy Tu | 15ed678 | 2021-12-14 11:03:16 -0800 | [diff] [blame] | 323 | if (!json_util::readJsonPatch( |
| 324 | req, sensorAsyncResp->asyncResp->res, "PowerControl", |
| 325 | powerCtlCollections, "Voltages", voltageCollections)) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 326 | { |
| 327 | return; |
| 328 | } |
Carol Wang | 4bb3dc3 | 2019-10-17 18:15:02 +0800 | [diff] [blame] | 329 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 330 | if (powerCtlCollections) |
| 331 | { |
| 332 | setPowerCapOverride(sensorAsyncResp, *powerCtlCollections); |
| 333 | } |
| 334 | if (voltageCollections) |
| 335 | { |
| 336 | std::unordered_map<std::string, std::vector<nlohmann::json>> |
| 337 | allCollections; |
| 338 | allCollections.emplace("Voltages", |
| 339 | *std::move(voltageCollections)); |
Bruce Lee | 80ac402 | 2021-06-04 15:41:39 +0800 | [diff] [blame] | 340 | setSensorsOverride(sensorAsyncResp, allCollections); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 341 | } |
| 342 | }); |
| 343 | } |
Ed Tanous | 2474adf | 2018-09-05 16:31:16 -0700 | [diff] [blame] | 344 | |
| 345 | } // namespace redfish |