blob: f0a4e0ab215427a461a1fd2424592071481da28e [file] [log] [blame]
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Willy Tu13451e32023-05-24 16:08:18 -070018#include "bmcweb_config.h"
19
Sui Chena51fc2d2022-07-14 17:21:53 -070020#include "app.hpp"
21#include "dbus_utility.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070022#include "query.hpp"
Jennifer Leec5d03ff2019-03-08 15:42:58 -080023#include "redfish_util.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070024#include "registries/privilege_registry.hpp"
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +020025#include "utils/dbus_utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080026#include "utils/json_utils.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070027#include "utils/sw_utils.hpp"
28#include "utils/systemd_utils.hpp"
Ed Tanous2b829372022-08-03 14:22:34 -070029#include "utils/time_utils.hpp"
Borawski.Lukasz9c3106852018-02-09 15:24:22 +010030
George Liue99073f2022-12-09 11:06:16 +080031#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070032#include <boost/url/format.hpp>
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +020033#include <sdbusplus/asio/property.hpp>
34#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050035
Ed Tanousa170f272022-06-30 21:53:27 -070036#include <algorithm>
George Liue99073f2022-12-09 11:06:16 +080037#include <array>
Gunnar Mills4bfefa72020-07-30 13:54:29 -050038#include <cstdint>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050039#include <memory>
Konstantin Aladyshev9970e932024-02-20 09:51:29 +030040#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070041#include <ranges>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050042#include <sstream>
Konstantin Aladyshev9970e932024-02-20 09:51:29 +030043#include <string>
George Liue99073f2022-12-09 11:06:16 +080044#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080045#include <variant>
James Feist5b4aa862018-08-16 14:07:01 -070046
Ed Tanous1abe55e2018-09-05 08:30:59 -070047namespace redfish
48{
Jennifer Leeed5befb2018-08-10 11:29:45 -070049
50/**
Gunnar Mills2a5c4402020-05-19 09:07:24 -050051 * Function reboots the BMC.
52 *
53 * @param[in] asyncResp - Shared pointer for completing asynchronous calls
Jennifer Leeed5befb2018-08-10 11:29:45 -070054 */
zhanghch058d1b46d2021-04-01 11:18:24 +080055inline void
56 doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Gunnar Mills2a5c4402020-05-19 09:07:24 -050057{
58 const char* processName = "xyz.openbmc_project.State.BMC";
59 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
60 const char* interfaceName = "xyz.openbmc_project.State.BMC";
61 const std::string& propertyValue =
62 "xyz.openbmc_project.State.BMC.Transition.Reboot";
63 const char* destProperty = "RequestedBMCTransition";
64
65 // Create the D-Bus variant for D-Bus call.
George Liu9ae226f2023-06-21 17:56:46 +080066 sdbusplus::asio::setProperty(
67 *crow::connections::systemBus, processName, objectPath, interfaceName,
68 destProperty, propertyValue,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080069 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -070070 // Use "Set" method to set the property value.
71 if (ec)
72 {
Ed Tanous62598e32023-07-17 17:06:25 -070073 BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -070074 messages::internalError(asyncResp->res);
75 return;
76 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -050077
Ed Tanous002d39b2022-05-31 08:59:27 -070078 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -050079 });
Gunnar Mills2a5c4402020-05-19 09:07:24 -050080}
81
zhanghch058d1b46d2021-04-01 11:18:24 +080082inline void
83 doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +000084{
85 const char* processName = "xyz.openbmc_project.State.BMC";
86 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
87 const char* interfaceName = "xyz.openbmc_project.State.BMC";
88 const std::string& propertyValue =
89 "xyz.openbmc_project.State.BMC.Transition.HardReboot";
90 const char* destProperty = "RequestedBMCTransition";
91
92 // Create the D-Bus variant for D-Bus call.
George Liu9ae226f2023-06-21 17:56:46 +080093 sdbusplus::asio::setProperty(
94 *crow::connections::systemBus, processName, objectPath, interfaceName,
95 destProperty, propertyValue,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080096 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -070097 // Use "Set" method to set the property value.
98 if (ec)
99 {
Ed Tanous62598e32023-07-17 17:06:25 -0700100 BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700101 messages::internalError(asyncResp->res);
102 return;
103 }
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000104
Ed Tanous002d39b2022-05-31 08:59:27 -0700105 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -0500106 });
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000107}
108
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500109/**
110 * ManagerResetAction class supports the POST method for the Reset (reboot)
111 * action.
112 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700113inline void requestRoutesManagerResetAction(App& app)
Jennifer Leeed5befb2018-08-10 11:29:45 -0700114{
Jennifer Leeed5befb2018-08-10 11:29:45 -0700115 /**
Jennifer Leeed5befb2018-08-10 11:29:45 -0700116 * Function handles POST method request.
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500117 * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000118 * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
Jennifer Leeed5befb2018-08-10 11:29:45 -0700119 */
Jennifer Leeed5befb2018-08-10 11:29:45 -0700120
Ed Tanous253f11b2024-05-16 09:38:31 -0700121 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/Actions/Manager.Reset/")
Ed Tanoused398212021-06-09 17:05:54 -0700122 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700123 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700124 [&app](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -0700125 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
126 const std::string& managerId) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000127 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700128 {
129 return;
130 }
Ed Tanous253f11b2024-05-16 09:38:31 -0700131 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
132 {
133 messages::resourceNotFound(asyncResp->res, "Manager", managerId);
134 return;
135 }
136
Ed Tanous62598e32023-07-17 17:06:25 -0700137 BMCWEB_LOG_DEBUG("Post Manager Reset.");
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500138
Ed Tanous002d39b2022-05-31 08:59:27 -0700139 std::string resetType;
Jennifer Leeed5befb2018-08-10 11:29:45 -0700140
Ed Tanous002d39b2022-05-31 08:59:27 -0700141 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
142 resetType))
143 {
144 return;
145 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500146
Ed Tanous002d39b2022-05-31 08:59:27 -0700147 if (resetType == "GracefulRestart")
148 {
Ed Tanous62598e32023-07-17 17:06:25 -0700149 BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
Ed Tanous002d39b2022-05-31 08:59:27 -0700150 doBMCGracefulRestart(asyncResp);
151 return;
152 }
153 if (resetType == "ForceRestart")
154 {
Ed Tanous62598e32023-07-17 17:06:25 -0700155 BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
Ed Tanous002d39b2022-05-31 08:59:27 -0700156 doBMCForceRestart(asyncResp);
157 return;
158 }
Ed Tanous62598e32023-07-17 17:06:25 -0700159 BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}", resetType);
Ed Tanous002d39b2022-05-31 08:59:27 -0700160 messages::actionParameterNotSupported(asyncResp->res, resetType,
161 "ResetType");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700162
Ed Tanous002d39b2022-05-31 08:59:27 -0700163 return;
Patrick Williams5a39f772023-10-20 11:20:21 -0500164 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700165}
Jennifer Leeed5befb2018-08-10 11:29:45 -0700166
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500167/**
168 * ManagerResetToDefaultsAction class supports POST method for factory reset
169 * action.
170 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700171inline void requestRoutesManagerResetToDefaultsAction(App& app)
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500172{
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500173 /**
174 * Function handles ResetToDefaults POST method request.
175 *
176 * Analyzes POST body message and factory resets BMC by calling
177 * BMC code updater factory reset followed by a BMC reboot.
178 *
179 * BMC code updater factory reset wipes the whole BMC read-write
180 * filesystem which includes things like the network settings.
181 *
182 * OpenBMC only supports ResetToDefaultsType "ResetAll".
183 */
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500184
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700185 BMCWEB_ROUTE(app,
Ed Tanous253f11b2024-05-16 09:38:31 -0700186 "/redfish/v1/Managers/<str>/Actions/Manager.ResetToDefaults/")
Ed Tanoused398212021-06-09 17:05:54 -0700187 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700188 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700189 [&app](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -0700190 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
191 const std::string& managerId) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000192 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700193 {
194 return;
195 }
Ed Tanous253f11b2024-05-16 09:38:31 -0700196
197 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
198 {
199 messages::resourceNotFound(asyncResp->res, "Manager", managerId);
200 return;
201 }
202
Ed Tanous62598e32023-07-17 17:06:25 -0700203 BMCWEB_LOG_DEBUG("Post ResetToDefaults.");
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500204
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300205 std::optional<std::string> resetType;
206 std::optional<std::string> resetToDefaultsType;
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500207
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300208 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
209 resetType, "ResetToDefaultsType",
210 resetToDefaultsType))
Ed Tanous002d39b2022-05-31 08:59:27 -0700211 {
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300212 BMCWEB_LOG_DEBUG("Missing property ResetType.");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700213
Ed Tanous002d39b2022-05-31 08:59:27 -0700214 messages::actionParameterMissing(asyncResp->res, "ResetToDefaults",
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300215 "ResetType");
Ed Tanous002d39b2022-05-31 08:59:27 -0700216 return;
217 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700218
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300219 if (resetToDefaultsType && !resetType)
220 {
221 BMCWEB_LOG_WARNING(
222 "Using deprecated ResetToDefaultsType, should be ResetType."
223 "Support for the ResetToDefaultsType will be dropped in 2Q24");
224 resetType = resetToDefaultsType;
225 }
226
Ed Tanous002d39b2022-05-31 08:59:27 -0700227 if (resetType != "ResetAll")
228 {
Konstantin Aladyshev9970e932024-02-20 09:51:29 +0300229 BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}",
230 *resetType);
231 messages::actionParameterNotSupported(asyncResp->res, *resetType,
232 "ResetType");
Ed Tanous002d39b2022-05-31 08:59:27 -0700233 return;
234 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700235
Ed Tanous002d39b2022-05-31 08:59:27 -0700236 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800237 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700238 if (ec)
239 {
Ed Tanous62598e32023-07-17 17:06:25 -0700240 BMCWEB_LOG_DEBUG("Failed to ResetToDefaults: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700241 messages::internalError(asyncResp->res);
242 return;
243 }
244 // Factory Reset doesn't actually happen until a reboot
245 // Can't erase what the BMC is running on
246 doBMCGracefulRestart(asyncResp);
Patrick Williams5a39f772023-10-20 11:20:21 -0500247 },
Ed Tanous002d39b2022-05-31 08:59:27 -0700248 "xyz.openbmc_project.Software.BMC.Updater",
249 "/xyz/openbmc_project/software",
250 "xyz.openbmc_project.Common.FactoryReset", "Reset");
Patrick Williams5a39f772023-10-20 11:20:21 -0500251 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700252}
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500253
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530254/**
255 * ManagerResetActionInfo derived class for delivering Manager
256 * ResetType AllowableValues using ResetInfo schema.
257 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700258inline void requestRoutesManagerResetActionInfo(App& app)
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530259{
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530260 /**
261 * Functions triggers appropriate requests on DBus
262 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700263
Ed Tanous253f11b2024-05-16 09:38:31 -0700264 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/ResetActionInfo/")
Ed Tanoused398212021-06-09 17:05:54 -0700265 .privileges(redfish::privileges::getActionInfo)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700266 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700267 [&app](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -0700268 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
269 const std::string& managerId) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000270 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700271 {
272 return;
273 }
Ed Tanous14766872022-03-15 10:44:42 -0700274
Ed Tanous253f11b2024-05-16 09:38:31 -0700275 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
276 {
277 messages::resourceNotFound(asyncResp->res, "Manager", managerId);
278 return;
279 }
280
Ed Tanous002d39b2022-05-31 08:59:27 -0700281 asyncResp->res.jsonValue["@odata.type"] =
282 "#ActionInfo.v1_1_2.ActionInfo";
283 asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -0700284 boost::urls::format("/redfish/v1/Managers/{}/ResetActionInfo",
285 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -0700286 asyncResp->res.jsonValue["Name"] = "Reset Action Info";
287 asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
288 nlohmann::json::object_t parameter;
289 parameter["Name"] = "ResetType";
290 parameter["Required"] = true;
291 parameter["DataType"] = "String";
Ed Tanous14766872022-03-15 10:44:42 -0700292
Ed Tanous002d39b2022-05-31 08:59:27 -0700293 nlohmann::json::array_t allowableValues;
Patrick Williamsad539542023-05-12 10:10:08 -0500294 allowableValues.emplace_back("GracefulRestart");
295 allowableValues.emplace_back("ForceRestart");
Ed Tanous002d39b2022-05-31 08:59:27 -0700296 parameter["AllowableValues"] = std::move(allowableValues);
Ed Tanous14766872022-03-15 10:44:42 -0700297
Ed Tanous002d39b2022-05-31 08:59:27 -0700298 nlohmann::json::array_t parameters;
Patrick Williamsad539542023-05-12 10:10:08 -0500299 parameters.emplace_back(std::move(parameter));
Ed Tanous14766872022-03-15 10:44:42 -0700300
Ed Tanous002d39b2022-05-31 08:59:27 -0700301 asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
Patrick Williams5a39f772023-10-20 11:20:21 -0500302 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700303}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530304
James Feist5b4aa862018-08-16 14:07:01 -0700305static constexpr const char* objectManagerIface =
306 "org.freedesktop.DBus.ObjectManager";
307static constexpr const char* pidConfigurationIface =
308 "xyz.openbmc_project.Configuration.Pid";
309static constexpr const char* pidZoneConfigurationIface =
310 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feistb7a08d02018-12-11 14:55:37 -0800311static constexpr const char* stepwiseConfigurationIface =
312 "xyz.openbmc_project.Configuration.Stepwise";
James Feist73df0db2019-03-25 15:29:35 -0700313static constexpr const char* thermalModeIface =
314 "xyz.openbmc_project.Control.ThermalMode";
Borawski.Lukasz9c3106852018-02-09 15:24:22 +0100315
zhanghch058d1b46d2021-04-01 11:18:24 +0800316inline void
317 asyncPopulatePid(const std::string& connection, const std::string& path,
318 const std::string& currentProfile,
319 const std::vector<std::string>& supportedProfiles,
320 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
James Feist5b4aa862018-08-16 14:07:01 -0700321{
George Liu5eb468d2023-06-20 17:03:24 +0800322 sdbusplus::message::object_path objPath(path);
323 dbus::utility::getManagedObjects(
324 connection, objPath,
James Feist73df0db2019-03-25 15:29:35 -0700325 [asyncResp, currentProfile, supportedProfiles](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800326 const boost::system::error_code& ec,
James Feist73df0db2019-03-25 15:29:35 -0700327 const dbus::utility::ManagedObjectType& managedObj) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700328 if (ec)
329 {
Ed Tanous62598e32023-07-17 17:06:25 -0700330 BMCWEB_LOG_ERROR("{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700331 messages::internalError(asyncResp->res);
332 return;
333 }
334 nlohmann::json& configRoot =
335 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
336 nlohmann::json& fans = configRoot["FanControllers"];
337 fans["@odata.type"] = "#OemManager.FanControllers";
Ed Tanous253f11b2024-05-16 09:38:31 -0700338 fans["@odata.id"] = boost::urls::format(
339 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/FanControllers",
340 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -0700341
342 nlohmann::json& pids = configRoot["PidControllers"];
343 pids["@odata.type"] = "#OemManager.PidControllers";
Ed Tanous253f11b2024-05-16 09:38:31 -0700344 pids["@odata.id"] = boost::urls::format(
345 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/PidControllers",
346 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -0700347
348 nlohmann::json& stepwise = configRoot["StepwiseControllers"];
349 stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
Ed Tanous253f11b2024-05-16 09:38:31 -0700350 stepwise["@odata.id"] = boost::urls::format(
351 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/StepwiseControllers",
352 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -0700353
354 nlohmann::json& zones = configRoot["FanZones"];
Ed Tanous253f11b2024-05-16 09:38:31 -0700355 zones["@odata.id"] = boost::urls::format(
356 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/FanZones",
357 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -0700358 zones["@odata.type"] = "#OemManager.FanZones";
Ed Tanous253f11b2024-05-16 09:38:31 -0700359 configRoot["@odata.id"] =
360 boost::urls::format("/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan",
361 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -0700362 configRoot["@odata.type"] = "#OemManager.Fan";
363 configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
364
365 if (!currentProfile.empty())
366 {
367 configRoot["Profile"] = currentProfile;
368 }
Carson Labradobf2dded2023-08-10 00:37:06 +0000369 BMCWEB_LOG_DEBUG("profile = {} !", currentProfile);
Ed Tanous002d39b2022-05-31 08:59:27 -0700370
371 for (const auto& pathPair : managedObj)
372 {
373 for (const auto& intfPair : pathPair.second)
James Feist5b4aa862018-08-16 14:07:01 -0700374 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700375 if (intfPair.first != pidConfigurationIface &&
376 intfPair.first != pidZoneConfigurationIface &&
377 intfPair.first != stepwiseConfigurationIface)
James Feist5b4aa862018-08-16 14:07:01 -0700378 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700379 continue;
380 }
James Feist73df0db2019-03-25 15:29:35 -0700381
Ed Tanous002d39b2022-05-31 08:59:27 -0700382 std::string name;
James Feist73df0db2019-03-25 15:29:35 -0700383
Ed Tanous002d39b2022-05-31 08:59:27 -0700384 for (const std::pair<std::string,
385 dbus::utility::DbusVariantType>& propPair :
386 intfPair.second)
387 {
388 if (propPair.first == "Name")
James Feist73df0db2019-03-25 15:29:35 -0700389 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700390 const std::string* namePtr =
391 std::get_if<std::string>(&propPair.second);
392 if (namePtr == nullptr)
James Feist73df0db2019-03-25 15:29:35 -0700393 {
Ed Tanous62598e32023-07-17 17:06:25 -0700394 BMCWEB_LOG_ERROR("Pid Name Field illegal");
James Feistc33a90e2019-03-01 10:17:44 -0800395 messages::internalError(asyncResp->res);
396 return;
397 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700398 name = *namePtr;
399 dbus::utility::escapePathForDbus(name);
James Feistb7a08d02018-12-11 14:55:37 -0800400 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700401 else if (propPair.first == "Profiles")
James Feistb7a08d02018-12-11 14:55:37 -0800402 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700403 const std::vector<std::string>* profiles =
404 std::get_if<std::vector<std::string>>(
405 &propPair.second);
406 if (profiles == nullptr)
James Feistb7a08d02018-12-11 14:55:37 -0800407 {
Ed Tanous62598e32023-07-17 17:06:25 -0700408 BMCWEB_LOG_ERROR("Pid Profiles Field illegal");
James Feistb7a08d02018-12-11 14:55:37 -0800409 messages::internalError(asyncResp->res);
410 return;
411 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700412 if (std::find(profiles->begin(), profiles->end(),
413 currentProfile) == profiles->end())
James Feistb7a08d02018-12-11 14:55:37 -0800414 {
Ed Tanous62598e32023-07-17 17:06:25 -0700415 BMCWEB_LOG_INFO(
416 "{} not supported in current profile", name);
Ed Tanous002d39b2022-05-31 08:59:27 -0700417 continue;
James Feistb7a08d02018-12-11 14:55:37 -0800418 }
419 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700420 }
421 nlohmann::json* config = nullptr;
422 const std::string* classPtr = nullptr;
423
424 for (const std::pair<std::string,
425 dbus::utility::DbusVariantType>& propPair :
426 intfPair.second)
427 {
428 if (propPair.first == "Class")
James Feistb7a08d02018-12-11 14:55:37 -0800429 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700430 classPtr = std::get_if<std::string>(&propPair.second);
431 }
432 }
433
Ed Tanous253f11b2024-05-16 09:38:31 -0700434 boost::urls::url url(
435 boost::urls::format("/redfish/v1/Managers/{}",
436 BMCWEB_REDFISH_MANAGER_URI_NAME));
Ed Tanous002d39b2022-05-31 08:59:27 -0700437 if (intfPair.first == pidZoneConfigurationIface)
438 {
439 std::string chassis;
440 if (!dbus::utility::getNthStringFromPath(pathPair.first.str,
441 5, chassis))
442 {
443 chassis = "#IllegalValue";
444 }
445 nlohmann::json& zone = zones[name];
Ed Tanousef4c65b2023-04-24 15:28:50 -0700446 zone["Chassis"]["@odata.id"] =
447 boost::urls::format("/redfish/v1/Chassis/{}", chassis);
Willy Tueddfc432022-09-26 16:46:38 +0000448 url.set_fragment(
449 ("/Oem/OpenBmc/Fan/FanZones"_json_pointer / name)
450 .to_string());
451 zone["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700452 zone["@odata.type"] = "#OemManager.FanZone";
453 config = &zone;
454 }
455
456 else if (intfPair.first == stepwiseConfigurationIface)
457 {
458 if (classPtr == nullptr)
459 {
Ed Tanous62598e32023-07-17 17:06:25 -0700460 BMCWEB_LOG_ERROR("Pid Class Field illegal");
James Feistb7a08d02018-12-11 14:55:37 -0800461 messages::internalError(asyncResp->res);
462 return;
463 }
464
Ed Tanous002d39b2022-05-31 08:59:27 -0700465 nlohmann::json& controller = stepwise[name];
466 config = &controller;
Willy Tueddfc432022-09-26 16:46:38 +0000467 url.set_fragment(
468 ("/Oem/OpenBmc/Fan/StepwiseControllers"_json_pointer /
469 name)
470 .to_string());
471 controller["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700472 controller["@odata.type"] =
473 "#OemManager.StepwiseController";
474
475 controller["Direction"] = *classPtr;
476 }
477
478 // pid and fans are off the same configuration
479 else if (intfPair.first == pidConfigurationIface)
480 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700481 if (classPtr == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700482 {
Ed Tanous62598e32023-07-17 17:06:25 -0700483 BMCWEB_LOG_ERROR("Pid Class Field illegal");
Ed Tanous002d39b2022-05-31 08:59:27 -0700484 messages::internalError(asyncResp->res);
485 return;
486 }
487 bool isFan = *classPtr == "fan";
488 nlohmann::json& element = isFan ? fans[name] : pids[name];
489 config = &element;
490 if (isFan)
491 {
Willy Tueddfc432022-09-26 16:46:38 +0000492 url.set_fragment(
493 ("/Oem/OpenBmc/Fan/FanControllers"_json_pointer /
494 name)
495 .to_string());
496 element["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700497 element["@odata.type"] = "#OemManager.FanController";
498 }
499 else
500 {
Willy Tueddfc432022-09-26 16:46:38 +0000501 url.set_fragment(
502 ("/Oem/OpenBmc/Fan/PidControllers"_json_pointer /
503 name)
504 .to_string());
505 element["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700506 element["@odata.type"] = "#OemManager.PidController";
507 }
508 }
509 else
510 {
Ed Tanous62598e32023-07-17 17:06:25 -0700511 BMCWEB_LOG_ERROR("Unexpected configuration");
Ed Tanous002d39b2022-05-31 08:59:27 -0700512 messages::internalError(asyncResp->res);
513 return;
514 }
James Feist5b4aa862018-08-16 14:07:01 -0700515
Ed Tanous002d39b2022-05-31 08:59:27 -0700516 // used for making maps out of 2 vectors
517 const std::vector<double>* keys = nullptr;
518 const std::vector<double>* values = nullptr;
519
520 for (const auto& propertyPair : intfPair.second)
521 {
522 if (propertyPair.first == "Type" ||
523 propertyPair.first == "Class" ||
524 propertyPair.first == "Name")
525 {
526 continue;
527 }
528
529 // zones
530 if (intfPair.first == pidZoneConfigurationIface)
531 {
532 const double* ptr =
533 std::get_if<double>(&propertyPair.second);
534 if (ptr == nullptr)
535 {
Ed Tanous62598e32023-07-17 17:06:25 -0700536 BMCWEB_LOG_ERROR("Field Illegal {}",
537 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700538 messages::internalError(asyncResp->res);
539 return;
540 }
541 (*config)[propertyPair.first] = *ptr;
542 }
543
544 if (intfPair.first == stepwiseConfigurationIface)
545 {
546 if (propertyPair.first == "Reading" ||
547 propertyPair.first == "Output")
548 {
549 const std::vector<double>* ptr =
550 std::get_if<std::vector<double>>(
551 &propertyPair.second);
552
553 if (ptr == nullptr)
554 {
Ed Tanous62598e32023-07-17 17:06:25 -0700555 BMCWEB_LOG_ERROR("Field Illegal {}",
556 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700557 messages::internalError(asyncResp->res);
558 return;
559 }
560
561 if (propertyPair.first == "Reading")
562 {
563 keys = ptr;
564 }
565 else
566 {
567 values = ptr;
568 }
569 if (keys != nullptr && values != nullptr)
570 {
571 if (keys->size() != values->size())
572 {
Ed Tanous62598e32023-07-17 17:06:25 -0700573 BMCWEB_LOG_ERROR(
574 "Reading and Output size don't match ");
Ed Tanous002d39b2022-05-31 08:59:27 -0700575 messages::internalError(asyncResp->res);
576 return;
577 }
578 nlohmann::json& steps = (*config)["Steps"];
579 steps = nlohmann::json::array();
580 for (size_t ii = 0; ii < keys->size(); ii++)
581 {
582 nlohmann::json::object_t step;
583 step["Target"] = (*keys)[ii];
584 step["Output"] = (*values)[ii];
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500585 steps.emplace_back(std::move(step));
Ed Tanous002d39b2022-05-31 08:59:27 -0700586 }
587 }
588 }
589 if (propertyPair.first == "NegativeHysteresis" ||
590 propertyPair.first == "PositiveHysteresis")
James Feist5b4aa862018-08-16 14:07:01 -0700591 {
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800592 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800593 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700594 if (ptr == nullptr)
595 {
Ed Tanous62598e32023-07-17 17:06:25 -0700596 BMCWEB_LOG_ERROR("Field Illegal {}",
597 propertyPair.first);
Jason M. Billsf12894f2018-10-09 12:45:45 -0700598 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700599 return;
600 }
James Feistb7a08d02018-12-11 14:55:37 -0800601 (*config)[propertyPair.first] = *ptr;
602 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700603 }
James Feistb7a08d02018-12-11 14:55:37 -0800604
Ed Tanous002d39b2022-05-31 08:59:27 -0700605 // pid and fans are off the same configuration
606 if (intfPair.first == pidConfigurationIface ||
607 intfPair.first == stepwiseConfigurationIface)
608 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700609 if (propertyPair.first == "Zones")
James Feistb7a08d02018-12-11 14:55:37 -0800610 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700611 const std::vector<std::string>* inputs =
612 std::get_if<std::vector<std::string>>(
613 &propertyPair.second);
614
615 if (inputs == nullptr)
James Feistb7a08d02018-12-11 14:55:37 -0800616 {
Ed Tanous62598e32023-07-17 17:06:25 -0700617 BMCWEB_LOG_ERROR("Zones Pid Field Illegal");
Ed Tanous002d39b2022-05-31 08:59:27 -0700618 messages::internalError(asyncResp->res);
619 return;
James Feistb7a08d02018-12-11 14:55:37 -0800620 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700621 auto& data = (*config)[propertyPair.first];
622 data = nlohmann::json::array();
623 for (std::string itemCopy : *inputs)
James Feistb7a08d02018-12-11 14:55:37 -0800624 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700625 dbus::utility::escapePathForDbus(itemCopy);
626 nlohmann::json::object_t input;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700627 boost::urls::url managerUrl = boost::urls::format(
Ed Tanous253f11b2024-05-16 09:38:31 -0700628 "/redfish/v1/Managers/{}#{}",
629 BMCWEB_REDFISH_MANAGER_URI_NAME,
Willy Tueddfc432022-09-26 16:46:38 +0000630 ("/Oem/OpenBmc/Fan/FanZones"_json_pointer /
631 itemCopy)
632 .to_string());
633 input["@odata.id"] = std::move(managerUrl);
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500634 data.emplace_back(std::move(input));
James Feistb7a08d02018-12-11 14:55:37 -0800635 }
James Feist5b4aa862018-08-16 14:07:01 -0700636 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700637 // todo(james): may never happen, but this
638 // assumes configuration data referenced in the
639 // PID config is provided by the same daemon, we
640 // could add another loop to cover all cases,
641 // but I'm okay kicking this can down the road a
642 // bit
James Feist5b4aa862018-08-16 14:07:01 -0700643
Ed Tanous002d39b2022-05-31 08:59:27 -0700644 else if (propertyPair.first == "Inputs" ||
645 propertyPair.first == "Outputs")
James Feist5b4aa862018-08-16 14:07:01 -0700646 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700647 auto& data = (*config)[propertyPair.first];
648 const std::vector<std::string>* inputs =
649 std::get_if<std::vector<std::string>>(
650 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700651
Ed Tanous002d39b2022-05-31 08:59:27 -0700652 if (inputs == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700653 {
Ed Tanous62598e32023-07-17 17:06:25 -0700654 BMCWEB_LOG_ERROR("Field Illegal {}",
655 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700656 messages::internalError(asyncResp->res);
657 return;
James Feist5b4aa862018-08-16 14:07:01 -0700658 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700659 data = *inputs;
660 }
661 else if (propertyPair.first == "SetPointOffset")
662 {
663 const std::string* ptr =
664 std::get_if<std::string>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700665
Ed Tanous002d39b2022-05-31 08:59:27 -0700666 if (ptr == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700667 {
Ed Tanous62598e32023-07-17 17:06:25 -0700668 BMCWEB_LOG_ERROR("Field Illegal {}",
669 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700670 messages::internalError(asyncResp->res);
671 return;
James Feistb943aae2019-07-11 16:33:56 -0700672 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700673 // translate from dbus to redfish
674 if (*ptr == "WarningHigh")
James Feistb943aae2019-07-11 16:33:56 -0700675 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700676 (*config)["SetPointOffset"] =
677 "UpperThresholdNonCritical";
James Feistb943aae2019-07-11 16:33:56 -0700678 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700679 else if (*ptr == "WarningLow")
James Feist5b4aa862018-08-16 14:07:01 -0700680 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700681 (*config)["SetPointOffset"] =
682 "LowerThresholdNonCritical";
James Feist5b4aa862018-08-16 14:07:01 -0700683 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700684 else if (*ptr == "CriticalHigh")
685 {
686 (*config)["SetPointOffset"] =
687 "UpperThresholdCritical";
688 }
689 else if (*ptr == "CriticalLow")
690 {
691 (*config)["SetPointOffset"] =
692 "LowerThresholdCritical";
693 }
694 else
695 {
Ed Tanous62598e32023-07-17 17:06:25 -0700696 BMCWEB_LOG_ERROR("Value Illegal {}", *ptr);
Ed Tanous002d39b2022-05-31 08:59:27 -0700697 messages::internalError(asyncResp->res);
698 return;
699 }
700 }
701 // doubles
702 else if (propertyPair.first == "FFGainCoefficient" ||
703 propertyPair.first == "FFOffCoefficient" ||
704 propertyPair.first == "ICoefficient" ||
705 propertyPair.first == "ILimitMax" ||
706 propertyPair.first == "ILimitMin" ||
707 propertyPair.first == "PositiveHysteresis" ||
708 propertyPair.first == "NegativeHysteresis" ||
709 propertyPair.first == "OutLimitMax" ||
710 propertyPair.first == "OutLimitMin" ||
711 propertyPair.first == "PCoefficient" ||
712 propertyPair.first == "SetPoint" ||
713 propertyPair.first == "SlewNeg" ||
714 propertyPair.first == "SlewPos")
715 {
716 const double* ptr =
717 std::get_if<double>(&propertyPair.second);
718 if (ptr == nullptr)
719 {
Ed Tanous62598e32023-07-17 17:06:25 -0700720 BMCWEB_LOG_ERROR("Field Illegal {}",
721 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700722 messages::internalError(asyncResp->res);
723 return;
724 }
725 (*config)[propertyPair.first] = *ptr;
James Feist5b4aa862018-08-16 14:07:01 -0700726 }
727 }
728 }
729 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700730 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500731 });
James Feist5b4aa862018-08-16 14:07:01 -0700732}
Jennifer Leeca537922018-08-10 10:07:30 -0700733
James Feist83ff9ab2018-08-31 10:18:24 -0700734enum class CreatePIDRet
735{
736 fail,
737 del,
738 patch
739};
740
zhanghch058d1b46d2021-04-01 11:18:24 +0800741inline bool
742 getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
Ed Tanous9e9b6042024-03-06 14:18:28 -0800743 std::vector<nlohmann::json::object_t>& config,
zhanghch058d1b46d2021-04-01 11:18:24 +0800744 std::vector<std::string>& zones)
James Feist5f2caae2018-12-12 14:08:25 -0800745{
James Feistb6baeaa2019-02-21 10:41:40 -0800746 if (config.empty())
747 {
Ed Tanous62598e32023-07-17 17:06:25 -0700748 BMCWEB_LOG_ERROR("Empty Zones");
Ed Tanousf818b042022-06-27 13:17:35 -0700749 messages::propertyValueFormatError(response->res, config, "Zones");
James Feistb6baeaa2019-02-21 10:41:40 -0800750 return false;
751 }
James Feist5f2caae2018-12-12 14:08:25 -0800752 for (auto& odata : config)
753 {
754 std::string path;
Ed Tanous9e9b6042024-03-06 14:18:28 -0800755 if (!redfish::json_util::readJsonObject(odata, response->res,
756 "@odata.id", path))
James Feist5f2caae2018-12-12 14:08:25 -0800757 {
758 return false;
759 }
760 std::string input;
James Feist61adbda2019-03-25 13:03:51 -0700761
762 // 8 below comes from
763 // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
764 // 0 1 2 3 4 5 6 7 8
765 if (!dbus::utility::getNthStringFromPath(path, 8, input))
James Feist5f2caae2018-12-12 14:08:25 -0800766 {
Ed Tanous62598e32023-07-17 17:06:25 -0700767 BMCWEB_LOG_ERROR("Got invalid path {}", path);
768 BMCWEB_LOG_ERROR("Illegal Type Zones");
Ed Tanousf818b042022-06-27 13:17:35 -0700769 messages::propertyValueFormatError(response->res, odata, "Zones");
James Feist5f2caae2018-12-12 14:08:25 -0800770 return false;
771 }
Ed Tanousa170f272022-06-30 21:53:27 -0700772 std::replace(input.begin(), input.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -0800773 zones.emplace_back(std::move(input));
774 }
775 return true;
776}
777
Ed Tanous711ac7a2021-12-20 09:34:41 -0800778inline const dbus::utility::ManagedObjectType::value_type*
James Feist73df0db2019-03-25 15:29:35 -0700779 findChassis(const dbus::utility::ManagedObjectType& managedObj,
Ed Tanous9e9b6042024-03-06 14:18:28 -0800780 std::string_view value, std::string& chassis)
James Feistb6baeaa2019-02-21 10:41:40 -0800781{
Ed Tanous62598e32023-07-17 17:06:25 -0700782 BMCWEB_LOG_DEBUG("Find Chassis: {}", value);
James Feistb6baeaa2019-02-21 10:41:40 -0800783
Ed Tanous9e9b6042024-03-06 14:18:28 -0800784 std::string escaped(value);
Yaswanth Reddy M6ce82fa2023-03-10 07:29:45 +0000785 std::replace(escaped.begin(), escaped.end(), ' ', '_');
James Feistb6baeaa2019-02-21 10:41:40 -0800786 escaped = "/" + escaped;
Ed Tanous3544d2a2023-08-06 18:12:20 -0700787 auto it = std::ranges::find_if(managedObj, [&escaped](const auto& obj) {
Ed Tanous18f8f602023-07-18 10:07:23 -0700788 if (obj.first.str.ends_with(escaped))
Ed Tanous002d39b2022-05-31 08:59:27 -0700789 {
Ed Tanous62598e32023-07-17 17:06:25 -0700790 BMCWEB_LOG_DEBUG("Matched {}", obj.first.str);
Ed Tanous002d39b2022-05-31 08:59:27 -0700791 return true;
792 }
793 return false;
794 });
James Feistb6baeaa2019-02-21 10:41:40 -0800795
796 if (it == managedObj.end())
797 {
James Feist73df0db2019-03-25 15:29:35 -0700798 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800799 }
800 // 5 comes from <chassis-name> being the 5th element
801 // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
James Feist73df0db2019-03-25 15:29:35 -0700802 if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
803 {
804 return &(*it);
805 }
806
807 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800808}
809
Ed Tanous23a21a12020-07-25 04:45:05 +0000810inline CreatePIDRet createPidInterface(
zhanghch058d1b46d2021-04-01 11:18:24 +0800811 const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
Ed Tanous9e9b6042024-03-06 14:18:28 -0800812 std::string_view name, nlohmann::json& jsonValue, const std::string& path,
James Feist83ff9ab2018-08-31 10:18:24 -0700813 const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800814 dbus::utility::DBusPropertiesMap& output, std::string& chassis,
815 const std::string& profile)
James Feist83ff9ab2018-08-31 10:18:24 -0700816{
James Feist5f2caae2018-12-12 14:08:25 -0800817 // common deleter
Ed Tanous9e9b6042024-03-06 14:18:28 -0800818 if (jsonValue == nullptr)
James Feist5f2caae2018-12-12 14:08:25 -0800819 {
820 std::string iface;
821 if (type == "PidControllers" || type == "FanControllers")
822 {
823 iface = pidConfigurationIface;
824 }
825 else if (type == "FanZones")
826 {
827 iface = pidZoneConfigurationIface;
828 }
829 else if (type == "StepwiseControllers")
830 {
831 iface = stepwiseConfigurationIface;
832 }
833 else
834 {
Ed Tanous62598e32023-07-17 17:06:25 -0700835 BMCWEB_LOG_ERROR("Illegal Type {}", type);
James Feist5f2caae2018-12-12 14:08:25 -0800836 messages::propertyUnknown(response->res, type);
837 return CreatePIDRet::fail;
838 }
James Feist6ee7f772020-02-06 16:25:27 -0800839
Ed Tanous62598e32023-07-17 17:06:25 -0700840 BMCWEB_LOG_DEBUG("del {} {}", path, iface);
James Feist5f2caae2018-12-12 14:08:25 -0800841 // delete interface
842 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800843 [response, path](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700844 if (ec)
845 {
Ed Tanous62598e32023-07-17 17:06:25 -0700846 BMCWEB_LOG_ERROR("Error patching {}: {}", path, ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700847 messages::internalError(response->res);
848 return;
849 }
850 messages::success(response->res);
Patrick Williams5a39f772023-10-20 11:20:21 -0500851 },
James Feist5f2caae2018-12-12 14:08:25 -0800852 "xyz.openbmc_project.EntityManager", path, iface, "Delete");
853 return CreatePIDRet::del;
854 }
855
Ed Tanous711ac7a2021-12-20 09:34:41 -0800856 const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800857 if (!createNewObject)
858 {
859 // if we aren't creating a new object, we should be able to find it on
860 // d-bus
Ed Tanous9e9b6042024-03-06 14:18:28 -0800861 managedItem = findChassis(managedObj, name, chassis);
James Feist73df0db2019-03-25 15:29:35 -0700862 if (managedItem == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800863 {
Ed Tanous62598e32023-07-17 17:06:25 -0700864 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousef4c65b2023-04-24 15:28:50 -0700865 messages::invalidObject(
866 response->res,
867 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800868 return CreatePIDRet::fail;
869 }
870 }
871
Ed Tanous26f69762022-01-25 09:49:11 -0800872 if (!profile.empty() &&
James Feist73df0db2019-03-25 15:29:35 -0700873 (type == "PidControllers" || type == "FanControllers" ||
874 type == "StepwiseControllers"))
875 {
876 if (managedItem == nullptr)
877 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800878 output.emplace_back("Profiles", std::vector<std::string>{profile});
James Feist73df0db2019-03-25 15:29:35 -0700879 }
880 else
881 {
882 std::string interface;
883 if (type == "StepwiseControllers")
884 {
885 interface = stepwiseConfigurationIface;
886 }
887 else
888 {
889 interface = pidConfigurationIface;
890 }
Ed Tanous711ac7a2021-12-20 09:34:41 -0800891 bool ifaceFound = false;
892 for (const auto& iface : managedItem->second)
893 {
894 if (iface.first == interface)
895 {
896 ifaceFound = true;
897 for (const auto& prop : iface.second)
898 {
899 if (prop.first == "Profiles")
900 {
901 const std::vector<std::string>* curProfiles =
902 std::get_if<std::vector<std::string>>(
903 &(prop.second));
904 if (curProfiles == nullptr)
905 {
Ed Tanous62598e32023-07-17 17:06:25 -0700906 BMCWEB_LOG_ERROR(
907 "Illegal profiles in managed object");
Ed Tanous711ac7a2021-12-20 09:34:41 -0800908 messages::internalError(response->res);
909 return CreatePIDRet::fail;
910 }
911 if (std::find(curProfiles->begin(),
912 curProfiles->end(),
913 profile) == curProfiles->end())
914 {
915 std::vector<std::string> newProfiles =
916 *curProfiles;
917 newProfiles.push_back(profile);
Ed Tanousb9d36b42022-02-26 21:42:46 -0800918 output.emplace_back("Profiles", newProfiles);
Ed Tanous711ac7a2021-12-20 09:34:41 -0800919 }
920 }
921 }
922 }
923 }
924
925 if (!ifaceFound)
James Feist73df0db2019-03-25 15:29:35 -0700926 {
Ed Tanous62598e32023-07-17 17:06:25 -0700927 BMCWEB_LOG_ERROR("Failed to find interface in managed object");
James Feist73df0db2019-03-25 15:29:35 -0700928 messages::internalError(response->res);
929 return CreatePIDRet::fail;
930 }
James Feist73df0db2019-03-25 15:29:35 -0700931 }
932 }
933
James Feist83ff9ab2018-08-31 10:18:24 -0700934 if (type == "PidControllers" || type == "FanControllers")
935 {
936 if (createNewObject)
937 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800938 output.emplace_back("Class",
939 type == "PidControllers" ? "temp" : "fan");
940 output.emplace_back("Type", "Pid");
James Feist83ff9ab2018-08-31 10:18:24 -0700941 }
James Feist5f2caae2018-12-12 14:08:25 -0800942
Ed Tanous9e9b6042024-03-06 14:18:28 -0800943 std::optional<std::vector<nlohmann::json::object_t>> zones;
James Feist5f2caae2018-12-12 14:08:25 -0800944 std::optional<std::vector<std::string>> inputs;
945 std::optional<std::vector<std::string>> outputs;
946 std::map<std::string, std::optional<double>> doubles;
James Feistb943aae2019-07-11 16:33:56 -0700947 std::optional<std::string> setpointOffset;
James Feist5f2caae2018-12-12 14:08:25 -0800948 if (!redfish::json_util::readJson(
Ed Tanous9e9b6042024-03-06 14:18:28 -0800949 jsonValue, response->res, "Inputs", inputs, "Outputs", outputs,
James Feist5f2caae2018-12-12 14:08:25 -0800950 "Zones", zones, "FFGainCoefficient",
951 doubles["FFGainCoefficient"], "FFOffCoefficient",
952 doubles["FFOffCoefficient"], "ICoefficient",
953 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
954 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
955 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
956 "PCoefficient", doubles["PCoefficient"], "SetPoint",
James Feistb943aae2019-07-11 16:33:56 -0700957 doubles["SetPoint"], "SetPointOffset", setpointOffset,
958 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
959 "PositiveHysteresis", doubles["PositiveHysteresis"],
960 "NegativeHysteresis", doubles["NegativeHysteresis"]))
James Feist83ff9ab2018-08-31 10:18:24 -0700961 {
James Feist5f2caae2018-12-12 14:08:25 -0800962 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700963 }
James Feist5f2caae2018-12-12 14:08:25 -0800964 if (zones)
James Feist83ff9ab2018-08-31 10:18:24 -0700965 {
James Feist5f2caae2018-12-12 14:08:25 -0800966 std::vector<std::string> zonesStr;
967 if (!getZonesFromJsonReq(response, *zones, zonesStr))
James Feist83ff9ab2018-08-31 10:18:24 -0700968 {
Ed Tanous62598e32023-07-17 17:06:25 -0700969 BMCWEB_LOG_ERROR("Illegal Zones");
James Feist5f2caae2018-12-12 14:08:25 -0800970 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700971 }
James Feistb6baeaa2019-02-21 10:41:40 -0800972 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -0800973 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800974 {
Ed Tanous62598e32023-07-17 17:06:25 -0700975 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousace85d62021-10-26 12:45:59 -0700976 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -0700977 response->res,
978 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800979 return CreatePIDRet::fail;
980 }
Ed Tanousb9d36b42022-02-26 21:42:46 -0800981 output.emplace_back("Zones", std::move(zonesStr));
James Feist5f2caae2018-12-12 14:08:25 -0800982 }
Ed Tanousafb9ee02022-12-21 11:59:17 -0800983
984 if (inputs)
James Feist5f2caae2018-12-12 14:08:25 -0800985 {
Ed Tanousafb9ee02022-12-21 11:59:17 -0800986 for (std::string& value : *inputs)
James Feist83ff9ab2018-08-31 10:18:24 -0700987 {
Ed Tanousafb9ee02022-12-21 11:59:17 -0800988 std::replace(value.begin(), value.end(), '_', ' ');
James Feist83ff9ab2018-08-31 10:18:24 -0700989 }
Ed Tanousafb9ee02022-12-21 11:59:17 -0800990 output.emplace_back("Inputs", *inputs);
991 }
992
993 if (outputs)
994 {
995 for (std::string& value : *outputs)
996 {
997 std::replace(value.begin(), value.end(), '_', ' ');
998 }
999 output.emplace_back("Outputs", *outputs);
James Feist5f2caae2018-12-12 14:08:25 -08001000 }
James Feist83ff9ab2018-08-31 10:18:24 -07001001
James Feistb943aae2019-07-11 16:33:56 -07001002 if (setpointOffset)
1003 {
1004 // translate between redfish and dbus names
1005 if (*setpointOffset == "UpperThresholdNonCritical")
1006 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001007 output.emplace_back("SetPointOffset", "WarningLow");
James Feistb943aae2019-07-11 16:33:56 -07001008 }
1009 else if (*setpointOffset == "LowerThresholdNonCritical")
1010 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001011 output.emplace_back("SetPointOffset", "WarningHigh");
James Feistb943aae2019-07-11 16:33:56 -07001012 }
1013 else if (*setpointOffset == "LowerThresholdCritical")
1014 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001015 output.emplace_back("SetPointOffset", "CriticalLow");
James Feistb943aae2019-07-11 16:33:56 -07001016 }
1017 else if (*setpointOffset == "UpperThresholdCritical")
1018 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001019 output.emplace_back("SetPointOffset", "CriticalHigh");
James Feistb943aae2019-07-11 16:33:56 -07001020 }
1021 else
1022 {
Ed Tanous62598e32023-07-17 17:06:25 -07001023 BMCWEB_LOG_ERROR("Invalid setpointoffset {}", *setpointOffset);
Ed Tanous9e9b6042024-03-06 14:18:28 -08001024 messages::propertyValueNotInList(response->res, name,
Ed Tanousace85d62021-10-26 12:45:59 -07001025 "SetPointOffset");
James Feistb943aae2019-07-11 16:33:56 -07001026 return CreatePIDRet::fail;
1027 }
1028 }
1029
James Feist5f2caae2018-12-12 14:08:25 -08001030 // doubles
1031 for (const auto& pairs : doubles)
1032 {
1033 if (!pairs.second)
James Feist83ff9ab2018-08-31 10:18:24 -07001034 {
James Feist5f2caae2018-12-12 14:08:25 -08001035 continue;
James Feist83ff9ab2018-08-31 10:18:24 -07001036 }
Ed Tanous62598e32023-07-17 17:06:25 -07001037 BMCWEB_LOG_DEBUG("{} = {}", pairs.first, *pairs.second);
Ed Tanousb9d36b42022-02-26 21:42:46 -08001038 output.emplace_back(pairs.first, *pairs.second);
James Feist83ff9ab2018-08-31 10:18:24 -07001039 }
1040 }
James Feist5f2caae2018-12-12 14:08:25 -08001041
James Feist83ff9ab2018-08-31 10:18:24 -07001042 else if (type == "FanZones")
1043 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001044 output.emplace_back("Type", "Pid.Zone");
James Feist83ff9ab2018-08-31 10:18:24 -07001045
Ed Tanous9e9b6042024-03-06 14:18:28 -08001046 std::optional<std::string> chassisId;
James Feist5f2caae2018-12-12 14:08:25 -08001047 std::optional<double> failSafePercent;
James Feistd3ec07f2019-02-25 14:51:15 -08001048 std::optional<double> minThermalOutput;
Ed Tanous9e9b6042024-03-06 14:18:28 -08001049 if (!redfish::json_util::readJson(jsonValue, response->res,
1050 "Chassis/@odata.id", chassisId,
1051 "FailSafePercent", failSafePercent,
1052 "MinThermalOutput", minThermalOutput))
James Feist83ff9ab2018-08-31 10:18:24 -07001053 {
James Feist5f2caae2018-12-12 14:08:25 -08001054 return CreatePIDRet::fail;
1055 }
James Feist83ff9ab2018-08-31 10:18:24 -07001056
Ed Tanous9e9b6042024-03-06 14:18:28 -08001057 if (chassisId)
James Feist5f2caae2018-12-12 14:08:25 -08001058 {
AppaRao Puli717794d2019-10-18 22:54:53 +05301059 // /redfish/v1/chassis/chassis_name/
Ed Tanous9e9b6042024-03-06 14:18:28 -08001060 if (!dbus::utility::getNthStringFromPath(*chassisId, 3, chassis))
James Feist5f2caae2018-12-12 14:08:25 -08001061 {
Ed Tanous9e9b6042024-03-06 14:18:28 -08001062 BMCWEB_LOG_ERROR("Got invalid path {}", *chassisId);
Ed Tanousace85d62021-10-26 12:45:59 -07001063 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001064 response->res,
Ed Tanous9e9b6042024-03-06 14:18:28 -08001065 boost::urls::format("/redfish/v1/Chassis/{}", *chassisId));
James Feist5f2caae2018-12-12 14:08:25 -08001066 return CreatePIDRet::fail;
1067 }
1068 }
James Feistd3ec07f2019-02-25 14:51:15 -08001069 if (minThermalOutput)
James Feist5f2caae2018-12-12 14:08:25 -08001070 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001071 output.emplace_back("MinThermalOutput", *minThermalOutput);
James Feist5f2caae2018-12-12 14:08:25 -08001072 }
1073 if (failSafePercent)
1074 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001075 output.emplace_back("FailSafePercent", *failSafePercent);
James Feist5f2caae2018-12-12 14:08:25 -08001076 }
1077 }
1078 else if (type == "StepwiseControllers")
1079 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001080 output.emplace_back("Type", "Stepwise");
James Feist5f2caae2018-12-12 14:08:25 -08001081
Ed Tanous9e9b6042024-03-06 14:18:28 -08001082 std::optional<std::vector<nlohmann::json::object_t>> zones;
1083 std::optional<std::vector<nlohmann::json::object_t>> steps;
James Feist5f2caae2018-12-12 14:08:25 -08001084 std::optional<std::vector<std::string>> inputs;
1085 std::optional<double> positiveHysteresis;
1086 std::optional<double> negativeHysteresis;
James Feistc33a90e2019-03-01 10:17:44 -08001087 std::optional<std::string> direction; // upper clipping curve vs lower
James Feist5f2caae2018-12-12 14:08:25 -08001088 if (!redfish::json_util::readJson(
Ed Tanous9e9b6042024-03-06 14:18:28 -08001089 jsonValue, response->res, "Zones", zones, "Steps", steps,
James Feistb6baeaa2019-02-21 10:41:40 -08001090 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
James Feistc33a90e2019-03-01 10:17:44 -08001091 "NegativeHysteresis", negativeHysteresis, "Direction",
1092 direction))
James Feist5f2caae2018-12-12 14:08:25 -08001093 {
James Feist5f2caae2018-12-12 14:08:25 -08001094 return CreatePIDRet::fail;
1095 }
1096
1097 if (zones)
1098 {
James Feistb6baeaa2019-02-21 10:41:40 -08001099 std::vector<std::string> zonesStrs;
1100 if (!getZonesFromJsonReq(response, *zones, zonesStrs))
James Feist5f2caae2018-12-12 14:08:25 -08001101 {
Ed Tanous62598e32023-07-17 17:06:25 -07001102 BMCWEB_LOG_ERROR("Illegal Zones");
James Feist5f2caae2018-12-12 14:08:25 -08001103 return CreatePIDRet::fail;
1104 }
James Feistb6baeaa2019-02-21 10:41:40 -08001105 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -08001106 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -08001107 {
Ed Tanous62598e32023-07-17 17:06:25 -07001108 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousace85d62021-10-26 12:45:59 -07001109 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001110 response->res,
1111 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -08001112 return CreatePIDRet::fail;
1113 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001114 output.emplace_back("Zones", std::move(zonesStrs));
James Feist5f2caae2018-12-12 14:08:25 -08001115 }
1116 if (steps)
1117 {
1118 std::vector<double> readings;
1119 std::vector<double> outputs;
1120 for (auto& step : *steps)
1121 {
Ed Tanous543f4402022-01-06 13:12:53 -08001122 double target = 0.0;
1123 double out = 0.0;
James Feist5f2caae2018-12-12 14:08:25 -08001124
Ed Tanous9e9b6042024-03-06 14:18:28 -08001125 if (!redfish::json_util::readJsonObject(
1126 step, response->res, "Target", target, "Output", out))
James Feist5f2caae2018-12-12 14:08:25 -08001127 {
James Feist5f2caae2018-12-12 14:08:25 -08001128 return CreatePIDRet::fail;
1129 }
1130 readings.emplace_back(target);
Ed Tanous23a21a12020-07-25 04:45:05 +00001131 outputs.emplace_back(out);
James Feist5f2caae2018-12-12 14:08:25 -08001132 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001133 output.emplace_back("Reading", std::move(readings));
1134 output.emplace_back("Output", std::move(outputs));
James Feist5f2caae2018-12-12 14:08:25 -08001135 }
1136 if (inputs)
1137 {
1138 for (std::string& value : *inputs)
1139 {
Ed Tanousa170f272022-06-30 21:53:27 -07001140 std::replace(value.begin(), value.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -08001141 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001142 output.emplace_back("Inputs", std::move(*inputs));
James Feist5f2caae2018-12-12 14:08:25 -08001143 }
1144 if (negativeHysteresis)
1145 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001146 output.emplace_back("NegativeHysteresis", *negativeHysteresis);
James Feist5f2caae2018-12-12 14:08:25 -08001147 }
1148 if (positiveHysteresis)
1149 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001150 output.emplace_back("PositiveHysteresis", *positiveHysteresis);
James Feist83ff9ab2018-08-31 10:18:24 -07001151 }
James Feistc33a90e2019-03-01 10:17:44 -08001152 if (direction)
1153 {
1154 constexpr const std::array<const char*, 2> allowedDirections = {
1155 "Ceiling", "Floor"};
Ed Tanous3544d2a2023-08-06 18:12:20 -07001156 if (std::ranges::find(allowedDirections, *direction) ==
1157 allowedDirections.end())
James Feistc33a90e2019-03-01 10:17:44 -08001158 {
1159 messages::propertyValueTypeError(response->res, "Direction",
1160 *direction);
1161 return CreatePIDRet::fail;
1162 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001163 output.emplace_back("Class", *direction);
James Feistc33a90e2019-03-01 10:17:44 -08001164 }
James Feist83ff9ab2018-08-31 10:18:24 -07001165 }
1166 else
1167 {
Ed Tanous62598e32023-07-17 17:06:25 -07001168 BMCWEB_LOG_ERROR("Illegal Type {}", type);
Jason M. Bills35a62c72018-10-09 12:45:45 -07001169 messages::propertyUnknown(response->res, type);
James Feist83ff9ab2018-08-31 10:18:24 -07001170 return CreatePIDRet::fail;
1171 }
1172 return CreatePIDRet::patch;
1173}
James Feist73df0db2019-03-25 15:29:35 -07001174struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
1175{
Ed Tanous6936afe2022-09-08 15:10:39 -07001176 struct CompletionValues
1177 {
1178 std::vector<std::string> supportedProfiles;
1179 std::string currentProfile;
1180 dbus::utility::MapperGetSubTreeResponse subtree;
1181 };
James Feist73df0db2019-03-25 15:29:35 -07001182
Ed Tanous4e23a442022-06-06 09:57:26 -07001183 explicit GetPIDValues(
1184 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
Ed Tanous23a21a12020-07-25 04:45:05 +00001185 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001186
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001187 {}
James Feist73df0db2019-03-25 15:29:35 -07001188
1189 void run()
1190 {
1191 std::shared_ptr<GetPIDValues> self = shared_from_this();
1192
1193 // get all configurations
George Liue99073f2022-12-09 11:06:16 +08001194 constexpr std::array<std::string_view, 4> interfaces = {
1195 pidConfigurationIface, pidZoneConfigurationIface,
1196 objectManagerIface, stepwiseConfigurationIface};
1197 dbus::utility::getSubTree(
1198 "/", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001199 [self](
George Liue99073f2022-12-09 11:06:16 +08001200 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001201 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001202 if (ec)
1203 {
Ed Tanous62598e32023-07-17 17:06:25 -07001204 BMCWEB_LOG_ERROR("{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001205 messages::internalError(self->asyncResp->res);
1206 return;
1207 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001208 self->complete.subtree = subtreeLocal;
Patrick Williams5a39f772023-10-20 11:20:21 -05001209 });
James Feist73df0db2019-03-25 15:29:35 -07001210
1211 // at the same time get the selected profile
George Liue99073f2022-12-09 11:06:16 +08001212 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1213 thermalModeIface};
1214 dbus::utility::getSubTree(
1215 "/", 0, thermalModeIfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001216 [self](
George Liue99073f2022-12-09 11:06:16 +08001217 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001218 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001219 if (ec || subtreeLocal.empty())
1220 {
1221 return;
1222 }
1223 if (subtreeLocal[0].second.size() != 1)
1224 {
1225 // invalid mapper response, should never happen
Ed Tanous62598e32023-07-17 17:06:25 -07001226 BMCWEB_LOG_ERROR("GetPIDValues: Mapper Error");
Ed Tanous002d39b2022-05-31 08:59:27 -07001227 messages::internalError(self->asyncResp->res);
1228 return;
1229 }
1230
1231 const std::string& path = subtreeLocal[0].first;
1232 const std::string& owner = subtreeLocal[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001233
1234 sdbusplus::asio::getAllProperties(
1235 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous002d39b2022-05-31 08:59:27 -07001236 [path, owner,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001237 self](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001238 const dbus::utility::DBusPropertiesMap& resp) {
1239 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001240 {
Ed Tanous62598e32023-07-17 17:06:25 -07001241 BMCWEB_LOG_ERROR(
1242 "GetPIDValues: Can't get thermalModeIface {}", path);
James Feist73df0db2019-03-25 15:29:35 -07001243 messages::internalError(self->asyncResp->res);
1244 return;
1245 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001246
Ed Tanous002d39b2022-05-31 08:59:27 -07001247 const std::string* current = nullptr;
1248 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001249
1250 const bool success = sdbusplus::unpackPropertiesNoThrow(
1251 dbus_utils::UnpackErrorPrinter(), resp, "Current", current,
1252 "Supported", supported);
1253
1254 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001255 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001256 messages::internalError(self->asyncResp->res);
1257 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001258 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001259
Ed Tanous002d39b2022-05-31 08:59:27 -07001260 if (current == nullptr || supported == nullptr)
1261 {
Ed Tanous62598e32023-07-17 17:06:25 -07001262 BMCWEB_LOG_ERROR(
1263 "GetPIDValues: thermal mode iface invalid {}", path);
Ed Tanous002d39b2022-05-31 08:59:27 -07001264 messages::internalError(self->asyncResp->res);
1265 return;
1266 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001267 self->complete.currentProfile = *current;
1268 self->complete.supportedProfiles = *supported;
George Liue99073f2022-12-09 11:06:16 +08001269 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001270 });
James Feist73df0db2019-03-25 15:29:35 -07001271 }
1272
Ed Tanous6936afe2022-09-08 15:10:39 -07001273 static void
1274 processingComplete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1275 const CompletionValues& completion)
James Feist73df0db2019-03-25 15:29:35 -07001276 {
1277 if (asyncResp->res.result() != boost::beast::http::status::ok)
1278 {
1279 return;
1280 }
1281 // create map of <connection, path to objMgr>>
Ed Tanous6936afe2022-09-08 15:10:39 -07001282 boost::container::flat_map<
1283 std::string, std::string, std::less<>,
1284 std::vector<std::pair<std::string, std::string>>>
1285 objectMgrPaths;
1286 boost::container::flat_set<std::string, std::less<>,
1287 std::vector<std::string>>
1288 calledConnections;
1289 for (const auto& pathGroup : completion.subtree)
James Feist73df0db2019-03-25 15:29:35 -07001290 {
1291 for (const auto& connectionGroup : pathGroup.second)
1292 {
1293 auto findConnection =
1294 calledConnections.find(connectionGroup.first);
1295 if (findConnection != calledConnections.end())
1296 {
1297 break;
1298 }
1299 for (const std::string& interface : connectionGroup.second)
1300 {
1301 if (interface == objectManagerIface)
1302 {
1303 objectMgrPaths[connectionGroup.first] = pathGroup.first;
1304 }
1305 // this list is alphabetical, so we
1306 // should have found the objMgr by now
1307 if (interface == pidConfigurationIface ||
1308 interface == pidZoneConfigurationIface ||
1309 interface == stepwiseConfigurationIface)
1310 {
1311 auto findObjMgr =
1312 objectMgrPaths.find(connectionGroup.first);
1313 if (findObjMgr == objectMgrPaths.end())
1314 {
Ed Tanous62598e32023-07-17 17:06:25 -07001315 BMCWEB_LOG_DEBUG("{}Has no Object Manager",
1316 connectionGroup.first);
James Feist73df0db2019-03-25 15:29:35 -07001317 continue;
1318 }
1319
1320 calledConnections.insert(connectionGroup.first);
1321
1322 asyncPopulatePid(findObjMgr->first, findObjMgr->second,
Ed Tanous6936afe2022-09-08 15:10:39 -07001323 completion.currentProfile,
1324 completion.supportedProfiles,
James Feist73df0db2019-03-25 15:29:35 -07001325 asyncResp);
1326 break;
1327 }
1328 }
1329 }
1330 }
1331 }
1332
Ed Tanous6936afe2022-09-08 15:10:39 -07001333 ~GetPIDValues()
1334 {
1335 boost::asio::post(crow::connections::systemBus->get_io_context(),
1336 std::bind_front(&processingComplete, asyncResp,
1337 std::move(complete)));
1338 }
1339
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001340 GetPIDValues(const GetPIDValues&) = delete;
1341 GetPIDValues(GetPIDValues&&) = delete;
1342 GetPIDValues& operator=(const GetPIDValues&) = delete;
1343 GetPIDValues& operator=(GetPIDValues&&) = delete;
1344
zhanghch058d1b46d2021-04-01 11:18:24 +08001345 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Ed Tanous6936afe2022-09-08 15:10:39 -07001346 CompletionValues complete;
James Feist73df0db2019-03-25 15:29:35 -07001347};
1348
1349struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
1350{
Ed Tanous9e9b6042024-03-06 14:18:28 -08001351 SetPIDValues(
1352 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
1353 std::vector<
1354 std::pair<std::string, std::optional<nlohmann::json::object_t>>>&&
1355 configurationsIn,
1356 std::optional<std::string>& profileIn) :
1357 asyncResp(asyncRespIn),
1358 configuration(std::move(configurationsIn)),
1359 profile(std::move(profileIn))
1360 {}
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001361
1362 SetPIDValues(const SetPIDValues&) = delete;
1363 SetPIDValues(SetPIDValues&&) = delete;
1364 SetPIDValues& operator=(const SetPIDValues&) = delete;
1365 SetPIDValues& operator=(SetPIDValues&&) = delete;
1366
James Feist73df0db2019-03-25 15:29:35 -07001367 void run()
1368 {
1369 if (asyncResp->res.result() != boost::beast::http::status::ok)
1370 {
1371 return;
1372 }
1373
1374 std::shared_ptr<SetPIDValues> self = shared_from_this();
1375
1376 // todo(james): might make sense to do a mapper call here if this
1377 // interface gets more traction
George Liu5eb468d2023-06-20 17:03:24 +08001378 sdbusplus::message::object_path objPath(
1379 "/xyz/openbmc_project/inventory");
1380 dbus::utility::getManagedObjects(
1381 "xyz.openbmc_project.EntityManager", objPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001382 [self](const boost::system::error_code& ec,
Ed Tanous914e2d52022-01-07 11:38:34 -08001383 const dbus::utility::ManagedObjectType& mObj) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001384 if (ec)
1385 {
Ed Tanous62598e32023-07-17 17:06:25 -07001386 BMCWEB_LOG_ERROR("Error communicating to Entity Manager");
Ed Tanous002d39b2022-05-31 08:59:27 -07001387 messages::internalError(self->asyncResp->res);
1388 return;
1389 }
1390 const std::array<const char*, 3> configurations = {
1391 pidConfigurationIface, pidZoneConfigurationIface,
1392 stepwiseConfigurationIface};
James Feiste69d9de2020-02-07 12:23:27 -08001393
Ed Tanous002d39b2022-05-31 08:59:27 -07001394 for (const auto& [path, object] : mObj)
1395 {
1396 for (const auto& [interface, _] : object)
James Feiste69d9de2020-02-07 12:23:27 -08001397 {
Ed Tanous3544d2a2023-08-06 18:12:20 -07001398 if (std::ranges::find(configurations, interface) !=
1399 configurations.end())
James Feiste69d9de2020-02-07 12:23:27 -08001400 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001401 self->objectCount++;
1402 break;
James Feiste69d9de2020-02-07 12:23:27 -08001403 }
James Feiste69d9de2020-02-07 12:23:27 -08001404 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001405 }
1406 self->managedObj = mObj;
Patrick Williams5a39f772023-10-20 11:20:21 -05001407 });
James Feist73df0db2019-03-25 15:29:35 -07001408
1409 // at the same time get the profile information
George Liue99073f2022-12-09 11:06:16 +08001410 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1411 thermalModeIface};
1412 dbus::utility::getSubTree(
1413 "/", 0, thermalModeIfaces,
1414 [self](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001415 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001416 if (ec || subtree.empty())
1417 {
1418 return;
1419 }
1420 if (subtree[0].second.empty())
1421 {
1422 // invalid mapper response, should never happen
Ed Tanous62598e32023-07-17 17:06:25 -07001423 BMCWEB_LOG_ERROR("SetPIDValues: Mapper Error");
Ed Tanous002d39b2022-05-31 08:59:27 -07001424 messages::internalError(self->asyncResp->res);
1425 return;
1426 }
1427
1428 const std::string& path = subtree[0].first;
1429 const std::string& owner = subtree[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001430 sdbusplus::asio::getAllProperties(
1431 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001432 [self, path, owner](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001433 const dbus::utility::DBusPropertiesMap& r) {
1434 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001435 {
Ed Tanous62598e32023-07-17 17:06:25 -07001436 BMCWEB_LOG_ERROR(
1437 "SetPIDValues: Can't get thermalModeIface {}", path);
James Feist73df0db2019-03-25 15:29:35 -07001438 messages::internalError(self->asyncResp->res);
1439 return;
1440 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001441 const std::string* current = nullptr;
1442 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001443
1444 const bool success = sdbusplus::unpackPropertiesNoThrow(
1445 dbus_utils::UnpackErrorPrinter(), r, "Current", current,
1446 "Supported", supported);
1447
1448 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001449 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001450 messages::internalError(self->asyncResp->res);
1451 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001452 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001453
Ed Tanous002d39b2022-05-31 08:59:27 -07001454 if (current == nullptr || supported == nullptr)
1455 {
Ed Tanous62598e32023-07-17 17:06:25 -07001456 BMCWEB_LOG_ERROR(
1457 "SetPIDValues: thermal mode iface invalid {}", path);
Ed Tanous002d39b2022-05-31 08:59:27 -07001458 messages::internalError(self->asyncResp->res);
1459 return;
1460 }
1461 self->currentProfile = *current;
1462 self->supportedProfiles = *supported;
1463 self->profileConnection = owner;
1464 self->profilePath = path;
George Liue99073f2022-12-09 11:06:16 +08001465 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001466 });
James Feist73df0db2019-03-25 15:29:35 -07001467 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001468 void pidSetDone()
James Feist73df0db2019-03-25 15:29:35 -07001469 {
1470 if (asyncResp->res.result() != boost::beast::http::status::ok)
1471 {
1472 return;
1473 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001474 std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001475 if (profile)
1476 {
Ed Tanous3544d2a2023-08-06 18:12:20 -07001477 if (std::ranges::find(supportedProfiles, *profile) ==
1478 supportedProfiles.end())
James Feist73df0db2019-03-25 15:29:35 -07001479 {
1480 messages::actionParameterUnknown(response->res, "Profile",
1481 *profile);
1482 return;
1483 }
1484 currentProfile = *profile;
George Liu9ae226f2023-06-21 17:56:46 +08001485 sdbusplus::asio::setProperty(
1486 *crow::connections::systemBus, profileConnection, profilePath,
1487 thermalModeIface, "Current", *profile,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001488 [response](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001489 if (ec)
1490 {
Ed Tanous62598e32023-07-17 17:06:25 -07001491 BMCWEB_LOG_ERROR("Error patching profile{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001492 messages::internalError(response->res);
1493 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001494 });
James Feist73df0db2019-03-25 15:29:35 -07001495 }
1496
1497 for (auto& containerPair : configuration)
1498 {
1499 auto& container = containerPair.second;
1500 if (!container)
1501 {
1502 continue;
1503 }
James Feist6ee7f772020-02-06 16:25:27 -08001504
Ed Tanous02cad962022-06-30 16:50:15 -07001505 const std::string& type = containerPair.first;
James Feist73df0db2019-03-25 15:29:35 -07001506
Ed Tanous9e9b6042024-03-06 14:18:28 -08001507 for (auto& [name, value] : *container)
James Feist73df0db2019-03-25 15:29:35 -07001508 {
Potin Laicddbf3d2023-02-14 14:28:58 +08001509 std::string dbusObjName = name;
1510 std::replace(dbusObjName.begin(), dbusObjName.end(), ' ', '_');
Ed Tanous62598e32023-07-17 17:06:25 -07001511 BMCWEB_LOG_DEBUG("looking for {}", name);
James Feist6ee7f772020-02-06 16:25:27 -08001512
Ed Tanous3544d2a2023-08-06 18:12:20 -07001513 auto pathItr = std::ranges::find_if(
1514 managedObj, [&dbusObjName](const auto& obj) {
Ed Tanous91f75ca2024-06-10 13:56:43 -07001515 return obj.first.filename() == dbusObjName;
Patrick Williams5a39f772023-10-20 11:20:21 -05001516 });
Ed Tanousb9d36b42022-02-26 21:42:46 -08001517 dbus::utility::DBusPropertiesMap output;
James Feist73df0db2019-03-25 15:29:35 -07001518
1519 output.reserve(16); // The pid interface length
1520
1521 // determines if we're patching entity-manager or
1522 // creating a new object
1523 bool createNewObject = (pathItr == managedObj.end());
Ed Tanous62598e32023-07-17 17:06:25 -07001524 BMCWEB_LOG_DEBUG("Found = {}", !createNewObject);
James Feist6ee7f772020-02-06 16:25:27 -08001525
James Feist73df0db2019-03-25 15:29:35 -07001526 std::string iface;
Ed Tanousea2b6702022-03-07 16:48:38 -08001527 if (!createNewObject)
James Feist73df0db2019-03-25 15:29:35 -07001528 {
Potin Lai8be2b5b2022-11-22 13:27:16 +08001529 bool findInterface = false;
Ed Tanousea2b6702022-03-07 16:48:38 -08001530 for (const auto& interface : pathItr->second)
James Feist73df0db2019-03-25 15:29:35 -07001531 {
Ed Tanousea2b6702022-03-07 16:48:38 -08001532 if (interface.first == pidConfigurationIface)
1533 {
1534 if (type == "PidControllers" ||
1535 type == "FanControllers")
1536 {
1537 iface = pidConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001538 findInterface = true;
1539 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001540 }
1541 }
1542 else if (interface.first == pidZoneConfigurationIface)
1543 {
1544 if (type == "FanZones")
1545 {
PavanKumarIntelda393502024-03-15 05:47:02 +00001546 iface = pidZoneConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001547 findInterface = true;
1548 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001549 }
1550 }
1551 else if (interface.first == stepwiseConfigurationIface)
1552 {
1553 if (type == "StepwiseControllers")
1554 {
1555 iface = stepwiseConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001556 findInterface = true;
1557 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001558 }
1559 }
James Feist73df0db2019-03-25 15:29:35 -07001560 }
Potin Lai8be2b5b2022-11-22 13:27:16 +08001561
1562 // create new object if interface not found
1563 if (!findInterface)
1564 {
1565 createNewObject = true;
1566 }
James Feist73df0db2019-03-25 15:29:35 -07001567 }
James Feist6ee7f772020-02-06 16:25:27 -08001568
Ed Tanous9e9b6042024-03-06 14:18:28 -08001569 if (createNewObject && value == nullptr)
James Feist6ee7f772020-02-06 16:25:27 -08001570 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -05001571 // can't delete a non-existent object
Ed Tanous9e9b6042024-03-06 14:18:28 -08001572 messages::propertyValueNotInList(response->res, value,
Ed Tanouse2616cc2022-06-27 12:45:55 -07001573 name);
James Feist6ee7f772020-02-06 16:25:27 -08001574 continue;
1575 }
1576
1577 std::string path;
1578 if (pathItr != managedObj.end())
1579 {
1580 path = pathItr->first.str;
1581 }
1582
Ed Tanous62598e32023-07-17 17:06:25 -07001583 BMCWEB_LOG_DEBUG("Create new = {}", createNewObject);
James Feiste69d9de2020-02-07 12:23:27 -08001584
1585 // arbitrary limit to avoid attacks
1586 constexpr const size_t controllerLimit = 500;
James Feist14b0b8d2020-02-12 11:52:07 -08001587 if (createNewObject && objectCount >= controllerLimit)
James Feiste69d9de2020-02-07 12:23:27 -08001588 {
1589 messages::resourceExhaustion(response->res, type);
1590 continue;
1591 }
Ed Tanousa170f272022-06-30 21:53:27 -07001592 std::string escaped = name;
1593 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1594 output.emplace_back("Name", escaped);
James Feist73df0db2019-03-25 15:29:35 -07001595
1596 std::string chassis;
1597 CreatePIDRet ret = createPidInterface(
Ed Tanous9e9b6042024-03-06 14:18:28 -08001598 response, type, name, value, path, managedObj,
1599 createNewObject, output, chassis, currentProfile);
James Feist73df0db2019-03-25 15:29:35 -07001600 if (ret == CreatePIDRet::fail)
1601 {
1602 return;
1603 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001604 if (ret == CreatePIDRet::del)
James Feist73df0db2019-03-25 15:29:35 -07001605 {
1606 continue;
1607 }
1608
1609 if (!createNewObject)
1610 {
1611 for (const auto& property : output)
1612 {
Potin Lai7a696972023-11-09 12:18:20 +08001613 crow::connections::systemBus->async_method_call(
James Feist73df0db2019-03-25 15:29:35 -07001614 [response,
1615 propertyName{std::string(property.first)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001616 const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001617 if (ec)
1618 {
Ed Tanous62598e32023-07-17 17:06:25 -07001619 BMCWEB_LOG_ERROR("Error patching {}: {}",
1620 propertyName, ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001621 messages::internalError(response->res);
1622 return;
1623 }
1624 messages::success(response->res);
Potin Lai7a696972023-11-09 12:18:20 +08001625 },
1626 "xyz.openbmc_project.EntityManager", path,
1627 "org.freedesktop.DBus.Properties", "Set", iface,
1628 property.first, property.second);
James Feist73df0db2019-03-25 15:29:35 -07001629 }
1630 }
1631 else
1632 {
1633 if (chassis.empty())
1634 {
Ed Tanous62598e32023-07-17 17:06:25 -07001635 BMCWEB_LOG_ERROR("Failed to get chassis from config");
Ed Tanousace85d62021-10-26 12:45:59 -07001636 messages::internalError(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001637 return;
1638 }
1639
1640 bool foundChassis = false;
1641 for (const auto& obj : managedObj)
1642 {
Ed Tanous91f75ca2024-06-10 13:56:43 -07001643 if (obj.first.filename() == chassis)
James Feist73df0db2019-03-25 15:29:35 -07001644 {
1645 chassis = obj.first.str;
1646 foundChassis = true;
1647 break;
1648 }
1649 }
1650 if (!foundChassis)
1651 {
Ed Tanous62598e32023-07-17 17:06:25 -07001652 BMCWEB_LOG_ERROR("Failed to find chassis on dbus");
James Feist73df0db2019-03-25 15:29:35 -07001653 messages::resourceMissingAtURI(
Ed Tanousace85d62021-10-26 12:45:59 -07001654 response->res,
Ed Tanousef4c65b2023-04-24 15:28:50 -07001655 boost::urls::format("/redfish/v1/Chassis/{}",
1656 chassis));
James Feist73df0db2019-03-25 15:29:35 -07001657 return;
1658 }
1659
1660 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001661 [response](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001662 if (ec)
1663 {
Ed Tanous62598e32023-07-17 17:06:25 -07001664 BMCWEB_LOG_ERROR("Error Adding Pid Object {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001665 messages::internalError(response->res);
1666 return;
1667 }
1668 messages::success(response->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05001669 },
James Feist73df0db2019-03-25 15:29:35 -07001670 "xyz.openbmc_project.EntityManager", chassis,
1671 "xyz.openbmc_project.AddObject", "AddObject", output);
1672 }
1673 }
1674 }
1675 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001676
1677 ~SetPIDValues()
1678 {
1679 try
1680 {
1681 pidSetDone();
1682 }
1683 catch (...)
1684 {
Ed Tanous62598e32023-07-17 17:06:25 -07001685 BMCWEB_LOG_CRITICAL("pidSetDone threw exception");
Ed Tanous24b2fe82022-01-06 12:45:54 -08001686 }
1687 }
1688
zhanghch058d1b46d2021-04-01 11:18:24 +08001689 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Ed Tanous9e9b6042024-03-06 14:18:28 -08001690 std::vector<std::pair<std::string, std::optional<nlohmann::json::object_t>>>
James Feist73df0db2019-03-25 15:29:35 -07001691 configuration;
1692 std::optional<std::string> profile;
1693 dbus::utility::ManagedObjectType managedObj;
1694 std::vector<std::string> supportedProfiles;
1695 std::string currentProfile;
1696 std::string profileConnection;
1697 std::string profilePath;
James Feist14b0b8d2020-02-12 11:52:07 -08001698 size_t objectCount = 0;
James Feist73df0db2019-03-25 15:29:35 -07001699};
James Feist83ff9ab2018-08-31 10:18:24 -07001700
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001701/**
1702 * @brief Retrieves BMC manager location data over DBus
1703 *
Ed Tanousac106bf2023-06-07 09:24:59 -07001704 * @param[in] asyncResp Shared pointer for completing asynchronous calls
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001705 * @param[in] connectionName - service name
1706 * @param[in] path - object path
1707 * @return none
1708 */
Ed Tanousac106bf2023-06-07 09:24:59 -07001709inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001710 const std::string& connectionName,
1711 const std::string& path)
1712{
Ed Tanous62598e32023-07-17 17:06:25 -07001713 BMCWEB_LOG_DEBUG("Get BMC manager Location data.");
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001714
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001715 sdbusplus::asio::getProperty<std::string>(
1716 *crow::connections::systemBus, connectionName, path,
1717 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Ed Tanousac106bf2023-06-07 09:24:59 -07001718 [asyncResp](const boost::system::error_code& ec,
1719 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001720 if (ec)
1721 {
Ed Tanous62598e32023-07-17 17:06:25 -07001722 BMCWEB_LOG_DEBUG("DBUS response error for "
1723 "Location");
Ed Tanousac106bf2023-06-07 09:24:59 -07001724 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001725 return;
1726 }
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001727
Ed Tanousac106bf2023-06-07 09:24:59 -07001728 asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001729 property;
Patrick Williams5a39f772023-10-20 11:20:21 -05001730 });
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001731}
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001732// avoid name collision systems.hpp
1733inline void
Ed Tanousac106bf2023-06-07 09:24:59 -07001734 managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001735{
Ed Tanous62598e32023-07-17 17:06:25 -07001736 BMCWEB_LOG_DEBUG("Getting Manager Last Reset Time");
Ed Tanous52cc1122020-07-18 13:51:21 -07001737
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001738 sdbusplus::asio::getProperty<uint64_t>(
1739 *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
1740 "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
1741 "LastRebootTime",
Ed Tanousac106bf2023-06-07 09:24:59 -07001742 [asyncResp](const boost::system::error_code& ec,
1743 const uint64_t lastResetTime) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001744 if (ec)
1745 {
Ed Tanous62598e32023-07-17 17:06:25 -07001746 BMCWEB_LOG_DEBUG("D-BUS response error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001747 return;
1748 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001749
Ed Tanous002d39b2022-05-31 08:59:27 -07001750 // LastRebootTime is epoch time, in milliseconds
1751 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
1752 uint64_t lastResetTimeStamp = lastResetTime / 1000;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001753
Ed Tanous002d39b2022-05-31 08:59:27 -07001754 // Convert to ISO 8601 standard
Ed Tanousac106bf2023-06-07 09:24:59 -07001755 asyncResp->res.jsonValue["LastResetTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001756 redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
Patrick Williams5a39f772023-10-20 11:20:21 -05001757 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001758}
1759
1760/**
1761 * @brief Set the running firmware image
1762 *
Ed Tanousac106bf2023-06-07 09:24:59 -07001763 * @param[i,o] asyncResp - Async response object
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001764 * @param[i] runningFirmwareTarget - Image to make the running image
1765 *
1766 * @return void
1767 */
1768inline void
Ed Tanousac106bf2023-06-07 09:24:59 -07001769 setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001770 const std::string& runningFirmwareTarget)
1771{
1772 // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1773 std::string::size_type idPos = runningFirmwareTarget.rfind('/');
1774 if (idPos == std::string::npos)
1775 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001776 messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001777 "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001778 BMCWEB_LOG_DEBUG("Can't parse firmware ID!");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001779 return;
1780 }
1781 idPos++;
1782 if (idPos >= runningFirmwareTarget.size())
1783 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001784 messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001785 "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001786 BMCWEB_LOG_DEBUG("Invalid firmware ID.");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001787 return;
1788 }
1789 std::string firmwareId = runningFirmwareTarget.substr(idPos);
1790
1791 // Make sure the image is valid before setting priority
George Liu5eb468d2023-06-20 17:03:24 +08001792 sdbusplus::message::object_path objPath("/xyz/openbmc_project/software");
1793 dbus::utility::getManagedObjects(
1794 "xyz.openbmc_project.Software.BMC.Updater", objPath,
1795 [asyncResp, firmwareId, runningFirmwareTarget](
1796 const boost::system::error_code& ec,
1797 const dbus::utility::ManagedObjectType& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001798 if (ec)
1799 {
Ed Tanous62598e32023-07-17 17:06:25 -07001800 BMCWEB_LOG_DEBUG("D-Bus response error getting objects.");
Ed Tanousac106bf2023-06-07 09:24:59 -07001801 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001802 return;
1803 }
1804
1805 if (subtree.empty())
1806 {
Ed Tanous62598e32023-07-17 17:06:25 -07001807 BMCWEB_LOG_DEBUG("Can't find image!");
Ed Tanousac106bf2023-06-07 09:24:59 -07001808 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001809 return;
1810 }
1811
1812 bool foundImage = false;
Ed Tanous02cad962022-06-30 16:50:15 -07001813 for (const auto& object : subtree)
Ed Tanous002d39b2022-05-31 08:59:27 -07001814 {
1815 const std::string& path =
1816 static_cast<const std::string&>(object.first);
1817 std::size_t idPos2 = path.rfind('/');
1818
1819 if (idPos2 == std::string::npos)
1820 {
1821 continue;
1822 }
1823
1824 idPos2++;
1825 if (idPos2 >= path.size())
1826 {
1827 continue;
1828 }
1829
1830 if (path.substr(idPos2) == firmwareId)
1831 {
1832 foundImage = true;
1833 break;
1834 }
1835 }
1836
1837 if (!foundImage)
1838 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001839 messages::propertyValueNotInList(
1840 asyncResp->res, runningFirmwareTarget, "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001841 BMCWEB_LOG_DEBUG("Invalid firmware ID.");
Ed Tanous002d39b2022-05-31 08:59:27 -07001842 return;
1843 }
1844
Ed Tanous62598e32023-07-17 17:06:25 -07001845 BMCWEB_LOG_DEBUG("Setting firmware version {} to priority 0.",
1846 firmwareId);
Ed Tanous002d39b2022-05-31 08:59:27 -07001847
1848 // Only support Immediate
1849 // An addition could be a Redfish Setting like
1850 // ActiveSoftwareImageApplyTime and support OnReset
George Liu9ae226f2023-06-21 17:56:46 +08001851 sdbusplus::asio::setProperty(
1852 *crow::connections::systemBus,
1853 "xyz.openbmc_project.Software.BMC.Updater",
1854 "/xyz/openbmc_project/software/" + firmwareId,
1855 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1856 static_cast<uint8_t>(0),
Ed Tanousac106bf2023-06-07 09:24:59 -07001857 [asyncResp](const boost::system::error_code& ec2) {
Ed Tanous8a592812022-06-04 09:06:59 -07001858 if (ec2)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001859 {
Ed Tanous62598e32023-07-17 17:06:25 -07001860 BMCWEB_LOG_DEBUG("D-Bus response error setting.");
Ed Tanousac106bf2023-06-07 09:24:59 -07001861 messages::internalError(asyncResp->res);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001862 return;
1863 }
Ed Tanousac106bf2023-06-07 09:24:59 -07001864 doBMCGracefulRestart(asyncResp);
George Liu5eb468d2023-06-20 17:03:24 +08001865 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001866 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001867}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001868
Ed Tanousc51afd52024-03-07 10:13:14 -08001869inline void
1870 afterSetDateTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1871 const boost::system::error_code& ec,
1872 const sdbusplus::message_t& msg)
1873{
1874 if (ec)
1875 {
1876 BMCWEB_LOG_DEBUG("Failed to set elapsed time. DBUS response error {}",
1877 ec);
1878 const sd_bus_error* dbusError = msg.get_error();
1879 if (dbusError != nullptr)
1880 {
1881 std::string_view errorName(dbusError->name);
1882 if (errorName ==
1883 "org.freedesktop.timedate1.AutomaticTimeSyncEnabled")
1884 {
1885 BMCWEB_LOG_DEBUG("Setting conflict");
1886 messages::propertyValueConflict(
1887 asyncResp->res, "DateTime",
1888 "Managers/NetworkProtocol/NTPProcotolEnabled");
1889 return;
1890 }
1891 }
1892 messages::internalError(asyncResp->res);
1893 return;
1894 }
1895 asyncResp->res.result(boost::beast::http::status::no_content);
1896}
1897
1898inline void setDateTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1899 const std::string& datetime)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001900{
Ed Tanous62598e32023-07-17 17:06:25 -07001901 BMCWEB_LOG_DEBUG("Set date time: {}", datetime);
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001902
Ed Tanousc2e32002023-01-07 22:05:08 -08001903 std::optional<redfish::time_utils::usSinceEpoch> us =
1904 redfish::time_utils::dateStringToEpoch(datetime);
1905 if (!us)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001906 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001907 messages::propertyValueFormatError(asyncResp->res, datetime,
1908 "DateTime");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001909 return;
1910 }
Ed Tanousc51afd52024-03-07 10:13:14 -08001911 // Set the absolute datetime
1912 bool relative = false;
1913 bool interactive = false;
1914 crow::connections::systemBus->async_method_call(
1915 [asyncResp](const boost::system::error_code& ec,
1916 const sdbusplus::message_t& msg) {
1917 afterSetDateTime(asyncResp, ec, msg);
1918 },
1919 "org.freedesktop.timedate1", "/org/freedesktop/timedate1",
1920 "org.freedesktop.timedate1", "SetTime", us->count(), relative,
1921 interactive);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001922}
1923
Ed Tanous75815e52022-10-05 17:21:13 -07001924inline void
1925 checkForQuiesced(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1926{
1927 sdbusplus::asio::getProperty<std::string>(
1928 *crow::connections::systemBus, "org.freedesktop.systemd1",
1929 "/org/freedesktop/systemd1/unit/obmc-bmc-service-quiesce@0.target",
1930 "org.freedesktop.systemd1.Unit", "ActiveState",
1931 [asyncResp](const boost::system::error_code& ec,
1932 const std::string& val) {
1933 if (!ec)
1934 {
1935 if (val == "active")
1936 {
1937 asyncResp->res.jsonValue["Status"]["Health"] = "Critical";
1938 asyncResp->res.jsonValue["Status"]["State"] = "Quiesced";
1939 return;
1940 }
1941 }
1942 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
1943 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
Patrick Williams5a39f772023-10-20 11:20:21 -05001944 });
Ed Tanous75815e52022-10-05 17:21:13 -07001945}
1946
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001947inline void requestRoutesManager(App& app)
1948{
1949 std::string uuid = persistent_data::getConfig().systemUuid;
1950
Ed Tanous253f11b2024-05-16 09:38:31 -07001951 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001952 .privileges(redfish::privileges::getManager)
Ed Tanous002d39b2022-05-31 08:59:27 -07001953 .methods(boost::beast::http::verb::get)(
1954 [&app, uuid](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -07001955 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1956 const std::string& managerId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001957 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001958 {
1959 return;
1960 }
Ed Tanous253f11b2024-05-16 09:38:31 -07001961
1962 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
1963 {
1964 messages::resourceNotFound(asyncResp->res, "Manager", managerId);
1965 return;
1966 }
1967
1968 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1969 "/redfish/v1/Managers/{}", BMCWEB_REDFISH_MANAGER_URI_NAME);
Sui Chena51fc2d2022-07-14 17:21:53 -07001970 asyncResp->res.jsonValue["@odata.type"] = "#Manager.v1_14_0.Manager";
Ed Tanous253f11b2024-05-16 09:38:31 -07001971 asyncResp->res.jsonValue["Id"] = BMCWEB_REDFISH_MANAGER_URI_NAME;
Ed Tanous002d39b2022-05-31 08:59:27 -07001972 asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
1973 asyncResp->res.jsonValue["Description"] =
1974 "Baseboard Management Controller";
1975 asyncResp->res.jsonValue["PowerState"] = "On";
Ed Tanous14766872022-03-15 10:44:42 -07001976
Ed Tanous002d39b2022-05-31 08:59:27 -07001977 asyncResp->res.jsonValue["ManagerType"] = "BMC";
1978 asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
1979 asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1980 asyncResp->res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001981
Ed Tanous002d39b2022-05-31 08:59:27 -07001982 asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07001983 boost::urls::format("/redfish/v1/Managers/{}/LogServices",
1984 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -07001985 asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07001986 boost::urls::format("/redfish/v1/Managers/{}/NetworkProtocol",
1987 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -07001988 asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07001989 boost::urls::format("/redfish/v1/Managers/{}/EthernetInterfaces",
1990 BMCWEB_REDFISH_MANAGER_URI_NAME);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001991
Ed Tanous25b54db2024-04-17 15:40:31 -07001992 if constexpr (BMCWEB_VM_NBDPROXY)
Ed Tanous36c0f2a2024-02-09 13:50:26 -08001993 {
1994 asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07001995 boost::urls::format("/redfish/v1/Managers/{}/VirtualMedia",
1996 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous36c0f2a2024-02-09 13:50:26 -08001997 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001998
Ed Tanous002d39b2022-05-31 08:59:27 -07001999 // default oem data
2000 nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
2001 nlohmann::json& oemOpenbmc = oem["OpenBmc"];
2002 oem["@odata.type"] = "#OemManager.Oem";
Ed Tanous253f11b2024-05-16 09:38:31 -07002003 oem["@odata.id"] = boost::urls::format("/redfish/v1/Managers/{}#/Oem",
2004 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -07002005 oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
Ed Tanous253f11b2024-05-16 09:38:31 -07002006 oemOpenbmc["@odata.id"] =
2007 boost::urls::format("/redfish/v1/Managers/{}#/Oem/OpenBmc",
2008 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous14766872022-03-15 10:44:42 -07002009
Ed Tanous002d39b2022-05-31 08:59:27 -07002010 nlohmann::json::object_t certificates;
Ed Tanous253f11b2024-05-16 09:38:31 -07002011 certificates["@odata.id"] = boost::urls::format(
2012 "/redfish/v1/Managers/{}/Truststore/Certificates",
2013 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -07002014 oemOpenbmc["Certificates"] = std::move(certificates);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002015
Ed Tanous002d39b2022-05-31 08:59:27 -07002016 // Manager.Reset (an action) can be many values, OpenBMC only
2017 // supports BMC reboot.
2018 nlohmann::json& managerReset =
2019 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
2020 managerReset["target"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07002021 boost::urls::format("/redfish/v1/Managers/{}/Actions/Manager.Reset",
2022 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -07002023 managerReset["@Redfish.ActionInfo"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07002024 boost::urls::format("/redfish/v1/Managers/{}/ResetActionInfo",
2025 BMCWEB_REDFISH_MANAGER_URI_NAME);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002026
Ed Tanous002d39b2022-05-31 08:59:27 -07002027 // ResetToDefaults (Factory Reset) has values like
2028 // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
2029 // on OpenBMC
2030 nlohmann::json& resetToDefaults =
2031 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
Ed Tanous253f11b2024-05-16 09:38:31 -07002032 resetToDefaults["target"] = boost::urls::format(
2033 "/redfish/v1/Managers/{}/Actions/Manager.ResetToDefaults",
2034 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous613dabe2022-07-09 11:17:36 -07002035 resetToDefaults["ResetType@Redfish.AllowableValues"] =
2036 nlohmann::json::array_t({"ResetAll"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002037
Ed Tanous002d39b2022-05-31 08:59:27 -07002038 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002039 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05302040
Ed Tanous002d39b2022-05-31 08:59:27 -07002041 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2042 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2043 redfishDateTimeOffset.second;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002044
Ed Tanous002d39b2022-05-31 08:59:27 -07002045 // TODO (Gunnar): Remove these one day since moved to ComputerSystem
2046 // Still used by OCP profiles
2047 // https://github.com/opencomputeproject/OCP-Profiles/issues/23
2048 // Fill in SerialConsole info
2049 asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
2050 asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
Ed Tanous613dabe2022-07-09 11:17:36 -07002051 asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
2052 nlohmann::json::array_t({"IPMI", "SSH"});
Ed Tanous25b54db2024-04-17 15:40:31 -07002053 if constexpr (BMCWEB_KVM)
2054 {
2055 // Fill in GraphicalConsole info
2056 asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] =
2057 true;
2058 asyncResp->res
2059 .jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4;
2060 asyncResp->res
2061 .jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
2062 nlohmann::json::array_t({"KVMIP"});
2063 }
2064 if constexpr (!BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002065 {
2066 asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] =
2067 1;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002068
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002069 nlohmann::json::array_t managerForServers;
2070 nlohmann::json::object_t manager;
Ed Tanous253f11b2024-05-16 09:38:31 -07002071 manager["@odata.id"] = std::format("/redfish/v1/Systems/{}",
2072 BMCWEB_REDFISH_SYSTEM_URI_NAME);
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002073 managerForServers.emplace_back(std::move(manager));
Ed Tanous14766872022-03-15 10:44:42 -07002074
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002075 asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
2076 std::move(managerForServers);
2077 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002078
Willy Tueee00132022-06-14 14:53:17 -07002079 sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
Ed Tanous002d39b2022-05-31 08:59:27 -07002080 "FirmwareVersion", true);
2081
2082 managerGetLastResetTime(asyncResp);
2083
Sui Chena51fc2d2022-07-14 17:21:53 -07002084 // ManagerDiagnosticData is added for all BMCs.
2085 nlohmann::json& managerDiagnosticData =
2086 asyncResp->res.jsonValue["ManagerDiagnosticData"];
2087 managerDiagnosticData["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07002088 boost::urls::format("/redfish/v1/Managers/{}/ManagerDiagnosticData",
2089 BMCWEB_REDFISH_MANAGER_URI_NAME);
Sui Chena51fc2d2022-07-14 17:21:53 -07002090
Ed Tanous25b54db2024-04-17 15:40:31 -07002091 if constexpr (BMCWEB_REDFISH_OEM_MANAGER_FAN_DATA)
2092 {
2093 auto pids = std::make_shared<GetPIDValues>(asyncResp);
2094 pids->run();
2095 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002096
2097 getMainChassisId(asyncResp,
2098 [](const std::string& chassisId,
2099 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2100 aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
2101 nlohmann::json::array_t managerForChassis;
Ed Tanous8a592812022-06-04 09:06:59 -07002102 nlohmann::json::object_t managerObj;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002103 boost::urls::url chassiUrl =
2104 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
Willy Tueddfc432022-09-26 16:46:38 +00002105 managerObj["@odata.id"] = chassiUrl;
Patrick Williamsad539542023-05-12 10:10:08 -05002106 managerForChassis.emplace_back(std::move(managerObj));
Ed Tanous002d39b2022-05-31 08:59:27 -07002107 aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
2108 std::move(managerForChassis);
2109 aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00002110 chassiUrl;
Ed Tanous002d39b2022-05-31 08:59:27 -07002111 });
Ed Tanous14766872022-03-15 10:44:42 -07002112
Ed Tanous75815e52022-10-05 17:21:13 -07002113 sdbusplus::asio::getProperty<double>(
2114 *crow::connections::systemBus, "org.freedesktop.systemd1",
2115 "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
2116 "Progress",
2117 [asyncResp](const boost::system::error_code& ec, double val) {
2118 if (ec)
2119 {
Ed Tanous62598e32023-07-17 17:06:25 -07002120 BMCWEB_LOG_ERROR("Error while getting progress");
Ed Tanous75815e52022-10-05 17:21:13 -07002121 messages::internalError(asyncResp->res);
2122 return;
2123 }
2124 if (val < 1.0)
2125 {
2126 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
2127 asyncResp->res.jsonValue["Status"]["State"] = "Starting";
2128 return;
2129 }
2130 checkForQuiesced(asyncResp);
Patrick Williams5a39f772023-10-20 11:20:21 -05002131 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002132
George Liue99073f2022-12-09 11:06:16 +08002133 constexpr std::array<std::string_view, 1> interfaces = {
2134 "xyz.openbmc_project.Inventory.Item.Bmc"};
2135 dbus::utility::getSubTree(
2136 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07002137 [asyncResp](
George Liue99073f2022-12-09 11:06:16 +08002138 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07002139 const dbus::utility::MapperGetSubTreeResponse& subtree) {
2140 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002141 {
Ed Tanous62598e32023-07-17 17:06:25 -07002142 BMCWEB_LOG_DEBUG("D-Bus response error on GetSubTree {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002143 return;
2144 }
2145 if (subtree.empty())
2146 {
Ed Tanous62598e32023-07-17 17:06:25 -07002147 BMCWEB_LOG_DEBUG("Can't find bmc D-Bus object!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002148 return;
2149 }
2150 // Assume only 1 bmc D-Bus object
2151 // Throw an error if there is more than 1
2152 if (subtree.size() > 1)
2153 {
Ed Tanous62598e32023-07-17 17:06:25 -07002154 BMCWEB_LOG_DEBUG("Found more than 1 bmc D-Bus object!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002155 messages::internalError(asyncResp->res);
2156 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002157 }
2158
Ed Tanous002d39b2022-05-31 08:59:27 -07002159 if (subtree[0].first.empty() || subtree[0].second.size() != 1)
2160 {
Ed Tanous62598e32023-07-17 17:06:25 -07002161 BMCWEB_LOG_DEBUG("Error getting bmc D-Bus object!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002162 messages::internalError(asyncResp->res);
2163 return;
2164 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002165
Ed Tanous002d39b2022-05-31 08:59:27 -07002166 const std::string& path = subtree[0].first;
2167 const std::string& connectionName = subtree[0].second[0].first;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002168
Ed Tanous002d39b2022-05-31 08:59:27 -07002169 for (const auto& interfaceName : subtree[0].second[0].second)
2170 {
2171 if (interfaceName ==
2172 "xyz.openbmc_project.Inventory.Decorator.Asset")
2173 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002174 sdbusplus::asio::getAllProperties(
2175 *crow::connections::systemBus, connectionName, path,
2176 "xyz.openbmc_project.Inventory.Decorator.Asset",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002177 [asyncResp](const boost::system::error_code& ec2,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002178 const dbus::utility::DBusPropertiesMap&
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002179 propertiesList) {
Ed Tanous8a592812022-06-04 09:06:59 -07002180 if (ec2)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002181 {
Ed Tanous62598e32023-07-17 17:06:25 -07002182 BMCWEB_LOG_DEBUG("Can't get bmc asset!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002183 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002184 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002185
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002186 const std::string* partNumber = nullptr;
2187 const std::string* serialNumber = nullptr;
2188 const std::string* manufacturer = nullptr;
2189 const std::string* model = nullptr;
2190 const std::string* sparePartNumber = nullptr;
2191
2192 const bool success = sdbusplus::unpackPropertiesNoThrow(
2193 dbus_utils::UnpackErrorPrinter(), propertiesList,
2194 "PartNumber", partNumber, "SerialNumber",
2195 serialNumber, "Manufacturer", manufacturer, "Model",
2196 model, "SparePartNumber", sparePartNumber);
2197
2198 if (!success)
2199 {
2200 messages::internalError(asyncResp->res);
2201 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07002202 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002203
2204 if (partNumber != nullptr)
2205 {
2206 asyncResp->res.jsonValue["PartNumber"] =
2207 *partNumber;
2208 }
2209
2210 if (serialNumber != nullptr)
2211 {
2212 asyncResp->res.jsonValue["SerialNumber"] =
2213 *serialNumber;
2214 }
2215
2216 if (manufacturer != nullptr)
2217 {
2218 asyncResp->res.jsonValue["Manufacturer"] =
2219 *manufacturer;
2220 }
2221
2222 if (model != nullptr)
2223 {
2224 asyncResp->res.jsonValue["Model"] = *model;
2225 }
2226
2227 if (sparePartNumber != nullptr)
2228 {
2229 asyncResp->res.jsonValue["SparePartNumber"] =
2230 *sparePartNumber;
2231 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002232 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002233 }
2234 else if (interfaceName ==
2235 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
2236 {
2237 getLocation(asyncResp, connectionName, path);
2238 }
2239 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002240 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002241 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002242
Ed Tanous253f11b2024-05-16 09:38:31 -07002243 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002244 .privileges(redfish::privileges::patchManager)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002245 .methods(boost::beast::http::verb::patch)(
2246 [&app](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -07002247 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2248 const std::string& managerId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002249 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002250 {
2251 return;
2252 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002253
2254 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2255 {
2256 messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2257 return;
2258 }
2259
Ed Tanous9e9b6042024-03-06 14:18:28 -08002260 std::optional<std::string> activeSoftwareImageOdataId;
Ed Tanous002d39b2022-05-31 08:59:27 -07002261 std::optional<std::string> datetime;
Ed Tanous9e9b6042024-03-06 14:18:28 -08002262 std::optional<nlohmann::json::object_t> pidControllers;
2263 std::optional<nlohmann::json::object_t> fanControllers;
2264 std::optional<nlohmann::json::object_t> fanZones;
2265 std::optional<nlohmann::json::object_t> stepwiseControllers;
2266 std::optional<std::string> profile;
Ed Tanous002d39b2022-05-31 08:59:27 -07002267
Ed Tanous9e9b6042024-03-06 14:18:28 -08002268 // clang-format off
2269 if (!json_util::readJsonPatch(req, asyncResp->res,
2270 "DateTime", datetime,
2271 "Links/ActiveSoftwareImage/@odata.id", activeSoftwareImageOdataId,
2272 "Oem/OpenBmc/Fan/FanControllers", fanControllers,
2273 "Oem/OpenBmc/Fan/FanZones", fanZones,
2274 "Oem/OpenBmc/Fan/PidControllers", pidControllers,
2275 "Oem/OpenBmc/Fan/Profile", profile,
2276 "Oem/OpenBmc/Fan/StepwiseControllers", stepwiseControllers
2277 ))
Ed Tanous002d39b2022-05-31 08:59:27 -07002278 {
2279 return;
2280 }
Ed Tanous9e9b6042024-03-06 14:18:28 -08002281 // clang-format on
Ed Tanous002d39b2022-05-31 08:59:27 -07002282
Ed Tanous9e9b6042024-03-06 14:18:28 -08002283 if (pidControllers || fanControllers || fanZones ||
2284 stepwiseControllers || profile)
Ed Tanous002d39b2022-05-31 08:59:27 -07002285 {
Ed Tanous25b54db2024-04-17 15:40:31 -07002286 if constexpr (BMCWEB_REDFISH_OEM_MANAGER_FAN_DATA)
Ed Tanous002d39b2022-05-31 08:59:27 -07002287 {
Ed Tanous25b54db2024-04-17 15:40:31 -07002288 std::vector<std::pair<std::string,
2289 std::optional<nlohmann::json::object_t>>>
2290 configuration;
2291 if (pidControllers)
2292 {
2293 configuration.emplace_back("PidControllers",
2294 std::move(pidControllers));
2295 }
2296 if (fanControllers)
2297 {
2298 configuration.emplace_back("FanControllers",
2299 std::move(fanControllers));
2300 }
2301 if (fanZones)
2302 {
2303 configuration.emplace_back("FanZones", std::move(fanZones));
2304 }
2305 if (stepwiseControllers)
2306 {
2307 configuration.emplace_back("StepwiseControllers",
2308 std::move(stepwiseControllers));
2309 }
2310 auto pid = std::make_shared<SetPIDValues>(
2311 asyncResp, std::move(configuration), profile);
2312 pid->run();
Ed Tanous002d39b2022-05-31 08:59:27 -07002313 }
Ed Tanous25b54db2024-04-17 15:40:31 -07002314 else
Ed Tanous002d39b2022-05-31 08:59:27 -07002315 {
Ed Tanous25b54db2024-04-17 15:40:31 -07002316 messages::propertyUnknown(asyncResp->res, "Oem");
2317 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07002318 }
2319 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07002320
Ed Tanous9e9b6042024-03-06 14:18:28 -08002321 if (activeSoftwareImageOdataId)
2322 {
2323 setActiveFirmwareImage(asyncResp, *activeSoftwareImageOdataId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002324 }
Ed Tanous9e9b6042024-03-06 14:18:28 -08002325
Ed Tanous002d39b2022-05-31 08:59:27 -07002326 if (datetime)
2327 {
Ed Tanousc51afd52024-03-07 10:13:14 -08002328 setDateTime(asyncResp, *datetime);
Ed Tanous002d39b2022-05-31 08:59:27 -07002329 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002330 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002331}
2332
2333inline void requestRoutesManagerCollection(App& app)
2334{
2335 BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
Ed Tanoused398212021-06-09 17:05:54 -07002336 .privileges(redfish::privileges::getManagerCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002337 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002338 [&app](const crow::Request& req,
2339 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002340 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002341 {
2342 return;
2343 }
2344 // Collections don't include the static data added by SubRoute
2345 // because it has a duplicate entry for members
2346 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
2347 asyncResp->res.jsonValue["@odata.type"] =
2348 "#ManagerCollection.ManagerCollection";
2349 asyncResp->res.jsonValue["Name"] = "Manager Collection";
2350 asyncResp->res.jsonValue["Members@odata.count"] = 1;
2351 nlohmann::json::array_t members;
2352 nlohmann::json& bmc = members.emplace_back();
Ed Tanous253f11b2024-05-16 09:38:31 -07002353 bmc["@odata.id"] = boost::urls::format("/redfish/v1/Managers/{}",
2354 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -07002355 asyncResp->res.jsonValue["Members"] = std::move(members);
Patrick Williams5a39f772023-10-20 11:20:21 -05002356 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002357}
Ed Tanous1abe55e2018-09-05 08:30:59 -07002358} // namespace redfish