blob: fca5fc54cb880e877e4e10150b3133e0d755bf2e [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
3// SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01004#pragma once
5
Willy Tu13451e32023-05-24 16:08:18 -07006#include "bmcweb_config.h"
7
Sui Chena51fc2d2022-07-14 17:21:53 -07008#include "app.hpp"
9#include "dbus_utility.hpp"
Ed Tanous539d8c62024-06-19 14:38:27 -070010#include "generated/enums/action_info.hpp"
11#include "generated/enums/manager.hpp"
12#include "generated/enums/resource.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070013#include "query.hpp"
Jennifer Leec5d03ff2019-03-08 15:42:58 -080014#include "redfish_util.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070015#include "registries/privilege_registry.hpp"
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +020016#include "utils/dbus_utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080017#include "utils/json_utils.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070018#include "utils/sw_utils.hpp"
19#include "utils/systemd_utils.hpp"
Ed Tanous2b829372022-08-03 14:22:34 -070020#include "utils/time_utils.hpp"
Borawski.Lukasz9c3106852018-02-09 15:24:22 +010021
George Liue99073f2022-12-09 11:06:16 +080022#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070023#include <boost/url/format.hpp>
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +020024#include <sdbusplus/asio/property.hpp>
25#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050026
Ed Tanousa170f272022-06-30 21:53:27 -070027#include <algorithm>
George Liue99073f2022-12-09 11:06:16 +080028#include <array>
Gunnar Mills4bfefa72020-07-30 13:54:29 -050029#include <cstdint>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050030#include <memory>
Konstantin Aladyshev9970e932024-02-20 09:51:29 +030031#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070032#include <ranges>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050033#include <sstream>
Konstantin Aladyshev9970e932024-02-20 09:51:29 +030034#include <string>
George Liue99073f2022-12-09 11:06:16 +080035#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080036#include <variant>
James Feist5b4aa862018-08-16 14:07:01 -070037
Ed Tanous1abe55e2018-09-05 08:30:59 -070038namespace redfish
39{
Jennifer Leeed5befb2018-08-10 11:29:45 -070040
Jagpal Singh Gilld27c31e2024-10-15 15:10:19 -070041inline std::string getBMCUpdateServiceName()
42{
43 if constexpr (BMCWEB_REDFISH_UPDATESERVICE_USE_DBUS)
44 {
45 return "xyz.openbmc_project.Software.Manager";
46 }
47 return "xyz.openbmc_project.Software.BMC.Updater";
48}
49
50inline std::string getBMCUpdateServicePath()
51{
52 if constexpr (BMCWEB_REDFISH_UPDATESERVICE_USE_DBUS)
53 {
54 return "/xyz/openbmc_project/software/bmc";
55 }
56 return "/xyz/openbmc_project/software";
57}
58
Jennifer Leeed5befb2018-08-10 11:29:45 -070059/**
Gunnar Mills2a5c4402020-05-19 09:07:24 -050060 * Function reboots the BMC.
61 *
62 * @param[in] asyncResp - Shared pointer for completing asynchronous calls
Jennifer Leeed5befb2018-08-10 11:29:45 -070063 */
zhanghch058d1b46d2021-04-01 11:18:24 +080064inline void
65 doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Gunnar Mills2a5c4402020-05-19 09:07:24 -050066{
67 const char* processName = "xyz.openbmc_project.State.BMC";
68 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
69 const char* interfaceName = "xyz.openbmc_project.State.BMC";
70 const std::string& propertyValue =
71 "xyz.openbmc_project.State.BMC.Transition.Reboot";
72 const char* destProperty = "RequestedBMCTransition";
73
74 // Create the D-Bus variant for D-Bus call.
George Liu9ae226f2023-06-21 17:56:46 +080075 sdbusplus::asio::setProperty(
76 *crow::connections::systemBus, processName, objectPath, interfaceName,
77 destProperty, propertyValue,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080078 [asyncResp](const boost::system::error_code& ec) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -040079 // Use "Set" method to set the property value.
80 if (ec)
81 {
82 BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
83 messages::internalError(asyncResp->res);
84 return;
85 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -050086
Patrick Williamsbd79bce2024-08-16 15:22:20 -040087 messages::success(asyncResp->res);
88 });
Gunnar Mills2a5c4402020-05-19 09:07:24 -050089}
90
zhanghch058d1b46d2021-04-01 11:18:24 +080091inline void
92 doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +000093{
94 const char* processName = "xyz.openbmc_project.State.BMC";
95 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
96 const char* interfaceName = "xyz.openbmc_project.State.BMC";
97 const std::string& propertyValue =
98 "xyz.openbmc_project.State.BMC.Transition.HardReboot";
99 const char* destProperty = "RequestedBMCTransition";
100
101 // Create the D-Bus variant for D-Bus call.
George Liu9ae226f2023-06-21 17:56:46 +0800102 sdbusplus::asio::setProperty(
103 *crow::connections::systemBus, processName, objectPath, interfaceName,
104 destProperty, propertyValue,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800105 [asyncResp](const boost::system::error_code& ec) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400106 // Use "Set" method to set the property value.
107 if (ec)
108 {
109 BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
110 messages::internalError(asyncResp->res);
111 return;
112 }
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000113
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400114 messages::success(asyncResp->res);
115 });
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000116}
117
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500118/**
119 * ManagerResetAction class supports the POST method for the Reset (reboot)
120 * action.
121 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700122inline void requestRoutesManagerResetAction(App& app)
Jennifer Leeed5befb2018-08-10 11:29:45 -0700123{
Jennifer Leeed5befb2018-08-10 11:29:45 -0700124 /**
Jennifer Leeed5befb2018-08-10 11:29:45 -0700125 * Function handles POST method request.
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500126 * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000127 * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
Jennifer Leeed5befb2018-08-10 11:29:45 -0700128 */
Jennifer Leeed5befb2018-08-10 11:29:45 -0700129
Ed Tanous253f11b2024-05-16 09:38:31 -0700130 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/Actions/Manager.Reset/")
Ed Tanoused398212021-06-09 17:05:54 -0700131 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700132 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700133 [&app](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -0700134 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
135 const std::string& managerId) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400136 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
137 {
138 return;
139 }
140 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
141 {
142 messages::resourceNotFound(asyncResp->res, "Manager",
143 managerId);
144 return;
145 }
Ed Tanous253f11b2024-05-16 09:38:31 -0700146
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400147 BMCWEB_LOG_DEBUG("Post Manager Reset.");
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500148
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400149 std::string resetType;
Jennifer Leeed5befb2018-08-10 11:29:45 -0700150
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400151 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
152 resetType))
153 {
154 return;
155 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500156
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400157 if (resetType == "GracefulRestart")
158 {
159 BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
160 doBMCGracefulRestart(asyncResp);
161 return;
162 }
163 if (resetType == "ForceRestart")
164 {
165 BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
166 doBMCForceRestart(asyncResp);
167 return;
168 }
169 BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}",
170 resetType);
171 messages::actionParameterNotSupported(asyncResp->res, resetType,
172 "ResetType");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700173
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400174 return;
175 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700176}
Jennifer Leeed5befb2018-08-10 11:29:45 -0700177
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500178/**
179 * ManagerResetToDefaultsAction class supports POST method for factory reset
180 * action.
181 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700182inline void requestRoutesManagerResetToDefaultsAction(App& app)
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500183{
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500184 /**
185 * Function handles ResetToDefaults POST method request.
186 *
187 * Analyzes POST body message and factory resets BMC by calling
188 * BMC code updater factory reset followed by a BMC reboot.
189 *
190 * BMC code updater factory reset wipes the whole BMC read-write
191 * filesystem which includes things like the network settings.
192 *
193 * OpenBMC only supports ResetToDefaultsType "ResetAll".
194 */
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500195
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700196 BMCWEB_ROUTE(app,
Ed Tanous253f11b2024-05-16 09:38:31 -0700197 "/redfish/v1/Managers/<str>/Actions/Manager.ResetToDefaults/")
Ed Tanoused398212021-06-09 17:05:54 -0700198 .privileges(redfish::privileges::postManager)
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400199 .methods(
200 boost::beast::http::verb::
201 post)([&app](
202 const crow::Request& req,
203 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
204 const std::string& managerId) {
205 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700206 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700207 return;
208 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400209
210 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
211 {
212 messages::resourceNotFound(asyncResp->res, "Manager",
213 managerId);
214 return;
215 }
216
217 BMCWEB_LOG_DEBUG("Post ResetToDefaults.");
218
219 std::optional<std::string> resetType;
220 std::optional<std::string> resetToDefaultsType;
221
Myung Baeafc474a2024-10-09 00:53:29 -0700222 if (!json_util::readJsonAction( //
223 req, asyncResp->res, //
224 "ResetToDefaultsType", resetToDefaultsType, //
225 "ResetType", resetType //
226 ))
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400227 {
228 BMCWEB_LOG_DEBUG("Missing property ResetType.");
229
230 messages::actionParameterMissing(
231 asyncResp->res, "ResetToDefaults", "ResetType");
232 return;
233 }
234
235 if (resetToDefaultsType && !resetType)
236 {
237 BMCWEB_LOG_WARNING(
238 "Using deprecated ResetToDefaultsType, should be ResetType."
239 "Support for the ResetToDefaultsType will be dropped in 2Q24");
240 resetType = resetToDefaultsType;
241 }
242
243 if (resetType != "ResetAll")
244 {
245 BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}",
246 *resetType);
247 messages::actionParameterNotSupported(asyncResp->res,
248 *resetType, "ResetType");
249 return;
250 }
251
252 crow::connections::systemBus->async_method_call(
253 [asyncResp](const boost::system::error_code& ec) {
254 if (ec)
255 {
256 BMCWEB_LOG_DEBUG("Failed to ResetToDefaults: {}", ec);
257 messages::internalError(asyncResp->res);
258 return;
259 }
260 // Factory Reset doesn't actually happen until a reboot
261 // Can't erase what the BMC is running on
262 doBMCGracefulRestart(asyncResp);
263 },
Jagpal Singh Gilld27c31e2024-10-15 15:10:19 -0700264 getBMCUpdateServiceName(), getBMCUpdateServicePath(),
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400265 "xyz.openbmc_project.Common.FactoryReset", "Reset");
266 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700267}
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500268
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530269/**
270 * ManagerResetActionInfo derived class for delivering Manager
271 * ResetType AllowableValues using ResetInfo schema.
272 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700273inline void requestRoutesManagerResetActionInfo(App& app)
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530274{
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530275 /**
276 * Functions triggers appropriate requests on DBus
277 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700278
Ed Tanous253f11b2024-05-16 09:38:31 -0700279 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/ResetActionInfo/")
Ed Tanoused398212021-06-09 17:05:54 -0700280 .privileges(redfish::privileges::getActionInfo)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700281 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700282 [&app](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -0700283 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
284 const std::string& managerId) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400285 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
286 {
287 return;
288 }
Ed Tanous14766872022-03-15 10:44:42 -0700289
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400290 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
291 {
292 messages::resourceNotFound(asyncResp->res, "Manager",
293 managerId);
294 return;
295 }
Ed Tanous253f11b2024-05-16 09:38:31 -0700296
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400297 asyncResp->res.jsonValue["@odata.type"] =
298 "#ActionInfo.v1_1_2.ActionInfo";
299 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
300 "/redfish/v1/Managers/{}/ResetActionInfo",
301 BMCWEB_REDFISH_MANAGER_URI_NAME);
302 asyncResp->res.jsonValue["Name"] = "Reset Action Info";
303 asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
304 nlohmann::json::object_t parameter;
305 parameter["Name"] = "ResetType";
306 parameter["Required"] = true;
307 parameter["DataType"] = action_info::ParameterTypes::String;
Ed Tanous14766872022-03-15 10:44:42 -0700308
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400309 nlohmann::json::array_t allowableValues;
310 allowableValues.emplace_back("GracefulRestart");
311 allowableValues.emplace_back("ForceRestart");
312 parameter["AllowableValues"] = std::move(allowableValues);
Ed Tanous14766872022-03-15 10:44:42 -0700313
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400314 nlohmann::json::array_t parameters;
315 parameters.emplace_back(std::move(parameter));
Ed Tanous14766872022-03-15 10:44:42 -0700316
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400317 asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
318 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700319}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530320
James Feist5b4aa862018-08-16 14:07:01 -0700321static constexpr const char* objectManagerIface =
322 "org.freedesktop.DBus.ObjectManager";
323static constexpr const char* pidConfigurationIface =
324 "xyz.openbmc_project.Configuration.Pid";
325static constexpr const char* pidZoneConfigurationIface =
326 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feistb7a08d02018-12-11 14:55:37 -0800327static constexpr const char* stepwiseConfigurationIface =
328 "xyz.openbmc_project.Configuration.Stepwise";
James Feist73df0db2019-03-25 15:29:35 -0700329static constexpr const char* thermalModeIface =
330 "xyz.openbmc_project.Control.ThermalMode";
Borawski.Lukasz9c3106852018-02-09 15:24:22 +0100331
zhanghch058d1b46d2021-04-01 11:18:24 +0800332inline void
333 asyncPopulatePid(const std::string& connection, const std::string& path,
334 const std::string& currentProfile,
335 const std::vector<std::string>& supportedProfiles,
336 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
James Feist5b4aa862018-08-16 14:07:01 -0700337{
George Liu5eb468d2023-06-20 17:03:24 +0800338 sdbusplus::message::object_path objPath(path);
339 dbus::utility::getManagedObjects(
340 connection, objPath,
James Feist73df0db2019-03-25 15:29:35 -0700341 [asyncResp, currentProfile, supportedProfiles](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800342 const boost::system::error_code& ec,
James Feist73df0db2019-03-25 15:29:35 -0700343 const dbus::utility::ManagedObjectType& managedObj) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400344 if (ec)
James Feist5b4aa862018-08-16 14:07:01 -0700345 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400346 BMCWEB_LOG_ERROR("{}", ec);
347 messages::internalError(asyncResp->res);
348 return;
349 }
350 nlohmann::json& configRoot =
351 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
352 nlohmann::json& fans = configRoot["FanControllers"];
353 fans["@odata.type"] =
354 "#OpenBMCManager.v1_0_0.Manager.FanControllers";
355 fans["@odata.id"] = boost::urls::format(
356 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/FanControllers",
357 BMCWEB_REDFISH_MANAGER_URI_NAME);
358
359 nlohmann::json& pids = configRoot["PidControllers"];
360 pids["@odata.type"] =
361 "#OpenBMCManager.v1_0_0.Manager.PidControllers";
362 pids["@odata.id"] = boost::urls::format(
363 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/PidControllers",
364 BMCWEB_REDFISH_MANAGER_URI_NAME);
365
366 nlohmann::json& stepwise = configRoot["StepwiseControllers"];
367 stepwise["@odata.type"] =
368 "#OpenBMCManager.v1_0_0.Manager.StepwiseControllers";
369 stepwise["@odata.id"] = boost::urls::format(
370 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/StepwiseControllers",
371 BMCWEB_REDFISH_MANAGER_URI_NAME);
372
373 nlohmann::json& zones = configRoot["FanZones"];
374 zones["@odata.id"] = boost::urls::format(
375 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/FanZones",
376 BMCWEB_REDFISH_MANAGER_URI_NAME);
377 zones["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager.FanZones";
378 configRoot["@odata.id"] =
379 boost::urls::format("/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan",
380 BMCWEB_REDFISH_MANAGER_URI_NAME);
381 configRoot["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager.Fan";
382 configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
383
384 if (!currentProfile.empty())
385 {
386 configRoot["Profile"] = currentProfile;
387 }
388 BMCWEB_LOG_DEBUG("profile = {} !", currentProfile);
389
390 for (const auto& pathPair : managedObj)
391 {
392 for (const auto& intfPair : pathPair.second)
James Feist5b4aa862018-08-16 14:07:01 -0700393 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400394 if (intfPair.first != pidConfigurationIface &&
395 intfPair.first != pidZoneConfigurationIface &&
396 intfPair.first != stepwiseConfigurationIface)
Ed Tanous002d39b2022-05-31 08:59:27 -0700397 {
398 continue;
399 }
400
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400401 std::string name;
402
403 for (const std::pair<std::string,
404 dbus::utility::DbusVariantType>&
405 propPair : intfPair.second)
406 {
407 if (propPair.first == "Name")
408 {
409 const std::string* namePtr =
410 std::get_if<std::string>(&propPair.second);
411 if (namePtr == nullptr)
412 {
413 BMCWEB_LOG_ERROR("Pid Name Field illegal");
414 messages::internalError(asyncResp->res);
415 return;
416 }
417 name = *namePtr;
418 dbus::utility::escapePathForDbus(name);
419 }
420 else if (propPair.first == "Profiles")
421 {
422 const std::vector<std::string>* profiles =
423 std::get_if<std::vector<std::string>>(
424 &propPair.second);
425 if (profiles == nullptr)
426 {
427 BMCWEB_LOG_ERROR("Pid Profiles Field illegal");
428 messages::internalError(asyncResp->res);
429 return;
430 }
431 if (std::find(profiles->begin(), profiles->end(),
432 currentProfile) == profiles->end())
433 {
434 BMCWEB_LOG_INFO(
435 "{} not supported in current profile",
436 name);
437 continue;
438 }
439 }
440 }
441 nlohmann::json* config = nullptr;
442 const std::string* classPtr = nullptr;
443
444 for (const std::pair<std::string,
445 dbus::utility::DbusVariantType>&
446 propPair : intfPair.second)
447 {
448 if (propPair.first == "Class")
449 {
450 classPtr =
451 std::get_if<std::string>(&propPair.second);
452 }
453 }
454
455 boost::urls::url url(
456 boost::urls::format("/redfish/v1/Managers/{}",
457 BMCWEB_REDFISH_MANAGER_URI_NAME));
Ed Tanous002d39b2022-05-31 08:59:27 -0700458 if (intfPair.first == pidZoneConfigurationIface)
459 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400460 std::string chassis;
461 if (!dbus::utility::getNthStringFromPath(
462 pathPair.first.str, 5, chassis))
Ed Tanous002d39b2022-05-31 08:59:27 -0700463 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400464 chassis = "#IllegalValue";
465 }
466 nlohmann::json& zone = zones[name];
467 zone["Chassis"]["@odata.id"] = boost::urls::format(
468 "/redfish/v1/Chassis/{}", chassis);
469 url.set_fragment(
470 ("/Oem/OpenBmc/Fan/FanZones"_json_pointer / name)
471 .to_string());
472 zone["@odata.id"] = std::move(url);
473 zone["@odata.type"] =
474 "#OpenBMCManager.v1_0_0.Manager.FanZone";
475 config = &zone;
476 }
477
478 else if (intfPair.first == stepwiseConfigurationIface)
479 {
480 if (classPtr == nullptr)
481 {
482 BMCWEB_LOG_ERROR("Pid Class Field illegal");
Ed Tanous002d39b2022-05-31 08:59:27 -0700483 messages::internalError(asyncResp->res);
484 return;
485 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700486
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400487 nlohmann::json& controller = stepwise[name];
488 config = &controller;
489 url.set_fragment(
490 ("/Oem/OpenBmc/Fan/StepwiseControllers"_json_pointer /
491 name)
492 .to_string());
493 controller["@odata.id"] = std::move(url);
494 controller["@odata.type"] =
495 "#OpenBMCManager.v1_0_0.Manager.StepwiseController";
Ed Tanous002d39b2022-05-31 08:59:27 -0700496
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400497 controller["Direction"] = *classPtr;
Ed Tanous002d39b2022-05-31 08:59:27 -0700498 }
James Feistb7a08d02018-12-11 14:55:37 -0800499
Ed Tanous002d39b2022-05-31 08:59:27 -0700500 // pid and fans are off the same configuration
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400501 else if (intfPair.first == pidConfigurationIface)
Ed Tanous002d39b2022-05-31 08:59:27 -0700502 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400503 if (classPtr == nullptr)
James Feistb7a08d02018-12-11 14:55:37 -0800504 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400505 BMCWEB_LOG_ERROR("Pid Class Field illegal");
506 messages::internalError(asyncResp->res);
507 return;
James Feist5b4aa862018-08-16 14:07:01 -0700508 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400509 bool isFan = *classPtr == "fan";
510 nlohmann::json& element =
511 isFan ? fans[name] : pids[name];
512 config = &element;
513 if (isFan)
James Feist5b4aa862018-08-16 14:07:01 -0700514 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400515 url.set_fragment(
516 ("/Oem/OpenBmc/Fan/FanControllers"_json_pointer /
517 name)
518 .to_string());
519 element["@odata.id"] = std::move(url);
520 element["@odata.type"] =
521 "#OpenBMCManager.v1_0_0.Manager.FanController";
Ed Tanous002d39b2022-05-31 08:59:27 -0700522 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400523 else
Ed Tanous002d39b2022-05-31 08:59:27 -0700524 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400525 url.set_fragment(
526 ("/Oem/OpenBmc/Fan/PidControllers"_json_pointer /
527 name)
528 .to_string());
529 element["@odata.id"] = std::move(url);
530 element["@odata.type"] =
531 "#OpenBMCManager.v1_0_0.Manager.PidController";
Ed Tanous002d39b2022-05-31 08:59:27 -0700532 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400533 }
534 else
535 {
536 BMCWEB_LOG_ERROR("Unexpected configuration");
537 messages::internalError(asyncResp->res);
538 return;
539 }
540
541 // used for making maps out of 2 vectors
542 const std::vector<double>* keys = nullptr;
543 const std::vector<double>* values = nullptr;
544
545 for (const auto& propertyPair : intfPair.second)
546 {
547 if (propertyPair.first == "Type" ||
548 propertyPair.first == "Class" ||
549 propertyPair.first == "Name")
550 {
551 continue;
552 }
553
554 // zones
555 if (intfPair.first == pidZoneConfigurationIface)
Ed Tanous002d39b2022-05-31 08:59:27 -0700556 {
557 const double* ptr =
558 std::get_if<double>(&propertyPair.second);
559 if (ptr == nullptr)
560 {
Ed Tanous62598e32023-07-17 17:06:25 -0700561 BMCWEB_LOG_ERROR("Field Illegal {}",
562 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700563 messages::internalError(asyncResp->res);
564 return;
565 }
566 (*config)[propertyPair.first] = *ptr;
James Feist5b4aa862018-08-16 14:07:01 -0700567 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400568
569 if (intfPair.first == stepwiseConfigurationIface)
570 {
571 if (propertyPair.first == "Reading" ||
572 propertyPair.first == "Output")
573 {
574 const std::vector<double>* ptr =
575 std::get_if<std::vector<double>>(
576 &propertyPair.second);
577
578 if (ptr == nullptr)
579 {
580 BMCWEB_LOG_ERROR("Field Illegal {}",
581 propertyPair.first);
582 messages::internalError(asyncResp->res);
583 return;
584 }
585
586 if (propertyPair.first == "Reading")
587 {
588 keys = ptr;
589 }
590 else
591 {
592 values = ptr;
593 }
594 if (keys != nullptr && values != nullptr)
595 {
596 if (keys->size() != values->size())
597 {
598 BMCWEB_LOG_ERROR(
599 "Reading and Output size don't match ");
600 messages::internalError(asyncResp->res);
601 return;
602 }
603 nlohmann::json& steps = (*config)["Steps"];
604 steps = nlohmann::json::array();
605 for (size_t ii = 0; ii < keys->size(); ii++)
606 {
607 nlohmann::json::object_t step;
608 step["Target"] = (*keys)[ii];
609 step["Output"] = (*values)[ii];
610 steps.emplace_back(std::move(step));
611 }
612 }
613 }
614 if (propertyPair.first == "NegativeHysteresis" ||
615 propertyPair.first == "PositiveHysteresis")
616 {
617 const double* ptr =
618 std::get_if<double>(&propertyPair.second);
619 if (ptr == nullptr)
620 {
621 BMCWEB_LOG_ERROR("Field Illegal {}",
622 propertyPair.first);
623 messages::internalError(asyncResp->res);
624 return;
625 }
626 (*config)[propertyPair.first] = *ptr;
627 }
628 }
629
630 // pid and fans are off the same configuration
631 if (intfPair.first == pidConfigurationIface ||
632 intfPair.first == stepwiseConfigurationIface)
633 {
634 if (propertyPair.first == "Zones")
635 {
636 const std::vector<std::string>* inputs =
637 std::get_if<std::vector<std::string>>(
638 &propertyPair.second);
639
640 if (inputs == nullptr)
641 {
642 BMCWEB_LOG_ERROR("Zones Pid Field Illegal");
643 messages::internalError(asyncResp->res);
644 return;
645 }
646 auto& data = (*config)[propertyPair.first];
647 data = nlohmann::json::array();
648 for (std::string itemCopy : *inputs)
649 {
650 dbus::utility::escapePathForDbus(itemCopy);
651 nlohmann::json::object_t input;
652 boost::urls::url managerUrl =
653 boost::urls::format(
654 "/redfish/v1/Managers/{}#{}",
655 BMCWEB_REDFISH_MANAGER_URI_NAME,
656 ("/Oem/OpenBmc/Fan/FanZones"_json_pointer /
657 itemCopy)
658 .to_string());
659 input["@odata.id"] = std::move(managerUrl);
660 data.emplace_back(std::move(input));
661 }
662 }
663 // todo(james): may never happen, but this
664 // assumes configuration data referenced in the
665 // PID config is provided by the same daemon, we
666 // could add another loop to cover all cases,
667 // but I'm okay kicking this can down the road a
668 // bit
669
670 else if (propertyPair.first == "Inputs" ||
671 propertyPair.first == "Outputs")
672 {
673 auto& data = (*config)[propertyPair.first];
674 const std::vector<std::string>* inputs =
675 std::get_if<std::vector<std::string>>(
676 &propertyPair.second);
677
678 if (inputs == nullptr)
679 {
680 BMCWEB_LOG_ERROR("Field Illegal {}",
681 propertyPair.first);
682 messages::internalError(asyncResp->res);
683 return;
684 }
685 data = *inputs;
686 }
687 else if (propertyPair.first == "SetPointOffset")
688 {
689 const std::string* ptr =
690 std::get_if<std::string>(
691 &propertyPair.second);
692
693 if (ptr == nullptr)
694 {
695 BMCWEB_LOG_ERROR("Field Illegal {}",
696 propertyPair.first);
697 messages::internalError(asyncResp->res);
698 return;
699 }
700 // translate from dbus to redfish
701 if (*ptr == "WarningHigh")
702 {
703 (*config)["SetPointOffset"] =
704 "UpperThresholdNonCritical";
705 }
706 else if (*ptr == "WarningLow")
707 {
708 (*config)["SetPointOffset"] =
709 "LowerThresholdNonCritical";
710 }
711 else if (*ptr == "CriticalHigh")
712 {
713 (*config)["SetPointOffset"] =
714 "UpperThresholdCritical";
715 }
716 else if (*ptr == "CriticalLow")
717 {
718 (*config)["SetPointOffset"] =
719 "LowerThresholdCritical";
720 }
721 else
722 {
723 BMCWEB_LOG_ERROR("Value Illegal {}", *ptr);
724 messages::internalError(asyncResp->res);
725 return;
726 }
727 }
728 // doubles
729 else if (propertyPair.first ==
730 "FFGainCoefficient" ||
731 propertyPair.first == "FFOffCoefficient" ||
732 propertyPair.first == "ICoefficient" ||
733 propertyPair.first == "ILimitMax" ||
734 propertyPair.first == "ILimitMin" ||
735 propertyPair.first ==
736 "PositiveHysteresis" ||
737 propertyPair.first ==
738 "NegativeHysteresis" ||
739 propertyPair.first == "OutLimitMax" ||
740 propertyPair.first == "OutLimitMin" ||
741 propertyPair.first == "PCoefficient" ||
742 propertyPair.first == "SetPoint" ||
743 propertyPair.first == "SlewNeg" ||
744 propertyPair.first == "SlewPos")
745 {
746 const double* ptr =
747 std::get_if<double>(&propertyPair.second);
748 if (ptr == nullptr)
749 {
750 BMCWEB_LOG_ERROR("Field Illegal {}",
751 propertyPair.first);
752 messages::internalError(asyncResp->res);
753 return;
754 }
755 (*config)[propertyPair.first] = *ptr;
756 }
757 }
James Feist5b4aa862018-08-16 14:07:01 -0700758 }
759 }
760 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400761 });
James Feist5b4aa862018-08-16 14:07:01 -0700762}
Jennifer Leeca537922018-08-10 10:07:30 -0700763
James Feist83ff9ab2018-08-31 10:18:24 -0700764enum class CreatePIDRet
765{
766 fail,
767 del,
768 patch
769};
770
zhanghch058d1b46d2021-04-01 11:18:24 +0800771inline bool
772 getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
Ed Tanous9e9b6042024-03-06 14:18:28 -0800773 std::vector<nlohmann::json::object_t>& config,
zhanghch058d1b46d2021-04-01 11:18:24 +0800774 std::vector<std::string>& zones)
James Feist5f2caae2018-12-12 14:08:25 -0800775{
James Feistb6baeaa2019-02-21 10:41:40 -0800776 if (config.empty())
777 {
Ed Tanous62598e32023-07-17 17:06:25 -0700778 BMCWEB_LOG_ERROR("Empty Zones");
Ed Tanousf818b042022-06-27 13:17:35 -0700779 messages::propertyValueFormatError(response->res, config, "Zones");
James Feistb6baeaa2019-02-21 10:41:40 -0800780 return false;
781 }
James Feist5f2caae2018-12-12 14:08:25 -0800782 for (auto& odata : config)
783 {
784 std::string path;
Ed Tanous9e9b6042024-03-06 14:18:28 -0800785 if (!redfish::json_util::readJsonObject(odata, response->res,
786 "@odata.id", path))
James Feist5f2caae2018-12-12 14:08:25 -0800787 {
788 return false;
789 }
790 std::string input;
James Feist61adbda2019-03-25 13:03:51 -0700791
792 // 8 below comes from
793 // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
794 // 0 1 2 3 4 5 6 7 8
795 if (!dbus::utility::getNthStringFromPath(path, 8, input))
James Feist5f2caae2018-12-12 14:08:25 -0800796 {
Ed Tanous62598e32023-07-17 17:06:25 -0700797 BMCWEB_LOG_ERROR("Got invalid path {}", path);
798 BMCWEB_LOG_ERROR("Illegal Type Zones");
Ed Tanousf818b042022-06-27 13:17:35 -0700799 messages::propertyValueFormatError(response->res, odata, "Zones");
James Feist5f2caae2018-12-12 14:08:25 -0800800 return false;
801 }
Ed Tanousa170f272022-06-30 21:53:27 -0700802 std::replace(input.begin(), input.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -0800803 zones.emplace_back(std::move(input));
804 }
805 return true;
806}
807
Ed Tanous711ac7a2021-12-20 09:34:41 -0800808inline const dbus::utility::ManagedObjectType::value_type*
James Feist73df0db2019-03-25 15:29:35 -0700809 findChassis(const dbus::utility::ManagedObjectType& managedObj,
Ed Tanous9e9b6042024-03-06 14:18:28 -0800810 std::string_view value, std::string& chassis)
James Feistb6baeaa2019-02-21 10:41:40 -0800811{
Ed Tanous62598e32023-07-17 17:06:25 -0700812 BMCWEB_LOG_DEBUG("Find Chassis: {}", value);
James Feistb6baeaa2019-02-21 10:41:40 -0800813
Ed Tanous9e9b6042024-03-06 14:18:28 -0800814 std::string escaped(value);
Yaswanth Reddy M6ce82fa2023-03-10 07:29:45 +0000815 std::replace(escaped.begin(), escaped.end(), ' ', '_');
James Feistb6baeaa2019-02-21 10:41:40 -0800816 escaped = "/" + escaped;
Ed Tanous3544d2a2023-08-06 18:12:20 -0700817 auto it = std::ranges::find_if(managedObj, [&escaped](const auto& obj) {
Ed Tanous18f8f602023-07-18 10:07:23 -0700818 if (obj.first.str.ends_with(escaped))
Ed Tanous002d39b2022-05-31 08:59:27 -0700819 {
Ed Tanous62598e32023-07-17 17:06:25 -0700820 BMCWEB_LOG_DEBUG("Matched {}", obj.first.str);
Ed Tanous002d39b2022-05-31 08:59:27 -0700821 return true;
822 }
823 return false;
824 });
James Feistb6baeaa2019-02-21 10:41:40 -0800825
826 if (it == managedObj.end())
827 {
James Feist73df0db2019-03-25 15:29:35 -0700828 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800829 }
830 // 5 comes from <chassis-name> being the 5th element
831 // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
James Feist73df0db2019-03-25 15:29:35 -0700832 if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
833 {
834 return &(*it);
835 }
836
837 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800838}
839
Ed Tanous23a21a12020-07-25 04:45:05 +0000840inline CreatePIDRet createPidInterface(
zhanghch058d1b46d2021-04-01 11:18:24 +0800841 const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
Ed Tanous9e9b6042024-03-06 14:18:28 -0800842 std::string_view name, nlohmann::json& jsonValue, const std::string& path,
James Feist83ff9ab2018-08-31 10:18:24 -0700843 const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800844 dbus::utility::DBusPropertiesMap& output, std::string& chassis,
845 const std::string& profile)
James Feist83ff9ab2018-08-31 10:18:24 -0700846{
James Feist5f2caae2018-12-12 14:08:25 -0800847 // common deleter
Ed Tanous9e9b6042024-03-06 14:18:28 -0800848 if (jsonValue == nullptr)
James Feist5f2caae2018-12-12 14:08:25 -0800849 {
850 std::string iface;
851 if (type == "PidControllers" || type == "FanControllers")
852 {
853 iface = pidConfigurationIface;
854 }
855 else if (type == "FanZones")
856 {
857 iface = pidZoneConfigurationIface;
858 }
859 else if (type == "StepwiseControllers")
860 {
861 iface = stepwiseConfigurationIface;
862 }
863 else
864 {
Ed Tanous62598e32023-07-17 17:06:25 -0700865 BMCWEB_LOG_ERROR("Illegal Type {}", type);
James Feist5f2caae2018-12-12 14:08:25 -0800866 messages::propertyUnknown(response->res, type);
867 return CreatePIDRet::fail;
868 }
James Feist6ee7f772020-02-06 16:25:27 -0800869
Ed Tanous62598e32023-07-17 17:06:25 -0700870 BMCWEB_LOG_DEBUG("del {} {}", path, iface);
James Feist5f2caae2018-12-12 14:08:25 -0800871 // delete interface
872 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800873 [response, path](const boost::system::error_code& ec) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400874 if (ec)
875 {
876 BMCWEB_LOG_ERROR("Error patching {}: {}", path, ec);
877 messages::internalError(response->res);
878 return;
879 }
880 messages::success(response->res);
881 },
James Feist5f2caae2018-12-12 14:08:25 -0800882 "xyz.openbmc_project.EntityManager", path, iface, "Delete");
883 return CreatePIDRet::del;
884 }
885
Ed Tanous711ac7a2021-12-20 09:34:41 -0800886 const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800887 if (!createNewObject)
888 {
889 // if we aren't creating a new object, we should be able to find it on
890 // d-bus
Ed Tanous9e9b6042024-03-06 14:18:28 -0800891 managedItem = findChassis(managedObj, name, chassis);
James Feist73df0db2019-03-25 15:29:35 -0700892 if (managedItem == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800893 {
Ed Tanous62598e32023-07-17 17:06:25 -0700894 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousef4c65b2023-04-24 15:28:50 -0700895 messages::invalidObject(
896 response->res,
897 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800898 return CreatePIDRet::fail;
899 }
900 }
901
Ed Tanous26f69762022-01-25 09:49:11 -0800902 if (!profile.empty() &&
James Feist73df0db2019-03-25 15:29:35 -0700903 (type == "PidControllers" || type == "FanControllers" ||
904 type == "StepwiseControllers"))
905 {
906 if (managedItem == nullptr)
907 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800908 output.emplace_back("Profiles", std::vector<std::string>{profile});
James Feist73df0db2019-03-25 15:29:35 -0700909 }
910 else
911 {
912 std::string interface;
913 if (type == "StepwiseControllers")
914 {
915 interface = stepwiseConfigurationIface;
916 }
917 else
918 {
919 interface = pidConfigurationIface;
920 }
Ed Tanous711ac7a2021-12-20 09:34:41 -0800921 bool ifaceFound = false;
922 for (const auto& iface : managedItem->second)
923 {
924 if (iface.first == interface)
925 {
926 ifaceFound = true;
927 for (const auto& prop : iface.second)
928 {
929 if (prop.first == "Profiles")
930 {
931 const std::vector<std::string>* curProfiles =
932 std::get_if<std::vector<std::string>>(
933 &(prop.second));
934 if (curProfiles == nullptr)
935 {
Ed Tanous62598e32023-07-17 17:06:25 -0700936 BMCWEB_LOG_ERROR(
937 "Illegal profiles in managed object");
Ed Tanous711ac7a2021-12-20 09:34:41 -0800938 messages::internalError(response->res);
939 return CreatePIDRet::fail;
940 }
941 if (std::find(curProfiles->begin(),
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400942 curProfiles->end(), profile) ==
943 curProfiles->end())
Ed Tanous711ac7a2021-12-20 09:34:41 -0800944 {
945 std::vector<std::string> newProfiles =
946 *curProfiles;
947 newProfiles.push_back(profile);
Ed Tanousb9d36b42022-02-26 21:42:46 -0800948 output.emplace_back("Profiles", newProfiles);
Ed Tanous711ac7a2021-12-20 09:34:41 -0800949 }
950 }
951 }
952 }
953 }
954
955 if (!ifaceFound)
James Feist73df0db2019-03-25 15:29:35 -0700956 {
Ed Tanous62598e32023-07-17 17:06:25 -0700957 BMCWEB_LOG_ERROR("Failed to find interface in managed object");
James Feist73df0db2019-03-25 15:29:35 -0700958 messages::internalError(response->res);
959 return CreatePIDRet::fail;
960 }
James Feist73df0db2019-03-25 15:29:35 -0700961 }
962 }
963
James Feist83ff9ab2018-08-31 10:18:24 -0700964 if (type == "PidControllers" || type == "FanControllers")
965 {
966 if (createNewObject)
967 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800968 output.emplace_back("Class",
969 type == "PidControllers" ? "temp" : "fan");
970 output.emplace_back("Type", "Pid");
James Feist83ff9ab2018-08-31 10:18:24 -0700971 }
James Feist5f2caae2018-12-12 14:08:25 -0800972
Ed Tanous9e9b6042024-03-06 14:18:28 -0800973 std::optional<std::vector<nlohmann::json::object_t>> zones;
James Feist5f2caae2018-12-12 14:08:25 -0800974 std::optional<std::vector<std::string>> inputs;
975 std::optional<std::vector<std::string>> outputs;
976 std::map<std::string, std::optional<double>> doubles;
James Feistb943aae2019-07-11 16:33:56 -0700977 std::optional<std::string> setpointOffset;
Myung Baeafc474a2024-10-09 00:53:29 -0700978 if (!redfish::json_util::readJson( //
979 jsonValue, response->res, //
980 "FFGainCoefficient", doubles["FFGainCoefficient"], //
981 "FFOffCoefficient", doubles["FFOffCoefficient"], //
982 "ICoefficient", doubles["ICoefficient"], //
983 "ILimitMax", doubles["ILimitMax"], //
984 "ILimitMin", doubles["ILimitMin"], //
985 "Inputs", inputs, //
986 "NegativeHysteresis", doubles["NegativeHysteresis"], //
987 "OutLimitMax", doubles["OutLimitMax"], //
988 "OutLimitMin", doubles["OutLimitMin"], //
989 "Outputs", outputs, //
990 "PCoefficient", doubles["PCoefficient"], //
991 "PositiveHysteresis", doubles["PositiveHysteresis"], //
992 "SetPoint", doubles["SetPoint"], //
993 "SetPointOffset", setpointOffset, //
994 "SlewNeg", doubles["SlewNeg"], //
995 "SlewPos", doubles["SlewPos"], //
996 "Zones", zones //
997 ))
James Feist83ff9ab2018-08-31 10:18:24 -0700998 {
James Feist5f2caae2018-12-12 14:08:25 -0800999 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -07001000 }
Myung Baeafc474a2024-10-09 00:53:29 -07001001
James Feist5f2caae2018-12-12 14:08:25 -08001002 if (zones)
James Feist83ff9ab2018-08-31 10:18:24 -07001003 {
James Feist5f2caae2018-12-12 14:08:25 -08001004 std::vector<std::string> zonesStr;
1005 if (!getZonesFromJsonReq(response, *zones, zonesStr))
James Feist83ff9ab2018-08-31 10:18:24 -07001006 {
Ed Tanous62598e32023-07-17 17:06:25 -07001007 BMCWEB_LOG_ERROR("Illegal Zones");
James Feist5f2caae2018-12-12 14:08:25 -08001008 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -07001009 }
James Feistb6baeaa2019-02-21 10:41:40 -08001010 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -08001011 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -08001012 {
Ed Tanous62598e32023-07-17 17:06:25 -07001013 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousace85d62021-10-26 12:45:59 -07001014 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001015 response->res,
1016 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -08001017 return CreatePIDRet::fail;
1018 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001019 output.emplace_back("Zones", std::move(zonesStr));
James Feist5f2caae2018-12-12 14:08:25 -08001020 }
Ed Tanousafb9ee02022-12-21 11:59:17 -08001021
1022 if (inputs)
James Feist5f2caae2018-12-12 14:08:25 -08001023 {
Ed Tanousafb9ee02022-12-21 11:59:17 -08001024 for (std::string& value : *inputs)
James Feist83ff9ab2018-08-31 10:18:24 -07001025 {
Ed Tanousafb9ee02022-12-21 11:59:17 -08001026 std::replace(value.begin(), value.end(), '_', ' ');
James Feist83ff9ab2018-08-31 10:18:24 -07001027 }
Ed Tanousafb9ee02022-12-21 11:59:17 -08001028 output.emplace_back("Inputs", *inputs);
1029 }
1030
1031 if (outputs)
1032 {
1033 for (std::string& value : *outputs)
1034 {
1035 std::replace(value.begin(), value.end(), '_', ' ');
1036 }
1037 output.emplace_back("Outputs", *outputs);
James Feist5f2caae2018-12-12 14:08:25 -08001038 }
James Feist83ff9ab2018-08-31 10:18:24 -07001039
James Feistb943aae2019-07-11 16:33:56 -07001040 if (setpointOffset)
1041 {
1042 // translate between redfish and dbus names
1043 if (*setpointOffset == "UpperThresholdNonCritical")
1044 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001045 output.emplace_back("SetPointOffset", "WarningLow");
James Feistb943aae2019-07-11 16:33:56 -07001046 }
1047 else if (*setpointOffset == "LowerThresholdNonCritical")
1048 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001049 output.emplace_back("SetPointOffset", "WarningHigh");
James Feistb943aae2019-07-11 16:33:56 -07001050 }
1051 else if (*setpointOffset == "LowerThresholdCritical")
1052 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001053 output.emplace_back("SetPointOffset", "CriticalLow");
James Feistb943aae2019-07-11 16:33:56 -07001054 }
1055 else if (*setpointOffset == "UpperThresholdCritical")
1056 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001057 output.emplace_back("SetPointOffset", "CriticalHigh");
James Feistb943aae2019-07-11 16:33:56 -07001058 }
1059 else
1060 {
Ed Tanous62598e32023-07-17 17:06:25 -07001061 BMCWEB_LOG_ERROR("Invalid setpointoffset {}", *setpointOffset);
Ed Tanous9e9b6042024-03-06 14:18:28 -08001062 messages::propertyValueNotInList(response->res, name,
Ed Tanousace85d62021-10-26 12:45:59 -07001063 "SetPointOffset");
James Feistb943aae2019-07-11 16:33:56 -07001064 return CreatePIDRet::fail;
1065 }
1066 }
1067
James Feist5f2caae2018-12-12 14:08:25 -08001068 // doubles
1069 for (const auto& pairs : doubles)
1070 {
1071 if (!pairs.second)
James Feist83ff9ab2018-08-31 10:18:24 -07001072 {
James Feist5f2caae2018-12-12 14:08:25 -08001073 continue;
James Feist83ff9ab2018-08-31 10:18:24 -07001074 }
Ed Tanous62598e32023-07-17 17:06:25 -07001075 BMCWEB_LOG_DEBUG("{} = {}", pairs.first, *pairs.second);
Ed Tanousb9d36b42022-02-26 21:42:46 -08001076 output.emplace_back(pairs.first, *pairs.second);
James Feist83ff9ab2018-08-31 10:18:24 -07001077 }
1078 }
James Feist5f2caae2018-12-12 14:08:25 -08001079
James Feist83ff9ab2018-08-31 10:18:24 -07001080 else if (type == "FanZones")
1081 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001082 output.emplace_back("Type", "Pid.Zone");
James Feist83ff9ab2018-08-31 10:18:24 -07001083
Ed Tanous9e9b6042024-03-06 14:18:28 -08001084 std::optional<std::string> chassisId;
James Feist5f2caae2018-12-12 14:08:25 -08001085 std::optional<double> failSafePercent;
James Feistd3ec07f2019-02-25 14:51:15 -08001086 std::optional<double> minThermalOutput;
Myung Baeafc474a2024-10-09 00:53:29 -07001087 if (!redfish::json_util::readJson( //
1088 jsonValue, response->res, //
1089 "Chassis/@odata.id", chassisId, //
1090 "FailSafePercent", failSafePercent, //
1091 "MinThermalOutput", minThermalOutput))
James Feist83ff9ab2018-08-31 10:18:24 -07001092 {
James Feist5f2caae2018-12-12 14:08:25 -08001093 return CreatePIDRet::fail;
1094 }
James Feist83ff9ab2018-08-31 10:18:24 -07001095
Ed Tanous9e9b6042024-03-06 14:18:28 -08001096 if (chassisId)
James Feist5f2caae2018-12-12 14:08:25 -08001097 {
AppaRao Puli717794d2019-10-18 22:54:53 +05301098 // /redfish/v1/chassis/chassis_name/
Ed Tanous9e9b6042024-03-06 14:18:28 -08001099 if (!dbus::utility::getNthStringFromPath(*chassisId, 3, chassis))
James Feist5f2caae2018-12-12 14:08:25 -08001100 {
Ed Tanous9e9b6042024-03-06 14:18:28 -08001101 BMCWEB_LOG_ERROR("Got invalid path {}", *chassisId);
Ed Tanousace85d62021-10-26 12:45:59 -07001102 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001103 response->res,
Ed Tanous9e9b6042024-03-06 14:18:28 -08001104 boost::urls::format("/redfish/v1/Chassis/{}", *chassisId));
James Feist5f2caae2018-12-12 14:08:25 -08001105 return CreatePIDRet::fail;
1106 }
1107 }
James Feistd3ec07f2019-02-25 14:51:15 -08001108 if (minThermalOutput)
James Feist5f2caae2018-12-12 14:08:25 -08001109 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001110 output.emplace_back("MinThermalOutput", *minThermalOutput);
James Feist5f2caae2018-12-12 14:08:25 -08001111 }
1112 if (failSafePercent)
1113 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001114 output.emplace_back("FailSafePercent", *failSafePercent);
James Feist5f2caae2018-12-12 14:08:25 -08001115 }
1116 }
1117 else if (type == "StepwiseControllers")
1118 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001119 output.emplace_back("Type", "Stepwise");
James Feist5f2caae2018-12-12 14:08:25 -08001120
Ed Tanous9e9b6042024-03-06 14:18:28 -08001121 std::optional<std::vector<nlohmann::json::object_t>> zones;
1122 std::optional<std::vector<nlohmann::json::object_t>> steps;
James Feist5f2caae2018-12-12 14:08:25 -08001123 std::optional<std::vector<std::string>> inputs;
1124 std::optional<double> positiveHysteresis;
1125 std::optional<double> negativeHysteresis;
James Feistc33a90e2019-03-01 10:17:44 -08001126 std::optional<std::string> direction; // upper clipping curve vs lower
Myung Baeafc474a2024-10-09 00:53:29 -07001127 if (!redfish::json_util::readJson( //
1128 jsonValue, response->res, //
1129 "Direction", direction, //
1130 "Inputs", inputs, //
1131 "NegativeHysteresis", negativeHysteresis, //
1132 "PositiveHysteresis", positiveHysteresis, //
1133 "Steps", steps, //
1134 "Zones", zones //
1135 ))
James Feist5f2caae2018-12-12 14:08:25 -08001136 {
James Feist5f2caae2018-12-12 14:08:25 -08001137 return CreatePIDRet::fail;
1138 }
1139
1140 if (zones)
1141 {
James Feistb6baeaa2019-02-21 10:41:40 -08001142 std::vector<std::string> zonesStrs;
1143 if (!getZonesFromJsonReq(response, *zones, zonesStrs))
James Feist5f2caae2018-12-12 14:08:25 -08001144 {
Ed Tanous62598e32023-07-17 17:06:25 -07001145 BMCWEB_LOG_ERROR("Illegal Zones");
James Feist5f2caae2018-12-12 14:08:25 -08001146 return CreatePIDRet::fail;
1147 }
James Feistb6baeaa2019-02-21 10:41:40 -08001148 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -08001149 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -08001150 {
Ed Tanous62598e32023-07-17 17:06:25 -07001151 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousace85d62021-10-26 12:45:59 -07001152 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001153 response->res,
1154 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -08001155 return CreatePIDRet::fail;
1156 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001157 output.emplace_back("Zones", std::move(zonesStrs));
James Feist5f2caae2018-12-12 14:08:25 -08001158 }
1159 if (steps)
1160 {
1161 std::vector<double> readings;
1162 std::vector<double> outputs;
1163 for (auto& step : *steps)
1164 {
Ed Tanous543f4402022-01-06 13:12:53 -08001165 double target = 0.0;
1166 double out = 0.0;
James Feist5f2caae2018-12-12 14:08:25 -08001167
Myung Baeafc474a2024-10-09 00:53:29 -07001168 if (!redfish::json_util::readJsonObject( //
1169 step, response->res, //
1170 "Output", out, //
1171 "Target", target //
1172 ))
James Feist5f2caae2018-12-12 14:08:25 -08001173 {
James Feist5f2caae2018-12-12 14:08:25 -08001174 return CreatePIDRet::fail;
1175 }
1176 readings.emplace_back(target);
Ed Tanous23a21a12020-07-25 04:45:05 +00001177 outputs.emplace_back(out);
James Feist5f2caae2018-12-12 14:08:25 -08001178 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001179 output.emplace_back("Reading", std::move(readings));
1180 output.emplace_back("Output", std::move(outputs));
James Feist5f2caae2018-12-12 14:08:25 -08001181 }
1182 if (inputs)
1183 {
1184 for (std::string& value : *inputs)
1185 {
Ed Tanousa170f272022-06-30 21:53:27 -07001186 std::replace(value.begin(), value.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -08001187 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001188 output.emplace_back("Inputs", std::move(*inputs));
James Feist5f2caae2018-12-12 14:08:25 -08001189 }
1190 if (negativeHysteresis)
1191 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001192 output.emplace_back("NegativeHysteresis", *negativeHysteresis);
James Feist5f2caae2018-12-12 14:08:25 -08001193 }
1194 if (positiveHysteresis)
1195 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001196 output.emplace_back("PositiveHysteresis", *positiveHysteresis);
James Feist83ff9ab2018-08-31 10:18:24 -07001197 }
James Feistc33a90e2019-03-01 10:17:44 -08001198 if (direction)
1199 {
1200 constexpr const std::array<const char*, 2> allowedDirections = {
1201 "Ceiling", "Floor"};
Ed Tanous3544d2a2023-08-06 18:12:20 -07001202 if (std::ranges::find(allowedDirections, *direction) ==
1203 allowedDirections.end())
James Feistc33a90e2019-03-01 10:17:44 -08001204 {
1205 messages::propertyValueTypeError(response->res, "Direction",
1206 *direction);
1207 return CreatePIDRet::fail;
1208 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001209 output.emplace_back("Class", *direction);
James Feistc33a90e2019-03-01 10:17:44 -08001210 }
James Feist83ff9ab2018-08-31 10:18:24 -07001211 }
1212 else
1213 {
Ed Tanous62598e32023-07-17 17:06:25 -07001214 BMCWEB_LOG_ERROR("Illegal Type {}", type);
Jason M. Bills35a62c72018-10-09 12:45:45 -07001215 messages::propertyUnknown(response->res, type);
James Feist83ff9ab2018-08-31 10:18:24 -07001216 return CreatePIDRet::fail;
1217 }
1218 return CreatePIDRet::patch;
1219}
James Feist73df0db2019-03-25 15:29:35 -07001220struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
1221{
Ed Tanous6936afe2022-09-08 15:10:39 -07001222 struct CompletionValues
1223 {
1224 std::vector<std::string> supportedProfiles;
1225 std::string currentProfile;
1226 dbus::utility::MapperGetSubTreeResponse subtree;
1227 };
James Feist73df0db2019-03-25 15:29:35 -07001228
Ed Tanous4e23a442022-06-06 09:57:26 -07001229 explicit GetPIDValues(
1230 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
Ed Tanous23a21a12020-07-25 04:45:05 +00001231 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001232
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001233 {}
James Feist73df0db2019-03-25 15:29:35 -07001234
1235 void run()
1236 {
1237 std::shared_ptr<GetPIDValues> self = shared_from_this();
1238
1239 // get all configurations
George Liue99073f2022-12-09 11:06:16 +08001240 constexpr std::array<std::string_view, 4> interfaces = {
1241 pidConfigurationIface, pidZoneConfigurationIface,
1242 objectManagerIface, stepwiseConfigurationIface};
1243 dbus::utility::getSubTree(
1244 "/", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001245 [self](
George Liue99073f2022-12-09 11:06:16 +08001246 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001247 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001248 if (ec)
1249 {
1250 BMCWEB_LOG_ERROR("{}", ec);
1251 messages::internalError(self->asyncResp->res);
1252 return;
1253 }
1254 self->complete.subtree = subtreeLocal;
1255 });
James Feist73df0db2019-03-25 15:29:35 -07001256
1257 // at the same time get the selected profile
George Liue99073f2022-12-09 11:06:16 +08001258 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1259 thermalModeIface};
1260 dbus::utility::getSubTree(
1261 "/", 0, thermalModeIfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001262 [self](
George Liue99073f2022-12-09 11:06:16 +08001263 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001264 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001265 if (ec || subtreeLocal.empty())
James Feist73df0db2019-03-25 15:29:35 -07001266 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001267 return;
1268 }
1269 if (subtreeLocal[0].second.size() != 1)
1270 {
1271 // invalid mapper response, should never happen
1272 BMCWEB_LOG_ERROR("GetPIDValues: Mapper Error");
James Feist73df0db2019-03-25 15:29:35 -07001273 messages::internalError(self->asyncResp->res);
1274 return;
1275 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001276
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001277 const std::string& path = subtreeLocal[0].first;
1278 const std::string& owner = subtreeLocal[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001279
Ed Tanousdeae6a72024-11-11 21:58:57 -08001280 dbus::utility::getAllProperties(
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001281 *crow::connections::systemBus, owner, path,
1282 thermalModeIface,
1283 [path, owner,
1284 self](const boost::system::error_code& ec2,
1285 const dbus::utility::DBusPropertiesMap& resp) {
1286 if (ec2)
1287 {
1288 BMCWEB_LOG_ERROR(
1289 "GetPIDValues: Can't get thermalModeIface {}",
1290 path);
1291 messages::internalError(self->asyncResp->res);
1292 return;
1293 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001294
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001295 const std::string* current = nullptr;
1296 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001297
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001298 const bool success = sdbusplus::unpackPropertiesNoThrow(
1299 dbus_utils::UnpackErrorPrinter(), resp, "Current",
1300 current, "Supported", supported);
1301
1302 if (!success)
1303 {
1304 messages::internalError(self->asyncResp->res);
1305 return;
1306 }
1307
1308 if (current == nullptr || supported == nullptr)
1309 {
1310 BMCWEB_LOG_ERROR(
1311 "GetPIDValues: thermal mode iface invalid {}",
1312 path);
1313 messages::internalError(self->asyncResp->res);
1314 return;
1315 }
1316 self->complete.currentProfile = *current;
1317 self->complete.supportedProfiles = *supported;
1318 });
George Liue99073f2022-12-09 11:06:16 +08001319 });
James Feist73df0db2019-03-25 15:29:35 -07001320 }
1321
Ed Tanous6936afe2022-09-08 15:10:39 -07001322 static void
1323 processingComplete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1324 const CompletionValues& completion)
James Feist73df0db2019-03-25 15:29:35 -07001325 {
1326 if (asyncResp->res.result() != boost::beast::http::status::ok)
1327 {
1328 return;
1329 }
1330 // create map of <connection, path to objMgr>>
Ed Tanous6936afe2022-09-08 15:10:39 -07001331 boost::container::flat_map<
1332 std::string, std::string, std::less<>,
1333 std::vector<std::pair<std::string, std::string>>>
1334 objectMgrPaths;
1335 boost::container::flat_set<std::string, std::less<>,
1336 std::vector<std::string>>
1337 calledConnections;
1338 for (const auto& pathGroup : completion.subtree)
James Feist73df0db2019-03-25 15:29:35 -07001339 {
1340 for (const auto& connectionGroup : pathGroup.second)
1341 {
1342 auto findConnection =
1343 calledConnections.find(connectionGroup.first);
1344 if (findConnection != calledConnections.end())
1345 {
1346 break;
1347 }
1348 for (const std::string& interface : connectionGroup.second)
1349 {
1350 if (interface == objectManagerIface)
1351 {
1352 objectMgrPaths[connectionGroup.first] = pathGroup.first;
1353 }
1354 // this list is alphabetical, so we
1355 // should have found the objMgr by now
1356 if (interface == pidConfigurationIface ||
1357 interface == pidZoneConfigurationIface ||
1358 interface == stepwiseConfigurationIface)
1359 {
1360 auto findObjMgr =
1361 objectMgrPaths.find(connectionGroup.first);
1362 if (findObjMgr == objectMgrPaths.end())
1363 {
Ed Tanous62598e32023-07-17 17:06:25 -07001364 BMCWEB_LOG_DEBUG("{}Has no Object Manager",
1365 connectionGroup.first);
James Feist73df0db2019-03-25 15:29:35 -07001366 continue;
1367 }
1368
1369 calledConnections.insert(connectionGroup.first);
1370
1371 asyncPopulatePid(findObjMgr->first, findObjMgr->second,
Ed Tanous6936afe2022-09-08 15:10:39 -07001372 completion.currentProfile,
1373 completion.supportedProfiles,
James Feist73df0db2019-03-25 15:29:35 -07001374 asyncResp);
1375 break;
1376 }
1377 }
1378 }
1379 }
1380 }
1381
Ed Tanous6936afe2022-09-08 15:10:39 -07001382 ~GetPIDValues()
1383 {
1384 boost::asio::post(crow::connections::systemBus->get_io_context(),
1385 std::bind_front(&processingComplete, asyncResp,
1386 std::move(complete)));
1387 }
1388
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001389 GetPIDValues(const GetPIDValues&) = delete;
1390 GetPIDValues(GetPIDValues&&) = delete;
1391 GetPIDValues& operator=(const GetPIDValues&) = delete;
1392 GetPIDValues& operator=(GetPIDValues&&) = delete;
1393
zhanghch058d1b46d2021-04-01 11:18:24 +08001394 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Ed Tanous6936afe2022-09-08 15:10:39 -07001395 CompletionValues complete;
James Feist73df0db2019-03-25 15:29:35 -07001396};
1397
1398struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
1399{
Ed Tanous9e9b6042024-03-06 14:18:28 -08001400 SetPIDValues(
1401 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
1402 std::vector<
1403 std::pair<std::string, std::optional<nlohmann::json::object_t>>>&&
1404 configurationsIn,
1405 std::optional<std::string>& profileIn) :
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001406 asyncResp(asyncRespIn), configuration(std::move(configurationsIn)),
Ed Tanous9e9b6042024-03-06 14:18:28 -08001407 profile(std::move(profileIn))
1408 {}
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001409
1410 SetPIDValues(const SetPIDValues&) = delete;
1411 SetPIDValues(SetPIDValues&&) = delete;
1412 SetPIDValues& operator=(const SetPIDValues&) = delete;
1413 SetPIDValues& operator=(SetPIDValues&&) = delete;
1414
James Feist73df0db2019-03-25 15:29:35 -07001415 void run()
1416 {
1417 if (asyncResp->res.result() != boost::beast::http::status::ok)
1418 {
1419 return;
1420 }
1421
1422 std::shared_ptr<SetPIDValues> self = shared_from_this();
1423
1424 // todo(james): might make sense to do a mapper call here if this
1425 // interface gets more traction
George Liu5eb468d2023-06-20 17:03:24 +08001426 sdbusplus::message::object_path objPath(
1427 "/xyz/openbmc_project/inventory");
1428 dbus::utility::getManagedObjects(
1429 "xyz.openbmc_project.EntityManager", objPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001430 [self](const boost::system::error_code& ec,
Ed Tanous914e2d52022-01-07 11:38:34 -08001431 const dbus::utility::ManagedObjectType& mObj) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001432 if (ec)
James Feiste69d9de2020-02-07 12:23:27 -08001433 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001434 BMCWEB_LOG_ERROR("Error communicating to Entity Manager");
1435 messages::internalError(self->asyncResp->res);
1436 return;
1437 }
1438 const std::array<const char*, 3> configurations = {
1439 pidConfigurationIface, pidZoneConfigurationIface,
1440 stepwiseConfigurationIface};
1441
1442 for (const auto& [path, object] : mObj)
1443 {
1444 for (const auto& [interface, _] : object)
James Feiste69d9de2020-02-07 12:23:27 -08001445 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001446 if (std::ranges::find(configurations, interface) !=
1447 configurations.end())
1448 {
1449 self->objectCount++;
1450 break;
1451 }
James Feiste69d9de2020-02-07 12:23:27 -08001452 }
James Feiste69d9de2020-02-07 12:23:27 -08001453 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001454 self->managedObj = mObj;
1455 });
James Feist73df0db2019-03-25 15:29:35 -07001456
1457 // at the same time get the profile information
George Liue99073f2022-12-09 11:06:16 +08001458 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1459 thermalModeIface};
1460 dbus::utility::getSubTree(
1461 "/", 0, thermalModeIfaces,
1462 [self](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001463 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001464 if (ec || subtree.empty())
James Feist73df0db2019-03-25 15:29:35 -07001465 {
James Feist73df0db2019-03-25 15:29:35 -07001466 return;
1467 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001468 if (subtree[0].second.empty())
Ed Tanous002d39b2022-05-31 08:59:27 -07001469 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001470 // invalid mapper response, should never happen
1471 BMCWEB_LOG_ERROR("SetPIDValues: Mapper Error");
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001472 messages::internalError(self->asyncResp->res);
1473 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001474 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001475
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001476 const std::string& path = subtree[0].first;
1477 const std::string& owner = subtree[0].second[0].first;
Ed Tanousdeae6a72024-11-11 21:58:57 -08001478 dbus::utility::getAllProperties(
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001479 *crow::connections::systemBus, owner, path,
1480 thermalModeIface,
1481 [self, path,
1482 owner](const boost::system::error_code& ec2,
1483 const dbus::utility::DBusPropertiesMap& r) {
1484 if (ec2)
1485 {
1486 BMCWEB_LOG_ERROR(
1487 "SetPIDValues: Can't get thermalModeIface {}",
1488 path);
1489 messages::internalError(self->asyncResp->res);
1490 return;
1491 }
1492 const std::string* current = nullptr;
1493 const std::vector<std::string>* supported = nullptr;
1494
1495 const bool success = sdbusplus::unpackPropertiesNoThrow(
1496 dbus_utils::UnpackErrorPrinter(), r, "Current",
1497 current, "Supported", supported);
1498
1499 if (!success)
1500 {
1501 messages::internalError(self->asyncResp->res);
1502 return;
1503 }
1504
1505 if (current == nullptr || supported == nullptr)
1506 {
1507 BMCWEB_LOG_ERROR(
1508 "SetPIDValues: thermal mode iface invalid {}",
1509 path);
1510 messages::internalError(self->asyncResp->res);
1511 return;
1512 }
1513 self->currentProfile = *current;
1514 self->supportedProfiles = *supported;
1515 self->profileConnection = owner;
1516 self->profilePath = path;
1517 });
George Liue99073f2022-12-09 11:06:16 +08001518 });
James Feist73df0db2019-03-25 15:29:35 -07001519 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001520 void pidSetDone()
James Feist73df0db2019-03-25 15:29:35 -07001521 {
1522 if (asyncResp->res.result() != boost::beast::http::status::ok)
1523 {
1524 return;
1525 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001526 std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001527 if (profile)
1528 {
Ed Tanous3544d2a2023-08-06 18:12:20 -07001529 if (std::ranges::find(supportedProfiles, *profile) ==
1530 supportedProfiles.end())
James Feist73df0db2019-03-25 15:29:35 -07001531 {
1532 messages::actionParameterUnknown(response->res, "Profile",
1533 *profile);
1534 return;
1535 }
1536 currentProfile = *profile;
George Liu9ae226f2023-06-21 17:56:46 +08001537 sdbusplus::asio::setProperty(
1538 *crow::connections::systemBus, profileConnection, profilePath,
1539 thermalModeIface, "Current", *profile,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001540 [response](const boost::system::error_code& ec) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001541 if (ec)
1542 {
1543 BMCWEB_LOG_ERROR("Error patching profile{}", ec);
1544 messages::internalError(response->res);
1545 }
1546 });
James Feist73df0db2019-03-25 15:29:35 -07001547 }
1548
1549 for (auto& containerPair : configuration)
1550 {
1551 auto& container = containerPair.second;
1552 if (!container)
1553 {
1554 continue;
1555 }
James Feist6ee7f772020-02-06 16:25:27 -08001556
Ed Tanous02cad962022-06-30 16:50:15 -07001557 const std::string& type = containerPair.first;
James Feist73df0db2019-03-25 15:29:35 -07001558
Ed Tanous9e9b6042024-03-06 14:18:28 -08001559 for (auto& [name, value] : *container)
James Feist73df0db2019-03-25 15:29:35 -07001560 {
Potin Laicddbf3d2023-02-14 14:28:58 +08001561 std::string dbusObjName = name;
1562 std::replace(dbusObjName.begin(), dbusObjName.end(), ' ', '_');
Ed Tanous62598e32023-07-17 17:06:25 -07001563 BMCWEB_LOG_DEBUG("looking for {}", name);
James Feist6ee7f772020-02-06 16:25:27 -08001564
Ed Tanous3544d2a2023-08-06 18:12:20 -07001565 auto pathItr = std::ranges::find_if(
1566 managedObj, [&dbusObjName](const auto& obj) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001567 return obj.first.filename() == dbusObjName;
1568 });
Ed Tanousb9d36b42022-02-26 21:42:46 -08001569 dbus::utility::DBusPropertiesMap output;
James Feist73df0db2019-03-25 15:29:35 -07001570
1571 output.reserve(16); // The pid interface length
1572
1573 // determines if we're patching entity-manager or
1574 // creating a new object
1575 bool createNewObject = (pathItr == managedObj.end());
Ed Tanous62598e32023-07-17 17:06:25 -07001576 BMCWEB_LOG_DEBUG("Found = {}", !createNewObject);
James Feist6ee7f772020-02-06 16:25:27 -08001577
James Feist73df0db2019-03-25 15:29:35 -07001578 std::string iface;
Ed Tanousea2b6702022-03-07 16:48:38 -08001579 if (!createNewObject)
James Feist73df0db2019-03-25 15:29:35 -07001580 {
Potin Lai8be2b5b2022-11-22 13:27:16 +08001581 bool findInterface = false;
Ed Tanousea2b6702022-03-07 16:48:38 -08001582 for (const auto& interface : pathItr->second)
James Feist73df0db2019-03-25 15:29:35 -07001583 {
Ed Tanousea2b6702022-03-07 16:48:38 -08001584 if (interface.first == pidConfigurationIface)
1585 {
1586 if (type == "PidControllers" ||
1587 type == "FanControllers")
1588 {
1589 iface = pidConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001590 findInterface = true;
1591 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001592 }
1593 }
1594 else if (interface.first == pidZoneConfigurationIface)
1595 {
1596 if (type == "FanZones")
1597 {
PavanKumarIntelda393502024-03-15 05:47:02 +00001598 iface = pidZoneConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001599 findInterface = true;
1600 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001601 }
1602 }
1603 else if (interface.first == stepwiseConfigurationIface)
1604 {
1605 if (type == "StepwiseControllers")
1606 {
1607 iface = stepwiseConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001608 findInterface = true;
1609 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001610 }
1611 }
James Feist73df0db2019-03-25 15:29:35 -07001612 }
Potin Lai8be2b5b2022-11-22 13:27:16 +08001613
1614 // create new object if interface not found
1615 if (!findInterface)
1616 {
1617 createNewObject = true;
1618 }
James Feist73df0db2019-03-25 15:29:35 -07001619 }
James Feist6ee7f772020-02-06 16:25:27 -08001620
Ed Tanous9e9b6042024-03-06 14:18:28 -08001621 if (createNewObject && value == nullptr)
James Feist6ee7f772020-02-06 16:25:27 -08001622 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -05001623 // can't delete a non-existent object
Ed Tanous9e9b6042024-03-06 14:18:28 -08001624 messages::propertyValueNotInList(response->res, value,
Ed Tanouse2616cc2022-06-27 12:45:55 -07001625 name);
James Feist6ee7f772020-02-06 16:25:27 -08001626 continue;
1627 }
1628
1629 std::string path;
1630 if (pathItr != managedObj.end())
1631 {
1632 path = pathItr->first.str;
1633 }
1634
Ed Tanous62598e32023-07-17 17:06:25 -07001635 BMCWEB_LOG_DEBUG("Create new = {}", createNewObject);
James Feiste69d9de2020-02-07 12:23:27 -08001636
1637 // arbitrary limit to avoid attacks
1638 constexpr const size_t controllerLimit = 500;
James Feist14b0b8d2020-02-12 11:52:07 -08001639 if (createNewObject && objectCount >= controllerLimit)
James Feiste69d9de2020-02-07 12:23:27 -08001640 {
1641 messages::resourceExhaustion(response->res, type);
1642 continue;
1643 }
Ed Tanousa170f272022-06-30 21:53:27 -07001644 std::string escaped = name;
1645 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1646 output.emplace_back("Name", escaped);
James Feist73df0db2019-03-25 15:29:35 -07001647
1648 std::string chassis;
1649 CreatePIDRet ret = createPidInterface(
Ed Tanous9e9b6042024-03-06 14:18:28 -08001650 response, type, name, value, path, managedObj,
1651 createNewObject, output, chassis, currentProfile);
James Feist73df0db2019-03-25 15:29:35 -07001652 if (ret == CreatePIDRet::fail)
1653 {
1654 return;
1655 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001656 if (ret == CreatePIDRet::del)
James Feist73df0db2019-03-25 15:29:35 -07001657 {
1658 continue;
1659 }
1660
1661 if (!createNewObject)
1662 {
1663 for (const auto& property : output)
1664 {
Potin Lai7a696972023-11-09 12:18:20 +08001665 crow::connections::systemBus->async_method_call(
James Feist73df0db2019-03-25 15:29:35 -07001666 [response,
1667 propertyName{std::string(property.first)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001668 const boost::system::error_code& ec) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001669 if (ec)
1670 {
1671 BMCWEB_LOG_ERROR("Error patching {}: {}",
1672 propertyName, ec);
1673 messages::internalError(response->res);
1674 return;
1675 }
1676 messages::success(response->res);
1677 },
Potin Lai7a696972023-11-09 12:18:20 +08001678 "xyz.openbmc_project.EntityManager", path,
1679 "org.freedesktop.DBus.Properties", "Set", iface,
1680 property.first, property.second);
James Feist73df0db2019-03-25 15:29:35 -07001681 }
1682 }
1683 else
1684 {
1685 if (chassis.empty())
1686 {
Ed Tanous62598e32023-07-17 17:06:25 -07001687 BMCWEB_LOG_ERROR("Failed to get chassis from config");
Ed Tanousace85d62021-10-26 12:45:59 -07001688 messages::internalError(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001689 return;
1690 }
1691
1692 bool foundChassis = false;
1693 for (const auto& obj : managedObj)
1694 {
Ed Tanous91f75ca2024-06-10 13:56:43 -07001695 if (obj.first.filename() == chassis)
James Feist73df0db2019-03-25 15:29:35 -07001696 {
1697 chassis = obj.first.str;
1698 foundChassis = true;
1699 break;
1700 }
1701 }
1702 if (!foundChassis)
1703 {
Ed Tanous62598e32023-07-17 17:06:25 -07001704 BMCWEB_LOG_ERROR("Failed to find chassis on dbus");
James Feist73df0db2019-03-25 15:29:35 -07001705 messages::resourceMissingAtURI(
Ed Tanousace85d62021-10-26 12:45:59 -07001706 response->res,
Ed Tanousef4c65b2023-04-24 15:28:50 -07001707 boost::urls::format("/redfish/v1/Chassis/{}",
1708 chassis));
James Feist73df0db2019-03-25 15:29:35 -07001709 return;
1710 }
1711
1712 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001713 [response](const boost::system::error_code& ec) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001714 if (ec)
1715 {
1716 BMCWEB_LOG_ERROR("Error Adding Pid Object {}",
1717 ec);
1718 messages::internalError(response->res);
1719 return;
1720 }
1721 messages::success(response->res);
1722 },
James Feist73df0db2019-03-25 15:29:35 -07001723 "xyz.openbmc_project.EntityManager", chassis,
1724 "xyz.openbmc_project.AddObject", "AddObject", output);
1725 }
1726 }
1727 }
1728 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001729
1730 ~SetPIDValues()
1731 {
1732 try
1733 {
1734 pidSetDone();
1735 }
1736 catch (...)
1737 {
Ed Tanous62598e32023-07-17 17:06:25 -07001738 BMCWEB_LOG_CRITICAL("pidSetDone threw exception");
Ed Tanous24b2fe82022-01-06 12:45:54 -08001739 }
1740 }
1741
zhanghch058d1b46d2021-04-01 11:18:24 +08001742 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Ed Tanous9e9b6042024-03-06 14:18:28 -08001743 std::vector<std::pair<std::string, std::optional<nlohmann::json::object_t>>>
James Feist73df0db2019-03-25 15:29:35 -07001744 configuration;
1745 std::optional<std::string> profile;
1746 dbus::utility::ManagedObjectType managedObj;
1747 std::vector<std::string> supportedProfiles;
1748 std::string currentProfile;
1749 std::string profileConnection;
1750 std::string profilePath;
James Feist14b0b8d2020-02-12 11:52:07 -08001751 size_t objectCount = 0;
James Feist73df0db2019-03-25 15:29:35 -07001752};
James Feist83ff9ab2018-08-31 10:18:24 -07001753
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001754/**
1755 * @brief Retrieves BMC manager location data over DBus
1756 *
Ed Tanousac106bf2023-06-07 09:24:59 -07001757 * @param[in] asyncResp Shared pointer for completing asynchronous calls
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001758 * @param[in] connectionName - service name
1759 * @param[in] path - object path
1760 * @return none
1761 */
Ed Tanousac106bf2023-06-07 09:24:59 -07001762inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001763 const std::string& connectionName,
1764 const std::string& path)
1765{
Ed Tanous62598e32023-07-17 17:06:25 -07001766 BMCWEB_LOG_DEBUG("Get BMC manager Location data.");
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001767
Ed Tanousdeae6a72024-11-11 21:58:57 -08001768 dbus::utility::getProperty<std::string>(
1769 connectionName, path,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001770 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Ed Tanousac106bf2023-06-07 09:24:59 -07001771 [asyncResp](const boost::system::error_code& ec,
1772 const std::string& property) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001773 if (ec)
1774 {
1775 BMCWEB_LOG_DEBUG("DBUS response error for "
1776 "Location");
1777 messages::internalError(asyncResp->res);
1778 return;
1779 }
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001780
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001781 asyncResp->res
1782 .jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
1783 property;
1784 });
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001785}
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001786// avoid name collision systems.hpp
1787inline void
Ed Tanousac106bf2023-06-07 09:24:59 -07001788 managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001789{
Ed Tanous62598e32023-07-17 17:06:25 -07001790 BMCWEB_LOG_DEBUG("Getting Manager Last Reset Time");
Ed Tanous52cc1122020-07-18 13:51:21 -07001791
Ed Tanousdeae6a72024-11-11 21:58:57 -08001792 dbus::utility::getProperty<uint64_t>(
1793 "xyz.openbmc_project.State.BMC", "/xyz/openbmc_project/state/bmc0",
1794 "xyz.openbmc_project.State.BMC", "LastRebootTime",
Ed Tanousac106bf2023-06-07 09:24:59 -07001795 [asyncResp](const boost::system::error_code& ec,
1796 const uint64_t lastResetTime) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001797 if (ec)
1798 {
1799 BMCWEB_LOG_DEBUG("D-BUS response error {}", ec);
1800 return;
1801 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001802
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001803 // LastRebootTime is epoch time, in milliseconds
1804 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
1805 uint64_t lastResetTimeStamp = lastResetTime / 1000;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001806
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001807 // Convert to ISO 8601 standard
1808 asyncResp->res.jsonValue["LastResetTime"] =
1809 redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
1810 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001811}
1812
1813/**
1814 * @brief Set the running firmware image
1815 *
Ed Tanousac106bf2023-06-07 09:24:59 -07001816 * @param[i,o] asyncResp - Async response object
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001817 * @param[i] runningFirmwareTarget - Image to make the running image
1818 *
1819 * @return void
1820 */
1821inline void
Ed Tanousac106bf2023-06-07 09:24:59 -07001822 setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001823 const std::string& runningFirmwareTarget)
1824{
1825 // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1826 std::string::size_type idPos = runningFirmwareTarget.rfind('/');
1827 if (idPos == std::string::npos)
1828 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001829 messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001830 "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001831 BMCWEB_LOG_DEBUG("Can't parse firmware ID!");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001832 return;
1833 }
1834 idPos++;
1835 if (idPos >= runningFirmwareTarget.size())
1836 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001837 messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001838 "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001839 BMCWEB_LOG_DEBUG("Invalid firmware ID.");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001840 return;
1841 }
1842 std::string firmwareId = runningFirmwareTarget.substr(idPos);
1843
1844 // Make sure the image is valid before setting priority
George Liu5eb468d2023-06-20 17:03:24 +08001845 sdbusplus::message::object_path objPath("/xyz/openbmc_project/software");
1846 dbus::utility::getManagedObjects(
Jagpal Singh Gilld27c31e2024-10-15 15:10:19 -07001847 getBMCUpdateServiceName(), objPath,
George Liu5eb468d2023-06-20 17:03:24 +08001848 [asyncResp, firmwareId, runningFirmwareTarget](
1849 const boost::system::error_code& ec,
1850 const dbus::utility::ManagedObjectType& subtree) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001851 if (ec)
Ed Tanous002d39b2022-05-31 08:59:27 -07001852 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001853 BMCWEB_LOG_DEBUG("D-Bus response error getting objects.");
Ed Tanousac106bf2023-06-07 09:24:59 -07001854 messages::internalError(asyncResp->res);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001855 return;
1856 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001857
1858 if (subtree.empty())
1859 {
1860 BMCWEB_LOG_DEBUG("Can't find image!");
1861 messages::internalError(asyncResp->res);
1862 return;
1863 }
1864
1865 bool foundImage = false;
1866 for (const auto& object : subtree)
1867 {
1868 const std::string& path =
1869 static_cast<const std::string&>(object.first);
1870 std::size_t idPos2 = path.rfind('/');
1871
1872 if (idPos2 == std::string::npos)
1873 {
1874 continue;
1875 }
1876
1877 idPos2++;
1878 if (idPos2 >= path.size())
1879 {
1880 continue;
1881 }
1882
1883 if (path.substr(idPos2) == firmwareId)
1884 {
1885 foundImage = true;
1886 break;
1887 }
1888 }
1889
1890 if (!foundImage)
1891 {
1892 messages::propertyValueNotInList(
1893 asyncResp->res, runningFirmwareTarget, "@odata.id");
1894 BMCWEB_LOG_DEBUG("Invalid firmware ID.");
1895 return;
1896 }
1897
1898 BMCWEB_LOG_DEBUG("Setting firmware version {} to priority 0.",
1899 firmwareId);
1900
1901 // Only support Immediate
1902 // An addition could be a Redfish Setting like
1903 // ActiveSoftwareImageApplyTime and support OnReset
1904 sdbusplus::asio::setProperty(
Jagpal Singh Gilld27c31e2024-10-15 15:10:19 -07001905 *crow::connections::systemBus, getBMCUpdateServiceName(),
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001906 "/xyz/openbmc_project/software/" + firmwareId,
1907 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1908 static_cast<uint8_t>(0),
1909 [asyncResp](const boost::system::error_code& ec2) {
1910 if (ec2)
1911 {
1912 BMCWEB_LOG_DEBUG("D-Bus response error setting.");
1913 messages::internalError(asyncResp->res);
1914 return;
1915 }
1916 doBMCGracefulRestart(asyncResp);
1917 });
George Liu5eb468d2023-06-20 17:03:24 +08001918 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001919}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001920
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001921inline void afterSetDateTime(
1922 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1923 const boost::system::error_code& ec, const sdbusplus::message_t& msg)
Ed Tanousc51afd52024-03-07 10:13:14 -08001924{
1925 if (ec)
1926 {
1927 BMCWEB_LOG_DEBUG("Failed to set elapsed time. DBUS response error {}",
1928 ec);
1929 const sd_bus_error* dbusError = msg.get_error();
1930 if (dbusError != nullptr)
1931 {
1932 std::string_view errorName(dbusError->name);
1933 if (errorName ==
1934 "org.freedesktop.timedate1.AutomaticTimeSyncEnabled")
1935 {
1936 BMCWEB_LOG_DEBUG("Setting conflict");
1937 messages::propertyValueConflict(
1938 asyncResp->res, "DateTime",
1939 "Managers/NetworkProtocol/NTPProcotolEnabled");
1940 return;
1941 }
1942 }
1943 messages::internalError(asyncResp->res);
1944 return;
1945 }
1946 asyncResp->res.result(boost::beast::http::status::no_content);
1947}
1948
1949inline void setDateTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1950 const std::string& datetime)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001951{
Ed Tanous62598e32023-07-17 17:06:25 -07001952 BMCWEB_LOG_DEBUG("Set date time: {}", datetime);
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001953
Ed Tanousc2e32002023-01-07 22:05:08 -08001954 std::optional<redfish::time_utils::usSinceEpoch> us =
1955 redfish::time_utils::dateStringToEpoch(datetime);
1956 if (!us)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001957 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001958 messages::propertyValueFormatError(asyncResp->res, datetime,
1959 "DateTime");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001960 return;
1961 }
Ed Tanousc51afd52024-03-07 10:13:14 -08001962 // Set the absolute datetime
1963 bool relative = false;
1964 bool interactive = false;
1965 crow::connections::systemBus->async_method_call(
1966 [asyncResp](const boost::system::error_code& ec,
1967 const sdbusplus::message_t& msg) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001968 afterSetDateTime(asyncResp, ec, msg);
1969 },
Ed Tanousc51afd52024-03-07 10:13:14 -08001970 "org.freedesktop.timedate1", "/org/freedesktop/timedate1",
1971 "org.freedesktop.timedate1", "SetTime", us->count(), relative,
1972 interactive);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001973}
1974
Ed Tanous75815e52022-10-05 17:21:13 -07001975inline void
1976 checkForQuiesced(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1977{
Ed Tanousdeae6a72024-11-11 21:58:57 -08001978 dbus::utility::getProperty<std::string>(
1979 "org.freedesktop.systemd1",
Ed Tanous75815e52022-10-05 17:21:13 -07001980 "/org/freedesktop/systemd1/unit/obmc-bmc-service-quiesce@0.target",
1981 "org.freedesktop.systemd1.Unit", "ActiveState",
1982 [asyncResp](const boost::system::error_code& ec,
1983 const std::string& val) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001984 if (!ec)
Ed Tanous75815e52022-10-05 17:21:13 -07001985 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001986 if (val == "active")
1987 {
1988 asyncResp->res.jsonValue["Status"]["Health"] =
1989 resource::Health::Critical;
1990 asyncResp->res.jsonValue["Status"]["State"] =
1991 resource::State::Quiesced;
1992 return;
1993 }
Ed Tanous75815e52022-10-05 17:21:13 -07001994 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04001995 asyncResp->res.jsonValue["Status"]["Health"] = resource::Health::OK;
1996 asyncResp->res.jsonValue["Status"]["State"] =
1997 resource::State::Enabled;
1998 });
Ed Tanous75815e52022-10-05 17:21:13 -07001999}
2000
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002001inline void requestRoutesManager(App& app)
2002{
2003 std::string uuid = persistent_data::getConfig().systemUuid;
2004
Ed Tanous253f11b2024-05-16 09:38:31 -07002005 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002006 .privileges(redfish::privileges::getManager)
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002007 .methods(
2008 boost::beast::http::verb::
2009 get)([&app,
2010 uuid](const crow::Request& req,
2011 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2012 const std::string& managerId) {
2013 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2014 {
2015 return;
2016 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002017
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002018 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2019 {
2020 messages::resourceNotFound(asyncResp->res, "Manager",
2021 managerId);
2022 return;
2023 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002024
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002025 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2026 "/redfish/v1/Managers/{}", BMCWEB_REDFISH_MANAGER_URI_NAME);
2027 asyncResp->res.jsonValue["@odata.type"] =
2028 "#Manager.v1_14_0.Manager";
2029 asyncResp->res.jsonValue["Id"] = BMCWEB_REDFISH_MANAGER_URI_NAME;
2030 asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
2031 asyncResp->res.jsonValue["Description"] =
2032 "Baseboard Management Controller";
2033 asyncResp->res.jsonValue["PowerState"] = resource::PowerState::On;
Ed Tanous14766872022-03-15 10:44:42 -07002034
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002035 asyncResp->res.jsonValue["ManagerType"] = manager::ManagerType::BMC;
2036 asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
2037 asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
2038 asyncResp->res.jsonValue["Model"] =
2039 "OpenBmc"; // TODO(ed), get model
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002040
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002041 asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
2042 boost::urls::format("/redfish/v1/Managers/{}/LogServices",
Ed Tanous253f11b2024-05-16 09:38:31 -07002043 BMCWEB_REDFISH_MANAGER_URI_NAME);
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002044 asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
2045 boost::urls::format("/redfish/v1/Managers/{}/NetworkProtocol",
2046 BMCWEB_REDFISH_MANAGER_URI_NAME);
2047 asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
2048 boost::urls::format(
2049 "/redfish/v1/Managers/{}/EthernetInterfaces",
2050 BMCWEB_REDFISH_MANAGER_URI_NAME);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002051
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002052 if constexpr (BMCWEB_VM_NBDPROXY)
Ed Tanous75815e52022-10-05 17:21:13 -07002053 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002054 asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
2055 boost::urls::format("/redfish/v1/Managers/{}/VirtualMedia",
2056 BMCWEB_REDFISH_MANAGER_URI_NAME);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002057 }
2058
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002059 // default oem data
2060 nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
2061 nlohmann::json& oemOpenbmc = oem["OpenBmc"];
2062 oem["@odata.id"] =
2063 boost::urls::format("/redfish/v1/Managers/{}#/Oem",
2064 BMCWEB_REDFISH_MANAGER_URI_NAME);
2065 oemOpenbmc["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager";
2066 oemOpenbmc["@odata.id"] =
2067 boost::urls::format("/redfish/v1/Managers/{}#/Oem/OpenBmc",
2068 BMCWEB_REDFISH_MANAGER_URI_NAME);
2069
2070 nlohmann::json::object_t certificates;
2071 certificates["@odata.id"] = boost::urls::format(
2072 "/redfish/v1/Managers/{}/Truststore/Certificates",
2073 BMCWEB_REDFISH_MANAGER_URI_NAME);
2074 oemOpenbmc["Certificates"] = std::move(certificates);
2075
2076 // Manager.Reset (an action) can be many values, OpenBMC only
2077 // supports BMC reboot.
2078 nlohmann::json& managerReset =
2079 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
2080 managerReset["target"] = boost::urls::format(
2081 "/redfish/v1/Managers/{}/Actions/Manager.Reset",
2082 BMCWEB_REDFISH_MANAGER_URI_NAME);
2083 managerReset["@Redfish.ActionInfo"] =
2084 boost::urls::format("/redfish/v1/Managers/{}/ResetActionInfo",
2085 BMCWEB_REDFISH_MANAGER_URI_NAME);
2086
2087 // ResetToDefaults (Factory Reset) has values like
2088 // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
2089 // on OpenBMC
2090 nlohmann::json& resetToDefaults =
2091 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
2092 resetToDefaults["target"] = boost::urls::format(
2093 "/redfish/v1/Managers/{}/Actions/Manager.ResetToDefaults",
2094 BMCWEB_REDFISH_MANAGER_URI_NAME);
2095 resetToDefaults["ResetType@Redfish.AllowableValues"] =
2096 nlohmann::json::array_t({"ResetAll"});
2097
2098 std::pair<std::string, std::string> redfishDateTimeOffset =
2099 redfish::time_utils::getDateTimeOffsetNow();
2100
2101 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2102 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2103 redfishDateTimeOffset.second;
2104
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002105 if constexpr (BMCWEB_KVM)
Ed Tanous002d39b2022-05-31 08:59:27 -07002106 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002107 // Fill in GraphicalConsole info
2108 asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] =
2109 true;
2110 asyncResp->res
2111 .jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4;
2112 asyncResp->res
2113 .jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
2114 nlohmann::json::array_t({"KVMIP"});
2115 }
2116 if constexpr (!BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
2117 {
2118 asyncResp->res
2119 .jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
2120
2121 nlohmann::json::array_t managerForServers;
2122 nlohmann::json::object_t manager;
2123 manager["@odata.id"] = std::format(
2124 "/redfish/v1/Systems/{}", BMCWEB_REDFISH_SYSTEM_URI_NAME);
2125 managerForServers.emplace_back(std::move(manager));
2126
2127 asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
2128 std::move(managerForServers);
Ed Tanous002d39b2022-05-31 08:59:27 -07002129 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002130
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002131 sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
2132 "FirmwareVersion", true);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002133
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002134 managerGetLastResetTime(asyncResp);
2135
2136 // ManagerDiagnosticData is added for all BMCs.
2137 nlohmann::json& managerDiagnosticData =
2138 asyncResp->res.jsonValue["ManagerDiagnosticData"];
2139 managerDiagnosticData["@odata.id"] = boost::urls::format(
2140 "/redfish/v1/Managers/{}/ManagerDiagnosticData",
2141 BMCWEB_REDFISH_MANAGER_URI_NAME);
2142
2143 if constexpr (BMCWEB_REDFISH_OEM_MANAGER_FAN_DATA)
Ed Tanous002d39b2022-05-31 08:59:27 -07002144 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002145 auto pids = std::make_shared<GetPIDValues>(asyncResp);
2146 pids->run();
2147 }
2148
2149 getMainChassisId(asyncResp, [](const std::string& chassisId,
2150 const std::shared_ptr<
2151 bmcweb::AsyncResp>& aRsp) {
2152 aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] =
2153 1;
2154 nlohmann::json::array_t managerForChassis;
2155 nlohmann::json::object_t managerObj;
2156 boost::urls::url chassiUrl =
2157 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
2158 managerObj["@odata.id"] = chassiUrl;
2159 managerForChassis.emplace_back(std::move(managerObj));
2160 aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
2161 std::move(managerForChassis);
2162 aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
2163 chassiUrl;
2164 });
2165
Ed Tanousdeae6a72024-11-11 21:58:57 -08002166 dbus::utility::getProperty<double>(
2167 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
2168 "org.freedesktop.systemd1.Manager", "Progress",
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002169 [asyncResp](const boost::system::error_code& ec, double val) {
2170 if (ec)
2171 {
2172 BMCWEB_LOG_ERROR("Error while getting progress");
2173 messages::internalError(asyncResp->res);
2174 return;
2175 }
2176 if (val < 1.0)
2177 {
2178 asyncResp->res.jsonValue["Status"]["Health"] =
2179 resource::Health::OK;
2180 asyncResp->res.jsonValue["Status"]["State"] =
2181 resource::State::Starting;
2182 return;
2183 }
2184 checkForQuiesced(asyncResp);
2185 });
2186
2187 constexpr std::array<std::string_view, 1> interfaces = {
2188 "xyz.openbmc_project.Inventory.Item.Bmc"};
2189 dbus::utility::getSubTree(
2190 "/xyz/openbmc_project/inventory", 0, interfaces,
2191 [asyncResp](
2192 const boost::system::error_code& ec,
2193 const dbus::utility::MapperGetSubTreeResponse& subtree) {
2194 if (ec)
2195 {
2196 BMCWEB_LOG_DEBUG(
2197 "D-Bus response error on GetSubTree {}", ec);
2198 return;
2199 }
2200 if (subtree.empty())
2201 {
2202 BMCWEB_LOG_DEBUG("Can't find bmc D-Bus object!");
2203 return;
2204 }
2205 // Assume only 1 bmc D-Bus object
2206 // Throw an error if there is more than 1
2207 if (subtree.size() > 1)
2208 {
2209 BMCWEB_LOG_DEBUG("Found more than 1 bmc D-Bus object!");
2210 messages::internalError(asyncResp->res);
2211 return;
2212 }
2213
2214 if (subtree[0].first.empty() ||
2215 subtree[0].second.size() != 1)
2216 {
2217 BMCWEB_LOG_DEBUG("Error getting bmc D-Bus object!");
2218 messages::internalError(asyncResp->res);
2219 return;
2220 }
2221
2222 const std::string& path = subtree[0].first;
2223 const std::string& connectionName =
2224 subtree[0].second[0].first;
2225
2226 for (const auto& interfaceName :
2227 subtree[0].second[0].second)
2228 {
2229 if (interfaceName ==
2230 "xyz.openbmc_project.Inventory.Decorator.Asset")
2231 {
Ed Tanousdeae6a72024-11-11 21:58:57 -08002232 dbus::utility::getAllProperties(
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002233 *crow::connections::systemBus, connectionName,
2234 path,
2235 "xyz.openbmc_project.Inventory.Decorator.Asset",
2236 [asyncResp](
2237 const boost::system::error_code& ec2,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002238 const dbus::utility::DBusPropertiesMap&
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002239 propertiesList) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002240 if (ec2)
2241 {
2242 BMCWEB_LOG_DEBUG(
2243 "Can't get bmc asset!");
2244 return;
2245 }
2246
2247 const std::string* partNumber = nullptr;
2248 const std::string* serialNumber = nullptr;
2249 const std::string* manufacturer = nullptr;
2250 const std::string* model = nullptr;
2251 const std::string* sparePartNumber =
2252 nullptr;
2253
2254 const bool success =
2255 sdbusplus::unpackPropertiesNoThrow(
2256 dbus_utils::UnpackErrorPrinter(),
2257 propertiesList, "PartNumber",
2258 partNumber, "SerialNumber",
2259 serialNumber, "Manufacturer",
2260 manufacturer, "Model", model,
2261 "SparePartNumber", sparePartNumber);
2262
2263 if (!success)
2264 {
2265 messages::internalError(asyncResp->res);
2266 return;
2267 }
2268
2269 if (partNumber != nullptr)
2270 {
2271 asyncResp->res.jsonValue["PartNumber"] =
2272 *partNumber;
2273 }
2274
2275 if (serialNumber != nullptr)
2276 {
2277 asyncResp->res
2278 .jsonValue["SerialNumber"] =
2279 *serialNumber;
2280 }
2281
2282 if (manufacturer != nullptr)
2283 {
2284 asyncResp->res
2285 .jsonValue["Manufacturer"] =
2286 *manufacturer;
2287 }
2288
2289 if (model != nullptr)
2290 {
2291 asyncResp->res.jsonValue["Model"] =
2292 *model;
2293 }
2294
2295 if (sparePartNumber != nullptr)
2296 {
2297 asyncResp->res
2298 .jsonValue["SparePartNumber"] =
2299 *sparePartNumber;
2300 }
2301 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002302 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002303 else if (
2304 interfaceName ==
2305 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002306 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002307 getLocation(asyncResp, connectionName, path);
Ed Tanous002d39b2022-05-31 08:59:27 -07002308 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002309 }
2310 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002311 });
2312
Ed Tanous253f11b2024-05-16 09:38:31 -07002313 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002314 .privileges(redfish::privileges::patchManager)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002315 .methods(boost::beast::http::verb::patch)(
2316 [&app](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -07002317 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2318 const std::string& managerId) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002319 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2320 {
2321 return;
2322 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002323
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002324 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2325 {
2326 messages::resourceNotFound(asyncResp->res, "Manager",
2327 managerId);
2328 return;
2329 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002330
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002331 std::optional<std::string> activeSoftwareImageOdataId;
2332 std::optional<std::string> datetime;
2333 std::optional<nlohmann::json::object_t> pidControllers;
2334 std::optional<nlohmann::json::object_t> fanControllers;
2335 std::optional<nlohmann::json::object_t> fanZones;
2336 std::optional<nlohmann::json::object_t> stepwiseControllers;
2337 std::optional<std::string> profile;
Ed Tanous002d39b2022-05-31 08:59:27 -07002338
Myung Baeafc474a2024-10-09 00:53:29 -07002339 if (!json_util::readJsonPatch( //
2340 req, asyncResp->res, //
2341 "DateTime", datetime, //
2342 "Links/ActiveSoftwareImage/@odata.id",
2343 activeSoftwareImageOdataId, //
2344 "Oem/OpenBmc/Fan/FanControllers", fanControllers, //
2345 "Oem/OpenBmc/Fan/FanZones", fanZones, //
2346 "Oem/OpenBmc/Fan/PidControllers", pidControllers, //
2347 "Oem/OpenBmc/Fan/Profile", profile, //
2348 "Oem/OpenBmc/Fan/StepwiseControllers",
2349 stepwiseControllers //
2350 ))
2351 {
2352 return;
2353 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002354
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002355 if (pidControllers || fanControllers || fanZones ||
2356 stepwiseControllers || profile)
2357 {
2358 if constexpr (BMCWEB_REDFISH_OEM_MANAGER_FAN_DATA)
2359 {
2360 std::vector<
2361 std::pair<std::string,
Ed Tanous25b54db2024-04-17 15:40:31 -07002362 std::optional<nlohmann::json::object_t>>>
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002363 configuration;
2364 if (pidControllers)
2365 {
2366 configuration.emplace_back(
2367 "PidControllers", std::move(pidControllers));
2368 }
2369 if (fanControllers)
2370 {
2371 configuration.emplace_back(
2372 "FanControllers", std::move(fanControllers));
2373 }
2374 if (fanZones)
2375 {
2376 configuration.emplace_back("FanZones",
2377 std::move(fanZones));
2378 }
2379 if (stepwiseControllers)
2380 {
2381 configuration.emplace_back(
2382 "StepwiseControllers",
2383 std::move(stepwiseControllers));
2384 }
2385 auto pid = std::make_shared<SetPIDValues>(
2386 asyncResp, std::move(configuration), profile);
2387 pid->run();
2388 }
2389 else
2390 {
2391 messages::propertyUnknown(asyncResp->res, "Oem");
2392 return;
2393 }
Ed Tanous25b54db2024-04-17 15:40:31 -07002394 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07002395
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002396 if (activeSoftwareImageOdataId)
2397 {
2398 setActiveFirmwareImage(asyncResp,
2399 *activeSoftwareImageOdataId);
2400 }
Ed Tanous9e9b6042024-03-06 14:18:28 -08002401
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002402 if (datetime)
2403 {
2404 setDateTime(asyncResp, *datetime);
2405 }
2406 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002407}
2408
2409inline void requestRoutesManagerCollection(App& app)
2410{
2411 BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
Ed Tanoused398212021-06-09 17:05:54 -07002412 .privileges(redfish::privileges::getManagerCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002413 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002414 [&app](const crow::Request& req,
2415 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -04002416 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2417 {
2418 return;
2419 }
2420 // Collections don't include the static data added by SubRoute
2421 // because it has a duplicate entry for members
2422 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
2423 asyncResp->res.jsonValue["@odata.type"] =
2424 "#ManagerCollection.ManagerCollection";
2425 asyncResp->res.jsonValue["Name"] = "Manager Collection";
2426 asyncResp->res.jsonValue["Members@odata.count"] = 1;
2427 nlohmann::json::array_t members;
2428 nlohmann::json& bmc = members.emplace_back();
2429 bmc["@odata.id"] = boost::urls::format(
2430 "/redfish/v1/Managers/{}", BMCWEB_REDFISH_MANAGER_URI_NAME);
2431 asyncResp->res.jsonValue["Members"] = std::move(members);
2432 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002433}
Ed Tanous1abe55e2018-09-05 08:30:59 -07002434} // namespace redfish