blob: 344ca83292119a928f45c89a916eedad4fbee53f [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 Tanoused398212021-06-09 17:05:54 -070025#include <registries/privilege_registry.hpp>
Andrew Geisslere90c5052019-06-28 13:52:27 -050026#include <utils/fw_utils.hpp>
Bernard Wong7bffdb72019-03-20 16:17:21 +080027#include <utils/systemd_utils.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050028
Gunnar Mills4bfefa72020-07-30 13:54:29 -050029#include <cstdint>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050030#include <memory>
31#include <sstream>
Ed Tanousabf2add2019-01-22 16:40:12 -080032#include <variant>
James Feist5b4aa862018-08-16 14:07:01 -070033
Ed Tanous1abe55e2018-09-05 08:30:59 -070034namespace redfish
35{
Jennifer Leeed5befb2018-08-10 11:29:45 -070036
37/**
Gunnar Mills2a5c4402020-05-19 09:07:24 -050038 * Function reboots the BMC.
39 *
40 * @param[in] asyncResp - Shared pointer for completing asynchronous calls
Jennifer Leeed5befb2018-08-10 11:29:45 -070041 */
zhanghch058d1b46d2021-04-01 11:18:24 +080042inline void
43 doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Gunnar Mills2a5c4402020-05-19 09:07:24 -050044{
45 const char* processName = "xyz.openbmc_project.State.BMC";
46 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
47 const char* interfaceName = "xyz.openbmc_project.State.BMC";
48 const std::string& propertyValue =
49 "xyz.openbmc_project.State.BMC.Transition.Reboot";
50 const char* destProperty = "RequestedBMCTransition";
51
52 // Create the D-Bus variant for D-Bus call.
53 VariantType dbusPropertyValue(propertyValue);
54
55 crow::connections::systemBus->async_method_call(
56 [asyncResp](const boost::system::error_code ec) {
57 // Use "Set" method to set the property value.
58 if (ec)
59 {
60 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
61 messages::internalError(asyncResp->res);
62 return;
63 }
64
65 messages::success(asyncResp->res);
66 },
67 processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
68 interfaceName, destProperty, dbusPropertyValue);
69}
70
zhanghch058d1b46d2021-04-01 11:18:24 +080071inline void
72 doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +000073{
74 const char* processName = "xyz.openbmc_project.State.BMC";
75 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
76 const char* interfaceName = "xyz.openbmc_project.State.BMC";
77 const std::string& propertyValue =
78 "xyz.openbmc_project.State.BMC.Transition.HardReboot";
79 const char* destProperty = "RequestedBMCTransition";
80
81 // Create the D-Bus variant for D-Bus call.
82 VariantType dbusPropertyValue(propertyValue);
83
84 crow::connections::systemBus->async_method_call(
85 [asyncResp](const boost::system::error_code ec) {
86 // Use "Set" method to set the property value.
87 if (ec)
88 {
89 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
90 messages::internalError(asyncResp->res);
91 return;
92 }
93
94 messages::success(asyncResp->res);
95 },
96 processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
97 interfaceName, destProperty, dbusPropertyValue);
98}
99
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500100/**
101 * ManagerResetAction class supports the POST method for the Reset (reboot)
102 * action.
103 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700104inline void requestRoutesManagerResetAction(App& app)
Jennifer Leeed5befb2018-08-10 11:29:45 -0700105{
Jennifer Leeed5befb2018-08-10 11:29:45 -0700106 /**
Jennifer Leeed5befb2018-08-10 11:29:45 -0700107 * Function handles POST method request.
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500108 * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000109 * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
Jennifer Leeed5befb2018-08-10 11:29:45 -0700110 */
Jennifer Leeed5befb2018-08-10 11:29:45 -0700111
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700112 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
Ed Tanoused398212021-06-09 17:05:54 -0700113 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700114 .methods(boost::beast::http::verb::post)(
115 [](const crow::Request& req,
116 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
117 BMCWEB_LOG_DEBUG << "Post Manager Reset.";
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500118
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700119 std::string resetType;
Jennifer Leeed5befb2018-08-10 11:29:45 -0700120
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700121 if (!json_util::readJson(req, asyncResp->res, "ResetType",
122 resetType))
123 {
124 return;
125 }
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500126
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700127 if (resetType == "GracefulRestart")
128 {
129 BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
130 doBMCGracefulRestart(asyncResp);
131 return;
132 }
133 if (resetType == "ForceRestart")
134 {
135 BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
136 doBMCForceRestart(asyncResp);
137 return;
138 }
139 BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
140 << resetType;
141 messages::actionParameterNotSupported(asyncResp->res, resetType,
142 "ResetType");
143
144 return;
145 });
146}
Jennifer Leeed5befb2018-08-10 11:29:45 -0700147
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500148/**
149 * ManagerResetToDefaultsAction class supports POST method for factory reset
150 * action.
151 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700152inline void requestRoutesManagerResetToDefaultsAction(App& app)
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500153{
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500154
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500155 /**
156 * Function handles ResetToDefaults POST method request.
157 *
158 * Analyzes POST body message and factory resets BMC by calling
159 * BMC code updater factory reset followed by a BMC reboot.
160 *
161 * BMC code updater factory reset wipes the whole BMC read-write
162 * filesystem which includes things like the network settings.
163 *
164 * OpenBMC only supports ResetToDefaultsType "ResetAll".
165 */
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500166
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700167 BMCWEB_ROUTE(app,
168 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/")
Ed Tanoused398212021-06-09 17:05:54 -0700169 .privileges(redfish::privileges::postManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700170 .methods(boost::beast::http::verb::post)(
171 [](const crow::Request& req,
172 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
173 BMCWEB_LOG_DEBUG << "Post ResetToDefaults.";
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500174
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700175 std::string resetType;
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500176
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700177 if (!json_util::readJson(req, asyncResp->res,
178 "ResetToDefaultsType", resetType))
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500179 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700180 BMCWEB_LOG_DEBUG << "Missing property ResetToDefaultsType.";
181
182 messages::actionParameterMissing(asyncResp->res,
183 "ResetToDefaults",
184 "ResetToDefaultsType");
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500185 return;
186 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700187
188 if (resetType != "ResetAll")
189 {
190 BMCWEB_LOG_DEBUG << "Invalid property value for "
191 "ResetToDefaultsType: "
192 << resetType;
193 messages::actionParameterNotSupported(
194 asyncResp->res, resetType, "ResetToDefaultsType");
195 return;
196 }
197
198 crow::connections::systemBus->async_method_call(
199 [asyncResp](const boost::system::error_code ec) {
200 if (ec)
201 {
202 BMCWEB_LOG_DEBUG << "Failed to ResetToDefaults: "
203 << ec;
204 messages::internalError(asyncResp->res);
205 return;
206 }
207 // Factory Reset doesn't actually happen until a reboot
208 // Can't erase what the BMC is running on
209 doBMCGracefulRestart(asyncResp);
210 },
211 "xyz.openbmc_project.Software.BMC.Updater",
212 "/xyz/openbmc_project/software",
213 "xyz.openbmc_project.Common.FactoryReset", "Reset");
214 });
215}
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500216
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530217/**
218 * ManagerResetActionInfo derived class for delivering Manager
219 * ResetType AllowableValues using ResetInfo schema.
220 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700221inline void requestRoutesManagerResetActionInfo(App& app)
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530222{
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530223 /**
224 * Functions triggers appropriate requests on DBus
225 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700226
227 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/ResetActionInfo/")
Ed Tanoused398212021-06-09 17:05:54 -0700228 .privileges(redfish::privileges::getActionInfo)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700229 .methods(boost::beast::http::verb::get)(
230 [](const crow::Request&,
231 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
232 asyncResp->res.jsonValue = {
233 {"@odata.type", "#ActionInfo.v1_1_2.ActionInfo"},
234 {"@odata.id", "/redfish/v1/Managers/bmc/ResetActionInfo"},
235 {"Name", "Reset Action Info"},
236 {"Id", "ResetActionInfo"},
237 {"Parameters",
238 {{{"Name", "ResetType"},
239 {"Required", true},
240 {"DataType", "String"},
241 {"AllowableValues",
242 {"GracefulRestart", "ForceRestart"}}}}}};
243 });
244}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530245
James Feist5b4aa862018-08-16 14:07:01 -0700246static constexpr const char* objectManagerIface =
247 "org.freedesktop.DBus.ObjectManager";
248static constexpr const char* pidConfigurationIface =
249 "xyz.openbmc_project.Configuration.Pid";
250static constexpr const char* pidZoneConfigurationIface =
251 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feistb7a08d02018-12-11 14:55:37 -0800252static constexpr const char* stepwiseConfigurationIface =
253 "xyz.openbmc_project.Configuration.Stepwise";
James Feist73df0db2019-03-25 15:29:35 -0700254static constexpr const char* thermalModeIface =
255 "xyz.openbmc_project.Control.ThermalMode";
Borawski.Lukasz9c3106852018-02-09 15:24:22 +0100256
zhanghch058d1b46d2021-04-01 11:18:24 +0800257inline void
258 asyncPopulatePid(const std::string& connection, const std::string& path,
259 const std::string& currentProfile,
260 const std::vector<std::string>& supportedProfiles,
261 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
James Feist5b4aa862018-08-16 14:07:01 -0700262{
263
264 crow::connections::systemBus->async_method_call(
James Feist73df0db2019-03-25 15:29:35 -0700265 [asyncResp, currentProfile, supportedProfiles](
266 const boost::system::error_code ec,
267 const dbus::utility::ManagedObjectType& managedObj) {
James Feist5b4aa862018-08-16 14:07:01 -0700268 if (ec)
269 {
270 BMCWEB_LOG_ERROR << ec;
James Feist5b4aa862018-08-16 14:07:01 -0700271 asyncResp->res.jsonValue.clear();
Jason M. Billsf12894f2018-10-09 12:45:45 -0700272 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700273 return;
274 }
275 nlohmann::json& configRoot =
276 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
277 nlohmann::json& fans = configRoot["FanControllers"];
278 fans["@odata.type"] = "#OemManager.FanControllers";
James Feist5b4aa862018-08-16 14:07:01 -0700279 fans["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/"
280 "Fan/FanControllers";
281
282 nlohmann::json& pids = configRoot["PidControllers"];
283 pids["@odata.type"] = "#OemManager.PidControllers";
James Feist5b4aa862018-08-16 14:07:01 -0700284 pids["@odata.id"] =
285 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
286
James Feistb7a08d02018-12-11 14:55:37 -0800287 nlohmann::json& stepwise = configRoot["StepwiseControllers"];
288 stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
James Feistb7a08d02018-12-11 14:55:37 -0800289 stepwise["@odata.id"] =
290 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
291
James Feist5b4aa862018-08-16 14:07:01 -0700292 nlohmann::json& zones = configRoot["FanZones"];
293 zones["@odata.id"] =
294 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
295 zones["@odata.type"] = "#OemManager.FanZones";
James Feist5b4aa862018-08-16 14:07:01 -0700296 configRoot["@odata.id"] =
297 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
298 configRoot["@odata.type"] = "#OemManager.Fan";
James Feist73df0db2019-03-25 15:29:35 -0700299 configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
300
301 if (!currentProfile.empty())
302 {
303 configRoot["Profile"] = currentProfile;
304 }
305 BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !";
James Feist5b4aa862018-08-16 14:07:01 -0700306
James Feist5b4aa862018-08-16 14:07:01 -0700307 for (const auto& pathPair : managedObj)
308 {
309 for (const auto& intfPair : pathPair.second)
310 {
311 if (intfPair.first != pidConfigurationIface &&
James Feistb7a08d02018-12-11 14:55:37 -0800312 intfPair.first != pidZoneConfigurationIface &&
313 intfPair.first != stepwiseConfigurationIface)
James Feist5b4aa862018-08-16 14:07:01 -0700314 {
315 continue;
316 }
317 auto findName = intfPair.second.find("Name");
318 if (findName == intfPair.second.end())
319 {
320 BMCWEB_LOG_ERROR << "Pid Field missing Name";
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800321 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700322 return;
323 }
James Feist73df0db2019-03-25 15:29:35 -0700324
James Feist5b4aa862018-08-16 14:07:01 -0700325 const std::string* namePtr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800326 std::get_if<std::string>(&findName->second);
James Feist5b4aa862018-08-16 14:07:01 -0700327 if (namePtr == nullptr)
328 {
329 BMCWEB_LOG_ERROR << "Pid Name Field illegal";
James Feistb7a08d02018-12-11 14:55:37 -0800330 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700331 return;
332 }
James Feist5b4aa862018-08-16 14:07:01 -0700333 std::string name = *namePtr;
334 dbus::utility::escapePathForDbus(name);
James Feist73df0db2019-03-25 15:29:35 -0700335
336 auto findProfiles = intfPair.second.find("Profiles");
337 if (findProfiles != intfPair.second.end())
338 {
339 const std::vector<std::string>* profiles =
340 std::get_if<std::vector<std::string>>(
341 &findProfiles->second);
342 if (profiles == nullptr)
343 {
344 BMCWEB_LOG_ERROR << "Pid Profiles Field illegal";
345 messages::internalError(asyncResp->res);
346 return;
347 }
348 if (std::find(profiles->begin(), profiles->end(),
349 currentProfile) == profiles->end())
350 {
351 BMCWEB_LOG_INFO
352 << name << " not supported in current profile";
353 continue;
354 }
355 }
James Feistb7a08d02018-12-11 14:55:37 -0800356 nlohmann::json* config = nullptr;
James Feistc33a90e2019-03-01 10:17:44 -0800357
358 const std::string* classPtr = nullptr;
359 auto findClass = intfPair.second.find("Class");
360 if (findClass != intfPair.second.end())
361 {
362 classPtr = std::get_if<std::string>(&findClass->second);
363 }
364
James Feist5b4aa862018-08-16 14:07:01 -0700365 if (intfPair.first == pidZoneConfigurationIface)
366 {
367 std::string chassis;
368 if (!dbus::utility::getNthStringFromPath(
369 pathPair.first.str, 5, chassis))
370 {
371 chassis = "#IllegalValue";
372 }
373 nlohmann::json& zone = zones[name];
374 zone["Chassis"] = {
375 {"@odata.id", "/redfish/v1/Chassis/" + chassis}};
376 zone["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/"
377 "OpenBmc/Fan/FanZones/" +
378 name;
379 zone["@odata.type"] = "#OemManager.FanZone";
James Feistb7a08d02018-12-11 14:55:37 -0800380 config = &zone;
James Feist5b4aa862018-08-16 14:07:01 -0700381 }
382
James Feistb7a08d02018-12-11 14:55:37 -0800383 else if (intfPair.first == stepwiseConfigurationIface)
384 {
James Feistc33a90e2019-03-01 10:17:44 -0800385 if (classPtr == nullptr)
386 {
387 BMCWEB_LOG_ERROR << "Pid Class Field illegal";
388 messages::internalError(asyncResp->res);
389 return;
390 }
391
James Feistb7a08d02018-12-11 14:55:37 -0800392 nlohmann::json& controller = stepwise[name];
393 config = &controller;
394
395 controller["@odata.id"] =
396 "/redfish/v1/Managers/bmc#/Oem/"
397 "OpenBmc/Fan/StepwiseControllers/" +
Ed Tanous271584a2019-07-09 16:24:22 -0700398 name;
James Feistb7a08d02018-12-11 14:55:37 -0800399 controller["@odata.type"] =
400 "#OemManager.StepwiseController";
401
James Feistc33a90e2019-03-01 10:17:44 -0800402 controller["Direction"] = *classPtr;
James Feistb7a08d02018-12-11 14:55:37 -0800403 }
404
405 // pid and fans are off the same configuration
406 else if (intfPair.first == pidConfigurationIface)
407 {
James Feistc33a90e2019-03-01 10:17:44 -0800408
James Feistb7a08d02018-12-11 14:55:37 -0800409 if (classPtr == nullptr)
410 {
411 BMCWEB_LOG_ERROR << "Pid Class Field illegal";
412 messages::internalError(asyncResp->res);
413 return;
414 }
415 bool isFan = *classPtr == "fan";
416 nlohmann::json& element =
417 isFan ? fans[name] : pids[name];
418 config = &element;
419 if (isFan)
420 {
421 element["@odata.id"] =
422 "/redfish/v1/Managers/bmc#/Oem/"
423 "OpenBmc/Fan/FanControllers/" +
Ed Tanous271584a2019-07-09 16:24:22 -0700424 name;
James Feistb7a08d02018-12-11 14:55:37 -0800425 element["@odata.type"] =
426 "#OemManager.FanController";
James Feistb7a08d02018-12-11 14:55:37 -0800427 }
428 else
429 {
430 element["@odata.id"] =
431 "/redfish/v1/Managers/bmc#/Oem/"
432 "OpenBmc/Fan/PidControllers/" +
Ed Tanous271584a2019-07-09 16:24:22 -0700433 name;
James Feistb7a08d02018-12-11 14:55:37 -0800434 element["@odata.type"] =
435 "#OemManager.PidController";
James Feistb7a08d02018-12-11 14:55:37 -0800436 }
437 }
438 else
439 {
440 BMCWEB_LOG_ERROR << "Unexpected configuration";
441 messages::internalError(asyncResp->res);
442 return;
443 }
444
445 // used for making maps out of 2 vectors
446 const std::vector<double>* keys = nullptr;
447 const std::vector<double>* values = nullptr;
448
James Feist5b4aa862018-08-16 14:07:01 -0700449 for (const auto& propertyPair : intfPair.second)
450 {
451 if (propertyPair.first == "Type" ||
452 propertyPair.first == "Class" ||
453 propertyPair.first == "Name")
454 {
455 continue;
456 }
457
458 // zones
459 if (intfPair.first == pidZoneConfigurationIface)
460 {
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800461 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800462 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700463 if (ptr == nullptr)
464 {
465 BMCWEB_LOG_ERROR << "Field Illegal "
466 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700467 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700468 return;
469 }
James Feistb7a08d02018-12-11 14:55:37 -0800470 (*config)[propertyPair.first] = *ptr;
471 }
472
473 if (intfPair.first == stepwiseConfigurationIface)
474 {
475 if (propertyPair.first == "Reading" ||
476 propertyPair.first == "Output")
477 {
478 const std::vector<double>* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800479 std::get_if<std::vector<double>>(
James Feistb7a08d02018-12-11 14:55:37 -0800480 &propertyPair.second);
481
482 if (ptr == nullptr)
483 {
484 BMCWEB_LOG_ERROR << "Field Illegal "
485 << propertyPair.first;
486 messages::internalError(asyncResp->res);
487 return;
488 }
489
490 if (propertyPair.first == "Reading")
491 {
492 keys = ptr;
493 }
494 else
495 {
496 values = ptr;
497 }
498 if (keys && values)
499 {
500 if (keys->size() != values->size())
501 {
502 BMCWEB_LOG_ERROR
503 << "Reading and Output size don't "
504 "match ";
505 messages::internalError(asyncResp->res);
506 return;
507 }
508 nlohmann::json& steps = (*config)["Steps"];
509 steps = nlohmann::json::array();
510 for (size_t ii = 0; ii < keys->size(); ii++)
511 {
512 steps.push_back(
513 {{"Target", (*keys)[ii]},
514 {"Output", (*values)[ii]}});
515 }
516 }
517 }
518 if (propertyPair.first == "NegativeHysteresis" ||
519 propertyPair.first == "PositiveHysteresis")
520 {
521 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800522 std::get_if<double>(&propertyPair.second);
James Feistb7a08d02018-12-11 14:55:37 -0800523 if (ptr == nullptr)
524 {
525 BMCWEB_LOG_ERROR << "Field Illegal "
526 << propertyPair.first;
527 messages::internalError(asyncResp->res);
528 return;
529 }
530 (*config)[propertyPair.first] = *ptr;
531 }
James Feist5b4aa862018-08-16 14:07:01 -0700532 }
533
534 // pid and fans are off the same configuration
James Feistb7a08d02018-12-11 14:55:37 -0800535 if (intfPair.first == pidConfigurationIface ||
536 intfPair.first == stepwiseConfigurationIface)
James Feist5b4aa862018-08-16 14:07:01 -0700537 {
James Feist5b4aa862018-08-16 14:07:01 -0700538
539 if (propertyPair.first == "Zones")
540 {
541 const std::vector<std::string>* inputs =
Ed Tanousabf2add2019-01-22 16:40:12 -0800542 std::get_if<std::vector<std::string>>(
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800543 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700544
545 if (inputs == nullptr)
546 {
547 BMCWEB_LOG_ERROR
548 << "Zones Pid Field Illegal";
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800549 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700550 return;
551 }
James Feistb7a08d02018-12-11 14:55:37 -0800552 auto& data = (*config)[propertyPair.first];
James Feist5b4aa862018-08-16 14:07:01 -0700553 data = nlohmann::json::array();
554 for (std::string itemCopy : *inputs)
555 {
556 dbus::utility::escapePathForDbus(itemCopy);
557 data.push_back(
558 {{"@odata.id",
559 "/redfish/v1/Managers/bmc#/Oem/"
560 "OpenBmc/Fan/FanZones/" +
561 itemCopy}});
562 }
563 }
564 // todo(james): may never happen, but this
565 // assumes configuration data referenced in the
566 // PID config is provided by the same daemon, we
567 // could add another loop to cover all cases,
568 // but I'm okay kicking this can down the road a
569 // bit
570
571 else if (propertyPair.first == "Inputs" ||
572 propertyPair.first == "Outputs")
573 {
James Feistb7a08d02018-12-11 14:55:37 -0800574 auto& data = (*config)[propertyPair.first];
James Feist5b4aa862018-08-16 14:07:01 -0700575 const std::vector<std::string>* inputs =
Ed Tanousabf2add2019-01-22 16:40:12 -0800576 std::get_if<std::vector<std::string>>(
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800577 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700578
579 if (inputs == nullptr)
580 {
581 BMCWEB_LOG_ERROR << "Field Illegal "
582 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700583 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700584 return;
585 }
586 data = *inputs;
James Feistb943aae2019-07-11 16:33:56 -0700587 }
588 else if (propertyPair.first == "SetPointOffset")
589 {
590 const std::string* ptr =
591 std::get_if<std::string>(
592 &propertyPair.second);
593
594 if (ptr == nullptr)
595 {
596 BMCWEB_LOG_ERROR << "Field Illegal "
597 << propertyPair.first;
598 messages::internalError(asyncResp->res);
599 return;
600 }
601 // translate from dbus to redfish
602 if (*ptr == "WarningHigh")
603 {
604 (*config)["SetPointOffset"] =
605 "UpperThresholdNonCritical";
606 }
607 else if (*ptr == "WarningLow")
608 {
609 (*config)["SetPointOffset"] =
610 "LowerThresholdNonCritical";
611 }
612 else if (*ptr == "CriticalHigh")
613 {
614 (*config)["SetPointOffset"] =
615 "UpperThresholdCritical";
616 }
617 else if (*ptr == "CriticalLow")
618 {
619 (*config)["SetPointOffset"] =
620 "LowerThresholdCritical";
621 }
622 else
623 {
624 BMCWEB_LOG_ERROR << "Value Illegal "
625 << *ptr;
626 messages::internalError(asyncResp->res);
627 return;
628 }
629 }
630 // doubles
James Feist5b4aa862018-08-16 14:07:01 -0700631 else if (propertyPair.first ==
632 "FFGainCoefficient" ||
633 propertyPair.first == "FFOffCoefficient" ||
634 propertyPair.first == "ICoefficient" ||
635 propertyPair.first == "ILimitMax" ||
636 propertyPair.first == "ILimitMin" ||
James Feistaad1a252019-02-19 10:13:52 -0800637 propertyPair.first ==
638 "PositiveHysteresis" ||
639 propertyPair.first ==
640 "NegativeHysteresis" ||
James Feist5b4aa862018-08-16 14:07:01 -0700641 propertyPair.first == "OutLimitMax" ||
642 propertyPair.first == "OutLimitMin" ||
643 propertyPair.first == "PCoefficient" ||
James Feist7625cb82019-01-23 11:58:21 -0800644 propertyPair.first == "SetPoint" ||
James Feist5b4aa862018-08-16 14:07:01 -0700645 propertyPair.first == "SlewNeg" ||
646 propertyPair.first == "SlewPos")
647 {
648 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800649 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700650 if (ptr == nullptr)
651 {
652 BMCWEB_LOG_ERROR << "Field Illegal "
653 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700654 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700655 return;
656 }
James Feistb7a08d02018-12-11 14:55:37 -0800657 (*config)[propertyPair.first] = *ptr;
James Feist5b4aa862018-08-16 14:07:01 -0700658 }
659 }
660 }
661 }
662 }
663 },
664 connection, path, objectManagerIface, "GetManagedObjects");
665}
Jennifer Leeca537922018-08-10 10:07:30 -0700666
James Feist83ff9ab2018-08-31 10:18:24 -0700667enum class CreatePIDRet
668{
669 fail,
670 del,
671 patch
672};
673
zhanghch058d1b46d2021-04-01 11:18:24 +0800674inline bool
675 getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
676 std::vector<nlohmann::json>& config,
677 std::vector<std::string>& zones)
James Feist5f2caae2018-12-12 14:08:25 -0800678{
James Feistb6baeaa2019-02-21 10:41:40 -0800679 if (config.empty())
680 {
681 BMCWEB_LOG_ERROR << "Empty Zones";
682 messages::propertyValueFormatError(response->res,
683 nlohmann::json::array(), "Zones");
684 return false;
685 }
James Feist5f2caae2018-12-12 14:08:25 -0800686 for (auto& odata : config)
687 {
688 std::string path;
689 if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
690 path))
691 {
692 return false;
693 }
694 std::string input;
James Feist61adbda2019-03-25 13:03:51 -0700695
696 // 8 below comes from
697 // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
698 // 0 1 2 3 4 5 6 7 8
699 if (!dbus::utility::getNthStringFromPath(path, 8, input))
James Feist5f2caae2018-12-12 14:08:25 -0800700 {
701 BMCWEB_LOG_ERROR << "Got invalid path " << path;
702 BMCWEB_LOG_ERROR << "Illegal Type Zones";
703 messages::propertyValueFormatError(response->res, odata.dump(),
704 "Zones");
705 return false;
706 }
707 boost::replace_all(input, "_", " ");
708 zones.emplace_back(std::move(input));
709 }
710 return true;
711}
712
Ed Tanous23a21a12020-07-25 04:45:05 +0000713inline const dbus::utility::ManagedItem*
James Feist73df0db2019-03-25 15:29:35 -0700714 findChassis(const dbus::utility::ManagedObjectType& managedObj,
715 const std::string& value, std::string& chassis)
James Feistb6baeaa2019-02-21 10:41:40 -0800716{
717 BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
718
719 std::string escaped = boost::replace_all_copy(value, " ", "_");
720 escaped = "/" + escaped;
721 auto it = std::find_if(
722 managedObj.begin(), managedObj.end(), [&escaped](const auto& obj) {
723 if (boost::algorithm::ends_with(obj.first.str, escaped))
724 {
725 BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
726 return true;
727 }
728 return false;
729 });
730
731 if (it == managedObj.end())
732 {
James Feist73df0db2019-03-25 15:29:35 -0700733 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800734 }
735 // 5 comes from <chassis-name> being the 5th element
736 // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
James Feist73df0db2019-03-25 15:29:35 -0700737 if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
738 {
739 return &(*it);
740 }
741
742 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800743}
744
Ed Tanous23a21a12020-07-25 04:45:05 +0000745inline CreatePIDRet createPidInterface(
zhanghch058d1b46d2021-04-01 11:18:24 +0800746 const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
Ed Tanousb5a76932020-09-29 16:16:58 -0700747 const nlohmann::json::iterator& it, const std::string& path,
James Feist83ff9ab2018-08-31 10:18:24 -0700748 const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
749 boost::container::flat_map<std::string, dbus::utility::DbusVariantType>&
750 output,
James Feist73df0db2019-03-25 15:29:35 -0700751 std::string& chassis, const std::string& profile)
James Feist83ff9ab2018-08-31 10:18:24 -0700752{
753
James Feist5f2caae2018-12-12 14:08:25 -0800754 // common deleter
James Feistb6baeaa2019-02-21 10:41:40 -0800755 if (it.value() == nullptr)
James Feist5f2caae2018-12-12 14:08:25 -0800756 {
757 std::string iface;
758 if (type == "PidControllers" || type == "FanControllers")
759 {
760 iface = pidConfigurationIface;
761 }
762 else if (type == "FanZones")
763 {
764 iface = pidZoneConfigurationIface;
765 }
766 else if (type == "StepwiseControllers")
767 {
768 iface = stepwiseConfigurationIface;
769 }
770 else
771 {
Gunnar Millsa0744d32020-11-09 15:40:45 -0600772 BMCWEB_LOG_ERROR << "Illegal Type " << type;
James Feist5f2caae2018-12-12 14:08:25 -0800773 messages::propertyUnknown(response->res, type);
774 return CreatePIDRet::fail;
775 }
James Feist6ee7f772020-02-06 16:25:27 -0800776
777 BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
James Feist5f2caae2018-12-12 14:08:25 -0800778 // delete interface
779 crow::connections::systemBus->async_method_call(
780 [response, path](const boost::system::error_code ec) {
781 if (ec)
782 {
783 BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
784 messages::internalError(response->res);
James Feistb6baeaa2019-02-21 10:41:40 -0800785 return;
James Feist5f2caae2018-12-12 14:08:25 -0800786 }
James Feistb6baeaa2019-02-21 10:41:40 -0800787 messages::success(response->res);
James Feist5f2caae2018-12-12 14:08:25 -0800788 },
789 "xyz.openbmc_project.EntityManager", path, iface, "Delete");
790 return CreatePIDRet::del;
791 }
792
James Feist73df0db2019-03-25 15:29:35 -0700793 const dbus::utility::ManagedItem* managedItem = nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800794 if (!createNewObject)
795 {
796 // if we aren't creating a new object, we should be able to find it on
797 // d-bus
James Feist73df0db2019-03-25 15:29:35 -0700798 managedItem = findChassis(managedObj, it.key(), chassis);
799 if (managedItem == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800800 {
801 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
802 messages::invalidObject(response->res, it.key());
803 return CreatePIDRet::fail;
804 }
805 }
806
James Feist73df0db2019-03-25 15:29:35 -0700807 if (profile.size() &&
808 (type == "PidControllers" || type == "FanControllers" ||
809 type == "StepwiseControllers"))
810 {
811 if (managedItem == nullptr)
812 {
813 output["Profiles"] = std::vector<std::string>{profile};
814 }
815 else
816 {
817 std::string interface;
818 if (type == "StepwiseControllers")
819 {
820 interface = stepwiseConfigurationIface;
821 }
822 else
823 {
824 interface = pidConfigurationIface;
825 }
826 auto findConfig = managedItem->second.find(interface);
827 if (findConfig == managedItem->second.end())
828 {
829 BMCWEB_LOG_ERROR
830 << "Failed to find interface in managed object";
831 messages::internalError(response->res);
832 return CreatePIDRet::fail;
833 }
834 auto findProfiles = findConfig->second.find("Profiles");
835 if (findProfiles != findConfig->second.end())
836 {
837 const std::vector<std::string>* curProfiles =
838 std::get_if<std::vector<std::string>>(
839 &(findProfiles->second));
840 if (curProfiles == nullptr)
841 {
842 BMCWEB_LOG_ERROR << "Illegal profiles in managed object";
843 messages::internalError(response->res);
844 return CreatePIDRet::fail;
845 }
846 if (std::find(curProfiles->begin(), curProfiles->end(),
847 profile) == curProfiles->end())
848 {
849 std::vector<std::string> newProfiles = *curProfiles;
850 newProfiles.push_back(profile);
851 output["Profiles"] = newProfiles;
852 }
853 }
854 }
855 }
856
James Feist83ff9ab2018-08-31 10:18:24 -0700857 if (type == "PidControllers" || type == "FanControllers")
858 {
859 if (createNewObject)
860 {
861 output["Class"] = type == "PidControllers" ? std::string("temp")
862 : std::string("fan");
863 output["Type"] = std::string("Pid");
864 }
James Feist5f2caae2018-12-12 14:08:25 -0800865
866 std::optional<std::vector<nlohmann::json>> zones;
867 std::optional<std::vector<std::string>> inputs;
868 std::optional<std::vector<std::string>> outputs;
869 std::map<std::string, std::optional<double>> doubles;
James Feistb943aae2019-07-11 16:33:56 -0700870 std::optional<std::string> setpointOffset;
James Feist5f2caae2018-12-12 14:08:25 -0800871 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -0800872 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
James Feist5f2caae2018-12-12 14:08:25 -0800873 "Zones", zones, "FFGainCoefficient",
874 doubles["FFGainCoefficient"], "FFOffCoefficient",
875 doubles["FFOffCoefficient"], "ICoefficient",
876 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
877 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
878 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
879 "PCoefficient", doubles["PCoefficient"], "SetPoint",
James Feistb943aae2019-07-11 16:33:56 -0700880 doubles["SetPoint"], "SetPointOffset", setpointOffset,
881 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
882 "PositiveHysteresis", doubles["PositiveHysteresis"],
883 "NegativeHysteresis", doubles["NegativeHysteresis"]))
James Feist83ff9ab2018-08-31 10:18:24 -0700884 {
Ed Tanous71f52d92021-02-19 08:51:17 -0800885 BMCWEB_LOG_ERROR
886 << "Illegal Property "
887 << it.value().dump(2, ' ', true,
888 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -0800889 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700890 }
James Feist5f2caae2018-12-12 14:08:25 -0800891 if (zones)
James Feist83ff9ab2018-08-31 10:18:24 -0700892 {
James Feist5f2caae2018-12-12 14:08:25 -0800893 std::vector<std::string> zonesStr;
894 if (!getZonesFromJsonReq(response, *zones, zonesStr))
James Feist83ff9ab2018-08-31 10:18:24 -0700895 {
Gunnar Millsa0744d32020-11-09 15:40:45 -0600896 BMCWEB_LOG_ERROR << "Illegal Zones";
James Feist5f2caae2018-12-12 14:08:25 -0800897 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700898 }
James Feistb6baeaa2019-02-21 10:41:40 -0800899 if (chassis.empty() &&
900 !findChassis(managedObj, zonesStr[0], chassis))
901 {
902 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
903 messages::invalidObject(response->res, it.key());
904 return CreatePIDRet::fail;
905 }
906
James Feist5f2caae2018-12-12 14:08:25 -0800907 output["Zones"] = std::move(zonesStr);
908 }
909 if (inputs || outputs)
910 {
911 std::array<std::optional<std::vector<std::string>>*, 2> containers =
912 {&inputs, &outputs};
913 size_t index = 0;
914 for (const auto& containerPtr : containers)
James Feist83ff9ab2018-08-31 10:18:24 -0700915 {
James Feist5f2caae2018-12-12 14:08:25 -0800916 std::optional<std::vector<std::string>>& container =
917 *containerPtr;
918 if (!container)
James Feist83ff9ab2018-08-31 10:18:24 -0700919 {
James Feist5f2caae2018-12-12 14:08:25 -0800920 index++;
921 continue;
James Feist83ff9ab2018-08-31 10:18:24 -0700922 }
James Feist5f2caae2018-12-12 14:08:25 -0800923
924 for (std::string& value : *container)
James Feist83ff9ab2018-08-31 10:18:24 -0700925 {
James Feist5f2caae2018-12-12 14:08:25 -0800926 boost::replace_all(value, "_", " ");
James Feist83ff9ab2018-08-31 10:18:24 -0700927 }
James Feist5f2caae2018-12-12 14:08:25 -0800928 std::string key;
929 if (index == 0)
James Feist83ff9ab2018-08-31 10:18:24 -0700930 {
James Feist5f2caae2018-12-12 14:08:25 -0800931 key = "Inputs";
James Feist83ff9ab2018-08-31 10:18:24 -0700932 }
James Feist5f2caae2018-12-12 14:08:25 -0800933 else
934 {
935 key = "Outputs";
936 }
937 output[key] = *container;
938 index++;
James Feist83ff9ab2018-08-31 10:18:24 -0700939 }
James Feist5f2caae2018-12-12 14:08:25 -0800940 }
James Feist83ff9ab2018-08-31 10:18:24 -0700941
James Feistb943aae2019-07-11 16:33:56 -0700942 if (setpointOffset)
943 {
944 // translate between redfish and dbus names
945 if (*setpointOffset == "UpperThresholdNonCritical")
946 {
947 output["SetPointOffset"] = std::string("WarningLow");
948 }
949 else if (*setpointOffset == "LowerThresholdNonCritical")
950 {
951 output["SetPointOffset"] = std::string("WarningHigh");
952 }
953 else if (*setpointOffset == "LowerThresholdCritical")
954 {
955 output["SetPointOffset"] = std::string("CriticalLow");
956 }
957 else if (*setpointOffset == "UpperThresholdCritical")
958 {
959 output["SetPointOffset"] = std::string("CriticalHigh");
960 }
961 else
962 {
963 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
964 << *setpointOffset;
965 messages::invalidObject(response->res, it.key());
966 return CreatePIDRet::fail;
967 }
968 }
969
James Feist5f2caae2018-12-12 14:08:25 -0800970 // doubles
971 for (const auto& pairs : doubles)
972 {
973 if (!pairs.second)
James Feist83ff9ab2018-08-31 10:18:24 -0700974 {
James Feist5f2caae2018-12-12 14:08:25 -0800975 continue;
James Feist83ff9ab2018-08-31 10:18:24 -0700976 }
James Feist5f2caae2018-12-12 14:08:25 -0800977 BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
978 output[pairs.first] = *(pairs.second);
James Feist83ff9ab2018-08-31 10:18:24 -0700979 }
980 }
James Feist5f2caae2018-12-12 14:08:25 -0800981
James Feist83ff9ab2018-08-31 10:18:24 -0700982 else if (type == "FanZones")
983 {
James Feist83ff9ab2018-08-31 10:18:24 -0700984 output["Type"] = std::string("Pid.Zone");
985
James Feist5f2caae2018-12-12 14:08:25 -0800986 std::optional<nlohmann::json> chassisContainer;
987 std::optional<double> failSafePercent;
James Feistd3ec07f2019-02-25 14:51:15 -0800988 std::optional<double> minThermalOutput;
James Feistb6baeaa2019-02-21 10:41:40 -0800989 if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
James Feist5f2caae2018-12-12 14:08:25 -0800990 chassisContainer, "FailSafePercent",
James Feistd3ec07f2019-02-25 14:51:15 -0800991 failSafePercent, "MinThermalOutput",
992 minThermalOutput))
James Feist83ff9ab2018-08-31 10:18:24 -0700993 {
Ed Tanous71f52d92021-02-19 08:51:17 -0800994 BMCWEB_LOG_ERROR
995 << "Illegal Property "
996 << it.value().dump(2, ' ', true,
997 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -0800998 return CreatePIDRet::fail;
999 }
James Feist83ff9ab2018-08-31 10:18:24 -07001000
James Feist5f2caae2018-12-12 14:08:25 -08001001 if (chassisContainer)
1002 {
1003
1004 std::string chassisId;
1005 if (!redfish::json_util::readJson(*chassisContainer, response->res,
1006 "@odata.id", chassisId))
James Feist83ff9ab2018-08-31 10:18:24 -07001007 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001008 BMCWEB_LOG_ERROR
1009 << "Illegal Property "
1010 << chassisContainer->dump(
1011 2, ' ', true,
1012 nlohmann::json::error_handler_t::replace);
James Feist83ff9ab2018-08-31 10:18:24 -07001013 return CreatePIDRet::fail;
1014 }
James Feist5f2caae2018-12-12 14:08:25 -08001015
AppaRao Puli717794d2019-10-18 22:54:53 +05301016 // /redfish/v1/chassis/chassis_name/
James Feist5f2caae2018-12-12 14:08:25 -08001017 if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
1018 {
1019 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
1020 messages::invalidObject(response->res, chassisId);
1021 return CreatePIDRet::fail;
1022 }
1023 }
James Feistd3ec07f2019-02-25 14:51:15 -08001024 if (minThermalOutput)
James Feist5f2caae2018-12-12 14:08:25 -08001025 {
James Feistd3ec07f2019-02-25 14:51:15 -08001026 output["MinThermalOutput"] = *minThermalOutput;
James Feist5f2caae2018-12-12 14:08:25 -08001027 }
1028 if (failSafePercent)
1029 {
1030 output["FailSafePercent"] = *failSafePercent;
1031 }
1032 }
1033 else if (type == "StepwiseControllers")
1034 {
1035 output["Type"] = std::string("Stepwise");
1036
1037 std::optional<std::vector<nlohmann::json>> zones;
1038 std::optional<std::vector<nlohmann::json>> steps;
1039 std::optional<std::vector<std::string>> inputs;
1040 std::optional<double> positiveHysteresis;
1041 std::optional<double> negativeHysteresis;
James Feistc33a90e2019-03-01 10:17:44 -08001042 std::optional<std::string> direction; // upper clipping curve vs lower
James Feist5f2caae2018-12-12 14:08:25 -08001043 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -08001044 it.value(), response->res, "Zones", zones, "Steps", steps,
1045 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
James Feistc33a90e2019-03-01 10:17:44 -08001046 "NegativeHysteresis", negativeHysteresis, "Direction",
1047 direction))
James Feist5f2caae2018-12-12 14:08:25 -08001048 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001049 BMCWEB_LOG_ERROR
1050 << "Illegal Property "
1051 << it.value().dump(2, ' ', true,
1052 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -08001053 return CreatePIDRet::fail;
1054 }
1055
1056 if (zones)
1057 {
James Feistb6baeaa2019-02-21 10:41:40 -08001058 std::vector<std::string> zonesStrs;
1059 if (!getZonesFromJsonReq(response, *zones, zonesStrs))
James Feist5f2caae2018-12-12 14:08:25 -08001060 {
Gunnar Millsa0744d32020-11-09 15:40:45 -06001061 BMCWEB_LOG_ERROR << "Illegal Zones";
James Feist5f2caae2018-12-12 14:08:25 -08001062 return CreatePIDRet::fail;
1063 }
James Feistb6baeaa2019-02-21 10:41:40 -08001064 if (chassis.empty() &&
1065 !findChassis(managedObj, zonesStrs[0], chassis))
1066 {
1067 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
1068 messages::invalidObject(response->res, it.key());
1069 return CreatePIDRet::fail;
1070 }
1071 output["Zones"] = std::move(zonesStrs);
James Feist5f2caae2018-12-12 14:08:25 -08001072 }
1073 if (steps)
1074 {
1075 std::vector<double> readings;
1076 std::vector<double> outputs;
1077 for (auto& step : *steps)
1078 {
1079 double target;
Ed Tanous23a21a12020-07-25 04:45:05 +00001080 double out;
James Feist5f2caae2018-12-12 14:08:25 -08001081
1082 if (!redfish::json_util::readJson(step, response->res, "Target",
Ed Tanous23a21a12020-07-25 04:45:05 +00001083 target, "Output", out))
James Feist5f2caae2018-12-12 14:08:25 -08001084 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001085 BMCWEB_LOG_ERROR
1086 << "Illegal Property "
1087 << it.value().dump(
1088 2, ' ', true,
1089 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -08001090 return CreatePIDRet::fail;
1091 }
1092 readings.emplace_back(target);
Ed Tanous23a21a12020-07-25 04:45:05 +00001093 outputs.emplace_back(out);
James Feist5f2caae2018-12-12 14:08:25 -08001094 }
1095 output["Reading"] = std::move(readings);
1096 output["Output"] = std::move(outputs);
1097 }
1098 if (inputs)
1099 {
1100 for (std::string& value : *inputs)
1101 {
James Feist5f2caae2018-12-12 14:08:25 -08001102 boost::replace_all(value, "_", " ");
1103 }
1104 output["Inputs"] = std::move(*inputs);
1105 }
1106 if (negativeHysteresis)
1107 {
1108 output["NegativeHysteresis"] = *negativeHysteresis;
1109 }
1110 if (positiveHysteresis)
1111 {
1112 output["PositiveHysteresis"] = *positiveHysteresis;
James Feist83ff9ab2018-08-31 10:18:24 -07001113 }
James Feistc33a90e2019-03-01 10:17:44 -08001114 if (direction)
1115 {
1116 constexpr const std::array<const char*, 2> allowedDirections = {
1117 "Ceiling", "Floor"};
1118 if (std::find(allowedDirections.begin(), allowedDirections.end(),
1119 *direction) == allowedDirections.end())
1120 {
1121 messages::propertyValueTypeError(response->res, "Direction",
1122 *direction);
1123 return CreatePIDRet::fail;
1124 }
1125 output["Class"] = *direction;
1126 }
James Feist83ff9ab2018-08-31 10:18:24 -07001127 }
1128 else
1129 {
Gunnar Millsa0744d32020-11-09 15:40:45 -06001130 BMCWEB_LOG_ERROR << "Illegal Type " << type;
Jason M. Bills35a62c72018-10-09 12:45:45 -07001131 messages::propertyUnknown(response->res, type);
James Feist83ff9ab2018-08-31 10:18:24 -07001132 return CreatePIDRet::fail;
1133 }
1134 return CreatePIDRet::patch;
1135}
James Feist73df0db2019-03-25 15:29:35 -07001136struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
1137{
1138
zhanghch058d1b46d2021-04-01 11:18:24 +08001139 GetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
Ed Tanous23a21a12020-07-25 04:45:05 +00001140 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001141
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001142 {}
James Feist73df0db2019-03-25 15:29:35 -07001143
1144 void run()
1145 {
1146 std::shared_ptr<GetPIDValues> self = shared_from_this();
1147
1148 // get all configurations
1149 crow::connections::systemBus->async_method_call(
1150 [self](const boost::system::error_code ec,
Ed Tanous23a21a12020-07-25 04:45:05 +00001151 const crow::openbmc_mapper::GetSubTreeType& subtreeLocal) {
James Feist73df0db2019-03-25 15:29:35 -07001152 if (ec)
1153 {
1154 BMCWEB_LOG_ERROR << ec;
1155 messages::internalError(self->asyncResp->res);
1156 return;
1157 }
Ed Tanous23a21a12020-07-25 04:45:05 +00001158 self->subtree = subtreeLocal;
James Feist73df0db2019-03-25 15:29:35 -07001159 },
1160 "xyz.openbmc_project.ObjectMapper",
1161 "/xyz/openbmc_project/object_mapper",
1162 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
1163 std::array<const char*, 4>{
1164 pidConfigurationIface, pidZoneConfigurationIface,
1165 objectManagerIface, stepwiseConfigurationIface});
1166
1167 // at the same time get the selected profile
1168 crow::connections::systemBus->async_method_call(
1169 [self](const boost::system::error_code ec,
Ed Tanous23a21a12020-07-25 04:45:05 +00001170 const crow::openbmc_mapper::GetSubTreeType& subtreeLocal) {
1171 if (ec || subtreeLocal.empty())
James Feist73df0db2019-03-25 15:29:35 -07001172 {
1173 return;
1174 }
Ed Tanous23a21a12020-07-25 04:45:05 +00001175 if (subtreeLocal[0].second.size() != 1)
James Feist73df0db2019-03-25 15:29:35 -07001176 {
1177 // invalid mapper response, should never happen
1178 BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
1179 messages::internalError(self->asyncResp->res);
1180 return;
1181 }
1182
Ed Tanous23a21a12020-07-25 04:45:05 +00001183 const std::string& path = subtreeLocal[0].first;
1184 const std::string& owner = subtreeLocal[0].second[0].first;
James Feist73df0db2019-03-25 15:29:35 -07001185 crow::connections::systemBus->async_method_call(
1186 [path, owner, self](
Ed Tanous23a21a12020-07-25 04:45:05 +00001187 const boost::system::error_code ec2,
James Feist73df0db2019-03-25 15:29:35 -07001188 const boost::container::flat_map<
1189 std::string, std::variant<std::vector<std::string>,
1190 std::string>>& resp) {
Ed Tanous23a21a12020-07-25 04:45:05 +00001191 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001192 {
1193 BMCWEB_LOG_ERROR << "GetPIDValues: Can't get "
1194 "thermalModeIface "
1195 << path;
1196 messages::internalError(self->asyncResp->res);
1197 return;
1198 }
Ed Tanous271584a2019-07-09 16:24:22 -07001199 const std::string* current = nullptr;
1200 const std::vector<std::string>* supported = nullptr;
James Feist73df0db2019-03-25 15:29:35 -07001201 for (auto& [key, value] : resp)
1202 {
1203 if (key == "Current")
1204 {
1205 current = std::get_if<std::string>(&value);
1206 if (current == nullptr)
1207 {
1208 BMCWEB_LOG_ERROR
1209 << "GetPIDValues: thermal mode "
1210 "iface invalid "
1211 << path;
1212 messages::internalError(
1213 self->asyncResp->res);
1214 return;
1215 }
1216 }
1217 if (key == "Supported")
1218 {
1219 supported =
1220 std::get_if<std::vector<std::string>>(
1221 &value);
1222 if (supported == nullptr)
1223 {
1224 BMCWEB_LOG_ERROR
1225 << "GetPIDValues: thermal mode "
1226 "iface invalid"
1227 << path;
1228 messages::internalError(
1229 self->asyncResp->res);
1230 return;
1231 }
1232 }
1233 }
1234 if (current == nullptr || supported == nullptr)
1235 {
1236 BMCWEB_LOG_ERROR << "GetPIDValues: thermal mode "
1237 "iface invalid "
1238 << path;
1239 messages::internalError(self->asyncResp->res);
1240 return;
1241 }
1242 self->currentProfile = *current;
1243 self->supportedProfiles = *supported;
1244 },
1245 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
1246 thermalModeIface);
1247 },
1248 "xyz.openbmc_project.ObjectMapper",
1249 "/xyz/openbmc_project/object_mapper",
1250 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
1251 std::array<const char*, 1>{thermalModeIface});
1252 }
1253
1254 ~GetPIDValues()
1255 {
1256 if (asyncResp->res.result() != boost::beast::http::status::ok)
1257 {
1258 return;
1259 }
1260 // create map of <connection, path to objMgr>>
1261 boost::container::flat_map<std::string, std::string> objectMgrPaths;
1262 boost::container::flat_set<std::string> calledConnections;
1263 for (const auto& pathGroup : subtree)
1264 {
1265 for (const auto& connectionGroup : pathGroup.second)
1266 {
1267 auto findConnection =
1268 calledConnections.find(connectionGroup.first);
1269 if (findConnection != calledConnections.end())
1270 {
1271 break;
1272 }
1273 for (const std::string& interface : connectionGroup.second)
1274 {
1275 if (interface == objectManagerIface)
1276 {
1277 objectMgrPaths[connectionGroup.first] = pathGroup.first;
1278 }
1279 // this list is alphabetical, so we
1280 // should have found the objMgr by now
1281 if (interface == pidConfigurationIface ||
1282 interface == pidZoneConfigurationIface ||
1283 interface == stepwiseConfigurationIface)
1284 {
1285 auto findObjMgr =
1286 objectMgrPaths.find(connectionGroup.first);
1287 if (findObjMgr == objectMgrPaths.end())
1288 {
1289 BMCWEB_LOG_DEBUG << connectionGroup.first
1290 << "Has no Object Manager";
1291 continue;
1292 }
1293
1294 calledConnections.insert(connectionGroup.first);
1295
1296 asyncPopulatePid(findObjMgr->first, findObjMgr->second,
1297 currentProfile, supportedProfiles,
1298 asyncResp);
1299 break;
1300 }
1301 }
1302 }
1303 }
1304 }
1305
1306 std::vector<std::string> supportedProfiles;
1307 std::string currentProfile;
1308 crow::openbmc_mapper::GetSubTreeType subtree;
zhanghch058d1b46d2021-04-01 11:18:24 +08001309 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001310};
1311
1312struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
1313{
1314
zhanghch058d1b46d2021-04-01 11:18:24 +08001315 SetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
James Feist73df0db2019-03-25 15:29:35 -07001316 nlohmann::json& data) :
Ed Tanous271584a2019-07-09 16:24:22 -07001317 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001318 {
1319
1320 std::optional<nlohmann::json> pidControllers;
1321 std::optional<nlohmann::json> fanControllers;
1322 std::optional<nlohmann::json> fanZones;
1323 std::optional<nlohmann::json> stepwiseControllers;
1324
1325 if (!redfish::json_util::readJson(
1326 data, asyncResp->res, "PidControllers", pidControllers,
1327 "FanControllers", fanControllers, "FanZones", fanZones,
1328 "StepwiseControllers", stepwiseControllers, "Profile", profile))
1329 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001330 BMCWEB_LOG_ERROR
1331 << "Illegal Property "
1332 << data.dump(2, ' ', true,
1333 nlohmann::json::error_handler_t::replace);
James Feist73df0db2019-03-25 15:29:35 -07001334 return;
1335 }
1336 configuration.emplace_back("PidControllers", std::move(pidControllers));
1337 configuration.emplace_back("FanControllers", std::move(fanControllers));
1338 configuration.emplace_back("FanZones", std::move(fanZones));
1339 configuration.emplace_back("StepwiseControllers",
1340 std::move(stepwiseControllers));
1341 }
1342 void run()
1343 {
1344 if (asyncResp->res.result() != boost::beast::http::status::ok)
1345 {
1346 return;
1347 }
1348
1349 std::shared_ptr<SetPIDValues> self = shared_from_this();
1350
1351 // todo(james): might make sense to do a mapper call here if this
1352 // interface gets more traction
1353 crow::connections::systemBus->async_method_call(
1354 [self](const boost::system::error_code ec,
Ed Tanous271584a2019-07-09 16:24:22 -07001355 dbus::utility::ManagedObjectType& mObj) {
James Feist73df0db2019-03-25 15:29:35 -07001356 if (ec)
1357 {
1358 BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
1359 messages::internalError(self->asyncResp->res);
1360 return;
1361 }
James Feiste69d9de2020-02-07 12:23:27 -08001362 const std::array<const char*, 3> configurations = {
1363 pidConfigurationIface, pidZoneConfigurationIface,
1364 stepwiseConfigurationIface};
1365
James Feist14b0b8d2020-02-12 11:52:07 -08001366 for (const auto& [path, object] : mObj)
James Feiste69d9de2020-02-07 12:23:27 -08001367 {
James Feist14b0b8d2020-02-12 11:52:07 -08001368 for (const auto& [interface, _] : object)
James Feiste69d9de2020-02-07 12:23:27 -08001369 {
1370 if (std::find(configurations.begin(),
1371 configurations.end(),
1372 interface) != configurations.end())
1373 {
James Feist14b0b8d2020-02-12 11:52:07 -08001374 self->objectCount++;
James Feiste69d9de2020-02-07 12:23:27 -08001375 break;
1376 }
1377 }
James Feiste69d9de2020-02-07 12:23:27 -08001378 }
Ed Tanous271584a2019-07-09 16:24:22 -07001379 self->managedObj = std::move(mObj);
James Feist73df0db2019-03-25 15:29:35 -07001380 },
1381 "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
1382 "GetManagedObjects");
1383
1384 // at the same time get the profile information
1385 crow::connections::systemBus->async_method_call(
1386 [self](const boost::system::error_code ec,
1387 const crow::openbmc_mapper::GetSubTreeType& subtree) {
1388 if (ec || subtree.empty())
1389 {
1390 return;
1391 }
1392 if (subtree[0].second.empty())
1393 {
1394 // invalid mapper response, should never happen
1395 BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
1396 messages::internalError(self->asyncResp->res);
1397 return;
1398 }
1399
1400 const std::string& path = subtree[0].first;
1401 const std::string& owner = subtree[0].second[0].first;
1402 crow::connections::systemBus->async_method_call(
1403 [self, path, owner](
Ed Tanouscb13a392020-07-25 19:02:03 +00001404 const boost::system::error_code ec2,
James Feist73df0db2019-03-25 15:29:35 -07001405 const boost::container::flat_map<
1406 std::string, std::variant<std::vector<std::string>,
Ed Tanous271584a2019-07-09 16:24:22 -07001407 std::string>>& r) {
Ed Tanouscb13a392020-07-25 19:02:03 +00001408 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001409 {
1410 BMCWEB_LOG_ERROR << "SetPIDValues: Can't get "
1411 "thermalModeIface "
1412 << path;
1413 messages::internalError(self->asyncResp->res);
1414 return;
1415 }
Ed Tanous271584a2019-07-09 16:24:22 -07001416 const std::string* current = nullptr;
1417 const std::vector<std::string>* supported = nullptr;
1418 for (auto& [key, value] : r)
James Feist73df0db2019-03-25 15:29:35 -07001419 {
1420 if (key == "Current")
1421 {
1422 current = std::get_if<std::string>(&value);
1423 if (current == nullptr)
1424 {
1425 BMCWEB_LOG_ERROR
1426 << "SetPIDValues: thermal mode "
1427 "iface invalid "
1428 << path;
1429 messages::internalError(
1430 self->asyncResp->res);
1431 return;
1432 }
1433 }
1434 if (key == "Supported")
1435 {
1436 supported =
1437 std::get_if<std::vector<std::string>>(
1438 &value);
1439 if (supported == nullptr)
1440 {
1441 BMCWEB_LOG_ERROR
1442 << "SetPIDValues: thermal mode "
1443 "iface invalid"
1444 << path;
1445 messages::internalError(
1446 self->asyncResp->res);
1447 return;
1448 }
1449 }
1450 }
1451 if (current == nullptr || supported == nullptr)
1452 {
1453 BMCWEB_LOG_ERROR << "SetPIDValues: thermal mode "
1454 "iface invalid "
1455 << path;
1456 messages::internalError(self->asyncResp->res);
1457 return;
1458 }
1459 self->currentProfile = *current;
1460 self->supportedProfiles = *supported;
1461 self->profileConnection = owner;
1462 self->profilePath = path;
1463 },
1464 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
1465 thermalModeIface);
1466 },
1467 "xyz.openbmc_project.ObjectMapper",
1468 "/xyz/openbmc_project/object_mapper",
1469 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
1470 std::array<const char*, 1>{thermalModeIface});
1471 }
1472 ~SetPIDValues()
1473 {
1474 if (asyncResp->res.result() != boost::beast::http::status::ok)
1475 {
1476 return;
1477 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001478 std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001479 if (profile)
1480 {
1481 if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
1482 *profile) == supportedProfiles.end())
1483 {
1484 messages::actionParameterUnknown(response->res, "Profile",
1485 *profile);
1486 return;
1487 }
1488 currentProfile = *profile;
1489 crow::connections::systemBus->async_method_call(
1490 [response](const boost::system::error_code ec) {
1491 if (ec)
1492 {
1493 BMCWEB_LOG_ERROR << "Error patching profile" << ec;
1494 messages::internalError(response->res);
1495 }
1496 },
1497 profileConnection, profilePath,
1498 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
1499 "Current", std::variant<std::string>(*profile));
1500 }
1501
1502 for (auto& containerPair : configuration)
1503 {
1504 auto& container = containerPair.second;
1505 if (!container)
1506 {
1507 continue;
1508 }
James Feist6ee7f772020-02-06 16:25:27 -08001509 BMCWEB_LOG_DEBUG << *container;
1510
James Feist73df0db2019-03-25 15:29:35 -07001511 std::string& type = containerPair.first;
1512
1513 for (nlohmann::json::iterator it = container->begin();
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301514 it != container->end(); ++it)
James Feist73df0db2019-03-25 15:29:35 -07001515 {
1516 const auto& name = it.key();
James Feist6ee7f772020-02-06 16:25:27 -08001517 BMCWEB_LOG_DEBUG << "looking for " << name;
1518
James Feist73df0db2019-03-25 15:29:35 -07001519 auto pathItr =
1520 std::find_if(managedObj.begin(), managedObj.end(),
1521 [&name](const auto& obj) {
1522 return boost::algorithm::ends_with(
1523 obj.first.str, "/" + name);
1524 });
1525 boost::container::flat_map<std::string,
1526 dbus::utility::DbusVariantType>
1527 output;
1528
1529 output.reserve(16); // The pid interface length
1530
1531 // determines if we're patching entity-manager or
1532 // creating a new object
1533 bool createNewObject = (pathItr == managedObj.end());
James Feist6ee7f772020-02-06 16:25:27 -08001534 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
1535
James Feist73df0db2019-03-25 15:29:35 -07001536 std::string iface;
1537 if (type == "PidControllers" || type == "FanControllers")
1538 {
1539 iface = pidConfigurationIface;
1540 if (!createNewObject &&
1541 pathItr->second.find(pidConfigurationIface) ==
1542 pathItr->second.end())
1543 {
1544 createNewObject = true;
1545 }
1546 }
1547 else if (type == "FanZones")
1548 {
1549 iface = pidZoneConfigurationIface;
1550 if (!createNewObject &&
1551 pathItr->second.find(pidZoneConfigurationIface) ==
1552 pathItr->second.end())
1553 {
1554
1555 createNewObject = true;
1556 }
1557 }
1558 else if (type == "StepwiseControllers")
1559 {
1560 iface = stepwiseConfigurationIface;
1561 if (!createNewObject &&
1562 pathItr->second.find(stepwiseConfigurationIface) ==
1563 pathItr->second.end())
1564 {
1565 createNewObject = true;
1566 }
1567 }
James Feist6ee7f772020-02-06 16:25:27 -08001568
1569 if (createNewObject && it.value() == nullptr)
1570 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -05001571 // can't delete a non-existent object
James Feist6ee7f772020-02-06 16:25:27 -08001572 messages::invalidObject(response->res, name);
1573 continue;
1574 }
1575
1576 std::string path;
1577 if (pathItr != managedObj.end())
1578 {
1579 path = pathItr->first.str;
1580 }
1581
James Feist73df0db2019-03-25 15:29:35 -07001582 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
James Feiste69d9de2020-02-07 12:23:27 -08001583
1584 // arbitrary limit to avoid attacks
1585 constexpr const size_t controllerLimit = 500;
James Feist14b0b8d2020-02-12 11:52:07 -08001586 if (createNewObject && objectCount >= controllerLimit)
James Feiste69d9de2020-02-07 12:23:27 -08001587 {
1588 messages::resourceExhaustion(response->res, type);
1589 continue;
1590 }
1591
James Feist73df0db2019-03-25 15:29:35 -07001592 output["Name"] = boost::replace_all_copy(name, "_", " ");
1593
1594 std::string chassis;
1595 CreatePIDRet ret = createPidInterface(
James Feist6ee7f772020-02-06 16:25:27 -08001596 response, type, it, path, managedObj, createNewObject,
1597 output, chassis, currentProfile);
James Feist73df0db2019-03-25 15:29:35 -07001598 if (ret == CreatePIDRet::fail)
1599 {
1600 return;
1601 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001602 if (ret == CreatePIDRet::del)
James Feist73df0db2019-03-25 15:29:35 -07001603 {
1604 continue;
1605 }
1606
1607 if (!createNewObject)
1608 {
1609 for (const auto& property : output)
1610 {
1611 crow::connections::systemBus->async_method_call(
1612 [response,
1613 propertyName{std::string(property.first)}](
1614 const boost::system::error_code ec) {
1615 if (ec)
1616 {
1617 BMCWEB_LOG_ERROR << "Error patching "
1618 << propertyName << ": "
1619 << ec;
1620 messages::internalError(response->res);
1621 return;
1622 }
1623 messages::success(response->res);
1624 },
James Feist6ee7f772020-02-06 16:25:27 -08001625 "xyz.openbmc_project.EntityManager", path,
James Feist73df0db2019-03-25 15:29:35 -07001626 "org.freedesktop.DBus.Properties", "Set", iface,
1627 property.first, property.second);
1628 }
1629 }
1630 else
1631 {
1632 if (chassis.empty())
1633 {
1634 BMCWEB_LOG_ERROR << "Failed to get chassis from config";
1635 messages::invalidObject(response->res, name);
1636 return;
1637 }
1638
1639 bool foundChassis = false;
1640 for (const auto& obj : managedObj)
1641 {
1642 if (boost::algorithm::ends_with(obj.first.str, chassis))
1643 {
1644 chassis = obj.first.str;
1645 foundChassis = true;
1646 break;
1647 }
1648 }
1649 if (!foundChassis)
1650 {
1651 BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
1652 messages::resourceMissingAtURI(
1653 response->res, "/redfish/v1/Chassis/" + chassis);
1654 return;
1655 }
1656
1657 crow::connections::systemBus->async_method_call(
1658 [response](const boost::system::error_code ec) {
1659 if (ec)
1660 {
1661 BMCWEB_LOG_ERROR << "Error Adding Pid Object "
1662 << ec;
1663 messages::internalError(response->res);
1664 return;
1665 }
1666 messages::success(response->res);
1667 },
1668 "xyz.openbmc_project.EntityManager", chassis,
1669 "xyz.openbmc_project.AddObject", "AddObject", output);
1670 }
1671 }
1672 }
1673 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001674 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001675 std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
1676 configuration;
1677 std::optional<std::string> profile;
1678 dbus::utility::ManagedObjectType managedObj;
1679 std::vector<std::string> supportedProfiles;
1680 std::string currentProfile;
1681 std::string profileConnection;
1682 std::string profilePath;
James Feist14b0b8d2020-02-12 11:52:07 -08001683 size_t objectCount = 0;
James Feist73df0db2019-03-25 15:29:35 -07001684};
James Feist83ff9ab2018-08-31 10:18:24 -07001685
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001686/**
1687 * @brief Retrieves BMC manager location data over DBus
1688 *
1689 * @param[in] aResp Shared pointer for completing asynchronous calls
1690 * @param[in] connectionName - service name
1691 * @param[in] path - object path
1692 * @return none
1693 */
zhanghch058d1b46d2021-04-01 11:18:24 +08001694inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001695 const std::string& connectionName,
1696 const std::string& path)
1697{
1698 BMCWEB_LOG_DEBUG << "Get BMC manager Location data.";
1699
1700 crow::connections::systemBus->async_method_call(
1701 [aResp](const boost::system::error_code ec,
1702 const std::variant<std::string>& property) {
1703 if (ec)
1704 {
1705 BMCWEB_LOG_DEBUG << "DBUS response error for "
1706 "Location";
1707 messages::internalError(aResp->res);
1708 return;
1709 }
1710
1711 const std::string* value = std::get_if<std::string>(&property);
1712
1713 if (value == nullptr)
1714 {
1715 // illegal value
1716 messages::internalError(aResp->res);
1717 return;
1718 }
1719
1720 aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
1721 *value;
1722 },
1723 connectionName, path, "org.freedesktop.DBus.Properties", "Get",
1724 "xyz.openbmc_project.Inventory.Decorator."
1725 "LocationCode",
1726 "LocationCode");
1727}
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001728// avoid name collision systems.hpp
1729inline void
1730 managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001731{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001732 BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time";
Ed Tanous52cc1122020-07-18 13:51:21 -07001733
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001734 crow::connections::systemBus->async_method_call(
1735 [aResp](const boost::system::error_code ec,
1736 std::variant<uint64_t>& lastResetTime) {
1737 if (ec)
James Feist83ff9ab2018-08-31 10:18:24 -07001738 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001739 BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
Ed Tanous43b761d2019-02-13 20:10:56 -08001740 return;
1741 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001742
1743 const uint64_t* lastResetTimePtr =
1744 std::get_if<uint64_t>(&lastResetTime);
1745
1746 if (!lastResetTimePtr)
Ed Tanous43b761d2019-02-13 20:10:56 -08001747 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001748 messages::internalError(aResp->res);
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001749 return;
1750 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001751 // LastRebootTime is epoch time, in milliseconds
1752 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
1753 time_t lastResetTimeStamp =
1754 static_cast<time_t>(*lastResetTimePtr / 1000);
1755
1756 // Convert to ISO 8601 standard
1757 aResp->res.jsonValue["LastResetTime"] =
1758 crow::utility::getDateTime(lastResetTimeStamp);
1759 },
1760 "xyz.openbmc_project.State.BMC", "/xyz/openbmc_project/state/bmc0",
1761 "org.freedesktop.DBus.Properties", "Get",
1762 "xyz.openbmc_project.State.BMC", "LastRebootTime");
1763}
1764
1765/**
1766 * @brief Set the running firmware image
1767 *
1768 * @param[i,o] aResp - Async response object
1769 * @param[i] runningFirmwareTarget - Image to make the running image
1770 *
1771 * @return void
1772 */
1773inline void
1774 setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1775 const std::string& runningFirmwareTarget)
1776{
1777 // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1778 std::string::size_type idPos = runningFirmwareTarget.rfind('/');
1779 if (idPos == std::string::npos)
1780 {
1781 messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
1782 "@odata.id");
1783 BMCWEB_LOG_DEBUG << "Can't parse firmware ID!";
1784 return;
1785 }
1786 idPos++;
1787 if (idPos >= runningFirmwareTarget.size())
1788 {
1789 messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
1790 "@odata.id");
1791 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
1792 return;
1793 }
1794 std::string firmwareId = runningFirmwareTarget.substr(idPos);
1795
1796 // Make sure the image is valid before setting priority
1797 crow::connections::systemBus->async_method_call(
1798 [aResp, firmwareId, runningFirmwareTarget](
1799 const boost::system::error_code ec, ManagedObjectType& subtree) {
1800 if (ec)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001801 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001802 BMCWEB_LOG_DEBUG << "D-Bus response error getting objects.";
1803 messages::internalError(aResp->res);
1804 return;
1805 }
1806
1807 if (subtree.size() == 0)
1808 {
1809 BMCWEB_LOG_DEBUG << "Can't find image!";
1810 messages::internalError(aResp->res);
1811 return;
1812 }
1813
1814 bool foundImage = false;
1815 for (auto& object : subtree)
1816 {
1817 const std::string& path =
1818 static_cast<const std::string&>(object.first);
1819 std::size_t idPos2 = path.rfind('/');
1820
1821 if (idPos2 == std::string::npos)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001822 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001823 continue;
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001824 }
1825
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001826 idPos2++;
1827 if (idPos2 >= path.size())
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001828 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001829 continue;
1830 }
1831
1832 if (path.substr(idPos2) == firmwareId)
1833 {
1834 foundImage = true;
1835 break;
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001836 }
1837 }
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301838
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001839 if (!foundImage)
1840 {
1841 messages::propertyValueNotInList(
1842 aResp->res, runningFirmwareTarget, "@odata.id");
1843 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
1844 return;
1845 }
Gunnar Mills4bf2b032020-06-23 22:28:31 -05001846
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001847 BMCWEB_LOG_DEBUG
1848 << "Setting firmware version " + firmwareId + " to priority 0.";
Gunnar Mills4bf2b032020-06-23 22:28:31 -05001849
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001850 // Only support Immediate
1851 // An addition could be a Redfish Setting like
1852 // ActiveSoftwareImageApplyTime and support OnReset
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301853 crow::connections::systemBus->async_method_call(
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001854 [aResp](const boost::system::error_code ec) {
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301855 if (ec)
1856 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001857 BMCWEB_LOG_DEBUG << "D-Bus response error setting.";
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301858 messages::internalError(aResp->res);
1859 return;
1860 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001861 doBMCGracefulRestart(aResp);
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301862 },
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001863
1864 "xyz.openbmc_project.Software.BMC.Updater",
1865 "/xyz/openbmc_project/software/" + firmwareId,
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301866 "org.freedesktop.DBus.Properties", "Set",
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001867 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1868 std::variant<uint8_t>(static_cast<uint8_t>(0)));
1869 },
1870 "xyz.openbmc_project.Software.BMC.Updater",
1871 "/xyz/openbmc_project/software", "org.freedesktop.DBus.ObjectManager",
1872 "GetManagedObjects");
1873}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001874
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001875inline void setDateTime(std::shared_ptr<bmcweb::AsyncResp> aResp,
1876 std::string datetime)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001877{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001878 BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001879
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001880 std::stringstream stream(datetime);
1881 // Convert from ISO 8601 to boost local_time
1882 // (BMC only has time in UTC)
1883 boost::posix_time::ptime posixTime;
1884 boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
1885 // Facet gets deleted with the stringsteam
1886 auto ifc = std::make_unique<boost::local_time::local_time_input_facet>(
1887 "%Y-%m-%d %H:%M:%S%F %ZP");
1888 stream.imbue(std::locale(stream.getloc(), ifc.release()));
1889
1890 boost::local_time::local_date_time ldt(boost::local_time::not_a_date_time);
1891
1892 if (stream >> ldt)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001893 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001894 posixTime = ldt.utc_time();
1895 boost::posix_time::time_duration dur = posixTime - epoch;
1896 uint64_t durMicroSecs = static_cast<uint64_t>(dur.total_microseconds());
1897 crow::connections::systemBus->async_method_call(
1898 [aResp{std::move(aResp)}, datetime{std::move(datetime)}](
1899 const boost::system::error_code ec) {
1900 if (ec)
1901 {
1902 BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1903 "DBUS response error "
1904 << ec;
1905 messages::internalError(aResp->res);
1906 return;
1907 }
1908 aResp->res.jsonValue["DateTime"] = datetime;
1909 },
1910 "xyz.openbmc_project.Time.Manager", "/xyz/openbmc_project/time/bmc",
1911 "org.freedesktop.DBus.Properties", "Set",
1912 "xyz.openbmc_project.Time.EpochTime", "Elapsed",
1913 std::variant<uint64_t>(durMicroSecs));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001914 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001915 else
1916 {
1917 messages::propertyValueFormatError(aResp->res, datetime, "DateTime");
1918 return;
1919 }
1920}
1921
1922inline void requestRoutesManager(App& app)
1923{
1924 std::string uuid = persistent_data::getConfig().systemUuid;
1925
1926 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07001927 .privileges(redfish::privileges::getManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001928 .methods(boost::beast::http::verb::get)([uuid](const crow::Request&,
1929 const std::shared_ptr<
1930 bmcweb::AsyncResp>&
1931 asyncResp) {
1932 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
1933 asyncResp->res.jsonValue["@odata.type"] =
1934 "#Manager.v1_11_0.Manager";
1935 asyncResp->res.jsonValue["Id"] = "bmc";
1936 asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
1937 asyncResp->res.jsonValue["Description"] =
1938 "Baseboard Management Controller";
1939 asyncResp->res.jsonValue["PowerState"] = "On";
1940 asyncResp->res.jsonValue["Status"] = {{"State", "Enabled"},
1941 {"Health", "OK"}};
1942 asyncResp->res.jsonValue["ManagerType"] = "BMC";
1943 asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
1944 asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1945 asyncResp->res.jsonValue["Model"] =
1946 "OpenBmc"; // TODO(ed), get model
1947
1948 asyncResp->res.jsonValue["LogServices"] = {
1949 {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}};
1950
1951 asyncResp->res.jsonValue["NetworkProtocol"] = {
1952 {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}};
1953
1954 asyncResp->res.jsonValue["EthernetInterfaces"] = {
1955 {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}};
1956
1957#ifdef BMCWEB_ENABLE_VM_NBDPROXY
1958 asyncResp->res.jsonValue["VirtualMedia"] = {
1959 {"@odata.id", "/redfish/v1/Managers/bmc/VirtualMedia"}};
1960#endif // BMCWEB_ENABLE_VM_NBDPROXY
1961
1962 // default oem data
1963 nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
1964 nlohmann::json& oemOpenbmc = oem["OpenBmc"];
1965 oem["@odata.type"] = "#OemManager.Oem";
1966 oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
1967 oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
1968 oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
1969 oemOpenbmc["Certificates"] = {
1970 {"@odata.id",
1971 "/redfish/v1/Managers/bmc/Truststore/Certificates"}};
1972
1973 // Manager.Reset (an action) can be many values, OpenBMC only
1974 // supports BMC reboot.
1975 nlohmann::json& managerReset =
1976 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
1977 managerReset["target"] =
1978 "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
1979 managerReset["@Redfish.ActionInfo"] =
1980 "/redfish/v1/Managers/bmc/ResetActionInfo";
1981
1982 // ResetToDefaults (Factory Reset) has values like
1983 // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
1984 // on OpenBMC
1985 nlohmann::json& resetToDefaults =
1986 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
1987 resetToDefaults["target"] =
1988 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
1989 resetToDefaults["ResetType@Redfish.AllowableValues"] = {"ResetAll"};
1990
Tejas Patil7c8c4052021-06-04 17:43:14 +05301991 std::pair<std::string, std::string> redfishDateTimeOffset =
1992 crow::utility::getDateTimeOffsetNow();
1993
1994 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1995 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1996 redfishDateTimeOffset.second;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001997
Gunnar Mills0e8ac5e2020-11-06 15:33:24 -06001998 // TODO (Gunnar): Remove these one day since moved to ComputerSystem
1999 // Still used by OCP profiles
2000 // https://github.com/opencomputeproject/OCP-Profiles/issues/23
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002001 // Fill in SerialConsole info
2002 asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
2003 asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] =
2004 15;
2005 asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
2006 {"IPMI", "SSH"};
2007#ifdef BMCWEB_ENABLE_KVM
2008 // Fill in GraphicalConsole info
2009 asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] =
2010 true;
2011 asyncResp->res
2012 .jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4;
2013 asyncResp->res.jsonValue["GraphicalConsole"]
2014 ["ConnectTypesSupported"] = {"KVMIP"};
2015#endif // BMCWEB_ENABLE_KVM
2016
2017 asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] =
2018 1;
2019 asyncResp->res.jsonValue["Links"]["ManagerForServers"] = {
2020 {{"@odata.id", "/redfish/v1/Systems/system"}}};
2021
2022 auto health = std::make_shared<HealthPopulate>(asyncResp);
2023 health->isManagersHealth = true;
2024 health->populate();
2025
2026 fw_util::populateFirmwareInformation(asyncResp, fw_util::bmcPurpose,
2027 "FirmwareVersion", true);
2028
2029 managerGetLastResetTime(asyncResp);
2030
2031 auto pids = std::make_shared<GetPIDValues>(asyncResp);
2032 pids->run();
2033
2034 getMainChassisId(
2035 asyncResp, [](const std::string& chassisId,
2036 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2037 aRsp->res
2038 .jsonValue["Links"]["ManagerForChassis@odata.count"] =
2039 1;
2040 aRsp->res.jsonValue["Links"]["ManagerForChassis"] = {
2041 {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
2042 aRsp->res.jsonValue["Links"]["ManagerInChassis"] = {
2043 {"@odata.id", "/redfish/v1/Chassis/" + chassisId}};
2044 });
2045
2046 static bool started = false;
2047
2048 if (!started)
2049 {
2050 crow::connections::systemBus->async_method_call(
2051 [asyncResp](const boost::system::error_code ec,
2052 const std::variant<double>& resp) {
2053 if (ec)
2054 {
2055 BMCWEB_LOG_ERROR << "Error while getting progress";
2056 messages::internalError(asyncResp->res);
2057 return;
2058 }
2059 const double* val = std::get_if<double>(&resp);
2060 if (val == nullptr)
2061 {
2062 BMCWEB_LOG_ERROR
2063 << "Invalid response while getting progress";
2064 messages::internalError(asyncResp->res);
2065 return;
2066 }
2067 if (*val < 1.0)
2068 {
2069 asyncResp->res.jsonValue["Status"]["State"] =
2070 "Starting";
2071 started = true;
2072 }
2073 },
2074 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
2075 "org.freedesktop.DBus.Properties", "Get",
2076 "org.freedesktop.systemd1.Manager", "Progress");
2077 }
2078
2079 crow::connections::systemBus->async_method_call(
2080 [asyncResp](
2081 const boost::system::error_code ec,
2082 const std::vector<
2083 std::pair<std::string,
2084 std::vector<std::pair<
2085 std::string, std::vector<std::string>>>>>&
2086 subtree) {
2087 if (ec)
2088 {
2089 BMCWEB_LOG_DEBUG
2090 << "D-Bus response error on GetSubTree " << ec;
2091 return;
2092 }
2093 if (subtree.size() == 0)
2094 {
2095 BMCWEB_LOG_DEBUG << "Can't find bmc D-Bus object!";
2096 return;
2097 }
2098 // Assume only 1 bmc D-Bus object
2099 // Throw an error if there is more than 1
2100 if (subtree.size() > 1)
2101 {
2102 BMCWEB_LOG_DEBUG
2103 << "Found more than 1 bmc D-Bus object!";
2104 messages::internalError(asyncResp->res);
2105 return;
2106 }
2107
2108 if (subtree[0].first.empty() ||
2109 subtree[0].second.size() != 1)
2110 {
2111 BMCWEB_LOG_DEBUG << "Error getting bmc D-Bus object!";
2112 messages::internalError(asyncResp->res);
2113 return;
2114 }
2115
2116 const std::string& path = subtree[0].first;
2117 const std::string& connectionName =
2118 subtree[0].second[0].first;
2119
2120 for (const auto& interfaceName :
2121 subtree[0].second[0].second)
2122 {
2123 if (interfaceName ==
2124 "xyz.openbmc_project.Inventory.Decorator.Asset")
2125 {
2126 crow::connections::systemBus->async_method_call(
2127 [asyncResp](
2128 const boost::system::error_code ec,
2129 const std::vector<
2130 std::pair<std::string,
2131 std::variant<std::string>>>&
2132 propertiesList) {
2133 if (ec)
2134 {
2135 BMCWEB_LOG_DEBUG
2136 << "Can't get bmc asset!";
2137 return;
2138 }
2139 for (const std::pair<
2140 std::string,
2141 std::variant<std::string>>&
2142 property : propertiesList)
2143 {
2144 const std::string& propertyName =
2145 property.first;
2146
2147 if ((propertyName == "PartNumber") ||
2148 (propertyName == "SerialNumber") ||
2149 (propertyName == "Manufacturer") ||
2150 (propertyName == "Model") ||
2151 (propertyName == "SparePartNumber"))
2152 {
2153 const std::string* value =
2154 std::get_if<std::string>(
2155 &property.second);
2156 if (value == nullptr)
2157 {
2158 // illegal property
2159 messages::internalError(
2160 asyncResp->res);
2161 return;
2162 }
2163 asyncResp->res
2164 .jsonValue[propertyName] =
2165 *value;
2166 }
2167 }
2168 },
2169 connectionName, path,
2170 "org.freedesktop.DBus.Properties", "GetAll",
2171 "xyz.openbmc_project.Inventory.Decorator."
2172 "Asset");
2173 }
2174 else if (interfaceName ==
2175 "xyz.openbmc_project.Inventory."
2176 "Decorator.LocationCode")
2177 {
2178 getLocation(asyncResp, connectionName, path);
2179 }
2180 }
2181 },
2182 "xyz.openbmc_project.ObjectMapper",
2183 "/xyz/openbmc_project/object_mapper",
2184 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2185 "/xyz/openbmc_project/inventory", int32_t(0),
2186 std::array<const char*, 1>{
2187 "xyz.openbmc_project.Inventory.Item.Bmc"});
2188 });
2189
2190 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07002191 .privileges(redfish::privileges::patchManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002192 .methods(
2193 boost::beast::http::verb::
2194 patch)([](const crow::Request& req,
2195 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2196 std::optional<nlohmann::json> oem;
2197 std::optional<nlohmann::json> links;
2198 std::optional<std::string> datetime;
2199
2200 if (!json_util::readJson(req, asyncResp->res, "Oem", oem,
2201 "DateTime", datetime, "Links", links))
2202 {
2203 return;
2204 }
2205
2206 if (oem)
2207 {
2208 std::optional<nlohmann::json> openbmc;
2209 if (!redfish::json_util::readJson(*oem, asyncResp->res,
2210 "OpenBmc", openbmc))
2211 {
2212 BMCWEB_LOG_ERROR
2213 << "Illegal Property "
2214 << oem->dump(2, ' ', true,
2215 nlohmann::json::error_handler_t::replace);
2216 return;
2217 }
2218 if (openbmc)
2219 {
2220 std::optional<nlohmann::json> fan;
2221 if (!redfish::json_util::readJson(*openbmc, asyncResp->res,
2222 "Fan", fan))
2223 {
2224 BMCWEB_LOG_ERROR
2225 << "Illegal Property "
2226 << openbmc->dump(
2227 2, ' ', true,
2228 nlohmann::json::error_handler_t::replace);
2229 return;
2230 }
2231 if (fan)
2232 {
2233 auto pid =
2234 std::make_shared<SetPIDValues>(asyncResp, *fan);
2235 pid->run();
2236 }
2237 }
2238 }
2239 if (links)
2240 {
2241 std::optional<nlohmann::json> activeSoftwareImage;
2242 if (!redfish::json_util::readJson(*links, asyncResp->res,
2243 "ActiveSoftwareImage",
2244 activeSoftwareImage))
2245 {
2246 return;
2247 }
2248 if (activeSoftwareImage)
2249 {
2250 std::optional<std::string> odataId;
2251 if (!json_util::readJson(*activeSoftwareImage,
2252 asyncResp->res, "@odata.id",
2253 odataId))
2254 {
2255 return;
2256 }
2257
2258 if (odataId)
2259 {
2260 setActiveFirmwareImage(asyncResp, *odataId);
2261 }
2262 }
2263 }
2264 if (datetime)
2265 {
2266 setDateTime(asyncResp, std::move(*datetime));
2267 }
2268 });
2269}
2270
2271inline void requestRoutesManagerCollection(App& app)
2272{
2273 BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
Ed Tanoused398212021-06-09 17:05:54 -07002274 .privileges(redfish::privileges::getManagerCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002275 .methods(boost::beast::http::verb::get)(
2276 [](const crow::Request&,
2277 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2278 // Collections don't include the static data added by SubRoute
2279 // because it has a duplicate entry for members
2280 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
2281 asyncResp->res.jsonValue["@odata.type"] =
2282 "#ManagerCollection.ManagerCollection";
2283 asyncResp->res.jsonValue["Name"] = "Manager Collection";
2284 asyncResp->res.jsonValue["Members@odata.count"] = 1;
2285 asyncResp->res.jsonValue["Members"] = {
2286 {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
2287 });
2288}
Ed Tanous1abe55e2018-09-05 08:30:59 -07002289} // namespace redfish