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 1813451e39SWilly Tu #include "bmcweb_config.h" 1913451e39SWilly Tu 20a51fc2d2SSui Chen #include "app.hpp" 21a51fc2d2SSui Chen #include "dbus_utility.hpp" 22b49ac873SJames Feist #include "health.hpp" 23a51fc2d2SSui Chen #include "query.hpp" 24c5d03ff4SJennifer Lee #include "redfish_util.hpp" 25a51fc2d2SSui Chen #include "registries/privilege_registry.hpp" 26fac6e53bSKrzysztof Grobelny #include "utils/dbus_utils.hpp" 273ccb3adbSEd Tanous #include "utils/json_utils.hpp" 28a51fc2d2SSui Chen #include "utils/sw_utils.hpp" 29a51fc2d2SSui Chen #include "utils/systemd_utils.hpp" 302b82937eSEd Tanous #include "utils/time_utils.hpp" 319c310685SBorawski.Lukasz 32e99073f5SGeorge Liu #include <boost/system/error_code.hpp> 33ef4c65b7SEd Tanous #include <boost/url/format.hpp> 34fac6e53bSKrzysztof Grobelny #include <sdbusplus/asio/property.hpp> 35fac6e53bSKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp> 361214b7e7SGunnar Mills 37a170f275SEd Tanous #include <algorithm> 38e99073f5SGeorge Liu #include <array> 394bfefa74SGunnar Mills #include <cstdint> 401214b7e7SGunnar Mills #include <memory> 411214b7e7SGunnar Mills #include <sstream> 42e99073f5SGeorge Liu #include <string_view> 43abf2add6SEd Tanous #include <variant> 445b4aa86bSJames Feist 451abe55efSEd Tanous namespace redfish 461abe55efSEd Tanous { 47ed5befbdSJennifer Lee 48ed5befbdSJennifer Lee /** 492a5c4407SGunnar Mills * Function reboots the BMC. 502a5c4407SGunnar Mills * 512a5c4407SGunnar Mills * @param[in] asyncResp - Shared pointer for completing asynchronous calls 52ed5befbdSJennifer Lee */ 538d1b46d7Szhanghch05 inline void 548d1b46d7Szhanghch05 doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 55ed5befbdSJennifer Lee { 56ed5befbdSJennifer Lee const char* processName = "xyz.openbmc_project.State.BMC"; 57ed5befbdSJennifer Lee const char* objectPath = "/xyz/openbmc_project/state/bmc0"; 58ed5befbdSJennifer Lee const char* interfaceName = "xyz.openbmc_project.State.BMC"; 59ed5befbdSJennifer Lee const std::string& propertyValue = 60ed5befbdSJennifer Lee "xyz.openbmc_project.State.BMC.Transition.Reboot"; 61ed5befbdSJennifer Lee const char* destProperty = "RequestedBMCTransition"; 62ed5befbdSJennifer Lee 63ed5befbdSJennifer Lee // Create the D-Bus variant for D-Bus call. 649ae226faSGeorge Liu sdbusplus::asio::setProperty( 659ae226faSGeorge Liu *crow::connections::systemBus, processName, objectPath, interfaceName, 669ae226faSGeorge Liu destProperty, propertyValue, 675e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec) { 68ed5befbdSJennifer Lee // Use "Set" method to set the property value. 69ed5befbdSJennifer Lee if (ec) 70ed5befbdSJennifer Lee { 7162598e31SEd Tanous BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec); 72ed5befbdSJennifer Lee messages::internalError(asyncResp->res); 73ed5befbdSJennifer Lee return; 74ed5befbdSJennifer Lee } 75ed5befbdSJennifer Lee 76ed5befbdSJennifer Lee messages::success(asyncResp->res); 779ae226faSGeorge Liu }); 78ed5befbdSJennifer Lee } 792a5c4407SGunnar Mills 808d1b46d7Szhanghch05 inline void 818d1b46d7Szhanghch05 doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 82f92af389SJayaprakash Mutyala { 83f92af389SJayaprakash Mutyala const char* processName = "xyz.openbmc_project.State.BMC"; 84f92af389SJayaprakash Mutyala const char* objectPath = "/xyz/openbmc_project/state/bmc0"; 85f92af389SJayaprakash Mutyala const char* interfaceName = "xyz.openbmc_project.State.BMC"; 86f92af389SJayaprakash Mutyala const std::string& propertyValue = 87f92af389SJayaprakash Mutyala "xyz.openbmc_project.State.BMC.Transition.HardReboot"; 88f92af389SJayaprakash Mutyala const char* destProperty = "RequestedBMCTransition"; 89f92af389SJayaprakash Mutyala 90f92af389SJayaprakash Mutyala // Create the D-Bus variant for D-Bus call. 919ae226faSGeorge Liu sdbusplus::asio::setProperty( 929ae226faSGeorge Liu *crow::connections::systemBus, processName, objectPath, interfaceName, 939ae226faSGeorge Liu destProperty, propertyValue, 945e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec) { 95f92af389SJayaprakash Mutyala // Use "Set" method to set the property value. 96f92af389SJayaprakash Mutyala if (ec) 97f92af389SJayaprakash Mutyala { 9862598e31SEd Tanous BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec); 99f92af389SJayaprakash Mutyala messages::internalError(asyncResp->res); 100f92af389SJayaprakash Mutyala return; 101f92af389SJayaprakash Mutyala } 102f92af389SJayaprakash Mutyala 103f92af389SJayaprakash Mutyala messages::success(asyncResp->res); 1049ae226faSGeorge Liu }); 105f92af389SJayaprakash Mutyala } 106f92af389SJayaprakash Mutyala 1072a5c4407SGunnar Mills /** 1082a5c4407SGunnar Mills * ManagerResetAction class supports the POST method for the Reset (reboot) 1092a5c4407SGunnar Mills * action. 1102a5c4407SGunnar Mills */ 1117e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetAction(App& app) 1122a5c4407SGunnar Mills { 1132a5c4407SGunnar Mills /** 1142a5c4407SGunnar Mills * Function handles POST method request. 1152a5c4407SGunnar Mills * Analyzes POST body before sending Reset (Reboot) request data to D-Bus. 116f92af389SJayaprakash Mutyala * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart". 1172a5c4407SGunnar Mills */ 1187e860f15SJohn Edward Broadbent 1197e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/") 120ed398213SEd Tanous .privileges(redfish::privileges::postManager) 1217e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 12245ca1b86SEd Tanous [&app](const crow::Request& req, 1237e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 1243ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 12545ca1b86SEd Tanous { 12645ca1b86SEd Tanous return; 12745ca1b86SEd Tanous } 12862598e31SEd Tanous BMCWEB_LOG_DEBUG("Post Manager Reset."); 1292a5c4407SGunnar Mills 1302a5c4407SGunnar Mills std::string resetType; 1312a5c4407SGunnar Mills 13215ed6780SWilly Tu if (!json_util::readJsonAction(req, asyncResp->res, "ResetType", 1337e860f15SJohn Edward Broadbent resetType)) 1342a5c4407SGunnar Mills { 1352a5c4407SGunnar Mills return; 1362a5c4407SGunnar Mills } 1372a5c4407SGunnar Mills 138f92af389SJayaprakash Mutyala if (resetType == "GracefulRestart") 139f92af389SJayaprakash Mutyala { 14062598e31SEd Tanous BMCWEB_LOG_DEBUG("Proceeding with {}", resetType); 141f92af389SJayaprakash Mutyala doBMCGracefulRestart(asyncResp); 142f92af389SJayaprakash Mutyala return; 143f92af389SJayaprakash Mutyala } 1443174e4dfSEd Tanous if (resetType == "ForceRestart") 145f92af389SJayaprakash Mutyala { 14662598e31SEd Tanous BMCWEB_LOG_DEBUG("Proceeding with {}", resetType); 147f92af389SJayaprakash Mutyala doBMCForceRestart(asyncResp); 148f92af389SJayaprakash Mutyala return; 149f92af389SJayaprakash Mutyala } 15062598e31SEd Tanous BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}", resetType); 1512a5c4407SGunnar Mills messages::actionParameterNotSupported(asyncResp->res, resetType, 1522a5c4407SGunnar Mills "ResetType"); 1532a5c4407SGunnar Mills 1542a5c4407SGunnar Mills return; 1557e860f15SJohn Edward Broadbent }); 1562a5c4407SGunnar Mills } 157ed5befbdSJennifer Lee 1583e40fc74SGunnar Mills /** 1593e40fc74SGunnar Mills * ManagerResetToDefaultsAction class supports POST method for factory reset 1603e40fc74SGunnar Mills * action. 1613e40fc74SGunnar Mills */ 1627e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetToDefaultsAction(App& app) 1633e40fc74SGunnar Mills { 1643e40fc74SGunnar Mills /** 1653e40fc74SGunnar Mills * Function handles ResetToDefaults POST method request. 1663e40fc74SGunnar Mills * 1673e40fc74SGunnar Mills * Analyzes POST body message and factory resets BMC by calling 1683e40fc74SGunnar Mills * BMC code updater factory reset followed by a BMC reboot. 1693e40fc74SGunnar Mills * 1703e40fc74SGunnar Mills * BMC code updater factory reset wipes the whole BMC read-write 1713e40fc74SGunnar Mills * filesystem which includes things like the network settings. 1723e40fc74SGunnar Mills * 1733e40fc74SGunnar Mills * OpenBMC only supports ResetToDefaultsType "ResetAll". 1743e40fc74SGunnar Mills */ 1757e860f15SJohn Edward Broadbent 1767e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 1777e860f15SJohn Edward Broadbent "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/") 178ed398213SEd Tanous .privileges(redfish::privileges::postManager) 1797e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 18045ca1b86SEd Tanous [&app](const crow::Request& req, 1817e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 1823ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 18345ca1b86SEd Tanous { 18445ca1b86SEd Tanous return; 18545ca1b86SEd Tanous } 18662598e31SEd Tanous BMCWEB_LOG_DEBUG("Post ResetToDefaults."); 1873e40fc74SGunnar Mills 1883e40fc74SGunnar Mills std::string resetType; 1893e40fc74SGunnar Mills 190002d39b4SEd Tanous if (!json_util::readJsonAction(req, asyncResp->res, 191002d39b4SEd Tanous "ResetToDefaultsType", resetType)) 1923e40fc74SGunnar Mills { 19362598e31SEd Tanous BMCWEB_LOG_DEBUG("Missing property ResetToDefaultsType."); 1943e40fc74SGunnar Mills 195002d39b4SEd Tanous messages::actionParameterMissing(asyncResp->res, "ResetToDefaults", 1963e40fc74SGunnar Mills "ResetToDefaultsType"); 1973e40fc74SGunnar Mills return; 1983e40fc74SGunnar Mills } 1993e40fc74SGunnar Mills 2003e40fc74SGunnar Mills if (resetType != "ResetAll") 2013e40fc74SGunnar Mills { 20262598e31SEd Tanous BMCWEB_LOG_DEBUG( 20362598e31SEd Tanous "Invalid property value for ResetToDefaultsType: {}", 20462598e31SEd Tanous resetType); 205002d39b4SEd Tanous messages::actionParameterNotSupported(asyncResp->res, resetType, 206002d39b4SEd Tanous "ResetToDefaultsType"); 2073e40fc74SGunnar Mills return; 2083e40fc74SGunnar Mills } 2093e40fc74SGunnar Mills 2103e40fc74SGunnar Mills crow::connections::systemBus->async_method_call( 2115e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec) { 2123e40fc74SGunnar Mills if (ec) 2133e40fc74SGunnar Mills { 21462598e31SEd Tanous BMCWEB_LOG_DEBUG("Failed to ResetToDefaults: {}", ec); 2153e40fc74SGunnar Mills messages::internalError(asyncResp->res); 2163e40fc74SGunnar Mills return; 2173e40fc74SGunnar Mills } 2183e40fc74SGunnar Mills // Factory Reset doesn't actually happen until a reboot 2193e40fc74SGunnar Mills // Can't erase what the BMC is running on 2203e40fc74SGunnar Mills doBMCGracefulRestart(asyncResp); 2213e40fc74SGunnar Mills }, 2223e40fc74SGunnar Mills "xyz.openbmc_project.Software.BMC.Updater", 2233e40fc74SGunnar Mills "/xyz/openbmc_project/software", 2243e40fc74SGunnar Mills "xyz.openbmc_project.Common.FactoryReset", "Reset"); 2257e860f15SJohn Edward Broadbent }); 2263e40fc74SGunnar Mills } 2273e40fc74SGunnar Mills 2281cb1a9e6SAppaRao Puli /** 2291cb1a9e6SAppaRao Puli * ManagerResetActionInfo derived class for delivering Manager 2301cb1a9e6SAppaRao Puli * ResetType AllowableValues using ResetInfo schema. 2311cb1a9e6SAppaRao Puli */ 2327e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetActionInfo(App& app) 2331cb1a9e6SAppaRao Puli { 2341cb1a9e6SAppaRao Puli /** 2351cb1a9e6SAppaRao Puli * Functions triggers appropriate requests on DBus 2361cb1a9e6SAppaRao Puli */ 2377e860f15SJohn Edward Broadbent 2387e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/ResetActionInfo/") 239ed398213SEd Tanous .privileges(redfish::privileges::getActionInfo) 2407e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 24145ca1b86SEd Tanous [&app](const crow::Request& req, 2427e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 2433ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 24445ca1b86SEd Tanous { 24545ca1b86SEd Tanous return; 24645ca1b86SEd Tanous } 2471476687dSEd Tanous 2481476687dSEd Tanous asyncResp->res.jsonValue["@odata.type"] = 2491476687dSEd Tanous "#ActionInfo.v1_1_2.ActionInfo"; 2501476687dSEd Tanous asyncResp->res.jsonValue["@odata.id"] = 2511476687dSEd Tanous "/redfish/v1/Managers/bmc/ResetActionInfo"; 2521476687dSEd Tanous asyncResp->res.jsonValue["Name"] = "Reset Action Info"; 2531476687dSEd Tanous asyncResp->res.jsonValue["Id"] = "ResetActionInfo"; 2541476687dSEd Tanous nlohmann::json::object_t parameter; 2551476687dSEd Tanous parameter["Name"] = "ResetType"; 2561476687dSEd Tanous parameter["Required"] = true; 2571476687dSEd Tanous parameter["DataType"] = "String"; 2581476687dSEd Tanous 2591476687dSEd Tanous nlohmann::json::array_t allowableValues; 260ad539545SPatrick Williams allowableValues.emplace_back("GracefulRestart"); 261ad539545SPatrick Williams allowableValues.emplace_back("ForceRestart"); 2621476687dSEd Tanous parameter["AllowableValues"] = std::move(allowableValues); 2631476687dSEd Tanous 2641476687dSEd Tanous nlohmann::json::array_t parameters; 265ad539545SPatrick Williams parameters.emplace_back(std::move(parameter)); 2661476687dSEd Tanous 2671476687dSEd Tanous asyncResp->res.jsonValue["Parameters"] = std::move(parameters); 2687e860f15SJohn Edward Broadbent }); 2691cb1a9e6SAppaRao Puli } 2701cb1a9e6SAppaRao Puli 2715b4aa86bSJames Feist static constexpr const char* objectManagerIface = 2725b4aa86bSJames Feist "org.freedesktop.DBus.ObjectManager"; 2735b4aa86bSJames Feist static constexpr const char* pidConfigurationIface = 2745b4aa86bSJames Feist "xyz.openbmc_project.Configuration.Pid"; 2755b4aa86bSJames Feist static constexpr const char* pidZoneConfigurationIface = 2765b4aa86bSJames Feist "xyz.openbmc_project.Configuration.Pid.Zone"; 277b7a08d04SJames Feist static constexpr const char* stepwiseConfigurationIface = 278b7a08d04SJames Feist "xyz.openbmc_project.Configuration.Stepwise"; 27973df0db0SJames Feist static constexpr const char* thermalModeIface = 28073df0db0SJames Feist "xyz.openbmc_project.Control.ThermalMode"; 2819c310685SBorawski.Lukasz 2828d1b46d7Szhanghch05 inline void 2838d1b46d7Szhanghch05 asyncPopulatePid(const std::string& connection, const std::string& path, 28473df0db0SJames Feist const std::string& currentProfile, 28573df0db0SJames Feist const std::vector<std::string>& supportedProfiles, 2868d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 2875b4aa86bSJames Feist { 2885eb468daSGeorge Liu sdbusplus::message::object_path objPath(path); 2895eb468daSGeorge Liu dbus::utility::getManagedObjects( 2905eb468daSGeorge Liu connection, objPath, 29173df0db0SJames Feist [asyncResp, currentProfile, supportedProfiles]( 2925e7e2dc5SEd Tanous const boost::system::error_code& ec, 2935b4aa86bSJames Feist const dbus::utility::ManagedObjectType& managedObj) { 2945b4aa86bSJames Feist if (ec) 2955b4aa86bSJames Feist { 29662598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec); 297f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2985b4aa86bSJames Feist return; 2995b4aa86bSJames Feist } 3005b4aa86bSJames Feist nlohmann::json& configRoot = 3015b4aa86bSJames Feist asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"]; 3025b4aa86bSJames Feist nlohmann::json& fans = configRoot["FanControllers"]; 3035b4aa86bSJames Feist fans["@odata.type"] = "#OemManager.FanControllers"; 3040fda0f12SGeorge Liu fans["@odata.id"] = 3050fda0f12SGeorge Liu "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers"; 3065b4aa86bSJames Feist 3075b4aa86bSJames Feist nlohmann::json& pids = configRoot["PidControllers"]; 3085b4aa86bSJames Feist pids["@odata.type"] = "#OemManager.PidControllers"; 3095b4aa86bSJames Feist pids["@odata.id"] = 3105b4aa86bSJames Feist "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers"; 3115b4aa86bSJames Feist 312b7a08d04SJames Feist nlohmann::json& stepwise = configRoot["StepwiseControllers"]; 313b7a08d04SJames Feist stepwise["@odata.type"] = "#OemManager.StepwiseControllers"; 314b7a08d04SJames Feist stepwise["@odata.id"] = 315b7a08d04SJames Feist "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers"; 316b7a08d04SJames Feist 3175b4aa86bSJames Feist nlohmann::json& zones = configRoot["FanZones"]; 3185b4aa86bSJames Feist zones["@odata.id"] = 3195b4aa86bSJames Feist "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones"; 3205b4aa86bSJames Feist zones["@odata.type"] = "#OemManager.FanZones"; 321002d39b4SEd Tanous configRoot["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan"; 3225b4aa86bSJames Feist configRoot["@odata.type"] = "#OemManager.Fan"; 32373df0db0SJames Feist configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles; 32473df0db0SJames Feist 32573df0db0SJames Feist if (!currentProfile.empty()) 32673df0db0SJames Feist { 32773df0db0SJames Feist configRoot["Profile"] = currentProfile; 32873df0db0SJames Feist } 329*bf2ddedeSCarson Labrado BMCWEB_LOG_DEBUG("profile = {} !", currentProfile); 3305b4aa86bSJames Feist 3315b4aa86bSJames Feist for (const auto& pathPair : managedObj) 3325b4aa86bSJames Feist { 3335b4aa86bSJames Feist for (const auto& intfPair : pathPair.second) 3345b4aa86bSJames Feist { 3355b4aa86bSJames Feist if (intfPair.first != pidConfigurationIface && 336b7a08d04SJames Feist intfPair.first != pidZoneConfigurationIface && 337b7a08d04SJames Feist intfPair.first != stepwiseConfigurationIface) 3385b4aa86bSJames Feist { 3395b4aa86bSJames Feist continue; 3405b4aa86bSJames Feist } 34173df0db0SJames Feist 342711ac7a9SEd Tanous std::string name; 343711ac7a9SEd Tanous 344711ac7a9SEd Tanous for (const std::pair<std::string, 345002d39b4SEd Tanous dbus::utility::DbusVariantType>& propPair : 346002d39b4SEd Tanous intfPair.second) 347711ac7a9SEd Tanous { 348711ac7a9SEd Tanous if (propPair.first == "Name") 349711ac7a9SEd Tanous { 3505b4aa86bSJames Feist const std::string* namePtr = 351711ac7a9SEd Tanous std::get_if<std::string>(&propPair.second); 3525b4aa86bSJames Feist if (namePtr == nullptr) 3535b4aa86bSJames Feist { 35462598e31SEd Tanous BMCWEB_LOG_ERROR("Pid Name Field illegal"); 355b7a08d04SJames Feist messages::internalError(asyncResp->res); 3565b4aa86bSJames Feist return; 3575b4aa86bSJames Feist } 358db697703SWilly Tu name = *namePtr; 3595b4aa86bSJames Feist dbus::utility::escapePathForDbus(name); 360711ac7a9SEd Tanous } 361711ac7a9SEd Tanous else if (propPair.first == "Profiles") 36273df0db0SJames Feist { 36373df0db0SJames Feist const std::vector<std::string>* profiles = 36473df0db0SJames Feist std::get_if<std::vector<std::string>>( 365711ac7a9SEd Tanous &propPair.second); 36673df0db0SJames Feist if (profiles == nullptr) 36773df0db0SJames Feist { 36862598e31SEd Tanous BMCWEB_LOG_ERROR("Pid Profiles Field illegal"); 36973df0db0SJames Feist messages::internalError(asyncResp->res); 37073df0db0SJames Feist return; 37173df0db0SJames Feist } 37273df0db0SJames Feist if (std::find(profiles->begin(), profiles->end(), 37373df0db0SJames Feist currentProfile) == profiles->end()) 37473df0db0SJames Feist { 37562598e31SEd Tanous BMCWEB_LOG_INFO( 37662598e31SEd Tanous "{} not supported in current profile", name); 37773df0db0SJames Feist continue; 37873df0db0SJames Feist } 37973df0db0SJames Feist } 380711ac7a9SEd Tanous } 381b7a08d04SJames Feist nlohmann::json* config = nullptr; 382c33a90ecSJames Feist const std::string* classPtr = nullptr; 383711ac7a9SEd Tanous 384711ac7a9SEd Tanous for (const std::pair<std::string, 385002d39b4SEd Tanous dbus::utility::DbusVariantType>& propPair : 386002d39b4SEd Tanous intfPair.second) 387c33a90ecSJames Feist { 388727dc83fSLei YU if (propPair.first == "Class") 389711ac7a9SEd Tanous { 390002d39b4SEd Tanous classPtr = std::get_if<std::string>(&propPair.second); 391711ac7a9SEd Tanous } 392c33a90ecSJames Feist } 393c33a90ecSJames Feist 394ef4c65b7SEd Tanous boost::urls::url url("/redfish/v1/Managers/bmc"); 3955b4aa86bSJames Feist if (intfPair.first == pidZoneConfigurationIface) 3965b4aa86bSJames Feist { 3975b4aa86bSJames Feist std::string chassis; 398002d39b4SEd Tanous if (!dbus::utility::getNthStringFromPath(pathPair.first.str, 399002d39b4SEd Tanous 5, chassis)) 4005b4aa86bSJames Feist { 4015b4aa86bSJames Feist chassis = "#IllegalValue"; 4025b4aa86bSJames Feist } 4035b4aa86bSJames Feist nlohmann::json& zone = zones[name]; 404ef4c65b7SEd Tanous zone["Chassis"]["@odata.id"] = 405ef4c65b7SEd Tanous boost::urls::format("/redfish/v1/Chassis/{}", chassis); 406eddfc437SWilly Tu url.set_fragment( 407eddfc437SWilly Tu ("/Oem/OpenBmc/Fan/FanZones"_json_pointer / name) 408eddfc437SWilly Tu .to_string()); 409eddfc437SWilly Tu zone["@odata.id"] = std::move(url); 4105b4aa86bSJames Feist zone["@odata.type"] = "#OemManager.FanZone"; 411b7a08d04SJames Feist config = &zone; 4125b4aa86bSJames Feist } 4135b4aa86bSJames Feist 414b7a08d04SJames Feist else if (intfPair.first == stepwiseConfigurationIface) 4155b4aa86bSJames Feist { 416c33a90ecSJames Feist if (classPtr == nullptr) 417c33a90ecSJames Feist { 41862598e31SEd Tanous BMCWEB_LOG_ERROR("Pid Class Field illegal"); 419c33a90ecSJames Feist messages::internalError(asyncResp->res); 420c33a90ecSJames Feist return; 421c33a90ecSJames Feist } 422c33a90ecSJames Feist 423b7a08d04SJames Feist nlohmann::json& controller = stepwise[name]; 424b7a08d04SJames Feist config = &controller; 425eddfc437SWilly Tu url.set_fragment( 426eddfc437SWilly Tu ("/Oem/OpenBmc/Fan/StepwiseControllers"_json_pointer / 427eddfc437SWilly Tu name) 428eddfc437SWilly Tu .to_string()); 429eddfc437SWilly Tu controller["@odata.id"] = std::move(url); 430b7a08d04SJames Feist controller["@odata.type"] = 431b7a08d04SJames Feist "#OemManager.StepwiseController"; 432b7a08d04SJames Feist 433c33a90ecSJames Feist controller["Direction"] = *classPtr; 4345b4aa86bSJames Feist } 4355b4aa86bSJames Feist 4365b4aa86bSJames Feist // pid and fans are off the same configuration 437b7a08d04SJames Feist else if (intfPair.first == pidConfigurationIface) 4385b4aa86bSJames Feist { 4395b4aa86bSJames Feist if (classPtr == nullptr) 4405b4aa86bSJames Feist { 44162598e31SEd Tanous BMCWEB_LOG_ERROR("Pid Class Field illegal"); 442a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 4435b4aa86bSJames Feist return; 4445b4aa86bSJames Feist } 4455b4aa86bSJames Feist bool isFan = *classPtr == "fan"; 446002d39b4SEd Tanous nlohmann::json& element = isFan ? fans[name] : pids[name]; 447b7a08d04SJames Feist config = &element; 4485b4aa86bSJames Feist if (isFan) 4495b4aa86bSJames Feist { 450eddfc437SWilly Tu url.set_fragment( 451eddfc437SWilly Tu ("/Oem/OpenBmc/Fan/FanControllers"_json_pointer / 452eddfc437SWilly Tu name) 453eddfc437SWilly Tu .to_string()); 454eddfc437SWilly Tu element["@odata.id"] = std::move(url); 455002d39b4SEd Tanous element["@odata.type"] = "#OemManager.FanController"; 4565b4aa86bSJames Feist } 4575b4aa86bSJames Feist else 4585b4aa86bSJames Feist { 459eddfc437SWilly Tu url.set_fragment( 460eddfc437SWilly Tu ("/Oem/OpenBmc/Fan/PidControllers"_json_pointer / 461eddfc437SWilly Tu name) 462eddfc437SWilly Tu .to_string()); 463eddfc437SWilly Tu element["@odata.id"] = std::move(url); 464002d39b4SEd Tanous element["@odata.type"] = "#OemManager.PidController"; 4655b4aa86bSJames Feist } 466b7a08d04SJames Feist } 467b7a08d04SJames Feist else 468b7a08d04SJames Feist { 46962598e31SEd Tanous BMCWEB_LOG_ERROR("Unexpected configuration"); 470b7a08d04SJames Feist messages::internalError(asyncResp->res); 471b7a08d04SJames Feist return; 472b7a08d04SJames Feist } 473b7a08d04SJames Feist 474b7a08d04SJames Feist // used for making maps out of 2 vectors 475b7a08d04SJames Feist const std::vector<double>* keys = nullptr; 476b7a08d04SJames Feist const std::vector<double>* values = nullptr; 477b7a08d04SJames Feist 478b7a08d04SJames Feist for (const auto& propertyPair : intfPair.second) 479b7a08d04SJames Feist { 480b7a08d04SJames Feist if (propertyPair.first == "Type" || 481b7a08d04SJames Feist propertyPair.first == "Class" || 482b7a08d04SJames Feist propertyPair.first == "Name") 483b7a08d04SJames Feist { 484b7a08d04SJames Feist continue; 485b7a08d04SJames Feist } 486b7a08d04SJames Feist 487b7a08d04SJames Feist // zones 488b7a08d04SJames Feist if (intfPair.first == pidZoneConfigurationIface) 489b7a08d04SJames Feist { 490b7a08d04SJames Feist const double* ptr = 491abf2add6SEd Tanous std::get_if<double>(&propertyPair.second); 492b7a08d04SJames Feist if (ptr == nullptr) 493b7a08d04SJames Feist { 49462598e31SEd Tanous BMCWEB_LOG_ERROR("Field Illegal {}", 49562598e31SEd Tanous propertyPair.first); 496b7a08d04SJames Feist messages::internalError(asyncResp->res); 497b7a08d04SJames Feist return; 498b7a08d04SJames Feist } 499b7a08d04SJames Feist (*config)[propertyPair.first] = *ptr; 500b7a08d04SJames Feist } 501b7a08d04SJames Feist 502b7a08d04SJames Feist if (intfPair.first == stepwiseConfigurationIface) 503b7a08d04SJames Feist { 504b7a08d04SJames Feist if (propertyPair.first == "Reading" || 505b7a08d04SJames Feist propertyPair.first == "Output") 506b7a08d04SJames Feist { 507b7a08d04SJames Feist const std::vector<double>* ptr = 508abf2add6SEd Tanous std::get_if<std::vector<double>>( 509b7a08d04SJames Feist &propertyPair.second); 510b7a08d04SJames Feist 511b7a08d04SJames Feist if (ptr == nullptr) 512b7a08d04SJames Feist { 51362598e31SEd Tanous BMCWEB_LOG_ERROR("Field Illegal {}", 51462598e31SEd Tanous propertyPair.first); 515b7a08d04SJames Feist messages::internalError(asyncResp->res); 516b7a08d04SJames Feist return; 517b7a08d04SJames Feist } 518b7a08d04SJames Feist 519b7a08d04SJames Feist if (propertyPair.first == "Reading") 520b7a08d04SJames Feist { 521b7a08d04SJames Feist keys = ptr; 522b7a08d04SJames Feist } 523b7a08d04SJames Feist else 524b7a08d04SJames Feist { 525b7a08d04SJames Feist values = ptr; 526b7a08d04SJames Feist } 527e662eae8SEd Tanous if (keys != nullptr && values != nullptr) 528b7a08d04SJames Feist { 529b7a08d04SJames Feist if (keys->size() != values->size()) 530b7a08d04SJames Feist { 53162598e31SEd Tanous BMCWEB_LOG_ERROR( 53262598e31SEd Tanous "Reading and Output size don't match "); 533b7a08d04SJames Feist messages::internalError(asyncResp->res); 534b7a08d04SJames Feist return; 535b7a08d04SJames Feist } 536b7a08d04SJames Feist nlohmann::json& steps = (*config)["Steps"]; 537b7a08d04SJames Feist steps = nlohmann::json::array(); 538b7a08d04SJames Feist for (size_t ii = 0; ii < keys->size(); ii++) 539b7a08d04SJames Feist { 5401476687dSEd Tanous nlohmann::json::object_t step; 5411476687dSEd Tanous step["Target"] = (*keys)[ii]; 5421476687dSEd Tanous step["Output"] = (*values)[ii]; 543b2ba3072SPatrick Williams steps.emplace_back(std::move(step)); 544b7a08d04SJames Feist } 545b7a08d04SJames Feist } 546b7a08d04SJames Feist } 547b7a08d04SJames Feist if (propertyPair.first == "NegativeHysteresis" || 548b7a08d04SJames Feist propertyPair.first == "PositiveHysteresis") 549b7a08d04SJames Feist { 550b7a08d04SJames Feist const double* ptr = 551abf2add6SEd Tanous std::get_if<double>(&propertyPair.second); 552b7a08d04SJames Feist if (ptr == nullptr) 553b7a08d04SJames Feist { 55462598e31SEd Tanous BMCWEB_LOG_ERROR("Field Illegal {}", 55562598e31SEd Tanous propertyPair.first); 556b7a08d04SJames Feist messages::internalError(asyncResp->res); 557b7a08d04SJames Feist return; 558b7a08d04SJames Feist } 559b7a08d04SJames Feist (*config)[propertyPair.first] = *ptr; 560b7a08d04SJames Feist } 561b7a08d04SJames Feist } 562b7a08d04SJames Feist 563b7a08d04SJames Feist // pid and fans are off the same configuration 564b7a08d04SJames Feist if (intfPair.first == pidConfigurationIface || 565b7a08d04SJames Feist intfPair.first == stepwiseConfigurationIface) 566b7a08d04SJames Feist { 5675b4aa86bSJames Feist if (propertyPair.first == "Zones") 5685b4aa86bSJames Feist { 5695b4aa86bSJames Feist const std::vector<std::string>* inputs = 570abf2add6SEd Tanous std::get_if<std::vector<std::string>>( 5711b6b96c5SEd Tanous &propertyPair.second); 5725b4aa86bSJames Feist 5735b4aa86bSJames Feist if (inputs == nullptr) 5745b4aa86bSJames Feist { 57562598e31SEd Tanous BMCWEB_LOG_ERROR("Zones Pid Field Illegal"); 576a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 5775b4aa86bSJames Feist return; 5785b4aa86bSJames Feist } 579b7a08d04SJames Feist auto& data = (*config)[propertyPair.first]; 5805b4aa86bSJames Feist data = nlohmann::json::array(); 5815b4aa86bSJames Feist for (std::string itemCopy : *inputs) 5825b4aa86bSJames Feist { 5835b4aa86bSJames Feist dbus::utility::escapePathForDbus(itemCopy); 5841476687dSEd Tanous nlohmann::json::object_t input; 585ef4c65b7SEd Tanous boost::urls::url managerUrl = boost::urls::format( 586ef4c65b7SEd Tanous "/redfish/v1/Managers/bmc#{}", 587eddfc437SWilly Tu ("/Oem/OpenBmc/Fan/FanZones"_json_pointer / 588eddfc437SWilly Tu itemCopy) 589eddfc437SWilly Tu .to_string()); 590eddfc437SWilly Tu input["@odata.id"] = std::move(managerUrl); 591b2ba3072SPatrick Williams data.emplace_back(std::move(input)); 5925b4aa86bSJames Feist } 5935b4aa86bSJames Feist } 5945b4aa86bSJames Feist // todo(james): may never happen, but this 5955b4aa86bSJames Feist // assumes configuration data referenced in the 5965b4aa86bSJames Feist // PID config is provided by the same daemon, we 5975b4aa86bSJames Feist // could add another loop to cover all cases, 5985b4aa86bSJames Feist // but I'm okay kicking this can down the road a 5995b4aa86bSJames Feist // bit 6005b4aa86bSJames Feist 6015b4aa86bSJames Feist else if (propertyPair.first == "Inputs" || 6025b4aa86bSJames Feist propertyPair.first == "Outputs") 6035b4aa86bSJames Feist { 604b7a08d04SJames Feist auto& data = (*config)[propertyPair.first]; 6055b4aa86bSJames Feist const std::vector<std::string>* inputs = 606abf2add6SEd Tanous std::get_if<std::vector<std::string>>( 6071b6b96c5SEd Tanous &propertyPair.second); 6085b4aa86bSJames Feist 6095b4aa86bSJames Feist if (inputs == nullptr) 6105b4aa86bSJames Feist { 61162598e31SEd Tanous BMCWEB_LOG_ERROR("Field Illegal {}", 61262598e31SEd Tanous propertyPair.first); 613f12894f8SJason M. Bills messages::internalError(asyncResp->res); 6145b4aa86bSJames Feist return; 6155b4aa86bSJames Feist } 6165b4aa86bSJames Feist data = *inputs; 617b943aaefSJames Feist } 618b943aaefSJames Feist else if (propertyPair.first == "SetPointOffset") 619b943aaefSJames Feist { 620b943aaefSJames Feist const std::string* ptr = 621002d39b4SEd Tanous std::get_if<std::string>(&propertyPair.second); 622b943aaefSJames Feist 623b943aaefSJames Feist if (ptr == nullptr) 624b943aaefSJames Feist { 62562598e31SEd Tanous BMCWEB_LOG_ERROR("Field Illegal {}", 62662598e31SEd Tanous propertyPair.first); 627b943aaefSJames Feist messages::internalError(asyncResp->res); 628b943aaefSJames Feist return; 629b943aaefSJames Feist } 630b943aaefSJames Feist // translate from dbus to redfish 631b943aaefSJames Feist if (*ptr == "WarningHigh") 632b943aaefSJames Feist { 633b943aaefSJames Feist (*config)["SetPointOffset"] = 634b943aaefSJames Feist "UpperThresholdNonCritical"; 635b943aaefSJames Feist } 636b943aaefSJames Feist else if (*ptr == "WarningLow") 637b943aaefSJames Feist { 638b943aaefSJames Feist (*config)["SetPointOffset"] = 639b943aaefSJames Feist "LowerThresholdNonCritical"; 640b943aaefSJames Feist } 641b943aaefSJames Feist else if (*ptr == "CriticalHigh") 642b943aaefSJames Feist { 643b943aaefSJames Feist (*config)["SetPointOffset"] = 644b943aaefSJames Feist "UpperThresholdCritical"; 645b943aaefSJames Feist } 646b943aaefSJames Feist else if (*ptr == "CriticalLow") 647b943aaefSJames Feist { 648b943aaefSJames Feist (*config)["SetPointOffset"] = 649b943aaefSJames Feist "LowerThresholdCritical"; 650b943aaefSJames Feist } 651b943aaefSJames Feist else 652b943aaefSJames Feist { 65362598e31SEd Tanous BMCWEB_LOG_ERROR("Value Illegal {}", *ptr); 654b943aaefSJames Feist messages::internalError(asyncResp->res); 655b943aaefSJames Feist return; 656b943aaefSJames Feist } 657b943aaefSJames Feist } 658b943aaefSJames Feist // doubles 659002d39b4SEd Tanous else if (propertyPair.first == "FFGainCoefficient" || 6605b4aa86bSJames Feist propertyPair.first == "FFOffCoefficient" || 6615b4aa86bSJames Feist propertyPair.first == "ICoefficient" || 6625b4aa86bSJames Feist propertyPair.first == "ILimitMax" || 6635b4aa86bSJames Feist propertyPair.first == "ILimitMin" || 664002d39b4SEd Tanous propertyPair.first == "PositiveHysteresis" || 665002d39b4SEd Tanous propertyPair.first == "NegativeHysteresis" || 6665b4aa86bSJames Feist propertyPair.first == "OutLimitMax" || 6675b4aa86bSJames Feist propertyPair.first == "OutLimitMin" || 6685b4aa86bSJames Feist propertyPair.first == "PCoefficient" || 6697625cb81SJames Feist propertyPair.first == "SetPoint" || 6705b4aa86bSJames Feist propertyPair.first == "SlewNeg" || 6715b4aa86bSJames Feist propertyPair.first == "SlewPos") 6725b4aa86bSJames Feist { 6735b4aa86bSJames Feist const double* ptr = 674abf2add6SEd Tanous std::get_if<double>(&propertyPair.second); 6755b4aa86bSJames Feist if (ptr == nullptr) 6765b4aa86bSJames Feist { 67762598e31SEd Tanous BMCWEB_LOG_ERROR("Field Illegal {}", 67862598e31SEd Tanous propertyPair.first); 679f12894f8SJason M. Bills messages::internalError(asyncResp->res); 6805b4aa86bSJames Feist return; 6815b4aa86bSJames Feist } 682b7a08d04SJames Feist (*config)[propertyPair.first] = *ptr; 6835b4aa86bSJames Feist } 6845b4aa86bSJames Feist } 6855b4aa86bSJames Feist } 6865b4aa86bSJames Feist } 6875b4aa86bSJames Feist } 6885eb468daSGeorge Liu }); 6895b4aa86bSJames Feist } 690ca537928SJennifer Lee 69183ff9ab6SJames Feist enum class CreatePIDRet 69283ff9ab6SJames Feist { 69383ff9ab6SJames Feist fail, 69483ff9ab6SJames Feist del, 69583ff9ab6SJames Feist patch 69683ff9ab6SJames Feist }; 69783ff9ab6SJames Feist 6988d1b46d7Szhanghch05 inline bool 6998d1b46d7Szhanghch05 getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response, 7005f2caaefSJames Feist std::vector<nlohmann::json>& config, 7015f2caaefSJames Feist std::vector<std::string>& zones) 7025f2caaefSJames Feist { 703b6baeaa4SJames Feist if (config.empty()) 704b6baeaa4SJames Feist { 70562598e31SEd Tanous BMCWEB_LOG_ERROR("Empty Zones"); 706f818b04dSEd Tanous messages::propertyValueFormatError(response->res, config, "Zones"); 707b6baeaa4SJames Feist return false; 708b6baeaa4SJames Feist } 7095f2caaefSJames Feist for (auto& odata : config) 7105f2caaefSJames Feist { 7115f2caaefSJames Feist std::string path; 7125f2caaefSJames Feist if (!redfish::json_util::readJson(odata, response->res, "@odata.id", 7135f2caaefSJames Feist path)) 7145f2caaefSJames Feist { 7155f2caaefSJames Feist return false; 7165f2caaefSJames Feist } 7175f2caaefSJames Feist std::string input; 71861adbda3SJames Feist 71961adbda3SJames Feist // 8 below comes from 72061adbda3SJames Feist // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left 72161adbda3SJames Feist // 0 1 2 3 4 5 6 7 8 72261adbda3SJames Feist if (!dbus::utility::getNthStringFromPath(path, 8, input)) 7235f2caaefSJames Feist { 72462598e31SEd Tanous BMCWEB_LOG_ERROR("Got invalid path {}", path); 72562598e31SEd Tanous BMCWEB_LOG_ERROR("Illegal Type Zones"); 726f818b04dSEd Tanous messages::propertyValueFormatError(response->res, odata, "Zones"); 7275f2caaefSJames Feist return false; 7285f2caaefSJames Feist } 729a170f275SEd Tanous std::replace(input.begin(), input.end(), '_', ' '); 7305f2caaefSJames Feist zones.emplace_back(std::move(input)); 7315f2caaefSJames Feist } 7325f2caaefSJames Feist return true; 7335f2caaefSJames Feist } 7345f2caaefSJames Feist 735711ac7a9SEd Tanous inline const dbus::utility::ManagedObjectType::value_type* 73673df0db0SJames Feist findChassis(const dbus::utility::ManagedObjectType& managedObj, 737b6baeaa4SJames Feist const std::string& value, std::string& chassis) 738b6baeaa4SJames Feist { 73962598e31SEd Tanous BMCWEB_LOG_DEBUG("Find Chassis: {}", value); 740b6baeaa4SJames Feist 741a170f275SEd Tanous std::string escaped = value; 7426ce82fabSYaswanth Reddy M std::replace(escaped.begin(), escaped.end(), ' ', '_'); 743b6baeaa4SJames Feist escaped = "/" + escaped; 744002d39b4SEd Tanous auto it = std::find_if(managedObj.begin(), managedObj.end(), 745002d39b4SEd Tanous [&escaped](const auto& obj) { 746b6baeaa4SJames Feist if (boost::algorithm::ends_with(obj.first.str, escaped)) 747b6baeaa4SJames Feist { 74862598e31SEd Tanous BMCWEB_LOG_DEBUG("Matched {}", obj.first.str); 749b6baeaa4SJames Feist return true; 750b6baeaa4SJames Feist } 751b6baeaa4SJames Feist return false; 752b6baeaa4SJames Feist }); 753b6baeaa4SJames Feist 754b6baeaa4SJames Feist if (it == managedObj.end()) 755b6baeaa4SJames Feist { 75673df0db0SJames Feist return nullptr; 757b6baeaa4SJames Feist } 758b6baeaa4SJames Feist // 5 comes from <chassis-name> being the 5th element 759b6baeaa4SJames Feist // /xyz/openbmc_project/inventory/system/chassis/<chassis-name> 76073df0db0SJames Feist if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis)) 76173df0db0SJames Feist { 76273df0db0SJames Feist return &(*it); 76373df0db0SJames Feist } 76473df0db0SJames Feist 76573df0db0SJames Feist return nullptr; 766b6baeaa4SJames Feist } 767b6baeaa4SJames Feist 76823a21a1cSEd Tanous inline CreatePIDRet createPidInterface( 7698d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type, 770b5a76932SEd Tanous const nlohmann::json::iterator& it, const std::string& path, 77183ff9ab6SJames Feist const dbus::utility::ManagedObjectType& managedObj, bool createNewObject, 772b9d36b47SEd Tanous dbus::utility::DBusPropertiesMap& output, std::string& chassis, 773b9d36b47SEd Tanous const std::string& profile) 77483ff9ab6SJames Feist { 7755f2caaefSJames Feist // common deleter 776b6baeaa4SJames Feist if (it.value() == nullptr) 7775f2caaefSJames Feist { 7785f2caaefSJames Feist std::string iface; 7795f2caaefSJames Feist if (type == "PidControllers" || type == "FanControllers") 7805f2caaefSJames Feist { 7815f2caaefSJames Feist iface = pidConfigurationIface; 7825f2caaefSJames Feist } 7835f2caaefSJames Feist else if (type == "FanZones") 7845f2caaefSJames Feist { 7855f2caaefSJames Feist iface = pidZoneConfigurationIface; 7865f2caaefSJames Feist } 7875f2caaefSJames Feist else if (type == "StepwiseControllers") 7885f2caaefSJames Feist { 7895f2caaefSJames Feist iface = stepwiseConfigurationIface; 7905f2caaefSJames Feist } 7915f2caaefSJames Feist else 7925f2caaefSJames Feist { 79362598e31SEd Tanous BMCWEB_LOG_ERROR("Illegal Type {}", type); 7945f2caaefSJames Feist messages::propertyUnknown(response->res, type); 7955f2caaefSJames Feist return CreatePIDRet::fail; 7965f2caaefSJames Feist } 7976ee7f774SJames Feist 79862598e31SEd Tanous BMCWEB_LOG_DEBUG("del {} {}", path, iface); 7995f2caaefSJames Feist // delete interface 8005f2caaefSJames Feist crow::connections::systemBus->async_method_call( 8015e7e2dc5SEd Tanous [response, path](const boost::system::error_code& ec) { 8025f2caaefSJames Feist if (ec) 8035f2caaefSJames Feist { 80462598e31SEd Tanous BMCWEB_LOG_ERROR("Error patching {}: {}", path, ec); 8055f2caaefSJames Feist messages::internalError(response->res); 806b6baeaa4SJames Feist return; 8075f2caaefSJames Feist } 808b6baeaa4SJames Feist messages::success(response->res); 8095f2caaefSJames Feist }, 8105f2caaefSJames Feist "xyz.openbmc_project.EntityManager", path, iface, "Delete"); 8115f2caaefSJames Feist return CreatePIDRet::del; 8125f2caaefSJames Feist } 8135f2caaefSJames Feist 814711ac7a9SEd Tanous const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr; 815b6baeaa4SJames Feist if (!createNewObject) 816b6baeaa4SJames Feist { 817b6baeaa4SJames Feist // if we aren't creating a new object, we should be able to find it on 818b6baeaa4SJames Feist // d-bus 81973df0db0SJames Feist managedItem = findChassis(managedObj, it.key(), chassis); 82073df0db0SJames Feist if (managedItem == nullptr) 821b6baeaa4SJames Feist { 82262598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to get chassis from config patch"); 823ef4c65b7SEd Tanous messages::invalidObject( 824ef4c65b7SEd Tanous response->res, 825ef4c65b7SEd Tanous boost::urls::format("/redfish/v1/Chassis/{}", chassis)); 826b6baeaa4SJames Feist return CreatePIDRet::fail; 827b6baeaa4SJames Feist } 828b6baeaa4SJames Feist } 829b6baeaa4SJames Feist 83026f6976fSEd Tanous if (!profile.empty() && 83173df0db0SJames Feist (type == "PidControllers" || type == "FanControllers" || 83273df0db0SJames Feist type == "StepwiseControllers")) 83373df0db0SJames Feist { 83473df0db0SJames Feist if (managedItem == nullptr) 83573df0db0SJames Feist { 836b9d36b47SEd Tanous output.emplace_back("Profiles", std::vector<std::string>{profile}); 83773df0db0SJames Feist } 83873df0db0SJames Feist else 83973df0db0SJames Feist { 84073df0db0SJames Feist std::string interface; 84173df0db0SJames Feist if (type == "StepwiseControllers") 84273df0db0SJames Feist { 84373df0db0SJames Feist interface = stepwiseConfigurationIface; 84473df0db0SJames Feist } 84573df0db0SJames Feist else 84673df0db0SJames Feist { 84773df0db0SJames Feist interface = pidConfigurationIface; 84873df0db0SJames Feist } 849711ac7a9SEd Tanous bool ifaceFound = false; 850711ac7a9SEd Tanous for (const auto& iface : managedItem->second) 851711ac7a9SEd Tanous { 852711ac7a9SEd Tanous if (iface.first == interface) 853711ac7a9SEd Tanous { 854711ac7a9SEd Tanous ifaceFound = true; 855711ac7a9SEd Tanous for (const auto& prop : iface.second) 856711ac7a9SEd Tanous { 857711ac7a9SEd Tanous if (prop.first == "Profiles") 858711ac7a9SEd Tanous { 859711ac7a9SEd Tanous const std::vector<std::string>* curProfiles = 860711ac7a9SEd Tanous std::get_if<std::vector<std::string>>( 861711ac7a9SEd Tanous &(prop.second)); 862711ac7a9SEd Tanous if (curProfiles == nullptr) 863711ac7a9SEd Tanous { 86462598e31SEd Tanous BMCWEB_LOG_ERROR( 86562598e31SEd Tanous "Illegal profiles in managed object"); 866711ac7a9SEd Tanous messages::internalError(response->res); 867711ac7a9SEd Tanous return CreatePIDRet::fail; 868711ac7a9SEd Tanous } 869711ac7a9SEd Tanous if (std::find(curProfiles->begin(), 870711ac7a9SEd Tanous curProfiles->end(), 871711ac7a9SEd Tanous profile) == curProfiles->end()) 872711ac7a9SEd Tanous { 873711ac7a9SEd Tanous std::vector<std::string> newProfiles = 874711ac7a9SEd Tanous *curProfiles; 875711ac7a9SEd Tanous newProfiles.push_back(profile); 876b9d36b47SEd Tanous output.emplace_back("Profiles", newProfiles); 877711ac7a9SEd Tanous } 878711ac7a9SEd Tanous } 879711ac7a9SEd Tanous } 880711ac7a9SEd Tanous } 881711ac7a9SEd Tanous } 882711ac7a9SEd Tanous 883711ac7a9SEd Tanous if (!ifaceFound) 88473df0db0SJames Feist { 88562598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to find interface in managed object"); 88673df0db0SJames Feist messages::internalError(response->res); 88773df0db0SJames Feist return CreatePIDRet::fail; 88873df0db0SJames Feist } 88973df0db0SJames Feist } 89073df0db0SJames Feist } 89173df0db0SJames Feist 89283ff9ab6SJames Feist if (type == "PidControllers" || type == "FanControllers") 89383ff9ab6SJames Feist { 89483ff9ab6SJames Feist if (createNewObject) 89583ff9ab6SJames Feist { 896b9d36b47SEd Tanous output.emplace_back("Class", 897b9d36b47SEd Tanous type == "PidControllers" ? "temp" : "fan"); 898b9d36b47SEd Tanous output.emplace_back("Type", "Pid"); 89983ff9ab6SJames Feist } 9005f2caaefSJames Feist 9015f2caaefSJames Feist std::optional<std::vector<nlohmann::json>> zones; 9025f2caaefSJames Feist std::optional<std::vector<std::string>> inputs; 9035f2caaefSJames Feist std::optional<std::vector<std::string>> outputs; 9045f2caaefSJames Feist std::map<std::string, std::optional<double>> doubles; 905b943aaefSJames Feist std::optional<std::string> setpointOffset; 9065f2caaefSJames Feist if (!redfish::json_util::readJson( 907b6baeaa4SJames Feist it.value(), response->res, "Inputs", inputs, "Outputs", outputs, 9085f2caaefSJames Feist "Zones", zones, "FFGainCoefficient", 9095f2caaefSJames Feist doubles["FFGainCoefficient"], "FFOffCoefficient", 9105f2caaefSJames Feist doubles["FFOffCoefficient"], "ICoefficient", 9115f2caaefSJames Feist doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"], 9125f2caaefSJames Feist "ILimitMin", doubles["ILimitMin"], "OutLimitMax", 9135f2caaefSJames Feist doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"], 9145f2caaefSJames Feist "PCoefficient", doubles["PCoefficient"], "SetPoint", 915b943aaefSJames Feist doubles["SetPoint"], "SetPointOffset", setpointOffset, 916b943aaefSJames Feist "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"], 917b943aaefSJames Feist "PositiveHysteresis", doubles["PositiveHysteresis"], 918b943aaefSJames Feist "NegativeHysteresis", doubles["NegativeHysteresis"])) 91983ff9ab6SJames Feist { 9205f2caaefSJames Feist return CreatePIDRet::fail; 92183ff9ab6SJames Feist } 9225f2caaefSJames Feist if (zones) 9235f2caaefSJames Feist { 9245f2caaefSJames Feist std::vector<std::string> zonesStr; 9255f2caaefSJames Feist if (!getZonesFromJsonReq(response, *zones, zonesStr)) 9265f2caaefSJames Feist { 92762598e31SEd Tanous BMCWEB_LOG_ERROR("Illegal Zones"); 9285f2caaefSJames Feist return CreatePIDRet::fail; 9295f2caaefSJames Feist } 930b6baeaa4SJames Feist if (chassis.empty() && 931e662eae8SEd Tanous findChassis(managedObj, zonesStr[0], chassis) == nullptr) 932b6baeaa4SJames Feist { 93362598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to get chassis from config patch"); 934ace85d60SEd Tanous messages::invalidObject( 935ef4c65b7SEd Tanous response->res, 936ef4c65b7SEd Tanous boost::urls::format("/redfish/v1/Chassis/{}", chassis)); 937b6baeaa4SJames Feist return CreatePIDRet::fail; 938b6baeaa4SJames Feist } 939b9d36b47SEd Tanous output.emplace_back("Zones", std::move(zonesStr)); 9405f2caaefSJames Feist } 941afb9ee06SEd Tanous 942afb9ee06SEd Tanous if (inputs) 9435f2caaefSJames Feist { 944afb9ee06SEd Tanous for (std::string& value : *inputs) 94583ff9ab6SJames Feist { 946a170f275SEd Tanous std::replace(value.begin(), value.end(), '_', ' '); 94783ff9ab6SJames Feist } 948afb9ee06SEd Tanous output.emplace_back("Inputs", *inputs); 949afb9ee06SEd Tanous } 950afb9ee06SEd Tanous 951afb9ee06SEd Tanous if (outputs) 9525f2caaefSJames Feist { 953afb9ee06SEd Tanous for (std::string& value : *outputs) 9545f2caaefSJames Feist { 955afb9ee06SEd Tanous std::replace(value.begin(), value.end(), '_', ' '); 9565f2caaefSJames Feist } 957afb9ee06SEd Tanous output.emplace_back("Outputs", *outputs); 95883ff9ab6SJames Feist } 95983ff9ab6SJames Feist 960b943aaefSJames Feist if (setpointOffset) 961b943aaefSJames Feist { 962b943aaefSJames Feist // translate between redfish and dbus names 963b943aaefSJames Feist if (*setpointOffset == "UpperThresholdNonCritical") 964b943aaefSJames Feist { 965b9d36b47SEd Tanous output.emplace_back("SetPointOffset", "WarningLow"); 966b943aaefSJames Feist } 967b943aaefSJames Feist else if (*setpointOffset == "LowerThresholdNonCritical") 968b943aaefSJames Feist { 969b9d36b47SEd Tanous output.emplace_back("SetPointOffset", "WarningHigh"); 970b943aaefSJames Feist } 971b943aaefSJames Feist else if (*setpointOffset == "LowerThresholdCritical") 972b943aaefSJames Feist { 973b9d36b47SEd Tanous output.emplace_back("SetPointOffset", "CriticalLow"); 974b943aaefSJames Feist } 975b943aaefSJames Feist else if (*setpointOffset == "UpperThresholdCritical") 976b943aaefSJames Feist { 977b9d36b47SEd Tanous output.emplace_back("SetPointOffset", "CriticalHigh"); 978b943aaefSJames Feist } 979b943aaefSJames Feist else 980b943aaefSJames Feist { 98162598e31SEd Tanous BMCWEB_LOG_ERROR("Invalid setpointoffset {}", *setpointOffset); 982ace85d60SEd Tanous messages::propertyValueNotInList(response->res, it.key(), 983ace85d60SEd Tanous "SetPointOffset"); 984b943aaefSJames Feist return CreatePIDRet::fail; 985b943aaefSJames Feist } 986b943aaefSJames Feist } 987b943aaefSJames Feist 98883ff9ab6SJames Feist // doubles 9895f2caaefSJames Feist for (const auto& pairs : doubles) 99083ff9ab6SJames Feist { 9915f2caaefSJames Feist if (!pairs.second) 99283ff9ab6SJames Feist { 9935f2caaefSJames Feist continue; 99483ff9ab6SJames Feist } 99562598e31SEd Tanous BMCWEB_LOG_DEBUG("{} = {}", pairs.first, *pairs.second); 996b9d36b47SEd Tanous output.emplace_back(pairs.first, *pairs.second); 9975f2caaefSJames Feist } 99883ff9ab6SJames Feist } 99983ff9ab6SJames Feist 100083ff9ab6SJames Feist else if (type == "FanZones") 100183ff9ab6SJames Feist { 1002b9d36b47SEd Tanous output.emplace_back("Type", "Pid.Zone"); 100383ff9ab6SJames Feist 10045f2caaefSJames Feist std::optional<nlohmann::json> chassisContainer; 10055f2caaefSJames Feist std::optional<double> failSafePercent; 1006d3ec07f8SJames Feist std::optional<double> minThermalOutput; 1007b6baeaa4SJames Feist if (!redfish::json_util::readJson(it.value(), response->res, "Chassis", 10085f2caaefSJames Feist chassisContainer, "FailSafePercent", 1009d3ec07f8SJames Feist failSafePercent, "MinThermalOutput", 1010d3ec07f8SJames Feist minThermalOutput)) 101183ff9ab6SJames Feist { 101283ff9ab6SJames Feist return CreatePIDRet::fail; 101383ff9ab6SJames Feist } 10145f2caaefSJames Feist 10155f2caaefSJames Feist if (chassisContainer) 101683ff9ab6SJames Feist { 10175f2caaefSJames Feist std::string chassisId; 10185f2caaefSJames Feist if (!redfish::json_util::readJson(*chassisContainer, response->res, 10195f2caaefSJames Feist "@odata.id", chassisId)) 10205f2caaefSJames Feist { 102183ff9ab6SJames Feist return CreatePIDRet::fail; 102283ff9ab6SJames Feist } 102383ff9ab6SJames Feist 1024717794d5SAppaRao Puli // /redfish/v1/chassis/chassis_name/ 10255f2caaefSJames Feist if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis)) 102683ff9ab6SJames Feist { 102762598e31SEd Tanous BMCWEB_LOG_ERROR("Got invalid path {}", chassisId); 1028ace85d60SEd Tanous messages::invalidObject( 1029ef4c65b7SEd Tanous response->res, 1030ef4c65b7SEd Tanous boost::urls::format("/redfish/v1/Chassis/{}", chassisId)); 103183ff9ab6SJames Feist return CreatePIDRet::fail; 103283ff9ab6SJames Feist } 103383ff9ab6SJames Feist } 1034d3ec07f8SJames Feist if (minThermalOutput) 103583ff9ab6SJames Feist { 1036b9d36b47SEd Tanous output.emplace_back("MinThermalOutput", *minThermalOutput); 10375f2caaefSJames Feist } 10385f2caaefSJames Feist if (failSafePercent) 103983ff9ab6SJames Feist { 1040b9d36b47SEd Tanous output.emplace_back("FailSafePercent", *failSafePercent); 10415f2caaefSJames Feist } 10425f2caaefSJames Feist } 10435f2caaefSJames Feist else if (type == "StepwiseControllers") 10445f2caaefSJames Feist { 1045b9d36b47SEd Tanous output.emplace_back("Type", "Stepwise"); 10465f2caaefSJames Feist 10475f2caaefSJames Feist std::optional<std::vector<nlohmann::json>> zones; 10485f2caaefSJames Feist std::optional<std::vector<nlohmann::json>> steps; 10495f2caaefSJames Feist std::optional<std::vector<std::string>> inputs; 10505f2caaefSJames Feist std::optional<double> positiveHysteresis; 10515f2caaefSJames Feist std::optional<double> negativeHysteresis; 1052c33a90ecSJames Feist std::optional<std::string> direction; // upper clipping curve vs lower 10535f2caaefSJames Feist if (!redfish::json_util::readJson( 1054b6baeaa4SJames Feist it.value(), response->res, "Zones", zones, "Steps", steps, 1055b6baeaa4SJames Feist "Inputs", inputs, "PositiveHysteresis", positiveHysteresis, 1056c33a90ecSJames Feist "NegativeHysteresis", negativeHysteresis, "Direction", 1057c33a90ecSJames Feist direction)) 10585f2caaefSJames Feist { 105983ff9ab6SJames Feist return CreatePIDRet::fail; 106083ff9ab6SJames Feist } 10615f2caaefSJames Feist 10625f2caaefSJames Feist if (zones) 106383ff9ab6SJames Feist { 1064b6baeaa4SJames Feist std::vector<std::string> zonesStrs; 1065b6baeaa4SJames Feist if (!getZonesFromJsonReq(response, *zones, zonesStrs)) 10665f2caaefSJames Feist { 106762598e31SEd Tanous BMCWEB_LOG_ERROR("Illegal Zones"); 106883ff9ab6SJames Feist return CreatePIDRet::fail; 106983ff9ab6SJames Feist } 1070b6baeaa4SJames Feist if (chassis.empty() && 1071e662eae8SEd Tanous findChassis(managedObj, zonesStrs[0], chassis) == nullptr) 1072b6baeaa4SJames Feist { 107362598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to get chassis from config patch"); 1074ace85d60SEd Tanous messages::invalidObject( 1075ef4c65b7SEd Tanous response->res, 1076ef4c65b7SEd Tanous boost::urls::format("/redfish/v1/Chassis/{}", chassis)); 1077b6baeaa4SJames Feist return CreatePIDRet::fail; 1078b6baeaa4SJames Feist } 1079b9d36b47SEd Tanous output.emplace_back("Zones", std::move(zonesStrs)); 10805f2caaefSJames Feist } 10815f2caaefSJames Feist if (steps) 10825f2caaefSJames Feist { 10835f2caaefSJames Feist std::vector<double> readings; 10845f2caaefSJames Feist std::vector<double> outputs; 10855f2caaefSJames Feist for (auto& step : *steps) 10865f2caaefSJames Feist { 1087543f4400SEd Tanous double target = 0.0; 1088543f4400SEd Tanous double out = 0.0; 10895f2caaefSJames Feist 10905f2caaefSJames Feist if (!redfish::json_util::readJson(step, response->res, "Target", 109123a21a1cSEd Tanous target, "Output", out)) 10925f2caaefSJames Feist { 10935f2caaefSJames Feist return CreatePIDRet::fail; 10945f2caaefSJames Feist } 10955f2caaefSJames Feist readings.emplace_back(target); 109623a21a1cSEd Tanous outputs.emplace_back(out); 10975f2caaefSJames Feist } 1098b9d36b47SEd Tanous output.emplace_back("Reading", std::move(readings)); 1099b9d36b47SEd Tanous output.emplace_back("Output", std::move(outputs)); 11005f2caaefSJames Feist } 11015f2caaefSJames Feist if (inputs) 11025f2caaefSJames Feist { 11035f2caaefSJames Feist for (std::string& value : *inputs) 11045f2caaefSJames Feist { 1105a170f275SEd Tanous std::replace(value.begin(), value.end(), '_', ' '); 11065f2caaefSJames Feist } 1107b9d36b47SEd Tanous output.emplace_back("Inputs", std::move(*inputs)); 11085f2caaefSJames Feist } 11095f2caaefSJames Feist if (negativeHysteresis) 11105f2caaefSJames Feist { 1111b9d36b47SEd Tanous output.emplace_back("NegativeHysteresis", *negativeHysteresis); 11125f2caaefSJames Feist } 11135f2caaefSJames Feist if (positiveHysteresis) 11145f2caaefSJames Feist { 1115b9d36b47SEd Tanous output.emplace_back("PositiveHysteresis", *positiveHysteresis); 111683ff9ab6SJames Feist } 1117c33a90ecSJames Feist if (direction) 1118c33a90ecSJames Feist { 1119c33a90ecSJames Feist constexpr const std::array<const char*, 2> allowedDirections = { 1120c33a90ecSJames Feist "Ceiling", "Floor"}; 1121c33a90ecSJames Feist if (std::find(allowedDirections.begin(), allowedDirections.end(), 1122c33a90ecSJames Feist *direction) == allowedDirections.end()) 1123c33a90ecSJames Feist { 1124c33a90ecSJames Feist messages::propertyValueTypeError(response->res, "Direction", 1125c33a90ecSJames Feist *direction); 1126c33a90ecSJames Feist return CreatePIDRet::fail; 1127c33a90ecSJames Feist } 1128b9d36b47SEd Tanous output.emplace_back("Class", *direction); 1129c33a90ecSJames Feist } 113083ff9ab6SJames Feist } 113183ff9ab6SJames Feist else 113283ff9ab6SJames Feist { 113362598e31SEd Tanous BMCWEB_LOG_ERROR("Illegal Type {}", type); 113435a62c7cSJason M. Bills messages::propertyUnknown(response->res, type); 113583ff9ab6SJames Feist return CreatePIDRet::fail; 113683ff9ab6SJames Feist } 113783ff9ab6SJames Feist return CreatePIDRet::patch; 113883ff9ab6SJames Feist } 113973df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues> 114073df0db0SJames Feist { 11416936afe4SEd Tanous struct CompletionValues 11426936afe4SEd Tanous { 11436936afe4SEd Tanous std::vector<std::string> supportedProfiles; 11446936afe4SEd Tanous std::string currentProfile; 11456936afe4SEd Tanous dbus::utility::MapperGetSubTreeResponse subtree; 11466936afe4SEd Tanous }; 114783ff9ab6SJames Feist 11484e23a444SEd Tanous explicit GetPIDValues( 11494e23a444SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) : 115023a21a1cSEd Tanous asyncResp(asyncRespIn) 115173df0db0SJames Feist 11521214b7e7SGunnar Mills {} 11539c310685SBorawski.Lukasz 115473df0db0SJames Feist void run() 11555b4aa86bSJames Feist { 115673df0db0SJames Feist std::shared_ptr<GetPIDValues> self = shared_from_this(); 115773df0db0SJames Feist 115873df0db0SJames Feist // get all configurations 1159e99073f5SGeorge Liu constexpr std::array<std::string_view, 4> interfaces = { 1160e99073f5SGeorge Liu pidConfigurationIface, pidZoneConfigurationIface, 1161e99073f5SGeorge Liu objectManagerIface, stepwiseConfigurationIface}; 1162e99073f5SGeorge Liu dbus::utility::getSubTree( 1163e99073f5SGeorge Liu "/", 0, interfaces, 1164b9d36b47SEd Tanous [self]( 1165e99073f5SGeorge Liu const boost::system::error_code& ec, 1166b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) { 11675b4aa86bSJames Feist if (ec) 11685b4aa86bSJames Feist { 116962598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec); 117073df0db0SJames Feist messages::internalError(self->asyncResp->res); 117173df0db0SJames Feist return; 117273df0db0SJames Feist } 11736936afe4SEd Tanous self->complete.subtree = subtreeLocal; 1174e99073f5SGeorge Liu }); 117573df0db0SJames Feist 117673df0db0SJames Feist // at the same time get the selected profile 1177e99073f5SGeorge Liu constexpr std::array<std::string_view, 1> thermalModeIfaces = { 1178e99073f5SGeorge Liu thermalModeIface}; 1179e99073f5SGeorge Liu dbus::utility::getSubTree( 1180e99073f5SGeorge Liu "/", 0, thermalModeIfaces, 1181b9d36b47SEd Tanous [self]( 1182e99073f5SGeorge Liu const boost::system::error_code& ec, 1183b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) { 118423a21a1cSEd Tanous if (ec || subtreeLocal.empty()) 118573df0db0SJames Feist { 118673df0db0SJames Feist return; 118773df0db0SJames Feist } 118823a21a1cSEd Tanous if (subtreeLocal[0].second.size() != 1) 118973df0db0SJames Feist { 119073df0db0SJames Feist // invalid mapper response, should never happen 119162598e31SEd Tanous BMCWEB_LOG_ERROR("GetPIDValues: Mapper Error"); 119273df0db0SJames Feist messages::internalError(self->asyncResp->res); 11935b4aa86bSJames Feist return; 11945b4aa86bSJames Feist } 11955b4aa86bSJames Feist 119623a21a1cSEd Tanous const std::string& path = subtreeLocal[0].first; 119723a21a1cSEd Tanous const std::string& owner = subtreeLocal[0].second[0].first; 1198fac6e53bSKrzysztof Grobelny 1199fac6e53bSKrzysztof Grobelny sdbusplus::asio::getAllProperties( 1200fac6e53bSKrzysztof Grobelny *crow::connections::systemBus, owner, path, thermalModeIface, 1201168e20c1SEd Tanous [path, owner, 12025e7e2dc5SEd Tanous self](const boost::system::error_code& ec2, 1203b9d36b47SEd Tanous const dbus::utility::DBusPropertiesMap& resp) { 120423a21a1cSEd Tanous if (ec2) 120573df0db0SJames Feist { 120662598e31SEd Tanous BMCWEB_LOG_ERROR( 120762598e31SEd Tanous "GetPIDValues: Can't get thermalModeIface {}", path); 120873df0db0SJames Feist messages::internalError(self->asyncResp->res); 120973df0db0SJames Feist return; 121073df0db0SJames Feist } 1211fac6e53bSKrzysztof Grobelny 1212271584abSEd Tanous const std::string* current = nullptr; 1213271584abSEd Tanous const std::vector<std::string>* supported = nullptr; 1214fac6e53bSKrzysztof Grobelny 1215fac6e53bSKrzysztof Grobelny const bool success = sdbusplus::unpackPropertiesNoThrow( 1216fac6e53bSKrzysztof Grobelny dbus_utils::UnpackErrorPrinter(), resp, "Current", current, 1217fac6e53bSKrzysztof Grobelny "Supported", supported); 1218fac6e53bSKrzysztof Grobelny 1219fac6e53bSKrzysztof Grobelny if (!success) 122073df0db0SJames Feist { 1221002d39b4SEd Tanous messages::internalError(self->asyncResp->res); 122273df0db0SJames Feist return; 122373df0db0SJames Feist } 1224fac6e53bSKrzysztof Grobelny 122573df0db0SJames Feist if (current == nullptr || supported == nullptr) 122673df0db0SJames Feist { 122762598e31SEd Tanous BMCWEB_LOG_ERROR( 122862598e31SEd Tanous "GetPIDValues: thermal mode iface invalid {}", path); 122973df0db0SJames Feist messages::internalError(self->asyncResp->res); 123073df0db0SJames Feist return; 123173df0db0SJames Feist } 12326936afe4SEd Tanous self->complete.currentProfile = *current; 12336936afe4SEd Tanous self->complete.supportedProfiles = *supported; 1234fac6e53bSKrzysztof Grobelny }); 1235e99073f5SGeorge Liu }); 123673df0db0SJames Feist } 123773df0db0SJames Feist 12386936afe4SEd Tanous static void 12396936afe4SEd Tanous processingComplete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 12406936afe4SEd Tanous const CompletionValues& completion) 124173df0db0SJames Feist { 124273df0db0SJames Feist if (asyncResp->res.result() != boost::beast::http::status::ok) 124373df0db0SJames Feist { 124473df0db0SJames Feist return; 124573df0db0SJames Feist } 12465b4aa86bSJames Feist // create map of <connection, path to objMgr>> 12476936afe4SEd Tanous boost::container::flat_map< 12486936afe4SEd Tanous std::string, std::string, std::less<>, 12496936afe4SEd Tanous std::vector<std::pair<std::string, std::string>>> 12506936afe4SEd Tanous objectMgrPaths; 12516936afe4SEd Tanous boost::container::flat_set<std::string, std::less<>, 12526936afe4SEd Tanous std::vector<std::string>> 12536936afe4SEd Tanous calledConnections; 12546936afe4SEd Tanous for (const auto& pathGroup : completion.subtree) 12555b4aa86bSJames Feist { 12565b4aa86bSJames Feist for (const auto& connectionGroup : pathGroup.second) 12575b4aa86bSJames Feist { 12586bce33bcSJames Feist auto findConnection = 12596bce33bcSJames Feist calledConnections.find(connectionGroup.first); 12606bce33bcSJames Feist if (findConnection != calledConnections.end()) 12616bce33bcSJames Feist { 12626bce33bcSJames Feist break; 12636bce33bcSJames Feist } 126473df0db0SJames Feist for (const std::string& interface : connectionGroup.second) 12655b4aa86bSJames Feist { 12665b4aa86bSJames Feist if (interface == objectManagerIface) 12675b4aa86bSJames Feist { 126873df0db0SJames Feist objectMgrPaths[connectionGroup.first] = pathGroup.first; 12695b4aa86bSJames Feist } 12705b4aa86bSJames Feist // this list is alphabetical, so we 12715b4aa86bSJames Feist // should have found the objMgr by now 12725b4aa86bSJames Feist if (interface == pidConfigurationIface || 1273b7a08d04SJames Feist interface == pidZoneConfigurationIface || 1274b7a08d04SJames Feist interface == stepwiseConfigurationIface) 12755b4aa86bSJames Feist { 12765b4aa86bSJames Feist auto findObjMgr = 12775b4aa86bSJames Feist objectMgrPaths.find(connectionGroup.first); 12785b4aa86bSJames Feist if (findObjMgr == objectMgrPaths.end()) 12795b4aa86bSJames Feist { 128062598e31SEd Tanous BMCWEB_LOG_DEBUG("{}Has no Object Manager", 128162598e31SEd Tanous connectionGroup.first); 12825b4aa86bSJames Feist continue; 12835b4aa86bSJames Feist } 12846bce33bcSJames Feist 12856bce33bcSJames Feist calledConnections.insert(connectionGroup.first); 12866bce33bcSJames Feist 128773df0db0SJames Feist asyncPopulatePid(findObjMgr->first, findObjMgr->second, 12886936afe4SEd Tanous completion.currentProfile, 12896936afe4SEd Tanous completion.supportedProfiles, 129073df0db0SJames Feist asyncResp); 12915b4aa86bSJames Feist break; 12925b4aa86bSJames Feist } 12935b4aa86bSJames Feist } 12945b4aa86bSJames Feist } 12955b4aa86bSJames Feist } 129673df0db0SJames Feist } 129773df0db0SJames Feist 12986936afe4SEd Tanous ~GetPIDValues() 12996936afe4SEd Tanous { 13006936afe4SEd Tanous boost::asio::post(crow::connections::systemBus->get_io_context(), 13016936afe4SEd Tanous std::bind_front(&processingComplete, asyncResp, 13026936afe4SEd Tanous std::move(complete))); 13036936afe4SEd Tanous } 13046936afe4SEd Tanous 1305ecd6a3a2SEd Tanous GetPIDValues(const GetPIDValues&) = delete; 1306ecd6a3a2SEd Tanous GetPIDValues(GetPIDValues&&) = delete; 1307ecd6a3a2SEd Tanous GetPIDValues& operator=(const GetPIDValues&) = delete; 1308ecd6a3a2SEd Tanous GetPIDValues& operator=(GetPIDValues&&) = delete; 1309ecd6a3a2SEd Tanous 13108d1b46d7Szhanghch05 std::shared_ptr<bmcweb::AsyncResp> asyncResp; 13116936afe4SEd Tanous CompletionValues complete; 131273df0db0SJames Feist }; 131373df0db0SJames Feist 131473df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues> 131573df0db0SJames Feist { 13168d1b46d7Szhanghch05 SetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn, 131773df0db0SJames Feist nlohmann::json& data) : 1318271584abSEd Tanous asyncResp(asyncRespIn) 131973df0db0SJames Feist { 132073df0db0SJames Feist std::optional<nlohmann::json> pidControllers; 132173df0db0SJames Feist std::optional<nlohmann::json> fanControllers; 132273df0db0SJames Feist std::optional<nlohmann::json> fanZones; 132373df0db0SJames Feist std::optional<nlohmann::json> stepwiseControllers; 132473df0db0SJames Feist 132573df0db0SJames Feist if (!redfish::json_util::readJson( 132673df0db0SJames Feist data, asyncResp->res, "PidControllers", pidControllers, 132773df0db0SJames Feist "FanControllers", fanControllers, "FanZones", fanZones, 132873df0db0SJames Feist "StepwiseControllers", stepwiseControllers, "Profile", profile)) 132973df0db0SJames Feist { 133073df0db0SJames Feist return; 133173df0db0SJames Feist } 133273df0db0SJames Feist configuration.emplace_back("PidControllers", std::move(pidControllers)); 133373df0db0SJames Feist configuration.emplace_back("FanControllers", std::move(fanControllers)); 133473df0db0SJames Feist configuration.emplace_back("FanZones", std::move(fanZones)); 133573df0db0SJames Feist configuration.emplace_back("StepwiseControllers", 133673df0db0SJames Feist std::move(stepwiseControllers)); 133773df0db0SJames Feist } 1338ecd6a3a2SEd Tanous 1339ecd6a3a2SEd Tanous SetPIDValues(const SetPIDValues&) = delete; 1340ecd6a3a2SEd Tanous SetPIDValues(SetPIDValues&&) = delete; 1341ecd6a3a2SEd Tanous SetPIDValues& operator=(const SetPIDValues&) = delete; 1342ecd6a3a2SEd Tanous SetPIDValues& operator=(SetPIDValues&&) = delete; 1343ecd6a3a2SEd Tanous 134473df0db0SJames Feist void run() 134573df0db0SJames Feist { 134673df0db0SJames Feist if (asyncResp->res.result() != boost::beast::http::status::ok) 134773df0db0SJames Feist { 134873df0db0SJames Feist return; 134973df0db0SJames Feist } 135073df0db0SJames Feist 135173df0db0SJames Feist std::shared_ptr<SetPIDValues> self = shared_from_this(); 135273df0db0SJames Feist 135373df0db0SJames Feist // todo(james): might make sense to do a mapper call here if this 135473df0db0SJames Feist // interface gets more traction 13555eb468daSGeorge Liu sdbusplus::message::object_path objPath( 13565eb468daSGeorge Liu "/xyz/openbmc_project/inventory"); 13575eb468daSGeorge Liu dbus::utility::getManagedObjects( 13585eb468daSGeorge Liu "xyz.openbmc_project.EntityManager", objPath, 13595e7e2dc5SEd Tanous [self](const boost::system::error_code& ec, 1360914e2d5dSEd Tanous const dbus::utility::ManagedObjectType& mObj) { 136173df0db0SJames Feist if (ec) 136273df0db0SJames Feist { 136362598e31SEd Tanous BMCWEB_LOG_ERROR("Error communicating to Entity Manager"); 136473df0db0SJames Feist messages::internalError(self->asyncResp->res); 136573df0db0SJames Feist return; 136673df0db0SJames Feist } 1367e69d9de2SJames Feist const std::array<const char*, 3> configurations = { 1368e69d9de2SJames Feist pidConfigurationIface, pidZoneConfigurationIface, 1369e69d9de2SJames Feist stepwiseConfigurationIface}; 1370e69d9de2SJames Feist 137114b0b8d5SJames Feist for (const auto& [path, object] : mObj) 1372e69d9de2SJames Feist { 137314b0b8d5SJames Feist for (const auto& [interface, _] : object) 1374e69d9de2SJames Feist { 1375002d39b4SEd Tanous if (std::find(configurations.begin(), configurations.end(), 1376e69d9de2SJames Feist interface) != configurations.end()) 1377e69d9de2SJames Feist { 137814b0b8d5SJames Feist self->objectCount++; 1379e69d9de2SJames Feist break; 1380e69d9de2SJames Feist } 1381e69d9de2SJames Feist } 1382e69d9de2SJames Feist } 1383914e2d5dSEd Tanous self->managedObj = mObj; 13845eb468daSGeorge Liu }); 138573df0db0SJames Feist 138673df0db0SJames Feist // at the same time get the profile information 1387e99073f5SGeorge Liu constexpr std::array<std::string_view, 1> thermalModeIfaces = { 1388e99073f5SGeorge Liu thermalModeIface}; 1389e99073f5SGeorge Liu dbus::utility::getSubTree( 1390e99073f5SGeorge Liu "/", 0, thermalModeIfaces, 1391e99073f5SGeorge Liu [self](const boost::system::error_code& ec, 1392b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 139373df0db0SJames Feist if (ec || subtree.empty()) 139473df0db0SJames Feist { 139573df0db0SJames Feist return; 139673df0db0SJames Feist } 139773df0db0SJames Feist if (subtree[0].second.empty()) 139873df0db0SJames Feist { 139973df0db0SJames Feist // invalid mapper response, should never happen 140062598e31SEd Tanous BMCWEB_LOG_ERROR("SetPIDValues: Mapper Error"); 140173df0db0SJames Feist messages::internalError(self->asyncResp->res); 140273df0db0SJames Feist return; 140373df0db0SJames Feist } 140473df0db0SJames Feist 140573df0db0SJames Feist const std::string& path = subtree[0].first; 140673df0db0SJames Feist const std::string& owner = subtree[0].second[0].first; 1407fac6e53bSKrzysztof Grobelny sdbusplus::asio::getAllProperties( 1408fac6e53bSKrzysztof Grobelny *crow::connections::systemBus, owner, path, thermalModeIface, 14095e7e2dc5SEd Tanous [self, path, owner](const boost::system::error_code& ec2, 1410b9d36b47SEd Tanous const dbus::utility::DBusPropertiesMap& r) { 1411cb13a392SEd Tanous if (ec2) 141273df0db0SJames Feist { 141362598e31SEd Tanous BMCWEB_LOG_ERROR( 141462598e31SEd Tanous "SetPIDValues: Can't get thermalModeIface {}", path); 141573df0db0SJames Feist messages::internalError(self->asyncResp->res); 141673df0db0SJames Feist return; 141773df0db0SJames Feist } 1418271584abSEd Tanous const std::string* current = nullptr; 1419271584abSEd Tanous const std::vector<std::string>* supported = nullptr; 1420fac6e53bSKrzysztof Grobelny 1421fac6e53bSKrzysztof Grobelny const bool success = sdbusplus::unpackPropertiesNoThrow( 1422fac6e53bSKrzysztof Grobelny dbus_utils::UnpackErrorPrinter(), r, "Current", current, 1423fac6e53bSKrzysztof Grobelny "Supported", supported); 1424fac6e53bSKrzysztof Grobelny 1425fac6e53bSKrzysztof Grobelny if (!success) 142673df0db0SJames Feist { 1427002d39b4SEd Tanous messages::internalError(self->asyncResp->res); 142873df0db0SJames Feist return; 142973df0db0SJames Feist } 1430fac6e53bSKrzysztof Grobelny 143173df0db0SJames Feist if (current == nullptr || supported == nullptr) 143273df0db0SJames Feist { 143362598e31SEd Tanous BMCWEB_LOG_ERROR( 143462598e31SEd Tanous "SetPIDValues: thermal mode iface invalid {}", path); 143573df0db0SJames Feist messages::internalError(self->asyncResp->res); 143673df0db0SJames Feist return; 143773df0db0SJames Feist } 143873df0db0SJames Feist self->currentProfile = *current; 143973df0db0SJames Feist self->supportedProfiles = *supported; 144073df0db0SJames Feist self->profileConnection = owner; 144173df0db0SJames Feist self->profilePath = path; 1442fac6e53bSKrzysztof Grobelny }); 1443e99073f5SGeorge Liu }); 144473df0db0SJames Feist } 144524b2fe81SEd Tanous void pidSetDone() 144673df0db0SJames Feist { 144773df0db0SJames Feist if (asyncResp->res.result() != boost::beast::http::status::ok) 144873df0db0SJames Feist { 144973df0db0SJames Feist return; 14505b4aa86bSJames Feist } 14518d1b46d7Szhanghch05 std::shared_ptr<bmcweb::AsyncResp> response = asyncResp; 145273df0db0SJames Feist if (profile) 145373df0db0SJames Feist { 145473df0db0SJames Feist if (std::find(supportedProfiles.begin(), supportedProfiles.end(), 145573df0db0SJames Feist *profile) == supportedProfiles.end()) 145673df0db0SJames Feist { 145773df0db0SJames Feist messages::actionParameterUnknown(response->res, "Profile", 145873df0db0SJames Feist *profile); 145973df0db0SJames Feist return; 146073df0db0SJames Feist } 146173df0db0SJames Feist currentProfile = *profile; 14629ae226faSGeorge Liu sdbusplus::asio::setProperty( 14639ae226faSGeorge Liu *crow::connections::systemBus, profileConnection, profilePath, 14649ae226faSGeorge Liu thermalModeIface, "Current", *profile, 14655e7e2dc5SEd Tanous [response](const boost::system::error_code& ec) { 146673df0db0SJames Feist if (ec) 146773df0db0SJames Feist { 146862598e31SEd Tanous BMCWEB_LOG_ERROR("Error patching profile{}", ec); 146973df0db0SJames Feist messages::internalError(response->res); 147073df0db0SJames Feist } 14719ae226faSGeorge Liu }); 147273df0db0SJames Feist } 147373df0db0SJames Feist 147473df0db0SJames Feist for (auto& containerPair : configuration) 147573df0db0SJames Feist { 147673df0db0SJames Feist auto& container = containerPair.second; 147773df0db0SJames Feist if (!container) 147873df0db0SJames Feist { 147973df0db0SJames Feist continue; 148073df0db0SJames Feist } 148162598e31SEd Tanous BMCWEB_LOG_DEBUG("{}", *container); 14826ee7f774SJames Feist 148302cad96eSEd Tanous const std::string& type = containerPair.first; 148473df0db0SJames Feist 148573df0db0SJames Feist for (nlohmann::json::iterator it = container->begin(); 148617a897dfSManojkiran Eda it != container->end(); ++it) 148773df0db0SJames Feist { 148873df0db0SJames Feist const auto& name = it.key(); 1489cddbf3dfSPotin Lai std::string dbusObjName = name; 1490cddbf3dfSPotin Lai std::replace(dbusObjName.begin(), dbusObjName.end(), ' ', '_'); 149162598e31SEd Tanous BMCWEB_LOG_DEBUG("looking for {}", name); 14926ee7f774SJames Feist 149389492a15SPatrick Williams auto pathItr = std::find_if(managedObj.begin(), 149489492a15SPatrick Williams managedObj.end(), 1495cddbf3dfSPotin Lai [&dbusObjName](const auto& obj) { 1496002d39b4SEd Tanous return boost::algorithm::ends_with(obj.first.str, 1497cddbf3dfSPotin Lai "/" + dbusObjName); 149873df0db0SJames Feist }); 1499b9d36b47SEd Tanous dbus::utility::DBusPropertiesMap output; 150073df0db0SJames Feist 150173df0db0SJames Feist output.reserve(16); // The pid interface length 150273df0db0SJames Feist 150373df0db0SJames Feist // determines if we're patching entity-manager or 150473df0db0SJames Feist // creating a new object 150573df0db0SJames Feist bool createNewObject = (pathItr == managedObj.end()); 150662598e31SEd Tanous BMCWEB_LOG_DEBUG("Found = {}", !createNewObject); 15076ee7f774SJames Feist 150873df0db0SJames Feist std::string iface; 1509ea2b670dSEd Tanous if (!createNewObject) 1510ea2b670dSEd Tanous { 15118be2b5b6SPotin Lai bool findInterface = false; 1512ea2b670dSEd Tanous for (const auto& interface : pathItr->second) 1513ea2b670dSEd Tanous { 1514ea2b670dSEd Tanous if (interface.first == pidConfigurationIface) 1515ea2b670dSEd Tanous { 1516ea2b670dSEd Tanous if (type == "PidControllers" || 1517ea2b670dSEd Tanous type == "FanControllers") 151873df0db0SJames Feist { 151973df0db0SJames Feist iface = pidConfigurationIface; 15208be2b5b6SPotin Lai findInterface = true; 15218be2b5b6SPotin Lai break; 152273df0db0SJames Feist } 152373df0db0SJames Feist } 1524ea2b670dSEd Tanous else if (interface.first == pidZoneConfigurationIface) 152573df0db0SJames Feist { 1526ea2b670dSEd Tanous if (type == "FanZones") 152773df0db0SJames Feist { 1528ea2b670dSEd Tanous iface = pidConfigurationIface; 15298be2b5b6SPotin Lai findInterface = true; 15308be2b5b6SPotin Lai break; 153173df0db0SJames Feist } 153273df0db0SJames Feist } 1533ea2b670dSEd Tanous else if (interface.first == stepwiseConfigurationIface) 1534ea2b670dSEd Tanous { 1535ea2b670dSEd Tanous if (type == "StepwiseControllers") 153673df0db0SJames Feist { 153773df0db0SJames Feist iface = stepwiseConfigurationIface; 15388be2b5b6SPotin Lai findInterface = true; 15398be2b5b6SPotin Lai break; 15408be2b5b6SPotin Lai } 15418be2b5b6SPotin Lai } 15428be2b5b6SPotin Lai } 15438be2b5b6SPotin Lai 15448be2b5b6SPotin Lai // create new object if interface not found 15458be2b5b6SPotin Lai if (!findInterface) 15468be2b5b6SPotin Lai { 154773df0db0SJames Feist createNewObject = true; 154873df0db0SJames Feist } 1549ea2b670dSEd Tanous } 15506ee7f774SJames Feist 15516ee7f774SJames Feist if (createNewObject && it.value() == nullptr) 15526ee7f774SJames Feist { 15534e0453b1SGunnar Mills // can't delete a non-existent object 1554e2616cc5SEd Tanous messages::propertyValueNotInList(response->res, it.value(), 1555e2616cc5SEd Tanous name); 15566ee7f774SJames Feist continue; 15576ee7f774SJames Feist } 15586ee7f774SJames Feist 15596ee7f774SJames Feist std::string path; 15606ee7f774SJames Feist if (pathItr != managedObj.end()) 15616ee7f774SJames Feist { 15626ee7f774SJames Feist path = pathItr->first.str; 15636ee7f774SJames Feist } 15646ee7f774SJames Feist 156562598e31SEd Tanous BMCWEB_LOG_DEBUG("Create new = {}", createNewObject); 1566e69d9de2SJames Feist 1567e69d9de2SJames Feist // arbitrary limit to avoid attacks 1568e69d9de2SJames Feist constexpr const size_t controllerLimit = 500; 156914b0b8d5SJames Feist if (createNewObject && objectCount >= controllerLimit) 1570e69d9de2SJames Feist { 1571e69d9de2SJames Feist messages::resourceExhaustion(response->res, type); 1572e69d9de2SJames Feist continue; 1573e69d9de2SJames Feist } 1574a170f275SEd Tanous std::string escaped = name; 1575a170f275SEd Tanous std::replace(escaped.begin(), escaped.end(), '_', ' '); 1576a170f275SEd Tanous output.emplace_back("Name", escaped); 157773df0db0SJames Feist 157873df0db0SJames Feist std::string chassis; 157973df0db0SJames Feist CreatePIDRet ret = createPidInterface( 15806ee7f774SJames Feist response, type, it, path, managedObj, createNewObject, 15816ee7f774SJames Feist output, chassis, currentProfile); 158273df0db0SJames Feist if (ret == CreatePIDRet::fail) 158373df0db0SJames Feist { 158473df0db0SJames Feist return; 158573df0db0SJames Feist } 15863174e4dfSEd Tanous if (ret == CreatePIDRet::del) 158773df0db0SJames Feist { 158873df0db0SJames Feist continue; 158973df0db0SJames Feist } 159073df0db0SJames Feist 159173df0db0SJames Feist if (!createNewObject) 159273df0db0SJames Feist { 159373df0db0SJames Feist for (const auto& property : output) 159473df0db0SJames Feist { 15959ae226faSGeorge Liu sdbusplus::asio::setProperty( 15969ae226faSGeorge Liu *crow::connections::systemBus, 15979ae226faSGeorge Liu "xyz.openbmc_project.EntityManager", path, iface, 15989ae226faSGeorge Liu property.first, property.second, 159973df0db0SJames Feist [response, 160073df0db0SJames Feist propertyName{std::string(property.first)}]( 16015e7e2dc5SEd Tanous const boost::system::error_code& ec) { 160273df0db0SJames Feist if (ec) 160373df0db0SJames Feist { 160462598e31SEd Tanous BMCWEB_LOG_ERROR("Error patching {}: {}", 160562598e31SEd Tanous propertyName, ec); 160673df0db0SJames Feist messages::internalError(response->res); 160773df0db0SJames Feist return; 160873df0db0SJames Feist } 160973df0db0SJames Feist messages::success(response->res); 16109ae226faSGeorge Liu }); 161173df0db0SJames Feist } 161273df0db0SJames Feist } 161373df0db0SJames Feist else 161473df0db0SJames Feist { 161573df0db0SJames Feist if (chassis.empty()) 161673df0db0SJames Feist { 161762598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to get chassis from config"); 1618ace85d60SEd Tanous messages::internalError(response->res); 161973df0db0SJames Feist return; 162073df0db0SJames Feist } 162173df0db0SJames Feist 162273df0db0SJames Feist bool foundChassis = false; 162373df0db0SJames Feist for (const auto& obj : managedObj) 162473df0db0SJames Feist { 162573df0db0SJames Feist if (boost::algorithm::ends_with(obj.first.str, chassis)) 162673df0db0SJames Feist { 162773df0db0SJames Feist chassis = obj.first.str; 162873df0db0SJames Feist foundChassis = true; 162973df0db0SJames Feist break; 163073df0db0SJames Feist } 163173df0db0SJames Feist } 163273df0db0SJames Feist if (!foundChassis) 163373df0db0SJames Feist { 163462598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to find chassis on dbus"); 163573df0db0SJames Feist messages::resourceMissingAtURI( 1636ace85d60SEd Tanous response->res, 1637ef4c65b7SEd Tanous boost::urls::format("/redfish/v1/Chassis/{}", 1638ef4c65b7SEd Tanous chassis)); 163973df0db0SJames Feist return; 164073df0db0SJames Feist } 164173df0db0SJames Feist 164273df0db0SJames Feist crow::connections::systemBus->async_method_call( 16435e7e2dc5SEd Tanous [response](const boost::system::error_code& ec) { 164473df0db0SJames Feist if (ec) 164573df0db0SJames Feist { 164662598e31SEd Tanous BMCWEB_LOG_ERROR("Error Adding Pid Object {}", ec); 164773df0db0SJames Feist messages::internalError(response->res); 164873df0db0SJames Feist return; 164973df0db0SJames Feist } 165073df0db0SJames Feist messages::success(response->res); 165173df0db0SJames Feist }, 165273df0db0SJames Feist "xyz.openbmc_project.EntityManager", chassis, 165373df0db0SJames Feist "xyz.openbmc_project.AddObject", "AddObject", output); 165473df0db0SJames Feist } 165573df0db0SJames Feist } 165673df0db0SJames Feist } 165773df0db0SJames Feist } 165824b2fe81SEd Tanous 165924b2fe81SEd Tanous ~SetPIDValues() 166024b2fe81SEd Tanous { 166124b2fe81SEd Tanous try 166224b2fe81SEd Tanous { 166324b2fe81SEd Tanous pidSetDone(); 166424b2fe81SEd Tanous } 166524b2fe81SEd Tanous catch (...) 166624b2fe81SEd Tanous { 166762598e31SEd Tanous BMCWEB_LOG_CRITICAL("pidSetDone threw exception"); 166824b2fe81SEd Tanous } 166924b2fe81SEd Tanous } 167024b2fe81SEd Tanous 16718d1b46d7Szhanghch05 std::shared_ptr<bmcweb::AsyncResp> asyncResp; 167273df0db0SJames Feist std::vector<std::pair<std::string, std::optional<nlohmann::json>>> 167373df0db0SJames Feist configuration; 167473df0db0SJames Feist std::optional<std::string> profile; 167573df0db0SJames Feist dbus::utility::ManagedObjectType managedObj; 167673df0db0SJames Feist std::vector<std::string> supportedProfiles; 167773df0db0SJames Feist std::string currentProfile; 167873df0db0SJames Feist std::string profileConnection; 167973df0db0SJames Feist std::string profilePath; 168014b0b8d5SJames Feist size_t objectCount = 0; 168173df0db0SJames Feist }; 168273df0db0SJames Feist 1683071d8fdfSSunnySrivastava1984 /** 1684071d8fdfSSunnySrivastava1984 * @brief Retrieves BMC manager location data over DBus 1685071d8fdfSSunnySrivastava1984 * 1686ac106bf6SEd Tanous * @param[in] asyncResp Shared pointer for completing asynchronous calls 1687071d8fdfSSunnySrivastava1984 * @param[in] connectionName - service name 1688071d8fdfSSunnySrivastava1984 * @param[in] path - object path 1689071d8fdfSSunnySrivastava1984 * @return none 1690071d8fdfSSunnySrivastava1984 */ 1691ac106bf6SEd Tanous inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1692071d8fdfSSunnySrivastava1984 const std::string& connectionName, 1693071d8fdfSSunnySrivastava1984 const std::string& path) 1694071d8fdfSSunnySrivastava1984 { 169562598e31SEd Tanous BMCWEB_LOG_DEBUG("Get BMC manager Location data."); 1696071d8fdfSSunnySrivastava1984 16971e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::string>( 16981e1e598dSJonathan Doman *crow::connections::systemBus, connectionName, path, 16991e1e598dSJonathan Doman "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode", 1700ac106bf6SEd Tanous [asyncResp](const boost::system::error_code& ec, 17011e1e598dSJonathan Doman const std::string& property) { 1702071d8fdfSSunnySrivastava1984 if (ec) 1703071d8fdfSSunnySrivastava1984 { 170462598e31SEd Tanous BMCWEB_LOG_DEBUG("DBUS response error for " 170562598e31SEd Tanous "Location"); 1706ac106bf6SEd Tanous messages::internalError(asyncResp->res); 1707071d8fdfSSunnySrivastava1984 return; 1708071d8fdfSSunnySrivastava1984 } 1709071d8fdfSSunnySrivastava1984 1710ac106bf6SEd Tanous asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] = 17111e1e598dSJonathan Doman property; 17121e1e598dSJonathan Doman }); 1713071d8fdfSSunnySrivastava1984 } 17147e860f15SJohn Edward Broadbent // avoid name collision systems.hpp 17157e860f15SJohn Edward Broadbent inline void 1716ac106bf6SEd Tanous managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 17174bf2b033SGunnar Mills { 171862598e31SEd Tanous BMCWEB_LOG_DEBUG("Getting Manager Last Reset Time"); 17194bf2b033SGunnar Mills 17201e1e598dSJonathan Doman sdbusplus::asio::getProperty<uint64_t>( 17211e1e598dSJonathan Doman *crow::connections::systemBus, "xyz.openbmc_project.State.BMC", 17221e1e598dSJonathan Doman "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC", 17231e1e598dSJonathan Doman "LastRebootTime", 1724ac106bf6SEd Tanous [asyncResp](const boost::system::error_code& ec, 17251e1e598dSJonathan Doman const uint64_t lastResetTime) { 17264bf2b033SGunnar Mills if (ec) 17274bf2b033SGunnar Mills { 172862598e31SEd Tanous BMCWEB_LOG_DEBUG("D-BUS response error {}", ec); 17294bf2b033SGunnar Mills return; 17304bf2b033SGunnar Mills } 17314bf2b033SGunnar Mills 17324bf2b033SGunnar Mills // LastRebootTime is epoch time, in milliseconds 17334bf2b033SGunnar Mills // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19 17341e1e598dSJonathan Doman uint64_t lastResetTimeStamp = lastResetTime / 1000; 17354bf2b033SGunnar Mills 17364bf2b033SGunnar Mills // Convert to ISO 8601 standard 1737ac106bf6SEd Tanous asyncResp->res.jsonValue["LastResetTime"] = 17382b82937eSEd Tanous redfish::time_utils::getDateTimeUint(lastResetTimeStamp); 17391e1e598dSJonathan Doman }); 17404bf2b033SGunnar Mills } 17414bf2b033SGunnar Mills 17424bfefa74SGunnar Mills /** 17434bfefa74SGunnar Mills * @brief Set the running firmware image 17444bfefa74SGunnar Mills * 1745ac106bf6SEd Tanous * @param[i,o] asyncResp - Async response object 17464bfefa74SGunnar Mills * @param[i] runningFirmwareTarget - Image to make the running image 17474bfefa74SGunnar Mills * 17484bfefa74SGunnar Mills * @return void 17494bfefa74SGunnar Mills */ 17507e860f15SJohn Edward Broadbent inline void 1751ac106bf6SEd Tanous setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1752f23b7296SEd Tanous const std::string& runningFirmwareTarget) 17534bfefa74SGunnar Mills { 17544bfefa74SGunnar Mills // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id> 1755f23b7296SEd Tanous std::string::size_type idPos = runningFirmwareTarget.rfind('/'); 17564bfefa74SGunnar Mills if (idPos == std::string::npos) 17574bfefa74SGunnar Mills { 1758ac106bf6SEd Tanous messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget, 17594bfefa74SGunnar Mills "@odata.id"); 176062598e31SEd Tanous BMCWEB_LOG_DEBUG("Can't parse firmware ID!"); 17614bfefa74SGunnar Mills return; 17624bfefa74SGunnar Mills } 17634bfefa74SGunnar Mills idPos++; 17644bfefa74SGunnar Mills if (idPos >= runningFirmwareTarget.size()) 17654bfefa74SGunnar Mills { 1766ac106bf6SEd Tanous messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget, 17674bfefa74SGunnar Mills "@odata.id"); 176862598e31SEd Tanous BMCWEB_LOG_DEBUG("Invalid firmware ID."); 17694bfefa74SGunnar Mills return; 17704bfefa74SGunnar Mills } 17714bfefa74SGunnar Mills std::string firmwareId = runningFirmwareTarget.substr(idPos); 17724bfefa74SGunnar Mills 17734bfefa74SGunnar Mills // Make sure the image is valid before setting priority 17745eb468daSGeorge Liu sdbusplus::message::object_path objPath("/xyz/openbmc_project/software"); 17755eb468daSGeorge Liu dbus::utility::getManagedObjects( 17765eb468daSGeorge Liu "xyz.openbmc_project.Software.BMC.Updater", objPath, 17775eb468daSGeorge Liu [asyncResp, firmwareId, runningFirmwareTarget]( 17785eb468daSGeorge Liu const boost::system::error_code& ec, 17795eb468daSGeorge Liu const dbus::utility::ManagedObjectType& subtree) { 17804bfefa74SGunnar Mills if (ec) 17814bfefa74SGunnar Mills { 178262598e31SEd Tanous BMCWEB_LOG_DEBUG("D-Bus response error getting objects."); 1783ac106bf6SEd Tanous messages::internalError(asyncResp->res); 17844bfefa74SGunnar Mills return; 17854bfefa74SGunnar Mills } 17864bfefa74SGunnar Mills 178726f6976fSEd Tanous if (subtree.empty()) 17884bfefa74SGunnar Mills { 178962598e31SEd Tanous BMCWEB_LOG_DEBUG("Can't find image!"); 1790ac106bf6SEd Tanous messages::internalError(asyncResp->res); 17914bfefa74SGunnar Mills return; 17924bfefa74SGunnar Mills } 17934bfefa74SGunnar Mills 17944bfefa74SGunnar Mills bool foundImage = false; 179502cad96eSEd Tanous for (const auto& object : subtree) 17964bfefa74SGunnar Mills { 17974bfefa74SGunnar Mills const std::string& path = 17984bfefa74SGunnar Mills static_cast<const std::string&>(object.first); 1799f23b7296SEd Tanous std::size_t idPos2 = path.rfind('/'); 18004bfefa74SGunnar Mills 18014bfefa74SGunnar Mills if (idPos2 == std::string::npos) 18024bfefa74SGunnar Mills { 18034bfefa74SGunnar Mills continue; 18044bfefa74SGunnar Mills } 18054bfefa74SGunnar Mills 18064bfefa74SGunnar Mills idPos2++; 18074bfefa74SGunnar Mills if (idPos2 >= path.size()) 18084bfefa74SGunnar Mills { 18094bfefa74SGunnar Mills continue; 18104bfefa74SGunnar Mills } 18114bfefa74SGunnar Mills 18124bfefa74SGunnar Mills if (path.substr(idPos2) == firmwareId) 18134bfefa74SGunnar Mills { 18144bfefa74SGunnar Mills foundImage = true; 18154bfefa74SGunnar Mills break; 18164bfefa74SGunnar Mills } 18174bfefa74SGunnar Mills } 18184bfefa74SGunnar Mills 18194bfefa74SGunnar Mills if (!foundImage) 18204bfefa74SGunnar Mills { 1821ac106bf6SEd Tanous messages::propertyValueNotInList( 1822ac106bf6SEd Tanous asyncResp->res, runningFirmwareTarget, "@odata.id"); 182362598e31SEd Tanous BMCWEB_LOG_DEBUG("Invalid firmware ID."); 18244bfefa74SGunnar Mills return; 18254bfefa74SGunnar Mills } 18264bfefa74SGunnar Mills 182762598e31SEd Tanous BMCWEB_LOG_DEBUG("Setting firmware version {} to priority 0.", 182862598e31SEd Tanous firmwareId); 18294bfefa74SGunnar Mills 18304bfefa74SGunnar Mills // Only support Immediate 18314bfefa74SGunnar Mills // An addition could be a Redfish Setting like 18324bfefa74SGunnar Mills // ActiveSoftwareImageApplyTime and support OnReset 18339ae226faSGeorge Liu sdbusplus::asio::setProperty( 18349ae226faSGeorge Liu *crow::connections::systemBus, 18359ae226faSGeorge Liu "xyz.openbmc_project.Software.BMC.Updater", 18369ae226faSGeorge Liu "/xyz/openbmc_project/software/" + firmwareId, 18379ae226faSGeorge Liu "xyz.openbmc_project.Software.RedundancyPriority", "Priority", 18389ae226faSGeorge Liu static_cast<uint8_t>(0), 1839ac106bf6SEd Tanous [asyncResp](const boost::system::error_code& ec2) { 18408a592810SEd Tanous if (ec2) 18414bfefa74SGunnar Mills { 184262598e31SEd Tanous BMCWEB_LOG_DEBUG("D-Bus response error setting."); 1843ac106bf6SEd Tanous messages::internalError(asyncResp->res); 18444bfefa74SGunnar Mills return; 18454bfefa74SGunnar Mills } 1846ac106bf6SEd Tanous doBMCGracefulRestart(asyncResp); 18479ae226faSGeorge Liu }); 18485eb468daSGeorge Liu }); 18494bfefa74SGunnar Mills } 18504bfefa74SGunnar Mills 1851ac106bf6SEd Tanous inline void setDateTime(std::shared_ptr<bmcweb::AsyncResp> asyncResp, 18527e860f15SJohn Edward Broadbent std::string datetime) 1853af5d6058SSantosh Puranik { 185462598e31SEd Tanous BMCWEB_LOG_DEBUG("Set date time: {}", datetime); 1855af5d6058SSantosh Puranik 1856c2e32007SEd Tanous std::optional<redfish::time_utils::usSinceEpoch> us = 1857c2e32007SEd Tanous redfish::time_utils::dateStringToEpoch(datetime); 1858c2e32007SEd Tanous if (!us) 1859af5d6058SSantosh Puranik { 1860ac106bf6SEd Tanous messages::propertyValueFormatError(asyncResp->res, datetime, 1861ac106bf6SEd Tanous "DateTime"); 1862c2e32007SEd Tanous return; 1863c2e32007SEd Tanous } 18649ae226faSGeorge Liu sdbusplus::asio::setProperty( 18659ae226faSGeorge Liu *crow::connections::systemBus, "xyz.openbmc_project.Time.Manager", 18669ae226faSGeorge Liu "/xyz/openbmc_project/time/bmc", "xyz.openbmc_project.Time.EpochTime", 18679ae226faSGeorge Liu "Elapsed", us->count(), 1868ac106bf6SEd Tanous [asyncResp{std::move(asyncResp)}, 18695e7e2dc5SEd Tanous datetime{std::move(datetime)}](const boost::system::error_code& ec) { 1870af5d6058SSantosh Puranik if (ec) 1871af5d6058SSantosh Puranik { 187262598e31SEd Tanous BMCWEB_LOG_DEBUG("Failed to set elapsed time. " 187362598e31SEd Tanous "DBUS response error {}", 187462598e31SEd Tanous ec); 1875ac106bf6SEd Tanous messages::internalError(asyncResp->res); 1876af5d6058SSantosh Puranik return; 1877af5d6058SSantosh Puranik } 1878ac106bf6SEd Tanous asyncResp->res.jsonValue["DateTime"] = datetime; 18799ae226faSGeorge Liu }); 188083ff9ab6SJames Feist } 18819c310685SBorawski.Lukasz 188275815e5cSEd Tanous inline void 188375815e5cSEd Tanous checkForQuiesced(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 188475815e5cSEd Tanous { 188575815e5cSEd Tanous sdbusplus::asio::getProperty<std::string>( 188675815e5cSEd Tanous *crow::connections::systemBus, "org.freedesktop.systemd1", 188775815e5cSEd Tanous "/org/freedesktop/systemd1/unit/obmc-bmc-service-quiesce@0.target", 188875815e5cSEd Tanous "org.freedesktop.systemd1.Unit", "ActiveState", 188975815e5cSEd Tanous [asyncResp](const boost::system::error_code& ec, 189075815e5cSEd Tanous const std::string& val) { 189175815e5cSEd Tanous if (!ec) 189275815e5cSEd Tanous { 189375815e5cSEd Tanous if (val == "active") 189475815e5cSEd Tanous { 189575815e5cSEd Tanous asyncResp->res.jsonValue["Status"]["Health"] = "Critical"; 189675815e5cSEd Tanous asyncResp->res.jsonValue["Status"]["State"] = "Quiesced"; 189775815e5cSEd Tanous return; 189875815e5cSEd Tanous } 189975815e5cSEd Tanous } 190075815e5cSEd Tanous asyncResp->res.jsonValue["Status"]["Health"] = "OK"; 190175815e5cSEd Tanous asyncResp->res.jsonValue["Status"]["State"] = "Enabled"; 190275815e5cSEd Tanous }); 190375815e5cSEd Tanous } 190475815e5cSEd Tanous 19057e860f15SJohn Edward Broadbent inline void requestRoutesManager(App& app) 19067e860f15SJohn Edward Broadbent { 19077e860f15SJohn Edward Broadbent std::string uuid = persistent_data::getConfig().systemUuid; 19089c310685SBorawski.Lukasz 19097e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/") 1910ed398213SEd Tanous .privileges(redfish::privileges::getManager) 1911002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 1912002d39b4SEd Tanous [&app, uuid](const crow::Request& req, 191345ca1b86SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 19143ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 191545ca1b86SEd Tanous { 191645ca1b86SEd Tanous return; 191745ca1b86SEd Tanous } 19187e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc"; 1919a51fc2d2SSui Chen asyncResp->res.jsonValue["@odata.type"] = "#Manager.v1_14_0.Manager"; 19207e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["Id"] = "bmc"; 19217e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["Name"] = "OpenBmc Manager"; 19227e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["Description"] = 19237e860f15SJohn Edward Broadbent "Baseboard Management Controller"; 19247e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["PowerState"] = "On"; 19251476687dSEd Tanous 19267e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["ManagerType"] = "BMC"; 19277e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid(); 19287e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid; 1929002d39b4SEd Tanous asyncResp->res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model 19307e860f15SJohn Edward Broadbent 19311476687dSEd Tanous asyncResp->res.jsonValue["LogServices"]["@odata.id"] = 19321476687dSEd Tanous "/redfish/v1/Managers/bmc/LogServices"; 19331476687dSEd Tanous asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] = 19341476687dSEd Tanous "/redfish/v1/Managers/bmc/NetworkProtocol"; 19351476687dSEd Tanous asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] = 19361476687dSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces"; 19377e860f15SJohn Edward Broadbent 19387e860f15SJohn Edward Broadbent #ifdef BMCWEB_ENABLE_VM_NBDPROXY 19391476687dSEd Tanous asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] = 19401476687dSEd Tanous "/redfish/v1/Managers/bmc/VirtualMedia"; 19417e860f15SJohn Edward Broadbent #endif // BMCWEB_ENABLE_VM_NBDPROXY 19427e860f15SJohn Edward Broadbent 19437e860f15SJohn Edward Broadbent // default oem data 19447e860f15SJohn Edward Broadbent nlohmann::json& oem = asyncResp->res.jsonValue["Oem"]; 19457e860f15SJohn Edward Broadbent nlohmann::json& oemOpenbmc = oem["OpenBmc"]; 19467e860f15SJohn Edward Broadbent oem["@odata.type"] = "#OemManager.Oem"; 19477e860f15SJohn Edward Broadbent oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem"; 19487e860f15SJohn Edward Broadbent oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc"; 19497e860f15SJohn Edward Broadbent oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc"; 19501476687dSEd Tanous 19511476687dSEd Tanous nlohmann::json::object_t certificates; 19521476687dSEd Tanous certificates["@odata.id"] = 19531476687dSEd Tanous "/redfish/v1/Managers/bmc/Truststore/Certificates"; 19541476687dSEd Tanous oemOpenbmc["Certificates"] = std::move(certificates); 19557e860f15SJohn Edward Broadbent 19567e860f15SJohn Edward Broadbent // Manager.Reset (an action) can be many values, OpenBMC only 19577e860f15SJohn Edward Broadbent // supports BMC reboot. 19587e860f15SJohn Edward Broadbent nlohmann::json& managerReset = 19597e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["Actions"]["#Manager.Reset"]; 19607e860f15SJohn Edward Broadbent managerReset["target"] = 19617e860f15SJohn Edward Broadbent "/redfish/v1/Managers/bmc/Actions/Manager.Reset"; 19627e860f15SJohn Edward Broadbent managerReset["@Redfish.ActionInfo"] = 19637e860f15SJohn Edward Broadbent "/redfish/v1/Managers/bmc/ResetActionInfo"; 19647e860f15SJohn Edward Broadbent 19657e860f15SJohn Edward Broadbent // ResetToDefaults (Factory Reset) has values like 19667e860f15SJohn Edward Broadbent // PreserveNetworkAndUsers and PreserveNetwork that aren't supported 19677e860f15SJohn Edward Broadbent // on OpenBMC 19687e860f15SJohn Edward Broadbent nlohmann::json& resetToDefaults = 19697e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"]; 19707e860f15SJohn Edward Broadbent resetToDefaults["target"] = 19717e860f15SJohn Edward Broadbent "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults"; 1972613dabeaSEd Tanous resetToDefaults["ResetType@Redfish.AllowableValues"] = 1973613dabeaSEd Tanous nlohmann::json::array_t({"ResetAll"}); 19747e860f15SJohn Edward Broadbent 19757c8c4058STejas Patil std::pair<std::string, std::string> redfishDateTimeOffset = 19762b82937eSEd Tanous redfish::time_utils::getDateTimeOffsetNow(); 19777c8c4058STejas Patil 19787c8c4058STejas Patil asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first; 19797c8c4058STejas Patil asyncResp->res.jsonValue["DateTimeLocalOffset"] = 19807c8c4058STejas Patil redfishDateTimeOffset.second; 19817e860f15SJohn Edward Broadbent 19820e8ac5e7SGunnar Mills // TODO (Gunnar): Remove these one day since moved to ComputerSystem 19830e8ac5e7SGunnar Mills // Still used by OCP profiles 19840e8ac5e7SGunnar Mills // https://github.com/opencomputeproject/OCP-Profiles/issues/23 19857e860f15SJohn Edward Broadbent // Fill in SerialConsole info 19867e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true; 1987002d39b4SEd Tanous asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15; 1988613dabeaSEd Tanous asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] = 1989613dabeaSEd Tanous nlohmann::json::array_t({"IPMI", "SSH"}); 19907e860f15SJohn Edward Broadbent #ifdef BMCWEB_ENABLE_KVM 19917e860f15SJohn Edward Broadbent // Fill in GraphicalConsole info 1992002d39b4SEd Tanous asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true; 1993002d39b4SEd Tanous asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 1994002d39b4SEd Tanous 4; 1995613dabeaSEd Tanous asyncResp->res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] = 1996613dabeaSEd Tanous nlohmann::json::array_t({"KVMIP"}); 19977e860f15SJohn Edward Broadbent #endif // BMCWEB_ENABLE_KVM 19987f3e84a1SEd Tanous if constexpr (!bmcwebEnableMultiHost) 19997f3e84a1SEd Tanous { 20007f3e84a1SEd Tanous asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] = 20017f3e84a1SEd Tanous 1; 20021476687dSEd Tanous 20031476687dSEd Tanous nlohmann::json::array_t managerForServers; 20041476687dSEd Tanous nlohmann::json::object_t manager; 20051476687dSEd Tanous manager["@odata.id"] = "/redfish/v1/Systems/system"; 2006ad539545SPatrick Williams managerForServers.emplace_back(std::move(manager)); 20071476687dSEd Tanous 20081476687dSEd Tanous asyncResp->res.jsonValue["Links"]["ManagerForServers"] = 20091476687dSEd Tanous std::move(managerForServers); 20107f3e84a1SEd Tanous } 201113451e39SWilly Tu if constexpr (bmcwebEnableHealthPopulate) 201213451e39SWilly Tu { 20137e860f15SJohn Edward Broadbent auto health = std::make_shared<HealthPopulate>(asyncResp); 20147e860f15SJohn Edward Broadbent health->isManagersHealth = true; 20157e860f15SJohn Edward Broadbent health->populate(); 201613451e39SWilly Tu } 20177e860f15SJohn Edward Broadbent 2018eee0013eSWilly Tu sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose, 20197e860f15SJohn Edward Broadbent "FirmwareVersion", true); 20207e860f15SJohn Edward Broadbent 20217e860f15SJohn Edward Broadbent managerGetLastResetTime(asyncResp); 20227e860f15SJohn Edward Broadbent 2023a51fc2d2SSui Chen // ManagerDiagnosticData is added for all BMCs. 2024a51fc2d2SSui Chen nlohmann::json& managerDiagnosticData = 2025a51fc2d2SSui Chen asyncResp->res.jsonValue["ManagerDiagnosticData"]; 2026a51fc2d2SSui Chen managerDiagnosticData["@odata.id"] = 2027a51fc2d2SSui Chen "/redfish/v1/Managers/bmc/ManagerDiagnosticData"; 2028a51fc2d2SSui Chen 202954dce7f5SGunnar Mills #ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA 20307e860f15SJohn Edward Broadbent auto pids = std::make_shared<GetPIDValues>(asyncResp); 20317e860f15SJohn Edward Broadbent pids->run(); 203254dce7f5SGunnar Mills #endif 20337e860f15SJohn Edward Broadbent 2034002d39b4SEd Tanous getMainChassisId(asyncResp, 2035002d39b4SEd Tanous [](const std::string& chassisId, 2036002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& aRsp) { 2037002d39b4SEd Tanous aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1; 20381476687dSEd Tanous nlohmann::json::array_t managerForChassis; 20398a592810SEd Tanous nlohmann::json::object_t managerObj; 2040ef4c65b7SEd Tanous boost::urls::url chassiUrl = 2041ef4c65b7SEd Tanous boost::urls::format("/redfish/v1/Chassis/{}", chassisId); 2042eddfc437SWilly Tu managerObj["@odata.id"] = chassiUrl; 2043ad539545SPatrick Williams managerForChassis.emplace_back(std::move(managerObj)); 20441476687dSEd Tanous aRsp->res.jsonValue["Links"]["ManagerForChassis"] = 20451476687dSEd Tanous std::move(managerForChassis); 20461476687dSEd Tanous aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] = 2047eddfc437SWilly Tu chassiUrl; 20487e860f15SJohn Edward Broadbent }); 20497e860f15SJohn Edward Broadbent 20501e1e598dSJonathan Doman sdbusplus::asio::getProperty<double>( 20511e1e598dSJonathan Doman *crow::connections::systemBus, "org.freedesktop.systemd1", 2052002d39b4SEd Tanous "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", 2053002d39b4SEd Tanous "Progress", 205475815e5cSEd Tanous [asyncResp](const boost::system::error_code& ec, double val) { 20557e860f15SJohn Edward Broadbent if (ec) 20561abe55efSEd Tanous { 205762598e31SEd Tanous BMCWEB_LOG_ERROR("Error while getting progress"); 20587e860f15SJohn Edward Broadbent messages::internalError(asyncResp->res); 20597e860f15SJohn Edward Broadbent return; 20607e860f15SJohn Edward Broadbent } 20611e1e598dSJonathan Doman if (val < 1.0) 20627e860f15SJohn Edward Broadbent { 206375815e5cSEd Tanous asyncResp->res.jsonValue["Status"]["Health"] = "OK"; 2064002d39b4SEd Tanous asyncResp->res.jsonValue["Status"]["State"] = "Starting"; 206575815e5cSEd Tanous return; 20667e860f15SJohn Edward Broadbent } 206775815e5cSEd Tanous checkForQuiesced(asyncResp); 20681e1e598dSJonathan Doman }); 20699c310685SBorawski.Lukasz 2070e99073f5SGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 2071e99073f5SGeorge Liu "xyz.openbmc_project.Inventory.Item.Bmc"}; 2072e99073f5SGeorge Liu dbus::utility::getSubTree( 2073e99073f5SGeorge Liu "/xyz/openbmc_project/inventory", 0, interfaces, 20747e860f15SJohn Edward Broadbent [asyncResp]( 2075e99073f5SGeorge Liu const boost::system::error_code& ec, 2076b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 20777e860f15SJohn Edward Broadbent if (ec) 20781abe55efSEd Tanous { 207962598e31SEd Tanous BMCWEB_LOG_DEBUG("D-Bus response error on GetSubTree {}", ec); 20807e860f15SJohn Edward Broadbent return; 20817e860f15SJohn Edward Broadbent } 208226f6976fSEd Tanous if (subtree.empty()) 20837e860f15SJohn Edward Broadbent { 208462598e31SEd Tanous BMCWEB_LOG_DEBUG("Can't find bmc D-Bus object!"); 20857e860f15SJohn Edward Broadbent return; 20867e860f15SJohn Edward Broadbent } 20877e860f15SJohn Edward Broadbent // Assume only 1 bmc D-Bus object 20887e860f15SJohn Edward Broadbent // Throw an error if there is more than 1 20897e860f15SJohn Edward Broadbent if (subtree.size() > 1) 20907e860f15SJohn Edward Broadbent { 209162598e31SEd Tanous BMCWEB_LOG_DEBUG("Found more than 1 bmc D-Bus object!"); 20927e860f15SJohn Edward Broadbent messages::internalError(asyncResp->res); 20937e860f15SJohn Edward Broadbent return; 20947e860f15SJohn Edward Broadbent } 20957e860f15SJohn Edward Broadbent 2096002d39b4SEd Tanous if (subtree[0].first.empty() || subtree[0].second.size() != 1) 20977e860f15SJohn Edward Broadbent { 209862598e31SEd Tanous BMCWEB_LOG_DEBUG("Error getting bmc D-Bus object!"); 20997e860f15SJohn Edward Broadbent messages::internalError(asyncResp->res); 21007e860f15SJohn Edward Broadbent return; 21017e860f15SJohn Edward Broadbent } 21027e860f15SJohn Edward Broadbent 21037e860f15SJohn Edward Broadbent const std::string& path = subtree[0].first; 2104002d39b4SEd Tanous const std::string& connectionName = subtree[0].second[0].first; 21057e860f15SJohn Edward Broadbent 2106002d39b4SEd Tanous for (const auto& interfaceName : subtree[0].second[0].second) 21077e860f15SJohn Edward Broadbent { 21087e860f15SJohn Edward Broadbent if (interfaceName == 21097e860f15SJohn Edward Broadbent "xyz.openbmc_project.Inventory.Decorator.Asset") 21107e860f15SJohn Edward Broadbent { 2111fac6e53bSKrzysztof Grobelny sdbusplus::asio::getAllProperties( 2112fac6e53bSKrzysztof Grobelny *crow::connections::systemBus, connectionName, path, 2113fac6e53bSKrzysztof Grobelny "xyz.openbmc_project.Inventory.Decorator.Asset", 21145e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec2, 2115b9d36b47SEd Tanous const dbus::utility::DBusPropertiesMap& 21167e860f15SJohn Edward Broadbent propertiesList) { 21178a592810SEd Tanous if (ec2) 21187e860f15SJohn Edward Broadbent { 211962598e31SEd Tanous BMCWEB_LOG_DEBUG("Can't get bmc asset!"); 21207e860f15SJohn Edward Broadbent return; 21217e860f15SJohn Edward Broadbent } 21227e860f15SJohn Edward Broadbent 2123fac6e53bSKrzysztof Grobelny const std::string* partNumber = nullptr; 2124fac6e53bSKrzysztof Grobelny const std::string* serialNumber = nullptr; 2125fac6e53bSKrzysztof Grobelny const std::string* manufacturer = nullptr; 2126fac6e53bSKrzysztof Grobelny const std::string* model = nullptr; 2127fac6e53bSKrzysztof Grobelny const std::string* sparePartNumber = nullptr; 2128fac6e53bSKrzysztof Grobelny 2129fac6e53bSKrzysztof Grobelny const bool success = sdbusplus::unpackPropertiesNoThrow( 2130fac6e53bSKrzysztof Grobelny dbus_utils::UnpackErrorPrinter(), propertiesList, 2131fac6e53bSKrzysztof Grobelny "PartNumber", partNumber, "SerialNumber", 2132fac6e53bSKrzysztof Grobelny serialNumber, "Manufacturer", manufacturer, "Model", 2133fac6e53bSKrzysztof Grobelny model, "SparePartNumber", sparePartNumber); 2134fac6e53bSKrzysztof Grobelny 2135fac6e53bSKrzysztof Grobelny if (!success) 21367e860f15SJohn Edward Broadbent { 2137002d39b4SEd Tanous messages::internalError(asyncResp->res); 21387e860f15SJohn Edward Broadbent return; 21397e860f15SJohn Edward Broadbent } 2140fac6e53bSKrzysztof Grobelny 2141fac6e53bSKrzysztof Grobelny if (partNumber != nullptr) 2142fac6e53bSKrzysztof Grobelny { 2143fac6e53bSKrzysztof Grobelny asyncResp->res.jsonValue["PartNumber"] = 2144fac6e53bSKrzysztof Grobelny *partNumber; 21457e860f15SJohn Edward Broadbent } 2146fac6e53bSKrzysztof Grobelny 2147fac6e53bSKrzysztof Grobelny if (serialNumber != nullptr) 2148fac6e53bSKrzysztof Grobelny { 2149fac6e53bSKrzysztof Grobelny asyncResp->res.jsonValue["SerialNumber"] = 2150fac6e53bSKrzysztof Grobelny *serialNumber; 21517e860f15SJohn Edward Broadbent } 2152fac6e53bSKrzysztof Grobelny 2153fac6e53bSKrzysztof Grobelny if (manufacturer != nullptr) 2154fac6e53bSKrzysztof Grobelny { 2155fac6e53bSKrzysztof Grobelny asyncResp->res.jsonValue["Manufacturer"] = 2156fac6e53bSKrzysztof Grobelny *manufacturer; 2157fac6e53bSKrzysztof Grobelny } 2158fac6e53bSKrzysztof Grobelny 2159fac6e53bSKrzysztof Grobelny if (model != nullptr) 2160fac6e53bSKrzysztof Grobelny { 2161fac6e53bSKrzysztof Grobelny asyncResp->res.jsonValue["Model"] = *model; 2162fac6e53bSKrzysztof Grobelny } 2163fac6e53bSKrzysztof Grobelny 2164fac6e53bSKrzysztof Grobelny if (sparePartNumber != nullptr) 2165fac6e53bSKrzysztof Grobelny { 2166fac6e53bSKrzysztof Grobelny asyncResp->res.jsonValue["SparePartNumber"] = 2167fac6e53bSKrzysztof Grobelny *sparePartNumber; 2168fac6e53bSKrzysztof Grobelny } 2169fac6e53bSKrzysztof Grobelny }); 21707e860f15SJohn Edward Broadbent } 2171002d39b4SEd Tanous else if (interfaceName == 21720fda0f12SGeorge Liu "xyz.openbmc_project.Inventory.Decorator.LocationCode") 21737e860f15SJohn Edward Broadbent { 21747e860f15SJohn Edward Broadbent getLocation(asyncResp, connectionName, path); 21757e860f15SJohn Edward Broadbent } 21767e860f15SJohn Edward Broadbent } 2177e99073f5SGeorge Liu }); 21787e860f15SJohn Edward Broadbent }); 21797e860f15SJohn Edward Broadbent 21807e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/") 2181ed398213SEd Tanous .privileges(redfish::privileges::patchManager) 218245ca1b86SEd Tanous .methods(boost::beast::http::verb::patch)( 218345ca1b86SEd Tanous [&app](const crow::Request& req, 21847e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 21853ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 218645ca1b86SEd Tanous { 218745ca1b86SEd Tanous return; 218845ca1b86SEd Tanous } 21897e860f15SJohn Edward Broadbent std::optional<nlohmann::json> oem; 21907e860f15SJohn Edward Broadbent std::optional<nlohmann::json> links; 21917e860f15SJohn Edward Broadbent std::optional<std::string> datetime; 21927e860f15SJohn Edward Broadbent 219315ed6780SWilly Tu if (!json_util::readJsonPatch(req, asyncResp->res, "Oem", oem, 2194002d39b4SEd Tanous "DateTime", datetime, "Links", links)) 21957e860f15SJohn Edward Broadbent { 21967e860f15SJohn Edward Broadbent return; 21977e860f15SJohn Edward Broadbent } 21987e860f15SJohn Edward Broadbent 21997e860f15SJohn Edward Broadbent if (oem) 22007e860f15SJohn Edward Broadbent { 220154dce7f5SGunnar Mills #ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA 22027e860f15SJohn Edward Broadbent std::optional<nlohmann::json> openbmc; 2203002d39b4SEd Tanous if (!redfish::json_util::readJson(*oem, asyncResp->res, "OpenBmc", 2204002d39b4SEd Tanous openbmc)) 22057e860f15SJohn Edward Broadbent { 22067e860f15SJohn Edward Broadbent return; 22077e860f15SJohn Edward Broadbent } 22087e860f15SJohn Edward Broadbent if (openbmc) 22097e860f15SJohn Edward Broadbent { 22107e860f15SJohn Edward Broadbent std::optional<nlohmann::json> fan; 2211002d39b4SEd Tanous if (!redfish::json_util::readJson(*openbmc, asyncResp->res, 2212002d39b4SEd Tanous "Fan", fan)) 22137e860f15SJohn Edward Broadbent { 22147e860f15SJohn Edward Broadbent return; 22157e860f15SJohn Edward Broadbent } 22167e860f15SJohn Edward Broadbent if (fan) 22177e860f15SJohn Edward Broadbent { 2218002d39b4SEd Tanous auto pid = std::make_shared<SetPIDValues>(asyncResp, *fan); 22197e860f15SJohn Edward Broadbent pid->run(); 22207e860f15SJohn Edward Broadbent } 22217e860f15SJohn Edward Broadbent } 222254dce7f5SGunnar Mills #else 222354dce7f5SGunnar Mills messages::propertyUnknown(asyncResp->res, "Oem"); 222454dce7f5SGunnar Mills return; 222554dce7f5SGunnar Mills #endif 22267e860f15SJohn Edward Broadbent } 22277e860f15SJohn Edward Broadbent if (links) 22287e860f15SJohn Edward Broadbent { 22297e860f15SJohn Edward Broadbent std::optional<nlohmann::json> activeSoftwareImage; 22307e860f15SJohn Edward Broadbent if (!redfish::json_util::readJson(*links, asyncResp->res, 22317e860f15SJohn Edward Broadbent "ActiveSoftwareImage", 22327e860f15SJohn Edward Broadbent activeSoftwareImage)) 22337e860f15SJohn Edward Broadbent { 22347e860f15SJohn Edward Broadbent return; 22357e860f15SJohn Edward Broadbent } 22367e860f15SJohn Edward Broadbent if (activeSoftwareImage) 22377e860f15SJohn Edward Broadbent { 22387e860f15SJohn Edward Broadbent std::optional<std::string> odataId; 2239002d39b4SEd Tanous if (!json_util::readJson(*activeSoftwareImage, asyncResp->res, 2240002d39b4SEd Tanous "@odata.id", odataId)) 22417e860f15SJohn Edward Broadbent { 22427e860f15SJohn Edward Broadbent return; 22437e860f15SJohn Edward Broadbent } 22447e860f15SJohn Edward Broadbent 22457e860f15SJohn Edward Broadbent if (odataId) 22467e860f15SJohn Edward Broadbent { 22477e860f15SJohn Edward Broadbent setActiveFirmwareImage(asyncResp, *odataId); 22487e860f15SJohn Edward Broadbent } 22497e860f15SJohn Edward Broadbent } 22507e860f15SJohn Edward Broadbent } 22517e860f15SJohn Edward Broadbent if (datetime) 22527e860f15SJohn Edward Broadbent { 22537e860f15SJohn Edward Broadbent setDateTime(asyncResp, std::move(*datetime)); 22547e860f15SJohn Edward Broadbent } 22557e860f15SJohn Edward Broadbent }); 22567e860f15SJohn Edward Broadbent } 22577e860f15SJohn Edward Broadbent 22587e860f15SJohn Edward Broadbent inline void requestRoutesManagerCollection(App& app) 22597e860f15SJohn Edward Broadbent { 22607e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Managers/") 2261ed398213SEd Tanous .privileges(redfish::privileges::getManagerCollection) 22627e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 226345ca1b86SEd Tanous [&app](const crow::Request& req, 22647e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 22653ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 226645ca1b86SEd Tanous { 226745ca1b86SEd Tanous return; 226845ca1b86SEd Tanous } 226983ff9ab6SJames Feist // Collections don't include the static data added by SubRoute 227083ff9ab6SJames Feist // because it has a duplicate entry for members 22718d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers"; 22728d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 22738d1b46d7Szhanghch05 "#ManagerCollection.ManagerCollection"; 22748d1b46d7Szhanghch05 asyncResp->res.jsonValue["Name"] = "Manager Collection"; 22758d1b46d7Szhanghch05 asyncResp->res.jsonValue["Members@odata.count"] = 1; 22761476687dSEd Tanous nlohmann::json::array_t members; 22771476687dSEd Tanous nlohmann::json& bmc = members.emplace_back(); 22781476687dSEd Tanous bmc["@odata.id"] = "/redfish/v1/Managers/bmc"; 22791476687dSEd Tanous asyncResp->res.jsonValue["Members"] = std::move(members); 22807e860f15SJohn Edward Broadbent }); 22819c310685SBorawski.Lukasz } 22829c310685SBorawski.Lukasz } // namespace redfish 2283