blob: aa4c694da9fcf25aeb98bc2068348f605eba5643 [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
James Feistb49ac872019-05-21 15:12:01 -070018#include "health.hpp"
Jennifer Leec5d03ff2019-03-08 15:42:58 -080019#include "redfish_util.hpp"
Borawski.Lukasz9c3106852018-02-09 15:24:22 +010020
John Edward Broadbent7e860f12021-04-08 15:57:16 -070021#include <app.hpp>
James Feist5b4aa862018-08-16 14:07:01 -070022#include <boost/algorithm/string/replace.hpp>
Santosh Puranikaf5d60582019-03-20 18:16:36 +053023#include <boost/date_time.hpp>
James Feist5b4aa862018-08-16 14:07:01 -070024#include <dbus_utility.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070025#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070026#include <registries/privilege_registry.hpp>
Andrew Geisslere90c5052019-06-28 13:52:27 -050027#include <utils/fw_utils.hpp>
Bernard Wong7bffdb72019-03-20 16:17:21 +080028#include <utils/systemd_utils.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050029
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) {
58 // 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 }
65
66 messages::success(asyncResp->res);
67 },
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) {
87 // 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 }
94
95 messages::success(asyncResp->res);
96 },
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) {
118 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
119 {
120 return;
121 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700122 BMCWEB_LOG_DEBUG << "Post Manager Reset.";
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500123
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700124 std::string resetType;
Jennifer Leeed5befb2018-08-10 11:29:45 -0700125
Willy Tu15ed6782021-12-14 11:03:16 -0800126 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
127 resetType))
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700128 {
129 return;
130 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500131
John Edward Broadbent7e860f12021-04-08 15:57:16 -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");
148
149 return;
150 });
151}
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) {
178 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
179 {
180 return;
181 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700182 BMCWEB_LOG_DEBUG << "Post ResetToDefaults.";
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500183
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700184 std::string resetType;
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500185
Willy Tu15ed6782021-12-14 11:03:16 -0800186 if (!json_util::readJsonAction(
187 req, asyncResp->res, "ResetToDefaultsType", resetType))
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500188 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700189 BMCWEB_LOG_DEBUG << "Missing property ResetToDefaultsType.";
190
191 messages::actionParameterMissing(asyncResp->res,
192 "ResetToDefaults",
193 "ResetToDefaultsType");
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500194 return;
195 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700196
197 if (resetType != "ResetAll")
198 {
George Liu0fda0f12021-11-16 10:06:17 +0800199 BMCWEB_LOG_DEBUG
200 << "Invalid property value for ResetToDefaultsType: "
201 << resetType;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700202 messages::actionParameterNotSupported(
203 asyncResp->res, resetType, "ResetToDefaultsType");
204 return;
205 }
206
207 crow::connections::systemBus->async_method_call(
208 [asyncResp](const boost::system::error_code ec) {
209 if (ec)
210 {
211 BMCWEB_LOG_DEBUG << "Failed to ResetToDefaults: "
212 << ec;
213 messages::internalError(asyncResp->res);
214 return;
215 }
216 // Factory Reset doesn't actually happen until a reboot
217 // Can't erase what the BMC is running on
218 doBMCGracefulRestart(asyncResp);
219 },
220 "xyz.openbmc_project.Software.BMC.Updater",
221 "/xyz/openbmc_project/software",
222 "xyz.openbmc_project.Common.FactoryReset", "Reset");
223 });
224}
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500225
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530226/**
227 * ManagerResetActionInfo derived class for delivering Manager
228 * ResetType AllowableValues using ResetInfo schema.
229 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700230inline void requestRoutesManagerResetActionInfo(App& app)
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530231{
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530232 /**
233 * Functions triggers appropriate requests on DBus
234 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700235
236 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/ResetActionInfo/")
Ed Tanoused398212021-06-09 17:05:54 -0700237 .privileges(redfish::privileges::getActionInfo)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700238 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700239 [&app](const crow::Request& req,
240 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
241 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
242 {
243 return;
244 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700245 asyncResp->res.jsonValue = {
246 {"@odata.type", "#ActionInfo.v1_1_2.ActionInfo"},
247 {"@odata.id", "/redfish/v1/Managers/bmc/ResetActionInfo"},
248 {"Name", "Reset Action Info"},
249 {"Id", "ResetActionInfo"},
250 {"Parameters",
251 {{{"Name", "ResetType"},
252 {"Required", true},
253 {"DataType", "String"},
254 {"AllowableValues",
255 {"GracefulRestart", "ForceRestart"}}}}}};
256 });
257}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530258
James Feist5b4aa862018-08-16 14:07:01 -0700259static constexpr const char* objectManagerIface =
260 "org.freedesktop.DBus.ObjectManager";
261static constexpr const char* pidConfigurationIface =
262 "xyz.openbmc_project.Configuration.Pid";
263static constexpr const char* pidZoneConfigurationIface =
264 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feistb7a08d02018-12-11 14:55:37 -0800265static constexpr const char* stepwiseConfigurationIface =
266 "xyz.openbmc_project.Configuration.Stepwise";
James Feist73df0db2019-03-25 15:29:35 -0700267static constexpr const char* thermalModeIface =
268 "xyz.openbmc_project.Control.ThermalMode";
Borawski.Lukasz9c3106852018-02-09 15:24:22 +0100269
zhanghch058d1b46d2021-04-01 11:18:24 +0800270inline void
271 asyncPopulatePid(const std::string& connection, const std::string& path,
272 const std::string& currentProfile,
273 const std::vector<std::string>& supportedProfiles,
274 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
James Feist5b4aa862018-08-16 14:07:01 -0700275{
276
277 crow::connections::systemBus->async_method_call(
James Feist73df0db2019-03-25 15:29:35 -0700278 [asyncResp, currentProfile, supportedProfiles](
279 const boost::system::error_code ec,
280 const dbus::utility::ManagedObjectType& managedObj) {
James Feist5b4aa862018-08-16 14:07:01 -0700281 if (ec)
282 {
283 BMCWEB_LOG_ERROR << ec;
James Feist5b4aa862018-08-16 14:07:01 -0700284 asyncResp->res.jsonValue.clear();
Jason M. Billsf12894f2018-10-09 12:45:45 -0700285 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700286 return;
287 }
288 nlohmann::json& configRoot =
289 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
290 nlohmann::json& fans = configRoot["FanControllers"];
291 fans["@odata.type"] = "#OemManager.FanControllers";
George Liu0fda0f12021-11-16 10:06:17 +0800292 fans["@odata.id"] =
293 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers";
James Feist5b4aa862018-08-16 14:07:01 -0700294
295 nlohmann::json& pids = configRoot["PidControllers"];
296 pids["@odata.type"] = "#OemManager.PidControllers";
James Feist5b4aa862018-08-16 14:07:01 -0700297 pids["@odata.id"] =
298 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
299
James Feistb7a08d02018-12-11 14:55:37 -0800300 nlohmann::json& stepwise = configRoot["StepwiseControllers"];
301 stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
James Feistb7a08d02018-12-11 14:55:37 -0800302 stepwise["@odata.id"] =
303 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
304
James Feist5b4aa862018-08-16 14:07:01 -0700305 nlohmann::json& zones = configRoot["FanZones"];
306 zones["@odata.id"] =
307 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
308 zones["@odata.type"] = "#OemManager.FanZones";
James Feist5b4aa862018-08-16 14:07:01 -0700309 configRoot["@odata.id"] =
310 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
311 configRoot["@odata.type"] = "#OemManager.Fan";
James Feist73df0db2019-03-25 15:29:35 -0700312 configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
313
314 if (!currentProfile.empty())
315 {
316 configRoot["Profile"] = currentProfile;
317 }
318 BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !";
James Feist5b4aa862018-08-16 14:07:01 -0700319
James Feist5b4aa862018-08-16 14:07:01 -0700320 for (const auto& pathPair : managedObj)
321 {
322 for (const auto& intfPair : pathPair.second)
323 {
324 if (intfPair.first != pidConfigurationIface &&
James Feistb7a08d02018-12-11 14:55:37 -0800325 intfPair.first != pidZoneConfigurationIface &&
326 intfPair.first != stepwiseConfigurationIface)
James Feist5b4aa862018-08-16 14:07:01 -0700327 {
328 continue;
329 }
James Feist73df0db2019-03-25 15:29:35 -0700330
Ed Tanous711ac7a2021-12-20 09:34:41 -0800331 std::string name;
James Feist73df0db2019-03-25 15:29:35 -0700332
Ed Tanous711ac7a2021-12-20 09:34:41 -0800333 for (const std::pair<std::string,
334 dbus::utility::DbusVariantType>&
335 propPair : intfPair.second)
James Feist73df0db2019-03-25 15:29:35 -0700336 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800337 if (propPair.first == "Name")
James Feist73df0db2019-03-25 15:29:35 -0700338 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800339 const std::string* namePtr =
340 std::get_if<std::string>(&propPair.second);
341 if (namePtr == nullptr)
342 {
343 BMCWEB_LOG_ERROR << "Pid Name Field illegal";
344 messages::internalError(asyncResp->res);
345 return;
346 }
Willy Tudb697702022-02-01 03:35:19 -0800347 name = *namePtr;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800348 dbus::utility::escapePathForDbus(name);
James Feist73df0db2019-03-25 15:29:35 -0700349 }
Ed Tanous711ac7a2021-12-20 09:34:41 -0800350 else if (propPair.first == "Profiles")
James Feist73df0db2019-03-25 15:29:35 -0700351 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800352 const std::vector<std::string>* profiles =
353 std::get_if<std::vector<std::string>>(
354 &propPair.second);
355 if (profiles == nullptr)
356 {
357 BMCWEB_LOG_ERROR
358 << "Pid Profiles Field illegal";
359 messages::internalError(asyncResp->res);
360 return;
361 }
362 if (std::find(profiles->begin(), profiles->end(),
363 currentProfile) == profiles->end())
364 {
365 BMCWEB_LOG_INFO
366 << name
367 << " not supported in current profile";
368 continue;
369 }
James Feist73df0db2019-03-25 15:29:35 -0700370 }
371 }
James Feistb7a08d02018-12-11 14:55:37 -0800372 nlohmann::json* config = nullptr;
James Feistc33a90e2019-03-01 10:17:44 -0800373 const std::string* classPtr = nullptr;
Ed Tanous711ac7a2021-12-20 09:34:41 -0800374
375 for (const std::pair<std::string,
376 dbus::utility::DbusVariantType>&
377 propPair : intfPair.second)
James Feistc33a90e2019-03-01 10:17:44 -0800378 {
Lei YU727dc832022-01-20 14:33:52 +0800379 if (propPair.first == "Class")
Ed Tanous711ac7a2021-12-20 09:34:41 -0800380 {
381 classPtr =
382 std::get_if<std::string>(&propPair.second);
383 }
James Feistc33a90e2019-03-01 10:17:44 -0800384 }
385
James Feist5b4aa862018-08-16 14:07:01 -0700386 if (intfPair.first == pidZoneConfigurationIface)
387 {
388 std::string chassis;
389 if (!dbus::utility::getNthStringFromPath(
390 pathPair.first.str, 5, chassis))
391 {
392 chassis = "#IllegalValue";
393 }
394 nlohmann::json& zone = zones[name];
395 zone["Chassis"] = {
396 {"@odata.id", "/redfish/v1/Chassis/" + chassis}};
George Liu0fda0f12021-11-16 10:06:17 +0800397 zone["@odata.id"] =
398 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/" +
399 name;
James Feist5b4aa862018-08-16 14:07:01 -0700400 zone["@odata.type"] = "#OemManager.FanZone";
James Feistb7a08d02018-12-11 14:55:37 -0800401 config = &zone;
James Feist5b4aa862018-08-16 14:07:01 -0700402 }
403
James Feistb7a08d02018-12-11 14:55:37 -0800404 else if (intfPair.first == stepwiseConfigurationIface)
405 {
James Feistc33a90e2019-03-01 10:17:44 -0800406 if (classPtr == nullptr)
407 {
408 BMCWEB_LOG_ERROR << "Pid Class Field illegal";
409 messages::internalError(asyncResp->res);
410 return;
411 }
412
James Feistb7a08d02018-12-11 14:55:37 -0800413 nlohmann::json& controller = stepwise[name];
414 config = &controller;
415
416 controller["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +0800417 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers/" +
Ed Tanous271584a2019-07-09 16:24:22 -0700418 name;
James Feistb7a08d02018-12-11 14:55:37 -0800419 controller["@odata.type"] =
420 "#OemManager.StepwiseController";
421
James Feistc33a90e2019-03-01 10:17:44 -0800422 controller["Direction"] = *classPtr;
James Feistb7a08d02018-12-11 14:55:37 -0800423 }
424
425 // pid and fans are off the same configuration
426 else if (intfPair.first == pidConfigurationIface)
427 {
James Feistc33a90e2019-03-01 10:17:44 -0800428
James Feistb7a08d02018-12-11 14:55:37 -0800429 if (classPtr == nullptr)
430 {
431 BMCWEB_LOG_ERROR << "Pid Class Field illegal";
432 messages::internalError(asyncResp->res);
433 return;
434 }
435 bool isFan = *classPtr == "fan";
436 nlohmann::json& element =
437 isFan ? fans[name] : pids[name];
438 config = &element;
439 if (isFan)
440 {
441 element["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +0800442 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers/" +
Ed Tanous271584a2019-07-09 16:24:22 -0700443 name;
James Feistb7a08d02018-12-11 14:55:37 -0800444 element["@odata.type"] =
445 "#OemManager.FanController";
James Feistb7a08d02018-12-11 14:55:37 -0800446 }
447 else
448 {
449 element["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +0800450 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers/" +
Ed Tanous271584a2019-07-09 16:24:22 -0700451 name;
James Feistb7a08d02018-12-11 14:55:37 -0800452 element["@odata.type"] =
453 "#OemManager.PidController";
James Feistb7a08d02018-12-11 14:55:37 -0800454 }
455 }
456 else
457 {
458 BMCWEB_LOG_ERROR << "Unexpected configuration";
459 messages::internalError(asyncResp->res);
460 return;
461 }
462
463 // used for making maps out of 2 vectors
464 const std::vector<double>* keys = nullptr;
465 const std::vector<double>* values = nullptr;
466
James Feist5b4aa862018-08-16 14:07:01 -0700467 for (const auto& propertyPair : intfPair.second)
468 {
469 if (propertyPair.first == "Type" ||
470 propertyPair.first == "Class" ||
471 propertyPair.first == "Name")
472 {
473 continue;
474 }
475
476 // zones
477 if (intfPair.first == pidZoneConfigurationIface)
478 {
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800479 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800480 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700481 if (ptr == nullptr)
482 {
483 BMCWEB_LOG_ERROR << "Field Illegal "
484 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700485 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700486 return;
487 }
James Feistb7a08d02018-12-11 14:55:37 -0800488 (*config)[propertyPair.first] = *ptr;
489 }
490
491 if (intfPair.first == stepwiseConfigurationIface)
492 {
493 if (propertyPair.first == "Reading" ||
494 propertyPair.first == "Output")
495 {
496 const std::vector<double>* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800497 std::get_if<std::vector<double>>(
James Feistb7a08d02018-12-11 14:55:37 -0800498 &propertyPair.second);
499
500 if (ptr == nullptr)
501 {
502 BMCWEB_LOG_ERROR << "Field Illegal "
503 << propertyPair.first;
504 messages::internalError(asyncResp->res);
505 return;
506 }
507
508 if (propertyPair.first == "Reading")
509 {
510 keys = ptr;
511 }
512 else
513 {
514 values = ptr;
515 }
Ed Tanouse662eae2022-01-25 10:39:19 -0800516 if (keys != nullptr && values != nullptr)
James Feistb7a08d02018-12-11 14:55:37 -0800517 {
518 if (keys->size() != values->size())
519 {
520 BMCWEB_LOG_ERROR
George Liu0fda0f12021-11-16 10:06:17 +0800521 << "Reading and Output size don't match ";
James Feistb7a08d02018-12-11 14:55:37 -0800522 messages::internalError(asyncResp->res);
523 return;
524 }
525 nlohmann::json& steps = (*config)["Steps"];
526 steps = nlohmann::json::array();
527 for (size_t ii = 0; ii < keys->size(); ii++)
528 {
529 steps.push_back(
530 {{"Target", (*keys)[ii]},
531 {"Output", (*values)[ii]}});
532 }
533 }
534 }
535 if (propertyPair.first == "NegativeHysteresis" ||
536 propertyPair.first == "PositiveHysteresis")
537 {
538 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800539 std::get_if<double>(&propertyPair.second);
James Feistb7a08d02018-12-11 14:55:37 -0800540 if (ptr == nullptr)
541 {
542 BMCWEB_LOG_ERROR << "Field Illegal "
543 << propertyPair.first;
544 messages::internalError(asyncResp->res);
545 return;
546 }
547 (*config)[propertyPair.first] = *ptr;
548 }
James Feist5b4aa862018-08-16 14:07:01 -0700549 }
550
551 // pid and fans are off the same configuration
James Feistb7a08d02018-12-11 14:55:37 -0800552 if (intfPair.first == pidConfigurationIface ||
553 intfPair.first == stepwiseConfigurationIface)
James Feist5b4aa862018-08-16 14:07:01 -0700554 {
James Feist5b4aa862018-08-16 14:07:01 -0700555
556 if (propertyPair.first == "Zones")
557 {
558 const std::vector<std::string>* inputs =
Ed Tanousabf2add2019-01-22 16:40:12 -0800559 std::get_if<std::vector<std::string>>(
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800560 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700561
562 if (inputs == nullptr)
563 {
564 BMCWEB_LOG_ERROR
565 << "Zones Pid Field Illegal";
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800566 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700567 return;
568 }
James Feistb7a08d02018-12-11 14:55:37 -0800569 auto& data = (*config)[propertyPair.first];
James Feist5b4aa862018-08-16 14:07:01 -0700570 data = nlohmann::json::array();
571 for (std::string itemCopy : *inputs)
572 {
573 dbus::utility::escapePathForDbus(itemCopy);
574 data.push_back(
575 {{"@odata.id",
George Liu0fda0f12021-11-16 10:06:17 +0800576 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/" +
James Feist5b4aa862018-08-16 14:07:01 -0700577 itemCopy}});
578 }
579 }
580 // todo(james): may never happen, but this
581 // assumes configuration data referenced in the
582 // PID config is provided by the same daemon, we
583 // could add another loop to cover all cases,
584 // but I'm okay kicking this can down the road a
585 // bit
586
587 else if (propertyPair.first == "Inputs" ||
588 propertyPair.first == "Outputs")
589 {
James Feistb7a08d02018-12-11 14:55:37 -0800590 auto& data = (*config)[propertyPair.first];
James Feist5b4aa862018-08-16 14:07:01 -0700591 const std::vector<std::string>* inputs =
Ed Tanousabf2add2019-01-22 16:40:12 -0800592 std::get_if<std::vector<std::string>>(
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800593 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700594
595 if (inputs == nullptr)
596 {
597 BMCWEB_LOG_ERROR << "Field Illegal "
598 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700599 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700600 return;
601 }
602 data = *inputs;
James Feistb943aae2019-07-11 16:33:56 -0700603 }
604 else if (propertyPair.first == "SetPointOffset")
605 {
606 const std::string* ptr =
607 std::get_if<std::string>(
608 &propertyPair.second);
609
610 if (ptr == nullptr)
611 {
612 BMCWEB_LOG_ERROR << "Field Illegal "
613 << propertyPair.first;
614 messages::internalError(asyncResp->res);
615 return;
616 }
617 // translate from dbus to redfish
618 if (*ptr == "WarningHigh")
619 {
620 (*config)["SetPointOffset"] =
621 "UpperThresholdNonCritical";
622 }
623 else if (*ptr == "WarningLow")
624 {
625 (*config)["SetPointOffset"] =
626 "LowerThresholdNonCritical";
627 }
628 else if (*ptr == "CriticalHigh")
629 {
630 (*config)["SetPointOffset"] =
631 "UpperThresholdCritical";
632 }
633 else if (*ptr == "CriticalLow")
634 {
635 (*config)["SetPointOffset"] =
636 "LowerThresholdCritical";
637 }
638 else
639 {
640 BMCWEB_LOG_ERROR << "Value Illegal "
641 << *ptr;
642 messages::internalError(asyncResp->res);
643 return;
644 }
645 }
646 // doubles
James Feist5b4aa862018-08-16 14:07:01 -0700647 else if (propertyPair.first ==
648 "FFGainCoefficient" ||
649 propertyPair.first == "FFOffCoefficient" ||
650 propertyPair.first == "ICoefficient" ||
651 propertyPair.first == "ILimitMax" ||
652 propertyPair.first == "ILimitMin" ||
James Feistaad1a252019-02-19 10:13:52 -0800653 propertyPair.first ==
654 "PositiveHysteresis" ||
655 propertyPair.first ==
656 "NegativeHysteresis" ||
James Feist5b4aa862018-08-16 14:07:01 -0700657 propertyPair.first == "OutLimitMax" ||
658 propertyPair.first == "OutLimitMin" ||
659 propertyPair.first == "PCoefficient" ||
James Feist7625cb82019-01-23 11:58:21 -0800660 propertyPair.first == "SetPoint" ||
James Feist5b4aa862018-08-16 14:07:01 -0700661 propertyPair.first == "SlewNeg" ||
662 propertyPair.first == "SlewPos")
663 {
664 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800665 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700666 if (ptr == nullptr)
667 {
668 BMCWEB_LOG_ERROR << "Field Illegal "
669 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700670 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700671 return;
672 }
James Feistb7a08d02018-12-11 14:55:37 -0800673 (*config)[propertyPair.first] = *ptr;
James Feist5b4aa862018-08-16 14:07:01 -0700674 }
675 }
676 }
677 }
678 }
679 },
680 connection, path, objectManagerIface, "GetManagedObjects");
681}
Jennifer Leeca537922018-08-10 10:07:30 -0700682
James Feist83ff9ab2018-08-31 10:18:24 -0700683enum class CreatePIDRet
684{
685 fail,
686 del,
687 patch
688};
689
zhanghch058d1b46d2021-04-01 11:18:24 +0800690inline bool
691 getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
692 std::vector<nlohmann::json>& config,
693 std::vector<std::string>& zones)
James Feist5f2caae2018-12-12 14:08:25 -0800694{
James Feistb6baeaa2019-02-21 10:41:40 -0800695 if (config.empty())
696 {
697 BMCWEB_LOG_ERROR << "Empty Zones";
Ed Tanous1668ce62022-02-07 23:44:31 -0800698 messages::propertyValueFormatError(response->res, "[]", "Zones");
James Feistb6baeaa2019-02-21 10:41:40 -0800699 return false;
700 }
James Feist5f2caae2018-12-12 14:08:25 -0800701 for (auto& odata : config)
702 {
703 std::string path;
704 if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
705 path))
706 {
707 return false;
708 }
709 std::string input;
James Feist61adbda2019-03-25 13:03:51 -0700710
711 // 8 below comes from
712 // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
713 // 0 1 2 3 4 5 6 7 8
714 if (!dbus::utility::getNthStringFromPath(path, 8, input))
James Feist5f2caae2018-12-12 14:08:25 -0800715 {
716 BMCWEB_LOG_ERROR << "Got invalid path " << path;
717 BMCWEB_LOG_ERROR << "Illegal Type Zones";
718 messages::propertyValueFormatError(response->res, odata.dump(),
719 "Zones");
720 return false;
721 }
722 boost::replace_all(input, "_", " ");
723 zones.emplace_back(std::move(input));
724 }
725 return true;
726}
727
Ed Tanous711ac7a2021-12-20 09:34:41 -0800728inline const dbus::utility::ManagedObjectType::value_type*
James Feist73df0db2019-03-25 15:29:35 -0700729 findChassis(const dbus::utility::ManagedObjectType& managedObj,
730 const std::string& value, std::string& chassis)
James Feistb6baeaa2019-02-21 10:41:40 -0800731{
732 BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
733
734 std::string escaped = boost::replace_all_copy(value, " ", "_");
735 escaped = "/" + escaped;
736 auto it = std::find_if(
737 managedObj.begin(), managedObj.end(), [&escaped](const auto& obj) {
738 if (boost::algorithm::ends_with(obj.first.str, escaped))
739 {
740 BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
741 return true;
742 }
743 return false;
744 });
745
746 if (it == managedObj.end())
747 {
James Feist73df0db2019-03-25 15:29:35 -0700748 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800749 }
750 // 5 comes from <chassis-name> being the 5th element
751 // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
James Feist73df0db2019-03-25 15:29:35 -0700752 if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
753 {
754 return &(*it);
755 }
756
757 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800758}
759
Ed Tanous23a21a12020-07-25 04:45:05 +0000760inline CreatePIDRet createPidInterface(
zhanghch058d1b46d2021-04-01 11:18:24 +0800761 const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
Ed Tanousb5a76932020-09-29 16:16:58 -0700762 const nlohmann::json::iterator& it, const std::string& path,
James Feist83ff9ab2018-08-31 10:18:24 -0700763 const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800764 dbus::utility::DBusPropertiesMap& output, std::string& chassis,
765 const std::string& profile)
James Feist83ff9ab2018-08-31 10:18:24 -0700766{
767
James Feist5f2caae2018-12-12 14:08:25 -0800768 // common deleter
James Feistb6baeaa2019-02-21 10:41:40 -0800769 if (it.value() == nullptr)
James Feist5f2caae2018-12-12 14:08:25 -0800770 {
771 std::string iface;
772 if (type == "PidControllers" || type == "FanControllers")
773 {
774 iface = pidConfigurationIface;
775 }
776 else if (type == "FanZones")
777 {
778 iface = pidZoneConfigurationIface;
779 }
780 else if (type == "StepwiseControllers")
781 {
782 iface = stepwiseConfigurationIface;
783 }
784 else
785 {
Gunnar Millsa0744d32020-11-09 15:40:45 -0600786 BMCWEB_LOG_ERROR << "Illegal Type " << type;
James Feist5f2caae2018-12-12 14:08:25 -0800787 messages::propertyUnknown(response->res, type);
788 return CreatePIDRet::fail;
789 }
James Feist6ee7f772020-02-06 16:25:27 -0800790
791 BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
James Feist5f2caae2018-12-12 14:08:25 -0800792 // delete interface
793 crow::connections::systemBus->async_method_call(
794 [response, path](const boost::system::error_code ec) {
795 if (ec)
796 {
797 BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
798 messages::internalError(response->res);
James Feistb6baeaa2019-02-21 10:41:40 -0800799 return;
James Feist5f2caae2018-12-12 14:08:25 -0800800 }
James Feistb6baeaa2019-02-21 10:41:40 -0800801 messages::success(response->res);
James Feist5f2caae2018-12-12 14:08:25 -0800802 },
803 "xyz.openbmc_project.EntityManager", path, iface, "Delete");
804 return CreatePIDRet::del;
805 }
806
Ed Tanous711ac7a2021-12-20 09:34:41 -0800807 const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800808 if (!createNewObject)
809 {
810 // if we aren't creating a new object, we should be able to find it on
811 // d-bus
James Feist73df0db2019-03-25 15:29:35 -0700812 managedItem = findChassis(managedObj, it.key(), chassis);
813 if (managedItem == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800814 {
815 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
Ed Tanousace85d62021-10-26 12:45:59 -0700816 messages::invalidObject(response->res,
817 crow::utility::urlFromPieces(
818 "redfish", "v1", "Chassis", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800819 return CreatePIDRet::fail;
820 }
821 }
822
Ed Tanous26f69762022-01-25 09:49:11 -0800823 if (!profile.empty() &&
James Feist73df0db2019-03-25 15:29:35 -0700824 (type == "PidControllers" || type == "FanControllers" ||
825 type == "StepwiseControllers"))
826 {
827 if (managedItem == nullptr)
828 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800829 output.emplace_back("Profiles", std::vector<std::string>{profile});
James Feist73df0db2019-03-25 15:29:35 -0700830 }
831 else
832 {
833 std::string interface;
834 if (type == "StepwiseControllers")
835 {
836 interface = stepwiseConfigurationIface;
837 }
838 else
839 {
840 interface = pidConfigurationIface;
841 }
Ed Tanous711ac7a2021-12-20 09:34:41 -0800842 bool ifaceFound = false;
843 for (const auto& iface : managedItem->second)
844 {
845 if (iface.first == interface)
846 {
847 ifaceFound = true;
848 for (const auto& prop : iface.second)
849 {
850 if (prop.first == "Profiles")
851 {
852 const std::vector<std::string>* curProfiles =
853 std::get_if<std::vector<std::string>>(
854 &(prop.second));
855 if (curProfiles == nullptr)
856 {
857 BMCWEB_LOG_ERROR
858 << "Illegal profiles in managed object";
859 messages::internalError(response->res);
860 return CreatePIDRet::fail;
861 }
862 if (std::find(curProfiles->begin(),
863 curProfiles->end(),
864 profile) == curProfiles->end())
865 {
866 std::vector<std::string> newProfiles =
867 *curProfiles;
868 newProfiles.push_back(profile);
Ed Tanousb9d36b42022-02-26 21:42:46 -0800869 output.emplace_back("Profiles", newProfiles);
Ed Tanous711ac7a2021-12-20 09:34:41 -0800870 }
871 }
872 }
873 }
874 }
875
876 if (!ifaceFound)
James Feist73df0db2019-03-25 15:29:35 -0700877 {
878 BMCWEB_LOG_ERROR
879 << "Failed to find interface in managed object";
880 messages::internalError(response->res);
881 return CreatePIDRet::fail;
882 }
James Feist73df0db2019-03-25 15:29:35 -0700883 }
884 }
885
James Feist83ff9ab2018-08-31 10:18:24 -0700886 if (type == "PidControllers" || type == "FanControllers")
887 {
888 if (createNewObject)
889 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800890 output.emplace_back("Class",
891 type == "PidControllers" ? "temp" : "fan");
892 output.emplace_back("Type", "Pid");
James Feist83ff9ab2018-08-31 10:18:24 -0700893 }
James Feist5f2caae2018-12-12 14:08:25 -0800894
895 std::optional<std::vector<nlohmann::json>> zones;
896 std::optional<std::vector<std::string>> inputs;
897 std::optional<std::vector<std::string>> outputs;
898 std::map<std::string, std::optional<double>> doubles;
James Feistb943aae2019-07-11 16:33:56 -0700899 std::optional<std::string> setpointOffset;
James Feist5f2caae2018-12-12 14:08:25 -0800900 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -0800901 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
James Feist5f2caae2018-12-12 14:08:25 -0800902 "Zones", zones, "FFGainCoefficient",
903 doubles["FFGainCoefficient"], "FFOffCoefficient",
904 doubles["FFOffCoefficient"], "ICoefficient",
905 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
906 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
907 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
908 "PCoefficient", doubles["PCoefficient"], "SetPoint",
James Feistb943aae2019-07-11 16:33:56 -0700909 doubles["SetPoint"], "SetPointOffset", setpointOffset,
910 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
911 "PositiveHysteresis", doubles["PositiveHysteresis"],
912 "NegativeHysteresis", doubles["NegativeHysteresis"]))
James Feist83ff9ab2018-08-31 10:18:24 -0700913 {
Ed Tanous71f52d92021-02-19 08:51:17 -0800914 BMCWEB_LOG_ERROR
915 << "Illegal Property "
916 << it.value().dump(2, ' ', true,
917 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -0800918 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700919 }
James Feist5f2caae2018-12-12 14:08:25 -0800920 if (zones)
James Feist83ff9ab2018-08-31 10:18:24 -0700921 {
James Feist5f2caae2018-12-12 14:08:25 -0800922 std::vector<std::string> zonesStr;
923 if (!getZonesFromJsonReq(response, *zones, zonesStr))
James Feist83ff9ab2018-08-31 10:18:24 -0700924 {
Gunnar Millsa0744d32020-11-09 15:40:45 -0600925 BMCWEB_LOG_ERROR << "Illegal Zones";
James Feist5f2caae2018-12-12 14:08:25 -0800926 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700927 }
James Feistb6baeaa2019-02-21 10:41:40 -0800928 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -0800929 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800930 {
931 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
Ed Tanousace85d62021-10-26 12:45:59 -0700932 messages::invalidObject(
933 response->res, crow::utility::urlFromPieces(
934 "redfish", "v1", "Chassis", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -0800935 return CreatePIDRet::fail;
936 }
Ed Tanousb9d36b42022-02-26 21:42:46 -0800937 output.emplace_back("Zones", std::move(zonesStr));
James Feist5f2caae2018-12-12 14:08:25 -0800938 }
939 if (inputs || outputs)
940 {
941 std::array<std::optional<std::vector<std::string>>*, 2> containers =
942 {&inputs, &outputs};
943 size_t index = 0;
944 for (const auto& containerPtr : containers)
James Feist83ff9ab2018-08-31 10:18:24 -0700945 {
James Feist5f2caae2018-12-12 14:08:25 -0800946 std::optional<std::vector<std::string>>& container =
947 *containerPtr;
948 if (!container)
James Feist83ff9ab2018-08-31 10:18:24 -0700949 {
James Feist5f2caae2018-12-12 14:08:25 -0800950 index++;
951 continue;
James Feist83ff9ab2018-08-31 10:18:24 -0700952 }
James Feist5f2caae2018-12-12 14:08:25 -0800953
954 for (std::string& value : *container)
James Feist83ff9ab2018-08-31 10:18:24 -0700955 {
James Feist5f2caae2018-12-12 14:08:25 -0800956 boost::replace_all(value, "_", " ");
James Feist83ff9ab2018-08-31 10:18:24 -0700957 }
James Feist5f2caae2018-12-12 14:08:25 -0800958 std::string key;
959 if (index == 0)
James Feist83ff9ab2018-08-31 10:18:24 -0700960 {
James Feist5f2caae2018-12-12 14:08:25 -0800961 key = "Inputs";
James Feist83ff9ab2018-08-31 10:18:24 -0700962 }
James Feist5f2caae2018-12-12 14:08:25 -0800963 else
964 {
965 key = "Outputs";
966 }
Ed Tanousb9d36b42022-02-26 21:42:46 -0800967 output.emplace_back(key, *container);
James Feist5f2caae2018-12-12 14:08:25 -0800968 index++;
James Feist83ff9ab2018-08-31 10:18:24 -0700969 }
James Feist5f2caae2018-12-12 14:08:25 -0800970 }
James Feist83ff9ab2018-08-31 10:18:24 -0700971
James Feistb943aae2019-07-11 16:33:56 -0700972 if (setpointOffset)
973 {
974 // translate between redfish and dbus names
975 if (*setpointOffset == "UpperThresholdNonCritical")
976 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800977 output.emplace_back("SetPointOffset", "WarningLow");
James Feistb943aae2019-07-11 16:33:56 -0700978 }
979 else if (*setpointOffset == "LowerThresholdNonCritical")
980 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800981 output.emplace_back("SetPointOffset", "WarningHigh");
James Feistb943aae2019-07-11 16:33:56 -0700982 }
983 else if (*setpointOffset == "LowerThresholdCritical")
984 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800985 output.emplace_back("SetPointOffset", "CriticalLow");
James Feistb943aae2019-07-11 16:33:56 -0700986 }
987 else if (*setpointOffset == "UpperThresholdCritical")
988 {
Ed Tanousb9d36b42022-02-26 21:42:46 -0800989 output.emplace_back("SetPointOffset", "CriticalHigh");
James Feistb943aae2019-07-11 16:33:56 -0700990 }
991 else
992 {
993 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
994 << *setpointOffset;
Ed Tanousace85d62021-10-26 12:45:59 -0700995 messages::propertyValueNotInList(response->res, it.key(),
996 "SetPointOffset");
James Feistb943aae2019-07-11 16:33:56 -0700997 return CreatePIDRet::fail;
998 }
999 }
1000
James Feist5f2caae2018-12-12 14:08:25 -08001001 // doubles
1002 for (const auto& pairs : doubles)
1003 {
1004 if (!pairs.second)
James Feist83ff9ab2018-08-31 10:18:24 -07001005 {
James Feist5f2caae2018-12-12 14:08:25 -08001006 continue;
James Feist83ff9ab2018-08-31 10:18:24 -07001007 }
James Feist5f2caae2018-12-12 14:08:25 -08001008 BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
Ed Tanousb9d36b42022-02-26 21:42:46 -08001009 output.emplace_back(pairs.first, *pairs.second);
James Feist83ff9ab2018-08-31 10:18:24 -07001010 }
1011 }
James Feist5f2caae2018-12-12 14:08:25 -08001012
James Feist83ff9ab2018-08-31 10:18:24 -07001013 else if (type == "FanZones")
1014 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001015 output.emplace_back("Type", "Pid.Zone");
James Feist83ff9ab2018-08-31 10:18:24 -07001016
James Feist5f2caae2018-12-12 14:08:25 -08001017 std::optional<nlohmann::json> chassisContainer;
1018 std::optional<double> failSafePercent;
James Feistd3ec07f2019-02-25 14:51:15 -08001019 std::optional<double> minThermalOutput;
James Feistb6baeaa2019-02-21 10:41:40 -08001020 if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
James Feist5f2caae2018-12-12 14:08:25 -08001021 chassisContainer, "FailSafePercent",
James Feistd3ec07f2019-02-25 14:51:15 -08001022 failSafePercent, "MinThermalOutput",
1023 minThermalOutput))
James Feist83ff9ab2018-08-31 10:18:24 -07001024 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001025 BMCWEB_LOG_ERROR
1026 << "Illegal Property "
1027 << it.value().dump(2, ' ', true,
1028 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -08001029 return CreatePIDRet::fail;
1030 }
James Feist83ff9ab2018-08-31 10:18:24 -07001031
James Feist5f2caae2018-12-12 14:08:25 -08001032 if (chassisContainer)
1033 {
1034
1035 std::string chassisId;
1036 if (!redfish::json_util::readJson(*chassisContainer, response->res,
1037 "@odata.id", chassisId))
James Feist83ff9ab2018-08-31 10:18:24 -07001038 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001039 BMCWEB_LOG_ERROR
1040 << "Illegal Property "
1041 << chassisContainer->dump(
1042 2, ' ', true,
1043 nlohmann::json::error_handler_t::replace);
James Feist83ff9ab2018-08-31 10:18:24 -07001044 return CreatePIDRet::fail;
1045 }
James Feist5f2caae2018-12-12 14:08:25 -08001046
AppaRao Puli717794d2019-10-18 22:54:53 +05301047 // /redfish/v1/chassis/chassis_name/
James Feist5f2caae2018-12-12 14:08:25 -08001048 if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
1049 {
1050 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
Ed Tanousace85d62021-10-26 12:45:59 -07001051 messages::invalidObject(
1052 response->res, crow::utility::urlFromPieces(
1053 "redfish", "v1", "Chassis", chassisId));
James Feist5f2caae2018-12-12 14:08:25 -08001054 return CreatePIDRet::fail;
1055 }
1056 }
James Feistd3ec07f2019-02-25 14:51:15 -08001057 if (minThermalOutput)
James Feist5f2caae2018-12-12 14:08:25 -08001058 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001059 output.emplace_back("MinThermalOutput", *minThermalOutput);
James Feist5f2caae2018-12-12 14:08:25 -08001060 }
1061 if (failSafePercent)
1062 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001063 output.emplace_back("FailSafePercent", *failSafePercent);
James Feist5f2caae2018-12-12 14:08:25 -08001064 }
1065 }
1066 else if (type == "StepwiseControllers")
1067 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001068 output.emplace_back("Type", "Stepwise");
James Feist5f2caae2018-12-12 14:08:25 -08001069
1070 std::optional<std::vector<nlohmann::json>> zones;
1071 std::optional<std::vector<nlohmann::json>> steps;
1072 std::optional<std::vector<std::string>> inputs;
1073 std::optional<double> positiveHysteresis;
1074 std::optional<double> negativeHysteresis;
James Feistc33a90e2019-03-01 10:17:44 -08001075 std::optional<std::string> direction; // upper clipping curve vs lower
James Feist5f2caae2018-12-12 14:08:25 -08001076 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -08001077 it.value(), response->res, "Zones", zones, "Steps", steps,
1078 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
James Feistc33a90e2019-03-01 10:17:44 -08001079 "NegativeHysteresis", negativeHysteresis, "Direction",
1080 direction))
James Feist5f2caae2018-12-12 14:08:25 -08001081 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001082 BMCWEB_LOG_ERROR
1083 << "Illegal Property "
1084 << it.value().dump(2, ' ', true,
1085 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -08001086 return CreatePIDRet::fail;
1087 }
1088
1089 if (zones)
1090 {
James Feistb6baeaa2019-02-21 10:41:40 -08001091 std::vector<std::string> zonesStrs;
1092 if (!getZonesFromJsonReq(response, *zones, zonesStrs))
James Feist5f2caae2018-12-12 14:08:25 -08001093 {
Gunnar Millsa0744d32020-11-09 15:40:45 -06001094 BMCWEB_LOG_ERROR << "Illegal Zones";
James Feist5f2caae2018-12-12 14:08:25 -08001095 return CreatePIDRet::fail;
1096 }
James Feistb6baeaa2019-02-21 10:41:40 -08001097 if (chassis.empty() &&
Ed Tanouse662eae2022-01-25 10:39:19 -08001098 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -08001099 {
1100 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
Ed Tanousace85d62021-10-26 12:45:59 -07001101 messages::invalidObject(
1102 response->res, crow::utility::urlFromPieces(
1103 "redfish", "v1", "Chassis", chassis));
James Feistb6baeaa2019-02-21 10:41:40 -08001104 return CreatePIDRet::fail;
1105 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001106 output.emplace_back("Zones", std::move(zonesStrs));
James Feist5f2caae2018-12-12 14:08:25 -08001107 }
1108 if (steps)
1109 {
1110 std::vector<double> readings;
1111 std::vector<double> outputs;
1112 for (auto& step : *steps)
1113 {
Ed Tanous543f4402022-01-06 13:12:53 -08001114 double target = 0.0;
1115 double out = 0.0;
James Feist5f2caae2018-12-12 14:08:25 -08001116
1117 if (!redfish::json_util::readJson(step, response->res, "Target",
Ed Tanous23a21a12020-07-25 04:45:05 +00001118 target, "Output", out))
James Feist5f2caae2018-12-12 14:08:25 -08001119 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001120 BMCWEB_LOG_ERROR
1121 << "Illegal Property "
1122 << it.value().dump(
1123 2, ' ', true,
1124 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -08001125 return CreatePIDRet::fail;
1126 }
1127 readings.emplace_back(target);
Ed Tanous23a21a12020-07-25 04:45:05 +00001128 outputs.emplace_back(out);
James Feist5f2caae2018-12-12 14:08:25 -08001129 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001130 output.emplace_back("Reading", std::move(readings));
1131 output.emplace_back("Output", std::move(outputs));
James Feist5f2caae2018-12-12 14:08:25 -08001132 }
1133 if (inputs)
1134 {
1135 for (std::string& value : *inputs)
1136 {
James Feist5f2caae2018-12-12 14:08:25 -08001137 boost::replace_all(value, "_", " ");
1138 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001139 output.emplace_back("Inputs", std::move(*inputs));
James Feist5f2caae2018-12-12 14:08:25 -08001140 }
1141 if (negativeHysteresis)
1142 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001143 output.emplace_back("NegativeHysteresis", *negativeHysteresis);
James Feist5f2caae2018-12-12 14:08:25 -08001144 }
1145 if (positiveHysteresis)
1146 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001147 output.emplace_back("PositiveHysteresis", *positiveHysteresis);
James Feist83ff9ab2018-08-31 10:18:24 -07001148 }
James Feistc33a90e2019-03-01 10:17:44 -08001149 if (direction)
1150 {
1151 constexpr const std::array<const char*, 2> allowedDirections = {
1152 "Ceiling", "Floor"};
1153 if (std::find(allowedDirections.begin(), allowedDirections.end(),
1154 *direction) == allowedDirections.end())
1155 {
1156 messages::propertyValueTypeError(response->res, "Direction",
1157 *direction);
1158 return CreatePIDRet::fail;
1159 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001160 output.emplace_back("Class", *direction);
James Feistc33a90e2019-03-01 10:17:44 -08001161 }
James Feist83ff9ab2018-08-31 10:18:24 -07001162 }
1163 else
1164 {
Gunnar Millsa0744d32020-11-09 15:40:45 -06001165 BMCWEB_LOG_ERROR << "Illegal Type " << type;
Jason M. Bills35a62c72018-10-09 12:45:45 -07001166 messages::propertyUnknown(response->res, type);
James Feist83ff9ab2018-08-31 10:18:24 -07001167 return CreatePIDRet::fail;
1168 }
1169 return CreatePIDRet::patch;
1170}
James Feist73df0db2019-03-25 15:29:35 -07001171struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
1172{
1173
zhanghch058d1b46d2021-04-01 11:18:24 +08001174 GetPIDValues(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) {
James Feist73df0db2019-03-25 15:29:35 -07001188 if (ec)
1189 {
1190 BMCWEB_LOG_ERROR << ec;
1191 messages::internalError(self->asyncResp->res);
1192 return;
1193 }
Ed Tanous23a21a12020-07-25 04:45:05 +00001194 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 Tanous23a21a12020-07-25 04:45:05 +00001208 if (ec || subtreeLocal.empty())
James Feist73df0db2019-03-25 15:29:35 -07001209 {
1210 return;
1211 }
Ed Tanous23a21a12020-07-25 04:45:05 +00001212 if (subtreeLocal[0].second.size() != 1)
James Feist73df0db2019-03-25 15:29:35 -07001213 {
1214 // invalid mapper response, should never happen
1215 BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
1216 messages::internalError(self->asyncResp->res);
1217 return;
1218 }
1219
Ed Tanous23a21a12020-07-25 04:45:05 +00001220 const std::string& path = subtreeLocal[0].first;
1221 const std::string& owner = subtreeLocal[0].second[0].first;
James Feist73df0db2019-03-25 15:29:35 -07001222 crow::connections::systemBus->async_method_call(
Ed Tanous168e20c2021-12-13 14:39:53 -08001223 [path, owner,
1224 self](const boost::system::error_code ec2,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001225 const dbus::utility::DBusPropertiesMap& resp) {
Ed Tanous23a21a12020-07-25 04:45:05 +00001226 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001227 {
George Liu0fda0f12021-11-16 10:06:17 +08001228 BMCWEB_LOG_ERROR
1229 << "GetPIDValues: Can't get thermalModeIface "
1230 << path;
James Feist73df0db2019-03-25 15:29:35 -07001231 messages::internalError(self->asyncResp->res);
1232 return;
1233 }
Ed Tanous271584a2019-07-09 16:24:22 -07001234 const std::string* current = nullptr;
1235 const std::vector<std::string>* supported = nullptr;
Ed Tanous9eb808c2022-01-25 10:19:23 -08001236 for (const auto& [key, value] : resp)
James Feist73df0db2019-03-25 15:29:35 -07001237 {
1238 if (key == "Current")
1239 {
1240 current = std::get_if<std::string>(&value);
1241 if (current == nullptr)
1242 {
1243 BMCWEB_LOG_ERROR
George Liu0fda0f12021-11-16 10:06:17 +08001244 << "GetPIDValues: thermal mode iface invalid "
James Feist73df0db2019-03-25 15:29:35 -07001245 << path;
1246 messages::internalError(
1247 self->asyncResp->res);
1248 return;
1249 }
1250 }
1251 if (key == "Supported")
1252 {
1253 supported =
1254 std::get_if<std::vector<std::string>>(
1255 &value);
1256 if (supported == nullptr)
1257 {
1258 BMCWEB_LOG_ERROR
George Liu0fda0f12021-11-16 10:06:17 +08001259 << "GetPIDValues: thermal mode iface invalid"
James Feist73df0db2019-03-25 15:29:35 -07001260 << path;
1261 messages::internalError(
1262 self->asyncResp->res);
1263 return;
1264 }
1265 }
1266 }
1267 if (current == nullptr || supported == nullptr)
1268 {
George Liu0fda0f12021-11-16 10:06:17 +08001269 BMCWEB_LOG_ERROR
1270 << "GetPIDValues: thermal mode iface invalid "
1271 << path;
James Feist73df0db2019-03-25 15:29:35 -07001272 messages::internalError(self->asyncResp->res);
1273 return;
1274 }
1275 self->currentProfile = *current;
1276 self->supportedProfiles = *supported;
1277 },
1278 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
1279 thermalModeIface);
1280 },
1281 "xyz.openbmc_project.ObjectMapper",
1282 "/xyz/openbmc_project/object_mapper",
1283 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
1284 std::array<const char*, 1>{thermalModeIface});
1285 }
1286
1287 ~GetPIDValues()
1288 {
1289 if (asyncResp->res.result() != boost::beast::http::status::ok)
1290 {
1291 return;
1292 }
1293 // create map of <connection, path to objMgr>>
1294 boost::container::flat_map<std::string, std::string> objectMgrPaths;
1295 boost::container::flat_set<std::string> calledConnections;
1296 for (const auto& pathGroup : subtree)
1297 {
1298 for (const auto& connectionGroup : pathGroup.second)
1299 {
1300 auto findConnection =
1301 calledConnections.find(connectionGroup.first);
1302 if (findConnection != calledConnections.end())
1303 {
1304 break;
1305 }
1306 for (const std::string& interface : connectionGroup.second)
1307 {
1308 if (interface == objectManagerIface)
1309 {
1310 objectMgrPaths[connectionGroup.first] = pathGroup.first;
1311 }
1312 // this list is alphabetical, so we
1313 // should have found the objMgr by now
1314 if (interface == pidConfigurationIface ||
1315 interface == pidZoneConfigurationIface ||
1316 interface == stepwiseConfigurationIface)
1317 {
1318 auto findObjMgr =
1319 objectMgrPaths.find(connectionGroup.first);
1320 if (findObjMgr == objectMgrPaths.end())
1321 {
1322 BMCWEB_LOG_DEBUG << connectionGroup.first
1323 << "Has no Object Manager";
1324 continue;
1325 }
1326
1327 calledConnections.insert(connectionGroup.first);
1328
1329 asyncPopulatePid(findObjMgr->first, findObjMgr->second,
1330 currentProfile, supportedProfiles,
1331 asyncResp);
1332 break;
1333 }
1334 }
1335 }
1336 }
1337 }
1338
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001339 GetPIDValues(const GetPIDValues&) = delete;
1340 GetPIDValues(GetPIDValues&&) = delete;
1341 GetPIDValues& operator=(const GetPIDValues&) = delete;
1342 GetPIDValues& operator=(GetPIDValues&&) = delete;
1343
James Feist73df0db2019-03-25 15:29:35 -07001344 std::vector<std::string> supportedProfiles;
1345 std::string currentProfile;
Ed Tanousb9d36b42022-02-26 21:42:46 -08001346 dbus::utility::MapperGetSubTreeResponse subtree;
zhanghch058d1b46d2021-04-01 11:18:24 +08001347 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001348};
1349
1350struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
1351{
1352
zhanghch058d1b46d2021-04-01 11:18:24 +08001353 SetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
James Feist73df0db2019-03-25 15:29:35 -07001354 nlohmann::json& data) :
Ed Tanous271584a2019-07-09 16:24:22 -07001355 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001356 {
1357
1358 std::optional<nlohmann::json> pidControllers;
1359 std::optional<nlohmann::json> fanControllers;
1360 std::optional<nlohmann::json> fanZones;
1361 std::optional<nlohmann::json> stepwiseControllers;
1362
1363 if (!redfish::json_util::readJson(
1364 data, asyncResp->res, "PidControllers", pidControllers,
1365 "FanControllers", fanControllers, "FanZones", fanZones,
1366 "StepwiseControllers", stepwiseControllers, "Profile", profile))
1367 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001368 BMCWEB_LOG_ERROR
1369 << "Illegal Property "
1370 << data.dump(2, ' ', true,
1371 nlohmann::json::error_handler_t::replace);
James Feist73df0db2019-03-25 15:29:35 -07001372 return;
1373 }
1374 configuration.emplace_back("PidControllers", std::move(pidControllers));
1375 configuration.emplace_back("FanControllers", std::move(fanControllers));
1376 configuration.emplace_back("FanZones", std::move(fanZones));
1377 configuration.emplace_back("StepwiseControllers",
1378 std::move(stepwiseControllers));
1379 }
Ed Tanousecd6a3a2022-01-07 09:18:40 -08001380
1381 SetPIDValues(const SetPIDValues&) = delete;
1382 SetPIDValues(SetPIDValues&&) = delete;
1383 SetPIDValues& operator=(const SetPIDValues&) = delete;
1384 SetPIDValues& operator=(SetPIDValues&&) = delete;
1385
James Feist73df0db2019-03-25 15:29:35 -07001386 void run()
1387 {
1388 if (asyncResp->res.result() != boost::beast::http::status::ok)
1389 {
1390 return;
1391 }
1392
1393 std::shared_ptr<SetPIDValues> self = shared_from_this();
1394
1395 // todo(james): might make sense to do a mapper call here if this
1396 // interface gets more traction
1397 crow::connections::systemBus->async_method_call(
1398 [self](const boost::system::error_code ec,
Ed Tanous914e2d52022-01-07 11:38:34 -08001399 const dbus::utility::ManagedObjectType& mObj) {
James Feist73df0db2019-03-25 15:29:35 -07001400 if (ec)
1401 {
1402 BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
1403 messages::internalError(self->asyncResp->res);
1404 return;
1405 }
James Feiste69d9de2020-02-07 12:23:27 -08001406 const std::array<const char*, 3> configurations = {
1407 pidConfigurationIface, pidZoneConfigurationIface,
1408 stepwiseConfigurationIface};
1409
James Feist14b0b8d2020-02-12 11:52:07 -08001410 for (const auto& [path, object] : mObj)
James Feiste69d9de2020-02-07 12:23:27 -08001411 {
James Feist14b0b8d2020-02-12 11:52:07 -08001412 for (const auto& [interface, _] : object)
James Feiste69d9de2020-02-07 12:23:27 -08001413 {
1414 if (std::find(configurations.begin(),
1415 configurations.end(),
1416 interface) != configurations.end())
1417 {
James Feist14b0b8d2020-02-12 11:52:07 -08001418 self->objectCount++;
James Feiste69d9de2020-02-07 12:23:27 -08001419 break;
1420 }
1421 }
James Feiste69d9de2020-02-07 12:23:27 -08001422 }
Ed Tanous914e2d52022-01-07 11:38:34 -08001423 self->managedObj = mObj;
James Feist73df0db2019-03-25 15:29:35 -07001424 },
1425 "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
1426 "GetManagedObjects");
1427
1428 // at the same time get the profile information
1429 crow::connections::systemBus->async_method_call(
1430 [self](const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001431 const dbus::utility::MapperGetSubTreeResponse& subtree) {
James Feist73df0db2019-03-25 15:29:35 -07001432 if (ec || subtree.empty())
1433 {
1434 return;
1435 }
1436 if (subtree[0].second.empty())
1437 {
1438 // invalid mapper response, should never happen
1439 BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
1440 messages::internalError(self->asyncResp->res);
1441 return;
1442 }
1443
1444 const std::string& path = subtree[0].first;
1445 const std::string& owner = subtree[0].second[0].first;
1446 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -08001447 [self, path,
1448 owner](const boost::system::error_code ec2,
1449 const dbus::utility::DBusPropertiesMap& r) {
Ed Tanouscb13a392020-07-25 19:02:03 +00001450 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001451 {
George Liu0fda0f12021-11-16 10:06:17 +08001452 BMCWEB_LOG_ERROR
1453 << "SetPIDValues: Can't get thermalModeIface "
1454 << path;
James Feist73df0db2019-03-25 15:29:35 -07001455 messages::internalError(self->asyncResp->res);
1456 return;
1457 }
Ed Tanous271584a2019-07-09 16:24:22 -07001458 const std::string* current = nullptr;
1459 const std::vector<std::string>* supported = nullptr;
Ed Tanous9eb808c2022-01-25 10:19:23 -08001460 for (const auto& [key, value] : r)
James Feist73df0db2019-03-25 15:29:35 -07001461 {
1462 if (key == "Current")
1463 {
1464 current = std::get_if<std::string>(&value);
1465 if (current == nullptr)
1466 {
1467 BMCWEB_LOG_ERROR
George Liu0fda0f12021-11-16 10:06:17 +08001468 << "SetPIDValues: thermal mode iface invalid "
James Feist73df0db2019-03-25 15:29:35 -07001469 << path;
1470 messages::internalError(
1471 self->asyncResp->res);
1472 return;
1473 }
1474 }
1475 if (key == "Supported")
1476 {
1477 supported =
1478 std::get_if<std::vector<std::string>>(
1479 &value);
1480 if (supported == nullptr)
1481 {
1482 BMCWEB_LOG_ERROR
George Liu0fda0f12021-11-16 10:06:17 +08001483 << "SetPIDValues: thermal mode iface invalid"
James Feist73df0db2019-03-25 15:29:35 -07001484 << path;
1485 messages::internalError(
1486 self->asyncResp->res);
1487 return;
1488 }
1489 }
1490 }
1491 if (current == nullptr || supported == nullptr)
1492 {
George Liu0fda0f12021-11-16 10:06:17 +08001493 BMCWEB_LOG_ERROR
1494 << "SetPIDValues: thermal mode iface invalid "
1495 << path;
James Feist73df0db2019-03-25 15:29:35 -07001496 messages::internalError(self->asyncResp->res);
1497 return;
1498 }
1499 self->currentProfile = *current;
1500 self->supportedProfiles = *supported;
1501 self->profileConnection = owner;
1502 self->profilePath = path;
1503 },
1504 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
1505 thermalModeIface);
1506 },
1507 "xyz.openbmc_project.ObjectMapper",
1508 "/xyz/openbmc_project/object_mapper",
1509 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
1510 std::array<const char*, 1>{thermalModeIface});
1511 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001512 void pidSetDone()
James Feist73df0db2019-03-25 15:29:35 -07001513 {
1514 if (asyncResp->res.result() != boost::beast::http::status::ok)
1515 {
1516 return;
1517 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001518 std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001519 if (profile)
1520 {
1521 if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
1522 *profile) == supportedProfiles.end())
1523 {
1524 messages::actionParameterUnknown(response->res, "Profile",
1525 *profile);
1526 return;
1527 }
1528 currentProfile = *profile;
1529 crow::connections::systemBus->async_method_call(
1530 [response](const boost::system::error_code ec) {
1531 if (ec)
1532 {
1533 BMCWEB_LOG_ERROR << "Error patching profile" << ec;
1534 messages::internalError(response->res);
1535 }
1536 },
1537 profileConnection, profilePath,
1538 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
Ed Tanous168e20c2021-12-13 14:39:53 -08001539 "Current", dbus::utility::DbusVariantType(*profile));
James Feist73df0db2019-03-25 15:29:35 -07001540 }
1541
1542 for (auto& containerPair : configuration)
1543 {
1544 auto& container = containerPair.second;
1545 if (!container)
1546 {
1547 continue;
1548 }
James Feist6ee7f772020-02-06 16:25:27 -08001549 BMCWEB_LOG_DEBUG << *container;
1550
James Feist73df0db2019-03-25 15:29:35 -07001551 std::string& type = containerPair.first;
1552
1553 for (nlohmann::json::iterator it = container->begin();
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301554 it != container->end(); ++it)
James Feist73df0db2019-03-25 15:29:35 -07001555 {
1556 const auto& name = it.key();
James Feist6ee7f772020-02-06 16:25:27 -08001557 BMCWEB_LOG_DEBUG << "looking for " << name;
1558
James Feist73df0db2019-03-25 15:29:35 -07001559 auto pathItr =
1560 std::find_if(managedObj.begin(), managedObj.end(),
1561 [&name](const auto& obj) {
1562 return boost::algorithm::ends_with(
1563 obj.first.str, "/" + name);
1564 });
Ed Tanousb9d36b42022-02-26 21:42:46 -08001565 dbus::utility::DBusPropertiesMap output;
James Feist73df0db2019-03-25 15:29:35 -07001566
1567 output.reserve(16); // The pid interface length
1568
1569 // determines if we're patching entity-manager or
1570 // creating a new object
1571 bool createNewObject = (pathItr == managedObj.end());
James Feist6ee7f772020-02-06 16:25:27 -08001572 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
1573
James Feist73df0db2019-03-25 15:29:35 -07001574 std::string iface;
Ed Tanous711ac7a2021-12-20 09:34:41 -08001575 /*
James Feist73df0db2019-03-25 15:29:35 -07001576 if (type == "PidControllers" || type == "FanControllers")
1577 {
1578 iface = pidConfigurationIface;
1579 if (!createNewObject &&
1580 pathItr->second.find(pidConfigurationIface) ==
1581 pathItr->second.end())
1582 {
1583 createNewObject = true;
1584 }
1585 }
1586 else if (type == "FanZones")
1587 {
1588 iface = pidZoneConfigurationIface;
1589 if (!createNewObject &&
1590 pathItr->second.find(pidZoneConfigurationIface) ==
1591 pathItr->second.end())
1592 {
1593
1594 createNewObject = true;
1595 }
1596 }
1597 else if (type == "StepwiseControllers")
1598 {
1599 iface = stepwiseConfigurationIface;
1600 if (!createNewObject &&
1601 pathItr->second.find(stepwiseConfigurationIface) ==
1602 pathItr->second.end())
1603 {
1604 createNewObject = true;
1605 }
Ed Tanous711ac7a2021-12-20 09:34:41 -08001606 }*/
James Feist6ee7f772020-02-06 16:25:27 -08001607
1608 if (createNewObject && it.value() == nullptr)
1609 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -05001610 // can't delete a non-existent object
Ed Tanous1668ce62022-02-07 23:44:31 -08001611 messages::propertyValueNotInList(response->res,
1612 it.value().dump(), name);
James Feist6ee7f772020-02-06 16:25:27 -08001613 continue;
1614 }
1615
1616 std::string path;
1617 if (pathItr != managedObj.end())
1618 {
1619 path = pathItr->first.str;
1620 }
1621
James Feist73df0db2019-03-25 15:29:35 -07001622 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
James Feiste69d9de2020-02-07 12:23:27 -08001623
1624 // arbitrary limit to avoid attacks
1625 constexpr const size_t controllerLimit = 500;
James Feist14b0b8d2020-02-12 11:52:07 -08001626 if (createNewObject && objectCount >= controllerLimit)
James Feiste69d9de2020-02-07 12:23:27 -08001627 {
1628 messages::resourceExhaustion(response->res, type);
1629 continue;
1630 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001631 output.emplace_back("Name",
1632 boost::replace_all_copy(name, "_", " "));
James Feist73df0db2019-03-25 15:29:35 -07001633
1634 std::string chassis;
1635 CreatePIDRet ret = createPidInterface(
James Feist6ee7f772020-02-06 16:25:27 -08001636 response, type, it, path, managedObj, createNewObject,
1637 output, chassis, currentProfile);
James Feist73df0db2019-03-25 15:29:35 -07001638 if (ret == CreatePIDRet::fail)
1639 {
1640 return;
1641 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001642 if (ret == CreatePIDRet::del)
James Feist73df0db2019-03-25 15:29:35 -07001643 {
1644 continue;
1645 }
1646
1647 if (!createNewObject)
1648 {
1649 for (const auto& property : output)
1650 {
1651 crow::connections::systemBus->async_method_call(
1652 [response,
1653 propertyName{std::string(property.first)}](
1654 const boost::system::error_code ec) {
1655 if (ec)
1656 {
1657 BMCWEB_LOG_ERROR << "Error patching "
1658 << propertyName << ": "
1659 << ec;
1660 messages::internalError(response->res);
1661 return;
1662 }
1663 messages::success(response->res);
1664 },
James Feist6ee7f772020-02-06 16:25:27 -08001665 "xyz.openbmc_project.EntityManager", path,
James Feist73df0db2019-03-25 15:29:35 -07001666 "org.freedesktop.DBus.Properties", "Set", iface,
1667 property.first, property.second);
1668 }
1669 }
1670 else
1671 {
1672 if (chassis.empty())
1673 {
1674 BMCWEB_LOG_ERROR << "Failed to get chassis from config";
Ed Tanousace85d62021-10-26 12:45:59 -07001675 messages::internalError(response->res);
James Feist73df0db2019-03-25 15:29:35 -07001676 return;
1677 }
1678
1679 bool foundChassis = false;
1680 for (const auto& obj : managedObj)
1681 {
1682 if (boost::algorithm::ends_with(obj.first.str, chassis))
1683 {
1684 chassis = obj.first.str;
1685 foundChassis = true;
1686 break;
1687 }
1688 }
1689 if (!foundChassis)
1690 {
1691 BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
1692 messages::resourceMissingAtURI(
Ed Tanousace85d62021-10-26 12:45:59 -07001693 response->res,
1694 crow::utility::urlFromPieces("redfish", "v1",
1695 "Chassis", chassis));
James Feist73df0db2019-03-25 15:29:35 -07001696 return;
1697 }
1698
1699 crow::connections::systemBus->async_method_call(
1700 [response](const boost::system::error_code ec) {
1701 if (ec)
1702 {
1703 BMCWEB_LOG_ERROR << "Error Adding Pid Object "
1704 << ec;
1705 messages::internalError(response->res);
1706 return;
1707 }
1708 messages::success(response->res);
1709 },
1710 "xyz.openbmc_project.EntityManager", chassis,
1711 "xyz.openbmc_project.AddObject", "AddObject", output);
1712 }
1713 }
1714 }
1715 }
Ed Tanous24b2fe82022-01-06 12:45:54 -08001716
1717 ~SetPIDValues()
1718 {
1719 try
1720 {
1721 pidSetDone();
1722 }
1723 catch (...)
1724 {
1725 BMCWEB_LOG_CRITICAL << "pidSetDone threw exception";
1726 }
1727 }
1728
zhanghch058d1b46d2021-04-01 11:18:24 +08001729 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001730 std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
1731 configuration;
1732 std::optional<std::string> profile;
1733 dbus::utility::ManagedObjectType managedObj;
1734 std::vector<std::string> supportedProfiles;
1735 std::string currentProfile;
1736 std::string profileConnection;
1737 std::string profilePath;
James Feist14b0b8d2020-02-12 11:52:07 -08001738 size_t objectCount = 0;
James Feist73df0db2019-03-25 15:29:35 -07001739};
James Feist83ff9ab2018-08-31 10:18:24 -07001740
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001741/**
1742 * @brief Retrieves BMC manager location data over DBus
1743 *
1744 * @param[in] aResp Shared pointer for completing asynchronous calls
1745 * @param[in] connectionName - service name
1746 * @param[in] path - object path
1747 * @return none
1748 */
zhanghch058d1b46d2021-04-01 11:18:24 +08001749inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001750 const std::string& connectionName,
1751 const std::string& path)
1752{
1753 BMCWEB_LOG_DEBUG << "Get BMC manager Location data.";
1754
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001755 sdbusplus::asio::getProperty<std::string>(
1756 *crow::connections::systemBus, connectionName, path,
1757 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001758 [aResp](const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001759 const std::string& property) {
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001760 if (ec)
1761 {
1762 BMCWEB_LOG_DEBUG << "DBUS response error for "
1763 "Location";
1764 messages::internalError(aResp->res);
1765 return;
1766 }
1767
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001768 aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001769 property;
1770 });
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001771}
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001772// avoid name collision systems.hpp
1773inline void
1774 managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001775{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001776 BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time";
Ed Tanous52cc1122020-07-18 13:51:21 -07001777
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001778 sdbusplus::asio::getProperty<uint64_t>(
1779 *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
1780 "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
1781 "LastRebootTime",
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001782 [aResp](const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001783 const uint64_t lastResetTime) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001784 if (ec)
James Feist83ff9ab2018-08-31 10:18:24 -07001785 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001786 BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
Ed Tanous43b761d2019-02-13 20:10:56 -08001787 return;
1788 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001789
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001790 // LastRebootTime is epoch time, in milliseconds
1791 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001792 uint64_t lastResetTimeStamp = lastResetTime / 1000;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001793
1794 // Convert to ISO 8601 standard
1795 aResp->res.jsonValue["LastResetTime"] =
Nan Zhou1d8782e2021-11-29 22:23:18 -08001796 crow::utility::getDateTimeUint(lastResetTimeStamp);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001797 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001798}
1799
1800/**
1801 * @brief Set the running firmware image
1802 *
1803 * @param[i,o] aResp - Async response object
1804 * @param[i] runningFirmwareTarget - Image to make the running image
1805 *
1806 * @return void
1807 */
1808inline void
1809 setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1810 const std::string& runningFirmwareTarget)
1811{
1812 // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1813 std::string::size_type idPos = runningFirmwareTarget.rfind('/');
1814 if (idPos == std::string::npos)
1815 {
1816 messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
1817 "@odata.id");
1818 BMCWEB_LOG_DEBUG << "Can't parse firmware ID!";
1819 return;
1820 }
1821 idPos++;
1822 if (idPos >= runningFirmwareTarget.size())
1823 {
1824 messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
1825 "@odata.id");
1826 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
1827 return;
1828 }
1829 std::string firmwareId = runningFirmwareTarget.substr(idPos);
1830
1831 // Make sure the image is valid before setting priority
1832 crow::connections::systemBus->async_method_call(
Ed Tanous711ac7a2021-12-20 09:34:41 -08001833 [aResp, firmwareId,
1834 runningFirmwareTarget](const boost::system::error_code ec,
1835 dbus::utility::ManagedObjectType& subtree) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001836 if (ec)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001837 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001838 BMCWEB_LOG_DEBUG << "D-Bus response error getting objects.";
1839 messages::internalError(aResp->res);
1840 return;
1841 }
1842
Ed Tanous26f69762022-01-25 09:49:11 -08001843 if (subtree.empty())
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001844 {
1845 BMCWEB_LOG_DEBUG << "Can't find image!";
1846 messages::internalError(aResp->res);
1847 return;
1848 }
1849
1850 bool foundImage = false;
1851 for (auto& object : subtree)
1852 {
1853 const std::string& path =
1854 static_cast<const std::string&>(object.first);
1855 std::size_t idPos2 = path.rfind('/');
1856
1857 if (idPos2 == std::string::npos)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001858 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001859 continue;
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001860 }
1861
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001862 idPos2++;
1863 if (idPos2 >= path.size())
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001864 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001865 continue;
1866 }
1867
1868 if (path.substr(idPos2) == firmwareId)
1869 {
1870 foundImage = true;
1871 break;
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001872 }
1873 }
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301874
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001875 if (!foundImage)
1876 {
1877 messages::propertyValueNotInList(
1878 aResp->res, runningFirmwareTarget, "@odata.id");
1879 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
1880 return;
1881 }
Gunnar Mills4bf2b032020-06-23 22:28:31 -05001882
Ed Tanous8cc8ede2022-02-28 10:20:59 -08001883 BMCWEB_LOG_DEBUG << "Setting firmware version " << firmwareId
1884 << " to priority 0.";
Gunnar Mills4bf2b032020-06-23 22:28:31 -05001885
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001886 // Only support Immediate
1887 // An addition could be a Redfish Setting like
1888 // ActiveSoftwareImageApplyTime and support OnReset
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301889 crow::connections::systemBus->async_method_call(
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001890 [aResp](const boost::system::error_code ec) {
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301891 if (ec)
1892 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001893 BMCWEB_LOG_DEBUG << "D-Bus response error setting.";
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301894 messages::internalError(aResp->res);
1895 return;
1896 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001897 doBMCGracefulRestart(aResp);
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301898 },
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001899
1900 "xyz.openbmc_project.Software.BMC.Updater",
1901 "/xyz/openbmc_project/software/" + firmwareId,
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301902 "org.freedesktop.DBus.Properties", "Set",
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001903 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
Ed Tanous168e20c2021-12-13 14:39:53 -08001904 dbus::utility::DbusVariantType(static_cast<uint8_t>(0)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001905 },
1906 "xyz.openbmc_project.Software.BMC.Updater",
1907 "/xyz/openbmc_project/software", "org.freedesktop.DBus.ObjectManager",
1908 "GetManagedObjects");
1909}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001910
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001911inline void setDateTime(std::shared_ptr<bmcweb::AsyncResp> aResp,
1912 std::string datetime)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001913{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001914 BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001915
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001916 std::stringstream stream(datetime);
1917 // Convert from ISO 8601 to boost local_time
1918 // (BMC only has time in UTC)
1919 boost::posix_time::ptime posixTime;
1920 boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
1921 // Facet gets deleted with the stringsteam
1922 auto ifc = std::make_unique<boost::local_time::local_time_input_facet>(
1923 "%Y-%m-%d %H:%M:%S%F %ZP");
1924 stream.imbue(std::locale(stream.getloc(), ifc.release()));
1925
1926 boost::local_time::local_date_time ldt(boost::local_time::not_a_date_time);
1927
1928 if (stream >> ldt)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001929 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001930 posixTime = ldt.utc_time();
1931 boost::posix_time::time_duration dur = posixTime - epoch;
1932 uint64_t durMicroSecs = static_cast<uint64_t>(dur.total_microseconds());
1933 crow::connections::systemBus->async_method_call(
1934 [aResp{std::move(aResp)}, datetime{std::move(datetime)}](
1935 const boost::system::error_code ec) {
1936 if (ec)
1937 {
1938 BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1939 "DBUS response error "
1940 << ec;
1941 messages::internalError(aResp->res);
1942 return;
1943 }
1944 aResp->res.jsonValue["DateTime"] = datetime;
1945 },
1946 "xyz.openbmc_project.Time.Manager", "/xyz/openbmc_project/time/bmc",
1947 "org.freedesktop.DBus.Properties", "Set",
1948 "xyz.openbmc_project.Time.EpochTime", "Elapsed",
Ed Tanous168e20c2021-12-13 14:39:53 -08001949 dbus::utility::DbusVariantType(durMicroSecs));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001950 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001951 else
1952 {
1953 messages::propertyValueFormatError(aResp->res, datetime, "DateTime");
1954 return;
1955 }
1956}
1957
1958inline void requestRoutesManager(App& app)
1959{
1960 std::string uuid = persistent_data::getConfig().systemUuid;
1961
1962 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07001963 .privileges(redfish::privileges::getManager)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001964 .methods(
1965 boost::beast::http::verb::
1966 get)([&app, uuid](
1967 const crow::Request& req,
1968 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1969 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1970 {
1971 return;
1972 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001973 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
1974 asyncResp->res.jsonValue["@odata.type"] =
1975 "#Manager.v1_11_0.Manager";
1976 asyncResp->res.jsonValue["Id"] = "bmc";
1977 asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
1978 asyncResp->res.jsonValue["Description"] =
1979 "Baseboard Management Controller";
1980 asyncResp->res.jsonValue["PowerState"] = "On";
1981 asyncResp->res.jsonValue["Status"] = {{"State", "Enabled"},
1982 {"Health", "OK"}};
1983 asyncResp->res.jsonValue["ManagerType"] = "BMC";
1984 asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
1985 asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1986 asyncResp->res.jsonValue["Model"] =
1987 "OpenBmc"; // TODO(ed), get model
1988
1989 asyncResp->res.jsonValue["LogServices"] = {
1990 {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}};
1991
1992 asyncResp->res.jsonValue["NetworkProtocol"] = {
1993 {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}};
1994
1995 asyncResp->res.jsonValue["EthernetInterfaces"] = {
1996 {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}};
1997
1998#ifdef BMCWEB_ENABLE_VM_NBDPROXY
1999 asyncResp->res.jsonValue["VirtualMedia"] = {
2000 {"@odata.id", "/redfish/v1/Managers/bmc/VirtualMedia"}};
2001#endif // BMCWEB_ENABLE_VM_NBDPROXY
2002
2003 // default oem data
2004 nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
2005 nlohmann::json& oemOpenbmc = oem["OpenBmc"];
2006 oem["@odata.type"] = "#OemManager.Oem";
2007 oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
2008 oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
2009 oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
2010 oemOpenbmc["Certificates"] = {
2011 {"@odata.id",
2012 "/redfish/v1/Managers/bmc/Truststore/Certificates"}};
2013
2014 // Manager.Reset (an action) can be many values, OpenBMC only
2015 // supports BMC reboot.
2016 nlohmann::json& managerReset =
2017 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
2018 managerReset["target"] =
2019 "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
2020 managerReset["@Redfish.ActionInfo"] =
2021 "/redfish/v1/Managers/bmc/ResetActionInfo";
2022
2023 // ResetToDefaults (Factory Reset) has values like
2024 // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
2025 // on OpenBMC
2026 nlohmann::json& resetToDefaults =
2027 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
2028 resetToDefaults["target"] =
2029 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
2030 resetToDefaults["ResetType@Redfish.AllowableValues"] = {"ResetAll"};
2031
Tejas Patil7c8c4052021-06-04 17:43:14 +05302032 std::pair<std::string, std::string> redfishDateTimeOffset =
2033 crow::utility::getDateTimeOffsetNow();
2034
2035 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2036 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2037 redfishDateTimeOffset.second;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002038
Gunnar Mills0e8ac5e2020-11-06 15:33:24 -06002039 // TODO (Gunnar): Remove these one day since moved to ComputerSystem
2040 // Still used by OCP profiles
2041 // https://github.com/opencomputeproject/OCP-Profiles/issues/23
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002042 // Fill in SerialConsole info
2043 asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
2044 asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] =
2045 15;
2046 asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
2047 {"IPMI", "SSH"};
2048#ifdef BMCWEB_ENABLE_KVM
2049 // Fill in GraphicalConsole info
2050 asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] =
2051 true;
2052 asyncResp->res
2053 .jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4;
2054 asyncResp->res.jsonValue["GraphicalConsole"]
2055 ["ConnectTypesSupported"] = {"KVMIP"};
2056#endif // BMCWEB_ENABLE_KVM
2057
2058 asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] =
2059 1;
2060 asyncResp->res.jsonValue["Links"]["ManagerForServers"] = {
2061 {{"@odata.id", "/redfish/v1/Systems/system"}}};
2062
2063 auto health = std::make_shared<HealthPopulate>(asyncResp);
2064 health->isManagersHealth = true;
2065 health->populate();
2066
2067 fw_util::populateFirmwareInformation(asyncResp, fw_util::bmcPurpose,
2068 "FirmwareVersion", true);
2069
2070 managerGetLastResetTime(asyncResp);
2071
2072 auto pids = std::make_shared<GetPIDValues>(asyncResp);
2073 pids->run();
2074
2075 getMainChassisId(
2076 asyncResp, [](const std::string& chassisId,
2077 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2078 aRsp->res
2079 .jsonValue["Links"]["ManagerForChassis@odata.count"] =
2080 1;
2081 aRsp->res.jsonValue["Links"]["ManagerForChassis"] = {
2082 {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
2083 aRsp->res.jsonValue["Links"]["ManagerInChassis"] = {
2084 {"@odata.id", "/redfish/v1/Chassis/" + chassisId}};
2085 });
2086
2087 static bool started = false;
2088
2089 if (!started)
2090 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002091 sdbusplus::asio::getProperty<double>(
2092 *crow::connections::systemBus, "org.freedesktop.systemd1",
2093 "/org/freedesktop/systemd1",
2094 "org.freedesktop.systemd1.Manager", "Progress",
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002095 [asyncResp](const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002096 const double& val) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002097 if (ec)
2098 {
2099 BMCWEB_LOG_ERROR << "Error while getting progress";
2100 messages::internalError(asyncResp->res);
2101 return;
2102 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002103 if (val < 1.0)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002104 {
2105 asyncResp->res.jsonValue["Status"]["State"] =
2106 "Starting";
2107 started = true;
2108 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002109 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002110 }
2111
2112 crow::connections::systemBus->async_method_call(
2113 [asyncResp](
2114 const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002115 const dbus::utility::MapperGetSubTreeResponse& subtree) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002116 if (ec)
2117 {
2118 BMCWEB_LOG_DEBUG
2119 << "D-Bus response error on GetSubTree " << ec;
2120 return;
2121 }
Ed Tanous26f69762022-01-25 09:49:11 -08002122 if (subtree.empty())
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002123 {
2124 BMCWEB_LOG_DEBUG << "Can't find bmc D-Bus object!";
2125 return;
2126 }
2127 // Assume only 1 bmc D-Bus object
2128 // Throw an error if there is more than 1
2129 if (subtree.size() > 1)
2130 {
2131 BMCWEB_LOG_DEBUG
2132 << "Found more than 1 bmc D-Bus object!";
2133 messages::internalError(asyncResp->res);
2134 return;
2135 }
2136
2137 if (subtree[0].first.empty() ||
2138 subtree[0].second.size() != 1)
2139 {
2140 BMCWEB_LOG_DEBUG << "Error getting bmc D-Bus object!";
2141 messages::internalError(asyncResp->res);
2142 return;
2143 }
2144
2145 const std::string& path = subtree[0].first;
2146 const std::string& connectionName =
2147 subtree[0].second[0].first;
2148
2149 for (const auto& interfaceName :
2150 subtree[0].second[0].second)
2151 {
2152 if (interfaceName ==
2153 "xyz.openbmc_project.Inventory.Decorator.Asset")
2154 {
2155 crow::connections::systemBus->async_method_call(
2156 [asyncResp](
2157 const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08002158 const dbus::utility::DBusPropertiesMap&
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002159 propertiesList) {
2160 if (ec)
2161 {
2162 BMCWEB_LOG_DEBUG
2163 << "Can't get bmc asset!";
2164 return;
2165 }
2166 for (const std::pair<
2167 std::string,
Ed Tanous168e20c2021-12-13 14:39:53 -08002168 dbus::utility::DbusVariantType>&
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002169 property : propertiesList)
2170 {
2171 const std::string& propertyName =
2172 property.first;
2173
2174 if ((propertyName == "PartNumber") ||
2175 (propertyName == "SerialNumber") ||
2176 (propertyName == "Manufacturer") ||
2177 (propertyName == "Model") ||
2178 (propertyName == "SparePartNumber"))
2179 {
2180 const std::string* value =
2181 std::get_if<std::string>(
2182 &property.second);
2183 if (value == nullptr)
2184 {
2185 // illegal property
2186 messages::internalError(
2187 asyncResp->res);
2188 return;
2189 }
2190 asyncResp->res
2191 .jsonValue[propertyName] =
2192 *value;
2193 }
2194 }
2195 },
2196 connectionName, path,
2197 "org.freedesktop.DBus.Properties", "GetAll",
George Liu0fda0f12021-11-16 10:06:17 +08002198 "xyz.openbmc_project.Inventory.Decorator.Asset");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002199 }
George Liu0fda0f12021-11-16 10:06:17 +08002200 else if (
2201 interfaceName ==
2202 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002203 {
2204 getLocation(asyncResp, connectionName, path);
2205 }
2206 }
2207 },
2208 "xyz.openbmc_project.ObjectMapper",
2209 "/xyz/openbmc_project/object_mapper",
2210 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2211 "/xyz/openbmc_project/inventory", int32_t(0),
2212 std::array<const char*, 1>{
2213 "xyz.openbmc_project.Inventory.Item.Bmc"});
2214 });
2215
2216 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07002217 .privileges(redfish::privileges::patchManager)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002218 .methods(boost::beast::http::verb::patch)(
2219 [&app](const crow::Request& req,
2220 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2221 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002222 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002223 return;
2224 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07002225 std::optional<nlohmann::json> oem;
2226 std::optional<nlohmann::json> links;
2227 std::optional<std::string> datetime;
2228
2229 if (!json_util::readJsonPatch(req, asyncResp->res, "Oem", oem,
2230 "DateTime", datetime, "Links",
2231 links))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002232 {
Ed Tanous45ca1b82022-03-25 13:07:27 -07002233 return;
2234 }
2235
2236 if (oem)
2237 {
2238 std::optional<nlohmann::json> openbmc;
2239 if (!redfish::json_util::readJson(*oem, asyncResp->res,
2240 "OpenBmc", openbmc))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002241 {
2242 BMCWEB_LOG_ERROR
2243 << "Illegal Property "
Ed Tanous45ca1b82022-03-25 13:07:27 -07002244 << oem->dump(
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002245 2, ' ', true,
2246 nlohmann::json::error_handler_t::replace);
2247 return;
2248 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07002249 if (openbmc)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002250 {
Ed Tanous45ca1b82022-03-25 13:07:27 -07002251 std::optional<nlohmann::json> fan;
2252 if (!redfish::json_util::readJson(
2253 *openbmc, asyncResp->res, "Fan", fan))
2254 {
2255 BMCWEB_LOG_ERROR
2256 << "Illegal Property "
2257 << openbmc->dump(2, ' ', true,
2258 nlohmann::json::
2259 error_handler_t::replace);
2260 return;
2261 }
2262 if (fan)
2263 {
2264 auto pid =
2265 std::make_shared<SetPIDValues>(asyncResp, *fan);
2266 pid->run();
2267 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002268 }
2269 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07002270 if (links)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002271 {
Ed Tanous45ca1b82022-03-25 13:07:27 -07002272 std::optional<nlohmann::json> activeSoftwareImage;
2273 if (!redfish::json_util::readJson(*links, asyncResp->res,
2274 "ActiveSoftwareImage",
2275 activeSoftwareImage))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002276 {
2277 return;
2278 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07002279 if (activeSoftwareImage)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002280 {
Ed Tanous45ca1b82022-03-25 13:07:27 -07002281 std::optional<std::string> odataId;
2282 if (!json_util::readJson(*activeSoftwareImage,
2283 asyncResp->res, "@odata.id",
2284 odataId))
2285 {
2286 return;
2287 }
2288
2289 if (odataId)
2290 {
2291 setActiveFirmwareImage(asyncResp, *odataId);
2292 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002293 }
2294 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07002295 if (datetime)
2296 {
2297 setDateTime(asyncResp, std::move(*datetime));
2298 }
2299 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002300}
2301
2302inline void requestRoutesManagerCollection(App& app)
2303{
2304 BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
Ed Tanoused398212021-06-09 17:05:54 -07002305 .privileges(redfish::privileges::getManagerCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002306 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002307 [&app](const crow::Request& req,
2308 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2309 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2310 {
2311 return;
2312 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002313 // Collections don't include the static data added by SubRoute
2314 // because it has a duplicate entry for members
2315 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
2316 asyncResp->res.jsonValue["@odata.type"] =
2317 "#ManagerCollection.ManagerCollection";
2318 asyncResp->res.jsonValue["Name"] = "Manager Collection";
2319 asyncResp->res.jsonValue["Members@odata.count"] = 1;
2320 asyncResp->res.jsonValue["Members"] = {
2321 {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
2322 });
2323}
Ed Tanous1abe55e2018-09-05 08:30:59 -07002324} // namespace redfish