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