Borawski.Lukasz | 9c310685 | 2018-02-09 15:24:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 18 | #include "health.hpp" |
Borawski.Lukasz | 9c310685 | 2018-02-09 15:24:22 +0100 | [diff] [blame] | 19 | #include "node.hpp" |
Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 20 | #include "redfish_util.hpp" |
Borawski.Lukasz | 9c310685 | 2018-02-09 15:24:22 +0100 | [diff] [blame] | 21 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 22 | #include <boost/algorithm/string/replace.hpp> |
Santosh Puranik | af5d6058 | 2019-03-20 18:16:36 +0530 | [diff] [blame] | 23 | #include <boost/date_time.hpp> |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 24 | #include <dbus_utility.hpp> |
Andrew Geissler | e90c505 | 2019-06-28 13:52:27 -0500 | [diff] [blame] | 25 | #include <utils/fw_utils.hpp> |
Bernard Wong | 7bffdb7 | 2019-03-20 16:17:21 +0800 | [diff] [blame] | 26 | #include <utils/systemd_utils.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 27 | |
Gunnar Mills | 4bfefa7 | 2020-07-30 13:54:29 -0500 | [diff] [blame] | 28 | #include <cstdint> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 29 | #include <memory> |
| 30 | #include <sstream> |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 31 | #include <variant> |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 32 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 33 | namespace redfish |
| 34 | { |
Jennifer Lee | ed5befb | 2018-08-10 11:29:45 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
Gunnar Mills | 2a5c440 | 2020-05-19 09:07:24 -0500 | [diff] [blame] | 37 | * Function reboots the BMC. |
| 38 | * |
| 39 | * @param[in] asyncResp - Shared pointer for completing asynchronous calls |
Jennifer Lee | ed5befb | 2018-08-10 11:29:45 -0700 | [diff] [blame] | 40 | */ |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 41 | inline void doBMCGracefulRestart(std::shared_ptr<AsyncResp> asyncResp) |
Gunnar Mills | 2a5c440 | 2020-05-19 09:07:24 -0500 | [diff] [blame] | 42 | { |
| 43 | const char* processName = "xyz.openbmc_project.State.BMC"; |
| 44 | const char* objectPath = "/xyz/openbmc_project/state/bmc0"; |
| 45 | const char* interfaceName = "xyz.openbmc_project.State.BMC"; |
| 46 | const std::string& propertyValue = |
| 47 | "xyz.openbmc_project.State.BMC.Transition.Reboot"; |
| 48 | const char* destProperty = "RequestedBMCTransition"; |
| 49 | |
| 50 | // Create the D-Bus variant for D-Bus call. |
| 51 | VariantType dbusPropertyValue(propertyValue); |
| 52 | |
| 53 | crow::connections::systemBus->async_method_call( |
| 54 | [asyncResp](const boost::system::error_code ec) { |
| 55 | // Use "Set" method to set the property value. |
| 56 | if (ec) |
| 57 | { |
| 58 | BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec; |
| 59 | messages::internalError(asyncResp->res); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | messages::success(asyncResp->res); |
| 64 | }, |
| 65 | processName, objectPath, "org.freedesktop.DBus.Properties", "Set", |
| 66 | interfaceName, destProperty, dbusPropertyValue); |
| 67 | } |
| 68 | |
Ed Tanous | 1536473 | 2020-09-11 13:41:59 -0700 | [diff] [blame] | 69 | inline void doBMCForceRestart(std::shared_ptr<AsyncResp> asyncResp) |
Jayaprakash Mutyala | f92af38 | 2020-06-16 23:29:41 +0000 | [diff] [blame] | 70 | { |
| 71 | const char* processName = "xyz.openbmc_project.State.BMC"; |
| 72 | const char* objectPath = "/xyz/openbmc_project/state/bmc0"; |
| 73 | const char* interfaceName = "xyz.openbmc_project.State.BMC"; |
| 74 | const std::string& propertyValue = |
| 75 | "xyz.openbmc_project.State.BMC.Transition.HardReboot"; |
| 76 | const char* destProperty = "RequestedBMCTransition"; |
| 77 | |
| 78 | // Create the D-Bus variant for D-Bus call. |
| 79 | VariantType dbusPropertyValue(propertyValue); |
| 80 | |
| 81 | crow::connections::systemBus->async_method_call( |
| 82 | [asyncResp](const boost::system::error_code ec) { |
| 83 | // Use "Set" method to set the property value. |
| 84 | if (ec) |
| 85 | { |
| 86 | BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec; |
| 87 | messages::internalError(asyncResp->res); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | messages::success(asyncResp->res); |
| 92 | }, |
| 93 | processName, objectPath, "org.freedesktop.DBus.Properties", "Set", |
| 94 | interfaceName, destProperty, dbusPropertyValue); |
| 95 | } |
| 96 | |
Gunnar Mills | 2a5c440 | 2020-05-19 09:07:24 -0500 | [diff] [blame] | 97 | /** |
| 98 | * ManagerResetAction class supports the POST method for the Reset (reboot) |
| 99 | * action. |
| 100 | */ |
| 101 | class ManagerResetAction : public Node |
Jennifer Lee | ed5befb | 2018-08-10 11:29:45 -0700 | [diff] [blame] | 102 | { |
| 103 | public: |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 104 | ManagerResetAction(App& app) : |
Jennifer Lee | ed5befb | 2018-08-10 11:29:45 -0700 | [diff] [blame] | 105 | Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/") |
| 106 | { |
| 107 | entityPrivileges = { |
Jennifer Lee | ed5befb | 2018-08-10 11:29:45 -0700 | [diff] [blame] | 108 | {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; |
| 109 | } |
| 110 | |
| 111 | private: |
| 112 | /** |
Jennifer Lee | ed5befb | 2018-08-10 11:29:45 -0700 | [diff] [blame] | 113 | * Function handles POST method request. |
Gunnar Mills | 2a5c440 | 2020-05-19 09:07:24 -0500 | [diff] [blame] | 114 | * Analyzes POST body before sending Reset (Reboot) request data to D-Bus. |
Jayaprakash Mutyala | f92af38 | 2020-06-16 23:29:41 +0000 | [diff] [blame] | 115 | * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart". |
Jennifer Lee | ed5befb | 2018-08-10 11:29:45 -0700 | [diff] [blame] | 116 | */ |
| 117 | void doPost(crow::Response& res, const crow::Request& req, |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 118 | const std::vector<std::string>&) override |
Jennifer Lee | ed5befb | 2018-08-10 11:29:45 -0700 | [diff] [blame] | 119 | { |
Gunnar Mills | 2a5c440 | 2020-05-19 09:07:24 -0500 | [diff] [blame] | 120 | BMCWEB_LOG_DEBUG << "Post Manager Reset."; |
Jennifer Lee | ed5befb | 2018-08-10 11:29:45 -0700 | [diff] [blame] | 121 | |
Gunnar Mills | 2a5c440 | 2020-05-19 09:07:24 -0500 | [diff] [blame] | 122 | std::string resetType; |
| 123 | auto asyncResp = std::make_shared<AsyncResp>(res); |
| 124 | |
| 125 | if (!json_util::readJson(req, asyncResp->res, "ResetType", resetType)) |
Jennifer Lee | ed5befb | 2018-08-10 11:29:45 -0700 | [diff] [blame] | 126 | { |
| 127 | return; |
| 128 | } |
| 129 | |
Jayaprakash Mutyala | f92af38 | 2020-06-16 23:29:41 +0000 | [diff] [blame] | 130 | if (resetType == "GracefulRestart") |
| 131 | { |
| 132 | BMCWEB_LOG_DEBUG << "Proceeding with " << resetType; |
| 133 | doBMCGracefulRestart(asyncResp); |
| 134 | return; |
| 135 | } |
| 136 | else if (resetType == "ForceRestart") |
| 137 | { |
| 138 | BMCWEB_LOG_DEBUG << "Proceeding with " << resetType; |
| 139 | doBMCForceRestart(asyncResp); |
| 140 | return; |
| 141 | } |
| 142 | else |
Jennifer Lee | ed5befb | 2018-08-10 11:29:45 -0700 | [diff] [blame] | 143 | { |
Gunnar Mills | 2a5c440 | 2020-05-19 09:07:24 -0500 | [diff] [blame] | 144 | BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: " |
Jennifer Lee | ed5befb | 2018-08-10 11:29:45 -0700 | [diff] [blame] | 145 | << resetType; |
Gunnar Mills | 2a5c440 | 2020-05-19 09:07:24 -0500 | [diff] [blame] | 146 | messages::actionParameterNotSupported(asyncResp->res, resetType, |
| 147 | "ResetType"); |
| 148 | |
Jennifer Lee | ed5befb | 2018-08-10 11:29:45 -0700 | [diff] [blame] | 149 | return; |
| 150 | } |
Jennifer Lee | ed5befb | 2018-08-10 11:29:45 -0700 | [diff] [blame] | 151 | } |
| 152 | }; |
| 153 | |
Gunnar Mills | 3e40fc7 | 2020-05-19 19:18:17 -0500 | [diff] [blame] | 154 | /** |
| 155 | * ManagerResetToDefaultsAction class supports POST method for factory reset |
| 156 | * action. |
| 157 | */ |
| 158 | class ManagerResetToDefaultsAction : public Node |
| 159 | { |
| 160 | public: |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 161 | ManagerResetToDefaultsAction(App& app) : |
Gunnar Mills | 3e40fc7 | 2020-05-19 19:18:17 -0500 | [diff] [blame] | 162 | Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/") |
| 163 | { |
| 164 | entityPrivileges = { |
| 165 | {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; |
| 166 | } |
| 167 | |
| 168 | private: |
| 169 | /** |
| 170 | * Function handles ResetToDefaults POST method request. |
| 171 | * |
| 172 | * Analyzes POST body message and factory resets BMC by calling |
| 173 | * BMC code updater factory reset followed by a BMC reboot. |
| 174 | * |
| 175 | * BMC code updater factory reset wipes the whole BMC read-write |
| 176 | * filesystem which includes things like the network settings. |
| 177 | * |
| 178 | * OpenBMC only supports ResetToDefaultsType "ResetAll". |
| 179 | */ |
| 180 | void doPost(crow::Response& res, const crow::Request& req, |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 181 | const std::vector<std::string>&) override |
Gunnar Mills | 3e40fc7 | 2020-05-19 19:18:17 -0500 | [diff] [blame] | 182 | { |
| 183 | BMCWEB_LOG_DEBUG << "Post ResetToDefaults."; |
| 184 | |
| 185 | std::string resetType; |
| 186 | auto asyncResp = std::make_shared<AsyncResp>(res); |
| 187 | |
| 188 | if (!json_util::readJson(req, asyncResp->res, "ResetToDefaultsType", |
| 189 | resetType)) |
| 190 | { |
| 191 | BMCWEB_LOG_DEBUG << "Missing property ResetToDefaultsType."; |
| 192 | |
| 193 | messages::actionParameterMissing(asyncResp->res, "ResetToDefaults", |
| 194 | "ResetToDefaultsType"); |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | if (resetType != "ResetAll") |
| 199 | { |
| 200 | BMCWEB_LOG_DEBUG << "Invalid property value for " |
| 201 | "ResetToDefaultsType: " |
| 202 | << resetType; |
| 203 | messages::actionParameterNotSupported(asyncResp->res, resetType, |
| 204 | "ResetToDefaultsType"); |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | crow::connections::systemBus->async_method_call( |
| 209 | [asyncResp](const boost::system::error_code ec) { |
| 210 | if (ec) |
| 211 | { |
| 212 | BMCWEB_LOG_DEBUG << "Failed to ResetToDefaults: " << ec; |
| 213 | messages::internalError(asyncResp->res); |
| 214 | return; |
| 215 | } |
| 216 | // Factory Reset doesn't actually happen until a reboot |
| 217 | // Can't erase what the BMC is running on |
| 218 | doBMCGracefulRestart(asyncResp); |
| 219 | }, |
| 220 | "xyz.openbmc_project.Software.BMC.Updater", |
| 221 | "/xyz/openbmc_project/software", |
| 222 | "xyz.openbmc_project.Common.FactoryReset", "Reset"); |
| 223 | } |
| 224 | }; |
| 225 | |
AppaRao Puli | 1cb1a9e | 2020-07-17 23:38:57 +0530 | [diff] [blame] | 226 | /** |
| 227 | * ManagerResetActionInfo derived class for delivering Manager |
| 228 | * ResetType AllowableValues using ResetInfo schema. |
| 229 | */ |
| 230 | class ManagerResetActionInfo : public Node |
| 231 | { |
| 232 | public: |
| 233 | /* |
| 234 | * Default Constructor |
| 235 | */ |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 236 | ManagerResetActionInfo(App& app) : |
AppaRao Puli | 1cb1a9e | 2020-07-17 23:38:57 +0530 | [diff] [blame] | 237 | Node(app, "/redfish/v1/Managers/bmc/ResetActionInfo/") |
| 238 | { |
| 239 | entityPrivileges = { |
| 240 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 241 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 242 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 243 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 244 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 245 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
| 246 | } |
| 247 | |
| 248 | private: |
| 249 | /** |
| 250 | * Functions triggers appropriate requests on DBus |
| 251 | */ |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 252 | void doGet(crow::Response& res, const crow::Request&, |
| 253 | const std::vector<std::string>&) override |
AppaRao Puli | 1cb1a9e | 2020-07-17 23:38:57 +0530 | [diff] [blame] | 254 | { |
| 255 | res.jsonValue = { |
| 256 | {"@odata.type", "#ActionInfo.v1_1_2.ActionInfo"}, |
| 257 | {"@odata.id", "/redfish/v1/Managers/bmc/ResetActionInfo"}, |
| 258 | {"Name", "Reset Action Info"}, |
| 259 | {"Id", "ResetActionInfo"}, |
| 260 | {"Parameters", |
| 261 | {{{"Name", "ResetType"}, |
| 262 | {"Required", true}, |
| 263 | {"DataType", "String"}, |
Jayaprakash Mutyala | f92af38 | 2020-06-16 23:29:41 +0000 | [diff] [blame] | 264 | {"AllowableValues", {"GracefulRestart", "ForceRestart"}}}}}}; |
AppaRao Puli | 1cb1a9e | 2020-07-17 23:38:57 +0530 | [diff] [blame] | 265 | res.end(); |
| 266 | } |
| 267 | }; |
| 268 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 269 | static constexpr const char* objectManagerIface = |
| 270 | "org.freedesktop.DBus.ObjectManager"; |
| 271 | static constexpr const char* pidConfigurationIface = |
| 272 | "xyz.openbmc_project.Configuration.Pid"; |
| 273 | static constexpr const char* pidZoneConfigurationIface = |
| 274 | "xyz.openbmc_project.Configuration.Pid.Zone"; |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 275 | static constexpr const char* stepwiseConfigurationIface = |
| 276 | "xyz.openbmc_project.Configuration.Stepwise"; |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 277 | static constexpr const char* thermalModeIface = |
| 278 | "xyz.openbmc_project.Control.ThermalMode"; |
Borawski.Lukasz | 9c310685 | 2018-02-09 15:24:22 +0100 | [diff] [blame] | 279 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 280 | inline void asyncPopulatePid(const std::string& connection, |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 281 | const std::string& path, |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 282 | const std::string& currentProfile, |
| 283 | const std::vector<std::string>& supportedProfiles, |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 284 | std::shared_ptr<AsyncResp> asyncResp) |
| 285 | { |
| 286 | |
| 287 | crow::connections::systemBus->async_method_call( |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 288 | [asyncResp, currentProfile, supportedProfiles]( |
| 289 | const boost::system::error_code ec, |
| 290 | const dbus::utility::ManagedObjectType& managedObj) { |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 291 | if (ec) |
| 292 | { |
| 293 | BMCWEB_LOG_ERROR << ec; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 294 | asyncResp->res.jsonValue.clear(); |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 295 | messages::internalError(asyncResp->res); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 296 | return; |
| 297 | } |
| 298 | nlohmann::json& configRoot = |
| 299 | asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"]; |
| 300 | nlohmann::json& fans = configRoot["FanControllers"]; |
| 301 | fans["@odata.type"] = "#OemManager.FanControllers"; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 302 | fans["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/" |
| 303 | "Fan/FanControllers"; |
| 304 | |
| 305 | nlohmann::json& pids = configRoot["PidControllers"]; |
| 306 | pids["@odata.type"] = "#OemManager.PidControllers"; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 307 | pids["@odata.id"] = |
| 308 | "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers"; |
| 309 | |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 310 | nlohmann::json& stepwise = configRoot["StepwiseControllers"]; |
| 311 | stepwise["@odata.type"] = "#OemManager.StepwiseControllers"; |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 312 | stepwise["@odata.id"] = |
| 313 | "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers"; |
| 314 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 315 | nlohmann::json& zones = configRoot["FanZones"]; |
| 316 | zones["@odata.id"] = |
| 317 | "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones"; |
| 318 | zones["@odata.type"] = "#OemManager.FanZones"; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 319 | configRoot["@odata.id"] = |
| 320 | "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan"; |
| 321 | configRoot["@odata.type"] = "#OemManager.Fan"; |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 322 | configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles; |
| 323 | |
| 324 | if (!currentProfile.empty()) |
| 325 | { |
| 326 | configRoot["Profile"] = currentProfile; |
| 327 | } |
| 328 | BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !"; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 329 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 330 | for (const auto& pathPair : managedObj) |
| 331 | { |
| 332 | for (const auto& intfPair : pathPair.second) |
| 333 | { |
| 334 | if (intfPair.first != pidConfigurationIface && |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 335 | intfPair.first != pidZoneConfigurationIface && |
| 336 | intfPair.first != stepwiseConfigurationIface) |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 337 | { |
| 338 | continue; |
| 339 | } |
| 340 | auto findName = intfPair.second.find("Name"); |
| 341 | if (findName == intfPair.second.end()) |
| 342 | { |
| 343 | BMCWEB_LOG_ERROR << "Pid Field missing Name"; |
Jason M. Bills | a08b46c | 2018-11-06 15:01:08 -0800 | [diff] [blame] | 344 | messages::internalError(asyncResp->res); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 345 | return; |
| 346 | } |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 347 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 348 | const std::string* namePtr = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 349 | std::get_if<std::string>(&findName->second); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 350 | if (namePtr == nullptr) |
| 351 | { |
| 352 | BMCWEB_LOG_ERROR << "Pid Name Field illegal"; |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 353 | messages::internalError(asyncResp->res); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 354 | return; |
| 355 | } |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 356 | std::string name = *namePtr; |
| 357 | dbus::utility::escapePathForDbus(name); |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 358 | |
| 359 | auto findProfiles = intfPair.second.find("Profiles"); |
| 360 | if (findProfiles != intfPair.second.end()) |
| 361 | { |
| 362 | const std::vector<std::string>* profiles = |
| 363 | std::get_if<std::vector<std::string>>( |
| 364 | &findProfiles->second); |
| 365 | if (profiles == nullptr) |
| 366 | { |
| 367 | BMCWEB_LOG_ERROR << "Pid Profiles Field illegal"; |
| 368 | messages::internalError(asyncResp->res); |
| 369 | return; |
| 370 | } |
| 371 | if (std::find(profiles->begin(), profiles->end(), |
| 372 | currentProfile) == profiles->end()) |
| 373 | { |
| 374 | BMCWEB_LOG_INFO |
| 375 | << name << " not supported in current profile"; |
| 376 | continue; |
| 377 | } |
| 378 | } |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 379 | nlohmann::json* config = nullptr; |
James Feist | c33a90e | 2019-03-01 10:17:44 -0800 | [diff] [blame] | 380 | |
| 381 | const std::string* classPtr = nullptr; |
| 382 | auto findClass = intfPair.second.find("Class"); |
| 383 | if (findClass != intfPair.second.end()) |
| 384 | { |
| 385 | classPtr = std::get_if<std::string>(&findClass->second); |
| 386 | } |
| 387 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 388 | if (intfPair.first == pidZoneConfigurationIface) |
| 389 | { |
| 390 | std::string chassis; |
| 391 | if (!dbus::utility::getNthStringFromPath( |
| 392 | pathPair.first.str, 5, chassis)) |
| 393 | { |
| 394 | chassis = "#IllegalValue"; |
| 395 | } |
| 396 | nlohmann::json& zone = zones[name]; |
| 397 | zone["Chassis"] = { |
| 398 | {"@odata.id", "/redfish/v1/Chassis/" + chassis}}; |
| 399 | zone["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/" |
| 400 | "OpenBmc/Fan/FanZones/" + |
| 401 | name; |
| 402 | zone["@odata.type"] = "#OemManager.FanZone"; |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 403 | config = &zone; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 404 | } |
| 405 | |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 406 | else if (intfPair.first == stepwiseConfigurationIface) |
| 407 | { |
James Feist | c33a90e | 2019-03-01 10:17:44 -0800 | [diff] [blame] | 408 | if (classPtr == nullptr) |
| 409 | { |
| 410 | BMCWEB_LOG_ERROR << "Pid Class Field illegal"; |
| 411 | messages::internalError(asyncResp->res); |
| 412 | return; |
| 413 | } |
| 414 | |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 415 | nlohmann::json& controller = stepwise[name]; |
| 416 | config = &controller; |
| 417 | |
| 418 | controller["@odata.id"] = |
| 419 | "/redfish/v1/Managers/bmc#/Oem/" |
| 420 | "OpenBmc/Fan/StepwiseControllers/" + |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 421 | name; |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 422 | controller["@odata.type"] = |
| 423 | "#OemManager.StepwiseController"; |
| 424 | |
James Feist | c33a90e | 2019-03-01 10:17:44 -0800 | [diff] [blame] | 425 | controller["Direction"] = *classPtr; |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | // pid and fans are off the same configuration |
| 429 | else if (intfPair.first == pidConfigurationIface) |
| 430 | { |
James Feist | c33a90e | 2019-03-01 10:17:44 -0800 | [diff] [blame] | 431 | |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 432 | if (classPtr == nullptr) |
| 433 | { |
| 434 | BMCWEB_LOG_ERROR << "Pid Class Field illegal"; |
| 435 | messages::internalError(asyncResp->res); |
| 436 | return; |
| 437 | } |
| 438 | bool isFan = *classPtr == "fan"; |
| 439 | nlohmann::json& element = |
| 440 | isFan ? fans[name] : pids[name]; |
| 441 | config = &element; |
| 442 | if (isFan) |
| 443 | { |
| 444 | element["@odata.id"] = |
| 445 | "/redfish/v1/Managers/bmc#/Oem/" |
| 446 | "OpenBmc/Fan/FanControllers/" + |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 447 | name; |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 448 | element["@odata.type"] = |
| 449 | "#OemManager.FanController"; |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 450 | } |
| 451 | else |
| 452 | { |
| 453 | element["@odata.id"] = |
| 454 | "/redfish/v1/Managers/bmc#/Oem/" |
| 455 | "OpenBmc/Fan/PidControllers/" + |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 456 | name; |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 457 | element["@odata.type"] = |
| 458 | "#OemManager.PidController"; |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | else |
| 462 | { |
| 463 | BMCWEB_LOG_ERROR << "Unexpected configuration"; |
| 464 | messages::internalError(asyncResp->res); |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | // used for making maps out of 2 vectors |
| 469 | const std::vector<double>* keys = nullptr; |
| 470 | const std::vector<double>* values = nullptr; |
| 471 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 472 | for (const auto& propertyPair : intfPair.second) |
| 473 | { |
| 474 | if (propertyPair.first == "Type" || |
| 475 | propertyPair.first == "Class" || |
| 476 | propertyPair.first == "Name") |
| 477 | { |
| 478 | continue; |
| 479 | } |
| 480 | |
| 481 | // zones |
| 482 | if (intfPair.first == pidZoneConfigurationIface) |
| 483 | { |
Ed Tanous | 1b6b96c | 2018-11-30 11:35:41 -0800 | [diff] [blame] | 484 | const double* ptr = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 485 | std::get_if<double>(&propertyPair.second); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 486 | if (ptr == nullptr) |
| 487 | { |
| 488 | BMCWEB_LOG_ERROR << "Field Illegal " |
| 489 | << propertyPair.first; |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 490 | messages::internalError(asyncResp->res); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 491 | return; |
| 492 | } |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 493 | (*config)[propertyPair.first] = *ptr; |
| 494 | } |
| 495 | |
| 496 | if (intfPair.first == stepwiseConfigurationIface) |
| 497 | { |
| 498 | if (propertyPair.first == "Reading" || |
| 499 | propertyPair.first == "Output") |
| 500 | { |
| 501 | const std::vector<double>* ptr = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 502 | std::get_if<std::vector<double>>( |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 503 | &propertyPair.second); |
| 504 | |
| 505 | if (ptr == nullptr) |
| 506 | { |
| 507 | BMCWEB_LOG_ERROR << "Field Illegal " |
| 508 | << propertyPair.first; |
| 509 | messages::internalError(asyncResp->res); |
| 510 | return; |
| 511 | } |
| 512 | |
| 513 | if (propertyPair.first == "Reading") |
| 514 | { |
| 515 | keys = ptr; |
| 516 | } |
| 517 | else |
| 518 | { |
| 519 | values = ptr; |
| 520 | } |
| 521 | if (keys && values) |
| 522 | { |
| 523 | if (keys->size() != values->size()) |
| 524 | { |
| 525 | BMCWEB_LOG_ERROR |
| 526 | << "Reading and Output size don't " |
| 527 | "match "; |
| 528 | messages::internalError(asyncResp->res); |
| 529 | return; |
| 530 | } |
| 531 | nlohmann::json& steps = (*config)["Steps"]; |
| 532 | steps = nlohmann::json::array(); |
| 533 | for (size_t ii = 0; ii < keys->size(); ii++) |
| 534 | { |
| 535 | steps.push_back( |
| 536 | {{"Target", (*keys)[ii]}, |
| 537 | {"Output", (*values)[ii]}}); |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | if (propertyPair.first == "NegativeHysteresis" || |
| 542 | propertyPair.first == "PositiveHysteresis") |
| 543 | { |
| 544 | const double* ptr = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 545 | std::get_if<double>(&propertyPair.second); |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 546 | if (ptr == nullptr) |
| 547 | { |
| 548 | BMCWEB_LOG_ERROR << "Field Illegal " |
| 549 | << propertyPair.first; |
| 550 | messages::internalError(asyncResp->res); |
| 551 | return; |
| 552 | } |
| 553 | (*config)[propertyPair.first] = *ptr; |
| 554 | } |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | // pid and fans are off the same configuration |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 558 | if (intfPair.first == pidConfigurationIface || |
| 559 | intfPair.first == stepwiseConfigurationIface) |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 560 | { |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 561 | |
| 562 | if (propertyPair.first == "Zones") |
| 563 | { |
| 564 | const std::vector<std::string>* inputs = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 565 | std::get_if<std::vector<std::string>>( |
Ed Tanous | 1b6b96c | 2018-11-30 11:35:41 -0800 | [diff] [blame] | 566 | &propertyPair.second); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 567 | |
| 568 | if (inputs == nullptr) |
| 569 | { |
| 570 | BMCWEB_LOG_ERROR |
| 571 | << "Zones Pid Field Illegal"; |
Jason M. Bills | a08b46c | 2018-11-06 15:01:08 -0800 | [diff] [blame] | 572 | messages::internalError(asyncResp->res); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 573 | return; |
| 574 | } |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 575 | auto& data = (*config)[propertyPair.first]; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 576 | data = nlohmann::json::array(); |
| 577 | for (std::string itemCopy : *inputs) |
| 578 | { |
| 579 | dbus::utility::escapePathForDbus(itemCopy); |
| 580 | data.push_back( |
| 581 | {{"@odata.id", |
| 582 | "/redfish/v1/Managers/bmc#/Oem/" |
| 583 | "OpenBmc/Fan/FanZones/" + |
| 584 | itemCopy}}); |
| 585 | } |
| 586 | } |
| 587 | // todo(james): may never happen, but this |
| 588 | // assumes configuration data referenced in the |
| 589 | // PID config is provided by the same daemon, we |
| 590 | // could add another loop to cover all cases, |
| 591 | // but I'm okay kicking this can down the road a |
| 592 | // bit |
| 593 | |
| 594 | else if (propertyPair.first == "Inputs" || |
| 595 | propertyPair.first == "Outputs") |
| 596 | { |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 597 | auto& data = (*config)[propertyPair.first]; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 598 | const std::vector<std::string>* inputs = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 599 | std::get_if<std::vector<std::string>>( |
Ed Tanous | 1b6b96c | 2018-11-30 11:35:41 -0800 | [diff] [blame] | 600 | &propertyPair.second); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 601 | |
| 602 | if (inputs == nullptr) |
| 603 | { |
| 604 | BMCWEB_LOG_ERROR << "Field Illegal " |
| 605 | << propertyPair.first; |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 606 | messages::internalError(asyncResp->res); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 607 | return; |
| 608 | } |
| 609 | data = *inputs; |
James Feist | b943aae | 2019-07-11 16:33:56 -0700 | [diff] [blame] | 610 | } |
| 611 | else if (propertyPair.first == "SetPointOffset") |
| 612 | { |
| 613 | const std::string* ptr = |
| 614 | std::get_if<std::string>( |
| 615 | &propertyPair.second); |
| 616 | |
| 617 | if (ptr == nullptr) |
| 618 | { |
| 619 | BMCWEB_LOG_ERROR << "Field Illegal " |
| 620 | << propertyPair.first; |
| 621 | messages::internalError(asyncResp->res); |
| 622 | return; |
| 623 | } |
| 624 | // translate from dbus to redfish |
| 625 | if (*ptr == "WarningHigh") |
| 626 | { |
| 627 | (*config)["SetPointOffset"] = |
| 628 | "UpperThresholdNonCritical"; |
| 629 | } |
| 630 | else if (*ptr == "WarningLow") |
| 631 | { |
| 632 | (*config)["SetPointOffset"] = |
| 633 | "LowerThresholdNonCritical"; |
| 634 | } |
| 635 | else if (*ptr == "CriticalHigh") |
| 636 | { |
| 637 | (*config)["SetPointOffset"] = |
| 638 | "UpperThresholdCritical"; |
| 639 | } |
| 640 | else if (*ptr == "CriticalLow") |
| 641 | { |
| 642 | (*config)["SetPointOffset"] = |
| 643 | "LowerThresholdCritical"; |
| 644 | } |
| 645 | else |
| 646 | { |
| 647 | BMCWEB_LOG_ERROR << "Value Illegal " |
| 648 | << *ptr; |
| 649 | messages::internalError(asyncResp->res); |
| 650 | return; |
| 651 | } |
| 652 | } |
| 653 | // doubles |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 654 | else if (propertyPair.first == |
| 655 | "FFGainCoefficient" || |
| 656 | propertyPair.first == "FFOffCoefficient" || |
| 657 | propertyPair.first == "ICoefficient" || |
| 658 | propertyPair.first == "ILimitMax" || |
| 659 | propertyPair.first == "ILimitMin" || |
James Feist | aad1a25 | 2019-02-19 10:13:52 -0800 | [diff] [blame] | 660 | propertyPair.first == |
| 661 | "PositiveHysteresis" || |
| 662 | propertyPair.first == |
| 663 | "NegativeHysteresis" || |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 664 | propertyPair.first == "OutLimitMax" || |
| 665 | propertyPair.first == "OutLimitMin" || |
| 666 | propertyPair.first == "PCoefficient" || |
James Feist | 7625cb8 | 2019-01-23 11:58:21 -0800 | [diff] [blame] | 667 | propertyPair.first == "SetPoint" || |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 668 | propertyPair.first == "SlewNeg" || |
| 669 | propertyPair.first == "SlewPos") |
| 670 | { |
| 671 | const double* ptr = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 672 | std::get_if<double>(&propertyPair.second); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 673 | if (ptr == nullptr) |
| 674 | { |
| 675 | BMCWEB_LOG_ERROR << "Field Illegal " |
| 676 | << propertyPair.first; |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 677 | messages::internalError(asyncResp->res); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 678 | return; |
| 679 | } |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 680 | (*config)[propertyPair.first] = *ptr; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | }, |
| 687 | connection, path, objectManagerIface, "GetManagedObjects"); |
| 688 | } |
Jennifer Lee | ca53792 | 2018-08-10 10:07:30 -0700 | [diff] [blame] | 689 | |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 690 | enum class CreatePIDRet |
| 691 | { |
| 692 | fail, |
| 693 | del, |
| 694 | patch |
| 695 | }; |
| 696 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 697 | inline bool getZonesFromJsonReq(const std::shared_ptr<AsyncResp>& response, |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 698 | std::vector<nlohmann::json>& config, |
| 699 | std::vector<std::string>& zones) |
| 700 | { |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 701 | if (config.empty()) |
| 702 | { |
| 703 | BMCWEB_LOG_ERROR << "Empty Zones"; |
| 704 | messages::propertyValueFormatError(response->res, |
| 705 | nlohmann::json::array(), "Zones"); |
| 706 | return false; |
| 707 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 708 | for (auto& odata : config) |
| 709 | { |
| 710 | std::string path; |
| 711 | if (!redfish::json_util::readJson(odata, response->res, "@odata.id", |
| 712 | path)) |
| 713 | { |
| 714 | return false; |
| 715 | } |
| 716 | std::string input; |
James Feist | 61adbda | 2019-03-25 13:03:51 -0700 | [diff] [blame] | 717 | |
| 718 | // 8 below comes from |
| 719 | // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left |
| 720 | // 0 1 2 3 4 5 6 7 8 |
| 721 | if (!dbus::utility::getNthStringFromPath(path, 8, input)) |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 722 | { |
| 723 | BMCWEB_LOG_ERROR << "Got invalid path " << path; |
| 724 | BMCWEB_LOG_ERROR << "Illegal Type Zones"; |
| 725 | messages::propertyValueFormatError(response->res, odata.dump(), |
| 726 | "Zones"); |
| 727 | return false; |
| 728 | } |
| 729 | boost::replace_all(input, "_", " "); |
| 730 | zones.emplace_back(std::move(input)); |
| 731 | } |
| 732 | return true; |
| 733 | } |
| 734 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 735 | inline const dbus::utility::ManagedItem* |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 736 | findChassis(const dbus::utility::ManagedObjectType& managedObj, |
| 737 | const std::string& value, std::string& chassis) |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 738 | { |
| 739 | BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n"; |
| 740 | |
| 741 | std::string escaped = boost::replace_all_copy(value, " ", "_"); |
| 742 | escaped = "/" + escaped; |
| 743 | auto it = std::find_if( |
| 744 | managedObj.begin(), managedObj.end(), [&escaped](const auto& obj) { |
| 745 | if (boost::algorithm::ends_with(obj.first.str, escaped)) |
| 746 | { |
| 747 | BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n"; |
| 748 | return true; |
| 749 | } |
| 750 | return false; |
| 751 | }); |
| 752 | |
| 753 | if (it == managedObj.end()) |
| 754 | { |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 755 | return nullptr; |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 756 | } |
| 757 | // 5 comes from <chassis-name> being the 5th element |
| 758 | // /xyz/openbmc_project/inventory/system/chassis/<chassis-name> |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 759 | if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis)) |
| 760 | { |
| 761 | return &(*it); |
| 762 | } |
| 763 | |
| 764 | return nullptr; |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 765 | } |
| 766 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 767 | inline CreatePIDRet createPidInterface( |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 768 | const std::shared_ptr<AsyncResp>& response, const std::string& type, |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 769 | nlohmann::json::iterator it, const std::string& path, |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 770 | const dbus::utility::ManagedObjectType& managedObj, bool createNewObject, |
| 771 | boost::container::flat_map<std::string, dbus::utility::DbusVariantType>& |
| 772 | output, |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 773 | std::string& chassis, const std::string& profile) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 774 | { |
| 775 | |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 776 | // common deleter |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 777 | if (it.value() == nullptr) |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 778 | { |
| 779 | std::string iface; |
| 780 | if (type == "PidControllers" || type == "FanControllers") |
| 781 | { |
| 782 | iface = pidConfigurationIface; |
| 783 | } |
| 784 | else if (type == "FanZones") |
| 785 | { |
| 786 | iface = pidZoneConfigurationIface; |
| 787 | } |
| 788 | else if (type == "StepwiseControllers") |
| 789 | { |
| 790 | iface = stepwiseConfigurationIface; |
| 791 | } |
| 792 | else |
| 793 | { |
| 794 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type " |
| 795 | << type; |
| 796 | messages::propertyUnknown(response->res, type); |
| 797 | return CreatePIDRet::fail; |
| 798 | } |
James Feist | 6ee7f77 | 2020-02-06 16:25:27 -0800 | [diff] [blame] | 799 | |
| 800 | BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n"; |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 801 | // delete interface |
| 802 | crow::connections::systemBus->async_method_call( |
| 803 | [response, path](const boost::system::error_code ec) { |
| 804 | if (ec) |
| 805 | { |
| 806 | BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec; |
| 807 | messages::internalError(response->res); |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 808 | return; |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 809 | } |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 810 | messages::success(response->res); |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 811 | }, |
| 812 | "xyz.openbmc_project.EntityManager", path, iface, "Delete"); |
| 813 | return CreatePIDRet::del; |
| 814 | } |
| 815 | |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 816 | const dbus::utility::ManagedItem* managedItem = nullptr; |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 817 | if (!createNewObject) |
| 818 | { |
| 819 | // if we aren't creating a new object, we should be able to find it on |
| 820 | // d-bus |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 821 | managedItem = findChassis(managedObj, it.key(), chassis); |
| 822 | if (managedItem == nullptr) |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 823 | { |
| 824 | BMCWEB_LOG_ERROR << "Failed to get chassis from config patch"; |
| 825 | messages::invalidObject(response->res, it.key()); |
| 826 | return CreatePIDRet::fail; |
| 827 | } |
| 828 | } |
| 829 | |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 830 | if (profile.size() && |
| 831 | (type == "PidControllers" || type == "FanControllers" || |
| 832 | type == "StepwiseControllers")) |
| 833 | { |
| 834 | if (managedItem == nullptr) |
| 835 | { |
| 836 | output["Profiles"] = std::vector<std::string>{profile}; |
| 837 | } |
| 838 | else |
| 839 | { |
| 840 | std::string interface; |
| 841 | if (type == "StepwiseControllers") |
| 842 | { |
| 843 | interface = stepwiseConfigurationIface; |
| 844 | } |
| 845 | else |
| 846 | { |
| 847 | interface = pidConfigurationIface; |
| 848 | } |
| 849 | auto findConfig = managedItem->second.find(interface); |
| 850 | if (findConfig == managedItem->second.end()) |
| 851 | { |
| 852 | BMCWEB_LOG_ERROR |
| 853 | << "Failed to find interface in managed object"; |
| 854 | messages::internalError(response->res); |
| 855 | return CreatePIDRet::fail; |
| 856 | } |
| 857 | auto findProfiles = findConfig->second.find("Profiles"); |
| 858 | if (findProfiles != findConfig->second.end()) |
| 859 | { |
| 860 | const std::vector<std::string>* curProfiles = |
| 861 | std::get_if<std::vector<std::string>>( |
| 862 | &(findProfiles->second)); |
| 863 | if (curProfiles == nullptr) |
| 864 | { |
| 865 | BMCWEB_LOG_ERROR << "Illegal profiles in managed object"; |
| 866 | messages::internalError(response->res); |
| 867 | return CreatePIDRet::fail; |
| 868 | } |
| 869 | if (std::find(curProfiles->begin(), curProfiles->end(), |
| 870 | profile) == curProfiles->end()) |
| 871 | { |
| 872 | std::vector<std::string> newProfiles = *curProfiles; |
| 873 | newProfiles.push_back(profile); |
| 874 | output["Profiles"] = newProfiles; |
| 875 | } |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 880 | if (type == "PidControllers" || type == "FanControllers") |
| 881 | { |
| 882 | if (createNewObject) |
| 883 | { |
| 884 | output["Class"] = type == "PidControllers" ? std::string("temp") |
| 885 | : std::string("fan"); |
| 886 | output["Type"] = std::string("Pid"); |
| 887 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 888 | |
| 889 | std::optional<std::vector<nlohmann::json>> zones; |
| 890 | std::optional<std::vector<std::string>> inputs; |
| 891 | std::optional<std::vector<std::string>> outputs; |
| 892 | std::map<std::string, std::optional<double>> doubles; |
James Feist | b943aae | 2019-07-11 16:33:56 -0700 | [diff] [blame] | 893 | std::optional<std::string> setpointOffset; |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 894 | if (!redfish::json_util::readJson( |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 895 | it.value(), response->res, "Inputs", inputs, "Outputs", outputs, |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 896 | "Zones", zones, "FFGainCoefficient", |
| 897 | doubles["FFGainCoefficient"], "FFOffCoefficient", |
| 898 | doubles["FFOffCoefficient"], "ICoefficient", |
| 899 | doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"], |
| 900 | "ILimitMin", doubles["ILimitMin"], "OutLimitMax", |
| 901 | doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"], |
| 902 | "PCoefficient", doubles["PCoefficient"], "SetPoint", |
James Feist | b943aae | 2019-07-11 16:33:56 -0700 | [diff] [blame] | 903 | doubles["SetPoint"], "SetPointOffset", setpointOffset, |
| 904 | "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"], |
| 905 | "PositiveHysteresis", doubles["PositiveHysteresis"], |
| 906 | "NegativeHysteresis", doubles["NegativeHysteresis"])) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 907 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 908 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property " |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 909 | << it.value().dump(); |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 910 | return CreatePIDRet::fail; |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 911 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 912 | if (zones) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 913 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 914 | std::vector<std::string> zonesStr; |
| 915 | if (!getZonesFromJsonReq(response, *zones, zonesStr)) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 916 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 917 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones"; |
| 918 | return CreatePIDRet::fail; |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 919 | } |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 920 | if (chassis.empty() && |
| 921 | !findChassis(managedObj, zonesStr[0], chassis)) |
| 922 | { |
| 923 | BMCWEB_LOG_ERROR << "Failed to get chassis from config patch"; |
| 924 | messages::invalidObject(response->res, it.key()); |
| 925 | return CreatePIDRet::fail; |
| 926 | } |
| 927 | |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 928 | output["Zones"] = std::move(zonesStr); |
| 929 | } |
| 930 | if (inputs || outputs) |
| 931 | { |
| 932 | std::array<std::optional<std::vector<std::string>>*, 2> containers = |
| 933 | {&inputs, &outputs}; |
| 934 | size_t index = 0; |
| 935 | for (const auto& containerPtr : containers) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 936 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 937 | std::optional<std::vector<std::string>>& container = |
| 938 | *containerPtr; |
| 939 | if (!container) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 940 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 941 | index++; |
| 942 | continue; |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 943 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 944 | |
| 945 | for (std::string& value : *container) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 946 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 947 | boost::replace_all(value, "_", " "); |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 948 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 949 | std::string key; |
| 950 | if (index == 0) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 951 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 952 | key = "Inputs"; |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 953 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 954 | else |
| 955 | { |
| 956 | key = "Outputs"; |
| 957 | } |
| 958 | output[key] = *container; |
| 959 | index++; |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 960 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 961 | } |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 962 | |
James Feist | b943aae | 2019-07-11 16:33:56 -0700 | [diff] [blame] | 963 | if (setpointOffset) |
| 964 | { |
| 965 | // translate between redfish and dbus names |
| 966 | if (*setpointOffset == "UpperThresholdNonCritical") |
| 967 | { |
| 968 | output["SetPointOffset"] = std::string("WarningLow"); |
| 969 | } |
| 970 | else if (*setpointOffset == "LowerThresholdNonCritical") |
| 971 | { |
| 972 | output["SetPointOffset"] = std::string("WarningHigh"); |
| 973 | } |
| 974 | else if (*setpointOffset == "LowerThresholdCritical") |
| 975 | { |
| 976 | output["SetPointOffset"] = std::string("CriticalLow"); |
| 977 | } |
| 978 | else if (*setpointOffset == "UpperThresholdCritical") |
| 979 | { |
| 980 | output["SetPointOffset"] = std::string("CriticalHigh"); |
| 981 | } |
| 982 | else |
| 983 | { |
| 984 | BMCWEB_LOG_ERROR << "Invalid setpointoffset " |
| 985 | << *setpointOffset; |
| 986 | messages::invalidObject(response->res, it.key()); |
| 987 | return CreatePIDRet::fail; |
| 988 | } |
| 989 | } |
| 990 | |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 991 | // doubles |
| 992 | for (const auto& pairs : doubles) |
| 993 | { |
| 994 | if (!pairs.second) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 995 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 996 | continue; |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 997 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 998 | BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second; |
| 999 | output[pairs.first] = *(pairs.second); |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1000 | } |
| 1001 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1002 | |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1003 | else if (type == "FanZones") |
| 1004 | { |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1005 | output["Type"] = std::string("Pid.Zone"); |
| 1006 | |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1007 | std::optional<nlohmann::json> chassisContainer; |
| 1008 | std::optional<double> failSafePercent; |
James Feist | d3ec07f | 2019-02-25 14:51:15 -0800 | [diff] [blame] | 1009 | std::optional<double> minThermalOutput; |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 1010 | if (!redfish::json_util::readJson(it.value(), response->res, "Chassis", |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1011 | chassisContainer, "FailSafePercent", |
James Feist | d3ec07f | 2019-02-25 14:51:15 -0800 | [diff] [blame] | 1012 | failSafePercent, "MinThermalOutput", |
| 1013 | minThermalOutput)) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1014 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1015 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property " |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 1016 | << it.value().dump(); |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1017 | return CreatePIDRet::fail; |
| 1018 | } |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1019 | |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1020 | if (chassisContainer) |
| 1021 | { |
| 1022 | |
| 1023 | std::string chassisId; |
| 1024 | if (!redfish::json_util::readJson(*chassisContainer, response->res, |
| 1025 | "@odata.id", chassisId)) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1026 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1027 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property " |
| 1028 | << chassisContainer->dump(); |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1029 | return CreatePIDRet::fail; |
| 1030 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1031 | |
AppaRao Puli | 717794d | 2019-10-18 22:54:53 +0530 | [diff] [blame] | 1032 | // /redfish/v1/chassis/chassis_name/ |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1033 | if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis)) |
| 1034 | { |
| 1035 | BMCWEB_LOG_ERROR << "Got invalid path " << chassisId; |
| 1036 | messages::invalidObject(response->res, chassisId); |
| 1037 | return CreatePIDRet::fail; |
| 1038 | } |
| 1039 | } |
James Feist | d3ec07f | 2019-02-25 14:51:15 -0800 | [diff] [blame] | 1040 | if (minThermalOutput) |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1041 | { |
James Feist | d3ec07f | 2019-02-25 14:51:15 -0800 | [diff] [blame] | 1042 | output["MinThermalOutput"] = *minThermalOutput; |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1043 | } |
| 1044 | if (failSafePercent) |
| 1045 | { |
| 1046 | output["FailSafePercent"] = *failSafePercent; |
| 1047 | } |
| 1048 | } |
| 1049 | else if (type == "StepwiseControllers") |
| 1050 | { |
| 1051 | output["Type"] = std::string("Stepwise"); |
| 1052 | |
| 1053 | std::optional<std::vector<nlohmann::json>> zones; |
| 1054 | std::optional<std::vector<nlohmann::json>> steps; |
| 1055 | std::optional<std::vector<std::string>> inputs; |
| 1056 | std::optional<double> positiveHysteresis; |
| 1057 | std::optional<double> negativeHysteresis; |
James Feist | c33a90e | 2019-03-01 10:17:44 -0800 | [diff] [blame] | 1058 | std::optional<std::string> direction; // upper clipping curve vs lower |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1059 | if (!redfish::json_util::readJson( |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 1060 | it.value(), response->res, "Zones", zones, "Steps", steps, |
| 1061 | "Inputs", inputs, "PositiveHysteresis", positiveHysteresis, |
James Feist | c33a90e | 2019-03-01 10:17:44 -0800 | [diff] [blame] | 1062 | "NegativeHysteresis", negativeHysteresis, "Direction", |
| 1063 | direction)) |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1064 | { |
| 1065 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property " |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 1066 | << it.value().dump(); |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1067 | return CreatePIDRet::fail; |
| 1068 | } |
| 1069 | |
| 1070 | if (zones) |
| 1071 | { |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 1072 | std::vector<std::string> zonesStrs; |
| 1073 | if (!getZonesFromJsonReq(response, *zones, zonesStrs)) |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1074 | { |
| 1075 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones"; |
| 1076 | return CreatePIDRet::fail; |
| 1077 | } |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 1078 | if (chassis.empty() && |
| 1079 | !findChassis(managedObj, zonesStrs[0], chassis)) |
| 1080 | { |
| 1081 | BMCWEB_LOG_ERROR << "Failed to get chassis from config patch"; |
| 1082 | messages::invalidObject(response->res, it.key()); |
| 1083 | return CreatePIDRet::fail; |
| 1084 | } |
| 1085 | output["Zones"] = std::move(zonesStrs); |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1086 | } |
| 1087 | if (steps) |
| 1088 | { |
| 1089 | std::vector<double> readings; |
| 1090 | std::vector<double> outputs; |
| 1091 | for (auto& step : *steps) |
| 1092 | { |
| 1093 | double target; |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1094 | double out; |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1095 | |
| 1096 | if (!redfish::json_util::readJson(step, response->res, "Target", |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1097 | target, "Output", out)) |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1098 | { |
| 1099 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 1100 | << ", Illegal Property " |
| 1101 | << it.value().dump(); |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1102 | return CreatePIDRet::fail; |
| 1103 | } |
| 1104 | readings.emplace_back(target); |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1105 | outputs.emplace_back(out); |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1106 | } |
| 1107 | output["Reading"] = std::move(readings); |
| 1108 | output["Output"] = std::move(outputs); |
| 1109 | } |
| 1110 | if (inputs) |
| 1111 | { |
| 1112 | for (std::string& value : *inputs) |
| 1113 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1114 | boost::replace_all(value, "_", " "); |
| 1115 | } |
| 1116 | output["Inputs"] = std::move(*inputs); |
| 1117 | } |
| 1118 | if (negativeHysteresis) |
| 1119 | { |
| 1120 | output["NegativeHysteresis"] = *negativeHysteresis; |
| 1121 | } |
| 1122 | if (positiveHysteresis) |
| 1123 | { |
| 1124 | output["PositiveHysteresis"] = *positiveHysteresis; |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1125 | } |
James Feist | c33a90e | 2019-03-01 10:17:44 -0800 | [diff] [blame] | 1126 | if (direction) |
| 1127 | { |
| 1128 | constexpr const std::array<const char*, 2> allowedDirections = { |
| 1129 | "Ceiling", "Floor"}; |
| 1130 | if (std::find(allowedDirections.begin(), allowedDirections.end(), |
| 1131 | *direction) == allowedDirections.end()) |
| 1132 | { |
| 1133 | messages::propertyValueTypeError(response->res, "Direction", |
| 1134 | *direction); |
| 1135 | return CreatePIDRet::fail; |
| 1136 | } |
| 1137 | output["Class"] = *direction; |
| 1138 | } |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1139 | } |
| 1140 | else |
| 1141 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1142 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type " << type; |
Jason M. Bills | 35a62c7 | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1143 | messages::propertyUnknown(response->res, type); |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1144 | return CreatePIDRet::fail; |
| 1145 | } |
| 1146 | return CreatePIDRet::patch; |
| 1147 | } |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1148 | struct GetPIDValues : std::enable_shared_from_this<GetPIDValues> |
| 1149 | { |
| 1150 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1151 | GetPIDValues(const std::shared_ptr<AsyncResp>& asyncRespIn) : |
| 1152 | asyncResp(asyncRespIn) |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1153 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1154 | {} |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1155 | |
| 1156 | void run() |
| 1157 | { |
| 1158 | std::shared_ptr<GetPIDValues> self = shared_from_this(); |
| 1159 | |
| 1160 | // get all configurations |
| 1161 | crow::connections::systemBus->async_method_call( |
| 1162 | [self](const boost::system::error_code ec, |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1163 | const crow::openbmc_mapper::GetSubTreeType& subtreeLocal) { |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1164 | if (ec) |
| 1165 | { |
| 1166 | BMCWEB_LOG_ERROR << ec; |
| 1167 | messages::internalError(self->asyncResp->res); |
| 1168 | return; |
| 1169 | } |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1170 | self->subtree = subtreeLocal; |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1171 | }, |
| 1172 | "xyz.openbmc_project.ObjectMapper", |
| 1173 | "/xyz/openbmc_project/object_mapper", |
| 1174 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, |
| 1175 | std::array<const char*, 4>{ |
| 1176 | pidConfigurationIface, pidZoneConfigurationIface, |
| 1177 | objectManagerIface, stepwiseConfigurationIface}); |
| 1178 | |
| 1179 | // at the same time get the selected profile |
| 1180 | crow::connections::systemBus->async_method_call( |
| 1181 | [self](const boost::system::error_code ec, |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1182 | const crow::openbmc_mapper::GetSubTreeType& subtreeLocal) { |
| 1183 | if (ec || subtreeLocal.empty()) |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1184 | { |
| 1185 | return; |
| 1186 | } |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1187 | if (subtreeLocal[0].second.size() != 1) |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1188 | { |
| 1189 | // invalid mapper response, should never happen |
| 1190 | BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error"; |
| 1191 | messages::internalError(self->asyncResp->res); |
| 1192 | return; |
| 1193 | } |
| 1194 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1195 | const std::string& path = subtreeLocal[0].first; |
| 1196 | const std::string& owner = subtreeLocal[0].second[0].first; |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1197 | crow::connections::systemBus->async_method_call( |
| 1198 | [path, owner, self]( |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1199 | const boost::system::error_code ec2, |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1200 | const boost::container::flat_map< |
| 1201 | std::string, std::variant<std::vector<std::string>, |
| 1202 | std::string>>& resp) { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1203 | if (ec2) |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1204 | { |
| 1205 | BMCWEB_LOG_ERROR << "GetPIDValues: Can't get " |
| 1206 | "thermalModeIface " |
| 1207 | << path; |
| 1208 | messages::internalError(self->asyncResp->res); |
| 1209 | return; |
| 1210 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1211 | const std::string* current = nullptr; |
| 1212 | const std::vector<std::string>* supported = nullptr; |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1213 | for (auto& [key, value] : resp) |
| 1214 | { |
| 1215 | if (key == "Current") |
| 1216 | { |
| 1217 | current = std::get_if<std::string>(&value); |
| 1218 | if (current == nullptr) |
| 1219 | { |
| 1220 | BMCWEB_LOG_ERROR |
| 1221 | << "GetPIDValues: thermal mode " |
| 1222 | "iface invalid " |
| 1223 | << path; |
| 1224 | messages::internalError( |
| 1225 | self->asyncResp->res); |
| 1226 | return; |
| 1227 | } |
| 1228 | } |
| 1229 | if (key == "Supported") |
| 1230 | { |
| 1231 | supported = |
| 1232 | std::get_if<std::vector<std::string>>( |
| 1233 | &value); |
| 1234 | if (supported == nullptr) |
| 1235 | { |
| 1236 | BMCWEB_LOG_ERROR |
| 1237 | << "GetPIDValues: thermal mode " |
| 1238 | "iface invalid" |
| 1239 | << path; |
| 1240 | messages::internalError( |
| 1241 | self->asyncResp->res); |
| 1242 | return; |
| 1243 | } |
| 1244 | } |
| 1245 | } |
| 1246 | if (current == nullptr || supported == nullptr) |
| 1247 | { |
| 1248 | BMCWEB_LOG_ERROR << "GetPIDValues: thermal mode " |
| 1249 | "iface invalid " |
| 1250 | << path; |
| 1251 | messages::internalError(self->asyncResp->res); |
| 1252 | return; |
| 1253 | } |
| 1254 | self->currentProfile = *current; |
| 1255 | self->supportedProfiles = *supported; |
| 1256 | }, |
| 1257 | owner, path, "org.freedesktop.DBus.Properties", "GetAll", |
| 1258 | thermalModeIface); |
| 1259 | }, |
| 1260 | "xyz.openbmc_project.ObjectMapper", |
| 1261 | "/xyz/openbmc_project/object_mapper", |
| 1262 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, |
| 1263 | std::array<const char*, 1>{thermalModeIface}); |
| 1264 | } |
| 1265 | |
| 1266 | ~GetPIDValues() |
| 1267 | { |
| 1268 | if (asyncResp->res.result() != boost::beast::http::status::ok) |
| 1269 | { |
| 1270 | return; |
| 1271 | } |
| 1272 | // create map of <connection, path to objMgr>> |
| 1273 | boost::container::flat_map<std::string, std::string> objectMgrPaths; |
| 1274 | boost::container::flat_set<std::string> calledConnections; |
| 1275 | for (const auto& pathGroup : subtree) |
| 1276 | { |
| 1277 | for (const auto& connectionGroup : pathGroup.second) |
| 1278 | { |
| 1279 | auto findConnection = |
| 1280 | calledConnections.find(connectionGroup.first); |
| 1281 | if (findConnection != calledConnections.end()) |
| 1282 | { |
| 1283 | break; |
| 1284 | } |
| 1285 | for (const std::string& interface : connectionGroup.second) |
| 1286 | { |
| 1287 | if (interface == objectManagerIface) |
| 1288 | { |
| 1289 | objectMgrPaths[connectionGroup.first] = pathGroup.first; |
| 1290 | } |
| 1291 | // this list is alphabetical, so we |
| 1292 | // should have found the objMgr by now |
| 1293 | if (interface == pidConfigurationIface || |
| 1294 | interface == pidZoneConfigurationIface || |
| 1295 | interface == stepwiseConfigurationIface) |
| 1296 | { |
| 1297 | auto findObjMgr = |
| 1298 | objectMgrPaths.find(connectionGroup.first); |
| 1299 | if (findObjMgr == objectMgrPaths.end()) |
| 1300 | { |
| 1301 | BMCWEB_LOG_DEBUG << connectionGroup.first |
| 1302 | << "Has no Object Manager"; |
| 1303 | continue; |
| 1304 | } |
| 1305 | |
| 1306 | calledConnections.insert(connectionGroup.first); |
| 1307 | |
| 1308 | asyncPopulatePid(findObjMgr->first, findObjMgr->second, |
| 1309 | currentProfile, supportedProfiles, |
| 1310 | asyncResp); |
| 1311 | break; |
| 1312 | } |
| 1313 | } |
| 1314 | } |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | std::vector<std::string> supportedProfiles; |
| 1319 | std::string currentProfile; |
| 1320 | crow::openbmc_mapper::GetSubTreeType subtree; |
| 1321 | std::shared_ptr<AsyncResp> asyncResp; |
| 1322 | }; |
| 1323 | |
| 1324 | struct SetPIDValues : std::enable_shared_from_this<SetPIDValues> |
| 1325 | { |
| 1326 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1327 | SetPIDValues(const std::shared_ptr<AsyncResp>& asyncRespIn, |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1328 | nlohmann::json& data) : |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1329 | asyncResp(asyncRespIn) |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1330 | { |
| 1331 | |
| 1332 | std::optional<nlohmann::json> pidControllers; |
| 1333 | std::optional<nlohmann::json> fanControllers; |
| 1334 | std::optional<nlohmann::json> fanZones; |
| 1335 | std::optional<nlohmann::json> stepwiseControllers; |
| 1336 | |
| 1337 | if (!redfish::json_util::readJson( |
| 1338 | data, asyncResp->res, "PidControllers", pidControllers, |
| 1339 | "FanControllers", fanControllers, "FanZones", fanZones, |
| 1340 | "StepwiseControllers", stepwiseControllers, "Profile", profile)) |
| 1341 | { |
| 1342 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property " |
| 1343 | << data.dump(); |
| 1344 | return; |
| 1345 | } |
| 1346 | configuration.emplace_back("PidControllers", std::move(pidControllers)); |
| 1347 | configuration.emplace_back("FanControllers", std::move(fanControllers)); |
| 1348 | configuration.emplace_back("FanZones", std::move(fanZones)); |
| 1349 | configuration.emplace_back("StepwiseControllers", |
| 1350 | std::move(stepwiseControllers)); |
| 1351 | } |
| 1352 | void run() |
| 1353 | { |
| 1354 | if (asyncResp->res.result() != boost::beast::http::status::ok) |
| 1355 | { |
| 1356 | return; |
| 1357 | } |
| 1358 | |
| 1359 | std::shared_ptr<SetPIDValues> self = shared_from_this(); |
| 1360 | |
| 1361 | // todo(james): might make sense to do a mapper call here if this |
| 1362 | // interface gets more traction |
| 1363 | crow::connections::systemBus->async_method_call( |
| 1364 | [self](const boost::system::error_code ec, |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1365 | dbus::utility::ManagedObjectType& mObj) { |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1366 | if (ec) |
| 1367 | { |
| 1368 | BMCWEB_LOG_ERROR << "Error communicating to Entity Manager"; |
| 1369 | messages::internalError(self->asyncResp->res); |
| 1370 | return; |
| 1371 | } |
James Feist | e69d9de | 2020-02-07 12:23:27 -0800 | [diff] [blame] | 1372 | const std::array<const char*, 3> configurations = { |
| 1373 | pidConfigurationIface, pidZoneConfigurationIface, |
| 1374 | stepwiseConfigurationIface}; |
| 1375 | |
James Feist | 14b0b8d | 2020-02-12 11:52:07 -0800 | [diff] [blame] | 1376 | for (const auto& [path, object] : mObj) |
James Feist | e69d9de | 2020-02-07 12:23:27 -0800 | [diff] [blame] | 1377 | { |
James Feist | 14b0b8d | 2020-02-12 11:52:07 -0800 | [diff] [blame] | 1378 | for (const auto& [interface, _] : object) |
James Feist | e69d9de | 2020-02-07 12:23:27 -0800 | [diff] [blame] | 1379 | { |
| 1380 | if (std::find(configurations.begin(), |
| 1381 | configurations.end(), |
| 1382 | interface) != configurations.end()) |
| 1383 | { |
James Feist | 14b0b8d | 2020-02-12 11:52:07 -0800 | [diff] [blame] | 1384 | self->objectCount++; |
James Feist | e69d9de | 2020-02-07 12:23:27 -0800 | [diff] [blame] | 1385 | break; |
| 1386 | } |
| 1387 | } |
James Feist | e69d9de | 2020-02-07 12:23:27 -0800 | [diff] [blame] | 1388 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1389 | self->managedObj = std::move(mObj); |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1390 | }, |
| 1391 | "xyz.openbmc_project.EntityManager", "/", objectManagerIface, |
| 1392 | "GetManagedObjects"); |
| 1393 | |
| 1394 | // at the same time get the profile information |
| 1395 | crow::connections::systemBus->async_method_call( |
| 1396 | [self](const boost::system::error_code ec, |
| 1397 | const crow::openbmc_mapper::GetSubTreeType& subtree) { |
| 1398 | if (ec || subtree.empty()) |
| 1399 | { |
| 1400 | return; |
| 1401 | } |
| 1402 | if (subtree[0].second.empty()) |
| 1403 | { |
| 1404 | // invalid mapper response, should never happen |
| 1405 | BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error"; |
| 1406 | messages::internalError(self->asyncResp->res); |
| 1407 | return; |
| 1408 | } |
| 1409 | |
| 1410 | const std::string& path = subtree[0].first; |
| 1411 | const std::string& owner = subtree[0].second[0].first; |
| 1412 | crow::connections::systemBus->async_method_call( |
| 1413 | [self, path, owner]( |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 1414 | const boost::system::error_code ec2, |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1415 | const boost::container::flat_map< |
| 1416 | std::string, std::variant<std::vector<std::string>, |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1417 | std::string>>& r) { |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 1418 | if (ec2) |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1419 | { |
| 1420 | BMCWEB_LOG_ERROR << "SetPIDValues: Can't get " |
| 1421 | "thermalModeIface " |
| 1422 | << path; |
| 1423 | messages::internalError(self->asyncResp->res); |
| 1424 | return; |
| 1425 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1426 | const std::string* current = nullptr; |
| 1427 | const std::vector<std::string>* supported = nullptr; |
| 1428 | for (auto& [key, value] : r) |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1429 | { |
| 1430 | if (key == "Current") |
| 1431 | { |
| 1432 | current = std::get_if<std::string>(&value); |
| 1433 | if (current == nullptr) |
| 1434 | { |
| 1435 | BMCWEB_LOG_ERROR |
| 1436 | << "SetPIDValues: thermal mode " |
| 1437 | "iface invalid " |
| 1438 | << path; |
| 1439 | messages::internalError( |
| 1440 | self->asyncResp->res); |
| 1441 | return; |
| 1442 | } |
| 1443 | } |
| 1444 | if (key == "Supported") |
| 1445 | { |
| 1446 | supported = |
| 1447 | std::get_if<std::vector<std::string>>( |
| 1448 | &value); |
| 1449 | if (supported == nullptr) |
| 1450 | { |
| 1451 | BMCWEB_LOG_ERROR |
| 1452 | << "SetPIDValues: thermal mode " |
| 1453 | "iface invalid" |
| 1454 | << path; |
| 1455 | messages::internalError( |
| 1456 | self->asyncResp->res); |
| 1457 | return; |
| 1458 | } |
| 1459 | } |
| 1460 | } |
| 1461 | if (current == nullptr || supported == nullptr) |
| 1462 | { |
| 1463 | BMCWEB_LOG_ERROR << "SetPIDValues: thermal mode " |
| 1464 | "iface invalid " |
| 1465 | << path; |
| 1466 | messages::internalError(self->asyncResp->res); |
| 1467 | return; |
| 1468 | } |
| 1469 | self->currentProfile = *current; |
| 1470 | self->supportedProfiles = *supported; |
| 1471 | self->profileConnection = owner; |
| 1472 | self->profilePath = path; |
| 1473 | }, |
| 1474 | owner, path, "org.freedesktop.DBus.Properties", "GetAll", |
| 1475 | thermalModeIface); |
| 1476 | }, |
| 1477 | "xyz.openbmc_project.ObjectMapper", |
| 1478 | "/xyz/openbmc_project/object_mapper", |
| 1479 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, |
| 1480 | std::array<const char*, 1>{thermalModeIface}); |
| 1481 | } |
| 1482 | ~SetPIDValues() |
| 1483 | { |
| 1484 | if (asyncResp->res.result() != boost::beast::http::status::ok) |
| 1485 | { |
| 1486 | return; |
| 1487 | } |
| 1488 | |
| 1489 | std::shared_ptr<AsyncResp> response = asyncResp; |
| 1490 | |
| 1491 | if (profile) |
| 1492 | { |
| 1493 | if (std::find(supportedProfiles.begin(), supportedProfiles.end(), |
| 1494 | *profile) == supportedProfiles.end()) |
| 1495 | { |
| 1496 | messages::actionParameterUnknown(response->res, "Profile", |
| 1497 | *profile); |
| 1498 | return; |
| 1499 | } |
| 1500 | currentProfile = *profile; |
| 1501 | crow::connections::systemBus->async_method_call( |
| 1502 | [response](const boost::system::error_code ec) { |
| 1503 | if (ec) |
| 1504 | { |
| 1505 | BMCWEB_LOG_ERROR << "Error patching profile" << ec; |
| 1506 | messages::internalError(response->res); |
| 1507 | } |
| 1508 | }, |
| 1509 | profileConnection, profilePath, |
| 1510 | "org.freedesktop.DBus.Properties", "Set", thermalModeIface, |
| 1511 | "Current", std::variant<std::string>(*profile)); |
| 1512 | } |
| 1513 | |
| 1514 | for (auto& containerPair : configuration) |
| 1515 | { |
| 1516 | auto& container = containerPair.second; |
| 1517 | if (!container) |
| 1518 | { |
| 1519 | continue; |
| 1520 | } |
James Feist | 6ee7f77 | 2020-02-06 16:25:27 -0800 | [diff] [blame] | 1521 | BMCWEB_LOG_DEBUG << *container; |
| 1522 | |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1523 | std::string& type = containerPair.first; |
| 1524 | |
| 1525 | for (nlohmann::json::iterator it = container->begin(); |
| 1526 | it != container->end(); it++) |
| 1527 | { |
| 1528 | const auto& name = it.key(); |
James Feist | 6ee7f77 | 2020-02-06 16:25:27 -0800 | [diff] [blame] | 1529 | BMCWEB_LOG_DEBUG << "looking for " << name; |
| 1530 | |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1531 | auto pathItr = |
| 1532 | std::find_if(managedObj.begin(), managedObj.end(), |
| 1533 | [&name](const auto& obj) { |
| 1534 | return boost::algorithm::ends_with( |
| 1535 | obj.first.str, "/" + name); |
| 1536 | }); |
| 1537 | boost::container::flat_map<std::string, |
| 1538 | dbus::utility::DbusVariantType> |
| 1539 | output; |
| 1540 | |
| 1541 | output.reserve(16); // The pid interface length |
| 1542 | |
| 1543 | // determines if we're patching entity-manager or |
| 1544 | // creating a new object |
| 1545 | bool createNewObject = (pathItr == managedObj.end()); |
James Feist | 6ee7f77 | 2020-02-06 16:25:27 -0800 | [diff] [blame] | 1546 | BMCWEB_LOG_DEBUG << "Found = " << !createNewObject; |
| 1547 | |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1548 | std::string iface; |
| 1549 | if (type == "PidControllers" || type == "FanControllers") |
| 1550 | { |
| 1551 | iface = pidConfigurationIface; |
| 1552 | if (!createNewObject && |
| 1553 | pathItr->second.find(pidConfigurationIface) == |
| 1554 | pathItr->second.end()) |
| 1555 | { |
| 1556 | createNewObject = true; |
| 1557 | } |
| 1558 | } |
| 1559 | else if (type == "FanZones") |
| 1560 | { |
| 1561 | iface = pidZoneConfigurationIface; |
| 1562 | if (!createNewObject && |
| 1563 | pathItr->second.find(pidZoneConfigurationIface) == |
| 1564 | pathItr->second.end()) |
| 1565 | { |
| 1566 | |
| 1567 | createNewObject = true; |
| 1568 | } |
| 1569 | } |
| 1570 | else if (type == "StepwiseControllers") |
| 1571 | { |
| 1572 | iface = stepwiseConfigurationIface; |
| 1573 | if (!createNewObject && |
| 1574 | pathItr->second.find(stepwiseConfigurationIface) == |
| 1575 | pathItr->second.end()) |
| 1576 | { |
| 1577 | createNewObject = true; |
| 1578 | } |
| 1579 | } |
James Feist | 6ee7f77 | 2020-02-06 16:25:27 -0800 | [diff] [blame] | 1580 | |
| 1581 | if (createNewObject && it.value() == nullptr) |
| 1582 | { |
Gunnar Mills | 4e0453b | 2020-07-08 14:00:30 -0500 | [diff] [blame] | 1583 | // can't delete a non-existent object |
James Feist | 6ee7f77 | 2020-02-06 16:25:27 -0800 | [diff] [blame] | 1584 | messages::invalidObject(response->res, name); |
| 1585 | continue; |
| 1586 | } |
| 1587 | |
| 1588 | std::string path; |
| 1589 | if (pathItr != managedObj.end()) |
| 1590 | { |
| 1591 | path = pathItr->first.str; |
| 1592 | } |
| 1593 | |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1594 | BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n"; |
James Feist | e69d9de | 2020-02-07 12:23:27 -0800 | [diff] [blame] | 1595 | |
| 1596 | // arbitrary limit to avoid attacks |
| 1597 | constexpr const size_t controllerLimit = 500; |
James Feist | 14b0b8d | 2020-02-12 11:52:07 -0800 | [diff] [blame] | 1598 | if (createNewObject && objectCount >= controllerLimit) |
James Feist | e69d9de | 2020-02-07 12:23:27 -0800 | [diff] [blame] | 1599 | { |
| 1600 | messages::resourceExhaustion(response->res, type); |
| 1601 | continue; |
| 1602 | } |
| 1603 | |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1604 | output["Name"] = boost::replace_all_copy(name, "_", " "); |
| 1605 | |
| 1606 | std::string chassis; |
| 1607 | CreatePIDRet ret = createPidInterface( |
James Feist | 6ee7f77 | 2020-02-06 16:25:27 -0800 | [diff] [blame] | 1608 | response, type, it, path, managedObj, createNewObject, |
| 1609 | output, chassis, currentProfile); |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1610 | if (ret == CreatePIDRet::fail) |
| 1611 | { |
| 1612 | return; |
| 1613 | } |
| 1614 | else if (ret == CreatePIDRet::del) |
| 1615 | { |
| 1616 | continue; |
| 1617 | } |
| 1618 | |
| 1619 | if (!createNewObject) |
| 1620 | { |
| 1621 | for (const auto& property : output) |
| 1622 | { |
| 1623 | crow::connections::systemBus->async_method_call( |
| 1624 | [response, |
| 1625 | propertyName{std::string(property.first)}]( |
| 1626 | const boost::system::error_code ec) { |
| 1627 | if (ec) |
| 1628 | { |
| 1629 | BMCWEB_LOG_ERROR << "Error patching " |
| 1630 | << propertyName << ": " |
| 1631 | << ec; |
| 1632 | messages::internalError(response->res); |
| 1633 | return; |
| 1634 | } |
| 1635 | messages::success(response->res); |
| 1636 | }, |
James Feist | 6ee7f77 | 2020-02-06 16:25:27 -0800 | [diff] [blame] | 1637 | "xyz.openbmc_project.EntityManager", path, |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1638 | "org.freedesktop.DBus.Properties", "Set", iface, |
| 1639 | property.first, property.second); |
| 1640 | } |
| 1641 | } |
| 1642 | else |
| 1643 | { |
| 1644 | if (chassis.empty()) |
| 1645 | { |
| 1646 | BMCWEB_LOG_ERROR << "Failed to get chassis from config"; |
| 1647 | messages::invalidObject(response->res, name); |
| 1648 | return; |
| 1649 | } |
| 1650 | |
| 1651 | bool foundChassis = false; |
| 1652 | for (const auto& obj : managedObj) |
| 1653 | { |
| 1654 | if (boost::algorithm::ends_with(obj.first.str, chassis)) |
| 1655 | { |
| 1656 | chassis = obj.first.str; |
| 1657 | foundChassis = true; |
| 1658 | break; |
| 1659 | } |
| 1660 | } |
| 1661 | if (!foundChassis) |
| 1662 | { |
| 1663 | BMCWEB_LOG_ERROR << "Failed to find chassis on dbus"; |
| 1664 | messages::resourceMissingAtURI( |
| 1665 | response->res, "/redfish/v1/Chassis/" + chassis); |
| 1666 | return; |
| 1667 | } |
| 1668 | |
| 1669 | crow::connections::systemBus->async_method_call( |
| 1670 | [response](const boost::system::error_code ec) { |
| 1671 | if (ec) |
| 1672 | { |
| 1673 | BMCWEB_LOG_ERROR << "Error Adding Pid Object " |
| 1674 | << ec; |
| 1675 | messages::internalError(response->res); |
| 1676 | return; |
| 1677 | } |
| 1678 | messages::success(response->res); |
| 1679 | }, |
| 1680 | "xyz.openbmc_project.EntityManager", chassis, |
| 1681 | "xyz.openbmc_project.AddObject", "AddObject", output); |
| 1682 | } |
| 1683 | } |
| 1684 | } |
| 1685 | } |
| 1686 | std::shared_ptr<AsyncResp> asyncResp; |
| 1687 | std::vector<std::pair<std::string, std::optional<nlohmann::json>>> |
| 1688 | configuration; |
| 1689 | std::optional<std::string> profile; |
| 1690 | dbus::utility::ManagedObjectType managedObj; |
| 1691 | std::vector<std::string> supportedProfiles; |
| 1692 | std::string currentProfile; |
| 1693 | std::string profileConnection; |
| 1694 | std::string profilePath; |
James Feist | 14b0b8d | 2020-02-12 11:52:07 -0800 | [diff] [blame] | 1695 | size_t objectCount = 0; |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1696 | }; |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1697 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1698 | class Manager : public Node |
| 1699 | { |
| 1700 | public: |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 1701 | Manager(App& app) : Node(app, "/redfish/v1/Managers/bmc/") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1702 | { |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 1703 | |
| 1704 | uuid = persistent_data::getConfig().systemUuid; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1705 | entityPrivileges = { |
| 1706 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 1707 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 1708 | {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, |
| 1709 | {boost::beast::http::verb::put, {{"ConfigureManager"}}}, |
| 1710 | {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, |
| 1711 | {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; |
Borawski.Lukasz | 9c310685 | 2018-02-09 15:24:22 +0100 | [diff] [blame] | 1712 | } |
| 1713 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1714 | private: |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 1715 | void doGet(crow::Response& res, const crow::Request&, |
| 1716 | const std::vector<std::string>&) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1717 | { |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1718 | res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc"; |
Gunnar Mills | 4bf2b03 | 2020-06-23 22:28:31 -0500 | [diff] [blame] | 1719 | res.jsonValue["@odata.type"] = "#Manager.v1_9_0.Manager"; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1720 | res.jsonValue["Id"] = "bmc"; |
| 1721 | res.jsonValue["Name"] = "OpenBmc Manager"; |
| 1722 | res.jsonValue["Description"] = "Baseboard Management Controller"; |
| 1723 | res.jsonValue["PowerState"] = "On"; |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 1724 | res.jsonValue["Status"] = {{"State", "Enabled"}, {"Health", "OK"}}; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1725 | res.jsonValue["ManagerType"] = "BMC"; |
Ed Tanous | 3602e23 | 2019-05-13 11:11:44 -0700 | [diff] [blame] | 1726 | res.jsonValue["UUID"] = systemd_utils::getUuid(); |
| 1727 | res.jsonValue["ServiceEntryPointUUID"] = uuid; |
Ed Tanous | 7517658 | 2018-12-14 08:14:34 -0800 | [diff] [blame] | 1728 | res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1729 | |
| 1730 | res.jsonValue["LogServices"] = { |
| 1731 | {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}}; |
| 1732 | |
| 1733 | res.jsonValue["NetworkProtocol"] = { |
| 1734 | {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}}; |
| 1735 | |
| 1736 | res.jsonValue["EthernetInterfaces"] = { |
| 1737 | {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}}; |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 1738 | |
| 1739 | #ifdef BMCWEB_ENABLE_VM_NBDPROXY |
| 1740 | res.jsonValue["VirtualMedia"] = { |
| 1741 | {"@odata.id", "/redfish/v1/Managers/bmc/VirtualMedia"}}; |
| 1742 | #endif // BMCWEB_ENABLE_VM_NBDPROXY |
| 1743 | |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1744 | // default oem data |
| 1745 | nlohmann::json& oem = res.jsonValue["Oem"]; |
| 1746 | nlohmann::json& oemOpenbmc = oem["OpenBmc"]; |
| 1747 | oem["@odata.type"] = "#OemManager.Oem"; |
| 1748 | oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem"; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1749 | oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc"; |
| 1750 | oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc"; |
Marri Devender Rao | cfcd5f6 | 2019-05-17 08:34:37 -0500 | [diff] [blame] | 1751 | oemOpenbmc["Certificates"] = { |
| 1752 | {"@odata.id", "/redfish/v1/Managers/bmc/Truststore/Certificates"}}; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1753 | |
Gunnar Mills | 2a5c440 | 2020-05-19 09:07:24 -0500 | [diff] [blame] | 1754 | // Manager.Reset (an action) can be many values, OpenBMC only supports |
| 1755 | // BMC reboot. |
| 1756 | nlohmann::json& managerReset = |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1757 | res.jsonValue["Actions"]["#Manager.Reset"]; |
Gunnar Mills | 2a5c440 | 2020-05-19 09:07:24 -0500 | [diff] [blame] | 1758 | managerReset["target"] = |
Jennifer Lee | ed5befb | 2018-08-10 11:29:45 -0700 | [diff] [blame] | 1759 | "/redfish/v1/Managers/bmc/Actions/Manager.Reset"; |
AppaRao Puli | 1cb1a9e | 2020-07-17 23:38:57 +0530 | [diff] [blame] | 1760 | managerReset["@Redfish.ActionInfo"] = |
| 1761 | "/redfish/v1/Managers/bmc/ResetActionInfo"; |
Jennifer Lee | ca53792 | 2018-08-10 10:07:30 -0700 | [diff] [blame] | 1762 | |
Gunnar Mills | 3e40fc7 | 2020-05-19 19:18:17 -0500 | [diff] [blame] | 1763 | // ResetToDefaults (Factory Reset) has values like |
| 1764 | // PreserveNetworkAndUsers and PreserveNetwork that aren't supported |
| 1765 | // on OpenBMC |
| 1766 | nlohmann::json& resetToDefaults = |
| 1767 | res.jsonValue["Actions"]["#Manager.ResetToDefaults"]; |
| 1768 | resetToDefaults["target"] = |
| 1769 | "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults"; |
| 1770 | resetToDefaults["ResetType@Redfish.AllowableValues"] = {"ResetAll"}; |
| 1771 | |
Andrew Geissler | cb92c03 | 2018-08-17 07:56:14 -0700 | [diff] [blame] | 1772 | res.jsonValue["DateTime"] = crow::utility::dateTimeNow(); |
Santosh Puranik | 474bfad | 2019-04-02 16:00:09 +0530 | [diff] [blame] | 1773 | |
Kuiying Wang | f8c3e6f | 2019-08-22 13:35:56 +0800 | [diff] [blame] | 1774 | // Fill in SerialConsole info |
Santosh Puranik | 474bfad | 2019-04-02 16:00:09 +0530 | [diff] [blame] | 1775 | res.jsonValue["SerialConsole"]["ServiceEnabled"] = true; |
Kuiying Wang | f8c3e6f | 2019-08-22 13:35:56 +0800 | [diff] [blame] | 1776 | res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15; |
Santosh Puranik | 474bfad | 2019-04-02 16:00:09 +0530 | [diff] [blame] | 1777 | res.jsonValue["SerialConsole"]["ConnectTypesSupported"] = {"IPMI", |
| 1778 | "SSH"}; |
Santosh Puranik | ef47bb1 | 2019-04-30 10:28:52 +0530 | [diff] [blame] | 1779 | #ifdef BMCWEB_ENABLE_KVM |
Kuiying Wang | f8c3e6f | 2019-08-22 13:35:56 +0800 | [diff] [blame] | 1780 | // Fill in GraphicalConsole info |
Santosh Puranik | ef47bb1 | 2019-04-30 10:28:52 +0530 | [diff] [blame] | 1781 | res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true; |
Jae Hyun Yoo | 704fae6 | 2019-10-02 13:01:27 -0700 | [diff] [blame] | 1782 | res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4; |
Santosh Puranik | ef47bb1 | 2019-04-30 10:28:52 +0530 | [diff] [blame] | 1783 | res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] = {"KVMIP"}; |
| 1784 | #endif // BMCWEB_ENABLE_KVM |
Santosh Puranik | 474bfad | 2019-04-02 16:00:09 +0530 | [diff] [blame] | 1785 | |
Gunnar Mills | 603a664 | 2019-01-21 17:03:51 -0600 | [diff] [blame] | 1786 | res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1; |
| 1787 | res.jsonValue["Links"]["ManagerForServers"] = { |
| 1788 | {{"@odata.id", "/redfish/v1/Systems/system"}}}; |
Shawn McCarney | 26f0389 | 2019-05-03 13:20:24 -0500 | [diff] [blame] | 1789 | |
Jennifer Lee | ed5befb | 2018-08-10 11:29:45 -0700 | [diff] [blame] | 1790 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 1791 | |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 1792 | auto health = std::make_shared<HealthPopulate>(asyncResp); |
| 1793 | health->isManagersHealth = true; |
| 1794 | health->populate(); |
| 1795 | |
Gunnar Mills | f97ddba | 2020-08-20 15:57:40 -0500 | [diff] [blame] | 1796 | fw_util::populateFirmwareInformation(asyncResp, fw_util::bmcPurpose, |
| 1797 | "FirmwareVersion", true); |
James Feist | 0f6b00b | 2019-06-10 14:15:53 -0700 | [diff] [blame] | 1798 | |
Gunnar Mills | 4bf2b03 | 2020-06-23 22:28:31 -0500 | [diff] [blame] | 1799 | getLastResetTime(asyncResp); |
| 1800 | |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1801 | auto pids = std::make_shared<GetPIDValues>(asyncResp); |
| 1802 | pids->run(); |
Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 1803 | |
| 1804 | getMainChassisId(asyncResp, [](const std::string& chassisId, |
| 1805 | const std::shared_ptr<AsyncResp> aRsp) { |
| 1806 | aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1; |
| 1807 | aRsp->res.jsonValue["Links"]["ManagerForChassis"] = { |
| 1808 | {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}}; |
Jason M. Bills | 2c0feb0 | 2019-07-26 16:35:20 -0700 | [diff] [blame] | 1809 | aRsp->res.jsonValue["Links"]["ManagerInChassis"] = { |
| 1810 | {"@odata.id", "/redfish/v1/Chassis/" + chassisId}}; |
Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 1811 | }); |
James Feist | 0f6b00b | 2019-06-10 14:15:53 -0700 | [diff] [blame] | 1812 | |
| 1813 | static bool started = false; |
| 1814 | |
| 1815 | if (!started) |
| 1816 | { |
| 1817 | crow::connections::systemBus->async_method_call( |
| 1818 | [asyncResp](const boost::system::error_code ec, |
| 1819 | const std::variant<double>& resp) { |
| 1820 | if (ec) |
| 1821 | { |
| 1822 | BMCWEB_LOG_ERROR << "Error while getting progress"; |
| 1823 | messages::internalError(asyncResp->res); |
| 1824 | return; |
| 1825 | } |
| 1826 | const double* val = std::get_if<double>(&resp); |
| 1827 | if (val == nullptr) |
| 1828 | { |
| 1829 | BMCWEB_LOG_ERROR |
| 1830 | << "Invalid response while getting progress"; |
| 1831 | messages::internalError(asyncResp->res); |
| 1832 | return; |
| 1833 | } |
| 1834 | if (*val < 1.0) |
| 1835 | { |
| 1836 | asyncResp->res.jsonValue["Status"]["State"] = |
| 1837 | "Starting"; |
| 1838 | started = true; |
| 1839 | } |
| 1840 | }, |
| 1841 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1", |
| 1842 | "org.freedesktop.DBus.Properties", "Get", |
| 1843 | "org.freedesktop.systemd1.Manager", "Progress"); |
| 1844 | } |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1845 | } |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 1846 | |
| 1847 | void doPatch(crow::Response& res, const crow::Request& req, |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 1848 | const std::vector<std::string>&) override |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 1849 | { |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 1850 | std::optional<nlohmann::json> oem; |
Gunnar Mills | 4bfefa7 | 2020-07-30 13:54:29 -0500 | [diff] [blame] | 1851 | std::optional<nlohmann::json> links; |
Santosh Puranik | af5d6058 | 2019-03-20 18:16:36 +0530 | [diff] [blame] | 1852 | std::optional<std::string> datetime; |
Santosh Puranik | 41352c2 | 2019-07-03 05:35:49 -0500 | [diff] [blame] | 1853 | std::shared_ptr<AsyncResp> response = std::make_shared<AsyncResp>(res); |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 1854 | |
Santosh Puranik | 41352c2 | 2019-07-03 05:35:49 -0500 | [diff] [blame] | 1855 | if (!json_util::readJson(req, response->res, "Oem", oem, "DateTime", |
Gunnar Mills | 4bfefa7 | 2020-07-30 13:54:29 -0500 | [diff] [blame] | 1856 | datetime, "Links", links)) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1857 | { |
| 1858 | return; |
| 1859 | } |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 1860 | |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 1861 | if (oem) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1862 | { |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 1863 | std::optional<nlohmann::json> openbmc; |
| 1864 | if (!redfish::json_util::readJson(*oem, res, "OpenBmc", openbmc)) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1865 | { |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 1866 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property " |
| 1867 | << oem->dump(); |
| 1868 | return; |
| 1869 | } |
| 1870 | if (openbmc) |
| 1871 | { |
| 1872 | std::optional<nlohmann::json> fan; |
| 1873 | if (!redfish::json_util::readJson(*openbmc, res, "Fan", fan)) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1874 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1875 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 1876 | << ", Illegal Property " |
| 1877 | << openbmc->dump(); |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1878 | return; |
| 1879 | } |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 1880 | if (fan) |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1881 | { |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 1882 | auto pid = std::make_shared<SetPIDValues>(response, *fan); |
| 1883 | pid->run(); |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1884 | } |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1885 | } |
| 1886 | } |
Gunnar Mills | 4bfefa7 | 2020-07-30 13:54:29 -0500 | [diff] [blame] | 1887 | if (links) |
| 1888 | { |
| 1889 | std::optional<nlohmann::json> activeSoftwareImage; |
| 1890 | if (!redfish::json_util::readJson( |
| 1891 | *links, res, "ActiveSoftwareImage", activeSoftwareImage)) |
| 1892 | { |
| 1893 | return; |
| 1894 | } |
| 1895 | if (activeSoftwareImage) |
| 1896 | { |
| 1897 | std::optional<std::string> odataId; |
| 1898 | if (!json_util::readJson(*activeSoftwareImage, res, "@odata.id", |
| 1899 | odataId)) |
| 1900 | { |
| 1901 | return; |
| 1902 | } |
| 1903 | |
| 1904 | if (odataId) |
| 1905 | { |
| 1906 | setActiveFirmwareImage(response, std::move(*odataId)); |
| 1907 | } |
| 1908 | } |
| 1909 | } |
Santosh Puranik | af5d6058 | 2019-03-20 18:16:36 +0530 | [diff] [blame] | 1910 | if (datetime) |
| 1911 | { |
| 1912 | setDateTime(response, std::move(*datetime)); |
| 1913 | } |
| 1914 | } |
| 1915 | |
Gunnar Mills | 4bf2b03 | 2020-06-23 22:28:31 -0500 | [diff] [blame] | 1916 | void getLastResetTime(std::shared_ptr<AsyncResp> aResp) |
| 1917 | { |
| 1918 | BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time"; |
| 1919 | |
| 1920 | crow::connections::systemBus->async_method_call( |
| 1921 | [aResp](const boost::system::error_code ec, |
| 1922 | std::variant<uint64_t>& lastResetTime) { |
| 1923 | if (ec) |
| 1924 | { |
| 1925 | BMCWEB_LOG_DEBUG << "D-BUS response error " << ec; |
| 1926 | return; |
| 1927 | } |
| 1928 | |
| 1929 | const uint64_t* lastResetTimePtr = |
| 1930 | std::get_if<uint64_t>(&lastResetTime); |
| 1931 | |
| 1932 | if (!lastResetTimePtr) |
| 1933 | { |
| 1934 | messages::internalError(aResp->res); |
| 1935 | return; |
| 1936 | } |
| 1937 | // LastRebootTime is epoch time, in milliseconds |
| 1938 | // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19 |
| 1939 | time_t lastResetTimeStamp = |
| 1940 | static_cast<time_t>(*lastResetTimePtr / 1000); |
| 1941 | |
| 1942 | // Convert to ISO 8601 standard |
| 1943 | aResp->res.jsonValue["LastResetTime"] = |
| 1944 | crow::utility::getDateTime(lastResetTimeStamp); |
| 1945 | }, |
| 1946 | "xyz.openbmc_project.State.BMC", "/xyz/openbmc_project/state/bmc0", |
| 1947 | "org.freedesktop.DBus.Properties", "Get", |
| 1948 | "xyz.openbmc_project.State.BMC", "LastRebootTime"); |
| 1949 | } |
| 1950 | |
Gunnar Mills | 4bfefa7 | 2020-07-30 13:54:29 -0500 | [diff] [blame] | 1951 | /** |
| 1952 | * @brief Set the running firmware image |
| 1953 | * |
| 1954 | * @param[i,o] aResp - Async response object |
| 1955 | * @param[i] runningFirmwareTarget - Image to make the running image |
| 1956 | * |
| 1957 | * @return void |
| 1958 | */ |
| 1959 | void setActiveFirmwareImage(std::shared_ptr<AsyncResp> aResp, |
| 1960 | const std::string&& runningFirmwareTarget) |
| 1961 | { |
| 1962 | // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id> |
| 1963 | std::string::size_type idPos = runningFirmwareTarget.rfind("/"); |
| 1964 | if (idPos == std::string::npos) |
| 1965 | { |
| 1966 | messages::propertyValueNotInList(aResp->res, runningFirmwareTarget, |
| 1967 | "@odata.id"); |
| 1968 | BMCWEB_LOG_DEBUG << "Can't parse firmware ID!"; |
| 1969 | return; |
| 1970 | } |
| 1971 | idPos++; |
| 1972 | if (idPos >= runningFirmwareTarget.size()) |
| 1973 | { |
| 1974 | messages::propertyValueNotInList(aResp->res, runningFirmwareTarget, |
| 1975 | "@odata.id"); |
| 1976 | BMCWEB_LOG_DEBUG << "Invalid firmware ID."; |
| 1977 | return; |
| 1978 | } |
| 1979 | std::string firmwareId = runningFirmwareTarget.substr(idPos); |
| 1980 | |
| 1981 | // Make sure the image is valid before setting priority |
| 1982 | crow::connections::systemBus->async_method_call( |
| 1983 | [aResp, firmwareId, |
| 1984 | runningFirmwareTarget](const boost::system::error_code ec, |
| 1985 | ManagedObjectType& subtree) { |
| 1986 | if (ec) |
| 1987 | { |
| 1988 | BMCWEB_LOG_DEBUG << "D-Bus response error getting objects."; |
| 1989 | messages::internalError(aResp->res); |
| 1990 | return; |
| 1991 | } |
| 1992 | |
| 1993 | if (subtree.size() == 0) |
| 1994 | { |
| 1995 | BMCWEB_LOG_DEBUG << "Can't find image!"; |
| 1996 | messages::internalError(aResp->res); |
| 1997 | return; |
| 1998 | } |
| 1999 | |
| 2000 | bool foundImage = false; |
| 2001 | for (auto& object : subtree) |
| 2002 | { |
| 2003 | const std::string& path = |
| 2004 | static_cast<const std::string&>(object.first); |
| 2005 | std::size_t idPos2 = path.rfind("/"); |
| 2006 | |
| 2007 | if (idPos2 == std::string::npos) |
| 2008 | { |
| 2009 | continue; |
| 2010 | } |
| 2011 | |
| 2012 | idPos2++; |
| 2013 | if (idPos2 >= path.size()) |
| 2014 | { |
| 2015 | continue; |
| 2016 | } |
| 2017 | |
| 2018 | if (path.substr(idPos2) == firmwareId) |
| 2019 | { |
| 2020 | foundImage = true; |
| 2021 | break; |
| 2022 | } |
| 2023 | } |
| 2024 | |
| 2025 | if (!foundImage) |
| 2026 | { |
| 2027 | messages::propertyValueNotInList( |
| 2028 | aResp->res, runningFirmwareTarget, "@odata.id"); |
| 2029 | BMCWEB_LOG_DEBUG << "Invalid firmware ID."; |
| 2030 | return; |
| 2031 | } |
| 2032 | |
| 2033 | BMCWEB_LOG_DEBUG << "Setting firmware version " + firmwareId + |
| 2034 | " to priority 0."; |
| 2035 | |
| 2036 | // Only support Immediate |
| 2037 | // An addition could be a Redfish Setting like |
| 2038 | // ActiveSoftwareImageApplyTime and support OnReset |
| 2039 | crow::connections::systemBus->async_method_call( |
| 2040 | [aResp](const boost::system::error_code ec) { |
| 2041 | if (ec) |
| 2042 | { |
| 2043 | BMCWEB_LOG_DEBUG << "D-Bus response error setting."; |
| 2044 | messages::internalError(aResp->res); |
| 2045 | return; |
| 2046 | } |
| 2047 | doBMCGracefulRestart(aResp); |
| 2048 | }, |
| 2049 | |
| 2050 | "xyz.openbmc_project.Software.BMC.Updater", |
| 2051 | "/xyz/openbmc_project/software/" + firmwareId, |
| 2052 | "org.freedesktop.DBus.Properties", "Set", |
| 2053 | "xyz.openbmc_project.Software.RedundancyPriority", |
| 2054 | "Priority", std::variant<uint8_t>(static_cast<uint8_t>(0))); |
| 2055 | }, |
| 2056 | "xyz.openbmc_project.Software.BMC.Updater", |
| 2057 | "/xyz/openbmc_project/software", |
| 2058 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 2059 | } |
| 2060 | |
Santosh Puranik | af5d6058 | 2019-03-20 18:16:36 +0530 | [diff] [blame] | 2061 | void setDateTime(std::shared_ptr<AsyncResp> aResp, |
| 2062 | std::string datetime) const |
| 2063 | { |
| 2064 | BMCWEB_LOG_DEBUG << "Set date time: " << datetime; |
| 2065 | |
| 2066 | std::stringstream stream(datetime); |
| 2067 | // Convert from ISO 8601 to boost local_time |
| 2068 | // (BMC only has time in UTC) |
| 2069 | boost::posix_time::ptime posixTime; |
| 2070 | boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1)); |
| 2071 | // Facet gets deleted with the stringsteam |
| 2072 | auto ifc = std::make_unique<boost::local_time::local_time_input_facet>( |
| 2073 | "%Y-%m-%d %H:%M:%S%F %ZP"); |
| 2074 | stream.imbue(std::locale(stream.getloc(), ifc.release())); |
| 2075 | |
| 2076 | boost::local_time::local_date_time ldt( |
| 2077 | boost::local_time::not_a_date_time); |
| 2078 | |
| 2079 | if (stream >> ldt) |
| 2080 | { |
| 2081 | posixTime = ldt.utc_time(); |
| 2082 | boost::posix_time::time_duration dur = posixTime - epoch; |
| 2083 | uint64_t durMicroSecs = |
| 2084 | static_cast<uint64_t>(dur.total_microseconds()); |
| 2085 | crow::connections::systemBus->async_method_call( |
| 2086 | [aResp{std::move(aResp)}, datetime{std::move(datetime)}]( |
| 2087 | const boost::system::error_code ec) { |
| 2088 | if (ec) |
| 2089 | { |
| 2090 | BMCWEB_LOG_DEBUG << "Failed to set elapsed time. " |
| 2091 | "DBUS response error " |
| 2092 | << ec; |
| 2093 | messages::internalError(aResp->res); |
| 2094 | return; |
| 2095 | } |
| 2096 | aResp->res.jsonValue["DateTime"] = datetime; |
| 2097 | }, |
| 2098 | "xyz.openbmc_project.Time.Manager", |
| 2099 | "/xyz/openbmc_project/time/bmc", |
| 2100 | "org.freedesktop.DBus.Properties", "Set", |
| 2101 | "xyz.openbmc_project.Time.EpochTime", "Elapsed", |
| 2102 | std::variant<uint64_t>(durMicroSecs)); |
| 2103 | } |
| 2104 | else |
| 2105 | { |
| 2106 | messages::propertyValueFormatError(aResp->res, datetime, |
| 2107 | "DateTime"); |
| 2108 | return; |
| 2109 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2110 | } |
| 2111 | |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 2112 | std::string uuid; |
Borawski.Lukasz | 9c310685 | 2018-02-09 15:24:22 +0100 | [diff] [blame] | 2113 | }; |
| 2114 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2115 | class ManagerCollection : public Node |
| 2116 | { |
| 2117 | public: |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 2118 | ManagerCollection(App& app) : Node(app, "/redfish/v1/Managers/") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2119 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2120 | entityPrivileges = { |
| 2121 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 2122 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 2123 | {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, |
| 2124 | {boost::beast::http::verb::put, {{"ConfigureManager"}}}, |
| 2125 | {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, |
| 2126 | {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; |
| 2127 | } |
Borawski.Lukasz | 9c310685 | 2018-02-09 15:24:22 +0100 | [diff] [blame] | 2128 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2129 | private: |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 2130 | void doGet(crow::Response& res, const crow::Request&, |
| 2131 | const std::vector<std::string>&) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2132 | { |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 2133 | // Collections don't include the static data added by SubRoute |
| 2134 | // because it has a duplicate entry for members |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2135 | res.jsonValue["@odata.id"] = "/redfish/v1/Managers"; |
| 2136 | res.jsonValue["@odata.type"] = "#ManagerCollection.ManagerCollection"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2137 | res.jsonValue["Name"] = "Manager Collection"; |
| 2138 | res.jsonValue["Members@odata.count"] = 1; |
| 2139 | res.jsonValue["Members"] = { |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 2140 | {{"@odata.id", "/redfish/v1/Managers/bmc"}}}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2141 | res.end(); |
| 2142 | } |
Borawski.Lukasz | 9c310685 | 2018-02-09 15:24:22 +0100 | [diff] [blame] | 2143 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2144 | } // namespace redfish |