blob: d1e43fb44802ba5c98cefca3a7bd4282ebf1c99a [file] [log] [blame]
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Sui Chena51fc2d2022-07-14 17:21:53 -070018#include "app.hpp"
19#include "dbus_utility.hpp"
James Feistb49ac872019-05-21 15:12:01 -070020#include "health.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070021#include "query.hpp"
Jennifer Leec5d03ff2019-03-08 15:42:58 -080022#include "redfish_util.hpp"
Sui Chena51fc2d2022-07-14 17:21:53 -070023#include "registries/privilege_registry.hpp"
24#include "utils/sw_utils.hpp"
25#include "utils/systemd_utils.hpp"
Borawski.Lukasz9c3106852018-02-09 15:24:22 +010026
Santosh Puranikaf5d60582019-03-20 18:16:36 +053027#include <boost/date_time.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050028
Ed Tanousa170f272022-06-30 21:53:27 -070029#include <algorithm>
Gunnar Mills4bfefa72020-07-30 13:54:29 -050030#include <cstdint>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050031#include <memory>
32#include <sstream>
Ed Tanousabf2add2019-01-22 16:40:12 -080033#include <variant>
James Feist5b4aa862018-08-16 14:07:01 -070034
Ed Tanous1abe55e2018-09-05 08:30:59 -070035namespace redfish
36{
Jennifer Leeed5befb2018-08-10 11:29:45 -070037
38/**
Gunnar Mills2a5c4402020-05-19 09:07:24 -050039 * Function reboots the BMC.
40 *
41 * @param[in] asyncResp - Shared pointer for completing asynchronous calls
Jennifer Leeed5befb2018-08-10 11:29:45 -070042 */
zhanghch058d1b46d2021-04-01 11:18:24 +080043inline void
44 doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Gunnar Mills2a5c4402020-05-19 09:07:24 -050045{
46 const char* processName = "xyz.openbmc_project.State.BMC";
47 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
48 const char* interfaceName = "xyz.openbmc_project.State.BMC";
49 const std::string& propertyValue =
50 "xyz.openbmc_project.State.BMC.Transition.Reboot";
51 const char* destProperty = "RequestedBMCTransition";
52
53 // Create the D-Bus variant for D-Bus call.
Ed Tanous168e20c2021-12-13 14:39:53 -080054 dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
Gunnar Mills2a5c4402020-05-19 09:07:24 -050055
56 crow::connections::systemBus->async_method_call(
57 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -070058 // Use "Set" method to set the property value.
59 if (ec)
60 {
61 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
62 messages::internalError(asyncResp->res);
63 return;
64 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -050065
Ed Tanous002d39b2022-05-31 08:59:27 -070066 messages::success(asyncResp->res);
Gunnar Mills2a5c4402020-05-19 09:07:24 -050067 },
68 processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
69 interfaceName, destProperty, dbusPropertyValue);
70}
71
zhanghch058d1b46d2021-04-01 11:18:24 +080072inline void
73 doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +000074{
75 const char* processName = "xyz.openbmc_project.State.BMC";
76 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
77 const char* interfaceName = "xyz.openbmc_project.State.BMC";
78 const std::string& propertyValue =
79 "xyz.openbmc_project.State.BMC.Transition.HardReboot";
80 const char* destProperty = "RequestedBMCTransition";
81
82 // Create the D-Bus variant for D-Bus call.
Ed Tanous168e20c2021-12-13 14:39:53 -080083 dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +000084
85 crow::connections::systemBus->async_method_call(
86 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -070087 // Use "Set" method to set the property value.
88 if (ec)
89 {
90 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
91 messages::internalError(asyncResp->res);
92 return;
93 }
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +000094
Ed Tanous002d39b2022-05-31 08:59:27 -070095 messages::success(asyncResp->res);
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +000096 },
97 processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
98 interfaceName, destProperty, dbusPropertyValue);
99}
100
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500101/**
102 * ManagerResetAction class supports the POST method for the Reset (reboot)
103 * action.
104 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700105inline void requestRoutesManagerResetAction(App& app)
Jennifer Leeed5befb2018-08-10 11:29:45 -0700106{
Jennifer Leeed5befb2018-08-10 11:29:45 -0700107 /**
Jennifer Leeed5befb2018-08-10 11:29:45 -0700108 * Function handles POST method request.
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500109 * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000110 * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
Jennifer Leeed5befb2018-08-10 11:29:45 -0700111 */
Jennifer Leeed5befb2018-08-10 11:29:45 -0700112
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700113 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
Ed Tanoused398212021-06-09 17:05:54 -0700114 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700115 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700116 [&app](const crow::Request& req,
117 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000118 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700119 {
120 return;
121 }
122 BMCWEB_LOG_DEBUG << "Post Manager Reset.";
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500123
Ed Tanous002d39b2022-05-31 08:59:27 -0700124 std::string resetType;
Jennifer Leeed5befb2018-08-10 11:29:45 -0700125
Ed Tanous002d39b2022-05-31 08:59:27 -0700126 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
127 resetType))
128 {
129 return;
130 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500131
Ed Tanous002d39b2022-05-31 08:59:27 -0700132 if (resetType == "GracefulRestart")
133 {
134 BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
135 doBMCGracefulRestart(asyncResp);
136 return;
137 }
138 if (resetType == "ForceRestart")
139 {
140 BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
141 doBMCForceRestart(asyncResp);
142 return;
143 }
144 BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
145 << resetType;
146 messages::actionParameterNotSupported(asyncResp->res, resetType,
147 "ResetType");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700148
Ed Tanous002d39b2022-05-31 08:59:27 -0700149 return;
150 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700151}
Jennifer Leeed5befb2018-08-10 11:29:45 -0700152
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500153/**
154 * ManagerResetToDefaultsAction class supports POST method for factory reset
155 * action.
156 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700157inline void requestRoutesManagerResetToDefaultsAction(App& app)
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500158{
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500159
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500160 /**
161 * Function handles ResetToDefaults POST method request.
162 *
163 * Analyzes POST body message and factory resets BMC by calling
164 * BMC code updater factory reset followed by a BMC reboot.
165 *
166 * BMC code updater factory reset wipes the whole BMC read-write
167 * filesystem which includes things like the network settings.
168 *
169 * OpenBMC only supports ResetToDefaultsType "ResetAll".
170 */
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500171
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700172 BMCWEB_ROUTE(app,
173 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/")
Ed Tanoused398212021-06-09 17:05:54 -0700174 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700175 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700176 [&app](const crow::Request& req,
177 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000178 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700179 {
180 return;
181 }
182 BMCWEB_LOG_DEBUG << "Post ResetToDefaults.";
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500183
Ed Tanous002d39b2022-05-31 08:59:27 -0700184 std::string resetType;
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500185
Ed Tanous002d39b2022-05-31 08:59:27 -0700186 if (!json_util::readJsonAction(req, asyncResp->res,
187 "ResetToDefaultsType", resetType))
188 {
189 BMCWEB_LOG_DEBUG << "Missing property ResetToDefaultsType.";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700190
Ed Tanous002d39b2022-05-31 08:59:27 -0700191 messages::actionParameterMissing(asyncResp->res, "ResetToDefaults",
192 "ResetToDefaultsType");
193 return;
194 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700195
Ed Tanous002d39b2022-05-31 08:59:27 -0700196 if (resetType != "ResetAll")
197 {
198 BMCWEB_LOG_DEBUG
199 << "Invalid property value for ResetToDefaultsType: "
200 << resetType;
201 messages::actionParameterNotSupported(asyncResp->res, resetType,
202 "ResetToDefaultsType");
203 return;
204 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700205
Ed Tanous002d39b2022-05-31 08:59:27 -0700206 crow::connections::systemBus->async_method_call(
207 [asyncResp](const boost::system::error_code ec) {
208 if (ec)
209 {
210 BMCWEB_LOG_DEBUG << "Failed to ResetToDefaults: " << ec;
211 messages::internalError(asyncResp->res);
212 return;
213 }
214 // Factory Reset doesn't actually happen until a reboot
215 // Can't erase what the BMC is running on
216 doBMCGracefulRestart(asyncResp);
217 },
218 "xyz.openbmc_project.Software.BMC.Updater",
219 "/xyz/openbmc_project/software",
220 "xyz.openbmc_project.Common.FactoryReset", "Reset");
221 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700222}
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500223
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530224/**
225 * ManagerResetActionInfo derived class for delivering Manager
226 * ResetType AllowableValues using ResetInfo schema.
227 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700228inline void requestRoutesManagerResetActionInfo(App& app)
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530229{
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530230 /**
231 * Functions triggers appropriate requests on DBus
232 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700233
234 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/ResetActionInfo/")
Ed Tanoused398212021-06-09 17:05:54 -0700235 .privileges(redfish::privileges::getActionInfo)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700236 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700237 [&app](const crow::Request& req,
238 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000239 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700240 {
241 return;
242 }
Ed Tanous14766872022-03-15 10:44:42 -0700243
Ed Tanous002d39b2022-05-31 08:59:27 -0700244 asyncResp->res.jsonValue["@odata.type"] =
245 "#ActionInfo.v1_1_2.ActionInfo";
246 asyncResp->res.jsonValue["@odata.id"] =
247 "/redfish/v1/Managers/bmc/ResetActionInfo";
248 asyncResp->res.jsonValue["Name"] = "Reset Action Info";
249 asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
250 nlohmann::json::object_t parameter;
251 parameter["Name"] = "ResetType";
252 parameter["Required"] = true;
253 parameter["DataType"] = "String";
Ed Tanous14766872022-03-15 10:44:42 -0700254
Ed Tanous002d39b2022-05-31 08:59:27 -0700255 nlohmann::json::array_t allowableValues;
256 allowableValues.push_back("GracefulRestart");
257 allowableValues.push_back("ForceRestart");
258 parameter["AllowableValues"] = std::move(allowableValues);
Ed Tanous14766872022-03-15 10:44:42 -0700259
Ed Tanous002d39b2022-05-31 08:59:27 -0700260 nlohmann::json::array_t parameters;
261 parameters.push_back(std::move(parameter));
Ed Tanous14766872022-03-15 10:44:42 -0700262
Ed Tanous002d39b2022-05-31 08:59:27 -0700263 asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
264 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700265}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530266
James Feist5b4aa862018-08-16 14:07:01 -0700267static constexpr const char* objectManagerIface =
268 "org.freedesktop.DBus.ObjectManager";
269static constexpr const char* pidConfigurationIface =
270 "xyz.openbmc_project.Configuration.Pid";
271static constexpr const char* pidZoneConfigurationIface =
272 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feistb7a08d02018-12-11 14:55:37 -0800273static constexpr const char* stepwiseConfigurationIface =
274 "xyz.openbmc_project.Configuration.Stepwise";
James Feist73df0db2019-03-25 15:29:35 -0700275static constexpr const char* thermalModeIface =
276 "xyz.openbmc_project.Control.ThermalMode";
Borawski.Lukasz9c3106852018-02-09 15:24:22 +0100277
zhanghch058d1b46d2021-04-01 11:18:24 +0800278inline void
279 asyncPopulatePid(const std::string& connection, const std::string& path,
280 const std::string& currentProfile,
281 const std::vector<std::string>& supportedProfiles,
282 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
James Feist5b4aa862018-08-16 14:07:01 -0700283{
284
285 crow::connections::systemBus->async_method_call(
James Feist73df0db2019-03-25 15:29:35 -0700286 [asyncResp, currentProfile, supportedProfiles](
287 const boost::system::error_code ec,
288 const dbus::utility::ManagedObjectType& managedObj) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700289 if (ec)
290 {
291 BMCWEB_LOG_ERROR << ec;
292 asyncResp->res.jsonValue.clear();
293 messages::internalError(asyncResp->res);
294 return;
295 }
296 nlohmann::json& configRoot =
297 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
298 nlohmann::json& fans = configRoot["FanControllers"];
299 fans["@odata.type"] = "#OemManager.FanControllers";
300 fans["@odata.id"] =
301 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers";
302
303 nlohmann::json& pids = configRoot["PidControllers"];
304 pids["@odata.type"] = "#OemManager.PidControllers";
305 pids["@odata.id"] =
306 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
307
308 nlohmann::json& stepwise = configRoot["StepwiseControllers"];
309 stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
310 stepwise["@odata.id"] =
311 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
312
313 nlohmann::json& zones = configRoot["FanZones"];
314 zones["@odata.id"] =
315 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
316 zones["@odata.type"] = "#OemManager.FanZones";
317 configRoot["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
318 configRoot["@odata.type"] = "#OemManager.Fan";
319 configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
320
321 if (!currentProfile.empty())
322 {
323 configRoot["Profile"] = currentProfile;
324 }
325 BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !";
326
327 for (const auto& pathPair : managedObj)
328 {
329 for (const auto& intfPair : pathPair.second)
James Feist5b4aa862018-08-16 14:07:01 -0700330 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700331 if (intfPair.first != pidConfigurationIface &&
332 intfPair.first != pidZoneConfigurationIface &&
333 intfPair.first != stepwiseConfigurationIface)
James Feist5b4aa862018-08-16 14:07:01 -0700334 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700335 continue;
336 }
James Feist73df0db2019-03-25 15:29:35 -0700337
Ed Tanous002d39b2022-05-31 08:59:27 -0700338 std::string name;
James Feist73df0db2019-03-25 15:29:35 -0700339
Ed Tanous002d39b2022-05-31 08:59:27 -0700340 for (const std::pair<std::string,
341 dbus::utility::DbusVariantType>& propPair :
342 intfPair.second)
343 {
344 if (propPair.first == "Name")
James Feist73df0db2019-03-25 15:29:35 -0700345 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700346 const std::string* namePtr =
347 std::get_if<std::string>(&propPair.second);
348 if (namePtr == nullptr)
James Feist73df0db2019-03-25 15:29:35 -0700349 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700350 BMCWEB_LOG_ERROR << "Pid Name Field illegal";
James Feistc33a90e2019-03-01 10:17:44 -0800351 messages::internalError(asyncResp->res);
352 return;
353 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700354 name = *namePtr;
355 dbus::utility::escapePathForDbus(name);
James Feistb7a08d02018-12-11 14:55:37 -0800356 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700357 else if (propPair.first == "Profiles")
James Feistb7a08d02018-12-11 14:55:37 -0800358 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700359 const std::vector<std::string>* profiles =
360 std::get_if<std::vector<std::string>>(
361 &propPair.second);
362 if (profiles == nullptr)
James Feistb7a08d02018-12-11 14:55:37 -0800363 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700364 BMCWEB_LOG_ERROR << "Pid Profiles Field illegal";
James Feistb7a08d02018-12-11 14:55:37 -0800365 messages::internalError(asyncResp->res);
366 return;
367 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700368 if (std::find(profiles->begin(), profiles->end(),
369 currentProfile) == profiles->end())
James Feistb7a08d02018-12-11 14:55:37 -0800370 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700371 BMCWEB_LOG_INFO
372 << name << " not supported in current profile";
373 continue;
James Feistb7a08d02018-12-11 14:55:37 -0800374 }
375 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700376 }
377 nlohmann::json* config = nullptr;
378 const std::string* classPtr = nullptr;
379
380 for (const std::pair<std::string,
381 dbus::utility::DbusVariantType>& propPair :
382 intfPair.second)
383 {
384 if (propPair.first == "Class")
James Feistb7a08d02018-12-11 14:55:37 -0800385 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700386 classPtr = std::get_if<std::string>(&propPair.second);
387 }
388 }
389
390 if (intfPair.first == pidZoneConfigurationIface)
391 {
392 std::string chassis;
393 if (!dbus::utility::getNthStringFromPath(pathPair.first.str,
394 5, chassis))
395 {
396 chassis = "#IllegalValue";
397 }
398 nlohmann::json& zone = zones[name];
399 zone["Chassis"] = {
400 {"@odata.id", "/redfish/v1/Chassis/" + chassis}};
401 zone["@odata.id"] =
402 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/" +
403 name;
404 zone["@odata.type"] = "#OemManager.FanZone";
405 config = &zone;
406 }
407
408 else if (intfPair.first == stepwiseConfigurationIface)
409 {
410 if (classPtr == nullptr)
411 {
412 BMCWEB_LOG_ERROR << "Pid Class Field illegal";
James Feistb7a08d02018-12-11 14:55:37 -0800413 messages::internalError(asyncResp->res);
414 return;
415 }
416
Ed Tanous002d39b2022-05-31 08:59:27 -0700417 nlohmann::json& controller = stepwise[name];
418 config = &controller;
James Feistb7a08d02018-12-11 14:55:37 -0800419
Ed Tanous002d39b2022-05-31 08:59:27 -0700420 controller["@odata.id"] =
421 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers/" +
422 name;
423 controller["@odata.type"] =
424 "#OemManager.StepwiseController";
425
426 controller["Direction"] = *classPtr;
427 }
428
429 // pid and fans are off the same configuration
430 else if (intfPair.first == pidConfigurationIface)
431 {
432
433 if (classPtr == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700434 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700435 BMCWEB_LOG_ERROR << "Pid Class Field illegal";
436 messages::internalError(asyncResp->res);
437 return;
438 }
439 bool isFan = *classPtr == "fan";
440 nlohmann::json& element = isFan ? fans[name] : pids[name];
441 config = &element;
442 if (isFan)
443 {
444 element["@odata.id"] =
445 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers/" +
446 name;
447 element["@odata.type"] = "#OemManager.FanController";
448 }
449 else
450 {
451 element["@odata.id"] =
452 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers/" +
453 name;
454 element["@odata.type"] = "#OemManager.PidController";
455 }
456 }
457 else
458 {
459 BMCWEB_LOG_ERROR << "Unexpected configuration";
460 messages::internalError(asyncResp->res);
461 return;
462 }
James Feist5b4aa862018-08-16 14:07:01 -0700463
Ed Tanous002d39b2022-05-31 08:59:27 -0700464 // used for making maps out of 2 vectors
465 const std::vector<double>* keys = nullptr;
466 const std::vector<double>* values = nullptr;
467
468 for (const auto& propertyPair : intfPair.second)
469 {
470 if (propertyPair.first == "Type" ||
471 propertyPair.first == "Class" ||
472 propertyPair.first == "Name")
473 {
474 continue;
475 }
476
477 // zones
478 if (intfPair.first == pidZoneConfigurationIface)
479 {
480 const double* ptr =
481 std::get_if<double>(&propertyPair.second);
482 if (ptr == nullptr)
483 {
484 BMCWEB_LOG_ERROR << "Field Illegal "
485 << propertyPair.first;
486 messages::internalError(asyncResp->res);
487 return;
488 }
489 (*config)[propertyPair.first] = *ptr;
490 }
491
492 if (intfPair.first == stepwiseConfigurationIface)
493 {
494 if (propertyPair.first == "Reading" ||
495 propertyPair.first == "Output")
496 {
497 const std::vector<double>* ptr =
498 std::get_if<std::vector<double>>(
499 &propertyPair.second);
500
501 if (ptr == nullptr)
502 {
503 BMCWEB_LOG_ERROR << "Field Illegal "
504 << propertyPair.first;
505 messages::internalError(asyncResp->res);
506 return;
507 }
508
509 if (propertyPair.first == "Reading")
510 {
511 keys = ptr;
512 }
513 else
514 {
515 values = ptr;
516 }
517 if (keys != nullptr && values != nullptr)
518 {
519 if (keys->size() != values->size())
520 {
521 BMCWEB_LOG_ERROR
522 << "Reading and Output size don't match ";
523 messages::internalError(asyncResp->res);
524 return;
525 }
526 nlohmann::json& steps = (*config)["Steps"];
527 steps = nlohmann::json::array();
528 for (size_t ii = 0; ii < keys->size(); ii++)
529 {
530 nlohmann::json::object_t step;
531 step["Target"] = (*keys)[ii];
532 step["Output"] = (*values)[ii];
533 steps.push_back(std::move(step));
534 }
535 }
536 }
537 if (propertyPair.first == "NegativeHysteresis" ||
538 propertyPair.first == "PositiveHysteresis")
James Feist5b4aa862018-08-16 14:07:01 -0700539 {
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800540 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800541 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700542 if (ptr == nullptr)
543 {
544 BMCWEB_LOG_ERROR << "Field Illegal "
545 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700546 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700547 return;
548 }
James Feistb7a08d02018-12-11 14:55:37 -0800549 (*config)[propertyPair.first] = *ptr;
550 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700551 }
James Feistb7a08d02018-12-11 14:55:37 -0800552
Ed Tanous002d39b2022-05-31 08:59:27 -0700553 // pid and fans are off the same configuration
554 if (intfPair.first == pidConfigurationIface ||
555 intfPair.first == stepwiseConfigurationIface)
556 {
557
558 if (propertyPair.first == "Zones")
James Feistb7a08d02018-12-11 14:55:37 -0800559 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700560 const std::vector<std::string>* inputs =
561 std::get_if<std::vector<std::string>>(
562 &propertyPair.second);
563
564 if (inputs == nullptr)
James Feistb7a08d02018-12-11 14:55:37 -0800565 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700566 BMCWEB_LOG_ERROR << "Zones Pid Field Illegal";
567 messages::internalError(asyncResp->res);
568 return;
James Feistb7a08d02018-12-11 14:55:37 -0800569 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700570 auto& data = (*config)[propertyPair.first];
571 data = nlohmann::json::array();
572 for (std::string itemCopy : *inputs)
James Feistb7a08d02018-12-11 14:55:37 -0800573 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700574 dbus::utility::escapePathForDbus(itemCopy);
575 nlohmann::json::object_t input;
576 input["@odata.id"] =
577 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/" +
578 itemCopy;
579 data.push_back(std::move(input));
James Feistb7a08d02018-12-11 14:55:37 -0800580 }
James Feist5b4aa862018-08-16 14:07:01 -0700581 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700582 // todo(james): may never happen, but this
583 // assumes configuration data referenced in the
584 // PID config is provided by the same daemon, we
585 // could add another loop to cover all cases,
586 // but I'm okay kicking this can down the road a
587 // bit
James Feist5b4aa862018-08-16 14:07:01 -0700588
Ed Tanous002d39b2022-05-31 08:59:27 -0700589 else if (propertyPair.first == "Inputs" ||
590 propertyPair.first == "Outputs")
James Feist5b4aa862018-08-16 14:07:01 -0700591 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700592 auto& data = (*config)[propertyPair.first];
593 const std::vector<std::string>* inputs =
594 std::get_if<std::vector<std::string>>(
595 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700596
Ed Tanous002d39b2022-05-31 08:59:27 -0700597 if (inputs == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700598 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700599 BMCWEB_LOG_ERROR << "Field Illegal "
600 << propertyPair.first;
601 messages::internalError(asyncResp->res);
602 return;
James Feist5b4aa862018-08-16 14:07:01 -0700603 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700604 data = *inputs;
605 }
606 else if (propertyPair.first == "SetPointOffset")
607 {
608 const std::string* ptr =
609 std::get_if<std::string>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700610
Ed Tanous002d39b2022-05-31 08:59:27 -0700611 if (ptr == nullptr)
James Feist5b4aa862018-08-16 14:07:01 -0700612 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700613 BMCWEB_LOG_ERROR << "Field Illegal "
614 << propertyPair.first;
615 messages::internalError(asyncResp->res);
616 return;
James Feistb943aae2019-07-11 16:33:56 -0700617 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700618 // translate from dbus to redfish
619 if (*ptr == "WarningHigh")
James Feistb943aae2019-07-11 16:33:56 -0700620 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700621 (*config)["SetPointOffset"] =
622 "UpperThresholdNonCritical";
James Feistb943aae2019-07-11 16:33:56 -0700623 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700624 else if (*ptr == "WarningLow")
James Feist5b4aa862018-08-16 14:07:01 -0700625 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700626 (*config)["SetPointOffset"] =
627 "LowerThresholdNonCritical";
James Feist5b4aa862018-08-16 14:07:01 -0700628 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700629 else if (*ptr == "CriticalHigh")
630 {
631 (*config)["SetPointOffset"] =
632 "UpperThresholdCritical";
633 }
634 else if (*ptr == "CriticalLow")
635 {
636 (*config)["SetPointOffset"] =
637 "LowerThresholdCritical";
638 }
639 else
640 {
641 BMCWEB_LOG_ERROR << "Value Illegal " << *ptr;
642 messages::internalError(asyncResp->res);
643 return;
644 }
645 }
646 // doubles
647 else if (propertyPair.first == "FFGainCoefficient" ||
648 propertyPair.first == "FFOffCoefficient" ||
649 propertyPair.first == "ICoefficient" ||
650 propertyPair.first == "ILimitMax" ||
651 propertyPair.first == "ILimitMin" ||
652 propertyPair.first == "PositiveHysteresis" ||
653 propertyPair.first == "NegativeHysteresis" ||
654 propertyPair.first == "OutLimitMax" ||
655 propertyPair.first == "OutLimitMin" ||
656 propertyPair.first == "PCoefficient" ||
657 propertyPair.first == "SetPoint" ||
658 propertyPair.first == "SlewNeg" ||
659 propertyPair.first == "SlewPos")
660 {
661 const double* ptr =
662 std::get_if<double>(&propertyPair.second);
663 if (ptr == nullptr)
664 {
665 BMCWEB_LOG_ERROR << "Field Illegal "
666 << propertyPair.first;
667 messages::internalError(asyncResp->res);
668 return;
669 }
670 (*config)[propertyPair.first] = *ptr;
James Feist5b4aa862018-08-16 14:07:01 -0700671 }
672 }
673 }
674 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700675 }
James Feist5b4aa862018-08-16 14:07:01 -0700676 },
677 connection, path, objectManagerIface, "GetManagedObjects");
678}
Jennifer Leeca537922018-08-10 10:07:30 -0700679
James Feist83ff9ab2018-08-31 10:18:24 -0700680enum class CreatePIDRet
681{
682 fail,
683 del,
684 patch
685};
686
zhanghch058d1b46d2021-04-01 11:18:24 +0800687inline bool
688 getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
689 std::vector<nlohmann::json>& config,
690 std::vector<std::string>& zones)
James Feist5f2caae2018-12-12 14:08:25 -0800691{
James Feistb6baeaa2019-02-21 10:41:40 -0800692 if (config.empty())
693 {
694 BMCWEB_LOG_ERROR << "Empty Zones";
Ed Tanous1668ce62022-02-07 23:44:31 -0800695 messages::propertyValueFormatError(response->res, "[]", "Zones");
James Feistb6baeaa2019-02-21 10:41:40 -0800696 return false;
697 }
James Feist5f2caae2018-12-12 14:08:25 -0800698 for (auto& odata : config)
699 {
700 std::string path;
701 if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
702 path))
703 {
704 return false;
705 }
706 std::string input;
James Feist61adbda2019-03-25 13:03:51 -0700707
708 // 8 below comes from
709 // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
710 // 0 1 2 3 4 5 6 7 8
711 if (!dbus::utility::getNthStringFromPath(path, 8, input))
James Feist5f2caae2018-12-12 14:08:25 -0800712 {
713 BMCWEB_LOG_ERROR << "Got invalid path " << path;
714 BMCWEB_LOG_ERROR << "Illegal Type Zones";
715 messages::propertyValueFormatError(response->res, odata.dump(),
716 "Zones");
717 return false;
718 }
Ed Tanousa170f272022-06-30 21:53:27 -0700719 std::replace(input.begin(), input.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -0800720 zones.emplace_back(std::move(input));
721 }
722 return true;
723}
724
Ed Tanous711ac7a2021-12-20 09:34:41 -0800725inline const dbus::utility::ManagedObjectType::value_type*
James Feist73df0db2019-03-25 15:29:35 -0700726 findChassis(const dbus::utility::ManagedObjectType& managedObj,
727 const std::string& value, std::string& chassis)
James Feistb6baeaa2019-02-21 10:41:40 -0800728{
729 BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
730
Ed Tanousa170f272022-06-30 21:53:27 -0700731 std::string escaped = value;
732 std::replace(escaped.begin(), escaped.end(), '_', ' ');
James Feistb6baeaa2019-02-21 10:41:40 -0800733 escaped = "/" + escaped;
Ed Tanous002d39b2022-05-31 08:59:27 -0700734 auto it = std::find_if(managedObj.begin(), managedObj.end(),
735 [&escaped](const auto& obj) {
736 if (boost::algorithm::ends_with(obj.first.str, escaped))
737 {
738 BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
739 return true;
740 }
741 return false;
742 });
James Feistb6baeaa2019-02-21 10:41:40 -0800743
744 if (it == managedObj.end())
745 {
James Feist73df0db2019-03-25 15:29:35 -0700746 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800747 }
748 // 5 comes from <chassis-name> being the 5th element
749 // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
James Feist73df0db2019-03-25 15:29:35 -0700750 if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
751 {
752 return &(*it);
753 }
754
755 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800756}
757
Ed Tanous23a21a12020-07-25 04:45:05 +0000758inline CreatePIDRet createPidInterface(
zhanghch058d1b46d2021-04-01 11:18:24 +0800759 const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
Ed Tanousb5a76932020-09-29 16:16:58 -0700760 const nlohmann::json::iterator& it, const std::string& path,
James Feist83ff9ab2018-08-31 10:18:24 -0700761 const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800762 dbus::utility::DBusPropertiesMap& output, std::string& chassis,
763 const std::string& profile)
James Feist83ff9ab2018-08-31 10:18:24 -0700764{
765
James Feist5f2caae2018-12-12 14:08:25 -0800766 // common deleter
James Feistb6baeaa2019-02-21 10:41:40 -0800767 if (it.value() == nullptr)
James Feist5f2caae2018-12-12 14:08:25 -0800768 {
769 std::string iface;
770 if (type == "PidControllers" || type == "FanControllers")
771 {
772 iface = pidConfigurationIface;
773 }
774 else if (type == "FanZones")
775 {
776 iface = pidZoneConfigurationIface;
777 }
778 else if (type == "StepwiseControllers")
779 {
780 iface = stepwiseConfigurationIface;
781 }
782 else
783 {
Gunnar Millsa0744d32020-11-09 15:40:45 -0600784 BMCWEB_LOG_ERROR << "Illegal Type " << type;
James Feist5f2caae2018-12-12 14:08:25 -0800785 messages::propertyUnknown(response->res, type);
786 return CreatePIDRet::fail;
787 }
James Feist6ee7f772020-02-06 16:25:27 -0800788
789 BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
James Feist5f2caae2018-12-12 14:08:25 -0800790 // delete interface
791 crow::connections::systemBus->async_method_call(
792 [response, path](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700793 if (ec)
794 {
795 BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
796 messages::internalError(response->res);
797 return;
798 }
799 messages::success(response->res);
James Feist5f2caae2018-12-12 14:08:25 -0800800 },
801 "xyz.openbmc_project.EntityManager", path, iface, "Delete");
802 return CreatePIDRet::del;
803 }
804
Ed Tanous711ac7a2021-12-20 09:34:41 -0800805 const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800806 if (!createNewObject)
807 {
808 // if we aren't creating a new object, we should be able to find it on
809 // d-bus
James Feist73df0db2019-03-25 15:29:35 -0700810 managedItem = findChassis(managedObj, it.key(), chassis);
811 if (managedItem == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800812 {
813 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
Ed Tanousace85d62021-10-26 12:45:59 -0700814 messages::invalidObject(response->res,
815 crow::utility::urlFromPieces(
816 "redfish", "v1", "Chassis", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800817 return CreatePIDRet::fail;
818 }
819 }
820
Ed Tanous26f69762022-01-25 09:49:11 -0800821 if (!profile.empty() &&
James Feist73df0db2019-03-25 15:29:35 -0700822 (type == "PidControllers" || type == "FanControllers" ||
823 type == "StepwiseControllers"))
824 {
825 if (managedItem == nullptr)
826 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800827 output.emplace_back("Profiles", std::vector<std::string>{profile});
James Feist73df0db2019-03-25 15:29:35 -0700828 }
829 else
830 {
831 std::string interface;
832 if (type == "StepwiseControllers")
833 {
834 interface = stepwiseConfigurationIface;
835 }
836 else
837 {
838 interface = pidConfigurationIface;
839 }
Ed Tanous711ac7a2021-12-20 09:34:41 -0800840 bool ifaceFound = false;
841 for (const auto& iface : managedItem->second)
842 {
843 if (iface.first == interface)
844 {
845 ifaceFound = true;
846 for (const auto& prop : iface.second)
847 {
848 if (prop.first == "Profiles")
849 {
850 const std::vector<std::string>* curProfiles =
851 std::get_if<std::vector<std::string>>(
852 &(prop.second));
853 if (curProfiles == nullptr)
854 {
855 BMCWEB_LOG_ERROR
856 << "Illegal profiles in managed object";
857 messages::internalError(response->res);
858 return CreatePIDRet::fail;
859 }
860 if (std::find(curProfiles->begin(),
861 curProfiles->end(),
862 profile) == curProfiles->end())
863 {
864 std::vector<std::string> newProfiles =
865 *curProfiles;
866 newProfiles.push_back(profile);
Ed Tanousb9d36b42022-02-26 21:42:46 -0800867 output.emplace_back("Profiles", newProfiles);
Ed Tanous711ac7a2021-12-20 09:34:41 -0800868 }
869 }
870 }
871 }
872 }
873
874 if (!ifaceFound)
James Feist73df0db2019-03-25 15:29:35 -0700875 {
876 BMCWEB_LOG_ERROR
877 << "Failed to find interface in managed object";
878 messages::internalError(response->res);
879 return CreatePIDRet::fail;
880 }
James Feist73df0db2019-03-25 15:29:35 -0700881 }
882 }
883
James Feist83ff9ab2018-08-31 10:18:24 -0700884 if (type == "PidControllers" || type == "FanControllers")
885 {
886 if (createNewObject)
887 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800888 output.emplace_back("Class",
889 type == "PidControllers" ? "temp" : "fan");
890 output.emplace_back("Type", "Pid");
James Feist83ff9ab2018-08-31 10:18:24 -0700891 }
James Feist5f2caae2018-12-12 14:08:25 -0800892
893 std::optional<std::vector<nlohmann::json>> zones;
894 std::optional<std::vector<std::string>> inputs;
895 std::optional<std::vector<std::string>> outputs;
896 std::map<std::string, std::optional<double>> doubles;
James Feistb943aae2019-07-11 16:33:56 -0700897 std::optional<std::string> setpointOffset;
James Feist5f2caae2018-12-12 14:08:25 -0800898 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -0800899 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
James Feist5f2caae2018-12-12 14:08:25 -0800900 "Zones", zones, "FFGainCoefficient",
901 doubles["FFGainCoefficient"], "FFOffCoefficient",
902 doubles["FFOffCoefficient"], "ICoefficient",
903 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
904 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
905 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
906 "PCoefficient", doubles["PCoefficient"], "SetPoint",
James Feistb943aae2019-07-11 16:33:56 -0700907 doubles["SetPoint"], "SetPointOffset", setpointOffset,
908 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
909 "PositiveHysteresis", doubles["PositiveHysteresis"],
910 "NegativeHysteresis", doubles["NegativeHysteresis"]))
James Feist83ff9ab2018-08-31 10:18:24 -0700911 {
Ed Tanous71f52d92021-02-19 08:51:17 -0800912 BMCWEB_LOG_ERROR
913 << "Illegal Property "
914 << it.value().dump(2, ' ', true,
915 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -0800916 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700917 }
James Feist5f2caae2018-12-12 14:08:25 -0800918 if (zones)
James Feist83ff9ab2018-08-31 10:18:24 -0700919 {
James Feist5f2caae2018-12-12 14:08:25 -0800920 std::vector<std::string> zonesStr;
921 if (!getZonesFromJsonReq(response, *zones, zonesStr))
James Feist83ff9ab2018-08-31 10:18:24 -0700922 {
Gunnar Millsa0744d32020-11-09 15:40:45 -0600923 BMCWEB_LOG_ERROR << "Illegal Zones";
James Feist5f2caae2018-12-12 14:08:25 -0800924 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700925 }
James Feistb6baeaa2019-02-21 10:41:40 -0800926 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -0800927 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800928 {
929 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
Ed Tanousace85d62021-10-26 12:45:59 -0700930 messages::invalidObject(
931 response->res, crow::utility::urlFromPieces(
932 "redfish", "v1", "Chassis", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800933 return CreatePIDRet::fail;
934 }
Ed Tanousb9d36b42022-02-26 21:42:46 -0800935 output.emplace_back("Zones", std::move(zonesStr));
James Feist5f2caae2018-12-12 14:08:25 -0800936 }
937 if (inputs || outputs)
938 {
Ed Tanous02cad962022-06-30 16:50:15 -0700939 std::array<
940 std::reference_wrapper<std::optional<std::vector<std::string>>>,
941 2>
942 containers = {inputs, outputs};
James Feist5f2caae2018-12-12 14:08:25 -0800943 size_t index = 0;
Ed Tanous02cad962022-06-30 16:50:15 -0700944 for (std::optional<std::vector<std::string>>& container :
945 containers)
James Feist83ff9ab2018-08-31 10:18:24 -0700946 {
James Feist5f2caae2018-12-12 14:08:25 -0800947 if (!container)
James Feist83ff9ab2018-08-31 10:18:24 -0700948 {
James Feist5f2caae2018-12-12 14:08:25 -0800949 index++;
950 continue;
James Feist83ff9ab2018-08-31 10:18:24 -0700951 }
James Feist5f2caae2018-12-12 14:08:25 -0800952 for (std::string& value : *container)
James Feist83ff9ab2018-08-31 10:18:24 -0700953 {
Ed Tanousa170f272022-06-30 21:53:27 -0700954 std::replace(value.begin(), value.end(), '_', ' ');
James Feist83ff9ab2018-08-31 10:18:24 -0700955 }
James Feist5f2caae2018-12-12 14:08:25 -0800956 std::string key;
957 if (index == 0)
James Feist83ff9ab2018-08-31 10:18:24 -0700958 {
James Feist5f2caae2018-12-12 14:08:25 -0800959 key = "Inputs";
James Feist83ff9ab2018-08-31 10:18:24 -0700960 }
James Feist5f2caae2018-12-12 14:08:25 -0800961 else
962 {
963 key = "Outputs";
964 }
Ed Tanousb9d36b42022-02-26 21:42:46 -0800965 output.emplace_back(key, *container);
James Feist5f2caae2018-12-12 14:08:25 -0800966 index++;
James Feist83ff9ab2018-08-31 10:18:24 -0700967 }
James Feist5f2caae2018-12-12 14:08:25 -0800968 }
James Feist83ff9ab2018-08-31 10:18:24 -0700969
James Feistb943aae2019-07-11 16:33:56 -0700970 if (setpointOffset)
971 {
972 // translate between redfish and dbus names
973 if (*setpointOffset == "UpperThresholdNonCritical")
974 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800975 output.emplace_back("SetPointOffset", "WarningLow");
James Feistb943aae2019-07-11 16:33:56 -0700976 }
977 else if (*setpointOffset == "LowerThresholdNonCritical")
978 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800979 output.emplace_back("SetPointOffset", "WarningHigh");
James Feistb943aae2019-07-11 16:33:56 -0700980 }
981 else if (*setpointOffset == "LowerThresholdCritical")
982 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800983 output.emplace_back("SetPointOffset", "CriticalLow");
James Feistb943aae2019-07-11 16:33:56 -0700984 }
985 else if (*setpointOffset == "UpperThresholdCritical")
986 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800987 output.emplace_back("SetPointOffset", "CriticalHigh");
James Feistb943aae2019-07-11 16:33:56 -0700988 }
989 else
990 {
991 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
992 << *setpointOffset;
Ed Tanousace85d62021-10-26 12:45:59 -0700993 messages::propertyValueNotInList(response->res, it.key(),
994 "SetPointOffset");
James Feistb943aae2019-07-11 16:33:56 -0700995 return CreatePIDRet::fail;
996 }
997 }
998
James Feist5f2caae2018-12-12 14:08:25 -0800999 // doubles
1000 for (const auto& pairs : doubles)
1001 {
1002 if (!pairs.second)
James Feist83ff9ab2018-08-31 10:18:24 -07001003 {
James Feist5f2caae2018-12-12 14:08:25 -08001004 continue;
James Feist83ff9ab2018-08-31 10:18:24 -07001005 }
James Feist5f2caae2018-12-12 14:08:25 -08001006 BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
Ed Tanousb9d36b42022-02-26 21:42:46 -08001007 output.emplace_back(pairs.first, *pairs.second);
James Feist83ff9ab2018-08-31 10:18:24 -07001008 }
1009 }
James Feist5f2caae2018-12-12 14:08:25 -08001010
James Feist83ff9ab2018-08-31 10:18:24 -07001011 else if (type == "FanZones")
1012 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001013 output.emplace_back("Type", "Pid.Zone");
James Feist83ff9ab2018-08-31 10:18:24 -07001014
James Feist5f2caae2018-12-12 14:08:25 -08001015 std::optional<nlohmann::json> chassisContainer;
1016 std::optional<double> failSafePercent;
James Feistd3ec07f2019-02-25 14:51:15 -08001017 std::optional<double> minThermalOutput;
James Feistb6baeaa2019-02-21 10:41:40 -08001018 if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
James Feist5f2caae2018-12-12 14:08:25 -08001019 chassisContainer, "FailSafePercent",
James Feistd3ec07f2019-02-25 14:51:15 -08001020 failSafePercent, "MinThermalOutput",
1021 minThermalOutput))
James Feist83ff9ab2018-08-31 10:18:24 -07001022 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001023 BMCWEB_LOG_ERROR
1024 << "Illegal Property "
1025 << it.value().dump(2, ' ', true,
1026 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -08001027 return CreatePIDRet::fail;
1028 }
James Feist83ff9ab2018-08-31 10:18:24 -07001029
James Feist5f2caae2018-12-12 14:08:25 -08001030 if (chassisContainer)
1031 {
1032
1033 std::string chassisId;
1034 if (!redfish::json_util::readJson(*chassisContainer, response->res,
1035 "@odata.id", chassisId))
James Feist83ff9ab2018-08-31 10:18:24 -07001036 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001037 BMCWEB_LOG_ERROR
1038 << "Illegal Property "
1039 << chassisContainer->dump(
1040 2, ' ', true,
1041 nlohmann::json::error_handler_t::replace);
James Feist83ff9ab2018-08-31 10:18:24 -07001042 return CreatePIDRet::fail;
1043 }
James Feist5f2caae2018-12-12 14:08:25 -08001044
AppaRao Puli717794d2019-10-18 22:54:53 +05301045 // /redfish/v1/chassis/chassis_name/
James Feist5f2caae2018-12-12 14:08:25 -08001046 if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
1047 {
1048 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
Ed Tanousace85d62021-10-26 12:45:59 -07001049 messages::invalidObject(
1050 response->res, crow::utility::urlFromPieces(
1051 "redfish", "v1", "Chassis", chassisId));
James Feist5f2caae2018-12-12 14:08:25 -08001052 return CreatePIDRet::fail;
1053 }
1054 }
James Feistd3ec07f2019-02-25 14:51:15 -08001055 if (minThermalOutput)
James Feist5f2caae2018-12-12 14:08:25 -08001056 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001057 output.emplace_back("MinThermalOutput", *minThermalOutput);
James Feist5f2caae2018-12-12 14:08:25 -08001058 }
1059 if (failSafePercent)
1060 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001061 output.emplace_back("FailSafePercent", *failSafePercent);
James Feist5f2caae2018-12-12 14:08:25 -08001062 }
1063 }
1064 else if (type == "StepwiseControllers")
1065 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001066 output.emplace_back("Type", "Stepwise");
James Feist5f2caae2018-12-12 14:08:25 -08001067
1068 std::optional<std::vector<nlohmann::json>> zones;
1069 std::optional<std::vector<nlohmann::json>> steps;
1070 std::optional<std::vector<std::string>> inputs;
1071 std::optional<double> positiveHysteresis;
1072 std::optional<double> negativeHysteresis;
James Feistc33a90e2019-03-01 10:17:44 -08001073 std::optional<std::string> direction; // upper clipping curve vs lower
James Feist5f2caae2018-12-12 14:08:25 -08001074 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -08001075 it.value(), response->res, "Zones", zones, "Steps", steps,
1076 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
James Feistc33a90e2019-03-01 10:17:44 -08001077 "NegativeHysteresis", negativeHysteresis, "Direction",
1078 direction))
James Feist5f2caae2018-12-12 14:08:25 -08001079 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001080 BMCWEB_LOG_ERROR
1081 << "Illegal Property "
1082 << it.value().dump(2, ' ', true,
1083 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -08001084 return CreatePIDRet::fail;
1085 }
1086
1087 if (zones)
1088 {
James Feistb6baeaa2019-02-21 10:41:40 -08001089 std::vector<std::string> zonesStrs;
1090 if (!getZonesFromJsonReq(response, *zones, zonesStrs))
James Feist5f2caae2018-12-12 14:08:25 -08001091 {
Gunnar Millsa0744d32020-11-09 15:40:45 -06001092 BMCWEB_LOG_ERROR << "Illegal Zones";
James Feist5f2caae2018-12-12 14:08:25 -08001093 return CreatePIDRet::fail;
1094 }
James Feistb6baeaa2019-02-21 10:41:40 -08001095 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -08001096 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -08001097 {
1098 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
Ed Tanousace85d62021-10-26 12:45:59 -07001099 messages::invalidObject(
1100 response->res, crow::utility::urlFromPieces(
1101 "redfish", "v1", "Chassis", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -08001102 return CreatePIDRet::fail;
1103 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001104 output.emplace_back("Zones", std::move(zonesStrs));
James Feist5f2caae2018-12-12 14:08:25 -08001105 }
1106 if (steps)
1107 {
1108 std::vector<double> readings;
1109 std::vector<double> outputs;
1110 for (auto& step : *steps)
1111 {
Ed Tanous543f4402022-01-06 13:12:53 -08001112 double target = 0.0;
1113 double out = 0.0;
James Feist5f2caae2018-12-12 14:08:25 -08001114
1115 if (!redfish::json_util::readJson(step, response->res, "Target",
Ed Tanous23a21a12020-07-25 04:45:05 +00001116 target, "Output", out))
James Feist5f2caae2018-12-12 14:08:25 -08001117 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001118 BMCWEB_LOG_ERROR
1119 << "Illegal Property "
1120 << it.value().dump(
1121 2, ' ', true,
1122 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -08001123 return CreatePIDRet::fail;
1124 }
1125 readings.emplace_back(target);
Ed Tanous23a21a12020-07-25 04:45:05 +00001126 outputs.emplace_back(out);
James Feist5f2caae2018-12-12 14:08:25 -08001127 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001128 output.emplace_back("Reading", std::move(readings));
1129 output.emplace_back("Output", std::move(outputs));
James Feist5f2caae2018-12-12 14:08:25 -08001130 }
1131 if (inputs)
1132 {
1133 for (std::string& value : *inputs)
1134 {
Ed Tanousa170f272022-06-30 21:53:27 -07001135
1136 std::replace(value.begin(), value.end(), '_', ' ');
James Feist5f2caae2018-12-12 14:08:25 -08001137 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001138 output.emplace_back("Inputs", std::move(*inputs));
James Feist5f2caae2018-12-12 14:08:25 -08001139 }
1140 if (negativeHysteresis)
1141 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001142 output.emplace_back("NegativeHysteresis", *negativeHysteresis);
James Feist5f2caae2018-12-12 14:08:25 -08001143 }
1144 if (positiveHysteresis)
1145 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001146 output.emplace_back("PositiveHysteresis", *positiveHysteresis);
James Feist83ff9ab2018-08-31 10:18:24 -07001147 }
James Feistc33a90e2019-03-01 10:17:44 -08001148 if (direction)
1149 {
1150 constexpr const std::array<const char*, 2> allowedDirections = {
1151 "Ceiling", "Floor"};
1152 if (std::find(allowedDirections.begin(), allowedDirections.end(),
1153 *direction) == allowedDirections.end())
1154 {
1155 messages::propertyValueTypeError(response->res, "Direction",
1156 *direction);
1157 return CreatePIDRet::fail;
1158 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001159 output.emplace_back("Class", *direction);
James Feistc33a90e2019-03-01 10:17:44 -08001160 }
James Feist83ff9ab2018-08-31 10:18:24 -07001161 }
1162 else
1163 {
Gunnar Millsa0744d32020-11-09 15:40:45 -06001164 BMCWEB_LOG_ERROR << "Illegal Type " << type;
Jason M. Bills35a62c72018-10-09 12:45:45 -07001165 messages::propertyUnknown(response->res, type);
James Feist83ff9ab2018-08-31 10:18:24 -07001166 return CreatePIDRet::fail;
1167 }
1168 return CreatePIDRet::patch;
1169}
James Feist73df0db2019-03-25 15:29:35 -07001170struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
1171{
1172
Ed Tanous4e23a442022-06-06 09:57:26 -07001173 explicit GetPIDValues(
1174 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
Ed Tanous23a21a12020-07-25 04:45:05 +00001175 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001176
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001177 {}
James Feist73df0db2019-03-25 15:29:35 -07001178
1179 void run()
1180 {
1181 std::shared_ptr<GetPIDValues> self = shared_from_this();
1182
1183 // get all configurations
1184 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -08001185 [self](
1186 const boost::system::error_code ec,
1187 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001188 if (ec)
1189 {
1190 BMCWEB_LOG_ERROR << ec;
1191 messages::internalError(self->asyncResp->res);
1192 return;
1193 }
1194 self->subtree = subtreeLocal;
James Feist73df0db2019-03-25 15:29:35 -07001195 },
1196 "xyz.openbmc_project.ObjectMapper",
1197 "/xyz/openbmc_project/object_mapper",
1198 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
1199 std::array<const char*, 4>{
1200 pidConfigurationIface, pidZoneConfigurationIface,
1201 objectManagerIface, stepwiseConfigurationIface});
1202
1203 // at the same time get the selected profile
1204 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -08001205 [self](
1206 const boost::system::error_code ec,
1207 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001208 if (ec || subtreeLocal.empty())
1209 {
1210 return;
1211 }
1212 if (subtreeLocal[0].second.size() != 1)
1213 {
1214 // invalid mapper response, should never happen
1215 BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
1216 messages::internalError(self->asyncResp->res);
1217 return;
1218 }
1219
1220 const std::string& path = subtreeLocal[0].first;
1221 const std::string& owner = subtreeLocal[0].second[0].first;
1222 crow::connections::systemBus->async_method_call(
1223 [path, owner,
1224 self](const boost::system::error_code ec2,
1225 const dbus::utility::DBusPropertiesMap& resp) {
1226 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001227 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001228 BMCWEB_LOG_ERROR
1229 << "GetPIDValues: Can't get thermalModeIface " << path;
James Feist73df0db2019-03-25 15:29:35 -07001230 messages::internalError(self->asyncResp->res);
1231 return;
1232 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001233 const std::string* current = nullptr;
1234 const std::vector<std::string>* supported = nullptr;
1235 for (const auto& [key, value] : resp)
1236 {
1237 if (key == "Current")
1238 {
1239 current = std::get_if<std::string>(&value);
1240 if (current == nullptr)
James Feist73df0db2019-03-25 15:29:35 -07001241 {
George Liu0fda0f12021-11-16 10:06:17 +08001242 BMCWEB_LOG_ERROR
1243 << "GetPIDValues: thermal mode iface invalid "
1244 << path;
James Feist73df0db2019-03-25 15:29:35 -07001245 messages::internalError(self->asyncResp->res);
1246 return;
1247 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001248 }
1249 if (key == "Supported")
1250 {
1251 supported =
1252 std::get_if<std::vector<std::string>>(&value);
1253 if (supported == nullptr)
1254 {
1255 BMCWEB_LOG_ERROR
1256 << "GetPIDValues: thermal mode iface invalid"
1257 << path;
1258 messages::internalError(self->asyncResp->res);
1259 return;
1260 }
1261 }
1262 }
1263 if (current == nullptr || supported == nullptr)
1264 {
1265 BMCWEB_LOG_ERROR
1266 << "GetPIDValues: thermal mode iface invalid " << path;
1267 messages::internalError(self->asyncResp->res);
1268 return;
1269 }
1270 self->currentProfile = *current;
1271 self->supportedProfiles = *supported;
1272 },
1273 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
1274 thermalModeIface);
James Feist73df0db2019-03-25 15:29:35 -07001275 },
1276 "xyz.openbmc_project.ObjectMapper",
1277 "/xyz/openbmc_project/object_mapper",
1278 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
1279 std::array<const char*, 1>{thermalModeIface});
1280 }
1281
1282 ~GetPIDValues()
1283 {
1284 if (asyncResp->res.result() != boost::beast::http::status::ok)
1285 {
1286 return;
1287 }
1288 // create map of <connection, path to objMgr>>
1289 boost::container::flat_map<std::string, std::string> objectMgrPaths;
1290 boost::container::flat_set<std::string> calledConnections;
1291 for (const auto& pathGroup : subtree)
1292 {
1293 for (const auto& connectionGroup : pathGroup.second)
1294 {
1295 auto findConnection =
1296 calledConnections.find(connectionGroup.first);
1297 if (findConnection != calledConnections.end())
1298 {
1299 break;
1300 }
1301 for (const std::string& interface : connectionGroup.second)
1302 {
1303 if (interface == objectManagerIface)
1304 {
1305 objectMgrPaths[connectionGroup.first] = pathGroup.first;
1306 }
1307 // this list is alphabetical, so we
1308 // should have found the objMgr by now
1309 if (interface == pidConfigurationIface ||
1310 interface == pidZoneConfigurationIface ||
1311 interface == stepwiseConfigurationIface)
1312 {
1313 auto findObjMgr =
1314 objectMgrPaths.find(connectionGroup.first);
1315 if (findObjMgr == objectMgrPaths.end())
1316 {
1317 BMCWEB_LOG_DEBUG << connectionGroup.first
1318 << "Has no Object Manager";
1319 continue;
1320 }
1321
1322 calledConnections.insert(connectionGroup.first);
1323
1324 asyncPopulatePid(findObjMgr->first, findObjMgr->second,
1325 currentProfile, supportedProfiles,
1326 asyncResp);
1327 break;
1328 }
1329 }
1330 }
1331 }
1332 }
1333
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001334 GetPIDValues(const GetPIDValues&) = delete;
1335 GetPIDValues(GetPIDValues&&) = delete;
1336 GetPIDValues& operator=(const GetPIDValues&) = delete;
1337 GetPIDValues& operator=(GetPIDValues&&) = delete;
1338
James Feist73df0db2019-03-25 15:29:35 -07001339 std::vector<std::string> supportedProfiles;
1340 std::string currentProfile;
Ed Tanousb9d36b42022-02-26 21:42:46 -08001341 dbus::utility::MapperGetSubTreeResponse subtree;
zhanghch058d1b46d2021-04-01 11:18:24 +08001342 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001343};
1344
1345struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
1346{
1347
zhanghch058d1b46d2021-04-01 11:18:24 +08001348 SetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
James Feist73df0db2019-03-25 15:29:35 -07001349 nlohmann::json& data) :
Ed Tanous271584a2019-07-09 16:24:22 -07001350 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001351 {
1352
1353 std::optional<nlohmann::json> pidControllers;
1354 std::optional<nlohmann::json> fanControllers;
1355 std::optional<nlohmann::json> fanZones;
1356 std::optional<nlohmann::json> stepwiseControllers;
1357
1358 if (!redfish::json_util::readJson(
1359 data, asyncResp->res, "PidControllers", pidControllers,
1360 "FanControllers", fanControllers, "FanZones", fanZones,
1361 "StepwiseControllers", stepwiseControllers, "Profile", profile))
1362 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001363 BMCWEB_LOG_ERROR
1364 << "Illegal Property "
1365 << data.dump(2, ' ', true,
1366 nlohmann::json::error_handler_t::replace);
James Feist73df0db2019-03-25 15:29:35 -07001367 return;
1368 }
1369 configuration.emplace_back("PidControllers", std::move(pidControllers));
1370 configuration.emplace_back("FanControllers", std::move(fanControllers));
1371 configuration.emplace_back("FanZones", std::move(fanZones));
1372 configuration.emplace_back("StepwiseControllers",
1373 std::move(stepwiseControllers));
1374 }
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001375
1376 SetPIDValues(const SetPIDValues&) = delete;
1377 SetPIDValues(SetPIDValues&&) = delete;
1378 SetPIDValues& operator=(const SetPIDValues&) = delete;
1379 SetPIDValues& operator=(SetPIDValues&&) = delete;
1380
James Feist73df0db2019-03-25 15:29:35 -07001381 void run()
1382 {
1383 if (asyncResp->res.result() != boost::beast::http::status::ok)
1384 {
1385 return;
1386 }
1387
1388 std::shared_ptr<SetPIDValues> self = shared_from_this();
1389
1390 // todo(james): might make sense to do a mapper call here if this
1391 // interface gets more traction
1392 crow::connections::systemBus->async_method_call(
1393 [self](const boost::system::error_code ec,
Ed Tanous914e2d52022-01-07 11:38:34 -08001394 const dbus::utility::ManagedObjectType& mObj) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001395 if (ec)
1396 {
1397 BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
1398 messages::internalError(self->asyncResp->res);
1399 return;
1400 }
1401 const std::array<const char*, 3> configurations = {
1402 pidConfigurationIface, pidZoneConfigurationIface,
1403 stepwiseConfigurationIface};
James Feiste69d9de2020-02-07 12:23:27 -08001404
Ed Tanous002d39b2022-05-31 08:59:27 -07001405 for (const auto& [path, object] : mObj)
1406 {
1407 for (const auto& [interface, _] : object)
James Feiste69d9de2020-02-07 12:23:27 -08001408 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001409 if (std::find(configurations.begin(), configurations.end(),
1410 interface) != configurations.end())
James Feiste69d9de2020-02-07 12:23:27 -08001411 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001412 self->objectCount++;
1413 break;
James Feiste69d9de2020-02-07 12:23:27 -08001414 }
James Feiste69d9de2020-02-07 12:23:27 -08001415 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001416 }
1417 self->managedObj = mObj;
James Feist73df0db2019-03-25 15:29:35 -07001418 },
1419 "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
1420 "GetManagedObjects");
1421
1422 // at the same time get the profile information
1423 crow::connections::systemBus->async_method_call(
1424 [self](const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001425 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001426 if (ec || subtree.empty())
1427 {
1428 return;
1429 }
1430 if (subtree[0].second.empty())
1431 {
1432 // invalid mapper response, should never happen
1433 BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
1434 messages::internalError(self->asyncResp->res);
1435 return;
1436 }
1437
1438 const std::string& path = subtree[0].first;
1439 const std::string& owner = subtree[0].second[0].first;
1440 crow::connections::systemBus->async_method_call(
1441 [self, path, owner](const boost::system::error_code ec2,
1442 const dbus::utility::DBusPropertiesMap& r) {
1443 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001444 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001445 BMCWEB_LOG_ERROR
1446 << "SetPIDValues: Can't get thermalModeIface " << path;
James Feist73df0db2019-03-25 15:29:35 -07001447 messages::internalError(self->asyncResp->res);
1448 return;
1449 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001450 const std::string* current = nullptr;
1451 const std::vector<std::string>* supported = nullptr;
1452 for (const auto& [key, value] : r)
1453 {
1454 if (key == "Current")
1455 {
1456 current = std::get_if<std::string>(&value);
1457 if (current == nullptr)
James Feist73df0db2019-03-25 15:29:35 -07001458 {
George Liu0fda0f12021-11-16 10:06:17 +08001459 BMCWEB_LOG_ERROR
1460 << "SetPIDValues: thermal mode iface invalid "
1461 << path;
James Feist73df0db2019-03-25 15:29:35 -07001462 messages::internalError(self->asyncResp->res);
1463 return;
1464 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001465 }
1466 if (key == "Supported")
1467 {
1468 supported =
1469 std::get_if<std::vector<std::string>>(&value);
1470 if (supported == nullptr)
1471 {
1472 BMCWEB_LOG_ERROR
1473 << "SetPIDValues: thermal mode iface invalid"
1474 << path;
1475 messages::internalError(self->asyncResp->res);
1476 return;
1477 }
1478 }
1479 }
1480 if (current == nullptr || supported == nullptr)
1481 {
1482 BMCWEB_LOG_ERROR
1483 << "SetPIDValues: thermal mode iface invalid " << path;
1484 messages::internalError(self->asyncResp->res);
1485 return;
1486 }
1487 self->currentProfile = *current;
1488 self->supportedProfiles = *supported;
1489 self->profileConnection = owner;
1490 self->profilePath = path;
1491 },
1492 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
1493 thermalModeIface);
James Feist73df0db2019-03-25 15:29:35 -07001494 },
1495 "xyz.openbmc_project.ObjectMapper",
1496 "/xyz/openbmc_project/object_mapper",
1497 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
1498 std::array<const char*, 1>{thermalModeIface});
1499 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001500 void pidSetDone()
James Feist73df0db2019-03-25 15:29:35 -07001501 {
1502 if (asyncResp->res.result() != boost::beast::http::status::ok)
1503 {
1504 return;
1505 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001506 std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001507 if (profile)
1508 {
1509 if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
1510 *profile) == supportedProfiles.end())
1511 {
1512 messages::actionParameterUnknown(response->res, "Profile",
1513 *profile);
1514 return;
1515 }
1516 currentProfile = *profile;
1517 crow::connections::systemBus->async_method_call(
1518 [response](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001519 if (ec)
1520 {
1521 BMCWEB_LOG_ERROR << "Error patching profile" << ec;
1522 messages::internalError(response->res);
1523 }
James Feist73df0db2019-03-25 15:29:35 -07001524 },
1525 profileConnection, profilePath,
1526 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
Ed Tanous168e20c2021-12-13 14:39:53 -08001527 "Current", dbus::utility::DbusVariantType(*profile));
James Feist73df0db2019-03-25 15:29:35 -07001528 }
1529
1530 for (auto& containerPair : configuration)
1531 {
1532 auto& container = containerPair.second;
1533 if (!container)
1534 {
1535 continue;
1536 }
James Feist6ee7f772020-02-06 16:25:27 -08001537 BMCWEB_LOG_DEBUG << *container;
1538
Ed Tanous02cad962022-06-30 16:50:15 -07001539 const std::string& type = containerPair.first;
James Feist73df0db2019-03-25 15:29:35 -07001540
1541 for (nlohmann::json::iterator it = container->begin();
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301542 it != container->end(); ++it)
James Feist73df0db2019-03-25 15:29:35 -07001543 {
1544 const auto& name = it.key();
James Feist6ee7f772020-02-06 16:25:27 -08001545 BMCWEB_LOG_DEBUG << "looking for " << name;
1546
James Feist73df0db2019-03-25 15:29:35 -07001547 auto pathItr =
1548 std::find_if(managedObj.begin(), managedObj.end(),
1549 [&name](const auto& obj) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001550 return boost::algorithm::ends_with(obj.first.str,
1551 "/" + name);
1552 });
Ed Tanousb9d36b42022-02-26 21:42:46 -08001553 dbus::utility::DBusPropertiesMap output;
James Feist73df0db2019-03-25 15:29:35 -07001554
1555 output.reserve(16); // The pid interface length
1556
1557 // determines if we're patching entity-manager or
1558 // creating a new object
1559 bool createNewObject = (pathItr == managedObj.end());
James Feist6ee7f772020-02-06 16:25:27 -08001560 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
1561
James Feist73df0db2019-03-25 15:29:35 -07001562 std::string iface;
Ed Tanous711ac7a2021-12-20 09:34:41 -08001563 /*
James Feist73df0db2019-03-25 15:29:35 -07001564 if (type == "PidControllers" || type == "FanControllers")
1565 {
1566 iface = pidConfigurationIface;
1567 if (!createNewObject &&
1568 pathItr->second.find(pidConfigurationIface) ==
1569 pathItr->second.end())
1570 {
1571 createNewObject = true;
1572 }
1573 }
1574 else if (type == "FanZones")
1575 {
1576 iface = pidZoneConfigurationIface;
1577 if (!createNewObject &&
1578 pathItr->second.find(pidZoneConfigurationIface) ==
1579 pathItr->second.end())
1580 {
1581
1582 createNewObject = true;
1583 }
1584 }
1585 else if (type == "StepwiseControllers")
1586 {
1587 iface = stepwiseConfigurationIface;
1588 if (!createNewObject &&
1589 pathItr->second.find(stepwiseConfigurationIface) ==
1590 pathItr->second.end())
1591 {
1592 createNewObject = true;
1593 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001594 }*/
James Feist6ee7f772020-02-06 16:25:27 -08001595
1596 if (createNewObject && it.value() == nullptr)
1597 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -05001598 // can't delete a non-existent object
Ed Tanous1668ce62022-02-07 23:44:31 -08001599 messages::propertyValueNotInList(response->res,
1600 it.value().dump(), name);
James Feist6ee7f772020-02-06 16:25:27 -08001601 continue;
1602 }
1603
1604 std::string path;
1605 if (pathItr != managedObj.end())
1606 {
1607 path = pathItr->first.str;
1608 }
1609
James Feist73df0db2019-03-25 15:29:35 -07001610 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
James Feiste69d9de2020-02-07 12:23:27 -08001611
1612 // arbitrary limit to avoid attacks
1613 constexpr const size_t controllerLimit = 500;
James Feist14b0b8d2020-02-12 11:52:07 -08001614 if (createNewObject && objectCount >= controllerLimit)
James Feiste69d9de2020-02-07 12:23:27 -08001615 {
1616 messages::resourceExhaustion(response->res, type);
1617 continue;
1618 }
Ed Tanousa170f272022-06-30 21:53:27 -07001619 std::string escaped = name;
1620 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1621 output.emplace_back("Name", escaped);
James Feist73df0db2019-03-25 15:29:35 -07001622
1623 std::string chassis;
1624 CreatePIDRet ret = createPidInterface(
James Feist6ee7f772020-02-06 16:25:27 -08001625 response, type, it, path, managedObj, createNewObject,
1626 output, chassis, currentProfile);
James Feist73df0db2019-03-25 15:29:35 -07001627 if (ret == CreatePIDRet::fail)
1628 {
1629 return;
1630 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001631 if (ret == CreatePIDRet::del)
James Feist73df0db2019-03-25 15:29:35 -07001632 {
1633 continue;
1634 }
1635
1636 if (!createNewObject)
1637 {
1638 for (const auto& property : output)
1639 {
1640 crow::connections::systemBus->async_method_call(
1641 [response,
1642 propertyName{std::string(property.first)}](
1643 const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001644 if (ec)
1645 {
1646 BMCWEB_LOG_ERROR << "Error patching "
1647 << propertyName << ": " << ec;
1648 messages::internalError(response->res);
1649 return;
1650 }
1651 messages::success(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001652 },
James Feist6ee7f772020-02-06 16:25:27 -08001653 "xyz.openbmc_project.EntityManager", path,
James Feist73df0db2019-03-25 15:29:35 -07001654 "org.freedesktop.DBus.Properties", "Set", iface,
1655 property.first, property.second);
1656 }
1657 }
1658 else
1659 {
1660 if (chassis.empty())
1661 {
1662 BMCWEB_LOG_ERROR << "Failed to get chassis from config";
Ed Tanousace85d62021-10-26 12:45:59 -07001663 messages::internalError(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001664 return;
1665 }
1666
1667 bool foundChassis = false;
1668 for (const auto& obj : managedObj)
1669 {
1670 if (boost::algorithm::ends_with(obj.first.str, chassis))
1671 {
1672 chassis = obj.first.str;
1673 foundChassis = true;
1674 break;
1675 }
1676 }
1677 if (!foundChassis)
1678 {
1679 BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
1680 messages::resourceMissingAtURI(
Ed Tanousace85d62021-10-26 12:45:59 -07001681 response->res,
1682 crow::utility::urlFromPieces("redfish", "v1",
1683 "Chassis", chassis));
James Feist73df0db2019-03-25 15:29:35 -07001684 return;
1685 }
1686
1687 crow::connections::systemBus->async_method_call(
1688 [response](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001689 if (ec)
1690 {
1691 BMCWEB_LOG_ERROR << "Error Adding Pid Object "
1692 << ec;
1693 messages::internalError(response->res);
1694 return;
1695 }
1696 messages::success(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001697 },
1698 "xyz.openbmc_project.EntityManager", chassis,
1699 "xyz.openbmc_project.AddObject", "AddObject", output);
1700 }
1701 }
1702 }
1703 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001704
1705 ~SetPIDValues()
1706 {
1707 try
1708 {
1709 pidSetDone();
1710 }
1711 catch (...)
1712 {
1713 BMCWEB_LOG_CRITICAL << "pidSetDone threw exception";
1714 }
1715 }
1716
zhanghch058d1b46d2021-04-01 11:18:24 +08001717 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001718 std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
1719 configuration;
1720 std::optional<std::string> profile;
1721 dbus::utility::ManagedObjectType managedObj;
1722 std::vector<std::string> supportedProfiles;
1723 std::string currentProfile;
1724 std::string profileConnection;
1725 std::string profilePath;
James Feist14b0b8d2020-02-12 11:52:07 -08001726 size_t objectCount = 0;
James Feist73df0db2019-03-25 15:29:35 -07001727};
James Feist83ff9ab2018-08-31 10:18:24 -07001728
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001729/**
1730 * @brief Retrieves BMC manager location data over DBus
1731 *
1732 * @param[in] aResp Shared pointer for completing asynchronous calls
1733 * @param[in] connectionName - service name
1734 * @param[in] path - object path
1735 * @return none
1736 */
zhanghch058d1b46d2021-04-01 11:18:24 +08001737inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001738 const std::string& connectionName,
1739 const std::string& path)
1740{
1741 BMCWEB_LOG_DEBUG << "Get BMC manager Location data.";
1742
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001743 sdbusplus::asio::getProperty<std::string>(
1744 *crow::connections::systemBus, connectionName, path,
1745 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001746 [aResp](const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001747 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001748 if (ec)
1749 {
1750 BMCWEB_LOG_DEBUG << "DBUS response error for "
1751 "Location";
1752 messages::internalError(aResp->res);
1753 return;
1754 }
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001755
Ed Tanous002d39b2022-05-31 08:59:27 -07001756 aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
1757 property;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001758 });
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001759}
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001760// avoid name collision systems.hpp
1761inline void
1762 managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001763{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001764 BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time";
Ed Tanous52cc1122020-07-18 13:51:21 -07001765
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001766 sdbusplus::asio::getProperty<uint64_t>(
1767 *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
1768 "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
1769 "LastRebootTime",
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001770 [aResp](const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001771 const uint64_t lastResetTime) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001772 if (ec)
1773 {
1774 BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
1775 return;
1776 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001777
Ed Tanous002d39b2022-05-31 08:59:27 -07001778 // LastRebootTime is epoch time, in milliseconds
1779 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
1780 uint64_t lastResetTimeStamp = lastResetTime / 1000;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001781
Ed Tanous002d39b2022-05-31 08:59:27 -07001782 // Convert to ISO 8601 standard
1783 aResp->res.jsonValue["LastResetTime"] =
1784 crow::utility::getDateTimeUint(lastResetTimeStamp);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001785 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001786}
1787
1788/**
1789 * @brief Set the running firmware image
1790 *
1791 * @param[i,o] aResp - Async response object
1792 * @param[i] runningFirmwareTarget - Image to make the running image
1793 *
1794 * @return void
1795 */
1796inline void
1797 setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1798 const std::string& runningFirmwareTarget)
1799{
1800 // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1801 std::string::size_type idPos = runningFirmwareTarget.rfind('/');
1802 if (idPos == std::string::npos)
1803 {
1804 messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
1805 "@odata.id");
1806 BMCWEB_LOG_DEBUG << "Can't parse firmware ID!";
1807 return;
1808 }
1809 idPos++;
1810 if (idPos >= runningFirmwareTarget.size())
1811 {
1812 messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
1813 "@odata.id");
1814 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
1815 return;
1816 }
1817 std::string firmwareId = runningFirmwareTarget.substr(idPos);
1818
1819 // Make sure the image is valid before setting priority
1820 crow::connections::systemBus->async_method_call(
Ed Tanous711ac7a2021-12-20 09:34:41 -08001821 [aResp, firmwareId,
1822 runningFirmwareTarget](const boost::system::error_code ec,
1823 dbus::utility::ManagedObjectType& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001824 if (ec)
1825 {
1826 BMCWEB_LOG_DEBUG << "D-Bus response error getting objects.";
1827 messages::internalError(aResp->res);
1828 return;
1829 }
1830
1831 if (subtree.empty())
1832 {
1833 BMCWEB_LOG_DEBUG << "Can't find image!";
1834 messages::internalError(aResp->res);
1835 return;
1836 }
1837
1838 bool foundImage = false;
Ed Tanous02cad962022-06-30 16:50:15 -07001839 for (const auto& object : subtree)
Ed Tanous002d39b2022-05-31 08:59:27 -07001840 {
1841 const std::string& path =
1842 static_cast<const std::string&>(object.first);
1843 std::size_t idPos2 = path.rfind('/');
1844
1845 if (idPos2 == std::string::npos)
1846 {
1847 continue;
1848 }
1849
1850 idPos2++;
1851 if (idPos2 >= path.size())
1852 {
1853 continue;
1854 }
1855
1856 if (path.substr(idPos2) == firmwareId)
1857 {
1858 foundImage = true;
1859 break;
1860 }
1861 }
1862
1863 if (!foundImage)
1864 {
1865 messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
1866 "@odata.id");
1867 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
1868 return;
1869 }
1870
1871 BMCWEB_LOG_DEBUG << "Setting firmware version " << firmwareId
1872 << " to priority 0.";
1873
1874 // Only support Immediate
1875 // An addition could be a Redfish Setting like
1876 // ActiveSoftwareImageApplyTime and support OnReset
1877 crow::connections::systemBus->async_method_call(
Ed Tanous8a592812022-06-04 09:06:59 -07001878 [aResp](const boost::system::error_code ec2) {
1879 if (ec2)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001880 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001881 BMCWEB_LOG_DEBUG << "D-Bus response error setting.";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001882 messages::internalError(aResp->res);
1883 return;
1884 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001885 doBMCGracefulRestart(aResp);
1886 },
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001887
Ed Tanous002d39b2022-05-31 08:59:27 -07001888 "xyz.openbmc_project.Software.BMC.Updater",
1889 "/xyz/openbmc_project/software/" + firmwareId,
1890 "org.freedesktop.DBus.Properties", "Set",
1891 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1892 dbus::utility::DbusVariantType(static_cast<uint8_t>(0)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001893 },
1894 "xyz.openbmc_project.Software.BMC.Updater",
1895 "/xyz/openbmc_project/software", "org.freedesktop.DBus.ObjectManager",
1896 "GetManagedObjects");
1897}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001898
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001899inline void setDateTime(std::shared_ptr<bmcweb::AsyncResp> aResp,
1900 std::string datetime)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001901{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001902 BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001903
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001904 std::stringstream stream(datetime);
1905 // Convert from ISO 8601 to boost local_time
1906 // (BMC only has time in UTC)
1907 boost::posix_time::ptime posixTime;
1908 boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
1909 // Facet gets deleted with the stringsteam
1910 auto ifc = std::make_unique<boost::local_time::local_time_input_facet>(
1911 "%Y-%m-%d %H:%M:%S%F %ZP");
1912 stream.imbue(std::locale(stream.getloc(), ifc.release()));
1913
1914 boost::local_time::local_date_time ldt(boost::local_time::not_a_date_time);
1915
1916 if (stream >> ldt)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001917 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001918 posixTime = ldt.utc_time();
1919 boost::posix_time::time_duration dur = posixTime - epoch;
1920 uint64_t durMicroSecs = static_cast<uint64_t>(dur.total_microseconds());
1921 crow::connections::systemBus->async_method_call(
1922 [aResp{std::move(aResp)}, datetime{std::move(datetime)}](
1923 const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001924 if (ec)
1925 {
1926 BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1927 "DBUS response error "
1928 << ec;
1929 messages::internalError(aResp->res);
1930 return;
1931 }
1932 aResp->res.jsonValue["DateTime"] = datetime;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001933 },
1934 "xyz.openbmc_project.Time.Manager", "/xyz/openbmc_project/time/bmc",
1935 "org.freedesktop.DBus.Properties", "Set",
1936 "xyz.openbmc_project.Time.EpochTime", "Elapsed",
Ed Tanous168e20c2021-12-13 14:39:53 -08001937 dbus::utility::DbusVariantType(durMicroSecs));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001938 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001939 else
1940 {
1941 messages::propertyValueFormatError(aResp->res, datetime, "DateTime");
1942 return;
1943 }
1944}
1945
1946inline void requestRoutesManager(App& app)
1947{
1948 std::string uuid = persistent_data::getConfig().systemUuid;
1949
1950 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07001951 .privileges(redfish::privileges::getManager)
Ed Tanous002d39b2022-05-31 08:59:27 -07001952 .methods(boost::beast::http::verb::get)(
1953 [&app, uuid](const crow::Request& req,
Ed Tanous45ca1b82022-03-25 13:07:27 -07001954 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001955 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001956 {
1957 return;
1958 }
1959 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
Sui Chena51fc2d2022-07-14 17:21:53 -07001960 asyncResp->res.jsonValue["@odata.type"] = "#Manager.v1_14_0.Manager";
Ed Tanous002d39b2022-05-31 08:59:27 -07001961 asyncResp->res.jsonValue["Id"] = "bmc";
1962 asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
1963 asyncResp->res.jsonValue["Description"] =
1964 "Baseboard Management Controller";
1965 asyncResp->res.jsonValue["PowerState"] = "On";
1966 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
1967 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
Ed Tanous14766872022-03-15 10:44:42 -07001968
Ed Tanous002d39b2022-05-31 08:59:27 -07001969 asyncResp->res.jsonValue["ManagerType"] = "BMC";
1970 asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
1971 asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1972 asyncResp->res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001973
Ed Tanous002d39b2022-05-31 08:59:27 -07001974 asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
1975 "/redfish/v1/Managers/bmc/LogServices";
1976 asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
1977 "/redfish/v1/Managers/bmc/NetworkProtocol";
1978 asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
1979 "/redfish/v1/Managers/bmc/EthernetInterfaces";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001980
1981#ifdef BMCWEB_ENABLE_VM_NBDPROXY
Ed Tanous002d39b2022-05-31 08:59:27 -07001982 asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
1983 "/redfish/v1/Managers/bmc/VirtualMedia";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001984#endif // BMCWEB_ENABLE_VM_NBDPROXY
1985
Ed Tanous002d39b2022-05-31 08:59:27 -07001986 // default oem data
1987 nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
1988 nlohmann::json& oemOpenbmc = oem["OpenBmc"];
1989 oem["@odata.type"] = "#OemManager.Oem";
1990 oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
1991 oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
1992 oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
Ed Tanous14766872022-03-15 10:44:42 -07001993
Ed Tanous002d39b2022-05-31 08:59:27 -07001994 nlohmann::json::object_t certificates;
1995 certificates["@odata.id"] =
1996 "/redfish/v1/Managers/bmc/Truststore/Certificates";
1997 oemOpenbmc["Certificates"] = std::move(certificates);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001998
Ed Tanous002d39b2022-05-31 08:59:27 -07001999 // Manager.Reset (an action) can be many values, OpenBMC only
2000 // supports BMC reboot.
2001 nlohmann::json& managerReset =
2002 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
2003 managerReset["target"] =
2004 "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
2005 managerReset["@Redfish.ActionInfo"] =
2006 "/redfish/v1/Managers/bmc/ResetActionInfo";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002007
Ed Tanous002d39b2022-05-31 08:59:27 -07002008 // ResetToDefaults (Factory Reset) has values like
2009 // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
2010 // on OpenBMC
2011 nlohmann::json& resetToDefaults =
2012 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
2013 resetToDefaults["target"] =
2014 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
2015 resetToDefaults["ResetType@Redfish.AllowableValues"] = {"ResetAll"};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002016
Ed Tanous002d39b2022-05-31 08:59:27 -07002017 std::pair<std::string, std::string> redfishDateTimeOffset =
2018 crow::utility::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05302019
Ed Tanous002d39b2022-05-31 08:59:27 -07002020 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2021 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2022 redfishDateTimeOffset.second;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002023
Ed Tanous002d39b2022-05-31 08:59:27 -07002024 // TODO (Gunnar): Remove these one day since moved to ComputerSystem
2025 // Still used by OCP profiles
2026 // https://github.com/opencomputeproject/OCP-Profiles/issues/23
2027 // Fill in SerialConsole info
2028 asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
2029 asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
2030 asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] = {
2031 "IPMI", "SSH"};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002032#ifdef BMCWEB_ENABLE_KVM
Ed Tanous002d39b2022-05-31 08:59:27 -07002033 // Fill in GraphicalConsole info
2034 asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
2035 asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] =
2036 4;
2037 asyncResp->res
2038 .jsonValue["GraphicalConsole"]["ConnectTypesSupported"] = {"KVMIP"};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002039#endif // BMCWEB_ENABLE_KVM
2040
Ed Tanous002d39b2022-05-31 08:59:27 -07002041 asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
Ed Tanous14766872022-03-15 10:44:42 -07002042
Ed Tanous002d39b2022-05-31 08:59:27 -07002043 nlohmann::json::array_t managerForServers;
2044 nlohmann::json::object_t manager;
2045 manager["@odata.id"] = "/redfish/v1/Systems/system";
2046 managerForServers.push_back(std::move(manager));
2047
2048 asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
2049 std::move(managerForServers);
2050
2051 auto health = std::make_shared<HealthPopulate>(asyncResp);
2052 health->isManagersHealth = true;
2053 health->populate();
2054
Willy Tueee00132022-06-14 14:53:17 -07002055 sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
Ed Tanous002d39b2022-05-31 08:59:27 -07002056 "FirmwareVersion", true);
2057
2058 managerGetLastResetTime(asyncResp);
2059
Sui Chena51fc2d2022-07-14 17:21:53 -07002060 // ManagerDiagnosticData is added for all BMCs.
2061 nlohmann::json& managerDiagnosticData =
2062 asyncResp->res.jsonValue["ManagerDiagnosticData"];
2063 managerDiagnosticData["@odata.id"] =
2064 "/redfish/v1/Managers/bmc/ManagerDiagnosticData";
2065
Ed Tanous002d39b2022-05-31 08:59:27 -07002066 auto pids = std::make_shared<GetPIDValues>(asyncResp);
2067 pids->run();
2068
2069 getMainChassisId(asyncResp,
2070 [](const std::string& chassisId,
2071 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2072 aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
2073 nlohmann::json::array_t managerForChassis;
Ed Tanous8a592812022-06-04 09:06:59 -07002074 nlohmann::json::object_t managerObj;
2075 managerObj["@odata.id"] = "/redfish/v1/Chassis/" + chassisId;
2076 managerForChassis.push_back(std::move(managerObj));
Ed Tanous002d39b2022-05-31 08:59:27 -07002077 aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
2078 std::move(managerForChassis);
2079 aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
2080 "/redfish/v1/Chassis/" + chassisId;
2081 });
Ed Tanous14766872022-03-15 10:44:42 -07002082
Ed Tanous002d39b2022-05-31 08:59:27 -07002083 static bool started = false;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002084
Ed Tanous002d39b2022-05-31 08:59:27 -07002085 if (!started)
2086 {
2087 sdbusplus::asio::getProperty<double>(
2088 *crow::connections::systemBus, "org.freedesktop.systemd1",
2089 "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
2090 "Progress",
2091 [asyncResp](const boost::system::error_code ec,
2092 const double& val) {
2093 if (ec)
2094 {
2095 BMCWEB_LOG_ERROR << "Error while getting progress";
2096 messages::internalError(asyncResp->res);
2097 return;
2098 }
2099 if (val < 1.0)
2100 {
2101 asyncResp->res.jsonValue["Status"]["State"] = "Starting";
2102 started = true;
2103 }
2104 });
2105 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002106
Ed Tanous002d39b2022-05-31 08:59:27 -07002107 crow::connections::systemBus->async_method_call(
2108 [asyncResp](
2109 const boost::system::error_code ec,
2110 const dbus::utility::MapperGetSubTreeResponse& subtree) {
2111 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002112 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002113 BMCWEB_LOG_DEBUG << "D-Bus response error on GetSubTree " << ec;
2114 return;
2115 }
2116 if (subtree.empty())
2117 {
2118 BMCWEB_LOG_DEBUG << "Can't find bmc D-Bus object!";
2119 return;
2120 }
2121 // Assume only 1 bmc D-Bus object
2122 // Throw an error if there is more than 1
2123 if (subtree.size() > 1)
2124 {
2125 BMCWEB_LOG_DEBUG << "Found more than 1 bmc D-Bus object!";
2126 messages::internalError(asyncResp->res);
2127 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002128 }
2129
Ed Tanous002d39b2022-05-31 08:59:27 -07002130 if (subtree[0].first.empty() || subtree[0].second.size() != 1)
2131 {
2132 BMCWEB_LOG_DEBUG << "Error getting bmc D-Bus object!";
2133 messages::internalError(asyncResp->res);
2134 return;
2135 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002136
Ed Tanous002d39b2022-05-31 08:59:27 -07002137 const std::string& path = subtree[0].first;
2138 const std::string& connectionName = subtree[0].second[0].first;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002139
Ed Tanous002d39b2022-05-31 08:59:27 -07002140 for (const auto& interfaceName : subtree[0].second[0].second)
2141 {
2142 if (interfaceName ==
2143 "xyz.openbmc_project.Inventory.Decorator.Asset")
2144 {
2145 crow::connections::systemBus->async_method_call(
Ed Tanous8a592812022-06-04 09:06:59 -07002146 [asyncResp](const boost::system::error_code ec2,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002147 const dbus::utility::DBusPropertiesMap&
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002148 propertiesList) {
Ed Tanous8a592812022-06-04 09:06:59 -07002149 if (ec2)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002150 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002151 BMCWEB_LOG_DEBUG << "Can't get bmc asset!";
2152 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002153 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002154 for (const std::pair<std::string,
2155 dbus::utility::DbusVariantType>&
2156 property : propertiesList)
2157 {
2158 const std::string& propertyName = property.first;
2159
2160 if ((propertyName == "PartNumber") ||
2161 (propertyName == "SerialNumber") ||
2162 (propertyName == "Manufacturer") ||
2163 (propertyName == "Model") ||
2164 (propertyName == "SparePartNumber"))
2165 {
2166 const std::string* value =
2167 std::get_if<std::string>(&property.second);
2168 if (value == nullptr)
2169 {
2170 // illegal property
2171 messages::internalError(asyncResp->res);
2172 return;
2173 }
2174 asyncResp->res.jsonValue[propertyName] = *value;
2175 }
2176 }
2177 },
2178 connectionName, path, "org.freedesktop.DBus.Properties",
2179 "GetAll",
2180 "xyz.openbmc_project.Inventory.Decorator.Asset");
2181 }
2182 else if (interfaceName ==
2183 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
2184 {
2185 getLocation(asyncResp, connectionName, path);
2186 }
2187 }
2188 },
2189 "xyz.openbmc_project.ObjectMapper",
2190 "/xyz/openbmc_project/object_mapper",
2191 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2192 "/xyz/openbmc_project/inventory", int32_t(0),
2193 std::array<const char*, 1>{
2194 "xyz.openbmc_project.Inventory.Item.Bmc"});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002195 });
2196
2197 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07002198 .privileges(redfish::privileges::patchManager)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002199 .methods(boost::beast::http::verb::patch)(
2200 [&app](const crow::Request& req,
2201 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002202 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002203 {
2204 return;
2205 }
2206 std::optional<nlohmann::json> oem;
2207 std::optional<nlohmann::json> links;
2208 std::optional<std::string> datetime;
2209
2210 if (!json_util::readJsonPatch(req, asyncResp->res, "Oem", oem,
2211 "DateTime", datetime, "Links", links))
2212 {
2213 return;
2214 }
2215
2216 if (oem)
2217 {
2218 std::optional<nlohmann::json> openbmc;
2219 if (!redfish::json_util::readJson(*oem, asyncResp->res, "OpenBmc",
2220 openbmc))
2221 {
2222 BMCWEB_LOG_ERROR
2223 << "Illegal Property "
2224 << oem->dump(2, ' ', true,
2225 nlohmann::json::error_handler_t::replace);
2226 return;
2227 }
2228 if (openbmc)
2229 {
2230 std::optional<nlohmann::json> fan;
2231 if (!redfish::json_util::readJson(*openbmc, asyncResp->res,
2232 "Fan", fan))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002233 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002234 BMCWEB_LOG_ERROR
2235 << "Illegal Property "
2236 << openbmc->dump(
2237 2, ' ', true,
2238 nlohmann::json::error_handler_t::replace);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002239 return;
2240 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002241 if (fan)
2242 {
2243 auto pid = std::make_shared<SetPIDValues>(asyncResp, *fan);
2244 pid->run();
2245 }
2246 }
2247 }
2248 if (links)
2249 {
2250 std::optional<nlohmann::json> activeSoftwareImage;
2251 if (!redfish::json_util::readJson(*links, asyncResp->res,
2252 "ActiveSoftwareImage",
2253 activeSoftwareImage))
2254 {
2255 return;
2256 }
2257 if (activeSoftwareImage)
2258 {
2259 std::optional<std::string> odataId;
2260 if (!json_util::readJson(*activeSoftwareImage, asyncResp->res,
2261 "@odata.id", odataId))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002262 {
Ed Tanous45ca1b82022-03-25 13:07:27 -07002263 return;
2264 }
2265
Ed Tanous002d39b2022-05-31 08:59:27 -07002266 if (odataId)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002267 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002268 setActiveFirmwareImage(asyncResp, *odataId);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002269 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002270 }
2271 }
2272 if (datetime)
2273 {
2274 setDateTime(asyncResp, std::move(*datetime));
2275 }
2276 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002277}
2278
2279inline void requestRoutesManagerCollection(App& app)
2280{
2281 BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
Ed Tanoused398212021-06-09 17:05:54 -07002282 .privileges(redfish::privileges::getManagerCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002283 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002284 [&app](const crow::Request& req,
2285 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002286 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002287 {
2288 return;
2289 }
2290 // Collections don't include the static data added by SubRoute
2291 // because it has a duplicate entry for members
2292 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
2293 asyncResp->res.jsonValue["@odata.type"] =
2294 "#ManagerCollection.ManagerCollection";
2295 asyncResp->res.jsonValue["Name"] = "Manager Collection";
2296 asyncResp->res.jsonValue["Members@odata.count"] = 1;
2297 nlohmann::json::array_t members;
2298 nlohmann::json& bmc = members.emplace_back();
2299 bmc["@odata.id"] = "/redfish/v1/Managers/bmc";
2300 asyncResp->res.jsonValue["Members"] = std::move(members);
2301 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002302}
Ed Tanous1abe55e2018-09-05 08:30:59 -07002303} // namespace redfish