blob: c9ac95cac39d1d379aa3dbfb207fef6931771329 [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"];
Ed Tanousfc1cdd12024-07-11 11:58:16 -0700337 fans["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager.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"];
Ed Tanousfc1cdd12024-07-11 11:58:16 -0700343 pids["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager.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"];
Ed Tanousfc1cdd12024-07-11 11:58:16 -0700349 stepwise["@odata.type"] =
350 "#OpenBMCManager.v1_0_0.Manager.StepwiseControllers";
Ed Tanous253f11b2024-05-16 09:38:31 -0700351 stepwise["@odata.id"] = boost::urls::format(
352 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/StepwiseControllers",
353 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -0700354
355 nlohmann::json& zones = configRoot["FanZones"];
Ed Tanous253f11b2024-05-16 09:38:31 -0700356 zones["@odata.id"] = boost::urls::format(
357 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/FanZones",
358 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanousfc1cdd12024-07-11 11:58:16 -0700359 zones["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager.FanZones";
Ed Tanous253f11b2024-05-16 09:38:31 -0700360 configRoot["@odata.id"] =
361 boost::urls::format("/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan",
362 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanousfc1cdd12024-07-11 11:58:16 -0700363 configRoot["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager.Fan";
Ed Tanous002d39b2022-05-31 08:59:27 -0700364 configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
365
366 if (!currentProfile.empty())
367 {
368 configRoot["Profile"] = currentProfile;
369 }
Carson Labradobf2dded2023-08-10 00:37:06 +0000370 BMCWEB_LOG_DEBUG("profile = {} !", currentProfile);
Ed Tanous002d39b2022-05-31 08:59:27 -0700371
372 for (const auto& pathPair : managedObj)
373 {
374 for (const auto& intfPair : pathPair.second)
James Feist5b4aa862018-08-16 14:07:01 -0700375 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700376 if (intfPair.first != pidConfigurationIface &&
377 intfPair.first != pidZoneConfigurationIface &&
378 intfPair.first != stepwiseConfigurationIface)
James Feist5b4aa862018-08-16 14:07:01 -0700379 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700380 continue;
381 }
James Feist73df0db2019-03-25 15:29:35 -0700382
Ed Tanous002d39b2022-05-31 08:59:27 -0700383 std::string name;
James Feist73df0db2019-03-25 15:29:35 -0700384
Ed Tanous002d39b2022-05-31 08:59:27 -0700385 for (const std::pair<std::string,
386 dbus::utility::DbusVariantType>& propPair :
387 intfPair.second)
388 {
389 if (propPair.first == "Name")
James Feist73df0db2019-03-25 15:29:35 -0700390 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700391 const std::string* namePtr =
392 std::get_if<std::string>(&propPair.second);
393 if (namePtr == nullptr)
James Feist73df0db2019-03-25 15:29:35 -0700394 {
Ed Tanous62598e32023-07-17 17:06:25 -0700395 BMCWEB_LOG_ERROR("Pid Name Field illegal");
James Feistc33a90e2019-03-01 10:17:44 -0800396 messages::internalError(asyncResp->res);
397 return;
398 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700399 name = *namePtr;
400 dbus::utility::escapePathForDbus(name);
James Feistb7a08d02018-12-11 14:55:37 -0800401 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700402 else if (propPair.first == "Profiles")
James Feistb7a08d02018-12-11 14:55:37 -0800403 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700404 const std::vector<std::string>* profiles =
405 std::get_if<std::vector<std::string>>(
406 &propPair.second);
407 if (profiles == nullptr)
James Feistb7a08d02018-12-11 14:55:37 -0800408 {
Ed Tanous62598e32023-07-17 17:06:25 -0700409 BMCWEB_LOG_ERROR("Pid Profiles Field illegal");
James Feistb7a08d02018-12-11 14:55:37 -0800410 messages::internalError(asyncResp->res);
411 return;
412 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700413 if (std::find(profiles->begin(), profiles->end(),
414 currentProfile) == profiles->end())
James Feistb7a08d02018-12-11 14:55:37 -0800415 {
Ed Tanous62598e32023-07-17 17:06:25 -0700416 BMCWEB_LOG_INFO(
417 "{} not supported in current profile", name);
Ed Tanous002d39b2022-05-31 08:59:27 -0700418 continue;
James Feistb7a08d02018-12-11 14:55:37 -0800419 }
420 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700421 }
422 nlohmann::json* config = nullptr;
423 const std::string* classPtr = nullptr;
424
425 for (const std::pair<std::string,
426 dbus::utility::DbusVariantType>& propPair :
427 intfPair.second)
428 {
429 if (propPair.first == "Class")
James Feistb7a08d02018-12-11 14:55:37 -0800430 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700431 classPtr = std::get_if<std::string>(&propPair.second);
432 }
433 }
434
Ed Tanous253f11b2024-05-16 09:38:31 -0700435 boost::urls::url url(
436 boost::urls::format("/redfish/v1/Managers/{}",
437 BMCWEB_REDFISH_MANAGER_URI_NAME));
Ed Tanous002d39b2022-05-31 08:59:27 -0700438 if (intfPair.first == pidZoneConfigurationIface)
439 {
440 std::string chassis;
441 if (!dbus::utility::getNthStringFromPath(pathPair.first.str,
442 5, chassis))
443 {
444 chassis = "#IllegalValue";
445 }
446 nlohmann::json& zone = zones[name];
Ed Tanousef4c65b2023-04-24 15:28:50 -0700447 zone["Chassis"]["@odata.id"] =
448 boost::urls::format("/redfish/v1/Chassis/{}", chassis);
Willy Tueddfc432022-09-26 16:46:38 +0000449 url.set_fragment(
450 ("/Oem/OpenBmc/Fan/FanZones"_json_pointer / name)
451 .to_string());
452 zone["@odata.id"] = std::move(url);
Ed Tanousfc1cdd12024-07-11 11:58:16 -0700453 zone["@odata.type"] =
454 "#OpenBMCManager.v1_0_0.Manager.FanZone";
Ed Tanous002d39b2022-05-31 08:59:27 -0700455 config = &zone;
456 }
457
458 else if (intfPair.first == stepwiseConfigurationIface)
459 {
460 if (classPtr == nullptr)
461 {
Ed Tanous62598e32023-07-17 17:06:25 -0700462 BMCWEB_LOG_ERROR("Pid Class Field illegal");
James Feistb7a08d02018-12-11 14:55:37 -0800463 messages::internalError(asyncResp->res);
464 return;
465 }
466
Ed Tanous002d39b2022-05-31 08:59:27 -0700467 nlohmann::json& controller = stepwise[name];
468 config = &controller;
Willy Tueddfc432022-09-26 16:46:38 +0000469 url.set_fragment(
470 ("/Oem/OpenBmc/Fan/StepwiseControllers"_json_pointer /
471 name)
472 .to_string());
473 controller["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700474 controller["@odata.type"] =
Ed Tanousfc1cdd12024-07-11 11:58:16 -0700475 "#OpenBMCManager.v1_0_0.Manager.StepwiseController";
Ed Tanous002d39b2022-05-31 08:59:27 -0700476
477 controller["Direction"] = *classPtr;
478 }
479
480 // pid and fans are off the same configuration
481 else if (intfPair.first == pidConfigurationIface)
482 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700483 if (classPtr == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700484 {
Ed Tanous62598e32023-07-17 17:06:25 -0700485 BMCWEB_LOG_ERROR("Pid Class Field illegal");
Ed Tanous002d39b2022-05-31 08:59:27 -0700486 messages::internalError(asyncResp->res);
487 return;
488 }
489 bool isFan = *classPtr == "fan";
490 nlohmann::json& element = isFan ? fans[name] : pids[name];
491 config = &element;
492 if (isFan)
493 {
Willy Tueddfc432022-09-26 16:46:38 +0000494 url.set_fragment(
495 ("/Oem/OpenBmc/Fan/FanControllers"_json_pointer /
496 name)
497 .to_string());
498 element["@odata.id"] = std::move(url);
Ed Tanousfc1cdd12024-07-11 11:58:16 -0700499 element["@odata.type"] =
500 "#OpenBMCManager.v1_0_0.Manager.FanController";
Ed Tanous002d39b2022-05-31 08:59:27 -0700501 }
502 else
503 {
Willy Tueddfc432022-09-26 16:46:38 +0000504 url.set_fragment(
505 ("/Oem/OpenBmc/Fan/PidControllers"_json_pointer /
506 name)
507 .to_string());
508 element["@odata.id"] = std::move(url);
Ed Tanousfc1cdd12024-07-11 11:58:16 -0700509 element["@odata.type"] =
510 "#OpenBMCManager.v1_0_0.Manager.PidController";
Ed Tanous002d39b2022-05-31 08:59:27 -0700511 }
512 }
513 else
514 {
Ed Tanous62598e32023-07-17 17:06:25 -0700515 BMCWEB_LOG_ERROR("Unexpected configuration");
Ed Tanous002d39b2022-05-31 08:59:27 -0700516 messages::internalError(asyncResp->res);
517 return;
518 }
James Feist5b4aa862018-08-16 14:07:01 -0700519
Ed Tanous002d39b2022-05-31 08:59:27 -0700520 // used for making maps out of 2 vectors
521 const std::vector<double>* keys = nullptr;
522 const std::vector<double>* values = nullptr;
523
524 for (const auto& propertyPair : intfPair.second)
525 {
526 if (propertyPair.first == "Type" ||
527 propertyPair.first == "Class" ||
528 propertyPair.first == "Name")
529 {
530 continue;
531 }
532
533 // zones
534 if (intfPair.first == pidZoneConfigurationIface)
535 {
536 const double* ptr =
537 std::get_if<double>(&propertyPair.second);
538 if (ptr == nullptr)
539 {
Ed Tanous62598e32023-07-17 17:06:25 -0700540 BMCWEB_LOG_ERROR("Field Illegal {}",
541 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700542 messages::internalError(asyncResp->res);
543 return;
544 }
545 (*config)[propertyPair.first] = *ptr;
546 }
547
548 if (intfPair.first == stepwiseConfigurationIface)
549 {
550 if (propertyPair.first == "Reading" ||
551 propertyPair.first == "Output")
552 {
553 const std::vector<double>* ptr =
554 std::get_if<std::vector<double>>(
555 &propertyPair.second);
556
557 if (ptr == nullptr)
558 {
Ed Tanous62598e32023-07-17 17:06:25 -0700559 BMCWEB_LOG_ERROR("Field Illegal {}",
560 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700561 messages::internalError(asyncResp->res);
562 return;
563 }
564
565 if (propertyPair.first == "Reading")
566 {
567 keys = ptr;
568 }
569 else
570 {
571 values = ptr;
572 }
573 if (keys != nullptr && values != nullptr)
574 {
575 if (keys->size() != values->size())
576 {
Ed Tanous62598e32023-07-17 17:06:25 -0700577 BMCWEB_LOG_ERROR(
578 "Reading and Output size don't match ");
Ed Tanous002d39b2022-05-31 08:59:27 -0700579 messages::internalError(asyncResp->res);
580 return;
581 }
582 nlohmann::json& steps = (*config)["Steps"];
583 steps = nlohmann::json::array();
584 for (size_t ii = 0; ii < keys->size(); ii++)
585 {
586 nlohmann::json::object_t step;
587 step["Target"] = (*keys)[ii];
588 step["Output"] = (*values)[ii];
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500589 steps.emplace_back(std::move(step));
Ed Tanous002d39b2022-05-31 08:59:27 -0700590 }
591 }
592 }
593 if (propertyPair.first == "NegativeHysteresis" ||
594 propertyPair.first == "PositiveHysteresis")
James Feist5b4aa862018-08-16 14:07:01 -0700595 {
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800596 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800597 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700598 if (ptr == nullptr)
599 {
Ed Tanous62598e32023-07-17 17:06:25 -0700600 BMCWEB_LOG_ERROR("Field Illegal {}",
601 propertyPair.first);
Jason M. Billsf12894f2018-10-09 12:45:45 -0700602 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700603 return;
604 }
James Feistb7a08d02018-12-11 14:55:37 -0800605 (*config)[propertyPair.first] = *ptr;
606 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700607 }
James Feistb7a08d02018-12-11 14:55:37 -0800608
Ed Tanous002d39b2022-05-31 08:59:27 -0700609 // pid and fans are off the same configuration
610 if (intfPair.first == pidConfigurationIface ||
611 intfPair.first == stepwiseConfigurationIface)
612 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700613 if (propertyPair.first == "Zones")
James Feistb7a08d02018-12-11 14:55:37 -0800614 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700615 const std::vector<std::string>* inputs =
616 std::get_if<std::vector<std::string>>(
617 &propertyPair.second);
618
619 if (inputs == nullptr)
James Feistb7a08d02018-12-11 14:55:37 -0800620 {
Ed Tanous62598e32023-07-17 17:06:25 -0700621 BMCWEB_LOG_ERROR("Zones Pid Field Illegal");
Ed Tanous002d39b2022-05-31 08:59:27 -0700622 messages::internalError(asyncResp->res);
623 return;
James Feistb7a08d02018-12-11 14:55:37 -0800624 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700625 auto& data = (*config)[propertyPair.first];
626 data = nlohmann::json::array();
627 for (std::string itemCopy : *inputs)
James Feistb7a08d02018-12-11 14:55:37 -0800628 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700629 dbus::utility::escapePathForDbus(itemCopy);
630 nlohmann::json::object_t input;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700631 boost::urls::url managerUrl = boost::urls::format(
Ed Tanous253f11b2024-05-16 09:38:31 -0700632 "/redfish/v1/Managers/{}#{}",
633 BMCWEB_REDFISH_MANAGER_URI_NAME,
Willy Tueddfc432022-09-26 16:46:38 +0000634 ("/Oem/OpenBmc/Fan/FanZones"_json_pointer /
635 itemCopy)
636 .to_string());
637 input["@odata.id"] = std::move(managerUrl);
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500638 data.emplace_back(std::move(input));
James Feistb7a08d02018-12-11 14:55:37 -0800639 }
James Feist5b4aa862018-08-16 14:07:01 -0700640 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700641 // todo(james): may never happen, but this
642 // assumes configuration data referenced in the
643 // PID config is provided by the same daemon, we
644 // could add another loop to cover all cases,
645 // but I'm okay kicking this can down the road a
646 // bit
James Feist5b4aa862018-08-16 14:07:01 -0700647
Ed Tanous002d39b2022-05-31 08:59:27 -0700648 else if (propertyPair.first == "Inputs" ||
649 propertyPair.first == "Outputs")
James Feist5b4aa862018-08-16 14:07:01 -0700650 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700651 auto& data = (*config)[propertyPair.first];
652 const std::vector<std::string>* inputs =
653 std::get_if<std::vector<std::string>>(
654 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700655
Ed Tanous002d39b2022-05-31 08:59:27 -0700656 if (inputs == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700657 {
Ed Tanous62598e32023-07-17 17:06:25 -0700658 BMCWEB_LOG_ERROR("Field Illegal {}",
659 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700660 messages::internalError(asyncResp->res);
661 return;
James Feist5b4aa862018-08-16 14:07:01 -0700662 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700663 data = *inputs;
664 }
665 else if (propertyPair.first == "SetPointOffset")
666 {
667 const std::string* ptr =
668 std::get_if<std::string>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700669
Ed Tanous002d39b2022-05-31 08:59:27 -0700670 if (ptr == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700671 {
Ed Tanous62598e32023-07-17 17:06:25 -0700672 BMCWEB_LOG_ERROR("Field Illegal {}",
673 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700674 messages::internalError(asyncResp->res);
675 return;
James Feistb943aae2019-07-11 16:33:56 -0700676 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700677 // translate from dbus to redfish
678 if (*ptr == "WarningHigh")
James Feistb943aae2019-07-11 16:33:56 -0700679 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700680 (*config)["SetPointOffset"] =
681 "UpperThresholdNonCritical";
James Feistb943aae2019-07-11 16:33:56 -0700682 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700683 else if (*ptr == "WarningLow")
James Feist5b4aa862018-08-16 14:07:01 -0700684 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700685 (*config)["SetPointOffset"] =
686 "LowerThresholdNonCritical";
James Feist5b4aa862018-08-16 14:07:01 -0700687 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700688 else if (*ptr == "CriticalHigh")
689 {
690 (*config)["SetPointOffset"] =
691 "UpperThresholdCritical";
692 }
693 else if (*ptr == "CriticalLow")
694 {
695 (*config)["SetPointOffset"] =
696 "LowerThresholdCritical";
697 }
698 else
699 {
Ed Tanous62598e32023-07-17 17:06:25 -0700700 BMCWEB_LOG_ERROR("Value Illegal {}", *ptr);
Ed Tanous002d39b2022-05-31 08:59:27 -0700701 messages::internalError(asyncResp->res);
702 return;
703 }
704 }
705 // doubles
706 else if (propertyPair.first == "FFGainCoefficient" ||
707 propertyPair.first == "FFOffCoefficient" ||
708 propertyPair.first == "ICoefficient" ||
709 propertyPair.first == "ILimitMax" ||
710 propertyPair.first == "ILimitMin" ||
711 propertyPair.first == "PositiveHysteresis" ||
712 propertyPair.first == "NegativeHysteresis" ||
713 propertyPair.first == "OutLimitMax" ||
714 propertyPair.first == "OutLimitMin" ||
715 propertyPair.first == "PCoefficient" ||
716 propertyPair.first == "SetPoint" ||
717 propertyPair.first == "SlewNeg" ||
718 propertyPair.first == "SlewPos")
719 {
720 const double* ptr =
721 std::get_if<double>(&propertyPair.second);
722 if (ptr == nullptr)
723 {
Ed Tanous62598e32023-07-17 17:06:25 -0700724 BMCWEB_LOG_ERROR("Field Illegal {}",
725 propertyPair.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700726 messages::internalError(asyncResp->res);
727 return;
728 }
729 (*config)[propertyPair.first] = *ptr;
James Feist5b4aa862018-08-16 14:07:01 -0700730 }
731 }
732 }
733 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700734 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500735 });
James Feist5b4aa862018-08-16 14:07:01 -0700736}
Jennifer Leeca537922018-08-10 10:07:30 -0700737
James Feist83ff9ab2018-08-31 10:18:24 -0700738enum class CreatePIDRet
739{
740 fail,
741 del,
742 patch
743};
744
zhanghch058d1b46d2021-04-01 11:18:24 +0800745inline bool
746 getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
Ed Tanous9e9b6042024-03-06 14:18:28 -0800747 std::vector<nlohmann::json::object_t>& config,
zhanghch058d1b46d2021-04-01 11:18:24 +0800748 std::vector<std::string>& zones)
James Feist5f2caae2018-12-12 14:08:25 -0800749{
James Feistb6baeaa2019-02-21 10:41:40 -0800750 if (config.empty())
751 {
Ed Tanous62598e32023-07-17 17:06:25 -0700752 BMCWEB_LOG_ERROR("Empty Zones");
Ed Tanousf818b042022-06-27 13:17:35 -0700753 messages::propertyValueFormatError(response->res, config, "Zones");
James Feistb6baeaa2019-02-21 10:41:40 -0800754 return false;
755 }
James Feist5f2caae2018-12-12 14:08:25 -0800756 for (auto& odata : config)
757 {
758 std::string path;
Ed Tanous9e9b6042024-03-06 14:18:28 -0800759 if (!redfish::json_util::readJsonObject(odata, response->res,
760 "@odata.id", path))
James Feist5f2caae2018-12-12 14:08:25 -0800761 {
762 return false;
763 }
764 std::string input;
James Feist61adbda2019-03-25 13:03:51 -0700765
766 // 8 below comes from
767 // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
768 // 0 1 2 3 4 5 6 7 8
769 if (!dbus::utility::getNthStringFromPath(path, 8, input))
James Feist5f2caae2018-12-12 14:08:25 -0800770 {
Ed Tanous62598e32023-07-17 17:06:25 -0700771 BMCWEB_LOG_ERROR("Got invalid path {}", path);
772 BMCWEB_LOG_ERROR("Illegal Type Zones");
Ed Tanousf818b042022-06-27 13:17:35 -0700773 messages::propertyValueFormatError(response->res, odata, "Zones");
James Feist5f2caae2018-12-12 14:08:25 -0800774 return false;
775 }
Ed Tanousa170f272022-06-30 21:53:27 -0700776 std::replace(input.begin(), input.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -0800777 zones.emplace_back(std::move(input));
778 }
779 return true;
780}
781
Ed Tanous711ac7a2021-12-20 09:34:41 -0800782inline const dbus::utility::ManagedObjectType::value_type*
James Feist73df0db2019-03-25 15:29:35 -0700783 findChassis(const dbus::utility::ManagedObjectType& managedObj,
Ed Tanous9e9b6042024-03-06 14:18:28 -0800784 std::string_view value, std::string& chassis)
James Feistb6baeaa2019-02-21 10:41:40 -0800785{
Ed Tanous62598e32023-07-17 17:06:25 -0700786 BMCWEB_LOG_DEBUG("Find Chassis: {}", value);
James Feistb6baeaa2019-02-21 10:41:40 -0800787
Ed Tanous9e9b6042024-03-06 14:18:28 -0800788 std::string escaped(value);
Yaswanth Reddy M6ce82fa2023-03-10 07:29:45 +0000789 std::replace(escaped.begin(), escaped.end(), ' ', '_');
James Feistb6baeaa2019-02-21 10:41:40 -0800790 escaped = "/" + escaped;
Ed Tanous3544d2a2023-08-06 18:12:20 -0700791 auto it = std::ranges::find_if(managedObj, [&escaped](const auto& obj) {
Ed Tanous18f8f602023-07-18 10:07:23 -0700792 if (obj.first.str.ends_with(escaped))
Ed Tanous002d39b2022-05-31 08:59:27 -0700793 {
Ed Tanous62598e32023-07-17 17:06:25 -0700794 BMCWEB_LOG_DEBUG("Matched {}", obj.first.str);
Ed Tanous002d39b2022-05-31 08:59:27 -0700795 return true;
796 }
797 return false;
798 });
James Feistb6baeaa2019-02-21 10:41:40 -0800799
800 if (it == managedObj.end())
801 {
James Feist73df0db2019-03-25 15:29:35 -0700802 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800803 }
804 // 5 comes from <chassis-name> being the 5th element
805 // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
James Feist73df0db2019-03-25 15:29:35 -0700806 if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
807 {
808 return &(*it);
809 }
810
811 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800812}
813
Ed Tanous23a21a12020-07-25 04:45:05 +0000814inline CreatePIDRet createPidInterface(
zhanghch058d1b46d2021-04-01 11:18:24 +0800815 const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
Ed Tanous9e9b6042024-03-06 14:18:28 -0800816 std::string_view name, nlohmann::json& jsonValue, const std::string& path,
James Feist83ff9ab2018-08-31 10:18:24 -0700817 const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800818 dbus::utility::DBusPropertiesMap& output, std::string& chassis,
819 const std::string& profile)
James Feist83ff9ab2018-08-31 10:18:24 -0700820{
James Feist5f2caae2018-12-12 14:08:25 -0800821 // common deleter
Ed Tanous9e9b6042024-03-06 14:18:28 -0800822 if (jsonValue == nullptr)
James Feist5f2caae2018-12-12 14:08:25 -0800823 {
824 std::string iface;
825 if (type == "PidControllers" || type == "FanControllers")
826 {
827 iface = pidConfigurationIface;
828 }
829 else if (type == "FanZones")
830 {
831 iface = pidZoneConfigurationIface;
832 }
833 else if (type == "StepwiseControllers")
834 {
835 iface = stepwiseConfigurationIface;
836 }
837 else
838 {
Ed Tanous62598e32023-07-17 17:06:25 -0700839 BMCWEB_LOG_ERROR("Illegal Type {}", type);
James Feist5f2caae2018-12-12 14:08:25 -0800840 messages::propertyUnknown(response->res, type);
841 return CreatePIDRet::fail;
842 }
James Feist6ee7f772020-02-06 16:25:27 -0800843
Ed Tanous62598e32023-07-17 17:06:25 -0700844 BMCWEB_LOG_DEBUG("del {} {}", path, iface);
James Feist5f2caae2018-12-12 14:08:25 -0800845 // delete interface
846 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800847 [response, path](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700848 if (ec)
849 {
Ed Tanous62598e32023-07-17 17:06:25 -0700850 BMCWEB_LOG_ERROR("Error patching {}: {}", path, ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700851 messages::internalError(response->res);
852 return;
853 }
854 messages::success(response->res);
Patrick Williams5a39f772023-10-20 11:20:21 -0500855 },
James Feist5f2caae2018-12-12 14:08:25 -0800856 "xyz.openbmc_project.EntityManager", path, iface, "Delete");
857 return CreatePIDRet::del;
858 }
859
Ed Tanous711ac7a2021-12-20 09:34:41 -0800860 const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800861 if (!createNewObject)
862 {
863 // if we aren't creating a new object, we should be able to find it on
864 // d-bus
Ed Tanous9e9b6042024-03-06 14:18:28 -0800865 managedItem = findChassis(managedObj, name, chassis);
James Feist73df0db2019-03-25 15:29:35 -0700866 if (managedItem == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800867 {
Ed Tanous62598e32023-07-17 17:06:25 -0700868 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousef4c65b2023-04-24 15:28:50 -0700869 messages::invalidObject(
870 response->res,
871 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800872 return CreatePIDRet::fail;
873 }
874 }
875
Ed Tanous26f69762022-01-25 09:49:11 -0800876 if (!profile.empty() &&
James Feist73df0db2019-03-25 15:29:35 -0700877 (type == "PidControllers" || type == "FanControllers" ||
878 type == "StepwiseControllers"))
879 {
880 if (managedItem == nullptr)
881 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800882 output.emplace_back("Profiles", std::vector<std::string>{profile});
James Feist73df0db2019-03-25 15:29:35 -0700883 }
884 else
885 {
886 std::string interface;
887 if (type == "StepwiseControllers")
888 {
889 interface = stepwiseConfigurationIface;
890 }
891 else
892 {
893 interface = pidConfigurationIface;
894 }
Ed Tanous711ac7a2021-12-20 09:34:41 -0800895 bool ifaceFound = false;
896 for (const auto& iface : managedItem->second)
897 {
898 if (iface.first == interface)
899 {
900 ifaceFound = true;
901 for (const auto& prop : iface.second)
902 {
903 if (prop.first == "Profiles")
904 {
905 const std::vector<std::string>* curProfiles =
906 std::get_if<std::vector<std::string>>(
907 &(prop.second));
908 if (curProfiles == nullptr)
909 {
Ed Tanous62598e32023-07-17 17:06:25 -0700910 BMCWEB_LOG_ERROR(
911 "Illegal profiles in managed object");
Ed Tanous711ac7a2021-12-20 09:34:41 -0800912 messages::internalError(response->res);
913 return CreatePIDRet::fail;
914 }
915 if (std::find(curProfiles->begin(),
916 curProfiles->end(),
917 profile) == curProfiles->end())
918 {
919 std::vector<std::string> newProfiles =
920 *curProfiles;
921 newProfiles.push_back(profile);
Ed Tanousb9d36b42022-02-26 21:42:46 -0800922 output.emplace_back("Profiles", newProfiles);
Ed Tanous711ac7a2021-12-20 09:34:41 -0800923 }
924 }
925 }
926 }
927 }
928
929 if (!ifaceFound)
James Feist73df0db2019-03-25 15:29:35 -0700930 {
Ed Tanous62598e32023-07-17 17:06:25 -0700931 BMCWEB_LOG_ERROR("Failed to find interface in managed object");
James Feist73df0db2019-03-25 15:29:35 -0700932 messages::internalError(response->res);
933 return CreatePIDRet::fail;
934 }
James Feist73df0db2019-03-25 15:29:35 -0700935 }
936 }
937
James Feist83ff9ab2018-08-31 10:18:24 -0700938 if (type == "PidControllers" || type == "FanControllers")
939 {
940 if (createNewObject)
941 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800942 output.emplace_back("Class",
943 type == "PidControllers" ? "temp" : "fan");
944 output.emplace_back("Type", "Pid");
James Feist83ff9ab2018-08-31 10:18:24 -0700945 }
James Feist5f2caae2018-12-12 14:08:25 -0800946
Ed Tanous9e9b6042024-03-06 14:18:28 -0800947 std::optional<std::vector<nlohmann::json::object_t>> zones;
James Feist5f2caae2018-12-12 14:08:25 -0800948 std::optional<std::vector<std::string>> inputs;
949 std::optional<std::vector<std::string>> outputs;
950 std::map<std::string, std::optional<double>> doubles;
James Feistb943aae2019-07-11 16:33:56 -0700951 std::optional<std::string> setpointOffset;
James Feist5f2caae2018-12-12 14:08:25 -0800952 if (!redfish::json_util::readJson(
Ed Tanous9e9b6042024-03-06 14:18:28 -0800953 jsonValue, response->res, "Inputs", inputs, "Outputs", outputs,
James Feist5f2caae2018-12-12 14:08:25 -0800954 "Zones", zones, "FFGainCoefficient",
955 doubles["FFGainCoefficient"], "FFOffCoefficient",
956 doubles["FFOffCoefficient"], "ICoefficient",
957 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
958 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
959 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
960 "PCoefficient", doubles["PCoefficient"], "SetPoint",
James Feistb943aae2019-07-11 16:33:56 -0700961 doubles["SetPoint"], "SetPointOffset", setpointOffset,
962 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
963 "PositiveHysteresis", doubles["PositiveHysteresis"],
964 "NegativeHysteresis", doubles["NegativeHysteresis"]))
James Feist83ff9ab2018-08-31 10:18:24 -0700965 {
James Feist5f2caae2018-12-12 14:08:25 -0800966 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700967 }
James Feist5f2caae2018-12-12 14:08:25 -0800968 if (zones)
James Feist83ff9ab2018-08-31 10:18:24 -0700969 {
James Feist5f2caae2018-12-12 14:08:25 -0800970 std::vector<std::string> zonesStr;
971 if (!getZonesFromJsonReq(response, *zones, zonesStr))
James Feist83ff9ab2018-08-31 10:18:24 -0700972 {
Ed Tanous62598e32023-07-17 17:06:25 -0700973 BMCWEB_LOG_ERROR("Illegal Zones");
James Feist5f2caae2018-12-12 14:08:25 -0800974 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700975 }
James Feistb6baeaa2019-02-21 10:41:40 -0800976 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -0800977 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800978 {
Ed Tanous62598e32023-07-17 17:06:25 -0700979 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousace85d62021-10-26 12:45:59 -0700980 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -0700981 response->res,
982 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800983 return CreatePIDRet::fail;
984 }
Ed Tanousb9d36b42022-02-26 21:42:46 -0800985 output.emplace_back("Zones", std::move(zonesStr));
James Feist5f2caae2018-12-12 14:08:25 -0800986 }
Ed Tanousafb9ee02022-12-21 11:59:17 -0800987
988 if (inputs)
James Feist5f2caae2018-12-12 14:08:25 -0800989 {
Ed Tanousafb9ee02022-12-21 11:59:17 -0800990 for (std::string& value : *inputs)
James Feist83ff9ab2018-08-31 10:18:24 -0700991 {
Ed Tanousafb9ee02022-12-21 11:59:17 -0800992 std::replace(value.begin(), value.end(), '_', ' ');
James Feist83ff9ab2018-08-31 10:18:24 -0700993 }
Ed Tanousafb9ee02022-12-21 11:59:17 -0800994 output.emplace_back("Inputs", *inputs);
995 }
996
997 if (outputs)
998 {
999 for (std::string& value : *outputs)
1000 {
1001 std::replace(value.begin(), value.end(), '_', ' ');
1002 }
1003 output.emplace_back("Outputs", *outputs);
James Feist5f2caae2018-12-12 14:08:25 -08001004 }
James Feist83ff9ab2018-08-31 10:18:24 -07001005
James Feistb943aae2019-07-11 16:33:56 -07001006 if (setpointOffset)
1007 {
1008 // translate between redfish and dbus names
1009 if (*setpointOffset == "UpperThresholdNonCritical")
1010 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001011 output.emplace_back("SetPointOffset", "WarningLow");
James Feistb943aae2019-07-11 16:33:56 -07001012 }
1013 else if (*setpointOffset == "LowerThresholdNonCritical")
1014 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001015 output.emplace_back("SetPointOffset", "WarningHigh");
James Feistb943aae2019-07-11 16:33:56 -07001016 }
1017 else if (*setpointOffset == "LowerThresholdCritical")
1018 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001019 output.emplace_back("SetPointOffset", "CriticalLow");
James Feistb943aae2019-07-11 16:33:56 -07001020 }
1021 else if (*setpointOffset == "UpperThresholdCritical")
1022 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001023 output.emplace_back("SetPointOffset", "CriticalHigh");
James Feistb943aae2019-07-11 16:33:56 -07001024 }
1025 else
1026 {
Ed Tanous62598e32023-07-17 17:06:25 -07001027 BMCWEB_LOG_ERROR("Invalid setpointoffset {}", *setpointOffset);
Ed Tanous9e9b6042024-03-06 14:18:28 -08001028 messages::propertyValueNotInList(response->res, name,
Ed Tanousace85d62021-10-26 12:45:59 -07001029 "SetPointOffset");
James Feistb943aae2019-07-11 16:33:56 -07001030 return CreatePIDRet::fail;
1031 }
1032 }
1033
James Feist5f2caae2018-12-12 14:08:25 -08001034 // doubles
1035 for (const auto& pairs : doubles)
1036 {
1037 if (!pairs.second)
James Feist83ff9ab2018-08-31 10:18:24 -07001038 {
James Feist5f2caae2018-12-12 14:08:25 -08001039 continue;
James Feist83ff9ab2018-08-31 10:18:24 -07001040 }
Ed Tanous62598e32023-07-17 17:06:25 -07001041 BMCWEB_LOG_DEBUG("{} = {}", pairs.first, *pairs.second);
Ed Tanousb9d36b42022-02-26 21:42:46 -08001042 output.emplace_back(pairs.first, *pairs.second);
James Feist83ff9ab2018-08-31 10:18:24 -07001043 }
1044 }
James Feist5f2caae2018-12-12 14:08:25 -08001045
James Feist83ff9ab2018-08-31 10:18:24 -07001046 else if (type == "FanZones")
1047 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001048 output.emplace_back("Type", "Pid.Zone");
James Feist83ff9ab2018-08-31 10:18:24 -07001049
Ed Tanous9e9b6042024-03-06 14:18:28 -08001050 std::optional<std::string> chassisId;
James Feist5f2caae2018-12-12 14:08:25 -08001051 std::optional<double> failSafePercent;
James Feistd3ec07f2019-02-25 14:51:15 -08001052 std::optional<double> minThermalOutput;
Ed Tanous9e9b6042024-03-06 14:18:28 -08001053 if (!redfish::json_util::readJson(jsonValue, response->res,
1054 "Chassis/@odata.id", chassisId,
1055 "FailSafePercent", failSafePercent,
1056 "MinThermalOutput", minThermalOutput))
James Feist83ff9ab2018-08-31 10:18:24 -07001057 {
James Feist5f2caae2018-12-12 14:08:25 -08001058 return CreatePIDRet::fail;
1059 }
James Feist83ff9ab2018-08-31 10:18:24 -07001060
Ed Tanous9e9b6042024-03-06 14:18:28 -08001061 if (chassisId)
James Feist5f2caae2018-12-12 14:08:25 -08001062 {
AppaRao Puli717794d2019-10-18 22:54:53 +05301063 // /redfish/v1/chassis/chassis_name/
Ed Tanous9e9b6042024-03-06 14:18:28 -08001064 if (!dbus::utility::getNthStringFromPath(*chassisId, 3, chassis))
James Feist5f2caae2018-12-12 14:08:25 -08001065 {
Ed Tanous9e9b6042024-03-06 14:18:28 -08001066 BMCWEB_LOG_ERROR("Got invalid path {}", *chassisId);
Ed Tanousace85d62021-10-26 12:45:59 -07001067 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001068 response->res,
Ed Tanous9e9b6042024-03-06 14:18:28 -08001069 boost::urls::format("/redfish/v1/Chassis/{}", *chassisId));
James Feist5f2caae2018-12-12 14:08:25 -08001070 return CreatePIDRet::fail;
1071 }
1072 }
James Feistd3ec07f2019-02-25 14:51:15 -08001073 if (minThermalOutput)
James Feist5f2caae2018-12-12 14:08:25 -08001074 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001075 output.emplace_back("MinThermalOutput", *minThermalOutput);
James Feist5f2caae2018-12-12 14:08:25 -08001076 }
1077 if (failSafePercent)
1078 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001079 output.emplace_back("FailSafePercent", *failSafePercent);
James Feist5f2caae2018-12-12 14:08:25 -08001080 }
1081 }
1082 else if (type == "StepwiseControllers")
1083 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001084 output.emplace_back("Type", "Stepwise");
James Feist5f2caae2018-12-12 14:08:25 -08001085
Ed Tanous9e9b6042024-03-06 14:18:28 -08001086 std::optional<std::vector<nlohmann::json::object_t>> zones;
1087 std::optional<std::vector<nlohmann::json::object_t>> steps;
James Feist5f2caae2018-12-12 14:08:25 -08001088 std::optional<std::vector<std::string>> inputs;
1089 std::optional<double> positiveHysteresis;
1090 std::optional<double> negativeHysteresis;
James Feistc33a90e2019-03-01 10:17:44 -08001091 std::optional<std::string> direction; // upper clipping curve vs lower
James Feist5f2caae2018-12-12 14:08:25 -08001092 if (!redfish::json_util::readJson(
Ed Tanous9e9b6042024-03-06 14:18:28 -08001093 jsonValue, response->res, "Zones", zones, "Steps", steps,
James Feistb6baeaa2019-02-21 10:41:40 -08001094 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
James Feistc33a90e2019-03-01 10:17:44 -08001095 "NegativeHysteresis", negativeHysteresis, "Direction",
1096 direction))
James Feist5f2caae2018-12-12 14:08:25 -08001097 {
James Feist5f2caae2018-12-12 14:08:25 -08001098 return CreatePIDRet::fail;
1099 }
1100
1101 if (zones)
1102 {
James Feistb6baeaa2019-02-21 10:41:40 -08001103 std::vector<std::string> zonesStrs;
1104 if (!getZonesFromJsonReq(response, *zones, zonesStrs))
James Feist5f2caae2018-12-12 14:08:25 -08001105 {
Ed Tanous62598e32023-07-17 17:06:25 -07001106 BMCWEB_LOG_ERROR("Illegal Zones");
James Feist5f2caae2018-12-12 14:08:25 -08001107 return CreatePIDRet::fail;
1108 }
James Feistb6baeaa2019-02-21 10:41:40 -08001109 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -08001110 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -08001111 {
Ed Tanous62598e32023-07-17 17:06:25 -07001112 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
Ed Tanousace85d62021-10-26 12:45:59 -07001113 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001114 response->res,
1115 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -08001116 return CreatePIDRet::fail;
1117 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001118 output.emplace_back("Zones", std::move(zonesStrs));
James Feist5f2caae2018-12-12 14:08:25 -08001119 }
1120 if (steps)
1121 {
1122 std::vector<double> readings;
1123 std::vector<double> outputs;
1124 for (auto& step : *steps)
1125 {
Ed Tanous543f4402022-01-06 13:12:53 -08001126 double target = 0.0;
1127 double out = 0.0;
James Feist5f2caae2018-12-12 14:08:25 -08001128
Ed Tanous9e9b6042024-03-06 14:18:28 -08001129 if (!redfish::json_util::readJsonObject(
1130 step, response->res, "Target", target, "Output", out))
James Feist5f2caae2018-12-12 14:08:25 -08001131 {
James Feist5f2caae2018-12-12 14:08:25 -08001132 return CreatePIDRet::fail;
1133 }
1134 readings.emplace_back(target);
Ed Tanous23a21a12020-07-25 04:45:05 +00001135 outputs.emplace_back(out);
James Feist5f2caae2018-12-12 14:08:25 -08001136 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001137 output.emplace_back("Reading", std::move(readings));
1138 output.emplace_back("Output", std::move(outputs));
James Feist5f2caae2018-12-12 14:08:25 -08001139 }
1140 if (inputs)
1141 {
1142 for (std::string& value : *inputs)
1143 {
Ed Tanousa170f272022-06-30 21:53:27 -07001144 std::replace(value.begin(), value.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -08001145 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001146 output.emplace_back("Inputs", std::move(*inputs));
James Feist5f2caae2018-12-12 14:08:25 -08001147 }
1148 if (negativeHysteresis)
1149 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001150 output.emplace_back("NegativeHysteresis", *negativeHysteresis);
James Feist5f2caae2018-12-12 14:08:25 -08001151 }
1152 if (positiveHysteresis)
1153 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001154 output.emplace_back("PositiveHysteresis", *positiveHysteresis);
James Feist83ff9ab2018-08-31 10:18:24 -07001155 }
James Feistc33a90e2019-03-01 10:17:44 -08001156 if (direction)
1157 {
1158 constexpr const std::array<const char*, 2> allowedDirections = {
1159 "Ceiling", "Floor"};
Ed Tanous3544d2a2023-08-06 18:12:20 -07001160 if (std::ranges::find(allowedDirections, *direction) ==
1161 allowedDirections.end())
James Feistc33a90e2019-03-01 10:17:44 -08001162 {
1163 messages::propertyValueTypeError(response->res, "Direction",
1164 *direction);
1165 return CreatePIDRet::fail;
1166 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001167 output.emplace_back("Class", *direction);
James Feistc33a90e2019-03-01 10:17:44 -08001168 }
James Feist83ff9ab2018-08-31 10:18:24 -07001169 }
1170 else
1171 {
Ed Tanous62598e32023-07-17 17:06:25 -07001172 BMCWEB_LOG_ERROR("Illegal Type {}", type);
Jason M. Bills35a62c72018-10-09 12:45:45 -07001173 messages::propertyUnknown(response->res, type);
James Feist83ff9ab2018-08-31 10:18:24 -07001174 return CreatePIDRet::fail;
1175 }
1176 return CreatePIDRet::patch;
1177}
James Feist73df0db2019-03-25 15:29:35 -07001178struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
1179{
Ed Tanous6936afe2022-09-08 15:10:39 -07001180 struct CompletionValues
1181 {
1182 std::vector<std::string> supportedProfiles;
1183 std::string currentProfile;
1184 dbus::utility::MapperGetSubTreeResponse subtree;
1185 };
James Feist73df0db2019-03-25 15:29:35 -07001186
Ed Tanous4e23a442022-06-06 09:57:26 -07001187 explicit GetPIDValues(
1188 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
Ed Tanous23a21a12020-07-25 04:45:05 +00001189 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001190
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001191 {}
James Feist73df0db2019-03-25 15:29:35 -07001192
1193 void run()
1194 {
1195 std::shared_ptr<GetPIDValues> self = shared_from_this();
1196
1197 // get all configurations
George Liue99073f2022-12-09 11:06:16 +08001198 constexpr std::array<std::string_view, 4> interfaces = {
1199 pidConfigurationIface, pidZoneConfigurationIface,
1200 objectManagerIface, stepwiseConfigurationIface};
1201 dbus::utility::getSubTree(
1202 "/", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001203 [self](
George Liue99073f2022-12-09 11:06:16 +08001204 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001205 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001206 if (ec)
1207 {
Ed Tanous62598e32023-07-17 17:06:25 -07001208 BMCWEB_LOG_ERROR("{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001209 messages::internalError(self->asyncResp->res);
1210 return;
1211 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001212 self->complete.subtree = subtreeLocal;
Patrick Williams5a39f772023-10-20 11:20:21 -05001213 });
James Feist73df0db2019-03-25 15:29:35 -07001214
1215 // at the same time get the selected profile
George Liue99073f2022-12-09 11:06:16 +08001216 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1217 thermalModeIface};
1218 dbus::utility::getSubTree(
1219 "/", 0, thermalModeIfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001220 [self](
George Liue99073f2022-12-09 11:06:16 +08001221 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001222 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001223 if (ec || subtreeLocal.empty())
1224 {
1225 return;
1226 }
1227 if (subtreeLocal[0].second.size() != 1)
1228 {
1229 // invalid mapper response, should never happen
Ed Tanous62598e32023-07-17 17:06:25 -07001230 BMCWEB_LOG_ERROR("GetPIDValues: Mapper Error");
Ed Tanous002d39b2022-05-31 08:59:27 -07001231 messages::internalError(self->asyncResp->res);
1232 return;
1233 }
1234
1235 const std::string& path = subtreeLocal[0].first;
1236 const std::string& owner = subtreeLocal[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001237
1238 sdbusplus::asio::getAllProperties(
1239 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous002d39b2022-05-31 08:59:27 -07001240 [path, owner,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001241 self](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001242 const dbus::utility::DBusPropertiesMap& resp) {
1243 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001244 {
Ed Tanous62598e32023-07-17 17:06:25 -07001245 BMCWEB_LOG_ERROR(
1246 "GetPIDValues: Can't get thermalModeIface {}", path);
James Feist73df0db2019-03-25 15:29:35 -07001247 messages::internalError(self->asyncResp->res);
1248 return;
1249 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001250
Ed Tanous002d39b2022-05-31 08:59:27 -07001251 const std::string* current = nullptr;
1252 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001253
1254 const bool success = sdbusplus::unpackPropertiesNoThrow(
1255 dbus_utils::UnpackErrorPrinter(), resp, "Current", current,
1256 "Supported", supported);
1257
1258 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001259 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001260 messages::internalError(self->asyncResp->res);
1261 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001262 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001263
Ed Tanous002d39b2022-05-31 08:59:27 -07001264 if (current == nullptr || supported == nullptr)
1265 {
Ed Tanous62598e32023-07-17 17:06:25 -07001266 BMCWEB_LOG_ERROR(
1267 "GetPIDValues: thermal mode iface invalid {}", path);
Ed Tanous002d39b2022-05-31 08:59:27 -07001268 messages::internalError(self->asyncResp->res);
1269 return;
1270 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001271 self->complete.currentProfile = *current;
1272 self->complete.supportedProfiles = *supported;
George Liue99073f2022-12-09 11:06:16 +08001273 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001274 });
James Feist73df0db2019-03-25 15:29:35 -07001275 }
1276
Ed Tanous6936afe2022-09-08 15:10:39 -07001277 static void
1278 processingComplete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1279 const CompletionValues& completion)
James Feist73df0db2019-03-25 15:29:35 -07001280 {
1281 if (asyncResp->res.result() != boost::beast::http::status::ok)
1282 {
1283 return;
1284 }
1285 // create map of <connection, path to objMgr>>
Ed Tanous6936afe2022-09-08 15:10:39 -07001286 boost::container::flat_map<
1287 std::string, std::string, std::less<>,
1288 std::vector<std::pair<std::string, std::string>>>
1289 objectMgrPaths;
1290 boost::container::flat_set<std::string, std::less<>,
1291 std::vector<std::string>>
1292 calledConnections;
1293 for (const auto& pathGroup : completion.subtree)
James Feist73df0db2019-03-25 15:29:35 -07001294 {
1295 for (const auto& connectionGroup : pathGroup.second)
1296 {
1297 auto findConnection =
1298 calledConnections.find(connectionGroup.first);
1299 if (findConnection != calledConnections.end())
1300 {
1301 break;
1302 }
1303 for (const std::string& interface : connectionGroup.second)
1304 {
1305 if (interface == objectManagerIface)
1306 {
1307 objectMgrPaths[connectionGroup.first] = pathGroup.first;
1308 }
1309 // this list is alphabetical, so we
1310 // should have found the objMgr by now
1311 if (interface == pidConfigurationIface ||
1312 interface == pidZoneConfigurationIface ||
1313 interface == stepwiseConfigurationIface)
1314 {
1315 auto findObjMgr =
1316 objectMgrPaths.find(connectionGroup.first);
1317 if (findObjMgr == objectMgrPaths.end())
1318 {
Ed Tanous62598e32023-07-17 17:06:25 -07001319 BMCWEB_LOG_DEBUG("{}Has no Object Manager",
1320 connectionGroup.first);
James Feist73df0db2019-03-25 15:29:35 -07001321 continue;
1322 }
1323
1324 calledConnections.insert(connectionGroup.first);
1325
1326 asyncPopulatePid(findObjMgr->first, findObjMgr->second,
Ed Tanous6936afe2022-09-08 15:10:39 -07001327 completion.currentProfile,
1328 completion.supportedProfiles,
James Feist73df0db2019-03-25 15:29:35 -07001329 asyncResp);
1330 break;
1331 }
1332 }
1333 }
1334 }
1335 }
1336
Ed Tanous6936afe2022-09-08 15:10:39 -07001337 ~GetPIDValues()
1338 {
1339 boost::asio::post(crow::connections::systemBus->get_io_context(),
1340 std::bind_front(&processingComplete, asyncResp,
1341 std::move(complete)));
1342 }
1343
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001344 GetPIDValues(const GetPIDValues&) = delete;
1345 GetPIDValues(GetPIDValues&&) = delete;
1346 GetPIDValues& operator=(const GetPIDValues&) = delete;
1347 GetPIDValues& operator=(GetPIDValues&&) = delete;
1348
zhanghch058d1b46d2021-04-01 11:18:24 +08001349 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Ed Tanous6936afe2022-09-08 15:10:39 -07001350 CompletionValues complete;
James Feist73df0db2019-03-25 15:29:35 -07001351};
1352
1353struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
1354{
Ed Tanous9e9b6042024-03-06 14:18:28 -08001355 SetPIDValues(
1356 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
1357 std::vector<
1358 std::pair<std::string, std::optional<nlohmann::json::object_t>>>&&
1359 configurationsIn,
1360 std::optional<std::string>& profileIn) :
1361 asyncResp(asyncRespIn),
1362 configuration(std::move(configurationsIn)),
1363 profile(std::move(profileIn))
1364 {}
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001365
1366 SetPIDValues(const SetPIDValues&) = delete;
1367 SetPIDValues(SetPIDValues&&) = delete;
1368 SetPIDValues& operator=(const SetPIDValues&) = delete;
1369 SetPIDValues& operator=(SetPIDValues&&) = delete;
1370
James Feist73df0db2019-03-25 15:29:35 -07001371 void run()
1372 {
1373 if (asyncResp->res.result() != boost::beast::http::status::ok)
1374 {
1375 return;
1376 }
1377
1378 std::shared_ptr<SetPIDValues> self = shared_from_this();
1379
1380 // todo(james): might make sense to do a mapper call here if this
1381 // interface gets more traction
George Liu5eb468d2023-06-20 17:03:24 +08001382 sdbusplus::message::object_path objPath(
1383 "/xyz/openbmc_project/inventory");
1384 dbus::utility::getManagedObjects(
1385 "xyz.openbmc_project.EntityManager", objPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001386 [self](const boost::system::error_code& ec,
Ed Tanous914e2d52022-01-07 11:38:34 -08001387 const dbus::utility::ManagedObjectType& mObj) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001388 if (ec)
1389 {
Ed Tanous62598e32023-07-17 17:06:25 -07001390 BMCWEB_LOG_ERROR("Error communicating to Entity Manager");
Ed Tanous002d39b2022-05-31 08:59:27 -07001391 messages::internalError(self->asyncResp->res);
1392 return;
1393 }
1394 const std::array<const char*, 3> configurations = {
1395 pidConfigurationIface, pidZoneConfigurationIface,
1396 stepwiseConfigurationIface};
James Feiste69d9de2020-02-07 12:23:27 -08001397
Ed Tanous002d39b2022-05-31 08:59:27 -07001398 for (const auto& [path, object] : mObj)
1399 {
1400 for (const auto& [interface, _] : object)
James Feiste69d9de2020-02-07 12:23:27 -08001401 {
Ed Tanous3544d2a2023-08-06 18:12:20 -07001402 if (std::ranges::find(configurations, interface) !=
1403 configurations.end())
James Feiste69d9de2020-02-07 12:23:27 -08001404 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001405 self->objectCount++;
1406 break;
James Feiste69d9de2020-02-07 12:23:27 -08001407 }
James Feiste69d9de2020-02-07 12:23:27 -08001408 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001409 }
1410 self->managedObj = mObj;
Patrick Williams5a39f772023-10-20 11:20:21 -05001411 });
James Feist73df0db2019-03-25 15:29:35 -07001412
1413 // at the same time get the profile information
George Liue99073f2022-12-09 11:06:16 +08001414 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1415 thermalModeIface};
1416 dbus::utility::getSubTree(
1417 "/", 0, thermalModeIfaces,
1418 [self](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001419 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001420 if (ec || subtree.empty())
1421 {
1422 return;
1423 }
1424 if (subtree[0].second.empty())
1425 {
1426 // invalid mapper response, should never happen
Ed Tanous62598e32023-07-17 17:06:25 -07001427 BMCWEB_LOG_ERROR("SetPIDValues: Mapper Error");
Ed Tanous002d39b2022-05-31 08:59:27 -07001428 messages::internalError(self->asyncResp->res);
1429 return;
1430 }
1431
1432 const std::string& path = subtree[0].first;
1433 const std::string& owner = subtree[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001434 sdbusplus::asio::getAllProperties(
1435 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001436 [self, path, owner](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001437 const dbus::utility::DBusPropertiesMap& r) {
1438 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001439 {
Ed Tanous62598e32023-07-17 17:06:25 -07001440 BMCWEB_LOG_ERROR(
1441 "SetPIDValues: Can't get thermalModeIface {}", path);
James Feist73df0db2019-03-25 15:29:35 -07001442 messages::internalError(self->asyncResp->res);
1443 return;
1444 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001445 const std::string* current = nullptr;
1446 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001447
1448 const bool success = sdbusplus::unpackPropertiesNoThrow(
1449 dbus_utils::UnpackErrorPrinter(), r, "Current", current,
1450 "Supported", supported);
1451
1452 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001453 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001454 messages::internalError(self->asyncResp->res);
1455 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001456 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001457
Ed Tanous002d39b2022-05-31 08:59:27 -07001458 if (current == nullptr || supported == nullptr)
1459 {
Ed Tanous62598e32023-07-17 17:06:25 -07001460 BMCWEB_LOG_ERROR(
1461 "SetPIDValues: thermal mode iface invalid {}", path);
Ed Tanous002d39b2022-05-31 08:59:27 -07001462 messages::internalError(self->asyncResp->res);
1463 return;
1464 }
1465 self->currentProfile = *current;
1466 self->supportedProfiles = *supported;
1467 self->profileConnection = owner;
1468 self->profilePath = path;
George Liue99073f2022-12-09 11:06:16 +08001469 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001470 });
James Feist73df0db2019-03-25 15:29:35 -07001471 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001472 void pidSetDone()
James Feist73df0db2019-03-25 15:29:35 -07001473 {
1474 if (asyncResp->res.result() != boost::beast::http::status::ok)
1475 {
1476 return;
1477 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001478 std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001479 if (profile)
1480 {
Ed Tanous3544d2a2023-08-06 18:12:20 -07001481 if (std::ranges::find(supportedProfiles, *profile) ==
1482 supportedProfiles.end())
James Feist73df0db2019-03-25 15:29:35 -07001483 {
1484 messages::actionParameterUnknown(response->res, "Profile",
1485 *profile);
1486 return;
1487 }
1488 currentProfile = *profile;
George Liu9ae226f2023-06-21 17:56:46 +08001489 sdbusplus::asio::setProperty(
1490 *crow::connections::systemBus, profileConnection, profilePath,
1491 thermalModeIface, "Current", *profile,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001492 [response](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001493 if (ec)
1494 {
Ed Tanous62598e32023-07-17 17:06:25 -07001495 BMCWEB_LOG_ERROR("Error patching profile{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001496 messages::internalError(response->res);
1497 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001498 });
James Feist73df0db2019-03-25 15:29:35 -07001499 }
1500
1501 for (auto& containerPair : configuration)
1502 {
1503 auto& container = containerPair.second;
1504 if (!container)
1505 {
1506 continue;
1507 }
James Feist6ee7f772020-02-06 16:25:27 -08001508
Ed Tanous02cad962022-06-30 16:50:15 -07001509 const std::string& type = containerPair.first;
James Feist73df0db2019-03-25 15:29:35 -07001510
Ed Tanous9e9b6042024-03-06 14:18:28 -08001511 for (auto& [name, value] : *container)
James Feist73df0db2019-03-25 15:29:35 -07001512 {
Potin Laicddbf3d2023-02-14 14:28:58 +08001513 std::string dbusObjName = name;
1514 std::replace(dbusObjName.begin(), dbusObjName.end(), ' ', '_');
Ed Tanous62598e32023-07-17 17:06:25 -07001515 BMCWEB_LOG_DEBUG("looking for {}", name);
James Feist6ee7f772020-02-06 16:25:27 -08001516
Ed Tanous3544d2a2023-08-06 18:12:20 -07001517 auto pathItr = std::ranges::find_if(
1518 managedObj, [&dbusObjName](const auto& obj) {
Ed Tanous91f75ca2024-06-10 13:56:43 -07001519 return obj.first.filename() == dbusObjName;
Patrick Williams5a39f772023-10-20 11:20:21 -05001520 });
Ed Tanousb9d36b42022-02-26 21:42:46 -08001521 dbus::utility::DBusPropertiesMap output;
James Feist73df0db2019-03-25 15:29:35 -07001522
1523 output.reserve(16); // The pid interface length
1524
1525 // determines if we're patching entity-manager or
1526 // creating a new object
1527 bool createNewObject = (pathItr == managedObj.end());
Ed Tanous62598e32023-07-17 17:06:25 -07001528 BMCWEB_LOG_DEBUG("Found = {}", !createNewObject);
James Feist6ee7f772020-02-06 16:25:27 -08001529
James Feist73df0db2019-03-25 15:29:35 -07001530 std::string iface;
Ed Tanousea2b6702022-03-07 16:48:38 -08001531 if (!createNewObject)
James Feist73df0db2019-03-25 15:29:35 -07001532 {
Potin Lai8be2b5b2022-11-22 13:27:16 +08001533 bool findInterface = false;
Ed Tanousea2b6702022-03-07 16:48:38 -08001534 for (const auto& interface : pathItr->second)
James Feist73df0db2019-03-25 15:29:35 -07001535 {
Ed Tanousea2b6702022-03-07 16:48:38 -08001536 if (interface.first == pidConfigurationIface)
1537 {
1538 if (type == "PidControllers" ||
1539 type == "FanControllers")
1540 {
1541 iface = pidConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001542 findInterface = true;
1543 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001544 }
1545 }
1546 else if (interface.first == pidZoneConfigurationIface)
1547 {
1548 if (type == "FanZones")
1549 {
PavanKumarIntelda393502024-03-15 05:47:02 +00001550 iface = pidZoneConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001551 findInterface = true;
1552 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001553 }
1554 }
1555 else if (interface.first == stepwiseConfigurationIface)
1556 {
1557 if (type == "StepwiseControllers")
1558 {
1559 iface = stepwiseConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001560 findInterface = true;
1561 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001562 }
1563 }
James Feist73df0db2019-03-25 15:29:35 -07001564 }
Potin Lai8be2b5b2022-11-22 13:27:16 +08001565
1566 // create new object if interface not found
1567 if (!findInterface)
1568 {
1569 createNewObject = true;
1570 }
James Feist73df0db2019-03-25 15:29:35 -07001571 }
James Feist6ee7f772020-02-06 16:25:27 -08001572
Ed Tanous9e9b6042024-03-06 14:18:28 -08001573 if (createNewObject && value == nullptr)
James Feist6ee7f772020-02-06 16:25:27 -08001574 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -05001575 // can't delete a non-existent object
Ed Tanous9e9b6042024-03-06 14:18:28 -08001576 messages::propertyValueNotInList(response->res, value,
Ed Tanouse2616cc2022-06-27 12:45:55 -07001577 name);
James Feist6ee7f772020-02-06 16:25:27 -08001578 continue;
1579 }
1580
1581 std::string path;
1582 if (pathItr != managedObj.end())
1583 {
1584 path = pathItr->first.str;
1585 }
1586
Ed Tanous62598e32023-07-17 17:06:25 -07001587 BMCWEB_LOG_DEBUG("Create new = {}", createNewObject);
James Feiste69d9de2020-02-07 12:23:27 -08001588
1589 // arbitrary limit to avoid attacks
1590 constexpr const size_t controllerLimit = 500;
James Feist14b0b8d2020-02-12 11:52:07 -08001591 if (createNewObject && objectCount >= controllerLimit)
James Feiste69d9de2020-02-07 12:23:27 -08001592 {
1593 messages::resourceExhaustion(response->res, type);
1594 continue;
1595 }
Ed Tanousa170f272022-06-30 21:53:27 -07001596 std::string escaped = name;
1597 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1598 output.emplace_back("Name", escaped);
James Feist73df0db2019-03-25 15:29:35 -07001599
1600 std::string chassis;
1601 CreatePIDRet ret = createPidInterface(
Ed Tanous9e9b6042024-03-06 14:18:28 -08001602 response, type, name, value, path, managedObj,
1603 createNewObject, output, chassis, currentProfile);
James Feist73df0db2019-03-25 15:29:35 -07001604 if (ret == CreatePIDRet::fail)
1605 {
1606 return;
1607 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001608 if (ret == CreatePIDRet::del)
James Feist73df0db2019-03-25 15:29:35 -07001609 {
1610 continue;
1611 }
1612
1613 if (!createNewObject)
1614 {
1615 for (const auto& property : output)
1616 {
Potin Lai7a696972023-11-09 12:18:20 +08001617 crow::connections::systemBus->async_method_call(
James Feist73df0db2019-03-25 15:29:35 -07001618 [response,
1619 propertyName{std::string(property.first)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001620 const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001621 if (ec)
1622 {
Ed Tanous62598e32023-07-17 17:06:25 -07001623 BMCWEB_LOG_ERROR("Error patching {}: {}",
1624 propertyName, ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001625 messages::internalError(response->res);
1626 return;
1627 }
1628 messages::success(response->res);
Potin Lai7a696972023-11-09 12:18:20 +08001629 },
1630 "xyz.openbmc_project.EntityManager", path,
1631 "org.freedesktop.DBus.Properties", "Set", iface,
1632 property.first, property.second);
James Feist73df0db2019-03-25 15:29:35 -07001633 }
1634 }
1635 else
1636 {
1637 if (chassis.empty())
1638 {
Ed Tanous62598e32023-07-17 17:06:25 -07001639 BMCWEB_LOG_ERROR("Failed to get chassis from config");
Ed Tanousace85d62021-10-26 12:45:59 -07001640 messages::internalError(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001641 return;
1642 }
1643
1644 bool foundChassis = false;
1645 for (const auto& obj : managedObj)
1646 {
Ed Tanous91f75ca2024-06-10 13:56:43 -07001647 if (obj.first.filename() == chassis)
James Feist73df0db2019-03-25 15:29:35 -07001648 {
1649 chassis = obj.first.str;
1650 foundChassis = true;
1651 break;
1652 }
1653 }
1654 if (!foundChassis)
1655 {
Ed Tanous62598e32023-07-17 17:06:25 -07001656 BMCWEB_LOG_ERROR("Failed to find chassis on dbus");
James Feist73df0db2019-03-25 15:29:35 -07001657 messages::resourceMissingAtURI(
Ed Tanousace85d62021-10-26 12:45:59 -07001658 response->res,
Ed Tanousef4c65b2023-04-24 15:28:50 -07001659 boost::urls::format("/redfish/v1/Chassis/{}",
1660 chassis));
James Feist73df0db2019-03-25 15:29:35 -07001661 return;
1662 }
1663
1664 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001665 [response](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001666 if (ec)
1667 {
Ed Tanous62598e32023-07-17 17:06:25 -07001668 BMCWEB_LOG_ERROR("Error Adding Pid Object {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001669 messages::internalError(response->res);
1670 return;
1671 }
1672 messages::success(response->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05001673 },
James Feist73df0db2019-03-25 15:29:35 -07001674 "xyz.openbmc_project.EntityManager", chassis,
1675 "xyz.openbmc_project.AddObject", "AddObject", output);
1676 }
1677 }
1678 }
1679 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001680
1681 ~SetPIDValues()
1682 {
1683 try
1684 {
1685 pidSetDone();
1686 }
1687 catch (...)
1688 {
Ed Tanous62598e32023-07-17 17:06:25 -07001689 BMCWEB_LOG_CRITICAL("pidSetDone threw exception");
Ed Tanous24b2fe82022-01-06 12:45:54 -08001690 }
1691 }
1692
zhanghch058d1b46d2021-04-01 11:18:24 +08001693 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Ed Tanous9e9b6042024-03-06 14:18:28 -08001694 std::vector<std::pair<std::string, std::optional<nlohmann::json::object_t>>>
James Feist73df0db2019-03-25 15:29:35 -07001695 configuration;
1696 std::optional<std::string> profile;
1697 dbus::utility::ManagedObjectType managedObj;
1698 std::vector<std::string> supportedProfiles;
1699 std::string currentProfile;
1700 std::string profileConnection;
1701 std::string profilePath;
James Feist14b0b8d2020-02-12 11:52:07 -08001702 size_t objectCount = 0;
James Feist73df0db2019-03-25 15:29:35 -07001703};
James Feist83ff9ab2018-08-31 10:18:24 -07001704
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001705/**
1706 * @brief Retrieves BMC manager location data over DBus
1707 *
Ed Tanousac106bf2023-06-07 09:24:59 -07001708 * @param[in] asyncResp Shared pointer for completing asynchronous calls
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001709 * @param[in] connectionName - service name
1710 * @param[in] path - object path
1711 * @return none
1712 */
Ed Tanousac106bf2023-06-07 09:24:59 -07001713inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001714 const std::string& connectionName,
1715 const std::string& path)
1716{
Ed Tanous62598e32023-07-17 17:06:25 -07001717 BMCWEB_LOG_DEBUG("Get BMC manager Location data.");
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001718
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001719 sdbusplus::asio::getProperty<std::string>(
1720 *crow::connections::systemBus, connectionName, path,
1721 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Ed Tanousac106bf2023-06-07 09:24:59 -07001722 [asyncResp](const boost::system::error_code& ec,
1723 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001724 if (ec)
1725 {
Ed Tanous62598e32023-07-17 17:06:25 -07001726 BMCWEB_LOG_DEBUG("DBUS response error for "
1727 "Location");
Ed Tanousac106bf2023-06-07 09:24:59 -07001728 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001729 return;
1730 }
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001731
Ed Tanousac106bf2023-06-07 09:24:59 -07001732 asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001733 property;
Patrick Williams5a39f772023-10-20 11:20:21 -05001734 });
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001735}
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001736// avoid name collision systems.hpp
1737inline void
Ed Tanousac106bf2023-06-07 09:24:59 -07001738 managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001739{
Ed Tanous62598e32023-07-17 17:06:25 -07001740 BMCWEB_LOG_DEBUG("Getting Manager Last Reset Time");
Ed Tanous52cc1122020-07-18 13:51:21 -07001741
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001742 sdbusplus::asio::getProperty<uint64_t>(
1743 *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
1744 "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
1745 "LastRebootTime",
Ed Tanousac106bf2023-06-07 09:24:59 -07001746 [asyncResp](const boost::system::error_code& ec,
1747 const uint64_t lastResetTime) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001748 if (ec)
1749 {
Ed Tanous62598e32023-07-17 17:06:25 -07001750 BMCWEB_LOG_DEBUG("D-BUS response error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001751 return;
1752 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001753
Ed Tanous002d39b2022-05-31 08:59:27 -07001754 // LastRebootTime is epoch time, in milliseconds
1755 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
1756 uint64_t lastResetTimeStamp = lastResetTime / 1000;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001757
Ed Tanous002d39b2022-05-31 08:59:27 -07001758 // Convert to ISO 8601 standard
Ed Tanousac106bf2023-06-07 09:24:59 -07001759 asyncResp->res.jsonValue["LastResetTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001760 redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
Patrick Williams5a39f772023-10-20 11:20:21 -05001761 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001762}
1763
1764/**
1765 * @brief Set the running firmware image
1766 *
Ed Tanousac106bf2023-06-07 09:24:59 -07001767 * @param[i,o] asyncResp - Async response object
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001768 * @param[i] runningFirmwareTarget - Image to make the running image
1769 *
1770 * @return void
1771 */
1772inline void
Ed Tanousac106bf2023-06-07 09:24:59 -07001773 setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001774 const std::string& runningFirmwareTarget)
1775{
1776 // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1777 std::string::size_type idPos = runningFirmwareTarget.rfind('/');
1778 if (idPos == std::string::npos)
1779 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001780 messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001781 "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001782 BMCWEB_LOG_DEBUG("Can't parse firmware ID!");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001783 return;
1784 }
1785 idPos++;
1786 if (idPos >= runningFirmwareTarget.size())
1787 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001788 messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001789 "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001790 BMCWEB_LOG_DEBUG("Invalid firmware ID.");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001791 return;
1792 }
1793 std::string firmwareId = runningFirmwareTarget.substr(idPos);
1794
1795 // Make sure the image is valid before setting priority
George Liu5eb468d2023-06-20 17:03:24 +08001796 sdbusplus::message::object_path objPath("/xyz/openbmc_project/software");
1797 dbus::utility::getManagedObjects(
1798 "xyz.openbmc_project.Software.BMC.Updater", objPath,
1799 [asyncResp, firmwareId, runningFirmwareTarget](
1800 const boost::system::error_code& ec,
1801 const dbus::utility::ManagedObjectType& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001802 if (ec)
1803 {
Ed Tanous62598e32023-07-17 17:06:25 -07001804 BMCWEB_LOG_DEBUG("D-Bus response error getting objects.");
Ed Tanousac106bf2023-06-07 09:24:59 -07001805 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001806 return;
1807 }
1808
1809 if (subtree.empty())
1810 {
Ed Tanous62598e32023-07-17 17:06:25 -07001811 BMCWEB_LOG_DEBUG("Can't find image!");
Ed Tanousac106bf2023-06-07 09:24:59 -07001812 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001813 return;
1814 }
1815
1816 bool foundImage = false;
Ed Tanous02cad962022-06-30 16:50:15 -07001817 for (const auto& object : subtree)
Ed Tanous002d39b2022-05-31 08:59:27 -07001818 {
1819 const std::string& path =
1820 static_cast<const std::string&>(object.first);
1821 std::size_t idPos2 = path.rfind('/');
1822
1823 if (idPos2 == std::string::npos)
1824 {
1825 continue;
1826 }
1827
1828 idPos2++;
1829 if (idPos2 >= path.size())
1830 {
1831 continue;
1832 }
1833
1834 if (path.substr(idPos2) == firmwareId)
1835 {
1836 foundImage = true;
1837 break;
1838 }
1839 }
1840
1841 if (!foundImage)
1842 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001843 messages::propertyValueNotInList(
1844 asyncResp->res, runningFirmwareTarget, "@odata.id");
Ed Tanous62598e32023-07-17 17:06:25 -07001845 BMCWEB_LOG_DEBUG("Invalid firmware ID.");
Ed Tanous002d39b2022-05-31 08:59:27 -07001846 return;
1847 }
1848
Ed Tanous62598e32023-07-17 17:06:25 -07001849 BMCWEB_LOG_DEBUG("Setting firmware version {} to priority 0.",
1850 firmwareId);
Ed Tanous002d39b2022-05-31 08:59:27 -07001851
1852 // Only support Immediate
1853 // An addition could be a Redfish Setting like
1854 // ActiveSoftwareImageApplyTime and support OnReset
George Liu9ae226f2023-06-21 17:56:46 +08001855 sdbusplus::asio::setProperty(
1856 *crow::connections::systemBus,
1857 "xyz.openbmc_project.Software.BMC.Updater",
1858 "/xyz/openbmc_project/software/" + firmwareId,
1859 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1860 static_cast<uint8_t>(0),
Ed Tanousac106bf2023-06-07 09:24:59 -07001861 [asyncResp](const boost::system::error_code& ec2) {
Ed Tanous8a592812022-06-04 09:06:59 -07001862 if (ec2)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001863 {
Ed Tanous62598e32023-07-17 17:06:25 -07001864 BMCWEB_LOG_DEBUG("D-Bus response error setting.");
Ed Tanousac106bf2023-06-07 09:24:59 -07001865 messages::internalError(asyncResp->res);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001866 return;
1867 }
Ed Tanousac106bf2023-06-07 09:24:59 -07001868 doBMCGracefulRestart(asyncResp);
George Liu5eb468d2023-06-20 17:03:24 +08001869 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001870 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001871}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001872
Ed Tanousc51afd52024-03-07 10:13:14 -08001873inline void
1874 afterSetDateTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1875 const boost::system::error_code& ec,
1876 const sdbusplus::message_t& msg)
1877{
1878 if (ec)
1879 {
1880 BMCWEB_LOG_DEBUG("Failed to set elapsed time. DBUS response error {}",
1881 ec);
1882 const sd_bus_error* dbusError = msg.get_error();
1883 if (dbusError != nullptr)
1884 {
1885 std::string_view errorName(dbusError->name);
1886 if (errorName ==
1887 "org.freedesktop.timedate1.AutomaticTimeSyncEnabled")
1888 {
1889 BMCWEB_LOG_DEBUG("Setting conflict");
1890 messages::propertyValueConflict(
1891 asyncResp->res, "DateTime",
1892 "Managers/NetworkProtocol/NTPProcotolEnabled");
1893 return;
1894 }
1895 }
1896 messages::internalError(asyncResp->res);
1897 return;
1898 }
1899 asyncResp->res.result(boost::beast::http::status::no_content);
1900}
1901
1902inline void setDateTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1903 const std::string& datetime)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001904{
Ed Tanous62598e32023-07-17 17:06:25 -07001905 BMCWEB_LOG_DEBUG("Set date time: {}", datetime);
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001906
Ed Tanousc2e32002023-01-07 22:05:08 -08001907 std::optional<redfish::time_utils::usSinceEpoch> us =
1908 redfish::time_utils::dateStringToEpoch(datetime);
1909 if (!us)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001910 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001911 messages::propertyValueFormatError(asyncResp->res, datetime,
1912 "DateTime");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001913 return;
1914 }
Ed Tanousc51afd52024-03-07 10:13:14 -08001915 // Set the absolute datetime
1916 bool relative = false;
1917 bool interactive = false;
1918 crow::connections::systemBus->async_method_call(
1919 [asyncResp](const boost::system::error_code& ec,
1920 const sdbusplus::message_t& msg) {
1921 afterSetDateTime(asyncResp, ec, msg);
1922 },
1923 "org.freedesktop.timedate1", "/org/freedesktop/timedate1",
1924 "org.freedesktop.timedate1", "SetTime", us->count(), relative,
1925 interactive);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001926}
1927
Ed Tanous75815e52022-10-05 17:21:13 -07001928inline void
1929 checkForQuiesced(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1930{
1931 sdbusplus::asio::getProperty<std::string>(
1932 *crow::connections::systemBus, "org.freedesktop.systemd1",
1933 "/org/freedesktop/systemd1/unit/obmc-bmc-service-quiesce@0.target",
1934 "org.freedesktop.systemd1.Unit", "ActiveState",
1935 [asyncResp](const boost::system::error_code& ec,
1936 const std::string& val) {
1937 if (!ec)
1938 {
1939 if (val == "active")
1940 {
1941 asyncResp->res.jsonValue["Status"]["Health"] = "Critical";
1942 asyncResp->res.jsonValue["Status"]["State"] = "Quiesced";
1943 return;
1944 }
1945 }
1946 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
1947 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
Patrick Williams5a39f772023-10-20 11:20:21 -05001948 });
Ed Tanous75815e52022-10-05 17:21:13 -07001949}
1950
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001951inline void requestRoutesManager(App& app)
1952{
1953 std::string uuid = persistent_data::getConfig().systemUuid;
1954
Ed Tanous253f11b2024-05-16 09:38:31 -07001955 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001956 .privileges(redfish::privileges::getManager)
Ed Tanous002d39b2022-05-31 08:59:27 -07001957 .methods(boost::beast::http::verb::get)(
1958 [&app, uuid](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -07001959 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1960 const std::string& managerId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001961 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001962 {
1963 return;
1964 }
Ed Tanous253f11b2024-05-16 09:38:31 -07001965
1966 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
1967 {
1968 messages::resourceNotFound(asyncResp->res, "Manager", managerId);
1969 return;
1970 }
1971
1972 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1973 "/redfish/v1/Managers/{}", BMCWEB_REDFISH_MANAGER_URI_NAME);
Sui Chena51fc2d2022-07-14 17:21:53 -07001974 asyncResp->res.jsonValue["@odata.type"] = "#Manager.v1_14_0.Manager";
Ed Tanous253f11b2024-05-16 09:38:31 -07001975 asyncResp->res.jsonValue["Id"] = BMCWEB_REDFISH_MANAGER_URI_NAME;
Ed Tanous002d39b2022-05-31 08:59:27 -07001976 asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
1977 asyncResp->res.jsonValue["Description"] =
1978 "Baseboard Management Controller";
1979 asyncResp->res.jsonValue["PowerState"] = "On";
Ed Tanous14766872022-03-15 10:44:42 -07001980
Ed Tanous002d39b2022-05-31 08:59:27 -07001981 asyncResp->res.jsonValue["ManagerType"] = "BMC";
1982 asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
1983 asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1984 asyncResp->res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001985
Ed Tanous002d39b2022-05-31 08:59:27 -07001986 asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07001987 boost::urls::format("/redfish/v1/Managers/{}/LogServices",
1988 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -07001989 asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07001990 boost::urls::format("/redfish/v1/Managers/{}/NetworkProtocol",
1991 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -07001992 asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07001993 boost::urls::format("/redfish/v1/Managers/{}/EthernetInterfaces",
1994 BMCWEB_REDFISH_MANAGER_URI_NAME);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001995
Ed Tanous25b54db2024-04-17 15:40:31 -07001996 if constexpr (BMCWEB_VM_NBDPROXY)
Ed Tanous36c0f2a2024-02-09 13:50:26 -08001997 {
1998 asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07001999 boost::urls::format("/redfish/v1/Managers/{}/VirtualMedia",
2000 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous36c0f2a2024-02-09 13:50:26 -08002001 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002002
Ed Tanous002d39b2022-05-31 08:59:27 -07002003 // default oem data
2004 nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
2005 nlohmann::json& oemOpenbmc = oem["OpenBmc"];
Ed Tanous253f11b2024-05-16 09:38:31 -07002006 oem["@odata.id"] = boost::urls::format("/redfish/v1/Managers/{}#/Oem",
2007 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanousfc1cdd12024-07-11 11:58:16 -07002008 oemOpenbmc["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager";
Ed Tanous253f11b2024-05-16 09:38:31 -07002009 oemOpenbmc["@odata.id"] =
2010 boost::urls::format("/redfish/v1/Managers/{}#/Oem/OpenBmc",
2011 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous14766872022-03-15 10:44:42 -07002012
Ed Tanous002d39b2022-05-31 08:59:27 -07002013 nlohmann::json::object_t certificates;
Ed Tanous253f11b2024-05-16 09:38:31 -07002014 certificates["@odata.id"] = boost::urls::format(
2015 "/redfish/v1/Managers/{}/Truststore/Certificates",
2016 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -07002017 oemOpenbmc["Certificates"] = std::move(certificates);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002018
Ed Tanous002d39b2022-05-31 08:59:27 -07002019 // Manager.Reset (an action) can be many values, OpenBMC only
2020 // supports BMC reboot.
2021 nlohmann::json& managerReset =
2022 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
2023 managerReset["target"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07002024 boost::urls::format("/redfish/v1/Managers/{}/Actions/Manager.Reset",
2025 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -07002026 managerReset["@Redfish.ActionInfo"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07002027 boost::urls::format("/redfish/v1/Managers/{}/ResetActionInfo",
2028 BMCWEB_REDFISH_MANAGER_URI_NAME);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002029
Ed Tanous002d39b2022-05-31 08:59:27 -07002030 // ResetToDefaults (Factory Reset) has values like
2031 // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
2032 // on OpenBMC
2033 nlohmann::json& resetToDefaults =
2034 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
Ed Tanous253f11b2024-05-16 09:38:31 -07002035 resetToDefaults["target"] = boost::urls::format(
2036 "/redfish/v1/Managers/{}/Actions/Manager.ResetToDefaults",
2037 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous613dabe2022-07-09 11:17:36 -07002038 resetToDefaults["ResetType@Redfish.AllowableValues"] =
2039 nlohmann::json::array_t({"ResetAll"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002040
Ed Tanous002d39b2022-05-31 08:59:27 -07002041 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002042 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05302043
Ed Tanous002d39b2022-05-31 08:59:27 -07002044 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2045 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2046 redfishDateTimeOffset.second;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002047
Ed Tanous002d39b2022-05-31 08:59:27 -07002048 // TODO (Gunnar): Remove these one day since moved to ComputerSystem
2049 // Still used by OCP profiles
2050 // https://github.com/opencomputeproject/OCP-Profiles/issues/23
2051 // Fill in SerialConsole info
2052 asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
2053 asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
Ed Tanous613dabe2022-07-09 11:17:36 -07002054 asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
2055 nlohmann::json::array_t({"IPMI", "SSH"});
Ed Tanous25b54db2024-04-17 15:40:31 -07002056 if constexpr (BMCWEB_KVM)
2057 {
2058 // Fill in GraphicalConsole info
2059 asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] =
2060 true;
2061 asyncResp->res
2062 .jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4;
2063 asyncResp->res
2064 .jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
2065 nlohmann::json::array_t({"KVMIP"});
2066 }
2067 if constexpr (!BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002068 {
2069 asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] =
2070 1;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002071
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002072 nlohmann::json::array_t managerForServers;
2073 nlohmann::json::object_t manager;
Ed Tanous253f11b2024-05-16 09:38:31 -07002074 manager["@odata.id"] = std::format("/redfish/v1/Systems/{}",
2075 BMCWEB_REDFISH_SYSTEM_URI_NAME);
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002076 managerForServers.emplace_back(std::move(manager));
Ed Tanous14766872022-03-15 10:44:42 -07002077
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002078 asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
2079 std::move(managerForServers);
2080 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002081
Willy Tueee00132022-06-14 14:53:17 -07002082 sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
Ed Tanous002d39b2022-05-31 08:59:27 -07002083 "FirmwareVersion", true);
2084
2085 managerGetLastResetTime(asyncResp);
2086
Sui Chena51fc2d2022-07-14 17:21:53 -07002087 // ManagerDiagnosticData is added for all BMCs.
2088 nlohmann::json& managerDiagnosticData =
2089 asyncResp->res.jsonValue["ManagerDiagnosticData"];
2090 managerDiagnosticData["@odata.id"] =
Ed Tanous253f11b2024-05-16 09:38:31 -07002091 boost::urls::format("/redfish/v1/Managers/{}/ManagerDiagnosticData",
2092 BMCWEB_REDFISH_MANAGER_URI_NAME);
Sui Chena51fc2d2022-07-14 17:21:53 -07002093
Ed Tanous25b54db2024-04-17 15:40:31 -07002094 if constexpr (BMCWEB_REDFISH_OEM_MANAGER_FAN_DATA)
2095 {
2096 auto pids = std::make_shared<GetPIDValues>(asyncResp);
2097 pids->run();
2098 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002099
2100 getMainChassisId(asyncResp,
2101 [](const std::string& chassisId,
2102 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2103 aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
2104 nlohmann::json::array_t managerForChassis;
Ed Tanous8a592812022-06-04 09:06:59 -07002105 nlohmann::json::object_t managerObj;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002106 boost::urls::url chassiUrl =
2107 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
Willy Tueddfc432022-09-26 16:46:38 +00002108 managerObj["@odata.id"] = chassiUrl;
Patrick Williamsad539542023-05-12 10:10:08 -05002109 managerForChassis.emplace_back(std::move(managerObj));
Ed Tanous002d39b2022-05-31 08:59:27 -07002110 aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
2111 std::move(managerForChassis);
2112 aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00002113 chassiUrl;
Ed Tanous002d39b2022-05-31 08:59:27 -07002114 });
Ed Tanous14766872022-03-15 10:44:42 -07002115
Ed Tanous75815e52022-10-05 17:21:13 -07002116 sdbusplus::asio::getProperty<double>(
2117 *crow::connections::systemBus, "org.freedesktop.systemd1",
2118 "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
2119 "Progress",
2120 [asyncResp](const boost::system::error_code& ec, double val) {
2121 if (ec)
2122 {
Ed Tanous62598e32023-07-17 17:06:25 -07002123 BMCWEB_LOG_ERROR("Error while getting progress");
Ed Tanous75815e52022-10-05 17:21:13 -07002124 messages::internalError(asyncResp->res);
2125 return;
2126 }
2127 if (val < 1.0)
2128 {
2129 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
2130 asyncResp->res.jsonValue["Status"]["State"] = "Starting";
2131 return;
2132 }
2133 checkForQuiesced(asyncResp);
Patrick Williams5a39f772023-10-20 11:20:21 -05002134 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002135
George Liue99073f2022-12-09 11:06:16 +08002136 constexpr std::array<std::string_view, 1> interfaces = {
2137 "xyz.openbmc_project.Inventory.Item.Bmc"};
2138 dbus::utility::getSubTree(
2139 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07002140 [asyncResp](
George Liue99073f2022-12-09 11:06:16 +08002141 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07002142 const dbus::utility::MapperGetSubTreeResponse& subtree) {
2143 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002144 {
Ed Tanous62598e32023-07-17 17:06:25 -07002145 BMCWEB_LOG_DEBUG("D-Bus response error on GetSubTree {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002146 return;
2147 }
2148 if (subtree.empty())
2149 {
Ed Tanous62598e32023-07-17 17:06:25 -07002150 BMCWEB_LOG_DEBUG("Can't find bmc D-Bus object!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002151 return;
2152 }
2153 // Assume only 1 bmc D-Bus object
2154 // Throw an error if there is more than 1
2155 if (subtree.size() > 1)
2156 {
Ed Tanous62598e32023-07-17 17:06:25 -07002157 BMCWEB_LOG_DEBUG("Found more than 1 bmc D-Bus object!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002158 messages::internalError(asyncResp->res);
2159 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002160 }
2161
Ed Tanous002d39b2022-05-31 08:59:27 -07002162 if (subtree[0].first.empty() || subtree[0].second.size() != 1)
2163 {
Ed Tanous62598e32023-07-17 17:06:25 -07002164 BMCWEB_LOG_DEBUG("Error getting bmc D-Bus object!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002165 messages::internalError(asyncResp->res);
2166 return;
2167 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002168
Ed Tanous002d39b2022-05-31 08:59:27 -07002169 const std::string& path = subtree[0].first;
2170 const std::string& connectionName = subtree[0].second[0].first;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002171
Ed Tanous002d39b2022-05-31 08:59:27 -07002172 for (const auto& interfaceName : subtree[0].second[0].second)
2173 {
2174 if (interfaceName ==
2175 "xyz.openbmc_project.Inventory.Decorator.Asset")
2176 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002177 sdbusplus::asio::getAllProperties(
2178 *crow::connections::systemBus, connectionName, path,
2179 "xyz.openbmc_project.Inventory.Decorator.Asset",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002180 [asyncResp](const boost::system::error_code& ec2,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002181 const dbus::utility::DBusPropertiesMap&
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002182 propertiesList) {
Ed Tanous8a592812022-06-04 09:06:59 -07002183 if (ec2)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002184 {
Ed Tanous62598e32023-07-17 17:06:25 -07002185 BMCWEB_LOG_DEBUG("Can't get bmc asset!");
Ed Tanous002d39b2022-05-31 08:59:27 -07002186 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002187 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002188
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002189 const std::string* partNumber = nullptr;
2190 const std::string* serialNumber = nullptr;
2191 const std::string* manufacturer = nullptr;
2192 const std::string* model = nullptr;
2193 const std::string* sparePartNumber = nullptr;
2194
2195 const bool success = sdbusplus::unpackPropertiesNoThrow(
2196 dbus_utils::UnpackErrorPrinter(), propertiesList,
2197 "PartNumber", partNumber, "SerialNumber",
2198 serialNumber, "Manufacturer", manufacturer, "Model",
2199 model, "SparePartNumber", sparePartNumber);
2200
2201 if (!success)
2202 {
2203 messages::internalError(asyncResp->res);
2204 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07002205 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002206
2207 if (partNumber != nullptr)
2208 {
2209 asyncResp->res.jsonValue["PartNumber"] =
2210 *partNumber;
2211 }
2212
2213 if (serialNumber != nullptr)
2214 {
2215 asyncResp->res.jsonValue["SerialNumber"] =
2216 *serialNumber;
2217 }
2218
2219 if (manufacturer != nullptr)
2220 {
2221 asyncResp->res.jsonValue["Manufacturer"] =
2222 *manufacturer;
2223 }
2224
2225 if (model != nullptr)
2226 {
2227 asyncResp->res.jsonValue["Model"] = *model;
2228 }
2229
2230 if (sparePartNumber != nullptr)
2231 {
2232 asyncResp->res.jsonValue["SparePartNumber"] =
2233 *sparePartNumber;
2234 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002235 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002236 }
2237 else if (interfaceName ==
2238 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
2239 {
2240 getLocation(asyncResp, connectionName, path);
2241 }
2242 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002243 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002244 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002245
Ed Tanous253f11b2024-05-16 09:38:31 -07002246 BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002247 .privileges(redfish::privileges::patchManager)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002248 .methods(boost::beast::http::verb::patch)(
2249 [&app](const crow::Request& req,
Ed Tanous253f11b2024-05-16 09:38:31 -07002250 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2251 const std::string& managerId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002252 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002253 {
2254 return;
2255 }
Ed Tanous253f11b2024-05-16 09:38:31 -07002256
2257 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2258 {
2259 messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2260 return;
2261 }
2262
Ed Tanous9e9b6042024-03-06 14:18:28 -08002263 std::optional<std::string> activeSoftwareImageOdataId;
Ed Tanous002d39b2022-05-31 08:59:27 -07002264 std::optional<std::string> datetime;
Ed Tanous9e9b6042024-03-06 14:18:28 -08002265 std::optional<nlohmann::json::object_t> pidControllers;
2266 std::optional<nlohmann::json::object_t> fanControllers;
2267 std::optional<nlohmann::json::object_t> fanZones;
2268 std::optional<nlohmann::json::object_t> stepwiseControllers;
2269 std::optional<std::string> profile;
Ed Tanous002d39b2022-05-31 08:59:27 -07002270
Ed Tanous9e9b6042024-03-06 14:18:28 -08002271 // clang-format off
2272 if (!json_util::readJsonPatch(req, asyncResp->res,
2273 "DateTime", datetime,
2274 "Links/ActiveSoftwareImage/@odata.id", activeSoftwareImageOdataId,
2275 "Oem/OpenBmc/Fan/FanControllers", fanControllers,
2276 "Oem/OpenBmc/Fan/FanZones", fanZones,
2277 "Oem/OpenBmc/Fan/PidControllers", pidControllers,
2278 "Oem/OpenBmc/Fan/Profile", profile,
2279 "Oem/OpenBmc/Fan/StepwiseControllers", stepwiseControllers
2280 ))
Ed Tanous002d39b2022-05-31 08:59:27 -07002281 {
2282 return;
2283 }
Ed Tanous9e9b6042024-03-06 14:18:28 -08002284 // clang-format on
Ed Tanous002d39b2022-05-31 08:59:27 -07002285
Ed Tanous9e9b6042024-03-06 14:18:28 -08002286 if (pidControllers || fanControllers || fanZones ||
2287 stepwiseControllers || profile)
Ed Tanous002d39b2022-05-31 08:59:27 -07002288 {
Ed Tanous25b54db2024-04-17 15:40:31 -07002289 if constexpr (BMCWEB_REDFISH_OEM_MANAGER_FAN_DATA)
Ed Tanous002d39b2022-05-31 08:59:27 -07002290 {
Ed Tanous25b54db2024-04-17 15:40:31 -07002291 std::vector<std::pair<std::string,
2292 std::optional<nlohmann::json::object_t>>>
2293 configuration;
2294 if (pidControllers)
2295 {
2296 configuration.emplace_back("PidControllers",
2297 std::move(pidControllers));
2298 }
2299 if (fanControllers)
2300 {
2301 configuration.emplace_back("FanControllers",
2302 std::move(fanControllers));
2303 }
2304 if (fanZones)
2305 {
2306 configuration.emplace_back("FanZones", std::move(fanZones));
2307 }
2308 if (stepwiseControllers)
2309 {
2310 configuration.emplace_back("StepwiseControllers",
2311 std::move(stepwiseControllers));
2312 }
2313 auto pid = std::make_shared<SetPIDValues>(
2314 asyncResp, std::move(configuration), profile);
2315 pid->run();
Ed Tanous002d39b2022-05-31 08:59:27 -07002316 }
Ed Tanous25b54db2024-04-17 15:40:31 -07002317 else
Ed Tanous002d39b2022-05-31 08:59:27 -07002318 {
Ed Tanous25b54db2024-04-17 15:40:31 -07002319 messages::propertyUnknown(asyncResp->res, "Oem");
2320 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07002321 }
2322 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07002323
Ed Tanous9e9b6042024-03-06 14:18:28 -08002324 if (activeSoftwareImageOdataId)
2325 {
2326 setActiveFirmwareImage(asyncResp, *activeSoftwareImageOdataId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002327 }
Ed Tanous9e9b6042024-03-06 14:18:28 -08002328
Ed Tanous002d39b2022-05-31 08:59:27 -07002329 if (datetime)
2330 {
Ed Tanousc51afd52024-03-07 10:13:14 -08002331 setDateTime(asyncResp, *datetime);
Ed Tanous002d39b2022-05-31 08:59:27 -07002332 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002333 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002334}
2335
2336inline void requestRoutesManagerCollection(App& app)
2337{
2338 BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
Ed Tanoused398212021-06-09 17:05:54 -07002339 .privileges(redfish::privileges::getManagerCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002340 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002341 [&app](const crow::Request& req,
2342 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002343 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002344 {
2345 return;
2346 }
2347 // Collections don't include the static data added by SubRoute
2348 // because it has a duplicate entry for members
2349 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
2350 asyncResp->res.jsonValue["@odata.type"] =
2351 "#ManagerCollection.ManagerCollection";
2352 asyncResp->res.jsonValue["Name"] = "Manager Collection";
2353 asyncResp->res.jsonValue["Members@odata.count"] = 1;
2354 nlohmann::json::array_t members;
2355 nlohmann::json& bmc = members.emplace_back();
Ed Tanous253f11b2024-05-16 09:38:31 -07002356 bmc["@odata.id"] = boost::urls::format("/redfish/v1/Managers/{}",
2357 BMCWEB_REDFISH_MANAGER_URI_NAME);
Ed Tanous002d39b2022-05-31 08:59:27 -07002358 asyncResp->res.jsonValue["Members"] = std::move(members);
Patrick Williams5a39f772023-10-20 11:20:21 -05002359 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002360}
Ed Tanous1abe55e2018-09-05 08:30:59 -07002361} // namespace redfish