blob: b08ee51c8ceade0d078fd51a0c8185b3056054df [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
Sui Chena51fc2d2022-07-14 17:21:53 -070018#include "app.hpp"
19#include "dbus_utility.hpp"
James Feistb49ac872019-05-21 15:12:01 -070020#include "health.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070021#include "query.hpp"
Jennifer Leec5d03ff2019-03-08 15:42:58 -080022#include "redfish_util.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070023#include "registries/privilege_registry.hpp"
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +020024#include "utils/dbus_utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080025#include "utils/json_utils.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070026#include "utils/sw_utils.hpp"
27#include "utils/systemd_utils.hpp"
Ed Tanous2b829372022-08-03 14:22:34 -070028#include "utils/time_utils.hpp"
Borawski.Lukasz9c3106852018-02-09 15:24:22 +010029
George Liue99073f2022-12-09 11:06:16 +080030#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070031#include <boost/url/format.hpp>
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +020032#include <sdbusplus/asio/property.hpp>
33#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050034
Ed Tanousa170f272022-06-30 21:53:27 -070035#include <algorithm>
George Liue99073f2022-12-09 11:06:16 +080036#include <array>
Gunnar Mills4bfefa72020-07-30 13:54:29 -050037#include <cstdint>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050038#include <memory>
39#include <sstream>
George Liue99073f2022-12-09 11:06:16 +080040#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080041#include <variant>
James Feist5b4aa862018-08-16 14:07:01 -070042
Ed Tanous1abe55e2018-09-05 08:30:59 -070043namespace redfish
44{
Jennifer Leeed5befb2018-08-10 11:29:45 -070045
46/**
Gunnar Mills2a5c4402020-05-19 09:07:24 -050047 * Function reboots the BMC.
48 *
49 * @param[in] asyncResp - Shared pointer for completing asynchronous calls
Jennifer Leeed5befb2018-08-10 11:29:45 -070050 */
zhanghch058d1b46d2021-04-01 11:18:24 +080051inline void
52 doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Gunnar Mills2a5c4402020-05-19 09:07:24 -050053{
54 const char* processName = "xyz.openbmc_project.State.BMC";
55 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
56 const char* interfaceName = "xyz.openbmc_project.State.BMC";
57 const std::string& propertyValue =
58 "xyz.openbmc_project.State.BMC.Transition.Reboot";
59 const char* destProperty = "RequestedBMCTransition";
60
61 // Create the D-Bus variant for D-Bus call.
Ed Tanous168e20c2021-12-13 14:39:53 -080062 dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
Gunnar Mills2a5c4402020-05-19 09:07:24 -050063
64 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080065 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -070066 // Use "Set" method to set the property value.
67 if (ec)
68 {
69 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
70 messages::internalError(asyncResp->res);
71 return;
72 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -050073
Ed Tanous002d39b2022-05-31 08:59:27 -070074 messages::success(asyncResp->res);
Gunnar Mills2a5c4402020-05-19 09:07:24 -050075 },
76 processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
77 interfaceName, destProperty, dbusPropertyValue);
78}
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.
Ed Tanous168e20c2021-12-13 14:39:53 -080091 dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +000092
93 crow::connections::systemBus->async_method_call(
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);
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000104 },
105 processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
106 interfaceName, destProperty, dbusPropertyValue);
107}
108
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500109/**
110 * ManagerResetAction class supports the POST method for the Reset (reboot)
111 * action.
112 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700113inline void requestRoutesManagerResetAction(App& app)
Jennifer Leeed5befb2018-08-10 11:29:45 -0700114{
Jennifer Leeed5befb2018-08-10 11:29:45 -0700115 /**
Jennifer Leeed5befb2018-08-10 11:29:45 -0700116 * Function handles POST method request.
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500117 * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000118 * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
Jennifer Leeed5befb2018-08-10 11:29:45 -0700119 */
Jennifer Leeed5befb2018-08-10 11:29:45 -0700120
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700121 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
Ed Tanoused398212021-06-09 17:05:54 -0700122 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700123 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700124 [&app](const crow::Request& req,
125 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000126 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700127 {
128 return;
129 }
130 BMCWEB_LOG_DEBUG << "Post Manager Reset.";
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500131
Ed Tanous002d39b2022-05-31 08:59:27 -0700132 std::string resetType;
Jennifer Leeed5befb2018-08-10 11:29:45 -0700133
Ed Tanous002d39b2022-05-31 08:59:27 -0700134 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
135 resetType))
136 {
137 return;
138 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500139
Ed Tanous002d39b2022-05-31 08:59:27 -0700140 if (resetType == "GracefulRestart")
141 {
142 BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
143 doBMCGracefulRestart(asyncResp);
144 return;
145 }
146 if (resetType == "ForceRestart")
147 {
148 BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
149 doBMCForceRestart(asyncResp);
150 return;
151 }
152 BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
153 << resetType;
154 messages::actionParameterNotSupported(asyncResp->res, resetType,
155 "ResetType");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700156
Ed Tanous002d39b2022-05-31 08:59:27 -0700157 return;
158 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700159}
Jennifer Leeed5befb2018-08-10 11:29:45 -0700160
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500161/**
162 * ManagerResetToDefaultsAction class supports POST method for factory reset
163 * action.
164 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700165inline void requestRoutesManagerResetToDefaultsAction(App& app)
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500166{
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500167 /**
168 * Function handles ResetToDefaults POST method request.
169 *
170 * Analyzes POST body message and factory resets BMC by calling
171 * BMC code updater factory reset followed by a BMC reboot.
172 *
173 * BMC code updater factory reset wipes the whole BMC read-write
174 * filesystem which includes things like the network settings.
175 *
176 * OpenBMC only supports ResetToDefaultsType "ResetAll".
177 */
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500178
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700179 BMCWEB_ROUTE(app,
180 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/")
Ed Tanoused398212021-06-09 17:05:54 -0700181 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700182 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700183 [&app](const crow::Request& req,
184 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000185 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700186 {
187 return;
188 }
189 BMCWEB_LOG_DEBUG << "Post ResetToDefaults.";
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500190
Ed Tanous002d39b2022-05-31 08:59:27 -0700191 std::string resetType;
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500192
Ed Tanous002d39b2022-05-31 08:59:27 -0700193 if (!json_util::readJsonAction(req, asyncResp->res,
194 "ResetToDefaultsType", resetType))
195 {
196 BMCWEB_LOG_DEBUG << "Missing property ResetToDefaultsType.";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700197
Ed Tanous002d39b2022-05-31 08:59:27 -0700198 messages::actionParameterMissing(asyncResp->res, "ResetToDefaults",
199 "ResetToDefaultsType");
200 return;
201 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700202
Ed Tanous002d39b2022-05-31 08:59:27 -0700203 if (resetType != "ResetAll")
204 {
205 BMCWEB_LOG_DEBUG
206 << "Invalid property value for ResetToDefaultsType: "
207 << resetType;
208 messages::actionParameterNotSupported(asyncResp->res, resetType,
209 "ResetToDefaultsType");
210 return;
211 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700212
Ed Tanous002d39b2022-05-31 08:59:27 -0700213 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800214 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700215 if (ec)
216 {
217 BMCWEB_LOG_DEBUG << "Failed to ResetToDefaults: " << ec;
218 messages::internalError(asyncResp->res);
219 return;
220 }
221 // Factory Reset doesn't actually happen until a reboot
222 // Can't erase what the BMC is running on
223 doBMCGracefulRestart(asyncResp);
224 },
225 "xyz.openbmc_project.Software.BMC.Updater",
226 "/xyz/openbmc_project/software",
227 "xyz.openbmc_project.Common.FactoryReset", "Reset");
228 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700229}
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500230
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530231/**
232 * ManagerResetActionInfo derived class for delivering Manager
233 * ResetType AllowableValues using ResetInfo schema.
234 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700235inline void requestRoutesManagerResetActionInfo(App& app)
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530236{
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530237 /**
238 * Functions triggers appropriate requests on DBus
239 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700240
241 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/ResetActionInfo/")
Ed Tanoused398212021-06-09 17:05:54 -0700242 .privileges(redfish::privileges::getActionInfo)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700243 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700244 [&app](const crow::Request& req,
245 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000246 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700247 {
248 return;
249 }
Ed Tanous14766872022-03-15 10:44:42 -0700250
Ed Tanous002d39b2022-05-31 08:59:27 -0700251 asyncResp->res.jsonValue["@odata.type"] =
252 "#ActionInfo.v1_1_2.ActionInfo";
253 asyncResp->res.jsonValue["@odata.id"] =
254 "/redfish/v1/Managers/bmc/ResetActionInfo";
255 asyncResp->res.jsonValue["Name"] = "Reset Action Info";
256 asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
257 nlohmann::json::object_t parameter;
258 parameter["Name"] = "ResetType";
259 parameter["Required"] = true;
260 parameter["DataType"] = "String";
Ed Tanous14766872022-03-15 10:44:42 -0700261
Ed Tanous002d39b2022-05-31 08:59:27 -0700262 nlohmann::json::array_t allowableValues;
Patrick Williamsad539542023-05-12 10:10:08 -0500263 allowableValues.emplace_back("GracefulRestart");
264 allowableValues.emplace_back("ForceRestart");
Ed Tanous002d39b2022-05-31 08:59:27 -0700265 parameter["AllowableValues"] = std::move(allowableValues);
Ed Tanous14766872022-03-15 10:44:42 -0700266
Ed Tanous002d39b2022-05-31 08:59:27 -0700267 nlohmann::json::array_t parameters;
Patrick Williamsad539542023-05-12 10:10:08 -0500268 parameters.emplace_back(std::move(parameter));
Ed Tanous14766872022-03-15 10:44:42 -0700269
Ed Tanous002d39b2022-05-31 08:59:27 -0700270 asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
271 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700272}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530273
James Feist5b4aa862018-08-16 14:07:01 -0700274static constexpr const char* objectManagerIface =
275 "org.freedesktop.DBus.ObjectManager";
276static constexpr const char* pidConfigurationIface =
277 "xyz.openbmc_project.Configuration.Pid";
278static constexpr const char* pidZoneConfigurationIface =
279 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feistb7a08d02018-12-11 14:55:37 -0800280static constexpr const char* stepwiseConfigurationIface =
281 "xyz.openbmc_project.Configuration.Stepwise";
James Feist73df0db2019-03-25 15:29:35 -0700282static constexpr const char* thermalModeIface =
283 "xyz.openbmc_project.Control.ThermalMode";
Borawski.Lukasz9c3106852018-02-09 15:24:22 +0100284
zhanghch058d1b46d2021-04-01 11:18:24 +0800285inline void
286 asyncPopulatePid(const std::string& connection, const std::string& path,
287 const std::string& currentProfile,
288 const std::vector<std::string>& supportedProfiles,
289 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
James Feist5b4aa862018-08-16 14:07:01 -0700290{
James Feist5b4aa862018-08-16 14:07:01 -0700291 crow::connections::systemBus->async_method_call(
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 }
James Feist5b4aa862018-08-16 14:07:01 -0700689 },
690 connection, path, objectManagerIface, "GetManagedObjects");
691}
Jennifer Leeca537922018-08-10 10:07:30 -0700692
James Feist83ff9ab2018-08-31 10:18:24 -0700693enum class CreatePIDRet
694{
695 fail,
696 del,
697 patch
698};
699
zhanghch058d1b46d2021-04-01 11:18:24 +0800700inline bool
701 getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
702 std::vector<nlohmann::json>& config,
703 std::vector<std::string>& zones)
James Feist5f2caae2018-12-12 14:08:25 -0800704{
James Feistb6baeaa2019-02-21 10:41:40 -0800705 if (config.empty())
706 {
707 BMCWEB_LOG_ERROR << "Empty Zones";
Ed Tanous1668ce62022-02-07 23:44:31 -0800708 messages::propertyValueFormatError(response->res, "[]", "Zones");
James Feistb6baeaa2019-02-21 10:41:40 -0800709 return false;
710 }
James Feist5f2caae2018-12-12 14:08:25 -0800711 for (auto& odata : config)
712 {
713 std::string path;
714 if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
715 path))
716 {
717 return false;
718 }
719 std::string input;
James Feist61adbda2019-03-25 13:03:51 -0700720
721 // 8 below comes from
722 // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
723 // 0 1 2 3 4 5 6 7 8
724 if (!dbus::utility::getNthStringFromPath(path, 8, input))
James Feist5f2caae2018-12-12 14:08:25 -0800725 {
726 BMCWEB_LOG_ERROR << "Got invalid path " << path;
727 BMCWEB_LOG_ERROR << "Illegal Type Zones";
728 messages::propertyValueFormatError(response->res, odata.dump(),
729 "Zones");
730 return false;
731 }
Ed Tanousa170f272022-06-30 21:53:27 -0700732 std::replace(input.begin(), input.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -0800733 zones.emplace_back(std::move(input));
734 }
735 return true;
736}
737
Ed Tanous711ac7a2021-12-20 09:34:41 -0800738inline const dbus::utility::ManagedObjectType::value_type*
James Feist73df0db2019-03-25 15:29:35 -0700739 findChassis(const dbus::utility::ManagedObjectType& managedObj,
740 const std::string& value, std::string& chassis)
James Feistb6baeaa2019-02-21 10:41:40 -0800741{
742 BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
743
Ed Tanousa170f272022-06-30 21:53:27 -0700744 std::string escaped = value;
Yaswanth Reddy M6ce82fa2023-03-10 07:29:45 +0000745 std::replace(escaped.begin(), escaped.end(), ' ', '_');
James Feistb6baeaa2019-02-21 10:41:40 -0800746 escaped = "/" + escaped;
Ed Tanous002d39b2022-05-31 08:59:27 -0700747 auto it = std::find_if(managedObj.begin(), managedObj.end(),
748 [&escaped](const auto& obj) {
749 if (boost::algorithm::ends_with(obj.first.str, escaped))
750 {
751 BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
752 return true;
753 }
754 return false;
755 });
James Feistb6baeaa2019-02-21 10:41:40 -0800756
757 if (it == managedObj.end())
758 {
James Feist73df0db2019-03-25 15:29:35 -0700759 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800760 }
761 // 5 comes from <chassis-name> being the 5th element
762 // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
James Feist73df0db2019-03-25 15:29:35 -0700763 if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
764 {
765 return &(*it);
766 }
767
768 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800769}
770
Ed Tanous23a21a12020-07-25 04:45:05 +0000771inline CreatePIDRet createPidInterface(
zhanghch058d1b46d2021-04-01 11:18:24 +0800772 const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
Ed Tanousb5a76932020-09-29 16:16:58 -0700773 const nlohmann::json::iterator& it, const std::string& path,
James Feist83ff9ab2018-08-31 10:18:24 -0700774 const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800775 dbus::utility::DBusPropertiesMap& output, std::string& chassis,
776 const std::string& profile)
James Feist83ff9ab2018-08-31 10:18:24 -0700777{
James Feist5f2caae2018-12-12 14:08:25 -0800778 // common deleter
James Feistb6baeaa2019-02-21 10:41:40 -0800779 if (it.value() == nullptr)
James Feist5f2caae2018-12-12 14:08:25 -0800780 {
781 std::string iface;
782 if (type == "PidControllers" || type == "FanControllers")
783 {
784 iface = pidConfigurationIface;
785 }
786 else if (type == "FanZones")
787 {
788 iface = pidZoneConfigurationIface;
789 }
790 else if (type == "StepwiseControllers")
791 {
792 iface = stepwiseConfigurationIface;
793 }
794 else
795 {
Gunnar Millsa0744d32020-11-09 15:40:45 -0600796 BMCWEB_LOG_ERROR << "Illegal Type " << type;
James Feist5f2caae2018-12-12 14:08:25 -0800797 messages::propertyUnknown(response->res, type);
798 return CreatePIDRet::fail;
799 }
James Feist6ee7f772020-02-06 16:25:27 -0800800
801 BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
James Feist5f2caae2018-12-12 14:08:25 -0800802 // delete interface
803 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800804 [response, path](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700805 if (ec)
806 {
807 BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
808 messages::internalError(response->res);
809 return;
810 }
811 messages::success(response->res);
James Feist5f2caae2018-12-12 14:08:25 -0800812 },
813 "xyz.openbmc_project.EntityManager", path, iface, "Delete");
814 return CreatePIDRet::del;
815 }
816
Ed Tanous711ac7a2021-12-20 09:34:41 -0800817 const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800818 if (!createNewObject)
819 {
820 // if we aren't creating a new object, we should be able to find it on
821 // d-bus
James Feist73df0db2019-03-25 15:29:35 -0700822 managedItem = findChassis(managedObj, it.key(), chassis);
823 if (managedItem == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800824 {
825 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
Ed Tanousef4c65b2023-04-24 15:28:50 -0700826 messages::invalidObject(
827 response->res,
828 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800829 return CreatePIDRet::fail;
830 }
831 }
832
Ed Tanous26f69762022-01-25 09:49:11 -0800833 if (!profile.empty() &&
James Feist73df0db2019-03-25 15:29:35 -0700834 (type == "PidControllers" || type == "FanControllers" ||
835 type == "StepwiseControllers"))
836 {
837 if (managedItem == nullptr)
838 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800839 output.emplace_back("Profiles", std::vector<std::string>{profile});
James Feist73df0db2019-03-25 15:29:35 -0700840 }
841 else
842 {
843 std::string interface;
844 if (type == "StepwiseControllers")
845 {
846 interface = stepwiseConfigurationIface;
847 }
848 else
849 {
850 interface = pidConfigurationIface;
851 }
Ed Tanous711ac7a2021-12-20 09:34:41 -0800852 bool ifaceFound = false;
853 for (const auto& iface : managedItem->second)
854 {
855 if (iface.first == interface)
856 {
857 ifaceFound = true;
858 for (const auto& prop : iface.second)
859 {
860 if (prop.first == "Profiles")
861 {
862 const std::vector<std::string>* curProfiles =
863 std::get_if<std::vector<std::string>>(
864 &(prop.second));
865 if (curProfiles == nullptr)
866 {
867 BMCWEB_LOG_ERROR
868 << "Illegal profiles in managed object";
869 messages::internalError(response->res);
870 return CreatePIDRet::fail;
871 }
872 if (std::find(curProfiles->begin(),
873 curProfiles->end(),
874 profile) == curProfiles->end())
875 {
876 std::vector<std::string> newProfiles =
877 *curProfiles;
878 newProfiles.push_back(profile);
Ed Tanousb9d36b42022-02-26 21:42:46 -0800879 output.emplace_back("Profiles", newProfiles);
Ed Tanous711ac7a2021-12-20 09:34:41 -0800880 }
881 }
882 }
883 }
884 }
885
886 if (!ifaceFound)
James Feist73df0db2019-03-25 15:29:35 -0700887 {
888 BMCWEB_LOG_ERROR
889 << "Failed to find interface in managed object";
890 messages::internalError(response->res);
891 return CreatePIDRet::fail;
892 }
James Feist73df0db2019-03-25 15:29:35 -0700893 }
894 }
895
James Feist83ff9ab2018-08-31 10:18:24 -0700896 if (type == "PidControllers" || type == "FanControllers")
897 {
898 if (createNewObject)
899 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800900 output.emplace_back("Class",
901 type == "PidControllers" ? "temp" : "fan");
902 output.emplace_back("Type", "Pid");
James Feist83ff9ab2018-08-31 10:18:24 -0700903 }
James Feist5f2caae2018-12-12 14:08:25 -0800904
905 std::optional<std::vector<nlohmann::json>> zones;
906 std::optional<std::vector<std::string>> inputs;
907 std::optional<std::vector<std::string>> outputs;
908 std::map<std::string, std::optional<double>> doubles;
James Feistb943aae2019-07-11 16:33:56 -0700909 std::optional<std::string> setpointOffset;
James Feist5f2caae2018-12-12 14:08:25 -0800910 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -0800911 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
James Feist5f2caae2018-12-12 14:08:25 -0800912 "Zones", zones, "FFGainCoefficient",
913 doubles["FFGainCoefficient"], "FFOffCoefficient",
914 doubles["FFOffCoefficient"], "ICoefficient",
915 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
916 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
917 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
918 "PCoefficient", doubles["PCoefficient"], "SetPoint",
James Feistb943aae2019-07-11 16:33:56 -0700919 doubles["SetPoint"], "SetPointOffset", setpointOffset,
920 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
921 "PositiveHysteresis", doubles["PositiveHysteresis"],
922 "NegativeHysteresis", doubles["NegativeHysteresis"]))
James Feist83ff9ab2018-08-31 10:18:24 -0700923 {
James Feist5f2caae2018-12-12 14:08:25 -0800924 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700925 }
James Feist5f2caae2018-12-12 14:08:25 -0800926 if (zones)
James Feist83ff9ab2018-08-31 10:18:24 -0700927 {
James Feist5f2caae2018-12-12 14:08:25 -0800928 std::vector<std::string> zonesStr;
929 if (!getZonesFromJsonReq(response, *zones, zonesStr))
James Feist83ff9ab2018-08-31 10:18:24 -0700930 {
Gunnar Millsa0744d32020-11-09 15:40:45 -0600931 BMCWEB_LOG_ERROR << "Illegal Zones";
James Feist5f2caae2018-12-12 14:08:25 -0800932 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700933 }
James Feistb6baeaa2019-02-21 10:41:40 -0800934 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -0800935 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800936 {
937 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
Ed Tanousace85d62021-10-26 12:45:59 -0700938 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -0700939 response->res,
940 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800941 return CreatePIDRet::fail;
942 }
Ed Tanousb9d36b42022-02-26 21:42:46 -0800943 output.emplace_back("Zones", std::move(zonesStr));
James Feist5f2caae2018-12-12 14:08:25 -0800944 }
Ed Tanousafb9ee02022-12-21 11:59:17 -0800945
946 if (inputs)
James Feist5f2caae2018-12-12 14:08:25 -0800947 {
Ed Tanousafb9ee02022-12-21 11:59:17 -0800948 for (std::string& value : *inputs)
James Feist83ff9ab2018-08-31 10:18:24 -0700949 {
Ed Tanousafb9ee02022-12-21 11:59:17 -0800950 std::replace(value.begin(), value.end(), '_', ' ');
James Feist83ff9ab2018-08-31 10:18:24 -0700951 }
Ed Tanousafb9ee02022-12-21 11:59:17 -0800952 output.emplace_back("Inputs", *inputs);
953 }
954
955 if (outputs)
956 {
957 for (std::string& value : *outputs)
958 {
959 std::replace(value.begin(), value.end(), '_', ' ');
960 }
961 output.emplace_back("Outputs", *outputs);
James Feist5f2caae2018-12-12 14:08:25 -0800962 }
James Feist83ff9ab2018-08-31 10:18:24 -0700963
James Feistb943aae2019-07-11 16:33:56 -0700964 if (setpointOffset)
965 {
966 // translate between redfish and dbus names
967 if (*setpointOffset == "UpperThresholdNonCritical")
968 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800969 output.emplace_back("SetPointOffset", "WarningLow");
James Feistb943aae2019-07-11 16:33:56 -0700970 }
971 else if (*setpointOffset == "LowerThresholdNonCritical")
972 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800973 output.emplace_back("SetPointOffset", "WarningHigh");
James Feistb943aae2019-07-11 16:33:56 -0700974 }
975 else if (*setpointOffset == "LowerThresholdCritical")
976 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800977 output.emplace_back("SetPointOffset", "CriticalLow");
James Feistb943aae2019-07-11 16:33:56 -0700978 }
979 else if (*setpointOffset == "UpperThresholdCritical")
980 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800981 output.emplace_back("SetPointOffset", "CriticalHigh");
James Feistb943aae2019-07-11 16:33:56 -0700982 }
983 else
984 {
985 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
986 << *setpointOffset;
Ed Tanousace85d62021-10-26 12:45:59 -0700987 messages::propertyValueNotInList(response->res, it.key(),
988 "SetPointOffset");
James Feistb943aae2019-07-11 16:33:56 -0700989 return CreatePIDRet::fail;
990 }
991 }
992
James Feist5f2caae2018-12-12 14:08:25 -0800993 // doubles
994 for (const auto& pairs : doubles)
995 {
996 if (!pairs.second)
James Feist83ff9ab2018-08-31 10:18:24 -0700997 {
James Feist5f2caae2018-12-12 14:08:25 -0800998 continue;
James Feist83ff9ab2018-08-31 10:18:24 -0700999 }
James Feist5f2caae2018-12-12 14:08:25 -08001000 BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
Ed Tanousb9d36b42022-02-26 21:42:46 -08001001 output.emplace_back(pairs.first, *pairs.second);
James Feist83ff9ab2018-08-31 10:18:24 -07001002 }
1003 }
James Feist5f2caae2018-12-12 14:08:25 -08001004
James Feist83ff9ab2018-08-31 10:18:24 -07001005 else if (type == "FanZones")
1006 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001007 output.emplace_back("Type", "Pid.Zone");
James Feist83ff9ab2018-08-31 10:18:24 -07001008
James Feist5f2caae2018-12-12 14:08:25 -08001009 std::optional<nlohmann::json> chassisContainer;
1010 std::optional<double> failSafePercent;
James Feistd3ec07f2019-02-25 14:51:15 -08001011 std::optional<double> minThermalOutput;
James Feistb6baeaa2019-02-21 10:41:40 -08001012 if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
James Feist5f2caae2018-12-12 14:08:25 -08001013 chassisContainer, "FailSafePercent",
James Feistd3ec07f2019-02-25 14:51:15 -08001014 failSafePercent, "MinThermalOutput",
1015 minThermalOutput))
James Feist83ff9ab2018-08-31 10:18:24 -07001016 {
James Feist5f2caae2018-12-12 14:08:25 -08001017 return CreatePIDRet::fail;
1018 }
James Feist83ff9ab2018-08-31 10:18:24 -07001019
James Feist5f2caae2018-12-12 14:08:25 -08001020 if (chassisContainer)
1021 {
James Feist5f2caae2018-12-12 14:08:25 -08001022 std::string chassisId;
1023 if (!redfish::json_util::readJson(*chassisContainer, response->res,
1024 "@odata.id", chassisId))
James Feist83ff9ab2018-08-31 10:18:24 -07001025 {
James Feist83ff9ab2018-08-31 10:18:24 -07001026 return CreatePIDRet::fail;
1027 }
James Feist5f2caae2018-12-12 14:08:25 -08001028
AppaRao Puli717794d2019-10-18 22:54:53 +05301029 // /redfish/v1/chassis/chassis_name/
James Feist5f2caae2018-12-12 14:08:25 -08001030 if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
1031 {
1032 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
Ed Tanousace85d62021-10-26 12:45:59 -07001033 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001034 response->res,
1035 boost::urls::format("/redfish/v1/Chassis/{}", chassisId));
James Feist5f2caae2018-12-12 14:08:25 -08001036 return CreatePIDRet::fail;
1037 }
1038 }
James Feistd3ec07f2019-02-25 14:51:15 -08001039 if (minThermalOutput)
James Feist5f2caae2018-12-12 14:08:25 -08001040 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001041 output.emplace_back("MinThermalOutput", *minThermalOutput);
James Feist5f2caae2018-12-12 14:08:25 -08001042 }
1043 if (failSafePercent)
1044 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001045 output.emplace_back("FailSafePercent", *failSafePercent);
James Feist5f2caae2018-12-12 14:08:25 -08001046 }
1047 }
1048 else if (type == "StepwiseControllers")
1049 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001050 output.emplace_back("Type", "Stepwise");
James Feist5f2caae2018-12-12 14:08:25 -08001051
1052 std::optional<std::vector<nlohmann::json>> zones;
1053 std::optional<std::vector<nlohmann::json>> steps;
1054 std::optional<std::vector<std::string>> inputs;
1055 std::optional<double> positiveHysteresis;
1056 std::optional<double> negativeHysteresis;
James Feistc33a90e2019-03-01 10:17:44 -08001057 std::optional<std::string> direction; // upper clipping curve vs lower
James Feist5f2caae2018-12-12 14:08:25 -08001058 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -08001059 it.value(), response->res, "Zones", zones, "Steps", steps,
1060 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
James Feistc33a90e2019-03-01 10:17:44 -08001061 "NegativeHysteresis", negativeHysteresis, "Direction",
1062 direction))
James Feist5f2caae2018-12-12 14:08:25 -08001063 {
James Feist5f2caae2018-12-12 14:08:25 -08001064 return CreatePIDRet::fail;
1065 }
1066
1067 if (zones)
1068 {
James Feistb6baeaa2019-02-21 10:41:40 -08001069 std::vector<std::string> zonesStrs;
1070 if (!getZonesFromJsonReq(response, *zones, zonesStrs))
James Feist5f2caae2018-12-12 14:08:25 -08001071 {
Gunnar Millsa0744d32020-11-09 15:40:45 -06001072 BMCWEB_LOG_ERROR << "Illegal Zones";
James Feist5f2caae2018-12-12 14:08:25 -08001073 return CreatePIDRet::fail;
1074 }
James Feistb6baeaa2019-02-21 10:41:40 -08001075 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -08001076 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -08001077 {
1078 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
Ed Tanousace85d62021-10-26 12:45:59 -07001079 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001080 response->res,
1081 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -08001082 return CreatePIDRet::fail;
1083 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001084 output.emplace_back("Zones", std::move(zonesStrs));
James Feist5f2caae2018-12-12 14:08:25 -08001085 }
1086 if (steps)
1087 {
1088 std::vector<double> readings;
1089 std::vector<double> outputs;
1090 for (auto& step : *steps)
1091 {
Ed Tanous543f4402022-01-06 13:12:53 -08001092 double target = 0.0;
1093 double out = 0.0;
James Feist5f2caae2018-12-12 14:08:25 -08001094
1095 if (!redfish::json_util::readJson(step, response->res, "Target",
Ed Tanous23a21a12020-07-25 04:45:05 +00001096 target, "Output", out))
James Feist5f2caae2018-12-12 14:08:25 -08001097 {
James Feist5f2caae2018-12-12 14:08:25 -08001098 return CreatePIDRet::fail;
1099 }
1100 readings.emplace_back(target);
Ed Tanous23a21a12020-07-25 04:45:05 +00001101 outputs.emplace_back(out);
James Feist5f2caae2018-12-12 14:08:25 -08001102 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001103 output.emplace_back("Reading", std::move(readings));
1104 output.emplace_back("Output", std::move(outputs));
James Feist5f2caae2018-12-12 14:08:25 -08001105 }
1106 if (inputs)
1107 {
1108 for (std::string& value : *inputs)
1109 {
Ed Tanousa170f272022-06-30 21:53:27 -07001110 std::replace(value.begin(), value.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -08001111 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001112 output.emplace_back("Inputs", std::move(*inputs));
James Feist5f2caae2018-12-12 14:08:25 -08001113 }
1114 if (negativeHysteresis)
1115 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001116 output.emplace_back("NegativeHysteresis", *negativeHysteresis);
James Feist5f2caae2018-12-12 14:08:25 -08001117 }
1118 if (positiveHysteresis)
1119 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001120 output.emplace_back("PositiveHysteresis", *positiveHysteresis);
James Feist83ff9ab2018-08-31 10:18:24 -07001121 }
James Feistc33a90e2019-03-01 10:17:44 -08001122 if (direction)
1123 {
1124 constexpr const std::array<const char*, 2> allowedDirections = {
1125 "Ceiling", "Floor"};
1126 if (std::find(allowedDirections.begin(), allowedDirections.end(),
1127 *direction) == allowedDirections.end())
1128 {
1129 messages::propertyValueTypeError(response->res, "Direction",
1130 *direction);
1131 return CreatePIDRet::fail;
1132 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001133 output.emplace_back("Class", *direction);
James Feistc33a90e2019-03-01 10:17:44 -08001134 }
James Feist83ff9ab2018-08-31 10:18:24 -07001135 }
1136 else
1137 {
Gunnar Millsa0744d32020-11-09 15:40:45 -06001138 BMCWEB_LOG_ERROR << "Illegal Type " << type;
Jason M. Bills35a62c72018-10-09 12:45:45 -07001139 messages::propertyUnknown(response->res, type);
James Feist83ff9ab2018-08-31 10:18:24 -07001140 return CreatePIDRet::fail;
1141 }
1142 return CreatePIDRet::patch;
1143}
James Feist73df0db2019-03-25 15:29:35 -07001144struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
1145{
Ed Tanous6936afe2022-09-08 15:10:39 -07001146 struct CompletionValues
1147 {
1148 std::vector<std::string> supportedProfiles;
1149 std::string currentProfile;
1150 dbus::utility::MapperGetSubTreeResponse subtree;
1151 };
James Feist73df0db2019-03-25 15:29:35 -07001152
Ed Tanous4e23a442022-06-06 09:57:26 -07001153 explicit GetPIDValues(
1154 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
Ed Tanous23a21a12020-07-25 04:45:05 +00001155 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001156
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001157 {}
James Feist73df0db2019-03-25 15:29:35 -07001158
1159 void run()
1160 {
1161 std::shared_ptr<GetPIDValues> self = shared_from_this();
1162
1163 // get all configurations
George Liue99073f2022-12-09 11:06:16 +08001164 constexpr std::array<std::string_view, 4> interfaces = {
1165 pidConfigurationIface, pidZoneConfigurationIface,
1166 objectManagerIface, stepwiseConfigurationIface};
1167 dbus::utility::getSubTree(
1168 "/", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001169 [self](
George Liue99073f2022-12-09 11:06:16 +08001170 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001171 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001172 if (ec)
1173 {
1174 BMCWEB_LOG_ERROR << ec;
1175 messages::internalError(self->asyncResp->res);
1176 return;
1177 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001178 self->complete.subtree = subtreeLocal;
George Liue99073f2022-12-09 11:06:16 +08001179 });
James Feist73df0db2019-03-25 15:29:35 -07001180
1181 // at the same time get the selected profile
George Liue99073f2022-12-09 11:06:16 +08001182 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1183 thermalModeIface};
1184 dbus::utility::getSubTree(
1185 "/", 0, thermalModeIfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001186 [self](
George Liue99073f2022-12-09 11:06:16 +08001187 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001188 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001189 if (ec || subtreeLocal.empty())
1190 {
1191 return;
1192 }
1193 if (subtreeLocal[0].second.size() != 1)
1194 {
1195 // invalid mapper response, should never happen
1196 BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
1197 messages::internalError(self->asyncResp->res);
1198 return;
1199 }
1200
1201 const std::string& path = subtreeLocal[0].first;
1202 const std::string& owner = subtreeLocal[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001203
1204 sdbusplus::asio::getAllProperties(
1205 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous002d39b2022-05-31 08:59:27 -07001206 [path, owner,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001207 self](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001208 const dbus::utility::DBusPropertiesMap& resp) {
1209 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001210 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001211 BMCWEB_LOG_ERROR
1212 << "GetPIDValues: Can't get thermalModeIface " << path;
James Feist73df0db2019-03-25 15:29:35 -07001213 messages::internalError(self->asyncResp->res);
1214 return;
1215 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001216
Ed Tanous002d39b2022-05-31 08:59:27 -07001217 const std::string* current = nullptr;
1218 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001219
1220 const bool success = sdbusplus::unpackPropertiesNoThrow(
1221 dbus_utils::UnpackErrorPrinter(), resp, "Current", current,
1222 "Supported", supported);
1223
1224 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001225 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001226 messages::internalError(self->asyncResp->res);
1227 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001228 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001229
Ed Tanous002d39b2022-05-31 08:59:27 -07001230 if (current == nullptr || supported == nullptr)
1231 {
1232 BMCWEB_LOG_ERROR
1233 << "GetPIDValues: thermal mode iface invalid " << path;
1234 messages::internalError(self->asyncResp->res);
1235 return;
1236 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001237 self->complete.currentProfile = *current;
1238 self->complete.supportedProfiles = *supported;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001239 });
George Liue99073f2022-12-09 11:06:16 +08001240 });
James Feist73df0db2019-03-25 15:29:35 -07001241 }
1242
Ed Tanous6936afe2022-09-08 15:10:39 -07001243 static void
1244 processingComplete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1245 const CompletionValues& completion)
James Feist73df0db2019-03-25 15:29:35 -07001246 {
1247 if (asyncResp->res.result() != boost::beast::http::status::ok)
1248 {
1249 return;
1250 }
1251 // create map of <connection, path to objMgr>>
Ed Tanous6936afe2022-09-08 15:10:39 -07001252 boost::container::flat_map<
1253 std::string, std::string, std::less<>,
1254 std::vector<std::pair<std::string, std::string>>>
1255 objectMgrPaths;
1256 boost::container::flat_set<std::string, std::less<>,
1257 std::vector<std::string>>
1258 calledConnections;
1259 for (const auto& pathGroup : completion.subtree)
James Feist73df0db2019-03-25 15:29:35 -07001260 {
1261 for (const auto& connectionGroup : pathGroup.second)
1262 {
1263 auto findConnection =
1264 calledConnections.find(connectionGroup.first);
1265 if (findConnection != calledConnections.end())
1266 {
1267 break;
1268 }
1269 for (const std::string& interface : connectionGroup.second)
1270 {
1271 if (interface == objectManagerIface)
1272 {
1273 objectMgrPaths[connectionGroup.first] = pathGroup.first;
1274 }
1275 // this list is alphabetical, so we
1276 // should have found the objMgr by now
1277 if (interface == pidConfigurationIface ||
1278 interface == pidZoneConfigurationIface ||
1279 interface == stepwiseConfigurationIface)
1280 {
1281 auto findObjMgr =
1282 objectMgrPaths.find(connectionGroup.first);
1283 if (findObjMgr == objectMgrPaths.end())
1284 {
1285 BMCWEB_LOG_DEBUG << connectionGroup.first
1286 << "Has no Object Manager";
1287 continue;
1288 }
1289
1290 calledConnections.insert(connectionGroup.first);
1291
1292 asyncPopulatePid(findObjMgr->first, findObjMgr->second,
Ed Tanous6936afe2022-09-08 15:10:39 -07001293 completion.currentProfile,
1294 completion.supportedProfiles,
James Feist73df0db2019-03-25 15:29:35 -07001295 asyncResp);
1296 break;
1297 }
1298 }
1299 }
1300 }
1301 }
1302
Ed Tanous6936afe2022-09-08 15:10:39 -07001303 ~GetPIDValues()
1304 {
1305 boost::asio::post(crow::connections::systemBus->get_io_context(),
1306 std::bind_front(&processingComplete, asyncResp,
1307 std::move(complete)));
1308 }
1309
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001310 GetPIDValues(const GetPIDValues&) = delete;
1311 GetPIDValues(GetPIDValues&&) = delete;
1312 GetPIDValues& operator=(const GetPIDValues&) = delete;
1313 GetPIDValues& operator=(GetPIDValues&&) = delete;
1314
zhanghch058d1b46d2021-04-01 11:18:24 +08001315 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Ed Tanous6936afe2022-09-08 15:10:39 -07001316 CompletionValues complete;
James Feist73df0db2019-03-25 15:29:35 -07001317};
1318
1319struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
1320{
zhanghch058d1b46d2021-04-01 11:18:24 +08001321 SetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
James Feist73df0db2019-03-25 15:29:35 -07001322 nlohmann::json& data) :
Ed Tanous271584a2019-07-09 16:24:22 -07001323 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001324 {
James Feist73df0db2019-03-25 15:29:35 -07001325 std::optional<nlohmann::json> pidControllers;
1326 std::optional<nlohmann::json> fanControllers;
1327 std::optional<nlohmann::json> fanZones;
1328 std::optional<nlohmann::json> stepwiseControllers;
1329
1330 if (!redfish::json_util::readJson(
1331 data, asyncResp->res, "PidControllers", pidControllers,
1332 "FanControllers", fanControllers, "FanZones", fanZones,
1333 "StepwiseControllers", stepwiseControllers, "Profile", profile))
1334 {
James Feist73df0db2019-03-25 15:29:35 -07001335 return;
1336 }
1337 configuration.emplace_back("PidControllers", std::move(pidControllers));
1338 configuration.emplace_back("FanControllers", std::move(fanControllers));
1339 configuration.emplace_back("FanZones", std::move(fanZones));
1340 configuration.emplace_back("StepwiseControllers",
1341 std::move(stepwiseControllers));
1342 }
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001343
1344 SetPIDValues(const SetPIDValues&) = delete;
1345 SetPIDValues(SetPIDValues&&) = delete;
1346 SetPIDValues& operator=(const SetPIDValues&) = delete;
1347 SetPIDValues& operator=(SetPIDValues&&) = delete;
1348
James Feist73df0db2019-03-25 15:29:35 -07001349 void run()
1350 {
1351 if (asyncResp->res.result() != boost::beast::http::status::ok)
1352 {
1353 return;
1354 }
1355
1356 std::shared_ptr<SetPIDValues> self = shared_from_this();
1357
1358 // todo(james): might make sense to do a mapper call here if this
1359 // interface gets more traction
1360 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001361 [self](const boost::system::error_code& ec,
Ed Tanous914e2d52022-01-07 11:38:34 -08001362 const dbus::utility::ManagedObjectType& mObj) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001363 if (ec)
1364 {
1365 BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
1366 messages::internalError(self->asyncResp->res);
1367 return;
1368 }
1369 const std::array<const char*, 3> configurations = {
1370 pidConfigurationIface, pidZoneConfigurationIface,
1371 stepwiseConfigurationIface};
James Feiste69d9de2020-02-07 12:23:27 -08001372
Ed Tanous002d39b2022-05-31 08:59:27 -07001373 for (const auto& [path, object] : mObj)
1374 {
1375 for (const auto& [interface, _] : object)
James Feiste69d9de2020-02-07 12:23:27 -08001376 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001377 if (std::find(configurations.begin(), configurations.end(),
1378 interface) != configurations.end())
James Feiste69d9de2020-02-07 12:23:27 -08001379 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001380 self->objectCount++;
1381 break;
James Feiste69d9de2020-02-07 12:23:27 -08001382 }
James Feiste69d9de2020-02-07 12:23:27 -08001383 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001384 }
1385 self->managedObj = mObj;
James Feist73df0db2019-03-25 15:29:35 -07001386 },
Nan Zhouc106b672022-09-20 22:35:31 +00001387 "xyz.openbmc_project.EntityManager",
1388 "/xyz/openbmc_project/inventory", objectManagerIface,
James Feist73df0db2019-03-25 15:29:35 -07001389 "GetManagedObjects");
1390
1391 // at the same time get the profile information
George Liue99073f2022-12-09 11:06:16 +08001392 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1393 thermalModeIface};
1394 dbus::utility::getSubTree(
1395 "/", 0, thermalModeIfaces,
1396 [self](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001397 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001398 if (ec || subtree.empty())
1399 {
1400 return;
1401 }
1402 if (subtree[0].second.empty())
1403 {
1404 // invalid mapper response, should never happen
1405 BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
1406 messages::internalError(self->asyncResp->res);
1407 return;
1408 }
1409
1410 const std::string& path = subtree[0].first;
1411 const std::string& owner = subtree[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001412 sdbusplus::asio::getAllProperties(
1413 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001414 [self, path, owner](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001415 const dbus::utility::DBusPropertiesMap& r) {
1416 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001417 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001418 BMCWEB_LOG_ERROR
1419 << "SetPIDValues: Can't get thermalModeIface " << path;
James Feist73df0db2019-03-25 15:29:35 -07001420 messages::internalError(self->asyncResp->res);
1421 return;
1422 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001423 const std::string* current = nullptr;
1424 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001425
1426 const bool success = sdbusplus::unpackPropertiesNoThrow(
1427 dbus_utils::UnpackErrorPrinter(), r, "Current", current,
1428 "Supported", supported);
1429
1430 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001431 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001432 messages::internalError(self->asyncResp->res);
1433 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001434 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001435
Ed Tanous002d39b2022-05-31 08:59:27 -07001436 if (current == nullptr || supported == nullptr)
1437 {
1438 BMCWEB_LOG_ERROR
1439 << "SetPIDValues: thermal mode iface invalid " << path;
1440 messages::internalError(self->asyncResp->res);
1441 return;
1442 }
1443 self->currentProfile = *current;
1444 self->supportedProfiles = *supported;
1445 self->profileConnection = owner;
1446 self->profilePath = path;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001447 });
George Liue99073f2022-12-09 11:06:16 +08001448 });
James Feist73df0db2019-03-25 15:29:35 -07001449 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001450 void pidSetDone()
James Feist73df0db2019-03-25 15:29:35 -07001451 {
1452 if (asyncResp->res.result() != boost::beast::http::status::ok)
1453 {
1454 return;
1455 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001456 std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001457 if (profile)
1458 {
1459 if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
1460 *profile) == supportedProfiles.end())
1461 {
1462 messages::actionParameterUnknown(response->res, "Profile",
1463 *profile);
1464 return;
1465 }
1466 currentProfile = *profile;
1467 crow::connections::systemBus->async_method_call(
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 }
James Feist73df0db2019-03-25 15:29:35 -07001474 },
1475 profileConnection, profilePath,
1476 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
Ed Tanous168e20c2021-12-13 14:39:53 -08001477 "Current", dbus::utility::DbusVariantType(*profile));
James Feist73df0db2019-03-25 15:29:35 -07001478 }
1479
1480 for (auto& containerPair : configuration)
1481 {
1482 auto& container = containerPair.second;
1483 if (!container)
1484 {
1485 continue;
1486 }
James Feist6ee7f772020-02-06 16:25:27 -08001487 BMCWEB_LOG_DEBUG << *container;
1488
Ed Tanous02cad962022-06-30 16:50:15 -07001489 const std::string& type = containerPair.first;
James Feist73df0db2019-03-25 15:29:35 -07001490
1491 for (nlohmann::json::iterator it = container->begin();
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301492 it != container->end(); ++it)
James Feist73df0db2019-03-25 15:29:35 -07001493 {
1494 const auto& name = it.key();
Potin Laicddbf3d2023-02-14 14:28:58 +08001495 std::string dbusObjName = name;
1496 std::replace(dbusObjName.begin(), dbusObjName.end(), ' ', '_');
James Feist6ee7f772020-02-06 16:25:27 -08001497 BMCWEB_LOG_DEBUG << "looking for " << name;
1498
Patrick Williams89492a12023-05-10 07:51:34 -05001499 auto pathItr = std::find_if(managedObj.begin(),
1500 managedObj.end(),
1501 [&dbusObjName](const auto& obj) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001502 return boost::algorithm::ends_with(obj.first.str,
Potin Laicddbf3d2023-02-14 14:28:58 +08001503 "/" + dbusObjName);
Patrick Williams89492a12023-05-10 07:51:34 -05001504 });
Ed Tanousb9d36b42022-02-26 21:42:46 -08001505 dbus::utility::DBusPropertiesMap output;
James Feist73df0db2019-03-25 15:29:35 -07001506
1507 output.reserve(16); // The pid interface length
1508
1509 // determines if we're patching entity-manager or
1510 // creating a new object
1511 bool createNewObject = (pathItr == managedObj.end());
James Feist6ee7f772020-02-06 16:25:27 -08001512 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
1513
James Feist73df0db2019-03-25 15:29:35 -07001514 std::string iface;
Ed Tanousea2b6702022-03-07 16:48:38 -08001515 if (!createNewObject)
James Feist73df0db2019-03-25 15:29:35 -07001516 {
Potin Lai8be2b5b2022-11-22 13:27:16 +08001517 bool findInterface = false;
Ed Tanousea2b6702022-03-07 16:48:38 -08001518 for (const auto& interface : pathItr->second)
James Feist73df0db2019-03-25 15:29:35 -07001519 {
Ed Tanousea2b6702022-03-07 16:48:38 -08001520 if (interface.first == pidConfigurationIface)
1521 {
1522 if (type == "PidControllers" ||
1523 type == "FanControllers")
1524 {
1525 iface = pidConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001526 findInterface = true;
1527 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001528 }
1529 }
1530 else if (interface.first == pidZoneConfigurationIface)
1531 {
1532 if (type == "FanZones")
1533 {
1534 iface = pidConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001535 findInterface = true;
1536 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001537 }
1538 }
1539 else if (interface.first == stepwiseConfigurationIface)
1540 {
1541 if (type == "StepwiseControllers")
1542 {
1543 iface = stepwiseConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001544 findInterface = true;
1545 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001546 }
1547 }
James Feist73df0db2019-03-25 15:29:35 -07001548 }
Potin Lai8be2b5b2022-11-22 13:27:16 +08001549
1550 // create new object if interface not found
1551 if (!findInterface)
1552 {
1553 createNewObject = true;
1554 }
James Feist73df0db2019-03-25 15:29:35 -07001555 }
James Feist6ee7f772020-02-06 16:25:27 -08001556
1557 if (createNewObject && it.value() == nullptr)
1558 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -05001559 // can't delete a non-existent object
Ed Tanous1668ce62022-02-07 23:44:31 -08001560 messages::propertyValueNotInList(response->res,
1561 it.value().dump(), name);
James Feist6ee7f772020-02-06 16:25:27 -08001562 continue;
1563 }
1564
1565 std::string path;
1566 if (pathItr != managedObj.end())
1567 {
1568 path = pathItr->first.str;
1569 }
1570
James Feist73df0db2019-03-25 15:29:35 -07001571 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
James Feiste69d9de2020-02-07 12:23:27 -08001572
1573 // arbitrary limit to avoid attacks
1574 constexpr const size_t controllerLimit = 500;
James Feist14b0b8d2020-02-12 11:52:07 -08001575 if (createNewObject && objectCount >= controllerLimit)
James Feiste69d9de2020-02-07 12:23:27 -08001576 {
1577 messages::resourceExhaustion(response->res, type);
1578 continue;
1579 }
Ed Tanousa170f272022-06-30 21:53:27 -07001580 std::string escaped = name;
1581 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1582 output.emplace_back("Name", escaped);
James Feist73df0db2019-03-25 15:29:35 -07001583
1584 std::string chassis;
1585 CreatePIDRet ret = createPidInterface(
James Feist6ee7f772020-02-06 16:25:27 -08001586 response, type, it, path, managedObj, createNewObject,
1587 output, chassis, currentProfile);
James Feist73df0db2019-03-25 15:29:35 -07001588 if (ret == CreatePIDRet::fail)
1589 {
1590 return;
1591 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001592 if (ret == CreatePIDRet::del)
James Feist73df0db2019-03-25 15:29:35 -07001593 {
1594 continue;
1595 }
1596
1597 if (!createNewObject)
1598 {
1599 for (const auto& property : output)
1600 {
1601 crow::connections::systemBus->async_method_call(
1602 [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);
James Feist73df0db2019-03-25 15:29:35 -07001613 },
James Feist6ee7f772020-02-06 16:25:27 -08001614 "xyz.openbmc_project.EntityManager", path,
James Feist73df0db2019-03-25 15:29:35 -07001615 "org.freedesktop.DBus.Properties", "Set", iface,
1616 property.first, property.second);
1617 }
1618 }
1619 else
1620 {
1621 if (chassis.empty())
1622 {
1623 BMCWEB_LOG_ERROR << "Failed to get chassis from config";
Ed Tanousace85d62021-10-26 12:45:59 -07001624 messages::internalError(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001625 return;
1626 }
1627
1628 bool foundChassis = false;
1629 for (const auto& obj : managedObj)
1630 {
1631 if (boost::algorithm::ends_with(obj.first.str, chassis))
1632 {
1633 chassis = obj.first.str;
1634 foundChassis = true;
1635 break;
1636 }
1637 }
1638 if (!foundChassis)
1639 {
1640 BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
1641 messages::resourceMissingAtURI(
Ed Tanousace85d62021-10-26 12:45:59 -07001642 response->res,
Ed Tanousef4c65b2023-04-24 15:28:50 -07001643 boost::urls::format("/redfish/v1/Chassis/{}",
1644 chassis));
James Feist73df0db2019-03-25 15:29:35 -07001645 return;
1646 }
1647
1648 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001649 [response](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001650 if (ec)
1651 {
1652 BMCWEB_LOG_ERROR << "Error Adding Pid Object "
1653 << ec;
1654 messages::internalError(response->res);
1655 return;
1656 }
1657 messages::success(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001658 },
1659 "xyz.openbmc_project.EntityManager", chassis,
1660 "xyz.openbmc_project.AddObject", "AddObject", output);
1661 }
1662 }
1663 }
1664 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001665
1666 ~SetPIDValues()
1667 {
1668 try
1669 {
1670 pidSetDone();
1671 }
1672 catch (...)
1673 {
1674 BMCWEB_LOG_CRITICAL << "pidSetDone threw exception";
1675 }
1676 }
1677
zhanghch058d1b46d2021-04-01 11:18:24 +08001678 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001679 std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
1680 configuration;
1681 std::optional<std::string> profile;
1682 dbus::utility::ManagedObjectType managedObj;
1683 std::vector<std::string> supportedProfiles;
1684 std::string currentProfile;
1685 std::string profileConnection;
1686 std::string profilePath;
James Feist14b0b8d2020-02-12 11:52:07 -08001687 size_t objectCount = 0;
James Feist73df0db2019-03-25 15:29:35 -07001688};
James Feist83ff9ab2018-08-31 10:18:24 -07001689
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001690/**
1691 * @brief Retrieves BMC manager location data over DBus
1692 *
1693 * @param[in] aResp Shared pointer for completing asynchronous calls
1694 * @param[in] connectionName - service name
1695 * @param[in] path - object path
1696 * @return none
1697 */
zhanghch058d1b46d2021-04-01 11:18:24 +08001698inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001699 const std::string& connectionName,
1700 const std::string& path)
1701{
1702 BMCWEB_LOG_DEBUG << "Get BMC manager Location data.";
1703
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001704 sdbusplus::asio::getProperty<std::string>(
1705 *crow::connections::systemBus, connectionName, path,
1706 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001707 [aResp](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001708 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001709 if (ec)
1710 {
1711 BMCWEB_LOG_DEBUG << "DBUS response error for "
1712 "Location";
1713 messages::internalError(aResp->res);
1714 return;
1715 }
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001716
Ed Tanous002d39b2022-05-31 08:59:27 -07001717 aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
1718 property;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001719 });
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001720}
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001721// avoid name collision systems.hpp
1722inline void
1723 managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001724{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001725 BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time";
Ed Tanous52cc1122020-07-18 13:51:21 -07001726
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001727 sdbusplus::asio::getProperty<uint64_t>(
1728 *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
1729 "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
1730 "LastRebootTime",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001731 [aResp](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001732 const uint64_t lastResetTime) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001733 if (ec)
1734 {
1735 BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
1736 return;
1737 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001738
Ed Tanous002d39b2022-05-31 08:59:27 -07001739 // LastRebootTime is epoch time, in milliseconds
1740 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
1741 uint64_t lastResetTimeStamp = lastResetTime / 1000;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001742
Ed Tanous002d39b2022-05-31 08:59:27 -07001743 // Convert to ISO 8601 standard
1744 aResp->res.jsonValue["LastResetTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001745 redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001746 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001747}
1748
1749/**
1750 * @brief Set the running firmware image
1751 *
1752 * @param[i,o] aResp - Async response object
1753 * @param[i] runningFirmwareTarget - Image to make the running image
1754 *
1755 * @return void
1756 */
1757inline void
1758 setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1759 const std::string& runningFirmwareTarget)
1760{
1761 // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1762 std::string::size_type idPos = runningFirmwareTarget.rfind('/');
1763 if (idPos == std::string::npos)
1764 {
1765 messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
1766 "@odata.id");
1767 BMCWEB_LOG_DEBUG << "Can't parse firmware ID!";
1768 return;
1769 }
1770 idPos++;
1771 if (idPos >= runningFirmwareTarget.size())
1772 {
1773 messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
1774 "@odata.id");
1775 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
1776 return;
1777 }
1778 std::string firmwareId = runningFirmwareTarget.substr(idPos);
1779
1780 // Make sure the image is valid before setting priority
1781 crow::connections::systemBus->async_method_call(
Ed Tanous711ac7a2021-12-20 09:34:41 -08001782 [aResp, firmwareId,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001783 runningFirmwareTarget](const boost::system::error_code& ec,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001784 dbus::utility::ManagedObjectType& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001785 if (ec)
1786 {
1787 BMCWEB_LOG_DEBUG << "D-Bus response error getting objects.";
1788 messages::internalError(aResp->res);
1789 return;
1790 }
1791
1792 if (subtree.empty())
1793 {
1794 BMCWEB_LOG_DEBUG << "Can't find image!";
1795 messages::internalError(aResp->res);
1796 return;
1797 }
1798
1799 bool foundImage = false;
Ed Tanous02cad962022-06-30 16:50:15 -07001800 for (const auto& object : subtree)
Ed Tanous002d39b2022-05-31 08:59:27 -07001801 {
1802 const std::string& path =
1803 static_cast<const std::string&>(object.first);
1804 std::size_t idPos2 = path.rfind('/');
1805
1806 if (idPos2 == std::string::npos)
1807 {
1808 continue;
1809 }
1810
1811 idPos2++;
1812 if (idPos2 >= path.size())
1813 {
1814 continue;
1815 }
1816
1817 if (path.substr(idPos2) == firmwareId)
1818 {
1819 foundImage = true;
1820 break;
1821 }
1822 }
1823
1824 if (!foundImage)
1825 {
1826 messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
1827 "@odata.id");
1828 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
1829 return;
1830 }
1831
1832 BMCWEB_LOG_DEBUG << "Setting firmware version " << firmwareId
1833 << " to priority 0.";
1834
1835 // Only support Immediate
1836 // An addition could be a Redfish Setting like
1837 // ActiveSoftwareImageApplyTime and support OnReset
1838 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001839 [aResp](const boost::system::error_code& ec2) {
Ed Tanous8a592812022-06-04 09:06:59 -07001840 if (ec2)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001841 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001842 BMCWEB_LOG_DEBUG << "D-Bus response error setting.";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001843 messages::internalError(aResp->res);
1844 return;
1845 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001846 doBMCGracefulRestart(aResp);
1847 },
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001848
Ed Tanous002d39b2022-05-31 08:59:27 -07001849 "xyz.openbmc_project.Software.BMC.Updater",
1850 "/xyz/openbmc_project/software/" + firmwareId,
1851 "org.freedesktop.DBus.Properties", "Set",
1852 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1853 dbus::utility::DbusVariantType(static_cast<uint8_t>(0)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001854 },
1855 "xyz.openbmc_project.Software.BMC.Updater",
1856 "/xyz/openbmc_project/software", "org.freedesktop.DBus.ObjectManager",
1857 "GetManagedObjects");
1858}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001859
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001860inline void setDateTime(std::shared_ptr<bmcweb::AsyncResp> aResp,
1861 std::string datetime)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001862{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001863 BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001864
Ed Tanousc2e32002023-01-07 22:05:08 -08001865 std::optional<redfish::time_utils::usSinceEpoch> us =
1866 redfish::time_utils::dateStringToEpoch(datetime);
1867 if (!us)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001868 {
1869 messages::propertyValueFormatError(aResp->res, datetime, "DateTime");
1870 return;
1871 }
Ed Tanousc2e32002023-01-07 22:05:08 -08001872 crow::connections::systemBus->async_method_call(
1873 [aResp{std::move(aResp)},
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001874 datetime{std::move(datetime)}](const boost::system::error_code& ec) {
Ed Tanousc2e32002023-01-07 22:05:08 -08001875 if (ec)
1876 {
1877 BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1878 "DBUS response error "
1879 << ec;
1880 messages::internalError(aResp->res);
1881 return;
1882 }
1883 aResp->res.jsonValue["DateTime"] = datetime;
1884 },
1885 "xyz.openbmc_project.Time.Manager", "/xyz/openbmc_project/time/bmc",
1886 "org.freedesktop.DBus.Properties", "Set",
1887 "xyz.openbmc_project.Time.EpochTime", "Elapsed",
1888 dbus::utility::DbusVariantType(us->count()));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001889}
1890
Ed Tanous75815e52022-10-05 17:21:13 -07001891inline void
1892 checkForQuiesced(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1893{
1894 sdbusplus::asio::getProperty<std::string>(
1895 *crow::connections::systemBus, "org.freedesktop.systemd1",
1896 "/org/freedesktop/systemd1/unit/obmc-bmc-service-quiesce@0.target",
1897 "org.freedesktop.systemd1.Unit", "ActiveState",
1898 [asyncResp](const boost::system::error_code& ec,
1899 const std::string& val) {
1900 if (!ec)
1901 {
1902 if (val == "active")
1903 {
1904 asyncResp->res.jsonValue["Status"]["Health"] = "Critical";
1905 asyncResp->res.jsonValue["Status"]["State"] = "Quiesced";
1906 return;
1907 }
1908 }
1909 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
1910 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
1911 });
1912}
1913
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001914inline void requestRoutesManager(App& app)
1915{
1916 std::string uuid = persistent_data::getConfig().systemUuid;
1917
1918 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07001919 .privileges(redfish::privileges::getManager)
Ed Tanous002d39b2022-05-31 08:59:27 -07001920 .methods(boost::beast::http::verb::get)(
1921 [&app, uuid](const crow::Request& req,
Ed Tanous45ca1b82022-03-25 13:07:27 -07001922 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001923 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001924 {
1925 return;
1926 }
1927 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
Sui Chena51fc2d2022-07-14 17:21:53 -07001928 asyncResp->res.jsonValue["@odata.type"] = "#Manager.v1_14_0.Manager";
Ed Tanous002d39b2022-05-31 08:59:27 -07001929 asyncResp->res.jsonValue["Id"] = "bmc";
1930 asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
1931 asyncResp->res.jsonValue["Description"] =
1932 "Baseboard Management Controller";
1933 asyncResp->res.jsonValue["PowerState"] = "On";
Ed Tanous14766872022-03-15 10:44:42 -07001934
Ed Tanous002d39b2022-05-31 08:59:27 -07001935 asyncResp->res.jsonValue["ManagerType"] = "BMC";
1936 asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
1937 asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1938 asyncResp->res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001939
Ed Tanous002d39b2022-05-31 08:59:27 -07001940 asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
1941 "/redfish/v1/Managers/bmc/LogServices";
1942 asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
1943 "/redfish/v1/Managers/bmc/NetworkProtocol";
1944 asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
1945 "/redfish/v1/Managers/bmc/EthernetInterfaces";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001946
1947#ifdef BMCWEB_ENABLE_VM_NBDPROXY
Ed Tanous002d39b2022-05-31 08:59:27 -07001948 asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
1949 "/redfish/v1/Managers/bmc/VirtualMedia";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001950#endif // BMCWEB_ENABLE_VM_NBDPROXY
1951
Ed Tanous002d39b2022-05-31 08:59:27 -07001952 // default oem data
1953 nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
1954 nlohmann::json& oemOpenbmc = oem["OpenBmc"];
1955 oem["@odata.type"] = "#OemManager.Oem";
1956 oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
1957 oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
1958 oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
Ed Tanous14766872022-03-15 10:44:42 -07001959
Ed Tanous002d39b2022-05-31 08:59:27 -07001960 nlohmann::json::object_t certificates;
1961 certificates["@odata.id"] =
1962 "/redfish/v1/Managers/bmc/Truststore/Certificates";
1963 oemOpenbmc["Certificates"] = std::move(certificates);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001964
Ed Tanous002d39b2022-05-31 08:59:27 -07001965 // Manager.Reset (an action) can be many values, OpenBMC only
1966 // supports BMC reboot.
1967 nlohmann::json& managerReset =
1968 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
1969 managerReset["target"] =
1970 "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
1971 managerReset["@Redfish.ActionInfo"] =
1972 "/redfish/v1/Managers/bmc/ResetActionInfo";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001973
Ed Tanous002d39b2022-05-31 08:59:27 -07001974 // ResetToDefaults (Factory Reset) has values like
1975 // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
1976 // on OpenBMC
1977 nlohmann::json& resetToDefaults =
1978 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
1979 resetToDefaults["target"] =
1980 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
Ed Tanous613dabe2022-07-09 11:17:36 -07001981 resetToDefaults["ResetType@Redfish.AllowableValues"] =
1982 nlohmann::json::array_t({"ResetAll"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001983
Ed Tanous002d39b2022-05-31 08:59:27 -07001984 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001985 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301986
Ed Tanous002d39b2022-05-31 08:59:27 -07001987 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1988 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1989 redfishDateTimeOffset.second;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001990
Ed Tanous002d39b2022-05-31 08:59:27 -07001991 // TODO (Gunnar): Remove these one day since moved to ComputerSystem
1992 // Still used by OCP profiles
1993 // https://github.com/opencomputeproject/OCP-Profiles/issues/23
1994 // Fill in SerialConsole info
1995 asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
1996 asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
Ed Tanous613dabe2022-07-09 11:17:36 -07001997 asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
1998 nlohmann::json::array_t({"IPMI", "SSH"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001999#ifdef BMCWEB_ENABLE_KVM
Ed Tanous002d39b2022-05-31 08:59:27 -07002000 // Fill in GraphicalConsole info
2001 asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
2002 asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] =
2003 4;
Ed Tanous613dabe2022-07-09 11:17:36 -07002004 asyncResp->res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
2005 nlohmann::json::array_t({"KVMIP"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002006#endif // BMCWEB_ENABLE_KVM
2007
Ed Tanous002d39b2022-05-31 08:59:27 -07002008 asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
Ed Tanous14766872022-03-15 10:44:42 -07002009
Ed Tanous002d39b2022-05-31 08:59:27 -07002010 nlohmann::json::array_t managerForServers;
2011 nlohmann::json::object_t manager;
2012 manager["@odata.id"] = "/redfish/v1/Systems/system";
Patrick Williamsad539542023-05-12 10:10:08 -05002013 managerForServers.emplace_back(std::move(manager));
Ed Tanous002d39b2022-05-31 08:59:27 -07002014
2015 asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
2016 std::move(managerForServers);
2017
2018 auto health = std::make_shared<HealthPopulate>(asyncResp);
2019 health->isManagersHealth = true;
2020 health->populate();
2021
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