blob: 2f50ca0f16ab3c481c9eb6d8f32d0185f067e259 [file] [log] [blame]
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Willy Tu13451e32023-05-24 16:08:18 -070018#include "bmcweb_config.h"
19
Sui Chena51fc2d2022-07-14 17:21:53 -070020#include "app.hpp"
21#include "dbus_utility.hpp"
James Feistb49ac872019-05-21 15:12:01 -070022#include "health.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070023#include "query.hpp"
Jennifer Leec5d03ff2019-03-08 15:42:58 -080024#include "redfish_util.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070025#include "registries/privilege_registry.hpp"
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +020026#include "utils/dbus_utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080027#include "utils/json_utils.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070028#include "utils/sw_utils.hpp"
29#include "utils/systemd_utils.hpp"
Ed Tanous2b829372022-08-03 14:22:34 -070030#include "utils/time_utils.hpp"
Borawski.Lukasz9c3106852018-02-09 15:24:22 +010031
George Liue99073f2022-12-09 11:06:16 +080032#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070033#include <boost/url/format.hpp>
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +020034#include <sdbusplus/asio/property.hpp>
35#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050036
Ed Tanousa170f272022-06-30 21:53:27 -070037#include <algorithm>
George Liue99073f2022-12-09 11:06:16 +080038#include <array>
Gunnar Mills4bfefa72020-07-30 13:54:29 -050039#include <cstdint>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050040#include <memory>
Konstantin Aladyshev9970e932024-02-20 09:51:29 +030041#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070042#include <ranges>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050043#include <sstream>
Konstantin Aladyshev9970e932024-02-20 09:51:29 +030044#include <string>
George Liue99073f2022-12-09 11:06:16 +080045#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080046#include <variant>
James Feist5b4aa862018-08-16 14:07:01 -070047
Ed Tanous1abe55e2018-09-05 08:30:59 -070048namespace redfish
49{
Jennifer Leeed5befb2018-08-10 11:29:45 -070050
51/**
Gunnar Mills2a5c4402020-05-19 09:07:24 -050052 * Function reboots the BMC.
53 *
54 * @param[in] asyncResp - Shared pointer for completing asynchronous calls
Jennifer Leeed5befb2018-08-10 11:29:45 -070055 */
zhanghch058d1b46d2021-04-01 11:18:24 +080056inline void
57 doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Gunnar Mills2a5c4402020-05-19 09:07:24 -050058{
59 const char* processName = "xyz.openbmc_project.State.BMC";
60 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
61 const char* interfaceName = "xyz.openbmc_project.State.BMC";
62 const std::string& propertyValue =
63 "xyz.openbmc_project.State.BMC.Transition.Reboot";
64 const char* destProperty = "RequestedBMCTransition";
65
66 // Create the D-Bus variant for D-Bus call.
George Liu9ae226f2023-06-21 17:56:46 +080067 sdbusplus::asio::setProperty(
68 *crow::connections::systemBus, processName, objectPath, interfaceName,
69 destProperty, propertyValue,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080070 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -070071 // Use "Set" method to set the property value.
72 if (ec)
73 {
Ed Tanous62598e32023-07-17 17:06:25 -070074 BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -070075 messages::internalError(asyncResp->res);
76 return;
77 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -050078
Ed Tanous002d39b2022-05-31 08:59:27 -070079 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -050080 });
Gunnar Mills2a5c4402020-05-19 09:07:24 -050081}
82
zhanghch058d1b46d2021-04-01 11:18:24 +080083inline void
84 doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +000085{
86 const char* processName = "xyz.openbmc_project.State.BMC";
87 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
88 const char* interfaceName = "xyz.openbmc_project.State.BMC";
89 const std::string& propertyValue =
90 "xyz.openbmc_project.State.BMC.Transition.HardReboot";
91 const char* destProperty = "RequestedBMCTransition";
92
93 // Create the D-Bus variant for D-Bus call.
George Liu9ae226f2023-06-21 17:56:46 +080094 sdbusplus::asio::setProperty(
95 *crow::connections::systemBus, processName, objectPath, interfaceName,
96 destProperty, propertyValue,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080097 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -070098 // Use "Set" method to set the property value.
99 if (ec)
100 {
Ed Tanous62598e32023-07-17 17:06:25 -0700101 BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700102 messages::internalError(asyncResp->res);
103 return;
104 }
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000105
Ed Tanous002d39b2022-05-31 08:59:27 -0700106 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -0500107 });
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000108}
109
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500110/**
111 * ManagerResetAction class supports the POST method for the Reset (reboot)
112 * action.
113 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700114inline void requestRoutesManagerResetAction(App& app)
Jennifer Leeed5befb2018-08-10 11:29:45 -0700115{
Jennifer Leeed5befb2018-08-10 11:29:45 -0700116 /**
Jennifer Leeed5befb2018-08-10 11:29:45 -0700117 * Function handles POST method request.
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500118 * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000119 * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
Jennifer Leeed5befb2018-08-10 11:29:45 -0700120 */
Jennifer Leeed5befb2018-08-10 11:29:45 -0700121
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700122 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
Ed Tanoused398212021-06-09 17:05:54 -0700123 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700124 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700125 [&app](const crow::Request& req,
126 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000127 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700128 {
129 return;
130 }
Ed Tanous62598e32023-07-17 17:06:25 -0700131 BMCWEB_LOG_DEBUG("Post Manager Reset.");
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500132
Ed Tanous002d39b2022-05-31 08:59:27 -0700133 std::string resetType;
Jennifer Leeed5befb2018-08-10 11:29:45 -0700134
Ed Tanous002d39b2022-05-31 08:59:27 -0700135 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
136 resetType))
137 {
138 return;
139 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500140
Ed Tanous002d39b2022-05-31 08:59:27 -0700141 if (resetType == "GracefulRestart")
142 {
Ed Tanous62598e32023-07-17 17:06:25 -0700143 BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
Ed Tanous002d39b2022-05-31 08:59:27 -0700144 doBMCGracefulRestart(asyncResp);
145 return;
146 }
147 if (resetType == "ForceRestart")
148 {
Ed Tanous62598e32023-07-17 17:06:25 -0700149 BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
Ed Tanous002d39b2022-05-31 08:59:27 -0700150 doBMCForceRestart(asyncResp);
151 return;
152 }
Ed Tanous62598e32023-07-17 17:06:25 -0700153 BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}", resetType);
Ed Tanous002d39b2022-05-31 08:59:27 -0700154 messages::actionParameterNotSupported(asyncResp->res, resetType,
155 "ResetType");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700156
Ed Tanous002d39b2022-05-31 08:59:27 -0700157 return;
Patrick Williams5a39f772023-10-20 11:20:21 -0500158 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700159}
Jennifer Leeed5befb2018-08-10 11:29:45 -0700160
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500161/**
162 * ManagerResetToDefaultsAction class supports POST method for factory reset
163 * action.
164 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700165inline void requestRoutesManagerResetToDefaultsAction(App& app)
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500166{
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500167 /**
168 * Function handles ResetToDefaults POST method request.
169 *
170 * Analyzes POST body message and factory resets BMC by calling
171 * BMC code updater factory reset followed by a BMC reboot.
172 *
173 * BMC code updater factory reset wipes the whole BMC read-write
174 * filesystem which includes things like the network settings.
175 *
176 * OpenBMC only supports ResetToDefaultsType "ResetAll".
177 */
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500178
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700179 BMCWEB_ROUTE(app,
180 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/")
Ed Tanoused398212021-06-09 17:05:54 -0700181 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700182 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700183 [&app](const crow::Request& req,
184 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000185 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700186 {
187 return;
188 }
Ed Tanous62598e32023-07-17 17:06:25 -0700189 BMCWEB_LOG_DEBUG("Post ResetToDefaults.");
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500190
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300191 std::optional<std::string> resetType;
192 std::optional<std::string> resetToDefaultsType;
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500193
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300194 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
195 resetType, "ResetToDefaultsType",
196 resetToDefaultsType))
Ed Tanous002d39b2022-05-31 08:59:27 -0700197 {
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300198 BMCWEB_LOG_DEBUG("Missing property ResetType.");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700199
Ed Tanous002d39b2022-05-31 08:59:27 -0700200 messages::actionParameterMissing(asyncResp->res, "ResetToDefaults",
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300201 "ResetType");
Ed Tanous002d39b2022-05-31 08:59:27 -0700202 return;
203 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700204
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300205 if (resetToDefaultsType && !resetType)
206 {
207 BMCWEB_LOG_WARNING(
208 "Using deprecated ResetToDefaultsType, should be ResetType."
209 "Support for the ResetToDefaultsType will be dropped in 2Q24");
210 resetType = resetToDefaultsType;
211 }
212
Ed Tanous002d39b2022-05-31 08:59:27 -0700213 if (resetType != "ResetAll")
214 {
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300215 BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}",
216 *resetType);
217 messages::actionParameterNotSupported(asyncResp->res, *resetType,
218 "ResetType");
Ed Tanous002d39b2022-05-31 08:59:27 -0700219 return;
220 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700221
Ed Tanous002d39b2022-05-31 08:59:27 -0700222 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800223 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700224 if (ec)
225 {
Ed Tanous62598e32023-07-17 17:06:25 -0700226 BMCWEB_LOG_DEBUG("Failed to ResetToDefaults: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700227 messages::internalError(asyncResp->res);
228 return;
229 }
230 // Factory Reset doesn't actually happen until a reboot
231 // Can't erase what the BMC is running on
232 doBMCGracefulRestart(asyncResp);
Patrick Williams5a39f772023-10-20 11:20:21 -0500233 },
Ed Tanous002d39b2022-05-31 08:59:27 -0700234 "xyz.openbmc_project.Software.BMC.Updater",
235 "/xyz/openbmc_project/software",
236 "xyz.openbmc_project.Common.FactoryReset", "Reset");
Patrick Williams5a39f772023-10-20 11:20:21 -0500237 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700238}
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500239
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530240/**
241 * ManagerResetActionInfo derived class for delivering Manager
242 * ResetType AllowableValues using ResetInfo schema.
243 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700244inline void requestRoutesManagerResetActionInfo(App& app)
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530245{
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530246 /**
247 * Functions triggers appropriate requests on DBus
248 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700249
250 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/ResetActionInfo/")
Ed Tanoused398212021-06-09 17:05:54 -0700251 .privileges(redfish::privileges::getActionInfo)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700252 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700253 [&app](const crow::Request& req,
254 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000255 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700256 {
257 return;
258 }
Ed Tanous14766872022-03-15 10:44:42 -0700259
Ed Tanous002d39b2022-05-31 08:59:27 -0700260 asyncResp->res.jsonValue["@odata.type"] =
261 "#ActionInfo.v1_1_2.ActionInfo";
262 asyncResp->res.jsonValue["@odata.id"] =
263 "/redfish/v1/Managers/bmc/ResetActionInfo";
264 asyncResp->res.jsonValue["Name"] = "Reset Action Info";
265 asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
266 nlohmann::json::object_t parameter;
267 parameter["Name"] = "ResetType";
268 parameter["Required"] = true;
269 parameter["DataType"] = "String";
Ed Tanous14766872022-03-15 10:44:42 -0700270
Ed Tanous002d39b2022-05-31 08:59:27 -0700271 nlohmann::json::array_t allowableValues;
Patrick Williamsad539542023-05-12 10:10:08 -0500272 allowableValues.emplace_back("GracefulRestart");
273 allowableValues.emplace_back("ForceRestart");
Ed Tanous002d39b2022-05-31 08:59:27 -0700274 parameter["AllowableValues"] = std::move(allowableValues);
Ed Tanous14766872022-03-15 10:44:42 -0700275
Ed Tanous002d39b2022-05-31 08:59:27 -0700276 nlohmann::json::array_t parameters;
Patrick Williamsad539542023-05-12 10:10:08 -0500277 parameters.emplace_back(std::move(parameter));
Ed Tanous14766872022-03-15 10:44:42 -0700278
Ed Tanous002d39b2022-05-31 08:59:27 -0700279 asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
Patrick Williams5a39f772023-10-20 11:20:21 -0500280 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700281}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530282
James Feist5b4aa862018-08-16 14:07:01 -0700283static constexpr const char* objectManagerIface =
284 "org.freedesktop.DBus.ObjectManager";
285static constexpr const char* pidConfigurationIface =
286 "xyz.openbmc_project.Configuration.Pid";
287static constexpr const char* pidZoneConfigurationIface =
288 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feistb7a08d02018-12-11 14:55:37 -0800289static constexpr const char* stepwiseConfigurationIface =
290 "xyz.openbmc_project.Configuration.Stepwise";
James Feist73df0db2019-03-25 15:29:35 -0700291static constexpr const char* thermalModeIface =
292 "xyz.openbmc_project.Control.ThermalMode";
Borawski.Lukasz9c3106852018-02-09 15:24:22 +0100293
zhanghch058d1b46d2021-04-01 11:18:24 +0800294inline void
295 asyncPopulatePid(const std::string& connection, const std::string& path,
296 const std::string& currentProfile,
297 const std::vector<std::string>& supportedProfiles,
298 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
James Feist5b4aa862018-08-16 14:07:01 -0700299{
George Liu5eb468d2023-06-20 17:03:24 +0800300 sdbusplus::message::object_path objPath(path);
301 dbus::utility::getManagedObjects(
302 connection, objPath,
James Feist73df0db2019-03-25 15:29:35 -0700303 [asyncResp, currentProfile, supportedProfiles](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800304 const boost::system::error_code& ec,
James Feist73df0db2019-03-25 15:29:35 -0700305 const dbus::utility::ManagedObjectType& managedObj) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700306 if (ec)
307 {
Ed Tanous62598e32023-07-17 17:06:25 -0700308 BMCWEB_LOG_ERROR("{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700309 messages::internalError(asyncResp->res);
310 return;
311 }
312 nlohmann::json& configRoot =
313 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
314 nlohmann::json& fans = configRoot["FanControllers"];
315 fans["@odata.type"] = "#OemManager.FanControllers";
316 fans["@odata.id"] =
317 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers";
318
319 nlohmann::json& pids = configRoot["PidControllers"];
320 pids["@odata.type"] = "#OemManager.PidControllers";
321 pids["@odata.id"] =
322 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
323
324 nlohmann::json& stepwise = configRoot["StepwiseControllers"];
325 stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
326 stepwise["@odata.id"] =
327 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
328
329 nlohmann::json& zones = configRoot["FanZones"];
330 zones["@odata.id"] =
331 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
332 zones["@odata.type"] = "#OemManager.FanZones";
333 configRoot["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
334 configRoot["@odata.type"] = "#OemManager.Fan";
335 configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
336
337 if (!currentProfile.empty())
338 {
339 configRoot["Profile"] = currentProfile;
340 }
Carson Labradobf2dded2023-08-10 00:37:06 +0000341 BMCWEB_LOG_DEBUG("profile = {} !", currentProfile);
Ed Tanous002d39b2022-05-31 08:59:27 -0700342
343 for (const auto& pathPair : managedObj)
344 {
345 for (const auto& intfPair : pathPair.second)
James Feist5b4aa862018-08-16 14:07:01 -0700346 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700347 if (intfPair.first != pidConfigurationIface &&
348 intfPair.first != pidZoneConfigurationIface &&
349 intfPair.first != stepwiseConfigurationIface)
James Feist5b4aa862018-08-16 14:07:01 -0700350 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700351 continue;
352 }
James Feist73df0db2019-03-25 15:29:35 -0700353
Ed Tanous002d39b2022-05-31 08:59:27 -0700354 std::string name;
James Feist73df0db2019-03-25 15:29:35 -0700355
Ed Tanous002d39b2022-05-31 08:59:27 -0700356 for (const std::pair<std::string,
357 dbus::utility::DbusVariantType>& propPair :
358 intfPair.second)
359 {
360 if (propPair.first == "Name")
James Feist73df0db2019-03-25 15:29:35 -0700361 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700362 const std::string* namePtr =
363 std::get_if<std::string>(&propPair.second);
364 if (namePtr == nullptr)
James Feist73df0db2019-03-25 15:29:35 -0700365 {
Ed Tanous62598e32023-07-17 17:06:25 -0700366 BMCWEB_LOG_ERROR("Pid Name Field illegal");
James Feistc33a90e2019-03-01 10:17:44 -0800367 messages::internalError(asyncResp->res);
368 return;
369 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700370 name = *namePtr;
371 dbus::utility::escapePathForDbus(name);
James Feistb7a08d02018-12-11 14:55:37 -0800372 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700373 else if (propPair.first == "Profiles")
James Feistb7a08d02018-12-11 14:55:37 -0800374 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700375 const std::vector<std::string>* profiles =
376 std::get_if<std::vector<std::string>>(
377 &propPair.second);
378 if (profiles == nullptr)
James Feistb7a08d02018-12-11 14:55:37 -0800379 {
Ed Tanous62598e32023-07-17 17:06:25 -0700380 BMCWEB_LOG_ERROR("Pid Profiles Field illegal");
James Feistb7a08d02018-12-11 14:55:37 -0800381 messages::internalError(asyncResp->res);
382 return;
383 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700384 if (std::find(profiles->begin(), profiles->end(),
385 currentProfile) == profiles->end())
James Feistb7a08d02018-12-11 14:55:37 -0800386 {
Ed Tanous62598e32023-07-17 17:06:25 -0700387 BMCWEB_LOG_INFO(
388 "{} not supported in current profile", name);
Ed Tanous002d39b2022-05-31 08:59:27 -0700389 continue;
James Feistb7a08d02018-12-11 14:55:37 -0800390 }
391 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700392 }
393 nlohmann::json* config = nullptr;
394 const std::string* classPtr = nullptr;
395
396 for (const std::pair<std::string,
397 dbus::utility::DbusVariantType>& propPair :
398 intfPair.second)
399 {
400 if (propPair.first == "Class")
James Feistb7a08d02018-12-11 14:55:37 -0800401 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700402 classPtr = std::get_if<std::string>(&propPair.second);
403 }
404 }
405
Ed Tanousef4c65b2023-04-24 15:28:50 -0700406 boost::urls::url url("/redfish/v1/Managers/bmc");
Ed Tanous002d39b2022-05-31 08:59:27 -0700407 if (intfPair.first == pidZoneConfigurationIface)
408 {
409 std::string chassis;
410 if (!dbus::utility::getNthStringFromPath(pathPair.first.str,
411 5, chassis))
412 {
413 chassis = "#IllegalValue";
414 }
415 nlohmann::json& zone = zones[name];
Ed Tanousef4c65b2023-04-24 15:28:50 -0700416 zone["Chassis"]["@odata.id"] =
417 boost::urls::format("/redfish/v1/Chassis/{}", chassis);
Willy Tueddfc432022-09-26 16:46:38 +0000418 url.set_fragment(
419 ("/Oem/OpenBmc/Fan/FanZones"_json_pointer / name)
420 .to_string());
421 zone["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700422 zone["@odata.type"] = "#OemManager.FanZone";
423 config = &zone;
424 }
425
426 else if (intfPair.first == stepwiseConfigurationIface)
427 {
428 if (classPtr == nullptr)
429 {
Ed Tanous62598e32023-07-17 17:06:25 -0700430 BMCWEB_LOG_ERROR("Pid Class Field illegal");
James Feistb7a08d02018-12-11 14:55:37 -0800431 messages::internalError(asyncResp->res);
432 return;
433 }
434
Ed Tanous002d39b2022-05-31 08:59:27 -0700435 nlohmann::json& controller = stepwise[name];
436 config = &controller;
Willy Tueddfc432022-09-26 16:46:38 +0000437 url.set_fragment(
438 ("/Oem/OpenBmc/Fan/StepwiseControllers"_json_pointer /
439 name)
440 .to_string());
441 controller["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700442 controller["@odata.type"] =
443 "#OemManager.StepwiseController";
444
445 controller["Direction"] = *classPtr;
446 }
447
448 // pid and fans are off the same configuration
449 else if (intfPair.first == pidConfigurationIface)
450 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700451 if (classPtr == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700452 {
Ed Tanous62598e32023-07-17 17:06:25 -0700453 BMCWEB_LOG_ERROR("Pid Class Field illegal");
Ed Tanous002d39b2022-05-31 08:59:27 -0700454 messages::internalError(asyncResp->res);
455 return;
456 }
457 bool isFan = *classPtr == "fan";
458 nlohmann::json& element = isFan ? fans[name] : pids[name];
459 config = &element;
460 if (isFan)
461 {
Willy Tueddfc432022-09-26 16:46:38 +0000462 url.set_fragment(
463 ("/Oem/OpenBmc/Fan/FanControllers"_json_pointer /
464 name)
465 .to_string());
466 element["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700467 element["@odata.type"] = "#OemManager.FanController";
468 }
469 else
470 {
Willy Tueddfc432022-09-26 16:46:38 +0000471 url.set_fragment(
472 ("/Oem/OpenBmc/Fan/PidControllers"_json_pointer /
473 name)
474 .to_string());
475 element["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700476 element["@odata.type"] = "#OemManager.PidController";
477 }
478 }
479 else
480 {
Ed Tanous62598e32023-07-17 17:06:25 -0700481 BMCWEB_LOG_ERROR("Unexpected configuration");
Ed Tanous002d39b2022-05-31 08:59:27 -0700482 messages::internalError(asyncResp->res);
483 return;
484 }
James Feist5b4aa862018-08-16 14:07:01 -0700485
Ed Tanous002d39b2022-05-31 08:59:27 -0700486 // used for making maps out of 2 vectors
487 const std::vector<double>* keys = nullptr;
488 const std::vector<double>* values = nullptr;
489
490 for (const auto& propertyPair : intfPair.second)
491 {
492 if (propertyPair.first == "Type" ||
493 propertyPair.first == "Class" ||
494 propertyPair.first == "Name")
495 {
496 continue;
497 }
498
499 // zones
500 if (intfPair.first == pidZoneConfigurationIface)
501 {
502 const double* ptr =
503 std::get_if<double>(&propertyPair.second);
504 if (ptr == nullptr)
505 {
Ed Tanous62598e32023-07-17 17:06:25 -0700506 BMCWEB_LOG_ERROR("Field Illegal {}",
507 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700508 messages::internalError(asyncResp->res);
509 return;
510 }
511 (*config)[propertyPair.first] = *ptr;
512 }
513
514 if (intfPair.first == stepwiseConfigurationIface)
515 {
516 if (propertyPair.first == "Reading" ||
517 propertyPair.first == "Output")
518 {
519 const std::vector<double>* ptr =
520 std::get_if<std::vector<double>>(
521 &propertyPair.second);
522
523 if (ptr == nullptr)
524 {
Ed Tanous62598e32023-07-17 17:06:25 -0700525 BMCWEB_LOG_ERROR("Field Illegal {}",
526 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700527 messages::internalError(asyncResp->res);
528 return;
529 }
530
531 if (propertyPair.first == "Reading")
532 {
533 keys = ptr;
534 }
535 else
536 {
537 values = ptr;
538 }
539 if (keys != nullptr && values != nullptr)
540 {
541 if (keys->size() != values->size())
542 {
Ed Tanous62598e32023-07-17 17:06:25 -0700543 BMCWEB_LOG_ERROR(
544 "Reading and Output size don't match ");
Ed Tanous002d39b2022-05-31 08:59:27 -0700545 messages::internalError(asyncResp->res);
546 return;
547 }
548 nlohmann::json& steps = (*config)["Steps"];
549 steps = nlohmann::json::array();
550 for (size_t ii = 0; ii < keys->size(); ii++)
551 {
552 nlohmann::json::object_t step;
553 step["Target"] = (*keys)[ii];
554 step["Output"] = (*values)[ii];
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500555 steps.emplace_back(std::move(step));
Ed Tanous002d39b2022-05-31 08:59:27 -0700556 }
557 }
558 }
559 if (propertyPair.first == "NegativeHysteresis" ||
560 propertyPair.first == "PositiveHysteresis")
James Feist5b4aa862018-08-16 14:07:01 -0700561 {
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800562 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800563 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700564 if (ptr == nullptr)
565 {
Ed Tanous62598e32023-07-17 17:06:25 -0700566 BMCWEB_LOG_ERROR("Field Illegal {}",
567 propertyPair.first);
Jason M. Billsf12894f2018-10-09 12:45:45 -0700568 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700569 return;
570 }
James Feistb7a08d02018-12-11 14:55:37 -0800571 (*config)[propertyPair.first] = *ptr;
572 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700573 }
James Feistb7a08d02018-12-11 14:55:37 -0800574
Ed Tanous002d39b2022-05-31 08:59:27 -0700575 // pid and fans are off the same configuration
576 if (intfPair.first == pidConfigurationIface ||
577 intfPair.first == stepwiseConfigurationIface)
578 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700579 if (propertyPair.first == "Zones")
James Feistb7a08d02018-12-11 14:55:37 -0800580 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700581 const std::vector<std::string>* inputs =
582 std::get_if<std::vector<std::string>>(
583 &propertyPair.second);
584
585 if (inputs == nullptr)
James Feistb7a08d02018-12-11 14:55:37 -0800586 {
Ed Tanous62598e32023-07-17 17:06:25 -0700587 BMCWEB_LOG_ERROR("Zones Pid Field Illegal");
Ed Tanous002d39b2022-05-31 08:59:27 -0700588 messages::internalError(asyncResp->res);
589 return;
James Feistb7a08d02018-12-11 14:55:37 -0800590 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700591 auto& data = (*config)[propertyPair.first];
592 data = nlohmann::json::array();
593 for (std::string itemCopy : *inputs)
James Feistb7a08d02018-12-11 14:55:37 -0800594 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700595 dbus::utility::escapePathForDbus(itemCopy);
596 nlohmann::json::object_t input;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700597 boost::urls::url managerUrl = boost::urls::format(
598 "/redfish/v1/Managers/bmc#{}",
Willy Tueddfc432022-09-26 16:46:38 +0000599 ("/Oem/OpenBmc/Fan/FanZones"_json_pointer /
600 itemCopy)
601 .to_string());
602 input["@odata.id"] = std::move(managerUrl);
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500603 data.emplace_back(std::move(input));
James Feistb7a08d02018-12-11 14:55:37 -0800604 }
James Feist5b4aa862018-08-16 14:07:01 -0700605 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700606 // todo(james): may never happen, but this
607 // assumes configuration data referenced in the
608 // PID config is provided by the same daemon, we
609 // could add another loop to cover all cases,
610 // but I'm okay kicking this can down the road a
611 // bit
James Feist5b4aa862018-08-16 14:07:01 -0700612
Ed Tanous002d39b2022-05-31 08:59:27 -0700613 else if (propertyPair.first == "Inputs" ||
614 propertyPair.first == "Outputs")
James Feist5b4aa862018-08-16 14:07:01 -0700615 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700616 auto& data = (*config)[propertyPair.first];
617 const std::vector<std::string>* inputs =
618 std::get_if<std::vector<std::string>>(
619 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700620
Ed Tanous002d39b2022-05-31 08:59:27 -0700621 if (inputs == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700622 {
Ed Tanous62598e32023-07-17 17:06:25 -0700623 BMCWEB_LOG_ERROR("Field Illegal {}",
624 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700625 messages::internalError(asyncResp->res);
626 return;
James Feist5b4aa862018-08-16 14:07:01 -0700627 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700628 data = *inputs;
629 }
630 else if (propertyPair.first == "SetPointOffset")
631 {
632 const std::string* ptr =
633 std::get_if<std::string>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700634
Ed Tanous002d39b2022-05-31 08:59:27 -0700635 if (ptr == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700636 {
Ed Tanous62598e32023-07-17 17:06:25 -0700637 BMCWEB_LOG_ERROR("Field Illegal {}",
638 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700639 messages::internalError(asyncResp->res);
640 return;
James Feistb943aae2019-07-11 16:33:56 -0700641 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700642 // translate from dbus to redfish
643 if (*ptr == "WarningHigh")
James Feistb943aae2019-07-11 16:33:56 -0700644 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700645 (*config)["SetPointOffset"] =
646 "UpperThresholdNonCritical";
James Feistb943aae2019-07-11 16:33:56 -0700647 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700648 else if (*ptr == "WarningLow")
James Feist5b4aa862018-08-16 14:07:01 -0700649 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700650 (*config)["SetPointOffset"] =
651 "LowerThresholdNonCritical";
James Feist5b4aa862018-08-16 14:07:01 -0700652 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700653 else if (*ptr == "CriticalHigh")
654 {
655 (*config)["SetPointOffset"] =
656 "UpperThresholdCritical";
657 }
658 else if (*ptr == "CriticalLow")
659 {
660 (*config)["SetPointOffset"] =
661 "LowerThresholdCritical";
662 }
663 else
664 {
Ed Tanous62598e32023-07-17 17:06:25 -0700665 BMCWEB_LOG_ERROR("Value Illegal {}", *ptr);
Ed Tanous002d39b2022-05-31 08:59:27 -0700666 messages::internalError(asyncResp->res);
667 return;
668 }
669 }
670 // doubles
671 else if (propertyPair.first == "FFGainCoefficient" ||
672 propertyPair.first == "FFOffCoefficient" ||
673 propertyPair.first == "ICoefficient" ||
674 propertyPair.first == "ILimitMax" ||
675 propertyPair.first == "ILimitMin" ||
676 propertyPair.first == "PositiveHysteresis" ||
677 propertyPair.first == "NegativeHysteresis" ||
678 propertyPair.first == "OutLimitMax" ||
679 propertyPair.first == "OutLimitMin" ||
680 propertyPair.first == "PCoefficient" ||
681 propertyPair.first == "SetPoint" ||
682 propertyPair.first == "SlewNeg" ||
683 propertyPair.first == "SlewPos")
684 {
685 const double* ptr =
686 std::get_if<double>(&propertyPair.second);
687 if (ptr == nullptr)
688 {
Ed Tanous62598e32023-07-17 17:06:25 -0700689 BMCWEB_LOG_ERROR("Field Illegal {}",
690 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700691 messages::internalError(asyncResp->res);
692 return;
693 }
694 (*config)[propertyPair.first] = *ptr;
James Feist5b4aa862018-08-16 14:07:01 -0700695 }
696 }
697 }
698 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700699 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500700 });
James Feist5b4aa862018-08-16 14:07:01 -0700701}
Jennifer Leeca537922018-08-10 10:07:30 -0700702
James Feist83ff9ab2018-08-31 10:18:24 -0700703enum class CreatePIDRet
704{
705 fail,
706 del,
707 patch
708};
709
zhanghch058d1b46d2021-04-01 11:18:24 +0800710inline bool
711 getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
Ed Tanous9e9b6042024-03-06 14:18:28 -0800712 std::vector<nlohmann::json::object_t>& config,
zhanghch058d1b46d2021-04-01 11:18:24 +0800713 std::vector<std::string>& zones)
James Feist5f2caae2018-12-12 14:08:25 -0800714{
James Feistb6baeaa2019-02-21 10:41:40 -0800715 if (config.empty())
716 {
Ed Tanous62598e32023-07-17 17:06:25 -0700717 BMCWEB_LOG_ERROR("Empty Zones");
Ed Tanousf818b042022-06-27 13:17:35 -0700718 messages::propertyValueFormatError(response->res, config, "Zones");
James Feistb6baeaa2019-02-21 10:41:40 -0800719 return false;
720 }
James Feist5f2caae2018-12-12 14:08:25 -0800721 for (auto& odata : config)
722 {
723 std::string path;
Ed Tanous9e9b6042024-03-06 14:18:28 -0800724 if (!redfish::json_util::readJsonObject(odata, response->res,
725 "@odata.id", path))
James Feist5f2caae2018-12-12 14:08:25 -0800726 {
727 return false;
728 }
729 std::string input;
James Feist61adbda2019-03-25 13:03:51 -0700730
731 // 8 below comes from
732 // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
733 // 0 1 2 3 4 5 6 7 8
734 if (!dbus::utility::getNthStringFromPath(path, 8, input))
James Feist5f2caae2018-12-12 14:08:25 -0800735 {
Ed Tanous62598e32023-07-17 17:06:25 -0700736 BMCWEB_LOG_ERROR("Got invalid path {}", path);
737 BMCWEB_LOG_ERROR("Illegal Type Zones");
Ed Tanousf818b042022-06-27 13:17:35 -0700738 messages::propertyValueFormatError(response->res, odata, "Zones");
James Feist5f2caae2018-12-12 14:08:25 -0800739 return false;
740 }
Ed Tanousa170f272022-06-30 21:53:27 -0700741 std::replace(input.begin(), input.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -0800742 zones.emplace_back(std::move(input));
743 }
744 return true;
745}
746
Ed Tanous711ac7a2021-12-20 09:34:41 -0800747inline const dbus::utility::ManagedObjectType::value_type*
James Feist73df0db2019-03-25 15:29:35 -0700748 findChassis(const dbus::utility::ManagedObjectType& managedObj,
Ed Tanous9e9b6042024-03-06 14:18:28 -0800749 std::string_view value, std::string& chassis)
James Feistb6baeaa2019-02-21 10:41:40 -0800750{
Ed Tanous62598e32023-07-17 17:06:25 -0700751 BMCWEB_LOG_DEBUG("Find Chassis: {}", value);
James Feistb6baeaa2019-02-21 10:41:40 -0800752
Ed Tanous9e9b6042024-03-06 14:18:28 -0800753 std::string escaped(value);
Yaswanth Reddy M6ce82fa2023-03-10 07:29:45 +0000754 std::replace(escaped.begin(), escaped.end(), ' ', '_');
James Feistb6baeaa2019-02-21 10:41:40 -0800755 escaped = "/" + escaped;
Ed Tanous3544d2a2023-08-06 18:12:20 -0700756 auto it = std::ranges::find_if(managedObj, [&escaped](const auto& obj) {
Ed Tanous18f8f602023-07-18 10:07:23 -0700757 if (obj.first.str.ends_with(escaped))
Ed Tanous002d39b2022-05-31 08:59:27 -0700758 {
Ed Tanous62598e32023-07-17 17:06:25 -0700759 BMCWEB_LOG_DEBUG("Matched {}", obj.first.str);
Ed Tanous002d39b2022-05-31 08:59:27 -0700760 return true;
761 }
762 return false;
763 });
James Feistb6baeaa2019-02-21 10:41:40 -0800764
765 if (it == managedObj.end())
766 {
James Feist73df0db2019-03-25 15:29:35 -0700767 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800768 }
769 // 5 comes from <chassis-name> being the 5th element
770 // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
James Feist73df0db2019-03-25 15:29:35 -0700771 if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
772 {
773 return &(*it);
774 }
775
776 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800777}
778
Ed Tanous23a21a12020-07-25 04:45:05 +0000779inline CreatePIDRet createPidInterface(
zhanghch058d1b46d2021-04-01 11:18:24 +0800780 const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
Ed Tanous9e9b6042024-03-06 14:18:28 -0800781 std::string_view name, nlohmann::json& jsonValue, const std::string& path,
James Feist83ff9ab2018-08-31 10:18:24 -0700782 const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800783 dbus::utility::DBusPropertiesMap& output, std::string& chassis,
784 const std::string& profile)
James Feist83ff9ab2018-08-31 10:18:24 -0700785{
James Feist5f2caae2018-12-12 14:08:25 -0800786 // common deleter
Ed Tanous9e9b6042024-03-06 14:18:28 -0800787 if (jsonValue == nullptr)
James Feist5f2caae2018-12-12 14:08:25 -0800788 {
789 std::string iface;
790 if (type == "PidControllers" || type == "FanControllers")
791 {
792 iface = pidConfigurationIface;
793 }
794 else if (type == "FanZones")
795 {
796 iface = pidZoneConfigurationIface;
797 }
798 else if (type == "StepwiseControllers")
799 {
800 iface = stepwiseConfigurationIface;
801 }
802 else
803 {
Ed Tanous62598e32023-07-17 17:06:25 -0700804 BMCWEB_LOG_ERROR("Illegal Type {}", type);
James Feist5f2caae2018-12-12 14:08:25 -0800805 messages::propertyUnknown(response->res, type);
806 return CreatePIDRet::fail;
807 }
James Feist6ee7f772020-02-06 16:25:27 -0800808
Ed Tanous62598e32023-07-17 17:06:25 -0700809 BMCWEB_LOG_DEBUG("del {} {}", path, iface);
James Feist5f2caae2018-12-12 14:08:25 -0800810 // delete interface
811 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800812 [response, path](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700813 if (ec)
814 {
Ed Tanous62598e32023-07-17 17:06:25 -0700815 BMCWEB_LOG_ERROR("Error patching {}: {}", path, ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700816 messages::internalError(response->res);
817 return;
818 }
819 messages::success(response->res);
Patrick Williams5a39f772023-10-20 11:20:21 -0500820 },
James Feist5f2caae2018-12-12 14:08:25 -0800821 "xyz.openbmc_project.EntityManager", path, iface, "Delete");
822 return CreatePIDRet::del;
823 }
824
Ed Tanous711ac7a2021-12-20 09:34:41 -0800825 const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800826 if (!createNewObject)
827 {
828 // if we aren't creating a new object, we should be able to find it on
829 // d-bus
Ed Tanous9e9b6042024-03-06 14:18:28 -0800830 managedItem = findChassis(managedObj, name, chassis);
James Feist73df0db2019-03-25 15:29:35 -0700831 if (managedItem == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800832 {
Ed Tanous62598e32023-07-17 17:06:25 -0700833 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousef4c65b2023-04-24 15:28:50 -0700834 messages::invalidObject(
835 response->res,
836 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800837 return CreatePIDRet::fail;
838 }
839 }
840
Ed Tanous26f69762022-01-25 09:49:11 -0800841 if (!profile.empty() &&
James Feist73df0db2019-03-25 15:29:35 -0700842 (type == "PidControllers" || type == "FanControllers" ||
843 type == "StepwiseControllers"))
844 {
845 if (managedItem == nullptr)
846 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800847 output.emplace_back("Profiles", std::vector<std::string>{profile});
James Feist73df0db2019-03-25 15:29:35 -0700848 }
849 else
850 {
851 std::string interface;
852 if (type == "StepwiseControllers")
853 {
854 interface = stepwiseConfigurationIface;
855 }
856 else
857 {
858 interface = pidConfigurationIface;
859 }
Ed Tanous711ac7a2021-12-20 09:34:41 -0800860 bool ifaceFound = false;
861 for (const auto& iface : managedItem->second)
862 {
863 if (iface.first == interface)
864 {
865 ifaceFound = true;
866 for (const auto& prop : iface.second)
867 {
868 if (prop.first == "Profiles")
869 {
870 const std::vector<std::string>* curProfiles =
871 std::get_if<std::vector<std::string>>(
872 &(prop.second));
873 if (curProfiles == nullptr)
874 {
Ed Tanous62598e32023-07-17 17:06:25 -0700875 BMCWEB_LOG_ERROR(
876 "Illegal profiles in managed object");
Ed Tanous711ac7a2021-12-20 09:34:41 -0800877 messages::internalError(response->res);
878 return CreatePIDRet::fail;
879 }
880 if (std::find(curProfiles->begin(),
881 curProfiles->end(),
882 profile) == curProfiles->end())
883 {
884 std::vector<std::string> newProfiles =
885 *curProfiles;
886 newProfiles.push_back(profile);
Ed Tanousb9d36b42022-02-26 21:42:46 -0800887 output.emplace_back("Profiles", newProfiles);
Ed Tanous711ac7a2021-12-20 09:34:41 -0800888 }
889 }
890 }
891 }
892 }
893
894 if (!ifaceFound)
James Feist73df0db2019-03-25 15:29:35 -0700895 {
Ed Tanous62598e32023-07-17 17:06:25 -0700896 BMCWEB_LOG_ERROR("Failed to find interface in managed object");
James Feist73df0db2019-03-25 15:29:35 -0700897 messages::internalError(response->res);
898 return CreatePIDRet::fail;
899 }
James Feist73df0db2019-03-25 15:29:35 -0700900 }
901 }
902
James Feist83ff9ab2018-08-31 10:18:24 -0700903 if (type == "PidControllers" || type == "FanControllers")
904 {
905 if (createNewObject)
906 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800907 output.emplace_back("Class",
908 type == "PidControllers" ? "temp" : "fan");
909 output.emplace_back("Type", "Pid");
James Feist83ff9ab2018-08-31 10:18:24 -0700910 }
James Feist5f2caae2018-12-12 14:08:25 -0800911
Ed Tanous9e9b6042024-03-06 14:18:28 -0800912 std::optional<std::vector<nlohmann::json::object_t>> zones;
James Feist5f2caae2018-12-12 14:08:25 -0800913 std::optional<std::vector<std::string>> inputs;
914 std::optional<std::vector<std::string>> outputs;
915 std::map<std::string, std::optional<double>> doubles;
James Feistb943aae2019-07-11 16:33:56 -0700916 std::optional<std::string> setpointOffset;
James Feist5f2caae2018-12-12 14:08:25 -0800917 if (!redfish::json_util::readJson(
Ed Tanous9e9b6042024-03-06 14:18:28 -0800918 jsonValue, response->res, "Inputs", inputs, "Outputs", outputs,
James Feist5f2caae2018-12-12 14:08:25 -0800919 "Zones", zones, "FFGainCoefficient",
920 doubles["FFGainCoefficient"], "FFOffCoefficient",
921 doubles["FFOffCoefficient"], "ICoefficient",
922 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
923 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
924 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
925 "PCoefficient", doubles["PCoefficient"], "SetPoint",
James Feistb943aae2019-07-11 16:33:56 -0700926 doubles["SetPoint"], "SetPointOffset", setpointOffset,
927 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
928 "PositiveHysteresis", doubles["PositiveHysteresis"],
929 "NegativeHysteresis", doubles["NegativeHysteresis"]))
James Feist83ff9ab2018-08-31 10:18:24 -0700930 {
James Feist5f2caae2018-12-12 14:08:25 -0800931 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700932 }
James Feist5f2caae2018-12-12 14:08:25 -0800933 if (zones)
James Feist83ff9ab2018-08-31 10:18:24 -0700934 {
James Feist5f2caae2018-12-12 14:08:25 -0800935 std::vector<std::string> zonesStr;
936 if (!getZonesFromJsonReq(response, *zones, zonesStr))
James Feist83ff9ab2018-08-31 10:18:24 -0700937 {
Ed Tanous62598e32023-07-17 17:06:25 -0700938 BMCWEB_LOG_ERROR("Illegal Zones");
James Feist5f2caae2018-12-12 14:08:25 -0800939 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700940 }
James Feistb6baeaa2019-02-21 10:41:40 -0800941 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -0800942 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800943 {
Ed Tanous62598e32023-07-17 17:06:25 -0700944 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousace85d62021-10-26 12:45:59 -0700945 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -0700946 response->res,
947 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800948 return CreatePIDRet::fail;
949 }
Ed Tanousb9d36b42022-02-26 21:42:46 -0800950 output.emplace_back("Zones", std::move(zonesStr));
James Feist5f2caae2018-12-12 14:08:25 -0800951 }
Ed Tanousafb9ee02022-12-21 11:59:17 -0800952
953 if (inputs)
James Feist5f2caae2018-12-12 14:08:25 -0800954 {
Ed Tanousafb9ee02022-12-21 11:59:17 -0800955 for (std::string& value : *inputs)
James Feist83ff9ab2018-08-31 10:18:24 -0700956 {
Ed Tanousafb9ee02022-12-21 11:59:17 -0800957 std::replace(value.begin(), value.end(), '_', ' ');
James Feist83ff9ab2018-08-31 10:18:24 -0700958 }
Ed Tanousafb9ee02022-12-21 11:59:17 -0800959 output.emplace_back("Inputs", *inputs);
960 }
961
962 if (outputs)
963 {
964 for (std::string& value : *outputs)
965 {
966 std::replace(value.begin(), value.end(), '_', ' ');
967 }
968 output.emplace_back("Outputs", *outputs);
James Feist5f2caae2018-12-12 14:08:25 -0800969 }
James Feist83ff9ab2018-08-31 10:18:24 -0700970
James Feistb943aae2019-07-11 16:33:56 -0700971 if (setpointOffset)
972 {
973 // translate between redfish and dbus names
974 if (*setpointOffset == "UpperThresholdNonCritical")
975 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800976 output.emplace_back("SetPointOffset", "WarningLow");
James Feistb943aae2019-07-11 16:33:56 -0700977 }
978 else if (*setpointOffset == "LowerThresholdNonCritical")
979 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800980 output.emplace_back("SetPointOffset", "WarningHigh");
James Feistb943aae2019-07-11 16:33:56 -0700981 }
982 else if (*setpointOffset == "LowerThresholdCritical")
983 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800984 output.emplace_back("SetPointOffset", "CriticalLow");
James Feistb943aae2019-07-11 16:33:56 -0700985 }
986 else if (*setpointOffset == "UpperThresholdCritical")
987 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800988 output.emplace_back("SetPointOffset", "CriticalHigh");
James Feistb943aae2019-07-11 16:33:56 -0700989 }
990 else
991 {
Ed Tanous62598e32023-07-17 17:06:25 -0700992 BMCWEB_LOG_ERROR("Invalid setpointoffset {}", *setpointOffset);
Ed Tanous9e9b6042024-03-06 14:18:28 -0800993 messages::propertyValueNotInList(response->res, name,
Ed Tanousace85d62021-10-26 12:45:59 -0700994 "SetPointOffset");
James Feistb943aae2019-07-11 16:33:56 -0700995 return CreatePIDRet::fail;
996 }
997 }
998
James Feist5f2caae2018-12-12 14:08:25 -0800999 // doubles
1000 for (const auto& pairs : doubles)
1001 {
1002 if (!pairs.second)
James Feist83ff9ab2018-08-31 10:18:24 -07001003 {
James Feist5f2caae2018-12-12 14:08:25 -08001004 continue;
James Feist83ff9ab2018-08-31 10:18:24 -07001005 }
Ed Tanous62598e32023-07-17 17:06:25 -07001006 BMCWEB_LOG_DEBUG("{} = {}", pairs.first, *pairs.second);
Ed Tanousb9d36b42022-02-26 21:42:46 -08001007 output.emplace_back(pairs.first, *pairs.second);
James Feist83ff9ab2018-08-31 10:18:24 -07001008 }
1009 }
James Feist5f2caae2018-12-12 14:08:25 -08001010
James Feist83ff9ab2018-08-31 10:18:24 -07001011 else if (type == "FanZones")
1012 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001013 output.emplace_back("Type", "Pid.Zone");
James Feist83ff9ab2018-08-31 10:18:24 -07001014
Ed Tanous9e9b6042024-03-06 14:18:28 -08001015 std::optional<std::string> chassisId;
James Feist5f2caae2018-12-12 14:08:25 -08001016 std::optional<double> failSafePercent;
James Feistd3ec07f2019-02-25 14:51:15 -08001017 std::optional<double> minThermalOutput;
Ed Tanous9e9b6042024-03-06 14:18:28 -08001018 if (!redfish::json_util::readJson(jsonValue, response->res,
1019 "Chassis/@odata.id", chassisId,
1020 "FailSafePercent", failSafePercent,
1021 "MinThermalOutput", minThermalOutput))
James Feist83ff9ab2018-08-31 10:18:24 -07001022 {
James Feist5f2caae2018-12-12 14:08:25 -08001023 return CreatePIDRet::fail;
1024 }
James Feist83ff9ab2018-08-31 10:18:24 -07001025
Ed Tanous9e9b6042024-03-06 14:18:28 -08001026 if (chassisId)
James Feist5f2caae2018-12-12 14:08:25 -08001027 {
AppaRao Puli717794d2019-10-18 22:54:53 +05301028 // /redfish/v1/chassis/chassis_name/
Ed Tanous9e9b6042024-03-06 14:18:28 -08001029 if (!dbus::utility::getNthStringFromPath(*chassisId, 3, chassis))
James Feist5f2caae2018-12-12 14:08:25 -08001030 {
Ed Tanous9e9b6042024-03-06 14:18:28 -08001031 BMCWEB_LOG_ERROR("Got invalid path {}", *chassisId);
Ed Tanousace85d62021-10-26 12:45:59 -07001032 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001033 response->res,
Ed Tanous9e9b6042024-03-06 14:18:28 -08001034 boost::urls::format("/redfish/v1/Chassis/{}", *chassisId));
James Feist5f2caae2018-12-12 14:08:25 -08001035 return CreatePIDRet::fail;
1036 }
1037 }
James Feistd3ec07f2019-02-25 14:51:15 -08001038 if (minThermalOutput)
James Feist5f2caae2018-12-12 14:08:25 -08001039 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001040 output.emplace_back("MinThermalOutput", *minThermalOutput);
James Feist5f2caae2018-12-12 14:08:25 -08001041 }
1042 if (failSafePercent)
1043 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001044 output.emplace_back("FailSafePercent", *failSafePercent);
James Feist5f2caae2018-12-12 14:08:25 -08001045 }
1046 }
1047 else if (type == "StepwiseControllers")
1048 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001049 output.emplace_back("Type", "Stepwise");
James Feist5f2caae2018-12-12 14:08:25 -08001050
Ed Tanous9e9b6042024-03-06 14:18:28 -08001051 std::optional<std::vector<nlohmann::json::object_t>> zones;
1052 std::optional<std::vector<nlohmann::json::object_t>> steps;
James Feist5f2caae2018-12-12 14:08:25 -08001053 std::optional<std::vector<std::string>> inputs;
1054 std::optional<double> positiveHysteresis;
1055 std::optional<double> negativeHysteresis;
James Feistc33a90e2019-03-01 10:17:44 -08001056 std::optional<std::string> direction; // upper clipping curve vs lower
James Feist5f2caae2018-12-12 14:08:25 -08001057 if (!redfish::json_util::readJson(
Ed Tanous9e9b6042024-03-06 14:18:28 -08001058 jsonValue, response->res, "Zones", zones, "Steps", steps,
James Feistb6baeaa2019-02-21 10:41:40 -08001059 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
James Feistc33a90e2019-03-01 10:17:44 -08001060 "NegativeHysteresis", negativeHysteresis, "Direction",
1061 direction))
James Feist5f2caae2018-12-12 14:08:25 -08001062 {
James Feist5f2caae2018-12-12 14:08:25 -08001063 return CreatePIDRet::fail;
1064 }
1065
1066 if (zones)
1067 {
James Feistb6baeaa2019-02-21 10:41:40 -08001068 std::vector<std::string> zonesStrs;
1069 if (!getZonesFromJsonReq(response, *zones, zonesStrs))
James Feist5f2caae2018-12-12 14:08:25 -08001070 {
Ed Tanous62598e32023-07-17 17:06:25 -07001071 BMCWEB_LOG_ERROR("Illegal Zones");
James Feist5f2caae2018-12-12 14:08:25 -08001072 return CreatePIDRet::fail;
1073 }
James Feistb6baeaa2019-02-21 10:41:40 -08001074 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -08001075 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -08001076 {
Ed Tanous62598e32023-07-17 17:06:25 -07001077 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousace85d62021-10-26 12:45:59 -07001078 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001079 response->res,
1080 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -08001081 return CreatePIDRet::fail;
1082 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001083 output.emplace_back("Zones", std::move(zonesStrs));
James Feist5f2caae2018-12-12 14:08:25 -08001084 }
1085 if (steps)
1086 {
1087 std::vector<double> readings;
1088 std::vector<double> outputs;
1089 for (auto& step : *steps)
1090 {
Ed Tanous543f4402022-01-06 13:12:53 -08001091 double target = 0.0;
1092 double out = 0.0;
James Feist5f2caae2018-12-12 14:08:25 -08001093
Ed Tanous9e9b6042024-03-06 14:18:28 -08001094 if (!redfish::json_util::readJsonObject(
1095 step, response->res, "Target", target, "Output", out))
James Feist5f2caae2018-12-12 14:08:25 -08001096 {
James Feist5f2caae2018-12-12 14:08:25 -08001097 return CreatePIDRet::fail;
1098 }
1099 readings.emplace_back(target);
Ed Tanous23a21a12020-07-25 04:45:05 +00001100 outputs.emplace_back(out);
James Feist5f2caae2018-12-12 14:08:25 -08001101 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001102 output.emplace_back("Reading", std::move(readings));
1103 output.emplace_back("Output", std::move(outputs));
James Feist5f2caae2018-12-12 14:08:25 -08001104 }
1105 if (inputs)
1106 {
1107 for (std::string& value : *inputs)
1108 {
Ed Tanousa170f272022-06-30 21:53:27 -07001109 std::replace(value.begin(), value.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -08001110 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001111 output.emplace_back("Inputs", std::move(*inputs));
James Feist5f2caae2018-12-12 14:08:25 -08001112 }
1113 if (negativeHysteresis)
1114 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001115 output.emplace_back("NegativeHysteresis", *negativeHysteresis);
James Feist5f2caae2018-12-12 14:08:25 -08001116 }
1117 if (positiveHysteresis)
1118 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001119 output.emplace_back("PositiveHysteresis", *positiveHysteresis);
James Feist83ff9ab2018-08-31 10:18:24 -07001120 }
James Feistc33a90e2019-03-01 10:17:44 -08001121 if (direction)
1122 {
1123 constexpr const std::array<const char*, 2> allowedDirections = {
1124 "Ceiling", "Floor"};
Ed Tanous3544d2a2023-08-06 18:12:20 -07001125 if (std::ranges::find(allowedDirections, *direction) ==
1126 allowedDirections.end())
James Feistc33a90e2019-03-01 10:17:44 -08001127 {
1128 messages::propertyValueTypeError(response->res, "Direction",
1129 *direction);
1130 return CreatePIDRet::fail;
1131 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001132 output.emplace_back("Class", *direction);
James Feistc33a90e2019-03-01 10:17:44 -08001133 }
James Feist83ff9ab2018-08-31 10:18:24 -07001134 }
1135 else
1136 {
Ed Tanous62598e32023-07-17 17:06:25 -07001137 BMCWEB_LOG_ERROR("Illegal Type {}", type);
Jason M. Bills35a62c72018-10-09 12:45:45 -07001138 messages::propertyUnknown(response->res, type);
James Feist83ff9ab2018-08-31 10:18:24 -07001139 return CreatePIDRet::fail;
1140 }
1141 return CreatePIDRet::patch;
1142}
James Feist73df0db2019-03-25 15:29:35 -07001143struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
1144{
Ed Tanous6936afe2022-09-08 15:10:39 -07001145 struct CompletionValues
1146 {
1147 std::vector<std::string> supportedProfiles;
1148 std::string currentProfile;
1149 dbus::utility::MapperGetSubTreeResponse subtree;
1150 };
James Feist73df0db2019-03-25 15:29:35 -07001151
Ed Tanous4e23a442022-06-06 09:57:26 -07001152 explicit GetPIDValues(
1153 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
Ed Tanous23a21a12020-07-25 04:45:05 +00001154 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001155
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001156 {}
James Feist73df0db2019-03-25 15:29:35 -07001157
1158 void run()
1159 {
1160 std::shared_ptr<GetPIDValues> self = shared_from_this();
1161
1162 // get all configurations
George Liue99073f2022-12-09 11:06:16 +08001163 constexpr std::array<std::string_view, 4> interfaces = {
1164 pidConfigurationIface, pidZoneConfigurationIface,
1165 objectManagerIface, stepwiseConfigurationIface};
1166 dbus::utility::getSubTree(
1167 "/", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001168 [self](
George Liue99073f2022-12-09 11:06:16 +08001169 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001170 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001171 if (ec)
1172 {
Ed Tanous62598e32023-07-17 17:06:25 -07001173 BMCWEB_LOG_ERROR("{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001174 messages::internalError(self->asyncResp->res);
1175 return;
1176 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001177 self->complete.subtree = subtreeLocal;
Patrick Williams5a39f772023-10-20 11:20:21 -05001178 });
James Feist73df0db2019-03-25 15:29:35 -07001179
1180 // at the same time get the selected profile
George Liue99073f2022-12-09 11:06:16 +08001181 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1182 thermalModeIface};
1183 dbus::utility::getSubTree(
1184 "/", 0, thermalModeIfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001185 [self](
George Liue99073f2022-12-09 11:06:16 +08001186 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001187 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001188 if (ec || subtreeLocal.empty())
1189 {
1190 return;
1191 }
1192 if (subtreeLocal[0].second.size() != 1)
1193 {
1194 // invalid mapper response, should never happen
Ed Tanous62598e32023-07-17 17:06:25 -07001195 BMCWEB_LOG_ERROR("GetPIDValues: Mapper Error");
Ed Tanous002d39b2022-05-31 08:59:27 -07001196 messages::internalError(self->asyncResp->res);
1197 return;
1198 }
1199
1200 const std::string& path = subtreeLocal[0].first;
1201 const std::string& owner = subtreeLocal[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001202
1203 sdbusplus::asio::getAllProperties(
1204 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous002d39b2022-05-31 08:59:27 -07001205 [path, owner,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001206 self](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001207 const dbus::utility::DBusPropertiesMap& resp) {
1208 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001209 {
Ed Tanous62598e32023-07-17 17:06:25 -07001210 BMCWEB_LOG_ERROR(
1211 "GetPIDValues: Can't get thermalModeIface {}", path);
James Feist73df0db2019-03-25 15:29:35 -07001212 messages::internalError(self->asyncResp->res);
1213 return;
1214 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001215
Ed Tanous002d39b2022-05-31 08:59:27 -07001216 const std::string* current = nullptr;
1217 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001218
1219 const bool success = sdbusplus::unpackPropertiesNoThrow(
1220 dbus_utils::UnpackErrorPrinter(), resp, "Current", current,
1221 "Supported", supported);
1222
1223 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001224 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001225 messages::internalError(self->asyncResp->res);
1226 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001227 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001228
Ed Tanous002d39b2022-05-31 08:59:27 -07001229 if (current == nullptr || supported == nullptr)
1230 {
Ed Tanous62598e32023-07-17 17:06:25 -07001231 BMCWEB_LOG_ERROR(
1232 "GetPIDValues: thermal mode iface invalid {}", path);
Ed Tanous002d39b2022-05-31 08:59:27 -07001233 messages::internalError(self->asyncResp->res);
1234 return;
1235 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001236 self->complete.currentProfile = *current;
1237 self->complete.supportedProfiles = *supported;
George Liue99073f2022-12-09 11:06:16 +08001238 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001239 });
James Feist73df0db2019-03-25 15:29:35 -07001240 }
1241
Ed Tanous6936afe2022-09-08 15:10:39 -07001242 static void
1243 processingComplete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1244 const CompletionValues& completion)
James Feist73df0db2019-03-25 15:29:35 -07001245 {
1246 if (asyncResp->res.result() != boost::beast::http::status::ok)
1247 {
1248 return;
1249 }
1250 // create map of <connection, path to objMgr>>
Ed Tanous6936afe2022-09-08 15:10:39 -07001251 boost::container::flat_map<
1252 std::string, std::string, std::less<>,
1253 std::vector<std::pair<std::string, std::string>>>
1254 objectMgrPaths;
1255 boost::container::flat_set<std::string, std::less<>,
1256 std::vector<std::string>>
1257 calledConnections;
1258 for (const auto& pathGroup : completion.subtree)
James Feist73df0db2019-03-25 15:29:35 -07001259 {
1260 for (const auto& connectionGroup : pathGroup.second)
1261 {
1262 auto findConnection =
1263 calledConnections.find(connectionGroup.first);
1264 if (findConnection != calledConnections.end())
1265 {
1266 break;
1267 }
1268 for (const std::string& interface : connectionGroup.second)
1269 {
1270 if (interface == objectManagerIface)
1271 {
1272 objectMgrPaths[connectionGroup.first] = pathGroup.first;
1273 }
1274 // this list is alphabetical, so we
1275 // should have found the objMgr by now
1276 if (interface == pidConfigurationIface ||
1277 interface == pidZoneConfigurationIface ||
1278 interface == stepwiseConfigurationIface)
1279 {
1280 auto findObjMgr =
1281 objectMgrPaths.find(connectionGroup.first);
1282 if (findObjMgr == objectMgrPaths.end())
1283 {
Ed Tanous62598e32023-07-17 17:06:25 -07001284 BMCWEB_LOG_DEBUG("{}Has no Object Manager",
1285 connectionGroup.first);
James Feist73df0db2019-03-25 15:29:35 -07001286 continue;
1287 }
1288
1289 calledConnections.insert(connectionGroup.first);
1290
1291 asyncPopulatePid(findObjMgr->first, findObjMgr->second,
Ed Tanous6936afe2022-09-08 15:10:39 -07001292 completion.currentProfile,
1293 completion.supportedProfiles,
James Feist73df0db2019-03-25 15:29:35 -07001294 asyncResp);
1295 break;
1296 }
1297 }
1298 }
1299 }
1300 }
1301
Ed Tanous6936afe2022-09-08 15:10:39 -07001302 ~GetPIDValues()
1303 {
1304 boost::asio::post(crow::connections::systemBus->get_io_context(),
1305 std::bind_front(&processingComplete, asyncResp,
1306 std::move(complete)));
1307 }
1308
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001309 GetPIDValues(const GetPIDValues&) = delete;
1310 GetPIDValues(GetPIDValues&&) = delete;
1311 GetPIDValues& operator=(const GetPIDValues&) = delete;
1312 GetPIDValues& operator=(GetPIDValues&&) = delete;
1313
zhanghch058d1b46d2021-04-01 11:18:24 +08001314 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Ed Tanous6936afe2022-09-08 15:10:39 -07001315 CompletionValues complete;
James Feist73df0db2019-03-25 15:29:35 -07001316};
1317
1318struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
1319{
Ed Tanous9e9b6042024-03-06 14:18:28 -08001320 SetPIDValues(
1321 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
1322 std::vector<
1323 std::pair<std::string, std::optional<nlohmann::json::object_t>>>&&
1324 configurationsIn,
1325 std::optional<std::string>& profileIn) :
1326 asyncResp(asyncRespIn),
1327 configuration(std::move(configurationsIn)),
1328 profile(std::move(profileIn))
1329 {}
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001330
1331 SetPIDValues(const SetPIDValues&) = delete;
1332 SetPIDValues(SetPIDValues&&) = delete;
1333 SetPIDValues& operator=(const SetPIDValues&) = delete;
1334 SetPIDValues& operator=(SetPIDValues&&) = delete;
1335
James Feist73df0db2019-03-25 15:29:35 -07001336 void run()
1337 {
1338 if (asyncResp->res.result() != boost::beast::http::status::ok)
1339 {
1340 return;
1341 }
1342
1343 std::shared_ptr<SetPIDValues> self = shared_from_this();
1344
1345 // todo(james): might make sense to do a mapper call here if this
1346 // interface gets more traction
George Liu5eb468d2023-06-20 17:03:24 +08001347 sdbusplus::message::object_path objPath(
1348 "/xyz/openbmc_project/inventory");
1349 dbus::utility::getManagedObjects(
1350 "xyz.openbmc_project.EntityManager", objPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001351 [self](const boost::system::error_code& ec,
Ed Tanous914e2d52022-01-07 11:38:34 -08001352 const dbus::utility::ManagedObjectType& mObj) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001353 if (ec)
1354 {
Ed Tanous62598e32023-07-17 17:06:25 -07001355 BMCWEB_LOG_ERROR("Error communicating to Entity Manager");
Ed Tanous002d39b2022-05-31 08:59:27 -07001356 messages::internalError(self->asyncResp->res);
1357 return;
1358 }
1359 const std::array<const char*, 3> configurations = {
1360 pidConfigurationIface, pidZoneConfigurationIface,
1361 stepwiseConfigurationIface};
James Feiste69d9de2020-02-07 12:23:27 -08001362
Ed Tanous002d39b2022-05-31 08:59:27 -07001363 for (const auto& [path, object] : mObj)
1364 {
1365 for (const auto& [interface, _] : object)
James Feiste69d9de2020-02-07 12:23:27 -08001366 {
Ed Tanous3544d2a2023-08-06 18:12:20 -07001367 if (std::ranges::find(configurations, interface) !=
1368 configurations.end())
James Feiste69d9de2020-02-07 12:23:27 -08001369 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001370 self->objectCount++;
1371 break;
James Feiste69d9de2020-02-07 12:23:27 -08001372 }
James Feiste69d9de2020-02-07 12:23:27 -08001373 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001374 }
1375 self->managedObj = mObj;
Patrick Williams5a39f772023-10-20 11:20:21 -05001376 });
James Feist73df0db2019-03-25 15:29:35 -07001377
1378 // at the same time get the profile information
George Liue99073f2022-12-09 11:06:16 +08001379 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1380 thermalModeIface};
1381 dbus::utility::getSubTree(
1382 "/", 0, thermalModeIfaces,
1383 [self](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001384 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001385 if (ec || subtree.empty())
1386 {
1387 return;
1388 }
1389 if (subtree[0].second.empty())
1390 {
1391 // invalid mapper response, should never happen
Ed Tanous62598e32023-07-17 17:06:25 -07001392 BMCWEB_LOG_ERROR("SetPIDValues: Mapper Error");
Ed Tanous002d39b2022-05-31 08:59:27 -07001393 messages::internalError(self->asyncResp->res);
1394 return;
1395 }
1396
1397 const std::string& path = subtree[0].first;
1398 const std::string& owner = subtree[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001399 sdbusplus::asio::getAllProperties(
1400 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001401 [self, path, owner](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001402 const dbus::utility::DBusPropertiesMap& r) {
1403 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001404 {
Ed Tanous62598e32023-07-17 17:06:25 -07001405 BMCWEB_LOG_ERROR(
1406 "SetPIDValues: Can't get thermalModeIface {}", path);
James Feist73df0db2019-03-25 15:29:35 -07001407 messages::internalError(self->asyncResp->res);
1408 return;
1409 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001410 const std::string* current = nullptr;
1411 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001412
1413 const bool success = sdbusplus::unpackPropertiesNoThrow(
1414 dbus_utils::UnpackErrorPrinter(), r, "Current", current,
1415 "Supported", supported);
1416
1417 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001418 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001419 messages::internalError(self->asyncResp->res);
1420 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001421 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001422
Ed Tanous002d39b2022-05-31 08:59:27 -07001423 if (current == nullptr || supported == nullptr)
1424 {
Ed Tanous62598e32023-07-17 17:06:25 -07001425 BMCWEB_LOG_ERROR(
1426 "SetPIDValues: thermal mode iface invalid {}", path);
Ed Tanous002d39b2022-05-31 08:59:27 -07001427 messages::internalError(self->asyncResp->res);
1428 return;
1429 }
1430 self->currentProfile = *current;
1431 self->supportedProfiles = *supported;
1432 self->profileConnection = owner;
1433 self->profilePath = path;
George Liue99073f2022-12-09 11:06:16 +08001434 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001435 });
James Feist73df0db2019-03-25 15:29:35 -07001436 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001437 void pidSetDone()
James Feist73df0db2019-03-25 15:29:35 -07001438 {
1439 if (asyncResp->res.result() != boost::beast::http::status::ok)
1440 {
1441 return;
1442 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001443 std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001444 if (profile)
1445 {
Ed Tanous3544d2a2023-08-06 18:12:20 -07001446 if (std::ranges::find(supportedProfiles, *profile) ==
1447 supportedProfiles.end())
James Feist73df0db2019-03-25 15:29:35 -07001448 {
1449 messages::actionParameterUnknown(response->res, "Profile",
1450 *profile);
1451 return;
1452 }
1453 currentProfile = *profile;
George Liu9ae226f2023-06-21 17:56:46 +08001454 sdbusplus::asio::setProperty(
1455 *crow::connections::systemBus, profileConnection, profilePath,
1456 thermalModeIface, "Current", *profile,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001457 [response](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001458 if (ec)
1459 {
Ed Tanous62598e32023-07-17 17:06:25 -07001460 BMCWEB_LOG_ERROR("Error patching profile{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001461 messages::internalError(response->res);
1462 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001463 });
James Feist73df0db2019-03-25 15:29:35 -07001464 }
1465
1466 for (auto& containerPair : configuration)
1467 {
1468 auto& container = containerPair.second;
1469 if (!container)
1470 {
1471 continue;
1472 }
James Feist6ee7f772020-02-06 16:25:27 -08001473
Ed Tanous02cad962022-06-30 16:50:15 -07001474 const std::string& type = containerPair.first;
James Feist73df0db2019-03-25 15:29:35 -07001475
Ed Tanous9e9b6042024-03-06 14:18:28 -08001476 for (auto& [name, value] : *container)
James Feist73df0db2019-03-25 15:29:35 -07001477 {
Potin Laicddbf3d2023-02-14 14:28:58 +08001478 std::string dbusObjName = name;
1479 std::replace(dbusObjName.begin(), dbusObjName.end(), ' ', '_');
Ed Tanous62598e32023-07-17 17:06:25 -07001480 BMCWEB_LOG_DEBUG("looking for {}", name);
James Feist6ee7f772020-02-06 16:25:27 -08001481
Ed Tanous3544d2a2023-08-06 18:12:20 -07001482 auto pathItr = std::ranges::find_if(
1483 managedObj, [&dbusObjName](const auto& obj) {
Ed Tanous18f8f602023-07-18 10:07:23 -07001484 return obj.first.parent_path() == dbusObjName;
Patrick Williams5a39f772023-10-20 11:20:21 -05001485 });
Ed Tanousb9d36b42022-02-26 21:42:46 -08001486 dbus::utility::DBusPropertiesMap output;
James Feist73df0db2019-03-25 15:29:35 -07001487
1488 output.reserve(16); // The pid interface length
1489
1490 // determines if we're patching entity-manager or
1491 // creating a new object
1492 bool createNewObject = (pathItr == managedObj.end());
Ed Tanous62598e32023-07-17 17:06:25 -07001493 BMCWEB_LOG_DEBUG("Found = {}", !createNewObject);
James Feist6ee7f772020-02-06 16:25:27 -08001494
James Feist73df0db2019-03-25 15:29:35 -07001495 std::string iface;
Ed Tanousea2b6702022-03-07 16:48:38 -08001496 if (!createNewObject)
James Feist73df0db2019-03-25 15:29:35 -07001497 {
Potin Lai8be2b5b2022-11-22 13:27:16 +08001498 bool findInterface = false;
Ed Tanousea2b6702022-03-07 16:48:38 -08001499 for (const auto& interface : pathItr->second)
James Feist73df0db2019-03-25 15:29:35 -07001500 {
Ed Tanousea2b6702022-03-07 16:48:38 -08001501 if (interface.first == pidConfigurationIface)
1502 {
1503 if (type == "PidControllers" ||
1504 type == "FanControllers")
1505 {
1506 iface = pidConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001507 findInterface = true;
1508 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001509 }
1510 }
1511 else if (interface.first == pidZoneConfigurationIface)
1512 {
1513 if (type == "FanZones")
1514 {
PavanKumarIntelda393502024-03-15 05:47:02 +00001515 iface = pidZoneConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001516 findInterface = true;
1517 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001518 }
1519 }
1520 else if (interface.first == stepwiseConfigurationIface)
1521 {
1522 if (type == "StepwiseControllers")
1523 {
1524 iface = stepwiseConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001525 findInterface = true;
1526 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001527 }
1528 }
James Feist73df0db2019-03-25 15:29:35 -07001529 }
Potin Lai8be2b5b2022-11-22 13:27:16 +08001530
1531 // create new object if interface not found
1532 if (!findInterface)
1533 {
1534 createNewObject = true;
1535 }
James Feist73df0db2019-03-25 15:29:35 -07001536 }
James Feist6ee7f772020-02-06 16:25:27 -08001537
Ed Tanous9e9b6042024-03-06 14:18:28 -08001538 if (createNewObject && value == nullptr)
James Feist6ee7f772020-02-06 16:25:27 -08001539 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -05001540 // can't delete a non-existent object
Ed Tanous9e9b6042024-03-06 14:18:28 -08001541 messages::propertyValueNotInList(response->res, value,
Ed Tanouse2616cc2022-06-27 12:45:55 -07001542 name);
James Feist6ee7f772020-02-06 16:25:27 -08001543 continue;
1544 }
1545
1546 std::string path;
1547 if (pathItr != managedObj.end())
1548 {
1549 path = pathItr->first.str;
1550 }
1551
Ed Tanous62598e32023-07-17 17:06:25 -07001552 BMCWEB_LOG_DEBUG("Create new = {}", createNewObject);
James Feiste69d9de2020-02-07 12:23:27 -08001553
1554 // arbitrary limit to avoid attacks
1555 constexpr const size_t controllerLimit = 500;
James Feist14b0b8d2020-02-12 11:52:07 -08001556 if (createNewObject && objectCount >= controllerLimit)
James Feiste69d9de2020-02-07 12:23:27 -08001557 {
1558 messages::resourceExhaustion(response->res, type);
1559 continue;
1560 }
Ed Tanousa170f272022-06-30 21:53:27 -07001561 std::string escaped = name;
1562 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1563 output.emplace_back("Name", escaped);
James Feist73df0db2019-03-25 15:29:35 -07001564
1565 std::string chassis;
1566 CreatePIDRet ret = createPidInterface(
Ed Tanous9e9b6042024-03-06 14:18:28 -08001567 response, type, name, value, path, managedObj,
1568 createNewObject, output, chassis, currentProfile);
James Feist73df0db2019-03-25 15:29:35 -07001569 if (ret == CreatePIDRet::fail)
1570 {
1571 return;
1572 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001573 if (ret == CreatePIDRet::del)
James Feist73df0db2019-03-25 15:29:35 -07001574 {
1575 continue;
1576 }
1577
1578 if (!createNewObject)
1579 {
1580 for (const auto& property : output)
1581 {
Potin Lai7a696972023-11-09 12:18:20 +08001582 crow::connections::systemBus->async_method_call(
James Feist73df0db2019-03-25 15:29:35 -07001583 [response,
1584 propertyName{std::string(property.first)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001585 const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001586 if (ec)
1587 {
Ed Tanous62598e32023-07-17 17:06:25 -07001588 BMCWEB_LOG_ERROR("Error patching {}: {}",
1589 propertyName, ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001590 messages::internalError(response->res);
1591 return;
1592 }
1593 messages::success(response->res);
Potin Lai7a696972023-11-09 12:18:20 +08001594 },
1595 "xyz.openbmc_project.EntityManager", path,
1596 "org.freedesktop.DBus.Properties", "Set", iface,
1597 property.first, property.second);
James Feist73df0db2019-03-25 15:29:35 -07001598 }
1599 }
1600 else
1601 {
1602 if (chassis.empty())
1603 {
Ed Tanous62598e32023-07-17 17:06:25 -07001604 BMCWEB_LOG_ERROR("Failed to get chassis from config");
Ed Tanousace85d62021-10-26 12:45:59 -07001605 messages::internalError(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001606 return;
1607 }
1608
1609 bool foundChassis = false;
1610 for (const auto& obj : managedObj)
1611 {
Ed Tanous18f8f602023-07-18 10:07:23 -07001612 if (obj.first.parent_path() == chassis)
James Feist73df0db2019-03-25 15:29:35 -07001613 {
1614 chassis = obj.first.str;
1615 foundChassis = true;
1616 break;
1617 }
1618 }
1619 if (!foundChassis)
1620 {
Ed Tanous62598e32023-07-17 17:06:25 -07001621 BMCWEB_LOG_ERROR("Failed to find chassis on dbus");
James Feist73df0db2019-03-25 15:29:35 -07001622 messages::resourceMissingAtURI(
Ed Tanousace85d62021-10-26 12:45:59 -07001623 response->res,
Ed Tanousef4c65b2023-04-24 15:28:50 -07001624 boost::urls::format("/redfish/v1/Chassis/{}",
1625 chassis));
James Feist73df0db2019-03-25 15:29:35 -07001626 return;
1627 }
1628
1629 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001630 [response](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001631 if (ec)
1632 {
Ed Tanous62598e32023-07-17 17:06:25 -07001633 BMCWEB_LOG_ERROR("Error Adding Pid Object {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001634 messages::internalError(response->res);
1635 return;
1636 }
1637 messages::success(response->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05001638 },
James Feist73df0db2019-03-25 15:29:35 -07001639 "xyz.openbmc_project.EntityManager", chassis,
1640 "xyz.openbmc_project.AddObject", "AddObject", output);
1641 }
1642 }
1643 }
1644 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001645
1646 ~SetPIDValues()
1647 {
1648 try
1649 {
1650 pidSetDone();
1651 }
1652 catch (...)
1653 {
Ed Tanous62598e32023-07-17 17:06:25 -07001654 BMCWEB_LOG_CRITICAL("pidSetDone threw exception");
Ed Tanous24b2fe82022-01-06 12:45:54 -08001655 }
1656 }
1657
zhanghch058d1b46d2021-04-01 11:18:24 +08001658 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Ed Tanous9e9b6042024-03-06 14:18:28 -08001659 std::vector<std::pair<std::string, std::optional<nlohmann::json::object_t>>>
James Feist73df0db2019-03-25 15:29:35 -07001660 configuration;
1661 std::optional<std::string> profile;
1662 dbus::utility::ManagedObjectType managedObj;
1663 std::vector<std::string> supportedProfiles;
1664 std::string currentProfile;
1665 std::string profileConnection;
1666 std::string profilePath;
James Feist14b0b8d2020-02-12 11:52:07 -08001667 size_t objectCount = 0;
James Feist73df0db2019-03-25 15:29:35 -07001668};
James Feist83ff9ab2018-08-31 10:18:24 -07001669
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001670/**
1671 * @brief Retrieves BMC manager location data over DBus
1672 *
Ed Tanousac106bf2023-06-07 09:24:59 -07001673 * @param[in] asyncResp Shared pointer for completing asynchronous calls
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001674 * @param[in] connectionName - service name
1675 * @param[in] path - object path
1676 * @return none
1677 */
Ed Tanousac106bf2023-06-07 09:24:59 -07001678inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001679 const std::string& connectionName,
1680 const std::string& path)
1681{
Ed Tanous62598e32023-07-17 17:06:25 -07001682 BMCWEB_LOG_DEBUG("Get BMC manager Location data.");
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001683
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001684 sdbusplus::asio::getProperty<std::string>(
1685 *crow::connections::systemBus, connectionName, path,
1686 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Ed Tanousac106bf2023-06-07 09:24:59 -07001687 [asyncResp](const boost::system::error_code& ec,
1688 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001689 if (ec)
1690 {
Ed Tanous62598e32023-07-17 17:06:25 -07001691 BMCWEB_LOG_DEBUG("DBUS response error for "
1692 "Location");
Ed Tanousac106bf2023-06-07 09:24:59 -07001693 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001694 return;
1695 }
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001696
Ed Tanousac106bf2023-06-07 09:24:59 -07001697 asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001698 property;
Patrick Williams5a39f772023-10-20 11:20:21 -05001699 });
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001700}
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001701// avoid name collision systems.hpp
1702inline void
Ed Tanousac106bf2023-06-07 09:24:59 -07001703 managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001704{
Ed Tanous62598e32023-07-17 17:06:25 -07001705 BMCWEB_LOG_DEBUG("Getting Manager Last Reset Time");
Ed Tanous52cc1122020-07-18 13:51:21 -07001706
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001707 sdbusplus::asio::getProperty<uint64_t>(
1708 *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
1709 "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
1710 "LastRebootTime",
Ed Tanousac106bf2023-06-07 09:24:59 -07001711 [asyncResp](const boost::system::error_code& ec,
1712 const uint64_t lastResetTime) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001713 if (ec)
1714 {
Ed Tanous62598e32023-07-17 17:06:25 -07001715 BMCWEB_LOG_DEBUG("D-BUS response error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001716 return;
1717 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001718
Ed Tanous002d39b2022-05-31 08:59:27 -07001719 // LastRebootTime is epoch time, in milliseconds
1720 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
1721 uint64_t lastResetTimeStamp = lastResetTime / 1000;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001722
Ed Tanous002d39b2022-05-31 08:59:27 -07001723 // Convert to ISO 8601 standard
Ed Tanousac106bf2023-06-07 09:24:59 -07001724 asyncResp->res.jsonValue["LastResetTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001725 redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
Patrick Williams5a39f772023-10-20 11:20:21 -05001726 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001727}
1728
1729/**
1730 * @brief Set the running firmware image
1731 *
Ed Tanousac106bf2023-06-07 09:24:59 -07001732 * @param[i,o] asyncResp - Async response object
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001733 * @param[i] runningFirmwareTarget - Image to make the running image
1734 *
1735 * @return void
1736 */
1737inline void
Ed Tanousac106bf2023-06-07 09:24:59 -07001738 setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001739 const std::string& runningFirmwareTarget)
1740{
1741 // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1742 std::string::size_type idPos = runningFirmwareTarget.rfind('/');
1743 if (idPos == std::string::npos)
1744 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001745 messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001746 "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001747 BMCWEB_LOG_DEBUG("Can't parse firmware ID!");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001748 return;
1749 }
1750 idPos++;
1751 if (idPos >= runningFirmwareTarget.size())
1752 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001753 messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001754 "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001755 BMCWEB_LOG_DEBUG("Invalid firmware ID.");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001756 return;
1757 }
1758 std::string firmwareId = runningFirmwareTarget.substr(idPos);
1759
1760 // Make sure the image is valid before setting priority
George Liu5eb468d2023-06-20 17:03:24 +08001761 sdbusplus::message::object_path objPath("/xyz/openbmc_project/software");
1762 dbus::utility::getManagedObjects(
1763 "xyz.openbmc_project.Software.BMC.Updater", objPath,
1764 [asyncResp, firmwareId, runningFirmwareTarget](
1765 const boost::system::error_code& ec,
1766 const dbus::utility::ManagedObjectType& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001767 if (ec)
1768 {
Ed Tanous62598e32023-07-17 17:06:25 -07001769 BMCWEB_LOG_DEBUG("D-Bus response error getting objects.");
Ed Tanousac106bf2023-06-07 09:24:59 -07001770 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001771 return;
1772 }
1773
1774 if (subtree.empty())
1775 {
Ed Tanous62598e32023-07-17 17:06:25 -07001776 BMCWEB_LOG_DEBUG("Can't find image!");
Ed Tanousac106bf2023-06-07 09:24:59 -07001777 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001778 return;
1779 }
1780
1781 bool foundImage = false;
Ed Tanous02cad962022-06-30 16:50:15 -07001782 for (const auto& object : subtree)
Ed Tanous002d39b2022-05-31 08:59:27 -07001783 {
1784 const std::string& path =
1785 static_cast<const std::string&>(object.first);
1786 std::size_t idPos2 = path.rfind('/');
1787
1788 if (idPos2 == std::string::npos)
1789 {
1790 continue;
1791 }
1792
1793 idPos2++;
1794 if (idPos2 >= path.size())
1795 {
1796 continue;
1797 }
1798
1799 if (path.substr(idPos2) == firmwareId)
1800 {
1801 foundImage = true;
1802 break;
1803 }
1804 }
1805
1806 if (!foundImage)
1807 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001808 messages::propertyValueNotInList(
1809 asyncResp->res, runningFirmwareTarget, "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001810 BMCWEB_LOG_DEBUG("Invalid firmware ID.");
Ed Tanous002d39b2022-05-31 08:59:27 -07001811 return;
1812 }
1813
Ed Tanous62598e32023-07-17 17:06:25 -07001814 BMCWEB_LOG_DEBUG("Setting firmware version {} to priority 0.",
1815 firmwareId);
Ed Tanous002d39b2022-05-31 08:59:27 -07001816
1817 // Only support Immediate
1818 // An addition could be a Redfish Setting like
1819 // ActiveSoftwareImageApplyTime and support OnReset
George Liu9ae226f2023-06-21 17:56:46 +08001820 sdbusplus::asio::setProperty(
1821 *crow::connections::systemBus,
1822 "xyz.openbmc_project.Software.BMC.Updater",
1823 "/xyz/openbmc_project/software/" + firmwareId,
1824 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1825 static_cast<uint8_t>(0),
Ed Tanousac106bf2023-06-07 09:24:59 -07001826 [asyncResp](const boost::system::error_code& ec2) {
Ed Tanous8a592812022-06-04 09:06:59 -07001827 if (ec2)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001828 {
Ed Tanous62598e32023-07-17 17:06:25 -07001829 BMCWEB_LOG_DEBUG("D-Bus response error setting.");
Ed Tanousac106bf2023-06-07 09:24:59 -07001830 messages::internalError(asyncResp->res);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001831 return;
1832 }
Ed Tanousac106bf2023-06-07 09:24:59 -07001833 doBMCGracefulRestart(asyncResp);
George Liu5eb468d2023-06-20 17:03:24 +08001834 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001835 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001836}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001837
Ed Tanousc51afd52024-03-07 10:13:14 -08001838inline void
1839 afterSetDateTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1840 const boost::system::error_code& ec,
1841 const sdbusplus::message_t& msg)
1842{
1843 if (ec)
1844 {
1845 BMCWEB_LOG_DEBUG("Failed to set elapsed time. DBUS response error {}",
1846 ec);
1847 const sd_bus_error* dbusError = msg.get_error();
1848 if (dbusError != nullptr)
1849 {
1850 std::string_view errorName(dbusError->name);
1851 if (errorName ==
1852 "org.freedesktop.timedate1.AutomaticTimeSyncEnabled")
1853 {
1854 BMCWEB_LOG_DEBUG("Setting conflict");
1855 messages::propertyValueConflict(
1856 asyncResp->res, "DateTime",
1857 "Managers/NetworkProtocol/NTPProcotolEnabled");
1858 return;
1859 }
1860 }
1861 messages::internalError(asyncResp->res);
1862 return;
1863 }
1864 asyncResp->res.result(boost::beast::http::status::no_content);
1865}
1866
1867inline void setDateTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1868 const std::string& datetime)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001869{
Ed Tanous62598e32023-07-17 17:06:25 -07001870 BMCWEB_LOG_DEBUG("Set date time: {}", datetime);
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001871
Ed Tanousc2e32002023-01-07 22:05:08 -08001872 std::optional<redfish::time_utils::usSinceEpoch> us =
1873 redfish::time_utils::dateStringToEpoch(datetime);
1874 if (!us)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001875 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001876 messages::propertyValueFormatError(asyncResp->res, datetime,
1877 "DateTime");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001878 return;
1879 }
Ed Tanousc51afd52024-03-07 10:13:14 -08001880 // Set the absolute datetime
1881 bool relative = false;
1882 bool interactive = false;
1883 crow::connections::systemBus->async_method_call(
1884 [asyncResp](const boost::system::error_code& ec,
1885 const sdbusplus::message_t& msg) {
1886 afterSetDateTime(asyncResp, ec, msg);
1887 },
1888 "org.freedesktop.timedate1", "/org/freedesktop/timedate1",
1889 "org.freedesktop.timedate1", "SetTime", us->count(), relative,
1890 interactive);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001891}
1892
Ed Tanous75815e52022-10-05 17:21:13 -07001893inline void
1894 checkForQuiesced(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1895{
1896 sdbusplus::asio::getProperty<std::string>(
1897 *crow::connections::systemBus, "org.freedesktop.systemd1",
1898 "/org/freedesktop/systemd1/unit/obmc-bmc-service-quiesce@0.target",
1899 "org.freedesktop.systemd1.Unit", "ActiveState",
1900 [asyncResp](const boost::system::error_code& ec,
1901 const std::string& val) {
1902 if (!ec)
1903 {
1904 if (val == "active")
1905 {
1906 asyncResp->res.jsonValue["Status"]["Health"] = "Critical";
1907 asyncResp->res.jsonValue["Status"]["State"] = "Quiesced";
1908 return;
1909 }
1910 }
1911 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
1912 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
Patrick Williams5a39f772023-10-20 11:20:21 -05001913 });
Ed Tanous75815e52022-10-05 17:21:13 -07001914}
1915
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001916inline void requestRoutesManager(App& app)
1917{
1918 std::string uuid = persistent_data::getConfig().systemUuid;
1919
1920 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07001921 .privileges(redfish::privileges::getManager)
Ed Tanous002d39b2022-05-31 08:59:27 -07001922 .methods(boost::beast::http::verb::get)(
1923 [&app, uuid](const crow::Request& req,
Ed Tanous45ca1b82022-03-25 13:07:27 -07001924 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001925 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001926 {
1927 return;
1928 }
1929 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
Sui Chena51fc2d2022-07-14 17:21:53 -07001930 asyncResp->res.jsonValue["@odata.type"] = "#Manager.v1_14_0.Manager";
Ed Tanous002d39b2022-05-31 08:59:27 -07001931 asyncResp->res.jsonValue["Id"] = "bmc";
1932 asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
1933 asyncResp->res.jsonValue["Description"] =
1934 "Baseboard Management Controller";
1935 asyncResp->res.jsonValue["PowerState"] = "On";
Ed Tanous14766872022-03-15 10:44:42 -07001936
Ed Tanous002d39b2022-05-31 08:59:27 -07001937 asyncResp->res.jsonValue["ManagerType"] = "BMC";
1938 asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
1939 asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1940 asyncResp->res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001941
Ed Tanous002d39b2022-05-31 08:59:27 -07001942 asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
1943 "/redfish/v1/Managers/bmc/LogServices";
1944 asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
1945 "/redfish/v1/Managers/bmc/NetworkProtocol";
1946 asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
1947 "/redfish/v1/Managers/bmc/EthernetInterfaces";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001948
1949#ifdef BMCWEB_ENABLE_VM_NBDPROXY
Ed Tanous002d39b2022-05-31 08:59:27 -07001950 asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
1951 "/redfish/v1/Managers/bmc/VirtualMedia";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001952#endif // BMCWEB_ENABLE_VM_NBDPROXY
1953
Ed Tanous002d39b2022-05-31 08:59:27 -07001954 // default oem data
1955 nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
1956 nlohmann::json& oemOpenbmc = oem["OpenBmc"];
1957 oem["@odata.type"] = "#OemManager.Oem";
1958 oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
1959 oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
1960 oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
Ed Tanous14766872022-03-15 10:44:42 -07001961
Ed Tanous002d39b2022-05-31 08:59:27 -07001962 nlohmann::json::object_t certificates;
1963 certificates["@odata.id"] =
1964 "/redfish/v1/Managers/bmc/Truststore/Certificates";
1965 oemOpenbmc["Certificates"] = std::move(certificates);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001966
Ed Tanous002d39b2022-05-31 08:59:27 -07001967 // Manager.Reset (an action) can be many values, OpenBMC only
1968 // supports BMC reboot.
1969 nlohmann::json& managerReset =
1970 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
1971 managerReset["target"] =
1972 "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
1973 managerReset["@Redfish.ActionInfo"] =
1974 "/redfish/v1/Managers/bmc/ResetActionInfo";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001975
Ed Tanous002d39b2022-05-31 08:59:27 -07001976 // ResetToDefaults (Factory Reset) has values like
1977 // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
1978 // on OpenBMC
1979 nlohmann::json& resetToDefaults =
1980 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
1981 resetToDefaults["target"] =
1982 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
Ed Tanous613dabe2022-07-09 11:17:36 -07001983 resetToDefaults["ResetType@Redfish.AllowableValues"] =
1984 nlohmann::json::array_t({"ResetAll"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001985
Ed Tanous002d39b2022-05-31 08:59:27 -07001986 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001987 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301988
Ed Tanous002d39b2022-05-31 08:59:27 -07001989 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1990 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1991 redfishDateTimeOffset.second;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001992
Ed Tanous002d39b2022-05-31 08:59:27 -07001993 // TODO (Gunnar): Remove these one day since moved to ComputerSystem
1994 // Still used by OCP profiles
1995 // https://github.com/opencomputeproject/OCP-Profiles/issues/23
1996 // Fill in SerialConsole info
1997 asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
1998 asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
Ed Tanous613dabe2022-07-09 11:17:36 -07001999 asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
2000 nlohmann::json::array_t({"IPMI", "SSH"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002001#ifdef BMCWEB_ENABLE_KVM
Ed Tanous002d39b2022-05-31 08:59:27 -07002002 // Fill in GraphicalConsole info
2003 asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
2004 asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] =
2005 4;
Ed Tanous613dabe2022-07-09 11:17:36 -07002006 asyncResp->res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
2007 nlohmann::json::array_t({"KVMIP"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002008#endif // BMCWEB_ENABLE_KVM
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002009 if constexpr (!bmcwebEnableMultiHost)
2010 {
2011 asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] =
2012 1;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002013
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002014 nlohmann::json::array_t managerForServers;
2015 nlohmann::json::object_t manager;
2016 manager["@odata.id"] = "/redfish/v1/Systems/system";
2017 managerForServers.emplace_back(std::move(manager));
Ed Tanous14766872022-03-15 10:44:42 -07002018
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002019 asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
2020 std::move(managerForServers);
2021 }
Willy Tu13451e32023-05-24 16:08:18 -07002022 if constexpr (bmcwebEnableHealthPopulate)
2023 {
2024 auto health = std::make_shared<HealthPopulate>(asyncResp);
2025 health->isManagersHealth = true;
2026 health->populate();
2027 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002028
Willy Tueee00132022-06-14 14:53:17 -07002029 sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
Ed Tanous002d39b2022-05-31 08:59:27 -07002030 "FirmwareVersion", true);
2031
2032 managerGetLastResetTime(asyncResp);
2033
Sui Chena51fc2d2022-07-14 17:21:53 -07002034 // ManagerDiagnosticData is added for all BMCs.
2035 nlohmann::json& managerDiagnosticData =
2036 asyncResp->res.jsonValue["ManagerDiagnosticData"];
2037 managerDiagnosticData["@odata.id"] =
2038 "/redfish/v1/Managers/bmc/ManagerDiagnosticData";
2039
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002040#ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
Ed Tanous002d39b2022-05-31 08:59:27 -07002041 auto pids = std::make_shared<GetPIDValues>(asyncResp);
2042 pids->run();
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002043#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07002044
2045 getMainChassisId(asyncResp,
2046 [](const std::string& chassisId,
2047 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2048 aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
2049 nlohmann::json::array_t managerForChassis;
Ed Tanous8a592812022-06-04 09:06:59 -07002050 nlohmann::json::object_t managerObj;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002051 boost::urls::url chassiUrl =
2052 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
Willy Tueddfc432022-09-26 16:46:38 +00002053 managerObj["@odata.id"] = chassiUrl;
Patrick Williamsad539542023-05-12 10:10:08 -05002054 managerForChassis.emplace_back(std::move(managerObj));
Ed Tanous002d39b2022-05-31 08:59:27 -07002055 aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
2056 std::move(managerForChassis);
2057 aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00002058 chassiUrl;
Ed Tanous002d39b2022-05-31 08:59:27 -07002059 });
Ed Tanous14766872022-03-15 10:44:42 -07002060
Ed Tanous75815e52022-10-05 17:21:13 -07002061 sdbusplus::asio::getProperty<double>(
2062 *crow::connections::systemBus, "org.freedesktop.systemd1",
2063 "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
2064 "Progress",
2065 [asyncResp](const boost::system::error_code& ec, double val) {
2066 if (ec)
2067 {
Ed Tanous62598e32023-07-17 17:06:25 -07002068 BMCWEB_LOG_ERROR("Error while getting progress");
Ed Tanous75815e52022-10-05 17:21:13 -07002069 messages::internalError(asyncResp->res);
2070 return;
2071 }
2072 if (val < 1.0)
2073 {
2074 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
2075 asyncResp->res.jsonValue["Status"]["State"] = "Starting";
2076 return;
2077 }
2078 checkForQuiesced(asyncResp);
Patrick Williams5a39f772023-10-20 11:20:21 -05002079 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002080
George Liue99073f2022-12-09 11:06:16 +08002081 constexpr std::array<std::string_view, 1> interfaces = {
2082 "xyz.openbmc_project.Inventory.Item.Bmc"};
2083 dbus::utility::getSubTree(
2084 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07002085 [asyncResp](
George Liue99073f2022-12-09 11:06:16 +08002086 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07002087 const dbus::utility::MapperGetSubTreeResponse& subtree) {
2088 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002089 {
Ed Tanous62598e32023-07-17 17:06:25 -07002090 BMCWEB_LOG_DEBUG("D-Bus response error on GetSubTree {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002091 return;
2092 }
2093 if (subtree.empty())
2094 {
Ed Tanous62598e32023-07-17 17:06:25 -07002095 BMCWEB_LOG_DEBUG("Can't find bmc D-Bus object!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002096 return;
2097 }
2098 // Assume only 1 bmc D-Bus object
2099 // Throw an error if there is more than 1
2100 if (subtree.size() > 1)
2101 {
Ed Tanous62598e32023-07-17 17:06:25 -07002102 BMCWEB_LOG_DEBUG("Found more than 1 bmc D-Bus object!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002103 messages::internalError(asyncResp->res);
2104 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002105 }
2106
Ed Tanous002d39b2022-05-31 08:59:27 -07002107 if (subtree[0].first.empty() || subtree[0].second.size() != 1)
2108 {
Ed Tanous62598e32023-07-17 17:06:25 -07002109 BMCWEB_LOG_DEBUG("Error getting bmc D-Bus object!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002110 messages::internalError(asyncResp->res);
2111 return;
2112 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002113
Ed Tanous002d39b2022-05-31 08:59:27 -07002114 const std::string& path = subtree[0].first;
2115 const std::string& connectionName = subtree[0].second[0].first;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002116
Ed Tanous002d39b2022-05-31 08:59:27 -07002117 for (const auto& interfaceName : subtree[0].second[0].second)
2118 {
2119 if (interfaceName ==
2120 "xyz.openbmc_project.Inventory.Decorator.Asset")
2121 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002122 sdbusplus::asio::getAllProperties(
2123 *crow::connections::systemBus, connectionName, path,
2124 "xyz.openbmc_project.Inventory.Decorator.Asset",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002125 [asyncResp](const boost::system::error_code& ec2,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002126 const dbus::utility::DBusPropertiesMap&
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002127 propertiesList) {
Ed Tanous8a592812022-06-04 09:06:59 -07002128 if (ec2)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002129 {
Ed Tanous62598e32023-07-17 17:06:25 -07002130 BMCWEB_LOG_DEBUG("Can't get bmc asset!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002131 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002132 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002133
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002134 const std::string* partNumber = nullptr;
2135 const std::string* serialNumber = nullptr;
2136 const std::string* manufacturer = nullptr;
2137 const std::string* model = nullptr;
2138 const std::string* sparePartNumber = nullptr;
2139
2140 const bool success = sdbusplus::unpackPropertiesNoThrow(
2141 dbus_utils::UnpackErrorPrinter(), propertiesList,
2142 "PartNumber", partNumber, "SerialNumber",
2143 serialNumber, "Manufacturer", manufacturer, "Model",
2144 model, "SparePartNumber", sparePartNumber);
2145
2146 if (!success)
2147 {
2148 messages::internalError(asyncResp->res);
2149 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07002150 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002151
2152 if (partNumber != nullptr)
2153 {
2154 asyncResp->res.jsonValue["PartNumber"] =
2155 *partNumber;
2156 }
2157
2158 if (serialNumber != nullptr)
2159 {
2160 asyncResp->res.jsonValue["SerialNumber"] =
2161 *serialNumber;
2162 }
2163
2164 if (manufacturer != nullptr)
2165 {
2166 asyncResp->res.jsonValue["Manufacturer"] =
2167 *manufacturer;
2168 }
2169
2170 if (model != nullptr)
2171 {
2172 asyncResp->res.jsonValue["Model"] = *model;
2173 }
2174
2175 if (sparePartNumber != nullptr)
2176 {
2177 asyncResp->res.jsonValue["SparePartNumber"] =
2178 *sparePartNumber;
2179 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002180 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002181 }
2182 else if (interfaceName ==
2183 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
2184 {
2185 getLocation(asyncResp, connectionName, path);
2186 }
2187 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002188 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002189 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002190
2191 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07002192 .privileges(redfish::privileges::patchManager)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002193 .methods(boost::beast::http::verb::patch)(
2194 [&app](const crow::Request& req,
2195 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002196 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002197 {
2198 return;
2199 }
Ed Tanous9e9b6042024-03-06 14:18:28 -08002200 std::optional<std::string> activeSoftwareImageOdataId;
Ed Tanous002d39b2022-05-31 08:59:27 -07002201 std::optional<std::string> datetime;
Ed Tanous9e9b6042024-03-06 14:18:28 -08002202 std::optional<nlohmann::json::object_t> pidControllers;
2203 std::optional<nlohmann::json::object_t> fanControllers;
2204 std::optional<nlohmann::json::object_t> fanZones;
2205 std::optional<nlohmann::json::object_t> stepwiseControllers;
2206 std::optional<std::string> profile;
Ed Tanous002d39b2022-05-31 08:59:27 -07002207
Ed Tanous9e9b6042024-03-06 14:18:28 -08002208 // clang-format off
2209 if (!json_util::readJsonPatch(req, asyncResp->res,
2210 "DateTime", datetime,
2211 "Links/ActiveSoftwareImage/@odata.id", activeSoftwareImageOdataId,
2212 "Oem/OpenBmc/Fan/FanControllers", fanControllers,
2213 "Oem/OpenBmc/Fan/FanZones", fanZones,
2214 "Oem/OpenBmc/Fan/PidControllers", pidControllers,
2215 "Oem/OpenBmc/Fan/Profile", profile,
2216 "Oem/OpenBmc/Fan/StepwiseControllers", stepwiseControllers
2217 ))
Ed Tanous002d39b2022-05-31 08:59:27 -07002218 {
2219 return;
2220 }
Ed Tanous9e9b6042024-03-06 14:18:28 -08002221 // clang-format on
Ed Tanous002d39b2022-05-31 08:59:27 -07002222
Ed Tanous9e9b6042024-03-06 14:18:28 -08002223 if (pidControllers || fanControllers || fanZones ||
2224 stepwiseControllers || profile)
Ed Tanous002d39b2022-05-31 08:59:27 -07002225 {
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002226#ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
Ed Tanous9e9b6042024-03-06 14:18:28 -08002227 std::vector<
2228 std::pair<std::string, std::optional<nlohmann::json::object_t>>>
2229 configuration;
2230 if (pidControllers)
Ed Tanous002d39b2022-05-31 08:59:27 -07002231 {
Ed Tanous9e9b6042024-03-06 14:18:28 -08002232 configuration.emplace_back("PidControllers",
2233 std::move(pidControllers));
Ed Tanous002d39b2022-05-31 08:59:27 -07002234 }
Ed Tanous9e9b6042024-03-06 14:18:28 -08002235 if (fanControllers)
Ed Tanous002d39b2022-05-31 08:59:27 -07002236 {
Ed Tanous9e9b6042024-03-06 14:18:28 -08002237 configuration.emplace_back("FanControllers",
2238 std::move(fanControllers));
Ed Tanous002d39b2022-05-31 08:59:27 -07002239 }
Ed Tanous9e9b6042024-03-06 14:18:28 -08002240 if (fanZones)
2241 {
2242 configuration.emplace_back("FanZones", std::move(fanZones));
2243 }
2244 if (stepwiseControllers)
2245 {
2246 configuration.emplace_back("StepwiseControllers",
2247 std::move(stepwiseControllers));
2248 }
2249 auto pid = std::make_shared<SetPIDValues>(
2250 asyncResp, std::move(configuration), profile);
2251 pid->run();
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002252#else
2253 messages::propertyUnknown(asyncResp->res, "Oem");
2254 return;
2255#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07002256 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07002257
Ed Tanous9e9b6042024-03-06 14:18:28 -08002258 if (activeSoftwareImageOdataId)
2259 {
2260 setActiveFirmwareImage(asyncResp, *activeSoftwareImageOdataId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002261 }
Ed Tanous9e9b6042024-03-06 14:18:28 -08002262
Ed Tanous002d39b2022-05-31 08:59:27 -07002263 if (datetime)
2264 {
Ed Tanousc51afd52024-03-07 10:13:14 -08002265 setDateTime(asyncResp, *datetime);
Ed Tanous002d39b2022-05-31 08:59:27 -07002266 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002267 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002268}
2269
2270inline void requestRoutesManagerCollection(App& app)
2271{
2272 BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
Ed Tanoused398212021-06-09 17:05:54 -07002273 .privileges(redfish::privileges::getManagerCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002274 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002275 [&app](const crow::Request& req,
2276 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002277 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002278 {
2279 return;
2280 }
2281 // Collections don't include the static data added by SubRoute
2282 // because it has a duplicate entry for members
2283 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
2284 asyncResp->res.jsonValue["@odata.type"] =
2285 "#ManagerCollection.ManagerCollection";
2286 asyncResp->res.jsonValue["Name"] = "Manager Collection";
2287 asyncResp->res.jsonValue["Members@odata.count"] = 1;
2288 nlohmann::json::array_t members;
2289 nlohmann::json& bmc = members.emplace_back();
2290 bmc["@odata.id"] = "/redfish/v1/Managers/bmc";
2291 asyncResp->res.jsonValue["Members"] = std::move(members);
Patrick Williams5a39f772023-10-20 11:20:21 -05002292 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002293}
Ed Tanous1abe55e2018-09-05 08:30:59 -07002294} // namespace redfish