blob: 39b0f8864b554f1b62dcd546e70a5d11efe00c86 [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.
Ed Tanous168e20c2021-12-13 14:39:53 -080053 dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
Gunnar Mills2a5c4402020-05-19 09:07:24 -050054
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.
Ed Tanous168e20c2021-12-13 14:39:53 -080082 dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +000083
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(
Ed Tanous168e20c2021-12-13 14:39:53 -08001181 [path, owner,
1182 self](const boost::system::error_code ec2,
1183 const boost::container::flat_map<
1184 std::string, dbus::utility::DbusVariantType>&
1185 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<
Ed Tanous168e20c2021-12-13 14:39:53 -08001399 std::string, dbus::utility::DbusVariantType>& r) {
Ed Tanouscb13a392020-07-25 19:02:03 +00001400 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001401 {
George Liu0fda0f12021-11-16 10:06:17 +08001402 BMCWEB_LOG_ERROR
1403 << "SetPIDValues: Can't get thermalModeIface "
1404 << path;
James Feist73df0db2019-03-25 15:29:35 -07001405 messages::internalError(self->asyncResp->res);
1406 return;
1407 }
Ed Tanous271584a2019-07-09 16:24:22 -07001408 const std::string* current = nullptr;
1409 const std::vector<std::string>* supported = nullptr;
1410 for (auto& [key, value] : r)
James Feist73df0db2019-03-25 15:29:35 -07001411 {
1412 if (key == "Current")
1413 {
1414 current = std::get_if<std::string>(&value);
1415 if (current == nullptr)
1416 {
1417 BMCWEB_LOG_ERROR
George Liu0fda0f12021-11-16 10:06:17 +08001418 << "SetPIDValues: thermal mode iface invalid "
James Feist73df0db2019-03-25 15:29:35 -07001419 << path;
1420 messages::internalError(
1421 self->asyncResp->res);
1422 return;
1423 }
1424 }
1425 if (key == "Supported")
1426 {
1427 supported =
1428 std::get_if<std::vector<std::string>>(
1429 &value);
1430 if (supported == nullptr)
1431 {
1432 BMCWEB_LOG_ERROR
George Liu0fda0f12021-11-16 10:06:17 +08001433 << "SetPIDValues: thermal mode iface invalid"
James Feist73df0db2019-03-25 15:29:35 -07001434 << path;
1435 messages::internalError(
1436 self->asyncResp->res);
1437 return;
1438 }
1439 }
1440 }
1441 if (current == nullptr || supported == nullptr)
1442 {
George Liu0fda0f12021-11-16 10:06:17 +08001443 BMCWEB_LOG_ERROR
1444 << "SetPIDValues: thermal mode iface invalid "
1445 << path;
James Feist73df0db2019-03-25 15:29:35 -07001446 messages::internalError(self->asyncResp->res);
1447 return;
1448 }
1449 self->currentProfile = *current;
1450 self->supportedProfiles = *supported;
1451 self->profileConnection = owner;
1452 self->profilePath = path;
1453 },
1454 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
1455 thermalModeIface);
1456 },
1457 "xyz.openbmc_project.ObjectMapper",
1458 "/xyz/openbmc_project/object_mapper",
1459 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
1460 std::array<const char*, 1>{thermalModeIface});
1461 }
1462 ~SetPIDValues()
1463 {
1464 if (asyncResp->res.result() != boost::beast::http::status::ok)
1465 {
1466 return;
1467 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001468 std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001469 if (profile)
1470 {
1471 if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
1472 *profile) == supportedProfiles.end())
1473 {
1474 messages::actionParameterUnknown(response->res, "Profile",
1475 *profile);
1476 return;
1477 }
1478 currentProfile = *profile;
1479 crow::connections::systemBus->async_method_call(
1480 [response](const boost::system::error_code ec) {
1481 if (ec)
1482 {
1483 BMCWEB_LOG_ERROR << "Error patching profile" << ec;
1484 messages::internalError(response->res);
1485 }
1486 },
1487 profileConnection, profilePath,
1488 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
Ed Tanous168e20c2021-12-13 14:39:53 -08001489 "Current", dbus::utility::DbusVariantType(*profile));
James Feist73df0db2019-03-25 15:29:35 -07001490 }
1491
1492 for (auto& containerPair : configuration)
1493 {
1494 auto& container = containerPair.second;
1495 if (!container)
1496 {
1497 continue;
1498 }
James Feist6ee7f772020-02-06 16:25:27 -08001499 BMCWEB_LOG_DEBUG << *container;
1500
James Feist73df0db2019-03-25 15:29:35 -07001501 std::string& type = containerPair.first;
1502
1503 for (nlohmann::json::iterator it = container->begin();
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301504 it != container->end(); ++it)
James Feist73df0db2019-03-25 15:29:35 -07001505 {
1506 const auto& name = it.key();
James Feist6ee7f772020-02-06 16:25:27 -08001507 BMCWEB_LOG_DEBUG << "looking for " << name;
1508
James Feist73df0db2019-03-25 15:29:35 -07001509 auto pathItr =
1510 std::find_if(managedObj.begin(), managedObj.end(),
1511 [&name](const auto& obj) {
1512 return boost::algorithm::ends_with(
1513 obj.first.str, "/" + name);
1514 });
1515 boost::container::flat_map<std::string,
1516 dbus::utility::DbusVariantType>
1517 output;
1518
1519 output.reserve(16); // The pid interface length
1520
1521 // determines if we're patching entity-manager or
1522 // creating a new object
1523 bool createNewObject = (pathItr == managedObj.end());
James Feist6ee7f772020-02-06 16:25:27 -08001524 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
1525
James Feist73df0db2019-03-25 15:29:35 -07001526 std::string iface;
1527 if (type == "PidControllers" || type == "FanControllers")
1528 {
1529 iface = pidConfigurationIface;
1530 if (!createNewObject &&
1531 pathItr->second.find(pidConfigurationIface) ==
1532 pathItr->second.end())
1533 {
1534 createNewObject = true;
1535 }
1536 }
1537 else if (type == "FanZones")
1538 {
1539 iface = pidZoneConfigurationIface;
1540 if (!createNewObject &&
1541 pathItr->second.find(pidZoneConfigurationIface) ==
1542 pathItr->second.end())
1543 {
1544
1545 createNewObject = true;
1546 }
1547 }
1548 else if (type == "StepwiseControllers")
1549 {
1550 iface = stepwiseConfigurationIface;
1551 if (!createNewObject &&
1552 pathItr->second.find(stepwiseConfigurationIface) ==
1553 pathItr->second.end())
1554 {
1555 createNewObject = true;
1556 }
1557 }
James Feist6ee7f772020-02-06 16:25:27 -08001558
1559 if (createNewObject && it.value() == nullptr)
1560 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -05001561 // can't delete a non-existent object
James Feist6ee7f772020-02-06 16:25:27 -08001562 messages::invalidObject(response->res, name);
1563 continue;
1564 }
1565
1566 std::string path;
1567 if (pathItr != managedObj.end())
1568 {
1569 path = pathItr->first.str;
1570 }
1571
James Feist73df0db2019-03-25 15:29:35 -07001572 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
James Feiste69d9de2020-02-07 12:23:27 -08001573
1574 // arbitrary limit to avoid attacks
1575 constexpr const size_t controllerLimit = 500;
James Feist14b0b8d2020-02-12 11:52:07 -08001576 if (createNewObject && objectCount >= controllerLimit)
James Feiste69d9de2020-02-07 12:23:27 -08001577 {
1578 messages::resourceExhaustion(response->res, type);
1579 continue;
1580 }
1581
James Feist73df0db2019-03-25 15:29:35 -07001582 output["Name"] = boost::replace_all_copy(name, "_", " ");
1583
1584 std::string chassis;
1585 CreatePIDRet ret = createPidInterface(
James Feist6ee7f772020-02-06 16:25:27 -08001586 response, type, it, path, managedObj, createNewObject,
1587 output, chassis, currentProfile);
James Feist73df0db2019-03-25 15:29:35 -07001588 if (ret == CreatePIDRet::fail)
1589 {
1590 return;
1591 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001592 if (ret == CreatePIDRet::del)
James Feist73df0db2019-03-25 15:29:35 -07001593 {
1594 continue;
1595 }
1596
1597 if (!createNewObject)
1598 {
1599 for (const auto& property : output)
1600 {
1601 crow::connections::systemBus->async_method_call(
1602 [response,
1603 propertyName{std::string(property.first)}](
1604 const boost::system::error_code ec) {
1605 if (ec)
1606 {
1607 BMCWEB_LOG_ERROR << "Error patching "
1608 << propertyName << ": "
1609 << ec;
1610 messages::internalError(response->res);
1611 return;
1612 }
1613 messages::success(response->res);
1614 },
James Feist6ee7f772020-02-06 16:25:27 -08001615 "xyz.openbmc_project.EntityManager", path,
James Feist73df0db2019-03-25 15:29:35 -07001616 "org.freedesktop.DBus.Properties", "Set", iface,
1617 property.first, property.second);
1618 }
1619 }
1620 else
1621 {
1622 if (chassis.empty())
1623 {
1624 BMCWEB_LOG_ERROR << "Failed to get chassis from config";
1625 messages::invalidObject(response->res, name);
1626 return;
1627 }
1628
1629 bool foundChassis = false;
1630 for (const auto& obj : managedObj)
1631 {
1632 if (boost::algorithm::ends_with(obj.first.str, chassis))
1633 {
1634 chassis = obj.first.str;
1635 foundChassis = true;
1636 break;
1637 }
1638 }
1639 if (!foundChassis)
1640 {
1641 BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
1642 messages::resourceMissingAtURI(
1643 response->res, "/redfish/v1/Chassis/" + chassis);
1644 return;
1645 }
1646
1647 crow::connections::systemBus->async_method_call(
1648 [response](const boost::system::error_code ec) {
1649 if (ec)
1650 {
1651 BMCWEB_LOG_ERROR << "Error Adding Pid Object "
1652 << ec;
1653 messages::internalError(response->res);
1654 return;
1655 }
1656 messages::success(response->res);
1657 },
1658 "xyz.openbmc_project.EntityManager", chassis,
1659 "xyz.openbmc_project.AddObject", "AddObject", output);
1660 }
1661 }
1662 }
1663 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001664 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
James Feist73df0db2019-03-25 15:29:35 -07001665 std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
1666 configuration;
1667 std::optional<std::string> profile;
1668 dbus::utility::ManagedObjectType managedObj;
1669 std::vector<std::string> supportedProfiles;
1670 std::string currentProfile;
1671 std::string profileConnection;
1672 std::string profilePath;
James Feist14b0b8d2020-02-12 11:52:07 -08001673 size_t objectCount = 0;
James Feist73df0db2019-03-25 15:29:35 -07001674};
James Feist83ff9ab2018-08-31 10:18:24 -07001675
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001676/**
1677 * @brief Retrieves BMC manager location data over DBus
1678 *
1679 * @param[in] aResp Shared pointer for completing asynchronous calls
1680 * @param[in] connectionName - service name
1681 * @param[in] path - object path
1682 * @return none
1683 */
zhanghch058d1b46d2021-04-01 11:18:24 +08001684inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001685 const std::string& connectionName,
1686 const std::string& path)
1687{
1688 BMCWEB_LOG_DEBUG << "Get BMC manager Location data.";
1689
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001690 sdbusplus::asio::getProperty<std::string>(
1691 *crow::connections::systemBus, connectionName, path,
1692 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001693 [aResp](const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001694 const std::string& property) {
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001695 if (ec)
1696 {
1697 BMCWEB_LOG_DEBUG << "DBUS response error for "
1698 "Location";
1699 messages::internalError(aResp->res);
1700 return;
1701 }
1702
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001703 aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001704 property;
1705 });
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001706}
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001707// avoid name collision systems.hpp
1708inline void
1709 managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001710{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001711 BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time";
Ed Tanous52cc1122020-07-18 13:51:21 -07001712
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001713 sdbusplus::asio::getProperty<uint64_t>(
1714 *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
1715 "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
1716 "LastRebootTime",
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001717 [aResp](const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001718 const uint64_t lastResetTime) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001719 if (ec)
James Feist83ff9ab2018-08-31 10:18:24 -07001720 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001721 BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
Ed Tanous43b761d2019-02-13 20:10:56 -08001722 return;
1723 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001724
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001725 // LastRebootTime is epoch time, in milliseconds
1726 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001727 uint64_t lastResetTimeStamp = lastResetTime / 1000;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001728
1729 // Convert to ISO 8601 standard
1730 aResp->res.jsonValue["LastResetTime"] =
Nan Zhou1d8782e2021-11-29 22:23:18 -08001731 crow::utility::getDateTimeUint(lastResetTimeStamp);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07001732 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001733}
1734
1735/**
1736 * @brief Set the running firmware image
1737 *
1738 * @param[i,o] aResp - Async response object
1739 * @param[i] runningFirmwareTarget - Image to make the running image
1740 *
1741 * @return void
1742 */
1743inline void
1744 setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1745 const std::string& runningFirmwareTarget)
1746{
1747 // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1748 std::string::size_type idPos = runningFirmwareTarget.rfind('/');
1749 if (idPos == std::string::npos)
1750 {
1751 messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
1752 "@odata.id");
1753 BMCWEB_LOG_DEBUG << "Can't parse firmware ID!";
1754 return;
1755 }
1756 idPos++;
1757 if (idPos >= runningFirmwareTarget.size())
1758 {
1759 messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
1760 "@odata.id");
1761 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
1762 return;
1763 }
1764 std::string firmwareId = runningFirmwareTarget.substr(idPos);
1765
1766 // Make sure the image is valid before setting priority
1767 crow::connections::systemBus->async_method_call(
1768 [aResp, firmwareId, runningFirmwareTarget](
1769 const boost::system::error_code ec, ManagedObjectType& subtree) {
1770 if (ec)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001771 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001772 BMCWEB_LOG_DEBUG << "D-Bus response error getting objects.";
1773 messages::internalError(aResp->res);
1774 return;
1775 }
1776
1777 if (subtree.size() == 0)
1778 {
1779 BMCWEB_LOG_DEBUG << "Can't find image!";
1780 messages::internalError(aResp->res);
1781 return;
1782 }
1783
1784 bool foundImage = false;
1785 for (auto& object : subtree)
1786 {
1787 const std::string& path =
1788 static_cast<const std::string&>(object.first);
1789 std::size_t idPos2 = path.rfind('/');
1790
1791 if (idPos2 == std::string::npos)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001792 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001793 continue;
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001794 }
1795
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001796 idPos2++;
1797 if (idPos2 >= path.size())
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001798 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001799 continue;
1800 }
1801
1802 if (path.substr(idPos2) == firmwareId)
1803 {
1804 foundImage = true;
1805 break;
Gunnar Mills4bfefa72020-07-30 13:54:29 -05001806 }
1807 }
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301808
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001809 if (!foundImage)
1810 {
1811 messages::propertyValueNotInList(
1812 aResp->res, runningFirmwareTarget, "@odata.id");
1813 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
1814 return;
1815 }
Gunnar Mills4bf2b032020-06-23 22:28:31 -05001816
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001817 BMCWEB_LOG_DEBUG
1818 << "Setting firmware version " + firmwareId + " to priority 0.";
Gunnar Mills4bf2b032020-06-23 22:28:31 -05001819
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001820 // Only support Immediate
1821 // An addition could be a Redfish Setting like
1822 // ActiveSoftwareImageApplyTime and support OnReset
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301823 crow::connections::systemBus->async_method_call(
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001824 [aResp](const boost::system::error_code ec) {
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301825 if (ec)
1826 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001827 BMCWEB_LOG_DEBUG << "D-Bus response error setting.";
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301828 messages::internalError(aResp->res);
1829 return;
1830 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001831 doBMCGracefulRestart(aResp);
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301832 },
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001833
1834 "xyz.openbmc_project.Software.BMC.Updater",
1835 "/xyz/openbmc_project/software/" + firmwareId,
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301836 "org.freedesktop.DBus.Properties", "Set",
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001837 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
Ed Tanous168e20c2021-12-13 14:39:53 -08001838 dbus::utility::DbusVariantType(static_cast<uint8_t>(0)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001839 },
1840 "xyz.openbmc_project.Software.BMC.Updater",
1841 "/xyz/openbmc_project/software", "org.freedesktop.DBus.ObjectManager",
1842 "GetManagedObjects");
1843}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001844
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001845inline void setDateTime(std::shared_ptr<bmcweb::AsyncResp> aResp,
1846 std::string datetime)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001847{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001848 BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001849
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001850 std::stringstream stream(datetime);
1851 // Convert from ISO 8601 to boost local_time
1852 // (BMC only has time in UTC)
1853 boost::posix_time::ptime posixTime;
1854 boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
1855 // Facet gets deleted with the stringsteam
1856 auto ifc = std::make_unique<boost::local_time::local_time_input_facet>(
1857 "%Y-%m-%d %H:%M:%S%F %ZP");
1858 stream.imbue(std::locale(stream.getloc(), ifc.release()));
1859
1860 boost::local_time::local_date_time ldt(boost::local_time::not_a_date_time);
1861
1862 if (stream >> ldt)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001863 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001864 posixTime = ldt.utc_time();
1865 boost::posix_time::time_duration dur = posixTime - epoch;
1866 uint64_t durMicroSecs = static_cast<uint64_t>(dur.total_microseconds());
1867 crow::connections::systemBus->async_method_call(
1868 [aResp{std::move(aResp)}, datetime{std::move(datetime)}](
1869 const boost::system::error_code ec) {
1870 if (ec)
1871 {
1872 BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1873 "DBUS response error "
1874 << ec;
1875 messages::internalError(aResp->res);
1876 return;
1877 }
1878 aResp->res.jsonValue["DateTime"] = datetime;
1879 },
1880 "xyz.openbmc_project.Time.Manager", "/xyz/openbmc_project/time/bmc",
1881 "org.freedesktop.DBus.Properties", "Set",
1882 "xyz.openbmc_project.Time.EpochTime", "Elapsed",
Ed Tanous168e20c2021-12-13 14:39:53 -08001883 dbus::utility::DbusVariantType(durMicroSecs));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001884 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001885 else
1886 {
1887 messages::propertyValueFormatError(aResp->res, datetime, "DateTime");
1888 return;
1889 }
1890}
1891
1892inline void requestRoutesManager(App& app)
1893{
1894 std::string uuid = persistent_data::getConfig().systemUuid;
1895
1896 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07001897 .privileges(redfish::privileges::getManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001898 .methods(boost::beast::http::verb::get)([uuid](const crow::Request&,
1899 const std::shared_ptr<
1900 bmcweb::AsyncResp>&
1901 asyncResp) {
1902 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
1903 asyncResp->res.jsonValue["@odata.type"] =
1904 "#Manager.v1_11_0.Manager";
1905 asyncResp->res.jsonValue["Id"] = "bmc";
1906 asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
1907 asyncResp->res.jsonValue["Description"] =
1908 "Baseboard Management Controller";
1909 asyncResp->res.jsonValue["PowerState"] = "On";
1910 asyncResp->res.jsonValue["Status"] = {{"State", "Enabled"},
1911 {"Health", "OK"}};
1912 asyncResp->res.jsonValue["ManagerType"] = "BMC";
1913 asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
1914 asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1915 asyncResp->res.jsonValue["Model"] =
1916 "OpenBmc"; // TODO(ed), get model
1917
1918 asyncResp->res.jsonValue["LogServices"] = {
1919 {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}};
1920
1921 asyncResp->res.jsonValue["NetworkProtocol"] = {
1922 {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}};
1923
1924 asyncResp->res.jsonValue["EthernetInterfaces"] = {
1925 {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}};
1926
1927#ifdef BMCWEB_ENABLE_VM_NBDPROXY
1928 asyncResp->res.jsonValue["VirtualMedia"] = {
1929 {"@odata.id", "/redfish/v1/Managers/bmc/VirtualMedia"}};
1930#endif // BMCWEB_ENABLE_VM_NBDPROXY
1931
1932 // default oem data
1933 nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
1934 nlohmann::json& oemOpenbmc = oem["OpenBmc"];
1935 oem["@odata.type"] = "#OemManager.Oem";
1936 oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
1937 oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
1938 oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
1939 oemOpenbmc["Certificates"] = {
1940 {"@odata.id",
1941 "/redfish/v1/Managers/bmc/Truststore/Certificates"}};
1942
1943 // Manager.Reset (an action) can be many values, OpenBMC only
1944 // supports BMC reboot.
1945 nlohmann::json& managerReset =
1946 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
1947 managerReset["target"] =
1948 "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
1949 managerReset["@Redfish.ActionInfo"] =
1950 "/redfish/v1/Managers/bmc/ResetActionInfo";
1951
1952 // ResetToDefaults (Factory Reset) has values like
1953 // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
1954 // on OpenBMC
1955 nlohmann::json& resetToDefaults =
1956 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
1957 resetToDefaults["target"] =
1958 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
1959 resetToDefaults["ResetType@Redfish.AllowableValues"] = {"ResetAll"};
1960
Tejas Patil7c8c4052021-06-04 17:43:14 +05301961 std::pair<std::string, std::string> redfishDateTimeOffset =
1962 crow::utility::getDateTimeOffsetNow();
1963
1964 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1965 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1966 redfishDateTimeOffset.second;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001967
Gunnar Mills0e8ac5e2020-11-06 15:33:24 -06001968 // TODO (Gunnar): Remove these one day since moved to ComputerSystem
1969 // Still used by OCP profiles
1970 // https://github.com/opencomputeproject/OCP-Profiles/issues/23
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001971 // Fill in SerialConsole info
1972 asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
1973 asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] =
1974 15;
1975 asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
1976 {"IPMI", "SSH"};
1977#ifdef BMCWEB_ENABLE_KVM
1978 // Fill in GraphicalConsole info
1979 asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] =
1980 true;
1981 asyncResp->res
1982 .jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4;
1983 asyncResp->res.jsonValue["GraphicalConsole"]
1984 ["ConnectTypesSupported"] = {"KVMIP"};
1985#endif // BMCWEB_ENABLE_KVM
1986
1987 asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] =
1988 1;
1989 asyncResp->res.jsonValue["Links"]["ManagerForServers"] = {
1990 {{"@odata.id", "/redfish/v1/Systems/system"}}};
1991
1992 auto health = std::make_shared<HealthPopulate>(asyncResp);
1993 health->isManagersHealth = true;
1994 health->populate();
1995
1996 fw_util::populateFirmwareInformation(asyncResp, fw_util::bmcPurpose,
1997 "FirmwareVersion", true);
1998
1999 managerGetLastResetTime(asyncResp);
2000
2001 auto pids = std::make_shared<GetPIDValues>(asyncResp);
2002 pids->run();
2003
2004 getMainChassisId(
2005 asyncResp, [](const std::string& chassisId,
2006 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2007 aRsp->res
2008 .jsonValue["Links"]["ManagerForChassis@odata.count"] =
2009 1;
2010 aRsp->res.jsonValue["Links"]["ManagerForChassis"] = {
2011 {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
2012 aRsp->res.jsonValue["Links"]["ManagerInChassis"] = {
2013 {"@odata.id", "/redfish/v1/Chassis/" + chassisId}};
2014 });
2015
2016 static bool started = false;
2017
2018 if (!started)
2019 {
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002020 sdbusplus::asio::getProperty<double>(
2021 *crow::connections::systemBus, "org.freedesktop.systemd1",
2022 "/org/freedesktop/systemd1",
2023 "org.freedesktop.systemd1.Manager", "Progress",
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002024 [asyncResp](const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002025 const double& val) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002026 if (ec)
2027 {
2028 BMCWEB_LOG_ERROR << "Error while getting progress";
2029 messages::internalError(asyncResp->res);
2030 return;
2031 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002032 if (val < 1.0)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002033 {
2034 asyncResp->res.jsonValue["Status"]["State"] =
2035 "Starting";
2036 started = true;
2037 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -07002038 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002039 }
2040
2041 crow::connections::systemBus->async_method_call(
2042 [asyncResp](
2043 const boost::system::error_code ec,
2044 const std::vector<
2045 std::pair<std::string,
2046 std::vector<std::pair<
2047 std::string, std::vector<std::string>>>>>&
2048 subtree) {
2049 if (ec)
2050 {
2051 BMCWEB_LOG_DEBUG
2052 << "D-Bus response error on GetSubTree " << ec;
2053 return;
2054 }
2055 if (subtree.size() == 0)
2056 {
2057 BMCWEB_LOG_DEBUG << "Can't find bmc D-Bus object!";
2058 return;
2059 }
2060 // Assume only 1 bmc D-Bus object
2061 // Throw an error if there is more than 1
2062 if (subtree.size() > 1)
2063 {
2064 BMCWEB_LOG_DEBUG
2065 << "Found more than 1 bmc D-Bus object!";
2066 messages::internalError(asyncResp->res);
2067 return;
2068 }
2069
2070 if (subtree[0].first.empty() ||
2071 subtree[0].second.size() != 1)
2072 {
2073 BMCWEB_LOG_DEBUG << "Error getting bmc D-Bus object!";
2074 messages::internalError(asyncResp->res);
2075 return;
2076 }
2077
2078 const std::string& path = subtree[0].first;
2079 const std::string& connectionName =
2080 subtree[0].second[0].first;
2081
2082 for (const auto& interfaceName :
2083 subtree[0].second[0].second)
2084 {
2085 if (interfaceName ==
2086 "xyz.openbmc_project.Inventory.Decorator.Asset")
2087 {
2088 crow::connections::systemBus->async_method_call(
2089 [asyncResp](
2090 const boost::system::error_code ec,
Ed Tanous168e20c2021-12-13 14:39:53 -08002091 const std::vector<std::pair<
2092 std::string,
2093 dbus::utility::DbusVariantType>>&
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002094 propertiesList) {
2095 if (ec)
2096 {
2097 BMCWEB_LOG_DEBUG
2098 << "Can't get bmc asset!";
2099 return;
2100 }
2101 for (const std::pair<
2102 std::string,
Ed Tanous168e20c2021-12-13 14:39:53 -08002103 dbus::utility::DbusVariantType>&
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002104 property : propertiesList)
2105 {
2106 const std::string& propertyName =
2107 property.first;
2108
2109 if ((propertyName == "PartNumber") ||
2110 (propertyName == "SerialNumber") ||
2111 (propertyName == "Manufacturer") ||
2112 (propertyName == "Model") ||
2113 (propertyName == "SparePartNumber"))
2114 {
2115 const std::string* value =
2116 std::get_if<std::string>(
2117 &property.second);
2118 if (value == nullptr)
2119 {
2120 // illegal property
2121 messages::internalError(
2122 asyncResp->res);
2123 return;
2124 }
2125 asyncResp->res
2126 .jsonValue[propertyName] =
2127 *value;
2128 }
2129 }
2130 },
2131 connectionName, path,
2132 "org.freedesktop.DBus.Properties", "GetAll",
George Liu0fda0f12021-11-16 10:06:17 +08002133 "xyz.openbmc_project.Inventory.Decorator.Asset");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002134 }
George Liu0fda0f12021-11-16 10:06:17 +08002135 else if (
2136 interfaceName ==
2137 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002138 {
2139 getLocation(asyncResp, connectionName, path);
2140 }
2141 }
2142 },
2143 "xyz.openbmc_project.ObjectMapper",
2144 "/xyz/openbmc_project/object_mapper",
2145 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2146 "/xyz/openbmc_project/inventory", int32_t(0),
2147 std::array<const char*, 1>{
2148 "xyz.openbmc_project.Inventory.Item.Bmc"});
2149 });
2150
2151 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
Ed Tanoused398212021-06-09 17:05:54 -07002152 .privileges(redfish::privileges::patchManager)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002153 .methods(
2154 boost::beast::http::verb::
2155 patch)([](const crow::Request& req,
2156 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2157 std::optional<nlohmann::json> oem;
2158 std::optional<nlohmann::json> links;
2159 std::optional<std::string> datetime;
2160
2161 if (!json_util::readJson(req, asyncResp->res, "Oem", oem,
2162 "DateTime", datetime, "Links", links))
2163 {
2164 return;
2165 }
2166
2167 if (oem)
2168 {
2169 std::optional<nlohmann::json> openbmc;
2170 if (!redfish::json_util::readJson(*oem, asyncResp->res,
2171 "OpenBmc", openbmc))
2172 {
2173 BMCWEB_LOG_ERROR
2174 << "Illegal Property "
2175 << oem->dump(2, ' ', true,
2176 nlohmann::json::error_handler_t::replace);
2177 return;
2178 }
2179 if (openbmc)
2180 {
2181 std::optional<nlohmann::json> fan;
2182 if (!redfish::json_util::readJson(*openbmc, asyncResp->res,
2183 "Fan", fan))
2184 {
2185 BMCWEB_LOG_ERROR
2186 << "Illegal Property "
2187 << openbmc->dump(
2188 2, ' ', true,
2189 nlohmann::json::error_handler_t::replace);
2190 return;
2191 }
2192 if (fan)
2193 {
2194 auto pid =
2195 std::make_shared<SetPIDValues>(asyncResp, *fan);
2196 pid->run();
2197 }
2198 }
2199 }
2200 if (links)
2201 {
2202 std::optional<nlohmann::json> activeSoftwareImage;
2203 if (!redfish::json_util::readJson(*links, asyncResp->res,
2204 "ActiveSoftwareImage",
2205 activeSoftwareImage))
2206 {
2207 return;
2208 }
2209 if (activeSoftwareImage)
2210 {
2211 std::optional<std::string> odataId;
2212 if (!json_util::readJson(*activeSoftwareImage,
2213 asyncResp->res, "@odata.id",
2214 odataId))
2215 {
2216 return;
2217 }
2218
2219 if (odataId)
2220 {
2221 setActiveFirmwareImage(asyncResp, *odataId);
2222 }
2223 }
2224 }
2225 if (datetime)
2226 {
2227 setDateTime(asyncResp, std::move(*datetime));
2228 }
2229 });
2230}
2231
2232inline void requestRoutesManagerCollection(App& app)
2233{
2234 BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
Ed Tanoused398212021-06-09 17:05:54 -07002235 .privileges(redfish::privileges::getManagerCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002236 .methods(boost::beast::http::verb::get)(
2237 [](const crow::Request&,
2238 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2239 // Collections don't include the static data added by SubRoute
2240 // because it has a duplicate entry for members
2241 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
2242 asyncResp->res.jsonValue["@odata.type"] =
2243 "#ManagerCollection.ManagerCollection";
2244 asyncResp->res.jsonValue["Name"] = "Manager Collection";
2245 asyncResp->res.jsonValue["Members@odata.count"] = 1;
2246 asyncResp->res.jsonValue["Members"] = {
2247 {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
2248 });
2249}
Ed Tanous1abe55e2018-09-05 08:30:59 -07002250} // namespace redfish