xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision 1476687deb1697d865b20458a0097c9ab5fd44e2)
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>
2545ca1b86SEd Tanous #include <query.hpp>
26ed398213SEd Tanous #include <registries/privilege_registry.hpp>
27e90c5052SAndrew Geissler #include <utils/fw_utils.hpp>
287bffdb7eSBernard Wong #include <utils/systemd_utils.hpp>
291214b7e7SGunnar Mills 
304bfefa74SGunnar Mills #include <cstdint>
311214b7e7SGunnar Mills #include <memory>
321214b7e7SGunnar Mills #include <sstream>
33abf2add6SEd Tanous #include <variant>
345b4aa86bSJames Feist 
351abe55efSEd Tanous namespace redfish
361abe55efSEd Tanous {
37ed5befbdSJennifer Lee 
38ed5befbdSJennifer Lee /**
392a5c4407SGunnar Mills  * Function reboots the BMC.
402a5c4407SGunnar Mills  *
412a5c4407SGunnar Mills  * @param[in] asyncResp - Shared pointer for completing asynchronous calls
42ed5befbdSJennifer Lee  */
438d1b46d7Szhanghch05 inline void
448d1b46d7Szhanghch05     doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
45ed5befbdSJennifer Lee {
46ed5befbdSJennifer Lee     const char* processName = "xyz.openbmc_project.State.BMC";
47ed5befbdSJennifer Lee     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
48ed5befbdSJennifer Lee     const char* interfaceName = "xyz.openbmc_project.State.BMC";
49ed5befbdSJennifer Lee     const std::string& propertyValue =
50ed5befbdSJennifer Lee         "xyz.openbmc_project.State.BMC.Transition.Reboot";
51ed5befbdSJennifer Lee     const char* destProperty = "RequestedBMCTransition";
52ed5befbdSJennifer Lee 
53ed5befbdSJennifer Lee     // Create the D-Bus variant for D-Bus call.
54168e20c1SEd Tanous     dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
55ed5befbdSJennifer Lee 
56ed5befbdSJennifer Lee     crow::connections::systemBus->async_method_call(
57ed5befbdSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
58ed5befbdSJennifer Lee             // Use "Set" method to set the property value.
59ed5befbdSJennifer Lee             if (ec)
60ed5befbdSJennifer Lee             {
612a5c4407SGunnar Mills                 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
62ed5befbdSJennifer Lee                 messages::internalError(asyncResp->res);
63ed5befbdSJennifer Lee                 return;
64ed5befbdSJennifer Lee             }
65ed5befbdSJennifer Lee 
66ed5befbdSJennifer Lee             messages::success(asyncResp->res);
67ed5befbdSJennifer Lee         },
68ed5befbdSJennifer Lee         processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
69ed5befbdSJennifer Lee         interfaceName, destProperty, dbusPropertyValue);
70ed5befbdSJennifer Lee }
712a5c4407SGunnar Mills 
728d1b46d7Szhanghch05 inline void
738d1b46d7Szhanghch05     doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
74f92af389SJayaprakash Mutyala {
75f92af389SJayaprakash Mutyala     const char* processName = "xyz.openbmc_project.State.BMC";
76f92af389SJayaprakash Mutyala     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
77f92af389SJayaprakash Mutyala     const char* interfaceName = "xyz.openbmc_project.State.BMC";
78f92af389SJayaprakash Mutyala     const std::string& propertyValue =
79f92af389SJayaprakash Mutyala         "xyz.openbmc_project.State.BMC.Transition.HardReboot";
80f92af389SJayaprakash Mutyala     const char* destProperty = "RequestedBMCTransition";
81f92af389SJayaprakash Mutyala 
82f92af389SJayaprakash Mutyala     // Create the D-Bus variant for D-Bus call.
83168e20c1SEd Tanous     dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
84f92af389SJayaprakash Mutyala 
85f92af389SJayaprakash Mutyala     crow::connections::systemBus->async_method_call(
86f92af389SJayaprakash Mutyala         [asyncResp](const boost::system::error_code ec) {
87f92af389SJayaprakash Mutyala             // Use "Set" method to set the property value.
88f92af389SJayaprakash Mutyala             if (ec)
89f92af389SJayaprakash Mutyala             {
90f92af389SJayaprakash Mutyala                 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
91f92af389SJayaprakash Mutyala                 messages::internalError(asyncResp->res);
92f92af389SJayaprakash Mutyala                 return;
93f92af389SJayaprakash Mutyala             }
94f92af389SJayaprakash Mutyala 
95f92af389SJayaprakash Mutyala             messages::success(asyncResp->res);
96f92af389SJayaprakash Mutyala         },
97f92af389SJayaprakash Mutyala         processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
98f92af389SJayaprakash Mutyala         interfaceName, destProperty, dbusPropertyValue);
99f92af389SJayaprakash Mutyala }
100f92af389SJayaprakash Mutyala 
1012a5c4407SGunnar Mills /**
1022a5c4407SGunnar Mills  * ManagerResetAction class supports the POST method for the Reset (reboot)
1032a5c4407SGunnar Mills  * action.
1042a5c4407SGunnar Mills  */
1057e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetAction(App& app)
1062a5c4407SGunnar Mills {
1072a5c4407SGunnar Mills     /**
1082a5c4407SGunnar Mills      * Function handles POST method request.
1092a5c4407SGunnar Mills      * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
110f92af389SJayaprakash Mutyala      * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
1112a5c4407SGunnar Mills      */
1127e860f15SJohn Edward Broadbent 
1137e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
114ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
1157e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
11645ca1b86SEd Tanous             [&app](const crow::Request& req,
1177e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
11845ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
11945ca1b86SEd Tanous                 {
12045ca1b86SEd Tanous                     return;
12145ca1b86SEd Tanous                 }
1222a5c4407SGunnar Mills                 BMCWEB_LOG_DEBUG << "Post Manager Reset.";
1232a5c4407SGunnar Mills 
1242a5c4407SGunnar Mills                 std::string resetType;
1252a5c4407SGunnar Mills 
12615ed6780SWilly Tu                 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
1277e860f15SJohn Edward Broadbent                                                resetType))
1282a5c4407SGunnar Mills                 {
1292a5c4407SGunnar Mills                     return;
1302a5c4407SGunnar Mills                 }
1312a5c4407SGunnar Mills 
132f92af389SJayaprakash Mutyala                 if (resetType == "GracefulRestart")
133f92af389SJayaprakash Mutyala                 {
134f92af389SJayaprakash Mutyala                     BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
135f92af389SJayaprakash Mutyala                     doBMCGracefulRestart(asyncResp);
136f92af389SJayaprakash Mutyala                     return;
137f92af389SJayaprakash Mutyala                 }
1383174e4dfSEd Tanous                 if (resetType == "ForceRestart")
139f92af389SJayaprakash Mutyala                 {
140f92af389SJayaprakash Mutyala                     BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
141f92af389SJayaprakash Mutyala                     doBMCForceRestart(asyncResp);
142f92af389SJayaprakash Mutyala                     return;
143f92af389SJayaprakash Mutyala                 }
1442a5c4407SGunnar Mills                 BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
1452a5c4407SGunnar Mills                                  << resetType;
1462a5c4407SGunnar Mills                 messages::actionParameterNotSupported(asyncResp->res, resetType,
1472a5c4407SGunnar Mills                                                       "ResetType");
1482a5c4407SGunnar Mills 
1492a5c4407SGunnar Mills                 return;
1507e860f15SJohn Edward Broadbent             });
1512a5c4407SGunnar Mills }
152ed5befbdSJennifer Lee 
1533e40fc74SGunnar Mills /**
1543e40fc74SGunnar Mills  * ManagerResetToDefaultsAction class supports POST method for factory reset
1553e40fc74SGunnar Mills  * action.
1563e40fc74SGunnar Mills  */
1577e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetToDefaultsAction(App& app)
1583e40fc74SGunnar Mills {
1593e40fc74SGunnar Mills 
1603e40fc74SGunnar Mills     /**
1613e40fc74SGunnar Mills      * Function handles ResetToDefaults POST method request.
1623e40fc74SGunnar Mills      *
1633e40fc74SGunnar Mills      * Analyzes POST body message and factory resets BMC by calling
1643e40fc74SGunnar Mills      * BMC code updater factory reset followed by a BMC reboot.
1653e40fc74SGunnar Mills      *
1663e40fc74SGunnar Mills      * BMC code updater factory reset wipes the whole BMC read-write
1673e40fc74SGunnar Mills      * filesystem which includes things like the network settings.
1683e40fc74SGunnar Mills      *
1693e40fc74SGunnar Mills      * OpenBMC only supports ResetToDefaultsType "ResetAll".
1703e40fc74SGunnar Mills      */
1717e860f15SJohn Edward Broadbent 
1727e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
1737e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/")
174ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
1757e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
17645ca1b86SEd Tanous             [&app](const crow::Request& req,
1777e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
17845ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
17945ca1b86SEd Tanous                 {
18045ca1b86SEd Tanous                     return;
18145ca1b86SEd Tanous                 }
1823e40fc74SGunnar Mills                 BMCWEB_LOG_DEBUG << "Post ResetToDefaults.";
1833e40fc74SGunnar Mills 
1843e40fc74SGunnar Mills                 std::string resetType;
1853e40fc74SGunnar Mills 
18615ed6780SWilly Tu                 if (!json_util::readJsonAction(
18715ed6780SWilly Tu                         req, asyncResp->res, "ResetToDefaultsType", resetType))
1883e40fc74SGunnar Mills                 {
1893e40fc74SGunnar Mills                     BMCWEB_LOG_DEBUG << "Missing property ResetToDefaultsType.";
1903e40fc74SGunnar Mills 
1917e860f15SJohn Edward Broadbent                     messages::actionParameterMissing(asyncResp->res,
1927e860f15SJohn Edward Broadbent                                                      "ResetToDefaults",
1933e40fc74SGunnar Mills                                                      "ResetToDefaultsType");
1943e40fc74SGunnar Mills                     return;
1953e40fc74SGunnar Mills                 }
1963e40fc74SGunnar Mills 
1973e40fc74SGunnar Mills                 if (resetType != "ResetAll")
1983e40fc74SGunnar Mills                 {
1990fda0f12SGeorge Liu                     BMCWEB_LOG_DEBUG
2000fda0f12SGeorge Liu                         << "Invalid property value for ResetToDefaultsType: "
2013e40fc74SGunnar Mills                         << resetType;
2027e860f15SJohn Edward Broadbent                     messages::actionParameterNotSupported(
2037e860f15SJohn Edward Broadbent                         asyncResp->res, resetType, "ResetToDefaultsType");
2043e40fc74SGunnar Mills                     return;
2053e40fc74SGunnar Mills                 }
2063e40fc74SGunnar Mills 
2073e40fc74SGunnar Mills                 crow::connections::systemBus->async_method_call(
2083e40fc74SGunnar Mills                     [asyncResp](const boost::system::error_code ec) {
2093e40fc74SGunnar Mills                         if (ec)
2103e40fc74SGunnar Mills                         {
2117e860f15SJohn Edward Broadbent                             BMCWEB_LOG_DEBUG << "Failed to ResetToDefaults: "
2127e860f15SJohn Edward Broadbent                                              << ec;
2133e40fc74SGunnar Mills                             messages::internalError(asyncResp->res);
2143e40fc74SGunnar Mills                             return;
2153e40fc74SGunnar Mills                         }
2163e40fc74SGunnar Mills                         // Factory Reset doesn't actually happen until a reboot
2173e40fc74SGunnar Mills                         // Can't erase what the BMC is running on
2183e40fc74SGunnar Mills                         doBMCGracefulRestart(asyncResp);
2193e40fc74SGunnar Mills                     },
2203e40fc74SGunnar Mills                     "xyz.openbmc_project.Software.BMC.Updater",
2213e40fc74SGunnar Mills                     "/xyz/openbmc_project/software",
2223e40fc74SGunnar Mills                     "xyz.openbmc_project.Common.FactoryReset", "Reset");
2237e860f15SJohn Edward Broadbent             });
2243e40fc74SGunnar Mills }
2253e40fc74SGunnar Mills 
2261cb1a9e6SAppaRao Puli /**
2271cb1a9e6SAppaRao Puli  * ManagerResetActionInfo derived class for delivering Manager
2281cb1a9e6SAppaRao Puli  * ResetType AllowableValues using ResetInfo schema.
2291cb1a9e6SAppaRao Puli  */
2307e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetActionInfo(App& app)
2311cb1a9e6SAppaRao Puli {
2321cb1a9e6SAppaRao Puli     /**
2331cb1a9e6SAppaRao Puli      * Functions triggers appropriate requests on DBus
2341cb1a9e6SAppaRao Puli      */
2357e860f15SJohn Edward Broadbent 
2367e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/ResetActionInfo/")
237ed398213SEd Tanous         .privileges(redfish::privileges::getActionInfo)
2387e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
23945ca1b86SEd Tanous             [&app](const crow::Request& req,
2407e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
24145ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
24245ca1b86SEd Tanous                 {
24345ca1b86SEd Tanous                     return;
24445ca1b86SEd Tanous                 }
245*1476687dSEd Tanous 
246*1476687dSEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
247*1476687dSEd Tanous                     "#ActionInfo.v1_1_2.ActionInfo";
248*1476687dSEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
249*1476687dSEd Tanous                     "/redfish/v1/Managers/bmc/ResetActionInfo";
250*1476687dSEd Tanous                 asyncResp->res.jsonValue["Name"] = "Reset Action Info";
251*1476687dSEd Tanous                 asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
252*1476687dSEd Tanous                 nlohmann::json::object_t parameter;
253*1476687dSEd Tanous                 parameter["Name"] = "ResetType";
254*1476687dSEd Tanous                 parameter["Required"] = true;
255*1476687dSEd Tanous                 parameter["DataType"] = "String";
256*1476687dSEd Tanous 
257*1476687dSEd Tanous                 nlohmann::json::array_t allowableValues;
258*1476687dSEd Tanous                 allowableValues.push_back("GracefulRestart");
259*1476687dSEd Tanous                 allowableValues.push_back("ForceRestart");
260*1476687dSEd Tanous                 parameter["AllowableValues"] = std::move(allowableValues);
261*1476687dSEd Tanous 
262*1476687dSEd Tanous                 nlohmann::json::array_t parameters;
263*1476687dSEd Tanous                 parameters.push_back(std::move(parameter));
264*1476687dSEd Tanous 
265*1476687dSEd Tanous                 asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
2667e860f15SJohn Edward Broadbent             });
2671cb1a9e6SAppaRao Puli }
2681cb1a9e6SAppaRao Puli 
2695b4aa86bSJames Feist static constexpr const char* objectManagerIface =
2705b4aa86bSJames Feist     "org.freedesktop.DBus.ObjectManager";
2715b4aa86bSJames Feist static constexpr const char* pidConfigurationIface =
2725b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid";
2735b4aa86bSJames Feist static constexpr const char* pidZoneConfigurationIface =
2745b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid.Zone";
275b7a08d04SJames Feist static constexpr const char* stepwiseConfigurationIface =
276b7a08d04SJames Feist     "xyz.openbmc_project.Configuration.Stepwise";
27773df0db0SJames Feist static constexpr const char* thermalModeIface =
27873df0db0SJames Feist     "xyz.openbmc_project.Control.ThermalMode";
2799c310685SBorawski.Lukasz 
2808d1b46d7Szhanghch05 inline void
2818d1b46d7Szhanghch05     asyncPopulatePid(const std::string& connection, const std::string& path,
28273df0db0SJames Feist                      const std::string& currentProfile,
28373df0db0SJames Feist                      const std::vector<std::string>& supportedProfiles,
2848d1b46d7Szhanghch05                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2855b4aa86bSJames Feist {
2865b4aa86bSJames Feist 
2875b4aa86bSJames Feist     crow::connections::systemBus->async_method_call(
28873df0db0SJames Feist         [asyncResp, currentProfile, supportedProfiles](
28973df0db0SJames Feist             const boost::system::error_code ec,
2905b4aa86bSJames Feist             const dbus::utility::ManagedObjectType& managedObj) {
2915b4aa86bSJames Feist             if (ec)
2925b4aa86bSJames Feist             {
2935b4aa86bSJames Feist                 BMCWEB_LOG_ERROR << ec;
2945b4aa86bSJames Feist                 asyncResp->res.jsonValue.clear();
295f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2965b4aa86bSJames Feist                 return;
2975b4aa86bSJames Feist             }
2985b4aa86bSJames Feist             nlohmann::json& configRoot =
2995b4aa86bSJames Feist                 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
3005b4aa86bSJames Feist             nlohmann::json& fans = configRoot["FanControllers"];
3015b4aa86bSJames Feist             fans["@odata.type"] = "#OemManager.FanControllers";
3020fda0f12SGeorge Liu             fans["@odata.id"] =
3030fda0f12SGeorge Liu                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers";
3045b4aa86bSJames Feist 
3055b4aa86bSJames Feist             nlohmann::json& pids = configRoot["PidControllers"];
3065b4aa86bSJames Feist             pids["@odata.type"] = "#OemManager.PidControllers";
3075b4aa86bSJames Feist             pids["@odata.id"] =
3085b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
3095b4aa86bSJames Feist 
310b7a08d04SJames Feist             nlohmann::json& stepwise = configRoot["StepwiseControllers"];
311b7a08d04SJames Feist             stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
312b7a08d04SJames Feist             stepwise["@odata.id"] =
313b7a08d04SJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
314b7a08d04SJames Feist 
3155b4aa86bSJames Feist             nlohmann::json& zones = configRoot["FanZones"];
3165b4aa86bSJames Feist             zones["@odata.id"] =
3175b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
3185b4aa86bSJames Feist             zones["@odata.type"] = "#OemManager.FanZones";
3195b4aa86bSJames Feist             configRoot["@odata.id"] =
3205b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
3215b4aa86bSJames Feist             configRoot["@odata.type"] = "#OemManager.Fan";
32273df0db0SJames Feist             configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
32373df0db0SJames Feist 
32473df0db0SJames Feist             if (!currentProfile.empty())
32573df0db0SJames Feist             {
32673df0db0SJames Feist                 configRoot["Profile"] = currentProfile;
32773df0db0SJames Feist             }
32873df0db0SJames Feist             BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !";
3295b4aa86bSJames Feist 
3305b4aa86bSJames Feist             for (const auto& pathPair : managedObj)
3315b4aa86bSJames Feist             {
3325b4aa86bSJames Feist                 for (const auto& intfPair : pathPair.second)
3335b4aa86bSJames Feist                 {
3345b4aa86bSJames Feist                     if (intfPair.first != pidConfigurationIface &&
335b7a08d04SJames Feist                         intfPair.first != pidZoneConfigurationIface &&
336b7a08d04SJames Feist                         intfPair.first != stepwiseConfigurationIface)
3375b4aa86bSJames Feist                     {
3385b4aa86bSJames Feist                         continue;
3395b4aa86bSJames Feist                     }
34073df0db0SJames Feist 
341711ac7a9SEd Tanous                     std::string name;
342711ac7a9SEd Tanous 
343711ac7a9SEd Tanous                     for (const std::pair<std::string,
344711ac7a9SEd Tanous                                          dbus::utility::DbusVariantType>&
345711ac7a9SEd Tanous                              propPair : intfPair.second)
346711ac7a9SEd Tanous                     {
347711ac7a9SEd Tanous                         if (propPair.first == "Name")
348711ac7a9SEd Tanous                         {
3495b4aa86bSJames Feist                             const std::string* namePtr =
350711ac7a9SEd Tanous                                 std::get_if<std::string>(&propPair.second);
3515b4aa86bSJames Feist                             if (namePtr == nullptr)
3525b4aa86bSJames Feist                             {
3535b4aa86bSJames Feist                                 BMCWEB_LOG_ERROR << "Pid Name Field illegal";
354b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
3555b4aa86bSJames Feist                                 return;
3565b4aa86bSJames Feist                             }
357db697703SWilly Tu                             name = *namePtr;
3585b4aa86bSJames Feist                             dbus::utility::escapePathForDbus(name);
359711ac7a9SEd Tanous                         }
360711ac7a9SEd Tanous                         else if (propPair.first == "Profiles")
36173df0db0SJames Feist                         {
36273df0db0SJames Feist                             const std::vector<std::string>* profiles =
36373df0db0SJames Feist                                 std::get_if<std::vector<std::string>>(
364711ac7a9SEd Tanous                                     &propPair.second);
36573df0db0SJames Feist                             if (profiles == nullptr)
36673df0db0SJames Feist                             {
367711ac7a9SEd Tanous                                 BMCWEB_LOG_ERROR
368711ac7a9SEd Tanous                                     << "Pid Profiles Field illegal";
36973df0db0SJames Feist                                 messages::internalError(asyncResp->res);
37073df0db0SJames Feist                                 return;
37173df0db0SJames Feist                             }
37273df0db0SJames Feist                             if (std::find(profiles->begin(), profiles->end(),
37373df0db0SJames Feist                                           currentProfile) == profiles->end())
37473df0db0SJames Feist                             {
37573df0db0SJames Feist                                 BMCWEB_LOG_INFO
376711ac7a9SEd Tanous                                     << name
377711ac7a9SEd Tanous                                     << " not supported in current profile";
37873df0db0SJames Feist                                 continue;
37973df0db0SJames Feist                             }
38073df0db0SJames Feist                         }
381711ac7a9SEd Tanous                     }
382b7a08d04SJames Feist                     nlohmann::json* config = nullptr;
383c33a90ecSJames Feist                     const std::string* classPtr = nullptr;
384711ac7a9SEd Tanous 
385711ac7a9SEd Tanous                     for (const std::pair<std::string,
386711ac7a9SEd Tanous                                          dbus::utility::DbusVariantType>&
387711ac7a9SEd Tanous                              propPair : intfPair.second)
388c33a90ecSJames Feist                     {
389727dc83fSLei YU                         if (propPair.first == "Class")
390711ac7a9SEd Tanous                         {
391711ac7a9SEd Tanous                             classPtr =
392711ac7a9SEd Tanous                                 std::get_if<std::string>(&propPair.second);
393711ac7a9SEd Tanous                         }
394c33a90ecSJames Feist                     }
395c33a90ecSJames Feist 
3965b4aa86bSJames Feist                     if (intfPair.first == pidZoneConfigurationIface)
3975b4aa86bSJames Feist                     {
3985b4aa86bSJames Feist                         std::string chassis;
3995b4aa86bSJames Feist                         if (!dbus::utility::getNthStringFromPath(
4005b4aa86bSJames Feist                                 pathPair.first.str, 5, chassis))
4015b4aa86bSJames Feist                         {
4025b4aa86bSJames Feist                             chassis = "#IllegalValue";
4035b4aa86bSJames Feist                         }
4045b4aa86bSJames Feist                         nlohmann::json& zone = zones[name];
4055b4aa86bSJames Feist                         zone["Chassis"] = {
4065b4aa86bSJames Feist                             {"@odata.id", "/redfish/v1/Chassis/" + chassis}};
4070fda0f12SGeorge Liu                         zone["@odata.id"] =
4080fda0f12SGeorge Liu                             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/" +
4095b4aa86bSJames Feist                             name;
4105b4aa86bSJames Feist                         zone["@odata.type"] = "#OemManager.FanZone";
411b7a08d04SJames Feist                         config = &zone;
4125b4aa86bSJames Feist                     }
4135b4aa86bSJames Feist 
414b7a08d04SJames Feist                     else if (intfPair.first == stepwiseConfigurationIface)
4155b4aa86bSJames Feist                     {
416c33a90ecSJames Feist                         if (classPtr == nullptr)
417c33a90ecSJames Feist                         {
418c33a90ecSJames Feist                             BMCWEB_LOG_ERROR << "Pid Class Field illegal";
419c33a90ecSJames Feist                             messages::internalError(asyncResp->res);
420c33a90ecSJames Feist                             return;
421c33a90ecSJames Feist                         }
422c33a90ecSJames Feist 
423b7a08d04SJames Feist                         nlohmann::json& controller = stepwise[name];
424b7a08d04SJames Feist                         config = &controller;
4255b4aa86bSJames Feist 
426b7a08d04SJames Feist                         controller["@odata.id"] =
4270fda0f12SGeorge Liu                             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers/" +
428271584abSEd Tanous                             name;
429b7a08d04SJames Feist                         controller["@odata.type"] =
430b7a08d04SJames Feist                             "#OemManager.StepwiseController";
431b7a08d04SJames Feist 
432c33a90ecSJames Feist                         controller["Direction"] = *classPtr;
4335b4aa86bSJames Feist                     }
4345b4aa86bSJames Feist 
4355b4aa86bSJames Feist                     // pid and fans are off the same configuration
436b7a08d04SJames Feist                     else if (intfPair.first == pidConfigurationIface)
4375b4aa86bSJames Feist                     {
438c33a90ecSJames Feist 
4395b4aa86bSJames Feist                         if (classPtr == nullptr)
4405b4aa86bSJames Feist                         {
4415b4aa86bSJames Feist                             BMCWEB_LOG_ERROR << "Pid Class Field illegal";
442a08b46ccSJason M. Bills                             messages::internalError(asyncResp->res);
4435b4aa86bSJames Feist                             return;
4445b4aa86bSJames Feist                         }
4455b4aa86bSJames Feist                         bool isFan = *classPtr == "fan";
4465b4aa86bSJames Feist                         nlohmann::json& element =
4475b4aa86bSJames Feist                             isFan ? fans[name] : pids[name];
448b7a08d04SJames Feist                         config = &element;
4495b4aa86bSJames Feist                         if (isFan)
4505b4aa86bSJames Feist                         {
4515b4aa86bSJames Feist                             element["@odata.id"] =
4520fda0f12SGeorge Liu                                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers/" +
453271584abSEd Tanous                                 name;
4545b4aa86bSJames Feist                             element["@odata.type"] =
4555b4aa86bSJames Feist                                 "#OemManager.FanController";
4565b4aa86bSJames Feist                         }
4575b4aa86bSJames Feist                         else
4585b4aa86bSJames Feist                         {
4595b4aa86bSJames Feist                             element["@odata.id"] =
4600fda0f12SGeorge Liu                                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers/" +
461271584abSEd Tanous                                 name;
4625b4aa86bSJames Feist                             element["@odata.type"] =
4635b4aa86bSJames Feist                                 "#OemManager.PidController";
4645b4aa86bSJames Feist                         }
465b7a08d04SJames Feist                     }
466b7a08d04SJames Feist                     else
467b7a08d04SJames Feist                     {
468b7a08d04SJames Feist                         BMCWEB_LOG_ERROR << "Unexpected configuration";
469b7a08d04SJames Feist                         messages::internalError(asyncResp->res);
470b7a08d04SJames Feist                         return;
471b7a08d04SJames Feist                     }
472b7a08d04SJames Feist 
473b7a08d04SJames Feist                     // used for making maps out of 2 vectors
474b7a08d04SJames Feist                     const std::vector<double>* keys = nullptr;
475b7a08d04SJames Feist                     const std::vector<double>* values = nullptr;
476b7a08d04SJames Feist 
477b7a08d04SJames Feist                     for (const auto& propertyPair : intfPair.second)
478b7a08d04SJames Feist                     {
479b7a08d04SJames Feist                         if (propertyPair.first == "Type" ||
480b7a08d04SJames Feist                             propertyPair.first == "Class" ||
481b7a08d04SJames Feist                             propertyPair.first == "Name")
482b7a08d04SJames Feist                         {
483b7a08d04SJames Feist                             continue;
484b7a08d04SJames Feist                         }
485b7a08d04SJames Feist 
486b7a08d04SJames Feist                         // zones
487b7a08d04SJames Feist                         if (intfPair.first == pidZoneConfigurationIface)
488b7a08d04SJames Feist                         {
489b7a08d04SJames Feist                             const double* ptr =
490abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
491b7a08d04SJames Feist                             if (ptr == nullptr)
492b7a08d04SJames Feist                             {
493b7a08d04SJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
494b7a08d04SJames Feist                                                  << propertyPair.first;
495b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
496b7a08d04SJames Feist                                 return;
497b7a08d04SJames Feist                             }
498b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
499b7a08d04SJames Feist                         }
500b7a08d04SJames Feist 
501b7a08d04SJames Feist                         if (intfPair.first == stepwiseConfigurationIface)
502b7a08d04SJames Feist                         {
503b7a08d04SJames Feist                             if (propertyPair.first == "Reading" ||
504b7a08d04SJames Feist                                 propertyPair.first == "Output")
505b7a08d04SJames Feist                             {
506b7a08d04SJames Feist                                 const std::vector<double>* ptr =
507abf2add6SEd Tanous                                     std::get_if<std::vector<double>>(
508b7a08d04SJames Feist                                         &propertyPair.second);
509b7a08d04SJames Feist 
510b7a08d04SJames Feist                                 if (ptr == nullptr)
511b7a08d04SJames Feist                                 {
512b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
513b7a08d04SJames Feist                                                      << propertyPair.first;
514b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
515b7a08d04SJames Feist                                     return;
516b7a08d04SJames Feist                                 }
517b7a08d04SJames Feist 
518b7a08d04SJames Feist                                 if (propertyPair.first == "Reading")
519b7a08d04SJames Feist                                 {
520b7a08d04SJames Feist                                     keys = ptr;
521b7a08d04SJames Feist                                 }
522b7a08d04SJames Feist                                 else
523b7a08d04SJames Feist                                 {
524b7a08d04SJames Feist                                     values = ptr;
525b7a08d04SJames Feist                                 }
526e662eae8SEd Tanous                                 if (keys != nullptr && values != nullptr)
527b7a08d04SJames Feist                                 {
528b7a08d04SJames Feist                                     if (keys->size() != values->size())
529b7a08d04SJames Feist                                     {
530b7a08d04SJames Feist                                         BMCWEB_LOG_ERROR
5310fda0f12SGeorge Liu                                             << "Reading and Output size don't match ";
532b7a08d04SJames Feist                                         messages::internalError(asyncResp->res);
533b7a08d04SJames Feist                                         return;
534b7a08d04SJames Feist                                     }
535b7a08d04SJames Feist                                     nlohmann::json& steps = (*config)["Steps"];
536b7a08d04SJames Feist                                     steps = nlohmann::json::array();
537b7a08d04SJames Feist                                     for (size_t ii = 0; ii < keys->size(); ii++)
538b7a08d04SJames Feist                                     {
539*1476687dSEd Tanous                                         nlohmann::json::object_t step;
540*1476687dSEd Tanous                                         step["Target"] = (*keys)[ii];
541*1476687dSEd Tanous                                         step["Output"] = (*values)[ii];
542*1476687dSEd Tanous                                         steps.push_back(std::move(step));
543b7a08d04SJames Feist                                     }
544b7a08d04SJames Feist                                 }
545b7a08d04SJames Feist                             }
546b7a08d04SJames Feist                             if (propertyPair.first == "NegativeHysteresis" ||
547b7a08d04SJames Feist                                 propertyPair.first == "PositiveHysteresis")
548b7a08d04SJames Feist                             {
549b7a08d04SJames Feist                                 const double* ptr =
550abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
551b7a08d04SJames Feist                                 if (ptr == nullptr)
552b7a08d04SJames Feist                                 {
553b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
554b7a08d04SJames Feist                                                      << propertyPair.first;
555b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
556b7a08d04SJames Feist                                     return;
557b7a08d04SJames Feist                                 }
558b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
559b7a08d04SJames Feist                             }
560b7a08d04SJames Feist                         }
561b7a08d04SJames Feist 
562b7a08d04SJames Feist                         // pid and fans are off the same configuration
563b7a08d04SJames Feist                         if (intfPair.first == pidConfigurationIface ||
564b7a08d04SJames Feist                             intfPair.first == stepwiseConfigurationIface)
565b7a08d04SJames Feist                         {
5665b4aa86bSJames Feist 
5675b4aa86bSJames Feist                             if (propertyPair.first == "Zones")
5685b4aa86bSJames Feist                             {
5695b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
570abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
5711b6b96c5SEd Tanous                                         &propertyPair.second);
5725b4aa86bSJames Feist 
5735b4aa86bSJames Feist                                 if (inputs == nullptr)
5745b4aa86bSJames Feist                                 {
5755b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR
5765b4aa86bSJames Feist                                         << "Zones Pid Field Illegal";
577a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
5785b4aa86bSJames Feist                                     return;
5795b4aa86bSJames Feist                                 }
580b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
5815b4aa86bSJames Feist                                 data = nlohmann::json::array();
5825b4aa86bSJames Feist                                 for (std::string itemCopy : *inputs)
5835b4aa86bSJames Feist                                 {
5845b4aa86bSJames Feist                                     dbus::utility::escapePathForDbus(itemCopy);
585*1476687dSEd Tanous                                     nlohmann::json::object_t input;
586*1476687dSEd Tanous                                     input["@odata.id"] =
5870fda0f12SGeorge Liu                                         "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/" +
588*1476687dSEd Tanous                                         itemCopy;
589*1476687dSEd Tanous                                     data.push_back(std::move(input));
5905b4aa86bSJames Feist                                 }
5915b4aa86bSJames Feist                             }
5925b4aa86bSJames Feist                             // todo(james): may never happen, but this
5935b4aa86bSJames Feist                             // assumes configuration data referenced in the
5945b4aa86bSJames Feist                             // PID config is provided by the same daemon, we
5955b4aa86bSJames Feist                             // could add another loop to cover all cases,
5965b4aa86bSJames Feist                             // but I'm okay kicking this can down the road a
5975b4aa86bSJames Feist                             // bit
5985b4aa86bSJames Feist 
5995b4aa86bSJames Feist                             else if (propertyPair.first == "Inputs" ||
6005b4aa86bSJames Feist                                      propertyPair.first == "Outputs")
6015b4aa86bSJames Feist                             {
602b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
6035b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
604abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
6051b6b96c5SEd Tanous                                         &propertyPair.second);
6065b4aa86bSJames Feist 
6075b4aa86bSJames Feist                                 if (inputs == nullptr)
6085b4aa86bSJames Feist                                 {
6095b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
6105b4aa86bSJames Feist                                                      << propertyPair.first;
611f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
6125b4aa86bSJames Feist                                     return;
6135b4aa86bSJames Feist                                 }
6145b4aa86bSJames Feist                                 data = *inputs;
615b943aaefSJames Feist                             }
616b943aaefSJames Feist                             else if (propertyPair.first == "SetPointOffset")
617b943aaefSJames Feist                             {
618b943aaefSJames Feist                                 const std::string* ptr =
619b943aaefSJames Feist                                     std::get_if<std::string>(
620b943aaefSJames Feist                                         &propertyPair.second);
621b943aaefSJames Feist 
622b943aaefSJames Feist                                 if (ptr == nullptr)
623b943aaefSJames Feist                                 {
624b943aaefSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
625b943aaefSJames Feist                                                      << propertyPair.first;
626b943aaefSJames Feist                                     messages::internalError(asyncResp->res);
627b943aaefSJames Feist                                     return;
628b943aaefSJames Feist                                 }
629b943aaefSJames Feist                                 // translate from dbus to redfish
630b943aaefSJames Feist                                 if (*ptr == "WarningHigh")
631b943aaefSJames Feist                                 {
632b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
633b943aaefSJames Feist                                         "UpperThresholdNonCritical";
634b943aaefSJames Feist                                 }
635b943aaefSJames Feist                                 else if (*ptr == "WarningLow")
636b943aaefSJames Feist                                 {
637b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
638b943aaefSJames Feist                                         "LowerThresholdNonCritical";
639b943aaefSJames Feist                                 }
640b943aaefSJames Feist                                 else if (*ptr == "CriticalHigh")
641b943aaefSJames Feist                                 {
642b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
643b943aaefSJames Feist                                         "UpperThresholdCritical";
644b943aaefSJames Feist                                 }
645b943aaefSJames Feist                                 else if (*ptr == "CriticalLow")
646b943aaefSJames Feist                                 {
647b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
648b943aaefSJames Feist                                         "LowerThresholdCritical";
649b943aaefSJames Feist                                 }
650b943aaefSJames Feist                                 else
651b943aaefSJames Feist                                 {
652b943aaefSJames Feist                                     BMCWEB_LOG_ERROR << "Value Illegal "
653b943aaefSJames Feist                                                      << *ptr;
654b943aaefSJames Feist                                     messages::internalError(asyncResp->res);
655b943aaefSJames Feist                                     return;
656b943aaefSJames Feist                                 }
657b943aaefSJames Feist                             }
658b943aaefSJames Feist                             // doubles
6595b4aa86bSJames Feist                             else if (propertyPair.first ==
6605b4aa86bSJames Feist                                          "FFGainCoefficient" ||
6615b4aa86bSJames Feist                                      propertyPair.first == "FFOffCoefficient" ||
6625b4aa86bSJames Feist                                      propertyPair.first == "ICoefficient" ||
6635b4aa86bSJames Feist                                      propertyPair.first == "ILimitMax" ||
6645b4aa86bSJames Feist                                      propertyPair.first == "ILimitMin" ||
665aad1a257SJames Feist                                      propertyPair.first ==
666aad1a257SJames Feist                                          "PositiveHysteresis" ||
667aad1a257SJames Feist                                      propertyPair.first ==
668aad1a257SJames Feist                                          "NegativeHysteresis" ||
6695b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMax" ||
6705b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMin" ||
6715b4aa86bSJames Feist                                      propertyPair.first == "PCoefficient" ||
6727625cb81SJames Feist                                      propertyPair.first == "SetPoint" ||
6735b4aa86bSJames Feist                                      propertyPair.first == "SlewNeg" ||
6745b4aa86bSJames Feist                                      propertyPair.first == "SlewPos")
6755b4aa86bSJames Feist                             {
6765b4aa86bSJames Feist                                 const double* ptr =
677abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
6785b4aa86bSJames Feist                                 if (ptr == nullptr)
6795b4aa86bSJames Feist                                 {
6805b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
6815b4aa86bSJames Feist                                                      << propertyPair.first;
682f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
6835b4aa86bSJames Feist                                     return;
6845b4aa86bSJames Feist                                 }
685b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
6865b4aa86bSJames Feist                             }
6875b4aa86bSJames Feist                         }
6885b4aa86bSJames Feist                     }
6895b4aa86bSJames Feist                 }
6905b4aa86bSJames Feist             }
6915b4aa86bSJames Feist         },
6925b4aa86bSJames Feist         connection, path, objectManagerIface, "GetManagedObjects");
6935b4aa86bSJames Feist }
694ca537928SJennifer Lee 
69583ff9ab6SJames Feist enum class CreatePIDRet
69683ff9ab6SJames Feist {
69783ff9ab6SJames Feist     fail,
69883ff9ab6SJames Feist     del,
69983ff9ab6SJames Feist     patch
70083ff9ab6SJames Feist };
70183ff9ab6SJames Feist 
7028d1b46d7Szhanghch05 inline bool
7038d1b46d7Szhanghch05     getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
7045f2caaefSJames Feist                         std::vector<nlohmann::json>& config,
7055f2caaefSJames Feist                         std::vector<std::string>& zones)
7065f2caaefSJames Feist {
707b6baeaa4SJames Feist     if (config.empty())
708b6baeaa4SJames Feist     {
709b6baeaa4SJames Feist         BMCWEB_LOG_ERROR << "Empty Zones";
7101668ce6dSEd Tanous         messages::propertyValueFormatError(response->res, "[]", "Zones");
711b6baeaa4SJames Feist         return false;
712b6baeaa4SJames Feist     }
7135f2caaefSJames Feist     for (auto& odata : config)
7145f2caaefSJames Feist     {
7155f2caaefSJames Feist         std::string path;
7165f2caaefSJames Feist         if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
7175f2caaefSJames Feist                                           path))
7185f2caaefSJames Feist         {
7195f2caaefSJames Feist             return false;
7205f2caaefSJames Feist         }
7215f2caaefSJames Feist         std::string input;
72261adbda3SJames Feist 
72361adbda3SJames Feist         // 8 below comes from
72461adbda3SJames Feist         // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
72561adbda3SJames Feist         //     0    1     2      3    4    5      6     7      8
72661adbda3SJames Feist         if (!dbus::utility::getNthStringFromPath(path, 8, input))
7275f2caaefSJames Feist         {
7285f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Got invalid path " << path;
7295f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Illegal Type Zones";
7305f2caaefSJames Feist             messages::propertyValueFormatError(response->res, odata.dump(),
7315f2caaefSJames Feist                                                "Zones");
7325f2caaefSJames Feist             return false;
7335f2caaefSJames Feist         }
7345f2caaefSJames Feist         boost::replace_all(input, "_", " ");
7355f2caaefSJames Feist         zones.emplace_back(std::move(input));
7365f2caaefSJames Feist     }
7375f2caaefSJames Feist     return true;
7385f2caaefSJames Feist }
7395f2caaefSJames Feist 
740711ac7a9SEd Tanous inline const dbus::utility::ManagedObjectType::value_type*
74173df0db0SJames Feist     findChassis(const dbus::utility::ManagedObjectType& managedObj,
742b6baeaa4SJames Feist                 const std::string& value, std::string& chassis)
743b6baeaa4SJames Feist {
744b6baeaa4SJames Feist     BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
745b6baeaa4SJames Feist 
746b6baeaa4SJames Feist     std::string escaped = boost::replace_all_copy(value, " ", "_");
747b6baeaa4SJames Feist     escaped = "/" + escaped;
748b6baeaa4SJames Feist     auto it = std::find_if(
749b6baeaa4SJames Feist         managedObj.begin(), managedObj.end(), [&escaped](const auto& obj) {
750b6baeaa4SJames Feist             if (boost::algorithm::ends_with(obj.first.str, escaped))
751b6baeaa4SJames Feist             {
752b6baeaa4SJames Feist                 BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
753b6baeaa4SJames Feist                 return true;
754b6baeaa4SJames Feist             }
755b6baeaa4SJames Feist             return false;
756b6baeaa4SJames Feist         });
757b6baeaa4SJames Feist 
758b6baeaa4SJames Feist     if (it == managedObj.end())
759b6baeaa4SJames Feist     {
76073df0db0SJames Feist         return nullptr;
761b6baeaa4SJames Feist     }
762b6baeaa4SJames Feist     // 5 comes from <chassis-name> being the 5th element
763b6baeaa4SJames Feist     // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
76473df0db0SJames Feist     if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
76573df0db0SJames Feist     {
76673df0db0SJames Feist         return &(*it);
76773df0db0SJames Feist     }
76873df0db0SJames Feist 
76973df0db0SJames Feist     return nullptr;
770b6baeaa4SJames Feist }
771b6baeaa4SJames Feist 
77223a21a1cSEd Tanous inline CreatePIDRet createPidInterface(
7738d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
774b5a76932SEd Tanous     const nlohmann::json::iterator& it, const std::string& path,
77583ff9ab6SJames Feist     const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
776b9d36b47SEd Tanous     dbus::utility::DBusPropertiesMap& output, std::string& chassis,
777b9d36b47SEd Tanous     const std::string& profile)
77883ff9ab6SJames Feist {
77983ff9ab6SJames Feist 
7805f2caaefSJames Feist     // common deleter
781b6baeaa4SJames Feist     if (it.value() == nullptr)
7825f2caaefSJames Feist     {
7835f2caaefSJames Feist         std::string iface;
7845f2caaefSJames Feist         if (type == "PidControllers" || type == "FanControllers")
7855f2caaefSJames Feist         {
7865f2caaefSJames Feist             iface = pidConfigurationIface;
7875f2caaefSJames Feist         }
7885f2caaefSJames Feist         else if (type == "FanZones")
7895f2caaefSJames Feist         {
7905f2caaefSJames Feist             iface = pidZoneConfigurationIface;
7915f2caaefSJames Feist         }
7925f2caaefSJames Feist         else if (type == "StepwiseControllers")
7935f2caaefSJames Feist         {
7945f2caaefSJames Feist             iface = stepwiseConfigurationIface;
7955f2caaefSJames Feist         }
7965f2caaefSJames Feist         else
7975f2caaefSJames Feist         {
798a0744d38SGunnar Mills             BMCWEB_LOG_ERROR << "Illegal Type " << type;
7995f2caaefSJames Feist             messages::propertyUnknown(response->res, type);
8005f2caaefSJames Feist             return CreatePIDRet::fail;
8015f2caaefSJames Feist         }
8026ee7f774SJames Feist 
8036ee7f774SJames Feist         BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
8045f2caaefSJames Feist         // delete interface
8055f2caaefSJames Feist         crow::connections::systemBus->async_method_call(
8065f2caaefSJames Feist             [response, path](const boost::system::error_code ec) {
8075f2caaefSJames Feist                 if (ec)
8085f2caaefSJames Feist                 {
8095f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
8105f2caaefSJames Feist                     messages::internalError(response->res);
811b6baeaa4SJames Feist                     return;
8125f2caaefSJames Feist                 }
813b6baeaa4SJames Feist                 messages::success(response->res);
8145f2caaefSJames Feist             },
8155f2caaefSJames Feist             "xyz.openbmc_project.EntityManager", path, iface, "Delete");
8165f2caaefSJames Feist         return CreatePIDRet::del;
8175f2caaefSJames Feist     }
8185f2caaefSJames Feist 
819711ac7a9SEd Tanous     const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
820b6baeaa4SJames Feist     if (!createNewObject)
821b6baeaa4SJames Feist     {
822b6baeaa4SJames Feist         // if we aren't creating a new object, we should be able to find it on
823b6baeaa4SJames Feist         // d-bus
82473df0db0SJames Feist         managedItem = findChassis(managedObj, it.key(), chassis);
82573df0db0SJames Feist         if (managedItem == nullptr)
826b6baeaa4SJames Feist         {
827b6baeaa4SJames Feist             BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
828ace85d60SEd Tanous             messages::invalidObject(response->res,
829ace85d60SEd Tanous                                     crow::utility::urlFromPieces(
830ace85d60SEd Tanous                                         "redfish", "v1", "Chassis", chassis));
831b6baeaa4SJames Feist             return CreatePIDRet::fail;
832b6baeaa4SJames Feist         }
833b6baeaa4SJames Feist     }
834b6baeaa4SJames Feist 
83526f6976fSEd Tanous     if (!profile.empty() &&
83673df0db0SJames Feist         (type == "PidControllers" || type == "FanControllers" ||
83773df0db0SJames Feist          type == "StepwiseControllers"))
83873df0db0SJames Feist     {
83973df0db0SJames Feist         if (managedItem == nullptr)
84073df0db0SJames Feist         {
841b9d36b47SEd Tanous             output.emplace_back("Profiles", std::vector<std::string>{profile});
84273df0db0SJames Feist         }
84373df0db0SJames Feist         else
84473df0db0SJames Feist         {
84573df0db0SJames Feist             std::string interface;
84673df0db0SJames Feist             if (type == "StepwiseControllers")
84773df0db0SJames Feist             {
84873df0db0SJames Feist                 interface = stepwiseConfigurationIface;
84973df0db0SJames Feist             }
85073df0db0SJames Feist             else
85173df0db0SJames Feist             {
85273df0db0SJames Feist                 interface = pidConfigurationIface;
85373df0db0SJames Feist             }
854711ac7a9SEd Tanous             bool ifaceFound = false;
855711ac7a9SEd Tanous             for (const auto& iface : managedItem->second)
856711ac7a9SEd Tanous             {
857711ac7a9SEd Tanous                 if (iface.first == interface)
858711ac7a9SEd Tanous                 {
859711ac7a9SEd Tanous                     ifaceFound = true;
860711ac7a9SEd Tanous                     for (const auto& prop : iface.second)
861711ac7a9SEd Tanous                     {
862711ac7a9SEd Tanous                         if (prop.first == "Profiles")
863711ac7a9SEd Tanous                         {
864711ac7a9SEd Tanous                             const std::vector<std::string>* curProfiles =
865711ac7a9SEd Tanous                                 std::get_if<std::vector<std::string>>(
866711ac7a9SEd Tanous                                     &(prop.second));
867711ac7a9SEd Tanous                             if (curProfiles == nullptr)
868711ac7a9SEd Tanous                             {
869711ac7a9SEd Tanous                                 BMCWEB_LOG_ERROR
870711ac7a9SEd Tanous                                     << "Illegal profiles in managed object";
871711ac7a9SEd Tanous                                 messages::internalError(response->res);
872711ac7a9SEd Tanous                                 return CreatePIDRet::fail;
873711ac7a9SEd Tanous                             }
874711ac7a9SEd Tanous                             if (std::find(curProfiles->begin(),
875711ac7a9SEd Tanous                                           curProfiles->end(),
876711ac7a9SEd Tanous                                           profile) == curProfiles->end())
877711ac7a9SEd Tanous                             {
878711ac7a9SEd Tanous                                 std::vector<std::string> newProfiles =
879711ac7a9SEd Tanous                                     *curProfiles;
880711ac7a9SEd Tanous                                 newProfiles.push_back(profile);
881b9d36b47SEd Tanous                                 output.emplace_back("Profiles", newProfiles);
882711ac7a9SEd Tanous                             }
883711ac7a9SEd Tanous                         }
884711ac7a9SEd Tanous                     }
885711ac7a9SEd Tanous                 }
886711ac7a9SEd Tanous             }
887711ac7a9SEd Tanous 
888711ac7a9SEd Tanous             if (!ifaceFound)
88973df0db0SJames Feist             {
89073df0db0SJames Feist                 BMCWEB_LOG_ERROR
89173df0db0SJames Feist                     << "Failed to find interface in managed object";
89273df0db0SJames Feist                 messages::internalError(response->res);
89373df0db0SJames Feist                 return CreatePIDRet::fail;
89473df0db0SJames Feist             }
89573df0db0SJames Feist         }
89673df0db0SJames Feist     }
89773df0db0SJames Feist 
89883ff9ab6SJames Feist     if (type == "PidControllers" || type == "FanControllers")
89983ff9ab6SJames Feist     {
90083ff9ab6SJames Feist         if (createNewObject)
90183ff9ab6SJames Feist         {
902b9d36b47SEd Tanous             output.emplace_back("Class",
903b9d36b47SEd Tanous                                 type == "PidControllers" ? "temp" : "fan");
904b9d36b47SEd Tanous             output.emplace_back("Type", "Pid");
90583ff9ab6SJames Feist         }
9065f2caaefSJames Feist 
9075f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
9085f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
9095f2caaefSJames Feist         std::optional<std::vector<std::string>> outputs;
9105f2caaefSJames Feist         std::map<std::string, std::optional<double>> doubles;
911b943aaefSJames Feist         std::optional<std::string> setpointOffset;
9125f2caaefSJames Feist         if (!redfish::json_util::readJson(
913b6baeaa4SJames Feist                 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
9145f2caaefSJames Feist                 "Zones", zones, "FFGainCoefficient",
9155f2caaefSJames Feist                 doubles["FFGainCoefficient"], "FFOffCoefficient",
9165f2caaefSJames Feist                 doubles["FFOffCoefficient"], "ICoefficient",
9175f2caaefSJames Feist                 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
9185f2caaefSJames Feist                 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
9195f2caaefSJames Feist                 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
9205f2caaefSJames Feist                 "PCoefficient", doubles["PCoefficient"], "SetPoint",
921b943aaefSJames Feist                 doubles["SetPoint"], "SetPointOffset", setpointOffset,
922b943aaefSJames Feist                 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
923b943aaefSJames Feist                 "PositiveHysteresis", doubles["PositiveHysteresis"],
924b943aaefSJames Feist                 "NegativeHysteresis", doubles["NegativeHysteresis"]))
92583ff9ab6SJames Feist         {
92671f52d96SEd Tanous             BMCWEB_LOG_ERROR
92771f52d96SEd Tanous                 << "Illegal Property "
92871f52d96SEd Tanous                 << it.value().dump(2, ' ', true,
92971f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace);
9305f2caaefSJames Feist             return CreatePIDRet::fail;
93183ff9ab6SJames Feist         }
9325f2caaefSJames Feist         if (zones)
9335f2caaefSJames Feist         {
9345f2caaefSJames Feist             std::vector<std::string> zonesStr;
9355f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStr))
9365f2caaefSJames Feist             {
937a0744d38SGunnar Mills                 BMCWEB_LOG_ERROR << "Illegal Zones";
9385f2caaefSJames Feist                 return CreatePIDRet::fail;
9395f2caaefSJames Feist             }
940b6baeaa4SJames Feist             if (chassis.empty() &&
941e662eae8SEd Tanous                 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
942b6baeaa4SJames Feist             {
943b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
944ace85d60SEd Tanous                 messages::invalidObject(
945ace85d60SEd Tanous                     response->res, crow::utility::urlFromPieces(
946ace85d60SEd Tanous                                        "redfish", "v1", "Chassis", chassis));
947b6baeaa4SJames Feist                 return CreatePIDRet::fail;
948b6baeaa4SJames Feist             }
949b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStr));
9505f2caaefSJames Feist         }
9515f2caaefSJames Feist         if (inputs || outputs)
9525f2caaefSJames Feist         {
9535f2caaefSJames Feist             std::array<std::optional<std::vector<std::string>>*, 2> containers =
9545f2caaefSJames Feist                 {&inputs, &outputs};
9555f2caaefSJames Feist             size_t index = 0;
9565f2caaefSJames Feist             for (const auto& containerPtr : containers)
9575f2caaefSJames Feist             {
9585f2caaefSJames Feist                 std::optional<std::vector<std::string>>& container =
9595f2caaefSJames Feist                     *containerPtr;
9605f2caaefSJames Feist                 if (!container)
9615f2caaefSJames Feist                 {
9625f2caaefSJames Feist                     index++;
9635f2caaefSJames Feist                     continue;
96483ff9ab6SJames Feist                 }
96583ff9ab6SJames Feist 
9665f2caaefSJames Feist                 for (std::string& value : *container)
96783ff9ab6SJames Feist                 {
9685f2caaefSJames Feist                     boost::replace_all(value, "_", " ");
96983ff9ab6SJames Feist                 }
9705f2caaefSJames Feist                 std::string key;
9715f2caaefSJames Feist                 if (index == 0)
9725f2caaefSJames Feist                 {
9735f2caaefSJames Feist                     key = "Inputs";
9745f2caaefSJames Feist                 }
9755f2caaefSJames Feist                 else
9765f2caaefSJames Feist                 {
9775f2caaefSJames Feist                     key = "Outputs";
9785f2caaefSJames Feist                 }
979b9d36b47SEd Tanous                 output.emplace_back(key, *container);
9805f2caaefSJames Feist                 index++;
9815f2caaefSJames Feist             }
98283ff9ab6SJames Feist         }
98383ff9ab6SJames Feist 
984b943aaefSJames Feist         if (setpointOffset)
985b943aaefSJames Feist         {
986b943aaefSJames Feist             // translate between redfish and dbus names
987b943aaefSJames Feist             if (*setpointOffset == "UpperThresholdNonCritical")
988b943aaefSJames Feist             {
989b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningLow");
990b943aaefSJames Feist             }
991b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdNonCritical")
992b943aaefSJames Feist             {
993b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningHigh");
994b943aaefSJames Feist             }
995b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdCritical")
996b943aaefSJames Feist             {
997b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalLow");
998b943aaefSJames Feist             }
999b943aaefSJames Feist             else if (*setpointOffset == "UpperThresholdCritical")
1000b943aaefSJames Feist             {
1001b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalHigh");
1002b943aaefSJames Feist             }
1003b943aaefSJames Feist             else
1004b943aaefSJames Feist             {
1005b943aaefSJames Feist                 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
1006b943aaefSJames Feist                                  << *setpointOffset;
1007ace85d60SEd Tanous                 messages::propertyValueNotInList(response->res, it.key(),
1008ace85d60SEd Tanous                                                  "SetPointOffset");
1009b943aaefSJames Feist                 return CreatePIDRet::fail;
1010b943aaefSJames Feist             }
1011b943aaefSJames Feist         }
1012b943aaefSJames Feist 
101383ff9ab6SJames Feist         // doubles
10145f2caaefSJames Feist         for (const auto& pairs : doubles)
101583ff9ab6SJames Feist         {
10165f2caaefSJames Feist             if (!pairs.second)
101783ff9ab6SJames Feist             {
10185f2caaefSJames Feist                 continue;
101983ff9ab6SJames Feist             }
10205f2caaefSJames Feist             BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
1021b9d36b47SEd Tanous             output.emplace_back(pairs.first, *pairs.second);
10225f2caaefSJames Feist         }
102383ff9ab6SJames Feist     }
102483ff9ab6SJames Feist 
102583ff9ab6SJames Feist     else if (type == "FanZones")
102683ff9ab6SJames Feist     {
1027b9d36b47SEd Tanous         output.emplace_back("Type", "Pid.Zone");
102883ff9ab6SJames Feist 
10295f2caaefSJames Feist         std::optional<nlohmann::json> chassisContainer;
10305f2caaefSJames Feist         std::optional<double> failSafePercent;
1031d3ec07f8SJames Feist         std::optional<double> minThermalOutput;
1032b6baeaa4SJames Feist         if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
10335f2caaefSJames Feist                                           chassisContainer, "FailSafePercent",
1034d3ec07f8SJames Feist                                           failSafePercent, "MinThermalOutput",
1035d3ec07f8SJames Feist                                           minThermalOutput))
103683ff9ab6SJames Feist         {
103771f52d96SEd Tanous             BMCWEB_LOG_ERROR
103871f52d96SEd Tanous                 << "Illegal Property "
103971f52d96SEd Tanous                 << it.value().dump(2, ' ', true,
104071f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace);
104183ff9ab6SJames Feist             return CreatePIDRet::fail;
104283ff9ab6SJames Feist         }
10435f2caaefSJames Feist 
10445f2caaefSJames Feist         if (chassisContainer)
104583ff9ab6SJames Feist         {
10465f2caaefSJames Feist 
10475f2caaefSJames Feist             std::string chassisId;
10485f2caaefSJames Feist             if (!redfish::json_util::readJson(*chassisContainer, response->res,
10495f2caaefSJames Feist                                               "@odata.id", chassisId))
10505f2caaefSJames Feist             {
105171f52d96SEd Tanous                 BMCWEB_LOG_ERROR
105271f52d96SEd Tanous                     << "Illegal Property "
105371f52d96SEd Tanous                     << chassisContainer->dump(
105471f52d96SEd Tanous                            2, ' ', true,
105571f52d96SEd Tanous                            nlohmann::json::error_handler_t::replace);
105683ff9ab6SJames Feist                 return CreatePIDRet::fail;
105783ff9ab6SJames Feist             }
105883ff9ab6SJames Feist 
1059717794d5SAppaRao Puli             // /redfish/v1/chassis/chassis_name/
10605f2caaefSJames Feist             if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
106183ff9ab6SJames Feist             {
10625f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
1063ace85d60SEd Tanous                 messages::invalidObject(
1064ace85d60SEd Tanous                     response->res, crow::utility::urlFromPieces(
1065ace85d60SEd Tanous                                        "redfish", "v1", "Chassis", chassisId));
106683ff9ab6SJames Feist                 return CreatePIDRet::fail;
106783ff9ab6SJames Feist             }
106883ff9ab6SJames Feist         }
1069d3ec07f8SJames Feist         if (minThermalOutput)
107083ff9ab6SJames Feist         {
1071b9d36b47SEd Tanous             output.emplace_back("MinThermalOutput", *minThermalOutput);
10725f2caaefSJames Feist         }
10735f2caaefSJames Feist         if (failSafePercent)
107483ff9ab6SJames Feist         {
1075b9d36b47SEd Tanous             output.emplace_back("FailSafePercent", *failSafePercent);
10765f2caaefSJames Feist         }
10775f2caaefSJames Feist     }
10785f2caaefSJames Feist     else if (type == "StepwiseControllers")
10795f2caaefSJames Feist     {
1080b9d36b47SEd Tanous         output.emplace_back("Type", "Stepwise");
10815f2caaefSJames Feist 
10825f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
10835f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> steps;
10845f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
10855f2caaefSJames Feist         std::optional<double> positiveHysteresis;
10865f2caaefSJames Feist         std::optional<double> negativeHysteresis;
1087c33a90ecSJames Feist         std::optional<std::string> direction; // upper clipping curve vs lower
10885f2caaefSJames Feist         if (!redfish::json_util::readJson(
1089b6baeaa4SJames Feist                 it.value(), response->res, "Zones", zones, "Steps", steps,
1090b6baeaa4SJames Feist                 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
1091c33a90ecSJames Feist                 "NegativeHysteresis", negativeHysteresis, "Direction",
1092c33a90ecSJames Feist                 direction))
10935f2caaefSJames Feist         {
109471f52d96SEd Tanous             BMCWEB_LOG_ERROR
109571f52d96SEd Tanous                 << "Illegal Property "
109671f52d96SEd Tanous                 << it.value().dump(2, ' ', true,
109771f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace);
109883ff9ab6SJames Feist             return CreatePIDRet::fail;
109983ff9ab6SJames Feist         }
11005f2caaefSJames Feist 
11015f2caaefSJames Feist         if (zones)
110283ff9ab6SJames Feist         {
1103b6baeaa4SJames Feist             std::vector<std::string> zonesStrs;
1104b6baeaa4SJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStrs))
11055f2caaefSJames Feist             {
1106a0744d38SGunnar Mills                 BMCWEB_LOG_ERROR << "Illegal Zones";
110783ff9ab6SJames Feist                 return CreatePIDRet::fail;
110883ff9ab6SJames Feist             }
1109b6baeaa4SJames Feist             if (chassis.empty() &&
1110e662eae8SEd Tanous                 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
1111b6baeaa4SJames Feist             {
1112b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
1113ace85d60SEd Tanous                 messages::invalidObject(
1114ace85d60SEd Tanous                     response->res, crow::utility::urlFromPieces(
1115ace85d60SEd Tanous                                        "redfish", "v1", "Chassis", chassis));
1116b6baeaa4SJames Feist                 return CreatePIDRet::fail;
1117b6baeaa4SJames Feist             }
1118b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStrs));
11195f2caaefSJames Feist         }
11205f2caaefSJames Feist         if (steps)
11215f2caaefSJames Feist         {
11225f2caaefSJames Feist             std::vector<double> readings;
11235f2caaefSJames Feist             std::vector<double> outputs;
11245f2caaefSJames Feist             for (auto& step : *steps)
11255f2caaefSJames Feist             {
1126543f4400SEd Tanous                 double target = 0.0;
1127543f4400SEd Tanous                 double out = 0.0;
11285f2caaefSJames Feist 
11295f2caaefSJames Feist                 if (!redfish::json_util::readJson(step, response->res, "Target",
113023a21a1cSEd Tanous                                                   target, "Output", out))
11315f2caaefSJames Feist                 {
113271f52d96SEd Tanous                     BMCWEB_LOG_ERROR
113371f52d96SEd Tanous                         << "Illegal Property "
113471f52d96SEd Tanous                         << it.value().dump(
113571f52d96SEd Tanous                                2, ' ', true,
113671f52d96SEd Tanous                                nlohmann::json::error_handler_t::replace);
11375f2caaefSJames Feist                     return CreatePIDRet::fail;
11385f2caaefSJames Feist                 }
11395f2caaefSJames Feist                 readings.emplace_back(target);
114023a21a1cSEd Tanous                 outputs.emplace_back(out);
11415f2caaefSJames Feist             }
1142b9d36b47SEd Tanous             output.emplace_back("Reading", std::move(readings));
1143b9d36b47SEd Tanous             output.emplace_back("Output", std::move(outputs));
11445f2caaefSJames Feist         }
11455f2caaefSJames Feist         if (inputs)
11465f2caaefSJames Feist         {
11475f2caaefSJames Feist             for (std::string& value : *inputs)
11485f2caaefSJames Feist             {
11495f2caaefSJames Feist                 boost::replace_all(value, "_", " ");
11505f2caaefSJames Feist             }
1151b9d36b47SEd Tanous             output.emplace_back("Inputs", std::move(*inputs));
11525f2caaefSJames Feist         }
11535f2caaefSJames Feist         if (negativeHysteresis)
11545f2caaefSJames Feist         {
1155b9d36b47SEd Tanous             output.emplace_back("NegativeHysteresis", *negativeHysteresis);
11565f2caaefSJames Feist         }
11575f2caaefSJames Feist         if (positiveHysteresis)
11585f2caaefSJames Feist         {
1159b9d36b47SEd Tanous             output.emplace_back("PositiveHysteresis", *positiveHysteresis);
116083ff9ab6SJames Feist         }
1161c33a90ecSJames Feist         if (direction)
1162c33a90ecSJames Feist         {
1163c33a90ecSJames Feist             constexpr const std::array<const char*, 2> allowedDirections = {
1164c33a90ecSJames Feist                 "Ceiling", "Floor"};
1165c33a90ecSJames Feist             if (std::find(allowedDirections.begin(), allowedDirections.end(),
1166c33a90ecSJames Feist                           *direction) == allowedDirections.end())
1167c33a90ecSJames Feist             {
1168c33a90ecSJames Feist                 messages::propertyValueTypeError(response->res, "Direction",
1169c33a90ecSJames Feist                                                  *direction);
1170c33a90ecSJames Feist                 return CreatePIDRet::fail;
1171c33a90ecSJames Feist             }
1172b9d36b47SEd Tanous             output.emplace_back("Class", *direction);
1173c33a90ecSJames Feist         }
117483ff9ab6SJames Feist     }
117583ff9ab6SJames Feist     else
117683ff9ab6SJames Feist     {
1177a0744d38SGunnar Mills         BMCWEB_LOG_ERROR << "Illegal Type " << type;
117835a62c7cSJason M. Bills         messages::propertyUnknown(response->res, type);
117983ff9ab6SJames Feist         return CreatePIDRet::fail;
118083ff9ab6SJames Feist     }
118183ff9ab6SJames Feist     return CreatePIDRet::patch;
118283ff9ab6SJames Feist }
118373df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
118473df0db0SJames Feist {
118583ff9ab6SJames Feist 
11868d1b46d7Szhanghch05     GetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
118723a21a1cSEd Tanous         asyncResp(asyncRespIn)
118873df0db0SJames Feist 
11891214b7e7SGunnar Mills     {}
11909c310685SBorawski.Lukasz 
119173df0db0SJames Feist     void run()
11925b4aa86bSJames Feist     {
119373df0db0SJames Feist         std::shared_ptr<GetPIDValues> self = shared_from_this();
119473df0db0SJames Feist 
119573df0db0SJames Feist         // get all configurations
11965b4aa86bSJames Feist         crow::connections::systemBus->async_method_call(
1197b9d36b47SEd Tanous             [self](
1198b9d36b47SEd Tanous                 const boost::system::error_code ec,
1199b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
12005b4aa86bSJames Feist                 if (ec)
12015b4aa86bSJames Feist                 {
12025b4aa86bSJames Feist                     BMCWEB_LOG_ERROR << ec;
120373df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
120473df0db0SJames Feist                     return;
120573df0db0SJames Feist                 }
120623a21a1cSEd Tanous                 self->subtree = subtreeLocal;
120773df0db0SJames Feist             },
120873df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
120973df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
121073df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
121173df0db0SJames Feist             std::array<const char*, 4>{
121273df0db0SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
121373df0db0SJames Feist                 objectManagerIface, stepwiseConfigurationIface});
121473df0db0SJames Feist 
121573df0db0SJames Feist         // at the same time get the selected profile
121673df0db0SJames Feist         crow::connections::systemBus->async_method_call(
1217b9d36b47SEd Tanous             [self](
1218b9d36b47SEd Tanous                 const boost::system::error_code ec,
1219b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
122023a21a1cSEd Tanous                 if (ec || subtreeLocal.empty())
122173df0db0SJames Feist                 {
122273df0db0SJames Feist                     return;
122373df0db0SJames Feist                 }
122423a21a1cSEd Tanous                 if (subtreeLocal[0].second.size() != 1)
122573df0db0SJames Feist                 {
122673df0db0SJames Feist                     // invalid mapper response, should never happen
122773df0db0SJames Feist                     BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
122873df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
12295b4aa86bSJames Feist                     return;
12305b4aa86bSJames Feist                 }
12315b4aa86bSJames Feist 
123223a21a1cSEd Tanous                 const std::string& path = subtreeLocal[0].first;
123323a21a1cSEd Tanous                 const std::string& owner = subtreeLocal[0].second[0].first;
123473df0db0SJames Feist                 crow::connections::systemBus->async_method_call(
1235168e20c1SEd Tanous                     [path, owner,
1236168e20c1SEd Tanous                      self](const boost::system::error_code ec2,
1237b9d36b47SEd Tanous                            const dbus::utility::DBusPropertiesMap& resp) {
123823a21a1cSEd Tanous                         if (ec2)
123973df0db0SJames Feist                         {
12400fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
12410fda0f12SGeorge Liu                                 << "GetPIDValues: Can't get thermalModeIface "
124273df0db0SJames Feist                                 << path;
124373df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
124473df0db0SJames Feist                             return;
124573df0db0SJames Feist                         }
1246271584abSEd Tanous                         const std::string* current = nullptr;
1247271584abSEd Tanous                         const std::vector<std::string>* supported = nullptr;
12489eb808c1SEd Tanous                         for (const auto& [key, value] : resp)
124973df0db0SJames Feist                         {
125073df0db0SJames Feist                             if (key == "Current")
125173df0db0SJames Feist                             {
125273df0db0SJames Feist                                 current = std::get_if<std::string>(&value);
125373df0db0SJames Feist                                 if (current == nullptr)
125473df0db0SJames Feist                                 {
125573df0db0SJames Feist                                     BMCWEB_LOG_ERROR
12560fda0f12SGeorge Liu                                         << "GetPIDValues: thermal mode iface invalid "
125773df0db0SJames Feist                                         << path;
125873df0db0SJames Feist                                     messages::internalError(
125973df0db0SJames Feist                                         self->asyncResp->res);
126073df0db0SJames Feist                                     return;
126173df0db0SJames Feist                                 }
126273df0db0SJames Feist                             }
126373df0db0SJames Feist                             if (key == "Supported")
126473df0db0SJames Feist                             {
126573df0db0SJames Feist                                 supported =
126673df0db0SJames Feist                                     std::get_if<std::vector<std::string>>(
126773df0db0SJames Feist                                         &value);
126873df0db0SJames Feist                                 if (supported == nullptr)
126973df0db0SJames Feist                                 {
127073df0db0SJames Feist                                     BMCWEB_LOG_ERROR
12710fda0f12SGeorge Liu                                         << "GetPIDValues: thermal mode iface invalid"
127273df0db0SJames Feist                                         << path;
127373df0db0SJames Feist                                     messages::internalError(
127473df0db0SJames Feist                                         self->asyncResp->res);
127573df0db0SJames Feist                                     return;
127673df0db0SJames Feist                                 }
127773df0db0SJames Feist                             }
127873df0db0SJames Feist                         }
127973df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
128073df0db0SJames Feist                         {
12810fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
12820fda0f12SGeorge Liu                                 << "GetPIDValues: thermal mode iface invalid "
128373df0db0SJames Feist                                 << path;
128473df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
128573df0db0SJames Feist                             return;
128673df0db0SJames Feist                         }
128773df0db0SJames Feist                         self->currentProfile = *current;
128873df0db0SJames Feist                         self->supportedProfiles = *supported;
128973df0db0SJames Feist                     },
129073df0db0SJames Feist                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
129173df0db0SJames Feist                     thermalModeIface);
129273df0db0SJames Feist             },
129373df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
129473df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
129573df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
129673df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
129773df0db0SJames Feist     }
129873df0db0SJames Feist 
129973df0db0SJames Feist     ~GetPIDValues()
130073df0db0SJames Feist     {
130173df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
130273df0db0SJames Feist         {
130373df0db0SJames Feist             return;
130473df0db0SJames Feist         }
13055b4aa86bSJames Feist         // create map of <connection, path to objMgr>>
130673df0db0SJames Feist         boost::container::flat_map<std::string, std::string> objectMgrPaths;
13076bce33bcSJames Feist         boost::container::flat_set<std::string> calledConnections;
13085b4aa86bSJames Feist         for (const auto& pathGroup : subtree)
13095b4aa86bSJames Feist         {
13105b4aa86bSJames Feist             for (const auto& connectionGroup : pathGroup.second)
13115b4aa86bSJames Feist             {
13126bce33bcSJames Feist                 auto findConnection =
13136bce33bcSJames Feist                     calledConnections.find(connectionGroup.first);
13146bce33bcSJames Feist                 if (findConnection != calledConnections.end())
13156bce33bcSJames Feist                 {
13166bce33bcSJames Feist                     break;
13176bce33bcSJames Feist                 }
131873df0db0SJames Feist                 for (const std::string& interface : connectionGroup.second)
13195b4aa86bSJames Feist                 {
13205b4aa86bSJames Feist                     if (interface == objectManagerIface)
13215b4aa86bSJames Feist                     {
132273df0db0SJames Feist                         objectMgrPaths[connectionGroup.first] = pathGroup.first;
13235b4aa86bSJames Feist                     }
13245b4aa86bSJames Feist                     // this list is alphabetical, so we
13255b4aa86bSJames Feist                     // should have found the objMgr by now
13265b4aa86bSJames Feist                     if (interface == pidConfigurationIface ||
1327b7a08d04SJames Feist                         interface == pidZoneConfigurationIface ||
1328b7a08d04SJames Feist                         interface == stepwiseConfigurationIface)
13295b4aa86bSJames Feist                     {
13305b4aa86bSJames Feist                         auto findObjMgr =
13315b4aa86bSJames Feist                             objectMgrPaths.find(connectionGroup.first);
13325b4aa86bSJames Feist                         if (findObjMgr == objectMgrPaths.end())
13335b4aa86bSJames Feist                         {
13345b4aa86bSJames Feist                             BMCWEB_LOG_DEBUG << connectionGroup.first
13355b4aa86bSJames Feist                                              << "Has no Object Manager";
13365b4aa86bSJames Feist                             continue;
13375b4aa86bSJames Feist                         }
13386bce33bcSJames Feist 
13396bce33bcSJames Feist                         calledConnections.insert(connectionGroup.first);
13406bce33bcSJames Feist 
134173df0db0SJames Feist                         asyncPopulatePid(findObjMgr->first, findObjMgr->second,
134273df0db0SJames Feist                                          currentProfile, supportedProfiles,
134373df0db0SJames Feist                                          asyncResp);
13445b4aa86bSJames Feist                         break;
13455b4aa86bSJames Feist                     }
13465b4aa86bSJames Feist                 }
13475b4aa86bSJames Feist             }
13485b4aa86bSJames Feist         }
134973df0db0SJames Feist     }
135073df0db0SJames Feist 
1351ecd6a3a2SEd Tanous     GetPIDValues(const GetPIDValues&) = delete;
1352ecd6a3a2SEd Tanous     GetPIDValues(GetPIDValues&&) = delete;
1353ecd6a3a2SEd Tanous     GetPIDValues& operator=(const GetPIDValues&) = delete;
1354ecd6a3a2SEd Tanous     GetPIDValues& operator=(GetPIDValues&&) = delete;
1355ecd6a3a2SEd Tanous 
135673df0db0SJames Feist     std::vector<std::string> supportedProfiles;
135773df0db0SJames Feist     std::string currentProfile;
1358b9d36b47SEd Tanous     dbus::utility::MapperGetSubTreeResponse subtree;
13598d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
136073df0db0SJames Feist };
136173df0db0SJames Feist 
136273df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
136373df0db0SJames Feist {
136473df0db0SJames Feist 
13658d1b46d7Szhanghch05     SetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
136673df0db0SJames Feist                  nlohmann::json& data) :
1367271584abSEd Tanous         asyncResp(asyncRespIn)
136873df0db0SJames Feist     {
136973df0db0SJames Feist 
137073df0db0SJames Feist         std::optional<nlohmann::json> pidControllers;
137173df0db0SJames Feist         std::optional<nlohmann::json> fanControllers;
137273df0db0SJames Feist         std::optional<nlohmann::json> fanZones;
137373df0db0SJames Feist         std::optional<nlohmann::json> stepwiseControllers;
137473df0db0SJames Feist 
137573df0db0SJames Feist         if (!redfish::json_util::readJson(
137673df0db0SJames Feist                 data, asyncResp->res, "PidControllers", pidControllers,
137773df0db0SJames Feist                 "FanControllers", fanControllers, "FanZones", fanZones,
137873df0db0SJames Feist                 "StepwiseControllers", stepwiseControllers, "Profile", profile))
137973df0db0SJames Feist         {
138071f52d96SEd Tanous             BMCWEB_LOG_ERROR
138171f52d96SEd Tanous                 << "Illegal Property "
138271f52d96SEd Tanous                 << data.dump(2, ' ', true,
138371f52d96SEd Tanous                              nlohmann::json::error_handler_t::replace);
138473df0db0SJames Feist             return;
138573df0db0SJames Feist         }
138673df0db0SJames Feist         configuration.emplace_back("PidControllers", std::move(pidControllers));
138773df0db0SJames Feist         configuration.emplace_back("FanControllers", std::move(fanControllers));
138873df0db0SJames Feist         configuration.emplace_back("FanZones", std::move(fanZones));
138973df0db0SJames Feist         configuration.emplace_back("StepwiseControllers",
139073df0db0SJames Feist                                    std::move(stepwiseControllers));
139173df0db0SJames Feist     }
1392ecd6a3a2SEd Tanous 
1393ecd6a3a2SEd Tanous     SetPIDValues(const SetPIDValues&) = delete;
1394ecd6a3a2SEd Tanous     SetPIDValues(SetPIDValues&&) = delete;
1395ecd6a3a2SEd Tanous     SetPIDValues& operator=(const SetPIDValues&) = delete;
1396ecd6a3a2SEd Tanous     SetPIDValues& operator=(SetPIDValues&&) = delete;
1397ecd6a3a2SEd Tanous 
139873df0db0SJames Feist     void run()
139973df0db0SJames Feist     {
140073df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
140173df0db0SJames Feist         {
140273df0db0SJames Feist             return;
140373df0db0SJames Feist         }
140473df0db0SJames Feist 
140573df0db0SJames Feist         std::shared_ptr<SetPIDValues> self = shared_from_this();
140673df0db0SJames Feist 
140773df0db0SJames Feist         // todo(james): might make sense to do a mapper call here if this
140873df0db0SJames Feist         // interface gets more traction
140973df0db0SJames Feist         crow::connections::systemBus->async_method_call(
141073df0db0SJames Feist             [self](const boost::system::error_code ec,
1411914e2d5dSEd Tanous                    const dbus::utility::ManagedObjectType& mObj) {
141273df0db0SJames Feist                 if (ec)
141373df0db0SJames Feist                 {
141473df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
141573df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
141673df0db0SJames Feist                     return;
141773df0db0SJames Feist                 }
1418e69d9de2SJames Feist                 const std::array<const char*, 3> configurations = {
1419e69d9de2SJames Feist                     pidConfigurationIface, pidZoneConfigurationIface,
1420e69d9de2SJames Feist                     stepwiseConfigurationIface};
1421e69d9de2SJames Feist 
142214b0b8d5SJames Feist                 for (const auto& [path, object] : mObj)
1423e69d9de2SJames Feist                 {
142414b0b8d5SJames Feist                     for (const auto& [interface, _] : object)
1425e69d9de2SJames Feist                     {
1426e69d9de2SJames Feist                         if (std::find(configurations.begin(),
1427e69d9de2SJames Feist                                       configurations.end(),
1428e69d9de2SJames Feist                                       interface) != configurations.end())
1429e69d9de2SJames Feist                         {
143014b0b8d5SJames Feist                             self->objectCount++;
1431e69d9de2SJames Feist                             break;
1432e69d9de2SJames Feist                         }
1433e69d9de2SJames Feist                     }
1434e69d9de2SJames Feist                 }
1435914e2d5dSEd Tanous                 self->managedObj = mObj;
143673df0db0SJames Feist             },
143773df0db0SJames Feist             "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
143873df0db0SJames Feist             "GetManagedObjects");
143973df0db0SJames Feist 
144073df0db0SJames Feist         // at the same time get the profile information
144173df0db0SJames Feist         crow::connections::systemBus->async_method_call(
144273df0db0SJames Feist             [self](const boost::system::error_code ec,
1443b9d36b47SEd Tanous                    const dbus::utility::MapperGetSubTreeResponse& subtree) {
144473df0db0SJames Feist                 if (ec || subtree.empty())
144573df0db0SJames Feist                 {
144673df0db0SJames Feist                     return;
144773df0db0SJames Feist                 }
144873df0db0SJames Feist                 if (subtree[0].second.empty())
144973df0db0SJames Feist                 {
145073df0db0SJames Feist                     // invalid mapper response, should never happen
145173df0db0SJames Feist                     BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
145273df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
145373df0db0SJames Feist                     return;
145473df0db0SJames Feist                 }
145573df0db0SJames Feist 
145673df0db0SJames Feist                 const std::string& path = subtree[0].first;
145773df0db0SJames Feist                 const std::string& owner = subtree[0].second[0].first;
145873df0db0SJames Feist                 crow::connections::systemBus->async_method_call(
1459b9d36b47SEd Tanous                     [self, path,
1460b9d36b47SEd Tanous                      owner](const boost::system::error_code ec2,
1461b9d36b47SEd Tanous                             const dbus::utility::DBusPropertiesMap& r) {
1462cb13a392SEd Tanous                         if (ec2)
146373df0db0SJames Feist                         {
14640fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
14650fda0f12SGeorge Liu                                 << "SetPIDValues: Can't get thermalModeIface "
146673df0db0SJames Feist                                 << path;
146773df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
146873df0db0SJames Feist                             return;
146973df0db0SJames Feist                         }
1470271584abSEd Tanous                         const std::string* current = nullptr;
1471271584abSEd Tanous                         const std::vector<std::string>* supported = nullptr;
14729eb808c1SEd Tanous                         for (const auto& [key, value] : r)
147373df0db0SJames Feist                         {
147473df0db0SJames Feist                             if (key == "Current")
147573df0db0SJames Feist                             {
147673df0db0SJames Feist                                 current = std::get_if<std::string>(&value);
147773df0db0SJames Feist                                 if (current == nullptr)
147873df0db0SJames Feist                                 {
147973df0db0SJames Feist                                     BMCWEB_LOG_ERROR
14800fda0f12SGeorge Liu                                         << "SetPIDValues: thermal mode iface invalid "
148173df0db0SJames Feist                                         << path;
148273df0db0SJames Feist                                     messages::internalError(
148373df0db0SJames Feist                                         self->asyncResp->res);
148473df0db0SJames Feist                                     return;
148573df0db0SJames Feist                                 }
148673df0db0SJames Feist                             }
148773df0db0SJames Feist                             if (key == "Supported")
148873df0db0SJames Feist                             {
148973df0db0SJames Feist                                 supported =
149073df0db0SJames Feist                                     std::get_if<std::vector<std::string>>(
149173df0db0SJames Feist                                         &value);
149273df0db0SJames Feist                                 if (supported == nullptr)
149373df0db0SJames Feist                                 {
149473df0db0SJames Feist                                     BMCWEB_LOG_ERROR
14950fda0f12SGeorge Liu                                         << "SetPIDValues: thermal mode iface invalid"
149673df0db0SJames Feist                                         << path;
149773df0db0SJames Feist                                     messages::internalError(
149873df0db0SJames Feist                                         self->asyncResp->res);
149973df0db0SJames Feist                                     return;
150073df0db0SJames Feist                                 }
150173df0db0SJames Feist                             }
150273df0db0SJames Feist                         }
150373df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
150473df0db0SJames Feist                         {
15050fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
15060fda0f12SGeorge Liu                                 << "SetPIDValues: thermal mode iface invalid "
150773df0db0SJames Feist                                 << path;
150873df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
150973df0db0SJames Feist                             return;
151073df0db0SJames Feist                         }
151173df0db0SJames Feist                         self->currentProfile = *current;
151273df0db0SJames Feist                         self->supportedProfiles = *supported;
151373df0db0SJames Feist                         self->profileConnection = owner;
151473df0db0SJames Feist                         self->profilePath = path;
151573df0db0SJames Feist                     },
151673df0db0SJames Feist                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
151773df0db0SJames Feist                     thermalModeIface);
15185b4aa86bSJames Feist             },
15195b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper",
15205b4aa86bSJames Feist             "/xyz/openbmc_project/object_mapper",
15215b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
152273df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
152373df0db0SJames Feist     }
152424b2fe81SEd Tanous     void pidSetDone()
152573df0db0SJames Feist     {
152673df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
152773df0db0SJames Feist         {
152873df0db0SJames Feist             return;
15295b4aa86bSJames Feist         }
15308d1b46d7Szhanghch05         std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
153173df0db0SJames Feist         if (profile)
153273df0db0SJames Feist         {
153373df0db0SJames Feist             if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
153473df0db0SJames Feist                           *profile) == supportedProfiles.end())
153573df0db0SJames Feist             {
153673df0db0SJames Feist                 messages::actionParameterUnknown(response->res, "Profile",
153773df0db0SJames Feist                                                  *profile);
153873df0db0SJames Feist                 return;
153973df0db0SJames Feist             }
154073df0db0SJames Feist             currentProfile = *profile;
154173df0db0SJames Feist             crow::connections::systemBus->async_method_call(
154273df0db0SJames Feist                 [response](const boost::system::error_code ec) {
154373df0db0SJames Feist                     if (ec)
154473df0db0SJames Feist                     {
154573df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Error patching profile" << ec;
154673df0db0SJames Feist                         messages::internalError(response->res);
154773df0db0SJames Feist                     }
154873df0db0SJames Feist                 },
154973df0db0SJames Feist                 profileConnection, profilePath,
155073df0db0SJames Feist                 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
1551168e20c1SEd Tanous                 "Current", dbus::utility::DbusVariantType(*profile));
155273df0db0SJames Feist         }
155373df0db0SJames Feist 
155473df0db0SJames Feist         for (auto& containerPair : configuration)
155573df0db0SJames Feist         {
155673df0db0SJames Feist             auto& container = containerPair.second;
155773df0db0SJames Feist             if (!container)
155873df0db0SJames Feist             {
155973df0db0SJames Feist                 continue;
156073df0db0SJames Feist             }
15616ee7f774SJames Feist             BMCWEB_LOG_DEBUG << *container;
15626ee7f774SJames Feist 
156373df0db0SJames Feist             std::string& type = containerPair.first;
156473df0db0SJames Feist 
156573df0db0SJames Feist             for (nlohmann::json::iterator it = container->begin();
156617a897dfSManojkiran Eda                  it != container->end(); ++it)
156773df0db0SJames Feist             {
156873df0db0SJames Feist                 const auto& name = it.key();
15696ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "looking for " << name;
15706ee7f774SJames Feist 
157173df0db0SJames Feist                 auto pathItr =
157273df0db0SJames Feist                     std::find_if(managedObj.begin(), managedObj.end(),
157373df0db0SJames Feist                                  [&name](const auto& obj) {
157473df0db0SJames Feist                                      return boost::algorithm::ends_with(
157573df0db0SJames Feist                                          obj.first.str, "/" + name);
157673df0db0SJames Feist                                  });
1577b9d36b47SEd Tanous                 dbus::utility::DBusPropertiesMap output;
157873df0db0SJames Feist 
157973df0db0SJames Feist                 output.reserve(16); // The pid interface length
158073df0db0SJames Feist 
158173df0db0SJames Feist                 // determines if we're patching entity-manager or
158273df0db0SJames Feist                 // creating a new object
158373df0db0SJames Feist                 bool createNewObject = (pathItr == managedObj.end());
15846ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
15856ee7f774SJames Feist 
158673df0db0SJames Feist                 std::string iface;
1587711ac7a9SEd Tanous                 /*
158873df0db0SJames Feist                 if (type == "PidControllers" || type == "FanControllers")
158973df0db0SJames Feist                 {
159073df0db0SJames Feist                     iface = pidConfigurationIface;
159173df0db0SJames Feist                     if (!createNewObject &&
159273df0db0SJames Feist                         pathItr->second.find(pidConfigurationIface) ==
159373df0db0SJames Feist                             pathItr->second.end())
159473df0db0SJames Feist                     {
159573df0db0SJames Feist                         createNewObject = true;
159673df0db0SJames Feist                     }
159773df0db0SJames Feist                 }
159873df0db0SJames Feist                 else if (type == "FanZones")
159973df0db0SJames Feist                 {
160073df0db0SJames Feist                     iface = pidZoneConfigurationIface;
160173df0db0SJames Feist                     if (!createNewObject &&
160273df0db0SJames Feist                         pathItr->second.find(pidZoneConfigurationIface) ==
160373df0db0SJames Feist                             pathItr->second.end())
160473df0db0SJames Feist                     {
160573df0db0SJames Feist 
160673df0db0SJames Feist                         createNewObject = true;
160773df0db0SJames Feist                     }
160873df0db0SJames Feist                 }
160973df0db0SJames Feist                 else if (type == "StepwiseControllers")
161073df0db0SJames Feist                 {
161173df0db0SJames Feist                     iface = stepwiseConfigurationIface;
161273df0db0SJames Feist                     if (!createNewObject &&
161373df0db0SJames Feist                         pathItr->second.find(stepwiseConfigurationIface) ==
161473df0db0SJames Feist                             pathItr->second.end())
161573df0db0SJames Feist                     {
161673df0db0SJames Feist                         createNewObject = true;
161773df0db0SJames Feist                     }
1618711ac7a9SEd Tanous                 }*/
16196ee7f774SJames Feist 
16206ee7f774SJames Feist                 if (createNewObject && it.value() == nullptr)
16216ee7f774SJames Feist                 {
16224e0453b1SGunnar Mills                     // can't delete a non-existent object
16231668ce6dSEd Tanous                     messages::propertyValueNotInList(response->res,
16241668ce6dSEd Tanous                                                      it.value().dump(), name);
16256ee7f774SJames Feist                     continue;
16266ee7f774SJames Feist                 }
16276ee7f774SJames Feist 
16286ee7f774SJames Feist                 std::string path;
16296ee7f774SJames Feist                 if (pathItr != managedObj.end())
16306ee7f774SJames Feist                 {
16316ee7f774SJames Feist                     path = pathItr->first.str;
16326ee7f774SJames Feist                 }
16336ee7f774SJames Feist 
163473df0db0SJames Feist                 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
1635e69d9de2SJames Feist 
1636e69d9de2SJames Feist                 // arbitrary limit to avoid attacks
1637e69d9de2SJames Feist                 constexpr const size_t controllerLimit = 500;
163814b0b8d5SJames Feist                 if (createNewObject && objectCount >= controllerLimit)
1639e69d9de2SJames Feist                 {
1640e69d9de2SJames Feist                     messages::resourceExhaustion(response->res, type);
1641e69d9de2SJames Feist                     continue;
1642e69d9de2SJames Feist                 }
1643b9d36b47SEd Tanous                 output.emplace_back("Name",
1644b9d36b47SEd Tanous                                     boost::replace_all_copy(name, "_", " "));
164573df0db0SJames Feist 
164673df0db0SJames Feist                 std::string chassis;
164773df0db0SJames Feist                 CreatePIDRet ret = createPidInterface(
16486ee7f774SJames Feist                     response, type, it, path, managedObj, createNewObject,
16496ee7f774SJames Feist                     output, chassis, currentProfile);
165073df0db0SJames Feist                 if (ret == CreatePIDRet::fail)
165173df0db0SJames Feist                 {
165273df0db0SJames Feist                     return;
165373df0db0SJames Feist                 }
16543174e4dfSEd Tanous                 if (ret == CreatePIDRet::del)
165573df0db0SJames Feist                 {
165673df0db0SJames Feist                     continue;
165773df0db0SJames Feist                 }
165873df0db0SJames Feist 
165973df0db0SJames Feist                 if (!createNewObject)
166073df0db0SJames Feist                 {
166173df0db0SJames Feist                     for (const auto& property : output)
166273df0db0SJames Feist                     {
166373df0db0SJames Feist                         crow::connections::systemBus->async_method_call(
166473df0db0SJames Feist                             [response,
166573df0db0SJames Feist                              propertyName{std::string(property.first)}](
166673df0db0SJames Feist                                 const boost::system::error_code ec) {
166773df0db0SJames Feist                                 if (ec)
166873df0db0SJames Feist                                 {
166973df0db0SJames Feist                                     BMCWEB_LOG_ERROR << "Error patching "
167073df0db0SJames Feist                                                      << propertyName << ": "
167173df0db0SJames Feist                                                      << ec;
167273df0db0SJames Feist                                     messages::internalError(response->res);
167373df0db0SJames Feist                                     return;
167473df0db0SJames Feist                                 }
167573df0db0SJames Feist                                 messages::success(response->res);
167673df0db0SJames Feist                             },
16776ee7f774SJames Feist                             "xyz.openbmc_project.EntityManager", path,
167873df0db0SJames Feist                             "org.freedesktop.DBus.Properties", "Set", iface,
167973df0db0SJames Feist                             property.first, property.second);
168073df0db0SJames Feist                     }
168173df0db0SJames Feist                 }
168273df0db0SJames Feist                 else
168373df0db0SJames Feist                 {
168473df0db0SJames Feist                     if (chassis.empty())
168573df0db0SJames Feist                     {
168673df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to get chassis from config";
1687ace85d60SEd Tanous                         messages::internalError(response->res);
168873df0db0SJames Feist                         return;
168973df0db0SJames Feist                     }
169073df0db0SJames Feist 
169173df0db0SJames Feist                     bool foundChassis = false;
169273df0db0SJames Feist                     for (const auto& obj : managedObj)
169373df0db0SJames Feist                     {
169473df0db0SJames Feist                         if (boost::algorithm::ends_with(obj.first.str, chassis))
169573df0db0SJames Feist                         {
169673df0db0SJames Feist                             chassis = obj.first.str;
169773df0db0SJames Feist                             foundChassis = true;
169873df0db0SJames Feist                             break;
169973df0db0SJames Feist                         }
170073df0db0SJames Feist                     }
170173df0db0SJames Feist                     if (!foundChassis)
170273df0db0SJames Feist                     {
170373df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
170473df0db0SJames Feist                         messages::resourceMissingAtURI(
1705ace85d60SEd Tanous                             response->res,
1706ace85d60SEd Tanous                             crow::utility::urlFromPieces("redfish", "v1",
1707ace85d60SEd Tanous                                                          "Chassis", chassis));
170873df0db0SJames Feist                         return;
170973df0db0SJames Feist                     }
171073df0db0SJames Feist 
171173df0db0SJames Feist                     crow::connections::systemBus->async_method_call(
171273df0db0SJames Feist                         [response](const boost::system::error_code ec) {
171373df0db0SJames Feist                             if (ec)
171473df0db0SJames Feist                             {
171573df0db0SJames Feist                                 BMCWEB_LOG_ERROR << "Error Adding Pid Object "
171673df0db0SJames Feist                                                  << ec;
171773df0db0SJames Feist                                 messages::internalError(response->res);
171873df0db0SJames Feist                                 return;
171973df0db0SJames Feist                             }
172073df0db0SJames Feist                             messages::success(response->res);
172173df0db0SJames Feist                         },
172273df0db0SJames Feist                         "xyz.openbmc_project.EntityManager", chassis,
172373df0db0SJames Feist                         "xyz.openbmc_project.AddObject", "AddObject", output);
172473df0db0SJames Feist                 }
172573df0db0SJames Feist             }
172673df0db0SJames Feist         }
172773df0db0SJames Feist     }
172824b2fe81SEd Tanous 
172924b2fe81SEd Tanous     ~SetPIDValues()
173024b2fe81SEd Tanous     {
173124b2fe81SEd Tanous         try
173224b2fe81SEd Tanous         {
173324b2fe81SEd Tanous             pidSetDone();
173424b2fe81SEd Tanous         }
173524b2fe81SEd Tanous         catch (...)
173624b2fe81SEd Tanous         {
173724b2fe81SEd Tanous             BMCWEB_LOG_CRITICAL << "pidSetDone threw exception";
173824b2fe81SEd Tanous         }
173924b2fe81SEd Tanous     }
174024b2fe81SEd Tanous 
17418d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
174273df0db0SJames Feist     std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
174373df0db0SJames Feist         configuration;
174473df0db0SJames Feist     std::optional<std::string> profile;
174573df0db0SJames Feist     dbus::utility::ManagedObjectType managedObj;
174673df0db0SJames Feist     std::vector<std::string> supportedProfiles;
174773df0db0SJames Feist     std::string currentProfile;
174873df0db0SJames Feist     std::string profileConnection;
174973df0db0SJames Feist     std::string profilePath;
175014b0b8d5SJames Feist     size_t objectCount = 0;
175173df0db0SJames Feist };
175273df0db0SJames Feist 
1753071d8fdfSSunnySrivastava1984 /**
1754071d8fdfSSunnySrivastava1984  * @brief Retrieves BMC manager location data over DBus
1755071d8fdfSSunnySrivastava1984  *
1756071d8fdfSSunnySrivastava1984  * @param[in] aResp Shared pointer for completing asynchronous calls
1757071d8fdfSSunnySrivastava1984  * @param[in] connectionName - service name
1758071d8fdfSSunnySrivastava1984  * @param[in] path - object path
1759071d8fdfSSunnySrivastava1984  * @return none
1760071d8fdfSSunnySrivastava1984  */
17618d1b46d7Szhanghch05 inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1762071d8fdfSSunnySrivastava1984                         const std::string& connectionName,
1763071d8fdfSSunnySrivastava1984                         const std::string& path)
1764071d8fdfSSunnySrivastava1984 {
1765071d8fdfSSunnySrivastava1984     BMCWEB_LOG_DEBUG << "Get BMC manager Location data.";
1766071d8fdfSSunnySrivastava1984 
17671e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
17681e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
17691e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
1770071d8fdfSSunnySrivastava1984         [aResp](const boost::system::error_code ec,
17711e1e598dSJonathan Doman                 const std::string& property) {
1772071d8fdfSSunnySrivastava1984             if (ec)
1773071d8fdfSSunnySrivastava1984             {
1774071d8fdfSSunnySrivastava1984                 BMCWEB_LOG_DEBUG << "DBUS response error for "
1775071d8fdfSSunnySrivastava1984                                     "Location";
1776071d8fdfSSunnySrivastava1984                 messages::internalError(aResp->res);
1777071d8fdfSSunnySrivastava1984                 return;
1778071d8fdfSSunnySrivastava1984             }
1779071d8fdfSSunnySrivastava1984 
1780071d8fdfSSunnySrivastava1984             aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
17811e1e598dSJonathan Doman                 property;
17821e1e598dSJonathan Doman         });
1783071d8fdfSSunnySrivastava1984 }
17847e860f15SJohn Edward Broadbent // avoid name collision systems.hpp
17857e860f15SJohn Edward Broadbent inline void
17867e860f15SJohn Edward Broadbent     managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
17874bf2b033SGunnar Mills {
17884bf2b033SGunnar Mills     BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time";
17894bf2b033SGunnar Mills 
17901e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint64_t>(
17911e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
17921e1e598dSJonathan Doman         "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
17931e1e598dSJonathan Doman         "LastRebootTime",
17944bf2b033SGunnar Mills         [aResp](const boost::system::error_code ec,
17951e1e598dSJonathan Doman                 const uint64_t lastResetTime) {
17964bf2b033SGunnar Mills             if (ec)
17974bf2b033SGunnar Mills             {
17984bf2b033SGunnar Mills                 BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
17994bf2b033SGunnar Mills                 return;
18004bf2b033SGunnar Mills             }
18014bf2b033SGunnar Mills 
18024bf2b033SGunnar Mills             // LastRebootTime is epoch time, in milliseconds
18034bf2b033SGunnar Mills             // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
18041e1e598dSJonathan Doman             uint64_t lastResetTimeStamp = lastResetTime / 1000;
18054bf2b033SGunnar Mills 
18064bf2b033SGunnar Mills             // Convert to ISO 8601 standard
18074bf2b033SGunnar Mills             aResp->res.jsonValue["LastResetTime"] =
18081d8782e7SNan Zhou                 crow::utility::getDateTimeUint(lastResetTimeStamp);
18091e1e598dSJonathan Doman         });
18104bf2b033SGunnar Mills }
18114bf2b033SGunnar Mills 
18124bfefa74SGunnar Mills /**
18134bfefa74SGunnar Mills  * @brief Set the running firmware image
18144bfefa74SGunnar Mills  *
18154bfefa74SGunnar Mills  * @param[i,o] aResp - Async response object
18164bfefa74SGunnar Mills  * @param[i] runningFirmwareTarget - Image to make the running image
18174bfefa74SGunnar Mills  *
18184bfefa74SGunnar Mills  * @return void
18194bfefa74SGunnar Mills  */
18207e860f15SJohn Edward Broadbent inline void
18217e860f15SJohn Edward Broadbent     setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1822f23b7296SEd Tanous                            const std::string& runningFirmwareTarget)
18234bfefa74SGunnar Mills {
18244bfefa74SGunnar Mills     // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1825f23b7296SEd Tanous     std::string::size_type idPos = runningFirmwareTarget.rfind('/');
18264bfefa74SGunnar Mills     if (idPos == std::string::npos)
18274bfefa74SGunnar Mills     {
18284bfefa74SGunnar Mills         messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
18294bfefa74SGunnar Mills                                          "@odata.id");
18304bfefa74SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't parse firmware ID!";
18314bfefa74SGunnar Mills         return;
18324bfefa74SGunnar Mills     }
18334bfefa74SGunnar Mills     idPos++;
18344bfefa74SGunnar Mills     if (idPos >= runningFirmwareTarget.size())
18354bfefa74SGunnar Mills     {
18364bfefa74SGunnar Mills         messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
18374bfefa74SGunnar Mills                                          "@odata.id");
18384bfefa74SGunnar Mills         BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
18394bfefa74SGunnar Mills         return;
18404bfefa74SGunnar Mills     }
18414bfefa74SGunnar Mills     std::string firmwareId = runningFirmwareTarget.substr(idPos);
18424bfefa74SGunnar Mills 
18434bfefa74SGunnar Mills     // Make sure the image is valid before setting priority
18444bfefa74SGunnar Mills     crow::connections::systemBus->async_method_call(
1845711ac7a9SEd Tanous         [aResp, firmwareId,
1846711ac7a9SEd Tanous          runningFirmwareTarget](const boost::system::error_code ec,
1847711ac7a9SEd Tanous                                 dbus::utility::ManagedObjectType& subtree) {
18484bfefa74SGunnar Mills             if (ec)
18494bfefa74SGunnar Mills             {
18504bfefa74SGunnar Mills                 BMCWEB_LOG_DEBUG << "D-Bus response error getting objects.";
18514bfefa74SGunnar Mills                 messages::internalError(aResp->res);
18524bfefa74SGunnar Mills                 return;
18534bfefa74SGunnar Mills             }
18544bfefa74SGunnar Mills 
185526f6976fSEd Tanous             if (subtree.empty())
18564bfefa74SGunnar Mills             {
18574bfefa74SGunnar Mills                 BMCWEB_LOG_DEBUG << "Can't find image!";
18584bfefa74SGunnar Mills                 messages::internalError(aResp->res);
18594bfefa74SGunnar Mills                 return;
18604bfefa74SGunnar Mills             }
18614bfefa74SGunnar Mills 
18624bfefa74SGunnar Mills             bool foundImage = false;
18634bfefa74SGunnar Mills             for (auto& object : subtree)
18644bfefa74SGunnar Mills             {
18654bfefa74SGunnar Mills                 const std::string& path =
18664bfefa74SGunnar Mills                     static_cast<const std::string&>(object.first);
1867f23b7296SEd Tanous                 std::size_t idPos2 = path.rfind('/');
18684bfefa74SGunnar Mills 
18694bfefa74SGunnar Mills                 if (idPos2 == std::string::npos)
18704bfefa74SGunnar Mills                 {
18714bfefa74SGunnar Mills                     continue;
18724bfefa74SGunnar Mills                 }
18734bfefa74SGunnar Mills 
18744bfefa74SGunnar Mills                 idPos2++;
18754bfefa74SGunnar Mills                 if (idPos2 >= path.size())
18764bfefa74SGunnar Mills                 {
18774bfefa74SGunnar Mills                     continue;
18784bfefa74SGunnar Mills                 }
18794bfefa74SGunnar Mills 
18804bfefa74SGunnar Mills                 if (path.substr(idPos2) == firmwareId)
18814bfefa74SGunnar Mills                 {
18824bfefa74SGunnar Mills                     foundImage = true;
18834bfefa74SGunnar Mills                     break;
18844bfefa74SGunnar Mills                 }
18854bfefa74SGunnar Mills             }
18864bfefa74SGunnar Mills 
18874bfefa74SGunnar Mills             if (!foundImage)
18884bfefa74SGunnar Mills             {
18894bfefa74SGunnar Mills                 messages::propertyValueNotInList(
18904bfefa74SGunnar Mills                     aResp->res, runningFirmwareTarget, "@odata.id");
18914bfefa74SGunnar Mills                 BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
18924bfefa74SGunnar Mills                 return;
18934bfefa74SGunnar Mills             }
18944bfefa74SGunnar Mills 
18958cc8edecSEd Tanous             BMCWEB_LOG_DEBUG << "Setting firmware version " << firmwareId
18968cc8edecSEd Tanous                              << " to priority 0.";
18974bfefa74SGunnar Mills 
18984bfefa74SGunnar Mills             // Only support Immediate
18994bfefa74SGunnar Mills             // An addition could be a Redfish Setting like
19004bfefa74SGunnar Mills             // ActiveSoftwareImageApplyTime and support OnReset
19014bfefa74SGunnar Mills             crow::connections::systemBus->async_method_call(
19024bfefa74SGunnar Mills                 [aResp](const boost::system::error_code ec) {
19034bfefa74SGunnar Mills                     if (ec)
19044bfefa74SGunnar Mills                     {
19054bfefa74SGunnar Mills                         BMCWEB_LOG_DEBUG << "D-Bus response error setting.";
19064bfefa74SGunnar Mills                         messages::internalError(aResp->res);
19074bfefa74SGunnar Mills                         return;
19084bfefa74SGunnar Mills                     }
19094bfefa74SGunnar Mills                     doBMCGracefulRestart(aResp);
19104bfefa74SGunnar Mills                 },
19114bfefa74SGunnar Mills 
19124bfefa74SGunnar Mills                 "xyz.openbmc_project.Software.BMC.Updater",
19134bfefa74SGunnar Mills                 "/xyz/openbmc_project/software/" + firmwareId,
19144bfefa74SGunnar Mills                 "org.freedesktop.DBus.Properties", "Set",
19157e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1916168e20c1SEd Tanous                 dbus::utility::DbusVariantType(static_cast<uint8_t>(0)));
19174bfefa74SGunnar Mills         },
19184bfefa74SGunnar Mills         "xyz.openbmc_project.Software.BMC.Updater",
19197e860f15SJohn Edward Broadbent         "/xyz/openbmc_project/software", "org.freedesktop.DBus.ObjectManager",
19207e860f15SJohn Edward Broadbent         "GetManagedObjects");
19214bfefa74SGunnar Mills }
19224bfefa74SGunnar Mills 
19237e860f15SJohn Edward Broadbent inline void setDateTime(std::shared_ptr<bmcweb::AsyncResp> aResp,
19247e860f15SJohn Edward Broadbent                         std::string datetime)
1925af5d6058SSantosh Puranik {
1926af5d6058SSantosh Puranik     BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
1927af5d6058SSantosh Puranik 
1928af5d6058SSantosh Puranik     std::stringstream stream(datetime);
1929af5d6058SSantosh Puranik     // Convert from ISO 8601 to boost local_time
1930af5d6058SSantosh Puranik     // (BMC only has time in UTC)
1931af5d6058SSantosh Puranik     boost::posix_time::ptime posixTime;
1932af5d6058SSantosh Puranik     boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
1933af5d6058SSantosh Puranik     // Facet gets deleted with the stringsteam
1934af5d6058SSantosh Puranik     auto ifc = std::make_unique<boost::local_time::local_time_input_facet>(
1935af5d6058SSantosh Puranik         "%Y-%m-%d %H:%M:%S%F %ZP");
1936af5d6058SSantosh Puranik     stream.imbue(std::locale(stream.getloc(), ifc.release()));
1937af5d6058SSantosh Puranik 
19387e860f15SJohn Edward Broadbent     boost::local_time::local_date_time ldt(boost::local_time::not_a_date_time);
1939af5d6058SSantosh Puranik 
1940af5d6058SSantosh Puranik     if (stream >> ldt)
1941af5d6058SSantosh Puranik     {
1942af5d6058SSantosh Puranik         posixTime = ldt.utc_time();
1943af5d6058SSantosh Puranik         boost::posix_time::time_duration dur = posixTime - epoch;
19447e860f15SJohn Edward Broadbent         uint64_t durMicroSecs = static_cast<uint64_t>(dur.total_microseconds());
1945af5d6058SSantosh Puranik         crow::connections::systemBus->async_method_call(
1946af5d6058SSantosh Puranik             [aResp{std::move(aResp)}, datetime{std::move(datetime)}](
1947af5d6058SSantosh Puranik                 const boost::system::error_code ec) {
1948af5d6058SSantosh Puranik                 if (ec)
1949af5d6058SSantosh Puranik                 {
1950af5d6058SSantosh Puranik                     BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1951af5d6058SSantosh Puranik                                         "DBUS response error "
1952af5d6058SSantosh Puranik                                      << ec;
1953af5d6058SSantosh Puranik                     messages::internalError(aResp->res);
1954af5d6058SSantosh Puranik                     return;
1955af5d6058SSantosh Puranik                 }
1956af5d6058SSantosh Puranik                 aResp->res.jsonValue["DateTime"] = datetime;
1957af5d6058SSantosh Puranik             },
19587e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Time.Manager", "/xyz/openbmc_project/time/bmc",
1959af5d6058SSantosh Puranik             "org.freedesktop.DBus.Properties", "Set",
1960af5d6058SSantosh Puranik             "xyz.openbmc_project.Time.EpochTime", "Elapsed",
1961168e20c1SEd Tanous             dbus::utility::DbusVariantType(durMicroSecs));
1962af5d6058SSantosh Puranik     }
1963af5d6058SSantosh Puranik     else
1964af5d6058SSantosh Puranik     {
19657e860f15SJohn Edward Broadbent         messages::propertyValueFormatError(aResp->res, datetime, "DateTime");
1966af5d6058SSantosh Puranik         return;
1967af5d6058SSantosh Puranik     }
196883ff9ab6SJames Feist }
19699c310685SBorawski.Lukasz 
19707e860f15SJohn Edward Broadbent inline void requestRoutesManager(App& app)
19717e860f15SJohn Edward Broadbent {
19727e860f15SJohn Edward Broadbent     std::string uuid = persistent_data::getConfig().systemUuid;
19739c310685SBorawski.Lukasz 
19747e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
1975ed398213SEd Tanous         .privileges(redfish::privileges::getManager)
197645ca1b86SEd Tanous         .methods(
197745ca1b86SEd Tanous             boost::beast::http::verb::
197845ca1b86SEd Tanous                 get)([&app, uuid](
197945ca1b86SEd Tanous                          const crow::Request& req,
198045ca1b86SEd Tanous                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
198145ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
198245ca1b86SEd Tanous             {
198345ca1b86SEd Tanous                 return;
198445ca1b86SEd Tanous             }
19857e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
19867e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.type"] =
19877e860f15SJohn Edward Broadbent                 "#Manager.v1_11_0.Manager";
19887e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Id"] = "bmc";
19897e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
19907e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Description"] =
19917e860f15SJohn Edward Broadbent                 "Baseboard Management Controller";
19927e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["PowerState"] = "On";
1993*1476687dSEd Tanous             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
1994*1476687dSEd Tanous             asyncResp->res.jsonValue["Status"]["Health"] = "OK";
1995*1476687dSEd Tanous 
19967e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["ManagerType"] = "BMC";
19977e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
19987e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
19997e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Model"] =
20007e860f15SJohn Edward Broadbent                 "OpenBmc"; // TODO(ed), get model
20017e860f15SJohn Edward Broadbent 
2002*1476687dSEd Tanous             asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
2003*1476687dSEd Tanous                 "/redfish/v1/Managers/bmc/LogServices";
2004*1476687dSEd Tanous             asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
2005*1476687dSEd Tanous                 "/redfish/v1/Managers/bmc/NetworkProtocol";
2006*1476687dSEd Tanous             asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
2007*1476687dSEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces";
20087e860f15SJohn Edward Broadbent 
20097e860f15SJohn Edward Broadbent #ifdef BMCWEB_ENABLE_VM_NBDPROXY
2010*1476687dSEd Tanous             asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
2011*1476687dSEd Tanous                 "/redfish/v1/Managers/bmc/VirtualMedia";
20127e860f15SJohn Edward Broadbent #endif // BMCWEB_ENABLE_VM_NBDPROXY
20137e860f15SJohn Edward Broadbent 
20147e860f15SJohn Edward Broadbent             // default oem data
20157e860f15SJohn Edward Broadbent             nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
20167e860f15SJohn Edward Broadbent             nlohmann::json& oemOpenbmc = oem["OpenBmc"];
20177e860f15SJohn Edward Broadbent             oem["@odata.type"] = "#OemManager.Oem";
20187e860f15SJohn Edward Broadbent             oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
20197e860f15SJohn Edward Broadbent             oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
20207e860f15SJohn Edward Broadbent             oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
2021*1476687dSEd Tanous 
2022*1476687dSEd Tanous             nlohmann::json::object_t certificates;
2023*1476687dSEd Tanous             certificates["@odata.id"] =
2024*1476687dSEd Tanous                 "/redfish/v1/Managers/bmc/Truststore/Certificates";
2025*1476687dSEd Tanous             oemOpenbmc["Certificates"] = std::move(certificates);
20267e860f15SJohn Edward Broadbent 
20277e860f15SJohn Edward Broadbent             // Manager.Reset (an action) can be many values, OpenBMC only
20287e860f15SJohn Edward Broadbent             // supports BMC reboot.
20297e860f15SJohn Edward Broadbent             nlohmann::json& managerReset =
20307e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
20317e860f15SJohn Edward Broadbent             managerReset["target"] =
20327e860f15SJohn Edward Broadbent                 "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
20337e860f15SJohn Edward Broadbent             managerReset["@Redfish.ActionInfo"] =
20347e860f15SJohn Edward Broadbent                 "/redfish/v1/Managers/bmc/ResetActionInfo";
20357e860f15SJohn Edward Broadbent 
20367e860f15SJohn Edward Broadbent             // ResetToDefaults (Factory Reset) has values like
20377e860f15SJohn Edward Broadbent             // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
20387e860f15SJohn Edward Broadbent             // on OpenBMC
20397e860f15SJohn Edward Broadbent             nlohmann::json& resetToDefaults =
20407e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
20417e860f15SJohn Edward Broadbent             resetToDefaults["target"] =
20427e860f15SJohn Edward Broadbent                 "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
20437e860f15SJohn Edward Broadbent             resetToDefaults["ResetType@Redfish.AllowableValues"] = {"ResetAll"};
20447e860f15SJohn Edward Broadbent 
20457c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
20467c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
20477c8c4058STejas Patil 
20487c8c4058STejas Patil             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
20497c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
20507c8c4058STejas Patil                 redfishDateTimeOffset.second;
20517e860f15SJohn Edward Broadbent 
20520e8ac5e7SGunnar Mills             // TODO (Gunnar): Remove these one day since moved to ComputerSystem
20530e8ac5e7SGunnar Mills             // Still used by OCP profiles
20540e8ac5e7SGunnar Mills             // https://github.com/opencomputeproject/OCP-Profiles/issues/23
20557e860f15SJohn Edward Broadbent             // Fill in SerialConsole info
20567e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
20577e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] =
20587e860f15SJohn Edward Broadbent                 15;
20597e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
20607e860f15SJohn Edward Broadbent                 {"IPMI", "SSH"};
20617e860f15SJohn Edward Broadbent #ifdef BMCWEB_ENABLE_KVM
20627e860f15SJohn Edward Broadbent             // Fill in GraphicalConsole info
20637e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] =
20647e860f15SJohn Edward Broadbent                 true;
20657e860f15SJohn Edward Broadbent             asyncResp->res
20667e860f15SJohn Edward Broadbent                 .jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4;
20677e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["GraphicalConsole"]
20687e860f15SJohn Edward Broadbent                                     ["ConnectTypesSupported"] = {"KVMIP"};
20697e860f15SJohn Edward Broadbent #endif // BMCWEB_ENABLE_KVM
20707e860f15SJohn Edward Broadbent 
20717e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] =
20727e860f15SJohn Edward Broadbent                 1;
2073*1476687dSEd Tanous 
2074*1476687dSEd Tanous             nlohmann::json::array_t managerForServers;
2075*1476687dSEd Tanous             nlohmann::json::object_t manager;
2076*1476687dSEd Tanous             manager["@odata.id"] = "/redfish/v1/Systems/system";
2077*1476687dSEd Tanous             managerForServers.push_back(std::move(manager));
2078*1476687dSEd Tanous 
2079*1476687dSEd Tanous             asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
2080*1476687dSEd Tanous                 std::move(managerForServers);
20817e860f15SJohn Edward Broadbent 
20827e860f15SJohn Edward Broadbent             auto health = std::make_shared<HealthPopulate>(asyncResp);
20837e860f15SJohn Edward Broadbent             health->isManagersHealth = true;
20847e860f15SJohn Edward Broadbent             health->populate();
20857e860f15SJohn Edward Broadbent 
20867e860f15SJohn Edward Broadbent             fw_util::populateFirmwareInformation(asyncResp, fw_util::bmcPurpose,
20877e860f15SJohn Edward Broadbent                                                  "FirmwareVersion", true);
20887e860f15SJohn Edward Broadbent 
20897e860f15SJohn Edward Broadbent             managerGetLastResetTime(asyncResp);
20907e860f15SJohn Edward Broadbent 
20917e860f15SJohn Edward Broadbent             auto pids = std::make_shared<GetPIDValues>(asyncResp);
20927e860f15SJohn Edward Broadbent             pids->run();
20937e860f15SJohn Edward Broadbent 
2094*1476687dSEd Tanous             getMainChassisId(asyncResp, [](const std::string& chassisId,
2095*1476687dSEd Tanous                                            const std::shared_ptr<
2096*1476687dSEd Tanous                                                bmcweb::AsyncResp>& aRsp) {
2097*1476687dSEd Tanous                 aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] =
20987e860f15SJohn Edward Broadbent                     1;
2099*1476687dSEd Tanous                 nlohmann::json::array_t managerForChassis;
2100*1476687dSEd Tanous                 nlohmann::json::object_t manager;
2101*1476687dSEd Tanous                 manager["@odata.id"] = "/redfish/v1/Chassis/" + chassisId;
2102*1476687dSEd Tanous                 managerForChassis.push_back(std::move(manager));
2103*1476687dSEd Tanous                 aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
2104*1476687dSEd Tanous                     std::move(managerForChassis);
2105*1476687dSEd Tanous                 aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
2106*1476687dSEd Tanous                     "/redfish/v1/Chassis/" + chassisId;
21077e860f15SJohn Edward Broadbent             });
21087e860f15SJohn Edward Broadbent 
21097e860f15SJohn Edward Broadbent             static bool started = false;
21107e860f15SJohn Edward Broadbent 
21117e860f15SJohn Edward Broadbent             if (!started)
21121abe55efSEd Tanous             {
21131e1e598dSJonathan Doman                 sdbusplus::asio::getProperty<double>(
21141e1e598dSJonathan Doman                     *crow::connections::systemBus, "org.freedesktop.systemd1",
21151e1e598dSJonathan Doman                     "/org/freedesktop/systemd1",
21161e1e598dSJonathan Doman                     "org.freedesktop.systemd1.Manager", "Progress",
21177e860f15SJohn Edward Broadbent                     [asyncResp](const boost::system::error_code ec,
21181e1e598dSJonathan Doman                                 const double& val) {
21197e860f15SJohn Edward Broadbent                         if (ec)
21201abe55efSEd Tanous                         {
21217e860f15SJohn Edward Broadbent                             BMCWEB_LOG_ERROR << "Error while getting progress";
21227e860f15SJohn Edward Broadbent                             messages::internalError(asyncResp->res);
21237e860f15SJohn Edward Broadbent                             return;
21247e860f15SJohn Edward Broadbent                         }
21251e1e598dSJonathan Doman                         if (val < 1.0)
21267e860f15SJohn Edward Broadbent                         {
21277e860f15SJohn Edward Broadbent                             asyncResp->res.jsonValue["Status"]["State"] =
21287e860f15SJohn Edward Broadbent                                 "Starting";
21297e860f15SJohn Edward Broadbent                             started = true;
21307e860f15SJohn Edward Broadbent                         }
21311e1e598dSJonathan Doman                     });
21329c310685SBorawski.Lukasz             }
21339c310685SBorawski.Lukasz 
21347e860f15SJohn Edward Broadbent             crow::connections::systemBus->async_method_call(
21357e860f15SJohn Edward Broadbent                 [asyncResp](
21367e860f15SJohn Edward Broadbent                     const boost::system::error_code ec,
2137b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
21387e860f15SJohn Edward Broadbent                     if (ec)
21391abe55efSEd Tanous                     {
21407e860f15SJohn Edward Broadbent                         BMCWEB_LOG_DEBUG
21417e860f15SJohn Edward Broadbent                             << "D-Bus response error on GetSubTree " << ec;
21427e860f15SJohn Edward Broadbent                         return;
21437e860f15SJohn Edward Broadbent                     }
214426f6976fSEd Tanous                     if (subtree.empty())
21457e860f15SJohn Edward Broadbent                     {
21467e860f15SJohn Edward Broadbent                         BMCWEB_LOG_DEBUG << "Can't find bmc D-Bus object!";
21477e860f15SJohn Edward Broadbent                         return;
21487e860f15SJohn Edward Broadbent                     }
21497e860f15SJohn Edward Broadbent                     // Assume only 1 bmc D-Bus object
21507e860f15SJohn Edward Broadbent                     // Throw an error if there is more than 1
21517e860f15SJohn Edward Broadbent                     if (subtree.size() > 1)
21527e860f15SJohn Edward Broadbent                     {
21537e860f15SJohn Edward Broadbent                         BMCWEB_LOG_DEBUG
21547e860f15SJohn Edward Broadbent                             << "Found more than 1 bmc D-Bus object!";
21557e860f15SJohn Edward Broadbent                         messages::internalError(asyncResp->res);
21567e860f15SJohn Edward Broadbent                         return;
21577e860f15SJohn Edward Broadbent                     }
21587e860f15SJohn Edward Broadbent 
21597e860f15SJohn Edward Broadbent                     if (subtree[0].first.empty() ||
21607e860f15SJohn Edward Broadbent                         subtree[0].second.size() != 1)
21617e860f15SJohn Edward Broadbent                     {
21627e860f15SJohn Edward Broadbent                         BMCWEB_LOG_DEBUG << "Error getting bmc D-Bus object!";
21637e860f15SJohn Edward Broadbent                         messages::internalError(asyncResp->res);
21647e860f15SJohn Edward Broadbent                         return;
21657e860f15SJohn Edward Broadbent                     }
21667e860f15SJohn Edward Broadbent 
21677e860f15SJohn Edward Broadbent                     const std::string& path = subtree[0].first;
21687e860f15SJohn Edward Broadbent                     const std::string& connectionName =
21697e860f15SJohn Edward Broadbent                         subtree[0].second[0].first;
21707e860f15SJohn Edward Broadbent 
21717e860f15SJohn Edward Broadbent                     for (const auto& interfaceName :
21727e860f15SJohn Edward Broadbent                          subtree[0].second[0].second)
21737e860f15SJohn Edward Broadbent                     {
21747e860f15SJohn Edward Broadbent                         if (interfaceName ==
21757e860f15SJohn Edward Broadbent                             "xyz.openbmc_project.Inventory.Decorator.Asset")
21767e860f15SJohn Edward Broadbent                         {
21777e860f15SJohn Edward Broadbent                             crow::connections::systemBus->async_method_call(
21787e860f15SJohn Edward Broadbent                                 [asyncResp](
21797e860f15SJohn Edward Broadbent                                     const boost::system::error_code ec,
2180b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap&
21817e860f15SJohn Edward Broadbent                                         propertiesList) {
21827e860f15SJohn Edward Broadbent                                     if (ec)
21837e860f15SJohn Edward Broadbent                                     {
21847e860f15SJohn Edward Broadbent                                         BMCWEB_LOG_DEBUG
21857e860f15SJohn Edward Broadbent                                             << "Can't get bmc asset!";
21867e860f15SJohn Edward Broadbent                                         return;
21877e860f15SJohn Edward Broadbent                                     }
21887e860f15SJohn Edward Broadbent                                     for (const std::pair<
21897e860f15SJohn Edward Broadbent                                              std::string,
2190168e20c1SEd Tanous                                              dbus::utility::DbusVariantType>&
21917e860f15SJohn Edward Broadbent                                              property : propertiesList)
21927e860f15SJohn Edward Broadbent                                     {
21937e860f15SJohn Edward Broadbent                                         const std::string& propertyName =
21947e860f15SJohn Edward Broadbent                                             property.first;
21957e860f15SJohn Edward Broadbent 
21967e860f15SJohn Edward Broadbent                                         if ((propertyName == "PartNumber") ||
21977e860f15SJohn Edward Broadbent                                             (propertyName == "SerialNumber") ||
21987e860f15SJohn Edward Broadbent                                             (propertyName == "Manufacturer") ||
21997e860f15SJohn Edward Broadbent                                             (propertyName == "Model") ||
22007e860f15SJohn Edward Broadbent                                             (propertyName == "SparePartNumber"))
22017e860f15SJohn Edward Broadbent                                         {
22027e860f15SJohn Edward Broadbent                                             const std::string* value =
22037e860f15SJohn Edward Broadbent                                                 std::get_if<std::string>(
22047e860f15SJohn Edward Broadbent                                                     &property.second);
22057e860f15SJohn Edward Broadbent                                             if (value == nullptr)
22067e860f15SJohn Edward Broadbent                                             {
22077e860f15SJohn Edward Broadbent                                                 // illegal property
22087e860f15SJohn Edward Broadbent                                                 messages::internalError(
22097e860f15SJohn Edward Broadbent                                                     asyncResp->res);
22107e860f15SJohn Edward Broadbent                                                 return;
22117e860f15SJohn Edward Broadbent                                             }
22127e860f15SJohn Edward Broadbent                                             asyncResp->res
22137e860f15SJohn Edward Broadbent                                                 .jsonValue[propertyName] =
22147e860f15SJohn Edward Broadbent                                                 *value;
22157e860f15SJohn Edward Broadbent                                         }
22167e860f15SJohn Edward Broadbent                                     }
22177e860f15SJohn Edward Broadbent                                 },
22187e860f15SJohn Edward Broadbent                                 connectionName, path,
22197e860f15SJohn Edward Broadbent                                 "org.freedesktop.DBus.Properties", "GetAll",
22200fda0f12SGeorge Liu                                 "xyz.openbmc_project.Inventory.Decorator.Asset");
22217e860f15SJohn Edward Broadbent                         }
22220fda0f12SGeorge Liu                         else if (
22230fda0f12SGeorge Liu                             interfaceName ==
22240fda0f12SGeorge Liu                             "xyz.openbmc_project.Inventory.Decorator.LocationCode")
22257e860f15SJohn Edward Broadbent                         {
22267e860f15SJohn Edward Broadbent                             getLocation(asyncResp, connectionName, path);
22277e860f15SJohn Edward Broadbent                         }
22287e860f15SJohn Edward Broadbent                     }
22297e860f15SJohn Edward Broadbent                 },
22307e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper",
22317e860f15SJohn Edward Broadbent                 "/xyz/openbmc_project/object_mapper",
22327e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
22337e860f15SJohn Edward Broadbent                 "/xyz/openbmc_project/inventory", int32_t(0),
22347e860f15SJohn Edward Broadbent                 std::array<const char*, 1>{
22357e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Inventory.Item.Bmc"});
22367e860f15SJohn Edward Broadbent         });
22377e860f15SJohn Edward Broadbent 
22387e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
2239ed398213SEd Tanous         .privileges(redfish::privileges::patchManager)
224045ca1b86SEd Tanous         .methods(boost::beast::http::verb::patch)(
224145ca1b86SEd Tanous             [&app](const crow::Request& req,
22427e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
224345ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
224445ca1b86SEd Tanous                 {
224545ca1b86SEd Tanous                     return;
224645ca1b86SEd Tanous                 }
22477e860f15SJohn Edward Broadbent                 std::optional<nlohmann::json> oem;
22487e860f15SJohn Edward Broadbent                 std::optional<nlohmann::json> links;
22497e860f15SJohn Edward Broadbent                 std::optional<std::string> datetime;
22507e860f15SJohn Edward Broadbent 
225115ed6780SWilly Tu                 if (!json_util::readJsonPatch(req, asyncResp->res, "Oem", oem,
225245ca1b86SEd Tanous                                               "DateTime", datetime, "Links",
225345ca1b86SEd Tanous                                               links))
22547e860f15SJohn Edward Broadbent                 {
22557e860f15SJohn Edward Broadbent                     return;
22567e860f15SJohn Edward Broadbent                 }
22577e860f15SJohn Edward Broadbent 
22587e860f15SJohn Edward Broadbent                 if (oem)
22597e860f15SJohn Edward Broadbent                 {
22607e860f15SJohn Edward Broadbent                     std::optional<nlohmann::json> openbmc;
22617e860f15SJohn Edward Broadbent                     if (!redfish::json_util::readJson(*oem, asyncResp->res,
22627e860f15SJohn Edward Broadbent                                                       "OpenBmc", openbmc))
22637e860f15SJohn Edward Broadbent                     {
22647e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR
22657e860f15SJohn Edward Broadbent                             << "Illegal Property "
226645ca1b86SEd Tanous                             << oem->dump(
226745ca1b86SEd Tanous                                    2, ' ', true,
22687e860f15SJohn Edward Broadbent                                    nlohmann::json::error_handler_t::replace);
22697e860f15SJohn Edward Broadbent                         return;
22707e860f15SJohn Edward Broadbent                     }
22717e860f15SJohn Edward Broadbent                     if (openbmc)
22727e860f15SJohn Edward Broadbent                     {
22737e860f15SJohn Edward Broadbent                         std::optional<nlohmann::json> fan;
227445ca1b86SEd Tanous                         if (!redfish::json_util::readJson(
227545ca1b86SEd Tanous                                 *openbmc, asyncResp->res, "Fan", fan))
22767e860f15SJohn Edward Broadbent                         {
22777e860f15SJohn Edward Broadbent                             BMCWEB_LOG_ERROR
22787e860f15SJohn Edward Broadbent                                 << "Illegal Property "
227945ca1b86SEd Tanous                                 << openbmc->dump(2, ' ', true,
228045ca1b86SEd Tanous                                                  nlohmann::json::
228145ca1b86SEd Tanous                                                      error_handler_t::replace);
22827e860f15SJohn Edward Broadbent                             return;
22837e860f15SJohn Edward Broadbent                         }
22847e860f15SJohn Edward Broadbent                         if (fan)
22857e860f15SJohn Edward Broadbent                         {
22867e860f15SJohn Edward Broadbent                             auto pid =
22877e860f15SJohn Edward Broadbent                                 std::make_shared<SetPIDValues>(asyncResp, *fan);
22887e860f15SJohn Edward Broadbent                             pid->run();
22897e860f15SJohn Edward Broadbent                         }
22907e860f15SJohn Edward Broadbent                     }
22917e860f15SJohn Edward Broadbent                 }
22927e860f15SJohn Edward Broadbent                 if (links)
22937e860f15SJohn Edward Broadbent                 {
22947e860f15SJohn Edward Broadbent                     std::optional<nlohmann::json> activeSoftwareImage;
22957e860f15SJohn Edward Broadbent                     if (!redfish::json_util::readJson(*links, asyncResp->res,
22967e860f15SJohn Edward Broadbent                                                       "ActiveSoftwareImage",
22977e860f15SJohn Edward Broadbent                                                       activeSoftwareImage))
22987e860f15SJohn Edward Broadbent                     {
22997e860f15SJohn Edward Broadbent                         return;
23007e860f15SJohn Edward Broadbent                     }
23017e860f15SJohn Edward Broadbent                     if (activeSoftwareImage)
23027e860f15SJohn Edward Broadbent                     {
23037e860f15SJohn Edward Broadbent                         std::optional<std::string> odataId;
23047e860f15SJohn Edward Broadbent                         if (!json_util::readJson(*activeSoftwareImage,
23057e860f15SJohn Edward Broadbent                                                  asyncResp->res, "@odata.id",
23067e860f15SJohn Edward Broadbent                                                  odataId))
23077e860f15SJohn Edward Broadbent                         {
23087e860f15SJohn Edward Broadbent                             return;
23097e860f15SJohn Edward Broadbent                         }
23107e860f15SJohn Edward Broadbent 
23117e860f15SJohn Edward Broadbent                         if (odataId)
23127e860f15SJohn Edward Broadbent                         {
23137e860f15SJohn Edward Broadbent                             setActiveFirmwareImage(asyncResp, *odataId);
23147e860f15SJohn Edward Broadbent                         }
23157e860f15SJohn Edward Broadbent                     }
23167e860f15SJohn Edward Broadbent                 }
23177e860f15SJohn Edward Broadbent                 if (datetime)
23187e860f15SJohn Edward Broadbent                 {
23197e860f15SJohn Edward Broadbent                     setDateTime(asyncResp, std::move(*datetime));
23207e860f15SJohn Edward Broadbent                 }
23217e860f15SJohn Edward Broadbent             });
23227e860f15SJohn Edward Broadbent }
23237e860f15SJohn Edward Broadbent 
23247e860f15SJohn Edward Broadbent inline void requestRoutesManagerCollection(App& app)
23257e860f15SJohn Edward Broadbent {
23267e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
2327ed398213SEd Tanous         .privileges(redfish::privileges::getManagerCollection)
23287e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
232945ca1b86SEd Tanous             [&app](const crow::Request& req,
23307e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
233145ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
233245ca1b86SEd Tanous                 {
233345ca1b86SEd Tanous                     return;
233445ca1b86SEd Tanous                 }
233583ff9ab6SJames Feist                 // Collections don't include the static data added by SubRoute
233683ff9ab6SJames Feist                 // because it has a duplicate entry for members
23378d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
23388d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
23398d1b46d7Szhanghch05                     "#ManagerCollection.ManagerCollection";
23408d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "Manager Collection";
23418d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Members@odata.count"] = 1;
2342*1476687dSEd Tanous                 nlohmann::json::array_t members;
2343*1476687dSEd Tanous                 nlohmann::json& bmc = members.emplace_back();
2344*1476687dSEd Tanous                 bmc["@odata.id"] = "/redfish/v1/Managers/bmc";
2345*1476687dSEd Tanous                 asyncResp->res.jsonValue["Members"] = std::move(members);
23467e860f15SJohn Edward Broadbent             });
23479c310685SBorawski.Lukasz }
23489c310685SBorawski.Lukasz } // namespace redfish
2349