19c310685SBorawski.Lukasz /* 29c310685SBorawski.Lukasz // Copyright (c) 2018 Intel Corporation 39c310685SBorawski.Lukasz // 49c310685SBorawski.Lukasz // Licensed under the Apache License, Version 2.0 (the "License"); 59c310685SBorawski.Lukasz // you may not use this file except in compliance with the License. 69c310685SBorawski.Lukasz // You may obtain a copy of the License at 79c310685SBorawski.Lukasz // 89c310685SBorawski.Lukasz // http://www.apache.org/licenses/LICENSE-2.0 99c310685SBorawski.Lukasz // 109c310685SBorawski.Lukasz // Unless required by applicable law or agreed to in writing, software 119c310685SBorawski.Lukasz // distributed under the License is distributed on an "AS IS" BASIS, 129c310685SBorawski.Lukasz // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139c310685SBorawski.Lukasz // See the License for the specific language governing permissions and 149c310685SBorawski.Lukasz // limitations under the License. 159c310685SBorawski.Lukasz */ 169c310685SBorawski.Lukasz #pragma once 179c310685SBorawski.Lukasz 18b49ac873SJames Feist #include "health.hpp" 199c310685SBorawski.Lukasz #include "node.hpp" 20c5d03ff4SJennifer Lee #include "redfish_util.hpp" 219c310685SBorawski.Lukasz 225b4aa86bSJames Feist #include <boost/algorithm/string/replace.hpp> 23af5d6058SSantosh Puranik #include <boost/date_time.hpp> 245b4aa86bSJames Feist #include <dbus_utility.hpp> 25e90c5052SAndrew Geissler #include <utils/fw_utils.hpp> 267bffdb7eSBernard Wong #include <utils/systemd_utils.hpp> 271214b7e7SGunnar Mills 284bfefa74SGunnar Mills #include <cstdint> 291214b7e7SGunnar Mills #include <memory> 301214b7e7SGunnar Mills #include <sstream> 31abf2add6SEd Tanous #include <variant> 325b4aa86bSJames Feist 331abe55efSEd Tanous namespace redfish 341abe55efSEd Tanous { 35ed5befbdSJennifer Lee 36ed5befbdSJennifer Lee /** 372a5c4407SGunnar Mills * Function reboots the BMC. 382a5c4407SGunnar Mills * 392a5c4407SGunnar Mills * @param[in] asyncResp - Shared pointer for completing asynchronous calls 40ed5befbdSJennifer Lee */ 41b5a76932SEd Tanous inline void doBMCGracefulRestart(const std::shared_ptr<AsyncResp>& asyncResp) 42ed5befbdSJennifer Lee { 43ed5befbdSJennifer Lee const char* processName = "xyz.openbmc_project.State.BMC"; 44ed5befbdSJennifer Lee const char* objectPath = "/xyz/openbmc_project/state/bmc0"; 45ed5befbdSJennifer Lee const char* interfaceName = "xyz.openbmc_project.State.BMC"; 46ed5befbdSJennifer Lee const std::string& propertyValue = 47ed5befbdSJennifer Lee "xyz.openbmc_project.State.BMC.Transition.Reboot"; 48ed5befbdSJennifer Lee const char* destProperty = "RequestedBMCTransition"; 49ed5befbdSJennifer Lee 50ed5befbdSJennifer Lee // Create the D-Bus variant for D-Bus call. 51ed5befbdSJennifer Lee VariantType dbusPropertyValue(propertyValue); 52ed5befbdSJennifer Lee 53ed5befbdSJennifer Lee crow::connections::systemBus->async_method_call( 54ed5befbdSJennifer Lee [asyncResp](const boost::system::error_code ec) { 55ed5befbdSJennifer Lee // Use "Set" method to set the property value. 56ed5befbdSJennifer Lee if (ec) 57ed5befbdSJennifer Lee { 582a5c4407SGunnar Mills BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec; 59ed5befbdSJennifer Lee messages::internalError(asyncResp->res); 60ed5befbdSJennifer Lee return; 61ed5befbdSJennifer Lee } 62ed5befbdSJennifer Lee 63ed5befbdSJennifer Lee messages::success(asyncResp->res); 64ed5befbdSJennifer Lee }, 65ed5befbdSJennifer Lee processName, objectPath, "org.freedesktop.DBus.Properties", "Set", 66ed5befbdSJennifer Lee interfaceName, destProperty, dbusPropertyValue); 67ed5befbdSJennifer Lee } 682a5c4407SGunnar Mills 69b5a76932SEd Tanous inline void doBMCForceRestart(const std::shared_ptr<AsyncResp>& asyncResp) 70f92af389SJayaprakash Mutyala { 71f92af389SJayaprakash Mutyala const char* processName = "xyz.openbmc_project.State.BMC"; 72f92af389SJayaprakash Mutyala const char* objectPath = "/xyz/openbmc_project/state/bmc0"; 73f92af389SJayaprakash Mutyala const char* interfaceName = "xyz.openbmc_project.State.BMC"; 74f92af389SJayaprakash Mutyala const std::string& propertyValue = 75f92af389SJayaprakash Mutyala "xyz.openbmc_project.State.BMC.Transition.HardReboot"; 76f92af389SJayaprakash Mutyala const char* destProperty = "RequestedBMCTransition"; 77f92af389SJayaprakash Mutyala 78f92af389SJayaprakash Mutyala // Create the D-Bus variant for D-Bus call. 79f92af389SJayaprakash Mutyala VariantType dbusPropertyValue(propertyValue); 80f92af389SJayaprakash Mutyala 81f92af389SJayaprakash Mutyala crow::connections::systemBus->async_method_call( 82f92af389SJayaprakash Mutyala [asyncResp](const boost::system::error_code ec) { 83f92af389SJayaprakash Mutyala // Use "Set" method to set the property value. 84f92af389SJayaprakash Mutyala if (ec) 85f92af389SJayaprakash Mutyala { 86f92af389SJayaprakash Mutyala BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec; 87f92af389SJayaprakash Mutyala messages::internalError(asyncResp->res); 88f92af389SJayaprakash Mutyala return; 89f92af389SJayaprakash Mutyala } 90f92af389SJayaprakash Mutyala 91f92af389SJayaprakash Mutyala messages::success(asyncResp->res); 92f92af389SJayaprakash Mutyala }, 93f92af389SJayaprakash Mutyala processName, objectPath, "org.freedesktop.DBus.Properties", "Set", 94f92af389SJayaprakash Mutyala interfaceName, destProperty, dbusPropertyValue); 95f92af389SJayaprakash Mutyala } 96f92af389SJayaprakash Mutyala 972a5c4407SGunnar Mills /** 982a5c4407SGunnar Mills * ManagerResetAction class supports the POST method for the Reset (reboot) 992a5c4407SGunnar Mills * action. 1002a5c4407SGunnar Mills */ 1012a5c4407SGunnar Mills class ManagerResetAction : public Node 1022a5c4407SGunnar Mills { 1032a5c4407SGunnar Mills public: 10452cc112dSEd Tanous ManagerResetAction(App& app) : 1052a5c4407SGunnar Mills Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/") 1062a5c4407SGunnar Mills { 1072a5c4407SGunnar Mills entityPrivileges = { 1082a5c4407SGunnar Mills {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; 1092a5c4407SGunnar Mills } 1102a5c4407SGunnar Mills 1112a5c4407SGunnar Mills private: 1122a5c4407SGunnar Mills /** 1132a5c4407SGunnar Mills * Function handles POST method request. 1142a5c4407SGunnar Mills * Analyzes POST body before sending Reset (Reboot) request data to D-Bus. 115f92af389SJayaprakash Mutyala * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart". 1162a5c4407SGunnar Mills */ 1172a5c4407SGunnar Mills void doPost(crow::Response& res, const crow::Request& req, 118cb13a392SEd Tanous const std::vector<std::string>&) override 1192a5c4407SGunnar Mills { 1202a5c4407SGunnar Mills BMCWEB_LOG_DEBUG << "Post Manager Reset."; 1212a5c4407SGunnar Mills 1222a5c4407SGunnar Mills std::string resetType; 1232a5c4407SGunnar Mills auto asyncResp = std::make_shared<AsyncResp>(res); 1242a5c4407SGunnar Mills 1252a5c4407SGunnar Mills if (!json_util::readJson(req, asyncResp->res, "ResetType", resetType)) 1262a5c4407SGunnar Mills { 1272a5c4407SGunnar Mills return; 1282a5c4407SGunnar Mills } 1292a5c4407SGunnar Mills 130f92af389SJayaprakash Mutyala if (resetType == "GracefulRestart") 131f92af389SJayaprakash Mutyala { 132f92af389SJayaprakash Mutyala BMCWEB_LOG_DEBUG << "Proceeding with " << resetType; 133f92af389SJayaprakash Mutyala doBMCGracefulRestart(asyncResp); 134f92af389SJayaprakash Mutyala return; 135f92af389SJayaprakash Mutyala } 1363174e4dfSEd Tanous if (resetType == "ForceRestart") 137f92af389SJayaprakash Mutyala { 138f92af389SJayaprakash Mutyala BMCWEB_LOG_DEBUG << "Proceeding with " << resetType; 139f92af389SJayaprakash Mutyala doBMCForceRestart(asyncResp); 140f92af389SJayaprakash Mutyala return; 141f92af389SJayaprakash Mutyala } 1422a5c4407SGunnar Mills BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: " 1432a5c4407SGunnar Mills << resetType; 1442a5c4407SGunnar Mills messages::actionParameterNotSupported(asyncResp->res, resetType, 1452a5c4407SGunnar Mills "ResetType"); 1462a5c4407SGunnar Mills 1472a5c4407SGunnar Mills return; 1482a5c4407SGunnar Mills } 149ed5befbdSJennifer Lee }; 150ed5befbdSJennifer Lee 1513e40fc74SGunnar Mills /** 1523e40fc74SGunnar Mills * ManagerResetToDefaultsAction class supports POST method for factory reset 1533e40fc74SGunnar Mills * action. 1543e40fc74SGunnar Mills */ 1553e40fc74SGunnar Mills class ManagerResetToDefaultsAction : public Node 1563e40fc74SGunnar Mills { 1573e40fc74SGunnar Mills public: 15852cc112dSEd Tanous ManagerResetToDefaultsAction(App& app) : 1593e40fc74SGunnar Mills Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/") 1603e40fc74SGunnar Mills { 1613e40fc74SGunnar Mills entityPrivileges = { 1623e40fc74SGunnar Mills {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; 1633e40fc74SGunnar Mills } 1643e40fc74SGunnar Mills 1653e40fc74SGunnar Mills private: 1663e40fc74SGunnar Mills /** 1673e40fc74SGunnar Mills * Function handles ResetToDefaults POST method request. 1683e40fc74SGunnar Mills * 1693e40fc74SGunnar Mills * Analyzes POST body message and factory resets BMC by calling 1703e40fc74SGunnar Mills * BMC code updater factory reset followed by a BMC reboot. 1713e40fc74SGunnar Mills * 1723e40fc74SGunnar Mills * BMC code updater factory reset wipes the whole BMC read-write 1733e40fc74SGunnar Mills * filesystem which includes things like the network settings. 1743e40fc74SGunnar Mills * 1753e40fc74SGunnar Mills * OpenBMC only supports ResetToDefaultsType "ResetAll". 1763e40fc74SGunnar Mills */ 1773e40fc74SGunnar Mills void doPost(crow::Response& res, const crow::Request& req, 178cb13a392SEd Tanous const std::vector<std::string>&) override 1793e40fc74SGunnar Mills { 1803e40fc74SGunnar Mills BMCWEB_LOG_DEBUG << "Post ResetToDefaults."; 1813e40fc74SGunnar Mills 1823e40fc74SGunnar Mills std::string resetType; 1833e40fc74SGunnar Mills auto asyncResp = std::make_shared<AsyncResp>(res); 1843e40fc74SGunnar Mills 1853e40fc74SGunnar Mills if (!json_util::readJson(req, asyncResp->res, "ResetToDefaultsType", 1863e40fc74SGunnar Mills resetType)) 1873e40fc74SGunnar Mills { 1883e40fc74SGunnar Mills BMCWEB_LOG_DEBUG << "Missing property ResetToDefaultsType."; 1893e40fc74SGunnar Mills 1903e40fc74SGunnar Mills messages::actionParameterMissing(asyncResp->res, "ResetToDefaults", 1913e40fc74SGunnar Mills "ResetToDefaultsType"); 1923e40fc74SGunnar Mills return; 1933e40fc74SGunnar Mills } 1943e40fc74SGunnar Mills 1953e40fc74SGunnar Mills if (resetType != "ResetAll") 1963e40fc74SGunnar Mills { 1973e40fc74SGunnar Mills BMCWEB_LOG_DEBUG << "Invalid property value for " 1983e40fc74SGunnar Mills "ResetToDefaultsType: " 1993e40fc74SGunnar Mills << resetType; 2003e40fc74SGunnar Mills messages::actionParameterNotSupported(asyncResp->res, resetType, 2013e40fc74SGunnar Mills "ResetToDefaultsType"); 2023e40fc74SGunnar Mills return; 2033e40fc74SGunnar Mills } 2043e40fc74SGunnar Mills 2053e40fc74SGunnar Mills crow::connections::systemBus->async_method_call( 2063e40fc74SGunnar Mills [asyncResp](const boost::system::error_code ec) { 2073e40fc74SGunnar Mills if (ec) 2083e40fc74SGunnar Mills { 2093e40fc74SGunnar Mills BMCWEB_LOG_DEBUG << "Failed to ResetToDefaults: " << ec; 2103e40fc74SGunnar Mills messages::internalError(asyncResp->res); 2113e40fc74SGunnar Mills return; 2123e40fc74SGunnar Mills } 2133e40fc74SGunnar Mills // Factory Reset doesn't actually happen until a reboot 2143e40fc74SGunnar Mills // Can't erase what the BMC is running on 2153e40fc74SGunnar Mills doBMCGracefulRestart(asyncResp); 2163e40fc74SGunnar Mills }, 2173e40fc74SGunnar Mills "xyz.openbmc_project.Software.BMC.Updater", 2183e40fc74SGunnar Mills "/xyz/openbmc_project/software", 2193e40fc74SGunnar Mills "xyz.openbmc_project.Common.FactoryReset", "Reset"); 2203e40fc74SGunnar Mills } 2213e40fc74SGunnar Mills }; 2223e40fc74SGunnar Mills 2231cb1a9e6SAppaRao Puli /** 2241cb1a9e6SAppaRao Puli * ManagerResetActionInfo derived class for delivering Manager 2251cb1a9e6SAppaRao Puli * ResetType AllowableValues using ResetInfo schema. 2261cb1a9e6SAppaRao Puli */ 2271cb1a9e6SAppaRao Puli class ManagerResetActionInfo : public Node 2281cb1a9e6SAppaRao Puli { 2291cb1a9e6SAppaRao Puli public: 2301cb1a9e6SAppaRao Puli /* 2311cb1a9e6SAppaRao Puli * Default Constructor 2321cb1a9e6SAppaRao Puli */ 23352cc112dSEd Tanous ManagerResetActionInfo(App& app) : 2341cb1a9e6SAppaRao Puli Node(app, "/redfish/v1/Managers/bmc/ResetActionInfo/") 2351cb1a9e6SAppaRao Puli { 2361cb1a9e6SAppaRao Puli entityPrivileges = { 2371cb1a9e6SAppaRao Puli {boost::beast::http::verb::get, {{"Login"}}}, 2381cb1a9e6SAppaRao Puli {boost::beast::http::verb::head, {{"Login"}}}, 2391cb1a9e6SAppaRao Puli {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 2401cb1a9e6SAppaRao Puli {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 2411cb1a9e6SAppaRao Puli {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 2421cb1a9e6SAppaRao Puli {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 2431cb1a9e6SAppaRao Puli } 2441cb1a9e6SAppaRao Puli 2451cb1a9e6SAppaRao Puli private: 2461cb1a9e6SAppaRao Puli /** 2471cb1a9e6SAppaRao Puli * Functions triggers appropriate requests on DBus 2481cb1a9e6SAppaRao Puli */ 249cb13a392SEd Tanous void doGet(crow::Response& res, const crow::Request&, 250cb13a392SEd Tanous const std::vector<std::string>&) override 2511cb1a9e6SAppaRao Puli { 2521cb1a9e6SAppaRao Puli res.jsonValue = { 2531cb1a9e6SAppaRao Puli {"@odata.type", "#ActionInfo.v1_1_2.ActionInfo"}, 2541cb1a9e6SAppaRao Puli {"@odata.id", "/redfish/v1/Managers/bmc/ResetActionInfo"}, 2551cb1a9e6SAppaRao Puli {"Name", "Reset Action Info"}, 2561cb1a9e6SAppaRao Puli {"Id", "ResetActionInfo"}, 2571cb1a9e6SAppaRao Puli {"Parameters", 2581cb1a9e6SAppaRao Puli {{{"Name", "ResetType"}, 2591cb1a9e6SAppaRao Puli {"Required", true}, 2601cb1a9e6SAppaRao Puli {"DataType", "String"}, 261f92af389SJayaprakash Mutyala {"AllowableValues", {"GracefulRestart", "ForceRestart"}}}}}}; 2621cb1a9e6SAppaRao Puli res.end(); 2631cb1a9e6SAppaRao Puli } 2641cb1a9e6SAppaRao Puli }; 2651cb1a9e6SAppaRao Puli 2665b4aa86bSJames Feist static constexpr const char* objectManagerIface = 2675b4aa86bSJames Feist "org.freedesktop.DBus.ObjectManager"; 2685b4aa86bSJames Feist static constexpr const char* pidConfigurationIface = 2695b4aa86bSJames Feist "xyz.openbmc_project.Configuration.Pid"; 2705b4aa86bSJames Feist static constexpr const char* pidZoneConfigurationIface = 2715b4aa86bSJames Feist "xyz.openbmc_project.Configuration.Pid.Zone"; 272b7a08d04SJames Feist static constexpr const char* stepwiseConfigurationIface = 273b7a08d04SJames Feist "xyz.openbmc_project.Configuration.Stepwise"; 27473df0db0SJames Feist static constexpr const char* thermalModeIface = 27573df0db0SJames Feist "xyz.openbmc_project.Control.ThermalMode"; 2769c310685SBorawski.Lukasz 27723a21a1cSEd Tanous inline void asyncPopulatePid(const std::string& connection, 2785b4aa86bSJames Feist const std::string& path, 27973df0db0SJames Feist const std::string& currentProfile, 28073df0db0SJames Feist const std::vector<std::string>& supportedProfiles, 281b5a76932SEd Tanous const std::shared_ptr<AsyncResp>& asyncResp) 2825b4aa86bSJames Feist { 2835b4aa86bSJames Feist 2845b4aa86bSJames Feist crow::connections::systemBus->async_method_call( 28573df0db0SJames Feist [asyncResp, currentProfile, supportedProfiles]( 28673df0db0SJames Feist const boost::system::error_code ec, 2875b4aa86bSJames Feist const dbus::utility::ManagedObjectType& managedObj) { 2885b4aa86bSJames Feist if (ec) 2895b4aa86bSJames Feist { 2905b4aa86bSJames Feist BMCWEB_LOG_ERROR << ec; 2915b4aa86bSJames Feist asyncResp->res.jsonValue.clear(); 292f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2935b4aa86bSJames Feist return; 2945b4aa86bSJames Feist } 2955b4aa86bSJames Feist nlohmann::json& configRoot = 2965b4aa86bSJames Feist asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"]; 2975b4aa86bSJames Feist nlohmann::json& fans = configRoot["FanControllers"]; 2985b4aa86bSJames Feist fans["@odata.type"] = "#OemManager.FanControllers"; 2995b4aa86bSJames Feist fans["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/" 3005b4aa86bSJames Feist "Fan/FanControllers"; 3015b4aa86bSJames Feist 3025b4aa86bSJames Feist nlohmann::json& pids = configRoot["PidControllers"]; 3035b4aa86bSJames Feist pids["@odata.type"] = "#OemManager.PidControllers"; 3045b4aa86bSJames Feist pids["@odata.id"] = 3055b4aa86bSJames Feist "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers"; 3065b4aa86bSJames Feist 307b7a08d04SJames Feist nlohmann::json& stepwise = configRoot["StepwiseControllers"]; 308b7a08d04SJames Feist stepwise["@odata.type"] = "#OemManager.StepwiseControllers"; 309b7a08d04SJames Feist stepwise["@odata.id"] = 310b7a08d04SJames Feist "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers"; 311b7a08d04SJames Feist 3125b4aa86bSJames Feist nlohmann::json& zones = configRoot["FanZones"]; 3135b4aa86bSJames Feist zones["@odata.id"] = 3145b4aa86bSJames Feist "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones"; 3155b4aa86bSJames Feist zones["@odata.type"] = "#OemManager.FanZones"; 3165b4aa86bSJames Feist configRoot["@odata.id"] = 3175b4aa86bSJames Feist "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan"; 3185b4aa86bSJames Feist configRoot["@odata.type"] = "#OemManager.Fan"; 31973df0db0SJames Feist configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles; 32073df0db0SJames Feist 32173df0db0SJames Feist if (!currentProfile.empty()) 32273df0db0SJames Feist { 32373df0db0SJames Feist configRoot["Profile"] = currentProfile; 32473df0db0SJames Feist } 32573df0db0SJames Feist BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !"; 3265b4aa86bSJames Feist 3275b4aa86bSJames Feist for (const auto& pathPair : managedObj) 3285b4aa86bSJames Feist { 3295b4aa86bSJames Feist for (const auto& intfPair : pathPair.second) 3305b4aa86bSJames Feist { 3315b4aa86bSJames Feist if (intfPair.first != pidConfigurationIface && 332b7a08d04SJames Feist intfPair.first != pidZoneConfigurationIface && 333b7a08d04SJames Feist intfPair.first != stepwiseConfigurationIface) 3345b4aa86bSJames Feist { 3355b4aa86bSJames Feist continue; 3365b4aa86bSJames Feist } 3375b4aa86bSJames Feist auto findName = intfPair.second.find("Name"); 3385b4aa86bSJames Feist if (findName == intfPair.second.end()) 3395b4aa86bSJames Feist { 3405b4aa86bSJames Feist BMCWEB_LOG_ERROR << "Pid Field missing Name"; 341a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 3425b4aa86bSJames Feist return; 3435b4aa86bSJames Feist } 34473df0db0SJames Feist 3455b4aa86bSJames Feist const std::string* namePtr = 346abf2add6SEd Tanous std::get_if<std::string>(&findName->second); 3475b4aa86bSJames Feist if (namePtr == nullptr) 3485b4aa86bSJames Feist { 3495b4aa86bSJames Feist BMCWEB_LOG_ERROR << "Pid Name Field illegal"; 350b7a08d04SJames Feist messages::internalError(asyncResp->res); 3515b4aa86bSJames Feist return; 3525b4aa86bSJames Feist } 3535b4aa86bSJames Feist std::string name = *namePtr; 3545b4aa86bSJames Feist dbus::utility::escapePathForDbus(name); 35573df0db0SJames Feist 35673df0db0SJames Feist auto findProfiles = intfPair.second.find("Profiles"); 35773df0db0SJames Feist if (findProfiles != intfPair.second.end()) 35873df0db0SJames Feist { 35973df0db0SJames Feist const std::vector<std::string>* profiles = 36073df0db0SJames Feist std::get_if<std::vector<std::string>>( 36173df0db0SJames Feist &findProfiles->second); 36273df0db0SJames Feist if (profiles == nullptr) 36373df0db0SJames Feist { 36473df0db0SJames Feist BMCWEB_LOG_ERROR << "Pid Profiles Field illegal"; 36573df0db0SJames Feist messages::internalError(asyncResp->res); 36673df0db0SJames Feist return; 36773df0db0SJames Feist } 36873df0db0SJames Feist if (std::find(profiles->begin(), profiles->end(), 36973df0db0SJames Feist currentProfile) == profiles->end()) 37073df0db0SJames Feist { 37173df0db0SJames Feist BMCWEB_LOG_INFO 37273df0db0SJames Feist << name << " not supported in current profile"; 37373df0db0SJames Feist continue; 37473df0db0SJames Feist } 37573df0db0SJames Feist } 376b7a08d04SJames Feist nlohmann::json* config = nullptr; 377c33a90ecSJames Feist 378c33a90ecSJames Feist const std::string* classPtr = nullptr; 379c33a90ecSJames Feist auto findClass = intfPair.second.find("Class"); 380c33a90ecSJames Feist if (findClass != intfPair.second.end()) 381c33a90ecSJames Feist { 382c33a90ecSJames Feist classPtr = std::get_if<std::string>(&findClass->second); 383c33a90ecSJames Feist } 384c33a90ecSJames Feist 3855b4aa86bSJames Feist if (intfPair.first == pidZoneConfigurationIface) 3865b4aa86bSJames Feist { 3875b4aa86bSJames Feist std::string chassis; 3885b4aa86bSJames Feist if (!dbus::utility::getNthStringFromPath( 3895b4aa86bSJames Feist pathPair.first.str, 5, chassis)) 3905b4aa86bSJames Feist { 3915b4aa86bSJames Feist chassis = "#IllegalValue"; 3925b4aa86bSJames Feist } 3935b4aa86bSJames Feist nlohmann::json& zone = zones[name]; 3945b4aa86bSJames Feist zone["Chassis"] = { 3955b4aa86bSJames Feist {"@odata.id", "/redfish/v1/Chassis/" + chassis}}; 3965b4aa86bSJames Feist zone["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/" 3975b4aa86bSJames Feist "OpenBmc/Fan/FanZones/" + 3985b4aa86bSJames Feist name; 3995b4aa86bSJames Feist zone["@odata.type"] = "#OemManager.FanZone"; 400b7a08d04SJames Feist config = &zone; 4015b4aa86bSJames Feist } 4025b4aa86bSJames Feist 403b7a08d04SJames Feist else if (intfPair.first == stepwiseConfigurationIface) 4045b4aa86bSJames Feist { 405c33a90ecSJames Feist if (classPtr == nullptr) 406c33a90ecSJames Feist { 407c33a90ecSJames Feist BMCWEB_LOG_ERROR << "Pid Class Field illegal"; 408c33a90ecSJames Feist messages::internalError(asyncResp->res); 409c33a90ecSJames Feist return; 410c33a90ecSJames Feist } 411c33a90ecSJames Feist 412b7a08d04SJames Feist nlohmann::json& controller = stepwise[name]; 413b7a08d04SJames Feist config = &controller; 4145b4aa86bSJames Feist 415b7a08d04SJames Feist controller["@odata.id"] = 416b7a08d04SJames Feist "/redfish/v1/Managers/bmc#/Oem/" 417b7a08d04SJames Feist "OpenBmc/Fan/StepwiseControllers/" + 418271584abSEd Tanous name; 419b7a08d04SJames Feist controller["@odata.type"] = 420b7a08d04SJames Feist "#OemManager.StepwiseController"; 421b7a08d04SJames Feist 422c33a90ecSJames Feist controller["Direction"] = *classPtr; 4235b4aa86bSJames Feist } 4245b4aa86bSJames Feist 4255b4aa86bSJames Feist // pid and fans are off the same configuration 426b7a08d04SJames Feist else if (intfPair.first == pidConfigurationIface) 4275b4aa86bSJames Feist { 428c33a90ecSJames Feist 4295b4aa86bSJames Feist if (classPtr == nullptr) 4305b4aa86bSJames Feist { 4315b4aa86bSJames Feist BMCWEB_LOG_ERROR << "Pid Class Field illegal"; 432a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 4335b4aa86bSJames Feist return; 4345b4aa86bSJames Feist } 4355b4aa86bSJames Feist bool isFan = *classPtr == "fan"; 4365b4aa86bSJames Feist nlohmann::json& element = 4375b4aa86bSJames Feist isFan ? fans[name] : pids[name]; 438b7a08d04SJames Feist config = &element; 4395b4aa86bSJames Feist if (isFan) 4405b4aa86bSJames Feist { 4415b4aa86bSJames Feist element["@odata.id"] = 4425b4aa86bSJames Feist "/redfish/v1/Managers/bmc#/Oem/" 4435b4aa86bSJames Feist "OpenBmc/Fan/FanControllers/" + 444271584abSEd Tanous name; 4455b4aa86bSJames Feist element["@odata.type"] = 4465b4aa86bSJames Feist "#OemManager.FanController"; 4475b4aa86bSJames Feist } 4485b4aa86bSJames Feist else 4495b4aa86bSJames Feist { 4505b4aa86bSJames Feist element["@odata.id"] = 4515b4aa86bSJames Feist "/redfish/v1/Managers/bmc#/Oem/" 4525b4aa86bSJames Feist "OpenBmc/Fan/PidControllers/" + 453271584abSEd Tanous name; 4545b4aa86bSJames Feist element["@odata.type"] = 4555b4aa86bSJames Feist "#OemManager.PidController"; 4565b4aa86bSJames Feist } 457b7a08d04SJames Feist } 458b7a08d04SJames Feist else 459b7a08d04SJames Feist { 460b7a08d04SJames Feist BMCWEB_LOG_ERROR << "Unexpected configuration"; 461b7a08d04SJames Feist messages::internalError(asyncResp->res); 462b7a08d04SJames Feist return; 463b7a08d04SJames Feist } 464b7a08d04SJames Feist 465b7a08d04SJames Feist // used for making maps out of 2 vectors 466b7a08d04SJames Feist const std::vector<double>* keys = nullptr; 467b7a08d04SJames Feist const std::vector<double>* values = nullptr; 468b7a08d04SJames Feist 469b7a08d04SJames Feist for (const auto& propertyPair : intfPair.second) 470b7a08d04SJames Feist { 471b7a08d04SJames Feist if (propertyPair.first == "Type" || 472b7a08d04SJames Feist propertyPair.first == "Class" || 473b7a08d04SJames Feist propertyPair.first == "Name") 474b7a08d04SJames Feist { 475b7a08d04SJames Feist continue; 476b7a08d04SJames Feist } 477b7a08d04SJames Feist 478b7a08d04SJames Feist // zones 479b7a08d04SJames Feist if (intfPair.first == pidZoneConfigurationIface) 480b7a08d04SJames Feist { 481b7a08d04SJames Feist const double* ptr = 482abf2add6SEd Tanous std::get_if<double>(&propertyPair.second); 483b7a08d04SJames Feist if (ptr == nullptr) 484b7a08d04SJames Feist { 485b7a08d04SJames Feist BMCWEB_LOG_ERROR << "Field Illegal " 486b7a08d04SJames Feist << propertyPair.first; 487b7a08d04SJames Feist messages::internalError(asyncResp->res); 488b7a08d04SJames Feist return; 489b7a08d04SJames Feist } 490b7a08d04SJames Feist (*config)[propertyPair.first] = *ptr; 491b7a08d04SJames Feist } 492b7a08d04SJames Feist 493b7a08d04SJames Feist if (intfPair.first == stepwiseConfigurationIface) 494b7a08d04SJames Feist { 495b7a08d04SJames Feist if (propertyPair.first == "Reading" || 496b7a08d04SJames Feist propertyPair.first == "Output") 497b7a08d04SJames Feist { 498b7a08d04SJames Feist const std::vector<double>* ptr = 499abf2add6SEd Tanous std::get_if<std::vector<double>>( 500b7a08d04SJames Feist &propertyPair.second); 501b7a08d04SJames Feist 502b7a08d04SJames Feist if (ptr == nullptr) 503b7a08d04SJames Feist { 504b7a08d04SJames Feist BMCWEB_LOG_ERROR << "Field Illegal " 505b7a08d04SJames Feist << propertyPair.first; 506b7a08d04SJames Feist messages::internalError(asyncResp->res); 507b7a08d04SJames Feist return; 508b7a08d04SJames Feist } 509b7a08d04SJames Feist 510b7a08d04SJames Feist if (propertyPair.first == "Reading") 511b7a08d04SJames Feist { 512b7a08d04SJames Feist keys = ptr; 513b7a08d04SJames Feist } 514b7a08d04SJames Feist else 515b7a08d04SJames Feist { 516b7a08d04SJames Feist values = ptr; 517b7a08d04SJames Feist } 518b7a08d04SJames Feist if (keys && values) 519b7a08d04SJames Feist { 520b7a08d04SJames Feist if (keys->size() != values->size()) 521b7a08d04SJames Feist { 522b7a08d04SJames Feist BMCWEB_LOG_ERROR 523b7a08d04SJames Feist << "Reading and Output size don't " 524b7a08d04SJames Feist "match "; 525b7a08d04SJames Feist messages::internalError(asyncResp->res); 526b7a08d04SJames Feist return; 527b7a08d04SJames Feist } 528b7a08d04SJames Feist nlohmann::json& steps = (*config)["Steps"]; 529b7a08d04SJames Feist steps = nlohmann::json::array(); 530b7a08d04SJames Feist for (size_t ii = 0; ii < keys->size(); ii++) 531b7a08d04SJames Feist { 532b7a08d04SJames Feist steps.push_back( 533b7a08d04SJames Feist {{"Target", (*keys)[ii]}, 534b7a08d04SJames Feist {"Output", (*values)[ii]}}); 535b7a08d04SJames Feist } 536b7a08d04SJames Feist } 537b7a08d04SJames Feist } 538b7a08d04SJames Feist if (propertyPair.first == "NegativeHysteresis" || 539b7a08d04SJames Feist propertyPair.first == "PositiveHysteresis") 540b7a08d04SJames Feist { 541b7a08d04SJames Feist const double* ptr = 542abf2add6SEd Tanous std::get_if<double>(&propertyPair.second); 543b7a08d04SJames Feist if (ptr == nullptr) 544b7a08d04SJames Feist { 545b7a08d04SJames Feist BMCWEB_LOG_ERROR << "Field Illegal " 546b7a08d04SJames Feist << propertyPair.first; 547b7a08d04SJames Feist messages::internalError(asyncResp->res); 548b7a08d04SJames Feist return; 549b7a08d04SJames Feist } 550b7a08d04SJames Feist (*config)[propertyPair.first] = *ptr; 551b7a08d04SJames Feist } 552b7a08d04SJames Feist } 553b7a08d04SJames Feist 554b7a08d04SJames Feist // pid and fans are off the same configuration 555b7a08d04SJames Feist if (intfPair.first == pidConfigurationIface || 556b7a08d04SJames Feist intfPair.first == stepwiseConfigurationIface) 557b7a08d04SJames Feist { 5585b4aa86bSJames Feist 5595b4aa86bSJames Feist if (propertyPair.first == "Zones") 5605b4aa86bSJames Feist { 5615b4aa86bSJames Feist const std::vector<std::string>* inputs = 562abf2add6SEd Tanous std::get_if<std::vector<std::string>>( 5631b6b96c5SEd Tanous &propertyPair.second); 5645b4aa86bSJames Feist 5655b4aa86bSJames Feist if (inputs == nullptr) 5665b4aa86bSJames Feist { 5675b4aa86bSJames Feist BMCWEB_LOG_ERROR 5685b4aa86bSJames Feist << "Zones Pid Field Illegal"; 569a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 5705b4aa86bSJames Feist return; 5715b4aa86bSJames Feist } 572b7a08d04SJames Feist auto& data = (*config)[propertyPair.first]; 5735b4aa86bSJames Feist data = nlohmann::json::array(); 5745b4aa86bSJames Feist for (std::string itemCopy : *inputs) 5755b4aa86bSJames Feist { 5765b4aa86bSJames Feist dbus::utility::escapePathForDbus(itemCopy); 5775b4aa86bSJames Feist data.push_back( 5785b4aa86bSJames Feist {{"@odata.id", 5795b4aa86bSJames Feist "/redfish/v1/Managers/bmc#/Oem/" 5805b4aa86bSJames Feist "OpenBmc/Fan/FanZones/" + 5815b4aa86bSJames Feist itemCopy}}); 5825b4aa86bSJames Feist } 5835b4aa86bSJames Feist } 5845b4aa86bSJames Feist // todo(james): may never happen, but this 5855b4aa86bSJames Feist // assumes configuration data referenced in the 5865b4aa86bSJames Feist // PID config is provided by the same daemon, we 5875b4aa86bSJames Feist // could add another loop to cover all cases, 5885b4aa86bSJames Feist // but I'm okay kicking this can down the road a 5895b4aa86bSJames Feist // bit 5905b4aa86bSJames Feist 5915b4aa86bSJames Feist else if (propertyPair.first == "Inputs" || 5925b4aa86bSJames Feist propertyPair.first == "Outputs") 5935b4aa86bSJames Feist { 594b7a08d04SJames Feist auto& data = (*config)[propertyPair.first]; 5955b4aa86bSJames Feist const std::vector<std::string>* inputs = 596abf2add6SEd Tanous std::get_if<std::vector<std::string>>( 5971b6b96c5SEd Tanous &propertyPair.second); 5985b4aa86bSJames Feist 5995b4aa86bSJames Feist if (inputs == nullptr) 6005b4aa86bSJames Feist { 6015b4aa86bSJames Feist BMCWEB_LOG_ERROR << "Field Illegal " 6025b4aa86bSJames Feist << propertyPair.first; 603f12894f8SJason M. Bills messages::internalError(asyncResp->res); 6045b4aa86bSJames Feist return; 6055b4aa86bSJames Feist } 6065b4aa86bSJames Feist data = *inputs; 607b943aaefSJames Feist } 608b943aaefSJames Feist else if (propertyPair.first == "SetPointOffset") 609b943aaefSJames Feist { 610b943aaefSJames Feist const std::string* ptr = 611b943aaefSJames Feist std::get_if<std::string>( 612b943aaefSJames Feist &propertyPair.second); 613b943aaefSJames Feist 614b943aaefSJames Feist if (ptr == nullptr) 615b943aaefSJames Feist { 616b943aaefSJames Feist BMCWEB_LOG_ERROR << "Field Illegal " 617b943aaefSJames Feist << propertyPair.first; 618b943aaefSJames Feist messages::internalError(asyncResp->res); 619b943aaefSJames Feist return; 620b943aaefSJames Feist } 621b943aaefSJames Feist // translate from dbus to redfish 622b943aaefSJames Feist if (*ptr == "WarningHigh") 623b943aaefSJames Feist { 624b943aaefSJames Feist (*config)["SetPointOffset"] = 625b943aaefSJames Feist "UpperThresholdNonCritical"; 626b943aaefSJames Feist } 627b943aaefSJames Feist else if (*ptr == "WarningLow") 628b943aaefSJames Feist { 629b943aaefSJames Feist (*config)["SetPointOffset"] = 630b943aaefSJames Feist "LowerThresholdNonCritical"; 631b943aaefSJames Feist } 632b943aaefSJames Feist else if (*ptr == "CriticalHigh") 633b943aaefSJames Feist { 634b943aaefSJames Feist (*config)["SetPointOffset"] = 635b943aaefSJames Feist "UpperThresholdCritical"; 636b943aaefSJames Feist } 637b943aaefSJames Feist else if (*ptr == "CriticalLow") 638b943aaefSJames Feist { 639b943aaefSJames Feist (*config)["SetPointOffset"] = 640b943aaefSJames Feist "LowerThresholdCritical"; 641b943aaefSJames Feist } 642b943aaefSJames Feist else 643b943aaefSJames Feist { 644b943aaefSJames Feist BMCWEB_LOG_ERROR << "Value Illegal " 645b943aaefSJames Feist << *ptr; 646b943aaefSJames Feist messages::internalError(asyncResp->res); 647b943aaefSJames Feist return; 648b943aaefSJames Feist } 649b943aaefSJames Feist } 650b943aaefSJames Feist // doubles 6515b4aa86bSJames Feist else if (propertyPair.first == 6525b4aa86bSJames Feist "FFGainCoefficient" || 6535b4aa86bSJames Feist propertyPair.first == "FFOffCoefficient" || 6545b4aa86bSJames Feist propertyPair.first == "ICoefficient" || 6555b4aa86bSJames Feist propertyPair.first == "ILimitMax" || 6565b4aa86bSJames Feist propertyPair.first == "ILimitMin" || 657aad1a257SJames Feist propertyPair.first == 658aad1a257SJames Feist "PositiveHysteresis" || 659aad1a257SJames Feist propertyPair.first == 660aad1a257SJames Feist "NegativeHysteresis" || 6615b4aa86bSJames Feist propertyPair.first == "OutLimitMax" || 6625b4aa86bSJames Feist propertyPair.first == "OutLimitMin" || 6635b4aa86bSJames Feist propertyPair.first == "PCoefficient" || 6647625cb81SJames Feist propertyPair.first == "SetPoint" || 6655b4aa86bSJames Feist propertyPair.first == "SlewNeg" || 6665b4aa86bSJames Feist propertyPair.first == "SlewPos") 6675b4aa86bSJames Feist { 6685b4aa86bSJames Feist const double* ptr = 669abf2add6SEd Tanous std::get_if<double>(&propertyPair.second); 6705b4aa86bSJames Feist if (ptr == nullptr) 6715b4aa86bSJames Feist { 6725b4aa86bSJames Feist BMCWEB_LOG_ERROR << "Field Illegal " 6735b4aa86bSJames Feist << propertyPair.first; 674f12894f8SJason M. Bills messages::internalError(asyncResp->res); 6755b4aa86bSJames Feist return; 6765b4aa86bSJames Feist } 677b7a08d04SJames Feist (*config)[propertyPair.first] = *ptr; 6785b4aa86bSJames Feist } 6795b4aa86bSJames Feist } 6805b4aa86bSJames Feist } 6815b4aa86bSJames Feist } 6825b4aa86bSJames Feist } 6835b4aa86bSJames Feist }, 6845b4aa86bSJames Feist connection, path, objectManagerIface, "GetManagedObjects"); 6855b4aa86bSJames Feist } 686ca537928SJennifer Lee 68783ff9ab6SJames Feist enum class CreatePIDRet 68883ff9ab6SJames Feist { 68983ff9ab6SJames Feist fail, 69083ff9ab6SJames Feist del, 69183ff9ab6SJames Feist patch 69283ff9ab6SJames Feist }; 69383ff9ab6SJames Feist 69423a21a1cSEd Tanous inline bool getZonesFromJsonReq(const std::shared_ptr<AsyncResp>& response, 6955f2caaefSJames Feist std::vector<nlohmann::json>& config, 6965f2caaefSJames Feist std::vector<std::string>& zones) 6975f2caaefSJames Feist { 698b6baeaa4SJames Feist if (config.empty()) 699b6baeaa4SJames Feist { 700b6baeaa4SJames Feist BMCWEB_LOG_ERROR << "Empty Zones"; 701b6baeaa4SJames Feist messages::propertyValueFormatError(response->res, 702b6baeaa4SJames Feist nlohmann::json::array(), "Zones"); 703b6baeaa4SJames Feist return false; 704b6baeaa4SJames Feist } 7055f2caaefSJames Feist for (auto& odata : config) 7065f2caaefSJames Feist { 7075f2caaefSJames Feist std::string path; 7085f2caaefSJames Feist if (!redfish::json_util::readJson(odata, response->res, "@odata.id", 7095f2caaefSJames Feist path)) 7105f2caaefSJames Feist { 7115f2caaefSJames Feist return false; 7125f2caaefSJames Feist } 7135f2caaefSJames Feist std::string input; 71461adbda3SJames Feist 71561adbda3SJames Feist // 8 below comes from 71661adbda3SJames Feist // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left 71761adbda3SJames Feist // 0 1 2 3 4 5 6 7 8 71861adbda3SJames Feist if (!dbus::utility::getNthStringFromPath(path, 8, input)) 7195f2caaefSJames Feist { 7205f2caaefSJames Feist BMCWEB_LOG_ERROR << "Got invalid path " << path; 7215f2caaefSJames Feist BMCWEB_LOG_ERROR << "Illegal Type Zones"; 7225f2caaefSJames Feist messages::propertyValueFormatError(response->res, odata.dump(), 7235f2caaefSJames Feist "Zones"); 7245f2caaefSJames Feist return false; 7255f2caaefSJames Feist } 7265f2caaefSJames Feist boost::replace_all(input, "_", " "); 7275f2caaefSJames Feist zones.emplace_back(std::move(input)); 7285f2caaefSJames Feist } 7295f2caaefSJames Feist return true; 7305f2caaefSJames Feist } 7315f2caaefSJames Feist 73223a21a1cSEd Tanous inline const dbus::utility::ManagedItem* 73373df0db0SJames Feist findChassis(const dbus::utility::ManagedObjectType& managedObj, 734b6baeaa4SJames Feist const std::string& value, std::string& chassis) 735b6baeaa4SJames Feist { 736b6baeaa4SJames Feist BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n"; 737b6baeaa4SJames Feist 738b6baeaa4SJames Feist std::string escaped = boost::replace_all_copy(value, " ", "_"); 739b6baeaa4SJames Feist escaped = "/" + escaped; 740b6baeaa4SJames Feist auto it = std::find_if( 741b6baeaa4SJames Feist managedObj.begin(), managedObj.end(), [&escaped](const auto& obj) { 742b6baeaa4SJames Feist if (boost::algorithm::ends_with(obj.first.str, escaped)) 743b6baeaa4SJames Feist { 744b6baeaa4SJames Feist BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n"; 745b6baeaa4SJames Feist return true; 746b6baeaa4SJames Feist } 747b6baeaa4SJames Feist return false; 748b6baeaa4SJames Feist }); 749b6baeaa4SJames Feist 750b6baeaa4SJames Feist if (it == managedObj.end()) 751b6baeaa4SJames Feist { 75273df0db0SJames Feist return nullptr; 753b6baeaa4SJames Feist } 754b6baeaa4SJames Feist // 5 comes from <chassis-name> being the 5th element 755b6baeaa4SJames Feist // /xyz/openbmc_project/inventory/system/chassis/<chassis-name> 75673df0db0SJames Feist if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis)) 75773df0db0SJames Feist { 75873df0db0SJames Feist return &(*it); 75973df0db0SJames Feist } 76073df0db0SJames Feist 76173df0db0SJames Feist return nullptr; 762b6baeaa4SJames Feist } 763b6baeaa4SJames Feist 76423a21a1cSEd Tanous inline CreatePIDRet createPidInterface( 76583ff9ab6SJames Feist const std::shared_ptr<AsyncResp>& response, const std::string& type, 766b5a76932SEd Tanous const nlohmann::json::iterator& it, const std::string& path, 76783ff9ab6SJames Feist const dbus::utility::ManagedObjectType& managedObj, bool createNewObject, 76883ff9ab6SJames Feist boost::container::flat_map<std::string, dbus::utility::DbusVariantType>& 76983ff9ab6SJames Feist output, 77073df0db0SJames Feist std::string& chassis, const std::string& profile) 77183ff9ab6SJames Feist { 77283ff9ab6SJames Feist 7735f2caaefSJames Feist // common deleter 774b6baeaa4SJames Feist if (it.value() == nullptr) 7755f2caaefSJames Feist { 7765f2caaefSJames Feist std::string iface; 7775f2caaefSJames Feist if (type == "PidControllers" || type == "FanControllers") 7785f2caaefSJames Feist { 7795f2caaefSJames Feist iface = pidConfigurationIface; 7805f2caaefSJames Feist } 7815f2caaefSJames Feist else if (type == "FanZones") 7825f2caaefSJames Feist { 7835f2caaefSJames Feist iface = pidZoneConfigurationIface; 7845f2caaefSJames Feist } 7855f2caaefSJames Feist else if (type == "StepwiseControllers") 7865f2caaefSJames Feist { 7875f2caaefSJames Feist iface = stepwiseConfigurationIface; 7885f2caaefSJames Feist } 7895f2caaefSJames Feist else 7905f2caaefSJames Feist { 791a0744d38SGunnar Mills BMCWEB_LOG_ERROR << "Illegal Type " << type; 7925f2caaefSJames Feist messages::propertyUnknown(response->res, type); 7935f2caaefSJames Feist return CreatePIDRet::fail; 7945f2caaefSJames Feist } 7956ee7f774SJames Feist 7966ee7f774SJames Feist BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n"; 7975f2caaefSJames Feist // delete interface 7985f2caaefSJames Feist crow::connections::systemBus->async_method_call( 7995f2caaefSJames Feist [response, path](const boost::system::error_code ec) { 8005f2caaefSJames Feist if (ec) 8015f2caaefSJames Feist { 8025f2caaefSJames Feist BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec; 8035f2caaefSJames Feist messages::internalError(response->res); 804b6baeaa4SJames Feist return; 8055f2caaefSJames Feist } 806b6baeaa4SJames Feist messages::success(response->res); 8075f2caaefSJames Feist }, 8085f2caaefSJames Feist "xyz.openbmc_project.EntityManager", path, iface, "Delete"); 8095f2caaefSJames Feist return CreatePIDRet::del; 8105f2caaefSJames Feist } 8115f2caaefSJames Feist 81273df0db0SJames Feist const dbus::utility::ManagedItem* managedItem = nullptr; 813b6baeaa4SJames Feist if (!createNewObject) 814b6baeaa4SJames Feist { 815b6baeaa4SJames Feist // if we aren't creating a new object, we should be able to find it on 816b6baeaa4SJames Feist // d-bus 81773df0db0SJames Feist managedItem = findChassis(managedObj, it.key(), chassis); 81873df0db0SJames Feist if (managedItem == nullptr) 819b6baeaa4SJames Feist { 820b6baeaa4SJames Feist BMCWEB_LOG_ERROR << "Failed to get chassis from config patch"; 821b6baeaa4SJames Feist messages::invalidObject(response->res, it.key()); 822b6baeaa4SJames Feist return CreatePIDRet::fail; 823b6baeaa4SJames Feist } 824b6baeaa4SJames Feist } 825b6baeaa4SJames Feist 82673df0db0SJames Feist if (profile.size() && 82773df0db0SJames Feist (type == "PidControllers" || type == "FanControllers" || 82873df0db0SJames Feist type == "StepwiseControllers")) 82973df0db0SJames Feist { 83073df0db0SJames Feist if (managedItem == nullptr) 83173df0db0SJames Feist { 83273df0db0SJames Feist output["Profiles"] = std::vector<std::string>{profile}; 83373df0db0SJames Feist } 83473df0db0SJames Feist else 83573df0db0SJames Feist { 83673df0db0SJames Feist std::string interface; 83773df0db0SJames Feist if (type == "StepwiseControllers") 83873df0db0SJames Feist { 83973df0db0SJames Feist interface = stepwiseConfigurationIface; 84073df0db0SJames Feist } 84173df0db0SJames Feist else 84273df0db0SJames Feist { 84373df0db0SJames Feist interface = pidConfigurationIface; 84473df0db0SJames Feist } 84573df0db0SJames Feist auto findConfig = managedItem->second.find(interface); 84673df0db0SJames Feist if (findConfig == managedItem->second.end()) 84773df0db0SJames Feist { 84873df0db0SJames Feist BMCWEB_LOG_ERROR 84973df0db0SJames Feist << "Failed to find interface in managed object"; 85073df0db0SJames Feist messages::internalError(response->res); 85173df0db0SJames Feist return CreatePIDRet::fail; 85273df0db0SJames Feist } 85373df0db0SJames Feist auto findProfiles = findConfig->second.find("Profiles"); 85473df0db0SJames Feist if (findProfiles != findConfig->second.end()) 85573df0db0SJames Feist { 85673df0db0SJames Feist const std::vector<std::string>* curProfiles = 85773df0db0SJames Feist std::get_if<std::vector<std::string>>( 85873df0db0SJames Feist &(findProfiles->second)); 85973df0db0SJames Feist if (curProfiles == nullptr) 86073df0db0SJames Feist { 86173df0db0SJames Feist BMCWEB_LOG_ERROR << "Illegal profiles in managed object"; 86273df0db0SJames Feist messages::internalError(response->res); 86373df0db0SJames Feist return CreatePIDRet::fail; 86473df0db0SJames Feist } 86573df0db0SJames Feist if (std::find(curProfiles->begin(), curProfiles->end(), 86673df0db0SJames Feist profile) == curProfiles->end()) 86773df0db0SJames Feist { 86873df0db0SJames Feist std::vector<std::string> newProfiles = *curProfiles; 86973df0db0SJames Feist newProfiles.push_back(profile); 87073df0db0SJames Feist output["Profiles"] = newProfiles; 87173df0db0SJames Feist } 87273df0db0SJames Feist } 87373df0db0SJames Feist } 87473df0db0SJames Feist } 87573df0db0SJames Feist 87683ff9ab6SJames Feist if (type == "PidControllers" || type == "FanControllers") 87783ff9ab6SJames Feist { 87883ff9ab6SJames Feist if (createNewObject) 87983ff9ab6SJames Feist { 88083ff9ab6SJames Feist output["Class"] = type == "PidControllers" ? std::string("temp") 88183ff9ab6SJames Feist : std::string("fan"); 88283ff9ab6SJames Feist output["Type"] = std::string("Pid"); 88383ff9ab6SJames Feist } 8845f2caaefSJames Feist 8855f2caaefSJames Feist std::optional<std::vector<nlohmann::json>> zones; 8865f2caaefSJames Feist std::optional<std::vector<std::string>> inputs; 8875f2caaefSJames Feist std::optional<std::vector<std::string>> outputs; 8885f2caaefSJames Feist std::map<std::string, std::optional<double>> doubles; 889b943aaefSJames Feist std::optional<std::string> setpointOffset; 8905f2caaefSJames Feist if (!redfish::json_util::readJson( 891b6baeaa4SJames Feist it.value(), response->res, "Inputs", inputs, "Outputs", outputs, 8925f2caaefSJames Feist "Zones", zones, "FFGainCoefficient", 8935f2caaefSJames Feist doubles["FFGainCoefficient"], "FFOffCoefficient", 8945f2caaefSJames Feist doubles["FFOffCoefficient"], "ICoefficient", 8955f2caaefSJames Feist doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"], 8965f2caaefSJames Feist "ILimitMin", doubles["ILimitMin"], "OutLimitMax", 8975f2caaefSJames Feist doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"], 8985f2caaefSJames Feist "PCoefficient", doubles["PCoefficient"], "SetPoint", 899b943aaefSJames Feist doubles["SetPoint"], "SetPointOffset", setpointOffset, 900b943aaefSJames Feist "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"], 901b943aaefSJames Feist "PositiveHysteresis", doubles["PositiveHysteresis"], 902b943aaefSJames Feist "NegativeHysteresis", doubles["NegativeHysteresis"])) 90383ff9ab6SJames Feist { 904a0744d38SGunnar Mills BMCWEB_LOG_ERROR << "Illegal Property " << it.value().dump(); 9055f2caaefSJames Feist return CreatePIDRet::fail; 90683ff9ab6SJames Feist } 9075f2caaefSJames Feist if (zones) 9085f2caaefSJames Feist { 9095f2caaefSJames Feist std::vector<std::string> zonesStr; 9105f2caaefSJames Feist if (!getZonesFromJsonReq(response, *zones, zonesStr)) 9115f2caaefSJames Feist { 912a0744d38SGunnar Mills BMCWEB_LOG_ERROR << "Illegal Zones"; 9135f2caaefSJames Feist return CreatePIDRet::fail; 9145f2caaefSJames Feist } 915b6baeaa4SJames Feist if (chassis.empty() && 916b6baeaa4SJames Feist !findChassis(managedObj, zonesStr[0], chassis)) 917b6baeaa4SJames Feist { 918b6baeaa4SJames Feist BMCWEB_LOG_ERROR << "Failed to get chassis from config patch"; 919b6baeaa4SJames Feist messages::invalidObject(response->res, it.key()); 920b6baeaa4SJames Feist return CreatePIDRet::fail; 921b6baeaa4SJames Feist } 922b6baeaa4SJames Feist 9235f2caaefSJames Feist output["Zones"] = std::move(zonesStr); 9245f2caaefSJames Feist } 9255f2caaefSJames Feist if (inputs || outputs) 9265f2caaefSJames Feist { 9275f2caaefSJames Feist std::array<std::optional<std::vector<std::string>>*, 2> containers = 9285f2caaefSJames Feist {&inputs, &outputs}; 9295f2caaefSJames Feist size_t index = 0; 9305f2caaefSJames Feist for (const auto& containerPtr : containers) 9315f2caaefSJames Feist { 9325f2caaefSJames Feist std::optional<std::vector<std::string>>& container = 9335f2caaefSJames Feist *containerPtr; 9345f2caaefSJames Feist if (!container) 9355f2caaefSJames Feist { 9365f2caaefSJames Feist index++; 9375f2caaefSJames Feist continue; 93883ff9ab6SJames Feist } 93983ff9ab6SJames Feist 9405f2caaefSJames Feist for (std::string& value : *container) 94183ff9ab6SJames Feist { 9425f2caaefSJames Feist boost::replace_all(value, "_", " "); 94383ff9ab6SJames Feist } 9445f2caaefSJames Feist std::string key; 9455f2caaefSJames Feist if (index == 0) 9465f2caaefSJames Feist { 9475f2caaefSJames Feist key = "Inputs"; 9485f2caaefSJames Feist } 9495f2caaefSJames Feist else 9505f2caaefSJames Feist { 9515f2caaefSJames Feist key = "Outputs"; 9525f2caaefSJames Feist } 9535f2caaefSJames Feist output[key] = *container; 9545f2caaefSJames Feist index++; 9555f2caaefSJames Feist } 95683ff9ab6SJames Feist } 95783ff9ab6SJames Feist 958b943aaefSJames Feist if (setpointOffset) 959b943aaefSJames Feist { 960b943aaefSJames Feist // translate between redfish and dbus names 961b943aaefSJames Feist if (*setpointOffset == "UpperThresholdNonCritical") 962b943aaefSJames Feist { 963b943aaefSJames Feist output["SetPointOffset"] = std::string("WarningLow"); 964b943aaefSJames Feist } 965b943aaefSJames Feist else if (*setpointOffset == "LowerThresholdNonCritical") 966b943aaefSJames Feist { 967b943aaefSJames Feist output["SetPointOffset"] = std::string("WarningHigh"); 968b943aaefSJames Feist } 969b943aaefSJames Feist else if (*setpointOffset == "LowerThresholdCritical") 970b943aaefSJames Feist { 971b943aaefSJames Feist output["SetPointOffset"] = std::string("CriticalLow"); 972b943aaefSJames Feist } 973b943aaefSJames Feist else if (*setpointOffset == "UpperThresholdCritical") 974b943aaefSJames Feist { 975b943aaefSJames Feist output["SetPointOffset"] = std::string("CriticalHigh"); 976b943aaefSJames Feist } 977b943aaefSJames Feist else 978b943aaefSJames Feist { 979b943aaefSJames Feist BMCWEB_LOG_ERROR << "Invalid setpointoffset " 980b943aaefSJames Feist << *setpointOffset; 981b943aaefSJames Feist messages::invalidObject(response->res, it.key()); 982b943aaefSJames Feist return CreatePIDRet::fail; 983b943aaefSJames Feist } 984b943aaefSJames Feist } 985b943aaefSJames Feist 98683ff9ab6SJames Feist // doubles 9875f2caaefSJames Feist for (const auto& pairs : doubles) 98883ff9ab6SJames Feist { 9895f2caaefSJames Feist if (!pairs.second) 99083ff9ab6SJames Feist { 9915f2caaefSJames Feist continue; 99283ff9ab6SJames Feist } 9935f2caaefSJames Feist BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second; 9945f2caaefSJames Feist output[pairs.first] = *(pairs.second); 9955f2caaefSJames Feist } 99683ff9ab6SJames Feist } 99783ff9ab6SJames Feist 99883ff9ab6SJames Feist else if (type == "FanZones") 99983ff9ab6SJames Feist { 100083ff9ab6SJames Feist output["Type"] = std::string("Pid.Zone"); 100183ff9ab6SJames Feist 10025f2caaefSJames Feist std::optional<nlohmann::json> chassisContainer; 10035f2caaefSJames Feist std::optional<double> failSafePercent; 1004d3ec07f8SJames Feist std::optional<double> minThermalOutput; 1005b6baeaa4SJames Feist if (!redfish::json_util::readJson(it.value(), response->res, "Chassis", 10065f2caaefSJames Feist chassisContainer, "FailSafePercent", 1007d3ec07f8SJames Feist failSafePercent, "MinThermalOutput", 1008d3ec07f8SJames Feist minThermalOutput)) 100983ff9ab6SJames Feist { 1010a0744d38SGunnar Mills BMCWEB_LOG_ERROR << "Illegal Property " << it.value().dump(); 101183ff9ab6SJames Feist return CreatePIDRet::fail; 101283ff9ab6SJames Feist } 10135f2caaefSJames Feist 10145f2caaefSJames Feist if (chassisContainer) 101583ff9ab6SJames Feist { 10165f2caaefSJames Feist 10175f2caaefSJames Feist std::string chassisId; 10185f2caaefSJames Feist if (!redfish::json_util::readJson(*chassisContainer, response->res, 10195f2caaefSJames Feist "@odata.id", chassisId)) 10205f2caaefSJames Feist { 1021a0744d38SGunnar Mills BMCWEB_LOG_ERROR << "Illegal Property " 10225f2caaefSJames Feist << chassisContainer->dump(); 102383ff9ab6SJames Feist return CreatePIDRet::fail; 102483ff9ab6SJames Feist } 102583ff9ab6SJames Feist 1026717794d5SAppaRao Puli // /redfish/v1/chassis/chassis_name/ 10275f2caaefSJames Feist if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis)) 102883ff9ab6SJames Feist { 10295f2caaefSJames Feist BMCWEB_LOG_ERROR << "Got invalid path " << chassisId; 10305f2caaefSJames Feist messages::invalidObject(response->res, chassisId); 103183ff9ab6SJames Feist return CreatePIDRet::fail; 103283ff9ab6SJames Feist } 103383ff9ab6SJames Feist } 1034d3ec07f8SJames Feist if (minThermalOutput) 103583ff9ab6SJames Feist { 1036d3ec07f8SJames Feist output["MinThermalOutput"] = *minThermalOutput; 10375f2caaefSJames Feist } 10385f2caaefSJames Feist if (failSafePercent) 103983ff9ab6SJames Feist { 10405f2caaefSJames Feist output["FailSafePercent"] = *failSafePercent; 10415f2caaefSJames Feist } 10425f2caaefSJames Feist } 10435f2caaefSJames Feist else if (type == "StepwiseControllers") 10445f2caaefSJames Feist { 10455f2caaefSJames Feist output["Type"] = std::string("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 { 1059a0744d38SGunnar Mills BMCWEB_LOG_ERROR << "Illegal Property " << it.value().dump(); 106083ff9ab6SJames Feist return CreatePIDRet::fail; 106183ff9ab6SJames Feist } 10625f2caaefSJames Feist 10635f2caaefSJames Feist if (zones) 106483ff9ab6SJames Feist { 1065b6baeaa4SJames Feist std::vector<std::string> zonesStrs; 1066b6baeaa4SJames Feist if (!getZonesFromJsonReq(response, *zones, zonesStrs)) 10675f2caaefSJames Feist { 1068a0744d38SGunnar Mills BMCWEB_LOG_ERROR << "Illegal Zones"; 106983ff9ab6SJames Feist return CreatePIDRet::fail; 107083ff9ab6SJames Feist } 1071b6baeaa4SJames Feist if (chassis.empty() && 1072b6baeaa4SJames Feist !findChassis(managedObj, zonesStrs[0], chassis)) 1073b6baeaa4SJames Feist { 1074b6baeaa4SJames Feist BMCWEB_LOG_ERROR << "Failed to get chassis from config patch"; 1075b6baeaa4SJames Feist messages::invalidObject(response->res, it.key()); 1076b6baeaa4SJames Feist return CreatePIDRet::fail; 1077b6baeaa4SJames Feist } 1078b6baeaa4SJames Feist output["Zones"] = std::move(zonesStrs); 10795f2caaefSJames Feist } 10805f2caaefSJames Feist if (steps) 10815f2caaefSJames Feist { 10825f2caaefSJames Feist std::vector<double> readings; 10835f2caaefSJames Feist std::vector<double> outputs; 10845f2caaefSJames Feist for (auto& step : *steps) 10855f2caaefSJames Feist { 10865f2caaefSJames Feist double target; 108723a21a1cSEd Tanous double out; 10885f2caaefSJames Feist 10895f2caaefSJames Feist if (!redfish::json_util::readJson(step, response->res, "Target", 109023a21a1cSEd Tanous target, "Output", out)) 10915f2caaefSJames Feist { 1092a0744d38SGunnar Mills BMCWEB_LOG_ERROR << "Illegal Property " 1093b6baeaa4SJames Feist << it.value().dump(); 10945f2caaefSJames Feist return CreatePIDRet::fail; 10955f2caaefSJames Feist } 10965f2caaefSJames Feist readings.emplace_back(target); 109723a21a1cSEd Tanous outputs.emplace_back(out); 10985f2caaefSJames Feist } 10995f2caaefSJames Feist output["Reading"] = std::move(readings); 11005f2caaefSJames Feist output["Output"] = std::move(outputs); 11015f2caaefSJames Feist } 11025f2caaefSJames Feist if (inputs) 11035f2caaefSJames Feist { 11045f2caaefSJames Feist for (std::string& value : *inputs) 11055f2caaefSJames Feist { 11065f2caaefSJames Feist boost::replace_all(value, "_", " "); 11075f2caaefSJames Feist } 11085f2caaefSJames Feist output["Inputs"] = std::move(*inputs); 11095f2caaefSJames Feist } 11105f2caaefSJames Feist if (negativeHysteresis) 11115f2caaefSJames Feist { 11125f2caaefSJames Feist output["NegativeHysteresis"] = *negativeHysteresis; 11135f2caaefSJames Feist } 11145f2caaefSJames Feist if (positiveHysteresis) 11155f2caaefSJames Feist { 11165f2caaefSJames Feist output["PositiveHysteresis"] = *positiveHysteresis; 111783ff9ab6SJames Feist } 1118c33a90ecSJames Feist if (direction) 1119c33a90ecSJames Feist { 1120c33a90ecSJames Feist constexpr const std::array<const char*, 2> allowedDirections = { 1121c33a90ecSJames Feist "Ceiling", "Floor"}; 1122c33a90ecSJames Feist if (std::find(allowedDirections.begin(), allowedDirections.end(), 1123c33a90ecSJames Feist *direction) == allowedDirections.end()) 1124c33a90ecSJames Feist { 1125c33a90ecSJames Feist messages::propertyValueTypeError(response->res, "Direction", 1126c33a90ecSJames Feist *direction); 1127c33a90ecSJames Feist return CreatePIDRet::fail; 1128c33a90ecSJames Feist } 1129c33a90ecSJames Feist output["Class"] = *direction; 1130c33a90ecSJames Feist } 113183ff9ab6SJames Feist } 113283ff9ab6SJames Feist else 113383ff9ab6SJames Feist { 1134a0744d38SGunnar Mills BMCWEB_LOG_ERROR << "Illegal Type " << type; 113535a62c7cSJason M. Bills messages::propertyUnknown(response->res, type); 113683ff9ab6SJames Feist return CreatePIDRet::fail; 113783ff9ab6SJames Feist } 113883ff9ab6SJames Feist return CreatePIDRet::patch; 113983ff9ab6SJames Feist } 114073df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues> 114173df0db0SJames Feist { 114283ff9ab6SJames Feist 114323a21a1cSEd Tanous GetPIDValues(const std::shared_ptr<AsyncResp>& asyncRespIn) : 114423a21a1cSEd Tanous asyncResp(asyncRespIn) 114573df0db0SJames Feist 11461214b7e7SGunnar Mills {} 11479c310685SBorawski.Lukasz 114873df0db0SJames Feist void run() 11495b4aa86bSJames Feist { 115073df0db0SJames Feist std::shared_ptr<GetPIDValues> self = shared_from_this(); 115173df0db0SJames Feist 115273df0db0SJames Feist // get all configurations 11535b4aa86bSJames Feist crow::connections::systemBus->async_method_call( 115473df0db0SJames Feist [self](const boost::system::error_code ec, 115523a21a1cSEd Tanous const crow::openbmc_mapper::GetSubTreeType& subtreeLocal) { 11565b4aa86bSJames Feist if (ec) 11575b4aa86bSJames Feist { 11585b4aa86bSJames Feist BMCWEB_LOG_ERROR << ec; 115973df0db0SJames Feist messages::internalError(self->asyncResp->res); 116073df0db0SJames Feist return; 116173df0db0SJames Feist } 116223a21a1cSEd Tanous self->subtree = subtreeLocal; 116373df0db0SJames Feist }, 116473df0db0SJames Feist "xyz.openbmc_project.ObjectMapper", 116573df0db0SJames Feist "/xyz/openbmc_project/object_mapper", 116673df0db0SJames Feist "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, 116773df0db0SJames Feist std::array<const char*, 4>{ 116873df0db0SJames Feist pidConfigurationIface, pidZoneConfigurationIface, 116973df0db0SJames Feist objectManagerIface, stepwiseConfigurationIface}); 117073df0db0SJames Feist 117173df0db0SJames Feist // at the same time get the selected profile 117273df0db0SJames Feist crow::connections::systemBus->async_method_call( 117373df0db0SJames Feist [self](const boost::system::error_code ec, 117423a21a1cSEd Tanous const crow::openbmc_mapper::GetSubTreeType& subtreeLocal) { 117523a21a1cSEd Tanous if (ec || subtreeLocal.empty()) 117673df0db0SJames Feist { 117773df0db0SJames Feist return; 117873df0db0SJames Feist } 117923a21a1cSEd Tanous if (subtreeLocal[0].second.size() != 1) 118073df0db0SJames Feist { 118173df0db0SJames Feist // invalid mapper response, should never happen 118273df0db0SJames Feist BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error"; 118373df0db0SJames Feist messages::internalError(self->asyncResp->res); 11845b4aa86bSJames Feist return; 11855b4aa86bSJames Feist } 11865b4aa86bSJames Feist 118723a21a1cSEd Tanous const std::string& path = subtreeLocal[0].first; 118823a21a1cSEd Tanous const std::string& owner = subtreeLocal[0].second[0].first; 118973df0db0SJames Feist crow::connections::systemBus->async_method_call( 119073df0db0SJames Feist [path, owner, self]( 119123a21a1cSEd Tanous const boost::system::error_code ec2, 119273df0db0SJames Feist const boost::container::flat_map< 119373df0db0SJames Feist std::string, std::variant<std::vector<std::string>, 119473df0db0SJames Feist std::string>>& resp) { 119523a21a1cSEd Tanous if (ec2) 119673df0db0SJames Feist { 119773df0db0SJames Feist BMCWEB_LOG_ERROR << "GetPIDValues: Can't get " 119873df0db0SJames Feist "thermalModeIface " 119973df0db0SJames Feist << path; 120073df0db0SJames Feist messages::internalError(self->asyncResp->res); 120173df0db0SJames Feist return; 120273df0db0SJames Feist } 1203271584abSEd Tanous const std::string* current = nullptr; 1204271584abSEd Tanous const std::vector<std::string>* supported = nullptr; 120573df0db0SJames Feist for (auto& [key, value] : resp) 120673df0db0SJames Feist { 120773df0db0SJames Feist if (key == "Current") 120873df0db0SJames Feist { 120973df0db0SJames Feist current = std::get_if<std::string>(&value); 121073df0db0SJames Feist if (current == nullptr) 121173df0db0SJames Feist { 121273df0db0SJames Feist BMCWEB_LOG_ERROR 121373df0db0SJames Feist << "GetPIDValues: thermal mode " 121473df0db0SJames Feist "iface invalid " 121573df0db0SJames Feist << path; 121673df0db0SJames Feist messages::internalError( 121773df0db0SJames Feist self->asyncResp->res); 121873df0db0SJames Feist return; 121973df0db0SJames Feist } 122073df0db0SJames Feist } 122173df0db0SJames Feist if (key == "Supported") 122273df0db0SJames Feist { 122373df0db0SJames Feist supported = 122473df0db0SJames Feist std::get_if<std::vector<std::string>>( 122573df0db0SJames Feist &value); 122673df0db0SJames Feist if (supported == nullptr) 122773df0db0SJames Feist { 122873df0db0SJames Feist BMCWEB_LOG_ERROR 122973df0db0SJames Feist << "GetPIDValues: thermal mode " 123073df0db0SJames Feist "iface invalid" 123173df0db0SJames Feist << path; 123273df0db0SJames Feist messages::internalError( 123373df0db0SJames Feist self->asyncResp->res); 123473df0db0SJames Feist return; 123573df0db0SJames Feist } 123673df0db0SJames Feist } 123773df0db0SJames Feist } 123873df0db0SJames Feist if (current == nullptr || supported == nullptr) 123973df0db0SJames Feist { 124073df0db0SJames Feist BMCWEB_LOG_ERROR << "GetPIDValues: thermal mode " 124173df0db0SJames Feist "iface invalid " 124273df0db0SJames Feist << path; 124373df0db0SJames Feist messages::internalError(self->asyncResp->res); 124473df0db0SJames Feist return; 124573df0db0SJames Feist } 124673df0db0SJames Feist self->currentProfile = *current; 124773df0db0SJames Feist self->supportedProfiles = *supported; 124873df0db0SJames Feist }, 124973df0db0SJames Feist owner, path, "org.freedesktop.DBus.Properties", "GetAll", 125073df0db0SJames Feist thermalModeIface); 125173df0db0SJames Feist }, 125273df0db0SJames Feist "xyz.openbmc_project.ObjectMapper", 125373df0db0SJames Feist "/xyz/openbmc_project/object_mapper", 125473df0db0SJames Feist "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, 125573df0db0SJames Feist std::array<const char*, 1>{thermalModeIface}); 125673df0db0SJames Feist } 125773df0db0SJames Feist 125873df0db0SJames Feist ~GetPIDValues() 125973df0db0SJames Feist { 126073df0db0SJames Feist if (asyncResp->res.result() != boost::beast::http::status::ok) 126173df0db0SJames Feist { 126273df0db0SJames Feist return; 126373df0db0SJames Feist } 12645b4aa86bSJames Feist // create map of <connection, path to objMgr>> 126573df0db0SJames Feist boost::container::flat_map<std::string, std::string> objectMgrPaths; 12666bce33bcSJames Feist boost::container::flat_set<std::string> calledConnections; 12675b4aa86bSJames Feist for (const auto& pathGroup : subtree) 12685b4aa86bSJames Feist { 12695b4aa86bSJames Feist for (const auto& connectionGroup : pathGroup.second) 12705b4aa86bSJames Feist { 12716bce33bcSJames Feist auto findConnection = 12726bce33bcSJames Feist calledConnections.find(connectionGroup.first); 12736bce33bcSJames Feist if (findConnection != calledConnections.end()) 12746bce33bcSJames Feist { 12756bce33bcSJames Feist break; 12766bce33bcSJames Feist } 127773df0db0SJames Feist for (const std::string& interface : connectionGroup.second) 12785b4aa86bSJames Feist { 12795b4aa86bSJames Feist if (interface == objectManagerIface) 12805b4aa86bSJames Feist { 128173df0db0SJames Feist objectMgrPaths[connectionGroup.first] = pathGroup.first; 12825b4aa86bSJames Feist } 12835b4aa86bSJames Feist // this list is alphabetical, so we 12845b4aa86bSJames Feist // should have found the objMgr by now 12855b4aa86bSJames Feist if (interface == pidConfigurationIface || 1286b7a08d04SJames Feist interface == pidZoneConfigurationIface || 1287b7a08d04SJames Feist interface == stepwiseConfigurationIface) 12885b4aa86bSJames Feist { 12895b4aa86bSJames Feist auto findObjMgr = 12905b4aa86bSJames Feist objectMgrPaths.find(connectionGroup.first); 12915b4aa86bSJames Feist if (findObjMgr == objectMgrPaths.end()) 12925b4aa86bSJames Feist { 12935b4aa86bSJames Feist BMCWEB_LOG_DEBUG << connectionGroup.first 12945b4aa86bSJames Feist << "Has no Object Manager"; 12955b4aa86bSJames Feist continue; 12965b4aa86bSJames Feist } 12976bce33bcSJames Feist 12986bce33bcSJames Feist calledConnections.insert(connectionGroup.first); 12996bce33bcSJames Feist 130073df0db0SJames Feist asyncPopulatePid(findObjMgr->first, findObjMgr->second, 130173df0db0SJames Feist currentProfile, supportedProfiles, 130273df0db0SJames Feist asyncResp); 13035b4aa86bSJames Feist break; 13045b4aa86bSJames Feist } 13055b4aa86bSJames Feist } 13065b4aa86bSJames Feist } 13075b4aa86bSJames Feist } 130873df0db0SJames Feist } 130973df0db0SJames Feist 131073df0db0SJames Feist std::vector<std::string> supportedProfiles; 131173df0db0SJames Feist std::string currentProfile; 131273df0db0SJames Feist crow::openbmc_mapper::GetSubTreeType subtree; 131373df0db0SJames Feist std::shared_ptr<AsyncResp> asyncResp; 131473df0db0SJames Feist }; 131573df0db0SJames Feist 131673df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues> 131773df0db0SJames Feist { 131873df0db0SJames Feist 1319271584abSEd Tanous SetPIDValues(const std::shared_ptr<AsyncResp>& asyncRespIn, 132073df0db0SJames Feist nlohmann::json& data) : 1321271584abSEd Tanous asyncResp(asyncRespIn) 132273df0db0SJames Feist { 132373df0db0SJames Feist 132473df0db0SJames Feist std::optional<nlohmann::json> pidControllers; 132573df0db0SJames Feist std::optional<nlohmann::json> fanControllers; 132673df0db0SJames Feist std::optional<nlohmann::json> fanZones; 132773df0db0SJames Feist std::optional<nlohmann::json> stepwiseControllers; 132873df0db0SJames Feist 132973df0db0SJames Feist if (!redfish::json_util::readJson( 133073df0db0SJames Feist data, asyncResp->res, "PidControllers", pidControllers, 133173df0db0SJames Feist "FanControllers", fanControllers, "FanZones", fanZones, 133273df0db0SJames Feist "StepwiseControllers", stepwiseControllers, "Profile", profile)) 133373df0db0SJames Feist { 1334a0744d38SGunnar Mills BMCWEB_LOG_ERROR << "Illegal Property " << data.dump(); 133573df0db0SJames Feist return; 133673df0db0SJames Feist } 133773df0db0SJames Feist configuration.emplace_back("PidControllers", std::move(pidControllers)); 133873df0db0SJames Feist configuration.emplace_back("FanControllers", std::move(fanControllers)); 133973df0db0SJames Feist configuration.emplace_back("FanZones", std::move(fanZones)); 134073df0db0SJames Feist configuration.emplace_back("StepwiseControllers", 134173df0db0SJames Feist std::move(stepwiseControllers)); 134273df0db0SJames Feist } 134373df0db0SJames Feist void run() 134473df0db0SJames Feist { 134573df0db0SJames Feist if (asyncResp->res.result() != boost::beast::http::status::ok) 134673df0db0SJames Feist { 134773df0db0SJames Feist return; 134873df0db0SJames Feist } 134973df0db0SJames Feist 135073df0db0SJames Feist std::shared_ptr<SetPIDValues> self = shared_from_this(); 135173df0db0SJames Feist 135273df0db0SJames Feist // todo(james): might make sense to do a mapper call here if this 135373df0db0SJames Feist // interface gets more traction 135473df0db0SJames Feist crow::connections::systemBus->async_method_call( 135573df0db0SJames Feist [self](const boost::system::error_code ec, 1356271584abSEd Tanous dbus::utility::ManagedObjectType& mObj) { 135773df0db0SJames Feist if (ec) 135873df0db0SJames Feist { 135973df0db0SJames Feist BMCWEB_LOG_ERROR << "Error communicating to Entity Manager"; 136073df0db0SJames Feist messages::internalError(self->asyncResp->res); 136173df0db0SJames Feist return; 136273df0db0SJames Feist } 1363e69d9de2SJames Feist const std::array<const char*, 3> configurations = { 1364e69d9de2SJames Feist pidConfigurationIface, pidZoneConfigurationIface, 1365e69d9de2SJames Feist stepwiseConfigurationIface}; 1366e69d9de2SJames Feist 136714b0b8d5SJames Feist for (const auto& [path, object] : mObj) 1368e69d9de2SJames Feist { 136914b0b8d5SJames Feist for (const auto& [interface, _] : object) 1370e69d9de2SJames Feist { 1371e69d9de2SJames Feist if (std::find(configurations.begin(), 1372e69d9de2SJames Feist configurations.end(), 1373e69d9de2SJames Feist interface) != configurations.end()) 1374e69d9de2SJames Feist { 137514b0b8d5SJames Feist self->objectCount++; 1376e69d9de2SJames Feist break; 1377e69d9de2SJames Feist } 1378e69d9de2SJames Feist } 1379e69d9de2SJames Feist } 1380271584abSEd Tanous self->managedObj = std::move(mObj); 138173df0db0SJames Feist }, 138273df0db0SJames Feist "xyz.openbmc_project.EntityManager", "/", objectManagerIface, 138373df0db0SJames Feist "GetManagedObjects"); 138473df0db0SJames Feist 138573df0db0SJames Feist // at the same time get the profile information 138673df0db0SJames Feist crow::connections::systemBus->async_method_call( 138773df0db0SJames Feist [self](const boost::system::error_code ec, 138873df0db0SJames Feist const crow::openbmc_mapper::GetSubTreeType& subtree) { 138973df0db0SJames Feist if (ec || subtree.empty()) 139073df0db0SJames Feist { 139173df0db0SJames Feist return; 139273df0db0SJames Feist } 139373df0db0SJames Feist if (subtree[0].second.empty()) 139473df0db0SJames Feist { 139573df0db0SJames Feist // invalid mapper response, should never happen 139673df0db0SJames Feist BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error"; 139773df0db0SJames Feist messages::internalError(self->asyncResp->res); 139873df0db0SJames Feist return; 139973df0db0SJames Feist } 140073df0db0SJames Feist 140173df0db0SJames Feist const std::string& path = subtree[0].first; 140273df0db0SJames Feist const std::string& owner = subtree[0].second[0].first; 140373df0db0SJames Feist crow::connections::systemBus->async_method_call( 140473df0db0SJames Feist [self, path, owner]( 1405cb13a392SEd Tanous const boost::system::error_code ec2, 140673df0db0SJames Feist const boost::container::flat_map< 140773df0db0SJames Feist std::string, std::variant<std::vector<std::string>, 1408271584abSEd Tanous std::string>>& r) { 1409cb13a392SEd Tanous if (ec2) 141073df0db0SJames Feist { 141173df0db0SJames Feist BMCWEB_LOG_ERROR << "SetPIDValues: Can't get " 141273df0db0SJames Feist "thermalModeIface " 141373df0db0SJames Feist << path; 141473df0db0SJames Feist messages::internalError(self->asyncResp->res); 141573df0db0SJames Feist return; 141673df0db0SJames Feist } 1417271584abSEd Tanous const std::string* current = nullptr; 1418271584abSEd Tanous const std::vector<std::string>* supported = nullptr; 1419271584abSEd Tanous for (auto& [key, value] : r) 142073df0db0SJames Feist { 142173df0db0SJames Feist if (key == "Current") 142273df0db0SJames Feist { 142373df0db0SJames Feist current = std::get_if<std::string>(&value); 142473df0db0SJames Feist if (current == nullptr) 142573df0db0SJames Feist { 142673df0db0SJames Feist BMCWEB_LOG_ERROR 142773df0db0SJames Feist << "SetPIDValues: thermal mode " 142873df0db0SJames Feist "iface invalid " 142973df0db0SJames Feist << path; 143073df0db0SJames Feist messages::internalError( 143173df0db0SJames Feist self->asyncResp->res); 143273df0db0SJames Feist return; 143373df0db0SJames Feist } 143473df0db0SJames Feist } 143573df0db0SJames Feist if (key == "Supported") 143673df0db0SJames Feist { 143773df0db0SJames Feist supported = 143873df0db0SJames Feist std::get_if<std::vector<std::string>>( 143973df0db0SJames Feist &value); 144073df0db0SJames Feist if (supported == nullptr) 144173df0db0SJames Feist { 144273df0db0SJames Feist BMCWEB_LOG_ERROR 144373df0db0SJames Feist << "SetPIDValues: thermal mode " 144473df0db0SJames Feist "iface invalid" 144573df0db0SJames Feist << path; 144673df0db0SJames Feist messages::internalError( 144773df0db0SJames Feist self->asyncResp->res); 144873df0db0SJames Feist return; 144973df0db0SJames Feist } 145073df0db0SJames Feist } 145173df0db0SJames Feist } 145273df0db0SJames Feist if (current == nullptr || supported == nullptr) 145373df0db0SJames Feist { 145473df0db0SJames Feist BMCWEB_LOG_ERROR << "SetPIDValues: thermal mode " 145573df0db0SJames Feist "iface invalid " 145673df0db0SJames Feist << path; 145773df0db0SJames Feist messages::internalError(self->asyncResp->res); 145873df0db0SJames Feist return; 145973df0db0SJames Feist } 146073df0db0SJames Feist self->currentProfile = *current; 146173df0db0SJames Feist self->supportedProfiles = *supported; 146273df0db0SJames Feist self->profileConnection = owner; 146373df0db0SJames Feist self->profilePath = path; 146473df0db0SJames Feist }, 146573df0db0SJames Feist owner, path, "org.freedesktop.DBus.Properties", "GetAll", 146673df0db0SJames Feist thermalModeIface); 14675b4aa86bSJames Feist }, 14685b4aa86bSJames Feist "xyz.openbmc_project.ObjectMapper", 14695b4aa86bSJames Feist "/xyz/openbmc_project/object_mapper", 14705b4aa86bSJames Feist "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, 147173df0db0SJames Feist std::array<const char*, 1>{thermalModeIface}); 147273df0db0SJames Feist } 147373df0db0SJames Feist ~SetPIDValues() 147473df0db0SJames Feist { 147573df0db0SJames Feist if (asyncResp->res.result() != boost::beast::http::status::ok) 147673df0db0SJames Feist { 147773df0db0SJames Feist return; 14785b4aa86bSJames Feist } 14795b4aa86bSJames Feist 148073df0db0SJames Feist std::shared_ptr<AsyncResp> response = asyncResp; 148173df0db0SJames Feist 148273df0db0SJames Feist if (profile) 148373df0db0SJames Feist { 148473df0db0SJames Feist if (std::find(supportedProfiles.begin(), supportedProfiles.end(), 148573df0db0SJames Feist *profile) == supportedProfiles.end()) 148673df0db0SJames Feist { 148773df0db0SJames Feist messages::actionParameterUnknown(response->res, "Profile", 148873df0db0SJames Feist *profile); 148973df0db0SJames Feist return; 149073df0db0SJames Feist } 149173df0db0SJames Feist currentProfile = *profile; 149273df0db0SJames Feist crow::connections::systemBus->async_method_call( 149373df0db0SJames Feist [response](const boost::system::error_code ec) { 149473df0db0SJames Feist if (ec) 149573df0db0SJames Feist { 149673df0db0SJames Feist BMCWEB_LOG_ERROR << "Error patching profile" << ec; 149773df0db0SJames Feist messages::internalError(response->res); 149873df0db0SJames Feist } 149973df0db0SJames Feist }, 150073df0db0SJames Feist profileConnection, profilePath, 150173df0db0SJames Feist "org.freedesktop.DBus.Properties", "Set", thermalModeIface, 150273df0db0SJames Feist "Current", std::variant<std::string>(*profile)); 150373df0db0SJames Feist } 150473df0db0SJames Feist 150573df0db0SJames Feist for (auto& containerPair : configuration) 150673df0db0SJames Feist { 150773df0db0SJames Feist auto& container = containerPair.second; 150873df0db0SJames Feist if (!container) 150973df0db0SJames Feist { 151073df0db0SJames Feist continue; 151173df0db0SJames Feist } 15126ee7f774SJames Feist BMCWEB_LOG_DEBUG << *container; 15136ee7f774SJames Feist 151473df0db0SJames Feist std::string& type = containerPair.first; 151573df0db0SJames Feist 151673df0db0SJames Feist for (nlohmann::json::iterator it = container->begin(); 151717a897dfSManojkiran Eda it != container->end(); ++it) 151873df0db0SJames Feist { 151973df0db0SJames Feist const auto& name = it.key(); 15206ee7f774SJames Feist BMCWEB_LOG_DEBUG << "looking for " << name; 15216ee7f774SJames Feist 152273df0db0SJames Feist auto pathItr = 152373df0db0SJames Feist std::find_if(managedObj.begin(), managedObj.end(), 152473df0db0SJames Feist [&name](const auto& obj) { 152573df0db0SJames Feist return boost::algorithm::ends_with( 152673df0db0SJames Feist obj.first.str, "/" + name); 152773df0db0SJames Feist }); 152873df0db0SJames Feist boost::container::flat_map<std::string, 152973df0db0SJames Feist dbus::utility::DbusVariantType> 153073df0db0SJames Feist output; 153173df0db0SJames Feist 153273df0db0SJames Feist output.reserve(16); // The pid interface length 153373df0db0SJames Feist 153473df0db0SJames Feist // determines if we're patching entity-manager or 153573df0db0SJames Feist // creating a new object 153673df0db0SJames Feist bool createNewObject = (pathItr == managedObj.end()); 15376ee7f774SJames Feist BMCWEB_LOG_DEBUG << "Found = " << !createNewObject; 15386ee7f774SJames Feist 153973df0db0SJames Feist std::string iface; 154073df0db0SJames Feist if (type == "PidControllers" || type == "FanControllers") 154173df0db0SJames Feist { 154273df0db0SJames Feist iface = pidConfigurationIface; 154373df0db0SJames Feist if (!createNewObject && 154473df0db0SJames Feist pathItr->second.find(pidConfigurationIface) == 154573df0db0SJames Feist pathItr->second.end()) 154673df0db0SJames Feist { 154773df0db0SJames Feist createNewObject = true; 154873df0db0SJames Feist } 154973df0db0SJames Feist } 155073df0db0SJames Feist else if (type == "FanZones") 155173df0db0SJames Feist { 155273df0db0SJames Feist iface = pidZoneConfigurationIface; 155373df0db0SJames Feist if (!createNewObject && 155473df0db0SJames Feist pathItr->second.find(pidZoneConfigurationIface) == 155573df0db0SJames Feist pathItr->second.end()) 155673df0db0SJames Feist { 155773df0db0SJames Feist 155873df0db0SJames Feist createNewObject = true; 155973df0db0SJames Feist } 156073df0db0SJames Feist } 156173df0db0SJames Feist else if (type == "StepwiseControllers") 156273df0db0SJames Feist { 156373df0db0SJames Feist iface = stepwiseConfigurationIface; 156473df0db0SJames Feist if (!createNewObject && 156573df0db0SJames Feist pathItr->second.find(stepwiseConfigurationIface) == 156673df0db0SJames Feist pathItr->second.end()) 156773df0db0SJames Feist { 156873df0db0SJames Feist createNewObject = true; 156973df0db0SJames Feist } 157073df0db0SJames Feist } 15716ee7f774SJames Feist 15726ee7f774SJames Feist if (createNewObject && it.value() == nullptr) 15736ee7f774SJames Feist { 15744e0453b1SGunnar Mills // can't delete a non-existent object 15756ee7f774SJames Feist messages::invalidObject(response->res, name); 15766ee7f774SJames Feist continue; 15776ee7f774SJames Feist } 15786ee7f774SJames Feist 15796ee7f774SJames Feist std::string path; 15806ee7f774SJames Feist if (pathItr != managedObj.end()) 15816ee7f774SJames Feist { 15826ee7f774SJames Feist path = pathItr->first.str; 15836ee7f774SJames Feist } 15846ee7f774SJames Feist 158573df0db0SJames Feist BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n"; 1586e69d9de2SJames Feist 1587e69d9de2SJames Feist // arbitrary limit to avoid attacks 1588e69d9de2SJames Feist constexpr const size_t controllerLimit = 500; 158914b0b8d5SJames Feist if (createNewObject && objectCount >= controllerLimit) 1590e69d9de2SJames Feist { 1591e69d9de2SJames Feist messages::resourceExhaustion(response->res, type); 1592e69d9de2SJames Feist continue; 1593e69d9de2SJames Feist } 1594e69d9de2SJames Feist 159573df0db0SJames Feist output["Name"] = boost::replace_all_copy(name, "_", " "); 159673df0db0SJames Feist 159773df0db0SJames Feist std::string chassis; 159873df0db0SJames Feist CreatePIDRet ret = createPidInterface( 15996ee7f774SJames Feist response, type, it, path, managedObj, createNewObject, 16006ee7f774SJames Feist output, chassis, currentProfile); 160173df0db0SJames Feist if (ret == CreatePIDRet::fail) 160273df0db0SJames Feist { 160373df0db0SJames Feist return; 160473df0db0SJames Feist } 16053174e4dfSEd Tanous if (ret == CreatePIDRet::del) 160673df0db0SJames Feist { 160773df0db0SJames Feist continue; 160873df0db0SJames Feist } 160973df0db0SJames Feist 161073df0db0SJames Feist if (!createNewObject) 161173df0db0SJames Feist { 161273df0db0SJames Feist for (const auto& property : output) 161373df0db0SJames Feist { 161473df0db0SJames Feist crow::connections::systemBus->async_method_call( 161573df0db0SJames Feist [response, 161673df0db0SJames Feist propertyName{std::string(property.first)}]( 161773df0db0SJames Feist const boost::system::error_code ec) { 161873df0db0SJames Feist if (ec) 161973df0db0SJames Feist { 162073df0db0SJames Feist BMCWEB_LOG_ERROR << "Error patching " 162173df0db0SJames Feist << propertyName << ": " 162273df0db0SJames Feist << ec; 162373df0db0SJames Feist messages::internalError(response->res); 162473df0db0SJames Feist return; 162573df0db0SJames Feist } 162673df0db0SJames Feist messages::success(response->res); 162773df0db0SJames Feist }, 16286ee7f774SJames Feist "xyz.openbmc_project.EntityManager", path, 162973df0db0SJames Feist "org.freedesktop.DBus.Properties", "Set", iface, 163073df0db0SJames Feist property.first, property.second); 163173df0db0SJames Feist } 163273df0db0SJames Feist } 163373df0db0SJames Feist else 163473df0db0SJames Feist { 163573df0db0SJames Feist if (chassis.empty()) 163673df0db0SJames Feist { 163773df0db0SJames Feist BMCWEB_LOG_ERROR << "Failed to get chassis from config"; 163873df0db0SJames Feist messages::invalidObject(response->res, name); 163973df0db0SJames Feist return; 164073df0db0SJames Feist } 164173df0db0SJames Feist 164273df0db0SJames Feist bool foundChassis = false; 164373df0db0SJames Feist for (const auto& obj : managedObj) 164473df0db0SJames Feist { 164573df0db0SJames Feist if (boost::algorithm::ends_with(obj.first.str, chassis)) 164673df0db0SJames Feist { 164773df0db0SJames Feist chassis = obj.first.str; 164873df0db0SJames Feist foundChassis = true; 164973df0db0SJames Feist break; 165073df0db0SJames Feist } 165173df0db0SJames Feist } 165273df0db0SJames Feist if (!foundChassis) 165373df0db0SJames Feist { 165473df0db0SJames Feist BMCWEB_LOG_ERROR << "Failed to find chassis on dbus"; 165573df0db0SJames Feist messages::resourceMissingAtURI( 165673df0db0SJames Feist response->res, "/redfish/v1/Chassis/" + chassis); 165773df0db0SJames Feist return; 165873df0db0SJames Feist } 165973df0db0SJames Feist 166073df0db0SJames Feist crow::connections::systemBus->async_method_call( 166173df0db0SJames Feist [response](const boost::system::error_code ec) { 166273df0db0SJames Feist if (ec) 166373df0db0SJames Feist { 166473df0db0SJames Feist BMCWEB_LOG_ERROR << "Error Adding Pid Object " 166573df0db0SJames Feist << ec; 166673df0db0SJames Feist messages::internalError(response->res); 166773df0db0SJames Feist return; 166873df0db0SJames Feist } 166973df0db0SJames Feist messages::success(response->res); 167073df0db0SJames Feist }, 167173df0db0SJames Feist "xyz.openbmc_project.EntityManager", chassis, 167273df0db0SJames Feist "xyz.openbmc_project.AddObject", "AddObject", output); 167373df0db0SJames Feist } 167473df0db0SJames Feist } 167573df0db0SJames Feist } 167673df0db0SJames Feist } 167773df0db0SJames Feist std::shared_ptr<AsyncResp> asyncResp; 167873df0db0SJames Feist std::vector<std::pair<std::string, std::optional<nlohmann::json>>> 167973df0db0SJames Feist configuration; 168073df0db0SJames Feist std::optional<std::string> profile; 168173df0db0SJames Feist dbus::utility::ManagedObjectType managedObj; 168273df0db0SJames Feist std::vector<std::string> supportedProfiles; 168373df0db0SJames Feist std::string currentProfile; 168473df0db0SJames Feist std::string profileConnection; 168573df0db0SJames Feist std::string profilePath; 168614b0b8d5SJames Feist size_t objectCount = 0; 168773df0db0SJames Feist }; 168873df0db0SJames Feist 1689*071d8fdfSSunnySrivastava1984 /** 1690*071d8fdfSSunnySrivastava1984 * @brief Retrieves BMC manager location data over DBus 1691*071d8fdfSSunnySrivastava1984 * 1692*071d8fdfSSunnySrivastava1984 * @param[in] aResp Shared pointer for completing asynchronous calls 1693*071d8fdfSSunnySrivastava1984 * @param[in] connectionName - service name 1694*071d8fdfSSunnySrivastava1984 * @param[in] path - object path 1695*071d8fdfSSunnySrivastava1984 * @return none 1696*071d8fdfSSunnySrivastava1984 */ 1697*071d8fdfSSunnySrivastava1984 inline void getLocation(const std::shared_ptr<AsyncResp>& aResp, 1698*071d8fdfSSunnySrivastava1984 const std::string& connectionName, 1699*071d8fdfSSunnySrivastava1984 const std::string& path) 1700*071d8fdfSSunnySrivastava1984 { 1701*071d8fdfSSunnySrivastava1984 BMCWEB_LOG_DEBUG << "Get BMC manager Location data."; 1702*071d8fdfSSunnySrivastava1984 1703*071d8fdfSSunnySrivastava1984 crow::connections::systemBus->async_method_call( 1704*071d8fdfSSunnySrivastava1984 [aResp](const boost::system::error_code ec, 1705*071d8fdfSSunnySrivastava1984 const std::variant<std::string>& property) { 1706*071d8fdfSSunnySrivastava1984 if (ec) 1707*071d8fdfSSunnySrivastava1984 { 1708*071d8fdfSSunnySrivastava1984 BMCWEB_LOG_DEBUG << "DBUS response error for " 1709*071d8fdfSSunnySrivastava1984 "Location"; 1710*071d8fdfSSunnySrivastava1984 messages::internalError(aResp->res); 1711*071d8fdfSSunnySrivastava1984 return; 1712*071d8fdfSSunnySrivastava1984 } 1713*071d8fdfSSunnySrivastava1984 1714*071d8fdfSSunnySrivastava1984 const std::string* value = std::get_if<std::string>(&property); 1715*071d8fdfSSunnySrivastava1984 1716*071d8fdfSSunnySrivastava1984 if (value == nullptr) 1717*071d8fdfSSunnySrivastava1984 { 1718*071d8fdfSSunnySrivastava1984 // illegal value 1719*071d8fdfSSunnySrivastava1984 messages::internalError(aResp->res); 1720*071d8fdfSSunnySrivastava1984 return; 1721*071d8fdfSSunnySrivastava1984 } 1722*071d8fdfSSunnySrivastava1984 1723*071d8fdfSSunnySrivastava1984 aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] = 1724*071d8fdfSSunnySrivastava1984 *value; 1725*071d8fdfSSunnySrivastava1984 }, 1726*071d8fdfSSunnySrivastava1984 connectionName, path, "org.freedesktop.DBus.Properties", "Get", 1727*071d8fdfSSunnySrivastava1984 "xyz.openbmc_project.Inventory.Decorator." 1728*071d8fdfSSunnySrivastava1984 "LocationCode", 1729*071d8fdfSSunnySrivastava1984 "LocationCode"); 1730*071d8fdfSSunnySrivastava1984 } 1731*071d8fdfSSunnySrivastava1984 173273df0db0SJames Feist class Manager : public Node 173373df0db0SJames Feist { 173473df0db0SJames Feist public: 173552cc112dSEd Tanous Manager(App& app) : Node(app, "/redfish/v1/Managers/bmc/") 173673df0db0SJames Feist { 173752cc112dSEd Tanous 173852cc112dSEd Tanous uuid = persistent_data::getConfig().systemUuid; 173973df0db0SJames Feist entityPrivileges = { 174073df0db0SJames Feist {boost::beast::http::verb::get, {{"Login"}}}, 174173df0db0SJames Feist {boost::beast::http::verb::head, {{"Login"}}}, 174273df0db0SJames Feist {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, 174373df0db0SJames Feist {boost::beast::http::verb::put, {{"ConfigureManager"}}}, 174473df0db0SJames Feist {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, 174573df0db0SJames Feist {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; 174673df0db0SJames Feist } 174773df0db0SJames Feist 174873df0db0SJames Feist private: 1749cb13a392SEd Tanous void doGet(crow::Response& res, const crow::Request&, 1750cb13a392SEd Tanous const std::vector<std::string>&) override 17511abe55efSEd Tanous { 17520f74e643SEd Tanous res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc"; 1753*071d8fdfSSunnySrivastava1984 res.jsonValue["@odata.type"] = "#Manager.v1_11_0.Manager"; 17540f74e643SEd Tanous res.jsonValue["Id"] = "bmc"; 17550f74e643SEd Tanous res.jsonValue["Name"] = "OpenBmc Manager"; 17560f74e643SEd Tanous res.jsonValue["Description"] = "Baseboard Management Controller"; 17570f74e643SEd Tanous res.jsonValue["PowerState"] = "On"; 1758029573d4SEd Tanous res.jsonValue["Status"] = {{"State", "Enabled"}, {"Health", "OK"}}; 17590f74e643SEd Tanous res.jsonValue["ManagerType"] = "BMC"; 17603602e232SEd Tanous res.jsonValue["UUID"] = systemd_utils::getUuid(); 17613602e232SEd Tanous res.jsonValue["ServiceEntryPointUUID"] = uuid; 17620f74e643SEd Tanous res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model 17630f74e643SEd Tanous 17640f74e643SEd Tanous res.jsonValue["LogServices"] = { 17650f74e643SEd Tanous {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}}; 17660f74e643SEd Tanous 17670f74e643SEd Tanous res.jsonValue["NetworkProtocol"] = { 17680f74e643SEd Tanous {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}}; 17690f74e643SEd Tanous 17700f74e643SEd Tanous res.jsonValue["EthernetInterfaces"] = { 17710f74e643SEd Tanous {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}}; 1772107077deSPrzemyslaw Czarnowski 1773107077deSPrzemyslaw Czarnowski #ifdef BMCWEB_ENABLE_VM_NBDPROXY 1774107077deSPrzemyslaw Czarnowski res.jsonValue["VirtualMedia"] = { 1775107077deSPrzemyslaw Czarnowski {"@odata.id", "/redfish/v1/Managers/bmc/VirtualMedia"}}; 1776107077deSPrzemyslaw Czarnowski #endif // BMCWEB_ENABLE_VM_NBDPROXY 1777107077deSPrzemyslaw Czarnowski 17780f74e643SEd Tanous // default oem data 17790f74e643SEd Tanous nlohmann::json& oem = res.jsonValue["Oem"]; 17800f74e643SEd Tanous nlohmann::json& oemOpenbmc = oem["OpenBmc"]; 17810f74e643SEd Tanous oem["@odata.type"] = "#OemManager.Oem"; 17820f74e643SEd Tanous oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem"; 17830f74e643SEd Tanous oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc"; 17840f74e643SEd Tanous oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc"; 1785cfcd5f6bSMarri Devender Rao oemOpenbmc["Certificates"] = { 1786cfcd5f6bSMarri Devender Rao {"@odata.id", "/redfish/v1/Managers/bmc/Truststore/Certificates"}}; 17870f74e643SEd Tanous 17882a5c4407SGunnar Mills // Manager.Reset (an action) can be many values, OpenBMC only supports 17892a5c4407SGunnar Mills // BMC reboot. 17902a5c4407SGunnar Mills nlohmann::json& managerReset = 17910f74e643SEd Tanous res.jsonValue["Actions"]["#Manager.Reset"]; 17922a5c4407SGunnar Mills managerReset["target"] = 1793ed5befbdSJennifer Lee "/redfish/v1/Managers/bmc/Actions/Manager.Reset"; 17941cb1a9e6SAppaRao Puli managerReset["@Redfish.ActionInfo"] = 17951cb1a9e6SAppaRao Puli "/redfish/v1/Managers/bmc/ResetActionInfo"; 1796ca537928SJennifer Lee 17973e40fc74SGunnar Mills // ResetToDefaults (Factory Reset) has values like 17983e40fc74SGunnar Mills // PreserveNetworkAndUsers and PreserveNetwork that aren't supported 17993e40fc74SGunnar Mills // on OpenBMC 18003e40fc74SGunnar Mills nlohmann::json& resetToDefaults = 18013e40fc74SGunnar Mills res.jsonValue["Actions"]["#Manager.ResetToDefaults"]; 18023e40fc74SGunnar Mills resetToDefaults["target"] = 18033e40fc74SGunnar Mills "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults"; 18043e40fc74SGunnar Mills resetToDefaults["ResetType@Redfish.AllowableValues"] = {"ResetAll"}; 18053e40fc74SGunnar Mills 1806cb92c03bSAndrew Geissler res.jsonValue["DateTime"] = crow::utility::dateTimeNow(); 1807474bfad5SSantosh Puranik 1808f8c3e6f0SKuiying Wang // Fill in SerialConsole info 1809474bfad5SSantosh Puranik res.jsonValue["SerialConsole"]["ServiceEnabled"] = true; 1810f8c3e6f0SKuiying Wang res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15; 1811474bfad5SSantosh Puranik res.jsonValue["SerialConsole"]["ConnectTypesSupported"] = {"IPMI", 1812474bfad5SSantosh Puranik "SSH"}; 1813ef47bb18SSantosh Puranik #ifdef BMCWEB_ENABLE_KVM 1814f8c3e6f0SKuiying Wang // Fill in GraphicalConsole info 1815ef47bb18SSantosh Puranik res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true; 1816704fae6cSJae Hyun Yoo res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4; 1817ef47bb18SSantosh Puranik res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] = {"KVMIP"}; 1818ef47bb18SSantosh Puranik #endif // BMCWEB_ENABLE_KVM 1819474bfad5SSantosh Puranik 1820603a6640SGunnar Mills res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1; 1821603a6640SGunnar Mills res.jsonValue["Links"]["ManagerForServers"] = { 1822603a6640SGunnar Mills {{"@odata.id", "/redfish/v1/Systems/system"}}}; 182326f03899SShawn McCarney 1824ed5befbdSJennifer Lee std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 18255b4aa86bSJames Feist 1826b49ac873SJames Feist auto health = std::make_shared<HealthPopulate>(asyncResp); 1827b49ac873SJames Feist health->isManagersHealth = true; 1828b49ac873SJames Feist health->populate(); 1829b49ac873SJames Feist 1830f97ddba7SGunnar Mills fw_util::populateFirmwareInformation(asyncResp, fw_util::bmcPurpose, 183172d566d9SGunnar Mills "FirmwareVersion", true); 18320f6b00bdSJames Feist 18334bf2b033SGunnar Mills getLastResetTime(asyncResp); 18344bf2b033SGunnar Mills 183573df0db0SJames Feist auto pids = std::make_shared<GetPIDValues>(asyncResp); 183673df0db0SJames Feist pids->run(); 1837c5d03ff4SJennifer Lee 1838c5d03ff4SJennifer Lee getMainChassisId(asyncResp, [](const std::string& chassisId, 1839b5a76932SEd Tanous const std::shared_ptr<AsyncResp>& aRsp) { 1840c5d03ff4SJennifer Lee aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1; 1841c5d03ff4SJennifer Lee aRsp->res.jsonValue["Links"]["ManagerForChassis"] = { 1842c5d03ff4SJennifer Lee {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}}; 18432c0feb00SJason M. Bills aRsp->res.jsonValue["Links"]["ManagerInChassis"] = { 18442c0feb00SJason M. Bills {"@odata.id", "/redfish/v1/Chassis/" + chassisId}}; 1845c5d03ff4SJennifer Lee }); 18460f6b00bdSJames Feist 18470f6b00bdSJames Feist static bool started = false; 18480f6b00bdSJames Feist 18490f6b00bdSJames Feist if (!started) 18500f6b00bdSJames Feist { 18510f6b00bdSJames Feist crow::connections::systemBus->async_method_call( 18520f6b00bdSJames Feist [asyncResp](const boost::system::error_code ec, 18530f6b00bdSJames Feist const std::variant<double>& resp) { 18540f6b00bdSJames Feist if (ec) 18550f6b00bdSJames Feist { 18560f6b00bdSJames Feist BMCWEB_LOG_ERROR << "Error while getting progress"; 18570f6b00bdSJames Feist messages::internalError(asyncResp->res); 18580f6b00bdSJames Feist return; 18590f6b00bdSJames Feist } 18600f6b00bdSJames Feist const double* val = std::get_if<double>(&resp); 18610f6b00bdSJames Feist if (val == nullptr) 18620f6b00bdSJames Feist { 18630f6b00bdSJames Feist BMCWEB_LOG_ERROR 18640f6b00bdSJames Feist << "Invalid response while getting progress"; 18650f6b00bdSJames Feist messages::internalError(asyncResp->res); 18660f6b00bdSJames Feist return; 18670f6b00bdSJames Feist } 18680f6b00bdSJames Feist if (*val < 1.0) 18690f6b00bdSJames Feist { 18700f6b00bdSJames Feist asyncResp->res.jsonValue["Status"]["State"] = 18710f6b00bdSJames Feist "Starting"; 18720f6b00bdSJames Feist started = true; 18730f6b00bdSJames Feist } 18740f6b00bdSJames Feist }, 18750f6b00bdSJames Feist "org.freedesktop.systemd1", "/org/freedesktop/systemd1", 18760f6b00bdSJames Feist "org.freedesktop.DBus.Properties", "Get", 18770f6b00bdSJames Feist "org.freedesktop.systemd1.Manager", "Progress"); 18780f6b00bdSJames Feist } 1879ef6ca6e4SChicago Duan 1880ef6ca6e4SChicago Duan crow::connections::systemBus->async_method_call( 1881ef6ca6e4SChicago Duan [asyncResp]( 1882ef6ca6e4SChicago Duan const boost::system::error_code ec, 1883ef6ca6e4SChicago Duan const std::vector<std::pair< 1884ef6ca6e4SChicago Duan std::string, std::vector<std::pair< 1885ef6ca6e4SChicago Duan std::string, std::vector<std::string>>>>>& 1886ef6ca6e4SChicago Duan subtree) { 1887ef6ca6e4SChicago Duan if (ec) 1888ef6ca6e4SChicago Duan { 1889ef6ca6e4SChicago Duan BMCWEB_LOG_DEBUG << "D-Bus response error on GetSubTree " 1890ef6ca6e4SChicago Duan << ec; 1891ef6ca6e4SChicago Duan return; 1892ef6ca6e4SChicago Duan } 1893ef6ca6e4SChicago Duan if (subtree.size() == 0) 1894ef6ca6e4SChicago Duan { 1895ef6ca6e4SChicago Duan BMCWEB_LOG_DEBUG << "Can't find bmc D-Bus object!"; 1896ef6ca6e4SChicago Duan return; 1897ef6ca6e4SChicago Duan } 1898ef6ca6e4SChicago Duan // Assume only 1 bmc D-Bus object 1899ef6ca6e4SChicago Duan // Throw an error if there is more than 1 1900ef6ca6e4SChicago Duan if (subtree.size() > 1) 1901ef6ca6e4SChicago Duan { 1902ef6ca6e4SChicago Duan BMCWEB_LOG_DEBUG << "Found more than 1 bmc D-Bus object!"; 1903ef6ca6e4SChicago Duan messages::internalError(asyncResp->res); 1904ef6ca6e4SChicago Duan return; 1905ef6ca6e4SChicago Duan } 1906ef6ca6e4SChicago Duan 1907ef6ca6e4SChicago Duan if (subtree[0].first.empty() || subtree[0].second.size() != 1) 1908ef6ca6e4SChicago Duan { 1909ef6ca6e4SChicago Duan BMCWEB_LOG_DEBUG << "Error getting bmc D-Bus object!"; 1910ef6ca6e4SChicago Duan messages::internalError(asyncResp->res); 1911ef6ca6e4SChicago Duan return; 1912ef6ca6e4SChicago Duan } 1913ef6ca6e4SChicago Duan 1914ef6ca6e4SChicago Duan const std::string& path = subtree[0].first; 1915ef6ca6e4SChicago Duan const std::string& connectionName = subtree[0].second[0].first; 1916ef6ca6e4SChicago Duan 1917*071d8fdfSSunnySrivastava1984 for (const auto& interfaceName : subtree[0].second[0].second) 1918*071d8fdfSSunnySrivastava1984 { 1919*071d8fdfSSunnySrivastava1984 if (interfaceName == 1920*071d8fdfSSunnySrivastava1984 "xyz.openbmc_project.Inventory.Decorator.Asset") 1921*071d8fdfSSunnySrivastava1984 { 1922ef6ca6e4SChicago Duan crow::connections::systemBus->async_method_call( 1923ef6ca6e4SChicago Duan [asyncResp]( 1924ef6ca6e4SChicago Duan const boost::system::error_code ec, 1925*071d8fdfSSunnySrivastava1984 const std::vector<std::pair< 1926*071d8fdfSSunnySrivastava1984 std::string, std::variant<std::string>>>& 1927ef6ca6e4SChicago Duan propertiesList) { 1928ef6ca6e4SChicago Duan if (ec) 1929ef6ca6e4SChicago Duan { 1930ef6ca6e4SChicago Duan BMCWEB_LOG_DEBUG << "Can't get bmc asset!"; 1931ef6ca6e4SChicago Duan return; 1932ef6ca6e4SChicago Duan } 1933ef6ca6e4SChicago Duan for (const std::pair<std::string, 1934ef6ca6e4SChicago Duan std::variant<std::string>>& 1935ef6ca6e4SChicago Duan property : propertiesList) 1936ef6ca6e4SChicago Duan { 1937*071d8fdfSSunnySrivastava1984 const std::string& propertyName = 1938*071d8fdfSSunnySrivastava1984 property.first; 1939ef6ca6e4SChicago Duan 1940ef6ca6e4SChicago Duan if ((propertyName == "PartNumber") || 1941ef6ca6e4SChicago Duan (propertyName == "SerialNumber") || 1942*071d8fdfSSunnySrivastava1984 (propertyName == "Manufacturer") || 1943*071d8fdfSSunnySrivastava1984 (propertyName == "Model") || 1944*071d8fdfSSunnySrivastava1984 (propertyName == "SparePartNumber")) 1945ef6ca6e4SChicago Duan { 1946ef6ca6e4SChicago Duan const std::string* value = 1947*071d8fdfSSunnySrivastava1984 std::get_if<std::string>( 1948*071d8fdfSSunnySrivastava1984 &property.second); 1949ef6ca6e4SChicago Duan if (value == nullptr) 1950ef6ca6e4SChicago Duan { 1951ef6ca6e4SChicago Duan // illegal property 1952*071d8fdfSSunnySrivastava1984 messages::internalError( 1953*071d8fdfSSunnySrivastava1984 asyncResp->res); 1954ef6ca6e4SChicago Duan continue; 1955ef6ca6e4SChicago Duan } 1956*071d8fdfSSunnySrivastava1984 asyncResp->res.jsonValue[propertyName] = 1957*071d8fdfSSunnySrivastava1984 *value; 1958ef6ca6e4SChicago Duan } 1959ef6ca6e4SChicago Duan } 1960ef6ca6e4SChicago Duan }, 1961*071d8fdfSSunnySrivastava1984 connectionName, path, 1962*071d8fdfSSunnySrivastava1984 "org.freedesktop.DBus.Properties", "GetAll", 1963*071d8fdfSSunnySrivastava1984 "xyz.openbmc_project.Inventory.Decorator.Asset"); 1964*071d8fdfSSunnySrivastava1984 } 1965*071d8fdfSSunnySrivastava1984 else if (interfaceName == "xyz.openbmc_project.Inventory." 1966*071d8fdfSSunnySrivastava1984 "Decorator.LocationCode") 1967*071d8fdfSSunnySrivastava1984 { 1968*071d8fdfSSunnySrivastava1984 getLocation(asyncResp, connectionName, path); 1969*071d8fdfSSunnySrivastava1984 } 1970*071d8fdfSSunnySrivastava1984 } 1971ef6ca6e4SChicago Duan }, 1972ef6ca6e4SChicago Duan "xyz.openbmc_project.ObjectMapper", 1973ef6ca6e4SChicago Duan "/xyz/openbmc_project/object_mapper", 1974ef6ca6e4SChicago Duan "xyz.openbmc_project.ObjectMapper", "GetSubTree", 1975ef6ca6e4SChicago Duan "/xyz/openbmc_project/inventory", int32_t(0), 1976ef6ca6e4SChicago Duan std::array<const char*, 1>{ 1977ef6ca6e4SChicago Duan "xyz.openbmc_project.Inventory.Item.Bmc"}); 197883ff9ab6SJames Feist } 19795b4aa86bSJames Feist 19805b4aa86bSJames Feist void doPatch(crow::Response& res, const crow::Request& req, 1981cb13a392SEd Tanous const std::vector<std::string>&) override 19825b4aa86bSJames Feist { 19830627a2c7SEd Tanous std::optional<nlohmann::json> oem; 19844bfefa74SGunnar Mills std::optional<nlohmann::json> links; 1985af5d6058SSantosh Puranik std::optional<std::string> datetime; 198641352c24SSantosh Puranik std::shared_ptr<AsyncResp> response = std::make_shared<AsyncResp>(res); 19870627a2c7SEd Tanous 198841352c24SSantosh Puranik if (!json_util::readJson(req, response->res, "Oem", oem, "DateTime", 19894bfefa74SGunnar Mills datetime, "Links", links)) 199083ff9ab6SJames Feist { 199183ff9ab6SJames Feist return; 199283ff9ab6SJames Feist } 19930627a2c7SEd Tanous 19940627a2c7SEd Tanous if (oem) 199583ff9ab6SJames Feist { 19965f2caaefSJames Feist std::optional<nlohmann::json> openbmc; 199743b761d0SEd Tanous if (!redfish::json_util::readJson(*oem, res, "OpenBmc", openbmc)) 199883ff9ab6SJames Feist { 1999a0744d38SGunnar Mills BMCWEB_LOG_ERROR << "Illegal Property " << oem->dump(); 200083ff9ab6SJames Feist return; 200183ff9ab6SJames Feist } 20025f2caaefSJames Feist if (openbmc) 200383ff9ab6SJames Feist { 20045f2caaefSJames Feist std::optional<nlohmann::json> fan; 200543b761d0SEd Tanous if (!redfish::json_util::readJson(*openbmc, res, "Fan", fan)) 200683ff9ab6SJames Feist { 2007a0744d38SGunnar Mills BMCWEB_LOG_ERROR << "Illegal Property " << openbmc->dump(); 200883ff9ab6SJames Feist return; 200983ff9ab6SJames Feist } 20105f2caaefSJames Feist if (fan) 201183ff9ab6SJames Feist { 201273df0db0SJames Feist auto pid = std::make_shared<SetPIDValues>(response, *fan); 201373df0db0SJames Feist pid->run(); 201483ff9ab6SJames Feist } 201583ff9ab6SJames Feist } 201683ff9ab6SJames Feist } 20174bfefa74SGunnar Mills if (links) 20184bfefa74SGunnar Mills { 20194bfefa74SGunnar Mills std::optional<nlohmann::json> activeSoftwareImage; 20204bfefa74SGunnar Mills if (!redfish::json_util::readJson( 20214bfefa74SGunnar Mills *links, res, "ActiveSoftwareImage", activeSoftwareImage)) 20224bfefa74SGunnar Mills { 20234bfefa74SGunnar Mills return; 20244bfefa74SGunnar Mills } 20254bfefa74SGunnar Mills if (activeSoftwareImage) 20264bfefa74SGunnar Mills { 20274bfefa74SGunnar Mills std::optional<std::string> odataId; 20284bfefa74SGunnar Mills if (!json_util::readJson(*activeSoftwareImage, res, "@odata.id", 20294bfefa74SGunnar Mills odataId)) 20304bfefa74SGunnar Mills { 20314bfefa74SGunnar Mills return; 20324bfefa74SGunnar Mills } 20334bfefa74SGunnar Mills 20344bfefa74SGunnar Mills if (odataId) 20354bfefa74SGunnar Mills { 2036f23b7296SEd Tanous setActiveFirmwareImage(response, *odataId); 20374bfefa74SGunnar Mills } 20384bfefa74SGunnar Mills } 20394bfefa74SGunnar Mills } 2040af5d6058SSantosh Puranik if (datetime) 2041af5d6058SSantosh Puranik { 2042af5d6058SSantosh Puranik setDateTime(response, std::move(*datetime)); 2043af5d6058SSantosh Puranik } 2044af5d6058SSantosh Puranik } 2045af5d6058SSantosh Puranik 2046b5a76932SEd Tanous void getLastResetTime(const std::shared_ptr<AsyncResp>& aResp) 20474bf2b033SGunnar Mills { 20484bf2b033SGunnar Mills BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time"; 20494bf2b033SGunnar Mills 20504bf2b033SGunnar Mills crow::connections::systemBus->async_method_call( 20514bf2b033SGunnar Mills [aResp](const boost::system::error_code ec, 20524bf2b033SGunnar Mills std::variant<uint64_t>& lastResetTime) { 20534bf2b033SGunnar Mills if (ec) 20544bf2b033SGunnar Mills { 20554bf2b033SGunnar Mills BMCWEB_LOG_DEBUG << "D-BUS response error " << ec; 20564bf2b033SGunnar Mills return; 20574bf2b033SGunnar Mills } 20584bf2b033SGunnar Mills 20594bf2b033SGunnar Mills const uint64_t* lastResetTimePtr = 20604bf2b033SGunnar Mills std::get_if<uint64_t>(&lastResetTime); 20614bf2b033SGunnar Mills 20624bf2b033SGunnar Mills if (!lastResetTimePtr) 20634bf2b033SGunnar Mills { 20644bf2b033SGunnar Mills messages::internalError(aResp->res); 20654bf2b033SGunnar Mills return; 20664bf2b033SGunnar Mills } 20674bf2b033SGunnar Mills // LastRebootTime is epoch time, in milliseconds 20684bf2b033SGunnar Mills // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19 20694bf2b033SGunnar Mills time_t lastResetTimeStamp = 20704bf2b033SGunnar Mills static_cast<time_t>(*lastResetTimePtr / 1000); 20714bf2b033SGunnar Mills 20724bf2b033SGunnar Mills // Convert to ISO 8601 standard 20734bf2b033SGunnar Mills aResp->res.jsonValue["LastResetTime"] = 20744bf2b033SGunnar Mills crow::utility::getDateTime(lastResetTimeStamp); 20754bf2b033SGunnar Mills }, 20764bf2b033SGunnar Mills "xyz.openbmc_project.State.BMC", "/xyz/openbmc_project/state/bmc0", 20774bf2b033SGunnar Mills "org.freedesktop.DBus.Properties", "Get", 20784bf2b033SGunnar Mills "xyz.openbmc_project.State.BMC", "LastRebootTime"); 20794bf2b033SGunnar Mills } 20804bf2b033SGunnar Mills 20814bfefa74SGunnar Mills /** 20824bfefa74SGunnar Mills * @brief Set the running firmware image 20834bfefa74SGunnar Mills * 20844bfefa74SGunnar Mills * @param[i,o] aResp - Async response object 20854bfefa74SGunnar Mills * @param[i] runningFirmwareTarget - Image to make the running image 20864bfefa74SGunnar Mills * 20874bfefa74SGunnar Mills * @return void 20884bfefa74SGunnar Mills */ 2089b5a76932SEd Tanous void setActiveFirmwareImage(const std::shared_ptr<AsyncResp>& aResp, 2090f23b7296SEd Tanous const std::string& runningFirmwareTarget) 20914bfefa74SGunnar Mills { 20924bfefa74SGunnar Mills // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id> 2093f23b7296SEd Tanous std::string::size_type idPos = runningFirmwareTarget.rfind('/'); 20944bfefa74SGunnar Mills if (idPos == std::string::npos) 20954bfefa74SGunnar Mills { 20964bfefa74SGunnar Mills messages::propertyValueNotInList(aResp->res, runningFirmwareTarget, 20974bfefa74SGunnar Mills "@odata.id"); 20984bfefa74SGunnar Mills BMCWEB_LOG_DEBUG << "Can't parse firmware ID!"; 20994bfefa74SGunnar Mills return; 21004bfefa74SGunnar Mills } 21014bfefa74SGunnar Mills idPos++; 21024bfefa74SGunnar Mills if (idPos >= runningFirmwareTarget.size()) 21034bfefa74SGunnar Mills { 21044bfefa74SGunnar Mills messages::propertyValueNotInList(aResp->res, runningFirmwareTarget, 21054bfefa74SGunnar Mills "@odata.id"); 21064bfefa74SGunnar Mills BMCWEB_LOG_DEBUG << "Invalid firmware ID."; 21074bfefa74SGunnar Mills return; 21084bfefa74SGunnar Mills } 21094bfefa74SGunnar Mills std::string firmwareId = runningFirmwareTarget.substr(idPos); 21104bfefa74SGunnar Mills 21114bfefa74SGunnar Mills // Make sure the image is valid before setting priority 21124bfefa74SGunnar Mills crow::connections::systemBus->async_method_call( 21134bfefa74SGunnar Mills [aResp, firmwareId, 21144bfefa74SGunnar Mills runningFirmwareTarget](const boost::system::error_code ec, 21154bfefa74SGunnar Mills ManagedObjectType& subtree) { 21164bfefa74SGunnar Mills if (ec) 21174bfefa74SGunnar Mills { 21184bfefa74SGunnar Mills BMCWEB_LOG_DEBUG << "D-Bus response error getting objects."; 21194bfefa74SGunnar Mills messages::internalError(aResp->res); 21204bfefa74SGunnar Mills return; 21214bfefa74SGunnar Mills } 21224bfefa74SGunnar Mills 21234bfefa74SGunnar Mills if (subtree.size() == 0) 21244bfefa74SGunnar Mills { 21254bfefa74SGunnar Mills BMCWEB_LOG_DEBUG << "Can't find image!"; 21264bfefa74SGunnar Mills messages::internalError(aResp->res); 21274bfefa74SGunnar Mills return; 21284bfefa74SGunnar Mills } 21294bfefa74SGunnar Mills 21304bfefa74SGunnar Mills bool foundImage = false; 21314bfefa74SGunnar Mills for (auto& object : subtree) 21324bfefa74SGunnar Mills { 21334bfefa74SGunnar Mills const std::string& path = 21344bfefa74SGunnar Mills static_cast<const std::string&>(object.first); 2135f23b7296SEd Tanous std::size_t idPos2 = path.rfind('/'); 21364bfefa74SGunnar Mills 21374bfefa74SGunnar Mills if (idPos2 == std::string::npos) 21384bfefa74SGunnar Mills { 21394bfefa74SGunnar Mills continue; 21404bfefa74SGunnar Mills } 21414bfefa74SGunnar Mills 21424bfefa74SGunnar Mills idPos2++; 21434bfefa74SGunnar Mills if (idPos2 >= path.size()) 21444bfefa74SGunnar Mills { 21454bfefa74SGunnar Mills continue; 21464bfefa74SGunnar Mills } 21474bfefa74SGunnar Mills 21484bfefa74SGunnar Mills if (path.substr(idPos2) == firmwareId) 21494bfefa74SGunnar Mills { 21504bfefa74SGunnar Mills foundImage = true; 21514bfefa74SGunnar Mills break; 21524bfefa74SGunnar Mills } 21534bfefa74SGunnar Mills } 21544bfefa74SGunnar Mills 21554bfefa74SGunnar Mills if (!foundImage) 21564bfefa74SGunnar Mills { 21574bfefa74SGunnar Mills messages::propertyValueNotInList( 21584bfefa74SGunnar Mills aResp->res, runningFirmwareTarget, "@odata.id"); 21594bfefa74SGunnar Mills BMCWEB_LOG_DEBUG << "Invalid firmware ID."; 21604bfefa74SGunnar Mills return; 21614bfefa74SGunnar Mills } 21624bfefa74SGunnar Mills 21634bfefa74SGunnar Mills BMCWEB_LOG_DEBUG << "Setting firmware version " + firmwareId + 21644bfefa74SGunnar Mills " to priority 0."; 21654bfefa74SGunnar Mills 21664bfefa74SGunnar Mills // Only support Immediate 21674bfefa74SGunnar Mills // An addition could be a Redfish Setting like 21684bfefa74SGunnar Mills // ActiveSoftwareImageApplyTime and support OnReset 21694bfefa74SGunnar Mills crow::connections::systemBus->async_method_call( 21704bfefa74SGunnar Mills [aResp](const boost::system::error_code ec) { 21714bfefa74SGunnar Mills if (ec) 21724bfefa74SGunnar Mills { 21734bfefa74SGunnar Mills BMCWEB_LOG_DEBUG << "D-Bus response error setting."; 21744bfefa74SGunnar Mills messages::internalError(aResp->res); 21754bfefa74SGunnar Mills return; 21764bfefa74SGunnar Mills } 21774bfefa74SGunnar Mills doBMCGracefulRestart(aResp); 21784bfefa74SGunnar Mills }, 21794bfefa74SGunnar Mills 21804bfefa74SGunnar Mills "xyz.openbmc_project.Software.BMC.Updater", 21814bfefa74SGunnar Mills "/xyz/openbmc_project/software/" + firmwareId, 21824bfefa74SGunnar Mills "org.freedesktop.DBus.Properties", "Set", 21834bfefa74SGunnar Mills "xyz.openbmc_project.Software.RedundancyPriority", 21844bfefa74SGunnar Mills "Priority", std::variant<uint8_t>(static_cast<uint8_t>(0))); 21854bfefa74SGunnar Mills }, 21864bfefa74SGunnar Mills "xyz.openbmc_project.Software.BMC.Updater", 21874bfefa74SGunnar Mills "/xyz/openbmc_project/software", 21884bfefa74SGunnar Mills "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 21894bfefa74SGunnar Mills } 21904bfefa74SGunnar Mills 2191af5d6058SSantosh Puranik void setDateTime(std::shared_ptr<AsyncResp> aResp, 2192af5d6058SSantosh Puranik std::string datetime) const 2193af5d6058SSantosh Puranik { 2194af5d6058SSantosh Puranik BMCWEB_LOG_DEBUG << "Set date time: " << datetime; 2195af5d6058SSantosh Puranik 2196af5d6058SSantosh Puranik std::stringstream stream(datetime); 2197af5d6058SSantosh Puranik // Convert from ISO 8601 to boost local_time 2198af5d6058SSantosh Puranik // (BMC only has time in UTC) 2199af5d6058SSantosh Puranik boost::posix_time::ptime posixTime; 2200af5d6058SSantosh Puranik boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1)); 2201af5d6058SSantosh Puranik // Facet gets deleted with the stringsteam 2202af5d6058SSantosh Puranik auto ifc = std::make_unique<boost::local_time::local_time_input_facet>( 2203af5d6058SSantosh Puranik "%Y-%m-%d %H:%M:%S%F %ZP"); 2204af5d6058SSantosh Puranik stream.imbue(std::locale(stream.getloc(), ifc.release())); 2205af5d6058SSantosh Puranik 2206af5d6058SSantosh Puranik boost::local_time::local_date_time ldt( 2207af5d6058SSantosh Puranik boost::local_time::not_a_date_time); 2208af5d6058SSantosh Puranik 2209af5d6058SSantosh Puranik if (stream >> ldt) 2210af5d6058SSantosh Puranik { 2211af5d6058SSantosh Puranik posixTime = ldt.utc_time(); 2212af5d6058SSantosh Puranik boost::posix_time::time_duration dur = posixTime - epoch; 2213af5d6058SSantosh Puranik uint64_t durMicroSecs = 2214af5d6058SSantosh Puranik static_cast<uint64_t>(dur.total_microseconds()); 2215af5d6058SSantosh Puranik crow::connections::systemBus->async_method_call( 2216af5d6058SSantosh Puranik [aResp{std::move(aResp)}, datetime{std::move(datetime)}]( 2217af5d6058SSantosh Puranik const boost::system::error_code ec) { 2218af5d6058SSantosh Puranik if (ec) 2219af5d6058SSantosh Puranik { 2220af5d6058SSantosh Puranik BMCWEB_LOG_DEBUG << "Failed to set elapsed time. " 2221af5d6058SSantosh Puranik "DBUS response error " 2222af5d6058SSantosh Puranik << ec; 2223af5d6058SSantosh Puranik messages::internalError(aResp->res); 2224af5d6058SSantosh Puranik return; 2225af5d6058SSantosh Puranik } 2226af5d6058SSantosh Puranik aResp->res.jsonValue["DateTime"] = datetime; 2227af5d6058SSantosh Puranik }, 2228af5d6058SSantosh Puranik "xyz.openbmc_project.Time.Manager", 2229af5d6058SSantosh Puranik "/xyz/openbmc_project/time/bmc", 2230af5d6058SSantosh Puranik "org.freedesktop.DBus.Properties", "Set", 2231af5d6058SSantosh Puranik "xyz.openbmc_project.Time.EpochTime", "Elapsed", 2232af5d6058SSantosh Puranik std::variant<uint64_t>(durMicroSecs)); 2233af5d6058SSantosh Puranik } 2234af5d6058SSantosh Puranik else 2235af5d6058SSantosh Puranik { 2236af5d6058SSantosh Puranik messages::propertyValueFormatError(aResp->res, datetime, 2237af5d6058SSantosh Puranik "DateTime"); 2238af5d6058SSantosh Puranik return; 2239af5d6058SSantosh Puranik } 224083ff9ab6SJames Feist } 22419c310685SBorawski.Lukasz 22420f74e643SEd Tanous std::string uuid; 22439c310685SBorawski.Lukasz }; 22449c310685SBorawski.Lukasz 22451abe55efSEd Tanous class ManagerCollection : public Node 22461abe55efSEd Tanous { 22479c310685SBorawski.Lukasz public: 224852cc112dSEd Tanous ManagerCollection(App& app) : Node(app, "/redfish/v1/Managers/") 22491abe55efSEd Tanous { 2250a434f2bdSEd Tanous entityPrivileges = { 2251a434f2bdSEd Tanous {boost::beast::http::verb::get, {{"Login"}}}, 2252e0d918bcSEd Tanous {boost::beast::http::verb::head, {{"Login"}}}, 2253e0d918bcSEd Tanous {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, 2254e0d918bcSEd Tanous {boost::beast::http::verb::put, {{"ConfigureManager"}}}, 2255e0d918bcSEd Tanous {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, 2256e0d918bcSEd Tanous {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; 22579c310685SBorawski.Lukasz } 22589c310685SBorawski.Lukasz 22599c310685SBorawski.Lukasz private: 2260cb13a392SEd Tanous void doGet(crow::Response& res, const crow::Request&, 2261cb13a392SEd Tanous const std::vector<std::string>&) override 22621abe55efSEd Tanous { 226383ff9ab6SJames Feist // Collections don't include the static data added by SubRoute 226483ff9ab6SJames Feist // because it has a duplicate entry for members 226555c7b7a2SEd Tanous res.jsonValue["@odata.id"] = "/redfish/v1/Managers"; 226655c7b7a2SEd Tanous res.jsonValue["@odata.type"] = "#ManagerCollection.ManagerCollection"; 226755c7b7a2SEd Tanous res.jsonValue["Name"] = "Manager Collection"; 226855c7b7a2SEd Tanous res.jsonValue["Members@odata.count"] = 1; 226955c7b7a2SEd Tanous res.jsonValue["Members"] = { 22705b4aa86bSJames Feist {{"@odata.id", "/redfish/v1/Managers/bmc"}}}; 22719c310685SBorawski.Lukasz res.end(); 22729c310685SBorawski.Lukasz } 22739c310685SBorawski.Lukasz }; 22749c310685SBorawski.Lukasz } // namespace redfish 2275