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 |
| 139 | auto chassisHandler = [sensorAsyncResp]( |
| 140 | const boost::system::error_code e, |
| 141 | const std::vector<std::string>& |
| 142 | chassisPaths) { |
| 143 | if (e) |
| 144 | { |
| 145 | BMCWEB_LOG_ERROR |
| 146 | << "Power Limit GetSubTreePaths handler Dbus error " |
| 147 | << e; |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | bool found = false; |
| 152 | for (const std::string& chassis : chassisPaths) |
| 153 | { |
| 154 | size_t len = std::string::npos; |
| 155 | size_t lastPos = chassis.rfind('/'); |
| 156 | if (lastPos == std::string::npos) |
Eddie James | 028f7eb | 2019-05-17 21:24:36 +0000 | [diff] [blame] | 157 | { |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 158 | continue; |
Eddie James | 028f7eb | 2019-05-17 21:24:36 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 161 | if (lastPos == chassis.size() - 1) |
Eddie James | 028f7eb | 2019-05-17 21:24:36 +0000 | [diff] [blame] | 162 | { |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 163 | size_t end = lastPos; |
| 164 | lastPos = chassis.rfind('/', lastPos - 1); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 165 | if (lastPos == std::string::npos) |
Eddie James | 028f7eb | 2019-05-17 21:24:36 +0000 | [diff] [blame] | 166 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 167 | continue; |
Eddie James | 028f7eb | 2019-05-17 21:24:36 +0000 | [diff] [blame] | 168 | } |
Eddie James | 028f7eb | 2019-05-17 21:24:36 +0000 | [diff] [blame] | 169 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 170 | len = end - (lastPos + 1); |
Eddie James | 028f7eb | 2019-05-17 21:24:36 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 173 | std::string interfaceChassisName = |
| 174 | chassis.substr(lastPos + 1, len); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 175 | if (interfaceChassisName.compare( |
| 176 | sensorAsyncResp->chassisId) == 0) |
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, |
| 193 | const std::vector<std::pair< |
| 194 | std::string, dbus::utility::DbusVariantType>>& |
| 195 | properties) { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 196 | if (ec) |
| 197 | { |
| 198 | messages::internalError( |
| 199 | sensorAsyncResp->asyncResp->res); |
| 200 | BMCWEB_LOG_ERROR |
| 201 | << "Power Limit GetAll handler: Dbus error " |
| 202 | << ec; |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | nlohmann::json& tempArray = |
| 207 | sensorAsyncResp->asyncResp->res |
| 208 | .jsonValue["PowerControl"]; |
| 209 | |
| 210 | // Put multiple "sensors" into a single PowerControl, 0, |
| 211 | // so only create the first one |
| 212 | if (tempArray.empty()) |
| 213 | { |
| 214 | // Mandatory properties odata.id and MemberId |
| 215 | // A warning without a odata.type |
| 216 | tempArray.push_back( |
| 217 | {{"@odata.type", "#Power.v1_0_0.PowerControl"}, |
| 218 | {"@odata.id", "/redfish/v1/Chassis/" + |
| 219 | sensorAsyncResp->chassisId + |
| 220 | "/Power#/PowerControl/0"}, |
| 221 | {"Name", "Chassis Power Control"}, |
| 222 | {"MemberId", "0"}}); |
| 223 | } |
| 224 | |
| 225 | nlohmann::json& sensorJson = tempArray.back(); |
| 226 | bool enabled = false; |
| 227 | double powerCap = 0.0; |
| 228 | int64_t scale = 0; |
| 229 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 230 | for (const std::pair<std::string, |
| 231 | dbus::utility::DbusVariantType>& |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 232 | property : properties) |
| 233 | { |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 234 | if (property.first.compare("Scale") == 0) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 235 | { |
| 236 | const int64_t* i = |
| 237 | std::get_if<int64_t>(&property.second); |
| 238 | |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 239 | if (i != nullptr) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 240 | { |
| 241 | scale = *i; |
| 242 | } |
| 243 | } |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 244 | else if (property.first.compare("PowerCap") == 0) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 245 | { |
| 246 | const double* d = |
| 247 | std::get_if<double>(&property.second); |
| 248 | const int64_t* i = |
| 249 | std::get_if<int64_t>(&property.second); |
| 250 | const uint32_t* u = |
| 251 | std::get_if<uint32_t>(&property.second); |
| 252 | |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 253 | if (d != nullptr) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 254 | { |
| 255 | powerCap = *d; |
| 256 | } |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 257 | else if (i != nullptr) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 258 | { |
| 259 | powerCap = static_cast<double>(*i); |
| 260 | } |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 261 | else if (u != nullptr) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 262 | { |
| 263 | powerCap = *u; |
| 264 | } |
| 265 | } |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 266 | else if (property.first.compare("PowerCapEnable") == |
| 267 | 0) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 268 | { |
| 269 | const bool* b = |
| 270 | std::get_if<bool>(&property.second); |
| 271 | |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 272 | if (b != nullptr) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 273 | { |
| 274 | enabled = *b; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | nlohmann::json& value = |
| 280 | sensorJson["PowerLimit"]["LimitInWatts"]; |
| 281 | |
| 282 | // LimitException is Mandatory attribute as per OCP |
| 283 | // Baseline Profile – v1.0.0, so currently making it |
| 284 | // "NoAction" as default value to make it OCP Compliant. |
| 285 | sensorJson["PowerLimit"]["LimitException"] = "NoAction"; |
| 286 | |
| 287 | if (enabled) |
| 288 | { |
| 289 | // Redfish specification indicates PowerLimit should |
| 290 | // be null if the limit is not enabled. |
| 291 | value = powerCap * std::pow(10, scale); |
| 292 | } |
| 293 | }; |
| 294 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 295 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 296 | std::move(valueHandler), "xyz.openbmc_project.Settings", |
| 297 | "/xyz/openbmc_project/control/host0/power_cap", |
| 298 | "org.freedesktop.DBus.Properties", "GetAll", |
| 299 | "xyz.openbmc_project.Control.Power.Cap"); |
| 300 | }; |
| 301 | |
| 302 | crow::connections::systemBus->async_method_call( |
| 303 | std::move(chassisHandler), "xyz.openbmc_project.ObjectMapper", |
| 304 | "/xyz/openbmc_project/object_mapper", |
| 305 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", |
| 306 | "/xyz/openbmc_project/inventory", 0, |
| 307 | std::array<const char*, 2>{ |
| 308 | "xyz.openbmc_project.Inventory.Item.Board", |
| 309 | "xyz.openbmc_project.Inventory.Item.Chassis"}); |
| 310 | }); |
Eddie James | 028f7eb | 2019-05-17 21:24:36 +0000 | [diff] [blame] | 311 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 312 | BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 313 | .privileges(redfish::privileges::patchPower) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 314 | .methods(boost::beast::http::verb::patch)( |
| 315 | [](const crow::Request& req, |
| 316 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 317 | const std::string& chassisName) { |
| 318 | auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>( |
| 319 | asyncResp, chassisName, |
| 320 | sensors::dbus::paths.at(sensors::node::power), |
| 321 | sensors::node::power); |
Carol Wang | 4bb3dc3 | 2019-10-17 18:15:02 +0800 | [diff] [blame] | 322 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 323 | std::optional<std::vector<nlohmann::json>> voltageCollections; |
| 324 | std::optional<std::vector<nlohmann::json>> powerCtlCollections; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 325 | |
Willy Tu | 15ed678 | 2021-12-14 11:03:16 -0800 | [diff] [blame^] | 326 | if (!json_util::readJsonPatch( |
| 327 | req, sensorAsyncResp->asyncResp->res, "PowerControl", |
| 328 | powerCtlCollections, "Voltages", voltageCollections)) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 329 | { |
| 330 | return; |
| 331 | } |
Carol Wang | 4bb3dc3 | 2019-10-17 18:15:02 +0800 | [diff] [blame] | 332 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 333 | if (powerCtlCollections) |
| 334 | { |
| 335 | setPowerCapOverride(sensorAsyncResp, *powerCtlCollections); |
| 336 | } |
| 337 | if (voltageCollections) |
| 338 | { |
| 339 | std::unordered_map<std::string, std::vector<nlohmann::json>> |
| 340 | allCollections; |
| 341 | allCollections.emplace("Voltages", |
| 342 | *std::move(voltageCollections)); |
Bruce Lee | 80ac402 | 2021-06-04 15:41:39 +0800 | [diff] [blame] | 343 | setSensorsOverride(sensorAsyncResp, allCollections); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 344 | } |
| 345 | }); |
| 346 | } |
Ed Tanous | 2474adf | 2018-09-05 16:31:16 -0700 | [diff] [blame] | 347 | |
| 348 | } // namespace redfish |