blob: 6e1427b344195876353d4d1c20a39d5446a803c1 [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>
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +020031#include <sdbusplus/asio/property.hpp>
32#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050033
Ed Tanousa170f272022-06-30 21:53:27 -070034#include <algorithm>
George Liue99073f2022-12-09 11:06:16 +080035#include <array>
Gunnar Mills4bfefa72020-07-30 13:54:29 -050036#include <cstdint>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050037#include <memory>
38#include <sstream>
George Liue99073f2022-12-09 11:06:16 +080039#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080040#include <variant>
James Feist5b4aa862018-08-16 14:07:01 -070041
Ed Tanous1abe55e2018-09-05 08:30:59 -070042namespace redfish
43{
Jennifer Leeed5befb2018-08-10 11:29:45 -070044
45/**
Gunnar Mills2a5c4402020-05-19 09:07:24 -050046 * Function reboots the BMC.
47 *
48 * @param[in] asyncResp - Shared pointer for completing asynchronous calls
Jennifer Leeed5befb2018-08-10 11:29:45 -070049 */
zhanghch058d1b46d2021-04-01 11:18:24 +080050inline void
51 doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Gunnar Mills2a5c4402020-05-19 09:07:24 -050052{
53 const char* processName = "xyz.openbmc_project.State.BMC";
54 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
55 const char* interfaceName = "xyz.openbmc_project.State.BMC";
56 const std::string& propertyValue =
57 "xyz.openbmc_project.State.BMC.Transition.Reboot";
58 const char* destProperty = "RequestedBMCTransition";
59
60 // Create the D-Bus variant for D-Bus call.
Ed Tanous168e20c2021-12-13 14:39:53 -080061 dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
Gunnar Mills2a5c4402020-05-19 09:07:24 -050062
63 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080064 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -070065 // Use "Set" method to set the property value.
66 if (ec)
67 {
68 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
69 messages::internalError(asyncResp->res);
70 return;
71 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -050072
Ed Tanous002d39b2022-05-31 08:59:27 -070073 messages::success(asyncResp->res);
Gunnar Mills2a5c4402020-05-19 09:07:24 -050074 },
75 processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
76 interfaceName, destProperty, dbusPropertyValue);
77}
78
zhanghch058d1b46d2021-04-01 11:18:24 +080079inline void
80 doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +000081{
82 const char* processName = "xyz.openbmc_project.State.BMC";
83 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
84 const char* interfaceName = "xyz.openbmc_project.State.BMC";
85 const std::string& propertyValue =
86 "xyz.openbmc_project.State.BMC.Transition.HardReboot";
87 const char* destProperty = "RequestedBMCTransition";
88
89 // Create the D-Bus variant for D-Bus call.
Ed Tanous168e20c2021-12-13 14:39:53 -080090 dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +000091
92 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080093 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -070094 // Use "Set" method to set the property value.
95 if (ec)
96 {
97 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
98 messages::internalError(asyncResp->res);
99 return;
100 }
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000101
Ed Tanous002d39b2022-05-31 08:59:27 -0700102 messages::success(asyncResp->res);
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000103 },
104 processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
105 interfaceName, destProperty, dbusPropertyValue);
106}
107
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500108/**
109 * ManagerResetAction class supports the POST method for the Reset (reboot)
110 * action.
111 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700112inline void requestRoutesManagerResetAction(App& app)
Jennifer Leeed5befb2018-08-10 11:29:45 -0700113{
Jennifer Leeed5befb2018-08-10 11:29:45 -0700114 /**
Jennifer Leeed5befb2018-08-10 11:29:45 -0700115 * Function handles POST method request.
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500116 * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000117 * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
Jennifer Leeed5befb2018-08-10 11:29:45 -0700118 */
Jennifer Leeed5befb2018-08-10 11:29:45 -0700119
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700120 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
Ed Tanoused398212021-06-09 17:05:54 -0700121 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700122 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700123 [&app](const crow::Request& req,
124 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000125 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700126 {
127 return;
128 }
129 BMCWEB_LOG_DEBUG << "Post Manager Reset.";
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500130
Ed Tanous002d39b2022-05-31 08:59:27 -0700131 std::string resetType;
Jennifer Leeed5befb2018-08-10 11:29:45 -0700132
Ed Tanous002d39b2022-05-31 08:59:27 -0700133 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
134 resetType))
135 {
136 return;
137 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500138
Ed Tanous002d39b2022-05-31 08:59:27 -0700139 if (resetType == "GracefulRestart")
140 {
141 BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
142 doBMCGracefulRestart(asyncResp);
143 return;
144 }
145 if (resetType == "ForceRestart")
146 {
147 BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
148 doBMCForceRestart(asyncResp);
149 return;
150 }
151 BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
152 << resetType;
153 messages::actionParameterNotSupported(asyncResp->res, resetType,
154 "ResetType");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700155
Ed Tanous002d39b2022-05-31 08:59:27 -0700156 return;
157 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700158}
Jennifer Leeed5befb2018-08-10 11:29:45 -0700159
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500160/**
161 * ManagerResetToDefaultsAction class supports POST method for factory reset
162 * action.
163 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700164inline void requestRoutesManagerResetToDefaultsAction(App& app)
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500165{
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;
263 allowableValues.push_back("GracefulRestart");
264 allowableValues.push_back("ForceRestart");
265 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;
268 parameters.push_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{
291
292 crow::connections::systemBus->async_method_call(
James Feist73df0db2019-03-25 15:29:35 -0700293 [asyncResp, currentProfile, supportedProfiles](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800294 const boost::system::error_code& ec,
James Feist73df0db2019-03-25 15:29:35 -0700295 const dbus::utility::ManagedObjectType& managedObj) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700296 if (ec)
297 {
298 BMCWEB_LOG_ERROR << ec;
299 asyncResp->res.jsonValue.clear();
300 messages::internalError(asyncResp->res);
301 return;
302 }
303 nlohmann::json& configRoot =
304 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
305 nlohmann::json& fans = configRoot["FanControllers"];
306 fans["@odata.type"] = "#OemManager.FanControllers";
307 fans["@odata.id"] =
308 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers";
309
310 nlohmann::json& pids = configRoot["PidControllers"];
311 pids["@odata.type"] = "#OemManager.PidControllers";
312 pids["@odata.id"] =
313 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
314
315 nlohmann::json& stepwise = configRoot["StepwiseControllers"];
316 stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
317 stepwise["@odata.id"] =
318 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
319
320 nlohmann::json& zones = configRoot["FanZones"];
321 zones["@odata.id"] =
322 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
323 zones["@odata.type"] = "#OemManager.FanZones";
324 configRoot["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
325 configRoot["@odata.type"] = "#OemManager.Fan";
326 configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
327
328 if (!currentProfile.empty())
329 {
330 configRoot["Profile"] = currentProfile;
331 }
332 BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !";
333
334 for (const auto& pathPair : managedObj)
335 {
336 for (const auto& intfPair : pathPair.second)
James Feist5b4aa862018-08-16 14:07:01 -0700337 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700338 if (intfPair.first != pidConfigurationIface &&
339 intfPair.first != pidZoneConfigurationIface &&
340 intfPair.first != stepwiseConfigurationIface)
James Feist5b4aa862018-08-16 14:07:01 -0700341 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700342 continue;
343 }
James Feist73df0db2019-03-25 15:29:35 -0700344
Ed Tanous002d39b2022-05-31 08:59:27 -0700345 std::string name;
James Feist73df0db2019-03-25 15:29:35 -0700346
Ed Tanous002d39b2022-05-31 08:59:27 -0700347 for (const std::pair<std::string,
348 dbus::utility::DbusVariantType>& propPair :
349 intfPair.second)
350 {
351 if (propPair.first == "Name")
James Feist73df0db2019-03-25 15:29:35 -0700352 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700353 const std::string* namePtr =
354 std::get_if<std::string>(&propPair.second);
355 if (namePtr == nullptr)
James Feist73df0db2019-03-25 15:29:35 -0700356 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700357 BMCWEB_LOG_ERROR << "Pid Name Field illegal";
James Feistc33a90e2019-03-01 10:17:44 -0800358 messages::internalError(asyncResp->res);
359 return;
360 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700361 name = *namePtr;
362 dbus::utility::escapePathForDbus(name);
James Feistb7a08d02018-12-11 14:55:37 -0800363 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700364 else if (propPair.first == "Profiles")
James Feistb7a08d02018-12-11 14:55:37 -0800365 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700366 const std::vector<std::string>* profiles =
367 std::get_if<std::vector<std::string>>(
368 &propPair.second);
369 if (profiles == nullptr)
James Feistb7a08d02018-12-11 14:55:37 -0800370 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700371 BMCWEB_LOG_ERROR << "Pid Profiles Field illegal";
James Feistb7a08d02018-12-11 14:55:37 -0800372 messages::internalError(asyncResp->res);
373 return;
374 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700375 if (std::find(profiles->begin(), profiles->end(),
376 currentProfile) == profiles->end())
James Feistb7a08d02018-12-11 14:55:37 -0800377 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700378 BMCWEB_LOG_INFO
379 << name << " not supported in current profile";
380 continue;
James Feistb7a08d02018-12-11 14:55:37 -0800381 }
382 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700383 }
384 nlohmann::json* config = nullptr;
385 const std::string* classPtr = nullptr;
386
387 for (const std::pair<std::string,
388 dbus::utility::DbusVariantType>& propPair :
389 intfPair.second)
390 {
391 if (propPair.first == "Class")
James Feistb7a08d02018-12-11 14:55:37 -0800392 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700393 classPtr = std::get_if<std::string>(&propPair.second);
394 }
395 }
396
Willy Tueddfc432022-09-26 16:46:38 +0000397 boost::urls::url url = crow::utility::urlFromPieces(
398 "redfish", "v1", "Managers", "bmc");
Ed Tanous002d39b2022-05-31 08:59:27 -0700399 if (intfPair.first == pidZoneConfigurationIface)
400 {
401 std::string chassis;
402 if (!dbus::utility::getNthStringFromPath(pathPair.first.str,
403 5, chassis))
404 {
405 chassis = "#IllegalValue";
406 }
407 nlohmann::json& zone = zones[name];
Willy Tueddfc432022-09-26 16:46:38 +0000408 zone["Chassis"]["@odata.id"] = crow::utility::urlFromPieces(
409 "redfish", "v1", "Chassis", chassis);
410 url.set_fragment(
411 ("/Oem/OpenBmc/Fan/FanZones"_json_pointer / name)
412 .to_string());
413 zone["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700414 zone["@odata.type"] = "#OemManager.FanZone";
415 config = &zone;
416 }
417
418 else if (intfPair.first == stepwiseConfigurationIface)
419 {
420 if (classPtr == nullptr)
421 {
422 BMCWEB_LOG_ERROR << "Pid Class Field illegal";
James Feistb7a08d02018-12-11 14:55:37 -0800423 messages::internalError(asyncResp->res);
424 return;
425 }
426
Ed Tanous002d39b2022-05-31 08:59:27 -0700427 nlohmann::json& controller = stepwise[name];
428 config = &controller;
Willy Tueddfc432022-09-26 16:46:38 +0000429 url.set_fragment(
430 ("/Oem/OpenBmc/Fan/StepwiseControllers"_json_pointer /
431 name)
432 .to_string());
433 controller["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700434 controller["@odata.type"] =
435 "#OemManager.StepwiseController";
436
437 controller["Direction"] = *classPtr;
438 }
439
440 // pid and fans are off the same configuration
441 else if (intfPair.first == pidConfigurationIface)
442 {
443
444 if (classPtr == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700445 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700446 BMCWEB_LOG_ERROR << "Pid Class Field illegal";
447 messages::internalError(asyncResp->res);
448 return;
449 }
450 bool isFan = *classPtr == "fan";
451 nlohmann::json& element = isFan ? fans[name] : pids[name];
452 config = &element;
453 if (isFan)
454 {
Willy Tueddfc432022-09-26 16:46:38 +0000455 url.set_fragment(
456 ("/Oem/OpenBmc/Fan/FanControllers"_json_pointer /
457 name)
458 .to_string());
459 element["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700460 element["@odata.type"] = "#OemManager.FanController";
461 }
462 else
463 {
Willy Tueddfc432022-09-26 16:46:38 +0000464 url.set_fragment(
465 ("/Oem/OpenBmc/Fan/PidControllers"_json_pointer /
466 name)
467 .to_string());
468 element["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700469 element["@odata.type"] = "#OemManager.PidController";
470 }
471 }
472 else
473 {
474 BMCWEB_LOG_ERROR << "Unexpected configuration";
475 messages::internalError(asyncResp->res);
476 return;
477 }
James Feist5b4aa862018-08-16 14:07:01 -0700478
Ed Tanous002d39b2022-05-31 08:59:27 -0700479 // used for making maps out of 2 vectors
480 const std::vector<double>* keys = nullptr;
481 const std::vector<double>* values = nullptr;
482
483 for (const auto& propertyPair : intfPair.second)
484 {
485 if (propertyPair.first == "Type" ||
486 propertyPair.first == "Class" ||
487 propertyPair.first == "Name")
488 {
489 continue;
490 }
491
492 // zones
493 if (intfPair.first == pidZoneConfigurationIface)
494 {
495 const double* ptr =
496 std::get_if<double>(&propertyPair.second);
497 if (ptr == nullptr)
498 {
499 BMCWEB_LOG_ERROR << "Field Illegal "
500 << propertyPair.first;
501 messages::internalError(asyncResp->res);
502 return;
503 }
504 (*config)[propertyPair.first] = *ptr;
505 }
506
507 if (intfPair.first == stepwiseConfigurationIface)
508 {
509 if (propertyPair.first == "Reading" ||
510 propertyPair.first == "Output")
511 {
512 const std::vector<double>* ptr =
513 std::get_if<std::vector<double>>(
514 &propertyPair.second);
515
516 if (ptr == nullptr)
517 {
518 BMCWEB_LOG_ERROR << "Field Illegal "
519 << propertyPair.first;
520 messages::internalError(asyncResp->res);
521 return;
522 }
523
524 if (propertyPair.first == "Reading")
525 {
526 keys = ptr;
527 }
528 else
529 {
530 values = ptr;
531 }
532 if (keys != nullptr && values != nullptr)
533 {
534 if (keys->size() != values->size())
535 {
536 BMCWEB_LOG_ERROR
537 << "Reading and Output size don't match ";
538 messages::internalError(asyncResp->res);
539 return;
540 }
541 nlohmann::json& steps = (*config)["Steps"];
542 steps = nlohmann::json::array();
543 for (size_t ii = 0; ii < keys->size(); ii++)
544 {
545 nlohmann::json::object_t step;
546 step["Target"] = (*keys)[ii];
547 step["Output"] = (*values)[ii];
548 steps.push_back(std::move(step));
549 }
550 }
551 }
552 if (propertyPair.first == "NegativeHysteresis" ||
553 propertyPair.first == "PositiveHysteresis")
James Feist5b4aa862018-08-16 14:07:01 -0700554 {
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800555 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800556 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700557 if (ptr == nullptr)
558 {
559 BMCWEB_LOG_ERROR << "Field Illegal "
560 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700561 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700562 return;
563 }
James Feistb7a08d02018-12-11 14:55:37 -0800564 (*config)[propertyPair.first] = *ptr;
565 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700566 }
James Feistb7a08d02018-12-11 14:55:37 -0800567
Ed Tanous002d39b2022-05-31 08:59:27 -0700568 // pid and fans are off the same configuration
569 if (intfPair.first == pidConfigurationIface ||
570 intfPair.first == stepwiseConfigurationIface)
571 {
572
573 if (propertyPair.first == "Zones")
James Feistb7a08d02018-12-11 14:55:37 -0800574 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700575 const std::vector<std::string>* inputs =
576 std::get_if<std::vector<std::string>>(
577 &propertyPair.second);
578
579 if (inputs == nullptr)
James Feistb7a08d02018-12-11 14:55:37 -0800580 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700581 BMCWEB_LOG_ERROR << "Zones Pid Field Illegal";
582 messages::internalError(asyncResp->res);
583 return;
James Feistb7a08d02018-12-11 14:55:37 -0800584 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700585 auto& data = (*config)[propertyPair.first];
586 data = nlohmann::json::array();
587 for (std::string itemCopy : *inputs)
James Feistb7a08d02018-12-11 14:55:37 -0800588 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700589 dbus::utility::escapePathForDbus(itemCopy);
590 nlohmann::json::object_t input;
Willy Tueddfc432022-09-26 16:46:38 +0000591 boost::urls::url managerUrl =
592 crow::utility::urlFromPieces(
593 "redfish", "v1", "Managers", "bmc");
594 managerUrl.set_fragment(
595 ("/Oem/OpenBmc/Fan/FanZones"_json_pointer /
596 itemCopy)
597 .to_string());
598 input["@odata.id"] = std::move(managerUrl);
Ed Tanous002d39b2022-05-31 08:59:27 -0700599 data.push_back(std::move(input));
James Feistb7a08d02018-12-11 14:55:37 -0800600 }
James Feist5b4aa862018-08-16 14:07:01 -0700601 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700602 // todo(james): may never happen, but this
603 // assumes configuration data referenced in the
604 // PID config is provided by the same daemon, we
605 // could add another loop to cover all cases,
606 // but I'm okay kicking this can down the road a
607 // bit
James Feist5b4aa862018-08-16 14:07:01 -0700608
Ed Tanous002d39b2022-05-31 08:59:27 -0700609 else if (propertyPair.first == "Inputs" ||
610 propertyPair.first == "Outputs")
James Feist5b4aa862018-08-16 14:07:01 -0700611 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700612 auto& data = (*config)[propertyPair.first];
613 const std::vector<std::string>* inputs =
614 std::get_if<std::vector<std::string>>(
615 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700616
Ed Tanous002d39b2022-05-31 08:59:27 -0700617 if (inputs == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700618 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700619 BMCWEB_LOG_ERROR << "Field Illegal "
620 << propertyPair.first;
621 messages::internalError(asyncResp->res);
622 return;
James Feist5b4aa862018-08-16 14:07:01 -0700623 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700624 data = *inputs;
625 }
626 else if (propertyPair.first == "SetPointOffset")
627 {
628 const std::string* ptr =
629 std::get_if<std::string>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700630
Ed Tanous002d39b2022-05-31 08:59:27 -0700631 if (ptr == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700632 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700633 BMCWEB_LOG_ERROR << "Field Illegal "
634 << propertyPair.first;
635 messages::internalError(asyncResp->res);
636 return;
James Feistb943aae2019-07-11 16:33:56 -0700637 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700638 // translate from dbus to redfish
639 if (*ptr == "WarningHigh")
James Feistb943aae2019-07-11 16:33:56 -0700640 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700641 (*config)["SetPointOffset"] =
642 "UpperThresholdNonCritical";
James Feistb943aae2019-07-11 16:33:56 -0700643 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700644 else if (*ptr == "WarningLow")
James Feist5b4aa862018-08-16 14:07:01 -0700645 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700646 (*config)["SetPointOffset"] =
647 "LowerThresholdNonCritical";
James Feist5b4aa862018-08-16 14:07:01 -0700648 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700649 else if (*ptr == "CriticalHigh")
650 {
651 (*config)["SetPointOffset"] =
652 "UpperThresholdCritical";
653 }
654 else if (*ptr == "CriticalLow")
655 {
656 (*config)["SetPointOffset"] =
657 "LowerThresholdCritical";
658 }
659 else
660 {
661 BMCWEB_LOG_ERROR << "Value Illegal " << *ptr;
662 messages::internalError(asyncResp->res);
663 return;
664 }
665 }
666 // doubles
667 else if (propertyPair.first == "FFGainCoefficient" ||
668 propertyPair.first == "FFOffCoefficient" ||
669 propertyPair.first == "ICoefficient" ||
670 propertyPair.first == "ILimitMax" ||
671 propertyPair.first == "ILimitMin" ||
672 propertyPair.first == "PositiveHysteresis" ||
673 propertyPair.first == "NegativeHysteresis" ||
674 propertyPair.first == "OutLimitMax" ||
675 propertyPair.first == "OutLimitMin" ||
676 propertyPair.first == "PCoefficient" ||
677 propertyPair.first == "SetPoint" ||
678 propertyPair.first == "SlewNeg" ||
679 propertyPair.first == "SlewPos")
680 {
681 const double* ptr =
682 std::get_if<double>(&propertyPair.second);
683 if (ptr == nullptr)
684 {
685 BMCWEB_LOG_ERROR << "Field Illegal "
686 << propertyPair.first;
687 messages::internalError(asyncResp->res);
688 return;
689 }
690 (*config)[propertyPair.first] = *ptr;
James Feist5b4aa862018-08-16 14:07:01 -0700691 }
692 }
693 }
694 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700695 }
James Feist5b4aa862018-08-16 14:07:01 -0700696 },
697 connection, path, objectManagerIface, "GetManagedObjects");
698}
Jennifer Leeca537922018-08-10 10:07:30 -0700699
James Feist83ff9ab2018-08-31 10:18:24 -0700700enum class CreatePIDRet
701{
702 fail,
703 del,
704 patch
705};
706
zhanghch058d1b46d2021-04-01 11:18:24 +0800707inline bool
708 getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
709 std::vector<nlohmann::json>& config,
710 std::vector<std::string>& zones)
James Feist5f2caae2018-12-12 14:08:25 -0800711{
James Feistb6baeaa2019-02-21 10:41:40 -0800712 if (config.empty())
713 {
714 BMCWEB_LOG_ERROR << "Empty Zones";
Ed Tanous1668ce62022-02-07 23:44:31 -0800715 messages::propertyValueFormatError(response->res, "[]", "Zones");
James Feistb6baeaa2019-02-21 10:41:40 -0800716 return false;
717 }
James Feist5f2caae2018-12-12 14:08:25 -0800718 for (auto& odata : config)
719 {
720 std::string path;
721 if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
722 path))
723 {
724 return false;
725 }
726 std::string input;
James Feist61adbda2019-03-25 13:03:51 -0700727
728 // 8 below comes from
729 // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
730 // 0 1 2 3 4 5 6 7 8
731 if (!dbus::utility::getNthStringFromPath(path, 8, input))
James Feist5f2caae2018-12-12 14:08:25 -0800732 {
733 BMCWEB_LOG_ERROR << "Got invalid path " << path;
734 BMCWEB_LOG_ERROR << "Illegal Type Zones";
735 messages::propertyValueFormatError(response->res, odata.dump(),
736 "Zones");
737 return false;
738 }
Ed Tanousa170f272022-06-30 21:53:27 -0700739 std::replace(input.begin(), input.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -0800740 zones.emplace_back(std::move(input));
741 }
742 return true;
743}
744
Ed Tanous711ac7a2021-12-20 09:34:41 -0800745inline const dbus::utility::ManagedObjectType::value_type*
James Feist73df0db2019-03-25 15:29:35 -0700746 findChassis(const dbus::utility::ManagedObjectType& managedObj,
747 const std::string& value, std::string& chassis)
James Feistb6baeaa2019-02-21 10:41:40 -0800748{
749 BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
750
Ed Tanousa170f272022-06-30 21:53:27 -0700751 std::string escaped = value;
Yaswanth Reddy M6ce82fa2023-03-10 07:29:45 +0000752 std::replace(escaped.begin(), escaped.end(), ' ', '_');
James Feistb6baeaa2019-02-21 10:41:40 -0800753 escaped = "/" + escaped;
Ed Tanous002d39b2022-05-31 08:59:27 -0700754 auto it = std::find_if(managedObj.begin(), managedObj.end(),
755 [&escaped](const auto& obj) {
756 if (boost::algorithm::ends_with(obj.first.str, escaped))
757 {
758 BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
759 return true;
760 }
761 return false;
762 });
James Feistb6baeaa2019-02-21 10:41:40 -0800763
764 if (it == managedObj.end())
765 {
James Feist73df0db2019-03-25 15:29:35 -0700766 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800767 }
768 // 5 comes from <chassis-name> being the 5th element
769 // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
James Feist73df0db2019-03-25 15:29:35 -0700770 if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
771 {
772 return &(*it);
773 }
774
775 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800776}
777
Ed Tanous23a21a12020-07-25 04:45:05 +0000778inline CreatePIDRet createPidInterface(
zhanghch058d1b46d2021-04-01 11:18:24 +0800779 const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
Ed Tanousb5a76932020-09-29 16:16:58 -0700780 const nlohmann::json::iterator& it, const std::string& path,
James Feist83ff9ab2018-08-31 10:18:24 -0700781 const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800782 dbus::utility::DBusPropertiesMap& output, std::string& chassis,
783 const std::string& profile)
James Feist83ff9ab2018-08-31 10:18:24 -0700784{
785
James Feist5f2caae2018-12-12 14:08:25 -0800786 // common deleter
James Feistb6baeaa2019-02-21 10:41:40 -0800787 if (it.value() == nullptr)
James Feist5f2caae2018-12-12 14:08:25 -0800788 {
789 std::string iface;
790 if (type == "PidControllers" || type == "FanControllers")
791 {
792 iface = pidConfigurationIface;
793 }
794 else if (type == "FanZones")
795 {
796 iface = pidZoneConfigurationIface;
797 }
798 else if (type == "StepwiseControllers")
799 {
800 iface = stepwiseConfigurationIface;
801 }
802 else
803 {
Gunnar Millsa0744d32020-11-09 15:40:45 -0600804 BMCWEB_LOG_ERROR << "Illegal Type " << type;
James Feist5f2caae2018-12-12 14:08:25 -0800805 messages::propertyUnknown(response->res, type);
806 return CreatePIDRet::fail;
807 }
James Feist6ee7f772020-02-06 16:25:27 -0800808
809 BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
James Feist5f2caae2018-12-12 14:08:25 -0800810 // delete interface
811 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800812 [response, path](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700813 if (ec)
814 {
815 BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
816 messages::internalError(response->res);
817 return;
818 }
819 messages::success(response->res);
James Feist5f2caae2018-12-12 14:08:25 -0800820 },
821 "xyz.openbmc_project.EntityManager", path, iface, "Delete");
822 return CreatePIDRet::del;
823 }
824
Ed Tanous711ac7a2021-12-20 09:34:41 -0800825 const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800826 if (!createNewObject)
827 {
828 // if we aren't creating a new object, we should be able to find it on
829 // d-bus
James Feist73df0db2019-03-25 15:29:35 -0700830 managedItem = findChassis(managedObj, it.key(), chassis);
831 if (managedItem == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800832 {
833 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
Ed Tanousace85d62021-10-26 12:45:59 -0700834 messages::invalidObject(response->res,
835 crow::utility::urlFromPieces(
836 "redfish", "v1", "Chassis", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800837 return CreatePIDRet::fail;
838 }
839 }
840
Ed Tanous26f69762022-01-25 09:49:11 -0800841 if (!profile.empty() &&
James Feist73df0db2019-03-25 15:29:35 -0700842 (type == "PidControllers" || type == "FanControllers" ||
843 type == "StepwiseControllers"))
844 {
845 if (managedItem == nullptr)
846 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800847 output.emplace_back("Profiles", std::vector<std::string>{profile});
James Feist73df0db2019-03-25 15:29:35 -0700848 }
849 else
850 {
851 std::string interface;
852 if (type == "StepwiseControllers")
853 {
854 interface = stepwiseConfigurationIface;
855 }
856 else
857 {
858 interface = pidConfigurationIface;
859 }
Ed Tanous711ac7a2021-12-20 09:34:41 -0800860 bool ifaceFound = false;
861 for (const auto& iface : managedItem->second)
862 {
863 if (iface.first == interface)
864 {
865 ifaceFound = true;
866 for (const auto& prop : iface.second)
867 {
868 if (prop.first == "Profiles")
869 {
870 const std::vector<std::string>* curProfiles =
871 std::get_if<std::vector<std::string>>(
872 &(prop.second));
873 if (curProfiles == nullptr)
874 {
875 BMCWEB_LOG_ERROR
876 << "Illegal profiles in managed object";
877 messages::internalError(response->res);
878 return CreatePIDRet::fail;
879 }
880 if (std::find(curProfiles->begin(),
881 curProfiles->end(),
882 profile) == curProfiles->end())
883 {
884 std::vector<std::string> newProfiles =
885 *curProfiles;
886 newProfiles.push_back(profile);
Ed Tanousb9d36b42022-02-26 21:42:46 -0800887 output.emplace_back("Profiles", newProfiles);
Ed Tanous711ac7a2021-12-20 09:34:41 -0800888 }
889 }
890 }
891 }
892 }
893
894 if (!ifaceFound)
James Feist73df0db2019-03-25 15:29:35 -0700895 {
896 BMCWEB_LOG_ERROR
897 << "Failed to find interface in managed object";
898 messages::internalError(response->res);
899 return CreatePIDRet::fail;
900 }
James Feist73df0db2019-03-25 15:29:35 -0700901 }
902 }
903
James Feist83ff9ab2018-08-31 10:18:24 -0700904 if (type == "PidControllers" || type == "FanControllers")
905 {
906 if (createNewObject)
907 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800908 output.emplace_back("Class",
909 type == "PidControllers" ? "temp" : "fan");
910 output.emplace_back("Type", "Pid");
James Feist83ff9ab2018-08-31 10:18:24 -0700911 }
James Feist5f2caae2018-12-12 14:08:25 -0800912
913 std::optional<std::vector<nlohmann::json>> zones;
914 std::optional<std::vector<std::string>> inputs;
915 std::optional<std::vector<std::string>> outputs;
916 std::map<std::string, std::optional<double>> doubles;
James Feistb943aae2019-07-11 16:33:56 -0700917 std::optional<std::string> setpointOffset;
James Feist5f2caae2018-12-12 14:08:25 -0800918 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -0800919 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
James Feist5f2caae2018-12-12 14:08:25 -0800920 "Zones", zones, "FFGainCoefficient",
921 doubles["FFGainCoefficient"], "FFOffCoefficient",
922 doubles["FFOffCoefficient"], "ICoefficient",
923 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
924 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
925 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
926 "PCoefficient", doubles["PCoefficient"], "SetPoint",
James Feistb943aae2019-07-11 16:33:56 -0700927 doubles["SetPoint"], "SetPointOffset", setpointOffset,
928 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
929 "PositiveHysteresis", doubles["PositiveHysteresis"],
930 "NegativeHysteresis", doubles["NegativeHysteresis"]))
James Feist83ff9ab2018-08-31 10:18:24 -0700931 {
James Feist5f2caae2018-12-12 14:08:25 -0800932 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700933 }
James Feist5f2caae2018-12-12 14:08:25 -0800934 if (zones)
James Feist83ff9ab2018-08-31 10:18:24 -0700935 {
James Feist5f2caae2018-12-12 14:08:25 -0800936 std::vector<std::string> zonesStr;
937 if (!getZonesFromJsonReq(response, *zones, zonesStr))
James Feist83ff9ab2018-08-31 10:18:24 -0700938 {
Gunnar Millsa0744d32020-11-09 15:40:45 -0600939 BMCWEB_LOG_ERROR << "Illegal Zones";
James Feist5f2caae2018-12-12 14:08:25 -0800940 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700941 }
James Feistb6baeaa2019-02-21 10:41:40 -0800942 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -0800943 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800944 {
945 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
Ed Tanousace85d62021-10-26 12:45:59 -0700946 messages::invalidObject(
947 response->res, crow::utility::urlFromPieces(
948 "redfish", "v1", "Chassis", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800949 return CreatePIDRet::fail;
950 }
Ed Tanousb9d36b42022-02-26 21:42:46 -0800951 output.emplace_back("Zones", std::move(zonesStr));
James Feist5f2caae2018-12-12 14:08:25 -0800952 }
Ed Tanousafb9ee02022-12-21 11:59:17 -0800953
954 if (inputs)
James Feist5f2caae2018-12-12 14:08:25 -0800955 {
Ed Tanousafb9ee02022-12-21 11:59:17 -0800956 for (std::string& value : *inputs)
James Feist83ff9ab2018-08-31 10:18:24 -0700957 {
Ed Tanousafb9ee02022-12-21 11:59:17 -0800958 std::replace(value.begin(), value.end(), '_', ' ');
James Feist83ff9ab2018-08-31 10:18:24 -0700959 }
Ed Tanousafb9ee02022-12-21 11:59:17 -0800960 output.emplace_back("Inputs", *inputs);
961 }
962
963 if (outputs)
964 {
965 for (std::string& value : *outputs)
966 {
967 std::replace(value.begin(), value.end(), '_', ' ');
968 }
969 output.emplace_back("Outputs", *outputs);
James Feist5f2caae2018-12-12 14:08:25 -0800970 }
James Feist83ff9ab2018-08-31 10:18:24 -0700971
James Feistb943aae2019-07-11 16:33:56 -0700972 if (setpointOffset)
973 {
974 // translate between redfish and dbus names
975 if (*setpointOffset == "UpperThresholdNonCritical")
976 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800977 output.emplace_back("SetPointOffset", "WarningLow");
James Feistb943aae2019-07-11 16:33:56 -0700978 }
979 else if (*setpointOffset == "LowerThresholdNonCritical")
980 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800981 output.emplace_back("SetPointOffset", "WarningHigh");
James Feistb943aae2019-07-11 16:33:56 -0700982 }
983 else if (*setpointOffset == "LowerThresholdCritical")
984 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800985 output.emplace_back("SetPointOffset", "CriticalLow");
James Feistb943aae2019-07-11 16:33:56 -0700986 }
987 else if (*setpointOffset == "UpperThresholdCritical")
988 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800989 output.emplace_back("SetPointOffset", "CriticalHigh");
James Feistb943aae2019-07-11 16:33:56 -0700990 }
991 else
992 {
993 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
994 << *setpointOffset;
Ed Tanousace85d62021-10-26 12:45:59 -0700995 messages::propertyValueNotInList(response->res, it.key(),
996 "SetPointOffset");
James Feistb943aae2019-07-11 16:33:56 -0700997 return CreatePIDRet::fail;
998 }
999 }
1000
James Feist5f2caae2018-12-12 14:08:25 -08001001 // doubles
1002 for (const auto& pairs : doubles)
1003 {
1004 if (!pairs.second)
James Feist83ff9ab2018-08-31 10:18:24 -07001005 {
James Feist5f2caae2018-12-12 14:08:25 -08001006 continue;
James Feist83ff9ab2018-08-31 10:18:24 -07001007 }
James Feist5f2caae2018-12-12 14:08:25 -08001008 BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
Ed Tanousb9d36b42022-02-26 21:42:46 -08001009 output.emplace_back(pairs.first, *pairs.second);
James Feist83ff9ab2018-08-31 10:18:24 -07001010 }
1011 }
James Feist5f2caae2018-12-12 14:08:25 -08001012
James Feist83ff9ab2018-08-31 10:18:24 -07001013 else if (type == "FanZones")
1014 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001015 output.emplace_back("Type", "Pid.Zone");
James Feist83ff9ab2018-08-31 10:18:24 -07001016
James Feist5f2caae2018-12-12 14:08:25 -08001017 std::optional<nlohmann::json> chassisContainer;
1018 std::optional<double> failSafePercent;
James Feistd3ec07f2019-02-25 14:51:15 -08001019 std::optional<double> minThermalOutput;
James Feistb6baeaa2019-02-21 10:41:40 -08001020 if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
James Feist5f2caae2018-12-12 14:08:25 -08001021 chassisContainer, "FailSafePercent",
James Feistd3ec07f2019-02-25 14:51:15 -08001022 failSafePercent, "MinThermalOutput",
1023 minThermalOutput))
James Feist83ff9ab2018-08-31 10:18:24 -07001024 {
James Feist5f2caae2018-12-12 14:08:25 -08001025 return CreatePIDRet::fail;
1026 }
James Feist83ff9ab2018-08-31 10:18:24 -07001027
James Feist5f2caae2018-12-12 14:08:25 -08001028 if (chassisContainer)
1029 {
1030
1031 std::string chassisId;
1032 if (!redfish::json_util::readJson(*chassisContainer, response->res,
1033 "@odata.id", chassisId))
James Feist83ff9ab2018-08-31 10:18:24 -07001034 {
James Feist83ff9ab2018-08-31 10:18:24 -07001035 return CreatePIDRet::fail;
1036 }
James Feist5f2caae2018-12-12 14:08:25 -08001037
AppaRao Puli717794d2019-10-18 22:54:53 +05301038 // /redfish/v1/chassis/chassis_name/
James Feist5f2caae2018-12-12 14:08:25 -08001039 if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
1040 {
1041 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
Ed Tanousace85d62021-10-26 12:45:59 -07001042 messages::invalidObject(
1043 response->res, crow::utility::urlFromPieces(
1044 "redfish", "v1", "Chassis", chassisId));
James Feist5f2caae2018-12-12 14:08:25 -08001045 return CreatePIDRet::fail;
1046 }
1047 }
James Feistd3ec07f2019-02-25 14:51:15 -08001048 if (minThermalOutput)
James Feist5f2caae2018-12-12 14:08:25 -08001049 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001050 output.emplace_back("MinThermalOutput", *minThermalOutput);
James Feist5f2caae2018-12-12 14:08:25 -08001051 }
1052 if (failSafePercent)
1053 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001054 output.emplace_back("FailSafePercent", *failSafePercent);
James Feist5f2caae2018-12-12 14:08:25 -08001055 }
1056 }
1057 else if (type == "StepwiseControllers")
1058 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001059 output.emplace_back("Type", "Stepwise");
James Feist5f2caae2018-12-12 14:08:25 -08001060
1061 std::optional<std::vector<nlohmann::json>> zones;
1062 std::optional<std::vector<nlohmann::json>> steps;
1063 std::optional<std::vector<std::string>> inputs;
1064 std::optional<double> positiveHysteresis;
1065 std::optional<double> negativeHysteresis;
James Feistc33a90e2019-03-01 10:17:44 -08001066 std::optional<std::string> direction; // upper clipping curve vs lower
James Feist5f2caae2018-12-12 14:08:25 -08001067 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -08001068 it.value(), response->res, "Zones", zones, "Steps", steps,
1069 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
James Feistc33a90e2019-03-01 10:17:44 -08001070 "NegativeHysteresis", negativeHysteresis, "Direction",
1071 direction))
James Feist5f2caae2018-12-12 14:08:25 -08001072 {
James Feist5f2caae2018-12-12 14:08:25 -08001073 return CreatePIDRet::fail;
1074 }
1075
1076 if (zones)
1077 {
James Feistb6baeaa2019-02-21 10:41:40 -08001078 std::vector<std::string> zonesStrs;
1079 if (!getZonesFromJsonReq(response, *zones, zonesStrs))
James Feist5f2caae2018-12-12 14:08:25 -08001080 {
Gunnar Millsa0744d32020-11-09 15:40:45 -06001081 BMCWEB_LOG_ERROR << "Illegal Zones";
James Feist5f2caae2018-12-12 14:08:25 -08001082 return CreatePIDRet::fail;
1083 }
James Feistb6baeaa2019-02-21 10:41:40 -08001084 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -08001085 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -08001086 {
1087 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
Ed Tanousace85d62021-10-26 12:45:59 -07001088 messages::invalidObject(
1089 response->res, crow::utility::urlFromPieces(
1090 "redfish", "v1", "Chassis", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -08001091 return CreatePIDRet::fail;
1092 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001093 output.emplace_back("Zones", std::move(zonesStrs));
James Feist5f2caae2018-12-12 14:08:25 -08001094 }
1095 if (steps)
1096 {
1097 std::vector<double> readings;
1098 std::vector<double> outputs;
1099 for (auto& step : *steps)
1100 {
Ed Tanous543f4402022-01-06 13:12:53 -08001101 double target = 0.0;
1102 double out = 0.0;
James Feist5f2caae2018-12-12 14:08:25 -08001103
1104 if (!redfish::json_util::readJson(step, response->res, "Target",
Ed Tanous23a21a12020-07-25 04:45:05 +00001105 target, "Output", out))
James Feist5f2caae2018-12-12 14:08:25 -08001106 {
James Feist5f2caae2018-12-12 14:08:25 -08001107 return CreatePIDRet::fail;
1108 }
1109 readings.emplace_back(target);
Ed Tanous23a21a12020-07-25 04:45:05 +00001110 outputs.emplace_back(out);
James Feist5f2caae2018-12-12 14:08:25 -08001111 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001112 output.emplace_back("Reading", std::move(readings));
1113 output.emplace_back("Output", std::move(outputs));
James Feist5f2caae2018-12-12 14:08:25 -08001114 }
1115 if (inputs)
1116 {
1117 for (std::string& value : *inputs)
1118 {
Ed Tanousa170f272022-06-30 21:53:27 -07001119
1120 std::replace(value.begin(), value.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -08001121 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001122 output.emplace_back("Inputs", std::move(*inputs));
James Feist5f2caae2018-12-12 14:08:25 -08001123 }
1124 if (negativeHysteresis)
1125 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001126 output.emplace_back("NegativeHysteresis", *negativeHysteresis);
James Feist5f2caae2018-12-12 14:08:25 -08001127 }
1128 if (positiveHysteresis)
1129 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001130 output.emplace_back("PositiveHysteresis", *positiveHysteresis);
James Feist83ff9ab2018-08-31 10:18:24 -07001131 }
James Feistc33a90e2019-03-01 10:17:44 -08001132 if (direction)
1133 {
1134 constexpr const std::array<const char*, 2> allowedDirections = {
1135 "Ceiling", "Floor"};
1136 if (std::find(allowedDirections.begin(), allowedDirections.end(),
1137 *direction) == allowedDirections.end())
1138 {
1139 messages::propertyValueTypeError(response->res, "Direction",
1140 *direction);
1141 return CreatePIDRet::fail;
1142 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001143 output.emplace_back("Class", *direction);
James Feistc33a90e2019-03-01 10:17:44 -08001144 }
James Feist83ff9ab2018-08-31 10:18:24 -07001145 }
1146 else
1147 {
Gunnar Millsa0744d32020-11-09 15:40:45 -06001148 BMCWEB_LOG_ERROR << "Illegal Type " << type;
Jason M. Bills35a62c72018-10-09 12:45:45 -07001149 messages::propertyUnknown(response->res, type);
James Feist83ff9ab2018-08-31 10:18:24 -07001150 return CreatePIDRet::fail;
1151 }
1152 return CreatePIDRet::patch;
1153}
James Feist73df0db2019-03-25 15:29:35 -07001154struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
1155{
Ed Tanous6936afe2022-09-08 15:10:39 -07001156 struct CompletionValues
1157 {
1158 std::vector<std::string> supportedProfiles;
1159 std::string currentProfile;
1160 dbus::utility::MapperGetSubTreeResponse subtree;
1161 };
James Feist73df0db2019-03-25 15:29:35 -07001162
Ed Tanous4e23a442022-06-06 09:57:26 -07001163 explicit GetPIDValues(
1164 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
Ed Tanous23a21a12020-07-25 04:45:05 +00001165 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001166
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001167 {}
James Feist73df0db2019-03-25 15:29:35 -07001168
1169 void run()
1170 {
1171 std::shared_ptr<GetPIDValues> self = shared_from_this();
1172
1173 // get all configurations
George Liue99073f2022-12-09 11:06:16 +08001174 constexpr std::array<std::string_view, 4> interfaces = {
1175 pidConfigurationIface, pidZoneConfigurationIface,
1176 objectManagerIface, stepwiseConfigurationIface};
1177 dbus::utility::getSubTree(
1178 "/", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001179 [self](
George Liue99073f2022-12-09 11:06:16 +08001180 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001181 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001182 if (ec)
1183 {
1184 BMCWEB_LOG_ERROR << ec;
1185 messages::internalError(self->asyncResp->res);
1186 return;
1187 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001188 self->complete.subtree = subtreeLocal;
George Liue99073f2022-12-09 11:06:16 +08001189 });
James Feist73df0db2019-03-25 15:29:35 -07001190
1191 // at the same time get the selected profile
George Liue99073f2022-12-09 11:06:16 +08001192 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1193 thermalModeIface};
1194 dbus::utility::getSubTree(
1195 "/", 0, thermalModeIfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001196 [self](
George Liue99073f2022-12-09 11:06:16 +08001197 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001198 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001199 if (ec || subtreeLocal.empty())
1200 {
1201 return;
1202 }
1203 if (subtreeLocal[0].second.size() != 1)
1204 {
1205 // invalid mapper response, should never happen
1206 BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
1207 messages::internalError(self->asyncResp->res);
1208 return;
1209 }
1210
1211 const std::string& path = subtreeLocal[0].first;
1212 const std::string& owner = subtreeLocal[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001213
1214 sdbusplus::asio::getAllProperties(
1215 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous002d39b2022-05-31 08:59:27 -07001216 [path, owner,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001217 self](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001218 const dbus::utility::DBusPropertiesMap& resp) {
1219 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001220 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001221 BMCWEB_LOG_ERROR
1222 << "GetPIDValues: Can't get thermalModeIface " << path;
James Feist73df0db2019-03-25 15:29:35 -07001223 messages::internalError(self->asyncResp->res);
1224 return;
1225 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001226
Ed Tanous002d39b2022-05-31 08:59:27 -07001227 const std::string* current = nullptr;
1228 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001229
1230 const bool success = sdbusplus::unpackPropertiesNoThrow(
1231 dbus_utils::UnpackErrorPrinter(), resp, "Current", current,
1232 "Supported", supported);
1233
1234 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001235 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001236 messages::internalError(self->asyncResp->res);
1237 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001238 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001239
Ed Tanous002d39b2022-05-31 08:59:27 -07001240 if (current == nullptr || supported == nullptr)
1241 {
1242 BMCWEB_LOG_ERROR
1243 << "GetPIDValues: thermal mode iface invalid " << path;
1244 messages::internalError(self->asyncResp->res);
1245 return;
1246 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001247 self->complete.currentProfile = *current;
1248 self->complete.supportedProfiles = *supported;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001249 });
George Liue99073f2022-12-09 11:06:16 +08001250 });
James Feist73df0db2019-03-25 15:29:35 -07001251 }
1252
Ed Tanous6936afe2022-09-08 15:10:39 -07001253 static void
1254 processingComplete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1255 const CompletionValues& completion)
James Feist73df0db2019-03-25 15:29:35 -07001256 {
1257 if (asyncResp->res.result() != boost::beast::http::status::ok)
1258 {
1259 return;
1260 }
1261 // create map of <connection, path to objMgr>>
Ed Tanous6936afe2022-09-08 15:10:39 -07001262 boost::container::flat_map<
1263 std::string, std::string, std::less<>,
1264 std::vector<std::pair<std::string, std::string>>>
1265 objectMgrPaths;
1266 boost::container::flat_set<std::string, std::less<>,
1267 std::vector<std::string>>
1268 calledConnections;
1269 for (const auto& pathGroup : completion.subtree)
James Feist73df0db2019-03-25 15:29:35 -07001270 {
1271 for (const auto& connectionGroup : pathGroup.second)
1272 {
1273 auto findConnection =
1274 calledConnections.find(connectionGroup.first);
1275 if (findConnection != calledConnections.end())
1276 {
1277 break;
1278 }
1279 for (const std::string& interface : connectionGroup.second)
1280 {
1281 if (interface == objectManagerIface)
1282 {
1283 objectMgrPaths[connectionGroup.first] = pathGroup.first;
1284 }
1285 // this list is alphabetical, so we
1286 // should have found the objMgr by now
1287 if (interface == pidConfigurationIface ||
1288 interface == pidZoneConfigurationIface ||
1289 interface == stepwiseConfigurationIface)
1290 {
1291 auto findObjMgr =
1292 objectMgrPaths.find(connectionGroup.first);
1293 if (findObjMgr == objectMgrPaths.end())
1294 {
1295 BMCWEB_LOG_DEBUG << connectionGroup.first
1296 << "Has no Object Manager";
1297 continue;
1298 }
1299
1300 calledConnections.insert(connectionGroup.first);
1301
1302 asyncPopulatePid(findObjMgr->first, findObjMgr->second,
Ed Tanous6936afe2022-09-08 15:10:39 -07001303 completion.currentProfile,
1304 completion.supportedProfiles,
James Feist73df0db2019-03-25 15:29:35 -07001305 asyncResp);
1306 break;
1307 }
1308 }
1309 }
1310 }
1311 }
1312
Ed Tanous6936afe2022-09-08 15:10:39 -07001313 ~GetPIDValues()
1314 {
1315 boost::asio::post(crow::connections::systemBus->get_io_context(),
1316 std::bind_front(&processingComplete, asyncResp,
1317 std::move(complete)));
1318 }
1319
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001320 GetPIDValues(const GetPIDValues&) = delete;
1321 GetPIDValues(GetPIDValues&&) = delete;
1322 GetPIDValues& operator=(const GetPIDValues&) = delete;
1323 GetPIDValues& operator=(GetPIDValues&&) = delete;
1324
zhanghch058d1b46d2021-04-01 11:18:24 +08001325 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Ed Tanous6936afe2022-09-08 15:10:39 -07001326 CompletionValues complete;
James Feist73df0db2019-03-25 15:29:35 -07001327};
1328
1329struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
1330{
1331
zhanghch058d1b46d2021-04-01 11:18:24 +08001332 SetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
James Feist73df0db2019-03-25 15:29:35 -07001333 nlohmann::json& data) :
Ed Tanous271584a2019-07-09 16:24:22 -07001334 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001335 {
1336
1337 std::optional<nlohmann::json> pidControllers;
1338 std::optional<nlohmann::json> fanControllers;
1339 std::optional<nlohmann::json> fanZones;
1340 std::optional<nlohmann::json> stepwiseControllers;
1341
1342 if (!redfish::json_util::readJson(
1343 data, asyncResp->res, "PidControllers", pidControllers,
1344 "FanControllers", fanControllers, "FanZones", fanZones,
1345 "StepwiseControllers", stepwiseControllers, "Profile", profile))
1346 {
James Feist73df0db2019-03-25 15:29:35 -07001347 return;
1348 }
1349 configuration.emplace_back("PidControllers", std::move(pidControllers));
1350 configuration.emplace_back("FanControllers", std::move(fanControllers));
1351 configuration.emplace_back("FanZones", std::move(fanZones));
1352 configuration.emplace_back("StepwiseControllers",
1353 std::move(stepwiseControllers));
1354 }
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001355
1356 SetPIDValues(const SetPIDValues&) = delete;
1357 SetPIDValues(SetPIDValues&&) = delete;
1358 SetPIDValues& operator=(const SetPIDValues&) = delete;
1359 SetPIDValues& operator=(SetPIDValues&&) = delete;
1360
James Feist73df0db2019-03-25 15:29:35 -07001361 void run()
1362 {
1363 if (asyncResp->res.result() != boost::beast::http::status::ok)
1364 {
1365 return;
1366 }
1367
1368 std::shared_ptr<SetPIDValues> self = shared_from_this();
1369
1370 // todo(james): might make sense to do a mapper call here if this
1371 // interface gets more traction
1372 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001373 [self](const boost::system::error_code& ec,
Ed Tanous914e2d52022-01-07 11:38:34 -08001374 const dbus::utility::ManagedObjectType& mObj) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001375 if (ec)
1376 {
1377 BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
1378 messages::internalError(self->asyncResp->res);
1379 return;
1380 }
1381 const std::array<const char*, 3> configurations = {
1382 pidConfigurationIface, pidZoneConfigurationIface,
1383 stepwiseConfigurationIface};
James Feiste69d9de2020-02-07 12:23:27 -08001384
Ed Tanous002d39b2022-05-31 08:59:27 -07001385 for (const auto& [path, object] : mObj)
1386 {
1387 for (const auto& [interface, _] : object)
James Feiste69d9de2020-02-07 12:23:27 -08001388 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001389 if (std::find(configurations.begin(), configurations.end(),
1390 interface) != configurations.end())
James Feiste69d9de2020-02-07 12:23:27 -08001391 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001392 self->objectCount++;
1393 break;
James Feiste69d9de2020-02-07 12:23:27 -08001394 }
James Feiste69d9de2020-02-07 12:23:27 -08001395 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001396 }
1397 self->managedObj = mObj;
James Feist73df0db2019-03-25 15:29:35 -07001398 },
Nan Zhouc106b672022-09-20 22:35:31 +00001399 "xyz.openbmc_project.EntityManager",
1400 "/xyz/openbmc_project/inventory", objectManagerIface,
James Feist73df0db2019-03-25 15:29:35 -07001401 "GetManagedObjects");
1402
1403 // at the same time get the profile information
George Liue99073f2022-12-09 11:06:16 +08001404 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1405 thermalModeIface};
1406 dbus::utility::getSubTree(
1407 "/", 0, thermalModeIfaces,
1408 [self](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001409 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001410 if (ec || subtree.empty())
1411 {
1412 return;
1413 }
1414 if (subtree[0].second.empty())
1415 {
1416 // invalid mapper response, should never happen
1417 BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
1418 messages::internalError(self->asyncResp->res);
1419 return;
1420 }
1421
1422 const std::string& path = subtree[0].first;
1423 const std::string& owner = subtree[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001424 sdbusplus::asio::getAllProperties(
1425 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001426 [self, path, owner](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001427 const dbus::utility::DBusPropertiesMap& r) {
1428 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001429 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001430 BMCWEB_LOG_ERROR
1431 << "SetPIDValues: Can't get thermalModeIface " << path;
James Feist73df0db2019-03-25 15:29:35 -07001432 messages::internalError(self->asyncResp->res);
1433 return;
1434 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001435 const std::string* current = nullptr;
1436 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001437
1438 const bool success = sdbusplus::unpackPropertiesNoThrow(
1439 dbus_utils::UnpackErrorPrinter(), r, "Current", current,
1440 "Supported", supported);
1441
1442 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001443 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001444 messages::internalError(self->asyncResp->res);
1445 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001446 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001447
Ed Tanous002d39b2022-05-31 08:59:27 -07001448 if (current == nullptr || supported == nullptr)
1449 {
1450 BMCWEB_LOG_ERROR
1451 << "SetPIDValues: thermal mode iface invalid " << path;
1452 messages::internalError(self->asyncResp->res);
1453 return;
1454 }
1455 self->currentProfile = *current;
1456 self->supportedProfiles = *supported;
1457 self->profileConnection = owner;
1458 self->profilePath = path;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001459 });
George Liue99073f2022-12-09 11:06:16 +08001460 });
James Feist73df0db2019-03-25 15:29:35 -07001461 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001462 void pidSetDone()
James Feist73df0db2019-03-25 15:29:35 -07001463 {
1464 if (asyncResp->res.result() != boost::beast::http::status::ok)
1465 {
1466 return;
1467 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001468 std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001469 if (profile)
1470 {
1471 if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
1472 *profile) == supportedProfiles.end())
1473 {
1474 messages::actionParameterUnknown(response->res, "Profile",
1475 *profile);
1476 return;
1477 }
1478 currentProfile = *profile;
1479 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001480 [response](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001481 if (ec)
1482 {
1483 BMCWEB_LOG_ERROR << "Error patching profile" << ec;
1484 messages::internalError(response->res);
1485 }
James Feist73df0db2019-03-25 15:29:35 -07001486 },
1487 profileConnection, profilePath,
1488 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
Ed Tanous168e20c2021-12-13 14:39:53 -08001489 "Current", dbus::utility::DbusVariantType(*profile));
James Feist73df0db2019-03-25 15:29:35 -07001490 }
1491
1492 for (auto& containerPair : configuration)
1493 {
1494 auto& container = containerPair.second;
1495 if (!container)
1496 {
1497 continue;
1498 }
James Feist6ee7f772020-02-06 16:25:27 -08001499 BMCWEB_LOG_DEBUG << *container;
1500
Ed Tanous02cad962022-06-30 16:50:15 -07001501 const std::string& type = containerPair.first;
James Feist73df0db2019-03-25 15:29:35 -07001502
1503 for (nlohmann::json::iterator it = container->begin();
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301504 it != container->end(); ++it)
James Feist73df0db2019-03-25 15:29:35 -07001505 {
1506 const auto& name = it.key();
Potin Laicddbf3d2023-02-14 14:28:58 +08001507 std::string dbusObjName = name;
1508 std::replace(dbusObjName.begin(), dbusObjName.end(), ' ', '_');
James Feist6ee7f772020-02-06 16:25:27 -08001509 BMCWEB_LOG_DEBUG << "looking for " << name;
1510
James Feist73df0db2019-03-25 15:29:35 -07001511 auto pathItr =
1512 std::find_if(managedObj.begin(), managedObj.end(),
Potin Laicddbf3d2023-02-14 14:28:58 +08001513 [&dbusObjName](const auto& obj) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001514 return boost::algorithm::ends_with(obj.first.str,
Potin Laicddbf3d2023-02-14 14:28:58 +08001515 "/" + dbusObjName);
Ed Tanous002d39b2022-05-31 08:59:27 -07001516 });
Ed Tanousb9d36b42022-02-26 21:42:46 -08001517 dbus::utility::DBusPropertiesMap output;
James Feist73df0db2019-03-25 15:29:35 -07001518
1519 output.reserve(16); // The pid interface length
1520
1521 // determines if we're patching entity-manager or
1522 // creating a new object
1523 bool createNewObject = (pathItr == managedObj.end());
James Feist6ee7f772020-02-06 16:25:27 -08001524 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
1525
James Feist73df0db2019-03-25 15:29:35 -07001526 std::string iface;
Ed Tanousea2b6702022-03-07 16:48:38 -08001527 if (!createNewObject)
James Feist73df0db2019-03-25 15:29:35 -07001528 {
Potin Lai8be2b5b2022-11-22 13:27:16 +08001529 bool findInterface = false;
Ed Tanousea2b6702022-03-07 16:48:38 -08001530 for (const auto& interface : pathItr->second)
James Feist73df0db2019-03-25 15:29:35 -07001531 {
Ed Tanousea2b6702022-03-07 16:48:38 -08001532 if (interface.first == pidConfigurationIface)
1533 {
1534 if (type == "PidControllers" ||
1535 type == "FanControllers")
1536 {
1537 iface = pidConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001538 findInterface = true;
1539 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001540 }
1541 }
1542 else if (interface.first == pidZoneConfigurationIface)
1543 {
1544 if (type == "FanZones")
1545 {
1546 iface = pidConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001547 findInterface = true;
1548 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001549 }
1550 }
1551 else if (interface.first == stepwiseConfigurationIface)
1552 {
1553 if (type == "StepwiseControllers")
1554 {
1555 iface = stepwiseConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001556 findInterface = true;
1557 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001558 }
1559 }
James Feist73df0db2019-03-25 15:29:35 -07001560 }
Potin Lai8be2b5b2022-11-22 13:27:16 +08001561
1562 // create new object if interface not found
1563 if (!findInterface)
1564 {
1565 createNewObject = true;
1566 }
James Feist73df0db2019-03-25 15:29:35 -07001567 }
James Feist6ee7f772020-02-06 16:25:27 -08001568
1569 if (createNewObject && it.value() == nullptr)
1570 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -05001571 // can't delete a non-existent object
Ed Tanous1668ce62022-02-07 23:44:31 -08001572 messages::propertyValueNotInList(response->res,
1573 it.value().dump(), name);
James Feist6ee7f772020-02-06 16:25:27 -08001574 continue;
1575 }
1576
1577 std::string path;
1578 if (pathItr != managedObj.end())
1579 {
1580 path = pathItr->first.str;
1581 }
1582
James Feist73df0db2019-03-25 15:29:35 -07001583 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
James Feiste69d9de2020-02-07 12:23:27 -08001584
1585 // arbitrary limit to avoid attacks
1586 constexpr const size_t controllerLimit = 500;
James Feist14b0b8d2020-02-12 11:52:07 -08001587 if (createNewObject && objectCount >= controllerLimit)
James Feiste69d9de2020-02-07 12:23:27 -08001588 {
1589 messages::resourceExhaustion(response->res, type);
1590 continue;
1591 }
Ed Tanousa170f272022-06-30 21:53:27 -07001592 std::string escaped = name;
1593 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1594 output.emplace_back("Name", escaped);
James Feist73df0db2019-03-25 15:29:35 -07001595
1596 std::string chassis;
1597 CreatePIDRet ret = createPidInterface(
James Feist6ee7f772020-02-06 16:25:27 -08001598 response, type, it, path, managedObj, createNewObject,
1599 output, chassis, currentProfile);
James Feist73df0db2019-03-25 15:29:35 -07001600 if (ret == CreatePIDRet::fail)
1601 {
1602 return;
1603 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001604 if (ret == CreatePIDRet::del)
James Feist73df0db2019-03-25 15:29:35 -07001605 {
1606 continue;
1607 }
1608
1609 if (!createNewObject)
1610 {
1611 for (const auto& property : output)
1612 {
1613 crow::connections::systemBus->async_method_call(
1614 [response,
1615 propertyName{std::string(property.first)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001616 const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001617 if (ec)
1618 {
1619 BMCWEB_LOG_ERROR << "Error patching "
1620 << propertyName << ": " << ec;
1621 messages::internalError(response->res);
1622 return;
1623 }
1624 messages::success(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001625 },
James Feist6ee7f772020-02-06 16:25:27 -08001626 "xyz.openbmc_project.EntityManager", path,
James Feist73df0db2019-03-25 15:29:35 -07001627 "org.freedesktop.DBus.Properties", "Set", iface,
1628 property.first, property.second);
1629 }
1630 }
1631 else
1632 {
1633 if (chassis.empty())
1634 {
1635 BMCWEB_LOG_ERROR << "Failed to get chassis from config";
Ed Tanousace85d62021-10-26 12:45:59 -07001636 messages::internalError(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001637 return;
1638 }
1639
1640 bool foundChassis = false;
1641 for (const auto& obj : managedObj)
1642 {
1643 if (boost::algorithm::ends_with(obj.first.str, chassis))
1644 {
1645 chassis = obj.first.str;
1646 foundChassis = true;
1647 break;
1648 }
1649 }
1650 if (!foundChassis)
1651 {
1652 BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
1653 messages::resourceMissingAtURI(
Ed Tanousace85d62021-10-26 12:45:59 -07001654 response->res,
1655 crow::utility::urlFromPieces("redfish", "v1",
1656 "Chassis", chassis));
James Feist73df0db2019-03-25 15:29:35 -07001657 return;
1658 }
1659
1660 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001661 [response](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001662 if (ec)
1663 {
1664 BMCWEB_LOG_ERROR << "Error Adding Pid Object "
1665 << ec;
1666 messages::internalError(response->res);
1667 return;
1668 }
1669 messages::success(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001670 },
1671 "xyz.openbmc_project.EntityManager", chassis,
1672 "xyz.openbmc_project.AddObject", "AddObject", output);
1673 }
1674 }
1675 }
1676 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001677
1678 ~SetPIDValues()
1679 {
1680 try
1681 {
1682 pidSetDone();
1683 }
1684 catch (...)
1685 {
1686 BMCWEB_LOG_CRITICAL << "pidSetDone threw exception";
1687 }
1688 }
1689
zhanghch058d1b46d2021-04-01 11:18:24 +08001690 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001691 std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
1692 configuration;
1693 std::optional<std::string> profile;
1694 dbus::utility::ManagedObjectType managedObj;
1695 std::vector<std::string> supportedProfiles;
1696 std::string currentProfile;
1697 std::string profileConnection;
1698 std::string profilePath;
James Feist14b0b8d2020-02-12 11:52:07 -08001699 size_t objectCount = 0;
James Feist73df0db2019-03-25 15:29:35 -07001700};
James Feist83ff9ab2018-08-31 10:18:24 -07001701
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001702/**
1703 * @brief Retrieves BMC manager location data over DBus
1704 *
1705 * @param[in] aResp Shared pointer for completing asynchronous calls
1706 * @param[in] connectionName - service name
1707 * @param[in] path - object path
1708 * @return none
1709 */
zhanghch058d1b46d2021-04-01 11:18:24 +08001710inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001711 const std::string& connectionName,
1712 const std::string& path)
1713{
1714 BMCWEB_LOG_DEBUG << "Get BMC manager Location data.";
1715
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001716 sdbusplus::asio::getProperty<std::string>(
1717 *crow::connections::systemBus, connectionName, path,
1718 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001719 [aResp](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001720 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001721 if (ec)
1722 {
1723 BMCWEB_LOG_DEBUG << "DBUS response error for "
1724 "Location";
1725 messages::internalError(aResp->res);
1726 return;
1727 }
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001728
Ed Tanous002d39b2022-05-31 08:59:27 -07001729 aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
1730 property;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001731 });
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001732}
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001733// avoid name collision systems.hpp
1734inline void
1735 managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001736{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001737 BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time";
Ed Tanous52cc1122020-07-18 13:51:21 -07001738
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001739 sdbusplus::asio::getProperty<uint64_t>(
1740 *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
1741 "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
1742 "LastRebootTime",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001743 [aResp](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001744 const uint64_t lastResetTime) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001745 if (ec)
1746 {
1747 BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
1748 return;
1749 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001750
Ed Tanous002d39b2022-05-31 08:59:27 -07001751 // LastRebootTime is epoch time, in milliseconds
1752 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
1753 uint64_t lastResetTimeStamp = lastResetTime / 1000;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001754
Ed Tanous002d39b2022-05-31 08:59:27 -07001755 // Convert to ISO 8601 standard
1756 aResp->res.jsonValue["LastResetTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001757 redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001758 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001759}
1760
1761/**
1762 * @brief Set the running firmware image
1763 *
1764 * @param[i,o] aResp - Async response object
1765 * @param[i] runningFirmwareTarget - Image to make the running image
1766 *
1767 * @return void
1768 */
1769inline void
1770 setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1771 const std::string& runningFirmwareTarget)
1772{
1773 // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1774 std::string::size_type idPos = runningFirmwareTarget.rfind('/');
1775 if (idPos == std::string::npos)
1776 {
1777 messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
1778 "@odata.id");
1779 BMCWEB_LOG_DEBUG << "Can't parse firmware ID!";
1780 return;
1781 }
1782 idPos++;
1783 if (idPos >= runningFirmwareTarget.size())
1784 {
1785 messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
1786 "@odata.id");
1787 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
1788 return;
1789 }
1790 std::string firmwareId = runningFirmwareTarget.substr(idPos);
1791
1792 // Make sure the image is valid before setting priority
1793 crow::connections::systemBus->async_method_call(
Ed Tanous711ac7a2021-12-20 09:34:41 -08001794 [aResp, firmwareId,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001795 runningFirmwareTarget](const boost::system::error_code& ec,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001796 dbus::utility::ManagedObjectType& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001797 if (ec)
1798 {
1799 BMCWEB_LOG_DEBUG << "D-Bus response error getting objects.";
1800 messages::internalError(aResp->res);
1801 return;
1802 }
1803
1804 if (subtree.empty())
1805 {
1806 BMCWEB_LOG_DEBUG << "Can't find image!";
1807 messages::internalError(aResp->res);
1808 return;
1809 }
1810
1811 bool foundImage = false;
Ed Tanous02cad962022-06-30 16:50:15 -07001812 for (const auto& object : subtree)
Ed Tanous002d39b2022-05-31 08:59:27 -07001813 {
1814 const std::string& path =
1815 static_cast<const std::string&>(object.first);
1816 std::size_t idPos2 = path.rfind('/');
1817
1818 if (idPos2 == std::string::npos)
1819 {
1820 continue;
1821 }
1822
1823 idPos2++;
1824 if (idPos2 >= path.size())
1825 {
1826 continue;
1827 }
1828
1829 if (path.substr(idPos2) == firmwareId)
1830 {
1831 foundImage = true;
1832 break;
1833 }
1834 }
1835
1836 if (!foundImage)
1837 {
1838 messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
1839 "@odata.id");
1840 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
1841 return;
1842 }
1843
1844 BMCWEB_LOG_DEBUG << "Setting firmware version " << firmwareId
1845 << " to priority 0.";
1846
1847 // Only support Immediate
1848 // An addition could be a Redfish Setting like
1849 // ActiveSoftwareImageApplyTime and support OnReset
1850 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001851 [aResp](const boost::system::error_code& ec2) {
Ed Tanous8a592812022-06-04 09:06:59 -07001852 if (ec2)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001853 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001854 BMCWEB_LOG_DEBUG << "D-Bus response error setting.";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001855 messages::internalError(aResp->res);
1856 return;
1857 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001858 doBMCGracefulRestart(aResp);
1859 },
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001860
Ed Tanous002d39b2022-05-31 08:59:27 -07001861 "xyz.openbmc_project.Software.BMC.Updater",
1862 "/xyz/openbmc_project/software/" + firmwareId,
1863 "org.freedesktop.DBus.Properties", "Set",
1864 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1865 dbus::utility::DbusVariantType(static_cast<uint8_t>(0)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001866 },
1867 "xyz.openbmc_project.Software.BMC.Updater",
1868 "/xyz/openbmc_project/software", "org.freedesktop.DBus.ObjectManager",
1869 "GetManagedObjects");
1870}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001871
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001872inline void setDateTime(std::shared_ptr<bmcweb::AsyncResp> aResp,
1873 std::string datetime)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001874{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001875 BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001876
Ed Tanousc2e32002023-01-07 22:05:08 -08001877 std::optional<redfish::time_utils::usSinceEpoch> us =
1878 redfish::time_utils::dateStringToEpoch(datetime);
1879 if (!us)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001880 {
1881 messages::propertyValueFormatError(aResp->res, datetime, "DateTime");
1882 return;
1883 }
Ed Tanousc2e32002023-01-07 22:05:08 -08001884 crow::connections::systemBus->async_method_call(
1885 [aResp{std::move(aResp)},
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001886 datetime{std::move(datetime)}](const boost::system::error_code& ec) {
Ed Tanousc2e32002023-01-07 22:05:08 -08001887 if (ec)
1888 {
1889 BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1890 "DBUS response error "
1891 << ec;
1892 messages::internalError(aResp->res);
1893 return;
1894 }
1895 aResp->res.jsonValue["DateTime"] = datetime;
1896 },
1897 "xyz.openbmc_project.Time.Manager", "/xyz/openbmc_project/time/bmc",
1898 "org.freedesktop.DBus.Properties", "Set",
1899 "xyz.openbmc_project.Time.EpochTime", "Elapsed",
1900 dbus::utility::DbusVariantType(us->count()));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001901}
1902
1903inline void requestRoutesManager(App& app)
1904{
1905 std::string uuid = persistent_data::getConfig().systemUuid;
1906
1907 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07001908 .privileges(redfish::privileges::getManager)
Ed Tanous002d39b2022-05-31 08:59:27 -07001909 .methods(boost::beast::http::verb::get)(
1910 [&app, uuid](const crow::Request& req,
Ed Tanous45ca1b82022-03-25 13:07:27 -07001911 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001912 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001913 {
1914 return;
1915 }
1916 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
Sui Chena51fc2d2022-07-14 17:21:53 -07001917 asyncResp->res.jsonValue["@odata.type"] = "#Manager.v1_14_0.Manager";
Ed Tanous002d39b2022-05-31 08:59:27 -07001918 asyncResp->res.jsonValue["Id"] = "bmc";
1919 asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
1920 asyncResp->res.jsonValue["Description"] =
1921 "Baseboard Management Controller";
1922 asyncResp->res.jsonValue["PowerState"] = "On";
1923 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
1924 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
Ed Tanous14766872022-03-15 10:44:42 -07001925
Ed Tanous002d39b2022-05-31 08:59:27 -07001926 asyncResp->res.jsonValue["ManagerType"] = "BMC";
1927 asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
1928 asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1929 asyncResp->res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001930
Ed Tanous002d39b2022-05-31 08:59:27 -07001931 asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
1932 "/redfish/v1/Managers/bmc/LogServices";
1933 asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
1934 "/redfish/v1/Managers/bmc/NetworkProtocol";
1935 asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
1936 "/redfish/v1/Managers/bmc/EthernetInterfaces";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001937
1938#ifdef BMCWEB_ENABLE_VM_NBDPROXY
Ed Tanous002d39b2022-05-31 08:59:27 -07001939 asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
1940 "/redfish/v1/Managers/bmc/VirtualMedia";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001941#endif // BMCWEB_ENABLE_VM_NBDPROXY
1942
Ed Tanous002d39b2022-05-31 08:59:27 -07001943 // default oem data
1944 nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
1945 nlohmann::json& oemOpenbmc = oem["OpenBmc"];
1946 oem["@odata.type"] = "#OemManager.Oem";
1947 oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
1948 oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
1949 oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
Ed Tanous14766872022-03-15 10:44:42 -07001950
Ed Tanous002d39b2022-05-31 08:59:27 -07001951 nlohmann::json::object_t certificates;
1952 certificates["@odata.id"] =
1953 "/redfish/v1/Managers/bmc/Truststore/Certificates";
1954 oemOpenbmc["Certificates"] = std::move(certificates);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001955
Ed Tanous002d39b2022-05-31 08:59:27 -07001956 // Manager.Reset (an action) can be many values, OpenBMC only
1957 // supports BMC reboot.
1958 nlohmann::json& managerReset =
1959 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
1960 managerReset["target"] =
1961 "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
1962 managerReset["@Redfish.ActionInfo"] =
1963 "/redfish/v1/Managers/bmc/ResetActionInfo";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001964
Ed Tanous002d39b2022-05-31 08:59:27 -07001965 // ResetToDefaults (Factory Reset) has values like
1966 // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
1967 // on OpenBMC
1968 nlohmann::json& resetToDefaults =
1969 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
1970 resetToDefaults["target"] =
1971 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
Ed Tanous613dabe2022-07-09 11:17:36 -07001972 resetToDefaults["ResetType@Redfish.AllowableValues"] =
1973 nlohmann::json::array_t({"ResetAll"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001974
Ed Tanous002d39b2022-05-31 08:59:27 -07001975 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001976 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301977
Ed Tanous002d39b2022-05-31 08:59:27 -07001978 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1979 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1980 redfishDateTimeOffset.second;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001981
Ed Tanous002d39b2022-05-31 08:59:27 -07001982 // TODO (Gunnar): Remove these one day since moved to ComputerSystem
1983 // Still used by OCP profiles
1984 // https://github.com/opencomputeproject/OCP-Profiles/issues/23
1985 // Fill in SerialConsole info
1986 asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
1987 asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
Ed Tanous613dabe2022-07-09 11:17:36 -07001988 asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
1989 nlohmann::json::array_t({"IPMI", "SSH"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001990#ifdef BMCWEB_ENABLE_KVM
Ed Tanous002d39b2022-05-31 08:59:27 -07001991 // Fill in GraphicalConsole info
1992 asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
1993 asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] =
1994 4;
Ed Tanous613dabe2022-07-09 11:17:36 -07001995 asyncResp->res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
1996 nlohmann::json::array_t({"KVMIP"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001997#endif // BMCWEB_ENABLE_KVM
1998
Ed Tanous002d39b2022-05-31 08:59:27 -07001999 asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
Ed Tanous14766872022-03-15 10:44:42 -07002000
Ed Tanous002d39b2022-05-31 08:59:27 -07002001 nlohmann::json::array_t managerForServers;
2002 nlohmann::json::object_t manager;
2003 manager["@odata.id"] = "/redfish/v1/Systems/system";
2004 managerForServers.push_back(std::move(manager));
2005
2006 asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
2007 std::move(managerForServers);
2008
2009 auto health = std::make_shared<HealthPopulate>(asyncResp);
2010 health->isManagersHealth = true;
2011 health->populate();
2012
Willy Tueee00132022-06-14 14:53:17 -07002013 sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
Ed Tanous002d39b2022-05-31 08:59:27 -07002014 "FirmwareVersion", true);
2015
2016 managerGetLastResetTime(asyncResp);
2017
Sui Chena51fc2d2022-07-14 17:21:53 -07002018 // ManagerDiagnosticData is added for all BMCs.
2019 nlohmann::json& managerDiagnosticData =
2020 asyncResp->res.jsonValue["ManagerDiagnosticData"];
2021 managerDiagnosticData["@odata.id"] =
2022 "/redfish/v1/Managers/bmc/ManagerDiagnosticData";
2023
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002024#ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
Ed Tanous002d39b2022-05-31 08:59:27 -07002025 auto pids = std::make_shared<GetPIDValues>(asyncResp);
2026 pids->run();
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002027#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07002028
2029 getMainChassisId(asyncResp,
2030 [](const std::string& chassisId,
2031 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2032 aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
2033 nlohmann::json::array_t managerForChassis;
Ed Tanous8a592812022-06-04 09:06:59 -07002034 nlohmann::json::object_t managerObj;
Willy Tueddfc432022-09-26 16:46:38 +00002035 boost::urls::url chassiUrl = crow::utility::urlFromPieces(
2036 "redfish", "v1", "Chassis", chassisId);
2037 managerObj["@odata.id"] = chassiUrl;
Ed Tanous8a592812022-06-04 09:06:59 -07002038 managerForChassis.push_back(std::move(managerObj));
Ed Tanous002d39b2022-05-31 08:59:27 -07002039 aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
2040 std::move(managerForChassis);
2041 aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00002042 chassiUrl;
Ed Tanous002d39b2022-05-31 08:59:27 -07002043 });
Ed Tanous14766872022-03-15 10:44:42 -07002044
Ed Tanous002d39b2022-05-31 08:59:27 -07002045 static bool started = false;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002046
Ed Tanous002d39b2022-05-31 08:59:27 -07002047 if (!started)
2048 {
2049 sdbusplus::asio::getProperty<double>(
2050 *crow::connections::systemBus, "org.freedesktop.systemd1",
2051 "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
2052 "Progress",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002053 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07002054 const double& val) {
2055 if (ec)
2056 {
2057 BMCWEB_LOG_ERROR << "Error while getting progress";
2058 messages::internalError(asyncResp->res);
2059 return;
2060 }
2061 if (val < 1.0)
2062 {
2063 asyncResp->res.jsonValue["Status"]["State"] = "Starting";
2064 started = true;
2065 }
2066 });
2067 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002068
George Liue99073f2022-12-09 11:06:16 +08002069 constexpr std::array<std::string_view, 1> interfaces = {
2070 "xyz.openbmc_project.Inventory.Item.Bmc"};
2071 dbus::utility::getSubTree(
2072 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07002073 [asyncResp](
George Liue99073f2022-12-09 11:06:16 +08002074 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07002075 const dbus::utility::MapperGetSubTreeResponse& subtree) {
2076 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002077 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002078 BMCWEB_LOG_DEBUG << "D-Bus response error on GetSubTree " << ec;
2079 return;
2080 }
2081 if (subtree.empty())
2082 {
2083 BMCWEB_LOG_DEBUG << "Can't find bmc D-Bus object!";
2084 return;
2085 }
2086 // Assume only 1 bmc D-Bus object
2087 // Throw an error if there is more than 1
2088 if (subtree.size() > 1)
2089 {
2090 BMCWEB_LOG_DEBUG << "Found more than 1 bmc D-Bus object!";
2091 messages::internalError(asyncResp->res);
2092 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002093 }
2094
Ed Tanous002d39b2022-05-31 08:59:27 -07002095 if (subtree[0].first.empty() || subtree[0].second.size() != 1)
2096 {
2097 BMCWEB_LOG_DEBUG << "Error getting bmc D-Bus object!";
2098 messages::internalError(asyncResp->res);
2099 return;
2100 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002101
Ed Tanous002d39b2022-05-31 08:59:27 -07002102 const std::string& path = subtree[0].first;
2103 const std::string& connectionName = subtree[0].second[0].first;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002104
Ed Tanous002d39b2022-05-31 08:59:27 -07002105 for (const auto& interfaceName : subtree[0].second[0].second)
2106 {
2107 if (interfaceName ==
2108 "xyz.openbmc_project.Inventory.Decorator.Asset")
2109 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002110
2111 sdbusplus::asio::getAllProperties(
2112 *crow::connections::systemBus, connectionName, path,
2113 "xyz.openbmc_project.Inventory.Decorator.Asset",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002114 [asyncResp](const boost::system::error_code& ec2,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002115 const dbus::utility::DBusPropertiesMap&
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002116 propertiesList) {
Ed Tanous8a592812022-06-04 09:06:59 -07002117 if (ec2)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002118 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002119 BMCWEB_LOG_DEBUG << "Can't get bmc asset!";
2120 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002121 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002122
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002123 const std::string* partNumber = nullptr;
2124 const std::string* serialNumber = nullptr;
2125 const std::string* manufacturer = nullptr;
2126 const std::string* model = nullptr;
2127 const std::string* sparePartNumber = nullptr;
2128
2129 const bool success = sdbusplus::unpackPropertiesNoThrow(
2130 dbus_utils::UnpackErrorPrinter(), propertiesList,
2131 "PartNumber", partNumber, "SerialNumber",
2132 serialNumber, "Manufacturer", manufacturer, "Model",
2133 model, "SparePartNumber", sparePartNumber);
2134
2135 if (!success)
2136 {
2137 messages::internalError(asyncResp->res);
2138 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07002139 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002140
2141 if (partNumber != nullptr)
2142 {
2143 asyncResp->res.jsonValue["PartNumber"] =
2144 *partNumber;
2145 }
2146
2147 if (serialNumber != nullptr)
2148 {
2149 asyncResp->res.jsonValue["SerialNumber"] =
2150 *serialNumber;
2151 }
2152
2153 if (manufacturer != nullptr)
2154 {
2155 asyncResp->res.jsonValue["Manufacturer"] =
2156 *manufacturer;
2157 }
2158
2159 if (model != nullptr)
2160 {
2161 asyncResp->res.jsonValue["Model"] = *model;
2162 }
2163
2164 if (sparePartNumber != nullptr)
2165 {
2166 asyncResp->res.jsonValue["SparePartNumber"] =
2167 *sparePartNumber;
2168 }
2169 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002170 }
2171 else if (interfaceName ==
2172 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
2173 {
2174 getLocation(asyncResp, connectionName, path);
2175 }
2176 }
George Liue99073f2022-12-09 11:06:16 +08002177 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002178 });
2179
2180 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07002181 .privileges(redfish::privileges::patchManager)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002182 .methods(boost::beast::http::verb::patch)(
2183 [&app](const crow::Request& req,
2184 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002185 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002186 {
2187 return;
2188 }
2189 std::optional<nlohmann::json> oem;
2190 std::optional<nlohmann::json> links;
2191 std::optional<std::string> datetime;
2192
2193 if (!json_util::readJsonPatch(req, asyncResp->res, "Oem", oem,
2194 "DateTime", datetime, "Links", links))
2195 {
2196 return;
2197 }
2198
2199 if (oem)
2200 {
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002201#ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
Ed Tanous002d39b2022-05-31 08:59:27 -07002202 std::optional<nlohmann::json> openbmc;
2203 if (!redfish::json_util::readJson(*oem, asyncResp->res, "OpenBmc",
2204 openbmc))
2205 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002206 return;
2207 }
2208 if (openbmc)
2209 {
2210 std::optional<nlohmann::json> fan;
2211 if (!redfish::json_util::readJson(*openbmc, asyncResp->res,
2212 "Fan", fan))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002213 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002214 return;
2215 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002216 if (fan)
2217 {
2218 auto pid = std::make_shared<SetPIDValues>(asyncResp, *fan);
2219 pid->run();
2220 }
2221 }
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002222#else
2223 messages::propertyUnknown(asyncResp->res, "Oem");
2224 return;
2225#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07002226 }
2227 if (links)
2228 {
2229 std::optional<nlohmann::json> activeSoftwareImage;
2230 if (!redfish::json_util::readJson(*links, asyncResp->res,
2231 "ActiveSoftwareImage",
2232 activeSoftwareImage))
2233 {
2234 return;
2235 }
2236 if (activeSoftwareImage)
2237 {
2238 std::optional<std::string> odataId;
2239 if (!json_util::readJson(*activeSoftwareImage, asyncResp->res,
2240 "@odata.id", odataId))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002241 {
Ed Tanous45ca1b82022-03-25 13:07:27 -07002242 return;
2243 }
2244
Ed Tanous002d39b2022-05-31 08:59:27 -07002245 if (odataId)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002246 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002247 setActiveFirmwareImage(asyncResp, *odataId);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002248 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002249 }
2250 }
2251 if (datetime)
2252 {
2253 setDateTime(asyncResp, std::move(*datetime));
2254 }
2255 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002256}
2257
2258inline void requestRoutesManagerCollection(App& app)
2259{
2260 BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
Ed Tanoused398212021-06-09 17:05:54 -07002261 .privileges(redfish::privileges::getManagerCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002262 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002263 [&app](const crow::Request& req,
2264 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002265 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002266 {
2267 return;
2268 }
2269 // Collections don't include the static data added by SubRoute
2270 // because it has a duplicate entry for members
2271 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
2272 asyncResp->res.jsonValue["@odata.type"] =
2273 "#ManagerCollection.ManagerCollection";
2274 asyncResp->res.jsonValue["Name"] = "Manager Collection";
2275 asyncResp->res.jsonValue["Members@odata.count"] = 1;
2276 nlohmann::json::array_t members;
2277 nlohmann::json& bmc = members.emplace_back();
2278 bmc["@odata.id"] = "/redfish/v1/Managers/bmc";
2279 asyncResp->res.jsonValue["Members"] = std::move(members);
2280 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002281}
Ed Tanous1abe55e2018-09-05 08:30:59 -07002282} // namespace redfish