xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision 5e7e2dc585dc7a7e62d2b648b541e7f50c39ea5d)
19c310685SBorawski.Lukasz /*
29c310685SBorawski.Lukasz // Copyright (c) 2018 Intel Corporation
39c310685SBorawski.Lukasz //
49c310685SBorawski.Lukasz // Licensed under the Apache License, Version 2.0 (the "License");
59c310685SBorawski.Lukasz // you may not use this file except in compliance with the License.
69c310685SBorawski.Lukasz // You may obtain a copy of the License at
79c310685SBorawski.Lukasz //
89c310685SBorawski.Lukasz //      http://www.apache.org/licenses/LICENSE-2.0
99c310685SBorawski.Lukasz //
109c310685SBorawski.Lukasz // Unless required by applicable law or agreed to in writing, software
119c310685SBorawski.Lukasz // distributed under the License is distributed on an "AS IS" BASIS,
129c310685SBorawski.Lukasz // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139c310685SBorawski.Lukasz // See the License for the specific language governing permissions and
149c310685SBorawski.Lukasz // limitations under the License.
159c310685SBorawski.Lukasz */
169c310685SBorawski.Lukasz #pragma once
179c310685SBorawski.Lukasz 
18a51fc2d2SSui Chen #include "app.hpp"
19a51fc2d2SSui Chen #include "dbus_utility.hpp"
20b49ac873SJames Feist #include "health.hpp"
21a51fc2d2SSui Chen #include "query.hpp"
22c5d03ff4SJennifer Lee #include "redfish_util.hpp"
23a51fc2d2SSui Chen #include "registries/privilege_registry.hpp"
24fac6e53bSKrzysztof Grobelny #include "utils/dbus_utils.hpp"
253ccb3adbSEd Tanous #include "utils/json_utils.hpp"
26a51fc2d2SSui Chen #include "utils/sw_utils.hpp"
27a51fc2d2SSui Chen #include "utils/systemd_utils.hpp"
282b82937eSEd Tanous #include "utils/time_utils.hpp"
299c310685SBorawski.Lukasz 
30e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
31fac6e53bSKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
32fac6e53bSKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
331214b7e7SGunnar Mills 
34a170f275SEd Tanous #include <algorithm>
35e99073f5SGeorge Liu #include <array>
364bfefa74SGunnar Mills #include <cstdint>
371214b7e7SGunnar Mills #include <memory>
381214b7e7SGunnar Mills #include <sstream>
39e99073f5SGeorge Liu #include <string_view>
40abf2add6SEd Tanous #include <variant>
415b4aa86bSJames Feist 
421abe55efSEd Tanous namespace redfish
431abe55efSEd Tanous {
44ed5befbdSJennifer Lee 
45ed5befbdSJennifer Lee /**
462a5c4407SGunnar Mills  * Function reboots the BMC.
472a5c4407SGunnar Mills  *
482a5c4407SGunnar Mills  * @param[in] asyncResp - Shared pointer for completing asynchronous calls
49ed5befbdSJennifer Lee  */
508d1b46d7Szhanghch05 inline void
518d1b46d7Szhanghch05     doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
52ed5befbdSJennifer Lee {
53ed5befbdSJennifer Lee     const char* processName = "xyz.openbmc_project.State.BMC";
54ed5befbdSJennifer Lee     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
55ed5befbdSJennifer Lee     const char* interfaceName = "xyz.openbmc_project.State.BMC";
56ed5befbdSJennifer Lee     const std::string& propertyValue =
57ed5befbdSJennifer Lee         "xyz.openbmc_project.State.BMC.Transition.Reboot";
58ed5befbdSJennifer Lee     const char* destProperty = "RequestedBMCTransition";
59ed5befbdSJennifer Lee 
60ed5befbdSJennifer Lee     // Create the D-Bus variant for D-Bus call.
61168e20c1SEd Tanous     dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
62ed5befbdSJennifer Lee 
63ed5befbdSJennifer Lee     crow::connections::systemBus->async_method_call(
64*5e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
65ed5befbdSJennifer Lee         // Use "Set" method to set the property value.
66ed5befbdSJennifer Lee         if (ec)
67ed5befbdSJennifer Lee         {
682a5c4407SGunnar Mills             BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
69ed5befbdSJennifer Lee             messages::internalError(asyncResp->res);
70ed5befbdSJennifer Lee             return;
71ed5befbdSJennifer Lee         }
72ed5befbdSJennifer Lee 
73ed5befbdSJennifer Lee         messages::success(asyncResp->res);
74ed5befbdSJennifer Lee         },
75ed5befbdSJennifer Lee         processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
76ed5befbdSJennifer Lee         interfaceName, destProperty, dbusPropertyValue);
77ed5befbdSJennifer Lee }
782a5c4407SGunnar Mills 
798d1b46d7Szhanghch05 inline void
808d1b46d7Szhanghch05     doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
81f92af389SJayaprakash Mutyala {
82f92af389SJayaprakash Mutyala     const char* processName = "xyz.openbmc_project.State.BMC";
83f92af389SJayaprakash Mutyala     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
84f92af389SJayaprakash Mutyala     const char* interfaceName = "xyz.openbmc_project.State.BMC";
85f92af389SJayaprakash Mutyala     const std::string& propertyValue =
86f92af389SJayaprakash Mutyala         "xyz.openbmc_project.State.BMC.Transition.HardReboot";
87f92af389SJayaprakash Mutyala     const char* destProperty = "RequestedBMCTransition";
88f92af389SJayaprakash Mutyala 
89f92af389SJayaprakash Mutyala     // Create the D-Bus variant for D-Bus call.
90168e20c1SEd Tanous     dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
91f92af389SJayaprakash Mutyala 
92f92af389SJayaprakash Mutyala     crow::connections::systemBus->async_method_call(
93*5e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
94f92af389SJayaprakash Mutyala         // Use "Set" method to set the property value.
95f92af389SJayaprakash Mutyala         if (ec)
96f92af389SJayaprakash Mutyala         {
97f92af389SJayaprakash Mutyala             BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
98f92af389SJayaprakash Mutyala             messages::internalError(asyncResp->res);
99f92af389SJayaprakash Mutyala             return;
100f92af389SJayaprakash Mutyala         }
101f92af389SJayaprakash Mutyala 
102f92af389SJayaprakash Mutyala         messages::success(asyncResp->res);
103f92af389SJayaprakash Mutyala         },
104f92af389SJayaprakash Mutyala         processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
105f92af389SJayaprakash Mutyala         interfaceName, destProperty, dbusPropertyValue);
106f92af389SJayaprakash Mutyala }
107f92af389SJayaprakash Mutyala 
1082a5c4407SGunnar Mills /**
1092a5c4407SGunnar Mills  * ManagerResetAction class supports the POST method for the Reset (reboot)
1102a5c4407SGunnar Mills  * action.
1112a5c4407SGunnar Mills  */
1127e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetAction(App& app)
1132a5c4407SGunnar Mills {
1142a5c4407SGunnar Mills     /**
1152a5c4407SGunnar Mills      * Function handles POST method request.
1162a5c4407SGunnar Mills      * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
117f92af389SJayaprakash Mutyala      * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
1182a5c4407SGunnar Mills      */
1197e860f15SJohn Edward Broadbent 
1207e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
121ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
1227e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
12345ca1b86SEd Tanous             [&app](const crow::Request& req,
1247e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1253ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
12645ca1b86SEd Tanous         {
12745ca1b86SEd Tanous             return;
12845ca1b86SEd Tanous         }
1292a5c4407SGunnar Mills         BMCWEB_LOG_DEBUG << "Post Manager Reset.";
1302a5c4407SGunnar Mills 
1312a5c4407SGunnar Mills         std::string resetType;
1322a5c4407SGunnar Mills 
13315ed6780SWilly Tu         if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
1347e860f15SJohn Edward Broadbent                                        resetType))
1352a5c4407SGunnar Mills         {
1362a5c4407SGunnar Mills             return;
1372a5c4407SGunnar Mills         }
1382a5c4407SGunnar Mills 
139f92af389SJayaprakash Mutyala         if (resetType == "GracefulRestart")
140f92af389SJayaprakash Mutyala         {
141f92af389SJayaprakash Mutyala             BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
142f92af389SJayaprakash Mutyala             doBMCGracefulRestart(asyncResp);
143f92af389SJayaprakash Mutyala             return;
144f92af389SJayaprakash Mutyala         }
1453174e4dfSEd Tanous         if (resetType == "ForceRestart")
146f92af389SJayaprakash Mutyala         {
147f92af389SJayaprakash Mutyala             BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
148f92af389SJayaprakash Mutyala             doBMCForceRestart(asyncResp);
149f92af389SJayaprakash Mutyala             return;
150f92af389SJayaprakash Mutyala         }
1512a5c4407SGunnar Mills         BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
1522a5c4407SGunnar Mills                          << resetType;
1532a5c4407SGunnar Mills         messages::actionParameterNotSupported(asyncResp->res, resetType,
1542a5c4407SGunnar Mills                                               "ResetType");
1552a5c4407SGunnar Mills 
1562a5c4407SGunnar Mills         return;
1577e860f15SJohn Edward Broadbent         });
1582a5c4407SGunnar Mills }
159ed5befbdSJennifer Lee 
1603e40fc74SGunnar Mills /**
1613e40fc74SGunnar Mills  * ManagerResetToDefaultsAction class supports POST method for factory reset
1623e40fc74SGunnar Mills  * action.
1633e40fc74SGunnar Mills  */
1647e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetToDefaultsAction(App& app)
1653e40fc74SGunnar Mills {
1663e40fc74SGunnar Mills 
1673e40fc74SGunnar Mills     /**
1683e40fc74SGunnar Mills      * Function handles ResetToDefaults POST method request.
1693e40fc74SGunnar Mills      *
1703e40fc74SGunnar Mills      * Analyzes POST body message and factory resets BMC by calling
1713e40fc74SGunnar Mills      * BMC code updater factory reset followed by a BMC reboot.
1723e40fc74SGunnar Mills      *
1733e40fc74SGunnar Mills      * BMC code updater factory reset wipes the whole BMC read-write
1743e40fc74SGunnar Mills      * filesystem which includes things like the network settings.
1753e40fc74SGunnar Mills      *
1763e40fc74SGunnar Mills      * OpenBMC only supports ResetToDefaultsType "ResetAll".
1773e40fc74SGunnar Mills      */
1787e860f15SJohn Edward Broadbent 
1797e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
1807e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/")
181ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
1827e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
18345ca1b86SEd Tanous             [&app](const crow::Request& req,
1847e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1853ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
18645ca1b86SEd Tanous         {
18745ca1b86SEd Tanous             return;
18845ca1b86SEd Tanous         }
1893e40fc74SGunnar Mills         BMCWEB_LOG_DEBUG << "Post ResetToDefaults.";
1903e40fc74SGunnar Mills 
1913e40fc74SGunnar Mills         std::string resetType;
1923e40fc74SGunnar Mills 
193002d39b4SEd Tanous         if (!json_util::readJsonAction(req, asyncResp->res,
194002d39b4SEd Tanous                                        "ResetToDefaultsType", resetType))
1953e40fc74SGunnar Mills         {
1963e40fc74SGunnar Mills             BMCWEB_LOG_DEBUG << "Missing property ResetToDefaultsType.";
1973e40fc74SGunnar Mills 
198002d39b4SEd Tanous             messages::actionParameterMissing(asyncResp->res, "ResetToDefaults",
1993e40fc74SGunnar Mills                                              "ResetToDefaultsType");
2003e40fc74SGunnar Mills             return;
2013e40fc74SGunnar Mills         }
2023e40fc74SGunnar Mills 
2033e40fc74SGunnar Mills         if (resetType != "ResetAll")
2043e40fc74SGunnar Mills         {
2050fda0f12SGeorge Liu             BMCWEB_LOG_DEBUG
2060fda0f12SGeorge Liu                 << "Invalid property value for ResetToDefaultsType: "
2073e40fc74SGunnar Mills                 << resetType;
208002d39b4SEd Tanous             messages::actionParameterNotSupported(asyncResp->res, resetType,
209002d39b4SEd Tanous                                                   "ResetToDefaultsType");
2103e40fc74SGunnar Mills             return;
2113e40fc74SGunnar Mills         }
2123e40fc74SGunnar Mills 
2133e40fc74SGunnar Mills         crow::connections::systemBus->async_method_call(
214*5e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
2153e40fc74SGunnar Mills             if (ec)
2163e40fc74SGunnar Mills             {
217002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "Failed to ResetToDefaults: " << ec;
2183e40fc74SGunnar Mills                 messages::internalError(asyncResp->res);
2193e40fc74SGunnar Mills                 return;
2203e40fc74SGunnar Mills             }
2213e40fc74SGunnar Mills             // Factory Reset doesn't actually happen until a reboot
2223e40fc74SGunnar Mills             // Can't erase what the BMC is running on
2233e40fc74SGunnar Mills             doBMCGracefulRestart(asyncResp);
2243e40fc74SGunnar Mills             },
2253e40fc74SGunnar Mills             "xyz.openbmc_project.Software.BMC.Updater",
2263e40fc74SGunnar Mills             "/xyz/openbmc_project/software",
2273e40fc74SGunnar Mills             "xyz.openbmc_project.Common.FactoryReset", "Reset");
2287e860f15SJohn Edward Broadbent         });
2293e40fc74SGunnar Mills }
2303e40fc74SGunnar Mills 
2311cb1a9e6SAppaRao Puli /**
2321cb1a9e6SAppaRao Puli  * ManagerResetActionInfo derived class for delivering Manager
2331cb1a9e6SAppaRao Puli  * ResetType AllowableValues using ResetInfo schema.
2341cb1a9e6SAppaRao Puli  */
2357e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetActionInfo(App& app)
2361cb1a9e6SAppaRao Puli {
2371cb1a9e6SAppaRao Puli     /**
2381cb1a9e6SAppaRao Puli      * Functions triggers appropriate requests on DBus
2391cb1a9e6SAppaRao Puli      */
2407e860f15SJohn Edward Broadbent 
2417e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/ResetActionInfo/")
242ed398213SEd Tanous         .privileges(redfish::privileges::getActionInfo)
2437e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
24445ca1b86SEd Tanous             [&app](const crow::Request& req,
2457e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2463ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
24745ca1b86SEd Tanous         {
24845ca1b86SEd Tanous             return;
24945ca1b86SEd Tanous         }
2501476687dSEd Tanous 
2511476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
2521476687dSEd Tanous             "#ActionInfo.v1_1_2.ActionInfo";
2531476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
2541476687dSEd Tanous             "/redfish/v1/Managers/bmc/ResetActionInfo";
2551476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "Reset Action Info";
2561476687dSEd Tanous         asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
2571476687dSEd Tanous         nlohmann::json::object_t parameter;
2581476687dSEd Tanous         parameter["Name"] = "ResetType";
2591476687dSEd Tanous         parameter["Required"] = true;
2601476687dSEd Tanous         parameter["DataType"] = "String";
2611476687dSEd Tanous 
2621476687dSEd Tanous         nlohmann::json::array_t allowableValues;
2631476687dSEd Tanous         allowableValues.push_back("GracefulRestart");
2641476687dSEd Tanous         allowableValues.push_back("ForceRestart");
2651476687dSEd Tanous         parameter["AllowableValues"] = std::move(allowableValues);
2661476687dSEd Tanous 
2671476687dSEd Tanous         nlohmann::json::array_t parameters;
2681476687dSEd Tanous         parameters.push_back(std::move(parameter));
2691476687dSEd Tanous 
2701476687dSEd Tanous         asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
2717e860f15SJohn Edward Broadbent         });
2721cb1a9e6SAppaRao Puli }
2731cb1a9e6SAppaRao Puli 
2745b4aa86bSJames Feist static constexpr const char* objectManagerIface =
2755b4aa86bSJames Feist     "org.freedesktop.DBus.ObjectManager";
2765b4aa86bSJames Feist static constexpr const char* pidConfigurationIface =
2775b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid";
2785b4aa86bSJames Feist static constexpr const char* pidZoneConfigurationIface =
2795b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid.Zone";
280b7a08d04SJames Feist static constexpr const char* stepwiseConfigurationIface =
281b7a08d04SJames Feist     "xyz.openbmc_project.Configuration.Stepwise";
28273df0db0SJames Feist static constexpr const char* thermalModeIface =
28373df0db0SJames Feist     "xyz.openbmc_project.Control.ThermalMode";
2849c310685SBorawski.Lukasz 
2858d1b46d7Szhanghch05 inline void
2868d1b46d7Szhanghch05     asyncPopulatePid(const std::string& connection, const std::string& path,
28773df0db0SJames Feist                      const std::string& currentProfile,
28873df0db0SJames Feist                      const std::vector<std::string>& supportedProfiles,
2898d1b46d7Szhanghch05                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2905b4aa86bSJames Feist {
2915b4aa86bSJames Feist 
2925b4aa86bSJames Feist     crow::connections::systemBus->async_method_call(
29373df0db0SJames Feist         [asyncResp, currentProfile, supportedProfiles](
294*5e7e2dc5SEd Tanous             const boost::system::error_code& ec,
2955b4aa86bSJames Feist             const dbus::utility::ManagedObjectType& managedObj) {
2965b4aa86bSJames Feist         if (ec)
2975b4aa86bSJames Feist         {
2985b4aa86bSJames Feist             BMCWEB_LOG_ERROR << ec;
2995b4aa86bSJames Feist             asyncResp->res.jsonValue.clear();
300f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
3015b4aa86bSJames Feist             return;
3025b4aa86bSJames Feist         }
3035b4aa86bSJames Feist         nlohmann::json& configRoot =
3045b4aa86bSJames Feist             asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
3055b4aa86bSJames Feist         nlohmann::json& fans = configRoot["FanControllers"];
3065b4aa86bSJames Feist         fans["@odata.type"] = "#OemManager.FanControllers";
3070fda0f12SGeorge Liu         fans["@odata.id"] =
3080fda0f12SGeorge Liu             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers";
3095b4aa86bSJames Feist 
3105b4aa86bSJames Feist         nlohmann::json& pids = configRoot["PidControllers"];
3115b4aa86bSJames Feist         pids["@odata.type"] = "#OemManager.PidControllers";
3125b4aa86bSJames Feist         pids["@odata.id"] =
3135b4aa86bSJames Feist             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
3145b4aa86bSJames Feist 
315b7a08d04SJames Feist         nlohmann::json& stepwise = configRoot["StepwiseControllers"];
316b7a08d04SJames Feist         stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
317b7a08d04SJames Feist         stepwise["@odata.id"] =
318b7a08d04SJames Feist             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
319b7a08d04SJames Feist 
3205b4aa86bSJames Feist         nlohmann::json& zones = configRoot["FanZones"];
3215b4aa86bSJames Feist         zones["@odata.id"] =
3225b4aa86bSJames Feist             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
3235b4aa86bSJames Feist         zones["@odata.type"] = "#OemManager.FanZones";
324002d39b4SEd Tanous         configRoot["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
3255b4aa86bSJames Feist         configRoot["@odata.type"] = "#OemManager.Fan";
32673df0db0SJames Feist         configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
32773df0db0SJames Feist 
32873df0db0SJames Feist         if (!currentProfile.empty())
32973df0db0SJames Feist         {
33073df0db0SJames Feist             configRoot["Profile"] = currentProfile;
33173df0db0SJames Feist         }
33273df0db0SJames Feist         BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !";
3335b4aa86bSJames Feist 
3345b4aa86bSJames Feist         for (const auto& pathPair : managedObj)
3355b4aa86bSJames Feist         {
3365b4aa86bSJames Feist             for (const auto& intfPair : pathPair.second)
3375b4aa86bSJames Feist             {
3385b4aa86bSJames Feist                 if (intfPair.first != pidConfigurationIface &&
339b7a08d04SJames Feist                     intfPair.first != pidZoneConfigurationIface &&
340b7a08d04SJames Feist                     intfPair.first != stepwiseConfigurationIface)
3415b4aa86bSJames Feist                 {
3425b4aa86bSJames Feist                     continue;
3435b4aa86bSJames Feist                 }
34473df0db0SJames Feist 
345711ac7a9SEd Tanous                 std::string name;
346711ac7a9SEd Tanous 
347711ac7a9SEd Tanous                 for (const std::pair<std::string,
348002d39b4SEd Tanous                                      dbus::utility::DbusVariantType>& propPair :
349002d39b4SEd Tanous                      intfPair.second)
350711ac7a9SEd Tanous                 {
351711ac7a9SEd Tanous                     if (propPair.first == "Name")
352711ac7a9SEd Tanous                     {
3535b4aa86bSJames Feist                         const std::string* namePtr =
354711ac7a9SEd Tanous                             std::get_if<std::string>(&propPair.second);
3555b4aa86bSJames Feist                         if (namePtr == nullptr)
3565b4aa86bSJames Feist                         {
3575b4aa86bSJames Feist                             BMCWEB_LOG_ERROR << "Pid Name Field illegal";
358b7a08d04SJames Feist                             messages::internalError(asyncResp->res);
3595b4aa86bSJames Feist                             return;
3605b4aa86bSJames Feist                         }
361db697703SWilly Tu                         name = *namePtr;
3625b4aa86bSJames Feist                         dbus::utility::escapePathForDbus(name);
363711ac7a9SEd Tanous                     }
364711ac7a9SEd Tanous                     else if (propPair.first == "Profiles")
36573df0db0SJames Feist                     {
36673df0db0SJames Feist                         const std::vector<std::string>* profiles =
36773df0db0SJames Feist                             std::get_if<std::vector<std::string>>(
368711ac7a9SEd Tanous                                 &propPair.second);
36973df0db0SJames Feist                         if (profiles == nullptr)
37073df0db0SJames Feist                         {
371002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "Pid Profiles Field illegal";
37273df0db0SJames Feist                             messages::internalError(asyncResp->res);
37373df0db0SJames Feist                             return;
37473df0db0SJames Feist                         }
37573df0db0SJames Feist                         if (std::find(profiles->begin(), profiles->end(),
37673df0db0SJames Feist                                       currentProfile) == profiles->end())
37773df0db0SJames Feist                         {
37873df0db0SJames Feist                             BMCWEB_LOG_INFO
379002d39b4SEd Tanous                                 << name << " not supported in current profile";
38073df0db0SJames Feist                             continue;
38173df0db0SJames Feist                         }
38273df0db0SJames Feist                     }
383711ac7a9SEd Tanous                 }
384b7a08d04SJames Feist                 nlohmann::json* config = nullptr;
385c33a90ecSJames Feist                 const std::string* classPtr = nullptr;
386711ac7a9SEd Tanous 
387711ac7a9SEd Tanous                 for (const std::pair<std::string,
388002d39b4SEd Tanous                                      dbus::utility::DbusVariantType>& propPair :
389002d39b4SEd Tanous                      intfPair.second)
390c33a90ecSJames Feist                 {
391727dc83fSLei YU                     if (propPair.first == "Class")
392711ac7a9SEd Tanous                     {
393002d39b4SEd Tanous                         classPtr = std::get_if<std::string>(&propPair.second);
394711ac7a9SEd Tanous                     }
395c33a90ecSJames Feist                 }
396c33a90ecSJames Feist 
397eddfc437SWilly Tu                 boost::urls::url url = crow::utility::urlFromPieces(
398eddfc437SWilly Tu                     "redfish", "v1", "Managers", "bmc");
3995b4aa86bSJames Feist                 if (intfPair.first == pidZoneConfigurationIface)
4005b4aa86bSJames Feist                 {
4015b4aa86bSJames Feist                     std::string chassis;
402002d39b4SEd Tanous                     if (!dbus::utility::getNthStringFromPath(pathPair.first.str,
403002d39b4SEd Tanous                                                              5, chassis))
4045b4aa86bSJames Feist                     {
4055b4aa86bSJames Feist                         chassis = "#IllegalValue";
4065b4aa86bSJames Feist                     }
4075b4aa86bSJames Feist                     nlohmann::json& zone = zones[name];
408eddfc437SWilly Tu                     zone["Chassis"]["@odata.id"] = crow::utility::urlFromPieces(
409eddfc437SWilly Tu                         "redfish", "v1", "Chassis", chassis);
410eddfc437SWilly Tu                     url.set_fragment(
411eddfc437SWilly Tu                         ("/Oem/OpenBmc/Fan/FanZones"_json_pointer / name)
412eddfc437SWilly Tu                             .to_string());
413eddfc437SWilly Tu                     zone["@odata.id"] = std::move(url);
4145b4aa86bSJames Feist                     zone["@odata.type"] = "#OemManager.FanZone";
415b7a08d04SJames Feist                     config = &zone;
4165b4aa86bSJames Feist                 }
4175b4aa86bSJames Feist 
418b7a08d04SJames Feist                 else if (intfPair.first == stepwiseConfigurationIface)
4195b4aa86bSJames Feist                 {
420c33a90ecSJames Feist                     if (classPtr == nullptr)
421c33a90ecSJames Feist                     {
422c33a90ecSJames Feist                         BMCWEB_LOG_ERROR << "Pid Class Field illegal";
423c33a90ecSJames Feist                         messages::internalError(asyncResp->res);
424c33a90ecSJames Feist                         return;
425c33a90ecSJames Feist                     }
426c33a90ecSJames Feist 
427b7a08d04SJames Feist                     nlohmann::json& controller = stepwise[name];
428b7a08d04SJames Feist                     config = &controller;
429eddfc437SWilly Tu                     url.set_fragment(
430eddfc437SWilly Tu                         ("/Oem/OpenBmc/Fan/StepwiseControllers"_json_pointer /
431eddfc437SWilly Tu                          name)
432eddfc437SWilly Tu                             .to_string());
433eddfc437SWilly Tu                     controller["@odata.id"] = std::move(url);
434b7a08d04SJames Feist                     controller["@odata.type"] =
435b7a08d04SJames Feist                         "#OemManager.StepwiseController";
436b7a08d04SJames Feist 
437c33a90ecSJames Feist                     controller["Direction"] = *classPtr;
4385b4aa86bSJames Feist                 }
4395b4aa86bSJames Feist 
4405b4aa86bSJames Feist                 // pid and fans are off the same configuration
441b7a08d04SJames Feist                 else if (intfPair.first == pidConfigurationIface)
4425b4aa86bSJames Feist                 {
443c33a90ecSJames Feist 
4445b4aa86bSJames Feist                     if (classPtr == nullptr)
4455b4aa86bSJames Feist                     {
4465b4aa86bSJames Feist                         BMCWEB_LOG_ERROR << "Pid Class Field illegal";
447a08b46ccSJason M. Bills                         messages::internalError(asyncResp->res);
4485b4aa86bSJames Feist                         return;
4495b4aa86bSJames Feist                     }
4505b4aa86bSJames Feist                     bool isFan = *classPtr == "fan";
451002d39b4SEd Tanous                     nlohmann::json& element = isFan ? fans[name] : pids[name];
452b7a08d04SJames Feist                     config = &element;
4535b4aa86bSJames Feist                     if (isFan)
4545b4aa86bSJames Feist                     {
455eddfc437SWilly Tu                         url.set_fragment(
456eddfc437SWilly Tu                             ("/Oem/OpenBmc/Fan/FanControllers"_json_pointer /
457eddfc437SWilly Tu                              name)
458eddfc437SWilly Tu                                 .to_string());
459eddfc437SWilly Tu                         element["@odata.id"] = std::move(url);
460002d39b4SEd Tanous                         element["@odata.type"] = "#OemManager.FanController";
4615b4aa86bSJames Feist                     }
4625b4aa86bSJames Feist                     else
4635b4aa86bSJames Feist                     {
464eddfc437SWilly Tu                         url.set_fragment(
465eddfc437SWilly Tu                             ("/Oem/OpenBmc/Fan/PidControllers"_json_pointer /
466eddfc437SWilly Tu                              name)
467eddfc437SWilly Tu                                 .to_string());
468eddfc437SWilly Tu                         element["@odata.id"] = std::move(url);
469002d39b4SEd Tanous                         element["@odata.type"] = "#OemManager.PidController";
4705b4aa86bSJames Feist                     }
471b7a08d04SJames Feist                 }
472b7a08d04SJames Feist                 else
473b7a08d04SJames Feist                 {
474b7a08d04SJames Feist                     BMCWEB_LOG_ERROR << "Unexpected configuration";
475b7a08d04SJames Feist                     messages::internalError(asyncResp->res);
476b7a08d04SJames Feist                     return;
477b7a08d04SJames Feist                 }
478b7a08d04SJames Feist 
479b7a08d04SJames Feist                 // used for making maps out of 2 vectors
480b7a08d04SJames Feist                 const std::vector<double>* keys = nullptr;
481b7a08d04SJames Feist                 const std::vector<double>* values = nullptr;
482b7a08d04SJames Feist 
483b7a08d04SJames Feist                 for (const auto& propertyPair : intfPair.second)
484b7a08d04SJames Feist                 {
485b7a08d04SJames Feist                     if (propertyPair.first == "Type" ||
486b7a08d04SJames Feist                         propertyPair.first == "Class" ||
487b7a08d04SJames Feist                         propertyPair.first == "Name")
488b7a08d04SJames Feist                     {
489b7a08d04SJames Feist                         continue;
490b7a08d04SJames Feist                     }
491b7a08d04SJames Feist 
492b7a08d04SJames Feist                     // zones
493b7a08d04SJames Feist                     if (intfPair.first == pidZoneConfigurationIface)
494b7a08d04SJames Feist                     {
495b7a08d04SJames Feist                         const double* ptr =
496abf2add6SEd Tanous                             std::get_if<double>(&propertyPair.second);
497b7a08d04SJames Feist                         if (ptr == nullptr)
498b7a08d04SJames Feist                         {
499b7a08d04SJames Feist                             BMCWEB_LOG_ERROR << "Field Illegal "
500b7a08d04SJames Feist                                              << propertyPair.first;
501b7a08d04SJames Feist                             messages::internalError(asyncResp->res);
502b7a08d04SJames Feist                             return;
503b7a08d04SJames Feist                         }
504b7a08d04SJames Feist                         (*config)[propertyPair.first] = *ptr;
505b7a08d04SJames Feist                     }
506b7a08d04SJames Feist 
507b7a08d04SJames Feist                     if (intfPair.first == stepwiseConfigurationIface)
508b7a08d04SJames Feist                     {
509b7a08d04SJames Feist                         if (propertyPair.first == "Reading" ||
510b7a08d04SJames Feist                             propertyPair.first == "Output")
511b7a08d04SJames Feist                         {
512b7a08d04SJames Feist                             const std::vector<double>* ptr =
513abf2add6SEd Tanous                                 std::get_if<std::vector<double>>(
514b7a08d04SJames Feist                                     &propertyPair.second);
515b7a08d04SJames Feist 
516b7a08d04SJames Feist                             if (ptr == nullptr)
517b7a08d04SJames Feist                             {
518b7a08d04SJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
519b7a08d04SJames Feist                                                  << propertyPair.first;
520b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
521b7a08d04SJames Feist                                 return;
522b7a08d04SJames Feist                             }
523b7a08d04SJames Feist 
524b7a08d04SJames Feist                             if (propertyPair.first == "Reading")
525b7a08d04SJames Feist                             {
526b7a08d04SJames Feist                                 keys = ptr;
527b7a08d04SJames Feist                             }
528b7a08d04SJames Feist                             else
529b7a08d04SJames Feist                             {
530b7a08d04SJames Feist                                 values = ptr;
531b7a08d04SJames Feist                             }
532e662eae8SEd Tanous                             if (keys != nullptr && values != nullptr)
533b7a08d04SJames Feist                             {
534b7a08d04SJames Feist                                 if (keys->size() != values->size())
535b7a08d04SJames Feist                                 {
536b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR
5370fda0f12SGeorge Liu                                         << "Reading and Output size don't match ";
538b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
539b7a08d04SJames Feist                                     return;
540b7a08d04SJames Feist                                 }
541b7a08d04SJames Feist                                 nlohmann::json& steps = (*config)["Steps"];
542b7a08d04SJames Feist                                 steps = nlohmann::json::array();
543b7a08d04SJames Feist                                 for (size_t ii = 0; ii < keys->size(); ii++)
544b7a08d04SJames Feist                                 {
5451476687dSEd Tanous                                     nlohmann::json::object_t step;
5461476687dSEd Tanous                                     step["Target"] = (*keys)[ii];
5471476687dSEd Tanous                                     step["Output"] = (*values)[ii];
5481476687dSEd Tanous                                     steps.push_back(std::move(step));
549b7a08d04SJames Feist                                 }
550b7a08d04SJames Feist                             }
551b7a08d04SJames Feist                         }
552b7a08d04SJames Feist                         if (propertyPair.first == "NegativeHysteresis" ||
553b7a08d04SJames Feist                             propertyPair.first == "PositiveHysteresis")
554b7a08d04SJames Feist                         {
555b7a08d04SJames Feist                             const double* ptr =
556abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
557b7a08d04SJames Feist                             if (ptr == nullptr)
558b7a08d04SJames Feist                             {
559b7a08d04SJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
560b7a08d04SJames Feist                                                  << propertyPair.first;
561b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
562b7a08d04SJames Feist                                 return;
563b7a08d04SJames Feist                             }
564b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
565b7a08d04SJames Feist                         }
566b7a08d04SJames Feist                     }
567b7a08d04SJames Feist 
568b7a08d04SJames Feist                     // pid and fans are off the same configuration
569b7a08d04SJames Feist                     if (intfPair.first == pidConfigurationIface ||
570b7a08d04SJames Feist                         intfPair.first == stepwiseConfigurationIface)
571b7a08d04SJames Feist                     {
5725b4aa86bSJames Feist 
5735b4aa86bSJames Feist                         if (propertyPair.first == "Zones")
5745b4aa86bSJames Feist                         {
5755b4aa86bSJames Feist                             const std::vector<std::string>* inputs =
576abf2add6SEd Tanous                                 std::get_if<std::vector<std::string>>(
5771b6b96c5SEd Tanous                                     &propertyPair.second);
5785b4aa86bSJames Feist 
5795b4aa86bSJames Feist                             if (inputs == nullptr)
5805b4aa86bSJames Feist                             {
581002d39b4SEd Tanous                                 BMCWEB_LOG_ERROR << "Zones Pid Field Illegal";
582a08b46ccSJason M. Bills                                 messages::internalError(asyncResp->res);
5835b4aa86bSJames Feist                                 return;
5845b4aa86bSJames Feist                             }
585b7a08d04SJames Feist                             auto& data = (*config)[propertyPair.first];
5865b4aa86bSJames Feist                             data = nlohmann::json::array();
5875b4aa86bSJames Feist                             for (std::string itemCopy : *inputs)
5885b4aa86bSJames Feist                             {
5895b4aa86bSJames Feist                                 dbus::utility::escapePathForDbus(itemCopy);
5901476687dSEd Tanous                                 nlohmann::json::object_t input;
591eddfc437SWilly Tu                                 boost::urls::url managerUrl =
592eddfc437SWilly Tu                                     crow::utility::urlFromPieces(
593eddfc437SWilly Tu                                         "redfish", "v1", "Managers", "bmc");
594eddfc437SWilly Tu                                 managerUrl.set_fragment(
595eddfc437SWilly Tu                                     ("/Oem/OpenBmc/Fan/FanZones"_json_pointer /
596eddfc437SWilly Tu                                      itemCopy)
597eddfc437SWilly Tu                                         .to_string());
598eddfc437SWilly Tu                                 input["@odata.id"] = std::move(managerUrl);
5991476687dSEd Tanous                                 data.push_back(std::move(input));
6005b4aa86bSJames Feist                             }
6015b4aa86bSJames Feist                         }
6025b4aa86bSJames Feist                         // todo(james): may never happen, but this
6035b4aa86bSJames Feist                         // assumes configuration data referenced in the
6045b4aa86bSJames Feist                         // PID config is provided by the same daemon, we
6055b4aa86bSJames Feist                         // could add another loop to cover all cases,
6065b4aa86bSJames Feist                         // but I'm okay kicking this can down the road a
6075b4aa86bSJames Feist                         // bit
6085b4aa86bSJames Feist 
6095b4aa86bSJames Feist                         else if (propertyPair.first == "Inputs" ||
6105b4aa86bSJames Feist                                  propertyPair.first == "Outputs")
6115b4aa86bSJames Feist                         {
612b7a08d04SJames Feist                             auto& data = (*config)[propertyPair.first];
6135b4aa86bSJames Feist                             const std::vector<std::string>* inputs =
614abf2add6SEd Tanous                                 std::get_if<std::vector<std::string>>(
6151b6b96c5SEd Tanous                                     &propertyPair.second);
6165b4aa86bSJames Feist 
6175b4aa86bSJames Feist                             if (inputs == nullptr)
6185b4aa86bSJames Feist                             {
6195b4aa86bSJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
6205b4aa86bSJames Feist                                                  << propertyPair.first;
621f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
6225b4aa86bSJames Feist                                 return;
6235b4aa86bSJames Feist                             }
6245b4aa86bSJames Feist                             data = *inputs;
625b943aaefSJames Feist                         }
626b943aaefSJames Feist                         else if (propertyPair.first == "SetPointOffset")
627b943aaefSJames Feist                         {
628b943aaefSJames Feist                             const std::string* ptr =
629002d39b4SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
630b943aaefSJames Feist 
631b943aaefSJames Feist                             if (ptr == nullptr)
632b943aaefSJames Feist                             {
633b943aaefSJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
634b943aaefSJames Feist                                                  << propertyPair.first;
635b943aaefSJames Feist                                 messages::internalError(asyncResp->res);
636b943aaefSJames Feist                                 return;
637b943aaefSJames Feist                             }
638b943aaefSJames Feist                             // translate from dbus to redfish
639b943aaefSJames Feist                             if (*ptr == "WarningHigh")
640b943aaefSJames Feist                             {
641b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
642b943aaefSJames Feist                                     "UpperThresholdNonCritical";
643b943aaefSJames Feist                             }
644b943aaefSJames Feist                             else if (*ptr == "WarningLow")
645b943aaefSJames Feist                             {
646b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
647b943aaefSJames Feist                                     "LowerThresholdNonCritical";
648b943aaefSJames Feist                             }
649b943aaefSJames Feist                             else if (*ptr == "CriticalHigh")
650b943aaefSJames Feist                             {
651b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
652b943aaefSJames Feist                                     "UpperThresholdCritical";
653b943aaefSJames Feist                             }
654b943aaefSJames Feist                             else if (*ptr == "CriticalLow")
655b943aaefSJames Feist                             {
656b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
657b943aaefSJames Feist                                     "LowerThresholdCritical";
658b943aaefSJames Feist                             }
659b943aaefSJames Feist                             else
660b943aaefSJames Feist                             {
661002d39b4SEd Tanous                                 BMCWEB_LOG_ERROR << "Value Illegal " << *ptr;
662b943aaefSJames Feist                                 messages::internalError(asyncResp->res);
663b943aaefSJames Feist                                 return;
664b943aaefSJames Feist                             }
665b943aaefSJames Feist                         }
666b943aaefSJames Feist                         // doubles
667002d39b4SEd Tanous                         else if (propertyPair.first == "FFGainCoefficient" ||
6685b4aa86bSJames Feist                                  propertyPair.first == "FFOffCoefficient" ||
6695b4aa86bSJames Feist                                  propertyPair.first == "ICoefficient" ||
6705b4aa86bSJames Feist                                  propertyPair.first == "ILimitMax" ||
6715b4aa86bSJames Feist                                  propertyPair.first == "ILimitMin" ||
672002d39b4SEd Tanous                                  propertyPair.first == "PositiveHysteresis" ||
673002d39b4SEd Tanous                                  propertyPair.first == "NegativeHysteresis" ||
6745b4aa86bSJames Feist                                  propertyPair.first == "OutLimitMax" ||
6755b4aa86bSJames Feist                                  propertyPair.first == "OutLimitMin" ||
6765b4aa86bSJames Feist                                  propertyPair.first == "PCoefficient" ||
6777625cb81SJames Feist                                  propertyPair.first == "SetPoint" ||
6785b4aa86bSJames Feist                                  propertyPair.first == "SlewNeg" ||
6795b4aa86bSJames Feist                                  propertyPair.first == "SlewPos")
6805b4aa86bSJames Feist                         {
6815b4aa86bSJames Feist                             const double* ptr =
682abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
6835b4aa86bSJames Feist                             if (ptr == nullptr)
6845b4aa86bSJames Feist                             {
6855b4aa86bSJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
6865b4aa86bSJames Feist                                                  << propertyPair.first;
687f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
6885b4aa86bSJames Feist                                 return;
6895b4aa86bSJames Feist                             }
690b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
6915b4aa86bSJames Feist                         }
6925b4aa86bSJames Feist                     }
6935b4aa86bSJames Feist                 }
6945b4aa86bSJames Feist             }
6955b4aa86bSJames Feist         }
6965b4aa86bSJames Feist         },
6975b4aa86bSJames Feist         connection, path, objectManagerIface, "GetManagedObjects");
6985b4aa86bSJames Feist }
699ca537928SJennifer Lee 
70083ff9ab6SJames Feist enum class CreatePIDRet
70183ff9ab6SJames Feist {
70283ff9ab6SJames Feist     fail,
70383ff9ab6SJames Feist     del,
70483ff9ab6SJames Feist     patch
70583ff9ab6SJames Feist };
70683ff9ab6SJames Feist 
7078d1b46d7Szhanghch05 inline bool
7088d1b46d7Szhanghch05     getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
7095f2caaefSJames Feist                         std::vector<nlohmann::json>& config,
7105f2caaefSJames Feist                         std::vector<std::string>& zones)
7115f2caaefSJames Feist {
712b6baeaa4SJames Feist     if (config.empty())
713b6baeaa4SJames Feist     {
714b6baeaa4SJames Feist         BMCWEB_LOG_ERROR << "Empty Zones";
7151668ce6dSEd Tanous         messages::propertyValueFormatError(response->res, "[]", "Zones");
716b6baeaa4SJames Feist         return false;
717b6baeaa4SJames Feist     }
7185f2caaefSJames Feist     for (auto& odata : config)
7195f2caaefSJames Feist     {
7205f2caaefSJames Feist         std::string path;
7215f2caaefSJames Feist         if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
7225f2caaefSJames Feist                                           path))
7235f2caaefSJames Feist         {
7245f2caaefSJames Feist             return false;
7255f2caaefSJames Feist         }
7265f2caaefSJames Feist         std::string input;
72761adbda3SJames Feist 
72861adbda3SJames Feist         // 8 below comes from
72961adbda3SJames Feist         // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
73061adbda3SJames Feist         //     0    1     2      3    4    5      6     7      8
73161adbda3SJames Feist         if (!dbus::utility::getNthStringFromPath(path, 8, input))
7325f2caaefSJames Feist         {
7335f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Got invalid path " << path;
7345f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Illegal Type Zones";
7355f2caaefSJames Feist             messages::propertyValueFormatError(response->res, odata.dump(),
7365f2caaefSJames Feist                                                "Zones");
7375f2caaefSJames Feist             return false;
7385f2caaefSJames Feist         }
739a170f275SEd Tanous         std::replace(input.begin(), input.end(), '_', ' ');
7405f2caaefSJames Feist         zones.emplace_back(std::move(input));
7415f2caaefSJames Feist     }
7425f2caaefSJames Feist     return true;
7435f2caaefSJames Feist }
7445f2caaefSJames Feist 
745711ac7a9SEd Tanous inline const dbus::utility::ManagedObjectType::value_type*
74673df0db0SJames Feist     findChassis(const dbus::utility::ManagedObjectType& managedObj,
747b6baeaa4SJames Feist                 const std::string& value, std::string& chassis)
748b6baeaa4SJames Feist {
749b6baeaa4SJames Feist     BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
750b6baeaa4SJames Feist 
751a170f275SEd Tanous     std::string escaped = value;
752a170f275SEd Tanous     std::replace(escaped.begin(), escaped.end(), '_', ' ');
753b6baeaa4SJames Feist     escaped = "/" + escaped;
754002d39b4SEd Tanous     auto it = std::find_if(managedObj.begin(), managedObj.end(),
755002d39b4SEd Tanous                            [&escaped](const auto& obj) {
756b6baeaa4SJames Feist         if (boost::algorithm::ends_with(obj.first.str, escaped))
757b6baeaa4SJames Feist         {
758b6baeaa4SJames Feist             BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
759b6baeaa4SJames Feist             return true;
760b6baeaa4SJames Feist         }
761b6baeaa4SJames Feist         return false;
762b6baeaa4SJames Feist     });
763b6baeaa4SJames Feist 
764b6baeaa4SJames Feist     if (it == managedObj.end())
765b6baeaa4SJames Feist     {
76673df0db0SJames Feist         return nullptr;
767b6baeaa4SJames Feist     }
768b6baeaa4SJames Feist     // 5 comes from <chassis-name> being the 5th element
769b6baeaa4SJames Feist     // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
77073df0db0SJames Feist     if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
77173df0db0SJames Feist     {
77273df0db0SJames Feist         return &(*it);
77373df0db0SJames Feist     }
77473df0db0SJames Feist 
77573df0db0SJames Feist     return nullptr;
776b6baeaa4SJames Feist }
777b6baeaa4SJames Feist 
77823a21a1cSEd Tanous inline CreatePIDRet createPidInterface(
7798d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
780b5a76932SEd Tanous     const nlohmann::json::iterator& it, const std::string& path,
78183ff9ab6SJames Feist     const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
782b9d36b47SEd Tanous     dbus::utility::DBusPropertiesMap& output, std::string& chassis,
783b9d36b47SEd Tanous     const std::string& profile)
78483ff9ab6SJames Feist {
78583ff9ab6SJames Feist 
7865f2caaefSJames Feist     // common deleter
787b6baeaa4SJames Feist     if (it.value() == nullptr)
7885f2caaefSJames Feist     {
7895f2caaefSJames Feist         std::string iface;
7905f2caaefSJames Feist         if (type == "PidControllers" || type == "FanControllers")
7915f2caaefSJames Feist         {
7925f2caaefSJames Feist             iface = pidConfigurationIface;
7935f2caaefSJames Feist         }
7945f2caaefSJames Feist         else if (type == "FanZones")
7955f2caaefSJames Feist         {
7965f2caaefSJames Feist             iface = pidZoneConfigurationIface;
7975f2caaefSJames Feist         }
7985f2caaefSJames Feist         else if (type == "StepwiseControllers")
7995f2caaefSJames Feist         {
8005f2caaefSJames Feist             iface = stepwiseConfigurationIface;
8015f2caaefSJames Feist         }
8025f2caaefSJames Feist         else
8035f2caaefSJames Feist         {
804a0744d38SGunnar Mills             BMCWEB_LOG_ERROR << "Illegal Type " << type;
8055f2caaefSJames Feist             messages::propertyUnknown(response->res, type);
8065f2caaefSJames Feist             return CreatePIDRet::fail;
8075f2caaefSJames Feist         }
8086ee7f774SJames Feist 
8096ee7f774SJames Feist         BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
8105f2caaefSJames Feist         // delete interface
8115f2caaefSJames Feist         crow::connections::systemBus->async_method_call(
812*5e7e2dc5SEd Tanous             [response, path](const boost::system::error_code& ec) {
8135f2caaefSJames Feist             if (ec)
8145f2caaefSJames Feist             {
8155f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
8165f2caaefSJames Feist                 messages::internalError(response->res);
817b6baeaa4SJames Feist                 return;
8185f2caaefSJames Feist             }
819b6baeaa4SJames Feist             messages::success(response->res);
8205f2caaefSJames Feist             },
8215f2caaefSJames Feist             "xyz.openbmc_project.EntityManager", path, iface, "Delete");
8225f2caaefSJames Feist         return CreatePIDRet::del;
8235f2caaefSJames Feist     }
8245f2caaefSJames Feist 
825711ac7a9SEd Tanous     const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
826b6baeaa4SJames Feist     if (!createNewObject)
827b6baeaa4SJames Feist     {
828b6baeaa4SJames Feist         // if we aren't creating a new object, we should be able to find it on
829b6baeaa4SJames Feist         // d-bus
83073df0db0SJames Feist         managedItem = findChassis(managedObj, it.key(), chassis);
83173df0db0SJames Feist         if (managedItem == nullptr)
832b6baeaa4SJames Feist         {
833b6baeaa4SJames Feist             BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
834ace85d60SEd Tanous             messages::invalidObject(response->res,
835ace85d60SEd Tanous                                     crow::utility::urlFromPieces(
836ace85d60SEd Tanous                                         "redfish", "v1", "Chassis", chassis));
837b6baeaa4SJames Feist             return CreatePIDRet::fail;
838b6baeaa4SJames Feist         }
839b6baeaa4SJames Feist     }
840b6baeaa4SJames Feist 
84126f6976fSEd Tanous     if (!profile.empty() &&
84273df0db0SJames Feist         (type == "PidControllers" || type == "FanControllers" ||
84373df0db0SJames Feist          type == "StepwiseControllers"))
84473df0db0SJames Feist     {
84573df0db0SJames Feist         if (managedItem == nullptr)
84673df0db0SJames Feist         {
847b9d36b47SEd Tanous             output.emplace_back("Profiles", std::vector<std::string>{profile});
84873df0db0SJames Feist         }
84973df0db0SJames Feist         else
85073df0db0SJames Feist         {
85173df0db0SJames Feist             std::string interface;
85273df0db0SJames Feist             if (type == "StepwiseControllers")
85373df0db0SJames Feist             {
85473df0db0SJames Feist                 interface = stepwiseConfigurationIface;
85573df0db0SJames Feist             }
85673df0db0SJames Feist             else
85773df0db0SJames Feist             {
85873df0db0SJames Feist                 interface = pidConfigurationIface;
85973df0db0SJames Feist             }
860711ac7a9SEd Tanous             bool ifaceFound = false;
861711ac7a9SEd Tanous             for (const auto& iface : managedItem->second)
862711ac7a9SEd Tanous             {
863711ac7a9SEd Tanous                 if (iface.first == interface)
864711ac7a9SEd Tanous                 {
865711ac7a9SEd Tanous                     ifaceFound = true;
866711ac7a9SEd Tanous                     for (const auto& prop : iface.second)
867711ac7a9SEd Tanous                     {
868711ac7a9SEd Tanous                         if (prop.first == "Profiles")
869711ac7a9SEd Tanous                         {
870711ac7a9SEd Tanous                             const std::vector<std::string>* curProfiles =
871711ac7a9SEd Tanous                                 std::get_if<std::vector<std::string>>(
872711ac7a9SEd Tanous                                     &(prop.second));
873711ac7a9SEd Tanous                             if (curProfiles == nullptr)
874711ac7a9SEd Tanous                             {
875711ac7a9SEd Tanous                                 BMCWEB_LOG_ERROR
876711ac7a9SEd Tanous                                     << "Illegal profiles in managed object";
877711ac7a9SEd Tanous                                 messages::internalError(response->res);
878711ac7a9SEd Tanous                                 return CreatePIDRet::fail;
879711ac7a9SEd Tanous                             }
880711ac7a9SEd Tanous                             if (std::find(curProfiles->begin(),
881711ac7a9SEd Tanous                                           curProfiles->end(),
882711ac7a9SEd Tanous                                           profile) == curProfiles->end())
883711ac7a9SEd Tanous                             {
884711ac7a9SEd Tanous                                 std::vector<std::string> newProfiles =
885711ac7a9SEd Tanous                                     *curProfiles;
886711ac7a9SEd Tanous                                 newProfiles.push_back(profile);
887b9d36b47SEd Tanous                                 output.emplace_back("Profiles", newProfiles);
888711ac7a9SEd Tanous                             }
889711ac7a9SEd Tanous                         }
890711ac7a9SEd Tanous                     }
891711ac7a9SEd Tanous                 }
892711ac7a9SEd Tanous             }
893711ac7a9SEd Tanous 
894711ac7a9SEd Tanous             if (!ifaceFound)
89573df0db0SJames Feist             {
89673df0db0SJames Feist                 BMCWEB_LOG_ERROR
89773df0db0SJames Feist                     << "Failed to find interface in managed object";
89873df0db0SJames Feist                 messages::internalError(response->res);
89973df0db0SJames Feist                 return CreatePIDRet::fail;
90073df0db0SJames Feist             }
90173df0db0SJames Feist         }
90273df0db0SJames Feist     }
90373df0db0SJames Feist 
90483ff9ab6SJames Feist     if (type == "PidControllers" || type == "FanControllers")
90583ff9ab6SJames Feist     {
90683ff9ab6SJames Feist         if (createNewObject)
90783ff9ab6SJames Feist         {
908b9d36b47SEd Tanous             output.emplace_back("Class",
909b9d36b47SEd Tanous                                 type == "PidControllers" ? "temp" : "fan");
910b9d36b47SEd Tanous             output.emplace_back("Type", "Pid");
91183ff9ab6SJames Feist         }
9125f2caaefSJames Feist 
9135f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
9145f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
9155f2caaefSJames Feist         std::optional<std::vector<std::string>> outputs;
9165f2caaefSJames Feist         std::map<std::string, std::optional<double>> doubles;
917b943aaefSJames Feist         std::optional<std::string> setpointOffset;
9185f2caaefSJames Feist         if (!redfish::json_util::readJson(
919b6baeaa4SJames Feist                 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
9205f2caaefSJames Feist                 "Zones", zones, "FFGainCoefficient",
9215f2caaefSJames Feist                 doubles["FFGainCoefficient"], "FFOffCoefficient",
9225f2caaefSJames Feist                 doubles["FFOffCoefficient"], "ICoefficient",
9235f2caaefSJames Feist                 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
9245f2caaefSJames Feist                 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
9255f2caaefSJames Feist                 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
9265f2caaefSJames Feist                 "PCoefficient", doubles["PCoefficient"], "SetPoint",
927b943aaefSJames Feist                 doubles["SetPoint"], "SetPointOffset", setpointOffset,
928b943aaefSJames Feist                 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
929b943aaefSJames Feist                 "PositiveHysteresis", doubles["PositiveHysteresis"],
930b943aaefSJames Feist                 "NegativeHysteresis", doubles["NegativeHysteresis"]))
93183ff9ab6SJames Feist         {
93271f52d96SEd Tanous             BMCWEB_LOG_ERROR
93371f52d96SEd Tanous                 << "Illegal Property "
93471f52d96SEd Tanous                 << it.value().dump(2, ' ', true,
93571f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace);
9365f2caaefSJames Feist             return CreatePIDRet::fail;
93783ff9ab6SJames Feist         }
9385f2caaefSJames Feist         if (zones)
9395f2caaefSJames Feist         {
9405f2caaefSJames Feist             std::vector<std::string> zonesStr;
9415f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStr))
9425f2caaefSJames Feist             {
943a0744d38SGunnar Mills                 BMCWEB_LOG_ERROR << "Illegal Zones";
9445f2caaefSJames Feist                 return CreatePIDRet::fail;
9455f2caaefSJames Feist             }
946b6baeaa4SJames Feist             if (chassis.empty() &&
947e662eae8SEd Tanous                 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
948b6baeaa4SJames Feist             {
949b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
950ace85d60SEd Tanous                 messages::invalidObject(
951ace85d60SEd Tanous                     response->res, crow::utility::urlFromPieces(
952ace85d60SEd Tanous                                        "redfish", "v1", "Chassis", chassis));
953b6baeaa4SJames Feist                 return CreatePIDRet::fail;
954b6baeaa4SJames Feist             }
955b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStr));
9565f2caaefSJames Feist         }
957afb9ee06SEd Tanous 
958afb9ee06SEd Tanous         if (inputs)
9595f2caaefSJames Feist         {
960afb9ee06SEd Tanous             for (std::string& value : *inputs)
96183ff9ab6SJames Feist             {
962a170f275SEd Tanous                 std::replace(value.begin(), value.end(), '_', ' ');
96383ff9ab6SJames Feist             }
964afb9ee06SEd Tanous             output.emplace_back("Inputs", *inputs);
965afb9ee06SEd Tanous         }
966afb9ee06SEd Tanous 
967afb9ee06SEd Tanous         if (outputs)
9685f2caaefSJames Feist         {
969afb9ee06SEd Tanous             for (std::string& value : *outputs)
9705f2caaefSJames Feist             {
971afb9ee06SEd Tanous                 std::replace(value.begin(), value.end(), '_', ' ');
9725f2caaefSJames Feist             }
973afb9ee06SEd Tanous             output.emplace_back("Outputs", *outputs);
97483ff9ab6SJames Feist         }
97583ff9ab6SJames Feist 
976b943aaefSJames Feist         if (setpointOffset)
977b943aaefSJames Feist         {
978b943aaefSJames Feist             // translate between redfish and dbus names
979b943aaefSJames Feist             if (*setpointOffset == "UpperThresholdNonCritical")
980b943aaefSJames Feist             {
981b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningLow");
982b943aaefSJames Feist             }
983b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdNonCritical")
984b943aaefSJames Feist             {
985b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningHigh");
986b943aaefSJames Feist             }
987b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdCritical")
988b943aaefSJames Feist             {
989b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalLow");
990b943aaefSJames Feist             }
991b943aaefSJames Feist             else if (*setpointOffset == "UpperThresholdCritical")
992b943aaefSJames Feist             {
993b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalHigh");
994b943aaefSJames Feist             }
995b943aaefSJames Feist             else
996b943aaefSJames Feist             {
997b943aaefSJames Feist                 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
998b943aaefSJames Feist                                  << *setpointOffset;
999ace85d60SEd Tanous                 messages::propertyValueNotInList(response->res, it.key(),
1000ace85d60SEd Tanous                                                  "SetPointOffset");
1001b943aaefSJames Feist                 return CreatePIDRet::fail;
1002b943aaefSJames Feist             }
1003b943aaefSJames Feist         }
1004b943aaefSJames Feist 
100583ff9ab6SJames Feist         // doubles
10065f2caaefSJames Feist         for (const auto& pairs : doubles)
100783ff9ab6SJames Feist         {
10085f2caaefSJames Feist             if (!pairs.second)
100983ff9ab6SJames Feist             {
10105f2caaefSJames Feist                 continue;
101183ff9ab6SJames Feist             }
10125f2caaefSJames Feist             BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
1013b9d36b47SEd Tanous             output.emplace_back(pairs.first, *pairs.second);
10145f2caaefSJames Feist         }
101583ff9ab6SJames Feist     }
101683ff9ab6SJames Feist 
101783ff9ab6SJames Feist     else if (type == "FanZones")
101883ff9ab6SJames Feist     {
1019b9d36b47SEd Tanous         output.emplace_back("Type", "Pid.Zone");
102083ff9ab6SJames Feist 
10215f2caaefSJames Feist         std::optional<nlohmann::json> chassisContainer;
10225f2caaefSJames Feist         std::optional<double> failSafePercent;
1023d3ec07f8SJames Feist         std::optional<double> minThermalOutput;
1024b6baeaa4SJames Feist         if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
10255f2caaefSJames Feist                                           chassisContainer, "FailSafePercent",
1026d3ec07f8SJames Feist                                           failSafePercent, "MinThermalOutput",
1027d3ec07f8SJames Feist                                           minThermalOutput))
102883ff9ab6SJames Feist         {
102971f52d96SEd Tanous             BMCWEB_LOG_ERROR
103071f52d96SEd Tanous                 << "Illegal Property "
103171f52d96SEd Tanous                 << it.value().dump(2, ' ', true,
103271f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace);
103383ff9ab6SJames Feist             return CreatePIDRet::fail;
103483ff9ab6SJames Feist         }
10355f2caaefSJames Feist 
10365f2caaefSJames Feist         if (chassisContainer)
103783ff9ab6SJames Feist         {
10385f2caaefSJames Feist 
10395f2caaefSJames Feist             std::string chassisId;
10405f2caaefSJames Feist             if (!redfish::json_util::readJson(*chassisContainer, response->res,
10415f2caaefSJames Feist                                               "@odata.id", chassisId))
10425f2caaefSJames Feist             {
104371f52d96SEd Tanous                 BMCWEB_LOG_ERROR
104471f52d96SEd Tanous                     << "Illegal Property "
104571f52d96SEd Tanous                     << chassisContainer->dump(
104671f52d96SEd Tanous                            2, ' ', true,
104771f52d96SEd Tanous                            nlohmann::json::error_handler_t::replace);
104883ff9ab6SJames Feist                 return CreatePIDRet::fail;
104983ff9ab6SJames Feist             }
105083ff9ab6SJames Feist 
1051717794d5SAppaRao Puli             // /redfish/v1/chassis/chassis_name/
10525f2caaefSJames Feist             if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
105383ff9ab6SJames Feist             {
10545f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
1055ace85d60SEd Tanous                 messages::invalidObject(
1056ace85d60SEd Tanous                     response->res, crow::utility::urlFromPieces(
1057ace85d60SEd Tanous                                        "redfish", "v1", "Chassis", chassisId));
105883ff9ab6SJames Feist                 return CreatePIDRet::fail;
105983ff9ab6SJames Feist             }
106083ff9ab6SJames Feist         }
1061d3ec07f8SJames Feist         if (minThermalOutput)
106283ff9ab6SJames Feist         {
1063b9d36b47SEd Tanous             output.emplace_back("MinThermalOutput", *minThermalOutput);
10645f2caaefSJames Feist         }
10655f2caaefSJames Feist         if (failSafePercent)
106683ff9ab6SJames Feist         {
1067b9d36b47SEd Tanous             output.emplace_back("FailSafePercent", *failSafePercent);
10685f2caaefSJames Feist         }
10695f2caaefSJames Feist     }
10705f2caaefSJames Feist     else if (type == "StepwiseControllers")
10715f2caaefSJames Feist     {
1072b9d36b47SEd Tanous         output.emplace_back("Type", "Stepwise");
10735f2caaefSJames Feist 
10745f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
10755f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> steps;
10765f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
10775f2caaefSJames Feist         std::optional<double> positiveHysteresis;
10785f2caaefSJames Feist         std::optional<double> negativeHysteresis;
1079c33a90ecSJames Feist         std::optional<std::string> direction; // upper clipping curve vs lower
10805f2caaefSJames Feist         if (!redfish::json_util::readJson(
1081b6baeaa4SJames Feist                 it.value(), response->res, "Zones", zones, "Steps", steps,
1082b6baeaa4SJames Feist                 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
1083c33a90ecSJames Feist                 "NegativeHysteresis", negativeHysteresis, "Direction",
1084c33a90ecSJames Feist                 direction))
10855f2caaefSJames Feist         {
108671f52d96SEd Tanous             BMCWEB_LOG_ERROR
108771f52d96SEd Tanous                 << "Illegal Property "
108871f52d96SEd Tanous                 << it.value().dump(2, ' ', true,
108971f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace);
109083ff9ab6SJames Feist             return CreatePIDRet::fail;
109183ff9ab6SJames Feist         }
10925f2caaefSJames Feist 
10935f2caaefSJames Feist         if (zones)
109483ff9ab6SJames Feist         {
1095b6baeaa4SJames Feist             std::vector<std::string> zonesStrs;
1096b6baeaa4SJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStrs))
10975f2caaefSJames Feist             {
1098a0744d38SGunnar Mills                 BMCWEB_LOG_ERROR << "Illegal Zones";
109983ff9ab6SJames Feist                 return CreatePIDRet::fail;
110083ff9ab6SJames Feist             }
1101b6baeaa4SJames Feist             if (chassis.empty() &&
1102e662eae8SEd Tanous                 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
1103b6baeaa4SJames Feist             {
1104b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
1105ace85d60SEd Tanous                 messages::invalidObject(
1106ace85d60SEd Tanous                     response->res, crow::utility::urlFromPieces(
1107ace85d60SEd Tanous                                        "redfish", "v1", "Chassis", chassis));
1108b6baeaa4SJames Feist                 return CreatePIDRet::fail;
1109b6baeaa4SJames Feist             }
1110b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStrs));
11115f2caaefSJames Feist         }
11125f2caaefSJames Feist         if (steps)
11135f2caaefSJames Feist         {
11145f2caaefSJames Feist             std::vector<double> readings;
11155f2caaefSJames Feist             std::vector<double> outputs;
11165f2caaefSJames Feist             for (auto& step : *steps)
11175f2caaefSJames Feist             {
1118543f4400SEd Tanous                 double target = 0.0;
1119543f4400SEd Tanous                 double out = 0.0;
11205f2caaefSJames Feist 
11215f2caaefSJames Feist                 if (!redfish::json_util::readJson(step, response->res, "Target",
112223a21a1cSEd Tanous                                                   target, "Output", out))
11235f2caaefSJames Feist                 {
112471f52d96SEd Tanous                     BMCWEB_LOG_ERROR
112571f52d96SEd Tanous                         << "Illegal Property "
112671f52d96SEd Tanous                         << it.value().dump(
112771f52d96SEd Tanous                                2, ' ', true,
112871f52d96SEd Tanous                                nlohmann::json::error_handler_t::replace);
11295f2caaefSJames Feist                     return CreatePIDRet::fail;
11305f2caaefSJames Feist                 }
11315f2caaefSJames Feist                 readings.emplace_back(target);
113223a21a1cSEd Tanous                 outputs.emplace_back(out);
11335f2caaefSJames Feist             }
1134b9d36b47SEd Tanous             output.emplace_back("Reading", std::move(readings));
1135b9d36b47SEd Tanous             output.emplace_back("Output", std::move(outputs));
11365f2caaefSJames Feist         }
11375f2caaefSJames Feist         if (inputs)
11385f2caaefSJames Feist         {
11395f2caaefSJames Feist             for (std::string& value : *inputs)
11405f2caaefSJames Feist             {
1141a170f275SEd Tanous 
1142a170f275SEd Tanous                 std::replace(value.begin(), value.end(), '_', ' ');
11435f2caaefSJames Feist             }
1144b9d36b47SEd Tanous             output.emplace_back("Inputs", std::move(*inputs));
11455f2caaefSJames Feist         }
11465f2caaefSJames Feist         if (negativeHysteresis)
11475f2caaefSJames Feist         {
1148b9d36b47SEd Tanous             output.emplace_back("NegativeHysteresis", *negativeHysteresis);
11495f2caaefSJames Feist         }
11505f2caaefSJames Feist         if (positiveHysteresis)
11515f2caaefSJames Feist         {
1152b9d36b47SEd Tanous             output.emplace_back("PositiveHysteresis", *positiveHysteresis);
115383ff9ab6SJames Feist         }
1154c33a90ecSJames Feist         if (direction)
1155c33a90ecSJames Feist         {
1156c33a90ecSJames Feist             constexpr const std::array<const char*, 2> allowedDirections = {
1157c33a90ecSJames Feist                 "Ceiling", "Floor"};
1158c33a90ecSJames Feist             if (std::find(allowedDirections.begin(), allowedDirections.end(),
1159c33a90ecSJames Feist                           *direction) == allowedDirections.end())
1160c33a90ecSJames Feist             {
1161c33a90ecSJames Feist                 messages::propertyValueTypeError(response->res, "Direction",
1162c33a90ecSJames Feist                                                  *direction);
1163c33a90ecSJames Feist                 return CreatePIDRet::fail;
1164c33a90ecSJames Feist             }
1165b9d36b47SEd Tanous             output.emplace_back("Class", *direction);
1166c33a90ecSJames Feist         }
116783ff9ab6SJames Feist     }
116883ff9ab6SJames Feist     else
116983ff9ab6SJames Feist     {
1170a0744d38SGunnar Mills         BMCWEB_LOG_ERROR << "Illegal Type " << type;
117135a62c7cSJason M. Bills         messages::propertyUnknown(response->res, type);
117283ff9ab6SJames Feist         return CreatePIDRet::fail;
117383ff9ab6SJames Feist     }
117483ff9ab6SJames Feist     return CreatePIDRet::patch;
117583ff9ab6SJames Feist }
117673df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
117773df0db0SJames Feist {
11786936afe4SEd Tanous     struct CompletionValues
11796936afe4SEd Tanous     {
11806936afe4SEd Tanous         std::vector<std::string> supportedProfiles;
11816936afe4SEd Tanous         std::string currentProfile;
11826936afe4SEd Tanous         dbus::utility::MapperGetSubTreeResponse subtree;
11836936afe4SEd Tanous     };
118483ff9ab6SJames Feist 
11854e23a444SEd Tanous     explicit GetPIDValues(
11864e23a444SEd Tanous         const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
118723a21a1cSEd Tanous         asyncResp(asyncRespIn)
118873df0db0SJames Feist 
11891214b7e7SGunnar Mills     {}
11909c310685SBorawski.Lukasz 
119173df0db0SJames Feist     void run()
11925b4aa86bSJames Feist     {
119373df0db0SJames Feist         std::shared_ptr<GetPIDValues> self = shared_from_this();
119473df0db0SJames Feist 
119573df0db0SJames Feist         // get all configurations
1196e99073f5SGeorge Liu         constexpr std::array<std::string_view, 4> interfaces = {
1197e99073f5SGeorge Liu             pidConfigurationIface, pidZoneConfigurationIface,
1198e99073f5SGeorge Liu             objectManagerIface, stepwiseConfigurationIface};
1199e99073f5SGeorge Liu         dbus::utility::getSubTree(
1200e99073f5SGeorge Liu             "/", 0, interfaces,
1201b9d36b47SEd Tanous             [self](
1202e99073f5SGeorge Liu                 const boost::system::error_code& ec,
1203b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
12045b4aa86bSJames Feist             if (ec)
12055b4aa86bSJames Feist             {
12065b4aa86bSJames Feist                 BMCWEB_LOG_ERROR << ec;
120773df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
120873df0db0SJames Feist                 return;
120973df0db0SJames Feist             }
12106936afe4SEd Tanous             self->complete.subtree = subtreeLocal;
1211e99073f5SGeorge Liu             });
121273df0db0SJames Feist 
121373df0db0SJames Feist         // at the same time get the selected profile
1214e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1215e99073f5SGeorge Liu             thermalModeIface};
1216e99073f5SGeorge Liu         dbus::utility::getSubTree(
1217e99073f5SGeorge Liu             "/", 0, thermalModeIfaces,
1218b9d36b47SEd Tanous             [self](
1219e99073f5SGeorge Liu                 const boost::system::error_code& ec,
1220b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
122123a21a1cSEd Tanous             if (ec || subtreeLocal.empty())
122273df0db0SJames Feist             {
122373df0db0SJames Feist                 return;
122473df0db0SJames Feist             }
122523a21a1cSEd Tanous             if (subtreeLocal[0].second.size() != 1)
122673df0db0SJames Feist             {
122773df0db0SJames Feist                 // invalid mapper response, should never happen
122873df0db0SJames Feist                 BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
122973df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
12305b4aa86bSJames Feist                 return;
12315b4aa86bSJames Feist             }
12325b4aa86bSJames Feist 
123323a21a1cSEd Tanous             const std::string& path = subtreeLocal[0].first;
123423a21a1cSEd Tanous             const std::string& owner = subtreeLocal[0].second[0].first;
1235fac6e53bSKrzysztof Grobelny 
1236fac6e53bSKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
1237fac6e53bSKrzysztof Grobelny                 *crow::connections::systemBus, owner, path, thermalModeIface,
1238168e20c1SEd Tanous                 [path, owner,
1239*5e7e2dc5SEd Tanous                  self](const boost::system::error_code& ec2,
1240b9d36b47SEd Tanous                        const dbus::utility::DBusPropertiesMap& resp) {
124123a21a1cSEd Tanous                 if (ec2)
124273df0db0SJames Feist                 {
12430fda0f12SGeorge Liu                     BMCWEB_LOG_ERROR
1244002d39b4SEd Tanous                         << "GetPIDValues: Can't get thermalModeIface " << path;
124573df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
124673df0db0SJames Feist                     return;
124773df0db0SJames Feist                 }
1248fac6e53bSKrzysztof Grobelny 
1249271584abSEd Tanous                 const std::string* current = nullptr;
1250271584abSEd Tanous                 const std::vector<std::string>* supported = nullptr;
1251fac6e53bSKrzysztof Grobelny 
1252fac6e53bSKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
1253fac6e53bSKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), resp, "Current", current,
1254fac6e53bSKrzysztof Grobelny                     "Supported", supported);
1255fac6e53bSKrzysztof Grobelny 
1256fac6e53bSKrzysztof Grobelny                 if (!success)
125773df0db0SJames Feist                 {
1258002d39b4SEd Tanous                     messages::internalError(self->asyncResp->res);
125973df0db0SJames Feist                     return;
126073df0db0SJames Feist                 }
1261fac6e53bSKrzysztof Grobelny 
126273df0db0SJames Feist                 if (current == nullptr || supported == nullptr)
126373df0db0SJames Feist                 {
12640fda0f12SGeorge Liu                     BMCWEB_LOG_ERROR
1265002d39b4SEd Tanous                         << "GetPIDValues: thermal mode iface invalid " << path;
126673df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
126773df0db0SJames Feist                     return;
126873df0db0SJames Feist                 }
12696936afe4SEd Tanous                 self->complete.currentProfile = *current;
12706936afe4SEd Tanous                 self->complete.supportedProfiles = *supported;
1271fac6e53bSKrzysztof Grobelny                 });
1272e99073f5SGeorge Liu             });
127373df0db0SJames Feist     }
127473df0db0SJames Feist 
12756936afe4SEd Tanous     static void
12766936afe4SEd Tanous         processingComplete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12776936afe4SEd Tanous                            const CompletionValues& completion)
127873df0db0SJames Feist     {
127973df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
128073df0db0SJames Feist         {
128173df0db0SJames Feist             return;
128273df0db0SJames Feist         }
12835b4aa86bSJames Feist         // create map of <connection, path to objMgr>>
12846936afe4SEd Tanous         boost::container::flat_map<
12856936afe4SEd Tanous             std::string, std::string, std::less<>,
12866936afe4SEd Tanous             std::vector<std::pair<std::string, std::string>>>
12876936afe4SEd Tanous             objectMgrPaths;
12886936afe4SEd Tanous         boost::container::flat_set<std::string, std::less<>,
12896936afe4SEd Tanous                                    std::vector<std::string>>
12906936afe4SEd Tanous             calledConnections;
12916936afe4SEd Tanous         for (const auto& pathGroup : completion.subtree)
12925b4aa86bSJames Feist         {
12935b4aa86bSJames Feist             for (const auto& connectionGroup : pathGroup.second)
12945b4aa86bSJames Feist             {
12956bce33bcSJames Feist                 auto findConnection =
12966bce33bcSJames Feist                     calledConnections.find(connectionGroup.first);
12976bce33bcSJames Feist                 if (findConnection != calledConnections.end())
12986bce33bcSJames Feist                 {
12996bce33bcSJames Feist                     break;
13006bce33bcSJames Feist                 }
130173df0db0SJames Feist                 for (const std::string& interface : connectionGroup.second)
13025b4aa86bSJames Feist                 {
13035b4aa86bSJames Feist                     if (interface == objectManagerIface)
13045b4aa86bSJames Feist                     {
130573df0db0SJames Feist                         objectMgrPaths[connectionGroup.first] = pathGroup.first;
13065b4aa86bSJames Feist                     }
13075b4aa86bSJames Feist                     // this list is alphabetical, so we
13085b4aa86bSJames Feist                     // should have found the objMgr by now
13095b4aa86bSJames Feist                     if (interface == pidConfigurationIface ||
1310b7a08d04SJames Feist                         interface == pidZoneConfigurationIface ||
1311b7a08d04SJames Feist                         interface == stepwiseConfigurationIface)
13125b4aa86bSJames Feist                     {
13135b4aa86bSJames Feist                         auto findObjMgr =
13145b4aa86bSJames Feist                             objectMgrPaths.find(connectionGroup.first);
13155b4aa86bSJames Feist                         if (findObjMgr == objectMgrPaths.end())
13165b4aa86bSJames Feist                         {
13175b4aa86bSJames Feist                             BMCWEB_LOG_DEBUG << connectionGroup.first
13185b4aa86bSJames Feist                                              << "Has no Object Manager";
13195b4aa86bSJames Feist                             continue;
13205b4aa86bSJames Feist                         }
13216bce33bcSJames Feist 
13226bce33bcSJames Feist                         calledConnections.insert(connectionGroup.first);
13236bce33bcSJames Feist 
132473df0db0SJames Feist                         asyncPopulatePid(findObjMgr->first, findObjMgr->second,
13256936afe4SEd Tanous                                          completion.currentProfile,
13266936afe4SEd Tanous                                          completion.supportedProfiles,
132773df0db0SJames Feist                                          asyncResp);
13285b4aa86bSJames Feist                         break;
13295b4aa86bSJames Feist                     }
13305b4aa86bSJames Feist                 }
13315b4aa86bSJames Feist             }
13325b4aa86bSJames Feist         }
133373df0db0SJames Feist     }
133473df0db0SJames Feist 
13356936afe4SEd Tanous     ~GetPIDValues()
13366936afe4SEd Tanous     {
13376936afe4SEd Tanous         boost::asio::post(crow::connections::systemBus->get_io_context(),
13386936afe4SEd Tanous                           std::bind_front(&processingComplete, asyncResp,
13396936afe4SEd Tanous                                           std::move(complete)));
13406936afe4SEd Tanous     }
13416936afe4SEd Tanous 
1342ecd6a3a2SEd Tanous     GetPIDValues(const GetPIDValues&) = delete;
1343ecd6a3a2SEd Tanous     GetPIDValues(GetPIDValues&&) = delete;
1344ecd6a3a2SEd Tanous     GetPIDValues& operator=(const GetPIDValues&) = delete;
1345ecd6a3a2SEd Tanous     GetPIDValues& operator=(GetPIDValues&&) = delete;
1346ecd6a3a2SEd Tanous 
13478d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
13486936afe4SEd Tanous     CompletionValues complete;
134973df0db0SJames Feist };
135073df0db0SJames Feist 
135173df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
135273df0db0SJames Feist {
135373df0db0SJames Feist 
13548d1b46d7Szhanghch05     SetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
135573df0db0SJames Feist                  nlohmann::json& data) :
1356271584abSEd Tanous         asyncResp(asyncRespIn)
135773df0db0SJames Feist     {
135873df0db0SJames Feist 
135973df0db0SJames Feist         std::optional<nlohmann::json> pidControllers;
136073df0db0SJames Feist         std::optional<nlohmann::json> fanControllers;
136173df0db0SJames Feist         std::optional<nlohmann::json> fanZones;
136273df0db0SJames Feist         std::optional<nlohmann::json> stepwiseControllers;
136373df0db0SJames Feist 
136473df0db0SJames Feist         if (!redfish::json_util::readJson(
136573df0db0SJames Feist                 data, asyncResp->res, "PidControllers", pidControllers,
136673df0db0SJames Feist                 "FanControllers", fanControllers, "FanZones", fanZones,
136773df0db0SJames Feist                 "StepwiseControllers", stepwiseControllers, "Profile", profile))
136873df0db0SJames Feist         {
136971f52d96SEd Tanous             BMCWEB_LOG_ERROR
137071f52d96SEd Tanous                 << "Illegal Property "
137171f52d96SEd Tanous                 << data.dump(2, ' ', true,
137271f52d96SEd Tanous                              nlohmann::json::error_handler_t::replace);
137373df0db0SJames Feist             return;
137473df0db0SJames Feist         }
137573df0db0SJames Feist         configuration.emplace_back("PidControllers", std::move(pidControllers));
137673df0db0SJames Feist         configuration.emplace_back("FanControllers", std::move(fanControllers));
137773df0db0SJames Feist         configuration.emplace_back("FanZones", std::move(fanZones));
137873df0db0SJames Feist         configuration.emplace_back("StepwiseControllers",
137973df0db0SJames Feist                                    std::move(stepwiseControllers));
138073df0db0SJames Feist     }
1381ecd6a3a2SEd Tanous 
1382ecd6a3a2SEd Tanous     SetPIDValues(const SetPIDValues&) = delete;
1383ecd6a3a2SEd Tanous     SetPIDValues(SetPIDValues&&) = delete;
1384ecd6a3a2SEd Tanous     SetPIDValues& operator=(const SetPIDValues&) = delete;
1385ecd6a3a2SEd Tanous     SetPIDValues& operator=(SetPIDValues&&) = delete;
1386ecd6a3a2SEd Tanous 
138773df0db0SJames Feist     void run()
138873df0db0SJames Feist     {
138973df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
139073df0db0SJames Feist         {
139173df0db0SJames Feist             return;
139273df0db0SJames Feist         }
139373df0db0SJames Feist 
139473df0db0SJames Feist         std::shared_ptr<SetPIDValues> self = shared_from_this();
139573df0db0SJames Feist 
139673df0db0SJames Feist         // todo(james): might make sense to do a mapper call here if this
139773df0db0SJames Feist         // interface gets more traction
139873df0db0SJames Feist         crow::connections::systemBus->async_method_call(
1399*5e7e2dc5SEd Tanous             [self](const boost::system::error_code& ec,
1400914e2d5dSEd Tanous                    const dbus::utility::ManagedObjectType& mObj) {
140173df0db0SJames Feist             if (ec)
140273df0db0SJames Feist             {
140373df0db0SJames Feist                 BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
140473df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
140573df0db0SJames Feist                 return;
140673df0db0SJames Feist             }
1407e69d9de2SJames Feist             const std::array<const char*, 3> configurations = {
1408e69d9de2SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
1409e69d9de2SJames Feist                 stepwiseConfigurationIface};
1410e69d9de2SJames Feist 
141114b0b8d5SJames Feist             for (const auto& [path, object] : mObj)
1412e69d9de2SJames Feist             {
141314b0b8d5SJames Feist                 for (const auto& [interface, _] : object)
1414e69d9de2SJames Feist                 {
1415002d39b4SEd Tanous                     if (std::find(configurations.begin(), configurations.end(),
1416e69d9de2SJames Feist                                   interface) != configurations.end())
1417e69d9de2SJames Feist                     {
141814b0b8d5SJames Feist                         self->objectCount++;
1419e69d9de2SJames Feist                         break;
1420e69d9de2SJames Feist                     }
1421e69d9de2SJames Feist                 }
1422e69d9de2SJames Feist             }
1423914e2d5dSEd Tanous             self->managedObj = mObj;
142473df0db0SJames Feist             },
1425c106b67aSNan Zhou             "xyz.openbmc_project.EntityManager",
1426c106b67aSNan Zhou             "/xyz/openbmc_project/inventory", objectManagerIface,
142773df0db0SJames Feist             "GetManagedObjects");
142873df0db0SJames Feist 
142973df0db0SJames Feist         // at the same time get the profile information
1430e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1431e99073f5SGeorge Liu             thermalModeIface};
1432e99073f5SGeorge Liu         dbus::utility::getSubTree(
1433e99073f5SGeorge Liu             "/", 0, thermalModeIfaces,
1434e99073f5SGeorge Liu             [self](const boost::system::error_code& ec,
1435b9d36b47SEd Tanous                    const dbus::utility::MapperGetSubTreeResponse& subtree) {
143673df0db0SJames Feist             if (ec || subtree.empty())
143773df0db0SJames Feist             {
143873df0db0SJames Feist                 return;
143973df0db0SJames Feist             }
144073df0db0SJames Feist             if (subtree[0].second.empty())
144173df0db0SJames Feist             {
144273df0db0SJames Feist                 // invalid mapper response, should never happen
144373df0db0SJames Feist                 BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
144473df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
144573df0db0SJames Feist                 return;
144673df0db0SJames Feist             }
144773df0db0SJames Feist 
144873df0db0SJames Feist             const std::string& path = subtree[0].first;
144973df0db0SJames Feist             const std::string& owner = subtree[0].second[0].first;
1450fac6e53bSKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
1451fac6e53bSKrzysztof Grobelny                 *crow::connections::systemBus, owner, path, thermalModeIface,
1452*5e7e2dc5SEd Tanous                 [self, path, owner](const boost::system::error_code& ec2,
1453b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap& r) {
1454cb13a392SEd Tanous                 if (ec2)
145573df0db0SJames Feist                 {
14560fda0f12SGeorge Liu                     BMCWEB_LOG_ERROR
1457002d39b4SEd Tanous                         << "SetPIDValues: Can't get thermalModeIface " << path;
145873df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
145973df0db0SJames Feist                     return;
146073df0db0SJames Feist                 }
1461271584abSEd Tanous                 const std::string* current = nullptr;
1462271584abSEd Tanous                 const std::vector<std::string>* supported = nullptr;
1463fac6e53bSKrzysztof Grobelny 
1464fac6e53bSKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
1465fac6e53bSKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), r, "Current", current,
1466fac6e53bSKrzysztof Grobelny                     "Supported", supported);
1467fac6e53bSKrzysztof Grobelny 
1468fac6e53bSKrzysztof Grobelny                 if (!success)
146973df0db0SJames Feist                 {
1470002d39b4SEd Tanous                     messages::internalError(self->asyncResp->res);
147173df0db0SJames Feist                     return;
147273df0db0SJames Feist                 }
1473fac6e53bSKrzysztof Grobelny 
147473df0db0SJames Feist                 if (current == nullptr || supported == nullptr)
147573df0db0SJames Feist                 {
14760fda0f12SGeorge Liu                     BMCWEB_LOG_ERROR
1477002d39b4SEd Tanous                         << "SetPIDValues: thermal mode iface invalid " << path;
147873df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
147973df0db0SJames Feist                     return;
148073df0db0SJames Feist                 }
148173df0db0SJames Feist                 self->currentProfile = *current;
148273df0db0SJames Feist                 self->supportedProfiles = *supported;
148373df0db0SJames Feist                 self->profileConnection = owner;
148473df0db0SJames Feist                 self->profilePath = path;
1485fac6e53bSKrzysztof Grobelny                 });
1486e99073f5SGeorge Liu             });
148773df0db0SJames Feist     }
148824b2fe81SEd Tanous     void pidSetDone()
148973df0db0SJames Feist     {
149073df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
149173df0db0SJames Feist         {
149273df0db0SJames Feist             return;
14935b4aa86bSJames Feist         }
14948d1b46d7Szhanghch05         std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
149573df0db0SJames Feist         if (profile)
149673df0db0SJames Feist         {
149773df0db0SJames Feist             if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
149873df0db0SJames Feist                           *profile) == supportedProfiles.end())
149973df0db0SJames Feist             {
150073df0db0SJames Feist                 messages::actionParameterUnknown(response->res, "Profile",
150173df0db0SJames Feist                                                  *profile);
150273df0db0SJames Feist                 return;
150373df0db0SJames Feist             }
150473df0db0SJames Feist             currentProfile = *profile;
150573df0db0SJames Feist             crow::connections::systemBus->async_method_call(
1506*5e7e2dc5SEd Tanous                 [response](const boost::system::error_code& ec) {
150773df0db0SJames Feist                 if (ec)
150873df0db0SJames Feist                 {
150973df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Error patching profile" << ec;
151073df0db0SJames Feist                     messages::internalError(response->res);
151173df0db0SJames Feist                 }
151273df0db0SJames Feist                 },
151373df0db0SJames Feist                 profileConnection, profilePath,
151473df0db0SJames Feist                 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
1515168e20c1SEd Tanous                 "Current", dbus::utility::DbusVariantType(*profile));
151673df0db0SJames Feist         }
151773df0db0SJames Feist 
151873df0db0SJames Feist         for (auto& containerPair : configuration)
151973df0db0SJames Feist         {
152073df0db0SJames Feist             auto& container = containerPair.second;
152173df0db0SJames Feist             if (!container)
152273df0db0SJames Feist             {
152373df0db0SJames Feist                 continue;
152473df0db0SJames Feist             }
15256ee7f774SJames Feist             BMCWEB_LOG_DEBUG << *container;
15266ee7f774SJames Feist 
152702cad96eSEd Tanous             const std::string& type = containerPair.first;
152873df0db0SJames Feist 
152973df0db0SJames Feist             for (nlohmann::json::iterator it = container->begin();
153017a897dfSManojkiran Eda                  it != container->end(); ++it)
153173df0db0SJames Feist             {
153273df0db0SJames Feist                 const auto& name = it.key();
15336ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "looking for " << name;
15346ee7f774SJames Feist 
153573df0db0SJames Feist                 auto pathItr =
153673df0db0SJames Feist                     std::find_if(managedObj.begin(), managedObj.end(),
153773df0db0SJames Feist                                  [&name](const auto& obj) {
1538002d39b4SEd Tanous                     return boost::algorithm::ends_with(obj.first.str,
1539002d39b4SEd Tanous                                                        "/" + name);
154073df0db0SJames Feist                     });
1541b9d36b47SEd Tanous                 dbus::utility::DBusPropertiesMap output;
154273df0db0SJames Feist 
154373df0db0SJames Feist                 output.reserve(16); // The pid interface length
154473df0db0SJames Feist 
154573df0db0SJames Feist                 // determines if we're patching entity-manager or
154673df0db0SJames Feist                 // creating a new object
154773df0db0SJames Feist                 bool createNewObject = (pathItr == managedObj.end());
15486ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
15496ee7f774SJames Feist 
155073df0db0SJames Feist                 std::string iface;
1551ea2b670dSEd Tanous                 if (!createNewObject)
1552ea2b670dSEd Tanous                 {
15538be2b5b6SPotin Lai                     bool findInterface = false;
1554ea2b670dSEd Tanous                     for (const auto& interface : pathItr->second)
1555ea2b670dSEd Tanous                     {
1556ea2b670dSEd Tanous                         if (interface.first == pidConfigurationIface)
1557ea2b670dSEd Tanous                         {
1558ea2b670dSEd Tanous                             if (type == "PidControllers" ||
1559ea2b670dSEd Tanous                                 type == "FanControllers")
156073df0db0SJames Feist                             {
156173df0db0SJames Feist                                 iface = pidConfigurationIface;
15628be2b5b6SPotin Lai                                 findInterface = true;
15638be2b5b6SPotin Lai                                 break;
156473df0db0SJames Feist                             }
156573df0db0SJames Feist                         }
1566ea2b670dSEd Tanous                         else if (interface.first == pidZoneConfigurationIface)
156773df0db0SJames Feist                         {
1568ea2b670dSEd Tanous                             if (type == "FanZones")
156973df0db0SJames Feist                             {
1570ea2b670dSEd Tanous                                 iface = pidConfigurationIface;
15718be2b5b6SPotin Lai                                 findInterface = true;
15728be2b5b6SPotin Lai                                 break;
157373df0db0SJames Feist                             }
157473df0db0SJames Feist                         }
1575ea2b670dSEd Tanous                         else if (interface.first == stepwiseConfigurationIface)
1576ea2b670dSEd Tanous                         {
1577ea2b670dSEd Tanous                             if (type == "StepwiseControllers")
157873df0db0SJames Feist                             {
157973df0db0SJames Feist                                 iface = stepwiseConfigurationIface;
15808be2b5b6SPotin Lai                                 findInterface = true;
15818be2b5b6SPotin Lai                                 break;
15828be2b5b6SPotin Lai                             }
15838be2b5b6SPotin Lai                         }
15848be2b5b6SPotin Lai                     }
15858be2b5b6SPotin Lai 
15868be2b5b6SPotin Lai                     // create new object if interface not found
15878be2b5b6SPotin Lai                     if (!findInterface)
15888be2b5b6SPotin Lai                     {
158973df0db0SJames Feist                         createNewObject = true;
159073df0db0SJames Feist                     }
1591ea2b670dSEd Tanous                 }
15926ee7f774SJames Feist 
15936ee7f774SJames Feist                 if (createNewObject && it.value() == nullptr)
15946ee7f774SJames Feist                 {
15954e0453b1SGunnar Mills                     // can't delete a non-existent object
15961668ce6dSEd Tanous                     messages::propertyValueNotInList(response->res,
15971668ce6dSEd Tanous                                                      it.value().dump(), name);
15986ee7f774SJames Feist                     continue;
15996ee7f774SJames Feist                 }
16006ee7f774SJames Feist 
16016ee7f774SJames Feist                 std::string path;
16026ee7f774SJames Feist                 if (pathItr != managedObj.end())
16036ee7f774SJames Feist                 {
16046ee7f774SJames Feist                     path = pathItr->first.str;
16056ee7f774SJames Feist                 }
16066ee7f774SJames Feist 
160773df0db0SJames Feist                 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
1608e69d9de2SJames Feist 
1609e69d9de2SJames Feist                 // arbitrary limit to avoid attacks
1610e69d9de2SJames Feist                 constexpr const size_t controllerLimit = 500;
161114b0b8d5SJames Feist                 if (createNewObject && objectCount >= controllerLimit)
1612e69d9de2SJames Feist                 {
1613e69d9de2SJames Feist                     messages::resourceExhaustion(response->res, type);
1614e69d9de2SJames Feist                     continue;
1615e69d9de2SJames Feist                 }
1616a170f275SEd Tanous                 std::string escaped = name;
1617a170f275SEd Tanous                 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1618a170f275SEd Tanous                 output.emplace_back("Name", escaped);
161973df0db0SJames Feist 
162073df0db0SJames Feist                 std::string chassis;
162173df0db0SJames Feist                 CreatePIDRet ret = createPidInterface(
16226ee7f774SJames Feist                     response, type, it, path, managedObj, createNewObject,
16236ee7f774SJames Feist                     output, chassis, currentProfile);
162473df0db0SJames Feist                 if (ret == CreatePIDRet::fail)
162573df0db0SJames Feist                 {
162673df0db0SJames Feist                     return;
162773df0db0SJames Feist                 }
16283174e4dfSEd Tanous                 if (ret == CreatePIDRet::del)
162973df0db0SJames Feist                 {
163073df0db0SJames Feist                     continue;
163173df0db0SJames Feist                 }
163273df0db0SJames Feist 
163373df0db0SJames Feist                 if (!createNewObject)
163473df0db0SJames Feist                 {
163573df0db0SJames Feist                     for (const auto& property : output)
163673df0db0SJames Feist                     {
163773df0db0SJames Feist                         crow::connections::systemBus->async_method_call(
163873df0db0SJames Feist                             [response,
163973df0db0SJames Feist                              propertyName{std::string(property.first)}](
1640*5e7e2dc5SEd Tanous                                 const boost::system::error_code& ec) {
164173df0db0SJames Feist                             if (ec)
164273df0db0SJames Feist                             {
164373df0db0SJames Feist                                 BMCWEB_LOG_ERROR << "Error patching "
1644002d39b4SEd Tanous                                                  << propertyName << ": " << ec;
164573df0db0SJames Feist                                 messages::internalError(response->res);
164673df0db0SJames Feist                                 return;
164773df0db0SJames Feist                             }
164873df0db0SJames Feist                             messages::success(response->res);
164973df0db0SJames Feist                             },
16506ee7f774SJames Feist                             "xyz.openbmc_project.EntityManager", path,
165173df0db0SJames Feist                             "org.freedesktop.DBus.Properties", "Set", iface,
165273df0db0SJames Feist                             property.first, property.second);
165373df0db0SJames Feist                     }
165473df0db0SJames Feist                 }
165573df0db0SJames Feist                 else
165673df0db0SJames Feist                 {
165773df0db0SJames Feist                     if (chassis.empty())
165873df0db0SJames Feist                     {
165973df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to get chassis from config";
1660ace85d60SEd Tanous                         messages::internalError(response->res);
166173df0db0SJames Feist                         return;
166273df0db0SJames Feist                     }
166373df0db0SJames Feist 
166473df0db0SJames Feist                     bool foundChassis = false;
166573df0db0SJames Feist                     for (const auto& obj : managedObj)
166673df0db0SJames Feist                     {
166773df0db0SJames Feist                         if (boost::algorithm::ends_with(obj.first.str, chassis))
166873df0db0SJames Feist                         {
166973df0db0SJames Feist                             chassis = obj.first.str;
167073df0db0SJames Feist                             foundChassis = true;
167173df0db0SJames Feist                             break;
167273df0db0SJames Feist                         }
167373df0db0SJames Feist                     }
167473df0db0SJames Feist                     if (!foundChassis)
167573df0db0SJames Feist                     {
167673df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
167773df0db0SJames Feist                         messages::resourceMissingAtURI(
1678ace85d60SEd Tanous                             response->res,
1679ace85d60SEd Tanous                             crow::utility::urlFromPieces("redfish", "v1",
1680ace85d60SEd Tanous                                                          "Chassis", chassis));
168173df0db0SJames Feist                         return;
168273df0db0SJames Feist                     }
168373df0db0SJames Feist 
168473df0db0SJames Feist                     crow::connections::systemBus->async_method_call(
1685*5e7e2dc5SEd Tanous                         [response](const boost::system::error_code& ec) {
168673df0db0SJames Feist                         if (ec)
168773df0db0SJames Feist                         {
168873df0db0SJames Feist                             BMCWEB_LOG_ERROR << "Error Adding Pid Object "
168973df0db0SJames Feist                                              << ec;
169073df0db0SJames Feist                             messages::internalError(response->res);
169173df0db0SJames Feist                             return;
169273df0db0SJames Feist                         }
169373df0db0SJames Feist                         messages::success(response->res);
169473df0db0SJames Feist                         },
169573df0db0SJames Feist                         "xyz.openbmc_project.EntityManager", chassis,
169673df0db0SJames Feist                         "xyz.openbmc_project.AddObject", "AddObject", output);
169773df0db0SJames Feist                 }
169873df0db0SJames Feist             }
169973df0db0SJames Feist         }
170073df0db0SJames Feist     }
170124b2fe81SEd Tanous 
170224b2fe81SEd Tanous     ~SetPIDValues()
170324b2fe81SEd Tanous     {
170424b2fe81SEd Tanous         try
170524b2fe81SEd Tanous         {
170624b2fe81SEd Tanous             pidSetDone();
170724b2fe81SEd Tanous         }
170824b2fe81SEd Tanous         catch (...)
170924b2fe81SEd Tanous         {
171024b2fe81SEd Tanous             BMCWEB_LOG_CRITICAL << "pidSetDone threw exception";
171124b2fe81SEd Tanous         }
171224b2fe81SEd Tanous     }
171324b2fe81SEd Tanous 
17148d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
171573df0db0SJames Feist     std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
171673df0db0SJames Feist         configuration;
171773df0db0SJames Feist     std::optional<std::string> profile;
171873df0db0SJames Feist     dbus::utility::ManagedObjectType managedObj;
171973df0db0SJames Feist     std::vector<std::string> supportedProfiles;
172073df0db0SJames Feist     std::string currentProfile;
172173df0db0SJames Feist     std::string profileConnection;
172273df0db0SJames Feist     std::string profilePath;
172314b0b8d5SJames Feist     size_t objectCount = 0;
172473df0db0SJames Feist };
172573df0db0SJames Feist 
1726071d8fdfSSunnySrivastava1984 /**
1727071d8fdfSSunnySrivastava1984  * @brief Retrieves BMC manager location data over DBus
1728071d8fdfSSunnySrivastava1984  *
1729071d8fdfSSunnySrivastava1984  * @param[in] aResp Shared pointer for completing asynchronous calls
1730071d8fdfSSunnySrivastava1984  * @param[in] connectionName - service name
1731071d8fdfSSunnySrivastava1984  * @param[in] path - object path
1732071d8fdfSSunnySrivastava1984  * @return none
1733071d8fdfSSunnySrivastava1984  */
17348d1b46d7Szhanghch05 inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1735071d8fdfSSunnySrivastava1984                         const std::string& connectionName,
1736071d8fdfSSunnySrivastava1984                         const std::string& path)
1737071d8fdfSSunnySrivastava1984 {
1738071d8fdfSSunnySrivastava1984     BMCWEB_LOG_DEBUG << "Get BMC manager Location data.";
1739071d8fdfSSunnySrivastava1984 
17401e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
17411e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
17421e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
1743*5e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec,
17441e1e598dSJonathan Doman                 const std::string& property) {
1745071d8fdfSSunnySrivastava1984         if (ec)
1746071d8fdfSSunnySrivastava1984         {
1747071d8fdfSSunnySrivastava1984             BMCWEB_LOG_DEBUG << "DBUS response error for "
1748071d8fdfSSunnySrivastava1984                                 "Location";
1749071d8fdfSSunnySrivastava1984             messages::internalError(aResp->res);
1750071d8fdfSSunnySrivastava1984             return;
1751071d8fdfSSunnySrivastava1984         }
1752071d8fdfSSunnySrivastava1984 
1753071d8fdfSSunnySrivastava1984         aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
17541e1e598dSJonathan Doman             property;
17551e1e598dSJonathan Doman         });
1756071d8fdfSSunnySrivastava1984 }
17577e860f15SJohn Edward Broadbent // avoid name collision systems.hpp
17587e860f15SJohn Edward Broadbent inline void
17597e860f15SJohn Edward Broadbent     managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
17604bf2b033SGunnar Mills {
17614bf2b033SGunnar Mills     BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time";
17624bf2b033SGunnar Mills 
17631e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint64_t>(
17641e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
17651e1e598dSJonathan Doman         "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
17661e1e598dSJonathan Doman         "LastRebootTime",
1767*5e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec,
17681e1e598dSJonathan Doman                 const uint64_t lastResetTime) {
17694bf2b033SGunnar Mills         if (ec)
17704bf2b033SGunnar Mills         {
17714bf2b033SGunnar Mills             BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
17724bf2b033SGunnar Mills             return;
17734bf2b033SGunnar Mills         }
17744bf2b033SGunnar Mills 
17754bf2b033SGunnar Mills         // LastRebootTime is epoch time, in milliseconds
17764bf2b033SGunnar Mills         // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
17771e1e598dSJonathan Doman         uint64_t lastResetTimeStamp = lastResetTime / 1000;
17784bf2b033SGunnar Mills 
17794bf2b033SGunnar Mills         // Convert to ISO 8601 standard
17804bf2b033SGunnar Mills         aResp->res.jsonValue["LastResetTime"] =
17812b82937eSEd Tanous             redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
17821e1e598dSJonathan Doman         });
17834bf2b033SGunnar Mills }
17844bf2b033SGunnar Mills 
17854bfefa74SGunnar Mills /**
17864bfefa74SGunnar Mills  * @brief Set the running firmware image
17874bfefa74SGunnar Mills  *
17884bfefa74SGunnar Mills  * @param[i,o] aResp - Async response object
17894bfefa74SGunnar Mills  * @param[i] runningFirmwareTarget - Image to make the running image
17904bfefa74SGunnar Mills  *
17914bfefa74SGunnar Mills  * @return void
17924bfefa74SGunnar Mills  */
17937e860f15SJohn Edward Broadbent inline void
17947e860f15SJohn Edward Broadbent     setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1795f23b7296SEd Tanous                            const std::string& runningFirmwareTarget)
17964bfefa74SGunnar Mills {
17974bfefa74SGunnar Mills     // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1798f23b7296SEd Tanous     std::string::size_type idPos = runningFirmwareTarget.rfind('/');
17994bfefa74SGunnar Mills     if (idPos == std::string::npos)
18004bfefa74SGunnar Mills     {
18014bfefa74SGunnar Mills         messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
18024bfefa74SGunnar Mills                                          "@odata.id");
18034bfefa74SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't parse firmware ID!";
18044bfefa74SGunnar Mills         return;
18054bfefa74SGunnar Mills     }
18064bfefa74SGunnar Mills     idPos++;
18074bfefa74SGunnar Mills     if (idPos >= runningFirmwareTarget.size())
18084bfefa74SGunnar Mills     {
18094bfefa74SGunnar Mills         messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
18104bfefa74SGunnar Mills                                          "@odata.id");
18114bfefa74SGunnar Mills         BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
18124bfefa74SGunnar Mills         return;
18134bfefa74SGunnar Mills     }
18144bfefa74SGunnar Mills     std::string firmwareId = runningFirmwareTarget.substr(idPos);
18154bfefa74SGunnar Mills 
18164bfefa74SGunnar Mills     // Make sure the image is valid before setting priority
18174bfefa74SGunnar Mills     crow::connections::systemBus->async_method_call(
1818711ac7a9SEd Tanous         [aResp, firmwareId,
1819*5e7e2dc5SEd Tanous          runningFirmwareTarget](const boost::system::error_code& ec,
1820711ac7a9SEd Tanous                                 dbus::utility::ManagedObjectType& subtree) {
18214bfefa74SGunnar Mills         if (ec)
18224bfefa74SGunnar Mills         {
18234bfefa74SGunnar Mills             BMCWEB_LOG_DEBUG << "D-Bus response error getting objects.";
18244bfefa74SGunnar Mills             messages::internalError(aResp->res);
18254bfefa74SGunnar Mills             return;
18264bfefa74SGunnar Mills         }
18274bfefa74SGunnar Mills 
182826f6976fSEd Tanous         if (subtree.empty())
18294bfefa74SGunnar Mills         {
18304bfefa74SGunnar Mills             BMCWEB_LOG_DEBUG << "Can't find image!";
18314bfefa74SGunnar Mills             messages::internalError(aResp->res);
18324bfefa74SGunnar Mills             return;
18334bfefa74SGunnar Mills         }
18344bfefa74SGunnar Mills 
18354bfefa74SGunnar Mills         bool foundImage = false;
183602cad96eSEd Tanous         for (const auto& object : subtree)
18374bfefa74SGunnar Mills         {
18384bfefa74SGunnar Mills             const std::string& path =
18394bfefa74SGunnar Mills                 static_cast<const std::string&>(object.first);
1840f23b7296SEd Tanous             std::size_t idPos2 = path.rfind('/');
18414bfefa74SGunnar Mills 
18424bfefa74SGunnar Mills             if (idPos2 == std::string::npos)
18434bfefa74SGunnar Mills             {
18444bfefa74SGunnar Mills                 continue;
18454bfefa74SGunnar Mills             }
18464bfefa74SGunnar Mills 
18474bfefa74SGunnar Mills             idPos2++;
18484bfefa74SGunnar Mills             if (idPos2 >= path.size())
18494bfefa74SGunnar Mills             {
18504bfefa74SGunnar Mills                 continue;
18514bfefa74SGunnar Mills             }
18524bfefa74SGunnar Mills 
18534bfefa74SGunnar Mills             if (path.substr(idPos2) == firmwareId)
18544bfefa74SGunnar Mills             {
18554bfefa74SGunnar Mills                 foundImage = true;
18564bfefa74SGunnar Mills                 break;
18574bfefa74SGunnar Mills             }
18584bfefa74SGunnar Mills         }
18594bfefa74SGunnar Mills 
18604bfefa74SGunnar Mills         if (!foundImage)
18614bfefa74SGunnar Mills         {
1862002d39b4SEd Tanous             messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
1863002d39b4SEd Tanous                                              "@odata.id");
18644bfefa74SGunnar Mills             BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
18654bfefa74SGunnar Mills             return;
18664bfefa74SGunnar Mills         }
18674bfefa74SGunnar Mills 
18688cc8edecSEd Tanous         BMCWEB_LOG_DEBUG << "Setting firmware version " << firmwareId
18698cc8edecSEd Tanous                          << " to priority 0.";
18704bfefa74SGunnar Mills 
18714bfefa74SGunnar Mills         // Only support Immediate
18724bfefa74SGunnar Mills         // An addition could be a Redfish Setting like
18734bfefa74SGunnar Mills         // ActiveSoftwareImageApplyTime and support OnReset
18744bfefa74SGunnar Mills         crow::connections::systemBus->async_method_call(
1875*5e7e2dc5SEd Tanous             [aResp](const boost::system::error_code& ec2) {
18768a592810SEd Tanous             if (ec2)
18774bfefa74SGunnar Mills             {
18784bfefa74SGunnar Mills                 BMCWEB_LOG_DEBUG << "D-Bus response error setting.";
18794bfefa74SGunnar Mills                 messages::internalError(aResp->res);
18804bfefa74SGunnar Mills                 return;
18814bfefa74SGunnar Mills             }
18824bfefa74SGunnar Mills             doBMCGracefulRestart(aResp);
18834bfefa74SGunnar Mills             },
18844bfefa74SGunnar Mills 
18854bfefa74SGunnar Mills             "xyz.openbmc_project.Software.BMC.Updater",
18864bfefa74SGunnar Mills             "/xyz/openbmc_project/software/" + firmwareId,
18874bfefa74SGunnar Mills             "org.freedesktop.DBus.Properties", "Set",
18887e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1889168e20c1SEd Tanous             dbus::utility::DbusVariantType(static_cast<uint8_t>(0)));
18904bfefa74SGunnar Mills         },
18914bfefa74SGunnar Mills         "xyz.openbmc_project.Software.BMC.Updater",
18927e860f15SJohn Edward Broadbent         "/xyz/openbmc_project/software", "org.freedesktop.DBus.ObjectManager",
18937e860f15SJohn Edward Broadbent         "GetManagedObjects");
18944bfefa74SGunnar Mills }
18954bfefa74SGunnar Mills 
18967e860f15SJohn Edward Broadbent inline void setDateTime(std::shared_ptr<bmcweb::AsyncResp> aResp,
18977e860f15SJohn Edward Broadbent                         std::string datetime)
1898af5d6058SSantosh Puranik {
1899af5d6058SSantosh Puranik     BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
1900af5d6058SSantosh Puranik 
1901c2e32007SEd Tanous     std::optional<redfish::time_utils::usSinceEpoch> us =
1902c2e32007SEd Tanous         redfish::time_utils::dateStringToEpoch(datetime);
1903c2e32007SEd Tanous     if (!us)
1904af5d6058SSantosh Puranik     {
1905c2e32007SEd Tanous         messages::propertyValueFormatError(aResp->res, datetime, "DateTime");
1906c2e32007SEd Tanous         return;
1907c2e32007SEd Tanous     }
1908af5d6058SSantosh Puranik     crow::connections::systemBus->async_method_call(
1909c2e32007SEd Tanous         [aResp{std::move(aResp)},
1910*5e7e2dc5SEd Tanous          datetime{std::move(datetime)}](const boost::system::error_code& ec) {
1911af5d6058SSantosh Puranik         if (ec)
1912af5d6058SSantosh Puranik         {
1913af5d6058SSantosh Puranik             BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1914af5d6058SSantosh Puranik                                 "DBUS response error "
1915af5d6058SSantosh Puranik                              << ec;
1916af5d6058SSantosh Puranik             messages::internalError(aResp->res);
1917af5d6058SSantosh Puranik             return;
1918af5d6058SSantosh Puranik         }
1919af5d6058SSantosh Puranik         aResp->res.jsonValue["DateTime"] = datetime;
1920af5d6058SSantosh Puranik         },
19217e860f15SJohn Edward Broadbent         "xyz.openbmc_project.Time.Manager", "/xyz/openbmc_project/time/bmc",
1922af5d6058SSantosh Puranik         "org.freedesktop.DBus.Properties", "Set",
1923af5d6058SSantosh Puranik         "xyz.openbmc_project.Time.EpochTime", "Elapsed",
1924c2e32007SEd Tanous         dbus::utility::DbusVariantType(us->count()));
192583ff9ab6SJames Feist }
19269c310685SBorawski.Lukasz 
19277e860f15SJohn Edward Broadbent inline void requestRoutesManager(App& app)
19287e860f15SJohn Edward Broadbent {
19297e860f15SJohn Edward Broadbent     std::string uuid = persistent_data::getConfig().systemUuid;
19309c310685SBorawski.Lukasz 
19317e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
1932ed398213SEd Tanous         .privileges(redfish::privileges::getManager)
1933002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1934002d39b4SEd Tanous             [&app, uuid](const crow::Request& req,
193545ca1b86SEd Tanous                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
19363ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
193745ca1b86SEd Tanous         {
193845ca1b86SEd Tanous             return;
193945ca1b86SEd Tanous         }
19407e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
1941a51fc2d2SSui Chen         asyncResp->res.jsonValue["@odata.type"] = "#Manager.v1_14_0.Manager";
19427e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["Id"] = "bmc";
19437e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
19447e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["Description"] =
19457e860f15SJohn Edward Broadbent             "Baseboard Management Controller";
19467e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["PowerState"] = "On";
19471476687dSEd Tanous         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
19481476687dSEd Tanous         asyncResp->res.jsonValue["Status"]["Health"] = "OK";
19491476687dSEd Tanous 
19507e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["ManagerType"] = "BMC";
19517e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
19527e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1953002d39b4SEd Tanous         asyncResp->res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
19547e860f15SJohn Edward Broadbent 
19551476687dSEd Tanous         asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
19561476687dSEd Tanous             "/redfish/v1/Managers/bmc/LogServices";
19571476687dSEd Tanous         asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
19581476687dSEd Tanous             "/redfish/v1/Managers/bmc/NetworkProtocol";
19591476687dSEd Tanous         asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
19601476687dSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
19617e860f15SJohn Edward Broadbent 
19627e860f15SJohn Edward Broadbent #ifdef BMCWEB_ENABLE_VM_NBDPROXY
19631476687dSEd Tanous         asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
19641476687dSEd Tanous             "/redfish/v1/Managers/bmc/VirtualMedia";
19657e860f15SJohn Edward Broadbent #endif // BMCWEB_ENABLE_VM_NBDPROXY
19667e860f15SJohn Edward Broadbent 
19677e860f15SJohn Edward Broadbent         // default oem data
19687e860f15SJohn Edward Broadbent         nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
19697e860f15SJohn Edward Broadbent         nlohmann::json& oemOpenbmc = oem["OpenBmc"];
19707e860f15SJohn Edward Broadbent         oem["@odata.type"] = "#OemManager.Oem";
19717e860f15SJohn Edward Broadbent         oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
19727e860f15SJohn Edward Broadbent         oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
19737e860f15SJohn Edward Broadbent         oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
19741476687dSEd Tanous 
19751476687dSEd Tanous         nlohmann::json::object_t certificates;
19761476687dSEd Tanous         certificates["@odata.id"] =
19771476687dSEd Tanous             "/redfish/v1/Managers/bmc/Truststore/Certificates";
19781476687dSEd Tanous         oemOpenbmc["Certificates"] = std::move(certificates);
19797e860f15SJohn Edward Broadbent 
19807e860f15SJohn Edward Broadbent         // Manager.Reset (an action) can be many values, OpenBMC only
19817e860f15SJohn Edward Broadbent         // supports BMC reboot.
19827e860f15SJohn Edward Broadbent         nlohmann::json& managerReset =
19837e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
19847e860f15SJohn Edward Broadbent         managerReset["target"] =
19857e860f15SJohn Edward Broadbent             "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
19867e860f15SJohn Edward Broadbent         managerReset["@Redfish.ActionInfo"] =
19877e860f15SJohn Edward Broadbent             "/redfish/v1/Managers/bmc/ResetActionInfo";
19887e860f15SJohn Edward Broadbent 
19897e860f15SJohn Edward Broadbent         // ResetToDefaults (Factory Reset) has values like
19907e860f15SJohn Edward Broadbent         // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
19917e860f15SJohn Edward Broadbent         // on OpenBMC
19927e860f15SJohn Edward Broadbent         nlohmann::json& resetToDefaults =
19937e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
19947e860f15SJohn Edward Broadbent         resetToDefaults["target"] =
19957e860f15SJohn Edward Broadbent             "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
1996613dabeaSEd Tanous         resetToDefaults["ResetType@Redfish.AllowableValues"] =
1997613dabeaSEd Tanous             nlohmann::json::array_t({"ResetAll"});
19987e860f15SJohn Edward Broadbent 
19997c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
20002b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
20017c8c4058STejas Patil 
20027c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
20037c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
20047c8c4058STejas Patil             redfishDateTimeOffset.second;
20057e860f15SJohn Edward Broadbent 
20060e8ac5e7SGunnar Mills         // TODO (Gunnar): Remove these one day since moved to ComputerSystem
20070e8ac5e7SGunnar Mills         // Still used by OCP profiles
20080e8ac5e7SGunnar Mills         // https://github.com/opencomputeproject/OCP-Profiles/issues/23
20097e860f15SJohn Edward Broadbent         // Fill in SerialConsole info
20107e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
2011002d39b4SEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
2012613dabeaSEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
2013613dabeaSEd Tanous             nlohmann::json::array_t({"IPMI", "SSH"});
20147e860f15SJohn Edward Broadbent #ifdef BMCWEB_ENABLE_KVM
20157e860f15SJohn Edward Broadbent         // Fill in GraphicalConsole info
2016002d39b4SEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
2017002d39b4SEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] =
2018002d39b4SEd Tanous             4;
2019613dabeaSEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
2020613dabeaSEd Tanous             nlohmann::json::array_t({"KVMIP"});
20217e860f15SJohn Edward Broadbent #endif // BMCWEB_ENABLE_KVM
20227e860f15SJohn Edward Broadbent 
2023002d39b4SEd Tanous         asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
20241476687dSEd Tanous 
20251476687dSEd Tanous         nlohmann::json::array_t managerForServers;
20261476687dSEd Tanous         nlohmann::json::object_t manager;
20271476687dSEd Tanous         manager["@odata.id"] = "/redfish/v1/Systems/system";
20281476687dSEd Tanous         managerForServers.push_back(std::move(manager));
20291476687dSEd Tanous 
20301476687dSEd Tanous         asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
20311476687dSEd Tanous             std::move(managerForServers);
20327e860f15SJohn Edward Broadbent 
20337e860f15SJohn Edward Broadbent         auto health = std::make_shared<HealthPopulate>(asyncResp);
20347e860f15SJohn Edward Broadbent         health->isManagersHealth = true;
20357e860f15SJohn Edward Broadbent         health->populate();
20367e860f15SJohn Edward Broadbent 
2037eee0013eSWilly Tu         sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
20387e860f15SJohn Edward Broadbent                                              "FirmwareVersion", true);
20397e860f15SJohn Edward Broadbent 
20407e860f15SJohn Edward Broadbent         managerGetLastResetTime(asyncResp);
20417e860f15SJohn Edward Broadbent 
2042a51fc2d2SSui Chen         // ManagerDiagnosticData is added for all BMCs.
2043a51fc2d2SSui Chen         nlohmann::json& managerDiagnosticData =
2044a51fc2d2SSui Chen             asyncResp->res.jsonValue["ManagerDiagnosticData"];
2045a51fc2d2SSui Chen         managerDiagnosticData["@odata.id"] =
2046a51fc2d2SSui Chen             "/redfish/v1/Managers/bmc/ManagerDiagnosticData";
2047a51fc2d2SSui Chen 
204854dce7f5SGunnar Mills #ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
20497e860f15SJohn Edward Broadbent         auto pids = std::make_shared<GetPIDValues>(asyncResp);
20507e860f15SJohn Edward Broadbent         pids->run();
205154dce7f5SGunnar Mills #endif
20527e860f15SJohn Edward Broadbent 
2053002d39b4SEd Tanous         getMainChassisId(asyncResp,
2054002d39b4SEd Tanous                          [](const std::string& chassisId,
2055002d39b4SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2056002d39b4SEd Tanous             aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
20571476687dSEd Tanous             nlohmann::json::array_t managerForChassis;
20588a592810SEd Tanous             nlohmann::json::object_t managerObj;
2059eddfc437SWilly Tu             boost::urls::url chassiUrl = crow::utility::urlFromPieces(
2060eddfc437SWilly Tu                 "redfish", "v1", "Chassis", chassisId);
2061eddfc437SWilly Tu             managerObj["@odata.id"] = chassiUrl;
20628a592810SEd Tanous             managerForChassis.push_back(std::move(managerObj));
20631476687dSEd Tanous             aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
20641476687dSEd Tanous                 std::move(managerForChassis);
20651476687dSEd Tanous             aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
2066eddfc437SWilly Tu                 chassiUrl;
20677e860f15SJohn Edward Broadbent         });
20687e860f15SJohn Edward Broadbent 
20697e860f15SJohn Edward Broadbent         static bool started = false;
20707e860f15SJohn Edward Broadbent 
20717e860f15SJohn Edward Broadbent         if (!started)
20721abe55efSEd Tanous         {
20731e1e598dSJonathan Doman             sdbusplus::asio::getProperty<double>(
20741e1e598dSJonathan Doman                 *crow::connections::systemBus, "org.freedesktop.systemd1",
2075002d39b4SEd Tanous                 "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
2076002d39b4SEd Tanous                 "Progress",
2077*5e7e2dc5SEd Tanous                 [asyncResp](const boost::system::error_code& ec,
20781e1e598dSJonathan Doman                             const double& val) {
20797e860f15SJohn Edward Broadbent                 if (ec)
20801abe55efSEd Tanous                 {
20817e860f15SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "Error while getting progress";
20827e860f15SJohn Edward Broadbent                     messages::internalError(asyncResp->res);
20837e860f15SJohn Edward Broadbent                     return;
20847e860f15SJohn Edward Broadbent                 }
20851e1e598dSJonathan Doman                 if (val < 1.0)
20867e860f15SJohn Edward Broadbent                 {
2087002d39b4SEd Tanous                     asyncResp->res.jsonValue["Status"]["State"] = "Starting";
20887e860f15SJohn Edward Broadbent                     started = true;
20897e860f15SJohn Edward Broadbent                 }
20901e1e598dSJonathan Doman                 });
20919c310685SBorawski.Lukasz         }
20929c310685SBorawski.Lukasz 
2093e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
2094e99073f5SGeorge Liu             "xyz.openbmc_project.Inventory.Item.Bmc"};
2095e99073f5SGeorge Liu         dbus::utility::getSubTree(
2096e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, interfaces,
20977e860f15SJohn Edward Broadbent             [asyncResp](
2098e99073f5SGeorge Liu                 const boost::system::error_code& ec,
2099b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
21007e860f15SJohn Edward Broadbent             if (ec)
21011abe55efSEd Tanous             {
2102002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "D-Bus response error on GetSubTree " << ec;
21037e860f15SJohn Edward Broadbent                 return;
21047e860f15SJohn Edward Broadbent             }
210526f6976fSEd Tanous             if (subtree.empty())
21067e860f15SJohn Edward Broadbent             {
21077e860f15SJohn Edward Broadbent                 BMCWEB_LOG_DEBUG << "Can't find bmc D-Bus object!";
21087e860f15SJohn Edward Broadbent                 return;
21097e860f15SJohn Edward Broadbent             }
21107e860f15SJohn Edward Broadbent             // Assume only 1 bmc D-Bus object
21117e860f15SJohn Edward Broadbent             // Throw an error if there is more than 1
21127e860f15SJohn Edward Broadbent             if (subtree.size() > 1)
21137e860f15SJohn Edward Broadbent             {
2114002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "Found more than 1 bmc D-Bus object!";
21157e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
21167e860f15SJohn Edward Broadbent                 return;
21177e860f15SJohn Edward Broadbent             }
21187e860f15SJohn Edward Broadbent 
2119002d39b4SEd Tanous             if (subtree[0].first.empty() || subtree[0].second.size() != 1)
21207e860f15SJohn Edward Broadbent             {
21217e860f15SJohn Edward Broadbent                 BMCWEB_LOG_DEBUG << "Error getting bmc D-Bus object!";
21227e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
21237e860f15SJohn Edward Broadbent                 return;
21247e860f15SJohn Edward Broadbent             }
21257e860f15SJohn Edward Broadbent 
21267e860f15SJohn Edward Broadbent             const std::string& path = subtree[0].first;
2127002d39b4SEd Tanous             const std::string& connectionName = subtree[0].second[0].first;
21287e860f15SJohn Edward Broadbent 
2129002d39b4SEd Tanous             for (const auto& interfaceName : subtree[0].second[0].second)
21307e860f15SJohn Edward Broadbent             {
21317e860f15SJohn Edward Broadbent                 if (interfaceName ==
21327e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Inventory.Decorator.Asset")
21337e860f15SJohn Edward Broadbent                 {
2134fac6e53bSKrzysztof Grobelny 
2135fac6e53bSKrzysztof Grobelny                     sdbusplus::asio::getAllProperties(
2136fac6e53bSKrzysztof Grobelny                         *crow::connections::systemBus, connectionName, path,
2137fac6e53bSKrzysztof Grobelny                         "xyz.openbmc_project.Inventory.Decorator.Asset",
2138*5e7e2dc5SEd Tanous                         [asyncResp](const boost::system::error_code& ec2,
2139b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap&
21407e860f15SJohn Edward Broadbent                                         propertiesList) {
21418a592810SEd Tanous                         if (ec2)
21427e860f15SJohn Edward Broadbent                         {
2143002d39b4SEd Tanous                             BMCWEB_LOG_DEBUG << "Can't get bmc asset!";
21447e860f15SJohn Edward Broadbent                             return;
21457e860f15SJohn Edward Broadbent                         }
21467e860f15SJohn Edward Broadbent 
2147fac6e53bSKrzysztof Grobelny                         const std::string* partNumber = nullptr;
2148fac6e53bSKrzysztof Grobelny                         const std::string* serialNumber = nullptr;
2149fac6e53bSKrzysztof Grobelny                         const std::string* manufacturer = nullptr;
2150fac6e53bSKrzysztof Grobelny                         const std::string* model = nullptr;
2151fac6e53bSKrzysztof Grobelny                         const std::string* sparePartNumber = nullptr;
2152fac6e53bSKrzysztof Grobelny 
2153fac6e53bSKrzysztof Grobelny                         const bool success = sdbusplus::unpackPropertiesNoThrow(
2154fac6e53bSKrzysztof Grobelny                             dbus_utils::UnpackErrorPrinter(), propertiesList,
2155fac6e53bSKrzysztof Grobelny                             "PartNumber", partNumber, "SerialNumber",
2156fac6e53bSKrzysztof Grobelny                             serialNumber, "Manufacturer", manufacturer, "Model",
2157fac6e53bSKrzysztof Grobelny                             model, "SparePartNumber", sparePartNumber);
2158fac6e53bSKrzysztof Grobelny 
2159fac6e53bSKrzysztof Grobelny                         if (!success)
21607e860f15SJohn Edward Broadbent                         {
2161002d39b4SEd Tanous                             messages::internalError(asyncResp->res);
21627e860f15SJohn Edward Broadbent                             return;
21637e860f15SJohn Edward Broadbent                         }
2164fac6e53bSKrzysztof Grobelny 
2165fac6e53bSKrzysztof Grobelny                         if (partNumber != nullptr)
2166fac6e53bSKrzysztof Grobelny                         {
2167fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["PartNumber"] =
2168fac6e53bSKrzysztof Grobelny                                 *partNumber;
21697e860f15SJohn Edward Broadbent                         }
2170fac6e53bSKrzysztof Grobelny 
2171fac6e53bSKrzysztof Grobelny                         if (serialNumber != nullptr)
2172fac6e53bSKrzysztof Grobelny                         {
2173fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["SerialNumber"] =
2174fac6e53bSKrzysztof Grobelny                                 *serialNumber;
21757e860f15SJohn Edward Broadbent                         }
2176fac6e53bSKrzysztof Grobelny 
2177fac6e53bSKrzysztof Grobelny                         if (manufacturer != nullptr)
2178fac6e53bSKrzysztof Grobelny                         {
2179fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["Manufacturer"] =
2180fac6e53bSKrzysztof Grobelny                                 *manufacturer;
2181fac6e53bSKrzysztof Grobelny                         }
2182fac6e53bSKrzysztof Grobelny 
2183fac6e53bSKrzysztof Grobelny                         if (model != nullptr)
2184fac6e53bSKrzysztof Grobelny                         {
2185fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["Model"] = *model;
2186fac6e53bSKrzysztof Grobelny                         }
2187fac6e53bSKrzysztof Grobelny 
2188fac6e53bSKrzysztof Grobelny                         if (sparePartNumber != nullptr)
2189fac6e53bSKrzysztof Grobelny                         {
2190fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["SparePartNumber"] =
2191fac6e53bSKrzysztof Grobelny                                 *sparePartNumber;
2192fac6e53bSKrzysztof Grobelny                         }
2193fac6e53bSKrzysztof Grobelny                         });
21947e860f15SJohn Edward Broadbent                 }
2195002d39b4SEd Tanous                 else if (interfaceName ==
21960fda0f12SGeorge Liu                          "xyz.openbmc_project.Inventory.Decorator.LocationCode")
21977e860f15SJohn Edward Broadbent                 {
21987e860f15SJohn Edward Broadbent                     getLocation(asyncResp, connectionName, path);
21997e860f15SJohn Edward Broadbent                 }
22007e860f15SJohn Edward Broadbent             }
2201e99073f5SGeorge Liu             });
22027e860f15SJohn Edward Broadbent         });
22037e860f15SJohn Edward Broadbent 
22047e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
2205ed398213SEd Tanous         .privileges(redfish::privileges::patchManager)
220645ca1b86SEd Tanous         .methods(boost::beast::http::verb::patch)(
220745ca1b86SEd Tanous             [&app](const crow::Request& req,
22087e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
22093ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
221045ca1b86SEd Tanous         {
221145ca1b86SEd Tanous             return;
221245ca1b86SEd Tanous         }
22137e860f15SJohn Edward Broadbent         std::optional<nlohmann::json> oem;
22147e860f15SJohn Edward Broadbent         std::optional<nlohmann::json> links;
22157e860f15SJohn Edward Broadbent         std::optional<std::string> datetime;
22167e860f15SJohn Edward Broadbent 
221715ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "Oem", oem,
2218002d39b4SEd Tanous                                       "DateTime", datetime, "Links", links))
22197e860f15SJohn Edward Broadbent         {
22207e860f15SJohn Edward Broadbent             return;
22217e860f15SJohn Edward Broadbent         }
22227e860f15SJohn Edward Broadbent 
22237e860f15SJohn Edward Broadbent         if (oem)
22247e860f15SJohn Edward Broadbent         {
222554dce7f5SGunnar Mills #ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
22267e860f15SJohn Edward Broadbent             std::optional<nlohmann::json> openbmc;
2227002d39b4SEd Tanous             if (!redfish::json_util::readJson(*oem, asyncResp->res, "OpenBmc",
2228002d39b4SEd Tanous                                               openbmc))
22297e860f15SJohn Edward Broadbent             {
22307e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR
22317e860f15SJohn Edward Broadbent                     << "Illegal Property "
2232002d39b4SEd Tanous                     << oem->dump(2, ' ', true,
22337e860f15SJohn Edward Broadbent                                  nlohmann::json::error_handler_t::replace);
22347e860f15SJohn Edward Broadbent                 return;
22357e860f15SJohn Edward Broadbent             }
22367e860f15SJohn Edward Broadbent             if (openbmc)
22377e860f15SJohn Edward Broadbent             {
22387e860f15SJohn Edward Broadbent                 std::optional<nlohmann::json> fan;
2239002d39b4SEd Tanous                 if (!redfish::json_util::readJson(*openbmc, asyncResp->res,
2240002d39b4SEd Tanous                                                   "Fan", fan))
22417e860f15SJohn Edward Broadbent                 {
22427e860f15SJohn Edward Broadbent                     BMCWEB_LOG_ERROR
22437e860f15SJohn Edward Broadbent                         << "Illegal Property "
2244002d39b4SEd Tanous                         << openbmc->dump(
2245002d39b4SEd Tanous                                2, ' ', true,
2246002d39b4SEd Tanous                                nlohmann::json::error_handler_t::replace);
22477e860f15SJohn Edward Broadbent                     return;
22487e860f15SJohn Edward Broadbent                 }
22497e860f15SJohn Edward Broadbent                 if (fan)
22507e860f15SJohn Edward Broadbent                 {
2251002d39b4SEd Tanous                     auto pid = std::make_shared<SetPIDValues>(asyncResp, *fan);
22527e860f15SJohn Edward Broadbent                     pid->run();
22537e860f15SJohn Edward Broadbent                 }
22547e860f15SJohn Edward Broadbent             }
225554dce7f5SGunnar Mills #else
225654dce7f5SGunnar Mills             messages::propertyUnknown(asyncResp->res, "Oem");
225754dce7f5SGunnar Mills             return;
225854dce7f5SGunnar Mills #endif
22597e860f15SJohn Edward Broadbent         }
22607e860f15SJohn Edward Broadbent         if (links)
22617e860f15SJohn Edward Broadbent         {
22627e860f15SJohn Edward Broadbent             std::optional<nlohmann::json> activeSoftwareImage;
22637e860f15SJohn Edward Broadbent             if (!redfish::json_util::readJson(*links, asyncResp->res,
22647e860f15SJohn Edward Broadbent                                               "ActiveSoftwareImage",
22657e860f15SJohn Edward Broadbent                                               activeSoftwareImage))
22667e860f15SJohn Edward Broadbent             {
22677e860f15SJohn Edward Broadbent                 return;
22687e860f15SJohn Edward Broadbent             }
22697e860f15SJohn Edward Broadbent             if (activeSoftwareImage)
22707e860f15SJohn Edward Broadbent             {
22717e860f15SJohn Edward Broadbent                 std::optional<std::string> odataId;
2272002d39b4SEd Tanous                 if (!json_util::readJson(*activeSoftwareImage, asyncResp->res,
2273002d39b4SEd Tanous                                          "@odata.id", odataId))
22747e860f15SJohn Edward Broadbent                 {
22757e860f15SJohn Edward Broadbent                     return;
22767e860f15SJohn Edward Broadbent                 }
22777e860f15SJohn Edward Broadbent 
22787e860f15SJohn Edward Broadbent                 if (odataId)
22797e860f15SJohn Edward Broadbent                 {
22807e860f15SJohn Edward Broadbent                     setActiveFirmwareImage(asyncResp, *odataId);
22817e860f15SJohn Edward Broadbent                 }
22827e860f15SJohn Edward Broadbent             }
22837e860f15SJohn Edward Broadbent         }
22847e860f15SJohn Edward Broadbent         if (datetime)
22857e860f15SJohn Edward Broadbent         {
22867e860f15SJohn Edward Broadbent             setDateTime(asyncResp, std::move(*datetime));
22877e860f15SJohn Edward Broadbent         }
22887e860f15SJohn Edward Broadbent         });
22897e860f15SJohn Edward Broadbent }
22907e860f15SJohn Edward Broadbent 
22917e860f15SJohn Edward Broadbent inline void requestRoutesManagerCollection(App& app)
22927e860f15SJohn Edward Broadbent {
22937e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
2294ed398213SEd Tanous         .privileges(redfish::privileges::getManagerCollection)
22957e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
229645ca1b86SEd Tanous             [&app](const crow::Request& req,
22977e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
22983ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
229945ca1b86SEd Tanous         {
230045ca1b86SEd Tanous             return;
230145ca1b86SEd Tanous         }
230283ff9ab6SJames Feist         // Collections don't include the static data added by SubRoute
230383ff9ab6SJames Feist         // because it has a duplicate entry for members
23048d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
23058d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
23068d1b46d7Szhanghch05             "#ManagerCollection.ManagerCollection";
23078d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Manager Collection";
23088d1b46d7Szhanghch05         asyncResp->res.jsonValue["Members@odata.count"] = 1;
23091476687dSEd Tanous         nlohmann::json::array_t members;
23101476687dSEd Tanous         nlohmann::json& bmc = members.emplace_back();
23111476687dSEd Tanous         bmc["@odata.id"] = "/redfish/v1/Managers/bmc";
23121476687dSEd Tanous         asyncResp->res.jsonValue["Members"] = std::move(members);
23137e860f15SJohn Edward Broadbent         });
23149c310685SBorawski.Lukasz }
23159c310685SBorawski.Lukasz } // namespace redfish
2316