blob: 3c08cb57c81154b2eb9171fd6ad0217489aae7aa [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"
Ed Tanous539d8c62024-06-19 14:38:27 -070022#include "generated/enums/action_info.hpp"
23#include "generated/enums/manager.hpp"
24#include "generated/enums/resource.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070025#include "query.hpp"
Jennifer Leec5d03ff2019-03-08 15:42:58 -080026#include "redfish_util.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070027#include "registries/privilege_registry.hpp"
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +020028#include "utils/dbus_utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080029#include "utils/json_utils.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070030#include "utils/sw_utils.hpp"
31#include "utils/systemd_utils.hpp"
Ed Tanous2b829372022-08-03 14:22:34 -070032#include "utils/time_utils.hpp"
Borawski.Lukasz9c3106852018-02-09 15:24:22 +010033
George Liue99073f2022-12-09 11:06:16 +080034#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070035#include <boost/url/format.hpp>
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +020036#include <sdbusplus/asio/property.hpp>
37#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050038
Ed Tanousa170f272022-06-30 21:53:27 -070039#include <algorithm>
George Liue99073f2022-12-09 11:06:16 +080040#include <array>
Gunnar Mills4bfefa72020-07-30 13:54:29 -050041#include <cstdint>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050042#include <memory>
Konstantin Aladyshev9970e932024-02-20 09:51:29 +030043#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070044#include <ranges>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050045#include <sstream>
Konstantin Aladyshev9970e932024-02-20 09:51:29 +030046#include <string>
George Liue99073f2022-12-09 11:06:16 +080047#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080048#include <variant>
James Feist5b4aa862018-08-16 14:07:01 -070049
Ed Tanous1abe55e2018-09-05 08:30:59 -070050namespace redfish
51{
Jennifer Leeed5befb2018-08-10 11:29:45 -070052
53/**
Gunnar Mills2a5c4402020-05-19 09:07:24 -050054 * Function reboots the BMC.
55 *
56 * @param[in] asyncResp - Shared pointer for completing asynchronous calls
Jennifer Leeed5befb2018-08-10 11:29:45 -070057 */
zhanghch058d1b46d2021-04-01 11:18:24 +080058inline void
59 doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Gunnar Mills2a5c4402020-05-19 09:07:24 -050060{
61 const char* processName = "xyz.openbmc_project.State.BMC";
62 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
63 const char* interfaceName = "xyz.openbmc_project.State.BMC";
64 const std::string& propertyValue =
65 "xyz.openbmc_project.State.BMC.Transition.Reboot";
66 const char* destProperty = "RequestedBMCTransition";
67
68 // Create the D-Bus variant for D-Bus call.
George Liu9ae226f2023-06-21 17:56:46 +080069 sdbusplus::asio::setProperty(
70 *crow::connections::systemBus, processName, objectPath, interfaceName,
71 destProperty, propertyValue,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080072 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -070073 // Use "Set" method to set the property value.
74 if (ec)
75 {
Ed Tanous62598e32023-07-17 17:06:25 -070076 BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -070077 messages::internalError(asyncResp->res);
78 return;
79 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -050080
Ed Tanous002d39b2022-05-31 08:59:27 -070081 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -050082 });
Gunnar Mills2a5c4402020-05-19 09:07:24 -050083}
84
zhanghch058d1b46d2021-04-01 11:18:24 +080085inline void
86 doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +000087{
88 const char* processName = "xyz.openbmc_project.State.BMC";
89 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
90 const char* interfaceName = "xyz.openbmc_project.State.BMC";
91 const std::string& propertyValue =
92 "xyz.openbmc_project.State.BMC.Transition.HardReboot";
93 const char* destProperty = "RequestedBMCTransition";
94
95 // Create the D-Bus variant for D-Bus call.
George Liu9ae226f2023-06-21 17:56:46 +080096 sdbusplus::asio::setProperty(
97 *crow::connections::systemBus, processName, objectPath, interfaceName,
98 destProperty, propertyValue,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080099 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700100 // Use "Set" method to set the property value.
101 if (ec)
102 {
Ed Tanous62598e32023-07-17 17:06:25 -0700103 BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700104 messages::internalError(asyncResp->res);
105 return;
106 }
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000107
Ed Tanous002d39b2022-05-31 08:59:27 -0700108 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -0500109 });
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000110}
111
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500112/**
113 * ManagerResetAction class supports the POST method for the Reset (reboot)
114 * action.
115 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700116inline void requestRoutesManagerResetAction(App& app)
Jennifer Leeed5befb2018-08-10 11:29:45 -0700117{
Jennifer Leeed5befb2018-08-10 11:29:45 -0700118 /**
Jennifer Leeed5befb2018-08-10 11:29:45 -0700119 * Function handles POST method request.
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500120 * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000121 * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
Jennifer Leeed5befb2018-08-10 11:29:45 -0700122 */
Jennifer Leeed5befb2018-08-10 11:29:45 -0700123
Ed Tanous253f11b2024-05-16 09:38:31 -0700124 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/Actions/Manager.Reset/")
Ed Tanoused398212021-06-09 17:05:54 -0700125 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700126 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700127 [&app](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -0700128 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
129 const std::string& managerId) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000130 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700131 {
132 return;
133 }
Ed Tanous253f11b2024-05-16 09:38:31 -0700134 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
135 {
136 messages::resourceNotFound(asyncResp->res, "Manager", managerId);
137 return;
138 }
139
Ed Tanous62598e32023-07-17 17:06:25 -0700140 BMCWEB_LOG_DEBUG("Post Manager Reset.");
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500141
Ed Tanous002d39b2022-05-31 08:59:27 -0700142 std::string resetType;
Jennifer Leeed5befb2018-08-10 11:29:45 -0700143
Ed Tanous002d39b2022-05-31 08:59:27 -0700144 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
145 resetType))
146 {
147 return;
148 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500149
Ed Tanous002d39b2022-05-31 08:59:27 -0700150 if (resetType == "GracefulRestart")
151 {
Ed Tanous62598e32023-07-17 17:06:25 -0700152 BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
Ed Tanous002d39b2022-05-31 08:59:27 -0700153 doBMCGracefulRestart(asyncResp);
154 return;
155 }
156 if (resetType == "ForceRestart")
157 {
Ed Tanous62598e32023-07-17 17:06:25 -0700158 BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
Ed Tanous002d39b2022-05-31 08:59:27 -0700159 doBMCForceRestart(asyncResp);
160 return;
161 }
Ed Tanous62598e32023-07-17 17:06:25 -0700162 BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}", resetType);
Ed Tanous002d39b2022-05-31 08:59:27 -0700163 messages::actionParameterNotSupported(asyncResp->res, resetType,
164 "ResetType");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700165
Ed Tanous002d39b2022-05-31 08:59:27 -0700166 return;
Patrick Williams5a39f772023-10-20 11:20:21 -0500167 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700168}
Jennifer Leeed5befb2018-08-10 11:29:45 -0700169
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500170/**
171 * ManagerResetToDefaultsAction class supports POST method for factory reset
172 * action.
173 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700174inline void requestRoutesManagerResetToDefaultsAction(App& app)
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500175{
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500176 /**
177 * Function handles ResetToDefaults POST method request.
178 *
179 * Analyzes POST body message and factory resets BMC by calling
180 * BMC code updater factory reset followed by a BMC reboot.
181 *
182 * BMC code updater factory reset wipes the whole BMC read-write
183 * filesystem which includes things like the network settings.
184 *
185 * OpenBMC only supports ResetToDefaultsType "ResetAll".
186 */
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500187
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700188 BMCWEB_ROUTE(app,
Ed Tanous253f11b2024-05-16 09:38:31 -0700189 "/redfish/v1/Managers/<str>/Actions/Manager.ResetToDefaults/")
Ed Tanoused398212021-06-09 17:05:54 -0700190 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700191 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700192 [&app](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -0700193 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
194 const std::string& managerId) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000195 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700196 {
197 return;
198 }
Ed Tanous253f11b2024-05-16 09:38:31 -0700199
200 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
201 {
202 messages::resourceNotFound(asyncResp->res, "Manager", managerId);
203 return;
204 }
205
Ed Tanous62598e32023-07-17 17:06:25 -0700206 BMCWEB_LOG_DEBUG("Post ResetToDefaults.");
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500207
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300208 std::optional<std::string> resetType;
209 std::optional<std::string> resetToDefaultsType;
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500210
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300211 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
212 resetType, "ResetToDefaultsType",
213 resetToDefaultsType))
Ed Tanous002d39b2022-05-31 08:59:27 -0700214 {
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300215 BMCWEB_LOG_DEBUG("Missing property ResetType.");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700216
Ed Tanous002d39b2022-05-31 08:59:27 -0700217 messages::actionParameterMissing(asyncResp->res, "ResetToDefaults",
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300218 "ResetType");
Ed Tanous002d39b2022-05-31 08:59:27 -0700219 return;
220 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700221
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300222 if (resetToDefaultsType && !resetType)
223 {
224 BMCWEB_LOG_WARNING(
225 "Using deprecated ResetToDefaultsType, should be ResetType."
226 "Support for the ResetToDefaultsType will be dropped in 2Q24");
227 resetType = resetToDefaultsType;
228 }
229
Ed Tanous002d39b2022-05-31 08:59:27 -0700230 if (resetType != "ResetAll")
231 {
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300232 BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}",
233 *resetType);
234 messages::actionParameterNotSupported(asyncResp->res, *resetType,
235 "ResetType");
Ed Tanous002d39b2022-05-31 08:59:27 -0700236 return;
237 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700238
Ed Tanous002d39b2022-05-31 08:59:27 -0700239 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800240 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700241 if (ec)
242 {
Ed Tanous62598e32023-07-17 17:06:25 -0700243 BMCWEB_LOG_DEBUG("Failed to ResetToDefaults: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700244 messages::internalError(asyncResp->res);
245 return;
246 }
247 // Factory Reset doesn't actually happen until a reboot
248 // Can't erase what the BMC is running on
249 doBMCGracefulRestart(asyncResp);
Patrick Williams5a39f772023-10-20 11:20:21 -0500250 },
Ed Tanous002d39b2022-05-31 08:59:27 -0700251 "xyz.openbmc_project.Software.BMC.Updater",
252 "/xyz/openbmc_project/software",
253 "xyz.openbmc_project.Common.FactoryReset", "Reset");
Patrick Williams5a39f772023-10-20 11:20:21 -0500254 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700255}
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500256
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530257/**
258 * ManagerResetActionInfo derived class for delivering Manager
259 * ResetType AllowableValues using ResetInfo schema.
260 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700261inline void requestRoutesManagerResetActionInfo(App& app)
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530262{
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530263 /**
264 * Functions triggers appropriate requests on DBus
265 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700266
Ed Tanous253f11b2024-05-16 09:38:31 -0700267 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/ResetActionInfo/")
Ed Tanoused398212021-06-09 17:05:54 -0700268 .privileges(redfish::privileges::getActionInfo)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700269 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700270 [&app](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -0700271 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
272 const std::string& managerId) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000273 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700274 {
275 return;
276 }
Ed Tanous14766872022-03-15 10:44:42 -0700277
Ed Tanous253f11b2024-05-16 09:38:31 -0700278 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
279 {
280 messages::resourceNotFound(asyncResp->res, "Manager", managerId);
281 return;
282 }
283
Ed Tanous002d39b2022-05-31 08:59:27 -0700284 asyncResp->res.jsonValue["@odata.type"] =
285 "#ActionInfo.v1_1_2.ActionInfo";
286 asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -0700287 boost::urls::format("/redfish/v1/Managers/{}/ResetActionInfo",
288 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -0700289 asyncResp->res.jsonValue["Name"] = "Reset Action Info";
290 asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
291 nlohmann::json::object_t parameter;
292 parameter["Name"] = "ResetType";
293 parameter["Required"] = true;
Ed Tanous539d8c62024-06-19 14:38:27 -0700294 parameter["DataType"] = action_info::ParameterTypes::String;
Ed Tanous14766872022-03-15 10:44:42 -0700295
Ed Tanous002d39b2022-05-31 08:59:27 -0700296 nlohmann::json::array_t allowableValues;
Patrick Williamsad539542023-05-12 10:10:08 -0500297 allowableValues.emplace_back("GracefulRestart");
298 allowableValues.emplace_back("ForceRestart");
Ed Tanous002d39b2022-05-31 08:59:27 -0700299 parameter["AllowableValues"] = std::move(allowableValues);
Ed Tanous14766872022-03-15 10:44:42 -0700300
Ed Tanous002d39b2022-05-31 08:59:27 -0700301 nlohmann::json::array_t parameters;
Patrick Williamsad539542023-05-12 10:10:08 -0500302 parameters.emplace_back(std::move(parameter));
Ed Tanous14766872022-03-15 10:44:42 -0700303
Ed Tanous002d39b2022-05-31 08:59:27 -0700304 asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
Patrick Williams5a39f772023-10-20 11:20:21 -0500305 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700306}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530307
James Feist5b4aa862018-08-16 14:07:01 -0700308static constexpr const char* objectManagerIface =
309 "org.freedesktop.DBus.ObjectManager";
310static constexpr const char* pidConfigurationIface =
311 "xyz.openbmc_project.Configuration.Pid";
312static constexpr const char* pidZoneConfigurationIface =
313 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feistb7a08d02018-12-11 14:55:37 -0800314static constexpr const char* stepwiseConfigurationIface =
315 "xyz.openbmc_project.Configuration.Stepwise";
James Feist73df0db2019-03-25 15:29:35 -0700316static constexpr const char* thermalModeIface =
317 "xyz.openbmc_project.Control.ThermalMode";
Borawski.Lukasz9c3106852018-02-09 15:24:22 +0100318
zhanghch058d1b46d2021-04-01 11:18:24 +0800319inline void
320 asyncPopulatePid(const std::string& connection, const std::string& path,
321 const std::string& currentProfile,
322 const std::vector<std::string>& supportedProfiles,
323 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
James Feist5b4aa862018-08-16 14:07:01 -0700324{
George Liu5eb468d2023-06-20 17:03:24 +0800325 sdbusplus::message::object_path objPath(path);
326 dbus::utility::getManagedObjects(
327 connection, objPath,
James Feist73df0db2019-03-25 15:29:35 -0700328 [asyncResp, currentProfile, supportedProfiles](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800329 const boost::system::error_code& ec,
James Feist73df0db2019-03-25 15:29:35 -0700330 const dbus::utility::ManagedObjectType& managedObj) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700331 if (ec)
332 {
Ed Tanous62598e32023-07-17 17:06:25 -0700333 BMCWEB_LOG_ERROR("{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700334 messages::internalError(asyncResp->res);
335 return;
336 }
337 nlohmann::json& configRoot =
338 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
339 nlohmann::json& fans = configRoot["FanControllers"];
Ed Tanousfc1cdd12024-07-11 11:58:16 -0700340 fans["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager.FanControllers";
Ed Tanous253f11b2024-05-16 09:38:31 -0700341 fans["@odata.id"] = boost::urls::format(
342 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/FanControllers",
343 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -0700344
345 nlohmann::json& pids = configRoot["PidControllers"];
Ed Tanousfc1cdd12024-07-11 11:58:16 -0700346 pids["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager.PidControllers";
Ed Tanous253f11b2024-05-16 09:38:31 -0700347 pids["@odata.id"] = boost::urls::format(
348 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/PidControllers",
349 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -0700350
351 nlohmann::json& stepwise = configRoot["StepwiseControllers"];
Ed Tanousfc1cdd12024-07-11 11:58:16 -0700352 stepwise["@odata.type"] =
353 "#OpenBMCManager.v1_0_0.Manager.StepwiseControllers";
Ed Tanous253f11b2024-05-16 09:38:31 -0700354 stepwise["@odata.id"] = boost::urls::format(
355 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/StepwiseControllers",
356 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -0700357
358 nlohmann::json& zones = configRoot["FanZones"];
Ed Tanous253f11b2024-05-16 09:38:31 -0700359 zones["@odata.id"] = boost::urls::format(
360 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/FanZones",
361 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanousfc1cdd12024-07-11 11:58:16 -0700362 zones["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager.FanZones";
Ed Tanous253f11b2024-05-16 09:38:31 -0700363 configRoot["@odata.id"] =
364 boost::urls::format("/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan",
365 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanousfc1cdd12024-07-11 11:58:16 -0700366 configRoot["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager.Fan";
Ed Tanous002d39b2022-05-31 08:59:27 -0700367 configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
368
369 if (!currentProfile.empty())
370 {
371 configRoot["Profile"] = currentProfile;
372 }
Carson Labradobf2dded2023-08-10 00:37:06 +0000373 BMCWEB_LOG_DEBUG("profile = {} !", currentProfile);
Ed Tanous002d39b2022-05-31 08:59:27 -0700374
375 for (const auto& pathPair : managedObj)
376 {
377 for (const auto& intfPair : pathPair.second)
James Feist5b4aa862018-08-16 14:07:01 -0700378 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700379 if (intfPair.first != pidConfigurationIface &&
380 intfPair.first != pidZoneConfigurationIface &&
381 intfPair.first != stepwiseConfigurationIface)
James Feist5b4aa862018-08-16 14:07:01 -0700382 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700383 continue;
384 }
James Feist73df0db2019-03-25 15:29:35 -0700385
Ed Tanous002d39b2022-05-31 08:59:27 -0700386 std::string name;
James Feist73df0db2019-03-25 15:29:35 -0700387
Ed Tanous002d39b2022-05-31 08:59:27 -0700388 for (const std::pair<std::string,
389 dbus::utility::DbusVariantType>& propPair :
390 intfPair.second)
391 {
392 if (propPair.first == "Name")
James Feist73df0db2019-03-25 15:29:35 -0700393 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700394 const std::string* namePtr =
395 std::get_if<std::string>(&propPair.second);
396 if (namePtr == nullptr)
James Feist73df0db2019-03-25 15:29:35 -0700397 {
Ed Tanous62598e32023-07-17 17:06:25 -0700398 BMCWEB_LOG_ERROR("Pid Name Field illegal");
James Feistc33a90e2019-03-01 10:17:44 -0800399 messages::internalError(asyncResp->res);
400 return;
401 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700402 name = *namePtr;
403 dbus::utility::escapePathForDbus(name);
James Feistb7a08d02018-12-11 14:55:37 -0800404 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700405 else if (propPair.first == "Profiles")
James Feistb7a08d02018-12-11 14:55:37 -0800406 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700407 const std::vector<std::string>* profiles =
408 std::get_if<std::vector<std::string>>(
409 &propPair.second);
410 if (profiles == nullptr)
James Feistb7a08d02018-12-11 14:55:37 -0800411 {
Ed Tanous62598e32023-07-17 17:06:25 -0700412 BMCWEB_LOG_ERROR("Pid Profiles Field illegal");
James Feistb7a08d02018-12-11 14:55:37 -0800413 messages::internalError(asyncResp->res);
414 return;
415 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700416 if (std::find(profiles->begin(), profiles->end(),
417 currentProfile) == profiles->end())
James Feistb7a08d02018-12-11 14:55:37 -0800418 {
Ed Tanous62598e32023-07-17 17:06:25 -0700419 BMCWEB_LOG_INFO(
420 "{} not supported in current profile", name);
Ed Tanous002d39b2022-05-31 08:59:27 -0700421 continue;
James Feistb7a08d02018-12-11 14:55:37 -0800422 }
423 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700424 }
425 nlohmann::json* config = nullptr;
426 const std::string* classPtr = nullptr;
427
428 for (const std::pair<std::string,
429 dbus::utility::DbusVariantType>& propPair :
430 intfPair.second)
431 {
432 if (propPair.first == "Class")
James Feistb7a08d02018-12-11 14:55:37 -0800433 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700434 classPtr = std::get_if<std::string>(&propPair.second);
435 }
436 }
437
Ed Tanous253f11b2024-05-16 09:38:31 -0700438 boost::urls::url url(
439 boost::urls::format("/redfish/v1/Managers/{}",
440 BMCWEB_REDFISH_MANAGER_URI_NAME));
Ed Tanous002d39b2022-05-31 08:59:27 -0700441 if (intfPair.first == pidZoneConfigurationIface)
442 {
443 std::string chassis;
444 if (!dbus::utility::getNthStringFromPath(pathPair.first.str,
445 5, chassis))
446 {
447 chassis = "#IllegalValue";
448 }
449 nlohmann::json& zone = zones[name];
Ed Tanousef4c65b2023-04-24 15:28:50 -0700450 zone["Chassis"]["@odata.id"] =
451 boost::urls::format("/redfish/v1/Chassis/{}", chassis);
Willy Tueddfc432022-09-26 16:46:38 +0000452 url.set_fragment(
453 ("/Oem/OpenBmc/Fan/FanZones"_json_pointer / name)
454 .to_string());
455 zone["@odata.id"] = std::move(url);
Ed Tanousfc1cdd12024-07-11 11:58:16 -0700456 zone["@odata.type"] =
457 "#OpenBMCManager.v1_0_0.Manager.FanZone";
Ed Tanous002d39b2022-05-31 08:59:27 -0700458 config = &zone;
459 }
460
461 else if (intfPair.first == stepwiseConfigurationIface)
462 {
463 if (classPtr == nullptr)
464 {
Ed Tanous62598e32023-07-17 17:06:25 -0700465 BMCWEB_LOG_ERROR("Pid Class Field illegal");
James Feistb7a08d02018-12-11 14:55:37 -0800466 messages::internalError(asyncResp->res);
467 return;
468 }
469
Ed Tanous002d39b2022-05-31 08:59:27 -0700470 nlohmann::json& controller = stepwise[name];
471 config = &controller;
Willy Tueddfc432022-09-26 16:46:38 +0000472 url.set_fragment(
473 ("/Oem/OpenBmc/Fan/StepwiseControllers"_json_pointer /
474 name)
475 .to_string());
476 controller["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700477 controller["@odata.type"] =
Ed Tanousfc1cdd12024-07-11 11:58:16 -0700478 "#OpenBMCManager.v1_0_0.Manager.StepwiseController";
Ed Tanous002d39b2022-05-31 08:59:27 -0700479
480 controller["Direction"] = *classPtr;
481 }
482
483 // pid and fans are off the same configuration
484 else if (intfPair.first == pidConfigurationIface)
485 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700486 if (classPtr == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700487 {
Ed Tanous62598e32023-07-17 17:06:25 -0700488 BMCWEB_LOG_ERROR("Pid Class Field illegal");
Ed Tanous002d39b2022-05-31 08:59:27 -0700489 messages::internalError(asyncResp->res);
490 return;
491 }
492 bool isFan = *classPtr == "fan";
493 nlohmann::json& element = isFan ? fans[name] : pids[name];
494 config = &element;
495 if (isFan)
496 {
Willy Tueddfc432022-09-26 16:46:38 +0000497 url.set_fragment(
498 ("/Oem/OpenBmc/Fan/FanControllers"_json_pointer /
499 name)
500 .to_string());
501 element["@odata.id"] = std::move(url);
Ed Tanousfc1cdd12024-07-11 11:58:16 -0700502 element["@odata.type"] =
503 "#OpenBMCManager.v1_0_0.Manager.FanController";
Ed Tanous002d39b2022-05-31 08:59:27 -0700504 }
505 else
506 {
Willy Tueddfc432022-09-26 16:46:38 +0000507 url.set_fragment(
508 ("/Oem/OpenBmc/Fan/PidControllers"_json_pointer /
509 name)
510 .to_string());
511 element["@odata.id"] = std::move(url);
Ed Tanousfc1cdd12024-07-11 11:58:16 -0700512 element["@odata.type"] =
513 "#OpenBMCManager.v1_0_0.Manager.PidController";
Ed Tanous002d39b2022-05-31 08:59:27 -0700514 }
515 }
516 else
517 {
Ed Tanous62598e32023-07-17 17:06:25 -0700518 BMCWEB_LOG_ERROR("Unexpected configuration");
Ed Tanous002d39b2022-05-31 08:59:27 -0700519 messages::internalError(asyncResp->res);
520 return;
521 }
James Feist5b4aa862018-08-16 14:07:01 -0700522
Ed Tanous002d39b2022-05-31 08:59:27 -0700523 // used for making maps out of 2 vectors
524 const std::vector<double>* keys = nullptr;
525 const std::vector<double>* values = nullptr;
526
527 for (const auto& propertyPair : intfPair.second)
528 {
529 if (propertyPair.first == "Type" ||
530 propertyPair.first == "Class" ||
531 propertyPair.first == "Name")
532 {
533 continue;
534 }
535
536 // zones
537 if (intfPair.first == pidZoneConfigurationIface)
538 {
539 const double* ptr =
540 std::get_if<double>(&propertyPair.second);
541 if (ptr == nullptr)
542 {
Ed Tanous62598e32023-07-17 17:06:25 -0700543 BMCWEB_LOG_ERROR("Field Illegal {}",
544 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700545 messages::internalError(asyncResp->res);
546 return;
547 }
548 (*config)[propertyPair.first] = *ptr;
549 }
550
551 if (intfPair.first == stepwiseConfigurationIface)
552 {
553 if (propertyPair.first == "Reading" ||
554 propertyPair.first == "Output")
555 {
556 const std::vector<double>* ptr =
557 std::get_if<std::vector<double>>(
558 &propertyPair.second);
559
560 if (ptr == nullptr)
561 {
Ed Tanous62598e32023-07-17 17:06:25 -0700562 BMCWEB_LOG_ERROR("Field Illegal {}",
563 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700564 messages::internalError(asyncResp->res);
565 return;
566 }
567
568 if (propertyPair.first == "Reading")
569 {
570 keys = ptr;
571 }
572 else
573 {
574 values = ptr;
575 }
576 if (keys != nullptr && values != nullptr)
577 {
578 if (keys->size() != values->size())
579 {
Ed Tanous62598e32023-07-17 17:06:25 -0700580 BMCWEB_LOG_ERROR(
581 "Reading and Output size don't match ");
Ed Tanous002d39b2022-05-31 08:59:27 -0700582 messages::internalError(asyncResp->res);
583 return;
584 }
585 nlohmann::json& steps = (*config)["Steps"];
586 steps = nlohmann::json::array();
587 for (size_t ii = 0; ii < keys->size(); ii++)
588 {
589 nlohmann::json::object_t step;
590 step["Target"] = (*keys)[ii];
591 step["Output"] = (*values)[ii];
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500592 steps.emplace_back(std::move(step));
Ed Tanous002d39b2022-05-31 08:59:27 -0700593 }
594 }
595 }
596 if (propertyPair.first == "NegativeHysteresis" ||
597 propertyPair.first == "PositiveHysteresis")
James Feist5b4aa862018-08-16 14:07:01 -0700598 {
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800599 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800600 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700601 if (ptr == nullptr)
602 {
Ed Tanous62598e32023-07-17 17:06:25 -0700603 BMCWEB_LOG_ERROR("Field Illegal {}",
604 propertyPair.first);
Jason M. Billsf12894f2018-10-09 12:45:45 -0700605 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700606 return;
607 }
James Feistb7a08d02018-12-11 14:55:37 -0800608 (*config)[propertyPair.first] = *ptr;
609 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700610 }
James Feistb7a08d02018-12-11 14:55:37 -0800611
Ed Tanous002d39b2022-05-31 08:59:27 -0700612 // pid and fans are off the same configuration
613 if (intfPair.first == pidConfigurationIface ||
614 intfPair.first == stepwiseConfigurationIface)
615 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700616 if (propertyPair.first == "Zones")
James Feistb7a08d02018-12-11 14:55:37 -0800617 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700618 const std::vector<std::string>* inputs =
619 std::get_if<std::vector<std::string>>(
620 &propertyPair.second);
621
622 if (inputs == nullptr)
James Feistb7a08d02018-12-11 14:55:37 -0800623 {
Ed Tanous62598e32023-07-17 17:06:25 -0700624 BMCWEB_LOG_ERROR("Zones Pid Field Illegal");
Ed Tanous002d39b2022-05-31 08:59:27 -0700625 messages::internalError(asyncResp->res);
626 return;
James Feistb7a08d02018-12-11 14:55:37 -0800627 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700628 auto& data = (*config)[propertyPair.first];
629 data = nlohmann::json::array();
630 for (std::string itemCopy : *inputs)
James Feistb7a08d02018-12-11 14:55:37 -0800631 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700632 dbus::utility::escapePathForDbus(itemCopy);
633 nlohmann::json::object_t input;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700634 boost::urls::url managerUrl = boost::urls::format(
Ed Tanous253f11b2024-05-16 09:38:31 -0700635 "/redfish/v1/Managers/{}#{}",
636 BMCWEB_REDFISH_MANAGER_URI_NAME,
Willy Tueddfc432022-09-26 16:46:38 +0000637 ("/Oem/OpenBmc/Fan/FanZones"_json_pointer /
638 itemCopy)
639 .to_string());
640 input["@odata.id"] = std::move(managerUrl);
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500641 data.emplace_back(std::move(input));
James Feistb7a08d02018-12-11 14:55:37 -0800642 }
James Feist5b4aa862018-08-16 14:07:01 -0700643 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700644 // todo(james): may never happen, but this
645 // assumes configuration data referenced in the
646 // PID config is provided by the same daemon, we
647 // could add another loop to cover all cases,
648 // but I'm okay kicking this can down the road a
649 // bit
James Feist5b4aa862018-08-16 14:07:01 -0700650
Ed Tanous002d39b2022-05-31 08:59:27 -0700651 else if (propertyPair.first == "Inputs" ||
652 propertyPair.first == "Outputs")
James Feist5b4aa862018-08-16 14:07:01 -0700653 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700654 auto& data = (*config)[propertyPair.first];
655 const std::vector<std::string>* inputs =
656 std::get_if<std::vector<std::string>>(
657 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700658
Ed Tanous002d39b2022-05-31 08:59:27 -0700659 if (inputs == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700660 {
Ed Tanous62598e32023-07-17 17:06:25 -0700661 BMCWEB_LOG_ERROR("Field Illegal {}",
662 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700663 messages::internalError(asyncResp->res);
664 return;
James Feist5b4aa862018-08-16 14:07:01 -0700665 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700666 data = *inputs;
667 }
668 else if (propertyPair.first == "SetPointOffset")
669 {
670 const std::string* ptr =
671 std::get_if<std::string>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700672
Ed Tanous002d39b2022-05-31 08:59:27 -0700673 if (ptr == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700674 {
Ed Tanous62598e32023-07-17 17:06:25 -0700675 BMCWEB_LOG_ERROR("Field Illegal {}",
676 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700677 messages::internalError(asyncResp->res);
678 return;
James Feistb943aae2019-07-11 16:33:56 -0700679 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700680 // translate from dbus to redfish
681 if (*ptr == "WarningHigh")
James Feistb943aae2019-07-11 16:33:56 -0700682 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700683 (*config)["SetPointOffset"] =
684 "UpperThresholdNonCritical";
James Feistb943aae2019-07-11 16:33:56 -0700685 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700686 else if (*ptr == "WarningLow")
James Feist5b4aa862018-08-16 14:07:01 -0700687 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700688 (*config)["SetPointOffset"] =
689 "LowerThresholdNonCritical";
James Feist5b4aa862018-08-16 14:07:01 -0700690 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700691 else if (*ptr == "CriticalHigh")
692 {
693 (*config)["SetPointOffset"] =
694 "UpperThresholdCritical";
695 }
696 else if (*ptr == "CriticalLow")
697 {
698 (*config)["SetPointOffset"] =
699 "LowerThresholdCritical";
700 }
701 else
702 {
Ed Tanous62598e32023-07-17 17:06:25 -0700703 BMCWEB_LOG_ERROR("Value Illegal {}", *ptr);
Ed Tanous002d39b2022-05-31 08:59:27 -0700704 messages::internalError(asyncResp->res);
705 return;
706 }
707 }
708 // doubles
709 else if (propertyPair.first == "FFGainCoefficient" ||
710 propertyPair.first == "FFOffCoefficient" ||
711 propertyPair.first == "ICoefficient" ||
712 propertyPair.first == "ILimitMax" ||
713 propertyPair.first == "ILimitMin" ||
714 propertyPair.first == "PositiveHysteresis" ||
715 propertyPair.first == "NegativeHysteresis" ||
716 propertyPair.first == "OutLimitMax" ||
717 propertyPair.first == "OutLimitMin" ||
718 propertyPair.first == "PCoefficient" ||
719 propertyPair.first == "SetPoint" ||
720 propertyPair.first == "SlewNeg" ||
721 propertyPair.first == "SlewPos")
722 {
723 const double* ptr =
724 std::get_if<double>(&propertyPair.second);
725 if (ptr == nullptr)
726 {
Ed Tanous62598e32023-07-17 17:06:25 -0700727 BMCWEB_LOG_ERROR("Field Illegal {}",
728 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700729 messages::internalError(asyncResp->res);
730 return;
731 }
732 (*config)[propertyPair.first] = *ptr;
James Feist5b4aa862018-08-16 14:07:01 -0700733 }
734 }
735 }
736 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700737 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500738 });
James Feist5b4aa862018-08-16 14:07:01 -0700739}
Jennifer Leeca537922018-08-10 10:07:30 -0700740
James Feist83ff9ab2018-08-31 10:18:24 -0700741enum class CreatePIDRet
742{
743 fail,
744 del,
745 patch
746};
747
zhanghch058d1b46d2021-04-01 11:18:24 +0800748inline bool
749 getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
Ed Tanous9e9b6042024-03-06 14:18:28 -0800750 std::vector<nlohmann::json::object_t>& config,
zhanghch058d1b46d2021-04-01 11:18:24 +0800751 std::vector<std::string>& zones)
James Feist5f2caae2018-12-12 14:08:25 -0800752{
James Feistb6baeaa2019-02-21 10:41:40 -0800753 if (config.empty())
754 {
Ed Tanous62598e32023-07-17 17:06:25 -0700755 BMCWEB_LOG_ERROR("Empty Zones");
Ed Tanousf818b042022-06-27 13:17:35 -0700756 messages::propertyValueFormatError(response->res, config, "Zones");
James Feistb6baeaa2019-02-21 10:41:40 -0800757 return false;
758 }
James Feist5f2caae2018-12-12 14:08:25 -0800759 for (auto& odata : config)
760 {
761 std::string path;
Ed Tanous9e9b6042024-03-06 14:18:28 -0800762 if (!redfish::json_util::readJsonObject(odata, response->res,
763 "@odata.id", path))
James Feist5f2caae2018-12-12 14:08:25 -0800764 {
765 return false;
766 }
767 std::string input;
James Feist61adbda2019-03-25 13:03:51 -0700768
769 // 8 below comes from
770 // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
771 // 0 1 2 3 4 5 6 7 8
772 if (!dbus::utility::getNthStringFromPath(path, 8, input))
James Feist5f2caae2018-12-12 14:08:25 -0800773 {
Ed Tanous62598e32023-07-17 17:06:25 -0700774 BMCWEB_LOG_ERROR("Got invalid path {}", path);
775 BMCWEB_LOG_ERROR("Illegal Type Zones");
Ed Tanousf818b042022-06-27 13:17:35 -0700776 messages::propertyValueFormatError(response->res, odata, "Zones");
James Feist5f2caae2018-12-12 14:08:25 -0800777 return false;
778 }
Ed Tanousa170f272022-06-30 21:53:27 -0700779 std::replace(input.begin(), input.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -0800780 zones.emplace_back(std::move(input));
781 }
782 return true;
783}
784
Ed Tanous711ac7a2021-12-20 09:34:41 -0800785inline const dbus::utility::ManagedObjectType::value_type*
James Feist73df0db2019-03-25 15:29:35 -0700786 findChassis(const dbus::utility::ManagedObjectType& managedObj,
Ed Tanous9e9b6042024-03-06 14:18:28 -0800787 std::string_view value, std::string& chassis)
James Feistb6baeaa2019-02-21 10:41:40 -0800788{
Ed Tanous62598e32023-07-17 17:06:25 -0700789 BMCWEB_LOG_DEBUG("Find Chassis: {}", value);
James Feistb6baeaa2019-02-21 10:41:40 -0800790
Ed Tanous9e9b6042024-03-06 14:18:28 -0800791 std::string escaped(value);
Yaswanth Reddy M6ce82fa2023-03-10 07:29:45 +0000792 std::replace(escaped.begin(), escaped.end(), ' ', '_');
James Feistb6baeaa2019-02-21 10:41:40 -0800793 escaped = "/" + escaped;
Ed Tanous3544d2a2023-08-06 18:12:20 -0700794 auto it = std::ranges::find_if(managedObj, [&escaped](const auto& obj) {
Ed Tanous18f8f602023-07-18 10:07:23 -0700795 if (obj.first.str.ends_with(escaped))
Ed Tanous002d39b2022-05-31 08:59:27 -0700796 {
Ed Tanous62598e32023-07-17 17:06:25 -0700797 BMCWEB_LOG_DEBUG("Matched {}", obj.first.str);
Ed Tanous002d39b2022-05-31 08:59:27 -0700798 return true;
799 }
800 return false;
801 });
James Feistb6baeaa2019-02-21 10:41:40 -0800802
803 if (it == managedObj.end())
804 {
James Feist73df0db2019-03-25 15:29:35 -0700805 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800806 }
807 // 5 comes from <chassis-name> being the 5th element
808 // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
James Feist73df0db2019-03-25 15:29:35 -0700809 if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
810 {
811 return &(*it);
812 }
813
814 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800815}
816
Ed Tanous23a21a12020-07-25 04:45:05 +0000817inline CreatePIDRet createPidInterface(
zhanghch058d1b46d2021-04-01 11:18:24 +0800818 const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
Ed Tanous9e9b6042024-03-06 14:18:28 -0800819 std::string_view name, nlohmann::json& jsonValue, const std::string& path,
James Feist83ff9ab2018-08-31 10:18:24 -0700820 const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800821 dbus::utility::DBusPropertiesMap& output, std::string& chassis,
822 const std::string& profile)
James Feist83ff9ab2018-08-31 10:18:24 -0700823{
James Feist5f2caae2018-12-12 14:08:25 -0800824 // common deleter
Ed Tanous9e9b6042024-03-06 14:18:28 -0800825 if (jsonValue == nullptr)
James Feist5f2caae2018-12-12 14:08:25 -0800826 {
827 std::string iface;
828 if (type == "PidControllers" || type == "FanControllers")
829 {
830 iface = pidConfigurationIface;
831 }
832 else if (type == "FanZones")
833 {
834 iface = pidZoneConfigurationIface;
835 }
836 else if (type == "StepwiseControllers")
837 {
838 iface = stepwiseConfigurationIface;
839 }
840 else
841 {
Ed Tanous62598e32023-07-17 17:06:25 -0700842 BMCWEB_LOG_ERROR("Illegal Type {}", type);
James Feist5f2caae2018-12-12 14:08:25 -0800843 messages::propertyUnknown(response->res, type);
844 return CreatePIDRet::fail;
845 }
James Feist6ee7f772020-02-06 16:25:27 -0800846
Ed Tanous62598e32023-07-17 17:06:25 -0700847 BMCWEB_LOG_DEBUG("del {} {}", path, iface);
James Feist5f2caae2018-12-12 14:08:25 -0800848 // delete interface
849 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800850 [response, path](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700851 if (ec)
852 {
Ed Tanous62598e32023-07-17 17:06:25 -0700853 BMCWEB_LOG_ERROR("Error patching {}: {}", path, ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700854 messages::internalError(response->res);
855 return;
856 }
857 messages::success(response->res);
Patrick Williams5a39f772023-10-20 11:20:21 -0500858 },
James Feist5f2caae2018-12-12 14:08:25 -0800859 "xyz.openbmc_project.EntityManager", path, iface, "Delete");
860 return CreatePIDRet::del;
861 }
862
Ed Tanous711ac7a2021-12-20 09:34:41 -0800863 const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800864 if (!createNewObject)
865 {
866 // if we aren't creating a new object, we should be able to find it on
867 // d-bus
Ed Tanous9e9b6042024-03-06 14:18:28 -0800868 managedItem = findChassis(managedObj, name, chassis);
James Feist73df0db2019-03-25 15:29:35 -0700869 if (managedItem == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800870 {
Ed Tanous62598e32023-07-17 17:06:25 -0700871 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousef4c65b2023-04-24 15:28:50 -0700872 messages::invalidObject(
873 response->res,
874 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800875 return CreatePIDRet::fail;
876 }
877 }
878
Ed Tanous26f69762022-01-25 09:49:11 -0800879 if (!profile.empty() &&
James Feist73df0db2019-03-25 15:29:35 -0700880 (type == "PidControllers" || type == "FanControllers" ||
881 type == "StepwiseControllers"))
882 {
883 if (managedItem == nullptr)
884 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800885 output.emplace_back("Profiles", std::vector<std::string>{profile});
James Feist73df0db2019-03-25 15:29:35 -0700886 }
887 else
888 {
889 std::string interface;
890 if (type == "StepwiseControllers")
891 {
892 interface = stepwiseConfigurationIface;
893 }
894 else
895 {
896 interface = pidConfigurationIface;
897 }
Ed Tanous711ac7a2021-12-20 09:34:41 -0800898 bool ifaceFound = false;
899 for (const auto& iface : managedItem->second)
900 {
901 if (iface.first == interface)
902 {
903 ifaceFound = true;
904 for (const auto& prop : iface.second)
905 {
906 if (prop.first == "Profiles")
907 {
908 const std::vector<std::string>* curProfiles =
909 std::get_if<std::vector<std::string>>(
910 &(prop.second));
911 if (curProfiles == nullptr)
912 {
Ed Tanous62598e32023-07-17 17:06:25 -0700913 BMCWEB_LOG_ERROR(
914 "Illegal profiles in managed object");
Ed Tanous711ac7a2021-12-20 09:34:41 -0800915 messages::internalError(response->res);
916 return CreatePIDRet::fail;
917 }
918 if (std::find(curProfiles->begin(),
919 curProfiles->end(),
920 profile) == curProfiles->end())
921 {
922 std::vector<std::string> newProfiles =
923 *curProfiles;
924 newProfiles.push_back(profile);
Ed Tanousb9d36b42022-02-26 21:42:46 -0800925 output.emplace_back("Profiles", newProfiles);
Ed Tanous711ac7a2021-12-20 09:34:41 -0800926 }
927 }
928 }
929 }
930 }
931
932 if (!ifaceFound)
James Feist73df0db2019-03-25 15:29:35 -0700933 {
Ed Tanous62598e32023-07-17 17:06:25 -0700934 BMCWEB_LOG_ERROR("Failed to find interface in managed object");
James Feist73df0db2019-03-25 15:29:35 -0700935 messages::internalError(response->res);
936 return CreatePIDRet::fail;
937 }
James Feist73df0db2019-03-25 15:29:35 -0700938 }
939 }
940
James Feist83ff9ab2018-08-31 10:18:24 -0700941 if (type == "PidControllers" || type == "FanControllers")
942 {
943 if (createNewObject)
944 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800945 output.emplace_back("Class",
946 type == "PidControllers" ? "temp" : "fan");
947 output.emplace_back("Type", "Pid");
James Feist83ff9ab2018-08-31 10:18:24 -0700948 }
James Feist5f2caae2018-12-12 14:08:25 -0800949
Ed Tanous9e9b6042024-03-06 14:18:28 -0800950 std::optional<std::vector<nlohmann::json::object_t>> zones;
James Feist5f2caae2018-12-12 14:08:25 -0800951 std::optional<std::vector<std::string>> inputs;
952 std::optional<std::vector<std::string>> outputs;
953 std::map<std::string, std::optional<double>> doubles;
James Feistb943aae2019-07-11 16:33:56 -0700954 std::optional<std::string> setpointOffset;
James Feist5f2caae2018-12-12 14:08:25 -0800955 if (!redfish::json_util::readJson(
Ed Tanous9e9b6042024-03-06 14:18:28 -0800956 jsonValue, response->res, "Inputs", inputs, "Outputs", outputs,
James Feist5f2caae2018-12-12 14:08:25 -0800957 "Zones", zones, "FFGainCoefficient",
958 doubles["FFGainCoefficient"], "FFOffCoefficient",
959 doubles["FFOffCoefficient"], "ICoefficient",
960 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
961 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
962 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
963 "PCoefficient", doubles["PCoefficient"], "SetPoint",
James Feistb943aae2019-07-11 16:33:56 -0700964 doubles["SetPoint"], "SetPointOffset", setpointOffset,
965 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
966 "PositiveHysteresis", doubles["PositiveHysteresis"],
967 "NegativeHysteresis", doubles["NegativeHysteresis"]))
James Feist83ff9ab2018-08-31 10:18:24 -0700968 {
James Feist5f2caae2018-12-12 14:08:25 -0800969 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700970 }
James Feist5f2caae2018-12-12 14:08:25 -0800971 if (zones)
James Feist83ff9ab2018-08-31 10:18:24 -0700972 {
James Feist5f2caae2018-12-12 14:08:25 -0800973 std::vector<std::string> zonesStr;
974 if (!getZonesFromJsonReq(response, *zones, zonesStr))
James Feist83ff9ab2018-08-31 10:18:24 -0700975 {
Ed Tanous62598e32023-07-17 17:06:25 -0700976 BMCWEB_LOG_ERROR("Illegal Zones");
James Feist5f2caae2018-12-12 14:08:25 -0800977 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700978 }
James Feistb6baeaa2019-02-21 10:41:40 -0800979 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -0800980 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800981 {
Ed Tanous62598e32023-07-17 17:06:25 -0700982 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousace85d62021-10-26 12:45:59 -0700983 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -0700984 response->res,
985 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800986 return CreatePIDRet::fail;
987 }
Ed Tanousb9d36b42022-02-26 21:42:46 -0800988 output.emplace_back("Zones", std::move(zonesStr));
James Feist5f2caae2018-12-12 14:08:25 -0800989 }
Ed Tanousafb9ee02022-12-21 11:59:17 -0800990
991 if (inputs)
James Feist5f2caae2018-12-12 14:08:25 -0800992 {
Ed Tanousafb9ee02022-12-21 11:59:17 -0800993 for (std::string& value : *inputs)
James Feist83ff9ab2018-08-31 10:18:24 -0700994 {
Ed Tanousafb9ee02022-12-21 11:59:17 -0800995 std::replace(value.begin(), value.end(), '_', ' ');
James Feist83ff9ab2018-08-31 10:18:24 -0700996 }
Ed Tanousafb9ee02022-12-21 11:59:17 -0800997 output.emplace_back("Inputs", *inputs);
998 }
999
1000 if (outputs)
1001 {
1002 for (std::string& value : *outputs)
1003 {
1004 std::replace(value.begin(), value.end(), '_', ' ');
1005 }
1006 output.emplace_back("Outputs", *outputs);
James Feist5f2caae2018-12-12 14:08:25 -08001007 }
James Feist83ff9ab2018-08-31 10:18:24 -07001008
James Feistb943aae2019-07-11 16:33:56 -07001009 if (setpointOffset)
1010 {
1011 // translate between redfish and dbus names
1012 if (*setpointOffset == "UpperThresholdNonCritical")
1013 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001014 output.emplace_back("SetPointOffset", "WarningLow");
James Feistb943aae2019-07-11 16:33:56 -07001015 }
1016 else if (*setpointOffset == "LowerThresholdNonCritical")
1017 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001018 output.emplace_back("SetPointOffset", "WarningHigh");
James Feistb943aae2019-07-11 16:33:56 -07001019 }
1020 else if (*setpointOffset == "LowerThresholdCritical")
1021 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001022 output.emplace_back("SetPointOffset", "CriticalLow");
James Feistb943aae2019-07-11 16:33:56 -07001023 }
1024 else if (*setpointOffset == "UpperThresholdCritical")
1025 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001026 output.emplace_back("SetPointOffset", "CriticalHigh");
James Feistb943aae2019-07-11 16:33:56 -07001027 }
1028 else
1029 {
Ed Tanous62598e32023-07-17 17:06:25 -07001030 BMCWEB_LOG_ERROR("Invalid setpointoffset {}", *setpointOffset);
Ed Tanous9e9b6042024-03-06 14:18:28 -08001031 messages::propertyValueNotInList(response->res, name,
Ed Tanousace85d62021-10-26 12:45:59 -07001032 "SetPointOffset");
James Feistb943aae2019-07-11 16:33:56 -07001033 return CreatePIDRet::fail;
1034 }
1035 }
1036
James Feist5f2caae2018-12-12 14:08:25 -08001037 // doubles
1038 for (const auto& pairs : doubles)
1039 {
1040 if (!pairs.second)
James Feist83ff9ab2018-08-31 10:18:24 -07001041 {
James Feist5f2caae2018-12-12 14:08:25 -08001042 continue;
James Feist83ff9ab2018-08-31 10:18:24 -07001043 }
Ed Tanous62598e32023-07-17 17:06:25 -07001044 BMCWEB_LOG_DEBUG("{} = {}", pairs.first, *pairs.second);
Ed Tanousb9d36b42022-02-26 21:42:46 -08001045 output.emplace_back(pairs.first, *pairs.second);
James Feist83ff9ab2018-08-31 10:18:24 -07001046 }
1047 }
James Feist5f2caae2018-12-12 14:08:25 -08001048
James Feist83ff9ab2018-08-31 10:18:24 -07001049 else if (type == "FanZones")
1050 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001051 output.emplace_back("Type", "Pid.Zone");
James Feist83ff9ab2018-08-31 10:18:24 -07001052
Ed Tanous9e9b6042024-03-06 14:18:28 -08001053 std::optional<std::string> chassisId;
James Feist5f2caae2018-12-12 14:08:25 -08001054 std::optional<double> failSafePercent;
James Feistd3ec07f2019-02-25 14:51:15 -08001055 std::optional<double> minThermalOutput;
Ed Tanous9e9b6042024-03-06 14:18:28 -08001056 if (!redfish::json_util::readJson(jsonValue, response->res,
1057 "Chassis/@odata.id", chassisId,
1058 "FailSafePercent", failSafePercent,
1059 "MinThermalOutput", minThermalOutput))
James Feist83ff9ab2018-08-31 10:18:24 -07001060 {
James Feist5f2caae2018-12-12 14:08:25 -08001061 return CreatePIDRet::fail;
1062 }
James Feist83ff9ab2018-08-31 10:18:24 -07001063
Ed Tanous9e9b6042024-03-06 14:18:28 -08001064 if (chassisId)
James Feist5f2caae2018-12-12 14:08:25 -08001065 {
AppaRao Puli717794d2019-10-18 22:54:53 +05301066 // /redfish/v1/chassis/chassis_name/
Ed Tanous9e9b6042024-03-06 14:18:28 -08001067 if (!dbus::utility::getNthStringFromPath(*chassisId, 3, chassis))
James Feist5f2caae2018-12-12 14:08:25 -08001068 {
Ed Tanous9e9b6042024-03-06 14:18:28 -08001069 BMCWEB_LOG_ERROR("Got invalid path {}", *chassisId);
Ed Tanousace85d62021-10-26 12:45:59 -07001070 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001071 response->res,
Ed Tanous9e9b6042024-03-06 14:18:28 -08001072 boost::urls::format("/redfish/v1/Chassis/{}", *chassisId));
James Feist5f2caae2018-12-12 14:08:25 -08001073 return CreatePIDRet::fail;
1074 }
1075 }
James Feistd3ec07f2019-02-25 14:51:15 -08001076 if (minThermalOutput)
James Feist5f2caae2018-12-12 14:08:25 -08001077 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001078 output.emplace_back("MinThermalOutput", *minThermalOutput);
James Feist5f2caae2018-12-12 14:08:25 -08001079 }
1080 if (failSafePercent)
1081 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001082 output.emplace_back("FailSafePercent", *failSafePercent);
James Feist5f2caae2018-12-12 14:08:25 -08001083 }
1084 }
1085 else if (type == "StepwiseControllers")
1086 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001087 output.emplace_back("Type", "Stepwise");
James Feist5f2caae2018-12-12 14:08:25 -08001088
Ed Tanous9e9b6042024-03-06 14:18:28 -08001089 std::optional<std::vector<nlohmann::json::object_t>> zones;
1090 std::optional<std::vector<nlohmann::json::object_t>> steps;
James Feist5f2caae2018-12-12 14:08:25 -08001091 std::optional<std::vector<std::string>> inputs;
1092 std::optional<double> positiveHysteresis;
1093 std::optional<double> negativeHysteresis;
James Feistc33a90e2019-03-01 10:17:44 -08001094 std::optional<std::string> direction; // upper clipping curve vs lower
James Feist5f2caae2018-12-12 14:08:25 -08001095 if (!redfish::json_util::readJson(
Ed Tanous9e9b6042024-03-06 14:18:28 -08001096 jsonValue, response->res, "Zones", zones, "Steps", steps,
James Feistb6baeaa2019-02-21 10:41:40 -08001097 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
James Feistc33a90e2019-03-01 10:17:44 -08001098 "NegativeHysteresis", negativeHysteresis, "Direction",
1099 direction))
James Feist5f2caae2018-12-12 14:08:25 -08001100 {
James Feist5f2caae2018-12-12 14:08:25 -08001101 return CreatePIDRet::fail;
1102 }
1103
1104 if (zones)
1105 {
James Feistb6baeaa2019-02-21 10:41:40 -08001106 std::vector<std::string> zonesStrs;
1107 if (!getZonesFromJsonReq(response, *zones, zonesStrs))
James Feist5f2caae2018-12-12 14:08:25 -08001108 {
Ed Tanous62598e32023-07-17 17:06:25 -07001109 BMCWEB_LOG_ERROR("Illegal Zones");
James Feist5f2caae2018-12-12 14:08:25 -08001110 return CreatePIDRet::fail;
1111 }
James Feistb6baeaa2019-02-21 10:41:40 -08001112 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -08001113 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -08001114 {
Ed Tanous62598e32023-07-17 17:06:25 -07001115 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousace85d62021-10-26 12:45:59 -07001116 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001117 response->res,
1118 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -08001119 return CreatePIDRet::fail;
1120 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001121 output.emplace_back("Zones", std::move(zonesStrs));
James Feist5f2caae2018-12-12 14:08:25 -08001122 }
1123 if (steps)
1124 {
1125 std::vector<double> readings;
1126 std::vector<double> outputs;
1127 for (auto& step : *steps)
1128 {
Ed Tanous543f4402022-01-06 13:12:53 -08001129 double target = 0.0;
1130 double out = 0.0;
James Feist5f2caae2018-12-12 14:08:25 -08001131
Ed Tanous9e9b6042024-03-06 14:18:28 -08001132 if (!redfish::json_util::readJsonObject(
1133 step, response->res, "Target", target, "Output", out))
James Feist5f2caae2018-12-12 14:08:25 -08001134 {
James Feist5f2caae2018-12-12 14:08:25 -08001135 return CreatePIDRet::fail;
1136 }
1137 readings.emplace_back(target);
Ed Tanous23a21a12020-07-25 04:45:05 +00001138 outputs.emplace_back(out);
James Feist5f2caae2018-12-12 14:08:25 -08001139 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001140 output.emplace_back("Reading", std::move(readings));
1141 output.emplace_back("Output", std::move(outputs));
James Feist5f2caae2018-12-12 14:08:25 -08001142 }
1143 if (inputs)
1144 {
1145 for (std::string& value : *inputs)
1146 {
Ed Tanousa170f272022-06-30 21:53:27 -07001147 std::replace(value.begin(), value.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -08001148 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001149 output.emplace_back("Inputs", std::move(*inputs));
James Feist5f2caae2018-12-12 14:08:25 -08001150 }
1151 if (negativeHysteresis)
1152 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001153 output.emplace_back("NegativeHysteresis", *negativeHysteresis);
James Feist5f2caae2018-12-12 14:08:25 -08001154 }
1155 if (positiveHysteresis)
1156 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001157 output.emplace_back("PositiveHysteresis", *positiveHysteresis);
James Feist83ff9ab2018-08-31 10:18:24 -07001158 }
James Feistc33a90e2019-03-01 10:17:44 -08001159 if (direction)
1160 {
1161 constexpr const std::array<const char*, 2> allowedDirections = {
1162 "Ceiling", "Floor"};
Ed Tanous3544d2a2023-08-06 18:12:20 -07001163 if (std::ranges::find(allowedDirections, *direction) ==
1164 allowedDirections.end())
James Feistc33a90e2019-03-01 10:17:44 -08001165 {
1166 messages::propertyValueTypeError(response->res, "Direction",
1167 *direction);
1168 return CreatePIDRet::fail;
1169 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001170 output.emplace_back("Class", *direction);
James Feistc33a90e2019-03-01 10:17:44 -08001171 }
James Feist83ff9ab2018-08-31 10:18:24 -07001172 }
1173 else
1174 {
Ed Tanous62598e32023-07-17 17:06:25 -07001175 BMCWEB_LOG_ERROR("Illegal Type {}", type);
Jason M. Bills35a62c72018-10-09 12:45:45 -07001176 messages::propertyUnknown(response->res, type);
James Feist83ff9ab2018-08-31 10:18:24 -07001177 return CreatePIDRet::fail;
1178 }
1179 return CreatePIDRet::patch;
1180}
James Feist73df0db2019-03-25 15:29:35 -07001181struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
1182{
Ed Tanous6936afe2022-09-08 15:10:39 -07001183 struct CompletionValues
1184 {
1185 std::vector<std::string> supportedProfiles;
1186 std::string currentProfile;
1187 dbus::utility::MapperGetSubTreeResponse subtree;
1188 };
James Feist73df0db2019-03-25 15:29:35 -07001189
Ed Tanous4e23a442022-06-06 09:57:26 -07001190 explicit GetPIDValues(
1191 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
Ed Tanous23a21a12020-07-25 04:45:05 +00001192 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001193
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001194 {}
James Feist73df0db2019-03-25 15:29:35 -07001195
1196 void run()
1197 {
1198 std::shared_ptr<GetPIDValues> self = shared_from_this();
1199
1200 // get all configurations
George Liue99073f2022-12-09 11:06:16 +08001201 constexpr std::array<std::string_view, 4> interfaces = {
1202 pidConfigurationIface, pidZoneConfigurationIface,
1203 objectManagerIface, stepwiseConfigurationIface};
1204 dbus::utility::getSubTree(
1205 "/", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001206 [self](
George Liue99073f2022-12-09 11:06:16 +08001207 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001208 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001209 if (ec)
1210 {
Ed Tanous62598e32023-07-17 17:06:25 -07001211 BMCWEB_LOG_ERROR("{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001212 messages::internalError(self->asyncResp->res);
1213 return;
1214 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001215 self->complete.subtree = subtreeLocal;
Patrick Williams5a39f772023-10-20 11:20:21 -05001216 });
James Feist73df0db2019-03-25 15:29:35 -07001217
1218 // at the same time get the selected profile
George Liue99073f2022-12-09 11:06:16 +08001219 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1220 thermalModeIface};
1221 dbus::utility::getSubTree(
1222 "/", 0, thermalModeIfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001223 [self](
George Liue99073f2022-12-09 11:06:16 +08001224 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001225 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001226 if (ec || subtreeLocal.empty())
1227 {
1228 return;
1229 }
1230 if (subtreeLocal[0].second.size() != 1)
1231 {
1232 // invalid mapper response, should never happen
Ed Tanous62598e32023-07-17 17:06:25 -07001233 BMCWEB_LOG_ERROR("GetPIDValues: Mapper Error");
Ed Tanous002d39b2022-05-31 08:59:27 -07001234 messages::internalError(self->asyncResp->res);
1235 return;
1236 }
1237
1238 const std::string& path = subtreeLocal[0].first;
1239 const std::string& owner = subtreeLocal[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001240
1241 sdbusplus::asio::getAllProperties(
1242 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous002d39b2022-05-31 08:59:27 -07001243 [path, owner,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001244 self](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001245 const dbus::utility::DBusPropertiesMap& resp) {
1246 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001247 {
Ed Tanous62598e32023-07-17 17:06:25 -07001248 BMCWEB_LOG_ERROR(
1249 "GetPIDValues: Can't get thermalModeIface {}", path);
James Feist73df0db2019-03-25 15:29:35 -07001250 messages::internalError(self->asyncResp->res);
1251 return;
1252 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001253
Ed Tanous002d39b2022-05-31 08:59:27 -07001254 const std::string* current = nullptr;
1255 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001256
1257 const bool success = sdbusplus::unpackPropertiesNoThrow(
1258 dbus_utils::UnpackErrorPrinter(), resp, "Current", current,
1259 "Supported", supported);
1260
1261 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001262 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001263 messages::internalError(self->asyncResp->res);
1264 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001265 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001266
Ed Tanous002d39b2022-05-31 08:59:27 -07001267 if (current == nullptr || supported == nullptr)
1268 {
Ed Tanous62598e32023-07-17 17:06:25 -07001269 BMCWEB_LOG_ERROR(
1270 "GetPIDValues: thermal mode iface invalid {}", path);
Ed Tanous002d39b2022-05-31 08:59:27 -07001271 messages::internalError(self->asyncResp->res);
1272 return;
1273 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001274 self->complete.currentProfile = *current;
1275 self->complete.supportedProfiles = *supported;
George Liue99073f2022-12-09 11:06:16 +08001276 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001277 });
James Feist73df0db2019-03-25 15:29:35 -07001278 }
1279
Ed Tanous6936afe2022-09-08 15:10:39 -07001280 static void
1281 processingComplete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1282 const CompletionValues& completion)
James Feist73df0db2019-03-25 15:29:35 -07001283 {
1284 if (asyncResp->res.result() != boost::beast::http::status::ok)
1285 {
1286 return;
1287 }
1288 // create map of <connection, path to objMgr>>
Ed Tanous6936afe2022-09-08 15:10:39 -07001289 boost::container::flat_map<
1290 std::string, std::string, std::less<>,
1291 std::vector<std::pair<std::string, std::string>>>
1292 objectMgrPaths;
1293 boost::container::flat_set<std::string, std::less<>,
1294 std::vector<std::string>>
1295 calledConnections;
1296 for (const auto& pathGroup : completion.subtree)
James Feist73df0db2019-03-25 15:29:35 -07001297 {
1298 for (const auto& connectionGroup : pathGroup.second)
1299 {
1300 auto findConnection =
1301 calledConnections.find(connectionGroup.first);
1302 if (findConnection != calledConnections.end())
1303 {
1304 break;
1305 }
1306 for (const std::string& interface : connectionGroup.second)
1307 {
1308 if (interface == objectManagerIface)
1309 {
1310 objectMgrPaths[connectionGroup.first] = pathGroup.first;
1311 }
1312 // this list is alphabetical, so we
1313 // should have found the objMgr by now
1314 if (interface == pidConfigurationIface ||
1315 interface == pidZoneConfigurationIface ||
1316 interface == stepwiseConfigurationIface)
1317 {
1318 auto findObjMgr =
1319 objectMgrPaths.find(connectionGroup.first);
1320 if (findObjMgr == objectMgrPaths.end())
1321 {
Ed Tanous62598e32023-07-17 17:06:25 -07001322 BMCWEB_LOG_DEBUG("{}Has no Object Manager",
1323 connectionGroup.first);
James Feist73df0db2019-03-25 15:29:35 -07001324 continue;
1325 }
1326
1327 calledConnections.insert(connectionGroup.first);
1328
1329 asyncPopulatePid(findObjMgr->first, findObjMgr->second,
Ed Tanous6936afe2022-09-08 15:10:39 -07001330 completion.currentProfile,
1331 completion.supportedProfiles,
James Feist73df0db2019-03-25 15:29:35 -07001332 asyncResp);
1333 break;
1334 }
1335 }
1336 }
1337 }
1338 }
1339
Ed Tanous6936afe2022-09-08 15:10:39 -07001340 ~GetPIDValues()
1341 {
1342 boost::asio::post(crow::connections::systemBus->get_io_context(),
1343 std::bind_front(&processingComplete, asyncResp,
1344 std::move(complete)));
1345 }
1346
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001347 GetPIDValues(const GetPIDValues&) = delete;
1348 GetPIDValues(GetPIDValues&&) = delete;
1349 GetPIDValues& operator=(const GetPIDValues&) = delete;
1350 GetPIDValues& operator=(GetPIDValues&&) = delete;
1351
zhanghch058d1b46d2021-04-01 11:18:24 +08001352 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Ed Tanous6936afe2022-09-08 15:10:39 -07001353 CompletionValues complete;
James Feist73df0db2019-03-25 15:29:35 -07001354};
1355
1356struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
1357{
Ed Tanous9e9b6042024-03-06 14:18:28 -08001358 SetPIDValues(
1359 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
1360 std::vector<
1361 std::pair<std::string, std::optional<nlohmann::json::object_t>>>&&
1362 configurationsIn,
1363 std::optional<std::string>& profileIn) :
1364 asyncResp(asyncRespIn),
1365 configuration(std::move(configurationsIn)),
1366 profile(std::move(profileIn))
1367 {}
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001368
1369 SetPIDValues(const SetPIDValues&) = delete;
1370 SetPIDValues(SetPIDValues&&) = delete;
1371 SetPIDValues& operator=(const SetPIDValues&) = delete;
1372 SetPIDValues& operator=(SetPIDValues&&) = delete;
1373
James Feist73df0db2019-03-25 15:29:35 -07001374 void run()
1375 {
1376 if (asyncResp->res.result() != boost::beast::http::status::ok)
1377 {
1378 return;
1379 }
1380
1381 std::shared_ptr<SetPIDValues> self = shared_from_this();
1382
1383 // todo(james): might make sense to do a mapper call here if this
1384 // interface gets more traction
George Liu5eb468d2023-06-20 17:03:24 +08001385 sdbusplus::message::object_path objPath(
1386 "/xyz/openbmc_project/inventory");
1387 dbus::utility::getManagedObjects(
1388 "xyz.openbmc_project.EntityManager", objPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001389 [self](const boost::system::error_code& ec,
Ed Tanous914e2d52022-01-07 11:38:34 -08001390 const dbus::utility::ManagedObjectType& mObj) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001391 if (ec)
1392 {
Ed Tanous62598e32023-07-17 17:06:25 -07001393 BMCWEB_LOG_ERROR("Error communicating to Entity Manager");
Ed Tanous002d39b2022-05-31 08:59:27 -07001394 messages::internalError(self->asyncResp->res);
1395 return;
1396 }
1397 const std::array<const char*, 3> configurations = {
1398 pidConfigurationIface, pidZoneConfigurationIface,
1399 stepwiseConfigurationIface};
James Feiste69d9de2020-02-07 12:23:27 -08001400
Ed Tanous002d39b2022-05-31 08:59:27 -07001401 for (const auto& [path, object] : mObj)
1402 {
1403 for (const auto& [interface, _] : object)
James Feiste69d9de2020-02-07 12:23:27 -08001404 {
Ed Tanous3544d2a2023-08-06 18:12:20 -07001405 if (std::ranges::find(configurations, interface) !=
1406 configurations.end())
James Feiste69d9de2020-02-07 12:23:27 -08001407 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001408 self->objectCount++;
1409 break;
James Feiste69d9de2020-02-07 12:23:27 -08001410 }
James Feiste69d9de2020-02-07 12:23:27 -08001411 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001412 }
1413 self->managedObj = mObj;
Patrick Williams5a39f772023-10-20 11:20:21 -05001414 });
James Feist73df0db2019-03-25 15:29:35 -07001415
1416 // at the same time get the profile information
George Liue99073f2022-12-09 11:06:16 +08001417 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1418 thermalModeIface};
1419 dbus::utility::getSubTree(
1420 "/", 0, thermalModeIfaces,
1421 [self](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001422 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001423 if (ec || subtree.empty())
1424 {
1425 return;
1426 }
1427 if (subtree[0].second.empty())
1428 {
1429 // invalid mapper response, should never happen
Ed Tanous62598e32023-07-17 17:06:25 -07001430 BMCWEB_LOG_ERROR("SetPIDValues: Mapper Error");
Ed Tanous002d39b2022-05-31 08:59:27 -07001431 messages::internalError(self->asyncResp->res);
1432 return;
1433 }
1434
1435 const std::string& path = subtree[0].first;
1436 const std::string& owner = subtree[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001437 sdbusplus::asio::getAllProperties(
1438 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001439 [self, path, owner](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001440 const dbus::utility::DBusPropertiesMap& r) {
1441 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001442 {
Ed Tanous62598e32023-07-17 17:06:25 -07001443 BMCWEB_LOG_ERROR(
1444 "SetPIDValues: Can't get thermalModeIface {}", path);
James Feist73df0db2019-03-25 15:29:35 -07001445 messages::internalError(self->asyncResp->res);
1446 return;
1447 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001448 const std::string* current = nullptr;
1449 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001450
1451 const bool success = sdbusplus::unpackPropertiesNoThrow(
1452 dbus_utils::UnpackErrorPrinter(), r, "Current", current,
1453 "Supported", supported);
1454
1455 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001456 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001457 messages::internalError(self->asyncResp->res);
1458 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001459 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001460
Ed Tanous002d39b2022-05-31 08:59:27 -07001461 if (current == nullptr || supported == nullptr)
1462 {
Ed Tanous62598e32023-07-17 17:06:25 -07001463 BMCWEB_LOG_ERROR(
1464 "SetPIDValues: thermal mode iface invalid {}", path);
Ed Tanous002d39b2022-05-31 08:59:27 -07001465 messages::internalError(self->asyncResp->res);
1466 return;
1467 }
1468 self->currentProfile = *current;
1469 self->supportedProfiles = *supported;
1470 self->profileConnection = owner;
1471 self->profilePath = path;
George Liue99073f2022-12-09 11:06:16 +08001472 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001473 });
James Feist73df0db2019-03-25 15:29:35 -07001474 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001475 void pidSetDone()
James Feist73df0db2019-03-25 15:29:35 -07001476 {
1477 if (asyncResp->res.result() != boost::beast::http::status::ok)
1478 {
1479 return;
1480 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001481 std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001482 if (profile)
1483 {
Ed Tanous3544d2a2023-08-06 18:12:20 -07001484 if (std::ranges::find(supportedProfiles, *profile) ==
1485 supportedProfiles.end())
James Feist73df0db2019-03-25 15:29:35 -07001486 {
1487 messages::actionParameterUnknown(response->res, "Profile",
1488 *profile);
1489 return;
1490 }
1491 currentProfile = *profile;
George Liu9ae226f2023-06-21 17:56:46 +08001492 sdbusplus::asio::setProperty(
1493 *crow::connections::systemBus, profileConnection, profilePath,
1494 thermalModeIface, "Current", *profile,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001495 [response](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001496 if (ec)
1497 {
Ed Tanous62598e32023-07-17 17:06:25 -07001498 BMCWEB_LOG_ERROR("Error patching profile{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001499 messages::internalError(response->res);
1500 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001501 });
James Feist73df0db2019-03-25 15:29:35 -07001502 }
1503
1504 for (auto& containerPair : configuration)
1505 {
1506 auto& container = containerPair.second;
1507 if (!container)
1508 {
1509 continue;
1510 }
James Feist6ee7f772020-02-06 16:25:27 -08001511
Ed Tanous02cad962022-06-30 16:50:15 -07001512 const std::string& type = containerPair.first;
James Feist73df0db2019-03-25 15:29:35 -07001513
Ed Tanous9e9b6042024-03-06 14:18:28 -08001514 for (auto& [name, value] : *container)
James Feist73df0db2019-03-25 15:29:35 -07001515 {
Potin Laicddbf3d2023-02-14 14:28:58 +08001516 std::string dbusObjName = name;
1517 std::replace(dbusObjName.begin(), dbusObjName.end(), ' ', '_');
Ed Tanous62598e32023-07-17 17:06:25 -07001518 BMCWEB_LOG_DEBUG("looking for {}", name);
James Feist6ee7f772020-02-06 16:25:27 -08001519
Ed Tanous3544d2a2023-08-06 18:12:20 -07001520 auto pathItr = std::ranges::find_if(
1521 managedObj, [&dbusObjName](const auto& obj) {
Ed Tanous91f75ca2024-06-10 13:56:43 -07001522 return obj.first.filename() == dbusObjName;
Patrick Williams5a39f772023-10-20 11:20:21 -05001523 });
Ed Tanousb9d36b42022-02-26 21:42:46 -08001524 dbus::utility::DBusPropertiesMap output;
James Feist73df0db2019-03-25 15:29:35 -07001525
1526 output.reserve(16); // The pid interface length
1527
1528 // determines if we're patching entity-manager or
1529 // creating a new object
1530 bool createNewObject = (pathItr == managedObj.end());
Ed Tanous62598e32023-07-17 17:06:25 -07001531 BMCWEB_LOG_DEBUG("Found = {}", !createNewObject);
James Feist6ee7f772020-02-06 16:25:27 -08001532
James Feist73df0db2019-03-25 15:29:35 -07001533 std::string iface;
Ed Tanousea2b6702022-03-07 16:48:38 -08001534 if (!createNewObject)
James Feist73df0db2019-03-25 15:29:35 -07001535 {
Potin Lai8be2b5b2022-11-22 13:27:16 +08001536 bool findInterface = false;
Ed Tanousea2b6702022-03-07 16:48:38 -08001537 for (const auto& interface : pathItr->second)
James Feist73df0db2019-03-25 15:29:35 -07001538 {
Ed Tanousea2b6702022-03-07 16:48:38 -08001539 if (interface.first == pidConfigurationIface)
1540 {
1541 if (type == "PidControllers" ||
1542 type == "FanControllers")
1543 {
1544 iface = pidConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001545 findInterface = true;
1546 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001547 }
1548 }
1549 else if (interface.first == pidZoneConfigurationIface)
1550 {
1551 if (type == "FanZones")
1552 {
PavanKumarIntelda393502024-03-15 05:47:02 +00001553 iface = pidZoneConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001554 findInterface = true;
1555 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001556 }
1557 }
1558 else if (interface.first == stepwiseConfigurationIface)
1559 {
1560 if (type == "StepwiseControllers")
1561 {
1562 iface = stepwiseConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001563 findInterface = true;
1564 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001565 }
1566 }
James Feist73df0db2019-03-25 15:29:35 -07001567 }
Potin Lai8be2b5b2022-11-22 13:27:16 +08001568
1569 // create new object if interface not found
1570 if (!findInterface)
1571 {
1572 createNewObject = true;
1573 }
James Feist73df0db2019-03-25 15:29:35 -07001574 }
James Feist6ee7f772020-02-06 16:25:27 -08001575
Ed Tanous9e9b6042024-03-06 14:18:28 -08001576 if (createNewObject && value == nullptr)
James Feist6ee7f772020-02-06 16:25:27 -08001577 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -05001578 // can't delete a non-existent object
Ed Tanous9e9b6042024-03-06 14:18:28 -08001579 messages::propertyValueNotInList(response->res, value,
Ed Tanouse2616cc2022-06-27 12:45:55 -07001580 name);
James Feist6ee7f772020-02-06 16:25:27 -08001581 continue;
1582 }
1583
1584 std::string path;
1585 if (pathItr != managedObj.end())
1586 {
1587 path = pathItr->first.str;
1588 }
1589
Ed Tanous62598e32023-07-17 17:06:25 -07001590 BMCWEB_LOG_DEBUG("Create new = {}", createNewObject);
James Feiste69d9de2020-02-07 12:23:27 -08001591
1592 // arbitrary limit to avoid attacks
1593 constexpr const size_t controllerLimit = 500;
James Feist14b0b8d2020-02-12 11:52:07 -08001594 if (createNewObject && objectCount >= controllerLimit)
James Feiste69d9de2020-02-07 12:23:27 -08001595 {
1596 messages::resourceExhaustion(response->res, type);
1597 continue;
1598 }
Ed Tanousa170f272022-06-30 21:53:27 -07001599 std::string escaped = name;
1600 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1601 output.emplace_back("Name", escaped);
James Feist73df0db2019-03-25 15:29:35 -07001602
1603 std::string chassis;
1604 CreatePIDRet ret = createPidInterface(
Ed Tanous9e9b6042024-03-06 14:18:28 -08001605 response, type, name, value, path, managedObj,
1606 createNewObject, output, chassis, currentProfile);
James Feist73df0db2019-03-25 15:29:35 -07001607 if (ret == CreatePIDRet::fail)
1608 {
1609 return;
1610 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001611 if (ret == CreatePIDRet::del)
James Feist73df0db2019-03-25 15:29:35 -07001612 {
1613 continue;
1614 }
1615
1616 if (!createNewObject)
1617 {
1618 for (const auto& property : output)
1619 {
Potin Lai7a696972023-11-09 12:18:20 +08001620 crow::connections::systemBus->async_method_call(
James Feist73df0db2019-03-25 15:29:35 -07001621 [response,
1622 propertyName{std::string(property.first)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001623 const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001624 if (ec)
1625 {
Ed Tanous62598e32023-07-17 17:06:25 -07001626 BMCWEB_LOG_ERROR("Error patching {}: {}",
1627 propertyName, ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001628 messages::internalError(response->res);
1629 return;
1630 }
1631 messages::success(response->res);
Potin Lai7a696972023-11-09 12:18:20 +08001632 },
1633 "xyz.openbmc_project.EntityManager", path,
1634 "org.freedesktop.DBus.Properties", "Set", iface,
1635 property.first, property.second);
James Feist73df0db2019-03-25 15:29:35 -07001636 }
1637 }
1638 else
1639 {
1640 if (chassis.empty())
1641 {
Ed Tanous62598e32023-07-17 17:06:25 -07001642 BMCWEB_LOG_ERROR("Failed to get chassis from config");
Ed Tanousace85d62021-10-26 12:45:59 -07001643 messages::internalError(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001644 return;
1645 }
1646
1647 bool foundChassis = false;
1648 for (const auto& obj : managedObj)
1649 {
Ed Tanous91f75ca2024-06-10 13:56:43 -07001650 if (obj.first.filename() == chassis)
James Feist73df0db2019-03-25 15:29:35 -07001651 {
1652 chassis = obj.first.str;
1653 foundChassis = true;
1654 break;
1655 }
1656 }
1657 if (!foundChassis)
1658 {
Ed Tanous62598e32023-07-17 17:06:25 -07001659 BMCWEB_LOG_ERROR("Failed to find chassis on dbus");
James Feist73df0db2019-03-25 15:29:35 -07001660 messages::resourceMissingAtURI(
Ed Tanousace85d62021-10-26 12:45:59 -07001661 response->res,
Ed Tanousef4c65b2023-04-24 15:28:50 -07001662 boost::urls::format("/redfish/v1/Chassis/{}",
1663 chassis));
James Feist73df0db2019-03-25 15:29:35 -07001664 return;
1665 }
1666
1667 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001668 [response](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001669 if (ec)
1670 {
Ed Tanous62598e32023-07-17 17:06:25 -07001671 BMCWEB_LOG_ERROR("Error Adding Pid Object {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001672 messages::internalError(response->res);
1673 return;
1674 }
1675 messages::success(response->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05001676 },
James Feist73df0db2019-03-25 15:29:35 -07001677 "xyz.openbmc_project.EntityManager", chassis,
1678 "xyz.openbmc_project.AddObject", "AddObject", output);
1679 }
1680 }
1681 }
1682 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001683
1684 ~SetPIDValues()
1685 {
1686 try
1687 {
1688 pidSetDone();
1689 }
1690 catch (...)
1691 {
Ed Tanous62598e32023-07-17 17:06:25 -07001692 BMCWEB_LOG_CRITICAL("pidSetDone threw exception");
Ed Tanous24b2fe82022-01-06 12:45:54 -08001693 }
1694 }
1695
zhanghch058d1b46d2021-04-01 11:18:24 +08001696 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Ed Tanous9e9b6042024-03-06 14:18:28 -08001697 std::vector<std::pair<std::string, std::optional<nlohmann::json::object_t>>>
James Feist73df0db2019-03-25 15:29:35 -07001698 configuration;
1699 std::optional<std::string> profile;
1700 dbus::utility::ManagedObjectType managedObj;
1701 std::vector<std::string> supportedProfiles;
1702 std::string currentProfile;
1703 std::string profileConnection;
1704 std::string profilePath;
James Feist14b0b8d2020-02-12 11:52:07 -08001705 size_t objectCount = 0;
James Feist73df0db2019-03-25 15:29:35 -07001706};
James Feist83ff9ab2018-08-31 10:18:24 -07001707
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001708/**
1709 * @brief Retrieves BMC manager location data over DBus
1710 *
Ed Tanousac106bf2023-06-07 09:24:59 -07001711 * @param[in] asyncResp Shared pointer for completing asynchronous calls
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001712 * @param[in] connectionName - service name
1713 * @param[in] path - object path
1714 * @return none
1715 */
Ed Tanousac106bf2023-06-07 09:24:59 -07001716inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001717 const std::string& connectionName,
1718 const std::string& path)
1719{
Ed Tanous62598e32023-07-17 17:06:25 -07001720 BMCWEB_LOG_DEBUG("Get BMC manager Location data.");
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001721
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001722 sdbusplus::asio::getProperty<std::string>(
1723 *crow::connections::systemBus, connectionName, path,
1724 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Ed Tanousac106bf2023-06-07 09:24:59 -07001725 [asyncResp](const boost::system::error_code& ec,
1726 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001727 if (ec)
1728 {
Ed Tanous62598e32023-07-17 17:06:25 -07001729 BMCWEB_LOG_DEBUG("DBUS response error for "
1730 "Location");
Ed Tanousac106bf2023-06-07 09:24:59 -07001731 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001732 return;
1733 }
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001734
Ed Tanousac106bf2023-06-07 09:24:59 -07001735 asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001736 property;
Patrick Williams5a39f772023-10-20 11:20:21 -05001737 });
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001738}
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001739// avoid name collision systems.hpp
1740inline void
Ed Tanousac106bf2023-06-07 09:24:59 -07001741 managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001742{
Ed Tanous62598e32023-07-17 17:06:25 -07001743 BMCWEB_LOG_DEBUG("Getting Manager Last Reset Time");
Ed Tanous52cc1122020-07-18 13:51:21 -07001744
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001745 sdbusplus::asio::getProperty<uint64_t>(
1746 *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
1747 "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
1748 "LastRebootTime",
Ed Tanousac106bf2023-06-07 09:24:59 -07001749 [asyncResp](const boost::system::error_code& ec,
1750 const uint64_t lastResetTime) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001751 if (ec)
1752 {
Ed Tanous62598e32023-07-17 17:06:25 -07001753 BMCWEB_LOG_DEBUG("D-BUS response error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001754 return;
1755 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001756
Ed Tanous002d39b2022-05-31 08:59:27 -07001757 // LastRebootTime is epoch time, in milliseconds
1758 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
1759 uint64_t lastResetTimeStamp = lastResetTime / 1000;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001760
Ed Tanous002d39b2022-05-31 08:59:27 -07001761 // Convert to ISO 8601 standard
Ed Tanousac106bf2023-06-07 09:24:59 -07001762 asyncResp->res.jsonValue["LastResetTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001763 redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
Patrick Williams5a39f772023-10-20 11:20:21 -05001764 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001765}
1766
1767/**
1768 * @brief Set the running firmware image
1769 *
Ed Tanousac106bf2023-06-07 09:24:59 -07001770 * @param[i,o] asyncResp - Async response object
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001771 * @param[i] runningFirmwareTarget - Image to make the running image
1772 *
1773 * @return void
1774 */
1775inline void
Ed Tanousac106bf2023-06-07 09:24:59 -07001776 setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001777 const std::string& runningFirmwareTarget)
1778{
1779 // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1780 std::string::size_type idPos = runningFirmwareTarget.rfind('/');
1781 if (idPos == std::string::npos)
1782 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001783 messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001784 "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001785 BMCWEB_LOG_DEBUG("Can't parse firmware ID!");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001786 return;
1787 }
1788 idPos++;
1789 if (idPos >= runningFirmwareTarget.size())
1790 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001791 messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001792 "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001793 BMCWEB_LOG_DEBUG("Invalid firmware ID.");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001794 return;
1795 }
1796 std::string firmwareId = runningFirmwareTarget.substr(idPos);
1797
1798 // Make sure the image is valid before setting priority
George Liu5eb468d2023-06-20 17:03:24 +08001799 sdbusplus::message::object_path objPath("/xyz/openbmc_project/software");
1800 dbus::utility::getManagedObjects(
1801 "xyz.openbmc_project.Software.BMC.Updater", objPath,
1802 [asyncResp, firmwareId, runningFirmwareTarget](
1803 const boost::system::error_code& ec,
1804 const dbus::utility::ManagedObjectType& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001805 if (ec)
1806 {
Ed Tanous62598e32023-07-17 17:06:25 -07001807 BMCWEB_LOG_DEBUG("D-Bus response error getting objects.");
Ed Tanousac106bf2023-06-07 09:24:59 -07001808 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001809 return;
1810 }
1811
1812 if (subtree.empty())
1813 {
Ed Tanous62598e32023-07-17 17:06:25 -07001814 BMCWEB_LOG_DEBUG("Can't find image!");
Ed Tanousac106bf2023-06-07 09:24:59 -07001815 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001816 return;
1817 }
1818
1819 bool foundImage = false;
Ed Tanous02cad962022-06-30 16:50:15 -07001820 for (const auto& object : subtree)
Ed Tanous002d39b2022-05-31 08:59:27 -07001821 {
1822 const std::string& path =
1823 static_cast<const std::string&>(object.first);
1824 std::size_t idPos2 = path.rfind('/');
1825
1826 if (idPos2 == std::string::npos)
1827 {
1828 continue;
1829 }
1830
1831 idPos2++;
1832 if (idPos2 >= path.size())
1833 {
1834 continue;
1835 }
1836
1837 if (path.substr(idPos2) == firmwareId)
1838 {
1839 foundImage = true;
1840 break;
1841 }
1842 }
1843
1844 if (!foundImage)
1845 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001846 messages::propertyValueNotInList(
1847 asyncResp->res, runningFirmwareTarget, "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001848 BMCWEB_LOG_DEBUG("Invalid firmware ID.");
Ed Tanous002d39b2022-05-31 08:59:27 -07001849 return;
1850 }
1851
Ed Tanous62598e32023-07-17 17:06:25 -07001852 BMCWEB_LOG_DEBUG("Setting firmware version {} to priority 0.",
1853 firmwareId);
Ed Tanous002d39b2022-05-31 08:59:27 -07001854
1855 // Only support Immediate
1856 // An addition could be a Redfish Setting like
1857 // ActiveSoftwareImageApplyTime and support OnReset
George Liu9ae226f2023-06-21 17:56:46 +08001858 sdbusplus::asio::setProperty(
1859 *crow::connections::systemBus,
1860 "xyz.openbmc_project.Software.BMC.Updater",
1861 "/xyz/openbmc_project/software/" + firmwareId,
1862 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1863 static_cast<uint8_t>(0),
Ed Tanousac106bf2023-06-07 09:24:59 -07001864 [asyncResp](const boost::system::error_code& ec2) {
Ed Tanous8a592812022-06-04 09:06:59 -07001865 if (ec2)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001866 {
Ed Tanous62598e32023-07-17 17:06:25 -07001867 BMCWEB_LOG_DEBUG("D-Bus response error setting.");
Ed Tanousac106bf2023-06-07 09:24:59 -07001868 messages::internalError(asyncResp->res);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001869 return;
1870 }
Ed Tanousac106bf2023-06-07 09:24:59 -07001871 doBMCGracefulRestart(asyncResp);
George Liu5eb468d2023-06-20 17:03:24 +08001872 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001873 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001874}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001875
Ed Tanousc51afd52024-03-07 10:13:14 -08001876inline void
1877 afterSetDateTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1878 const boost::system::error_code& ec,
1879 const sdbusplus::message_t& msg)
1880{
1881 if (ec)
1882 {
1883 BMCWEB_LOG_DEBUG("Failed to set elapsed time. DBUS response error {}",
1884 ec);
1885 const sd_bus_error* dbusError = msg.get_error();
1886 if (dbusError != nullptr)
1887 {
1888 std::string_view errorName(dbusError->name);
1889 if (errorName ==
1890 "org.freedesktop.timedate1.AutomaticTimeSyncEnabled")
1891 {
1892 BMCWEB_LOG_DEBUG("Setting conflict");
1893 messages::propertyValueConflict(
1894 asyncResp->res, "DateTime",
1895 "Managers/NetworkProtocol/NTPProcotolEnabled");
1896 return;
1897 }
1898 }
1899 messages::internalError(asyncResp->res);
1900 return;
1901 }
1902 asyncResp->res.result(boost::beast::http::status::no_content);
1903}
1904
1905inline void setDateTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1906 const std::string& datetime)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001907{
Ed Tanous62598e32023-07-17 17:06:25 -07001908 BMCWEB_LOG_DEBUG("Set date time: {}", datetime);
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001909
Ed Tanousc2e32002023-01-07 22:05:08 -08001910 std::optional<redfish::time_utils::usSinceEpoch> us =
1911 redfish::time_utils::dateStringToEpoch(datetime);
1912 if (!us)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001913 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001914 messages::propertyValueFormatError(asyncResp->res, datetime,
1915 "DateTime");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001916 return;
1917 }
Ed Tanousc51afd52024-03-07 10:13:14 -08001918 // Set the absolute datetime
1919 bool relative = false;
1920 bool interactive = false;
1921 crow::connections::systemBus->async_method_call(
1922 [asyncResp](const boost::system::error_code& ec,
1923 const sdbusplus::message_t& msg) {
1924 afterSetDateTime(asyncResp, ec, msg);
1925 },
1926 "org.freedesktop.timedate1", "/org/freedesktop/timedate1",
1927 "org.freedesktop.timedate1", "SetTime", us->count(), relative,
1928 interactive);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001929}
1930
Ed Tanous75815e52022-10-05 17:21:13 -07001931inline void
1932 checkForQuiesced(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1933{
1934 sdbusplus::asio::getProperty<std::string>(
1935 *crow::connections::systemBus, "org.freedesktop.systemd1",
1936 "/org/freedesktop/systemd1/unit/obmc-bmc-service-quiesce@0.target",
1937 "org.freedesktop.systemd1.Unit", "ActiveState",
1938 [asyncResp](const boost::system::error_code& ec,
1939 const std::string& val) {
1940 if (!ec)
1941 {
1942 if (val == "active")
1943 {
Ed Tanous539d8c62024-06-19 14:38:27 -07001944 asyncResp->res.jsonValue["Status"]["Health"] =
1945 resource::Health::Critical;
1946 asyncResp->res.jsonValue["Status"]["State"] =
1947 resource::State::Quiesced;
Ed Tanous75815e52022-10-05 17:21:13 -07001948 return;
1949 }
1950 }
Ed Tanous539d8c62024-06-19 14:38:27 -07001951 asyncResp->res.jsonValue["Status"]["Health"] = resource::Health::OK;
1952 asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
Patrick Williams5a39f772023-10-20 11:20:21 -05001953 });
Ed Tanous75815e52022-10-05 17:21:13 -07001954}
1955
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001956inline void requestRoutesManager(App& app)
1957{
1958 std::string uuid = persistent_data::getConfig().systemUuid;
1959
Ed Tanous253f11b2024-05-16 09:38:31 -07001960 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001961 .privileges(redfish::privileges::getManager)
Ed Tanous002d39b2022-05-31 08:59:27 -07001962 .methods(boost::beast::http::verb::get)(
1963 [&app, uuid](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -07001964 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1965 const std::string& managerId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001966 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001967 {
1968 return;
1969 }
Ed Tanous253f11b2024-05-16 09:38:31 -07001970
1971 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
1972 {
1973 messages::resourceNotFound(asyncResp->res, "Manager", managerId);
1974 return;
1975 }
1976
1977 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1978 "/redfish/v1/Managers/{}", BMCWEB_REDFISH_MANAGER_URI_NAME);
Sui Chena51fc2d2022-07-14 17:21:53 -07001979 asyncResp->res.jsonValue["@odata.type"] = "#Manager.v1_14_0.Manager";
Ed Tanous253f11b2024-05-16 09:38:31 -07001980 asyncResp->res.jsonValue["Id"] = BMCWEB_REDFISH_MANAGER_URI_NAME;
Ed Tanous002d39b2022-05-31 08:59:27 -07001981 asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
1982 asyncResp->res.jsonValue["Description"] =
1983 "Baseboard Management Controller";
Ed Tanous539d8c62024-06-19 14:38:27 -07001984 asyncResp->res.jsonValue["PowerState"] = resource::PowerState::On;
Ed Tanous14766872022-03-15 10:44:42 -07001985
Ed Tanous539d8c62024-06-19 14:38:27 -07001986 asyncResp->res.jsonValue["ManagerType"] = manager::ManagerType::BMC;
Ed Tanous002d39b2022-05-31 08:59:27 -07001987 asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
1988 asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1989 asyncResp->res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001990
Ed Tanous002d39b2022-05-31 08:59:27 -07001991 asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07001992 boost::urls::format("/redfish/v1/Managers/{}/LogServices",
1993 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -07001994 asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07001995 boost::urls::format("/redfish/v1/Managers/{}/NetworkProtocol",
1996 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -07001997 asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07001998 boost::urls::format("/redfish/v1/Managers/{}/EthernetInterfaces",
1999 BMCWEB_REDFISH_MANAGER_URI_NAME);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002000
Ed Tanous25b54db2024-04-17 15:40:31 -07002001 if constexpr (BMCWEB_VM_NBDPROXY)
Ed Tanous36c0f2a2024-02-09 13:50:26 -08002002 {
2003 asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07002004 boost::urls::format("/redfish/v1/Managers/{}/VirtualMedia",
2005 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous36c0f2a2024-02-09 13:50:26 -08002006 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002007
Ed Tanous002d39b2022-05-31 08:59:27 -07002008 // default oem data
2009 nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
2010 nlohmann::json& oemOpenbmc = oem["OpenBmc"];
Ed Tanous253f11b2024-05-16 09:38:31 -07002011 oem["@odata.id"] = boost::urls::format("/redfish/v1/Managers/{}#/Oem",
2012 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanousfc1cdd12024-07-11 11:58:16 -07002013 oemOpenbmc["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager";
Ed Tanous253f11b2024-05-16 09:38:31 -07002014 oemOpenbmc["@odata.id"] =
2015 boost::urls::format("/redfish/v1/Managers/{}#/Oem/OpenBmc",
2016 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous14766872022-03-15 10:44:42 -07002017
Ed Tanous002d39b2022-05-31 08:59:27 -07002018 nlohmann::json::object_t certificates;
Ed Tanous253f11b2024-05-16 09:38:31 -07002019 certificates["@odata.id"] = boost::urls::format(
2020 "/redfish/v1/Managers/{}/Truststore/Certificates",
2021 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -07002022 oemOpenbmc["Certificates"] = std::move(certificates);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002023
Ed Tanous002d39b2022-05-31 08:59:27 -07002024 // Manager.Reset (an action) can be many values, OpenBMC only
2025 // supports BMC reboot.
2026 nlohmann::json& managerReset =
2027 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
2028 managerReset["target"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07002029 boost::urls::format("/redfish/v1/Managers/{}/Actions/Manager.Reset",
2030 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -07002031 managerReset["@Redfish.ActionInfo"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07002032 boost::urls::format("/redfish/v1/Managers/{}/ResetActionInfo",
2033 BMCWEB_REDFISH_MANAGER_URI_NAME);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002034
Ed Tanous002d39b2022-05-31 08:59:27 -07002035 // ResetToDefaults (Factory Reset) has values like
2036 // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
2037 // on OpenBMC
2038 nlohmann::json& resetToDefaults =
2039 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
Ed Tanous253f11b2024-05-16 09:38:31 -07002040 resetToDefaults["target"] = boost::urls::format(
2041 "/redfish/v1/Managers/{}/Actions/Manager.ResetToDefaults",
2042 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous613dabe2022-07-09 11:17:36 -07002043 resetToDefaults["ResetType@Redfish.AllowableValues"] =
2044 nlohmann::json::array_t({"ResetAll"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002045
Ed Tanous002d39b2022-05-31 08:59:27 -07002046 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002047 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05302048
Ed Tanous002d39b2022-05-31 08:59:27 -07002049 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2050 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2051 redfishDateTimeOffset.second;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002052
Ed Tanous002d39b2022-05-31 08:59:27 -07002053 // TODO (Gunnar): Remove these one day since moved to ComputerSystem
2054 // Still used by OCP profiles
2055 // https://github.com/opencomputeproject/OCP-Profiles/issues/23
2056 // Fill in SerialConsole info
2057 asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
2058 asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
Ed Tanous613dabe2022-07-09 11:17:36 -07002059 asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
2060 nlohmann::json::array_t({"IPMI", "SSH"});
Ed Tanous25b54db2024-04-17 15:40:31 -07002061 if constexpr (BMCWEB_KVM)
2062 {
2063 // Fill in GraphicalConsole info
2064 asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] =
2065 true;
2066 asyncResp->res
2067 .jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4;
2068 asyncResp->res
2069 .jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
2070 nlohmann::json::array_t({"KVMIP"});
2071 }
2072 if constexpr (!BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002073 {
2074 asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] =
2075 1;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002076
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002077 nlohmann::json::array_t managerForServers;
2078 nlohmann::json::object_t manager;
Ed Tanous253f11b2024-05-16 09:38:31 -07002079 manager["@odata.id"] = std::format("/redfish/v1/Systems/{}",
2080 BMCWEB_REDFISH_SYSTEM_URI_NAME);
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002081 managerForServers.emplace_back(std::move(manager));
Ed Tanous14766872022-03-15 10:44:42 -07002082
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002083 asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
2084 std::move(managerForServers);
2085 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002086
Willy Tueee00132022-06-14 14:53:17 -07002087 sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
Ed Tanous002d39b2022-05-31 08:59:27 -07002088 "FirmwareVersion", true);
2089
2090 managerGetLastResetTime(asyncResp);
2091
Sui Chena51fc2d2022-07-14 17:21:53 -07002092 // ManagerDiagnosticData is added for all BMCs.
2093 nlohmann::json& managerDiagnosticData =
2094 asyncResp->res.jsonValue["ManagerDiagnosticData"];
2095 managerDiagnosticData["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07002096 boost::urls::format("/redfish/v1/Managers/{}/ManagerDiagnosticData",
2097 BMCWEB_REDFISH_MANAGER_URI_NAME);
Sui Chena51fc2d2022-07-14 17:21:53 -07002098
Ed Tanous25b54db2024-04-17 15:40:31 -07002099 if constexpr (BMCWEB_REDFISH_OEM_MANAGER_FAN_DATA)
2100 {
2101 auto pids = std::make_shared<GetPIDValues>(asyncResp);
2102 pids->run();
2103 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002104
2105 getMainChassisId(asyncResp,
2106 [](const std::string& chassisId,
2107 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2108 aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
2109 nlohmann::json::array_t managerForChassis;
Ed Tanous8a592812022-06-04 09:06:59 -07002110 nlohmann::json::object_t managerObj;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002111 boost::urls::url chassiUrl =
2112 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
Willy Tueddfc432022-09-26 16:46:38 +00002113 managerObj["@odata.id"] = chassiUrl;
Patrick Williamsad539542023-05-12 10:10:08 -05002114 managerForChassis.emplace_back(std::move(managerObj));
Ed Tanous002d39b2022-05-31 08:59:27 -07002115 aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
2116 std::move(managerForChassis);
2117 aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00002118 chassiUrl;
Ed Tanous002d39b2022-05-31 08:59:27 -07002119 });
Ed Tanous14766872022-03-15 10:44:42 -07002120
Ed Tanous75815e52022-10-05 17:21:13 -07002121 sdbusplus::asio::getProperty<double>(
2122 *crow::connections::systemBus, "org.freedesktop.systemd1",
2123 "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
2124 "Progress",
2125 [asyncResp](const boost::system::error_code& ec, double val) {
2126 if (ec)
2127 {
Ed Tanous62598e32023-07-17 17:06:25 -07002128 BMCWEB_LOG_ERROR("Error while getting progress");
Ed Tanous75815e52022-10-05 17:21:13 -07002129 messages::internalError(asyncResp->res);
2130 return;
2131 }
2132 if (val < 1.0)
2133 {
Ed Tanous539d8c62024-06-19 14:38:27 -07002134 asyncResp->res.jsonValue["Status"]["Health"] =
2135 resource::Health::OK;
2136 asyncResp->res.jsonValue["Status"]["State"] =
2137 resource::State::Starting;
Ed Tanous75815e52022-10-05 17:21:13 -07002138 return;
2139 }
2140 checkForQuiesced(asyncResp);
Patrick Williams5a39f772023-10-20 11:20:21 -05002141 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002142
George Liue99073f2022-12-09 11:06:16 +08002143 constexpr std::array<std::string_view, 1> interfaces = {
2144 "xyz.openbmc_project.Inventory.Item.Bmc"};
2145 dbus::utility::getSubTree(
2146 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07002147 [asyncResp](
George Liue99073f2022-12-09 11:06:16 +08002148 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07002149 const dbus::utility::MapperGetSubTreeResponse& subtree) {
2150 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002151 {
Ed Tanous62598e32023-07-17 17:06:25 -07002152 BMCWEB_LOG_DEBUG("D-Bus response error on GetSubTree {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002153 return;
2154 }
2155 if (subtree.empty())
2156 {
Ed Tanous62598e32023-07-17 17:06:25 -07002157 BMCWEB_LOG_DEBUG("Can't find bmc D-Bus object!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002158 return;
2159 }
2160 // Assume only 1 bmc D-Bus object
2161 // Throw an error if there is more than 1
2162 if (subtree.size() > 1)
2163 {
Ed Tanous62598e32023-07-17 17:06:25 -07002164 BMCWEB_LOG_DEBUG("Found more than 1 bmc D-Bus object!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002165 messages::internalError(asyncResp->res);
2166 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002167 }
2168
Ed Tanous002d39b2022-05-31 08:59:27 -07002169 if (subtree[0].first.empty() || subtree[0].second.size() != 1)
2170 {
Ed Tanous62598e32023-07-17 17:06:25 -07002171 BMCWEB_LOG_DEBUG("Error getting bmc D-Bus object!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002172 messages::internalError(asyncResp->res);
2173 return;
2174 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002175
Ed Tanous002d39b2022-05-31 08:59:27 -07002176 const std::string& path = subtree[0].first;
2177 const std::string& connectionName = subtree[0].second[0].first;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002178
Ed Tanous002d39b2022-05-31 08:59:27 -07002179 for (const auto& interfaceName : subtree[0].second[0].second)
2180 {
2181 if (interfaceName ==
2182 "xyz.openbmc_project.Inventory.Decorator.Asset")
2183 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002184 sdbusplus::asio::getAllProperties(
2185 *crow::connections::systemBus, connectionName, path,
2186 "xyz.openbmc_project.Inventory.Decorator.Asset",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002187 [asyncResp](const boost::system::error_code& ec2,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002188 const dbus::utility::DBusPropertiesMap&
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002189 propertiesList) {
Ed Tanous8a592812022-06-04 09:06:59 -07002190 if (ec2)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002191 {
Ed Tanous62598e32023-07-17 17:06:25 -07002192 BMCWEB_LOG_DEBUG("Can't get bmc asset!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002193 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002194 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002195
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002196 const std::string* partNumber = nullptr;
2197 const std::string* serialNumber = nullptr;
2198 const std::string* manufacturer = nullptr;
2199 const std::string* model = nullptr;
2200 const std::string* sparePartNumber = nullptr;
2201
2202 const bool success = sdbusplus::unpackPropertiesNoThrow(
2203 dbus_utils::UnpackErrorPrinter(), propertiesList,
2204 "PartNumber", partNumber, "SerialNumber",
2205 serialNumber, "Manufacturer", manufacturer, "Model",
2206 model, "SparePartNumber", sparePartNumber);
2207
2208 if (!success)
2209 {
2210 messages::internalError(asyncResp->res);
2211 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07002212 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002213
2214 if (partNumber != nullptr)
2215 {
2216 asyncResp->res.jsonValue["PartNumber"] =
2217 *partNumber;
2218 }
2219
2220 if (serialNumber != nullptr)
2221 {
2222 asyncResp->res.jsonValue["SerialNumber"] =
2223 *serialNumber;
2224 }
2225
2226 if (manufacturer != nullptr)
2227 {
2228 asyncResp->res.jsonValue["Manufacturer"] =
2229 *manufacturer;
2230 }
2231
2232 if (model != nullptr)
2233 {
2234 asyncResp->res.jsonValue["Model"] = *model;
2235 }
2236
2237 if (sparePartNumber != nullptr)
2238 {
2239 asyncResp->res.jsonValue["SparePartNumber"] =
2240 *sparePartNumber;
2241 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002242 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002243 }
2244 else if (interfaceName ==
2245 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
2246 {
2247 getLocation(asyncResp, connectionName, path);
2248 }
2249 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002250 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002251 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002252
Ed Tanous253f11b2024-05-16 09:38:31 -07002253 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002254 .privileges(redfish::privileges::patchManager)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002255 .methods(boost::beast::http::verb::patch)(
2256 [&app](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -07002257 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2258 const std::string& managerId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002259 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002260 {
2261 return;
2262 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002263
2264 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2265 {
2266 messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2267 return;
2268 }
2269
Ed Tanous9e9b6042024-03-06 14:18:28 -08002270 std::optional<std::string> activeSoftwareImageOdataId;
Ed Tanous002d39b2022-05-31 08:59:27 -07002271 std::optional<std::string> datetime;
Ed Tanous9e9b6042024-03-06 14:18:28 -08002272 std::optional<nlohmann::json::object_t> pidControllers;
2273 std::optional<nlohmann::json::object_t> fanControllers;
2274 std::optional<nlohmann::json::object_t> fanZones;
2275 std::optional<nlohmann::json::object_t> stepwiseControllers;
2276 std::optional<std::string> profile;
Ed Tanous002d39b2022-05-31 08:59:27 -07002277
Ed Tanous9e9b6042024-03-06 14:18:28 -08002278 // clang-format off
2279 if (!json_util::readJsonPatch(req, asyncResp->res,
2280 "DateTime", datetime,
2281 "Links/ActiveSoftwareImage/@odata.id", activeSoftwareImageOdataId,
2282 "Oem/OpenBmc/Fan/FanControllers", fanControllers,
2283 "Oem/OpenBmc/Fan/FanZones", fanZones,
2284 "Oem/OpenBmc/Fan/PidControllers", pidControllers,
2285 "Oem/OpenBmc/Fan/Profile", profile,
2286 "Oem/OpenBmc/Fan/StepwiseControllers", stepwiseControllers
2287 ))
Ed Tanous002d39b2022-05-31 08:59:27 -07002288 {
2289 return;
2290 }
Ed Tanous9e9b6042024-03-06 14:18:28 -08002291 // clang-format on
Ed Tanous002d39b2022-05-31 08:59:27 -07002292
Ed Tanous9e9b6042024-03-06 14:18:28 -08002293 if (pidControllers || fanControllers || fanZones ||
2294 stepwiseControllers || profile)
Ed Tanous002d39b2022-05-31 08:59:27 -07002295 {
Ed Tanous25b54db2024-04-17 15:40:31 -07002296 if constexpr (BMCWEB_REDFISH_OEM_MANAGER_FAN_DATA)
Ed Tanous002d39b2022-05-31 08:59:27 -07002297 {
Ed Tanous25b54db2024-04-17 15:40:31 -07002298 std::vector<std::pair<std::string,
2299 std::optional<nlohmann::json::object_t>>>
2300 configuration;
2301 if (pidControllers)
2302 {
2303 configuration.emplace_back("PidControllers",
2304 std::move(pidControllers));
2305 }
2306 if (fanControllers)
2307 {
2308 configuration.emplace_back("FanControllers",
2309 std::move(fanControllers));
2310 }
2311 if (fanZones)
2312 {
2313 configuration.emplace_back("FanZones", std::move(fanZones));
2314 }
2315 if (stepwiseControllers)
2316 {
2317 configuration.emplace_back("StepwiseControllers",
2318 std::move(stepwiseControllers));
2319 }
2320 auto pid = std::make_shared<SetPIDValues>(
2321 asyncResp, std::move(configuration), profile);
2322 pid->run();
Ed Tanous002d39b2022-05-31 08:59:27 -07002323 }
Ed Tanous25b54db2024-04-17 15:40:31 -07002324 else
Ed Tanous002d39b2022-05-31 08:59:27 -07002325 {
Ed Tanous25b54db2024-04-17 15:40:31 -07002326 messages::propertyUnknown(asyncResp->res, "Oem");
2327 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07002328 }
2329 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07002330
Ed Tanous9e9b6042024-03-06 14:18:28 -08002331 if (activeSoftwareImageOdataId)
2332 {
2333 setActiveFirmwareImage(asyncResp, *activeSoftwareImageOdataId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002334 }
Ed Tanous9e9b6042024-03-06 14:18:28 -08002335
Ed Tanous002d39b2022-05-31 08:59:27 -07002336 if (datetime)
2337 {
Ed Tanousc51afd52024-03-07 10:13:14 -08002338 setDateTime(asyncResp, *datetime);
Ed Tanous002d39b2022-05-31 08:59:27 -07002339 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002340 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002341}
2342
2343inline void requestRoutesManagerCollection(App& app)
2344{
2345 BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
Ed Tanoused398212021-06-09 17:05:54 -07002346 .privileges(redfish::privileges::getManagerCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002347 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002348 [&app](const crow::Request& req,
2349 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002350 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002351 {
2352 return;
2353 }
2354 // Collections don't include the static data added by SubRoute
2355 // because it has a duplicate entry for members
2356 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
2357 asyncResp->res.jsonValue["@odata.type"] =
2358 "#ManagerCollection.ManagerCollection";
2359 asyncResp->res.jsonValue["Name"] = "Manager Collection";
2360 asyncResp->res.jsonValue["Members@odata.count"] = 1;
2361 nlohmann::json::array_t members;
2362 nlohmann::json& bmc = members.emplace_back();
Ed Tanous253f11b2024-05-16 09:38:31 -07002363 bmc["@odata.id"] = boost::urls::format("/redfish/v1/Managers/{}",
2364 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -07002365 asyncResp->res.jsonValue["Members"] = std::move(members);
Patrick Williams5a39f772023-10-20 11:20:21 -05002366 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002367}
Ed Tanous1abe55e2018-09-05 08:30:59 -07002368} // namespace redfish