blob: c61132f2151431640afc999bfb14e09888ca76e3 [file] [log] [blame]
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Willy Tu13451e32023-05-24 16:08:18 -070018#include "bmcweb_config.h"
19
Sui Chena51fc2d2022-07-14 17:21:53 -070020#include "app.hpp"
21#include "dbus_utility.hpp"
James Feistb49ac872019-05-21 15:12:01 -070022#include "health.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070023#include "query.hpp"
Jennifer Leec5d03ff2019-03-08 15:42:58 -080024#include "redfish_util.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070025#include "registries/privilege_registry.hpp"
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +020026#include "utils/dbus_utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080027#include "utils/json_utils.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070028#include "utils/sw_utils.hpp"
29#include "utils/systemd_utils.hpp"
Ed Tanous2b829372022-08-03 14:22:34 -070030#include "utils/time_utils.hpp"
Borawski.Lukasz9c3106852018-02-09 15:24:22 +010031
George Liue99073f2022-12-09 11:06:16 +080032#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070033#include <boost/url/format.hpp>
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +020034#include <sdbusplus/asio/property.hpp>
35#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050036
Ed Tanousa170f272022-06-30 21:53:27 -070037#include <algorithm>
George Liue99073f2022-12-09 11:06:16 +080038#include <array>
Gunnar Mills4bfefa72020-07-30 13:54:29 -050039#include <cstdint>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050040#include <memory>
Ed Tanous3544d2a2023-08-06 18:12:20 -070041#include <ranges>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050042#include <sstream>
George Liue99073f2022-12-09 11:06:16 +080043#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080044#include <variant>
James Feist5b4aa862018-08-16 14:07:01 -070045
Ed Tanous1abe55e2018-09-05 08:30:59 -070046namespace redfish
47{
Jennifer Leeed5befb2018-08-10 11:29:45 -070048
49/**
Gunnar Mills2a5c4402020-05-19 09:07:24 -050050 * Function reboots the BMC.
51 *
52 * @param[in] asyncResp - Shared pointer for completing asynchronous calls
Jennifer Leeed5befb2018-08-10 11:29:45 -070053 */
zhanghch058d1b46d2021-04-01 11:18:24 +080054inline void
55 doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Gunnar Mills2a5c4402020-05-19 09:07:24 -050056{
57 const char* processName = "xyz.openbmc_project.State.BMC";
58 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
59 const char* interfaceName = "xyz.openbmc_project.State.BMC";
60 const std::string& propertyValue =
61 "xyz.openbmc_project.State.BMC.Transition.Reboot";
62 const char* destProperty = "RequestedBMCTransition";
63
64 // Create the D-Bus variant for D-Bus call.
George Liu9ae226f2023-06-21 17:56:46 +080065 sdbusplus::asio::setProperty(
66 *crow::connections::systemBus, processName, objectPath, interfaceName,
67 destProperty, propertyValue,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080068 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -070069 // Use "Set" method to set the property value.
70 if (ec)
71 {
Ed Tanous62598e32023-07-17 17:06:25 -070072 BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -070073 messages::internalError(asyncResp->res);
74 return;
75 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -050076
Ed Tanous002d39b2022-05-31 08:59:27 -070077 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -050078 });
Gunnar Mills2a5c4402020-05-19 09:07:24 -050079}
80
zhanghch058d1b46d2021-04-01 11:18:24 +080081inline void
82 doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +000083{
84 const char* processName = "xyz.openbmc_project.State.BMC";
85 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
86 const char* interfaceName = "xyz.openbmc_project.State.BMC";
87 const std::string& propertyValue =
88 "xyz.openbmc_project.State.BMC.Transition.HardReboot";
89 const char* destProperty = "RequestedBMCTransition";
90
91 // Create the D-Bus variant for D-Bus call.
George Liu9ae226f2023-06-21 17:56:46 +080092 sdbusplus::asio::setProperty(
93 *crow::connections::systemBus, processName, objectPath, interfaceName,
94 destProperty, propertyValue,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080095 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -070096 // Use "Set" method to set the property value.
97 if (ec)
98 {
Ed Tanous62598e32023-07-17 17:06:25 -070099 BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700100 messages::internalError(asyncResp->res);
101 return;
102 }
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000103
Ed Tanous002d39b2022-05-31 08:59:27 -0700104 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -0500105 });
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000106}
107
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500108/**
109 * ManagerResetAction class supports the POST method for the Reset (reboot)
110 * action.
111 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700112inline void requestRoutesManagerResetAction(App& app)
Jennifer Leeed5befb2018-08-10 11:29:45 -0700113{
Jennifer Leeed5befb2018-08-10 11:29:45 -0700114 /**
Jennifer Leeed5befb2018-08-10 11:29:45 -0700115 * Function handles POST method request.
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500116 * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000117 * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
Jennifer Leeed5befb2018-08-10 11:29:45 -0700118 */
Jennifer Leeed5befb2018-08-10 11:29:45 -0700119
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700120 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
Ed Tanoused398212021-06-09 17:05:54 -0700121 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700122 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700123 [&app](const crow::Request& req,
124 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000125 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700126 {
127 return;
128 }
Ed Tanous62598e32023-07-17 17:06:25 -0700129 BMCWEB_LOG_DEBUG("Post Manager Reset.");
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500130
Ed Tanous002d39b2022-05-31 08:59:27 -0700131 std::string resetType;
Jennifer Leeed5befb2018-08-10 11:29:45 -0700132
Ed Tanous002d39b2022-05-31 08:59:27 -0700133 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
134 resetType))
135 {
136 return;
137 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500138
Ed Tanous002d39b2022-05-31 08:59:27 -0700139 if (resetType == "GracefulRestart")
140 {
Ed Tanous62598e32023-07-17 17:06:25 -0700141 BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
Ed Tanous002d39b2022-05-31 08:59:27 -0700142 doBMCGracefulRestart(asyncResp);
143 return;
144 }
145 if (resetType == "ForceRestart")
146 {
Ed Tanous62598e32023-07-17 17:06:25 -0700147 BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
Ed Tanous002d39b2022-05-31 08:59:27 -0700148 doBMCForceRestart(asyncResp);
149 return;
150 }
Ed Tanous62598e32023-07-17 17:06:25 -0700151 BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}", resetType);
Ed Tanous002d39b2022-05-31 08:59:27 -0700152 messages::actionParameterNotSupported(asyncResp->res, resetType,
153 "ResetType");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700154
Ed Tanous002d39b2022-05-31 08:59:27 -0700155 return;
Patrick Williams5a39f772023-10-20 11:20:21 -0500156 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700157}
Jennifer Leeed5befb2018-08-10 11:29:45 -0700158
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500159/**
160 * ManagerResetToDefaultsAction class supports POST method for factory reset
161 * action.
162 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700163inline void requestRoutesManagerResetToDefaultsAction(App& app)
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500164{
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500165 /**
166 * Function handles ResetToDefaults POST method request.
167 *
168 * Analyzes POST body message and factory resets BMC by calling
169 * BMC code updater factory reset followed by a BMC reboot.
170 *
171 * BMC code updater factory reset wipes the whole BMC read-write
172 * filesystem which includes things like the network settings.
173 *
174 * OpenBMC only supports ResetToDefaultsType "ResetAll".
175 */
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500176
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700177 BMCWEB_ROUTE(app,
178 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/")
Ed Tanoused398212021-06-09 17:05:54 -0700179 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700180 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700181 [&app](const crow::Request& req,
182 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000183 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700184 {
185 return;
186 }
Ed Tanous62598e32023-07-17 17:06:25 -0700187 BMCWEB_LOG_DEBUG("Post ResetToDefaults.");
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500188
Ed Tanous002d39b2022-05-31 08:59:27 -0700189 std::string resetType;
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500190
Ed Tanous002d39b2022-05-31 08:59:27 -0700191 if (!json_util::readJsonAction(req, asyncResp->res,
192 "ResetToDefaultsType", resetType))
193 {
Ed Tanous62598e32023-07-17 17:06:25 -0700194 BMCWEB_LOG_DEBUG("Missing property ResetToDefaultsType.");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700195
Ed Tanous002d39b2022-05-31 08:59:27 -0700196 messages::actionParameterMissing(asyncResp->res, "ResetToDefaults",
197 "ResetToDefaultsType");
198 return;
199 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700200
Ed Tanous002d39b2022-05-31 08:59:27 -0700201 if (resetType != "ResetAll")
202 {
Ed Tanous62598e32023-07-17 17:06:25 -0700203 BMCWEB_LOG_DEBUG(
204 "Invalid property value for ResetToDefaultsType: {}",
205 resetType);
Ed Tanous002d39b2022-05-31 08:59:27 -0700206 messages::actionParameterNotSupported(asyncResp->res, resetType,
207 "ResetToDefaultsType");
208 return;
209 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700210
Ed Tanous002d39b2022-05-31 08:59:27 -0700211 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800212 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700213 if (ec)
214 {
Ed Tanous62598e32023-07-17 17:06:25 -0700215 BMCWEB_LOG_DEBUG("Failed to ResetToDefaults: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700216 messages::internalError(asyncResp->res);
217 return;
218 }
219 // Factory Reset doesn't actually happen until a reboot
220 // Can't erase what the BMC is running on
221 doBMCGracefulRestart(asyncResp);
Patrick Williams5a39f772023-10-20 11:20:21 -0500222 },
Ed Tanous002d39b2022-05-31 08:59:27 -0700223 "xyz.openbmc_project.Software.BMC.Updater",
224 "/xyz/openbmc_project/software",
225 "xyz.openbmc_project.Common.FactoryReset", "Reset");
Patrick Williams5a39f772023-10-20 11:20:21 -0500226 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700227}
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500228
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530229/**
230 * ManagerResetActionInfo derived class for delivering Manager
231 * ResetType AllowableValues using ResetInfo schema.
232 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700233inline void requestRoutesManagerResetActionInfo(App& app)
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530234{
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530235 /**
236 * Functions triggers appropriate requests on DBus
237 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700238
239 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/ResetActionInfo/")
Ed Tanoused398212021-06-09 17:05:54 -0700240 .privileges(redfish::privileges::getActionInfo)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700241 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700242 [&app](const crow::Request& req,
243 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000244 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700245 {
246 return;
247 }
Ed Tanous14766872022-03-15 10:44:42 -0700248
Ed Tanous002d39b2022-05-31 08:59:27 -0700249 asyncResp->res.jsonValue["@odata.type"] =
250 "#ActionInfo.v1_1_2.ActionInfo";
251 asyncResp->res.jsonValue["@odata.id"] =
252 "/redfish/v1/Managers/bmc/ResetActionInfo";
253 asyncResp->res.jsonValue["Name"] = "Reset Action Info";
254 asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
255 nlohmann::json::object_t parameter;
256 parameter["Name"] = "ResetType";
257 parameter["Required"] = true;
258 parameter["DataType"] = "String";
Ed Tanous14766872022-03-15 10:44:42 -0700259
Ed Tanous002d39b2022-05-31 08:59:27 -0700260 nlohmann::json::array_t allowableValues;
Patrick Williamsad539542023-05-12 10:10:08 -0500261 allowableValues.emplace_back("GracefulRestart");
262 allowableValues.emplace_back("ForceRestart");
Ed Tanous002d39b2022-05-31 08:59:27 -0700263 parameter["AllowableValues"] = std::move(allowableValues);
Ed Tanous14766872022-03-15 10:44:42 -0700264
Ed Tanous002d39b2022-05-31 08:59:27 -0700265 nlohmann::json::array_t parameters;
Patrick Williamsad539542023-05-12 10:10:08 -0500266 parameters.emplace_back(std::move(parameter));
Ed Tanous14766872022-03-15 10:44:42 -0700267
Ed Tanous002d39b2022-05-31 08:59:27 -0700268 asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
Patrick Williams5a39f772023-10-20 11:20:21 -0500269 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700270}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530271
James Feist5b4aa862018-08-16 14:07:01 -0700272static constexpr const char* objectManagerIface =
273 "org.freedesktop.DBus.ObjectManager";
274static constexpr const char* pidConfigurationIface =
275 "xyz.openbmc_project.Configuration.Pid";
276static constexpr const char* pidZoneConfigurationIface =
277 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feistb7a08d02018-12-11 14:55:37 -0800278static constexpr const char* stepwiseConfigurationIface =
279 "xyz.openbmc_project.Configuration.Stepwise";
James Feist73df0db2019-03-25 15:29:35 -0700280static constexpr const char* thermalModeIface =
281 "xyz.openbmc_project.Control.ThermalMode";
Borawski.Lukasz9c3106852018-02-09 15:24:22 +0100282
zhanghch058d1b46d2021-04-01 11:18:24 +0800283inline void
284 asyncPopulatePid(const std::string& connection, const std::string& path,
285 const std::string& currentProfile,
286 const std::vector<std::string>& supportedProfiles,
287 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
James Feist5b4aa862018-08-16 14:07:01 -0700288{
George Liu5eb468d2023-06-20 17:03:24 +0800289 sdbusplus::message::object_path objPath(path);
290 dbus::utility::getManagedObjects(
291 connection, objPath,
James Feist73df0db2019-03-25 15:29:35 -0700292 [asyncResp, currentProfile, supportedProfiles](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800293 const boost::system::error_code& ec,
James Feist73df0db2019-03-25 15:29:35 -0700294 const dbus::utility::ManagedObjectType& managedObj) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700295 if (ec)
296 {
Ed Tanous62598e32023-07-17 17:06:25 -0700297 BMCWEB_LOG_ERROR("{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700298 messages::internalError(asyncResp->res);
299 return;
300 }
301 nlohmann::json& configRoot =
302 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
303 nlohmann::json& fans = configRoot["FanControllers"];
304 fans["@odata.type"] = "#OemManager.FanControllers";
305 fans["@odata.id"] =
306 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers";
307
308 nlohmann::json& pids = configRoot["PidControllers"];
309 pids["@odata.type"] = "#OemManager.PidControllers";
310 pids["@odata.id"] =
311 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
312
313 nlohmann::json& stepwise = configRoot["StepwiseControllers"];
314 stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
315 stepwise["@odata.id"] =
316 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
317
318 nlohmann::json& zones = configRoot["FanZones"];
319 zones["@odata.id"] =
320 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
321 zones["@odata.type"] = "#OemManager.FanZones";
322 configRoot["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
323 configRoot["@odata.type"] = "#OemManager.Fan";
324 configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
325
326 if (!currentProfile.empty())
327 {
328 configRoot["Profile"] = currentProfile;
329 }
Carson Labradobf2dded2023-08-10 00:37:06 +0000330 BMCWEB_LOG_DEBUG("profile = {} !", currentProfile);
Ed Tanous002d39b2022-05-31 08:59:27 -0700331
332 for (const auto& pathPair : managedObj)
333 {
334 for (const auto& intfPair : pathPair.second)
James Feist5b4aa862018-08-16 14:07:01 -0700335 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700336 if (intfPair.first != pidConfigurationIface &&
337 intfPair.first != pidZoneConfigurationIface &&
338 intfPair.first != stepwiseConfigurationIface)
James Feist5b4aa862018-08-16 14:07:01 -0700339 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700340 continue;
341 }
James Feist73df0db2019-03-25 15:29:35 -0700342
Ed Tanous002d39b2022-05-31 08:59:27 -0700343 std::string name;
James Feist73df0db2019-03-25 15:29:35 -0700344
Ed Tanous002d39b2022-05-31 08:59:27 -0700345 for (const std::pair<std::string,
346 dbus::utility::DbusVariantType>& propPair :
347 intfPair.second)
348 {
349 if (propPair.first == "Name")
James Feist73df0db2019-03-25 15:29:35 -0700350 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700351 const std::string* namePtr =
352 std::get_if<std::string>(&propPair.second);
353 if (namePtr == nullptr)
James Feist73df0db2019-03-25 15:29:35 -0700354 {
Ed Tanous62598e32023-07-17 17:06:25 -0700355 BMCWEB_LOG_ERROR("Pid Name Field illegal");
James Feistc33a90e2019-03-01 10:17:44 -0800356 messages::internalError(asyncResp->res);
357 return;
358 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700359 name = *namePtr;
360 dbus::utility::escapePathForDbus(name);
James Feistb7a08d02018-12-11 14:55:37 -0800361 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700362 else if (propPair.first == "Profiles")
James Feistb7a08d02018-12-11 14:55:37 -0800363 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700364 const std::vector<std::string>* profiles =
365 std::get_if<std::vector<std::string>>(
366 &propPair.second);
367 if (profiles == nullptr)
James Feistb7a08d02018-12-11 14:55:37 -0800368 {
Ed Tanous62598e32023-07-17 17:06:25 -0700369 BMCWEB_LOG_ERROR("Pid Profiles Field illegal");
James Feistb7a08d02018-12-11 14:55:37 -0800370 messages::internalError(asyncResp->res);
371 return;
372 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700373 if (std::find(profiles->begin(), profiles->end(),
374 currentProfile) == profiles->end())
James Feistb7a08d02018-12-11 14:55:37 -0800375 {
Ed Tanous62598e32023-07-17 17:06:25 -0700376 BMCWEB_LOG_INFO(
377 "{} not supported in current profile", name);
Ed Tanous002d39b2022-05-31 08:59:27 -0700378 continue;
James Feistb7a08d02018-12-11 14:55:37 -0800379 }
380 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700381 }
382 nlohmann::json* config = nullptr;
383 const std::string* classPtr = nullptr;
384
385 for (const std::pair<std::string,
386 dbus::utility::DbusVariantType>& propPair :
387 intfPair.second)
388 {
389 if (propPair.first == "Class")
James Feistb7a08d02018-12-11 14:55:37 -0800390 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700391 classPtr = std::get_if<std::string>(&propPair.second);
392 }
393 }
394
Ed Tanousef4c65b2023-04-24 15:28:50 -0700395 boost::urls::url url("/redfish/v1/Managers/bmc");
Ed Tanous002d39b2022-05-31 08:59:27 -0700396 if (intfPair.first == pidZoneConfigurationIface)
397 {
398 std::string chassis;
399 if (!dbus::utility::getNthStringFromPath(pathPair.first.str,
400 5, chassis))
401 {
402 chassis = "#IllegalValue";
403 }
404 nlohmann::json& zone = zones[name];
Ed Tanousef4c65b2023-04-24 15:28:50 -0700405 zone["Chassis"]["@odata.id"] =
406 boost::urls::format("/redfish/v1/Chassis/{}", chassis);
Willy Tueddfc432022-09-26 16:46:38 +0000407 url.set_fragment(
408 ("/Oem/OpenBmc/Fan/FanZones"_json_pointer / name)
409 .to_string());
410 zone["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700411 zone["@odata.type"] = "#OemManager.FanZone";
412 config = &zone;
413 }
414
415 else if (intfPair.first == stepwiseConfigurationIface)
416 {
417 if (classPtr == nullptr)
418 {
Ed Tanous62598e32023-07-17 17:06:25 -0700419 BMCWEB_LOG_ERROR("Pid Class Field illegal");
James Feistb7a08d02018-12-11 14:55:37 -0800420 messages::internalError(asyncResp->res);
421 return;
422 }
423
Ed Tanous002d39b2022-05-31 08:59:27 -0700424 nlohmann::json& controller = stepwise[name];
425 config = &controller;
Willy Tueddfc432022-09-26 16:46:38 +0000426 url.set_fragment(
427 ("/Oem/OpenBmc/Fan/StepwiseControllers"_json_pointer /
428 name)
429 .to_string());
430 controller["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700431 controller["@odata.type"] =
432 "#OemManager.StepwiseController";
433
434 controller["Direction"] = *classPtr;
435 }
436
437 // pid and fans are off the same configuration
438 else if (intfPair.first == pidConfigurationIface)
439 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700440 if (classPtr == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700441 {
Ed Tanous62598e32023-07-17 17:06:25 -0700442 BMCWEB_LOG_ERROR("Pid Class Field illegal");
Ed Tanous002d39b2022-05-31 08:59:27 -0700443 messages::internalError(asyncResp->res);
444 return;
445 }
446 bool isFan = *classPtr == "fan";
447 nlohmann::json& element = isFan ? fans[name] : pids[name];
448 config = &element;
449 if (isFan)
450 {
Willy Tueddfc432022-09-26 16:46:38 +0000451 url.set_fragment(
452 ("/Oem/OpenBmc/Fan/FanControllers"_json_pointer /
453 name)
454 .to_string());
455 element["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700456 element["@odata.type"] = "#OemManager.FanController";
457 }
458 else
459 {
Willy Tueddfc432022-09-26 16:46:38 +0000460 url.set_fragment(
461 ("/Oem/OpenBmc/Fan/PidControllers"_json_pointer /
462 name)
463 .to_string());
464 element["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700465 element["@odata.type"] = "#OemManager.PidController";
466 }
467 }
468 else
469 {
Ed Tanous62598e32023-07-17 17:06:25 -0700470 BMCWEB_LOG_ERROR("Unexpected configuration");
Ed Tanous002d39b2022-05-31 08:59:27 -0700471 messages::internalError(asyncResp->res);
472 return;
473 }
James Feist5b4aa862018-08-16 14:07:01 -0700474
Ed Tanous002d39b2022-05-31 08:59:27 -0700475 // used for making maps out of 2 vectors
476 const std::vector<double>* keys = nullptr;
477 const std::vector<double>* values = nullptr;
478
479 for (const auto& propertyPair : intfPair.second)
480 {
481 if (propertyPair.first == "Type" ||
482 propertyPair.first == "Class" ||
483 propertyPair.first == "Name")
484 {
485 continue;
486 }
487
488 // zones
489 if (intfPair.first == pidZoneConfigurationIface)
490 {
491 const double* ptr =
492 std::get_if<double>(&propertyPair.second);
493 if (ptr == nullptr)
494 {
Ed Tanous62598e32023-07-17 17:06:25 -0700495 BMCWEB_LOG_ERROR("Field Illegal {}",
496 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700497 messages::internalError(asyncResp->res);
498 return;
499 }
500 (*config)[propertyPair.first] = *ptr;
501 }
502
503 if (intfPair.first == stepwiseConfigurationIface)
504 {
505 if (propertyPair.first == "Reading" ||
506 propertyPair.first == "Output")
507 {
508 const std::vector<double>* ptr =
509 std::get_if<std::vector<double>>(
510 &propertyPair.second);
511
512 if (ptr == nullptr)
513 {
Ed Tanous62598e32023-07-17 17:06:25 -0700514 BMCWEB_LOG_ERROR("Field Illegal {}",
515 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700516 messages::internalError(asyncResp->res);
517 return;
518 }
519
520 if (propertyPair.first == "Reading")
521 {
522 keys = ptr;
523 }
524 else
525 {
526 values = ptr;
527 }
528 if (keys != nullptr && values != nullptr)
529 {
530 if (keys->size() != values->size())
531 {
Ed Tanous62598e32023-07-17 17:06:25 -0700532 BMCWEB_LOG_ERROR(
533 "Reading and Output size don't match ");
Ed Tanous002d39b2022-05-31 08:59:27 -0700534 messages::internalError(asyncResp->res);
535 return;
536 }
537 nlohmann::json& steps = (*config)["Steps"];
538 steps = nlohmann::json::array();
539 for (size_t ii = 0; ii < keys->size(); ii++)
540 {
541 nlohmann::json::object_t step;
542 step["Target"] = (*keys)[ii];
543 step["Output"] = (*values)[ii];
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500544 steps.emplace_back(std::move(step));
Ed Tanous002d39b2022-05-31 08:59:27 -0700545 }
546 }
547 }
548 if (propertyPair.first == "NegativeHysteresis" ||
549 propertyPair.first == "PositiveHysteresis")
James Feist5b4aa862018-08-16 14:07:01 -0700550 {
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800551 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800552 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700553 if (ptr == nullptr)
554 {
Ed Tanous62598e32023-07-17 17:06:25 -0700555 BMCWEB_LOG_ERROR("Field Illegal {}",
556 propertyPair.first);
Jason M. Billsf12894f2018-10-09 12:45:45 -0700557 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700558 return;
559 }
James Feistb7a08d02018-12-11 14:55:37 -0800560 (*config)[propertyPair.first] = *ptr;
561 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700562 }
James Feistb7a08d02018-12-11 14:55:37 -0800563
Ed Tanous002d39b2022-05-31 08:59:27 -0700564 // pid and fans are off the same configuration
565 if (intfPair.first == pidConfigurationIface ||
566 intfPair.first == stepwiseConfigurationIface)
567 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700568 if (propertyPair.first == "Zones")
James Feistb7a08d02018-12-11 14:55:37 -0800569 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700570 const std::vector<std::string>* inputs =
571 std::get_if<std::vector<std::string>>(
572 &propertyPair.second);
573
574 if (inputs == nullptr)
James Feistb7a08d02018-12-11 14:55:37 -0800575 {
Ed Tanous62598e32023-07-17 17:06:25 -0700576 BMCWEB_LOG_ERROR("Zones Pid Field Illegal");
Ed Tanous002d39b2022-05-31 08:59:27 -0700577 messages::internalError(asyncResp->res);
578 return;
James Feistb7a08d02018-12-11 14:55:37 -0800579 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700580 auto& data = (*config)[propertyPair.first];
581 data = nlohmann::json::array();
582 for (std::string itemCopy : *inputs)
James Feistb7a08d02018-12-11 14:55:37 -0800583 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700584 dbus::utility::escapePathForDbus(itemCopy);
585 nlohmann::json::object_t input;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700586 boost::urls::url managerUrl = boost::urls::format(
587 "/redfish/v1/Managers/bmc#{}",
Willy Tueddfc432022-09-26 16:46:38 +0000588 ("/Oem/OpenBmc/Fan/FanZones"_json_pointer /
589 itemCopy)
590 .to_string());
591 input["@odata.id"] = std::move(managerUrl);
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500592 data.emplace_back(std::move(input));
James Feistb7a08d02018-12-11 14:55:37 -0800593 }
James Feist5b4aa862018-08-16 14:07:01 -0700594 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700595 // todo(james): may never happen, but this
596 // assumes configuration data referenced in the
597 // PID config is provided by the same daemon, we
598 // could add another loop to cover all cases,
599 // but I'm okay kicking this can down the road a
600 // bit
James Feist5b4aa862018-08-16 14:07:01 -0700601
Ed Tanous002d39b2022-05-31 08:59:27 -0700602 else if (propertyPair.first == "Inputs" ||
603 propertyPair.first == "Outputs")
James Feist5b4aa862018-08-16 14:07:01 -0700604 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700605 auto& data = (*config)[propertyPair.first];
606 const std::vector<std::string>* inputs =
607 std::get_if<std::vector<std::string>>(
608 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700609
Ed Tanous002d39b2022-05-31 08:59:27 -0700610 if (inputs == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700611 {
Ed Tanous62598e32023-07-17 17:06:25 -0700612 BMCWEB_LOG_ERROR("Field Illegal {}",
613 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700614 messages::internalError(asyncResp->res);
615 return;
James Feist5b4aa862018-08-16 14:07:01 -0700616 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700617 data = *inputs;
618 }
619 else if (propertyPair.first == "SetPointOffset")
620 {
621 const std::string* ptr =
622 std::get_if<std::string>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700623
Ed Tanous002d39b2022-05-31 08:59:27 -0700624 if (ptr == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700625 {
Ed Tanous62598e32023-07-17 17:06:25 -0700626 BMCWEB_LOG_ERROR("Field Illegal {}",
627 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700628 messages::internalError(asyncResp->res);
629 return;
James Feistb943aae2019-07-11 16:33:56 -0700630 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700631 // translate from dbus to redfish
632 if (*ptr == "WarningHigh")
James Feistb943aae2019-07-11 16:33:56 -0700633 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700634 (*config)["SetPointOffset"] =
635 "UpperThresholdNonCritical";
James Feistb943aae2019-07-11 16:33:56 -0700636 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700637 else if (*ptr == "WarningLow")
James Feist5b4aa862018-08-16 14:07:01 -0700638 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700639 (*config)["SetPointOffset"] =
640 "LowerThresholdNonCritical";
James Feist5b4aa862018-08-16 14:07:01 -0700641 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700642 else if (*ptr == "CriticalHigh")
643 {
644 (*config)["SetPointOffset"] =
645 "UpperThresholdCritical";
646 }
647 else if (*ptr == "CriticalLow")
648 {
649 (*config)["SetPointOffset"] =
650 "LowerThresholdCritical";
651 }
652 else
653 {
Ed Tanous62598e32023-07-17 17:06:25 -0700654 BMCWEB_LOG_ERROR("Value Illegal {}", *ptr);
Ed Tanous002d39b2022-05-31 08:59:27 -0700655 messages::internalError(asyncResp->res);
656 return;
657 }
658 }
659 // doubles
660 else if (propertyPair.first == "FFGainCoefficient" ||
661 propertyPair.first == "FFOffCoefficient" ||
662 propertyPair.first == "ICoefficient" ||
663 propertyPair.first == "ILimitMax" ||
664 propertyPair.first == "ILimitMin" ||
665 propertyPair.first == "PositiveHysteresis" ||
666 propertyPair.first == "NegativeHysteresis" ||
667 propertyPair.first == "OutLimitMax" ||
668 propertyPair.first == "OutLimitMin" ||
669 propertyPair.first == "PCoefficient" ||
670 propertyPair.first == "SetPoint" ||
671 propertyPair.first == "SlewNeg" ||
672 propertyPair.first == "SlewPos")
673 {
674 const double* ptr =
675 std::get_if<double>(&propertyPair.second);
676 if (ptr == nullptr)
677 {
Ed Tanous62598e32023-07-17 17:06:25 -0700678 BMCWEB_LOG_ERROR("Field Illegal {}",
679 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700680 messages::internalError(asyncResp->res);
681 return;
682 }
683 (*config)[propertyPair.first] = *ptr;
James Feist5b4aa862018-08-16 14:07:01 -0700684 }
685 }
686 }
687 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700688 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500689 });
James Feist5b4aa862018-08-16 14:07:01 -0700690}
Jennifer Leeca537922018-08-10 10:07:30 -0700691
James Feist83ff9ab2018-08-31 10:18:24 -0700692enum class CreatePIDRet
693{
694 fail,
695 del,
696 patch
697};
698
zhanghch058d1b46d2021-04-01 11:18:24 +0800699inline bool
700 getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
701 std::vector<nlohmann::json>& config,
702 std::vector<std::string>& zones)
James Feist5f2caae2018-12-12 14:08:25 -0800703{
James Feistb6baeaa2019-02-21 10:41:40 -0800704 if (config.empty())
705 {
Ed Tanous62598e32023-07-17 17:06:25 -0700706 BMCWEB_LOG_ERROR("Empty Zones");
Ed Tanousf818b042022-06-27 13:17:35 -0700707 messages::propertyValueFormatError(response->res, config, "Zones");
James Feistb6baeaa2019-02-21 10:41:40 -0800708 return false;
709 }
James Feist5f2caae2018-12-12 14:08:25 -0800710 for (auto& odata : config)
711 {
712 std::string path;
713 if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
714 path))
715 {
716 return false;
717 }
718 std::string input;
James Feist61adbda2019-03-25 13:03:51 -0700719
720 // 8 below comes from
721 // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
722 // 0 1 2 3 4 5 6 7 8
723 if (!dbus::utility::getNthStringFromPath(path, 8, input))
James Feist5f2caae2018-12-12 14:08:25 -0800724 {
Ed Tanous62598e32023-07-17 17:06:25 -0700725 BMCWEB_LOG_ERROR("Got invalid path {}", path);
726 BMCWEB_LOG_ERROR("Illegal Type Zones");
Ed Tanousf818b042022-06-27 13:17:35 -0700727 messages::propertyValueFormatError(response->res, odata, "Zones");
James Feist5f2caae2018-12-12 14:08:25 -0800728 return false;
729 }
Ed Tanousa170f272022-06-30 21:53:27 -0700730 std::replace(input.begin(), input.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -0800731 zones.emplace_back(std::move(input));
732 }
733 return true;
734}
735
Ed Tanous711ac7a2021-12-20 09:34:41 -0800736inline const dbus::utility::ManagedObjectType::value_type*
James Feist73df0db2019-03-25 15:29:35 -0700737 findChassis(const dbus::utility::ManagedObjectType& managedObj,
738 const std::string& value, std::string& chassis)
James Feistb6baeaa2019-02-21 10:41:40 -0800739{
Ed Tanous62598e32023-07-17 17:06:25 -0700740 BMCWEB_LOG_DEBUG("Find Chassis: {}", value);
James Feistb6baeaa2019-02-21 10:41:40 -0800741
Ed Tanousa170f272022-06-30 21:53:27 -0700742 std::string escaped = value;
Yaswanth Reddy M6ce82fa2023-03-10 07:29:45 +0000743 std::replace(escaped.begin(), escaped.end(), ' ', '_');
James Feistb6baeaa2019-02-21 10:41:40 -0800744 escaped = "/" + escaped;
Ed Tanous3544d2a2023-08-06 18:12:20 -0700745 auto it = std::ranges::find_if(managedObj, [&escaped](const auto& obj) {
Ed Tanous18f8f602023-07-18 10:07:23 -0700746 if (obj.first.str.ends_with(escaped))
Ed Tanous002d39b2022-05-31 08:59:27 -0700747 {
Ed Tanous62598e32023-07-17 17:06:25 -0700748 BMCWEB_LOG_DEBUG("Matched {}", obj.first.str);
Ed Tanous002d39b2022-05-31 08:59:27 -0700749 return true;
750 }
751 return false;
752 });
James Feistb6baeaa2019-02-21 10:41:40 -0800753
754 if (it == managedObj.end())
755 {
James Feist73df0db2019-03-25 15:29:35 -0700756 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800757 }
758 // 5 comes from <chassis-name> being the 5th element
759 // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
James Feist73df0db2019-03-25 15:29:35 -0700760 if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
761 {
762 return &(*it);
763 }
764
765 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800766}
767
Ed Tanous23a21a12020-07-25 04:45:05 +0000768inline CreatePIDRet createPidInterface(
zhanghch058d1b46d2021-04-01 11:18:24 +0800769 const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
Ed Tanousb5a76932020-09-29 16:16:58 -0700770 const nlohmann::json::iterator& it, const std::string& path,
James Feist83ff9ab2018-08-31 10:18:24 -0700771 const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800772 dbus::utility::DBusPropertiesMap& output, std::string& chassis,
773 const std::string& profile)
James Feist83ff9ab2018-08-31 10:18:24 -0700774{
James Feist5f2caae2018-12-12 14:08:25 -0800775 // common deleter
James Feistb6baeaa2019-02-21 10:41:40 -0800776 if (it.value() == nullptr)
James Feist5f2caae2018-12-12 14:08:25 -0800777 {
778 std::string iface;
779 if (type == "PidControllers" || type == "FanControllers")
780 {
781 iface = pidConfigurationIface;
782 }
783 else if (type == "FanZones")
784 {
785 iface = pidZoneConfigurationIface;
786 }
787 else if (type == "StepwiseControllers")
788 {
789 iface = stepwiseConfigurationIface;
790 }
791 else
792 {
Ed Tanous62598e32023-07-17 17:06:25 -0700793 BMCWEB_LOG_ERROR("Illegal Type {}", type);
James Feist5f2caae2018-12-12 14:08:25 -0800794 messages::propertyUnknown(response->res, type);
795 return CreatePIDRet::fail;
796 }
James Feist6ee7f772020-02-06 16:25:27 -0800797
Ed Tanous62598e32023-07-17 17:06:25 -0700798 BMCWEB_LOG_DEBUG("del {} {}", path, iface);
James Feist5f2caae2018-12-12 14:08:25 -0800799 // delete interface
800 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800801 [response, path](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700802 if (ec)
803 {
Ed Tanous62598e32023-07-17 17:06:25 -0700804 BMCWEB_LOG_ERROR("Error patching {}: {}", path, ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700805 messages::internalError(response->res);
806 return;
807 }
808 messages::success(response->res);
Patrick Williams5a39f772023-10-20 11:20:21 -0500809 },
James Feist5f2caae2018-12-12 14:08:25 -0800810 "xyz.openbmc_project.EntityManager", path, iface, "Delete");
811 return CreatePIDRet::del;
812 }
813
Ed Tanous711ac7a2021-12-20 09:34:41 -0800814 const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800815 if (!createNewObject)
816 {
817 // if we aren't creating a new object, we should be able to find it on
818 // d-bus
James Feist73df0db2019-03-25 15:29:35 -0700819 managedItem = findChassis(managedObj, it.key(), chassis);
820 if (managedItem == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800821 {
Ed Tanous62598e32023-07-17 17:06:25 -0700822 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousef4c65b2023-04-24 15:28:50 -0700823 messages::invalidObject(
824 response->res,
825 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800826 return CreatePIDRet::fail;
827 }
828 }
829
Ed Tanous26f69762022-01-25 09:49:11 -0800830 if (!profile.empty() &&
James Feist73df0db2019-03-25 15:29:35 -0700831 (type == "PidControllers" || type == "FanControllers" ||
832 type == "StepwiseControllers"))
833 {
834 if (managedItem == nullptr)
835 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800836 output.emplace_back("Profiles", std::vector<std::string>{profile});
James Feist73df0db2019-03-25 15:29:35 -0700837 }
838 else
839 {
840 std::string interface;
841 if (type == "StepwiseControllers")
842 {
843 interface = stepwiseConfigurationIface;
844 }
845 else
846 {
847 interface = pidConfigurationIface;
848 }
Ed Tanous711ac7a2021-12-20 09:34:41 -0800849 bool ifaceFound = false;
850 for (const auto& iface : managedItem->second)
851 {
852 if (iface.first == interface)
853 {
854 ifaceFound = true;
855 for (const auto& prop : iface.second)
856 {
857 if (prop.first == "Profiles")
858 {
859 const std::vector<std::string>* curProfiles =
860 std::get_if<std::vector<std::string>>(
861 &(prop.second));
862 if (curProfiles == nullptr)
863 {
Ed Tanous62598e32023-07-17 17:06:25 -0700864 BMCWEB_LOG_ERROR(
865 "Illegal profiles in managed object");
Ed Tanous711ac7a2021-12-20 09:34:41 -0800866 messages::internalError(response->res);
867 return CreatePIDRet::fail;
868 }
869 if (std::find(curProfiles->begin(),
870 curProfiles->end(),
871 profile) == curProfiles->end())
872 {
873 std::vector<std::string> newProfiles =
874 *curProfiles;
875 newProfiles.push_back(profile);
Ed Tanousb9d36b42022-02-26 21:42:46 -0800876 output.emplace_back("Profiles", newProfiles);
Ed Tanous711ac7a2021-12-20 09:34:41 -0800877 }
878 }
879 }
880 }
881 }
882
883 if (!ifaceFound)
James Feist73df0db2019-03-25 15:29:35 -0700884 {
Ed Tanous62598e32023-07-17 17:06:25 -0700885 BMCWEB_LOG_ERROR("Failed to find interface in managed object");
James Feist73df0db2019-03-25 15:29:35 -0700886 messages::internalError(response->res);
887 return CreatePIDRet::fail;
888 }
James Feist73df0db2019-03-25 15:29:35 -0700889 }
890 }
891
James Feist83ff9ab2018-08-31 10:18:24 -0700892 if (type == "PidControllers" || type == "FanControllers")
893 {
894 if (createNewObject)
895 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800896 output.emplace_back("Class",
897 type == "PidControllers" ? "temp" : "fan");
898 output.emplace_back("Type", "Pid");
James Feist83ff9ab2018-08-31 10:18:24 -0700899 }
James Feist5f2caae2018-12-12 14:08:25 -0800900
901 std::optional<std::vector<nlohmann::json>> zones;
902 std::optional<std::vector<std::string>> inputs;
903 std::optional<std::vector<std::string>> outputs;
904 std::map<std::string, std::optional<double>> doubles;
James Feistb943aae2019-07-11 16:33:56 -0700905 std::optional<std::string> setpointOffset;
James Feist5f2caae2018-12-12 14:08:25 -0800906 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -0800907 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
James Feist5f2caae2018-12-12 14:08:25 -0800908 "Zones", zones, "FFGainCoefficient",
909 doubles["FFGainCoefficient"], "FFOffCoefficient",
910 doubles["FFOffCoefficient"], "ICoefficient",
911 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
912 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
913 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
914 "PCoefficient", doubles["PCoefficient"], "SetPoint",
James Feistb943aae2019-07-11 16:33:56 -0700915 doubles["SetPoint"], "SetPointOffset", setpointOffset,
916 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
917 "PositiveHysteresis", doubles["PositiveHysteresis"],
918 "NegativeHysteresis", doubles["NegativeHysteresis"]))
James Feist83ff9ab2018-08-31 10:18:24 -0700919 {
James Feist5f2caae2018-12-12 14:08:25 -0800920 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700921 }
James Feist5f2caae2018-12-12 14:08:25 -0800922 if (zones)
James Feist83ff9ab2018-08-31 10:18:24 -0700923 {
James Feist5f2caae2018-12-12 14:08:25 -0800924 std::vector<std::string> zonesStr;
925 if (!getZonesFromJsonReq(response, *zones, zonesStr))
James Feist83ff9ab2018-08-31 10:18:24 -0700926 {
Ed Tanous62598e32023-07-17 17:06:25 -0700927 BMCWEB_LOG_ERROR("Illegal Zones");
James Feist5f2caae2018-12-12 14:08:25 -0800928 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700929 }
James Feistb6baeaa2019-02-21 10:41:40 -0800930 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -0800931 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800932 {
Ed Tanous62598e32023-07-17 17:06:25 -0700933 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousace85d62021-10-26 12:45:59 -0700934 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -0700935 response->res,
936 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800937 return CreatePIDRet::fail;
938 }
Ed Tanousb9d36b42022-02-26 21:42:46 -0800939 output.emplace_back("Zones", std::move(zonesStr));
James Feist5f2caae2018-12-12 14:08:25 -0800940 }
Ed Tanousafb9ee02022-12-21 11:59:17 -0800941
942 if (inputs)
James Feist5f2caae2018-12-12 14:08:25 -0800943 {
Ed Tanousafb9ee02022-12-21 11:59:17 -0800944 for (std::string& value : *inputs)
James Feist83ff9ab2018-08-31 10:18:24 -0700945 {
Ed Tanousafb9ee02022-12-21 11:59:17 -0800946 std::replace(value.begin(), value.end(), '_', ' ');
James Feist83ff9ab2018-08-31 10:18:24 -0700947 }
Ed Tanousafb9ee02022-12-21 11:59:17 -0800948 output.emplace_back("Inputs", *inputs);
949 }
950
951 if (outputs)
952 {
953 for (std::string& value : *outputs)
954 {
955 std::replace(value.begin(), value.end(), '_', ' ');
956 }
957 output.emplace_back("Outputs", *outputs);
James Feist5f2caae2018-12-12 14:08:25 -0800958 }
James Feist83ff9ab2018-08-31 10:18:24 -0700959
James Feistb943aae2019-07-11 16:33:56 -0700960 if (setpointOffset)
961 {
962 // translate between redfish and dbus names
963 if (*setpointOffset == "UpperThresholdNonCritical")
964 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800965 output.emplace_back("SetPointOffset", "WarningLow");
James Feistb943aae2019-07-11 16:33:56 -0700966 }
967 else if (*setpointOffset == "LowerThresholdNonCritical")
968 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800969 output.emplace_back("SetPointOffset", "WarningHigh");
James Feistb943aae2019-07-11 16:33:56 -0700970 }
971 else if (*setpointOffset == "LowerThresholdCritical")
972 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800973 output.emplace_back("SetPointOffset", "CriticalLow");
James Feistb943aae2019-07-11 16:33:56 -0700974 }
975 else if (*setpointOffset == "UpperThresholdCritical")
976 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800977 output.emplace_back("SetPointOffset", "CriticalHigh");
James Feistb943aae2019-07-11 16:33:56 -0700978 }
979 else
980 {
Ed Tanous62598e32023-07-17 17:06:25 -0700981 BMCWEB_LOG_ERROR("Invalid setpointoffset {}", *setpointOffset);
Ed Tanousace85d62021-10-26 12:45:59 -0700982 messages::propertyValueNotInList(response->res, it.key(),
983 "SetPointOffset");
James Feistb943aae2019-07-11 16:33:56 -0700984 return CreatePIDRet::fail;
985 }
986 }
987
James Feist5f2caae2018-12-12 14:08:25 -0800988 // doubles
989 for (const auto& pairs : doubles)
990 {
991 if (!pairs.second)
James Feist83ff9ab2018-08-31 10:18:24 -0700992 {
James Feist5f2caae2018-12-12 14:08:25 -0800993 continue;
James Feist83ff9ab2018-08-31 10:18:24 -0700994 }
Ed Tanous62598e32023-07-17 17:06:25 -0700995 BMCWEB_LOG_DEBUG("{} = {}", pairs.first, *pairs.second);
Ed Tanousb9d36b42022-02-26 21:42:46 -0800996 output.emplace_back(pairs.first, *pairs.second);
James Feist83ff9ab2018-08-31 10:18:24 -0700997 }
998 }
James Feist5f2caae2018-12-12 14:08:25 -0800999
James Feist83ff9ab2018-08-31 10:18:24 -07001000 else if (type == "FanZones")
1001 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001002 output.emplace_back("Type", "Pid.Zone");
James Feist83ff9ab2018-08-31 10:18:24 -07001003
James Feist5f2caae2018-12-12 14:08:25 -08001004 std::optional<nlohmann::json> chassisContainer;
1005 std::optional<double> failSafePercent;
James Feistd3ec07f2019-02-25 14:51:15 -08001006 std::optional<double> minThermalOutput;
James Feistb6baeaa2019-02-21 10:41:40 -08001007 if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
James Feist5f2caae2018-12-12 14:08:25 -08001008 chassisContainer, "FailSafePercent",
James Feistd3ec07f2019-02-25 14:51:15 -08001009 failSafePercent, "MinThermalOutput",
1010 minThermalOutput))
James Feist83ff9ab2018-08-31 10:18:24 -07001011 {
James Feist5f2caae2018-12-12 14:08:25 -08001012 return CreatePIDRet::fail;
1013 }
James Feist83ff9ab2018-08-31 10:18:24 -07001014
James Feist5f2caae2018-12-12 14:08:25 -08001015 if (chassisContainer)
1016 {
James Feist5f2caae2018-12-12 14:08:25 -08001017 std::string chassisId;
1018 if (!redfish::json_util::readJson(*chassisContainer, response->res,
1019 "@odata.id", chassisId))
James Feist83ff9ab2018-08-31 10:18:24 -07001020 {
James Feist83ff9ab2018-08-31 10:18:24 -07001021 return CreatePIDRet::fail;
1022 }
James Feist5f2caae2018-12-12 14:08:25 -08001023
AppaRao Puli717794d2019-10-18 22:54:53 +05301024 // /redfish/v1/chassis/chassis_name/
James Feist5f2caae2018-12-12 14:08:25 -08001025 if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
1026 {
Ed Tanous62598e32023-07-17 17:06:25 -07001027 BMCWEB_LOG_ERROR("Got invalid path {}", chassisId);
Ed Tanousace85d62021-10-26 12:45:59 -07001028 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001029 response->res,
1030 boost::urls::format("/redfish/v1/Chassis/{}", chassisId));
James Feist5f2caae2018-12-12 14:08:25 -08001031 return CreatePIDRet::fail;
1032 }
1033 }
James Feistd3ec07f2019-02-25 14:51:15 -08001034 if (minThermalOutput)
James Feist5f2caae2018-12-12 14:08:25 -08001035 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001036 output.emplace_back("MinThermalOutput", *minThermalOutput);
James Feist5f2caae2018-12-12 14:08:25 -08001037 }
1038 if (failSafePercent)
1039 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001040 output.emplace_back("FailSafePercent", *failSafePercent);
James Feist5f2caae2018-12-12 14:08:25 -08001041 }
1042 }
1043 else if (type == "StepwiseControllers")
1044 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001045 output.emplace_back("Type", "Stepwise");
James Feist5f2caae2018-12-12 14:08:25 -08001046
1047 std::optional<std::vector<nlohmann::json>> zones;
1048 std::optional<std::vector<nlohmann::json>> steps;
1049 std::optional<std::vector<std::string>> inputs;
1050 std::optional<double> positiveHysteresis;
1051 std::optional<double> negativeHysteresis;
James Feistc33a90e2019-03-01 10:17:44 -08001052 std::optional<std::string> direction; // upper clipping curve vs lower
James Feist5f2caae2018-12-12 14:08:25 -08001053 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -08001054 it.value(), response->res, "Zones", zones, "Steps", steps,
1055 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
James Feistc33a90e2019-03-01 10:17:44 -08001056 "NegativeHysteresis", negativeHysteresis, "Direction",
1057 direction))
James Feist5f2caae2018-12-12 14:08:25 -08001058 {
James Feist5f2caae2018-12-12 14:08:25 -08001059 return CreatePIDRet::fail;
1060 }
1061
1062 if (zones)
1063 {
James Feistb6baeaa2019-02-21 10:41:40 -08001064 std::vector<std::string> zonesStrs;
1065 if (!getZonesFromJsonReq(response, *zones, zonesStrs))
James Feist5f2caae2018-12-12 14:08:25 -08001066 {
Ed Tanous62598e32023-07-17 17:06:25 -07001067 BMCWEB_LOG_ERROR("Illegal Zones");
James Feist5f2caae2018-12-12 14:08:25 -08001068 return CreatePIDRet::fail;
1069 }
James Feistb6baeaa2019-02-21 10:41:40 -08001070 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -08001071 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -08001072 {
Ed Tanous62598e32023-07-17 17:06:25 -07001073 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousace85d62021-10-26 12:45:59 -07001074 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001075 response->res,
1076 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -08001077 return CreatePIDRet::fail;
1078 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001079 output.emplace_back("Zones", std::move(zonesStrs));
James Feist5f2caae2018-12-12 14:08:25 -08001080 }
1081 if (steps)
1082 {
1083 std::vector<double> readings;
1084 std::vector<double> outputs;
1085 for (auto& step : *steps)
1086 {
Ed Tanous543f4402022-01-06 13:12:53 -08001087 double target = 0.0;
1088 double out = 0.0;
James Feist5f2caae2018-12-12 14:08:25 -08001089
1090 if (!redfish::json_util::readJson(step, response->res, "Target",
Ed Tanous23a21a12020-07-25 04:45:05 +00001091 target, "Output", out))
James Feist5f2caae2018-12-12 14:08:25 -08001092 {
James Feist5f2caae2018-12-12 14:08:25 -08001093 return CreatePIDRet::fail;
1094 }
1095 readings.emplace_back(target);
Ed Tanous23a21a12020-07-25 04:45:05 +00001096 outputs.emplace_back(out);
James Feist5f2caae2018-12-12 14:08:25 -08001097 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001098 output.emplace_back("Reading", std::move(readings));
1099 output.emplace_back("Output", std::move(outputs));
James Feist5f2caae2018-12-12 14:08:25 -08001100 }
1101 if (inputs)
1102 {
1103 for (std::string& value : *inputs)
1104 {
Ed Tanousa170f272022-06-30 21:53:27 -07001105 std::replace(value.begin(), value.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -08001106 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001107 output.emplace_back("Inputs", std::move(*inputs));
James Feist5f2caae2018-12-12 14:08:25 -08001108 }
1109 if (negativeHysteresis)
1110 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001111 output.emplace_back("NegativeHysteresis", *negativeHysteresis);
James Feist5f2caae2018-12-12 14:08:25 -08001112 }
1113 if (positiveHysteresis)
1114 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001115 output.emplace_back("PositiveHysteresis", *positiveHysteresis);
James Feist83ff9ab2018-08-31 10:18:24 -07001116 }
James Feistc33a90e2019-03-01 10:17:44 -08001117 if (direction)
1118 {
1119 constexpr const std::array<const char*, 2> allowedDirections = {
1120 "Ceiling", "Floor"};
Ed Tanous3544d2a2023-08-06 18:12:20 -07001121 if (std::ranges::find(allowedDirections, *direction) ==
1122 allowedDirections.end())
James Feistc33a90e2019-03-01 10:17:44 -08001123 {
1124 messages::propertyValueTypeError(response->res, "Direction",
1125 *direction);
1126 return CreatePIDRet::fail;
1127 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001128 output.emplace_back("Class", *direction);
James Feistc33a90e2019-03-01 10:17:44 -08001129 }
James Feist83ff9ab2018-08-31 10:18:24 -07001130 }
1131 else
1132 {
Ed Tanous62598e32023-07-17 17:06:25 -07001133 BMCWEB_LOG_ERROR("Illegal Type {}", type);
Jason M. Bills35a62c72018-10-09 12:45:45 -07001134 messages::propertyUnknown(response->res, type);
James Feist83ff9ab2018-08-31 10:18:24 -07001135 return CreatePIDRet::fail;
1136 }
1137 return CreatePIDRet::patch;
1138}
James Feist73df0db2019-03-25 15:29:35 -07001139struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
1140{
Ed Tanous6936afe2022-09-08 15:10:39 -07001141 struct CompletionValues
1142 {
1143 std::vector<std::string> supportedProfiles;
1144 std::string currentProfile;
1145 dbus::utility::MapperGetSubTreeResponse subtree;
1146 };
James Feist73df0db2019-03-25 15:29:35 -07001147
Ed Tanous4e23a442022-06-06 09:57:26 -07001148 explicit GetPIDValues(
1149 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
Ed Tanous23a21a12020-07-25 04:45:05 +00001150 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001151
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001152 {}
James Feist73df0db2019-03-25 15:29:35 -07001153
1154 void run()
1155 {
1156 std::shared_ptr<GetPIDValues> self = shared_from_this();
1157
1158 // get all configurations
George Liue99073f2022-12-09 11:06:16 +08001159 constexpr std::array<std::string_view, 4> interfaces = {
1160 pidConfigurationIface, pidZoneConfigurationIface,
1161 objectManagerIface, stepwiseConfigurationIface};
1162 dbus::utility::getSubTree(
1163 "/", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001164 [self](
George Liue99073f2022-12-09 11:06:16 +08001165 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001166 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001167 if (ec)
1168 {
Ed Tanous62598e32023-07-17 17:06:25 -07001169 BMCWEB_LOG_ERROR("{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001170 messages::internalError(self->asyncResp->res);
1171 return;
1172 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001173 self->complete.subtree = subtreeLocal;
Patrick Williams5a39f772023-10-20 11:20:21 -05001174 });
James Feist73df0db2019-03-25 15:29:35 -07001175
1176 // at the same time get the selected profile
George Liue99073f2022-12-09 11:06:16 +08001177 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1178 thermalModeIface};
1179 dbus::utility::getSubTree(
1180 "/", 0, thermalModeIfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001181 [self](
George Liue99073f2022-12-09 11:06:16 +08001182 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001183 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001184 if (ec || subtreeLocal.empty())
1185 {
1186 return;
1187 }
1188 if (subtreeLocal[0].second.size() != 1)
1189 {
1190 // invalid mapper response, should never happen
Ed Tanous62598e32023-07-17 17:06:25 -07001191 BMCWEB_LOG_ERROR("GetPIDValues: Mapper Error");
Ed Tanous002d39b2022-05-31 08:59:27 -07001192 messages::internalError(self->asyncResp->res);
1193 return;
1194 }
1195
1196 const std::string& path = subtreeLocal[0].first;
1197 const std::string& owner = subtreeLocal[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001198
1199 sdbusplus::asio::getAllProperties(
1200 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous002d39b2022-05-31 08:59:27 -07001201 [path, owner,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001202 self](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001203 const dbus::utility::DBusPropertiesMap& resp) {
1204 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001205 {
Ed Tanous62598e32023-07-17 17:06:25 -07001206 BMCWEB_LOG_ERROR(
1207 "GetPIDValues: Can't get thermalModeIface {}", path);
James Feist73df0db2019-03-25 15:29:35 -07001208 messages::internalError(self->asyncResp->res);
1209 return;
1210 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001211
Ed Tanous002d39b2022-05-31 08:59:27 -07001212 const std::string* current = nullptr;
1213 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001214
1215 const bool success = sdbusplus::unpackPropertiesNoThrow(
1216 dbus_utils::UnpackErrorPrinter(), resp, "Current", current,
1217 "Supported", supported);
1218
1219 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001220 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001221 messages::internalError(self->asyncResp->res);
1222 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001223 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001224
Ed Tanous002d39b2022-05-31 08:59:27 -07001225 if (current == nullptr || supported == nullptr)
1226 {
Ed Tanous62598e32023-07-17 17:06:25 -07001227 BMCWEB_LOG_ERROR(
1228 "GetPIDValues: thermal mode iface invalid {}", path);
Ed Tanous002d39b2022-05-31 08:59:27 -07001229 messages::internalError(self->asyncResp->res);
1230 return;
1231 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001232 self->complete.currentProfile = *current;
1233 self->complete.supportedProfiles = *supported;
George Liue99073f2022-12-09 11:06:16 +08001234 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001235 });
James Feist73df0db2019-03-25 15:29:35 -07001236 }
1237
Ed Tanous6936afe2022-09-08 15:10:39 -07001238 static void
1239 processingComplete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1240 const CompletionValues& completion)
James Feist73df0db2019-03-25 15:29:35 -07001241 {
1242 if (asyncResp->res.result() != boost::beast::http::status::ok)
1243 {
1244 return;
1245 }
1246 // create map of <connection, path to objMgr>>
Ed Tanous6936afe2022-09-08 15:10:39 -07001247 boost::container::flat_map<
1248 std::string, std::string, std::less<>,
1249 std::vector<std::pair<std::string, std::string>>>
1250 objectMgrPaths;
1251 boost::container::flat_set<std::string, std::less<>,
1252 std::vector<std::string>>
1253 calledConnections;
1254 for (const auto& pathGroup : completion.subtree)
James Feist73df0db2019-03-25 15:29:35 -07001255 {
1256 for (const auto& connectionGroup : pathGroup.second)
1257 {
1258 auto findConnection =
1259 calledConnections.find(connectionGroup.first);
1260 if (findConnection != calledConnections.end())
1261 {
1262 break;
1263 }
1264 for (const std::string& interface : connectionGroup.second)
1265 {
1266 if (interface == objectManagerIface)
1267 {
1268 objectMgrPaths[connectionGroup.first] = pathGroup.first;
1269 }
1270 // this list is alphabetical, so we
1271 // should have found the objMgr by now
1272 if (interface == pidConfigurationIface ||
1273 interface == pidZoneConfigurationIface ||
1274 interface == stepwiseConfigurationIface)
1275 {
1276 auto findObjMgr =
1277 objectMgrPaths.find(connectionGroup.first);
1278 if (findObjMgr == objectMgrPaths.end())
1279 {
Ed Tanous62598e32023-07-17 17:06:25 -07001280 BMCWEB_LOG_DEBUG("{}Has no Object Manager",
1281 connectionGroup.first);
James Feist73df0db2019-03-25 15:29:35 -07001282 continue;
1283 }
1284
1285 calledConnections.insert(connectionGroup.first);
1286
1287 asyncPopulatePid(findObjMgr->first, findObjMgr->second,
Ed Tanous6936afe2022-09-08 15:10:39 -07001288 completion.currentProfile,
1289 completion.supportedProfiles,
James Feist73df0db2019-03-25 15:29:35 -07001290 asyncResp);
1291 break;
1292 }
1293 }
1294 }
1295 }
1296 }
1297
Ed Tanous6936afe2022-09-08 15:10:39 -07001298 ~GetPIDValues()
1299 {
1300 boost::asio::post(crow::connections::systemBus->get_io_context(),
1301 std::bind_front(&processingComplete, asyncResp,
1302 std::move(complete)));
1303 }
1304
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001305 GetPIDValues(const GetPIDValues&) = delete;
1306 GetPIDValues(GetPIDValues&&) = delete;
1307 GetPIDValues& operator=(const GetPIDValues&) = delete;
1308 GetPIDValues& operator=(GetPIDValues&&) = delete;
1309
zhanghch058d1b46d2021-04-01 11:18:24 +08001310 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Ed Tanous6936afe2022-09-08 15:10:39 -07001311 CompletionValues complete;
James Feist73df0db2019-03-25 15:29:35 -07001312};
1313
1314struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
1315{
zhanghch058d1b46d2021-04-01 11:18:24 +08001316 SetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
James Feist73df0db2019-03-25 15:29:35 -07001317 nlohmann::json& data) :
Ed Tanous271584a2019-07-09 16:24:22 -07001318 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001319 {
James Feist73df0db2019-03-25 15:29:35 -07001320 std::optional<nlohmann::json> pidControllers;
1321 std::optional<nlohmann::json> fanControllers;
1322 std::optional<nlohmann::json> fanZones;
1323 std::optional<nlohmann::json> stepwiseControllers;
1324
1325 if (!redfish::json_util::readJson(
1326 data, asyncResp->res, "PidControllers", pidControllers,
1327 "FanControllers", fanControllers, "FanZones", fanZones,
1328 "StepwiseControllers", stepwiseControllers, "Profile", profile))
1329 {
James Feist73df0db2019-03-25 15:29:35 -07001330 return;
1331 }
1332 configuration.emplace_back("PidControllers", std::move(pidControllers));
1333 configuration.emplace_back("FanControllers", std::move(fanControllers));
1334 configuration.emplace_back("FanZones", std::move(fanZones));
1335 configuration.emplace_back("StepwiseControllers",
1336 std::move(stepwiseControllers));
1337 }
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001338
1339 SetPIDValues(const SetPIDValues&) = delete;
1340 SetPIDValues(SetPIDValues&&) = delete;
1341 SetPIDValues& operator=(const SetPIDValues&) = delete;
1342 SetPIDValues& operator=(SetPIDValues&&) = delete;
1343
James Feist73df0db2019-03-25 15:29:35 -07001344 void run()
1345 {
1346 if (asyncResp->res.result() != boost::beast::http::status::ok)
1347 {
1348 return;
1349 }
1350
1351 std::shared_ptr<SetPIDValues> self = shared_from_this();
1352
1353 // todo(james): might make sense to do a mapper call here if this
1354 // interface gets more traction
George Liu5eb468d2023-06-20 17:03:24 +08001355 sdbusplus::message::object_path objPath(
1356 "/xyz/openbmc_project/inventory");
1357 dbus::utility::getManagedObjects(
1358 "xyz.openbmc_project.EntityManager", objPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001359 [self](const boost::system::error_code& ec,
Ed Tanous914e2d52022-01-07 11:38:34 -08001360 const dbus::utility::ManagedObjectType& mObj) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001361 if (ec)
1362 {
Ed Tanous62598e32023-07-17 17:06:25 -07001363 BMCWEB_LOG_ERROR("Error communicating to Entity Manager");
Ed Tanous002d39b2022-05-31 08:59:27 -07001364 messages::internalError(self->asyncResp->res);
1365 return;
1366 }
1367 const std::array<const char*, 3> configurations = {
1368 pidConfigurationIface, pidZoneConfigurationIface,
1369 stepwiseConfigurationIface};
James Feiste69d9de2020-02-07 12:23:27 -08001370
Ed Tanous002d39b2022-05-31 08:59:27 -07001371 for (const auto& [path, object] : mObj)
1372 {
1373 for (const auto& [interface, _] : object)
James Feiste69d9de2020-02-07 12:23:27 -08001374 {
Ed Tanous3544d2a2023-08-06 18:12:20 -07001375 if (std::ranges::find(configurations, interface) !=
1376 configurations.end())
James Feiste69d9de2020-02-07 12:23:27 -08001377 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001378 self->objectCount++;
1379 break;
James Feiste69d9de2020-02-07 12:23:27 -08001380 }
James Feiste69d9de2020-02-07 12:23:27 -08001381 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001382 }
1383 self->managedObj = mObj;
Patrick Williams5a39f772023-10-20 11:20:21 -05001384 });
James Feist73df0db2019-03-25 15:29:35 -07001385
1386 // at the same time get the profile information
George Liue99073f2022-12-09 11:06:16 +08001387 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1388 thermalModeIface};
1389 dbus::utility::getSubTree(
1390 "/", 0, thermalModeIfaces,
1391 [self](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001392 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001393 if (ec || subtree.empty())
1394 {
1395 return;
1396 }
1397 if (subtree[0].second.empty())
1398 {
1399 // invalid mapper response, should never happen
Ed Tanous62598e32023-07-17 17:06:25 -07001400 BMCWEB_LOG_ERROR("SetPIDValues: Mapper Error");
Ed Tanous002d39b2022-05-31 08:59:27 -07001401 messages::internalError(self->asyncResp->res);
1402 return;
1403 }
1404
1405 const std::string& path = subtree[0].first;
1406 const std::string& owner = subtree[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001407 sdbusplus::asio::getAllProperties(
1408 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001409 [self, path, owner](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001410 const dbus::utility::DBusPropertiesMap& r) {
1411 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001412 {
Ed Tanous62598e32023-07-17 17:06:25 -07001413 BMCWEB_LOG_ERROR(
1414 "SetPIDValues: Can't get thermalModeIface {}", path);
James Feist73df0db2019-03-25 15:29:35 -07001415 messages::internalError(self->asyncResp->res);
1416 return;
1417 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001418 const std::string* current = nullptr;
1419 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001420
1421 const bool success = sdbusplus::unpackPropertiesNoThrow(
1422 dbus_utils::UnpackErrorPrinter(), r, "Current", current,
1423 "Supported", supported);
1424
1425 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001426 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001427 messages::internalError(self->asyncResp->res);
1428 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001429 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001430
Ed Tanous002d39b2022-05-31 08:59:27 -07001431 if (current == nullptr || supported == nullptr)
1432 {
Ed Tanous62598e32023-07-17 17:06:25 -07001433 BMCWEB_LOG_ERROR(
1434 "SetPIDValues: thermal mode iface invalid {}", path);
Ed Tanous002d39b2022-05-31 08:59:27 -07001435 messages::internalError(self->asyncResp->res);
1436 return;
1437 }
1438 self->currentProfile = *current;
1439 self->supportedProfiles = *supported;
1440 self->profileConnection = owner;
1441 self->profilePath = path;
George Liue99073f2022-12-09 11:06:16 +08001442 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001443 });
James Feist73df0db2019-03-25 15:29:35 -07001444 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001445 void pidSetDone()
James Feist73df0db2019-03-25 15:29:35 -07001446 {
1447 if (asyncResp->res.result() != boost::beast::http::status::ok)
1448 {
1449 return;
1450 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001451 std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001452 if (profile)
1453 {
Ed Tanous3544d2a2023-08-06 18:12:20 -07001454 if (std::ranges::find(supportedProfiles, *profile) ==
1455 supportedProfiles.end())
James Feist73df0db2019-03-25 15:29:35 -07001456 {
1457 messages::actionParameterUnknown(response->res, "Profile",
1458 *profile);
1459 return;
1460 }
1461 currentProfile = *profile;
George Liu9ae226f2023-06-21 17:56:46 +08001462 sdbusplus::asio::setProperty(
1463 *crow::connections::systemBus, profileConnection, profilePath,
1464 thermalModeIface, "Current", *profile,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001465 [response](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001466 if (ec)
1467 {
Ed Tanous62598e32023-07-17 17:06:25 -07001468 BMCWEB_LOG_ERROR("Error patching profile{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001469 messages::internalError(response->res);
1470 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001471 });
James Feist73df0db2019-03-25 15:29:35 -07001472 }
1473
1474 for (auto& containerPair : configuration)
1475 {
1476 auto& container = containerPair.second;
1477 if (!container)
1478 {
1479 continue;
1480 }
Ed Tanous62598e32023-07-17 17:06:25 -07001481 BMCWEB_LOG_DEBUG("{}", *container);
James Feist6ee7f772020-02-06 16:25:27 -08001482
Ed Tanous02cad962022-06-30 16:50:15 -07001483 const std::string& type = containerPair.first;
James Feist73df0db2019-03-25 15:29:35 -07001484
1485 for (nlohmann::json::iterator it = container->begin();
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301486 it != container->end(); ++it)
James Feist73df0db2019-03-25 15:29:35 -07001487 {
1488 const auto& name = it.key();
Potin Laicddbf3d2023-02-14 14:28:58 +08001489 std::string dbusObjName = name;
1490 std::replace(dbusObjName.begin(), dbusObjName.end(), ' ', '_');
Ed Tanous62598e32023-07-17 17:06:25 -07001491 BMCWEB_LOG_DEBUG("looking for {}", name);
James Feist6ee7f772020-02-06 16:25:27 -08001492
Ed Tanous3544d2a2023-08-06 18:12:20 -07001493 auto pathItr = std::ranges::find_if(
1494 managedObj, [&dbusObjName](const auto& obj) {
Ed Tanous18f8f602023-07-18 10:07:23 -07001495 return obj.first.parent_path() == dbusObjName;
Patrick Williams5a39f772023-10-20 11:20:21 -05001496 });
Ed Tanousb9d36b42022-02-26 21:42:46 -08001497 dbus::utility::DBusPropertiesMap output;
James Feist73df0db2019-03-25 15:29:35 -07001498
1499 output.reserve(16); // The pid interface length
1500
1501 // determines if we're patching entity-manager or
1502 // creating a new object
1503 bool createNewObject = (pathItr == managedObj.end());
Ed Tanous62598e32023-07-17 17:06:25 -07001504 BMCWEB_LOG_DEBUG("Found = {}", !createNewObject);
James Feist6ee7f772020-02-06 16:25:27 -08001505
James Feist73df0db2019-03-25 15:29:35 -07001506 std::string iface;
Ed Tanousea2b6702022-03-07 16:48:38 -08001507 if (!createNewObject)
James Feist73df0db2019-03-25 15:29:35 -07001508 {
Potin Lai8be2b5b2022-11-22 13:27:16 +08001509 bool findInterface = false;
Ed Tanousea2b6702022-03-07 16:48:38 -08001510 for (const auto& interface : pathItr->second)
James Feist73df0db2019-03-25 15:29:35 -07001511 {
Ed Tanousea2b6702022-03-07 16:48:38 -08001512 if (interface.first == pidConfigurationIface)
1513 {
1514 if (type == "PidControllers" ||
1515 type == "FanControllers")
1516 {
1517 iface = pidConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001518 findInterface = true;
1519 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001520 }
1521 }
1522 else if (interface.first == pidZoneConfigurationIface)
1523 {
1524 if (type == "FanZones")
1525 {
1526 iface = pidConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001527 findInterface = true;
1528 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001529 }
1530 }
1531 else if (interface.first == stepwiseConfigurationIface)
1532 {
1533 if (type == "StepwiseControllers")
1534 {
1535 iface = stepwiseConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001536 findInterface = true;
1537 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001538 }
1539 }
James Feist73df0db2019-03-25 15:29:35 -07001540 }
Potin Lai8be2b5b2022-11-22 13:27:16 +08001541
1542 // create new object if interface not found
1543 if (!findInterface)
1544 {
1545 createNewObject = true;
1546 }
James Feist73df0db2019-03-25 15:29:35 -07001547 }
James Feist6ee7f772020-02-06 16:25:27 -08001548
1549 if (createNewObject && it.value() == nullptr)
1550 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -05001551 // can't delete a non-existent object
Ed Tanouse2616cc2022-06-27 12:45:55 -07001552 messages::propertyValueNotInList(response->res, it.value(),
1553 name);
James Feist6ee7f772020-02-06 16:25:27 -08001554 continue;
1555 }
1556
1557 std::string path;
1558 if (pathItr != managedObj.end())
1559 {
1560 path = pathItr->first.str;
1561 }
1562
Ed Tanous62598e32023-07-17 17:06:25 -07001563 BMCWEB_LOG_DEBUG("Create new = {}", createNewObject);
James Feiste69d9de2020-02-07 12:23:27 -08001564
1565 // arbitrary limit to avoid attacks
1566 constexpr const size_t controllerLimit = 500;
James Feist14b0b8d2020-02-12 11:52:07 -08001567 if (createNewObject && objectCount >= controllerLimit)
James Feiste69d9de2020-02-07 12:23:27 -08001568 {
1569 messages::resourceExhaustion(response->res, type);
1570 continue;
1571 }
Ed Tanousa170f272022-06-30 21:53:27 -07001572 std::string escaped = name;
1573 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1574 output.emplace_back("Name", escaped);
James Feist73df0db2019-03-25 15:29:35 -07001575
1576 std::string chassis;
1577 CreatePIDRet ret = createPidInterface(
James Feist6ee7f772020-02-06 16:25:27 -08001578 response, type, it, path, managedObj, createNewObject,
1579 output, chassis, currentProfile);
James Feist73df0db2019-03-25 15:29:35 -07001580 if (ret == CreatePIDRet::fail)
1581 {
1582 return;
1583 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001584 if (ret == CreatePIDRet::del)
James Feist73df0db2019-03-25 15:29:35 -07001585 {
1586 continue;
1587 }
1588
1589 if (!createNewObject)
1590 {
1591 for (const auto& property : output)
1592 {
Potin Lai7a696972023-11-09 12:18:20 +08001593 crow::connections::systemBus->async_method_call(
James Feist73df0db2019-03-25 15:29:35 -07001594 [response,
1595 propertyName{std::string(property.first)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001596 const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001597 if (ec)
1598 {
Ed Tanous62598e32023-07-17 17:06:25 -07001599 BMCWEB_LOG_ERROR("Error patching {}: {}",
1600 propertyName, ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001601 messages::internalError(response->res);
1602 return;
1603 }
1604 messages::success(response->res);
Potin Lai7a696972023-11-09 12:18:20 +08001605 },
1606 "xyz.openbmc_project.EntityManager", path,
1607 "org.freedesktop.DBus.Properties", "Set", iface,
1608 property.first, property.second);
James Feist73df0db2019-03-25 15:29:35 -07001609 }
1610 }
1611 else
1612 {
1613 if (chassis.empty())
1614 {
Ed Tanous62598e32023-07-17 17:06:25 -07001615 BMCWEB_LOG_ERROR("Failed to get chassis from config");
Ed Tanousace85d62021-10-26 12:45:59 -07001616 messages::internalError(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001617 return;
1618 }
1619
1620 bool foundChassis = false;
1621 for (const auto& obj : managedObj)
1622 {
Ed Tanous18f8f602023-07-18 10:07:23 -07001623 if (obj.first.parent_path() == chassis)
James Feist73df0db2019-03-25 15:29:35 -07001624 {
1625 chassis = obj.first.str;
1626 foundChassis = true;
1627 break;
1628 }
1629 }
1630 if (!foundChassis)
1631 {
Ed Tanous62598e32023-07-17 17:06:25 -07001632 BMCWEB_LOG_ERROR("Failed to find chassis on dbus");
James Feist73df0db2019-03-25 15:29:35 -07001633 messages::resourceMissingAtURI(
Ed Tanousace85d62021-10-26 12:45:59 -07001634 response->res,
Ed Tanousef4c65b2023-04-24 15:28:50 -07001635 boost::urls::format("/redfish/v1/Chassis/{}",
1636 chassis));
James Feist73df0db2019-03-25 15:29:35 -07001637 return;
1638 }
1639
1640 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001641 [response](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001642 if (ec)
1643 {
Ed Tanous62598e32023-07-17 17:06:25 -07001644 BMCWEB_LOG_ERROR("Error Adding Pid Object {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001645 messages::internalError(response->res);
1646 return;
1647 }
1648 messages::success(response->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05001649 },
James Feist73df0db2019-03-25 15:29:35 -07001650 "xyz.openbmc_project.EntityManager", chassis,
1651 "xyz.openbmc_project.AddObject", "AddObject", output);
1652 }
1653 }
1654 }
1655 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001656
1657 ~SetPIDValues()
1658 {
1659 try
1660 {
1661 pidSetDone();
1662 }
1663 catch (...)
1664 {
Ed Tanous62598e32023-07-17 17:06:25 -07001665 BMCWEB_LOG_CRITICAL("pidSetDone threw exception");
Ed Tanous24b2fe82022-01-06 12:45:54 -08001666 }
1667 }
1668
zhanghch058d1b46d2021-04-01 11:18:24 +08001669 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001670 std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
1671 configuration;
1672 std::optional<std::string> profile;
1673 dbus::utility::ManagedObjectType managedObj;
1674 std::vector<std::string> supportedProfiles;
1675 std::string currentProfile;
1676 std::string profileConnection;
1677 std::string profilePath;
James Feist14b0b8d2020-02-12 11:52:07 -08001678 size_t objectCount = 0;
James Feist73df0db2019-03-25 15:29:35 -07001679};
James Feist83ff9ab2018-08-31 10:18:24 -07001680
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001681/**
1682 * @brief Retrieves BMC manager location data over DBus
1683 *
Ed Tanousac106bf2023-06-07 09:24:59 -07001684 * @param[in] asyncResp Shared pointer for completing asynchronous calls
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001685 * @param[in] connectionName - service name
1686 * @param[in] path - object path
1687 * @return none
1688 */
Ed Tanousac106bf2023-06-07 09:24:59 -07001689inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001690 const std::string& connectionName,
1691 const std::string& path)
1692{
Ed Tanous62598e32023-07-17 17:06:25 -07001693 BMCWEB_LOG_DEBUG("Get BMC manager Location data.");
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001694
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001695 sdbusplus::asio::getProperty<std::string>(
1696 *crow::connections::systemBus, connectionName, path,
1697 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Ed Tanousac106bf2023-06-07 09:24:59 -07001698 [asyncResp](const boost::system::error_code& ec,
1699 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001700 if (ec)
1701 {
Ed Tanous62598e32023-07-17 17:06:25 -07001702 BMCWEB_LOG_DEBUG("DBUS response error for "
1703 "Location");
Ed Tanousac106bf2023-06-07 09:24:59 -07001704 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001705 return;
1706 }
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001707
Ed Tanousac106bf2023-06-07 09:24:59 -07001708 asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001709 property;
Patrick Williams5a39f772023-10-20 11:20:21 -05001710 });
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001711}
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001712// avoid name collision systems.hpp
1713inline void
Ed Tanousac106bf2023-06-07 09:24:59 -07001714 managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001715{
Ed Tanous62598e32023-07-17 17:06:25 -07001716 BMCWEB_LOG_DEBUG("Getting Manager Last Reset Time");
Ed Tanous52cc1122020-07-18 13:51:21 -07001717
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001718 sdbusplus::asio::getProperty<uint64_t>(
1719 *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
1720 "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
1721 "LastRebootTime",
Ed Tanousac106bf2023-06-07 09:24:59 -07001722 [asyncResp](const boost::system::error_code& ec,
1723 const uint64_t lastResetTime) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001724 if (ec)
1725 {
Ed Tanous62598e32023-07-17 17:06:25 -07001726 BMCWEB_LOG_DEBUG("D-BUS response error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001727 return;
1728 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001729
Ed Tanous002d39b2022-05-31 08:59:27 -07001730 // LastRebootTime is epoch time, in milliseconds
1731 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
1732 uint64_t lastResetTimeStamp = lastResetTime / 1000;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001733
Ed Tanous002d39b2022-05-31 08:59:27 -07001734 // Convert to ISO 8601 standard
Ed Tanousac106bf2023-06-07 09:24:59 -07001735 asyncResp->res.jsonValue["LastResetTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001736 redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
Patrick Williams5a39f772023-10-20 11:20:21 -05001737 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001738}
1739
1740/**
1741 * @brief Set the running firmware image
1742 *
Ed Tanousac106bf2023-06-07 09:24:59 -07001743 * @param[i,o] asyncResp - Async response object
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001744 * @param[i] runningFirmwareTarget - Image to make the running image
1745 *
1746 * @return void
1747 */
1748inline void
Ed Tanousac106bf2023-06-07 09:24:59 -07001749 setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001750 const std::string& runningFirmwareTarget)
1751{
1752 // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1753 std::string::size_type idPos = runningFirmwareTarget.rfind('/');
1754 if (idPos == std::string::npos)
1755 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001756 messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001757 "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001758 BMCWEB_LOG_DEBUG("Can't parse firmware ID!");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001759 return;
1760 }
1761 idPos++;
1762 if (idPos >= runningFirmwareTarget.size())
1763 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001764 messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001765 "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001766 BMCWEB_LOG_DEBUG("Invalid firmware ID.");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001767 return;
1768 }
1769 std::string firmwareId = runningFirmwareTarget.substr(idPos);
1770
1771 // Make sure the image is valid before setting priority
George Liu5eb468d2023-06-20 17:03:24 +08001772 sdbusplus::message::object_path objPath("/xyz/openbmc_project/software");
1773 dbus::utility::getManagedObjects(
1774 "xyz.openbmc_project.Software.BMC.Updater", objPath,
1775 [asyncResp, firmwareId, runningFirmwareTarget](
1776 const boost::system::error_code& ec,
1777 const dbus::utility::ManagedObjectType& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001778 if (ec)
1779 {
Ed Tanous62598e32023-07-17 17:06:25 -07001780 BMCWEB_LOG_DEBUG("D-Bus response error getting objects.");
Ed Tanousac106bf2023-06-07 09:24:59 -07001781 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001782 return;
1783 }
1784
1785 if (subtree.empty())
1786 {
Ed Tanous62598e32023-07-17 17:06:25 -07001787 BMCWEB_LOG_DEBUG("Can't find image!");
Ed Tanousac106bf2023-06-07 09:24:59 -07001788 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001789 return;
1790 }
1791
1792 bool foundImage = false;
Ed Tanous02cad962022-06-30 16:50:15 -07001793 for (const auto& object : subtree)
Ed Tanous002d39b2022-05-31 08:59:27 -07001794 {
1795 const std::string& path =
1796 static_cast<const std::string&>(object.first);
1797 std::size_t idPos2 = path.rfind('/');
1798
1799 if (idPos2 == std::string::npos)
1800 {
1801 continue;
1802 }
1803
1804 idPos2++;
1805 if (idPos2 >= path.size())
1806 {
1807 continue;
1808 }
1809
1810 if (path.substr(idPos2) == firmwareId)
1811 {
1812 foundImage = true;
1813 break;
1814 }
1815 }
1816
1817 if (!foundImage)
1818 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001819 messages::propertyValueNotInList(
1820 asyncResp->res, runningFirmwareTarget, "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001821 BMCWEB_LOG_DEBUG("Invalid firmware ID.");
Ed Tanous002d39b2022-05-31 08:59:27 -07001822 return;
1823 }
1824
Ed Tanous62598e32023-07-17 17:06:25 -07001825 BMCWEB_LOG_DEBUG("Setting firmware version {} to priority 0.",
1826 firmwareId);
Ed Tanous002d39b2022-05-31 08:59:27 -07001827
1828 // Only support Immediate
1829 // An addition could be a Redfish Setting like
1830 // ActiveSoftwareImageApplyTime and support OnReset
George Liu9ae226f2023-06-21 17:56:46 +08001831 sdbusplus::asio::setProperty(
1832 *crow::connections::systemBus,
1833 "xyz.openbmc_project.Software.BMC.Updater",
1834 "/xyz/openbmc_project/software/" + firmwareId,
1835 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1836 static_cast<uint8_t>(0),
Ed Tanousac106bf2023-06-07 09:24:59 -07001837 [asyncResp](const boost::system::error_code& ec2) {
Ed Tanous8a592812022-06-04 09:06:59 -07001838 if (ec2)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001839 {
Ed Tanous62598e32023-07-17 17:06:25 -07001840 BMCWEB_LOG_DEBUG("D-Bus response error setting.");
Ed Tanousac106bf2023-06-07 09:24:59 -07001841 messages::internalError(asyncResp->res);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001842 return;
1843 }
Ed Tanousac106bf2023-06-07 09:24:59 -07001844 doBMCGracefulRestart(asyncResp);
George Liu5eb468d2023-06-20 17:03:24 +08001845 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001846 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001847}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001848
Ed Tanousac106bf2023-06-07 09:24:59 -07001849inline void setDateTime(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001850 std::string datetime)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001851{
Ed Tanous62598e32023-07-17 17:06:25 -07001852 BMCWEB_LOG_DEBUG("Set date time: {}", datetime);
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001853
Ed Tanousc2e32002023-01-07 22:05:08 -08001854 std::optional<redfish::time_utils::usSinceEpoch> us =
1855 redfish::time_utils::dateStringToEpoch(datetime);
1856 if (!us)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001857 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001858 messages::propertyValueFormatError(asyncResp->res, datetime,
1859 "DateTime");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001860 return;
1861 }
George Liu9ae226f2023-06-21 17:56:46 +08001862 sdbusplus::asio::setProperty(
1863 *crow::connections::systemBus, "xyz.openbmc_project.Time.Manager",
1864 "/xyz/openbmc_project/time/bmc", "xyz.openbmc_project.Time.EpochTime",
1865 "Elapsed", us->count(),
Ed Tanousac106bf2023-06-07 09:24:59 -07001866 [asyncResp{std::move(asyncResp)},
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001867 datetime{std::move(datetime)}](const boost::system::error_code& ec) {
Ed Tanousc2e32002023-01-07 22:05:08 -08001868 if (ec)
1869 {
Ed Tanous62598e32023-07-17 17:06:25 -07001870 BMCWEB_LOG_DEBUG("Failed to set elapsed time. "
1871 "DBUS response error {}",
1872 ec);
Ed Tanousac106bf2023-06-07 09:24:59 -07001873 messages::internalError(asyncResp->res);
Ed Tanousc2e32002023-01-07 22:05:08 -08001874 return;
1875 }
Ed Tanousac106bf2023-06-07 09:24:59 -07001876 asyncResp->res.jsonValue["DateTime"] = datetime;
Patrick Williams5a39f772023-10-20 11:20:21 -05001877 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001878}
1879
Ed Tanous75815e52022-10-05 17:21:13 -07001880inline void
1881 checkForQuiesced(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1882{
1883 sdbusplus::asio::getProperty<std::string>(
1884 *crow::connections::systemBus, "org.freedesktop.systemd1",
1885 "/org/freedesktop/systemd1/unit/obmc-bmc-service-quiesce@0.target",
1886 "org.freedesktop.systemd1.Unit", "ActiveState",
1887 [asyncResp](const boost::system::error_code& ec,
1888 const std::string& val) {
1889 if (!ec)
1890 {
1891 if (val == "active")
1892 {
1893 asyncResp->res.jsonValue["Status"]["Health"] = "Critical";
1894 asyncResp->res.jsonValue["Status"]["State"] = "Quiesced";
1895 return;
1896 }
1897 }
1898 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
1899 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
Patrick Williams5a39f772023-10-20 11:20:21 -05001900 });
Ed Tanous75815e52022-10-05 17:21:13 -07001901}
1902
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001903inline void requestRoutesManager(App& app)
1904{
1905 std::string uuid = persistent_data::getConfig().systemUuid;
1906
1907 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07001908 .privileges(redfish::privileges::getManager)
Ed Tanous002d39b2022-05-31 08:59:27 -07001909 .methods(boost::beast::http::verb::get)(
1910 [&app, uuid](const crow::Request& req,
Ed Tanous45ca1b82022-03-25 13:07:27 -07001911 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001912 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001913 {
1914 return;
1915 }
1916 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
Sui Chena51fc2d2022-07-14 17:21:53 -07001917 asyncResp->res.jsonValue["@odata.type"] = "#Manager.v1_14_0.Manager";
Ed Tanous002d39b2022-05-31 08:59:27 -07001918 asyncResp->res.jsonValue["Id"] = "bmc";
1919 asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
1920 asyncResp->res.jsonValue["Description"] =
1921 "Baseboard Management Controller";
1922 asyncResp->res.jsonValue["PowerState"] = "On";
Ed Tanous14766872022-03-15 10:44:42 -07001923
Ed Tanous002d39b2022-05-31 08:59:27 -07001924 asyncResp->res.jsonValue["ManagerType"] = "BMC";
1925 asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
1926 asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1927 asyncResp->res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001928
Ed Tanous002d39b2022-05-31 08:59:27 -07001929 asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
1930 "/redfish/v1/Managers/bmc/LogServices";
1931 asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
1932 "/redfish/v1/Managers/bmc/NetworkProtocol";
1933 asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
1934 "/redfish/v1/Managers/bmc/EthernetInterfaces";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001935
1936#ifdef BMCWEB_ENABLE_VM_NBDPROXY
Ed Tanous002d39b2022-05-31 08:59:27 -07001937 asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
1938 "/redfish/v1/Managers/bmc/VirtualMedia";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001939#endif // BMCWEB_ENABLE_VM_NBDPROXY
1940
Ed Tanous002d39b2022-05-31 08:59:27 -07001941 // default oem data
1942 nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
1943 nlohmann::json& oemOpenbmc = oem["OpenBmc"];
1944 oem["@odata.type"] = "#OemManager.Oem";
1945 oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
1946 oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
1947 oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
Ed Tanous14766872022-03-15 10:44:42 -07001948
Ed Tanous002d39b2022-05-31 08:59:27 -07001949 nlohmann::json::object_t certificates;
1950 certificates["@odata.id"] =
1951 "/redfish/v1/Managers/bmc/Truststore/Certificates";
1952 oemOpenbmc["Certificates"] = std::move(certificates);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001953
Ed Tanous002d39b2022-05-31 08:59:27 -07001954 // Manager.Reset (an action) can be many values, OpenBMC only
1955 // supports BMC reboot.
1956 nlohmann::json& managerReset =
1957 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
1958 managerReset["target"] =
1959 "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
1960 managerReset["@Redfish.ActionInfo"] =
1961 "/redfish/v1/Managers/bmc/ResetActionInfo";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001962
Ed Tanous002d39b2022-05-31 08:59:27 -07001963 // ResetToDefaults (Factory Reset) has values like
1964 // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
1965 // on OpenBMC
1966 nlohmann::json& resetToDefaults =
1967 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
1968 resetToDefaults["target"] =
1969 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
Ed Tanous613dabe2022-07-09 11:17:36 -07001970 resetToDefaults["ResetType@Redfish.AllowableValues"] =
1971 nlohmann::json::array_t({"ResetAll"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001972
Ed Tanous002d39b2022-05-31 08:59:27 -07001973 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001974 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301975
Ed Tanous002d39b2022-05-31 08:59:27 -07001976 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1977 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1978 redfishDateTimeOffset.second;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001979
Ed Tanous002d39b2022-05-31 08:59:27 -07001980 // TODO (Gunnar): Remove these one day since moved to ComputerSystem
1981 // Still used by OCP profiles
1982 // https://github.com/opencomputeproject/OCP-Profiles/issues/23
1983 // Fill in SerialConsole info
1984 asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
1985 asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
Ed Tanous613dabe2022-07-09 11:17:36 -07001986 asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
1987 nlohmann::json::array_t({"IPMI", "SSH"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001988#ifdef BMCWEB_ENABLE_KVM
Ed Tanous002d39b2022-05-31 08:59:27 -07001989 // Fill in GraphicalConsole info
1990 asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
1991 asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] =
1992 4;
Ed Tanous613dabe2022-07-09 11:17:36 -07001993 asyncResp->res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
1994 nlohmann::json::array_t({"KVMIP"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001995#endif // BMCWEB_ENABLE_KVM
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001996 if constexpr (!bmcwebEnableMultiHost)
1997 {
1998 asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] =
1999 1;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002000
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002001 nlohmann::json::array_t managerForServers;
2002 nlohmann::json::object_t manager;
2003 manager["@odata.id"] = "/redfish/v1/Systems/system";
2004 managerForServers.emplace_back(std::move(manager));
Ed Tanous14766872022-03-15 10:44:42 -07002005
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002006 asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
2007 std::move(managerForServers);
2008 }
Willy Tu13451e32023-05-24 16:08:18 -07002009 if constexpr (bmcwebEnableHealthPopulate)
2010 {
2011 auto health = std::make_shared<HealthPopulate>(asyncResp);
2012 health->isManagersHealth = true;
2013 health->populate();
2014 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002015
Willy Tueee00132022-06-14 14:53:17 -07002016 sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
Ed Tanous002d39b2022-05-31 08:59:27 -07002017 "FirmwareVersion", true);
2018
2019 managerGetLastResetTime(asyncResp);
2020
Sui Chena51fc2d2022-07-14 17:21:53 -07002021 // ManagerDiagnosticData is added for all BMCs.
2022 nlohmann::json& managerDiagnosticData =
2023 asyncResp->res.jsonValue["ManagerDiagnosticData"];
2024 managerDiagnosticData["@odata.id"] =
2025 "/redfish/v1/Managers/bmc/ManagerDiagnosticData";
2026
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002027#ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
Ed Tanous002d39b2022-05-31 08:59:27 -07002028 auto pids = std::make_shared<GetPIDValues>(asyncResp);
2029 pids->run();
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002030#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07002031
2032 getMainChassisId(asyncResp,
2033 [](const std::string& chassisId,
2034 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2035 aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
2036 nlohmann::json::array_t managerForChassis;
Ed Tanous8a592812022-06-04 09:06:59 -07002037 nlohmann::json::object_t managerObj;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002038 boost::urls::url chassiUrl =
2039 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
Willy Tueddfc432022-09-26 16:46:38 +00002040 managerObj["@odata.id"] = chassiUrl;
Patrick Williamsad539542023-05-12 10:10:08 -05002041 managerForChassis.emplace_back(std::move(managerObj));
Ed Tanous002d39b2022-05-31 08:59:27 -07002042 aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
2043 std::move(managerForChassis);
2044 aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00002045 chassiUrl;
Ed Tanous002d39b2022-05-31 08:59:27 -07002046 });
Ed Tanous14766872022-03-15 10:44:42 -07002047
Ed Tanous75815e52022-10-05 17:21:13 -07002048 sdbusplus::asio::getProperty<double>(
2049 *crow::connections::systemBus, "org.freedesktop.systemd1",
2050 "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
2051 "Progress",
2052 [asyncResp](const boost::system::error_code& ec, double val) {
2053 if (ec)
2054 {
Ed Tanous62598e32023-07-17 17:06:25 -07002055 BMCWEB_LOG_ERROR("Error while getting progress");
Ed Tanous75815e52022-10-05 17:21:13 -07002056 messages::internalError(asyncResp->res);
2057 return;
2058 }
2059 if (val < 1.0)
2060 {
2061 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
2062 asyncResp->res.jsonValue["Status"]["State"] = "Starting";
2063 return;
2064 }
2065 checkForQuiesced(asyncResp);
Patrick Williams5a39f772023-10-20 11:20:21 -05002066 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002067
George Liue99073f2022-12-09 11:06:16 +08002068 constexpr std::array<std::string_view, 1> interfaces = {
2069 "xyz.openbmc_project.Inventory.Item.Bmc"};
2070 dbus::utility::getSubTree(
2071 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07002072 [asyncResp](
George Liue99073f2022-12-09 11:06:16 +08002073 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07002074 const dbus::utility::MapperGetSubTreeResponse& subtree) {
2075 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002076 {
Ed Tanous62598e32023-07-17 17:06:25 -07002077 BMCWEB_LOG_DEBUG("D-Bus response error on GetSubTree {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002078 return;
2079 }
2080 if (subtree.empty())
2081 {
Ed Tanous62598e32023-07-17 17:06:25 -07002082 BMCWEB_LOG_DEBUG("Can't find bmc D-Bus object!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002083 return;
2084 }
2085 // Assume only 1 bmc D-Bus object
2086 // Throw an error if there is more than 1
2087 if (subtree.size() > 1)
2088 {
Ed Tanous62598e32023-07-17 17:06:25 -07002089 BMCWEB_LOG_DEBUG("Found more than 1 bmc D-Bus object!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002090 messages::internalError(asyncResp->res);
2091 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002092 }
2093
Ed Tanous002d39b2022-05-31 08:59:27 -07002094 if (subtree[0].first.empty() || subtree[0].second.size() != 1)
2095 {
Ed Tanous62598e32023-07-17 17:06:25 -07002096 BMCWEB_LOG_DEBUG("Error getting bmc D-Bus object!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002097 messages::internalError(asyncResp->res);
2098 return;
2099 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002100
Ed Tanous002d39b2022-05-31 08:59:27 -07002101 const std::string& path = subtree[0].first;
2102 const std::string& connectionName = subtree[0].second[0].first;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002103
Ed Tanous002d39b2022-05-31 08:59:27 -07002104 for (const auto& interfaceName : subtree[0].second[0].second)
2105 {
2106 if (interfaceName ==
2107 "xyz.openbmc_project.Inventory.Decorator.Asset")
2108 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002109 sdbusplus::asio::getAllProperties(
2110 *crow::connections::systemBus, connectionName, path,
2111 "xyz.openbmc_project.Inventory.Decorator.Asset",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002112 [asyncResp](const boost::system::error_code& ec2,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002113 const dbus::utility::DBusPropertiesMap&
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002114 propertiesList) {
Ed Tanous8a592812022-06-04 09:06:59 -07002115 if (ec2)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002116 {
Ed Tanous62598e32023-07-17 17:06:25 -07002117 BMCWEB_LOG_DEBUG("Can't get bmc asset!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002118 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002119 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002120
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002121 const std::string* partNumber = nullptr;
2122 const std::string* serialNumber = nullptr;
2123 const std::string* manufacturer = nullptr;
2124 const std::string* model = nullptr;
2125 const std::string* sparePartNumber = nullptr;
2126
2127 const bool success = sdbusplus::unpackPropertiesNoThrow(
2128 dbus_utils::UnpackErrorPrinter(), propertiesList,
2129 "PartNumber", partNumber, "SerialNumber",
2130 serialNumber, "Manufacturer", manufacturer, "Model",
2131 model, "SparePartNumber", sparePartNumber);
2132
2133 if (!success)
2134 {
2135 messages::internalError(asyncResp->res);
2136 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07002137 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002138
2139 if (partNumber != nullptr)
2140 {
2141 asyncResp->res.jsonValue["PartNumber"] =
2142 *partNumber;
2143 }
2144
2145 if (serialNumber != nullptr)
2146 {
2147 asyncResp->res.jsonValue["SerialNumber"] =
2148 *serialNumber;
2149 }
2150
2151 if (manufacturer != nullptr)
2152 {
2153 asyncResp->res.jsonValue["Manufacturer"] =
2154 *manufacturer;
2155 }
2156
2157 if (model != nullptr)
2158 {
2159 asyncResp->res.jsonValue["Model"] = *model;
2160 }
2161
2162 if (sparePartNumber != nullptr)
2163 {
2164 asyncResp->res.jsonValue["SparePartNumber"] =
2165 *sparePartNumber;
2166 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002167 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002168 }
2169 else if (interfaceName ==
2170 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
2171 {
2172 getLocation(asyncResp, connectionName, path);
2173 }
2174 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002175 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002176 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002177
2178 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07002179 .privileges(redfish::privileges::patchManager)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002180 .methods(boost::beast::http::verb::patch)(
2181 [&app](const crow::Request& req,
2182 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002183 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002184 {
2185 return;
2186 }
2187 std::optional<nlohmann::json> oem;
2188 std::optional<nlohmann::json> links;
2189 std::optional<std::string> datetime;
2190
2191 if (!json_util::readJsonPatch(req, asyncResp->res, "Oem", oem,
2192 "DateTime", datetime, "Links", links))
2193 {
2194 return;
2195 }
2196
2197 if (oem)
2198 {
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002199#ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
Ed Tanous002d39b2022-05-31 08:59:27 -07002200 std::optional<nlohmann::json> openbmc;
2201 if (!redfish::json_util::readJson(*oem, asyncResp->res, "OpenBmc",
2202 openbmc))
2203 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002204 return;
2205 }
2206 if (openbmc)
2207 {
2208 std::optional<nlohmann::json> fan;
2209 if (!redfish::json_util::readJson(*openbmc, asyncResp->res,
2210 "Fan", fan))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002211 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002212 return;
2213 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002214 if (fan)
2215 {
2216 auto pid = std::make_shared<SetPIDValues>(asyncResp, *fan);
2217 pid->run();
2218 }
2219 }
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002220#else
2221 messages::propertyUnknown(asyncResp->res, "Oem");
2222 return;
2223#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07002224 }
2225 if (links)
2226 {
2227 std::optional<nlohmann::json> activeSoftwareImage;
2228 if (!redfish::json_util::readJson(*links, asyncResp->res,
2229 "ActiveSoftwareImage",
2230 activeSoftwareImage))
2231 {
2232 return;
2233 }
2234 if (activeSoftwareImage)
2235 {
2236 std::optional<std::string> odataId;
2237 if (!json_util::readJson(*activeSoftwareImage, asyncResp->res,
2238 "@odata.id", odataId))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002239 {
Ed Tanous45ca1b82022-03-25 13:07:27 -07002240 return;
2241 }
2242
Ed Tanous002d39b2022-05-31 08:59:27 -07002243 if (odataId)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002244 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002245 setActiveFirmwareImage(asyncResp, *odataId);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002246 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002247 }
2248 }
2249 if (datetime)
2250 {
2251 setDateTime(asyncResp, std::move(*datetime));
2252 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002253 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002254}
2255
2256inline void requestRoutesManagerCollection(App& app)
2257{
2258 BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
Ed Tanoused398212021-06-09 17:05:54 -07002259 .privileges(redfish::privileges::getManagerCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002260 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002261 [&app](const crow::Request& req,
2262 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002263 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002264 {
2265 return;
2266 }
2267 // Collections don't include the static data added by SubRoute
2268 // because it has a duplicate entry for members
2269 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
2270 asyncResp->res.jsonValue["@odata.type"] =
2271 "#ManagerCollection.ManagerCollection";
2272 asyncResp->res.jsonValue["Name"] = "Manager Collection";
2273 asyncResp->res.jsonValue["Members@odata.count"] = 1;
2274 nlohmann::json::array_t members;
2275 nlohmann::json& bmc = members.emplace_back();
2276 bmc["@odata.id"] = "/redfish/v1/Managers/bmc";
2277 asyncResp->res.jsonValue["Members"] = std::move(members);
Patrick Williams5a39f772023-10-20 11:20:21 -05002278 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002279}
Ed Tanous1abe55e2018-09-05 08:30:59 -07002280} // namespace redfish