xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision 613dabea03cd2f5cb999be37aaf539280c63ea7a)
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 
18a51fc2d2SSui Chen #include "app.hpp"
19a51fc2d2SSui Chen #include "dbus_utility.hpp"
20b49ac873SJames Feist #include "health.hpp"
21a51fc2d2SSui Chen #include "query.hpp"
22c5d03ff4SJennifer Lee #include "redfish_util.hpp"
23a51fc2d2SSui Chen #include "registries/privilege_registry.hpp"
24fac6e53bSKrzysztof Grobelny #include "utils/dbus_utils.hpp"
25a51fc2d2SSui Chen #include "utils/sw_utils.hpp"
26a51fc2d2SSui Chen #include "utils/systemd_utils.hpp"
272b82937eSEd Tanous #include "utils/time_utils.hpp"
289c310685SBorawski.Lukasz 
29af5d6058SSantosh Puranik #include <boost/date_time.hpp>
30fac6e53bSKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
31fac6e53bSKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
321214b7e7SGunnar Mills 
33a170f275SEd Tanous #include <algorithm>
344bfefa74SGunnar Mills #include <cstdint>
351214b7e7SGunnar Mills #include <memory>
361214b7e7SGunnar Mills #include <sstream>
37abf2add6SEd Tanous #include <variant>
385b4aa86bSJames Feist 
391abe55efSEd Tanous namespace redfish
401abe55efSEd Tanous {
41ed5befbdSJennifer Lee 
42ed5befbdSJennifer Lee /**
432a5c4407SGunnar Mills  * Function reboots the BMC.
442a5c4407SGunnar Mills  *
452a5c4407SGunnar Mills  * @param[in] asyncResp - Shared pointer for completing asynchronous calls
46ed5befbdSJennifer Lee  */
478d1b46d7Szhanghch05 inline void
488d1b46d7Szhanghch05     doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
49ed5befbdSJennifer Lee {
50ed5befbdSJennifer Lee     const char* processName = "xyz.openbmc_project.State.BMC";
51ed5befbdSJennifer Lee     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
52ed5befbdSJennifer Lee     const char* interfaceName = "xyz.openbmc_project.State.BMC";
53ed5befbdSJennifer Lee     const std::string& propertyValue =
54ed5befbdSJennifer Lee         "xyz.openbmc_project.State.BMC.Transition.Reboot";
55ed5befbdSJennifer Lee     const char* destProperty = "RequestedBMCTransition";
56ed5befbdSJennifer Lee 
57ed5befbdSJennifer Lee     // Create the D-Bus variant for D-Bus call.
58168e20c1SEd Tanous     dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
59ed5befbdSJennifer Lee 
60ed5befbdSJennifer Lee     crow::connections::systemBus->async_method_call(
61ed5befbdSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
62ed5befbdSJennifer Lee         // Use "Set" method to set the property value.
63ed5befbdSJennifer Lee         if (ec)
64ed5befbdSJennifer Lee         {
652a5c4407SGunnar Mills             BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
66ed5befbdSJennifer Lee             messages::internalError(asyncResp->res);
67ed5befbdSJennifer Lee             return;
68ed5befbdSJennifer Lee         }
69ed5befbdSJennifer Lee 
70ed5befbdSJennifer Lee         messages::success(asyncResp->res);
71ed5befbdSJennifer Lee         },
72ed5befbdSJennifer Lee         processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
73ed5befbdSJennifer Lee         interfaceName, destProperty, dbusPropertyValue);
74ed5befbdSJennifer Lee }
752a5c4407SGunnar Mills 
768d1b46d7Szhanghch05 inline void
778d1b46d7Szhanghch05     doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
78f92af389SJayaprakash Mutyala {
79f92af389SJayaprakash Mutyala     const char* processName = "xyz.openbmc_project.State.BMC";
80f92af389SJayaprakash Mutyala     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
81f92af389SJayaprakash Mutyala     const char* interfaceName = "xyz.openbmc_project.State.BMC";
82f92af389SJayaprakash Mutyala     const std::string& propertyValue =
83f92af389SJayaprakash Mutyala         "xyz.openbmc_project.State.BMC.Transition.HardReboot";
84f92af389SJayaprakash Mutyala     const char* destProperty = "RequestedBMCTransition";
85f92af389SJayaprakash Mutyala 
86f92af389SJayaprakash Mutyala     // Create the D-Bus variant for D-Bus call.
87168e20c1SEd Tanous     dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
88f92af389SJayaprakash Mutyala 
89f92af389SJayaprakash Mutyala     crow::connections::systemBus->async_method_call(
90f92af389SJayaprakash Mutyala         [asyncResp](const boost::system::error_code ec) {
91f92af389SJayaprakash Mutyala         // Use "Set" method to set the property value.
92f92af389SJayaprakash Mutyala         if (ec)
93f92af389SJayaprakash Mutyala         {
94f92af389SJayaprakash Mutyala             BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
95f92af389SJayaprakash Mutyala             messages::internalError(asyncResp->res);
96f92af389SJayaprakash Mutyala             return;
97f92af389SJayaprakash Mutyala         }
98f92af389SJayaprakash Mutyala 
99f92af389SJayaprakash Mutyala         messages::success(asyncResp->res);
100f92af389SJayaprakash Mutyala         },
101f92af389SJayaprakash Mutyala         processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
102f92af389SJayaprakash Mutyala         interfaceName, destProperty, dbusPropertyValue);
103f92af389SJayaprakash Mutyala }
104f92af389SJayaprakash Mutyala 
1052a5c4407SGunnar Mills /**
1062a5c4407SGunnar Mills  * ManagerResetAction class supports the POST method for the Reset (reboot)
1072a5c4407SGunnar Mills  * action.
1082a5c4407SGunnar Mills  */
1097e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetAction(App& app)
1102a5c4407SGunnar Mills {
1112a5c4407SGunnar Mills     /**
1122a5c4407SGunnar Mills      * Function handles POST method request.
1132a5c4407SGunnar Mills      * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
114f92af389SJayaprakash Mutyala      * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
1152a5c4407SGunnar Mills      */
1167e860f15SJohn Edward Broadbent 
1177e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
118ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
1197e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
12045ca1b86SEd Tanous             [&app](const crow::Request& req,
1217e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1223ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
12345ca1b86SEd Tanous         {
12445ca1b86SEd Tanous             return;
12545ca1b86SEd Tanous         }
1262a5c4407SGunnar Mills         BMCWEB_LOG_DEBUG << "Post Manager Reset.";
1272a5c4407SGunnar Mills 
1282a5c4407SGunnar Mills         std::string resetType;
1292a5c4407SGunnar Mills 
13015ed6780SWilly Tu         if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
1317e860f15SJohn Edward Broadbent                                        resetType))
1322a5c4407SGunnar Mills         {
1332a5c4407SGunnar Mills             return;
1342a5c4407SGunnar Mills         }
1352a5c4407SGunnar Mills 
136f92af389SJayaprakash Mutyala         if (resetType == "GracefulRestart")
137f92af389SJayaprakash Mutyala         {
138f92af389SJayaprakash Mutyala             BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
139f92af389SJayaprakash Mutyala             doBMCGracefulRestart(asyncResp);
140f92af389SJayaprakash Mutyala             return;
141f92af389SJayaprakash Mutyala         }
1423174e4dfSEd Tanous         if (resetType == "ForceRestart")
143f92af389SJayaprakash Mutyala         {
144f92af389SJayaprakash Mutyala             BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
145f92af389SJayaprakash Mutyala             doBMCForceRestart(asyncResp);
146f92af389SJayaprakash Mutyala             return;
147f92af389SJayaprakash Mutyala         }
1482a5c4407SGunnar Mills         BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
1492a5c4407SGunnar Mills                          << resetType;
1502a5c4407SGunnar Mills         messages::actionParameterNotSupported(asyncResp->res, resetType,
1512a5c4407SGunnar Mills                                               "ResetType");
1522a5c4407SGunnar Mills 
1532a5c4407SGunnar Mills         return;
1547e860f15SJohn Edward Broadbent         });
1552a5c4407SGunnar Mills }
156ed5befbdSJennifer Lee 
1573e40fc74SGunnar Mills /**
1583e40fc74SGunnar Mills  * ManagerResetToDefaultsAction class supports POST method for factory reset
1593e40fc74SGunnar Mills  * action.
1603e40fc74SGunnar Mills  */
1617e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetToDefaultsAction(App& app)
1623e40fc74SGunnar Mills {
1633e40fc74SGunnar Mills 
1643e40fc74SGunnar Mills     /**
1653e40fc74SGunnar Mills      * Function handles ResetToDefaults POST method request.
1663e40fc74SGunnar Mills      *
1673e40fc74SGunnar Mills      * Analyzes POST body message and factory resets BMC by calling
1683e40fc74SGunnar Mills      * BMC code updater factory reset followed by a BMC reboot.
1693e40fc74SGunnar Mills      *
1703e40fc74SGunnar Mills      * BMC code updater factory reset wipes the whole BMC read-write
1713e40fc74SGunnar Mills      * filesystem which includes things like the network settings.
1723e40fc74SGunnar Mills      *
1733e40fc74SGunnar Mills      * OpenBMC only supports ResetToDefaultsType "ResetAll".
1743e40fc74SGunnar Mills      */
1757e860f15SJohn Edward Broadbent 
1767e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
1777e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/")
178ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
1797e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
18045ca1b86SEd Tanous             [&app](const crow::Request& req,
1817e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1823ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
18345ca1b86SEd Tanous         {
18445ca1b86SEd Tanous             return;
18545ca1b86SEd Tanous         }
1863e40fc74SGunnar Mills         BMCWEB_LOG_DEBUG << "Post ResetToDefaults.";
1873e40fc74SGunnar Mills 
1883e40fc74SGunnar Mills         std::string resetType;
1893e40fc74SGunnar Mills 
190002d39b4SEd Tanous         if (!json_util::readJsonAction(req, asyncResp->res,
191002d39b4SEd Tanous                                        "ResetToDefaultsType", resetType))
1923e40fc74SGunnar Mills         {
1933e40fc74SGunnar Mills             BMCWEB_LOG_DEBUG << "Missing property ResetToDefaultsType.";
1943e40fc74SGunnar Mills 
195002d39b4SEd Tanous             messages::actionParameterMissing(asyncResp->res, "ResetToDefaults",
1963e40fc74SGunnar Mills                                              "ResetToDefaultsType");
1973e40fc74SGunnar Mills             return;
1983e40fc74SGunnar Mills         }
1993e40fc74SGunnar Mills 
2003e40fc74SGunnar Mills         if (resetType != "ResetAll")
2013e40fc74SGunnar Mills         {
2020fda0f12SGeorge Liu             BMCWEB_LOG_DEBUG
2030fda0f12SGeorge Liu                 << "Invalid property value for ResetToDefaultsType: "
2043e40fc74SGunnar Mills                 << resetType;
205002d39b4SEd Tanous             messages::actionParameterNotSupported(asyncResp->res, resetType,
206002d39b4SEd Tanous                                                   "ResetToDefaultsType");
2073e40fc74SGunnar Mills             return;
2083e40fc74SGunnar Mills         }
2093e40fc74SGunnar Mills 
2103e40fc74SGunnar Mills         crow::connections::systemBus->async_method_call(
2113e40fc74SGunnar Mills             [asyncResp](const boost::system::error_code ec) {
2123e40fc74SGunnar Mills             if (ec)
2133e40fc74SGunnar Mills             {
214002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "Failed to ResetToDefaults: " << ec;
2153e40fc74SGunnar Mills                 messages::internalError(asyncResp->res);
2163e40fc74SGunnar Mills                 return;
2173e40fc74SGunnar Mills             }
2183e40fc74SGunnar Mills             // Factory Reset doesn't actually happen until a reboot
2193e40fc74SGunnar Mills             // Can't erase what the BMC is running on
2203e40fc74SGunnar Mills             doBMCGracefulRestart(asyncResp);
2213e40fc74SGunnar Mills             },
2223e40fc74SGunnar Mills             "xyz.openbmc_project.Software.BMC.Updater",
2233e40fc74SGunnar Mills             "/xyz/openbmc_project/software",
2243e40fc74SGunnar Mills             "xyz.openbmc_project.Common.FactoryReset", "Reset");
2257e860f15SJohn Edward Broadbent         });
2263e40fc74SGunnar Mills }
2273e40fc74SGunnar Mills 
2281cb1a9e6SAppaRao Puli /**
2291cb1a9e6SAppaRao Puli  * ManagerResetActionInfo derived class for delivering Manager
2301cb1a9e6SAppaRao Puli  * ResetType AllowableValues using ResetInfo schema.
2311cb1a9e6SAppaRao Puli  */
2327e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetActionInfo(App& app)
2331cb1a9e6SAppaRao Puli {
2341cb1a9e6SAppaRao Puli     /**
2351cb1a9e6SAppaRao Puli      * Functions triggers appropriate requests on DBus
2361cb1a9e6SAppaRao Puli      */
2377e860f15SJohn Edward Broadbent 
2387e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/ResetActionInfo/")
239ed398213SEd Tanous         .privileges(redfish::privileges::getActionInfo)
2407e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
24145ca1b86SEd Tanous             [&app](const crow::Request& req,
2427e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2433ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
24445ca1b86SEd Tanous         {
24545ca1b86SEd Tanous             return;
24645ca1b86SEd Tanous         }
2471476687dSEd Tanous 
2481476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
2491476687dSEd Tanous             "#ActionInfo.v1_1_2.ActionInfo";
2501476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
2511476687dSEd Tanous             "/redfish/v1/Managers/bmc/ResetActionInfo";
2521476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "Reset Action Info";
2531476687dSEd Tanous         asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
2541476687dSEd Tanous         nlohmann::json::object_t parameter;
2551476687dSEd Tanous         parameter["Name"] = "ResetType";
2561476687dSEd Tanous         parameter["Required"] = true;
2571476687dSEd Tanous         parameter["DataType"] = "String";
2581476687dSEd Tanous 
2591476687dSEd Tanous         nlohmann::json::array_t allowableValues;
2601476687dSEd Tanous         allowableValues.push_back("GracefulRestart");
2611476687dSEd Tanous         allowableValues.push_back("ForceRestart");
2621476687dSEd Tanous         parameter["AllowableValues"] = std::move(allowableValues);
2631476687dSEd Tanous 
2641476687dSEd Tanous         nlohmann::json::array_t parameters;
2651476687dSEd Tanous         parameters.push_back(std::move(parameter));
2661476687dSEd Tanous 
2671476687dSEd Tanous         asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
2687e860f15SJohn Edward Broadbent         });
2691cb1a9e6SAppaRao Puli }
2701cb1a9e6SAppaRao Puli 
2715b4aa86bSJames Feist static constexpr const char* objectManagerIface =
2725b4aa86bSJames Feist     "org.freedesktop.DBus.ObjectManager";
2735b4aa86bSJames Feist static constexpr const char* pidConfigurationIface =
2745b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid";
2755b4aa86bSJames Feist static constexpr const char* pidZoneConfigurationIface =
2765b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid.Zone";
277b7a08d04SJames Feist static constexpr const char* stepwiseConfigurationIface =
278b7a08d04SJames Feist     "xyz.openbmc_project.Configuration.Stepwise";
27973df0db0SJames Feist static constexpr const char* thermalModeIface =
28073df0db0SJames Feist     "xyz.openbmc_project.Control.ThermalMode";
2819c310685SBorawski.Lukasz 
2828d1b46d7Szhanghch05 inline void
2838d1b46d7Szhanghch05     asyncPopulatePid(const std::string& connection, const std::string& path,
28473df0db0SJames Feist                      const std::string& currentProfile,
28573df0db0SJames Feist                      const std::vector<std::string>& supportedProfiles,
2868d1b46d7Szhanghch05                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2875b4aa86bSJames Feist {
2885b4aa86bSJames Feist 
2895b4aa86bSJames Feist     crow::connections::systemBus->async_method_call(
29073df0db0SJames Feist         [asyncResp, currentProfile, supportedProfiles](
29173df0db0SJames Feist             const boost::system::error_code ec,
2925b4aa86bSJames Feist             const dbus::utility::ManagedObjectType& managedObj) {
2935b4aa86bSJames Feist         if (ec)
2945b4aa86bSJames Feist         {
2955b4aa86bSJames Feist             BMCWEB_LOG_ERROR << ec;
2965b4aa86bSJames Feist             asyncResp->res.jsonValue.clear();
297f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2985b4aa86bSJames Feist             return;
2995b4aa86bSJames Feist         }
3005b4aa86bSJames Feist         nlohmann::json& configRoot =
3015b4aa86bSJames Feist             asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
3025b4aa86bSJames Feist         nlohmann::json& fans = configRoot["FanControllers"];
3035b4aa86bSJames Feist         fans["@odata.type"] = "#OemManager.FanControllers";
3040fda0f12SGeorge Liu         fans["@odata.id"] =
3050fda0f12SGeorge Liu             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers";
3065b4aa86bSJames Feist 
3075b4aa86bSJames Feist         nlohmann::json& pids = configRoot["PidControllers"];
3085b4aa86bSJames Feist         pids["@odata.type"] = "#OemManager.PidControllers";
3095b4aa86bSJames Feist         pids["@odata.id"] =
3105b4aa86bSJames Feist             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
3115b4aa86bSJames Feist 
312b7a08d04SJames Feist         nlohmann::json& stepwise = configRoot["StepwiseControllers"];
313b7a08d04SJames Feist         stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
314b7a08d04SJames Feist         stepwise["@odata.id"] =
315b7a08d04SJames Feist             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
316b7a08d04SJames Feist 
3175b4aa86bSJames Feist         nlohmann::json& zones = configRoot["FanZones"];
3185b4aa86bSJames Feist         zones["@odata.id"] =
3195b4aa86bSJames Feist             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
3205b4aa86bSJames Feist         zones["@odata.type"] = "#OemManager.FanZones";
321002d39b4SEd Tanous         configRoot["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
3225b4aa86bSJames Feist         configRoot["@odata.type"] = "#OemManager.Fan";
32373df0db0SJames Feist         configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
32473df0db0SJames Feist 
32573df0db0SJames Feist         if (!currentProfile.empty())
32673df0db0SJames Feist         {
32773df0db0SJames Feist             configRoot["Profile"] = currentProfile;
32873df0db0SJames Feist         }
32973df0db0SJames Feist         BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !";
3305b4aa86bSJames Feist 
3315b4aa86bSJames Feist         for (const auto& pathPair : managedObj)
3325b4aa86bSJames Feist         {
3335b4aa86bSJames Feist             for (const auto& intfPair : pathPair.second)
3345b4aa86bSJames Feist             {
3355b4aa86bSJames Feist                 if (intfPair.first != pidConfigurationIface &&
336b7a08d04SJames Feist                     intfPair.first != pidZoneConfigurationIface &&
337b7a08d04SJames Feist                     intfPair.first != stepwiseConfigurationIface)
3385b4aa86bSJames Feist                 {
3395b4aa86bSJames Feist                     continue;
3405b4aa86bSJames Feist                 }
34173df0db0SJames Feist 
342711ac7a9SEd Tanous                 std::string name;
343711ac7a9SEd Tanous 
344711ac7a9SEd Tanous                 for (const std::pair<std::string,
345002d39b4SEd Tanous                                      dbus::utility::DbusVariantType>& propPair :
346002d39b4SEd Tanous                      intfPair.second)
347711ac7a9SEd Tanous                 {
348711ac7a9SEd Tanous                     if (propPair.first == "Name")
349711ac7a9SEd Tanous                     {
3505b4aa86bSJames Feist                         const std::string* namePtr =
351711ac7a9SEd Tanous                             std::get_if<std::string>(&propPair.second);
3525b4aa86bSJames Feist                         if (namePtr == nullptr)
3535b4aa86bSJames Feist                         {
3545b4aa86bSJames Feist                             BMCWEB_LOG_ERROR << "Pid Name Field illegal";
355b7a08d04SJames Feist                             messages::internalError(asyncResp->res);
3565b4aa86bSJames Feist                             return;
3575b4aa86bSJames Feist                         }
358db697703SWilly Tu                         name = *namePtr;
3595b4aa86bSJames Feist                         dbus::utility::escapePathForDbus(name);
360711ac7a9SEd Tanous                     }
361711ac7a9SEd Tanous                     else if (propPair.first == "Profiles")
36273df0db0SJames Feist                     {
36373df0db0SJames Feist                         const std::vector<std::string>* profiles =
36473df0db0SJames Feist                             std::get_if<std::vector<std::string>>(
365711ac7a9SEd Tanous                                 &propPair.second);
36673df0db0SJames Feist                         if (profiles == nullptr)
36773df0db0SJames Feist                         {
368002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "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
376002d39b4SEd Tanous                                 << name << " not supported in current profile";
37773df0db0SJames Feist                             continue;
37873df0db0SJames Feist                         }
37973df0db0SJames Feist                     }
380711ac7a9SEd Tanous                 }
381b7a08d04SJames Feist                 nlohmann::json* config = nullptr;
382c33a90ecSJames Feist                 const std::string* classPtr = nullptr;
383711ac7a9SEd Tanous 
384711ac7a9SEd Tanous                 for (const std::pair<std::string,
385002d39b4SEd Tanous                                      dbus::utility::DbusVariantType>& propPair :
386002d39b4SEd Tanous                      intfPair.second)
387c33a90ecSJames Feist                 {
388727dc83fSLei YU                     if (propPair.first == "Class")
389711ac7a9SEd Tanous                     {
390002d39b4SEd Tanous                         classPtr = std::get_if<std::string>(&propPair.second);
391711ac7a9SEd Tanous                     }
392c33a90ecSJames Feist                 }
393c33a90ecSJames Feist 
3945b4aa86bSJames Feist                 if (intfPair.first == pidZoneConfigurationIface)
3955b4aa86bSJames Feist                 {
3965b4aa86bSJames Feist                     std::string chassis;
397002d39b4SEd Tanous                     if (!dbus::utility::getNthStringFromPath(pathPair.first.str,
398002d39b4SEd Tanous                                                              5, chassis))
3995b4aa86bSJames Feist                     {
4005b4aa86bSJames Feist                         chassis = "#IllegalValue";
4015b4aa86bSJames Feist                     }
4025b4aa86bSJames Feist                     nlohmann::json& zone = zones[name];
403*613dabeaSEd Tanous                     zone["Chassis"]["@odata.id"] =
404*613dabeaSEd Tanous                         "/redfish/v1/Chassis/" + chassis;
4050fda0f12SGeorge Liu                     zone["@odata.id"] =
4060fda0f12SGeorge Liu                         "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/" +
4075b4aa86bSJames Feist                         name;
4085b4aa86bSJames Feist                     zone["@odata.type"] = "#OemManager.FanZone";
409b7a08d04SJames Feist                     config = &zone;
4105b4aa86bSJames Feist                 }
4115b4aa86bSJames Feist 
412b7a08d04SJames Feist                 else if (intfPair.first == stepwiseConfigurationIface)
4135b4aa86bSJames Feist                 {
414c33a90ecSJames Feist                     if (classPtr == nullptr)
415c33a90ecSJames Feist                     {
416c33a90ecSJames Feist                         BMCWEB_LOG_ERROR << "Pid Class Field illegal";
417c33a90ecSJames Feist                         messages::internalError(asyncResp->res);
418c33a90ecSJames Feist                         return;
419c33a90ecSJames Feist                     }
420c33a90ecSJames Feist 
421b7a08d04SJames Feist                     nlohmann::json& controller = stepwise[name];
422b7a08d04SJames Feist                     config = &controller;
4235b4aa86bSJames Feist 
424b7a08d04SJames Feist                     controller["@odata.id"] =
4250fda0f12SGeorge Liu                         "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers/" +
426271584abSEd Tanous                         name;
427b7a08d04SJames Feist                     controller["@odata.type"] =
428b7a08d04SJames Feist                         "#OemManager.StepwiseController";
429b7a08d04SJames Feist 
430c33a90ecSJames Feist                     controller["Direction"] = *classPtr;
4315b4aa86bSJames Feist                 }
4325b4aa86bSJames Feist 
4335b4aa86bSJames Feist                 // pid and fans are off the same configuration
434b7a08d04SJames Feist                 else if (intfPair.first == pidConfigurationIface)
4355b4aa86bSJames Feist                 {
436c33a90ecSJames Feist 
4375b4aa86bSJames Feist                     if (classPtr == nullptr)
4385b4aa86bSJames Feist                     {
4395b4aa86bSJames Feist                         BMCWEB_LOG_ERROR << "Pid Class Field illegal";
440a08b46ccSJason M. Bills                         messages::internalError(asyncResp->res);
4415b4aa86bSJames Feist                         return;
4425b4aa86bSJames Feist                     }
4435b4aa86bSJames Feist                     bool isFan = *classPtr == "fan";
444002d39b4SEd Tanous                     nlohmann::json& element = isFan ? fans[name] : pids[name];
445b7a08d04SJames Feist                     config = &element;
4465b4aa86bSJames Feist                     if (isFan)
4475b4aa86bSJames Feist                     {
4485b4aa86bSJames Feist                         element["@odata.id"] =
4490fda0f12SGeorge Liu                             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers/" +
450271584abSEd Tanous                             name;
451002d39b4SEd Tanous                         element["@odata.type"] = "#OemManager.FanController";
4525b4aa86bSJames Feist                     }
4535b4aa86bSJames Feist                     else
4545b4aa86bSJames Feist                     {
4555b4aa86bSJames Feist                         element["@odata.id"] =
4560fda0f12SGeorge Liu                             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers/" +
457271584abSEd Tanous                             name;
458002d39b4SEd Tanous                         element["@odata.type"] = "#OemManager.PidController";
4595b4aa86bSJames Feist                     }
460b7a08d04SJames Feist                 }
461b7a08d04SJames Feist                 else
462b7a08d04SJames Feist                 {
463b7a08d04SJames Feist                     BMCWEB_LOG_ERROR << "Unexpected configuration";
464b7a08d04SJames Feist                     messages::internalError(asyncResp->res);
465b7a08d04SJames Feist                     return;
466b7a08d04SJames Feist                 }
467b7a08d04SJames Feist 
468b7a08d04SJames Feist                 // used for making maps out of 2 vectors
469b7a08d04SJames Feist                 const std::vector<double>* keys = nullptr;
470b7a08d04SJames Feist                 const std::vector<double>* values = nullptr;
471b7a08d04SJames Feist 
472b7a08d04SJames Feist                 for (const auto& propertyPair : intfPair.second)
473b7a08d04SJames Feist                 {
474b7a08d04SJames Feist                     if (propertyPair.first == "Type" ||
475b7a08d04SJames Feist                         propertyPair.first == "Class" ||
476b7a08d04SJames Feist                         propertyPair.first == "Name")
477b7a08d04SJames Feist                     {
478b7a08d04SJames Feist                         continue;
479b7a08d04SJames Feist                     }
480b7a08d04SJames Feist 
481b7a08d04SJames Feist                     // zones
482b7a08d04SJames Feist                     if (intfPair.first == pidZoneConfigurationIface)
483b7a08d04SJames Feist                     {
484b7a08d04SJames Feist                         const double* ptr =
485abf2add6SEd Tanous                             std::get_if<double>(&propertyPair.second);
486b7a08d04SJames Feist                         if (ptr == nullptr)
487b7a08d04SJames Feist                         {
488b7a08d04SJames Feist                             BMCWEB_LOG_ERROR << "Field Illegal "
489b7a08d04SJames Feist                                              << propertyPair.first;
490b7a08d04SJames Feist                             messages::internalError(asyncResp->res);
491b7a08d04SJames Feist                             return;
492b7a08d04SJames Feist                         }
493b7a08d04SJames Feist                         (*config)[propertyPair.first] = *ptr;
494b7a08d04SJames Feist                     }
495b7a08d04SJames Feist 
496b7a08d04SJames Feist                     if (intfPair.first == stepwiseConfigurationIface)
497b7a08d04SJames Feist                     {
498b7a08d04SJames Feist                         if (propertyPair.first == "Reading" ||
499b7a08d04SJames Feist                             propertyPair.first == "Output")
500b7a08d04SJames Feist                         {
501b7a08d04SJames Feist                             const std::vector<double>* ptr =
502abf2add6SEd Tanous                                 std::get_if<std::vector<double>>(
503b7a08d04SJames Feist                                     &propertyPair.second);
504b7a08d04SJames Feist 
505b7a08d04SJames Feist                             if (ptr == nullptr)
506b7a08d04SJames Feist                             {
507b7a08d04SJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
508b7a08d04SJames Feist                                                  << propertyPair.first;
509b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
510b7a08d04SJames Feist                                 return;
511b7a08d04SJames Feist                             }
512b7a08d04SJames Feist 
513b7a08d04SJames Feist                             if (propertyPair.first == "Reading")
514b7a08d04SJames Feist                             {
515b7a08d04SJames Feist                                 keys = ptr;
516b7a08d04SJames Feist                             }
517b7a08d04SJames Feist                             else
518b7a08d04SJames Feist                             {
519b7a08d04SJames Feist                                 values = ptr;
520b7a08d04SJames Feist                             }
521e662eae8SEd Tanous                             if (keys != nullptr && values != nullptr)
522b7a08d04SJames Feist                             {
523b7a08d04SJames Feist                                 if (keys->size() != values->size())
524b7a08d04SJames Feist                                 {
525b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR
5260fda0f12SGeorge Liu                                         << "Reading and Output size don't match ";
527b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
528b7a08d04SJames Feist                                     return;
529b7a08d04SJames Feist                                 }
530b7a08d04SJames Feist                                 nlohmann::json& steps = (*config)["Steps"];
531b7a08d04SJames Feist                                 steps = nlohmann::json::array();
532b7a08d04SJames Feist                                 for (size_t ii = 0; ii < keys->size(); ii++)
533b7a08d04SJames Feist                                 {
5341476687dSEd Tanous                                     nlohmann::json::object_t step;
5351476687dSEd Tanous                                     step["Target"] = (*keys)[ii];
5361476687dSEd Tanous                                     step["Output"] = (*values)[ii];
5371476687dSEd Tanous                                     steps.push_back(std::move(step));
538b7a08d04SJames Feist                                 }
539b7a08d04SJames Feist                             }
540b7a08d04SJames Feist                         }
541b7a08d04SJames Feist                         if (propertyPair.first == "NegativeHysteresis" ||
542b7a08d04SJames Feist                             propertyPair.first == "PositiveHysteresis")
543b7a08d04SJames Feist                         {
544b7a08d04SJames Feist                             const double* ptr =
545abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
546b7a08d04SJames Feist                             if (ptr == nullptr)
547b7a08d04SJames Feist                             {
548b7a08d04SJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
549b7a08d04SJames Feist                                                  << propertyPair.first;
550b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
551b7a08d04SJames Feist                                 return;
552b7a08d04SJames Feist                             }
553b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
554b7a08d04SJames Feist                         }
555b7a08d04SJames Feist                     }
556b7a08d04SJames Feist 
557b7a08d04SJames Feist                     // pid and fans are off the same configuration
558b7a08d04SJames Feist                     if (intfPair.first == pidConfigurationIface ||
559b7a08d04SJames Feist                         intfPair.first == stepwiseConfigurationIface)
560b7a08d04SJames Feist                     {
5615b4aa86bSJames Feist 
5625b4aa86bSJames Feist                         if (propertyPair.first == "Zones")
5635b4aa86bSJames Feist                         {
5645b4aa86bSJames Feist                             const std::vector<std::string>* inputs =
565abf2add6SEd Tanous                                 std::get_if<std::vector<std::string>>(
5661b6b96c5SEd Tanous                                     &propertyPair.second);
5675b4aa86bSJames Feist 
5685b4aa86bSJames Feist                             if (inputs == nullptr)
5695b4aa86bSJames Feist                             {
570002d39b4SEd Tanous                                 BMCWEB_LOG_ERROR << "Zones Pid Field Illegal";
571a08b46ccSJason M. Bills                                 messages::internalError(asyncResp->res);
5725b4aa86bSJames Feist                                 return;
5735b4aa86bSJames Feist                             }
574b7a08d04SJames Feist                             auto& data = (*config)[propertyPair.first];
5755b4aa86bSJames Feist                             data = nlohmann::json::array();
5765b4aa86bSJames Feist                             for (std::string itemCopy : *inputs)
5775b4aa86bSJames Feist                             {
5785b4aa86bSJames Feist                                 dbus::utility::escapePathForDbus(itemCopy);
5791476687dSEd Tanous                                 nlohmann::json::object_t input;
5801476687dSEd Tanous                                 input["@odata.id"] =
5810fda0f12SGeorge Liu                                     "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/" +
5821476687dSEd Tanous                                     itemCopy;
5831476687dSEd Tanous                                 data.push_back(std::move(input));
5845b4aa86bSJames Feist                             }
5855b4aa86bSJames Feist                         }
5865b4aa86bSJames Feist                         // todo(james): may never happen, but this
5875b4aa86bSJames Feist                         // assumes configuration data referenced in the
5885b4aa86bSJames Feist                         // PID config is provided by the same daemon, we
5895b4aa86bSJames Feist                         // could add another loop to cover all cases,
5905b4aa86bSJames Feist                         // but I'm okay kicking this can down the road a
5915b4aa86bSJames Feist                         // bit
5925b4aa86bSJames Feist 
5935b4aa86bSJames Feist                         else if (propertyPair.first == "Inputs" ||
5945b4aa86bSJames Feist                                  propertyPair.first == "Outputs")
5955b4aa86bSJames Feist                         {
596b7a08d04SJames Feist                             auto& data = (*config)[propertyPair.first];
5975b4aa86bSJames Feist                             const std::vector<std::string>* inputs =
598abf2add6SEd Tanous                                 std::get_if<std::vector<std::string>>(
5991b6b96c5SEd Tanous                                     &propertyPair.second);
6005b4aa86bSJames Feist 
6015b4aa86bSJames Feist                             if (inputs == nullptr)
6025b4aa86bSJames Feist                             {
6035b4aa86bSJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
6045b4aa86bSJames Feist                                                  << propertyPair.first;
605f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
6065b4aa86bSJames Feist                                 return;
6075b4aa86bSJames Feist                             }
6085b4aa86bSJames Feist                             data = *inputs;
609b943aaefSJames Feist                         }
610b943aaefSJames Feist                         else if (propertyPair.first == "SetPointOffset")
611b943aaefSJames Feist                         {
612b943aaefSJames Feist                             const std::string* ptr =
613002d39b4SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
614b943aaefSJames Feist 
615b943aaefSJames Feist                             if (ptr == nullptr)
616b943aaefSJames Feist                             {
617b943aaefSJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
618b943aaefSJames Feist                                                  << propertyPair.first;
619b943aaefSJames Feist                                 messages::internalError(asyncResp->res);
620b943aaefSJames Feist                                 return;
621b943aaefSJames Feist                             }
622b943aaefSJames Feist                             // translate from dbus to redfish
623b943aaefSJames Feist                             if (*ptr == "WarningHigh")
624b943aaefSJames Feist                             {
625b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
626b943aaefSJames Feist                                     "UpperThresholdNonCritical";
627b943aaefSJames Feist                             }
628b943aaefSJames Feist                             else if (*ptr == "WarningLow")
629b943aaefSJames Feist                             {
630b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
631b943aaefSJames Feist                                     "LowerThresholdNonCritical";
632b943aaefSJames Feist                             }
633b943aaefSJames Feist                             else if (*ptr == "CriticalHigh")
634b943aaefSJames Feist                             {
635b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
636b943aaefSJames Feist                                     "UpperThresholdCritical";
637b943aaefSJames Feist                             }
638b943aaefSJames Feist                             else if (*ptr == "CriticalLow")
639b943aaefSJames Feist                             {
640b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
641b943aaefSJames Feist                                     "LowerThresholdCritical";
642b943aaefSJames Feist                             }
643b943aaefSJames Feist                             else
644b943aaefSJames Feist                             {
645002d39b4SEd Tanous                                 BMCWEB_LOG_ERROR << "Value Illegal " << *ptr;
646b943aaefSJames Feist                                 messages::internalError(asyncResp->res);
647b943aaefSJames Feist                                 return;
648b943aaefSJames Feist                             }
649b943aaefSJames Feist                         }
650b943aaefSJames Feist                         // doubles
651002d39b4SEd Tanous                         else if (propertyPair.first == "FFGainCoefficient" ||
6525b4aa86bSJames Feist                                  propertyPair.first == "FFOffCoefficient" ||
6535b4aa86bSJames Feist                                  propertyPair.first == "ICoefficient" ||
6545b4aa86bSJames Feist                                  propertyPair.first == "ILimitMax" ||
6555b4aa86bSJames Feist                                  propertyPair.first == "ILimitMin" ||
656002d39b4SEd Tanous                                  propertyPair.first == "PositiveHysteresis" ||
657002d39b4SEd Tanous                                  propertyPair.first == "NegativeHysteresis" ||
6585b4aa86bSJames Feist                                  propertyPair.first == "OutLimitMax" ||
6595b4aa86bSJames Feist                                  propertyPair.first == "OutLimitMin" ||
6605b4aa86bSJames Feist                                  propertyPair.first == "PCoefficient" ||
6617625cb81SJames Feist                                  propertyPair.first == "SetPoint" ||
6625b4aa86bSJames Feist                                  propertyPair.first == "SlewNeg" ||
6635b4aa86bSJames Feist                                  propertyPair.first == "SlewPos")
6645b4aa86bSJames Feist                         {
6655b4aa86bSJames Feist                             const double* ptr =
666abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
6675b4aa86bSJames Feist                             if (ptr == nullptr)
6685b4aa86bSJames Feist                             {
6695b4aa86bSJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
6705b4aa86bSJames Feist                                                  << propertyPair.first;
671f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
6725b4aa86bSJames Feist                                 return;
6735b4aa86bSJames Feist                             }
674b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
6755b4aa86bSJames Feist                         }
6765b4aa86bSJames Feist                     }
6775b4aa86bSJames Feist                 }
6785b4aa86bSJames Feist             }
6795b4aa86bSJames Feist         }
6805b4aa86bSJames Feist         },
6815b4aa86bSJames Feist         connection, path, objectManagerIface, "GetManagedObjects");
6825b4aa86bSJames Feist }
683ca537928SJennifer Lee 
68483ff9ab6SJames Feist enum class CreatePIDRet
68583ff9ab6SJames Feist {
68683ff9ab6SJames Feist     fail,
68783ff9ab6SJames Feist     del,
68883ff9ab6SJames Feist     patch
68983ff9ab6SJames Feist };
69083ff9ab6SJames Feist 
6918d1b46d7Szhanghch05 inline bool
6928d1b46d7Szhanghch05     getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
6935f2caaefSJames Feist                         std::vector<nlohmann::json>& config,
6945f2caaefSJames Feist                         std::vector<std::string>& zones)
6955f2caaefSJames Feist {
696b6baeaa4SJames Feist     if (config.empty())
697b6baeaa4SJames Feist     {
698b6baeaa4SJames Feist         BMCWEB_LOG_ERROR << "Empty Zones";
6991668ce6dSEd Tanous         messages::propertyValueFormatError(response->res, "[]", "Zones");
700b6baeaa4SJames Feist         return false;
701b6baeaa4SJames Feist     }
7025f2caaefSJames Feist     for (auto& odata : config)
7035f2caaefSJames Feist     {
7045f2caaefSJames Feist         std::string path;
7055f2caaefSJames Feist         if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
7065f2caaefSJames Feist                                           path))
7075f2caaefSJames Feist         {
7085f2caaefSJames Feist             return false;
7095f2caaefSJames Feist         }
7105f2caaefSJames Feist         std::string input;
71161adbda3SJames Feist 
71261adbda3SJames Feist         // 8 below comes from
71361adbda3SJames Feist         // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
71461adbda3SJames Feist         //     0    1     2      3    4    5      6     7      8
71561adbda3SJames Feist         if (!dbus::utility::getNthStringFromPath(path, 8, input))
7165f2caaefSJames Feist         {
7175f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Got invalid path " << path;
7185f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Illegal Type Zones";
7195f2caaefSJames Feist             messages::propertyValueFormatError(response->res, odata.dump(),
7205f2caaefSJames Feist                                                "Zones");
7215f2caaefSJames Feist             return false;
7225f2caaefSJames Feist         }
723a170f275SEd Tanous         std::replace(input.begin(), input.end(), '_', ' ');
7245f2caaefSJames Feist         zones.emplace_back(std::move(input));
7255f2caaefSJames Feist     }
7265f2caaefSJames Feist     return true;
7275f2caaefSJames Feist }
7285f2caaefSJames Feist 
729711ac7a9SEd Tanous inline const dbus::utility::ManagedObjectType::value_type*
73073df0db0SJames Feist     findChassis(const dbus::utility::ManagedObjectType& managedObj,
731b6baeaa4SJames Feist                 const std::string& value, std::string& chassis)
732b6baeaa4SJames Feist {
733b6baeaa4SJames Feist     BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
734b6baeaa4SJames Feist 
735a170f275SEd Tanous     std::string escaped = value;
736a170f275SEd Tanous     std::replace(escaped.begin(), escaped.end(), '_', ' ');
737b6baeaa4SJames Feist     escaped = "/" + escaped;
738002d39b4SEd Tanous     auto it = std::find_if(managedObj.begin(), managedObj.end(),
739002d39b4SEd Tanous                            [&escaped](const auto& obj) {
740b6baeaa4SJames Feist         if (boost::algorithm::ends_with(obj.first.str, escaped))
741b6baeaa4SJames Feist         {
742b6baeaa4SJames Feist             BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
743b6baeaa4SJames Feist             return true;
744b6baeaa4SJames Feist         }
745b6baeaa4SJames Feist         return false;
746b6baeaa4SJames Feist     });
747b6baeaa4SJames Feist 
748b6baeaa4SJames Feist     if (it == managedObj.end())
749b6baeaa4SJames Feist     {
75073df0db0SJames Feist         return nullptr;
751b6baeaa4SJames Feist     }
752b6baeaa4SJames Feist     // 5 comes from <chassis-name> being the 5th element
753b6baeaa4SJames Feist     // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
75473df0db0SJames Feist     if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
75573df0db0SJames Feist     {
75673df0db0SJames Feist         return &(*it);
75773df0db0SJames Feist     }
75873df0db0SJames Feist 
75973df0db0SJames Feist     return nullptr;
760b6baeaa4SJames Feist }
761b6baeaa4SJames Feist 
76223a21a1cSEd Tanous inline CreatePIDRet createPidInterface(
7638d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
764b5a76932SEd Tanous     const nlohmann::json::iterator& it, const std::string& path,
76583ff9ab6SJames Feist     const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
766b9d36b47SEd Tanous     dbus::utility::DBusPropertiesMap& output, std::string& chassis,
767b9d36b47SEd Tanous     const std::string& profile)
76883ff9ab6SJames Feist {
76983ff9ab6SJames Feist 
7705f2caaefSJames Feist     // common deleter
771b6baeaa4SJames Feist     if (it.value() == nullptr)
7725f2caaefSJames Feist     {
7735f2caaefSJames Feist         std::string iface;
7745f2caaefSJames Feist         if (type == "PidControllers" || type == "FanControllers")
7755f2caaefSJames Feist         {
7765f2caaefSJames Feist             iface = pidConfigurationIface;
7775f2caaefSJames Feist         }
7785f2caaefSJames Feist         else if (type == "FanZones")
7795f2caaefSJames Feist         {
7805f2caaefSJames Feist             iface = pidZoneConfigurationIface;
7815f2caaefSJames Feist         }
7825f2caaefSJames Feist         else if (type == "StepwiseControllers")
7835f2caaefSJames Feist         {
7845f2caaefSJames Feist             iface = stepwiseConfigurationIface;
7855f2caaefSJames Feist         }
7865f2caaefSJames Feist         else
7875f2caaefSJames Feist         {
788a0744d38SGunnar Mills             BMCWEB_LOG_ERROR << "Illegal Type " << type;
7895f2caaefSJames Feist             messages::propertyUnknown(response->res, type);
7905f2caaefSJames Feist             return CreatePIDRet::fail;
7915f2caaefSJames Feist         }
7926ee7f774SJames Feist 
7936ee7f774SJames Feist         BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
7945f2caaefSJames Feist         // delete interface
7955f2caaefSJames Feist         crow::connections::systemBus->async_method_call(
7965f2caaefSJames Feist             [response, path](const boost::system::error_code ec) {
7975f2caaefSJames Feist             if (ec)
7985f2caaefSJames Feist             {
7995f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
8005f2caaefSJames Feist                 messages::internalError(response->res);
801b6baeaa4SJames Feist                 return;
8025f2caaefSJames Feist             }
803b6baeaa4SJames Feist             messages::success(response->res);
8045f2caaefSJames Feist             },
8055f2caaefSJames Feist             "xyz.openbmc_project.EntityManager", path, iface, "Delete");
8065f2caaefSJames Feist         return CreatePIDRet::del;
8075f2caaefSJames Feist     }
8085f2caaefSJames Feist 
809711ac7a9SEd Tanous     const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
810b6baeaa4SJames Feist     if (!createNewObject)
811b6baeaa4SJames Feist     {
812b6baeaa4SJames Feist         // if we aren't creating a new object, we should be able to find it on
813b6baeaa4SJames Feist         // d-bus
81473df0db0SJames Feist         managedItem = findChassis(managedObj, it.key(), chassis);
81573df0db0SJames Feist         if (managedItem == nullptr)
816b6baeaa4SJames Feist         {
817b6baeaa4SJames Feist             BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
818ace85d60SEd Tanous             messages::invalidObject(response->res,
819ace85d60SEd Tanous                                     crow::utility::urlFromPieces(
820ace85d60SEd Tanous                                         "redfish", "v1", "Chassis", chassis));
821b6baeaa4SJames Feist             return CreatePIDRet::fail;
822b6baeaa4SJames Feist         }
823b6baeaa4SJames Feist     }
824b6baeaa4SJames Feist 
82526f6976fSEd Tanous     if (!profile.empty() &&
82673df0db0SJames Feist         (type == "PidControllers" || type == "FanControllers" ||
82773df0db0SJames Feist          type == "StepwiseControllers"))
82873df0db0SJames Feist     {
82973df0db0SJames Feist         if (managedItem == nullptr)
83073df0db0SJames Feist         {
831b9d36b47SEd Tanous             output.emplace_back("Profiles", std::vector<std::string>{profile});
83273df0db0SJames Feist         }
83373df0db0SJames Feist         else
83473df0db0SJames Feist         {
83573df0db0SJames Feist             std::string interface;
83673df0db0SJames Feist             if (type == "StepwiseControllers")
83773df0db0SJames Feist             {
83873df0db0SJames Feist                 interface = stepwiseConfigurationIface;
83973df0db0SJames Feist             }
84073df0db0SJames Feist             else
84173df0db0SJames Feist             {
84273df0db0SJames Feist                 interface = pidConfigurationIface;
84373df0db0SJames Feist             }
844711ac7a9SEd Tanous             bool ifaceFound = false;
845711ac7a9SEd Tanous             for (const auto& iface : managedItem->second)
846711ac7a9SEd Tanous             {
847711ac7a9SEd Tanous                 if (iface.first == interface)
848711ac7a9SEd Tanous                 {
849711ac7a9SEd Tanous                     ifaceFound = true;
850711ac7a9SEd Tanous                     for (const auto& prop : iface.second)
851711ac7a9SEd Tanous                     {
852711ac7a9SEd Tanous                         if (prop.first == "Profiles")
853711ac7a9SEd Tanous                         {
854711ac7a9SEd Tanous                             const std::vector<std::string>* curProfiles =
855711ac7a9SEd Tanous                                 std::get_if<std::vector<std::string>>(
856711ac7a9SEd Tanous                                     &(prop.second));
857711ac7a9SEd Tanous                             if (curProfiles == nullptr)
858711ac7a9SEd Tanous                             {
859711ac7a9SEd Tanous                                 BMCWEB_LOG_ERROR
860711ac7a9SEd Tanous                                     << "Illegal profiles in managed object";
861711ac7a9SEd Tanous                                 messages::internalError(response->res);
862711ac7a9SEd Tanous                                 return CreatePIDRet::fail;
863711ac7a9SEd Tanous                             }
864711ac7a9SEd Tanous                             if (std::find(curProfiles->begin(),
865711ac7a9SEd Tanous                                           curProfiles->end(),
866711ac7a9SEd Tanous                                           profile) == curProfiles->end())
867711ac7a9SEd Tanous                             {
868711ac7a9SEd Tanous                                 std::vector<std::string> newProfiles =
869711ac7a9SEd Tanous                                     *curProfiles;
870711ac7a9SEd Tanous                                 newProfiles.push_back(profile);
871b9d36b47SEd Tanous                                 output.emplace_back("Profiles", newProfiles);
872711ac7a9SEd Tanous                             }
873711ac7a9SEd Tanous                         }
874711ac7a9SEd Tanous                     }
875711ac7a9SEd Tanous                 }
876711ac7a9SEd Tanous             }
877711ac7a9SEd Tanous 
878711ac7a9SEd Tanous             if (!ifaceFound)
87973df0db0SJames Feist             {
88073df0db0SJames Feist                 BMCWEB_LOG_ERROR
88173df0db0SJames Feist                     << "Failed to find interface in managed object";
88273df0db0SJames Feist                 messages::internalError(response->res);
88373df0db0SJames Feist                 return CreatePIDRet::fail;
88473df0db0SJames Feist             }
88573df0db0SJames Feist         }
88673df0db0SJames Feist     }
88773df0db0SJames Feist 
88883ff9ab6SJames Feist     if (type == "PidControllers" || type == "FanControllers")
88983ff9ab6SJames Feist     {
89083ff9ab6SJames Feist         if (createNewObject)
89183ff9ab6SJames Feist         {
892b9d36b47SEd Tanous             output.emplace_back("Class",
893b9d36b47SEd Tanous                                 type == "PidControllers" ? "temp" : "fan");
894b9d36b47SEd Tanous             output.emplace_back("Type", "Pid");
89583ff9ab6SJames Feist         }
8965f2caaefSJames Feist 
8975f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
8985f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
8995f2caaefSJames Feist         std::optional<std::vector<std::string>> outputs;
9005f2caaefSJames Feist         std::map<std::string, std::optional<double>> doubles;
901b943aaefSJames Feist         std::optional<std::string> setpointOffset;
9025f2caaefSJames Feist         if (!redfish::json_util::readJson(
903b6baeaa4SJames Feist                 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
9045f2caaefSJames Feist                 "Zones", zones, "FFGainCoefficient",
9055f2caaefSJames Feist                 doubles["FFGainCoefficient"], "FFOffCoefficient",
9065f2caaefSJames Feist                 doubles["FFOffCoefficient"], "ICoefficient",
9075f2caaefSJames Feist                 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
9085f2caaefSJames Feist                 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
9095f2caaefSJames Feist                 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
9105f2caaefSJames Feist                 "PCoefficient", doubles["PCoefficient"], "SetPoint",
911b943aaefSJames Feist                 doubles["SetPoint"], "SetPointOffset", setpointOffset,
912b943aaefSJames Feist                 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
913b943aaefSJames Feist                 "PositiveHysteresis", doubles["PositiveHysteresis"],
914b943aaefSJames Feist                 "NegativeHysteresis", doubles["NegativeHysteresis"]))
91583ff9ab6SJames Feist         {
91671f52d96SEd Tanous             BMCWEB_LOG_ERROR
91771f52d96SEd Tanous                 << "Illegal Property "
91871f52d96SEd Tanous                 << it.value().dump(2, ' ', true,
91971f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace);
9205f2caaefSJames Feist             return CreatePIDRet::fail;
92183ff9ab6SJames Feist         }
9225f2caaefSJames Feist         if (zones)
9235f2caaefSJames Feist         {
9245f2caaefSJames Feist             std::vector<std::string> zonesStr;
9255f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStr))
9265f2caaefSJames Feist             {
927a0744d38SGunnar Mills                 BMCWEB_LOG_ERROR << "Illegal Zones";
9285f2caaefSJames Feist                 return CreatePIDRet::fail;
9295f2caaefSJames Feist             }
930b6baeaa4SJames Feist             if (chassis.empty() &&
931e662eae8SEd Tanous                 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
932b6baeaa4SJames Feist             {
933b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
934ace85d60SEd Tanous                 messages::invalidObject(
935ace85d60SEd Tanous                     response->res, crow::utility::urlFromPieces(
936ace85d60SEd Tanous                                        "redfish", "v1", "Chassis", chassis));
937b6baeaa4SJames Feist                 return CreatePIDRet::fail;
938b6baeaa4SJames Feist             }
939b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStr));
9405f2caaefSJames Feist         }
9415f2caaefSJames Feist         if (inputs || outputs)
9425f2caaefSJames Feist         {
94302cad96eSEd Tanous             std::array<
94402cad96eSEd Tanous                 std::reference_wrapper<std::optional<std::vector<std::string>>>,
94502cad96eSEd Tanous                 2>
94602cad96eSEd Tanous                 containers = {inputs, outputs};
9475f2caaefSJames Feist             size_t index = 0;
94802cad96eSEd Tanous             for (std::optional<std::vector<std::string>>& container :
94902cad96eSEd Tanous                  containers)
9505f2caaefSJames Feist             {
9515f2caaefSJames Feist                 if (!container)
9525f2caaefSJames Feist                 {
9535f2caaefSJames Feist                     index++;
9545f2caaefSJames Feist                     continue;
95583ff9ab6SJames Feist                 }
9565f2caaefSJames Feist                 for (std::string& value : *container)
95783ff9ab6SJames Feist                 {
958a170f275SEd Tanous                     std::replace(value.begin(), value.end(), '_', ' ');
95983ff9ab6SJames Feist                 }
9605f2caaefSJames Feist                 std::string key;
9615f2caaefSJames Feist                 if (index == 0)
9625f2caaefSJames Feist                 {
9635f2caaefSJames Feist                     key = "Inputs";
9645f2caaefSJames Feist                 }
9655f2caaefSJames Feist                 else
9665f2caaefSJames Feist                 {
9675f2caaefSJames Feist                     key = "Outputs";
9685f2caaefSJames Feist                 }
969b9d36b47SEd Tanous                 output.emplace_back(key, *container);
9705f2caaefSJames Feist                 index++;
9715f2caaefSJames Feist             }
97283ff9ab6SJames Feist         }
97383ff9ab6SJames Feist 
974b943aaefSJames Feist         if (setpointOffset)
975b943aaefSJames Feist         {
976b943aaefSJames Feist             // translate between redfish and dbus names
977b943aaefSJames Feist             if (*setpointOffset == "UpperThresholdNonCritical")
978b943aaefSJames Feist             {
979b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningLow");
980b943aaefSJames Feist             }
981b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdNonCritical")
982b943aaefSJames Feist             {
983b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningHigh");
984b943aaefSJames Feist             }
985b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdCritical")
986b943aaefSJames Feist             {
987b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalLow");
988b943aaefSJames Feist             }
989b943aaefSJames Feist             else if (*setpointOffset == "UpperThresholdCritical")
990b943aaefSJames Feist             {
991b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalHigh");
992b943aaefSJames Feist             }
993b943aaefSJames Feist             else
994b943aaefSJames Feist             {
995b943aaefSJames Feist                 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
996b943aaefSJames Feist                                  << *setpointOffset;
997ace85d60SEd Tanous                 messages::propertyValueNotInList(response->res, it.key(),
998ace85d60SEd Tanous                                                  "SetPointOffset");
999b943aaefSJames Feist                 return CreatePIDRet::fail;
1000b943aaefSJames Feist             }
1001b943aaefSJames Feist         }
1002b943aaefSJames Feist 
100383ff9ab6SJames Feist         // doubles
10045f2caaefSJames Feist         for (const auto& pairs : doubles)
100583ff9ab6SJames Feist         {
10065f2caaefSJames Feist             if (!pairs.second)
100783ff9ab6SJames Feist             {
10085f2caaefSJames Feist                 continue;
100983ff9ab6SJames Feist             }
10105f2caaefSJames Feist             BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
1011b9d36b47SEd Tanous             output.emplace_back(pairs.first, *pairs.second);
10125f2caaefSJames Feist         }
101383ff9ab6SJames Feist     }
101483ff9ab6SJames Feist 
101583ff9ab6SJames Feist     else if (type == "FanZones")
101683ff9ab6SJames Feist     {
1017b9d36b47SEd Tanous         output.emplace_back("Type", "Pid.Zone");
101883ff9ab6SJames Feist 
10195f2caaefSJames Feist         std::optional<nlohmann::json> chassisContainer;
10205f2caaefSJames Feist         std::optional<double> failSafePercent;
1021d3ec07f8SJames Feist         std::optional<double> minThermalOutput;
1022b6baeaa4SJames Feist         if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
10235f2caaefSJames Feist                                           chassisContainer, "FailSafePercent",
1024d3ec07f8SJames Feist                                           failSafePercent, "MinThermalOutput",
1025d3ec07f8SJames Feist                                           minThermalOutput))
102683ff9ab6SJames Feist         {
102771f52d96SEd Tanous             BMCWEB_LOG_ERROR
102871f52d96SEd Tanous                 << "Illegal Property "
102971f52d96SEd Tanous                 << it.value().dump(2, ' ', true,
103071f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace);
103183ff9ab6SJames Feist             return CreatePIDRet::fail;
103283ff9ab6SJames Feist         }
10335f2caaefSJames Feist 
10345f2caaefSJames Feist         if (chassisContainer)
103583ff9ab6SJames Feist         {
10365f2caaefSJames Feist 
10375f2caaefSJames Feist             std::string chassisId;
10385f2caaefSJames Feist             if (!redfish::json_util::readJson(*chassisContainer, response->res,
10395f2caaefSJames Feist                                               "@odata.id", chassisId))
10405f2caaefSJames Feist             {
104171f52d96SEd Tanous                 BMCWEB_LOG_ERROR
104271f52d96SEd Tanous                     << "Illegal Property "
104371f52d96SEd Tanous                     << chassisContainer->dump(
104471f52d96SEd Tanous                            2, ' ', true,
104571f52d96SEd Tanous                            nlohmann::json::error_handler_t::replace);
104683ff9ab6SJames Feist                 return CreatePIDRet::fail;
104783ff9ab6SJames Feist             }
104883ff9ab6SJames Feist 
1049717794d5SAppaRao Puli             // /redfish/v1/chassis/chassis_name/
10505f2caaefSJames Feist             if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
105183ff9ab6SJames Feist             {
10525f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
1053ace85d60SEd Tanous                 messages::invalidObject(
1054ace85d60SEd Tanous                     response->res, crow::utility::urlFromPieces(
1055ace85d60SEd Tanous                                        "redfish", "v1", "Chassis", chassisId));
105683ff9ab6SJames Feist                 return CreatePIDRet::fail;
105783ff9ab6SJames Feist             }
105883ff9ab6SJames Feist         }
1059d3ec07f8SJames Feist         if (minThermalOutput)
106083ff9ab6SJames Feist         {
1061b9d36b47SEd Tanous             output.emplace_back("MinThermalOutput", *minThermalOutput);
10625f2caaefSJames Feist         }
10635f2caaefSJames Feist         if (failSafePercent)
106483ff9ab6SJames Feist         {
1065b9d36b47SEd Tanous             output.emplace_back("FailSafePercent", *failSafePercent);
10665f2caaefSJames Feist         }
10675f2caaefSJames Feist     }
10685f2caaefSJames Feist     else if (type == "StepwiseControllers")
10695f2caaefSJames Feist     {
1070b9d36b47SEd Tanous         output.emplace_back("Type", "Stepwise");
10715f2caaefSJames Feist 
10725f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
10735f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> steps;
10745f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
10755f2caaefSJames Feist         std::optional<double> positiveHysteresis;
10765f2caaefSJames Feist         std::optional<double> negativeHysteresis;
1077c33a90ecSJames Feist         std::optional<std::string> direction; // upper clipping curve vs lower
10785f2caaefSJames Feist         if (!redfish::json_util::readJson(
1079b6baeaa4SJames Feist                 it.value(), response->res, "Zones", zones, "Steps", steps,
1080b6baeaa4SJames Feist                 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
1081c33a90ecSJames Feist                 "NegativeHysteresis", negativeHysteresis, "Direction",
1082c33a90ecSJames Feist                 direction))
10835f2caaefSJames Feist         {
108471f52d96SEd Tanous             BMCWEB_LOG_ERROR
108571f52d96SEd Tanous                 << "Illegal Property "
108671f52d96SEd Tanous                 << it.value().dump(2, ' ', true,
108771f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace);
108883ff9ab6SJames Feist             return CreatePIDRet::fail;
108983ff9ab6SJames Feist         }
10905f2caaefSJames Feist 
10915f2caaefSJames Feist         if (zones)
109283ff9ab6SJames Feist         {
1093b6baeaa4SJames Feist             std::vector<std::string> zonesStrs;
1094b6baeaa4SJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStrs))
10955f2caaefSJames Feist             {
1096a0744d38SGunnar Mills                 BMCWEB_LOG_ERROR << "Illegal Zones";
109783ff9ab6SJames Feist                 return CreatePIDRet::fail;
109883ff9ab6SJames Feist             }
1099b6baeaa4SJames Feist             if (chassis.empty() &&
1100e662eae8SEd Tanous                 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
1101b6baeaa4SJames Feist             {
1102b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
1103ace85d60SEd Tanous                 messages::invalidObject(
1104ace85d60SEd Tanous                     response->res, crow::utility::urlFromPieces(
1105ace85d60SEd Tanous                                        "redfish", "v1", "Chassis", chassis));
1106b6baeaa4SJames Feist                 return CreatePIDRet::fail;
1107b6baeaa4SJames Feist             }
1108b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStrs));
11095f2caaefSJames Feist         }
11105f2caaefSJames Feist         if (steps)
11115f2caaefSJames Feist         {
11125f2caaefSJames Feist             std::vector<double> readings;
11135f2caaefSJames Feist             std::vector<double> outputs;
11145f2caaefSJames Feist             for (auto& step : *steps)
11155f2caaefSJames Feist             {
1116543f4400SEd Tanous                 double target = 0.0;
1117543f4400SEd Tanous                 double out = 0.0;
11185f2caaefSJames Feist 
11195f2caaefSJames Feist                 if (!redfish::json_util::readJson(step, response->res, "Target",
112023a21a1cSEd Tanous                                                   target, "Output", out))
11215f2caaefSJames Feist                 {
112271f52d96SEd Tanous                     BMCWEB_LOG_ERROR
112371f52d96SEd Tanous                         << "Illegal Property "
112471f52d96SEd Tanous                         << it.value().dump(
112571f52d96SEd Tanous                                2, ' ', true,
112671f52d96SEd Tanous                                nlohmann::json::error_handler_t::replace);
11275f2caaefSJames Feist                     return CreatePIDRet::fail;
11285f2caaefSJames Feist                 }
11295f2caaefSJames Feist                 readings.emplace_back(target);
113023a21a1cSEd Tanous                 outputs.emplace_back(out);
11315f2caaefSJames Feist             }
1132b9d36b47SEd Tanous             output.emplace_back("Reading", std::move(readings));
1133b9d36b47SEd Tanous             output.emplace_back("Output", std::move(outputs));
11345f2caaefSJames Feist         }
11355f2caaefSJames Feist         if (inputs)
11365f2caaefSJames Feist         {
11375f2caaefSJames Feist             for (std::string& value : *inputs)
11385f2caaefSJames Feist             {
1139a170f275SEd Tanous 
1140a170f275SEd Tanous                 std::replace(value.begin(), value.end(), '_', ' ');
11415f2caaefSJames Feist             }
1142b9d36b47SEd Tanous             output.emplace_back("Inputs", std::move(*inputs));
11435f2caaefSJames Feist         }
11445f2caaefSJames Feist         if (negativeHysteresis)
11455f2caaefSJames Feist         {
1146b9d36b47SEd Tanous             output.emplace_back("NegativeHysteresis", *negativeHysteresis);
11475f2caaefSJames Feist         }
11485f2caaefSJames Feist         if (positiveHysteresis)
11495f2caaefSJames Feist         {
1150b9d36b47SEd Tanous             output.emplace_back("PositiveHysteresis", *positiveHysteresis);
115183ff9ab6SJames Feist         }
1152c33a90ecSJames Feist         if (direction)
1153c33a90ecSJames Feist         {
1154c33a90ecSJames Feist             constexpr const std::array<const char*, 2> allowedDirections = {
1155c33a90ecSJames Feist                 "Ceiling", "Floor"};
1156c33a90ecSJames Feist             if (std::find(allowedDirections.begin(), allowedDirections.end(),
1157c33a90ecSJames Feist                           *direction) == allowedDirections.end())
1158c33a90ecSJames Feist             {
1159c33a90ecSJames Feist                 messages::propertyValueTypeError(response->res, "Direction",
1160c33a90ecSJames Feist                                                  *direction);
1161c33a90ecSJames Feist                 return CreatePIDRet::fail;
1162c33a90ecSJames Feist             }
1163b9d36b47SEd Tanous             output.emplace_back("Class", *direction);
1164c33a90ecSJames Feist         }
116583ff9ab6SJames Feist     }
116683ff9ab6SJames Feist     else
116783ff9ab6SJames Feist     {
1168a0744d38SGunnar Mills         BMCWEB_LOG_ERROR << "Illegal Type " << type;
116935a62c7cSJason M. Bills         messages::propertyUnknown(response->res, type);
117083ff9ab6SJames Feist         return CreatePIDRet::fail;
117183ff9ab6SJames Feist     }
117283ff9ab6SJames Feist     return CreatePIDRet::patch;
117383ff9ab6SJames Feist }
117473df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
117573df0db0SJames Feist {
11766936afe4SEd Tanous     struct CompletionValues
11776936afe4SEd Tanous     {
11786936afe4SEd Tanous         std::vector<std::string> supportedProfiles;
11796936afe4SEd Tanous         std::string currentProfile;
11806936afe4SEd Tanous         dbus::utility::MapperGetSubTreeResponse subtree;
11816936afe4SEd Tanous     };
118283ff9ab6SJames Feist 
11834e23a444SEd Tanous     explicit GetPIDValues(
11844e23a444SEd Tanous         const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
118523a21a1cSEd Tanous         asyncResp(asyncRespIn)
118673df0db0SJames Feist 
11871214b7e7SGunnar Mills     {}
11889c310685SBorawski.Lukasz 
118973df0db0SJames Feist     void run()
11905b4aa86bSJames Feist     {
119173df0db0SJames Feist         std::shared_ptr<GetPIDValues> self = shared_from_this();
119273df0db0SJames Feist 
119373df0db0SJames Feist         // get all configurations
11945b4aa86bSJames Feist         crow::connections::systemBus->async_method_call(
1195b9d36b47SEd Tanous             [self](
1196b9d36b47SEd Tanous                 const boost::system::error_code ec,
1197b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
11985b4aa86bSJames Feist             if (ec)
11995b4aa86bSJames Feist             {
12005b4aa86bSJames Feist                 BMCWEB_LOG_ERROR << ec;
120173df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
120273df0db0SJames Feist                 return;
120373df0db0SJames Feist             }
12046936afe4SEd Tanous             self->complete.subtree = subtreeLocal;
120573df0db0SJames Feist             },
120673df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
120773df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
120873df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
120973df0db0SJames Feist             std::array<const char*, 4>{
121073df0db0SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
121173df0db0SJames Feist                 objectManagerIface, stepwiseConfigurationIface});
121273df0db0SJames Feist 
121373df0db0SJames Feist         // at the same time get the selected profile
121473df0db0SJames Feist         crow::connections::systemBus->async_method_call(
1215b9d36b47SEd Tanous             [self](
1216b9d36b47SEd Tanous                 const boost::system::error_code ec,
1217b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
121823a21a1cSEd Tanous             if (ec || subtreeLocal.empty())
121973df0db0SJames Feist             {
122073df0db0SJames Feist                 return;
122173df0db0SJames Feist             }
122223a21a1cSEd Tanous             if (subtreeLocal[0].second.size() != 1)
122373df0db0SJames Feist             {
122473df0db0SJames Feist                 // invalid mapper response, should never happen
122573df0db0SJames Feist                 BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
122673df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
12275b4aa86bSJames Feist                 return;
12285b4aa86bSJames Feist             }
12295b4aa86bSJames Feist 
123023a21a1cSEd Tanous             const std::string& path = subtreeLocal[0].first;
123123a21a1cSEd Tanous             const std::string& owner = subtreeLocal[0].second[0].first;
1232fac6e53bSKrzysztof Grobelny 
1233fac6e53bSKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
1234fac6e53bSKrzysztof Grobelny                 *crow::connections::systemBus, owner, path, thermalModeIface,
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
1241002d39b4SEd Tanous                         << "GetPIDValues: Can't get thermalModeIface " << path;
124273df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
124373df0db0SJames Feist                     return;
124473df0db0SJames Feist                 }
1245fac6e53bSKrzysztof Grobelny 
1246271584abSEd Tanous                 const std::string* current = nullptr;
1247271584abSEd Tanous                 const std::vector<std::string>* supported = nullptr;
1248fac6e53bSKrzysztof Grobelny 
1249fac6e53bSKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
1250fac6e53bSKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), resp, "Current", current,
1251fac6e53bSKrzysztof Grobelny                     "Supported", supported);
1252fac6e53bSKrzysztof Grobelny 
1253fac6e53bSKrzysztof Grobelny                 if (!success)
125473df0db0SJames Feist                 {
1255002d39b4SEd Tanous                     messages::internalError(self->asyncResp->res);
125673df0db0SJames Feist                     return;
125773df0db0SJames Feist                 }
1258fac6e53bSKrzysztof Grobelny 
125973df0db0SJames Feist                 if (current == nullptr || supported == nullptr)
126073df0db0SJames Feist                 {
12610fda0f12SGeorge Liu                     BMCWEB_LOG_ERROR
1262002d39b4SEd Tanous                         << "GetPIDValues: thermal mode iface invalid " << path;
126373df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
126473df0db0SJames Feist                     return;
126573df0db0SJames Feist                 }
12666936afe4SEd Tanous                 self->complete.currentProfile = *current;
12676936afe4SEd Tanous                 self->complete.supportedProfiles = *supported;
1268fac6e53bSKrzysztof Grobelny                 });
126973df0db0SJames Feist             },
127073df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
127173df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
127273df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
127373df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
127473df0db0SJames Feist     }
127573df0db0SJames Feist 
12766936afe4SEd Tanous     static void
12776936afe4SEd Tanous         processingComplete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12786936afe4SEd Tanous                            const CompletionValues& completion)
127973df0db0SJames Feist     {
128073df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
128173df0db0SJames Feist         {
128273df0db0SJames Feist             return;
128373df0db0SJames Feist         }
12845b4aa86bSJames Feist         // create map of <connection, path to objMgr>>
12856936afe4SEd Tanous         boost::container::flat_map<
12866936afe4SEd Tanous             std::string, std::string, std::less<>,
12876936afe4SEd Tanous             std::vector<std::pair<std::string, std::string>>>
12886936afe4SEd Tanous             objectMgrPaths;
12896936afe4SEd Tanous         boost::container::flat_set<std::string, std::less<>,
12906936afe4SEd Tanous                                    std::vector<std::string>>
12916936afe4SEd Tanous             calledConnections;
12926936afe4SEd Tanous         for (const auto& pathGroup : completion.subtree)
12935b4aa86bSJames Feist         {
12945b4aa86bSJames Feist             for (const auto& connectionGroup : pathGroup.second)
12955b4aa86bSJames Feist             {
12966bce33bcSJames Feist                 auto findConnection =
12976bce33bcSJames Feist                     calledConnections.find(connectionGroup.first);
12986bce33bcSJames Feist                 if (findConnection != calledConnections.end())
12996bce33bcSJames Feist                 {
13006bce33bcSJames Feist                     break;
13016bce33bcSJames Feist                 }
130273df0db0SJames Feist                 for (const std::string& interface : connectionGroup.second)
13035b4aa86bSJames Feist                 {
13045b4aa86bSJames Feist                     if (interface == objectManagerIface)
13055b4aa86bSJames Feist                     {
130673df0db0SJames Feist                         objectMgrPaths[connectionGroup.first] = pathGroup.first;
13075b4aa86bSJames Feist                     }
13085b4aa86bSJames Feist                     // this list is alphabetical, so we
13095b4aa86bSJames Feist                     // should have found the objMgr by now
13105b4aa86bSJames Feist                     if (interface == pidConfigurationIface ||
1311b7a08d04SJames Feist                         interface == pidZoneConfigurationIface ||
1312b7a08d04SJames Feist                         interface == stepwiseConfigurationIface)
13135b4aa86bSJames Feist                     {
13145b4aa86bSJames Feist                         auto findObjMgr =
13155b4aa86bSJames Feist                             objectMgrPaths.find(connectionGroup.first);
13165b4aa86bSJames Feist                         if (findObjMgr == objectMgrPaths.end())
13175b4aa86bSJames Feist                         {
13185b4aa86bSJames Feist                             BMCWEB_LOG_DEBUG << connectionGroup.first
13195b4aa86bSJames Feist                                              << "Has no Object Manager";
13205b4aa86bSJames Feist                             continue;
13215b4aa86bSJames Feist                         }
13226bce33bcSJames Feist 
13236bce33bcSJames Feist                         calledConnections.insert(connectionGroup.first);
13246bce33bcSJames Feist 
132573df0db0SJames Feist                         asyncPopulatePid(findObjMgr->first, findObjMgr->second,
13266936afe4SEd Tanous                                          completion.currentProfile,
13276936afe4SEd Tanous                                          completion.supportedProfiles,
132873df0db0SJames Feist                                          asyncResp);
13295b4aa86bSJames Feist                         break;
13305b4aa86bSJames Feist                     }
13315b4aa86bSJames Feist                 }
13325b4aa86bSJames Feist             }
13335b4aa86bSJames Feist         }
133473df0db0SJames Feist     }
133573df0db0SJames Feist 
13366936afe4SEd Tanous     ~GetPIDValues()
13376936afe4SEd Tanous     {
13386936afe4SEd Tanous         boost::asio::post(crow::connections::systemBus->get_io_context(),
13396936afe4SEd Tanous                           std::bind_front(&processingComplete, asyncResp,
13406936afe4SEd Tanous                                           std::move(complete)));
13416936afe4SEd Tanous     }
13426936afe4SEd Tanous 
1343ecd6a3a2SEd Tanous     GetPIDValues(const GetPIDValues&) = delete;
1344ecd6a3a2SEd Tanous     GetPIDValues(GetPIDValues&&) = delete;
1345ecd6a3a2SEd Tanous     GetPIDValues& operator=(const GetPIDValues&) = delete;
1346ecd6a3a2SEd Tanous     GetPIDValues& operator=(GetPIDValues&&) = delete;
1347ecd6a3a2SEd Tanous 
13488d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
13496936afe4SEd Tanous     CompletionValues complete;
135073df0db0SJames Feist };
135173df0db0SJames Feist 
135273df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
135373df0db0SJames Feist {
135473df0db0SJames Feist 
13558d1b46d7Szhanghch05     SetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
135673df0db0SJames Feist                  nlohmann::json& data) :
1357271584abSEd Tanous         asyncResp(asyncRespIn)
135873df0db0SJames Feist     {
135973df0db0SJames Feist 
136073df0db0SJames Feist         std::optional<nlohmann::json> pidControllers;
136173df0db0SJames Feist         std::optional<nlohmann::json> fanControllers;
136273df0db0SJames Feist         std::optional<nlohmann::json> fanZones;
136373df0db0SJames Feist         std::optional<nlohmann::json> stepwiseControllers;
136473df0db0SJames Feist 
136573df0db0SJames Feist         if (!redfish::json_util::readJson(
136673df0db0SJames Feist                 data, asyncResp->res, "PidControllers", pidControllers,
136773df0db0SJames Feist                 "FanControllers", fanControllers, "FanZones", fanZones,
136873df0db0SJames Feist                 "StepwiseControllers", stepwiseControllers, "Profile", profile))
136973df0db0SJames Feist         {
137071f52d96SEd Tanous             BMCWEB_LOG_ERROR
137171f52d96SEd Tanous                 << "Illegal Property "
137271f52d96SEd Tanous                 << data.dump(2, ' ', true,
137371f52d96SEd Tanous                              nlohmann::json::error_handler_t::replace);
137473df0db0SJames Feist             return;
137573df0db0SJames Feist         }
137673df0db0SJames Feist         configuration.emplace_back("PidControllers", std::move(pidControllers));
137773df0db0SJames Feist         configuration.emplace_back("FanControllers", std::move(fanControllers));
137873df0db0SJames Feist         configuration.emplace_back("FanZones", std::move(fanZones));
137973df0db0SJames Feist         configuration.emplace_back("StepwiseControllers",
138073df0db0SJames Feist                                    std::move(stepwiseControllers));
138173df0db0SJames Feist     }
1382ecd6a3a2SEd Tanous 
1383ecd6a3a2SEd Tanous     SetPIDValues(const SetPIDValues&) = delete;
1384ecd6a3a2SEd Tanous     SetPIDValues(SetPIDValues&&) = delete;
1385ecd6a3a2SEd Tanous     SetPIDValues& operator=(const SetPIDValues&) = delete;
1386ecd6a3a2SEd Tanous     SetPIDValues& operator=(SetPIDValues&&) = delete;
1387ecd6a3a2SEd Tanous 
138873df0db0SJames Feist     void run()
138973df0db0SJames Feist     {
139073df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
139173df0db0SJames Feist         {
139273df0db0SJames Feist             return;
139373df0db0SJames Feist         }
139473df0db0SJames Feist 
139573df0db0SJames Feist         std::shared_ptr<SetPIDValues> self = shared_from_this();
139673df0db0SJames Feist 
139773df0db0SJames Feist         // todo(james): might make sense to do a mapper call here if this
139873df0db0SJames Feist         // interface gets more traction
139973df0db0SJames Feist         crow::connections::systemBus->async_method_call(
140073df0db0SJames Feist             [self](const boost::system::error_code ec,
1401914e2d5dSEd Tanous                    const dbus::utility::ManagedObjectType& mObj) {
140273df0db0SJames Feist             if (ec)
140373df0db0SJames Feist             {
140473df0db0SJames Feist                 BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
140573df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
140673df0db0SJames Feist                 return;
140773df0db0SJames Feist             }
1408e69d9de2SJames Feist             const std::array<const char*, 3> configurations = {
1409e69d9de2SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
1410e69d9de2SJames Feist                 stepwiseConfigurationIface};
1411e69d9de2SJames Feist 
141214b0b8d5SJames Feist             for (const auto& [path, object] : mObj)
1413e69d9de2SJames Feist             {
141414b0b8d5SJames Feist                 for (const auto& [interface, _] : object)
1415e69d9de2SJames Feist                 {
1416002d39b4SEd Tanous                     if (std::find(configurations.begin(), configurations.end(),
1417e69d9de2SJames Feist                                   interface) != configurations.end())
1418e69d9de2SJames Feist                     {
141914b0b8d5SJames Feist                         self->objectCount++;
1420e69d9de2SJames Feist                         break;
1421e69d9de2SJames Feist                     }
1422e69d9de2SJames Feist                 }
1423e69d9de2SJames Feist             }
1424914e2d5dSEd Tanous             self->managedObj = mObj;
142573df0db0SJames Feist             },
142673df0db0SJames Feist             "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
142773df0db0SJames Feist             "GetManagedObjects");
142873df0db0SJames Feist 
142973df0db0SJames Feist         // at the same time get the profile information
143073df0db0SJames Feist         crow::connections::systemBus->async_method_call(
143173df0db0SJames Feist             [self](const boost::system::error_code ec,
1432b9d36b47SEd Tanous                    const dbus::utility::MapperGetSubTreeResponse& subtree) {
143373df0db0SJames Feist             if (ec || subtree.empty())
143473df0db0SJames Feist             {
143573df0db0SJames Feist                 return;
143673df0db0SJames Feist             }
143773df0db0SJames Feist             if (subtree[0].second.empty())
143873df0db0SJames Feist             {
143973df0db0SJames Feist                 // invalid mapper response, should never happen
144073df0db0SJames Feist                 BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
144173df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
144273df0db0SJames Feist                 return;
144373df0db0SJames Feist             }
144473df0db0SJames Feist 
144573df0db0SJames Feist             const std::string& path = subtree[0].first;
144673df0db0SJames Feist             const std::string& owner = subtree[0].second[0].first;
1447fac6e53bSKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
1448fac6e53bSKrzysztof Grobelny                 *crow::connections::systemBus, owner, path, thermalModeIface,
1449002d39b4SEd Tanous                 [self, path, owner](const boost::system::error_code ec2,
1450b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap& r) {
1451cb13a392SEd Tanous                 if (ec2)
145273df0db0SJames Feist                 {
14530fda0f12SGeorge Liu                     BMCWEB_LOG_ERROR
1454002d39b4SEd Tanous                         << "SetPIDValues: Can't get thermalModeIface " << path;
145573df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
145673df0db0SJames Feist                     return;
145773df0db0SJames Feist                 }
1458271584abSEd Tanous                 const std::string* current = nullptr;
1459271584abSEd Tanous                 const std::vector<std::string>* supported = nullptr;
1460fac6e53bSKrzysztof Grobelny 
1461fac6e53bSKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
1462fac6e53bSKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), r, "Current", current,
1463fac6e53bSKrzysztof Grobelny                     "Supported", supported);
1464fac6e53bSKrzysztof Grobelny 
1465fac6e53bSKrzysztof Grobelny                 if (!success)
146673df0db0SJames Feist                 {
1467002d39b4SEd Tanous                     messages::internalError(self->asyncResp->res);
146873df0db0SJames Feist                     return;
146973df0db0SJames Feist                 }
1470fac6e53bSKrzysztof Grobelny 
147173df0db0SJames Feist                 if (current == nullptr || supported == nullptr)
147273df0db0SJames Feist                 {
14730fda0f12SGeorge Liu                     BMCWEB_LOG_ERROR
1474002d39b4SEd Tanous                         << "SetPIDValues: thermal mode iface invalid " << path;
147573df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
147673df0db0SJames Feist                     return;
147773df0db0SJames Feist                 }
147873df0db0SJames Feist                 self->currentProfile = *current;
147973df0db0SJames Feist                 self->supportedProfiles = *supported;
148073df0db0SJames Feist                 self->profileConnection = owner;
148173df0db0SJames Feist                 self->profilePath = path;
1482fac6e53bSKrzysztof Grobelny                 });
14835b4aa86bSJames Feist             },
14845b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper",
14855b4aa86bSJames Feist             "/xyz/openbmc_project/object_mapper",
14865b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
148773df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
148873df0db0SJames Feist     }
148924b2fe81SEd Tanous     void pidSetDone()
149073df0db0SJames Feist     {
149173df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
149273df0db0SJames Feist         {
149373df0db0SJames Feist             return;
14945b4aa86bSJames Feist         }
14958d1b46d7Szhanghch05         std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
149673df0db0SJames Feist         if (profile)
149773df0db0SJames Feist         {
149873df0db0SJames Feist             if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
149973df0db0SJames Feist                           *profile) == supportedProfiles.end())
150073df0db0SJames Feist             {
150173df0db0SJames Feist                 messages::actionParameterUnknown(response->res, "Profile",
150273df0db0SJames Feist                                                  *profile);
150373df0db0SJames Feist                 return;
150473df0db0SJames Feist             }
150573df0db0SJames Feist             currentProfile = *profile;
150673df0db0SJames Feist             crow::connections::systemBus->async_method_call(
150773df0db0SJames Feist                 [response](const boost::system::error_code ec) {
150873df0db0SJames Feist                 if (ec)
150973df0db0SJames Feist                 {
151073df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Error patching profile" << ec;
151173df0db0SJames Feist                     messages::internalError(response->res);
151273df0db0SJames Feist                 }
151373df0db0SJames Feist                 },
151473df0db0SJames Feist                 profileConnection, profilePath,
151573df0db0SJames Feist                 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
1516168e20c1SEd Tanous                 "Current", dbus::utility::DbusVariantType(*profile));
151773df0db0SJames Feist         }
151873df0db0SJames Feist 
151973df0db0SJames Feist         for (auto& containerPair : configuration)
152073df0db0SJames Feist         {
152173df0db0SJames Feist             auto& container = containerPair.second;
152273df0db0SJames Feist             if (!container)
152373df0db0SJames Feist             {
152473df0db0SJames Feist                 continue;
152573df0db0SJames Feist             }
15266ee7f774SJames Feist             BMCWEB_LOG_DEBUG << *container;
15276ee7f774SJames Feist 
152802cad96eSEd Tanous             const std::string& type = containerPair.first;
152973df0db0SJames Feist 
153073df0db0SJames Feist             for (nlohmann::json::iterator it = container->begin();
153117a897dfSManojkiran Eda                  it != container->end(); ++it)
153273df0db0SJames Feist             {
153373df0db0SJames Feist                 const auto& name = it.key();
15346ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "looking for " << name;
15356ee7f774SJames Feist 
153673df0db0SJames Feist                 auto pathItr =
153773df0db0SJames Feist                     std::find_if(managedObj.begin(), managedObj.end(),
153873df0db0SJames Feist                                  [&name](const auto& obj) {
1539002d39b4SEd Tanous                     return boost::algorithm::ends_with(obj.first.str,
1540002d39b4SEd Tanous                                                        "/" + name);
154173df0db0SJames Feist                     });
1542b9d36b47SEd Tanous                 dbus::utility::DBusPropertiesMap output;
154373df0db0SJames Feist 
154473df0db0SJames Feist                 output.reserve(16); // The pid interface length
154573df0db0SJames Feist 
154673df0db0SJames Feist                 // determines if we're patching entity-manager or
154773df0db0SJames Feist                 // creating a new object
154873df0db0SJames Feist                 bool createNewObject = (pathItr == managedObj.end());
15496ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
15506ee7f774SJames Feist 
155173df0db0SJames Feist                 std::string iface;
1552ea2b670dSEd Tanous                 if (!createNewObject)
1553ea2b670dSEd Tanous                 {
1554ea2b670dSEd Tanous                     for (const auto& interface : pathItr->second)
1555ea2b670dSEd Tanous                     {
1556ea2b670dSEd Tanous                         if (interface.first == pidConfigurationIface)
1557ea2b670dSEd Tanous                         {
1558ea2b670dSEd Tanous                             if (type == "PidControllers" ||
1559ea2b670dSEd Tanous                                 type == "FanControllers")
156073df0db0SJames Feist                             {
156173df0db0SJames Feist                                 iface = pidConfigurationIface;
156273df0db0SJames Feist                                 createNewObject = true;
156373df0db0SJames Feist                             }
156473df0db0SJames Feist                         }
1565ea2b670dSEd Tanous                         else if (interface.first == pidZoneConfigurationIface)
156673df0db0SJames Feist                         {
1567ea2b670dSEd Tanous                             if (type == "FanZones")
156873df0db0SJames Feist                             {
1569ea2b670dSEd Tanous                                 iface = pidConfigurationIface;
157073df0db0SJames Feist                                 createNewObject = true;
157173df0db0SJames Feist                             }
157273df0db0SJames Feist                         }
1573ea2b670dSEd Tanous                         else if (interface.first == stepwiseConfigurationIface)
1574ea2b670dSEd Tanous                         {
1575ea2b670dSEd Tanous                             if (type == "StepwiseControllers")
157673df0db0SJames Feist                             {
157773df0db0SJames Feist                                 iface = stepwiseConfigurationIface;
157873df0db0SJames Feist                                 createNewObject = true;
157973df0db0SJames Feist                             }
1580ea2b670dSEd Tanous                         }
1581ea2b670dSEd Tanous                     }
1582ea2b670dSEd Tanous                 }
15836ee7f774SJames Feist 
15846ee7f774SJames Feist                 if (createNewObject && it.value() == nullptr)
15856ee7f774SJames Feist                 {
15864e0453b1SGunnar Mills                     // can't delete a non-existent object
15871668ce6dSEd Tanous                     messages::propertyValueNotInList(response->res,
15881668ce6dSEd Tanous                                                      it.value().dump(), name);
15896ee7f774SJames Feist                     continue;
15906ee7f774SJames Feist                 }
15916ee7f774SJames Feist 
15926ee7f774SJames Feist                 std::string path;
15936ee7f774SJames Feist                 if (pathItr != managedObj.end())
15946ee7f774SJames Feist                 {
15956ee7f774SJames Feist                     path = pathItr->first.str;
15966ee7f774SJames Feist                 }
15976ee7f774SJames Feist 
159873df0db0SJames Feist                 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
1599e69d9de2SJames Feist 
1600e69d9de2SJames Feist                 // arbitrary limit to avoid attacks
1601e69d9de2SJames Feist                 constexpr const size_t controllerLimit = 500;
160214b0b8d5SJames Feist                 if (createNewObject && objectCount >= controllerLimit)
1603e69d9de2SJames Feist                 {
1604e69d9de2SJames Feist                     messages::resourceExhaustion(response->res, type);
1605e69d9de2SJames Feist                     continue;
1606e69d9de2SJames Feist                 }
1607a170f275SEd Tanous                 std::string escaped = name;
1608a170f275SEd Tanous                 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1609a170f275SEd Tanous                 output.emplace_back("Name", escaped);
161073df0db0SJames Feist 
161173df0db0SJames Feist                 std::string chassis;
161273df0db0SJames Feist                 CreatePIDRet ret = createPidInterface(
16136ee7f774SJames Feist                     response, type, it, path, managedObj, createNewObject,
16146ee7f774SJames Feist                     output, chassis, currentProfile);
161573df0db0SJames Feist                 if (ret == CreatePIDRet::fail)
161673df0db0SJames Feist                 {
161773df0db0SJames Feist                     return;
161873df0db0SJames Feist                 }
16193174e4dfSEd Tanous                 if (ret == CreatePIDRet::del)
162073df0db0SJames Feist                 {
162173df0db0SJames Feist                     continue;
162273df0db0SJames Feist                 }
162373df0db0SJames Feist 
162473df0db0SJames Feist                 if (!createNewObject)
162573df0db0SJames Feist                 {
162673df0db0SJames Feist                     for (const auto& property : output)
162773df0db0SJames Feist                     {
162873df0db0SJames Feist                         crow::connections::systemBus->async_method_call(
162973df0db0SJames Feist                             [response,
163073df0db0SJames Feist                              propertyName{std::string(property.first)}](
163173df0db0SJames Feist                                 const boost::system::error_code ec) {
163273df0db0SJames Feist                             if (ec)
163373df0db0SJames Feist                             {
163473df0db0SJames Feist                                 BMCWEB_LOG_ERROR << "Error patching "
1635002d39b4SEd Tanous                                                  << propertyName << ": " << ec;
163673df0db0SJames Feist                                 messages::internalError(response->res);
163773df0db0SJames Feist                                 return;
163873df0db0SJames Feist                             }
163973df0db0SJames Feist                             messages::success(response->res);
164073df0db0SJames Feist                             },
16416ee7f774SJames Feist                             "xyz.openbmc_project.EntityManager", path,
164273df0db0SJames Feist                             "org.freedesktop.DBus.Properties", "Set", iface,
164373df0db0SJames Feist                             property.first, property.second);
164473df0db0SJames Feist                     }
164573df0db0SJames Feist                 }
164673df0db0SJames Feist                 else
164773df0db0SJames Feist                 {
164873df0db0SJames Feist                     if (chassis.empty())
164973df0db0SJames Feist                     {
165073df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to get chassis from config";
1651ace85d60SEd Tanous                         messages::internalError(response->res);
165273df0db0SJames Feist                         return;
165373df0db0SJames Feist                     }
165473df0db0SJames Feist 
165573df0db0SJames Feist                     bool foundChassis = false;
165673df0db0SJames Feist                     for (const auto& obj : managedObj)
165773df0db0SJames Feist                     {
165873df0db0SJames Feist                         if (boost::algorithm::ends_with(obj.first.str, chassis))
165973df0db0SJames Feist                         {
166073df0db0SJames Feist                             chassis = obj.first.str;
166173df0db0SJames Feist                             foundChassis = true;
166273df0db0SJames Feist                             break;
166373df0db0SJames Feist                         }
166473df0db0SJames Feist                     }
166573df0db0SJames Feist                     if (!foundChassis)
166673df0db0SJames Feist                     {
166773df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
166873df0db0SJames Feist                         messages::resourceMissingAtURI(
1669ace85d60SEd Tanous                             response->res,
1670ace85d60SEd Tanous                             crow::utility::urlFromPieces("redfish", "v1",
1671ace85d60SEd Tanous                                                          "Chassis", chassis));
167273df0db0SJames Feist                         return;
167373df0db0SJames Feist                     }
167473df0db0SJames Feist 
167573df0db0SJames Feist                     crow::connections::systemBus->async_method_call(
167673df0db0SJames Feist                         [response](const boost::system::error_code ec) {
167773df0db0SJames Feist                         if (ec)
167873df0db0SJames Feist                         {
167973df0db0SJames Feist                             BMCWEB_LOG_ERROR << "Error Adding Pid Object "
168073df0db0SJames Feist                                              << ec;
168173df0db0SJames Feist                             messages::internalError(response->res);
168273df0db0SJames Feist                             return;
168373df0db0SJames Feist                         }
168473df0db0SJames Feist                         messages::success(response->res);
168573df0db0SJames Feist                         },
168673df0db0SJames Feist                         "xyz.openbmc_project.EntityManager", chassis,
168773df0db0SJames Feist                         "xyz.openbmc_project.AddObject", "AddObject", output);
168873df0db0SJames Feist                 }
168973df0db0SJames Feist             }
169073df0db0SJames Feist         }
169173df0db0SJames Feist     }
169224b2fe81SEd Tanous 
169324b2fe81SEd Tanous     ~SetPIDValues()
169424b2fe81SEd Tanous     {
169524b2fe81SEd Tanous         try
169624b2fe81SEd Tanous         {
169724b2fe81SEd Tanous             pidSetDone();
169824b2fe81SEd Tanous         }
169924b2fe81SEd Tanous         catch (...)
170024b2fe81SEd Tanous         {
170124b2fe81SEd Tanous             BMCWEB_LOG_CRITICAL << "pidSetDone threw exception";
170224b2fe81SEd Tanous         }
170324b2fe81SEd Tanous     }
170424b2fe81SEd Tanous 
17058d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
170673df0db0SJames Feist     std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
170773df0db0SJames Feist         configuration;
170873df0db0SJames Feist     std::optional<std::string> profile;
170973df0db0SJames Feist     dbus::utility::ManagedObjectType managedObj;
171073df0db0SJames Feist     std::vector<std::string> supportedProfiles;
171173df0db0SJames Feist     std::string currentProfile;
171273df0db0SJames Feist     std::string profileConnection;
171373df0db0SJames Feist     std::string profilePath;
171414b0b8d5SJames Feist     size_t objectCount = 0;
171573df0db0SJames Feist };
171673df0db0SJames Feist 
1717071d8fdfSSunnySrivastava1984 /**
1718071d8fdfSSunnySrivastava1984  * @brief Retrieves BMC manager location data over DBus
1719071d8fdfSSunnySrivastava1984  *
1720071d8fdfSSunnySrivastava1984  * @param[in] aResp Shared pointer for completing asynchronous calls
1721071d8fdfSSunnySrivastava1984  * @param[in] connectionName - service name
1722071d8fdfSSunnySrivastava1984  * @param[in] path - object path
1723071d8fdfSSunnySrivastava1984  * @return none
1724071d8fdfSSunnySrivastava1984  */
17258d1b46d7Szhanghch05 inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1726071d8fdfSSunnySrivastava1984                         const std::string& connectionName,
1727071d8fdfSSunnySrivastava1984                         const std::string& path)
1728071d8fdfSSunnySrivastava1984 {
1729071d8fdfSSunnySrivastava1984     BMCWEB_LOG_DEBUG << "Get BMC manager Location data.";
1730071d8fdfSSunnySrivastava1984 
17311e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
17321e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
17331e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
1734071d8fdfSSunnySrivastava1984         [aResp](const boost::system::error_code ec,
17351e1e598dSJonathan Doman                 const std::string& property) {
1736071d8fdfSSunnySrivastava1984         if (ec)
1737071d8fdfSSunnySrivastava1984         {
1738071d8fdfSSunnySrivastava1984             BMCWEB_LOG_DEBUG << "DBUS response error for "
1739071d8fdfSSunnySrivastava1984                                 "Location";
1740071d8fdfSSunnySrivastava1984             messages::internalError(aResp->res);
1741071d8fdfSSunnySrivastava1984             return;
1742071d8fdfSSunnySrivastava1984         }
1743071d8fdfSSunnySrivastava1984 
1744071d8fdfSSunnySrivastava1984         aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
17451e1e598dSJonathan Doman             property;
17461e1e598dSJonathan Doman         });
1747071d8fdfSSunnySrivastava1984 }
17487e860f15SJohn Edward Broadbent // avoid name collision systems.hpp
17497e860f15SJohn Edward Broadbent inline void
17507e860f15SJohn Edward Broadbent     managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
17514bf2b033SGunnar Mills {
17524bf2b033SGunnar Mills     BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time";
17534bf2b033SGunnar Mills 
17541e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint64_t>(
17551e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
17561e1e598dSJonathan Doman         "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
17571e1e598dSJonathan Doman         "LastRebootTime",
17584bf2b033SGunnar Mills         [aResp](const boost::system::error_code ec,
17591e1e598dSJonathan Doman                 const uint64_t lastResetTime) {
17604bf2b033SGunnar Mills         if (ec)
17614bf2b033SGunnar Mills         {
17624bf2b033SGunnar Mills             BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
17634bf2b033SGunnar Mills             return;
17644bf2b033SGunnar Mills         }
17654bf2b033SGunnar Mills 
17664bf2b033SGunnar Mills         // LastRebootTime is epoch time, in milliseconds
17674bf2b033SGunnar Mills         // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
17681e1e598dSJonathan Doman         uint64_t lastResetTimeStamp = lastResetTime / 1000;
17694bf2b033SGunnar Mills 
17704bf2b033SGunnar Mills         // Convert to ISO 8601 standard
17714bf2b033SGunnar Mills         aResp->res.jsonValue["LastResetTime"] =
17722b82937eSEd Tanous             redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
17731e1e598dSJonathan Doman         });
17744bf2b033SGunnar Mills }
17754bf2b033SGunnar Mills 
17764bfefa74SGunnar Mills /**
17774bfefa74SGunnar Mills  * @brief Set the running firmware image
17784bfefa74SGunnar Mills  *
17794bfefa74SGunnar Mills  * @param[i,o] aResp - Async response object
17804bfefa74SGunnar Mills  * @param[i] runningFirmwareTarget - Image to make the running image
17814bfefa74SGunnar Mills  *
17824bfefa74SGunnar Mills  * @return void
17834bfefa74SGunnar Mills  */
17847e860f15SJohn Edward Broadbent inline void
17857e860f15SJohn Edward Broadbent     setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1786f23b7296SEd Tanous                            const std::string& runningFirmwareTarget)
17874bfefa74SGunnar Mills {
17884bfefa74SGunnar Mills     // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1789f23b7296SEd Tanous     std::string::size_type idPos = runningFirmwareTarget.rfind('/');
17904bfefa74SGunnar Mills     if (idPos == std::string::npos)
17914bfefa74SGunnar Mills     {
17924bfefa74SGunnar Mills         messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
17934bfefa74SGunnar Mills                                          "@odata.id");
17944bfefa74SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't parse firmware ID!";
17954bfefa74SGunnar Mills         return;
17964bfefa74SGunnar Mills     }
17974bfefa74SGunnar Mills     idPos++;
17984bfefa74SGunnar Mills     if (idPos >= runningFirmwareTarget.size())
17994bfefa74SGunnar Mills     {
18004bfefa74SGunnar Mills         messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
18014bfefa74SGunnar Mills                                          "@odata.id");
18024bfefa74SGunnar Mills         BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
18034bfefa74SGunnar Mills         return;
18044bfefa74SGunnar Mills     }
18054bfefa74SGunnar Mills     std::string firmwareId = runningFirmwareTarget.substr(idPos);
18064bfefa74SGunnar Mills 
18074bfefa74SGunnar Mills     // Make sure the image is valid before setting priority
18084bfefa74SGunnar Mills     crow::connections::systemBus->async_method_call(
1809711ac7a9SEd Tanous         [aResp, firmwareId,
1810711ac7a9SEd Tanous          runningFirmwareTarget](const boost::system::error_code ec,
1811711ac7a9SEd Tanous                                 dbus::utility::ManagedObjectType& subtree) {
18124bfefa74SGunnar Mills         if (ec)
18134bfefa74SGunnar Mills         {
18144bfefa74SGunnar Mills             BMCWEB_LOG_DEBUG << "D-Bus response error getting objects.";
18154bfefa74SGunnar Mills             messages::internalError(aResp->res);
18164bfefa74SGunnar Mills             return;
18174bfefa74SGunnar Mills         }
18184bfefa74SGunnar Mills 
181926f6976fSEd Tanous         if (subtree.empty())
18204bfefa74SGunnar Mills         {
18214bfefa74SGunnar Mills             BMCWEB_LOG_DEBUG << "Can't find image!";
18224bfefa74SGunnar Mills             messages::internalError(aResp->res);
18234bfefa74SGunnar Mills             return;
18244bfefa74SGunnar Mills         }
18254bfefa74SGunnar Mills 
18264bfefa74SGunnar Mills         bool foundImage = false;
182702cad96eSEd Tanous         for (const auto& object : subtree)
18284bfefa74SGunnar Mills         {
18294bfefa74SGunnar Mills             const std::string& path =
18304bfefa74SGunnar Mills                 static_cast<const std::string&>(object.first);
1831f23b7296SEd Tanous             std::size_t idPos2 = path.rfind('/');
18324bfefa74SGunnar Mills 
18334bfefa74SGunnar Mills             if (idPos2 == std::string::npos)
18344bfefa74SGunnar Mills             {
18354bfefa74SGunnar Mills                 continue;
18364bfefa74SGunnar Mills             }
18374bfefa74SGunnar Mills 
18384bfefa74SGunnar Mills             idPos2++;
18394bfefa74SGunnar Mills             if (idPos2 >= path.size())
18404bfefa74SGunnar Mills             {
18414bfefa74SGunnar Mills                 continue;
18424bfefa74SGunnar Mills             }
18434bfefa74SGunnar Mills 
18444bfefa74SGunnar Mills             if (path.substr(idPos2) == firmwareId)
18454bfefa74SGunnar Mills             {
18464bfefa74SGunnar Mills                 foundImage = true;
18474bfefa74SGunnar Mills                 break;
18484bfefa74SGunnar Mills             }
18494bfefa74SGunnar Mills         }
18504bfefa74SGunnar Mills 
18514bfefa74SGunnar Mills         if (!foundImage)
18524bfefa74SGunnar Mills         {
1853002d39b4SEd Tanous             messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
1854002d39b4SEd Tanous                                              "@odata.id");
18554bfefa74SGunnar Mills             BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
18564bfefa74SGunnar Mills             return;
18574bfefa74SGunnar Mills         }
18584bfefa74SGunnar Mills 
18598cc8edecSEd Tanous         BMCWEB_LOG_DEBUG << "Setting firmware version " << firmwareId
18608cc8edecSEd Tanous                          << " to priority 0.";
18614bfefa74SGunnar Mills 
18624bfefa74SGunnar Mills         // Only support Immediate
18634bfefa74SGunnar Mills         // An addition could be a Redfish Setting like
18644bfefa74SGunnar Mills         // ActiveSoftwareImageApplyTime and support OnReset
18654bfefa74SGunnar Mills         crow::connections::systemBus->async_method_call(
18668a592810SEd Tanous             [aResp](const boost::system::error_code ec2) {
18678a592810SEd Tanous             if (ec2)
18684bfefa74SGunnar Mills             {
18694bfefa74SGunnar Mills                 BMCWEB_LOG_DEBUG << "D-Bus response error setting.";
18704bfefa74SGunnar Mills                 messages::internalError(aResp->res);
18714bfefa74SGunnar Mills                 return;
18724bfefa74SGunnar Mills             }
18734bfefa74SGunnar Mills             doBMCGracefulRestart(aResp);
18744bfefa74SGunnar Mills             },
18754bfefa74SGunnar Mills 
18764bfefa74SGunnar Mills             "xyz.openbmc_project.Software.BMC.Updater",
18774bfefa74SGunnar Mills             "/xyz/openbmc_project/software/" + firmwareId,
18784bfefa74SGunnar Mills             "org.freedesktop.DBus.Properties", "Set",
18797e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1880168e20c1SEd Tanous             dbus::utility::DbusVariantType(static_cast<uint8_t>(0)));
18814bfefa74SGunnar Mills         },
18824bfefa74SGunnar Mills         "xyz.openbmc_project.Software.BMC.Updater",
18837e860f15SJohn Edward Broadbent         "/xyz/openbmc_project/software", "org.freedesktop.DBus.ObjectManager",
18847e860f15SJohn Edward Broadbent         "GetManagedObjects");
18854bfefa74SGunnar Mills }
18864bfefa74SGunnar Mills 
18877e860f15SJohn Edward Broadbent inline void setDateTime(std::shared_ptr<bmcweb::AsyncResp> aResp,
18887e860f15SJohn Edward Broadbent                         std::string datetime)
1889af5d6058SSantosh Puranik {
1890af5d6058SSantosh Puranik     BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
1891af5d6058SSantosh Puranik 
1892af5d6058SSantosh Puranik     std::stringstream stream(datetime);
1893af5d6058SSantosh Puranik     // Convert from ISO 8601 to boost local_time
1894af5d6058SSantosh Puranik     // (BMC only has time in UTC)
1895af5d6058SSantosh Puranik     boost::posix_time::ptime posixTime;
1896af5d6058SSantosh Puranik     boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
1897af5d6058SSantosh Puranik     // Facet gets deleted with the stringsteam
1898af5d6058SSantosh Puranik     auto ifc = std::make_unique<boost::local_time::local_time_input_facet>(
1899af5d6058SSantosh Puranik         "%Y-%m-%d %H:%M:%S%F %ZP");
1900af5d6058SSantosh Puranik     stream.imbue(std::locale(stream.getloc(), ifc.release()));
1901af5d6058SSantosh Puranik 
19027e860f15SJohn Edward Broadbent     boost::local_time::local_date_time ldt(boost::local_time::not_a_date_time);
1903af5d6058SSantosh Puranik 
1904af5d6058SSantosh Puranik     if (stream >> ldt)
1905af5d6058SSantosh Puranik     {
1906af5d6058SSantosh Puranik         posixTime = ldt.utc_time();
1907af5d6058SSantosh Puranik         boost::posix_time::time_duration dur = posixTime - epoch;
19087e860f15SJohn Edward Broadbent         uint64_t durMicroSecs = static_cast<uint64_t>(dur.total_microseconds());
1909af5d6058SSantosh Puranik         crow::connections::systemBus->async_method_call(
1910af5d6058SSantosh Puranik             [aResp{std::move(aResp)}, datetime{std::move(datetime)}](
1911af5d6058SSantosh Puranik                 const boost::system::error_code ec) {
1912af5d6058SSantosh Puranik             if (ec)
1913af5d6058SSantosh Puranik             {
1914af5d6058SSantosh Puranik                 BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1915af5d6058SSantosh Puranik                                     "DBUS response error "
1916af5d6058SSantosh Puranik                                  << ec;
1917af5d6058SSantosh Puranik                 messages::internalError(aResp->res);
1918af5d6058SSantosh Puranik                 return;
1919af5d6058SSantosh Puranik             }
1920af5d6058SSantosh Puranik             aResp->res.jsonValue["DateTime"] = datetime;
1921af5d6058SSantosh Puranik             },
19227e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Time.Manager", "/xyz/openbmc_project/time/bmc",
1923af5d6058SSantosh Puranik             "org.freedesktop.DBus.Properties", "Set",
1924af5d6058SSantosh Puranik             "xyz.openbmc_project.Time.EpochTime", "Elapsed",
1925168e20c1SEd Tanous             dbus::utility::DbusVariantType(durMicroSecs));
1926af5d6058SSantosh Puranik     }
1927af5d6058SSantosh Puranik     else
1928af5d6058SSantosh Puranik     {
19297e860f15SJohn Edward Broadbent         messages::propertyValueFormatError(aResp->res, datetime, "DateTime");
1930af5d6058SSantosh Puranik         return;
1931af5d6058SSantosh Puranik     }
193283ff9ab6SJames Feist }
19339c310685SBorawski.Lukasz 
19347e860f15SJohn Edward Broadbent inline void requestRoutesManager(App& app)
19357e860f15SJohn Edward Broadbent {
19367e860f15SJohn Edward Broadbent     std::string uuid = persistent_data::getConfig().systemUuid;
19379c310685SBorawski.Lukasz 
19387e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
1939ed398213SEd Tanous         .privileges(redfish::privileges::getManager)
1940002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1941002d39b4SEd Tanous             [&app, uuid](const crow::Request& req,
194245ca1b86SEd Tanous                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
19433ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
194445ca1b86SEd Tanous         {
194545ca1b86SEd Tanous             return;
194645ca1b86SEd Tanous         }
19477e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
1948a51fc2d2SSui Chen         asyncResp->res.jsonValue["@odata.type"] = "#Manager.v1_14_0.Manager";
19497e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["Id"] = "bmc";
19507e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
19517e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["Description"] =
19527e860f15SJohn Edward Broadbent             "Baseboard Management Controller";
19537e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["PowerState"] = "On";
19541476687dSEd Tanous         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
19551476687dSEd Tanous         asyncResp->res.jsonValue["Status"]["Health"] = "OK";
19561476687dSEd Tanous 
19577e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["ManagerType"] = "BMC";
19587e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
19597e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1960002d39b4SEd Tanous         asyncResp->res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
19617e860f15SJohn Edward Broadbent 
19621476687dSEd Tanous         asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
19631476687dSEd Tanous             "/redfish/v1/Managers/bmc/LogServices";
19641476687dSEd Tanous         asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
19651476687dSEd Tanous             "/redfish/v1/Managers/bmc/NetworkProtocol";
19661476687dSEd Tanous         asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
19671476687dSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
19687e860f15SJohn Edward Broadbent 
19697e860f15SJohn Edward Broadbent #ifdef BMCWEB_ENABLE_VM_NBDPROXY
19701476687dSEd Tanous         asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
19711476687dSEd Tanous             "/redfish/v1/Managers/bmc/VirtualMedia";
19727e860f15SJohn Edward Broadbent #endif // BMCWEB_ENABLE_VM_NBDPROXY
19737e860f15SJohn Edward Broadbent 
19747e860f15SJohn Edward Broadbent         // default oem data
19757e860f15SJohn Edward Broadbent         nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
19767e860f15SJohn Edward Broadbent         nlohmann::json& oemOpenbmc = oem["OpenBmc"];
19777e860f15SJohn Edward Broadbent         oem["@odata.type"] = "#OemManager.Oem";
19787e860f15SJohn Edward Broadbent         oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
19797e860f15SJohn Edward Broadbent         oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
19807e860f15SJohn Edward Broadbent         oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
19811476687dSEd Tanous 
19821476687dSEd Tanous         nlohmann::json::object_t certificates;
19831476687dSEd Tanous         certificates["@odata.id"] =
19841476687dSEd Tanous             "/redfish/v1/Managers/bmc/Truststore/Certificates";
19851476687dSEd Tanous         oemOpenbmc["Certificates"] = std::move(certificates);
19867e860f15SJohn Edward Broadbent 
19877e860f15SJohn Edward Broadbent         // Manager.Reset (an action) can be many values, OpenBMC only
19887e860f15SJohn Edward Broadbent         // supports BMC reboot.
19897e860f15SJohn Edward Broadbent         nlohmann::json& managerReset =
19907e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
19917e860f15SJohn Edward Broadbent         managerReset["target"] =
19927e860f15SJohn Edward Broadbent             "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
19937e860f15SJohn Edward Broadbent         managerReset["@Redfish.ActionInfo"] =
19947e860f15SJohn Edward Broadbent             "/redfish/v1/Managers/bmc/ResetActionInfo";
19957e860f15SJohn Edward Broadbent 
19967e860f15SJohn Edward Broadbent         // ResetToDefaults (Factory Reset) has values like
19977e860f15SJohn Edward Broadbent         // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
19987e860f15SJohn Edward Broadbent         // on OpenBMC
19997e860f15SJohn Edward Broadbent         nlohmann::json& resetToDefaults =
20007e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
20017e860f15SJohn Edward Broadbent         resetToDefaults["target"] =
20027e860f15SJohn Edward Broadbent             "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
2003*613dabeaSEd Tanous         resetToDefaults["ResetType@Redfish.AllowableValues"] =
2004*613dabeaSEd Tanous             nlohmann::json::array_t({"ResetAll"});
20057e860f15SJohn Edward Broadbent 
20067c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
20072b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
20087c8c4058STejas Patil 
20097c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
20107c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
20117c8c4058STejas Patil             redfishDateTimeOffset.second;
20127e860f15SJohn Edward Broadbent 
20130e8ac5e7SGunnar Mills         // TODO (Gunnar): Remove these one day since moved to ComputerSystem
20140e8ac5e7SGunnar Mills         // Still used by OCP profiles
20150e8ac5e7SGunnar Mills         // https://github.com/opencomputeproject/OCP-Profiles/issues/23
20167e860f15SJohn Edward Broadbent         // Fill in SerialConsole info
20177e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
2018002d39b4SEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
2019*613dabeaSEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
2020*613dabeaSEd Tanous             nlohmann::json::array_t({"IPMI", "SSH"});
20217e860f15SJohn Edward Broadbent #ifdef BMCWEB_ENABLE_KVM
20227e860f15SJohn Edward Broadbent         // Fill in GraphicalConsole info
2023002d39b4SEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
2024002d39b4SEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] =
2025002d39b4SEd Tanous             4;
2026*613dabeaSEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
2027*613dabeaSEd Tanous             nlohmann::json::array_t({"KVMIP"});
20287e860f15SJohn Edward Broadbent #endif // BMCWEB_ENABLE_KVM
20297e860f15SJohn Edward Broadbent 
2030002d39b4SEd Tanous         asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
20311476687dSEd Tanous 
20321476687dSEd Tanous         nlohmann::json::array_t managerForServers;
20331476687dSEd Tanous         nlohmann::json::object_t manager;
20341476687dSEd Tanous         manager["@odata.id"] = "/redfish/v1/Systems/system";
20351476687dSEd Tanous         managerForServers.push_back(std::move(manager));
20361476687dSEd Tanous 
20371476687dSEd Tanous         asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
20381476687dSEd Tanous             std::move(managerForServers);
20397e860f15SJohn Edward Broadbent 
20407e860f15SJohn Edward Broadbent         auto health = std::make_shared<HealthPopulate>(asyncResp);
20417e860f15SJohn Edward Broadbent         health->isManagersHealth = true;
20427e860f15SJohn Edward Broadbent         health->populate();
20437e860f15SJohn Edward Broadbent 
2044eee0013eSWilly Tu         sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
20457e860f15SJohn Edward Broadbent                                              "FirmwareVersion", true);
20467e860f15SJohn Edward Broadbent 
20477e860f15SJohn Edward Broadbent         managerGetLastResetTime(asyncResp);
20487e860f15SJohn Edward Broadbent 
2049a51fc2d2SSui Chen         // ManagerDiagnosticData is added for all BMCs.
2050a51fc2d2SSui Chen         nlohmann::json& managerDiagnosticData =
2051a51fc2d2SSui Chen             asyncResp->res.jsonValue["ManagerDiagnosticData"];
2052a51fc2d2SSui Chen         managerDiagnosticData["@odata.id"] =
2053a51fc2d2SSui Chen             "/redfish/v1/Managers/bmc/ManagerDiagnosticData";
2054a51fc2d2SSui Chen 
205554dce7f5SGunnar Mills #ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
20567e860f15SJohn Edward Broadbent         auto pids = std::make_shared<GetPIDValues>(asyncResp);
20577e860f15SJohn Edward Broadbent         pids->run();
205854dce7f5SGunnar Mills #endif
20597e860f15SJohn Edward Broadbent 
2060002d39b4SEd Tanous         getMainChassisId(asyncResp,
2061002d39b4SEd Tanous                          [](const std::string& chassisId,
2062002d39b4SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2063002d39b4SEd Tanous             aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
20641476687dSEd Tanous             nlohmann::json::array_t managerForChassis;
20658a592810SEd Tanous             nlohmann::json::object_t managerObj;
20668a592810SEd Tanous             managerObj["@odata.id"] = "/redfish/v1/Chassis/" + chassisId;
20678a592810SEd Tanous             managerForChassis.push_back(std::move(managerObj));
20681476687dSEd Tanous             aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
20691476687dSEd Tanous                 std::move(managerForChassis);
20701476687dSEd Tanous             aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
20711476687dSEd Tanous                 "/redfish/v1/Chassis/" + chassisId;
20727e860f15SJohn Edward Broadbent         });
20737e860f15SJohn Edward Broadbent 
20747e860f15SJohn Edward Broadbent         static bool started = false;
20757e860f15SJohn Edward Broadbent 
20767e860f15SJohn Edward Broadbent         if (!started)
20771abe55efSEd Tanous         {
20781e1e598dSJonathan Doman             sdbusplus::asio::getProperty<double>(
20791e1e598dSJonathan Doman                 *crow::connections::systemBus, "org.freedesktop.systemd1",
2080002d39b4SEd Tanous                 "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
2081002d39b4SEd Tanous                 "Progress",
20827e860f15SJohn Edward Broadbent                 [asyncResp](const boost::system::error_code ec,
20831e1e598dSJonathan Doman                             const double& val) {
20847e860f15SJohn Edward Broadbent                 if (ec)
20851abe55efSEd Tanous                 {
20867e860f15SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "Error while getting progress";
20877e860f15SJohn Edward Broadbent                     messages::internalError(asyncResp->res);
20887e860f15SJohn Edward Broadbent                     return;
20897e860f15SJohn Edward Broadbent                 }
20901e1e598dSJonathan Doman                 if (val < 1.0)
20917e860f15SJohn Edward Broadbent                 {
2092002d39b4SEd Tanous                     asyncResp->res.jsonValue["Status"]["State"] = "Starting";
20937e860f15SJohn Edward Broadbent                     started = true;
20947e860f15SJohn Edward Broadbent                 }
20951e1e598dSJonathan Doman                 });
20969c310685SBorawski.Lukasz         }
20979c310685SBorawski.Lukasz 
20987e860f15SJohn Edward Broadbent         crow::connections::systemBus->async_method_call(
20997e860f15SJohn Edward Broadbent             [asyncResp](
21007e860f15SJohn Edward Broadbent                 const boost::system::error_code ec,
2101b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
21027e860f15SJohn Edward Broadbent             if (ec)
21031abe55efSEd Tanous             {
2104002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "D-Bus response error on GetSubTree " << ec;
21057e860f15SJohn Edward Broadbent                 return;
21067e860f15SJohn Edward Broadbent             }
210726f6976fSEd Tanous             if (subtree.empty())
21087e860f15SJohn Edward Broadbent             {
21097e860f15SJohn Edward Broadbent                 BMCWEB_LOG_DEBUG << "Can't find bmc D-Bus object!";
21107e860f15SJohn Edward Broadbent                 return;
21117e860f15SJohn Edward Broadbent             }
21127e860f15SJohn Edward Broadbent             // Assume only 1 bmc D-Bus object
21137e860f15SJohn Edward Broadbent             // Throw an error if there is more than 1
21147e860f15SJohn Edward Broadbent             if (subtree.size() > 1)
21157e860f15SJohn Edward Broadbent             {
2116002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "Found more than 1 bmc D-Bus object!";
21177e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
21187e860f15SJohn Edward Broadbent                 return;
21197e860f15SJohn Edward Broadbent             }
21207e860f15SJohn Edward Broadbent 
2121002d39b4SEd Tanous             if (subtree[0].first.empty() || subtree[0].second.size() != 1)
21227e860f15SJohn Edward Broadbent             {
21237e860f15SJohn Edward Broadbent                 BMCWEB_LOG_DEBUG << "Error getting bmc D-Bus object!";
21247e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
21257e860f15SJohn Edward Broadbent                 return;
21267e860f15SJohn Edward Broadbent             }
21277e860f15SJohn Edward Broadbent 
21287e860f15SJohn Edward Broadbent             const std::string& path = subtree[0].first;
2129002d39b4SEd Tanous             const std::string& connectionName = subtree[0].second[0].first;
21307e860f15SJohn Edward Broadbent 
2131002d39b4SEd Tanous             for (const auto& interfaceName : subtree[0].second[0].second)
21327e860f15SJohn Edward Broadbent             {
21337e860f15SJohn Edward Broadbent                 if (interfaceName ==
21347e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Inventory.Decorator.Asset")
21357e860f15SJohn Edward Broadbent                 {
2136fac6e53bSKrzysztof Grobelny 
2137fac6e53bSKrzysztof Grobelny                     sdbusplus::asio::getAllProperties(
2138fac6e53bSKrzysztof Grobelny                         *crow::connections::systemBus, connectionName, path,
2139fac6e53bSKrzysztof Grobelny                         "xyz.openbmc_project.Inventory.Decorator.Asset",
21408a592810SEd Tanous                         [asyncResp](const boost::system::error_code ec2,
2141b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap&
21427e860f15SJohn Edward Broadbent                                         propertiesList) {
21438a592810SEd Tanous                         if (ec2)
21447e860f15SJohn Edward Broadbent                         {
2145002d39b4SEd Tanous                             BMCWEB_LOG_DEBUG << "Can't get bmc asset!";
21467e860f15SJohn Edward Broadbent                             return;
21477e860f15SJohn Edward Broadbent                         }
21487e860f15SJohn Edward Broadbent 
2149fac6e53bSKrzysztof Grobelny                         const std::string* partNumber = nullptr;
2150fac6e53bSKrzysztof Grobelny                         const std::string* serialNumber = nullptr;
2151fac6e53bSKrzysztof Grobelny                         const std::string* manufacturer = nullptr;
2152fac6e53bSKrzysztof Grobelny                         const std::string* model = nullptr;
2153fac6e53bSKrzysztof Grobelny                         const std::string* sparePartNumber = nullptr;
2154fac6e53bSKrzysztof Grobelny 
2155fac6e53bSKrzysztof Grobelny                         const bool success = sdbusplus::unpackPropertiesNoThrow(
2156fac6e53bSKrzysztof Grobelny                             dbus_utils::UnpackErrorPrinter(), propertiesList,
2157fac6e53bSKrzysztof Grobelny                             "PartNumber", partNumber, "SerialNumber",
2158fac6e53bSKrzysztof Grobelny                             serialNumber, "Manufacturer", manufacturer, "Model",
2159fac6e53bSKrzysztof Grobelny                             model, "SparePartNumber", sparePartNumber);
2160fac6e53bSKrzysztof Grobelny 
2161fac6e53bSKrzysztof Grobelny                         if (!success)
21627e860f15SJohn Edward Broadbent                         {
2163002d39b4SEd Tanous                             messages::internalError(asyncResp->res);
21647e860f15SJohn Edward Broadbent                             return;
21657e860f15SJohn Edward Broadbent                         }
2166fac6e53bSKrzysztof Grobelny 
2167fac6e53bSKrzysztof Grobelny                         if (partNumber != nullptr)
2168fac6e53bSKrzysztof Grobelny                         {
2169fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["PartNumber"] =
2170fac6e53bSKrzysztof Grobelny                                 *partNumber;
21717e860f15SJohn Edward Broadbent                         }
2172fac6e53bSKrzysztof Grobelny 
2173fac6e53bSKrzysztof Grobelny                         if (serialNumber != nullptr)
2174fac6e53bSKrzysztof Grobelny                         {
2175fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["SerialNumber"] =
2176fac6e53bSKrzysztof Grobelny                                 *serialNumber;
21777e860f15SJohn Edward Broadbent                         }
2178fac6e53bSKrzysztof Grobelny 
2179fac6e53bSKrzysztof Grobelny                         if (manufacturer != nullptr)
2180fac6e53bSKrzysztof Grobelny                         {
2181fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["Manufacturer"] =
2182fac6e53bSKrzysztof Grobelny                                 *manufacturer;
2183fac6e53bSKrzysztof Grobelny                         }
2184fac6e53bSKrzysztof Grobelny 
2185fac6e53bSKrzysztof Grobelny                         if (model != nullptr)
2186fac6e53bSKrzysztof Grobelny                         {
2187fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["Model"] = *model;
2188fac6e53bSKrzysztof Grobelny                         }
2189fac6e53bSKrzysztof Grobelny 
2190fac6e53bSKrzysztof Grobelny                         if (sparePartNumber != nullptr)
2191fac6e53bSKrzysztof Grobelny                         {
2192fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["SparePartNumber"] =
2193fac6e53bSKrzysztof Grobelny                                 *sparePartNumber;
2194fac6e53bSKrzysztof Grobelny                         }
2195fac6e53bSKrzysztof Grobelny                         });
21967e860f15SJohn Edward Broadbent                 }
2197002d39b4SEd Tanous                 else if (interfaceName ==
21980fda0f12SGeorge Liu                          "xyz.openbmc_project.Inventory.Decorator.LocationCode")
21997e860f15SJohn Edward Broadbent                 {
22007e860f15SJohn Edward Broadbent                     getLocation(asyncResp, connectionName, path);
22017e860f15SJohn Edward Broadbent                 }
22027e860f15SJohn Edward Broadbent             }
22037e860f15SJohn Edward Broadbent             },
22047e860f15SJohn Edward Broadbent             "xyz.openbmc_project.ObjectMapper",
22057e860f15SJohn Edward Broadbent             "/xyz/openbmc_project/object_mapper",
22067e860f15SJohn Edward Broadbent             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
22077e860f15SJohn Edward Broadbent             "/xyz/openbmc_project/inventory", int32_t(0),
22087e860f15SJohn Edward Broadbent             std::array<const char*, 1>{
22097e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.Inventory.Item.Bmc"});
22107e860f15SJohn Edward Broadbent         });
22117e860f15SJohn Edward Broadbent 
22127e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
2213ed398213SEd Tanous         .privileges(redfish::privileges::patchManager)
221445ca1b86SEd Tanous         .methods(boost::beast::http::verb::patch)(
221545ca1b86SEd Tanous             [&app](const crow::Request& req,
22167e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
22173ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
221845ca1b86SEd Tanous         {
221945ca1b86SEd Tanous             return;
222045ca1b86SEd Tanous         }
22217e860f15SJohn Edward Broadbent         std::optional<nlohmann::json> oem;
22227e860f15SJohn Edward Broadbent         std::optional<nlohmann::json> links;
22237e860f15SJohn Edward Broadbent         std::optional<std::string> datetime;
22247e860f15SJohn Edward Broadbent 
222515ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "Oem", oem,
2226002d39b4SEd Tanous                                       "DateTime", datetime, "Links", links))
22277e860f15SJohn Edward Broadbent         {
22287e860f15SJohn Edward Broadbent             return;
22297e860f15SJohn Edward Broadbent         }
22307e860f15SJohn Edward Broadbent 
22317e860f15SJohn Edward Broadbent         if (oem)
22327e860f15SJohn Edward Broadbent         {
223354dce7f5SGunnar Mills #ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
22347e860f15SJohn Edward Broadbent             std::optional<nlohmann::json> openbmc;
2235002d39b4SEd Tanous             if (!redfish::json_util::readJson(*oem, asyncResp->res, "OpenBmc",
2236002d39b4SEd Tanous                                               openbmc))
22377e860f15SJohn Edward Broadbent             {
22387e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR
22397e860f15SJohn Edward Broadbent                     << "Illegal Property "
2240002d39b4SEd Tanous                     << oem->dump(2, ' ', true,
22417e860f15SJohn Edward Broadbent                                  nlohmann::json::error_handler_t::replace);
22427e860f15SJohn Edward Broadbent                 return;
22437e860f15SJohn Edward Broadbent             }
22447e860f15SJohn Edward Broadbent             if (openbmc)
22457e860f15SJohn Edward Broadbent             {
22467e860f15SJohn Edward Broadbent                 std::optional<nlohmann::json> fan;
2247002d39b4SEd Tanous                 if (!redfish::json_util::readJson(*openbmc, asyncResp->res,
2248002d39b4SEd Tanous                                                   "Fan", fan))
22497e860f15SJohn Edward Broadbent                 {
22507e860f15SJohn Edward Broadbent                     BMCWEB_LOG_ERROR
22517e860f15SJohn Edward Broadbent                         << "Illegal Property "
2252002d39b4SEd Tanous                         << openbmc->dump(
2253002d39b4SEd Tanous                                2, ' ', true,
2254002d39b4SEd Tanous                                nlohmann::json::error_handler_t::replace);
22557e860f15SJohn Edward Broadbent                     return;
22567e860f15SJohn Edward Broadbent                 }
22577e860f15SJohn Edward Broadbent                 if (fan)
22587e860f15SJohn Edward Broadbent                 {
2259002d39b4SEd Tanous                     auto pid = std::make_shared<SetPIDValues>(asyncResp, *fan);
22607e860f15SJohn Edward Broadbent                     pid->run();
22617e860f15SJohn Edward Broadbent                 }
22627e860f15SJohn Edward Broadbent             }
226354dce7f5SGunnar Mills #else
226454dce7f5SGunnar Mills             messages::propertyUnknown(asyncResp->res, "Oem");
226554dce7f5SGunnar Mills             return;
226654dce7f5SGunnar Mills #endif
22677e860f15SJohn Edward Broadbent         }
22687e860f15SJohn Edward Broadbent         if (links)
22697e860f15SJohn Edward Broadbent         {
22707e860f15SJohn Edward Broadbent             std::optional<nlohmann::json> activeSoftwareImage;
22717e860f15SJohn Edward Broadbent             if (!redfish::json_util::readJson(*links, asyncResp->res,
22727e860f15SJohn Edward Broadbent                                               "ActiveSoftwareImage",
22737e860f15SJohn Edward Broadbent                                               activeSoftwareImage))
22747e860f15SJohn Edward Broadbent             {
22757e860f15SJohn Edward Broadbent                 return;
22767e860f15SJohn Edward Broadbent             }
22777e860f15SJohn Edward Broadbent             if (activeSoftwareImage)
22787e860f15SJohn Edward Broadbent             {
22797e860f15SJohn Edward Broadbent                 std::optional<std::string> odataId;
2280002d39b4SEd Tanous                 if (!json_util::readJson(*activeSoftwareImage, asyncResp->res,
2281002d39b4SEd Tanous                                          "@odata.id", odataId))
22827e860f15SJohn Edward Broadbent                 {
22837e860f15SJohn Edward Broadbent                     return;
22847e860f15SJohn Edward Broadbent                 }
22857e860f15SJohn Edward Broadbent 
22867e860f15SJohn Edward Broadbent                 if (odataId)
22877e860f15SJohn Edward Broadbent                 {
22887e860f15SJohn Edward Broadbent                     setActiveFirmwareImage(asyncResp, *odataId);
22897e860f15SJohn Edward Broadbent                 }
22907e860f15SJohn Edward Broadbent             }
22917e860f15SJohn Edward Broadbent         }
22927e860f15SJohn Edward Broadbent         if (datetime)
22937e860f15SJohn Edward Broadbent         {
22947e860f15SJohn Edward Broadbent             setDateTime(asyncResp, std::move(*datetime));
22957e860f15SJohn Edward Broadbent         }
22967e860f15SJohn Edward Broadbent         });
22977e860f15SJohn Edward Broadbent }
22987e860f15SJohn Edward Broadbent 
22997e860f15SJohn Edward Broadbent inline void requestRoutesManagerCollection(App& app)
23007e860f15SJohn Edward Broadbent {
23017e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
2302ed398213SEd Tanous         .privileges(redfish::privileges::getManagerCollection)
23037e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
230445ca1b86SEd Tanous             [&app](const crow::Request& req,
23057e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
23063ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
230745ca1b86SEd Tanous         {
230845ca1b86SEd Tanous             return;
230945ca1b86SEd Tanous         }
231083ff9ab6SJames Feist         // Collections don't include the static data added by SubRoute
231183ff9ab6SJames Feist         // because it has a duplicate entry for members
23128d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
23138d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
23148d1b46d7Szhanghch05             "#ManagerCollection.ManagerCollection";
23158d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Manager Collection";
23168d1b46d7Szhanghch05         asyncResp->res.jsonValue["Members@odata.count"] = 1;
23171476687dSEd Tanous         nlohmann::json::array_t members;
23181476687dSEd Tanous         nlohmann::json& bmc = members.emplace_back();
23191476687dSEd Tanous         bmc["@odata.id"] = "/redfish/v1/Managers/bmc";
23201476687dSEd Tanous         asyncResp->res.jsonValue["Members"] = std::move(members);
23217e860f15SJohn Edward Broadbent         });
23229c310685SBorawski.Lukasz }
23239c310685SBorawski.Lukasz } // namespace redfish
2324