blob: 592bbd8642d9b0835e61e4cd98708d5ad39cb32b [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"
Sui Chena51fc2d2022-07-14 17:21:53 -070022#include "query.hpp"
Jennifer Leec5d03ff2019-03-08 15:42:58 -080023#include "redfish_util.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070024#include "registries/privilege_registry.hpp"
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +020025#include "utils/dbus_utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080026#include "utils/json_utils.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070027#include "utils/sw_utils.hpp"
28#include "utils/systemd_utils.hpp"
Ed Tanous2b829372022-08-03 14:22:34 -070029#include "utils/time_utils.hpp"
Borawski.Lukasz9c3106852018-02-09 15:24:22 +010030
George Liue99073f2022-12-09 11:06:16 +080031#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070032#include <boost/url/format.hpp>
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +020033#include <sdbusplus/asio/property.hpp>
34#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050035
Ed Tanousa170f272022-06-30 21:53:27 -070036#include <algorithm>
George Liue99073f2022-12-09 11:06:16 +080037#include <array>
Gunnar Mills4bfefa72020-07-30 13:54:29 -050038#include <cstdint>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050039#include <memory>
Konstantin Aladyshev9970e932024-02-20 09:51:29 +030040#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070041#include <ranges>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050042#include <sstream>
Konstantin Aladyshev9970e932024-02-20 09:51:29 +030043#include <string>
George Liue99073f2022-12-09 11:06:16 +080044#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080045#include <variant>
James Feist5b4aa862018-08-16 14:07:01 -070046
Ed Tanous1abe55e2018-09-05 08:30:59 -070047namespace redfish
48{
Jennifer Leeed5befb2018-08-10 11:29:45 -070049
50/**
Gunnar Mills2a5c4402020-05-19 09:07:24 -050051 * Function reboots the BMC.
52 *
53 * @param[in] asyncResp - Shared pointer for completing asynchronous calls
Jennifer Leeed5befb2018-08-10 11:29:45 -070054 */
zhanghch058d1b46d2021-04-01 11:18:24 +080055inline void
56 doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Gunnar Mills2a5c4402020-05-19 09:07:24 -050057{
58 const char* processName = "xyz.openbmc_project.State.BMC";
59 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
60 const char* interfaceName = "xyz.openbmc_project.State.BMC";
61 const std::string& propertyValue =
62 "xyz.openbmc_project.State.BMC.Transition.Reboot";
63 const char* destProperty = "RequestedBMCTransition";
64
65 // Create the D-Bus variant for D-Bus call.
George Liu9ae226f2023-06-21 17:56:46 +080066 sdbusplus::asio::setProperty(
67 *crow::connections::systemBus, processName, objectPath, interfaceName,
68 destProperty, propertyValue,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080069 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -070070 // Use "Set" method to set the property value.
71 if (ec)
72 {
Ed Tanous62598e32023-07-17 17:06:25 -070073 BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -070074 messages::internalError(asyncResp->res);
75 return;
76 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -050077
Ed Tanous002d39b2022-05-31 08:59:27 -070078 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -050079 });
Gunnar Mills2a5c4402020-05-19 09:07:24 -050080}
81
zhanghch058d1b46d2021-04-01 11:18:24 +080082inline void
83 doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +000084{
85 const char* processName = "xyz.openbmc_project.State.BMC";
86 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
87 const char* interfaceName = "xyz.openbmc_project.State.BMC";
88 const std::string& propertyValue =
89 "xyz.openbmc_project.State.BMC.Transition.HardReboot";
90 const char* destProperty = "RequestedBMCTransition";
91
92 // Create the D-Bus variant for D-Bus call.
George Liu9ae226f2023-06-21 17:56:46 +080093 sdbusplus::asio::setProperty(
94 *crow::connections::systemBus, processName, objectPath, interfaceName,
95 destProperty, propertyValue,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080096 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -070097 // Use "Set" method to set the property value.
98 if (ec)
99 {
Ed Tanous62598e32023-07-17 17:06:25 -0700100 BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700101 messages::internalError(asyncResp->res);
102 return;
103 }
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000104
Ed Tanous002d39b2022-05-31 08:59:27 -0700105 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -0500106 });
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000107}
108
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500109/**
110 * ManagerResetAction class supports the POST method for the Reset (reboot)
111 * action.
112 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700113inline void requestRoutesManagerResetAction(App& app)
Jennifer Leeed5befb2018-08-10 11:29:45 -0700114{
Jennifer Leeed5befb2018-08-10 11:29:45 -0700115 /**
Jennifer Leeed5befb2018-08-10 11:29:45 -0700116 * Function handles POST method request.
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500117 * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000118 * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
Jennifer Leeed5befb2018-08-10 11:29:45 -0700119 */
Jennifer Leeed5befb2018-08-10 11:29:45 -0700120
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700121 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
Ed Tanoused398212021-06-09 17:05:54 -0700122 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700123 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700124 [&app](const crow::Request& req,
125 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000126 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700127 {
128 return;
129 }
Ed Tanous62598e32023-07-17 17:06:25 -0700130 BMCWEB_LOG_DEBUG("Post Manager Reset.");
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500131
Ed Tanous002d39b2022-05-31 08:59:27 -0700132 std::string resetType;
Jennifer Leeed5befb2018-08-10 11:29:45 -0700133
Ed Tanous002d39b2022-05-31 08:59:27 -0700134 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
135 resetType))
136 {
137 return;
138 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500139
Ed Tanous002d39b2022-05-31 08:59:27 -0700140 if (resetType == "GracefulRestart")
141 {
Ed Tanous62598e32023-07-17 17:06:25 -0700142 BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
Ed Tanous002d39b2022-05-31 08:59:27 -0700143 doBMCGracefulRestart(asyncResp);
144 return;
145 }
146 if (resetType == "ForceRestart")
147 {
Ed Tanous62598e32023-07-17 17:06:25 -0700148 BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
Ed Tanous002d39b2022-05-31 08:59:27 -0700149 doBMCForceRestart(asyncResp);
150 return;
151 }
Ed Tanous62598e32023-07-17 17:06:25 -0700152 BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}", resetType);
Ed Tanous002d39b2022-05-31 08:59:27 -0700153 messages::actionParameterNotSupported(asyncResp->res, resetType,
154 "ResetType");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700155
Ed Tanous002d39b2022-05-31 08:59:27 -0700156 return;
Patrick Williams5a39f772023-10-20 11:20:21 -0500157 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700158}
Jennifer Leeed5befb2018-08-10 11:29:45 -0700159
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500160/**
161 * ManagerResetToDefaultsAction class supports POST method for factory reset
162 * action.
163 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700164inline void requestRoutesManagerResetToDefaultsAction(App& app)
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500165{
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500166 /**
167 * Function handles ResetToDefaults POST method request.
168 *
169 * Analyzes POST body message and factory resets BMC by calling
170 * BMC code updater factory reset followed by a BMC reboot.
171 *
172 * BMC code updater factory reset wipes the whole BMC read-write
173 * filesystem which includes things like the network settings.
174 *
175 * OpenBMC only supports ResetToDefaultsType "ResetAll".
176 */
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500177
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700178 BMCWEB_ROUTE(app,
179 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/")
Ed Tanoused398212021-06-09 17:05:54 -0700180 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700181 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700182 [&app](const crow::Request& req,
183 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000184 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700185 {
186 return;
187 }
Ed Tanous62598e32023-07-17 17:06:25 -0700188 BMCWEB_LOG_DEBUG("Post ResetToDefaults.");
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500189
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300190 std::optional<std::string> resetType;
191 std::optional<std::string> resetToDefaultsType;
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500192
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300193 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
194 resetType, "ResetToDefaultsType",
195 resetToDefaultsType))
Ed Tanous002d39b2022-05-31 08:59:27 -0700196 {
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300197 BMCWEB_LOG_DEBUG("Missing property ResetType.");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700198
Ed Tanous002d39b2022-05-31 08:59:27 -0700199 messages::actionParameterMissing(asyncResp->res, "ResetToDefaults",
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300200 "ResetType");
Ed Tanous002d39b2022-05-31 08:59:27 -0700201 return;
202 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700203
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300204 if (resetToDefaultsType && !resetType)
205 {
206 BMCWEB_LOG_WARNING(
207 "Using deprecated ResetToDefaultsType, should be ResetType."
208 "Support for the ResetToDefaultsType will be dropped in 2Q24");
209 resetType = resetToDefaultsType;
210 }
211
Ed Tanous002d39b2022-05-31 08:59:27 -0700212 if (resetType != "ResetAll")
213 {
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300214 BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}",
215 *resetType);
216 messages::actionParameterNotSupported(asyncResp->res, *resetType,
217 "ResetType");
Ed Tanous002d39b2022-05-31 08:59:27 -0700218 return;
219 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700220
Ed Tanous002d39b2022-05-31 08:59:27 -0700221 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800222 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700223 if (ec)
224 {
Ed Tanous62598e32023-07-17 17:06:25 -0700225 BMCWEB_LOG_DEBUG("Failed to ResetToDefaults: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700226 messages::internalError(asyncResp->res);
227 return;
228 }
229 // Factory Reset doesn't actually happen until a reboot
230 // Can't erase what the BMC is running on
231 doBMCGracefulRestart(asyncResp);
Patrick Williams5a39f772023-10-20 11:20:21 -0500232 },
Ed Tanous002d39b2022-05-31 08:59:27 -0700233 "xyz.openbmc_project.Software.BMC.Updater",
234 "/xyz/openbmc_project/software",
235 "xyz.openbmc_project.Common.FactoryReset", "Reset");
Patrick Williams5a39f772023-10-20 11:20:21 -0500236 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700237}
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500238
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530239/**
240 * ManagerResetActionInfo derived class for delivering Manager
241 * ResetType AllowableValues using ResetInfo schema.
242 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700243inline void requestRoutesManagerResetActionInfo(App& app)
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530244{
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530245 /**
246 * Functions triggers appropriate requests on DBus
247 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700248
249 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/ResetActionInfo/")
Ed Tanoused398212021-06-09 17:05:54 -0700250 .privileges(redfish::privileges::getActionInfo)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700251 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700252 [&app](const crow::Request& req,
253 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000254 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700255 {
256 return;
257 }
Ed Tanous14766872022-03-15 10:44:42 -0700258
Ed Tanous002d39b2022-05-31 08:59:27 -0700259 asyncResp->res.jsonValue["@odata.type"] =
260 "#ActionInfo.v1_1_2.ActionInfo";
261 asyncResp->res.jsonValue["@odata.id"] =
262 "/redfish/v1/Managers/bmc/ResetActionInfo";
263 asyncResp->res.jsonValue["Name"] = "Reset Action Info";
264 asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
265 nlohmann::json::object_t parameter;
266 parameter["Name"] = "ResetType";
267 parameter["Required"] = true;
268 parameter["DataType"] = "String";
Ed Tanous14766872022-03-15 10:44:42 -0700269
Ed Tanous002d39b2022-05-31 08:59:27 -0700270 nlohmann::json::array_t allowableValues;
Patrick Williamsad539542023-05-12 10:10:08 -0500271 allowableValues.emplace_back("GracefulRestart");
272 allowableValues.emplace_back("ForceRestart");
Ed Tanous002d39b2022-05-31 08:59:27 -0700273 parameter["AllowableValues"] = std::move(allowableValues);
Ed Tanous14766872022-03-15 10:44:42 -0700274
Ed Tanous002d39b2022-05-31 08:59:27 -0700275 nlohmann::json::array_t parameters;
Patrick Williamsad539542023-05-12 10:10:08 -0500276 parameters.emplace_back(std::move(parameter));
Ed Tanous14766872022-03-15 10:44:42 -0700277
Ed Tanous002d39b2022-05-31 08:59:27 -0700278 asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
Patrick Williams5a39f772023-10-20 11:20:21 -0500279 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700280}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530281
James Feist5b4aa862018-08-16 14:07:01 -0700282static constexpr const char* objectManagerIface =
283 "org.freedesktop.DBus.ObjectManager";
284static constexpr const char* pidConfigurationIface =
285 "xyz.openbmc_project.Configuration.Pid";
286static constexpr const char* pidZoneConfigurationIface =
287 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feistb7a08d02018-12-11 14:55:37 -0800288static constexpr const char* stepwiseConfigurationIface =
289 "xyz.openbmc_project.Configuration.Stepwise";
James Feist73df0db2019-03-25 15:29:35 -0700290static constexpr const char* thermalModeIface =
291 "xyz.openbmc_project.Control.ThermalMode";
Borawski.Lukasz9c3106852018-02-09 15:24:22 +0100292
zhanghch058d1b46d2021-04-01 11:18:24 +0800293inline void
294 asyncPopulatePid(const std::string& connection, const std::string& path,
295 const std::string& currentProfile,
296 const std::vector<std::string>& supportedProfiles,
297 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
James Feist5b4aa862018-08-16 14:07:01 -0700298{
George Liu5eb468d2023-06-20 17:03:24 +0800299 sdbusplus::message::object_path objPath(path);
300 dbus::utility::getManagedObjects(
301 connection, objPath,
James Feist73df0db2019-03-25 15:29:35 -0700302 [asyncResp, currentProfile, supportedProfiles](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800303 const boost::system::error_code& ec,
James Feist73df0db2019-03-25 15:29:35 -0700304 const dbus::utility::ManagedObjectType& managedObj) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700305 if (ec)
306 {
Ed Tanous62598e32023-07-17 17:06:25 -0700307 BMCWEB_LOG_ERROR("{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700308 messages::internalError(asyncResp->res);
309 return;
310 }
311 nlohmann::json& configRoot =
312 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
313 nlohmann::json& fans = configRoot["FanControllers"];
314 fans["@odata.type"] = "#OemManager.FanControllers";
315 fans["@odata.id"] =
316 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers";
317
318 nlohmann::json& pids = configRoot["PidControllers"];
319 pids["@odata.type"] = "#OemManager.PidControllers";
320 pids["@odata.id"] =
321 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
322
323 nlohmann::json& stepwise = configRoot["StepwiseControllers"];
324 stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
325 stepwise["@odata.id"] =
326 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
327
328 nlohmann::json& zones = configRoot["FanZones"];
329 zones["@odata.id"] =
330 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
331 zones["@odata.type"] = "#OemManager.FanZones";
332 configRoot["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
333 configRoot["@odata.type"] = "#OemManager.Fan";
334 configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
335
336 if (!currentProfile.empty())
337 {
338 configRoot["Profile"] = currentProfile;
339 }
Carson Labradobf2dded2023-08-10 00:37:06 +0000340 BMCWEB_LOG_DEBUG("profile = {} !", currentProfile);
Ed Tanous002d39b2022-05-31 08:59:27 -0700341
342 for (const auto& pathPair : managedObj)
343 {
344 for (const auto& intfPair : pathPair.second)
James Feist5b4aa862018-08-16 14:07:01 -0700345 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700346 if (intfPair.first != pidConfigurationIface &&
347 intfPair.first != pidZoneConfigurationIface &&
348 intfPair.first != stepwiseConfigurationIface)
James Feist5b4aa862018-08-16 14:07:01 -0700349 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700350 continue;
351 }
James Feist73df0db2019-03-25 15:29:35 -0700352
Ed Tanous002d39b2022-05-31 08:59:27 -0700353 std::string name;
James Feist73df0db2019-03-25 15:29:35 -0700354
Ed Tanous002d39b2022-05-31 08:59:27 -0700355 for (const std::pair<std::string,
356 dbus::utility::DbusVariantType>& propPair :
357 intfPair.second)
358 {
359 if (propPair.first == "Name")
James Feist73df0db2019-03-25 15:29:35 -0700360 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700361 const std::string* namePtr =
362 std::get_if<std::string>(&propPair.second);
363 if (namePtr == nullptr)
James Feist73df0db2019-03-25 15:29:35 -0700364 {
Ed Tanous62598e32023-07-17 17:06:25 -0700365 BMCWEB_LOG_ERROR("Pid Name Field illegal");
James Feistc33a90e2019-03-01 10:17:44 -0800366 messages::internalError(asyncResp->res);
367 return;
368 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700369 name = *namePtr;
370 dbus::utility::escapePathForDbus(name);
James Feistb7a08d02018-12-11 14:55:37 -0800371 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700372 else if (propPair.first == "Profiles")
James Feistb7a08d02018-12-11 14:55:37 -0800373 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700374 const std::vector<std::string>* profiles =
375 std::get_if<std::vector<std::string>>(
376 &propPair.second);
377 if (profiles == nullptr)
James Feistb7a08d02018-12-11 14:55:37 -0800378 {
Ed Tanous62598e32023-07-17 17:06:25 -0700379 BMCWEB_LOG_ERROR("Pid Profiles Field illegal");
James Feistb7a08d02018-12-11 14:55:37 -0800380 messages::internalError(asyncResp->res);
381 return;
382 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700383 if (std::find(profiles->begin(), profiles->end(),
384 currentProfile) == profiles->end())
James Feistb7a08d02018-12-11 14:55:37 -0800385 {
Ed Tanous62598e32023-07-17 17:06:25 -0700386 BMCWEB_LOG_INFO(
387 "{} not supported in current profile", name);
Ed Tanous002d39b2022-05-31 08:59:27 -0700388 continue;
James Feistb7a08d02018-12-11 14:55:37 -0800389 }
390 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700391 }
392 nlohmann::json* config = nullptr;
393 const std::string* classPtr = nullptr;
394
395 for (const std::pair<std::string,
396 dbus::utility::DbusVariantType>& propPair :
397 intfPair.second)
398 {
399 if (propPair.first == "Class")
James Feistb7a08d02018-12-11 14:55:37 -0800400 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700401 classPtr = std::get_if<std::string>(&propPair.second);
402 }
403 }
404
Ed Tanousef4c65b2023-04-24 15:28:50 -0700405 boost::urls::url url("/redfish/v1/Managers/bmc");
Ed Tanous002d39b2022-05-31 08:59:27 -0700406 if (intfPair.first == pidZoneConfigurationIface)
407 {
408 std::string chassis;
409 if (!dbus::utility::getNthStringFromPath(pathPair.first.str,
410 5, chassis))
411 {
412 chassis = "#IllegalValue";
413 }
414 nlohmann::json& zone = zones[name];
Ed Tanousef4c65b2023-04-24 15:28:50 -0700415 zone["Chassis"]["@odata.id"] =
416 boost::urls::format("/redfish/v1/Chassis/{}", chassis);
Willy Tueddfc432022-09-26 16:46:38 +0000417 url.set_fragment(
418 ("/Oem/OpenBmc/Fan/FanZones"_json_pointer / name)
419 .to_string());
420 zone["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700421 zone["@odata.type"] = "#OemManager.FanZone";
422 config = &zone;
423 }
424
425 else if (intfPair.first == stepwiseConfigurationIface)
426 {
427 if (classPtr == nullptr)
428 {
Ed Tanous62598e32023-07-17 17:06:25 -0700429 BMCWEB_LOG_ERROR("Pid Class Field illegal");
James Feistb7a08d02018-12-11 14:55:37 -0800430 messages::internalError(asyncResp->res);
431 return;
432 }
433
Ed Tanous002d39b2022-05-31 08:59:27 -0700434 nlohmann::json& controller = stepwise[name];
435 config = &controller;
Willy Tueddfc432022-09-26 16:46:38 +0000436 url.set_fragment(
437 ("/Oem/OpenBmc/Fan/StepwiseControllers"_json_pointer /
438 name)
439 .to_string());
440 controller["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700441 controller["@odata.type"] =
442 "#OemManager.StepwiseController";
443
444 controller["Direction"] = *classPtr;
445 }
446
447 // pid and fans are off the same configuration
448 else if (intfPair.first == pidConfigurationIface)
449 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700450 if (classPtr == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700451 {
Ed Tanous62598e32023-07-17 17:06:25 -0700452 BMCWEB_LOG_ERROR("Pid Class Field illegal");
Ed Tanous002d39b2022-05-31 08:59:27 -0700453 messages::internalError(asyncResp->res);
454 return;
455 }
456 bool isFan = *classPtr == "fan";
457 nlohmann::json& element = isFan ? fans[name] : pids[name];
458 config = &element;
459 if (isFan)
460 {
Willy Tueddfc432022-09-26 16:46:38 +0000461 url.set_fragment(
462 ("/Oem/OpenBmc/Fan/FanControllers"_json_pointer /
463 name)
464 .to_string());
465 element["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700466 element["@odata.type"] = "#OemManager.FanController";
467 }
468 else
469 {
Willy Tueddfc432022-09-26 16:46:38 +0000470 url.set_fragment(
471 ("/Oem/OpenBmc/Fan/PidControllers"_json_pointer /
472 name)
473 .to_string());
474 element["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700475 element["@odata.type"] = "#OemManager.PidController";
476 }
477 }
478 else
479 {
Ed Tanous62598e32023-07-17 17:06:25 -0700480 BMCWEB_LOG_ERROR("Unexpected configuration");
Ed Tanous002d39b2022-05-31 08:59:27 -0700481 messages::internalError(asyncResp->res);
482 return;
483 }
James Feist5b4aa862018-08-16 14:07:01 -0700484
Ed Tanous002d39b2022-05-31 08:59:27 -0700485 // used for making maps out of 2 vectors
486 const std::vector<double>* keys = nullptr;
487 const std::vector<double>* values = nullptr;
488
489 for (const auto& propertyPair : intfPair.second)
490 {
491 if (propertyPair.first == "Type" ||
492 propertyPair.first == "Class" ||
493 propertyPair.first == "Name")
494 {
495 continue;
496 }
497
498 // zones
499 if (intfPair.first == pidZoneConfigurationIface)
500 {
501 const double* ptr =
502 std::get_if<double>(&propertyPair.second);
503 if (ptr == nullptr)
504 {
Ed Tanous62598e32023-07-17 17:06:25 -0700505 BMCWEB_LOG_ERROR("Field Illegal {}",
506 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700507 messages::internalError(asyncResp->res);
508 return;
509 }
510 (*config)[propertyPair.first] = *ptr;
511 }
512
513 if (intfPair.first == stepwiseConfigurationIface)
514 {
515 if (propertyPair.first == "Reading" ||
516 propertyPair.first == "Output")
517 {
518 const std::vector<double>* ptr =
519 std::get_if<std::vector<double>>(
520 &propertyPair.second);
521
522 if (ptr == nullptr)
523 {
Ed Tanous62598e32023-07-17 17:06:25 -0700524 BMCWEB_LOG_ERROR("Field Illegal {}",
525 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700526 messages::internalError(asyncResp->res);
527 return;
528 }
529
530 if (propertyPair.first == "Reading")
531 {
532 keys = ptr;
533 }
534 else
535 {
536 values = ptr;
537 }
538 if (keys != nullptr && values != nullptr)
539 {
540 if (keys->size() != values->size())
541 {
Ed Tanous62598e32023-07-17 17:06:25 -0700542 BMCWEB_LOG_ERROR(
543 "Reading and Output size don't match ");
Ed Tanous002d39b2022-05-31 08:59:27 -0700544 messages::internalError(asyncResp->res);
545 return;
546 }
547 nlohmann::json& steps = (*config)["Steps"];
548 steps = nlohmann::json::array();
549 for (size_t ii = 0; ii < keys->size(); ii++)
550 {
551 nlohmann::json::object_t step;
552 step["Target"] = (*keys)[ii];
553 step["Output"] = (*values)[ii];
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500554 steps.emplace_back(std::move(step));
Ed Tanous002d39b2022-05-31 08:59:27 -0700555 }
556 }
557 }
558 if (propertyPair.first == "NegativeHysteresis" ||
559 propertyPair.first == "PositiveHysteresis")
James Feist5b4aa862018-08-16 14:07:01 -0700560 {
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800561 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800562 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700563 if (ptr == nullptr)
564 {
Ed Tanous62598e32023-07-17 17:06:25 -0700565 BMCWEB_LOG_ERROR("Field Illegal {}",
566 propertyPair.first);
Jason M. Billsf12894f2018-10-09 12:45:45 -0700567 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700568 return;
569 }
James Feistb7a08d02018-12-11 14:55:37 -0800570 (*config)[propertyPair.first] = *ptr;
571 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700572 }
James Feistb7a08d02018-12-11 14:55:37 -0800573
Ed Tanous002d39b2022-05-31 08:59:27 -0700574 // pid and fans are off the same configuration
575 if (intfPair.first == pidConfigurationIface ||
576 intfPair.first == stepwiseConfigurationIface)
577 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700578 if (propertyPair.first == "Zones")
James Feistb7a08d02018-12-11 14:55:37 -0800579 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700580 const std::vector<std::string>* inputs =
581 std::get_if<std::vector<std::string>>(
582 &propertyPair.second);
583
584 if (inputs == nullptr)
James Feistb7a08d02018-12-11 14:55:37 -0800585 {
Ed Tanous62598e32023-07-17 17:06:25 -0700586 BMCWEB_LOG_ERROR("Zones Pid Field Illegal");
Ed Tanous002d39b2022-05-31 08:59:27 -0700587 messages::internalError(asyncResp->res);
588 return;
James Feistb7a08d02018-12-11 14:55:37 -0800589 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700590 auto& data = (*config)[propertyPair.first];
591 data = nlohmann::json::array();
592 for (std::string itemCopy : *inputs)
James Feistb7a08d02018-12-11 14:55:37 -0800593 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700594 dbus::utility::escapePathForDbus(itemCopy);
595 nlohmann::json::object_t input;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700596 boost::urls::url managerUrl = boost::urls::format(
597 "/redfish/v1/Managers/bmc#{}",
Willy Tueddfc432022-09-26 16:46:38 +0000598 ("/Oem/OpenBmc/Fan/FanZones"_json_pointer /
599 itemCopy)
600 .to_string());
601 input["@odata.id"] = std::move(managerUrl);
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500602 data.emplace_back(std::move(input));
James Feistb7a08d02018-12-11 14:55:37 -0800603 }
James Feist5b4aa862018-08-16 14:07:01 -0700604 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700605 // todo(james): may never happen, but this
606 // assumes configuration data referenced in the
607 // PID config is provided by the same daemon, we
608 // could add another loop to cover all cases,
609 // but I'm okay kicking this can down the road a
610 // bit
James Feist5b4aa862018-08-16 14:07:01 -0700611
Ed Tanous002d39b2022-05-31 08:59:27 -0700612 else if (propertyPair.first == "Inputs" ||
613 propertyPair.first == "Outputs")
James Feist5b4aa862018-08-16 14:07:01 -0700614 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700615 auto& data = (*config)[propertyPair.first];
616 const std::vector<std::string>* inputs =
617 std::get_if<std::vector<std::string>>(
618 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700619
Ed Tanous002d39b2022-05-31 08:59:27 -0700620 if (inputs == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700621 {
Ed Tanous62598e32023-07-17 17:06:25 -0700622 BMCWEB_LOG_ERROR("Field Illegal {}",
623 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700624 messages::internalError(asyncResp->res);
625 return;
James Feist5b4aa862018-08-16 14:07:01 -0700626 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700627 data = *inputs;
628 }
629 else if (propertyPair.first == "SetPointOffset")
630 {
631 const std::string* ptr =
632 std::get_if<std::string>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700633
Ed Tanous002d39b2022-05-31 08:59:27 -0700634 if (ptr == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700635 {
Ed Tanous62598e32023-07-17 17:06:25 -0700636 BMCWEB_LOG_ERROR("Field Illegal {}",
637 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700638 messages::internalError(asyncResp->res);
639 return;
James Feistb943aae2019-07-11 16:33:56 -0700640 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700641 // translate from dbus to redfish
642 if (*ptr == "WarningHigh")
James Feistb943aae2019-07-11 16:33:56 -0700643 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700644 (*config)["SetPointOffset"] =
645 "UpperThresholdNonCritical";
James Feistb943aae2019-07-11 16:33:56 -0700646 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700647 else if (*ptr == "WarningLow")
James Feist5b4aa862018-08-16 14:07:01 -0700648 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700649 (*config)["SetPointOffset"] =
650 "LowerThresholdNonCritical";
James Feist5b4aa862018-08-16 14:07:01 -0700651 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700652 else if (*ptr == "CriticalHigh")
653 {
654 (*config)["SetPointOffset"] =
655 "UpperThresholdCritical";
656 }
657 else if (*ptr == "CriticalLow")
658 {
659 (*config)["SetPointOffset"] =
660 "LowerThresholdCritical";
661 }
662 else
663 {
Ed Tanous62598e32023-07-17 17:06:25 -0700664 BMCWEB_LOG_ERROR("Value Illegal {}", *ptr);
Ed Tanous002d39b2022-05-31 08:59:27 -0700665 messages::internalError(asyncResp->res);
666 return;
667 }
668 }
669 // doubles
670 else if (propertyPair.first == "FFGainCoefficient" ||
671 propertyPair.first == "FFOffCoefficient" ||
672 propertyPair.first == "ICoefficient" ||
673 propertyPair.first == "ILimitMax" ||
674 propertyPair.first == "ILimitMin" ||
675 propertyPair.first == "PositiveHysteresis" ||
676 propertyPair.first == "NegativeHysteresis" ||
677 propertyPair.first == "OutLimitMax" ||
678 propertyPair.first == "OutLimitMin" ||
679 propertyPair.first == "PCoefficient" ||
680 propertyPair.first == "SetPoint" ||
681 propertyPair.first == "SlewNeg" ||
682 propertyPair.first == "SlewPos")
683 {
684 const double* ptr =
685 std::get_if<double>(&propertyPair.second);
686 if (ptr == nullptr)
687 {
Ed Tanous62598e32023-07-17 17:06:25 -0700688 BMCWEB_LOG_ERROR("Field Illegal {}",
689 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700690 messages::internalError(asyncResp->res);
691 return;
692 }
693 (*config)[propertyPair.first] = *ptr;
James Feist5b4aa862018-08-16 14:07:01 -0700694 }
695 }
696 }
697 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700698 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500699 });
James Feist5b4aa862018-08-16 14:07:01 -0700700}
Jennifer Leeca537922018-08-10 10:07:30 -0700701
James Feist83ff9ab2018-08-31 10:18:24 -0700702enum class CreatePIDRet
703{
704 fail,
705 del,
706 patch
707};
708
zhanghch058d1b46d2021-04-01 11:18:24 +0800709inline bool
710 getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
Ed Tanous9e9b6042024-03-06 14:18:28 -0800711 std::vector<nlohmann::json::object_t>& config,
zhanghch058d1b46d2021-04-01 11:18:24 +0800712 std::vector<std::string>& zones)
James Feist5f2caae2018-12-12 14:08:25 -0800713{
James Feistb6baeaa2019-02-21 10:41:40 -0800714 if (config.empty())
715 {
Ed Tanous62598e32023-07-17 17:06:25 -0700716 BMCWEB_LOG_ERROR("Empty Zones");
Ed Tanousf818b042022-06-27 13:17:35 -0700717 messages::propertyValueFormatError(response->res, config, "Zones");
James Feistb6baeaa2019-02-21 10:41:40 -0800718 return false;
719 }
James Feist5f2caae2018-12-12 14:08:25 -0800720 for (auto& odata : config)
721 {
722 std::string path;
Ed Tanous9e9b6042024-03-06 14:18:28 -0800723 if (!redfish::json_util::readJsonObject(odata, response->res,
724 "@odata.id", path))
James Feist5f2caae2018-12-12 14:08:25 -0800725 {
726 return false;
727 }
728 std::string input;
James Feist61adbda2019-03-25 13:03:51 -0700729
730 // 8 below comes from
731 // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
732 // 0 1 2 3 4 5 6 7 8
733 if (!dbus::utility::getNthStringFromPath(path, 8, input))
James Feist5f2caae2018-12-12 14:08:25 -0800734 {
Ed Tanous62598e32023-07-17 17:06:25 -0700735 BMCWEB_LOG_ERROR("Got invalid path {}", path);
736 BMCWEB_LOG_ERROR("Illegal Type Zones");
Ed Tanousf818b042022-06-27 13:17:35 -0700737 messages::propertyValueFormatError(response->res, odata, "Zones");
James Feist5f2caae2018-12-12 14:08:25 -0800738 return false;
739 }
Ed Tanousa170f272022-06-30 21:53:27 -0700740 std::replace(input.begin(), input.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -0800741 zones.emplace_back(std::move(input));
742 }
743 return true;
744}
745
Ed Tanous711ac7a2021-12-20 09:34:41 -0800746inline const dbus::utility::ManagedObjectType::value_type*
James Feist73df0db2019-03-25 15:29:35 -0700747 findChassis(const dbus::utility::ManagedObjectType& managedObj,
Ed Tanous9e9b6042024-03-06 14:18:28 -0800748 std::string_view value, std::string& chassis)
James Feistb6baeaa2019-02-21 10:41:40 -0800749{
Ed Tanous62598e32023-07-17 17:06:25 -0700750 BMCWEB_LOG_DEBUG("Find Chassis: {}", value);
James Feistb6baeaa2019-02-21 10:41:40 -0800751
Ed Tanous9e9b6042024-03-06 14:18:28 -0800752 std::string escaped(value);
Yaswanth Reddy M6ce82fa2023-03-10 07:29:45 +0000753 std::replace(escaped.begin(), escaped.end(), ' ', '_');
James Feistb6baeaa2019-02-21 10:41:40 -0800754 escaped = "/" + escaped;
Ed Tanous3544d2a2023-08-06 18:12:20 -0700755 auto it = std::ranges::find_if(managedObj, [&escaped](const auto& obj) {
Ed Tanous18f8f602023-07-18 10:07:23 -0700756 if (obj.first.str.ends_with(escaped))
Ed Tanous002d39b2022-05-31 08:59:27 -0700757 {
Ed Tanous62598e32023-07-17 17:06:25 -0700758 BMCWEB_LOG_DEBUG("Matched {}", obj.first.str);
Ed Tanous002d39b2022-05-31 08:59:27 -0700759 return true;
760 }
761 return false;
762 });
James Feistb6baeaa2019-02-21 10:41:40 -0800763
764 if (it == managedObj.end())
765 {
James Feist73df0db2019-03-25 15:29:35 -0700766 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800767 }
768 // 5 comes from <chassis-name> being the 5th element
769 // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
James Feist73df0db2019-03-25 15:29:35 -0700770 if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
771 {
772 return &(*it);
773 }
774
775 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800776}
777
Ed Tanous23a21a12020-07-25 04:45:05 +0000778inline CreatePIDRet createPidInterface(
zhanghch058d1b46d2021-04-01 11:18:24 +0800779 const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
Ed Tanous9e9b6042024-03-06 14:18:28 -0800780 std::string_view name, nlohmann::json& jsonValue, const std::string& path,
James Feist83ff9ab2018-08-31 10:18:24 -0700781 const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800782 dbus::utility::DBusPropertiesMap& output, std::string& chassis,
783 const std::string& profile)
James Feist83ff9ab2018-08-31 10:18:24 -0700784{
James Feist5f2caae2018-12-12 14:08:25 -0800785 // common deleter
Ed Tanous9e9b6042024-03-06 14:18:28 -0800786 if (jsonValue == nullptr)
James Feist5f2caae2018-12-12 14:08:25 -0800787 {
788 std::string iface;
789 if (type == "PidControllers" || type == "FanControllers")
790 {
791 iface = pidConfigurationIface;
792 }
793 else if (type == "FanZones")
794 {
795 iface = pidZoneConfigurationIface;
796 }
797 else if (type == "StepwiseControllers")
798 {
799 iface = stepwiseConfigurationIface;
800 }
801 else
802 {
Ed Tanous62598e32023-07-17 17:06:25 -0700803 BMCWEB_LOG_ERROR("Illegal Type {}", type);
James Feist5f2caae2018-12-12 14:08:25 -0800804 messages::propertyUnknown(response->res, type);
805 return CreatePIDRet::fail;
806 }
James Feist6ee7f772020-02-06 16:25:27 -0800807
Ed Tanous62598e32023-07-17 17:06:25 -0700808 BMCWEB_LOG_DEBUG("del {} {}", path, iface);
James Feist5f2caae2018-12-12 14:08:25 -0800809 // delete interface
810 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800811 [response, path](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700812 if (ec)
813 {
Ed Tanous62598e32023-07-17 17:06:25 -0700814 BMCWEB_LOG_ERROR("Error patching {}: {}", path, ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700815 messages::internalError(response->res);
816 return;
817 }
818 messages::success(response->res);
Patrick Williams5a39f772023-10-20 11:20:21 -0500819 },
James Feist5f2caae2018-12-12 14:08:25 -0800820 "xyz.openbmc_project.EntityManager", path, iface, "Delete");
821 return CreatePIDRet::del;
822 }
823
Ed Tanous711ac7a2021-12-20 09:34:41 -0800824 const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800825 if (!createNewObject)
826 {
827 // if we aren't creating a new object, we should be able to find it on
828 // d-bus
Ed Tanous9e9b6042024-03-06 14:18:28 -0800829 managedItem = findChassis(managedObj, name, chassis);
James Feist73df0db2019-03-25 15:29:35 -0700830 if (managedItem == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800831 {
Ed Tanous62598e32023-07-17 17:06:25 -0700832 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousef4c65b2023-04-24 15:28:50 -0700833 messages::invalidObject(
834 response->res,
835 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800836 return CreatePIDRet::fail;
837 }
838 }
839
Ed Tanous26f69762022-01-25 09:49:11 -0800840 if (!profile.empty() &&
James Feist73df0db2019-03-25 15:29:35 -0700841 (type == "PidControllers" || type == "FanControllers" ||
842 type == "StepwiseControllers"))
843 {
844 if (managedItem == nullptr)
845 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800846 output.emplace_back("Profiles", std::vector<std::string>{profile});
James Feist73df0db2019-03-25 15:29:35 -0700847 }
848 else
849 {
850 std::string interface;
851 if (type == "StepwiseControllers")
852 {
853 interface = stepwiseConfigurationIface;
854 }
855 else
856 {
857 interface = pidConfigurationIface;
858 }
Ed Tanous711ac7a2021-12-20 09:34:41 -0800859 bool ifaceFound = false;
860 for (const auto& iface : managedItem->second)
861 {
862 if (iface.first == interface)
863 {
864 ifaceFound = true;
865 for (const auto& prop : iface.second)
866 {
867 if (prop.first == "Profiles")
868 {
869 const std::vector<std::string>* curProfiles =
870 std::get_if<std::vector<std::string>>(
871 &(prop.second));
872 if (curProfiles == nullptr)
873 {
Ed Tanous62598e32023-07-17 17:06:25 -0700874 BMCWEB_LOG_ERROR(
875 "Illegal profiles in managed object");
Ed Tanous711ac7a2021-12-20 09:34:41 -0800876 messages::internalError(response->res);
877 return CreatePIDRet::fail;
878 }
879 if (std::find(curProfiles->begin(),
880 curProfiles->end(),
881 profile) == curProfiles->end())
882 {
883 std::vector<std::string> newProfiles =
884 *curProfiles;
885 newProfiles.push_back(profile);
Ed Tanousb9d36b42022-02-26 21:42:46 -0800886 output.emplace_back("Profiles", newProfiles);
Ed Tanous711ac7a2021-12-20 09:34:41 -0800887 }
888 }
889 }
890 }
891 }
892
893 if (!ifaceFound)
James Feist73df0db2019-03-25 15:29:35 -0700894 {
Ed Tanous62598e32023-07-17 17:06:25 -0700895 BMCWEB_LOG_ERROR("Failed to find interface in managed object");
James Feist73df0db2019-03-25 15:29:35 -0700896 messages::internalError(response->res);
897 return CreatePIDRet::fail;
898 }
James Feist73df0db2019-03-25 15:29:35 -0700899 }
900 }
901
James Feist83ff9ab2018-08-31 10:18:24 -0700902 if (type == "PidControllers" || type == "FanControllers")
903 {
904 if (createNewObject)
905 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800906 output.emplace_back("Class",
907 type == "PidControllers" ? "temp" : "fan");
908 output.emplace_back("Type", "Pid");
James Feist83ff9ab2018-08-31 10:18:24 -0700909 }
James Feist5f2caae2018-12-12 14:08:25 -0800910
Ed Tanous9e9b6042024-03-06 14:18:28 -0800911 std::optional<std::vector<nlohmann::json::object_t>> zones;
James Feist5f2caae2018-12-12 14:08:25 -0800912 std::optional<std::vector<std::string>> inputs;
913 std::optional<std::vector<std::string>> outputs;
914 std::map<std::string, std::optional<double>> doubles;
James Feistb943aae2019-07-11 16:33:56 -0700915 std::optional<std::string> setpointOffset;
James Feist5f2caae2018-12-12 14:08:25 -0800916 if (!redfish::json_util::readJson(
Ed Tanous9e9b6042024-03-06 14:18:28 -0800917 jsonValue, response->res, "Inputs", inputs, "Outputs", outputs,
James Feist5f2caae2018-12-12 14:08:25 -0800918 "Zones", zones, "FFGainCoefficient",
919 doubles["FFGainCoefficient"], "FFOffCoefficient",
920 doubles["FFOffCoefficient"], "ICoefficient",
921 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
922 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
923 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
924 "PCoefficient", doubles["PCoefficient"], "SetPoint",
James Feistb943aae2019-07-11 16:33:56 -0700925 doubles["SetPoint"], "SetPointOffset", setpointOffset,
926 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
927 "PositiveHysteresis", doubles["PositiveHysteresis"],
928 "NegativeHysteresis", doubles["NegativeHysteresis"]))
James Feist83ff9ab2018-08-31 10:18:24 -0700929 {
James Feist5f2caae2018-12-12 14:08:25 -0800930 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700931 }
James Feist5f2caae2018-12-12 14:08:25 -0800932 if (zones)
James Feist83ff9ab2018-08-31 10:18:24 -0700933 {
James Feist5f2caae2018-12-12 14:08:25 -0800934 std::vector<std::string> zonesStr;
935 if (!getZonesFromJsonReq(response, *zones, zonesStr))
James Feist83ff9ab2018-08-31 10:18:24 -0700936 {
Ed Tanous62598e32023-07-17 17:06:25 -0700937 BMCWEB_LOG_ERROR("Illegal Zones");
James Feist5f2caae2018-12-12 14:08:25 -0800938 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700939 }
James Feistb6baeaa2019-02-21 10:41:40 -0800940 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -0800941 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800942 {
Ed Tanous62598e32023-07-17 17:06:25 -0700943 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousace85d62021-10-26 12:45:59 -0700944 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -0700945 response->res,
946 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800947 return CreatePIDRet::fail;
948 }
Ed Tanousb9d36b42022-02-26 21:42:46 -0800949 output.emplace_back("Zones", std::move(zonesStr));
James Feist5f2caae2018-12-12 14:08:25 -0800950 }
Ed Tanousafb9ee02022-12-21 11:59:17 -0800951
952 if (inputs)
James Feist5f2caae2018-12-12 14:08:25 -0800953 {
Ed Tanousafb9ee02022-12-21 11:59:17 -0800954 for (std::string& value : *inputs)
James Feist83ff9ab2018-08-31 10:18:24 -0700955 {
Ed Tanousafb9ee02022-12-21 11:59:17 -0800956 std::replace(value.begin(), value.end(), '_', ' ');
James Feist83ff9ab2018-08-31 10:18:24 -0700957 }
Ed Tanousafb9ee02022-12-21 11:59:17 -0800958 output.emplace_back("Inputs", *inputs);
959 }
960
961 if (outputs)
962 {
963 for (std::string& value : *outputs)
964 {
965 std::replace(value.begin(), value.end(), '_', ' ');
966 }
967 output.emplace_back("Outputs", *outputs);
James Feist5f2caae2018-12-12 14:08:25 -0800968 }
James Feist83ff9ab2018-08-31 10:18:24 -0700969
James Feistb943aae2019-07-11 16:33:56 -0700970 if (setpointOffset)
971 {
972 // translate between redfish and dbus names
973 if (*setpointOffset == "UpperThresholdNonCritical")
974 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800975 output.emplace_back("SetPointOffset", "WarningLow");
James Feistb943aae2019-07-11 16:33:56 -0700976 }
977 else if (*setpointOffset == "LowerThresholdNonCritical")
978 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800979 output.emplace_back("SetPointOffset", "WarningHigh");
James Feistb943aae2019-07-11 16:33:56 -0700980 }
981 else if (*setpointOffset == "LowerThresholdCritical")
982 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800983 output.emplace_back("SetPointOffset", "CriticalLow");
James Feistb943aae2019-07-11 16:33:56 -0700984 }
985 else if (*setpointOffset == "UpperThresholdCritical")
986 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800987 output.emplace_back("SetPointOffset", "CriticalHigh");
James Feistb943aae2019-07-11 16:33:56 -0700988 }
989 else
990 {
Ed Tanous62598e32023-07-17 17:06:25 -0700991 BMCWEB_LOG_ERROR("Invalid setpointoffset {}", *setpointOffset);
Ed Tanous9e9b6042024-03-06 14:18:28 -0800992 messages::propertyValueNotInList(response->res, name,
Ed Tanousace85d62021-10-26 12:45:59 -0700993 "SetPointOffset");
James Feistb943aae2019-07-11 16:33:56 -0700994 return CreatePIDRet::fail;
995 }
996 }
997
James Feist5f2caae2018-12-12 14:08:25 -0800998 // doubles
999 for (const auto& pairs : doubles)
1000 {
1001 if (!pairs.second)
James Feist83ff9ab2018-08-31 10:18:24 -07001002 {
James Feist5f2caae2018-12-12 14:08:25 -08001003 continue;
James Feist83ff9ab2018-08-31 10:18:24 -07001004 }
Ed Tanous62598e32023-07-17 17:06:25 -07001005 BMCWEB_LOG_DEBUG("{} = {}", pairs.first, *pairs.second);
Ed Tanousb9d36b42022-02-26 21:42:46 -08001006 output.emplace_back(pairs.first, *pairs.second);
James Feist83ff9ab2018-08-31 10:18:24 -07001007 }
1008 }
James Feist5f2caae2018-12-12 14:08:25 -08001009
James Feist83ff9ab2018-08-31 10:18:24 -07001010 else if (type == "FanZones")
1011 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001012 output.emplace_back("Type", "Pid.Zone");
James Feist83ff9ab2018-08-31 10:18:24 -07001013
Ed Tanous9e9b6042024-03-06 14:18:28 -08001014 std::optional<std::string> chassisId;
James Feist5f2caae2018-12-12 14:08:25 -08001015 std::optional<double> failSafePercent;
James Feistd3ec07f2019-02-25 14:51:15 -08001016 std::optional<double> minThermalOutput;
Ed Tanous9e9b6042024-03-06 14:18:28 -08001017 if (!redfish::json_util::readJson(jsonValue, response->res,
1018 "Chassis/@odata.id", chassisId,
1019 "FailSafePercent", failSafePercent,
1020 "MinThermalOutput", minThermalOutput))
James Feist83ff9ab2018-08-31 10:18:24 -07001021 {
James Feist5f2caae2018-12-12 14:08:25 -08001022 return CreatePIDRet::fail;
1023 }
James Feist83ff9ab2018-08-31 10:18:24 -07001024
Ed Tanous9e9b6042024-03-06 14:18:28 -08001025 if (chassisId)
James Feist5f2caae2018-12-12 14:08:25 -08001026 {
AppaRao Puli717794d2019-10-18 22:54:53 +05301027 // /redfish/v1/chassis/chassis_name/
Ed Tanous9e9b6042024-03-06 14:18:28 -08001028 if (!dbus::utility::getNthStringFromPath(*chassisId, 3, chassis))
James Feist5f2caae2018-12-12 14:08:25 -08001029 {
Ed Tanous9e9b6042024-03-06 14:18:28 -08001030 BMCWEB_LOG_ERROR("Got invalid path {}", *chassisId);
Ed Tanousace85d62021-10-26 12:45:59 -07001031 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001032 response->res,
Ed Tanous9e9b6042024-03-06 14:18:28 -08001033 boost::urls::format("/redfish/v1/Chassis/{}", *chassisId));
James Feist5f2caae2018-12-12 14:08:25 -08001034 return CreatePIDRet::fail;
1035 }
1036 }
James Feistd3ec07f2019-02-25 14:51:15 -08001037 if (minThermalOutput)
James Feist5f2caae2018-12-12 14:08:25 -08001038 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001039 output.emplace_back("MinThermalOutput", *minThermalOutput);
James Feist5f2caae2018-12-12 14:08:25 -08001040 }
1041 if (failSafePercent)
1042 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001043 output.emplace_back("FailSafePercent", *failSafePercent);
James Feist5f2caae2018-12-12 14:08:25 -08001044 }
1045 }
1046 else if (type == "StepwiseControllers")
1047 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001048 output.emplace_back("Type", "Stepwise");
James Feist5f2caae2018-12-12 14:08:25 -08001049
Ed Tanous9e9b6042024-03-06 14:18:28 -08001050 std::optional<std::vector<nlohmann::json::object_t>> zones;
1051 std::optional<std::vector<nlohmann::json::object_t>> steps;
James Feist5f2caae2018-12-12 14:08:25 -08001052 std::optional<std::vector<std::string>> inputs;
1053 std::optional<double> positiveHysteresis;
1054 std::optional<double> negativeHysteresis;
James Feistc33a90e2019-03-01 10:17:44 -08001055 std::optional<std::string> direction; // upper clipping curve vs lower
James Feist5f2caae2018-12-12 14:08:25 -08001056 if (!redfish::json_util::readJson(
Ed Tanous9e9b6042024-03-06 14:18:28 -08001057 jsonValue, response->res, "Zones", zones, "Steps", steps,
James Feistb6baeaa2019-02-21 10:41:40 -08001058 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
James Feistc33a90e2019-03-01 10:17:44 -08001059 "NegativeHysteresis", negativeHysteresis, "Direction",
1060 direction))
James Feist5f2caae2018-12-12 14:08:25 -08001061 {
James Feist5f2caae2018-12-12 14:08:25 -08001062 return CreatePIDRet::fail;
1063 }
1064
1065 if (zones)
1066 {
James Feistb6baeaa2019-02-21 10:41:40 -08001067 std::vector<std::string> zonesStrs;
1068 if (!getZonesFromJsonReq(response, *zones, zonesStrs))
James Feist5f2caae2018-12-12 14:08:25 -08001069 {
Ed Tanous62598e32023-07-17 17:06:25 -07001070 BMCWEB_LOG_ERROR("Illegal Zones");
James Feist5f2caae2018-12-12 14:08:25 -08001071 return CreatePIDRet::fail;
1072 }
James Feistb6baeaa2019-02-21 10:41:40 -08001073 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -08001074 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -08001075 {
Ed Tanous62598e32023-07-17 17:06:25 -07001076 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousace85d62021-10-26 12:45:59 -07001077 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001078 response->res,
1079 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -08001080 return CreatePIDRet::fail;
1081 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001082 output.emplace_back("Zones", std::move(zonesStrs));
James Feist5f2caae2018-12-12 14:08:25 -08001083 }
1084 if (steps)
1085 {
1086 std::vector<double> readings;
1087 std::vector<double> outputs;
1088 for (auto& step : *steps)
1089 {
Ed Tanous543f4402022-01-06 13:12:53 -08001090 double target = 0.0;
1091 double out = 0.0;
James Feist5f2caae2018-12-12 14:08:25 -08001092
Ed Tanous9e9b6042024-03-06 14:18:28 -08001093 if (!redfish::json_util::readJsonObject(
1094 step, response->res, "Target", target, "Output", out))
James Feist5f2caae2018-12-12 14:08:25 -08001095 {
James Feist5f2caae2018-12-12 14:08:25 -08001096 return CreatePIDRet::fail;
1097 }
1098 readings.emplace_back(target);
Ed Tanous23a21a12020-07-25 04:45:05 +00001099 outputs.emplace_back(out);
James Feist5f2caae2018-12-12 14:08:25 -08001100 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001101 output.emplace_back("Reading", std::move(readings));
1102 output.emplace_back("Output", std::move(outputs));
James Feist5f2caae2018-12-12 14:08:25 -08001103 }
1104 if (inputs)
1105 {
1106 for (std::string& value : *inputs)
1107 {
Ed Tanousa170f272022-06-30 21:53:27 -07001108 std::replace(value.begin(), value.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -08001109 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001110 output.emplace_back("Inputs", std::move(*inputs));
James Feist5f2caae2018-12-12 14:08:25 -08001111 }
1112 if (negativeHysteresis)
1113 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001114 output.emplace_back("NegativeHysteresis", *negativeHysteresis);
James Feist5f2caae2018-12-12 14:08:25 -08001115 }
1116 if (positiveHysteresis)
1117 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001118 output.emplace_back("PositiveHysteresis", *positiveHysteresis);
James Feist83ff9ab2018-08-31 10:18:24 -07001119 }
James Feistc33a90e2019-03-01 10:17:44 -08001120 if (direction)
1121 {
1122 constexpr const std::array<const char*, 2> allowedDirections = {
1123 "Ceiling", "Floor"};
Ed Tanous3544d2a2023-08-06 18:12:20 -07001124 if (std::ranges::find(allowedDirections, *direction) ==
1125 allowedDirections.end())
James Feistc33a90e2019-03-01 10:17:44 -08001126 {
1127 messages::propertyValueTypeError(response->res, "Direction",
1128 *direction);
1129 return CreatePIDRet::fail;
1130 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001131 output.emplace_back("Class", *direction);
James Feistc33a90e2019-03-01 10:17:44 -08001132 }
James Feist83ff9ab2018-08-31 10:18:24 -07001133 }
1134 else
1135 {
Ed Tanous62598e32023-07-17 17:06:25 -07001136 BMCWEB_LOG_ERROR("Illegal Type {}", type);
Jason M. Bills35a62c72018-10-09 12:45:45 -07001137 messages::propertyUnknown(response->res, type);
James Feist83ff9ab2018-08-31 10:18:24 -07001138 return CreatePIDRet::fail;
1139 }
1140 return CreatePIDRet::patch;
1141}
James Feist73df0db2019-03-25 15:29:35 -07001142struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
1143{
Ed Tanous6936afe2022-09-08 15:10:39 -07001144 struct CompletionValues
1145 {
1146 std::vector<std::string> supportedProfiles;
1147 std::string currentProfile;
1148 dbus::utility::MapperGetSubTreeResponse subtree;
1149 };
James Feist73df0db2019-03-25 15:29:35 -07001150
Ed Tanous4e23a442022-06-06 09:57:26 -07001151 explicit GetPIDValues(
1152 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
Ed Tanous23a21a12020-07-25 04:45:05 +00001153 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001154
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001155 {}
James Feist73df0db2019-03-25 15:29:35 -07001156
1157 void run()
1158 {
1159 std::shared_ptr<GetPIDValues> self = shared_from_this();
1160
1161 // get all configurations
George Liue99073f2022-12-09 11:06:16 +08001162 constexpr std::array<std::string_view, 4> interfaces = {
1163 pidConfigurationIface, pidZoneConfigurationIface,
1164 objectManagerIface, stepwiseConfigurationIface};
1165 dbus::utility::getSubTree(
1166 "/", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001167 [self](
George Liue99073f2022-12-09 11:06:16 +08001168 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001169 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001170 if (ec)
1171 {
Ed Tanous62598e32023-07-17 17:06:25 -07001172 BMCWEB_LOG_ERROR("{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001173 messages::internalError(self->asyncResp->res);
1174 return;
1175 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001176 self->complete.subtree = subtreeLocal;
Patrick Williams5a39f772023-10-20 11:20:21 -05001177 });
James Feist73df0db2019-03-25 15:29:35 -07001178
1179 // at the same time get the selected profile
George Liue99073f2022-12-09 11:06:16 +08001180 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1181 thermalModeIface};
1182 dbus::utility::getSubTree(
1183 "/", 0, thermalModeIfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001184 [self](
George Liue99073f2022-12-09 11:06:16 +08001185 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001186 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001187 if (ec || subtreeLocal.empty())
1188 {
1189 return;
1190 }
1191 if (subtreeLocal[0].second.size() != 1)
1192 {
1193 // invalid mapper response, should never happen
Ed Tanous62598e32023-07-17 17:06:25 -07001194 BMCWEB_LOG_ERROR("GetPIDValues: Mapper Error");
Ed Tanous002d39b2022-05-31 08:59:27 -07001195 messages::internalError(self->asyncResp->res);
1196 return;
1197 }
1198
1199 const std::string& path = subtreeLocal[0].first;
1200 const std::string& owner = subtreeLocal[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001201
1202 sdbusplus::asio::getAllProperties(
1203 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous002d39b2022-05-31 08:59:27 -07001204 [path, owner,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001205 self](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001206 const dbus::utility::DBusPropertiesMap& resp) {
1207 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001208 {
Ed Tanous62598e32023-07-17 17:06:25 -07001209 BMCWEB_LOG_ERROR(
1210 "GetPIDValues: Can't get thermalModeIface {}", path);
James Feist73df0db2019-03-25 15:29:35 -07001211 messages::internalError(self->asyncResp->res);
1212 return;
1213 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001214
Ed Tanous002d39b2022-05-31 08:59:27 -07001215 const std::string* current = nullptr;
1216 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001217
1218 const bool success = sdbusplus::unpackPropertiesNoThrow(
1219 dbus_utils::UnpackErrorPrinter(), resp, "Current", current,
1220 "Supported", supported);
1221
1222 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001223 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001224 messages::internalError(self->asyncResp->res);
1225 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001226 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001227
Ed Tanous002d39b2022-05-31 08:59:27 -07001228 if (current == nullptr || supported == nullptr)
1229 {
Ed Tanous62598e32023-07-17 17:06:25 -07001230 BMCWEB_LOG_ERROR(
1231 "GetPIDValues: thermal mode iface invalid {}", path);
Ed Tanous002d39b2022-05-31 08:59:27 -07001232 messages::internalError(self->asyncResp->res);
1233 return;
1234 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001235 self->complete.currentProfile = *current;
1236 self->complete.supportedProfiles = *supported;
George Liue99073f2022-12-09 11:06:16 +08001237 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001238 });
James Feist73df0db2019-03-25 15:29:35 -07001239 }
1240
Ed Tanous6936afe2022-09-08 15:10:39 -07001241 static void
1242 processingComplete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1243 const CompletionValues& completion)
James Feist73df0db2019-03-25 15:29:35 -07001244 {
1245 if (asyncResp->res.result() != boost::beast::http::status::ok)
1246 {
1247 return;
1248 }
1249 // create map of <connection, path to objMgr>>
Ed Tanous6936afe2022-09-08 15:10:39 -07001250 boost::container::flat_map<
1251 std::string, std::string, std::less<>,
1252 std::vector<std::pair<std::string, std::string>>>
1253 objectMgrPaths;
1254 boost::container::flat_set<std::string, std::less<>,
1255 std::vector<std::string>>
1256 calledConnections;
1257 for (const auto& pathGroup : completion.subtree)
James Feist73df0db2019-03-25 15:29:35 -07001258 {
1259 for (const auto& connectionGroup : pathGroup.second)
1260 {
1261 auto findConnection =
1262 calledConnections.find(connectionGroup.first);
1263 if (findConnection != calledConnections.end())
1264 {
1265 break;
1266 }
1267 for (const std::string& interface : connectionGroup.second)
1268 {
1269 if (interface == objectManagerIface)
1270 {
1271 objectMgrPaths[connectionGroup.first] = pathGroup.first;
1272 }
1273 // this list is alphabetical, so we
1274 // should have found the objMgr by now
1275 if (interface == pidConfigurationIface ||
1276 interface == pidZoneConfigurationIface ||
1277 interface == stepwiseConfigurationIface)
1278 {
1279 auto findObjMgr =
1280 objectMgrPaths.find(connectionGroup.first);
1281 if (findObjMgr == objectMgrPaths.end())
1282 {
Ed Tanous62598e32023-07-17 17:06:25 -07001283 BMCWEB_LOG_DEBUG("{}Has no Object Manager",
1284 connectionGroup.first);
James Feist73df0db2019-03-25 15:29:35 -07001285 continue;
1286 }
1287
1288 calledConnections.insert(connectionGroup.first);
1289
1290 asyncPopulatePid(findObjMgr->first, findObjMgr->second,
Ed Tanous6936afe2022-09-08 15:10:39 -07001291 completion.currentProfile,
1292 completion.supportedProfiles,
James Feist73df0db2019-03-25 15:29:35 -07001293 asyncResp);
1294 break;
1295 }
1296 }
1297 }
1298 }
1299 }
1300
Ed Tanous6936afe2022-09-08 15:10:39 -07001301 ~GetPIDValues()
1302 {
1303 boost::asio::post(crow::connections::systemBus->get_io_context(),
1304 std::bind_front(&processingComplete, asyncResp,
1305 std::move(complete)));
1306 }
1307
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001308 GetPIDValues(const GetPIDValues&) = delete;
1309 GetPIDValues(GetPIDValues&&) = delete;
1310 GetPIDValues& operator=(const GetPIDValues&) = delete;
1311 GetPIDValues& operator=(GetPIDValues&&) = delete;
1312
zhanghch058d1b46d2021-04-01 11:18:24 +08001313 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Ed Tanous6936afe2022-09-08 15:10:39 -07001314 CompletionValues complete;
James Feist73df0db2019-03-25 15:29:35 -07001315};
1316
1317struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
1318{
Ed Tanous9e9b6042024-03-06 14:18:28 -08001319 SetPIDValues(
1320 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
1321 std::vector<
1322 std::pair<std::string, std::optional<nlohmann::json::object_t>>>&&
1323 configurationsIn,
1324 std::optional<std::string>& profileIn) :
1325 asyncResp(asyncRespIn),
1326 configuration(std::move(configurationsIn)),
1327 profile(std::move(profileIn))
1328 {}
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001329
1330 SetPIDValues(const SetPIDValues&) = delete;
1331 SetPIDValues(SetPIDValues&&) = delete;
1332 SetPIDValues& operator=(const SetPIDValues&) = delete;
1333 SetPIDValues& operator=(SetPIDValues&&) = delete;
1334
James Feist73df0db2019-03-25 15:29:35 -07001335 void run()
1336 {
1337 if (asyncResp->res.result() != boost::beast::http::status::ok)
1338 {
1339 return;
1340 }
1341
1342 std::shared_ptr<SetPIDValues> self = shared_from_this();
1343
1344 // todo(james): might make sense to do a mapper call here if this
1345 // interface gets more traction
George Liu5eb468d2023-06-20 17:03:24 +08001346 sdbusplus::message::object_path objPath(
1347 "/xyz/openbmc_project/inventory");
1348 dbus::utility::getManagedObjects(
1349 "xyz.openbmc_project.EntityManager", objPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001350 [self](const boost::system::error_code& ec,
Ed Tanous914e2d52022-01-07 11:38:34 -08001351 const dbus::utility::ManagedObjectType& mObj) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001352 if (ec)
1353 {
Ed Tanous62598e32023-07-17 17:06:25 -07001354 BMCWEB_LOG_ERROR("Error communicating to Entity Manager");
Ed Tanous002d39b2022-05-31 08:59:27 -07001355 messages::internalError(self->asyncResp->res);
1356 return;
1357 }
1358 const std::array<const char*, 3> configurations = {
1359 pidConfigurationIface, pidZoneConfigurationIface,
1360 stepwiseConfigurationIface};
James Feiste69d9de2020-02-07 12:23:27 -08001361
Ed Tanous002d39b2022-05-31 08:59:27 -07001362 for (const auto& [path, object] : mObj)
1363 {
1364 for (const auto& [interface, _] : object)
James Feiste69d9de2020-02-07 12:23:27 -08001365 {
Ed Tanous3544d2a2023-08-06 18:12:20 -07001366 if (std::ranges::find(configurations, interface) !=
1367 configurations.end())
James Feiste69d9de2020-02-07 12:23:27 -08001368 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001369 self->objectCount++;
1370 break;
James Feiste69d9de2020-02-07 12:23:27 -08001371 }
James Feiste69d9de2020-02-07 12:23:27 -08001372 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001373 }
1374 self->managedObj = mObj;
Patrick Williams5a39f772023-10-20 11:20:21 -05001375 });
James Feist73df0db2019-03-25 15:29:35 -07001376
1377 // at the same time get the profile information
George Liue99073f2022-12-09 11:06:16 +08001378 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1379 thermalModeIface};
1380 dbus::utility::getSubTree(
1381 "/", 0, thermalModeIfaces,
1382 [self](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001383 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001384 if (ec || subtree.empty())
1385 {
1386 return;
1387 }
1388 if (subtree[0].second.empty())
1389 {
1390 // invalid mapper response, should never happen
Ed Tanous62598e32023-07-17 17:06:25 -07001391 BMCWEB_LOG_ERROR("SetPIDValues: Mapper Error");
Ed Tanous002d39b2022-05-31 08:59:27 -07001392 messages::internalError(self->asyncResp->res);
1393 return;
1394 }
1395
1396 const std::string& path = subtree[0].first;
1397 const std::string& owner = subtree[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001398 sdbusplus::asio::getAllProperties(
1399 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001400 [self, path, owner](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001401 const dbus::utility::DBusPropertiesMap& r) {
1402 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001403 {
Ed Tanous62598e32023-07-17 17:06:25 -07001404 BMCWEB_LOG_ERROR(
1405 "SetPIDValues: Can't get thermalModeIface {}", path);
James Feist73df0db2019-03-25 15:29:35 -07001406 messages::internalError(self->asyncResp->res);
1407 return;
1408 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001409 const std::string* current = nullptr;
1410 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001411
1412 const bool success = sdbusplus::unpackPropertiesNoThrow(
1413 dbus_utils::UnpackErrorPrinter(), r, "Current", current,
1414 "Supported", supported);
1415
1416 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001417 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001418 messages::internalError(self->asyncResp->res);
1419 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001420 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001421
Ed Tanous002d39b2022-05-31 08:59:27 -07001422 if (current == nullptr || supported == nullptr)
1423 {
Ed Tanous62598e32023-07-17 17:06:25 -07001424 BMCWEB_LOG_ERROR(
1425 "SetPIDValues: thermal mode iface invalid {}", path);
Ed Tanous002d39b2022-05-31 08:59:27 -07001426 messages::internalError(self->asyncResp->res);
1427 return;
1428 }
1429 self->currentProfile = *current;
1430 self->supportedProfiles = *supported;
1431 self->profileConnection = owner;
1432 self->profilePath = path;
George Liue99073f2022-12-09 11:06:16 +08001433 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001434 });
James Feist73df0db2019-03-25 15:29:35 -07001435 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001436 void pidSetDone()
James Feist73df0db2019-03-25 15:29:35 -07001437 {
1438 if (asyncResp->res.result() != boost::beast::http::status::ok)
1439 {
1440 return;
1441 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001442 std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001443 if (profile)
1444 {
Ed Tanous3544d2a2023-08-06 18:12:20 -07001445 if (std::ranges::find(supportedProfiles, *profile) ==
1446 supportedProfiles.end())
James Feist73df0db2019-03-25 15:29:35 -07001447 {
1448 messages::actionParameterUnknown(response->res, "Profile",
1449 *profile);
1450 return;
1451 }
1452 currentProfile = *profile;
George Liu9ae226f2023-06-21 17:56:46 +08001453 sdbusplus::asio::setProperty(
1454 *crow::connections::systemBus, profileConnection, profilePath,
1455 thermalModeIface, "Current", *profile,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001456 [response](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001457 if (ec)
1458 {
Ed Tanous62598e32023-07-17 17:06:25 -07001459 BMCWEB_LOG_ERROR("Error patching profile{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001460 messages::internalError(response->res);
1461 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001462 });
James Feist73df0db2019-03-25 15:29:35 -07001463 }
1464
1465 for (auto& containerPair : configuration)
1466 {
1467 auto& container = containerPair.second;
1468 if (!container)
1469 {
1470 continue;
1471 }
James Feist6ee7f772020-02-06 16:25:27 -08001472
Ed Tanous02cad962022-06-30 16:50:15 -07001473 const std::string& type = containerPair.first;
James Feist73df0db2019-03-25 15:29:35 -07001474
Ed Tanous9e9b6042024-03-06 14:18:28 -08001475 for (auto& [name, value] : *container)
James Feist73df0db2019-03-25 15:29:35 -07001476 {
Potin Laicddbf3d2023-02-14 14:28:58 +08001477 std::string dbusObjName = name;
1478 std::replace(dbusObjName.begin(), dbusObjName.end(), ' ', '_');
Ed Tanous62598e32023-07-17 17:06:25 -07001479 BMCWEB_LOG_DEBUG("looking for {}", name);
James Feist6ee7f772020-02-06 16:25:27 -08001480
Ed Tanous3544d2a2023-08-06 18:12:20 -07001481 auto pathItr = std::ranges::find_if(
1482 managedObj, [&dbusObjName](const auto& obj) {
Ed Tanous18f8f602023-07-18 10:07:23 -07001483 return obj.first.parent_path() == dbusObjName;
Patrick Williams5a39f772023-10-20 11:20:21 -05001484 });
Ed Tanousb9d36b42022-02-26 21:42:46 -08001485 dbus::utility::DBusPropertiesMap output;
James Feist73df0db2019-03-25 15:29:35 -07001486
1487 output.reserve(16); // The pid interface length
1488
1489 // determines if we're patching entity-manager or
1490 // creating a new object
1491 bool createNewObject = (pathItr == managedObj.end());
Ed Tanous62598e32023-07-17 17:06:25 -07001492 BMCWEB_LOG_DEBUG("Found = {}", !createNewObject);
James Feist6ee7f772020-02-06 16:25:27 -08001493
James Feist73df0db2019-03-25 15:29:35 -07001494 std::string iface;
Ed Tanousea2b6702022-03-07 16:48:38 -08001495 if (!createNewObject)
James Feist73df0db2019-03-25 15:29:35 -07001496 {
Potin Lai8be2b5b2022-11-22 13:27:16 +08001497 bool findInterface = false;
Ed Tanousea2b6702022-03-07 16:48:38 -08001498 for (const auto& interface : pathItr->second)
James Feist73df0db2019-03-25 15:29:35 -07001499 {
Ed Tanousea2b6702022-03-07 16:48:38 -08001500 if (interface.first == pidConfigurationIface)
1501 {
1502 if (type == "PidControllers" ||
1503 type == "FanControllers")
1504 {
1505 iface = pidConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001506 findInterface = true;
1507 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001508 }
1509 }
1510 else if (interface.first == pidZoneConfigurationIface)
1511 {
1512 if (type == "FanZones")
1513 {
PavanKumarIntelda393502024-03-15 05:47:02 +00001514 iface = pidZoneConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001515 findInterface = true;
1516 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001517 }
1518 }
1519 else if (interface.first == stepwiseConfigurationIface)
1520 {
1521 if (type == "StepwiseControllers")
1522 {
1523 iface = stepwiseConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001524 findInterface = true;
1525 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001526 }
1527 }
James Feist73df0db2019-03-25 15:29:35 -07001528 }
Potin Lai8be2b5b2022-11-22 13:27:16 +08001529
1530 // create new object if interface not found
1531 if (!findInterface)
1532 {
1533 createNewObject = true;
1534 }
James Feist73df0db2019-03-25 15:29:35 -07001535 }
James Feist6ee7f772020-02-06 16:25:27 -08001536
Ed Tanous9e9b6042024-03-06 14:18:28 -08001537 if (createNewObject && value == nullptr)
James Feist6ee7f772020-02-06 16:25:27 -08001538 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -05001539 // can't delete a non-existent object
Ed Tanous9e9b6042024-03-06 14:18:28 -08001540 messages::propertyValueNotInList(response->res, value,
Ed Tanouse2616cc2022-06-27 12:45:55 -07001541 name);
James Feist6ee7f772020-02-06 16:25:27 -08001542 continue;
1543 }
1544
1545 std::string path;
1546 if (pathItr != managedObj.end())
1547 {
1548 path = pathItr->first.str;
1549 }
1550
Ed Tanous62598e32023-07-17 17:06:25 -07001551 BMCWEB_LOG_DEBUG("Create new = {}", createNewObject);
James Feiste69d9de2020-02-07 12:23:27 -08001552
1553 // arbitrary limit to avoid attacks
1554 constexpr const size_t controllerLimit = 500;
James Feist14b0b8d2020-02-12 11:52:07 -08001555 if (createNewObject && objectCount >= controllerLimit)
James Feiste69d9de2020-02-07 12:23:27 -08001556 {
1557 messages::resourceExhaustion(response->res, type);
1558 continue;
1559 }
Ed Tanousa170f272022-06-30 21:53:27 -07001560 std::string escaped = name;
1561 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1562 output.emplace_back("Name", escaped);
James Feist73df0db2019-03-25 15:29:35 -07001563
1564 std::string chassis;
1565 CreatePIDRet ret = createPidInterface(
Ed Tanous9e9b6042024-03-06 14:18:28 -08001566 response, type, name, value, path, managedObj,
1567 createNewObject, output, chassis, currentProfile);
James Feist73df0db2019-03-25 15:29:35 -07001568 if (ret == CreatePIDRet::fail)
1569 {
1570 return;
1571 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001572 if (ret == CreatePIDRet::del)
James Feist73df0db2019-03-25 15:29:35 -07001573 {
1574 continue;
1575 }
1576
1577 if (!createNewObject)
1578 {
1579 for (const auto& property : output)
1580 {
Potin Lai7a696972023-11-09 12:18:20 +08001581 crow::connections::systemBus->async_method_call(
James Feist73df0db2019-03-25 15:29:35 -07001582 [response,
1583 propertyName{std::string(property.first)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001584 const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001585 if (ec)
1586 {
Ed Tanous62598e32023-07-17 17:06:25 -07001587 BMCWEB_LOG_ERROR("Error patching {}: {}",
1588 propertyName, ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001589 messages::internalError(response->res);
1590 return;
1591 }
1592 messages::success(response->res);
Potin Lai7a696972023-11-09 12:18:20 +08001593 },
1594 "xyz.openbmc_project.EntityManager", path,
1595 "org.freedesktop.DBus.Properties", "Set", iface,
1596 property.first, property.second);
James Feist73df0db2019-03-25 15:29:35 -07001597 }
1598 }
1599 else
1600 {
1601 if (chassis.empty())
1602 {
Ed Tanous62598e32023-07-17 17:06:25 -07001603 BMCWEB_LOG_ERROR("Failed to get chassis from config");
Ed Tanousace85d62021-10-26 12:45:59 -07001604 messages::internalError(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001605 return;
1606 }
1607
1608 bool foundChassis = false;
1609 for (const auto& obj : managedObj)
1610 {
Ed Tanous18f8f602023-07-18 10:07:23 -07001611 if (obj.first.parent_path() == chassis)
James Feist73df0db2019-03-25 15:29:35 -07001612 {
1613 chassis = obj.first.str;
1614 foundChassis = true;
1615 break;
1616 }
1617 }
1618 if (!foundChassis)
1619 {
Ed Tanous62598e32023-07-17 17:06:25 -07001620 BMCWEB_LOG_ERROR("Failed to find chassis on dbus");
James Feist73df0db2019-03-25 15:29:35 -07001621 messages::resourceMissingAtURI(
Ed Tanousace85d62021-10-26 12:45:59 -07001622 response->res,
Ed Tanousef4c65b2023-04-24 15:28:50 -07001623 boost::urls::format("/redfish/v1/Chassis/{}",
1624 chassis));
James Feist73df0db2019-03-25 15:29:35 -07001625 return;
1626 }
1627
1628 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001629 [response](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001630 if (ec)
1631 {
Ed Tanous62598e32023-07-17 17:06:25 -07001632 BMCWEB_LOG_ERROR("Error Adding Pid Object {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001633 messages::internalError(response->res);
1634 return;
1635 }
1636 messages::success(response->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05001637 },
James Feist73df0db2019-03-25 15:29:35 -07001638 "xyz.openbmc_project.EntityManager", chassis,
1639 "xyz.openbmc_project.AddObject", "AddObject", output);
1640 }
1641 }
1642 }
1643 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001644
1645 ~SetPIDValues()
1646 {
1647 try
1648 {
1649 pidSetDone();
1650 }
1651 catch (...)
1652 {
Ed Tanous62598e32023-07-17 17:06:25 -07001653 BMCWEB_LOG_CRITICAL("pidSetDone threw exception");
Ed Tanous24b2fe82022-01-06 12:45:54 -08001654 }
1655 }
1656
zhanghch058d1b46d2021-04-01 11:18:24 +08001657 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Ed Tanous9e9b6042024-03-06 14:18:28 -08001658 std::vector<std::pair<std::string, std::optional<nlohmann::json::object_t>>>
James Feist73df0db2019-03-25 15:29:35 -07001659 configuration;
1660 std::optional<std::string> profile;
1661 dbus::utility::ManagedObjectType managedObj;
1662 std::vector<std::string> supportedProfiles;
1663 std::string currentProfile;
1664 std::string profileConnection;
1665 std::string profilePath;
James Feist14b0b8d2020-02-12 11:52:07 -08001666 size_t objectCount = 0;
James Feist73df0db2019-03-25 15:29:35 -07001667};
James Feist83ff9ab2018-08-31 10:18:24 -07001668
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001669/**
1670 * @brief Retrieves BMC manager location data over DBus
1671 *
Ed Tanousac106bf2023-06-07 09:24:59 -07001672 * @param[in] asyncResp Shared pointer for completing asynchronous calls
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001673 * @param[in] connectionName - service name
1674 * @param[in] path - object path
1675 * @return none
1676 */
Ed Tanousac106bf2023-06-07 09:24:59 -07001677inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001678 const std::string& connectionName,
1679 const std::string& path)
1680{
Ed Tanous62598e32023-07-17 17:06:25 -07001681 BMCWEB_LOG_DEBUG("Get BMC manager Location data.");
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001682
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001683 sdbusplus::asio::getProperty<std::string>(
1684 *crow::connections::systemBus, connectionName, path,
1685 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Ed Tanousac106bf2023-06-07 09:24:59 -07001686 [asyncResp](const boost::system::error_code& ec,
1687 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001688 if (ec)
1689 {
Ed Tanous62598e32023-07-17 17:06:25 -07001690 BMCWEB_LOG_DEBUG("DBUS response error for "
1691 "Location");
Ed Tanousac106bf2023-06-07 09:24:59 -07001692 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001693 return;
1694 }
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001695
Ed Tanousac106bf2023-06-07 09:24:59 -07001696 asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001697 property;
Patrick Williams5a39f772023-10-20 11:20:21 -05001698 });
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001699}
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001700// avoid name collision systems.hpp
1701inline void
Ed Tanousac106bf2023-06-07 09:24:59 -07001702 managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001703{
Ed Tanous62598e32023-07-17 17:06:25 -07001704 BMCWEB_LOG_DEBUG("Getting Manager Last Reset Time");
Ed Tanous52cc1122020-07-18 13:51:21 -07001705
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001706 sdbusplus::asio::getProperty<uint64_t>(
1707 *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
1708 "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
1709 "LastRebootTime",
Ed Tanousac106bf2023-06-07 09:24:59 -07001710 [asyncResp](const boost::system::error_code& ec,
1711 const uint64_t lastResetTime) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001712 if (ec)
1713 {
Ed Tanous62598e32023-07-17 17:06:25 -07001714 BMCWEB_LOG_DEBUG("D-BUS response error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001715 return;
1716 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001717
Ed Tanous002d39b2022-05-31 08:59:27 -07001718 // LastRebootTime is epoch time, in milliseconds
1719 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
1720 uint64_t lastResetTimeStamp = lastResetTime / 1000;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001721
Ed Tanous002d39b2022-05-31 08:59:27 -07001722 // Convert to ISO 8601 standard
Ed Tanousac106bf2023-06-07 09:24:59 -07001723 asyncResp->res.jsonValue["LastResetTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001724 redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
Patrick Williams5a39f772023-10-20 11:20:21 -05001725 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001726}
1727
1728/**
1729 * @brief Set the running firmware image
1730 *
Ed Tanousac106bf2023-06-07 09:24:59 -07001731 * @param[i,o] asyncResp - Async response object
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001732 * @param[i] runningFirmwareTarget - Image to make the running image
1733 *
1734 * @return void
1735 */
1736inline void
Ed Tanousac106bf2023-06-07 09:24:59 -07001737 setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001738 const std::string& runningFirmwareTarget)
1739{
1740 // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1741 std::string::size_type idPos = runningFirmwareTarget.rfind('/');
1742 if (idPos == std::string::npos)
1743 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001744 messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001745 "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001746 BMCWEB_LOG_DEBUG("Can't parse firmware ID!");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001747 return;
1748 }
1749 idPos++;
1750 if (idPos >= runningFirmwareTarget.size())
1751 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001752 messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001753 "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001754 BMCWEB_LOG_DEBUG("Invalid firmware ID.");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001755 return;
1756 }
1757 std::string firmwareId = runningFirmwareTarget.substr(idPos);
1758
1759 // Make sure the image is valid before setting priority
George Liu5eb468d2023-06-20 17:03:24 +08001760 sdbusplus::message::object_path objPath("/xyz/openbmc_project/software");
1761 dbus::utility::getManagedObjects(
1762 "xyz.openbmc_project.Software.BMC.Updater", objPath,
1763 [asyncResp, firmwareId, runningFirmwareTarget](
1764 const boost::system::error_code& ec,
1765 const dbus::utility::ManagedObjectType& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001766 if (ec)
1767 {
Ed Tanous62598e32023-07-17 17:06:25 -07001768 BMCWEB_LOG_DEBUG("D-Bus response error getting objects.");
Ed Tanousac106bf2023-06-07 09:24:59 -07001769 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001770 return;
1771 }
1772
1773 if (subtree.empty())
1774 {
Ed Tanous62598e32023-07-17 17:06:25 -07001775 BMCWEB_LOG_DEBUG("Can't find image!");
Ed Tanousac106bf2023-06-07 09:24:59 -07001776 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001777 return;
1778 }
1779
1780 bool foundImage = false;
Ed Tanous02cad962022-06-30 16:50:15 -07001781 for (const auto& object : subtree)
Ed Tanous002d39b2022-05-31 08:59:27 -07001782 {
1783 const std::string& path =
1784 static_cast<const std::string&>(object.first);
1785 std::size_t idPos2 = path.rfind('/');
1786
1787 if (idPos2 == std::string::npos)
1788 {
1789 continue;
1790 }
1791
1792 idPos2++;
1793 if (idPos2 >= path.size())
1794 {
1795 continue;
1796 }
1797
1798 if (path.substr(idPos2) == firmwareId)
1799 {
1800 foundImage = true;
1801 break;
1802 }
1803 }
1804
1805 if (!foundImage)
1806 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001807 messages::propertyValueNotInList(
1808 asyncResp->res, runningFirmwareTarget, "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001809 BMCWEB_LOG_DEBUG("Invalid firmware ID.");
Ed Tanous002d39b2022-05-31 08:59:27 -07001810 return;
1811 }
1812
Ed Tanous62598e32023-07-17 17:06:25 -07001813 BMCWEB_LOG_DEBUG("Setting firmware version {} to priority 0.",
1814 firmwareId);
Ed Tanous002d39b2022-05-31 08:59:27 -07001815
1816 // Only support Immediate
1817 // An addition could be a Redfish Setting like
1818 // ActiveSoftwareImageApplyTime and support OnReset
George Liu9ae226f2023-06-21 17:56:46 +08001819 sdbusplus::asio::setProperty(
1820 *crow::connections::systemBus,
1821 "xyz.openbmc_project.Software.BMC.Updater",
1822 "/xyz/openbmc_project/software/" + firmwareId,
1823 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1824 static_cast<uint8_t>(0),
Ed Tanousac106bf2023-06-07 09:24:59 -07001825 [asyncResp](const boost::system::error_code& ec2) {
Ed Tanous8a592812022-06-04 09:06:59 -07001826 if (ec2)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001827 {
Ed Tanous62598e32023-07-17 17:06:25 -07001828 BMCWEB_LOG_DEBUG("D-Bus response error setting.");
Ed Tanousac106bf2023-06-07 09:24:59 -07001829 messages::internalError(asyncResp->res);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001830 return;
1831 }
Ed Tanousac106bf2023-06-07 09:24:59 -07001832 doBMCGracefulRestart(asyncResp);
George Liu5eb468d2023-06-20 17:03:24 +08001833 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001834 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001835}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001836
Ed Tanousc51afd52024-03-07 10:13:14 -08001837inline void
1838 afterSetDateTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1839 const boost::system::error_code& ec,
1840 const sdbusplus::message_t& msg)
1841{
1842 if (ec)
1843 {
1844 BMCWEB_LOG_DEBUG("Failed to set elapsed time. DBUS response error {}",
1845 ec);
1846 const sd_bus_error* dbusError = msg.get_error();
1847 if (dbusError != nullptr)
1848 {
1849 std::string_view errorName(dbusError->name);
1850 if (errorName ==
1851 "org.freedesktop.timedate1.AutomaticTimeSyncEnabled")
1852 {
1853 BMCWEB_LOG_DEBUG("Setting conflict");
1854 messages::propertyValueConflict(
1855 asyncResp->res, "DateTime",
1856 "Managers/NetworkProtocol/NTPProcotolEnabled");
1857 return;
1858 }
1859 }
1860 messages::internalError(asyncResp->res);
1861 return;
1862 }
1863 asyncResp->res.result(boost::beast::http::status::no_content);
1864}
1865
1866inline void setDateTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1867 const std::string& datetime)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001868{
Ed Tanous62598e32023-07-17 17:06:25 -07001869 BMCWEB_LOG_DEBUG("Set date time: {}", datetime);
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001870
Ed Tanousc2e32002023-01-07 22:05:08 -08001871 std::optional<redfish::time_utils::usSinceEpoch> us =
1872 redfish::time_utils::dateStringToEpoch(datetime);
1873 if (!us)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001874 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001875 messages::propertyValueFormatError(asyncResp->res, datetime,
1876 "DateTime");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001877 return;
1878 }
Ed Tanousc51afd52024-03-07 10:13:14 -08001879 // Set the absolute datetime
1880 bool relative = false;
1881 bool interactive = false;
1882 crow::connections::systemBus->async_method_call(
1883 [asyncResp](const boost::system::error_code& ec,
1884 const sdbusplus::message_t& msg) {
1885 afterSetDateTime(asyncResp, ec, msg);
1886 },
1887 "org.freedesktop.timedate1", "/org/freedesktop/timedate1",
1888 "org.freedesktop.timedate1", "SetTime", us->count(), relative,
1889 interactive);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001890}
1891
Ed Tanous75815e52022-10-05 17:21:13 -07001892inline void
1893 checkForQuiesced(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1894{
1895 sdbusplus::asio::getProperty<std::string>(
1896 *crow::connections::systemBus, "org.freedesktop.systemd1",
1897 "/org/freedesktop/systemd1/unit/obmc-bmc-service-quiesce@0.target",
1898 "org.freedesktop.systemd1.Unit", "ActiveState",
1899 [asyncResp](const boost::system::error_code& ec,
1900 const std::string& val) {
1901 if (!ec)
1902 {
1903 if (val == "active")
1904 {
1905 asyncResp->res.jsonValue["Status"]["Health"] = "Critical";
1906 asyncResp->res.jsonValue["Status"]["State"] = "Quiesced";
1907 return;
1908 }
1909 }
1910 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
1911 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
Patrick Williams5a39f772023-10-20 11:20:21 -05001912 });
Ed Tanous75815e52022-10-05 17:21:13 -07001913}
1914
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001915inline void requestRoutesManager(App& app)
1916{
1917 std::string uuid = persistent_data::getConfig().systemUuid;
1918
1919 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07001920 .privileges(redfish::privileges::getManager)
Ed Tanous002d39b2022-05-31 08:59:27 -07001921 .methods(boost::beast::http::verb::get)(
1922 [&app, uuid](const crow::Request& req,
Ed Tanous45ca1b82022-03-25 13:07:27 -07001923 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001924 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001925 {
1926 return;
1927 }
1928 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
Sui Chena51fc2d2022-07-14 17:21:53 -07001929 asyncResp->res.jsonValue["@odata.type"] = "#Manager.v1_14_0.Manager";
Ed Tanous002d39b2022-05-31 08:59:27 -07001930 asyncResp->res.jsonValue["Id"] = "bmc";
1931 asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
1932 asyncResp->res.jsonValue["Description"] =
1933 "Baseboard Management Controller";
1934 asyncResp->res.jsonValue["PowerState"] = "On";
Ed Tanous14766872022-03-15 10:44:42 -07001935
Ed Tanous002d39b2022-05-31 08:59:27 -07001936 asyncResp->res.jsonValue["ManagerType"] = "BMC";
1937 asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
1938 asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1939 asyncResp->res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001940
Ed Tanous002d39b2022-05-31 08:59:27 -07001941 asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
1942 "/redfish/v1/Managers/bmc/LogServices";
1943 asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
1944 "/redfish/v1/Managers/bmc/NetworkProtocol";
1945 asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
1946 "/redfish/v1/Managers/bmc/EthernetInterfaces";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001947
Ed Tanous36c0f2a2024-02-09 13:50:26 -08001948 if constexpr (bmcwebNbdProxy)
1949 {
1950 asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
1951 "/redfish/v1/Managers/bmc/VirtualMedia";
1952 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001953
Ed Tanous002d39b2022-05-31 08:59:27 -07001954 // default oem data
1955 nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
1956 nlohmann::json& oemOpenbmc = oem["OpenBmc"];
1957 oem["@odata.type"] = "#OemManager.Oem";
1958 oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
1959 oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
1960 oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
Ed Tanous14766872022-03-15 10:44:42 -07001961
Ed Tanous002d39b2022-05-31 08:59:27 -07001962 nlohmann::json::object_t certificates;
1963 certificates["@odata.id"] =
1964 "/redfish/v1/Managers/bmc/Truststore/Certificates";
1965 oemOpenbmc["Certificates"] = std::move(certificates);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001966
Ed Tanous002d39b2022-05-31 08:59:27 -07001967 // Manager.Reset (an action) can be many values, OpenBMC only
1968 // supports BMC reboot.
1969 nlohmann::json& managerReset =
1970 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
1971 managerReset["target"] =
1972 "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
1973 managerReset["@Redfish.ActionInfo"] =
1974 "/redfish/v1/Managers/bmc/ResetActionInfo";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001975
Ed Tanous002d39b2022-05-31 08:59:27 -07001976 // ResetToDefaults (Factory Reset) has values like
1977 // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
1978 // on OpenBMC
1979 nlohmann::json& resetToDefaults =
1980 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
1981 resetToDefaults["target"] =
1982 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
Ed Tanous613dabe2022-07-09 11:17:36 -07001983 resetToDefaults["ResetType@Redfish.AllowableValues"] =
1984 nlohmann::json::array_t({"ResetAll"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001985
Ed Tanous002d39b2022-05-31 08:59:27 -07001986 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001987 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301988
Ed Tanous002d39b2022-05-31 08:59:27 -07001989 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1990 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1991 redfishDateTimeOffset.second;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001992
Ed Tanous002d39b2022-05-31 08:59:27 -07001993 // TODO (Gunnar): Remove these one day since moved to ComputerSystem
1994 // Still used by OCP profiles
1995 // https://github.com/opencomputeproject/OCP-Profiles/issues/23
1996 // Fill in SerialConsole info
1997 asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
1998 asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
Ed Tanous613dabe2022-07-09 11:17:36 -07001999 asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
2000 nlohmann::json::array_t({"IPMI", "SSH"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002001#ifdef BMCWEB_ENABLE_KVM
Ed Tanous002d39b2022-05-31 08:59:27 -07002002 // Fill in GraphicalConsole info
2003 asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
2004 asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] =
2005 4;
Ed Tanous613dabe2022-07-09 11:17:36 -07002006 asyncResp->res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
2007 nlohmann::json::array_t({"KVMIP"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002008#endif // BMCWEB_ENABLE_KVM
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002009 if constexpr (!bmcwebEnableMultiHost)
2010 {
2011 asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] =
2012 1;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002013
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002014 nlohmann::json::array_t managerForServers;
2015 nlohmann::json::object_t manager;
2016 manager["@odata.id"] = "/redfish/v1/Systems/system";
2017 managerForServers.emplace_back(std::move(manager));
Ed Tanous14766872022-03-15 10:44:42 -07002018
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002019 asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
2020 std::move(managerForServers);
2021 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002022
Willy Tueee00132022-06-14 14:53:17 -07002023 sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
Ed Tanous002d39b2022-05-31 08:59:27 -07002024 "FirmwareVersion", true);
2025
2026 managerGetLastResetTime(asyncResp);
2027
Sui Chena51fc2d2022-07-14 17:21:53 -07002028 // ManagerDiagnosticData is added for all BMCs.
2029 nlohmann::json& managerDiagnosticData =
2030 asyncResp->res.jsonValue["ManagerDiagnosticData"];
2031 managerDiagnosticData["@odata.id"] =
2032 "/redfish/v1/Managers/bmc/ManagerDiagnosticData";
2033
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002034#ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
Ed Tanous002d39b2022-05-31 08:59:27 -07002035 auto pids = std::make_shared<GetPIDValues>(asyncResp);
2036 pids->run();
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002037#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07002038
2039 getMainChassisId(asyncResp,
2040 [](const std::string& chassisId,
2041 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2042 aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
2043 nlohmann::json::array_t managerForChassis;
Ed Tanous8a592812022-06-04 09:06:59 -07002044 nlohmann::json::object_t managerObj;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002045 boost::urls::url chassiUrl =
2046 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
Willy Tueddfc432022-09-26 16:46:38 +00002047 managerObj["@odata.id"] = chassiUrl;
Patrick Williamsad539542023-05-12 10:10:08 -05002048 managerForChassis.emplace_back(std::move(managerObj));
Ed Tanous002d39b2022-05-31 08:59:27 -07002049 aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
2050 std::move(managerForChassis);
2051 aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00002052 chassiUrl;
Ed Tanous002d39b2022-05-31 08:59:27 -07002053 });
Ed Tanous14766872022-03-15 10:44:42 -07002054
Ed Tanous75815e52022-10-05 17:21:13 -07002055 sdbusplus::asio::getProperty<double>(
2056 *crow::connections::systemBus, "org.freedesktop.systemd1",
2057 "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
2058 "Progress",
2059 [asyncResp](const boost::system::error_code& ec, double val) {
2060 if (ec)
2061 {
Ed Tanous62598e32023-07-17 17:06:25 -07002062 BMCWEB_LOG_ERROR("Error while getting progress");
Ed Tanous75815e52022-10-05 17:21:13 -07002063 messages::internalError(asyncResp->res);
2064 return;
2065 }
2066 if (val < 1.0)
2067 {
2068 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
2069 asyncResp->res.jsonValue["Status"]["State"] = "Starting";
2070 return;
2071 }
2072 checkForQuiesced(asyncResp);
Patrick Williams5a39f772023-10-20 11:20:21 -05002073 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002074
George Liue99073f2022-12-09 11:06:16 +08002075 constexpr std::array<std::string_view, 1> interfaces = {
2076 "xyz.openbmc_project.Inventory.Item.Bmc"};
2077 dbus::utility::getSubTree(
2078 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07002079 [asyncResp](
George Liue99073f2022-12-09 11:06:16 +08002080 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07002081 const dbus::utility::MapperGetSubTreeResponse& subtree) {
2082 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002083 {
Ed Tanous62598e32023-07-17 17:06:25 -07002084 BMCWEB_LOG_DEBUG("D-Bus response error on GetSubTree {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002085 return;
2086 }
2087 if (subtree.empty())
2088 {
Ed Tanous62598e32023-07-17 17:06:25 -07002089 BMCWEB_LOG_DEBUG("Can't find bmc D-Bus object!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002090 return;
2091 }
2092 // Assume only 1 bmc D-Bus object
2093 // Throw an error if there is more than 1
2094 if (subtree.size() > 1)
2095 {
Ed Tanous62598e32023-07-17 17:06:25 -07002096 BMCWEB_LOG_DEBUG("Found more than 1 bmc D-Bus object!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002097 messages::internalError(asyncResp->res);
2098 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002099 }
2100
Ed Tanous002d39b2022-05-31 08:59:27 -07002101 if (subtree[0].first.empty() || subtree[0].second.size() != 1)
2102 {
Ed Tanous62598e32023-07-17 17:06:25 -07002103 BMCWEB_LOG_DEBUG("Error getting bmc D-Bus object!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002104 messages::internalError(asyncResp->res);
2105 return;
2106 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002107
Ed Tanous002d39b2022-05-31 08:59:27 -07002108 const std::string& path = subtree[0].first;
2109 const std::string& connectionName = subtree[0].second[0].first;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002110
Ed Tanous002d39b2022-05-31 08:59:27 -07002111 for (const auto& interfaceName : subtree[0].second[0].second)
2112 {
2113 if (interfaceName ==
2114 "xyz.openbmc_project.Inventory.Decorator.Asset")
2115 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002116 sdbusplus::asio::getAllProperties(
2117 *crow::connections::systemBus, connectionName, path,
2118 "xyz.openbmc_project.Inventory.Decorator.Asset",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002119 [asyncResp](const boost::system::error_code& ec2,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002120 const dbus::utility::DBusPropertiesMap&
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002121 propertiesList) {
Ed Tanous8a592812022-06-04 09:06:59 -07002122 if (ec2)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002123 {
Ed Tanous62598e32023-07-17 17:06:25 -07002124 BMCWEB_LOG_DEBUG("Can't get bmc asset!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002125 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002126 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002127
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002128 const std::string* partNumber = nullptr;
2129 const std::string* serialNumber = nullptr;
2130 const std::string* manufacturer = nullptr;
2131 const std::string* model = nullptr;
2132 const std::string* sparePartNumber = nullptr;
2133
2134 const bool success = sdbusplus::unpackPropertiesNoThrow(
2135 dbus_utils::UnpackErrorPrinter(), propertiesList,
2136 "PartNumber", partNumber, "SerialNumber",
2137 serialNumber, "Manufacturer", manufacturer, "Model",
2138 model, "SparePartNumber", sparePartNumber);
2139
2140 if (!success)
2141 {
2142 messages::internalError(asyncResp->res);
2143 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07002144 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002145
2146 if (partNumber != nullptr)
2147 {
2148 asyncResp->res.jsonValue["PartNumber"] =
2149 *partNumber;
2150 }
2151
2152 if (serialNumber != nullptr)
2153 {
2154 asyncResp->res.jsonValue["SerialNumber"] =
2155 *serialNumber;
2156 }
2157
2158 if (manufacturer != nullptr)
2159 {
2160 asyncResp->res.jsonValue["Manufacturer"] =
2161 *manufacturer;
2162 }
2163
2164 if (model != nullptr)
2165 {
2166 asyncResp->res.jsonValue["Model"] = *model;
2167 }
2168
2169 if (sparePartNumber != nullptr)
2170 {
2171 asyncResp->res.jsonValue["SparePartNumber"] =
2172 *sparePartNumber;
2173 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002174 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002175 }
2176 else if (interfaceName ==
2177 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
2178 {
2179 getLocation(asyncResp, connectionName, path);
2180 }
2181 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002182 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002183 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002184
2185 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07002186 .privileges(redfish::privileges::patchManager)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002187 .methods(boost::beast::http::verb::patch)(
2188 [&app](const crow::Request& req,
2189 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002190 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002191 {
2192 return;
2193 }
Ed Tanous9e9b6042024-03-06 14:18:28 -08002194 std::optional<std::string> activeSoftwareImageOdataId;
Ed Tanous002d39b2022-05-31 08:59:27 -07002195 std::optional<std::string> datetime;
Ed Tanous9e9b6042024-03-06 14:18:28 -08002196 std::optional<nlohmann::json::object_t> pidControllers;
2197 std::optional<nlohmann::json::object_t> fanControllers;
2198 std::optional<nlohmann::json::object_t> fanZones;
2199 std::optional<nlohmann::json::object_t> stepwiseControllers;
2200 std::optional<std::string> profile;
Ed Tanous002d39b2022-05-31 08:59:27 -07002201
Ed Tanous9e9b6042024-03-06 14:18:28 -08002202 // clang-format off
2203 if (!json_util::readJsonPatch(req, asyncResp->res,
2204 "DateTime", datetime,
2205 "Links/ActiveSoftwareImage/@odata.id", activeSoftwareImageOdataId,
2206 "Oem/OpenBmc/Fan/FanControllers", fanControllers,
2207 "Oem/OpenBmc/Fan/FanZones", fanZones,
2208 "Oem/OpenBmc/Fan/PidControllers", pidControllers,
2209 "Oem/OpenBmc/Fan/Profile", profile,
2210 "Oem/OpenBmc/Fan/StepwiseControllers", stepwiseControllers
2211 ))
Ed Tanous002d39b2022-05-31 08:59:27 -07002212 {
2213 return;
2214 }
Ed Tanous9e9b6042024-03-06 14:18:28 -08002215 // clang-format on
Ed Tanous002d39b2022-05-31 08:59:27 -07002216
Ed Tanous9e9b6042024-03-06 14:18:28 -08002217 if (pidControllers || fanControllers || fanZones ||
2218 stepwiseControllers || profile)
Ed Tanous002d39b2022-05-31 08:59:27 -07002219 {
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002220#ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
Ed Tanous9e9b6042024-03-06 14:18:28 -08002221 std::vector<
2222 std::pair<std::string, std::optional<nlohmann::json::object_t>>>
2223 configuration;
2224 if (pidControllers)
Ed Tanous002d39b2022-05-31 08:59:27 -07002225 {
Ed Tanous9e9b6042024-03-06 14:18:28 -08002226 configuration.emplace_back("PidControllers",
2227 std::move(pidControllers));
Ed Tanous002d39b2022-05-31 08:59:27 -07002228 }
Ed Tanous9e9b6042024-03-06 14:18:28 -08002229 if (fanControllers)
Ed Tanous002d39b2022-05-31 08:59:27 -07002230 {
Ed Tanous9e9b6042024-03-06 14:18:28 -08002231 configuration.emplace_back("FanControllers",
2232 std::move(fanControllers));
Ed Tanous002d39b2022-05-31 08:59:27 -07002233 }
Ed Tanous9e9b6042024-03-06 14:18:28 -08002234 if (fanZones)
2235 {
2236 configuration.emplace_back("FanZones", std::move(fanZones));
2237 }
2238 if (stepwiseControllers)
2239 {
2240 configuration.emplace_back("StepwiseControllers",
2241 std::move(stepwiseControllers));
2242 }
2243 auto pid = std::make_shared<SetPIDValues>(
2244 asyncResp, std::move(configuration), profile);
2245 pid->run();
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002246#else
2247 messages::propertyUnknown(asyncResp->res, "Oem");
2248 return;
2249#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07002250 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07002251
Ed Tanous9e9b6042024-03-06 14:18:28 -08002252 if (activeSoftwareImageOdataId)
2253 {
2254 setActiveFirmwareImage(asyncResp, *activeSoftwareImageOdataId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002255 }
Ed Tanous9e9b6042024-03-06 14:18:28 -08002256
Ed Tanous002d39b2022-05-31 08:59:27 -07002257 if (datetime)
2258 {
Ed Tanousc51afd52024-03-07 10:13:14 -08002259 setDateTime(asyncResp, *datetime);
Ed Tanous002d39b2022-05-31 08:59:27 -07002260 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002261 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002262}
2263
2264inline void requestRoutesManagerCollection(App& app)
2265{
2266 BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
Ed Tanoused398212021-06-09 17:05:54 -07002267 .privileges(redfish::privileges::getManagerCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002268 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002269 [&app](const crow::Request& req,
2270 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002271 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002272 {
2273 return;
2274 }
2275 // Collections don't include the static data added by SubRoute
2276 // because it has a duplicate entry for members
2277 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
2278 asyncResp->res.jsonValue["@odata.type"] =
2279 "#ManagerCollection.ManagerCollection";
2280 asyncResp->res.jsonValue["Name"] = "Manager Collection";
2281 asyncResp->res.jsonValue["Members@odata.count"] = 1;
2282 nlohmann::json::array_t members;
2283 nlohmann::json& bmc = members.emplace_back();
2284 bmc["@odata.id"] = "/redfish/v1/Managers/bmc";
2285 asyncResp->res.jsonValue["Members"] = std::move(members);
Patrick Williams5a39f772023-10-20 11:20:21 -05002286 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002287}
Ed Tanous1abe55e2018-09-05 08:30:59 -07002288} // namespace redfish