xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision b9d36b4791d77a47e1f3c5c4564fcdf7cc68c115)
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 
18b49ac873SJames Feist #include "health.hpp"
19c5d03ff4SJennifer Lee #include "redfish_util.hpp"
209c310685SBorawski.Lukasz 
217e860f15SJohn Edward Broadbent #include <app.hpp>
225b4aa86bSJames Feist #include <boost/algorithm/string/replace.hpp>
23af5d6058SSantosh Puranik #include <boost/date_time.hpp>
245b4aa86bSJames Feist #include <dbus_utility.hpp>
25ed398213SEd Tanous #include <registries/privilege_registry.hpp>
26e90c5052SAndrew Geissler #include <utils/fw_utils.hpp>
277bffdb7eSBernard Wong #include <utils/systemd_utils.hpp>
281214b7e7SGunnar Mills 
294bfefa74SGunnar Mills #include <cstdint>
301214b7e7SGunnar Mills #include <memory>
311214b7e7SGunnar Mills #include <sstream>
32abf2add6SEd Tanous #include <variant>
335b4aa86bSJames Feist 
341abe55efSEd Tanous namespace redfish
351abe55efSEd Tanous {
36ed5befbdSJennifer Lee 
37ed5befbdSJennifer Lee /**
382a5c4407SGunnar Mills  * Function reboots the BMC.
392a5c4407SGunnar Mills  *
402a5c4407SGunnar Mills  * @param[in] asyncResp - Shared pointer for completing asynchronous calls
41ed5befbdSJennifer Lee  */
428d1b46d7Szhanghch05 inline void
438d1b46d7Szhanghch05     doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
44ed5befbdSJennifer Lee {
45ed5befbdSJennifer Lee     const char* processName = "xyz.openbmc_project.State.BMC";
46ed5befbdSJennifer Lee     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
47ed5befbdSJennifer Lee     const char* interfaceName = "xyz.openbmc_project.State.BMC";
48ed5befbdSJennifer Lee     const std::string& propertyValue =
49ed5befbdSJennifer Lee         "xyz.openbmc_project.State.BMC.Transition.Reboot";
50ed5befbdSJennifer Lee     const char* destProperty = "RequestedBMCTransition";
51ed5befbdSJennifer Lee 
52ed5befbdSJennifer Lee     // Create the D-Bus variant for D-Bus call.
53168e20c1SEd Tanous     dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
54ed5befbdSJennifer Lee 
55ed5befbdSJennifer Lee     crow::connections::systemBus->async_method_call(
56ed5befbdSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
57ed5befbdSJennifer Lee             // Use "Set" method to set the property value.
58ed5befbdSJennifer Lee             if (ec)
59ed5befbdSJennifer Lee             {
602a5c4407SGunnar Mills                 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
61ed5befbdSJennifer Lee                 messages::internalError(asyncResp->res);
62ed5befbdSJennifer Lee                 return;
63ed5befbdSJennifer Lee             }
64ed5befbdSJennifer Lee 
65ed5befbdSJennifer Lee             messages::success(asyncResp->res);
66ed5befbdSJennifer Lee         },
67ed5befbdSJennifer Lee         processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
68ed5befbdSJennifer Lee         interfaceName, destProperty, dbusPropertyValue);
69ed5befbdSJennifer Lee }
702a5c4407SGunnar Mills 
718d1b46d7Szhanghch05 inline void
728d1b46d7Szhanghch05     doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
73f92af389SJayaprakash Mutyala {
74f92af389SJayaprakash Mutyala     const char* processName = "xyz.openbmc_project.State.BMC";
75f92af389SJayaprakash Mutyala     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
76f92af389SJayaprakash Mutyala     const char* interfaceName = "xyz.openbmc_project.State.BMC";
77f92af389SJayaprakash Mutyala     const std::string& propertyValue =
78f92af389SJayaprakash Mutyala         "xyz.openbmc_project.State.BMC.Transition.HardReboot";
79f92af389SJayaprakash Mutyala     const char* destProperty = "RequestedBMCTransition";
80f92af389SJayaprakash Mutyala 
81f92af389SJayaprakash Mutyala     // Create the D-Bus variant for D-Bus call.
82168e20c1SEd Tanous     dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
83f92af389SJayaprakash Mutyala 
84f92af389SJayaprakash Mutyala     crow::connections::systemBus->async_method_call(
85f92af389SJayaprakash Mutyala         [asyncResp](const boost::system::error_code ec) {
86f92af389SJayaprakash Mutyala             // Use "Set" method to set the property value.
87f92af389SJayaprakash Mutyala             if (ec)
88f92af389SJayaprakash Mutyala             {
89f92af389SJayaprakash Mutyala                 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
90f92af389SJayaprakash Mutyala                 messages::internalError(asyncResp->res);
91f92af389SJayaprakash Mutyala                 return;
92f92af389SJayaprakash Mutyala             }
93f92af389SJayaprakash Mutyala 
94f92af389SJayaprakash Mutyala             messages::success(asyncResp->res);
95f92af389SJayaprakash Mutyala         },
96f92af389SJayaprakash Mutyala         processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
97f92af389SJayaprakash Mutyala         interfaceName, destProperty, dbusPropertyValue);
98f92af389SJayaprakash Mutyala }
99f92af389SJayaprakash Mutyala 
1002a5c4407SGunnar Mills /**
1012a5c4407SGunnar Mills  * ManagerResetAction class supports the POST method for the Reset (reboot)
1022a5c4407SGunnar Mills  * action.
1032a5c4407SGunnar Mills  */
1047e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetAction(App& app)
1052a5c4407SGunnar Mills {
1062a5c4407SGunnar Mills     /**
1072a5c4407SGunnar Mills      * Function handles POST method request.
1082a5c4407SGunnar Mills      * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
109f92af389SJayaprakash Mutyala      * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
1102a5c4407SGunnar Mills      */
1117e860f15SJohn Edward Broadbent 
1127e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
113ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
1147e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
1157e860f15SJohn Edward Broadbent             [](const crow::Request& req,
1167e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1172a5c4407SGunnar Mills                 BMCWEB_LOG_DEBUG << "Post Manager Reset.";
1182a5c4407SGunnar Mills 
1192a5c4407SGunnar Mills                 std::string resetType;
1202a5c4407SGunnar Mills 
12115ed6780SWilly Tu                 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
1227e860f15SJohn Edward Broadbent                                                resetType))
1232a5c4407SGunnar Mills                 {
1242a5c4407SGunnar Mills                     return;
1252a5c4407SGunnar Mills                 }
1262a5c4407SGunnar Mills 
127f92af389SJayaprakash Mutyala                 if (resetType == "GracefulRestart")
128f92af389SJayaprakash Mutyala                 {
129f92af389SJayaprakash Mutyala                     BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
130f92af389SJayaprakash Mutyala                     doBMCGracefulRestart(asyncResp);
131f92af389SJayaprakash Mutyala                     return;
132f92af389SJayaprakash Mutyala                 }
1333174e4dfSEd Tanous                 if (resetType == "ForceRestart")
134f92af389SJayaprakash Mutyala                 {
135f92af389SJayaprakash Mutyala                     BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
136f92af389SJayaprakash Mutyala                     doBMCForceRestart(asyncResp);
137f92af389SJayaprakash Mutyala                     return;
138f92af389SJayaprakash Mutyala                 }
1392a5c4407SGunnar Mills                 BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
1402a5c4407SGunnar Mills                                  << resetType;
1412a5c4407SGunnar Mills                 messages::actionParameterNotSupported(asyncResp->res, resetType,
1422a5c4407SGunnar Mills                                                       "ResetType");
1432a5c4407SGunnar Mills 
1442a5c4407SGunnar Mills                 return;
1457e860f15SJohn Edward Broadbent             });
1462a5c4407SGunnar Mills }
147ed5befbdSJennifer Lee 
1483e40fc74SGunnar Mills /**
1493e40fc74SGunnar Mills  * ManagerResetToDefaultsAction class supports POST method for factory reset
1503e40fc74SGunnar Mills  * action.
1513e40fc74SGunnar Mills  */
1527e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetToDefaultsAction(App& app)
1533e40fc74SGunnar Mills {
1543e40fc74SGunnar Mills 
1553e40fc74SGunnar Mills     /**
1563e40fc74SGunnar Mills      * Function handles ResetToDefaults POST method request.
1573e40fc74SGunnar Mills      *
1583e40fc74SGunnar Mills      * Analyzes POST body message and factory resets BMC by calling
1593e40fc74SGunnar Mills      * BMC code updater factory reset followed by a BMC reboot.
1603e40fc74SGunnar Mills      *
1613e40fc74SGunnar Mills      * BMC code updater factory reset wipes the whole BMC read-write
1623e40fc74SGunnar Mills      * filesystem which includes things like the network settings.
1633e40fc74SGunnar Mills      *
1643e40fc74SGunnar Mills      * OpenBMC only supports ResetToDefaultsType "ResetAll".
1653e40fc74SGunnar Mills      */
1667e860f15SJohn Edward Broadbent 
1677e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
1687e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/")
169ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
1707e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
1717e860f15SJohn Edward Broadbent             [](const crow::Request& req,
1727e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1733e40fc74SGunnar Mills                 BMCWEB_LOG_DEBUG << "Post ResetToDefaults.";
1743e40fc74SGunnar Mills 
1753e40fc74SGunnar Mills                 std::string resetType;
1763e40fc74SGunnar Mills 
17715ed6780SWilly Tu                 if (!json_util::readJsonAction(
17815ed6780SWilly Tu                         req, asyncResp->res, "ResetToDefaultsType", resetType))
1793e40fc74SGunnar Mills                 {
1803e40fc74SGunnar Mills                     BMCWEB_LOG_DEBUG << "Missing property ResetToDefaultsType.";
1813e40fc74SGunnar Mills 
1827e860f15SJohn Edward Broadbent                     messages::actionParameterMissing(asyncResp->res,
1837e860f15SJohn Edward Broadbent                                                      "ResetToDefaults",
1843e40fc74SGunnar Mills                                                      "ResetToDefaultsType");
1853e40fc74SGunnar Mills                     return;
1863e40fc74SGunnar Mills                 }
1873e40fc74SGunnar Mills 
1883e40fc74SGunnar Mills                 if (resetType != "ResetAll")
1893e40fc74SGunnar Mills                 {
1900fda0f12SGeorge Liu                     BMCWEB_LOG_DEBUG
1910fda0f12SGeorge Liu                         << "Invalid property value for ResetToDefaultsType: "
1923e40fc74SGunnar Mills                         << resetType;
1937e860f15SJohn Edward Broadbent                     messages::actionParameterNotSupported(
1947e860f15SJohn Edward Broadbent                         asyncResp->res, resetType, "ResetToDefaultsType");
1953e40fc74SGunnar Mills                     return;
1963e40fc74SGunnar Mills                 }
1973e40fc74SGunnar Mills 
1983e40fc74SGunnar Mills                 crow::connections::systemBus->async_method_call(
1993e40fc74SGunnar Mills                     [asyncResp](const boost::system::error_code ec) {
2003e40fc74SGunnar Mills                         if (ec)
2013e40fc74SGunnar Mills                         {
2027e860f15SJohn Edward Broadbent                             BMCWEB_LOG_DEBUG << "Failed to ResetToDefaults: "
2037e860f15SJohn Edward Broadbent                                              << ec;
2043e40fc74SGunnar Mills                             messages::internalError(asyncResp->res);
2053e40fc74SGunnar Mills                             return;
2063e40fc74SGunnar Mills                         }
2073e40fc74SGunnar Mills                         // Factory Reset doesn't actually happen until a reboot
2083e40fc74SGunnar Mills                         // Can't erase what the BMC is running on
2093e40fc74SGunnar Mills                         doBMCGracefulRestart(asyncResp);
2103e40fc74SGunnar Mills                     },
2113e40fc74SGunnar Mills                     "xyz.openbmc_project.Software.BMC.Updater",
2123e40fc74SGunnar Mills                     "/xyz/openbmc_project/software",
2133e40fc74SGunnar Mills                     "xyz.openbmc_project.Common.FactoryReset", "Reset");
2147e860f15SJohn Edward Broadbent             });
2153e40fc74SGunnar Mills }
2163e40fc74SGunnar Mills 
2171cb1a9e6SAppaRao Puli /**
2181cb1a9e6SAppaRao Puli  * ManagerResetActionInfo derived class for delivering Manager
2191cb1a9e6SAppaRao Puli  * ResetType AllowableValues using ResetInfo schema.
2201cb1a9e6SAppaRao Puli  */
2217e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetActionInfo(App& app)
2221cb1a9e6SAppaRao Puli {
2231cb1a9e6SAppaRao Puli     /**
2241cb1a9e6SAppaRao Puli      * Functions triggers appropriate requests on DBus
2251cb1a9e6SAppaRao Puli      */
2267e860f15SJohn Edward Broadbent 
2277e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/ResetActionInfo/")
228ed398213SEd Tanous         .privileges(redfish::privileges::getActionInfo)
2297e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2307e860f15SJohn Edward Broadbent             [](const crow::Request&,
2317e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2328d1b46d7Szhanghch05                 asyncResp->res.jsonValue = {
2331cb1a9e6SAppaRao Puli                     {"@odata.type", "#ActionInfo.v1_1_2.ActionInfo"},
2341cb1a9e6SAppaRao Puli                     {"@odata.id", "/redfish/v1/Managers/bmc/ResetActionInfo"},
2351cb1a9e6SAppaRao Puli                     {"Name", "Reset Action Info"},
2361cb1a9e6SAppaRao Puli                     {"Id", "ResetActionInfo"},
2371cb1a9e6SAppaRao Puli                     {"Parameters",
2381cb1a9e6SAppaRao Puli                      {{{"Name", "ResetType"},
2391cb1a9e6SAppaRao Puli                        {"Required", true},
2401cb1a9e6SAppaRao Puli                        {"DataType", "String"},
2417e860f15SJohn Edward Broadbent                        {"AllowableValues",
2427e860f15SJohn Edward Broadbent                         {"GracefulRestart", "ForceRestart"}}}}}};
2437e860f15SJohn Edward Broadbent             });
2441cb1a9e6SAppaRao Puli }
2451cb1a9e6SAppaRao Puli 
2465b4aa86bSJames Feist static constexpr const char* objectManagerIface =
2475b4aa86bSJames Feist     "org.freedesktop.DBus.ObjectManager";
2485b4aa86bSJames Feist static constexpr const char* pidConfigurationIface =
2495b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid";
2505b4aa86bSJames Feist static constexpr const char* pidZoneConfigurationIface =
2515b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid.Zone";
252b7a08d04SJames Feist static constexpr const char* stepwiseConfigurationIface =
253b7a08d04SJames Feist     "xyz.openbmc_project.Configuration.Stepwise";
25473df0db0SJames Feist static constexpr const char* thermalModeIface =
25573df0db0SJames Feist     "xyz.openbmc_project.Control.ThermalMode";
2569c310685SBorawski.Lukasz 
2578d1b46d7Szhanghch05 inline void
2588d1b46d7Szhanghch05     asyncPopulatePid(const std::string& connection, const std::string& path,
25973df0db0SJames Feist                      const std::string& currentProfile,
26073df0db0SJames Feist                      const std::vector<std::string>& supportedProfiles,
2618d1b46d7Szhanghch05                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2625b4aa86bSJames Feist {
2635b4aa86bSJames Feist 
2645b4aa86bSJames Feist     crow::connections::systemBus->async_method_call(
26573df0db0SJames Feist         [asyncResp, currentProfile, supportedProfiles](
26673df0db0SJames Feist             const boost::system::error_code ec,
2675b4aa86bSJames Feist             const dbus::utility::ManagedObjectType& managedObj) {
2685b4aa86bSJames Feist             if (ec)
2695b4aa86bSJames Feist             {
2705b4aa86bSJames Feist                 BMCWEB_LOG_ERROR << ec;
2715b4aa86bSJames Feist                 asyncResp->res.jsonValue.clear();
272f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2735b4aa86bSJames Feist                 return;
2745b4aa86bSJames Feist             }
2755b4aa86bSJames Feist             nlohmann::json& configRoot =
2765b4aa86bSJames Feist                 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
2775b4aa86bSJames Feist             nlohmann::json& fans = configRoot["FanControllers"];
2785b4aa86bSJames Feist             fans["@odata.type"] = "#OemManager.FanControllers";
2790fda0f12SGeorge Liu             fans["@odata.id"] =
2800fda0f12SGeorge Liu                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers";
2815b4aa86bSJames Feist 
2825b4aa86bSJames Feist             nlohmann::json& pids = configRoot["PidControllers"];
2835b4aa86bSJames Feist             pids["@odata.type"] = "#OemManager.PidControllers";
2845b4aa86bSJames Feist             pids["@odata.id"] =
2855b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
2865b4aa86bSJames Feist 
287b7a08d04SJames Feist             nlohmann::json& stepwise = configRoot["StepwiseControllers"];
288b7a08d04SJames Feist             stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
289b7a08d04SJames Feist             stepwise["@odata.id"] =
290b7a08d04SJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
291b7a08d04SJames Feist 
2925b4aa86bSJames Feist             nlohmann::json& zones = configRoot["FanZones"];
2935b4aa86bSJames Feist             zones["@odata.id"] =
2945b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
2955b4aa86bSJames Feist             zones["@odata.type"] = "#OemManager.FanZones";
2965b4aa86bSJames Feist             configRoot["@odata.id"] =
2975b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
2985b4aa86bSJames Feist             configRoot["@odata.type"] = "#OemManager.Fan";
29973df0db0SJames Feist             configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
30073df0db0SJames Feist 
30173df0db0SJames Feist             if (!currentProfile.empty())
30273df0db0SJames Feist             {
30373df0db0SJames Feist                 configRoot["Profile"] = currentProfile;
30473df0db0SJames Feist             }
30573df0db0SJames Feist             BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !";
3065b4aa86bSJames Feist 
3075b4aa86bSJames Feist             for (const auto& pathPair : managedObj)
3085b4aa86bSJames Feist             {
3095b4aa86bSJames Feist                 for (const auto& intfPair : pathPair.second)
3105b4aa86bSJames Feist                 {
3115b4aa86bSJames Feist                     if (intfPair.first != pidConfigurationIface &&
312b7a08d04SJames Feist                         intfPair.first != pidZoneConfigurationIface &&
313b7a08d04SJames Feist                         intfPair.first != stepwiseConfigurationIface)
3145b4aa86bSJames Feist                     {
3155b4aa86bSJames Feist                         continue;
3165b4aa86bSJames Feist                     }
31773df0db0SJames Feist 
318711ac7a9SEd Tanous                     std::string name;
319711ac7a9SEd Tanous 
320711ac7a9SEd Tanous                     for (const std::pair<std::string,
321711ac7a9SEd Tanous                                          dbus::utility::DbusVariantType>&
322711ac7a9SEd Tanous                              propPair : intfPair.second)
323711ac7a9SEd Tanous                     {
324711ac7a9SEd Tanous                         if (propPair.first == "Name")
325711ac7a9SEd Tanous                         {
3265b4aa86bSJames Feist                             const std::string* namePtr =
327711ac7a9SEd Tanous                                 std::get_if<std::string>(&propPair.second);
3285b4aa86bSJames Feist                             if (namePtr == nullptr)
3295b4aa86bSJames Feist                             {
3305b4aa86bSJames Feist                                 BMCWEB_LOG_ERROR << "Pid Name Field illegal";
331b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
3325b4aa86bSJames Feist                                 return;
3335b4aa86bSJames Feist                             }
334db697703SWilly Tu                             name = *namePtr;
3355b4aa86bSJames Feist                             dbus::utility::escapePathForDbus(name);
336711ac7a9SEd Tanous                         }
337711ac7a9SEd Tanous                         else if (propPair.first == "Profiles")
33873df0db0SJames Feist                         {
33973df0db0SJames Feist                             const std::vector<std::string>* profiles =
34073df0db0SJames Feist                                 std::get_if<std::vector<std::string>>(
341711ac7a9SEd Tanous                                     &propPair.second);
34273df0db0SJames Feist                             if (profiles == nullptr)
34373df0db0SJames Feist                             {
344711ac7a9SEd Tanous                                 BMCWEB_LOG_ERROR
345711ac7a9SEd Tanous                                     << "Pid Profiles Field illegal";
34673df0db0SJames Feist                                 messages::internalError(asyncResp->res);
34773df0db0SJames Feist                                 return;
34873df0db0SJames Feist                             }
34973df0db0SJames Feist                             if (std::find(profiles->begin(), profiles->end(),
35073df0db0SJames Feist                                           currentProfile) == profiles->end())
35173df0db0SJames Feist                             {
35273df0db0SJames Feist                                 BMCWEB_LOG_INFO
353711ac7a9SEd Tanous                                     << name
354711ac7a9SEd Tanous                                     << " not supported in current profile";
35573df0db0SJames Feist                                 continue;
35673df0db0SJames Feist                             }
35773df0db0SJames Feist                         }
358711ac7a9SEd Tanous                     }
359b7a08d04SJames Feist                     nlohmann::json* config = nullptr;
360c33a90ecSJames Feist                     const std::string* classPtr = nullptr;
361711ac7a9SEd Tanous 
362711ac7a9SEd Tanous                     for (const std::pair<std::string,
363711ac7a9SEd Tanous                                          dbus::utility::DbusVariantType>&
364711ac7a9SEd Tanous                              propPair : intfPair.second)
365c33a90ecSJames Feist                     {
366727dc83fSLei YU                         if (propPair.first == "Class")
367711ac7a9SEd Tanous                         {
368711ac7a9SEd Tanous                             classPtr =
369711ac7a9SEd Tanous                                 std::get_if<std::string>(&propPair.second);
370711ac7a9SEd Tanous                         }
371c33a90ecSJames Feist                     }
372c33a90ecSJames Feist 
3735b4aa86bSJames Feist                     if (intfPair.first == pidZoneConfigurationIface)
3745b4aa86bSJames Feist                     {
3755b4aa86bSJames Feist                         std::string chassis;
3765b4aa86bSJames Feist                         if (!dbus::utility::getNthStringFromPath(
3775b4aa86bSJames Feist                                 pathPair.first.str, 5, chassis))
3785b4aa86bSJames Feist                         {
3795b4aa86bSJames Feist                             chassis = "#IllegalValue";
3805b4aa86bSJames Feist                         }
3815b4aa86bSJames Feist                         nlohmann::json& zone = zones[name];
3825b4aa86bSJames Feist                         zone["Chassis"] = {
3835b4aa86bSJames Feist                             {"@odata.id", "/redfish/v1/Chassis/" + chassis}};
3840fda0f12SGeorge Liu                         zone["@odata.id"] =
3850fda0f12SGeorge Liu                             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/" +
3865b4aa86bSJames Feist                             name;
3875b4aa86bSJames Feist                         zone["@odata.type"] = "#OemManager.FanZone";
388b7a08d04SJames Feist                         config = &zone;
3895b4aa86bSJames Feist                     }
3905b4aa86bSJames Feist 
391b7a08d04SJames Feist                     else if (intfPair.first == stepwiseConfigurationIface)
3925b4aa86bSJames Feist                     {
393c33a90ecSJames Feist                         if (classPtr == nullptr)
394c33a90ecSJames Feist                         {
395c33a90ecSJames Feist                             BMCWEB_LOG_ERROR << "Pid Class Field illegal";
396c33a90ecSJames Feist                             messages::internalError(asyncResp->res);
397c33a90ecSJames Feist                             return;
398c33a90ecSJames Feist                         }
399c33a90ecSJames Feist 
400b7a08d04SJames Feist                         nlohmann::json& controller = stepwise[name];
401b7a08d04SJames Feist                         config = &controller;
4025b4aa86bSJames Feist 
403b7a08d04SJames Feist                         controller["@odata.id"] =
4040fda0f12SGeorge Liu                             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers/" +
405271584abSEd Tanous                             name;
406b7a08d04SJames Feist                         controller["@odata.type"] =
407b7a08d04SJames Feist                             "#OemManager.StepwiseController";
408b7a08d04SJames Feist 
409c33a90ecSJames Feist                         controller["Direction"] = *classPtr;
4105b4aa86bSJames Feist                     }
4115b4aa86bSJames Feist 
4125b4aa86bSJames Feist                     // pid and fans are off the same configuration
413b7a08d04SJames Feist                     else if (intfPair.first == pidConfigurationIface)
4145b4aa86bSJames Feist                     {
415c33a90ecSJames Feist 
4165b4aa86bSJames Feist                         if (classPtr == nullptr)
4175b4aa86bSJames Feist                         {
4185b4aa86bSJames Feist                             BMCWEB_LOG_ERROR << "Pid Class Field illegal";
419a08b46ccSJason M. Bills                             messages::internalError(asyncResp->res);
4205b4aa86bSJames Feist                             return;
4215b4aa86bSJames Feist                         }
4225b4aa86bSJames Feist                         bool isFan = *classPtr == "fan";
4235b4aa86bSJames Feist                         nlohmann::json& element =
4245b4aa86bSJames Feist                             isFan ? fans[name] : pids[name];
425b7a08d04SJames Feist                         config = &element;
4265b4aa86bSJames Feist                         if (isFan)
4275b4aa86bSJames Feist                         {
4285b4aa86bSJames Feist                             element["@odata.id"] =
4290fda0f12SGeorge Liu                                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers/" +
430271584abSEd Tanous                                 name;
4315b4aa86bSJames Feist                             element["@odata.type"] =
4325b4aa86bSJames Feist                                 "#OemManager.FanController";
4335b4aa86bSJames Feist                         }
4345b4aa86bSJames Feist                         else
4355b4aa86bSJames Feist                         {
4365b4aa86bSJames Feist                             element["@odata.id"] =
4370fda0f12SGeorge Liu                                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers/" +
438271584abSEd Tanous                                 name;
4395b4aa86bSJames Feist                             element["@odata.type"] =
4405b4aa86bSJames Feist                                 "#OemManager.PidController";
4415b4aa86bSJames Feist                         }
442b7a08d04SJames Feist                     }
443b7a08d04SJames Feist                     else
444b7a08d04SJames Feist                     {
445b7a08d04SJames Feist                         BMCWEB_LOG_ERROR << "Unexpected configuration";
446b7a08d04SJames Feist                         messages::internalError(asyncResp->res);
447b7a08d04SJames Feist                         return;
448b7a08d04SJames Feist                     }
449b7a08d04SJames Feist 
450b7a08d04SJames Feist                     // used for making maps out of 2 vectors
451b7a08d04SJames Feist                     const std::vector<double>* keys = nullptr;
452b7a08d04SJames Feist                     const std::vector<double>* values = nullptr;
453b7a08d04SJames Feist 
454b7a08d04SJames Feist                     for (const auto& propertyPair : intfPair.second)
455b7a08d04SJames Feist                     {
456b7a08d04SJames Feist                         if (propertyPair.first == "Type" ||
457b7a08d04SJames Feist                             propertyPair.first == "Class" ||
458b7a08d04SJames Feist                             propertyPair.first == "Name")
459b7a08d04SJames Feist                         {
460b7a08d04SJames Feist                             continue;
461b7a08d04SJames Feist                         }
462b7a08d04SJames Feist 
463b7a08d04SJames Feist                         // zones
464b7a08d04SJames Feist                         if (intfPair.first == pidZoneConfigurationIface)
465b7a08d04SJames Feist                         {
466b7a08d04SJames Feist                             const double* ptr =
467abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
468b7a08d04SJames Feist                             if (ptr == nullptr)
469b7a08d04SJames Feist                             {
470b7a08d04SJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
471b7a08d04SJames Feist                                                  << propertyPair.first;
472b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
473b7a08d04SJames Feist                                 return;
474b7a08d04SJames Feist                             }
475b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
476b7a08d04SJames Feist                         }
477b7a08d04SJames Feist 
478b7a08d04SJames Feist                         if (intfPair.first == stepwiseConfigurationIface)
479b7a08d04SJames Feist                         {
480b7a08d04SJames Feist                             if (propertyPair.first == "Reading" ||
481b7a08d04SJames Feist                                 propertyPair.first == "Output")
482b7a08d04SJames Feist                             {
483b7a08d04SJames Feist                                 const std::vector<double>* ptr =
484abf2add6SEd Tanous                                     std::get_if<std::vector<double>>(
485b7a08d04SJames Feist                                         &propertyPair.second);
486b7a08d04SJames Feist 
487b7a08d04SJames Feist                                 if (ptr == nullptr)
488b7a08d04SJames Feist                                 {
489b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
490b7a08d04SJames Feist                                                      << propertyPair.first;
491b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
492b7a08d04SJames Feist                                     return;
493b7a08d04SJames Feist                                 }
494b7a08d04SJames Feist 
495b7a08d04SJames Feist                                 if (propertyPair.first == "Reading")
496b7a08d04SJames Feist                                 {
497b7a08d04SJames Feist                                     keys = ptr;
498b7a08d04SJames Feist                                 }
499b7a08d04SJames Feist                                 else
500b7a08d04SJames Feist                                 {
501b7a08d04SJames Feist                                     values = ptr;
502b7a08d04SJames Feist                                 }
503e662eae8SEd Tanous                                 if (keys != nullptr && values != nullptr)
504b7a08d04SJames Feist                                 {
505b7a08d04SJames Feist                                     if (keys->size() != values->size())
506b7a08d04SJames Feist                                     {
507b7a08d04SJames Feist                                         BMCWEB_LOG_ERROR
5080fda0f12SGeorge Liu                                             << "Reading and Output size don't match ";
509b7a08d04SJames Feist                                         messages::internalError(asyncResp->res);
510b7a08d04SJames Feist                                         return;
511b7a08d04SJames Feist                                     }
512b7a08d04SJames Feist                                     nlohmann::json& steps = (*config)["Steps"];
513b7a08d04SJames Feist                                     steps = nlohmann::json::array();
514b7a08d04SJames Feist                                     for (size_t ii = 0; ii < keys->size(); ii++)
515b7a08d04SJames Feist                                     {
516b7a08d04SJames Feist                                         steps.push_back(
517b7a08d04SJames Feist                                             {{"Target", (*keys)[ii]},
518b7a08d04SJames Feist                                              {"Output", (*values)[ii]}});
519b7a08d04SJames Feist                                     }
520b7a08d04SJames Feist                                 }
521b7a08d04SJames Feist                             }
522b7a08d04SJames Feist                             if (propertyPair.first == "NegativeHysteresis" ||
523b7a08d04SJames Feist                                 propertyPair.first == "PositiveHysteresis")
524b7a08d04SJames Feist                             {
525b7a08d04SJames Feist                                 const double* ptr =
526abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
527b7a08d04SJames Feist                                 if (ptr == nullptr)
528b7a08d04SJames Feist                                 {
529b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
530b7a08d04SJames Feist                                                      << propertyPair.first;
531b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
532b7a08d04SJames Feist                                     return;
533b7a08d04SJames Feist                                 }
534b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
535b7a08d04SJames Feist                             }
536b7a08d04SJames Feist                         }
537b7a08d04SJames Feist 
538b7a08d04SJames Feist                         // pid and fans are off the same configuration
539b7a08d04SJames Feist                         if (intfPair.first == pidConfigurationIface ||
540b7a08d04SJames Feist                             intfPair.first == stepwiseConfigurationIface)
541b7a08d04SJames Feist                         {
5425b4aa86bSJames Feist 
5435b4aa86bSJames Feist                             if (propertyPair.first == "Zones")
5445b4aa86bSJames Feist                             {
5455b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
546abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
5471b6b96c5SEd Tanous                                         &propertyPair.second);
5485b4aa86bSJames Feist 
5495b4aa86bSJames Feist                                 if (inputs == nullptr)
5505b4aa86bSJames Feist                                 {
5515b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR
5525b4aa86bSJames Feist                                         << "Zones Pid Field Illegal";
553a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
5545b4aa86bSJames Feist                                     return;
5555b4aa86bSJames Feist                                 }
556b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
5575b4aa86bSJames Feist                                 data = nlohmann::json::array();
5585b4aa86bSJames Feist                                 for (std::string itemCopy : *inputs)
5595b4aa86bSJames Feist                                 {
5605b4aa86bSJames Feist                                     dbus::utility::escapePathForDbus(itemCopy);
5615b4aa86bSJames Feist                                     data.push_back(
5625b4aa86bSJames Feist                                         {{"@odata.id",
5630fda0f12SGeorge Liu                                           "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/" +
5645b4aa86bSJames Feist                                               itemCopy}});
5655b4aa86bSJames Feist                                 }
5665b4aa86bSJames Feist                             }
5675b4aa86bSJames Feist                             // todo(james): may never happen, but this
5685b4aa86bSJames Feist                             // assumes configuration data referenced in the
5695b4aa86bSJames Feist                             // PID config is provided by the same daemon, we
5705b4aa86bSJames Feist                             // could add another loop to cover all cases,
5715b4aa86bSJames Feist                             // but I'm okay kicking this can down the road a
5725b4aa86bSJames Feist                             // bit
5735b4aa86bSJames Feist 
5745b4aa86bSJames Feist                             else if (propertyPair.first == "Inputs" ||
5755b4aa86bSJames Feist                                      propertyPair.first == "Outputs")
5765b4aa86bSJames Feist                             {
577b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
5785b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
579abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
5801b6b96c5SEd Tanous                                         &propertyPair.second);
5815b4aa86bSJames Feist 
5825b4aa86bSJames Feist                                 if (inputs == nullptr)
5835b4aa86bSJames Feist                                 {
5845b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
5855b4aa86bSJames Feist                                                      << propertyPair.first;
586f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
5875b4aa86bSJames Feist                                     return;
5885b4aa86bSJames Feist                                 }
5895b4aa86bSJames Feist                                 data = *inputs;
590b943aaefSJames Feist                             }
591b943aaefSJames Feist                             else if (propertyPair.first == "SetPointOffset")
592b943aaefSJames Feist                             {
593b943aaefSJames Feist                                 const std::string* ptr =
594b943aaefSJames Feist                                     std::get_if<std::string>(
595b943aaefSJames Feist                                         &propertyPair.second);
596b943aaefSJames Feist 
597b943aaefSJames Feist                                 if (ptr == nullptr)
598b943aaefSJames Feist                                 {
599b943aaefSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
600b943aaefSJames Feist                                                      << propertyPair.first;
601b943aaefSJames Feist                                     messages::internalError(asyncResp->res);
602b943aaefSJames Feist                                     return;
603b943aaefSJames Feist                                 }
604b943aaefSJames Feist                                 // translate from dbus to redfish
605b943aaefSJames Feist                                 if (*ptr == "WarningHigh")
606b943aaefSJames Feist                                 {
607b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
608b943aaefSJames Feist                                         "UpperThresholdNonCritical";
609b943aaefSJames Feist                                 }
610b943aaefSJames Feist                                 else if (*ptr == "WarningLow")
611b943aaefSJames Feist                                 {
612b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
613b943aaefSJames Feist                                         "LowerThresholdNonCritical";
614b943aaefSJames Feist                                 }
615b943aaefSJames Feist                                 else if (*ptr == "CriticalHigh")
616b943aaefSJames Feist                                 {
617b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
618b943aaefSJames Feist                                         "UpperThresholdCritical";
619b943aaefSJames Feist                                 }
620b943aaefSJames Feist                                 else if (*ptr == "CriticalLow")
621b943aaefSJames Feist                                 {
622b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
623b943aaefSJames Feist                                         "LowerThresholdCritical";
624b943aaefSJames Feist                                 }
625b943aaefSJames Feist                                 else
626b943aaefSJames Feist                                 {
627b943aaefSJames Feist                                     BMCWEB_LOG_ERROR << "Value Illegal "
628b943aaefSJames Feist                                                      << *ptr;
629b943aaefSJames Feist                                     messages::internalError(asyncResp->res);
630b943aaefSJames Feist                                     return;
631b943aaefSJames Feist                                 }
632b943aaefSJames Feist                             }
633b943aaefSJames Feist                             // doubles
6345b4aa86bSJames Feist                             else if (propertyPair.first ==
6355b4aa86bSJames Feist                                          "FFGainCoefficient" ||
6365b4aa86bSJames Feist                                      propertyPair.first == "FFOffCoefficient" ||
6375b4aa86bSJames Feist                                      propertyPair.first == "ICoefficient" ||
6385b4aa86bSJames Feist                                      propertyPair.first == "ILimitMax" ||
6395b4aa86bSJames Feist                                      propertyPair.first == "ILimitMin" ||
640aad1a257SJames Feist                                      propertyPair.first ==
641aad1a257SJames Feist                                          "PositiveHysteresis" ||
642aad1a257SJames Feist                                      propertyPair.first ==
643aad1a257SJames Feist                                          "NegativeHysteresis" ||
6445b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMax" ||
6455b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMin" ||
6465b4aa86bSJames Feist                                      propertyPair.first == "PCoefficient" ||
6477625cb81SJames Feist                                      propertyPair.first == "SetPoint" ||
6485b4aa86bSJames Feist                                      propertyPair.first == "SlewNeg" ||
6495b4aa86bSJames Feist                                      propertyPair.first == "SlewPos")
6505b4aa86bSJames Feist                             {
6515b4aa86bSJames Feist                                 const double* ptr =
652abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
6535b4aa86bSJames Feist                                 if (ptr == nullptr)
6545b4aa86bSJames Feist                                 {
6555b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
6565b4aa86bSJames Feist                                                      << propertyPair.first;
657f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
6585b4aa86bSJames Feist                                     return;
6595b4aa86bSJames Feist                                 }
660b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
6615b4aa86bSJames Feist                             }
6625b4aa86bSJames Feist                         }
6635b4aa86bSJames Feist                     }
6645b4aa86bSJames Feist                 }
6655b4aa86bSJames Feist             }
6665b4aa86bSJames Feist         },
6675b4aa86bSJames Feist         connection, path, objectManagerIface, "GetManagedObjects");
6685b4aa86bSJames Feist }
669ca537928SJennifer Lee 
67083ff9ab6SJames Feist enum class CreatePIDRet
67183ff9ab6SJames Feist {
67283ff9ab6SJames Feist     fail,
67383ff9ab6SJames Feist     del,
67483ff9ab6SJames Feist     patch
67583ff9ab6SJames Feist };
67683ff9ab6SJames Feist 
6778d1b46d7Szhanghch05 inline bool
6788d1b46d7Szhanghch05     getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
6795f2caaefSJames Feist                         std::vector<nlohmann::json>& config,
6805f2caaefSJames Feist                         std::vector<std::string>& zones)
6815f2caaefSJames Feist {
682b6baeaa4SJames Feist     if (config.empty())
683b6baeaa4SJames Feist     {
684b6baeaa4SJames Feist         BMCWEB_LOG_ERROR << "Empty Zones";
6851668ce6dSEd Tanous         messages::propertyValueFormatError(response->res, "[]", "Zones");
686b6baeaa4SJames Feist         return false;
687b6baeaa4SJames Feist     }
6885f2caaefSJames Feist     for (auto& odata : config)
6895f2caaefSJames Feist     {
6905f2caaefSJames Feist         std::string path;
6915f2caaefSJames Feist         if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
6925f2caaefSJames Feist                                           path))
6935f2caaefSJames Feist         {
6945f2caaefSJames Feist             return false;
6955f2caaefSJames Feist         }
6965f2caaefSJames Feist         std::string input;
69761adbda3SJames Feist 
69861adbda3SJames Feist         // 8 below comes from
69961adbda3SJames Feist         // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
70061adbda3SJames Feist         //     0    1     2      3    4    5      6     7      8
70161adbda3SJames Feist         if (!dbus::utility::getNthStringFromPath(path, 8, input))
7025f2caaefSJames Feist         {
7035f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Got invalid path " << path;
7045f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Illegal Type Zones";
7055f2caaefSJames Feist             messages::propertyValueFormatError(response->res, odata.dump(),
7065f2caaefSJames Feist                                                "Zones");
7075f2caaefSJames Feist             return false;
7085f2caaefSJames Feist         }
7095f2caaefSJames Feist         boost::replace_all(input, "_", " ");
7105f2caaefSJames Feist         zones.emplace_back(std::move(input));
7115f2caaefSJames Feist     }
7125f2caaefSJames Feist     return true;
7135f2caaefSJames Feist }
7145f2caaefSJames Feist 
715711ac7a9SEd Tanous inline const dbus::utility::ManagedObjectType::value_type*
71673df0db0SJames Feist     findChassis(const dbus::utility::ManagedObjectType& managedObj,
717b6baeaa4SJames Feist                 const std::string& value, std::string& chassis)
718b6baeaa4SJames Feist {
719b6baeaa4SJames Feist     BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
720b6baeaa4SJames Feist 
721b6baeaa4SJames Feist     std::string escaped = boost::replace_all_copy(value, " ", "_");
722b6baeaa4SJames Feist     escaped = "/" + escaped;
723b6baeaa4SJames Feist     auto it = std::find_if(
724b6baeaa4SJames Feist         managedObj.begin(), managedObj.end(), [&escaped](const auto& obj) {
725b6baeaa4SJames Feist             if (boost::algorithm::ends_with(obj.first.str, escaped))
726b6baeaa4SJames Feist             {
727b6baeaa4SJames Feist                 BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
728b6baeaa4SJames Feist                 return true;
729b6baeaa4SJames Feist             }
730b6baeaa4SJames Feist             return false;
731b6baeaa4SJames Feist         });
732b6baeaa4SJames Feist 
733b6baeaa4SJames Feist     if (it == managedObj.end())
734b6baeaa4SJames Feist     {
73573df0db0SJames Feist         return nullptr;
736b6baeaa4SJames Feist     }
737b6baeaa4SJames Feist     // 5 comes from <chassis-name> being the 5th element
738b6baeaa4SJames Feist     // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
73973df0db0SJames Feist     if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
74073df0db0SJames Feist     {
74173df0db0SJames Feist         return &(*it);
74273df0db0SJames Feist     }
74373df0db0SJames Feist 
74473df0db0SJames Feist     return nullptr;
745b6baeaa4SJames Feist }
746b6baeaa4SJames Feist 
74723a21a1cSEd Tanous inline CreatePIDRet createPidInterface(
7488d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
749b5a76932SEd Tanous     const nlohmann::json::iterator& it, const std::string& path,
75083ff9ab6SJames Feist     const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
751*b9d36b47SEd Tanous     dbus::utility::DBusPropertiesMap& output, std::string& chassis,
752*b9d36b47SEd Tanous     const std::string& profile)
75383ff9ab6SJames Feist {
75483ff9ab6SJames Feist 
7555f2caaefSJames Feist     // common deleter
756b6baeaa4SJames Feist     if (it.value() == nullptr)
7575f2caaefSJames Feist     {
7585f2caaefSJames Feist         std::string iface;
7595f2caaefSJames Feist         if (type == "PidControllers" || type == "FanControllers")
7605f2caaefSJames Feist         {
7615f2caaefSJames Feist             iface = pidConfigurationIface;
7625f2caaefSJames Feist         }
7635f2caaefSJames Feist         else if (type == "FanZones")
7645f2caaefSJames Feist         {
7655f2caaefSJames Feist             iface = pidZoneConfigurationIface;
7665f2caaefSJames Feist         }
7675f2caaefSJames Feist         else if (type == "StepwiseControllers")
7685f2caaefSJames Feist         {
7695f2caaefSJames Feist             iface = stepwiseConfigurationIface;
7705f2caaefSJames Feist         }
7715f2caaefSJames Feist         else
7725f2caaefSJames Feist         {
773a0744d38SGunnar Mills             BMCWEB_LOG_ERROR << "Illegal Type " << type;
7745f2caaefSJames Feist             messages::propertyUnknown(response->res, type);
7755f2caaefSJames Feist             return CreatePIDRet::fail;
7765f2caaefSJames Feist         }
7776ee7f774SJames Feist 
7786ee7f774SJames Feist         BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
7795f2caaefSJames Feist         // delete interface
7805f2caaefSJames Feist         crow::connections::systemBus->async_method_call(
7815f2caaefSJames Feist             [response, path](const boost::system::error_code ec) {
7825f2caaefSJames Feist                 if (ec)
7835f2caaefSJames Feist                 {
7845f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
7855f2caaefSJames Feist                     messages::internalError(response->res);
786b6baeaa4SJames Feist                     return;
7875f2caaefSJames Feist                 }
788b6baeaa4SJames Feist                 messages::success(response->res);
7895f2caaefSJames Feist             },
7905f2caaefSJames Feist             "xyz.openbmc_project.EntityManager", path, iface, "Delete");
7915f2caaefSJames Feist         return CreatePIDRet::del;
7925f2caaefSJames Feist     }
7935f2caaefSJames Feist 
794711ac7a9SEd Tanous     const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
795b6baeaa4SJames Feist     if (!createNewObject)
796b6baeaa4SJames Feist     {
797b6baeaa4SJames Feist         // if we aren't creating a new object, we should be able to find it on
798b6baeaa4SJames Feist         // d-bus
79973df0db0SJames Feist         managedItem = findChassis(managedObj, it.key(), chassis);
80073df0db0SJames Feist         if (managedItem == nullptr)
801b6baeaa4SJames Feist         {
802b6baeaa4SJames Feist             BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
803ace85d60SEd Tanous             messages::invalidObject(response->res,
804ace85d60SEd Tanous                                     crow::utility::urlFromPieces(
805ace85d60SEd Tanous                                         "redfish", "v1", "Chassis", chassis));
806b6baeaa4SJames Feist             return CreatePIDRet::fail;
807b6baeaa4SJames Feist         }
808b6baeaa4SJames Feist     }
809b6baeaa4SJames Feist 
81026f6976fSEd Tanous     if (!profile.empty() &&
81173df0db0SJames Feist         (type == "PidControllers" || type == "FanControllers" ||
81273df0db0SJames Feist          type == "StepwiseControllers"))
81373df0db0SJames Feist     {
81473df0db0SJames Feist         if (managedItem == nullptr)
81573df0db0SJames Feist         {
816*b9d36b47SEd Tanous             output.emplace_back("Profiles", std::vector<std::string>{profile});
81773df0db0SJames Feist         }
81873df0db0SJames Feist         else
81973df0db0SJames Feist         {
82073df0db0SJames Feist             std::string interface;
82173df0db0SJames Feist             if (type == "StepwiseControllers")
82273df0db0SJames Feist             {
82373df0db0SJames Feist                 interface = stepwiseConfigurationIface;
82473df0db0SJames Feist             }
82573df0db0SJames Feist             else
82673df0db0SJames Feist             {
82773df0db0SJames Feist                 interface = pidConfigurationIface;
82873df0db0SJames Feist             }
829711ac7a9SEd Tanous             bool ifaceFound = false;
830711ac7a9SEd Tanous             for (const auto& iface : managedItem->second)
831711ac7a9SEd Tanous             {
832711ac7a9SEd Tanous                 if (iface.first == interface)
833711ac7a9SEd Tanous                 {
834711ac7a9SEd Tanous                     ifaceFound = true;
835711ac7a9SEd Tanous                     for (const auto& prop : iface.second)
836711ac7a9SEd Tanous                     {
837711ac7a9SEd Tanous                         if (prop.first == "Profiles")
838711ac7a9SEd Tanous                         {
839711ac7a9SEd Tanous                             const std::vector<std::string>* curProfiles =
840711ac7a9SEd Tanous                                 std::get_if<std::vector<std::string>>(
841711ac7a9SEd Tanous                                     &(prop.second));
842711ac7a9SEd Tanous                             if (curProfiles == nullptr)
843711ac7a9SEd Tanous                             {
844711ac7a9SEd Tanous                                 BMCWEB_LOG_ERROR
845711ac7a9SEd Tanous                                     << "Illegal profiles in managed object";
846711ac7a9SEd Tanous                                 messages::internalError(response->res);
847711ac7a9SEd Tanous                                 return CreatePIDRet::fail;
848711ac7a9SEd Tanous                             }
849711ac7a9SEd Tanous                             if (std::find(curProfiles->begin(),
850711ac7a9SEd Tanous                                           curProfiles->end(),
851711ac7a9SEd Tanous                                           profile) == curProfiles->end())
852711ac7a9SEd Tanous                             {
853711ac7a9SEd Tanous                                 std::vector<std::string> newProfiles =
854711ac7a9SEd Tanous                                     *curProfiles;
855711ac7a9SEd Tanous                                 newProfiles.push_back(profile);
856*b9d36b47SEd Tanous                                 output.emplace_back("Profiles", newProfiles);
857711ac7a9SEd Tanous                             }
858711ac7a9SEd Tanous                         }
859711ac7a9SEd Tanous                     }
860711ac7a9SEd Tanous                 }
861711ac7a9SEd Tanous             }
862711ac7a9SEd Tanous 
863711ac7a9SEd Tanous             if (!ifaceFound)
86473df0db0SJames Feist             {
86573df0db0SJames Feist                 BMCWEB_LOG_ERROR
86673df0db0SJames Feist                     << "Failed to find interface in managed object";
86773df0db0SJames Feist                 messages::internalError(response->res);
86873df0db0SJames Feist                 return CreatePIDRet::fail;
86973df0db0SJames Feist             }
87073df0db0SJames Feist         }
87173df0db0SJames Feist     }
87273df0db0SJames Feist 
87383ff9ab6SJames Feist     if (type == "PidControllers" || type == "FanControllers")
87483ff9ab6SJames Feist     {
87583ff9ab6SJames Feist         if (createNewObject)
87683ff9ab6SJames Feist         {
877*b9d36b47SEd Tanous             output.emplace_back("Class",
878*b9d36b47SEd Tanous                                 type == "PidControllers" ? "temp" : "fan");
879*b9d36b47SEd Tanous             output.emplace_back("Type", "Pid");
88083ff9ab6SJames Feist         }
8815f2caaefSJames Feist 
8825f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
8835f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
8845f2caaefSJames Feist         std::optional<std::vector<std::string>> outputs;
8855f2caaefSJames Feist         std::map<std::string, std::optional<double>> doubles;
886b943aaefSJames Feist         std::optional<std::string> setpointOffset;
8875f2caaefSJames Feist         if (!redfish::json_util::readJson(
888b6baeaa4SJames Feist                 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
8895f2caaefSJames Feist                 "Zones", zones, "FFGainCoefficient",
8905f2caaefSJames Feist                 doubles["FFGainCoefficient"], "FFOffCoefficient",
8915f2caaefSJames Feist                 doubles["FFOffCoefficient"], "ICoefficient",
8925f2caaefSJames Feist                 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
8935f2caaefSJames Feist                 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
8945f2caaefSJames Feist                 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
8955f2caaefSJames Feist                 "PCoefficient", doubles["PCoefficient"], "SetPoint",
896b943aaefSJames Feist                 doubles["SetPoint"], "SetPointOffset", setpointOffset,
897b943aaefSJames Feist                 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
898b943aaefSJames Feist                 "PositiveHysteresis", doubles["PositiveHysteresis"],
899b943aaefSJames Feist                 "NegativeHysteresis", doubles["NegativeHysteresis"]))
90083ff9ab6SJames Feist         {
90171f52d96SEd Tanous             BMCWEB_LOG_ERROR
90271f52d96SEd Tanous                 << "Illegal Property "
90371f52d96SEd Tanous                 << it.value().dump(2, ' ', true,
90471f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace);
9055f2caaefSJames Feist             return CreatePIDRet::fail;
90683ff9ab6SJames Feist         }
9075f2caaefSJames Feist         if (zones)
9085f2caaefSJames Feist         {
9095f2caaefSJames Feist             std::vector<std::string> zonesStr;
9105f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStr))
9115f2caaefSJames Feist             {
912a0744d38SGunnar Mills                 BMCWEB_LOG_ERROR << "Illegal Zones";
9135f2caaefSJames Feist                 return CreatePIDRet::fail;
9145f2caaefSJames Feist             }
915b6baeaa4SJames Feist             if (chassis.empty() &&
916e662eae8SEd Tanous                 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
917b6baeaa4SJames Feist             {
918b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
919ace85d60SEd Tanous                 messages::invalidObject(
920ace85d60SEd Tanous                     response->res, crow::utility::urlFromPieces(
921ace85d60SEd Tanous                                        "redfish", "v1", "Chassis", chassis));
922b6baeaa4SJames Feist                 return CreatePIDRet::fail;
923b6baeaa4SJames Feist             }
924*b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStr));
9255f2caaefSJames Feist         }
9265f2caaefSJames Feist         if (inputs || outputs)
9275f2caaefSJames Feist         {
9285f2caaefSJames Feist             std::array<std::optional<std::vector<std::string>>*, 2> containers =
9295f2caaefSJames Feist                 {&inputs, &outputs};
9305f2caaefSJames Feist             size_t index = 0;
9315f2caaefSJames Feist             for (const auto& containerPtr : containers)
9325f2caaefSJames Feist             {
9335f2caaefSJames Feist                 std::optional<std::vector<std::string>>& container =
9345f2caaefSJames Feist                     *containerPtr;
9355f2caaefSJames Feist                 if (!container)
9365f2caaefSJames Feist                 {
9375f2caaefSJames Feist                     index++;
9385f2caaefSJames Feist                     continue;
93983ff9ab6SJames Feist                 }
94083ff9ab6SJames Feist 
9415f2caaefSJames Feist                 for (std::string& value : *container)
94283ff9ab6SJames Feist                 {
9435f2caaefSJames Feist                     boost::replace_all(value, "_", " ");
94483ff9ab6SJames Feist                 }
9455f2caaefSJames Feist                 std::string key;
9465f2caaefSJames Feist                 if (index == 0)
9475f2caaefSJames Feist                 {
9485f2caaefSJames Feist                     key = "Inputs";
9495f2caaefSJames Feist                 }
9505f2caaefSJames Feist                 else
9515f2caaefSJames Feist                 {
9525f2caaefSJames Feist                     key = "Outputs";
9535f2caaefSJames Feist                 }
954*b9d36b47SEd Tanous                 output.emplace_back(key, *container);
9555f2caaefSJames Feist                 index++;
9565f2caaefSJames Feist             }
95783ff9ab6SJames Feist         }
95883ff9ab6SJames Feist 
959b943aaefSJames Feist         if (setpointOffset)
960b943aaefSJames Feist         {
961b943aaefSJames Feist             // translate between redfish and dbus names
962b943aaefSJames Feist             if (*setpointOffset == "UpperThresholdNonCritical")
963b943aaefSJames Feist             {
964*b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningLow");
965b943aaefSJames Feist             }
966b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdNonCritical")
967b943aaefSJames Feist             {
968*b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningHigh");
969b943aaefSJames Feist             }
970b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdCritical")
971b943aaefSJames Feist             {
972*b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalLow");
973b943aaefSJames Feist             }
974b943aaefSJames Feist             else if (*setpointOffset == "UpperThresholdCritical")
975b943aaefSJames Feist             {
976*b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalHigh");
977b943aaefSJames Feist             }
978b943aaefSJames Feist             else
979b943aaefSJames Feist             {
980b943aaefSJames Feist                 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
981b943aaefSJames Feist                                  << *setpointOffset;
982ace85d60SEd Tanous                 messages::propertyValueNotInList(response->res, it.key(),
983ace85d60SEd Tanous                                                  "SetPointOffset");
984b943aaefSJames Feist                 return CreatePIDRet::fail;
985b943aaefSJames Feist             }
986b943aaefSJames Feist         }
987b943aaefSJames Feist 
98883ff9ab6SJames Feist         // doubles
9895f2caaefSJames Feist         for (const auto& pairs : doubles)
99083ff9ab6SJames Feist         {
9915f2caaefSJames Feist             if (!pairs.second)
99283ff9ab6SJames Feist             {
9935f2caaefSJames Feist                 continue;
99483ff9ab6SJames Feist             }
9955f2caaefSJames Feist             BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
996*b9d36b47SEd Tanous             output.emplace_back(pairs.first, *pairs.second);
9975f2caaefSJames Feist         }
99883ff9ab6SJames Feist     }
99983ff9ab6SJames Feist 
100083ff9ab6SJames Feist     else if (type == "FanZones")
100183ff9ab6SJames Feist     {
1002*b9d36b47SEd Tanous         output.emplace_back("Type", "Pid.Zone");
100383ff9ab6SJames Feist 
10045f2caaefSJames Feist         std::optional<nlohmann::json> chassisContainer;
10055f2caaefSJames Feist         std::optional<double> failSafePercent;
1006d3ec07f8SJames Feist         std::optional<double> minThermalOutput;
1007b6baeaa4SJames Feist         if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
10085f2caaefSJames Feist                                           chassisContainer, "FailSafePercent",
1009d3ec07f8SJames Feist                                           failSafePercent, "MinThermalOutput",
1010d3ec07f8SJames Feist                                           minThermalOutput))
101183ff9ab6SJames Feist         {
101271f52d96SEd Tanous             BMCWEB_LOG_ERROR
101371f52d96SEd Tanous                 << "Illegal Property "
101471f52d96SEd Tanous                 << it.value().dump(2, ' ', true,
101571f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace);
101683ff9ab6SJames Feist             return CreatePIDRet::fail;
101783ff9ab6SJames Feist         }
10185f2caaefSJames Feist 
10195f2caaefSJames Feist         if (chassisContainer)
102083ff9ab6SJames Feist         {
10215f2caaefSJames Feist 
10225f2caaefSJames Feist             std::string chassisId;
10235f2caaefSJames Feist             if (!redfish::json_util::readJson(*chassisContainer, response->res,
10245f2caaefSJames Feist                                               "@odata.id", chassisId))
10255f2caaefSJames Feist             {
102671f52d96SEd Tanous                 BMCWEB_LOG_ERROR
102771f52d96SEd Tanous                     << "Illegal Property "
102871f52d96SEd Tanous                     << chassisContainer->dump(
102971f52d96SEd Tanous                            2, ' ', true,
103071f52d96SEd Tanous                            nlohmann::json::error_handler_t::replace);
103183ff9ab6SJames Feist                 return CreatePIDRet::fail;
103283ff9ab6SJames Feist             }
103383ff9ab6SJames Feist 
1034717794d5SAppaRao Puli             // /redfish/v1/chassis/chassis_name/
10355f2caaefSJames Feist             if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
103683ff9ab6SJames Feist             {
10375f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
1038ace85d60SEd Tanous                 messages::invalidObject(
1039ace85d60SEd Tanous                     response->res, crow::utility::urlFromPieces(
1040ace85d60SEd Tanous                                        "redfish", "v1", "Chassis", chassisId));
104183ff9ab6SJames Feist                 return CreatePIDRet::fail;
104283ff9ab6SJames Feist             }
104383ff9ab6SJames Feist         }
1044d3ec07f8SJames Feist         if (minThermalOutput)
104583ff9ab6SJames Feist         {
1046*b9d36b47SEd Tanous             output.emplace_back("MinThermalOutput", *minThermalOutput);
10475f2caaefSJames Feist         }
10485f2caaefSJames Feist         if (failSafePercent)
104983ff9ab6SJames Feist         {
1050*b9d36b47SEd Tanous             output.emplace_back("FailSafePercent", *failSafePercent);
10515f2caaefSJames Feist         }
10525f2caaefSJames Feist     }
10535f2caaefSJames Feist     else if (type == "StepwiseControllers")
10545f2caaefSJames Feist     {
1055*b9d36b47SEd Tanous         output.emplace_back("Type", "Stepwise");
10565f2caaefSJames Feist 
10575f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
10585f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> steps;
10595f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
10605f2caaefSJames Feist         std::optional<double> positiveHysteresis;
10615f2caaefSJames Feist         std::optional<double> negativeHysteresis;
1062c33a90ecSJames Feist         std::optional<std::string> direction; // upper clipping curve vs lower
10635f2caaefSJames Feist         if (!redfish::json_util::readJson(
1064b6baeaa4SJames Feist                 it.value(), response->res, "Zones", zones, "Steps", steps,
1065b6baeaa4SJames Feist                 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
1066c33a90ecSJames Feist                 "NegativeHysteresis", negativeHysteresis, "Direction",
1067c33a90ecSJames Feist                 direction))
10685f2caaefSJames Feist         {
106971f52d96SEd Tanous             BMCWEB_LOG_ERROR
107071f52d96SEd Tanous                 << "Illegal Property "
107171f52d96SEd Tanous                 << it.value().dump(2, ' ', true,
107271f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace);
107383ff9ab6SJames Feist             return CreatePIDRet::fail;
107483ff9ab6SJames Feist         }
10755f2caaefSJames Feist 
10765f2caaefSJames Feist         if (zones)
107783ff9ab6SJames Feist         {
1078b6baeaa4SJames Feist             std::vector<std::string> zonesStrs;
1079b6baeaa4SJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStrs))
10805f2caaefSJames Feist             {
1081a0744d38SGunnar Mills                 BMCWEB_LOG_ERROR << "Illegal Zones";
108283ff9ab6SJames Feist                 return CreatePIDRet::fail;
108383ff9ab6SJames Feist             }
1084b6baeaa4SJames Feist             if (chassis.empty() &&
1085e662eae8SEd Tanous                 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
1086b6baeaa4SJames Feist             {
1087b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
1088ace85d60SEd Tanous                 messages::invalidObject(
1089ace85d60SEd Tanous                     response->res, crow::utility::urlFromPieces(
1090ace85d60SEd Tanous                                        "redfish", "v1", "Chassis", chassis));
1091b6baeaa4SJames Feist                 return CreatePIDRet::fail;
1092b6baeaa4SJames Feist             }
1093*b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStrs));
10945f2caaefSJames Feist         }
10955f2caaefSJames Feist         if (steps)
10965f2caaefSJames Feist         {
10975f2caaefSJames Feist             std::vector<double> readings;
10985f2caaefSJames Feist             std::vector<double> outputs;
10995f2caaefSJames Feist             for (auto& step : *steps)
11005f2caaefSJames Feist             {
1101543f4400SEd Tanous                 double target = 0.0;
1102543f4400SEd Tanous                 double out = 0.0;
11035f2caaefSJames Feist 
11045f2caaefSJames Feist                 if (!redfish::json_util::readJson(step, response->res, "Target",
110523a21a1cSEd Tanous                                                   target, "Output", out))
11065f2caaefSJames Feist                 {
110771f52d96SEd Tanous                     BMCWEB_LOG_ERROR
110871f52d96SEd Tanous                         << "Illegal Property "
110971f52d96SEd Tanous                         << it.value().dump(
111071f52d96SEd Tanous                                2, ' ', true,
111171f52d96SEd Tanous                                nlohmann::json::error_handler_t::replace);
11125f2caaefSJames Feist                     return CreatePIDRet::fail;
11135f2caaefSJames Feist                 }
11145f2caaefSJames Feist                 readings.emplace_back(target);
111523a21a1cSEd Tanous                 outputs.emplace_back(out);
11165f2caaefSJames Feist             }
1117*b9d36b47SEd Tanous             output.emplace_back("Reading", std::move(readings));
1118*b9d36b47SEd Tanous             output.emplace_back("Output", std::move(outputs));
11195f2caaefSJames Feist         }
11205f2caaefSJames Feist         if (inputs)
11215f2caaefSJames Feist         {
11225f2caaefSJames Feist             for (std::string& value : *inputs)
11235f2caaefSJames Feist             {
11245f2caaefSJames Feist                 boost::replace_all(value, "_", " ");
11255f2caaefSJames Feist             }
1126*b9d36b47SEd Tanous             output.emplace_back("Inputs", std::move(*inputs));
11275f2caaefSJames Feist         }
11285f2caaefSJames Feist         if (negativeHysteresis)
11295f2caaefSJames Feist         {
1130*b9d36b47SEd Tanous             output.emplace_back("NegativeHysteresis", *negativeHysteresis);
11315f2caaefSJames Feist         }
11325f2caaefSJames Feist         if (positiveHysteresis)
11335f2caaefSJames Feist         {
1134*b9d36b47SEd Tanous             output.emplace_back("PositiveHysteresis", *positiveHysteresis);
113583ff9ab6SJames Feist         }
1136c33a90ecSJames Feist         if (direction)
1137c33a90ecSJames Feist         {
1138c33a90ecSJames Feist             constexpr const std::array<const char*, 2> allowedDirections = {
1139c33a90ecSJames Feist                 "Ceiling", "Floor"};
1140c33a90ecSJames Feist             if (std::find(allowedDirections.begin(), allowedDirections.end(),
1141c33a90ecSJames Feist                           *direction) == allowedDirections.end())
1142c33a90ecSJames Feist             {
1143c33a90ecSJames Feist                 messages::propertyValueTypeError(response->res, "Direction",
1144c33a90ecSJames Feist                                                  *direction);
1145c33a90ecSJames Feist                 return CreatePIDRet::fail;
1146c33a90ecSJames Feist             }
1147*b9d36b47SEd Tanous             output.emplace_back("Class", *direction);
1148c33a90ecSJames Feist         }
114983ff9ab6SJames Feist     }
115083ff9ab6SJames Feist     else
115183ff9ab6SJames Feist     {
1152a0744d38SGunnar Mills         BMCWEB_LOG_ERROR << "Illegal Type " << type;
115335a62c7cSJason M. Bills         messages::propertyUnknown(response->res, type);
115483ff9ab6SJames Feist         return CreatePIDRet::fail;
115583ff9ab6SJames Feist     }
115683ff9ab6SJames Feist     return CreatePIDRet::patch;
115783ff9ab6SJames Feist }
115873df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
115973df0db0SJames Feist {
116083ff9ab6SJames Feist 
11618d1b46d7Szhanghch05     GetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
116223a21a1cSEd Tanous         asyncResp(asyncRespIn)
116373df0db0SJames Feist 
11641214b7e7SGunnar Mills     {}
11659c310685SBorawski.Lukasz 
116673df0db0SJames Feist     void run()
11675b4aa86bSJames Feist     {
116873df0db0SJames Feist         std::shared_ptr<GetPIDValues> self = shared_from_this();
116973df0db0SJames Feist 
117073df0db0SJames Feist         // get all configurations
11715b4aa86bSJames Feist         crow::connections::systemBus->async_method_call(
1172*b9d36b47SEd Tanous             [self](
1173*b9d36b47SEd Tanous                 const boost::system::error_code ec,
1174*b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
11755b4aa86bSJames Feist                 if (ec)
11765b4aa86bSJames Feist                 {
11775b4aa86bSJames Feist                     BMCWEB_LOG_ERROR << ec;
117873df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
117973df0db0SJames Feist                     return;
118073df0db0SJames Feist                 }
118123a21a1cSEd Tanous                 self->subtree = subtreeLocal;
118273df0db0SJames Feist             },
118373df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
118473df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
118573df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
118673df0db0SJames Feist             std::array<const char*, 4>{
118773df0db0SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
118873df0db0SJames Feist                 objectManagerIface, stepwiseConfigurationIface});
118973df0db0SJames Feist 
119073df0db0SJames Feist         // at the same time get the selected profile
119173df0db0SJames Feist         crow::connections::systemBus->async_method_call(
1192*b9d36b47SEd Tanous             [self](
1193*b9d36b47SEd Tanous                 const boost::system::error_code ec,
1194*b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
119523a21a1cSEd Tanous                 if (ec || subtreeLocal.empty())
119673df0db0SJames Feist                 {
119773df0db0SJames Feist                     return;
119873df0db0SJames Feist                 }
119923a21a1cSEd Tanous                 if (subtreeLocal[0].second.size() != 1)
120073df0db0SJames Feist                 {
120173df0db0SJames Feist                     // invalid mapper response, should never happen
120273df0db0SJames Feist                     BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
120373df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
12045b4aa86bSJames Feist                     return;
12055b4aa86bSJames Feist                 }
12065b4aa86bSJames Feist 
120723a21a1cSEd Tanous                 const std::string& path = subtreeLocal[0].first;
120823a21a1cSEd Tanous                 const std::string& owner = subtreeLocal[0].second[0].first;
120973df0db0SJames Feist                 crow::connections::systemBus->async_method_call(
1210168e20c1SEd Tanous                     [path, owner,
1211168e20c1SEd Tanous                      self](const boost::system::error_code ec2,
1212*b9d36b47SEd Tanous                            const dbus::utility::DBusPropertiesMap& resp) {
121323a21a1cSEd Tanous                         if (ec2)
121473df0db0SJames Feist                         {
12150fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
12160fda0f12SGeorge Liu                                 << "GetPIDValues: Can't get thermalModeIface "
121773df0db0SJames Feist                                 << path;
121873df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
121973df0db0SJames Feist                             return;
122073df0db0SJames Feist                         }
1221271584abSEd Tanous                         const std::string* current = nullptr;
1222271584abSEd Tanous                         const std::vector<std::string>* supported = nullptr;
12239eb808c1SEd Tanous                         for (const auto& [key, value] : resp)
122473df0db0SJames Feist                         {
122573df0db0SJames Feist                             if (key == "Current")
122673df0db0SJames Feist                             {
122773df0db0SJames Feist                                 current = std::get_if<std::string>(&value);
122873df0db0SJames Feist                                 if (current == nullptr)
122973df0db0SJames Feist                                 {
123073df0db0SJames Feist                                     BMCWEB_LOG_ERROR
12310fda0f12SGeorge Liu                                         << "GetPIDValues: thermal mode iface invalid "
123273df0db0SJames Feist                                         << path;
123373df0db0SJames Feist                                     messages::internalError(
123473df0db0SJames Feist                                         self->asyncResp->res);
123573df0db0SJames Feist                                     return;
123673df0db0SJames Feist                                 }
123773df0db0SJames Feist                             }
123873df0db0SJames Feist                             if (key == "Supported")
123973df0db0SJames Feist                             {
124073df0db0SJames Feist                                 supported =
124173df0db0SJames Feist                                     std::get_if<std::vector<std::string>>(
124273df0db0SJames Feist                                         &value);
124373df0db0SJames Feist                                 if (supported == nullptr)
124473df0db0SJames Feist                                 {
124573df0db0SJames Feist                                     BMCWEB_LOG_ERROR
12460fda0f12SGeorge Liu                                         << "GetPIDValues: thermal mode iface invalid"
124773df0db0SJames Feist                                         << path;
124873df0db0SJames Feist                                     messages::internalError(
124973df0db0SJames Feist                                         self->asyncResp->res);
125073df0db0SJames Feist                                     return;
125173df0db0SJames Feist                                 }
125273df0db0SJames Feist                             }
125373df0db0SJames Feist                         }
125473df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
125573df0db0SJames Feist                         {
12560fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
12570fda0f12SGeorge Liu                                 << "GetPIDValues: thermal mode iface invalid "
125873df0db0SJames Feist                                 << path;
125973df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
126073df0db0SJames Feist                             return;
126173df0db0SJames Feist                         }
126273df0db0SJames Feist                         self->currentProfile = *current;
126373df0db0SJames Feist                         self->supportedProfiles = *supported;
126473df0db0SJames Feist                     },
126573df0db0SJames Feist                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
126673df0db0SJames Feist                     thermalModeIface);
126773df0db0SJames Feist             },
126873df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
126973df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
127073df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
127173df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
127273df0db0SJames Feist     }
127373df0db0SJames Feist 
127473df0db0SJames Feist     ~GetPIDValues()
127573df0db0SJames Feist     {
127673df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
127773df0db0SJames Feist         {
127873df0db0SJames Feist             return;
127973df0db0SJames Feist         }
12805b4aa86bSJames Feist         // create map of <connection, path to objMgr>>
128173df0db0SJames Feist         boost::container::flat_map<std::string, std::string> objectMgrPaths;
12826bce33bcSJames Feist         boost::container::flat_set<std::string> calledConnections;
12835b4aa86bSJames Feist         for (const auto& pathGroup : subtree)
12845b4aa86bSJames Feist         {
12855b4aa86bSJames Feist             for (const auto& connectionGroup : pathGroup.second)
12865b4aa86bSJames Feist             {
12876bce33bcSJames Feist                 auto findConnection =
12886bce33bcSJames Feist                     calledConnections.find(connectionGroup.first);
12896bce33bcSJames Feist                 if (findConnection != calledConnections.end())
12906bce33bcSJames Feist                 {
12916bce33bcSJames Feist                     break;
12926bce33bcSJames Feist                 }
129373df0db0SJames Feist                 for (const std::string& interface : connectionGroup.second)
12945b4aa86bSJames Feist                 {
12955b4aa86bSJames Feist                     if (interface == objectManagerIface)
12965b4aa86bSJames Feist                     {
129773df0db0SJames Feist                         objectMgrPaths[connectionGroup.first] = pathGroup.first;
12985b4aa86bSJames Feist                     }
12995b4aa86bSJames Feist                     // this list is alphabetical, so we
13005b4aa86bSJames Feist                     // should have found the objMgr by now
13015b4aa86bSJames Feist                     if (interface == pidConfigurationIface ||
1302b7a08d04SJames Feist                         interface == pidZoneConfigurationIface ||
1303b7a08d04SJames Feist                         interface == stepwiseConfigurationIface)
13045b4aa86bSJames Feist                     {
13055b4aa86bSJames Feist                         auto findObjMgr =
13065b4aa86bSJames Feist                             objectMgrPaths.find(connectionGroup.first);
13075b4aa86bSJames Feist                         if (findObjMgr == objectMgrPaths.end())
13085b4aa86bSJames Feist                         {
13095b4aa86bSJames Feist                             BMCWEB_LOG_DEBUG << connectionGroup.first
13105b4aa86bSJames Feist                                              << "Has no Object Manager";
13115b4aa86bSJames Feist                             continue;
13125b4aa86bSJames Feist                         }
13136bce33bcSJames Feist 
13146bce33bcSJames Feist                         calledConnections.insert(connectionGroup.first);
13156bce33bcSJames Feist 
131673df0db0SJames Feist                         asyncPopulatePid(findObjMgr->first, findObjMgr->second,
131773df0db0SJames Feist                                          currentProfile, supportedProfiles,
131873df0db0SJames Feist                                          asyncResp);
13195b4aa86bSJames Feist                         break;
13205b4aa86bSJames Feist                     }
13215b4aa86bSJames Feist                 }
13225b4aa86bSJames Feist             }
13235b4aa86bSJames Feist         }
132473df0db0SJames Feist     }
132573df0db0SJames Feist 
1326ecd6a3a2SEd Tanous     GetPIDValues(const GetPIDValues&) = delete;
1327ecd6a3a2SEd Tanous     GetPIDValues(GetPIDValues&&) = delete;
1328ecd6a3a2SEd Tanous     GetPIDValues& operator=(const GetPIDValues&) = delete;
1329ecd6a3a2SEd Tanous     GetPIDValues& operator=(GetPIDValues&&) = delete;
1330ecd6a3a2SEd Tanous 
133173df0db0SJames Feist     std::vector<std::string> supportedProfiles;
133273df0db0SJames Feist     std::string currentProfile;
1333*b9d36b47SEd Tanous     dbus::utility::MapperGetSubTreeResponse subtree;
13348d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
133573df0db0SJames Feist };
133673df0db0SJames Feist 
133773df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
133873df0db0SJames Feist {
133973df0db0SJames Feist 
13408d1b46d7Szhanghch05     SetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
134173df0db0SJames Feist                  nlohmann::json& data) :
1342271584abSEd Tanous         asyncResp(asyncRespIn)
134373df0db0SJames Feist     {
134473df0db0SJames Feist 
134573df0db0SJames Feist         std::optional<nlohmann::json> pidControllers;
134673df0db0SJames Feist         std::optional<nlohmann::json> fanControllers;
134773df0db0SJames Feist         std::optional<nlohmann::json> fanZones;
134873df0db0SJames Feist         std::optional<nlohmann::json> stepwiseControllers;
134973df0db0SJames Feist 
135073df0db0SJames Feist         if (!redfish::json_util::readJson(
135173df0db0SJames Feist                 data, asyncResp->res, "PidControllers", pidControllers,
135273df0db0SJames Feist                 "FanControllers", fanControllers, "FanZones", fanZones,
135373df0db0SJames Feist                 "StepwiseControllers", stepwiseControllers, "Profile", profile))
135473df0db0SJames Feist         {
135571f52d96SEd Tanous             BMCWEB_LOG_ERROR
135671f52d96SEd Tanous                 << "Illegal Property "
135771f52d96SEd Tanous                 << data.dump(2, ' ', true,
135871f52d96SEd Tanous                              nlohmann::json::error_handler_t::replace);
135973df0db0SJames Feist             return;
136073df0db0SJames Feist         }
136173df0db0SJames Feist         configuration.emplace_back("PidControllers", std::move(pidControllers));
136273df0db0SJames Feist         configuration.emplace_back("FanControllers", std::move(fanControllers));
136373df0db0SJames Feist         configuration.emplace_back("FanZones", std::move(fanZones));
136473df0db0SJames Feist         configuration.emplace_back("StepwiseControllers",
136573df0db0SJames Feist                                    std::move(stepwiseControllers));
136673df0db0SJames Feist     }
1367ecd6a3a2SEd Tanous 
1368ecd6a3a2SEd Tanous     SetPIDValues(const SetPIDValues&) = delete;
1369ecd6a3a2SEd Tanous     SetPIDValues(SetPIDValues&&) = delete;
1370ecd6a3a2SEd Tanous     SetPIDValues& operator=(const SetPIDValues&) = delete;
1371ecd6a3a2SEd Tanous     SetPIDValues& operator=(SetPIDValues&&) = delete;
1372ecd6a3a2SEd Tanous 
137373df0db0SJames Feist     void run()
137473df0db0SJames Feist     {
137573df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
137673df0db0SJames Feist         {
137773df0db0SJames Feist             return;
137873df0db0SJames Feist         }
137973df0db0SJames Feist 
138073df0db0SJames Feist         std::shared_ptr<SetPIDValues> self = shared_from_this();
138173df0db0SJames Feist 
138273df0db0SJames Feist         // todo(james): might make sense to do a mapper call here if this
138373df0db0SJames Feist         // interface gets more traction
138473df0db0SJames Feist         crow::connections::systemBus->async_method_call(
138573df0db0SJames Feist             [self](const boost::system::error_code ec,
1386914e2d5dSEd Tanous                    const dbus::utility::ManagedObjectType& mObj) {
138773df0db0SJames Feist                 if (ec)
138873df0db0SJames Feist                 {
138973df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
139073df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
139173df0db0SJames Feist                     return;
139273df0db0SJames Feist                 }
1393e69d9de2SJames Feist                 const std::array<const char*, 3> configurations = {
1394e69d9de2SJames Feist                     pidConfigurationIface, pidZoneConfigurationIface,
1395e69d9de2SJames Feist                     stepwiseConfigurationIface};
1396e69d9de2SJames Feist 
139714b0b8d5SJames Feist                 for (const auto& [path, object] : mObj)
1398e69d9de2SJames Feist                 {
139914b0b8d5SJames Feist                     for (const auto& [interface, _] : object)
1400e69d9de2SJames Feist                     {
1401e69d9de2SJames Feist                         if (std::find(configurations.begin(),
1402e69d9de2SJames Feist                                       configurations.end(),
1403e69d9de2SJames Feist                                       interface) != configurations.end())
1404e69d9de2SJames Feist                         {
140514b0b8d5SJames Feist                             self->objectCount++;
1406e69d9de2SJames Feist                             break;
1407e69d9de2SJames Feist                         }
1408e69d9de2SJames Feist                     }
1409e69d9de2SJames Feist                 }
1410914e2d5dSEd Tanous                 self->managedObj = mObj;
141173df0db0SJames Feist             },
141273df0db0SJames Feist             "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
141373df0db0SJames Feist             "GetManagedObjects");
141473df0db0SJames Feist 
141573df0db0SJames Feist         // at the same time get the profile information
141673df0db0SJames Feist         crow::connections::systemBus->async_method_call(
141773df0db0SJames Feist             [self](const boost::system::error_code ec,
1418*b9d36b47SEd Tanous                    const dbus::utility::MapperGetSubTreeResponse& subtree) {
141973df0db0SJames Feist                 if (ec || subtree.empty())
142073df0db0SJames Feist                 {
142173df0db0SJames Feist                     return;
142273df0db0SJames Feist                 }
142373df0db0SJames Feist                 if (subtree[0].second.empty())
142473df0db0SJames Feist                 {
142573df0db0SJames Feist                     // invalid mapper response, should never happen
142673df0db0SJames Feist                     BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
142773df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
142873df0db0SJames Feist                     return;
142973df0db0SJames Feist                 }
143073df0db0SJames Feist 
143173df0db0SJames Feist                 const std::string& path = subtree[0].first;
143273df0db0SJames Feist                 const std::string& owner = subtree[0].second[0].first;
143373df0db0SJames Feist                 crow::connections::systemBus->async_method_call(
1434*b9d36b47SEd Tanous                     [self, path,
1435*b9d36b47SEd Tanous                      owner](const boost::system::error_code ec2,
1436*b9d36b47SEd Tanous                             const dbus::utility::DBusPropertiesMap& r) {
1437cb13a392SEd Tanous                         if (ec2)
143873df0db0SJames Feist                         {
14390fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
14400fda0f12SGeorge Liu                                 << "SetPIDValues: Can't get thermalModeIface "
144173df0db0SJames Feist                                 << path;
144273df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
144373df0db0SJames Feist                             return;
144473df0db0SJames Feist                         }
1445271584abSEd Tanous                         const std::string* current = nullptr;
1446271584abSEd Tanous                         const std::vector<std::string>* supported = nullptr;
14479eb808c1SEd Tanous                         for (const auto& [key, value] : r)
144873df0db0SJames Feist                         {
144973df0db0SJames Feist                             if (key == "Current")
145073df0db0SJames Feist                             {
145173df0db0SJames Feist                                 current = std::get_if<std::string>(&value);
145273df0db0SJames Feist                                 if (current == nullptr)
145373df0db0SJames Feist                                 {
145473df0db0SJames Feist                                     BMCWEB_LOG_ERROR
14550fda0f12SGeorge Liu                                         << "SetPIDValues: thermal mode iface invalid "
145673df0db0SJames Feist                                         << path;
145773df0db0SJames Feist                                     messages::internalError(
145873df0db0SJames Feist                                         self->asyncResp->res);
145973df0db0SJames Feist                                     return;
146073df0db0SJames Feist                                 }
146173df0db0SJames Feist                             }
146273df0db0SJames Feist                             if (key == "Supported")
146373df0db0SJames Feist                             {
146473df0db0SJames Feist                                 supported =
146573df0db0SJames Feist                                     std::get_if<std::vector<std::string>>(
146673df0db0SJames Feist                                         &value);
146773df0db0SJames Feist                                 if (supported == nullptr)
146873df0db0SJames Feist                                 {
146973df0db0SJames Feist                                     BMCWEB_LOG_ERROR
14700fda0f12SGeorge Liu                                         << "SetPIDValues: thermal mode iface invalid"
147173df0db0SJames Feist                                         << path;
147273df0db0SJames Feist                                     messages::internalError(
147373df0db0SJames Feist                                         self->asyncResp->res);
147473df0db0SJames Feist                                     return;
147573df0db0SJames Feist                                 }
147673df0db0SJames Feist                             }
147773df0db0SJames Feist                         }
147873df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
147973df0db0SJames Feist                         {
14800fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
14810fda0f12SGeorge Liu                                 << "SetPIDValues: thermal mode iface invalid "
148273df0db0SJames Feist                                 << path;
148373df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
148473df0db0SJames Feist                             return;
148573df0db0SJames Feist                         }
148673df0db0SJames Feist                         self->currentProfile = *current;
148773df0db0SJames Feist                         self->supportedProfiles = *supported;
148873df0db0SJames Feist                         self->profileConnection = owner;
148973df0db0SJames Feist                         self->profilePath = path;
149073df0db0SJames Feist                     },
149173df0db0SJames Feist                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
149273df0db0SJames Feist                     thermalModeIface);
14935b4aa86bSJames Feist             },
14945b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper",
14955b4aa86bSJames Feist             "/xyz/openbmc_project/object_mapper",
14965b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
149773df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
149873df0db0SJames Feist     }
149924b2fe81SEd Tanous     void pidSetDone()
150073df0db0SJames Feist     {
150173df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
150273df0db0SJames Feist         {
150373df0db0SJames Feist             return;
15045b4aa86bSJames Feist         }
15058d1b46d7Szhanghch05         std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
150673df0db0SJames Feist         if (profile)
150773df0db0SJames Feist         {
150873df0db0SJames Feist             if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
150973df0db0SJames Feist                           *profile) == supportedProfiles.end())
151073df0db0SJames Feist             {
151173df0db0SJames Feist                 messages::actionParameterUnknown(response->res, "Profile",
151273df0db0SJames Feist                                                  *profile);
151373df0db0SJames Feist                 return;
151473df0db0SJames Feist             }
151573df0db0SJames Feist             currentProfile = *profile;
151673df0db0SJames Feist             crow::connections::systemBus->async_method_call(
151773df0db0SJames Feist                 [response](const boost::system::error_code ec) {
151873df0db0SJames Feist                     if (ec)
151973df0db0SJames Feist                     {
152073df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Error patching profile" << ec;
152173df0db0SJames Feist                         messages::internalError(response->res);
152273df0db0SJames Feist                     }
152373df0db0SJames Feist                 },
152473df0db0SJames Feist                 profileConnection, profilePath,
152573df0db0SJames Feist                 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
1526168e20c1SEd Tanous                 "Current", dbus::utility::DbusVariantType(*profile));
152773df0db0SJames Feist         }
152873df0db0SJames Feist 
152973df0db0SJames Feist         for (auto& containerPair : configuration)
153073df0db0SJames Feist         {
153173df0db0SJames Feist             auto& container = containerPair.second;
153273df0db0SJames Feist             if (!container)
153373df0db0SJames Feist             {
153473df0db0SJames Feist                 continue;
153573df0db0SJames Feist             }
15366ee7f774SJames Feist             BMCWEB_LOG_DEBUG << *container;
15376ee7f774SJames Feist 
153873df0db0SJames Feist             std::string& type = containerPair.first;
153973df0db0SJames Feist 
154073df0db0SJames Feist             for (nlohmann::json::iterator it = container->begin();
154117a897dfSManojkiran Eda                  it != container->end(); ++it)
154273df0db0SJames Feist             {
154373df0db0SJames Feist                 const auto& name = it.key();
15446ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "looking for " << name;
15456ee7f774SJames Feist 
154673df0db0SJames Feist                 auto pathItr =
154773df0db0SJames Feist                     std::find_if(managedObj.begin(), managedObj.end(),
154873df0db0SJames Feist                                  [&name](const auto& obj) {
154973df0db0SJames Feist                                      return boost::algorithm::ends_with(
155073df0db0SJames Feist                                          obj.first.str, "/" + name);
155173df0db0SJames Feist                                  });
1552*b9d36b47SEd Tanous                 dbus::utility::DBusPropertiesMap output;
155373df0db0SJames Feist 
155473df0db0SJames Feist                 output.reserve(16); // The pid interface length
155573df0db0SJames Feist 
155673df0db0SJames Feist                 // determines if we're patching entity-manager or
155773df0db0SJames Feist                 // creating a new object
155873df0db0SJames Feist                 bool createNewObject = (pathItr == managedObj.end());
15596ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
15606ee7f774SJames Feist 
156173df0db0SJames Feist                 std::string iface;
1562711ac7a9SEd Tanous                 /*
156373df0db0SJames Feist                 if (type == "PidControllers" || type == "FanControllers")
156473df0db0SJames Feist                 {
156573df0db0SJames Feist                     iface = pidConfigurationIface;
156673df0db0SJames Feist                     if (!createNewObject &&
156773df0db0SJames Feist                         pathItr->second.find(pidConfigurationIface) ==
156873df0db0SJames Feist                             pathItr->second.end())
156973df0db0SJames Feist                     {
157073df0db0SJames Feist                         createNewObject = true;
157173df0db0SJames Feist                     }
157273df0db0SJames Feist                 }
157373df0db0SJames Feist                 else if (type == "FanZones")
157473df0db0SJames Feist                 {
157573df0db0SJames Feist                     iface = pidZoneConfigurationIface;
157673df0db0SJames Feist                     if (!createNewObject &&
157773df0db0SJames Feist                         pathItr->second.find(pidZoneConfigurationIface) ==
157873df0db0SJames Feist                             pathItr->second.end())
157973df0db0SJames Feist                     {
158073df0db0SJames Feist 
158173df0db0SJames Feist                         createNewObject = true;
158273df0db0SJames Feist                     }
158373df0db0SJames Feist                 }
158473df0db0SJames Feist                 else if (type == "StepwiseControllers")
158573df0db0SJames Feist                 {
158673df0db0SJames Feist                     iface = stepwiseConfigurationIface;
158773df0db0SJames Feist                     if (!createNewObject &&
158873df0db0SJames Feist                         pathItr->second.find(stepwiseConfigurationIface) ==
158973df0db0SJames Feist                             pathItr->second.end())
159073df0db0SJames Feist                     {
159173df0db0SJames Feist                         createNewObject = true;
159273df0db0SJames Feist                     }
1593711ac7a9SEd Tanous                 }*/
15946ee7f774SJames Feist 
15956ee7f774SJames Feist                 if (createNewObject && it.value() == nullptr)
15966ee7f774SJames Feist                 {
15974e0453b1SGunnar Mills                     // can't delete a non-existent object
15981668ce6dSEd Tanous                     messages::propertyValueNotInList(response->res,
15991668ce6dSEd Tanous                                                      it.value().dump(), name);
16006ee7f774SJames Feist                     continue;
16016ee7f774SJames Feist                 }
16026ee7f774SJames Feist 
16036ee7f774SJames Feist                 std::string path;
16046ee7f774SJames Feist                 if (pathItr != managedObj.end())
16056ee7f774SJames Feist                 {
16066ee7f774SJames Feist                     path = pathItr->first.str;
16076ee7f774SJames Feist                 }
16086ee7f774SJames Feist 
160973df0db0SJames Feist                 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
1610e69d9de2SJames Feist 
1611e69d9de2SJames Feist                 // arbitrary limit to avoid attacks
1612e69d9de2SJames Feist                 constexpr const size_t controllerLimit = 500;
161314b0b8d5SJames Feist                 if (createNewObject && objectCount >= controllerLimit)
1614e69d9de2SJames Feist                 {
1615e69d9de2SJames Feist                     messages::resourceExhaustion(response->res, type);
1616e69d9de2SJames Feist                     continue;
1617e69d9de2SJames Feist                 }
1618*b9d36b47SEd Tanous                 output.emplace_back("Name",
1619*b9d36b47SEd Tanous                                     boost::replace_all_copy(name, "_", " "));
162073df0db0SJames Feist 
162173df0db0SJames Feist                 std::string chassis;
162273df0db0SJames Feist                 CreatePIDRet ret = createPidInterface(
16236ee7f774SJames Feist                     response, type, it, path, managedObj, createNewObject,
16246ee7f774SJames Feist                     output, chassis, currentProfile);
162573df0db0SJames Feist                 if (ret == CreatePIDRet::fail)
162673df0db0SJames Feist                 {
162773df0db0SJames Feist                     return;
162873df0db0SJames Feist                 }
16293174e4dfSEd Tanous                 if (ret == CreatePIDRet::del)
163073df0db0SJames Feist                 {
163173df0db0SJames Feist                     continue;
163273df0db0SJames Feist                 }
163373df0db0SJames Feist 
163473df0db0SJames Feist                 if (!createNewObject)
163573df0db0SJames Feist                 {
163673df0db0SJames Feist                     for (const auto& property : output)
163773df0db0SJames Feist                     {
163873df0db0SJames Feist                         crow::connections::systemBus->async_method_call(
163973df0db0SJames Feist                             [response,
164073df0db0SJames Feist                              propertyName{std::string(property.first)}](
164173df0db0SJames Feist                                 const boost::system::error_code ec) {
164273df0db0SJames Feist                                 if (ec)
164373df0db0SJames Feist                                 {
164473df0db0SJames Feist                                     BMCWEB_LOG_ERROR << "Error patching "
164573df0db0SJames Feist                                                      << propertyName << ": "
164673df0db0SJames Feist                                                      << ec;
164773df0db0SJames Feist                                     messages::internalError(response->res);
164873df0db0SJames Feist                                     return;
164973df0db0SJames Feist                                 }
165073df0db0SJames Feist                                 messages::success(response->res);
165173df0db0SJames Feist                             },
16526ee7f774SJames Feist                             "xyz.openbmc_project.EntityManager", path,
165373df0db0SJames Feist                             "org.freedesktop.DBus.Properties", "Set", iface,
165473df0db0SJames Feist                             property.first, property.second);
165573df0db0SJames Feist                     }
165673df0db0SJames Feist                 }
165773df0db0SJames Feist                 else
165873df0db0SJames Feist                 {
165973df0db0SJames Feist                     if (chassis.empty())
166073df0db0SJames Feist                     {
166173df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to get chassis from config";
1662ace85d60SEd Tanous                         messages::internalError(response->res);
166373df0db0SJames Feist                         return;
166473df0db0SJames Feist                     }
166573df0db0SJames Feist 
166673df0db0SJames Feist                     bool foundChassis = false;
166773df0db0SJames Feist                     for (const auto& obj : managedObj)
166873df0db0SJames Feist                     {
166973df0db0SJames Feist                         if (boost::algorithm::ends_with(obj.first.str, chassis))
167073df0db0SJames Feist                         {
167173df0db0SJames Feist                             chassis = obj.first.str;
167273df0db0SJames Feist                             foundChassis = true;
167373df0db0SJames Feist                             break;
167473df0db0SJames Feist                         }
167573df0db0SJames Feist                     }
167673df0db0SJames Feist                     if (!foundChassis)
167773df0db0SJames Feist                     {
167873df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
167973df0db0SJames Feist                         messages::resourceMissingAtURI(
1680ace85d60SEd Tanous                             response->res,
1681ace85d60SEd Tanous                             crow::utility::urlFromPieces("redfish", "v1",
1682ace85d60SEd Tanous                                                          "Chassis", chassis));
168373df0db0SJames Feist                         return;
168473df0db0SJames Feist                     }
168573df0db0SJames Feist 
168673df0db0SJames Feist                     crow::connections::systemBus->async_method_call(
168773df0db0SJames Feist                         [response](const boost::system::error_code ec) {
168873df0db0SJames Feist                             if (ec)
168973df0db0SJames Feist                             {
169073df0db0SJames Feist                                 BMCWEB_LOG_ERROR << "Error Adding Pid Object "
169173df0db0SJames Feist                                                  << ec;
169273df0db0SJames Feist                                 messages::internalError(response->res);
169373df0db0SJames Feist                                 return;
169473df0db0SJames Feist                             }
169573df0db0SJames Feist                             messages::success(response->res);
169673df0db0SJames Feist                         },
169773df0db0SJames Feist                         "xyz.openbmc_project.EntityManager", chassis,
169873df0db0SJames Feist                         "xyz.openbmc_project.AddObject", "AddObject", output);
169973df0db0SJames Feist                 }
170073df0db0SJames Feist             }
170173df0db0SJames Feist         }
170273df0db0SJames Feist     }
170324b2fe81SEd Tanous 
170424b2fe81SEd Tanous     ~SetPIDValues()
170524b2fe81SEd Tanous     {
170624b2fe81SEd Tanous         try
170724b2fe81SEd Tanous         {
170824b2fe81SEd Tanous             pidSetDone();
170924b2fe81SEd Tanous         }
171024b2fe81SEd Tanous         catch (...)
171124b2fe81SEd Tanous         {
171224b2fe81SEd Tanous             BMCWEB_LOG_CRITICAL << "pidSetDone threw exception";
171324b2fe81SEd Tanous         }
171424b2fe81SEd Tanous     }
171524b2fe81SEd Tanous 
17168d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
171773df0db0SJames Feist     std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
171873df0db0SJames Feist         configuration;
171973df0db0SJames Feist     std::optional<std::string> profile;
172073df0db0SJames Feist     dbus::utility::ManagedObjectType managedObj;
172173df0db0SJames Feist     std::vector<std::string> supportedProfiles;
172273df0db0SJames Feist     std::string currentProfile;
172373df0db0SJames Feist     std::string profileConnection;
172473df0db0SJames Feist     std::string profilePath;
172514b0b8d5SJames Feist     size_t objectCount = 0;
172673df0db0SJames Feist };
172773df0db0SJames Feist 
1728071d8fdfSSunnySrivastava1984 /**
1729071d8fdfSSunnySrivastava1984  * @brief Retrieves BMC manager location data over DBus
1730071d8fdfSSunnySrivastava1984  *
1731071d8fdfSSunnySrivastava1984  * @param[in] aResp Shared pointer for completing asynchronous calls
1732071d8fdfSSunnySrivastava1984  * @param[in] connectionName - service name
1733071d8fdfSSunnySrivastava1984  * @param[in] path - object path
1734071d8fdfSSunnySrivastava1984  * @return none
1735071d8fdfSSunnySrivastava1984  */
17368d1b46d7Szhanghch05 inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1737071d8fdfSSunnySrivastava1984                         const std::string& connectionName,
1738071d8fdfSSunnySrivastava1984                         const std::string& path)
1739071d8fdfSSunnySrivastava1984 {
1740071d8fdfSSunnySrivastava1984     BMCWEB_LOG_DEBUG << "Get BMC manager Location data.";
1741071d8fdfSSunnySrivastava1984 
17421e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
17431e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
17441e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
1745071d8fdfSSunnySrivastava1984         [aResp](const boost::system::error_code ec,
17461e1e598dSJonathan Doman                 const std::string& property) {
1747071d8fdfSSunnySrivastava1984             if (ec)
1748071d8fdfSSunnySrivastava1984             {
1749071d8fdfSSunnySrivastava1984                 BMCWEB_LOG_DEBUG << "DBUS response error for "
1750071d8fdfSSunnySrivastava1984                                     "Location";
1751071d8fdfSSunnySrivastava1984                 messages::internalError(aResp->res);
1752071d8fdfSSunnySrivastava1984                 return;
1753071d8fdfSSunnySrivastava1984             }
1754071d8fdfSSunnySrivastava1984 
1755071d8fdfSSunnySrivastava1984             aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
17561e1e598dSJonathan Doman                 property;
17571e1e598dSJonathan Doman         });
1758071d8fdfSSunnySrivastava1984 }
17597e860f15SJohn Edward Broadbent // avoid name collision systems.hpp
17607e860f15SJohn Edward Broadbent inline void
17617e860f15SJohn Edward Broadbent     managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
17624bf2b033SGunnar Mills {
17634bf2b033SGunnar Mills     BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time";
17644bf2b033SGunnar Mills 
17651e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint64_t>(
17661e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
17671e1e598dSJonathan Doman         "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
17681e1e598dSJonathan Doman         "LastRebootTime",
17694bf2b033SGunnar Mills         [aResp](const boost::system::error_code ec,
17701e1e598dSJonathan Doman                 const uint64_t lastResetTime) {
17714bf2b033SGunnar Mills             if (ec)
17724bf2b033SGunnar Mills             {
17734bf2b033SGunnar Mills                 BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
17744bf2b033SGunnar Mills                 return;
17754bf2b033SGunnar Mills             }
17764bf2b033SGunnar Mills 
17774bf2b033SGunnar Mills             // LastRebootTime is epoch time, in milliseconds
17784bf2b033SGunnar Mills             // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
17791e1e598dSJonathan Doman             uint64_t lastResetTimeStamp = lastResetTime / 1000;
17804bf2b033SGunnar Mills 
17814bf2b033SGunnar Mills             // Convert to ISO 8601 standard
17824bf2b033SGunnar Mills             aResp->res.jsonValue["LastResetTime"] =
17831d8782e7SNan Zhou                 crow::utility::getDateTimeUint(lastResetTimeStamp);
17841e1e598dSJonathan Doman         });
17854bf2b033SGunnar Mills }
17864bf2b033SGunnar Mills 
17874bfefa74SGunnar Mills /**
17884bfefa74SGunnar Mills  * @brief Set the running firmware image
17894bfefa74SGunnar Mills  *
17904bfefa74SGunnar Mills  * @param[i,o] aResp - Async response object
17914bfefa74SGunnar Mills  * @param[i] runningFirmwareTarget - Image to make the running image
17924bfefa74SGunnar Mills  *
17934bfefa74SGunnar Mills  * @return void
17944bfefa74SGunnar Mills  */
17957e860f15SJohn Edward Broadbent inline void
17967e860f15SJohn Edward Broadbent     setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1797f23b7296SEd Tanous                            const std::string& runningFirmwareTarget)
17984bfefa74SGunnar Mills {
17994bfefa74SGunnar Mills     // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1800f23b7296SEd Tanous     std::string::size_type idPos = runningFirmwareTarget.rfind('/');
18014bfefa74SGunnar Mills     if (idPos == std::string::npos)
18024bfefa74SGunnar Mills     {
18034bfefa74SGunnar Mills         messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
18044bfefa74SGunnar Mills                                          "@odata.id");
18054bfefa74SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't parse firmware ID!";
18064bfefa74SGunnar Mills         return;
18074bfefa74SGunnar Mills     }
18084bfefa74SGunnar Mills     idPos++;
18094bfefa74SGunnar Mills     if (idPos >= runningFirmwareTarget.size())
18104bfefa74SGunnar Mills     {
18114bfefa74SGunnar Mills         messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
18124bfefa74SGunnar Mills                                          "@odata.id");
18134bfefa74SGunnar Mills         BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
18144bfefa74SGunnar Mills         return;
18154bfefa74SGunnar Mills     }
18164bfefa74SGunnar Mills     std::string firmwareId = runningFirmwareTarget.substr(idPos);
18174bfefa74SGunnar Mills 
18184bfefa74SGunnar Mills     // Make sure the image is valid before setting priority
18194bfefa74SGunnar Mills     crow::connections::systemBus->async_method_call(
1820711ac7a9SEd Tanous         [aResp, firmwareId,
1821711ac7a9SEd Tanous          runningFirmwareTarget](const boost::system::error_code ec,
1822711ac7a9SEd Tanous                                 dbus::utility::ManagedObjectType& subtree) {
18234bfefa74SGunnar Mills             if (ec)
18244bfefa74SGunnar Mills             {
18254bfefa74SGunnar Mills                 BMCWEB_LOG_DEBUG << "D-Bus response error getting objects.";
18264bfefa74SGunnar Mills                 messages::internalError(aResp->res);
18274bfefa74SGunnar Mills                 return;
18284bfefa74SGunnar Mills             }
18294bfefa74SGunnar Mills 
183026f6976fSEd Tanous             if (subtree.empty())
18314bfefa74SGunnar Mills             {
18324bfefa74SGunnar Mills                 BMCWEB_LOG_DEBUG << "Can't find image!";
18334bfefa74SGunnar Mills                 messages::internalError(aResp->res);
18344bfefa74SGunnar Mills                 return;
18354bfefa74SGunnar Mills             }
18364bfefa74SGunnar Mills 
18374bfefa74SGunnar Mills             bool foundImage = false;
18384bfefa74SGunnar Mills             for (auto& object : subtree)
18394bfefa74SGunnar Mills             {
18404bfefa74SGunnar Mills                 const std::string& path =
18414bfefa74SGunnar Mills                     static_cast<const std::string&>(object.first);
1842f23b7296SEd Tanous                 std::size_t idPos2 = path.rfind('/');
18434bfefa74SGunnar Mills 
18444bfefa74SGunnar Mills                 if (idPos2 == std::string::npos)
18454bfefa74SGunnar Mills                 {
18464bfefa74SGunnar Mills                     continue;
18474bfefa74SGunnar Mills                 }
18484bfefa74SGunnar Mills 
18494bfefa74SGunnar Mills                 idPos2++;
18504bfefa74SGunnar Mills                 if (idPos2 >= path.size())
18514bfefa74SGunnar Mills                 {
18524bfefa74SGunnar Mills                     continue;
18534bfefa74SGunnar Mills                 }
18544bfefa74SGunnar Mills 
18554bfefa74SGunnar Mills                 if (path.substr(idPos2) == firmwareId)
18564bfefa74SGunnar Mills                 {
18574bfefa74SGunnar Mills                     foundImage = true;
18584bfefa74SGunnar Mills                     break;
18594bfefa74SGunnar Mills                 }
18604bfefa74SGunnar Mills             }
18614bfefa74SGunnar Mills 
18624bfefa74SGunnar Mills             if (!foundImage)
18634bfefa74SGunnar Mills             {
18644bfefa74SGunnar Mills                 messages::propertyValueNotInList(
18654bfefa74SGunnar Mills                     aResp->res, runningFirmwareTarget, "@odata.id");
18664bfefa74SGunnar Mills                 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
18674bfefa74SGunnar Mills                 return;
18684bfefa74SGunnar Mills             }
18694bfefa74SGunnar Mills 
18708cc8edecSEd Tanous             BMCWEB_LOG_DEBUG << "Setting firmware version " << firmwareId
18718cc8edecSEd Tanous                              << " to priority 0.";
18724bfefa74SGunnar Mills 
18734bfefa74SGunnar Mills             // Only support Immediate
18744bfefa74SGunnar Mills             // An addition could be a Redfish Setting like
18754bfefa74SGunnar Mills             // ActiveSoftwareImageApplyTime and support OnReset
18764bfefa74SGunnar Mills             crow::connections::systemBus->async_method_call(
18774bfefa74SGunnar Mills                 [aResp](const boost::system::error_code ec) {
18784bfefa74SGunnar Mills                     if (ec)
18794bfefa74SGunnar Mills                     {
18804bfefa74SGunnar Mills                         BMCWEB_LOG_DEBUG << "D-Bus response error setting.";
18814bfefa74SGunnar Mills                         messages::internalError(aResp->res);
18824bfefa74SGunnar Mills                         return;
18834bfefa74SGunnar Mills                     }
18844bfefa74SGunnar Mills                     doBMCGracefulRestart(aResp);
18854bfefa74SGunnar Mills                 },
18864bfefa74SGunnar Mills 
18874bfefa74SGunnar Mills                 "xyz.openbmc_project.Software.BMC.Updater",
18884bfefa74SGunnar Mills                 "/xyz/openbmc_project/software/" + firmwareId,
18894bfefa74SGunnar Mills                 "org.freedesktop.DBus.Properties", "Set",
18907e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1891168e20c1SEd Tanous                 dbus::utility::DbusVariantType(static_cast<uint8_t>(0)));
18924bfefa74SGunnar Mills         },
18934bfefa74SGunnar Mills         "xyz.openbmc_project.Software.BMC.Updater",
18947e860f15SJohn Edward Broadbent         "/xyz/openbmc_project/software", "org.freedesktop.DBus.ObjectManager",
18957e860f15SJohn Edward Broadbent         "GetManagedObjects");
18964bfefa74SGunnar Mills }
18974bfefa74SGunnar Mills 
18987e860f15SJohn Edward Broadbent inline void setDateTime(std::shared_ptr<bmcweb::AsyncResp> aResp,
18997e860f15SJohn Edward Broadbent                         std::string datetime)
1900af5d6058SSantosh Puranik {
1901af5d6058SSantosh Puranik     BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
1902af5d6058SSantosh Puranik 
1903af5d6058SSantosh Puranik     std::stringstream stream(datetime);
1904af5d6058SSantosh Puranik     // Convert from ISO 8601 to boost local_time
1905af5d6058SSantosh Puranik     // (BMC only has time in UTC)
1906af5d6058SSantosh Puranik     boost::posix_time::ptime posixTime;
1907af5d6058SSantosh Puranik     boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
1908af5d6058SSantosh Puranik     // Facet gets deleted with the stringsteam
1909af5d6058SSantosh Puranik     auto ifc = std::make_unique<boost::local_time::local_time_input_facet>(
1910af5d6058SSantosh Puranik         "%Y-%m-%d %H:%M:%S%F %ZP");
1911af5d6058SSantosh Puranik     stream.imbue(std::locale(stream.getloc(), ifc.release()));
1912af5d6058SSantosh Puranik 
19137e860f15SJohn Edward Broadbent     boost::local_time::local_date_time ldt(boost::local_time::not_a_date_time);
1914af5d6058SSantosh Puranik 
1915af5d6058SSantosh Puranik     if (stream >> ldt)
1916af5d6058SSantosh Puranik     {
1917af5d6058SSantosh Puranik         posixTime = ldt.utc_time();
1918af5d6058SSantosh Puranik         boost::posix_time::time_duration dur = posixTime - epoch;
19197e860f15SJohn Edward Broadbent         uint64_t durMicroSecs = static_cast<uint64_t>(dur.total_microseconds());
1920af5d6058SSantosh Puranik         crow::connections::systemBus->async_method_call(
1921af5d6058SSantosh Puranik             [aResp{std::move(aResp)}, datetime{std::move(datetime)}](
1922af5d6058SSantosh Puranik                 const boost::system::error_code ec) {
1923af5d6058SSantosh Puranik                 if (ec)
1924af5d6058SSantosh Puranik                 {
1925af5d6058SSantosh Puranik                     BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1926af5d6058SSantosh Puranik                                         "DBUS response error "
1927af5d6058SSantosh Puranik                                      << ec;
1928af5d6058SSantosh Puranik                     messages::internalError(aResp->res);
1929af5d6058SSantosh Puranik                     return;
1930af5d6058SSantosh Puranik                 }
1931af5d6058SSantosh Puranik                 aResp->res.jsonValue["DateTime"] = datetime;
1932af5d6058SSantosh Puranik             },
19337e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Time.Manager", "/xyz/openbmc_project/time/bmc",
1934af5d6058SSantosh Puranik             "org.freedesktop.DBus.Properties", "Set",
1935af5d6058SSantosh Puranik             "xyz.openbmc_project.Time.EpochTime", "Elapsed",
1936168e20c1SEd Tanous             dbus::utility::DbusVariantType(durMicroSecs));
1937af5d6058SSantosh Puranik     }
1938af5d6058SSantosh Puranik     else
1939af5d6058SSantosh Puranik     {
19407e860f15SJohn Edward Broadbent         messages::propertyValueFormatError(aResp->res, datetime, "DateTime");
1941af5d6058SSantosh Puranik         return;
1942af5d6058SSantosh Puranik     }
194383ff9ab6SJames Feist }
19449c310685SBorawski.Lukasz 
19457e860f15SJohn Edward Broadbent inline void requestRoutesManager(App& app)
19467e860f15SJohn Edward Broadbent {
19477e860f15SJohn Edward Broadbent     std::string uuid = persistent_data::getConfig().systemUuid;
19489c310685SBorawski.Lukasz 
19497e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
1950ed398213SEd Tanous         .privileges(redfish::privileges::getManager)
19517e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)([uuid](const crow::Request&,
19527e860f15SJohn Edward Broadbent                                                        const std::shared_ptr<
19537e860f15SJohn Edward Broadbent                                                            bmcweb::AsyncResp>&
19547e860f15SJohn Edward Broadbent                                                            asyncResp) {
19557e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
19567e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.type"] =
19577e860f15SJohn Edward Broadbent                 "#Manager.v1_11_0.Manager";
19587e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Id"] = "bmc";
19597e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
19607e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Description"] =
19617e860f15SJohn Edward Broadbent                 "Baseboard Management Controller";
19627e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["PowerState"] = "On";
19637e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Status"] = {{"State", "Enabled"},
19647e860f15SJohn Edward Broadbent                                                   {"Health", "OK"}};
19657e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["ManagerType"] = "BMC";
19667e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
19677e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
19687e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Model"] =
19697e860f15SJohn Edward Broadbent                 "OpenBmc"; // TODO(ed), get model
19707e860f15SJohn Edward Broadbent 
19717e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["LogServices"] = {
19727e860f15SJohn Edward Broadbent                 {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}};
19737e860f15SJohn Edward Broadbent 
19747e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["NetworkProtocol"] = {
19757e860f15SJohn Edward Broadbent                 {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}};
19767e860f15SJohn Edward Broadbent 
19777e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["EthernetInterfaces"] = {
19787e860f15SJohn Edward Broadbent                 {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}};
19797e860f15SJohn Edward Broadbent 
19807e860f15SJohn Edward Broadbent #ifdef BMCWEB_ENABLE_VM_NBDPROXY
19817e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["VirtualMedia"] = {
19827e860f15SJohn Edward Broadbent                 {"@odata.id", "/redfish/v1/Managers/bmc/VirtualMedia"}};
19837e860f15SJohn Edward Broadbent #endif // BMCWEB_ENABLE_VM_NBDPROXY
19847e860f15SJohn Edward Broadbent 
19857e860f15SJohn Edward Broadbent             // default oem data
19867e860f15SJohn Edward Broadbent             nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
19877e860f15SJohn Edward Broadbent             nlohmann::json& oemOpenbmc = oem["OpenBmc"];
19887e860f15SJohn Edward Broadbent             oem["@odata.type"] = "#OemManager.Oem";
19897e860f15SJohn Edward Broadbent             oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
19907e860f15SJohn Edward Broadbent             oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
19917e860f15SJohn Edward Broadbent             oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
19927e860f15SJohn Edward Broadbent             oemOpenbmc["Certificates"] = {
19937e860f15SJohn Edward Broadbent                 {"@odata.id",
19947e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/Truststore/Certificates"}};
19957e860f15SJohn Edward Broadbent 
19967e860f15SJohn Edward Broadbent             // Manager.Reset (an action) can be many values, OpenBMC only
19977e860f15SJohn Edward Broadbent             // supports BMC reboot.
19987e860f15SJohn Edward Broadbent             nlohmann::json& managerReset =
19997e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
20007e860f15SJohn Edward Broadbent             managerReset["target"] =
20017e860f15SJohn Edward Broadbent                 "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
20027e860f15SJohn Edward Broadbent             managerReset["@Redfish.ActionInfo"] =
20037e860f15SJohn Edward Broadbent                 "/redfish/v1/Managers/bmc/ResetActionInfo";
20047e860f15SJohn Edward Broadbent 
20057e860f15SJohn Edward Broadbent             // ResetToDefaults (Factory Reset) has values like
20067e860f15SJohn Edward Broadbent             // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
20077e860f15SJohn Edward Broadbent             // on OpenBMC
20087e860f15SJohn Edward Broadbent             nlohmann::json& resetToDefaults =
20097e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
20107e860f15SJohn Edward Broadbent             resetToDefaults["target"] =
20117e860f15SJohn Edward Broadbent                 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
20127e860f15SJohn Edward Broadbent             resetToDefaults["ResetType@Redfish.AllowableValues"] = {"ResetAll"};
20137e860f15SJohn Edward Broadbent 
20147c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
20157c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
20167c8c4058STejas Patil 
20177c8c4058STejas Patil             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
20187c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
20197c8c4058STejas Patil                 redfishDateTimeOffset.second;
20207e860f15SJohn Edward Broadbent 
20210e8ac5e7SGunnar Mills             // TODO (Gunnar): Remove these one day since moved to ComputerSystem
20220e8ac5e7SGunnar Mills             // Still used by OCP profiles
20230e8ac5e7SGunnar Mills             // https://github.com/opencomputeproject/OCP-Profiles/issues/23
20247e860f15SJohn Edward Broadbent             // Fill in SerialConsole info
20257e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
20267e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] =
20277e860f15SJohn Edward Broadbent                 15;
20287e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
20297e860f15SJohn Edward Broadbent                 {"IPMI", "SSH"};
20307e860f15SJohn Edward Broadbent #ifdef BMCWEB_ENABLE_KVM
20317e860f15SJohn Edward Broadbent             // Fill in GraphicalConsole info
20327e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] =
20337e860f15SJohn Edward Broadbent                 true;
20347e860f15SJohn Edward Broadbent             asyncResp->res
20357e860f15SJohn Edward Broadbent                 .jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4;
20367e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["GraphicalConsole"]
20377e860f15SJohn Edward Broadbent                                     ["ConnectTypesSupported"] = {"KVMIP"};
20387e860f15SJohn Edward Broadbent #endif // BMCWEB_ENABLE_KVM
20397e860f15SJohn Edward Broadbent 
20407e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] =
20417e860f15SJohn Edward Broadbent                 1;
20427e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Links"]["ManagerForServers"] = {
20437e860f15SJohn Edward Broadbent                 {{"@odata.id", "/redfish/v1/Systems/system"}}};
20447e860f15SJohn Edward Broadbent 
20457e860f15SJohn Edward Broadbent             auto health = std::make_shared<HealthPopulate>(asyncResp);
20467e860f15SJohn Edward Broadbent             health->isManagersHealth = true;
20477e860f15SJohn Edward Broadbent             health->populate();
20487e860f15SJohn Edward Broadbent 
20497e860f15SJohn Edward Broadbent             fw_util::populateFirmwareInformation(asyncResp, fw_util::bmcPurpose,
20507e860f15SJohn Edward Broadbent                                                  "FirmwareVersion", true);
20517e860f15SJohn Edward Broadbent 
20527e860f15SJohn Edward Broadbent             managerGetLastResetTime(asyncResp);
20537e860f15SJohn Edward Broadbent 
20547e860f15SJohn Edward Broadbent             auto pids = std::make_shared<GetPIDValues>(asyncResp);
20557e860f15SJohn Edward Broadbent             pids->run();
20567e860f15SJohn Edward Broadbent 
20577e860f15SJohn Edward Broadbent             getMainChassisId(
20587e860f15SJohn Edward Broadbent                 asyncResp, [](const std::string& chassisId,
20597e860f15SJohn Edward Broadbent                               const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
20607e860f15SJohn Edward Broadbent                     aRsp->res
20617e860f15SJohn Edward Broadbent                         .jsonValue["Links"]["ManagerForChassis@odata.count"] =
20627e860f15SJohn Edward Broadbent                         1;
20637e860f15SJohn Edward Broadbent                     aRsp->res.jsonValue["Links"]["ManagerForChassis"] = {
20647e860f15SJohn Edward Broadbent                         {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
20657e860f15SJohn Edward Broadbent                     aRsp->res.jsonValue["Links"]["ManagerInChassis"] = {
20667e860f15SJohn Edward Broadbent                         {"@odata.id", "/redfish/v1/Chassis/" + chassisId}};
20677e860f15SJohn Edward Broadbent                 });
20687e860f15SJohn Edward Broadbent 
20697e860f15SJohn Edward Broadbent             static bool started = false;
20707e860f15SJohn Edward Broadbent 
20717e860f15SJohn Edward Broadbent             if (!started)
20721abe55efSEd Tanous             {
20731e1e598dSJonathan Doman                 sdbusplus::asio::getProperty<double>(
20741e1e598dSJonathan Doman                     *crow::connections::systemBus, "org.freedesktop.systemd1",
20751e1e598dSJonathan Doman                     "/org/freedesktop/systemd1",
20761e1e598dSJonathan Doman                     "org.freedesktop.systemd1.Manager", "Progress",
20777e860f15SJohn Edward Broadbent                     [asyncResp](const boost::system::error_code ec,
20781e1e598dSJonathan Doman                                 const double& val) {
20797e860f15SJohn Edward Broadbent                         if (ec)
20801abe55efSEd Tanous                         {
20817e860f15SJohn Edward Broadbent                             BMCWEB_LOG_ERROR << "Error while getting progress";
20827e860f15SJohn Edward Broadbent                             messages::internalError(asyncResp->res);
20837e860f15SJohn Edward Broadbent                             return;
20847e860f15SJohn Edward Broadbent                         }
20851e1e598dSJonathan Doman                         if (val < 1.0)
20867e860f15SJohn Edward Broadbent                         {
20877e860f15SJohn Edward Broadbent                             asyncResp->res.jsonValue["Status"]["State"] =
20887e860f15SJohn Edward Broadbent                                 "Starting";
20897e860f15SJohn Edward Broadbent                             started = true;
20907e860f15SJohn Edward Broadbent                         }
20911e1e598dSJonathan Doman                     });
20929c310685SBorawski.Lukasz             }
20939c310685SBorawski.Lukasz 
20947e860f15SJohn Edward Broadbent             crow::connections::systemBus->async_method_call(
20957e860f15SJohn Edward Broadbent                 [asyncResp](
20967e860f15SJohn Edward Broadbent                     const boost::system::error_code ec,
2097*b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
20987e860f15SJohn Edward Broadbent                     if (ec)
20991abe55efSEd Tanous                     {
21007e860f15SJohn Edward Broadbent                         BMCWEB_LOG_DEBUG
21017e860f15SJohn Edward Broadbent                             << "D-Bus response error on GetSubTree " << ec;
21027e860f15SJohn Edward Broadbent                         return;
21037e860f15SJohn Edward Broadbent                     }
210426f6976fSEd Tanous                     if (subtree.empty())
21057e860f15SJohn Edward Broadbent                     {
21067e860f15SJohn Edward Broadbent                         BMCWEB_LOG_DEBUG << "Can't find bmc D-Bus object!";
21077e860f15SJohn Edward Broadbent                         return;
21087e860f15SJohn Edward Broadbent                     }
21097e860f15SJohn Edward Broadbent                     // Assume only 1 bmc D-Bus object
21107e860f15SJohn Edward Broadbent                     // Throw an error if there is more than 1
21117e860f15SJohn Edward Broadbent                     if (subtree.size() > 1)
21127e860f15SJohn Edward Broadbent                     {
21137e860f15SJohn Edward Broadbent                         BMCWEB_LOG_DEBUG
21147e860f15SJohn Edward Broadbent                             << "Found more than 1 bmc D-Bus object!";
21157e860f15SJohn Edward Broadbent                         messages::internalError(asyncResp->res);
21167e860f15SJohn Edward Broadbent                         return;
21177e860f15SJohn Edward Broadbent                     }
21187e860f15SJohn Edward Broadbent 
21197e860f15SJohn Edward Broadbent                     if (subtree[0].first.empty() ||
21207e860f15SJohn Edward Broadbent                         subtree[0].second.size() != 1)
21217e860f15SJohn Edward Broadbent                     {
21227e860f15SJohn Edward Broadbent                         BMCWEB_LOG_DEBUG << "Error getting bmc D-Bus object!";
21237e860f15SJohn Edward Broadbent                         messages::internalError(asyncResp->res);
21247e860f15SJohn Edward Broadbent                         return;
21257e860f15SJohn Edward Broadbent                     }
21267e860f15SJohn Edward Broadbent 
21277e860f15SJohn Edward Broadbent                     const std::string& path = subtree[0].first;
21287e860f15SJohn Edward Broadbent                     const std::string& connectionName =
21297e860f15SJohn Edward Broadbent                         subtree[0].second[0].first;
21307e860f15SJohn Edward Broadbent 
21317e860f15SJohn Edward Broadbent                     for (const auto& interfaceName :
21327e860f15SJohn Edward Broadbent                          subtree[0].second[0].second)
21337e860f15SJohn Edward Broadbent                     {
21347e860f15SJohn Edward Broadbent                         if (interfaceName ==
21357e860f15SJohn Edward Broadbent                             "xyz.openbmc_project.Inventory.Decorator.Asset")
21367e860f15SJohn Edward Broadbent                         {
21377e860f15SJohn Edward Broadbent                             crow::connections::systemBus->async_method_call(
21387e860f15SJohn Edward Broadbent                                 [asyncResp](
21397e860f15SJohn Edward Broadbent                                     const boost::system::error_code ec,
2140*b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap&
21417e860f15SJohn Edward Broadbent                                         propertiesList) {
21427e860f15SJohn Edward Broadbent                                     if (ec)
21437e860f15SJohn Edward Broadbent                                     {
21447e860f15SJohn Edward Broadbent                                         BMCWEB_LOG_DEBUG
21457e860f15SJohn Edward Broadbent                                             << "Can't get bmc asset!";
21467e860f15SJohn Edward Broadbent                                         return;
21477e860f15SJohn Edward Broadbent                                     }
21487e860f15SJohn Edward Broadbent                                     for (const std::pair<
21497e860f15SJohn Edward Broadbent                                              std::string,
2150168e20c1SEd Tanous                                              dbus::utility::DbusVariantType>&
21517e860f15SJohn Edward Broadbent                                              property : propertiesList)
21527e860f15SJohn Edward Broadbent                                     {
21537e860f15SJohn Edward Broadbent                                         const std::string& propertyName =
21547e860f15SJohn Edward Broadbent                                             property.first;
21557e860f15SJohn Edward Broadbent 
21567e860f15SJohn Edward Broadbent                                         if ((propertyName == "PartNumber") ||
21577e860f15SJohn Edward Broadbent                                             (propertyName == "SerialNumber") ||
21587e860f15SJohn Edward Broadbent                                             (propertyName == "Manufacturer") ||
21597e860f15SJohn Edward Broadbent                                             (propertyName == "Model") ||
21607e860f15SJohn Edward Broadbent                                             (propertyName == "SparePartNumber"))
21617e860f15SJohn Edward Broadbent                                         {
21627e860f15SJohn Edward Broadbent                                             const std::string* value =
21637e860f15SJohn Edward Broadbent                                                 std::get_if<std::string>(
21647e860f15SJohn Edward Broadbent                                                     &property.second);
21657e860f15SJohn Edward Broadbent                                             if (value == nullptr)
21667e860f15SJohn Edward Broadbent                                             {
21677e860f15SJohn Edward Broadbent                                                 // illegal property
21687e860f15SJohn Edward Broadbent                                                 messages::internalError(
21697e860f15SJohn Edward Broadbent                                                     asyncResp->res);
21707e860f15SJohn Edward Broadbent                                                 return;
21717e860f15SJohn Edward Broadbent                                             }
21727e860f15SJohn Edward Broadbent                                             asyncResp->res
21737e860f15SJohn Edward Broadbent                                                 .jsonValue[propertyName] =
21747e860f15SJohn Edward Broadbent                                                 *value;
21757e860f15SJohn Edward Broadbent                                         }
21767e860f15SJohn Edward Broadbent                                     }
21777e860f15SJohn Edward Broadbent                                 },
21787e860f15SJohn Edward Broadbent                                 connectionName, path,
21797e860f15SJohn Edward Broadbent                                 "org.freedesktop.DBus.Properties", "GetAll",
21800fda0f12SGeorge Liu                                 "xyz.openbmc_project.Inventory.Decorator.Asset");
21817e860f15SJohn Edward Broadbent                         }
21820fda0f12SGeorge Liu                         else if (
21830fda0f12SGeorge Liu                             interfaceName ==
21840fda0f12SGeorge Liu                             "xyz.openbmc_project.Inventory.Decorator.LocationCode")
21857e860f15SJohn Edward Broadbent                         {
21867e860f15SJohn Edward Broadbent                             getLocation(asyncResp, connectionName, path);
21877e860f15SJohn Edward Broadbent                         }
21887e860f15SJohn Edward Broadbent                     }
21897e860f15SJohn Edward Broadbent                 },
21907e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper",
21917e860f15SJohn Edward Broadbent                 "/xyz/openbmc_project/object_mapper",
21927e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
21937e860f15SJohn Edward Broadbent                 "/xyz/openbmc_project/inventory", int32_t(0),
21947e860f15SJohn Edward Broadbent                 std::array<const char*, 1>{
21957e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Inventory.Item.Bmc"});
21967e860f15SJohn Edward Broadbent         });
21977e860f15SJohn Edward Broadbent 
21987e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
2199ed398213SEd Tanous         .privileges(redfish::privileges::patchManager)
22007e860f15SJohn Edward Broadbent         .methods(
22017e860f15SJohn Edward Broadbent             boost::beast::http::verb::
22027e860f15SJohn Edward Broadbent                 patch)([](const crow::Request& req,
22037e860f15SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
22047e860f15SJohn Edward Broadbent             std::optional<nlohmann::json> oem;
22057e860f15SJohn Edward Broadbent             std::optional<nlohmann::json> links;
22067e860f15SJohn Edward Broadbent             std::optional<std::string> datetime;
22077e860f15SJohn Edward Broadbent 
220815ed6780SWilly Tu             if (!json_util::readJsonPatch(req, asyncResp->res, "Oem", oem,
22097e860f15SJohn Edward Broadbent                                           "DateTime", datetime, "Links", links))
22107e860f15SJohn Edward Broadbent             {
22117e860f15SJohn Edward Broadbent                 return;
22127e860f15SJohn Edward Broadbent             }
22137e860f15SJohn Edward Broadbent 
22147e860f15SJohn Edward Broadbent             if (oem)
22157e860f15SJohn Edward Broadbent             {
22167e860f15SJohn Edward Broadbent                 std::optional<nlohmann::json> openbmc;
22177e860f15SJohn Edward Broadbent                 if (!redfish::json_util::readJson(*oem, asyncResp->res,
22187e860f15SJohn Edward Broadbent                                                   "OpenBmc", openbmc))
22197e860f15SJohn Edward Broadbent                 {
22207e860f15SJohn Edward Broadbent                     BMCWEB_LOG_ERROR
22217e860f15SJohn Edward Broadbent                         << "Illegal Property "
22227e860f15SJohn Edward Broadbent                         << oem->dump(2, ' ', true,
22237e860f15SJohn Edward Broadbent                                      nlohmann::json::error_handler_t::replace);
22247e860f15SJohn Edward Broadbent                     return;
22257e860f15SJohn Edward Broadbent                 }
22267e860f15SJohn Edward Broadbent                 if (openbmc)
22277e860f15SJohn Edward Broadbent                 {
22287e860f15SJohn Edward Broadbent                     std::optional<nlohmann::json> fan;
22297e860f15SJohn Edward Broadbent                     if (!redfish::json_util::readJson(*openbmc, asyncResp->res,
22307e860f15SJohn Edward Broadbent                                                       "Fan", fan))
22317e860f15SJohn Edward Broadbent                     {
22327e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR
22337e860f15SJohn Edward Broadbent                             << "Illegal Property "
22347e860f15SJohn Edward Broadbent                             << openbmc->dump(
22357e860f15SJohn Edward Broadbent                                    2, ' ', true,
22367e860f15SJohn Edward Broadbent                                    nlohmann::json::error_handler_t::replace);
22377e860f15SJohn Edward Broadbent                         return;
22387e860f15SJohn Edward Broadbent                     }
22397e860f15SJohn Edward Broadbent                     if (fan)
22407e860f15SJohn Edward Broadbent                     {
22417e860f15SJohn Edward Broadbent                         auto pid =
22427e860f15SJohn Edward Broadbent                             std::make_shared<SetPIDValues>(asyncResp, *fan);
22437e860f15SJohn Edward Broadbent                         pid->run();
22447e860f15SJohn Edward Broadbent                     }
22457e860f15SJohn Edward Broadbent                 }
22467e860f15SJohn Edward Broadbent             }
22477e860f15SJohn Edward Broadbent             if (links)
22487e860f15SJohn Edward Broadbent             {
22497e860f15SJohn Edward Broadbent                 std::optional<nlohmann::json> activeSoftwareImage;
22507e860f15SJohn Edward Broadbent                 if (!redfish::json_util::readJson(*links, asyncResp->res,
22517e860f15SJohn Edward Broadbent                                                   "ActiveSoftwareImage",
22527e860f15SJohn Edward Broadbent                                                   activeSoftwareImage))
22537e860f15SJohn Edward Broadbent                 {
22547e860f15SJohn Edward Broadbent                     return;
22557e860f15SJohn Edward Broadbent                 }
22567e860f15SJohn Edward Broadbent                 if (activeSoftwareImage)
22577e860f15SJohn Edward Broadbent                 {
22587e860f15SJohn Edward Broadbent                     std::optional<std::string> odataId;
22597e860f15SJohn Edward Broadbent                     if (!json_util::readJson(*activeSoftwareImage,
22607e860f15SJohn Edward Broadbent                                              asyncResp->res, "@odata.id",
22617e860f15SJohn Edward Broadbent                                              odataId))
22627e860f15SJohn Edward Broadbent                     {
22637e860f15SJohn Edward Broadbent                         return;
22647e860f15SJohn Edward Broadbent                     }
22657e860f15SJohn Edward Broadbent 
22667e860f15SJohn Edward Broadbent                     if (odataId)
22677e860f15SJohn Edward Broadbent                     {
22687e860f15SJohn Edward Broadbent                         setActiveFirmwareImage(asyncResp, *odataId);
22697e860f15SJohn Edward Broadbent                     }
22707e860f15SJohn Edward Broadbent                 }
22717e860f15SJohn Edward Broadbent             }
22727e860f15SJohn Edward Broadbent             if (datetime)
22737e860f15SJohn Edward Broadbent             {
22747e860f15SJohn Edward Broadbent                 setDateTime(asyncResp, std::move(*datetime));
22757e860f15SJohn Edward Broadbent             }
22767e860f15SJohn Edward Broadbent         });
22777e860f15SJohn Edward Broadbent }
22787e860f15SJohn Edward Broadbent 
22797e860f15SJohn Edward Broadbent inline void requestRoutesManagerCollection(App& app)
22807e860f15SJohn Edward Broadbent {
22817e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
2282ed398213SEd Tanous         .privileges(redfish::privileges::getManagerCollection)
22837e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
22847e860f15SJohn Edward Broadbent             [](const crow::Request&,
22857e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
228683ff9ab6SJames Feist                 // Collections don't include the static data added by SubRoute
228783ff9ab6SJames Feist                 // because it has a duplicate entry for members
22888d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
22898d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
22908d1b46d7Szhanghch05                     "#ManagerCollection.ManagerCollection";
22918d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "Manager Collection";
22928d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Members@odata.count"] = 1;
22938d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Members"] = {
22945b4aa86bSJames Feist                     {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
22957e860f15SJohn Edward Broadbent             });
22969c310685SBorawski.Lukasz }
22979c310685SBorawski.Lukasz } // namespace redfish
2298