blob: ca5d8c5460cef8fb23df54c53e1b860abeaf6a0e [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 {
George Liu0fda0f12021-11-16 10:06:17 +0800190 BMCWEB_LOG_DEBUG
191 << "Invalid property value for ResetToDefaultsType: "
192 << resetType;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700193 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";
George Liu0fda0f12021-11-16 10:06:17 +0800279 fans["@odata.id"] =
280 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers";
James Feist5b4aa862018-08-16 14:07:01 -0700281
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}};
George Liu0fda0f12021-11-16 10:06:17 +0800376 zone["@odata.id"] =
377 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/" +
378 name;
James Feist5b4aa862018-08-16 14:07:01 -0700379 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"] =
George Liu0fda0f12021-11-16 10:06:17 +0800396 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers/" +
Ed Tanous271584a2019-07-09 16:24:22 -0700397 name;
James Feistb7a08d02018-12-11 14:55:37 -0800398 controller["@odata.type"] =
399 "#OemManager.StepwiseController";
400
James Feistc33a90e2019-03-01 10:17:44 -0800401 controller["Direction"] = *classPtr;
James Feistb7a08d02018-12-11 14:55:37 -0800402 }
403
404 // pid and fans are off the same configuration
405 else if (intfPair.first == pidConfigurationIface)
406 {
James Feistc33a90e2019-03-01 10:17:44 -0800407
James Feistb7a08d02018-12-11 14:55:37 -0800408 if (classPtr == nullptr)
409 {
410 BMCWEB_LOG_ERROR << "Pid Class Field illegal";
411 messages::internalError(asyncResp->res);
412 return;
413 }
414 bool isFan = *classPtr == "fan";
415 nlohmann::json& element =
416 isFan ? fans[name] : pids[name];
417 config = &element;
418 if (isFan)
419 {
420 element["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +0800421 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers/" +
Ed Tanous271584a2019-07-09 16:24:22 -0700422 name;
James Feistb7a08d02018-12-11 14:55:37 -0800423 element["@odata.type"] =
424 "#OemManager.FanController";
James Feistb7a08d02018-12-11 14:55:37 -0800425 }
426 else
427 {
428 element["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +0800429 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers/" +
Ed Tanous271584a2019-07-09 16:24:22 -0700430 name;
James Feistb7a08d02018-12-11 14:55:37 -0800431 element["@odata.type"] =
432 "#OemManager.PidController";
James Feistb7a08d02018-12-11 14:55:37 -0800433 }
434 }
435 else
436 {
437 BMCWEB_LOG_ERROR << "Unexpected configuration";
438 messages::internalError(asyncResp->res);
439 return;
440 }
441
442 // used for making maps out of 2 vectors
443 const std::vector<double>* keys = nullptr;
444 const std::vector<double>* values = nullptr;
445
James Feist5b4aa862018-08-16 14:07:01 -0700446 for (const auto& propertyPair : intfPair.second)
447 {
448 if (propertyPair.first == "Type" ||
449 propertyPair.first == "Class" ||
450 propertyPair.first == "Name")
451 {
452 continue;
453 }
454
455 // zones
456 if (intfPair.first == pidZoneConfigurationIface)
457 {
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800458 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800459 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700460 if (ptr == nullptr)
461 {
462 BMCWEB_LOG_ERROR << "Field Illegal "
463 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700464 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700465 return;
466 }
James Feistb7a08d02018-12-11 14:55:37 -0800467 (*config)[propertyPair.first] = *ptr;
468 }
469
470 if (intfPair.first == stepwiseConfigurationIface)
471 {
472 if (propertyPair.first == "Reading" ||
473 propertyPair.first == "Output")
474 {
475 const std::vector<double>* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800476 std::get_if<std::vector<double>>(
James Feistb7a08d02018-12-11 14:55:37 -0800477 &propertyPair.second);
478
479 if (ptr == nullptr)
480 {
481 BMCWEB_LOG_ERROR << "Field Illegal "
482 << propertyPair.first;
483 messages::internalError(asyncResp->res);
484 return;
485 }
486
487 if (propertyPair.first == "Reading")
488 {
489 keys = ptr;
490 }
491 else
492 {
493 values = ptr;
494 }
495 if (keys && values)
496 {
497 if (keys->size() != values->size())
498 {
499 BMCWEB_LOG_ERROR
George Liu0fda0f12021-11-16 10:06:17 +0800500 << "Reading and Output size don't match ";
James Feistb7a08d02018-12-11 14:55:37 -0800501 messages::internalError(asyncResp->res);
502 return;
503 }
504 nlohmann::json& steps = (*config)["Steps"];
505 steps = nlohmann::json::array();
506 for (size_t ii = 0; ii < keys->size(); ii++)
507 {
508 steps.push_back(
509 {{"Target", (*keys)[ii]},
510 {"Output", (*values)[ii]}});
511 }
512 }
513 }
514 if (propertyPair.first == "NegativeHysteresis" ||
515 propertyPair.first == "PositiveHysteresis")
516 {
517 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800518 std::get_if<double>(&propertyPair.second);
James Feistb7a08d02018-12-11 14:55:37 -0800519 if (ptr == nullptr)
520 {
521 BMCWEB_LOG_ERROR << "Field Illegal "
522 << propertyPair.first;
523 messages::internalError(asyncResp->res);
524 return;
525 }
526 (*config)[propertyPair.first] = *ptr;
527 }
James Feist5b4aa862018-08-16 14:07:01 -0700528 }
529
530 // pid and fans are off the same configuration
James Feistb7a08d02018-12-11 14:55:37 -0800531 if (intfPair.first == pidConfigurationIface ||
532 intfPair.first == stepwiseConfigurationIface)
James Feist5b4aa862018-08-16 14:07:01 -0700533 {
James Feist5b4aa862018-08-16 14:07:01 -0700534
535 if (propertyPair.first == "Zones")
536 {
537 const std::vector<std::string>* inputs =
Ed Tanousabf2add2019-01-22 16:40:12 -0800538 std::get_if<std::vector<std::string>>(
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800539 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700540
541 if (inputs == nullptr)
542 {
543 BMCWEB_LOG_ERROR
544 << "Zones Pid Field Illegal";
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800545 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700546 return;
547 }
James Feistb7a08d02018-12-11 14:55:37 -0800548 auto& data = (*config)[propertyPair.first];
James Feist5b4aa862018-08-16 14:07:01 -0700549 data = nlohmann::json::array();
550 for (std::string itemCopy : *inputs)
551 {
552 dbus::utility::escapePathForDbus(itemCopy);
553 data.push_back(
554 {{"@odata.id",
George Liu0fda0f12021-11-16 10:06:17 +0800555 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/" +
James Feist5b4aa862018-08-16 14:07:01 -0700556 itemCopy}});
557 }
558 }
559 // todo(james): may never happen, but this
560 // assumes configuration data referenced in the
561 // PID config is provided by the same daemon, we
562 // could add another loop to cover all cases,
563 // but I'm okay kicking this can down the road a
564 // bit
565
566 else if (propertyPair.first == "Inputs" ||
567 propertyPair.first == "Outputs")
568 {
James Feistb7a08d02018-12-11 14:55:37 -0800569 auto& data = (*config)[propertyPair.first];
James Feist5b4aa862018-08-16 14:07:01 -0700570 const std::vector<std::string>* inputs =
Ed Tanousabf2add2019-01-22 16:40:12 -0800571 std::get_if<std::vector<std::string>>(
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800572 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700573
574 if (inputs == nullptr)
575 {
576 BMCWEB_LOG_ERROR << "Field Illegal "
577 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700578 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700579 return;
580 }
581 data = *inputs;
James Feistb943aae2019-07-11 16:33:56 -0700582 }
583 else if (propertyPair.first == "SetPointOffset")
584 {
585 const std::string* ptr =
586 std::get_if<std::string>(
587 &propertyPair.second);
588
589 if (ptr == nullptr)
590 {
591 BMCWEB_LOG_ERROR << "Field Illegal "
592 << propertyPair.first;
593 messages::internalError(asyncResp->res);
594 return;
595 }
596 // translate from dbus to redfish
597 if (*ptr == "WarningHigh")
598 {
599 (*config)["SetPointOffset"] =
600 "UpperThresholdNonCritical";
601 }
602 else if (*ptr == "WarningLow")
603 {
604 (*config)["SetPointOffset"] =
605 "LowerThresholdNonCritical";
606 }
607 else if (*ptr == "CriticalHigh")
608 {
609 (*config)["SetPointOffset"] =
610 "UpperThresholdCritical";
611 }
612 else if (*ptr == "CriticalLow")
613 {
614 (*config)["SetPointOffset"] =
615 "LowerThresholdCritical";
616 }
617 else
618 {
619 BMCWEB_LOG_ERROR << "Value Illegal "
620 << *ptr;
621 messages::internalError(asyncResp->res);
622 return;
623 }
624 }
625 // doubles
James Feist5b4aa862018-08-16 14:07:01 -0700626 else if (propertyPair.first ==
627 "FFGainCoefficient" ||
628 propertyPair.first == "FFOffCoefficient" ||
629 propertyPair.first == "ICoefficient" ||
630 propertyPair.first == "ILimitMax" ||
631 propertyPair.first == "ILimitMin" ||
James Feistaad1a252019-02-19 10:13:52 -0800632 propertyPair.first ==
633 "PositiveHysteresis" ||
634 propertyPair.first ==
635 "NegativeHysteresis" ||
James Feist5b4aa862018-08-16 14:07:01 -0700636 propertyPair.first == "OutLimitMax" ||
637 propertyPair.first == "OutLimitMin" ||
638 propertyPair.first == "PCoefficient" ||
James Feist7625cb82019-01-23 11:58:21 -0800639 propertyPair.first == "SetPoint" ||
James Feist5b4aa862018-08-16 14:07:01 -0700640 propertyPair.first == "SlewNeg" ||
641 propertyPair.first == "SlewPos")
642 {
643 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800644 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700645 if (ptr == nullptr)
646 {
647 BMCWEB_LOG_ERROR << "Field Illegal "
648 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700649 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700650 return;
651 }
James Feistb7a08d02018-12-11 14:55:37 -0800652 (*config)[propertyPair.first] = *ptr;
James Feist5b4aa862018-08-16 14:07:01 -0700653 }
654 }
655 }
656 }
657 }
658 },
659 connection, path, objectManagerIface, "GetManagedObjects");
660}
Jennifer Leeca537922018-08-10 10:07:30 -0700661
James Feist83ff9ab2018-08-31 10:18:24 -0700662enum class CreatePIDRet
663{
664 fail,
665 del,
666 patch
667};
668
zhanghch058d1b46d2021-04-01 11:18:24 +0800669inline bool
670 getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
671 std::vector<nlohmann::json>& config,
672 std::vector<std::string>& zones)
James Feist5f2caae2018-12-12 14:08:25 -0800673{
James Feistb6baeaa2019-02-21 10:41:40 -0800674 if (config.empty())
675 {
676 BMCWEB_LOG_ERROR << "Empty Zones";
677 messages::propertyValueFormatError(response->res,
678 nlohmann::json::array(), "Zones");
679 return false;
680 }
James Feist5f2caae2018-12-12 14:08:25 -0800681 for (auto& odata : config)
682 {
683 std::string path;
684 if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
685 path))
686 {
687 return false;
688 }
689 std::string input;
James Feist61adbda2019-03-25 13:03:51 -0700690
691 // 8 below comes from
692 // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
693 // 0 1 2 3 4 5 6 7 8
694 if (!dbus::utility::getNthStringFromPath(path, 8, input))
James Feist5f2caae2018-12-12 14:08:25 -0800695 {
696 BMCWEB_LOG_ERROR << "Got invalid path " << path;
697 BMCWEB_LOG_ERROR << "Illegal Type Zones";
698 messages::propertyValueFormatError(response->res, odata.dump(),
699 "Zones");
700 return false;
701 }
702 boost::replace_all(input, "_", " ");
703 zones.emplace_back(std::move(input));
704 }
705 return true;
706}
707
Ed Tanous23a21a12020-07-25 04:45:05 +0000708inline const dbus::utility::ManagedItem*
James Feist73df0db2019-03-25 15:29:35 -0700709 findChassis(const dbus::utility::ManagedObjectType& managedObj,
710 const std::string& value, std::string& chassis)
James Feistb6baeaa2019-02-21 10:41:40 -0800711{
712 BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
713
714 std::string escaped = boost::replace_all_copy(value, " ", "_");
715 escaped = "/" + escaped;
716 auto it = std::find_if(
717 managedObj.begin(), managedObj.end(), [&escaped](const auto& obj) {
718 if (boost::algorithm::ends_with(obj.first.str, escaped))
719 {
720 BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
721 return true;
722 }
723 return false;
724 });
725
726 if (it == managedObj.end())
727 {
James Feist73df0db2019-03-25 15:29:35 -0700728 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800729 }
730 // 5 comes from <chassis-name> being the 5th element
731 // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
James Feist73df0db2019-03-25 15:29:35 -0700732 if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
733 {
734 return &(*it);
735 }
736
737 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800738}
739
Ed Tanous23a21a12020-07-25 04:45:05 +0000740inline CreatePIDRet createPidInterface(
zhanghch058d1b46d2021-04-01 11:18:24 +0800741 const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
Ed Tanousb5a76932020-09-29 16:16:58 -0700742 const nlohmann::json::iterator& it, const std::string& path,
James Feist83ff9ab2018-08-31 10:18:24 -0700743 const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
744 boost::container::flat_map<std::string, dbus::utility::DbusVariantType>&
745 output,
James Feist73df0db2019-03-25 15:29:35 -0700746 std::string& chassis, const std::string& profile)
James Feist83ff9ab2018-08-31 10:18:24 -0700747{
748
James Feist5f2caae2018-12-12 14:08:25 -0800749 // common deleter
James Feistb6baeaa2019-02-21 10:41:40 -0800750 if (it.value() == nullptr)
James Feist5f2caae2018-12-12 14:08:25 -0800751 {
752 std::string iface;
753 if (type == "PidControllers" || type == "FanControllers")
754 {
755 iface = pidConfigurationIface;
756 }
757 else if (type == "FanZones")
758 {
759 iface = pidZoneConfigurationIface;
760 }
761 else if (type == "StepwiseControllers")
762 {
763 iface = stepwiseConfigurationIface;
764 }
765 else
766 {
Gunnar Millsa0744d32020-11-09 15:40:45 -0600767 BMCWEB_LOG_ERROR << "Illegal Type " << type;
James Feist5f2caae2018-12-12 14:08:25 -0800768 messages::propertyUnknown(response->res, type);
769 return CreatePIDRet::fail;
770 }
James Feist6ee7f772020-02-06 16:25:27 -0800771
772 BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
James Feist5f2caae2018-12-12 14:08:25 -0800773 // delete interface
774 crow::connections::systemBus->async_method_call(
775 [response, path](const boost::system::error_code ec) {
776 if (ec)
777 {
778 BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
779 messages::internalError(response->res);
James Feistb6baeaa2019-02-21 10:41:40 -0800780 return;
James Feist5f2caae2018-12-12 14:08:25 -0800781 }
James Feistb6baeaa2019-02-21 10:41:40 -0800782 messages::success(response->res);
James Feist5f2caae2018-12-12 14:08:25 -0800783 },
784 "xyz.openbmc_project.EntityManager", path, iface, "Delete");
785 return CreatePIDRet::del;
786 }
787
James Feist73df0db2019-03-25 15:29:35 -0700788 const dbus::utility::ManagedItem* managedItem = nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800789 if (!createNewObject)
790 {
791 // if we aren't creating a new object, we should be able to find it on
792 // d-bus
James Feist73df0db2019-03-25 15:29:35 -0700793 managedItem = findChassis(managedObj, it.key(), chassis);
794 if (managedItem == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800795 {
796 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
797 messages::invalidObject(response->res, it.key());
798 return CreatePIDRet::fail;
799 }
800 }
801
James Feist73df0db2019-03-25 15:29:35 -0700802 if (profile.size() &&
803 (type == "PidControllers" || type == "FanControllers" ||
804 type == "StepwiseControllers"))
805 {
806 if (managedItem == nullptr)
807 {
808 output["Profiles"] = std::vector<std::string>{profile};
809 }
810 else
811 {
812 std::string interface;
813 if (type == "StepwiseControllers")
814 {
815 interface = stepwiseConfigurationIface;
816 }
817 else
818 {
819 interface = pidConfigurationIface;
820 }
821 auto findConfig = managedItem->second.find(interface);
822 if (findConfig == managedItem->second.end())
823 {
824 BMCWEB_LOG_ERROR
825 << "Failed to find interface in managed object";
826 messages::internalError(response->res);
827 return CreatePIDRet::fail;
828 }
829 auto findProfiles = findConfig->second.find("Profiles");
830 if (findProfiles != findConfig->second.end())
831 {
832 const std::vector<std::string>* curProfiles =
833 std::get_if<std::vector<std::string>>(
834 &(findProfiles->second));
835 if (curProfiles == nullptr)
836 {
837 BMCWEB_LOG_ERROR << "Illegal profiles in managed object";
838 messages::internalError(response->res);
839 return CreatePIDRet::fail;
840 }
841 if (std::find(curProfiles->begin(), curProfiles->end(),
842 profile) == curProfiles->end())
843 {
844 std::vector<std::string> newProfiles = *curProfiles;
845 newProfiles.push_back(profile);
846 output["Profiles"] = newProfiles;
847 }
848 }
849 }
850 }
851
James Feist83ff9ab2018-08-31 10:18:24 -0700852 if (type == "PidControllers" || type == "FanControllers")
853 {
854 if (createNewObject)
855 {
856 output["Class"] = type == "PidControllers" ? std::string("temp")
857 : std::string("fan");
858 output["Type"] = std::string("Pid");
859 }
James Feist5f2caae2018-12-12 14:08:25 -0800860
861 std::optional<std::vector<nlohmann::json>> zones;
862 std::optional<std::vector<std::string>> inputs;
863 std::optional<std::vector<std::string>> outputs;
864 std::map<std::string, std::optional<double>> doubles;
James Feistb943aae2019-07-11 16:33:56 -0700865 std::optional<std::string> setpointOffset;
James Feist5f2caae2018-12-12 14:08:25 -0800866 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -0800867 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
James Feist5f2caae2018-12-12 14:08:25 -0800868 "Zones", zones, "FFGainCoefficient",
869 doubles["FFGainCoefficient"], "FFOffCoefficient",
870 doubles["FFOffCoefficient"], "ICoefficient",
871 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
872 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
873 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
874 "PCoefficient", doubles["PCoefficient"], "SetPoint",
James Feistb943aae2019-07-11 16:33:56 -0700875 doubles["SetPoint"], "SetPointOffset", setpointOffset,
876 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
877 "PositiveHysteresis", doubles["PositiveHysteresis"],
878 "NegativeHysteresis", doubles["NegativeHysteresis"]))
James Feist83ff9ab2018-08-31 10:18:24 -0700879 {
Ed Tanous71f52d92021-02-19 08:51:17 -0800880 BMCWEB_LOG_ERROR
881 << "Illegal Property "
882 << it.value().dump(2, ' ', true,
883 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -0800884 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700885 }
James Feist5f2caae2018-12-12 14:08:25 -0800886 if (zones)
James Feist83ff9ab2018-08-31 10:18:24 -0700887 {
James Feist5f2caae2018-12-12 14:08:25 -0800888 std::vector<std::string> zonesStr;
889 if (!getZonesFromJsonReq(response, *zones, zonesStr))
James Feist83ff9ab2018-08-31 10:18:24 -0700890 {
Gunnar Millsa0744d32020-11-09 15:40:45 -0600891 BMCWEB_LOG_ERROR << "Illegal Zones";
James Feist5f2caae2018-12-12 14:08:25 -0800892 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700893 }
James Feistb6baeaa2019-02-21 10:41:40 -0800894 if (chassis.empty() &&
895 !findChassis(managedObj, zonesStr[0], chassis))
896 {
897 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
898 messages::invalidObject(response->res, it.key());
899 return CreatePIDRet::fail;
900 }
901
James Feist5f2caae2018-12-12 14:08:25 -0800902 output["Zones"] = std::move(zonesStr);
903 }
904 if (inputs || outputs)
905 {
906 std::array<std::optional<std::vector<std::string>>*, 2> containers =
907 {&inputs, &outputs};
908 size_t index = 0;
909 for (const auto& containerPtr : containers)
James Feist83ff9ab2018-08-31 10:18:24 -0700910 {
James Feist5f2caae2018-12-12 14:08:25 -0800911 std::optional<std::vector<std::string>>& container =
912 *containerPtr;
913 if (!container)
James Feist83ff9ab2018-08-31 10:18:24 -0700914 {
James Feist5f2caae2018-12-12 14:08:25 -0800915 index++;
916 continue;
James Feist83ff9ab2018-08-31 10:18:24 -0700917 }
James Feist5f2caae2018-12-12 14:08:25 -0800918
919 for (std::string& value : *container)
James Feist83ff9ab2018-08-31 10:18:24 -0700920 {
James Feist5f2caae2018-12-12 14:08:25 -0800921 boost::replace_all(value, "_", " ");
James Feist83ff9ab2018-08-31 10:18:24 -0700922 }
James Feist5f2caae2018-12-12 14:08:25 -0800923 std::string key;
924 if (index == 0)
James Feist83ff9ab2018-08-31 10:18:24 -0700925 {
James Feist5f2caae2018-12-12 14:08:25 -0800926 key = "Inputs";
James Feist83ff9ab2018-08-31 10:18:24 -0700927 }
James Feist5f2caae2018-12-12 14:08:25 -0800928 else
929 {
930 key = "Outputs";
931 }
932 output[key] = *container;
933 index++;
James Feist83ff9ab2018-08-31 10:18:24 -0700934 }
James Feist5f2caae2018-12-12 14:08:25 -0800935 }
James Feist83ff9ab2018-08-31 10:18:24 -0700936
James Feistb943aae2019-07-11 16:33:56 -0700937 if (setpointOffset)
938 {
939 // translate between redfish and dbus names
940 if (*setpointOffset == "UpperThresholdNonCritical")
941 {
942 output["SetPointOffset"] = std::string("WarningLow");
943 }
944 else if (*setpointOffset == "LowerThresholdNonCritical")
945 {
946 output["SetPointOffset"] = std::string("WarningHigh");
947 }
948 else if (*setpointOffset == "LowerThresholdCritical")
949 {
950 output["SetPointOffset"] = std::string("CriticalLow");
951 }
952 else if (*setpointOffset == "UpperThresholdCritical")
953 {
954 output["SetPointOffset"] = std::string("CriticalHigh");
955 }
956 else
957 {
958 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
959 << *setpointOffset;
960 messages::invalidObject(response->res, it.key());
961 return CreatePIDRet::fail;
962 }
963 }
964
James Feist5f2caae2018-12-12 14:08:25 -0800965 // doubles
966 for (const auto& pairs : doubles)
967 {
968 if (!pairs.second)
James Feist83ff9ab2018-08-31 10:18:24 -0700969 {
James Feist5f2caae2018-12-12 14:08:25 -0800970 continue;
James Feist83ff9ab2018-08-31 10:18:24 -0700971 }
James Feist5f2caae2018-12-12 14:08:25 -0800972 BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
973 output[pairs.first] = *(pairs.second);
James Feist83ff9ab2018-08-31 10:18:24 -0700974 }
975 }
James Feist5f2caae2018-12-12 14:08:25 -0800976
James Feist83ff9ab2018-08-31 10:18:24 -0700977 else if (type == "FanZones")
978 {
James Feist83ff9ab2018-08-31 10:18:24 -0700979 output["Type"] = std::string("Pid.Zone");
980
James Feist5f2caae2018-12-12 14:08:25 -0800981 std::optional<nlohmann::json> chassisContainer;
982 std::optional<double> failSafePercent;
James Feistd3ec07f2019-02-25 14:51:15 -0800983 std::optional<double> minThermalOutput;
James Feistb6baeaa2019-02-21 10:41:40 -0800984 if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
James Feist5f2caae2018-12-12 14:08:25 -0800985 chassisContainer, "FailSafePercent",
James Feistd3ec07f2019-02-25 14:51:15 -0800986 failSafePercent, "MinThermalOutput",
987 minThermalOutput))
James Feist83ff9ab2018-08-31 10:18:24 -0700988 {
Ed Tanous71f52d92021-02-19 08:51:17 -0800989 BMCWEB_LOG_ERROR
990 << "Illegal Property "
991 << it.value().dump(2, ' ', true,
992 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -0800993 return CreatePIDRet::fail;
994 }
James Feist83ff9ab2018-08-31 10:18:24 -0700995
James Feist5f2caae2018-12-12 14:08:25 -0800996 if (chassisContainer)
997 {
998
999 std::string chassisId;
1000 if (!redfish::json_util::readJson(*chassisContainer, response->res,
1001 "@odata.id", chassisId))
James Feist83ff9ab2018-08-31 10:18:24 -07001002 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001003 BMCWEB_LOG_ERROR
1004 << "Illegal Property "
1005 << chassisContainer->dump(
1006 2, ' ', true,
1007 nlohmann::json::error_handler_t::replace);
James Feist83ff9ab2018-08-31 10:18:24 -07001008 return CreatePIDRet::fail;
1009 }
James Feist5f2caae2018-12-12 14:08:25 -08001010
AppaRao Puli717794d2019-10-18 22:54:53 +05301011 // /redfish/v1/chassis/chassis_name/
James Feist5f2caae2018-12-12 14:08:25 -08001012 if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
1013 {
1014 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
1015 messages::invalidObject(response->res, chassisId);
1016 return CreatePIDRet::fail;
1017 }
1018 }
James Feistd3ec07f2019-02-25 14:51:15 -08001019 if (minThermalOutput)
James Feist5f2caae2018-12-12 14:08:25 -08001020 {
James Feistd3ec07f2019-02-25 14:51:15 -08001021 output["MinThermalOutput"] = *minThermalOutput;
James Feist5f2caae2018-12-12 14:08:25 -08001022 }
1023 if (failSafePercent)
1024 {
1025 output["FailSafePercent"] = *failSafePercent;
1026 }
1027 }
1028 else if (type == "StepwiseControllers")
1029 {
1030 output["Type"] = std::string("Stepwise");
1031
1032 std::optional<std::vector<nlohmann::json>> zones;
1033 std::optional<std::vector<nlohmann::json>> steps;
1034 std::optional<std::vector<std::string>> inputs;
1035 std::optional<double> positiveHysteresis;
1036 std::optional<double> negativeHysteresis;
James Feistc33a90e2019-03-01 10:17:44 -08001037 std::optional<std::string> direction; // upper clipping curve vs lower
James Feist5f2caae2018-12-12 14:08:25 -08001038 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -08001039 it.value(), response->res, "Zones", zones, "Steps", steps,
1040 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
James Feistc33a90e2019-03-01 10:17:44 -08001041 "NegativeHysteresis", negativeHysteresis, "Direction",
1042 direction))
James Feist5f2caae2018-12-12 14:08:25 -08001043 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001044 BMCWEB_LOG_ERROR
1045 << "Illegal Property "
1046 << it.value().dump(2, ' ', true,
1047 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -08001048 return CreatePIDRet::fail;
1049 }
1050
1051 if (zones)
1052 {
James Feistb6baeaa2019-02-21 10:41:40 -08001053 std::vector<std::string> zonesStrs;
1054 if (!getZonesFromJsonReq(response, *zones, zonesStrs))
James Feist5f2caae2018-12-12 14:08:25 -08001055 {
Gunnar Millsa0744d32020-11-09 15:40:45 -06001056 BMCWEB_LOG_ERROR << "Illegal Zones";
James Feist5f2caae2018-12-12 14:08:25 -08001057 return CreatePIDRet::fail;
1058 }
James Feistb6baeaa2019-02-21 10:41:40 -08001059 if (chassis.empty() &&
1060 !findChassis(managedObj, zonesStrs[0], chassis))
1061 {
1062 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
1063 messages::invalidObject(response->res, it.key());
1064 return CreatePIDRet::fail;
1065 }
1066 output["Zones"] = std::move(zonesStrs);
James Feist5f2caae2018-12-12 14:08:25 -08001067 }
1068 if (steps)
1069 {
1070 std::vector<double> readings;
1071 std::vector<double> outputs;
1072 for (auto& step : *steps)
1073 {
1074 double target;
Ed Tanous23a21a12020-07-25 04:45:05 +00001075 double out;
James Feist5f2caae2018-12-12 14:08:25 -08001076
1077 if (!redfish::json_util::readJson(step, response->res, "Target",
Ed Tanous23a21a12020-07-25 04:45:05 +00001078 target, "Output", out))
James Feist5f2caae2018-12-12 14:08:25 -08001079 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001080 BMCWEB_LOG_ERROR
1081 << "Illegal Property "
1082 << it.value().dump(
1083 2, ' ', true,
1084 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -08001085 return CreatePIDRet::fail;
1086 }
1087 readings.emplace_back(target);
Ed Tanous23a21a12020-07-25 04:45:05 +00001088 outputs.emplace_back(out);
James Feist5f2caae2018-12-12 14:08:25 -08001089 }
1090 output["Reading"] = std::move(readings);
1091 output["Output"] = std::move(outputs);
1092 }
1093 if (inputs)
1094 {
1095 for (std::string& value : *inputs)
1096 {
James Feist5f2caae2018-12-12 14:08:25 -08001097 boost::replace_all(value, "_", " ");
1098 }
1099 output["Inputs"] = std::move(*inputs);
1100 }
1101 if (negativeHysteresis)
1102 {
1103 output["NegativeHysteresis"] = *negativeHysteresis;
1104 }
1105 if (positiveHysteresis)
1106 {
1107 output["PositiveHysteresis"] = *positiveHysteresis;
James Feist83ff9ab2018-08-31 10:18:24 -07001108 }
James Feistc33a90e2019-03-01 10:17:44 -08001109 if (direction)
1110 {
1111 constexpr const std::array<const char*, 2> allowedDirections = {
1112 "Ceiling", "Floor"};
1113 if (std::find(allowedDirections.begin(), allowedDirections.end(),
1114 *direction) == allowedDirections.end())
1115 {
1116 messages::propertyValueTypeError(response->res, "Direction",
1117 *direction);
1118 return CreatePIDRet::fail;
1119 }
1120 output["Class"] = *direction;
1121 }
James Feist83ff9ab2018-08-31 10:18:24 -07001122 }
1123 else
1124 {
Gunnar Millsa0744d32020-11-09 15:40:45 -06001125 BMCWEB_LOG_ERROR << "Illegal Type " << type;
Jason M. Bills35a62c72018-10-09 12:45:45 -07001126 messages::propertyUnknown(response->res, type);
James Feist83ff9ab2018-08-31 10:18:24 -07001127 return CreatePIDRet::fail;
1128 }
1129 return CreatePIDRet::patch;
1130}
James Feist73df0db2019-03-25 15:29:35 -07001131struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
1132{
1133
zhanghch058d1b46d2021-04-01 11:18:24 +08001134 GetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
Ed Tanous23a21a12020-07-25 04:45:05 +00001135 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001136
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001137 {}
James Feist73df0db2019-03-25 15:29:35 -07001138
1139 void run()
1140 {
1141 std::shared_ptr<GetPIDValues> self = shared_from_this();
1142
1143 // get all configurations
1144 crow::connections::systemBus->async_method_call(
1145 [self](const boost::system::error_code ec,
Ed Tanous23a21a12020-07-25 04:45:05 +00001146 const crow::openbmc_mapper::GetSubTreeType& subtreeLocal) {
James Feist73df0db2019-03-25 15:29:35 -07001147 if (ec)
1148 {
1149 BMCWEB_LOG_ERROR << ec;
1150 messages::internalError(self->asyncResp->res);
1151 return;
1152 }
Ed Tanous23a21a12020-07-25 04:45:05 +00001153 self->subtree = subtreeLocal;
James Feist73df0db2019-03-25 15:29:35 -07001154 },
1155 "xyz.openbmc_project.ObjectMapper",
1156 "/xyz/openbmc_project/object_mapper",
1157 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
1158 std::array<const char*, 4>{
1159 pidConfigurationIface, pidZoneConfigurationIface,
1160 objectManagerIface, stepwiseConfigurationIface});
1161
1162 // at the same time get the selected profile
1163 crow::connections::systemBus->async_method_call(
1164 [self](const boost::system::error_code ec,
Ed Tanous23a21a12020-07-25 04:45:05 +00001165 const crow::openbmc_mapper::GetSubTreeType& subtreeLocal) {
1166 if (ec || subtreeLocal.empty())
James Feist73df0db2019-03-25 15:29:35 -07001167 {
1168 return;
1169 }
Ed Tanous23a21a12020-07-25 04:45:05 +00001170 if (subtreeLocal[0].second.size() != 1)
James Feist73df0db2019-03-25 15:29:35 -07001171 {
1172 // invalid mapper response, should never happen
1173 BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
1174 messages::internalError(self->asyncResp->res);
1175 return;
1176 }
1177
Ed Tanous23a21a12020-07-25 04:45:05 +00001178 const std::string& path = subtreeLocal[0].first;
1179 const std::string& owner = subtreeLocal[0].second[0].first;
James Feist73df0db2019-03-25 15:29:35 -07001180 crow::connections::systemBus->async_method_call(
1181 [path, owner, self](
Ed Tanous23a21a12020-07-25 04:45:05 +00001182 const boost::system::error_code ec2,
James Feist73df0db2019-03-25 15:29:35 -07001183 const boost::container::flat_map<
1184 std::string, std::variant<std::vector<std::string>,
1185 std::string>>& resp) {
Ed Tanous23a21a12020-07-25 04:45:05 +00001186 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001187 {
George Liu0fda0f12021-11-16 10:06:17 +08001188 BMCWEB_LOG_ERROR
1189 << "GetPIDValues: Can't get thermalModeIface "
1190 << path;
James Feist73df0db2019-03-25 15:29:35 -07001191 messages::internalError(self->asyncResp->res);
1192 return;
1193 }
Ed Tanous271584a2019-07-09 16:24:22 -07001194 const std::string* current = nullptr;
1195 const std::vector<std::string>* supported = nullptr;
James Feist73df0db2019-03-25 15:29:35 -07001196 for (auto& [key, value] : resp)
1197 {
1198 if (key == "Current")
1199 {
1200 current = std::get_if<std::string>(&value);
1201 if (current == nullptr)
1202 {
1203 BMCWEB_LOG_ERROR
George Liu0fda0f12021-11-16 10:06:17 +08001204 << "GetPIDValues: thermal mode iface invalid "
James Feist73df0db2019-03-25 15:29:35 -07001205 << path;
1206 messages::internalError(
1207 self->asyncResp->res);
1208 return;
1209 }
1210 }
1211 if (key == "Supported")
1212 {
1213 supported =
1214 std::get_if<std::vector<std::string>>(
1215 &value);
1216 if (supported == nullptr)
1217 {
1218 BMCWEB_LOG_ERROR
George Liu0fda0f12021-11-16 10:06:17 +08001219 << "GetPIDValues: thermal mode iface invalid"
James Feist73df0db2019-03-25 15:29:35 -07001220 << path;
1221 messages::internalError(
1222 self->asyncResp->res);
1223 return;
1224 }
1225 }
1226 }
1227 if (current == nullptr || supported == nullptr)
1228 {
George Liu0fda0f12021-11-16 10:06:17 +08001229 BMCWEB_LOG_ERROR
1230 << "GetPIDValues: thermal mode iface invalid "
1231 << path;
James Feist73df0db2019-03-25 15:29:35 -07001232 messages::internalError(self->asyncResp->res);
1233 return;
1234 }
1235 self->currentProfile = *current;
1236 self->supportedProfiles = *supported;
1237 },
1238 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
1239 thermalModeIface);
1240 },
1241 "xyz.openbmc_project.ObjectMapper",
1242 "/xyz/openbmc_project/object_mapper",
1243 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
1244 std::array<const char*, 1>{thermalModeIface});
1245 }
1246
1247 ~GetPIDValues()
1248 {
1249 if (asyncResp->res.result() != boost::beast::http::status::ok)
1250 {
1251 return;
1252 }
1253 // create map of <connection, path to objMgr>>
1254 boost::container::flat_map<std::string, std::string> objectMgrPaths;
1255 boost::container::flat_set<std::string> calledConnections;
1256 for (const auto& pathGroup : subtree)
1257 {
1258 for (const auto& connectionGroup : pathGroup.second)
1259 {
1260 auto findConnection =
1261 calledConnections.find(connectionGroup.first);
1262 if (findConnection != calledConnections.end())
1263 {
1264 break;
1265 }
1266 for (const std::string& interface : connectionGroup.second)
1267 {
1268 if (interface == objectManagerIface)
1269 {
1270 objectMgrPaths[connectionGroup.first] = pathGroup.first;
1271 }
1272 // this list is alphabetical, so we
1273 // should have found the objMgr by now
1274 if (interface == pidConfigurationIface ||
1275 interface == pidZoneConfigurationIface ||
1276 interface == stepwiseConfigurationIface)
1277 {
1278 auto findObjMgr =
1279 objectMgrPaths.find(connectionGroup.first);
1280 if (findObjMgr == objectMgrPaths.end())
1281 {
1282 BMCWEB_LOG_DEBUG << connectionGroup.first
1283 << "Has no Object Manager";
1284 continue;
1285 }
1286
1287 calledConnections.insert(connectionGroup.first);
1288
1289 asyncPopulatePid(findObjMgr->first, findObjMgr->second,
1290 currentProfile, supportedProfiles,
1291 asyncResp);
1292 break;
1293 }
1294 }
1295 }
1296 }
1297 }
1298
1299 std::vector<std::string> supportedProfiles;
1300 std::string currentProfile;
1301 crow::openbmc_mapper::GetSubTreeType subtree;
zhanghch058d1b46d2021-04-01 11:18:24 +08001302 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001303};
1304
1305struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
1306{
1307
zhanghch058d1b46d2021-04-01 11:18:24 +08001308 SetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
James Feist73df0db2019-03-25 15:29:35 -07001309 nlohmann::json& data) :
Ed Tanous271584a2019-07-09 16:24:22 -07001310 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001311 {
1312
1313 std::optional<nlohmann::json> pidControllers;
1314 std::optional<nlohmann::json> fanControllers;
1315 std::optional<nlohmann::json> fanZones;
1316 std::optional<nlohmann::json> stepwiseControllers;
1317
1318 if (!redfish::json_util::readJson(
1319 data, asyncResp->res, "PidControllers", pidControllers,
1320 "FanControllers", fanControllers, "FanZones", fanZones,
1321 "StepwiseControllers", stepwiseControllers, "Profile", profile))
1322 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001323 BMCWEB_LOG_ERROR
1324 << "Illegal Property "
1325 << data.dump(2, ' ', true,
1326 nlohmann::json::error_handler_t::replace);
James Feist73df0db2019-03-25 15:29:35 -07001327 return;
1328 }
1329 configuration.emplace_back("PidControllers", std::move(pidControllers));
1330 configuration.emplace_back("FanControllers", std::move(fanControllers));
1331 configuration.emplace_back("FanZones", std::move(fanZones));
1332 configuration.emplace_back("StepwiseControllers",
1333 std::move(stepwiseControllers));
1334 }
1335 void run()
1336 {
1337 if (asyncResp->res.result() != boost::beast::http::status::ok)
1338 {
1339 return;
1340 }
1341
1342 std::shared_ptr<SetPIDValues> self = shared_from_this();
1343
1344 // todo(james): might make sense to do a mapper call here if this
1345 // interface gets more traction
1346 crow::connections::systemBus->async_method_call(
1347 [self](const boost::system::error_code ec,
Ed Tanous271584a2019-07-09 16:24:22 -07001348 dbus::utility::ManagedObjectType& mObj) {
James Feist73df0db2019-03-25 15:29:35 -07001349 if (ec)
1350 {
1351 BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
1352 messages::internalError(self->asyncResp->res);
1353 return;
1354 }
James Feiste69d9de2020-02-07 12:23:27 -08001355 const std::array<const char*, 3> configurations = {
1356 pidConfigurationIface, pidZoneConfigurationIface,
1357 stepwiseConfigurationIface};
1358
James Feist14b0b8d2020-02-12 11:52:07 -08001359 for (const auto& [path, object] : mObj)
James Feiste69d9de2020-02-07 12:23:27 -08001360 {
James Feist14b0b8d2020-02-12 11:52:07 -08001361 for (const auto& [interface, _] : object)
James Feiste69d9de2020-02-07 12:23:27 -08001362 {
1363 if (std::find(configurations.begin(),
1364 configurations.end(),
1365 interface) != configurations.end())
1366 {
James Feist14b0b8d2020-02-12 11:52:07 -08001367 self->objectCount++;
James Feiste69d9de2020-02-07 12:23:27 -08001368 break;
1369 }
1370 }
James Feiste69d9de2020-02-07 12:23:27 -08001371 }
Ed Tanous271584a2019-07-09 16:24:22 -07001372 self->managedObj = std::move(mObj);
James Feist73df0db2019-03-25 15:29:35 -07001373 },
1374 "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
1375 "GetManagedObjects");
1376
1377 // at the same time get the profile information
1378 crow::connections::systemBus->async_method_call(
1379 [self](const boost::system::error_code ec,
1380 const crow::openbmc_mapper::GetSubTreeType& subtree) {
1381 if (ec || subtree.empty())
1382 {
1383 return;
1384 }
1385 if (subtree[0].second.empty())
1386 {
1387 // invalid mapper response, should never happen
1388 BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
1389 messages::internalError(self->asyncResp->res);
1390 return;
1391 }
1392
1393 const std::string& path = subtree[0].first;
1394 const std::string& owner = subtree[0].second[0].first;
1395 crow::connections::systemBus->async_method_call(
1396 [self, path, owner](
Ed Tanouscb13a392020-07-25 19:02:03 +00001397 const boost::system::error_code ec2,
James Feist73df0db2019-03-25 15:29:35 -07001398 const boost::container::flat_map<
1399 std::string, std::variant<std::vector<std::string>,
Ed Tanous271584a2019-07-09 16:24:22 -07001400 std::string>>& r) {
Ed Tanouscb13a392020-07-25 19:02:03 +00001401 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001402 {
George Liu0fda0f12021-11-16 10:06:17 +08001403 BMCWEB_LOG_ERROR
1404 << "SetPIDValues: Can't get thermalModeIface "
1405 << path;
James Feist73df0db2019-03-25 15:29:35 -07001406 messages::internalError(self->asyncResp->res);
1407 return;
1408 }
Ed Tanous271584a2019-07-09 16:24:22 -07001409 const std::string* current = nullptr;
1410 const std::vector<std::string>* supported = nullptr;
1411 for (auto& [key, value] : r)
James Feist73df0db2019-03-25 15:29:35 -07001412 {
1413 if (key == "Current")
1414 {
1415 current = std::get_if<std::string>(&value);
1416 if (current == nullptr)
1417 {
1418 BMCWEB_LOG_ERROR
George Liu0fda0f12021-11-16 10:06:17 +08001419 << "SetPIDValues: thermal mode iface invalid "
James Feist73df0db2019-03-25 15:29:35 -07001420 << path;
1421 messages::internalError(
1422 self->asyncResp->res);
1423 return;
1424 }
1425 }
1426 if (key == "Supported")
1427 {
1428 supported =
1429 std::get_if<std::vector<std::string>>(
1430 &value);
1431 if (supported == nullptr)
1432 {
1433 BMCWEB_LOG_ERROR
George Liu0fda0f12021-11-16 10:06:17 +08001434 << "SetPIDValues: thermal mode iface invalid"
James Feist73df0db2019-03-25 15:29:35 -07001435 << path;
1436 messages::internalError(
1437 self->asyncResp->res);
1438 return;
1439 }
1440 }
1441 }
1442 if (current == nullptr || supported == nullptr)
1443 {
George Liu0fda0f12021-11-16 10:06:17 +08001444 BMCWEB_LOG_ERROR
1445 << "SetPIDValues: thermal mode iface invalid "
1446 << path;
James Feist73df0db2019-03-25 15:29:35 -07001447 messages::internalError(self->asyncResp->res);
1448 return;
1449 }
1450 self->currentProfile = *current;
1451 self->supportedProfiles = *supported;
1452 self->profileConnection = owner;
1453 self->profilePath = path;
1454 },
1455 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
1456 thermalModeIface);
1457 },
1458 "xyz.openbmc_project.ObjectMapper",
1459 "/xyz/openbmc_project/object_mapper",
1460 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
1461 std::array<const char*, 1>{thermalModeIface});
1462 }
1463 ~SetPIDValues()
1464 {
1465 if (asyncResp->res.result() != boost::beast::http::status::ok)
1466 {
1467 return;
1468 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001469 std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001470 if (profile)
1471 {
1472 if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
1473 *profile) == supportedProfiles.end())
1474 {
1475 messages::actionParameterUnknown(response->res, "Profile",
1476 *profile);
1477 return;
1478 }
1479 currentProfile = *profile;
1480 crow::connections::systemBus->async_method_call(
1481 [response](const boost::system::error_code ec) {
1482 if (ec)
1483 {
1484 BMCWEB_LOG_ERROR << "Error patching profile" << ec;
1485 messages::internalError(response->res);
1486 }
1487 },
1488 profileConnection, profilePath,
1489 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
1490 "Current", std::variant<std::string>(*profile));
1491 }
1492
1493 for (auto& containerPair : configuration)
1494 {
1495 auto& container = containerPair.second;
1496 if (!container)
1497 {
1498 continue;
1499 }
James Feist6ee7f772020-02-06 16:25:27 -08001500 BMCWEB_LOG_DEBUG << *container;
1501
James Feist73df0db2019-03-25 15:29:35 -07001502 std::string& type = containerPair.first;
1503
1504 for (nlohmann::json::iterator it = container->begin();
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301505 it != container->end(); ++it)
James Feist73df0db2019-03-25 15:29:35 -07001506 {
1507 const auto& name = it.key();
James Feist6ee7f772020-02-06 16:25:27 -08001508 BMCWEB_LOG_DEBUG << "looking for " << name;
1509
James Feist73df0db2019-03-25 15:29:35 -07001510 auto pathItr =
1511 std::find_if(managedObj.begin(), managedObj.end(),
1512 [&name](const auto& obj) {
1513 return boost::algorithm::ends_with(
1514 obj.first.str, "/" + name);
1515 });
1516 boost::container::flat_map<std::string,
1517 dbus::utility::DbusVariantType>
1518 output;
1519
1520 output.reserve(16); // The pid interface length
1521
1522 // determines if we're patching entity-manager or
1523 // creating a new object
1524 bool createNewObject = (pathItr == managedObj.end());
James Feist6ee7f772020-02-06 16:25:27 -08001525 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
1526
James Feist73df0db2019-03-25 15:29:35 -07001527 std::string iface;
1528 if (type == "PidControllers" || type == "FanControllers")
1529 {
1530 iface = pidConfigurationIface;
1531 if (!createNewObject &&
1532 pathItr->second.find(pidConfigurationIface) ==
1533 pathItr->second.end())
1534 {
1535 createNewObject = true;
1536 }
1537 }
1538 else if (type == "FanZones")
1539 {
1540 iface = pidZoneConfigurationIface;
1541 if (!createNewObject &&
1542 pathItr->second.find(pidZoneConfigurationIface) ==
1543 pathItr->second.end())
1544 {
1545
1546 createNewObject = true;
1547 }
1548 }
1549 else if (type == "StepwiseControllers")
1550 {
1551 iface = stepwiseConfigurationIface;
1552 if (!createNewObject &&
1553 pathItr->second.find(stepwiseConfigurationIface) ==
1554 pathItr->second.end())
1555 {
1556 createNewObject = true;
1557 }
1558 }
James Feist6ee7f772020-02-06 16:25:27 -08001559
1560 if (createNewObject && it.value() == nullptr)
1561 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -05001562 // can't delete a non-existent object
James Feist6ee7f772020-02-06 16:25:27 -08001563 messages::invalidObject(response->res, name);
1564 continue;
1565 }
1566
1567 std::string path;
1568 if (pathItr != managedObj.end())
1569 {
1570 path = pathItr->first.str;
1571 }
1572
James Feist73df0db2019-03-25 15:29:35 -07001573 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
James Feiste69d9de2020-02-07 12:23:27 -08001574
1575 // arbitrary limit to avoid attacks
1576 constexpr const size_t controllerLimit = 500;
James Feist14b0b8d2020-02-12 11:52:07 -08001577 if (createNewObject && objectCount >= controllerLimit)
James Feiste69d9de2020-02-07 12:23:27 -08001578 {
1579 messages::resourceExhaustion(response->res, type);
1580 continue;
1581 }
1582
James Feist73df0db2019-03-25 15:29:35 -07001583 output["Name"] = boost::replace_all_copy(name, "_", " ");
1584
1585 std::string chassis;
1586 CreatePIDRet ret = createPidInterface(
James Feist6ee7f772020-02-06 16:25:27 -08001587 response, type, it, path, managedObj, createNewObject,
1588 output, chassis, currentProfile);
James Feist73df0db2019-03-25 15:29:35 -07001589 if (ret == CreatePIDRet::fail)
1590 {
1591 return;
1592 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001593 if (ret == CreatePIDRet::del)
James Feist73df0db2019-03-25 15:29:35 -07001594 {
1595 continue;
1596 }
1597
1598 if (!createNewObject)
1599 {
1600 for (const auto& property : output)
1601 {
1602 crow::connections::systemBus->async_method_call(
1603 [response,
1604 propertyName{std::string(property.first)}](
1605 const boost::system::error_code ec) {
1606 if (ec)
1607 {
1608 BMCWEB_LOG_ERROR << "Error patching "
1609 << propertyName << ": "
1610 << ec;
1611 messages::internalError(response->res);
1612 return;
1613 }
1614 messages::success(response->res);
1615 },
James Feist6ee7f772020-02-06 16:25:27 -08001616 "xyz.openbmc_project.EntityManager", path,
James Feist73df0db2019-03-25 15:29:35 -07001617 "org.freedesktop.DBus.Properties", "Set", iface,
1618 property.first, property.second);
1619 }
1620 }
1621 else
1622 {
1623 if (chassis.empty())
1624 {
1625 BMCWEB_LOG_ERROR << "Failed to get chassis from config";
1626 messages::invalidObject(response->res, name);
1627 return;
1628 }
1629
1630 bool foundChassis = false;
1631 for (const auto& obj : managedObj)
1632 {
1633 if (boost::algorithm::ends_with(obj.first.str, chassis))
1634 {
1635 chassis = obj.first.str;
1636 foundChassis = true;
1637 break;
1638 }
1639 }
1640 if (!foundChassis)
1641 {
1642 BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
1643 messages::resourceMissingAtURI(
1644 response->res, "/redfish/v1/Chassis/" + chassis);
1645 return;
1646 }
1647
1648 crow::connections::systemBus->async_method_call(
1649 [response](const boost::system::error_code ec) {
1650 if (ec)
1651 {
1652 BMCWEB_LOG_ERROR << "Error Adding Pid Object "
1653 << ec;
1654 messages::internalError(response->res);
1655 return;
1656 }
1657 messages::success(response->res);
1658 },
1659 "xyz.openbmc_project.EntityManager", chassis,
1660 "xyz.openbmc_project.AddObject", "AddObject", output);
1661 }
1662 }
1663 }
1664 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001665 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001666 std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
1667 configuration;
1668 std::optional<std::string> profile;
1669 dbus::utility::ManagedObjectType managedObj;
1670 std::vector<std::string> supportedProfiles;
1671 std::string currentProfile;
1672 std::string profileConnection;
1673 std::string profilePath;
James Feist14b0b8d2020-02-12 11:52:07 -08001674 size_t objectCount = 0;
James Feist73df0db2019-03-25 15:29:35 -07001675};
James Feist83ff9ab2018-08-31 10:18:24 -07001676
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001677/**
1678 * @brief Retrieves BMC manager location data over DBus
1679 *
1680 * @param[in] aResp Shared pointer for completing asynchronous calls
1681 * @param[in] connectionName - service name
1682 * @param[in] path - object path
1683 * @return none
1684 */
zhanghch058d1b46d2021-04-01 11:18:24 +08001685inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001686 const std::string& connectionName,
1687 const std::string& path)
1688{
1689 BMCWEB_LOG_DEBUG << "Get BMC manager Location data.";
1690
1691 crow::connections::systemBus->async_method_call(
1692 [aResp](const boost::system::error_code ec,
1693 const std::variant<std::string>& property) {
1694 if (ec)
1695 {
1696 BMCWEB_LOG_DEBUG << "DBUS response error for "
1697 "Location";
1698 messages::internalError(aResp->res);
1699 return;
1700 }
1701
1702 const std::string* value = std::get_if<std::string>(&property);
1703
1704 if (value == nullptr)
1705 {
1706 // illegal value
1707 messages::internalError(aResp->res);
1708 return;
1709 }
1710
1711 aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
1712 *value;
1713 },
1714 connectionName, path, "org.freedesktop.DBus.Properties", "Get",
George Liu0fda0f12021-11-16 10:06:17 +08001715 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode");
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001716}
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001717// avoid name collision systems.hpp
1718inline void
1719 managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001720{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001721 BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time";
Ed Tanous52cc1122020-07-18 13:51:21 -07001722
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001723 crow::connections::systemBus->async_method_call(
1724 [aResp](const boost::system::error_code ec,
1725 std::variant<uint64_t>& lastResetTime) {
1726 if (ec)
James Feist83ff9ab2018-08-31 10:18:24 -07001727 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001728 BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
Ed Tanous43b761d2019-02-13 20:10:56 -08001729 return;
1730 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001731
1732 const uint64_t* lastResetTimePtr =
1733 std::get_if<uint64_t>(&lastResetTime);
1734
1735 if (!lastResetTimePtr)
Ed Tanous43b761d2019-02-13 20:10:56 -08001736 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001737 messages::internalError(aResp->res);
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001738 return;
1739 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001740 // LastRebootTime is epoch time, in milliseconds
1741 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
Nan Zhou1d8782e2021-11-29 22:23:18 -08001742 uint64_t lastResetTimeStamp = *lastResetTimePtr / 1000;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001743
1744 // Convert to ISO 8601 standard
1745 aResp->res.jsonValue["LastResetTime"] =
Nan Zhou1d8782e2021-11-29 22:23:18 -08001746 crow::utility::getDateTimeUint(lastResetTimeStamp);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001747 },
1748 "xyz.openbmc_project.State.BMC", "/xyz/openbmc_project/state/bmc0",
1749 "org.freedesktop.DBus.Properties", "Get",
1750 "xyz.openbmc_project.State.BMC", "LastRebootTime");
1751}
1752
1753/**
1754 * @brief Set the running firmware image
1755 *
1756 * @param[i,o] aResp - Async response object
1757 * @param[i] runningFirmwareTarget - Image to make the running image
1758 *
1759 * @return void
1760 */
1761inline void
1762 setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1763 const std::string& runningFirmwareTarget)
1764{
1765 // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1766 std::string::size_type idPos = runningFirmwareTarget.rfind('/');
1767 if (idPos == std::string::npos)
1768 {
1769 messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
1770 "@odata.id");
1771 BMCWEB_LOG_DEBUG << "Can't parse firmware ID!";
1772 return;
1773 }
1774 idPos++;
1775 if (idPos >= runningFirmwareTarget.size())
1776 {
1777 messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
1778 "@odata.id");
1779 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
1780 return;
1781 }
1782 std::string firmwareId = runningFirmwareTarget.substr(idPos);
1783
1784 // Make sure the image is valid before setting priority
1785 crow::connections::systemBus->async_method_call(
1786 [aResp, firmwareId, runningFirmwareTarget](
1787 const boost::system::error_code ec, ManagedObjectType& subtree) {
1788 if (ec)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001789 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001790 BMCWEB_LOG_DEBUG << "D-Bus response error getting objects.";
1791 messages::internalError(aResp->res);
1792 return;
1793 }
1794
1795 if (subtree.size() == 0)
1796 {
1797 BMCWEB_LOG_DEBUG << "Can't find image!";
1798 messages::internalError(aResp->res);
1799 return;
1800 }
1801
1802 bool foundImage = false;
1803 for (auto& object : subtree)
1804 {
1805 const std::string& path =
1806 static_cast<const std::string&>(object.first);
1807 std::size_t idPos2 = path.rfind('/');
1808
1809 if (idPos2 == std::string::npos)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001810 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001811 continue;
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001812 }
1813
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001814 idPos2++;
1815 if (idPos2 >= path.size())
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001816 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001817 continue;
1818 }
1819
1820 if (path.substr(idPos2) == firmwareId)
1821 {
1822 foundImage = true;
1823 break;
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001824 }
1825 }
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301826
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001827 if (!foundImage)
1828 {
1829 messages::propertyValueNotInList(
1830 aResp->res, runningFirmwareTarget, "@odata.id");
1831 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
1832 return;
1833 }
Gunnar Mills4bf2b032020-06-23 22:28:31 -05001834
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001835 BMCWEB_LOG_DEBUG
1836 << "Setting firmware version " + firmwareId + " to priority 0.";
Gunnar Mills4bf2b032020-06-23 22:28:31 -05001837
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001838 // Only support Immediate
1839 // An addition could be a Redfish Setting like
1840 // ActiveSoftwareImageApplyTime and support OnReset
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301841 crow::connections::systemBus->async_method_call(
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001842 [aResp](const boost::system::error_code ec) {
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301843 if (ec)
1844 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001845 BMCWEB_LOG_DEBUG << "D-Bus response error setting.";
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301846 messages::internalError(aResp->res);
1847 return;
1848 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001849 doBMCGracefulRestart(aResp);
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301850 },
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001851
1852 "xyz.openbmc_project.Software.BMC.Updater",
1853 "/xyz/openbmc_project/software/" + firmwareId,
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301854 "org.freedesktop.DBus.Properties", "Set",
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001855 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1856 std::variant<uint8_t>(static_cast<uint8_t>(0)));
1857 },
1858 "xyz.openbmc_project.Software.BMC.Updater",
1859 "/xyz/openbmc_project/software", "org.freedesktop.DBus.ObjectManager",
1860 "GetManagedObjects");
1861}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001862
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001863inline void setDateTime(std::shared_ptr<bmcweb::AsyncResp> aResp,
1864 std::string datetime)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001865{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001866 BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001867
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001868 std::stringstream stream(datetime);
1869 // Convert from ISO 8601 to boost local_time
1870 // (BMC only has time in UTC)
1871 boost::posix_time::ptime posixTime;
1872 boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
1873 // Facet gets deleted with the stringsteam
1874 auto ifc = std::make_unique<boost::local_time::local_time_input_facet>(
1875 "%Y-%m-%d %H:%M:%S%F %ZP");
1876 stream.imbue(std::locale(stream.getloc(), ifc.release()));
1877
1878 boost::local_time::local_date_time ldt(boost::local_time::not_a_date_time);
1879
1880 if (stream >> ldt)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001881 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001882 posixTime = ldt.utc_time();
1883 boost::posix_time::time_duration dur = posixTime - epoch;
1884 uint64_t durMicroSecs = static_cast<uint64_t>(dur.total_microseconds());
1885 crow::connections::systemBus->async_method_call(
1886 [aResp{std::move(aResp)}, datetime{std::move(datetime)}](
1887 const boost::system::error_code ec) {
1888 if (ec)
1889 {
1890 BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1891 "DBUS response error "
1892 << ec;
1893 messages::internalError(aResp->res);
1894 return;
1895 }
1896 aResp->res.jsonValue["DateTime"] = datetime;
1897 },
1898 "xyz.openbmc_project.Time.Manager", "/xyz/openbmc_project/time/bmc",
1899 "org.freedesktop.DBus.Properties", "Set",
1900 "xyz.openbmc_project.Time.EpochTime", "Elapsed",
1901 std::variant<uint64_t>(durMicroSecs));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001902 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001903 else
1904 {
1905 messages::propertyValueFormatError(aResp->res, datetime, "DateTime");
1906 return;
1907 }
1908}
1909
1910inline void requestRoutesManager(App& app)
1911{
1912 std::string uuid = persistent_data::getConfig().systemUuid;
1913
1914 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07001915 .privileges(redfish::privileges::getManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001916 .methods(boost::beast::http::verb::get)([uuid](const crow::Request&,
1917 const std::shared_ptr<
1918 bmcweb::AsyncResp>&
1919 asyncResp) {
1920 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
1921 asyncResp->res.jsonValue["@odata.type"] =
1922 "#Manager.v1_11_0.Manager";
1923 asyncResp->res.jsonValue["Id"] = "bmc";
1924 asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
1925 asyncResp->res.jsonValue["Description"] =
1926 "Baseboard Management Controller";
1927 asyncResp->res.jsonValue["PowerState"] = "On";
1928 asyncResp->res.jsonValue["Status"] = {{"State", "Enabled"},
1929 {"Health", "OK"}};
1930 asyncResp->res.jsonValue["ManagerType"] = "BMC";
1931 asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
1932 asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1933 asyncResp->res.jsonValue["Model"] =
1934 "OpenBmc"; // TODO(ed), get model
1935
1936 asyncResp->res.jsonValue["LogServices"] = {
1937 {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}};
1938
1939 asyncResp->res.jsonValue["NetworkProtocol"] = {
1940 {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}};
1941
1942 asyncResp->res.jsonValue["EthernetInterfaces"] = {
1943 {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}};
1944
1945#ifdef BMCWEB_ENABLE_VM_NBDPROXY
1946 asyncResp->res.jsonValue["VirtualMedia"] = {
1947 {"@odata.id", "/redfish/v1/Managers/bmc/VirtualMedia"}};
1948#endif // BMCWEB_ENABLE_VM_NBDPROXY
1949
1950 // default oem data
1951 nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
1952 nlohmann::json& oemOpenbmc = oem["OpenBmc"];
1953 oem["@odata.type"] = "#OemManager.Oem";
1954 oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
1955 oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
1956 oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
1957 oemOpenbmc["Certificates"] = {
1958 {"@odata.id",
1959 "/redfish/v1/Managers/bmc/Truststore/Certificates"}};
1960
1961 // Manager.Reset (an action) can be many values, OpenBMC only
1962 // supports BMC reboot.
1963 nlohmann::json& managerReset =
1964 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
1965 managerReset["target"] =
1966 "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
1967 managerReset["@Redfish.ActionInfo"] =
1968 "/redfish/v1/Managers/bmc/ResetActionInfo";
1969
1970 // ResetToDefaults (Factory Reset) has values like
1971 // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
1972 // on OpenBMC
1973 nlohmann::json& resetToDefaults =
1974 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
1975 resetToDefaults["target"] =
1976 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
1977 resetToDefaults["ResetType@Redfish.AllowableValues"] = {"ResetAll"};
1978
Tejas Patil7c8c4052021-06-04 17:43:14 +05301979 std::pair<std::string, std::string> redfishDateTimeOffset =
1980 crow::utility::getDateTimeOffsetNow();
1981
1982 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1983 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1984 redfishDateTimeOffset.second;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001985
Gunnar Mills0e8ac5e2020-11-06 15:33:24 -06001986 // TODO (Gunnar): Remove these one day since moved to ComputerSystem
1987 // Still used by OCP profiles
1988 // https://github.com/opencomputeproject/OCP-Profiles/issues/23
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001989 // Fill in SerialConsole info
1990 asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
1991 asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] =
1992 15;
1993 asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
1994 {"IPMI", "SSH"};
1995#ifdef BMCWEB_ENABLE_KVM
1996 // Fill in GraphicalConsole info
1997 asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] =
1998 true;
1999 asyncResp->res
2000 .jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4;
2001 asyncResp->res.jsonValue["GraphicalConsole"]
2002 ["ConnectTypesSupported"] = {"KVMIP"};
2003#endif // BMCWEB_ENABLE_KVM
2004
2005 asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] =
2006 1;
2007 asyncResp->res.jsonValue["Links"]["ManagerForServers"] = {
2008 {{"@odata.id", "/redfish/v1/Systems/system"}}};
2009
2010 auto health = std::make_shared<HealthPopulate>(asyncResp);
2011 health->isManagersHealth = true;
2012 health->populate();
2013
2014 fw_util::populateFirmwareInformation(asyncResp, fw_util::bmcPurpose,
2015 "FirmwareVersion", true);
2016
2017 managerGetLastResetTime(asyncResp);
2018
2019 auto pids = std::make_shared<GetPIDValues>(asyncResp);
2020 pids->run();
2021
2022 getMainChassisId(
2023 asyncResp, [](const std::string& chassisId,
2024 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2025 aRsp->res
2026 .jsonValue["Links"]["ManagerForChassis@odata.count"] =
2027 1;
2028 aRsp->res.jsonValue["Links"]["ManagerForChassis"] = {
2029 {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
2030 aRsp->res.jsonValue["Links"]["ManagerInChassis"] = {
2031 {"@odata.id", "/redfish/v1/Chassis/" + chassisId}};
2032 });
2033
2034 static bool started = false;
2035
2036 if (!started)
2037 {
2038 crow::connections::systemBus->async_method_call(
2039 [asyncResp](const boost::system::error_code ec,
2040 const std::variant<double>& resp) {
2041 if (ec)
2042 {
2043 BMCWEB_LOG_ERROR << "Error while getting progress";
2044 messages::internalError(asyncResp->res);
2045 return;
2046 }
2047 const double* val = std::get_if<double>(&resp);
2048 if (val == nullptr)
2049 {
2050 BMCWEB_LOG_ERROR
2051 << "Invalid response while getting progress";
2052 messages::internalError(asyncResp->res);
2053 return;
2054 }
2055 if (*val < 1.0)
2056 {
2057 asyncResp->res.jsonValue["Status"]["State"] =
2058 "Starting";
2059 started = true;
2060 }
2061 },
2062 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
2063 "org.freedesktop.DBus.Properties", "Get",
2064 "org.freedesktop.systemd1.Manager", "Progress");
2065 }
2066
2067 crow::connections::systemBus->async_method_call(
2068 [asyncResp](
2069 const boost::system::error_code ec,
2070 const std::vector<
2071 std::pair<std::string,
2072 std::vector<std::pair<
2073 std::string, std::vector<std::string>>>>>&
2074 subtree) {
2075 if (ec)
2076 {
2077 BMCWEB_LOG_DEBUG
2078 << "D-Bus response error on GetSubTree " << ec;
2079 return;
2080 }
2081 if (subtree.size() == 0)
2082 {
2083 BMCWEB_LOG_DEBUG << "Can't find bmc D-Bus object!";
2084 return;
2085 }
2086 // Assume only 1 bmc D-Bus object
2087 // Throw an error if there is more than 1
2088 if (subtree.size() > 1)
2089 {
2090 BMCWEB_LOG_DEBUG
2091 << "Found more than 1 bmc D-Bus object!";
2092 messages::internalError(asyncResp->res);
2093 return;
2094 }
2095
2096 if (subtree[0].first.empty() ||
2097 subtree[0].second.size() != 1)
2098 {
2099 BMCWEB_LOG_DEBUG << "Error getting bmc D-Bus object!";
2100 messages::internalError(asyncResp->res);
2101 return;
2102 }
2103
2104 const std::string& path = subtree[0].first;
2105 const std::string& connectionName =
2106 subtree[0].second[0].first;
2107
2108 for (const auto& interfaceName :
2109 subtree[0].second[0].second)
2110 {
2111 if (interfaceName ==
2112 "xyz.openbmc_project.Inventory.Decorator.Asset")
2113 {
2114 crow::connections::systemBus->async_method_call(
2115 [asyncResp](
2116 const boost::system::error_code ec,
2117 const std::vector<
2118 std::pair<std::string,
2119 std::variant<std::string>>>&
2120 propertiesList) {
2121 if (ec)
2122 {
2123 BMCWEB_LOG_DEBUG
2124 << "Can't get bmc asset!";
2125 return;
2126 }
2127 for (const std::pair<
2128 std::string,
2129 std::variant<std::string>>&
2130 property : propertiesList)
2131 {
2132 const std::string& propertyName =
2133 property.first;
2134
2135 if ((propertyName == "PartNumber") ||
2136 (propertyName == "SerialNumber") ||
2137 (propertyName == "Manufacturer") ||
2138 (propertyName == "Model") ||
2139 (propertyName == "SparePartNumber"))
2140 {
2141 const std::string* value =
2142 std::get_if<std::string>(
2143 &property.second);
2144 if (value == nullptr)
2145 {
2146 // illegal property
2147 messages::internalError(
2148 asyncResp->res);
2149 return;
2150 }
2151 asyncResp->res
2152 .jsonValue[propertyName] =
2153 *value;
2154 }
2155 }
2156 },
2157 connectionName, path,
2158 "org.freedesktop.DBus.Properties", "GetAll",
George Liu0fda0f12021-11-16 10:06:17 +08002159 "xyz.openbmc_project.Inventory.Decorator.Asset");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002160 }
George Liu0fda0f12021-11-16 10:06:17 +08002161 else if (
2162 interfaceName ==
2163 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002164 {
2165 getLocation(asyncResp, connectionName, path);
2166 }
2167 }
2168 },
2169 "xyz.openbmc_project.ObjectMapper",
2170 "/xyz/openbmc_project/object_mapper",
2171 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2172 "/xyz/openbmc_project/inventory", int32_t(0),
2173 std::array<const char*, 1>{
2174 "xyz.openbmc_project.Inventory.Item.Bmc"});
2175 });
2176
2177 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07002178 .privileges(redfish::privileges::patchManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002179 .methods(
2180 boost::beast::http::verb::
2181 patch)([](const crow::Request& req,
2182 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2183 std::optional<nlohmann::json> oem;
2184 std::optional<nlohmann::json> links;
2185 std::optional<std::string> datetime;
2186
2187 if (!json_util::readJson(req, asyncResp->res, "Oem", oem,
2188 "DateTime", datetime, "Links", links))
2189 {
2190 return;
2191 }
2192
2193 if (oem)
2194 {
2195 std::optional<nlohmann::json> openbmc;
2196 if (!redfish::json_util::readJson(*oem, asyncResp->res,
2197 "OpenBmc", openbmc))
2198 {
2199 BMCWEB_LOG_ERROR
2200 << "Illegal Property "
2201 << oem->dump(2, ' ', true,
2202 nlohmann::json::error_handler_t::replace);
2203 return;
2204 }
2205 if (openbmc)
2206 {
2207 std::optional<nlohmann::json> fan;
2208 if (!redfish::json_util::readJson(*openbmc, asyncResp->res,
2209 "Fan", fan))
2210 {
2211 BMCWEB_LOG_ERROR
2212 << "Illegal Property "
2213 << openbmc->dump(
2214 2, ' ', true,
2215 nlohmann::json::error_handler_t::replace);
2216 return;
2217 }
2218 if (fan)
2219 {
2220 auto pid =
2221 std::make_shared<SetPIDValues>(asyncResp, *fan);
2222 pid->run();
2223 }
2224 }
2225 }
2226 if (links)
2227 {
2228 std::optional<nlohmann::json> activeSoftwareImage;
2229 if (!redfish::json_util::readJson(*links, asyncResp->res,
2230 "ActiveSoftwareImage",
2231 activeSoftwareImage))
2232 {
2233 return;
2234 }
2235 if (activeSoftwareImage)
2236 {
2237 std::optional<std::string> odataId;
2238 if (!json_util::readJson(*activeSoftwareImage,
2239 asyncResp->res, "@odata.id",
2240 odataId))
2241 {
2242 return;
2243 }
2244
2245 if (odataId)
2246 {
2247 setActiveFirmwareImage(asyncResp, *odataId);
2248 }
2249 }
2250 }
2251 if (datetime)
2252 {
2253 setDateTime(asyncResp, std::move(*datetime));
2254 }
2255 });
2256}
2257
2258inline void requestRoutesManagerCollection(App& app)
2259{
2260 BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
Ed Tanoused398212021-06-09 17:05:54 -07002261 .privileges(redfish::privileges::getManagerCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002262 .methods(boost::beast::http::verb::get)(
2263 [](const crow::Request&,
2264 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2265 // Collections don't include the static data added by SubRoute
2266 // because it has a duplicate entry for members
2267 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
2268 asyncResp->res.jsonValue["@odata.type"] =
2269 "#ManagerCollection.ManagerCollection";
2270 asyncResp->res.jsonValue["Name"] = "Manager Collection";
2271 asyncResp->res.jsonValue["Members@odata.count"] = 1;
2272 asyncResp->res.jsonValue["Members"] = {
2273 {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
2274 });
2275}
Ed Tanous1abe55e2018-09-05 08:30:59 -07002276} // namespace redfish