xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision 539d8c6bc399e516d39bcaa18262cf206f4c1035)
19c310685SBorawski.Lukasz /*
29c310685SBorawski.Lukasz // Copyright (c) 2018 Intel Corporation
39c310685SBorawski.Lukasz //
49c310685SBorawski.Lukasz // Licensed under the Apache License, Version 2.0 (the "License");
59c310685SBorawski.Lukasz // you may not use this file except in compliance with the License.
69c310685SBorawski.Lukasz // You may obtain a copy of the License at
79c310685SBorawski.Lukasz //
89c310685SBorawski.Lukasz //      http://www.apache.org/licenses/LICENSE-2.0
99c310685SBorawski.Lukasz //
109c310685SBorawski.Lukasz // Unless required by applicable law or agreed to in writing, software
119c310685SBorawski.Lukasz // distributed under the License is distributed on an "AS IS" BASIS,
129c310685SBorawski.Lukasz // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139c310685SBorawski.Lukasz // See the License for the specific language governing permissions and
149c310685SBorawski.Lukasz // limitations under the License.
159c310685SBorawski.Lukasz */
169c310685SBorawski.Lukasz #pragma once
179c310685SBorawski.Lukasz 
1813451e39SWilly Tu #include "bmcweb_config.h"
1913451e39SWilly Tu 
20a51fc2d2SSui Chen #include "app.hpp"
21a51fc2d2SSui Chen #include "dbus_utility.hpp"
22*539d8c6bSEd Tanous #include "generated/enums/action_info.hpp"
23*539d8c6bSEd Tanous #include "generated/enums/manager.hpp"
24*539d8c6bSEd Tanous #include "generated/enums/resource.hpp"
25a51fc2d2SSui Chen #include "query.hpp"
26c5d03ff4SJennifer Lee #include "redfish_util.hpp"
27a51fc2d2SSui Chen #include "registries/privilege_registry.hpp"
28fac6e53bSKrzysztof Grobelny #include "utils/dbus_utils.hpp"
293ccb3adbSEd Tanous #include "utils/json_utils.hpp"
30a51fc2d2SSui Chen #include "utils/sw_utils.hpp"
31a51fc2d2SSui Chen #include "utils/systemd_utils.hpp"
322b82937eSEd Tanous #include "utils/time_utils.hpp"
339c310685SBorawski.Lukasz 
34e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
35ef4c65b7SEd Tanous #include <boost/url/format.hpp>
36fac6e53bSKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
37fac6e53bSKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
381214b7e7SGunnar Mills 
39a170f275SEd Tanous #include <algorithm>
40e99073f5SGeorge Liu #include <array>
414bfefa74SGunnar Mills #include <cstdint>
421214b7e7SGunnar Mills #include <memory>
439970e93fSKonstantin Aladyshev #include <optional>
443544d2a7SEd Tanous #include <ranges>
451214b7e7SGunnar Mills #include <sstream>
469970e93fSKonstantin Aladyshev #include <string>
47e99073f5SGeorge Liu #include <string_view>
48abf2add6SEd Tanous #include <variant>
495b4aa86bSJames Feist 
501abe55efSEd Tanous namespace redfish
511abe55efSEd Tanous {
52ed5befbdSJennifer Lee 
53ed5befbdSJennifer Lee /**
542a5c4407SGunnar Mills  * Function reboots the BMC.
552a5c4407SGunnar Mills  *
562a5c4407SGunnar Mills  * @param[in] asyncResp - Shared pointer for completing asynchronous calls
57ed5befbdSJennifer Lee  */
588d1b46d7Szhanghch05 inline void
598d1b46d7Szhanghch05     doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
60ed5befbdSJennifer Lee {
61ed5befbdSJennifer Lee     const char* processName = "xyz.openbmc_project.State.BMC";
62ed5befbdSJennifer Lee     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
63ed5befbdSJennifer Lee     const char* interfaceName = "xyz.openbmc_project.State.BMC";
64ed5befbdSJennifer Lee     const std::string& propertyValue =
65ed5befbdSJennifer Lee         "xyz.openbmc_project.State.BMC.Transition.Reboot";
66ed5befbdSJennifer Lee     const char* destProperty = "RequestedBMCTransition";
67ed5befbdSJennifer Lee 
68ed5befbdSJennifer Lee     // Create the D-Bus variant for D-Bus call.
699ae226faSGeorge Liu     sdbusplus::asio::setProperty(
709ae226faSGeorge Liu         *crow::connections::systemBus, processName, objectPath, interfaceName,
719ae226faSGeorge Liu         destProperty, propertyValue,
725e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
73ed5befbdSJennifer Lee         // Use "Set" method to set the property value.
74ed5befbdSJennifer Lee         if (ec)
75ed5befbdSJennifer Lee         {
7662598e31SEd Tanous             BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
77ed5befbdSJennifer Lee             messages::internalError(asyncResp->res);
78ed5befbdSJennifer Lee             return;
79ed5befbdSJennifer Lee         }
80ed5befbdSJennifer Lee 
81ed5befbdSJennifer Lee         messages::success(asyncResp->res);
829ae226faSGeorge Liu     });
83ed5befbdSJennifer Lee }
842a5c4407SGunnar Mills 
858d1b46d7Szhanghch05 inline void
868d1b46d7Szhanghch05     doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
87f92af389SJayaprakash Mutyala {
88f92af389SJayaprakash Mutyala     const char* processName = "xyz.openbmc_project.State.BMC";
89f92af389SJayaprakash Mutyala     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
90f92af389SJayaprakash Mutyala     const char* interfaceName = "xyz.openbmc_project.State.BMC";
91f92af389SJayaprakash Mutyala     const std::string& propertyValue =
92f92af389SJayaprakash Mutyala         "xyz.openbmc_project.State.BMC.Transition.HardReboot";
93f92af389SJayaprakash Mutyala     const char* destProperty = "RequestedBMCTransition";
94f92af389SJayaprakash Mutyala 
95f92af389SJayaprakash Mutyala     // Create the D-Bus variant for D-Bus call.
969ae226faSGeorge Liu     sdbusplus::asio::setProperty(
979ae226faSGeorge Liu         *crow::connections::systemBus, processName, objectPath, interfaceName,
989ae226faSGeorge Liu         destProperty, propertyValue,
995e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
100f92af389SJayaprakash Mutyala         // Use "Set" method to set the property value.
101f92af389SJayaprakash Mutyala         if (ec)
102f92af389SJayaprakash Mutyala         {
10362598e31SEd Tanous             BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
104f92af389SJayaprakash Mutyala             messages::internalError(asyncResp->res);
105f92af389SJayaprakash Mutyala             return;
106f92af389SJayaprakash Mutyala         }
107f92af389SJayaprakash Mutyala 
108f92af389SJayaprakash Mutyala         messages::success(asyncResp->res);
1099ae226faSGeorge Liu     });
110f92af389SJayaprakash Mutyala }
111f92af389SJayaprakash Mutyala 
1122a5c4407SGunnar Mills /**
1132a5c4407SGunnar Mills  * ManagerResetAction class supports the POST method for the Reset (reboot)
1142a5c4407SGunnar Mills  * action.
1152a5c4407SGunnar Mills  */
1167e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetAction(App& app)
1172a5c4407SGunnar Mills {
1182a5c4407SGunnar Mills     /**
1192a5c4407SGunnar Mills      * Function handles POST method request.
1202a5c4407SGunnar Mills      * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
121f92af389SJayaprakash Mutyala      * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
1222a5c4407SGunnar Mills      */
1237e860f15SJohn Edward Broadbent 
124253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/Actions/Manager.Reset/")
125ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
1267e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
12745ca1b86SEd Tanous             [&app](const crow::Request& req,
128253f11b8SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
129253f11b8SEd Tanous                    const std::string& managerId) {
1303ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
13145ca1b86SEd Tanous         {
13245ca1b86SEd Tanous             return;
13345ca1b86SEd Tanous         }
134253f11b8SEd Tanous         if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
135253f11b8SEd Tanous         {
136253f11b8SEd Tanous             messages::resourceNotFound(asyncResp->res, "Manager", managerId);
137253f11b8SEd Tanous             return;
138253f11b8SEd Tanous         }
139253f11b8SEd Tanous 
14062598e31SEd Tanous         BMCWEB_LOG_DEBUG("Post Manager Reset.");
1412a5c4407SGunnar Mills 
1422a5c4407SGunnar Mills         std::string resetType;
1432a5c4407SGunnar Mills 
14415ed6780SWilly Tu         if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
1457e860f15SJohn Edward Broadbent                                        resetType))
1462a5c4407SGunnar Mills         {
1472a5c4407SGunnar Mills             return;
1482a5c4407SGunnar Mills         }
1492a5c4407SGunnar Mills 
150f92af389SJayaprakash Mutyala         if (resetType == "GracefulRestart")
151f92af389SJayaprakash Mutyala         {
15262598e31SEd Tanous             BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
153f92af389SJayaprakash Mutyala             doBMCGracefulRestart(asyncResp);
154f92af389SJayaprakash Mutyala             return;
155f92af389SJayaprakash Mutyala         }
1563174e4dfSEd Tanous         if (resetType == "ForceRestart")
157f92af389SJayaprakash Mutyala         {
15862598e31SEd Tanous             BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
159f92af389SJayaprakash Mutyala             doBMCForceRestart(asyncResp);
160f92af389SJayaprakash Mutyala             return;
161f92af389SJayaprakash Mutyala         }
16262598e31SEd Tanous         BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}", resetType);
1632a5c4407SGunnar Mills         messages::actionParameterNotSupported(asyncResp->res, resetType,
1642a5c4407SGunnar Mills                                               "ResetType");
1652a5c4407SGunnar Mills 
1662a5c4407SGunnar Mills         return;
1677e860f15SJohn Edward Broadbent     });
1682a5c4407SGunnar Mills }
169ed5befbdSJennifer Lee 
1703e40fc74SGunnar Mills /**
1713e40fc74SGunnar Mills  * ManagerResetToDefaultsAction class supports POST method for factory reset
1723e40fc74SGunnar Mills  * action.
1733e40fc74SGunnar Mills  */
1747e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetToDefaultsAction(App& app)
1753e40fc74SGunnar Mills {
1763e40fc74SGunnar Mills     /**
1773e40fc74SGunnar Mills      * Function handles ResetToDefaults POST method request.
1783e40fc74SGunnar Mills      *
1793e40fc74SGunnar Mills      * Analyzes POST body message and factory resets BMC by calling
1803e40fc74SGunnar Mills      * BMC code updater factory reset followed by a BMC reboot.
1813e40fc74SGunnar Mills      *
1823e40fc74SGunnar Mills      * BMC code updater factory reset wipes the whole BMC read-write
1833e40fc74SGunnar Mills      * filesystem which includes things like the network settings.
1843e40fc74SGunnar Mills      *
1853e40fc74SGunnar Mills      * OpenBMC only supports ResetToDefaultsType "ResetAll".
1863e40fc74SGunnar Mills      */
1877e860f15SJohn Edward Broadbent 
1887e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
189253f11b8SEd Tanous                  "/redfish/v1/Managers/<str>/Actions/Manager.ResetToDefaults/")
190ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
1917e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
19245ca1b86SEd Tanous             [&app](const crow::Request& req,
193253f11b8SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
194253f11b8SEd Tanous                    const std::string& managerId) {
1953ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
19645ca1b86SEd Tanous         {
19745ca1b86SEd Tanous             return;
19845ca1b86SEd Tanous         }
199253f11b8SEd Tanous 
200253f11b8SEd Tanous         if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
201253f11b8SEd Tanous         {
202253f11b8SEd Tanous             messages::resourceNotFound(asyncResp->res, "Manager", managerId);
203253f11b8SEd Tanous             return;
204253f11b8SEd Tanous         }
205253f11b8SEd Tanous 
20662598e31SEd Tanous         BMCWEB_LOG_DEBUG("Post ResetToDefaults.");
2073e40fc74SGunnar Mills 
2089970e93fSKonstantin Aladyshev         std::optional<std::string> resetType;
2099970e93fSKonstantin Aladyshev         std::optional<std::string> resetToDefaultsType;
2103e40fc74SGunnar Mills 
2119970e93fSKonstantin Aladyshev         if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
2129970e93fSKonstantin Aladyshev                                        resetType, "ResetToDefaultsType",
2139970e93fSKonstantin Aladyshev                                        resetToDefaultsType))
2143e40fc74SGunnar Mills         {
2159970e93fSKonstantin Aladyshev             BMCWEB_LOG_DEBUG("Missing property ResetType.");
2163e40fc74SGunnar Mills 
217002d39b4SEd Tanous             messages::actionParameterMissing(asyncResp->res, "ResetToDefaults",
2189970e93fSKonstantin Aladyshev                                              "ResetType");
2193e40fc74SGunnar Mills             return;
2203e40fc74SGunnar Mills         }
2213e40fc74SGunnar Mills 
2229970e93fSKonstantin Aladyshev         if (resetToDefaultsType && !resetType)
2239970e93fSKonstantin Aladyshev         {
2249970e93fSKonstantin Aladyshev             BMCWEB_LOG_WARNING(
2259970e93fSKonstantin Aladyshev                 "Using deprecated ResetToDefaultsType, should be ResetType."
2269970e93fSKonstantin Aladyshev                 "Support for the ResetToDefaultsType will be dropped in 2Q24");
2279970e93fSKonstantin Aladyshev             resetType = resetToDefaultsType;
2289970e93fSKonstantin Aladyshev         }
2299970e93fSKonstantin Aladyshev 
2303e40fc74SGunnar Mills         if (resetType != "ResetAll")
2313e40fc74SGunnar Mills         {
2329970e93fSKonstantin Aladyshev             BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}",
2339970e93fSKonstantin Aladyshev                              *resetType);
2349970e93fSKonstantin Aladyshev             messages::actionParameterNotSupported(asyncResp->res, *resetType,
2359970e93fSKonstantin Aladyshev                                                   "ResetType");
2363e40fc74SGunnar Mills             return;
2373e40fc74SGunnar Mills         }
2383e40fc74SGunnar Mills 
2393e40fc74SGunnar Mills         crow::connections::systemBus->async_method_call(
2405e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
2413e40fc74SGunnar Mills             if (ec)
2423e40fc74SGunnar Mills             {
24362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Failed to ResetToDefaults: {}", ec);
2443e40fc74SGunnar Mills                 messages::internalError(asyncResp->res);
2453e40fc74SGunnar Mills                 return;
2463e40fc74SGunnar Mills             }
2473e40fc74SGunnar Mills             // Factory Reset doesn't actually happen until a reboot
2483e40fc74SGunnar Mills             // Can't erase what the BMC is running on
2493e40fc74SGunnar Mills             doBMCGracefulRestart(asyncResp);
2503e40fc74SGunnar Mills         },
2513e40fc74SGunnar Mills             "xyz.openbmc_project.Software.BMC.Updater",
2523e40fc74SGunnar Mills             "/xyz/openbmc_project/software",
2533e40fc74SGunnar Mills             "xyz.openbmc_project.Common.FactoryReset", "Reset");
2547e860f15SJohn Edward Broadbent     });
2553e40fc74SGunnar Mills }
2563e40fc74SGunnar Mills 
2571cb1a9e6SAppaRao Puli /**
2581cb1a9e6SAppaRao Puli  * ManagerResetActionInfo derived class for delivering Manager
2591cb1a9e6SAppaRao Puli  * ResetType AllowableValues using ResetInfo schema.
2601cb1a9e6SAppaRao Puli  */
2617e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetActionInfo(App& app)
2621cb1a9e6SAppaRao Puli {
2631cb1a9e6SAppaRao Puli     /**
2641cb1a9e6SAppaRao Puli      * Functions triggers appropriate requests on DBus
2651cb1a9e6SAppaRao Puli      */
2667e860f15SJohn Edward Broadbent 
267253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/ResetActionInfo/")
268ed398213SEd Tanous         .privileges(redfish::privileges::getActionInfo)
2697e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
27045ca1b86SEd Tanous             [&app](const crow::Request& req,
271253f11b8SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
272253f11b8SEd Tanous                    const std::string& managerId) {
2733ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
27445ca1b86SEd Tanous         {
27545ca1b86SEd Tanous             return;
27645ca1b86SEd Tanous         }
2771476687dSEd Tanous 
278253f11b8SEd Tanous         if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
279253f11b8SEd Tanous         {
280253f11b8SEd Tanous             messages::resourceNotFound(asyncResp->res, "Manager", managerId);
281253f11b8SEd Tanous             return;
282253f11b8SEd Tanous         }
283253f11b8SEd Tanous 
2841476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
2851476687dSEd Tanous             "#ActionInfo.v1_1_2.ActionInfo";
2861476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
287253f11b8SEd Tanous             boost::urls::format("/redfish/v1/Managers/{}/ResetActionInfo",
288253f11b8SEd Tanous                                 BMCWEB_REDFISH_MANAGER_URI_NAME);
2891476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "Reset Action Info";
2901476687dSEd Tanous         asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
2911476687dSEd Tanous         nlohmann::json::object_t parameter;
2921476687dSEd Tanous         parameter["Name"] = "ResetType";
2931476687dSEd Tanous         parameter["Required"] = true;
294*539d8c6bSEd Tanous         parameter["DataType"] = action_info::ParameterTypes::String;
2951476687dSEd Tanous 
2961476687dSEd Tanous         nlohmann::json::array_t allowableValues;
297ad539545SPatrick Williams         allowableValues.emplace_back("GracefulRestart");
298ad539545SPatrick Williams         allowableValues.emplace_back("ForceRestart");
2991476687dSEd Tanous         parameter["AllowableValues"] = std::move(allowableValues);
3001476687dSEd Tanous 
3011476687dSEd Tanous         nlohmann::json::array_t parameters;
302ad539545SPatrick Williams         parameters.emplace_back(std::move(parameter));
3031476687dSEd Tanous 
3041476687dSEd Tanous         asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
3057e860f15SJohn Edward Broadbent     });
3061cb1a9e6SAppaRao Puli }
3071cb1a9e6SAppaRao Puli 
3085b4aa86bSJames Feist static constexpr const char* objectManagerIface =
3095b4aa86bSJames Feist     "org.freedesktop.DBus.ObjectManager";
3105b4aa86bSJames Feist static constexpr const char* pidConfigurationIface =
3115b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid";
3125b4aa86bSJames Feist static constexpr const char* pidZoneConfigurationIface =
3135b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid.Zone";
314b7a08d04SJames Feist static constexpr const char* stepwiseConfigurationIface =
315b7a08d04SJames Feist     "xyz.openbmc_project.Configuration.Stepwise";
31673df0db0SJames Feist static constexpr const char* thermalModeIface =
31773df0db0SJames Feist     "xyz.openbmc_project.Control.ThermalMode";
3189c310685SBorawski.Lukasz 
3198d1b46d7Szhanghch05 inline void
3208d1b46d7Szhanghch05     asyncPopulatePid(const std::string& connection, const std::string& path,
32173df0db0SJames Feist                      const std::string& currentProfile,
32273df0db0SJames Feist                      const std::vector<std::string>& supportedProfiles,
3238d1b46d7Szhanghch05                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
3245b4aa86bSJames Feist {
3255eb468daSGeorge Liu     sdbusplus::message::object_path objPath(path);
3265eb468daSGeorge Liu     dbus::utility::getManagedObjects(
3275eb468daSGeorge Liu         connection, objPath,
32873df0db0SJames Feist         [asyncResp, currentProfile, supportedProfiles](
3295e7e2dc5SEd Tanous             const boost::system::error_code& ec,
3305b4aa86bSJames Feist             const dbus::utility::ManagedObjectType& managedObj) {
3315b4aa86bSJames Feist         if (ec)
3325b4aa86bSJames Feist         {
33362598e31SEd Tanous             BMCWEB_LOG_ERROR("{}", ec);
334f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
3355b4aa86bSJames Feist             return;
3365b4aa86bSJames Feist         }
3375b4aa86bSJames Feist         nlohmann::json& configRoot =
3385b4aa86bSJames Feist             asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
3395b4aa86bSJames Feist         nlohmann::json& fans = configRoot["FanControllers"];
340fc1cdd14SEd Tanous         fans["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager.FanControllers";
341253f11b8SEd Tanous         fans["@odata.id"] = boost::urls::format(
342253f11b8SEd Tanous             "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/FanControllers",
343253f11b8SEd Tanous             BMCWEB_REDFISH_MANAGER_URI_NAME);
3445b4aa86bSJames Feist 
3455b4aa86bSJames Feist         nlohmann::json& pids = configRoot["PidControllers"];
346fc1cdd14SEd Tanous         pids["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager.PidControllers";
347253f11b8SEd Tanous         pids["@odata.id"] = boost::urls::format(
348253f11b8SEd Tanous             "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/PidControllers",
349253f11b8SEd Tanous             BMCWEB_REDFISH_MANAGER_URI_NAME);
3505b4aa86bSJames Feist 
351b7a08d04SJames Feist         nlohmann::json& stepwise = configRoot["StepwiseControllers"];
352fc1cdd14SEd Tanous         stepwise["@odata.type"] =
353fc1cdd14SEd Tanous             "#OpenBMCManager.v1_0_0.Manager.StepwiseControllers";
354253f11b8SEd Tanous         stepwise["@odata.id"] = boost::urls::format(
355253f11b8SEd Tanous             "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/StepwiseControllers",
356253f11b8SEd Tanous             BMCWEB_REDFISH_MANAGER_URI_NAME);
357b7a08d04SJames Feist 
3585b4aa86bSJames Feist         nlohmann::json& zones = configRoot["FanZones"];
359253f11b8SEd Tanous         zones["@odata.id"] = boost::urls::format(
360253f11b8SEd Tanous             "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/FanZones",
361253f11b8SEd Tanous             BMCWEB_REDFISH_MANAGER_URI_NAME);
362fc1cdd14SEd Tanous         zones["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager.FanZones";
363253f11b8SEd Tanous         configRoot["@odata.id"] =
364253f11b8SEd Tanous             boost::urls::format("/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan",
365253f11b8SEd Tanous                                 BMCWEB_REDFISH_MANAGER_URI_NAME);
366fc1cdd14SEd Tanous         configRoot["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager.Fan";
36773df0db0SJames Feist         configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
36873df0db0SJames Feist 
36973df0db0SJames Feist         if (!currentProfile.empty())
37073df0db0SJames Feist         {
37173df0db0SJames Feist             configRoot["Profile"] = currentProfile;
37273df0db0SJames Feist         }
373bf2ddedeSCarson Labrado         BMCWEB_LOG_DEBUG("profile = {} !", currentProfile);
3745b4aa86bSJames Feist 
3755b4aa86bSJames Feist         for (const auto& pathPair : managedObj)
3765b4aa86bSJames Feist         {
3775b4aa86bSJames Feist             for (const auto& intfPair : pathPair.second)
3785b4aa86bSJames Feist             {
3795b4aa86bSJames Feist                 if (intfPair.first != pidConfigurationIface &&
380b7a08d04SJames Feist                     intfPair.first != pidZoneConfigurationIface &&
381b7a08d04SJames Feist                     intfPair.first != stepwiseConfigurationIface)
3825b4aa86bSJames Feist                 {
3835b4aa86bSJames Feist                     continue;
3845b4aa86bSJames Feist                 }
38573df0db0SJames Feist 
386711ac7a9SEd Tanous                 std::string name;
387711ac7a9SEd Tanous 
388711ac7a9SEd Tanous                 for (const std::pair<std::string,
389002d39b4SEd Tanous                                      dbus::utility::DbusVariantType>& propPair :
390002d39b4SEd Tanous                      intfPair.second)
391711ac7a9SEd Tanous                 {
392711ac7a9SEd Tanous                     if (propPair.first == "Name")
393711ac7a9SEd Tanous                     {
3945b4aa86bSJames Feist                         const std::string* namePtr =
395711ac7a9SEd Tanous                             std::get_if<std::string>(&propPair.second);
3965b4aa86bSJames Feist                         if (namePtr == nullptr)
3975b4aa86bSJames Feist                         {
39862598e31SEd Tanous                             BMCWEB_LOG_ERROR("Pid Name Field illegal");
399b7a08d04SJames Feist                             messages::internalError(asyncResp->res);
4005b4aa86bSJames Feist                             return;
4015b4aa86bSJames Feist                         }
402db697703SWilly Tu                         name = *namePtr;
4035b4aa86bSJames Feist                         dbus::utility::escapePathForDbus(name);
404711ac7a9SEd Tanous                     }
405711ac7a9SEd Tanous                     else if (propPair.first == "Profiles")
40673df0db0SJames Feist                     {
40773df0db0SJames Feist                         const std::vector<std::string>* profiles =
40873df0db0SJames Feist                             std::get_if<std::vector<std::string>>(
409711ac7a9SEd Tanous                                 &propPair.second);
41073df0db0SJames Feist                         if (profiles == nullptr)
41173df0db0SJames Feist                         {
41262598e31SEd Tanous                             BMCWEB_LOG_ERROR("Pid Profiles Field illegal");
41373df0db0SJames Feist                             messages::internalError(asyncResp->res);
41473df0db0SJames Feist                             return;
41573df0db0SJames Feist                         }
41673df0db0SJames Feist                         if (std::find(profiles->begin(), profiles->end(),
41773df0db0SJames Feist                                       currentProfile) == profiles->end())
41873df0db0SJames Feist                         {
41962598e31SEd Tanous                             BMCWEB_LOG_INFO(
42062598e31SEd Tanous                                 "{} not supported in current profile", name);
42173df0db0SJames Feist                             continue;
42273df0db0SJames Feist                         }
42373df0db0SJames Feist                     }
424711ac7a9SEd Tanous                 }
425b7a08d04SJames Feist                 nlohmann::json* config = nullptr;
426c33a90ecSJames Feist                 const std::string* classPtr = nullptr;
427711ac7a9SEd Tanous 
428711ac7a9SEd Tanous                 for (const std::pair<std::string,
429002d39b4SEd Tanous                                      dbus::utility::DbusVariantType>& propPair :
430002d39b4SEd Tanous                      intfPair.second)
431c33a90ecSJames Feist                 {
432727dc83fSLei YU                     if (propPair.first == "Class")
433711ac7a9SEd Tanous                     {
434002d39b4SEd Tanous                         classPtr = std::get_if<std::string>(&propPair.second);
435711ac7a9SEd Tanous                     }
436c33a90ecSJames Feist                 }
437c33a90ecSJames Feist 
438253f11b8SEd Tanous                 boost::urls::url url(
439253f11b8SEd Tanous                     boost::urls::format("/redfish/v1/Managers/{}",
440253f11b8SEd Tanous                                         BMCWEB_REDFISH_MANAGER_URI_NAME));
4415b4aa86bSJames Feist                 if (intfPair.first == pidZoneConfigurationIface)
4425b4aa86bSJames Feist                 {
4435b4aa86bSJames Feist                     std::string chassis;
444002d39b4SEd Tanous                     if (!dbus::utility::getNthStringFromPath(pathPair.first.str,
445002d39b4SEd Tanous                                                              5, chassis))
4465b4aa86bSJames Feist                     {
4475b4aa86bSJames Feist                         chassis = "#IllegalValue";
4485b4aa86bSJames Feist                     }
4495b4aa86bSJames Feist                     nlohmann::json& zone = zones[name];
450ef4c65b7SEd Tanous                     zone["Chassis"]["@odata.id"] =
451ef4c65b7SEd Tanous                         boost::urls::format("/redfish/v1/Chassis/{}", chassis);
452eddfc437SWilly Tu                     url.set_fragment(
453eddfc437SWilly Tu                         ("/Oem/OpenBmc/Fan/FanZones"_json_pointer / name)
454eddfc437SWilly Tu                             .to_string());
455eddfc437SWilly Tu                     zone["@odata.id"] = std::move(url);
456fc1cdd14SEd Tanous                     zone["@odata.type"] =
457fc1cdd14SEd Tanous                         "#OpenBMCManager.v1_0_0.Manager.FanZone";
458b7a08d04SJames Feist                     config = &zone;
4595b4aa86bSJames Feist                 }
4605b4aa86bSJames Feist 
461b7a08d04SJames Feist                 else if (intfPair.first == stepwiseConfigurationIface)
4625b4aa86bSJames Feist                 {
463c33a90ecSJames Feist                     if (classPtr == nullptr)
464c33a90ecSJames Feist                     {
46562598e31SEd Tanous                         BMCWEB_LOG_ERROR("Pid Class Field illegal");
466c33a90ecSJames Feist                         messages::internalError(asyncResp->res);
467c33a90ecSJames Feist                         return;
468c33a90ecSJames Feist                     }
469c33a90ecSJames Feist 
470b7a08d04SJames Feist                     nlohmann::json& controller = stepwise[name];
471b7a08d04SJames Feist                     config = &controller;
472eddfc437SWilly Tu                     url.set_fragment(
473eddfc437SWilly Tu                         ("/Oem/OpenBmc/Fan/StepwiseControllers"_json_pointer /
474eddfc437SWilly Tu                          name)
475eddfc437SWilly Tu                             .to_string());
476eddfc437SWilly Tu                     controller["@odata.id"] = std::move(url);
477b7a08d04SJames Feist                     controller["@odata.type"] =
478fc1cdd14SEd Tanous                         "#OpenBMCManager.v1_0_0.Manager.StepwiseController";
479b7a08d04SJames Feist 
480c33a90ecSJames Feist                     controller["Direction"] = *classPtr;
4815b4aa86bSJames Feist                 }
4825b4aa86bSJames Feist 
4835b4aa86bSJames Feist                 // pid and fans are off the same configuration
484b7a08d04SJames Feist                 else if (intfPair.first == pidConfigurationIface)
4855b4aa86bSJames Feist                 {
4865b4aa86bSJames Feist                     if (classPtr == nullptr)
4875b4aa86bSJames Feist                     {
48862598e31SEd Tanous                         BMCWEB_LOG_ERROR("Pid Class Field illegal");
489a08b46ccSJason M. Bills                         messages::internalError(asyncResp->res);
4905b4aa86bSJames Feist                         return;
4915b4aa86bSJames Feist                     }
4925b4aa86bSJames Feist                     bool isFan = *classPtr == "fan";
493002d39b4SEd Tanous                     nlohmann::json& element = isFan ? fans[name] : pids[name];
494b7a08d04SJames Feist                     config = &element;
4955b4aa86bSJames Feist                     if (isFan)
4965b4aa86bSJames Feist                     {
497eddfc437SWilly Tu                         url.set_fragment(
498eddfc437SWilly Tu                             ("/Oem/OpenBmc/Fan/FanControllers"_json_pointer /
499eddfc437SWilly Tu                              name)
500eddfc437SWilly Tu                                 .to_string());
501eddfc437SWilly Tu                         element["@odata.id"] = std::move(url);
502fc1cdd14SEd Tanous                         element["@odata.type"] =
503fc1cdd14SEd Tanous                             "#OpenBMCManager.v1_0_0.Manager.FanController";
5045b4aa86bSJames Feist                     }
5055b4aa86bSJames Feist                     else
5065b4aa86bSJames Feist                     {
507eddfc437SWilly Tu                         url.set_fragment(
508eddfc437SWilly Tu                             ("/Oem/OpenBmc/Fan/PidControllers"_json_pointer /
509eddfc437SWilly Tu                              name)
510eddfc437SWilly Tu                                 .to_string());
511eddfc437SWilly Tu                         element["@odata.id"] = std::move(url);
512fc1cdd14SEd Tanous                         element["@odata.type"] =
513fc1cdd14SEd Tanous                             "#OpenBMCManager.v1_0_0.Manager.PidController";
5145b4aa86bSJames Feist                     }
515b7a08d04SJames Feist                 }
516b7a08d04SJames Feist                 else
517b7a08d04SJames Feist                 {
51862598e31SEd Tanous                     BMCWEB_LOG_ERROR("Unexpected configuration");
519b7a08d04SJames Feist                     messages::internalError(asyncResp->res);
520b7a08d04SJames Feist                     return;
521b7a08d04SJames Feist                 }
522b7a08d04SJames Feist 
523b7a08d04SJames Feist                 // used for making maps out of 2 vectors
524b7a08d04SJames Feist                 const std::vector<double>* keys = nullptr;
525b7a08d04SJames Feist                 const std::vector<double>* values = nullptr;
526b7a08d04SJames Feist 
527b7a08d04SJames Feist                 for (const auto& propertyPair : intfPair.second)
528b7a08d04SJames Feist                 {
529b7a08d04SJames Feist                     if (propertyPair.first == "Type" ||
530b7a08d04SJames Feist                         propertyPair.first == "Class" ||
531b7a08d04SJames Feist                         propertyPair.first == "Name")
532b7a08d04SJames Feist                     {
533b7a08d04SJames Feist                         continue;
534b7a08d04SJames Feist                     }
535b7a08d04SJames Feist 
536b7a08d04SJames Feist                     // zones
537b7a08d04SJames Feist                     if (intfPair.first == pidZoneConfigurationIface)
538b7a08d04SJames Feist                     {
539b7a08d04SJames Feist                         const double* ptr =
540abf2add6SEd Tanous                             std::get_if<double>(&propertyPair.second);
541b7a08d04SJames Feist                         if (ptr == nullptr)
542b7a08d04SJames Feist                         {
54362598e31SEd Tanous                             BMCWEB_LOG_ERROR("Field Illegal {}",
54462598e31SEd Tanous                                              propertyPair.first);
545b7a08d04SJames Feist                             messages::internalError(asyncResp->res);
546b7a08d04SJames Feist                             return;
547b7a08d04SJames Feist                         }
548b7a08d04SJames Feist                         (*config)[propertyPair.first] = *ptr;
549b7a08d04SJames Feist                     }
550b7a08d04SJames Feist 
551b7a08d04SJames Feist                     if (intfPair.first == stepwiseConfigurationIface)
552b7a08d04SJames Feist                     {
553b7a08d04SJames Feist                         if (propertyPair.first == "Reading" ||
554b7a08d04SJames Feist                             propertyPair.first == "Output")
555b7a08d04SJames Feist                         {
556b7a08d04SJames Feist                             const std::vector<double>* ptr =
557abf2add6SEd Tanous                                 std::get_if<std::vector<double>>(
558b7a08d04SJames Feist                                     &propertyPair.second);
559b7a08d04SJames Feist 
560b7a08d04SJames Feist                             if (ptr == nullptr)
561b7a08d04SJames Feist                             {
56262598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Field Illegal {}",
56362598e31SEd Tanous                                                  propertyPair.first);
564b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
565b7a08d04SJames Feist                                 return;
566b7a08d04SJames Feist                             }
567b7a08d04SJames Feist 
568b7a08d04SJames Feist                             if (propertyPair.first == "Reading")
569b7a08d04SJames Feist                             {
570b7a08d04SJames Feist                                 keys = ptr;
571b7a08d04SJames Feist                             }
572b7a08d04SJames Feist                             else
573b7a08d04SJames Feist                             {
574b7a08d04SJames Feist                                 values = ptr;
575b7a08d04SJames Feist                             }
576e662eae8SEd Tanous                             if (keys != nullptr && values != nullptr)
577b7a08d04SJames Feist                             {
578b7a08d04SJames Feist                                 if (keys->size() != values->size())
579b7a08d04SJames Feist                                 {
58062598e31SEd Tanous                                     BMCWEB_LOG_ERROR(
58162598e31SEd Tanous                                         "Reading and Output size don't match ");
582b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
583b7a08d04SJames Feist                                     return;
584b7a08d04SJames Feist                                 }
585b7a08d04SJames Feist                                 nlohmann::json& steps = (*config)["Steps"];
586b7a08d04SJames Feist                                 steps = nlohmann::json::array();
587b7a08d04SJames Feist                                 for (size_t ii = 0; ii < keys->size(); ii++)
588b7a08d04SJames Feist                                 {
5891476687dSEd Tanous                                     nlohmann::json::object_t step;
5901476687dSEd Tanous                                     step["Target"] = (*keys)[ii];
5911476687dSEd Tanous                                     step["Output"] = (*values)[ii];
592b2ba3072SPatrick Williams                                     steps.emplace_back(std::move(step));
593b7a08d04SJames Feist                                 }
594b7a08d04SJames Feist                             }
595b7a08d04SJames Feist                         }
596b7a08d04SJames Feist                         if (propertyPair.first == "NegativeHysteresis" ||
597b7a08d04SJames Feist                             propertyPair.first == "PositiveHysteresis")
598b7a08d04SJames Feist                         {
599b7a08d04SJames Feist                             const double* ptr =
600abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
601b7a08d04SJames Feist                             if (ptr == nullptr)
602b7a08d04SJames Feist                             {
60362598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Field Illegal {}",
60462598e31SEd Tanous                                                  propertyPair.first);
605b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
606b7a08d04SJames Feist                                 return;
607b7a08d04SJames Feist                             }
608b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
609b7a08d04SJames Feist                         }
610b7a08d04SJames Feist                     }
611b7a08d04SJames Feist 
612b7a08d04SJames Feist                     // pid and fans are off the same configuration
613b7a08d04SJames Feist                     if (intfPair.first == pidConfigurationIface ||
614b7a08d04SJames Feist                         intfPair.first == stepwiseConfigurationIface)
615b7a08d04SJames Feist                     {
6165b4aa86bSJames Feist                         if (propertyPair.first == "Zones")
6175b4aa86bSJames Feist                         {
6185b4aa86bSJames Feist                             const std::vector<std::string>* inputs =
619abf2add6SEd Tanous                                 std::get_if<std::vector<std::string>>(
6201b6b96c5SEd Tanous                                     &propertyPair.second);
6215b4aa86bSJames Feist 
6225b4aa86bSJames Feist                             if (inputs == nullptr)
6235b4aa86bSJames Feist                             {
62462598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Zones Pid Field Illegal");
625a08b46ccSJason M. Bills                                 messages::internalError(asyncResp->res);
6265b4aa86bSJames Feist                                 return;
6275b4aa86bSJames Feist                             }
628b7a08d04SJames Feist                             auto& data = (*config)[propertyPair.first];
6295b4aa86bSJames Feist                             data = nlohmann::json::array();
6305b4aa86bSJames Feist                             for (std::string itemCopy : *inputs)
6315b4aa86bSJames Feist                             {
6325b4aa86bSJames Feist                                 dbus::utility::escapePathForDbus(itemCopy);
6331476687dSEd Tanous                                 nlohmann::json::object_t input;
634ef4c65b7SEd Tanous                                 boost::urls::url managerUrl = boost::urls::format(
635253f11b8SEd Tanous                                     "/redfish/v1/Managers/{}#{}",
636253f11b8SEd Tanous                                     BMCWEB_REDFISH_MANAGER_URI_NAME,
637eddfc437SWilly Tu                                     ("/Oem/OpenBmc/Fan/FanZones"_json_pointer /
638eddfc437SWilly Tu                                      itemCopy)
639eddfc437SWilly Tu                                         .to_string());
640eddfc437SWilly Tu                                 input["@odata.id"] = std::move(managerUrl);
641b2ba3072SPatrick Williams                                 data.emplace_back(std::move(input));
6425b4aa86bSJames Feist                             }
6435b4aa86bSJames Feist                         }
6445b4aa86bSJames Feist                         // todo(james): may never happen, but this
6455b4aa86bSJames Feist                         // assumes configuration data referenced in the
6465b4aa86bSJames Feist                         // PID config is provided by the same daemon, we
6475b4aa86bSJames Feist                         // could add another loop to cover all cases,
6485b4aa86bSJames Feist                         // but I'm okay kicking this can down the road a
6495b4aa86bSJames Feist                         // bit
6505b4aa86bSJames Feist 
6515b4aa86bSJames Feist                         else if (propertyPair.first == "Inputs" ||
6525b4aa86bSJames Feist                                  propertyPair.first == "Outputs")
6535b4aa86bSJames Feist                         {
654b7a08d04SJames Feist                             auto& data = (*config)[propertyPair.first];
6555b4aa86bSJames Feist                             const std::vector<std::string>* inputs =
656abf2add6SEd Tanous                                 std::get_if<std::vector<std::string>>(
6571b6b96c5SEd Tanous                                     &propertyPair.second);
6585b4aa86bSJames Feist 
6595b4aa86bSJames Feist                             if (inputs == nullptr)
6605b4aa86bSJames Feist                             {
66162598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Field Illegal {}",
66262598e31SEd Tanous                                                  propertyPair.first);
663f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
6645b4aa86bSJames Feist                                 return;
6655b4aa86bSJames Feist                             }
6665b4aa86bSJames Feist                             data = *inputs;
667b943aaefSJames Feist                         }
668b943aaefSJames Feist                         else if (propertyPair.first == "SetPointOffset")
669b943aaefSJames Feist                         {
670b943aaefSJames Feist                             const std::string* ptr =
671002d39b4SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
672b943aaefSJames Feist 
673b943aaefSJames Feist                             if (ptr == nullptr)
674b943aaefSJames Feist                             {
67562598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Field Illegal {}",
67662598e31SEd Tanous                                                  propertyPair.first);
677b943aaefSJames Feist                                 messages::internalError(asyncResp->res);
678b943aaefSJames Feist                                 return;
679b943aaefSJames Feist                             }
680b943aaefSJames Feist                             // translate from dbus to redfish
681b943aaefSJames Feist                             if (*ptr == "WarningHigh")
682b943aaefSJames Feist                             {
683b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
684b943aaefSJames Feist                                     "UpperThresholdNonCritical";
685b943aaefSJames Feist                             }
686b943aaefSJames Feist                             else if (*ptr == "WarningLow")
687b943aaefSJames Feist                             {
688b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
689b943aaefSJames Feist                                     "LowerThresholdNonCritical";
690b943aaefSJames Feist                             }
691b943aaefSJames Feist                             else if (*ptr == "CriticalHigh")
692b943aaefSJames Feist                             {
693b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
694b943aaefSJames Feist                                     "UpperThresholdCritical";
695b943aaefSJames Feist                             }
696b943aaefSJames Feist                             else if (*ptr == "CriticalLow")
697b943aaefSJames Feist                             {
698b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
699b943aaefSJames Feist                                     "LowerThresholdCritical";
700b943aaefSJames Feist                             }
701b943aaefSJames Feist                             else
702b943aaefSJames Feist                             {
70362598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Value Illegal {}", *ptr);
704b943aaefSJames Feist                                 messages::internalError(asyncResp->res);
705b943aaefSJames Feist                                 return;
706b943aaefSJames Feist                             }
707b943aaefSJames Feist                         }
708b943aaefSJames Feist                         // doubles
709002d39b4SEd Tanous                         else if (propertyPair.first == "FFGainCoefficient" ||
7105b4aa86bSJames Feist                                  propertyPair.first == "FFOffCoefficient" ||
7115b4aa86bSJames Feist                                  propertyPair.first == "ICoefficient" ||
7125b4aa86bSJames Feist                                  propertyPair.first == "ILimitMax" ||
7135b4aa86bSJames Feist                                  propertyPair.first == "ILimitMin" ||
714002d39b4SEd Tanous                                  propertyPair.first == "PositiveHysteresis" ||
715002d39b4SEd Tanous                                  propertyPair.first == "NegativeHysteresis" ||
7165b4aa86bSJames Feist                                  propertyPair.first == "OutLimitMax" ||
7175b4aa86bSJames Feist                                  propertyPair.first == "OutLimitMin" ||
7185b4aa86bSJames Feist                                  propertyPair.first == "PCoefficient" ||
7197625cb81SJames Feist                                  propertyPair.first == "SetPoint" ||
7205b4aa86bSJames Feist                                  propertyPair.first == "SlewNeg" ||
7215b4aa86bSJames Feist                                  propertyPair.first == "SlewPos")
7225b4aa86bSJames Feist                         {
7235b4aa86bSJames Feist                             const double* ptr =
724abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
7255b4aa86bSJames Feist                             if (ptr == nullptr)
7265b4aa86bSJames Feist                             {
72762598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Field Illegal {}",
72862598e31SEd Tanous                                                  propertyPair.first);
729f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
7305b4aa86bSJames Feist                                 return;
7315b4aa86bSJames Feist                             }
732b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
7335b4aa86bSJames Feist                         }
7345b4aa86bSJames Feist                     }
7355b4aa86bSJames Feist                 }
7365b4aa86bSJames Feist             }
7375b4aa86bSJames Feist         }
7385eb468daSGeorge Liu     });
7395b4aa86bSJames Feist }
740ca537928SJennifer Lee 
74183ff9ab6SJames Feist enum class CreatePIDRet
74283ff9ab6SJames Feist {
74383ff9ab6SJames Feist     fail,
74483ff9ab6SJames Feist     del,
74583ff9ab6SJames Feist     patch
74683ff9ab6SJames Feist };
74783ff9ab6SJames Feist 
7488d1b46d7Szhanghch05 inline bool
7498d1b46d7Szhanghch05     getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
7509e9b6049SEd Tanous                         std::vector<nlohmann::json::object_t>& config,
7515f2caaefSJames Feist                         std::vector<std::string>& zones)
7525f2caaefSJames Feist {
753b6baeaa4SJames Feist     if (config.empty())
754b6baeaa4SJames Feist     {
75562598e31SEd Tanous         BMCWEB_LOG_ERROR("Empty Zones");
756f818b04dSEd Tanous         messages::propertyValueFormatError(response->res, config, "Zones");
757b6baeaa4SJames Feist         return false;
758b6baeaa4SJames Feist     }
7595f2caaefSJames Feist     for (auto& odata : config)
7605f2caaefSJames Feist     {
7615f2caaefSJames Feist         std::string path;
7629e9b6049SEd Tanous         if (!redfish::json_util::readJsonObject(odata, response->res,
7639e9b6049SEd Tanous                                                 "@odata.id", path))
7645f2caaefSJames Feist         {
7655f2caaefSJames Feist             return false;
7665f2caaefSJames Feist         }
7675f2caaefSJames Feist         std::string input;
76861adbda3SJames Feist 
76961adbda3SJames Feist         // 8 below comes from
77061adbda3SJames Feist         // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
77161adbda3SJames Feist         //     0    1     2      3    4    5      6     7      8
77261adbda3SJames Feist         if (!dbus::utility::getNthStringFromPath(path, 8, input))
7735f2caaefSJames Feist         {
77462598e31SEd Tanous             BMCWEB_LOG_ERROR("Got invalid path {}", path);
77562598e31SEd Tanous             BMCWEB_LOG_ERROR("Illegal Type Zones");
776f818b04dSEd Tanous             messages::propertyValueFormatError(response->res, odata, "Zones");
7775f2caaefSJames Feist             return false;
7785f2caaefSJames Feist         }
779a170f275SEd Tanous         std::replace(input.begin(), input.end(), '_', ' ');
7805f2caaefSJames Feist         zones.emplace_back(std::move(input));
7815f2caaefSJames Feist     }
7825f2caaefSJames Feist     return true;
7835f2caaefSJames Feist }
7845f2caaefSJames Feist 
785711ac7a9SEd Tanous inline const dbus::utility::ManagedObjectType::value_type*
78673df0db0SJames Feist     findChassis(const dbus::utility::ManagedObjectType& managedObj,
7879e9b6049SEd Tanous                 std::string_view value, std::string& chassis)
788b6baeaa4SJames Feist {
78962598e31SEd Tanous     BMCWEB_LOG_DEBUG("Find Chassis: {}", value);
790b6baeaa4SJames Feist 
7919e9b6049SEd Tanous     std::string escaped(value);
7926ce82fabSYaswanth Reddy M     std::replace(escaped.begin(), escaped.end(), ' ', '_');
793b6baeaa4SJames Feist     escaped = "/" + escaped;
7943544d2a7SEd Tanous     auto it = std::ranges::find_if(managedObj, [&escaped](const auto& obj) {
79518f8f608SEd Tanous         if (obj.first.str.ends_with(escaped))
796b6baeaa4SJames Feist         {
79762598e31SEd Tanous             BMCWEB_LOG_DEBUG("Matched {}", obj.first.str);
798b6baeaa4SJames Feist             return true;
799b6baeaa4SJames Feist         }
800b6baeaa4SJames Feist         return false;
801b6baeaa4SJames Feist     });
802b6baeaa4SJames Feist 
803b6baeaa4SJames Feist     if (it == managedObj.end())
804b6baeaa4SJames Feist     {
80573df0db0SJames Feist         return nullptr;
806b6baeaa4SJames Feist     }
807b6baeaa4SJames Feist     // 5 comes from <chassis-name> being the 5th element
808b6baeaa4SJames Feist     // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
80973df0db0SJames Feist     if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
81073df0db0SJames Feist     {
81173df0db0SJames Feist         return &(*it);
81273df0db0SJames Feist     }
81373df0db0SJames Feist 
81473df0db0SJames Feist     return nullptr;
815b6baeaa4SJames Feist }
816b6baeaa4SJames Feist 
81723a21a1cSEd Tanous inline CreatePIDRet createPidInterface(
8188d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
8199e9b6049SEd Tanous     std::string_view name, nlohmann::json& jsonValue, const std::string& path,
82083ff9ab6SJames Feist     const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
821b9d36b47SEd Tanous     dbus::utility::DBusPropertiesMap& output, std::string& chassis,
822b9d36b47SEd Tanous     const std::string& profile)
82383ff9ab6SJames Feist {
8245f2caaefSJames Feist     // common deleter
8259e9b6049SEd Tanous     if (jsonValue == nullptr)
8265f2caaefSJames Feist     {
8275f2caaefSJames Feist         std::string iface;
8285f2caaefSJames Feist         if (type == "PidControllers" || type == "FanControllers")
8295f2caaefSJames Feist         {
8305f2caaefSJames Feist             iface = pidConfigurationIface;
8315f2caaefSJames Feist         }
8325f2caaefSJames Feist         else if (type == "FanZones")
8335f2caaefSJames Feist         {
8345f2caaefSJames Feist             iface = pidZoneConfigurationIface;
8355f2caaefSJames Feist         }
8365f2caaefSJames Feist         else if (type == "StepwiseControllers")
8375f2caaefSJames Feist         {
8385f2caaefSJames Feist             iface = stepwiseConfigurationIface;
8395f2caaefSJames Feist         }
8405f2caaefSJames Feist         else
8415f2caaefSJames Feist         {
84262598e31SEd Tanous             BMCWEB_LOG_ERROR("Illegal Type {}", type);
8435f2caaefSJames Feist             messages::propertyUnknown(response->res, type);
8445f2caaefSJames Feist             return CreatePIDRet::fail;
8455f2caaefSJames Feist         }
8466ee7f774SJames Feist 
84762598e31SEd Tanous         BMCWEB_LOG_DEBUG("del {} {}", path, iface);
8485f2caaefSJames Feist         // delete interface
8495f2caaefSJames Feist         crow::connections::systemBus->async_method_call(
8505e7e2dc5SEd Tanous             [response, path](const boost::system::error_code& ec) {
8515f2caaefSJames Feist             if (ec)
8525f2caaefSJames Feist             {
85362598e31SEd Tanous                 BMCWEB_LOG_ERROR("Error patching {}: {}", path, ec);
8545f2caaefSJames Feist                 messages::internalError(response->res);
855b6baeaa4SJames Feist                 return;
8565f2caaefSJames Feist             }
857b6baeaa4SJames Feist             messages::success(response->res);
8585f2caaefSJames Feist         },
8595f2caaefSJames Feist             "xyz.openbmc_project.EntityManager", path, iface, "Delete");
8605f2caaefSJames Feist         return CreatePIDRet::del;
8615f2caaefSJames Feist     }
8625f2caaefSJames Feist 
863711ac7a9SEd Tanous     const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
864b6baeaa4SJames Feist     if (!createNewObject)
865b6baeaa4SJames Feist     {
866b6baeaa4SJames Feist         // if we aren't creating a new object, we should be able to find it on
867b6baeaa4SJames Feist         // d-bus
8689e9b6049SEd Tanous         managedItem = findChassis(managedObj, name, chassis);
86973df0db0SJames Feist         if (managedItem == nullptr)
870b6baeaa4SJames Feist         {
87162598e31SEd Tanous             BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
872ef4c65b7SEd Tanous             messages::invalidObject(
873ef4c65b7SEd Tanous                 response->res,
874ef4c65b7SEd Tanous                 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
875b6baeaa4SJames Feist             return CreatePIDRet::fail;
876b6baeaa4SJames Feist         }
877b6baeaa4SJames Feist     }
878b6baeaa4SJames Feist 
87926f6976fSEd Tanous     if (!profile.empty() &&
88073df0db0SJames Feist         (type == "PidControllers" || type == "FanControllers" ||
88173df0db0SJames Feist          type == "StepwiseControllers"))
88273df0db0SJames Feist     {
88373df0db0SJames Feist         if (managedItem == nullptr)
88473df0db0SJames Feist         {
885b9d36b47SEd Tanous             output.emplace_back("Profiles", std::vector<std::string>{profile});
88673df0db0SJames Feist         }
88773df0db0SJames Feist         else
88873df0db0SJames Feist         {
88973df0db0SJames Feist             std::string interface;
89073df0db0SJames Feist             if (type == "StepwiseControllers")
89173df0db0SJames Feist             {
89273df0db0SJames Feist                 interface = stepwiseConfigurationIface;
89373df0db0SJames Feist             }
89473df0db0SJames Feist             else
89573df0db0SJames Feist             {
89673df0db0SJames Feist                 interface = pidConfigurationIface;
89773df0db0SJames Feist             }
898711ac7a9SEd Tanous             bool ifaceFound = false;
899711ac7a9SEd Tanous             for (const auto& iface : managedItem->second)
900711ac7a9SEd Tanous             {
901711ac7a9SEd Tanous                 if (iface.first == interface)
902711ac7a9SEd Tanous                 {
903711ac7a9SEd Tanous                     ifaceFound = true;
904711ac7a9SEd Tanous                     for (const auto& prop : iface.second)
905711ac7a9SEd Tanous                     {
906711ac7a9SEd Tanous                         if (prop.first == "Profiles")
907711ac7a9SEd Tanous                         {
908711ac7a9SEd Tanous                             const std::vector<std::string>* curProfiles =
909711ac7a9SEd Tanous                                 std::get_if<std::vector<std::string>>(
910711ac7a9SEd Tanous                                     &(prop.second));
911711ac7a9SEd Tanous                             if (curProfiles == nullptr)
912711ac7a9SEd Tanous                             {
91362598e31SEd Tanous                                 BMCWEB_LOG_ERROR(
91462598e31SEd Tanous                                     "Illegal profiles in managed object");
915711ac7a9SEd Tanous                                 messages::internalError(response->res);
916711ac7a9SEd Tanous                                 return CreatePIDRet::fail;
917711ac7a9SEd Tanous                             }
918711ac7a9SEd Tanous                             if (std::find(curProfiles->begin(),
919711ac7a9SEd Tanous                                           curProfiles->end(),
920711ac7a9SEd Tanous                                           profile) == curProfiles->end())
921711ac7a9SEd Tanous                             {
922711ac7a9SEd Tanous                                 std::vector<std::string> newProfiles =
923711ac7a9SEd Tanous                                     *curProfiles;
924711ac7a9SEd Tanous                                 newProfiles.push_back(profile);
925b9d36b47SEd Tanous                                 output.emplace_back("Profiles", newProfiles);
926711ac7a9SEd Tanous                             }
927711ac7a9SEd Tanous                         }
928711ac7a9SEd Tanous                     }
929711ac7a9SEd Tanous                 }
930711ac7a9SEd Tanous             }
931711ac7a9SEd Tanous 
932711ac7a9SEd Tanous             if (!ifaceFound)
93373df0db0SJames Feist             {
93462598e31SEd Tanous                 BMCWEB_LOG_ERROR("Failed to find interface in managed object");
93573df0db0SJames Feist                 messages::internalError(response->res);
93673df0db0SJames Feist                 return CreatePIDRet::fail;
93773df0db0SJames Feist             }
93873df0db0SJames Feist         }
93973df0db0SJames Feist     }
94073df0db0SJames Feist 
94183ff9ab6SJames Feist     if (type == "PidControllers" || type == "FanControllers")
94283ff9ab6SJames Feist     {
94383ff9ab6SJames Feist         if (createNewObject)
94483ff9ab6SJames Feist         {
945b9d36b47SEd Tanous             output.emplace_back("Class",
946b9d36b47SEd Tanous                                 type == "PidControllers" ? "temp" : "fan");
947b9d36b47SEd Tanous             output.emplace_back("Type", "Pid");
94883ff9ab6SJames Feist         }
9495f2caaefSJames Feist 
9509e9b6049SEd Tanous         std::optional<std::vector<nlohmann::json::object_t>> zones;
9515f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
9525f2caaefSJames Feist         std::optional<std::vector<std::string>> outputs;
9535f2caaefSJames Feist         std::map<std::string, std::optional<double>> doubles;
954b943aaefSJames Feist         std::optional<std::string> setpointOffset;
9555f2caaefSJames Feist         if (!redfish::json_util::readJson(
9569e9b6049SEd Tanous                 jsonValue, response->res, "Inputs", inputs, "Outputs", outputs,
9575f2caaefSJames Feist                 "Zones", zones, "FFGainCoefficient",
9585f2caaefSJames Feist                 doubles["FFGainCoefficient"], "FFOffCoefficient",
9595f2caaefSJames Feist                 doubles["FFOffCoefficient"], "ICoefficient",
9605f2caaefSJames Feist                 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
9615f2caaefSJames Feist                 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
9625f2caaefSJames Feist                 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
9635f2caaefSJames Feist                 "PCoefficient", doubles["PCoefficient"], "SetPoint",
964b943aaefSJames Feist                 doubles["SetPoint"], "SetPointOffset", setpointOffset,
965b943aaefSJames Feist                 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
966b943aaefSJames Feist                 "PositiveHysteresis", doubles["PositiveHysteresis"],
967b943aaefSJames Feist                 "NegativeHysteresis", doubles["NegativeHysteresis"]))
96883ff9ab6SJames Feist         {
9695f2caaefSJames Feist             return CreatePIDRet::fail;
97083ff9ab6SJames Feist         }
9715f2caaefSJames Feist         if (zones)
9725f2caaefSJames Feist         {
9735f2caaefSJames Feist             std::vector<std::string> zonesStr;
9745f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStr))
9755f2caaefSJames Feist             {
97662598e31SEd Tanous                 BMCWEB_LOG_ERROR("Illegal Zones");
9775f2caaefSJames Feist                 return CreatePIDRet::fail;
9785f2caaefSJames Feist             }
979b6baeaa4SJames Feist             if (chassis.empty() &&
980e662eae8SEd Tanous                 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
981b6baeaa4SJames Feist             {
98262598e31SEd Tanous                 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
983ace85d60SEd Tanous                 messages::invalidObject(
984ef4c65b7SEd Tanous                     response->res,
985ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", chassis));
986b6baeaa4SJames Feist                 return CreatePIDRet::fail;
987b6baeaa4SJames Feist             }
988b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStr));
9895f2caaefSJames Feist         }
990afb9ee06SEd Tanous 
991afb9ee06SEd Tanous         if (inputs)
9925f2caaefSJames Feist         {
993afb9ee06SEd Tanous             for (std::string& value : *inputs)
99483ff9ab6SJames Feist             {
995a170f275SEd Tanous                 std::replace(value.begin(), value.end(), '_', ' ');
99683ff9ab6SJames Feist             }
997afb9ee06SEd Tanous             output.emplace_back("Inputs", *inputs);
998afb9ee06SEd Tanous         }
999afb9ee06SEd Tanous 
1000afb9ee06SEd Tanous         if (outputs)
10015f2caaefSJames Feist         {
1002afb9ee06SEd Tanous             for (std::string& value : *outputs)
10035f2caaefSJames Feist             {
1004afb9ee06SEd Tanous                 std::replace(value.begin(), value.end(), '_', ' ');
10055f2caaefSJames Feist             }
1006afb9ee06SEd Tanous             output.emplace_back("Outputs", *outputs);
100783ff9ab6SJames Feist         }
100883ff9ab6SJames Feist 
1009b943aaefSJames Feist         if (setpointOffset)
1010b943aaefSJames Feist         {
1011b943aaefSJames Feist             // translate between redfish and dbus names
1012b943aaefSJames Feist             if (*setpointOffset == "UpperThresholdNonCritical")
1013b943aaefSJames Feist             {
1014b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningLow");
1015b943aaefSJames Feist             }
1016b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdNonCritical")
1017b943aaefSJames Feist             {
1018b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningHigh");
1019b943aaefSJames Feist             }
1020b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdCritical")
1021b943aaefSJames Feist             {
1022b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalLow");
1023b943aaefSJames Feist             }
1024b943aaefSJames Feist             else if (*setpointOffset == "UpperThresholdCritical")
1025b943aaefSJames Feist             {
1026b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalHigh");
1027b943aaefSJames Feist             }
1028b943aaefSJames Feist             else
1029b943aaefSJames Feist             {
103062598e31SEd Tanous                 BMCWEB_LOG_ERROR("Invalid setpointoffset {}", *setpointOffset);
10319e9b6049SEd Tanous                 messages::propertyValueNotInList(response->res, name,
1032ace85d60SEd Tanous                                                  "SetPointOffset");
1033b943aaefSJames Feist                 return CreatePIDRet::fail;
1034b943aaefSJames Feist             }
1035b943aaefSJames Feist         }
1036b943aaefSJames Feist 
103783ff9ab6SJames Feist         // doubles
10385f2caaefSJames Feist         for (const auto& pairs : doubles)
103983ff9ab6SJames Feist         {
10405f2caaefSJames Feist             if (!pairs.second)
104183ff9ab6SJames Feist             {
10425f2caaefSJames Feist                 continue;
104383ff9ab6SJames Feist             }
104462598e31SEd Tanous             BMCWEB_LOG_DEBUG("{} = {}", pairs.first, *pairs.second);
1045b9d36b47SEd Tanous             output.emplace_back(pairs.first, *pairs.second);
10465f2caaefSJames Feist         }
104783ff9ab6SJames Feist     }
104883ff9ab6SJames Feist 
104983ff9ab6SJames Feist     else if (type == "FanZones")
105083ff9ab6SJames Feist     {
1051b9d36b47SEd Tanous         output.emplace_back("Type", "Pid.Zone");
105283ff9ab6SJames Feist 
10539e9b6049SEd Tanous         std::optional<std::string> chassisId;
10545f2caaefSJames Feist         std::optional<double> failSafePercent;
1055d3ec07f8SJames Feist         std::optional<double> minThermalOutput;
10569e9b6049SEd Tanous         if (!redfish::json_util::readJson(jsonValue, response->res,
10579e9b6049SEd Tanous                                           "Chassis/@odata.id", chassisId,
10589e9b6049SEd Tanous                                           "FailSafePercent", failSafePercent,
10599e9b6049SEd Tanous                                           "MinThermalOutput", minThermalOutput))
106083ff9ab6SJames Feist         {
106183ff9ab6SJames Feist             return CreatePIDRet::fail;
106283ff9ab6SJames Feist         }
10635f2caaefSJames Feist 
10649e9b6049SEd Tanous         if (chassisId)
106583ff9ab6SJames Feist         {
1066717794d5SAppaRao Puli             // /redfish/v1/chassis/chassis_name/
10679e9b6049SEd Tanous             if (!dbus::utility::getNthStringFromPath(*chassisId, 3, chassis))
106883ff9ab6SJames Feist             {
10699e9b6049SEd Tanous                 BMCWEB_LOG_ERROR("Got invalid path {}", *chassisId);
1070ace85d60SEd Tanous                 messages::invalidObject(
1071ef4c65b7SEd Tanous                     response->res,
10729e9b6049SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", *chassisId));
107383ff9ab6SJames Feist                 return CreatePIDRet::fail;
107483ff9ab6SJames Feist             }
107583ff9ab6SJames Feist         }
1076d3ec07f8SJames Feist         if (minThermalOutput)
107783ff9ab6SJames Feist         {
1078b9d36b47SEd Tanous             output.emplace_back("MinThermalOutput", *minThermalOutput);
10795f2caaefSJames Feist         }
10805f2caaefSJames Feist         if (failSafePercent)
108183ff9ab6SJames Feist         {
1082b9d36b47SEd Tanous             output.emplace_back("FailSafePercent", *failSafePercent);
10835f2caaefSJames Feist         }
10845f2caaefSJames Feist     }
10855f2caaefSJames Feist     else if (type == "StepwiseControllers")
10865f2caaefSJames Feist     {
1087b9d36b47SEd Tanous         output.emplace_back("Type", "Stepwise");
10885f2caaefSJames Feist 
10899e9b6049SEd Tanous         std::optional<std::vector<nlohmann::json::object_t>> zones;
10909e9b6049SEd Tanous         std::optional<std::vector<nlohmann::json::object_t>> steps;
10915f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
10925f2caaefSJames Feist         std::optional<double> positiveHysteresis;
10935f2caaefSJames Feist         std::optional<double> negativeHysteresis;
1094c33a90ecSJames Feist         std::optional<std::string> direction; // upper clipping curve vs lower
10955f2caaefSJames Feist         if (!redfish::json_util::readJson(
10969e9b6049SEd Tanous                 jsonValue, response->res, "Zones", zones, "Steps", steps,
1097b6baeaa4SJames Feist                 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
1098c33a90ecSJames Feist                 "NegativeHysteresis", negativeHysteresis, "Direction",
1099c33a90ecSJames Feist                 direction))
11005f2caaefSJames Feist         {
110183ff9ab6SJames Feist             return CreatePIDRet::fail;
110283ff9ab6SJames Feist         }
11035f2caaefSJames Feist 
11045f2caaefSJames Feist         if (zones)
110583ff9ab6SJames Feist         {
1106b6baeaa4SJames Feist             std::vector<std::string> zonesStrs;
1107b6baeaa4SJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStrs))
11085f2caaefSJames Feist             {
110962598e31SEd Tanous                 BMCWEB_LOG_ERROR("Illegal Zones");
111083ff9ab6SJames Feist                 return CreatePIDRet::fail;
111183ff9ab6SJames Feist             }
1112b6baeaa4SJames Feist             if (chassis.empty() &&
1113e662eae8SEd Tanous                 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
1114b6baeaa4SJames Feist             {
111562598e31SEd Tanous                 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
1116ace85d60SEd Tanous                 messages::invalidObject(
1117ef4c65b7SEd Tanous                     response->res,
1118ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", chassis));
1119b6baeaa4SJames Feist                 return CreatePIDRet::fail;
1120b6baeaa4SJames Feist             }
1121b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStrs));
11225f2caaefSJames Feist         }
11235f2caaefSJames Feist         if (steps)
11245f2caaefSJames Feist         {
11255f2caaefSJames Feist             std::vector<double> readings;
11265f2caaefSJames Feist             std::vector<double> outputs;
11275f2caaefSJames Feist             for (auto& step : *steps)
11285f2caaefSJames Feist             {
1129543f4400SEd Tanous                 double target = 0.0;
1130543f4400SEd Tanous                 double out = 0.0;
11315f2caaefSJames Feist 
11329e9b6049SEd Tanous                 if (!redfish::json_util::readJsonObject(
11339e9b6049SEd Tanous                         step, response->res, "Target", target, "Output", out))
11345f2caaefSJames Feist                 {
11355f2caaefSJames Feist                     return CreatePIDRet::fail;
11365f2caaefSJames Feist                 }
11375f2caaefSJames Feist                 readings.emplace_back(target);
113823a21a1cSEd Tanous                 outputs.emplace_back(out);
11395f2caaefSJames Feist             }
1140b9d36b47SEd Tanous             output.emplace_back("Reading", std::move(readings));
1141b9d36b47SEd Tanous             output.emplace_back("Output", std::move(outputs));
11425f2caaefSJames Feist         }
11435f2caaefSJames Feist         if (inputs)
11445f2caaefSJames Feist         {
11455f2caaefSJames Feist             for (std::string& value : *inputs)
11465f2caaefSJames Feist             {
1147a170f275SEd Tanous                 std::replace(value.begin(), value.end(), '_', ' ');
11485f2caaefSJames Feist             }
1149b9d36b47SEd Tanous             output.emplace_back("Inputs", std::move(*inputs));
11505f2caaefSJames Feist         }
11515f2caaefSJames Feist         if (negativeHysteresis)
11525f2caaefSJames Feist         {
1153b9d36b47SEd Tanous             output.emplace_back("NegativeHysteresis", *negativeHysteresis);
11545f2caaefSJames Feist         }
11555f2caaefSJames Feist         if (positiveHysteresis)
11565f2caaefSJames Feist         {
1157b9d36b47SEd Tanous             output.emplace_back("PositiveHysteresis", *positiveHysteresis);
115883ff9ab6SJames Feist         }
1159c33a90ecSJames Feist         if (direction)
1160c33a90ecSJames Feist         {
1161c33a90ecSJames Feist             constexpr const std::array<const char*, 2> allowedDirections = {
1162c33a90ecSJames Feist                 "Ceiling", "Floor"};
11633544d2a7SEd Tanous             if (std::ranges::find(allowedDirections, *direction) ==
11643544d2a7SEd Tanous                 allowedDirections.end())
1165c33a90ecSJames Feist             {
1166c33a90ecSJames Feist                 messages::propertyValueTypeError(response->res, "Direction",
1167c33a90ecSJames Feist                                                  *direction);
1168c33a90ecSJames Feist                 return CreatePIDRet::fail;
1169c33a90ecSJames Feist             }
1170b9d36b47SEd Tanous             output.emplace_back("Class", *direction);
1171c33a90ecSJames Feist         }
117283ff9ab6SJames Feist     }
117383ff9ab6SJames Feist     else
117483ff9ab6SJames Feist     {
117562598e31SEd Tanous         BMCWEB_LOG_ERROR("Illegal Type {}", type);
117635a62c7cSJason M. Bills         messages::propertyUnknown(response->res, type);
117783ff9ab6SJames Feist         return CreatePIDRet::fail;
117883ff9ab6SJames Feist     }
117983ff9ab6SJames Feist     return CreatePIDRet::patch;
118083ff9ab6SJames Feist }
118173df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
118273df0db0SJames Feist {
11836936afe4SEd Tanous     struct CompletionValues
11846936afe4SEd Tanous     {
11856936afe4SEd Tanous         std::vector<std::string> supportedProfiles;
11866936afe4SEd Tanous         std::string currentProfile;
11876936afe4SEd Tanous         dbus::utility::MapperGetSubTreeResponse subtree;
11886936afe4SEd Tanous     };
118983ff9ab6SJames Feist 
11904e23a444SEd Tanous     explicit GetPIDValues(
11914e23a444SEd Tanous         const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
119223a21a1cSEd Tanous         asyncResp(asyncRespIn)
119373df0db0SJames Feist 
11941214b7e7SGunnar Mills     {}
11959c310685SBorawski.Lukasz 
119673df0db0SJames Feist     void run()
11975b4aa86bSJames Feist     {
119873df0db0SJames Feist         std::shared_ptr<GetPIDValues> self = shared_from_this();
119973df0db0SJames Feist 
120073df0db0SJames Feist         // get all configurations
1201e99073f5SGeorge Liu         constexpr std::array<std::string_view, 4> interfaces = {
1202e99073f5SGeorge Liu             pidConfigurationIface, pidZoneConfigurationIface,
1203e99073f5SGeorge Liu             objectManagerIface, stepwiseConfigurationIface};
1204e99073f5SGeorge Liu         dbus::utility::getSubTree(
1205e99073f5SGeorge Liu             "/", 0, interfaces,
1206b9d36b47SEd Tanous             [self](
1207e99073f5SGeorge Liu                 const boost::system::error_code& ec,
1208b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
12095b4aa86bSJames Feist             if (ec)
12105b4aa86bSJames Feist             {
121162598e31SEd Tanous                 BMCWEB_LOG_ERROR("{}", ec);
121273df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
121373df0db0SJames Feist                 return;
121473df0db0SJames Feist             }
12156936afe4SEd Tanous             self->complete.subtree = subtreeLocal;
1216e99073f5SGeorge Liu         });
121773df0db0SJames Feist 
121873df0db0SJames Feist         // at the same time get the selected profile
1219e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1220e99073f5SGeorge Liu             thermalModeIface};
1221e99073f5SGeorge Liu         dbus::utility::getSubTree(
1222e99073f5SGeorge Liu             "/", 0, thermalModeIfaces,
1223b9d36b47SEd Tanous             [self](
1224e99073f5SGeorge Liu                 const boost::system::error_code& ec,
1225b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
122623a21a1cSEd Tanous             if (ec || subtreeLocal.empty())
122773df0db0SJames Feist             {
122873df0db0SJames Feist                 return;
122973df0db0SJames Feist             }
123023a21a1cSEd Tanous             if (subtreeLocal[0].second.size() != 1)
123173df0db0SJames Feist             {
123273df0db0SJames Feist                 // invalid mapper response, should never happen
123362598e31SEd Tanous                 BMCWEB_LOG_ERROR("GetPIDValues: Mapper Error");
123473df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
12355b4aa86bSJames Feist                 return;
12365b4aa86bSJames Feist             }
12375b4aa86bSJames Feist 
123823a21a1cSEd Tanous             const std::string& path = subtreeLocal[0].first;
123923a21a1cSEd Tanous             const std::string& owner = subtreeLocal[0].second[0].first;
1240fac6e53bSKrzysztof Grobelny 
1241fac6e53bSKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
1242fac6e53bSKrzysztof Grobelny                 *crow::connections::systemBus, owner, path, thermalModeIface,
1243168e20c1SEd Tanous                 [path, owner,
12445e7e2dc5SEd Tanous                  self](const boost::system::error_code& ec2,
1245b9d36b47SEd Tanous                        const dbus::utility::DBusPropertiesMap& resp) {
124623a21a1cSEd Tanous                 if (ec2)
124773df0db0SJames Feist                 {
124862598e31SEd Tanous                     BMCWEB_LOG_ERROR(
124962598e31SEd Tanous                         "GetPIDValues: Can't get thermalModeIface {}", path);
125073df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
125173df0db0SJames Feist                     return;
125273df0db0SJames Feist                 }
1253fac6e53bSKrzysztof Grobelny 
1254271584abSEd Tanous                 const std::string* current = nullptr;
1255271584abSEd Tanous                 const std::vector<std::string>* supported = nullptr;
1256fac6e53bSKrzysztof Grobelny 
1257fac6e53bSKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
1258fac6e53bSKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), resp, "Current", current,
1259fac6e53bSKrzysztof Grobelny                     "Supported", supported);
1260fac6e53bSKrzysztof Grobelny 
1261fac6e53bSKrzysztof Grobelny                 if (!success)
126273df0db0SJames Feist                 {
1263002d39b4SEd Tanous                     messages::internalError(self->asyncResp->res);
126473df0db0SJames Feist                     return;
126573df0db0SJames Feist                 }
1266fac6e53bSKrzysztof Grobelny 
126773df0db0SJames Feist                 if (current == nullptr || supported == nullptr)
126873df0db0SJames Feist                 {
126962598e31SEd Tanous                     BMCWEB_LOG_ERROR(
127062598e31SEd Tanous                         "GetPIDValues: thermal mode iface invalid {}", path);
127173df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
127273df0db0SJames Feist                     return;
127373df0db0SJames Feist                 }
12746936afe4SEd Tanous                 self->complete.currentProfile = *current;
12756936afe4SEd Tanous                 self->complete.supportedProfiles = *supported;
1276fac6e53bSKrzysztof Grobelny             });
1277e99073f5SGeorge Liu         });
127873df0db0SJames Feist     }
127973df0db0SJames Feist 
12806936afe4SEd Tanous     static void
12816936afe4SEd Tanous         processingComplete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12826936afe4SEd Tanous                            const CompletionValues& completion)
128373df0db0SJames Feist     {
128473df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
128573df0db0SJames Feist         {
128673df0db0SJames Feist             return;
128773df0db0SJames Feist         }
12885b4aa86bSJames Feist         // create map of <connection, path to objMgr>>
12896936afe4SEd Tanous         boost::container::flat_map<
12906936afe4SEd Tanous             std::string, std::string, std::less<>,
12916936afe4SEd Tanous             std::vector<std::pair<std::string, std::string>>>
12926936afe4SEd Tanous             objectMgrPaths;
12936936afe4SEd Tanous         boost::container::flat_set<std::string, std::less<>,
12946936afe4SEd Tanous                                    std::vector<std::string>>
12956936afe4SEd Tanous             calledConnections;
12966936afe4SEd Tanous         for (const auto& pathGroup : completion.subtree)
12975b4aa86bSJames Feist         {
12985b4aa86bSJames Feist             for (const auto& connectionGroup : pathGroup.second)
12995b4aa86bSJames Feist             {
13006bce33bcSJames Feist                 auto findConnection =
13016bce33bcSJames Feist                     calledConnections.find(connectionGroup.first);
13026bce33bcSJames Feist                 if (findConnection != calledConnections.end())
13036bce33bcSJames Feist                 {
13046bce33bcSJames Feist                     break;
13056bce33bcSJames Feist                 }
130673df0db0SJames Feist                 for (const std::string& interface : connectionGroup.second)
13075b4aa86bSJames Feist                 {
13085b4aa86bSJames Feist                     if (interface == objectManagerIface)
13095b4aa86bSJames Feist                     {
131073df0db0SJames Feist                         objectMgrPaths[connectionGroup.first] = pathGroup.first;
13115b4aa86bSJames Feist                     }
13125b4aa86bSJames Feist                     // this list is alphabetical, so we
13135b4aa86bSJames Feist                     // should have found the objMgr by now
13145b4aa86bSJames Feist                     if (interface == pidConfigurationIface ||
1315b7a08d04SJames Feist                         interface == pidZoneConfigurationIface ||
1316b7a08d04SJames Feist                         interface == stepwiseConfigurationIface)
13175b4aa86bSJames Feist                     {
13185b4aa86bSJames Feist                         auto findObjMgr =
13195b4aa86bSJames Feist                             objectMgrPaths.find(connectionGroup.first);
13205b4aa86bSJames Feist                         if (findObjMgr == objectMgrPaths.end())
13215b4aa86bSJames Feist                         {
132262598e31SEd Tanous                             BMCWEB_LOG_DEBUG("{}Has no Object Manager",
132362598e31SEd Tanous                                              connectionGroup.first);
13245b4aa86bSJames Feist                             continue;
13255b4aa86bSJames Feist                         }
13266bce33bcSJames Feist 
13276bce33bcSJames Feist                         calledConnections.insert(connectionGroup.first);
13286bce33bcSJames Feist 
132973df0db0SJames Feist                         asyncPopulatePid(findObjMgr->first, findObjMgr->second,
13306936afe4SEd Tanous                                          completion.currentProfile,
13316936afe4SEd Tanous                                          completion.supportedProfiles,
133273df0db0SJames Feist                                          asyncResp);
13335b4aa86bSJames Feist                         break;
13345b4aa86bSJames Feist                     }
13355b4aa86bSJames Feist                 }
13365b4aa86bSJames Feist             }
13375b4aa86bSJames Feist         }
133873df0db0SJames Feist     }
133973df0db0SJames Feist 
13406936afe4SEd Tanous     ~GetPIDValues()
13416936afe4SEd Tanous     {
13426936afe4SEd Tanous         boost::asio::post(crow::connections::systemBus->get_io_context(),
13436936afe4SEd Tanous                           std::bind_front(&processingComplete, asyncResp,
13446936afe4SEd Tanous                                           std::move(complete)));
13456936afe4SEd Tanous     }
13466936afe4SEd Tanous 
1347ecd6a3a2SEd Tanous     GetPIDValues(const GetPIDValues&) = delete;
1348ecd6a3a2SEd Tanous     GetPIDValues(GetPIDValues&&) = delete;
1349ecd6a3a2SEd Tanous     GetPIDValues& operator=(const GetPIDValues&) = delete;
1350ecd6a3a2SEd Tanous     GetPIDValues& operator=(GetPIDValues&&) = delete;
1351ecd6a3a2SEd Tanous 
13528d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
13536936afe4SEd Tanous     CompletionValues complete;
135473df0db0SJames Feist };
135573df0db0SJames Feist 
135673df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
135773df0db0SJames Feist {
13589e9b6049SEd Tanous     SetPIDValues(
13599e9b6049SEd Tanous         const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
13609e9b6049SEd Tanous         std::vector<
13619e9b6049SEd Tanous             std::pair<std::string, std::optional<nlohmann::json::object_t>>>&&
13629e9b6049SEd Tanous             configurationsIn,
13639e9b6049SEd Tanous         std::optional<std::string>& profileIn) :
13649e9b6049SEd Tanous         asyncResp(asyncRespIn),
13659e9b6049SEd Tanous         configuration(std::move(configurationsIn)),
13669e9b6049SEd Tanous         profile(std::move(profileIn))
13679e9b6049SEd Tanous     {}
1368ecd6a3a2SEd Tanous 
1369ecd6a3a2SEd Tanous     SetPIDValues(const SetPIDValues&) = delete;
1370ecd6a3a2SEd Tanous     SetPIDValues(SetPIDValues&&) = delete;
1371ecd6a3a2SEd Tanous     SetPIDValues& operator=(const SetPIDValues&) = delete;
1372ecd6a3a2SEd Tanous     SetPIDValues& operator=(SetPIDValues&&) = delete;
1373ecd6a3a2SEd Tanous 
137473df0db0SJames Feist     void run()
137573df0db0SJames Feist     {
137673df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
137773df0db0SJames Feist         {
137873df0db0SJames Feist             return;
137973df0db0SJames Feist         }
138073df0db0SJames Feist 
138173df0db0SJames Feist         std::shared_ptr<SetPIDValues> self = shared_from_this();
138273df0db0SJames Feist 
138373df0db0SJames Feist         // todo(james): might make sense to do a mapper call here if this
138473df0db0SJames Feist         // interface gets more traction
13855eb468daSGeorge Liu         sdbusplus::message::object_path objPath(
13865eb468daSGeorge Liu             "/xyz/openbmc_project/inventory");
13875eb468daSGeorge Liu         dbus::utility::getManagedObjects(
13885eb468daSGeorge Liu             "xyz.openbmc_project.EntityManager", objPath,
13895e7e2dc5SEd Tanous             [self](const boost::system::error_code& ec,
1390914e2d5dSEd Tanous                    const dbus::utility::ManagedObjectType& mObj) {
139173df0db0SJames Feist             if (ec)
139273df0db0SJames Feist             {
139362598e31SEd Tanous                 BMCWEB_LOG_ERROR("Error communicating to Entity Manager");
139473df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
139573df0db0SJames Feist                 return;
139673df0db0SJames Feist             }
1397e69d9de2SJames Feist             const std::array<const char*, 3> configurations = {
1398e69d9de2SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
1399e69d9de2SJames Feist                 stepwiseConfigurationIface};
1400e69d9de2SJames Feist 
140114b0b8d5SJames Feist             for (const auto& [path, object] : mObj)
1402e69d9de2SJames Feist             {
140314b0b8d5SJames Feist                 for (const auto& [interface, _] : object)
1404e69d9de2SJames Feist                 {
14053544d2a7SEd Tanous                     if (std::ranges::find(configurations, interface) !=
14063544d2a7SEd Tanous                         configurations.end())
1407e69d9de2SJames Feist                     {
140814b0b8d5SJames Feist                         self->objectCount++;
1409e69d9de2SJames Feist                         break;
1410e69d9de2SJames Feist                     }
1411e69d9de2SJames Feist                 }
1412e69d9de2SJames Feist             }
1413914e2d5dSEd Tanous             self->managedObj = mObj;
14145eb468daSGeorge Liu         });
141573df0db0SJames Feist 
141673df0db0SJames Feist         // at the same time get the profile information
1417e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1418e99073f5SGeorge Liu             thermalModeIface};
1419e99073f5SGeorge Liu         dbus::utility::getSubTree(
1420e99073f5SGeorge Liu             "/", 0, thermalModeIfaces,
1421e99073f5SGeorge Liu             [self](const boost::system::error_code& ec,
1422b9d36b47SEd Tanous                    const dbus::utility::MapperGetSubTreeResponse& subtree) {
142373df0db0SJames Feist             if (ec || subtree.empty())
142473df0db0SJames Feist             {
142573df0db0SJames Feist                 return;
142673df0db0SJames Feist             }
142773df0db0SJames Feist             if (subtree[0].second.empty())
142873df0db0SJames Feist             {
142973df0db0SJames Feist                 // invalid mapper response, should never happen
143062598e31SEd Tanous                 BMCWEB_LOG_ERROR("SetPIDValues: Mapper Error");
143173df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
143273df0db0SJames Feist                 return;
143373df0db0SJames Feist             }
143473df0db0SJames Feist 
143573df0db0SJames Feist             const std::string& path = subtree[0].first;
143673df0db0SJames Feist             const std::string& owner = subtree[0].second[0].first;
1437fac6e53bSKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
1438fac6e53bSKrzysztof Grobelny                 *crow::connections::systemBus, owner, path, thermalModeIface,
14395e7e2dc5SEd Tanous                 [self, path, owner](const boost::system::error_code& ec2,
1440b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap& r) {
1441cb13a392SEd Tanous                 if (ec2)
144273df0db0SJames Feist                 {
144362598e31SEd Tanous                     BMCWEB_LOG_ERROR(
144462598e31SEd Tanous                         "SetPIDValues: Can't get thermalModeIface {}", path);
144573df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
144673df0db0SJames Feist                     return;
144773df0db0SJames Feist                 }
1448271584abSEd Tanous                 const std::string* current = nullptr;
1449271584abSEd Tanous                 const std::vector<std::string>* supported = nullptr;
1450fac6e53bSKrzysztof Grobelny 
1451fac6e53bSKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
1452fac6e53bSKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), r, "Current", current,
1453fac6e53bSKrzysztof Grobelny                     "Supported", supported);
1454fac6e53bSKrzysztof Grobelny 
1455fac6e53bSKrzysztof Grobelny                 if (!success)
145673df0db0SJames Feist                 {
1457002d39b4SEd Tanous                     messages::internalError(self->asyncResp->res);
145873df0db0SJames Feist                     return;
145973df0db0SJames Feist                 }
1460fac6e53bSKrzysztof Grobelny 
146173df0db0SJames Feist                 if (current == nullptr || supported == nullptr)
146273df0db0SJames Feist                 {
146362598e31SEd Tanous                     BMCWEB_LOG_ERROR(
146462598e31SEd Tanous                         "SetPIDValues: thermal mode iface invalid {}", path);
146573df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
146673df0db0SJames Feist                     return;
146773df0db0SJames Feist                 }
146873df0db0SJames Feist                 self->currentProfile = *current;
146973df0db0SJames Feist                 self->supportedProfiles = *supported;
147073df0db0SJames Feist                 self->profileConnection = owner;
147173df0db0SJames Feist                 self->profilePath = path;
1472fac6e53bSKrzysztof Grobelny             });
1473e99073f5SGeorge Liu         });
147473df0db0SJames Feist     }
147524b2fe81SEd Tanous     void pidSetDone()
147673df0db0SJames Feist     {
147773df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
147873df0db0SJames Feist         {
147973df0db0SJames Feist             return;
14805b4aa86bSJames Feist         }
14818d1b46d7Szhanghch05         std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
148273df0db0SJames Feist         if (profile)
148373df0db0SJames Feist         {
14843544d2a7SEd Tanous             if (std::ranges::find(supportedProfiles, *profile) ==
14853544d2a7SEd Tanous                 supportedProfiles.end())
148673df0db0SJames Feist             {
148773df0db0SJames Feist                 messages::actionParameterUnknown(response->res, "Profile",
148873df0db0SJames Feist                                                  *profile);
148973df0db0SJames Feist                 return;
149073df0db0SJames Feist             }
149173df0db0SJames Feist             currentProfile = *profile;
14929ae226faSGeorge Liu             sdbusplus::asio::setProperty(
14939ae226faSGeorge Liu                 *crow::connections::systemBus, profileConnection, profilePath,
14949ae226faSGeorge Liu                 thermalModeIface, "Current", *profile,
14955e7e2dc5SEd Tanous                 [response](const boost::system::error_code& ec) {
149673df0db0SJames Feist                 if (ec)
149773df0db0SJames Feist                 {
149862598e31SEd Tanous                     BMCWEB_LOG_ERROR("Error patching profile{}", ec);
149973df0db0SJames Feist                     messages::internalError(response->res);
150073df0db0SJames Feist                 }
15019ae226faSGeorge Liu             });
150273df0db0SJames Feist         }
150373df0db0SJames Feist 
150473df0db0SJames Feist         for (auto& containerPair : configuration)
150573df0db0SJames Feist         {
150673df0db0SJames Feist             auto& container = containerPair.second;
150773df0db0SJames Feist             if (!container)
150873df0db0SJames Feist             {
150973df0db0SJames Feist                 continue;
151073df0db0SJames Feist             }
15116ee7f774SJames Feist 
151202cad96eSEd Tanous             const std::string& type = containerPair.first;
151373df0db0SJames Feist 
15149e9b6049SEd Tanous             for (auto& [name, value] : *container)
151573df0db0SJames Feist             {
1516cddbf3dfSPotin Lai                 std::string dbusObjName = name;
1517cddbf3dfSPotin Lai                 std::replace(dbusObjName.begin(), dbusObjName.end(), ' ', '_');
151862598e31SEd Tanous                 BMCWEB_LOG_DEBUG("looking for {}", name);
15196ee7f774SJames Feist 
15203544d2a7SEd Tanous                 auto pathItr = std::ranges::find_if(
15213544d2a7SEd Tanous                     managedObj, [&dbusObjName](const auto& obj) {
152291f75cafSEd Tanous                     return obj.first.filename() == dbusObjName;
152373df0db0SJames Feist                 });
1524b9d36b47SEd Tanous                 dbus::utility::DBusPropertiesMap output;
152573df0db0SJames Feist 
152673df0db0SJames Feist                 output.reserve(16); // The pid interface length
152773df0db0SJames Feist 
152873df0db0SJames Feist                 // determines if we're patching entity-manager or
152973df0db0SJames Feist                 // creating a new object
153073df0db0SJames Feist                 bool createNewObject = (pathItr == managedObj.end());
153162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Found = {}", !createNewObject);
15326ee7f774SJames Feist 
153373df0db0SJames Feist                 std::string iface;
1534ea2b670dSEd Tanous                 if (!createNewObject)
1535ea2b670dSEd Tanous                 {
15368be2b5b6SPotin Lai                     bool findInterface = false;
1537ea2b670dSEd Tanous                     for (const auto& interface : pathItr->second)
1538ea2b670dSEd Tanous                     {
1539ea2b670dSEd Tanous                         if (interface.first == pidConfigurationIface)
1540ea2b670dSEd Tanous                         {
1541ea2b670dSEd Tanous                             if (type == "PidControllers" ||
1542ea2b670dSEd Tanous                                 type == "FanControllers")
154373df0db0SJames Feist                             {
154473df0db0SJames Feist                                 iface = pidConfigurationIface;
15458be2b5b6SPotin Lai                                 findInterface = true;
15468be2b5b6SPotin Lai                                 break;
154773df0db0SJames Feist                             }
154873df0db0SJames Feist                         }
1549ea2b670dSEd Tanous                         else if (interface.first == pidZoneConfigurationIface)
155073df0db0SJames Feist                         {
1551ea2b670dSEd Tanous                             if (type == "FanZones")
155273df0db0SJames Feist                             {
1553da39350aSPavanKumarIntel                                 iface = pidZoneConfigurationIface;
15548be2b5b6SPotin Lai                                 findInterface = true;
15558be2b5b6SPotin Lai                                 break;
155673df0db0SJames Feist                             }
155773df0db0SJames Feist                         }
1558ea2b670dSEd Tanous                         else if (interface.first == stepwiseConfigurationIface)
1559ea2b670dSEd Tanous                         {
1560ea2b670dSEd Tanous                             if (type == "StepwiseControllers")
156173df0db0SJames Feist                             {
156273df0db0SJames Feist                                 iface = stepwiseConfigurationIface;
15638be2b5b6SPotin Lai                                 findInterface = true;
15648be2b5b6SPotin Lai                                 break;
15658be2b5b6SPotin Lai                             }
15668be2b5b6SPotin Lai                         }
15678be2b5b6SPotin Lai                     }
15688be2b5b6SPotin Lai 
15698be2b5b6SPotin Lai                     // create new object if interface not found
15708be2b5b6SPotin Lai                     if (!findInterface)
15718be2b5b6SPotin Lai                     {
157273df0db0SJames Feist                         createNewObject = true;
157373df0db0SJames Feist                     }
1574ea2b670dSEd Tanous                 }
15756ee7f774SJames Feist 
15769e9b6049SEd Tanous                 if (createNewObject && value == nullptr)
15776ee7f774SJames Feist                 {
15784e0453b1SGunnar Mills                     // can't delete a non-existent object
15799e9b6049SEd Tanous                     messages::propertyValueNotInList(response->res, value,
1580e2616cc5SEd Tanous                                                      name);
15816ee7f774SJames Feist                     continue;
15826ee7f774SJames Feist                 }
15836ee7f774SJames Feist 
15846ee7f774SJames Feist                 std::string path;
15856ee7f774SJames Feist                 if (pathItr != managedObj.end())
15866ee7f774SJames Feist                 {
15876ee7f774SJames Feist                     path = pathItr->first.str;
15886ee7f774SJames Feist                 }
15896ee7f774SJames Feist 
159062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Create new = {}", createNewObject);
1591e69d9de2SJames Feist 
1592e69d9de2SJames Feist                 // arbitrary limit to avoid attacks
1593e69d9de2SJames Feist                 constexpr const size_t controllerLimit = 500;
159414b0b8d5SJames Feist                 if (createNewObject && objectCount >= controllerLimit)
1595e69d9de2SJames Feist                 {
1596e69d9de2SJames Feist                     messages::resourceExhaustion(response->res, type);
1597e69d9de2SJames Feist                     continue;
1598e69d9de2SJames Feist                 }
1599a170f275SEd Tanous                 std::string escaped = name;
1600a170f275SEd Tanous                 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1601a170f275SEd Tanous                 output.emplace_back("Name", escaped);
160273df0db0SJames Feist 
160373df0db0SJames Feist                 std::string chassis;
160473df0db0SJames Feist                 CreatePIDRet ret = createPidInterface(
16059e9b6049SEd Tanous                     response, type, name, value, path, managedObj,
16069e9b6049SEd Tanous                     createNewObject, output, chassis, currentProfile);
160773df0db0SJames Feist                 if (ret == CreatePIDRet::fail)
160873df0db0SJames Feist                 {
160973df0db0SJames Feist                     return;
161073df0db0SJames Feist                 }
16113174e4dfSEd Tanous                 if (ret == CreatePIDRet::del)
161273df0db0SJames Feist                 {
161373df0db0SJames Feist                     continue;
161473df0db0SJames Feist                 }
161573df0db0SJames Feist 
161673df0db0SJames Feist                 if (!createNewObject)
161773df0db0SJames Feist                 {
161873df0db0SJames Feist                     for (const auto& property : output)
161973df0db0SJames Feist                     {
16207a696974SPotin Lai                         crow::connections::systemBus->async_method_call(
162173df0db0SJames Feist                             [response,
162273df0db0SJames Feist                              propertyName{std::string(property.first)}](
16235e7e2dc5SEd Tanous                                 const boost::system::error_code& ec) {
162473df0db0SJames Feist                             if (ec)
162573df0db0SJames Feist                             {
162662598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Error patching {}: {}",
162762598e31SEd Tanous                                                  propertyName, ec);
162873df0db0SJames Feist                                 messages::internalError(response->res);
162973df0db0SJames Feist                                 return;
163073df0db0SJames Feist                             }
163173df0db0SJames Feist                             messages::success(response->res);
16327a696974SPotin Lai                         },
16337a696974SPotin Lai                             "xyz.openbmc_project.EntityManager", path,
16347a696974SPotin Lai                             "org.freedesktop.DBus.Properties", "Set", iface,
16357a696974SPotin Lai                             property.first, property.second);
163673df0db0SJames Feist                     }
163773df0db0SJames Feist                 }
163873df0db0SJames Feist                 else
163973df0db0SJames Feist                 {
164073df0db0SJames Feist                     if (chassis.empty())
164173df0db0SJames Feist                     {
164262598e31SEd Tanous                         BMCWEB_LOG_ERROR("Failed to get chassis from config");
1643ace85d60SEd Tanous                         messages::internalError(response->res);
164473df0db0SJames Feist                         return;
164573df0db0SJames Feist                     }
164673df0db0SJames Feist 
164773df0db0SJames Feist                     bool foundChassis = false;
164873df0db0SJames Feist                     for (const auto& obj : managedObj)
164973df0db0SJames Feist                     {
165091f75cafSEd Tanous                         if (obj.first.filename() == chassis)
165173df0db0SJames Feist                         {
165273df0db0SJames Feist                             chassis = obj.first.str;
165373df0db0SJames Feist                             foundChassis = true;
165473df0db0SJames Feist                             break;
165573df0db0SJames Feist                         }
165673df0db0SJames Feist                     }
165773df0db0SJames Feist                     if (!foundChassis)
165873df0db0SJames Feist                     {
165962598e31SEd Tanous                         BMCWEB_LOG_ERROR("Failed to find chassis on dbus");
166073df0db0SJames Feist                         messages::resourceMissingAtURI(
1661ace85d60SEd Tanous                             response->res,
1662ef4c65b7SEd Tanous                             boost::urls::format("/redfish/v1/Chassis/{}",
1663ef4c65b7SEd Tanous                                                 chassis));
166473df0db0SJames Feist                         return;
166573df0db0SJames Feist                     }
166673df0db0SJames Feist 
166773df0db0SJames Feist                     crow::connections::systemBus->async_method_call(
16685e7e2dc5SEd Tanous                         [response](const boost::system::error_code& ec) {
166973df0db0SJames Feist                         if (ec)
167073df0db0SJames Feist                         {
167162598e31SEd Tanous                             BMCWEB_LOG_ERROR("Error Adding Pid Object {}", ec);
167273df0db0SJames Feist                             messages::internalError(response->res);
167373df0db0SJames Feist                             return;
167473df0db0SJames Feist                         }
167573df0db0SJames Feist                         messages::success(response->res);
167673df0db0SJames Feist                     },
167773df0db0SJames Feist                         "xyz.openbmc_project.EntityManager", chassis,
167873df0db0SJames Feist                         "xyz.openbmc_project.AddObject", "AddObject", output);
167973df0db0SJames Feist                 }
168073df0db0SJames Feist             }
168173df0db0SJames Feist         }
168273df0db0SJames Feist     }
168324b2fe81SEd Tanous 
168424b2fe81SEd Tanous     ~SetPIDValues()
168524b2fe81SEd Tanous     {
168624b2fe81SEd Tanous         try
168724b2fe81SEd Tanous         {
168824b2fe81SEd Tanous             pidSetDone();
168924b2fe81SEd Tanous         }
169024b2fe81SEd Tanous         catch (...)
169124b2fe81SEd Tanous         {
169262598e31SEd Tanous             BMCWEB_LOG_CRITICAL("pidSetDone threw exception");
169324b2fe81SEd Tanous         }
169424b2fe81SEd Tanous     }
169524b2fe81SEd Tanous 
16968d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
16979e9b6049SEd Tanous     std::vector<std::pair<std::string, std::optional<nlohmann::json::object_t>>>
169873df0db0SJames Feist         configuration;
169973df0db0SJames Feist     std::optional<std::string> profile;
170073df0db0SJames Feist     dbus::utility::ManagedObjectType managedObj;
170173df0db0SJames Feist     std::vector<std::string> supportedProfiles;
170273df0db0SJames Feist     std::string currentProfile;
170373df0db0SJames Feist     std::string profileConnection;
170473df0db0SJames Feist     std::string profilePath;
170514b0b8d5SJames Feist     size_t objectCount = 0;
170673df0db0SJames Feist };
170773df0db0SJames Feist 
1708071d8fdfSSunnySrivastava1984 /**
1709071d8fdfSSunnySrivastava1984  * @brief Retrieves BMC manager location data over DBus
1710071d8fdfSSunnySrivastava1984  *
1711ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for completing asynchronous calls
1712071d8fdfSSunnySrivastava1984  * @param[in] connectionName - service name
1713071d8fdfSSunnySrivastava1984  * @param[in] path - object path
1714071d8fdfSSunnySrivastava1984  * @return none
1715071d8fdfSSunnySrivastava1984  */
1716ac106bf6SEd Tanous inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1717071d8fdfSSunnySrivastava1984                         const std::string& connectionName,
1718071d8fdfSSunnySrivastava1984                         const std::string& path)
1719071d8fdfSSunnySrivastava1984 {
172062598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get BMC manager Location data.");
1721071d8fdfSSunnySrivastava1984 
17221e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
17231e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
17241e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
1725ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
17261e1e598dSJonathan Doman                     const std::string& property) {
1727071d8fdfSSunnySrivastava1984         if (ec)
1728071d8fdfSSunnySrivastava1984         {
172962598e31SEd Tanous             BMCWEB_LOG_DEBUG("DBUS response error for "
173062598e31SEd Tanous                              "Location");
1731ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
1732071d8fdfSSunnySrivastava1984             return;
1733071d8fdfSSunnySrivastava1984         }
1734071d8fdfSSunnySrivastava1984 
1735ac106bf6SEd Tanous         asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
17361e1e598dSJonathan Doman             property;
17371e1e598dSJonathan Doman     });
1738071d8fdfSSunnySrivastava1984 }
17397e860f15SJohn Edward Broadbent // avoid name collision systems.hpp
17407e860f15SJohn Edward Broadbent inline void
1741ac106bf6SEd Tanous     managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
17424bf2b033SGunnar Mills {
174362598e31SEd Tanous     BMCWEB_LOG_DEBUG("Getting Manager Last Reset Time");
17444bf2b033SGunnar Mills 
17451e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint64_t>(
17461e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
17471e1e598dSJonathan Doman         "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
17481e1e598dSJonathan Doman         "LastRebootTime",
1749ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
17501e1e598dSJonathan Doman                     const uint64_t lastResetTime) {
17514bf2b033SGunnar Mills         if (ec)
17524bf2b033SGunnar Mills         {
175362598e31SEd Tanous             BMCWEB_LOG_DEBUG("D-BUS response error {}", ec);
17544bf2b033SGunnar Mills             return;
17554bf2b033SGunnar Mills         }
17564bf2b033SGunnar Mills 
17574bf2b033SGunnar Mills         // LastRebootTime is epoch time, in milliseconds
17584bf2b033SGunnar Mills         // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
17591e1e598dSJonathan Doman         uint64_t lastResetTimeStamp = lastResetTime / 1000;
17604bf2b033SGunnar Mills 
17614bf2b033SGunnar Mills         // Convert to ISO 8601 standard
1762ac106bf6SEd Tanous         asyncResp->res.jsonValue["LastResetTime"] =
17632b82937eSEd Tanous             redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
17641e1e598dSJonathan Doman     });
17654bf2b033SGunnar Mills }
17664bf2b033SGunnar Mills 
17674bfefa74SGunnar Mills /**
17684bfefa74SGunnar Mills  * @brief Set the running firmware image
17694bfefa74SGunnar Mills  *
1770ac106bf6SEd Tanous  * @param[i,o] asyncResp - Async response object
17714bfefa74SGunnar Mills  * @param[i] runningFirmwareTarget - Image to make the running image
17724bfefa74SGunnar Mills  *
17734bfefa74SGunnar Mills  * @return void
17744bfefa74SGunnar Mills  */
17757e860f15SJohn Edward Broadbent inline void
1776ac106bf6SEd Tanous     setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1777f23b7296SEd Tanous                            const std::string& runningFirmwareTarget)
17784bfefa74SGunnar Mills {
17794bfefa74SGunnar Mills     // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1780f23b7296SEd Tanous     std::string::size_type idPos = runningFirmwareTarget.rfind('/');
17814bfefa74SGunnar Mills     if (idPos == std::string::npos)
17824bfefa74SGunnar Mills     {
1783ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
17844bfefa74SGunnar Mills                                          "@odata.id");
178562598e31SEd Tanous         BMCWEB_LOG_DEBUG("Can't parse firmware ID!");
17864bfefa74SGunnar Mills         return;
17874bfefa74SGunnar Mills     }
17884bfefa74SGunnar Mills     idPos++;
17894bfefa74SGunnar Mills     if (idPos >= runningFirmwareTarget.size())
17904bfefa74SGunnar Mills     {
1791ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
17924bfefa74SGunnar Mills                                          "@odata.id");
179362598e31SEd Tanous         BMCWEB_LOG_DEBUG("Invalid firmware ID.");
17944bfefa74SGunnar Mills         return;
17954bfefa74SGunnar Mills     }
17964bfefa74SGunnar Mills     std::string firmwareId = runningFirmwareTarget.substr(idPos);
17974bfefa74SGunnar Mills 
17984bfefa74SGunnar Mills     // Make sure the image is valid before setting priority
17995eb468daSGeorge Liu     sdbusplus::message::object_path objPath("/xyz/openbmc_project/software");
18005eb468daSGeorge Liu     dbus::utility::getManagedObjects(
18015eb468daSGeorge Liu         "xyz.openbmc_project.Software.BMC.Updater", objPath,
18025eb468daSGeorge Liu         [asyncResp, firmwareId, runningFirmwareTarget](
18035eb468daSGeorge Liu             const boost::system::error_code& ec,
18045eb468daSGeorge Liu             const dbus::utility::ManagedObjectType& subtree) {
18054bfefa74SGunnar Mills         if (ec)
18064bfefa74SGunnar Mills         {
180762598e31SEd Tanous             BMCWEB_LOG_DEBUG("D-Bus response error getting objects.");
1808ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
18094bfefa74SGunnar Mills             return;
18104bfefa74SGunnar Mills         }
18114bfefa74SGunnar Mills 
181226f6976fSEd Tanous         if (subtree.empty())
18134bfefa74SGunnar Mills         {
181462598e31SEd Tanous             BMCWEB_LOG_DEBUG("Can't find image!");
1815ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
18164bfefa74SGunnar Mills             return;
18174bfefa74SGunnar Mills         }
18184bfefa74SGunnar Mills 
18194bfefa74SGunnar Mills         bool foundImage = false;
182002cad96eSEd Tanous         for (const auto& object : subtree)
18214bfefa74SGunnar Mills         {
18224bfefa74SGunnar Mills             const std::string& path =
18234bfefa74SGunnar Mills                 static_cast<const std::string&>(object.first);
1824f23b7296SEd Tanous             std::size_t idPos2 = path.rfind('/');
18254bfefa74SGunnar Mills 
18264bfefa74SGunnar Mills             if (idPos2 == std::string::npos)
18274bfefa74SGunnar Mills             {
18284bfefa74SGunnar Mills                 continue;
18294bfefa74SGunnar Mills             }
18304bfefa74SGunnar Mills 
18314bfefa74SGunnar Mills             idPos2++;
18324bfefa74SGunnar Mills             if (idPos2 >= path.size())
18334bfefa74SGunnar Mills             {
18344bfefa74SGunnar Mills                 continue;
18354bfefa74SGunnar Mills             }
18364bfefa74SGunnar Mills 
18374bfefa74SGunnar Mills             if (path.substr(idPos2) == firmwareId)
18384bfefa74SGunnar Mills             {
18394bfefa74SGunnar Mills                 foundImage = true;
18404bfefa74SGunnar Mills                 break;
18414bfefa74SGunnar Mills             }
18424bfefa74SGunnar Mills         }
18434bfefa74SGunnar Mills 
18444bfefa74SGunnar Mills         if (!foundImage)
18454bfefa74SGunnar Mills         {
1846ac106bf6SEd Tanous             messages::propertyValueNotInList(
1847ac106bf6SEd Tanous                 asyncResp->res, runningFirmwareTarget, "@odata.id");
184862598e31SEd Tanous             BMCWEB_LOG_DEBUG("Invalid firmware ID.");
18494bfefa74SGunnar Mills             return;
18504bfefa74SGunnar Mills         }
18514bfefa74SGunnar Mills 
185262598e31SEd Tanous         BMCWEB_LOG_DEBUG("Setting firmware version {} to priority 0.",
185362598e31SEd Tanous                          firmwareId);
18544bfefa74SGunnar Mills 
18554bfefa74SGunnar Mills         // Only support Immediate
18564bfefa74SGunnar Mills         // An addition could be a Redfish Setting like
18574bfefa74SGunnar Mills         // ActiveSoftwareImageApplyTime and support OnReset
18589ae226faSGeorge Liu         sdbusplus::asio::setProperty(
18599ae226faSGeorge Liu             *crow::connections::systemBus,
18609ae226faSGeorge Liu             "xyz.openbmc_project.Software.BMC.Updater",
18619ae226faSGeorge Liu             "/xyz/openbmc_project/software/" + firmwareId,
18629ae226faSGeorge Liu             "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
18639ae226faSGeorge Liu             static_cast<uint8_t>(0),
1864ac106bf6SEd Tanous             [asyncResp](const boost::system::error_code& ec2) {
18658a592810SEd Tanous             if (ec2)
18664bfefa74SGunnar Mills             {
186762598e31SEd Tanous                 BMCWEB_LOG_DEBUG("D-Bus response error setting.");
1868ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
18694bfefa74SGunnar Mills                 return;
18704bfefa74SGunnar Mills             }
1871ac106bf6SEd Tanous             doBMCGracefulRestart(asyncResp);
18729ae226faSGeorge Liu         });
18735eb468daSGeorge Liu     });
18744bfefa74SGunnar Mills }
18754bfefa74SGunnar Mills 
1876c51afd54SEd Tanous inline void
1877c51afd54SEd Tanous     afterSetDateTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1878c51afd54SEd Tanous                      const boost::system::error_code& ec,
1879c51afd54SEd Tanous                      const sdbusplus::message_t& msg)
1880c51afd54SEd Tanous {
1881c51afd54SEd Tanous     if (ec)
1882c51afd54SEd Tanous     {
1883c51afd54SEd Tanous         BMCWEB_LOG_DEBUG("Failed to set elapsed time. DBUS response error {}",
1884c51afd54SEd Tanous                          ec);
1885c51afd54SEd Tanous         const sd_bus_error* dbusError = msg.get_error();
1886c51afd54SEd Tanous         if (dbusError != nullptr)
1887c51afd54SEd Tanous         {
1888c51afd54SEd Tanous             std::string_view errorName(dbusError->name);
1889c51afd54SEd Tanous             if (errorName ==
1890c51afd54SEd Tanous                 "org.freedesktop.timedate1.AutomaticTimeSyncEnabled")
1891c51afd54SEd Tanous             {
1892c51afd54SEd Tanous                 BMCWEB_LOG_DEBUG("Setting conflict");
1893c51afd54SEd Tanous                 messages::propertyValueConflict(
1894c51afd54SEd Tanous                     asyncResp->res, "DateTime",
1895c51afd54SEd Tanous                     "Managers/NetworkProtocol/NTPProcotolEnabled");
1896c51afd54SEd Tanous                 return;
1897c51afd54SEd Tanous             }
1898c51afd54SEd Tanous         }
1899c51afd54SEd Tanous         messages::internalError(asyncResp->res);
1900c51afd54SEd Tanous         return;
1901c51afd54SEd Tanous     }
1902c51afd54SEd Tanous     asyncResp->res.result(boost::beast::http::status::no_content);
1903c51afd54SEd Tanous }
1904c51afd54SEd Tanous 
1905c51afd54SEd Tanous inline void setDateTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1906c51afd54SEd Tanous                         const std::string& datetime)
1907af5d6058SSantosh Puranik {
190862598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set date time: {}", datetime);
1909af5d6058SSantosh Puranik 
1910c2e32007SEd Tanous     std::optional<redfish::time_utils::usSinceEpoch> us =
1911c2e32007SEd Tanous         redfish::time_utils::dateStringToEpoch(datetime);
1912c2e32007SEd Tanous     if (!us)
1913af5d6058SSantosh Puranik     {
1914ac106bf6SEd Tanous         messages::propertyValueFormatError(asyncResp->res, datetime,
1915ac106bf6SEd Tanous                                            "DateTime");
1916c2e32007SEd Tanous         return;
1917c2e32007SEd Tanous     }
1918c51afd54SEd Tanous     // Set the absolute datetime
1919c51afd54SEd Tanous     bool relative = false;
1920c51afd54SEd Tanous     bool interactive = false;
1921c51afd54SEd Tanous     crow::connections::systemBus->async_method_call(
1922c51afd54SEd Tanous         [asyncResp](const boost::system::error_code& ec,
1923c51afd54SEd Tanous                     const sdbusplus::message_t& msg) {
1924c51afd54SEd Tanous         afterSetDateTime(asyncResp, ec, msg);
1925c51afd54SEd Tanous     },
1926c51afd54SEd Tanous         "org.freedesktop.timedate1", "/org/freedesktop/timedate1",
1927c51afd54SEd Tanous         "org.freedesktop.timedate1", "SetTime", us->count(), relative,
1928c51afd54SEd Tanous         interactive);
192983ff9ab6SJames Feist }
19309c310685SBorawski.Lukasz 
193175815e5cSEd Tanous inline void
193275815e5cSEd Tanous     checkForQuiesced(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
193375815e5cSEd Tanous {
193475815e5cSEd Tanous     sdbusplus::asio::getProperty<std::string>(
193575815e5cSEd Tanous         *crow::connections::systemBus, "org.freedesktop.systemd1",
193675815e5cSEd Tanous         "/org/freedesktop/systemd1/unit/obmc-bmc-service-quiesce@0.target",
193775815e5cSEd Tanous         "org.freedesktop.systemd1.Unit", "ActiveState",
193875815e5cSEd Tanous         [asyncResp](const boost::system::error_code& ec,
193975815e5cSEd Tanous                     const std::string& val) {
194075815e5cSEd Tanous         if (!ec)
194175815e5cSEd Tanous         {
194275815e5cSEd Tanous             if (val == "active")
194375815e5cSEd Tanous             {
1944*539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Status"]["Health"] =
1945*539d8c6bSEd Tanous                     resource::Health::Critical;
1946*539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Status"]["State"] =
1947*539d8c6bSEd Tanous                     resource::State::Quiesced;
194875815e5cSEd Tanous                 return;
194975815e5cSEd Tanous             }
195075815e5cSEd Tanous         }
1951*539d8c6bSEd Tanous         asyncResp->res.jsonValue["Status"]["Health"] = resource::Health::OK;
1952*539d8c6bSEd Tanous         asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
195375815e5cSEd Tanous     });
195475815e5cSEd Tanous }
195575815e5cSEd Tanous 
19567e860f15SJohn Edward Broadbent inline void requestRoutesManager(App& app)
19577e860f15SJohn Edward Broadbent {
19587e860f15SJohn Edward Broadbent     std::string uuid = persistent_data::getConfig().systemUuid;
19599c310685SBorawski.Lukasz 
1960253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/")
1961ed398213SEd Tanous         .privileges(redfish::privileges::getManager)
1962002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1963002d39b4SEd Tanous             [&app, uuid](const crow::Request& req,
1964253f11b8SEd Tanous                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1965253f11b8SEd Tanous                          const std::string& managerId) {
19663ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
196745ca1b86SEd Tanous         {
196845ca1b86SEd Tanous             return;
196945ca1b86SEd Tanous         }
1970253f11b8SEd Tanous 
1971253f11b8SEd Tanous         if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
1972253f11b8SEd Tanous         {
1973253f11b8SEd Tanous             messages::resourceNotFound(asyncResp->res, "Manager", managerId);
1974253f11b8SEd Tanous             return;
1975253f11b8SEd Tanous         }
1976253f11b8SEd Tanous 
1977253f11b8SEd Tanous         asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1978253f11b8SEd Tanous             "/redfish/v1/Managers/{}", BMCWEB_REDFISH_MANAGER_URI_NAME);
1979a51fc2d2SSui Chen         asyncResp->res.jsonValue["@odata.type"] = "#Manager.v1_14_0.Manager";
1980253f11b8SEd Tanous         asyncResp->res.jsonValue["Id"] = BMCWEB_REDFISH_MANAGER_URI_NAME;
19817e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
19827e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["Description"] =
19837e860f15SJohn Edward Broadbent             "Baseboard Management Controller";
1984*539d8c6bSEd Tanous         asyncResp->res.jsonValue["PowerState"] = resource::PowerState::On;
19851476687dSEd Tanous 
1986*539d8c6bSEd Tanous         asyncResp->res.jsonValue["ManagerType"] = manager::ManagerType::BMC;
19877e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
19887e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1989002d39b4SEd Tanous         asyncResp->res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
19907e860f15SJohn Edward Broadbent 
19911476687dSEd Tanous         asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
1992253f11b8SEd Tanous             boost::urls::format("/redfish/v1/Managers/{}/LogServices",
1993253f11b8SEd Tanous                                 BMCWEB_REDFISH_MANAGER_URI_NAME);
19941476687dSEd Tanous         asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
1995253f11b8SEd Tanous             boost::urls::format("/redfish/v1/Managers/{}/NetworkProtocol",
1996253f11b8SEd Tanous                                 BMCWEB_REDFISH_MANAGER_URI_NAME);
19971476687dSEd Tanous         asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
1998253f11b8SEd Tanous             boost::urls::format("/redfish/v1/Managers/{}/EthernetInterfaces",
1999253f11b8SEd Tanous                                 BMCWEB_REDFISH_MANAGER_URI_NAME);
20007e860f15SJohn Edward Broadbent 
200125b54dbaSEd Tanous         if constexpr (BMCWEB_VM_NBDPROXY)
200236c0f2a3SEd Tanous         {
20031476687dSEd Tanous             asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
2004253f11b8SEd Tanous                 boost::urls::format("/redfish/v1/Managers/{}/VirtualMedia",
2005253f11b8SEd Tanous                                     BMCWEB_REDFISH_MANAGER_URI_NAME);
200636c0f2a3SEd Tanous         }
20077e860f15SJohn Edward Broadbent 
20087e860f15SJohn Edward Broadbent         // default oem data
20097e860f15SJohn Edward Broadbent         nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
20107e860f15SJohn Edward Broadbent         nlohmann::json& oemOpenbmc = oem["OpenBmc"];
2011253f11b8SEd Tanous         oem["@odata.id"] = boost::urls::format("/redfish/v1/Managers/{}#/Oem",
2012253f11b8SEd Tanous                                                BMCWEB_REDFISH_MANAGER_URI_NAME);
2013fc1cdd14SEd Tanous         oemOpenbmc["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager";
2014253f11b8SEd Tanous         oemOpenbmc["@odata.id"] =
2015253f11b8SEd Tanous             boost::urls::format("/redfish/v1/Managers/{}#/Oem/OpenBmc",
2016253f11b8SEd Tanous                                 BMCWEB_REDFISH_MANAGER_URI_NAME);
20171476687dSEd Tanous 
20181476687dSEd Tanous         nlohmann::json::object_t certificates;
2019253f11b8SEd Tanous         certificates["@odata.id"] = boost::urls::format(
2020253f11b8SEd Tanous             "/redfish/v1/Managers/{}/Truststore/Certificates",
2021253f11b8SEd Tanous             BMCWEB_REDFISH_MANAGER_URI_NAME);
20221476687dSEd Tanous         oemOpenbmc["Certificates"] = std::move(certificates);
20237e860f15SJohn Edward Broadbent 
20247e860f15SJohn Edward Broadbent         // Manager.Reset (an action) can be many values, OpenBMC only
20257e860f15SJohn Edward Broadbent         // supports BMC reboot.
20267e860f15SJohn Edward Broadbent         nlohmann::json& managerReset =
20277e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
20287e860f15SJohn Edward Broadbent         managerReset["target"] =
2029253f11b8SEd Tanous             boost::urls::format("/redfish/v1/Managers/{}/Actions/Manager.Reset",
2030253f11b8SEd Tanous                                 BMCWEB_REDFISH_MANAGER_URI_NAME);
20317e860f15SJohn Edward Broadbent         managerReset["@Redfish.ActionInfo"] =
2032253f11b8SEd Tanous             boost::urls::format("/redfish/v1/Managers/{}/ResetActionInfo",
2033253f11b8SEd Tanous                                 BMCWEB_REDFISH_MANAGER_URI_NAME);
20347e860f15SJohn Edward Broadbent 
20357e860f15SJohn Edward Broadbent         // ResetToDefaults (Factory Reset) has values like
20367e860f15SJohn Edward Broadbent         // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
20377e860f15SJohn Edward Broadbent         // on OpenBMC
20387e860f15SJohn Edward Broadbent         nlohmann::json& resetToDefaults =
20397e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
2040253f11b8SEd Tanous         resetToDefaults["target"] = boost::urls::format(
2041253f11b8SEd Tanous             "/redfish/v1/Managers/{}/Actions/Manager.ResetToDefaults",
2042253f11b8SEd Tanous             BMCWEB_REDFISH_MANAGER_URI_NAME);
2043613dabeaSEd Tanous         resetToDefaults["ResetType@Redfish.AllowableValues"] =
2044613dabeaSEd Tanous             nlohmann::json::array_t({"ResetAll"});
20457e860f15SJohn Edward Broadbent 
20467c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
20472b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
20487c8c4058STejas Patil 
20497c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
20507c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
20517c8c4058STejas Patil             redfishDateTimeOffset.second;
20527e860f15SJohn Edward Broadbent 
20530e8ac5e7SGunnar Mills         // TODO (Gunnar): Remove these one day since moved to ComputerSystem
20540e8ac5e7SGunnar Mills         // Still used by OCP profiles
20550e8ac5e7SGunnar Mills         // https://github.com/opencomputeproject/OCP-Profiles/issues/23
20567e860f15SJohn Edward Broadbent         // Fill in SerialConsole info
20577e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
2058002d39b4SEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
2059613dabeaSEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
2060613dabeaSEd Tanous             nlohmann::json::array_t({"IPMI", "SSH"});
206125b54dbaSEd Tanous         if constexpr (BMCWEB_KVM)
206225b54dbaSEd Tanous         {
20637e860f15SJohn Edward Broadbent             // Fill in GraphicalConsole info
206425b54dbaSEd Tanous             asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] =
206525b54dbaSEd Tanous                 true;
206625b54dbaSEd Tanous             asyncResp->res
206725b54dbaSEd Tanous                 .jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4;
206825b54dbaSEd Tanous             asyncResp->res
206925b54dbaSEd Tanous                 .jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
2070613dabeaSEd Tanous                 nlohmann::json::array_t({"KVMIP"});
207125b54dbaSEd Tanous         }
207225b54dbaSEd Tanous         if constexpr (!BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
20737f3e84a1SEd Tanous         {
20747f3e84a1SEd Tanous             asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] =
20757f3e84a1SEd Tanous                 1;
20761476687dSEd Tanous 
20771476687dSEd Tanous             nlohmann::json::array_t managerForServers;
20781476687dSEd Tanous             nlohmann::json::object_t manager;
2079253f11b8SEd Tanous             manager["@odata.id"] = std::format("/redfish/v1/Systems/{}",
2080253f11b8SEd Tanous                                                BMCWEB_REDFISH_SYSTEM_URI_NAME);
2081ad539545SPatrick Williams             managerForServers.emplace_back(std::move(manager));
20821476687dSEd Tanous 
20831476687dSEd Tanous             asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
20841476687dSEd Tanous                 std::move(managerForServers);
20857f3e84a1SEd Tanous         }
20867e860f15SJohn Edward Broadbent 
2087eee0013eSWilly Tu         sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
20887e860f15SJohn Edward Broadbent                                              "FirmwareVersion", true);
20897e860f15SJohn Edward Broadbent 
20907e860f15SJohn Edward Broadbent         managerGetLastResetTime(asyncResp);
20917e860f15SJohn Edward Broadbent 
2092a51fc2d2SSui Chen         // ManagerDiagnosticData is added for all BMCs.
2093a51fc2d2SSui Chen         nlohmann::json& managerDiagnosticData =
2094a51fc2d2SSui Chen             asyncResp->res.jsonValue["ManagerDiagnosticData"];
2095a51fc2d2SSui Chen         managerDiagnosticData["@odata.id"] =
2096253f11b8SEd Tanous             boost::urls::format("/redfish/v1/Managers/{}/ManagerDiagnosticData",
2097253f11b8SEd Tanous                                 BMCWEB_REDFISH_MANAGER_URI_NAME);
2098a51fc2d2SSui Chen 
209925b54dbaSEd Tanous         if constexpr (BMCWEB_REDFISH_OEM_MANAGER_FAN_DATA)
210025b54dbaSEd Tanous         {
21017e860f15SJohn Edward Broadbent             auto pids = std::make_shared<GetPIDValues>(asyncResp);
21027e860f15SJohn Edward Broadbent             pids->run();
210325b54dbaSEd Tanous         }
21047e860f15SJohn Edward Broadbent 
2105002d39b4SEd Tanous         getMainChassisId(asyncResp,
2106002d39b4SEd Tanous                          [](const std::string& chassisId,
2107002d39b4SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2108002d39b4SEd Tanous             aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
21091476687dSEd Tanous             nlohmann::json::array_t managerForChassis;
21108a592810SEd Tanous             nlohmann::json::object_t managerObj;
2111ef4c65b7SEd Tanous             boost::urls::url chassiUrl =
2112ef4c65b7SEd Tanous                 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
2113eddfc437SWilly Tu             managerObj["@odata.id"] = chassiUrl;
2114ad539545SPatrick Williams             managerForChassis.emplace_back(std::move(managerObj));
21151476687dSEd Tanous             aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
21161476687dSEd Tanous                 std::move(managerForChassis);
21171476687dSEd Tanous             aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
2118eddfc437SWilly Tu                 chassiUrl;
21197e860f15SJohn Edward Broadbent         });
21207e860f15SJohn Edward Broadbent 
21211e1e598dSJonathan Doman         sdbusplus::asio::getProperty<double>(
21221e1e598dSJonathan Doman             *crow::connections::systemBus, "org.freedesktop.systemd1",
2123002d39b4SEd Tanous             "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
2124002d39b4SEd Tanous             "Progress",
212575815e5cSEd Tanous             [asyncResp](const boost::system::error_code& ec, double val) {
21267e860f15SJohn Edward Broadbent             if (ec)
21271abe55efSEd Tanous             {
212862598e31SEd Tanous                 BMCWEB_LOG_ERROR("Error while getting progress");
21297e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
21307e860f15SJohn Edward Broadbent                 return;
21317e860f15SJohn Edward Broadbent             }
21321e1e598dSJonathan Doman             if (val < 1.0)
21337e860f15SJohn Edward Broadbent             {
2134*539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Status"]["Health"] =
2135*539d8c6bSEd Tanous                     resource::Health::OK;
2136*539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Status"]["State"] =
2137*539d8c6bSEd Tanous                     resource::State::Starting;
213875815e5cSEd Tanous                 return;
21397e860f15SJohn Edward Broadbent             }
214075815e5cSEd Tanous             checkForQuiesced(asyncResp);
21411e1e598dSJonathan Doman         });
21429c310685SBorawski.Lukasz 
2143e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
2144e99073f5SGeorge Liu             "xyz.openbmc_project.Inventory.Item.Bmc"};
2145e99073f5SGeorge Liu         dbus::utility::getSubTree(
2146e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, interfaces,
21477e860f15SJohn Edward Broadbent             [asyncResp](
2148e99073f5SGeorge Liu                 const boost::system::error_code& ec,
2149b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
21507e860f15SJohn Edward Broadbent             if (ec)
21511abe55efSEd Tanous             {
215262598e31SEd Tanous                 BMCWEB_LOG_DEBUG("D-Bus response error on GetSubTree {}", ec);
21537e860f15SJohn Edward Broadbent                 return;
21547e860f15SJohn Edward Broadbent             }
215526f6976fSEd Tanous             if (subtree.empty())
21567e860f15SJohn Edward Broadbent             {
215762598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Can't find bmc D-Bus object!");
21587e860f15SJohn Edward Broadbent                 return;
21597e860f15SJohn Edward Broadbent             }
21607e860f15SJohn Edward Broadbent             // Assume only 1 bmc D-Bus object
21617e860f15SJohn Edward Broadbent             // Throw an error if there is more than 1
21627e860f15SJohn Edward Broadbent             if (subtree.size() > 1)
21637e860f15SJohn Edward Broadbent             {
216462598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Found more than 1 bmc D-Bus object!");
21657e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
21667e860f15SJohn Edward Broadbent                 return;
21677e860f15SJohn Edward Broadbent             }
21687e860f15SJohn Edward Broadbent 
2169002d39b4SEd Tanous             if (subtree[0].first.empty() || subtree[0].second.size() != 1)
21707e860f15SJohn Edward Broadbent             {
217162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Error getting bmc D-Bus object!");
21727e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
21737e860f15SJohn Edward Broadbent                 return;
21747e860f15SJohn Edward Broadbent             }
21757e860f15SJohn Edward Broadbent 
21767e860f15SJohn Edward Broadbent             const std::string& path = subtree[0].first;
2177002d39b4SEd Tanous             const std::string& connectionName = subtree[0].second[0].first;
21787e860f15SJohn Edward Broadbent 
2179002d39b4SEd Tanous             for (const auto& interfaceName : subtree[0].second[0].second)
21807e860f15SJohn Edward Broadbent             {
21817e860f15SJohn Edward Broadbent                 if (interfaceName ==
21827e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Inventory.Decorator.Asset")
21837e860f15SJohn Edward Broadbent                 {
2184fac6e53bSKrzysztof Grobelny                     sdbusplus::asio::getAllProperties(
2185fac6e53bSKrzysztof Grobelny                         *crow::connections::systemBus, connectionName, path,
2186fac6e53bSKrzysztof Grobelny                         "xyz.openbmc_project.Inventory.Decorator.Asset",
21875e7e2dc5SEd Tanous                         [asyncResp](const boost::system::error_code& ec2,
2188b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap&
21897e860f15SJohn Edward Broadbent                                         propertiesList) {
21908a592810SEd Tanous                         if (ec2)
21917e860f15SJohn Edward Broadbent                         {
219262598e31SEd Tanous                             BMCWEB_LOG_DEBUG("Can't get bmc asset!");
21937e860f15SJohn Edward Broadbent                             return;
21947e860f15SJohn Edward Broadbent                         }
21957e860f15SJohn Edward Broadbent 
2196fac6e53bSKrzysztof Grobelny                         const std::string* partNumber = nullptr;
2197fac6e53bSKrzysztof Grobelny                         const std::string* serialNumber = nullptr;
2198fac6e53bSKrzysztof Grobelny                         const std::string* manufacturer = nullptr;
2199fac6e53bSKrzysztof Grobelny                         const std::string* model = nullptr;
2200fac6e53bSKrzysztof Grobelny                         const std::string* sparePartNumber = nullptr;
2201fac6e53bSKrzysztof Grobelny 
2202fac6e53bSKrzysztof Grobelny                         const bool success = sdbusplus::unpackPropertiesNoThrow(
2203fac6e53bSKrzysztof Grobelny                             dbus_utils::UnpackErrorPrinter(), propertiesList,
2204fac6e53bSKrzysztof Grobelny                             "PartNumber", partNumber, "SerialNumber",
2205fac6e53bSKrzysztof Grobelny                             serialNumber, "Manufacturer", manufacturer, "Model",
2206fac6e53bSKrzysztof Grobelny                             model, "SparePartNumber", sparePartNumber);
2207fac6e53bSKrzysztof Grobelny 
2208fac6e53bSKrzysztof Grobelny                         if (!success)
22097e860f15SJohn Edward Broadbent                         {
2210002d39b4SEd Tanous                             messages::internalError(asyncResp->res);
22117e860f15SJohn Edward Broadbent                             return;
22127e860f15SJohn Edward Broadbent                         }
2213fac6e53bSKrzysztof Grobelny 
2214fac6e53bSKrzysztof Grobelny                         if (partNumber != nullptr)
2215fac6e53bSKrzysztof Grobelny                         {
2216fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["PartNumber"] =
2217fac6e53bSKrzysztof Grobelny                                 *partNumber;
22187e860f15SJohn Edward Broadbent                         }
2219fac6e53bSKrzysztof Grobelny 
2220fac6e53bSKrzysztof Grobelny                         if (serialNumber != nullptr)
2221fac6e53bSKrzysztof Grobelny                         {
2222fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["SerialNumber"] =
2223fac6e53bSKrzysztof Grobelny                                 *serialNumber;
22247e860f15SJohn Edward Broadbent                         }
2225fac6e53bSKrzysztof Grobelny 
2226fac6e53bSKrzysztof Grobelny                         if (manufacturer != nullptr)
2227fac6e53bSKrzysztof Grobelny                         {
2228fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["Manufacturer"] =
2229fac6e53bSKrzysztof Grobelny                                 *manufacturer;
2230fac6e53bSKrzysztof Grobelny                         }
2231fac6e53bSKrzysztof Grobelny 
2232fac6e53bSKrzysztof Grobelny                         if (model != nullptr)
2233fac6e53bSKrzysztof Grobelny                         {
2234fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["Model"] = *model;
2235fac6e53bSKrzysztof Grobelny                         }
2236fac6e53bSKrzysztof Grobelny 
2237fac6e53bSKrzysztof Grobelny                         if (sparePartNumber != nullptr)
2238fac6e53bSKrzysztof Grobelny                         {
2239fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["SparePartNumber"] =
2240fac6e53bSKrzysztof Grobelny                                 *sparePartNumber;
2241fac6e53bSKrzysztof Grobelny                         }
2242fac6e53bSKrzysztof Grobelny                     });
22437e860f15SJohn Edward Broadbent                 }
2244002d39b4SEd Tanous                 else if (interfaceName ==
22450fda0f12SGeorge Liu                          "xyz.openbmc_project.Inventory.Decorator.LocationCode")
22467e860f15SJohn Edward Broadbent                 {
22477e860f15SJohn Edward Broadbent                     getLocation(asyncResp, connectionName, path);
22487e860f15SJohn Edward Broadbent                 }
22497e860f15SJohn Edward Broadbent             }
2250e99073f5SGeorge Liu         });
22517e860f15SJohn Edward Broadbent     });
22527e860f15SJohn Edward Broadbent 
2253253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/")
2254ed398213SEd Tanous         .privileges(redfish::privileges::patchManager)
225545ca1b86SEd Tanous         .methods(boost::beast::http::verb::patch)(
225645ca1b86SEd Tanous             [&app](const crow::Request& req,
2257253f11b8SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2258253f11b8SEd Tanous                    const std::string& managerId) {
22593ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
226045ca1b86SEd Tanous         {
226145ca1b86SEd Tanous             return;
226245ca1b86SEd Tanous         }
2263253f11b8SEd Tanous 
2264253f11b8SEd Tanous         if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2265253f11b8SEd Tanous         {
2266253f11b8SEd Tanous             messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2267253f11b8SEd Tanous             return;
2268253f11b8SEd Tanous         }
2269253f11b8SEd Tanous 
22709e9b6049SEd Tanous         std::optional<std::string> activeSoftwareImageOdataId;
22717e860f15SJohn Edward Broadbent         std::optional<std::string> datetime;
22729e9b6049SEd Tanous         std::optional<nlohmann::json::object_t> pidControllers;
22739e9b6049SEd Tanous         std::optional<nlohmann::json::object_t> fanControllers;
22749e9b6049SEd Tanous         std::optional<nlohmann::json::object_t> fanZones;
22759e9b6049SEd Tanous         std::optional<nlohmann::json::object_t> stepwiseControllers;
22769e9b6049SEd Tanous         std::optional<std::string> profile;
22777e860f15SJohn Edward Broadbent 
22789e9b6049SEd Tanous         // clang-format off
22799e9b6049SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res,
22809e9b6049SEd Tanous               "DateTime", datetime,
22819e9b6049SEd Tanous               "Links/ActiveSoftwareImage/@odata.id", activeSoftwareImageOdataId,
22829e9b6049SEd Tanous               "Oem/OpenBmc/Fan/FanControllers", fanControllers,
22839e9b6049SEd Tanous               "Oem/OpenBmc/Fan/FanZones", fanZones,
22849e9b6049SEd Tanous               "Oem/OpenBmc/Fan/PidControllers", pidControllers,
22859e9b6049SEd Tanous               "Oem/OpenBmc/Fan/Profile", profile,
22869e9b6049SEd Tanous               "Oem/OpenBmc/Fan/StepwiseControllers", stepwiseControllers
22879e9b6049SEd Tanous         ))
22887e860f15SJohn Edward Broadbent         {
22897e860f15SJohn Edward Broadbent             return;
22907e860f15SJohn Edward Broadbent         }
22919e9b6049SEd Tanous         // clang-format on
22927e860f15SJohn Edward Broadbent 
22939e9b6049SEd Tanous         if (pidControllers || fanControllers || fanZones ||
22949e9b6049SEd Tanous             stepwiseControllers || profile)
22957e860f15SJohn Edward Broadbent         {
229625b54dbaSEd Tanous             if constexpr (BMCWEB_REDFISH_OEM_MANAGER_FAN_DATA)
229725b54dbaSEd Tanous             {
229825b54dbaSEd Tanous                 std::vector<std::pair<std::string,
229925b54dbaSEd Tanous                                       std::optional<nlohmann::json::object_t>>>
23009e9b6049SEd Tanous                     configuration;
23019e9b6049SEd Tanous                 if (pidControllers)
23027e860f15SJohn Edward Broadbent                 {
23039e9b6049SEd Tanous                     configuration.emplace_back("PidControllers",
23049e9b6049SEd Tanous                                                std::move(pidControllers));
23057e860f15SJohn Edward Broadbent                 }
23069e9b6049SEd Tanous                 if (fanControllers)
23077e860f15SJohn Edward Broadbent                 {
23089e9b6049SEd Tanous                     configuration.emplace_back("FanControllers",
23099e9b6049SEd Tanous                                                std::move(fanControllers));
23107e860f15SJohn Edward Broadbent                 }
23119e9b6049SEd Tanous                 if (fanZones)
23127e860f15SJohn Edward Broadbent                 {
23139e9b6049SEd Tanous                     configuration.emplace_back("FanZones", std::move(fanZones));
23149e9b6049SEd Tanous                 }
23159e9b6049SEd Tanous                 if (stepwiseControllers)
23169e9b6049SEd Tanous                 {
23179e9b6049SEd Tanous                     configuration.emplace_back("StepwiseControllers",
23189e9b6049SEd Tanous                                                std::move(stepwiseControllers));
23199e9b6049SEd Tanous                 }
23209e9b6049SEd Tanous                 auto pid = std::make_shared<SetPIDValues>(
23219e9b6049SEd Tanous                     asyncResp, std::move(configuration), profile);
23227e860f15SJohn Edward Broadbent                 pid->run();
232325b54dbaSEd Tanous             }
232425b54dbaSEd Tanous             else
232525b54dbaSEd Tanous             {
232654dce7f5SGunnar Mills                 messages::propertyUnknown(asyncResp->res, "Oem");
232754dce7f5SGunnar Mills                 return;
232825b54dbaSEd Tanous             }
23297e860f15SJohn Edward Broadbent         }
23309e9b6049SEd Tanous 
23319e9b6049SEd Tanous         if (activeSoftwareImageOdataId)
23327e860f15SJohn Edward Broadbent         {
23339e9b6049SEd Tanous             setActiveFirmwareImage(asyncResp, *activeSoftwareImageOdataId);
23347e860f15SJohn Edward Broadbent         }
23357e860f15SJohn Edward Broadbent 
23367e860f15SJohn Edward Broadbent         if (datetime)
23377e860f15SJohn Edward Broadbent         {
2338c51afd54SEd Tanous             setDateTime(asyncResp, *datetime);
23397e860f15SJohn Edward Broadbent         }
23407e860f15SJohn Edward Broadbent     });
23417e860f15SJohn Edward Broadbent }
23427e860f15SJohn Edward Broadbent 
23437e860f15SJohn Edward Broadbent inline void requestRoutesManagerCollection(App& app)
23447e860f15SJohn Edward Broadbent {
23457e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
2346ed398213SEd Tanous         .privileges(redfish::privileges::getManagerCollection)
23477e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
234845ca1b86SEd Tanous             [&app](const crow::Request& req,
23497e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
23503ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
235145ca1b86SEd Tanous         {
235245ca1b86SEd Tanous             return;
235345ca1b86SEd Tanous         }
235483ff9ab6SJames Feist         // Collections don't include the static data added by SubRoute
235583ff9ab6SJames Feist         // because it has a duplicate entry for members
23568d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
23578d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
23588d1b46d7Szhanghch05             "#ManagerCollection.ManagerCollection";
23598d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Manager Collection";
23608d1b46d7Szhanghch05         asyncResp->res.jsonValue["Members@odata.count"] = 1;
23611476687dSEd Tanous         nlohmann::json::array_t members;
23621476687dSEd Tanous         nlohmann::json& bmc = members.emplace_back();
2363253f11b8SEd Tanous         bmc["@odata.id"] = boost::urls::format("/redfish/v1/Managers/{}",
2364253f11b8SEd Tanous                                                BMCWEB_REDFISH_MANAGER_URI_NAME);
23651476687dSEd Tanous         asyncResp->res.jsonValue["Members"] = std::move(members);
23667e860f15SJohn Edward Broadbent     });
23679c310685SBorawski.Lukasz }
23689c310685SBorawski.Lukasz } // namespace redfish
2369