xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision 36c0f2a35e670a4b798b7b42fd18455085e9d9c0)
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"
22a51fc2d2SSui Chen #include "query.hpp"
23c5d03ff4SJennifer Lee #include "redfish_util.hpp"
24a51fc2d2SSui Chen #include "registries/privilege_registry.hpp"
25fac6e53bSKrzysztof Grobelny #include "utils/dbus_utils.hpp"
263ccb3adbSEd Tanous #include "utils/json_utils.hpp"
27a51fc2d2SSui Chen #include "utils/sw_utils.hpp"
28a51fc2d2SSui Chen #include "utils/systemd_utils.hpp"
292b82937eSEd Tanous #include "utils/time_utils.hpp"
309c310685SBorawski.Lukasz 
31e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
32ef4c65b7SEd Tanous #include <boost/url/format.hpp>
33fac6e53bSKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
34fac6e53bSKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
351214b7e7SGunnar Mills 
36a170f275SEd Tanous #include <algorithm>
37e99073f5SGeorge Liu #include <array>
384bfefa74SGunnar Mills #include <cstdint>
391214b7e7SGunnar Mills #include <memory>
409970e93fSKonstantin Aladyshev #include <optional>
413544d2a7SEd Tanous #include <ranges>
421214b7e7SGunnar Mills #include <sstream>
439970e93fSKonstantin Aladyshev #include <string>
44e99073f5SGeorge Liu #include <string_view>
45abf2add6SEd Tanous #include <variant>
465b4aa86bSJames Feist 
471abe55efSEd Tanous namespace redfish
481abe55efSEd Tanous {
49ed5befbdSJennifer Lee 
50ed5befbdSJennifer Lee /**
512a5c4407SGunnar Mills  * Function reboots the BMC.
522a5c4407SGunnar Mills  *
532a5c4407SGunnar Mills  * @param[in] asyncResp - Shared pointer for completing asynchronous calls
54ed5befbdSJennifer Lee  */
558d1b46d7Szhanghch05 inline void
568d1b46d7Szhanghch05     doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
57ed5befbdSJennifer Lee {
58ed5befbdSJennifer Lee     const char* processName = "xyz.openbmc_project.State.BMC";
59ed5befbdSJennifer Lee     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
60ed5befbdSJennifer Lee     const char* interfaceName = "xyz.openbmc_project.State.BMC";
61ed5befbdSJennifer Lee     const std::string& propertyValue =
62ed5befbdSJennifer Lee         "xyz.openbmc_project.State.BMC.Transition.Reboot";
63ed5befbdSJennifer Lee     const char* destProperty = "RequestedBMCTransition";
64ed5befbdSJennifer Lee 
65ed5befbdSJennifer Lee     // Create the D-Bus variant for D-Bus call.
669ae226faSGeorge Liu     sdbusplus::asio::setProperty(
679ae226faSGeorge Liu         *crow::connections::systemBus, processName, objectPath, interfaceName,
689ae226faSGeorge Liu         destProperty, propertyValue,
695e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
70ed5befbdSJennifer Lee         // Use "Set" method to set the property value.
71ed5befbdSJennifer Lee         if (ec)
72ed5befbdSJennifer Lee         {
7362598e31SEd Tanous             BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
74ed5befbdSJennifer Lee             messages::internalError(asyncResp->res);
75ed5befbdSJennifer Lee             return;
76ed5befbdSJennifer Lee         }
77ed5befbdSJennifer Lee 
78ed5befbdSJennifer Lee         messages::success(asyncResp->res);
799ae226faSGeorge Liu     });
80ed5befbdSJennifer Lee }
812a5c4407SGunnar Mills 
828d1b46d7Szhanghch05 inline void
838d1b46d7Szhanghch05     doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
84f92af389SJayaprakash Mutyala {
85f92af389SJayaprakash Mutyala     const char* processName = "xyz.openbmc_project.State.BMC";
86f92af389SJayaprakash Mutyala     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
87f92af389SJayaprakash Mutyala     const char* interfaceName = "xyz.openbmc_project.State.BMC";
88f92af389SJayaprakash Mutyala     const std::string& propertyValue =
89f92af389SJayaprakash Mutyala         "xyz.openbmc_project.State.BMC.Transition.HardReboot";
90f92af389SJayaprakash Mutyala     const char* destProperty = "RequestedBMCTransition";
91f92af389SJayaprakash Mutyala 
92f92af389SJayaprakash Mutyala     // Create the D-Bus variant for D-Bus call.
939ae226faSGeorge Liu     sdbusplus::asio::setProperty(
949ae226faSGeorge Liu         *crow::connections::systemBus, processName, objectPath, interfaceName,
959ae226faSGeorge Liu         destProperty, propertyValue,
965e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
97f92af389SJayaprakash Mutyala         // Use "Set" method to set the property value.
98f92af389SJayaprakash Mutyala         if (ec)
99f92af389SJayaprakash Mutyala         {
10062598e31SEd Tanous             BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
101f92af389SJayaprakash Mutyala             messages::internalError(asyncResp->res);
102f92af389SJayaprakash Mutyala             return;
103f92af389SJayaprakash Mutyala         }
104f92af389SJayaprakash Mutyala 
105f92af389SJayaprakash Mutyala         messages::success(asyncResp->res);
1069ae226faSGeorge Liu     });
107f92af389SJayaprakash Mutyala }
108f92af389SJayaprakash Mutyala 
1092a5c4407SGunnar Mills /**
1102a5c4407SGunnar Mills  * ManagerResetAction class supports the POST method for the Reset (reboot)
1112a5c4407SGunnar Mills  * action.
1122a5c4407SGunnar Mills  */
1137e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetAction(App& app)
1142a5c4407SGunnar Mills {
1152a5c4407SGunnar Mills     /**
1162a5c4407SGunnar Mills      * Function handles POST method request.
1172a5c4407SGunnar Mills      * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
118f92af389SJayaprakash Mutyala      * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
1192a5c4407SGunnar Mills      */
1207e860f15SJohn Edward Broadbent 
1217e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
122ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
1237e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
12445ca1b86SEd Tanous             [&app](const crow::Request& req,
1257e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1263ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
12745ca1b86SEd Tanous         {
12845ca1b86SEd Tanous             return;
12945ca1b86SEd Tanous         }
13062598e31SEd Tanous         BMCWEB_LOG_DEBUG("Post Manager Reset.");
1312a5c4407SGunnar Mills 
1322a5c4407SGunnar Mills         std::string resetType;
1332a5c4407SGunnar Mills 
13415ed6780SWilly Tu         if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
1357e860f15SJohn Edward Broadbent                                        resetType))
1362a5c4407SGunnar Mills         {
1372a5c4407SGunnar Mills             return;
1382a5c4407SGunnar Mills         }
1392a5c4407SGunnar Mills 
140f92af389SJayaprakash Mutyala         if (resetType == "GracefulRestart")
141f92af389SJayaprakash Mutyala         {
14262598e31SEd Tanous             BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
143f92af389SJayaprakash Mutyala             doBMCGracefulRestart(asyncResp);
144f92af389SJayaprakash Mutyala             return;
145f92af389SJayaprakash Mutyala         }
1463174e4dfSEd Tanous         if (resetType == "ForceRestart")
147f92af389SJayaprakash Mutyala         {
14862598e31SEd Tanous             BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
149f92af389SJayaprakash Mutyala             doBMCForceRestart(asyncResp);
150f92af389SJayaprakash Mutyala             return;
151f92af389SJayaprakash Mutyala         }
15262598e31SEd Tanous         BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}", resetType);
1532a5c4407SGunnar Mills         messages::actionParameterNotSupported(asyncResp->res, resetType,
1542a5c4407SGunnar Mills                                               "ResetType");
1552a5c4407SGunnar Mills 
1562a5c4407SGunnar Mills         return;
1577e860f15SJohn Edward Broadbent     });
1582a5c4407SGunnar Mills }
159ed5befbdSJennifer Lee 
1603e40fc74SGunnar Mills /**
1613e40fc74SGunnar Mills  * ManagerResetToDefaultsAction class supports POST method for factory reset
1623e40fc74SGunnar Mills  * action.
1633e40fc74SGunnar Mills  */
1647e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetToDefaultsAction(App& app)
1653e40fc74SGunnar Mills {
1663e40fc74SGunnar Mills     /**
1673e40fc74SGunnar Mills      * Function handles ResetToDefaults POST method request.
1683e40fc74SGunnar Mills      *
1693e40fc74SGunnar Mills      * Analyzes POST body message and factory resets BMC by calling
1703e40fc74SGunnar Mills      * BMC code updater factory reset followed by a BMC reboot.
1713e40fc74SGunnar Mills      *
1723e40fc74SGunnar Mills      * BMC code updater factory reset wipes the whole BMC read-write
1733e40fc74SGunnar Mills      * filesystem which includes things like the network settings.
1743e40fc74SGunnar Mills      *
1753e40fc74SGunnar Mills      * OpenBMC only supports ResetToDefaultsType "ResetAll".
1763e40fc74SGunnar Mills      */
1777e860f15SJohn Edward Broadbent 
1787e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
1797e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/")
180ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
1817e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
18245ca1b86SEd Tanous             [&app](const crow::Request& req,
1837e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1843ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
18545ca1b86SEd Tanous         {
18645ca1b86SEd Tanous             return;
18745ca1b86SEd Tanous         }
18862598e31SEd Tanous         BMCWEB_LOG_DEBUG("Post ResetToDefaults.");
1893e40fc74SGunnar Mills 
1909970e93fSKonstantin Aladyshev         std::optional<std::string> resetType;
1919970e93fSKonstantin Aladyshev         std::optional<std::string> resetToDefaultsType;
1923e40fc74SGunnar Mills 
1939970e93fSKonstantin Aladyshev         if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
1949970e93fSKonstantin Aladyshev                                        resetType, "ResetToDefaultsType",
1959970e93fSKonstantin Aladyshev                                        resetToDefaultsType))
1963e40fc74SGunnar Mills         {
1979970e93fSKonstantin Aladyshev             BMCWEB_LOG_DEBUG("Missing property ResetType.");
1983e40fc74SGunnar Mills 
199002d39b4SEd Tanous             messages::actionParameterMissing(asyncResp->res, "ResetToDefaults",
2009970e93fSKonstantin Aladyshev                                              "ResetType");
2013e40fc74SGunnar Mills             return;
2023e40fc74SGunnar Mills         }
2033e40fc74SGunnar Mills 
2049970e93fSKonstantin Aladyshev         if (resetToDefaultsType && !resetType)
2059970e93fSKonstantin Aladyshev         {
2069970e93fSKonstantin Aladyshev             BMCWEB_LOG_WARNING(
2079970e93fSKonstantin Aladyshev                 "Using deprecated ResetToDefaultsType, should be ResetType."
2089970e93fSKonstantin Aladyshev                 "Support for the ResetToDefaultsType will be dropped in 2Q24");
2099970e93fSKonstantin Aladyshev             resetType = resetToDefaultsType;
2109970e93fSKonstantin Aladyshev         }
2119970e93fSKonstantin Aladyshev 
2123e40fc74SGunnar Mills         if (resetType != "ResetAll")
2133e40fc74SGunnar Mills         {
2149970e93fSKonstantin Aladyshev             BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}",
2159970e93fSKonstantin Aladyshev                              *resetType);
2169970e93fSKonstantin Aladyshev             messages::actionParameterNotSupported(asyncResp->res, *resetType,
2179970e93fSKonstantin Aladyshev                                                   "ResetType");
2183e40fc74SGunnar Mills             return;
2193e40fc74SGunnar Mills         }
2203e40fc74SGunnar Mills 
2213e40fc74SGunnar Mills         crow::connections::systemBus->async_method_call(
2225e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
2233e40fc74SGunnar Mills             if (ec)
2243e40fc74SGunnar Mills             {
22562598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Failed to ResetToDefaults: {}", ec);
2263e40fc74SGunnar Mills                 messages::internalError(asyncResp->res);
2273e40fc74SGunnar Mills                 return;
2283e40fc74SGunnar Mills             }
2293e40fc74SGunnar Mills             // Factory Reset doesn't actually happen until a reboot
2303e40fc74SGunnar Mills             // Can't erase what the BMC is running on
2313e40fc74SGunnar Mills             doBMCGracefulRestart(asyncResp);
2323e40fc74SGunnar Mills         },
2333e40fc74SGunnar Mills             "xyz.openbmc_project.Software.BMC.Updater",
2343e40fc74SGunnar Mills             "/xyz/openbmc_project/software",
2353e40fc74SGunnar Mills             "xyz.openbmc_project.Common.FactoryReset", "Reset");
2367e860f15SJohn Edward Broadbent     });
2373e40fc74SGunnar Mills }
2383e40fc74SGunnar Mills 
2391cb1a9e6SAppaRao Puli /**
2401cb1a9e6SAppaRao Puli  * ManagerResetActionInfo derived class for delivering Manager
2411cb1a9e6SAppaRao Puli  * ResetType AllowableValues using ResetInfo schema.
2421cb1a9e6SAppaRao Puli  */
2437e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetActionInfo(App& app)
2441cb1a9e6SAppaRao Puli {
2451cb1a9e6SAppaRao Puli     /**
2461cb1a9e6SAppaRao Puli      * Functions triggers appropriate requests on DBus
2471cb1a9e6SAppaRao Puli      */
2487e860f15SJohn Edward Broadbent 
2497e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/ResetActionInfo/")
250ed398213SEd Tanous         .privileges(redfish::privileges::getActionInfo)
2517e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
25245ca1b86SEd Tanous             [&app](const crow::Request& req,
2537e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2543ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
25545ca1b86SEd Tanous         {
25645ca1b86SEd Tanous             return;
25745ca1b86SEd Tanous         }
2581476687dSEd Tanous 
2591476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
2601476687dSEd Tanous             "#ActionInfo.v1_1_2.ActionInfo";
2611476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
2621476687dSEd Tanous             "/redfish/v1/Managers/bmc/ResetActionInfo";
2631476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "Reset Action Info";
2641476687dSEd Tanous         asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
2651476687dSEd Tanous         nlohmann::json::object_t parameter;
2661476687dSEd Tanous         parameter["Name"] = "ResetType";
2671476687dSEd Tanous         parameter["Required"] = true;
2681476687dSEd Tanous         parameter["DataType"] = "String";
2691476687dSEd Tanous 
2701476687dSEd Tanous         nlohmann::json::array_t allowableValues;
271ad539545SPatrick Williams         allowableValues.emplace_back("GracefulRestart");
272ad539545SPatrick Williams         allowableValues.emplace_back("ForceRestart");
2731476687dSEd Tanous         parameter["AllowableValues"] = std::move(allowableValues);
2741476687dSEd Tanous 
2751476687dSEd Tanous         nlohmann::json::array_t parameters;
276ad539545SPatrick Williams         parameters.emplace_back(std::move(parameter));
2771476687dSEd Tanous 
2781476687dSEd Tanous         asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
2797e860f15SJohn Edward Broadbent     });
2801cb1a9e6SAppaRao Puli }
2811cb1a9e6SAppaRao Puli 
2825b4aa86bSJames Feist static constexpr const char* objectManagerIface =
2835b4aa86bSJames Feist     "org.freedesktop.DBus.ObjectManager";
2845b4aa86bSJames Feist static constexpr const char* pidConfigurationIface =
2855b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid";
2865b4aa86bSJames Feist static constexpr const char* pidZoneConfigurationIface =
2875b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid.Zone";
288b7a08d04SJames Feist static constexpr const char* stepwiseConfigurationIface =
289b7a08d04SJames Feist     "xyz.openbmc_project.Configuration.Stepwise";
29073df0db0SJames Feist static constexpr const char* thermalModeIface =
29173df0db0SJames Feist     "xyz.openbmc_project.Control.ThermalMode";
2929c310685SBorawski.Lukasz 
2938d1b46d7Szhanghch05 inline void
2948d1b46d7Szhanghch05     asyncPopulatePid(const std::string& connection, const std::string& path,
29573df0db0SJames Feist                      const std::string& currentProfile,
29673df0db0SJames Feist                      const std::vector<std::string>& supportedProfiles,
2978d1b46d7Szhanghch05                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2985b4aa86bSJames Feist {
2995eb468daSGeorge Liu     sdbusplus::message::object_path objPath(path);
3005eb468daSGeorge Liu     dbus::utility::getManagedObjects(
3015eb468daSGeorge Liu         connection, objPath,
30273df0db0SJames Feist         [asyncResp, currentProfile, supportedProfiles](
3035e7e2dc5SEd Tanous             const boost::system::error_code& ec,
3045b4aa86bSJames Feist             const dbus::utility::ManagedObjectType& managedObj) {
3055b4aa86bSJames Feist         if (ec)
3065b4aa86bSJames Feist         {
30762598e31SEd Tanous             BMCWEB_LOG_ERROR("{}", ec);
308f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
3095b4aa86bSJames Feist             return;
3105b4aa86bSJames Feist         }
3115b4aa86bSJames Feist         nlohmann::json& configRoot =
3125b4aa86bSJames Feist             asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
3135b4aa86bSJames Feist         nlohmann::json& fans = configRoot["FanControllers"];
3145b4aa86bSJames Feist         fans["@odata.type"] = "#OemManager.FanControllers";
3150fda0f12SGeorge Liu         fans["@odata.id"] =
3160fda0f12SGeorge Liu             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers";
3175b4aa86bSJames Feist 
3185b4aa86bSJames Feist         nlohmann::json& pids = configRoot["PidControllers"];
3195b4aa86bSJames Feist         pids["@odata.type"] = "#OemManager.PidControllers";
3205b4aa86bSJames Feist         pids["@odata.id"] =
3215b4aa86bSJames Feist             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
3225b4aa86bSJames Feist 
323b7a08d04SJames Feist         nlohmann::json& stepwise = configRoot["StepwiseControllers"];
324b7a08d04SJames Feist         stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
325b7a08d04SJames Feist         stepwise["@odata.id"] =
326b7a08d04SJames Feist             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
327b7a08d04SJames Feist 
3285b4aa86bSJames Feist         nlohmann::json& zones = configRoot["FanZones"];
3295b4aa86bSJames Feist         zones["@odata.id"] =
3305b4aa86bSJames Feist             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
3315b4aa86bSJames Feist         zones["@odata.type"] = "#OemManager.FanZones";
332002d39b4SEd Tanous         configRoot["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
3335b4aa86bSJames Feist         configRoot["@odata.type"] = "#OemManager.Fan";
33473df0db0SJames Feist         configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
33573df0db0SJames Feist 
33673df0db0SJames Feist         if (!currentProfile.empty())
33773df0db0SJames Feist         {
33873df0db0SJames Feist             configRoot["Profile"] = currentProfile;
33973df0db0SJames Feist         }
340bf2ddedeSCarson Labrado         BMCWEB_LOG_DEBUG("profile = {} !", currentProfile);
3415b4aa86bSJames Feist 
3425b4aa86bSJames Feist         for (const auto& pathPair : managedObj)
3435b4aa86bSJames Feist         {
3445b4aa86bSJames Feist             for (const auto& intfPair : pathPair.second)
3455b4aa86bSJames Feist             {
3465b4aa86bSJames Feist                 if (intfPair.first != pidConfigurationIface &&
347b7a08d04SJames Feist                     intfPair.first != pidZoneConfigurationIface &&
348b7a08d04SJames Feist                     intfPair.first != stepwiseConfigurationIface)
3495b4aa86bSJames Feist                 {
3505b4aa86bSJames Feist                     continue;
3515b4aa86bSJames Feist                 }
35273df0db0SJames Feist 
353711ac7a9SEd Tanous                 std::string name;
354711ac7a9SEd Tanous 
355711ac7a9SEd Tanous                 for (const std::pair<std::string,
356002d39b4SEd Tanous                                      dbus::utility::DbusVariantType>& propPair :
357002d39b4SEd Tanous                      intfPair.second)
358711ac7a9SEd Tanous                 {
359711ac7a9SEd Tanous                     if (propPair.first == "Name")
360711ac7a9SEd Tanous                     {
3615b4aa86bSJames Feist                         const std::string* namePtr =
362711ac7a9SEd Tanous                             std::get_if<std::string>(&propPair.second);
3635b4aa86bSJames Feist                         if (namePtr == nullptr)
3645b4aa86bSJames Feist                         {
36562598e31SEd Tanous                             BMCWEB_LOG_ERROR("Pid Name Field illegal");
366b7a08d04SJames Feist                             messages::internalError(asyncResp->res);
3675b4aa86bSJames Feist                             return;
3685b4aa86bSJames Feist                         }
369db697703SWilly Tu                         name = *namePtr;
3705b4aa86bSJames Feist                         dbus::utility::escapePathForDbus(name);
371711ac7a9SEd Tanous                     }
372711ac7a9SEd Tanous                     else if (propPair.first == "Profiles")
37373df0db0SJames Feist                     {
37473df0db0SJames Feist                         const std::vector<std::string>* profiles =
37573df0db0SJames Feist                             std::get_if<std::vector<std::string>>(
376711ac7a9SEd Tanous                                 &propPair.second);
37773df0db0SJames Feist                         if (profiles == nullptr)
37873df0db0SJames Feist                         {
37962598e31SEd Tanous                             BMCWEB_LOG_ERROR("Pid Profiles Field illegal");
38073df0db0SJames Feist                             messages::internalError(asyncResp->res);
38173df0db0SJames Feist                             return;
38273df0db0SJames Feist                         }
38373df0db0SJames Feist                         if (std::find(profiles->begin(), profiles->end(),
38473df0db0SJames Feist                                       currentProfile) == profiles->end())
38573df0db0SJames Feist                         {
38662598e31SEd Tanous                             BMCWEB_LOG_INFO(
38762598e31SEd Tanous                                 "{} not supported in current profile", name);
38873df0db0SJames Feist                             continue;
38973df0db0SJames Feist                         }
39073df0db0SJames Feist                     }
391711ac7a9SEd Tanous                 }
392b7a08d04SJames Feist                 nlohmann::json* config = nullptr;
393c33a90ecSJames Feist                 const std::string* classPtr = nullptr;
394711ac7a9SEd Tanous 
395711ac7a9SEd Tanous                 for (const std::pair<std::string,
396002d39b4SEd Tanous                                      dbus::utility::DbusVariantType>& propPair :
397002d39b4SEd Tanous                      intfPair.second)
398c33a90ecSJames Feist                 {
399727dc83fSLei YU                     if (propPair.first == "Class")
400711ac7a9SEd Tanous                     {
401002d39b4SEd Tanous                         classPtr = std::get_if<std::string>(&propPair.second);
402711ac7a9SEd Tanous                     }
403c33a90ecSJames Feist                 }
404c33a90ecSJames Feist 
405ef4c65b7SEd Tanous                 boost::urls::url url("/redfish/v1/Managers/bmc");
4065b4aa86bSJames Feist                 if (intfPair.first == pidZoneConfigurationIface)
4075b4aa86bSJames Feist                 {
4085b4aa86bSJames Feist                     std::string chassis;
409002d39b4SEd Tanous                     if (!dbus::utility::getNthStringFromPath(pathPair.first.str,
410002d39b4SEd Tanous                                                              5, chassis))
4115b4aa86bSJames Feist                     {
4125b4aa86bSJames Feist                         chassis = "#IllegalValue";
4135b4aa86bSJames Feist                     }
4145b4aa86bSJames Feist                     nlohmann::json& zone = zones[name];
415ef4c65b7SEd Tanous                     zone["Chassis"]["@odata.id"] =
416ef4c65b7SEd Tanous                         boost::urls::format("/redfish/v1/Chassis/{}", chassis);
417eddfc437SWilly Tu                     url.set_fragment(
418eddfc437SWilly Tu                         ("/Oem/OpenBmc/Fan/FanZones"_json_pointer / name)
419eddfc437SWilly Tu                             .to_string());
420eddfc437SWilly Tu                     zone["@odata.id"] = std::move(url);
4215b4aa86bSJames Feist                     zone["@odata.type"] = "#OemManager.FanZone";
422b7a08d04SJames Feist                     config = &zone;
4235b4aa86bSJames Feist                 }
4245b4aa86bSJames Feist 
425b7a08d04SJames Feist                 else if (intfPair.first == stepwiseConfigurationIface)
4265b4aa86bSJames Feist                 {
427c33a90ecSJames Feist                     if (classPtr == nullptr)
428c33a90ecSJames Feist                     {
42962598e31SEd Tanous                         BMCWEB_LOG_ERROR("Pid Class Field illegal");
430c33a90ecSJames Feist                         messages::internalError(asyncResp->res);
431c33a90ecSJames Feist                         return;
432c33a90ecSJames Feist                     }
433c33a90ecSJames Feist 
434b7a08d04SJames Feist                     nlohmann::json& controller = stepwise[name];
435b7a08d04SJames Feist                     config = &controller;
436eddfc437SWilly Tu                     url.set_fragment(
437eddfc437SWilly Tu                         ("/Oem/OpenBmc/Fan/StepwiseControllers"_json_pointer /
438eddfc437SWilly Tu                          name)
439eddfc437SWilly Tu                             .to_string());
440eddfc437SWilly Tu                     controller["@odata.id"] = std::move(url);
441b7a08d04SJames Feist                     controller["@odata.type"] =
442b7a08d04SJames Feist                         "#OemManager.StepwiseController";
443b7a08d04SJames Feist 
444c33a90ecSJames Feist                     controller["Direction"] = *classPtr;
4455b4aa86bSJames Feist                 }
4465b4aa86bSJames Feist 
4475b4aa86bSJames Feist                 // pid and fans are off the same configuration
448b7a08d04SJames Feist                 else if (intfPair.first == pidConfigurationIface)
4495b4aa86bSJames Feist                 {
4505b4aa86bSJames Feist                     if (classPtr == nullptr)
4515b4aa86bSJames Feist                     {
45262598e31SEd Tanous                         BMCWEB_LOG_ERROR("Pid Class Field illegal");
453a08b46ccSJason M. Bills                         messages::internalError(asyncResp->res);
4545b4aa86bSJames Feist                         return;
4555b4aa86bSJames Feist                     }
4565b4aa86bSJames Feist                     bool isFan = *classPtr == "fan";
457002d39b4SEd Tanous                     nlohmann::json& element = isFan ? fans[name] : pids[name];
458b7a08d04SJames Feist                     config = &element;
4595b4aa86bSJames Feist                     if (isFan)
4605b4aa86bSJames Feist                     {
461eddfc437SWilly Tu                         url.set_fragment(
462eddfc437SWilly Tu                             ("/Oem/OpenBmc/Fan/FanControllers"_json_pointer /
463eddfc437SWilly Tu                              name)
464eddfc437SWilly Tu                                 .to_string());
465eddfc437SWilly Tu                         element["@odata.id"] = std::move(url);
466002d39b4SEd Tanous                         element["@odata.type"] = "#OemManager.FanController";
4675b4aa86bSJames Feist                     }
4685b4aa86bSJames Feist                     else
4695b4aa86bSJames Feist                     {
470eddfc437SWilly Tu                         url.set_fragment(
471eddfc437SWilly Tu                             ("/Oem/OpenBmc/Fan/PidControllers"_json_pointer /
472eddfc437SWilly Tu                              name)
473eddfc437SWilly Tu                                 .to_string());
474eddfc437SWilly Tu                         element["@odata.id"] = std::move(url);
475002d39b4SEd Tanous                         element["@odata.type"] = "#OemManager.PidController";
4765b4aa86bSJames Feist                     }
477b7a08d04SJames Feist                 }
478b7a08d04SJames Feist                 else
479b7a08d04SJames Feist                 {
48062598e31SEd Tanous                     BMCWEB_LOG_ERROR("Unexpected configuration");
481b7a08d04SJames Feist                     messages::internalError(asyncResp->res);
482b7a08d04SJames Feist                     return;
483b7a08d04SJames Feist                 }
484b7a08d04SJames Feist 
485b7a08d04SJames Feist                 // used for making maps out of 2 vectors
486b7a08d04SJames Feist                 const std::vector<double>* keys = nullptr;
487b7a08d04SJames Feist                 const std::vector<double>* values = nullptr;
488b7a08d04SJames Feist 
489b7a08d04SJames Feist                 for (const auto& propertyPair : intfPair.second)
490b7a08d04SJames Feist                 {
491b7a08d04SJames Feist                     if (propertyPair.first == "Type" ||
492b7a08d04SJames Feist                         propertyPair.first == "Class" ||
493b7a08d04SJames Feist                         propertyPair.first == "Name")
494b7a08d04SJames Feist                     {
495b7a08d04SJames Feist                         continue;
496b7a08d04SJames Feist                     }
497b7a08d04SJames Feist 
498b7a08d04SJames Feist                     // zones
499b7a08d04SJames Feist                     if (intfPair.first == pidZoneConfigurationIface)
500b7a08d04SJames Feist                     {
501b7a08d04SJames Feist                         const double* ptr =
502abf2add6SEd Tanous                             std::get_if<double>(&propertyPair.second);
503b7a08d04SJames Feist                         if (ptr == nullptr)
504b7a08d04SJames Feist                         {
50562598e31SEd Tanous                             BMCWEB_LOG_ERROR("Field Illegal {}",
50662598e31SEd Tanous                                              propertyPair.first);
507b7a08d04SJames Feist                             messages::internalError(asyncResp->res);
508b7a08d04SJames Feist                             return;
509b7a08d04SJames Feist                         }
510b7a08d04SJames Feist                         (*config)[propertyPair.first] = *ptr;
511b7a08d04SJames Feist                     }
512b7a08d04SJames Feist 
513b7a08d04SJames Feist                     if (intfPair.first == stepwiseConfigurationIface)
514b7a08d04SJames Feist                     {
515b7a08d04SJames Feist                         if (propertyPair.first == "Reading" ||
516b7a08d04SJames Feist                             propertyPair.first == "Output")
517b7a08d04SJames Feist                         {
518b7a08d04SJames Feist                             const std::vector<double>* ptr =
519abf2add6SEd Tanous                                 std::get_if<std::vector<double>>(
520b7a08d04SJames Feist                                     &propertyPair.second);
521b7a08d04SJames Feist 
522b7a08d04SJames Feist                             if (ptr == nullptr)
523b7a08d04SJames Feist                             {
52462598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Field Illegal {}",
52562598e31SEd Tanous                                                  propertyPair.first);
526b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
527b7a08d04SJames Feist                                 return;
528b7a08d04SJames Feist                             }
529b7a08d04SJames Feist 
530b7a08d04SJames Feist                             if (propertyPair.first == "Reading")
531b7a08d04SJames Feist                             {
532b7a08d04SJames Feist                                 keys = ptr;
533b7a08d04SJames Feist                             }
534b7a08d04SJames Feist                             else
535b7a08d04SJames Feist                             {
536b7a08d04SJames Feist                                 values = ptr;
537b7a08d04SJames Feist                             }
538e662eae8SEd Tanous                             if (keys != nullptr && values != nullptr)
539b7a08d04SJames Feist                             {
540b7a08d04SJames Feist                                 if (keys->size() != values->size())
541b7a08d04SJames Feist                                 {
54262598e31SEd Tanous                                     BMCWEB_LOG_ERROR(
54362598e31SEd Tanous                                         "Reading and Output size don't match ");
544b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
545b7a08d04SJames Feist                                     return;
546b7a08d04SJames Feist                                 }
547b7a08d04SJames Feist                                 nlohmann::json& steps = (*config)["Steps"];
548b7a08d04SJames Feist                                 steps = nlohmann::json::array();
549b7a08d04SJames Feist                                 for (size_t ii = 0; ii < keys->size(); ii++)
550b7a08d04SJames Feist                                 {
5511476687dSEd Tanous                                     nlohmann::json::object_t step;
5521476687dSEd Tanous                                     step["Target"] = (*keys)[ii];
5531476687dSEd Tanous                                     step["Output"] = (*values)[ii];
554b2ba3072SPatrick Williams                                     steps.emplace_back(std::move(step));
555b7a08d04SJames Feist                                 }
556b7a08d04SJames Feist                             }
557b7a08d04SJames Feist                         }
558b7a08d04SJames Feist                         if (propertyPair.first == "NegativeHysteresis" ||
559b7a08d04SJames Feist                             propertyPair.first == "PositiveHysteresis")
560b7a08d04SJames Feist                         {
561b7a08d04SJames Feist                             const double* ptr =
562abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
563b7a08d04SJames Feist                             if (ptr == nullptr)
564b7a08d04SJames Feist                             {
56562598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Field Illegal {}",
56662598e31SEd Tanous                                                  propertyPair.first);
567b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
568b7a08d04SJames Feist                                 return;
569b7a08d04SJames Feist                             }
570b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
571b7a08d04SJames Feist                         }
572b7a08d04SJames Feist                     }
573b7a08d04SJames Feist 
574b7a08d04SJames Feist                     // pid and fans are off the same configuration
575b7a08d04SJames Feist                     if (intfPair.first == pidConfigurationIface ||
576b7a08d04SJames Feist                         intfPair.first == stepwiseConfigurationIface)
577b7a08d04SJames Feist                     {
5785b4aa86bSJames Feist                         if (propertyPair.first == "Zones")
5795b4aa86bSJames Feist                         {
5805b4aa86bSJames Feist                             const std::vector<std::string>* inputs =
581abf2add6SEd Tanous                                 std::get_if<std::vector<std::string>>(
5821b6b96c5SEd Tanous                                     &propertyPair.second);
5835b4aa86bSJames Feist 
5845b4aa86bSJames Feist                             if (inputs == nullptr)
5855b4aa86bSJames Feist                             {
58662598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Zones Pid Field Illegal");
587a08b46ccSJason M. Bills                                 messages::internalError(asyncResp->res);
5885b4aa86bSJames Feist                                 return;
5895b4aa86bSJames Feist                             }
590b7a08d04SJames Feist                             auto& data = (*config)[propertyPair.first];
5915b4aa86bSJames Feist                             data = nlohmann::json::array();
5925b4aa86bSJames Feist                             for (std::string itemCopy : *inputs)
5935b4aa86bSJames Feist                             {
5945b4aa86bSJames Feist                                 dbus::utility::escapePathForDbus(itemCopy);
5951476687dSEd Tanous                                 nlohmann::json::object_t input;
596ef4c65b7SEd Tanous                                 boost::urls::url managerUrl = boost::urls::format(
597ef4c65b7SEd Tanous                                     "/redfish/v1/Managers/bmc#{}",
598eddfc437SWilly Tu                                     ("/Oem/OpenBmc/Fan/FanZones"_json_pointer /
599eddfc437SWilly Tu                                      itemCopy)
600eddfc437SWilly Tu                                         .to_string());
601eddfc437SWilly Tu                                 input["@odata.id"] = std::move(managerUrl);
602b2ba3072SPatrick Williams                                 data.emplace_back(std::move(input));
6035b4aa86bSJames Feist                             }
6045b4aa86bSJames Feist                         }
6055b4aa86bSJames Feist                         // todo(james): may never happen, but this
6065b4aa86bSJames Feist                         // assumes configuration data referenced in the
6075b4aa86bSJames Feist                         // PID config is provided by the same daemon, we
6085b4aa86bSJames Feist                         // could add another loop to cover all cases,
6095b4aa86bSJames Feist                         // but I'm okay kicking this can down the road a
6105b4aa86bSJames Feist                         // bit
6115b4aa86bSJames Feist 
6125b4aa86bSJames Feist                         else if (propertyPair.first == "Inputs" ||
6135b4aa86bSJames Feist                                  propertyPair.first == "Outputs")
6145b4aa86bSJames Feist                         {
615b7a08d04SJames Feist                             auto& data = (*config)[propertyPair.first];
6165b4aa86bSJames Feist                             const std::vector<std::string>* inputs =
617abf2add6SEd Tanous                                 std::get_if<std::vector<std::string>>(
6181b6b96c5SEd Tanous                                     &propertyPair.second);
6195b4aa86bSJames Feist 
6205b4aa86bSJames Feist                             if (inputs == nullptr)
6215b4aa86bSJames Feist                             {
62262598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Field Illegal {}",
62362598e31SEd Tanous                                                  propertyPair.first);
624f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
6255b4aa86bSJames Feist                                 return;
6265b4aa86bSJames Feist                             }
6275b4aa86bSJames Feist                             data = *inputs;
628b943aaefSJames Feist                         }
629b943aaefSJames Feist                         else if (propertyPair.first == "SetPointOffset")
630b943aaefSJames Feist                         {
631b943aaefSJames Feist                             const std::string* ptr =
632002d39b4SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
633b943aaefSJames Feist 
634b943aaefSJames Feist                             if (ptr == nullptr)
635b943aaefSJames Feist                             {
63662598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Field Illegal {}",
63762598e31SEd Tanous                                                  propertyPair.first);
638b943aaefSJames Feist                                 messages::internalError(asyncResp->res);
639b943aaefSJames Feist                                 return;
640b943aaefSJames Feist                             }
641b943aaefSJames Feist                             // translate from dbus to redfish
642b943aaefSJames Feist                             if (*ptr == "WarningHigh")
643b943aaefSJames Feist                             {
644b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
645b943aaefSJames Feist                                     "UpperThresholdNonCritical";
646b943aaefSJames Feist                             }
647b943aaefSJames Feist                             else if (*ptr == "WarningLow")
648b943aaefSJames Feist                             {
649b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
650b943aaefSJames Feist                                     "LowerThresholdNonCritical";
651b943aaefSJames Feist                             }
652b943aaefSJames Feist                             else if (*ptr == "CriticalHigh")
653b943aaefSJames Feist                             {
654b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
655b943aaefSJames Feist                                     "UpperThresholdCritical";
656b943aaefSJames Feist                             }
657b943aaefSJames Feist                             else if (*ptr == "CriticalLow")
658b943aaefSJames Feist                             {
659b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
660b943aaefSJames Feist                                     "LowerThresholdCritical";
661b943aaefSJames Feist                             }
662b943aaefSJames Feist                             else
663b943aaefSJames Feist                             {
66462598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Value Illegal {}", *ptr);
665b943aaefSJames Feist                                 messages::internalError(asyncResp->res);
666b943aaefSJames Feist                                 return;
667b943aaefSJames Feist                             }
668b943aaefSJames Feist                         }
669b943aaefSJames Feist                         // doubles
670002d39b4SEd Tanous                         else if (propertyPair.first == "FFGainCoefficient" ||
6715b4aa86bSJames Feist                                  propertyPair.first == "FFOffCoefficient" ||
6725b4aa86bSJames Feist                                  propertyPair.first == "ICoefficient" ||
6735b4aa86bSJames Feist                                  propertyPair.first == "ILimitMax" ||
6745b4aa86bSJames Feist                                  propertyPair.first == "ILimitMin" ||
675002d39b4SEd Tanous                                  propertyPair.first == "PositiveHysteresis" ||
676002d39b4SEd Tanous                                  propertyPair.first == "NegativeHysteresis" ||
6775b4aa86bSJames Feist                                  propertyPair.first == "OutLimitMax" ||
6785b4aa86bSJames Feist                                  propertyPair.first == "OutLimitMin" ||
6795b4aa86bSJames Feist                                  propertyPair.first == "PCoefficient" ||
6807625cb81SJames Feist                                  propertyPair.first == "SetPoint" ||
6815b4aa86bSJames Feist                                  propertyPair.first == "SlewNeg" ||
6825b4aa86bSJames Feist                                  propertyPair.first == "SlewPos")
6835b4aa86bSJames Feist                         {
6845b4aa86bSJames Feist                             const double* ptr =
685abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
6865b4aa86bSJames Feist                             if (ptr == nullptr)
6875b4aa86bSJames Feist                             {
68862598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Field Illegal {}",
68962598e31SEd Tanous                                                  propertyPair.first);
690f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
6915b4aa86bSJames Feist                                 return;
6925b4aa86bSJames Feist                             }
693b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
6945b4aa86bSJames Feist                         }
6955b4aa86bSJames Feist                     }
6965b4aa86bSJames Feist                 }
6975b4aa86bSJames Feist             }
6985b4aa86bSJames Feist         }
6995eb468daSGeorge Liu     });
7005b4aa86bSJames Feist }
701ca537928SJennifer Lee 
70283ff9ab6SJames Feist enum class CreatePIDRet
70383ff9ab6SJames Feist {
70483ff9ab6SJames Feist     fail,
70583ff9ab6SJames Feist     del,
70683ff9ab6SJames Feist     patch
70783ff9ab6SJames Feist };
70883ff9ab6SJames Feist 
7098d1b46d7Szhanghch05 inline bool
7108d1b46d7Szhanghch05     getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
7119e9b6049SEd Tanous                         std::vector<nlohmann::json::object_t>& config,
7125f2caaefSJames Feist                         std::vector<std::string>& zones)
7135f2caaefSJames Feist {
714b6baeaa4SJames Feist     if (config.empty())
715b6baeaa4SJames Feist     {
71662598e31SEd Tanous         BMCWEB_LOG_ERROR("Empty Zones");
717f818b04dSEd Tanous         messages::propertyValueFormatError(response->res, config, "Zones");
718b6baeaa4SJames Feist         return false;
719b6baeaa4SJames Feist     }
7205f2caaefSJames Feist     for (auto& odata : config)
7215f2caaefSJames Feist     {
7225f2caaefSJames Feist         std::string path;
7239e9b6049SEd Tanous         if (!redfish::json_util::readJsonObject(odata, response->res,
7249e9b6049SEd Tanous                                                 "@odata.id", path))
7255f2caaefSJames Feist         {
7265f2caaefSJames Feist             return false;
7275f2caaefSJames Feist         }
7285f2caaefSJames Feist         std::string input;
72961adbda3SJames Feist 
73061adbda3SJames Feist         // 8 below comes from
73161adbda3SJames Feist         // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
73261adbda3SJames Feist         //     0    1     2      3    4    5      6     7      8
73361adbda3SJames Feist         if (!dbus::utility::getNthStringFromPath(path, 8, input))
7345f2caaefSJames Feist         {
73562598e31SEd Tanous             BMCWEB_LOG_ERROR("Got invalid path {}", path);
73662598e31SEd Tanous             BMCWEB_LOG_ERROR("Illegal Type Zones");
737f818b04dSEd Tanous             messages::propertyValueFormatError(response->res, odata, "Zones");
7385f2caaefSJames Feist             return false;
7395f2caaefSJames Feist         }
740a170f275SEd Tanous         std::replace(input.begin(), input.end(), '_', ' ');
7415f2caaefSJames Feist         zones.emplace_back(std::move(input));
7425f2caaefSJames Feist     }
7435f2caaefSJames Feist     return true;
7445f2caaefSJames Feist }
7455f2caaefSJames Feist 
746711ac7a9SEd Tanous inline const dbus::utility::ManagedObjectType::value_type*
74773df0db0SJames Feist     findChassis(const dbus::utility::ManagedObjectType& managedObj,
7489e9b6049SEd Tanous                 std::string_view value, std::string& chassis)
749b6baeaa4SJames Feist {
75062598e31SEd Tanous     BMCWEB_LOG_DEBUG("Find Chassis: {}", value);
751b6baeaa4SJames Feist 
7529e9b6049SEd Tanous     std::string escaped(value);
7536ce82fabSYaswanth Reddy M     std::replace(escaped.begin(), escaped.end(), ' ', '_');
754b6baeaa4SJames Feist     escaped = "/" + escaped;
7553544d2a7SEd Tanous     auto it = std::ranges::find_if(managedObj, [&escaped](const auto& obj) {
75618f8f608SEd Tanous         if (obj.first.str.ends_with(escaped))
757b6baeaa4SJames Feist         {
75862598e31SEd Tanous             BMCWEB_LOG_DEBUG("Matched {}", obj.first.str);
759b6baeaa4SJames Feist             return true;
760b6baeaa4SJames Feist         }
761b6baeaa4SJames Feist         return false;
762b6baeaa4SJames Feist     });
763b6baeaa4SJames Feist 
764b6baeaa4SJames Feist     if (it == managedObj.end())
765b6baeaa4SJames Feist     {
76673df0db0SJames Feist         return nullptr;
767b6baeaa4SJames Feist     }
768b6baeaa4SJames Feist     // 5 comes from <chassis-name> being the 5th element
769b6baeaa4SJames Feist     // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
77073df0db0SJames Feist     if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
77173df0db0SJames Feist     {
77273df0db0SJames Feist         return &(*it);
77373df0db0SJames Feist     }
77473df0db0SJames Feist 
77573df0db0SJames Feist     return nullptr;
776b6baeaa4SJames Feist }
777b6baeaa4SJames Feist 
77823a21a1cSEd Tanous inline CreatePIDRet createPidInterface(
7798d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
7809e9b6049SEd Tanous     std::string_view name, nlohmann::json& jsonValue, const std::string& path,
78183ff9ab6SJames Feist     const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
782b9d36b47SEd Tanous     dbus::utility::DBusPropertiesMap& output, std::string& chassis,
783b9d36b47SEd Tanous     const std::string& profile)
78483ff9ab6SJames Feist {
7855f2caaefSJames Feist     // common deleter
7869e9b6049SEd Tanous     if (jsonValue == nullptr)
7875f2caaefSJames Feist     {
7885f2caaefSJames Feist         std::string iface;
7895f2caaefSJames Feist         if (type == "PidControllers" || type == "FanControllers")
7905f2caaefSJames Feist         {
7915f2caaefSJames Feist             iface = pidConfigurationIface;
7925f2caaefSJames Feist         }
7935f2caaefSJames Feist         else if (type == "FanZones")
7945f2caaefSJames Feist         {
7955f2caaefSJames Feist             iface = pidZoneConfigurationIface;
7965f2caaefSJames Feist         }
7975f2caaefSJames Feist         else if (type == "StepwiseControllers")
7985f2caaefSJames Feist         {
7995f2caaefSJames Feist             iface = stepwiseConfigurationIface;
8005f2caaefSJames Feist         }
8015f2caaefSJames Feist         else
8025f2caaefSJames Feist         {
80362598e31SEd Tanous             BMCWEB_LOG_ERROR("Illegal Type {}", type);
8045f2caaefSJames Feist             messages::propertyUnknown(response->res, type);
8055f2caaefSJames Feist             return CreatePIDRet::fail;
8065f2caaefSJames Feist         }
8076ee7f774SJames Feist 
80862598e31SEd Tanous         BMCWEB_LOG_DEBUG("del {} {}", path, iface);
8095f2caaefSJames Feist         // delete interface
8105f2caaefSJames Feist         crow::connections::systemBus->async_method_call(
8115e7e2dc5SEd Tanous             [response, path](const boost::system::error_code& ec) {
8125f2caaefSJames Feist             if (ec)
8135f2caaefSJames Feist             {
81462598e31SEd Tanous                 BMCWEB_LOG_ERROR("Error patching {}: {}", path, ec);
8155f2caaefSJames Feist                 messages::internalError(response->res);
816b6baeaa4SJames Feist                 return;
8175f2caaefSJames Feist             }
818b6baeaa4SJames Feist             messages::success(response->res);
8195f2caaefSJames Feist         },
8205f2caaefSJames Feist             "xyz.openbmc_project.EntityManager", path, iface, "Delete");
8215f2caaefSJames Feist         return CreatePIDRet::del;
8225f2caaefSJames Feist     }
8235f2caaefSJames Feist 
824711ac7a9SEd Tanous     const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
825b6baeaa4SJames Feist     if (!createNewObject)
826b6baeaa4SJames Feist     {
827b6baeaa4SJames Feist         // if we aren't creating a new object, we should be able to find it on
828b6baeaa4SJames Feist         // d-bus
8299e9b6049SEd Tanous         managedItem = findChassis(managedObj, name, chassis);
83073df0db0SJames Feist         if (managedItem == nullptr)
831b6baeaa4SJames Feist         {
83262598e31SEd Tanous             BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
833ef4c65b7SEd Tanous             messages::invalidObject(
834ef4c65b7SEd Tanous                 response->res,
835ef4c65b7SEd Tanous                 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
836b6baeaa4SJames Feist             return CreatePIDRet::fail;
837b6baeaa4SJames Feist         }
838b6baeaa4SJames Feist     }
839b6baeaa4SJames Feist 
84026f6976fSEd Tanous     if (!profile.empty() &&
84173df0db0SJames Feist         (type == "PidControllers" || type == "FanControllers" ||
84273df0db0SJames Feist          type == "StepwiseControllers"))
84373df0db0SJames Feist     {
84473df0db0SJames Feist         if (managedItem == nullptr)
84573df0db0SJames Feist         {
846b9d36b47SEd Tanous             output.emplace_back("Profiles", std::vector<std::string>{profile});
84773df0db0SJames Feist         }
84873df0db0SJames Feist         else
84973df0db0SJames Feist         {
85073df0db0SJames Feist             std::string interface;
85173df0db0SJames Feist             if (type == "StepwiseControllers")
85273df0db0SJames Feist             {
85373df0db0SJames Feist                 interface = stepwiseConfigurationIface;
85473df0db0SJames Feist             }
85573df0db0SJames Feist             else
85673df0db0SJames Feist             {
85773df0db0SJames Feist                 interface = pidConfigurationIface;
85873df0db0SJames Feist             }
859711ac7a9SEd Tanous             bool ifaceFound = false;
860711ac7a9SEd Tanous             for (const auto& iface : managedItem->second)
861711ac7a9SEd Tanous             {
862711ac7a9SEd Tanous                 if (iface.first == interface)
863711ac7a9SEd Tanous                 {
864711ac7a9SEd Tanous                     ifaceFound = true;
865711ac7a9SEd Tanous                     for (const auto& prop : iface.second)
866711ac7a9SEd Tanous                     {
867711ac7a9SEd Tanous                         if (prop.first == "Profiles")
868711ac7a9SEd Tanous                         {
869711ac7a9SEd Tanous                             const std::vector<std::string>* curProfiles =
870711ac7a9SEd Tanous                                 std::get_if<std::vector<std::string>>(
871711ac7a9SEd Tanous                                     &(prop.second));
872711ac7a9SEd Tanous                             if (curProfiles == nullptr)
873711ac7a9SEd Tanous                             {
87462598e31SEd Tanous                                 BMCWEB_LOG_ERROR(
87562598e31SEd Tanous                                     "Illegal profiles in managed object");
876711ac7a9SEd Tanous                                 messages::internalError(response->res);
877711ac7a9SEd Tanous                                 return CreatePIDRet::fail;
878711ac7a9SEd Tanous                             }
879711ac7a9SEd Tanous                             if (std::find(curProfiles->begin(),
880711ac7a9SEd Tanous                                           curProfiles->end(),
881711ac7a9SEd Tanous                                           profile) == curProfiles->end())
882711ac7a9SEd Tanous                             {
883711ac7a9SEd Tanous                                 std::vector<std::string> newProfiles =
884711ac7a9SEd Tanous                                     *curProfiles;
885711ac7a9SEd Tanous                                 newProfiles.push_back(profile);
886b9d36b47SEd Tanous                                 output.emplace_back("Profiles", newProfiles);
887711ac7a9SEd Tanous                             }
888711ac7a9SEd Tanous                         }
889711ac7a9SEd Tanous                     }
890711ac7a9SEd Tanous                 }
891711ac7a9SEd Tanous             }
892711ac7a9SEd Tanous 
893711ac7a9SEd Tanous             if (!ifaceFound)
89473df0db0SJames Feist             {
89562598e31SEd Tanous                 BMCWEB_LOG_ERROR("Failed to find interface in managed object");
89673df0db0SJames Feist                 messages::internalError(response->res);
89773df0db0SJames Feist                 return CreatePIDRet::fail;
89873df0db0SJames Feist             }
89973df0db0SJames Feist         }
90073df0db0SJames Feist     }
90173df0db0SJames Feist 
90283ff9ab6SJames Feist     if (type == "PidControllers" || type == "FanControllers")
90383ff9ab6SJames Feist     {
90483ff9ab6SJames Feist         if (createNewObject)
90583ff9ab6SJames Feist         {
906b9d36b47SEd Tanous             output.emplace_back("Class",
907b9d36b47SEd Tanous                                 type == "PidControllers" ? "temp" : "fan");
908b9d36b47SEd Tanous             output.emplace_back("Type", "Pid");
90983ff9ab6SJames Feist         }
9105f2caaefSJames Feist 
9119e9b6049SEd Tanous         std::optional<std::vector<nlohmann::json::object_t>> zones;
9125f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
9135f2caaefSJames Feist         std::optional<std::vector<std::string>> outputs;
9145f2caaefSJames Feist         std::map<std::string, std::optional<double>> doubles;
915b943aaefSJames Feist         std::optional<std::string> setpointOffset;
9165f2caaefSJames Feist         if (!redfish::json_util::readJson(
9179e9b6049SEd Tanous                 jsonValue, response->res, "Inputs", inputs, "Outputs", outputs,
9185f2caaefSJames Feist                 "Zones", zones, "FFGainCoefficient",
9195f2caaefSJames Feist                 doubles["FFGainCoefficient"], "FFOffCoefficient",
9205f2caaefSJames Feist                 doubles["FFOffCoefficient"], "ICoefficient",
9215f2caaefSJames Feist                 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
9225f2caaefSJames Feist                 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
9235f2caaefSJames Feist                 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
9245f2caaefSJames Feist                 "PCoefficient", doubles["PCoefficient"], "SetPoint",
925b943aaefSJames Feist                 doubles["SetPoint"], "SetPointOffset", setpointOffset,
926b943aaefSJames Feist                 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
927b943aaefSJames Feist                 "PositiveHysteresis", doubles["PositiveHysteresis"],
928b943aaefSJames Feist                 "NegativeHysteresis", doubles["NegativeHysteresis"]))
92983ff9ab6SJames Feist         {
9305f2caaefSJames Feist             return CreatePIDRet::fail;
93183ff9ab6SJames Feist         }
9325f2caaefSJames Feist         if (zones)
9335f2caaefSJames Feist         {
9345f2caaefSJames Feist             std::vector<std::string> zonesStr;
9355f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStr))
9365f2caaefSJames Feist             {
93762598e31SEd Tanous                 BMCWEB_LOG_ERROR("Illegal Zones");
9385f2caaefSJames Feist                 return CreatePIDRet::fail;
9395f2caaefSJames Feist             }
940b6baeaa4SJames Feist             if (chassis.empty() &&
941e662eae8SEd Tanous                 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
942b6baeaa4SJames Feist             {
94362598e31SEd Tanous                 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
944ace85d60SEd Tanous                 messages::invalidObject(
945ef4c65b7SEd Tanous                     response->res,
946ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", chassis));
947b6baeaa4SJames Feist                 return CreatePIDRet::fail;
948b6baeaa4SJames Feist             }
949b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStr));
9505f2caaefSJames Feist         }
951afb9ee06SEd Tanous 
952afb9ee06SEd Tanous         if (inputs)
9535f2caaefSJames Feist         {
954afb9ee06SEd Tanous             for (std::string& value : *inputs)
95583ff9ab6SJames Feist             {
956a170f275SEd Tanous                 std::replace(value.begin(), value.end(), '_', ' ');
95783ff9ab6SJames Feist             }
958afb9ee06SEd Tanous             output.emplace_back("Inputs", *inputs);
959afb9ee06SEd Tanous         }
960afb9ee06SEd Tanous 
961afb9ee06SEd Tanous         if (outputs)
9625f2caaefSJames Feist         {
963afb9ee06SEd Tanous             for (std::string& value : *outputs)
9645f2caaefSJames Feist             {
965afb9ee06SEd Tanous                 std::replace(value.begin(), value.end(), '_', ' ');
9665f2caaefSJames Feist             }
967afb9ee06SEd Tanous             output.emplace_back("Outputs", *outputs);
96883ff9ab6SJames Feist         }
96983ff9ab6SJames Feist 
970b943aaefSJames Feist         if (setpointOffset)
971b943aaefSJames Feist         {
972b943aaefSJames Feist             // translate between redfish and dbus names
973b943aaefSJames Feist             if (*setpointOffset == "UpperThresholdNonCritical")
974b943aaefSJames Feist             {
975b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningLow");
976b943aaefSJames Feist             }
977b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdNonCritical")
978b943aaefSJames Feist             {
979b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningHigh");
980b943aaefSJames Feist             }
981b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdCritical")
982b943aaefSJames Feist             {
983b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalLow");
984b943aaefSJames Feist             }
985b943aaefSJames Feist             else if (*setpointOffset == "UpperThresholdCritical")
986b943aaefSJames Feist             {
987b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalHigh");
988b943aaefSJames Feist             }
989b943aaefSJames Feist             else
990b943aaefSJames Feist             {
99162598e31SEd Tanous                 BMCWEB_LOG_ERROR("Invalid setpointoffset {}", *setpointOffset);
9929e9b6049SEd Tanous                 messages::propertyValueNotInList(response->res, name,
993ace85d60SEd Tanous                                                  "SetPointOffset");
994b943aaefSJames Feist                 return CreatePIDRet::fail;
995b943aaefSJames Feist             }
996b943aaefSJames Feist         }
997b943aaefSJames Feist 
99883ff9ab6SJames Feist         // doubles
9995f2caaefSJames Feist         for (const auto& pairs : doubles)
100083ff9ab6SJames Feist         {
10015f2caaefSJames Feist             if (!pairs.second)
100283ff9ab6SJames Feist             {
10035f2caaefSJames Feist                 continue;
100483ff9ab6SJames Feist             }
100562598e31SEd Tanous             BMCWEB_LOG_DEBUG("{} = {}", pairs.first, *pairs.second);
1006b9d36b47SEd Tanous             output.emplace_back(pairs.first, *pairs.second);
10075f2caaefSJames Feist         }
100883ff9ab6SJames Feist     }
100983ff9ab6SJames Feist 
101083ff9ab6SJames Feist     else if (type == "FanZones")
101183ff9ab6SJames Feist     {
1012b9d36b47SEd Tanous         output.emplace_back("Type", "Pid.Zone");
101383ff9ab6SJames Feist 
10149e9b6049SEd Tanous         std::optional<std::string> chassisId;
10155f2caaefSJames Feist         std::optional<double> failSafePercent;
1016d3ec07f8SJames Feist         std::optional<double> minThermalOutput;
10179e9b6049SEd Tanous         if (!redfish::json_util::readJson(jsonValue, response->res,
10189e9b6049SEd Tanous                                           "Chassis/@odata.id", chassisId,
10199e9b6049SEd Tanous                                           "FailSafePercent", failSafePercent,
10209e9b6049SEd Tanous                                           "MinThermalOutput", minThermalOutput))
102183ff9ab6SJames Feist         {
102283ff9ab6SJames Feist             return CreatePIDRet::fail;
102383ff9ab6SJames Feist         }
10245f2caaefSJames Feist 
10259e9b6049SEd Tanous         if (chassisId)
102683ff9ab6SJames Feist         {
1027717794d5SAppaRao Puli             // /redfish/v1/chassis/chassis_name/
10289e9b6049SEd Tanous             if (!dbus::utility::getNthStringFromPath(*chassisId, 3, chassis))
102983ff9ab6SJames Feist             {
10309e9b6049SEd Tanous                 BMCWEB_LOG_ERROR("Got invalid path {}", *chassisId);
1031ace85d60SEd Tanous                 messages::invalidObject(
1032ef4c65b7SEd Tanous                     response->res,
10339e9b6049SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", *chassisId));
103483ff9ab6SJames Feist                 return CreatePIDRet::fail;
103583ff9ab6SJames Feist             }
103683ff9ab6SJames Feist         }
1037d3ec07f8SJames Feist         if (minThermalOutput)
103883ff9ab6SJames Feist         {
1039b9d36b47SEd Tanous             output.emplace_back("MinThermalOutput", *minThermalOutput);
10405f2caaefSJames Feist         }
10415f2caaefSJames Feist         if (failSafePercent)
104283ff9ab6SJames Feist         {
1043b9d36b47SEd Tanous             output.emplace_back("FailSafePercent", *failSafePercent);
10445f2caaefSJames Feist         }
10455f2caaefSJames Feist     }
10465f2caaefSJames Feist     else if (type == "StepwiseControllers")
10475f2caaefSJames Feist     {
1048b9d36b47SEd Tanous         output.emplace_back("Type", "Stepwise");
10495f2caaefSJames Feist 
10509e9b6049SEd Tanous         std::optional<std::vector<nlohmann::json::object_t>> zones;
10519e9b6049SEd Tanous         std::optional<std::vector<nlohmann::json::object_t>> steps;
10525f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
10535f2caaefSJames Feist         std::optional<double> positiveHysteresis;
10545f2caaefSJames Feist         std::optional<double> negativeHysteresis;
1055c33a90ecSJames Feist         std::optional<std::string> direction; // upper clipping curve vs lower
10565f2caaefSJames Feist         if (!redfish::json_util::readJson(
10579e9b6049SEd Tanous                 jsonValue, response->res, "Zones", zones, "Steps", steps,
1058b6baeaa4SJames Feist                 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
1059c33a90ecSJames Feist                 "NegativeHysteresis", negativeHysteresis, "Direction",
1060c33a90ecSJames Feist                 direction))
10615f2caaefSJames Feist         {
106283ff9ab6SJames Feist             return CreatePIDRet::fail;
106383ff9ab6SJames Feist         }
10645f2caaefSJames Feist 
10655f2caaefSJames Feist         if (zones)
106683ff9ab6SJames Feist         {
1067b6baeaa4SJames Feist             std::vector<std::string> zonesStrs;
1068b6baeaa4SJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStrs))
10695f2caaefSJames Feist             {
107062598e31SEd Tanous                 BMCWEB_LOG_ERROR("Illegal Zones");
107183ff9ab6SJames Feist                 return CreatePIDRet::fail;
107283ff9ab6SJames Feist             }
1073b6baeaa4SJames Feist             if (chassis.empty() &&
1074e662eae8SEd Tanous                 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
1075b6baeaa4SJames Feist             {
107662598e31SEd Tanous                 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
1077ace85d60SEd Tanous                 messages::invalidObject(
1078ef4c65b7SEd Tanous                     response->res,
1079ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", chassis));
1080b6baeaa4SJames Feist                 return CreatePIDRet::fail;
1081b6baeaa4SJames Feist             }
1082b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStrs));
10835f2caaefSJames Feist         }
10845f2caaefSJames Feist         if (steps)
10855f2caaefSJames Feist         {
10865f2caaefSJames Feist             std::vector<double> readings;
10875f2caaefSJames Feist             std::vector<double> outputs;
10885f2caaefSJames Feist             for (auto& step : *steps)
10895f2caaefSJames Feist             {
1090543f4400SEd Tanous                 double target = 0.0;
1091543f4400SEd Tanous                 double out = 0.0;
10925f2caaefSJames Feist 
10939e9b6049SEd Tanous                 if (!redfish::json_util::readJsonObject(
10949e9b6049SEd Tanous                         step, response->res, "Target", target, "Output", out))
10955f2caaefSJames Feist                 {
10965f2caaefSJames Feist                     return CreatePIDRet::fail;
10975f2caaefSJames Feist                 }
10985f2caaefSJames Feist                 readings.emplace_back(target);
109923a21a1cSEd Tanous                 outputs.emplace_back(out);
11005f2caaefSJames Feist             }
1101b9d36b47SEd Tanous             output.emplace_back("Reading", std::move(readings));
1102b9d36b47SEd Tanous             output.emplace_back("Output", std::move(outputs));
11035f2caaefSJames Feist         }
11045f2caaefSJames Feist         if (inputs)
11055f2caaefSJames Feist         {
11065f2caaefSJames Feist             for (std::string& value : *inputs)
11075f2caaefSJames Feist             {
1108a170f275SEd Tanous                 std::replace(value.begin(), value.end(), '_', ' ');
11095f2caaefSJames Feist             }
1110b9d36b47SEd Tanous             output.emplace_back("Inputs", std::move(*inputs));
11115f2caaefSJames Feist         }
11125f2caaefSJames Feist         if (negativeHysteresis)
11135f2caaefSJames Feist         {
1114b9d36b47SEd Tanous             output.emplace_back("NegativeHysteresis", *negativeHysteresis);
11155f2caaefSJames Feist         }
11165f2caaefSJames Feist         if (positiveHysteresis)
11175f2caaefSJames Feist         {
1118b9d36b47SEd Tanous             output.emplace_back("PositiveHysteresis", *positiveHysteresis);
111983ff9ab6SJames Feist         }
1120c33a90ecSJames Feist         if (direction)
1121c33a90ecSJames Feist         {
1122c33a90ecSJames Feist             constexpr const std::array<const char*, 2> allowedDirections = {
1123c33a90ecSJames Feist                 "Ceiling", "Floor"};
11243544d2a7SEd Tanous             if (std::ranges::find(allowedDirections, *direction) ==
11253544d2a7SEd Tanous                 allowedDirections.end())
1126c33a90ecSJames Feist             {
1127c33a90ecSJames Feist                 messages::propertyValueTypeError(response->res, "Direction",
1128c33a90ecSJames Feist                                                  *direction);
1129c33a90ecSJames Feist                 return CreatePIDRet::fail;
1130c33a90ecSJames Feist             }
1131b9d36b47SEd Tanous             output.emplace_back("Class", *direction);
1132c33a90ecSJames Feist         }
113383ff9ab6SJames Feist     }
113483ff9ab6SJames Feist     else
113583ff9ab6SJames Feist     {
113662598e31SEd Tanous         BMCWEB_LOG_ERROR("Illegal Type {}", type);
113735a62c7cSJason M. Bills         messages::propertyUnknown(response->res, type);
113883ff9ab6SJames Feist         return CreatePIDRet::fail;
113983ff9ab6SJames Feist     }
114083ff9ab6SJames Feist     return CreatePIDRet::patch;
114183ff9ab6SJames Feist }
114273df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
114373df0db0SJames Feist {
11446936afe4SEd Tanous     struct CompletionValues
11456936afe4SEd Tanous     {
11466936afe4SEd Tanous         std::vector<std::string> supportedProfiles;
11476936afe4SEd Tanous         std::string currentProfile;
11486936afe4SEd Tanous         dbus::utility::MapperGetSubTreeResponse subtree;
11496936afe4SEd Tanous     };
115083ff9ab6SJames Feist 
11514e23a444SEd Tanous     explicit GetPIDValues(
11524e23a444SEd Tanous         const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
115323a21a1cSEd Tanous         asyncResp(asyncRespIn)
115473df0db0SJames Feist 
11551214b7e7SGunnar Mills     {}
11569c310685SBorawski.Lukasz 
115773df0db0SJames Feist     void run()
11585b4aa86bSJames Feist     {
115973df0db0SJames Feist         std::shared_ptr<GetPIDValues> self = shared_from_this();
116073df0db0SJames Feist 
116173df0db0SJames Feist         // get all configurations
1162e99073f5SGeorge Liu         constexpr std::array<std::string_view, 4> interfaces = {
1163e99073f5SGeorge Liu             pidConfigurationIface, pidZoneConfigurationIface,
1164e99073f5SGeorge Liu             objectManagerIface, stepwiseConfigurationIface};
1165e99073f5SGeorge Liu         dbus::utility::getSubTree(
1166e99073f5SGeorge Liu             "/", 0, interfaces,
1167b9d36b47SEd Tanous             [self](
1168e99073f5SGeorge Liu                 const boost::system::error_code& ec,
1169b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
11705b4aa86bSJames Feist             if (ec)
11715b4aa86bSJames Feist             {
117262598e31SEd Tanous                 BMCWEB_LOG_ERROR("{}", ec);
117373df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
117473df0db0SJames Feist                 return;
117573df0db0SJames Feist             }
11766936afe4SEd Tanous             self->complete.subtree = subtreeLocal;
1177e99073f5SGeorge Liu         });
117873df0db0SJames Feist 
117973df0db0SJames Feist         // at the same time get the selected profile
1180e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1181e99073f5SGeorge Liu             thermalModeIface};
1182e99073f5SGeorge Liu         dbus::utility::getSubTree(
1183e99073f5SGeorge Liu             "/", 0, thermalModeIfaces,
1184b9d36b47SEd Tanous             [self](
1185e99073f5SGeorge Liu                 const boost::system::error_code& ec,
1186b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
118723a21a1cSEd Tanous             if (ec || subtreeLocal.empty())
118873df0db0SJames Feist             {
118973df0db0SJames Feist                 return;
119073df0db0SJames Feist             }
119123a21a1cSEd Tanous             if (subtreeLocal[0].second.size() != 1)
119273df0db0SJames Feist             {
119373df0db0SJames Feist                 // invalid mapper response, should never happen
119462598e31SEd Tanous                 BMCWEB_LOG_ERROR("GetPIDValues: Mapper Error");
119573df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
11965b4aa86bSJames Feist                 return;
11975b4aa86bSJames Feist             }
11985b4aa86bSJames Feist 
119923a21a1cSEd Tanous             const std::string& path = subtreeLocal[0].first;
120023a21a1cSEd Tanous             const std::string& owner = subtreeLocal[0].second[0].first;
1201fac6e53bSKrzysztof Grobelny 
1202fac6e53bSKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
1203fac6e53bSKrzysztof Grobelny                 *crow::connections::systemBus, owner, path, thermalModeIface,
1204168e20c1SEd Tanous                 [path, owner,
12055e7e2dc5SEd Tanous                  self](const boost::system::error_code& ec2,
1206b9d36b47SEd Tanous                        const dbus::utility::DBusPropertiesMap& resp) {
120723a21a1cSEd Tanous                 if (ec2)
120873df0db0SJames Feist                 {
120962598e31SEd Tanous                     BMCWEB_LOG_ERROR(
121062598e31SEd Tanous                         "GetPIDValues: Can't get thermalModeIface {}", path);
121173df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
121273df0db0SJames Feist                     return;
121373df0db0SJames Feist                 }
1214fac6e53bSKrzysztof Grobelny 
1215271584abSEd Tanous                 const std::string* current = nullptr;
1216271584abSEd Tanous                 const std::vector<std::string>* supported = nullptr;
1217fac6e53bSKrzysztof Grobelny 
1218fac6e53bSKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
1219fac6e53bSKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), resp, "Current", current,
1220fac6e53bSKrzysztof Grobelny                     "Supported", supported);
1221fac6e53bSKrzysztof Grobelny 
1222fac6e53bSKrzysztof Grobelny                 if (!success)
122373df0db0SJames Feist                 {
1224002d39b4SEd Tanous                     messages::internalError(self->asyncResp->res);
122573df0db0SJames Feist                     return;
122673df0db0SJames Feist                 }
1227fac6e53bSKrzysztof Grobelny 
122873df0db0SJames Feist                 if (current == nullptr || supported == nullptr)
122973df0db0SJames Feist                 {
123062598e31SEd Tanous                     BMCWEB_LOG_ERROR(
123162598e31SEd Tanous                         "GetPIDValues: thermal mode iface invalid {}", path);
123273df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
123373df0db0SJames Feist                     return;
123473df0db0SJames Feist                 }
12356936afe4SEd Tanous                 self->complete.currentProfile = *current;
12366936afe4SEd Tanous                 self->complete.supportedProfiles = *supported;
1237fac6e53bSKrzysztof Grobelny             });
1238e99073f5SGeorge Liu         });
123973df0db0SJames Feist     }
124073df0db0SJames Feist 
12416936afe4SEd Tanous     static void
12426936afe4SEd Tanous         processingComplete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12436936afe4SEd Tanous                            const CompletionValues& completion)
124473df0db0SJames Feist     {
124573df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
124673df0db0SJames Feist         {
124773df0db0SJames Feist             return;
124873df0db0SJames Feist         }
12495b4aa86bSJames Feist         // create map of <connection, path to objMgr>>
12506936afe4SEd Tanous         boost::container::flat_map<
12516936afe4SEd Tanous             std::string, std::string, std::less<>,
12526936afe4SEd Tanous             std::vector<std::pair<std::string, std::string>>>
12536936afe4SEd Tanous             objectMgrPaths;
12546936afe4SEd Tanous         boost::container::flat_set<std::string, std::less<>,
12556936afe4SEd Tanous                                    std::vector<std::string>>
12566936afe4SEd Tanous             calledConnections;
12576936afe4SEd Tanous         for (const auto& pathGroup : completion.subtree)
12585b4aa86bSJames Feist         {
12595b4aa86bSJames Feist             for (const auto& connectionGroup : pathGroup.second)
12605b4aa86bSJames Feist             {
12616bce33bcSJames Feist                 auto findConnection =
12626bce33bcSJames Feist                     calledConnections.find(connectionGroup.first);
12636bce33bcSJames Feist                 if (findConnection != calledConnections.end())
12646bce33bcSJames Feist                 {
12656bce33bcSJames Feist                     break;
12666bce33bcSJames Feist                 }
126773df0db0SJames Feist                 for (const std::string& interface : connectionGroup.second)
12685b4aa86bSJames Feist                 {
12695b4aa86bSJames Feist                     if (interface == objectManagerIface)
12705b4aa86bSJames Feist                     {
127173df0db0SJames Feist                         objectMgrPaths[connectionGroup.first] = pathGroup.first;
12725b4aa86bSJames Feist                     }
12735b4aa86bSJames Feist                     // this list is alphabetical, so we
12745b4aa86bSJames Feist                     // should have found the objMgr by now
12755b4aa86bSJames Feist                     if (interface == pidConfigurationIface ||
1276b7a08d04SJames Feist                         interface == pidZoneConfigurationIface ||
1277b7a08d04SJames Feist                         interface == stepwiseConfigurationIface)
12785b4aa86bSJames Feist                     {
12795b4aa86bSJames Feist                         auto findObjMgr =
12805b4aa86bSJames Feist                             objectMgrPaths.find(connectionGroup.first);
12815b4aa86bSJames Feist                         if (findObjMgr == objectMgrPaths.end())
12825b4aa86bSJames Feist                         {
128362598e31SEd Tanous                             BMCWEB_LOG_DEBUG("{}Has no Object Manager",
128462598e31SEd Tanous                                              connectionGroup.first);
12855b4aa86bSJames Feist                             continue;
12865b4aa86bSJames Feist                         }
12876bce33bcSJames Feist 
12886bce33bcSJames Feist                         calledConnections.insert(connectionGroup.first);
12896bce33bcSJames Feist 
129073df0db0SJames Feist                         asyncPopulatePid(findObjMgr->first, findObjMgr->second,
12916936afe4SEd Tanous                                          completion.currentProfile,
12926936afe4SEd Tanous                                          completion.supportedProfiles,
129373df0db0SJames Feist                                          asyncResp);
12945b4aa86bSJames Feist                         break;
12955b4aa86bSJames Feist                     }
12965b4aa86bSJames Feist                 }
12975b4aa86bSJames Feist             }
12985b4aa86bSJames Feist         }
129973df0db0SJames Feist     }
130073df0db0SJames Feist 
13016936afe4SEd Tanous     ~GetPIDValues()
13026936afe4SEd Tanous     {
13036936afe4SEd Tanous         boost::asio::post(crow::connections::systemBus->get_io_context(),
13046936afe4SEd Tanous                           std::bind_front(&processingComplete, asyncResp,
13056936afe4SEd Tanous                                           std::move(complete)));
13066936afe4SEd Tanous     }
13076936afe4SEd Tanous 
1308ecd6a3a2SEd Tanous     GetPIDValues(const GetPIDValues&) = delete;
1309ecd6a3a2SEd Tanous     GetPIDValues(GetPIDValues&&) = delete;
1310ecd6a3a2SEd Tanous     GetPIDValues& operator=(const GetPIDValues&) = delete;
1311ecd6a3a2SEd Tanous     GetPIDValues& operator=(GetPIDValues&&) = delete;
1312ecd6a3a2SEd Tanous 
13138d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
13146936afe4SEd Tanous     CompletionValues complete;
131573df0db0SJames Feist };
131673df0db0SJames Feist 
131773df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
131873df0db0SJames Feist {
13199e9b6049SEd Tanous     SetPIDValues(
13209e9b6049SEd Tanous         const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
13219e9b6049SEd Tanous         std::vector<
13229e9b6049SEd Tanous             std::pair<std::string, std::optional<nlohmann::json::object_t>>>&&
13239e9b6049SEd Tanous             configurationsIn,
13249e9b6049SEd Tanous         std::optional<std::string>& profileIn) :
13259e9b6049SEd Tanous         asyncResp(asyncRespIn),
13269e9b6049SEd Tanous         configuration(std::move(configurationsIn)),
13279e9b6049SEd Tanous         profile(std::move(profileIn))
13289e9b6049SEd Tanous     {}
1329ecd6a3a2SEd Tanous 
1330ecd6a3a2SEd Tanous     SetPIDValues(const SetPIDValues&) = delete;
1331ecd6a3a2SEd Tanous     SetPIDValues(SetPIDValues&&) = delete;
1332ecd6a3a2SEd Tanous     SetPIDValues& operator=(const SetPIDValues&) = delete;
1333ecd6a3a2SEd Tanous     SetPIDValues& operator=(SetPIDValues&&) = delete;
1334ecd6a3a2SEd Tanous 
133573df0db0SJames Feist     void run()
133673df0db0SJames Feist     {
133773df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
133873df0db0SJames Feist         {
133973df0db0SJames Feist             return;
134073df0db0SJames Feist         }
134173df0db0SJames Feist 
134273df0db0SJames Feist         std::shared_ptr<SetPIDValues> self = shared_from_this();
134373df0db0SJames Feist 
134473df0db0SJames Feist         // todo(james): might make sense to do a mapper call here if this
134573df0db0SJames Feist         // interface gets more traction
13465eb468daSGeorge Liu         sdbusplus::message::object_path objPath(
13475eb468daSGeorge Liu             "/xyz/openbmc_project/inventory");
13485eb468daSGeorge Liu         dbus::utility::getManagedObjects(
13495eb468daSGeorge Liu             "xyz.openbmc_project.EntityManager", objPath,
13505e7e2dc5SEd Tanous             [self](const boost::system::error_code& ec,
1351914e2d5dSEd Tanous                    const dbus::utility::ManagedObjectType& mObj) {
135273df0db0SJames Feist             if (ec)
135373df0db0SJames Feist             {
135462598e31SEd Tanous                 BMCWEB_LOG_ERROR("Error communicating to Entity Manager");
135573df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
135673df0db0SJames Feist                 return;
135773df0db0SJames Feist             }
1358e69d9de2SJames Feist             const std::array<const char*, 3> configurations = {
1359e69d9de2SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
1360e69d9de2SJames Feist                 stepwiseConfigurationIface};
1361e69d9de2SJames Feist 
136214b0b8d5SJames Feist             for (const auto& [path, object] : mObj)
1363e69d9de2SJames Feist             {
136414b0b8d5SJames Feist                 for (const auto& [interface, _] : object)
1365e69d9de2SJames Feist                 {
13663544d2a7SEd Tanous                     if (std::ranges::find(configurations, interface) !=
13673544d2a7SEd Tanous                         configurations.end())
1368e69d9de2SJames Feist                     {
136914b0b8d5SJames Feist                         self->objectCount++;
1370e69d9de2SJames Feist                         break;
1371e69d9de2SJames Feist                     }
1372e69d9de2SJames Feist                 }
1373e69d9de2SJames Feist             }
1374914e2d5dSEd Tanous             self->managedObj = mObj;
13755eb468daSGeorge Liu         });
137673df0db0SJames Feist 
137773df0db0SJames Feist         // at the same time get the profile information
1378e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1379e99073f5SGeorge Liu             thermalModeIface};
1380e99073f5SGeorge Liu         dbus::utility::getSubTree(
1381e99073f5SGeorge Liu             "/", 0, thermalModeIfaces,
1382e99073f5SGeorge Liu             [self](const boost::system::error_code& ec,
1383b9d36b47SEd Tanous                    const dbus::utility::MapperGetSubTreeResponse& subtree) {
138473df0db0SJames Feist             if (ec || subtree.empty())
138573df0db0SJames Feist             {
138673df0db0SJames Feist                 return;
138773df0db0SJames Feist             }
138873df0db0SJames Feist             if (subtree[0].second.empty())
138973df0db0SJames Feist             {
139073df0db0SJames Feist                 // invalid mapper response, should never happen
139162598e31SEd Tanous                 BMCWEB_LOG_ERROR("SetPIDValues: Mapper Error");
139273df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
139373df0db0SJames Feist                 return;
139473df0db0SJames Feist             }
139573df0db0SJames Feist 
139673df0db0SJames Feist             const std::string& path = subtree[0].first;
139773df0db0SJames Feist             const std::string& owner = subtree[0].second[0].first;
1398fac6e53bSKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
1399fac6e53bSKrzysztof Grobelny                 *crow::connections::systemBus, owner, path, thermalModeIface,
14005e7e2dc5SEd Tanous                 [self, path, owner](const boost::system::error_code& ec2,
1401b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap& r) {
1402cb13a392SEd Tanous                 if (ec2)
140373df0db0SJames Feist                 {
140462598e31SEd Tanous                     BMCWEB_LOG_ERROR(
140562598e31SEd Tanous                         "SetPIDValues: Can't get thermalModeIface {}", path);
140673df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
140773df0db0SJames Feist                     return;
140873df0db0SJames Feist                 }
1409271584abSEd Tanous                 const std::string* current = nullptr;
1410271584abSEd Tanous                 const std::vector<std::string>* supported = nullptr;
1411fac6e53bSKrzysztof Grobelny 
1412fac6e53bSKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
1413fac6e53bSKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), r, "Current", current,
1414fac6e53bSKrzysztof Grobelny                     "Supported", supported);
1415fac6e53bSKrzysztof Grobelny 
1416fac6e53bSKrzysztof Grobelny                 if (!success)
141773df0db0SJames Feist                 {
1418002d39b4SEd Tanous                     messages::internalError(self->asyncResp->res);
141973df0db0SJames Feist                     return;
142073df0db0SJames Feist                 }
1421fac6e53bSKrzysztof Grobelny 
142273df0db0SJames Feist                 if (current == nullptr || supported == nullptr)
142373df0db0SJames Feist                 {
142462598e31SEd Tanous                     BMCWEB_LOG_ERROR(
142562598e31SEd Tanous                         "SetPIDValues: thermal mode iface invalid {}", path);
142673df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
142773df0db0SJames Feist                     return;
142873df0db0SJames Feist                 }
142973df0db0SJames Feist                 self->currentProfile = *current;
143073df0db0SJames Feist                 self->supportedProfiles = *supported;
143173df0db0SJames Feist                 self->profileConnection = owner;
143273df0db0SJames Feist                 self->profilePath = path;
1433fac6e53bSKrzysztof Grobelny             });
1434e99073f5SGeorge Liu         });
143573df0db0SJames Feist     }
143624b2fe81SEd Tanous     void pidSetDone()
143773df0db0SJames Feist     {
143873df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
143973df0db0SJames Feist         {
144073df0db0SJames Feist             return;
14415b4aa86bSJames Feist         }
14428d1b46d7Szhanghch05         std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
144373df0db0SJames Feist         if (profile)
144473df0db0SJames Feist         {
14453544d2a7SEd Tanous             if (std::ranges::find(supportedProfiles, *profile) ==
14463544d2a7SEd Tanous                 supportedProfiles.end())
144773df0db0SJames Feist             {
144873df0db0SJames Feist                 messages::actionParameterUnknown(response->res, "Profile",
144973df0db0SJames Feist                                                  *profile);
145073df0db0SJames Feist                 return;
145173df0db0SJames Feist             }
145273df0db0SJames Feist             currentProfile = *profile;
14539ae226faSGeorge Liu             sdbusplus::asio::setProperty(
14549ae226faSGeorge Liu                 *crow::connections::systemBus, profileConnection, profilePath,
14559ae226faSGeorge Liu                 thermalModeIface, "Current", *profile,
14565e7e2dc5SEd Tanous                 [response](const boost::system::error_code& ec) {
145773df0db0SJames Feist                 if (ec)
145873df0db0SJames Feist                 {
145962598e31SEd Tanous                     BMCWEB_LOG_ERROR("Error patching profile{}", ec);
146073df0db0SJames Feist                     messages::internalError(response->res);
146173df0db0SJames Feist                 }
14629ae226faSGeorge Liu             });
146373df0db0SJames Feist         }
146473df0db0SJames Feist 
146573df0db0SJames Feist         for (auto& containerPair : configuration)
146673df0db0SJames Feist         {
146773df0db0SJames Feist             auto& container = containerPair.second;
146873df0db0SJames Feist             if (!container)
146973df0db0SJames Feist             {
147073df0db0SJames Feist                 continue;
147173df0db0SJames Feist             }
14726ee7f774SJames Feist 
147302cad96eSEd Tanous             const std::string& type = containerPair.first;
147473df0db0SJames Feist 
14759e9b6049SEd Tanous             for (auto& [name, value] : *container)
147673df0db0SJames Feist             {
1477cddbf3dfSPotin Lai                 std::string dbusObjName = name;
1478cddbf3dfSPotin Lai                 std::replace(dbusObjName.begin(), dbusObjName.end(), ' ', '_');
147962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("looking for {}", name);
14806ee7f774SJames Feist 
14813544d2a7SEd Tanous                 auto pathItr = std::ranges::find_if(
14823544d2a7SEd Tanous                     managedObj, [&dbusObjName](const auto& obj) {
148318f8f608SEd Tanous                     return obj.first.parent_path() == dbusObjName;
148473df0db0SJames Feist                 });
1485b9d36b47SEd Tanous                 dbus::utility::DBusPropertiesMap output;
148673df0db0SJames Feist 
148773df0db0SJames Feist                 output.reserve(16); // The pid interface length
148873df0db0SJames Feist 
148973df0db0SJames Feist                 // determines if we're patching entity-manager or
149073df0db0SJames Feist                 // creating a new object
149173df0db0SJames Feist                 bool createNewObject = (pathItr == managedObj.end());
149262598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Found = {}", !createNewObject);
14936ee7f774SJames Feist 
149473df0db0SJames Feist                 std::string iface;
1495ea2b670dSEd Tanous                 if (!createNewObject)
1496ea2b670dSEd Tanous                 {
14978be2b5b6SPotin Lai                     bool findInterface = false;
1498ea2b670dSEd Tanous                     for (const auto& interface : pathItr->second)
1499ea2b670dSEd Tanous                     {
1500ea2b670dSEd Tanous                         if (interface.first == pidConfigurationIface)
1501ea2b670dSEd Tanous                         {
1502ea2b670dSEd Tanous                             if (type == "PidControllers" ||
1503ea2b670dSEd Tanous                                 type == "FanControllers")
150473df0db0SJames Feist                             {
150573df0db0SJames Feist                                 iface = pidConfigurationIface;
15068be2b5b6SPotin Lai                                 findInterface = true;
15078be2b5b6SPotin Lai                                 break;
150873df0db0SJames Feist                             }
150973df0db0SJames Feist                         }
1510ea2b670dSEd Tanous                         else if (interface.first == pidZoneConfigurationIface)
151173df0db0SJames Feist                         {
1512ea2b670dSEd Tanous                             if (type == "FanZones")
151373df0db0SJames Feist                             {
1514da39350aSPavanKumarIntel                                 iface = pidZoneConfigurationIface;
15158be2b5b6SPotin Lai                                 findInterface = true;
15168be2b5b6SPotin Lai                                 break;
151773df0db0SJames Feist                             }
151873df0db0SJames Feist                         }
1519ea2b670dSEd Tanous                         else if (interface.first == stepwiseConfigurationIface)
1520ea2b670dSEd Tanous                         {
1521ea2b670dSEd Tanous                             if (type == "StepwiseControllers")
152273df0db0SJames Feist                             {
152373df0db0SJames Feist                                 iface = stepwiseConfigurationIface;
15248be2b5b6SPotin Lai                                 findInterface = true;
15258be2b5b6SPotin Lai                                 break;
15268be2b5b6SPotin Lai                             }
15278be2b5b6SPotin Lai                         }
15288be2b5b6SPotin Lai                     }
15298be2b5b6SPotin Lai 
15308be2b5b6SPotin Lai                     // create new object if interface not found
15318be2b5b6SPotin Lai                     if (!findInterface)
15328be2b5b6SPotin Lai                     {
153373df0db0SJames Feist                         createNewObject = true;
153473df0db0SJames Feist                     }
1535ea2b670dSEd Tanous                 }
15366ee7f774SJames Feist 
15379e9b6049SEd Tanous                 if (createNewObject && value == nullptr)
15386ee7f774SJames Feist                 {
15394e0453b1SGunnar Mills                     // can't delete a non-existent object
15409e9b6049SEd Tanous                     messages::propertyValueNotInList(response->res, value,
1541e2616cc5SEd Tanous                                                      name);
15426ee7f774SJames Feist                     continue;
15436ee7f774SJames Feist                 }
15446ee7f774SJames Feist 
15456ee7f774SJames Feist                 std::string path;
15466ee7f774SJames Feist                 if (pathItr != managedObj.end())
15476ee7f774SJames Feist                 {
15486ee7f774SJames Feist                     path = pathItr->first.str;
15496ee7f774SJames Feist                 }
15506ee7f774SJames Feist 
155162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Create new = {}", createNewObject);
1552e69d9de2SJames Feist 
1553e69d9de2SJames Feist                 // arbitrary limit to avoid attacks
1554e69d9de2SJames Feist                 constexpr const size_t controllerLimit = 500;
155514b0b8d5SJames Feist                 if (createNewObject && objectCount >= controllerLimit)
1556e69d9de2SJames Feist                 {
1557e69d9de2SJames Feist                     messages::resourceExhaustion(response->res, type);
1558e69d9de2SJames Feist                     continue;
1559e69d9de2SJames Feist                 }
1560a170f275SEd Tanous                 std::string escaped = name;
1561a170f275SEd Tanous                 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1562a170f275SEd Tanous                 output.emplace_back("Name", escaped);
156373df0db0SJames Feist 
156473df0db0SJames Feist                 std::string chassis;
156573df0db0SJames Feist                 CreatePIDRet ret = createPidInterface(
15669e9b6049SEd Tanous                     response, type, name, value, path, managedObj,
15679e9b6049SEd Tanous                     createNewObject, output, chassis, currentProfile);
156873df0db0SJames Feist                 if (ret == CreatePIDRet::fail)
156973df0db0SJames Feist                 {
157073df0db0SJames Feist                     return;
157173df0db0SJames Feist                 }
15723174e4dfSEd Tanous                 if (ret == CreatePIDRet::del)
157373df0db0SJames Feist                 {
157473df0db0SJames Feist                     continue;
157573df0db0SJames Feist                 }
157673df0db0SJames Feist 
157773df0db0SJames Feist                 if (!createNewObject)
157873df0db0SJames Feist                 {
157973df0db0SJames Feist                     for (const auto& property : output)
158073df0db0SJames Feist                     {
15817a696974SPotin Lai                         crow::connections::systemBus->async_method_call(
158273df0db0SJames Feist                             [response,
158373df0db0SJames Feist                              propertyName{std::string(property.first)}](
15845e7e2dc5SEd Tanous                                 const boost::system::error_code& ec) {
158573df0db0SJames Feist                             if (ec)
158673df0db0SJames Feist                             {
158762598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Error patching {}: {}",
158862598e31SEd Tanous                                                  propertyName, ec);
158973df0db0SJames Feist                                 messages::internalError(response->res);
159073df0db0SJames Feist                                 return;
159173df0db0SJames Feist                             }
159273df0db0SJames Feist                             messages::success(response->res);
15937a696974SPotin Lai                         },
15947a696974SPotin Lai                             "xyz.openbmc_project.EntityManager", path,
15957a696974SPotin Lai                             "org.freedesktop.DBus.Properties", "Set", iface,
15967a696974SPotin Lai                             property.first, property.second);
159773df0db0SJames Feist                     }
159873df0db0SJames Feist                 }
159973df0db0SJames Feist                 else
160073df0db0SJames Feist                 {
160173df0db0SJames Feist                     if (chassis.empty())
160273df0db0SJames Feist                     {
160362598e31SEd Tanous                         BMCWEB_LOG_ERROR("Failed to get chassis from config");
1604ace85d60SEd Tanous                         messages::internalError(response->res);
160573df0db0SJames Feist                         return;
160673df0db0SJames Feist                     }
160773df0db0SJames Feist 
160873df0db0SJames Feist                     bool foundChassis = false;
160973df0db0SJames Feist                     for (const auto& obj : managedObj)
161073df0db0SJames Feist                     {
161118f8f608SEd Tanous                         if (obj.first.parent_path() == chassis)
161273df0db0SJames Feist                         {
161373df0db0SJames Feist                             chassis = obj.first.str;
161473df0db0SJames Feist                             foundChassis = true;
161573df0db0SJames Feist                             break;
161673df0db0SJames Feist                         }
161773df0db0SJames Feist                     }
161873df0db0SJames Feist                     if (!foundChassis)
161973df0db0SJames Feist                     {
162062598e31SEd Tanous                         BMCWEB_LOG_ERROR("Failed to find chassis on dbus");
162173df0db0SJames Feist                         messages::resourceMissingAtURI(
1622ace85d60SEd Tanous                             response->res,
1623ef4c65b7SEd Tanous                             boost::urls::format("/redfish/v1/Chassis/{}",
1624ef4c65b7SEd Tanous                                                 chassis));
162573df0db0SJames Feist                         return;
162673df0db0SJames Feist                     }
162773df0db0SJames Feist 
162873df0db0SJames Feist                     crow::connections::systemBus->async_method_call(
16295e7e2dc5SEd Tanous                         [response](const boost::system::error_code& ec) {
163073df0db0SJames Feist                         if (ec)
163173df0db0SJames Feist                         {
163262598e31SEd Tanous                             BMCWEB_LOG_ERROR("Error Adding Pid Object {}", ec);
163373df0db0SJames Feist                             messages::internalError(response->res);
163473df0db0SJames Feist                             return;
163573df0db0SJames Feist                         }
163673df0db0SJames Feist                         messages::success(response->res);
163773df0db0SJames Feist                     },
163873df0db0SJames Feist                         "xyz.openbmc_project.EntityManager", chassis,
163973df0db0SJames Feist                         "xyz.openbmc_project.AddObject", "AddObject", output);
164073df0db0SJames Feist                 }
164173df0db0SJames Feist             }
164273df0db0SJames Feist         }
164373df0db0SJames Feist     }
164424b2fe81SEd Tanous 
164524b2fe81SEd Tanous     ~SetPIDValues()
164624b2fe81SEd Tanous     {
164724b2fe81SEd Tanous         try
164824b2fe81SEd Tanous         {
164924b2fe81SEd Tanous             pidSetDone();
165024b2fe81SEd Tanous         }
165124b2fe81SEd Tanous         catch (...)
165224b2fe81SEd Tanous         {
165362598e31SEd Tanous             BMCWEB_LOG_CRITICAL("pidSetDone threw exception");
165424b2fe81SEd Tanous         }
165524b2fe81SEd Tanous     }
165624b2fe81SEd Tanous 
16578d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
16589e9b6049SEd Tanous     std::vector<std::pair<std::string, std::optional<nlohmann::json::object_t>>>
165973df0db0SJames Feist         configuration;
166073df0db0SJames Feist     std::optional<std::string> profile;
166173df0db0SJames Feist     dbus::utility::ManagedObjectType managedObj;
166273df0db0SJames Feist     std::vector<std::string> supportedProfiles;
166373df0db0SJames Feist     std::string currentProfile;
166473df0db0SJames Feist     std::string profileConnection;
166573df0db0SJames Feist     std::string profilePath;
166614b0b8d5SJames Feist     size_t objectCount = 0;
166773df0db0SJames Feist };
166873df0db0SJames Feist 
1669071d8fdfSSunnySrivastava1984 /**
1670071d8fdfSSunnySrivastava1984  * @brief Retrieves BMC manager location data over DBus
1671071d8fdfSSunnySrivastava1984  *
1672ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for completing asynchronous calls
1673071d8fdfSSunnySrivastava1984  * @param[in] connectionName - service name
1674071d8fdfSSunnySrivastava1984  * @param[in] path - object path
1675071d8fdfSSunnySrivastava1984  * @return none
1676071d8fdfSSunnySrivastava1984  */
1677ac106bf6SEd Tanous inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1678071d8fdfSSunnySrivastava1984                         const std::string& connectionName,
1679071d8fdfSSunnySrivastava1984                         const std::string& path)
1680071d8fdfSSunnySrivastava1984 {
168162598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get BMC manager Location data.");
1682071d8fdfSSunnySrivastava1984 
16831e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
16841e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
16851e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
1686ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
16871e1e598dSJonathan Doman                     const std::string& property) {
1688071d8fdfSSunnySrivastava1984         if (ec)
1689071d8fdfSSunnySrivastava1984         {
169062598e31SEd Tanous             BMCWEB_LOG_DEBUG("DBUS response error for "
169162598e31SEd Tanous                              "Location");
1692ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
1693071d8fdfSSunnySrivastava1984             return;
1694071d8fdfSSunnySrivastava1984         }
1695071d8fdfSSunnySrivastava1984 
1696ac106bf6SEd Tanous         asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
16971e1e598dSJonathan Doman             property;
16981e1e598dSJonathan Doman     });
1699071d8fdfSSunnySrivastava1984 }
17007e860f15SJohn Edward Broadbent // avoid name collision systems.hpp
17017e860f15SJohn Edward Broadbent inline void
1702ac106bf6SEd Tanous     managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
17034bf2b033SGunnar Mills {
170462598e31SEd Tanous     BMCWEB_LOG_DEBUG("Getting Manager Last Reset Time");
17054bf2b033SGunnar Mills 
17061e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint64_t>(
17071e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
17081e1e598dSJonathan Doman         "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
17091e1e598dSJonathan Doman         "LastRebootTime",
1710ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
17111e1e598dSJonathan Doman                     const uint64_t lastResetTime) {
17124bf2b033SGunnar Mills         if (ec)
17134bf2b033SGunnar Mills         {
171462598e31SEd Tanous             BMCWEB_LOG_DEBUG("D-BUS response error {}", ec);
17154bf2b033SGunnar Mills             return;
17164bf2b033SGunnar Mills         }
17174bf2b033SGunnar Mills 
17184bf2b033SGunnar Mills         // LastRebootTime is epoch time, in milliseconds
17194bf2b033SGunnar Mills         // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
17201e1e598dSJonathan Doman         uint64_t lastResetTimeStamp = lastResetTime / 1000;
17214bf2b033SGunnar Mills 
17224bf2b033SGunnar Mills         // Convert to ISO 8601 standard
1723ac106bf6SEd Tanous         asyncResp->res.jsonValue["LastResetTime"] =
17242b82937eSEd Tanous             redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
17251e1e598dSJonathan Doman     });
17264bf2b033SGunnar Mills }
17274bf2b033SGunnar Mills 
17284bfefa74SGunnar Mills /**
17294bfefa74SGunnar Mills  * @brief Set the running firmware image
17304bfefa74SGunnar Mills  *
1731ac106bf6SEd Tanous  * @param[i,o] asyncResp - Async response object
17324bfefa74SGunnar Mills  * @param[i] runningFirmwareTarget - Image to make the running image
17334bfefa74SGunnar Mills  *
17344bfefa74SGunnar Mills  * @return void
17354bfefa74SGunnar Mills  */
17367e860f15SJohn Edward Broadbent inline void
1737ac106bf6SEd Tanous     setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1738f23b7296SEd Tanous                            const std::string& runningFirmwareTarget)
17394bfefa74SGunnar Mills {
17404bfefa74SGunnar Mills     // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1741f23b7296SEd Tanous     std::string::size_type idPos = runningFirmwareTarget.rfind('/');
17424bfefa74SGunnar Mills     if (idPos == std::string::npos)
17434bfefa74SGunnar Mills     {
1744ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
17454bfefa74SGunnar Mills                                          "@odata.id");
174662598e31SEd Tanous         BMCWEB_LOG_DEBUG("Can't parse firmware ID!");
17474bfefa74SGunnar Mills         return;
17484bfefa74SGunnar Mills     }
17494bfefa74SGunnar Mills     idPos++;
17504bfefa74SGunnar Mills     if (idPos >= runningFirmwareTarget.size())
17514bfefa74SGunnar Mills     {
1752ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
17534bfefa74SGunnar Mills                                          "@odata.id");
175462598e31SEd Tanous         BMCWEB_LOG_DEBUG("Invalid firmware ID.");
17554bfefa74SGunnar Mills         return;
17564bfefa74SGunnar Mills     }
17574bfefa74SGunnar Mills     std::string firmwareId = runningFirmwareTarget.substr(idPos);
17584bfefa74SGunnar Mills 
17594bfefa74SGunnar Mills     // Make sure the image is valid before setting priority
17605eb468daSGeorge Liu     sdbusplus::message::object_path objPath("/xyz/openbmc_project/software");
17615eb468daSGeorge Liu     dbus::utility::getManagedObjects(
17625eb468daSGeorge Liu         "xyz.openbmc_project.Software.BMC.Updater", objPath,
17635eb468daSGeorge Liu         [asyncResp, firmwareId, runningFirmwareTarget](
17645eb468daSGeorge Liu             const boost::system::error_code& ec,
17655eb468daSGeorge Liu             const dbus::utility::ManagedObjectType& subtree) {
17664bfefa74SGunnar Mills         if (ec)
17674bfefa74SGunnar Mills         {
176862598e31SEd Tanous             BMCWEB_LOG_DEBUG("D-Bus response error getting objects.");
1769ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
17704bfefa74SGunnar Mills             return;
17714bfefa74SGunnar Mills         }
17724bfefa74SGunnar Mills 
177326f6976fSEd Tanous         if (subtree.empty())
17744bfefa74SGunnar Mills         {
177562598e31SEd Tanous             BMCWEB_LOG_DEBUG("Can't find image!");
1776ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
17774bfefa74SGunnar Mills             return;
17784bfefa74SGunnar Mills         }
17794bfefa74SGunnar Mills 
17804bfefa74SGunnar Mills         bool foundImage = false;
178102cad96eSEd Tanous         for (const auto& object : subtree)
17824bfefa74SGunnar Mills         {
17834bfefa74SGunnar Mills             const std::string& path =
17844bfefa74SGunnar Mills                 static_cast<const std::string&>(object.first);
1785f23b7296SEd Tanous             std::size_t idPos2 = path.rfind('/');
17864bfefa74SGunnar Mills 
17874bfefa74SGunnar Mills             if (idPos2 == std::string::npos)
17884bfefa74SGunnar Mills             {
17894bfefa74SGunnar Mills                 continue;
17904bfefa74SGunnar Mills             }
17914bfefa74SGunnar Mills 
17924bfefa74SGunnar Mills             idPos2++;
17934bfefa74SGunnar Mills             if (idPos2 >= path.size())
17944bfefa74SGunnar Mills             {
17954bfefa74SGunnar Mills                 continue;
17964bfefa74SGunnar Mills             }
17974bfefa74SGunnar Mills 
17984bfefa74SGunnar Mills             if (path.substr(idPos2) == firmwareId)
17994bfefa74SGunnar Mills             {
18004bfefa74SGunnar Mills                 foundImage = true;
18014bfefa74SGunnar Mills                 break;
18024bfefa74SGunnar Mills             }
18034bfefa74SGunnar Mills         }
18044bfefa74SGunnar Mills 
18054bfefa74SGunnar Mills         if (!foundImage)
18064bfefa74SGunnar Mills         {
1807ac106bf6SEd Tanous             messages::propertyValueNotInList(
1808ac106bf6SEd Tanous                 asyncResp->res, runningFirmwareTarget, "@odata.id");
180962598e31SEd Tanous             BMCWEB_LOG_DEBUG("Invalid firmware ID.");
18104bfefa74SGunnar Mills             return;
18114bfefa74SGunnar Mills         }
18124bfefa74SGunnar Mills 
181362598e31SEd Tanous         BMCWEB_LOG_DEBUG("Setting firmware version {} to priority 0.",
181462598e31SEd Tanous                          firmwareId);
18154bfefa74SGunnar Mills 
18164bfefa74SGunnar Mills         // Only support Immediate
18174bfefa74SGunnar Mills         // An addition could be a Redfish Setting like
18184bfefa74SGunnar Mills         // ActiveSoftwareImageApplyTime and support OnReset
18199ae226faSGeorge Liu         sdbusplus::asio::setProperty(
18209ae226faSGeorge Liu             *crow::connections::systemBus,
18219ae226faSGeorge Liu             "xyz.openbmc_project.Software.BMC.Updater",
18229ae226faSGeorge Liu             "/xyz/openbmc_project/software/" + firmwareId,
18239ae226faSGeorge Liu             "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
18249ae226faSGeorge Liu             static_cast<uint8_t>(0),
1825ac106bf6SEd Tanous             [asyncResp](const boost::system::error_code& ec2) {
18268a592810SEd Tanous             if (ec2)
18274bfefa74SGunnar Mills             {
182862598e31SEd Tanous                 BMCWEB_LOG_DEBUG("D-Bus response error setting.");
1829ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
18304bfefa74SGunnar Mills                 return;
18314bfefa74SGunnar Mills             }
1832ac106bf6SEd Tanous             doBMCGracefulRestart(asyncResp);
18339ae226faSGeorge Liu         });
18345eb468daSGeorge Liu     });
18354bfefa74SGunnar Mills }
18364bfefa74SGunnar Mills 
1837c51afd54SEd Tanous inline void
1838c51afd54SEd Tanous     afterSetDateTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1839c51afd54SEd Tanous                      const boost::system::error_code& ec,
1840c51afd54SEd Tanous                      const sdbusplus::message_t& msg)
1841c51afd54SEd Tanous {
1842c51afd54SEd Tanous     if (ec)
1843c51afd54SEd Tanous     {
1844c51afd54SEd Tanous         BMCWEB_LOG_DEBUG("Failed to set elapsed time. DBUS response error {}",
1845c51afd54SEd Tanous                          ec);
1846c51afd54SEd Tanous         const sd_bus_error* dbusError = msg.get_error();
1847c51afd54SEd Tanous         if (dbusError != nullptr)
1848c51afd54SEd Tanous         {
1849c51afd54SEd Tanous             std::string_view errorName(dbusError->name);
1850c51afd54SEd Tanous             if (errorName ==
1851c51afd54SEd Tanous                 "org.freedesktop.timedate1.AutomaticTimeSyncEnabled")
1852c51afd54SEd Tanous             {
1853c51afd54SEd Tanous                 BMCWEB_LOG_DEBUG("Setting conflict");
1854c51afd54SEd Tanous                 messages::propertyValueConflict(
1855c51afd54SEd Tanous                     asyncResp->res, "DateTime",
1856c51afd54SEd Tanous                     "Managers/NetworkProtocol/NTPProcotolEnabled");
1857c51afd54SEd Tanous                 return;
1858c51afd54SEd Tanous             }
1859c51afd54SEd Tanous         }
1860c51afd54SEd Tanous         messages::internalError(asyncResp->res);
1861c51afd54SEd Tanous         return;
1862c51afd54SEd Tanous     }
1863c51afd54SEd Tanous     asyncResp->res.result(boost::beast::http::status::no_content);
1864c51afd54SEd Tanous }
1865c51afd54SEd Tanous 
1866c51afd54SEd Tanous inline void setDateTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1867c51afd54SEd Tanous                         const std::string& datetime)
1868af5d6058SSantosh Puranik {
186962598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set date time: {}", datetime);
1870af5d6058SSantosh Puranik 
1871c2e32007SEd Tanous     std::optional<redfish::time_utils::usSinceEpoch> us =
1872c2e32007SEd Tanous         redfish::time_utils::dateStringToEpoch(datetime);
1873c2e32007SEd Tanous     if (!us)
1874af5d6058SSantosh Puranik     {
1875ac106bf6SEd Tanous         messages::propertyValueFormatError(asyncResp->res, datetime,
1876ac106bf6SEd Tanous                                            "DateTime");
1877c2e32007SEd Tanous         return;
1878c2e32007SEd Tanous     }
1879c51afd54SEd Tanous     // Set the absolute datetime
1880c51afd54SEd Tanous     bool relative = false;
1881c51afd54SEd Tanous     bool interactive = false;
1882c51afd54SEd Tanous     crow::connections::systemBus->async_method_call(
1883c51afd54SEd Tanous         [asyncResp](const boost::system::error_code& ec,
1884c51afd54SEd Tanous                     const sdbusplus::message_t& msg) {
1885c51afd54SEd Tanous         afterSetDateTime(asyncResp, ec, msg);
1886c51afd54SEd Tanous     },
1887c51afd54SEd Tanous         "org.freedesktop.timedate1", "/org/freedesktop/timedate1",
1888c51afd54SEd Tanous         "org.freedesktop.timedate1", "SetTime", us->count(), relative,
1889c51afd54SEd Tanous         interactive);
189083ff9ab6SJames Feist }
18919c310685SBorawski.Lukasz 
189275815e5cSEd Tanous inline void
189375815e5cSEd Tanous     checkForQuiesced(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
189475815e5cSEd Tanous {
189575815e5cSEd Tanous     sdbusplus::asio::getProperty<std::string>(
189675815e5cSEd Tanous         *crow::connections::systemBus, "org.freedesktop.systemd1",
189775815e5cSEd Tanous         "/org/freedesktop/systemd1/unit/obmc-bmc-service-quiesce@0.target",
189875815e5cSEd Tanous         "org.freedesktop.systemd1.Unit", "ActiveState",
189975815e5cSEd Tanous         [asyncResp](const boost::system::error_code& ec,
190075815e5cSEd Tanous                     const std::string& val) {
190175815e5cSEd Tanous         if (!ec)
190275815e5cSEd Tanous         {
190375815e5cSEd Tanous             if (val == "active")
190475815e5cSEd Tanous             {
190575815e5cSEd Tanous                 asyncResp->res.jsonValue["Status"]["Health"] = "Critical";
190675815e5cSEd Tanous                 asyncResp->res.jsonValue["Status"]["State"] = "Quiesced";
190775815e5cSEd Tanous                 return;
190875815e5cSEd Tanous             }
190975815e5cSEd Tanous         }
191075815e5cSEd Tanous         asyncResp->res.jsonValue["Status"]["Health"] = "OK";
191175815e5cSEd Tanous         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
191275815e5cSEd Tanous     });
191375815e5cSEd Tanous }
191475815e5cSEd Tanous 
19157e860f15SJohn Edward Broadbent inline void requestRoutesManager(App& app)
19167e860f15SJohn Edward Broadbent {
19177e860f15SJohn Edward Broadbent     std::string uuid = persistent_data::getConfig().systemUuid;
19189c310685SBorawski.Lukasz 
19197e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
1920ed398213SEd Tanous         .privileges(redfish::privileges::getManager)
1921002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1922002d39b4SEd Tanous             [&app, uuid](const crow::Request& req,
192345ca1b86SEd Tanous                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
19243ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
192545ca1b86SEd Tanous         {
192645ca1b86SEd Tanous             return;
192745ca1b86SEd Tanous         }
19287e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
1929a51fc2d2SSui Chen         asyncResp->res.jsonValue["@odata.type"] = "#Manager.v1_14_0.Manager";
19307e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["Id"] = "bmc";
19317e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
19327e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["Description"] =
19337e860f15SJohn Edward Broadbent             "Baseboard Management Controller";
19347e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["PowerState"] = "On";
19351476687dSEd Tanous 
19367e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["ManagerType"] = "BMC";
19377e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
19387e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1939002d39b4SEd Tanous         asyncResp->res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
19407e860f15SJohn Edward Broadbent 
19411476687dSEd Tanous         asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
19421476687dSEd Tanous             "/redfish/v1/Managers/bmc/LogServices";
19431476687dSEd Tanous         asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
19441476687dSEd Tanous             "/redfish/v1/Managers/bmc/NetworkProtocol";
19451476687dSEd Tanous         asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
19461476687dSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
19477e860f15SJohn Edward Broadbent 
1948*36c0f2a3SEd Tanous         if constexpr (bmcwebNbdProxy)
1949*36c0f2a3SEd Tanous         {
19501476687dSEd Tanous             asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
19511476687dSEd Tanous                 "/redfish/v1/Managers/bmc/VirtualMedia";
1952*36c0f2a3SEd Tanous         }
19537e860f15SJohn Edward Broadbent 
19547e860f15SJohn Edward Broadbent         // default oem data
19557e860f15SJohn Edward Broadbent         nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
19567e860f15SJohn Edward Broadbent         nlohmann::json& oemOpenbmc = oem["OpenBmc"];
19577e860f15SJohn Edward Broadbent         oem["@odata.type"] = "#OemManager.Oem";
19587e860f15SJohn Edward Broadbent         oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
19597e860f15SJohn Edward Broadbent         oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
19607e860f15SJohn Edward Broadbent         oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
19611476687dSEd Tanous 
19621476687dSEd Tanous         nlohmann::json::object_t certificates;
19631476687dSEd Tanous         certificates["@odata.id"] =
19641476687dSEd Tanous             "/redfish/v1/Managers/bmc/Truststore/Certificates";
19651476687dSEd Tanous         oemOpenbmc["Certificates"] = std::move(certificates);
19667e860f15SJohn Edward Broadbent 
19677e860f15SJohn Edward Broadbent         // Manager.Reset (an action) can be many values, OpenBMC only
19687e860f15SJohn Edward Broadbent         // supports BMC reboot.
19697e860f15SJohn Edward Broadbent         nlohmann::json& managerReset =
19707e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
19717e860f15SJohn Edward Broadbent         managerReset["target"] =
19727e860f15SJohn Edward Broadbent             "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
19737e860f15SJohn Edward Broadbent         managerReset["@Redfish.ActionInfo"] =
19747e860f15SJohn Edward Broadbent             "/redfish/v1/Managers/bmc/ResetActionInfo";
19757e860f15SJohn Edward Broadbent 
19767e860f15SJohn Edward Broadbent         // ResetToDefaults (Factory Reset) has values like
19777e860f15SJohn Edward Broadbent         // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
19787e860f15SJohn Edward Broadbent         // on OpenBMC
19797e860f15SJohn Edward Broadbent         nlohmann::json& resetToDefaults =
19807e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
19817e860f15SJohn Edward Broadbent         resetToDefaults["target"] =
19827e860f15SJohn Edward Broadbent             "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
1983613dabeaSEd Tanous         resetToDefaults["ResetType@Redfish.AllowableValues"] =
1984613dabeaSEd Tanous             nlohmann::json::array_t({"ResetAll"});
19857e860f15SJohn Edward Broadbent 
19867c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
19872b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
19887c8c4058STejas Patil 
19897c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
19907c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
19917c8c4058STejas Patil             redfishDateTimeOffset.second;
19927e860f15SJohn Edward Broadbent 
19930e8ac5e7SGunnar Mills         // TODO (Gunnar): Remove these one day since moved to ComputerSystem
19940e8ac5e7SGunnar Mills         // Still used by OCP profiles
19950e8ac5e7SGunnar Mills         // https://github.com/opencomputeproject/OCP-Profiles/issues/23
19967e860f15SJohn Edward Broadbent         // Fill in SerialConsole info
19977e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
1998002d39b4SEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
1999613dabeaSEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
2000613dabeaSEd Tanous             nlohmann::json::array_t({"IPMI", "SSH"});
20017e860f15SJohn Edward Broadbent #ifdef BMCWEB_ENABLE_KVM
20027e860f15SJohn Edward Broadbent         // Fill in GraphicalConsole info
2003002d39b4SEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
2004002d39b4SEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] =
2005002d39b4SEd Tanous             4;
2006613dabeaSEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
2007613dabeaSEd Tanous             nlohmann::json::array_t({"KVMIP"});
20087e860f15SJohn Edward Broadbent #endif // BMCWEB_ENABLE_KVM
20097f3e84a1SEd Tanous         if constexpr (!bmcwebEnableMultiHost)
20107f3e84a1SEd Tanous         {
20117f3e84a1SEd Tanous             asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] =
20127f3e84a1SEd Tanous                 1;
20131476687dSEd Tanous 
20141476687dSEd Tanous             nlohmann::json::array_t managerForServers;
20151476687dSEd Tanous             nlohmann::json::object_t manager;
20161476687dSEd Tanous             manager["@odata.id"] = "/redfish/v1/Systems/system";
2017ad539545SPatrick Williams             managerForServers.emplace_back(std::move(manager));
20181476687dSEd Tanous 
20191476687dSEd Tanous             asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
20201476687dSEd Tanous                 std::move(managerForServers);
20217f3e84a1SEd Tanous         }
20227e860f15SJohn Edward Broadbent 
2023eee0013eSWilly Tu         sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
20247e860f15SJohn Edward Broadbent                                              "FirmwareVersion", true);
20257e860f15SJohn Edward Broadbent 
20267e860f15SJohn Edward Broadbent         managerGetLastResetTime(asyncResp);
20277e860f15SJohn Edward Broadbent 
2028a51fc2d2SSui Chen         // ManagerDiagnosticData is added for all BMCs.
2029a51fc2d2SSui Chen         nlohmann::json& managerDiagnosticData =
2030a51fc2d2SSui Chen             asyncResp->res.jsonValue["ManagerDiagnosticData"];
2031a51fc2d2SSui Chen         managerDiagnosticData["@odata.id"] =
2032a51fc2d2SSui Chen             "/redfish/v1/Managers/bmc/ManagerDiagnosticData";
2033a51fc2d2SSui Chen 
203454dce7f5SGunnar Mills #ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
20357e860f15SJohn Edward Broadbent         auto pids = std::make_shared<GetPIDValues>(asyncResp);
20367e860f15SJohn Edward Broadbent         pids->run();
203754dce7f5SGunnar Mills #endif
20387e860f15SJohn Edward Broadbent 
2039002d39b4SEd Tanous         getMainChassisId(asyncResp,
2040002d39b4SEd Tanous                          [](const std::string& chassisId,
2041002d39b4SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2042002d39b4SEd Tanous             aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
20431476687dSEd Tanous             nlohmann::json::array_t managerForChassis;
20448a592810SEd Tanous             nlohmann::json::object_t managerObj;
2045ef4c65b7SEd Tanous             boost::urls::url chassiUrl =
2046ef4c65b7SEd Tanous                 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
2047eddfc437SWilly Tu             managerObj["@odata.id"] = chassiUrl;
2048ad539545SPatrick Williams             managerForChassis.emplace_back(std::move(managerObj));
20491476687dSEd Tanous             aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
20501476687dSEd Tanous                 std::move(managerForChassis);
20511476687dSEd Tanous             aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
2052eddfc437SWilly Tu                 chassiUrl;
20537e860f15SJohn Edward Broadbent         });
20547e860f15SJohn Edward Broadbent 
20551e1e598dSJonathan Doman         sdbusplus::asio::getProperty<double>(
20561e1e598dSJonathan Doman             *crow::connections::systemBus, "org.freedesktop.systemd1",
2057002d39b4SEd Tanous             "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
2058002d39b4SEd Tanous             "Progress",
205975815e5cSEd Tanous             [asyncResp](const boost::system::error_code& ec, double val) {
20607e860f15SJohn Edward Broadbent             if (ec)
20611abe55efSEd Tanous             {
206262598e31SEd Tanous                 BMCWEB_LOG_ERROR("Error while getting progress");
20637e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
20647e860f15SJohn Edward Broadbent                 return;
20657e860f15SJohn Edward Broadbent             }
20661e1e598dSJonathan Doman             if (val < 1.0)
20677e860f15SJohn Edward Broadbent             {
206875815e5cSEd Tanous                 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
2069002d39b4SEd Tanous                 asyncResp->res.jsonValue["Status"]["State"] = "Starting";
207075815e5cSEd Tanous                 return;
20717e860f15SJohn Edward Broadbent             }
207275815e5cSEd Tanous             checkForQuiesced(asyncResp);
20731e1e598dSJonathan Doman         });
20749c310685SBorawski.Lukasz 
2075e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
2076e99073f5SGeorge Liu             "xyz.openbmc_project.Inventory.Item.Bmc"};
2077e99073f5SGeorge Liu         dbus::utility::getSubTree(
2078e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, interfaces,
20797e860f15SJohn Edward Broadbent             [asyncResp](
2080e99073f5SGeorge Liu                 const boost::system::error_code& ec,
2081b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
20827e860f15SJohn Edward Broadbent             if (ec)
20831abe55efSEd Tanous             {
208462598e31SEd Tanous                 BMCWEB_LOG_DEBUG("D-Bus response error on GetSubTree {}", ec);
20857e860f15SJohn Edward Broadbent                 return;
20867e860f15SJohn Edward Broadbent             }
208726f6976fSEd Tanous             if (subtree.empty())
20887e860f15SJohn Edward Broadbent             {
208962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Can't find bmc D-Bus object!");
20907e860f15SJohn Edward Broadbent                 return;
20917e860f15SJohn Edward Broadbent             }
20927e860f15SJohn Edward Broadbent             // Assume only 1 bmc D-Bus object
20937e860f15SJohn Edward Broadbent             // Throw an error if there is more than 1
20947e860f15SJohn Edward Broadbent             if (subtree.size() > 1)
20957e860f15SJohn Edward Broadbent             {
209662598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Found more than 1 bmc D-Bus object!");
20977e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
20987e860f15SJohn Edward Broadbent                 return;
20997e860f15SJohn Edward Broadbent             }
21007e860f15SJohn Edward Broadbent 
2101002d39b4SEd Tanous             if (subtree[0].first.empty() || subtree[0].second.size() != 1)
21027e860f15SJohn Edward Broadbent             {
210362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Error getting bmc D-Bus object!");
21047e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
21057e860f15SJohn Edward Broadbent                 return;
21067e860f15SJohn Edward Broadbent             }
21077e860f15SJohn Edward Broadbent 
21087e860f15SJohn Edward Broadbent             const std::string& path = subtree[0].first;
2109002d39b4SEd Tanous             const std::string& connectionName = subtree[0].second[0].first;
21107e860f15SJohn Edward Broadbent 
2111002d39b4SEd Tanous             for (const auto& interfaceName : subtree[0].second[0].second)
21127e860f15SJohn Edward Broadbent             {
21137e860f15SJohn Edward Broadbent                 if (interfaceName ==
21147e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Inventory.Decorator.Asset")
21157e860f15SJohn Edward Broadbent                 {
2116fac6e53bSKrzysztof Grobelny                     sdbusplus::asio::getAllProperties(
2117fac6e53bSKrzysztof Grobelny                         *crow::connections::systemBus, connectionName, path,
2118fac6e53bSKrzysztof Grobelny                         "xyz.openbmc_project.Inventory.Decorator.Asset",
21195e7e2dc5SEd Tanous                         [asyncResp](const boost::system::error_code& ec2,
2120b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap&
21217e860f15SJohn Edward Broadbent                                         propertiesList) {
21228a592810SEd Tanous                         if (ec2)
21237e860f15SJohn Edward Broadbent                         {
212462598e31SEd Tanous                             BMCWEB_LOG_DEBUG("Can't get bmc asset!");
21257e860f15SJohn Edward Broadbent                             return;
21267e860f15SJohn Edward Broadbent                         }
21277e860f15SJohn Edward Broadbent 
2128fac6e53bSKrzysztof Grobelny                         const std::string* partNumber = nullptr;
2129fac6e53bSKrzysztof Grobelny                         const std::string* serialNumber = nullptr;
2130fac6e53bSKrzysztof Grobelny                         const std::string* manufacturer = nullptr;
2131fac6e53bSKrzysztof Grobelny                         const std::string* model = nullptr;
2132fac6e53bSKrzysztof Grobelny                         const std::string* sparePartNumber = nullptr;
2133fac6e53bSKrzysztof Grobelny 
2134fac6e53bSKrzysztof Grobelny                         const bool success = sdbusplus::unpackPropertiesNoThrow(
2135fac6e53bSKrzysztof Grobelny                             dbus_utils::UnpackErrorPrinter(), propertiesList,
2136fac6e53bSKrzysztof Grobelny                             "PartNumber", partNumber, "SerialNumber",
2137fac6e53bSKrzysztof Grobelny                             serialNumber, "Manufacturer", manufacturer, "Model",
2138fac6e53bSKrzysztof Grobelny                             model, "SparePartNumber", sparePartNumber);
2139fac6e53bSKrzysztof Grobelny 
2140fac6e53bSKrzysztof Grobelny                         if (!success)
21417e860f15SJohn Edward Broadbent                         {
2142002d39b4SEd Tanous                             messages::internalError(asyncResp->res);
21437e860f15SJohn Edward Broadbent                             return;
21447e860f15SJohn Edward Broadbent                         }
2145fac6e53bSKrzysztof Grobelny 
2146fac6e53bSKrzysztof Grobelny                         if (partNumber != nullptr)
2147fac6e53bSKrzysztof Grobelny                         {
2148fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["PartNumber"] =
2149fac6e53bSKrzysztof Grobelny                                 *partNumber;
21507e860f15SJohn Edward Broadbent                         }
2151fac6e53bSKrzysztof Grobelny 
2152fac6e53bSKrzysztof Grobelny                         if (serialNumber != nullptr)
2153fac6e53bSKrzysztof Grobelny                         {
2154fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["SerialNumber"] =
2155fac6e53bSKrzysztof Grobelny                                 *serialNumber;
21567e860f15SJohn Edward Broadbent                         }
2157fac6e53bSKrzysztof Grobelny 
2158fac6e53bSKrzysztof Grobelny                         if (manufacturer != nullptr)
2159fac6e53bSKrzysztof Grobelny                         {
2160fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["Manufacturer"] =
2161fac6e53bSKrzysztof Grobelny                                 *manufacturer;
2162fac6e53bSKrzysztof Grobelny                         }
2163fac6e53bSKrzysztof Grobelny 
2164fac6e53bSKrzysztof Grobelny                         if (model != nullptr)
2165fac6e53bSKrzysztof Grobelny                         {
2166fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["Model"] = *model;
2167fac6e53bSKrzysztof Grobelny                         }
2168fac6e53bSKrzysztof Grobelny 
2169fac6e53bSKrzysztof Grobelny                         if (sparePartNumber != nullptr)
2170fac6e53bSKrzysztof Grobelny                         {
2171fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["SparePartNumber"] =
2172fac6e53bSKrzysztof Grobelny                                 *sparePartNumber;
2173fac6e53bSKrzysztof Grobelny                         }
2174fac6e53bSKrzysztof Grobelny                     });
21757e860f15SJohn Edward Broadbent                 }
2176002d39b4SEd Tanous                 else if (interfaceName ==
21770fda0f12SGeorge Liu                          "xyz.openbmc_project.Inventory.Decorator.LocationCode")
21787e860f15SJohn Edward Broadbent                 {
21797e860f15SJohn Edward Broadbent                     getLocation(asyncResp, connectionName, path);
21807e860f15SJohn Edward Broadbent                 }
21817e860f15SJohn Edward Broadbent             }
2182e99073f5SGeorge Liu         });
21837e860f15SJohn Edward Broadbent     });
21847e860f15SJohn Edward Broadbent 
21857e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
2186ed398213SEd Tanous         .privileges(redfish::privileges::patchManager)
218745ca1b86SEd Tanous         .methods(boost::beast::http::verb::patch)(
218845ca1b86SEd Tanous             [&app](const crow::Request& req,
21897e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
21903ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
219145ca1b86SEd Tanous         {
219245ca1b86SEd Tanous             return;
219345ca1b86SEd Tanous         }
21949e9b6049SEd Tanous         std::optional<std::string> activeSoftwareImageOdataId;
21957e860f15SJohn Edward Broadbent         std::optional<std::string> datetime;
21969e9b6049SEd Tanous         std::optional<nlohmann::json::object_t> pidControllers;
21979e9b6049SEd Tanous         std::optional<nlohmann::json::object_t> fanControllers;
21989e9b6049SEd Tanous         std::optional<nlohmann::json::object_t> fanZones;
21999e9b6049SEd Tanous         std::optional<nlohmann::json::object_t> stepwiseControllers;
22009e9b6049SEd Tanous         std::optional<std::string> profile;
22017e860f15SJohn Edward Broadbent 
22029e9b6049SEd Tanous         // clang-format off
22039e9b6049SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res,
22049e9b6049SEd Tanous               "DateTime", datetime,
22059e9b6049SEd Tanous               "Links/ActiveSoftwareImage/@odata.id", activeSoftwareImageOdataId,
22069e9b6049SEd Tanous               "Oem/OpenBmc/Fan/FanControllers", fanControllers,
22079e9b6049SEd Tanous               "Oem/OpenBmc/Fan/FanZones", fanZones,
22089e9b6049SEd Tanous               "Oem/OpenBmc/Fan/PidControllers", pidControllers,
22099e9b6049SEd Tanous               "Oem/OpenBmc/Fan/Profile", profile,
22109e9b6049SEd Tanous               "Oem/OpenBmc/Fan/StepwiseControllers", stepwiseControllers
22119e9b6049SEd Tanous         ))
22127e860f15SJohn Edward Broadbent         {
22137e860f15SJohn Edward Broadbent             return;
22147e860f15SJohn Edward Broadbent         }
22159e9b6049SEd Tanous         // clang-format on
22167e860f15SJohn Edward Broadbent 
22179e9b6049SEd Tanous         if (pidControllers || fanControllers || fanZones ||
22189e9b6049SEd Tanous             stepwiseControllers || profile)
22197e860f15SJohn Edward Broadbent         {
222054dce7f5SGunnar Mills #ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
22219e9b6049SEd Tanous             std::vector<
22229e9b6049SEd Tanous                 std::pair<std::string, std::optional<nlohmann::json::object_t>>>
22239e9b6049SEd Tanous                 configuration;
22249e9b6049SEd Tanous             if (pidControllers)
22257e860f15SJohn Edward Broadbent             {
22269e9b6049SEd Tanous                 configuration.emplace_back("PidControllers",
22279e9b6049SEd Tanous                                            std::move(pidControllers));
22287e860f15SJohn Edward Broadbent             }
22299e9b6049SEd Tanous             if (fanControllers)
22307e860f15SJohn Edward Broadbent             {
22319e9b6049SEd Tanous                 configuration.emplace_back("FanControllers",
22329e9b6049SEd Tanous                                            std::move(fanControllers));
22337e860f15SJohn Edward Broadbent             }
22349e9b6049SEd Tanous             if (fanZones)
22357e860f15SJohn Edward Broadbent             {
22369e9b6049SEd Tanous                 configuration.emplace_back("FanZones", std::move(fanZones));
22379e9b6049SEd Tanous             }
22389e9b6049SEd Tanous             if (stepwiseControllers)
22399e9b6049SEd Tanous             {
22409e9b6049SEd Tanous                 configuration.emplace_back("StepwiseControllers",
22419e9b6049SEd Tanous                                            std::move(stepwiseControllers));
22429e9b6049SEd Tanous             }
22439e9b6049SEd Tanous             auto pid = std::make_shared<SetPIDValues>(
22449e9b6049SEd Tanous                 asyncResp, std::move(configuration), profile);
22457e860f15SJohn Edward Broadbent             pid->run();
224654dce7f5SGunnar Mills #else
224754dce7f5SGunnar Mills             messages::propertyUnknown(asyncResp->res, "Oem");
224854dce7f5SGunnar Mills             return;
224954dce7f5SGunnar Mills #endif
22507e860f15SJohn Edward Broadbent         }
22519e9b6049SEd Tanous 
22529e9b6049SEd Tanous         if (activeSoftwareImageOdataId)
22537e860f15SJohn Edward Broadbent         {
22549e9b6049SEd Tanous             setActiveFirmwareImage(asyncResp, *activeSoftwareImageOdataId);
22557e860f15SJohn Edward Broadbent         }
22567e860f15SJohn Edward Broadbent 
22577e860f15SJohn Edward Broadbent         if (datetime)
22587e860f15SJohn Edward Broadbent         {
2259c51afd54SEd Tanous             setDateTime(asyncResp, *datetime);
22607e860f15SJohn Edward Broadbent         }
22617e860f15SJohn Edward Broadbent     });
22627e860f15SJohn Edward Broadbent }
22637e860f15SJohn Edward Broadbent 
22647e860f15SJohn Edward Broadbent inline void requestRoutesManagerCollection(App& app)
22657e860f15SJohn Edward Broadbent {
22667e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
2267ed398213SEd Tanous         .privileges(redfish::privileges::getManagerCollection)
22687e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
226945ca1b86SEd Tanous             [&app](const crow::Request& req,
22707e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
22713ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
227245ca1b86SEd Tanous         {
227345ca1b86SEd Tanous             return;
227445ca1b86SEd Tanous         }
227583ff9ab6SJames Feist         // Collections don't include the static data added by SubRoute
227683ff9ab6SJames Feist         // because it has a duplicate entry for members
22778d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
22788d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
22798d1b46d7Szhanghch05             "#ManagerCollection.ManagerCollection";
22808d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Manager Collection";
22818d1b46d7Szhanghch05         asyncResp->res.jsonValue["Members@odata.count"] = 1;
22821476687dSEd Tanous         nlohmann::json::array_t members;
22831476687dSEd Tanous         nlohmann::json& bmc = members.emplace_back();
22841476687dSEd Tanous         bmc["@odata.id"] = "/redfish/v1/Managers/bmc";
22851476687dSEd Tanous         asyncResp->res.jsonValue["Members"] = std::move(members);
22867e860f15SJohn Edward Broadbent     });
22879c310685SBorawski.Lukasz }
22889c310685SBorawski.Lukasz } // namespace redfish
2289