blob: c97ba025e8a348a9037f2ea6cf8c5a4ee892c034 [file] [log] [blame]
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Willy Tu13451e32023-05-24 16:08:18 -070018#include "bmcweb_config.h"
19
Sui Chena51fc2d2022-07-14 17:21:53 -070020#include "app.hpp"
21#include "dbus_utility.hpp"
James Feistb49ac872019-05-21 15:12:01 -070022#include "health.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070023#include "query.hpp"
Jennifer Leec5d03ff2019-03-08 15:42:58 -080024#include "redfish_util.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070025#include "registries/privilege_registry.hpp"
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +020026#include "utils/dbus_utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080027#include "utils/json_utils.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070028#include "utils/sw_utils.hpp"
29#include "utils/systemd_utils.hpp"
Ed Tanous2b829372022-08-03 14:22:34 -070030#include "utils/time_utils.hpp"
Borawski.Lukasz9c3106852018-02-09 15:24:22 +010031
George Liue99073f2022-12-09 11:06:16 +080032#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070033#include <boost/url/format.hpp>
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +020034#include <sdbusplus/asio/property.hpp>
35#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050036
Ed Tanousa170f272022-06-30 21:53:27 -070037#include <algorithm>
George Liue99073f2022-12-09 11:06:16 +080038#include <array>
Gunnar Mills4bfefa72020-07-30 13:54:29 -050039#include <cstdint>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050040#include <memory>
41#include <sstream>
George Liue99073f2022-12-09 11:06:16 +080042#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080043#include <variant>
James Feist5b4aa862018-08-16 14:07:01 -070044
Ed Tanous1abe55e2018-09-05 08:30:59 -070045namespace redfish
46{
Jennifer Leeed5befb2018-08-10 11:29:45 -070047
48/**
Gunnar Mills2a5c4402020-05-19 09:07:24 -050049 * Function reboots the BMC.
50 *
51 * @param[in] asyncResp - Shared pointer for completing asynchronous calls
Jennifer Leeed5befb2018-08-10 11:29:45 -070052 */
zhanghch058d1b46d2021-04-01 11:18:24 +080053inline void
54 doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Gunnar Mills2a5c4402020-05-19 09:07:24 -050055{
56 const char* processName = "xyz.openbmc_project.State.BMC";
57 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
58 const char* interfaceName = "xyz.openbmc_project.State.BMC";
59 const std::string& propertyValue =
60 "xyz.openbmc_project.State.BMC.Transition.Reboot";
61 const char* destProperty = "RequestedBMCTransition";
62
63 // Create the D-Bus variant for D-Bus call.
George Liu9ae226f2023-06-21 17:56:46 +080064 sdbusplus::asio::setProperty(
65 *crow::connections::systemBus, processName, objectPath, interfaceName,
66 destProperty, propertyValue,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080067 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -070068 // Use "Set" method to set the property value.
69 if (ec)
70 {
71 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
72 messages::internalError(asyncResp->res);
73 return;
74 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -050075
Ed Tanous002d39b2022-05-31 08:59:27 -070076 messages::success(asyncResp->res);
George Liu9ae226f2023-06-21 17:56:46 +080077 });
Gunnar Mills2a5c4402020-05-19 09:07:24 -050078}
79
zhanghch058d1b46d2021-04-01 11:18:24 +080080inline void
81 doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +000082{
83 const char* processName = "xyz.openbmc_project.State.BMC";
84 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
85 const char* interfaceName = "xyz.openbmc_project.State.BMC";
86 const std::string& propertyValue =
87 "xyz.openbmc_project.State.BMC.Transition.HardReboot";
88 const char* destProperty = "RequestedBMCTransition";
89
90 // Create the D-Bus variant for D-Bus call.
George Liu9ae226f2023-06-21 17:56:46 +080091 sdbusplus::asio::setProperty(
92 *crow::connections::systemBus, processName, objectPath, interfaceName,
93 destProperty, propertyValue,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080094 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -070095 // Use "Set" method to set the property value.
96 if (ec)
97 {
98 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
99 messages::internalError(asyncResp->res);
100 return;
101 }
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000102
Ed Tanous002d39b2022-05-31 08:59:27 -0700103 messages::success(asyncResp->res);
George Liu9ae226f2023-06-21 17:56:46 +0800104 });
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000105}
106
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500107/**
108 * ManagerResetAction class supports the POST method for the Reset (reboot)
109 * action.
110 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700111inline void requestRoutesManagerResetAction(App& app)
Jennifer Leeed5befb2018-08-10 11:29:45 -0700112{
Jennifer Leeed5befb2018-08-10 11:29:45 -0700113 /**
Jennifer Leeed5befb2018-08-10 11:29:45 -0700114 * Function handles POST method request.
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500115 * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000116 * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
Jennifer Leeed5befb2018-08-10 11:29:45 -0700117 */
Jennifer Leeed5befb2018-08-10 11:29:45 -0700118
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700119 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
Ed Tanoused398212021-06-09 17:05:54 -0700120 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700121 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700122 [&app](const crow::Request& req,
123 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000124 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700125 {
126 return;
127 }
128 BMCWEB_LOG_DEBUG << "Post Manager Reset.";
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500129
Ed Tanous002d39b2022-05-31 08:59:27 -0700130 std::string resetType;
Jennifer Leeed5befb2018-08-10 11:29:45 -0700131
Ed Tanous002d39b2022-05-31 08:59:27 -0700132 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
133 resetType))
134 {
135 return;
136 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500137
Ed Tanous002d39b2022-05-31 08:59:27 -0700138 if (resetType == "GracefulRestart")
139 {
140 BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
141 doBMCGracefulRestart(asyncResp);
142 return;
143 }
144 if (resetType == "ForceRestart")
145 {
146 BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
147 doBMCForceRestart(asyncResp);
148 return;
149 }
150 BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
151 << resetType;
152 messages::actionParameterNotSupported(asyncResp->res, resetType,
153 "ResetType");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700154
Ed Tanous002d39b2022-05-31 08:59:27 -0700155 return;
156 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700157}
Jennifer Leeed5befb2018-08-10 11:29:45 -0700158
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500159/**
160 * ManagerResetToDefaultsAction class supports POST method for factory reset
161 * action.
162 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700163inline void requestRoutesManagerResetToDefaultsAction(App& app)
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500164{
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500165 /**
166 * Function handles ResetToDefaults POST method request.
167 *
168 * Analyzes POST body message and factory resets BMC by calling
169 * BMC code updater factory reset followed by a BMC reboot.
170 *
171 * BMC code updater factory reset wipes the whole BMC read-write
172 * filesystem which includes things like the network settings.
173 *
174 * OpenBMC only supports ResetToDefaultsType "ResetAll".
175 */
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500176
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700177 BMCWEB_ROUTE(app,
178 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/")
Ed Tanoused398212021-06-09 17:05:54 -0700179 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700180 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700181 [&app](const crow::Request& req,
182 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000183 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700184 {
185 return;
186 }
187 BMCWEB_LOG_DEBUG << "Post ResetToDefaults.";
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500188
Ed Tanous002d39b2022-05-31 08:59:27 -0700189 std::string resetType;
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500190
Ed Tanous002d39b2022-05-31 08:59:27 -0700191 if (!json_util::readJsonAction(req, asyncResp->res,
192 "ResetToDefaultsType", resetType))
193 {
194 BMCWEB_LOG_DEBUG << "Missing property ResetToDefaultsType.";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700195
Ed Tanous002d39b2022-05-31 08:59:27 -0700196 messages::actionParameterMissing(asyncResp->res, "ResetToDefaults",
197 "ResetToDefaultsType");
198 return;
199 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700200
Ed Tanous002d39b2022-05-31 08:59:27 -0700201 if (resetType != "ResetAll")
202 {
203 BMCWEB_LOG_DEBUG
204 << "Invalid property value for ResetToDefaultsType: "
205 << resetType;
206 messages::actionParameterNotSupported(asyncResp->res, resetType,
207 "ResetToDefaultsType");
208 return;
209 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700210
Ed Tanous002d39b2022-05-31 08:59:27 -0700211 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800212 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700213 if (ec)
214 {
215 BMCWEB_LOG_DEBUG << "Failed to ResetToDefaults: " << ec;
216 messages::internalError(asyncResp->res);
217 return;
218 }
219 // Factory Reset doesn't actually happen until a reboot
220 // Can't erase what the BMC is running on
221 doBMCGracefulRestart(asyncResp);
222 },
223 "xyz.openbmc_project.Software.BMC.Updater",
224 "/xyz/openbmc_project/software",
225 "xyz.openbmc_project.Common.FactoryReset", "Reset");
226 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700227}
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500228
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530229/**
230 * ManagerResetActionInfo derived class for delivering Manager
231 * ResetType AllowableValues using ResetInfo schema.
232 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700233inline void requestRoutesManagerResetActionInfo(App& app)
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530234{
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530235 /**
236 * Functions triggers appropriate requests on DBus
237 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700238
239 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/ResetActionInfo/")
Ed Tanoused398212021-06-09 17:05:54 -0700240 .privileges(redfish::privileges::getActionInfo)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700241 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700242 [&app](const crow::Request& req,
243 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000244 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700245 {
246 return;
247 }
Ed Tanous14766872022-03-15 10:44:42 -0700248
Ed Tanous002d39b2022-05-31 08:59:27 -0700249 asyncResp->res.jsonValue["@odata.type"] =
250 "#ActionInfo.v1_1_2.ActionInfo";
251 asyncResp->res.jsonValue["@odata.id"] =
252 "/redfish/v1/Managers/bmc/ResetActionInfo";
253 asyncResp->res.jsonValue["Name"] = "Reset Action Info";
254 asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
255 nlohmann::json::object_t parameter;
256 parameter["Name"] = "ResetType";
257 parameter["Required"] = true;
258 parameter["DataType"] = "String";
Ed Tanous14766872022-03-15 10:44:42 -0700259
Ed Tanous002d39b2022-05-31 08:59:27 -0700260 nlohmann::json::array_t allowableValues;
Patrick Williamsad539542023-05-12 10:10:08 -0500261 allowableValues.emplace_back("GracefulRestart");
262 allowableValues.emplace_back("ForceRestart");
Ed Tanous002d39b2022-05-31 08:59:27 -0700263 parameter["AllowableValues"] = std::move(allowableValues);
Ed Tanous14766872022-03-15 10:44:42 -0700264
Ed Tanous002d39b2022-05-31 08:59:27 -0700265 nlohmann::json::array_t parameters;
Patrick Williamsad539542023-05-12 10:10:08 -0500266 parameters.emplace_back(std::move(parameter));
Ed Tanous14766872022-03-15 10:44:42 -0700267
Ed Tanous002d39b2022-05-31 08:59:27 -0700268 asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
269 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700270}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530271
James Feist5b4aa862018-08-16 14:07:01 -0700272static constexpr const char* objectManagerIface =
273 "org.freedesktop.DBus.ObjectManager";
274static constexpr const char* pidConfigurationIface =
275 "xyz.openbmc_project.Configuration.Pid";
276static constexpr const char* pidZoneConfigurationIface =
277 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feistb7a08d02018-12-11 14:55:37 -0800278static constexpr const char* stepwiseConfigurationIface =
279 "xyz.openbmc_project.Configuration.Stepwise";
James Feist73df0db2019-03-25 15:29:35 -0700280static constexpr const char* thermalModeIface =
281 "xyz.openbmc_project.Control.ThermalMode";
Borawski.Lukasz9c3106852018-02-09 15:24:22 +0100282
zhanghch058d1b46d2021-04-01 11:18:24 +0800283inline void
284 asyncPopulatePid(const std::string& connection, const std::string& path,
285 const std::string& currentProfile,
286 const std::vector<std::string>& supportedProfiles,
287 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
James Feist5b4aa862018-08-16 14:07:01 -0700288{
George Liu5eb468d2023-06-20 17:03:24 +0800289 sdbusplus::message::object_path objPath(path);
290 dbus::utility::getManagedObjects(
291 connection, objPath,
James Feist73df0db2019-03-25 15:29:35 -0700292 [asyncResp, currentProfile, supportedProfiles](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800293 const boost::system::error_code& ec,
James Feist73df0db2019-03-25 15:29:35 -0700294 const dbus::utility::ManagedObjectType& managedObj) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700295 if (ec)
296 {
297 BMCWEB_LOG_ERROR << ec;
Ed Tanous002d39b2022-05-31 08:59:27 -0700298 messages::internalError(asyncResp->res);
299 return;
300 }
301 nlohmann::json& configRoot =
302 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
303 nlohmann::json& fans = configRoot["FanControllers"];
304 fans["@odata.type"] = "#OemManager.FanControllers";
305 fans["@odata.id"] =
306 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers";
307
308 nlohmann::json& pids = configRoot["PidControllers"];
309 pids["@odata.type"] = "#OemManager.PidControllers";
310 pids["@odata.id"] =
311 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
312
313 nlohmann::json& stepwise = configRoot["StepwiseControllers"];
314 stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
315 stepwise["@odata.id"] =
316 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
317
318 nlohmann::json& zones = configRoot["FanZones"];
319 zones["@odata.id"] =
320 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
321 zones["@odata.type"] = "#OemManager.FanZones";
322 configRoot["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
323 configRoot["@odata.type"] = "#OemManager.Fan";
324 configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
325
326 if (!currentProfile.empty())
327 {
328 configRoot["Profile"] = currentProfile;
329 }
330 BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !";
331
332 for (const auto& pathPair : managedObj)
333 {
334 for (const auto& intfPair : pathPair.second)
James Feist5b4aa862018-08-16 14:07:01 -0700335 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700336 if (intfPair.first != pidConfigurationIface &&
337 intfPair.first != pidZoneConfigurationIface &&
338 intfPair.first != stepwiseConfigurationIface)
James Feist5b4aa862018-08-16 14:07:01 -0700339 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700340 continue;
341 }
James Feist73df0db2019-03-25 15:29:35 -0700342
Ed Tanous002d39b2022-05-31 08:59:27 -0700343 std::string name;
James Feist73df0db2019-03-25 15:29:35 -0700344
Ed Tanous002d39b2022-05-31 08:59:27 -0700345 for (const std::pair<std::string,
346 dbus::utility::DbusVariantType>& propPair :
347 intfPair.second)
348 {
349 if (propPair.first == "Name")
James Feist73df0db2019-03-25 15:29:35 -0700350 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700351 const std::string* namePtr =
352 std::get_if<std::string>(&propPair.second);
353 if (namePtr == nullptr)
James Feist73df0db2019-03-25 15:29:35 -0700354 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700355 BMCWEB_LOG_ERROR << "Pid Name Field illegal";
James Feistc33a90e2019-03-01 10:17:44 -0800356 messages::internalError(asyncResp->res);
357 return;
358 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700359 name = *namePtr;
360 dbus::utility::escapePathForDbus(name);
James Feistb7a08d02018-12-11 14:55:37 -0800361 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700362 else if (propPair.first == "Profiles")
James Feistb7a08d02018-12-11 14:55:37 -0800363 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700364 const std::vector<std::string>* profiles =
365 std::get_if<std::vector<std::string>>(
366 &propPair.second);
367 if (profiles == nullptr)
James Feistb7a08d02018-12-11 14:55:37 -0800368 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700369 BMCWEB_LOG_ERROR << "Pid Profiles Field illegal";
James Feistb7a08d02018-12-11 14:55:37 -0800370 messages::internalError(asyncResp->res);
371 return;
372 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700373 if (std::find(profiles->begin(), profiles->end(),
374 currentProfile) == profiles->end())
James Feistb7a08d02018-12-11 14:55:37 -0800375 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700376 BMCWEB_LOG_INFO
377 << name << " not supported in current profile";
378 continue;
James Feistb7a08d02018-12-11 14:55:37 -0800379 }
380 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700381 }
382 nlohmann::json* config = nullptr;
383 const std::string* classPtr = nullptr;
384
385 for (const std::pair<std::string,
386 dbus::utility::DbusVariantType>& propPair :
387 intfPair.second)
388 {
389 if (propPair.first == "Class")
James Feistb7a08d02018-12-11 14:55:37 -0800390 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700391 classPtr = std::get_if<std::string>(&propPair.second);
392 }
393 }
394
Ed Tanousef4c65b2023-04-24 15:28:50 -0700395 boost::urls::url url("/redfish/v1/Managers/bmc");
Ed Tanous002d39b2022-05-31 08:59:27 -0700396 if (intfPair.first == pidZoneConfigurationIface)
397 {
398 std::string chassis;
399 if (!dbus::utility::getNthStringFromPath(pathPair.first.str,
400 5, chassis))
401 {
402 chassis = "#IllegalValue";
403 }
404 nlohmann::json& zone = zones[name];
Ed Tanousef4c65b2023-04-24 15:28:50 -0700405 zone["Chassis"]["@odata.id"] =
406 boost::urls::format("/redfish/v1/Chassis/{}", chassis);
Willy Tueddfc432022-09-26 16:46:38 +0000407 url.set_fragment(
408 ("/Oem/OpenBmc/Fan/FanZones"_json_pointer / name)
409 .to_string());
410 zone["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700411 zone["@odata.type"] = "#OemManager.FanZone";
412 config = &zone;
413 }
414
415 else if (intfPair.first == stepwiseConfigurationIface)
416 {
417 if (classPtr == nullptr)
418 {
419 BMCWEB_LOG_ERROR << "Pid Class Field illegal";
James Feistb7a08d02018-12-11 14:55:37 -0800420 messages::internalError(asyncResp->res);
421 return;
422 }
423
Ed Tanous002d39b2022-05-31 08:59:27 -0700424 nlohmann::json& controller = stepwise[name];
425 config = &controller;
Willy Tueddfc432022-09-26 16:46:38 +0000426 url.set_fragment(
427 ("/Oem/OpenBmc/Fan/StepwiseControllers"_json_pointer /
428 name)
429 .to_string());
430 controller["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700431 controller["@odata.type"] =
432 "#OemManager.StepwiseController";
433
434 controller["Direction"] = *classPtr;
435 }
436
437 // pid and fans are off the same configuration
438 else if (intfPair.first == pidConfigurationIface)
439 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700440 if (classPtr == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700441 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700442 BMCWEB_LOG_ERROR << "Pid Class Field illegal";
443 messages::internalError(asyncResp->res);
444 return;
445 }
446 bool isFan = *classPtr == "fan";
447 nlohmann::json& element = isFan ? fans[name] : pids[name];
448 config = &element;
449 if (isFan)
450 {
Willy Tueddfc432022-09-26 16:46:38 +0000451 url.set_fragment(
452 ("/Oem/OpenBmc/Fan/FanControllers"_json_pointer /
453 name)
454 .to_string());
455 element["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700456 element["@odata.type"] = "#OemManager.FanController";
457 }
458 else
459 {
Willy Tueddfc432022-09-26 16:46:38 +0000460 url.set_fragment(
461 ("/Oem/OpenBmc/Fan/PidControllers"_json_pointer /
462 name)
463 .to_string());
464 element["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700465 element["@odata.type"] = "#OemManager.PidController";
466 }
467 }
468 else
469 {
470 BMCWEB_LOG_ERROR << "Unexpected configuration";
471 messages::internalError(asyncResp->res);
472 return;
473 }
James Feist5b4aa862018-08-16 14:07:01 -0700474
Ed Tanous002d39b2022-05-31 08:59:27 -0700475 // used for making maps out of 2 vectors
476 const std::vector<double>* keys = nullptr;
477 const std::vector<double>* values = nullptr;
478
479 for (const auto& propertyPair : intfPair.second)
480 {
481 if (propertyPair.first == "Type" ||
482 propertyPair.first == "Class" ||
483 propertyPair.first == "Name")
484 {
485 continue;
486 }
487
488 // zones
489 if (intfPair.first == pidZoneConfigurationIface)
490 {
491 const double* ptr =
492 std::get_if<double>(&propertyPair.second);
493 if (ptr == nullptr)
494 {
495 BMCWEB_LOG_ERROR << "Field Illegal "
496 << propertyPair.first;
497 messages::internalError(asyncResp->res);
498 return;
499 }
500 (*config)[propertyPair.first] = *ptr;
501 }
502
503 if (intfPair.first == stepwiseConfigurationIface)
504 {
505 if (propertyPair.first == "Reading" ||
506 propertyPair.first == "Output")
507 {
508 const std::vector<double>* ptr =
509 std::get_if<std::vector<double>>(
510 &propertyPair.second);
511
512 if (ptr == nullptr)
513 {
514 BMCWEB_LOG_ERROR << "Field Illegal "
515 << propertyPair.first;
516 messages::internalError(asyncResp->res);
517 return;
518 }
519
520 if (propertyPair.first == "Reading")
521 {
522 keys = ptr;
523 }
524 else
525 {
526 values = ptr;
527 }
528 if (keys != nullptr && values != nullptr)
529 {
530 if (keys->size() != values->size())
531 {
532 BMCWEB_LOG_ERROR
533 << "Reading and Output size don't match ";
534 messages::internalError(asyncResp->res);
535 return;
536 }
537 nlohmann::json& steps = (*config)["Steps"];
538 steps = nlohmann::json::array();
539 for (size_t ii = 0; ii < keys->size(); ii++)
540 {
541 nlohmann::json::object_t step;
542 step["Target"] = (*keys)[ii];
543 step["Output"] = (*values)[ii];
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500544 steps.emplace_back(std::move(step));
Ed Tanous002d39b2022-05-31 08:59:27 -0700545 }
546 }
547 }
548 if (propertyPair.first == "NegativeHysteresis" ||
549 propertyPair.first == "PositiveHysteresis")
James Feist5b4aa862018-08-16 14:07:01 -0700550 {
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800551 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800552 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700553 if (ptr == nullptr)
554 {
555 BMCWEB_LOG_ERROR << "Field Illegal "
556 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700557 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700558 return;
559 }
James Feistb7a08d02018-12-11 14:55:37 -0800560 (*config)[propertyPair.first] = *ptr;
561 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700562 }
James Feistb7a08d02018-12-11 14:55:37 -0800563
Ed Tanous002d39b2022-05-31 08:59:27 -0700564 // pid and fans are off the same configuration
565 if (intfPair.first == pidConfigurationIface ||
566 intfPair.first == stepwiseConfigurationIface)
567 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700568 if (propertyPair.first == "Zones")
James Feistb7a08d02018-12-11 14:55:37 -0800569 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700570 const std::vector<std::string>* inputs =
571 std::get_if<std::vector<std::string>>(
572 &propertyPair.second);
573
574 if (inputs == nullptr)
James Feistb7a08d02018-12-11 14:55:37 -0800575 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700576 BMCWEB_LOG_ERROR << "Zones Pid Field Illegal";
577 messages::internalError(asyncResp->res);
578 return;
James Feistb7a08d02018-12-11 14:55:37 -0800579 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700580 auto& data = (*config)[propertyPair.first];
581 data = nlohmann::json::array();
582 for (std::string itemCopy : *inputs)
James Feistb7a08d02018-12-11 14:55:37 -0800583 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700584 dbus::utility::escapePathForDbus(itemCopy);
585 nlohmann::json::object_t input;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700586 boost::urls::url managerUrl = boost::urls::format(
587 "/redfish/v1/Managers/bmc#{}",
Willy Tueddfc432022-09-26 16:46:38 +0000588 ("/Oem/OpenBmc/Fan/FanZones"_json_pointer /
589 itemCopy)
590 .to_string());
591 input["@odata.id"] = std::move(managerUrl);
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500592 data.emplace_back(std::move(input));
James Feistb7a08d02018-12-11 14:55:37 -0800593 }
James Feist5b4aa862018-08-16 14:07:01 -0700594 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700595 // todo(james): may never happen, but this
596 // assumes configuration data referenced in the
597 // PID config is provided by the same daemon, we
598 // could add another loop to cover all cases,
599 // but I'm okay kicking this can down the road a
600 // bit
James Feist5b4aa862018-08-16 14:07:01 -0700601
Ed Tanous002d39b2022-05-31 08:59:27 -0700602 else if (propertyPair.first == "Inputs" ||
603 propertyPair.first == "Outputs")
James Feist5b4aa862018-08-16 14:07:01 -0700604 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700605 auto& data = (*config)[propertyPair.first];
606 const std::vector<std::string>* inputs =
607 std::get_if<std::vector<std::string>>(
608 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700609
Ed Tanous002d39b2022-05-31 08:59:27 -0700610 if (inputs == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700611 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700612 BMCWEB_LOG_ERROR << "Field Illegal "
613 << propertyPair.first;
614 messages::internalError(asyncResp->res);
615 return;
James Feist5b4aa862018-08-16 14:07:01 -0700616 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700617 data = *inputs;
618 }
619 else if (propertyPair.first == "SetPointOffset")
620 {
621 const std::string* ptr =
622 std::get_if<std::string>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700623
Ed Tanous002d39b2022-05-31 08:59:27 -0700624 if (ptr == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700625 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700626 BMCWEB_LOG_ERROR << "Field Illegal "
627 << propertyPair.first;
628 messages::internalError(asyncResp->res);
629 return;
James Feistb943aae2019-07-11 16:33:56 -0700630 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700631 // translate from dbus to redfish
632 if (*ptr == "WarningHigh")
James Feistb943aae2019-07-11 16:33:56 -0700633 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700634 (*config)["SetPointOffset"] =
635 "UpperThresholdNonCritical";
James Feistb943aae2019-07-11 16:33:56 -0700636 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700637 else if (*ptr == "WarningLow")
James Feist5b4aa862018-08-16 14:07:01 -0700638 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700639 (*config)["SetPointOffset"] =
640 "LowerThresholdNonCritical";
James Feist5b4aa862018-08-16 14:07:01 -0700641 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700642 else if (*ptr == "CriticalHigh")
643 {
644 (*config)["SetPointOffset"] =
645 "UpperThresholdCritical";
646 }
647 else if (*ptr == "CriticalLow")
648 {
649 (*config)["SetPointOffset"] =
650 "LowerThresholdCritical";
651 }
652 else
653 {
654 BMCWEB_LOG_ERROR << "Value Illegal " << *ptr;
655 messages::internalError(asyncResp->res);
656 return;
657 }
658 }
659 // doubles
660 else if (propertyPair.first == "FFGainCoefficient" ||
661 propertyPair.first == "FFOffCoefficient" ||
662 propertyPair.first == "ICoefficient" ||
663 propertyPair.first == "ILimitMax" ||
664 propertyPair.first == "ILimitMin" ||
665 propertyPair.first == "PositiveHysteresis" ||
666 propertyPair.first == "NegativeHysteresis" ||
667 propertyPair.first == "OutLimitMax" ||
668 propertyPair.first == "OutLimitMin" ||
669 propertyPair.first == "PCoefficient" ||
670 propertyPair.first == "SetPoint" ||
671 propertyPair.first == "SlewNeg" ||
672 propertyPair.first == "SlewPos")
673 {
674 const double* ptr =
675 std::get_if<double>(&propertyPair.second);
676 if (ptr == nullptr)
677 {
678 BMCWEB_LOG_ERROR << "Field Illegal "
679 << propertyPair.first;
680 messages::internalError(asyncResp->res);
681 return;
682 }
683 (*config)[propertyPair.first] = *ptr;
James Feist5b4aa862018-08-16 14:07:01 -0700684 }
685 }
686 }
687 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700688 }
George Liu5eb468d2023-06-20 17:03:24 +0800689 });
James Feist5b4aa862018-08-16 14:07:01 -0700690}
Jennifer Leeca537922018-08-10 10:07:30 -0700691
James Feist83ff9ab2018-08-31 10:18:24 -0700692enum class CreatePIDRet
693{
694 fail,
695 del,
696 patch
697};
698
zhanghch058d1b46d2021-04-01 11:18:24 +0800699inline bool
700 getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
701 std::vector<nlohmann::json>& config,
702 std::vector<std::string>& zones)
James Feist5f2caae2018-12-12 14:08:25 -0800703{
James Feistb6baeaa2019-02-21 10:41:40 -0800704 if (config.empty())
705 {
706 BMCWEB_LOG_ERROR << "Empty Zones";
Ed Tanousf818b042022-06-27 13:17:35 -0700707 messages::propertyValueFormatError(response->res, config, "Zones");
James Feistb6baeaa2019-02-21 10:41:40 -0800708 return false;
709 }
James Feist5f2caae2018-12-12 14:08:25 -0800710 for (auto& odata : config)
711 {
712 std::string path;
713 if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
714 path))
715 {
716 return false;
717 }
718 std::string input;
James Feist61adbda2019-03-25 13:03:51 -0700719
720 // 8 below comes from
721 // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
722 // 0 1 2 3 4 5 6 7 8
723 if (!dbus::utility::getNthStringFromPath(path, 8, input))
James Feist5f2caae2018-12-12 14:08:25 -0800724 {
725 BMCWEB_LOG_ERROR << "Got invalid path " << path;
726 BMCWEB_LOG_ERROR << "Illegal Type Zones";
Ed Tanousf818b042022-06-27 13:17:35 -0700727 messages::propertyValueFormatError(response->res, odata, "Zones");
James Feist5f2caae2018-12-12 14:08:25 -0800728 return false;
729 }
Ed Tanousa170f272022-06-30 21:53:27 -0700730 std::replace(input.begin(), input.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -0800731 zones.emplace_back(std::move(input));
732 }
733 return true;
734}
735
Ed Tanous711ac7a2021-12-20 09:34:41 -0800736inline const dbus::utility::ManagedObjectType::value_type*
James Feist73df0db2019-03-25 15:29:35 -0700737 findChassis(const dbus::utility::ManagedObjectType& managedObj,
738 const std::string& value, std::string& chassis)
James Feistb6baeaa2019-02-21 10:41:40 -0800739{
740 BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
741
Ed Tanousa170f272022-06-30 21:53:27 -0700742 std::string escaped = value;
Yaswanth Reddy M6ce82fa2023-03-10 07:29:45 +0000743 std::replace(escaped.begin(), escaped.end(), ' ', '_');
James Feistb6baeaa2019-02-21 10:41:40 -0800744 escaped = "/" + escaped;
Ed Tanous002d39b2022-05-31 08:59:27 -0700745 auto it = std::find_if(managedObj.begin(), managedObj.end(),
746 [&escaped](const auto& obj) {
747 if (boost::algorithm::ends_with(obj.first.str, escaped))
748 {
749 BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
750 return true;
751 }
752 return false;
753 });
James Feistb6baeaa2019-02-21 10:41:40 -0800754
755 if (it == managedObj.end())
756 {
James Feist73df0db2019-03-25 15:29:35 -0700757 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800758 }
759 // 5 comes from <chassis-name> being the 5th element
760 // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
James Feist73df0db2019-03-25 15:29:35 -0700761 if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
762 {
763 return &(*it);
764 }
765
766 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800767}
768
Ed Tanous23a21a12020-07-25 04:45:05 +0000769inline CreatePIDRet createPidInterface(
zhanghch058d1b46d2021-04-01 11:18:24 +0800770 const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
Ed Tanousb5a76932020-09-29 16:16:58 -0700771 const nlohmann::json::iterator& it, const std::string& path,
James Feist83ff9ab2018-08-31 10:18:24 -0700772 const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800773 dbus::utility::DBusPropertiesMap& output, std::string& chassis,
774 const std::string& profile)
James Feist83ff9ab2018-08-31 10:18:24 -0700775{
James Feist5f2caae2018-12-12 14:08:25 -0800776 // common deleter
James Feistb6baeaa2019-02-21 10:41:40 -0800777 if (it.value() == nullptr)
James Feist5f2caae2018-12-12 14:08:25 -0800778 {
779 std::string iface;
780 if (type == "PidControllers" || type == "FanControllers")
781 {
782 iface = pidConfigurationIface;
783 }
784 else if (type == "FanZones")
785 {
786 iface = pidZoneConfigurationIface;
787 }
788 else if (type == "StepwiseControllers")
789 {
790 iface = stepwiseConfigurationIface;
791 }
792 else
793 {
Gunnar Millsa0744d32020-11-09 15:40:45 -0600794 BMCWEB_LOG_ERROR << "Illegal Type " << type;
James Feist5f2caae2018-12-12 14:08:25 -0800795 messages::propertyUnknown(response->res, type);
796 return CreatePIDRet::fail;
797 }
James Feist6ee7f772020-02-06 16:25:27 -0800798
799 BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
James Feist5f2caae2018-12-12 14:08:25 -0800800 // delete interface
801 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800802 [response, path](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700803 if (ec)
804 {
805 BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
806 messages::internalError(response->res);
807 return;
808 }
809 messages::success(response->res);
James Feist5f2caae2018-12-12 14:08:25 -0800810 },
811 "xyz.openbmc_project.EntityManager", path, iface, "Delete");
812 return CreatePIDRet::del;
813 }
814
Ed Tanous711ac7a2021-12-20 09:34:41 -0800815 const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800816 if (!createNewObject)
817 {
818 // if we aren't creating a new object, we should be able to find it on
819 // d-bus
James Feist73df0db2019-03-25 15:29:35 -0700820 managedItem = findChassis(managedObj, it.key(), chassis);
821 if (managedItem == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800822 {
823 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
Ed Tanousef4c65b2023-04-24 15:28:50 -0700824 messages::invalidObject(
825 response->res,
826 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800827 return CreatePIDRet::fail;
828 }
829 }
830
Ed Tanous26f69762022-01-25 09:49:11 -0800831 if (!profile.empty() &&
James Feist73df0db2019-03-25 15:29:35 -0700832 (type == "PidControllers" || type == "FanControllers" ||
833 type == "StepwiseControllers"))
834 {
835 if (managedItem == nullptr)
836 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800837 output.emplace_back("Profiles", std::vector<std::string>{profile});
James Feist73df0db2019-03-25 15:29:35 -0700838 }
839 else
840 {
841 std::string interface;
842 if (type == "StepwiseControllers")
843 {
844 interface = stepwiseConfigurationIface;
845 }
846 else
847 {
848 interface = pidConfigurationIface;
849 }
Ed Tanous711ac7a2021-12-20 09:34:41 -0800850 bool ifaceFound = false;
851 for (const auto& iface : managedItem->second)
852 {
853 if (iface.first == interface)
854 {
855 ifaceFound = true;
856 for (const auto& prop : iface.second)
857 {
858 if (prop.first == "Profiles")
859 {
860 const std::vector<std::string>* curProfiles =
861 std::get_if<std::vector<std::string>>(
862 &(prop.second));
863 if (curProfiles == nullptr)
864 {
865 BMCWEB_LOG_ERROR
866 << "Illegal profiles in managed object";
867 messages::internalError(response->res);
868 return CreatePIDRet::fail;
869 }
870 if (std::find(curProfiles->begin(),
871 curProfiles->end(),
872 profile) == curProfiles->end())
873 {
874 std::vector<std::string> newProfiles =
875 *curProfiles;
876 newProfiles.push_back(profile);
Ed Tanousb9d36b42022-02-26 21:42:46 -0800877 output.emplace_back("Profiles", newProfiles);
Ed Tanous711ac7a2021-12-20 09:34:41 -0800878 }
879 }
880 }
881 }
882 }
883
884 if (!ifaceFound)
James Feist73df0db2019-03-25 15:29:35 -0700885 {
886 BMCWEB_LOG_ERROR
887 << "Failed to find interface in managed object";
888 messages::internalError(response->res);
889 return CreatePIDRet::fail;
890 }
James Feist73df0db2019-03-25 15:29:35 -0700891 }
892 }
893
James Feist83ff9ab2018-08-31 10:18:24 -0700894 if (type == "PidControllers" || type == "FanControllers")
895 {
896 if (createNewObject)
897 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800898 output.emplace_back("Class",
899 type == "PidControllers" ? "temp" : "fan");
900 output.emplace_back("Type", "Pid");
James Feist83ff9ab2018-08-31 10:18:24 -0700901 }
James Feist5f2caae2018-12-12 14:08:25 -0800902
903 std::optional<std::vector<nlohmann::json>> zones;
904 std::optional<std::vector<std::string>> inputs;
905 std::optional<std::vector<std::string>> outputs;
906 std::map<std::string, std::optional<double>> doubles;
James Feistb943aae2019-07-11 16:33:56 -0700907 std::optional<std::string> setpointOffset;
James Feist5f2caae2018-12-12 14:08:25 -0800908 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -0800909 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
James Feist5f2caae2018-12-12 14:08:25 -0800910 "Zones", zones, "FFGainCoefficient",
911 doubles["FFGainCoefficient"], "FFOffCoefficient",
912 doubles["FFOffCoefficient"], "ICoefficient",
913 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
914 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
915 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
916 "PCoefficient", doubles["PCoefficient"], "SetPoint",
James Feistb943aae2019-07-11 16:33:56 -0700917 doubles["SetPoint"], "SetPointOffset", setpointOffset,
918 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
919 "PositiveHysteresis", doubles["PositiveHysteresis"],
920 "NegativeHysteresis", doubles["NegativeHysteresis"]))
James Feist83ff9ab2018-08-31 10:18:24 -0700921 {
James Feist5f2caae2018-12-12 14:08:25 -0800922 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700923 }
James Feist5f2caae2018-12-12 14:08:25 -0800924 if (zones)
James Feist83ff9ab2018-08-31 10:18:24 -0700925 {
James Feist5f2caae2018-12-12 14:08:25 -0800926 std::vector<std::string> zonesStr;
927 if (!getZonesFromJsonReq(response, *zones, zonesStr))
James Feist83ff9ab2018-08-31 10:18:24 -0700928 {
Gunnar Millsa0744d32020-11-09 15:40:45 -0600929 BMCWEB_LOG_ERROR << "Illegal Zones";
James Feist5f2caae2018-12-12 14:08:25 -0800930 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700931 }
James Feistb6baeaa2019-02-21 10:41:40 -0800932 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -0800933 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800934 {
935 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
Ed Tanousace85d62021-10-26 12:45:59 -0700936 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -0700937 response->res,
938 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800939 return CreatePIDRet::fail;
940 }
Ed Tanousb9d36b42022-02-26 21:42:46 -0800941 output.emplace_back("Zones", std::move(zonesStr));
James Feist5f2caae2018-12-12 14:08:25 -0800942 }
Ed Tanousafb9ee02022-12-21 11:59:17 -0800943
944 if (inputs)
James Feist5f2caae2018-12-12 14:08:25 -0800945 {
Ed Tanousafb9ee02022-12-21 11:59:17 -0800946 for (std::string& value : *inputs)
James Feist83ff9ab2018-08-31 10:18:24 -0700947 {
Ed Tanousafb9ee02022-12-21 11:59:17 -0800948 std::replace(value.begin(), value.end(), '_', ' ');
James Feist83ff9ab2018-08-31 10:18:24 -0700949 }
Ed Tanousafb9ee02022-12-21 11:59:17 -0800950 output.emplace_back("Inputs", *inputs);
951 }
952
953 if (outputs)
954 {
955 for (std::string& value : *outputs)
956 {
957 std::replace(value.begin(), value.end(), '_', ' ');
958 }
959 output.emplace_back("Outputs", *outputs);
James Feist5f2caae2018-12-12 14:08:25 -0800960 }
James Feist83ff9ab2018-08-31 10:18:24 -0700961
James Feistb943aae2019-07-11 16:33:56 -0700962 if (setpointOffset)
963 {
964 // translate between redfish and dbus names
965 if (*setpointOffset == "UpperThresholdNonCritical")
966 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800967 output.emplace_back("SetPointOffset", "WarningLow");
James Feistb943aae2019-07-11 16:33:56 -0700968 }
969 else if (*setpointOffset == "LowerThresholdNonCritical")
970 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800971 output.emplace_back("SetPointOffset", "WarningHigh");
James Feistb943aae2019-07-11 16:33:56 -0700972 }
973 else if (*setpointOffset == "LowerThresholdCritical")
974 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800975 output.emplace_back("SetPointOffset", "CriticalLow");
James Feistb943aae2019-07-11 16:33:56 -0700976 }
977 else if (*setpointOffset == "UpperThresholdCritical")
978 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800979 output.emplace_back("SetPointOffset", "CriticalHigh");
James Feistb943aae2019-07-11 16:33:56 -0700980 }
981 else
982 {
983 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
984 << *setpointOffset;
Ed Tanousace85d62021-10-26 12:45:59 -0700985 messages::propertyValueNotInList(response->res, it.key(),
986 "SetPointOffset");
James Feistb943aae2019-07-11 16:33:56 -0700987 return CreatePIDRet::fail;
988 }
989 }
990
James Feist5f2caae2018-12-12 14:08:25 -0800991 // doubles
992 for (const auto& pairs : doubles)
993 {
994 if (!pairs.second)
James Feist83ff9ab2018-08-31 10:18:24 -0700995 {
James Feist5f2caae2018-12-12 14:08:25 -0800996 continue;
James Feist83ff9ab2018-08-31 10:18:24 -0700997 }
James Feist5f2caae2018-12-12 14:08:25 -0800998 BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
Ed Tanousb9d36b42022-02-26 21:42:46 -0800999 output.emplace_back(pairs.first, *pairs.second);
James Feist83ff9ab2018-08-31 10:18:24 -07001000 }
1001 }
James Feist5f2caae2018-12-12 14:08:25 -08001002
James Feist83ff9ab2018-08-31 10:18:24 -07001003 else if (type == "FanZones")
1004 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001005 output.emplace_back("Type", "Pid.Zone");
James Feist83ff9ab2018-08-31 10:18:24 -07001006
James Feist5f2caae2018-12-12 14:08:25 -08001007 std::optional<nlohmann::json> chassisContainer;
1008 std::optional<double> failSafePercent;
James Feistd3ec07f2019-02-25 14:51:15 -08001009 std::optional<double> minThermalOutput;
James Feistb6baeaa2019-02-21 10:41:40 -08001010 if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
James Feist5f2caae2018-12-12 14:08:25 -08001011 chassisContainer, "FailSafePercent",
James Feistd3ec07f2019-02-25 14:51:15 -08001012 failSafePercent, "MinThermalOutput",
1013 minThermalOutput))
James Feist83ff9ab2018-08-31 10:18:24 -07001014 {
James Feist5f2caae2018-12-12 14:08:25 -08001015 return CreatePIDRet::fail;
1016 }
James Feist83ff9ab2018-08-31 10:18:24 -07001017
James Feist5f2caae2018-12-12 14:08:25 -08001018 if (chassisContainer)
1019 {
James Feist5f2caae2018-12-12 14:08:25 -08001020 std::string chassisId;
1021 if (!redfish::json_util::readJson(*chassisContainer, response->res,
1022 "@odata.id", chassisId))
James Feist83ff9ab2018-08-31 10:18:24 -07001023 {
James Feist83ff9ab2018-08-31 10:18:24 -07001024 return CreatePIDRet::fail;
1025 }
James Feist5f2caae2018-12-12 14:08:25 -08001026
AppaRao Puli717794d2019-10-18 22:54:53 +05301027 // /redfish/v1/chassis/chassis_name/
James Feist5f2caae2018-12-12 14:08:25 -08001028 if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
1029 {
1030 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
Ed Tanousace85d62021-10-26 12:45:59 -07001031 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001032 response->res,
1033 boost::urls::format("/redfish/v1/Chassis/{}", chassisId));
James Feist5f2caae2018-12-12 14:08:25 -08001034 return CreatePIDRet::fail;
1035 }
1036 }
James Feistd3ec07f2019-02-25 14:51:15 -08001037 if (minThermalOutput)
James Feist5f2caae2018-12-12 14:08:25 -08001038 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001039 output.emplace_back("MinThermalOutput", *minThermalOutput);
James Feist5f2caae2018-12-12 14:08:25 -08001040 }
1041 if (failSafePercent)
1042 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001043 output.emplace_back("FailSafePercent", *failSafePercent);
James Feist5f2caae2018-12-12 14:08:25 -08001044 }
1045 }
1046 else if (type == "StepwiseControllers")
1047 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001048 output.emplace_back("Type", "Stepwise");
James Feist5f2caae2018-12-12 14:08:25 -08001049
1050 std::optional<std::vector<nlohmann::json>> zones;
1051 std::optional<std::vector<nlohmann::json>> steps;
1052 std::optional<std::vector<std::string>> inputs;
1053 std::optional<double> positiveHysteresis;
1054 std::optional<double> negativeHysteresis;
James Feistc33a90e2019-03-01 10:17:44 -08001055 std::optional<std::string> direction; // upper clipping curve vs lower
James Feist5f2caae2018-12-12 14:08:25 -08001056 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -08001057 it.value(), response->res, "Zones", zones, "Steps", steps,
1058 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
James Feistc33a90e2019-03-01 10:17:44 -08001059 "NegativeHysteresis", negativeHysteresis, "Direction",
1060 direction))
James Feist5f2caae2018-12-12 14:08:25 -08001061 {
James Feist5f2caae2018-12-12 14:08:25 -08001062 return CreatePIDRet::fail;
1063 }
1064
1065 if (zones)
1066 {
James Feistb6baeaa2019-02-21 10:41:40 -08001067 std::vector<std::string> zonesStrs;
1068 if (!getZonesFromJsonReq(response, *zones, zonesStrs))
James Feist5f2caae2018-12-12 14:08:25 -08001069 {
Gunnar Millsa0744d32020-11-09 15:40:45 -06001070 BMCWEB_LOG_ERROR << "Illegal Zones";
James Feist5f2caae2018-12-12 14:08:25 -08001071 return CreatePIDRet::fail;
1072 }
James Feistb6baeaa2019-02-21 10:41:40 -08001073 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -08001074 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -08001075 {
1076 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
Ed Tanousace85d62021-10-26 12:45:59 -07001077 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001078 response->res,
1079 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -08001080 return CreatePIDRet::fail;
1081 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001082 output.emplace_back("Zones", std::move(zonesStrs));
James Feist5f2caae2018-12-12 14:08:25 -08001083 }
1084 if (steps)
1085 {
1086 std::vector<double> readings;
1087 std::vector<double> outputs;
1088 for (auto& step : *steps)
1089 {
Ed Tanous543f4402022-01-06 13:12:53 -08001090 double target = 0.0;
1091 double out = 0.0;
James Feist5f2caae2018-12-12 14:08:25 -08001092
1093 if (!redfish::json_util::readJson(step, response->res, "Target",
Ed Tanous23a21a12020-07-25 04:45:05 +00001094 target, "Output", out))
James Feist5f2caae2018-12-12 14:08:25 -08001095 {
James Feist5f2caae2018-12-12 14:08:25 -08001096 return CreatePIDRet::fail;
1097 }
1098 readings.emplace_back(target);
Ed Tanous23a21a12020-07-25 04:45:05 +00001099 outputs.emplace_back(out);
James Feist5f2caae2018-12-12 14:08:25 -08001100 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001101 output.emplace_back("Reading", std::move(readings));
1102 output.emplace_back("Output", std::move(outputs));
James Feist5f2caae2018-12-12 14:08:25 -08001103 }
1104 if (inputs)
1105 {
1106 for (std::string& value : *inputs)
1107 {
Ed Tanousa170f272022-06-30 21:53:27 -07001108 std::replace(value.begin(), value.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -08001109 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001110 output.emplace_back("Inputs", std::move(*inputs));
James Feist5f2caae2018-12-12 14:08:25 -08001111 }
1112 if (negativeHysteresis)
1113 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001114 output.emplace_back("NegativeHysteresis", *negativeHysteresis);
James Feist5f2caae2018-12-12 14:08:25 -08001115 }
1116 if (positiveHysteresis)
1117 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001118 output.emplace_back("PositiveHysteresis", *positiveHysteresis);
James Feist83ff9ab2018-08-31 10:18:24 -07001119 }
James Feistc33a90e2019-03-01 10:17:44 -08001120 if (direction)
1121 {
1122 constexpr const std::array<const char*, 2> allowedDirections = {
1123 "Ceiling", "Floor"};
1124 if (std::find(allowedDirections.begin(), allowedDirections.end(),
1125 *direction) == allowedDirections.end())
1126 {
1127 messages::propertyValueTypeError(response->res, "Direction",
1128 *direction);
1129 return CreatePIDRet::fail;
1130 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001131 output.emplace_back("Class", *direction);
James Feistc33a90e2019-03-01 10:17:44 -08001132 }
James Feist83ff9ab2018-08-31 10:18:24 -07001133 }
1134 else
1135 {
Gunnar Millsa0744d32020-11-09 15:40:45 -06001136 BMCWEB_LOG_ERROR << "Illegal Type " << type;
Jason M. Bills35a62c72018-10-09 12:45:45 -07001137 messages::propertyUnknown(response->res, type);
James Feist83ff9ab2018-08-31 10:18:24 -07001138 return CreatePIDRet::fail;
1139 }
1140 return CreatePIDRet::patch;
1141}
James Feist73df0db2019-03-25 15:29:35 -07001142struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
1143{
Ed Tanous6936afe2022-09-08 15:10:39 -07001144 struct CompletionValues
1145 {
1146 std::vector<std::string> supportedProfiles;
1147 std::string currentProfile;
1148 dbus::utility::MapperGetSubTreeResponse subtree;
1149 };
James Feist73df0db2019-03-25 15:29:35 -07001150
Ed Tanous4e23a442022-06-06 09:57:26 -07001151 explicit GetPIDValues(
1152 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
Ed Tanous23a21a12020-07-25 04:45:05 +00001153 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001154
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001155 {}
James Feist73df0db2019-03-25 15:29:35 -07001156
1157 void run()
1158 {
1159 std::shared_ptr<GetPIDValues> self = shared_from_this();
1160
1161 // get all configurations
George Liue99073f2022-12-09 11:06:16 +08001162 constexpr std::array<std::string_view, 4> interfaces = {
1163 pidConfigurationIface, pidZoneConfigurationIface,
1164 objectManagerIface, stepwiseConfigurationIface};
1165 dbus::utility::getSubTree(
1166 "/", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001167 [self](
George Liue99073f2022-12-09 11:06:16 +08001168 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001169 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001170 if (ec)
1171 {
1172 BMCWEB_LOG_ERROR << ec;
1173 messages::internalError(self->asyncResp->res);
1174 return;
1175 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001176 self->complete.subtree = subtreeLocal;
George Liue99073f2022-12-09 11:06:16 +08001177 });
James Feist73df0db2019-03-25 15:29:35 -07001178
1179 // at the same time get the selected profile
George Liue99073f2022-12-09 11:06:16 +08001180 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1181 thermalModeIface};
1182 dbus::utility::getSubTree(
1183 "/", 0, thermalModeIfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001184 [self](
George Liue99073f2022-12-09 11:06:16 +08001185 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001186 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001187 if (ec || subtreeLocal.empty())
1188 {
1189 return;
1190 }
1191 if (subtreeLocal[0].second.size() != 1)
1192 {
1193 // invalid mapper response, should never happen
1194 BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
1195 messages::internalError(self->asyncResp->res);
1196 return;
1197 }
1198
1199 const std::string& path = subtreeLocal[0].first;
1200 const std::string& owner = subtreeLocal[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001201
1202 sdbusplus::asio::getAllProperties(
1203 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous002d39b2022-05-31 08:59:27 -07001204 [path, owner,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001205 self](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001206 const dbus::utility::DBusPropertiesMap& resp) {
1207 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001208 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001209 BMCWEB_LOG_ERROR
1210 << "GetPIDValues: Can't get thermalModeIface " << path;
James Feist73df0db2019-03-25 15:29:35 -07001211 messages::internalError(self->asyncResp->res);
1212 return;
1213 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001214
Ed Tanous002d39b2022-05-31 08:59:27 -07001215 const std::string* current = nullptr;
1216 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001217
1218 const bool success = sdbusplus::unpackPropertiesNoThrow(
1219 dbus_utils::UnpackErrorPrinter(), resp, "Current", current,
1220 "Supported", supported);
1221
1222 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001223 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001224 messages::internalError(self->asyncResp->res);
1225 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001226 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001227
Ed Tanous002d39b2022-05-31 08:59:27 -07001228 if (current == nullptr || supported == nullptr)
1229 {
1230 BMCWEB_LOG_ERROR
1231 << "GetPIDValues: thermal mode iface invalid " << path;
1232 messages::internalError(self->asyncResp->res);
1233 return;
1234 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001235 self->complete.currentProfile = *current;
1236 self->complete.supportedProfiles = *supported;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001237 });
George Liue99073f2022-12-09 11:06:16 +08001238 });
James Feist73df0db2019-03-25 15:29:35 -07001239 }
1240
Ed Tanous6936afe2022-09-08 15:10:39 -07001241 static void
1242 processingComplete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1243 const CompletionValues& completion)
James Feist73df0db2019-03-25 15:29:35 -07001244 {
1245 if (asyncResp->res.result() != boost::beast::http::status::ok)
1246 {
1247 return;
1248 }
1249 // create map of <connection, path to objMgr>>
Ed Tanous6936afe2022-09-08 15:10:39 -07001250 boost::container::flat_map<
1251 std::string, std::string, std::less<>,
1252 std::vector<std::pair<std::string, std::string>>>
1253 objectMgrPaths;
1254 boost::container::flat_set<std::string, std::less<>,
1255 std::vector<std::string>>
1256 calledConnections;
1257 for (const auto& pathGroup : completion.subtree)
James Feist73df0db2019-03-25 15:29:35 -07001258 {
1259 for (const auto& connectionGroup : pathGroup.second)
1260 {
1261 auto findConnection =
1262 calledConnections.find(connectionGroup.first);
1263 if (findConnection != calledConnections.end())
1264 {
1265 break;
1266 }
1267 for (const std::string& interface : connectionGroup.second)
1268 {
1269 if (interface == objectManagerIface)
1270 {
1271 objectMgrPaths[connectionGroup.first] = pathGroup.first;
1272 }
1273 // this list is alphabetical, so we
1274 // should have found the objMgr by now
1275 if (interface == pidConfigurationIface ||
1276 interface == pidZoneConfigurationIface ||
1277 interface == stepwiseConfigurationIface)
1278 {
1279 auto findObjMgr =
1280 objectMgrPaths.find(connectionGroup.first);
1281 if (findObjMgr == objectMgrPaths.end())
1282 {
1283 BMCWEB_LOG_DEBUG << connectionGroup.first
1284 << "Has no Object Manager";
1285 continue;
1286 }
1287
1288 calledConnections.insert(connectionGroup.first);
1289
1290 asyncPopulatePid(findObjMgr->first, findObjMgr->second,
Ed Tanous6936afe2022-09-08 15:10:39 -07001291 completion.currentProfile,
1292 completion.supportedProfiles,
James Feist73df0db2019-03-25 15:29:35 -07001293 asyncResp);
1294 break;
1295 }
1296 }
1297 }
1298 }
1299 }
1300
Ed Tanous6936afe2022-09-08 15:10:39 -07001301 ~GetPIDValues()
1302 {
1303 boost::asio::post(crow::connections::systemBus->get_io_context(),
1304 std::bind_front(&processingComplete, asyncResp,
1305 std::move(complete)));
1306 }
1307
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001308 GetPIDValues(const GetPIDValues&) = delete;
1309 GetPIDValues(GetPIDValues&&) = delete;
1310 GetPIDValues& operator=(const GetPIDValues&) = delete;
1311 GetPIDValues& operator=(GetPIDValues&&) = delete;
1312
zhanghch058d1b46d2021-04-01 11:18:24 +08001313 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Ed Tanous6936afe2022-09-08 15:10:39 -07001314 CompletionValues complete;
James Feist73df0db2019-03-25 15:29:35 -07001315};
1316
1317struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
1318{
zhanghch058d1b46d2021-04-01 11:18:24 +08001319 SetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
James Feist73df0db2019-03-25 15:29:35 -07001320 nlohmann::json& data) :
Ed Tanous271584a2019-07-09 16:24:22 -07001321 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001322 {
James Feist73df0db2019-03-25 15:29:35 -07001323 std::optional<nlohmann::json> pidControllers;
1324 std::optional<nlohmann::json> fanControllers;
1325 std::optional<nlohmann::json> fanZones;
1326 std::optional<nlohmann::json> stepwiseControllers;
1327
1328 if (!redfish::json_util::readJson(
1329 data, asyncResp->res, "PidControllers", pidControllers,
1330 "FanControllers", fanControllers, "FanZones", fanZones,
1331 "StepwiseControllers", stepwiseControllers, "Profile", profile))
1332 {
James Feist73df0db2019-03-25 15:29:35 -07001333 return;
1334 }
1335 configuration.emplace_back("PidControllers", std::move(pidControllers));
1336 configuration.emplace_back("FanControllers", std::move(fanControllers));
1337 configuration.emplace_back("FanZones", std::move(fanZones));
1338 configuration.emplace_back("StepwiseControllers",
1339 std::move(stepwiseControllers));
1340 }
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001341
1342 SetPIDValues(const SetPIDValues&) = delete;
1343 SetPIDValues(SetPIDValues&&) = delete;
1344 SetPIDValues& operator=(const SetPIDValues&) = delete;
1345 SetPIDValues& operator=(SetPIDValues&&) = delete;
1346
James Feist73df0db2019-03-25 15:29:35 -07001347 void run()
1348 {
1349 if (asyncResp->res.result() != boost::beast::http::status::ok)
1350 {
1351 return;
1352 }
1353
1354 std::shared_ptr<SetPIDValues> self = shared_from_this();
1355
1356 // todo(james): might make sense to do a mapper call here if this
1357 // interface gets more traction
George Liu5eb468d2023-06-20 17:03:24 +08001358 sdbusplus::message::object_path objPath(
1359 "/xyz/openbmc_project/inventory");
1360 dbus::utility::getManagedObjects(
1361 "xyz.openbmc_project.EntityManager", objPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001362 [self](const boost::system::error_code& ec,
Ed Tanous914e2d52022-01-07 11:38:34 -08001363 const dbus::utility::ManagedObjectType& mObj) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001364 if (ec)
1365 {
1366 BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
1367 messages::internalError(self->asyncResp->res);
1368 return;
1369 }
1370 const std::array<const char*, 3> configurations = {
1371 pidConfigurationIface, pidZoneConfigurationIface,
1372 stepwiseConfigurationIface};
James Feiste69d9de2020-02-07 12:23:27 -08001373
Ed Tanous002d39b2022-05-31 08:59:27 -07001374 for (const auto& [path, object] : mObj)
1375 {
1376 for (const auto& [interface, _] : object)
James Feiste69d9de2020-02-07 12:23:27 -08001377 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001378 if (std::find(configurations.begin(), configurations.end(),
1379 interface) != configurations.end())
James Feiste69d9de2020-02-07 12:23:27 -08001380 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001381 self->objectCount++;
1382 break;
James Feiste69d9de2020-02-07 12:23:27 -08001383 }
James Feiste69d9de2020-02-07 12:23:27 -08001384 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001385 }
1386 self->managedObj = mObj;
George Liu5eb468d2023-06-20 17:03:24 +08001387 });
James Feist73df0db2019-03-25 15:29:35 -07001388
1389 // at the same time get the profile information
George Liue99073f2022-12-09 11:06:16 +08001390 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1391 thermalModeIface};
1392 dbus::utility::getSubTree(
1393 "/", 0, thermalModeIfaces,
1394 [self](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001395 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001396 if (ec || subtree.empty())
1397 {
1398 return;
1399 }
1400 if (subtree[0].second.empty())
1401 {
1402 // invalid mapper response, should never happen
1403 BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
1404 messages::internalError(self->asyncResp->res);
1405 return;
1406 }
1407
1408 const std::string& path = subtree[0].first;
1409 const std::string& owner = subtree[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001410 sdbusplus::asio::getAllProperties(
1411 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001412 [self, path, owner](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001413 const dbus::utility::DBusPropertiesMap& r) {
1414 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001415 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001416 BMCWEB_LOG_ERROR
1417 << "SetPIDValues: Can't get thermalModeIface " << path;
James Feist73df0db2019-03-25 15:29:35 -07001418 messages::internalError(self->asyncResp->res);
1419 return;
1420 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001421 const std::string* current = nullptr;
1422 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001423
1424 const bool success = sdbusplus::unpackPropertiesNoThrow(
1425 dbus_utils::UnpackErrorPrinter(), r, "Current", current,
1426 "Supported", supported);
1427
1428 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001429 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001430 messages::internalError(self->asyncResp->res);
1431 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001432 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001433
Ed Tanous002d39b2022-05-31 08:59:27 -07001434 if (current == nullptr || supported == nullptr)
1435 {
1436 BMCWEB_LOG_ERROR
1437 << "SetPIDValues: thermal mode iface invalid " << path;
1438 messages::internalError(self->asyncResp->res);
1439 return;
1440 }
1441 self->currentProfile = *current;
1442 self->supportedProfiles = *supported;
1443 self->profileConnection = owner;
1444 self->profilePath = path;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001445 });
George Liue99073f2022-12-09 11:06:16 +08001446 });
James Feist73df0db2019-03-25 15:29:35 -07001447 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001448 void pidSetDone()
James Feist73df0db2019-03-25 15:29:35 -07001449 {
1450 if (asyncResp->res.result() != boost::beast::http::status::ok)
1451 {
1452 return;
1453 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001454 std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001455 if (profile)
1456 {
1457 if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
1458 *profile) == supportedProfiles.end())
1459 {
1460 messages::actionParameterUnknown(response->res, "Profile",
1461 *profile);
1462 return;
1463 }
1464 currentProfile = *profile;
George Liu9ae226f2023-06-21 17:56:46 +08001465 sdbusplus::asio::setProperty(
1466 *crow::connections::systemBus, profileConnection, profilePath,
1467 thermalModeIface, "Current", *profile,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001468 [response](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001469 if (ec)
1470 {
1471 BMCWEB_LOG_ERROR << "Error patching profile" << ec;
1472 messages::internalError(response->res);
1473 }
George Liu9ae226f2023-06-21 17:56:46 +08001474 });
James Feist73df0db2019-03-25 15:29:35 -07001475 }
1476
1477 for (auto& containerPair : configuration)
1478 {
1479 auto& container = containerPair.second;
1480 if (!container)
1481 {
1482 continue;
1483 }
James Feist6ee7f772020-02-06 16:25:27 -08001484 BMCWEB_LOG_DEBUG << *container;
1485
Ed Tanous02cad962022-06-30 16:50:15 -07001486 const std::string& type = containerPair.first;
James Feist73df0db2019-03-25 15:29:35 -07001487
1488 for (nlohmann::json::iterator it = container->begin();
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301489 it != container->end(); ++it)
James Feist73df0db2019-03-25 15:29:35 -07001490 {
1491 const auto& name = it.key();
Potin Laicddbf3d2023-02-14 14:28:58 +08001492 std::string dbusObjName = name;
1493 std::replace(dbusObjName.begin(), dbusObjName.end(), ' ', '_');
James Feist6ee7f772020-02-06 16:25:27 -08001494 BMCWEB_LOG_DEBUG << "looking for " << name;
1495
Patrick Williams89492a12023-05-10 07:51:34 -05001496 auto pathItr = std::find_if(managedObj.begin(),
1497 managedObj.end(),
1498 [&dbusObjName](const auto& obj) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001499 return boost::algorithm::ends_with(obj.first.str,
Potin Laicddbf3d2023-02-14 14:28:58 +08001500 "/" + dbusObjName);
Patrick Williams89492a12023-05-10 07:51:34 -05001501 });
Ed Tanousb9d36b42022-02-26 21:42:46 -08001502 dbus::utility::DBusPropertiesMap output;
James Feist73df0db2019-03-25 15:29:35 -07001503
1504 output.reserve(16); // The pid interface length
1505
1506 // determines if we're patching entity-manager or
1507 // creating a new object
1508 bool createNewObject = (pathItr == managedObj.end());
James Feist6ee7f772020-02-06 16:25:27 -08001509 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
1510
James Feist73df0db2019-03-25 15:29:35 -07001511 std::string iface;
Ed Tanousea2b6702022-03-07 16:48:38 -08001512 if (!createNewObject)
James Feist73df0db2019-03-25 15:29:35 -07001513 {
Potin Lai8be2b5b2022-11-22 13:27:16 +08001514 bool findInterface = false;
Ed Tanousea2b6702022-03-07 16:48:38 -08001515 for (const auto& interface : pathItr->second)
James Feist73df0db2019-03-25 15:29:35 -07001516 {
Ed Tanousea2b6702022-03-07 16:48:38 -08001517 if (interface.first == pidConfigurationIface)
1518 {
1519 if (type == "PidControllers" ||
1520 type == "FanControllers")
1521 {
1522 iface = pidConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001523 findInterface = true;
1524 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001525 }
1526 }
1527 else if (interface.first == pidZoneConfigurationIface)
1528 {
1529 if (type == "FanZones")
1530 {
1531 iface = pidConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001532 findInterface = true;
1533 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001534 }
1535 }
1536 else if (interface.first == stepwiseConfigurationIface)
1537 {
1538 if (type == "StepwiseControllers")
1539 {
1540 iface = stepwiseConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001541 findInterface = true;
1542 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001543 }
1544 }
James Feist73df0db2019-03-25 15:29:35 -07001545 }
Potin Lai8be2b5b2022-11-22 13:27:16 +08001546
1547 // create new object if interface not found
1548 if (!findInterface)
1549 {
1550 createNewObject = true;
1551 }
James Feist73df0db2019-03-25 15:29:35 -07001552 }
James Feist6ee7f772020-02-06 16:25:27 -08001553
1554 if (createNewObject && it.value() == nullptr)
1555 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -05001556 // can't delete a non-existent object
Ed Tanouse2616cc2022-06-27 12:45:55 -07001557 messages::propertyValueNotInList(response->res, it.value(),
1558 name);
James Feist6ee7f772020-02-06 16:25:27 -08001559 continue;
1560 }
1561
1562 std::string path;
1563 if (pathItr != managedObj.end())
1564 {
1565 path = pathItr->first.str;
1566 }
1567
James Feist73df0db2019-03-25 15:29:35 -07001568 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
James Feiste69d9de2020-02-07 12:23:27 -08001569
1570 // arbitrary limit to avoid attacks
1571 constexpr const size_t controllerLimit = 500;
James Feist14b0b8d2020-02-12 11:52:07 -08001572 if (createNewObject && objectCount >= controllerLimit)
James Feiste69d9de2020-02-07 12:23:27 -08001573 {
1574 messages::resourceExhaustion(response->res, type);
1575 continue;
1576 }
Ed Tanousa170f272022-06-30 21:53:27 -07001577 std::string escaped = name;
1578 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1579 output.emplace_back("Name", escaped);
James Feist73df0db2019-03-25 15:29:35 -07001580
1581 std::string chassis;
1582 CreatePIDRet ret = createPidInterface(
James Feist6ee7f772020-02-06 16:25:27 -08001583 response, type, it, path, managedObj, createNewObject,
1584 output, chassis, currentProfile);
James Feist73df0db2019-03-25 15:29:35 -07001585 if (ret == CreatePIDRet::fail)
1586 {
1587 return;
1588 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001589 if (ret == CreatePIDRet::del)
James Feist73df0db2019-03-25 15:29:35 -07001590 {
1591 continue;
1592 }
1593
1594 if (!createNewObject)
1595 {
1596 for (const auto& property : output)
1597 {
George Liu9ae226f2023-06-21 17:56:46 +08001598 sdbusplus::asio::setProperty(
1599 *crow::connections::systemBus,
1600 "xyz.openbmc_project.EntityManager", path, iface,
1601 property.first, property.second,
James Feist73df0db2019-03-25 15:29:35 -07001602 [response,
1603 propertyName{std::string(property.first)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001604 const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001605 if (ec)
1606 {
1607 BMCWEB_LOG_ERROR << "Error patching "
1608 << propertyName << ": " << ec;
1609 messages::internalError(response->res);
1610 return;
1611 }
1612 messages::success(response->res);
George Liu9ae226f2023-06-21 17:56:46 +08001613 });
James Feist73df0db2019-03-25 15:29:35 -07001614 }
1615 }
1616 else
1617 {
1618 if (chassis.empty())
1619 {
1620 BMCWEB_LOG_ERROR << "Failed to get chassis from config";
Ed Tanousace85d62021-10-26 12:45:59 -07001621 messages::internalError(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001622 return;
1623 }
1624
1625 bool foundChassis = false;
1626 for (const auto& obj : managedObj)
1627 {
1628 if (boost::algorithm::ends_with(obj.first.str, chassis))
1629 {
1630 chassis = obj.first.str;
1631 foundChassis = true;
1632 break;
1633 }
1634 }
1635 if (!foundChassis)
1636 {
1637 BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
1638 messages::resourceMissingAtURI(
Ed Tanousace85d62021-10-26 12:45:59 -07001639 response->res,
Ed Tanousef4c65b2023-04-24 15:28:50 -07001640 boost::urls::format("/redfish/v1/Chassis/{}",
1641 chassis));
James Feist73df0db2019-03-25 15:29:35 -07001642 return;
1643 }
1644
1645 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001646 [response](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001647 if (ec)
1648 {
1649 BMCWEB_LOG_ERROR << "Error Adding Pid Object "
1650 << ec;
1651 messages::internalError(response->res);
1652 return;
1653 }
1654 messages::success(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001655 },
1656 "xyz.openbmc_project.EntityManager", chassis,
1657 "xyz.openbmc_project.AddObject", "AddObject", output);
1658 }
1659 }
1660 }
1661 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001662
1663 ~SetPIDValues()
1664 {
1665 try
1666 {
1667 pidSetDone();
1668 }
1669 catch (...)
1670 {
1671 BMCWEB_LOG_CRITICAL << "pidSetDone threw exception";
1672 }
1673 }
1674
zhanghch058d1b46d2021-04-01 11:18:24 +08001675 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001676 std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
1677 configuration;
1678 std::optional<std::string> profile;
1679 dbus::utility::ManagedObjectType managedObj;
1680 std::vector<std::string> supportedProfiles;
1681 std::string currentProfile;
1682 std::string profileConnection;
1683 std::string profilePath;
James Feist14b0b8d2020-02-12 11:52:07 -08001684 size_t objectCount = 0;
James Feist73df0db2019-03-25 15:29:35 -07001685};
James Feist83ff9ab2018-08-31 10:18:24 -07001686
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001687/**
1688 * @brief Retrieves BMC manager location data over DBus
1689 *
Ed Tanousac106bf2023-06-07 09:24:59 -07001690 * @param[in] asyncResp Shared pointer for completing asynchronous calls
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001691 * @param[in] connectionName - service name
1692 * @param[in] path - object path
1693 * @return none
1694 */
Ed Tanousac106bf2023-06-07 09:24:59 -07001695inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001696 const std::string& connectionName,
1697 const std::string& path)
1698{
1699 BMCWEB_LOG_DEBUG << "Get BMC manager Location data.";
1700
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001701 sdbusplus::asio::getProperty<std::string>(
1702 *crow::connections::systemBus, connectionName, path,
1703 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Ed Tanousac106bf2023-06-07 09:24:59 -07001704 [asyncResp](const boost::system::error_code& ec,
1705 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001706 if (ec)
1707 {
1708 BMCWEB_LOG_DEBUG << "DBUS response error for "
1709 "Location";
Ed Tanousac106bf2023-06-07 09:24:59 -07001710 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001711 return;
1712 }
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001713
Ed Tanousac106bf2023-06-07 09:24:59 -07001714 asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001715 property;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001716 });
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001717}
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001718// avoid name collision systems.hpp
1719inline void
Ed Tanousac106bf2023-06-07 09:24:59 -07001720 managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001721{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001722 BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time";
Ed Tanous52cc1122020-07-18 13:51:21 -07001723
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001724 sdbusplus::asio::getProperty<uint64_t>(
1725 *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
1726 "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
1727 "LastRebootTime",
Ed Tanousac106bf2023-06-07 09:24:59 -07001728 [asyncResp](const boost::system::error_code& ec,
1729 const uint64_t lastResetTime) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001730 if (ec)
1731 {
1732 BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
1733 return;
1734 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001735
Ed Tanous002d39b2022-05-31 08:59:27 -07001736 // LastRebootTime is epoch time, in milliseconds
1737 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
1738 uint64_t lastResetTimeStamp = lastResetTime / 1000;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001739
Ed Tanous002d39b2022-05-31 08:59:27 -07001740 // Convert to ISO 8601 standard
Ed Tanousac106bf2023-06-07 09:24:59 -07001741 asyncResp->res.jsonValue["LastResetTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001742 redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001743 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001744}
1745
1746/**
1747 * @brief Set the running firmware image
1748 *
Ed Tanousac106bf2023-06-07 09:24:59 -07001749 * @param[i,o] asyncResp - Async response object
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001750 * @param[i] runningFirmwareTarget - Image to make the running image
1751 *
1752 * @return void
1753 */
1754inline void
Ed Tanousac106bf2023-06-07 09:24:59 -07001755 setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001756 const std::string& runningFirmwareTarget)
1757{
1758 // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1759 std::string::size_type idPos = runningFirmwareTarget.rfind('/');
1760 if (idPos == std::string::npos)
1761 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001762 messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001763 "@odata.id");
1764 BMCWEB_LOG_DEBUG << "Can't parse firmware ID!";
1765 return;
1766 }
1767 idPos++;
1768 if (idPos >= runningFirmwareTarget.size())
1769 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001770 messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001771 "@odata.id");
1772 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
1773 return;
1774 }
1775 std::string firmwareId = runningFirmwareTarget.substr(idPos);
1776
1777 // Make sure the image is valid before setting priority
George Liu5eb468d2023-06-20 17:03:24 +08001778 sdbusplus::message::object_path objPath("/xyz/openbmc_project/software");
1779 dbus::utility::getManagedObjects(
1780 "xyz.openbmc_project.Software.BMC.Updater", objPath,
1781 [asyncResp, firmwareId, runningFirmwareTarget](
1782 const boost::system::error_code& ec,
1783 const dbus::utility::ManagedObjectType& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001784 if (ec)
1785 {
1786 BMCWEB_LOG_DEBUG << "D-Bus response error getting objects.";
Ed Tanousac106bf2023-06-07 09:24:59 -07001787 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001788 return;
1789 }
1790
1791 if (subtree.empty())
1792 {
1793 BMCWEB_LOG_DEBUG << "Can't find image!";
Ed Tanousac106bf2023-06-07 09:24:59 -07001794 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001795 return;
1796 }
1797
1798 bool foundImage = false;
Ed Tanous02cad962022-06-30 16:50:15 -07001799 for (const auto& object : subtree)
Ed Tanous002d39b2022-05-31 08:59:27 -07001800 {
1801 const std::string& path =
1802 static_cast<const std::string&>(object.first);
1803 std::size_t idPos2 = path.rfind('/');
1804
1805 if (idPos2 == std::string::npos)
1806 {
1807 continue;
1808 }
1809
1810 idPos2++;
1811 if (idPos2 >= path.size())
1812 {
1813 continue;
1814 }
1815
1816 if (path.substr(idPos2) == firmwareId)
1817 {
1818 foundImage = true;
1819 break;
1820 }
1821 }
1822
1823 if (!foundImage)
1824 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001825 messages::propertyValueNotInList(
1826 asyncResp->res, runningFirmwareTarget, "@odata.id");
Ed Tanous002d39b2022-05-31 08:59:27 -07001827 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
1828 return;
1829 }
1830
1831 BMCWEB_LOG_DEBUG << "Setting firmware version " << firmwareId
1832 << " to priority 0.";
1833
1834 // Only support Immediate
1835 // An addition could be a Redfish Setting like
1836 // ActiveSoftwareImageApplyTime and support OnReset
George Liu9ae226f2023-06-21 17:56:46 +08001837 sdbusplus::asio::setProperty(
1838 *crow::connections::systemBus,
1839 "xyz.openbmc_project.Software.BMC.Updater",
1840 "/xyz/openbmc_project/software/" + firmwareId,
1841 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1842 static_cast<uint8_t>(0),
Ed Tanousac106bf2023-06-07 09:24:59 -07001843 [asyncResp](const boost::system::error_code& ec2) {
Ed Tanous8a592812022-06-04 09:06:59 -07001844 if (ec2)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001845 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001846 BMCWEB_LOG_DEBUG << "D-Bus response error setting.";
Ed Tanousac106bf2023-06-07 09:24:59 -07001847 messages::internalError(asyncResp->res);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001848 return;
1849 }
Ed Tanousac106bf2023-06-07 09:24:59 -07001850 doBMCGracefulRestart(asyncResp);
George Liu9ae226f2023-06-21 17:56:46 +08001851 });
George Liu5eb468d2023-06-20 17:03:24 +08001852 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001853}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001854
Ed Tanousac106bf2023-06-07 09:24:59 -07001855inline void setDateTime(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001856 std::string datetime)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001857{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001858 BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001859
Ed Tanousc2e32002023-01-07 22:05:08 -08001860 std::optional<redfish::time_utils::usSinceEpoch> us =
1861 redfish::time_utils::dateStringToEpoch(datetime);
1862 if (!us)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001863 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001864 messages::propertyValueFormatError(asyncResp->res, datetime,
1865 "DateTime");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001866 return;
1867 }
George Liu9ae226f2023-06-21 17:56:46 +08001868 sdbusplus::asio::setProperty(
1869 *crow::connections::systemBus, "xyz.openbmc_project.Time.Manager",
1870 "/xyz/openbmc_project/time/bmc", "xyz.openbmc_project.Time.EpochTime",
1871 "Elapsed", us->count(),
Ed Tanousac106bf2023-06-07 09:24:59 -07001872 [asyncResp{std::move(asyncResp)},
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001873 datetime{std::move(datetime)}](const boost::system::error_code& ec) {
Ed Tanousc2e32002023-01-07 22:05:08 -08001874 if (ec)
1875 {
1876 BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1877 "DBUS response error "
1878 << ec;
Ed Tanousac106bf2023-06-07 09:24:59 -07001879 messages::internalError(asyncResp->res);
Ed Tanousc2e32002023-01-07 22:05:08 -08001880 return;
1881 }
Ed Tanousac106bf2023-06-07 09:24:59 -07001882 asyncResp->res.jsonValue["DateTime"] = datetime;
George Liu9ae226f2023-06-21 17:56:46 +08001883 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001884}
1885
Ed Tanous75815e52022-10-05 17:21:13 -07001886inline void
1887 checkForQuiesced(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1888{
1889 sdbusplus::asio::getProperty<std::string>(
1890 *crow::connections::systemBus, "org.freedesktop.systemd1",
1891 "/org/freedesktop/systemd1/unit/obmc-bmc-service-quiesce@0.target",
1892 "org.freedesktop.systemd1.Unit", "ActiveState",
1893 [asyncResp](const boost::system::error_code& ec,
1894 const std::string& val) {
1895 if (!ec)
1896 {
1897 if (val == "active")
1898 {
1899 asyncResp->res.jsonValue["Status"]["Health"] = "Critical";
1900 asyncResp->res.jsonValue["Status"]["State"] = "Quiesced";
1901 return;
1902 }
1903 }
1904 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
1905 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
1906 });
1907}
1908
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001909inline void requestRoutesManager(App& app)
1910{
1911 std::string uuid = persistent_data::getConfig().systemUuid;
1912
1913 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07001914 .privileges(redfish::privileges::getManager)
Ed Tanous002d39b2022-05-31 08:59:27 -07001915 .methods(boost::beast::http::verb::get)(
1916 [&app, uuid](const crow::Request& req,
Ed Tanous45ca1b82022-03-25 13:07:27 -07001917 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001918 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001919 {
1920 return;
1921 }
1922 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
Sui Chena51fc2d2022-07-14 17:21:53 -07001923 asyncResp->res.jsonValue["@odata.type"] = "#Manager.v1_14_0.Manager";
Ed Tanous002d39b2022-05-31 08:59:27 -07001924 asyncResp->res.jsonValue["Id"] = "bmc";
1925 asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
1926 asyncResp->res.jsonValue["Description"] =
1927 "Baseboard Management Controller";
1928 asyncResp->res.jsonValue["PowerState"] = "On";
Ed Tanous14766872022-03-15 10:44:42 -07001929
Ed Tanous002d39b2022-05-31 08:59:27 -07001930 asyncResp->res.jsonValue["ManagerType"] = "BMC";
1931 asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
1932 asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1933 asyncResp->res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001934
Ed Tanous002d39b2022-05-31 08:59:27 -07001935 asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
1936 "/redfish/v1/Managers/bmc/LogServices";
1937 asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
1938 "/redfish/v1/Managers/bmc/NetworkProtocol";
1939 asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
1940 "/redfish/v1/Managers/bmc/EthernetInterfaces";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001941
1942#ifdef BMCWEB_ENABLE_VM_NBDPROXY
Ed Tanous002d39b2022-05-31 08:59:27 -07001943 asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
1944 "/redfish/v1/Managers/bmc/VirtualMedia";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001945#endif // BMCWEB_ENABLE_VM_NBDPROXY
1946
Ed Tanous002d39b2022-05-31 08:59:27 -07001947 // default oem data
1948 nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
1949 nlohmann::json& oemOpenbmc = oem["OpenBmc"];
1950 oem["@odata.type"] = "#OemManager.Oem";
1951 oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
1952 oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
1953 oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
Ed Tanous14766872022-03-15 10:44:42 -07001954
Ed Tanous002d39b2022-05-31 08:59:27 -07001955 nlohmann::json::object_t certificates;
1956 certificates["@odata.id"] =
1957 "/redfish/v1/Managers/bmc/Truststore/Certificates";
1958 oemOpenbmc["Certificates"] = std::move(certificates);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001959
Ed Tanous002d39b2022-05-31 08:59:27 -07001960 // Manager.Reset (an action) can be many values, OpenBMC only
1961 // supports BMC reboot.
1962 nlohmann::json& managerReset =
1963 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
1964 managerReset["target"] =
1965 "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
1966 managerReset["@Redfish.ActionInfo"] =
1967 "/redfish/v1/Managers/bmc/ResetActionInfo";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001968
Ed Tanous002d39b2022-05-31 08:59:27 -07001969 // ResetToDefaults (Factory Reset) has values like
1970 // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
1971 // on OpenBMC
1972 nlohmann::json& resetToDefaults =
1973 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
1974 resetToDefaults["target"] =
1975 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
Ed Tanous613dabe2022-07-09 11:17:36 -07001976 resetToDefaults["ResetType@Redfish.AllowableValues"] =
1977 nlohmann::json::array_t({"ResetAll"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001978
Ed Tanous002d39b2022-05-31 08:59:27 -07001979 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001980 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301981
Ed Tanous002d39b2022-05-31 08:59:27 -07001982 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1983 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1984 redfishDateTimeOffset.second;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001985
Ed Tanous002d39b2022-05-31 08:59:27 -07001986 // TODO (Gunnar): Remove these one day since moved to ComputerSystem
1987 // Still used by OCP profiles
1988 // https://github.com/opencomputeproject/OCP-Profiles/issues/23
1989 // Fill in SerialConsole info
1990 asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
1991 asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
Ed Tanous613dabe2022-07-09 11:17:36 -07001992 asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
1993 nlohmann::json::array_t({"IPMI", "SSH"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001994#ifdef BMCWEB_ENABLE_KVM
Ed Tanous002d39b2022-05-31 08:59:27 -07001995 // Fill in GraphicalConsole info
1996 asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
1997 asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] =
1998 4;
Ed Tanous613dabe2022-07-09 11:17:36 -07001999 asyncResp->res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
2000 nlohmann::json::array_t({"KVMIP"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002001#endif // BMCWEB_ENABLE_KVM
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002002 if constexpr (!bmcwebEnableMultiHost)
2003 {
2004 asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] =
2005 1;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002006
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002007 nlohmann::json::array_t managerForServers;
2008 nlohmann::json::object_t manager;
2009 manager["@odata.id"] = "/redfish/v1/Systems/system";
2010 managerForServers.emplace_back(std::move(manager));
Ed Tanous14766872022-03-15 10:44:42 -07002011
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002012 asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
2013 std::move(managerForServers);
2014 }
Willy Tu13451e32023-05-24 16:08:18 -07002015 if constexpr (bmcwebEnableHealthPopulate)
2016 {
2017 auto health = std::make_shared<HealthPopulate>(asyncResp);
2018 health->isManagersHealth = true;
2019 health->populate();
2020 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002021
Willy Tueee00132022-06-14 14:53:17 -07002022 sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
Ed Tanous002d39b2022-05-31 08:59:27 -07002023 "FirmwareVersion", true);
2024
2025 managerGetLastResetTime(asyncResp);
2026
Sui Chena51fc2d2022-07-14 17:21:53 -07002027 // ManagerDiagnosticData is added for all BMCs.
2028 nlohmann::json& managerDiagnosticData =
2029 asyncResp->res.jsonValue["ManagerDiagnosticData"];
2030 managerDiagnosticData["@odata.id"] =
2031 "/redfish/v1/Managers/bmc/ManagerDiagnosticData";
2032
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002033#ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
Ed Tanous002d39b2022-05-31 08:59:27 -07002034 auto pids = std::make_shared<GetPIDValues>(asyncResp);
2035 pids->run();
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002036#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07002037
2038 getMainChassisId(asyncResp,
2039 [](const std::string& chassisId,
2040 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2041 aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
2042 nlohmann::json::array_t managerForChassis;
Ed Tanous8a592812022-06-04 09:06:59 -07002043 nlohmann::json::object_t managerObj;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002044 boost::urls::url chassiUrl =
2045 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
Willy Tueddfc432022-09-26 16:46:38 +00002046 managerObj["@odata.id"] = chassiUrl;
Patrick Williamsad539542023-05-12 10:10:08 -05002047 managerForChassis.emplace_back(std::move(managerObj));
Ed Tanous002d39b2022-05-31 08:59:27 -07002048 aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
2049 std::move(managerForChassis);
2050 aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00002051 chassiUrl;
Ed Tanous002d39b2022-05-31 08:59:27 -07002052 });
Ed Tanous14766872022-03-15 10:44:42 -07002053
Ed Tanous75815e52022-10-05 17:21:13 -07002054 sdbusplus::asio::getProperty<double>(
2055 *crow::connections::systemBus, "org.freedesktop.systemd1",
2056 "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
2057 "Progress",
2058 [asyncResp](const boost::system::error_code& ec, double val) {
2059 if (ec)
2060 {
2061 BMCWEB_LOG_ERROR << "Error while getting progress";
2062 messages::internalError(asyncResp->res);
2063 return;
2064 }
2065 if (val < 1.0)
2066 {
2067 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
2068 asyncResp->res.jsonValue["Status"]["State"] = "Starting";
2069 return;
2070 }
2071 checkForQuiesced(asyncResp);
2072 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002073
George Liue99073f2022-12-09 11:06:16 +08002074 constexpr std::array<std::string_view, 1> interfaces = {
2075 "xyz.openbmc_project.Inventory.Item.Bmc"};
2076 dbus::utility::getSubTree(
2077 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07002078 [asyncResp](
George Liue99073f2022-12-09 11:06:16 +08002079 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07002080 const dbus::utility::MapperGetSubTreeResponse& subtree) {
2081 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002082 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002083 BMCWEB_LOG_DEBUG << "D-Bus response error on GetSubTree " << ec;
2084 return;
2085 }
2086 if (subtree.empty())
2087 {
2088 BMCWEB_LOG_DEBUG << "Can't find bmc D-Bus object!";
2089 return;
2090 }
2091 // Assume only 1 bmc D-Bus object
2092 // Throw an error if there is more than 1
2093 if (subtree.size() > 1)
2094 {
2095 BMCWEB_LOG_DEBUG << "Found more than 1 bmc D-Bus object!";
2096 messages::internalError(asyncResp->res);
2097 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002098 }
2099
Ed Tanous002d39b2022-05-31 08:59:27 -07002100 if (subtree[0].first.empty() || subtree[0].second.size() != 1)
2101 {
2102 BMCWEB_LOG_DEBUG << "Error getting bmc D-Bus object!";
2103 messages::internalError(asyncResp->res);
2104 return;
2105 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002106
Ed Tanous002d39b2022-05-31 08:59:27 -07002107 const std::string& path = subtree[0].first;
2108 const std::string& connectionName = subtree[0].second[0].first;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002109
Ed Tanous002d39b2022-05-31 08:59:27 -07002110 for (const auto& interfaceName : subtree[0].second[0].second)
2111 {
2112 if (interfaceName ==
2113 "xyz.openbmc_project.Inventory.Decorator.Asset")
2114 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002115 sdbusplus::asio::getAllProperties(
2116 *crow::connections::systemBus, connectionName, path,
2117 "xyz.openbmc_project.Inventory.Decorator.Asset",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002118 [asyncResp](const boost::system::error_code& ec2,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002119 const dbus::utility::DBusPropertiesMap&
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002120 propertiesList) {
Ed Tanous8a592812022-06-04 09:06:59 -07002121 if (ec2)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002122 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002123 BMCWEB_LOG_DEBUG << "Can't get bmc asset!";
2124 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002125 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002126
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002127 const std::string* partNumber = nullptr;
2128 const std::string* serialNumber = nullptr;
2129 const std::string* manufacturer = nullptr;
2130 const std::string* model = nullptr;
2131 const std::string* sparePartNumber = nullptr;
2132
2133 const bool success = sdbusplus::unpackPropertiesNoThrow(
2134 dbus_utils::UnpackErrorPrinter(), propertiesList,
2135 "PartNumber", partNumber, "SerialNumber",
2136 serialNumber, "Manufacturer", manufacturer, "Model",
2137 model, "SparePartNumber", sparePartNumber);
2138
2139 if (!success)
2140 {
2141 messages::internalError(asyncResp->res);
2142 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07002143 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002144
2145 if (partNumber != nullptr)
2146 {
2147 asyncResp->res.jsonValue["PartNumber"] =
2148 *partNumber;
2149 }
2150
2151 if (serialNumber != nullptr)
2152 {
2153 asyncResp->res.jsonValue["SerialNumber"] =
2154 *serialNumber;
2155 }
2156
2157 if (manufacturer != nullptr)
2158 {
2159 asyncResp->res.jsonValue["Manufacturer"] =
2160 *manufacturer;
2161 }
2162
2163 if (model != nullptr)
2164 {
2165 asyncResp->res.jsonValue["Model"] = *model;
2166 }
2167
2168 if (sparePartNumber != nullptr)
2169 {
2170 asyncResp->res.jsonValue["SparePartNumber"] =
2171 *sparePartNumber;
2172 }
2173 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002174 }
2175 else if (interfaceName ==
2176 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
2177 {
2178 getLocation(asyncResp, connectionName, path);
2179 }
2180 }
George Liue99073f2022-12-09 11:06:16 +08002181 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002182 });
2183
2184 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07002185 .privileges(redfish::privileges::patchManager)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002186 .methods(boost::beast::http::verb::patch)(
2187 [&app](const crow::Request& req,
2188 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002189 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002190 {
2191 return;
2192 }
2193 std::optional<nlohmann::json> oem;
2194 std::optional<nlohmann::json> links;
2195 std::optional<std::string> datetime;
2196
2197 if (!json_util::readJsonPatch(req, asyncResp->res, "Oem", oem,
2198 "DateTime", datetime, "Links", links))
2199 {
2200 return;
2201 }
2202
2203 if (oem)
2204 {
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002205#ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
Ed Tanous002d39b2022-05-31 08:59:27 -07002206 std::optional<nlohmann::json> openbmc;
2207 if (!redfish::json_util::readJson(*oem, asyncResp->res, "OpenBmc",
2208 openbmc))
2209 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002210 return;
2211 }
2212 if (openbmc)
2213 {
2214 std::optional<nlohmann::json> fan;
2215 if (!redfish::json_util::readJson(*openbmc, asyncResp->res,
2216 "Fan", fan))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002217 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002218 return;
2219 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002220 if (fan)
2221 {
2222 auto pid = std::make_shared<SetPIDValues>(asyncResp, *fan);
2223 pid->run();
2224 }
2225 }
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002226#else
2227 messages::propertyUnknown(asyncResp->res, "Oem");
2228 return;
2229#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07002230 }
2231 if (links)
2232 {
2233 std::optional<nlohmann::json> activeSoftwareImage;
2234 if (!redfish::json_util::readJson(*links, asyncResp->res,
2235 "ActiveSoftwareImage",
2236 activeSoftwareImage))
2237 {
2238 return;
2239 }
2240 if (activeSoftwareImage)
2241 {
2242 std::optional<std::string> odataId;
2243 if (!json_util::readJson(*activeSoftwareImage, asyncResp->res,
2244 "@odata.id", odataId))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002245 {
Ed Tanous45ca1b82022-03-25 13:07:27 -07002246 return;
2247 }
2248
Ed Tanous002d39b2022-05-31 08:59:27 -07002249 if (odataId)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002250 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002251 setActiveFirmwareImage(asyncResp, *odataId);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002252 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002253 }
2254 }
2255 if (datetime)
2256 {
2257 setDateTime(asyncResp, std::move(*datetime));
2258 }
2259 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002260}
2261
2262inline void requestRoutesManagerCollection(App& app)
2263{
2264 BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
Ed Tanoused398212021-06-09 17:05:54 -07002265 .privileges(redfish::privileges::getManagerCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002266 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002267 [&app](const crow::Request& req,
2268 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002269 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002270 {
2271 return;
2272 }
2273 // Collections don't include the static data added by SubRoute
2274 // because it has a duplicate entry for members
2275 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
2276 asyncResp->res.jsonValue["@odata.type"] =
2277 "#ManagerCollection.ManagerCollection";
2278 asyncResp->res.jsonValue["Name"] = "Manager Collection";
2279 asyncResp->res.jsonValue["Members@odata.count"] = 1;
2280 nlohmann::json::array_t members;
2281 nlohmann::json& bmc = members.emplace_back();
2282 bmc["@odata.id"] = "/redfish/v1/Managers/bmc";
2283 asyncResp->res.jsonValue["Members"] = std::move(members);
2284 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002285}
Ed Tanous1abe55e2018-09-05 08:30:59 -07002286} // namespace redfish