blob: 67f8d990206ad5855db3cf415503b7657f9d0b65 [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"
Borawski.Lukasz9c3106852018-02-09 15:24:22 +010019#include "node.hpp"
Jennifer Leec5d03ff2019-03-08 15:42:58 -080020#include "redfish_util.hpp"
Borawski.Lukasz9c3106852018-02-09 15:24:22 +010021
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>
Andrew Geisslere90c5052019-06-28 13:52:27 -050025#include <utils/fw_utils.hpp>
Bernard Wong7bffdb72019-03-20 16:17:21 +080026#include <utils/systemd_utils.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050027
Gunnar Mills4bfefa72020-07-30 13:54:29 -050028#include <cstdint>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050029#include <memory>
30#include <sstream>
Ed Tanousabf2add2019-01-22 16:40:12 -080031#include <variant>
James Feist5b4aa862018-08-16 14:07:01 -070032
Ed Tanous1abe55e2018-09-05 08:30:59 -070033namespace redfish
34{
Jennifer Leeed5befb2018-08-10 11:29:45 -070035
36/**
Gunnar Mills2a5c4402020-05-19 09:07:24 -050037 * Function reboots the BMC.
38 *
39 * @param[in] asyncResp - Shared pointer for completing asynchronous calls
Jennifer Leeed5befb2018-08-10 11:29:45 -070040 */
zhanghch058d1b46d2021-04-01 11:18:24 +080041inline void
42 doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Gunnar Mills2a5c4402020-05-19 09:07:24 -050043{
44 const char* processName = "xyz.openbmc_project.State.BMC";
45 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
46 const char* interfaceName = "xyz.openbmc_project.State.BMC";
47 const std::string& propertyValue =
48 "xyz.openbmc_project.State.BMC.Transition.Reboot";
49 const char* destProperty = "RequestedBMCTransition";
50
51 // Create the D-Bus variant for D-Bus call.
52 VariantType dbusPropertyValue(propertyValue);
53
54 crow::connections::systemBus->async_method_call(
55 [asyncResp](const boost::system::error_code ec) {
56 // Use "Set" method to set the property value.
57 if (ec)
58 {
59 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
60 messages::internalError(asyncResp->res);
61 return;
62 }
63
64 messages::success(asyncResp->res);
65 },
66 processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
67 interfaceName, destProperty, dbusPropertyValue);
68}
69
zhanghch058d1b46d2021-04-01 11:18:24 +080070inline void
71 doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +000072{
73 const char* processName = "xyz.openbmc_project.State.BMC";
74 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
75 const char* interfaceName = "xyz.openbmc_project.State.BMC";
76 const std::string& propertyValue =
77 "xyz.openbmc_project.State.BMC.Transition.HardReboot";
78 const char* destProperty = "RequestedBMCTransition";
79
80 // Create the D-Bus variant for D-Bus call.
81 VariantType dbusPropertyValue(propertyValue);
82
83 crow::connections::systemBus->async_method_call(
84 [asyncResp](const boost::system::error_code ec) {
85 // Use "Set" method to set the property value.
86 if (ec)
87 {
88 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
89 messages::internalError(asyncResp->res);
90 return;
91 }
92
93 messages::success(asyncResp->res);
94 },
95 processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
96 interfaceName, destProperty, dbusPropertyValue);
97}
98
Gunnar Mills2a5c4402020-05-19 09:07:24 -050099/**
100 * ManagerResetAction class supports the POST method for the Reset (reboot)
101 * action.
102 */
103class ManagerResetAction : public Node
Jennifer Leeed5befb2018-08-10 11:29:45 -0700104{
105 public:
Ed Tanous52cc1122020-07-18 13:51:21 -0700106 ManagerResetAction(App& app) :
Jennifer Leeed5befb2018-08-10 11:29:45 -0700107 Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
108 {
109 entityPrivileges = {
Jennifer Leeed5befb2018-08-10 11:29:45 -0700110 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
111 }
112
113 private:
114 /**
Jennifer Leeed5befb2018-08-10 11:29:45 -0700115 * Function handles POST method request.
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500116 * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000117 * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
Jennifer Leeed5befb2018-08-10 11:29:45 -0700118 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800119 void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
120 const crow::Request& req,
Ed Tanouscb13a392020-07-25 19:02:03 +0000121 const std::vector<std::string>&) override
Jennifer Leeed5befb2018-08-10 11:29:45 -0700122 {
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500123 BMCWEB_LOG_DEBUG << "Post Manager Reset.";
Jennifer Leeed5befb2018-08-10 11:29:45 -0700124
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500125 std::string resetType;
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500126
127 if (!json_util::readJson(req, asyncResp->res, "ResetType", resetType))
Jennifer Leeed5befb2018-08-10 11:29:45 -0700128 {
129 return;
130 }
131
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000132 if (resetType == "GracefulRestart")
133 {
134 BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
135 doBMCGracefulRestart(asyncResp);
136 return;
137 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700138 if (resetType == "ForceRestart")
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000139 {
140 BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
141 doBMCForceRestart(asyncResp);
142 return;
143 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700144 BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
145 << resetType;
146 messages::actionParameterNotSupported(asyncResp->res, resetType,
147 "ResetType");
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500148
Ed Tanous3174e4d2020-10-07 11:41:22 -0700149 return;
Jennifer Leeed5befb2018-08-10 11:29:45 -0700150 }
151};
152
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500153/**
154 * ManagerResetToDefaultsAction class supports POST method for factory reset
155 * action.
156 */
157class ManagerResetToDefaultsAction : public Node
158{
159 public:
Ed Tanous52cc1122020-07-18 13:51:21 -0700160 ManagerResetToDefaultsAction(App& app) :
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500161 Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/")
162 {
163 entityPrivileges = {
164 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
165 }
166
167 private:
168 /**
169 * Function handles ResetToDefaults POST method request.
170 *
171 * Analyzes POST body message and factory resets BMC by calling
172 * BMC code updater factory reset followed by a BMC reboot.
173 *
174 * BMC code updater factory reset wipes the whole BMC read-write
175 * filesystem which includes things like the network settings.
176 *
177 * OpenBMC only supports ResetToDefaultsType "ResetAll".
178 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800179 void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
180 const crow::Request& req,
Ed Tanouscb13a392020-07-25 19:02:03 +0000181 const std::vector<std::string>&) override
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500182 {
183 BMCWEB_LOG_DEBUG << "Post ResetToDefaults.";
184
185 std::string resetType;
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500186
187 if (!json_util::readJson(req, asyncResp->res, "ResetToDefaultsType",
188 resetType))
189 {
190 BMCWEB_LOG_DEBUG << "Missing property ResetToDefaultsType.";
191
192 messages::actionParameterMissing(asyncResp->res, "ResetToDefaults",
193 "ResetToDefaultsType");
194 return;
195 }
196
197 if (resetType != "ResetAll")
198 {
199 BMCWEB_LOG_DEBUG << "Invalid property value for "
200 "ResetToDefaultsType: "
201 << resetType;
202 messages::actionParameterNotSupported(asyncResp->res, resetType,
203 "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: " << ec;
212 messages::internalError(asyncResp->res);
213 return;
214 }
215 // Factory Reset doesn't actually happen until a reboot
216 // Can't erase what the BMC is running on
217 doBMCGracefulRestart(asyncResp);
218 },
219 "xyz.openbmc_project.Software.BMC.Updater",
220 "/xyz/openbmc_project/software",
221 "xyz.openbmc_project.Common.FactoryReset", "Reset");
222 }
223};
224
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530225/**
226 * ManagerResetActionInfo derived class for delivering Manager
227 * ResetType AllowableValues using ResetInfo schema.
228 */
229class ManagerResetActionInfo : public Node
230{
231 public:
232 /*
233 * Default Constructor
234 */
Ed Tanous52cc1122020-07-18 13:51:21 -0700235 ManagerResetActionInfo(App& app) :
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530236 Node(app, "/redfish/v1/Managers/bmc/ResetActionInfo/")
237 {
238 entityPrivileges = {
239 {boost::beast::http::verb::get, {{"Login"}}},
240 {boost::beast::http::verb::head, {{"Login"}}},
241 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
242 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
243 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
244 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
245 }
246
247 private:
248 /**
249 * Functions triggers appropriate requests on DBus
250 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800251 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
252 const crow::Request&, const std::vector<std::string>&) override
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530253 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800254 asyncResp->res.jsonValue = {
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530255 {"@odata.type", "#ActionInfo.v1_1_2.ActionInfo"},
256 {"@odata.id", "/redfish/v1/Managers/bmc/ResetActionInfo"},
257 {"Name", "Reset Action Info"},
258 {"Id", "ResetActionInfo"},
259 {"Parameters",
260 {{{"Name", "ResetType"},
261 {"Required", true},
262 {"DataType", "String"},
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000263 {"AllowableValues", {"GracefulRestart", "ForceRestart"}}}}}};
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530264 }
265};
266
James Feist5b4aa862018-08-16 14:07:01 -0700267static constexpr const char* objectManagerIface =
268 "org.freedesktop.DBus.ObjectManager";
269static constexpr const char* pidConfigurationIface =
270 "xyz.openbmc_project.Configuration.Pid";
271static constexpr const char* pidZoneConfigurationIface =
272 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feistb7a08d02018-12-11 14:55:37 -0800273static constexpr const char* stepwiseConfigurationIface =
274 "xyz.openbmc_project.Configuration.Stepwise";
James Feist73df0db2019-03-25 15:29:35 -0700275static constexpr const char* thermalModeIface =
276 "xyz.openbmc_project.Control.ThermalMode";
Borawski.Lukasz9c3106852018-02-09 15:24:22 +0100277
zhanghch058d1b46d2021-04-01 11:18:24 +0800278inline void
279 asyncPopulatePid(const std::string& connection, const std::string& path,
280 const std::string& currentProfile,
281 const std::vector<std::string>& supportedProfiles,
282 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
James Feist5b4aa862018-08-16 14:07:01 -0700283{
284
285 crow::connections::systemBus->async_method_call(
James Feist73df0db2019-03-25 15:29:35 -0700286 [asyncResp, currentProfile, supportedProfiles](
287 const boost::system::error_code ec,
288 const dbus::utility::ManagedObjectType& managedObj) {
James Feist5b4aa862018-08-16 14:07:01 -0700289 if (ec)
290 {
291 BMCWEB_LOG_ERROR << ec;
James Feist5b4aa862018-08-16 14:07:01 -0700292 asyncResp->res.jsonValue.clear();
Jason M. Billsf12894f2018-10-09 12:45:45 -0700293 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700294 return;
295 }
296 nlohmann::json& configRoot =
297 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
298 nlohmann::json& fans = configRoot["FanControllers"];
299 fans["@odata.type"] = "#OemManager.FanControllers";
James Feist5b4aa862018-08-16 14:07:01 -0700300 fans["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/"
301 "Fan/FanControllers";
302
303 nlohmann::json& pids = configRoot["PidControllers"];
304 pids["@odata.type"] = "#OemManager.PidControllers";
James Feist5b4aa862018-08-16 14:07:01 -0700305 pids["@odata.id"] =
306 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
307
James Feistb7a08d02018-12-11 14:55:37 -0800308 nlohmann::json& stepwise = configRoot["StepwiseControllers"];
309 stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
James Feistb7a08d02018-12-11 14:55:37 -0800310 stepwise["@odata.id"] =
311 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
312
James Feist5b4aa862018-08-16 14:07:01 -0700313 nlohmann::json& zones = configRoot["FanZones"];
314 zones["@odata.id"] =
315 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
316 zones["@odata.type"] = "#OemManager.FanZones";
James Feist5b4aa862018-08-16 14:07:01 -0700317 configRoot["@odata.id"] =
318 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
319 configRoot["@odata.type"] = "#OemManager.Fan";
James Feist73df0db2019-03-25 15:29:35 -0700320 configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
321
322 if (!currentProfile.empty())
323 {
324 configRoot["Profile"] = currentProfile;
325 }
326 BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !";
James Feist5b4aa862018-08-16 14:07:01 -0700327
James Feist5b4aa862018-08-16 14:07:01 -0700328 for (const auto& pathPair : managedObj)
329 {
330 for (const auto& intfPair : pathPair.second)
331 {
332 if (intfPair.first != pidConfigurationIface &&
James Feistb7a08d02018-12-11 14:55:37 -0800333 intfPair.first != pidZoneConfigurationIface &&
334 intfPair.first != stepwiseConfigurationIface)
James Feist5b4aa862018-08-16 14:07:01 -0700335 {
336 continue;
337 }
338 auto findName = intfPair.second.find("Name");
339 if (findName == intfPair.second.end())
340 {
341 BMCWEB_LOG_ERROR << "Pid Field missing Name";
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800342 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700343 return;
344 }
James Feist73df0db2019-03-25 15:29:35 -0700345
James Feist5b4aa862018-08-16 14:07:01 -0700346 const std::string* namePtr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800347 std::get_if<std::string>(&findName->second);
James Feist5b4aa862018-08-16 14:07:01 -0700348 if (namePtr == nullptr)
349 {
350 BMCWEB_LOG_ERROR << "Pid Name Field illegal";
James Feistb7a08d02018-12-11 14:55:37 -0800351 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700352 return;
353 }
James Feist5b4aa862018-08-16 14:07:01 -0700354 std::string name = *namePtr;
355 dbus::utility::escapePathForDbus(name);
James Feist73df0db2019-03-25 15:29:35 -0700356
357 auto findProfiles = intfPair.second.find("Profiles");
358 if (findProfiles != intfPair.second.end())
359 {
360 const std::vector<std::string>* profiles =
361 std::get_if<std::vector<std::string>>(
362 &findProfiles->second);
363 if (profiles == nullptr)
364 {
365 BMCWEB_LOG_ERROR << "Pid Profiles Field illegal";
366 messages::internalError(asyncResp->res);
367 return;
368 }
369 if (std::find(profiles->begin(), profiles->end(),
370 currentProfile) == profiles->end())
371 {
372 BMCWEB_LOG_INFO
373 << name << " not supported in current profile";
374 continue;
375 }
376 }
James Feistb7a08d02018-12-11 14:55:37 -0800377 nlohmann::json* config = nullptr;
James Feistc33a90e2019-03-01 10:17:44 -0800378
379 const std::string* classPtr = nullptr;
380 auto findClass = intfPair.second.find("Class");
381 if (findClass != intfPair.second.end())
382 {
383 classPtr = std::get_if<std::string>(&findClass->second);
384 }
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}};
397 zone["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/"
398 "OpenBmc/Fan/FanZones/" +
399 name;
400 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"] =
417 "/redfish/v1/Managers/bmc#/Oem/"
418 "OpenBmc/Fan/StepwiseControllers/" +
Ed Tanous271584a2019-07-09 16:24:22 -0700419 name;
James Feistb7a08d02018-12-11 14:55:37 -0800420 controller["@odata.type"] =
421 "#OemManager.StepwiseController";
422
James Feistc33a90e2019-03-01 10:17:44 -0800423 controller["Direction"] = *classPtr;
James Feistb7a08d02018-12-11 14:55:37 -0800424 }
425
426 // pid and fans are off the same configuration
427 else if (intfPair.first == pidConfigurationIface)
428 {
James Feistc33a90e2019-03-01 10:17:44 -0800429
James Feistb7a08d02018-12-11 14:55:37 -0800430 if (classPtr == nullptr)
431 {
432 BMCWEB_LOG_ERROR << "Pid Class Field illegal";
433 messages::internalError(asyncResp->res);
434 return;
435 }
436 bool isFan = *classPtr == "fan";
437 nlohmann::json& element =
438 isFan ? fans[name] : pids[name];
439 config = &element;
440 if (isFan)
441 {
442 element["@odata.id"] =
443 "/redfish/v1/Managers/bmc#/Oem/"
444 "OpenBmc/Fan/FanControllers/" +
Ed Tanous271584a2019-07-09 16:24:22 -0700445 name;
James Feistb7a08d02018-12-11 14:55:37 -0800446 element["@odata.type"] =
447 "#OemManager.FanController";
James Feistb7a08d02018-12-11 14:55:37 -0800448 }
449 else
450 {
451 element["@odata.id"] =
452 "/redfish/v1/Managers/bmc#/Oem/"
453 "OpenBmc/Fan/PidControllers/" +
Ed Tanous271584a2019-07-09 16:24:22 -0700454 name;
James Feistb7a08d02018-12-11 14:55:37 -0800455 element["@odata.type"] =
456 "#OemManager.PidController";
James Feistb7a08d02018-12-11 14:55:37 -0800457 }
458 }
459 else
460 {
461 BMCWEB_LOG_ERROR << "Unexpected configuration";
462 messages::internalError(asyncResp->res);
463 return;
464 }
465
466 // used for making maps out of 2 vectors
467 const std::vector<double>* keys = nullptr;
468 const std::vector<double>* values = nullptr;
469
James Feist5b4aa862018-08-16 14:07:01 -0700470 for (const auto& propertyPair : intfPair.second)
471 {
472 if (propertyPair.first == "Type" ||
473 propertyPair.first == "Class" ||
474 propertyPair.first == "Name")
475 {
476 continue;
477 }
478
479 // zones
480 if (intfPair.first == pidZoneConfigurationIface)
481 {
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800482 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800483 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700484 if (ptr == nullptr)
485 {
486 BMCWEB_LOG_ERROR << "Field Illegal "
487 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700488 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700489 return;
490 }
James Feistb7a08d02018-12-11 14:55:37 -0800491 (*config)[propertyPair.first] = *ptr;
492 }
493
494 if (intfPair.first == stepwiseConfigurationIface)
495 {
496 if (propertyPair.first == "Reading" ||
497 propertyPair.first == "Output")
498 {
499 const std::vector<double>* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800500 std::get_if<std::vector<double>>(
James Feistb7a08d02018-12-11 14:55:37 -0800501 &propertyPair.second);
502
503 if (ptr == nullptr)
504 {
505 BMCWEB_LOG_ERROR << "Field Illegal "
506 << propertyPair.first;
507 messages::internalError(asyncResp->res);
508 return;
509 }
510
511 if (propertyPair.first == "Reading")
512 {
513 keys = ptr;
514 }
515 else
516 {
517 values = ptr;
518 }
519 if (keys && values)
520 {
521 if (keys->size() != values->size())
522 {
523 BMCWEB_LOG_ERROR
524 << "Reading and Output size don't "
525 "match ";
526 messages::internalError(asyncResp->res);
527 return;
528 }
529 nlohmann::json& steps = (*config)["Steps"];
530 steps = nlohmann::json::array();
531 for (size_t ii = 0; ii < keys->size(); ii++)
532 {
533 steps.push_back(
534 {{"Target", (*keys)[ii]},
535 {"Output", (*values)[ii]}});
536 }
537 }
538 }
539 if (propertyPair.first == "NegativeHysteresis" ||
540 propertyPair.first == "PositiveHysteresis")
541 {
542 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800543 std::get_if<double>(&propertyPair.second);
James Feistb7a08d02018-12-11 14:55:37 -0800544 if (ptr == nullptr)
545 {
546 BMCWEB_LOG_ERROR << "Field Illegal "
547 << propertyPair.first;
548 messages::internalError(asyncResp->res);
549 return;
550 }
551 (*config)[propertyPair.first] = *ptr;
552 }
James Feist5b4aa862018-08-16 14:07:01 -0700553 }
554
555 // pid and fans are off the same configuration
James Feistb7a08d02018-12-11 14:55:37 -0800556 if (intfPair.first == pidConfigurationIface ||
557 intfPair.first == stepwiseConfigurationIface)
James Feist5b4aa862018-08-16 14:07:01 -0700558 {
James Feist5b4aa862018-08-16 14:07:01 -0700559
560 if (propertyPair.first == "Zones")
561 {
562 const std::vector<std::string>* inputs =
Ed Tanousabf2add2019-01-22 16:40:12 -0800563 std::get_if<std::vector<std::string>>(
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800564 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700565
566 if (inputs == nullptr)
567 {
568 BMCWEB_LOG_ERROR
569 << "Zones Pid Field Illegal";
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800570 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700571 return;
572 }
James Feistb7a08d02018-12-11 14:55:37 -0800573 auto& data = (*config)[propertyPair.first];
James Feist5b4aa862018-08-16 14:07:01 -0700574 data = nlohmann::json::array();
575 for (std::string itemCopy : *inputs)
576 {
577 dbus::utility::escapePathForDbus(itemCopy);
578 data.push_back(
579 {{"@odata.id",
580 "/redfish/v1/Managers/bmc#/Oem/"
581 "OpenBmc/Fan/FanZones/" +
582 itemCopy}});
583 }
584 }
585 // todo(james): may never happen, but this
586 // assumes configuration data referenced in the
587 // PID config is provided by the same daemon, we
588 // could add another loop to cover all cases,
589 // but I'm okay kicking this can down the road a
590 // bit
591
592 else if (propertyPair.first == "Inputs" ||
593 propertyPair.first == "Outputs")
594 {
James Feistb7a08d02018-12-11 14:55:37 -0800595 auto& data = (*config)[propertyPair.first];
James Feist5b4aa862018-08-16 14:07:01 -0700596 const std::vector<std::string>* inputs =
Ed Tanousabf2add2019-01-22 16:40:12 -0800597 std::get_if<std::vector<std::string>>(
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800598 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700599
600 if (inputs == nullptr)
601 {
602 BMCWEB_LOG_ERROR << "Field Illegal "
603 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700604 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700605 return;
606 }
607 data = *inputs;
James Feistb943aae2019-07-11 16:33:56 -0700608 }
609 else if (propertyPair.first == "SetPointOffset")
610 {
611 const std::string* ptr =
612 std::get_if<std::string>(
613 &propertyPair.second);
614
615 if (ptr == nullptr)
616 {
617 BMCWEB_LOG_ERROR << "Field Illegal "
618 << propertyPair.first;
619 messages::internalError(asyncResp->res);
620 return;
621 }
622 // translate from dbus to redfish
623 if (*ptr == "WarningHigh")
624 {
625 (*config)["SetPointOffset"] =
626 "UpperThresholdNonCritical";
627 }
628 else if (*ptr == "WarningLow")
629 {
630 (*config)["SetPointOffset"] =
631 "LowerThresholdNonCritical";
632 }
633 else if (*ptr == "CriticalHigh")
634 {
635 (*config)["SetPointOffset"] =
636 "UpperThresholdCritical";
637 }
638 else if (*ptr == "CriticalLow")
639 {
640 (*config)["SetPointOffset"] =
641 "LowerThresholdCritical";
642 }
643 else
644 {
645 BMCWEB_LOG_ERROR << "Value Illegal "
646 << *ptr;
647 messages::internalError(asyncResp->res);
648 return;
649 }
650 }
651 // doubles
James Feist5b4aa862018-08-16 14:07:01 -0700652 else if (propertyPair.first ==
653 "FFGainCoefficient" ||
654 propertyPair.first == "FFOffCoefficient" ||
655 propertyPair.first == "ICoefficient" ||
656 propertyPair.first == "ILimitMax" ||
657 propertyPair.first == "ILimitMin" ||
James Feistaad1a252019-02-19 10:13:52 -0800658 propertyPair.first ==
659 "PositiveHysteresis" ||
660 propertyPair.first ==
661 "NegativeHysteresis" ||
James Feist5b4aa862018-08-16 14:07:01 -0700662 propertyPair.first == "OutLimitMax" ||
663 propertyPair.first == "OutLimitMin" ||
664 propertyPair.first == "PCoefficient" ||
James Feist7625cb82019-01-23 11:58:21 -0800665 propertyPair.first == "SetPoint" ||
James Feist5b4aa862018-08-16 14:07:01 -0700666 propertyPair.first == "SlewNeg" ||
667 propertyPair.first == "SlewPos")
668 {
669 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800670 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700671 if (ptr == nullptr)
672 {
673 BMCWEB_LOG_ERROR << "Field Illegal "
674 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700675 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700676 return;
677 }
James Feistb7a08d02018-12-11 14:55:37 -0800678 (*config)[propertyPair.first] = *ptr;
James Feist5b4aa862018-08-16 14:07:01 -0700679 }
680 }
681 }
682 }
683 }
684 },
685 connection, path, objectManagerIface, "GetManagedObjects");
686}
Jennifer Leeca537922018-08-10 10:07:30 -0700687
James Feist83ff9ab2018-08-31 10:18:24 -0700688enum class CreatePIDRet
689{
690 fail,
691 del,
692 patch
693};
694
zhanghch058d1b46d2021-04-01 11:18:24 +0800695inline bool
696 getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
697 std::vector<nlohmann::json>& config,
698 std::vector<std::string>& zones)
James Feist5f2caae2018-12-12 14:08:25 -0800699{
James Feistb6baeaa2019-02-21 10:41:40 -0800700 if (config.empty())
701 {
702 BMCWEB_LOG_ERROR << "Empty Zones";
703 messages::propertyValueFormatError(response->res,
704 nlohmann::json::array(), "Zones");
705 return false;
706 }
James Feist5f2caae2018-12-12 14:08:25 -0800707 for (auto& odata : config)
708 {
709 std::string path;
710 if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
711 path))
712 {
713 return false;
714 }
715 std::string input;
James Feist61adbda2019-03-25 13:03:51 -0700716
717 // 8 below comes from
718 // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
719 // 0 1 2 3 4 5 6 7 8
720 if (!dbus::utility::getNthStringFromPath(path, 8, input))
James Feist5f2caae2018-12-12 14:08:25 -0800721 {
722 BMCWEB_LOG_ERROR << "Got invalid path " << path;
723 BMCWEB_LOG_ERROR << "Illegal Type Zones";
724 messages::propertyValueFormatError(response->res, odata.dump(),
725 "Zones");
726 return false;
727 }
728 boost::replace_all(input, "_", " ");
729 zones.emplace_back(std::move(input));
730 }
731 return true;
732}
733
Ed Tanous23a21a12020-07-25 04:45:05 +0000734inline const dbus::utility::ManagedItem*
James Feist73df0db2019-03-25 15:29:35 -0700735 findChassis(const dbus::utility::ManagedObjectType& managedObj,
736 const std::string& value, std::string& chassis)
James Feistb6baeaa2019-02-21 10:41:40 -0800737{
738 BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
739
740 std::string escaped = boost::replace_all_copy(value, " ", "_");
741 escaped = "/" + escaped;
742 auto it = std::find_if(
743 managedObj.begin(), managedObj.end(), [&escaped](const auto& obj) {
744 if (boost::algorithm::ends_with(obj.first.str, escaped))
745 {
746 BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
747 return true;
748 }
749 return false;
750 });
751
752 if (it == managedObj.end())
753 {
James Feist73df0db2019-03-25 15:29:35 -0700754 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800755 }
756 // 5 comes from <chassis-name> being the 5th element
757 // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
James Feist73df0db2019-03-25 15:29:35 -0700758 if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
759 {
760 return &(*it);
761 }
762
763 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800764}
765
Ed Tanous23a21a12020-07-25 04:45:05 +0000766inline CreatePIDRet createPidInterface(
zhanghch058d1b46d2021-04-01 11:18:24 +0800767 const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
Ed Tanousb5a76932020-09-29 16:16:58 -0700768 const nlohmann::json::iterator& it, const std::string& path,
James Feist83ff9ab2018-08-31 10:18:24 -0700769 const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
770 boost::container::flat_map<std::string, dbus::utility::DbusVariantType>&
771 output,
James Feist73df0db2019-03-25 15:29:35 -0700772 std::string& chassis, const std::string& profile)
James Feist83ff9ab2018-08-31 10:18:24 -0700773{
774
James Feist5f2caae2018-12-12 14:08:25 -0800775 // common deleter
James Feistb6baeaa2019-02-21 10:41:40 -0800776 if (it.value() == nullptr)
James Feist5f2caae2018-12-12 14:08:25 -0800777 {
778 std::string iface;
779 if (type == "PidControllers" || type == "FanControllers")
780 {
781 iface = pidConfigurationIface;
782 }
783 else if (type == "FanZones")
784 {
785 iface = pidZoneConfigurationIface;
786 }
787 else if (type == "StepwiseControllers")
788 {
789 iface = stepwiseConfigurationIface;
790 }
791 else
792 {
Gunnar Millsa0744d32020-11-09 15:40:45 -0600793 BMCWEB_LOG_ERROR << "Illegal Type " << type;
James Feist5f2caae2018-12-12 14:08:25 -0800794 messages::propertyUnknown(response->res, type);
795 return CreatePIDRet::fail;
796 }
James Feist6ee7f772020-02-06 16:25:27 -0800797
798 BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
James Feist5f2caae2018-12-12 14:08:25 -0800799 // delete interface
800 crow::connections::systemBus->async_method_call(
801 [response, path](const boost::system::error_code ec) {
802 if (ec)
803 {
804 BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
805 messages::internalError(response->res);
James Feistb6baeaa2019-02-21 10:41:40 -0800806 return;
James Feist5f2caae2018-12-12 14:08:25 -0800807 }
James Feistb6baeaa2019-02-21 10:41:40 -0800808 messages::success(response->res);
James Feist5f2caae2018-12-12 14:08:25 -0800809 },
810 "xyz.openbmc_project.EntityManager", path, iface, "Delete");
811 return CreatePIDRet::del;
812 }
813
James Feist73df0db2019-03-25 15:29:35 -0700814 const dbus::utility::ManagedItem* managedItem = nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800815 if (!createNewObject)
816 {
817 // if we aren't creating a new object, we should be able to find it on
818 // d-bus
James Feist73df0db2019-03-25 15:29:35 -0700819 managedItem = findChassis(managedObj, it.key(), chassis);
820 if (managedItem == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800821 {
822 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
823 messages::invalidObject(response->res, it.key());
824 return CreatePIDRet::fail;
825 }
826 }
827
James Feist73df0db2019-03-25 15:29:35 -0700828 if (profile.size() &&
829 (type == "PidControllers" || type == "FanControllers" ||
830 type == "StepwiseControllers"))
831 {
832 if (managedItem == nullptr)
833 {
834 output["Profiles"] = std::vector<std::string>{profile};
835 }
836 else
837 {
838 std::string interface;
839 if (type == "StepwiseControllers")
840 {
841 interface = stepwiseConfigurationIface;
842 }
843 else
844 {
845 interface = pidConfigurationIface;
846 }
847 auto findConfig = managedItem->second.find(interface);
848 if (findConfig == managedItem->second.end())
849 {
850 BMCWEB_LOG_ERROR
851 << "Failed to find interface in managed object";
852 messages::internalError(response->res);
853 return CreatePIDRet::fail;
854 }
855 auto findProfiles = findConfig->second.find("Profiles");
856 if (findProfiles != findConfig->second.end())
857 {
858 const std::vector<std::string>* curProfiles =
859 std::get_if<std::vector<std::string>>(
860 &(findProfiles->second));
861 if (curProfiles == nullptr)
862 {
863 BMCWEB_LOG_ERROR << "Illegal profiles in managed object";
864 messages::internalError(response->res);
865 return CreatePIDRet::fail;
866 }
867 if (std::find(curProfiles->begin(), curProfiles->end(),
868 profile) == curProfiles->end())
869 {
870 std::vector<std::string> newProfiles = *curProfiles;
871 newProfiles.push_back(profile);
872 output["Profiles"] = newProfiles;
873 }
874 }
875 }
876 }
877
James Feist83ff9ab2018-08-31 10:18:24 -0700878 if (type == "PidControllers" || type == "FanControllers")
879 {
880 if (createNewObject)
881 {
882 output["Class"] = type == "PidControllers" ? std::string("temp")
883 : std::string("fan");
884 output["Type"] = std::string("Pid");
885 }
James Feist5f2caae2018-12-12 14:08:25 -0800886
887 std::optional<std::vector<nlohmann::json>> zones;
888 std::optional<std::vector<std::string>> inputs;
889 std::optional<std::vector<std::string>> outputs;
890 std::map<std::string, std::optional<double>> doubles;
James Feistb943aae2019-07-11 16:33:56 -0700891 std::optional<std::string> setpointOffset;
James Feist5f2caae2018-12-12 14:08:25 -0800892 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -0800893 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
James Feist5f2caae2018-12-12 14:08:25 -0800894 "Zones", zones, "FFGainCoefficient",
895 doubles["FFGainCoefficient"], "FFOffCoefficient",
896 doubles["FFOffCoefficient"], "ICoefficient",
897 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
898 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
899 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
900 "PCoefficient", doubles["PCoefficient"], "SetPoint",
James Feistb943aae2019-07-11 16:33:56 -0700901 doubles["SetPoint"], "SetPointOffset", setpointOffset,
902 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
903 "PositiveHysteresis", doubles["PositiveHysteresis"],
904 "NegativeHysteresis", doubles["NegativeHysteresis"]))
James Feist83ff9ab2018-08-31 10:18:24 -0700905 {
Ed Tanous71f52d92021-02-19 08:51:17 -0800906 BMCWEB_LOG_ERROR
907 << "Illegal Property "
908 << it.value().dump(2, ' ', true,
909 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -0800910 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700911 }
James Feist5f2caae2018-12-12 14:08:25 -0800912 if (zones)
James Feist83ff9ab2018-08-31 10:18:24 -0700913 {
James Feist5f2caae2018-12-12 14:08:25 -0800914 std::vector<std::string> zonesStr;
915 if (!getZonesFromJsonReq(response, *zones, zonesStr))
James Feist83ff9ab2018-08-31 10:18:24 -0700916 {
Gunnar Millsa0744d32020-11-09 15:40:45 -0600917 BMCWEB_LOG_ERROR << "Illegal Zones";
James Feist5f2caae2018-12-12 14:08:25 -0800918 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700919 }
James Feistb6baeaa2019-02-21 10:41:40 -0800920 if (chassis.empty() &&
921 !findChassis(managedObj, zonesStr[0], chassis))
922 {
923 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
924 messages::invalidObject(response->res, it.key());
925 return CreatePIDRet::fail;
926 }
927
James Feist5f2caae2018-12-12 14:08:25 -0800928 output["Zones"] = std::move(zonesStr);
929 }
930 if (inputs || outputs)
931 {
932 std::array<std::optional<std::vector<std::string>>*, 2> containers =
933 {&inputs, &outputs};
934 size_t index = 0;
935 for (const auto& containerPtr : containers)
James Feist83ff9ab2018-08-31 10:18:24 -0700936 {
James Feist5f2caae2018-12-12 14:08:25 -0800937 std::optional<std::vector<std::string>>& container =
938 *containerPtr;
939 if (!container)
James Feist83ff9ab2018-08-31 10:18:24 -0700940 {
James Feist5f2caae2018-12-12 14:08:25 -0800941 index++;
942 continue;
James Feist83ff9ab2018-08-31 10:18:24 -0700943 }
James Feist5f2caae2018-12-12 14:08:25 -0800944
945 for (std::string& value : *container)
James Feist83ff9ab2018-08-31 10:18:24 -0700946 {
James Feist5f2caae2018-12-12 14:08:25 -0800947 boost::replace_all(value, "_", " ");
James Feist83ff9ab2018-08-31 10:18:24 -0700948 }
James Feist5f2caae2018-12-12 14:08:25 -0800949 std::string key;
950 if (index == 0)
James Feist83ff9ab2018-08-31 10:18:24 -0700951 {
James Feist5f2caae2018-12-12 14:08:25 -0800952 key = "Inputs";
James Feist83ff9ab2018-08-31 10:18:24 -0700953 }
James Feist5f2caae2018-12-12 14:08:25 -0800954 else
955 {
956 key = "Outputs";
957 }
958 output[key] = *container;
959 index++;
James Feist83ff9ab2018-08-31 10:18:24 -0700960 }
James Feist5f2caae2018-12-12 14:08:25 -0800961 }
James Feist83ff9ab2018-08-31 10:18:24 -0700962
James Feistb943aae2019-07-11 16:33:56 -0700963 if (setpointOffset)
964 {
965 // translate between redfish and dbus names
966 if (*setpointOffset == "UpperThresholdNonCritical")
967 {
968 output["SetPointOffset"] = std::string("WarningLow");
969 }
970 else if (*setpointOffset == "LowerThresholdNonCritical")
971 {
972 output["SetPointOffset"] = std::string("WarningHigh");
973 }
974 else if (*setpointOffset == "LowerThresholdCritical")
975 {
976 output["SetPointOffset"] = std::string("CriticalLow");
977 }
978 else if (*setpointOffset == "UpperThresholdCritical")
979 {
980 output["SetPointOffset"] = std::string("CriticalHigh");
981 }
982 else
983 {
984 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
985 << *setpointOffset;
986 messages::invalidObject(response->res, it.key());
987 return CreatePIDRet::fail;
988 }
989 }
990
James Feist5f2caae2018-12-12 14:08:25 -0800991 // doubles
992 for (const auto& pairs : doubles)
993 {
994 if (!pairs.second)
James Feist83ff9ab2018-08-31 10:18:24 -0700995 {
James Feist5f2caae2018-12-12 14:08:25 -0800996 continue;
James Feist83ff9ab2018-08-31 10:18:24 -0700997 }
James Feist5f2caae2018-12-12 14:08:25 -0800998 BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
999 output[pairs.first] = *(pairs.second);
James Feist83ff9ab2018-08-31 10:18:24 -07001000 }
1001 }
James Feist5f2caae2018-12-12 14:08:25 -08001002
James Feist83ff9ab2018-08-31 10:18:24 -07001003 else if (type == "FanZones")
1004 {
James Feist83ff9ab2018-08-31 10:18:24 -07001005 output["Type"] = std::string("Pid.Zone");
1006
James Feist5f2caae2018-12-12 14:08:25 -08001007 std::optional<nlohmann::json> chassisContainer;
1008 std::optional<double> failSafePercent;
James Feistd3ec07f2019-02-25 14:51:15 -08001009 std::optional<double> minThermalOutput;
James Feistb6baeaa2019-02-21 10:41:40 -08001010 if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
James Feist5f2caae2018-12-12 14:08:25 -08001011 chassisContainer, "FailSafePercent",
James Feistd3ec07f2019-02-25 14:51:15 -08001012 failSafePercent, "MinThermalOutput",
1013 minThermalOutput))
James Feist83ff9ab2018-08-31 10:18:24 -07001014 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001015 BMCWEB_LOG_ERROR
1016 << "Illegal Property "
1017 << it.value().dump(2, ' ', true,
1018 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -08001019 return CreatePIDRet::fail;
1020 }
James Feist83ff9ab2018-08-31 10:18:24 -07001021
James Feist5f2caae2018-12-12 14:08:25 -08001022 if (chassisContainer)
1023 {
1024
1025 std::string chassisId;
1026 if (!redfish::json_util::readJson(*chassisContainer, response->res,
1027 "@odata.id", chassisId))
James Feist83ff9ab2018-08-31 10:18:24 -07001028 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001029 BMCWEB_LOG_ERROR
1030 << "Illegal Property "
1031 << chassisContainer->dump(
1032 2, ' ', true,
1033 nlohmann::json::error_handler_t::replace);
James Feist83ff9ab2018-08-31 10:18:24 -07001034 return CreatePIDRet::fail;
1035 }
James Feist5f2caae2018-12-12 14:08:25 -08001036
AppaRao Puli717794d2019-10-18 22:54:53 +05301037 // /redfish/v1/chassis/chassis_name/
James Feist5f2caae2018-12-12 14:08:25 -08001038 if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
1039 {
1040 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
1041 messages::invalidObject(response->res, chassisId);
1042 return CreatePIDRet::fail;
1043 }
1044 }
James Feistd3ec07f2019-02-25 14:51:15 -08001045 if (minThermalOutput)
James Feist5f2caae2018-12-12 14:08:25 -08001046 {
James Feistd3ec07f2019-02-25 14:51:15 -08001047 output["MinThermalOutput"] = *minThermalOutput;
James Feist5f2caae2018-12-12 14:08:25 -08001048 }
1049 if (failSafePercent)
1050 {
1051 output["FailSafePercent"] = *failSafePercent;
1052 }
1053 }
1054 else if (type == "StepwiseControllers")
1055 {
1056 output["Type"] = std::string("Stepwise");
1057
1058 std::optional<std::vector<nlohmann::json>> zones;
1059 std::optional<std::vector<nlohmann::json>> steps;
1060 std::optional<std::vector<std::string>> inputs;
1061 std::optional<double> positiveHysteresis;
1062 std::optional<double> negativeHysteresis;
James Feistc33a90e2019-03-01 10:17:44 -08001063 std::optional<std::string> direction; // upper clipping curve vs lower
James Feist5f2caae2018-12-12 14:08:25 -08001064 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -08001065 it.value(), response->res, "Zones", zones, "Steps", steps,
1066 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
James Feistc33a90e2019-03-01 10:17:44 -08001067 "NegativeHysteresis", negativeHysteresis, "Direction",
1068 direction))
James Feist5f2caae2018-12-12 14:08:25 -08001069 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001070 BMCWEB_LOG_ERROR
1071 << "Illegal Property "
1072 << it.value().dump(2, ' ', true,
1073 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -08001074 return CreatePIDRet::fail;
1075 }
1076
1077 if (zones)
1078 {
James Feistb6baeaa2019-02-21 10:41:40 -08001079 std::vector<std::string> zonesStrs;
1080 if (!getZonesFromJsonReq(response, *zones, zonesStrs))
James Feist5f2caae2018-12-12 14:08:25 -08001081 {
Gunnar Millsa0744d32020-11-09 15:40:45 -06001082 BMCWEB_LOG_ERROR << "Illegal Zones";
James Feist5f2caae2018-12-12 14:08:25 -08001083 return CreatePIDRet::fail;
1084 }
James Feistb6baeaa2019-02-21 10:41:40 -08001085 if (chassis.empty() &&
1086 !findChassis(managedObj, zonesStrs[0], chassis))
1087 {
1088 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
1089 messages::invalidObject(response->res, it.key());
1090 return CreatePIDRet::fail;
1091 }
1092 output["Zones"] = std::move(zonesStrs);
James Feist5f2caae2018-12-12 14:08:25 -08001093 }
1094 if (steps)
1095 {
1096 std::vector<double> readings;
1097 std::vector<double> outputs;
1098 for (auto& step : *steps)
1099 {
1100 double target;
Ed Tanous23a21a12020-07-25 04:45:05 +00001101 double out;
James Feist5f2caae2018-12-12 14:08:25 -08001102
1103 if (!redfish::json_util::readJson(step, response->res, "Target",
Ed Tanous23a21a12020-07-25 04:45:05 +00001104 target, "Output", out))
James Feist5f2caae2018-12-12 14:08:25 -08001105 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001106 BMCWEB_LOG_ERROR
1107 << "Illegal Property "
1108 << it.value().dump(
1109 2, ' ', true,
1110 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -08001111 return CreatePIDRet::fail;
1112 }
1113 readings.emplace_back(target);
Ed Tanous23a21a12020-07-25 04:45:05 +00001114 outputs.emplace_back(out);
James Feist5f2caae2018-12-12 14:08:25 -08001115 }
1116 output["Reading"] = std::move(readings);
1117 output["Output"] = std::move(outputs);
1118 }
1119 if (inputs)
1120 {
1121 for (std::string& value : *inputs)
1122 {
James Feist5f2caae2018-12-12 14:08:25 -08001123 boost::replace_all(value, "_", " ");
1124 }
1125 output["Inputs"] = std::move(*inputs);
1126 }
1127 if (negativeHysteresis)
1128 {
1129 output["NegativeHysteresis"] = *negativeHysteresis;
1130 }
1131 if (positiveHysteresis)
1132 {
1133 output["PositiveHysteresis"] = *positiveHysteresis;
James Feist83ff9ab2018-08-31 10:18:24 -07001134 }
James Feistc33a90e2019-03-01 10:17:44 -08001135 if (direction)
1136 {
1137 constexpr const std::array<const char*, 2> allowedDirections = {
1138 "Ceiling", "Floor"};
1139 if (std::find(allowedDirections.begin(), allowedDirections.end(),
1140 *direction) == allowedDirections.end())
1141 {
1142 messages::propertyValueTypeError(response->res, "Direction",
1143 *direction);
1144 return CreatePIDRet::fail;
1145 }
1146 output["Class"] = *direction;
1147 }
James Feist83ff9ab2018-08-31 10:18:24 -07001148 }
1149 else
1150 {
Gunnar Millsa0744d32020-11-09 15:40:45 -06001151 BMCWEB_LOG_ERROR << "Illegal Type " << type;
Jason M. Bills35a62c72018-10-09 12:45:45 -07001152 messages::propertyUnknown(response->res, type);
James Feist83ff9ab2018-08-31 10:18:24 -07001153 return CreatePIDRet::fail;
1154 }
1155 return CreatePIDRet::patch;
1156}
James Feist73df0db2019-03-25 15:29:35 -07001157struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
1158{
1159
zhanghch058d1b46d2021-04-01 11:18:24 +08001160 GetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
Ed Tanous23a21a12020-07-25 04:45:05 +00001161 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001162
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001163 {}
James Feist73df0db2019-03-25 15:29:35 -07001164
1165 void run()
1166 {
1167 std::shared_ptr<GetPIDValues> self = shared_from_this();
1168
1169 // get all configurations
1170 crow::connections::systemBus->async_method_call(
1171 [self](const boost::system::error_code ec,
Ed Tanous23a21a12020-07-25 04:45:05 +00001172 const crow::openbmc_mapper::GetSubTreeType& subtreeLocal) {
James Feist73df0db2019-03-25 15:29:35 -07001173 if (ec)
1174 {
1175 BMCWEB_LOG_ERROR << ec;
1176 messages::internalError(self->asyncResp->res);
1177 return;
1178 }
Ed Tanous23a21a12020-07-25 04:45:05 +00001179 self->subtree = subtreeLocal;
James Feist73df0db2019-03-25 15:29:35 -07001180 },
1181 "xyz.openbmc_project.ObjectMapper",
1182 "/xyz/openbmc_project/object_mapper",
1183 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
1184 std::array<const char*, 4>{
1185 pidConfigurationIface, pidZoneConfigurationIface,
1186 objectManagerIface, stepwiseConfigurationIface});
1187
1188 // at the same time get the selected profile
1189 crow::connections::systemBus->async_method_call(
1190 [self](const boost::system::error_code ec,
Ed Tanous23a21a12020-07-25 04:45:05 +00001191 const crow::openbmc_mapper::GetSubTreeType& subtreeLocal) {
1192 if (ec || subtreeLocal.empty())
James Feist73df0db2019-03-25 15:29:35 -07001193 {
1194 return;
1195 }
Ed Tanous23a21a12020-07-25 04:45:05 +00001196 if (subtreeLocal[0].second.size() != 1)
James Feist73df0db2019-03-25 15:29:35 -07001197 {
1198 // invalid mapper response, should never happen
1199 BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
1200 messages::internalError(self->asyncResp->res);
1201 return;
1202 }
1203
Ed Tanous23a21a12020-07-25 04:45:05 +00001204 const std::string& path = subtreeLocal[0].first;
1205 const std::string& owner = subtreeLocal[0].second[0].first;
James Feist73df0db2019-03-25 15:29:35 -07001206 crow::connections::systemBus->async_method_call(
1207 [path, owner, self](
Ed Tanous23a21a12020-07-25 04:45:05 +00001208 const boost::system::error_code ec2,
James Feist73df0db2019-03-25 15:29:35 -07001209 const boost::container::flat_map<
1210 std::string, std::variant<std::vector<std::string>,
1211 std::string>>& resp) {
Ed Tanous23a21a12020-07-25 04:45:05 +00001212 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001213 {
1214 BMCWEB_LOG_ERROR << "GetPIDValues: Can't get "
1215 "thermalModeIface "
1216 << path;
1217 messages::internalError(self->asyncResp->res);
1218 return;
1219 }
Ed Tanous271584a2019-07-09 16:24:22 -07001220 const std::string* current = nullptr;
1221 const std::vector<std::string>* supported = nullptr;
James Feist73df0db2019-03-25 15:29:35 -07001222 for (auto& [key, value] : resp)
1223 {
1224 if (key == "Current")
1225 {
1226 current = std::get_if<std::string>(&value);
1227 if (current == nullptr)
1228 {
1229 BMCWEB_LOG_ERROR
1230 << "GetPIDValues: thermal mode "
1231 "iface invalid "
1232 << path;
1233 messages::internalError(
1234 self->asyncResp->res);
1235 return;
1236 }
1237 }
1238 if (key == "Supported")
1239 {
1240 supported =
1241 std::get_if<std::vector<std::string>>(
1242 &value);
1243 if (supported == nullptr)
1244 {
1245 BMCWEB_LOG_ERROR
1246 << "GetPIDValues: thermal mode "
1247 "iface invalid"
1248 << path;
1249 messages::internalError(
1250 self->asyncResp->res);
1251 return;
1252 }
1253 }
1254 }
1255 if (current == nullptr || supported == nullptr)
1256 {
1257 BMCWEB_LOG_ERROR << "GetPIDValues: thermal mode "
1258 "iface invalid "
1259 << path;
1260 messages::internalError(self->asyncResp->res);
1261 return;
1262 }
1263 self->currentProfile = *current;
1264 self->supportedProfiles = *supported;
1265 },
1266 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
1267 thermalModeIface);
1268 },
1269 "xyz.openbmc_project.ObjectMapper",
1270 "/xyz/openbmc_project/object_mapper",
1271 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
1272 std::array<const char*, 1>{thermalModeIface});
1273 }
1274
1275 ~GetPIDValues()
1276 {
1277 if (asyncResp->res.result() != boost::beast::http::status::ok)
1278 {
1279 return;
1280 }
1281 // create map of <connection, path to objMgr>>
1282 boost::container::flat_map<std::string, std::string> objectMgrPaths;
1283 boost::container::flat_set<std::string> calledConnections;
1284 for (const auto& pathGroup : subtree)
1285 {
1286 for (const auto& connectionGroup : pathGroup.second)
1287 {
1288 auto findConnection =
1289 calledConnections.find(connectionGroup.first);
1290 if (findConnection != calledConnections.end())
1291 {
1292 break;
1293 }
1294 for (const std::string& interface : connectionGroup.second)
1295 {
1296 if (interface == objectManagerIface)
1297 {
1298 objectMgrPaths[connectionGroup.first] = pathGroup.first;
1299 }
1300 // this list is alphabetical, so we
1301 // should have found the objMgr by now
1302 if (interface == pidConfigurationIface ||
1303 interface == pidZoneConfigurationIface ||
1304 interface == stepwiseConfigurationIface)
1305 {
1306 auto findObjMgr =
1307 objectMgrPaths.find(connectionGroup.first);
1308 if (findObjMgr == objectMgrPaths.end())
1309 {
1310 BMCWEB_LOG_DEBUG << connectionGroup.first
1311 << "Has no Object Manager";
1312 continue;
1313 }
1314
1315 calledConnections.insert(connectionGroup.first);
1316
1317 asyncPopulatePid(findObjMgr->first, findObjMgr->second,
1318 currentProfile, supportedProfiles,
1319 asyncResp);
1320 break;
1321 }
1322 }
1323 }
1324 }
1325 }
1326
1327 std::vector<std::string> supportedProfiles;
1328 std::string currentProfile;
1329 crow::openbmc_mapper::GetSubTreeType subtree;
zhanghch058d1b46d2021-04-01 11:18:24 +08001330 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001331};
1332
1333struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
1334{
1335
zhanghch058d1b46d2021-04-01 11:18:24 +08001336 SetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
James Feist73df0db2019-03-25 15:29:35 -07001337 nlohmann::json& data) :
Ed Tanous271584a2019-07-09 16:24:22 -07001338 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001339 {
1340
1341 std::optional<nlohmann::json> pidControllers;
1342 std::optional<nlohmann::json> fanControllers;
1343 std::optional<nlohmann::json> fanZones;
1344 std::optional<nlohmann::json> stepwiseControllers;
1345
1346 if (!redfish::json_util::readJson(
1347 data, asyncResp->res, "PidControllers", pidControllers,
1348 "FanControllers", fanControllers, "FanZones", fanZones,
1349 "StepwiseControllers", stepwiseControllers, "Profile", profile))
1350 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001351 BMCWEB_LOG_ERROR
1352 << "Illegal Property "
1353 << data.dump(2, ' ', true,
1354 nlohmann::json::error_handler_t::replace);
James Feist73df0db2019-03-25 15:29:35 -07001355 return;
1356 }
1357 configuration.emplace_back("PidControllers", std::move(pidControllers));
1358 configuration.emplace_back("FanControllers", std::move(fanControllers));
1359 configuration.emplace_back("FanZones", std::move(fanZones));
1360 configuration.emplace_back("StepwiseControllers",
1361 std::move(stepwiseControllers));
1362 }
1363 void run()
1364 {
1365 if (asyncResp->res.result() != boost::beast::http::status::ok)
1366 {
1367 return;
1368 }
1369
1370 std::shared_ptr<SetPIDValues> self = shared_from_this();
1371
1372 // todo(james): might make sense to do a mapper call here if this
1373 // interface gets more traction
1374 crow::connections::systemBus->async_method_call(
1375 [self](const boost::system::error_code ec,
Ed Tanous271584a2019-07-09 16:24:22 -07001376 dbus::utility::ManagedObjectType& mObj) {
James Feist73df0db2019-03-25 15:29:35 -07001377 if (ec)
1378 {
1379 BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
1380 messages::internalError(self->asyncResp->res);
1381 return;
1382 }
James Feiste69d9de2020-02-07 12:23:27 -08001383 const std::array<const char*, 3> configurations = {
1384 pidConfigurationIface, pidZoneConfigurationIface,
1385 stepwiseConfigurationIface};
1386
James Feist14b0b8d2020-02-12 11:52:07 -08001387 for (const auto& [path, object] : mObj)
James Feiste69d9de2020-02-07 12:23:27 -08001388 {
James Feist14b0b8d2020-02-12 11:52:07 -08001389 for (const auto& [interface, _] : object)
James Feiste69d9de2020-02-07 12:23:27 -08001390 {
1391 if (std::find(configurations.begin(),
1392 configurations.end(),
1393 interface) != configurations.end())
1394 {
James Feist14b0b8d2020-02-12 11:52:07 -08001395 self->objectCount++;
James Feiste69d9de2020-02-07 12:23:27 -08001396 break;
1397 }
1398 }
James Feiste69d9de2020-02-07 12:23:27 -08001399 }
Ed Tanous271584a2019-07-09 16:24:22 -07001400 self->managedObj = std::move(mObj);
James Feist73df0db2019-03-25 15:29:35 -07001401 },
1402 "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
1403 "GetManagedObjects");
1404
1405 // at the same time get the profile information
1406 crow::connections::systemBus->async_method_call(
1407 [self](const boost::system::error_code ec,
1408 const crow::openbmc_mapper::GetSubTreeType& subtree) {
1409 if (ec || subtree.empty())
1410 {
1411 return;
1412 }
1413 if (subtree[0].second.empty())
1414 {
1415 // invalid mapper response, should never happen
1416 BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
1417 messages::internalError(self->asyncResp->res);
1418 return;
1419 }
1420
1421 const std::string& path = subtree[0].first;
1422 const std::string& owner = subtree[0].second[0].first;
1423 crow::connections::systemBus->async_method_call(
1424 [self, path, owner](
Ed Tanouscb13a392020-07-25 19:02:03 +00001425 const boost::system::error_code ec2,
James Feist73df0db2019-03-25 15:29:35 -07001426 const boost::container::flat_map<
1427 std::string, std::variant<std::vector<std::string>,
Ed Tanous271584a2019-07-09 16:24:22 -07001428 std::string>>& r) {
Ed Tanouscb13a392020-07-25 19:02:03 +00001429 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001430 {
1431 BMCWEB_LOG_ERROR << "SetPIDValues: Can't get "
1432 "thermalModeIface "
1433 << path;
1434 messages::internalError(self->asyncResp->res);
1435 return;
1436 }
Ed Tanous271584a2019-07-09 16:24:22 -07001437 const std::string* current = nullptr;
1438 const std::vector<std::string>* supported = nullptr;
1439 for (auto& [key, value] : r)
James Feist73df0db2019-03-25 15:29:35 -07001440 {
1441 if (key == "Current")
1442 {
1443 current = std::get_if<std::string>(&value);
1444 if (current == nullptr)
1445 {
1446 BMCWEB_LOG_ERROR
1447 << "SetPIDValues: thermal mode "
1448 "iface invalid "
1449 << path;
1450 messages::internalError(
1451 self->asyncResp->res);
1452 return;
1453 }
1454 }
1455 if (key == "Supported")
1456 {
1457 supported =
1458 std::get_if<std::vector<std::string>>(
1459 &value);
1460 if (supported == nullptr)
1461 {
1462 BMCWEB_LOG_ERROR
1463 << "SetPIDValues: thermal mode "
1464 "iface invalid"
1465 << path;
1466 messages::internalError(
1467 self->asyncResp->res);
1468 return;
1469 }
1470 }
1471 }
1472 if (current == nullptr || supported == nullptr)
1473 {
1474 BMCWEB_LOG_ERROR << "SetPIDValues: thermal mode "
1475 "iface invalid "
1476 << path;
1477 messages::internalError(self->asyncResp->res);
1478 return;
1479 }
1480 self->currentProfile = *current;
1481 self->supportedProfiles = *supported;
1482 self->profileConnection = owner;
1483 self->profilePath = path;
1484 },
1485 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
1486 thermalModeIface);
1487 },
1488 "xyz.openbmc_project.ObjectMapper",
1489 "/xyz/openbmc_project/object_mapper",
1490 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
1491 std::array<const char*, 1>{thermalModeIface});
1492 }
1493 ~SetPIDValues()
1494 {
1495 if (asyncResp->res.result() != boost::beast::http::status::ok)
1496 {
1497 return;
1498 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001499 std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001500 if (profile)
1501 {
1502 if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
1503 *profile) == supportedProfiles.end())
1504 {
1505 messages::actionParameterUnknown(response->res, "Profile",
1506 *profile);
1507 return;
1508 }
1509 currentProfile = *profile;
1510 crow::connections::systemBus->async_method_call(
1511 [response](const boost::system::error_code ec) {
1512 if (ec)
1513 {
1514 BMCWEB_LOG_ERROR << "Error patching profile" << ec;
1515 messages::internalError(response->res);
1516 }
1517 },
1518 profileConnection, profilePath,
1519 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
1520 "Current", std::variant<std::string>(*profile));
1521 }
1522
1523 for (auto& containerPair : configuration)
1524 {
1525 auto& container = containerPair.second;
1526 if (!container)
1527 {
1528 continue;
1529 }
James Feist6ee7f772020-02-06 16:25:27 -08001530 BMCWEB_LOG_DEBUG << *container;
1531
James Feist73df0db2019-03-25 15:29:35 -07001532 std::string& type = containerPair.first;
1533
1534 for (nlohmann::json::iterator it = container->begin();
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301535 it != container->end(); ++it)
James Feist73df0db2019-03-25 15:29:35 -07001536 {
1537 const auto& name = it.key();
James Feist6ee7f772020-02-06 16:25:27 -08001538 BMCWEB_LOG_DEBUG << "looking for " << name;
1539
James Feist73df0db2019-03-25 15:29:35 -07001540 auto pathItr =
1541 std::find_if(managedObj.begin(), managedObj.end(),
1542 [&name](const auto& obj) {
1543 return boost::algorithm::ends_with(
1544 obj.first.str, "/" + name);
1545 });
1546 boost::container::flat_map<std::string,
1547 dbus::utility::DbusVariantType>
1548 output;
1549
1550 output.reserve(16); // The pid interface length
1551
1552 // determines if we're patching entity-manager or
1553 // creating a new object
1554 bool createNewObject = (pathItr == managedObj.end());
James Feist6ee7f772020-02-06 16:25:27 -08001555 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
1556
James Feist73df0db2019-03-25 15:29:35 -07001557 std::string iface;
1558 if (type == "PidControllers" || type == "FanControllers")
1559 {
1560 iface = pidConfigurationIface;
1561 if (!createNewObject &&
1562 pathItr->second.find(pidConfigurationIface) ==
1563 pathItr->second.end())
1564 {
1565 createNewObject = true;
1566 }
1567 }
1568 else if (type == "FanZones")
1569 {
1570 iface = pidZoneConfigurationIface;
1571 if (!createNewObject &&
1572 pathItr->second.find(pidZoneConfigurationIface) ==
1573 pathItr->second.end())
1574 {
1575
1576 createNewObject = true;
1577 }
1578 }
1579 else if (type == "StepwiseControllers")
1580 {
1581 iface = stepwiseConfigurationIface;
1582 if (!createNewObject &&
1583 pathItr->second.find(stepwiseConfigurationIface) ==
1584 pathItr->second.end())
1585 {
1586 createNewObject = true;
1587 }
1588 }
James Feist6ee7f772020-02-06 16:25:27 -08001589
1590 if (createNewObject && it.value() == nullptr)
1591 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -05001592 // can't delete a non-existent object
James Feist6ee7f772020-02-06 16:25:27 -08001593 messages::invalidObject(response->res, name);
1594 continue;
1595 }
1596
1597 std::string path;
1598 if (pathItr != managedObj.end())
1599 {
1600 path = pathItr->first.str;
1601 }
1602
James Feist73df0db2019-03-25 15:29:35 -07001603 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
James Feiste69d9de2020-02-07 12:23:27 -08001604
1605 // arbitrary limit to avoid attacks
1606 constexpr const size_t controllerLimit = 500;
James Feist14b0b8d2020-02-12 11:52:07 -08001607 if (createNewObject && objectCount >= controllerLimit)
James Feiste69d9de2020-02-07 12:23:27 -08001608 {
1609 messages::resourceExhaustion(response->res, type);
1610 continue;
1611 }
1612
James Feist73df0db2019-03-25 15:29:35 -07001613 output["Name"] = boost::replace_all_copy(name, "_", " ");
1614
1615 std::string chassis;
1616 CreatePIDRet ret = createPidInterface(
James Feist6ee7f772020-02-06 16:25:27 -08001617 response, type, it, path, managedObj, createNewObject,
1618 output, chassis, currentProfile);
James Feist73df0db2019-03-25 15:29:35 -07001619 if (ret == CreatePIDRet::fail)
1620 {
1621 return;
1622 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001623 if (ret == CreatePIDRet::del)
James Feist73df0db2019-03-25 15:29:35 -07001624 {
1625 continue;
1626 }
1627
1628 if (!createNewObject)
1629 {
1630 for (const auto& property : output)
1631 {
1632 crow::connections::systemBus->async_method_call(
1633 [response,
1634 propertyName{std::string(property.first)}](
1635 const boost::system::error_code ec) {
1636 if (ec)
1637 {
1638 BMCWEB_LOG_ERROR << "Error patching "
1639 << propertyName << ": "
1640 << ec;
1641 messages::internalError(response->res);
1642 return;
1643 }
1644 messages::success(response->res);
1645 },
James Feist6ee7f772020-02-06 16:25:27 -08001646 "xyz.openbmc_project.EntityManager", path,
James Feist73df0db2019-03-25 15:29:35 -07001647 "org.freedesktop.DBus.Properties", "Set", iface,
1648 property.first, property.second);
1649 }
1650 }
1651 else
1652 {
1653 if (chassis.empty())
1654 {
1655 BMCWEB_LOG_ERROR << "Failed to get chassis from config";
1656 messages::invalidObject(response->res, name);
1657 return;
1658 }
1659
1660 bool foundChassis = false;
1661 for (const auto& obj : managedObj)
1662 {
1663 if (boost::algorithm::ends_with(obj.first.str, chassis))
1664 {
1665 chassis = obj.first.str;
1666 foundChassis = true;
1667 break;
1668 }
1669 }
1670 if (!foundChassis)
1671 {
1672 BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
1673 messages::resourceMissingAtURI(
1674 response->res, "/redfish/v1/Chassis/" + chassis);
1675 return;
1676 }
1677
1678 crow::connections::systemBus->async_method_call(
1679 [response](const boost::system::error_code ec) {
1680 if (ec)
1681 {
1682 BMCWEB_LOG_ERROR << "Error Adding Pid Object "
1683 << ec;
1684 messages::internalError(response->res);
1685 return;
1686 }
1687 messages::success(response->res);
1688 },
1689 "xyz.openbmc_project.EntityManager", chassis,
1690 "xyz.openbmc_project.AddObject", "AddObject", output);
1691 }
1692 }
1693 }
1694 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001695 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001696 std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
1697 configuration;
1698 std::optional<std::string> profile;
1699 dbus::utility::ManagedObjectType managedObj;
1700 std::vector<std::string> supportedProfiles;
1701 std::string currentProfile;
1702 std::string profileConnection;
1703 std::string profilePath;
James Feist14b0b8d2020-02-12 11:52:07 -08001704 size_t objectCount = 0;
James Feist73df0db2019-03-25 15:29:35 -07001705};
James Feist83ff9ab2018-08-31 10:18:24 -07001706
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001707/**
1708 * @brief Retrieves BMC manager location data over DBus
1709 *
1710 * @param[in] aResp Shared pointer for completing asynchronous calls
1711 * @param[in] connectionName - service name
1712 * @param[in] path - object path
1713 * @return none
1714 */
zhanghch058d1b46d2021-04-01 11:18:24 +08001715inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001716 const std::string& connectionName,
1717 const std::string& path)
1718{
1719 BMCWEB_LOG_DEBUG << "Get BMC manager Location data.";
1720
1721 crow::connections::systemBus->async_method_call(
1722 [aResp](const boost::system::error_code ec,
1723 const std::variant<std::string>& property) {
1724 if (ec)
1725 {
1726 BMCWEB_LOG_DEBUG << "DBUS response error for "
1727 "Location";
1728 messages::internalError(aResp->res);
1729 return;
1730 }
1731
1732 const std::string* value = std::get_if<std::string>(&property);
1733
1734 if (value == nullptr)
1735 {
1736 // illegal value
1737 messages::internalError(aResp->res);
1738 return;
1739 }
1740
1741 aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
1742 *value;
1743 },
1744 connectionName, path, "org.freedesktop.DBus.Properties", "Get",
1745 "xyz.openbmc_project.Inventory.Decorator."
1746 "LocationCode",
1747 "LocationCode");
1748}
1749
Ed Tanous1abe55e2018-09-05 08:30:59 -07001750class Manager : public Node
1751{
1752 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07001753 Manager(App& app) : Node(app, "/redfish/v1/Managers/bmc/")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001754 {
Ed Tanous52cc1122020-07-18 13:51:21 -07001755
1756 uuid = persistent_data::getConfig().systemUuid;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001757 entityPrivileges = {
1758 {boost::beast::http::verb::get, {{"Login"}}},
1759 {boost::beast::http::verb::head, {{"Login"}}},
1760 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1761 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1762 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1763 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001764 }
1765
Ed Tanous1abe55e2018-09-05 08:30:59 -07001766 private:
zhanghch058d1b46d2021-04-01 11:18:24 +08001767 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1768 const crow::Request&, const std::vector<std::string>&) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07001769 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001770 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
1771 asyncResp->res.jsonValue["@odata.type"] = "#Manager.v1_11_0.Manager";
1772 asyncResp->res.jsonValue["Id"] = "bmc";
1773 asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
1774 asyncResp->res.jsonValue["Description"] =
1775 "Baseboard Management Controller";
1776 asyncResp->res.jsonValue["PowerState"] = "On";
1777 asyncResp->res.jsonValue["Status"] = {{"State", "Enabled"},
1778 {"Health", "OK"}};
1779 asyncResp->res.jsonValue["ManagerType"] = "BMC";
1780 asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
1781 asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1782 asyncResp->res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
Ed Tanous0f74e642018-11-12 15:17:05 -08001783
zhanghch058d1b46d2021-04-01 11:18:24 +08001784 asyncResp->res.jsonValue["LogServices"] = {
Ed Tanous0f74e642018-11-12 15:17:05 -08001785 {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}};
1786
zhanghch058d1b46d2021-04-01 11:18:24 +08001787 asyncResp->res.jsonValue["NetworkProtocol"] = {
Ed Tanous0f74e642018-11-12 15:17:05 -08001788 {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}};
1789
zhanghch058d1b46d2021-04-01 11:18:24 +08001790 asyncResp->res.jsonValue["EthernetInterfaces"] = {
Ed Tanous0f74e642018-11-12 15:17:05 -08001791 {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}};
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +02001792
1793#ifdef BMCWEB_ENABLE_VM_NBDPROXY
zhanghch058d1b46d2021-04-01 11:18:24 +08001794 asyncResp->res.jsonValue["VirtualMedia"] = {
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +02001795 {"@odata.id", "/redfish/v1/Managers/bmc/VirtualMedia"}};
1796#endif // BMCWEB_ENABLE_VM_NBDPROXY
1797
Ed Tanous0f74e642018-11-12 15:17:05 -08001798 // default oem data
zhanghch058d1b46d2021-04-01 11:18:24 +08001799 nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
Ed Tanous0f74e642018-11-12 15:17:05 -08001800 nlohmann::json& oemOpenbmc = oem["OpenBmc"];
1801 oem["@odata.type"] = "#OemManager.Oem";
1802 oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
Ed Tanous0f74e642018-11-12 15:17:05 -08001803 oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
1804 oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
Marri Devender Raocfcd5f62019-05-17 08:34:37 -05001805 oemOpenbmc["Certificates"] = {
1806 {"@odata.id", "/redfish/v1/Managers/bmc/Truststore/Certificates"}};
Ed Tanous0f74e642018-11-12 15:17:05 -08001807
Gunnar Mills2a5c4402020-05-19 09:07:24 -05001808 // Manager.Reset (an action) can be many values, OpenBMC only supports
1809 // BMC reboot.
1810 nlohmann::json& managerReset =
zhanghch058d1b46d2021-04-01 11:18:24 +08001811 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
Gunnar Mills2a5c4402020-05-19 09:07:24 -05001812 managerReset["target"] =
Jennifer Leeed5befb2018-08-10 11:29:45 -07001813 "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +05301814 managerReset["@Redfish.ActionInfo"] =
1815 "/redfish/v1/Managers/bmc/ResetActionInfo";
Jennifer Leeca537922018-08-10 10:07:30 -07001816
Gunnar Mills3e40fc72020-05-19 19:18:17 -05001817 // ResetToDefaults (Factory Reset) has values like
1818 // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
1819 // on OpenBMC
1820 nlohmann::json& resetToDefaults =
zhanghch058d1b46d2021-04-01 11:18:24 +08001821 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
Gunnar Mills3e40fc72020-05-19 19:18:17 -05001822 resetToDefaults["target"] =
1823 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
1824 resetToDefaults["ResetType@Redfish.AllowableValues"] = {"ResetAll"};
1825
zhanghch058d1b46d2021-04-01 11:18:24 +08001826 asyncResp->res.jsonValue["DateTime"] = crow::utility::dateTimeNow();
Santosh Puranik474bfad2019-04-02 16:00:09 +05301827
Kuiying Wangf8c3e6f2019-08-22 13:35:56 +08001828 // Fill in SerialConsole info
zhanghch058d1b46d2021-04-01 11:18:24 +08001829 asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
1830 asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
1831 asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] = {
1832 "IPMI", "SSH"};
Santosh Puranikef47bb12019-04-30 10:28:52 +05301833#ifdef BMCWEB_ENABLE_KVM
Kuiying Wangf8c3e6f2019-08-22 13:35:56 +08001834 // Fill in GraphicalConsole info
zhanghch058d1b46d2021-04-01 11:18:24 +08001835 asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
1836 asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] =
1837 4;
1838 asyncResp->res
1839 .jsonValue["GraphicalConsole"]["ConnectTypesSupported"] = {"KVMIP"};
Santosh Puranikef47bb12019-04-30 10:28:52 +05301840#endif // BMCWEB_ENABLE_KVM
Santosh Puranik474bfad2019-04-02 16:00:09 +05301841
zhanghch058d1b46d2021-04-01 11:18:24 +08001842 asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
1843 asyncResp->res.jsonValue["Links"]["ManagerForServers"] = {
Gunnar Mills603a6642019-01-21 17:03:51 -06001844 {{"@odata.id", "/redfish/v1/Systems/system"}}};
Shawn McCarney26f03892019-05-03 13:20:24 -05001845
James Feistb49ac872019-05-21 15:12:01 -07001846 auto health = std::make_shared<HealthPopulate>(asyncResp);
1847 health->isManagersHealth = true;
1848 health->populate();
1849
Gunnar Millsf97ddba2020-08-20 15:57:40 -05001850 fw_util::populateFirmwareInformation(asyncResp, fw_util::bmcPurpose,
1851 "FirmwareVersion", true);
James Feist0f6b00b2019-06-10 14:15:53 -07001852
Gunnar Mills4bf2b032020-06-23 22:28:31 -05001853 getLastResetTime(asyncResp);
1854
James Feist73df0db2019-03-25 15:29:35 -07001855 auto pids = std::make_shared<GetPIDValues>(asyncResp);
1856 pids->run();
Jennifer Leec5d03ff2019-03-08 15:42:58 -08001857
zhanghch058d1b46d2021-04-01 11:18:24 +08001858 getMainChassisId(
1859 asyncResp, [](const std::string& chassisId,
1860 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
1861 aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] =
1862 1;
1863 aRsp->res.jsonValue["Links"]["ManagerForChassis"] = {
1864 {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
1865 aRsp->res.jsonValue["Links"]["ManagerInChassis"] = {
1866 {"@odata.id", "/redfish/v1/Chassis/" + chassisId}};
1867 });
James Feist0f6b00b2019-06-10 14:15:53 -07001868
1869 static bool started = false;
1870
1871 if (!started)
1872 {
1873 crow::connections::systemBus->async_method_call(
1874 [asyncResp](const boost::system::error_code ec,
1875 const std::variant<double>& resp) {
1876 if (ec)
1877 {
1878 BMCWEB_LOG_ERROR << "Error while getting progress";
1879 messages::internalError(asyncResp->res);
1880 return;
1881 }
1882 const double* val = std::get_if<double>(&resp);
1883 if (val == nullptr)
1884 {
1885 BMCWEB_LOG_ERROR
1886 << "Invalid response while getting progress";
1887 messages::internalError(asyncResp->res);
1888 return;
1889 }
1890 if (*val < 1.0)
1891 {
1892 asyncResp->res.jsonValue["Status"]["State"] =
1893 "Starting";
1894 started = true;
1895 }
1896 },
1897 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1898 "org.freedesktop.DBus.Properties", "Get",
1899 "org.freedesktop.systemd1.Manager", "Progress");
1900 }
Chicago Duanef6ca6e2020-11-27 16:23:05 +08001901
1902 crow::connections::systemBus->async_method_call(
1903 [asyncResp](
1904 const boost::system::error_code ec,
1905 const std::vector<std::pair<
1906 std::string, std::vector<std::pair<
1907 std::string, std::vector<std::string>>>>>&
1908 subtree) {
1909 if (ec)
1910 {
1911 BMCWEB_LOG_DEBUG << "D-Bus response error on GetSubTree "
1912 << ec;
1913 return;
1914 }
1915 if (subtree.size() == 0)
1916 {
1917 BMCWEB_LOG_DEBUG << "Can't find bmc D-Bus object!";
1918 return;
1919 }
1920 // Assume only 1 bmc D-Bus object
1921 // Throw an error if there is more than 1
1922 if (subtree.size() > 1)
1923 {
1924 BMCWEB_LOG_DEBUG << "Found more than 1 bmc D-Bus object!";
1925 messages::internalError(asyncResp->res);
1926 return;
1927 }
1928
1929 if (subtree[0].first.empty() || subtree[0].second.size() != 1)
1930 {
1931 BMCWEB_LOG_DEBUG << "Error getting bmc D-Bus object!";
1932 messages::internalError(asyncResp->res);
1933 return;
1934 }
1935
1936 const std::string& path = subtree[0].first;
1937 const std::string& connectionName = subtree[0].second[0].first;
1938
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001939 for (const auto& interfaceName : subtree[0].second[0].second)
1940 {
1941 if (interfaceName ==
1942 "xyz.openbmc_project.Inventory.Decorator.Asset")
1943 {
1944 crow::connections::systemBus->async_method_call(
1945 [asyncResp](
1946 const boost::system::error_code ec,
1947 const std::vector<std::pair<
1948 std::string, std::variant<std::string>>>&
1949 propertiesList) {
1950 if (ec)
Chicago Duanef6ca6e2020-11-27 16:23:05 +08001951 {
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001952 BMCWEB_LOG_DEBUG << "Can't get bmc asset!";
1953 return;
Chicago Duanef6ca6e2020-11-27 16:23:05 +08001954 }
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001955 for (const std::pair<std::string,
1956 std::variant<std::string>>&
1957 property : propertiesList)
1958 {
1959 const std::string& propertyName =
1960 property.first;
1961
1962 if ((propertyName == "PartNumber") ||
1963 (propertyName == "SerialNumber") ||
1964 (propertyName == "Manufacturer") ||
1965 (propertyName == "Model") ||
1966 (propertyName == "SparePartNumber"))
1967 {
1968 const std::string* value =
1969 std::get_if<std::string>(
1970 &property.second);
1971 if (value == nullptr)
1972 {
1973 // illegal property
1974 messages::internalError(
1975 asyncResp->res);
Chicago Duan601af5e2021-04-15 16:59:25 +08001976 return;
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001977 }
1978 asyncResp->res.jsonValue[propertyName] =
1979 *value;
1980 }
1981 }
1982 },
1983 connectionName, path,
1984 "org.freedesktop.DBus.Properties", "GetAll",
1985 "xyz.openbmc_project.Inventory.Decorator.Asset");
1986 }
1987 else if (interfaceName == "xyz.openbmc_project.Inventory."
1988 "Decorator.LocationCode")
1989 {
1990 getLocation(asyncResp, connectionName, path);
1991 }
1992 }
Chicago Duanef6ca6e2020-11-27 16:23:05 +08001993 },
1994 "xyz.openbmc_project.ObjectMapper",
1995 "/xyz/openbmc_project/object_mapper",
1996 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1997 "/xyz/openbmc_project/inventory", int32_t(0),
1998 std::array<const char*, 1>{
1999 "xyz.openbmc_project.Inventory.Item.Bmc"});
James Feist83ff9ab2018-08-31 10:18:24 -07002000 }
James Feist5b4aa862018-08-16 14:07:01 -07002001
zhanghch058d1b46d2021-04-01 11:18:24 +08002002 void doPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2003 const crow::Request& req,
Ed Tanouscb13a392020-07-25 19:02:03 +00002004 const std::vector<std::string>&) override
James Feist5b4aa862018-08-16 14:07:01 -07002005 {
Ed Tanous0627a2c2018-11-29 17:09:23 -08002006 std::optional<nlohmann::json> oem;
Gunnar Mills4bfefa72020-07-30 13:54:29 -05002007 std::optional<nlohmann::json> links;
Santosh Puranikaf5d60582019-03-20 18:16:36 +05302008 std::optional<std::string> datetime;
Ed Tanous0627a2c2018-11-29 17:09:23 -08002009
zhanghch058d1b46d2021-04-01 11:18:24 +08002010 if (!json_util::readJson(req, asyncResp->res, "Oem", oem, "DateTime",
Gunnar Mills4bfefa72020-07-30 13:54:29 -05002011 datetime, "Links", links))
James Feist83ff9ab2018-08-31 10:18:24 -07002012 {
2013 return;
2014 }
Ed Tanous0627a2c2018-11-29 17:09:23 -08002015
Ed Tanous0627a2c2018-11-29 17:09:23 -08002016 if (oem)
James Feist83ff9ab2018-08-31 10:18:24 -07002017 {
Ed Tanous43b761d2019-02-13 20:10:56 -08002018 std::optional<nlohmann::json> openbmc;
zhanghch058d1b46d2021-04-01 11:18:24 +08002019 if (!redfish::json_util::readJson(*oem, asyncResp->res, "OpenBmc",
2020 openbmc))
James Feist83ff9ab2018-08-31 10:18:24 -07002021 {
Ed Tanous71f52d92021-02-19 08:51:17 -08002022 BMCWEB_LOG_ERROR
2023 << "Illegal Property "
2024 << oem->dump(2, ' ', true,
2025 nlohmann::json::error_handler_t::replace);
Ed Tanous43b761d2019-02-13 20:10:56 -08002026 return;
2027 }
2028 if (openbmc)
2029 {
2030 std::optional<nlohmann::json> fan;
zhanghch058d1b46d2021-04-01 11:18:24 +08002031 if (!redfish::json_util::readJson(*openbmc, asyncResp->res,
2032 "Fan", fan))
James Feist83ff9ab2018-08-31 10:18:24 -07002033 {
Ed Tanous71f52d92021-02-19 08:51:17 -08002034 BMCWEB_LOG_ERROR
2035 << "Illegal Property "
2036 << openbmc->dump(
2037 2, ' ', true,
2038 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -08002039 return;
2040 }
Ed Tanous43b761d2019-02-13 20:10:56 -08002041 if (fan)
James Feist5f2caae2018-12-12 14:08:25 -08002042 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002043 auto pid = std::make_shared<SetPIDValues>(asyncResp, *fan);
James Feist73df0db2019-03-25 15:29:35 -07002044 pid->run();
James Feist83ff9ab2018-08-31 10:18:24 -07002045 }
James Feist83ff9ab2018-08-31 10:18:24 -07002046 }
2047 }
Gunnar Mills4bfefa72020-07-30 13:54:29 -05002048 if (links)
2049 {
2050 std::optional<nlohmann::json> activeSoftwareImage;
zhanghch058d1b46d2021-04-01 11:18:24 +08002051 if (!redfish::json_util::readJson(*links, asyncResp->res,
2052 "ActiveSoftwareImage",
2053 activeSoftwareImage))
Gunnar Mills4bfefa72020-07-30 13:54:29 -05002054 {
2055 return;
2056 }
2057 if (activeSoftwareImage)
2058 {
2059 std::optional<std::string> odataId;
zhanghch058d1b46d2021-04-01 11:18:24 +08002060 if (!json_util::readJson(*activeSoftwareImage, asyncResp->res,
2061 "@odata.id", odataId))
Gunnar Mills4bfefa72020-07-30 13:54:29 -05002062 {
2063 return;
2064 }
2065
2066 if (odataId)
2067 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002068 setActiveFirmwareImage(asyncResp, *odataId);
Gunnar Mills4bfefa72020-07-30 13:54:29 -05002069 }
2070 }
2071 }
Santosh Puranikaf5d60582019-03-20 18:16:36 +05302072 if (datetime)
2073 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002074 setDateTime(asyncResp, std::move(*datetime));
Santosh Puranikaf5d60582019-03-20 18:16:36 +05302075 }
2076 }
2077
zhanghch058d1b46d2021-04-01 11:18:24 +08002078 void getLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
Gunnar Mills4bf2b032020-06-23 22:28:31 -05002079 {
2080 BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time";
2081
2082 crow::connections::systemBus->async_method_call(
2083 [aResp](const boost::system::error_code ec,
2084 std::variant<uint64_t>& lastResetTime) {
2085 if (ec)
2086 {
2087 BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
2088 return;
2089 }
2090
2091 const uint64_t* lastResetTimePtr =
2092 std::get_if<uint64_t>(&lastResetTime);
2093
2094 if (!lastResetTimePtr)
2095 {
2096 messages::internalError(aResp->res);
2097 return;
2098 }
2099 // LastRebootTime is epoch time, in milliseconds
2100 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
2101 time_t lastResetTimeStamp =
2102 static_cast<time_t>(*lastResetTimePtr / 1000);
2103
2104 // Convert to ISO 8601 standard
2105 aResp->res.jsonValue["LastResetTime"] =
2106 crow::utility::getDateTime(lastResetTimeStamp);
2107 },
2108 "xyz.openbmc_project.State.BMC", "/xyz/openbmc_project/state/bmc0",
2109 "org.freedesktop.DBus.Properties", "Get",
2110 "xyz.openbmc_project.State.BMC", "LastRebootTime");
2111 }
2112
Gunnar Mills4bfefa72020-07-30 13:54:29 -05002113 /**
2114 * @brief Set the running firmware image
2115 *
2116 * @param[i,o] aResp - Async response object
2117 * @param[i] runningFirmwareTarget - Image to make the running image
2118 *
2119 * @return void
2120 */
zhanghch058d1b46d2021-04-01 11:18:24 +08002121 void setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Ed Tanousf23b7292020-10-15 09:41:17 -07002122 const std::string& runningFirmwareTarget)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05002123 {
2124 // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
Ed Tanousf23b7292020-10-15 09:41:17 -07002125 std::string::size_type idPos = runningFirmwareTarget.rfind('/');
Gunnar Mills4bfefa72020-07-30 13:54:29 -05002126 if (idPos == std::string::npos)
2127 {
2128 messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
2129 "@odata.id");
2130 BMCWEB_LOG_DEBUG << "Can't parse firmware ID!";
2131 return;
2132 }
2133 idPos++;
2134 if (idPos >= runningFirmwareTarget.size())
2135 {
2136 messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
2137 "@odata.id");
2138 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
2139 return;
2140 }
2141 std::string firmwareId = runningFirmwareTarget.substr(idPos);
2142
2143 // Make sure the image is valid before setting priority
2144 crow::connections::systemBus->async_method_call(
2145 [aResp, firmwareId,
2146 runningFirmwareTarget](const boost::system::error_code ec,
2147 ManagedObjectType& subtree) {
2148 if (ec)
2149 {
2150 BMCWEB_LOG_DEBUG << "D-Bus response error getting objects.";
2151 messages::internalError(aResp->res);
2152 return;
2153 }
2154
2155 if (subtree.size() == 0)
2156 {
2157 BMCWEB_LOG_DEBUG << "Can't find image!";
2158 messages::internalError(aResp->res);
2159 return;
2160 }
2161
2162 bool foundImage = false;
2163 for (auto& object : subtree)
2164 {
2165 const std::string& path =
2166 static_cast<const std::string&>(object.first);
Ed Tanousf23b7292020-10-15 09:41:17 -07002167 std::size_t idPos2 = path.rfind('/');
Gunnar Mills4bfefa72020-07-30 13:54:29 -05002168
2169 if (idPos2 == std::string::npos)
2170 {
2171 continue;
2172 }
2173
2174 idPos2++;
2175 if (idPos2 >= path.size())
2176 {
2177 continue;
2178 }
2179
2180 if (path.substr(idPos2) == firmwareId)
2181 {
2182 foundImage = true;
2183 break;
2184 }
2185 }
2186
2187 if (!foundImage)
2188 {
2189 messages::propertyValueNotInList(
2190 aResp->res, runningFirmwareTarget, "@odata.id");
2191 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
2192 return;
2193 }
2194
2195 BMCWEB_LOG_DEBUG << "Setting firmware version " + firmwareId +
2196 " to priority 0.";
2197
2198 // Only support Immediate
2199 // An addition could be a Redfish Setting like
2200 // ActiveSoftwareImageApplyTime and support OnReset
2201 crow::connections::systemBus->async_method_call(
2202 [aResp](const boost::system::error_code ec) {
2203 if (ec)
2204 {
2205 BMCWEB_LOG_DEBUG << "D-Bus response error setting.";
2206 messages::internalError(aResp->res);
2207 return;
2208 }
2209 doBMCGracefulRestart(aResp);
2210 },
2211
2212 "xyz.openbmc_project.Software.BMC.Updater",
2213 "/xyz/openbmc_project/software/" + firmwareId,
2214 "org.freedesktop.DBus.Properties", "Set",
2215 "xyz.openbmc_project.Software.RedundancyPriority",
2216 "Priority", std::variant<uint8_t>(static_cast<uint8_t>(0)));
2217 },
2218 "xyz.openbmc_project.Software.BMC.Updater",
2219 "/xyz/openbmc_project/software",
2220 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
2221 }
2222
zhanghch058d1b46d2021-04-01 11:18:24 +08002223 void setDateTime(std::shared_ptr<bmcweb::AsyncResp> aResp,
Santosh Puranikaf5d60582019-03-20 18:16:36 +05302224 std::string datetime) const
2225 {
2226 BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
2227
2228 std::stringstream stream(datetime);
2229 // Convert from ISO 8601 to boost local_time
2230 // (BMC only has time in UTC)
2231 boost::posix_time::ptime posixTime;
2232 boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
2233 // Facet gets deleted with the stringsteam
2234 auto ifc = std::make_unique<boost::local_time::local_time_input_facet>(
2235 "%Y-%m-%d %H:%M:%S%F %ZP");
2236 stream.imbue(std::locale(stream.getloc(), ifc.release()));
2237
2238 boost::local_time::local_date_time ldt(
2239 boost::local_time::not_a_date_time);
2240
2241 if (stream >> ldt)
2242 {
2243 posixTime = ldt.utc_time();
2244 boost::posix_time::time_duration dur = posixTime - epoch;
2245 uint64_t durMicroSecs =
2246 static_cast<uint64_t>(dur.total_microseconds());
2247 crow::connections::systemBus->async_method_call(
2248 [aResp{std::move(aResp)}, datetime{std::move(datetime)}](
2249 const boost::system::error_code ec) {
2250 if (ec)
2251 {
2252 BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
2253 "DBUS response error "
2254 << ec;
2255 messages::internalError(aResp->res);
2256 return;
2257 }
2258 aResp->res.jsonValue["DateTime"] = datetime;
2259 },
2260 "xyz.openbmc_project.Time.Manager",
2261 "/xyz/openbmc_project/time/bmc",
2262 "org.freedesktop.DBus.Properties", "Set",
2263 "xyz.openbmc_project.Time.EpochTime", "Elapsed",
2264 std::variant<uint64_t>(durMicroSecs));
2265 }
2266 else
2267 {
2268 messages::propertyValueFormatError(aResp->res, datetime,
2269 "DateTime");
2270 return;
2271 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002272 }
2273
Ed Tanous0f74e642018-11-12 15:17:05 -08002274 std::string uuid;
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01002275};
2276
Ed Tanous1abe55e2018-09-05 08:30:59 -07002277class ManagerCollection : public Node
2278{
2279 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002280 ManagerCollection(App& app) : Node(app, "/redfish/v1/Managers/")
Ed Tanous1abe55e2018-09-05 08:30:59 -07002281 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002282 entityPrivileges = {
2283 {boost::beast::http::verb::get, {{"Login"}}},
2284 {boost::beast::http::verb::head, {{"Login"}}},
2285 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2286 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2287 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2288 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2289 }
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01002290
Ed Tanous1abe55e2018-09-05 08:30:59 -07002291 private:
zhanghch058d1b46d2021-04-01 11:18:24 +08002292 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2293 const crow::Request&, const std::vector<std::string>&) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002294 {
James Feist83ff9ab2018-08-31 10:18:24 -07002295 // Collections don't include the static data added by SubRoute
2296 // because it has a duplicate entry for members
zhanghch058d1b46d2021-04-01 11:18:24 +08002297 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
2298 asyncResp->res.jsonValue["@odata.type"] =
2299 "#ManagerCollection.ManagerCollection";
2300 asyncResp->res.jsonValue["Name"] = "Manager Collection";
2301 asyncResp->res.jsonValue["Members@odata.count"] = 1;
2302 asyncResp->res.jsonValue["Members"] = {
James Feist5b4aa862018-08-16 14:07:01 -07002303 {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
Ed Tanous1abe55e2018-09-05 08:30:59 -07002304 }
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01002305};
Ed Tanous1abe55e2018-09-05 08:30:59 -07002306} // namespace redfish