blob: ea0589e862d46f67fc750103a63fc14805cf8e64 [file] [log] [blame]
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
James Feistb49ac872019-05-21 15:12:01 -070018#include "health.hpp"
Borawski.Lukasz9c3106852018-02-09 15:24:22 +010019#include "node.hpp"
Jennifer Leec5d03ff2019-03-08 15:42:58 -080020#include "redfish_util.hpp"
Borawski.Lukasz9c3106852018-02-09 15:24:22 +010021
James Feist5b4aa862018-08-16 14:07:01 -070022#include <boost/algorithm/string/replace.hpp>
Santosh Puranikaf5d60582019-03-20 18:16:36 +053023#include <boost/date_time.hpp>
James Feist5b4aa862018-08-16 14:07:01 -070024#include <dbus_utility.hpp>
Andrew Geisslere90c5052019-06-28 13:52:27 -050025#include <utils/fw_utils.hpp>
Bernard Wong7bffdb72019-03-20 16:17:21 +080026#include <utils/systemd_utils.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050027
Gunnar Mills4bfefa72020-07-30 13:54:29 -050028#include <cstdint>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050029#include <memory>
30#include <sstream>
Ed Tanousabf2add2019-01-22 16:40:12 -080031#include <variant>
James Feist5b4aa862018-08-16 14:07:01 -070032
Ed Tanous1abe55e2018-09-05 08:30:59 -070033namespace redfish
34{
Jennifer Leeed5befb2018-08-10 11:29:45 -070035
36/**
Gunnar Mills2a5c4402020-05-19 09:07:24 -050037 * Function reboots the BMC.
38 *
39 * @param[in] asyncResp - Shared pointer for completing asynchronous calls
Jennifer Leeed5befb2018-08-10 11:29:45 -070040 */
Ed Tanousb5a76932020-09-29 16:16:58 -070041inline void doBMCGracefulRestart(const std::shared_ptr<AsyncResp>& asyncResp)
Gunnar Mills2a5c4402020-05-19 09:07:24 -050042{
43 const char* processName = "xyz.openbmc_project.State.BMC";
44 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
45 const char* interfaceName = "xyz.openbmc_project.State.BMC";
46 const std::string& propertyValue =
47 "xyz.openbmc_project.State.BMC.Transition.Reboot";
48 const char* destProperty = "RequestedBMCTransition";
49
50 // Create the D-Bus variant for D-Bus call.
51 VariantType dbusPropertyValue(propertyValue);
52
53 crow::connections::systemBus->async_method_call(
54 [asyncResp](const boost::system::error_code ec) {
55 // Use "Set" method to set the property value.
56 if (ec)
57 {
58 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
59 messages::internalError(asyncResp->res);
60 return;
61 }
62
63 messages::success(asyncResp->res);
64 },
65 processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
66 interfaceName, destProperty, dbusPropertyValue);
67}
68
Ed Tanousb5a76932020-09-29 16:16:58 -070069inline void doBMCForceRestart(const std::shared_ptr<AsyncResp>& asyncResp)
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +000070{
71 const char* processName = "xyz.openbmc_project.State.BMC";
72 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
73 const char* interfaceName = "xyz.openbmc_project.State.BMC";
74 const std::string& propertyValue =
75 "xyz.openbmc_project.State.BMC.Transition.HardReboot";
76 const char* destProperty = "RequestedBMCTransition";
77
78 // Create the D-Bus variant for D-Bus call.
79 VariantType dbusPropertyValue(propertyValue);
80
81 crow::connections::systemBus->async_method_call(
82 [asyncResp](const boost::system::error_code ec) {
83 // Use "Set" method to set the property value.
84 if (ec)
85 {
86 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
87 messages::internalError(asyncResp->res);
88 return;
89 }
90
91 messages::success(asyncResp->res);
92 },
93 processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
94 interfaceName, destProperty, dbusPropertyValue);
95}
96
Gunnar Mills2a5c4402020-05-19 09:07:24 -050097/**
98 * ManagerResetAction class supports the POST method for the Reset (reboot)
99 * action.
100 */
101class ManagerResetAction : public Node
Jennifer Leeed5befb2018-08-10 11:29:45 -0700102{
103 public:
Ed Tanous52cc1122020-07-18 13:51:21 -0700104 ManagerResetAction(App& app) :
Jennifer Leeed5befb2018-08-10 11:29:45 -0700105 Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
106 {
107 entityPrivileges = {
Jennifer Leeed5befb2018-08-10 11:29:45 -0700108 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
109 }
110
111 private:
112 /**
Jennifer Leeed5befb2018-08-10 11:29:45 -0700113 * Function handles POST method request.
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500114 * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000115 * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
Jennifer Leeed5befb2018-08-10 11:29:45 -0700116 */
117 void doPost(crow::Response& res, const crow::Request& req,
Ed Tanouscb13a392020-07-25 19:02:03 +0000118 const std::vector<std::string>&) override
Jennifer Leeed5befb2018-08-10 11:29:45 -0700119 {
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500120 BMCWEB_LOG_DEBUG << "Post Manager Reset.";
Jennifer Leeed5befb2018-08-10 11:29:45 -0700121
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500122 std::string resetType;
123 auto asyncResp = std::make_shared<AsyncResp>(res);
124
125 if (!json_util::readJson(req, asyncResp->res, "ResetType", resetType))
Jennifer Leeed5befb2018-08-10 11:29:45 -0700126 {
127 return;
128 }
129
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000130 if (resetType == "GracefulRestart")
131 {
132 BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
133 doBMCGracefulRestart(asyncResp);
134 return;
135 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700136 if (resetType == "ForceRestart")
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000137 {
138 BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
139 doBMCForceRestart(asyncResp);
140 return;
141 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700142 BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
143 << resetType;
144 messages::actionParameterNotSupported(asyncResp->res, resetType,
145 "ResetType");
Gunnar Mills2a5c4402020-05-19 09:07:24 -0500146
Ed Tanous3174e4d2020-10-07 11:41:22 -0700147 return;
Jennifer Leeed5befb2018-08-10 11:29:45 -0700148 }
149};
150
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500151/**
152 * ManagerResetToDefaultsAction class supports POST method for factory reset
153 * action.
154 */
155class ManagerResetToDefaultsAction : public Node
156{
157 public:
Ed Tanous52cc1122020-07-18 13:51:21 -0700158 ManagerResetToDefaultsAction(App& app) :
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500159 Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/")
160 {
161 entityPrivileges = {
162 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
163 }
164
165 private:
166 /**
167 * Function handles ResetToDefaults POST method request.
168 *
169 * Analyzes POST body message and factory resets BMC by calling
170 * BMC code updater factory reset followed by a BMC reboot.
171 *
172 * BMC code updater factory reset wipes the whole BMC read-write
173 * filesystem which includes things like the network settings.
174 *
175 * OpenBMC only supports ResetToDefaultsType "ResetAll".
176 */
177 void doPost(crow::Response& res, const crow::Request& req,
Ed Tanouscb13a392020-07-25 19:02:03 +0000178 const std::vector<std::string>&) override
Gunnar Mills3e40fc72020-05-19 19:18:17 -0500179 {
180 BMCWEB_LOG_DEBUG << "Post ResetToDefaults.";
181
182 std::string resetType;
183 auto asyncResp = std::make_shared<AsyncResp>(res);
184
185 if (!json_util::readJson(req, asyncResp->res, "ResetToDefaultsType",
186 resetType))
187 {
188 BMCWEB_LOG_DEBUG << "Missing property ResetToDefaultsType.";
189
190 messages::actionParameterMissing(asyncResp->res, "ResetToDefaults",
191 "ResetToDefaultsType");
192 return;
193 }
194
195 if (resetType != "ResetAll")
196 {
197 BMCWEB_LOG_DEBUG << "Invalid property value for "
198 "ResetToDefaultsType: "
199 << resetType;
200 messages::actionParameterNotSupported(asyncResp->res, resetType,
201 "ResetToDefaultsType");
202 return;
203 }
204
205 crow::connections::systemBus->async_method_call(
206 [asyncResp](const boost::system::error_code ec) {
207 if (ec)
208 {
209 BMCWEB_LOG_DEBUG << "Failed to ResetToDefaults: " << ec;
210 messages::internalError(asyncResp->res);
211 return;
212 }
213 // Factory Reset doesn't actually happen until a reboot
214 // Can't erase what the BMC is running on
215 doBMCGracefulRestart(asyncResp);
216 },
217 "xyz.openbmc_project.Software.BMC.Updater",
218 "/xyz/openbmc_project/software",
219 "xyz.openbmc_project.Common.FactoryReset", "Reset");
220 }
221};
222
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530223/**
224 * ManagerResetActionInfo derived class for delivering Manager
225 * ResetType AllowableValues using ResetInfo schema.
226 */
227class ManagerResetActionInfo : public Node
228{
229 public:
230 /*
231 * Default Constructor
232 */
Ed Tanous52cc1122020-07-18 13:51:21 -0700233 ManagerResetActionInfo(App& app) :
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530234 Node(app, "/redfish/v1/Managers/bmc/ResetActionInfo/")
235 {
236 entityPrivileges = {
237 {boost::beast::http::verb::get, {{"Login"}}},
238 {boost::beast::http::verb::head, {{"Login"}}},
239 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
240 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
241 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
242 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
243 }
244
245 private:
246 /**
247 * Functions triggers appropriate requests on DBus
248 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000249 void doGet(crow::Response& res, const crow::Request&,
250 const std::vector<std::string>&) override
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530251 {
252 res.jsonValue = {
253 {"@odata.type", "#ActionInfo.v1_1_2.ActionInfo"},
254 {"@odata.id", "/redfish/v1/Managers/bmc/ResetActionInfo"},
255 {"Name", "Reset Action Info"},
256 {"Id", "ResetActionInfo"},
257 {"Parameters",
258 {{{"Name", "ResetType"},
259 {"Required", true},
260 {"DataType", "String"},
Jayaprakash Mutyalaf92af382020-06-16 23:29:41 +0000261 {"AllowableValues", {"GracefulRestart", "ForceRestart"}}}}}};
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530262 res.end();
263 }
264};
265
James Feist5b4aa862018-08-16 14:07:01 -0700266static constexpr const char* objectManagerIface =
267 "org.freedesktop.DBus.ObjectManager";
268static constexpr const char* pidConfigurationIface =
269 "xyz.openbmc_project.Configuration.Pid";
270static constexpr const char* pidZoneConfigurationIface =
271 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feistb7a08d02018-12-11 14:55:37 -0800272static constexpr const char* stepwiseConfigurationIface =
273 "xyz.openbmc_project.Configuration.Stepwise";
James Feist73df0db2019-03-25 15:29:35 -0700274static constexpr const char* thermalModeIface =
275 "xyz.openbmc_project.Control.ThermalMode";
Borawski.Lukasz9c3106852018-02-09 15:24:22 +0100276
Ed Tanous23a21a12020-07-25 04:45:05 +0000277inline void asyncPopulatePid(const std::string& connection,
James Feist5b4aa862018-08-16 14:07:01 -0700278 const std::string& path,
James Feist73df0db2019-03-25 15:29:35 -0700279 const std::string& currentProfile,
280 const std::vector<std::string>& supportedProfiles,
Ed Tanousb5a76932020-09-29 16:16:58 -0700281 const std::shared_ptr<AsyncResp>& asyncResp)
James Feist5b4aa862018-08-16 14:07:01 -0700282{
283
284 crow::connections::systemBus->async_method_call(
James Feist73df0db2019-03-25 15:29:35 -0700285 [asyncResp, currentProfile, supportedProfiles](
286 const boost::system::error_code ec,
287 const dbus::utility::ManagedObjectType& managedObj) {
James Feist5b4aa862018-08-16 14:07:01 -0700288 if (ec)
289 {
290 BMCWEB_LOG_ERROR << ec;
James Feist5b4aa862018-08-16 14:07:01 -0700291 asyncResp->res.jsonValue.clear();
Jason M. Billsf12894f2018-10-09 12:45:45 -0700292 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700293 return;
294 }
295 nlohmann::json& configRoot =
296 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
297 nlohmann::json& fans = configRoot["FanControllers"];
298 fans["@odata.type"] = "#OemManager.FanControllers";
James Feist5b4aa862018-08-16 14:07:01 -0700299 fans["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/"
300 "Fan/FanControllers";
301
302 nlohmann::json& pids = configRoot["PidControllers"];
303 pids["@odata.type"] = "#OemManager.PidControllers";
James Feist5b4aa862018-08-16 14:07:01 -0700304 pids["@odata.id"] =
305 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
306
James Feistb7a08d02018-12-11 14:55:37 -0800307 nlohmann::json& stepwise = configRoot["StepwiseControllers"];
308 stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
James Feistb7a08d02018-12-11 14:55:37 -0800309 stepwise["@odata.id"] =
310 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
311
James Feist5b4aa862018-08-16 14:07:01 -0700312 nlohmann::json& zones = configRoot["FanZones"];
313 zones["@odata.id"] =
314 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
315 zones["@odata.type"] = "#OemManager.FanZones";
James Feist5b4aa862018-08-16 14:07:01 -0700316 configRoot["@odata.id"] =
317 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
318 configRoot["@odata.type"] = "#OemManager.Fan";
James Feist73df0db2019-03-25 15:29:35 -0700319 configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
320
321 if (!currentProfile.empty())
322 {
323 configRoot["Profile"] = currentProfile;
324 }
325 BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !";
James Feist5b4aa862018-08-16 14:07:01 -0700326
James Feist5b4aa862018-08-16 14:07:01 -0700327 for (const auto& pathPair : managedObj)
328 {
329 for (const auto& intfPair : pathPair.second)
330 {
331 if (intfPair.first != pidConfigurationIface &&
James Feistb7a08d02018-12-11 14:55:37 -0800332 intfPair.first != pidZoneConfigurationIface &&
333 intfPair.first != stepwiseConfigurationIface)
James Feist5b4aa862018-08-16 14:07:01 -0700334 {
335 continue;
336 }
337 auto findName = intfPair.second.find("Name");
338 if (findName == intfPair.second.end())
339 {
340 BMCWEB_LOG_ERROR << "Pid Field missing Name";
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800341 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700342 return;
343 }
James Feist73df0db2019-03-25 15:29:35 -0700344
James Feist5b4aa862018-08-16 14:07:01 -0700345 const std::string* namePtr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800346 std::get_if<std::string>(&findName->second);
James Feist5b4aa862018-08-16 14:07:01 -0700347 if (namePtr == nullptr)
348 {
349 BMCWEB_LOG_ERROR << "Pid Name Field illegal";
James Feistb7a08d02018-12-11 14:55:37 -0800350 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700351 return;
352 }
James Feist5b4aa862018-08-16 14:07:01 -0700353 std::string name = *namePtr;
354 dbus::utility::escapePathForDbus(name);
James Feist73df0db2019-03-25 15:29:35 -0700355
356 auto findProfiles = intfPair.second.find("Profiles");
357 if (findProfiles != intfPair.second.end())
358 {
359 const std::vector<std::string>* profiles =
360 std::get_if<std::vector<std::string>>(
361 &findProfiles->second);
362 if (profiles == nullptr)
363 {
364 BMCWEB_LOG_ERROR << "Pid Profiles Field illegal";
365 messages::internalError(asyncResp->res);
366 return;
367 }
368 if (std::find(profiles->begin(), profiles->end(),
369 currentProfile) == profiles->end())
370 {
371 BMCWEB_LOG_INFO
372 << name << " not supported in current profile";
373 continue;
374 }
375 }
James Feistb7a08d02018-12-11 14:55:37 -0800376 nlohmann::json* config = nullptr;
James Feistc33a90e2019-03-01 10:17:44 -0800377
378 const std::string* classPtr = nullptr;
379 auto findClass = intfPair.second.find("Class");
380 if (findClass != intfPair.second.end())
381 {
382 classPtr = std::get_if<std::string>(&findClass->second);
383 }
384
James Feist5b4aa862018-08-16 14:07:01 -0700385 if (intfPair.first == pidZoneConfigurationIface)
386 {
387 std::string chassis;
388 if (!dbus::utility::getNthStringFromPath(
389 pathPair.first.str, 5, chassis))
390 {
391 chassis = "#IllegalValue";
392 }
393 nlohmann::json& zone = zones[name];
394 zone["Chassis"] = {
395 {"@odata.id", "/redfish/v1/Chassis/" + chassis}};
396 zone["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/"
397 "OpenBmc/Fan/FanZones/" +
398 name;
399 zone["@odata.type"] = "#OemManager.FanZone";
James Feistb7a08d02018-12-11 14:55:37 -0800400 config = &zone;
James Feist5b4aa862018-08-16 14:07:01 -0700401 }
402
James Feistb7a08d02018-12-11 14:55:37 -0800403 else if (intfPair.first == stepwiseConfigurationIface)
404 {
James Feistc33a90e2019-03-01 10:17:44 -0800405 if (classPtr == nullptr)
406 {
407 BMCWEB_LOG_ERROR << "Pid Class Field illegal";
408 messages::internalError(asyncResp->res);
409 return;
410 }
411
James Feistb7a08d02018-12-11 14:55:37 -0800412 nlohmann::json& controller = stepwise[name];
413 config = &controller;
414
415 controller["@odata.id"] =
416 "/redfish/v1/Managers/bmc#/Oem/"
417 "OpenBmc/Fan/StepwiseControllers/" +
Ed Tanous271584a2019-07-09 16:24:22 -0700418 name;
James Feistb7a08d02018-12-11 14:55:37 -0800419 controller["@odata.type"] =
420 "#OemManager.StepwiseController";
421
James Feistc33a90e2019-03-01 10:17:44 -0800422 controller["Direction"] = *classPtr;
James Feistb7a08d02018-12-11 14:55:37 -0800423 }
424
425 // pid and fans are off the same configuration
426 else if (intfPair.first == pidConfigurationIface)
427 {
James Feistc33a90e2019-03-01 10:17:44 -0800428
James Feistb7a08d02018-12-11 14:55:37 -0800429 if (classPtr == nullptr)
430 {
431 BMCWEB_LOG_ERROR << "Pid Class Field illegal";
432 messages::internalError(asyncResp->res);
433 return;
434 }
435 bool isFan = *classPtr == "fan";
436 nlohmann::json& element =
437 isFan ? fans[name] : pids[name];
438 config = &element;
439 if (isFan)
440 {
441 element["@odata.id"] =
442 "/redfish/v1/Managers/bmc#/Oem/"
443 "OpenBmc/Fan/FanControllers/" +
Ed Tanous271584a2019-07-09 16:24:22 -0700444 name;
James Feistb7a08d02018-12-11 14:55:37 -0800445 element["@odata.type"] =
446 "#OemManager.FanController";
James Feistb7a08d02018-12-11 14:55:37 -0800447 }
448 else
449 {
450 element["@odata.id"] =
451 "/redfish/v1/Managers/bmc#/Oem/"
452 "OpenBmc/Fan/PidControllers/" +
Ed Tanous271584a2019-07-09 16:24:22 -0700453 name;
James Feistb7a08d02018-12-11 14:55:37 -0800454 element["@odata.type"] =
455 "#OemManager.PidController";
James Feistb7a08d02018-12-11 14:55:37 -0800456 }
457 }
458 else
459 {
460 BMCWEB_LOG_ERROR << "Unexpected configuration";
461 messages::internalError(asyncResp->res);
462 return;
463 }
464
465 // used for making maps out of 2 vectors
466 const std::vector<double>* keys = nullptr;
467 const std::vector<double>* values = nullptr;
468
James Feist5b4aa862018-08-16 14:07:01 -0700469 for (const auto& propertyPair : intfPair.second)
470 {
471 if (propertyPair.first == "Type" ||
472 propertyPair.first == "Class" ||
473 propertyPair.first == "Name")
474 {
475 continue;
476 }
477
478 // zones
479 if (intfPair.first == pidZoneConfigurationIface)
480 {
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800481 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800482 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700483 if (ptr == nullptr)
484 {
485 BMCWEB_LOG_ERROR << "Field Illegal "
486 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700487 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700488 return;
489 }
James Feistb7a08d02018-12-11 14:55:37 -0800490 (*config)[propertyPair.first] = *ptr;
491 }
492
493 if (intfPair.first == stepwiseConfigurationIface)
494 {
495 if (propertyPair.first == "Reading" ||
496 propertyPair.first == "Output")
497 {
498 const std::vector<double>* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800499 std::get_if<std::vector<double>>(
James Feistb7a08d02018-12-11 14:55:37 -0800500 &propertyPair.second);
501
502 if (ptr == nullptr)
503 {
504 BMCWEB_LOG_ERROR << "Field Illegal "
505 << propertyPair.first;
506 messages::internalError(asyncResp->res);
507 return;
508 }
509
510 if (propertyPair.first == "Reading")
511 {
512 keys = ptr;
513 }
514 else
515 {
516 values = ptr;
517 }
518 if (keys && values)
519 {
520 if (keys->size() != values->size())
521 {
522 BMCWEB_LOG_ERROR
523 << "Reading and Output size don't "
524 "match ";
525 messages::internalError(asyncResp->res);
526 return;
527 }
528 nlohmann::json& steps = (*config)["Steps"];
529 steps = nlohmann::json::array();
530 for (size_t ii = 0; ii < keys->size(); ii++)
531 {
532 steps.push_back(
533 {{"Target", (*keys)[ii]},
534 {"Output", (*values)[ii]}});
535 }
536 }
537 }
538 if (propertyPair.first == "NegativeHysteresis" ||
539 propertyPair.first == "PositiveHysteresis")
540 {
541 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800542 std::get_if<double>(&propertyPair.second);
James Feistb7a08d02018-12-11 14:55:37 -0800543 if (ptr == nullptr)
544 {
545 BMCWEB_LOG_ERROR << "Field Illegal "
546 << propertyPair.first;
547 messages::internalError(asyncResp->res);
548 return;
549 }
550 (*config)[propertyPair.first] = *ptr;
551 }
James Feist5b4aa862018-08-16 14:07:01 -0700552 }
553
554 // pid and fans are off the same configuration
James Feistb7a08d02018-12-11 14:55:37 -0800555 if (intfPair.first == pidConfigurationIface ||
556 intfPair.first == stepwiseConfigurationIface)
James Feist5b4aa862018-08-16 14:07:01 -0700557 {
James Feist5b4aa862018-08-16 14:07:01 -0700558
559 if (propertyPair.first == "Zones")
560 {
561 const std::vector<std::string>* inputs =
Ed Tanousabf2add2019-01-22 16:40:12 -0800562 std::get_if<std::vector<std::string>>(
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800563 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700564
565 if (inputs == nullptr)
566 {
567 BMCWEB_LOG_ERROR
568 << "Zones Pid Field Illegal";
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800569 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700570 return;
571 }
James Feistb7a08d02018-12-11 14:55:37 -0800572 auto& data = (*config)[propertyPair.first];
James Feist5b4aa862018-08-16 14:07:01 -0700573 data = nlohmann::json::array();
574 for (std::string itemCopy : *inputs)
575 {
576 dbus::utility::escapePathForDbus(itemCopy);
577 data.push_back(
578 {{"@odata.id",
579 "/redfish/v1/Managers/bmc#/Oem/"
580 "OpenBmc/Fan/FanZones/" +
581 itemCopy}});
582 }
583 }
584 // todo(james): may never happen, but this
585 // assumes configuration data referenced in the
586 // PID config is provided by the same daemon, we
587 // could add another loop to cover all cases,
588 // but I'm okay kicking this can down the road a
589 // bit
590
591 else if (propertyPair.first == "Inputs" ||
592 propertyPair.first == "Outputs")
593 {
James Feistb7a08d02018-12-11 14:55:37 -0800594 auto& data = (*config)[propertyPair.first];
James Feist5b4aa862018-08-16 14:07:01 -0700595 const std::vector<std::string>* inputs =
Ed Tanousabf2add2019-01-22 16:40:12 -0800596 std::get_if<std::vector<std::string>>(
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800597 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700598
599 if (inputs == nullptr)
600 {
601 BMCWEB_LOG_ERROR << "Field Illegal "
602 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700603 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700604 return;
605 }
606 data = *inputs;
James Feistb943aae2019-07-11 16:33:56 -0700607 }
608 else if (propertyPair.first == "SetPointOffset")
609 {
610 const std::string* ptr =
611 std::get_if<std::string>(
612 &propertyPair.second);
613
614 if (ptr == nullptr)
615 {
616 BMCWEB_LOG_ERROR << "Field Illegal "
617 << propertyPair.first;
618 messages::internalError(asyncResp->res);
619 return;
620 }
621 // translate from dbus to redfish
622 if (*ptr == "WarningHigh")
623 {
624 (*config)["SetPointOffset"] =
625 "UpperThresholdNonCritical";
626 }
627 else if (*ptr == "WarningLow")
628 {
629 (*config)["SetPointOffset"] =
630 "LowerThresholdNonCritical";
631 }
632 else if (*ptr == "CriticalHigh")
633 {
634 (*config)["SetPointOffset"] =
635 "UpperThresholdCritical";
636 }
637 else if (*ptr == "CriticalLow")
638 {
639 (*config)["SetPointOffset"] =
640 "LowerThresholdCritical";
641 }
642 else
643 {
644 BMCWEB_LOG_ERROR << "Value Illegal "
645 << *ptr;
646 messages::internalError(asyncResp->res);
647 return;
648 }
649 }
650 // doubles
James Feist5b4aa862018-08-16 14:07:01 -0700651 else if (propertyPair.first ==
652 "FFGainCoefficient" ||
653 propertyPair.first == "FFOffCoefficient" ||
654 propertyPair.first == "ICoefficient" ||
655 propertyPair.first == "ILimitMax" ||
656 propertyPair.first == "ILimitMin" ||
James Feistaad1a252019-02-19 10:13:52 -0800657 propertyPair.first ==
658 "PositiveHysteresis" ||
659 propertyPair.first ==
660 "NegativeHysteresis" ||
James Feist5b4aa862018-08-16 14:07:01 -0700661 propertyPair.first == "OutLimitMax" ||
662 propertyPair.first == "OutLimitMin" ||
663 propertyPair.first == "PCoefficient" ||
James Feist7625cb82019-01-23 11:58:21 -0800664 propertyPair.first == "SetPoint" ||
James Feist5b4aa862018-08-16 14:07:01 -0700665 propertyPair.first == "SlewNeg" ||
666 propertyPair.first == "SlewPos")
667 {
668 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800669 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700670 if (ptr == nullptr)
671 {
672 BMCWEB_LOG_ERROR << "Field Illegal "
673 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700674 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700675 return;
676 }
James Feistb7a08d02018-12-11 14:55:37 -0800677 (*config)[propertyPair.first] = *ptr;
James Feist5b4aa862018-08-16 14:07:01 -0700678 }
679 }
680 }
681 }
682 }
683 },
684 connection, path, objectManagerIface, "GetManagedObjects");
685}
Jennifer Leeca537922018-08-10 10:07:30 -0700686
James Feist83ff9ab2018-08-31 10:18:24 -0700687enum class CreatePIDRet
688{
689 fail,
690 del,
691 patch
692};
693
Ed Tanous23a21a12020-07-25 04:45:05 +0000694inline bool getZonesFromJsonReq(const std::shared_ptr<AsyncResp>& response,
James Feist5f2caae2018-12-12 14:08:25 -0800695 std::vector<nlohmann::json>& config,
696 std::vector<std::string>& zones)
697{
James Feistb6baeaa2019-02-21 10:41:40 -0800698 if (config.empty())
699 {
700 BMCWEB_LOG_ERROR << "Empty Zones";
701 messages::propertyValueFormatError(response->res,
702 nlohmann::json::array(), "Zones");
703 return false;
704 }
James Feist5f2caae2018-12-12 14:08:25 -0800705 for (auto& odata : config)
706 {
707 std::string path;
708 if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
709 path))
710 {
711 return false;
712 }
713 std::string input;
James Feist61adbda2019-03-25 13:03:51 -0700714
715 // 8 below comes from
716 // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
717 // 0 1 2 3 4 5 6 7 8
718 if (!dbus::utility::getNthStringFromPath(path, 8, input))
James Feist5f2caae2018-12-12 14:08:25 -0800719 {
720 BMCWEB_LOG_ERROR << "Got invalid path " << path;
721 BMCWEB_LOG_ERROR << "Illegal Type Zones";
722 messages::propertyValueFormatError(response->res, odata.dump(),
723 "Zones");
724 return false;
725 }
726 boost::replace_all(input, "_", " ");
727 zones.emplace_back(std::move(input));
728 }
729 return true;
730}
731
Ed Tanous23a21a12020-07-25 04:45:05 +0000732inline const dbus::utility::ManagedItem*
James Feist73df0db2019-03-25 15:29:35 -0700733 findChassis(const dbus::utility::ManagedObjectType& managedObj,
734 const std::string& value, std::string& chassis)
James Feistb6baeaa2019-02-21 10:41:40 -0800735{
736 BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
737
738 std::string escaped = boost::replace_all_copy(value, " ", "_");
739 escaped = "/" + escaped;
740 auto it = std::find_if(
741 managedObj.begin(), managedObj.end(), [&escaped](const auto& obj) {
742 if (boost::algorithm::ends_with(obj.first.str, escaped))
743 {
744 BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
745 return true;
746 }
747 return false;
748 });
749
750 if (it == managedObj.end())
751 {
James Feist73df0db2019-03-25 15:29:35 -0700752 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800753 }
754 // 5 comes from <chassis-name> being the 5th element
755 // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
James Feist73df0db2019-03-25 15:29:35 -0700756 if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
757 {
758 return &(*it);
759 }
760
761 return nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800762}
763
Ed Tanous23a21a12020-07-25 04:45:05 +0000764inline CreatePIDRet createPidInterface(
James Feist83ff9ab2018-08-31 10:18:24 -0700765 const std::shared_ptr<AsyncResp>& response, const std::string& type,
Ed Tanousb5a76932020-09-29 16:16:58 -0700766 const nlohmann::json::iterator& it, const std::string& path,
James Feist83ff9ab2018-08-31 10:18:24 -0700767 const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
768 boost::container::flat_map<std::string, dbus::utility::DbusVariantType>&
769 output,
James Feist73df0db2019-03-25 15:29:35 -0700770 std::string& chassis, const std::string& profile)
James Feist83ff9ab2018-08-31 10:18:24 -0700771{
772
James Feist5f2caae2018-12-12 14:08:25 -0800773 // common deleter
James Feistb6baeaa2019-02-21 10:41:40 -0800774 if (it.value() == nullptr)
James Feist5f2caae2018-12-12 14:08:25 -0800775 {
776 std::string iface;
777 if (type == "PidControllers" || type == "FanControllers")
778 {
779 iface = pidConfigurationIface;
780 }
781 else if (type == "FanZones")
782 {
783 iface = pidZoneConfigurationIface;
784 }
785 else if (type == "StepwiseControllers")
786 {
787 iface = stepwiseConfigurationIface;
788 }
789 else
790 {
Gunnar Millsa0744d32020-11-09 15:40:45 -0600791 BMCWEB_LOG_ERROR << "Illegal Type " << type;
James Feist5f2caae2018-12-12 14:08:25 -0800792 messages::propertyUnknown(response->res, type);
793 return CreatePIDRet::fail;
794 }
James Feist6ee7f772020-02-06 16:25:27 -0800795
796 BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
James Feist5f2caae2018-12-12 14:08:25 -0800797 // delete interface
798 crow::connections::systemBus->async_method_call(
799 [response, path](const boost::system::error_code ec) {
800 if (ec)
801 {
802 BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
803 messages::internalError(response->res);
James Feistb6baeaa2019-02-21 10:41:40 -0800804 return;
James Feist5f2caae2018-12-12 14:08:25 -0800805 }
James Feistb6baeaa2019-02-21 10:41:40 -0800806 messages::success(response->res);
James Feist5f2caae2018-12-12 14:08:25 -0800807 },
808 "xyz.openbmc_project.EntityManager", path, iface, "Delete");
809 return CreatePIDRet::del;
810 }
811
James Feist73df0db2019-03-25 15:29:35 -0700812 const dbus::utility::ManagedItem* managedItem = nullptr;
James Feistb6baeaa2019-02-21 10:41:40 -0800813 if (!createNewObject)
814 {
815 // if we aren't creating a new object, we should be able to find it on
816 // d-bus
James Feist73df0db2019-03-25 15:29:35 -0700817 managedItem = findChassis(managedObj, it.key(), chassis);
818 if (managedItem == nullptr)
James Feistb6baeaa2019-02-21 10:41:40 -0800819 {
820 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
821 messages::invalidObject(response->res, it.key());
822 return CreatePIDRet::fail;
823 }
824 }
825
James Feist73df0db2019-03-25 15:29:35 -0700826 if (profile.size() &&
827 (type == "PidControllers" || type == "FanControllers" ||
828 type == "StepwiseControllers"))
829 {
830 if (managedItem == nullptr)
831 {
832 output["Profiles"] = std::vector<std::string>{profile};
833 }
834 else
835 {
836 std::string interface;
837 if (type == "StepwiseControllers")
838 {
839 interface = stepwiseConfigurationIface;
840 }
841 else
842 {
843 interface = pidConfigurationIface;
844 }
845 auto findConfig = managedItem->second.find(interface);
846 if (findConfig == managedItem->second.end())
847 {
848 BMCWEB_LOG_ERROR
849 << "Failed to find interface in managed object";
850 messages::internalError(response->res);
851 return CreatePIDRet::fail;
852 }
853 auto findProfiles = findConfig->second.find("Profiles");
854 if (findProfiles != findConfig->second.end())
855 {
856 const std::vector<std::string>* curProfiles =
857 std::get_if<std::vector<std::string>>(
858 &(findProfiles->second));
859 if (curProfiles == nullptr)
860 {
861 BMCWEB_LOG_ERROR << "Illegal profiles in managed object";
862 messages::internalError(response->res);
863 return CreatePIDRet::fail;
864 }
865 if (std::find(curProfiles->begin(), curProfiles->end(),
866 profile) == curProfiles->end())
867 {
868 std::vector<std::string> newProfiles = *curProfiles;
869 newProfiles.push_back(profile);
870 output["Profiles"] = newProfiles;
871 }
872 }
873 }
874 }
875
James Feist83ff9ab2018-08-31 10:18:24 -0700876 if (type == "PidControllers" || type == "FanControllers")
877 {
878 if (createNewObject)
879 {
880 output["Class"] = type == "PidControllers" ? std::string("temp")
881 : std::string("fan");
882 output["Type"] = std::string("Pid");
883 }
James Feist5f2caae2018-12-12 14:08:25 -0800884
885 std::optional<std::vector<nlohmann::json>> zones;
886 std::optional<std::vector<std::string>> inputs;
887 std::optional<std::vector<std::string>> outputs;
888 std::map<std::string, std::optional<double>> doubles;
James Feistb943aae2019-07-11 16:33:56 -0700889 std::optional<std::string> setpointOffset;
James Feist5f2caae2018-12-12 14:08:25 -0800890 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -0800891 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
James Feist5f2caae2018-12-12 14:08:25 -0800892 "Zones", zones, "FFGainCoefficient",
893 doubles["FFGainCoefficient"], "FFOffCoefficient",
894 doubles["FFOffCoefficient"], "ICoefficient",
895 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
896 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
897 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
898 "PCoefficient", doubles["PCoefficient"], "SetPoint",
James Feistb943aae2019-07-11 16:33:56 -0700899 doubles["SetPoint"], "SetPointOffset", setpointOffset,
900 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
901 "PositiveHysteresis", doubles["PositiveHysteresis"],
902 "NegativeHysteresis", doubles["NegativeHysteresis"]))
James Feist83ff9ab2018-08-31 10:18:24 -0700903 {
Ed Tanous71f52d92021-02-19 08:51:17 -0800904 BMCWEB_LOG_ERROR
905 << "Illegal Property "
906 << it.value().dump(2, ' ', true,
907 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -0800908 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700909 }
James Feist5f2caae2018-12-12 14:08:25 -0800910 if (zones)
James Feist83ff9ab2018-08-31 10:18:24 -0700911 {
James Feist5f2caae2018-12-12 14:08:25 -0800912 std::vector<std::string> zonesStr;
913 if (!getZonesFromJsonReq(response, *zones, zonesStr))
James Feist83ff9ab2018-08-31 10:18:24 -0700914 {
Gunnar Millsa0744d32020-11-09 15:40:45 -0600915 BMCWEB_LOG_ERROR << "Illegal Zones";
James Feist5f2caae2018-12-12 14:08:25 -0800916 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700917 }
James Feistb6baeaa2019-02-21 10:41:40 -0800918 if (chassis.empty() &&
919 !findChassis(managedObj, zonesStr[0], chassis))
920 {
921 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
922 messages::invalidObject(response->res, it.key());
923 return CreatePIDRet::fail;
924 }
925
James Feist5f2caae2018-12-12 14:08:25 -0800926 output["Zones"] = std::move(zonesStr);
927 }
928 if (inputs || outputs)
929 {
930 std::array<std::optional<std::vector<std::string>>*, 2> containers =
931 {&inputs, &outputs};
932 size_t index = 0;
933 for (const auto& containerPtr : containers)
James Feist83ff9ab2018-08-31 10:18:24 -0700934 {
James Feist5f2caae2018-12-12 14:08:25 -0800935 std::optional<std::vector<std::string>>& container =
936 *containerPtr;
937 if (!container)
James Feist83ff9ab2018-08-31 10:18:24 -0700938 {
James Feist5f2caae2018-12-12 14:08:25 -0800939 index++;
940 continue;
James Feist83ff9ab2018-08-31 10:18:24 -0700941 }
James Feist5f2caae2018-12-12 14:08:25 -0800942
943 for (std::string& value : *container)
James Feist83ff9ab2018-08-31 10:18:24 -0700944 {
James Feist5f2caae2018-12-12 14:08:25 -0800945 boost::replace_all(value, "_", " ");
James Feist83ff9ab2018-08-31 10:18:24 -0700946 }
James Feist5f2caae2018-12-12 14:08:25 -0800947 std::string key;
948 if (index == 0)
James Feist83ff9ab2018-08-31 10:18:24 -0700949 {
James Feist5f2caae2018-12-12 14:08:25 -0800950 key = "Inputs";
James Feist83ff9ab2018-08-31 10:18:24 -0700951 }
James Feist5f2caae2018-12-12 14:08:25 -0800952 else
953 {
954 key = "Outputs";
955 }
956 output[key] = *container;
957 index++;
James Feist83ff9ab2018-08-31 10:18:24 -0700958 }
James Feist5f2caae2018-12-12 14:08:25 -0800959 }
James Feist83ff9ab2018-08-31 10:18:24 -0700960
James Feistb943aae2019-07-11 16:33:56 -0700961 if (setpointOffset)
962 {
963 // translate between redfish and dbus names
964 if (*setpointOffset == "UpperThresholdNonCritical")
965 {
966 output["SetPointOffset"] = std::string("WarningLow");
967 }
968 else if (*setpointOffset == "LowerThresholdNonCritical")
969 {
970 output["SetPointOffset"] = std::string("WarningHigh");
971 }
972 else if (*setpointOffset == "LowerThresholdCritical")
973 {
974 output["SetPointOffset"] = std::string("CriticalLow");
975 }
976 else if (*setpointOffset == "UpperThresholdCritical")
977 {
978 output["SetPointOffset"] = std::string("CriticalHigh");
979 }
980 else
981 {
982 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
983 << *setpointOffset;
984 messages::invalidObject(response->res, it.key());
985 return CreatePIDRet::fail;
986 }
987 }
988
James Feist5f2caae2018-12-12 14:08:25 -0800989 // doubles
990 for (const auto& pairs : doubles)
991 {
992 if (!pairs.second)
James Feist83ff9ab2018-08-31 10:18:24 -0700993 {
James Feist5f2caae2018-12-12 14:08:25 -0800994 continue;
James Feist83ff9ab2018-08-31 10:18:24 -0700995 }
James Feist5f2caae2018-12-12 14:08:25 -0800996 BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
997 output[pairs.first] = *(pairs.second);
James Feist83ff9ab2018-08-31 10:18:24 -0700998 }
999 }
James Feist5f2caae2018-12-12 14:08:25 -08001000
James Feist83ff9ab2018-08-31 10:18:24 -07001001 else if (type == "FanZones")
1002 {
James Feist83ff9ab2018-08-31 10:18:24 -07001003 output["Type"] = std::string("Pid.Zone");
1004
James Feist5f2caae2018-12-12 14:08:25 -08001005 std::optional<nlohmann::json> chassisContainer;
1006 std::optional<double> failSafePercent;
James Feistd3ec07f2019-02-25 14:51:15 -08001007 std::optional<double> minThermalOutput;
James Feistb6baeaa2019-02-21 10:41:40 -08001008 if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
James Feist5f2caae2018-12-12 14:08:25 -08001009 chassisContainer, "FailSafePercent",
James Feistd3ec07f2019-02-25 14:51:15 -08001010 failSafePercent, "MinThermalOutput",
1011 minThermalOutput))
James Feist83ff9ab2018-08-31 10:18:24 -07001012 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001013 BMCWEB_LOG_ERROR
1014 << "Illegal Property "
1015 << it.value().dump(2, ' ', true,
1016 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -08001017 return CreatePIDRet::fail;
1018 }
James Feist83ff9ab2018-08-31 10:18:24 -07001019
James Feist5f2caae2018-12-12 14:08:25 -08001020 if (chassisContainer)
1021 {
1022
1023 std::string chassisId;
1024 if (!redfish::json_util::readJson(*chassisContainer, response->res,
1025 "@odata.id", chassisId))
James Feist83ff9ab2018-08-31 10:18:24 -07001026 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001027 BMCWEB_LOG_ERROR
1028 << "Illegal Property "
1029 << chassisContainer->dump(
1030 2, ' ', true,
1031 nlohmann::json::error_handler_t::replace);
James Feist83ff9ab2018-08-31 10:18:24 -07001032 return CreatePIDRet::fail;
1033 }
James Feist5f2caae2018-12-12 14:08:25 -08001034
AppaRao Puli717794d2019-10-18 22:54:53 +05301035 // /redfish/v1/chassis/chassis_name/
James Feist5f2caae2018-12-12 14:08:25 -08001036 if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
1037 {
1038 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
1039 messages::invalidObject(response->res, chassisId);
1040 return CreatePIDRet::fail;
1041 }
1042 }
James Feistd3ec07f2019-02-25 14:51:15 -08001043 if (minThermalOutput)
James Feist5f2caae2018-12-12 14:08:25 -08001044 {
James Feistd3ec07f2019-02-25 14:51:15 -08001045 output["MinThermalOutput"] = *minThermalOutput;
James Feist5f2caae2018-12-12 14:08:25 -08001046 }
1047 if (failSafePercent)
1048 {
1049 output["FailSafePercent"] = *failSafePercent;
1050 }
1051 }
1052 else if (type == "StepwiseControllers")
1053 {
1054 output["Type"] = std::string("Stepwise");
1055
1056 std::optional<std::vector<nlohmann::json>> zones;
1057 std::optional<std::vector<nlohmann::json>> steps;
1058 std::optional<std::vector<std::string>> inputs;
1059 std::optional<double> positiveHysteresis;
1060 std::optional<double> negativeHysteresis;
James Feistc33a90e2019-03-01 10:17:44 -08001061 std::optional<std::string> direction; // upper clipping curve vs lower
James Feist5f2caae2018-12-12 14:08:25 -08001062 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -08001063 it.value(), response->res, "Zones", zones, "Steps", steps,
1064 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
James Feistc33a90e2019-03-01 10:17:44 -08001065 "NegativeHysteresis", negativeHysteresis, "Direction",
1066 direction))
James Feist5f2caae2018-12-12 14:08:25 -08001067 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001068 BMCWEB_LOG_ERROR
1069 << "Illegal Property "
1070 << it.value().dump(2, ' ', true,
1071 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -08001072 return CreatePIDRet::fail;
1073 }
1074
1075 if (zones)
1076 {
James Feistb6baeaa2019-02-21 10:41:40 -08001077 std::vector<std::string> zonesStrs;
1078 if (!getZonesFromJsonReq(response, *zones, zonesStrs))
James Feist5f2caae2018-12-12 14:08:25 -08001079 {
Gunnar Millsa0744d32020-11-09 15:40:45 -06001080 BMCWEB_LOG_ERROR << "Illegal Zones";
James Feist5f2caae2018-12-12 14:08:25 -08001081 return CreatePIDRet::fail;
1082 }
James Feistb6baeaa2019-02-21 10:41:40 -08001083 if (chassis.empty() &&
1084 !findChassis(managedObj, zonesStrs[0], chassis))
1085 {
1086 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
1087 messages::invalidObject(response->res, it.key());
1088 return CreatePIDRet::fail;
1089 }
1090 output["Zones"] = std::move(zonesStrs);
James Feist5f2caae2018-12-12 14:08:25 -08001091 }
1092 if (steps)
1093 {
1094 std::vector<double> readings;
1095 std::vector<double> outputs;
1096 for (auto& step : *steps)
1097 {
1098 double target;
Ed Tanous23a21a12020-07-25 04:45:05 +00001099 double out;
James Feist5f2caae2018-12-12 14:08:25 -08001100
1101 if (!redfish::json_util::readJson(step, response->res, "Target",
Ed Tanous23a21a12020-07-25 04:45:05 +00001102 target, "Output", out))
James Feist5f2caae2018-12-12 14:08:25 -08001103 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001104 BMCWEB_LOG_ERROR
1105 << "Illegal Property "
1106 << it.value().dump(
1107 2, ' ', true,
1108 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -08001109 return CreatePIDRet::fail;
1110 }
1111 readings.emplace_back(target);
Ed Tanous23a21a12020-07-25 04:45:05 +00001112 outputs.emplace_back(out);
James Feist5f2caae2018-12-12 14:08:25 -08001113 }
1114 output["Reading"] = std::move(readings);
1115 output["Output"] = std::move(outputs);
1116 }
1117 if (inputs)
1118 {
1119 for (std::string& value : *inputs)
1120 {
James Feist5f2caae2018-12-12 14:08:25 -08001121 boost::replace_all(value, "_", " ");
1122 }
1123 output["Inputs"] = std::move(*inputs);
1124 }
1125 if (negativeHysteresis)
1126 {
1127 output["NegativeHysteresis"] = *negativeHysteresis;
1128 }
1129 if (positiveHysteresis)
1130 {
1131 output["PositiveHysteresis"] = *positiveHysteresis;
James Feist83ff9ab2018-08-31 10:18:24 -07001132 }
James Feistc33a90e2019-03-01 10:17:44 -08001133 if (direction)
1134 {
1135 constexpr const std::array<const char*, 2> allowedDirections = {
1136 "Ceiling", "Floor"};
1137 if (std::find(allowedDirections.begin(), allowedDirections.end(),
1138 *direction) == allowedDirections.end())
1139 {
1140 messages::propertyValueTypeError(response->res, "Direction",
1141 *direction);
1142 return CreatePIDRet::fail;
1143 }
1144 output["Class"] = *direction;
1145 }
James Feist83ff9ab2018-08-31 10:18:24 -07001146 }
1147 else
1148 {
Gunnar Millsa0744d32020-11-09 15:40:45 -06001149 BMCWEB_LOG_ERROR << "Illegal Type " << type;
Jason M. Bills35a62c72018-10-09 12:45:45 -07001150 messages::propertyUnknown(response->res, type);
James Feist83ff9ab2018-08-31 10:18:24 -07001151 return CreatePIDRet::fail;
1152 }
1153 return CreatePIDRet::patch;
1154}
James Feist73df0db2019-03-25 15:29:35 -07001155struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
1156{
1157
Ed Tanous23a21a12020-07-25 04:45:05 +00001158 GetPIDValues(const std::shared_ptr<AsyncResp>& asyncRespIn) :
1159 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001160
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001161 {}
James Feist73df0db2019-03-25 15:29:35 -07001162
1163 void run()
1164 {
1165 std::shared_ptr<GetPIDValues> self = shared_from_this();
1166
1167 // get all configurations
1168 crow::connections::systemBus->async_method_call(
1169 [self](const boost::system::error_code ec,
Ed Tanous23a21a12020-07-25 04:45:05 +00001170 const crow::openbmc_mapper::GetSubTreeType& subtreeLocal) {
James Feist73df0db2019-03-25 15:29:35 -07001171 if (ec)
1172 {
1173 BMCWEB_LOG_ERROR << ec;
1174 messages::internalError(self->asyncResp->res);
1175 return;
1176 }
Ed Tanous23a21a12020-07-25 04:45:05 +00001177 self->subtree = subtreeLocal;
James Feist73df0db2019-03-25 15:29:35 -07001178 },
1179 "xyz.openbmc_project.ObjectMapper",
1180 "/xyz/openbmc_project/object_mapper",
1181 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
1182 std::array<const char*, 4>{
1183 pidConfigurationIface, pidZoneConfigurationIface,
1184 objectManagerIface, stepwiseConfigurationIface});
1185
1186 // at the same time get the selected profile
1187 crow::connections::systemBus->async_method_call(
1188 [self](const boost::system::error_code ec,
Ed Tanous23a21a12020-07-25 04:45:05 +00001189 const crow::openbmc_mapper::GetSubTreeType& subtreeLocal) {
1190 if (ec || subtreeLocal.empty())
James Feist73df0db2019-03-25 15:29:35 -07001191 {
1192 return;
1193 }
Ed Tanous23a21a12020-07-25 04:45:05 +00001194 if (subtreeLocal[0].second.size() != 1)
James Feist73df0db2019-03-25 15:29:35 -07001195 {
1196 // invalid mapper response, should never happen
1197 BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
1198 messages::internalError(self->asyncResp->res);
1199 return;
1200 }
1201
Ed Tanous23a21a12020-07-25 04:45:05 +00001202 const std::string& path = subtreeLocal[0].first;
1203 const std::string& owner = subtreeLocal[0].second[0].first;
James Feist73df0db2019-03-25 15:29:35 -07001204 crow::connections::systemBus->async_method_call(
1205 [path, owner, self](
Ed Tanous23a21a12020-07-25 04:45:05 +00001206 const boost::system::error_code ec2,
James Feist73df0db2019-03-25 15:29:35 -07001207 const boost::container::flat_map<
1208 std::string, std::variant<std::vector<std::string>,
1209 std::string>>& resp) {
Ed Tanous23a21a12020-07-25 04:45:05 +00001210 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001211 {
1212 BMCWEB_LOG_ERROR << "GetPIDValues: Can't get "
1213 "thermalModeIface "
1214 << path;
1215 messages::internalError(self->asyncResp->res);
1216 return;
1217 }
Ed Tanous271584a2019-07-09 16:24:22 -07001218 const std::string* current = nullptr;
1219 const std::vector<std::string>* supported = nullptr;
James Feist73df0db2019-03-25 15:29:35 -07001220 for (auto& [key, value] : resp)
1221 {
1222 if (key == "Current")
1223 {
1224 current = std::get_if<std::string>(&value);
1225 if (current == nullptr)
1226 {
1227 BMCWEB_LOG_ERROR
1228 << "GetPIDValues: thermal mode "
1229 "iface invalid "
1230 << path;
1231 messages::internalError(
1232 self->asyncResp->res);
1233 return;
1234 }
1235 }
1236 if (key == "Supported")
1237 {
1238 supported =
1239 std::get_if<std::vector<std::string>>(
1240 &value);
1241 if (supported == nullptr)
1242 {
1243 BMCWEB_LOG_ERROR
1244 << "GetPIDValues: thermal mode "
1245 "iface invalid"
1246 << path;
1247 messages::internalError(
1248 self->asyncResp->res);
1249 return;
1250 }
1251 }
1252 }
1253 if (current == nullptr || supported == nullptr)
1254 {
1255 BMCWEB_LOG_ERROR << "GetPIDValues: thermal mode "
1256 "iface invalid "
1257 << path;
1258 messages::internalError(self->asyncResp->res);
1259 return;
1260 }
1261 self->currentProfile = *current;
1262 self->supportedProfiles = *supported;
1263 },
1264 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
1265 thermalModeIface);
1266 },
1267 "xyz.openbmc_project.ObjectMapper",
1268 "/xyz/openbmc_project/object_mapper",
1269 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
1270 std::array<const char*, 1>{thermalModeIface});
1271 }
1272
1273 ~GetPIDValues()
1274 {
1275 if (asyncResp->res.result() != boost::beast::http::status::ok)
1276 {
1277 return;
1278 }
1279 // create map of <connection, path to objMgr>>
1280 boost::container::flat_map<std::string, std::string> objectMgrPaths;
1281 boost::container::flat_set<std::string> calledConnections;
1282 for (const auto& pathGroup : subtree)
1283 {
1284 for (const auto& connectionGroup : pathGroup.second)
1285 {
1286 auto findConnection =
1287 calledConnections.find(connectionGroup.first);
1288 if (findConnection != calledConnections.end())
1289 {
1290 break;
1291 }
1292 for (const std::string& interface : connectionGroup.second)
1293 {
1294 if (interface == objectManagerIface)
1295 {
1296 objectMgrPaths[connectionGroup.first] = pathGroup.first;
1297 }
1298 // this list is alphabetical, so we
1299 // should have found the objMgr by now
1300 if (interface == pidConfigurationIface ||
1301 interface == pidZoneConfigurationIface ||
1302 interface == stepwiseConfigurationIface)
1303 {
1304 auto findObjMgr =
1305 objectMgrPaths.find(connectionGroup.first);
1306 if (findObjMgr == objectMgrPaths.end())
1307 {
1308 BMCWEB_LOG_DEBUG << connectionGroup.first
1309 << "Has no Object Manager";
1310 continue;
1311 }
1312
1313 calledConnections.insert(connectionGroup.first);
1314
1315 asyncPopulatePid(findObjMgr->first, findObjMgr->second,
1316 currentProfile, supportedProfiles,
1317 asyncResp);
1318 break;
1319 }
1320 }
1321 }
1322 }
1323 }
1324
1325 std::vector<std::string> supportedProfiles;
1326 std::string currentProfile;
1327 crow::openbmc_mapper::GetSubTreeType subtree;
1328 std::shared_ptr<AsyncResp> asyncResp;
1329};
1330
1331struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
1332{
1333
Ed Tanous271584a2019-07-09 16:24:22 -07001334 SetPIDValues(const std::shared_ptr<AsyncResp>& asyncRespIn,
James Feist73df0db2019-03-25 15:29:35 -07001335 nlohmann::json& data) :
Ed Tanous271584a2019-07-09 16:24:22 -07001336 asyncResp(asyncRespIn)
James Feist73df0db2019-03-25 15:29:35 -07001337 {
1338
1339 std::optional<nlohmann::json> pidControllers;
1340 std::optional<nlohmann::json> fanControllers;
1341 std::optional<nlohmann::json> fanZones;
1342 std::optional<nlohmann::json> stepwiseControllers;
1343
1344 if (!redfish::json_util::readJson(
1345 data, asyncResp->res, "PidControllers", pidControllers,
1346 "FanControllers", fanControllers, "FanZones", fanZones,
1347 "StepwiseControllers", stepwiseControllers, "Profile", profile))
1348 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001349 BMCWEB_LOG_ERROR
1350 << "Illegal Property "
1351 << data.dump(2, ' ', true,
1352 nlohmann::json::error_handler_t::replace);
James Feist73df0db2019-03-25 15:29:35 -07001353 return;
1354 }
1355 configuration.emplace_back("PidControllers", std::move(pidControllers));
1356 configuration.emplace_back("FanControllers", std::move(fanControllers));
1357 configuration.emplace_back("FanZones", std::move(fanZones));
1358 configuration.emplace_back("StepwiseControllers",
1359 std::move(stepwiseControllers));
1360 }
1361 void run()
1362 {
1363 if (asyncResp->res.result() != boost::beast::http::status::ok)
1364 {
1365 return;
1366 }
1367
1368 std::shared_ptr<SetPIDValues> self = shared_from_this();
1369
1370 // todo(james): might make sense to do a mapper call here if this
1371 // interface gets more traction
1372 crow::connections::systemBus->async_method_call(
1373 [self](const boost::system::error_code ec,
Ed Tanous271584a2019-07-09 16:24:22 -07001374 dbus::utility::ManagedObjectType& mObj) {
James Feist73df0db2019-03-25 15:29:35 -07001375 if (ec)
1376 {
1377 BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
1378 messages::internalError(self->asyncResp->res);
1379 return;
1380 }
James Feiste69d9de2020-02-07 12:23:27 -08001381 const std::array<const char*, 3> configurations = {
1382 pidConfigurationIface, pidZoneConfigurationIface,
1383 stepwiseConfigurationIface};
1384
James Feist14b0b8d2020-02-12 11:52:07 -08001385 for (const auto& [path, object] : mObj)
James Feiste69d9de2020-02-07 12:23:27 -08001386 {
James Feist14b0b8d2020-02-12 11:52:07 -08001387 for (const auto& [interface, _] : object)
James Feiste69d9de2020-02-07 12:23:27 -08001388 {
1389 if (std::find(configurations.begin(),
1390 configurations.end(),
1391 interface) != configurations.end())
1392 {
James Feist14b0b8d2020-02-12 11:52:07 -08001393 self->objectCount++;
James Feiste69d9de2020-02-07 12:23:27 -08001394 break;
1395 }
1396 }
James Feiste69d9de2020-02-07 12:23:27 -08001397 }
Ed Tanous271584a2019-07-09 16:24:22 -07001398 self->managedObj = std::move(mObj);
James Feist73df0db2019-03-25 15:29:35 -07001399 },
1400 "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
1401 "GetManagedObjects");
1402
1403 // at the same time get the profile information
1404 crow::connections::systemBus->async_method_call(
1405 [self](const boost::system::error_code ec,
1406 const crow::openbmc_mapper::GetSubTreeType& subtree) {
1407 if (ec || subtree.empty())
1408 {
1409 return;
1410 }
1411 if (subtree[0].second.empty())
1412 {
1413 // invalid mapper response, should never happen
1414 BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
1415 messages::internalError(self->asyncResp->res);
1416 return;
1417 }
1418
1419 const std::string& path = subtree[0].first;
1420 const std::string& owner = subtree[0].second[0].first;
1421 crow::connections::systemBus->async_method_call(
1422 [self, path, owner](
Ed Tanouscb13a392020-07-25 19:02:03 +00001423 const boost::system::error_code ec2,
James Feist73df0db2019-03-25 15:29:35 -07001424 const boost::container::flat_map<
1425 std::string, std::variant<std::vector<std::string>,
Ed Tanous271584a2019-07-09 16:24:22 -07001426 std::string>>& r) {
Ed Tanouscb13a392020-07-25 19:02:03 +00001427 if (ec2)
James Feist73df0db2019-03-25 15:29:35 -07001428 {
1429 BMCWEB_LOG_ERROR << "SetPIDValues: Can't get "
1430 "thermalModeIface "
1431 << path;
1432 messages::internalError(self->asyncResp->res);
1433 return;
1434 }
Ed Tanous271584a2019-07-09 16:24:22 -07001435 const std::string* current = nullptr;
1436 const std::vector<std::string>* supported = nullptr;
1437 for (auto& [key, value] : r)
James Feist73df0db2019-03-25 15:29:35 -07001438 {
1439 if (key == "Current")
1440 {
1441 current = std::get_if<std::string>(&value);
1442 if (current == nullptr)
1443 {
1444 BMCWEB_LOG_ERROR
1445 << "SetPIDValues: thermal mode "
1446 "iface invalid "
1447 << path;
1448 messages::internalError(
1449 self->asyncResp->res);
1450 return;
1451 }
1452 }
1453 if (key == "Supported")
1454 {
1455 supported =
1456 std::get_if<std::vector<std::string>>(
1457 &value);
1458 if (supported == nullptr)
1459 {
1460 BMCWEB_LOG_ERROR
1461 << "SetPIDValues: thermal mode "
1462 "iface invalid"
1463 << path;
1464 messages::internalError(
1465 self->asyncResp->res);
1466 return;
1467 }
1468 }
1469 }
1470 if (current == nullptr || supported == nullptr)
1471 {
1472 BMCWEB_LOG_ERROR << "SetPIDValues: thermal mode "
1473 "iface invalid "
1474 << path;
1475 messages::internalError(self->asyncResp->res);
1476 return;
1477 }
1478 self->currentProfile = *current;
1479 self->supportedProfiles = *supported;
1480 self->profileConnection = owner;
1481 self->profilePath = path;
1482 },
1483 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
1484 thermalModeIface);
1485 },
1486 "xyz.openbmc_project.ObjectMapper",
1487 "/xyz/openbmc_project/object_mapper",
1488 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
1489 std::array<const char*, 1>{thermalModeIface});
1490 }
1491 ~SetPIDValues()
1492 {
1493 if (asyncResp->res.result() != boost::beast::http::status::ok)
1494 {
1495 return;
1496 }
1497
1498 std::shared_ptr<AsyncResp> response = asyncResp;
1499
1500 if (profile)
1501 {
1502 if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
1503 *profile) == supportedProfiles.end())
1504 {
1505 messages::actionParameterUnknown(response->res, "Profile",
1506 *profile);
1507 return;
1508 }
1509 currentProfile = *profile;
1510 crow::connections::systemBus->async_method_call(
1511 [response](const boost::system::error_code ec) {
1512 if (ec)
1513 {
1514 BMCWEB_LOG_ERROR << "Error patching profile" << ec;
1515 messages::internalError(response->res);
1516 }
1517 },
1518 profileConnection, profilePath,
1519 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
1520 "Current", std::variant<std::string>(*profile));
1521 }
1522
1523 for (auto& containerPair : configuration)
1524 {
1525 auto& container = containerPair.second;
1526 if (!container)
1527 {
1528 continue;
1529 }
James Feist6ee7f772020-02-06 16:25:27 -08001530 BMCWEB_LOG_DEBUG << *container;
1531
James Feist73df0db2019-03-25 15:29:35 -07001532 std::string& type = containerPair.first;
1533
1534 for (nlohmann::json::iterator it = container->begin();
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301535 it != container->end(); ++it)
James Feist73df0db2019-03-25 15:29:35 -07001536 {
1537 const auto& name = it.key();
James Feist6ee7f772020-02-06 16:25:27 -08001538 BMCWEB_LOG_DEBUG << "looking for " << name;
1539
James Feist73df0db2019-03-25 15:29:35 -07001540 auto pathItr =
1541 std::find_if(managedObj.begin(), managedObj.end(),
1542 [&name](const auto& obj) {
1543 return boost::algorithm::ends_with(
1544 obj.first.str, "/" + name);
1545 });
1546 boost::container::flat_map<std::string,
1547 dbus::utility::DbusVariantType>
1548 output;
1549
1550 output.reserve(16); // The pid interface length
1551
1552 // determines if we're patching entity-manager or
1553 // creating a new object
1554 bool createNewObject = (pathItr == managedObj.end());
James Feist6ee7f772020-02-06 16:25:27 -08001555 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
1556
James Feist73df0db2019-03-25 15:29:35 -07001557 std::string iface;
1558 if (type == "PidControllers" || type == "FanControllers")
1559 {
1560 iface = pidConfigurationIface;
1561 if (!createNewObject &&
1562 pathItr->second.find(pidConfigurationIface) ==
1563 pathItr->second.end())
1564 {
1565 createNewObject = true;
1566 }
1567 }
1568 else if (type == "FanZones")
1569 {
1570 iface = pidZoneConfigurationIface;
1571 if (!createNewObject &&
1572 pathItr->second.find(pidZoneConfigurationIface) ==
1573 pathItr->second.end())
1574 {
1575
1576 createNewObject = true;
1577 }
1578 }
1579 else if (type == "StepwiseControllers")
1580 {
1581 iface = stepwiseConfigurationIface;
1582 if (!createNewObject &&
1583 pathItr->second.find(stepwiseConfigurationIface) ==
1584 pathItr->second.end())
1585 {
1586 createNewObject = true;
1587 }
1588 }
James Feist6ee7f772020-02-06 16:25:27 -08001589
1590 if (createNewObject && it.value() == nullptr)
1591 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -05001592 // can't delete a non-existent object
James Feist6ee7f772020-02-06 16:25:27 -08001593 messages::invalidObject(response->res, name);
1594 continue;
1595 }
1596
1597 std::string path;
1598 if (pathItr != managedObj.end())
1599 {
1600 path = pathItr->first.str;
1601 }
1602
James Feist73df0db2019-03-25 15:29:35 -07001603 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
James Feiste69d9de2020-02-07 12:23:27 -08001604
1605 // arbitrary limit to avoid attacks
1606 constexpr const size_t controllerLimit = 500;
James Feist14b0b8d2020-02-12 11:52:07 -08001607 if (createNewObject && objectCount >= controllerLimit)
James Feiste69d9de2020-02-07 12:23:27 -08001608 {
1609 messages::resourceExhaustion(response->res, type);
1610 continue;
1611 }
1612
James Feist73df0db2019-03-25 15:29:35 -07001613 output["Name"] = boost::replace_all_copy(name, "_", " ");
1614
1615 std::string chassis;
1616 CreatePIDRet ret = createPidInterface(
James Feist6ee7f772020-02-06 16:25:27 -08001617 response, type, it, path, managedObj, createNewObject,
1618 output, chassis, currentProfile);
James Feist73df0db2019-03-25 15:29:35 -07001619 if (ret == CreatePIDRet::fail)
1620 {
1621 return;
1622 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001623 if (ret == CreatePIDRet::del)
James Feist73df0db2019-03-25 15:29:35 -07001624 {
1625 continue;
1626 }
1627
1628 if (!createNewObject)
1629 {
1630 for (const auto& property : output)
1631 {
1632 crow::connections::systemBus->async_method_call(
1633 [response,
1634 propertyName{std::string(property.first)}](
1635 const boost::system::error_code ec) {
1636 if (ec)
1637 {
1638 BMCWEB_LOG_ERROR << "Error patching "
1639 << propertyName << ": "
1640 << ec;
1641 messages::internalError(response->res);
1642 return;
1643 }
1644 messages::success(response->res);
1645 },
James Feist6ee7f772020-02-06 16:25:27 -08001646 "xyz.openbmc_project.EntityManager", path,
James Feist73df0db2019-03-25 15:29:35 -07001647 "org.freedesktop.DBus.Properties", "Set", iface,
1648 property.first, property.second);
1649 }
1650 }
1651 else
1652 {
1653 if (chassis.empty())
1654 {
1655 BMCWEB_LOG_ERROR << "Failed to get chassis from config";
1656 messages::invalidObject(response->res, name);
1657 return;
1658 }
1659
1660 bool foundChassis = false;
1661 for (const auto& obj : managedObj)
1662 {
1663 if (boost::algorithm::ends_with(obj.first.str, chassis))
1664 {
1665 chassis = obj.first.str;
1666 foundChassis = true;
1667 break;
1668 }
1669 }
1670 if (!foundChassis)
1671 {
1672 BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
1673 messages::resourceMissingAtURI(
1674 response->res, "/redfish/v1/Chassis/" + chassis);
1675 return;
1676 }
1677
1678 crow::connections::systemBus->async_method_call(
1679 [response](const boost::system::error_code ec) {
1680 if (ec)
1681 {
1682 BMCWEB_LOG_ERROR << "Error Adding Pid Object "
1683 << ec;
1684 messages::internalError(response->res);
1685 return;
1686 }
1687 messages::success(response->res);
1688 },
1689 "xyz.openbmc_project.EntityManager", chassis,
1690 "xyz.openbmc_project.AddObject", "AddObject", output);
1691 }
1692 }
1693 }
1694 }
1695 std::shared_ptr<AsyncResp> asyncResp;
1696 std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
1697 configuration;
1698 std::optional<std::string> profile;
1699 dbus::utility::ManagedObjectType managedObj;
1700 std::vector<std::string> supportedProfiles;
1701 std::string currentProfile;
1702 std::string profileConnection;
1703 std::string profilePath;
James Feist14b0b8d2020-02-12 11:52:07 -08001704 size_t objectCount = 0;
James Feist73df0db2019-03-25 15:29:35 -07001705};
James Feist83ff9ab2018-08-31 10:18:24 -07001706
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001707/**
1708 * @brief Retrieves BMC manager location data over DBus
1709 *
1710 * @param[in] aResp Shared pointer for completing asynchronous calls
1711 * @param[in] connectionName - service name
1712 * @param[in] path - object path
1713 * @return none
1714 */
1715inline void getLocation(const std::shared_ptr<AsyncResp>& aResp,
1716 const std::string& connectionName,
1717 const std::string& path)
1718{
1719 BMCWEB_LOG_DEBUG << "Get BMC manager Location data.";
1720
1721 crow::connections::systemBus->async_method_call(
1722 [aResp](const boost::system::error_code ec,
1723 const std::variant<std::string>& property) {
1724 if (ec)
1725 {
1726 BMCWEB_LOG_DEBUG << "DBUS response error for "
1727 "Location";
1728 messages::internalError(aResp->res);
1729 return;
1730 }
1731
1732 const std::string* value = std::get_if<std::string>(&property);
1733
1734 if (value == nullptr)
1735 {
1736 // illegal value
1737 messages::internalError(aResp->res);
1738 return;
1739 }
1740
1741 aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
1742 *value;
1743 },
1744 connectionName, path, "org.freedesktop.DBus.Properties", "Get",
1745 "xyz.openbmc_project.Inventory.Decorator."
1746 "LocationCode",
1747 "LocationCode");
1748}
1749
Ed Tanous1abe55e2018-09-05 08:30:59 -07001750class Manager : public Node
1751{
1752 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07001753 Manager(App& app) : Node(app, "/redfish/v1/Managers/bmc/")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001754 {
Ed Tanous52cc1122020-07-18 13:51:21 -07001755
1756 uuid = persistent_data::getConfig().systemUuid;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001757 entityPrivileges = {
1758 {boost::beast::http::verb::get, {{"Login"}}},
1759 {boost::beast::http::verb::head, {{"Login"}}},
1760 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1761 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1762 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1763 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001764 }
1765
Ed Tanous1abe55e2018-09-05 08:30:59 -07001766 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00001767 void doGet(crow::Response& res, const crow::Request&,
1768 const std::vector<std::string>&) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07001769 {
Ed Tanous0f74e642018-11-12 15:17:05 -08001770 res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001771 res.jsonValue["@odata.type"] = "#Manager.v1_11_0.Manager";
Ed Tanous0f74e642018-11-12 15:17:05 -08001772 res.jsonValue["Id"] = "bmc";
1773 res.jsonValue["Name"] = "OpenBmc Manager";
1774 res.jsonValue["Description"] = "Baseboard Management Controller";
1775 res.jsonValue["PowerState"] = "On";
Ed Tanous029573d2019-02-01 10:57:49 -08001776 res.jsonValue["Status"] = {{"State", "Enabled"}, {"Health", "OK"}};
Ed Tanous0f74e642018-11-12 15:17:05 -08001777 res.jsonValue["ManagerType"] = "BMC";
Ed Tanous3602e232019-05-13 11:11:44 -07001778 res.jsonValue["UUID"] = systemd_utils::getUuid();
1779 res.jsonValue["ServiceEntryPointUUID"] = uuid;
Ed Tanous75176582018-12-14 08:14:34 -08001780 res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
Ed Tanous0f74e642018-11-12 15:17:05 -08001781
1782 res.jsonValue["LogServices"] = {
1783 {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}};
1784
1785 res.jsonValue["NetworkProtocol"] = {
1786 {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}};
1787
1788 res.jsonValue["EthernetInterfaces"] = {
1789 {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}};
Przemyslaw Czarnowski107077d2019-07-11 10:16:43 +02001790
1791#ifdef BMCWEB_ENABLE_VM_NBDPROXY
1792 res.jsonValue["VirtualMedia"] = {
1793 {"@odata.id", "/redfish/v1/Managers/bmc/VirtualMedia"}};
1794#endif // BMCWEB_ENABLE_VM_NBDPROXY
1795
Ed Tanous0f74e642018-11-12 15:17:05 -08001796 // default oem data
1797 nlohmann::json& oem = res.jsonValue["Oem"];
1798 nlohmann::json& oemOpenbmc = oem["OpenBmc"];
1799 oem["@odata.type"] = "#OemManager.Oem";
1800 oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
Ed Tanous0f74e642018-11-12 15:17:05 -08001801 oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
1802 oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
Marri Devender Raocfcd5f62019-05-17 08:34:37 -05001803 oemOpenbmc["Certificates"] = {
1804 {"@odata.id", "/redfish/v1/Managers/bmc/Truststore/Certificates"}};
Ed Tanous0f74e642018-11-12 15:17:05 -08001805
Gunnar Mills2a5c4402020-05-19 09:07:24 -05001806 // Manager.Reset (an action) can be many values, OpenBMC only supports
1807 // BMC reboot.
1808 nlohmann::json& managerReset =
Ed Tanous0f74e642018-11-12 15:17:05 -08001809 res.jsonValue["Actions"]["#Manager.Reset"];
Gunnar Mills2a5c4402020-05-19 09:07:24 -05001810 managerReset["target"] =
Jennifer Leeed5befb2018-08-10 11:29:45 -07001811 "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +05301812 managerReset["@Redfish.ActionInfo"] =
1813 "/redfish/v1/Managers/bmc/ResetActionInfo";
Jennifer Leeca537922018-08-10 10:07:30 -07001814
Gunnar Mills3e40fc72020-05-19 19:18:17 -05001815 // ResetToDefaults (Factory Reset) has values like
1816 // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
1817 // on OpenBMC
1818 nlohmann::json& resetToDefaults =
1819 res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
1820 resetToDefaults["target"] =
1821 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
1822 resetToDefaults["ResetType@Redfish.AllowableValues"] = {"ResetAll"};
1823
Andrew Geisslercb92c032018-08-17 07:56:14 -07001824 res.jsonValue["DateTime"] = crow::utility::dateTimeNow();
Santosh Puranik474bfad2019-04-02 16:00:09 +05301825
Kuiying Wangf8c3e6f2019-08-22 13:35:56 +08001826 // Fill in SerialConsole info
Santosh Puranik474bfad2019-04-02 16:00:09 +05301827 res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
Kuiying Wangf8c3e6f2019-08-22 13:35:56 +08001828 res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
Santosh Puranik474bfad2019-04-02 16:00:09 +05301829 res.jsonValue["SerialConsole"]["ConnectTypesSupported"] = {"IPMI",
1830 "SSH"};
Santosh Puranikef47bb12019-04-30 10:28:52 +05301831#ifdef BMCWEB_ENABLE_KVM
Kuiying Wangf8c3e6f2019-08-22 13:35:56 +08001832 // Fill in GraphicalConsole info
Santosh Puranikef47bb12019-04-30 10:28:52 +05301833 res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
Jae Hyun Yoo704fae62019-10-02 13:01:27 -07001834 res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4;
Santosh Puranikef47bb12019-04-30 10:28:52 +05301835 res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] = {"KVMIP"};
1836#endif // BMCWEB_ENABLE_KVM
Santosh Puranik474bfad2019-04-02 16:00:09 +05301837
Gunnar Mills603a6642019-01-21 17:03:51 -06001838 res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
1839 res.jsonValue["Links"]["ManagerForServers"] = {
1840 {{"@odata.id", "/redfish/v1/Systems/system"}}};
Shawn McCarney26f03892019-05-03 13:20:24 -05001841
Jennifer Leeed5befb2018-08-10 11:29:45 -07001842 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
James Feist5b4aa862018-08-16 14:07:01 -07001843
James Feistb49ac872019-05-21 15:12:01 -07001844 auto health = std::make_shared<HealthPopulate>(asyncResp);
1845 health->isManagersHealth = true;
1846 health->populate();
1847
Gunnar Millsf97ddba2020-08-20 15:57:40 -05001848 fw_util::populateFirmwareInformation(asyncResp, fw_util::bmcPurpose,
1849 "FirmwareVersion", true);
James Feist0f6b00b2019-06-10 14:15:53 -07001850
Gunnar Mills4bf2b032020-06-23 22:28:31 -05001851 getLastResetTime(asyncResp);
1852
James Feist73df0db2019-03-25 15:29:35 -07001853 auto pids = std::make_shared<GetPIDValues>(asyncResp);
1854 pids->run();
Jennifer Leec5d03ff2019-03-08 15:42:58 -08001855
1856 getMainChassisId(asyncResp, [](const std::string& chassisId,
Ed Tanousb5a76932020-09-29 16:16:58 -07001857 const std::shared_ptr<AsyncResp>& aRsp) {
Jennifer Leec5d03ff2019-03-08 15:42:58 -08001858 aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
1859 aRsp->res.jsonValue["Links"]["ManagerForChassis"] = {
1860 {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
Jason M. Bills2c0feb02019-07-26 16:35:20 -07001861 aRsp->res.jsonValue["Links"]["ManagerInChassis"] = {
1862 {"@odata.id", "/redfish/v1/Chassis/" + chassisId}};
Jennifer Leec5d03ff2019-03-08 15:42:58 -08001863 });
James Feist0f6b00b2019-06-10 14:15:53 -07001864
1865 static bool started = false;
1866
1867 if (!started)
1868 {
1869 crow::connections::systemBus->async_method_call(
1870 [asyncResp](const boost::system::error_code ec,
1871 const std::variant<double>& resp) {
1872 if (ec)
1873 {
1874 BMCWEB_LOG_ERROR << "Error while getting progress";
1875 messages::internalError(asyncResp->res);
1876 return;
1877 }
1878 const double* val = std::get_if<double>(&resp);
1879 if (val == nullptr)
1880 {
1881 BMCWEB_LOG_ERROR
1882 << "Invalid response while getting progress";
1883 messages::internalError(asyncResp->res);
1884 return;
1885 }
1886 if (*val < 1.0)
1887 {
1888 asyncResp->res.jsonValue["Status"]["State"] =
1889 "Starting";
1890 started = true;
1891 }
1892 },
1893 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1894 "org.freedesktop.DBus.Properties", "Get",
1895 "org.freedesktop.systemd1.Manager", "Progress");
1896 }
Chicago Duanef6ca6e2020-11-27 16:23:05 +08001897
1898 crow::connections::systemBus->async_method_call(
1899 [asyncResp](
1900 const boost::system::error_code ec,
1901 const std::vector<std::pair<
1902 std::string, std::vector<std::pair<
1903 std::string, std::vector<std::string>>>>>&
1904 subtree) {
1905 if (ec)
1906 {
1907 BMCWEB_LOG_DEBUG << "D-Bus response error on GetSubTree "
1908 << ec;
1909 return;
1910 }
1911 if (subtree.size() == 0)
1912 {
1913 BMCWEB_LOG_DEBUG << "Can't find bmc D-Bus object!";
1914 return;
1915 }
1916 // Assume only 1 bmc D-Bus object
1917 // Throw an error if there is more than 1
1918 if (subtree.size() > 1)
1919 {
1920 BMCWEB_LOG_DEBUG << "Found more than 1 bmc D-Bus object!";
1921 messages::internalError(asyncResp->res);
1922 return;
1923 }
1924
1925 if (subtree[0].first.empty() || subtree[0].second.size() != 1)
1926 {
1927 BMCWEB_LOG_DEBUG << "Error getting bmc D-Bus object!";
1928 messages::internalError(asyncResp->res);
1929 return;
1930 }
1931
1932 const std::string& path = subtree[0].first;
1933 const std::string& connectionName = subtree[0].second[0].first;
1934
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001935 for (const auto& interfaceName : subtree[0].second[0].second)
1936 {
1937 if (interfaceName ==
1938 "xyz.openbmc_project.Inventory.Decorator.Asset")
1939 {
1940 crow::connections::systemBus->async_method_call(
1941 [asyncResp](
1942 const boost::system::error_code ec,
1943 const std::vector<std::pair<
1944 std::string, std::variant<std::string>>>&
1945 propertiesList) {
1946 if (ec)
Chicago Duanef6ca6e2020-11-27 16:23:05 +08001947 {
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001948 BMCWEB_LOG_DEBUG << "Can't get bmc asset!";
1949 return;
Chicago Duanef6ca6e2020-11-27 16:23:05 +08001950 }
SunnySrivastava1984071d8fd2020-10-28 02:20:30 -05001951 for (const std::pair<std::string,
1952 std::variant<std::string>>&
1953 property : propertiesList)
1954 {
1955 const std::string& propertyName =
1956 property.first;
1957
1958 if ((propertyName == "PartNumber") ||
1959 (propertyName == "SerialNumber") ||
1960 (propertyName == "Manufacturer") ||
1961 (propertyName == "Model") ||
1962 (propertyName == "SparePartNumber"))
1963 {
1964 const std::string* value =
1965 std::get_if<std::string>(
1966 &property.second);
1967 if (value == nullptr)
1968 {
1969 // illegal property
1970 messages::internalError(
1971 asyncResp->res);
1972 continue;
1973 }
1974 asyncResp->res.jsonValue[propertyName] =
1975 *value;
1976 }
1977 }
1978 },
1979 connectionName, path,
1980 "org.freedesktop.DBus.Properties", "GetAll",
1981 "xyz.openbmc_project.Inventory.Decorator.Asset");
1982 }
1983 else if (interfaceName == "xyz.openbmc_project.Inventory."
1984 "Decorator.LocationCode")
1985 {
1986 getLocation(asyncResp, connectionName, path);
1987 }
1988 }
Chicago Duanef6ca6e2020-11-27 16:23:05 +08001989 },
1990 "xyz.openbmc_project.ObjectMapper",
1991 "/xyz/openbmc_project/object_mapper",
1992 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1993 "/xyz/openbmc_project/inventory", int32_t(0),
1994 std::array<const char*, 1>{
1995 "xyz.openbmc_project.Inventory.Item.Bmc"});
James Feist83ff9ab2018-08-31 10:18:24 -07001996 }
James Feist5b4aa862018-08-16 14:07:01 -07001997
1998 void doPatch(crow::Response& res, const crow::Request& req,
Ed Tanouscb13a392020-07-25 19:02:03 +00001999 const std::vector<std::string>&) override
James Feist5b4aa862018-08-16 14:07:01 -07002000 {
Ed Tanous0627a2c2018-11-29 17:09:23 -08002001 std::optional<nlohmann::json> oem;
Gunnar Mills4bfefa72020-07-30 13:54:29 -05002002 std::optional<nlohmann::json> links;
Santosh Puranikaf5d60582019-03-20 18:16:36 +05302003 std::optional<std::string> datetime;
Santosh Puranik41352c22019-07-03 05:35:49 -05002004 std::shared_ptr<AsyncResp> response = std::make_shared<AsyncResp>(res);
Ed Tanous0627a2c2018-11-29 17:09:23 -08002005
Santosh Puranik41352c22019-07-03 05:35:49 -05002006 if (!json_util::readJson(req, response->res, "Oem", oem, "DateTime",
Gunnar Mills4bfefa72020-07-30 13:54:29 -05002007 datetime, "Links", links))
James Feist83ff9ab2018-08-31 10:18:24 -07002008 {
2009 return;
2010 }
Ed Tanous0627a2c2018-11-29 17:09:23 -08002011
Ed Tanous0627a2c2018-11-29 17:09:23 -08002012 if (oem)
James Feist83ff9ab2018-08-31 10:18:24 -07002013 {
Ed Tanous43b761d2019-02-13 20:10:56 -08002014 std::optional<nlohmann::json> openbmc;
2015 if (!redfish::json_util::readJson(*oem, res, "OpenBmc", openbmc))
James Feist83ff9ab2018-08-31 10:18:24 -07002016 {
Ed Tanous71f52d92021-02-19 08:51:17 -08002017 BMCWEB_LOG_ERROR
2018 << "Illegal Property "
2019 << oem->dump(2, ' ', true,
2020 nlohmann::json::error_handler_t::replace);
Ed Tanous43b761d2019-02-13 20:10:56 -08002021 return;
2022 }
2023 if (openbmc)
2024 {
2025 std::optional<nlohmann::json> fan;
2026 if (!redfish::json_util::readJson(*openbmc, res, "Fan", fan))
James Feist83ff9ab2018-08-31 10:18:24 -07002027 {
Ed Tanous71f52d92021-02-19 08:51:17 -08002028 BMCWEB_LOG_ERROR
2029 << "Illegal Property "
2030 << openbmc->dump(
2031 2, ' ', true,
2032 nlohmann::json::error_handler_t::replace);
James Feist5f2caae2018-12-12 14:08:25 -08002033 return;
2034 }
Ed Tanous43b761d2019-02-13 20:10:56 -08002035 if (fan)
James Feist5f2caae2018-12-12 14:08:25 -08002036 {
James Feist73df0db2019-03-25 15:29:35 -07002037 auto pid = std::make_shared<SetPIDValues>(response, *fan);
2038 pid->run();
James Feist83ff9ab2018-08-31 10:18:24 -07002039 }
James Feist83ff9ab2018-08-31 10:18:24 -07002040 }
2041 }
Gunnar Mills4bfefa72020-07-30 13:54:29 -05002042 if (links)
2043 {
2044 std::optional<nlohmann::json> activeSoftwareImage;
2045 if (!redfish::json_util::readJson(
2046 *links, res, "ActiveSoftwareImage", activeSoftwareImage))
2047 {
2048 return;
2049 }
2050 if (activeSoftwareImage)
2051 {
2052 std::optional<std::string> odataId;
2053 if (!json_util::readJson(*activeSoftwareImage, res, "@odata.id",
2054 odataId))
2055 {
2056 return;
2057 }
2058
2059 if (odataId)
2060 {
Ed Tanousf23b7292020-10-15 09:41:17 -07002061 setActiveFirmwareImage(response, *odataId);
Gunnar Mills4bfefa72020-07-30 13:54:29 -05002062 }
2063 }
2064 }
Santosh Puranikaf5d60582019-03-20 18:16:36 +05302065 if (datetime)
2066 {
2067 setDateTime(response, std::move(*datetime));
2068 }
2069 }
2070
Ed Tanousb5a76932020-09-29 16:16:58 -07002071 void getLastResetTime(const std::shared_ptr<AsyncResp>& aResp)
Gunnar Mills4bf2b032020-06-23 22:28:31 -05002072 {
2073 BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time";
2074
2075 crow::connections::systemBus->async_method_call(
2076 [aResp](const boost::system::error_code ec,
2077 std::variant<uint64_t>& lastResetTime) {
2078 if (ec)
2079 {
2080 BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
2081 return;
2082 }
2083
2084 const uint64_t* lastResetTimePtr =
2085 std::get_if<uint64_t>(&lastResetTime);
2086
2087 if (!lastResetTimePtr)
2088 {
2089 messages::internalError(aResp->res);
2090 return;
2091 }
2092 // LastRebootTime is epoch time, in milliseconds
2093 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
2094 time_t lastResetTimeStamp =
2095 static_cast<time_t>(*lastResetTimePtr / 1000);
2096
2097 // Convert to ISO 8601 standard
2098 aResp->res.jsonValue["LastResetTime"] =
2099 crow::utility::getDateTime(lastResetTimeStamp);
2100 },
2101 "xyz.openbmc_project.State.BMC", "/xyz/openbmc_project/state/bmc0",
2102 "org.freedesktop.DBus.Properties", "Get",
2103 "xyz.openbmc_project.State.BMC", "LastRebootTime");
2104 }
2105
Gunnar Mills4bfefa72020-07-30 13:54:29 -05002106 /**
2107 * @brief Set the running firmware image
2108 *
2109 * @param[i,o] aResp - Async response object
2110 * @param[i] runningFirmwareTarget - Image to make the running image
2111 *
2112 * @return void
2113 */
Ed Tanousb5a76932020-09-29 16:16:58 -07002114 void setActiveFirmwareImage(const std::shared_ptr<AsyncResp>& aResp,
Ed Tanousf23b7292020-10-15 09:41:17 -07002115 const std::string& runningFirmwareTarget)
Gunnar Mills4bfefa72020-07-30 13:54:29 -05002116 {
2117 // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
Ed Tanousf23b7292020-10-15 09:41:17 -07002118 std::string::size_type idPos = runningFirmwareTarget.rfind('/');
Gunnar Mills4bfefa72020-07-30 13:54:29 -05002119 if (idPos == std::string::npos)
2120 {
2121 messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
2122 "@odata.id");
2123 BMCWEB_LOG_DEBUG << "Can't parse firmware ID!";
2124 return;
2125 }
2126 idPos++;
2127 if (idPos >= runningFirmwareTarget.size())
2128 {
2129 messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
2130 "@odata.id");
2131 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
2132 return;
2133 }
2134 std::string firmwareId = runningFirmwareTarget.substr(idPos);
2135
2136 // Make sure the image is valid before setting priority
2137 crow::connections::systemBus->async_method_call(
2138 [aResp, firmwareId,
2139 runningFirmwareTarget](const boost::system::error_code ec,
2140 ManagedObjectType& subtree) {
2141 if (ec)
2142 {
2143 BMCWEB_LOG_DEBUG << "D-Bus response error getting objects.";
2144 messages::internalError(aResp->res);
2145 return;
2146 }
2147
2148 if (subtree.size() == 0)
2149 {
2150 BMCWEB_LOG_DEBUG << "Can't find image!";
2151 messages::internalError(aResp->res);
2152 return;
2153 }
2154
2155 bool foundImage = false;
2156 for (auto& object : subtree)
2157 {
2158 const std::string& path =
2159 static_cast<const std::string&>(object.first);
Ed Tanousf23b7292020-10-15 09:41:17 -07002160 std::size_t idPos2 = path.rfind('/');
Gunnar Mills4bfefa72020-07-30 13:54:29 -05002161
2162 if (idPos2 == std::string::npos)
2163 {
2164 continue;
2165 }
2166
2167 idPos2++;
2168 if (idPos2 >= path.size())
2169 {
2170 continue;
2171 }
2172
2173 if (path.substr(idPos2) == firmwareId)
2174 {
2175 foundImage = true;
2176 break;
2177 }
2178 }
2179
2180 if (!foundImage)
2181 {
2182 messages::propertyValueNotInList(
2183 aResp->res, runningFirmwareTarget, "@odata.id");
2184 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
2185 return;
2186 }
2187
2188 BMCWEB_LOG_DEBUG << "Setting firmware version " + firmwareId +
2189 " to priority 0.";
2190
2191 // Only support Immediate
2192 // An addition could be a Redfish Setting like
2193 // ActiveSoftwareImageApplyTime and support OnReset
2194 crow::connections::systemBus->async_method_call(
2195 [aResp](const boost::system::error_code ec) {
2196 if (ec)
2197 {
2198 BMCWEB_LOG_DEBUG << "D-Bus response error setting.";
2199 messages::internalError(aResp->res);
2200 return;
2201 }
2202 doBMCGracefulRestart(aResp);
2203 },
2204
2205 "xyz.openbmc_project.Software.BMC.Updater",
2206 "/xyz/openbmc_project/software/" + firmwareId,
2207 "org.freedesktop.DBus.Properties", "Set",
2208 "xyz.openbmc_project.Software.RedundancyPriority",
2209 "Priority", std::variant<uint8_t>(static_cast<uint8_t>(0)));
2210 },
2211 "xyz.openbmc_project.Software.BMC.Updater",
2212 "/xyz/openbmc_project/software",
2213 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
2214 }
2215
Santosh Puranikaf5d60582019-03-20 18:16:36 +05302216 void setDateTime(std::shared_ptr<AsyncResp> aResp,
2217 std::string datetime) const
2218 {
2219 BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
2220
2221 std::stringstream stream(datetime);
2222 // Convert from ISO 8601 to boost local_time
2223 // (BMC only has time in UTC)
2224 boost::posix_time::ptime posixTime;
2225 boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
2226 // Facet gets deleted with the stringsteam
2227 auto ifc = std::make_unique<boost::local_time::local_time_input_facet>(
2228 "%Y-%m-%d %H:%M:%S%F %ZP");
2229 stream.imbue(std::locale(stream.getloc(), ifc.release()));
2230
2231 boost::local_time::local_date_time ldt(
2232 boost::local_time::not_a_date_time);
2233
2234 if (stream >> ldt)
2235 {
2236 posixTime = ldt.utc_time();
2237 boost::posix_time::time_duration dur = posixTime - epoch;
2238 uint64_t durMicroSecs =
2239 static_cast<uint64_t>(dur.total_microseconds());
2240 crow::connections::systemBus->async_method_call(
2241 [aResp{std::move(aResp)}, datetime{std::move(datetime)}](
2242 const boost::system::error_code ec) {
2243 if (ec)
2244 {
2245 BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
2246 "DBUS response error "
2247 << ec;
2248 messages::internalError(aResp->res);
2249 return;
2250 }
2251 aResp->res.jsonValue["DateTime"] = datetime;
2252 },
2253 "xyz.openbmc_project.Time.Manager",
2254 "/xyz/openbmc_project/time/bmc",
2255 "org.freedesktop.DBus.Properties", "Set",
2256 "xyz.openbmc_project.Time.EpochTime", "Elapsed",
2257 std::variant<uint64_t>(durMicroSecs));
2258 }
2259 else
2260 {
2261 messages::propertyValueFormatError(aResp->res, datetime,
2262 "DateTime");
2263 return;
2264 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002265 }
2266
Ed Tanous0f74e642018-11-12 15:17:05 -08002267 std::string uuid;
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01002268};
2269
Ed Tanous1abe55e2018-09-05 08:30:59 -07002270class ManagerCollection : public Node
2271{
2272 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002273 ManagerCollection(App& app) : Node(app, "/redfish/v1/Managers/")
Ed Tanous1abe55e2018-09-05 08:30:59 -07002274 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002275 entityPrivileges = {
2276 {boost::beast::http::verb::get, {{"Login"}}},
2277 {boost::beast::http::verb::head, {{"Login"}}},
2278 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2279 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2280 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2281 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2282 }
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01002283
Ed Tanous1abe55e2018-09-05 08:30:59 -07002284 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00002285 void doGet(crow::Response& res, const crow::Request&,
2286 const std::vector<std::string>&) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002287 {
James Feist83ff9ab2018-08-31 10:18:24 -07002288 // Collections don't include the static data added by SubRoute
2289 // because it has a duplicate entry for members
Ed Tanous1abe55e2018-09-05 08:30:59 -07002290 res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
2291 res.jsonValue["@odata.type"] = "#ManagerCollection.ManagerCollection";
Ed Tanous1abe55e2018-09-05 08:30:59 -07002292 res.jsonValue["Name"] = "Manager Collection";
2293 res.jsonValue["Members@odata.count"] = 1;
2294 res.jsonValue["Members"] = {
James Feist5b4aa862018-08-16 14:07:01 -07002295 {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
Ed Tanous1abe55e2018-09-05 08:30:59 -07002296 res.end();
2297 }
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01002298};
Ed Tanous1abe55e2018-09-05 08:30:59 -07002299} // namespace redfish