blob: 21f74b77c8e0a79c0483541ec25acf3ee57c9c31 [file] [log] [blame]
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Willy Tu13451e32023-05-24 16:08:18 -070018#include "bmcweb_config.h"
19
Sui Chena51fc2d2022-07-14 17:21:53 -070020#include "app.hpp"
21#include "dbus_utility.hpp"
James Feistb49ac872019-05-21 15:12:01 -070022#include "health.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070023#include "query.hpp"
Jennifer Leec5d03ff2019-03-08 15:42:58 -080024#include "redfish_util.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070025#include "registries/privilege_registry.hpp"
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +020026#include "utils/dbus_utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080027#include "utils/json_utils.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070028#include "utils/sw_utils.hpp"
29#include "utils/systemd_utils.hpp"
Ed Tanous2b829372022-08-03 14:22:34 -070030#include "utils/time_utils.hpp"
Borawski.Lukasz9c3106852018-02-09 15:24:22 +010031
George Liue99073f2022-12-09 11:06:16 +080032#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070033#include <boost/url/format.hpp>
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +020034#include <sdbusplus/asio/property.hpp>
35#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050036
Ed Tanousa170f272022-06-30 21:53:27 -070037#include <algorithm>
George Liue99073f2022-12-09 11:06:16 +080038#include <array>
Gunnar Mills4bfefa72020-07-30 13:54:29 -050039#include <cstdint>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050040#include <memory>
41#include <sstream>
George Liue99073f2022-12-09 11:06:16 +080042#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080043#include <variant>
James Feist5b4aa862018-08-16 14:07:01 -070044
Ed Tanous1abe55e2018-09-05 08:30:59 -070045namespace redfish
46{
Jennifer Leeed5befb2018-08-10 11:29:45 -070047
48/**
Gunnar Mills2a5c4402020-05-19 09:07:24 -050049 * Function reboots the BMC.
50 *
51 * @param[in] asyncResp - Shared pointer for completing asynchronous calls
Jennifer Leeed5befb2018-08-10 11:29:45 -070052 */
zhanghch058d1b46d2021-04-01 11:18:24 +080053inline void
54 doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Gunnar Mills2a5c4402020-05-19 09:07:24 -050055{
56 const char* processName = "xyz.openbmc_project.State.BMC";
57 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
58 const char* interfaceName = "xyz.openbmc_project.State.BMC";
59 const std::string& propertyValue =
60 "xyz.openbmc_project.State.BMC.Transition.Reboot";
61 const char* destProperty = "RequestedBMCTransition";
62
63 // Create the D-Bus variant for D-Bus call.
Ed Tanous168e20c2021-12-13 14:39:53 -080064 dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
Gunnar Mills2a5c4402020-05-19 09:07:24 -050065
66 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080067 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -070068 // Use "Set" method to set the property value.
69 if (ec)
70 {
71 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
72 messages::internalError(asyncResp->res);
73 return;
74 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -050075
Ed Tanous002d39b2022-05-31 08:59:27 -070076 messages::success(asyncResp->res);
Gunnar Mills2a5c4402020-05-19 09:07:24 -050077 },
78 processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
79 interfaceName, destProperty, dbusPropertyValue);
80}
81
zhanghch058d1b46d2021-04-01 11:18:24 +080082inline void
83 doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +000084{
85 const char* processName = "xyz.openbmc_project.State.BMC";
86 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
87 const char* interfaceName = "xyz.openbmc_project.State.BMC";
88 const std::string& propertyValue =
89 "xyz.openbmc_project.State.BMC.Transition.HardReboot";
90 const char* destProperty = "RequestedBMCTransition";
91
92 // Create the D-Bus variant for D-Bus call.
Ed Tanous168e20c2021-12-13 14:39:53 -080093 dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +000094
95 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080096 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -070097 // Use "Set" method to set the property value.
98 if (ec)
99 {
100 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
101 messages::internalError(asyncResp->res);
102 return;
103 }
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000104
Ed Tanous002d39b2022-05-31 08:59:27 -0700105 messages::success(asyncResp->res);
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000106 },
107 processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
108 interfaceName, destProperty, dbusPropertyValue);
109}
110
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500111/**
112 * ManagerResetAction class supports the POST method for the Reset (reboot)
113 * action.
114 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700115inline void requestRoutesManagerResetAction(App& app)
Jennifer Leeed5befb2018-08-10 11:29:45 -0700116{
Jennifer Leeed5befb2018-08-10 11:29:45 -0700117 /**
Jennifer Leeed5befb2018-08-10 11:29:45 -0700118 * Function handles POST method request.
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500119 * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000120 * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
Jennifer Leeed5befb2018-08-10 11:29:45 -0700121 */
Jennifer Leeed5befb2018-08-10 11:29:45 -0700122
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700123 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
Ed Tanoused398212021-06-09 17:05:54 -0700124 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700125 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700126 [&app](const crow::Request& req,
127 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000128 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700129 {
130 return;
131 }
132 BMCWEB_LOG_DEBUG << "Post Manager Reset.";
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500133
Ed Tanous002d39b2022-05-31 08:59:27 -0700134 std::string resetType;
Jennifer Leeed5befb2018-08-10 11:29:45 -0700135
Ed Tanous002d39b2022-05-31 08:59:27 -0700136 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
137 resetType))
138 {
139 return;
140 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500141
Ed Tanous002d39b2022-05-31 08:59:27 -0700142 if (resetType == "GracefulRestart")
143 {
144 BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
145 doBMCGracefulRestart(asyncResp);
146 return;
147 }
148 if (resetType == "ForceRestart")
149 {
150 BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
151 doBMCForceRestart(asyncResp);
152 return;
153 }
154 BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
155 << resetType;
156 messages::actionParameterNotSupported(asyncResp->res, resetType,
157 "ResetType");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700158
Ed Tanous002d39b2022-05-31 08:59:27 -0700159 return;
160 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700161}
Jennifer Leeed5befb2018-08-10 11:29:45 -0700162
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500163/**
164 * ManagerResetToDefaultsAction class supports POST method for factory reset
165 * action.
166 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700167inline void requestRoutesManagerResetToDefaultsAction(App& app)
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500168{
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500169 /**
170 * Function handles ResetToDefaults POST method request.
171 *
172 * Analyzes POST body message and factory resets BMC by calling
173 * BMC code updater factory reset followed by a BMC reboot.
174 *
175 * BMC code updater factory reset wipes the whole BMC read-write
176 * filesystem which includes things like the network settings.
177 *
178 * OpenBMC only supports ResetToDefaultsType "ResetAll".
179 */
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500180
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700181 BMCWEB_ROUTE(app,
182 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/")
Ed Tanoused398212021-06-09 17:05:54 -0700183 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700184 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700185 [&app](const crow::Request& req,
186 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000187 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700188 {
189 return;
190 }
191 BMCWEB_LOG_DEBUG << "Post ResetToDefaults.";
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500192
Ed Tanous002d39b2022-05-31 08:59:27 -0700193 std::string resetType;
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500194
Ed Tanous002d39b2022-05-31 08:59:27 -0700195 if (!json_util::readJsonAction(req, asyncResp->res,
196 "ResetToDefaultsType", resetType))
197 {
198 BMCWEB_LOG_DEBUG << "Missing property ResetToDefaultsType.";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700199
Ed Tanous002d39b2022-05-31 08:59:27 -0700200 messages::actionParameterMissing(asyncResp->res, "ResetToDefaults",
201 "ResetToDefaultsType");
202 return;
203 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700204
Ed Tanous002d39b2022-05-31 08:59:27 -0700205 if (resetType != "ResetAll")
206 {
207 BMCWEB_LOG_DEBUG
208 << "Invalid property value for ResetToDefaultsType: "
209 << resetType;
210 messages::actionParameterNotSupported(asyncResp->res, resetType,
211 "ResetToDefaultsType");
212 return;
213 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700214
Ed Tanous002d39b2022-05-31 08:59:27 -0700215 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800216 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700217 if (ec)
218 {
219 BMCWEB_LOG_DEBUG << "Failed to ResetToDefaults: " << ec;
220 messages::internalError(asyncResp->res);
221 return;
222 }
223 // Factory Reset doesn't actually happen until a reboot
224 // Can't erase what the BMC is running on
225 doBMCGracefulRestart(asyncResp);
226 },
227 "xyz.openbmc_project.Software.BMC.Updater",
228 "/xyz/openbmc_project/software",
229 "xyz.openbmc_project.Common.FactoryReset", "Reset");
230 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700231}
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500232
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530233/**
234 * ManagerResetActionInfo derived class for delivering Manager
235 * ResetType AllowableValues using ResetInfo schema.
236 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700237inline void requestRoutesManagerResetActionInfo(App& app)
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530238{
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530239 /**
240 * Functions triggers appropriate requests on DBus
241 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700242
243 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/ResetActionInfo/")
Ed Tanoused398212021-06-09 17:05:54 -0700244 .privileges(redfish::privileges::getActionInfo)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700245 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700246 [&app](const crow::Request& req,
247 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000248 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700249 {
250 return;
251 }
Ed Tanous14766872022-03-15 10:44:42 -0700252
Ed Tanous002d39b2022-05-31 08:59:27 -0700253 asyncResp->res.jsonValue["@odata.type"] =
254 "#ActionInfo.v1_1_2.ActionInfo";
255 asyncResp->res.jsonValue["@odata.id"] =
256 "/redfish/v1/Managers/bmc/ResetActionInfo";
257 asyncResp->res.jsonValue["Name"] = "Reset Action Info";
258 asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
259 nlohmann::json::object_t parameter;
260 parameter["Name"] = "ResetType";
261 parameter["Required"] = true;
262 parameter["DataType"] = "String";
Ed Tanous14766872022-03-15 10:44:42 -0700263
Ed Tanous002d39b2022-05-31 08:59:27 -0700264 nlohmann::json::array_t allowableValues;
Patrick Williamsad539542023-05-12 10:10:08 -0500265 allowableValues.emplace_back("GracefulRestart");
266 allowableValues.emplace_back("ForceRestart");
Ed Tanous002d39b2022-05-31 08:59:27 -0700267 parameter["AllowableValues"] = std::move(allowableValues);
Ed Tanous14766872022-03-15 10:44:42 -0700268
Ed Tanous002d39b2022-05-31 08:59:27 -0700269 nlohmann::json::array_t parameters;
Patrick Williamsad539542023-05-12 10:10:08 -0500270 parameters.emplace_back(std::move(parameter));
Ed Tanous14766872022-03-15 10:44:42 -0700271
Ed Tanous002d39b2022-05-31 08:59:27 -0700272 asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
273 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700274}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530275
James Feist5b4aa862018-08-16 14:07:01 -0700276static constexpr const char* objectManagerIface =
277 "org.freedesktop.DBus.ObjectManager";
278static constexpr const char* pidConfigurationIface =
279 "xyz.openbmc_project.Configuration.Pid";
280static constexpr const char* pidZoneConfigurationIface =
281 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feistb7a08d02018-12-11 14:55:37 -0800282static constexpr const char* stepwiseConfigurationIface =
283 "xyz.openbmc_project.Configuration.Stepwise";
James Feist73df0db2019-03-25 15:29:35 -0700284static constexpr const char* thermalModeIface =
285 "xyz.openbmc_project.Control.ThermalMode";
Borawski.Lukasz9c3106852018-02-09 15:24:22 +0100286
zhanghch058d1b46d2021-04-01 11:18:24 +0800287inline void
288 asyncPopulatePid(const std::string& connection, const std::string& path,
289 const std::string& currentProfile,
290 const std::vector<std::string>& supportedProfiles,
291 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
James Feist5b4aa862018-08-16 14:07:01 -0700292{
James Feist5b4aa862018-08-16 14:07:01 -0700293 crow::connections::systemBus->async_method_call(
James Feist73df0db2019-03-25 15:29:35 -0700294 [asyncResp, currentProfile, supportedProfiles](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800295 const boost::system::error_code& ec,
James Feist73df0db2019-03-25 15:29:35 -0700296 const dbus::utility::ManagedObjectType& managedObj) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700297 if (ec)
298 {
299 BMCWEB_LOG_ERROR << ec;
Ed Tanous002d39b2022-05-31 08:59:27 -0700300 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
Ed Tanousef4c65b2023-04-24 15:28:50 -0700397 boost::urls::url url("/redfish/v1/Managers/bmc");
Ed Tanous002d39b2022-05-31 08:59:27 -0700398 if (intfPair.first == pidZoneConfigurationIface)
399 {
400 std::string chassis;
401 if (!dbus::utility::getNthStringFromPath(pathPair.first.str,
402 5, chassis))
403 {
404 chassis = "#IllegalValue";
405 }
406 nlohmann::json& zone = zones[name];
Ed Tanousef4c65b2023-04-24 15:28:50 -0700407 zone["Chassis"]["@odata.id"] =
408 boost::urls::format("/redfish/v1/Chassis/{}", chassis);
Willy Tueddfc432022-09-26 16:46:38 +0000409 url.set_fragment(
410 ("/Oem/OpenBmc/Fan/FanZones"_json_pointer / name)
411 .to_string());
412 zone["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700413 zone["@odata.type"] = "#OemManager.FanZone";
414 config = &zone;
415 }
416
417 else if (intfPair.first == stepwiseConfigurationIface)
418 {
419 if (classPtr == nullptr)
420 {
421 BMCWEB_LOG_ERROR << "Pid Class Field illegal";
James Feistb7a08d02018-12-11 14:55:37 -0800422 messages::internalError(asyncResp->res);
423 return;
424 }
425
Ed Tanous002d39b2022-05-31 08:59:27 -0700426 nlohmann::json& controller = stepwise[name];
427 config = &controller;
Willy Tueddfc432022-09-26 16:46:38 +0000428 url.set_fragment(
429 ("/Oem/OpenBmc/Fan/StepwiseControllers"_json_pointer /
430 name)
431 .to_string());
432 controller["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700433 controller["@odata.type"] =
434 "#OemManager.StepwiseController";
435
436 controller["Direction"] = *classPtr;
437 }
438
439 // pid and fans are off the same configuration
440 else if (intfPair.first == pidConfigurationIface)
441 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700442 if (classPtr == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700443 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700444 BMCWEB_LOG_ERROR << "Pid Class Field illegal";
445 messages::internalError(asyncResp->res);
446 return;
447 }
448 bool isFan = *classPtr == "fan";
449 nlohmann::json& element = isFan ? fans[name] : pids[name];
450 config = &element;
451 if (isFan)
452 {
Willy Tueddfc432022-09-26 16:46:38 +0000453 url.set_fragment(
454 ("/Oem/OpenBmc/Fan/FanControllers"_json_pointer /
455 name)
456 .to_string());
457 element["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700458 element["@odata.type"] = "#OemManager.FanController";
459 }
460 else
461 {
Willy Tueddfc432022-09-26 16:46:38 +0000462 url.set_fragment(
463 ("/Oem/OpenBmc/Fan/PidControllers"_json_pointer /
464 name)
465 .to_string());
466 element["@odata.id"] = std::move(url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700467 element["@odata.type"] = "#OemManager.PidController";
468 }
469 }
470 else
471 {
472 BMCWEB_LOG_ERROR << "Unexpected configuration";
473 messages::internalError(asyncResp->res);
474 return;
475 }
James Feist5b4aa862018-08-16 14:07:01 -0700476
Ed Tanous002d39b2022-05-31 08:59:27 -0700477 // used for making maps out of 2 vectors
478 const std::vector<double>* keys = nullptr;
479 const std::vector<double>* values = nullptr;
480
481 for (const auto& propertyPair : intfPair.second)
482 {
483 if (propertyPair.first == "Type" ||
484 propertyPair.first == "Class" ||
485 propertyPair.first == "Name")
486 {
487 continue;
488 }
489
490 // zones
491 if (intfPair.first == pidZoneConfigurationIface)
492 {
493 const double* ptr =
494 std::get_if<double>(&propertyPair.second);
495 if (ptr == nullptr)
496 {
497 BMCWEB_LOG_ERROR << "Field Illegal "
498 << propertyPair.first;
499 messages::internalError(asyncResp->res);
500 return;
501 }
502 (*config)[propertyPair.first] = *ptr;
503 }
504
505 if (intfPair.first == stepwiseConfigurationIface)
506 {
507 if (propertyPair.first == "Reading" ||
508 propertyPair.first == "Output")
509 {
510 const std::vector<double>* ptr =
511 std::get_if<std::vector<double>>(
512 &propertyPair.second);
513
514 if (ptr == nullptr)
515 {
516 BMCWEB_LOG_ERROR << "Field Illegal "
517 << propertyPair.first;
518 messages::internalError(asyncResp->res);
519 return;
520 }
521
522 if (propertyPair.first == "Reading")
523 {
524 keys = ptr;
525 }
526 else
527 {
528 values = ptr;
529 }
530 if (keys != nullptr && values != nullptr)
531 {
532 if (keys->size() != values->size())
533 {
534 BMCWEB_LOG_ERROR
535 << "Reading and Output size don't match ";
536 messages::internalError(asyncResp->res);
537 return;
538 }
539 nlohmann::json& steps = (*config)["Steps"];
540 steps = nlohmann::json::array();
541 for (size_t ii = 0; ii < keys->size(); ii++)
542 {
543 nlohmann::json::object_t step;
544 step["Target"] = (*keys)[ii];
545 step["Output"] = (*values)[ii];
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500546 steps.emplace_back(std::move(step));
Ed Tanous002d39b2022-05-31 08:59:27 -0700547 }
548 }
549 }
550 if (propertyPair.first == "NegativeHysteresis" ||
551 propertyPair.first == "PositiveHysteresis")
James Feist5b4aa862018-08-16 14:07:01 -0700552 {
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800553 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800554 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700555 if (ptr == nullptr)
556 {
557 BMCWEB_LOG_ERROR << "Field Illegal "
558 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700559 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700560 return;
561 }
James Feistb7a08d02018-12-11 14:55:37 -0800562 (*config)[propertyPair.first] = *ptr;
563 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700564 }
James Feistb7a08d02018-12-11 14:55:37 -0800565
Ed Tanous002d39b2022-05-31 08:59:27 -0700566 // pid and fans are off the same configuration
567 if (intfPair.first == pidConfigurationIface ||
568 intfPair.first == stepwiseConfigurationIface)
569 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700570 if (propertyPair.first == "Zones")
James Feistb7a08d02018-12-11 14:55:37 -0800571 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700572 const std::vector<std::string>* inputs =
573 std::get_if<std::vector<std::string>>(
574 &propertyPair.second);
575
576 if (inputs == nullptr)
James Feistb7a08d02018-12-11 14:55:37 -0800577 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700578 BMCWEB_LOG_ERROR << "Zones Pid Field Illegal";
579 messages::internalError(asyncResp->res);
580 return;
James Feistb7a08d02018-12-11 14:55:37 -0800581 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700582 auto& data = (*config)[propertyPair.first];
583 data = nlohmann::json::array();
584 for (std::string itemCopy : *inputs)
James Feistb7a08d02018-12-11 14:55:37 -0800585 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700586 dbus::utility::escapePathForDbus(itemCopy);
587 nlohmann::json::object_t input;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700588 boost::urls::url managerUrl = boost::urls::format(
589 "/redfish/v1/Managers/bmc#{}",
Willy Tueddfc432022-09-26 16:46:38 +0000590 ("/Oem/OpenBmc/Fan/FanZones"_json_pointer /
591 itemCopy)
592 .to_string());
593 input["@odata.id"] = std::move(managerUrl);
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500594 data.emplace_back(std::move(input));
James Feistb7a08d02018-12-11 14:55:37 -0800595 }
James Feist5b4aa862018-08-16 14:07:01 -0700596 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700597 // todo(james): may never happen, but this
598 // assumes configuration data referenced in the
599 // PID config is provided by the same daemon, we
600 // could add another loop to cover all cases,
601 // but I'm okay kicking this can down the road a
602 // bit
James Feist5b4aa862018-08-16 14:07:01 -0700603
Ed Tanous002d39b2022-05-31 08:59:27 -0700604 else if (propertyPair.first == "Inputs" ||
605 propertyPair.first == "Outputs")
James Feist5b4aa862018-08-16 14:07:01 -0700606 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700607 auto& data = (*config)[propertyPair.first];
608 const std::vector<std::string>* inputs =
609 std::get_if<std::vector<std::string>>(
610 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700611
Ed Tanous002d39b2022-05-31 08:59:27 -0700612 if (inputs == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700613 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700614 BMCWEB_LOG_ERROR << "Field Illegal "
615 << propertyPair.first;
616 messages::internalError(asyncResp->res);
617 return;
James Feist5b4aa862018-08-16 14:07:01 -0700618 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700619 data = *inputs;
620 }
621 else if (propertyPair.first == "SetPointOffset")
622 {
623 const std::string* ptr =
624 std::get_if<std::string>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700625
Ed Tanous002d39b2022-05-31 08:59:27 -0700626 if (ptr == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700627 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700628 BMCWEB_LOG_ERROR << "Field Illegal "
629 << propertyPair.first;
630 messages::internalError(asyncResp->res);
631 return;
James Feistb943aae2019-07-11 16:33:56 -0700632 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700633 // translate from dbus to redfish
634 if (*ptr == "WarningHigh")
James Feistb943aae2019-07-11 16:33:56 -0700635 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700636 (*config)["SetPointOffset"] =
637 "UpperThresholdNonCritical";
James Feistb943aae2019-07-11 16:33:56 -0700638 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700639 else if (*ptr == "WarningLow")
James Feist5b4aa862018-08-16 14:07:01 -0700640 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700641 (*config)["SetPointOffset"] =
642 "LowerThresholdNonCritical";
James Feist5b4aa862018-08-16 14:07:01 -0700643 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700644 else if (*ptr == "CriticalHigh")
645 {
646 (*config)["SetPointOffset"] =
647 "UpperThresholdCritical";
648 }
649 else if (*ptr == "CriticalLow")
650 {
651 (*config)["SetPointOffset"] =
652 "LowerThresholdCritical";
653 }
654 else
655 {
656 BMCWEB_LOG_ERROR << "Value Illegal " << *ptr;
657 messages::internalError(asyncResp->res);
658 return;
659 }
660 }
661 // doubles
662 else if (propertyPair.first == "FFGainCoefficient" ||
663 propertyPair.first == "FFOffCoefficient" ||
664 propertyPair.first == "ICoefficient" ||
665 propertyPair.first == "ILimitMax" ||
666 propertyPair.first == "ILimitMin" ||
667 propertyPair.first == "PositiveHysteresis" ||
668 propertyPair.first == "NegativeHysteresis" ||
669 propertyPair.first == "OutLimitMax" ||
670 propertyPair.first == "OutLimitMin" ||
671 propertyPair.first == "PCoefficient" ||
672 propertyPair.first == "SetPoint" ||
673 propertyPair.first == "SlewNeg" ||
674 propertyPair.first == "SlewPos")
675 {
676 const double* ptr =
677 std::get_if<double>(&propertyPair.second);
678 if (ptr == nullptr)
679 {
680 BMCWEB_LOG_ERROR << "Field Illegal "
681 << propertyPair.first;
682 messages::internalError(asyncResp->res);
683 return;
684 }
685 (*config)[propertyPair.first] = *ptr;
James Feist5b4aa862018-08-16 14:07:01 -0700686 }
687 }
688 }
689 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700690 }
James Feist5b4aa862018-08-16 14:07:01 -0700691 },
692 connection, path, objectManagerIface, "GetManagedObjects");
693}
Jennifer Leeca537922018-08-10 10:07:30 -0700694
James Feist83ff9ab2018-08-31 10:18:24 -0700695enum class CreatePIDRet
696{
697 fail,
698 del,
699 patch
700};
701
zhanghch058d1b46d2021-04-01 11:18:24 +0800702inline bool
703 getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
704 std::vector<nlohmann::json>& config,
705 std::vector<std::string>& zones)
James Feist5f2caae2018-12-12 14:08:25 -0800706{
James Feistb6baeaa2019-02-21 10:41:40 -0800707 if (config.empty())
708 {
709 BMCWEB_LOG_ERROR << "Empty Zones";
Ed Tanous1668ce62022-02-07 23:44:31 -0800710 messages::propertyValueFormatError(response->res, "[]", "Zones");
James Feistb6baeaa2019-02-21 10:41:40 -0800711 return false;
712 }
James Feist5f2caae2018-12-12 14:08:25 -0800713 for (auto& odata : config)
714 {
715 std::string path;
716 if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
717 path))
718 {
719 return false;
720 }
721 std::string input;
James Feist61adbda2019-03-25 13:03:51 -0700722
723 // 8 below comes from
724 // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
725 // 0 1 2 3 4 5 6 7 8
726 if (!dbus::utility::getNthStringFromPath(path, 8, input))
James Feist5f2caae2018-12-12 14:08:25 -0800727 {
728 BMCWEB_LOG_ERROR << "Got invalid path " << path;
729 BMCWEB_LOG_ERROR << "Illegal Type Zones";
730 messages::propertyValueFormatError(response->res, odata.dump(),
731 "Zones");
732 return false;
733 }
Ed Tanousa170f272022-06-30 21:53:27 -0700734 std::replace(input.begin(), input.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -0800735 zones.emplace_back(std::move(input));
736 }
737 return true;
738}
739
Ed Tanous711ac7a2021-12-20 09:34:41 -0800740inline const dbus::utility::ManagedObjectType::value_type*
James Feist73df0db2019-03-25 15:29:35 -0700741 findChassis(const dbus::utility::ManagedObjectType& managedObj,
742 const std::string& value, std::string& chassis)
James Feistb6baeaa2019-02-21 10:41:40 -0800743{
744 BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
745
Ed Tanousa170f272022-06-30 21:53:27 -0700746 std::string escaped = value;
Yaswanth Reddy M6ce82fa2023-03-10 07:29:45 +0000747 std::replace(escaped.begin(), escaped.end(), ' ', '_');
James Feistb6baeaa2019-02-21 10:41:40 -0800748 escaped = "/" + escaped;
Ed Tanous002d39b2022-05-31 08:59:27 -0700749 auto it = std::find_if(managedObj.begin(), managedObj.end(),
750 [&escaped](const auto& obj) {
751 if (boost::algorithm::ends_with(obj.first.str, escaped))
752 {
753 BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
754 return true;
755 }
756 return false;
757 });
James Feistb6baeaa2019-02-21 10:41:40 -0800758
759 if (it == managedObj.end())
760 {
James Feist73df0db2019-03-25 15:29:35 -0700761 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800762 }
763 // 5 comes from <chassis-name> being the 5th element
764 // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
James Feist73df0db2019-03-25 15:29:35 -0700765 if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
766 {
767 return &(*it);
768 }
769
770 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800771}
772
Ed Tanous23a21a12020-07-25 04:45:05 +0000773inline CreatePIDRet createPidInterface(
zhanghch058d1b46d2021-04-01 11:18:24 +0800774 const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
Ed Tanousb5a76932020-09-29 16:16:58 -0700775 const nlohmann::json::iterator& it, const std::string& path,
James Feist83ff9ab2018-08-31 10:18:24 -0700776 const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800777 dbus::utility::DBusPropertiesMap& output, std::string& chassis,
778 const std::string& profile)
James Feist83ff9ab2018-08-31 10:18:24 -0700779{
James Feist5f2caae2018-12-12 14:08:25 -0800780 // common deleter
James Feistb6baeaa2019-02-21 10:41:40 -0800781 if (it.value() == nullptr)
James Feist5f2caae2018-12-12 14:08:25 -0800782 {
783 std::string iface;
784 if (type == "PidControllers" || type == "FanControllers")
785 {
786 iface = pidConfigurationIface;
787 }
788 else if (type == "FanZones")
789 {
790 iface = pidZoneConfigurationIface;
791 }
792 else if (type == "StepwiseControllers")
793 {
794 iface = stepwiseConfigurationIface;
795 }
796 else
797 {
Gunnar Millsa0744d32020-11-09 15:40:45 -0600798 BMCWEB_LOG_ERROR << "Illegal Type " << type;
James Feist5f2caae2018-12-12 14:08:25 -0800799 messages::propertyUnknown(response->res, type);
800 return CreatePIDRet::fail;
801 }
James Feist6ee7f772020-02-06 16:25:27 -0800802
803 BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
James Feist5f2caae2018-12-12 14:08:25 -0800804 // delete interface
805 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800806 [response, path](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700807 if (ec)
808 {
809 BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
810 messages::internalError(response->res);
811 return;
812 }
813 messages::success(response->res);
James Feist5f2caae2018-12-12 14:08:25 -0800814 },
815 "xyz.openbmc_project.EntityManager", path, iface, "Delete");
816 return CreatePIDRet::del;
817 }
818
Ed Tanous711ac7a2021-12-20 09:34:41 -0800819 const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800820 if (!createNewObject)
821 {
822 // if we aren't creating a new object, we should be able to find it on
823 // d-bus
James Feist73df0db2019-03-25 15:29:35 -0700824 managedItem = findChassis(managedObj, it.key(), chassis);
825 if (managedItem == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800826 {
827 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
Ed Tanousef4c65b2023-04-24 15:28:50 -0700828 messages::invalidObject(
829 response->res,
830 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800831 return CreatePIDRet::fail;
832 }
833 }
834
Ed Tanous26f69762022-01-25 09:49:11 -0800835 if (!profile.empty() &&
James Feist73df0db2019-03-25 15:29:35 -0700836 (type == "PidControllers" || type == "FanControllers" ||
837 type == "StepwiseControllers"))
838 {
839 if (managedItem == nullptr)
840 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800841 output.emplace_back("Profiles", std::vector<std::string>{profile});
James Feist73df0db2019-03-25 15:29:35 -0700842 }
843 else
844 {
845 std::string interface;
846 if (type == "StepwiseControllers")
847 {
848 interface = stepwiseConfigurationIface;
849 }
850 else
851 {
852 interface = pidConfigurationIface;
853 }
Ed Tanous711ac7a2021-12-20 09:34:41 -0800854 bool ifaceFound = false;
855 for (const auto& iface : managedItem->second)
856 {
857 if (iface.first == interface)
858 {
859 ifaceFound = true;
860 for (const auto& prop : iface.second)
861 {
862 if (prop.first == "Profiles")
863 {
864 const std::vector<std::string>* curProfiles =
865 std::get_if<std::vector<std::string>>(
866 &(prop.second));
867 if (curProfiles == nullptr)
868 {
869 BMCWEB_LOG_ERROR
870 << "Illegal profiles in managed object";
871 messages::internalError(response->res);
872 return CreatePIDRet::fail;
873 }
874 if (std::find(curProfiles->begin(),
875 curProfiles->end(),
876 profile) == curProfiles->end())
877 {
878 std::vector<std::string> newProfiles =
879 *curProfiles;
880 newProfiles.push_back(profile);
Ed Tanousb9d36b42022-02-26 21:42:46 -0800881 output.emplace_back("Profiles", newProfiles);
Ed Tanous711ac7a2021-12-20 09:34:41 -0800882 }
883 }
884 }
885 }
886 }
887
888 if (!ifaceFound)
James Feist73df0db2019-03-25 15:29:35 -0700889 {
890 BMCWEB_LOG_ERROR
891 << "Failed to find interface in managed object";
892 messages::internalError(response->res);
893 return CreatePIDRet::fail;
894 }
James Feist73df0db2019-03-25 15:29:35 -0700895 }
896 }
897
James Feist83ff9ab2018-08-31 10:18:24 -0700898 if (type == "PidControllers" || type == "FanControllers")
899 {
900 if (createNewObject)
901 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800902 output.emplace_back("Class",
903 type == "PidControllers" ? "temp" : "fan");
904 output.emplace_back("Type", "Pid");
James Feist83ff9ab2018-08-31 10:18:24 -0700905 }
James Feist5f2caae2018-12-12 14:08:25 -0800906
907 std::optional<std::vector<nlohmann::json>> zones;
908 std::optional<std::vector<std::string>> inputs;
909 std::optional<std::vector<std::string>> outputs;
910 std::map<std::string, std::optional<double>> doubles;
James Feistb943aae2019-07-11 16:33:56 -0700911 std::optional<std::string> setpointOffset;
James Feist5f2caae2018-12-12 14:08:25 -0800912 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -0800913 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
James Feist5f2caae2018-12-12 14:08:25 -0800914 "Zones", zones, "FFGainCoefficient",
915 doubles["FFGainCoefficient"], "FFOffCoefficient",
916 doubles["FFOffCoefficient"], "ICoefficient",
917 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
918 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
919 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
920 "PCoefficient", doubles["PCoefficient"], "SetPoint",
James Feistb943aae2019-07-11 16:33:56 -0700921 doubles["SetPoint"], "SetPointOffset", setpointOffset,
922 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
923 "PositiveHysteresis", doubles["PositiveHysteresis"],
924 "NegativeHysteresis", doubles["NegativeHysteresis"]))
James Feist83ff9ab2018-08-31 10:18:24 -0700925 {
James Feist5f2caae2018-12-12 14:08:25 -0800926 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700927 }
James Feist5f2caae2018-12-12 14:08:25 -0800928 if (zones)
James Feist83ff9ab2018-08-31 10:18:24 -0700929 {
James Feist5f2caae2018-12-12 14:08:25 -0800930 std::vector<std::string> zonesStr;
931 if (!getZonesFromJsonReq(response, *zones, zonesStr))
James Feist83ff9ab2018-08-31 10:18:24 -0700932 {
Gunnar Millsa0744d32020-11-09 15:40:45 -0600933 BMCWEB_LOG_ERROR << "Illegal Zones";
James Feist5f2caae2018-12-12 14:08:25 -0800934 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700935 }
James Feistb6baeaa2019-02-21 10:41:40 -0800936 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -0800937 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800938 {
939 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
Ed Tanousace85d62021-10-26 12:45:59 -0700940 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -0700941 response->res,
942 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800943 return CreatePIDRet::fail;
944 }
Ed Tanousb9d36b42022-02-26 21:42:46 -0800945 output.emplace_back("Zones", std::move(zonesStr));
James Feist5f2caae2018-12-12 14:08:25 -0800946 }
Ed Tanousafb9ee02022-12-21 11:59:17 -0800947
948 if (inputs)
James Feist5f2caae2018-12-12 14:08:25 -0800949 {
Ed Tanousafb9ee02022-12-21 11:59:17 -0800950 for (std::string& value : *inputs)
James Feist83ff9ab2018-08-31 10:18:24 -0700951 {
Ed Tanousafb9ee02022-12-21 11:59:17 -0800952 std::replace(value.begin(), value.end(), '_', ' ');
James Feist83ff9ab2018-08-31 10:18:24 -0700953 }
Ed Tanousafb9ee02022-12-21 11:59:17 -0800954 output.emplace_back("Inputs", *inputs);
955 }
956
957 if (outputs)
958 {
959 for (std::string& value : *outputs)
960 {
961 std::replace(value.begin(), value.end(), '_', ' ');
962 }
963 output.emplace_back("Outputs", *outputs);
James Feist5f2caae2018-12-12 14:08:25 -0800964 }
James Feist83ff9ab2018-08-31 10:18:24 -0700965
James Feistb943aae2019-07-11 16:33:56 -0700966 if (setpointOffset)
967 {
968 // translate between redfish and dbus names
969 if (*setpointOffset == "UpperThresholdNonCritical")
970 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800971 output.emplace_back("SetPointOffset", "WarningLow");
James Feistb943aae2019-07-11 16:33:56 -0700972 }
973 else if (*setpointOffset == "LowerThresholdNonCritical")
974 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800975 output.emplace_back("SetPointOffset", "WarningHigh");
James Feistb943aae2019-07-11 16:33:56 -0700976 }
977 else if (*setpointOffset == "LowerThresholdCritical")
978 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800979 output.emplace_back("SetPointOffset", "CriticalLow");
James Feistb943aae2019-07-11 16:33:56 -0700980 }
981 else if (*setpointOffset == "UpperThresholdCritical")
982 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800983 output.emplace_back("SetPointOffset", "CriticalHigh");
James Feistb943aae2019-07-11 16:33:56 -0700984 }
985 else
986 {
987 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
988 << *setpointOffset;
Ed Tanousace85d62021-10-26 12:45:59 -0700989 messages::propertyValueNotInList(response->res, it.key(),
990 "SetPointOffset");
James Feistb943aae2019-07-11 16:33:56 -0700991 return CreatePIDRet::fail;
992 }
993 }
994
James Feist5f2caae2018-12-12 14:08:25 -0800995 // doubles
996 for (const auto& pairs : doubles)
997 {
998 if (!pairs.second)
James Feist83ff9ab2018-08-31 10:18:24 -0700999 {
James Feist5f2caae2018-12-12 14:08:25 -08001000 continue;
James Feist83ff9ab2018-08-31 10:18:24 -07001001 }
James Feist5f2caae2018-12-12 14:08:25 -08001002 BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
Ed Tanousb9d36b42022-02-26 21:42:46 -08001003 output.emplace_back(pairs.first, *pairs.second);
James Feist83ff9ab2018-08-31 10:18:24 -07001004 }
1005 }
James Feist5f2caae2018-12-12 14:08:25 -08001006
James Feist83ff9ab2018-08-31 10:18:24 -07001007 else if (type == "FanZones")
1008 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001009 output.emplace_back("Type", "Pid.Zone");
James Feist83ff9ab2018-08-31 10:18:24 -07001010
James Feist5f2caae2018-12-12 14:08:25 -08001011 std::optional<nlohmann::json> chassisContainer;
1012 std::optional<double> failSafePercent;
James Feistd3ec07f2019-02-25 14:51:15 -08001013 std::optional<double> minThermalOutput;
James Feistb6baeaa2019-02-21 10:41:40 -08001014 if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
James Feist5f2caae2018-12-12 14:08:25 -08001015 chassisContainer, "FailSafePercent",
James Feistd3ec07f2019-02-25 14:51:15 -08001016 failSafePercent, "MinThermalOutput",
1017 minThermalOutput))
James Feist83ff9ab2018-08-31 10:18:24 -07001018 {
James Feist5f2caae2018-12-12 14:08:25 -08001019 return CreatePIDRet::fail;
1020 }
James Feist83ff9ab2018-08-31 10:18:24 -07001021
James Feist5f2caae2018-12-12 14:08:25 -08001022 if (chassisContainer)
1023 {
James Feist5f2caae2018-12-12 14:08:25 -08001024 std::string chassisId;
1025 if (!redfish::json_util::readJson(*chassisContainer, response->res,
1026 "@odata.id", chassisId))
James Feist83ff9ab2018-08-31 10:18:24 -07001027 {
James Feist83ff9ab2018-08-31 10:18:24 -07001028 return CreatePIDRet::fail;
1029 }
James Feist5f2caae2018-12-12 14:08:25 -08001030
AppaRao Puli717794d2019-10-18 22:54:53 +05301031 // /redfish/v1/chassis/chassis_name/
James Feist5f2caae2018-12-12 14:08:25 -08001032 if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
1033 {
1034 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
Ed Tanousace85d62021-10-26 12:45:59 -07001035 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001036 response->res,
1037 boost::urls::format("/redfish/v1/Chassis/{}", chassisId));
James Feist5f2caae2018-12-12 14:08:25 -08001038 return CreatePIDRet::fail;
1039 }
1040 }
James Feistd3ec07f2019-02-25 14:51:15 -08001041 if (minThermalOutput)
James Feist5f2caae2018-12-12 14:08:25 -08001042 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001043 output.emplace_back("MinThermalOutput", *minThermalOutput);
James Feist5f2caae2018-12-12 14:08:25 -08001044 }
1045 if (failSafePercent)
1046 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001047 output.emplace_back("FailSafePercent", *failSafePercent);
James Feist5f2caae2018-12-12 14:08:25 -08001048 }
1049 }
1050 else if (type == "StepwiseControllers")
1051 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001052 output.emplace_back("Type", "Stepwise");
James Feist5f2caae2018-12-12 14:08:25 -08001053
1054 std::optional<std::vector<nlohmann::json>> zones;
1055 std::optional<std::vector<nlohmann::json>> steps;
1056 std::optional<std::vector<std::string>> inputs;
1057 std::optional<double> positiveHysteresis;
1058 std::optional<double> negativeHysteresis;
James Feistc33a90e2019-03-01 10:17:44 -08001059 std::optional<std::string> direction; // upper clipping curve vs lower
James Feist5f2caae2018-12-12 14:08:25 -08001060 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -08001061 it.value(), response->res, "Zones", zones, "Steps", steps,
1062 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
James Feistc33a90e2019-03-01 10:17:44 -08001063 "NegativeHysteresis", negativeHysteresis, "Direction",
1064 direction))
James Feist5f2caae2018-12-12 14:08:25 -08001065 {
James Feist5f2caae2018-12-12 14:08:25 -08001066 return CreatePIDRet::fail;
1067 }
1068
1069 if (zones)
1070 {
James Feistb6baeaa2019-02-21 10:41:40 -08001071 std::vector<std::string> zonesStrs;
1072 if (!getZonesFromJsonReq(response, *zones, zonesStrs))
James Feist5f2caae2018-12-12 14:08:25 -08001073 {
Gunnar Millsa0744d32020-11-09 15:40:45 -06001074 BMCWEB_LOG_ERROR << "Illegal Zones";
James Feist5f2caae2018-12-12 14:08:25 -08001075 return CreatePIDRet::fail;
1076 }
James Feistb6baeaa2019-02-21 10:41:40 -08001077 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -08001078 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -08001079 {
1080 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
Ed Tanousace85d62021-10-26 12:45:59 -07001081 messages::invalidObject(
Ed Tanousef4c65b2023-04-24 15:28:50 -07001082 response->res,
1083 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -08001084 return CreatePIDRet::fail;
1085 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001086 output.emplace_back("Zones", std::move(zonesStrs));
James Feist5f2caae2018-12-12 14:08:25 -08001087 }
1088 if (steps)
1089 {
1090 std::vector<double> readings;
1091 std::vector<double> outputs;
1092 for (auto& step : *steps)
1093 {
Ed Tanous543f4402022-01-06 13:12:53 -08001094 double target = 0.0;
1095 double out = 0.0;
James Feist5f2caae2018-12-12 14:08:25 -08001096
1097 if (!redfish::json_util::readJson(step, response->res, "Target",
Ed Tanous23a21a12020-07-25 04:45:05 +00001098 target, "Output", out))
James Feist5f2caae2018-12-12 14:08:25 -08001099 {
James Feist5f2caae2018-12-12 14:08:25 -08001100 return CreatePIDRet::fail;
1101 }
1102 readings.emplace_back(target);
Ed Tanous23a21a12020-07-25 04:45:05 +00001103 outputs.emplace_back(out);
James Feist5f2caae2018-12-12 14:08:25 -08001104 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001105 output.emplace_back("Reading", std::move(readings));
1106 output.emplace_back("Output", std::move(outputs));
James Feist5f2caae2018-12-12 14:08:25 -08001107 }
1108 if (inputs)
1109 {
1110 for (std::string& value : *inputs)
1111 {
Ed Tanousa170f272022-06-30 21:53:27 -07001112 std::replace(value.begin(), value.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -08001113 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001114 output.emplace_back("Inputs", std::move(*inputs));
James Feist5f2caae2018-12-12 14:08:25 -08001115 }
1116 if (negativeHysteresis)
1117 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001118 output.emplace_back("NegativeHysteresis", *negativeHysteresis);
James Feist5f2caae2018-12-12 14:08:25 -08001119 }
1120 if (positiveHysteresis)
1121 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001122 output.emplace_back("PositiveHysteresis", *positiveHysteresis);
James Feist83ff9ab2018-08-31 10:18:24 -07001123 }
James Feistc33a90e2019-03-01 10:17:44 -08001124 if (direction)
1125 {
1126 constexpr const std::array<const char*, 2> allowedDirections = {
1127 "Ceiling", "Floor"};
1128 if (std::find(allowedDirections.begin(), allowedDirections.end(),
1129 *direction) == allowedDirections.end())
1130 {
1131 messages::propertyValueTypeError(response->res, "Direction",
1132 *direction);
1133 return CreatePIDRet::fail;
1134 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001135 output.emplace_back("Class", *direction);
James Feistc33a90e2019-03-01 10:17:44 -08001136 }
James Feist83ff9ab2018-08-31 10:18:24 -07001137 }
1138 else
1139 {
Gunnar Millsa0744d32020-11-09 15:40:45 -06001140 BMCWEB_LOG_ERROR << "Illegal Type " << type;
Jason M. Bills35a62c72018-10-09 12:45:45 -07001141 messages::propertyUnknown(response->res, type);
James Feist83ff9ab2018-08-31 10:18:24 -07001142 return CreatePIDRet::fail;
1143 }
1144 return CreatePIDRet::patch;
1145}
James Feist73df0db2019-03-25 15:29:35 -07001146struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
1147{
Ed Tanous6936afe2022-09-08 15:10:39 -07001148 struct CompletionValues
1149 {
1150 std::vector<std::string> supportedProfiles;
1151 std::string currentProfile;
1152 dbus::utility::MapperGetSubTreeResponse subtree;
1153 };
James Feist73df0db2019-03-25 15:29:35 -07001154
Ed Tanous4e23a442022-06-06 09:57:26 -07001155 explicit GetPIDValues(
1156 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
Ed Tanous23a21a12020-07-25 04:45:05 +00001157 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001158
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001159 {}
James Feist73df0db2019-03-25 15:29:35 -07001160
1161 void run()
1162 {
1163 std::shared_ptr<GetPIDValues> self = shared_from_this();
1164
1165 // get all configurations
George Liue99073f2022-12-09 11:06:16 +08001166 constexpr std::array<std::string_view, 4> interfaces = {
1167 pidConfigurationIface, pidZoneConfigurationIface,
1168 objectManagerIface, stepwiseConfigurationIface};
1169 dbus::utility::getSubTree(
1170 "/", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001171 [self](
George Liue99073f2022-12-09 11:06:16 +08001172 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001173 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001174 if (ec)
1175 {
1176 BMCWEB_LOG_ERROR << ec;
1177 messages::internalError(self->asyncResp->res);
1178 return;
1179 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001180 self->complete.subtree = subtreeLocal;
George Liue99073f2022-12-09 11:06:16 +08001181 });
James Feist73df0db2019-03-25 15:29:35 -07001182
1183 // at the same time get the selected profile
George Liue99073f2022-12-09 11:06:16 +08001184 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1185 thermalModeIface};
1186 dbus::utility::getSubTree(
1187 "/", 0, thermalModeIfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001188 [self](
George Liue99073f2022-12-09 11:06:16 +08001189 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001190 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001191 if (ec || subtreeLocal.empty())
1192 {
1193 return;
1194 }
1195 if (subtreeLocal[0].second.size() != 1)
1196 {
1197 // invalid mapper response, should never happen
1198 BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
1199 messages::internalError(self->asyncResp->res);
1200 return;
1201 }
1202
1203 const std::string& path = subtreeLocal[0].first;
1204 const std::string& owner = subtreeLocal[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001205
1206 sdbusplus::asio::getAllProperties(
1207 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous002d39b2022-05-31 08:59:27 -07001208 [path, owner,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001209 self](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001210 const dbus::utility::DBusPropertiesMap& resp) {
1211 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001212 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001213 BMCWEB_LOG_ERROR
1214 << "GetPIDValues: Can't get thermalModeIface " << path;
James Feist73df0db2019-03-25 15:29:35 -07001215 messages::internalError(self->asyncResp->res);
1216 return;
1217 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001218
Ed Tanous002d39b2022-05-31 08:59:27 -07001219 const std::string* current = nullptr;
1220 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001221
1222 const bool success = sdbusplus::unpackPropertiesNoThrow(
1223 dbus_utils::UnpackErrorPrinter(), resp, "Current", current,
1224 "Supported", supported);
1225
1226 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001227 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001228 messages::internalError(self->asyncResp->res);
1229 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001230 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001231
Ed Tanous002d39b2022-05-31 08:59:27 -07001232 if (current == nullptr || supported == nullptr)
1233 {
1234 BMCWEB_LOG_ERROR
1235 << "GetPIDValues: thermal mode iface invalid " << path;
1236 messages::internalError(self->asyncResp->res);
1237 return;
1238 }
Ed Tanous6936afe2022-09-08 15:10:39 -07001239 self->complete.currentProfile = *current;
1240 self->complete.supportedProfiles = *supported;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001241 });
George Liue99073f2022-12-09 11:06:16 +08001242 });
James Feist73df0db2019-03-25 15:29:35 -07001243 }
1244
Ed Tanous6936afe2022-09-08 15:10:39 -07001245 static void
1246 processingComplete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1247 const CompletionValues& completion)
James Feist73df0db2019-03-25 15:29:35 -07001248 {
1249 if (asyncResp->res.result() != boost::beast::http::status::ok)
1250 {
1251 return;
1252 }
1253 // create map of <connection, path to objMgr>>
Ed Tanous6936afe2022-09-08 15:10:39 -07001254 boost::container::flat_map<
1255 std::string, std::string, std::less<>,
1256 std::vector<std::pair<std::string, std::string>>>
1257 objectMgrPaths;
1258 boost::container::flat_set<std::string, std::less<>,
1259 std::vector<std::string>>
1260 calledConnections;
1261 for (const auto& pathGroup : completion.subtree)
James Feist73df0db2019-03-25 15:29:35 -07001262 {
1263 for (const auto& connectionGroup : pathGroup.second)
1264 {
1265 auto findConnection =
1266 calledConnections.find(connectionGroup.first);
1267 if (findConnection != calledConnections.end())
1268 {
1269 break;
1270 }
1271 for (const std::string& interface : connectionGroup.second)
1272 {
1273 if (interface == objectManagerIface)
1274 {
1275 objectMgrPaths[connectionGroup.first] = pathGroup.first;
1276 }
1277 // this list is alphabetical, so we
1278 // should have found the objMgr by now
1279 if (interface == pidConfigurationIface ||
1280 interface == pidZoneConfigurationIface ||
1281 interface == stepwiseConfigurationIface)
1282 {
1283 auto findObjMgr =
1284 objectMgrPaths.find(connectionGroup.first);
1285 if (findObjMgr == objectMgrPaths.end())
1286 {
1287 BMCWEB_LOG_DEBUG << connectionGroup.first
1288 << "Has no Object Manager";
1289 continue;
1290 }
1291
1292 calledConnections.insert(connectionGroup.first);
1293
1294 asyncPopulatePid(findObjMgr->first, findObjMgr->second,
Ed Tanous6936afe2022-09-08 15:10:39 -07001295 completion.currentProfile,
1296 completion.supportedProfiles,
James Feist73df0db2019-03-25 15:29:35 -07001297 asyncResp);
1298 break;
1299 }
1300 }
1301 }
1302 }
1303 }
1304
Ed Tanous6936afe2022-09-08 15:10:39 -07001305 ~GetPIDValues()
1306 {
1307 boost::asio::post(crow::connections::systemBus->get_io_context(),
1308 std::bind_front(&processingComplete, asyncResp,
1309 std::move(complete)));
1310 }
1311
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001312 GetPIDValues(const GetPIDValues&) = delete;
1313 GetPIDValues(GetPIDValues&&) = delete;
1314 GetPIDValues& operator=(const GetPIDValues&) = delete;
1315 GetPIDValues& operator=(GetPIDValues&&) = delete;
1316
zhanghch058d1b46d2021-04-01 11:18:24 +08001317 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Ed Tanous6936afe2022-09-08 15:10:39 -07001318 CompletionValues complete;
James Feist73df0db2019-03-25 15:29:35 -07001319};
1320
1321struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
1322{
zhanghch058d1b46d2021-04-01 11:18:24 +08001323 SetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
James Feist73df0db2019-03-25 15:29:35 -07001324 nlohmann::json& data) :
Ed Tanous271584a2019-07-09 16:24:22 -07001325 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001326 {
James Feist73df0db2019-03-25 15:29:35 -07001327 std::optional<nlohmann::json> pidControllers;
1328 std::optional<nlohmann::json> fanControllers;
1329 std::optional<nlohmann::json> fanZones;
1330 std::optional<nlohmann::json> stepwiseControllers;
1331
1332 if (!redfish::json_util::readJson(
1333 data, asyncResp->res, "PidControllers", pidControllers,
1334 "FanControllers", fanControllers, "FanZones", fanZones,
1335 "StepwiseControllers", stepwiseControllers, "Profile", profile))
1336 {
James Feist73df0db2019-03-25 15:29:35 -07001337 return;
1338 }
1339 configuration.emplace_back("PidControllers", std::move(pidControllers));
1340 configuration.emplace_back("FanControllers", std::move(fanControllers));
1341 configuration.emplace_back("FanZones", std::move(fanZones));
1342 configuration.emplace_back("StepwiseControllers",
1343 std::move(stepwiseControllers));
1344 }
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001345
1346 SetPIDValues(const SetPIDValues&) = delete;
1347 SetPIDValues(SetPIDValues&&) = delete;
1348 SetPIDValues& operator=(const SetPIDValues&) = delete;
1349 SetPIDValues& operator=(SetPIDValues&&) = delete;
1350
James Feist73df0db2019-03-25 15:29:35 -07001351 void run()
1352 {
1353 if (asyncResp->res.result() != boost::beast::http::status::ok)
1354 {
1355 return;
1356 }
1357
1358 std::shared_ptr<SetPIDValues> self = shared_from_this();
1359
1360 // todo(james): might make sense to do a mapper call here if this
1361 // interface gets more traction
1362 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001363 [self](const boost::system::error_code& ec,
Ed Tanous914e2d52022-01-07 11:38:34 -08001364 const dbus::utility::ManagedObjectType& mObj) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001365 if (ec)
1366 {
1367 BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
1368 messages::internalError(self->asyncResp->res);
1369 return;
1370 }
1371 const std::array<const char*, 3> configurations = {
1372 pidConfigurationIface, pidZoneConfigurationIface,
1373 stepwiseConfigurationIface};
James Feiste69d9de2020-02-07 12:23:27 -08001374
Ed Tanous002d39b2022-05-31 08:59:27 -07001375 for (const auto& [path, object] : mObj)
1376 {
1377 for (const auto& [interface, _] : object)
James Feiste69d9de2020-02-07 12:23:27 -08001378 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001379 if (std::find(configurations.begin(), configurations.end(),
1380 interface) != configurations.end())
James Feiste69d9de2020-02-07 12:23:27 -08001381 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001382 self->objectCount++;
1383 break;
James Feiste69d9de2020-02-07 12:23:27 -08001384 }
James Feiste69d9de2020-02-07 12:23:27 -08001385 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001386 }
1387 self->managedObj = mObj;
James Feist73df0db2019-03-25 15:29:35 -07001388 },
Nan Zhouc106b672022-09-20 22:35:31 +00001389 "xyz.openbmc_project.EntityManager",
1390 "/xyz/openbmc_project/inventory", objectManagerIface,
James Feist73df0db2019-03-25 15:29:35 -07001391 "GetManagedObjects");
1392
1393 // at the same time get the profile information
George Liue99073f2022-12-09 11:06:16 +08001394 constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1395 thermalModeIface};
1396 dbus::utility::getSubTree(
1397 "/", 0, thermalModeIfaces,
1398 [self](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001399 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001400 if (ec || subtree.empty())
1401 {
1402 return;
1403 }
1404 if (subtree[0].second.empty())
1405 {
1406 // invalid mapper response, should never happen
1407 BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
1408 messages::internalError(self->asyncResp->res);
1409 return;
1410 }
1411
1412 const std::string& path = subtree[0].first;
1413 const std::string& owner = subtree[0].second[0].first;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001414 sdbusplus::asio::getAllProperties(
1415 *crow::connections::systemBus, owner, path, thermalModeIface,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001416 [self, path, owner](const boost::system::error_code& ec2,
Ed Tanous002d39b2022-05-31 08:59:27 -07001417 const dbus::utility::DBusPropertiesMap& r) {
1418 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001419 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001420 BMCWEB_LOG_ERROR
1421 << "SetPIDValues: Can't get thermalModeIface " << path;
James Feist73df0db2019-03-25 15:29:35 -07001422 messages::internalError(self->asyncResp->res);
1423 return;
1424 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001425 const std::string* current = nullptr;
1426 const std::vector<std::string>* supported = nullptr;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001427
1428 const bool success = sdbusplus::unpackPropertiesNoThrow(
1429 dbus_utils::UnpackErrorPrinter(), r, "Current", current,
1430 "Supported", supported);
1431
1432 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001433 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001434 messages::internalError(self->asyncResp->res);
1435 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001436 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001437
Ed Tanous002d39b2022-05-31 08:59:27 -07001438 if (current == nullptr || supported == nullptr)
1439 {
1440 BMCWEB_LOG_ERROR
1441 << "SetPIDValues: thermal mode iface invalid " << path;
1442 messages::internalError(self->asyncResp->res);
1443 return;
1444 }
1445 self->currentProfile = *current;
1446 self->supportedProfiles = *supported;
1447 self->profileConnection = owner;
1448 self->profilePath = path;
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02001449 });
George Liue99073f2022-12-09 11:06:16 +08001450 });
James Feist73df0db2019-03-25 15:29:35 -07001451 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001452 void pidSetDone()
James Feist73df0db2019-03-25 15:29:35 -07001453 {
1454 if (asyncResp->res.result() != boost::beast::http::status::ok)
1455 {
1456 return;
1457 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001458 std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001459 if (profile)
1460 {
1461 if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
1462 *profile) == supportedProfiles.end())
1463 {
1464 messages::actionParameterUnknown(response->res, "Profile",
1465 *profile);
1466 return;
1467 }
1468 currentProfile = *profile;
1469 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001470 [response](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001471 if (ec)
1472 {
1473 BMCWEB_LOG_ERROR << "Error patching profile" << ec;
1474 messages::internalError(response->res);
1475 }
James Feist73df0db2019-03-25 15:29:35 -07001476 },
1477 profileConnection, profilePath,
1478 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
Ed Tanous168e20c2021-12-13 14:39:53 -08001479 "Current", dbus::utility::DbusVariantType(*profile));
James Feist73df0db2019-03-25 15:29:35 -07001480 }
1481
1482 for (auto& containerPair : configuration)
1483 {
1484 auto& container = containerPair.second;
1485 if (!container)
1486 {
1487 continue;
1488 }
James Feist6ee7f772020-02-06 16:25:27 -08001489 BMCWEB_LOG_DEBUG << *container;
1490
Ed Tanous02cad962022-06-30 16:50:15 -07001491 const std::string& type = containerPair.first;
James Feist73df0db2019-03-25 15:29:35 -07001492
1493 for (nlohmann::json::iterator it = container->begin();
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301494 it != container->end(); ++it)
James Feist73df0db2019-03-25 15:29:35 -07001495 {
1496 const auto& name = it.key();
Potin Laicddbf3d2023-02-14 14:28:58 +08001497 std::string dbusObjName = name;
1498 std::replace(dbusObjName.begin(), dbusObjName.end(), ' ', '_');
James Feist6ee7f772020-02-06 16:25:27 -08001499 BMCWEB_LOG_DEBUG << "looking for " << name;
1500
Patrick Williams89492a12023-05-10 07:51:34 -05001501 auto pathItr = std::find_if(managedObj.begin(),
1502 managedObj.end(),
1503 [&dbusObjName](const auto& obj) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001504 return boost::algorithm::ends_with(obj.first.str,
Potin Laicddbf3d2023-02-14 14:28:58 +08001505 "/" + dbusObjName);
Patrick Williams89492a12023-05-10 07:51:34 -05001506 });
Ed Tanousb9d36b42022-02-26 21:42:46 -08001507 dbus::utility::DBusPropertiesMap output;
James Feist73df0db2019-03-25 15:29:35 -07001508
1509 output.reserve(16); // The pid interface length
1510
1511 // determines if we're patching entity-manager or
1512 // creating a new object
1513 bool createNewObject = (pathItr == managedObj.end());
James Feist6ee7f772020-02-06 16:25:27 -08001514 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
1515
James Feist73df0db2019-03-25 15:29:35 -07001516 std::string iface;
Ed Tanousea2b6702022-03-07 16:48:38 -08001517 if (!createNewObject)
James Feist73df0db2019-03-25 15:29:35 -07001518 {
Potin Lai8be2b5b2022-11-22 13:27:16 +08001519 bool findInterface = false;
Ed Tanousea2b6702022-03-07 16:48:38 -08001520 for (const auto& interface : pathItr->second)
James Feist73df0db2019-03-25 15:29:35 -07001521 {
Ed Tanousea2b6702022-03-07 16:48:38 -08001522 if (interface.first == pidConfigurationIface)
1523 {
1524 if (type == "PidControllers" ||
1525 type == "FanControllers")
1526 {
1527 iface = pidConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001528 findInterface = true;
1529 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001530 }
1531 }
1532 else if (interface.first == pidZoneConfigurationIface)
1533 {
1534 if (type == "FanZones")
1535 {
1536 iface = pidConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001537 findInterface = true;
1538 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001539 }
1540 }
1541 else if (interface.first == stepwiseConfigurationIface)
1542 {
1543 if (type == "StepwiseControllers")
1544 {
1545 iface = stepwiseConfigurationIface;
Potin Lai8be2b5b2022-11-22 13:27:16 +08001546 findInterface = true;
1547 break;
Ed Tanousea2b6702022-03-07 16:48:38 -08001548 }
1549 }
James Feist73df0db2019-03-25 15:29:35 -07001550 }
Potin Lai8be2b5b2022-11-22 13:27:16 +08001551
1552 // create new object if interface not found
1553 if (!findInterface)
1554 {
1555 createNewObject = true;
1556 }
James Feist73df0db2019-03-25 15:29:35 -07001557 }
James Feist6ee7f772020-02-06 16:25:27 -08001558
1559 if (createNewObject && it.value() == nullptr)
1560 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -05001561 // can't delete a non-existent object
Ed Tanous1668ce62022-02-07 23:44:31 -08001562 messages::propertyValueNotInList(response->res,
1563 it.value().dump(), name);
James Feist6ee7f772020-02-06 16:25:27 -08001564 continue;
1565 }
1566
1567 std::string path;
1568 if (pathItr != managedObj.end())
1569 {
1570 path = pathItr->first.str;
1571 }
1572
James Feist73df0db2019-03-25 15:29:35 -07001573 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
James Feiste69d9de2020-02-07 12:23:27 -08001574
1575 // arbitrary limit to avoid attacks
1576 constexpr const size_t controllerLimit = 500;
James Feist14b0b8d2020-02-12 11:52:07 -08001577 if (createNewObject && objectCount >= controllerLimit)
James Feiste69d9de2020-02-07 12:23:27 -08001578 {
1579 messages::resourceExhaustion(response->res, type);
1580 continue;
1581 }
Ed Tanousa170f272022-06-30 21:53:27 -07001582 std::string escaped = name;
1583 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1584 output.emplace_back("Name", escaped);
James Feist73df0db2019-03-25 15:29:35 -07001585
1586 std::string chassis;
1587 CreatePIDRet ret = createPidInterface(
James Feist6ee7f772020-02-06 16:25:27 -08001588 response, type, it, path, managedObj, createNewObject,
1589 output, chassis, currentProfile);
James Feist73df0db2019-03-25 15:29:35 -07001590 if (ret == CreatePIDRet::fail)
1591 {
1592 return;
1593 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001594 if (ret == CreatePIDRet::del)
James Feist73df0db2019-03-25 15:29:35 -07001595 {
1596 continue;
1597 }
1598
1599 if (!createNewObject)
1600 {
1601 for (const auto& property : output)
1602 {
1603 crow::connections::systemBus->async_method_call(
1604 [response,
1605 propertyName{std::string(property.first)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001606 const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001607 if (ec)
1608 {
1609 BMCWEB_LOG_ERROR << "Error patching "
1610 << propertyName << ": " << ec;
1611 messages::internalError(response->res);
1612 return;
1613 }
1614 messages::success(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001615 },
James Feist6ee7f772020-02-06 16:25:27 -08001616 "xyz.openbmc_project.EntityManager", path,
James Feist73df0db2019-03-25 15:29:35 -07001617 "org.freedesktop.DBus.Properties", "Set", iface,
1618 property.first, property.second);
1619 }
1620 }
1621 else
1622 {
1623 if (chassis.empty())
1624 {
1625 BMCWEB_LOG_ERROR << "Failed to get chassis from config";
Ed Tanousace85d62021-10-26 12:45:59 -07001626 messages::internalError(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001627 return;
1628 }
1629
1630 bool foundChassis = false;
1631 for (const auto& obj : managedObj)
1632 {
1633 if (boost::algorithm::ends_with(obj.first.str, chassis))
1634 {
1635 chassis = obj.first.str;
1636 foundChassis = true;
1637 break;
1638 }
1639 }
1640 if (!foundChassis)
1641 {
1642 BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
1643 messages::resourceMissingAtURI(
Ed Tanousace85d62021-10-26 12:45:59 -07001644 response->res,
Ed Tanousef4c65b2023-04-24 15:28:50 -07001645 boost::urls::format("/redfish/v1/Chassis/{}",
1646 chassis));
James Feist73df0db2019-03-25 15:29:35 -07001647 return;
1648 }
1649
1650 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001651 [response](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001652 if (ec)
1653 {
1654 BMCWEB_LOG_ERROR << "Error Adding Pid Object "
1655 << ec;
1656 messages::internalError(response->res);
1657 return;
1658 }
1659 messages::success(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001660 },
1661 "xyz.openbmc_project.EntityManager", chassis,
1662 "xyz.openbmc_project.AddObject", "AddObject", output);
1663 }
1664 }
1665 }
1666 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001667
1668 ~SetPIDValues()
1669 {
1670 try
1671 {
1672 pidSetDone();
1673 }
1674 catch (...)
1675 {
1676 BMCWEB_LOG_CRITICAL << "pidSetDone threw exception";
1677 }
1678 }
1679
zhanghch058d1b46d2021-04-01 11:18:24 +08001680 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001681 std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
1682 configuration;
1683 std::optional<std::string> profile;
1684 dbus::utility::ManagedObjectType managedObj;
1685 std::vector<std::string> supportedProfiles;
1686 std::string currentProfile;
1687 std::string profileConnection;
1688 std::string profilePath;
James Feist14b0b8d2020-02-12 11:52:07 -08001689 size_t objectCount = 0;
James Feist73df0db2019-03-25 15:29:35 -07001690};
James Feist83ff9ab2018-08-31 10:18:24 -07001691
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001692/**
1693 * @brief Retrieves BMC manager location data over DBus
1694 *
Ed Tanousac106bf2023-06-07 09:24:59 -07001695 * @param[in] asyncResp Shared pointer for completing asynchronous calls
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001696 * @param[in] connectionName - service name
1697 * @param[in] path - object path
1698 * @return none
1699 */
Ed Tanousac106bf2023-06-07 09:24:59 -07001700inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001701 const std::string& connectionName,
1702 const std::string& path)
1703{
1704 BMCWEB_LOG_DEBUG << "Get BMC manager Location data.";
1705
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001706 sdbusplus::asio::getProperty<std::string>(
1707 *crow::connections::systemBus, connectionName, path,
1708 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Ed Tanousac106bf2023-06-07 09:24:59 -07001709 [asyncResp](const boost::system::error_code& ec,
1710 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001711 if (ec)
1712 {
1713 BMCWEB_LOG_DEBUG << "DBUS response error for "
1714 "Location";
Ed Tanousac106bf2023-06-07 09:24:59 -07001715 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001716 return;
1717 }
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001718
Ed Tanousac106bf2023-06-07 09:24:59 -07001719 asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07001720 property;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001721 });
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001722}
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001723// avoid name collision systems.hpp
1724inline void
Ed Tanousac106bf2023-06-07 09:24:59 -07001725 managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001726{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001727 BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time";
Ed Tanous52cc1122020-07-18 13:51:21 -07001728
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001729 sdbusplus::asio::getProperty<uint64_t>(
1730 *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
1731 "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
1732 "LastRebootTime",
Ed Tanousac106bf2023-06-07 09:24:59 -07001733 [asyncResp](const boost::system::error_code& ec,
1734 const uint64_t lastResetTime) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001735 if (ec)
1736 {
1737 BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
1738 return;
1739 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001740
Ed Tanous002d39b2022-05-31 08:59:27 -07001741 // LastRebootTime is epoch time, in milliseconds
1742 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
1743 uint64_t lastResetTimeStamp = lastResetTime / 1000;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001744
Ed Tanous002d39b2022-05-31 08:59:27 -07001745 // Convert to ISO 8601 standard
Ed Tanousac106bf2023-06-07 09:24:59 -07001746 asyncResp->res.jsonValue["LastResetTime"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001747 redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001748 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001749}
1750
1751/**
1752 * @brief Set the running firmware image
1753 *
Ed Tanousac106bf2023-06-07 09:24:59 -07001754 * @param[i,o] asyncResp - Async response object
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001755 * @param[i] runningFirmwareTarget - Image to make the running image
1756 *
1757 * @return void
1758 */
1759inline void
Ed Tanousac106bf2023-06-07 09:24:59 -07001760 setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001761 const std::string& runningFirmwareTarget)
1762{
1763 // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1764 std::string::size_type idPos = runningFirmwareTarget.rfind('/');
1765 if (idPos == std::string::npos)
1766 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001767 messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001768 "@odata.id");
1769 BMCWEB_LOG_DEBUG << "Can't parse firmware ID!";
1770 return;
1771 }
1772 idPos++;
1773 if (idPos >= runningFirmwareTarget.size())
1774 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001775 messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001776 "@odata.id");
1777 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
1778 return;
1779 }
1780 std::string firmwareId = runningFirmwareTarget.substr(idPos);
1781
1782 // Make sure the image is valid before setting priority
1783 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -07001784 [asyncResp, firmwareId,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001785 runningFirmwareTarget](const boost::system::error_code& ec,
Ed Tanous711ac7a2021-12-20 09:34:41 -08001786 dbus::utility::ManagedObjectType& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001787 if (ec)
1788 {
1789 BMCWEB_LOG_DEBUG << "D-Bus response error getting objects.";
Ed Tanousac106bf2023-06-07 09:24:59 -07001790 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001791 return;
1792 }
1793
1794 if (subtree.empty())
1795 {
1796 BMCWEB_LOG_DEBUG << "Can't find image!";
Ed Tanousac106bf2023-06-07 09:24:59 -07001797 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07001798 return;
1799 }
1800
1801 bool foundImage = false;
Ed Tanous02cad962022-06-30 16:50:15 -07001802 for (const auto& object : subtree)
Ed Tanous002d39b2022-05-31 08:59:27 -07001803 {
1804 const std::string& path =
1805 static_cast<const std::string&>(object.first);
1806 std::size_t idPos2 = path.rfind('/');
1807
1808 if (idPos2 == std::string::npos)
1809 {
1810 continue;
1811 }
1812
1813 idPos2++;
1814 if (idPos2 >= path.size())
1815 {
1816 continue;
1817 }
1818
1819 if (path.substr(idPos2) == firmwareId)
1820 {
1821 foundImage = true;
1822 break;
1823 }
1824 }
1825
1826 if (!foundImage)
1827 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001828 messages::propertyValueNotInList(
1829 asyncResp->res, runningFirmwareTarget, "@odata.id");
Ed Tanous002d39b2022-05-31 08:59:27 -07001830 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
1831 return;
1832 }
1833
1834 BMCWEB_LOG_DEBUG << "Setting firmware version " << firmwareId
1835 << " to priority 0.";
1836
1837 // Only support Immediate
1838 // An addition could be a Redfish Setting like
1839 // ActiveSoftwareImageApplyTime and support OnReset
1840 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -07001841 [asyncResp](const boost::system::error_code& ec2) {
Ed Tanous8a592812022-06-04 09:06:59 -07001842 if (ec2)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001843 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001844 BMCWEB_LOG_DEBUG << "D-Bus response error setting.";
Ed Tanousac106bf2023-06-07 09:24:59 -07001845 messages::internalError(asyncResp->res);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001846 return;
1847 }
Ed Tanousac106bf2023-06-07 09:24:59 -07001848 doBMCGracefulRestart(asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001849 },
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001850
Ed Tanous002d39b2022-05-31 08:59:27 -07001851 "xyz.openbmc_project.Software.BMC.Updater",
1852 "/xyz/openbmc_project/software/" + firmwareId,
1853 "org.freedesktop.DBus.Properties", "Set",
1854 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1855 dbus::utility::DbusVariantType(static_cast<uint8_t>(0)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001856 },
1857 "xyz.openbmc_project.Software.BMC.Updater",
1858 "/xyz/openbmc_project/software", "org.freedesktop.DBus.ObjectManager",
1859 "GetManagedObjects");
1860}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001861
Ed Tanousac106bf2023-06-07 09:24:59 -07001862inline void setDateTime(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001863 std::string datetime)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001864{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001865 BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001866
Ed Tanousc2e32002023-01-07 22:05:08 -08001867 std::optional<redfish::time_utils::usSinceEpoch> us =
1868 redfish::time_utils::dateStringToEpoch(datetime);
1869 if (!us)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001870 {
Ed Tanousac106bf2023-06-07 09:24:59 -07001871 messages::propertyValueFormatError(asyncResp->res, datetime,
1872 "DateTime");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001873 return;
1874 }
Ed Tanousc2e32002023-01-07 22:05:08 -08001875 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -07001876 [asyncResp{std::move(asyncResp)},
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001877 datetime{std::move(datetime)}](const boost::system::error_code& ec) {
Ed Tanousc2e32002023-01-07 22:05:08 -08001878 if (ec)
1879 {
1880 BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1881 "DBUS response error "
1882 << ec;
Ed Tanousac106bf2023-06-07 09:24:59 -07001883 messages::internalError(asyncResp->res);
Ed Tanousc2e32002023-01-07 22:05:08 -08001884 return;
1885 }
Ed Tanousac106bf2023-06-07 09:24:59 -07001886 asyncResp->res.jsonValue["DateTime"] = datetime;
Ed Tanousc2e32002023-01-07 22:05:08 -08001887 },
1888 "xyz.openbmc_project.Time.Manager", "/xyz/openbmc_project/time/bmc",
1889 "org.freedesktop.DBus.Properties", "Set",
1890 "xyz.openbmc_project.Time.EpochTime", "Elapsed",
1891 dbus::utility::DbusVariantType(us->count()));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001892}
1893
Ed Tanous75815e52022-10-05 17:21:13 -07001894inline void
1895 checkForQuiesced(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1896{
1897 sdbusplus::asio::getProperty<std::string>(
1898 *crow::connections::systemBus, "org.freedesktop.systemd1",
1899 "/org/freedesktop/systemd1/unit/obmc-bmc-service-quiesce@0.target",
1900 "org.freedesktop.systemd1.Unit", "ActiveState",
1901 [asyncResp](const boost::system::error_code& ec,
1902 const std::string& val) {
1903 if (!ec)
1904 {
1905 if (val == "active")
1906 {
1907 asyncResp->res.jsonValue["Status"]["Health"] = "Critical";
1908 asyncResp->res.jsonValue["Status"]["State"] = "Quiesced";
1909 return;
1910 }
1911 }
1912 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
1913 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
1914 });
1915}
1916
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001917inline void requestRoutesManager(App& app)
1918{
1919 std::string uuid = persistent_data::getConfig().systemUuid;
1920
1921 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07001922 .privileges(redfish::privileges::getManager)
Ed Tanous002d39b2022-05-31 08:59:27 -07001923 .methods(boost::beast::http::verb::get)(
1924 [&app, uuid](const crow::Request& req,
Ed Tanous45ca1b82022-03-25 13:07:27 -07001925 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001926 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001927 {
1928 return;
1929 }
1930 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
Sui Chena51fc2d2022-07-14 17:21:53 -07001931 asyncResp->res.jsonValue["@odata.type"] = "#Manager.v1_14_0.Manager";
Ed Tanous002d39b2022-05-31 08:59:27 -07001932 asyncResp->res.jsonValue["Id"] = "bmc";
1933 asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
1934 asyncResp->res.jsonValue["Description"] =
1935 "Baseboard Management Controller";
1936 asyncResp->res.jsonValue["PowerState"] = "On";
Ed Tanous14766872022-03-15 10:44:42 -07001937
Ed Tanous002d39b2022-05-31 08:59:27 -07001938 asyncResp->res.jsonValue["ManagerType"] = "BMC";
1939 asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
1940 asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1941 asyncResp->res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001942
Ed Tanous002d39b2022-05-31 08:59:27 -07001943 asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
1944 "/redfish/v1/Managers/bmc/LogServices";
1945 asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
1946 "/redfish/v1/Managers/bmc/NetworkProtocol";
1947 asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
1948 "/redfish/v1/Managers/bmc/EthernetInterfaces";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001949
1950#ifdef BMCWEB_ENABLE_VM_NBDPROXY
Ed Tanous002d39b2022-05-31 08:59:27 -07001951 asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
1952 "/redfish/v1/Managers/bmc/VirtualMedia";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001953#endif // BMCWEB_ENABLE_VM_NBDPROXY
1954
Ed Tanous002d39b2022-05-31 08:59:27 -07001955 // default oem data
1956 nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
1957 nlohmann::json& oemOpenbmc = oem["OpenBmc"];
1958 oem["@odata.type"] = "#OemManager.Oem";
1959 oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
1960 oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
1961 oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
Ed Tanous14766872022-03-15 10:44:42 -07001962
Ed Tanous002d39b2022-05-31 08:59:27 -07001963 nlohmann::json::object_t certificates;
1964 certificates["@odata.id"] =
1965 "/redfish/v1/Managers/bmc/Truststore/Certificates";
1966 oemOpenbmc["Certificates"] = std::move(certificates);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001967
Ed Tanous002d39b2022-05-31 08:59:27 -07001968 // Manager.Reset (an action) can be many values, OpenBMC only
1969 // supports BMC reboot.
1970 nlohmann::json& managerReset =
1971 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
1972 managerReset["target"] =
1973 "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
1974 managerReset["@Redfish.ActionInfo"] =
1975 "/redfish/v1/Managers/bmc/ResetActionInfo";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001976
Ed Tanous002d39b2022-05-31 08:59:27 -07001977 // ResetToDefaults (Factory Reset) has values like
1978 // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
1979 // on OpenBMC
1980 nlohmann::json& resetToDefaults =
1981 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
1982 resetToDefaults["target"] =
1983 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
Ed Tanous613dabe2022-07-09 11:17:36 -07001984 resetToDefaults["ResetType@Redfish.AllowableValues"] =
1985 nlohmann::json::array_t({"ResetAll"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001986
Ed Tanous002d39b2022-05-31 08:59:27 -07001987 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001988 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301989
Ed Tanous002d39b2022-05-31 08:59:27 -07001990 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1991 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1992 redfishDateTimeOffset.second;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001993
Ed Tanous002d39b2022-05-31 08:59:27 -07001994 // TODO (Gunnar): Remove these one day since moved to ComputerSystem
1995 // Still used by OCP profiles
1996 // https://github.com/opencomputeproject/OCP-Profiles/issues/23
1997 // Fill in SerialConsole info
1998 asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
1999 asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
Ed Tanous613dabe2022-07-09 11:17:36 -07002000 asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
2001 nlohmann::json::array_t({"IPMI", "SSH"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002002#ifdef BMCWEB_ENABLE_KVM
Ed Tanous002d39b2022-05-31 08:59:27 -07002003 // Fill in GraphicalConsole info
2004 asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
2005 asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] =
2006 4;
Ed Tanous613dabe2022-07-09 11:17:36 -07002007 asyncResp->res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
2008 nlohmann::json::array_t({"KVMIP"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002009#endif // BMCWEB_ENABLE_KVM
2010
Ed Tanous002d39b2022-05-31 08:59:27 -07002011 asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
Ed Tanous14766872022-03-15 10:44:42 -07002012
Ed Tanous002d39b2022-05-31 08:59:27 -07002013 nlohmann::json::array_t managerForServers;
2014 nlohmann::json::object_t manager;
2015 manager["@odata.id"] = "/redfish/v1/Systems/system";
Patrick Williamsad539542023-05-12 10:10:08 -05002016 managerForServers.emplace_back(std::move(manager));
Ed Tanous002d39b2022-05-31 08:59:27 -07002017
2018 asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
2019 std::move(managerForServers);
2020
Willy Tu13451e32023-05-24 16:08:18 -07002021 if constexpr (bmcwebEnableHealthPopulate)
2022 {
2023 auto health = std::make_shared<HealthPopulate>(asyncResp);
2024 health->isManagersHealth = true;
2025 health->populate();
2026 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002027
Willy Tueee00132022-06-14 14:53:17 -07002028 sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
Ed Tanous002d39b2022-05-31 08:59:27 -07002029 "FirmwareVersion", true);
2030
2031 managerGetLastResetTime(asyncResp);
2032
Sui Chena51fc2d2022-07-14 17:21:53 -07002033 // ManagerDiagnosticData is added for all BMCs.
2034 nlohmann::json& managerDiagnosticData =
2035 asyncResp->res.jsonValue["ManagerDiagnosticData"];
2036 managerDiagnosticData["@odata.id"] =
2037 "/redfish/v1/Managers/bmc/ManagerDiagnosticData";
2038
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002039#ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
Ed Tanous002d39b2022-05-31 08:59:27 -07002040 auto pids = std::make_shared<GetPIDValues>(asyncResp);
2041 pids->run();
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002042#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07002043
2044 getMainChassisId(asyncResp,
2045 [](const std::string& chassisId,
2046 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2047 aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
2048 nlohmann::json::array_t managerForChassis;
Ed Tanous8a592812022-06-04 09:06:59 -07002049 nlohmann::json::object_t managerObj;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002050 boost::urls::url chassiUrl =
2051 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
Willy Tueddfc432022-09-26 16:46:38 +00002052 managerObj["@odata.id"] = chassiUrl;
Patrick Williamsad539542023-05-12 10:10:08 -05002053 managerForChassis.emplace_back(std::move(managerObj));
Ed Tanous002d39b2022-05-31 08:59:27 -07002054 aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
2055 std::move(managerForChassis);
2056 aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00002057 chassiUrl;
Ed Tanous002d39b2022-05-31 08:59:27 -07002058 });
Ed Tanous14766872022-03-15 10:44:42 -07002059
Ed Tanous75815e52022-10-05 17:21:13 -07002060 sdbusplus::asio::getProperty<double>(
2061 *crow::connections::systemBus, "org.freedesktop.systemd1",
2062 "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
2063 "Progress",
2064 [asyncResp](const boost::system::error_code& ec, double val) {
2065 if (ec)
2066 {
2067 BMCWEB_LOG_ERROR << "Error while getting progress";
2068 messages::internalError(asyncResp->res);
2069 return;
2070 }
2071 if (val < 1.0)
2072 {
2073 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
2074 asyncResp->res.jsonValue["Status"]["State"] = "Starting";
2075 return;
2076 }
2077 checkForQuiesced(asyncResp);
2078 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002079
George Liue99073f2022-12-09 11:06:16 +08002080 constexpr std::array<std::string_view, 1> interfaces = {
2081 "xyz.openbmc_project.Inventory.Item.Bmc"};
2082 dbus::utility::getSubTree(
2083 "/xyz/openbmc_project/inventory", 0, interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -07002084 [asyncResp](
George Liue99073f2022-12-09 11:06:16 +08002085 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07002086 const dbus::utility::MapperGetSubTreeResponse& subtree) {
2087 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002088 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002089 BMCWEB_LOG_DEBUG << "D-Bus response error on GetSubTree " << ec;
2090 return;
2091 }
2092 if (subtree.empty())
2093 {
2094 BMCWEB_LOG_DEBUG << "Can't find bmc D-Bus object!";
2095 return;
2096 }
2097 // Assume only 1 bmc D-Bus object
2098 // Throw an error if there is more than 1
2099 if (subtree.size() > 1)
2100 {
2101 BMCWEB_LOG_DEBUG << "Found more than 1 bmc D-Bus object!";
2102 messages::internalError(asyncResp->res);
2103 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002104 }
2105
Ed Tanous002d39b2022-05-31 08:59:27 -07002106 if (subtree[0].first.empty() || subtree[0].second.size() != 1)
2107 {
2108 BMCWEB_LOG_DEBUG << "Error getting bmc D-Bus object!";
2109 messages::internalError(asyncResp->res);
2110 return;
2111 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002112
Ed Tanous002d39b2022-05-31 08:59:27 -07002113 const std::string& path = subtree[0].first;
2114 const std::string& connectionName = subtree[0].second[0].first;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002115
Ed Tanous002d39b2022-05-31 08:59:27 -07002116 for (const auto& interfaceName : subtree[0].second[0].second)
2117 {
2118 if (interfaceName ==
2119 "xyz.openbmc_project.Inventory.Decorator.Asset")
2120 {
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002121 sdbusplus::asio::getAllProperties(
2122 *crow::connections::systemBus, connectionName, path,
2123 "xyz.openbmc_project.Inventory.Decorator.Asset",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002124 [asyncResp](const boost::system::error_code& ec2,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002125 const dbus::utility::DBusPropertiesMap&
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002126 propertiesList) {
Ed Tanous8a592812022-06-04 09:06:59 -07002127 if (ec2)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002128 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002129 BMCWEB_LOG_DEBUG << "Can't get bmc asset!";
2130 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002131 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002132
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002133 const std::string* partNumber = nullptr;
2134 const std::string* serialNumber = nullptr;
2135 const std::string* manufacturer = nullptr;
2136 const std::string* model = nullptr;
2137 const std::string* sparePartNumber = nullptr;
2138
2139 const bool success = sdbusplus::unpackPropertiesNoThrow(
2140 dbus_utils::UnpackErrorPrinter(), propertiesList,
2141 "PartNumber", partNumber, "SerialNumber",
2142 serialNumber, "Manufacturer", manufacturer, "Model",
2143 model, "SparePartNumber", sparePartNumber);
2144
2145 if (!success)
2146 {
2147 messages::internalError(asyncResp->res);
2148 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07002149 }
Krzysztof Grobelnyfac6e532022-08-04 12:42:45 +02002150
2151 if (partNumber != nullptr)
2152 {
2153 asyncResp->res.jsonValue["PartNumber"] =
2154 *partNumber;
2155 }
2156
2157 if (serialNumber != nullptr)
2158 {
2159 asyncResp->res.jsonValue["SerialNumber"] =
2160 *serialNumber;
2161 }
2162
2163 if (manufacturer != nullptr)
2164 {
2165 asyncResp->res.jsonValue["Manufacturer"] =
2166 *manufacturer;
2167 }
2168
2169 if (model != nullptr)
2170 {
2171 asyncResp->res.jsonValue["Model"] = *model;
2172 }
2173
2174 if (sparePartNumber != nullptr)
2175 {
2176 asyncResp->res.jsonValue["SparePartNumber"] =
2177 *sparePartNumber;
2178 }
2179 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002180 }
2181 else if (interfaceName ==
2182 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
2183 {
2184 getLocation(asyncResp, connectionName, path);
2185 }
2186 }
George Liue99073f2022-12-09 11:06:16 +08002187 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002188 });
2189
2190 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07002191 .privileges(redfish::privileges::patchManager)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002192 .methods(boost::beast::http::verb::patch)(
2193 [&app](const crow::Request& req,
2194 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002195 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002196 {
2197 return;
2198 }
2199 std::optional<nlohmann::json> oem;
2200 std::optional<nlohmann::json> links;
2201 std::optional<std::string> datetime;
2202
2203 if (!json_util::readJsonPatch(req, asyncResp->res, "Oem", oem,
2204 "DateTime", datetime, "Links", links))
2205 {
2206 return;
2207 }
2208
2209 if (oem)
2210 {
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002211#ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
Ed Tanous002d39b2022-05-31 08:59:27 -07002212 std::optional<nlohmann::json> openbmc;
2213 if (!redfish::json_util::readJson(*oem, asyncResp->res, "OpenBmc",
2214 openbmc))
2215 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002216 return;
2217 }
2218 if (openbmc)
2219 {
2220 std::optional<nlohmann::json> fan;
2221 if (!redfish::json_util::readJson(*openbmc, asyncResp->res,
2222 "Fan", fan))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002223 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002224 return;
2225 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002226 if (fan)
2227 {
2228 auto pid = std::make_shared<SetPIDValues>(asyncResp, *fan);
2229 pid->run();
2230 }
2231 }
Gunnar Mills54dce7f2022-08-05 17:01:32 +00002232#else
2233 messages::propertyUnknown(asyncResp->res, "Oem");
2234 return;
2235#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07002236 }
2237 if (links)
2238 {
2239 std::optional<nlohmann::json> activeSoftwareImage;
2240 if (!redfish::json_util::readJson(*links, asyncResp->res,
2241 "ActiveSoftwareImage",
2242 activeSoftwareImage))
2243 {
2244 return;
2245 }
2246 if (activeSoftwareImage)
2247 {
2248 std::optional<std::string> odataId;
2249 if (!json_util::readJson(*activeSoftwareImage, asyncResp->res,
2250 "@odata.id", odataId))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002251 {
Ed Tanous45ca1b82022-03-25 13:07:27 -07002252 return;
2253 }
2254
Ed Tanous002d39b2022-05-31 08:59:27 -07002255 if (odataId)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002256 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002257 setActiveFirmwareImage(asyncResp, *odataId);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002258 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002259 }
2260 }
2261 if (datetime)
2262 {
2263 setDateTime(asyncResp, std::move(*datetime));
2264 }
2265 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002266}
2267
2268inline void requestRoutesManagerCollection(App& app)
2269{
2270 BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
Ed Tanoused398212021-06-09 17:05:54 -07002271 .privileges(redfish::privileges::getManagerCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002272 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002273 [&app](const crow::Request& req,
2274 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002275 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002276 {
2277 return;
2278 }
2279 // Collections don't include the static data added by SubRoute
2280 // because it has a duplicate entry for members
2281 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
2282 asyncResp->res.jsonValue["@odata.type"] =
2283 "#ManagerCollection.ManagerCollection";
2284 asyncResp->res.jsonValue["Name"] = "Manager Collection";
2285 asyncResp->res.jsonValue["Members@odata.count"] = 1;
2286 nlohmann::json::array_t members;
2287 nlohmann::json& bmc = members.emplace_back();
2288 bmc["@odata.id"] = "/redfish/v1/Managers/bmc";
2289 asyncResp->res.jsonValue["Members"] = std::move(members);
2290 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002291}
Ed Tanous1abe55e2018-09-05 08:30:59 -07002292} // namespace redfish