xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision bd79bce8c3f1deb1fb2773868b9ece25233cf27b)
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"
22539d8c6bSEd Tanous #include "generated/enums/action_info.hpp"
23539d8c6bSEd Tanous #include "generated/enums/manager.hpp"
24539d8c6bSEd Tanous #include "generated/enums/resource.hpp"
25a51fc2d2SSui Chen #include "query.hpp"
26c5d03ff4SJennifer Lee #include "redfish_util.hpp"
27a51fc2d2SSui Chen #include "registries/privilege_registry.hpp"
28fac6e53bSKrzysztof Grobelny #include "utils/dbus_utils.hpp"
293ccb3adbSEd Tanous #include "utils/json_utils.hpp"
30a51fc2d2SSui Chen #include "utils/sw_utils.hpp"
31a51fc2d2SSui Chen #include "utils/systemd_utils.hpp"
322b82937eSEd Tanous #include "utils/time_utils.hpp"
339c310685SBorawski.Lukasz 
34e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
35ef4c65b7SEd Tanous #include <boost/url/format.hpp>
36fac6e53bSKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
37fac6e53bSKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
381214b7e7SGunnar Mills 
39a170f275SEd Tanous #include <algorithm>
40e99073f5SGeorge Liu #include <array>
414bfefa74SGunnar Mills #include <cstdint>
421214b7e7SGunnar Mills #include <memory>
439970e93fSKonstantin Aladyshev #include <optional>
443544d2a7SEd Tanous #include <ranges>
451214b7e7SGunnar Mills #include <sstream>
469970e93fSKonstantin Aladyshev #include <string>
47e99073f5SGeorge Liu #include <string_view>
48abf2add6SEd Tanous #include <variant>
495b4aa86bSJames Feist 
501abe55efSEd Tanous namespace redfish
511abe55efSEd Tanous {
52ed5befbdSJennifer Lee 
53ed5befbdSJennifer Lee /**
542a5c4407SGunnar Mills  * Function reboots the BMC.
552a5c4407SGunnar Mills  *
562a5c4407SGunnar Mills  * @param[in] asyncResp - Shared pointer for completing asynchronous calls
57ed5befbdSJennifer Lee  */
588d1b46d7Szhanghch05 inline void
598d1b46d7Szhanghch05     doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
60ed5befbdSJennifer Lee {
61ed5befbdSJennifer Lee     const char* processName = "xyz.openbmc_project.State.BMC";
62ed5befbdSJennifer Lee     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
63ed5befbdSJennifer Lee     const char* interfaceName = "xyz.openbmc_project.State.BMC";
64ed5befbdSJennifer Lee     const std::string& propertyValue =
65ed5befbdSJennifer Lee         "xyz.openbmc_project.State.BMC.Transition.Reboot";
66ed5befbdSJennifer Lee     const char* destProperty = "RequestedBMCTransition";
67ed5befbdSJennifer Lee 
68ed5befbdSJennifer Lee     // Create the D-Bus variant for D-Bus call.
699ae226faSGeorge Liu     sdbusplus::asio::setProperty(
709ae226faSGeorge Liu         *crow::connections::systemBus, processName, objectPath, interfaceName,
719ae226faSGeorge Liu         destProperty, propertyValue,
725e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
73ed5befbdSJennifer Lee             // Use "Set" method to set the property value.
74ed5befbdSJennifer Lee             if (ec)
75ed5befbdSJennifer Lee             {
7662598e31SEd Tanous                 BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
77ed5befbdSJennifer Lee                 messages::internalError(asyncResp->res);
78ed5befbdSJennifer Lee                 return;
79ed5befbdSJennifer Lee             }
80ed5befbdSJennifer Lee 
81ed5befbdSJennifer Lee             messages::success(asyncResp->res);
829ae226faSGeorge Liu         });
83ed5befbdSJennifer Lee }
842a5c4407SGunnar Mills 
858d1b46d7Szhanghch05 inline void
868d1b46d7Szhanghch05     doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
87f92af389SJayaprakash Mutyala {
88f92af389SJayaprakash Mutyala     const char* processName = "xyz.openbmc_project.State.BMC";
89f92af389SJayaprakash Mutyala     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
90f92af389SJayaprakash Mutyala     const char* interfaceName = "xyz.openbmc_project.State.BMC";
91f92af389SJayaprakash Mutyala     const std::string& propertyValue =
92f92af389SJayaprakash Mutyala         "xyz.openbmc_project.State.BMC.Transition.HardReboot";
93f92af389SJayaprakash Mutyala     const char* destProperty = "RequestedBMCTransition";
94f92af389SJayaprakash Mutyala 
95f92af389SJayaprakash Mutyala     // Create the D-Bus variant for D-Bus call.
969ae226faSGeorge Liu     sdbusplus::asio::setProperty(
979ae226faSGeorge Liu         *crow::connections::systemBus, processName, objectPath, interfaceName,
989ae226faSGeorge Liu         destProperty, propertyValue,
995e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
100f92af389SJayaprakash Mutyala             // Use "Set" method to set the property value.
101f92af389SJayaprakash Mutyala             if (ec)
102f92af389SJayaprakash Mutyala             {
10362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
104f92af389SJayaprakash Mutyala                 messages::internalError(asyncResp->res);
105f92af389SJayaprakash Mutyala                 return;
106f92af389SJayaprakash Mutyala             }
107f92af389SJayaprakash Mutyala 
108f92af389SJayaprakash Mutyala             messages::success(asyncResp->res);
1099ae226faSGeorge Liu         });
110f92af389SJayaprakash Mutyala }
111f92af389SJayaprakash Mutyala 
1122a5c4407SGunnar Mills /**
1132a5c4407SGunnar Mills  * ManagerResetAction class supports the POST method for the Reset (reboot)
1142a5c4407SGunnar Mills  * action.
1152a5c4407SGunnar Mills  */
1167e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetAction(App& app)
1172a5c4407SGunnar Mills {
1182a5c4407SGunnar Mills     /**
1192a5c4407SGunnar Mills      * Function handles POST method request.
1202a5c4407SGunnar Mills      * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
121f92af389SJayaprakash Mutyala      * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
1222a5c4407SGunnar Mills      */
1237e860f15SJohn Edward Broadbent 
124253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/Actions/Manager.Reset/")
125ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
1267e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
12745ca1b86SEd Tanous             [&app](const crow::Request& req,
128253f11b8SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
129253f11b8SEd Tanous                    const std::string& managerId) {
1303ba00073SCarson Labrado                 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
13145ca1b86SEd Tanous                 {
13245ca1b86SEd Tanous                     return;
13345ca1b86SEd Tanous                 }
134253f11b8SEd Tanous                 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
135253f11b8SEd Tanous                 {
136*bd79bce8SPatrick Williams                     messages::resourceNotFound(asyncResp->res, "Manager",
137*bd79bce8SPatrick Williams                                                managerId);
138253f11b8SEd Tanous                     return;
139253f11b8SEd Tanous                 }
140253f11b8SEd Tanous 
14162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Post Manager Reset.");
1422a5c4407SGunnar Mills 
1432a5c4407SGunnar Mills                 std::string resetType;
1442a5c4407SGunnar Mills 
14515ed6780SWilly Tu                 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
1467e860f15SJohn Edward Broadbent                                                resetType))
1472a5c4407SGunnar Mills                 {
1482a5c4407SGunnar Mills                     return;
1492a5c4407SGunnar Mills                 }
1502a5c4407SGunnar Mills 
151f92af389SJayaprakash Mutyala                 if (resetType == "GracefulRestart")
152f92af389SJayaprakash Mutyala                 {
15362598e31SEd Tanous                     BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
154f92af389SJayaprakash Mutyala                     doBMCGracefulRestart(asyncResp);
155f92af389SJayaprakash Mutyala                     return;
156f92af389SJayaprakash Mutyala                 }
1573174e4dfSEd Tanous                 if (resetType == "ForceRestart")
158f92af389SJayaprakash Mutyala                 {
15962598e31SEd Tanous                     BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
160f92af389SJayaprakash Mutyala                     doBMCForceRestart(asyncResp);
161f92af389SJayaprakash Mutyala                     return;
162f92af389SJayaprakash Mutyala                 }
163*bd79bce8SPatrick Williams                 BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}",
164*bd79bce8SPatrick Williams                                  resetType);
1652a5c4407SGunnar Mills                 messages::actionParameterNotSupported(asyncResp->res, resetType,
1662a5c4407SGunnar Mills                                                       "ResetType");
1672a5c4407SGunnar Mills 
1682a5c4407SGunnar Mills                 return;
1697e860f15SJohn Edward Broadbent             });
1702a5c4407SGunnar Mills }
171ed5befbdSJennifer Lee 
1723e40fc74SGunnar Mills /**
1733e40fc74SGunnar Mills  * ManagerResetToDefaultsAction class supports POST method for factory reset
1743e40fc74SGunnar Mills  * action.
1753e40fc74SGunnar Mills  */
1767e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetToDefaultsAction(App& app)
1773e40fc74SGunnar Mills {
1783e40fc74SGunnar Mills     /**
1793e40fc74SGunnar Mills      * Function handles ResetToDefaults POST method request.
1803e40fc74SGunnar Mills      *
1813e40fc74SGunnar Mills      * Analyzes POST body message and factory resets BMC by calling
1823e40fc74SGunnar Mills      * BMC code updater factory reset followed by a BMC reboot.
1833e40fc74SGunnar Mills      *
1843e40fc74SGunnar Mills      * BMC code updater factory reset wipes the whole BMC read-write
1853e40fc74SGunnar Mills      * filesystem which includes things like the network settings.
1863e40fc74SGunnar Mills      *
1873e40fc74SGunnar Mills      * OpenBMC only supports ResetToDefaultsType "ResetAll".
1883e40fc74SGunnar Mills      */
1897e860f15SJohn Edward Broadbent 
1907e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
191253f11b8SEd Tanous                  "/redfish/v1/Managers/<str>/Actions/Manager.ResetToDefaults/")
192ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
193*bd79bce8SPatrick Williams         .methods(
194*bd79bce8SPatrick Williams             boost::beast::http::verb::
195*bd79bce8SPatrick Williams                 post)([&app](
196*bd79bce8SPatrick Williams                           const crow::Request& req,
197253f11b8SEd Tanous                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
198253f11b8SEd Tanous                           const std::string& managerId) {
1993ba00073SCarson Labrado             if (!redfish::setUpRedfishRoute(app, req, asyncResp))
20045ca1b86SEd Tanous             {
20145ca1b86SEd Tanous                 return;
20245ca1b86SEd Tanous             }
203253f11b8SEd Tanous 
204253f11b8SEd Tanous             if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
205253f11b8SEd Tanous             {
206*bd79bce8SPatrick Williams                 messages::resourceNotFound(asyncResp->res, "Manager",
207*bd79bce8SPatrick Williams                                            managerId);
208253f11b8SEd Tanous                 return;
209253f11b8SEd Tanous             }
210253f11b8SEd Tanous 
21162598e31SEd Tanous             BMCWEB_LOG_DEBUG("Post ResetToDefaults.");
2123e40fc74SGunnar Mills 
2139970e93fSKonstantin Aladyshev             std::optional<std::string> resetType;
2149970e93fSKonstantin Aladyshev             std::optional<std::string> resetToDefaultsType;
2153e40fc74SGunnar Mills 
2169970e93fSKonstantin Aladyshev             if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
2179970e93fSKonstantin Aladyshev                                            resetType, "ResetToDefaultsType",
2189970e93fSKonstantin Aladyshev                                            resetToDefaultsType))
2193e40fc74SGunnar Mills             {
2209970e93fSKonstantin Aladyshev                 BMCWEB_LOG_DEBUG("Missing property ResetType.");
2213e40fc74SGunnar Mills 
222*bd79bce8SPatrick Williams                 messages::actionParameterMissing(
223*bd79bce8SPatrick Williams                     asyncResp->res, "ResetToDefaults", "ResetType");
2243e40fc74SGunnar Mills                 return;
2253e40fc74SGunnar Mills             }
2263e40fc74SGunnar Mills 
2279970e93fSKonstantin Aladyshev             if (resetToDefaultsType && !resetType)
2289970e93fSKonstantin Aladyshev             {
2299970e93fSKonstantin Aladyshev                 BMCWEB_LOG_WARNING(
2309970e93fSKonstantin Aladyshev                     "Using deprecated ResetToDefaultsType, should be ResetType."
2319970e93fSKonstantin Aladyshev                     "Support for the ResetToDefaultsType will be dropped in 2Q24");
2329970e93fSKonstantin Aladyshev                 resetType = resetToDefaultsType;
2339970e93fSKonstantin Aladyshev             }
2349970e93fSKonstantin Aladyshev 
2353e40fc74SGunnar Mills             if (resetType != "ResetAll")
2363e40fc74SGunnar Mills             {
2379970e93fSKonstantin Aladyshev                 BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}",
2389970e93fSKonstantin Aladyshev                                  *resetType);
239*bd79bce8SPatrick Williams                 messages::actionParameterNotSupported(asyncResp->res,
240*bd79bce8SPatrick Williams                                                       *resetType, "ResetType");
2413e40fc74SGunnar Mills                 return;
2423e40fc74SGunnar Mills             }
2433e40fc74SGunnar Mills 
2443e40fc74SGunnar Mills             crow::connections::systemBus->async_method_call(
2455e7e2dc5SEd Tanous                 [asyncResp](const boost::system::error_code& ec) {
2463e40fc74SGunnar Mills                     if (ec)
2473e40fc74SGunnar Mills                     {
24862598e31SEd Tanous                         BMCWEB_LOG_DEBUG("Failed to ResetToDefaults: {}", ec);
2493e40fc74SGunnar Mills                         messages::internalError(asyncResp->res);
2503e40fc74SGunnar Mills                         return;
2513e40fc74SGunnar Mills                     }
2523e40fc74SGunnar Mills                     // Factory Reset doesn't actually happen until a reboot
2533e40fc74SGunnar Mills                     // Can't erase what the BMC is running on
2543e40fc74SGunnar Mills                     doBMCGracefulRestart(asyncResp);
2553e40fc74SGunnar Mills                 },
2563e40fc74SGunnar Mills                 "xyz.openbmc_project.Software.BMC.Updater",
2573e40fc74SGunnar Mills                 "/xyz/openbmc_project/software",
2583e40fc74SGunnar Mills                 "xyz.openbmc_project.Common.FactoryReset", "Reset");
2597e860f15SJohn Edward Broadbent         });
2603e40fc74SGunnar Mills }
2613e40fc74SGunnar Mills 
2621cb1a9e6SAppaRao Puli /**
2631cb1a9e6SAppaRao Puli  * ManagerResetActionInfo derived class for delivering Manager
2641cb1a9e6SAppaRao Puli  * ResetType AllowableValues using ResetInfo schema.
2651cb1a9e6SAppaRao Puli  */
2667e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetActionInfo(App& app)
2671cb1a9e6SAppaRao Puli {
2681cb1a9e6SAppaRao Puli     /**
2691cb1a9e6SAppaRao Puli      * Functions triggers appropriate requests on DBus
2701cb1a9e6SAppaRao Puli      */
2717e860f15SJohn Edward Broadbent 
272253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/ResetActionInfo/")
273ed398213SEd Tanous         .privileges(redfish::privileges::getActionInfo)
2747e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
27545ca1b86SEd Tanous             [&app](const crow::Request& req,
276253f11b8SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
277253f11b8SEd Tanous                    const std::string& managerId) {
2783ba00073SCarson Labrado                 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
27945ca1b86SEd Tanous                 {
28045ca1b86SEd Tanous                     return;
28145ca1b86SEd Tanous                 }
2821476687dSEd Tanous 
283253f11b8SEd Tanous                 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
284253f11b8SEd Tanous                 {
285*bd79bce8SPatrick Williams                     messages::resourceNotFound(asyncResp->res, "Manager",
286*bd79bce8SPatrick Williams                                                managerId);
287253f11b8SEd Tanous                     return;
288253f11b8SEd Tanous                 }
289253f11b8SEd Tanous 
2901476687dSEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
2911476687dSEd Tanous                     "#ActionInfo.v1_1_2.ActionInfo";
292*bd79bce8SPatrick Williams                 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
293*bd79bce8SPatrick Williams                     "/redfish/v1/Managers/{}/ResetActionInfo",
294253f11b8SEd Tanous                     BMCWEB_REDFISH_MANAGER_URI_NAME);
2951476687dSEd Tanous                 asyncResp->res.jsonValue["Name"] = "Reset Action Info";
2961476687dSEd Tanous                 asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
2971476687dSEd Tanous                 nlohmann::json::object_t parameter;
2981476687dSEd Tanous                 parameter["Name"] = "ResetType";
2991476687dSEd Tanous                 parameter["Required"] = true;
300539d8c6bSEd Tanous                 parameter["DataType"] = action_info::ParameterTypes::String;
3011476687dSEd Tanous 
3021476687dSEd Tanous                 nlohmann::json::array_t allowableValues;
303ad539545SPatrick Williams                 allowableValues.emplace_back("GracefulRestart");
304ad539545SPatrick Williams                 allowableValues.emplace_back("ForceRestart");
3051476687dSEd Tanous                 parameter["AllowableValues"] = std::move(allowableValues);
3061476687dSEd Tanous 
3071476687dSEd Tanous                 nlohmann::json::array_t parameters;
308ad539545SPatrick Williams                 parameters.emplace_back(std::move(parameter));
3091476687dSEd Tanous 
3101476687dSEd Tanous                 asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
3117e860f15SJohn Edward Broadbent             });
3121cb1a9e6SAppaRao Puli }
3131cb1a9e6SAppaRao Puli 
3145b4aa86bSJames Feist static constexpr const char* objectManagerIface =
3155b4aa86bSJames Feist     "org.freedesktop.DBus.ObjectManager";
3165b4aa86bSJames Feist static constexpr const char* pidConfigurationIface =
3175b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid";
3185b4aa86bSJames Feist static constexpr const char* pidZoneConfigurationIface =
3195b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid.Zone";
320b7a08d04SJames Feist static constexpr const char* stepwiseConfigurationIface =
321b7a08d04SJames Feist     "xyz.openbmc_project.Configuration.Stepwise";
32273df0db0SJames Feist static constexpr const char* thermalModeIface =
32373df0db0SJames Feist     "xyz.openbmc_project.Control.ThermalMode";
3249c310685SBorawski.Lukasz 
3258d1b46d7Szhanghch05 inline void
3268d1b46d7Szhanghch05     asyncPopulatePid(const std::string& connection, const std::string& path,
32773df0db0SJames Feist                      const std::string& currentProfile,
32873df0db0SJames Feist                      const std::vector<std::string>& supportedProfiles,
3298d1b46d7Szhanghch05                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
3305b4aa86bSJames Feist {
3315eb468daSGeorge Liu     sdbusplus::message::object_path objPath(path);
3325eb468daSGeorge Liu     dbus::utility::getManagedObjects(
3335eb468daSGeorge Liu         connection, objPath,
33473df0db0SJames Feist         [asyncResp, currentProfile, supportedProfiles](
3355e7e2dc5SEd Tanous             const boost::system::error_code& ec,
3365b4aa86bSJames Feist             const dbus::utility::ManagedObjectType& managedObj) {
3375b4aa86bSJames Feist             if (ec)
3385b4aa86bSJames Feist             {
33962598e31SEd Tanous                 BMCWEB_LOG_ERROR("{}", ec);
340f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
3415b4aa86bSJames Feist                 return;
3425b4aa86bSJames Feist             }
3435b4aa86bSJames Feist             nlohmann::json& configRoot =
3445b4aa86bSJames Feist                 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
3455b4aa86bSJames Feist             nlohmann::json& fans = configRoot["FanControllers"];
346*bd79bce8SPatrick Williams             fans["@odata.type"] =
347*bd79bce8SPatrick Williams                 "#OpenBMCManager.v1_0_0.Manager.FanControllers";
348253f11b8SEd Tanous             fans["@odata.id"] = boost::urls::format(
349253f11b8SEd Tanous                 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/FanControllers",
350253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME);
3515b4aa86bSJames Feist 
3525b4aa86bSJames Feist             nlohmann::json& pids = configRoot["PidControllers"];
353*bd79bce8SPatrick Williams             pids["@odata.type"] =
354*bd79bce8SPatrick Williams                 "#OpenBMCManager.v1_0_0.Manager.PidControllers";
355253f11b8SEd Tanous             pids["@odata.id"] = boost::urls::format(
356253f11b8SEd Tanous                 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/PidControllers",
357253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME);
3585b4aa86bSJames Feist 
359b7a08d04SJames Feist             nlohmann::json& stepwise = configRoot["StepwiseControllers"];
360fc1cdd14SEd Tanous             stepwise["@odata.type"] =
361fc1cdd14SEd Tanous                 "#OpenBMCManager.v1_0_0.Manager.StepwiseControllers";
362253f11b8SEd Tanous             stepwise["@odata.id"] = boost::urls::format(
363253f11b8SEd Tanous                 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/StepwiseControllers",
364253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME);
365b7a08d04SJames Feist 
3665b4aa86bSJames Feist             nlohmann::json& zones = configRoot["FanZones"];
367253f11b8SEd Tanous             zones["@odata.id"] = boost::urls::format(
368253f11b8SEd Tanous                 "/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan/FanZones",
369253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME);
370fc1cdd14SEd Tanous             zones["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager.FanZones";
371253f11b8SEd Tanous             configRoot["@odata.id"] =
372253f11b8SEd Tanous                 boost::urls::format("/redfish/v1/Managers/{}#/Oem/OpenBmc/Fan",
373253f11b8SEd Tanous                                     BMCWEB_REDFISH_MANAGER_URI_NAME);
374fc1cdd14SEd Tanous             configRoot["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager.Fan";
37573df0db0SJames Feist             configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
37673df0db0SJames Feist 
37773df0db0SJames Feist             if (!currentProfile.empty())
37873df0db0SJames Feist             {
37973df0db0SJames Feist                 configRoot["Profile"] = currentProfile;
38073df0db0SJames Feist             }
381bf2ddedeSCarson Labrado             BMCWEB_LOG_DEBUG("profile = {} !", currentProfile);
3825b4aa86bSJames Feist 
3835b4aa86bSJames Feist             for (const auto& pathPair : managedObj)
3845b4aa86bSJames Feist             {
3855b4aa86bSJames Feist                 for (const auto& intfPair : pathPair.second)
3865b4aa86bSJames Feist                 {
3875b4aa86bSJames Feist                     if (intfPair.first != pidConfigurationIface &&
388b7a08d04SJames Feist                         intfPair.first != pidZoneConfigurationIface &&
389b7a08d04SJames Feist                         intfPair.first != stepwiseConfigurationIface)
3905b4aa86bSJames Feist                     {
3915b4aa86bSJames Feist                         continue;
3925b4aa86bSJames Feist                     }
39373df0db0SJames Feist 
394711ac7a9SEd Tanous                     std::string name;
395711ac7a9SEd Tanous 
396711ac7a9SEd Tanous                     for (const std::pair<std::string,
397*bd79bce8SPatrick Williams                                          dbus::utility::DbusVariantType>&
398*bd79bce8SPatrick Williams                              propPair : intfPair.second)
399711ac7a9SEd Tanous                     {
400711ac7a9SEd Tanous                         if (propPair.first == "Name")
401711ac7a9SEd Tanous                         {
4025b4aa86bSJames Feist                             const std::string* namePtr =
403711ac7a9SEd Tanous                                 std::get_if<std::string>(&propPair.second);
4045b4aa86bSJames Feist                             if (namePtr == nullptr)
4055b4aa86bSJames Feist                             {
40662598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Pid Name Field illegal");
407b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
4085b4aa86bSJames Feist                                 return;
4095b4aa86bSJames Feist                             }
410db697703SWilly Tu                             name = *namePtr;
4115b4aa86bSJames Feist                             dbus::utility::escapePathForDbus(name);
412711ac7a9SEd Tanous                         }
413711ac7a9SEd Tanous                         else if (propPair.first == "Profiles")
41473df0db0SJames Feist                         {
41573df0db0SJames Feist                             const std::vector<std::string>* profiles =
41673df0db0SJames Feist                                 std::get_if<std::vector<std::string>>(
417711ac7a9SEd Tanous                                     &propPair.second);
41873df0db0SJames Feist                             if (profiles == nullptr)
41973df0db0SJames Feist                             {
42062598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Pid Profiles Field illegal");
42173df0db0SJames Feist                                 messages::internalError(asyncResp->res);
42273df0db0SJames Feist                                 return;
42373df0db0SJames Feist                             }
42473df0db0SJames Feist                             if (std::find(profiles->begin(), profiles->end(),
42573df0db0SJames Feist                                           currentProfile) == profiles->end())
42673df0db0SJames Feist                             {
42762598e31SEd Tanous                                 BMCWEB_LOG_INFO(
428*bd79bce8SPatrick Williams                                     "{} not supported in current profile",
429*bd79bce8SPatrick Williams                                     name);
43073df0db0SJames Feist                                 continue;
43173df0db0SJames Feist                             }
43273df0db0SJames Feist                         }
433711ac7a9SEd Tanous                     }
434b7a08d04SJames Feist                     nlohmann::json* config = nullptr;
435c33a90ecSJames Feist                     const std::string* classPtr = nullptr;
436711ac7a9SEd Tanous 
437711ac7a9SEd Tanous                     for (const std::pair<std::string,
438*bd79bce8SPatrick Williams                                          dbus::utility::DbusVariantType>&
439*bd79bce8SPatrick Williams                              propPair : intfPair.second)
440c33a90ecSJames Feist                     {
441727dc83fSLei YU                         if (propPair.first == "Class")
442711ac7a9SEd Tanous                         {
443*bd79bce8SPatrick Williams                             classPtr =
444*bd79bce8SPatrick Williams                                 std::get_if<std::string>(&propPair.second);
445711ac7a9SEd Tanous                         }
446c33a90ecSJames Feist                     }
447c33a90ecSJames Feist 
448253f11b8SEd Tanous                     boost::urls::url url(
449253f11b8SEd Tanous                         boost::urls::format("/redfish/v1/Managers/{}",
450253f11b8SEd Tanous                                             BMCWEB_REDFISH_MANAGER_URI_NAME));
4515b4aa86bSJames Feist                     if (intfPair.first == pidZoneConfigurationIface)
4525b4aa86bSJames Feist                     {
4535b4aa86bSJames Feist                         std::string chassis;
454*bd79bce8SPatrick Williams                         if (!dbus::utility::getNthStringFromPath(
455*bd79bce8SPatrick Williams                                 pathPair.first.str, 5, chassis))
4565b4aa86bSJames Feist                         {
4575b4aa86bSJames Feist                             chassis = "#IllegalValue";
4585b4aa86bSJames Feist                         }
4595b4aa86bSJames Feist                         nlohmann::json& zone = zones[name];
460*bd79bce8SPatrick Williams                         zone["Chassis"]["@odata.id"] = boost::urls::format(
461*bd79bce8SPatrick Williams                             "/redfish/v1/Chassis/{}", chassis);
462eddfc437SWilly Tu                         url.set_fragment(
463eddfc437SWilly Tu                             ("/Oem/OpenBmc/Fan/FanZones"_json_pointer / name)
464eddfc437SWilly Tu                                 .to_string());
465eddfc437SWilly Tu                         zone["@odata.id"] = std::move(url);
466fc1cdd14SEd Tanous                         zone["@odata.type"] =
467fc1cdd14SEd Tanous                             "#OpenBMCManager.v1_0_0.Manager.FanZone";
468b7a08d04SJames Feist                         config = &zone;
4695b4aa86bSJames Feist                     }
4705b4aa86bSJames Feist 
471b7a08d04SJames Feist                     else if (intfPair.first == stepwiseConfigurationIface)
4725b4aa86bSJames Feist                     {
473c33a90ecSJames Feist                         if (classPtr == nullptr)
474c33a90ecSJames Feist                         {
47562598e31SEd Tanous                             BMCWEB_LOG_ERROR("Pid Class Field illegal");
476c33a90ecSJames Feist                             messages::internalError(asyncResp->res);
477c33a90ecSJames Feist                             return;
478c33a90ecSJames Feist                         }
479c33a90ecSJames Feist 
480b7a08d04SJames Feist                         nlohmann::json& controller = stepwise[name];
481b7a08d04SJames Feist                         config = &controller;
482eddfc437SWilly Tu                         url.set_fragment(
483eddfc437SWilly Tu                             ("/Oem/OpenBmc/Fan/StepwiseControllers"_json_pointer /
484eddfc437SWilly Tu                              name)
485eddfc437SWilly Tu                                 .to_string());
486eddfc437SWilly Tu                         controller["@odata.id"] = std::move(url);
487b7a08d04SJames Feist                         controller["@odata.type"] =
488fc1cdd14SEd Tanous                             "#OpenBMCManager.v1_0_0.Manager.StepwiseController";
489b7a08d04SJames Feist 
490c33a90ecSJames Feist                         controller["Direction"] = *classPtr;
4915b4aa86bSJames Feist                     }
4925b4aa86bSJames Feist 
4935b4aa86bSJames Feist                     // pid and fans are off the same configuration
494b7a08d04SJames Feist                     else if (intfPair.first == pidConfigurationIface)
4955b4aa86bSJames Feist                     {
4965b4aa86bSJames Feist                         if (classPtr == nullptr)
4975b4aa86bSJames Feist                         {
49862598e31SEd Tanous                             BMCWEB_LOG_ERROR("Pid Class Field illegal");
499a08b46ccSJason M. Bills                             messages::internalError(asyncResp->res);
5005b4aa86bSJames Feist                             return;
5015b4aa86bSJames Feist                         }
5025b4aa86bSJames Feist                         bool isFan = *classPtr == "fan";
503*bd79bce8SPatrick Williams                         nlohmann::json& element =
504*bd79bce8SPatrick Williams                             isFan ? fans[name] : pids[name];
505b7a08d04SJames Feist                         config = &element;
5065b4aa86bSJames Feist                         if (isFan)
5075b4aa86bSJames Feist                         {
508eddfc437SWilly Tu                             url.set_fragment(
509eddfc437SWilly Tu                                 ("/Oem/OpenBmc/Fan/FanControllers"_json_pointer /
510eddfc437SWilly Tu                                  name)
511eddfc437SWilly Tu                                     .to_string());
512eddfc437SWilly Tu                             element["@odata.id"] = std::move(url);
513fc1cdd14SEd Tanous                             element["@odata.type"] =
514fc1cdd14SEd Tanous                                 "#OpenBMCManager.v1_0_0.Manager.FanController";
5155b4aa86bSJames Feist                         }
5165b4aa86bSJames Feist                         else
5175b4aa86bSJames Feist                         {
518eddfc437SWilly Tu                             url.set_fragment(
519eddfc437SWilly Tu                                 ("/Oem/OpenBmc/Fan/PidControllers"_json_pointer /
520eddfc437SWilly Tu                                  name)
521eddfc437SWilly Tu                                     .to_string());
522eddfc437SWilly Tu                             element["@odata.id"] = std::move(url);
523fc1cdd14SEd Tanous                             element["@odata.type"] =
524fc1cdd14SEd Tanous                                 "#OpenBMCManager.v1_0_0.Manager.PidController";
5255b4aa86bSJames Feist                         }
526b7a08d04SJames Feist                     }
527b7a08d04SJames Feist                     else
528b7a08d04SJames Feist                     {
52962598e31SEd Tanous                         BMCWEB_LOG_ERROR("Unexpected configuration");
530b7a08d04SJames Feist                         messages::internalError(asyncResp->res);
531b7a08d04SJames Feist                         return;
532b7a08d04SJames Feist                     }
533b7a08d04SJames Feist 
534b7a08d04SJames Feist                     // used for making maps out of 2 vectors
535b7a08d04SJames Feist                     const std::vector<double>* keys = nullptr;
536b7a08d04SJames Feist                     const std::vector<double>* values = nullptr;
537b7a08d04SJames Feist 
538b7a08d04SJames Feist                     for (const auto& propertyPair : intfPair.second)
539b7a08d04SJames Feist                     {
540b7a08d04SJames Feist                         if (propertyPair.first == "Type" ||
541b7a08d04SJames Feist                             propertyPair.first == "Class" ||
542b7a08d04SJames Feist                             propertyPair.first == "Name")
543b7a08d04SJames Feist                         {
544b7a08d04SJames Feist                             continue;
545b7a08d04SJames Feist                         }
546b7a08d04SJames Feist 
547b7a08d04SJames Feist                         // zones
548b7a08d04SJames Feist                         if (intfPair.first == pidZoneConfigurationIface)
549b7a08d04SJames Feist                         {
550b7a08d04SJames Feist                             const double* ptr =
551abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
552b7a08d04SJames Feist                             if (ptr == nullptr)
553b7a08d04SJames Feist                             {
55462598e31SEd Tanous                                 BMCWEB_LOG_ERROR("Field Illegal {}",
55562598e31SEd Tanous                                                  propertyPair.first);
556b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
557b7a08d04SJames Feist                                 return;
558b7a08d04SJames Feist                             }
559b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
560b7a08d04SJames Feist                         }
561b7a08d04SJames Feist 
562b7a08d04SJames Feist                         if (intfPair.first == stepwiseConfigurationIface)
563b7a08d04SJames Feist                         {
564b7a08d04SJames Feist                             if (propertyPair.first == "Reading" ||
565b7a08d04SJames Feist                                 propertyPair.first == "Output")
566b7a08d04SJames Feist                             {
567b7a08d04SJames Feist                                 const std::vector<double>* ptr =
568abf2add6SEd Tanous                                     std::get_if<std::vector<double>>(
569b7a08d04SJames Feist                                         &propertyPair.second);
570b7a08d04SJames Feist 
571b7a08d04SJames Feist                                 if (ptr == nullptr)
572b7a08d04SJames Feist                                 {
57362598e31SEd Tanous                                     BMCWEB_LOG_ERROR("Field Illegal {}",
57462598e31SEd Tanous                                                      propertyPair.first);
575b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
576b7a08d04SJames Feist                                     return;
577b7a08d04SJames Feist                                 }
578b7a08d04SJames Feist 
579b7a08d04SJames Feist                                 if (propertyPair.first == "Reading")
580b7a08d04SJames Feist                                 {
581b7a08d04SJames Feist                                     keys = ptr;
582b7a08d04SJames Feist                                 }
583b7a08d04SJames Feist                                 else
584b7a08d04SJames Feist                                 {
585b7a08d04SJames Feist                                     values = ptr;
586b7a08d04SJames Feist                                 }
587e662eae8SEd Tanous                                 if (keys != nullptr && values != nullptr)
588b7a08d04SJames Feist                                 {
589b7a08d04SJames Feist                                     if (keys->size() != values->size())
590b7a08d04SJames Feist                                     {
59162598e31SEd Tanous                                         BMCWEB_LOG_ERROR(
59262598e31SEd Tanous                                             "Reading and Output size don't match ");
593b7a08d04SJames Feist                                         messages::internalError(asyncResp->res);
594b7a08d04SJames Feist                                         return;
595b7a08d04SJames Feist                                     }
596b7a08d04SJames Feist                                     nlohmann::json& steps = (*config)["Steps"];
597b7a08d04SJames Feist                                     steps = nlohmann::json::array();
598b7a08d04SJames Feist                                     for (size_t ii = 0; ii < keys->size(); ii++)
599b7a08d04SJames Feist                                     {
6001476687dSEd Tanous                                         nlohmann::json::object_t step;
6011476687dSEd Tanous                                         step["Target"] = (*keys)[ii];
6021476687dSEd Tanous                                         step["Output"] = (*values)[ii];
603b2ba3072SPatrick Williams                                         steps.emplace_back(std::move(step));
604b7a08d04SJames Feist                                     }
605b7a08d04SJames Feist                                 }
606b7a08d04SJames Feist                             }
607b7a08d04SJames Feist                             if (propertyPair.first == "NegativeHysteresis" ||
608b7a08d04SJames Feist                                 propertyPair.first == "PositiveHysteresis")
609b7a08d04SJames Feist                             {
610b7a08d04SJames Feist                                 const double* ptr =
611abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
612b7a08d04SJames Feist                                 if (ptr == nullptr)
613b7a08d04SJames Feist                                 {
61462598e31SEd Tanous                                     BMCWEB_LOG_ERROR("Field Illegal {}",
61562598e31SEd Tanous                                                      propertyPair.first);
616b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
617b7a08d04SJames Feist                                     return;
618b7a08d04SJames Feist                                 }
619b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
620b7a08d04SJames Feist                             }
621b7a08d04SJames Feist                         }
622b7a08d04SJames Feist 
623b7a08d04SJames Feist                         // pid and fans are off the same configuration
624b7a08d04SJames Feist                         if (intfPair.first == pidConfigurationIface ||
625b7a08d04SJames Feist                             intfPair.first == stepwiseConfigurationIface)
626b7a08d04SJames Feist                         {
6275b4aa86bSJames Feist                             if (propertyPair.first == "Zones")
6285b4aa86bSJames Feist                             {
6295b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
630abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
6311b6b96c5SEd Tanous                                         &propertyPair.second);
6325b4aa86bSJames Feist 
6335b4aa86bSJames Feist                                 if (inputs == nullptr)
6345b4aa86bSJames Feist                                 {
63562598e31SEd Tanous                                     BMCWEB_LOG_ERROR("Zones Pid Field Illegal");
636a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
6375b4aa86bSJames Feist                                     return;
6385b4aa86bSJames Feist                                 }
639b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
6405b4aa86bSJames Feist                                 data = nlohmann::json::array();
6415b4aa86bSJames Feist                                 for (std::string itemCopy : *inputs)
6425b4aa86bSJames Feist                                 {
6435b4aa86bSJames Feist                                     dbus::utility::escapePathForDbus(itemCopy);
6441476687dSEd Tanous                                     nlohmann::json::object_t input;
645*bd79bce8SPatrick Williams                                     boost::urls::url managerUrl =
646*bd79bce8SPatrick Williams                                         boost::urls::format(
647253f11b8SEd Tanous                                             "/redfish/v1/Managers/{}#{}",
648253f11b8SEd Tanous                                             BMCWEB_REDFISH_MANAGER_URI_NAME,
649eddfc437SWilly Tu                                             ("/Oem/OpenBmc/Fan/FanZones"_json_pointer /
650eddfc437SWilly Tu                                              itemCopy)
651eddfc437SWilly Tu                                                 .to_string());
652eddfc437SWilly Tu                                     input["@odata.id"] = std::move(managerUrl);
653b2ba3072SPatrick Williams                                     data.emplace_back(std::move(input));
6545b4aa86bSJames Feist                                 }
6555b4aa86bSJames Feist                             }
6565b4aa86bSJames Feist                             // todo(james): may never happen, but this
6575b4aa86bSJames Feist                             // assumes configuration data referenced in the
6585b4aa86bSJames Feist                             // PID config is provided by the same daemon, we
6595b4aa86bSJames Feist                             // could add another loop to cover all cases,
6605b4aa86bSJames Feist                             // but I'm okay kicking this can down the road a
6615b4aa86bSJames Feist                             // bit
6625b4aa86bSJames Feist 
6635b4aa86bSJames Feist                             else if (propertyPair.first == "Inputs" ||
6645b4aa86bSJames Feist                                      propertyPair.first == "Outputs")
6655b4aa86bSJames Feist                             {
666b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
6675b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
668abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
6691b6b96c5SEd Tanous                                         &propertyPair.second);
6705b4aa86bSJames Feist 
6715b4aa86bSJames Feist                                 if (inputs == nullptr)
6725b4aa86bSJames Feist                                 {
67362598e31SEd Tanous                                     BMCWEB_LOG_ERROR("Field Illegal {}",
67462598e31SEd Tanous                                                      propertyPair.first);
675f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
6765b4aa86bSJames Feist                                     return;
6775b4aa86bSJames Feist                                 }
6785b4aa86bSJames Feist                                 data = *inputs;
679b943aaefSJames Feist                             }
680b943aaefSJames Feist                             else if (propertyPair.first == "SetPointOffset")
681b943aaefSJames Feist                             {
682b943aaefSJames Feist                                 const std::string* ptr =
683*bd79bce8SPatrick Williams                                     std::get_if<std::string>(
684*bd79bce8SPatrick Williams                                         &propertyPair.second);
685b943aaefSJames Feist 
686b943aaefSJames Feist                                 if (ptr == nullptr)
687b943aaefSJames Feist                                 {
68862598e31SEd Tanous                                     BMCWEB_LOG_ERROR("Field Illegal {}",
68962598e31SEd Tanous                                                      propertyPair.first);
690b943aaefSJames Feist                                     messages::internalError(asyncResp->res);
691b943aaefSJames Feist                                     return;
692b943aaefSJames Feist                                 }
693b943aaefSJames Feist                                 // translate from dbus to redfish
694b943aaefSJames Feist                                 if (*ptr == "WarningHigh")
695b943aaefSJames Feist                                 {
696b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
697b943aaefSJames Feist                                         "UpperThresholdNonCritical";
698b943aaefSJames Feist                                 }
699b943aaefSJames Feist                                 else if (*ptr == "WarningLow")
700b943aaefSJames Feist                                 {
701b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
702b943aaefSJames Feist                                         "LowerThresholdNonCritical";
703b943aaefSJames Feist                                 }
704b943aaefSJames Feist                                 else if (*ptr == "CriticalHigh")
705b943aaefSJames Feist                                 {
706b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
707b943aaefSJames Feist                                         "UpperThresholdCritical";
708b943aaefSJames Feist                                 }
709b943aaefSJames Feist                                 else if (*ptr == "CriticalLow")
710b943aaefSJames Feist                                 {
711b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
712b943aaefSJames Feist                                         "LowerThresholdCritical";
713b943aaefSJames Feist                                 }
714b943aaefSJames Feist                                 else
715b943aaefSJames Feist                                 {
71662598e31SEd Tanous                                     BMCWEB_LOG_ERROR("Value Illegal {}", *ptr);
717b943aaefSJames Feist                                     messages::internalError(asyncResp->res);
718b943aaefSJames Feist                                     return;
719b943aaefSJames Feist                                 }
720b943aaefSJames Feist                             }
721b943aaefSJames Feist                             // doubles
722*bd79bce8SPatrick Williams                             else if (propertyPair.first ==
723*bd79bce8SPatrick Williams                                          "FFGainCoefficient" ||
7245b4aa86bSJames Feist                                      propertyPair.first == "FFOffCoefficient" ||
7255b4aa86bSJames Feist                                      propertyPair.first == "ICoefficient" ||
7265b4aa86bSJames Feist                                      propertyPair.first == "ILimitMax" ||
7275b4aa86bSJames Feist                                      propertyPair.first == "ILimitMin" ||
728*bd79bce8SPatrick Williams                                      propertyPair.first ==
729*bd79bce8SPatrick Williams                                          "PositiveHysteresis" ||
730*bd79bce8SPatrick Williams                                      propertyPair.first ==
731*bd79bce8SPatrick Williams                                          "NegativeHysteresis" ||
7325b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMax" ||
7335b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMin" ||
7345b4aa86bSJames Feist                                      propertyPair.first == "PCoefficient" ||
7357625cb81SJames Feist                                      propertyPair.first == "SetPoint" ||
7365b4aa86bSJames Feist                                      propertyPair.first == "SlewNeg" ||
7375b4aa86bSJames Feist                                      propertyPair.first == "SlewPos")
7385b4aa86bSJames Feist                             {
7395b4aa86bSJames Feist                                 const double* ptr =
740abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
7415b4aa86bSJames Feist                                 if (ptr == nullptr)
7425b4aa86bSJames Feist                                 {
74362598e31SEd Tanous                                     BMCWEB_LOG_ERROR("Field Illegal {}",
74462598e31SEd Tanous                                                      propertyPair.first);
745f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
7465b4aa86bSJames Feist                                     return;
7475b4aa86bSJames Feist                                 }
748b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
7495b4aa86bSJames Feist                             }
7505b4aa86bSJames Feist                         }
7515b4aa86bSJames Feist                     }
7525b4aa86bSJames Feist                 }
7535b4aa86bSJames Feist             }
7545eb468daSGeorge Liu         });
7555b4aa86bSJames Feist }
756ca537928SJennifer Lee 
75783ff9ab6SJames Feist enum class CreatePIDRet
75883ff9ab6SJames Feist {
75983ff9ab6SJames Feist     fail,
76083ff9ab6SJames Feist     del,
76183ff9ab6SJames Feist     patch
76283ff9ab6SJames Feist };
76383ff9ab6SJames Feist 
7648d1b46d7Szhanghch05 inline bool
7658d1b46d7Szhanghch05     getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
7669e9b6049SEd Tanous                         std::vector<nlohmann::json::object_t>& config,
7675f2caaefSJames Feist                         std::vector<std::string>& zones)
7685f2caaefSJames Feist {
769b6baeaa4SJames Feist     if (config.empty())
770b6baeaa4SJames Feist     {
77162598e31SEd Tanous         BMCWEB_LOG_ERROR("Empty Zones");
772f818b04dSEd Tanous         messages::propertyValueFormatError(response->res, config, "Zones");
773b6baeaa4SJames Feist         return false;
774b6baeaa4SJames Feist     }
7755f2caaefSJames Feist     for (auto& odata : config)
7765f2caaefSJames Feist     {
7775f2caaefSJames Feist         std::string path;
7789e9b6049SEd Tanous         if (!redfish::json_util::readJsonObject(odata, response->res,
7799e9b6049SEd Tanous                                                 "@odata.id", path))
7805f2caaefSJames Feist         {
7815f2caaefSJames Feist             return false;
7825f2caaefSJames Feist         }
7835f2caaefSJames Feist         std::string input;
78461adbda3SJames Feist 
78561adbda3SJames Feist         // 8 below comes from
78661adbda3SJames Feist         // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
78761adbda3SJames Feist         //     0    1     2      3    4    5      6     7      8
78861adbda3SJames Feist         if (!dbus::utility::getNthStringFromPath(path, 8, input))
7895f2caaefSJames Feist         {
79062598e31SEd Tanous             BMCWEB_LOG_ERROR("Got invalid path {}", path);
79162598e31SEd Tanous             BMCWEB_LOG_ERROR("Illegal Type Zones");
792f818b04dSEd Tanous             messages::propertyValueFormatError(response->res, odata, "Zones");
7935f2caaefSJames Feist             return false;
7945f2caaefSJames Feist         }
795a170f275SEd Tanous         std::replace(input.begin(), input.end(), '_', ' ');
7965f2caaefSJames Feist         zones.emplace_back(std::move(input));
7975f2caaefSJames Feist     }
7985f2caaefSJames Feist     return true;
7995f2caaefSJames Feist }
8005f2caaefSJames Feist 
801711ac7a9SEd Tanous inline const dbus::utility::ManagedObjectType::value_type*
80273df0db0SJames Feist     findChassis(const dbus::utility::ManagedObjectType& managedObj,
8039e9b6049SEd Tanous                 std::string_view value, std::string& chassis)
804b6baeaa4SJames Feist {
80562598e31SEd Tanous     BMCWEB_LOG_DEBUG("Find Chassis: {}", value);
806b6baeaa4SJames Feist 
8079e9b6049SEd Tanous     std::string escaped(value);
8086ce82fabSYaswanth Reddy M     std::replace(escaped.begin(), escaped.end(), ' ', '_');
809b6baeaa4SJames Feist     escaped = "/" + escaped;
8103544d2a7SEd Tanous     auto it = std::ranges::find_if(managedObj, [&escaped](const auto& obj) {
81118f8f608SEd Tanous         if (obj.first.str.ends_with(escaped))
812b6baeaa4SJames Feist         {
81362598e31SEd Tanous             BMCWEB_LOG_DEBUG("Matched {}", obj.first.str);
814b6baeaa4SJames Feist             return true;
815b6baeaa4SJames Feist         }
816b6baeaa4SJames Feist         return false;
817b6baeaa4SJames Feist     });
818b6baeaa4SJames Feist 
819b6baeaa4SJames Feist     if (it == managedObj.end())
820b6baeaa4SJames Feist     {
82173df0db0SJames Feist         return nullptr;
822b6baeaa4SJames Feist     }
823b6baeaa4SJames Feist     // 5 comes from <chassis-name> being the 5th element
824b6baeaa4SJames Feist     // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
82573df0db0SJames Feist     if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
82673df0db0SJames Feist     {
82773df0db0SJames Feist         return &(*it);
82873df0db0SJames Feist     }
82973df0db0SJames Feist 
83073df0db0SJames Feist     return nullptr;
831b6baeaa4SJames Feist }
832b6baeaa4SJames Feist 
83323a21a1cSEd Tanous inline CreatePIDRet createPidInterface(
8348d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
8359e9b6049SEd Tanous     std::string_view name, nlohmann::json& jsonValue, const std::string& path,
83683ff9ab6SJames Feist     const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
837b9d36b47SEd Tanous     dbus::utility::DBusPropertiesMap& output, std::string& chassis,
838b9d36b47SEd Tanous     const std::string& profile)
83983ff9ab6SJames Feist {
8405f2caaefSJames Feist     // common deleter
8419e9b6049SEd Tanous     if (jsonValue == nullptr)
8425f2caaefSJames Feist     {
8435f2caaefSJames Feist         std::string iface;
8445f2caaefSJames Feist         if (type == "PidControllers" || type == "FanControllers")
8455f2caaefSJames Feist         {
8465f2caaefSJames Feist             iface = pidConfigurationIface;
8475f2caaefSJames Feist         }
8485f2caaefSJames Feist         else if (type == "FanZones")
8495f2caaefSJames Feist         {
8505f2caaefSJames Feist             iface = pidZoneConfigurationIface;
8515f2caaefSJames Feist         }
8525f2caaefSJames Feist         else if (type == "StepwiseControllers")
8535f2caaefSJames Feist         {
8545f2caaefSJames Feist             iface = stepwiseConfigurationIface;
8555f2caaefSJames Feist         }
8565f2caaefSJames Feist         else
8575f2caaefSJames Feist         {
85862598e31SEd Tanous             BMCWEB_LOG_ERROR("Illegal Type {}", type);
8595f2caaefSJames Feist             messages::propertyUnknown(response->res, type);
8605f2caaefSJames Feist             return CreatePIDRet::fail;
8615f2caaefSJames Feist         }
8626ee7f774SJames Feist 
86362598e31SEd Tanous         BMCWEB_LOG_DEBUG("del {} {}", path, iface);
8645f2caaefSJames Feist         // delete interface
8655f2caaefSJames Feist         crow::connections::systemBus->async_method_call(
8665e7e2dc5SEd Tanous             [response, path](const boost::system::error_code& ec) {
8675f2caaefSJames Feist                 if (ec)
8685f2caaefSJames Feist                 {
86962598e31SEd Tanous                     BMCWEB_LOG_ERROR("Error patching {}: {}", path, ec);
8705f2caaefSJames Feist                     messages::internalError(response->res);
871b6baeaa4SJames Feist                     return;
8725f2caaefSJames Feist                 }
873b6baeaa4SJames Feist                 messages::success(response->res);
8745f2caaefSJames Feist             },
8755f2caaefSJames Feist             "xyz.openbmc_project.EntityManager", path, iface, "Delete");
8765f2caaefSJames Feist         return CreatePIDRet::del;
8775f2caaefSJames Feist     }
8785f2caaefSJames Feist 
879711ac7a9SEd Tanous     const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
880b6baeaa4SJames Feist     if (!createNewObject)
881b6baeaa4SJames Feist     {
882b6baeaa4SJames Feist         // if we aren't creating a new object, we should be able to find it on
883b6baeaa4SJames Feist         // d-bus
8849e9b6049SEd Tanous         managedItem = findChassis(managedObj, name, chassis);
88573df0db0SJames Feist         if (managedItem == nullptr)
886b6baeaa4SJames Feist         {
88762598e31SEd Tanous             BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
888ef4c65b7SEd Tanous             messages::invalidObject(
889ef4c65b7SEd Tanous                 response->res,
890ef4c65b7SEd Tanous                 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
891b6baeaa4SJames Feist             return CreatePIDRet::fail;
892b6baeaa4SJames Feist         }
893b6baeaa4SJames Feist     }
894b6baeaa4SJames Feist 
89526f6976fSEd Tanous     if (!profile.empty() &&
89673df0db0SJames Feist         (type == "PidControllers" || type == "FanControllers" ||
89773df0db0SJames Feist          type == "StepwiseControllers"))
89873df0db0SJames Feist     {
89973df0db0SJames Feist         if (managedItem == nullptr)
90073df0db0SJames Feist         {
901b9d36b47SEd Tanous             output.emplace_back("Profiles", std::vector<std::string>{profile});
90273df0db0SJames Feist         }
90373df0db0SJames Feist         else
90473df0db0SJames Feist         {
90573df0db0SJames Feist             std::string interface;
90673df0db0SJames Feist             if (type == "StepwiseControllers")
90773df0db0SJames Feist             {
90873df0db0SJames Feist                 interface = stepwiseConfigurationIface;
90973df0db0SJames Feist             }
91073df0db0SJames Feist             else
91173df0db0SJames Feist             {
91273df0db0SJames Feist                 interface = pidConfigurationIface;
91373df0db0SJames Feist             }
914711ac7a9SEd Tanous             bool ifaceFound = false;
915711ac7a9SEd Tanous             for (const auto& iface : managedItem->second)
916711ac7a9SEd Tanous             {
917711ac7a9SEd Tanous                 if (iface.first == interface)
918711ac7a9SEd Tanous                 {
919711ac7a9SEd Tanous                     ifaceFound = true;
920711ac7a9SEd Tanous                     for (const auto& prop : iface.second)
921711ac7a9SEd Tanous                     {
922711ac7a9SEd Tanous                         if (prop.first == "Profiles")
923711ac7a9SEd Tanous                         {
924711ac7a9SEd Tanous                             const std::vector<std::string>* curProfiles =
925711ac7a9SEd Tanous                                 std::get_if<std::vector<std::string>>(
926711ac7a9SEd Tanous                                     &(prop.second));
927711ac7a9SEd Tanous                             if (curProfiles == nullptr)
928711ac7a9SEd Tanous                             {
92962598e31SEd Tanous                                 BMCWEB_LOG_ERROR(
93062598e31SEd Tanous                                     "Illegal profiles in managed object");
931711ac7a9SEd Tanous                                 messages::internalError(response->res);
932711ac7a9SEd Tanous                                 return CreatePIDRet::fail;
933711ac7a9SEd Tanous                             }
934711ac7a9SEd Tanous                             if (std::find(curProfiles->begin(),
935*bd79bce8SPatrick Williams                                           curProfiles->end(), profile) ==
936*bd79bce8SPatrick Williams                                 curProfiles->end())
937711ac7a9SEd Tanous                             {
938711ac7a9SEd Tanous                                 std::vector<std::string> newProfiles =
939711ac7a9SEd Tanous                                     *curProfiles;
940711ac7a9SEd Tanous                                 newProfiles.push_back(profile);
941b9d36b47SEd Tanous                                 output.emplace_back("Profiles", newProfiles);
942711ac7a9SEd Tanous                             }
943711ac7a9SEd Tanous                         }
944711ac7a9SEd Tanous                     }
945711ac7a9SEd Tanous                 }
946711ac7a9SEd Tanous             }
947711ac7a9SEd Tanous 
948711ac7a9SEd Tanous             if (!ifaceFound)
94973df0db0SJames Feist             {
95062598e31SEd Tanous                 BMCWEB_LOG_ERROR("Failed to find interface in managed object");
95173df0db0SJames Feist                 messages::internalError(response->res);
95273df0db0SJames Feist                 return CreatePIDRet::fail;
95373df0db0SJames Feist             }
95473df0db0SJames Feist         }
95573df0db0SJames Feist     }
95673df0db0SJames Feist 
95783ff9ab6SJames Feist     if (type == "PidControllers" || type == "FanControllers")
95883ff9ab6SJames Feist     {
95983ff9ab6SJames Feist         if (createNewObject)
96083ff9ab6SJames Feist         {
961b9d36b47SEd Tanous             output.emplace_back("Class",
962b9d36b47SEd Tanous                                 type == "PidControllers" ? "temp" : "fan");
963b9d36b47SEd Tanous             output.emplace_back("Type", "Pid");
96483ff9ab6SJames Feist         }
9655f2caaefSJames Feist 
9669e9b6049SEd Tanous         std::optional<std::vector<nlohmann::json::object_t>> zones;
9675f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
9685f2caaefSJames Feist         std::optional<std::vector<std::string>> outputs;
9695f2caaefSJames Feist         std::map<std::string, std::optional<double>> doubles;
970b943aaefSJames Feist         std::optional<std::string> setpointOffset;
9715f2caaefSJames Feist         if (!redfish::json_util::readJson(
9729e9b6049SEd Tanous                 jsonValue, response->res, "Inputs", inputs, "Outputs", outputs,
9735f2caaefSJames Feist                 "Zones", zones, "FFGainCoefficient",
9745f2caaefSJames Feist                 doubles["FFGainCoefficient"], "FFOffCoefficient",
9755f2caaefSJames Feist                 doubles["FFOffCoefficient"], "ICoefficient",
9765f2caaefSJames Feist                 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
9775f2caaefSJames Feist                 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
9785f2caaefSJames Feist                 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
9795f2caaefSJames Feist                 "PCoefficient", doubles["PCoefficient"], "SetPoint",
980b943aaefSJames Feist                 doubles["SetPoint"], "SetPointOffset", setpointOffset,
981b943aaefSJames Feist                 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
982b943aaefSJames Feist                 "PositiveHysteresis", doubles["PositiveHysteresis"],
983b943aaefSJames Feist                 "NegativeHysteresis", doubles["NegativeHysteresis"]))
98483ff9ab6SJames Feist         {
9855f2caaefSJames Feist             return CreatePIDRet::fail;
98683ff9ab6SJames Feist         }
9875f2caaefSJames Feist         if (zones)
9885f2caaefSJames Feist         {
9895f2caaefSJames Feist             std::vector<std::string> zonesStr;
9905f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStr))
9915f2caaefSJames Feist             {
99262598e31SEd Tanous                 BMCWEB_LOG_ERROR("Illegal Zones");
9935f2caaefSJames Feist                 return CreatePIDRet::fail;
9945f2caaefSJames Feist             }
995b6baeaa4SJames Feist             if (chassis.empty() &&
996e662eae8SEd Tanous                 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
997b6baeaa4SJames Feist             {
99862598e31SEd Tanous                 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
999ace85d60SEd Tanous                 messages::invalidObject(
1000ef4c65b7SEd Tanous                     response->res,
1001ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", chassis));
1002b6baeaa4SJames Feist                 return CreatePIDRet::fail;
1003b6baeaa4SJames Feist             }
1004b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStr));
10055f2caaefSJames Feist         }
1006afb9ee06SEd Tanous 
1007afb9ee06SEd Tanous         if (inputs)
10085f2caaefSJames Feist         {
1009afb9ee06SEd Tanous             for (std::string& value : *inputs)
101083ff9ab6SJames Feist             {
1011a170f275SEd Tanous                 std::replace(value.begin(), value.end(), '_', ' ');
101283ff9ab6SJames Feist             }
1013afb9ee06SEd Tanous             output.emplace_back("Inputs", *inputs);
1014afb9ee06SEd Tanous         }
1015afb9ee06SEd Tanous 
1016afb9ee06SEd Tanous         if (outputs)
10175f2caaefSJames Feist         {
1018afb9ee06SEd Tanous             for (std::string& value : *outputs)
10195f2caaefSJames Feist             {
1020afb9ee06SEd Tanous                 std::replace(value.begin(), value.end(), '_', ' ');
10215f2caaefSJames Feist             }
1022afb9ee06SEd Tanous             output.emplace_back("Outputs", *outputs);
102383ff9ab6SJames Feist         }
102483ff9ab6SJames Feist 
1025b943aaefSJames Feist         if (setpointOffset)
1026b943aaefSJames Feist         {
1027b943aaefSJames Feist             // translate between redfish and dbus names
1028b943aaefSJames Feist             if (*setpointOffset == "UpperThresholdNonCritical")
1029b943aaefSJames Feist             {
1030b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningLow");
1031b943aaefSJames Feist             }
1032b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdNonCritical")
1033b943aaefSJames Feist             {
1034b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningHigh");
1035b943aaefSJames Feist             }
1036b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdCritical")
1037b943aaefSJames Feist             {
1038b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalLow");
1039b943aaefSJames Feist             }
1040b943aaefSJames Feist             else if (*setpointOffset == "UpperThresholdCritical")
1041b943aaefSJames Feist             {
1042b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalHigh");
1043b943aaefSJames Feist             }
1044b943aaefSJames Feist             else
1045b943aaefSJames Feist             {
104662598e31SEd Tanous                 BMCWEB_LOG_ERROR("Invalid setpointoffset {}", *setpointOffset);
10479e9b6049SEd Tanous                 messages::propertyValueNotInList(response->res, name,
1048ace85d60SEd Tanous                                                  "SetPointOffset");
1049b943aaefSJames Feist                 return CreatePIDRet::fail;
1050b943aaefSJames Feist             }
1051b943aaefSJames Feist         }
1052b943aaefSJames Feist 
105383ff9ab6SJames Feist         // doubles
10545f2caaefSJames Feist         for (const auto& pairs : doubles)
105583ff9ab6SJames Feist         {
10565f2caaefSJames Feist             if (!pairs.second)
105783ff9ab6SJames Feist             {
10585f2caaefSJames Feist                 continue;
105983ff9ab6SJames Feist             }
106062598e31SEd Tanous             BMCWEB_LOG_DEBUG("{} = {}", pairs.first, *pairs.second);
1061b9d36b47SEd Tanous             output.emplace_back(pairs.first, *pairs.second);
10625f2caaefSJames Feist         }
106383ff9ab6SJames Feist     }
106483ff9ab6SJames Feist 
106583ff9ab6SJames Feist     else if (type == "FanZones")
106683ff9ab6SJames Feist     {
1067b9d36b47SEd Tanous         output.emplace_back("Type", "Pid.Zone");
106883ff9ab6SJames Feist 
10699e9b6049SEd Tanous         std::optional<std::string> chassisId;
10705f2caaefSJames Feist         std::optional<double> failSafePercent;
1071d3ec07f8SJames Feist         std::optional<double> minThermalOutput;
1072*bd79bce8SPatrick Williams         if (!redfish::json_util::readJson(
1073*bd79bce8SPatrick Williams                 jsonValue, response->res, "Chassis/@odata.id", chassisId,
1074*bd79bce8SPatrick Williams                 "FailSafePercent", failSafePercent, "MinThermalOutput",
1075*bd79bce8SPatrick Williams                 minThermalOutput))
107683ff9ab6SJames Feist         {
107783ff9ab6SJames Feist             return CreatePIDRet::fail;
107883ff9ab6SJames Feist         }
10795f2caaefSJames Feist 
10809e9b6049SEd Tanous         if (chassisId)
108183ff9ab6SJames Feist         {
1082717794d5SAppaRao Puli             // /redfish/v1/chassis/chassis_name/
10839e9b6049SEd Tanous             if (!dbus::utility::getNthStringFromPath(*chassisId, 3, chassis))
108483ff9ab6SJames Feist             {
10859e9b6049SEd Tanous                 BMCWEB_LOG_ERROR("Got invalid path {}", *chassisId);
1086ace85d60SEd Tanous                 messages::invalidObject(
1087ef4c65b7SEd Tanous                     response->res,
10889e9b6049SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", *chassisId));
108983ff9ab6SJames Feist                 return CreatePIDRet::fail;
109083ff9ab6SJames Feist             }
109183ff9ab6SJames Feist         }
1092d3ec07f8SJames Feist         if (minThermalOutput)
109383ff9ab6SJames Feist         {
1094b9d36b47SEd Tanous             output.emplace_back("MinThermalOutput", *minThermalOutput);
10955f2caaefSJames Feist         }
10965f2caaefSJames Feist         if (failSafePercent)
109783ff9ab6SJames Feist         {
1098b9d36b47SEd Tanous             output.emplace_back("FailSafePercent", *failSafePercent);
10995f2caaefSJames Feist         }
11005f2caaefSJames Feist     }
11015f2caaefSJames Feist     else if (type == "StepwiseControllers")
11025f2caaefSJames Feist     {
1103b9d36b47SEd Tanous         output.emplace_back("Type", "Stepwise");
11045f2caaefSJames Feist 
11059e9b6049SEd Tanous         std::optional<std::vector<nlohmann::json::object_t>> zones;
11069e9b6049SEd Tanous         std::optional<std::vector<nlohmann::json::object_t>> steps;
11075f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
11085f2caaefSJames Feist         std::optional<double> positiveHysteresis;
11095f2caaefSJames Feist         std::optional<double> negativeHysteresis;
1110c33a90ecSJames Feist         std::optional<std::string> direction; // upper clipping curve vs lower
11115f2caaefSJames Feist         if (!redfish::json_util::readJson(
11129e9b6049SEd Tanous                 jsonValue, response->res, "Zones", zones, "Steps", steps,
1113b6baeaa4SJames Feist                 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
1114c33a90ecSJames Feist                 "NegativeHysteresis", negativeHysteresis, "Direction",
1115c33a90ecSJames Feist                 direction))
11165f2caaefSJames Feist         {
111783ff9ab6SJames Feist             return CreatePIDRet::fail;
111883ff9ab6SJames Feist         }
11195f2caaefSJames Feist 
11205f2caaefSJames Feist         if (zones)
112183ff9ab6SJames Feist         {
1122b6baeaa4SJames Feist             std::vector<std::string> zonesStrs;
1123b6baeaa4SJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStrs))
11245f2caaefSJames Feist             {
112562598e31SEd Tanous                 BMCWEB_LOG_ERROR("Illegal Zones");
112683ff9ab6SJames Feist                 return CreatePIDRet::fail;
112783ff9ab6SJames Feist             }
1128b6baeaa4SJames Feist             if (chassis.empty() &&
1129e662eae8SEd Tanous                 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
1130b6baeaa4SJames Feist             {
113162598e31SEd Tanous                 BMCWEB_LOG_ERROR("Failed to get chassis from config patch");
1132ace85d60SEd Tanous                 messages::invalidObject(
1133ef4c65b7SEd Tanous                     response->res,
1134ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", chassis));
1135b6baeaa4SJames Feist                 return CreatePIDRet::fail;
1136b6baeaa4SJames Feist             }
1137b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStrs));
11385f2caaefSJames Feist         }
11395f2caaefSJames Feist         if (steps)
11405f2caaefSJames Feist         {
11415f2caaefSJames Feist             std::vector<double> readings;
11425f2caaefSJames Feist             std::vector<double> outputs;
11435f2caaefSJames Feist             for (auto& step : *steps)
11445f2caaefSJames Feist             {
1145543f4400SEd Tanous                 double target = 0.0;
1146543f4400SEd Tanous                 double out = 0.0;
11475f2caaefSJames Feist 
11489e9b6049SEd Tanous                 if (!redfish::json_util::readJsonObject(
11499e9b6049SEd Tanous                         step, response->res, "Target", target, "Output", out))
11505f2caaefSJames Feist                 {
11515f2caaefSJames Feist                     return CreatePIDRet::fail;
11525f2caaefSJames Feist                 }
11535f2caaefSJames Feist                 readings.emplace_back(target);
115423a21a1cSEd Tanous                 outputs.emplace_back(out);
11555f2caaefSJames Feist             }
1156b9d36b47SEd Tanous             output.emplace_back("Reading", std::move(readings));
1157b9d36b47SEd Tanous             output.emplace_back("Output", std::move(outputs));
11585f2caaefSJames Feist         }
11595f2caaefSJames Feist         if (inputs)
11605f2caaefSJames Feist         {
11615f2caaefSJames Feist             for (std::string& value : *inputs)
11625f2caaefSJames Feist             {
1163a170f275SEd Tanous                 std::replace(value.begin(), value.end(), '_', ' ');
11645f2caaefSJames Feist             }
1165b9d36b47SEd Tanous             output.emplace_back("Inputs", std::move(*inputs));
11665f2caaefSJames Feist         }
11675f2caaefSJames Feist         if (negativeHysteresis)
11685f2caaefSJames Feist         {
1169b9d36b47SEd Tanous             output.emplace_back("NegativeHysteresis", *negativeHysteresis);
11705f2caaefSJames Feist         }
11715f2caaefSJames Feist         if (positiveHysteresis)
11725f2caaefSJames Feist         {
1173b9d36b47SEd Tanous             output.emplace_back("PositiveHysteresis", *positiveHysteresis);
117483ff9ab6SJames Feist         }
1175c33a90ecSJames Feist         if (direction)
1176c33a90ecSJames Feist         {
1177c33a90ecSJames Feist             constexpr const std::array<const char*, 2> allowedDirections = {
1178c33a90ecSJames Feist                 "Ceiling", "Floor"};
11793544d2a7SEd Tanous             if (std::ranges::find(allowedDirections, *direction) ==
11803544d2a7SEd Tanous                 allowedDirections.end())
1181c33a90ecSJames Feist             {
1182c33a90ecSJames Feist                 messages::propertyValueTypeError(response->res, "Direction",
1183c33a90ecSJames Feist                                                  *direction);
1184c33a90ecSJames Feist                 return CreatePIDRet::fail;
1185c33a90ecSJames Feist             }
1186b9d36b47SEd Tanous             output.emplace_back("Class", *direction);
1187c33a90ecSJames Feist         }
118883ff9ab6SJames Feist     }
118983ff9ab6SJames Feist     else
119083ff9ab6SJames Feist     {
119162598e31SEd Tanous         BMCWEB_LOG_ERROR("Illegal Type {}", type);
119235a62c7cSJason M. Bills         messages::propertyUnknown(response->res, type);
119383ff9ab6SJames Feist         return CreatePIDRet::fail;
119483ff9ab6SJames Feist     }
119583ff9ab6SJames Feist     return CreatePIDRet::patch;
119683ff9ab6SJames Feist }
119773df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
119873df0db0SJames Feist {
11996936afe4SEd Tanous     struct CompletionValues
12006936afe4SEd Tanous     {
12016936afe4SEd Tanous         std::vector<std::string> supportedProfiles;
12026936afe4SEd Tanous         std::string currentProfile;
12036936afe4SEd Tanous         dbus::utility::MapperGetSubTreeResponse subtree;
12046936afe4SEd Tanous     };
120583ff9ab6SJames Feist 
12064e23a444SEd Tanous     explicit GetPIDValues(
12074e23a444SEd Tanous         const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
120823a21a1cSEd Tanous         asyncResp(asyncRespIn)
120973df0db0SJames Feist 
12101214b7e7SGunnar Mills     {}
12119c310685SBorawski.Lukasz 
121273df0db0SJames Feist     void run()
12135b4aa86bSJames Feist     {
121473df0db0SJames Feist         std::shared_ptr<GetPIDValues> self = shared_from_this();
121573df0db0SJames Feist 
121673df0db0SJames Feist         // get all configurations
1217e99073f5SGeorge Liu         constexpr std::array<std::string_view, 4> interfaces = {
1218e99073f5SGeorge Liu             pidConfigurationIface, pidZoneConfigurationIface,
1219e99073f5SGeorge Liu             objectManagerIface, stepwiseConfigurationIface};
1220e99073f5SGeorge Liu         dbus::utility::getSubTree(
1221e99073f5SGeorge Liu             "/", 0, interfaces,
1222b9d36b47SEd Tanous             [self](
1223e99073f5SGeorge Liu                 const boost::system::error_code& ec,
1224b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
12255b4aa86bSJames Feist                 if (ec)
12265b4aa86bSJames Feist                 {
122762598e31SEd Tanous                     BMCWEB_LOG_ERROR("{}", ec);
122873df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
122973df0db0SJames Feist                     return;
123073df0db0SJames Feist                 }
12316936afe4SEd Tanous                 self->complete.subtree = subtreeLocal;
1232e99073f5SGeorge Liu             });
123373df0db0SJames Feist 
123473df0db0SJames Feist         // at the same time get the selected profile
1235e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1236e99073f5SGeorge Liu             thermalModeIface};
1237e99073f5SGeorge Liu         dbus::utility::getSubTree(
1238e99073f5SGeorge Liu             "/", 0, thermalModeIfaces,
1239b9d36b47SEd Tanous             [self](
1240e99073f5SGeorge Liu                 const boost::system::error_code& ec,
1241b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
124223a21a1cSEd Tanous                 if (ec || subtreeLocal.empty())
124373df0db0SJames Feist                 {
124473df0db0SJames Feist                     return;
124573df0db0SJames Feist                 }
124623a21a1cSEd Tanous                 if (subtreeLocal[0].second.size() != 1)
124773df0db0SJames Feist                 {
124873df0db0SJames Feist                     // invalid mapper response, should never happen
124962598e31SEd Tanous                     BMCWEB_LOG_ERROR("GetPIDValues: Mapper Error");
125073df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
12515b4aa86bSJames Feist                     return;
12525b4aa86bSJames Feist                 }
12535b4aa86bSJames Feist 
125423a21a1cSEd Tanous                 const std::string& path = subtreeLocal[0].first;
125523a21a1cSEd Tanous                 const std::string& owner = subtreeLocal[0].second[0].first;
1256fac6e53bSKrzysztof Grobelny 
1257fac6e53bSKrzysztof Grobelny                 sdbusplus::asio::getAllProperties(
1258*bd79bce8SPatrick Williams                     *crow::connections::systemBus, owner, path,
1259*bd79bce8SPatrick Williams                     thermalModeIface,
1260168e20c1SEd Tanous                     [path, owner,
12615e7e2dc5SEd Tanous                      self](const boost::system::error_code& ec2,
1262b9d36b47SEd Tanous                            const dbus::utility::DBusPropertiesMap& resp) {
126323a21a1cSEd Tanous                         if (ec2)
126473df0db0SJames Feist                         {
126562598e31SEd Tanous                             BMCWEB_LOG_ERROR(
1266*bd79bce8SPatrick Williams                                 "GetPIDValues: Can't get thermalModeIface {}",
1267*bd79bce8SPatrick Williams                                 path);
126873df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
126973df0db0SJames Feist                             return;
127073df0db0SJames Feist                         }
1271fac6e53bSKrzysztof Grobelny 
1272271584abSEd Tanous                         const std::string* current = nullptr;
1273271584abSEd Tanous                         const std::vector<std::string>* supported = nullptr;
1274fac6e53bSKrzysztof Grobelny 
1275fac6e53bSKrzysztof Grobelny                         const bool success = sdbusplus::unpackPropertiesNoThrow(
1276*bd79bce8SPatrick Williams                             dbus_utils::UnpackErrorPrinter(), resp, "Current",
1277*bd79bce8SPatrick Williams                             current, "Supported", supported);
1278fac6e53bSKrzysztof Grobelny 
1279fac6e53bSKrzysztof Grobelny                         if (!success)
128073df0db0SJames Feist                         {
1281002d39b4SEd Tanous                             messages::internalError(self->asyncResp->res);
128273df0db0SJames Feist                             return;
128373df0db0SJames Feist                         }
1284fac6e53bSKrzysztof Grobelny 
128573df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
128673df0db0SJames Feist                         {
128762598e31SEd Tanous                             BMCWEB_LOG_ERROR(
1288*bd79bce8SPatrick Williams                                 "GetPIDValues: thermal mode iface invalid {}",
1289*bd79bce8SPatrick Williams                                 path);
129073df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
129173df0db0SJames Feist                             return;
129273df0db0SJames Feist                         }
12936936afe4SEd Tanous                         self->complete.currentProfile = *current;
12946936afe4SEd Tanous                         self->complete.supportedProfiles = *supported;
1295fac6e53bSKrzysztof Grobelny                     });
1296e99073f5SGeorge Liu             });
129773df0db0SJames Feist     }
129873df0db0SJames Feist 
12996936afe4SEd Tanous     static void
13006936afe4SEd Tanous         processingComplete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
13016936afe4SEd Tanous                            const CompletionValues& completion)
130273df0db0SJames Feist     {
130373df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
130473df0db0SJames Feist         {
130573df0db0SJames Feist             return;
130673df0db0SJames Feist         }
13075b4aa86bSJames Feist         // create map of <connection, path to objMgr>>
13086936afe4SEd Tanous         boost::container::flat_map<
13096936afe4SEd Tanous             std::string, std::string, std::less<>,
13106936afe4SEd Tanous             std::vector<std::pair<std::string, std::string>>>
13116936afe4SEd Tanous             objectMgrPaths;
13126936afe4SEd Tanous         boost::container::flat_set<std::string, std::less<>,
13136936afe4SEd Tanous                                    std::vector<std::string>>
13146936afe4SEd Tanous             calledConnections;
13156936afe4SEd Tanous         for (const auto& pathGroup : completion.subtree)
13165b4aa86bSJames Feist         {
13175b4aa86bSJames Feist             for (const auto& connectionGroup : pathGroup.second)
13185b4aa86bSJames Feist             {
13196bce33bcSJames Feist                 auto findConnection =
13206bce33bcSJames Feist                     calledConnections.find(connectionGroup.first);
13216bce33bcSJames Feist                 if (findConnection != calledConnections.end())
13226bce33bcSJames Feist                 {
13236bce33bcSJames Feist                     break;
13246bce33bcSJames Feist                 }
132573df0db0SJames Feist                 for (const std::string& interface : connectionGroup.second)
13265b4aa86bSJames Feist                 {
13275b4aa86bSJames Feist                     if (interface == objectManagerIface)
13285b4aa86bSJames Feist                     {
132973df0db0SJames Feist                         objectMgrPaths[connectionGroup.first] = pathGroup.first;
13305b4aa86bSJames Feist                     }
13315b4aa86bSJames Feist                     // this list is alphabetical, so we
13325b4aa86bSJames Feist                     // should have found the objMgr by now
13335b4aa86bSJames Feist                     if (interface == pidConfigurationIface ||
1334b7a08d04SJames Feist                         interface == pidZoneConfigurationIface ||
1335b7a08d04SJames Feist                         interface == stepwiseConfigurationIface)
13365b4aa86bSJames Feist                     {
13375b4aa86bSJames Feist                         auto findObjMgr =
13385b4aa86bSJames Feist                             objectMgrPaths.find(connectionGroup.first);
13395b4aa86bSJames Feist                         if (findObjMgr == objectMgrPaths.end())
13405b4aa86bSJames Feist                         {
134162598e31SEd Tanous                             BMCWEB_LOG_DEBUG("{}Has no Object Manager",
134262598e31SEd Tanous                                              connectionGroup.first);
13435b4aa86bSJames Feist                             continue;
13445b4aa86bSJames Feist                         }
13456bce33bcSJames Feist 
13466bce33bcSJames Feist                         calledConnections.insert(connectionGroup.first);
13476bce33bcSJames Feist 
134873df0db0SJames Feist                         asyncPopulatePid(findObjMgr->first, findObjMgr->second,
13496936afe4SEd Tanous                                          completion.currentProfile,
13506936afe4SEd Tanous                                          completion.supportedProfiles,
135173df0db0SJames Feist                                          asyncResp);
13525b4aa86bSJames Feist                         break;
13535b4aa86bSJames Feist                     }
13545b4aa86bSJames Feist                 }
13555b4aa86bSJames Feist             }
13565b4aa86bSJames Feist         }
135773df0db0SJames Feist     }
135873df0db0SJames Feist 
13596936afe4SEd Tanous     ~GetPIDValues()
13606936afe4SEd Tanous     {
13616936afe4SEd Tanous         boost::asio::post(crow::connections::systemBus->get_io_context(),
13626936afe4SEd Tanous                           std::bind_front(&processingComplete, asyncResp,
13636936afe4SEd Tanous                                           std::move(complete)));
13646936afe4SEd Tanous     }
13656936afe4SEd Tanous 
1366ecd6a3a2SEd Tanous     GetPIDValues(const GetPIDValues&) = delete;
1367ecd6a3a2SEd Tanous     GetPIDValues(GetPIDValues&&) = delete;
1368ecd6a3a2SEd Tanous     GetPIDValues& operator=(const GetPIDValues&) = delete;
1369ecd6a3a2SEd Tanous     GetPIDValues& operator=(GetPIDValues&&) = delete;
1370ecd6a3a2SEd Tanous 
13718d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
13726936afe4SEd Tanous     CompletionValues complete;
137373df0db0SJames Feist };
137473df0db0SJames Feist 
137573df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
137673df0db0SJames Feist {
13779e9b6049SEd Tanous     SetPIDValues(
13789e9b6049SEd Tanous         const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
13799e9b6049SEd Tanous         std::vector<
13809e9b6049SEd Tanous             std::pair<std::string, std::optional<nlohmann::json::object_t>>>&&
13819e9b6049SEd Tanous             configurationsIn,
13829e9b6049SEd Tanous         std::optional<std::string>& profileIn) :
1383*bd79bce8SPatrick Williams         asyncResp(asyncRespIn), configuration(std::move(configurationsIn)),
13849e9b6049SEd Tanous         profile(std::move(profileIn))
13859e9b6049SEd Tanous     {}
1386ecd6a3a2SEd Tanous 
1387ecd6a3a2SEd Tanous     SetPIDValues(const SetPIDValues&) = delete;
1388ecd6a3a2SEd Tanous     SetPIDValues(SetPIDValues&&) = delete;
1389ecd6a3a2SEd Tanous     SetPIDValues& operator=(const SetPIDValues&) = delete;
1390ecd6a3a2SEd Tanous     SetPIDValues& operator=(SetPIDValues&&) = delete;
1391ecd6a3a2SEd Tanous 
139273df0db0SJames Feist     void run()
139373df0db0SJames Feist     {
139473df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
139573df0db0SJames Feist         {
139673df0db0SJames Feist             return;
139773df0db0SJames Feist         }
139873df0db0SJames Feist 
139973df0db0SJames Feist         std::shared_ptr<SetPIDValues> self = shared_from_this();
140073df0db0SJames Feist 
140173df0db0SJames Feist         // todo(james): might make sense to do a mapper call here if this
140273df0db0SJames Feist         // interface gets more traction
14035eb468daSGeorge Liu         sdbusplus::message::object_path objPath(
14045eb468daSGeorge Liu             "/xyz/openbmc_project/inventory");
14055eb468daSGeorge Liu         dbus::utility::getManagedObjects(
14065eb468daSGeorge Liu             "xyz.openbmc_project.EntityManager", objPath,
14075e7e2dc5SEd Tanous             [self](const boost::system::error_code& ec,
1408914e2d5dSEd Tanous                    const dbus::utility::ManagedObjectType& mObj) {
140973df0db0SJames Feist                 if (ec)
141073df0db0SJames Feist                 {
141162598e31SEd Tanous                     BMCWEB_LOG_ERROR("Error communicating to Entity Manager");
141273df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
141373df0db0SJames Feist                     return;
141473df0db0SJames Feist                 }
1415e69d9de2SJames Feist                 const std::array<const char*, 3> configurations = {
1416e69d9de2SJames Feist                     pidConfigurationIface, pidZoneConfigurationIface,
1417e69d9de2SJames Feist                     stepwiseConfigurationIface};
1418e69d9de2SJames Feist 
141914b0b8d5SJames Feist                 for (const auto& [path, object] : mObj)
1420e69d9de2SJames Feist                 {
142114b0b8d5SJames Feist                     for (const auto& [interface, _] : object)
1422e69d9de2SJames Feist                     {
14233544d2a7SEd Tanous                         if (std::ranges::find(configurations, interface) !=
14243544d2a7SEd Tanous                             configurations.end())
1425e69d9de2SJames Feist                         {
142614b0b8d5SJames Feist                             self->objectCount++;
1427e69d9de2SJames Feist                             break;
1428e69d9de2SJames Feist                         }
1429e69d9de2SJames Feist                     }
1430e69d9de2SJames Feist                 }
1431914e2d5dSEd Tanous                 self->managedObj = mObj;
14325eb468daSGeorge Liu             });
143373df0db0SJames Feist 
143473df0db0SJames Feist         // at the same time get the profile information
1435e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1436e99073f5SGeorge Liu             thermalModeIface};
1437e99073f5SGeorge Liu         dbus::utility::getSubTree(
1438e99073f5SGeorge Liu             "/", 0, thermalModeIfaces,
1439e99073f5SGeorge Liu             [self](const boost::system::error_code& ec,
1440b9d36b47SEd Tanous                    const dbus::utility::MapperGetSubTreeResponse& subtree) {
144173df0db0SJames Feist                 if (ec || subtree.empty())
144273df0db0SJames Feist                 {
144373df0db0SJames Feist                     return;
144473df0db0SJames Feist                 }
144573df0db0SJames Feist                 if (subtree[0].second.empty())
144673df0db0SJames Feist                 {
144773df0db0SJames Feist                     // invalid mapper response, should never happen
144862598e31SEd Tanous                     BMCWEB_LOG_ERROR("SetPIDValues: Mapper Error");
144973df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
145073df0db0SJames Feist                     return;
145173df0db0SJames Feist                 }
145273df0db0SJames Feist 
145373df0db0SJames Feist                 const std::string& path = subtree[0].first;
145473df0db0SJames Feist                 const std::string& owner = subtree[0].second[0].first;
1455fac6e53bSKrzysztof Grobelny                 sdbusplus::asio::getAllProperties(
1456*bd79bce8SPatrick Williams                     *crow::connections::systemBus, owner, path,
1457*bd79bce8SPatrick Williams                     thermalModeIface,
1458*bd79bce8SPatrick Williams                     [self, path,
1459*bd79bce8SPatrick Williams                      owner](const boost::system::error_code& ec2,
1460b9d36b47SEd Tanous                             const dbus::utility::DBusPropertiesMap& r) {
1461cb13a392SEd Tanous                         if (ec2)
146273df0db0SJames Feist                         {
146362598e31SEd Tanous                             BMCWEB_LOG_ERROR(
1464*bd79bce8SPatrick Williams                                 "SetPIDValues: Can't get thermalModeIface {}",
1465*bd79bce8SPatrick Williams                                 path);
146673df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
146773df0db0SJames Feist                             return;
146873df0db0SJames Feist                         }
1469271584abSEd Tanous                         const std::string* current = nullptr;
1470271584abSEd Tanous                         const std::vector<std::string>* supported = nullptr;
1471fac6e53bSKrzysztof Grobelny 
1472fac6e53bSKrzysztof Grobelny                         const bool success = sdbusplus::unpackPropertiesNoThrow(
1473*bd79bce8SPatrick Williams                             dbus_utils::UnpackErrorPrinter(), r, "Current",
1474*bd79bce8SPatrick Williams                             current, "Supported", supported);
1475fac6e53bSKrzysztof Grobelny 
1476fac6e53bSKrzysztof Grobelny                         if (!success)
147773df0db0SJames Feist                         {
1478002d39b4SEd Tanous                             messages::internalError(self->asyncResp->res);
147973df0db0SJames Feist                             return;
148073df0db0SJames Feist                         }
1481fac6e53bSKrzysztof Grobelny 
148273df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
148373df0db0SJames Feist                         {
148462598e31SEd Tanous                             BMCWEB_LOG_ERROR(
1485*bd79bce8SPatrick Williams                                 "SetPIDValues: thermal mode iface invalid {}",
1486*bd79bce8SPatrick Williams                                 path);
148773df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
148873df0db0SJames Feist                             return;
148973df0db0SJames Feist                         }
149073df0db0SJames Feist                         self->currentProfile = *current;
149173df0db0SJames Feist                         self->supportedProfiles = *supported;
149273df0db0SJames Feist                         self->profileConnection = owner;
149373df0db0SJames Feist                         self->profilePath = path;
1494fac6e53bSKrzysztof Grobelny                     });
1495e99073f5SGeorge Liu             });
149673df0db0SJames Feist     }
149724b2fe81SEd Tanous     void pidSetDone()
149873df0db0SJames Feist     {
149973df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
150073df0db0SJames Feist         {
150173df0db0SJames Feist             return;
15025b4aa86bSJames Feist         }
15038d1b46d7Szhanghch05         std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
150473df0db0SJames Feist         if (profile)
150573df0db0SJames Feist         {
15063544d2a7SEd Tanous             if (std::ranges::find(supportedProfiles, *profile) ==
15073544d2a7SEd Tanous                 supportedProfiles.end())
150873df0db0SJames Feist             {
150973df0db0SJames Feist                 messages::actionParameterUnknown(response->res, "Profile",
151073df0db0SJames Feist                                                  *profile);
151173df0db0SJames Feist                 return;
151273df0db0SJames Feist             }
151373df0db0SJames Feist             currentProfile = *profile;
15149ae226faSGeorge Liu             sdbusplus::asio::setProperty(
15159ae226faSGeorge Liu                 *crow::connections::systemBus, profileConnection, profilePath,
15169ae226faSGeorge Liu                 thermalModeIface, "Current", *profile,
15175e7e2dc5SEd Tanous                 [response](const boost::system::error_code& ec) {
151873df0db0SJames Feist                     if (ec)
151973df0db0SJames Feist                     {
152062598e31SEd Tanous                         BMCWEB_LOG_ERROR("Error patching profile{}", ec);
152173df0db0SJames Feist                         messages::internalError(response->res);
152273df0db0SJames Feist                     }
15239ae226faSGeorge Liu                 });
152473df0db0SJames Feist         }
152573df0db0SJames Feist 
152673df0db0SJames Feist         for (auto& containerPair : configuration)
152773df0db0SJames Feist         {
152873df0db0SJames Feist             auto& container = containerPair.second;
152973df0db0SJames Feist             if (!container)
153073df0db0SJames Feist             {
153173df0db0SJames Feist                 continue;
153273df0db0SJames Feist             }
15336ee7f774SJames Feist 
153402cad96eSEd Tanous             const std::string& type = containerPair.first;
153573df0db0SJames Feist 
15369e9b6049SEd Tanous             for (auto& [name, value] : *container)
153773df0db0SJames Feist             {
1538cddbf3dfSPotin Lai                 std::string dbusObjName = name;
1539cddbf3dfSPotin Lai                 std::replace(dbusObjName.begin(), dbusObjName.end(), ' ', '_');
154062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("looking for {}", name);
15416ee7f774SJames Feist 
15423544d2a7SEd Tanous                 auto pathItr = std::ranges::find_if(
15433544d2a7SEd Tanous                     managedObj, [&dbusObjName](const auto& obj) {
154491f75cafSEd Tanous                         return obj.first.filename() == dbusObjName;
154573df0db0SJames Feist                     });
1546b9d36b47SEd Tanous                 dbus::utility::DBusPropertiesMap output;
154773df0db0SJames Feist 
154873df0db0SJames Feist                 output.reserve(16); // The pid interface length
154973df0db0SJames Feist 
155073df0db0SJames Feist                 // determines if we're patching entity-manager or
155173df0db0SJames Feist                 // creating a new object
155273df0db0SJames Feist                 bool createNewObject = (pathItr == managedObj.end());
155362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Found = {}", !createNewObject);
15546ee7f774SJames Feist 
155573df0db0SJames Feist                 std::string iface;
1556ea2b670dSEd Tanous                 if (!createNewObject)
1557ea2b670dSEd Tanous                 {
15588be2b5b6SPotin Lai                     bool findInterface = false;
1559ea2b670dSEd Tanous                     for (const auto& interface : pathItr->second)
1560ea2b670dSEd Tanous                     {
1561ea2b670dSEd Tanous                         if (interface.first == pidConfigurationIface)
1562ea2b670dSEd Tanous                         {
1563ea2b670dSEd Tanous                             if (type == "PidControllers" ||
1564ea2b670dSEd Tanous                                 type == "FanControllers")
156573df0db0SJames Feist                             {
156673df0db0SJames Feist                                 iface = pidConfigurationIface;
15678be2b5b6SPotin Lai                                 findInterface = true;
15688be2b5b6SPotin Lai                                 break;
156973df0db0SJames Feist                             }
157073df0db0SJames Feist                         }
1571ea2b670dSEd Tanous                         else if (interface.first == pidZoneConfigurationIface)
157273df0db0SJames Feist                         {
1573ea2b670dSEd Tanous                             if (type == "FanZones")
157473df0db0SJames Feist                             {
1575da39350aSPavanKumarIntel                                 iface = pidZoneConfigurationIface;
15768be2b5b6SPotin Lai                                 findInterface = true;
15778be2b5b6SPotin Lai                                 break;
157873df0db0SJames Feist                             }
157973df0db0SJames Feist                         }
1580ea2b670dSEd Tanous                         else if (interface.first == stepwiseConfigurationIface)
1581ea2b670dSEd Tanous                         {
1582ea2b670dSEd Tanous                             if (type == "StepwiseControllers")
158373df0db0SJames Feist                             {
158473df0db0SJames Feist                                 iface = stepwiseConfigurationIface;
15858be2b5b6SPotin Lai                                 findInterface = true;
15868be2b5b6SPotin Lai                                 break;
15878be2b5b6SPotin Lai                             }
15888be2b5b6SPotin Lai                         }
15898be2b5b6SPotin Lai                     }
15908be2b5b6SPotin Lai 
15918be2b5b6SPotin Lai                     // create new object if interface not found
15928be2b5b6SPotin Lai                     if (!findInterface)
15938be2b5b6SPotin Lai                     {
159473df0db0SJames Feist                         createNewObject = true;
159573df0db0SJames Feist                     }
1596ea2b670dSEd Tanous                 }
15976ee7f774SJames Feist 
15989e9b6049SEd Tanous                 if (createNewObject && value == nullptr)
15996ee7f774SJames Feist                 {
16004e0453b1SGunnar Mills                     // can't delete a non-existent object
16019e9b6049SEd Tanous                     messages::propertyValueNotInList(response->res, value,
1602e2616cc5SEd Tanous                                                      name);
16036ee7f774SJames Feist                     continue;
16046ee7f774SJames Feist                 }
16056ee7f774SJames Feist 
16066ee7f774SJames Feist                 std::string path;
16076ee7f774SJames Feist                 if (pathItr != managedObj.end())
16086ee7f774SJames Feist                 {
16096ee7f774SJames Feist                     path = pathItr->first.str;
16106ee7f774SJames Feist                 }
16116ee7f774SJames Feist 
161262598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Create new = {}", createNewObject);
1613e69d9de2SJames Feist 
1614e69d9de2SJames Feist                 // arbitrary limit to avoid attacks
1615e69d9de2SJames Feist                 constexpr const size_t controllerLimit = 500;
161614b0b8d5SJames Feist                 if (createNewObject && objectCount >= controllerLimit)
1617e69d9de2SJames Feist                 {
1618e69d9de2SJames Feist                     messages::resourceExhaustion(response->res, type);
1619e69d9de2SJames Feist                     continue;
1620e69d9de2SJames Feist                 }
1621a170f275SEd Tanous                 std::string escaped = name;
1622a170f275SEd Tanous                 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1623a170f275SEd Tanous                 output.emplace_back("Name", escaped);
162473df0db0SJames Feist 
162573df0db0SJames Feist                 std::string chassis;
162673df0db0SJames Feist                 CreatePIDRet ret = createPidInterface(
16279e9b6049SEd Tanous                     response, type, name, value, path, managedObj,
16289e9b6049SEd Tanous                     createNewObject, output, chassis, currentProfile);
162973df0db0SJames Feist                 if (ret == CreatePIDRet::fail)
163073df0db0SJames Feist                 {
163173df0db0SJames Feist                     return;
163273df0db0SJames Feist                 }
16333174e4dfSEd Tanous                 if (ret == CreatePIDRet::del)
163473df0db0SJames Feist                 {
163573df0db0SJames Feist                     continue;
163673df0db0SJames Feist                 }
163773df0db0SJames Feist 
163873df0db0SJames Feist                 if (!createNewObject)
163973df0db0SJames Feist                 {
164073df0db0SJames Feist                     for (const auto& property : output)
164173df0db0SJames Feist                     {
16427a696974SPotin Lai                         crow::connections::systemBus->async_method_call(
164373df0db0SJames Feist                             [response,
164473df0db0SJames Feist                              propertyName{std::string(property.first)}](
16455e7e2dc5SEd Tanous                                 const boost::system::error_code& ec) {
164673df0db0SJames Feist                                 if (ec)
164773df0db0SJames Feist                                 {
164862598e31SEd Tanous                                     BMCWEB_LOG_ERROR("Error patching {}: {}",
164962598e31SEd Tanous                                                      propertyName, ec);
165073df0db0SJames Feist                                     messages::internalError(response->res);
165173df0db0SJames Feist                                     return;
165273df0db0SJames Feist                                 }
165373df0db0SJames Feist                                 messages::success(response->res);
16547a696974SPotin Lai                             },
16557a696974SPotin Lai                             "xyz.openbmc_project.EntityManager", path,
16567a696974SPotin Lai                             "org.freedesktop.DBus.Properties", "Set", iface,
16577a696974SPotin Lai                             property.first, property.second);
165873df0db0SJames Feist                     }
165973df0db0SJames Feist                 }
166073df0db0SJames Feist                 else
166173df0db0SJames Feist                 {
166273df0db0SJames Feist                     if (chassis.empty())
166373df0db0SJames Feist                     {
166462598e31SEd Tanous                         BMCWEB_LOG_ERROR("Failed to get chassis from config");
1665ace85d60SEd Tanous                         messages::internalError(response->res);
166673df0db0SJames Feist                         return;
166773df0db0SJames Feist                     }
166873df0db0SJames Feist 
166973df0db0SJames Feist                     bool foundChassis = false;
167073df0db0SJames Feist                     for (const auto& obj : managedObj)
167173df0db0SJames Feist                     {
167291f75cafSEd Tanous                         if (obj.first.filename() == chassis)
167373df0db0SJames Feist                         {
167473df0db0SJames Feist                             chassis = obj.first.str;
167573df0db0SJames Feist                             foundChassis = true;
167673df0db0SJames Feist                             break;
167773df0db0SJames Feist                         }
167873df0db0SJames Feist                     }
167973df0db0SJames Feist                     if (!foundChassis)
168073df0db0SJames Feist                     {
168162598e31SEd Tanous                         BMCWEB_LOG_ERROR("Failed to find chassis on dbus");
168273df0db0SJames Feist                         messages::resourceMissingAtURI(
1683ace85d60SEd Tanous                             response->res,
1684ef4c65b7SEd Tanous                             boost::urls::format("/redfish/v1/Chassis/{}",
1685ef4c65b7SEd Tanous                                                 chassis));
168673df0db0SJames Feist                         return;
168773df0db0SJames Feist                     }
168873df0db0SJames Feist 
168973df0db0SJames Feist                     crow::connections::systemBus->async_method_call(
16905e7e2dc5SEd Tanous                         [response](const boost::system::error_code& ec) {
169173df0db0SJames Feist                             if (ec)
169273df0db0SJames Feist                             {
1693*bd79bce8SPatrick Williams                                 BMCWEB_LOG_ERROR("Error Adding Pid Object {}",
1694*bd79bce8SPatrick Williams                                                  ec);
169573df0db0SJames Feist                                 messages::internalError(response->res);
169673df0db0SJames Feist                                 return;
169773df0db0SJames Feist                             }
169873df0db0SJames Feist                             messages::success(response->res);
169973df0db0SJames Feist                         },
170073df0db0SJames Feist                         "xyz.openbmc_project.EntityManager", chassis,
170173df0db0SJames Feist                         "xyz.openbmc_project.AddObject", "AddObject", output);
170273df0db0SJames Feist                 }
170373df0db0SJames Feist             }
170473df0db0SJames Feist         }
170573df0db0SJames Feist     }
170624b2fe81SEd Tanous 
170724b2fe81SEd Tanous     ~SetPIDValues()
170824b2fe81SEd Tanous     {
170924b2fe81SEd Tanous         try
171024b2fe81SEd Tanous         {
171124b2fe81SEd Tanous             pidSetDone();
171224b2fe81SEd Tanous         }
171324b2fe81SEd Tanous         catch (...)
171424b2fe81SEd Tanous         {
171562598e31SEd Tanous             BMCWEB_LOG_CRITICAL("pidSetDone threw exception");
171624b2fe81SEd Tanous         }
171724b2fe81SEd Tanous     }
171824b2fe81SEd Tanous 
17198d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
17209e9b6049SEd Tanous     std::vector<std::pair<std::string, std::optional<nlohmann::json::object_t>>>
172173df0db0SJames Feist         configuration;
172273df0db0SJames Feist     std::optional<std::string> profile;
172373df0db0SJames Feist     dbus::utility::ManagedObjectType managedObj;
172473df0db0SJames Feist     std::vector<std::string> supportedProfiles;
172573df0db0SJames Feist     std::string currentProfile;
172673df0db0SJames Feist     std::string profileConnection;
172773df0db0SJames Feist     std::string profilePath;
172814b0b8d5SJames Feist     size_t objectCount = 0;
172973df0db0SJames Feist };
173073df0db0SJames Feist 
1731071d8fdfSSunnySrivastava1984 /**
1732071d8fdfSSunnySrivastava1984  * @brief Retrieves BMC manager location data over DBus
1733071d8fdfSSunnySrivastava1984  *
1734ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for completing asynchronous calls
1735071d8fdfSSunnySrivastava1984  * @param[in] connectionName - service name
1736071d8fdfSSunnySrivastava1984  * @param[in] path - object path
1737071d8fdfSSunnySrivastava1984  * @return none
1738071d8fdfSSunnySrivastava1984  */
1739ac106bf6SEd Tanous inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1740071d8fdfSSunnySrivastava1984                         const std::string& connectionName,
1741071d8fdfSSunnySrivastava1984                         const std::string& path)
1742071d8fdfSSunnySrivastava1984 {
174362598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get BMC manager Location data.");
1744071d8fdfSSunnySrivastava1984 
17451e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
17461e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
17471e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
1748ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
17491e1e598dSJonathan Doman                     const std::string& property) {
1750071d8fdfSSunnySrivastava1984             if (ec)
1751071d8fdfSSunnySrivastava1984             {
175262598e31SEd Tanous                 BMCWEB_LOG_DEBUG("DBUS response error for "
175362598e31SEd Tanous                                  "Location");
1754ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
1755071d8fdfSSunnySrivastava1984                 return;
1756071d8fdfSSunnySrivastava1984             }
1757071d8fdfSSunnySrivastava1984 
1758*bd79bce8SPatrick Williams             asyncResp->res
1759*bd79bce8SPatrick Williams                 .jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
17601e1e598dSJonathan Doman                 property;
17611e1e598dSJonathan Doman         });
1762071d8fdfSSunnySrivastava1984 }
17637e860f15SJohn Edward Broadbent // avoid name collision systems.hpp
17647e860f15SJohn Edward Broadbent inline void
1765ac106bf6SEd Tanous     managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
17664bf2b033SGunnar Mills {
176762598e31SEd Tanous     BMCWEB_LOG_DEBUG("Getting Manager Last Reset Time");
17684bf2b033SGunnar Mills 
17691e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint64_t>(
17701e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
17711e1e598dSJonathan Doman         "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
17721e1e598dSJonathan Doman         "LastRebootTime",
1773ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
17741e1e598dSJonathan Doman                     const uint64_t lastResetTime) {
17754bf2b033SGunnar Mills             if (ec)
17764bf2b033SGunnar Mills             {
177762598e31SEd Tanous                 BMCWEB_LOG_DEBUG("D-BUS response error {}", ec);
17784bf2b033SGunnar Mills                 return;
17794bf2b033SGunnar Mills             }
17804bf2b033SGunnar Mills 
17814bf2b033SGunnar Mills             // LastRebootTime is epoch time, in milliseconds
17824bf2b033SGunnar Mills             // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
17831e1e598dSJonathan Doman             uint64_t lastResetTimeStamp = lastResetTime / 1000;
17844bf2b033SGunnar Mills 
17854bf2b033SGunnar Mills             // Convert to ISO 8601 standard
1786ac106bf6SEd Tanous             asyncResp->res.jsonValue["LastResetTime"] =
17872b82937eSEd Tanous                 redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
17881e1e598dSJonathan Doman         });
17894bf2b033SGunnar Mills }
17904bf2b033SGunnar Mills 
17914bfefa74SGunnar Mills /**
17924bfefa74SGunnar Mills  * @brief Set the running firmware image
17934bfefa74SGunnar Mills  *
1794ac106bf6SEd Tanous  * @param[i,o] asyncResp - Async response object
17954bfefa74SGunnar Mills  * @param[i] runningFirmwareTarget - Image to make the running image
17964bfefa74SGunnar Mills  *
17974bfefa74SGunnar Mills  * @return void
17984bfefa74SGunnar Mills  */
17997e860f15SJohn Edward Broadbent inline void
1800ac106bf6SEd Tanous     setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1801f23b7296SEd Tanous                            const std::string& runningFirmwareTarget)
18024bfefa74SGunnar Mills {
18034bfefa74SGunnar Mills     // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1804f23b7296SEd Tanous     std::string::size_type idPos = runningFirmwareTarget.rfind('/');
18054bfefa74SGunnar Mills     if (idPos == std::string::npos)
18064bfefa74SGunnar Mills     {
1807ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
18084bfefa74SGunnar Mills                                          "@odata.id");
180962598e31SEd Tanous         BMCWEB_LOG_DEBUG("Can't parse firmware ID!");
18104bfefa74SGunnar Mills         return;
18114bfefa74SGunnar Mills     }
18124bfefa74SGunnar Mills     idPos++;
18134bfefa74SGunnar Mills     if (idPos >= runningFirmwareTarget.size())
18144bfefa74SGunnar Mills     {
1815ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
18164bfefa74SGunnar Mills                                          "@odata.id");
181762598e31SEd Tanous         BMCWEB_LOG_DEBUG("Invalid firmware ID.");
18184bfefa74SGunnar Mills         return;
18194bfefa74SGunnar Mills     }
18204bfefa74SGunnar Mills     std::string firmwareId = runningFirmwareTarget.substr(idPos);
18214bfefa74SGunnar Mills 
18224bfefa74SGunnar Mills     // Make sure the image is valid before setting priority
18235eb468daSGeorge Liu     sdbusplus::message::object_path objPath("/xyz/openbmc_project/software");
18245eb468daSGeorge Liu     dbus::utility::getManagedObjects(
18255eb468daSGeorge Liu         "xyz.openbmc_project.Software.BMC.Updater", objPath,
18265eb468daSGeorge Liu         [asyncResp, firmwareId, runningFirmwareTarget](
18275eb468daSGeorge Liu             const boost::system::error_code& ec,
18285eb468daSGeorge Liu             const dbus::utility::ManagedObjectType& subtree) {
18294bfefa74SGunnar Mills             if (ec)
18304bfefa74SGunnar Mills             {
183162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("D-Bus response error getting objects.");
1832ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
18334bfefa74SGunnar Mills                 return;
18344bfefa74SGunnar Mills             }
18354bfefa74SGunnar Mills 
183626f6976fSEd Tanous             if (subtree.empty())
18374bfefa74SGunnar Mills             {
183862598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Can't find image!");
1839ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
18404bfefa74SGunnar Mills                 return;
18414bfefa74SGunnar Mills             }
18424bfefa74SGunnar Mills 
18434bfefa74SGunnar Mills             bool foundImage = false;
184402cad96eSEd Tanous             for (const auto& object : subtree)
18454bfefa74SGunnar Mills             {
18464bfefa74SGunnar Mills                 const std::string& path =
18474bfefa74SGunnar Mills                     static_cast<const std::string&>(object.first);
1848f23b7296SEd Tanous                 std::size_t idPos2 = path.rfind('/');
18494bfefa74SGunnar Mills 
18504bfefa74SGunnar Mills                 if (idPos2 == std::string::npos)
18514bfefa74SGunnar Mills                 {
18524bfefa74SGunnar Mills                     continue;
18534bfefa74SGunnar Mills                 }
18544bfefa74SGunnar Mills 
18554bfefa74SGunnar Mills                 idPos2++;
18564bfefa74SGunnar Mills                 if (idPos2 >= path.size())
18574bfefa74SGunnar Mills                 {
18584bfefa74SGunnar Mills                     continue;
18594bfefa74SGunnar Mills                 }
18604bfefa74SGunnar Mills 
18614bfefa74SGunnar Mills                 if (path.substr(idPos2) == firmwareId)
18624bfefa74SGunnar Mills                 {
18634bfefa74SGunnar Mills                     foundImage = true;
18644bfefa74SGunnar Mills                     break;
18654bfefa74SGunnar Mills                 }
18664bfefa74SGunnar Mills             }
18674bfefa74SGunnar Mills 
18684bfefa74SGunnar Mills             if (!foundImage)
18694bfefa74SGunnar Mills             {
1870ac106bf6SEd Tanous                 messages::propertyValueNotInList(
1871ac106bf6SEd Tanous                     asyncResp->res, runningFirmwareTarget, "@odata.id");
187262598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Invalid firmware ID.");
18734bfefa74SGunnar Mills                 return;
18744bfefa74SGunnar Mills             }
18754bfefa74SGunnar Mills 
187662598e31SEd Tanous             BMCWEB_LOG_DEBUG("Setting firmware version {} to priority 0.",
187762598e31SEd Tanous                              firmwareId);
18784bfefa74SGunnar Mills 
18794bfefa74SGunnar Mills             // Only support Immediate
18804bfefa74SGunnar Mills             // An addition could be a Redfish Setting like
18814bfefa74SGunnar Mills             // ActiveSoftwareImageApplyTime and support OnReset
18829ae226faSGeorge Liu             sdbusplus::asio::setProperty(
18839ae226faSGeorge Liu                 *crow::connections::systemBus,
18849ae226faSGeorge Liu                 "xyz.openbmc_project.Software.BMC.Updater",
18859ae226faSGeorge Liu                 "/xyz/openbmc_project/software/" + firmwareId,
18869ae226faSGeorge Liu                 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
18879ae226faSGeorge Liu                 static_cast<uint8_t>(0),
1888ac106bf6SEd Tanous                 [asyncResp](const boost::system::error_code& ec2) {
18898a592810SEd Tanous                     if (ec2)
18904bfefa74SGunnar Mills                     {
189162598e31SEd Tanous                         BMCWEB_LOG_DEBUG("D-Bus response error setting.");
1892ac106bf6SEd Tanous                         messages::internalError(asyncResp->res);
18934bfefa74SGunnar Mills                         return;
18944bfefa74SGunnar Mills                     }
1895ac106bf6SEd Tanous                     doBMCGracefulRestart(asyncResp);
18969ae226faSGeorge Liu                 });
18975eb468daSGeorge Liu         });
18984bfefa74SGunnar Mills }
18994bfefa74SGunnar Mills 
1900*bd79bce8SPatrick Williams inline void afterSetDateTime(
1901*bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1902*bd79bce8SPatrick Williams     const boost::system::error_code& ec, const sdbusplus::message_t& msg)
1903c51afd54SEd Tanous {
1904c51afd54SEd Tanous     if (ec)
1905c51afd54SEd Tanous     {
1906c51afd54SEd Tanous         BMCWEB_LOG_DEBUG("Failed to set elapsed time. DBUS response error {}",
1907c51afd54SEd Tanous                          ec);
1908c51afd54SEd Tanous         const sd_bus_error* dbusError = msg.get_error();
1909c51afd54SEd Tanous         if (dbusError != nullptr)
1910c51afd54SEd Tanous         {
1911c51afd54SEd Tanous             std::string_view errorName(dbusError->name);
1912c51afd54SEd Tanous             if (errorName ==
1913c51afd54SEd Tanous                 "org.freedesktop.timedate1.AutomaticTimeSyncEnabled")
1914c51afd54SEd Tanous             {
1915c51afd54SEd Tanous                 BMCWEB_LOG_DEBUG("Setting conflict");
1916c51afd54SEd Tanous                 messages::propertyValueConflict(
1917c51afd54SEd Tanous                     asyncResp->res, "DateTime",
1918c51afd54SEd Tanous                     "Managers/NetworkProtocol/NTPProcotolEnabled");
1919c51afd54SEd Tanous                 return;
1920c51afd54SEd Tanous             }
1921c51afd54SEd Tanous         }
1922c51afd54SEd Tanous         messages::internalError(asyncResp->res);
1923c51afd54SEd Tanous         return;
1924c51afd54SEd Tanous     }
1925c51afd54SEd Tanous     asyncResp->res.result(boost::beast::http::status::no_content);
1926c51afd54SEd Tanous }
1927c51afd54SEd Tanous 
1928c51afd54SEd Tanous inline void setDateTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1929c51afd54SEd Tanous                         const std::string& datetime)
1930af5d6058SSantosh Puranik {
193162598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set date time: {}", datetime);
1932af5d6058SSantosh Puranik 
1933c2e32007SEd Tanous     std::optional<redfish::time_utils::usSinceEpoch> us =
1934c2e32007SEd Tanous         redfish::time_utils::dateStringToEpoch(datetime);
1935c2e32007SEd Tanous     if (!us)
1936af5d6058SSantosh Puranik     {
1937ac106bf6SEd Tanous         messages::propertyValueFormatError(asyncResp->res, datetime,
1938ac106bf6SEd Tanous                                            "DateTime");
1939c2e32007SEd Tanous         return;
1940c2e32007SEd Tanous     }
1941c51afd54SEd Tanous     // Set the absolute datetime
1942c51afd54SEd Tanous     bool relative = false;
1943c51afd54SEd Tanous     bool interactive = false;
1944c51afd54SEd Tanous     crow::connections::systemBus->async_method_call(
1945c51afd54SEd Tanous         [asyncResp](const boost::system::error_code& ec,
1946c51afd54SEd Tanous                     const sdbusplus::message_t& msg) {
1947c51afd54SEd Tanous             afterSetDateTime(asyncResp, ec, msg);
1948c51afd54SEd Tanous         },
1949c51afd54SEd Tanous         "org.freedesktop.timedate1", "/org/freedesktop/timedate1",
1950c51afd54SEd Tanous         "org.freedesktop.timedate1", "SetTime", us->count(), relative,
1951c51afd54SEd Tanous         interactive);
195283ff9ab6SJames Feist }
19539c310685SBorawski.Lukasz 
195475815e5cSEd Tanous inline void
195575815e5cSEd Tanous     checkForQuiesced(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
195675815e5cSEd Tanous {
195775815e5cSEd Tanous     sdbusplus::asio::getProperty<std::string>(
195875815e5cSEd Tanous         *crow::connections::systemBus, "org.freedesktop.systemd1",
195975815e5cSEd Tanous         "/org/freedesktop/systemd1/unit/obmc-bmc-service-quiesce@0.target",
196075815e5cSEd Tanous         "org.freedesktop.systemd1.Unit", "ActiveState",
196175815e5cSEd Tanous         [asyncResp](const boost::system::error_code& ec,
196275815e5cSEd Tanous                     const std::string& val) {
196375815e5cSEd Tanous             if (!ec)
196475815e5cSEd Tanous             {
196575815e5cSEd Tanous                 if (val == "active")
196675815e5cSEd Tanous                 {
1967539d8c6bSEd Tanous                     asyncResp->res.jsonValue["Status"]["Health"] =
1968539d8c6bSEd Tanous                         resource::Health::Critical;
1969539d8c6bSEd Tanous                     asyncResp->res.jsonValue["Status"]["State"] =
1970539d8c6bSEd Tanous                         resource::State::Quiesced;
197175815e5cSEd Tanous                     return;
197275815e5cSEd Tanous                 }
197375815e5cSEd Tanous             }
1974539d8c6bSEd Tanous             asyncResp->res.jsonValue["Status"]["Health"] = resource::Health::OK;
1975*bd79bce8SPatrick Williams             asyncResp->res.jsonValue["Status"]["State"] =
1976*bd79bce8SPatrick Williams                 resource::State::Enabled;
197775815e5cSEd Tanous         });
197875815e5cSEd Tanous }
197975815e5cSEd Tanous 
19807e860f15SJohn Edward Broadbent inline void requestRoutesManager(App& app)
19817e860f15SJohn Edward Broadbent {
19827e860f15SJohn Edward Broadbent     std::string uuid = persistent_data::getConfig().systemUuid;
19839c310685SBorawski.Lukasz 
1984253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/")
1985ed398213SEd Tanous         .privileges(redfish::privileges::getManager)
1986*bd79bce8SPatrick Williams         .methods(
1987*bd79bce8SPatrick Williams             boost::beast::http::verb::
1988*bd79bce8SPatrick Williams                 get)([&app,
1989*bd79bce8SPatrick Williams                       uuid](const crow::Request& req,
1990253f11b8SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1991253f11b8SEd Tanous                             const std::string& managerId) {
19923ba00073SCarson Labrado             if (!redfish::setUpRedfishRoute(app, req, asyncResp))
199345ca1b86SEd Tanous             {
199445ca1b86SEd Tanous                 return;
199545ca1b86SEd Tanous             }
1996253f11b8SEd Tanous 
1997253f11b8SEd Tanous             if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
1998253f11b8SEd Tanous             {
1999*bd79bce8SPatrick Williams                 messages::resourceNotFound(asyncResp->res, "Manager",
2000*bd79bce8SPatrick Williams                                            managerId);
2001253f11b8SEd Tanous                 return;
2002253f11b8SEd Tanous             }
2003253f11b8SEd Tanous 
2004253f11b8SEd Tanous             asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2005253f11b8SEd Tanous                 "/redfish/v1/Managers/{}", BMCWEB_REDFISH_MANAGER_URI_NAME);
2006*bd79bce8SPatrick Williams             asyncResp->res.jsonValue["@odata.type"] =
2007*bd79bce8SPatrick Williams                 "#Manager.v1_14_0.Manager";
2008253f11b8SEd Tanous             asyncResp->res.jsonValue["Id"] = BMCWEB_REDFISH_MANAGER_URI_NAME;
20097e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
20107e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Description"] =
20117e860f15SJohn Edward Broadbent                 "Baseboard Management Controller";
2012539d8c6bSEd Tanous             asyncResp->res.jsonValue["PowerState"] = resource::PowerState::On;
20131476687dSEd Tanous 
2014539d8c6bSEd Tanous             asyncResp->res.jsonValue["ManagerType"] = manager::ManagerType::BMC;
20157e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
20167e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
2017*bd79bce8SPatrick Williams             asyncResp->res.jsonValue["Model"] =
2018*bd79bce8SPatrick Williams                 "OpenBmc"; // TODO(ed), get model
20197e860f15SJohn Edward Broadbent 
20201476687dSEd Tanous             asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
2021253f11b8SEd Tanous                 boost::urls::format("/redfish/v1/Managers/{}/LogServices",
2022253f11b8SEd Tanous                                     BMCWEB_REDFISH_MANAGER_URI_NAME);
20231476687dSEd Tanous             asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
2024253f11b8SEd Tanous                 boost::urls::format("/redfish/v1/Managers/{}/NetworkProtocol",
2025253f11b8SEd Tanous                                     BMCWEB_REDFISH_MANAGER_URI_NAME);
20261476687dSEd Tanous             asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
2027*bd79bce8SPatrick Williams                 boost::urls::format(
2028*bd79bce8SPatrick Williams                     "/redfish/v1/Managers/{}/EthernetInterfaces",
2029253f11b8SEd Tanous                     BMCWEB_REDFISH_MANAGER_URI_NAME);
20307e860f15SJohn Edward Broadbent 
203125b54dbaSEd Tanous             if constexpr (BMCWEB_VM_NBDPROXY)
203236c0f2a3SEd Tanous             {
20331476687dSEd Tanous                 asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
2034253f11b8SEd Tanous                     boost::urls::format("/redfish/v1/Managers/{}/VirtualMedia",
2035253f11b8SEd Tanous                                         BMCWEB_REDFISH_MANAGER_URI_NAME);
203636c0f2a3SEd Tanous             }
20377e860f15SJohn Edward Broadbent 
20387e860f15SJohn Edward Broadbent             // default oem data
20397e860f15SJohn Edward Broadbent             nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
20407e860f15SJohn Edward Broadbent             nlohmann::json& oemOpenbmc = oem["OpenBmc"];
2041*bd79bce8SPatrick Williams             oem["@odata.id"] =
2042*bd79bce8SPatrick Williams                 boost::urls::format("/redfish/v1/Managers/{}#/Oem",
2043253f11b8SEd Tanous                                     BMCWEB_REDFISH_MANAGER_URI_NAME);
2044fc1cdd14SEd Tanous             oemOpenbmc["@odata.type"] = "#OpenBMCManager.v1_0_0.Manager";
2045253f11b8SEd Tanous             oemOpenbmc["@odata.id"] =
2046253f11b8SEd Tanous                 boost::urls::format("/redfish/v1/Managers/{}#/Oem/OpenBmc",
2047253f11b8SEd Tanous                                     BMCWEB_REDFISH_MANAGER_URI_NAME);
20481476687dSEd Tanous 
20491476687dSEd Tanous             nlohmann::json::object_t certificates;
2050253f11b8SEd Tanous             certificates["@odata.id"] = boost::urls::format(
2051253f11b8SEd Tanous                 "/redfish/v1/Managers/{}/Truststore/Certificates",
2052253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME);
20531476687dSEd Tanous             oemOpenbmc["Certificates"] = std::move(certificates);
20547e860f15SJohn Edward Broadbent 
20557e860f15SJohn Edward Broadbent             // Manager.Reset (an action) can be many values, OpenBMC only
20567e860f15SJohn Edward Broadbent             // supports BMC reboot.
20577e860f15SJohn Edward Broadbent             nlohmann::json& managerReset =
20587e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
2059*bd79bce8SPatrick Williams             managerReset["target"] = boost::urls::format(
2060*bd79bce8SPatrick Williams                 "/redfish/v1/Managers/{}/Actions/Manager.Reset",
2061253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME);
20627e860f15SJohn Edward Broadbent             managerReset["@Redfish.ActionInfo"] =
2063253f11b8SEd Tanous                 boost::urls::format("/redfish/v1/Managers/{}/ResetActionInfo",
2064253f11b8SEd Tanous                                     BMCWEB_REDFISH_MANAGER_URI_NAME);
20657e860f15SJohn Edward Broadbent 
20667e860f15SJohn Edward Broadbent             // ResetToDefaults (Factory Reset) has values like
20677e860f15SJohn Edward Broadbent             // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
20687e860f15SJohn Edward Broadbent             // on OpenBMC
20697e860f15SJohn Edward Broadbent             nlohmann::json& resetToDefaults =
20707e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
2071253f11b8SEd Tanous             resetToDefaults["target"] = boost::urls::format(
2072253f11b8SEd Tanous                 "/redfish/v1/Managers/{}/Actions/Manager.ResetToDefaults",
2073253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME);
2074613dabeaSEd Tanous             resetToDefaults["ResetType@Redfish.AllowableValues"] =
2075613dabeaSEd Tanous                 nlohmann::json::array_t({"ResetAll"});
20767e860f15SJohn Edward Broadbent 
20777c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
20782b82937eSEd Tanous                 redfish::time_utils::getDateTimeOffsetNow();
20797c8c4058STejas Patil 
20807c8c4058STejas Patil             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
20817c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
20827c8c4058STejas Patil                 redfishDateTimeOffset.second;
20837e860f15SJohn Edward Broadbent 
20840e8ac5e7SGunnar Mills             // TODO (Gunnar): Remove these one day since moved to ComputerSystem
20850e8ac5e7SGunnar Mills             // Still used by OCP profiles
20860e8ac5e7SGunnar Mills             // https://github.com/opencomputeproject/OCP-Profiles/issues/23
20877e860f15SJohn Edward Broadbent             // Fill in SerialConsole info
20887e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
2089*bd79bce8SPatrick Williams             asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] =
2090*bd79bce8SPatrick Williams                 15;
2091613dabeaSEd Tanous             asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
2092613dabeaSEd Tanous                 nlohmann::json::array_t({"IPMI", "SSH"});
209325b54dbaSEd Tanous             if constexpr (BMCWEB_KVM)
209425b54dbaSEd Tanous             {
20957e860f15SJohn Edward Broadbent                 // Fill in GraphicalConsole info
209625b54dbaSEd Tanous                 asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] =
209725b54dbaSEd Tanous                     true;
209825b54dbaSEd Tanous                 asyncResp->res
209925b54dbaSEd Tanous                     .jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4;
210025b54dbaSEd Tanous                 asyncResp->res
210125b54dbaSEd Tanous                     .jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
2102613dabeaSEd Tanous                     nlohmann::json::array_t({"KVMIP"});
210325b54dbaSEd Tanous             }
210425b54dbaSEd Tanous             if constexpr (!BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
21057f3e84a1SEd Tanous             {
2106*bd79bce8SPatrick Williams                 asyncResp->res
2107*bd79bce8SPatrick Williams                     .jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
21081476687dSEd Tanous 
21091476687dSEd Tanous                 nlohmann::json::array_t managerForServers;
21101476687dSEd Tanous                 nlohmann::json::object_t manager;
2111*bd79bce8SPatrick Williams                 manager["@odata.id"] = std::format(
2112*bd79bce8SPatrick Williams                     "/redfish/v1/Systems/{}", BMCWEB_REDFISH_SYSTEM_URI_NAME);
2113ad539545SPatrick Williams                 managerForServers.emplace_back(std::move(manager));
21141476687dSEd Tanous 
21151476687dSEd Tanous                 asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
21161476687dSEd Tanous                     std::move(managerForServers);
21177f3e84a1SEd Tanous             }
21187e860f15SJohn Edward Broadbent 
2119eee0013eSWilly Tu             sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
21207e860f15SJohn Edward Broadbent                                                  "FirmwareVersion", true);
21217e860f15SJohn Edward Broadbent 
21227e860f15SJohn Edward Broadbent             managerGetLastResetTime(asyncResp);
21237e860f15SJohn Edward Broadbent 
2124a51fc2d2SSui Chen             // ManagerDiagnosticData is added for all BMCs.
2125a51fc2d2SSui Chen             nlohmann::json& managerDiagnosticData =
2126a51fc2d2SSui Chen                 asyncResp->res.jsonValue["ManagerDiagnosticData"];
2127*bd79bce8SPatrick Williams             managerDiagnosticData["@odata.id"] = boost::urls::format(
2128*bd79bce8SPatrick Williams                 "/redfish/v1/Managers/{}/ManagerDiagnosticData",
2129253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME);
2130a51fc2d2SSui Chen 
213125b54dbaSEd Tanous             if constexpr (BMCWEB_REDFISH_OEM_MANAGER_FAN_DATA)
213225b54dbaSEd Tanous             {
21337e860f15SJohn Edward Broadbent                 auto pids = std::make_shared<GetPIDValues>(asyncResp);
21347e860f15SJohn Edward Broadbent                 pids->run();
213525b54dbaSEd Tanous             }
21367e860f15SJohn Edward Broadbent 
2137*bd79bce8SPatrick Williams             getMainChassisId(asyncResp, [](const std::string& chassisId,
2138*bd79bce8SPatrick Williams                                            const std::shared_ptr<
2139*bd79bce8SPatrick Williams                                                bmcweb::AsyncResp>& aRsp) {
2140*bd79bce8SPatrick Williams                 aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] =
2141*bd79bce8SPatrick Williams                     1;
21421476687dSEd Tanous                 nlohmann::json::array_t managerForChassis;
21438a592810SEd Tanous                 nlohmann::json::object_t managerObj;
2144ef4c65b7SEd Tanous                 boost::urls::url chassiUrl =
2145ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
2146eddfc437SWilly Tu                 managerObj["@odata.id"] = chassiUrl;
2147ad539545SPatrick Williams                 managerForChassis.emplace_back(std::move(managerObj));
21481476687dSEd Tanous                 aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
21491476687dSEd Tanous                     std::move(managerForChassis);
21501476687dSEd Tanous                 aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
2151eddfc437SWilly Tu                     chassiUrl;
21527e860f15SJohn Edward Broadbent             });
21537e860f15SJohn Edward Broadbent 
21541e1e598dSJonathan Doman             sdbusplus::asio::getProperty<double>(
21551e1e598dSJonathan Doman                 *crow::connections::systemBus, "org.freedesktop.systemd1",
2156002d39b4SEd Tanous                 "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
2157002d39b4SEd Tanous                 "Progress",
215875815e5cSEd Tanous                 [asyncResp](const boost::system::error_code& ec, double val) {
21597e860f15SJohn Edward Broadbent                     if (ec)
21601abe55efSEd Tanous                     {
216162598e31SEd Tanous                         BMCWEB_LOG_ERROR("Error while getting progress");
21627e860f15SJohn Edward Broadbent                         messages::internalError(asyncResp->res);
21637e860f15SJohn Edward Broadbent                         return;
21647e860f15SJohn Edward Broadbent                     }
21651e1e598dSJonathan Doman                     if (val < 1.0)
21667e860f15SJohn Edward Broadbent                     {
2167539d8c6bSEd Tanous                         asyncResp->res.jsonValue["Status"]["Health"] =
2168539d8c6bSEd Tanous                             resource::Health::OK;
2169539d8c6bSEd Tanous                         asyncResp->res.jsonValue["Status"]["State"] =
2170539d8c6bSEd Tanous                             resource::State::Starting;
217175815e5cSEd Tanous                         return;
21727e860f15SJohn Edward Broadbent                     }
217375815e5cSEd Tanous                     checkForQuiesced(asyncResp);
21741e1e598dSJonathan Doman                 });
21759c310685SBorawski.Lukasz 
2176e99073f5SGeorge Liu             constexpr std::array<std::string_view, 1> interfaces = {
2177e99073f5SGeorge Liu                 "xyz.openbmc_project.Inventory.Item.Bmc"};
2178e99073f5SGeorge Liu             dbus::utility::getSubTree(
2179e99073f5SGeorge Liu                 "/xyz/openbmc_project/inventory", 0, interfaces,
21807e860f15SJohn Edward Broadbent                 [asyncResp](
2181e99073f5SGeorge Liu                     const boost::system::error_code& ec,
2182b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
21837e860f15SJohn Edward Broadbent                     if (ec)
21841abe55efSEd Tanous                     {
2185*bd79bce8SPatrick Williams                         BMCWEB_LOG_DEBUG(
2186*bd79bce8SPatrick Williams                             "D-Bus response error on GetSubTree {}", ec);
21877e860f15SJohn Edward Broadbent                         return;
21887e860f15SJohn Edward Broadbent                     }
218926f6976fSEd Tanous                     if (subtree.empty())
21907e860f15SJohn Edward Broadbent                     {
219162598e31SEd Tanous                         BMCWEB_LOG_DEBUG("Can't find bmc D-Bus object!");
21927e860f15SJohn Edward Broadbent                         return;
21937e860f15SJohn Edward Broadbent                     }
21947e860f15SJohn Edward Broadbent                     // Assume only 1 bmc D-Bus object
21957e860f15SJohn Edward Broadbent                     // Throw an error if there is more than 1
21967e860f15SJohn Edward Broadbent                     if (subtree.size() > 1)
21977e860f15SJohn Edward Broadbent                     {
219862598e31SEd Tanous                         BMCWEB_LOG_DEBUG("Found more than 1 bmc D-Bus object!");
21997e860f15SJohn Edward Broadbent                         messages::internalError(asyncResp->res);
22007e860f15SJohn Edward Broadbent                         return;
22017e860f15SJohn Edward Broadbent                     }
22027e860f15SJohn Edward Broadbent 
2203*bd79bce8SPatrick Williams                     if (subtree[0].first.empty() ||
2204*bd79bce8SPatrick Williams                         subtree[0].second.size() != 1)
22057e860f15SJohn Edward Broadbent                     {
220662598e31SEd Tanous                         BMCWEB_LOG_DEBUG("Error getting bmc D-Bus object!");
22077e860f15SJohn Edward Broadbent                         messages::internalError(asyncResp->res);
22087e860f15SJohn Edward Broadbent                         return;
22097e860f15SJohn Edward Broadbent                     }
22107e860f15SJohn Edward Broadbent 
22117e860f15SJohn Edward Broadbent                     const std::string& path = subtree[0].first;
2212*bd79bce8SPatrick Williams                     const std::string& connectionName =
2213*bd79bce8SPatrick Williams                         subtree[0].second[0].first;
22147e860f15SJohn Edward Broadbent 
2215*bd79bce8SPatrick Williams                     for (const auto& interfaceName :
2216*bd79bce8SPatrick Williams                          subtree[0].second[0].second)
22177e860f15SJohn Edward Broadbent                     {
22187e860f15SJohn Edward Broadbent                         if (interfaceName ==
22197e860f15SJohn Edward Broadbent                             "xyz.openbmc_project.Inventory.Decorator.Asset")
22207e860f15SJohn Edward Broadbent                         {
2221fac6e53bSKrzysztof Grobelny                             sdbusplus::asio::getAllProperties(
2222*bd79bce8SPatrick Williams                                 *crow::connections::systemBus, connectionName,
2223*bd79bce8SPatrick Williams                                 path,
2224fac6e53bSKrzysztof Grobelny                                 "xyz.openbmc_project.Inventory.Decorator.Asset",
2225*bd79bce8SPatrick Williams                                 [asyncResp](
2226*bd79bce8SPatrick Williams                                     const boost::system::error_code& ec2,
2227b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap&
22287e860f15SJohn Edward Broadbent                                         propertiesList) {
22298a592810SEd Tanous                                     if (ec2)
22307e860f15SJohn Edward Broadbent                                     {
2231*bd79bce8SPatrick Williams                                         BMCWEB_LOG_DEBUG(
2232*bd79bce8SPatrick Williams                                             "Can't get bmc asset!");
22337e860f15SJohn Edward Broadbent                                         return;
22347e860f15SJohn Edward Broadbent                                     }
22357e860f15SJohn Edward Broadbent 
2236fac6e53bSKrzysztof Grobelny                                     const std::string* partNumber = nullptr;
2237fac6e53bSKrzysztof Grobelny                                     const std::string* serialNumber = nullptr;
2238fac6e53bSKrzysztof Grobelny                                     const std::string* manufacturer = nullptr;
2239fac6e53bSKrzysztof Grobelny                                     const std::string* model = nullptr;
2240*bd79bce8SPatrick Williams                                     const std::string* sparePartNumber =
2241*bd79bce8SPatrick Williams                                         nullptr;
2242fac6e53bSKrzysztof Grobelny 
2243*bd79bce8SPatrick Williams                                     const bool success =
2244*bd79bce8SPatrick Williams                                         sdbusplus::unpackPropertiesNoThrow(
2245*bd79bce8SPatrick Williams                                             dbus_utils::UnpackErrorPrinter(),
2246*bd79bce8SPatrick Williams                                             propertiesList, "PartNumber",
2247*bd79bce8SPatrick Williams                                             partNumber, "SerialNumber",
2248*bd79bce8SPatrick Williams                                             serialNumber, "Manufacturer",
2249*bd79bce8SPatrick Williams                                             manufacturer, "Model", model,
2250*bd79bce8SPatrick Williams                                             "SparePartNumber", sparePartNumber);
2251fac6e53bSKrzysztof Grobelny 
2252fac6e53bSKrzysztof Grobelny                                     if (!success)
22537e860f15SJohn Edward Broadbent                                     {
2254002d39b4SEd Tanous                                         messages::internalError(asyncResp->res);
22557e860f15SJohn Edward Broadbent                                         return;
22567e860f15SJohn Edward Broadbent                                     }
2257fac6e53bSKrzysztof Grobelny 
2258fac6e53bSKrzysztof Grobelny                                     if (partNumber != nullptr)
2259fac6e53bSKrzysztof Grobelny                                     {
2260fac6e53bSKrzysztof Grobelny                                         asyncResp->res.jsonValue["PartNumber"] =
2261fac6e53bSKrzysztof Grobelny                                             *partNumber;
22627e860f15SJohn Edward Broadbent                                     }
2263fac6e53bSKrzysztof Grobelny 
2264fac6e53bSKrzysztof Grobelny                                     if (serialNumber != nullptr)
2265fac6e53bSKrzysztof Grobelny                                     {
2266*bd79bce8SPatrick Williams                                         asyncResp->res
2267*bd79bce8SPatrick Williams                                             .jsonValue["SerialNumber"] =
2268fac6e53bSKrzysztof Grobelny                                             *serialNumber;
22697e860f15SJohn Edward Broadbent                                     }
2270fac6e53bSKrzysztof Grobelny 
2271fac6e53bSKrzysztof Grobelny                                     if (manufacturer != nullptr)
2272fac6e53bSKrzysztof Grobelny                                     {
2273*bd79bce8SPatrick Williams                                         asyncResp->res
2274*bd79bce8SPatrick Williams                                             .jsonValue["Manufacturer"] =
2275fac6e53bSKrzysztof Grobelny                                             *manufacturer;
2276fac6e53bSKrzysztof Grobelny                                     }
2277fac6e53bSKrzysztof Grobelny 
2278fac6e53bSKrzysztof Grobelny                                     if (model != nullptr)
2279fac6e53bSKrzysztof Grobelny                                     {
2280*bd79bce8SPatrick Williams                                         asyncResp->res.jsonValue["Model"] =
2281*bd79bce8SPatrick Williams                                             *model;
2282fac6e53bSKrzysztof Grobelny                                     }
2283fac6e53bSKrzysztof Grobelny 
2284fac6e53bSKrzysztof Grobelny                                     if (sparePartNumber != nullptr)
2285fac6e53bSKrzysztof Grobelny                                     {
2286*bd79bce8SPatrick Williams                                         asyncResp->res
2287*bd79bce8SPatrick Williams                                             .jsonValue["SparePartNumber"] =
2288fac6e53bSKrzysztof Grobelny                                             *sparePartNumber;
2289fac6e53bSKrzysztof Grobelny                                     }
2290fac6e53bSKrzysztof Grobelny                                 });
22917e860f15SJohn Edward Broadbent                         }
2292*bd79bce8SPatrick Williams                         else if (
2293*bd79bce8SPatrick Williams                             interfaceName ==
22940fda0f12SGeorge Liu                             "xyz.openbmc_project.Inventory.Decorator.LocationCode")
22957e860f15SJohn Edward Broadbent                         {
22967e860f15SJohn Edward Broadbent                             getLocation(asyncResp, connectionName, path);
22977e860f15SJohn Edward Broadbent                         }
22987e860f15SJohn Edward Broadbent                     }
2299e99073f5SGeorge Liu                 });
23007e860f15SJohn Edward Broadbent         });
23017e860f15SJohn Edward Broadbent 
2302253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/")
2303ed398213SEd Tanous         .privileges(redfish::privileges::patchManager)
230445ca1b86SEd Tanous         .methods(boost::beast::http::verb::patch)(
230545ca1b86SEd Tanous             [&app](const crow::Request& req,
2306253f11b8SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2307253f11b8SEd Tanous                    const std::string& managerId) {
23083ba00073SCarson Labrado                 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
230945ca1b86SEd Tanous                 {
231045ca1b86SEd Tanous                     return;
231145ca1b86SEd Tanous                 }
2312253f11b8SEd Tanous 
2313253f11b8SEd Tanous                 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2314253f11b8SEd Tanous                 {
2315*bd79bce8SPatrick Williams                     messages::resourceNotFound(asyncResp->res, "Manager",
2316*bd79bce8SPatrick Williams                                                managerId);
2317253f11b8SEd Tanous                     return;
2318253f11b8SEd Tanous                 }
2319253f11b8SEd Tanous 
23209e9b6049SEd Tanous                 std::optional<std::string> activeSoftwareImageOdataId;
23217e860f15SJohn Edward Broadbent                 std::optional<std::string> datetime;
23229e9b6049SEd Tanous                 std::optional<nlohmann::json::object_t> pidControllers;
23239e9b6049SEd Tanous                 std::optional<nlohmann::json::object_t> fanControllers;
23249e9b6049SEd Tanous                 std::optional<nlohmann::json::object_t> fanZones;
23259e9b6049SEd Tanous                 std::optional<nlohmann::json::object_t> stepwiseControllers;
23269e9b6049SEd Tanous                 std::optional<std::string> profile;
23277e860f15SJohn Edward Broadbent 
23289e9b6049SEd Tanous                 // clang-format off
23299e9b6049SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res,
23309e9b6049SEd Tanous               "DateTime", datetime,
23319e9b6049SEd Tanous               "Links/ActiveSoftwareImage/@odata.id", activeSoftwareImageOdataId,
23329e9b6049SEd Tanous               "Oem/OpenBmc/Fan/FanControllers", fanControllers,
23339e9b6049SEd Tanous               "Oem/OpenBmc/Fan/FanZones", fanZones,
23349e9b6049SEd Tanous               "Oem/OpenBmc/Fan/PidControllers", pidControllers,
23359e9b6049SEd Tanous               "Oem/OpenBmc/Fan/Profile", profile,
23369e9b6049SEd Tanous               "Oem/OpenBmc/Fan/StepwiseControllers", stepwiseControllers
23379e9b6049SEd Tanous         ))
23387e860f15SJohn Edward Broadbent         {
23397e860f15SJohn Edward Broadbent             return;
23407e860f15SJohn Edward Broadbent         }
23419e9b6049SEd Tanous                 // clang-format on
23427e860f15SJohn Edward Broadbent 
23439e9b6049SEd Tanous                 if (pidControllers || fanControllers || fanZones ||
23449e9b6049SEd Tanous                     stepwiseControllers || profile)
23457e860f15SJohn Edward Broadbent                 {
234625b54dbaSEd Tanous                     if constexpr (BMCWEB_REDFISH_OEM_MANAGER_FAN_DATA)
234725b54dbaSEd Tanous                     {
2348*bd79bce8SPatrick Williams                         std::vector<
2349*bd79bce8SPatrick Williams                             std::pair<std::string,
235025b54dbaSEd Tanous                                       std::optional<nlohmann::json::object_t>>>
23519e9b6049SEd Tanous                             configuration;
23529e9b6049SEd Tanous                         if (pidControllers)
23537e860f15SJohn Edward Broadbent                         {
2354*bd79bce8SPatrick Williams                             configuration.emplace_back(
2355*bd79bce8SPatrick Williams                                 "PidControllers", std::move(pidControllers));
23567e860f15SJohn Edward Broadbent                         }
23579e9b6049SEd Tanous                         if (fanControllers)
23587e860f15SJohn Edward Broadbent                         {
2359*bd79bce8SPatrick Williams                             configuration.emplace_back(
2360*bd79bce8SPatrick Williams                                 "FanControllers", std::move(fanControllers));
23617e860f15SJohn Edward Broadbent                         }
23629e9b6049SEd Tanous                         if (fanZones)
23637e860f15SJohn Edward Broadbent                         {
2364*bd79bce8SPatrick Williams                             configuration.emplace_back("FanZones",
2365*bd79bce8SPatrick Williams                                                        std::move(fanZones));
23669e9b6049SEd Tanous                         }
23679e9b6049SEd Tanous                         if (stepwiseControllers)
23689e9b6049SEd Tanous                         {
2369*bd79bce8SPatrick Williams                             configuration.emplace_back(
2370*bd79bce8SPatrick Williams                                 "StepwiseControllers",
23719e9b6049SEd Tanous                                 std::move(stepwiseControllers));
23729e9b6049SEd Tanous                         }
23739e9b6049SEd Tanous                         auto pid = std::make_shared<SetPIDValues>(
23749e9b6049SEd Tanous                             asyncResp, std::move(configuration), profile);
23757e860f15SJohn Edward Broadbent                         pid->run();
237625b54dbaSEd Tanous                     }
237725b54dbaSEd Tanous                     else
237825b54dbaSEd Tanous                     {
237954dce7f5SGunnar Mills                         messages::propertyUnknown(asyncResp->res, "Oem");
238054dce7f5SGunnar Mills                         return;
238125b54dbaSEd Tanous                     }
23827e860f15SJohn Edward Broadbent                 }
23839e9b6049SEd Tanous 
23849e9b6049SEd Tanous                 if (activeSoftwareImageOdataId)
23857e860f15SJohn Edward Broadbent                 {
2386*bd79bce8SPatrick Williams                     setActiveFirmwareImage(asyncResp,
2387*bd79bce8SPatrick Williams                                            *activeSoftwareImageOdataId);
23887e860f15SJohn Edward Broadbent                 }
23897e860f15SJohn Edward Broadbent 
23907e860f15SJohn Edward Broadbent                 if (datetime)
23917e860f15SJohn Edward Broadbent                 {
2392c51afd54SEd Tanous                     setDateTime(asyncResp, *datetime);
23937e860f15SJohn Edward Broadbent                 }
23947e860f15SJohn Edward Broadbent             });
23957e860f15SJohn Edward Broadbent }
23967e860f15SJohn Edward Broadbent 
23977e860f15SJohn Edward Broadbent inline void requestRoutesManagerCollection(App& app)
23987e860f15SJohn Edward Broadbent {
23997e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
2400ed398213SEd Tanous         .privileges(redfish::privileges::getManagerCollection)
24017e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
240245ca1b86SEd Tanous             [&app](const crow::Request& req,
24037e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
24043ba00073SCarson Labrado                 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
240545ca1b86SEd Tanous                 {
240645ca1b86SEd Tanous                     return;
240745ca1b86SEd Tanous                 }
240883ff9ab6SJames Feist                 // Collections don't include the static data added by SubRoute
240983ff9ab6SJames Feist                 // because it has a duplicate entry for members
24108d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
24118d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
24128d1b46d7Szhanghch05                     "#ManagerCollection.ManagerCollection";
24138d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "Manager Collection";
24148d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Members@odata.count"] = 1;
24151476687dSEd Tanous                 nlohmann::json::array_t members;
24161476687dSEd Tanous                 nlohmann::json& bmc = members.emplace_back();
2417*bd79bce8SPatrick Williams                 bmc["@odata.id"] = boost::urls::format(
2418*bd79bce8SPatrick Williams                     "/redfish/v1/Managers/{}", BMCWEB_REDFISH_MANAGER_URI_NAME);
24191476687dSEd Tanous                 asyncResp->res.jsonValue["Members"] = std::move(members);
24207e860f15SJohn Edward Broadbent             });
24219c310685SBorawski.Lukasz }
24229c310685SBorawski.Lukasz } // namespace redfish
2423