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