xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision 40e9b92ec19acffb46f83a6e55b18974da5d708e)
1*40e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
2*40e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3*40e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
49c310685SBorawski.Lukasz #pragma once
59c310685SBorawski.Lukasz 
613451e39SWilly Tu #include "bmcweb_config.h"
713451e39SWilly Tu 
8a51fc2d2SSui Chen #include "app.hpp"
9a51fc2d2SSui Chen #include "dbus_utility.hpp"
10539d8c6bSEd Tanous #include "generated/enums/action_info.hpp"
11539d8c6bSEd Tanous #include "generated/enums/manager.hpp"
12539d8c6bSEd Tanous #include "generated/enums/resource.hpp"
13a51fc2d2SSui Chen #include "query.hpp"
14c5d03ff4SJennifer Lee #include "redfish_util.hpp"
15a51fc2d2SSui Chen #include "registries/privilege_registry.hpp"
16fac6e53bSKrzysztof Grobelny #include "utils/dbus_utils.hpp"
173ccb3adbSEd Tanous #include "utils/json_utils.hpp"
18a51fc2d2SSui Chen #include "utils/sw_utils.hpp"
19a51fc2d2SSui Chen #include "utils/systemd_utils.hpp"
202b82937eSEd Tanous #include "utils/time_utils.hpp"
219c310685SBorawski.Lukasz 
22e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
23ef4c65b7SEd Tanous #include <boost/url/format.hpp>
24fac6e53bSKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
25fac6e53bSKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
261214b7e7SGunnar Mills 
27a170f275SEd Tanous #include <algorithm>
28e99073f5SGeorge Liu #include <array>
294bfefa74SGunnar Mills #include <cstdint>
301214b7e7SGunnar Mills #include <memory>
319970e93fSKonstantin Aladyshev #include <optional>
323544d2a7SEd Tanous #include <ranges>
331214b7e7SGunnar Mills #include <sstream>
349970e93fSKonstantin Aladyshev #include <string>
35e99073f5SGeorge Liu #include <string_view>
36abf2add6SEd Tanous #include <variant>
375b4aa86bSJames Feist 
381abe55efSEd Tanous namespace redfish
391abe55efSEd Tanous {
40ed5befbdSJennifer Lee 
41d27c31e9SJagpal Singh Gill inline std::string getBMCUpdateServiceName()
42d27c31e9SJagpal Singh Gill {
43d27c31e9SJagpal Singh Gill     if constexpr (BMCWEB_REDFISH_UPDATESERVICE_USE_DBUS)
44d27c31e9SJagpal Singh Gill     {
45d27c31e9SJagpal Singh Gill         return "xyz.openbmc_project.Software.Manager";
46d27c31e9SJagpal Singh Gill     }
47d27c31e9SJagpal Singh Gill     return "xyz.openbmc_project.Software.BMC.Updater";
48d27c31e9SJagpal Singh Gill }
49d27c31e9SJagpal Singh Gill 
50d27c31e9SJagpal Singh Gill inline std::string getBMCUpdateServicePath()
51d27c31e9SJagpal Singh Gill {
52d27c31e9SJagpal Singh Gill     if constexpr (BMCWEB_REDFISH_UPDATESERVICE_USE_DBUS)
53d27c31e9SJagpal Singh Gill     {
54d27c31e9SJagpal Singh Gill         return "/xyz/openbmc_project/software/bmc";
55d27c31e9SJagpal Singh Gill     }
56d27c31e9SJagpal Singh Gill     return "/xyz/openbmc_project/software";
57d27c31e9SJagpal Singh Gill }
58d27c31e9SJagpal Singh Gill 
59ed5befbdSJennifer Lee /**
602a5c4407SGunnar Mills  * Function reboots the BMC.
612a5c4407SGunnar Mills  *
622a5c4407SGunnar Mills  * @param[in] asyncResp - Shared pointer for completing asynchronous calls
63ed5befbdSJennifer Lee  */
648d1b46d7Szhanghch05 inline void
658d1b46d7Szhanghch05     doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
66ed5befbdSJennifer Lee {
67ed5befbdSJennifer Lee     const char* processName = "xyz.openbmc_project.State.BMC";
68ed5befbdSJennifer Lee     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
69ed5befbdSJennifer Lee     const char* interfaceName = "xyz.openbmc_project.State.BMC";
70ed5befbdSJennifer Lee     const std::string& propertyValue =
71ed5befbdSJennifer Lee         "xyz.openbmc_project.State.BMC.Transition.Reboot";
72ed5befbdSJennifer Lee     const char* destProperty = "RequestedBMCTransition";
73ed5befbdSJennifer Lee 
74ed5befbdSJennifer Lee     // Create the D-Bus variant for D-Bus call.
759ae226faSGeorge Liu     sdbusplus::asio::setProperty(
769ae226faSGeorge Liu         *crow::connections::systemBus, processName, objectPath, interfaceName,
779ae226faSGeorge Liu         destProperty, propertyValue,
785e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
79ed5befbdSJennifer Lee             // Use "Set" method to set the property value.
80ed5befbdSJennifer Lee             if (ec)
81ed5befbdSJennifer Lee             {
8262598e31SEd Tanous                 BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
83ed5befbdSJennifer Lee                 messages::internalError(asyncResp->res);
84ed5befbdSJennifer Lee                 return;
85ed5befbdSJennifer Lee             }
86ed5befbdSJennifer Lee 
87ed5befbdSJennifer Lee             messages::success(asyncResp->res);
889ae226faSGeorge Liu         });
89ed5befbdSJennifer Lee }
902a5c4407SGunnar Mills 
918d1b46d7Szhanghch05 inline void
928d1b46d7Szhanghch05     doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
93f92af389SJayaprakash Mutyala {
94f92af389SJayaprakash Mutyala     const char* processName = "xyz.openbmc_project.State.BMC";
95f92af389SJayaprakash Mutyala     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
96f92af389SJayaprakash Mutyala     const char* interfaceName = "xyz.openbmc_project.State.BMC";
97f92af389SJayaprakash Mutyala     const std::string& propertyValue =
98f92af389SJayaprakash Mutyala         "xyz.openbmc_project.State.BMC.Transition.HardReboot";
99f92af389SJayaprakash Mutyala     const char* destProperty = "RequestedBMCTransition";
100f92af389SJayaprakash Mutyala 
101f92af389SJayaprakash Mutyala     // Create the D-Bus variant for D-Bus call.
1029ae226faSGeorge Liu     sdbusplus::asio::setProperty(
1039ae226faSGeorge Liu         *crow::connections::systemBus, processName, objectPath, interfaceName,
1049ae226faSGeorge Liu         destProperty, propertyValue,
1055e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
106f92af389SJayaprakash Mutyala             // Use "Set" method to set the property value.
107f92af389SJayaprakash Mutyala             if (ec)
108f92af389SJayaprakash Mutyala             {
10962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
110f92af389SJayaprakash Mutyala                 messages::internalError(asyncResp->res);
111f92af389SJayaprakash Mutyala                 return;
112f92af389SJayaprakash Mutyala             }
113f92af389SJayaprakash Mutyala 
114f92af389SJayaprakash Mutyala             messages::success(asyncResp->res);
1159ae226faSGeorge Liu         });
116f92af389SJayaprakash Mutyala }
117f92af389SJayaprakash Mutyala 
1182a5c4407SGunnar Mills /**
1192a5c4407SGunnar Mills  * ManagerResetAction class supports the POST method for the Reset (reboot)
1202a5c4407SGunnar Mills  * action.
1212a5c4407SGunnar Mills  */
1227e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetAction(App& app)
1232a5c4407SGunnar Mills {
1242a5c4407SGunnar Mills     /**
1252a5c4407SGunnar Mills      * Function handles POST method request.
1262a5c4407SGunnar Mills      * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
127f92af389SJayaprakash Mutyala      * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
1282a5c4407SGunnar Mills      */
1297e860f15SJohn Edward Broadbent 
130253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/Actions/Manager.Reset/")
131ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
1327e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
13345ca1b86SEd Tanous             [&app](const crow::Request& req,
134253f11b8SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
135253f11b8SEd Tanous                    const std::string& managerId) {
1363ba00073SCarson Labrado                 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
13745ca1b86SEd Tanous                 {
13845ca1b86SEd Tanous                     return;
13945ca1b86SEd Tanous                 }
140253f11b8SEd Tanous                 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
141253f11b8SEd Tanous                 {
142bd79bce8SPatrick Williams                     messages::resourceNotFound(asyncResp->res, "Manager",
143bd79bce8SPatrick Williams                                                managerId);
144253f11b8SEd Tanous                     return;
145253f11b8SEd Tanous                 }
146253f11b8SEd Tanous 
14762598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Post Manager Reset.");
1482a5c4407SGunnar Mills 
1492a5c4407SGunnar Mills                 std::string resetType;
1502a5c4407SGunnar Mills 
15115ed6780SWilly Tu                 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
1527e860f15SJohn Edward Broadbent                                                resetType))
1532a5c4407SGunnar Mills                 {
1542a5c4407SGunnar Mills                     return;
1552a5c4407SGunnar Mills                 }
1562a5c4407SGunnar Mills 
157f92af389SJayaprakash Mutyala                 if (resetType == "GracefulRestart")
158f92af389SJayaprakash Mutyala                 {
15962598e31SEd Tanous                     BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
160f92af389SJayaprakash Mutyala                     doBMCGracefulRestart(asyncResp);
161f92af389SJayaprakash Mutyala                     return;
162f92af389SJayaprakash Mutyala                 }
1633174e4dfSEd Tanous                 if (resetType == "ForceRestart")
164f92af389SJayaprakash Mutyala                 {
16562598e31SEd Tanous                     BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
166f92af389SJayaprakash Mutyala                     doBMCForceRestart(asyncResp);
167f92af389SJayaprakash Mutyala                     return;
168f92af389SJayaprakash Mutyala                 }
169bd79bce8SPatrick Williams                 BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}",
170bd79bce8SPatrick Williams                                  resetType);
1712a5c4407SGunnar Mills                 messages::actionParameterNotSupported(asyncResp->res, resetType,
1722a5c4407SGunnar Mills                                                       "ResetType");
1732a5c4407SGunnar Mills 
1742a5c4407SGunnar Mills                 return;
1757e860f15SJohn Edward Broadbent             });
1762a5c4407SGunnar Mills }
177ed5befbdSJennifer Lee 
1783e40fc74SGunnar Mills /**
1793e40fc74SGunnar Mills  * ManagerResetToDefaultsAction class supports POST method for factory reset
1803e40fc74SGunnar Mills  * action.
1813e40fc74SGunnar Mills  */
1827e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetToDefaultsAction(App& app)
1833e40fc74SGunnar Mills {
1843e40fc74SGunnar Mills     /**
1853e40fc74SGunnar Mills      * Function handles ResetToDefaults POST method request.
1863e40fc74SGunnar Mills      *
1873e40fc74SGunnar Mills      * Analyzes POST body message and factory resets BMC by calling
1883e40fc74SGunnar Mills      * BMC code updater factory reset followed by a BMC reboot.
1893e40fc74SGunnar Mills      *
1903e40fc74SGunnar Mills      * BMC code updater factory reset wipes the whole BMC read-write
1913e40fc74SGunnar Mills      * filesystem which includes things like the network settings.
1923e40fc74SGunnar Mills      *
1933e40fc74SGunnar Mills      * OpenBMC only supports ResetToDefaultsType "ResetAll".
1943e40fc74SGunnar Mills      */
1957e860f15SJohn Edward Broadbent 
1967e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
197253f11b8SEd Tanous                  "/redfish/v1/Managers/<str>/Actions/Manager.ResetToDefaults/")
198ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
199bd79bce8SPatrick Williams         .methods(
200bd79bce8SPatrick Williams             boost::beast::http::verb::
201bd79bce8SPatrick Williams                 post)([&app](
202bd79bce8SPatrick Williams                           const crow::Request& req,
203253f11b8SEd Tanous                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
204253f11b8SEd Tanous                           const std::string& managerId) {
2053ba00073SCarson Labrado             if (!redfish::setUpRedfishRoute(app, req, asyncResp))
20645ca1b86SEd Tanous             {
20745ca1b86SEd Tanous                 return;
20845ca1b86SEd Tanous             }
209253f11b8SEd Tanous 
210253f11b8SEd Tanous             if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
211253f11b8SEd Tanous             {
212bd79bce8SPatrick Williams                 messages::resourceNotFound(asyncResp->res, "Manager",
213bd79bce8SPatrick Williams                                            managerId);
214253f11b8SEd Tanous                 return;
215253f11b8SEd Tanous             }
216253f11b8SEd Tanous 
21762598e31SEd Tanous             BMCWEB_LOG_DEBUG("Post ResetToDefaults.");
2183e40fc74SGunnar Mills 
2199970e93fSKonstantin Aladyshev             std::optional<std::string> resetType;
2209970e93fSKonstantin Aladyshev             std::optional<std::string> resetToDefaultsType;
2213e40fc74SGunnar Mills 
222afc474aeSMyung Bae             if (!json_util::readJsonAction( //
223afc474aeSMyung Bae                     req, asyncResp->res, //
224afc474aeSMyung Bae                     "ResetToDefaultsType", resetToDefaultsType, //
225afc474aeSMyung Bae                     "ResetType", resetType //
226afc474aeSMyung Bae                     ))
2273e40fc74SGunnar Mills             {
2289970e93fSKonstantin Aladyshev                 BMCWEB_LOG_DEBUG("Missing property ResetType.");
2293e40fc74SGunnar Mills 
230bd79bce8SPatrick Williams                 messages::actionParameterMissing(
231bd79bce8SPatrick Williams                     asyncResp->res, "ResetToDefaults", "ResetType");
2323e40fc74SGunnar Mills                 return;
2333e40fc74SGunnar Mills             }
2343e40fc74SGunnar Mills 
2359970e93fSKonstantin Aladyshev             if (resetToDefaultsType && !resetType)
2369970e93fSKonstantin Aladyshev             {
2379970e93fSKonstantin Aladyshev                 BMCWEB_LOG_WARNING(
2389970e93fSKonstantin Aladyshev                     "Using deprecated ResetToDefaultsType, should be ResetType."
2399970e93fSKonstantin Aladyshev                     "Support for the ResetToDefaultsType will be dropped in 2Q24");
2409970e93fSKonstantin Aladyshev                 resetType = resetToDefaultsType;
2419970e93fSKonstantin Aladyshev             }
2429970e93fSKonstantin Aladyshev 
2433e40fc74SGunnar Mills             if (resetType != "ResetAll")
2443e40fc74SGunnar Mills             {
2459970e93fSKonstantin Aladyshev                 BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}",
2469970e93fSKonstantin Aladyshev                                  *resetType);
247bd79bce8SPatrick Williams                 messages::actionParameterNotSupported(asyncResp->res,
248bd79bce8SPatrick Williams                                                       *resetType, "ResetType");
2493e40fc74SGunnar Mills                 return;
2503e40fc74SGunnar Mills             }
2513e40fc74SGunnar Mills 
2523e40fc74SGunnar Mills             crow::connections::systemBus->async_method_call(
2535e7e2dc5SEd Tanous                 [asyncResp](const boost::system::error_code& ec) {
2543e40fc74SGunnar Mills                     if (ec)
2553e40fc74SGunnar Mills                     {
25662598e31SEd Tanous                         BMCWEB_LOG_DEBUG("Failed to ResetToDefaults: {}", ec);
2573e40fc74SGunnar Mills                         messages::internalError(asyncResp->res);
2583e40fc74SGunnar Mills                         return;
2593e40fc74SGunnar Mills                     }
2603e40fc74SGunnar Mills                     // Factory Reset doesn't actually happen until a reboot
2613e40fc74SGunnar Mills                     // Can't erase what the BMC is running on
2623e40fc74SGunnar Mills                     doBMCGracefulRestart(asyncResp);
2633e40fc74SGunnar Mills                 },
264d27c31e9SJagpal Singh Gill                 getBMCUpdateServiceName(), getBMCUpdateServicePath(),
2653e40fc74SGunnar Mills                 "xyz.openbmc_project.Common.FactoryReset", "Reset");
2667e860f15SJohn Edward Broadbent         });
2673e40fc74SGunnar Mills }
2683e40fc74SGunnar Mills 
2691cb1a9e6SAppaRao Puli /**
2701cb1a9e6SAppaRao Puli  * ManagerResetActionInfo derived class for delivering Manager
2711cb1a9e6SAppaRao Puli  * ResetType AllowableValues using ResetInfo schema.
2721cb1a9e6SAppaRao Puli  */
2737e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetActionInfo(App& app)
2741cb1a9e6SAppaRao Puli {
2751cb1a9e6SAppaRao Puli     /**
2761cb1a9e6SAppaRao Puli      * Functions triggers appropriate requests on DBus
2771cb1a9e6SAppaRao Puli      */
2787e860f15SJohn Edward Broadbent 
279253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/ResetActionInfo/")
280ed398213SEd Tanous         .privileges(redfish::privileges::getActionInfo)
2817e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
28245ca1b86SEd Tanous             [&app](const crow::Request& req,
283253f11b8SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
284253f11b8SEd Tanous                    const std::string& managerId) {
2853ba00073SCarson Labrado                 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
28645ca1b86SEd Tanous                 {
28745ca1b86SEd Tanous                     return;
28845ca1b86SEd Tanous                 }
2891476687dSEd Tanous 
290253f11b8SEd Tanous                 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
291253f11b8SEd Tanous                 {
292bd79bce8SPatrick Williams                     messages::resourceNotFound(asyncResp->res, "Manager",
293bd79bce8SPatrick Williams                                                managerId);
294253f11b8SEd Tanous                     return;
295253f11b8SEd Tanous                 }
296253f11b8SEd Tanous 
2971476687dSEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
2981476687dSEd Tanous                     "#ActionInfo.v1_1_2.ActionInfo";
299bd79bce8SPatrick Williams                 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
300bd79bce8SPatrick Williams                     "/redfish/v1/Managers/{}/ResetActionInfo",
301253f11b8SEd Tanous                     BMCWEB_REDFISH_MANAGER_URI_NAME);
3021476687dSEd Tanous                 asyncResp->res.jsonValue["Name"] = "Reset Action Info";
3031476687dSEd Tanous                 asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
3041476687dSEd Tanous                 nlohmann::json::object_t parameter;
3051476687dSEd Tanous                 parameter["Name"] = "ResetType";
3061476687dSEd Tanous                 parameter["Required"] = true;
307539d8c6bSEd Tanous                 parameter["DataType"] = action_info::ParameterTypes::String;
3081476687dSEd Tanous 
3091476687dSEd Tanous                 nlohmann::json::array_t allowableValues;
310ad539545SPatrick Williams                 allowableValues.emplace_back("GracefulRestart");
311ad539545SPatrick Williams                 allowableValues.emplace_back("ForceRestart");
3121476687dSEd Tanous                 parameter["AllowableValues"] = std::move(allowableValues);
3131476687dSEd Tanous 
3141476687dSEd Tanous                 nlohmann::json::array_t parameters;
315ad539545SPatrick Williams                 parameters.emplace_back(std::move(parameter));
3161476687dSEd Tanous 
3171476687dSEd Tanous                 asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
3187e860f15SJohn Edward Broadbent             });
3191cb1a9e6SAppaRao Puli }
3201cb1a9e6SAppaRao Puli 
3215b4aa86bSJames Feist static constexpr const char* objectManagerIface =
3225b4aa86bSJames Feist     "org.freedesktop.DBus.ObjectManager";
3235b4aa86bSJames Feist static constexpr const char* pidConfigurationIface =
3245b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid";
3255b4aa86bSJames Feist static constexpr const char* pidZoneConfigurationIface =
3265b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid.Zone";
327b7a08d04SJames Feist static constexpr const char* stepwiseConfigurationIface =
328b7a08d04SJames Feist     "xyz.openbmc_project.Configuration.Stepwise";
32973df0db0SJames Feist static constexpr const char* thermalModeIface =
33073df0db0SJames Feist     "xyz.openbmc_project.Control.ThermalMode";
3319c310685SBorawski.Lukasz 
3328d1b46d7Szhanghch05 inline void
3338d1b46d7Szhanghch05     asyncPopulatePid(const std::string& connection, const std::string& path,
33473df0db0SJames Feist                      const std::string& currentProfile,
33573df0db0SJames Feist                      const std::vector<std::string>& supportedProfiles,
3368d1b46d7Szhanghch05                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
3375b4aa86bSJames Feist {
3385eb468daSGeorge Liu     sdbusplus::message::object_path objPath(path);
3395eb468daSGeorge Liu     dbus::utility::getManagedObjects(
3405eb468daSGeorge Liu         connection, objPath,
34173df0db0SJames Feist         [asyncResp, currentProfile, supportedProfiles](
3425e7e2dc5SEd Tanous             const boost::system::error_code& ec,
3435b4aa86bSJames Feist             const dbus::utility::ManagedObjectType& managedObj) {
3445b4aa86bSJames Feist             if (ec)
3455b4aa86bSJames Feist             {
34662598e31SEd Tanous                 BMCWEB_LOG_ERROR("{}", ec);
347f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
3485b4aa86bSJames Feist                 return;
3495b4aa86bSJames Feist             }
3505b4aa86bSJames Feist             nlohmann::json& configRoot =
3515b4aa86bSJames Feist                 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
3525b4aa86bSJames Feist             nlohmann::json& fans = configRoot["FanControllers"];
353bd79bce8SPatrick Williams             fans["@odata.type"] =
354bd79bce8SPatrick Williams                 "#OpenBMCManager.v1_0_0.Manager.FanControllers";
355253f11b8SEd Tanous             fans["@odata.id"] = boost::urls::format(
356253f11b8SEd Tanous                 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/FanControllers",
357253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME);
3585b4aa86bSJames Feist 
3595b4aa86bSJames Feist             nlohmann::json& pids = configRoot["PidControllers"];
360bd79bce8SPatrick Williams             pids["@odata.type"] =
361bd79bce8SPatrick Williams                 "#OpenBMCManager.v1_0_0.Manager.PidControllers";
362253f11b8SEd Tanous             pids["@odata.id"] = boost::urls::format(
363253f11b8SEd Tanous                 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/PidControllers",
364253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME);
3655b4aa86bSJames Feist 
366b7a08d04SJames Feist             nlohmann::json& stepwise = configRoot["StepwiseControllers"];
367fc1cdd14SEd Tanous             stepwise["@odata.type"] =
368fc1cdd14SEd Tanous                 "#OpenBMCManager.v1_0_0.Manager.StepwiseControllers";
369253f11b8SEd Tanous             stepwise["@odata.id"] = boost::urls::format(
370253f11b8SEd Tanous                 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/StepwiseControllers",
371253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME);
372b7a08d04SJames Feist 
3735b4aa86bSJames Feist             nlohmann::json& zones = configRoot["FanZones"];
374253f11b8SEd Tanous             zones["@odata.id"] = boost::urls::format(
375253f11b8SEd Tanous                 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/FanZones",
376253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME);
377fc1cdd14SEd Tanous             zones["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager.FanZones";
378253f11b8SEd Tanous             configRoot["@odata.id"] =
379253f11b8SEd Tanous                 boost::urls::format("/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan",
380253f11b8SEd Tanous                                     BMCWEB_REDFISH_MANAGER_URI_NAME);
381fc1cdd14SEd Tanous             configRoot["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager.Fan";
38273df0db0SJames Feist             configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
38373df0db0SJames Feist 
38473df0db0SJames Feist             if (!currentProfile.empty())
38573df0db0SJames Feist             {
38673df0db0SJames Feist                 configRoot["Profile"] = currentProfile;
38773df0db0SJames Feist             }
388bf2ddedeSCarson Labrado             BMCWEB_LOG_DEBUG("profile = {} !", currentProfile);
3895b4aa86bSJames Feist 
3905b4aa86bSJames Feist             for (const auto& pathPair : managedObj)
3915b4aa86bSJames Feist             {
3925b4aa86bSJames Feist                 for (const auto& intfPair : pathPair.second)
3935b4aa86bSJames Feist                 {
3945b4aa86bSJames Feist                     if (intfPair.first != pidConfigurationIface &&
395b7a08d04SJames Feist                         intfPair.first != pidZoneConfigurationIface &&
396b7a08d04SJames Feist                         intfPair.first != stepwiseConfigurationIface)
3975b4aa86bSJames Feist                     {
3985b4aa86bSJames Feist                         continue;
3995b4aa86bSJames Feist                     }
40073df0db0SJames Feist 
401711ac7a9SEd Tanous                     std::string name;
402711ac7a9SEd Tanous 
403711ac7a9SEd Tanous                     for (const std::pair<std::string,
404bd79bce8SPatrick Williams                                          dbus::utility::DbusVariantType>&
405bd79bce8SPatrick Williams                              propPair : intfPair.second)
406711ac7a9SEd Tanous                     {
407711ac7a9SEd Tanous                         if (propPair.first == "Name")
408711ac7a9SEd Tanous                         {
4095b4aa86bSJames Feist                             const std::string* namePtr =
410711ac7a9SEd Tanous                                 std::get_if<std::string>(&propPair.second);
4115b4aa86bSJames Feist                             if (namePtr == nullptr)
4125b4aa86bSJames Feist                             {
41362598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Pid Name Field illegal");
414b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
4155b4aa86bSJames Feist                                 return;
4165b4aa86bSJames Feist                             }
417db697703SWilly Tu                             name = *namePtr;
4185b4aa86bSJames Feist                             dbus::utility::escapePathForDbus(name);
419711ac7a9SEd Tanous                         }
420711ac7a9SEd Tanous                         else if (propPair.first == "Profiles")
42173df0db0SJames Feist                         {
42273df0db0SJames Feist                             const std::vector<std::string>* profiles =
42373df0db0SJames Feist                                 std::get_if<std::vector<std::string>>(
424711ac7a9SEd Tanous                                     &propPair.second);
42573df0db0SJames Feist                             if (profiles == nullptr)
42673df0db0SJames Feist                             {
42762598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Pid Profiles Field illegal");
42873df0db0SJames Feist                                 messages::internalError(asyncResp->res);
42973df0db0SJames Feist                                 return;
43073df0db0SJames Feist                             }
43173df0db0SJames Feist                             if (std::find(profiles->begin(), profiles->end(),
43273df0db0SJames Feist                                           currentProfile) == profiles->end())
43373df0db0SJames Feist                             {
43462598e31SEd Tanous                                 BMCWEB_LOG_INFO(
435bd79bce8SPatrick Williams                                     "{} not supported in current profile",
436bd79bce8SPatrick Williams                                     name);
43773df0db0SJames Feist                                 continue;
43873df0db0SJames Feist                             }
43973df0db0SJames Feist                         }
440711ac7a9SEd Tanous                     }
441b7a08d04SJames Feist                     nlohmann::json* config = nullptr;
442c33a90ecSJames Feist                     const std::string* classPtr = nullptr;
443711ac7a9SEd Tanous 
444711ac7a9SEd Tanous                     for (const std::pair<std::string,
445bd79bce8SPatrick Williams                                          dbus::utility::DbusVariantType>&
446bd79bce8SPatrick Williams                              propPair : intfPair.second)
447c33a90ecSJames Feist                     {
448727dc83fSLei YU                         if (propPair.first == "Class")
449711ac7a9SEd Tanous                         {
450bd79bce8SPatrick Williams                             classPtr =
451bd79bce8SPatrick Williams                                 std::get_if<std::string>(&propPair.second);
452711ac7a9SEd Tanous                         }
453c33a90ecSJames Feist                     }
454c33a90ecSJames Feist 
455253f11b8SEd Tanous                     boost::urls::url url(
456253f11b8SEd Tanous                         boost::urls::format("/redfish/v1/Managers/{}",
457253f11b8SEd Tanous                                             BMCWEB_REDFISH_MANAGER_URI_NAME));
4585b4aa86bSJames Feist                     if (intfPair.first == pidZoneConfigurationIface)
4595b4aa86bSJames Feist                     {
4605b4aa86bSJames Feist                         std::string chassis;
461bd79bce8SPatrick Williams                         if (!dbus::utility::getNthStringFromPath(
462bd79bce8SPatrick Williams                                 pathPair.first.str, 5, chassis))
4635b4aa86bSJames Feist                         {
4645b4aa86bSJames Feist                             chassis = "#IllegalValue";
4655b4aa86bSJames Feist                         }
4665b4aa86bSJames Feist                         nlohmann::json& zone = zones[name];
467bd79bce8SPatrick Williams                         zone["Chassis"]["@odata.id"] = boost::urls::format(
468bd79bce8SPatrick Williams                             "/redfish/v1/Chassis/{}", chassis);
469eddfc437SWilly Tu                         url.set_fragment(
470eddfc437SWilly Tu                             ("/Oem/OpenBmc/Fan/FanZones"_json_pointer / name)
471eddfc437SWilly Tu                                 .to_string());
472eddfc437SWilly Tu                         zone["@odata.id"] = std::move(url);
473fc1cdd14SEd Tanous                         zone["@odata.type"] =
474fc1cdd14SEd Tanous                             "#OpenBMCManager.v1_0_0.Manager.FanZone";
475b7a08d04SJames Feist                         config = &zone;
4765b4aa86bSJames Feist                     }
4775b4aa86bSJames Feist 
478b7a08d04SJames Feist                     else if (intfPair.first == stepwiseConfigurationIface)
4795b4aa86bSJames Feist                     {
480c33a90ecSJames Feist                         if (classPtr == nullptr)
481c33a90ecSJames Feist                         {
48262598e31SEd Tanous                             BMCWEB_LOG_ERROR("Pid Class Field illegal");
483c33a90ecSJames Feist                             messages::internalError(asyncResp->res);
484c33a90ecSJames Feist                             return;
485c33a90ecSJames Feist                         }
486c33a90ecSJames Feist 
487b7a08d04SJames Feist                         nlohmann::json& controller = stepwise[name];
488b7a08d04SJames Feist                         config = &controller;
489eddfc437SWilly Tu                         url.set_fragment(
490eddfc437SWilly Tu                             ("/Oem/OpenBmc/Fan/StepwiseControllers"_json_pointer /
491eddfc437SWilly Tu                              name)
492eddfc437SWilly Tu                                 .to_string());
493eddfc437SWilly Tu                         controller["@odata.id"] = std::move(url);
494b7a08d04SJames Feist                         controller["@odata.type"] =
495fc1cdd14SEd Tanous                             "#OpenBMCManager.v1_0_0.Manager.StepwiseController";
496b7a08d04SJames Feist 
497c33a90ecSJames Feist                         controller["Direction"] = *classPtr;
4985b4aa86bSJames Feist                     }
4995b4aa86bSJames Feist 
5005b4aa86bSJames Feist                     // pid and fans are off the same configuration
501b7a08d04SJames Feist                     else if (intfPair.first == pidConfigurationIface)
5025b4aa86bSJames Feist                     {
5035b4aa86bSJames Feist                         if (classPtr == nullptr)
5045b4aa86bSJames Feist                         {
50562598e31SEd Tanous                             BMCWEB_LOG_ERROR("Pid Class Field illegal");
506a08b46ccSJason M. Bills                             messages::internalError(asyncResp->res);
5075b4aa86bSJames Feist                             return;
5085b4aa86bSJames Feist                         }
5095b4aa86bSJames Feist                         bool isFan = *classPtr == "fan";
510bd79bce8SPatrick Williams                         nlohmann::json& element =
511bd79bce8SPatrick Williams                             isFan ? fans[name] : pids[name];
512b7a08d04SJames Feist                         config = &element;
5135b4aa86bSJames Feist                         if (isFan)
5145b4aa86bSJames Feist                         {
515eddfc437SWilly Tu                             url.set_fragment(
516eddfc437SWilly Tu                                 ("/Oem/OpenBmc/Fan/FanControllers"_json_pointer /
517eddfc437SWilly Tu                                  name)
518eddfc437SWilly Tu                                     .to_string());
519eddfc437SWilly Tu                             element["@odata.id"] = std::move(url);
520fc1cdd14SEd Tanous                             element["@odata.type"] =
521fc1cdd14SEd Tanous                                 "#OpenBMCManager.v1_0_0.Manager.FanController";
5225b4aa86bSJames Feist                         }
5235b4aa86bSJames Feist                         else
5245b4aa86bSJames Feist                         {
525eddfc437SWilly Tu                             url.set_fragment(
526eddfc437SWilly Tu                                 ("/Oem/OpenBmc/Fan/PidControllers"_json_pointer /
527eddfc437SWilly Tu                                  name)
528eddfc437SWilly Tu                                     .to_string());
529eddfc437SWilly Tu                             element["@odata.id"] = std::move(url);
530fc1cdd14SEd Tanous                             element["@odata.type"] =
531fc1cdd14SEd Tanous                                 "#OpenBMCManager.v1_0_0.Manager.PidController";
5325b4aa86bSJames Feist                         }
533b7a08d04SJames Feist                     }
534b7a08d04SJames Feist                     else
535b7a08d04SJames Feist                     {
53662598e31SEd Tanous                         BMCWEB_LOG_ERROR("Unexpected configuration");
537b7a08d04SJames Feist                         messages::internalError(asyncResp->res);
538b7a08d04SJames Feist                         return;
539b7a08d04SJames Feist                     }
540b7a08d04SJames Feist 
541b7a08d04SJames Feist                     // used for making maps out of 2 vectors
542b7a08d04SJames Feist                     const std::vector<double>* keys = nullptr;
543b7a08d04SJames Feist                     const std::vector<double>* values = nullptr;
544b7a08d04SJames Feist 
545b7a08d04SJames Feist                     for (const auto& propertyPair : intfPair.second)
546b7a08d04SJames Feist                     {
547b7a08d04SJames Feist                         if (propertyPair.first == "Type" ||
548b7a08d04SJames Feist                             propertyPair.first == "Class" ||
549b7a08d04SJames Feist                             propertyPair.first == "Name")
550b7a08d04SJames Feist                         {
551b7a08d04SJames Feist                             continue;
552b7a08d04SJames Feist                         }
553b7a08d04SJames Feist 
554b7a08d04SJames Feist                         // zones
555b7a08d04SJames Feist                         if (intfPair.first == pidZoneConfigurationIface)
556b7a08d04SJames Feist                         {
557b7a08d04SJames Feist                             const double* ptr =
558abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
559b7a08d04SJames Feist                             if (ptr == nullptr)
560b7a08d04SJames Feist                             {
56162598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Field Illegal {}",
56262598e31SEd Tanous                                                  propertyPair.first);
563b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
564b7a08d04SJames Feist                                 return;
565b7a08d04SJames Feist                             }
566b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
567b7a08d04SJames Feist                         }
568b7a08d04SJames Feist 
569b7a08d04SJames Feist                         if (intfPair.first == stepwiseConfigurationIface)
570b7a08d04SJames Feist                         {
571b7a08d04SJames Feist                             if (propertyPair.first == "Reading" ||
572b7a08d04SJames Feist                                 propertyPair.first == "Output")
573b7a08d04SJames Feist                             {
574b7a08d04SJames Feist                                 const std::vector<double>* ptr =
575abf2add6SEd Tanous                                     std::get_if<std::vector<double>>(
576b7a08d04SJames Feist                                         &propertyPair.second);
577b7a08d04SJames Feist 
578b7a08d04SJames Feist                                 if (ptr == nullptr)
579b7a08d04SJames Feist                                 {
58062598e31SEd Tanous                                     BMCWEB_LOG_ERROR("Field Illegal {}",
58162598e31SEd Tanous                                                      propertyPair.first);
582b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
583b7a08d04SJames Feist                                     return;
584b7a08d04SJames Feist                                 }
585b7a08d04SJames Feist 
586b7a08d04SJames Feist                                 if (propertyPair.first == "Reading")
587b7a08d04SJames Feist                                 {
588b7a08d04SJames Feist                                     keys = ptr;
589b7a08d04SJames Feist                                 }
590b7a08d04SJames Feist                                 else
591b7a08d04SJames Feist                                 {
592b7a08d04SJames Feist                                     values = ptr;
593b7a08d04SJames Feist                                 }
594e662eae8SEd Tanous                                 if (keys != nullptr && values != nullptr)
595b7a08d04SJames Feist                                 {
596b7a08d04SJames Feist                                     if (keys->size() != values->size())
597b7a08d04SJames Feist                                     {
59862598e31SEd Tanous                                         BMCWEB_LOG_ERROR(
59962598e31SEd Tanous                                             "Reading and Output size don't match ");
600b7a08d04SJames Feist                                         messages::internalError(asyncResp->res);
601b7a08d04SJames Feist                                         return;
602b7a08d04SJames Feist                                     }
603b7a08d04SJames Feist                                     nlohmann::json& steps = (*config)["Steps"];
604b7a08d04SJames Feist                                     steps = nlohmann::json::array();
605b7a08d04SJames Feist                                     for (size_t ii = 0; ii < keys->size(); ii++)
606b7a08d04SJames Feist                                     {
6071476687dSEd Tanous                                         nlohmann::json::object_t step;
6081476687dSEd Tanous                                         step["Target"] = (*keys)[ii];
6091476687dSEd Tanous                                         step["Output"] = (*values)[ii];
610b2ba3072SPatrick Williams                                         steps.emplace_back(std::move(step));
611b7a08d04SJames Feist                                     }
612b7a08d04SJames Feist                                 }
613b7a08d04SJames Feist                             }
614b7a08d04SJames Feist                             if (propertyPair.first == "NegativeHysteresis" ||
615b7a08d04SJames Feist                                 propertyPair.first == "PositiveHysteresis")
616b7a08d04SJames Feist                             {
617b7a08d04SJames Feist                                 const double* ptr =
618abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
619b7a08d04SJames Feist                                 if (ptr == nullptr)
620b7a08d04SJames Feist                                 {
62162598e31SEd Tanous                                     BMCWEB_LOG_ERROR("Field Illegal {}",
62262598e31SEd Tanous                                                      propertyPair.first);
623b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
624b7a08d04SJames Feist                                     return;
625b7a08d04SJames Feist                                 }
626b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
627b7a08d04SJames Feist                             }
628b7a08d04SJames Feist                         }
629b7a08d04SJames Feist 
630b7a08d04SJames Feist                         // pid and fans are off the same configuration
631b7a08d04SJames Feist                         if (intfPair.first == pidConfigurationIface ||
632b7a08d04SJames Feist                             intfPair.first == stepwiseConfigurationIface)
633b7a08d04SJames Feist                         {
6345b4aa86bSJames Feist                             if (propertyPair.first == "Zones")
6355b4aa86bSJames Feist                             {
6365b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
637abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
6381b6b96c5SEd Tanous                                         &propertyPair.second);
6395b4aa86bSJames Feist 
6405b4aa86bSJames Feist                                 if (inputs == nullptr)
6415b4aa86bSJames Feist                                 {
64262598e31SEd Tanous                                     BMCWEB_LOG_ERROR("Zones Pid Field Illegal");
643a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
6445b4aa86bSJames Feist                                     return;
6455b4aa86bSJames Feist                                 }
646b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
6475b4aa86bSJames Feist                                 data = nlohmann::json::array();
6485b4aa86bSJames Feist                                 for (std::string itemCopy : *inputs)
6495b4aa86bSJames Feist                                 {
6505b4aa86bSJames Feist                                     dbus::utility::escapePathForDbus(itemCopy);
6511476687dSEd Tanous                                     nlohmann::json::object_t input;
652bd79bce8SPatrick Williams                                     boost::urls::url managerUrl =
653bd79bce8SPatrick Williams                                         boost::urls::format(
654253f11b8SEd Tanous                                             "/redfish/v1/Managers/{}#{}",
655253f11b8SEd Tanous                                             BMCWEB_REDFISH_MANAGER_URI_NAME,
656eddfc437SWilly Tu                                             ("/Oem/OpenBmc/Fan/FanZones"_json_pointer /
657eddfc437SWilly Tu                                              itemCopy)
658eddfc437SWilly Tu                                                 .to_string());
659eddfc437SWilly Tu                                     input["@odata.id"] = std::move(managerUrl);
660b2ba3072SPatrick Williams                                     data.emplace_back(std::move(input));
6615b4aa86bSJames Feist                                 }
6625b4aa86bSJames Feist                             }
6635b4aa86bSJames Feist                             // todo(james): may never happen, but this
6645b4aa86bSJames Feist                             // assumes configuration data referenced in the
6655b4aa86bSJames Feist                             // PID config is provided by the same daemon, we
6665b4aa86bSJames Feist                             // could add another loop to cover all cases,
6675b4aa86bSJames Feist                             // but I'm okay kicking this can down the road a
6685b4aa86bSJames Feist                             // bit
6695b4aa86bSJames Feist 
6705b4aa86bSJames Feist                             else if (propertyPair.first == "Inputs" ||
6715b4aa86bSJames Feist                                      propertyPair.first == "Outputs")
6725b4aa86bSJames Feist                             {
673b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
6745b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
675abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
6761b6b96c5SEd Tanous                                         &propertyPair.second);
6775b4aa86bSJames Feist 
6785b4aa86bSJames Feist                                 if (inputs == nullptr)
6795b4aa86bSJames Feist                                 {
68062598e31SEd Tanous                                     BMCWEB_LOG_ERROR("Field Illegal {}",
68162598e31SEd Tanous                                                      propertyPair.first);
682f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
6835b4aa86bSJames Feist                                     return;
6845b4aa86bSJames Feist                                 }
6855b4aa86bSJames Feist                                 data = *inputs;
686b943aaefSJames Feist                             }
687b943aaefSJames Feist                             else if (propertyPair.first == "SetPointOffset")
688b943aaefSJames Feist                             {
689b943aaefSJames Feist                                 const std::string* ptr =
690bd79bce8SPatrick Williams                                     std::get_if<std::string>(
691bd79bce8SPatrick Williams                                         &propertyPair.second);
692b943aaefSJames Feist 
693b943aaefSJames Feist                                 if (ptr == nullptr)
694b943aaefSJames Feist                                 {
69562598e31SEd Tanous                                     BMCWEB_LOG_ERROR("Field Illegal {}",
69662598e31SEd Tanous                                                      propertyPair.first);
697b943aaefSJames Feist                                     messages::internalError(asyncResp->res);
698b943aaefSJames Feist                                     return;
699b943aaefSJames Feist                                 }
700b943aaefSJames Feist                                 // translate from dbus to redfish
701b943aaefSJames Feist                                 if (*ptr == "WarningHigh")
702b943aaefSJames Feist                                 {
703b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
704b943aaefSJames Feist                                         "UpperThresholdNonCritical";
705b943aaefSJames Feist                                 }
706b943aaefSJames Feist                                 else if (*ptr == "WarningLow")
707b943aaefSJames Feist                                 {
708b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
709b943aaefSJames Feist                                         "LowerThresholdNonCritical";
710b943aaefSJames Feist                                 }
711b943aaefSJames Feist                                 else if (*ptr == "CriticalHigh")
712b943aaefSJames Feist                                 {
713b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
714b943aaefSJames Feist                                         "UpperThresholdCritical";
715b943aaefSJames Feist                                 }
716b943aaefSJames Feist                                 else if (*ptr == "CriticalLow")
717b943aaefSJames Feist                                 {
718b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
719b943aaefSJames Feist                                         "LowerThresholdCritical";
720b943aaefSJames Feist                                 }
721b943aaefSJames Feist                                 else
722b943aaefSJames Feist                                 {
72362598e31SEd Tanous                                     BMCWEB_LOG_ERROR("Value Illegal {}", *ptr);
724b943aaefSJames Feist                                     messages::internalError(asyncResp->res);
725b943aaefSJames Feist                                     return;
726b943aaefSJames Feist                                 }
727b943aaefSJames Feist                             }
728b943aaefSJames Feist                             // doubles
729bd79bce8SPatrick Williams                             else if (propertyPair.first ==
730bd79bce8SPatrick Williams                                          "FFGainCoefficient" ||
7315b4aa86bSJames Feist                                      propertyPair.first == "FFOffCoefficient" ||
7325b4aa86bSJames Feist                                      propertyPair.first == "ICoefficient" ||
7335b4aa86bSJames Feist                                      propertyPair.first == "ILimitMax" ||
7345b4aa86bSJames Feist                                      propertyPair.first == "ILimitMin" ||
735bd79bce8SPatrick Williams                                      propertyPair.first ==
736bd79bce8SPatrick Williams                                          "PositiveHysteresis" ||
737bd79bce8SPatrick Williams                                      propertyPair.first ==
738bd79bce8SPatrick Williams                                          "NegativeHysteresis" ||
7395b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMax" ||
7405b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMin" ||
7415b4aa86bSJames Feist                                      propertyPair.first == "PCoefficient" ||
7427625cb81SJames Feist                                      propertyPair.first == "SetPoint" ||
7435b4aa86bSJames Feist                                      propertyPair.first == "SlewNeg" ||
7445b4aa86bSJames Feist                                      propertyPair.first == "SlewPos")
7455b4aa86bSJames Feist                             {
7465b4aa86bSJames Feist                                 const double* ptr =
747abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
7485b4aa86bSJames Feist                                 if (ptr == nullptr)
7495b4aa86bSJames Feist                                 {
75062598e31SEd Tanous                                     BMCWEB_LOG_ERROR("Field Illegal {}",
75162598e31SEd Tanous                                                      propertyPair.first);
752f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
7535b4aa86bSJames Feist                                     return;
7545b4aa86bSJames Feist                                 }
755b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
7565b4aa86bSJames Feist                             }
7575b4aa86bSJames Feist                         }
7585b4aa86bSJames Feist                     }
7595b4aa86bSJames Feist                 }
7605b4aa86bSJames Feist             }
7615eb468daSGeorge Liu         });
7625b4aa86bSJames Feist }
763ca537928SJennifer Lee 
76483ff9ab6SJames Feist enum class CreatePIDRet
76583ff9ab6SJames Feist {
76683ff9ab6SJames Feist     fail,
76783ff9ab6SJames Feist     del,
76883ff9ab6SJames Feist     patch
76983ff9ab6SJames Feist };
77083ff9ab6SJames Feist 
7718d1b46d7Szhanghch05 inline bool
7728d1b46d7Szhanghch05     getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
7739e9b6049SEd Tanous                         std::vector<nlohmann::json::object_t>& config,
7745f2caaefSJames Feist                         std::vector<std::string>& zones)
7755f2caaefSJames Feist {
776b6baeaa4SJames Feist     if (config.empty())
777b6baeaa4SJames Feist     {
77862598e31SEd Tanous         BMCWEB_LOG_ERROR("Empty Zones");
779f818b04dSEd Tanous         messages::propertyValueFormatError(response->res, config, "Zones");
780b6baeaa4SJames Feist         return false;
781b6baeaa4SJames Feist     }
7825f2caaefSJames Feist     for (auto& odata : config)
7835f2caaefSJames Feist     {
7845f2caaefSJames Feist         std::string path;
7859e9b6049SEd Tanous         if (!redfish::json_util::readJsonObject(odata, response->res,
7869e9b6049SEd Tanous                                                 "@odata.id", path))
7875f2caaefSJames Feist         {
7885f2caaefSJames Feist             return false;
7895f2caaefSJames Feist         }
7905f2caaefSJames Feist         std::string input;
79161adbda3SJames Feist 
79261adbda3SJames Feist         // 8 below comes from
79361adbda3SJames Feist         // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
79461adbda3SJames Feist         //     0    1     2      3    4    5      6     7      8
79561adbda3SJames Feist         if (!dbus::utility::getNthStringFromPath(path, 8, input))
7965f2caaefSJames Feist         {
79762598e31SEd Tanous             BMCWEB_LOG_ERROR("Got invalid path {}", path);
79862598e31SEd Tanous             BMCWEB_LOG_ERROR("Illegal Type Zones");
799f818b04dSEd Tanous             messages::propertyValueFormatError(response->res, odata, "Zones");
8005f2caaefSJames Feist             return false;
8015f2caaefSJames Feist         }
802a170f275SEd Tanous         std::replace(input.begin(), input.end(), '_', ' ');
8035f2caaefSJames Feist         zones.emplace_back(std::move(input));
8045f2caaefSJames Feist     }
8055f2caaefSJames Feist     return true;
8065f2caaefSJames Feist }
8075f2caaefSJames Feist 
808711ac7a9SEd Tanous inline const dbus::utility::ManagedObjectType::value_type*
80973df0db0SJames Feist     findChassis(const dbus::utility::ManagedObjectType& managedObj,
8109e9b6049SEd Tanous                 std::string_view value, std::string& chassis)
811b6baeaa4SJames Feist {
81262598e31SEd Tanous     BMCWEB_LOG_DEBUG("Find Chassis: {}", value);
813b6baeaa4SJames Feist 
8149e9b6049SEd Tanous     std::string escaped(value);
8156ce82fabSYaswanth Reddy M     std::replace(escaped.begin(), escaped.end(), ' ', '_');
816b6baeaa4SJames Feist     escaped = "/" + escaped;
8173544d2a7SEd Tanous     auto it = std::ranges::find_if(managedObj, [&escaped](const auto& obj) {
81818f8f608SEd Tanous         if (obj.first.str.ends_with(escaped))
819b6baeaa4SJames Feist         {
82062598e31SEd Tanous             BMCWEB_LOG_DEBUG("Matched {}", obj.first.str);
821b6baeaa4SJames Feist             return true;
822b6baeaa4SJames Feist         }
823b6baeaa4SJames Feist         return false;
824b6baeaa4SJames Feist     });
825b6baeaa4SJames Feist 
826b6baeaa4SJames Feist     if (it == managedObj.end())
827b6baeaa4SJames Feist     {
82873df0db0SJames Feist         return nullptr;
829b6baeaa4SJames Feist     }
830b6baeaa4SJames Feist     // 5 comes from <chassis-name> being the 5th element
831b6baeaa4SJames Feist     // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
83273df0db0SJames Feist     if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
83373df0db0SJames Feist     {
83473df0db0SJames Feist         return &(*it);
83573df0db0SJames Feist     }
83673df0db0SJames Feist 
83773df0db0SJames Feist     return nullptr;
838b6baeaa4SJames Feist }
839b6baeaa4SJames Feist 
84023a21a1cSEd Tanous inline CreatePIDRet createPidInterface(
8418d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
8429e9b6049SEd Tanous     std::string_view name, nlohmann::json& jsonValue, const std::string& path,
84383ff9ab6SJames Feist     const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
844b9d36b47SEd Tanous     dbus::utility::DBusPropertiesMap& output, std::string& chassis,
845b9d36b47SEd Tanous     const std::string& profile)
84683ff9ab6SJames Feist {
8475f2caaefSJames Feist     // common deleter
8489e9b6049SEd Tanous     if (jsonValue == nullptr)
8495f2caaefSJames Feist     {
8505f2caaefSJames Feist         std::string iface;
8515f2caaefSJames Feist         if (type == "PidControllers" || type == "FanControllers")
8525f2caaefSJames Feist         {
8535f2caaefSJames Feist             iface = pidConfigurationIface;
8545f2caaefSJames Feist         }
8555f2caaefSJames Feist         else if (type == "FanZones")
8565f2caaefSJames Feist         {
8575f2caaefSJames Feist             iface = pidZoneConfigurationIface;
8585f2caaefSJames Feist         }
8595f2caaefSJames Feist         else if (type == "StepwiseControllers")
8605f2caaefSJames Feist         {
8615f2caaefSJames Feist             iface = stepwiseConfigurationIface;
8625f2caaefSJames Feist         }
8635f2caaefSJames Feist         else
8645f2caaefSJames Feist         {
86562598e31SEd Tanous             BMCWEB_LOG_ERROR("Illegal Type {}", type);
8665f2caaefSJames Feist             messages::propertyUnknown(response->res, type);
8675f2caaefSJames Feist             return CreatePIDRet::fail;
8685f2caaefSJames Feist         }
8696ee7f774SJames Feist 
87062598e31SEd Tanous         BMCWEB_LOG_DEBUG("del {} {}", path, iface);
8715f2caaefSJames Feist         // delete interface
8725f2caaefSJames Feist         crow::connections::systemBus->async_method_call(
8735e7e2dc5SEd Tanous             [response, path](const boost::system::error_code& ec) {
8745f2caaefSJames Feist                 if (ec)
8755f2caaefSJames Feist                 {
87662598e31SEd Tanous                     BMCWEB_LOG_ERROR("Error patching {}: {}", path, ec);
8775f2caaefSJames Feist                     messages::internalError(response->res);
878b6baeaa4SJames Feist                     return;
8795f2caaefSJames Feist                 }
880b6baeaa4SJames Feist                 messages::success(response->res);
8815f2caaefSJames Feist             },
8825f2caaefSJames Feist             "xyz.openbmc_project.EntityManager", path, iface, "Delete");
8835f2caaefSJames Feist         return CreatePIDRet::del;
8845f2caaefSJames Feist     }
8855f2caaefSJames Feist 
886711ac7a9SEd Tanous     const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
887b6baeaa4SJames Feist     if (!createNewObject)
888b6baeaa4SJames Feist     {
889b6baeaa4SJames Feist         // if we aren't creating a new object, we should be able to find it on
890b6baeaa4SJames Feist         // d-bus
8919e9b6049SEd Tanous         managedItem = findChassis(managedObj, name, chassis);
89273df0db0SJames Feist         if (managedItem == nullptr)
893b6baeaa4SJames Feist         {
89462598e31SEd Tanous             BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
895ef4c65b7SEd Tanous             messages::invalidObject(
896ef4c65b7SEd Tanous                 response->res,
897ef4c65b7SEd Tanous                 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
898b6baeaa4SJames Feist             return CreatePIDRet::fail;
899b6baeaa4SJames Feist         }
900b6baeaa4SJames Feist     }
901b6baeaa4SJames Feist 
90226f6976fSEd Tanous     if (!profile.empty() &&
90373df0db0SJames Feist         (type == "PidControllers" || type == "FanControllers" ||
90473df0db0SJames Feist          type == "StepwiseControllers"))
90573df0db0SJames Feist     {
90673df0db0SJames Feist         if (managedItem == nullptr)
90773df0db0SJames Feist         {
908b9d36b47SEd Tanous             output.emplace_back("Profiles", std::vector<std::string>{profile});
90973df0db0SJames Feist         }
91073df0db0SJames Feist         else
91173df0db0SJames Feist         {
91273df0db0SJames Feist             std::string interface;
91373df0db0SJames Feist             if (type == "StepwiseControllers")
91473df0db0SJames Feist             {
91573df0db0SJames Feist                 interface = stepwiseConfigurationIface;
91673df0db0SJames Feist             }
91773df0db0SJames Feist             else
91873df0db0SJames Feist             {
91973df0db0SJames Feist                 interface = pidConfigurationIface;
92073df0db0SJames Feist             }
921711ac7a9SEd Tanous             bool ifaceFound = false;
922711ac7a9SEd Tanous             for (const auto& iface : managedItem->second)
923711ac7a9SEd Tanous             {
924711ac7a9SEd Tanous                 if (iface.first == interface)
925711ac7a9SEd Tanous                 {
926711ac7a9SEd Tanous                     ifaceFound = true;
927711ac7a9SEd Tanous                     for (const auto& prop : iface.second)
928711ac7a9SEd Tanous                     {
929711ac7a9SEd Tanous                         if (prop.first == "Profiles")
930711ac7a9SEd Tanous                         {
931711ac7a9SEd Tanous                             const std::vector<std::string>* curProfiles =
932711ac7a9SEd Tanous                                 std::get_if<std::vector<std::string>>(
933711ac7a9SEd Tanous                                     &(prop.second));
934711ac7a9SEd Tanous                             if (curProfiles == nullptr)
935711ac7a9SEd Tanous                             {
93662598e31SEd Tanous                                 BMCWEB_LOG_ERROR(
93762598e31SEd Tanous                                     "Illegal profiles in managed object");
938711ac7a9SEd Tanous                                 messages::internalError(response->res);
939711ac7a9SEd Tanous                                 return CreatePIDRet::fail;
940711ac7a9SEd Tanous                             }
941711ac7a9SEd Tanous                             if (std::find(curProfiles->begin(),
942bd79bce8SPatrick Williams                                           curProfiles->end(), profile) ==
943bd79bce8SPatrick Williams                                 curProfiles->end())
944711ac7a9SEd Tanous                             {
945711ac7a9SEd Tanous                                 std::vector<std::string> newProfiles =
946711ac7a9SEd Tanous                                     *curProfiles;
947711ac7a9SEd Tanous                                 newProfiles.push_back(profile);
948b9d36b47SEd Tanous                                 output.emplace_back("Profiles", newProfiles);
949711ac7a9SEd Tanous                             }
950711ac7a9SEd Tanous                         }
951711ac7a9SEd Tanous                     }
952711ac7a9SEd Tanous                 }
953711ac7a9SEd Tanous             }
954711ac7a9SEd Tanous 
955711ac7a9SEd Tanous             if (!ifaceFound)
95673df0db0SJames Feist             {
95762598e31SEd Tanous                 BMCWEB_LOG_ERROR("Failed to find interface in managed object");
95873df0db0SJames Feist                 messages::internalError(response->res);
95973df0db0SJames Feist                 return CreatePIDRet::fail;
96073df0db0SJames Feist             }
96173df0db0SJames Feist         }
96273df0db0SJames Feist     }
96373df0db0SJames Feist 
96483ff9ab6SJames Feist     if (type == "PidControllers" || type == "FanControllers")
96583ff9ab6SJames Feist     {
96683ff9ab6SJames Feist         if (createNewObject)
96783ff9ab6SJames Feist         {
968b9d36b47SEd Tanous             output.emplace_back("Class",
969b9d36b47SEd Tanous                                 type == "PidControllers" ? "temp" : "fan");
970b9d36b47SEd Tanous             output.emplace_back("Type", "Pid");
97183ff9ab6SJames Feist         }
9725f2caaefSJames Feist 
9739e9b6049SEd Tanous         std::optional<std::vector<nlohmann::json::object_t>> zones;
9745f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
9755f2caaefSJames Feist         std::optional<std::vector<std::string>> outputs;
9765f2caaefSJames Feist         std::map<std::string, std::optional<double>> doubles;
977b943aaefSJames Feist         std::optional<std::string> setpointOffset;
978afc474aeSMyung Bae         if (!redfish::json_util::readJson( //
979afc474aeSMyung Bae                 jsonValue, response->res, //
980afc474aeSMyung Bae                 "FFGainCoefficient", doubles["FFGainCoefficient"], //
981afc474aeSMyung Bae                 "FFOffCoefficient", doubles["FFOffCoefficient"], //
982afc474aeSMyung Bae                 "ICoefficient", doubles["ICoefficient"], //
983afc474aeSMyung Bae                 "ILimitMax", doubles["ILimitMax"], //
984afc474aeSMyung Bae                 "ILimitMin", doubles["ILimitMin"], //
985afc474aeSMyung Bae                 "Inputs", inputs, //
986afc474aeSMyung Bae                 "NegativeHysteresis", doubles["NegativeHysteresis"], //
987afc474aeSMyung Bae                 "OutLimitMax", doubles["OutLimitMax"], //
988afc474aeSMyung Bae                 "OutLimitMin", doubles["OutLimitMin"], //
989afc474aeSMyung Bae                 "Outputs", outputs, //
990afc474aeSMyung Bae                 "PCoefficient", doubles["PCoefficient"], //
991afc474aeSMyung Bae                 "PositiveHysteresis", doubles["PositiveHysteresis"], //
992afc474aeSMyung Bae                 "SetPoint", doubles["SetPoint"], //
993afc474aeSMyung Bae                 "SetPointOffset", setpointOffset, //
994afc474aeSMyung Bae                 "SlewNeg", doubles["SlewNeg"], //
995afc474aeSMyung Bae                 "SlewPos", doubles["SlewPos"], //
996afc474aeSMyung Bae                 "Zones", zones //
997afc474aeSMyung Bae                 ))
99883ff9ab6SJames Feist         {
9995f2caaefSJames Feist             return CreatePIDRet::fail;
100083ff9ab6SJames Feist         }
1001afc474aeSMyung Bae 
10025f2caaefSJames Feist         if (zones)
10035f2caaefSJames Feist         {
10045f2caaefSJames Feist             std::vector<std::string> zonesStr;
10055f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStr))
10065f2caaefSJames Feist             {
100762598e31SEd Tanous                 BMCWEB_LOG_ERROR("Illegal Zones");
10085f2caaefSJames Feist                 return CreatePIDRet::fail;
10095f2caaefSJames Feist             }
1010b6baeaa4SJames Feist             if (chassis.empty() &&
1011e662eae8SEd Tanous                 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
1012b6baeaa4SJames Feist             {
101362598e31SEd Tanous                 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
1014ace85d60SEd Tanous                 messages::invalidObject(
1015ef4c65b7SEd Tanous                     response->res,
1016ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", chassis));
1017b6baeaa4SJames Feist                 return CreatePIDRet::fail;
1018b6baeaa4SJames Feist             }
1019b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStr));
10205f2caaefSJames Feist         }
1021afb9ee06SEd Tanous 
1022afb9ee06SEd Tanous         if (inputs)
10235f2caaefSJames Feist         {
1024afb9ee06SEd Tanous             for (std::string& value : *inputs)
102583ff9ab6SJames Feist             {
1026a170f275SEd Tanous                 std::replace(value.begin(), value.end(), '_', ' ');
102783ff9ab6SJames Feist             }
1028afb9ee06SEd Tanous             output.emplace_back("Inputs", *inputs);
1029afb9ee06SEd Tanous         }
1030afb9ee06SEd Tanous 
1031afb9ee06SEd Tanous         if (outputs)
10325f2caaefSJames Feist         {
1033afb9ee06SEd Tanous             for (std::string& value : *outputs)
10345f2caaefSJames Feist             {
1035afb9ee06SEd Tanous                 std::replace(value.begin(), value.end(), '_', ' ');
10365f2caaefSJames Feist             }
1037afb9ee06SEd Tanous             output.emplace_back("Outputs", *outputs);
103883ff9ab6SJames Feist         }
103983ff9ab6SJames Feist 
1040b943aaefSJames Feist         if (setpointOffset)
1041b943aaefSJames Feist         {
1042b943aaefSJames Feist             // translate between redfish and dbus names
1043b943aaefSJames Feist             if (*setpointOffset == "UpperThresholdNonCritical")
1044b943aaefSJames Feist             {
1045b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningLow");
1046b943aaefSJames Feist             }
1047b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdNonCritical")
1048b943aaefSJames Feist             {
1049b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningHigh");
1050b943aaefSJames Feist             }
1051b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdCritical")
1052b943aaefSJames Feist             {
1053b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalLow");
1054b943aaefSJames Feist             }
1055b943aaefSJames Feist             else if (*setpointOffset == "UpperThresholdCritical")
1056b943aaefSJames Feist             {
1057b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalHigh");
1058b943aaefSJames Feist             }
1059b943aaefSJames Feist             else
1060b943aaefSJames Feist             {
106162598e31SEd Tanous                 BMCWEB_LOG_ERROR("Invalid setpointoffset {}", *setpointOffset);
10629e9b6049SEd Tanous                 messages::propertyValueNotInList(response->res, name,
1063ace85d60SEd Tanous                                                  "SetPointOffset");
1064b943aaefSJames Feist                 return CreatePIDRet::fail;
1065b943aaefSJames Feist             }
1066b943aaefSJames Feist         }
1067b943aaefSJames Feist 
106883ff9ab6SJames Feist         // doubles
10695f2caaefSJames Feist         for (const auto& pairs : doubles)
107083ff9ab6SJames Feist         {
10715f2caaefSJames Feist             if (!pairs.second)
107283ff9ab6SJames Feist             {
10735f2caaefSJames Feist                 continue;
107483ff9ab6SJames Feist             }
107562598e31SEd Tanous             BMCWEB_LOG_DEBUG("{} = {}", pairs.first, *pairs.second);
1076b9d36b47SEd Tanous             output.emplace_back(pairs.first, *pairs.second);
10775f2caaefSJames Feist         }
107883ff9ab6SJames Feist     }
107983ff9ab6SJames Feist 
108083ff9ab6SJames Feist     else if (type == "FanZones")
108183ff9ab6SJames Feist     {
1082b9d36b47SEd Tanous         output.emplace_back("Type", "Pid.Zone");
108383ff9ab6SJames Feist 
10849e9b6049SEd Tanous         std::optional<std::string> chassisId;
10855f2caaefSJames Feist         std::optional<double> failSafePercent;
1086d3ec07f8SJames Feist         std::optional<double> minThermalOutput;
1087afc474aeSMyung Bae         if (!redfish::json_util::readJson( //
1088afc474aeSMyung Bae                 jsonValue, response->res, //
1089afc474aeSMyung Bae                 "Chassis/@odata.id", chassisId, //
1090afc474aeSMyung Bae                 "FailSafePercent", failSafePercent, //
1091afc474aeSMyung Bae                 "MinThermalOutput", minThermalOutput))
109283ff9ab6SJames Feist         {
109383ff9ab6SJames Feist             return CreatePIDRet::fail;
109483ff9ab6SJames Feist         }
10955f2caaefSJames Feist 
10969e9b6049SEd Tanous         if (chassisId)
109783ff9ab6SJames Feist         {
1098717794d5SAppaRao Puli             // /redfish/v1/chassis/chassis_name/
10999e9b6049SEd Tanous             if (!dbus::utility::getNthStringFromPath(*chassisId, 3, chassis))
110083ff9ab6SJames Feist             {
11019e9b6049SEd Tanous                 BMCWEB_LOG_ERROR("Got invalid path {}", *chassisId);
1102ace85d60SEd Tanous                 messages::invalidObject(
1103ef4c65b7SEd Tanous                     response->res,
11049e9b6049SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", *chassisId));
110583ff9ab6SJames Feist                 return CreatePIDRet::fail;
110683ff9ab6SJames Feist             }
110783ff9ab6SJames Feist         }
1108d3ec07f8SJames Feist         if (minThermalOutput)
110983ff9ab6SJames Feist         {
1110b9d36b47SEd Tanous             output.emplace_back("MinThermalOutput", *minThermalOutput);
11115f2caaefSJames Feist         }
11125f2caaefSJames Feist         if (failSafePercent)
111383ff9ab6SJames Feist         {
1114b9d36b47SEd Tanous             output.emplace_back("FailSafePercent", *failSafePercent);
11155f2caaefSJames Feist         }
11165f2caaefSJames Feist     }
11175f2caaefSJames Feist     else if (type == "StepwiseControllers")
11185f2caaefSJames Feist     {
1119b9d36b47SEd Tanous         output.emplace_back("Type", "Stepwise");
11205f2caaefSJames Feist 
11219e9b6049SEd Tanous         std::optional<std::vector<nlohmann::json::object_t>> zones;
11229e9b6049SEd Tanous         std::optional<std::vector<nlohmann::json::object_t>> steps;
11235f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
11245f2caaefSJames Feist         std::optional<double> positiveHysteresis;
11255f2caaefSJames Feist         std::optional<double> negativeHysteresis;
1126c33a90ecSJames Feist         std::optional<std::string> direction; // upper clipping curve vs lower
1127afc474aeSMyung Bae         if (!redfish::json_util::readJson( //
1128afc474aeSMyung Bae                 jsonValue, response->res, //
1129afc474aeSMyung Bae                 "Direction", direction, //
1130afc474aeSMyung Bae                 "Inputs", inputs, //
1131afc474aeSMyung Bae                 "NegativeHysteresis", negativeHysteresis, //
1132afc474aeSMyung Bae                 "PositiveHysteresis", positiveHysteresis, //
1133afc474aeSMyung Bae                 "Steps", steps, //
1134afc474aeSMyung Bae                 "Zones", zones //
1135afc474aeSMyung Bae                 ))
11365f2caaefSJames Feist         {
113783ff9ab6SJames Feist             return CreatePIDRet::fail;
113883ff9ab6SJames Feist         }
11395f2caaefSJames Feist 
11405f2caaefSJames Feist         if (zones)
114183ff9ab6SJames Feist         {
1142b6baeaa4SJames Feist             std::vector<std::string> zonesStrs;
1143b6baeaa4SJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStrs))
11445f2caaefSJames Feist             {
114562598e31SEd Tanous                 BMCWEB_LOG_ERROR("Illegal Zones");
114683ff9ab6SJames Feist                 return CreatePIDRet::fail;
114783ff9ab6SJames Feist             }
1148b6baeaa4SJames Feist             if (chassis.empty() &&
1149e662eae8SEd Tanous                 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
1150b6baeaa4SJames Feist             {
115162598e31SEd Tanous                 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
1152ace85d60SEd Tanous                 messages::invalidObject(
1153ef4c65b7SEd Tanous                     response->res,
1154ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", chassis));
1155b6baeaa4SJames Feist                 return CreatePIDRet::fail;
1156b6baeaa4SJames Feist             }
1157b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStrs));
11585f2caaefSJames Feist         }
11595f2caaefSJames Feist         if (steps)
11605f2caaefSJames Feist         {
11615f2caaefSJames Feist             std::vector<double> readings;
11625f2caaefSJames Feist             std::vector<double> outputs;
11635f2caaefSJames Feist             for (auto& step : *steps)
11645f2caaefSJames Feist             {
1165543f4400SEd Tanous                 double target = 0.0;
1166543f4400SEd Tanous                 double out = 0.0;
11675f2caaefSJames Feist 
1168afc474aeSMyung Bae                 if (!redfish::json_util::readJsonObject( //
1169afc474aeSMyung Bae                         step, response->res, //
1170afc474aeSMyung Bae                         "Output", out, //
1171afc474aeSMyung Bae                         "Target", target //
1172afc474aeSMyung Bae                         ))
11735f2caaefSJames Feist                 {
11745f2caaefSJames Feist                     return CreatePIDRet::fail;
11755f2caaefSJames Feist                 }
11765f2caaefSJames Feist                 readings.emplace_back(target);
117723a21a1cSEd Tanous                 outputs.emplace_back(out);
11785f2caaefSJames Feist             }
1179b9d36b47SEd Tanous             output.emplace_back("Reading", std::move(readings));
1180b9d36b47SEd Tanous             output.emplace_back("Output", std::move(outputs));
11815f2caaefSJames Feist         }
11825f2caaefSJames Feist         if (inputs)
11835f2caaefSJames Feist         {
11845f2caaefSJames Feist             for (std::string& value : *inputs)
11855f2caaefSJames Feist             {
1186a170f275SEd Tanous                 std::replace(value.begin(), value.end(), '_', ' ');
11875f2caaefSJames Feist             }
1188b9d36b47SEd Tanous             output.emplace_back("Inputs", std::move(*inputs));
11895f2caaefSJames Feist         }
11905f2caaefSJames Feist         if (negativeHysteresis)
11915f2caaefSJames Feist         {
1192b9d36b47SEd Tanous             output.emplace_back("NegativeHysteresis", *negativeHysteresis);
11935f2caaefSJames Feist         }
11945f2caaefSJames Feist         if (positiveHysteresis)
11955f2caaefSJames Feist         {
1196b9d36b47SEd Tanous             output.emplace_back("PositiveHysteresis", *positiveHysteresis);
119783ff9ab6SJames Feist         }
1198c33a90ecSJames Feist         if (direction)
1199c33a90ecSJames Feist         {
1200c33a90ecSJames Feist             constexpr const std::array<const char*, 2> allowedDirections = {
1201c33a90ecSJames Feist                 "Ceiling", "Floor"};
12023544d2a7SEd Tanous             if (std::ranges::find(allowedDirections, *direction) ==
12033544d2a7SEd Tanous                 allowedDirections.end())
1204c33a90ecSJames Feist             {
1205c33a90ecSJames Feist                 messages::propertyValueTypeError(response->res, "Direction",
1206c33a90ecSJames Feist                                                  *direction);
1207c33a90ecSJames Feist                 return CreatePIDRet::fail;
1208c33a90ecSJames Feist             }
1209b9d36b47SEd Tanous             output.emplace_back("Class", *direction);
1210c33a90ecSJames Feist         }
121183ff9ab6SJames Feist     }
121283ff9ab6SJames Feist     else
121383ff9ab6SJames Feist     {
121462598e31SEd Tanous         BMCWEB_LOG_ERROR("Illegal Type {}", type);
121535a62c7cSJason M. Bills         messages::propertyUnknown(response->res, type);
121683ff9ab6SJames Feist         return CreatePIDRet::fail;
121783ff9ab6SJames Feist     }
121883ff9ab6SJames Feist     return CreatePIDRet::patch;
121983ff9ab6SJames Feist }
122073df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
122173df0db0SJames Feist {
12226936afe4SEd Tanous     struct CompletionValues
12236936afe4SEd Tanous     {
12246936afe4SEd Tanous         std::vector<std::string> supportedProfiles;
12256936afe4SEd Tanous         std::string currentProfile;
12266936afe4SEd Tanous         dbus::utility::MapperGetSubTreeResponse subtree;
12276936afe4SEd Tanous     };
122883ff9ab6SJames Feist 
12294e23a444SEd Tanous     explicit GetPIDValues(
12304e23a444SEd Tanous         const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
123123a21a1cSEd Tanous         asyncResp(asyncRespIn)
123273df0db0SJames Feist 
12331214b7e7SGunnar Mills     {}
12349c310685SBorawski.Lukasz 
123573df0db0SJames Feist     void run()
12365b4aa86bSJames Feist     {
123773df0db0SJames Feist         std::shared_ptr<GetPIDValues> self = shared_from_this();
123873df0db0SJames Feist 
123973df0db0SJames Feist         // get all configurations
1240e99073f5SGeorge Liu         constexpr std::array<std::string_view, 4> interfaces = {
1241e99073f5SGeorge Liu             pidConfigurationIface, pidZoneConfigurationIface,
1242e99073f5SGeorge Liu             objectManagerIface, stepwiseConfigurationIface};
1243e99073f5SGeorge Liu         dbus::utility::getSubTree(
1244e99073f5SGeorge Liu             "/", 0, interfaces,
1245b9d36b47SEd Tanous             [self](
1246e99073f5SGeorge Liu                 const boost::system::error_code& ec,
1247b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
12485b4aa86bSJames Feist                 if (ec)
12495b4aa86bSJames Feist                 {
125062598e31SEd Tanous                     BMCWEB_LOG_ERROR("{}", ec);
125173df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
125273df0db0SJames Feist                     return;
125373df0db0SJames Feist                 }
12546936afe4SEd Tanous                 self->complete.subtree = subtreeLocal;
1255e99073f5SGeorge Liu             });
125673df0db0SJames Feist 
125773df0db0SJames Feist         // at the same time get the selected profile
1258e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1259e99073f5SGeorge Liu             thermalModeIface};
1260e99073f5SGeorge Liu         dbus::utility::getSubTree(
1261e99073f5SGeorge Liu             "/", 0, thermalModeIfaces,
1262b9d36b47SEd Tanous             [self](
1263e99073f5SGeorge Liu                 const boost::system::error_code& ec,
1264b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
126523a21a1cSEd Tanous                 if (ec || subtreeLocal.empty())
126673df0db0SJames Feist                 {
126773df0db0SJames Feist                     return;
126873df0db0SJames Feist                 }
126923a21a1cSEd Tanous                 if (subtreeLocal[0].second.size() != 1)
127073df0db0SJames Feist                 {
127173df0db0SJames Feist                     // invalid mapper response, should never happen
127262598e31SEd Tanous                     BMCWEB_LOG_ERROR("GetPIDValues: Mapper Error");
127373df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
12745b4aa86bSJames Feist                     return;
12755b4aa86bSJames Feist                 }
12765b4aa86bSJames Feist 
127723a21a1cSEd Tanous                 const std::string& path = subtreeLocal[0].first;
127823a21a1cSEd Tanous                 const std::string& owner = subtreeLocal[0].second[0].first;
1279fac6e53bSKrzysztof Grobelny 
1280deae6a78SEd Tanous                 dbus::utility::getAllProperties(
1281bd79bce8SPatrick Williams                     *crow::connections::systemBus, owner, path,
1282bd79bce8SPatrick Williams                     thermalModeIface,
1283168e20c1SEd Tanous                     [path, owner,
12845e7e2dc5SEd Tanous                      self](const boost::system::error_code& ec2,
1285b9d36b47SEd Tanous                            const dbus::utility::DBusPropertiesMap& resp) {
128623a21a1cSEd Tanous                         if (ec2)
128773df0db0SJames Feist                         {
128862598e31SEd Tanous                             BMCWEB_LOG_ERROR(
1289bd79bce8SPatrick Williams                                 "GetPIDValues: Can't get thermalModeIface {}",
1290bd79bce8SPatrick Williams                                 path);
129173df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
129273df0db0SJames Feist                             return;
129373df0db0SJames Feist                         }
1294fac6e53bSKrzysztof Grobelny 
1295271584abSEd Tanous                         const std::string* current = nullptr;
1296271584abSEd Tanous                         const std::vector<std::string>* supported = nullptr;
1297fac6e53bSKrzysztof Grobelny 
1298fac6e53bSKrzysztof Grobelny                         const bool success = sdbusplus::unpackPropertiesNoThrow(
1299bd79bce8SPatrick Williams                             dbus_utils::UnpackErrorPrinter(), resp, "Current",
1300bd79bce8SPatrick Williams                             current, "Supported", supported);
1301fac6e53bSKrzysztof Grobelny 
1302fac6e53bSKrzysztof Grobelny                         if (!success)
130373df0db0SJames Feist                         {
1304002d39b4SEd Tanous                             messages::internalError(self->asyncResp->res);
130573df0db0SJames Feist                             return;
130673df0db0SJames Feist                         }
1307fac6e53bSKrzysztof Grobelny 
130873df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
130973df0db0SJames Feist                         {
131062598e31SEd Tanous                             BMCWEB_LOG_ERROR(
1311bd79bce8SPatrick Williams                                 "GetPIDValues: thermal mode iface invalid {}",
1312bd79bce8SPatrick Williams                                 path);
131373df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
131473df0db0SJames Feist                             return;
131573df0db0SJames Feist                         }
13166936afe4SEd Tanous                         self->complete.currentProfile = *current;
13176936afe4SEd Tanous                         self->complete.supportedProfiles = *supported;
1318fac6e53bSKrzysztof Grobelny                     });
1319e99073f5SGeorge Liu             });
132073df0db0SJames Feist     }
132173df0db0SJames Feist 
13226936afe4SEd Tanous     static void
13236936afe4SEd Tanous         processingComplete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
13246936afe4SEd Tanous                            const CompletionValues& completion)
132573df0db0SJames Feist     {
132673df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
132773df0db0SJames Feist         {
132873df0db0SJames Feist             return;
132973df0db0SJames Feist         }
13305b4aa86bSJames Feist         // create map of <connection, path to objMgr>>
13316936afe4SEd Tanous         boost::container::flat_map<
13326936afe4SEd Tanous             std::string, std::string, std::less<>,
13336936afe4SEd Tanous             std::vector<std::pair<std::string, std::string>>>
13346936afe4SEd Tanous             objectMgrPaths;
13356936afe4SEd Tanous         boost::container::flat_set<std::string, std::less<>,
13366936afe4SEd Tanous                                    std::vector<std::string>>
13376936afe4SEd Tanous             calledConnections;
13386936afe4SEd Tanous         for (const auto& pathGroup : completion.subtree)
13395b4aa86bSJames Feist         {
13405b4aa86bSJames Feist             for (const auto& connectionGroup : pathGroup.second)
13415b4aa86bSJames Feist             {
13426bce33bcSJames Feist                 auto findConnection =
13436bce33bcSJames Feist                     calledConnections.find(connectionGroup.first);
13446bce33bcSJames Feist                 if (findConnection != calledConnections.end())
13456bce33bcSJames Feist                 {
13466bce33bcSJames Feist                     break;
13476bce33bcSJames Feist                 }
134873df0db0SJames Feist                 for (const std::string& interface : connectionGroup.second)
13495b4aa86bSJames Feist                 {
13505b4aa86bSJames Feist                     if (interface == objectManagerIface)
13515b4aa86bSJames Feist                     {
135273df0db0SJames Feist                         objectMgrPaths[connectionGroup.first] = pathGroup.first;
13535b4aa86bSJames Feist                     }
13545b4aa86bSJames Feist                     // this list is alphabetical, so we
13555b4aa86bSJames Feist                     // should have found the objMgr by now
13565b4aa86bSJames Feist                     if (interface == pidConfigurationIface ||
1357b7a08d04SJames Feist                         interface == pidZoneConfigurationIface ||
1358b7a08d04SJames Feist                         interface == stepwiseConfigurationIface)
13595b4aa86bSJames Feist                     {
13605b4aa86bSJames Feist                         auto findObjMgr =
13615b4aa86bSJames Feist                             objectMgrPaths.find(connectionGroup.first);
13625b4aa86bSJames Feist                         if (findObjMgr == objectMgrPaths.end())
13635b4aa86bSJames Feist                         {
136462598e31SEd Tanous                             BMCWEB_LOG_DEBUG("{}Has no Object Manager",
136562598e31SEd Tanous                                              connectionGroup.first);
13665b4aa86bSJames Feist                             continue;
13675b4aa86bSJames Feist                         }
13686bce33bcSJames Feist 
13696bce33bcSJames Feist                         calledConnections.insert(connectionGroup.first);
13706bce33bcSJames Feist 
137173df0db0SJames Feist                         asyncPopulatePid(findObjMgr->first, findObjMgr->second,
13726936afe4SEd Tanous                                          completion.currentProfile,
13736936afe4SEd Tanous                                          completion.supportedProfiles,
137473df0db0SJames Feist                                          asyncResp);
13755b4aa86bSJames Feist                         break;
13765b4aa86bSJames Feist                     }
13775b4aa86bSJames Feist                 }
13785b4aa86bSJames Feist             }
13795b4aa86bSJames Feist         }
138073df0db0SJames Feist     }
138173df0db0SJames Feist 
13826936afe4SEd Tanous     ~GetPIDValues()
13836936afe4SEd Tanous     {
13846936afe4SEd Tanous         boost::asio::post(crow::connections::systemBus->get_io_context(),
13856936afe4SEd Tanous                           std::bind_front(&processingComplete, asyncResp,
13866936afe4SEd Tanous                                           std::move(complete)));
13876936afe4SEd Tanous     }
13886936afe4SEd Tanous 
1389ecd6a3a2SEd Tanous     GetPIDValues(const GetPIDValues&) = delete;
1390ecd6a3a2SEd Tanous     GetPIDValues(GetPIDValues&&) = delete;
1391ecd6a3a2SEd Tanous     GetPIDValues& operator=(const GetPIDValues&) = delete;
1392ecd6a3a2SEd Tanous     GetPIDValues& operator=(GetPIDValues&&) = delete;
1393ecd6a3a2SEd Tanous 
13948d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
13956936afe4SEd Tanous     CompletionValues complete;
139673df0db0SJames Feist };
139773df0db0SJames Feist 
139873df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
139973df0db0SJames Feist {
14009e9b6049SEd Tanous     SetPIDValues(
14019e9b6049SEd Tanous         const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
14029e9b6049SEd Tanous         std::vector<
14039e9b6049SEd Tanous             std::pair<std::string, std::optional<nlohmann::json::object_t>>>&&
14049e9b6049SEd Tanous             configurationsIn,
14059e9b6049SEd Tanous         std::optional<std::string>& profileIn) :
1406bd79bce8SPatrick Williams         asyncResp(asyncRespIn), configuration(std::move(configurationsIn)),
14079e9b6049SEd Tanous         profile(std::move(profileIn))
14089e9b6049SEd Tanous     {}
1409ecd6a3a2SEd Tanous 
1410ecd6a3a2SEd Tanous     SetPIDValues(const SetPIDValues&) = delete;
1411ecd6a3a2SEd Tanous     SetPIDValues(SetPIDValues&&) = delete;
1412ecd6a3a2SEd Tanous     SetPIDValues& operator=(const SetPIDValues&) = delete;
1413ecd6a3a2SEd Tanous     SetPIDValues& operator=(SetPIDValues&&) = delete;
1414ecd6a3a2SEd Tanous 
141573df0db0SJames Feist     void run()
141673df0db0SJames Feist     {
141773df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
141873df0db0SJames Feist         {
141973df0db0SJames Feist             return;
142073df0db0SJames Feist         }
142173df0db0SJames Feist 
142273df0db0SJames Feist         std::shared_ptr<SetPIDValues> self = shared_from_this();
142373df0db0SJames Feist 
142473df0db0SJames Feist         // todo(james): might make sense to do a mapper call here if this
142573df0db0SJames Feist         // interface gets more traction
14265eb468daSGeorge Liu         sdbusplus::message::object_path objPath(
14275eb468daSGeorge Liu             "/xyz/openbmc_project/inventory");
14285eb468daSGeorge Liu         dbus::utility::getManagedObjects(
14295eb468daSGeorge Liu             "xyz.openbmc_project.EntityManager", objPath,
14305e7e2dc5SEd Tanous             [self](const boost::system::error_code& ec,
1431914e2d5dSEd Tanous                    const dbus::utility::ManagedObjectType& mObj) {
143273df0db0SJames Feist                 if (ec)
143373df0db0SJames Feist                 {
143462598e31SEd Tanous                     BMCWEB_LOG_ERROR("Error communicating to Entity Manager");
143573df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
143673df0db0SJames Feist                     return;
143773df0db0SJames Feist                 }
1438e69d9de2SJames Feist                 const std::array<const char*, 3> configurations = {
1439e69d9de2SJames Feist                     pidConfigurationIface, pidZoneConfigurationIface,
1440e69d9de2SJames Feist                     stepwiseConfigurationIface};
1441e69d9de2SJames Feist 
144214b0b8d5SJames Feist                 for (const auto& [path, object] : mObj)
1443e69d9de2SJames Feist                 {
144414b0b8d5SJames Feist                     for (const auto& [interface, _] : object)
1445e69d9de2SJames Feist                     {
14463544d2a7SEd Tanous                         if (std::ranges::find(configurations, interface) !=
14473544d2a7SEd Tanous                             configurations.end())
1448e69d9de2SJames Feist                         {
144914b0b8d5SJames Feist                             self->objectCount++;
1450e69d9de2SJames Feist                             break;
1451e69d9de2SJames Feist                         }
1452e69d9de2SJames Feist                     }
1453e69d9de2SJames Feist                 }
1454914e2d5dSEd Tanous                 self->managedObj = mObj;
14555eb468daSGeorge Liu             });
145673df0db0SJames Feist 
145773df0db0SJames Feist         // at the same time get the profile information
1458e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1459e99073f5SGeorge Liu             thermalModeIface};
1460e99073f5SGeorge Liu         dbus::utility::getSubTree(
1461e99073f5SGeorge Liu             "/", 0, thermalModeIfaces,
1462e99073f5SGeorge Liu             [self](const boost::system::error_code& ec,
1463b9d36b47SEd Tanous                    const dbus::utility::MapperGetSubTreeResponse& subtree) {
146473df0db0SJames Feist                 if (ec || subtree.empty())
146573df0db0SJames Feist                 {
146673df0db0SJames Feist                     return;
146773df0db0SJames Feist                 }
146873df0db0SJames Feist                 if (subtree[0].second.empty())
146973df0db0SJames Feist                 {
147073df0db0SJames Feist                     // invalid mapper response, should never happen
147162598e31SEd Tanous                     BMCWEB_LOG_ERROR("SetPIDValues: Mapper Error");
147273df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
147373df0db0SJames Feist                     return;
147473df0db0SJames Feist                 }
147573df0db0SJames Feist 
147673df0db0SJames Feist                 const std::string& path = subtree[0].first;
147773df0db0SJames Feist                 const std::string& owner = subtree[0].second[0].first;
1478deae6a78SEd Tanous                 dbus::utility::getAllProperties(
1479bd79bce8SPatrick Williams                     *crow::connections::systemBus, owner, path,
1480bd79bce8SPatrick Williams                     thermalModeIface,
1481bd79bce8SPatrick Williams                     [self, path,
1482bd79bce8SPatrick Williams                      owner](const boost::system::error_code& ec2,
1483b9d36b47SEd Tanous                             const dbus::utility::DBusPropertiesMap& r) {
1484cb13a392SEd Tanous                         if (ec2)
148573df0db0SJames Feist                         {
148662598e31SEd Tanous                             BMCWEB_LOG_ERROR(
1487bd79bce8SPatrick Williams                                 "SetPIDValues: Can't get thermalModeIface {}",
1488bd79bce8SPatrick Williams                                 path);
148973df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
149073df0db0SJames Feist                             return;
149173df0db0SJames Feist                         }
1492271584abSEd Tanous                         const std::string* current = nullptr;
1493271584abSEd Tanous                         const std::vector<std::string>* supported = nullptr;
1494fac6e53bSKrzysztof Grobelny 
1495fac6e53bSKrzysztof Grobelny                         const bool success = sdbusplus::unpackPropertiesNoThrow(
1496bd79bce8SPatrick Williams                             dbus_utils::UnpackErrorPrinter(), r, "Current",
1497bd79bce8SPatrick Williams                             current, "Supported", supported);
1498fac6e53bSKrzysztof Grobelny 
1499fac6e53bSKrzysztof Grobelny                         if (!success)
150073df0db0SJames Feist                         {
1501002d39b4SEd Tanous                             messages::internalError(self->asyncResp->res);
150273df0db0SJames Feist                             return;
150373df0db0SJames Feist                         }
1504fac6e53bSKrzysztof Grobelny 
150573df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
150673df0db0SJames Feist                         {
150762598e31SEd Tanous                             BMCWEB_LOG_ERROR(
1508bd79bce8SPatrick Williams                                 "SetPIDValues: thermal mode iface invalid {}",
1509bd79bce8SPatrick Williams                                 path);
151073df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
151173df0db0SJames Feist                             return;
151273df0db0SJames Feist                         }
151373df0db0SJames Feist                         self->currentProfile = *current;
151473df0db0SJames Feist                         self->supportedProfiles = *supported;
151573df0db0SJames Feist                         self->profileConnection = owner;
151673df0db0SJames Feist                         self->profilePath = path;
1517fac6e53bSKrzysztof Grobelny                     });
1518e99073f5SGeorge Liu             });
151973df0db0SJames Feist     }
152024b2fe81SEd Tanous     void pidSetDone()
152173df0db0SJames Feist     {
152273df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
152373df0db0SJames Feist         {
152473df0db0SJames Feist             return;
15255b4aa86bSJames Feist         }
15268d1b46d7Szhanghch05         std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
152773df0db0SJames Feist         if (profile)
152873df0db0SJames Feist         {
15293544d2a7SEd Tanous             if (std::ranges::find(supportedProfiles, *profile) ==
15303544d2a7SEd Tanous                 supportedProfiles.end())
153173df0db0SJames Feist             {
153273df0db0SJames Feist                 messages::actionParameterUnknown(response->res, "Profile",
153373df0db0SJames Feist                                                  *profile);
153473df0db0SJames Feist                 return;
153573df0db0SJames Feist             }
153673df0db0SJames Feist             currentProfile = *profile;
15379ae226faSGeorge Liu             sdbusplus::asio::setProperty(
15389ae226faSGeorge Liu                 *crow::connections::systemBus, profileConnection, profilePath,
15399ae226faSGeorge Liu                 thermalModeIface, "Current", *profile,
15405e7e2dc5SEd Tanous                 [response](const boost::system::error_code& ec) {
154173df0db0SJames Feist                     if (ec)
154273df0db0SJames Feist                     {
154362598e31SEd Tanous                         BMCWEB_LOG_ERROR("Error patching profile{}", ec);
154473df0db0SJames Feist                         messages::internalError(response->res);
154573df0db0SJames Feist                     }
15469ae226faSGeorge Liu                 });
154773df0db0SJames Feist         }
154873df0db0SJames Feist 
154973df0db0SJames Feist         for (auto& containerPair : configuration)
155073df0db0SJames Feist         {
155173df0db0SJames Feist             auto& container = containerPair.second;
155273df0db0SJames Feist             if (!container)
155373df0db0SJames Feist             {
155473df0db0SJames Feist                 continue;
155573df0db0SJames Feist             }
15566ee7f774SJames Feist 
155702cad96eSEd Tanous             const std::string& type = containerPair.first;
155873df0db0SJames Feist 
15599e9b6049SEd Tanous             for (auto& [name, value] : *container)
156073df0db0SJames Feist             {
1561cddbf3dfSPotin Lai                 std::string dbusObjName = name;
1562cddbf3dfSPotin Lai                 std::replace(dbusObjName.begin(), dbusObjName.end(), ' ', '_');
156362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("looking for {}", name);
15646ee7f774SJames Feist 
15653544d2a7SEd Tanous                 auto pathItr = std::ranges::find_if(
15663544d2a7SEd Tanous                     managedObj, [&dbusObjName](const auto& obj) {
156791f75cafSEd Tanous                         return obj.first.filename() == dbusObjName;
156873df0db0SJames Feist                     });
1569b9d36b47SEd Tanous                 dbus::utility::DBusPropertiesMap output;
157073df0db0SJames Feist 
157173df0db0SJames Feist                 output.reserve(16); // The pid interface length
157273df0db0SJames Feist 
157373df0db0SJames Feist                 // determines if we're patching entity-manager or
157473df0db0SJames Feist                 // creating a new object
157573df0db0SJames Feist                 bool createNewObject = (pathItr == managedObj.end());
157662598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Found = {}", !createNewObject);
15776ee7f774SJames Feist 
157873df0db0SJames Feist                 std::string iface;
1579ea2b670dSEd Tanous                 if (!createNewObject)
1580ea2b670dSEd Tanous                 {
15818be2b5b6SPotin Lai                     bool findInterface = false;
1582ea2b670dSEd Tanous                     for (const auto& interface : pathItr->second)
1583ea2b670dSEd Tanous                     {
1584ea2b670dSEd Tanous                         if (interface.first == pidConfigurationIface)
1585ea2b670dSEd Tanous                         {
1586ea2b670dSEd Tanous                             if (type == "PidControllers" ||
1587ea2b670dSEd Tanous                                 type == "FanControllers")
158873df0db0SJames Feist                             {
158973df0db0SJames Feist                                 iface = pidConfigurationIface;
15908be2b5b6SPotin Lai                                 findInterface = true;
15918be2b5b6SPotin Lai                                 break;
159273df0db0SJames Feist                             }
159373df0db0SJames Feist                         }
1594ea2b670dSEd Tanous                         else if (interface.first == pidZoneConfigurationIface)
159573df0db0SJames Feist                         {
1596ea2b670dSEd Tanous                             if (type == "FanZones")
159773df0db0SJames Feist                             {
1598da39350aSPavanKumarIntel                                 iface = pidZoneConfigurationIface;
15998be2b5b6SPotin Lai                                 findInterface = true;
16008be2b5b6SPotin Lai                                 break;
160173df0db0SJames Feist                             }
160273df0db0SJames Feist                         }
1603ea2b670dSEd Tanous                         else if (interface.first == stepwiseConfigurationIface)
1604ea2b670dSEd Tanous                         {
1605ea2b670dSEd Tanous                             if (type == "StepwiseControllers")
160673df0db0SJames Feist                             {
160773df0db0SJames Feist                                 iface = stepwiseConfigurationIface;
16088be2b5b6SPotin Lai                                 findInterface = true;
16098be2b5b6SPotin Lai                                 break;
16108be2b5b6SPotin Lai                             }
16118be2b5b6SPotin Lai                         }
16128be2b5b6SPotin Lai                     }
16138be2b5b6SPotin Lai 
16148be2b5b6SPotin Lai                     // create new object if interface not found
16158be2b5b6SPotin Lai                     if (!findInterface)
16168be2b5b6SPotin Lai                     {
161773df0db0SJames Feist                         createNewObject = true;
161873df0db0SJames Feist                     }
1619ea2b670dSEd Tanous                 }
16206ee7f774SJames Feist 
16219e9b6049SEd Tanous                 if (createNewObject && value == nullptr)
16226ee7f774SJames Feist                 {
16234e0453b1SGunnar Mills                     // can't delete a non-existent object
16249e9b6049SEd Tanous                     messages::propertyValueNotInList(response->res, value,
1625e2616cc5SEd Tanous                                                      name);
16266ee7f774SJames Feist                     continue;
16276ee7f774SJames Feist                 }
16286ee7f774SJames Feist 
16296ee7f774SJames Feist                 std::string path;
16306ee7f774SJames Feist                 if (pathItr != managedObj.end())
16316ee7f774SJames Feist                 {
16326ee7f774SJames Feist                     path = pathItr->first.str;
16336ee7f774SJames Feist                 }
16346ee7f774SJames Feist 
163562598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Create new = {}", createNewObject);
1636e69d9de2SJames Feist 
1637e69d9de2SJames Feist                 // arbitrary limit to avoid attacks
1638e69d9de2SJames Feist                 constexpr const size_t controllerLimit = 500;
163914b0b8d5SJames Feist                 if (createNewObject && objectCount >= controllerLimit)
1640e69d9de2SJames Feist                 {
1641e69d9de2SJames Feist                     messages::resourceExhaustion(response->res, type);
1642e69d9de2SJames Feist                     continue;
1643e69d9de2SJames Feist                 }
1644a170f275SEd Tanous                 std::string escaped = name;
1645a170f275SEd Tanous                 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1646a170f275SEd Tanous                 output.emplace_back("Name", escaped);
164773df0db0SJames Feist 
164873df0db0SJames Feist                 std::string chassis;
164973df0db0SJames Feist                 CreatePIDRet ret = createPidInterface(
16509e9b6049SEd Tanous                     response, type, name, value, path, managedObj,
16519e9b6049SEd Tanous                     createNewObject, output, chassis, currentProfile);
165273df0db0SJames Feist                 if (ret == CreatePIDRet::fail)
165373df0db0SJames Feist                 {
165473df0db0SJames Feist                     return;
165573df0db0SJames Feist                 }
16563174e4dfSEd Tanous                 if (ret == CreatePIDRet::del)
165773df0db0SJames Feist                 {
165873df0db0SJames Feist                     continue;
165973df0db0SJames Feist                 }
166073df0db0SJames Feist 
166173df0db0SJames Feist                 if (!createNewObject)
166273df0db0SJames Feist                 {
166373df0db0SJames Feist                     for (const auto& property : output)
166473df0db0SJames Feist                     {
16657a696974SPotin Lai                         crow::connections::systemBus->async_method_call(
166673df0db0SJames Feist                             [response,
166773df0db0SJames Feist                              propertyName{std::string(property.first)}](
16685e7e2dc5SEd Tanous                                 const boost::system::error_code& ec) {
166973df0db0SJames Feist                                 if (ec)
167073df0db0SJames Feist                                 {
167162598e31SEd Tanous                                     BMCWEB_LOG_ERROR("Error patching {}: {}",
167262598e31SEd Tanous                                                      propertyName, ec);
167373df0db0SJames Feist                                     messages::internalError(response->res);
167473df0db0SJames Feist                                     return;
167573df0db0SJames Feist                                 }
167673df0db0SJames Feist                                 messages::success(response->res);
16777a696974SPotin Lai                             },
16787a696974SPotin Lai                             "xyz.openbmc_project.EntityManager", path,
16797a696974SPotin Lai                             "org.freedesktop.DBus.Properties", "Set", iface,
16807a696974SPotin Lai                             property.first, property.second);
168173df0db0SJames Feist                     }
168273df0db0SJames Feist                 }
168373df0db0SJames Feist                 else
168473df0db0SJames Feist                 {
168573df0db0SJames Feist                     if (chassis.empty())
168673df0db0SJames Feist                     {
168762598e31SEd Tanous                         BMCWEB_LOG_ERROR("Failed to get chassis from config");
1688ace85d60SEd Tanous                         messages::internalError(response->res);
168973df0db0SJames Feist                         return;
169073df0db0SJames Feist                     }
169173df0db0SJames Feist 
169273df0db0SJames Feist                     bool foundChassis = false;
169373df0db0SJames Feist                     for (const auto& obj : managedObj)
169473df0db0SJames Feist                     {
169591f75cafSEd Tanous                         if (obj.first.filename() == chassis)
169673df0db0SJames Feist                         {
169773df0db0SJames Feist                             chassis = obj.first.str;
169873df0db0SJames Feist                             foundChassis = true;
169973df0db0SJames Feist                             break;
170073df0db0SJames Feist                         }
170173df0db0SJames Feist                     }
170273df0db0SJames Feist                     if (!foundChassis)
170373df0db0SJames Feist                     {
170462598e31SEd Tanous                         BMCWEB_LOG_ERROR("Failed to find chassis on dbus");
170573df0db0SJames Feist                         messages::resourceMissingAtURI(
1706ace85d60SEd Tanous                             response->res,
1707ef4c65b7SEd Tanous                             boost::urls::format("/redfish/v1/Chassis/{}",
1708ef4c65b7SEd Tanous                                                 chassis));
170973df0db0SJames Feist                         return;
171073df0db0SJames Feist                     }
171173df0db0SJames Feist 
171273df0db0SJames Feist                     crow::connections::systemBus->async_method_call(
17135e7e2dc5SEd Tanous                         [response](const boost::system::error_code& ec) {
171473df0db0SJames Feist                             if (ec)
171573df0db0SJames Feist                             {
1716bd79bce8SPatrick Williams                                 BMCWEB_LOG_ERROR("Error Adding Pid Object {}",
1717bd79bce8SPatrick Williams                                                  ec);
171873df0db0SJames Feist                                 messages::internalError(response->res);
171973df0db0SJames Feist                                 return;
172073df0db0SJames Feist                             }
172173df0db0SJames Feist                             messages::success(response->res);
172273df0db0SJames Feist                         },
172373df0db0SJames Feist                         "xyz.openbmc_project.EntityManager", chassis,
172473df0db0SJames Feist                         "xyz.openbmc_project.AddObject", "AddObject", output);
172573df0db0SJames Feist                 }
172673df0db0SJames Feist             }
172773df0db0SJames Feist         }
172873df0db0SJames Feist     }
172924b2fe81SEd Tanous 
173024b2fe81SEd Tanous     ~SetPIDValues()
173124b2fe81SEd Tanous     {
173224b2fe81SEd Tanous         try
173324b2fe81SEd Tanous         {
173424b2fe81SEd Tanous             pidSetDone();
173524b2fe81SEd Tanous         }
173624b2fe81SEd Tanous         catch (...)
173724b2fe81SEd Tanous         {
173862598e31SEd Tanous             BMCWEB_LOG_CRITICAL("pidSetDone threw exception");
173924b2fe81SEd Tanous         }
174024b2fe81SEd Tanous     }
174124b2fe81SEd Tanous 
17428d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
17439e9b6049SEd Tanous     std::vector<std::pair<std::string, std::optional<nlohmann::json::object_t>>>
174473df0db0SJames Feist         configuration;
174573df0db0SJames Feist     std::optional<std::string> profile;
174673df0db0SJames Feist     dbus::utility::ManagedObjectType managedObj;
174773df0db0SJames Feist     std::vector<std::string> supportedProfiles;
174873df0db0SJames Feist     std::string currentProfile;
174973df0db0SJames Feist     std::string profileConnection;
175073df0db0SJames Feist     std::string profilePath;
175114b0b8d5SJames Feist     size_t objectCount = 0;
175273df0db0SJames Feist };
175373df0db0SJames Feist 
1754071d8fdfSSunnySrivastava1984 /**
1755071d8fdfSSunnySrivastava1984  * @brief Retrieves BMC manager location data over DBus
1756071d8fdfSSunnySrivastava1984  *
1757ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for completing asynchronous calls
1758071d8fdfSSunnySrivastava1984  * @param[in] connectionName - service name
1759071d8fdfSSunnySrivastava1984  * @param[in] path - object path
1760071d8fdfSSunnySrivastava1984  * @return none
1761071d8fdfSSunnySrivastava1984  */
1762ac106bf6SEd Tanous inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1763071d8fdfSSunnySrivastava1984                         const std::string& connectionName,
1764071d8fdfSSunnySrivastava1984                         const std::string& path)
1765071d8fdfSSunnySrivastava1984 {
176662598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get BMC manager Location data.");
1767071d8fdfSSunnySrivastava1984 
1768deae6a78SEd Tanous     dbus::utility::getProperty<std::string>(
1769deae6a78SEd Tanous         connectionName, path,
17701e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
1771ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
17721e1e598dSJonathan Doman                     const std::string& property) {
1773071d8fdfSSunnySrivastava1984             if (ec)
1774071d8fdfSSunnySrivastava1984             {
177562598e31SEd Tanous                 BMCWEB_LOG_DEBUG("DBUS response error for "
177662598e31SEd Tanous                                  "Location");
1777ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
1778071d8fdfSSunnySrivastava1984                 return;
1779071d8fdfSSunnySrivastava1984             }
1780071d8fdfSSunnySrivastava1984 
1781bd79bce8SPatrick Williams             asyncResp->res
1782bd79bce8SPatrick Williams                 .jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
17831e1e598dSJonathan Doman                 property;
17841e1e598dSJonathan Doman         });
1785071d8fdfSSunnySrivastava1984 }
17867e860f15SJohn Edward Broadbent // avoid name collision systems.hpp
17877e860f15SJohn Edward Broadbent inline void
1788ac106bf6SEd Tanous     managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
17894bf2b033SGunnar Mills {
179062598e31SEd Tanous     BMCWEB_LOG_DEBUG("Getting Manager Last Reset Time");
17914bf2b033SGunnar Mills 
1792deae6a78SEd Tanous     dbus::utility::getProperty<uint64_t>(
1793deae6a78SEd Tanous         "xyz.openbmc_project.State.BMC", "/xyz/openbmc_project/state/bmc0",
1794deae6a78SEd Tanous         "xyz.openbmc_project.State.BMC", "LastRebootTime",
1795ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
17961e1e598dSJonathan Doman                     const uint64_t lastResetTime) {
17974bf2b033SGunnar Mills             if (ec)
17984bf2b033SGunnar Mills             {
179962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("D-BUS response error {}", ec);
18004bf2b033SGunnar Mills                 return;
18014bf2b033SGunnar Mills             }
18024bf2b033SGunnar Mills 
18034bf2b033SGunnar Mills             // LastRebootTime is epoch time, in milliseconds
18044bf2b033SGunnar Mills             // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
18051e1e598dSJonathan Doman             uint64_t lastResetTimeStamp = lastResetTime / 1000;
18064bf2b033SGunnar Mills 
18074bf2b033SGunnar Mills             // Convert to ISO 8601 standard
1808ac106bf6SEd Tanous             asyncResp->res.jsonValue["LastResetTime"] =
18092b82937eSEd Tanous                 redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
18101e1e598dSJonathan Doman         });
18114bf2b033SGunnar Mills }
18124bf2b033SGunnar Mills 
18134bfefa74SGunnar Mills /**
18144bfefa74SGunnar Mills  * @brief Set the running firmware image
18154bfefa74SGunnar Mills  *
1816ac106bf6SEd Tanous  * @param[i,o] asyncResp - Async response object
18174bfefa74SGunnar Mills  * @param[i] runningFirmwareTarget - Image to make the running image
18184bfefa74SGunnar Mills  *
18194bfefa74SGunnar Mills  * @return void
18204bfefa74SGunnar Mills  */
18217e860f15SJohn Edward Broadbent inline void
1822ac106bf6SEd Tanous     setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1823f23b7296SEd Tanous                            const std::string& runningFirmwareTarget)
18244bfefa74SGunnar Mills {
18254bfefa74SGunnar Mills     // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1826f23b7296SEd Tanous     std::string::size_type idPos = runningFirmwareTarget.rfind('/');
18274bfefa74SGunnar Mills     if (idPos == std::string::npos)
18284bfefa74SGunnar Mills     {
1829ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
18304bfefa74SGunnar Mills                                          "@odata.id");
183162598e31SEd Tanous         BMCWEB_LOG_DEBUG("Can't parse firmware ID!");
18324bfefa74SGunnar Mills         return;
18334bfefa74SGunnar Mills     }
18344bfefa74SGunnar Mills     idPos++;
18354bfefa74SGunnar Mills     if (idPos >= runningFirmwareTarget.size())
18364bfefa74SGunnar Mills     {
1837ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
18384bfefa74SGunnar Mills                                          "@odata.id");
183962598e31SEd Tanous         BMCWEB_LOG_DEBUG("Invalid firmware ID.");
18404bfefa74SGunnar Mills         return;
18414bfefa74SGunnar Mills     }
18424bfefa74SGunnar Mills     std::string firmwareId = runningFirmwareTarget.substr(idPos);
18434bfefa74SGunnar Mills 
18444bfefa74SGunnar Mills     // Make sure the image is valid before setting priority
18455eb468daSGeorge Liu     sdbusplus::message::object_path objPath("/xyz/openbmc_project/software");
18465eb468daSGeorge Liu     dbus::utility::getManagedObjects(
1847d27c31e9SJagpal Singh Gill         getBMCUpdateServiceName(), objPath,
18485eb468daSGeorge Liu         [asyncResp, firmwareId, runningFirmwareTarget](
18495eb468daSGeorge Liu             const boost::system::error_code& ec,
18505eb468daSGeorge Liu             const dbus::utility::ManagedObjectType& subtree) {
18514bfefa74SGunnar Mills             if (ec)
18524bfefa74SGunnar Mills             {
185362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("D-Bus response error getting objects.");
1854ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
18554bfefa74SGunnar Mills                 return;
18564bfefa74SGunnar Mills             }
18574bfefa74SGunnar Mills 
185826f6976fSEd Tanous             if (subtree.empty())
18594bfefa74SGunnar Mills             {
186062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Can't find image!");
1861ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
18624bfefa74SGunnar Mills                 return;
18634bfefa74SGunnar Mills             }
18644bfefa74SGunnar Mills 
18654bfefa74SGunnar Mills             bool foundImage = false;
186602cad96eSEd Tanous             for (const auto& object : subtree)
18674bfefa74SGunnar Mills             {
18684bfefa74SGunnar Mills                 const std::string& path =
18694bfefa74SGunnar Mills                     static_cast<const std::string&>(object.first);
1870f23b7296SEd Tanous                 std::size_t idPos2 = path.rfind('/');
18714bfefa74SGunnar Mills 
18724bfefa74SGunnar Mills                 if (idPos2 == std::string::npos)
18734bfefa74SGunnar Mills                 {
18744bfefa74SGunnar Mills                     continue;
18754bfefa74SGunnar Mills                 }
18764bfefa74SGunnar Mills 
18774bfefa74SGunnar Mills                 idPos2++;
18784bfefa74SGunnar Mills                 if (idPos2 >= path.size())
18794bfefa74SGunnar Mills                 {
18804bfefa74SGunnar Mills                     continue;
18814bfefa74SGunnar Mills                 }
18824bfefa74SGunnar Mills 
18834bfefa74SGunnar Mills                 if (path.substr(idPos2) == firmwareId)
18844bfefa74SGunnar Mills                 {
18854bfefa74SGunnar Mills                     foundImage = true;
18864bfefa74SGunnar Mills                     break;
18874bfefa74SGunnar Mills                 }
18884bfefa74SGunnar Mills             }
18894bfefa74SGunnar Mills 
18904bfefa74SGunnar Mills             if (!foundImage)
18914bfefa74SGunnar Mills             {
1892ac106bf6SEd Tanous                 messages::propertyValueNotInList(
1893ac106bf6SEd Tanous                     asyncResp->res, runningFirmwareTarget, "@odata.id");
189462598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Invalid firmware ID.");
18954bfefa74SGunnar Mills                 return;
18964bfefa74SGunnar Mills             }
18974bfefa74SGunnar Mills 
189862598e31SEd Tanous             BMCWEB_LOG_DEBUG("Setting firmware version {} to priority 0.",
189962598e31SEd Tanous                              firmwareId);
19004bfefa74SGunnar Mills 
19014bfefa74SGunnar Mills             // Only support Immediate
19024bfefa74SGunnar Mills             // An addition could be a Redfish Setting like
19034bfefa74SGunnar Mills             // ActiveSoftwareImageApplyTime and support OnReset
19049ae226faSGeorge Liu             sdbusplus::asio::setProperty(
1905d27c31e9SJagpal Singh Gill                 *crow::connections::systemBus, getBMCUpdateServiceName(),
19069ae226faSGeorge Liu                 "/xyz/openbmc_project/software/" + firmwareId,
19079ae226faSGeorge Liu                 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
19089ae226faSGeorge Liu                 static_cast<uint8_t>(0),
1909ac106bf6SEd Tanous                 [asyncResp](const boost::system::error_code& ec2) {
19108a592810SEd Tanous                     if (ec2)
19114bfefa74SGunnar Mills                     {
191262598e31SEd Tanous                         BMCWEB_LOG_DEBUG("D-Bus response error setting.");
1913ac106bf6SEd Tanous                         messages::internalError(asyncResp->res);
19144bfefa74SGunnar Mills                         return;
19154bfefa74SGunnar Mills                     }
1916ac106bf6SEd Tanous                     doBMCGracefulRestart(asyncResp);
19179ae226faSGeorge Liu                 });
19185eb468daSGeorge Liu         });
19194bfefa74SGunnar Mills }
19204bfefa74SGunnar Mills 
1921bd79bce8SPatrick Williams inline void afterSetDateTime(
1922bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1923bd79bce8SPatrick Williams     const boost::system::error_code& ec, const sdbusplus::message_t& msg)
1924c51afd54SEd Tanous {
1925c51afd54SEd Tanous     if (ec)
1926c51afd54SEd Tanous     {
1927c51afd54SEd Tanous         BMCWEB_LOG_DEBUG("Failed to set elapsed time. DBUS response error {}",
1928c51afd54SEd Tanous                          ec);
1929c51afd54SEd Tanous         const sd_bus_error* dbusError = msg.get_error();
1930c51afd54SEd Tanous         if (dbusError != nullptr)
1931c51afd54SEd Tanous         {
1932c51afd54SEd Tanous             std::string_view errorName(dbusError->name);
1933c51afd54SEd Tanous             if (errorName ==
1934c51afd54SEd Tanous                 "org.freedesktop.timedate1.AutomaticTimeSyncEnabled")
1935c51afd54SEd Tanous             {
1936c51afd54SEd Tanous                 BMCWEB_LOG_DEBUG("Setting conflict");
1937c51afd54SEd Tanous                 messages::propertyValueConflict(
1938c51afd54SEd Tanous                     asyncResp->res, "DateTime",
1939c51afd54SEd Tanous                     "Managers/NetworkProtocol/NTPProcotolEnabled");
1940c51afd54SEd Tanous                 return;
1941c51afd54SEd Tanous             }
1942c51afd54SEd Tanous         }
1943c51afd54SEd Tanous         messages::internalError(asyncResp->res);
1944c51afd54SEd Tanous         return;
1945c51afd54SEd Tanous     }
1946c51afd54SEd Tanous     asyncResp->res.result(boost::beast::http::status::no_content);
1947c51afd54SEd Tanous }
1948c51afd54SEd Tanous 
1949c51afd54SEd Tanous inline void setDateTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1950c51afd54SEd Tanous                         const std::string& datetime)
1951af5d6058SSantosh Puranik {
195262598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set date time: {}", datetime);
1953af5d6058SSantosh Puranik 
1954c2e32007SEd Tanous     std::optional<redfish::time_utils::usSinceEpoch> us =
1955c2e32007SEd Tanous         redfish::time_utils::dateStringToEpoch(datetime);
1956c2e32007SEd Tanous     if (!us)
1957af5d6058SSantosh Puranik     {
1958ac106bf6SEd Tanous         messages::propertyValueFormatError(asyncResp->res, datetime,
1959ac106bf6SEd Tanous                                            "DateTime");
1960c2e32007SEd Tanous         return;
1961c2e32007SEd Tanous     }
1962c51afd54SEd Tanous     // Set the absolute datetime
1963c51afd54SEd Tanous     bool relative = false;
1964c51afd54SEd Tanous     bool interactive = false;
1965c51afd54SEd Tanous     crow::connections::systemBus->async_method_call(
1966c51afd54SEd Tanous         [asyncResp](const boost::system::error_code& ec,
1967c51afd54SEd Tanous                     const sdbusplus::message_t& msg) {
1968c51afd54SEd Tanous             afterSetDateTime(asyncResp, ec, msg);
1969c51afd54SEd Tanous         },
1970c51afd54SEd Tanous         "org.freedesktop.timedate1", "/org/freedesktop/timedate1",
1971c51afd54SEd Tanous         "org.freedesktop.timedate1", "SetTime", us->count(), relative,
1972c51afd54SEd Tanous         interactive);
197383ff9ab6SJames Feist }
19749c310685SBorawski.Lukasz 
197575815e5cSEd Tanous inline void
197675815e5cSEd Tanous     checkForQuiesced(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
197775815e5cSEd Tanous {
1978deae6a78SEd Tanous     dbus::utility::getProperty<std::string>(
1979deae6a78SEd Tanous         "org.freedesktop.systemd1",
198075815e5cSEd Tanous         "/org/freedesktop/systemd1/unit/obmc-bmc-service-quiesce@0.target",
198175815e5cSEd Tanous         "org.freedesktop.systemd1.Unit", "ActiveState",
198275815e5cSEd Tanous         [asyncResp](const boost::system::error_code& ec,
198375815e5cSEd Tanous                     const std::string& val) {
198475815e5cSEd Tanous             if (!ec)
198575815e5cSEd Tanous             {
198675815e5cSEd Tanous                 if (val == "active")
198775815e5cSEd Tanous                 {
1988539d8c6bSEd Tanous                     asyncResp->res.jsonValue["Status"]["Health"] =
1989539d8c6bSEd Tanous                         resource::Health::Critical;
1990539d8c6bSEd Tanous                     asyncResp->res.jsonValue["Status"]["State"] =
1991539d8c6bSEd Tanous                         resource::State::Quiesced;
199275815e5cSEd Tanous                     return;
199375815e5cSEd Tanous                 }
199475815e5cSEd Tanous             }
1995539d8c6bSEd Tanous             asyncResp->res.jsonValue["Status"]["Health"] = resource::Health::OK;
1996bd79bce8SPatrick Williams             asyncResp->res.jsonValue["Status"]["State"] =
1997bd79bce8SPatrick Williams                 resource::State::Enabled;
199875815e5cSEd Tanous         });
199975815e5cSEd Tanous }
200075815e5cSEd Tanous 
20017e860f15SJohn Edward Broadbent inline void requestRoutesManager(App& app)
20027e860f15SJohn Edward Broadbent {
20037e860f15SJohn Edward Broadbent     std::string uuid = persistent_data::getConfig().systemUuid;
20049c310685SBorawski.Lukasz 
2005253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/")
2006ed398213SEd Tanous         .privileges(redfish::privileges::getManager)
2007bd79bce8SPatrick Williams         .methods(
2008bd79bce8SPatrick Williams             boost::beast::http::verb::
2009bd79bce8SPatrick Williams                 get)([&app,
2010bd79bce8SPatrick Williams                       uuid](const crow::Request& req,
2011253f11b8SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2012253f11b8SEd Tanous                             const std::string& managerId) {
20133ba00073SCarson Labrado             if (!redfish::setUpRedfishRoute(app, req, asyncResp))
201445ca1b86SEd Tanous             {
201545ca1b86SEd Tanous                 return;
201645ca1b86SEd Tanous             }
2017253f11b8SEd Tanous 
2018253f11b8SEd Tanous             if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2019253f11b8SEd Tanous             {
2020bd79bce8SPatrick Williams                 messages::resourceNotFound(asyncResp->res, "Manager",
2021bd79bce8SPatrick Williams                                            managerId);
2022253f11b8SEd Tanous                 return;
2023253f11b8SEd Tanous             }
2024253f11b8SEd Tanous 
2025253f11b8SEd Tanous             asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2026253f11b8SEd Tanous                 "/redfish/v1/Managers/{}", BMCWEB_REDFISH_MANAGER_URI_NAME);
2027bd79bce8SPatrick Williams             asyncResp->res.jsonValue["@odata.type"] =
2028bd79bce8SPatrick Williams                 "#Manager.v1_14_0.Manager";
2029253f11b8SEd Tanous             asyncResp->res.jsonValue["Id"] = BMCWEB_REDFISH_MANAGER_URI_NAME;
20307e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
20317e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Description"] =
20327e860f15SJohn Edward Broadbent                 "Baseboard Management Controller";
2033539d8c6bSEd Tanous             asyncResp->res.jsonValue["PowerState"] = resource::PowerState::On;
20341476687dSEd Tanous 
2035539d8c6bSEd Tanous             asyncResp->res.jsonValue["ManagerType"] = manager::ManagerType::BMC;
20367e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
20377e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
2038bd79bce8SPatrick Williams             asyncResp->res.jsonValue["Model"] =
2039bd79bce8SPatrick Williams                 "OpenBmc"; // TODO(ed), get model
20407e860f15SJohn Edward Broadbent 
20411476687dSEd Tanous             asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
2042253f11b8SEd Tanous                 boost::urls::format("/redfish/v1/Managers/{}/LogServices",
2043253f11b8SEd Tanous                                     BMCWEB_REDFISH_MANAGER_URI_NAME);
20441476687dSEd Tanous             asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
2045253f11b8SEd Tanous                 boost::urls::format("/redfish/v1/Managers/{}/NetworkProtocol",
2046253f11b8SEd Tanous                                     BMCWEB_REDFISH_MANAGER_URI_NAME);
20471476687dSEd Tanous             asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
2048bd79bce8SPatrick Williams                 boost::urls::format(
2049bd79bce8SPatrick Williams                     "/redfish/v1/Managers/{}/EthernetInterfaces",
2050253f11b8SEd Tanous                     BMCWEB_REDFISH_MANAGER_URI_NAME);
20517e860f15SJohn Edward Broadbent 
205225b54dbaSEd Tanous             if constexpr (BMCWEB_VM_NBDPROXY)
205336c0f2a3SEd Tanous             {
20541476687dSEd Tanous                 asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
2055253f11b8SEd Tanous                     boost::urls::format("/redfish/v1/Managers/{}/VirtualMedia",
2056253f11b8SEd Tanous                                         BMCWEB_REDFISH_MANAGER_URI_NAME);
205736c0f2a3SEd Tanous             }
20587e860f15SJohn Edward Broadbent 
20597e860f15SJohn Edward Broadbent             // default oem data
20607e860f15SJohn Edward Broadbent             nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
20617e860f15SJohn Edward Broadbent             nlohmann::json& oemOpenbmc = oem["OpenBmc"];
2062bd79bce8SPatrick Williams             oem["@odata.id"] =
2063bd79bce8SPatrick Williams                 boost::urls::format("/redfish/v1/Managers/{}#/Oem",
2064253f11b8SEd Tanous                                     BMCWEB_REDFISH_MANAGER_URI_NAME);
2065fc1cdd14SEd Tanous             oemOpenbmc["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager";
2066253f11b8SEd Tanous             oemOpenbmc["@odata.id"] =
2067253f11b8SEd Tanous                 boost::urls::format("/redfish/v1/Managers/{}#/Oem/OpenBmc",
2068253f11b8SEd Tanous                                     BMCWEB_REDFISH_MANAGER_URI_NAME);
20691476687dSEd Tanous 
20701476687dSEd Tanous             nlohmann::json::object_t certificates;
2071253f11b8SEd Tanous             certificates["@odata.id"] = boost::urls::format(
2072253f11b8SEd Tanous                 "/redfish/v1/Managers/{}/Truststore/Certificates",
2073253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME);
20741476687dSEd Tanous             oemOpenbmc["Certificates"] = std::move(certificates);
20757e860f15SJohn Edward Broadbent 
20767e860f15SJohn Edward Broadbent             // Manager.Reset (an action) can be many values, OpenBMC only
20777e860f15SJohn Edward Broadbent             // supports BMC reboot.
20787e860f15SJohn Edward Broadbent             nlohmann::json& managerReset =
20797e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
2080bd79bce8SPatrick Williams             managerReset["target"] = boost::urls::format(
2081bd79bce8SPatrick Williams                 "/redfish/v1/Managers/{}/Actions/Manager.Reset",
2082253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME);
20837e860f15SJohn Edward Broadbent             managerReset["@Redfish.ActionInfo"] =
2084253f11b8SEd Tanous                 boost::urls::format("/redfish/v1/Managers/{}/ResetActionInfo",
2085253f11b8SEd Tanous                                     BMCWEB_REDFISH_MANAGER_URI_NAME);
20867e860f15SJohn Edward Broadbent 
20877e860f15SJohn Edward Broadbent             // ResetToDefaults (Factory Reset) has values like
20887e860f15SJohn Edward Broadbent             // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
20897e860f15SJohn Edward Broadbent             // on OpenBMC
20907e860f15SJohn Edward Broadbent             nlohmann::json& resetToDefaults =
20917e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
2092253f11b8SEd Tanous             resetToDefaults["target"] = boost::urls::format(
2093253f11b8SEd Tanous                 "/redfish/v1/Managers/{}/Actions/Manager.ResetToDefaults",
2094253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME);
2095613dabeaSEd Tanous             resetToDefaults["ResetType@Redfish.AllowableValues"] =
2096613dabeaSEd Tanous                 nlohmann::json::array_t({"ResetAll"});
20977e860f15SJohn Edward Broadbent 
20987c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
20992b82937eSEd Tanous                 redfish::time_utils::getDateTimeOffsetNow();
21007c8c4058STejas Patil 
21017c8c4058STejas Patil             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
21027c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
21037c8c4058STejas Patil                 redfishDateTimeOffset.second;
21047e860f15SJohn Edward Broadbent 
210525b54dbaSEd Tanous             if constexpr (BMCWEB_KVM)
210625b54dbaSEd Tanous             {
21077e860f15SJohn Edward Broadbent                 // Fill in GraphicalConsole info
210825b54dbaSEd Tanous                 asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] =
210925b54dbaSEd Tanous                     true;
211025b54dbaSEd Tanous                 asyncResp->res
211125b54dbaSEd Tanous                     .jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4;
211225b54dbaSEd Tanous                 asyncResp->res
211325b54dbaSEd Tanous                     .jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
2114613dabeaSEd Tanous                     nlohmann::json::array_t({"KVMIP"});
211525b54dbaSEd Tanous             }
211625b54dbaSEd Tanous             if constexpr (!BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
21177f3e84a1SEd Tanous             {
2118bd79bce8SPatrick Williams                 asyncResp->res
2119bd79bce8SPatrick Williams                     .jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
21201476687dSEd Tanous 
21211476687dSEd Tanous                 nlohmann::json::array_t managerForServers;
21221476687dSEd Tanous                 nlohmann::json::object_t manager;
2123bd79bce8SPatrick Williams                 manager["@odata.id"] = std::format(
2124bd79bce8SPatrick Williams                     "/redfish/v1/Systems/{}", BMCWEB_REDFISH_SYSTEM_URI_NAME);
2125ad539545SPatrick Williams                 managerForServers.emplace_back(std::move(manager));
21261476687dSEd Tanous 
21271476687dSEd Tanous                 asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
21281476687dSEd Tanous                     std::move(managerForServers);
21297f3e84a1SEd Tanous             }
21307e860f15SJohn Edward Broadbent 
2131eee0013eSWilly Tu             sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
21327e860f15SJohn Edward Broadbent                                                  "FirmwareVersion", true);
21337e860f15SJohn Edward Broadbent 
21347e860f15SJohn Edward Broadbent             managerGetLastResetTime(asyncResp);
21357e860f15SJohn Edward Broadbent 
2136a51fc2d2SSui Chen             // ManagerDiagnosticData is added for all BMCs.
2137a51fc2d2SSui Chen             nlohmann::json& managerDiagnosticData =
2138a51fc2d2SSui Chen                 asyncResp->res.jsonValue["ManagerDiagnosticData"];
2139bd79bce8SPatrick Williams             managerDiagnosticData["@odata.id"] = boost::urls::format(
2140bd79bce8SPatrick Williams                 "/redfish/v1/Managers/{}/ManagerDiagnosticData",
2141253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME);
2142a51fc2d2SSui Chen 
214325b54dbaSEd Tanous             if constexpr (BMCWEB_REDFISH_OEM_MANAGER_FAN_DATA)
214425b54dbaSEd Tanous             {
21457e860f15SJohn Edward Broadbent                 auto pids = std::make_shared<GetPIDValues>(asyncResp);
21467e860f15SJohn Edward Broadbent                 pids->run();
214725b54dbaSEd Tanous             }
21487e860f15SJohn Edward Broadbent 
2149bd79bce8SPatrick Williams             getMainChassisId(asyncResp, [](const std::string& chassisId,
2150bd79bce8SPatrick Williams                                            const std::shared_ptr<
2151bd79bce8SPatrick Williams                                                bmcweb::AsyncResp>& aRsp) {
2152bd79bce8SPatrick Williams                 aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] =
2153bd79bce8SPatrick Williams                     1;
21541476687dSEd Tanous                 nlohmann::json::array_t managerForChassis;
21558a592810SEd Tanous                 nlohmann::json::object_t managerObj;
2156ef4c65b7SEd Tanous                 boost::urls::url chassiUrl =
2157ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
2158eddfc437SWilly Tu                 managerObj["@odata.id"] = chassiUrl;
2159ad539545SPatrick Williams                 managerForChassis.emplace_back(std::move(managerObj));
21601476687dSEd Tanous                 aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
21611476687dSEd Tanous                     std::move(managerForChassis);
21621476687dSEd Tanous                 aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
2163eddfc437SWilly Tu                     chassiUrl;
21647e860f15SJohn Edward Broadbent             });
21657e860f15SJohn Edward Broadbent 
2166deae6a78SEd Tanous             dbus::utility::getProperty<double>(
2167deae6a78SEd Tanous                 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
2168deae6a78SEd Tanous                 "org.freedesktop.systemd1.Manager", "Progress",
216975815e5cSEd Tanous                 [asyncResp](const boost::system::error_code& ec, double val) {
21707e860f15SJohn Edward Broadbent                     if (ec)
21711abe55efSEd Tanous                     {
217262598e31SEd Tanous                         BMCWEB_LOG_ERROR("Error while getting progress");
21737e860f15SJohn Edward Broadbent                         messages::internalError(asyncResp->res);
21747e860f15SJohn Edward Broadbent                         return;
21757e860f15SJohn Edward Broadbent                     }
21761e1e598dSJonathan Doman                     if (val < 1.0)
21777e860f15SJohn Edward Broadbent                     {
2178539d8c6bSEd Tanous                         asyncResp->res.jsonValue["Status"]["Health"] =
2179539d8c6bSEd Tanous                             resource::Health::OK;
2180539d8c6bSEd Tanous                         asyncResp->res.jsonValue["Status"]["State"] =
2181539d8c6bSEd Tanous                             resource::State::Starting;
218275815e5cSEd Tanous                         return;
21837e860f15SJohn Edward Broadbent                     }
218475815e5cSEd Tanous                     checkForQuiesced(asyncResp);
21851e1e598dSJonathan Doman                 });
21869c310685SBorawski.Lukasz 
2187e99073f5SGeorge Liu             constexpr std::array<std::string_view, 1> interfaces = {
2188e99073f5SGeorge Liu                 "xyz.openbmc_project.Inventory.Item.Bmc"};
2189e99073f5SGeorge Liu             dbus::utility::getSubTree(
2190e99073f5SGeorge Liu                 "/xyz/openbmc_project/inventory", 0, interfaces,
21917e860f15SJohn Edward Broadbent                 [asyncResp](
2192e99073f5SGeorge Liu                     const boost::system::error_code& ec,
2193b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
21947e860f15SJohn Edward Broadbent                     if (ec)
21951abe55efSEd Tanous                     {
2196bd79bce8SPatrick Williams                         BMCWEB_LOG_DEBUG(
2197bd79bce8SPatrick Williams                             "D-Bus response error on GetSubTree {}", ec);
21987e860f15SJohn Edward Broadbent                         return;
21997e860f15SJohn Edward Broadbent                     }
220026f6976fSEd Tanous                     if (subtree.empty())
22017e860f15SJohn Edward Broadbent                     {
220262598e31SEd Tanous                         BMCWEB_LOG_DEBUG("Can't find bmc D-Bus object!");
22037e860f15SJohn Edward Broadbent                         return;
22047e860f15SJohn Edward Broadbent                     }
22057e860f15SJohn Edward Broadbent                     // Assume only 1 bmc D-Bus object
22067e860f15SJohn Edward Broadbent                     // Throw an error if there is more than 1
22077e860f15SJohn Edward Broadbent                     if (subtree.size() > 1)
22087e860f15SJohn Edward Broadbent                     {
220962598e31SEd Tanous                         BMCWEB_LOG_DEBUG("Found more than 1 bmc D-Bus object!");
22107e860f15SJohn Edward Broadbent                         messages::internalError(asyncResp->res);
22117e860f15SJohn Edward Broadbent                         return;
22127e860f15SJohn Edward Broadbent                     }
22137e860f15SJohn Edward Broadbent 
2214bd79bce8SPatrick Williams                     if (subtree[0].first.empty() ||
2215bd79bce8SPatrick Williams                         subtree[0].second.size() != 1)
22167e860f15SJohn Edward Broadbent                     {
221762598e31SEd Tanous                         BMCWEB_LOG_DEBUG("Error getting bmc D-Bus object!");
22187e860f15SJohn Edward Broadbent                         messages::internalError(asyncResp->res);
22197e860f15SJohn Edward Broadbent                         return;
22207e860f15SJohn Edward Broadbent                     }
22217e860f15SJohn Edward Broadbent 
22227e860f15SJohn Edward Broadbent                     const std::string& path = subtree[0].first;
2223bd79bce8SPatrick Williams                     const std::string& connectionName =
2224bd79bce8SPatrick Williams                         subtree[0].second[0].first;
22257e860f15SJohn Edward Broadbent 
2226bd79bce8SPatrick Williams                     for (const auto& interfaceName :
2227bd79bce8SPatrick Williams                          subtree[0].second[0].second)
22287e860f15SJohn Edward Broadbent                     {
22297e860f15SJohn Edward Broadbent                         if (interfaceName ==
22307e860f15SJohn Edward Broadbent                             "xyz.openbmc_project.Inventory.Decorator.Asset")
22317e860f15SJohn Edward Broadbent                         {
2232deae6a78SEd Tanous                             dbus::utility::getAllProperties(
2233bd79bce8SPatrick Williams                                 *crow::connections::systemBus, connectionName,
2234bd79bce8SPatrick Williams                                 path,
2235fac6e53bSKrzysztof Grobelny                                 "xyz.openbmc_project.Inventory.Decorator.Asset",
2236bd79bce8SPatrick Williams                                 [asyncResp](
2237bd79bce8SPatrick Williams                                     const boost::system::error_code& ec2,
2238b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap&
22397e860f15SJohn Edward Broadbent                                         propertiesList) {
22408a592810SEd Tanous                                     if (ec2)
22417e860f15SJohn Edward Broadbent                                     {
2242bd79bce8SPatrick Williams                                         BMCWEB_LOG_DEBUG(
2243bd79bce8SPatrick Williams                                             "Can't get bmc asset!");
22447e860f15SJohn Edward Broadbent                                         return;
22457e860f15SJohn Edward Broadbent                                     }
22467e860f15SJohn Edward Broadbent 
2247fac6e53bSKrzysztof Grobelny                                     const std::string* partNumber = nullptr;
2248fac6e53bSKrzysztof Grobelny                                     const std::string* serialNumber = nullptr;
2249fac6e53bSKrzysztof Grobelny                                     const std::string* manufacturer = nullptr;
2250fac6e53bSKrzysztof Grobelny                                     const std::string* model = nullptr;
2251bd79bce8SPatrick Williams                                     const std::string* sparePartNumber =
2252bd79bce8SPatrick Williams                                         nullptr;
2253fac6e53bSKrzysztof Grobelny 
2254bd79bce8SPatrick Williams                                     const bool success =
2255bd79bce8SPatrick Williams                                         sdbusplus::unpackPropertiesNoThrow(
2256bd79bce8SPatrick Williams                                             dbus_utils::UnpackErrorPrinter(),
2257bd79bce8SPatrick Williams                                             propertiesList, "PartNumber",
2258bd79bce8SPatrick Williams                                             partNumber, "SerialNumber",
2259bd79bce8SPatrick Williams                                             serialNumber, "Manufacturer",
2260bd79bce8SPatrick Williams                                             manufacturer, "Model", model,
2261bd79bce8SPatrick Williams                                             "SparePartNumber", sparePartNumber);
2262fac6e53bSKrzysztof Grobelny 
2263fac6e53bSKrzysztof Grobelny                                     if (!success)
22647e860f15SJohn Edward Broadbent                                     {
2265002d39b4SEd Tanous                                         messages::internalError(asyncResp->res);
22667e860f15SJohn Edward Broadbent                                         return;
22677e860f15SJohn Edward Broadbent                                     }
2268fac6e53bSKrzysztof Grobelny 
2269fac6e53bSKrzysztof Grobelny                                     if (partNumber != nullptr)
2270fac6e53bSKrzysztof Grobelny                                     {
2271fac6e53bSKrzysztof Grobelny                                         asyncResp->res.jsonValue["PartNumber"] =
2272fac6e53bSKrzysztof Grobelny                                             *partNumber;
22737e860f15SJohn Edward Broadbent                                     }
2274fac6e53bSKrzysztof Grobelny 
2275fac6e53bSKrzysztof Grobelny                                     if (serialNumber != nullptr)
2276fac6e53bSKrzysztof Grobelny                                     {
2277bd79bce8SPatrick Williams                                         asyncResp->res
2278bd79bce8SPatrick Williams                                             .jsonValue["SerialNumber"] =
2279fac6e53bSKrzysztof Grobelny                                             *serialNumber;
22807e860f15SJohn Edward Broadbent                                     }
2281fac6e53bSKrzysztof Grobelny 
2282fac6e53bSKrzysztof Grobelny                                     if (manufacturer != nullptr)
2283fac6e53bSKrzysztof Grobelny                                     {
2284bd79bce8SPatrick Williams                                         asyncResp->res
2285bd79bce8SPatrick Williams                                             .jsonValue["Manufacturer"] =
2286fac6e53bSKrzysztof Grobelny                                             *manufacturer;
2287fac6e53bSKrzysztof Grobelny                                     }
2288fac6e53bSKrzysztof Grobelny 
2289fac6e53bSKrzysztof Grobelny                                     if (model != nullptr)
2290fac6e53bSKrzysztof Grobelny                                     {
2291bd79bce8SPatrick Williams                                         asyncResp->res.jsonValue["Model"] =
2292bd79bce8SPatrick Williams                                             *model;
2293fac6e53bSKrzysztof Grobelny                                     }
2294fac6e53bSKrzysztof Grobelny 
2295fac6e53bSKrzysztof Grobelny                                     if (sparePartNumber != nullptr)
2296fac6e53bSKrzysztof Grobelny                                     {
2297bd79bce8SPatrick Williams                                         asyncResp->res
2298bd79bce8SPatrick Williams                                             .jsonValue["SparePartNumber"] =
2299fac6e53bSKrzysztof Grobelny                                             *sparePartNumber;
2300fac6e53bSKrzysztof Grobelny                                     }
2301fac6e53bSKrzysztof Grobelny                                 });
23027e860f15SJohn Edward Broadbent                         }
2303bd79bce8SPatrick Williams                         else if (
2304bd79bce8SPatrick Williams                             interfaceName ==
23050fda0f12SGeorge Liu                             "xyz.openbmc_project.Inventory.Decorator.LocationCode")
23067e860f15SJohn Edward Broadbent                         {
23077e860f15SJohn Edward Broadbent                             getLocation(asyncResp, connectionName, path);
23087e860f15SJohn Edward Broadbent                         }
23097e860f15SJohn Edward Broadbent                     }
2310e99073f5SGeorge Liu                 });
23117e860f15SJohn Edward Broadbent         });
23127e860f15SJohn Edward Broadbent 
2313253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/")
2314ed398213SEd Tanous         .privileges(redfish::privileges::patchManager)
231545ca1b86SEd Tanous         .methods(boost::beast::http::verb::patch)(
231645ca1b86SEd Tanous             [&app](const crow::Request& req,
2317253f11b8SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2318253f11b8SEd Tanous                    const std::string& managerId) {
23193ba00073SCarson Labrado                 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
232045ca1b86SEd Tanous                 {
232145ca1b86SEd Tanous                     return;
232245ca1b86SEd Tanous                 }
2323253f11b8SEd Tanous 
2324253f11b8SEd Tanous                 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2325253f11b8SEd Tanous                 {
2326bd79bce8SPatrick Williams                     messages::resourceNotFound(asyncResp->res, "Manager",
2327bd79bce8SPatrick Williams                                                managerId);
2328253f11b8SEd Tanous                     return;
2329253f11b8SEd Tanous                 }
2330253f11b8SEd Tanous 
23319e9b6049SEd Tanous                 std::optional<std::string> activeSoftwareImageOdataId;
23327e860f15SJohn Edward Broadbent                 std::optional<std::string> datetime;
23339e9b6049SEd Tanous                 std::optional<nlohmann::json::object_t> pidControllers;
23349e9b6049SEd Tanous                 std::optional<nlohmann::json::object_t> fanControllers;
23359e9b6049SEd Tanous                 std::optional<nlohmann::json::object_t> fanZones;
23369e9b6049SEd Tanous                 std::optional<nlohmann::json::object_t> stepwiseControllers;
23379e9b6049SEd Tanous                 std::optional<std::string> profile;
23387e860f15SJohn Edward Broadbent 
2339afc474aeSMyung Bae                 if (!json_util::readJsonPatch( //
2340afc474aeSMyung Bae                         req, asyncResp->res, //
2341afc474aeSMyung Bae                         "DateTime", datetime, //
2342afc474aeSMyung Bae                         "Links/ActiveSoftwareImage/@odata.id",
2343afc474aeSMyung Bae                         activeSoftwareImageOdataId, //
2344afc474aeSMyung Bae                         "Oem/OpenBmc/Fan/FanControllers", fanControllers, //
2345afc474aeSMyung Bae                         "Oem/OpenBmc/Fan/FanZones", fanZones, //
2346afc474aeSMyung Bae                         "Oem/OpenBmc/Fan/PidControllers", pidControllers, //
2347afc474aeSMyung Bae                         "Oem/OpenBmc/Fan/Profile", profile, //
2348afc474aeSMyung Bae                         "Oem/OpenBmc/Fan/StepwiseControllers",
2349afc474aeSMyung Bae                         stepwiseControllers //
23509e9b6049SEd Tanous                         ))
23517e860f15SJohn Edward Broadbent                 {
23527e860f15SJohn Edward Broadbent                     return;
23537e860f15SJohn Edward Broadbent                 }
23547e860f15SJohn Edward Broadbent 
23559e9b6049SEd Tanous                 if (pidControllers || fanControllers || fanZones ||
23569e9b6049SEd Tanous                     stepwiseControllers || profile)
23577e860f15SJohn Edward Broadbent                 {
235825b54dbaSEd Tanous                     if constexpr (BMCWEB_REDFISH_OEM_MANAGER_FAN_DATA)
235925b54dbaSEd Tanous                     {
2360bd79bce8SPatrick Williams                         std::vector<
2361bd79bce8SPatrick Williams                             std::pair<std::string,
236225b54dbaSEd Tanous                                       std::optional<nlohmann::json::object_t>>>
23639e9b6049SEd Tanous                             configuration;
23649e9b6049SEd Tanous                         if (pidControllers)
23657e860f15SJohn Edward Broadbent                         {
2366bd79bce8SPatrick Williams                             configuration.emplace_back(
2367bd79bce8SPatrick Williams                                 "PidControllers", std::move(pidControllers));
23687e860f15SJohn Edward Broadbent                         }
23699e9b6049SEd Tanous                         if (fanControllers)
23707e860f15SJohn Edward Broadbent                         {
2371bd79bce8SPatrick Williams                             configuration.emplace_back(
2372bd79bce8SPatrick Williams                                 "FanControllers", std::move(fanControllers));
23737e860f15SJohn Edward Broadbent                         }
23749e9b6049SEd Tanous                         if (fanZones)
23757e860f15SJohn Edward Broadbent                         {
2376bd79bce8SPatrick Williams                             configuration.emplace_back("FanZones",
2377bd79bce8SPatrick Williams                                                        std::move(fanZones));
23789e9b6049SEd Tanous                         }
23799e9b6049SEd Tanous                         if (stepwiseControllers)
23809e9b6049SEd Tanous                         {
2381bd79bce8SPatrick Williams                             configuration.emplace_back(
2382bd79bce8SPatrick Williams                                 "StepwiseControllers",
23839e9b6049SEd Tanous                                 std::move(stepwiseControllers));
23849e9b6049SEd Tanous                         }
23859e9b6049SEd Tanous                         auto pid = std::make_shared<SetPIDValues>(
23869e9b6049SEd Tanous                             asyncResp, std::move(configuration), profile);
23877e860f15SJohn Edward Broadbent                         pid->run();
238825b54dbaSEd Tanous                     }
238925b54dbaSEd Tanous                     else
239025b54dbaSEd Tanous                     {
239154dce7f5SGunnar Mills                         messages::propertyUnknown(asyncResp->res, "Oem");
239254dce7f5SGunnar Mills                         return;
239325b54dbaSEd Tanous                     }
23947e860f15SJohn Edward Broadbent                 }
23959e9b6049SEd Tanous 
23969e9b6049SEd Tanous                 if (activeSoftwareImageOdataId)
23977e860f15SJohn Edward Broadbent                 {
2398bd79bce8SPatrick Williams                     setActiveFirmwareImage(asyncResp,
2399bd79bce8SPatrick Williams                                            *activeSoftwareImageOdataId);
24007e860f15SJohn Edward Broadbent                 }
24017e860f15SJohn Edward Broadbent 
24027e860f15SJohn Edward Broadbent                 if (datetime)
24037e860f15SJohn Edward Broadbent                 {
2404c51afd54SEd Tanous                     setDateTime(asyncResp, *datetime);
24057e860f15SJohn Edward Broadbent                 }
24067e860f15SJohn Edward Broadbent             });
24077e860f15SJohn Edward Broadbent }
24087e860f15SJohn Edward Broadbent 
24097e860f15SJohn Edward Broadbent inline void requestRoutesManagerCollection(App& app)
24107e860f15SJohn Edward Broadbent {
24117e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
2412ed398213SEd Tanous         .privileges(redfish::privileges::getManagerCollection)
24137e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
241445ca1b86SEd Tanous             [&app](const crow::Request& req,
24157e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
24163ba00073SCarson Labrado                 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
241745ca1b86SEd Tanous                 {
241845ca1b86SEd Tanous                     return;
241945ca1b86SEd Tanous                 }
242083ff9ab6SJames Feist                 // Collections don't include the static data added by SubRoute
242183ff9ab6SJames Feist                 // because it has a duplicate entry for members
24228d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
24238d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
24248d1b46d7Szhanghch05                     "#ManagerCollection.ManagerCollection";
24258d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "Manager Collection";
24268d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Members@odata.count"] = 1;
24271476687dSEd Tanous                 nlohmann::json::array_t members;
24281476687dSEd Tanous                 nlohmann::json& bmc = members.emplace_back();
2429bd79bce8SPatrick Williams                 bmc["@odata.id"] = boost::urls::format(
2430bd79bce8SPatrick Williams                     "/redfish/v1/Managers/{}", BMCWEB_REDFISH_MANAGER_URI_NAME);
24311476687dSEd Tanous                 asyncResp->res.jsonValue["Members"] = std::move(members);
24327e860f15SJohn Edward Broadbent             });
24339c310685SBorawski.Lukasz }
24349c310685SBorawski.Lukasz } // namespace redfish
2435