blob: 26abec0f8e1d123d4093658067b98746aab14e36 [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,
712 std::vector<nlohmann::json>& config,
713 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;
724 if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
725 path))
726 {
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,
749 const std::string& 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 Tanousa170f272022-06-30 21:53:27 -0700753 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 Tanousb5a76932020-09-29 16:16:58 -0700781 const nlohmann::json::iterator& it, 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
James Feistb6baeaa2019-02-21 10:41:40 -0800787 if (it.value() == 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
James Feist73df0db2019-03-25 15:29:35 -0700830 managedItem = findChassis(managedObj, it.key(), chassis);
831 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
912 std::optional<std::vector<nlohmann::json>> zones;
913 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(
James Feistb6baeaa2019-02-21 10:41:40 -0800918 it.value(), 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 Tanousace85d62021-10-26 12:45:59 -0700993 messages::propertyValueNotInList(response->res, it.key(),
994 "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
James Feist5f2caae2018-12-12 14:08:25 -08001015 std::optional<nlohmann::json> chassisContainer;
1016 std::optional<double> failSafePercent;
James Feistd3ec07f2019-02-25 14:51:15 -08001017 std::optional<double> minThermalOutput;
James Feistb6baeaa2019-02-21 10:41:40 -08001018 if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
James Feist5f2caae2018-12-12 14:08:25 -08001019 chassisContainer, "FailSafePercent",
James Feistd3ec07f2019-02-25 14:51:15 -08001020 failSafePercent, "MinThermalOutput",
1021 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
James Feist5f2caae2018-12-12 14:08:25 -08001026 if (chassisContainer)
1027 {
James Feist5f2caae2018-12-12 14:08:25 -08001028 std::string chassisId;
1029 if (!redfish::json_util::readJson(*chassisContainer, response->res,
1030 "@odata.id", chassisId))
James Feist83ff9ab2018-08-31 10:18:24 -07001031 {
James Feist83ff9ab2018-08-31 10:18:24 -07001032 return CreatePIDRet::fail;
1033 }
James Feist5f2caae2018-12-12 14:08:25 -08001034
AppaRao Puli717794d2019-10-18 22:54:53 +05301035 // /redfish/v1/chassis/chassis_name/
James Feist5f2caae2018-12-12 14:08:25 -08001036 if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
1037 {
Ed Tanous62598e32023-07-17 17:06:25 -07001038 BMCWEB_LOG_ERROR("Got invalid path {}", chassisId);
Ed Tanousace85d62021-10-26 12:45:59 -07001039 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001040 response->res,
1041 boost::urls::format("/redfish/v1/Chassis/{}", chassisId));
James Feist5f2caae2018-12-12 14:08:25 -08001042 return CreatePIDRet::fail;
1043 }
1044 }
James Feistd3ec07f2019-02-25 14:51:15 -08001045 if (minThermalOutput)
James Feist5f2caae2018-12-12 14:08:25 -08001046 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001047 output.emplace_back("MinThermalOutput", *minThermalOutput);
James Feist5f2caae2018-12-12 14:08:25 -08001048 }
1049 if (failSafePercent)
1050 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001051 output.emplace_back("FailSafePercent", *failSafePercent);
James Feist5f2caae2018-12-12 14:08:25 -08001052 }
1053 }
1054 else if (type == "StepwiseControllers")
1055 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001056 output.emplace_back("Type", "Stepwise");
James Feist5f2caae2018-12-12 14:08:25 -08001057
1058 std::optional<std::vector<nlohmann::json>> zones;
1059 std::optional<std::vector<nlohmann::json>> steps;
1060 std::optional<std::vector<std::string>> inputs;
1061 std::optional<double> positiveHysteresis;
1062 std::optional<double> negativeHysteresis;
James Feistc33a90e2019-03-01 10:17:44 -08001063 std::optional<std::string> direction; // upper clipping curve vs lower
James Feist5f2caae2018-12-12 14:08:25 -08001064 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -08001065 it.value(), response->res, "Zones", zones, "Steps", steps,
1066 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
James Feistc33a90e2019-03-01 10:17:44 -08001067 "NegativeHysteresis", negativeHysteresis, "Direction",
1068 direction))
James Feist5f2caae2018-12-12 14:08:25 -08001069 {
James Feist5f2caae2018-12-12 14:08:25 -08001070 return CreatePIDRet::fail;
1071 }
1072
1073 if (zones)
1074 {
James Feistb6baeaa2019-02-21 10:41:40 -08001075 std::vector<std::string> zonesStrs;
1076 if (!getZonesFromJsonReq(response, *zones, zonesStrs))
James Feist5f2caae2018-12-12 14:08:25 -08001077 {
Ed Tanous62598e32023-07-17 17:06:25 -07001078 BMCWEB_LOG_ERROR("Illegal Zones");
James Feist5f2caae2018-12-12 14:08:25 -08001079 return CreatePIDRet::fail;
1080 }
James Feistb6baeaa2019-02-21 10:41:40 -08001081 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -08001082 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -08001083 {
Ed Tanous62598e32023-07-17 17:06:25 -07001084 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousace85d62021-10-26 12:45:59 -07001085 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001086 response->res,
1087 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -08001088 return CreatePIDRet::fail;
1089 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001090 output.emplace_back("Zones", std::move(zonesStrs));
James Feist5f2caae2018-12-12 14:08:25 -08001091 }
1092 if (steps)
1093 {
1094 std::vector<double> readings;
1095 std::vector<double> outputs;
1096 for (auto& step : *steps)
1097 {
Ed Tanous543f4402022-01-06 13:12:53 -08001098 double target = 0.0;
1099 double out = 0.0;
James Feist5f2caae2018-12-12 14:08:25 -08001100
1101 if (!redfish::json_util::readJson(step, response->res, "Target",
Ed Tanous23a21a12020-07-25 04:45:05 +00001102 target, "Output", out))
James Feist5f2caae2018-12-12 14:08:25 -08001103 {
James Feist5f2caae2018-12-12 14:08:25 -08001104 return CreatePIDRet::fail;
1105 }
1106 readings.emplace_back(target);
Ed Tanous23a21a12020-07-25 04:45:05 +00001107 outputs.emplace_back(out);
James Feist5f2caae2018-12-12 14:08:25 -08001108 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001109 output.emplace_back("Reading", std::move(readings));
1110 output.emplace_back("Output", std::move(outputs));
James Feist5f2caae2018-12-12 14:08:25 -08001111 }
1112 if (inputs)
1113 {
1114 for (std::string& value : *inputs)
1115 {
Ed Tanousa170f272022-06-30 21:53:27 -07001116 std::replace(value.begin(), value.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -08001117 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001118 output.emplace_back("Inputs", std::move(*inputs));
James Feist5f2caae2018-12-12 14:08:25 -08001119 }
1120 if (negativeHysteresis)
1121 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001122 output.emplace_back("NegativeHysteresis", *negativeHysteresis);
James Feist5f2caae2018-12-12 14:08:25 -08001123 }
1124 if (positiveHysteresis)
1125 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001126 output.emplace_back("PositiveHysteresis", *positiveHysteresis);
James Feist83ff9ab2018-08-31 10:18:24 -07001127 }
James Feistc33a90e2019-03-01 10:17:44 -08001128 if (direction)
1129 {
1130 constexpr const std::array<const char*, 2> allowedDirections = {
1131 "Ceiling", "Floor"};
Ed Tanous3544d2a2023-08-06 18:12:20 -07001132 if (std::ranges::find(allowedDirections, *direction) ==
1133 allowedDirections.end())
James Feistc33a90e2019-03-01 10:17:44 -08001134 {
1135 messages::propertyValueTypeError(response->res, "Direction",
1136 *direction);
1137 return CreatePIDRet::fail;
1138 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001139 output.emplace_back("Class", *direction);
James Feistc33a90e2019-03-01 10:17:44 -08001140 }
James Feist83ff9ab2018-08-31 10:18:24 -07001141 }
1142 else
1143 {
Ed Tanous62598e32023-07-17 17:06:25 -07001144 BMCWEB_LOG_ERROR("Illegal Type {}", type);
Jason M. Bills35a62c72018-10-09 12:45:45 -07001145 messages::propertyUnknown(response->res, type);
James Feist83ff9ab2018-08-31 10:18:24 -07001146 return CreatePIDRet::fail;
1147 }
1148 return CreatePIDRet::patch;
1149}
James Feist73df0db2019-03-25 15:29:35 -07001150struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
1151{
Ed Tanous6936afe2022-09-08 15:10:39 -07001152 struct CompletionValues
1153 {
1154 std::vector<std::string> supportedProfiles;
1155 std::string currentProfile;
1156 dbus::utility::MapperGetSubTreeResponse subtree;
1157 };
James Feist73df0db2019-03-25 15:29:35 -07001158
Ed Tanous4e23a442022-06-06 09:57:26 -07001159 explicit GetPIDValues(
1160 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
Ed Tanous23a21a12020-07-25 04:45:05 +00001161 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001162
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001163 {}
James Feist73df0db2019-03-25 15:29:35 -07001164
1165 void run()
1166 {
1167 std::shared_ptr<GetPIDValues> self = shared_from_this();
1168
1169 // get all configurations
George Liue99073f2022-12-09 11:06:16 +08001170 constexpr std::array<std::string_view, 4> interfaces = {
1171 pidConfigurationIface, pidZoneConfigurationIface,
1172 objectManagerIface, stepwiseConfigurationIface};
1173 dbus::utility::getSubTree(
1174 "/", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001175 [self](
George Liue99073f2022-12-09 11:06:16 +08001176 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001177 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001178 if (ec)
1179 {
Ed Tanous62598e32023-07-17 17:06:25 -07001180 BMCWEB_LOG_ERROR("{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001181 messages::internalError(self->asyncResp->res);
1182 return;
1183 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001184 self->complete.subtree = subtreeLocal;
Patrick Williams5a39f772023-10-20 11:20:21 -05001185 });
James Feist73df0db2019-03-25 15:29:35 -07001186
1187 // at the same time get the selected profile
George Liue99073f2022-12-09 11:06:16 +08001188 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1189 thermalModeIface};
1190 dbus::utility::getSubTree(
1191 "/", 0, thermalModeIfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001192 [self](
George Liue99073f2022-12-09 11:06:16 +08001193 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001194 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001195 if (ec || subtreeLocal.empty())
1196 {
1197 return;
1198 }
1199 if (subtreeLocal[0].second.size() != 1)
1200 {
1201 // invalid mapper response, should never happen
Ed Tanous62598e32023-07-17 17:06:25 -07001202 BMCWEB_LOG_ERROR("GetPIDValues: Mapper Error");
Ed Tanous002d39b2022-05-31 08:59:27 -07001203 messages::internalError(self->asyncResp->res);
1204 return;
1205 }
1206
1207 const std::string& path = subtreeLocal[0].first;
1208 const std::string& owner = subtreeLocal[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001209
1210 sdbusplus::asio::getAllProperties(
1211 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous002d39b2022-05-31 08:59:27 -07001212 [path, owner,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001213 self](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001214 const dbus::utility::DBusPropertiesMap& resp) {
1215 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001216 {
Ed Tanous62598e32023-07-17 17:06:25 -07001217 BMCWEB_LOG_ERROR(
1218 "GetPIDValues: Can't get thermalModeIface {}", path);
James Feist73df0db2019-03-25 15:29:35 -07001219 messages::internalError(self->asyncResp->res);
1220 return;
1221 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001222
Ed Tanous002d39b2022-05-31 08:59:27 -07001223 const std::string* current = nullptr;
1224 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001225
1226 const bool success = sdbusplus::unpackPropertiesNoThrow(
1227 dbus_utils::UnpackErrorPrinter(), resp, "Current", current,
1228 "Supported", supported);
1229
1230 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001231 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001232 messages::internalError(self->asyncResp->res);
1233 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001234 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001235
Ed Tanous002d39b2022-05-31 08:59:27 -07001236 if (current == nullptr || supported == nullptr)
1237 {
Ed Tanous62598e32023-07-17 17:06:25 -07001238 BMCWEB_LOG_ERROR(
1239 "GetPIDValues: thermal mode iface invalid {}", path);
Ed Tanous002d39b2022-05-31 08:59:27 -07001240 messages::internalError(self->asyncResp->res);
1241 return;
1242 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001243 self->complete.currentProfile = *current;
1244 self->complete.supportedProfiles = *supported;
George Liue99073f2022-12-09 11:06:16 +08001245 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001246 });
James Feist73df0db2019-03-25 15:29:35 -07001247 }
1248
Ed Tanous6936afe2022-09-08 15:10:39 -07001249 static void
1250 processingComplete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1251 const CompletionValues& completion)
James Feist73df0db2019-03-25 15:29:35 -07001252 {
1253 if (asyncResp->res.result() != boost::beast::http::status::ok)
1254 {
1255 return;
1256 }
1257 // create map of <connection, path to objMgr>>
Ed Tanous6936afe2022-09-08 15:10:39 -07001258 boost::container::flat_map<
1259 std::string, std::string, std::less<>,
1260 std::vector<std::pair<std::string, std::string>>>
1261 objectMgrPaths;
1262 boost::container::flat_set<std::string, std::less<>,
1263 std::vector<std::string>>
1264 calledConnections;
1265 for (const auto& pathGroup : completion.subtree)
James Feist73df0db2019-03-25 15:29:35 -07001266 {
1267 for (const auto& connectionGroup : pathGroup.second)
1268 {
1269 auto findConnection =
1270 calledConnections.find(connectionGroup.first);
1271 if (findConnection != calledConnections.end())
1272 {
1273 break;
1274 }
1275 for (const std::string& interface : connectionGroup.second)
1276 {
1277 if (interface == objectManagerIface)
1278 {
1279 objectMgrPaths[connectionGroup.first] = pathGroup.first;
1280 }
1281 // this list is alphabetical, so we
1282 // should have found the objMgr by now
1283 if (interface == pidConfigurationIface ||
1284 interface == pidZoneConfigurationIface ||
1285 interface == stepwiseConfigurationIface)
1286 {
1287 auto findObjMgr =
1288 objectMgrPaths.find(connectionGroup.first);
1289 if (findObjMgr == objectMgrPaths.end())
1290 {
Ed Tanous62598e32023-07-17 17:06:25 -07001291 BMCWEB_LOG_DEBUG("{}Has no Object Manager",
1292 connectionGroup.first);
James Feist73df0db2019-03-25 15:29:35 -07001293 continue;
1294 }
1295
1296 calledConnections.insert(connectionGroup.first);
1297
1298 asyncPopulatePid(findObjMgr->first, findObjMgr->second,
Ed Tanous6936afe2022-09-08 15:10:39 -07001299 completion.currentProfile,
1300 completion.supportedProfiles,
James Feist73df0db2019-03-25 15:29:35 -07001301 asyncResp);
1302 break;
1303 }
1304 }
1305 }
1306 }
1307 }
1308
Ed Tanous6936afe2022-09-08 15:10:39 -07001309 ~GetPIDValues()
1310 {
1311 boost::asio::post(crow::connections::systemBus->get_io_context(),
1312 std::bind_front(&processingComplete, asyncResp,
1313 std::move(complete)));
1314 }
1315
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001316 GetPIDValues(const GetPIDValues&) = delete;
1317 GetPIDValues(GetPIDValues&&) = delete;
1318 GetPIDValues& operator=(const GetPIDValues&) = delete;
1319 GetPIDValues& operator=(GetPIDValues&&) = delete;
1320
zhanghch058d1b46d2021-04-01 11:18:24 +08001321 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Ed Tanous6936afe2022-09-08 15:10:39 -07001322 CompletionValues complete;
James Feist73df0db2019-03-25 15:29:35 -07001323};
1324
1325struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
1326{
zhanghch058d1b46d2021-04-01 11:18:24 +08001327 SetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
James Feist73df0db2019-03-25 15:29:35 -07001328 nlohmann::json& data) :
Ed Tanous271584a2019-07-09 16:24:22 -07001329 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001330 {
James Feist73df0db2019-03-25 15:29:35 -07001331 std::optional<nlohmann::json> pidControllers;
1332 std::optional<nlohmann::json> fanControllers;
1333 std::optional<nlohmann::json> fanZones;
1334 std::optional<nlohmann::json> stepwiseControllers;
1335
1336 if (!redfish::json_util::readJson(
1337 data, asyncResp->res, "PidControllers", pidControllers,
1338 "FanControllers", fanControllers, "FanZones", fanZones,
1339 "StepwiseControllers", stepwiseControllers, "Profile", profile))
1340 {
James Feist73df0db2019-03-25 15:29:35 -07001341 return;
1342 }
1343 configuration.emplace_back("PidControllers", std::move(pidControllers));
1344 configuration.emplace_back("FanControllers", std::move(fanControllers));
1345 configuration.emplace_back("FanZones", std::move(fanZones));
1346 configuration.emplace_back("StepwiseControllers",
1347 std::move(stepwiseControllers));
1348 }
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001349
1350 SetPIDValues(const SetPIDValues&) = delete;
1351 SetPIDValues(SetPIDValues&&) = delete;
1352 SetPIDValues& operator=(const SetPIDValues&) = delete;
1353 SetPIDValues& operator=(SetPIDValues&&) = delete;
1354
James Feist73df0db2019-03-25 15:29:35 -07001355 void run()
1356 {
1357 if (asyncResp->res.result() != boost::beast::http::status::ok)
1358 {
1359 return;
1360 }
1361
1362 std::shared_ptr<SetPIDValues> self = shared_from_this();
1363
1364 // todo(james): might make sense to do a mapper call here if this
1365 // interface gets more traction
George Liu5eb468d2023-06-20 17:03:24 +08001366 sdbusplus::message::object_path objPath(
1367 "/xyz/openbmc_project/inventory");
1368 dbus::utility::getManagedObjects(
1369 "xyz.openbmc_project.EntityManager", objPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001370 [self](const boost::system::error_code& ec,
Ed Tanous914e2d52022-01-07 11:38:34 -08001371 const dbus::utility::ManagedObjectType& mObj) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001372 if (ec)
1373 {
Ed Tanous62598e32023-07-17 17:06:25 -07001374 BMCWEB_LOG_ERROR("Error communicating to Entity Manager");
Ed Tanous002d39b2022-05-31 08:59:27 -07001375 messages::internalError(self->asyncResp->res);
1376 return;
1377 }
1378 const std::array<const char*, 3> configurations = {
1379 pidConfigurationIface, pidZoneConfigurationIface,
1380 stepwiseConfigurationIface};
James Feiste69d9de2020-02-07 12:23:27 -08001381
Ed Tanous002d39b2022-05-31 08:59:27 -07001382 for (const auto& [path, object] : mObj)
1383 {
1384 for (const auto& [interface, _] : object)
James Feiste69d9de2020-02-07 12:23:27 -08001385 {
Ed Tanous3544d2a2023-08-06 18:12:20 -07001386 if (std::ranges::find(configurations, interface) !=
1387 configurations.end())
James Feiste69d9de2020-02-07 12:23:27 -08001388 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001389 self->objectCount++;
1390 break;
James Feiste69d9de2020-02-07 12:23:27 -08001391 }
James Feiste69d9de2020-02-07 12:23:27 -08001392 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001393 }
1394 self->managedObj = mObj;
Patrick Williams5a39f772023-10-20 11:20:21 -05001395 });
James Feist73df0db2019-03-25 15:29:35 -07001396
1397 // at the same time get the profile information
George Liue99073f2022-12-09 11:06:16 +08001398 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1399 thermalModeIface};
1400 dbus::utility::getSubTree(
1401 "/", 0, thermalModeIfaces,
1402 [self](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001403 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001404 if (ec || subtree.empty())
1405 {
1406 return;
1407 }
1408 if (subtree[0].second.empty())
1409 {
1410 // invalid mapper response, should never happen
Ed Tanous62598e32023-07-17 17:06:25 -07001411 BMCWEB_LOG_ERROR("SetPIDValues: Mapper Error");
Ed Tanous002d39b2022-05-31 08:59:27 -07001412 messages::internalError(self->asyncResp->res);
1413 return;
1414 }
1415
1416 const std::string& path = subtree[0].first;
1417 const std::string& owner = subtree[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001418 sdbusplus::asio::getAllProperties(
1419 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001420 [self, path, owner](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001421 const dbus::utility::DBusPropertiesMap& r) {
1422 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001423 {
Ed Tanous62598e32023-07-17 17:06:25 -07001424 BMCWEB_LOG_ERROR(
1425 "SetPIDValues: Can't get thermalModeIface {}", path);
James Feist73df0db2019-03-25 15:29:35 -07001426 messages::internalError(self->asyncResp->res);
1427 return;
1428 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001429 const std::string* current = nullptr;
1430 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001431
1432 const bool success = sdbusplus::unpackPropertiesNoThrow(
1433 dbus_utils::UnpackErrorPrinter(), r, "Current", current,
1434 "Supported", supported);
1435
1436 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001437 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001438 messages::internalError(self->asyncResp->res);
1439 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001440 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001441
Ed Tanous002d39b2022-05-31 08:59:27 -07001442 if (current == nullptr || supported == nullptr)
1443 {
Ed Tanous62598e32023-07-17 17:06:25 -07001444 BMCWEB_LOG_ERROR(
1445 "SetPIDValues: thermal mode iface invalid {}", path);
Ed Tanous002d39b2022-05-31 08:59:27 -07001446 messages::internalError(self->asyncResp->res);
1447 return;
1448 }
1449 self->currentProfile = *current;
1450 self->supportedProfiles = *supported;
1451 self->profileConnection = owner;
1452 self->profilePath = path;
George Liue99073f2022-12-09 11:06:16 +08001453 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001454 });
James Feist73df0db2019-03-25 15:29:35 -07001455 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001456 void pidSetDone()
James Feist73df0db2019-03-25 15:29:35 -07001457 {
1458 if (asyncResp->res.result() != boost::beast::http::status::ok)
1459 {
1460 return;
1461 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001462 std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001463 if (profile)
1464 {
Ed Tanous3544d2a2023-08-06 18:12:20 -07001465 if (std::ranges::find(supportedProfiles, *profile) ==
1466 supportedProfiles.end())
James Feist73df0db2019-03-25 15:29:35 -07001467 {
1468 messages::actionParameterUnknown(response->res, "Profile",
1469 *profile);
1470 return;
1471 }
1472 currentProfile = *profile;
George Liu9ae226f2023-06-21 17:56:46 +08001473 sdbusplus::asio::setProperty(
1474 *crow::connections::systemBus, profileConnection, profilePath,
1475 thermalModeIface, "Current", *profile,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001476 [response](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001477 if (ec)
1478 {
Ed Tanous62598e32023-07-17 17:06:25 -07001479 BMCWEB_LOG_ERROR("Error patching profile{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001480 messages::internalError(response->res);
1481 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001482 });
James Feist73df0db2019-03-25 15:29:35 -07001483 }
1484
1485 for (auto& containerPair : configuration)
1486 {
1487 auto& container = containerPair.second;
1488 if (!container)
1489 {
1490 continue;
1491 }
Ed Tanous62598e32023-07-17 17:06:25 -07001492 BMCWEB_LOG_DEBUG("{}", *container);
James Feist6ee7f772020-02-06 16:25:27 -08001493
Ed Tanous02cad962022-06-30 16:50:15 -07001494 const std::string& type = containerPair.first;
James Feist73df0db2019-03-25 15:29:35 -07001495
1496 for (nlohmann::json::iterator it = container->begin();
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301497 it != container->end(); ++it)
James Feist73df0db2019-03-25 15:29:35 -07001498 {
1499 const auto& name = it.key();
Potin Laicddbf3d2023-02-14 14:28:58 +08001500 std::string dbusObjName = name;
1501 std::replace(dbusObjName.begin(), dbusObjName.end(), ' ', '_');
Ed Tanous62598e32023-07-17 17:06:25 -07001502 BMCWEB_LOG_DEBUG("looking for {}", name);
James Feist6ee7f772020-02-06 16:25:27 -08001503
Ed Tanous3544d2a2023-08-06 18:12:20 -07001504 auto pathItr = std::ranges::find_if(
1505 managedObj, [&dbusObjName](const auto& obj) {
Ed Tanous18f8f602023-07-18 10:07:23 -07001506 return obj.first.parent_path() == dbusObjName;
Patrick Williams5a39f772023-10-20 11:20:21 -05001507 });
Ed Tanousb9d36b42022-02-26 21:42:46 -08001508 dbus::utility::DBusPropertiesMap output;
James Feist73df0db2019-03-25 15:29:35 -07001509
1510 output.reserve(16); // The pid interface length
1511
1512 // determines if we're patching entity-manager or
1513 // creating a new object
1514 bool createNewObject = (pathItr == managedObj.end());
Ed Tanous62598e32023-07-17 17:06:25 -07001515 BMCWEB_LOG_DEBUG("Found = {}", !createNewObject);
James Feist6ee7f772020-02-06 16:25:27 -08001516
James Feist73df0db2019-03-25 15:29:35 -07001517 std::string iface;
Ed Tanousea2b6702022-03-07 16:48:38 -08001518 if (!createNewObject)
James Feist73df0db2019-03-25 15:29:35 -07001519 {
Potin Lai8be2b5b2022-11-22 13:27:16 +08001520 bool findInterface = false;
Ed Tanousea2b6702022-03-07 16:48:38 -08001521 for (const auto& interface : pathItr->second)
James Feist73df0db2019-03-25 15:29:35 -07001522 {
Ed Tanousea2b6702022-03-07 16:48:38 -08001523 if (interface.first == pidConfigurationIface)
1524 {
1525 if (type == "PidControllers" ||
1526 type == "FanControllers")
1527 {
1528 iface = pidConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001529 findInterface = true;
1530 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001531 }
1532 }
1533 else if (interface.first == pidZoneConfigurationIface)
1534 {
1535 if (type == "FanZones")
1536 {
PavanKumarIntelda393502024-03-15 05:47:02 +00001537 iface = pidZoneConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001538 findInterface = true;
1539 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001540 }
1541 }
1542 else if (interface.first == stepwiseConfigurationIface)
1543 {
1544 if (type == "StepwiseControllers")
1545 {
1546 iface = stepwiseConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001547 findInterface = true;
1548 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001549 }
1550 }
James Feist73df0db2019-03-25 15:29:35 -07001551 }
Potin Lai8be2b5b2022-11-22 13:27:16 +08001552
1553 // create new object if interface not found
1554 if (!findInterface)
1555 {
1556 createNewObject = true;
1557 }
James Feist73df0db2019-03-25 15:29:35 -07001558 }
James Feist6ee7f772020-02-06 16:25:27 -08001559
1560 if (createNewObject && it.value() == nullptr)
1561 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -05001562 // can't delete a non-existent object
Ed Tanouse2616cc2022-06-27 12:45:55 -07001563 messages::propertyValueNotInList(response->res, it.value(),
1564 name);
James Feist6ee7f772020-02-06 16:25:27 -08001565 continue;
1566 }
1567
1568 std::string path;
1569 if (pathItr != managedObj.end())
1570 {
1571 path = pathItr->first.str;
1572 }
1573
Ed Tanous62598e32023-07-17 17:06:25 -07001574 BMCWEB_LOG_DEBUG("Create new = {}", createNewObject);
James Feiste69d9de2020-02-07 12:23:27 -08001575
1576 // arbitrary limit to avoid attacks
1577 constexpr const size_t controllerLimit = 500;
James Feist14b0b8d2020-02-12 11:52:07 -08001578 if (createNewObject && objectCount >= controllerLimit)
James Feiste69d9de2020-02-07 12:23:27 -08001579 {
1580 messages::resourceExhaustion(response->res, type);
1581 continue;
1582 }
Ed Tanousa170f272022-06-30 21:53:27 -07001583 std::string escaped = name;
1584 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1585 output.emplace_back("Name", escaped);
James Feist73df0db2019-03-25 15:29:35 -07001586
1587 std::string chassis;
1588 CreatePIDRet ret = createPidInterface(
James Feist6ee7f772020-02-06 16:25:27 -08001589 response, type, it, path, managedObj, createNewObject,
1590 output, chassis, currentProfile);
James Feist73df0db2019-03-25 15:29:35 -07001591 if (ret == CreatePIDRet::fail)
1592 {
1593 return;
1594 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001595 if (ret == CreatePIDRet::del)
James Feist73df0db2019-03-25 15:29:35 -07001596 {
1597 continue;
1598 }
1599
1600 if (!createNewObject)
1601 {
1602 for (const auto& property : output)
1603 {
Potin Lai7a696972023-11-09 12:18:20 +08001604 crow::connections::systemBus->async_method_call(
James Feist73df0db2019-03-25 15:29:35 -07001605 [response,
1606 propertyName{std::string(property.first)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001607 const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001608 if (ec)
1609 {
Ed Tanous62598e32023-07-17 17:06:25 -07001610 BMCWEB_LOG_ERROR("Error patching {}: {}",
1611 propertyName, ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001612 messages::internalError(response->res);
1613 return;
1614 }
1615 messages::success(response->res);
Potin Lai7a696972023-11-09 12:18:20 +08001616 },
1617 "xyz.openbmc_project.EntityManager", path,
1618 "org.freedesktop.DBus.Properties", "Set", iface,
1619 property.first, property.second);
James Feist73df0db2019-03-25 15:29:35 -07001620 }
1621 }
1622 else
1623 {
1624 if (chassis.empty())
1625 {
Ed Tanous62598e32023-07-17 17:06:25 -07001626 BMCWEB_LOG_ERROR("Failed to get chassis from config");
Ed Tanousace85d62021-10-26 12:45:59 -07001627 messages::internalError(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001628 return;
1629 }
1630
1631 bool foundChassis = false;
1632 for (const auto& obj : managedObj)
1633 {
Ed Tanous18f8f602023-07-18 10:07:23 -07001634 if (obj.first.parent_path() == chassis)
James Feist73df0db2019-03-25 15:29:35 -07001635 {
1636 chassis = obj.first.str;
1637 foundChassis = true;
1638 break;
1639 }
1640 }
1641 if (!foundChassis)
1642 {
Ed Tanous62598e32023-07-17 17:06:25 -07001643 BMCWEB_LOG_ERROR("Failed to find chassis on dbus");
James Feist73df0db2019-03-25 15:29:35 -07001644 messages::resourceMissingAtURI(
Ed Tanousace85d62021-10-26 12:45:59 -07001645 response->res,
Ed Tanousef4c65b2023-04-24 15:28:50 -07001646 boost::urls::format("/redfish/v1/Chassis/{}",
1647 chassis));
James Feist73df0db2019-03-25 15:29:35 -07001648 return;
1649 }
1650
1651 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001652 [response](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001653 if (ec)
1654 {
Ed Tanous62598e32023-07-17 17:06:25 -07001655 BMCWEB_LOG_ERROR("Error Adding Pid Object {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001656 messages::internalError(response->res);
1657 return;
1658 }
1659 messages::success(response->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05001660 },
James Feist73df0db2019-03-25 15:29:35 -07001661 "xyz.openbmc_project.EntityManager", chassis,
1662 "xyz.openbmc_project.AddObject", "AddObject", output);
1663 }
1664 }
1665 }
1666 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001667
1668 ~SetPIDValues()
1669 {
1670 try
1671 {
1672 pidSetDone();
1673 }
1674 catch (...)
1675 {
Ed Tanous62598e32023-07-17 17:06:25 -07001676 BMCWEB_LOG_CRITICAL("pidSetDone threw exception");
Ed Tanous24b2fe82022-01-06 12:45:54 -08001677 }
1678 }
1679
zhanghch058d1b46d2021-04-01 11:18:24 +08001680 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001681 std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
1682 configuration;
1683 std::optional<std::string> profile;
1684 dbus::utility::ManagedObjectType managedObj;
1685 std::vector<std::string> supportedProfiles;
1686 std::string currentProfile;
1687 std::string profileConnection;
1688 std::string profilePath;
James Feist14b0b8d2020-02-12 11:52:07 -08001689 size_t objectCount = 0;
James Feist73df0db2019-03-25 15:29:35 -07001690};
James Feist83ff9ab2018-08-31 10:18:24 -07001691
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001692/**
1693 * @brief Retrieves BMC manager location data over DBus
1694 *
Ed Tanousac106bf2023-06-07 09:24:59 -07001695 * @param[in] asyncResp Shared pointer for completing asynchronous calls
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001696 * @param[in] connectionName - service name
1697 * @param[in] path - object path
1698 * @return none
1699 */
Ed Tanousac106bf2023-06-07 09:24:59 -07001700inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001701 const std::string& connectionName,
1702 const std::string& path)
1703{
Ed Tanous62598e32023-07-17 17:06:25 -07001704 BMCWEB_LOG_DEBUG("Get BMC manager Location data.");
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001705
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001706 sdbusplus::asio::getProperty<std::string>(
1707 *crow::connections::systemBus, connectionName, path,
1708 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Ed Tanousac106bf2023-06-07 09:24:59 -07001709 [asyncResp](const boost::system::error_code& ec,
1710 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001711 if (ec)
1712 {
Ed Tanous62598e32023-07-17 17:06:25 -07001713 BMCWEB_LOG_DEBUG("DBUS response error for "
1714 "Location");
Ed Tanousac106bf2023-06-07 09:24:59 -07001715 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001716 return;
1717 }
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001718
Ed Tanousac106bf2023-06-07 09:24:59 -07001719 asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001720 property;
Patrick Williams5a39f772023-10-20 11:20:21 -05001721 });
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001722}
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001723// avoid name collision systems.hpp
1724inline void
Ed Tanousac106bf2023-06-07 09:24:59 -07001725 managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001726{
Ed Tanous62598e32023-07-17 17:06:25 -07001727 BMCWEB_LOG_DEBUG("Getting Manager Last Reset Time");
Ed Tanous52cc1122020-07-18 13:51:21 -07001728
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001729 sdbusplus::asio::getProperty<uint64_t>(
1730 *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
1731 "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
1732 "LastRebootTime",
Ed Tanousac106bf2023-06-07 09:24:59 -07001733 [asyncResp](const boost::system::error_code& ec,
1734 const uint64_t lastResetTime) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001735 if (ec)
1736 {
Ed Tanous62598e32023-07-17 17:06:25 -07001737 BMCWEB_LOG_DEBUG("D-BUS response error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001738 return;
1739 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001740
Ed Tanous002d39b2022-05-31 08:59:27 -07001741 // LastRebootTime is epoch time, in milliseconds
1742 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
1743 uint64_t lastResetTimeStamp = lastResetTime / 1000;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001744
Ed Tanous002d39b2022-05-31 08:59:27 -07001745 // Convert to ISO 8601 standard
Ed Tanousac106bf2023-06-07 09:24:59 -07001746 asyncResp->res.jsonValue["LastResetTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001747 redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
Patrick Williams5a39f772023-10-20 11:20:21 -05001748 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001749}
1750
1751/**
1752 * @brief Set the running firmware image
1753 *
Ed Tanousac106bf2023-06-07 09:24:59 -07001754 * @param[i,o] asyncResp - Async response object
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001755 * @param[i] runningFirmwareTarget - Image to make the running image
1756 *
1757 * @return void
1758 */
1759inline void
Ed Tanousac106bf2023-06-07 09:24:59 -07001760 setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001761 const std::string& runningFirmwareTarget)
1762{
1763 // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1764 std::string::size_type idPos = runningFirmwareTarget.rfind('/');
1765 if (idPos == std::string::npos)
1766 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001767 messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001768 "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001769 BMCWEB_LOG_DEBUG("Can't parse firmware ID!");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001770 return;
1771 }
1772 idPos++;
1773 if (idPos >= runningFirmwareTarget.size())
1774 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001775 messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001776 "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001777 BMCWEB_LOG_DEBUG("Invalid firmware ID.");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001778 return;
1779 }
1780 std::string firmwareId = runningFirmwareTarget.substr(idPos);
1781
1782 // Make sure the image is valid before setting priority
George Liu5eb468d2023-06-20 17:03:24 +08001783 sdbusplus::message::object_path objPath("/xyz/openbmc_project/software");
1784 dbus::utility::getManagedObjects(
1785 "xyz.openbmc_project.Software.BMC.Updater", objPath,
1786 [asyncResp, firmwareId, runningFirmwareTarget](
1787 const boost::system::error_code& ec,
1788 const dbus::utility::ManagedObjectType& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001789 if (ec)
1790 {
Ed Tanous62598e32023-07-17 17:06:25 -07001791 BMCWEB_LOG_DEBUG("D-Bus response error getting objects.");
Ed Tanousac106bf2023-06-07 09:24:59 -07001792 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001793 return;
1794 }
1795
1796 if (subtree.empty())
1797 {
Ed Tanous62598e32023-07-17 17:06:25 -07001798 BMCWEB_LOG_DEBUG("Can't find image!");
Ed Tanousac106bf2023-06-07 09:24:59 -07001799 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001800 return;
1801 }
1802
1803 bool foundImage = false;
Ed Tanous02cad962022-06-30 16:50:15 -07001804 for (const auto& object : subtree)
Ed Tanous002d39b2022-05-31 08:59:27 -07001805 {
1806 const std::string& path =
1807 static_cast<const std::string&>(object.first);
1808 std::size_t idPos2 = path.rfind('/');
1809
1810 if (idPos2 == std::string::npos)
1811 {
1812 continue;
1813 }
1814
1815 idPos2++;
1816 if (idPos2 >= path.size())
1817 {
1818 continue;
1819 }
1820
1821 if (path.substr(idPos2) == firmwareId)
1822 {
1823 foundImage = true;
1824 break;
1825 }
1826 }
1827
1828 if (!foundImage)
1829 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001830 messages::propertyValueNotInList(
1831 asyncResp->res, runningFirmwareTarget, "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001832 BMCWEB_LOG_DEBUG("Invalid firmware ID.");
Ed Tanous002d39b2022-05-31 08:59:27 -07001833 return;
1834 }
1835
Ed Tanous62598e32023-07-17 17:06:25 -07001836 BMCWEB_LOG_DEBUG("Setting firmware version {} to priority 0.",
1837 firmwareId);
Ed Tanous002d39b2022-05-31 08:59:27 -07001838
1839 // Only support Immediate
1840 // An addition could be a Redfish Setting like
1841 // ActiveSoftwareImageApplyTime and support OnReset
George Liu9ae226f2023-06-21 17:56:46 +08001842 sdbusplus::asio::setProperty(
1843 *crow::connections::systemBus,
1844 "xyz.openbmc_project.Software.BMC.Updater",
1845 "/xyz/openbmc_project/software/" + firmwareId,
1846 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1847 static_cast<uint8_t>(0),
Ed Tanousac106bf2023-06-07 09:24:59 -07001848 [asyncResp](const boost::system::error_code& ec2) {
Ed Tanous8a592812022-06-04 09:06:59 -07001849 if (ec2)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001850 {
Ed Tanous62598e32023-07-17 17:06:25 -07001851 BMCWEB_LOG_DEBUG("D-Bus response error setting.");
Ed Tanousac106bf2023-06-07 09:24:59 -07001852 messages::internalError(asyncResp->res);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001853 return;
1854 }
Ed Tanousac106bf2023-06-07 09:24:59 -07001855 doBMCGracefulRestart(asyncResp);
George Liu5eb468d2023-06-20 17:03:24 +08001856 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001857 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001858}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001859
Ed Tanousc51afd52024-03-07 10:13:14 -08001860inline void
1861 afterSetDateTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1862 const boost::system::error_code& ec,
1863 const sdbusplus::message_t& msg)
1864{
1865 if (ec)
1866 {
1867 BMCWEB_LOG_DEBUG("Failed to set elapsed time. DBUS response error {}",
1868 ec);
1869 const sd_bus_error* dbusError = msg.get_error();
1870 if (dbusError != nullptr)
1871 {
1872 std::string_view errorName(dbusError->name);
1873 if (errorName ==
1874 "org.freedesktop.timedate1.AutomaticTimeSyncEnabled")
1875 {
1876 BMCWEB_LOG_DEBUG("Setting conflict");
1877 messages::propertyValueConflict(
1878 asyncResp->res, "DateTime",
1879 "Managers/NetworkProtocol/NTPProcotolEnabled");
1880 return;
1881 }
1882 }
1883 messages::internalError(asyncResp->res);
1884 return;
1885 }
1886 asyncResp->res.result(boost::beast::http::status::no_content);
1887}
1888
1889inline void setDateTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1890 const std::string& datetime)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001891{
Ed Tanous62598e32023-07-17 17:06:25 -07001892 BMCWEB_LOG_DEBUG("Set date time: {}", datetime);
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001893
Ed Tanousc2e32002023-01-07 22:05:08 -08001894 std::optional<redfish::time_utils::usSinceEpoch> us =
1895 redfish::time_utils::dateStringToEpoch(datetime);
1896 if (!us)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001897 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001898 messages::propertyValueFormatError(asyncResp->res, datetime,
1899 "DateTime");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001900 return;
1901 }
Ed Tanousc51afd52024-03-07 10:13:14 -08001902 // Set the absolute datetime
1903 bool relative = false;
1904 bool interactive = false;
1905 crow::connections::systemBus->async_method_call(
1906 [asyncResp](const boost::system::error_code& ec,
1907 const sdbusplus::message_t& msg) {
1908 afterSetDateTime(asyncResp, ec, msg);
1909 },
1910 "org.freedesktop.timedate1", "/org/freedesktop/timedate1",
1911 "org.freedesktop.timedate1", "SetTime", us->count(), relative,
1912 interactive);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001913}
1914
Ed Tanous75815e52022-10-05 17:21:13 -07001915inline void
1916 checkForQuiesced(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1917{
1918 sdbusplus::asio::getProperty<std::string>(
1919 *crow::connections::systemBus, "org.freedesktop.systemd1",
1920 "/org/freedesktop/systemd1/unit/obmc-bmc-service-quiesce@0.target",
1921 "org.freedesktop.systemd1.Unit", "ActiveState",
1922 [asyncResp](const boost::system::error_code& ec,
1923 const std::string& val) {
1924 if (!ec)
1925 {
1926 if (val == "active")
1927 {
1928 asyncResp->res.jsonValue["Status"]["Health"] = "Critical";
1929 asyncResp->res.jsonValue["Status"]["State"] = "Quiesced";
1930 return;
1931 }
1932 }
1933 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
1934 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
Patrick Williams5a39f772023-10-20 11:20:21 -05001935 });
Ed Tanous75815e52022-10-05 17:21:13 -07001936}
1937
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001938inline void requestRoutesManager(App& app)
1939{
1940 std::string uuid = persistent_data::getConfig().systemUuid;
1941
1942 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07001943 .privileges(redfish::privileges::getManager)
Ed Tanous002d39b2022-05-31 08:59:27 -07001944 .methods(boost::beast::http::verb::get)(
1945 [&app, uuid](const crow::Request& req,
Ed Tanous45ca1b82022-03-25 13:07:27 -07001946 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001947 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001948 {
1949 return;
1950 }
1951 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
Sui Chena51fc2d2022-07-14 17:21:53 -07001952 asyncResp->res.jsonValue["@odata.type"] = "#Manager.v1_14_0.Manager";
Ed Tanous002d39b2022-05-31 08:59:27 -07001953 asyncResp->res.jsonValue["Id"] = "bmc";
1954 asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
1955 asyncResp->res.jsonValue["Description"] =
1956 "Baseboard Management Controller";
1957 asyncResp->res.jsonValue["PowerState"] = "On";
Ed Tanous14766872022-03-15 10:44:42 -07001958
Ed Tanous002d39b2022-05-31 08:59:27 -07001959 asyncResp->res.jsonValue["ManagerType"] = "BMC";
1960 asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
1961 asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1962 asyncResp->res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001963
Ed Tanous002d39b2022-05-31 08:59:27 -07001964 asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
1965 "/redfish/v1/Managers/bmc/LogServices";
1966 asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
1967 "/redfish/v1/Managers/bmc/NetworkProtocol";
1968 asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
1969 "/redfish/v1/Managers/bmc/EthernetInterfaces";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001970
1971#ifdef BMCWEB_ENABLE_VM_NBDPROXY
Ed Tanous002d39b2022-05-31 08:59:27 -07001972 asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
1973 "/redfish/v1/Managers/bmc/VirtualMedia";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001974#endif // BMCWEB_ENABLE_VM_NBDPROXY
1975
Ed Tanous002d39b2022-05-31 08:59:27 -07001976 // default oem data
1977 nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
1978 nlohmann::json& oemOpenbmc = oem["OpenBmc"];
1979 oem["@odata.type"] = "#OemManager.Oem";
1980 oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
1981 oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
1982 oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
Ed Tanous14766872022-03-15 10:44:42 -07001983
Ed Tanous002d39b2022-05-31 08:59:27 -07001984 nlohmann::json::object_t certificates;
1985 certificates["@odata.id"] =
1986 "/redfish/v1/Managers/bmc/Truststore/Certificates";
1987 oemOpenbmc["Certificates"] = std::move(certificates);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001988
Ed Tanous002d39b2022-05-31 08:59:27 -07001989 // Manager.Reset (an action) can be many values, OpenBMC only
1990 // supports BMC reboot.
1991 nlohmann::json& managerReset =
1992 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
1993 managerReset["target"] =
1994 "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
1995 managerReset["@Redfish.ActionInfo"] =
1996 "/redfish/v1/Managers/bmc/ResetActionInfo";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001997
Ed Tanous002d39b2022-05-31 08:59:27 -07001998 // ResetToDefaults (Factory Reset) has values like
1999 // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
2000 // on OpenBMC
2001 nlohmann::json& resetToDefaults =
2002 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
2003 resetToDefaults["target"] =
2004 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
Ed Tanous613dabe2022-07-09 11:17:36 -07002005 resetToDefaults["ResetType@Redfish.AllowableValues"] =
2006 nlohmann::json::array_t({"ResetAll"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002007
Ed Tanous002d39b2022-05-31 08:59:27 -07002008 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002009 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05302010
Ed Tanous002d39b2022-05-31 08:59:27 -07002011 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2012 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2013 redfishDateTimeOffset.second;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002014
Ed Tanous002d39b2022-05-31 08:59:27 -07002015 // TODO (Gunnar): Remove these one day since moved to ComputerSystem
2016 // Still used by OCP profiles
2017 // https://github.com/opencomputeproject/OCP-Profiles/issues/23
2018 // Fill in SerialConsole info
2019 asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
2020 asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
Ed Tanous613dabe2022-07-09 11:17:36 -07002021 asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
2022 nlohmann::json::array_t({"IPMI", "SSH"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002023#ifdef BMCWEB_ENABLE_KVM
Ed Tanous002d39b2022-05-31 08:59:27 -07002024 // Fill in GraphicalConsole info
2025 asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
2026 asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] =
2027 4;
Ed Tanous613dabe2022-07-09 11:17:36 -07002028 asyncResp->res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
2029 nlohmann::json::array_t({"KVMIP"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002030#endif // BMCWEB_ENABLE_KVM
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002031 if constexpr (!bmcwebEnableMultiHost)
2032 {
2033 asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] =
2034 1;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002035
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002036 nlohmann::json::array_t managerForServers;
2037 nlohmann::json::object_t manager;
2038 manager["@odata.id"] = "/redfish/v1/Systems/system";
2039 managerForServers.emplace_back(std::move(manager));
Ed Tanous14766872022-03-15 10:44:42 -07002040
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002041 asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
2042 std::move(managerForServers);
2043 }
Willy Tu13451e32023-05-24 16:08:18 -07002044 if constexpr (bmcwebEnableHealthPopulate)
2045 {
2046 auto health = std::make_shared<HealthPopulate>(asyncResp);
2047 health->isManagersHealth = true;
2048 health->populate();
2049 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002050
Willy Tueee00132022-06-14 14:53:17 -07002051 sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
Ed Tanous002d39b2022-05-31 08:59:27 -07002052 "FirmwareVersion", true);
2053
2054 managerGetLastResetTime(asyncResp);
2055
Sui Chena51fc2d2022-07-14 17:21:53 -07002056 // ManagerDiagnosticData is added for all BMCs.
2057 nlohmann::json& managerDiagnosticData =
2058 asyncResp->res.jsonValue["ManagerDiagnosticData"];
2059 managerDiagnosticData["@odata.id"] =
2060 "/redfish/v1/Managers/bmc/ManagerDiagnosticData";
2061
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002062#ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
Ed Tanous002d39b2022-05-31 08:59:27 -07002063 auto pids = std::make_shared<GetPIDValues>(asyncResp);
2064 pids->run();
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002065#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07002066
2067 getMainChassisId(asyncResp,
2068 [](const std::string& chassisId,
2069 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2070 aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
2071 nlohmann::json::array_t managerForChassis;
Ed Tanous8a592812022-06-04 09:06:59 -07002072 nlohmann::json::object_t managerObj;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002073 boost::urls::url chassiUrl =
2074 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
Willy Tueddfc432022-09-26 16:46:38 +00002075 managerObj["@odata.id"] = chassiUrl;
Patrick Williamsad539542023-05-12 10:10:08 -05002076 managerForChassis.emplace_back(std::move(managerObj));
Ed Tanous002d39b2022-05-31 08:59:27 -07002077 aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
2078 std::move(managerForChassis);
2079 aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00002080 chassiUrl;
Ed Tanous002d39b2022-05-31 08:59:27 -07002081 });
Ed Tanous14766872022-03-15 10:44:42 -07002082
Ed Tanous75815e52022-10-05 17:21:13 -07002083 sdbusplus::asio::getProperty<double>(
2084 *crow::connections::systemBus, "org.freedesktop.systemd1",
2085 "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
2086 "Progress",
2087 [asyncResp](const boost::system::error_code& ec, double val) {
2088 if (ec)
2089 {
Ed Tanous62598e32023-07-17 17:06:25 -07002090 BMCWEB_LOG_ERROR("Error while getting progress");
Ed Tanous75815e52022-10-05 17:21:13 -07002091 messages::internalError(asyncResp->res);
2092 return;
2093 }
2094 if (val < 1.0)
2095 {
2096 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
2097 asyncResp->res.jsonValue["Status"]["State"] = "Starting";
2098 return;
2099 }
2100 checkForQuiesced(asyncResp);
Patrick Williams5a39f772023-10-20 11:20:21 -05002101 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002102
George Liue99073f2022-12-09 11:06:16 +08002103 constexpr std::array<std::string_view, 1> interfaces = {
2104 "xyz.openbmc_project.Inventory.Item.Bmc"};
2105 dbus::utility::getSubTree(
2106 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07002107 [asyncResp](
George Liue99073f2022-12-09 11:06:16 +08002108 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07002109 const dbus::utility::MapperGetSubTreeResponse& subtree) {
2110 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002111 {
Ed Tanous62598e32023-07-17 17:06:25 -07002112 BMCWEB_LOG_DEBUG("D-Bus response error on GetSubTree {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002113 return;
2114 }
2115 if (subtree.empty())
2116 {
Ed Tanous62598e32023-07-17 17:06:25 -07002117 BMCWEB_LOG_DEBUG("Can't find bmc D-Bus object!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002118 return;
2119 }
2120 // Assume only 1 bmc D-Bus object
2121 // Throw an error if there is more than 1
2122 if (subtree.size() > 1)
2123 {
Ed Tanous62598e32023-07-17 17:06:25 -07002124 BMCWEB_LOG_DEBUG("Found more than 1 bmc D-Bus object!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002125 messages::internalError(asyncResp->res);
2126 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002127 }
2128
Ed Tanous002d39b2022-05-31 08:59:27 -07002129 if (subtree[0].first.empty() || subtree[0].second.size() != 1)
2130 {
Ed Tanous62598e32023-07-17 17:06:25 -07002131 BMCWEB_LOG_DEBUG("Error getting bmc D-Bus object!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002132 messages::internalError(asyncResp->res);
2133 return;
2134 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002135
Ed Tanous002d39b2022-05-31 08:59:27 -07002136 const std::string& path = subtree[0].first;
2137 const std::string& connectionName = subtree[0].second[0].first;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002138
Ed Tanous002d39b2022-05-31 08:59:27 -07002139 for (const auto& interfaceName : subtree[0].second[0].second)
2140 {
2141 if (interfaceName ==
2142 "xyz.openbmc_project.Inventory.Decorator.Asset")
2143 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002144 sdbusplus::asio::getAllProperties(
2145 *crow::connections::systemBus, connectionName, path,
2146 "xyz.openbmc_project.Inventory.Decorator.Asset",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002147 [asyncResp](const boost::system::error_code& ec2,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002148 const dbus::utility::DBusPropertiesMap&
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002149 propertiesList) {
Ed Tanous8a592812022-06-04 09:06:59 -07002150 if (ec2)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002151 {
Ed Tanous62598e32023-07-17 17:06:25 -07002152 BMCWEB_LOG_DEBUG("Can't get bmc asset!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002153 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002154 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002155
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002156 const std::string* partNumber = nullptr;
2157 const std::string* serialNumber = nullptr;
2158 const std::string* manufacturer = nullptr;
2159 const std::string* model = nullptr;
2160 const std::string* sparePartNumber = nullptr;
2161
2162 const bool success = sdbusplus::unpackPropertiesNoThrow(
2163 dbus_utils::UnpackErrorPrinter(), propertiesList,
2164 "PartNumber", partNumber, "SerialNumber",
2165 serialNumber, "Manufacturer", manufacturer, "Model",
2166 model, "SparePartNumber", sparePartNumber);
2167
2168 if (!success)
2169 {
2170 messages::internalError(asyncResp->res);
2171 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07002172 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002173
2174 if (partNumber != nullptr)
2175 {
2176 asyncResp->res.jsonValue["PartNumber"] =
2177 *partNumber;
2178 }
2179
2180 if (serialNumber != nullptr)
2181 {
2182 asyncResp->res.jsonValue["SerialNumber"] =
2183 *serialNumber;
2184 }
2185
2186 if (manufacturer != nullptr)
2187 {
2188 asyncResp->res.jsonValue["Manufacturer"] =
2189 *manufacturer;
2190 }
2191
2192 if (model != nullptr)
2193 {
2194 asyncResp->res.jsonValue["Model"] = *model;
2195 }
2196
2197 if (sparePartNumber != nullptr)
2198 {
2199 asyncResp->res.jsonValue["SparePartNumber"] =
2200 *sparePartNumber;
2201 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002202 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002203 }
2204 else if (interfaceName ==
2205 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
2206 {
2207 getLocation(asyncResp, connectionName, path);
2208 }
2209 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002210 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002211 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002212
2213 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07002214 .privileges(redfish::privileges::patchManager)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002215 .methods(boost::beast::http::verb::patch)(
2216 [&app](const crow::Request& req,
2217 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002218 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002219 {
2220 return;
2221 }
2222 std::optional<nlohmann::json> oem;
2223 std::optional<nlohmann::json> links;
2224 std::optional<std::string> datetime;
2225
2226 if (!json_util::readJsonPatch(req, asyncResp->res, "Oem", oem,
2227 "DateTime", datetime, "Links", links))
2228 {
2229 return;
2230 }
2231
2232 if (oem)
2233 {
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002234#ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
Ed Tanous002d39b2022-05-31 08:59:27 -07002235 std::optional<nlohmann::json> openbmc;
2236 if (!redfish::json_util::readJson(*oem, asyncResp->res, "OpenBmc",
2237 openbmc))
2238 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002239 return;
2240 }
2241 if (openbmc)
2242 {
2243 std::optional<nlohmann::json> fan;
2244 if (!redfish::json_util::readJson(*openbmc, asyncResp->res,
2245 "Fan", fan))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002246 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002247 return;
2248 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002249 if (fan)
2250 {
2251 auto pid = std::make_shared<SetPIDValues>(asyncResp, *fan);
2252 pid->run();
2253 }
2254 }
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002255#else
2256 messages::propertyUnknown(asyncResp->res, "Oem");
2257 return;
2258#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07002259 }
2260 if (links)
2261 {
2262 std::optional<nlohmann::json> activeSoftwareImage;
2263 if (!redfish::json_util::readJson(*links, asyncResp->res,
2264 "ActiveSoftwareImage",
2265 activeSoftwareImage))
2266 {
2267 return;
2268 }
2269 if (activeSoftwareImage)
2270 {
2271 std::optional<std::string> odataId;
2272 if (!json_util::readJson(*activeSoftwareImage, asyncResp->res,
2273 "@odata.id", odataId))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002274 {
Ed Tanous45ca1b82022-03-25 13:07:27 -07002275 return;
2276 }
2277
Ed Tanous002d39b2022-05-31 08:59:27 -07002278 if (odataId)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002279 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002280 setActiveFirmwareImage(asyncResp, *odataId);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002281 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002282 }
2283 }
2284 if (datetime)
2285 {
Ed Tanousc51afd52024-03-07 10:13:14 -08002286 setDateTime(asyncResp, *datetime);
Ed Tanous002d39b2022-05-31 08:59:27 -07002287 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002288 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002289}
2290
2291inline void requestRoutesManagerCollection(App& app)
2292{
2293 BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
Ed Tanoused398212021-06-09 17:05:54 -07002294 .privileges(redfish::privileges::getManagerCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002295 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002296 [&app](const crow::Request& req,
2297 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002298 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002299 {
2300 return;
2301 }
2302 // Collections don't include the static data added by SubRoute
2303 // because it has a duplicate entry for members
2304 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
2305 asyncResp->res.jsonValue["@odata.type"] =
2306 "#ManagerCollection.ManagerCollection";
2307 asyncResp->res.jsonValue["Name"] = "Manager Collection";
2308 asyncResp->res.jsonValue["Members@odata.count"] = 1;
2309 nlohmann::json::array_t members;
2310 nlohmann::json& bmc = members.emplace_back();
2311 bmc["@odata.id"] = "/redfish/v1/Managers/bmc";
2312 asyncResp->res.jsonValue["Members"] = std::move(members);
Patrick Williams5a39f772023-10-20 11:20:21 -05002313 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002314}
Ed Tanous1abe55e2018-09-05 08:30:59 -07002315} // namespace redfish