xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision fac6e53bf1d5d3099db6890f8935f4f053fbb4d7)
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"
24*fac6e53bSKrzysztof Grobelny #include "utils/dbus_utils.hpp"
25a51fc2d2SSui Chen #include "utils/sw_utils.hpp"
26a51fc2d2SSui Chen #include "utils/systemd_utils.hpp"
279c310685SBorawski.Lukasz 
28af5d6058SSantosh Puranik #include <boost/date_time.hpp>
29*fac6e53bSKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
30*fac6e53bSKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
311214b7e7SGunnar Mills 
32a170f275SEd Tanous #include <algorithm>
334bfefa74SGunnar Mills #include <cstdint>
341214b7e7SGunnar Mills #include <memory>
351214b7e7SGunnar Mills #include <sstream>
36abf2add6SEd Tanous #include <variant>
375b4aa86bSJames Feist 
381abe55efSEd Tanous namespace redfish
391abe55efSEd Tanous {
40ed5befbdSJennifer Lee 
41ed5befbdSJennifer Lee /**
422a5c4407SGunnar Mills  * Function reboots the BMC.
432a5c4407SGunnar Mills  *
442a5c4407SGunnar Mills  * @param[in] asyncResp - Shared pointer for completing asynchronous calls
45ed5befbdSJennifer Lee  */
468d1b46d7Szhanghch05 inline void
478d1b46d7Szhanghch05     doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
48ed5befbdSJennifer Lee {
49ed5befbdSJennifer Lee     const char* processName = "xyz.openbmc_project.State.BMC";
50ed5befbdSJennifer Lee     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
51ed5befbdSJennifer Lee     const char* interfaceName = "xyz.openbmc_project.State.BMC";
52ed5befbdSJennifer Lee     const std::string& propertyValue =
53ed5befbdSJennifer Lee         "xyz.openbmc_project.State.BMC.Transition.Reboot";
54ed5befbdSJennifer Lee     const char* destProperty = "RequestedBMCTransition";
55ed5befbdSJennifer Lee 
56ed5befbdSJennifer Lee     // Create the D-Bus variant for D-Bus call.
57168e20c1SEd Tanous     dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
58ed5befbdSJennifer Lee 
59ed5befbdSJennifer Lee     crow::connections::systemBus->async_method_call(
60ed5befbdSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
61ed5befbdSJennifer Lee         // Use "Set" method to set the property value.
62ed5befbdSJennifer Lee         if (ec)
63ed5befbdSJennifer Lee         {
642a5c4407SGunnar Mills             BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
65ed5befbdSJennifer Lee             messages::internalError(asyncResp->res);
66ed5befbdSJennifer Lee             return;
67ed5befbdSJennifer Lee         }
68ed5befbdSJennifer Lee 
69ed5befbdSJennifer Lee         messages::success(asyncResp->res);
70ed5befbdSJennifer Lee         },
71ed5befbdSJennifer Lee         processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
72ed5befbdSJennifer Lee         interfaceName, destProperty, dbusPropertyValue);
73ed5befbdSJennifer Lee }
742a5c4407SGunnar Mills 
758d1b46d7Szhanghch05 inline void
768d1b46d7Szhanghch05     doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
77f92af389SJayaprakash Mutyala {
78f92af389SJayaprakash Mutyala     const char* processName = "xyz.openbmc_project.State.BMC";
79f92af389SJayaprakash Mutyala     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
80f92af389SJayaprakash Mutyala     const char* interfaceName = "xyz.openbmc_project.State.BMC";
81f92af389SJayaprakash Mutyala     const std::string& propertyValue =
82f92af389SJayaprakash Mutyala         "xyz.openbmc_project.State.BMC.Transition.HardReboot";
83f92af389SJayaprakash Mutyala     const char* destProperty = "RequestedBMCTransition";
84f92af389SJayaprakash Mutyala 
85f92af389SJayaprakash Mutyala     // Create the D-Bus variant for D-Bus call.
86168e20c1SEd Tanous     dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
87f92af389SJayaprakash Mutyala 
88f92af389SJayaprakash Mutyala     crow::connections::systemBus->async_method_call(
89f92af389SJayaprakash Mutyala         [asyncResp](const boost::system::error_code ec) {
90f92af389SJayaprakash Mutyala         // Use "Set" method to set the property value.
91f92af389SJayaprakash Mutyala         if (ec)
92f92af389SJayaprakash Mutyala         {
93f92af389SJayaprakash Mutyala             BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
94f92af389SJayaprakash Mutyala             messages::internalError(asyncResp->res);
95f92af389SJayaprakash Mutyala             return;
96f92af389SJayaprakash Mutyala         }
97f92af389SJayaprakash Mutyala 
98f92af389SJayaprakash Mutyala         messages::success(asyncResp->res);
99f92af389SJayaprakash Mutyala         },
100f92af389SJayaprakash Mutyala         processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
101f92af389SJayaprakash Mutyala         interfaceName, destProperty, dbusPropertyValue);
102f92af389SJayaprakash Mutyala }
103f92af389SJayaprakash Mutyala 
1042a5c4407SGunnar Mills /**
1052a5c4407SGunnar Mills  * ManagerResetAction class supports the POST method for the Reset (reboot)
1062a5c4407SGunnar Mills  * action.
1072a5c4407SGunnar Mills  */
1087e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetAction(App& app)
1092a5c4407SGunnar Mills {
1102a5c4407SGunnar Mills     /**
1112a5c4407SGunnar Mills      * Function handles POST method request.
1122a5c4407SGunnar Mills      * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
113f92af389SJayaprakash Mutyala      * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
1142a5c4407SGunnar Mills      */
1157e860f15SJohn Edward Broadbent 
1167e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
117ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
1187e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
11945ca1b86SEd Tanous             [&app](const crow::Request& req,
1207e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1213ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
12245ca1b86SEd Tanous         {
12345ca1b86SEd Tanous             return;
12445ca1b86SEd Tanous         }
1252a5c4407SGunnar Mills         BMCWEB_LOG_DEBUG << "Post Manager Reset.";
1262a5c4407SGunnar Mills 
1272a5c4407SGunnar Mills         std::string resetType;
1282a5c4407SGunnar Mills 
12915ed6780SWilly Tu         if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
1307e860f15SJohn Edward Broadbent                                        resetType))
1312a5c4407SGunnar Mills         {
1322a5c4407SGunnar Mills             return;
1332a5c4407SGunnar Mills         }
1342a5c4407SGunnar Mills 
135f92af389SJayaprakash Mutyala         if (resetType == "GracefulRestart")
136f92af389SJayaprakash Mutyala         {
137f92af389SJayaprakash Mutyala             BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
138f92af389SJayaprakash Mutyala             doBMCGracefulRestart(asyncResp);
139f92af389SJayaprakash Mutyala             return;
140f92af389SJayaprakash Mutyala         }
1413174e4dfSEd Tanous         if (resetType == "ForceRestart")
142f92af389SJayaprakash Mutyala         {
143f92af389SJayaprakash Mutyala             BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
144f92af389SJayaprakash Mutyala             doBMCForceRestart(asyncResp);
145f92af389SJayaprakash Mutyala             return;
146f92af389SJayaprakash Mutyala         }
1472a5c4407SGunnar Mills         BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
1482a5c4407SGunnar Mills                          << resetType;
1492a5c4407SGunnar Mills         messages::actionParameterNotSupported(asyncResp->res, resetType,
1502a5c4407SGunnar Mills                                               "ResetType");
1512a5c4407SGunnar Mills 
1522a5c4407SGunnar Mills         return;
1537e860f15SJohn Edward Broadbent         });
1542a5c4407SGunnar Mills }
155ed5befbdSJennifer Lee 
1563e40fc74SGunnar Mills /**
1573e40fc74SGunnar Mills  * ManagerResetToDefaultsAction class supports POST method for factory reset
1583e40fc74SGunnar Mills  * action.
1593e40fc74SGunnar Mills  */
1607e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetToDefaultsAction(App& app)
1613e40fc74SGunnar Mills {
1623e40fc74SGunnar Mills 
1633e40fc74SGunnar Mills     /**
1643e40fc74SGunnar Mills      * Function handles ResetToDefaults POST method request.
1653e40fc74SGunnar Mills      *
1663e40fc74SGunnar Mills      * Analyzes POST body message and factory resets BMC by calling
1673e40fc74SGunnar Mills      * BMC code updater factory reset followed by a BMC reboot.
1683e40fc74SGunnar Mills      *
1693e40fc74SGunnar Mills      * BMC code updater factory reset wipes the whole BMC read-write
1703e40fc74SGunnar Mills      * filesystem which includes things like the network settings.
1713e40fc74SGunnar Mills      *
1723e40fc74SGunnar Mills      * OpenBMC only supports ResetToDefaultsType "ResetAll".
1733e40fc74SGunnar Mills      */
1747e860f15SJohn Edward Broadbent 
1757e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
1767e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/")
177ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
1787e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
17945ca1b86SEd Tanous             [&app](const crow::Request& req,
1807e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1813ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
18245ca1b86SEd Tanous         {
18345ca1b86SEd Tanous             return;
18445ca1b86SEd Tanous         }
1853e40fc74SGunnar Mills         BMCWEB_LOG_DEBUG << "Post ResetToDefaults.";
1863e40fc74SGunnar Mills 
1873e40fc74SGunnar Mills         std::string resetType;
1883e40fc74SGunnar Mills 
189002d39b4SEd Tanous         if (!json_util::readJsonAction(req, asyncResp->res,
190002d39b4SEd Tanous                                        "ResetToDefaultsType", resetType))
1913e40fc74SGunnar Mills         {
1923e40fc74SGunnar Mills             BMCWEB_LOG_DEBUG << "Missing property ResetToDefaultsType.";
1933e40fc74SGunnar Mills 
194002d39b4SEd Tanous             messages::actionParameterMissing(asyncResp->res, "ResetToDefaults",
1953e40fc74SGunnar Mills                                              "ResetToDefaultsType");
1963e40fc74SGunnar Mills             return;
1973e40fc74SGunnar Mills         }
1983e40fc74SGunnar Mills 
1993e40fc74SGunnar Mills         if (resetType != "ResetAll")
2003e40fc74SGunnar Mills         {
2010fda0f12SGeorge Liu             BMCWEB_LOG_DEBUG
2020fda0f12SGeorge Liu                 << "Invalid property value for ResetToDefaultsType: "
2033e40fc74SGunnar Mills                 << resetType;
204002d39b4SEd Tanous             messages::actionParameterNotSupported(asyncResp->res, resetType,
205002d39b4SEd Tanous                                                   "ResetToDefaultsType");
2063e40fc74SGunnar Mills             return;
2073e40fc74SGunnar Mills         }
2083e40fc74SGunnar Mills 
2093e40fc74SGunnar Mills         crow::connections::systemBus->async_method_call(
2103e40fc74SGunnar Mills             [asyncResp](const boost::system::error_code ec) {
2113e40fc74SGunnar Mills             if (ec)
2123e40fc74SGunnar Mills             {
213002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "Failed to ResetToDefaults: " << ec;
2143e40fc74SGunnar Mills                 messages::internalError(asyncResp->res);
2153e40fc74SGunnar Mills                 return;
2163e40fc74SGunnar Mills             }
2173e40fc74SGunnar Mills             // Factory Reset doesn't actually happen until a reboot
2183e40fc74SGunnar Mills             // Can't erase what the BMC is running on
2193e40fc74SGunnar Mills             doBMCGracefulRestart(asyncResp);
2203e40fc74SGunnar Mills             },
2213e40fc74SGunnar Mills             "xyz.openbmc_project.Software.BMC.Updater",
2223e40fc74SGunnar Mills             "/xyz/openbmc_project/software",
2233e40fc74SGunnar Mills             "xyz.openbmc_project.Common.FactoryReset", "Reset");
2247e860f15SJohn Edward Broadbent         });
2253e40fc74SGunnar Mills }
2263e40fc74SGunnar Mills 
2271cb1a9e6SAppaRao Puli /**
2281cb1a9e6SAppaRao Puli  * ManagerResetActionInfo derived class for delivering Manager
2291cb1a9e6SAppaRao Puli  * ResetType AllowableValues using ResetInfo schema.
2301cb1a9e6SAppaRao Puli  */
2317e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetActionInfo(App& app)
2321cb1a9e6SAppaRao Puli {
2331cb1a9e6SAppaRao Puli     /**
2341cb1a9e6SAppaRao Puli      * Functions triggers appropriate requests on DBus
2351cb1a9e6SAppaRao Puli      */
2367e860f15SJohn Edward Broadbent 
2377e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/ResetActionInfo/")
238ed398213SEd Tanous         .privileges(redfish::privileges::getActionInfo)
2397e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
24045ca1b86SEd Tanous             [&app](const crow::Request& req,
2417e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2423ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
24345ca1b86SEd Tanous         {
24445ca1b86SEd Tanous             return;
24545ca1b86SEd Tanous         }
2461476687dSEd Tanous 
2471476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
2481476687dSEd Tanous             "#ActionInfo.v1_1_2.ActionInfo";
2491476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
2501476687dSEd Tanous             "/redfish/v1/Managers/bmc/ResetActionInfo";
2511476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "Reset Action Info";
2521476687dSEd Tanous         asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
2531476687dSEd Tanous         nlohmann::json::object_t parameter;
2541476687dSEd Tanous         parameter["Name"] = "ResetType";
2551476687dSEd Tanous         parameter["Required"] = true;
2561476687dSEd Tanous         parameter["DataType"] = "String";
2571476687dSEd Tanous 
2581476687dSEd Tanous         nlohmann::json::array_t allowableValues;
2591476687dSEd Tanous         allowableValues.push_back("GracefulRestart");
2601476687dSEd Tanous         allowableValues.push_back("ForceRestart");
2611476687dSEd Tanous         parameter["AllowableValues"] = std::move(allowableValues);
2621476687dSEd Tanous 
2631476687dSEd Tanous         nlohmann::json::array_t parameters;
2641476687dSEd Tanous         parameters.push_back(std::move(parameter));
2651476687dSEd Tanous 
2661476687dSEd Tanous         asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
2677e860f15SJohn Edward Broadbent         });
2681cb1a9e6SAppaRao Puli }
2691cb1a9e6SAppaRao Puli 
2705b4aa86bSJames Feist static constexpr const char* objectManagerIface =
2715b4aa86bSJames Feist     "org.freedesktop.DBus.ObjectManager";
2725b4aa86bSJames Feist static constexpr const char* pidConfigurationIface =
2735b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid";
2745b4aa86bSJames Feist static constexpr const char* pidZoneConfigurationIface =
2755b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid.Zone";
276b7a08d04SJames Feist static constexpr const char* stepwiseConfigurationIface =
277b7a08d04SJames Feist     "xyz.openbmc_project.Configuration.Stepwise";
27873df0db0SJames Feist static constexpr const char* thermalModeIface =
27973df0db0SJames Feist     "xyz.openbmc_project.Control.ThermalMode";
2809c310685SBorawski.Lukasz 
2818d1b46d7Szhanghch05 inline void
2828d1b46d7Szhanghch05     asyncPopulatePid(const std::string& connection, const std::string& path,
28373df0db0SJames Feist                      const std::string& currentProfile,
28473df0db0SJames Feist                      const std::vector<std::string>& supportedProfiles,
2858d1b46d7Szhanghch05                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2865b4aa86bSJames Feist {
2875b4aa86bSJames Feist 
2885b4aa86bSJames Feist     crow::connections::systemBus->async_method_call(
28973df0db0SJames Feist         [asyncResp, currentProfile, supportedProfiles](
29073df0db0SJames Feist             const boost::system::error_code ec,
2915b4aa86bSJames Feist             const dbus::utility::ManagedObjectType& managedObj) {
2925b4aa86bSJames Feist         if (ec)
2935b4aa86bSJames Feist         {
2945b4aa86bSJames Feist             BMCWEB_LOG_ERROR << ec;
2955b4aa86bSJames Feist             asyncResp->res.jsonValue.clear();
296f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2975b4aa86bSJames Feist             return;
2985b4aa86bSJames Feist         }
2995b4aa86bSJames Feist         nlohmann::json& configRoot =
3005b4aa86bSJames Feist             asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
3015b4aa86bSJames Feist         nlohmann::json& fans = configRoot["FanControllers"];
3025b4aa86bSJames Feist         fans["@odata.type"] = "#OemManager.FanControllers";
3030fda0f12SGeorge Liu         fans["@odata.id"] =
3040fda0f12SGeorge Liu             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers";
3055b4aa86bSJames Feist 
3065b4aa86bSJames Feist         nlohmann::json& pids = configRoot["PidControllers"];
3075b4aa86bSJames Feist         pids["@odata.type"] = "#OemManager.PidControllers";
3085b4aa86bSJames Feist         pids["@odata.id"] =
3095b4aa86bSJames Feist             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
3105b4aa86bSJames Feist 
311b7a08d04SJames Feist         nlohmann::json& stepwise = configRoot["StepwiseControllers"];
312b7a08d04SJames Feist         stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
313b7a08d04SJames Feist         stepwise["@odata.id"] =
314b7a08d04SJames Feist             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
315b7a08d04SJames Feist 
3165b4aa86bSJames Feist         nlohmann::json& zones = configRoot["FanZones"];
3175b4aa86bSJames Feist         zones["@odata.id"] =
3185b4aa86bSJames Feist             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
3195b4aa86bSJames Feist         zones["@odata.type"] = "#OemManager.FanZones";
320002d39b4SEd Tanous         configRoot["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
3215b4aa86bSJames Feist         configRoot["@odata.type"] = "#OemManager.Fan";
32273df0db0SJames Feist         configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
32373df0db0SJames Feist 
32473df0db0SJames Feist         if (!currentProfile.empty())
32573df0db0SJames Feist         {
32673df0db0SJames Feist             configRoot["Profile"] = currentProfile;
32773df0db0SJames Feist         }
32873df0db0SJames Feist         BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !";
3295b4aa86bSJames Feist 
3305b4aa86bSJames Feist         for (const auto& pathPair : managedObj)
3315b4aa86bSJames Feist         {
3325b4aa86bSJames Feist             for (const auto& intfPair : pathPair.second)
3335b4aa86bSJames Feist             {
3345b4aa86bSJames Feist                 if (intfPair.first != pidConfigurationIface &&
335b7a08d04SJames Feist                     intfPair.first != pidZoneConfigurationIface &&
336b7a08d04SJames Feist                     intfPair.first != stepwiseConfigurationIface)
3375b4aa86bSJames Feist                 {
3385b4aa86bSJames Feist                     continue;
3395b4aa86bSJames Feist                 }
34073df0db0SJames Feist 
341711ac7a9SEd Tanous                 std::string name;
342711ac7a9SEd Tanous 
343711ac7a9SEd Tanous                 for (const std::pair<std::string,
344002d39b4SEd Tanous                                      dbus::utility::DbusVariantType>& propPair :
345002d39b4SEd Tanous                      intfPair.second)
346711ac7a9SEd Tanous                 {
347711ac7a9SEd Tanous                     if (propPair.first == "Name")
348711ac7a9SEd Tanous                     {
3495b4aa86bSJames Feist                         const std::string* namePtr =
350711ac7a9SEd Tanous                             std::get_if<std::string>(&propPair.second);
3515b4aa86bSJames Feist                         if (namePtr == nullptr)
3525b4aa86bSJames Feist                         {
3535b4aa86bSJames Feist                             BMCWEB_LOG_ERROR << "Pid Name Field illegal";
354b7a08d04SJames Feist                             messages::internalError(asyncResp->res);
3555b4aa86bSJames Feist                             return;
3565b4aa86bSJames Feist                         }
357db697703SWilly Tu                         name = *namePtr;
3585b4aa86bSJames Feist                         dbus::utility::escapePathForDbus(name);
359711ac7a9SEd Tanous                     }
360711ac7a9SEd Tanous                     else if (propPair.first == "Profiles")
36173df0db0SJames Feist                     {
36273df0db0SJames Feist                         const std::vector<std::string>* profiles =
36373df0db0SJames Feist                             std::get_if<std::vector<std::string>>(
364711ac7a9SEd Tanous                                 &propPair.second);
36573df0db0SJames Feist                         if (profiles == nullptr)
36673df0db0SJames Feist                         {
367002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "Pid Profiles Field illegal";
36873df0db0SJames Feist                             messages::internalError(asyncResp->res);
36973df0db0SJames Feist                             return;
37073df0db0SJames Feist                         }
37173df0db0SJames Feist                         if (std::find(profiles->begin(), profiles->end(),
37273df0db0SJames Feist                                       currentProfile) == profiles->end())
37373df0db0SJames Feist                         {
37473df0db0SJames Feist                             BMCWEB_LOG_INFO
375002d39b4SEd Tanous                                 << name << " not supported in current profile";
37673df0db0SJames Feist                             continue;
37773df0db0SJames Feist                         }
37873df0db0SJames Feist                     }
379711ac7a9SEd Tanous                 }
380b7a08d04SJames Feist                 nlohmann::json* config = nullptr;
381c33a90ecSJames Feist                 const std::string* classPtr = nullptr;
382711ac7a9SEd Tanous 
383711ac7a9SEd Tanous                 for (const std::pair<std::string,
384002d39b4SEd Tanous                                      dbus::utility::DbusVariantType>& propPair :
385002d39b4SEd Tanous                      intfPair.second)
386c33a90ecSJames Feist                 {
387727dc83fSLei YU                     if (propPair.first == "Class")
388711ac7a9SEd Tanous                     {
389002d39b4SEd Tanous                         classPtr = std::get_if<std::string>(&propPair.second);
390711ac7a9SEd Tanous                     }
391c33a90ecSJames Feist                 }
392c33a90ecSJames Feist 
3935b4aa86bSJames Feist                 if (intfPair.first == pidZoneConfigurationIface)
3945b4aa86bSJames Feist                 {
3955b4aa86bSJames Feist                     std::string chassis;
396002d39b4SEd Tanous                     if (!dbus::utility::getNthStringFromPath(pathPair.first.str,
397002d39b4SEd Tanous                                                              5, chassis))
3985b4aa86bSJames Feist                     {
3995b4aa86bSJames Feist                         chassis = "#IllegalValue";
4005b4aa86bSJames Feist                     }
4015b4aa86bSJames Feist                     nlohmann::json& zone = zones[name];
4025b4aa86bSJames Feist                     zone["Chassis"] = {
4035b4aa86bSJames Feist                         {"@odata.id", "/redfish/v1/Chassis/" + chassis}};
4040fda0f12SGeorge Liu                     zone["@odata.id"] =
4050fda0f12SGeorge Liu                         "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/" +
4065b4aa86bSJames Feist                         name;
4075b4aa86bSJames Feist                     zone["@odata.type"] = "#OemManager.FanZone";
408b7a08d04SJames Feist                     config = &zone;
4095b4aa86bSJames Feist                 }
4105b4aa86bSJames Feist 
411b7a08d04SJames Feist                 else if (intfPair.first == stepwiseConfigurationIface)
4125b4aa86bSJames Feist                 {
413c33a90ecSJames Feist                     if (classPtr == nullptr)
414c33a90ecSJames Feist                     {
415c33a90ecSJames Feist                         BMCWEB_LOG_ERROR << "Pid Class Field illegal";
416c33a90ecSJames Feist                         messages::internalError(asyncResp->res);
417c33a90ecSJames Feist                         return;
418c33a90ecSJames Feist                     }
419c33a90ecSJames Feist 
420b7a08d04SJames Feist                     nlohmann::json& controller = stepwise[name];
421b7a08d04SJames Feist                     config = &controller;
4225b4aa86bSJames Feist 
423b7a08d04SJames Feist                     controller["@odata.id"] =
4240fda0f12SGeorge Liu                         "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers/" +
425271584abSEd Tanous                         name;
426b7a08d04SJames Feist                     controller["@odata.type"] =
427b7a08d04SJames Feist                         "#OemManager.StepwiseController";
428b7a08d04SJames Feist 
429c33a90ecSJames Feist                     controller["Direction"] = *classPtr;
4305b4aa86bSJames Feist                 }
4315b4aa86bSJames Feist 
4325b4aa86bSJames Feist                 // pid and fans are off the same configuration
433b7a08d04SJames Feist                 else if (intfPair.first == pidConfigurationIface)
4345b4aa86bSJames Feist                 {
435c33a90ecSJames Feist 
4365b4aa86bSJames Feist                     if (classPtr == nullptr)
4375b4aa86bSJames Feist                     {
4385b4aa86bSJames Feist                         BMCWEB_LOG_ERROR << "Pid Class Field illegal";
439a08b46ccSJason M. Bills                         messages::internalError(asyncResp->res);
4405b4aa86bSJames Feist                         return;
4415b4aa86bSJames Feist                     }
4425b4aa86bSJames Feist                     bool isFan = *classPtr == "fan";
443002d39b4SEd Tanous                     nlohmann::json& element = isFan ? fans[name] : pids[name];
444b7a08d04SJames Feist                     config = &element;
4455b4aa86bSJames Feist                     if (isFan)
4465b4aa86bSJames Feist                     {
4475b4aa86bSJames Feist                         element["@odata.id"] =
4480fda0f12SGeorge Liu                             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers/" +
449271584abSEd Tanous                             name;
450002d39b4SEd Tanous                         element["@odata.type"] = "#OemManager.FanController";
4515b4aa86bSJames Feist                     }
4525b4aa86bSJames Feist                     else
4535b4aa86bSJames Feist                     {
4545b4aa86bSJames Feist                         element["@odata.id"] =
4550fda0f12SGeorge Liu                             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers/" +
456271584abSEd Tanous                             name;
457002d39b4SEd Tanous                         element["@odata.type"] = "#OemManager.PidController";
4585b4aa86bSJames Feist                     }
459b7a08d04SJames Feist                 }
460b7a08d04SJames Feist                 else
461b7a08d04SJames Feist                 {
462b7a08d04SJames Feist                     BMCWEB_LOG_ERROR << "Unexpected configuration";
463b7a08d04SJames Feist                     messages::internalError(asyncResp->res);
464b7a08d04SJames Feist                     return;
465b7a08d04SJames Feist                 }
466b7a08d04SJames Feist 
467b7a08d04SJames Feist                 // used for making maps out of 2 vectors
468b7a08d04SJames Feist                 const std::vector<double>* keys = nullptr;
469b7a08d04SJames Feist                 const std::vector<double>* values = nullptr;
470b7a08d04SJames Feist 
471b7a08d04SJames Feist                 for (const auto& propertyPair : intfPair.second)
472b7a08d04SJames Feist                 {
473b7a08d04SJames Feist                     if (propertyPair.first == "Type" ||
474b7a08d04SJames Feist                         propertyPair.first == "Class" ||
475b7a08d04SJames Feist                         propertyPair.first == "Name")
476b7a08d04SJames Feist                     {
477b7a08d04SJames Feist                         continue;
478b7a08d04SJames Feist                     }
479b7a08d04SJames Feist 
480b7a08d04SJames Feist                     // zones
481b7a08d04SJames Feist                     if (intfPair.first == pidZoneConfigurationIface)
482b7a08d04SJames Feist                     {
483b7a08d04SJames Feist                         const double* ptr =
484abf2add6SEd Tanous                             std::get_if<double>(&propertyPair.second);
485b7a08d04SJames Feist                         if (ptr == nullptr)
486b7a08d04SJames Feist                         {
487b7a08d04SJames Feist                             BMCWEB_LOG_ERROR << "Field Illegal "
488b7a08d04SJames Feist                                              << propertyPair.first;
489b7a08d04SJames Feist                             messages::internalError(asyncResp->res);
490b7a08d04SJames Feist                             return;
491b7a08d04SJames Feist                         }
492b7a08d04SJames Feist                         (*config)[propertyPair.first] = *ptr;
493b7a08d04SJames Feist                     }
494b7a08d04SJames Feist 
495b7a08d04SJames Feist                     if (intfPair.first == stepwiseConfigurationIface)
496b7a08d04SJames Feist                     {
497b7a08d04SJames Feist                         if (propertyPair.first == "Reading" ||
498b7a08d04SJames Feist                             propertyPair.first == "Output")
499b7a08d04SJames Feist                         {
500b7a08d04SJames Feist                             const std::vector<double>* ptr =
501abf2add6SEd Tanous                                 std::get_if<std::vector<double>>(
502b7a08d04SJames Feist                                     &propertyPair.second);
503b7a08d04SJames Feist 
504b7a08d04SJames Feist                             if (ptr == nullptr)
505b7a08d04SJames Feist                             {
506b7a08d04SJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
507b7a08d04SJames Feist                                                  << propertyPair.first;
508b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
509b7a08d04SJames Feist                                 return;
510b7a08d04SJames Feist                             }
511b7a08d04SJames Feist 
512b7a08d04SJames Feist                             if (propertyPair.first == "Reading")
513b7a08d04SJames Feist                             {
514b7a08d04SJames Feist                                 keys = ptr;
515b7a08d04SJames Feist                             }
516b7a08d04SJames Feist                             else
517b7a08d04SJames Feist                             {
518b7a08d04SJames Feist                                 values = ptr;
519b7a08d04SJames Feist                             }
520e662eae8SEd Tanous                             if (keys != nullptr && values != nullptr)
521b7a08d04SJames Feist                             {
522b7a08d04SJames Feist                                 if (keys->size() != values->size())
523b7a08d04SJames Feist                                 {
524b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR
5250fda0f12SGeorge Liu                                         << "Reading and Output size don't match ";
526b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
527b7a08d04SJames Feist                                     return;
528b7a08d04SJames Feist                                 }
529b7a08d04SJames Feist                                 nlohmann::json& steps = (*config)["Steps"];
530b7a08d04SJames Feist                                 steps = nlohmann::json::array();
531b7a08d04SJames Feist                                 for (size_t ii = 0; ii < keys->size(); ii++)
532b7a08d04SJames Feist                                 {
5331476687dSEd Tanous                                     nlohmann::json::object_t step;
5341476687dSEd Tanous                                     step["Target"] = (*keys)[ii];
5351476687dSEd Tanous                                     step["Output"] = (*values)[ii];
5361476687dSEd Tanous                                     steps.push_back(std::move(step));
537b7a08d04SJames Feist                                 }
538b7a08d04SJames Feist                             }
539b7a08d04SJames Feist                         }
540b7a08d04SJames Feist                         if (propertyPair.first == "NegativeHysteresis" ||
541b7a08d04SJames Feist                             propertyPair.first == "PositiveHysteresis")
542b7a08d04SJames Feist                         {
543b7a08d04SJames Feist                             const double* ptr =
544abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
545b7a08d04SJames Feist                             if (ptr == nullptr)
546b7a08d04SJames Feist                             {
547b7a08d04SJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
548b7a08d04SJames Feist                                                  << propertyPair.first;
549b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
550b7a08d04SJames Feist                                 return;
551b7a08d04SJames Feist                             }
552b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
553b7a08d04SJames Feist                         }
554b7a08d04SJames Feist                     }
555b7a08d04SJames Feist 
556b7a08d04SJames Feist                     // pid and fans are off the same configuration
557b7a08d04SJames Feist                     if (intfPair.first == pidConfigurationIface ||
558b7a08d04SJames Feist                         intfPair.first == stepwiseConfigurationIface)
559b7a08d04SJames Feist                     {
5605b4aa86bSJames Feist 
5615b4aa86bSJames Feist                         if (propertyPair.first == "Zones")
5625b4aa86bSJames Feist                         {
5635b4aa86bSJames Feist                             const std::vector<std::string>* inputs =
564abf2add6SEd Tanous                                 std::get_if<std::vector<std::string>>(
5651b6b96c5SEd Tanous                                     &propertyPair.second);
5665b4aa86bSJames Feist 
5675b4aa86bSJames Feist                             if (inputs == nullptr)
5685b4aa86bSJames Feist                             {
569002d39b4SEd Tanous                                 BMCWEB_LOG_ERROR << "Zones Pid Field Illegal";
570a08b46ccSJason M. Bills                                 messages::internalError(asyncResp->res);
5715b4aa86bSJames Feist                                 return;
5725b4aa86bSJames Feist                             }
573b7a08d04SJames Feist                             auto& data = (*config)[propertyPair.first];
5745b4aa86bSJames Feist                             data = nlohmann::json::array();
5755b4aa86bSJames Feist                             for (std::string itemCopy : *inputs)
5765b4aa86bSJames Feist                             {
5775b4aa86bSJames Feist                                 dbus::utility::escapePathForDbus(itemCopy);
5781476687dSEd Tanous                                 nlohmann::json::object_t input;
5791476687dSEd Tanous                                 input["@odata.id"] =
5800fda0f12SGeorge Liu                                     "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/" +
5811476687dSEd Tanous                                     itemCopy;
5821476687dSEd Tanous                                 data.push_back(std::move(input));
5835b4aa86bSJames Feist                             }
5845b4aa86bSJames Feist                         }
5855b4aa86bSJames Feist                         // todo(james): may never happen, but this
5865b4aa86bSJames Feist                         // assumes configuration data referenced in the
5875b4aa86bSJames Feist                         // PID config is provided by the same daemon, we
5885b4aa86bSJames Feist                         // could add another loop to cover all cases,
5895b4aa86bSJames Feist                         // but I'm okay kicking this can down the road a
5905b4aa86bSJames Feist                         // bit
5915b4aa86bSJames Feist 
5925b4aa86bSJames Feist                         else if (propertyPair.first == "Inputs" ||
5935b4aa86bSJames Feist                                  propertyPair.first == "Outputs")
5945b4aa86bSJames Feist                         {
595b7a08d04SJames Feist                             auto& data = (*config)[propertyPair.first];
5965b4aa86bSJames Feist                             const std::vector<std::string>* inputs =
597abf2add6SEd Tanous                                 std::get_if<std::vector<std::string>>(
5981b6b96c5SEd Tanous                                     &propertyPair.second);
5995b4aa86bSJames Feist 
6005b4aa86bSJames Feist                             if (inputs == nullptr)
6015b4aa86bSJames Feist                             {
6025b4aa86bSJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
6035b4aa86bSJames Feist                                                  << propertyPair.first;
604f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
6055b4aa86bSJames Feist                                 return;
6065b4aa86bSJames Feist                             }
6075b4aa86bSJames Feist                             data = *inputs;
608b943aaefSJames Feist                         }
609b943aaefSJames Feist                         else if (propertyPair.first == "SetPointOffset")
610b943aaefSJames Feist                         {
611b943aaefSJames Feist                             const std::string* ptr =
612002d39b4SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
613b943aaefSJames Feist 
614b943aaefSJames Feist                             if (ptr == nullptr)
615b943aaefSJames Feist                             {
616b943aaefSJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
617b943aaefSJames Feist                                                  << propertyPair.first;
618b943aaefSJames Feist                                 messages::internalError(asyncResp->res);
619b943aaefSJames Feist                                 return;
620b943aaefSJames Feist                             }
621b943aaefSJames Feist                             // translate from dbus to redfish
622b943aaefSJames Feist                             if (*ptr == "WarningHigh")
623b943aaefSJames Feist                             {
624b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
625b943aaefSJames Feist                                     "UpperThresholdNonCritical";
626b943aaefSJames Feist                             }
627b943aaefSJames Feist                             else if (*ptr == "WarningLow")
628b943aaefSJames Feist                             {
629b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
630b943aaefSJames Feist                                     "LowerThresholdNonCritical";
631b943aaefSJames Feist                             }
632b943aaefSJames Feist                             else if (*ptr == "CriticalHigh")
633b943aaefSJames Feist                             {
634b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
635b943aaefSJames Feist                                     "UpperThresholdCritical";
636b943aaefSJames Feist                             }
637b943aaefSJames Feist                             else if (*ptr == "CriticalLow")
638b943aaefSJames Feist                             {
639b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
640b943aaefSJames Feist                                     "LowerThresholdCritical";
641b943aaefSJames Feist                             }
642b943aaefSJames Feist                             else
643b943aaefSJames Feist                             {
644002d39b4SEd Tanous                                 BMCWEB_LOG_ERROR << "Value Illegal " << *ptr;
645b943aaefSJames Feist                                 messages::internalError(asyncResp->res);
646b943aaefSJames Feist                                 return;
647b943aaefSJames Feist                             }
648b943aaefSJames Feist                         }
649b943aaefSJames Feist                         // doubles
650002d39b4SEd Tanous                         else if (propertyPair.first == "FFGainCoefficient" ||
6515b4aa86bSJames Feist                                  propertyPair.first == "FFOffCoefficient" ||
6525b4aa86bSJames Feist                                  propertyPair.first == "ICoefficient" ||
6535b4aa86bSJames Feist                                  propertyPair.first == "ILimitMax" ||
6545b4aa86bSJames Feist                                  propertyPair.first == "ILimitMin" ||
655002d39b4SEd Tanous                                  propertyPair.first == "PositiveHysteresis" ||
656002d39b4SEd Tanous                                  propertyPair.first == "NegativeHysteresis" ||
6575b4aa86bSJames Feist                                  propertyPair.first == "OutLimitMax" ||
6585b4aa86bSJames Feist                                  propertyPair.first == "OutLimitMin" ||
6595b4aa86bSJames Feist                                  propertyPair.first == "PCoefficient" ||
6607625cb81SJames Feist                                  propertyPair.first == "SetPoint" ||
6615b4aa86bSJames Feist                                  propertyPair.first == "SlewNeg" ||
6625b4aa86bSJames Feist                                  propertyPair.first == "SlewPos")
6635b4aa86bSJames Feist                         {
6645b4aa86bSJames Feist                             const double* ptr =
665abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
6665b4aa86bSJames Feist                             if (ptr == nullptr)
6675b4aa86bSJames Feist                             {
6685b4aa86bSJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
6695b4aa86bSJames Feist                                                  << propertyPair.first;
670f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
6715b4aa86bSJames Feist                                 return;
6725b4aa86bSJames Feist                             }
673b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
6745b4aa86bSJames Feist                         }
6755b4aa86bSJames Feist                     }
6765b4aa86bSJames Feist                 }
6775b4aa86bSJames Feist             }
6785b4aa86bSJames Feist         }
6795b4aa86bSJames Feist         },
6805b4aa86bSJames Feist         connection, path, objectManagerIface, "GetManagedObjects");
6815b4aa86bSJames Feist }
682ca537928SJennifer Lee 
68383ff9ab6SJames Feist enum class CreatePIDRet
68483ff9ab6SJames Feist {
68583ff9ab6SJames Feist     fail,
68683ff9ab6SJames Feist     del,
68783ff9ab6SJames Feist     patch
68883ff9ab6SJames Feist };
68983ff9ab6SJames Feist 
6908d1b46d7Szhanghch05 inline bool
6918d1b46d7Szhanghch05     getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
6925f2caaefSJames Feist                         std::vector<nlohmann::json>& config,
6935f2caaefSJames Feist                         std::vector<std::string>& zones)
6945f2caaefSJames Feist {
695b6baeaa4SJames Feist     if (config.empty())
696b6baeaa4SJames Feist     {
697b6baeaa4SJames Feist         BMCWEB_LOG_ERROR << "Empty Zones";
6981668ce6dSEd Tanous         messages::propertyValueFormatError(response->res, "[]", "Zones");
699b6baeaa4SJames Feist         return false;
700b6baeaa4SJames Feist     }
7015f2caaefSJames Feist     for (auto& odata : config)
7025f2caaefSJames Feist     {
7035f2caaefSJames Feist         std::string path;
7045f2caaefSJames Feist         if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
7055f2caaefSJames Feist                                           path))
7065f2caaefSJames Feist         {
7075f2caaefSJames Feist             return false;
7085f2caaefSJames Feist         }
7095f2caaefSJames Feist         std::string input;
71061adbda3SJames Feist 
71161adbda3SJames Feist         // 8 below comes from
71261adbda3SJames Feist         // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
71361adbda3SJames Feist         //     0    1     2      3    4    5      6     7      8
71461adbda3SJames Feist         if (!dbus::utility::getNthStringFromPath(path, 8, input))
7155f2caaefSJames Feist         {
7165f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Got invalid path " << path;
7175f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Illegal Type Zones";
7185f2caaefSJames Feist             messages::propertyValueFormatError(response->res, odata.dump(),
7195f2caaefSJames Feist                                                "Zones");
7205f2caaefSJames Feist             return false;
7215f2caaefSJames Feist         }
722a170f275SEd Tanous         std::replace(input.begin(), input.end(), '_', ' ');
7235f2caaefSJames Feist         zones.emplace_back(std::move(input));
7245f2caaefSJames Feist     }
7255f2caaefSJames Feist     return true;
7265f2caaefSJames Feist }
7275f2caaefSJames Feist 
728711ac7a9SEd Tanous inline const dbus::utility::ManagedObjectType::value_type*
72973df0db0SJames Feist     findChassis(const dbus::utility::ManagedObjectType& managedObj,
730b6baeaa4SJames Feist                 const std::string& value, std::string& chassis)
731b6baeaa4SJames Feist {
732b6baeaa4SJames Feist     BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
733b6baeaa4SJames Feist 
734a170f275SEd Tanous     std::string escaped = value;
735a170f275SEd Tanous     std::replace(escaped.begin(), escaped.end(), '_', ' ');
736b6baeaa4SJames Feist     escaped = "/" + escaped;
737002d39b4SEd Tanous     auto it = std::find_if(managedObj.begin(), managedObj.end(),
738002d39b4SEd Tanous                            [&escaped](const auto& obj) {
739b6baeaa4SJames Feist         if (boost::algorithm::ends_with(obj.first.str, escaped))
740b6baeaa4SJames Feist         {
741b6baeaa4SJames Feist             BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
742b6baeaa4SJames Feist             return true;
743b6baeaa4SJames Feist         }
744b6baeaa4SJames Feist         return false;
745b6baeaa4SJames Feist     });
746b6baeaa4SJames Feist 
747b6baeaa4SJames Feist     if (it == managedObj.end())
748b6baeaa4SJames Feist     {
74973df0db0SJames Feist         return nullptr;
750b6baeaa4SJames Feist     }
751b6baeaa4SJames Feist     // 5 comes from <chassis-name> being the 5th element
752b6baeaa4SJames Feist     // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
75373df0db0SJames Feist     if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
75473df0db0SJames Feist     {
75573df0db0SJames Feist         return &(*it);
75673df0db0SJames Feist     }
75773df0db0SJames Feist 
75873df0db0SJames Feist     return nullptr;
759b6baeaa4SJames Feist }
760b6baeaa4SJames Feist 
76123a21a1cSEd Tanous inline CreatePIDRet createPidInterface(
7628d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
763b5a76932SEd Tanous     const nlohmann::json::iterator& it, const std::string& path,
76483ff9ab6SJames Feist     const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
765b9d36b47SEd Tanous     dbus::utility::DBusPropertiesMap& output, std::string& chassis,
766b9d36b47SEd Tanous     const std::string& profile)
76783ff9ab6SJames Feist {
76883ff9ab6SJames Feist 
7695f2caaefSJames Feist     // common deleter
770b6baeaa4SJames Feist     if (it.value() == nullptr)
7715f2caaefSJames Feist     {
7725f2caaefSJames Feist         std::string iface;
7735f2caaefSJames Feist         if (type == "PidControllers" || type == "FanControllers")
7745f2caaefSJames Feist         {
7755f2caaefSJames Feist             iface = pidConfigurationIface;
7765f2caaefSJames Feist         }
7775f2caaefSJames Feist         else if (type == "FanZones")
7785f2caaefSJames Feist         {
7795f2caaefSJames Feist             iface = pidZoneConfigurationIface;
7805f2caaefSJames Feist         }
7815f2caaefSJames Feist         else if (type == "StepwiseControllers")
7825f2caaefSJames Feist         {
7835f2caaefSJames Feist             iface = stepwiseConfigurationIface;
7845f2caaefSJames Feist         }
7855f2caaefSJames Feist         else
7865f2caaefSJames Feist         {
787a0744d38SGunnar Mills             BMCWEB_LOG_ERROR << "Illegal Type " << type;
7885f2caaefSJames Feist             messages::propertyUnknown(response->res, type);
7895f2caaefSJames Feist             return CreatePIDRet::fail;
7905f2caaefSJames Feist         }
7916ee7f774SJames Feist 
7926ee7f774SJames Feist         BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
7935f2caaefSJames Feist         // delete interface
7945f2caaefSJames Feist         crow::connections::systemBus->async_method_call(
7955f2caaefSJames Feist             [response, path](const boost::system::error_code ec) {
7965f2caaefSJames Feist             if (ec)
7975f2caaefSJames Feist             {
7985f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
7995f2caaefSJames Feist                 messages::internalError(response->res);
800b6baeaa4SJames Feist                 return;
8015f2caaefSJames Feist             }
802b6baeaa4SJames Feist             messages::success(response->res);
8035f2caaefSJames Feist             },
8045f2caaefSJames Feist             "xyz.openbmc_project.EntityManager", path, iface, "Delete");
8055f2caaefSJames Feist         return CreatePIDRet::del;
8065f2caaefSJames Feist     }
8075f2caaefSJames Feist 
808711ac7a9SEd Tanous     const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
809b6baeaa4SJames Feist     if (!createNewObject)
810b6baeaa4SJames Feist     {
811b6baeaa4SJames Feist         // if we aren't creating a new object, we should be able to find it on
812b6baeaa4SJames Feist         // d-bus
81373df0db0SJames Feist         managedItem = findChassis(managedObj, it.key(), chassis);
81473df0db0SJames Feist         if (managedItem == nullptr)
815b6baeaa4SJames Feist         {
816b6baeaa4SJames Feist             BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
817ace85d60SEd Tanous             messages::invalidObject(response->res,
818ace85d60SEd Tanous                                     crow::utility::urlFromPieces(
819ace85d60SEd Tanous                                         "redfish", "v1", "Chassis", chassis));
820b6baeaa4SJames Feist             return CreatePIDRet::fail;
821b6baeaa4SJames Feist         }
822b6baeaa4SJames Feist     }
823b6baeaa4SJames Feist 
82426f6976fSEd Tanous     if (!profile.empty() &&
82573df0db0SJames Feist         (type == "PidControllers" || type == "FanControllers" ||
82673df0db0SJames Feist          type == "StepwiseControllers"))
82773df0db0SJames Feist     {
82873df0db0SJames Feist         if (managedItem == nullptr)
82973df0db0SJames Feist         {
830b9d36b47SEd Tanous             output.emplace_back("Profiles", std::vector<std::string>{profile});
83173df0db0SJames Feist         }
83273df0db0SJames Feist         else
83373df0db0SJames Feist         {
83473df0db0SJames Feist             std::string interface;
83573df0db0SJames Feist             if (type == "StepwiseControllers")
83673df0db0SJames Feist             {
83773df0db0SJames Feist                 interface = stepwiseConfigurationIface;
83873df0db0SJames Feist             }
83973df0db0SJames Feist             else
84073df0db0SJames Feist             {
84173df0db0SJames Feist                 interface = pidConfigurationIface;
84273df0db0SJames Feist             }
843711ac7a9SEd Tanous             bool ifaceFound = false;
844711ac7a9SEd Tanous             for (const auto& iface : managedItem->second)
845711ac7a9SEd Tanous             {
846711ac7a9SEd Tanous                 if (iface.first == interface)
847711ac7a9SEd Tanous                 {
848711ac7a9SEd Tanous                     ifaceFound = true;
849711ac7a9SEd Tanous                     for (const auto& prop : iface.second)
850711ac7a9SEd Tanous                     {
851711ac7a9SEd Tanous                         if (prop.first == "Profiles")
852711ac7a9SEd Tanous                         {
853711ac7a9SEd Tanous                             const std::vector<std::string>* curProfiles =
854711ac7a9SEd Tanous                                 std::get_if<std::vector<std::string>>(
855711ac7a9SEd Tanous                                     &(prop.second));
856711ac7a9SEd Tanous                             if (curProfiles == nullptr)
857711ac7a9SEd Tanous                             {
858711ac7a9SEd Tanous                                 BMCWEB_LOG_ERROR
859711ac7a9SEd Tanous                                     << "Illegal profiles in managed object";
860711ac7a9SEd Tanous                                 messages::internalError(response->res);
861711ac7a9SEd Tanous                                 return CreatePIDRet::fail;
862711ac7a9SEd Tanous                             }
863711ac7a9SEd Tanous                             if (std::find(curProfiles->begin(),
864711ac7a9SEd Tanous                                           curProfiles->end(),
865711ac7a9SEd Tanous                                           profile) == curProfiles->end())
866711ac7a9SEd Tanous                             {
867711ac7a9SEd Tanous                                 std::vector<std::string> newProfiles =
868711ac7a9SEd Tanous                                     *curProfiles;
869711ac7a9SEd Tanous                                 newProfiles.push_back(profile);
870b9d36b47SEd Tanous                                 output.emplace_back("Profiles", newProfiles);
871711ac7a9SEd Tanous                             }
872711ac7a9SEd Tanous                         }
873711ac7a9SEd Tanous                     }
874711ac7a9SEd Tanous                 }
875711ac7a9SEd Tanous             }
876711ac7a9SEd Tanous 
877711ac7a9SEd Tanous             if (!ifaceFound)
87873df0db0SJames Feist             {
87973df0db0SJames Feist                 BMCWEB_LOG_ERROR
88073df0db0SJames Feist                     << "Failed to find interface in managed object";
88173df0db0SJames Feist                 messages::internalError(response->res);
88273df0db0SJames Feist                 return CreatePIDRet::fail;
88373df0db0SJames Feist             }
88473df0db0SJames Feist         }
88573df0db0SJames Feist     }
88673df0db0SJames Feist 
88783ff9ab6SJames Feist     if (type == "PidControllers" || type == "FanControllers")
88883ff9ab6SJames Feist     {
88983ff9ab6SJames Feist         if (createNewObject)
89083ff9ab6SJames Feist         {
891b9d36b47SEd Tanous             output.emplace_back("Class",
892b9d36b47SEd Tanous                                 type == "PidControllers" ? "temp" : "fan");
893b9d36b47SEd Tanous             output.emplace_back("Type", "Pid");
89483ff9ab6SJames Feist         }
8955f2caaefSJames Feist 
8965f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
8975f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
8985f2caaefSJames Feist         std::optional<std::vector<std::string>> outputs;
8995f2caaefSJames Feist         std::map<std::string, std::optional<double>> doubles;
900b943aaefSJames Feist         std::optional<std::string> setpointOffset;
9015f2caaefSJames Feist         if (!redfish::json_util::readJson(
902b6baeaa4SJames Feist                 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
9035f2caaefSJames Feist                 "Zones", zones, "FFGainCoefficient",
9045f2caaefSJames Feist                 doubles["FFGainCoefficient"], "FFOffCoefficient",
9055f2caaefSJames Feist                 doubles["FFOffCoefficient"], "ICoefficient",
9065f2caaefSJames Feist                 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
9075f2caaefSJames Feist                 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
9085f2caaefSJames Feist                 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
9095f2caaefSJames Feist                 "PCoefficient", doubles["PCoefficient"], "SetPoint",
910b943aaefSJames Feist                 doubles["SetPoint"], "SetPointOffset", setpointOffset,
911b943aaefSJames Feist                 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
912b943aaefSJames Feist                 "PositiveHysteresis", doubles["PositiveHysteresis"],
913b943aaefSJames Feist                 "NegativeHysteresis", doubles["NegativeHysteresis"]))
91483ff9ab6SJames Feist         {
91571f52d96SEd Tanous             BMCWEB_LOG_ERROR
91671f52d96SEd Tanous                 << "Illegal Property "
91771f52d96SEd Tanous                 << it.value().dump(2, ' ', true,
91871f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace);
9195f2caaefSJames Feist             return CreatePIDRet::fail;
92083ff9ab6SJames Feist         }
9215f2caaefSJames Feist         if (zones)
9225f2caaefSJames Feist         {
9235f2caaefSJames Feist             std::vector<std::string> zonesStr;
9245f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStr))
9255f2caaefSJames Feist             {
926a0744d38SGunnar Mills                 BMCWEB_LOG_ERROR << "Illegal Zones";
9275f2caaefSJames Feist                 return CreatePIDRet::fail;
9285f2caaefSJames Feist             }
929b6baeaa4SJames Feist             if (chassis.empty() &&
930e662eae8SEd Tanous                 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
931b6baeaa4SJames Feist             {
932b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
933ace85d60SEd Tanous                 messages::invalidObject(
934ace85d60SEd Tanous                     response->res, crow::utility::urlFromPieces(
935ace85d60SEd Tanous                                        "redfish", "v1", "Chassis", chassis));
936b6baeaa4SJames Feist                 return CreatePIDRet::fail;
937b6baeaa4SJames Feist             }
938b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStr));
9395f2caaefSJames Feist         }
9405f2caaefSJames Feist         if (inputs || outputs)
9415f2caaefSJames Feist         {
94202cad96eSEd Tanous             std::array<
94302cad96eSEd Tanous                 std::reference_wrapper<std::optional<std::vector<std::string>>>,
94402cad96eSEd Tanous                 2>
94502cad96eSEd Tanous                 containers = {inputs, outputs};
9465f2caaefSJames Feist             size_t index = 0;
94702cad96eSEd Tanous             for (std::optional<std::vector<std::string>>& container :
94802cad96eSEd Tanous                  containers)
9495f2caaefSJames Feist             {
9505f2caaefSJames Feist                 if (!container)
9515f2caaefSJames Feist                 {
9525f2caaefSJames Feist                     index++;
9535f2caaefSJames Feist                     continue;
95483ff9ab6SJames Feist                 }
9555f2caaefSJames Feist                 for (std::string& value : *container)
95683ff9ab6SJames Feist                 {
957a170f275SEd Tanous                     std::replace(value.begin(), value.end(), '_', ' ');
95883ff9ab6SJames Feist                 }
9595f2caaefSJames Feist                 std::string key;
9605f2caaefSJames Feist                 if (index == 0)
9615f2caaefSJames Feist                 {
9625f2caaefSJames Feist                     key = "Inputs";
9635f2caaefSJames Feist                 }
9645f2caaefSJames Feist                 else
9655f2caaefSJames Feist                 {
9665f2caaefSJames Feist                     key = "Outputs";
9675f2caaefSJames Feist                 }
968b9d36b47SEd Tanous                 output.emplace_back(key, *container);
9695f2caaefSJames Feist                 index++;
9705f2caaefSJames Feist             }
97183ff9ab6SJames Feist         }
97283ff9ab6SJames Feist 
973b943aaefSJames Feist         if (setpointOffset)
974b943aaefSJames Feist         {
975b943aaefSJames Feist             // translate between redfish and dbus names
976b943aaefSJames Feist             if (*setpointOffset == "UpperThresholdNonCritical")
977b943aaefSJames Feist             {
978b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningLow");
979b943aaefSJames Feist             }
980b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdNonCritical")
981b943aaefSJames Feist             {
982b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningHigh");
983b943aaefSJames Feist             }
984b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdCritical")
985b943aaefSJames Feist             {
986b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalLow");
987b943aaefSJames Feist             }
988b943aaefSJames Feist             else if (*setpointOffset == "UpperThresholdCritical")
989b943aaefSJames Feist             {
990b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalHigh");
991b943aaefSJames Feist             }
992b943aaefSJames Feist             else
993b943aaefSJames Feist             {
994b943aaefSJames Feist                 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
995b943aaefSJames Feist                                  << *setpointOffset;
996ace85d60SEd Tanous                 messages::propertyValueNotInList(response->res, it.key(),
997ace85d60SEd Tanous                                                  "SetPointOffset");
998b943aaefSJames Feist                 return CreatePIDRet::fail;
999b943aaefSJames Feist             }
1000b943aaefSJames Feist         }
1001b943aaefSJames Feist 
100283ff9ab6SJames Feist         // doubles
10035f2caaefSJames Feist         for (const auto& pairs : doubles)
100483ff9ab6SJames Feist         {
10055f2caaefSJames Feist             if (!pairs.second)
100683ff9ab6SJames Feist             {
10075f2caaefSJames Feist                 continue;
100883ff9ab6SJames Feist             }
10095f2caaefSJames Feist             BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
1010b9d36b47SEd Tanous             output.emplace_back(pairs.first, *pairs.second);
10115f2caaefSJames Feist         }
101283ff9ab6SJames Feist     }
101383ff9ab6SJames Feist 
101483ff9ab6SJames Feist     else if (type == "FanZones")
101583ff9ab6SJames Feist     {
1016b9d36b47SEd Tanous         output.emplace_back("Type", "Pid.Zone");
101783ff9ab6SJames Feist 
10185f2caaefSJames Feist         std::optional<nlohmann::json> chassisContainer;
10195f2caaefSJames Feist         std::optional<double> failSafePercent;
1020d3ec07f8SJames Feist         std::optional<double> minThermalOutput;
1021b6baeaa4SJames Feist         if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
10225f2caaefSJames Feist                                           chassisContainer, "FailSafePercent",
1023d3ec07f8SJames Feist                                           failSafePercent, "MinThermalOutput",
1024d3ec07f8SJames Feist                                           minThermalOutput))
102583ff9ab6SJames Feist         {
102671f52d96SEd Tanous             BMCWEB_LOG_ERROR
102771f52d96SEd Tanous                 << "Illegal Property "
102871f52d96SEd Tanous                 << it.value().dump(2, ' ', true,
102971f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace);
103083ff9ab6SJames Feist             return CreatePIDRet::fail;
103183ff9ab6SJames Feist         }
10325f2caaefSJames Feist 
10335f2caaefSJames Feist         if (chassisContainer)
103483ff9ab6SJames Feist         {
10355f2caaefSJames Feist 
10365f2caaefSJames Feist             std::string chassisId;
10375f2caaefSJames Feist             if (!redfish::json_util::readJson(*chassisContainer, response->res,
10385f2caaefSJames Feist                                               "@odata.id", chassisId))
10395f2caaefSJames Feist             {
104071f52d96SEd Tanous                 BMCWEB_LOG_ERROR
104171f52d96SEd Tanous                     << "Illegal Property "
104271f52d96SEd Tanous                     << chassisContainer->dump(
104371f52d96SEd Tanous                            2, ' ', true,
104471f52d96SEd Tanous                            nlohmann::json::error_handler_t::replace);
104583ff9ab6SJames Feist                 return CreatePIDRet::fail;
104683ff9ab6SJames Feist             }
104783ff9ab6SJames Feist 
1048717794d5SAppaRao Puli             // /redfish/v1/chassis/chassis_name/
10495f2caaefSJames Feist             if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
105083ff9ab6SJames Feist             {
10515f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
1052ace85d60SEd Tanous                 messages::invalidObject(
1053ace85d60SEd Tanous                     response->res, crow::utility::urlFromPieces(
1054ace85d60SEd Tanous                                        "redfish", "v1", "Chassis", chassisId));
105583ff9ab6SJames Feist                 return CreatePIDRet::fail;
105683ff9ab6SJames Feist             }
105783ff9ab6SJames Feist         }
1058d3ec07f8SJames Feist         if (minThermalOutput)
105983ff9ab6SJames Feist         {
1060b9d36b47SEd Tanous             output.emplace_back("MinThermalOutput", *minThermalOutput);
10615f2caaefSJames Feist         }
10625f2caaefSJames Feist         if (failSafePercent)
106383ff9ab6SJames Feist         {
1064b9d36b47SEd Tanous             output.emplace_back("FailSafePercent", *failSafePercent);
10655f2caaefSJames Feist         }
10665f2caaefSJames Feist     }
10675f2caaefSJames Feist     else if (type == "StepwiseControllers")
10685f2caaefSJames Feist     {
1069b9d36b47SEd Tanous         output.emplace_back("Type", "Stepwise");
10705f2caaefSJames Feist 
10715f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
10725f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> steps;
10735f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
10745f2caaefSJames Feist         std::optional<double> positiveHysteresis;
10755f2caaefSJames Feist         std::optional<double> negativeHysteresis;
1076c33a90ecSJames Feist         std::optional<std::string> direction; // upper clipping curve vs lower
10775f2caaefSJames Feist         if (!redfish::json_util::readJson(
1078b6baeaa4SJames Feist                 it.value(), response->res, "Zones", zones, "Steps", steps,
1079b6baeaa4SJames Feist                 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
1080c33a90ecSJames Feist                 "NegativeHysteresis", negativeHysteresis, "Direction",
1081c33a90ecSJames Feist                 direction))
10825f2caaefSJames Feist         {
108371f52d96SEd Tanous             BMCWEB_LOG_ERROR
108471f52d96SEd Tanous                 << "Illegal Property "
108571f52d96SEd Tanous                 << it.value().dump(2, ' ', true,
108671f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace);
108783ff9ab6SJames Feist             return CreatePIDRet::fail;
108883ff9ab6SJames Feist         }
10895f2caaefSJames Feist 
10905f2caaefSJames Feist         if (zones)
109183ff9ab6SJames Feist         {
1092b6baeaa4SJames Feist             std::vector<std::string> zonesStrs;
1093b6baeaa4SJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStrs))
10945f2caaefSJames Feist             {
1095a0744d38SGunnar Mills                 BMCWEB_LOG_ERROR << "Illegal Zones";
109683ff9ab6SJames Feist                 return CreatePIDRet::fail;
109783ff9ab6SJames Feist             }
1098b6baeaa4SJames Feist             if (chassis.empty() &&
1099e662eae8SEd Tanous                 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
1100b6baeaa4SJames Feist             {
1101b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
1102ace85d60SEd Tanous                 messages::invalidObject(
1103ace85d60SEd Tanous                     response->res, crow::utility::urlFromPieces(
1104ace85d60SEd Tanous                                        "redfish", "v1", "Chassis", chassis));
1105b6baeaa4SJames Feist                 return CreatePIDRet::fail;
1106b6baeaa4SJames Feist             }
1107b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStrs));
11085f2caaefSJames Feist         }
11095f2caaefSJames Feist         if (steps)
11105f2caaefSJames Feist         {
11115f2caaefSJames Feist             std::vector<double> readings;
11125f2caaefSJames Feist             std::vector<double> outputs;
11135f2caaefSJames Feist             for (auto& step : *steps)
11145f2caaefSJames Feist             {
1115543f4400SEd Tanous                 double target = 0.0;
1116543f4400SEd Tanous                 double out = 0.0;
11175f2caaefSJames Feist 
11185f2caaefSJames Feist                 if (!redfish::json_util::readJson(step, response->res, "Target",
111923a21a1cSEd Tanous                                                   target, "Output", out))
11205f2caaefSJames Feist                 {
112171f52d96SEd Tanous                     BMCWEB_LOG_ERROR
112271f52d96SEd Tanous                         << "Illegal Property "
112371f52d96SEd Tanous                         << it.value().dump(
112471f52d96SEd Tanous                                2, ' ', true,
112571f52d96SEd Tanous                                nlohmann::json::error_handler_t::replace);
11265f2caaefSJames Feist                     return CreatePIDRet::fail;
11275f2caaefSJames Feist                 }
11285f2caaefSJames Feist                 readings.emplace_back(target);
112923a21a1cSEd Tanous                 outputs.emplace_back(out);
11305f2caaefSJames Feist             }
1131b9d36b47SEd Tanous             output.emplace_back("Reading", std::move(readings));
1132b9d36b47SEd Tanous             output.emplace_back("Output", std::move(outputs));
11335f2caaefSJames Feist         }
11345f2caaefSJames Feist         if (inputs)
11355f2caaefSJames Feist         {
11365f2caaefSJames Feist             for (std::string& value : *inputs)
11375f2caaefSJames Feist             {
1138a170f275SEd Tanous 
1139a170f275SEd Tanous                 std::replace(value.begin(), value.end(), '_', ' ');
11405f2caaefSJames Feist             }
1141b9d36b47SEd Tanous             output.emplace_back("Inputs", std::move(*inputs));
11425f2caaefSJames Feist         }
11435f2caaefSJames Feist         if (negativeHysteresis)
11445f2caaefSJames Feist         {
1145b9d36b47SEd Tanous             output.emplace_back("NegativeHysteresis", *negativeHysteresis);
11465f2caaefSJames Feist         }
11475f2caaefSJames Feist         if (positiveHysteresis)
11485f2caaefSJames Feist         {
1149b9d36b47SEd Tanous             output.emplace_back("PositiveHysteresis", *positiveHysteresis);
115083ff9ab6SJames Feist         }
1151c33a90ecSJames Feist         if (direction)
1152c33a90ecSJames Feist         {
1153c33a90ecSJames Feist             constexpr const std::array<const char*, 2> allowedDirections = {
1154c33a90ecSJames Feist                 "Ceiling", "Floor"};
1155c33a90ecSJames Feist             if (std::find(allowedDirections.begin(), allowedDirections.end(),
1156c33a90ecSJames Feist                           *direction) == allowedDirections.end())
1157c33a90ecSJames Feist             {
1158c33a90ecSJames Feist                 messages::propertyValueTypeError(response->res, "Direction",
1159c33a90ecSJames Feist                                                  *direction);
1160c33a90ecSJames Feist                 return CreatePIDRet::fail;
1161c33a90ecSJames Feist             }
1162b9d36b47SEd Tanous             output.emplace_back("Class", *direction);
1163c33a90ecSJames Feist         }
116483ff9ab6SJames Feist     }
116583ff9ab6SJames Feist     else
116683ff9ab6SJames Feist     {
1167a0744d38SGunnar Mills         BMCWEB_LOG_ERROR << "Illegal Type " << type;
116835a62c7cSJason M. Bills         messages::propertyUnknown(response->res, type);
116983ff9ab6SJames Feist         return CreatePIDRet::fail;
117083ff9ab6SJames Feist     }
117183ff9ab6SJames Feist     return CreatePIDRet::patch;
117283ff9ab6SJames Feist }
117373df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
117473df0db0SJames Feist {
117583ff9ab6SJames Feist 
11764e23a444SEd Tanous     explicit GetPIDValues(
11774e23a444SEd Tanous         const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
117823a21a1cSEd Tanous         asyncResp(asyncRespIn)
117973df0db0SJames Feist 
11801214b7e7SGunnar Mills     {}
11819c310685SBorawski.Lukasz 
118273df0db0SJames Feist     void run()
11835b4aa86bSJames Feist     {
118473df0db0SJames Feist         std::shared_ptr<GetPIDValues> self = shared_from_this();
118573df0db0SJames Feist 
118673df0db0SJames Feist         // get all configurations
11875b4aa86bSJames Feist         crow::connections::systemBus->async_method_call(
1188b9d36b47SEd Tanous             [self](
1189b9d36b47SEd Tanous                 const boost::system::error_code ec,
1190b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
11915b4aa86bSJames Feist             if (ec)
11925b4aa86bSJames Feist             {
11935b4aa86bSJames Feist                 BMCWEB_LOG_ERROR << ec;
119473df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
119573df0db0SJames Feist                 return;
119673df0db0SJames Feist             }
119723a21a1cSEd Tanous             self->subtree = subtreeLocal;
119873df0db0SJames Feist             },
119973df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
120073df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
120173df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
120273df0db0SJames Feist             std::array<const char*, 4>{
120373df0db0SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
120473df0db0SJames Feist                 objectManagerIface, stepwiseConfigurationIface});
120573df0db0SJames Feist 
120673df0db0SJames Feist         // at the same time get the selected profile
120773df0db0SJames Feist         crow::connections::systemBus->async_method_call(
1208b9d36b47SEd Tanous             [self](
1209b9d36b47SEd Tanous                 const boost::system::error_code ec,
1210b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
121123a21a1cSEd Tanous             if (ec || subtreeLocal.empty())
121273df0db0SJames Feist             {
121373df0db0SJames Feist                 return;
121473df0db0SJames Feist             }
121523a21a1cSEd Tanous             if (subtreeLocal[0].second.size() != 1)
121673df0db0SJames Feist             {
121773df0db0SJames Feist                 // invalid mapper response, should never happen
121873df0db0SJames Feist                 BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
121973df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
12205b4aa86bSJames Feist                 return;
12215b4aa86bSJames Feist             }
12225b4aa86bSJames Feist 
122323a21a1cSEd Tanous             const std::string& path = subtreeLocal[0].first;
122423a21a1cSEd Tanous             const std::string& owner = subtreeLocal[0].second[0].first;
1225*fac6e53bSKrzysztof Grobelny 
1226*fac6e53bSKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
1227*fac6e53bSKrzysztof Grobelny                 *crow::connections::systemBus, owner, path, thermalModeIface,
1228168e20c1SEd Tanous                 [path, owner,
1229168e20c1SEd Tanous                  self](const boost::system::error_code ec2,
1230b9d36b47SEd Tanous                        const dbus::utility::DBusPropertiesMap& resp) {
123123a21a1cSEd Tanous                 if (ec2)
123273df0db0SJames Feist                 {
12330fda0f12SGeorge Liu                     BMCWEB_LOG_ERROR
1234002d39b4SEd Tanous                         << "GetPIDValues: Can't get thermalModeIface " << path;
123573df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
123673df0db0SJames Feist                     return;
123773df0db0SJames Feist                 }
1238*fac6e53bSKrzysztof Grobelny 
1239271584abSEd Tanous                 const std::string* current = nullptr;
1240271584abSEd Tanous                 const std::vector<std::string>* supported = nullptr;
1241*fac6e53bSKrzysztof Grobelny 
1242*fac6e53bSKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
1243*fac6e53bSKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), resp, "Current", current,
1244*fac6e53bSKrzysztof Grobelny                     "Supported", supported);
1245*fac6e53bSKrzysztof Grobelny 
1246*fac6e53bSKrzysztof Grobelny                 if (!success)
124773df0db0SJames Feist                 {
1248002d39b4SEd Tanous                     messages::internalError(self->asyncResp->res);
124973df0db0SJames Feist                     return;
125073df0db0SJames Feist                 }
1251*fac6e53bSKrzysztof Grobelny 
125273df0db0SJames Feist                 if (current == nullptr || supported == nullptr)
125373df0db0SJames Feist                 {
12540fda0f12SGeorge Liu                     BMCWEB_LOG_ERROR
1255002d39b4SEd Tanous                         << "GetPIDValues: thermal mode iface invalid " << path;
125673df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
125773df0db0SJames Feist                     return;
125873df0db0SJames Feist                 }
125973df0db0SJames Feist                 self->currentProfile = *current;
126073df0db0SJames Feist                 self->supportedProfiles = *supported;
1261*fac6e53bSKrzysztof Grobelny                 });
126273df0db0SJames Feist             },
126373df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
126473df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
126573df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
126673df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
126773df0db0SJames Feist     }
126873df0db0SJames Feist 
126973df0db0SJames Feist     ~GetPIDValues()
127073df0db0SJames Feist     {
127173df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
127273df0db0SJames Feist         {
127373df0db0SJames Feist             return;
127473df0db0SJames Feist         }
12755b4aa86bSJames Feist         // create map of <connection, path to objMgr>>
127673df0db0SJames Feist         boost::container::flat_map<std::string, std::string> objectMgrPaths;
12776bce33bcSJames Feist         boost::container::flat_set<std::string> calledConnections;
12785b4aa86bSJames Feist         for (const auto& pathGroup : subtree)
12795b4aa86bSJames Feist         {
12805b4aa86bSJames Feist             for (const auto& connectionGroup : pathGroup.second)
12815b4aa86bSJames Feist             {
12826bce33bcSJames Feist                 auto findConnection =
12836bce33bcSJames Feist                     calledConnections.find(connectionGroup.first);
12846bce33bcSJames Feist                 if (findConnection != calledConnections.end())
12856bce33bcSJames Feist                 {
12866bce33bcSJames Feist                     break;
12876bce33bcSJames Feist                 }
128873df0db0SJames Feist                 for (const std::string& interface : connectionGroup.second)
12895b4aa86bSJames Feist                 {
12905b4aa86bSJames Feist                     if (interface == objectManagerIface)
12915b4aa86bSJames Feist                     {
129273df0db0SJames Feist                         objectMgrPaths[connectionGroup.first] = pathGroup.first;
12935b4aa86bSJames Feist                     }
12945b4aa86bSJames Feist                     // this list is alphabetical, so we
12955b4aa86bSJames Feist                     // should have found the objMgr by now
12965b4aa86bSJames Feist                     if (interface == pidConfigurationIface ||
1297b7a08d04SJames Feist                         interface == pidZoneConfigurationIface ||
1298b7a08d04SJames Feist                         interface == stepwiseConfigurationIface)
12995b4aa86bSJames Feist                     {
13005b4aa86bSJames Feist                         auto findObjMgr =
13015b4aa86bSJames Feist                             objectMgrPaths.find(connectionGroup.first);
13025b4aa86bSJames Feist                         if (findObjMgr == objectMgrPaths.end())
13035b4aa86bSJames Feist                         {
13045b4aa86bSJames Feist                             BMCWEB_LOG_DEBUG << connectionGroup.first
13055b4aa86bSJames Feist                                              << "Has no Object Manager";
13065b4aa86bSJames Feist                             continue;
13075b4aa86bSJames Feist                         }
13086bce33bcSJames Feist 
13096bce33bcSJames Feist                         calledConnections.insert(connectionGroup.first);
13106bce33bcSJames Feist 
131173df0db0SJames Feist                         asyncPopulatePid(findObjMgr->first, findObjMgr->second,
131273df0db0SJames Feist                                          currentProfile, supportedProfiles,
131373df0db0SJames Feist                                          asyncResp);
13145b4aa86bSJames Feist                         break;
13155b4aa86bSJames Feist                     }
13165b4aa86bSJames Feist                 }
13175b4aa86bSJames Feist             }
13185b4aa86bSJames Feist         }
131973df0db0SJames Feist     }
132073df0db0SJames Feist 
1321ecd6a3a2SEd Tanous     GetPIDValues(const GetPIDValues&) = delete;
1322ecd6a3a2SEd Tanous     GetPIDValues(GetPIDValues&&) = delete;
1323ecd6a3a2SEd Tanous     GetPIDValues& operator=(const GetPIDValues&) = delete;
1324ecd6a3a2SEd Tanous     GetPIDValues& operator=(GetPIDValues&&) = delete;
1325ecd6a3a2SEd Tanous 
132673df0db0SJames Feist     std::vector<std::string> supportedProfiles;
132773df0db0SJames Feist     std::string currentProfile;
1328b9d36b47SEd Tanous     dbus::utility::MapperGetSubTreeResponse subtree;
13298d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
133073df0db0SJames Feist };
133173df0db0SJames Feist 
133273df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
133373df0db0SJames Feist {
133473df0db0SJames Feist 
13358d1b46d7Szhanghch05     SetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
133673df0db0SJames Feist                  nlohmann::json& data) :
1337271584abSEd Tanous         asyncResp(asyncRespIn)
133873df0db0SJames Feist     {
133973df0db0SJames Feist 
134073df0db0SJames Feist         std::optional<nlohmann::json> pidControllers;
134173df0db0SJames Feist         std::optional<nlohmann::json> fanControllers;
134273df0db0SJames Feist         std::optional<nlohmann::json> fanZones;
134373df0db0SJames Feist         std::optional<nlohmann::json> stepwiseControllers;
134473df0db0SJames Feist 
134573df0db0SJames Feist         if (!redfish::json_util::readJson(
134673df0db0SJames Feist                 data, asyncResp->res, "PidControllers", pidControllers,
134773df0db0SJames Feist                 "FanControllers", fanControllers, "FanZones", fanZones,
134873df0db0SJames Feist                 "StepwiseControllers", stepwiseControllers, "Profile", profile))
134973df0db0SJames Feist         {
135071f52d96SEd Tanous             BMCWEB_LOG_ERROR
135171f52d96SEd Tanous                 << "Illegal Property "
135271f52d96SEd Tanous                 << data.dump(2, ' ', true,
135371f52d96SEd Tanous                              nlohmann::json::error_handler_t::replace);
135473df0db0SJames Feist             return;
135573df0db0SJames Feist         }
135673df0db0SJames Feist         configuration.emplace_back("PidControllers", std::move(pidControllers));
135773df0db0SJames Feist         configuration.emplace_back("FanControllers", std::move(fanControllers));
135873df0db0SJames Feist         configuration.emplace_back("FanZones", std::move(fanZones));
135973df0db0SJames Feist         configuration.emplace_back("StepwiseControllers",
136073df0db0SJames Feist                                    std::move(stepwiseControllers));
136173df0db0SJames Feist     }
1362ecd6a3a2SEd Tanous 
1363ecd6a3a2SEd Tanous     SetPIDValues(const SetPIDValues&) = delete;
1364ecd6a3a2SEd Tanous     SetPIDValues(SetPIDValues&&) = delete;
1365ecd6a3a2SEd Tanous     SetPIDValues& operator=(const SetPIDValues&) = delete;
1366ecd6a3a2SEd Tanous     SetPIDValues& operator=(SetPIDValues&&) = delete;
1367ecd6a3a2SEd Tanous 
136873df0db0SJames Feist     void run()
136973df0db0SJames Feist     {
137073df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
137173df0db0SJames Feist         {
137273df0db0SJames Feist             return;
137373df0db0SJames Feist         }
137473df0db0SJames Feist 
137573df0db0SJames Feist         std::shared_ptr<SetPIDValues> self = shared_from_this();
137673df0db0SJames Feist 
137773df0db0SJames Feist         // todo(james): might make sense to do a mapper call here if this
137873df0db0SJames Feist         // interface gets more traction
137973df0db0SJames Feist         crow::connections::systemBus->async_method_call(
138073df0db0SJames Feist             [self](const boost::system::error_code ec,
1381914e2d5dSEd Tanous                    const dbus::utility::ManagedObjectType& mObj) {
138273df0db0SJames Feist             if (ec)
138373df0db0SJames Feist             {
138473df0db0SJames Feist                 BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
138573df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
138673df0db0SJames Feist                 return;
138773df0db0SJames Feist             }
1388e69d9de2SJames Feist             const std::array<const char*, 3> configurations = {
1389e69d9de2SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
1390e69d9de2SJames Feist                 stepwiseConfigurationIface};
1391e69d9de2SJames Feist 
139214b0b8d5SJames Feist             for (const auto& [path, object] : mObj)
1393e69d9de2SJames Feist             {
139414b0b8d5SJames Feist                 for (const auto& [interface, _] : object)
1395e69d9de2SJames Feist                 {
1396002d39b4SEd Tanous                     if (std::find(configurations.begin(), configurations.end(),
1397e69d9de2SJames Feist                                   interface) != configurations.end())
1398e69d9de2SJames Feist                     {
139914b0b8d5SJames Feist                         self->objectCount++;
1400e69d9de2SJames Feist                         break;
1401e69d9de2SJames Feist                     }
1402e69d9de2SJames Feist                 }
1403e69d9de2SJames Feist             }
1404914e2d5dSEd Tanous             self->managedObj = mObj;
140573df0db0SJames Feist             },
140673df0db0SJames Feist             "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
140773df0db0SJames Feist             "GetManagedObjects");
140873df0db0SJames Feist 
140973df0db0SJames Feist         // at the same time get the profile information
141073df0db0SJames Feist         crow::connections::systemBus->async_method_call(
141173df0db0SJames Feist             [self](const boost::system::error_code ec,
1412b9d36b47SEd Tanous                    const dbus::utility::MapperGetSubTreeResponse& subtree) {
141373df0db0SJames Feist             if (ec || subtree.empty())
141473df0db0SJames Feist             {
141573df0db0SJames Feist                 return;
141673df0db0SJames Feist             }
141773df0db0SJames Feist             if (subtree[0].second.empty())
141873df0db0SJames Feist             {
141973df0db0SJames Feist                 // invalid mapper response, should never happen
142073df0db0SJames Feist                 BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
142173df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
142273df0db0SJames Feist                 return;
142373df0db0SJames Feist             }
142473df0db0SJames Feist 
142573df0db0SJames Feist             const std::string& path = subtree[0].first;
142673df0db0SJames Feist             const std::string& owner = subtree[0].second[0].first;
1427*fac6e53bSKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
1428*fac6e53bSKrzysztof Grobelny                 *crow::connections::systemBus, owner, path, thermalModeIface,
1429002d39b4SEd Tanous                 [self, path, owner](const boost::system::error_code ec2,
1430b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap& r) {
1431cb13a392SEd Tanous                 if (ec2)
143273df0db0SJames Feist                 {
14330fda0f12SGeorge Liu                     BMCWEB_LOG_ERROR
1434002d39b4SEd Tanous                         << "SetPIDValues: Can't get thermalModeIface " << path;
143573df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
143673df0db0SJames Feist                     return;
143773df0db0SJames Feist                 }
1438271584abSEd Tanous                 const std::string* current = nullptr;
1439271584abSEd Tanous                 const std::vector<std::string>* supported = nullptr;
1440*fac6e53bSKrzysztof Grobelny 
1441*fac6e53bSKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
1442*fac6e53bSKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), r, "Current", current,
1443*fac6e53bSKrzysztof Grobelny                     "Supported", supported);
1444*fac6e53bSKrzysztof Grobelny 
1445*fac6e53bSKrzysztof Grobelny                 if (!success)
144673df0db0SJames Feist                 {
1447002d39b4SEd Tanous                     messages::internalError(self->asyncResp->res);
144873df0db0SJames Feist                     return;
144973df0db0SJames Feist                 }
1450*fac6e53bSKrzysztof Grobelny 
145173df0db0SJames Feist                 if (current == nullptr || supported == nullptr)
145273df0db0SJames Feist                 {
14530fda0f12SGeorge Liu                     BMCWEB_LOG_ERROR
1454002d39b4SEd Tanous                         << "SetPIDValues: thermal mode iface invalid " << path;
145573df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
145673df0db0SJames Feist                     return;
145773df0db0SJames Feist                 }
145873df0db0SJames Feist                 self->currentProfile = *current;
145973df0db0SJames Feist                 self->supportedProfiles = *supported;
146073df0db0SJames Feist                 self->profileConnection = owner;
146173df0db0SJames Feist                 self->profilePath = path;
1462*fac6e53bSKrzysztof Grobelny                 });
14635b4aa86bSJames Feist             },
14645b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper",
14655b4aa86bSJames Feist             "/xyz/openbmc_project/object_mapper",
14665b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
146773df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
146873df0db0SJames Feist     }
146924b2fe81SEd Tanous     void pidSetDone()
147073df0db0SJames Feist     {
147173df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
147273df0db0SJames Feist         {
147373df0db0SJames Feist             return;
14745b4aa86bSJames Feist         }
14758d1b46d7Szhanghch05         std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
147673df0db0SJames Feist         if (profile)
147773df0db0SJames Feist         {
147873df0db0SJames Feist             if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
147973df0db0SJames Feist                           *profile) == supportedProfiles.end())
148073df0db0SJames Feist             {
148173df0db0SJames Feist                 messages::actionParameterUnknown(response->res, "Profile",
148273df0db0SJames Feist                                                  *profile);
148373df0db0SJames Feist                 return;
148473df0db0SJames Feist             }
148573df0db0SJames Feist             currentProfile = *profile;
148673df0db0SJames Feist             crow::connections::systemBus->async_method_call(
148773df0db0SJames Feist                 [response](const boost::system::error_code ec) {
148873df0db0SJames Feist                 if (ec)
148973df0db0SJames Feist                 {
149073df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Error patching profile" << ec;
149173df0db0SJames Feist                     messages::internalError(response->res);
149273df0db0SJames Feist                 }
149373df0db0SJames Feist                 },
149473df0db0SJames Feist                 profileConnection, profilePath,
149573df0db0SJames Feist                 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
1496168e20c1SEd Tanous                 "Current", dbus::utility::DbusVariantType(*profile));
149773df0db0SJames Feist         }
149873df0db0SJames Feist 
149973df0db0SJames Feist         for (auto& containerPair : configuration)
150073df0db0SJames Feist         {
150173df0db0SJames Feist             auto& container = containerPair.second;
150273df0db0SJames Feist             if (!container)
150373df0db0SJames Feist             {
150473df0db0SJames Feist                 continue;
150573df0db0SJames Feist             }
15066ee7f774SJames Feist             BMCWEB_LOG_DEBUG << *container;
15076ee7f774SJames Feist 
150802cad96eSEd Tanous             const std::string& type = containerPair.first;
150973df0db0SJames Feist 
151073df0db0SJames Feist             for (nlohmann::json::iterator it = container->begin();
151117a897dfSManojkiran Eda                  it != container->end(); ++it)
151273df0db0SJames Feist             {
151373df0db0SJames Feist                 const auto& name = it.key();
15146ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "looking for " << name;
15156ee7f774SJames Feist 
151673df0db0SJames Feist                 auto pathItr =
151773df0db0SJames Feist                     std::find_if(managedObj.begin(), managedObj.end(),
151873df0db0SJames Feist                                  [&name](const auto& obj) {
1519002d39b4SEd Tanous                     return boost::algorithm::ends_with(obj.first.str,
1520002d39b4SEd Tanous                                                        "/" + name);
152173df0db0SJames Feist                     });
1522b9d36b47SEd Tanous                 dbus::utility::DBusPropertiesMap output;
152373df0db0SJames Feist 
152473df0db0SJames Feist                 output.reserve(16); // The pid interface length
152573df0db0SJames Feist 
152673df0db0SJames Feist                 // determines if we're patching entity-manager or
152773df0db0SJames Feist                 // creating a new object
152873df0db0SJames Feist                 bool createNewObject = (pathItr == managedObj.end());
15296ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
15306ee7f774SJames Feist 
153173df0db0SJames Feist                 std::string iface;
1532711ac7a9SEd Tanous                 /*
153373df0db0SJames Feist                 if (type == "PidControllers" || type == "FanControllers")
153473df0db0SJames Feist                 {
153573df0db0SJames Feist                     iface = pidConfigurationIface;
153673df0db0SJames Feist                     if (!createNewObject &&
153773df0db0SJames Feist                         pathItr->second.find(pidConfigurationIface) ==
153873df0db0SJames Feist                             pathItr->second.end())
153973df0db0SJames Feist                     {
154073df0db0SJames Feist                         createNewObject = true;
154173df0db0SJames Feist                     }
154273df0db0SJames Feist                 }
154373df0db0SJames Feist                 else if (type == "FanZones")
154473df0db0SJames Feist                 {
154573df0db0SJames Feist                     iface = pidZoneConfigurationIface;
154673df0db0SJames Feist                     if (!createNewObject &&
154773df0db0SJames Feist                         pathItr->second.find(pidZoneConfigurationIface) ==
154873df0db0SJames Feist                             pathItr->second.end())
154973df0db0SJames Feist                     {
155073df0db0SJames Feist 
155173df0db0SJames Feist                         createNewObject = true;
155273df0db0SJames Feist                     }
155373df0db0SJames Feist                 }
155473df0db0SJames Feist                 else if (type == "StepwiseControllers")
155573df0db0SJames Feist                 {
155673df0db0SJames Feist                     iface = stepwiseConfigurationIface;
155773df0db0SJames Feist                     if (!createNewObject &&
155873df0db0SJames Feist                         pathItr->second.find(stepwiseConfigurationIface) ==
155973df0db0SJames Feist                             pathItr->second.end())
156073df0db0SJames Feist                     {
156173df0db0SJames Feist                         createNewObject = true;
156273df0db0SJames Feist                     }
1563711ac7a9SEd Tanous                 }*/
15646ee7f774SJames Feist 
15656ee7f774SJames Feist                 if (createNewObject && it.value() == nullptr)
15666ee7f774SJames Feist                 {
15674e0453b1SGunnar Mills                     // can't delete a non-existent object
15681668ce6dSEd Tanous                     messages::propertyValueNotInList(response->res,
15691668ce6dSEd Tanous                                                      it.value().dump(), name);
15706ee7f774SJames Feist                     continue;
15716ee7f774SJames Feist                 }
15726ee7f774SJames Feist 
15736ee7f774SJames Feist                 std::string path;
15746ee7f774SJames Feist                 if (pathItr != managedObj.end())
15756ee7f774SJames Feist                 {
15766ee7f774SJames Feist                     path = pathItr->first.str;
15776ee7f774SJames Feist                 }
15786ee7f774SJames Feist 
157973df0db0SJames Feist                 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
1580e69d9de2SJames Feist 
1581e69d9de2SJames Feist                 // arbitrary limit to avoid attacks
1582e69d9de2SJames Feist                 constexpr const size_t controllerLimit = 500;
158314b0b8d5SJames Feist                 if (createNewObject && objectCount >= controllerLimit)
1584e69d9de2SJames Feist                 {
1585e69d9de2SJames Feist                     messages::resourceExhaustion(response->res, type);
1586e69d9de2SJames Feist                     continue;
1587e69d9de2SJames Feist                 }
1588a170f275SEd Tanous                 std::string escaped = name;
1589a170f275SEd Tanous                 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1590a170f275SEd Tanous                 output.emplace_back("Name", escaped);
159173df0db0SJames Feist 
159273df0db0SJames Feist                 std::string chassis;
159373df0db0SJames Feist                 CreatePIDRet ret = createPidInterface(
15946ee7f774SJames Feist                     response, type, it, path, managedObj, createNewObject,
15956ee7f774SJames Feist                     output, chassis, currentProfile);
159673df0db0SJames Feist                 if (ret == CreatePIDRet::fail)
159773df0db0SJames Feist                 {
159873df0db0SJames Feist                     return;
159973df0db0SJames Feist                 }
16003174e4dfSEd Tanous                 if (ret == CreatePIDRet::del)
160173df0db0SJames Feist                 {
160273df0db0SJames Feist                     continue;
160373df0db0SJames Feist                 }
160473df0db0SJames Feist 
160573df0db0SJames Feist                 if (!createNewObject)
160673df0db0SJames Feist                 {
160773df0db0SJames Feist                     for (const auto& property : output)
160873df0db0SJames Feist                     {
160973df0db0SJames Feist                         crow::connections::systemBus->async_method_call(
161073df0db0SJames Feist                             [response,
161173df0db0SJames Feist                              propertyName{std::string(property.first)}](
161273df0db0SJames Feist                                 const boost::system::error_code ec) {
161373df0db0SJames Feist                             if (ec)
161473df0db0SJames Feist                             {
161573df0db0SJames Feist                                 BMCWEB_LOG_ERROR << "Error patching "
1616002d39b4SEd Tanous                                                  << propertyName << ": " << ec;
161773df0db0SJames Feist                                 messages::internalError(response->res);
161873df0db0SJames Feist                                 return;
161973df0db0SJames Feist                             }
162073df0db0SJames Feist                             messages::success(response->res);
162173df0db0SJames Feist                             },
16226ee7f774SJames Feist                             "xyz.openbmc_project.EntityManager", path,
162373df0db0SJames Feist                             "org.freedesktop.DBus.Properties", "Set", iface,
162473df0db0SJames Feist                             property.first, property.second);
162573df0db0SJames Feist                     }
162673df0db0SJames Feist                 }
162773df0db0SJames Feist                 else
162873df0db0SJames Feist                 {
162973df0db0SJames Feist                     if (chassis.empty())
163073df0db0SJames Feist                     {
163173df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to get chassis from config";
1632ace85d60SEd Tanous                         messages::internalError(response->res);
163373df0db0SJames Feist                         return;
163473df0db0SJames Feist                     }
163573df0db0SJames Feist 
163673df0db0SJames Feist                     bool foundChassis = false;
163773df0db0SJames Feist                     for (const auto& obj : managedObj)
163873df0db0SJames Feist                     {
163973df0db0SJames Feist                         if (boost::algorithm::ends_with(obj.first.str, chassis))
164073df0db0SJames Feist                         {
164173df0db0SJames Feist                             chassis = obj.first.str;
164273df0db0SJames Feist                             foundChassis = true;
164373df0db0SJames Feist                             break;
164473df0db0SJames Feist                         }
164573df0db0SJames Feist                     }
164673df0db0SJames Feist                     if (!foundChassis)
164773df0db0SJames Feist                     {
164873df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
164973df0db0SJames Feist                         messages::resourceMissingAtURI(
1650ace85d60SEd Tanous                             response->res,
1651ace85d60SEd Tanous                             crow::utility::urlFromPieces("redfish", "v1",
1652ace85d60SEd Tanous                                                          "Chassis", chassis));
165373df0db0SJames Feist                         return;
165473df0db0SJames Feist                     }
165573df0db0SJames Feist 
165673df0db0SJames Feist                     crow::connections::systemBus->async_method_call(
165773df0db0SJames Feist                         [response](const boost::system::error_code ec) {
165873df0db0SJames Feist                         if (ec)
165973df0db0SJames Feist                         {
166073df0db0SJames Feist                             BMCWEB_LOG_ERROR << "Error Adding Pid Object "
166173df0db0SJames Feist                                              << ec;
166273df0db0SJames Feist                             messages::internalError(response->res);
166373df0db0SJames Feist                             return;
166473df0db0SJames Feist                         }
166573df0db0SJames Feist                         messages::success(response->res);
166673df0db0SJames Feist                         },
166773df0db0SJames Feist                         "xyz.openbmc_project.EntityManager", chassis,
166873df0db0SJames Feist                         "xyz.openbmc_project.AddObject", "AddObject", output);
166973df0db0SJames Feist                 }
167073df0db0SJames Feist             }
167173df0db0SJames Feist         }
167273df0db0SJames Feist     }
167324b2fe81SEd Tanous 
167424b2fe81SEd Tanous     ~SetPIDValues()
167524b2fe81SEd Tanous     {
167624b2fe81SEd Tanous         try
167724b2fe81SEd Tanous         {
167824b2fe81SEd Tanous             pidSetDone();
167924b2fe81SEd Tanous         }
168024b2fe81SEd Tanous         catch (...)
168124b2fe81SEd Tanous         {
168224b2fe81SEd Tanous             BMCWEB_LOG_CRITICAL << "pidSetDone threw exception";
168324b2fe81SEd Tanous         }
168424b2fe81SEd Tanous     }
168524b2fe81SEd Tanous 
16868d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
168773df0db0SJames Feist     std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
168873df0db0SJames Feist         configuration;
168973df0db0SJames Feist     std::optional<std::string> profile;
169073df0db0SJames Feist     dbus::utility::ManagedObjectType managedObj;
169173df0db0SJames Feist     std::vector<std::string> supportedProfiles;
169273df0db0SJames Feist     std::string currentProfile;
169373df0db0SJames Feist     std::string profileConnection;
169473df0db0SJames Feist     std::string profilePath;
169514b0b8d5SJames Feist     size_t objectCount = 0;
169673df0db0SJames Feist };
169773df0db0SJames Feist 
1698071d8fdfSSunnySrivastava1984 /**
1699071d8fdfSSunnySrivastava1984  * @brief Retrieves BMC manager location data over DBus
1700071d8fdfSSunnySrivastava1984  *
1701071d8fdfSSunnySrivastava1984  * @param[in] aResp Shared pointer for completing asynchronous calls
1702071d8fdfSSunnySrivastava1984  * @param[in] connectionName - service name
1703071d8fdfSSunnySrivastava1984  * @param[in] path - object path
1704071d8fdfSSunnySrivastava1984  * @return none
1705071d8fdfSSunnySrivastava1984  */
17068d1b46d7Szhanghch05 inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1707071d8fdfSSunnySrivastava1984                         const std::string& connectionName,
1708071d8fdfSSunnySrivastava1984                         const std::string& path)
1709071d8fdfSSunnySrivastava1984 {
1710071d8fdfSSunnySrivastava1984     BMCWEB_LOG_DEBUG << "Get BMC manager Location data.";
1711071d8fdfSSunnySrivastava1984 
17121e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
17131e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
17141e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
1715071d8fdfSSunnySrivastava1984         [aResp](const boost::system::error_code ec,
17161e1e598dSJonathan Doman                 const std::string& property) {
1717071d8fdfSSunnySrivastava1984         if (ec)
1718071d8fdfSSunnySrivastava1984         {
1719071d8fdfSSunnySrivastava1984             BMCWEB_LOG_DEBUG << "DBUS response error for "
1720071d8fdfSSunnySrivastava1984                                 "Location";
1721071d8fdfSSunnySrivastava1984             messages::internalError(aResp->res);
1722071d8fdfSSunnySrivastava1984             return;
1723071d8fdfSSunnySrivastava1984         }
1724071d8fdfSSunnySrivastava1984 
1725071d8fdfSSunnySrivastava1984         aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
17261e1e598dSJonathan Doman             property;
17271e1e598dSJonathan Doman         });
1728071d8fdfSSunnySrivastava1984 }
17297e860f15SJohn Edward Broadbent // avoid name collision systems.hpp
17307e860f15SJohn Edward Broadbent inline void
17317e860f15SJohn Edward Broadbent     managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
17324bf2b033SGunnar Mills {
17334bf2b033SGunnar Mills     BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time";
17344bf2b033SGunnar Mills 
17351e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint64_t>(
17361e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
17371e1e598dSJonathan Doman         "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
17381e1e598dSJonathan Doman         "LastRebootTime",
17394bf2b033SGunnar Mills         [aResp](const boost::system::error_code ec,
17401e1e598dSJonathan Doman                 const uint64_t lastResetTime) {
17414bf2b033SGunnar Mills         if (ec)
17424bf2b033SGunnar Mills         {
17434bf2b033SGunnar Mills             BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
17444bf2b033SGunnar Mills             return;
17454bf2b033SGunnar Mills         }
17464bf2b033SGunnar Mills 
17474bf2b033SGunnar Mills         // LastRebootTime is epoch time, in milliseconds
17484bf2b033SGunnar Mills         // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
17491e1e598dSJonathan Doman         uint64_t lastResetTimeStamp = lastResetTime / 1000;
17504bf2b033SGunnar Mills 
17514bf2b033SGunnar Mills         // Convert to ISO 8601 standard
17524bf2b033SGunnar Mills         aResp->res.jsonValue["LastResetTime"] =
17531d8782e7SNan Zhou             crow::utility::getDateTimeUint(lastResetTimeStamp);
17541e1e598dSJonathan Doman         });
17554bf2b033SGunnar Mills }
17564bf2b033SGunnar Mills 
17574bfefa74SGunnar Mills /**
17584bfefa74SGunnar Mills  * @brief Set the running firmware image
17594bfefa74SGunnar Mills  *
17604bfefa74SGunnar Mills  * @param[i,o] aResp - Async response object
17614bfefa74SGunnar Mills  * @param[i] runningFirmwareTarget - Image to make the running image
17624bfefa74SGunnar Mills  *
17634bfefa74SGunnar Mills  * @return void
17644bfefa74SGunnar Mills  */
17657e860f15SJohn Edward Broadbent inline void
17667e860f15SJohn Edward Broadbent     setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1767f23b7296SEd Tanous                            const std::string& runningFirmwareTarget)
17684bfefa74SGunnar Mills {
17694bfefa74SGunnar Mills     // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1770f23b7296SEd Tanous     std::string::size_type idPos = runningFirmwareTarget.rfind('/');
17714bfefa74SGunnar Mills     if (idPos == std::string::npos)
17724bfefa74SGunnar Mills     {
17734bfefa74SGunnar Mills         messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
17744bfefa74SGunnar Mills                                          "@odata.id");
17754bfefa74SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't parse firmware ID!";
17764bfefa74SGunnar Mills         return;
17774bfefa74SGunnar Mills     }
17784bfefa74SGunnar Mills     idPos++;
17794bfefa74SGunnar Mills     if (idPos >= runningFirmwareTarget.size())
17804bfefa74SGunnar Mills     {
17814bfefa74SGunnar Mills         messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
17824bfefa74SGunnar Mills                                          "@odata.id");
17834bfefa74SGunnar Mills         BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
17844bfefa74SGunnar Mills         return;
17854bfefa74SGunnar Mills     }
17864bfefa74SGunnar Mills     std::string firmwareId = runningFirmwareTarget.substr(idPos);
17874bfefa74SGunnar Mills 
17884bfefa74SGunnar Mills     // Make sure the image is valid before setting priority
17894bfefa74SGunnar Mills     crow::connections::systemBus->async_method_call(
1790711ac7a9SEd Tanous         [aResp, firmwareId,
1791711ac7a9SEd Tanous          runningFirmwareTarget](const boost::system::error_code ec,
1792711ac7a9SEd Tanous                                 dbus::utility::ManagedObjectType& subtree) {
17934bfefa74SGunnar Mills         if (ec)
17944bfefa74SGunnar Mills         {
17954bfefa74SGunnar Mills             BMCWEB_LOG_DEBUG << "D-Bus response error getting objects.";
17964bfefa74SGunnar Mills             messages::internalError(aResp->res);
17974bfefa74SGunnar Mills             return;
17984bfefa74SGunnar Mills         }
17994bfefa74SGunnar Mills 
180026f6976fSEd Tanous         if (subtree.empty())
18014bfefa74SGunnar Mills         {
18024bfefa74SGunnar Mills             BMCWEB_LOG_DEBUG << "Can't find image!";
18034bfefa74SGunnar Mills             messages::internalError(aResp->res);
18044bfefa74SGunnar Mills             return;
18054bfefa74SGunnar Mills         }
18064bfefa74SGunnar Mills 
18074bfefa74SGunnar Mills         bool foundImage = false;
180802cad96eSEd Tanous         for (const auto& object : subtree)
18094bfefa74SGunnar Mills         {
18104bfefa74SGunnar Mills             const std::string& path =
18114bfefa74SGunnar Mills                 static_cast<const std::string&>(object.first);
1812f23b7296SEd Tanous             std::size_t idPos2 = path.rfind('/');
18134bfefa74SGunnar Mills 
18144bfefa74SGunnar Mills             if (idPos2 == std::string::npos)
18154bfefa74SGunnar Mills             {
18164bfefa74SGunnar Mills                 continue;
18174bfefa74SGunnar Mills             }
18184bfefa74SGunnar Mills 
18194bfefa74SGunnar Mills             idPos2++;
18204bfefa74SGunnar Mills             if (idPos2 >= path.size())
18214bfefa74SGunnar Mills             {
18224bfefa74SGunnar Mills                 continue;
18234bfefa74SGunnar Mills             }
18244bfefa74SGunnar Mills 
18254bfefa74SGunnar Mills             if (path.substr(idPos2) == firmwareId)
18264bfefa74SGunnar Mills             {
18274bfefa74SGunnar Mills                 foundImage = true;
18284bfefa74SGunnar Mills                 break;
18294bfefa74SGunnar Mills             }
18304bfefa74SGunnar Mills         }
18314bfefa74SGunnar Mills 
18324bfefa74SGunnar Mills         if (!foundImage)
18334bfefa74SGunnar Mills         {
1834002d39b4SEd Tanous             messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
1835002d39b4SEd Tanous                                              "@odata.id");
18364bfefa74SGunnar Mills             BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
18374bfefa74SGunnar Mills             return;
18384bfefa74SGunnar Mills         }
18394bfefa74SGunnar Mills 
18408cc8edecSEd Tanous         BMCWEB_LOG_DEBUG << "Setting firmware version " << firmwareId
18418cc8edecSEd Tanous                          << " to priority 0.";
18424bfefa74SGunnar Mills 
18434bfefa74SGunnar Mills         // Only support Immediate
18444bfefa74SGunnar Mills         // An addition could be a Redfish Setting like
18454bfefa74SGunnar Mills         // ActiveSoftwareImageApplyTime and support OnReset
18464bfefa74SGunnar Mills         crow::connections::systemBus->async_method_call(
18478a592810SEd Tanous             [aResp](const boost::system::error_code ec2) {
18488a592810SEd Tanous             if (ec2)
18494bfefa74SGunnar Mills             {
18504bfefa74SGunnar Mills                 BMCWEB_LOG_DEBUG << "D-Bus response error setting.";
18514bfefa74SGunnar Mills                 messages::internalError(aResp->res);
18524bfefa74SGunnar Mills                 return;
18534bfefa74SGunnar Mills             }
18544bfefa74SGunnar Mills             doBMCGracefulRestart(aResp);
18554bfefa74SGunnar Mills             },
18564bfefa74SGunnar Mills 
18574bfefa74SGunnar Mills             "xyz.openbmc_project.Software.BMC.Updater",
18584bfefa74SGunnar Mills             "/xyz/openbmc_project/software/" + firmwareId,
18594bfefa74SGunnar Mills             "org.freedesktop.DBus.Properties", "Set",
18607e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1861168e20c1SEd Tanous             dbus::utility::DbusVariantType(static_cast<uint8_t>(0)));
18624bfefa74SGunnar Mills         },
18634bfefa74SGunnar Mills         "xyz.openbmc_project.Software.BMC.Updater",
18647e860f15SJohn Edward Broadbent         "/xyz/openbmc_project/software", "org.freedesktop.DBus.ObjectManager",
18657e860f15SJohn Edward Broadbent         "GetManagedObjects");
18664bfefa74SGunnar Mills }
18674bfefa74SGunnar Mills 
18687e860f15SJohn Edward Broadbent inline void setDateTime(std::shared_ptr<bmcweb::AsyncResp> aResp,
18697e860f15SJohn Edward Broadbent                         std::string datetime)
1870af5d6058SSantosh Puranik {
1871af5d6058SSantosh Puranik     BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
1872af5d6058SSantosh Puranik 
1873af5d6058SSantosh Puranik     std::stringstream stream(datetime);
1874af5d6058SSantosh Puranik     // Convert from ISO 8601 to boost local_time
1875af5d6058SSantosh Puranik     // (BMC only has time in UTC)
1876af5d6058SSantosh Puranik     boost::posix_time::ptime posixTime;
1877af5d6058SSantosh Puranik     boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
1878af5d6058SSantosh Puranik     // Facet gets deleted with the stringsteam
1879af5d6058SSantosh Puranik     auto ifc = std::make_unique<boost::local_time::local_time_input_facet>(
1880af5d6058SSantosh Puranik         "%Y-%m-%d %H:%M:%S%F %ZP");
1881af5d6058SSantosh Puranik     stream.imbue(std::locale(stream.getloc(), ifc.release()));
1882af5d6058SSantosh Puranik 
18837e860f15SJohn Edward Broadbent     boost::local_time::local_date_time ldt(boost::local_time::not_a_date_time);
1884af5d6058SSantosh Puranik 
1885af5d6058SSantosh Puranik     if (stream >> ldt)
1886af5d6058SSantosh Puranik     {
1887af5d6058SSantosh Puranik         posixTime = ldt.utc_time();
1888af5d6058SSantosh Puranik         boost::posix_time::time_duration dur = posixTime - epoch;
18897e860f15SJohn Edward Broadbent         uint64_t durMicroSecs = static_cast<uint64_t>(dur.total_microseconds());
1890af5d6058SSantosh Puranik         crow::connections::systemBus->async_method_call(
1891af5d6058SSantosh Puranik             [aResp{std::move(aResp)}, datetime{std::move(datetime)}](
1892af5d6058SSantosh Puranik                 const boost::system::error_code ec) {
1893af5d6058SSantosh Puranik             if (ec)
1894af5d6058SSantosh Puranik             {
1895af5d6058SSantosh Puranik                 BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1896af5d6058SSantosh Puranik                                     "DBUS response error "
1897af5d6058SSantosh Puranik                                  << ec;
1898af5d6058SSantosh Puranik                 messages::internalError(aResp->res);
1899af5d6058SSantosh Puranik                 return;
1900af5d6058SSantosh Puranik             }
1901af5d6058SSantosh Puranik             aResp->res.jsonValue["DateTime"] = datetime;
1902af5d6058SSantosh Puranik             },
19037e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Time.Manager", "/xyz/openbmc_project/time/bmc",
1904af5d6058SSantosh Puranik             "org.freedesktop.DBus.Properties", "Set",
1905af5d6058SSantosh Puranik             "xyz.openbmc_project.Time.EpochTime", "Elapsed",
1906168e20c1SEd Tanous             dbus::utility::DbusVariantType(durMicroSecs));
1907af5d6058SSantosh Puranik     }
1908af5d6058SSantosh Puranik     else
1909af5d6058SSantosh Puranik     {
19107e860f15SJohn Edward Broadbent         messages::propertyValueFormatError(aResp->res, datetime, "DateTime");
1911af5d6058SSantosh Puranik         return;
1912af5d6058SSantosh Puranik     }
191383ff9ab6SJames Feist }
19149c310685SBorawski.Lukasz 
19157e860f15SJohn Edward Broadbent inline void requestRoutesManager(App& app)
19167e860f15SJohn Edward Broadbent {
19177e860f15SJohn Edward Broadbent     std::string uuid = persistent_data::getConfig().systemUuid;
19189c310685SBorawski.Lukasz 
19197e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
1920ed398213SEd Tanous         .privileges(redfish::privileges::getManager)
1921002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1922002d39b4SEd Tanous             [&app, uuid](const crow::Request& req,
192345ca1b86SEd Tanous                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
19243ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
192545ca1b86SEd Tanous         {
192645ca1b86SEd Tanous             return;
192745ca1b86SEd Tanous         }
19287e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
1929a51fc2d2SSui Chen         asyncResp->res.jsonValue["@odata.type"] = "#Manager.v1_14_0.Manager";
19307e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["Id"] = "bmc";
19317e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
19327e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["Description"] =
19337e860f15SJohn Edward Broadbent             "Baseboard Management Controller";
19347e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["PowerState"] = "On";
19351476687dSEd Tanous         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
19361476687dSEd Tanous         asyncResp->res.jsonValue["Status"]["Health"] = "OK";
19371476687dSEd Tanous 
19387e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["ManagerType"] = "BMC";
19397e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
19407e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1941002d39b4SEd Tanous         asyncResp->res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
19427e860f15SJohn Edward Broadbent 
19431476687dSEd Tanous         asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
19441476687dSEd Tanous             "/redfish/v1/Managers/bmc/LogServices";
19451476687dSEd Tanous         asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
19461476687dSEd Tanous             "/redfish/v1/Managers/bmc/NetworkProtocol";
19471476687dSEd Tanous         asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
19481476687dSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
19497e860f15SJohn Edward Broadbent 
19507e860f15SJohn Edward Broadbent #ifdef BMCWEB_ENABLE_VM_NBDPROXY
19511476687dSEd Tanous         asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
19521476687dSEd Tanous             "/redfish/v1/Managers/bmc/VirtualMedia";
19537e860f15SJohn Edward Broadbent #endif // BMCWEB_ENABLE_VM_NBDPROXY
19547e860f15SJohn Edward Broadbent 
19557e860f15SJohn Edward Broadbent         // default oem data
19567e860f15SJohn Edward Broadbent         nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
19577e860f15SJohn Edward Broadbent         nlohmann::json& oemOpenbmc = oem["OpenBmc"];
19587e860f15SJohn Edward Broadbent         oem["@odata.type"] = "#OemManager.Oem";
19597e860f15SJohn Edward Broadbent         oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
19607e860f15SJohn Edward Broadbent         oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
19617e860f15SJohn Edward Broadbent         oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
19621476687dSEd Tanous 
19631476687dSEd Tanous         nlohmann::json::object_t certificates;
19641476687dSEd Tanous         certificates["@odata.id"] =
19651476687dSEd Tanous             "/redfish/v1/Managers/bmc/Truststore/Certificates";
19661476687dSEd Tanous         oemOpenbmc["Certificates"] = std::move(certificates);
19677e860f15SJohn Edward Broadbent 
19687e860f15SJohn Edward Broadbent         // Manager.Reset (an action) can be many values, OpenBMC only
19697e860f15SJohn Edward Broadbent         // supports BMC reboot.
19707e860f15SJohn Edward Broadbent         nlohmann::json& managerReset =
19717e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
19727e860f15SJohn Edward Broadbent         managerReset["target"] =
19737e860f15SJohn Edward Broadbent             "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
19747e860f15SJohn Edward Broadbent         managerReset["@Redfish.ActionInfo"] =
19757e860f15SJohn Edward Broadbent             "/redfish/v1/Managers/bmc/ResetActionInfo";
19767e860f15SJohn Edward Broadbent 
19777e860f15SJohn Edward Broadbent         // ResetToDefaults (Factory Reset) has values like
19787e860f15SJohn Edward Broadbent         // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
19797e860f15SJohn Edward Broadbent         // on OpenBMC
19807e860f15SJohn Edward Broadbent         nlohmann::json& resetToDefaults =
19817e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
19827e860f15SJohn Edward Broadbent         resetToDefaults["target"] =
19837e860f15SJohn Edward Broadbent             "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
19847e860f15SJohn Edward Broadbent         resetToDefaults["ResetType@Redfish.AllowableValues"] = {"ResetAll"};
19857e860f15SJohn Edward Broadbent 
19867c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
19877c8c4058STejas Patil             crow::utility::getDateTimeOffsetNow();
19887c8c4058STejas Patil 
19897c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
19907c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
19917c8c4058STejas Patil             redfishDateTimeOffset.second;
19927e860f15SJohn Edward Broadbent 
19930e8ac5e7SGunnar Mills         // TODO (Gunnar): Remove these one day since moved to ComputerSystem
19940e8ac5e7SGunnar Mills         // Still used by OCP profiles
19950e8ac5e7SGunnar Mills         // https://github.com/opencomputeproject/OCP-Profiles/issues/23
19967e860f15SJohn Edward Broadbent         // Fill in SerialConsole info
19977e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
1998002d39b4SEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
1999002d39b4SEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] = {
2000002d39b4SEd Tanous             "IPMI", "SSH"};
20017e860f15SJohn Edward Broadbent #ifdef BMCWEB_ENABLE_KVM
20027e860f15SJohn Edward Broadbent         // Fill in GraphicalConsole info
2003002d39b4SEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
2004002d39b4SEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] =
2005002d39b4SEd Tanous             4;
20067e860f15SJohn Edward Broadbent         asyncResp->res
2007002d39b4SEd Tanous             .jsonValue["GraphicalConsole"]["ConnectTypesSupported"] = {"KVMIP"};
20087e860f15SJohn Edward Broadbent #endif // BMCWEB_ENABLE_KVM
20097e860f15SJohn Edward Broadbent 
2010002d39b4SEd Tanous         asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
20111476687dSEd Tanous 
20121476687dSEd Tanous         nlohmann::json::array_t managerForServers;
20131476687dSEd Tanous         nlohmann::json::object_t manager;
20141476687dSEd Tanous         manager["@odata.id"] = "/redfish/v1/Systems/system";
20151476687dSEd Tanous         managerForServers.push_back(std::move(manager));
20161476687dSEd Tanous 
20171476687dSEd Tanous         asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
20181476687dSEd Tanous             std::move(managerForServers);
20197e860f15SJohn Edward Broadbent 
20207e860f15SJohn Edward Broadbent         auto health = std::make_shared<HealthPopulate>(asyncResp);
20217e860f15SJohn Edward Broadbent         health->isManagersHealth = true;
20227e860f15SJohn Edward Broadbent         health->populate();
20237e860f15SJohn Edward Broadbent 
2024eee0013eSWilly Tu         sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
20257e860f15SJohn Edward Broadbent                                              "FirmwareVersion", true);
20267e860f15SJohn Edward Broadbent 
20277e860f15SJohn Edward Broadbent         managerGetLastResetTime(asyncResp);
20287e860f15SJohn Edward Broadbent 
2029a51fc2d2SSui Chen         // ManagerDiagnosticData is added for all BMCs.
2030a51fc2d2SSui Chen         nlohmann::json& managerDiagnosticData =
2031a51fc2d2SSui Chen             asyncResp->res.jsonValue["ManagerDiagnosticData"];
2032a51fc2d2SSui Chen         managerDiagnosticData["@odata.id"] =
2033a51fc2d2SSui Chen             "/redfish/v1/Managers/bmc/ManagerDiagnosticData";
2034a51fc2d2SSui Chen 
203554dce7f5SGunnar Mills #ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
20367e860f15SJohn Edward Broadbent         auto pids = std::make_shared<GetPIDValues>(asyncResp);
20377e860f15SJohn Edward Broadbent         pids->run();
203854dce7f5SGunnar Mills #endif
20397e860f15SJohn Edward Broadbent 
2040002d39b4SEd Tanous         getMainChassisId(asyncResp,
2041002d39b4SEd Tanous                          [](const std::string& chassisId,
2042002d39b4SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2043002d39b4SEd Tanous             aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
20441476687dSEd Tanous             nlohmann::json::array_t managerForChassis;
20458a592810SEd Tanous             nlohmann::json::object_t managerObj;
20468a592810SEd Tanous             managerObj["@odata.id"] = "/redfish/v1/Chassis/" + chassisId;
20478a592810SEd Tanous             managerForChassis.push_back(std::move(managerObj));
20481476687dSEd Tanous             aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
20491476687dSEd Tanous                 std::move(managerForChassis);
20501476687dSEd Tanous             aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
20511476687dSEd Tanous                 "/redfish/v1/Chassis/" + chassisId;
20527e860f15SJohn Edward Broadbent         });
20537e860f15SJohn Edward Broadbent 
20547e860f15SJohn Edward Broadbent         static bool started = false;
20557e860f15SJohn Edward Broadbent 
20567e860f15SJohn Edward Broadbent         if (!started)
20571abe55efSEd Tanous         {
20581e1e598dSJonathan Doman             sdbusplus::asio::getProperty<double>(
20591e1e598dSJonathan Doman                 *crow::connections::systemBus, "org.freedesktop.systemd1",
2060002d39b4SEd Tanous                 "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
2061002d39b4SEd Tanous                 "Progress",
20627e860f15SJohn Edward Broadbent                 [asyncResp](const boost::system::error_code ec,
20631e1e598dSJonathan Doman                             const double& val) {
20647e860f15SJohn Edward Broadbent                 if (ec)
20651abe55efSEd Tanous                 {
20667e860f15SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "Error while getting progress";
20677e860f15SJohn Edward Broadbent                     messages::internalError(asyncResp->res);
20687e860f15SJohn Edward Broadbent                     return;
20697e860f15SJohn Edward Broadbent                 }
20701e1e598dSJonathan Doman                 if (val < 1.0)
20717e860f15SJohn Edward Broadbent                 {
2072002d39b4SEd Tanous                     asyncResp->res.jsonValue["Status"]["State"] = "Starting";
20737e860f15SJohn Edward Broadbent                     started = true;
20747e860f15SJohn Edward Broadbent                 }
20751e1e598dSJonathan Doman                 });
20769c310685SBorawski.Lukasz         }
20779c310685SBorawski.Lukasz 
20787e860f15SJohn Edward Broadbent         crow::connections::systemBus->async_method_call(
20797e860f15SJohn Edward Broadbent             [asyncResp](
20807e860f15SJohn Edward Broadbent                 const boost::system::error_code ec,
2081b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
20827e860f15SJohn Edward Broadbent             if (ec)
20831abe55efSEd Tanous             {
2084002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "D-Bus response error on GetSubTree " << ec;
20857e860f15SJohn Edward Broadbent                 return;
20867e860f15SJohn Edward Broadbent             }
208726f6976fSEd Tanous             if (subtree.empty())
20887e860f15SJohn Edward Broadbent             {
20897e860f15SJohn Edward Broadbent                 BMCWEB_LOG_DEBUG << "Can't find bmc D-Bus object!";
20907e860f15SJohn Edward Broadbent                 return;
20917e860f15SJohn Edward Broadbent             }
20927e860f15SJohn Edward Broadbent             // Assume only 1 bmc D-Bus object
20937e860f15SJohn Edward Broadbent             // Throw an error if there is more than 1
20947e860f15SJohn Edward Broadbent             if (subtree.size() > 1)
20957e860f15SJohn Edward Broadbent             {
2096002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "Found more than 1 bmc D-Bus object!";
20977e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
20987e860f15SJohn Edward Broadbent                 return;
20997e860f15SJohn Edward Broadbent             }
21007e860f15SJohn Edward Broadbent 
2101002d39b4SEd Tanous             if (subtree[0].first.empty() || subtree[0].second.size() != 1)
21027e860f15SJohn Edward Broadbent             {
21037e860f15SJohn Edward Broadbent                 BMCWEB_LOG_DEBUG << "Error getting bmc D-Bus object!";
21047e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
21057e860f15SJohn Edward Broadbent                 return;
21067e860f15SJohn Edward Broadbent             }
21077e860f15SJohn Edward Broadbent 
21087e860f15SJohn Edward Broadbent             const std::string& path = subtree[0].first;
2109002d39b4SEd Tanous             const std::string& connectionName = subtree[0].second[0].first;
21107e860f15SJohn Edward Broadbent 
2111002d39b4SEd Tanous             for (const auto& interfaceName : subtree[0].second[0].second)
21127e860f15SJohn Edward Broadbent             {
21137e860f15SJohn Edward Broadbent                 if (interfaceName ==
21147e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Inventory.Decorator.Asset")
21157e860f15SJohn Edward Broadbent                 {
2116*fac6e53bSKrzysztof Grobelny 
2117*fac6e53bSKrzysztof Grobelny                     sdbusplus::asio::getAllProperties(
2118*fac6e53bSKrzysztof Grobelny                         *crow::connections::systemBus, connectionName, path,
2119*fac6e53bSKrzysztof Grobelny                         "xyz.openbmc_project.Inventory.Decorator.Asset",
21208a592810SEd Tanous                         [asyncResp](const boost::system::error_code ec2,
2121b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap&
21227e860f15SJohn Edward Broadbent                                         propertiesList) {
21238a592810SEd Tanous                         if (ec2)
21247e860f15SJohn Edward Broadbent                         {
2125002d39b4SEd Tanous                             BMCWEB_LOG_DEBUG << "Can't get bmc asset!";
21267e860f15SJohn Edward Broadbent                             return;
21277e860f15SJohn Edward Broadbent                         }
21287e860f15SJohn Edward Broadbent 
2129*fac6e53bSKrzysztof Grobelny                         const std::string* partNumber = nullptr;
2130*fac6e53bSKrzysztof Grobelny                         const std::string* serialNumber = nullptr;
2131*fac6e53bSKrzysztof Grobelny                         const std::string* manufacturer = nullptr;
2132*fac6e53bSKrzysztof Grobelny                         const std::string* model = nullptr;
2133*fac6e53bSKrzysztof Grobelny                         const std::string* sparePartNumber = nullptr;
2134*fac6e53bSKrzysztof Grobelny 
2135*fac6e53bSKrzysztof Grobelny                         const bool success = sdbusplus::unpackPropertiesNoThrow(
2136*fac6e53bSKrzysztof Grobelny                             dbus_utils::UnpackErrorPrinter(), propertiesList,
2137*fac6e53bSKrzysztof Grobelny                             "PartNumber", partNumber, "SerialNumber",
2138*fac6e53bSKrzysztof Grobelny                             serialNumber, "Manufacturer", manufacturer, "Model",
2139*fac6e53bSKrzysztof Grobelny                             model, "SparePartNumber", sparePartNumber);
2140*fac6e53bSKrzysztof Grobelny 
2141*fac6e53bSKrzysztof Grobelny                         if (!success)
21427e860f15SJohn Edward Broadbent                         {
2143002d39b4SEd Tanous                             messages::internalError(asyncResp->res);
21447e860f15SJohn Edward Broadbent                             return;
21457e860f15SJohn Edward Broadbent                         }
2146*fac6e53bSKrzysztof Grobelny 
2147*fac6e53bSKrzysztof Grobelny                         if (partNumber != nullptr)
2148*fac6e53bSKrzysztof Grobelny                         {
2149*fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["PartNumber"] =
2150*fac6e53bSKrzysztof Grobelny                                 *partNumber;
21517e860f15SJohn Edward Broadbent                         }
2152*fac6e53bSKrzysztof Grobelny 
2153*fac6e53bSKrzysztof Grobelny                         if (serialNumber != nullptr)
2154*fac6e53bSKrzysztof Grobelny                         {
2155*fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["SerialNumber"] =
2156*fac6e53bSKrzysztof Grobelny                                 *serialNumber;
21577e860f15SJohn Edward Broadbent                         }
2158*fac6e53bSKrzysztof Grobelny 
2159*fac6e53bSKrzysztof Grobelny                         if (manufacturer != nullptr)
2160*fac6e53bSKrzysztof Grobelny                         {
2161*fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["Manufacturer"] =
2162*fac6e53bSKrzysztof Grobelny                                 *manufacturer;
2163*fac6e53bSKrzysztof Grobelny                         }
2164*fac6e53bSKrzysztof Grobelny 
2165*fac6e53bSKrzysztof Grobelny                         if (model != nullptr)
2166*fac6e53bSKrzysztof Grobelny                         {
2167*fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["Model"] = *model;
2168*fac6e53bSKrzysztof Grobelny                         }
2169*fac6e53bSKrzysztof Grobelny 
2170*fac6e53bSKrzysztof Grobelny                         if (sparePartNumber != nullptr)
2171*fac6e53bSKrzysztof Grobelny                         {
2172*fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["SparePartNumber"] =
2173*fac6e53bSKrzysztof Grobelny                                 *sparePartNumber;
2174*fac6e53bSKrzysztof Grobelny                         }
2175*fac6e53bSKrzysztof Grobelny                         });
21767e860f15SJohn Edward Broadbent                 }
2177002d39b4SEd Tanous                 else if (interfaceName ==
21780fda0f12SGeorge Liu                          "xyz.openbmc_project.Inventory.Decorator.LocationCode")
21797e860f15SJohn Edward Broadbent                 {
21807e860f15SJohn Edward Broadbent                     getLocation(asyncResp, connectionName, path);
21817e860f15SJohn Edward Broadbent                 }
21827e860f15SJohn Edward Broadbent             }
21837e860f15SJohn Edward Broadbent             },
21847e860f15SJohn Edward Broadbent             "xyz.openbmc_project.ObjectMapper",
21857e860f15SJohn Edward Broadbent             "/xyz/openbmc_project/object_mapper",
21867e860f15SJohn Edward Broadbent             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
21877e860f15SJohn Edward Broadbent             "/xyz/openbmc_project/inventory", int32_t(0),
21887e860f15SJohn Edward Broadbent             std::array<const char*, 1>{
21897e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.Inventory.Item.Bmc"});
21907e860f15SJohn Edward Broadbent         });
21917e860f15SJohn Edward Broadbent 
21927e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
2193ed398213SEd Tanous         .privileges(redfish::privileges::patchManager)
219445ca1b86SEd Tanous         .methods(boost::beast::http::verb::patch)(
219545ca1b86SEd Tanous             [&app](const crow::Request& req,
21967e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
21973ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
219845ca1b86SEd Tanous         {
219945ca1b86SEd Tanous             return;
220045ca1b86SEd Tanous         }
22017e860f15SJohn Edward Broadbent         std::optional<nlohmann::json> oem;
22027e860f15SJohn Edward Broadbent         std::optional<nlohmann::json> links;
22037e860f15SJohn Edward Broadbent         std::optional<std::string> datetime;
22047e860f15SJohn Edward Broadbent 
220515ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "Oem", oem,
2206002d39b4SEd Tanous                                       "DateTime", datetime, "Links", links))
22077e860f15SJohn Edward Broadbent         {
22087e860f15SJohn Edward Broadbent             return;
22097e860f15SJohn Edward Broadbent         }
22107e860f15SJohn Edward Broadbent 
22117e860f15SJohn Edward Broadbent         if (oem)
22127e860f15SJohn Edward Broadbent         {
221354dce7f5SGunnar Mills #ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
22147e860f15SJohn Edward Broadbent             std::optional<nlohmann::json> openbmc;
2215002d39b4SEd Tanous             if (!redfish::json_util::readJson(*oem, asyncResp->res, "OpenBmc",
2216002d39b4SEd Tanous                                               openbmc))
22177e860f15SJohn Edward Broadbent             {
22187e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR
22197e860f15SJohn Edward Broadbent                     << "Illegal Property "
2220002d39b4SEd Tanous                     << oem->dump(2, ' ', true,
22217e860f15SJohn Edward Broadbent                                  nlohmann::json::error_handler_t::replace);
22227e860f15SJohn Edward Broadbent                 return;
22237e860f15SJohn Edward Broadbent             }
22247e860f15SJohn Edward Broadbent             if (openbmc)
22257e860f15SJohn Edward Broadbent             {
22267e860f15SJohn Edward Broadbent                 std::optional<nlohmann::json> fan;
2227002d39b4SEd Tanous                 if (!redfish::json_util::readJson(*openbmc, asyncResp->res,
2228002d39b4SEd Tanous                                                   "Fan", fan))
22297e860f15SJohn Edward Broadbent                 {
22307e860f15SJohn Edward Broadbent                     BMCWEB_LOG_ERROR
22317e860f15SJohn Edward Broadbent                         << "Illegal Property "
2232002d39b4SEd Tanous                         << openbmc->dump(
2233002d39b4SEd Tanous                                2, ' ', true,
2234002d39b4SEd Tanous                                nlohmann::json::error_handler_t::replace);
22357e860f15SJohn Edward Broadbent                     return;
22367e860f15SJohn Edward Broadbent                 }
22377e860f15SJohn Edward Broadbent                 if (fan)
22387e860f15SJohn Edward Broadbent                 {
2239002d39b4SEd Tanous                     auto pid = std::make_shared<SetPIDValues>(asyncResp, *fan);
22407e860f15SJohn Edward Broadbent                     pid->run();
22417e860f15SJohn Edward Broadbent                 }
22427e860f15SJohn Edward Broadbent             }
224354dce7f5SGunnar Mills #else
224454dce7f5SGunnar Mills             messages::propertyUnknown(asyncResp->res, "Oem");
224554dce7f5SGunnar Mills             return;
224654dce7f5SGunnar Mills #endif
22477e860f15SJohn Edward Broadbent         }
22487e860f15SJohn Edward Broadbent         if (links)
22497e860f15SJohn Edward Broadbent         {
22507e860f15SJohn Edward Broadbent             std::optional<nlohmann::json> activeSoftwareImage;
22517e860f15SJohn Edward Broadbent             if (!redfish::json_util::readJson(*links, asyncResp->res,
22527e860f15SJohn Edward Broadbent                                               "ActiveSoftwareImage",
22537e860f15SJohn Edward Broadbent                                               activeSoftwareImage))
22547e860f15SJohn Edward Broadbent             {
22557e860f15SJohn Edward Broadbent                 return;
22567e860f15SJohn Edward Broadbent             }
22577e860f15SJohn Edward Broadbent             if (activeSoftwareImage)
22587e860f15SJohn Edward Broadbent             {
22597e860f15SJohn Edward Broadbent                 std::optional<std::string> odataId;
2260002d39b4SEd Tanous                 if (!json_util::readJson(*activeSoftwareImage, asyncResp->res,
2261002d39b4SEd Tanous                                          "@odata.id", odataId))
22627e860f15SJohn Edward Broadbent                 {
22637e860f15SJohn Edward Broadbent                     return;
22647e860f15SJohn Edward Broadbent                 }
22657e860f15SJohn Edward Broadbent 
22667e860f15SJohn Edward Broadbent                 if (odataId)
22677e860f15SJohn Edward Broadbent                 {
22687e860f15SJohn Edward Broadbent                     setActiveFirmwareImage(asyncResp, *odataId);
22697e860f15SJohn Edward Broadbent                 }
22707e860f15SJohn Edward Broadbent             }
22717e860f15SJohn Edward Broadbent         }
22727e860f15SJohn Edward Broadbent         if (datetime)
22737e860f15SJohn Edward Broadbent         {
22747e860f15SJohn Edward Broadbent             setDateTime(asyncResp, std::move(*datetime));
22757e860f15SJohn Edward Broadbent         }
22767e860f15SJohn Edward Broadbent         });
22777e860f15SJohn Edward Broadbent }
22787e860f15SJohn Edward Broadbent 
22797e860f15SJohn Edward Broadbent inline void requestRoutesManagerCollection(App& app)
22807e860f15SJohn Edward Broadbent {
22817e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
2282ed398213SEd Tanous         .privileges(redfish::privileges::getManagerCollection)
22837e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
228445ca1b86SEd Tanous             [&app](const crow::Request& req,
22857e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
22863ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
228745ca1b86SEd Tanous         {
228845ca1b86SEd Tanous             return;
228945ca1b86SEd Tanous         }
229083ff9ab6SJames Feist         // Collections don't include the static data added by SubRoute
229183ff9ab6SJames Feist         // because it has a duplicate entry for members
22928d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
22938d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
22948d1b46d7Szhanghch05             "#ManagerCollection.ManagerCollection";
22958d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Manager Collection";
22968d1b46d7Szhanghch05         asyncResp->res.jsonValue["Members@odata.count"] = 1;
22971476687dSEd Tanous         nlohmann::json::array_t members;
22981476687dSEd Tanous         nlohmann::json& bmc = members.emplace_back();
22991476687dSEd Tanous         bmc["@odata.id"] = "/redfish/v1/Managers/bmc";
23001476687dSEd Tanous         asyncResp->res.jsonValue["Members"] = std::move(members);
23017e860f15SJohn Edward Broadbent         });
23029c310685SBorawski.Lukasz }
23039c310685SBorawski.Lukasz } // namespace redfish
2304