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> 2573df0db0SJames Feist #include <memory> 26af5d6058SSantosh Puranik #include <sstream> 27e90c5052SAndrew Geissler #include <utils/fw_utils.hpp> 287bffdb7eSBernard Wong #include <utils/systemd_utils.hpp> 29abf2add6SEd Tanous #include <variant> 305b4aa86bSJames Feist 311abe55efSEd Tanous namespace redfish 321abe55efSEd Tanous { 33ed5befbdSJennifer Lee 34ed5befbdSJennifer Lee /** 352a5c4407SGunnar Mills * Function reboots the BMC. 362a5c4407SGunnar Mills * 372a5c4407SGunnar Mills * @param[in] asyncResp - Shared pointer for completing asynchronous calls 38ed5befbdSJennifer Lee */ 392a5c4407SGunnar Mills void doBMCGracefulRestart(std::shared_ptr<AsyncResp> asyncResp) 40ed5befbdSJennifer Lee { 41ed5befbdSJennifer Lee const char* processName = "xyz.openbmc_project.State.BMC"; 42ed5befbdSJennifer Lee const char* objectPath = "/xyz/openbmc_project/state/bmc0"; 43ed5befbdSJennifer Lee const char* interfaceName = "xyz.openbmc_project.State.BMC"; 44ed5befbdSJennifer Lee const std::string& propertyValue = 45ed5befbdSJennifer Lee "xyz.openbmc_project.State.BMC.Transition.Reboot"; 46ed5befbdSJennifer Lee const char* destProperty = "RequestedBMCTransition"; 47ed5befbdSJennifer Lee 48ed5befbdSJennifer Lee // Create the D-Bus variant for D-Bus call. 49ed5befbdSJennifer Lee VariantType dbusPropertyValue(propertyValue); 50ed5befbdSJennifer Lee 51ed5befbdSJennifer Lee crow::connections::systemBus->async_method_call( 52ed5befbdSJennifer Lee [asyncResp](const boost::system::error_code ec) { 53ed5befbdSJennifer Lee // Use "Set" method to set the property value. 54ed5befbdSJennifer Lee if (ec) 55ed5befbdSJennifer Lee { 562a5c4407SGunnar Mills BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec; 57ed5befbdSJennifer Lee messages::internalError(asyncResp->res); 58ed5befbdSJennifer Lee return; 59ed5befbdSJennifer Lee } 60ed5befbdSJennifer Lee 61ed5befbdSJennifer Lee messages::success(asyncResp->res); 62ed5befbdSJennifer Lee }, 63ed5befbdSJennifer Lee processName, objectPath, "org.freedesktop.DBus.Properties", "Set", 64ed5befbdSJennifer Lee interfaceName, destProperty, dbusPropertyValue); 65ed5befbdSJennifer Lee } 662a5c4407SGunnar Mills 672a5c4407SGunnar Mills /** 682a5c4407SGunnar Mills * ManagerResetAction class supports the POST method for the Reset (reboot) 692a5c4407SGunnar Mills * action. 702a5c4407SGunnar Mills */ 712a5c4407SGunnar Mills class ManagerResetAction : public Node 722a5c4407SGunnar Mills { 732a5c4407SGunnar Mills public: 742a5c4407SGunnar Mills ManagerResetAction(CrowApp& app) : 752a5c4407SGunnar Mills Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/") 762a5c4407SGunnar Mills { 772a5c4407SGunnar Mills entityPrivileges = { 782a5c4407SGunnar Mills {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; 792a5c4407SGunnar Mills } 802a5c4407SGunnar Mills 812a5c4407SGunnar Mills private: 822a5c4407SGunnar Mills /** 832a5c4407SGunnar Mills * Function handles POST method request. 842a5c4407SGunnar Mills * Analyzes POST body before sending Reset (Reboot) request data to D-Bus. 852a5c4407SGunnar Mills * OpenBMC only supports ResetType "GracefulRestart". 862a5c4407SGunnar Mills */ 872a5c4407SGunnar Mills void doPost(crow::Response& res, const crow::Request& req, 882a5c4407SGunnar Mills const std::vector<std::string>& params) override 892a5c4407SGunnar Mills { 902a5c4407SGunnar Mills BMCWEB_LOG_DEBUG << "Post Manager Reset."; 912a5c4407SGunnar Mills 922a5c4407SGunnar Mills std::string resetType; 932a5c4407SGunnar Mills auto asyncResp = std::make_shared<AsyncResp>(res); 942a5c4407SGunnar Mills 952a5c4407SGunnar Mills if (!json_util::readJson(req, asyncResp->res, "ResetType", resetType)) 962a5c4407SGunnar Mills { 972a5c4407SGunnar Mills return; 982a5c4407SGunnar Mills } 992a5c4407SGunnar Mills 1002a5c4407SGunnar Mills if (resetType != "GracefulRestart") 1012a5c4407SGunnar Mills { 1022a5c4407SGunnar Mills BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: " 1032a5c4407SGunnar Mills << resetType; 1042a5c4407SGunnar Mills messages::actionParameterNotSupported(asyncResp->res, resetType, 1052a5c4407SGunnar Mills "ResetType"); 1062a5c4407SGunnar Mills 1072a5c4407SGunnar Mills return; 1082a5c4407SGunnar Mills } 1092a5c4407SGunnar Mills doBMCGracefulRestart(asyncResp); 1102a5c4407SGunnar Mills } 111ed5befbdSJennifer Lee }; 112ed5befbdSJennifer Lee 113*3e40fc74SGunnar Mills /** 114*3e40fc74SGunnar Mills * ManagerResetToDefaultsAction class supports POST method for factory reset 115*3e40fc74SGunnar Mills * action. 116*3e40fc74SGunnar Mills */ 117*3e40fc74SGunnar Mills class ManagerResetToDefaultsAction : public Node 118*3e40fc74SGunnar Mills { 119*3e40fc74SGunnar Mills public: 120*3e40fc74SGunnar Mills ManagerResetToDefaultsAction(CrowApp& app) : 121*3e40fc74SGunnar Mills Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/") 122*3e40fc74SGunnar Mills { 123*3e40fc74SGunnar Mills entityPrivileges = { 124*3e40fc74SGunnar Mills {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; 125*3e40fc74SGunnar Mills } 126*3e40fc74SGunnar Mills 127*3e40fc74SGunnar Mills private: 128*3e40fc74SGunnar Mills /** 129*3e40fc74SGunnar Mills * Function handles ResetToDefaults POST method request. 130*3e40fc74SGunnar Mills * 131*3e40fc74SGunnar Mills * Analyzes POST body message and factory resets BMC by calling 132*3e40fc74SGunnar Mills * BMC code updater factory reset followed by a BMC reboot. 133*3e40fc74SGunnar Mills * 134*3e40fc74SGunnar Mills * BMC code updater factory reset wipes the whole BMC read-write 135*3e40fc74SGunnar Mills * filesystem which includes things like the network settings. 136*3e40fc74SGunnar Mills * 137*3e40fc74SGunnar Mills * OpenBMC only supports ResetToDefaultsType "ResetAll". 138*3e40fc74SGunnar Mills */ 139*3e40fc74SGunnar Mills void doPost(crow::Response& res, const crow::Request& req, 140*3e40fc74SGunnar Mills const std::vector<std::string>& params) override 141*3e40fc74SGunnar Mills { 142*3e40fc74SGunnar Mills BMCWEB_LOG_DEBUG << "Post ResetToDefaults."; 143*3e40fc74SGunnar Mills 144*3e40fc74SGunnar Mills std::string resetType; 145*3e40fc74SGunnar Mills auto asyncResp = std::make_shared<AsyncResp>(res); 146*3e40fc74SGunnar Mills 147*3e40fc74SGunnar Mills if (!json_util::readJson(req, asyncResp->res, "ResetToDefaultsType", 148*3e40fc74SGunnar Mills resetType)) 149*3e40fc74SGunnar Mills { 150*3e40fc74SGunnar Mills BMCWEB_LOG_DEBUG << "Missing property ResetToDefaultsType."; 151*3e40fc74SGunnar Mills 152*3e40fc74SGunnar Mills messages::actionParameterMissing(asyncResp->res, "ResetToDefaults", 153*3e40fc74SGunnar Mills "ResetToDefaultsType"); 154*3e40fc74SGunnar Mills return; 155*3e40fc74SGunnar Mills } 156*3e40fc74SGunnar Mills 157*3e40fc74SGunnar Mills if (resetType != "ResetAll") 158*3e40fc74SGunnar Mills { 159*3e40fc74SGunnar Mills BMCWEB_LOG_DEBUG << "Invalid property value for " 160*3e40fc74SGunnar Mills "ResetToDefaultsType: " 161*3e40fc74SGunnar Mills << resetType; 162*3e40fc74SGunnar Mills messages::actionParameterNotSupported(asyncResp->res, resetType, 163*3e40fc74SGunnar Mills "ResetToDefaultsType"); 164*3e40fc74SGunnar Mills return; 165*3e40fc74SGunnar Mills } 166*3e40fc74SGunnar Mills 167*3e40fc74SGunnar Mills crow::connections::systemBus->async_method_call( 168*3e40fc74SGunnar Mills [asyncResp](const boost::system::error_code ec) { 169*3e40fc74SGunnar Mills if (ec) 170*3e40fc74SGunnar Mills { 171*3e40fc74SGunnar Mills BMCWEB_LOG_DEBUG << "Failed to ResetToDefaults: " << ec; 172*3e40fc74SGunnar Mills messages::internalError(asyncResp->res); 173*3e40fc74SGunnar Mills return; 174*3e40fc74SGunnar Mills } 175*3e40fc74SGunnar Mills // Factory Reset doesn't actually happen until a reboot 176*3e40fc74SGunnar Mills // Can't erase what the BMC is running on 177*3e40fc74SGunnar Mills doBMCGracefulRestart(asyncResp); 178*3e40fc74SGunnar Mills }, 179*3e40fc74SGunnar Mills "xyz.openbmc_project.Software.BMC.Updater", 180*3e40fc74SGunnar Mills "/xyz/openbmc_project/software", 181*3e40fc74SGunnar Mills "xyz.openbmc_project.Common.FactoryReset", "Reset"); 182*3e40fc74SGunnar Mills } 183*3e40fc74SGunnar Mills }; 184*3e40fc74SGunnar Mills 1855b4aa86bSJames Feist static constexpr const char* objectManagerIface = 1865b4aa86bSJames Feist "org.freedesktop.DBus.ObjectManager"; 1875b4aa86bSJames Feist static constexpr const char* pidConfigurationIface = 1885b4aa86bSJames Feist "xyz.openbmc_project.Configuration.Pid"; 1895b4aa86bSJames Feist static constexpr const char* pidZoneConfigurationIface = 1905b4aa86bSJames Feist "xyz.openbmc_project.Configuration.Pid.Zone"; 191b7a08d04SJames Feist static constexpr const char* stepwiseConfigurationIface = 192b7a08d04SJames Feist "xyz.openbmc_project.Configuration.Stepwise"; 19373df0db0SJames Feist static constexpr const char* thermalModeIface = 19473df0db0SJames Feist "xyz.openbmc_project.Control.ThermalMode"; 1959c310685SBorawski.Lukasz 1965b4aa86bSJames Feist static void asyncPopulatePid(const std::string& connection, 1975b4aa86bSJames Feist const std::string& path, 19873df0db0SJames Feist const std::string& currentProfile, 19973df0db0SJames Feist const std::vector<std::string>& supportedProfiles, 2005b4aa86bSJames Feist std::shared_ptr<AsyncResp> asyncResp) 2015b4aa86bSJames Feist { 2025b4aa86bSJames Feist 2035b4aa86bSJames Feist crow::connections::systemBus->async_method_call( 20473df0db0SJames Feist [asyncResp, currentProfile, supportedProfiles]( 20573df0db0SJames Feist const boost::system::error_code ec, 2065b4aa86bSJames Feist const dbus::utility::ManagedObjectType& managedObj) { 2075b4aa86bSJames Feist if (ec) 2085b4aa86bSJames Feist { 2095b4aa86bSJames Feist BMCWEB_LOG_ERROR << ec; 2105b4aa86bSJames Feist asyncResp->res.jsonValue.clear(); 211f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2125b4aa86bSJames Feist return; 2135b4aa86bSJames Feist } 2145b4aa86bSJames Feist nlohmann::json& configRoot = 2155b4aa86bSJames Feist asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"]; 2165b4aa86bSJames Feist nlohmann::json& fans = configRoot["FanControllers"]; 2175b4aa86bSJames Feist fans["@odata.type"] = "#OemManager.FanControllers"; 2185b4aa86bSJames Feist fans["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/" 2195b4aa86bSJames Feist "Fan/FanControllers"; 2205b4aa86bSJames Feist 2215b4aa86bSJames Feist nlohmann::json& pids = configRoot["PidControllers"]; 2225b4aa86bSJames Feist pids["@odata.type"] = "#OemManager.PidControllers"; 2235b4aa86bSJames Feist pids["@odata.id"] = 2245b4aa86bSJames Feist "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers"; 2255b4aa86bSJames Feist 226b7a08d04SJames Feist nlohmann::json& stepwise = configRoot["StepwiseControllers"]; 227b7a08d04SJames Feist stepwise["@odata.type"] = "#OemManager.StepwiseControllers"; 228b7a08d04SJames Feist stepwise["@odata.id"] = 229b7a08d04SJames Feist "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers"; 230b7a08d04SJames Feist 2315b4aa86bSJames Feist nlohmann::json& zones = configRoot["FanZones"]; 2325b4aa86bSJames Feist zones["@odata.id"] = 2335b4aa86bSJames Feist "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones"; 2345b4aa86bSJames Feist zones["@odata.type"] = "#OemManager.FanZones"; 2355b4aa86bSJames Feist configRoot["@odata.id"] = 2365b4aa86bSJames Feist "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan"; 2375b4aa86bSJames Feist configRoot["@odata.type"] = "#OemManager.Fan"; 23873df0db0SJames Feist configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles; 23973df0db0SJames Feist 24073df0db0SJames Feist if (!currentProfile.empty()) 24173df0db0SJames Feist { 24273df0db0SJames Feist configRoot["Profile"] = currentProfile; 24373df0db0SJames Feist } 24473df0db0SJames Feist BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !"; 2455b4aa86bSJames Feist 2465b4aa86bSJames Feist for (const auto& pathPair : managedObj) 2475b4aa86bSJames Feist { 2485b4aa86bSJames Feist for (const auto& intfPair : pathPair.second) 2495b4aa86bSJames Feist { 2505b4aa86bSJames Feist if (intfPair.first != pidConfigurationIface && 251b7a08d04SJames Feist intfPair.first != pidZoneConfigurationIface && 252b7a08d04SJames Feist intfPair.first != stepwiseConfigurationIface) 2535b4aa86bSJames Feist { 2545b4aa86bSJames Feist continue; 2555b4aa86bSJames Feist } 2565b4aa86bSJames Feist auto findName = intfPair.second.find("Name"); 2575b4aa86bSJames Feist if (findName == intfPair.second.end()) 2585b4aa86bSJames Feist { 2595b4aa86bSJames Feist BMCWEB_LOG_ERROR << "Pid Field missing Name"; 260a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 2615b4aa86bSJames Feist return; 2625b4aa86bSJames Feist } 26373df0db0SJames Feist 2645b4aa86bSJames Feist const std::string* namePtr = 265abf2add6SEd Tanous std::get_if<std::string>(&findName->second); 2665b4aa86bSJames Feist if (namePtr == nullptr) 2675b4aa86bSJames Feist { 2685b4aa86bSJames Feist BMCWEB_LOG_ERROR << "Pid Name Field illegal"; 269b7a08d04SJames Feist messages::internalError(asyncResp->res); 2705b4aa86bSJames Feist return; 2715b4aa86bSJames Feist } 2725b4aa86bSJames Feist std::string name = *namePtr; 2735b4aa86bSJames Feist dbus::utility::escapePathForDbus(name); 27473df0db0SJames Feist 27573df0db0SJames Feist auto findProfiles = intfPair.second.find("Profiles"); 27673df0db0SJames Feist if (findProfiles != intfPair.second.end()) 27773df0db0SJames Feist { 27873df0db0SJames Feist const std::vector<std::string>* profiles = 27973df0db0SJames Feist std::get_if<std::vector<std::string>>( 28073df0db0SJames Feist &findProfiles->second); 28173df0db0SJames Feist if (profiles == nullptr) 28273df0db0SJames Feist { 28373df0db0SJames Feist BMCWEB_LOG_ERROR << "Pid Profiles Field illegal"; 28473df0db0SJames Feist messages::internalError(asyncResp->res); 28573df0db0SJames Feist return; 28673df0db0SJames Feist } 28773df0db0SJames Feist if (std::find(profiles->begin(), profiles->end(), 28873df0db0SJames Feist currentProfile) == profiles->end()) 28973df0db0SJames Feist { 29073df0db0SJames Feist BMCWEB_LOG_INFO 29173df0db0SJames Feist << name << " not supported in current profile"; 29273df0db0SJames Feist continue; 29373df0db0SJames Feist } 29473df0db0SJames Feist } 295b7a08d04SJames Feist nlohmann::json* config = nullptr; 296c33a90ecSJames Feist 297c33a90ecSJames Feist const std::string* classPtr = nullptr; 298c33a90ecSJames Feist auto findClass = intfPair.second.find("Class"); 299c33a90ecSJames Feist if (findClass != intfPair.second.end()) 300c33a90ecSJames Feist { 301c33a90ecSJames Feist classPtr = std::get_if<std::string>(&findClass->second); 302c33a90ecSJames Feist } 303c33a90ecSJames Feist 3045b4aa86bSJames Feist if (intfPair.first == pidZoneConfigurationIface) 3055b4aa86bSJames Feist { 3065b4aa86bSJames Feist std::string chassis; 3075b4aa86bSJames Feist if (!dbus::utility::getNthStringFromPath( 3085b4aa86bSJames Feist pathPair.first.str, 5, chassis)) 3095b4aa86bSJames Feist { 3105b4aa86bSJames Feist chassis = "#IllegalValue"; 3115b4aa86bSJames Feist } 3125b4aa86bSJames Feist nlohmann::json& zone = zones[name]; 3135b4aa86bSJames Feist zone["Chassis"] = { 3145b4aa86bSJames Feist {"@odata.id", "/redfish/v1/Chassis/" + chassis}}; 3155b4aa86bSJames Feist zone["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/" 3165b4aa86bSJames Feist "OpenBmc/Fan/FanZones/" + 3175b4aa86bSJames Feist name; 3185b4aa86bSJames Feist zone["@odata.type"] = "#OemManager.FanZone"; 319b7a08d04SJames Feist config = &zone; 3205b4aa86bSJames Feist } 3215b4aa86bSJames Feist 322b7a08d04SJames Feist else if (intfPair.first == stepwiseConfigurationIface) 3235b4aa86bSJames Feist { 324c33a90ecSJames Feist if (classPtr == nullptr) 325c33a90ecSJames Feist { 326c33a90ecSJames Feist BMCWEB_LOG_ERROR << "Pid Class Field illegal"; 327c33a90ecSJames Feist messages::internalError(asyncResp->res); 328c33a90ecSJames Feist return; 329c33a90ecSJames Feist } 330c33a90ecSJames Feist 331b7a08d04SJames Feist nlohmann::json& controller = stepwise[name]; 332b7a08d04SJames Feist config = &controller; 3335b4aa86bSJames Feist 334b7a08d04SJames Feist controller["@odata.id"] = 335b7a08d04SJames Feist "/redfish/v1/Managers/bmc#/Oem/" 336b7a08d04SJames Feist "OpenBmc/Fan/StepwiseControllers/" + 337271584abSEd Tanous name; 338b7a08d04SJames Feist controller["@odata.type"] = 339b7a08d04SJames Feist "#OemManager.StepwiseController"; 340b7a08d04SJames Feist 341c33a90ecSJames Feist controller["Direction"] = *classPtr; 3425b4aa86bSJames Feist } 3435b4aa86bSJames Feist 3445b4aa86bSJames Feist // pid and fans are off the same configuration 345b7a08d04SJames Feist else if (intfPair.first == pidConfigurationIface) 3465b4aa86bSJames Feist { 347c33a90ecSJames Feist 3485b4aa86bSJames Feist if (classPtr == nullptr) 3495b4aa86bSJames Feist { 3505b4aa86bSJames Feist BMCWEB_LOG_ERROR << "Pid Class Field illegal"; 351a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 3525b4aa86bSJames Feist return; 3535b4aa86bSJames Feist } 3545b4aa86bSJames Feist bool isFan = *classPtr == "fan"; 3555b4aa86bSJames Feist nlohmann::json& element = 3565b4aa86bSJames Feist isFan ? fans[name] : pids[name]; 357b7a08d04SJames Feist config = &element; 3585b4aa86bSJames Feist if (isFan) 3595b4aa86bSJames Feist { 3605b4aa86bSJames Feist element["@odata.id"] = 3615b4aa86bSJames Feist "/redfish/v1/Managers/bmc#/Oem/" 3625b4aa86bSJames Feist "OpenBmc/Fan/FanControllers/" + 363271584abSEd Tanous name; 3645b4aa86bSJames Feist element["@odata.type"] = 3655b4aa86bSJames Feist "#OemManager.FanController"; 3665b4aa86bSJames Feist } 3675b4aa86bSJames Feist else 3685b4aa86bSJames Feist { 3695b4aa86bSJames Feist element["@odata.id"] = 3705b4aa86bSJames Feist "/redfish/v1/Managers/bmc#/Oem/" 3715b4aa86bSJames Feist "OpenBmc/Fan/PidControllers/" + 372271584abSEd Tanous name; 3735b4aa86bSJames Feist element["@odata.type"] = 3745b4aa86bSJames Feist "#OemManager.PidController"; 3755b4aa86bSJames Feist } 376b7a08d04SJames Feist } 377b7a08d04SJames Feist else 378b7a08d04SJames Feist { 379b7a08d04SJames Feist BMCWEB_LOG_ERROR << "Unexpected configuration"; 380b7a08d04SJames Feist messages::internalError(asyncResp->res); 381b7a08d04SJames Feist return; 382b7a08d04SJames Feist } 383b7a08d04SJames Feist 384b7a08d04SJames Feist // used for making maps out of 2 vectors 385b7a08d04SJames Feist const std::vector<double>* keys = nullptr; 386b7a08d04SJames Feist const std::vector<double>* values = nullptr; 387b7a08d04SJames Feist 388b7a08d04SJames Feist for (const auto& propertyPair : intfPair.second) 389b7a08d04SJames Feist { 390b7a08d04SJames Feist if (propertyPair.first == "Type" || 391b7a08d04SJames Feist propertyPair.first == "Class" || 392b7a08d04SJames Feist propertyPair.first == "Name") 393b7a08d04SJames Feist { 394b7a08d04SJames Feist continue; 395b7a08d04SJames Feist } 396b7a08d04SJames Feist 397b7a08d04SJames Feist // zones 398b7a08d04SJames Feist if (intfPair.first == pidZoneConfigurationIface) 399b7a08d04SJames Feist { 400b7a08d04SJames Feist const double* ptr = 401abf2add6SEd Tanous std::get_if<double>(&propertyPair.second); 402b7a08d04SJames Feist if (ptr == nullptr) 403b7a08d04SJames Feist { 404b7a08d04SJames Feist BMCWEB_LOG_ERROR << "Field Illegal " 405b7a08d04SJames Feist << propertyPair.first; 406b7a08d04SJames Feist messages::internalError(asyncResp->res); 407b7a08d04SJames Feist return; 408b7a08d04SJames Feist } 409b7a08d04SJames Feist (*config)[propertyPair.first] = *ptr; 410b7a08d04SJames Feist } 411b7a08d04SJames Feist 412b7a08d04SJames Feist if (intfPair.first == stepwiseConfigurationIface) 413b7a08d04SJames Feist { 414b7a08d04SJames Feist if (propertyPair.first == "Reading" || 415b7a08d04SJames Feist propertyPair.first == "Output") 416b7a08d04SJames Feist { 417b7a08d04SJames Feist const std::vector<double>* ptr = 418abf2add6SEd Tanous std::get_if<std::vector<double>>( 419b7a08d04SJames Feist &propertyPair.second); 420b7a08d04SJames Feist 421b7a08d04SJames Feist if (ptr == nullptr) 422b7a08d04SJames Feist { 423b7a08d04SJames Feist BMCWEB_LOG_ERROR << "Field Illegal " 424b7a08d04SJames Feist << propertyPair.first; 425b7a08d04SJames Feist messages::internalError(asyncResp->res); 426b7a08d04SJames Feist return; 427b7a08d04SJames Feist } 428b7a08d04SJames Feist 429b7a08d04SJames Feist if (propertyPair.first == "Reading") 430b7a08d04SJames Feist { 431b7a08d04SJames Feist keys = ptr; 432b7a08d04SJames Feist } 433b7a08d04SJames Feist else 434b7a08d04SJames Feist { 435b7a08d04SJames Feist values = ptr; 436b7a08d04SJames Feist } 437b7a08d04SJames Feist if (keys && values) 438b7a08d04SJames Feist { 439b7a08d04SJames Feist if (keys->size() != values->size()) 440b7a08d04SJames Feist { 441b7a08d04SJames Feist BMCWEB_LOG_ERROR 442b7a08d04SJames Feist << "Reading and Output size don't " 443b7a08d04SJames Feist "match "; 444b7a08d04SJames Feist messages::internalError(asyncResp->res); 445b7a08d04SJames Feist return; 446b7a08d04SJames Feist } 447b7a08d04SJames Feist nlohmann::json& steps = (*config)["Steps"]; 448b7a08d04SJames Feist steps = nlohmann::json::array(); 449b7a08d04SJames Feist for (size_t ii = 0; ii < keys->size(); ii++) 450b7a08d04SJames Feist { 451b7a08d04SJames Feist steps.push_back( 452b7a08d04SJames Feist {{"Target", (*keys)[ii]}, 453b7a08d04SJames Feist {"Output", (*values)[ii]}}); 454b7a08d04SJames Feist } 455b7a08d04SJames Feist } 456b7a08d04SJames Feist } 457b7a08d04SJames Feist if (propertyPair.first == "NegativeHysteresis" || 458b7a08d04SJames Feist propertyPair.first == "PositiveHysteresis") 459b7a08d04SJames Feist { 460b7a08d04SJames Feist const double* ptr = 461abf2add6SEd Tanous std::get_if<double>(&propertyPair.second); 462b7a08d04SJames Feist if (ptr == nullptr) 463b7a08d04SJames Feist { 464b7a08d04SJames Feist BMCWEB_LOG_ERROR << "Field Illegal " 465b7a08d04SJames Feist << propertyPair.first; 466b7a08d04SJames Feist messages::internalError(asyncResp->res); 467b7a08d04SJames Feist return; 468b7a08d04SJames Feist } 469b7a08d04SJames Feist (*config)[propertyPair.first] = *ptr; 470b7a08d04SJames Feist } 471b7a08d04SJames Feist } 472b7a08d04SJames Feist 473b7a08d04SJames Feist // pid and fans are off the same configuration 474b7a08d04SJames Feist if (intfPair.first == pidConfigurationIface || 475b7a08d04SJames Feist intfPair.first == stepwiseConfigurationIface) 476b7a08d04SJames Feist { 4775b4aa86bSJames Feist 4785b4aa86bSJames Feist if (propertyPair.first == "Zones") 4795b4aa86bSJames Feist { 4805b4aa86bSJames Feist const std::vector<std::string>* inputs = 481abf2add6SEd Tanous std::get_if<std::vector<std::string>>( 4821b6b96c5SEd Tanous &propertyPair.second); 4835b4aa86bSJames Feist 4845b4aa86bSJames Feist if (inputs == nullptr) 4855b4aa86bSJames Feist { 4865b4aa86bSJames Feist BMCWEB_LOG_ERROR 4875b4aa86bSJames Feist << "Zones Pid Field Illegal"; 488a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 4895b4aa86bSJames Feist return; 4905b4aa86bSJames Feist } 491b7a08d04SJames Feist auto& data = (*config)[propertyPair.first]; 4925b4aa86bSJames Feist data = nlohmann::json::array(); 4935b4aa86bSJames Feist for (std::string itemCopy : *inputs) 4945b4aa86bSJames Feist { 4955b4aa86bSJames Feist dbus::utility::escapePathForDbus(itemCopy); 4965b4aa86bSJames Feist data.push_back( 4975b4aa86bSJames Feist {{"@odata.id", 4985b4aa86bSJames Feist "/redfish/v1/Managers/bmc#/Oem/" 4995b4aa86bSJames Feist "OpenBmc/Fan/FanZones/" + 5005b4aa86bSJames Feist itemCopy}}); 5015b4aa86bSJames Feist } 5025b4aa86bSJames Feist } 5035b4aa86bSJames Feist // todo(james): may never happen, but this 5045b4aa86bSJames Feist // assumes configuration data referenced in the 5055b4aa86bSJames Feist // PID config is provided by the same daemon, we 5065b4aa86bSJames Feist // could add another loop to cover all cases, 5075b4aa86bSJames Feist // but I'm okay kicking this can down the road a 5085b4aa86bSJames Feist // bit 5095b4aa86bSJames Feist 5105b4aa86bSJames Feist else if (propertyPair.first == "Inputs" || 5115b4aa86bSJames Feist propertyPair.first == "Outputs") 5125b4aa86bSJames Feist { 513b7a08d04SJames Feist auto& data = (*config)[propertyPair.first]; 5145b4aa86bSJames Feist const std::vector<std::string>* inputs = 515abf2add6SEd Tanous std::get_if<std::vector<std::string>>( 5161b6b96c5SEd Tanous &propertyPair.second); 5175b4aa86bSJames Feist 5185b4aa86bSJames Feist if (inputs == nullptr) 5195b4aa86bSJames Feist { 5205b4aa86bSJames Feist BMCWEB_LOG_ERROR << "Field Illegal " 5215b4aa86bSJames Feist << propertyPair.first; 522f12894f8SJason M. Bills messages::internalError(asyncResp->res); 5235b4aa86bSJames Feist return; 5245b4aa86bSJames Feist } 5255b4aa86bSJames Feist data = *inputs; 526b943aaefSJames Feist } 527b943aaefSJames Feist else if (propertyPair.first == "SetPointOffset") 528b943aaefSJames Feist { 529b943aaefSJames Feist const std::string* ptr = 530b943aaefSJames Feist std::get_if<std::string>( 531b943aaefSJames Feist &propertyPair.second); 532b943aaefSJames Feist 533b943aaefSJames Feist if (ptr == nullptr) 534b943aaefSJames Feist { 535b943aaefSJames Feist BMCWEB_LOG_ERROR << "Field Illegal " 536b943aaefSJames Feist << propertyPair.first; 537b943aaefSJames Feist messages::internalError(asyncResp->res); 538b943aaefSJames Feist return; 539b943aaefSJames Feist } 540b943aaefSJames Feist // translate from dbus to redfish 541b943aaefSJames Feist if (*ptr == "WarningHigh") 542b943aaefSJames Feist { 543b943aaefSJames Feist (*config)["SetPointOffset"] = 544b943aaefSJames Feist "UpperThresholdNonCritical"; 545b943aaefSJames Feist } 546b943aaefSJames Feist else if (*ptr == "WarningLow") 547b943aaefSJames Feist { 548b943aaefSJames Feist (*config)["SetPointOffset"] = 549b943aaefSJames Feist "LowerThresholdNonCritical"; 550b943aaefSJames Feist } 551b943aaefSJames Feist else if (*ptr == "CriticalHigh") 552b943aaefSJames Feist { 553b943aaefSJames Feist (*config)["SetPointOffset"] = 554b943aaefSJames Feist "UpperThresholdCritical"; 555b943aaefSJames Feist } 556b943aaefSJames Feist else if (*ptr == "CriticalLow") 557b943aaefSJames Feist { 558b943aaefSJames Feist (*config)["SetPointOffset"] = 559b943aaefSJames Feist "LowerThresholdCritical"; 560b943aaefSJames Feist } 561b943aaefSJames Feist else 562b943aaefSJames Feist { 563b943aaefSJames Feist BMCWEB_LOG_ERROR << "Value Illegal " 564b943aaefSJames Feist << *ptr; 565b943aaefSJames Feist messages::internalError(asyncResp->res); 566b943aaefSJames Feist return; 567b943aaefSJames Feist } 568b943aaefSJames Feist } 569b943aaefSJames Feist // doubles 5705b4aa86bSJames Feist else if (propertyPair.first == 5715b4aa86bSJames Feist "FFGainCoefficient" || 5725b4aa86bSJames Feist propertyPair.first == "FFOffCoefficient" || 5735b4aa86bSJames Feist propertyPair.first == "ICoefficient" || 5745b4aa86bSJames Feist propertyPair.first == "ILimitMax" || 5755b4aa86bSJames Feist propertyPair.first == "ILimitMin" || 576aad1a257SJames Feist propertyPair.first == 577aad1a257SJames Feist "PositiveHysteresis" || 578aad1a257SJames Feist propertyPair.first == 579aad1a257SJames Feist "NegativeHysteresis" || 5805b4aa86bSJames Feist propertyPair.first == "OutLimitMax" || 5815b4aa86bSJames Feist propertyPair.first == "OutLimitMin" || 5825b4aa86bSJames Feist propertyPair.first == "PCoefficient" || 5837625cb81SJames Feist propertyPair.first == "SetPoint" || 5845b4aa86bSJames Feist propertyPair.first == "SlewNeg" || 5855b4aa86bSJames Feist propertyPair.first == "SlewPos") 5865b4aa86bSJames Feist { 5875b4aa86bSJames Feist const double* ptr = 588abf2add6SEd Tanous std::get_if<double>(&propertyPair.second); 5895b4aa86bSJames Feist if (ptr == nullptr) 5905b4aa86bSJames Feist { 5915b4aa86bSJames Feist BMCWEB_LOG_ERROR << "Field Illegal " 5925b4aa86bSJames Feist << propertyPair.first; 593f12894f8SJason M. Bills messages::internalError(asyncResp->res); 5945b4aa86bSJames Feist return; 5955b4aa86bSJames Feist } 596b7a08d04SJames Feist (*config)[propertyPair.first] = *ptr; 5975b4aa86bSJames Feist } 5985b4aa86bSJames Feist } 5995b4aa86bSJames Feist } 6005b4aa86bSJames Feist } 6015b4aa86bSJames Feist } 6025b4aa86bSJames Feist }, 6035b4aa86bSJames Feist connection, path, objectManagerIface, "GetManagedObjects"); 6045b4aa86bSJames Feist } 605ca537928SJennifer Lee 60683ff9ab6SJames Feist enum class CreatePIDRet 60783ff9ab6SJames Feist { 60883ff9ab6SJames Feist fail, 60983ff9ab6SJames Feist del, 61083ff9ab6SJames Feist patch 61183ff9ab6SJames Feist }; 61283ff9ab6SJames Feist 6135f2caaefSJames Feist static bool getZonesFromJsonReq(const std::shared_ptr<AsyncResp>& response, 6145f2caaefSJames Feist std::vector<nlohmann::json>& config, 6155f2caaefSJames Feist std::vector<std::string>& zones) 6165f2caaefSJames Feist { 617b6baeaa4SJames Feist if (config.empty()) 618b6baeaa4SJames Feist { 619b6baeaa4SJames Feist BMCWEB_LOG_ERROR << "Empty Zones"; 620b6baeaa4SJames Feist messages::propertyValueFormatError(response->res, 621b6baeaa4SJames Feist nlohmann::json::array(), "Zones"); 622b6baeaa4SJames Feist return false; 623b6baeaa4SJames Feist } 6245f2caaefSJames Feist for (auto& odata : config) 6255f2caaefSJames Feist { 6265f2caaefSJames Feist std::string path; 6275f2caaefSJames Feist if (!redfish::json_util::readJson(odata, response->res, "@odata.id", 6285f2caaefSJames Feist path)) 6295f2caaefSJames Feist { 6305f2caaefSJames Feist return false; 6315f2caaefSJames Feist } 6325f2caaefSJames Feist std::string input; 63361adbda3SJames Feist 63461adbda3SJames Feist // 8 below comes from 63561adbda3SJames Feist // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left 63661adbda3SJames Feist // 0 1 2 3 4 5 6 7 8 63761adbda3SJames Feist if (!dbus::utility::getNthStringFromPath(path, 8, input)) 6385f2caaefSJames Feist { 6395f2caaefSJames Feist BMCWEB_LOG_ERROR << "Got invalid path " << path; 6405f2caaefSJames Feist BMCWEB_LOG_ERROR << "Illegal Type Zones"; 6415f2caaefSJames Feist messages::propertyValueFormatError(response->res, odata.dump(), 6425f2caaefSJames Feist "Zones"); 6435f2caaefSJames Feist return false; 6445f2caaefSJames Feist } 6455f2caaefSJames Feist boost::replace_all(input, "_", " "); 6465f2caaefSJames Feist zones.emplace_back(std::move(input)); 6475f2caaefSJames Feist } 6485f2caaefSJames Feist return true; 6495f2caaefSJames Feist } 6505f2caaefSJames Feist 65173df0db0SJames Feist static const dbus::utility::ManagedItem* 65273df0db0SJames Feist findChassis(const dbus::utility::ManagedObjectType& managedObj, 653b6baeaa4SJames Feist const std::string& value, std::string& chassis) 654b6baeaa4SJames Feist { 655b6baeaa4SJames Feist BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n"; 656b6baeaa4SJames Feist 657b6baeaa4SJames Feist std::string escaped = boost::replace_all_copy(value, " ", "_"); 658b6baeaa4SJames Feist escaped = "/" + escaped; 659b6baeaa4SJames Feist auto it = std::find_if( 660b6baeaa4SJames Feist managedObj.begin(), managedObj.end(), [&escaped](const auto& obj) { 661b6baeaa4SJames Feist if (boost::algorithm::ends_with(obj.first.str, escaped)) 662b6baeaa4SJames Feist { 663b6baeaa4SJames Feist BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n"; 664b6baeaa4SJames Feist return true; 665b6baeaa4SJames Feist } 666b6baeaa4SJames Feist return false; 667b6baeaa4SJames Feist }); 668b6baeaa4SJames Feist 669b6baeaa4SJames Feist if (it == managedObj.end()) 670b6baeaa4SJames Feist { 67173df0db0SJames Feist return nullptr; 672b6baeaa4SJames Feist } 673b6baeaa4SJames Feist // 5 comes from <chassis-name> being the 5th element 674b6baeaa4SJames Feist // /xyz/openbmc_project/inventory/system/chassis/<chassis-name> 67573df0db0SJames Feist if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis)) 67673df0db0SJames Feist { 67773df0db0SJames Feist return &(*it); 67873df0db0SJames Feist } 67973df0db0SJames Feist 68073df0db0SJames Feist return nullptr; 681b6baeaa4SJames Feist } 682b6baeaa4SJames Feist 68383ff9ab6SJames Feist static CreatePIDRet createPidInterface( 68483ff9ab6SJames Feist const std::shared_ptr<AsyncResp>& response, const std::string& type, 685b6baeaa4SJames Feist nlohmann::json::iterator it, const std::string& path, 68683ff9ab6SJames Feist const dbus::utility::ManagedObjectType& managedObj, bool createNewObject, 68783ff9ab6SJames Feist boost::container::flat_map<std::string, dbus::utility::DbusVariantType>& 68883ff9ab6SJames Feist output, 68973df0db0SJames Feist std::string& chassis, const std::string& profile) 69083ff9ab6SJames Feist { 69183ff9ab6SJames Feist 6925f2caaefSJames Feist // common deleter 693b6baeaa4SJames Feist if (it.value() == nullptr) 6945f2caaefSJames Feist { 6955f2caaefSJames Feist std::string iface; 6965f2caaefSJames Feist if (type == "PidControllers" || type == "FanControllers") 6975f2caaefSJames Feist { 6985f2caaefSJames Feist iface = pidConfigurationIface; 6995f2caaefSJames Feist } 7005f2caaefSJames Feist else if (type == "FanZones") 7015f2caaefSJames Feist { 7025f2caaefSJames Feist iface = pidZoneConfigurationIface; 7035f2caaefSJames Feist } 7045f2caaefSJames Feist else if (type == "StepwiseControllers") 7055f2caaefSJames Feist { 7065f2caaefSJames Feist iface = stepwiseConfigurationIface; 7075f2caaefSJames Feist } 7085f2caaefSJames Feist else 7095f2caaefSJames Feist { 7105f2caaefSJames Feist BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type " 7115f2caaefSJames Feist << type; 7125f2caaefSJames Feist messages::propertyUnknown(response->res, type); 7135f2caaefSJames Feist return CreatePIDRet::fail; 7145f2caaefSJames Feist } 7156ee7f774SJames Feist 7166ee7f774SJames Feist BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n"; 7175f2caaefSJames Feist // delete interface 7185f2caaefSJames Feist crow::connections::systemBus->async_method_call( 7195f2caaefSJames Feist [response, path](const boost::system::error_code ec) { 7205f2caaefSJames Feist if (ec) 7215f2caaefSJames Feist { 7225f2caaefSJames Feist BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec; 7235f2caaefSJames Feist messages::internalError(response->res); 724b6baeaa4SJames Feist return; 7255f2caaefSJames Feist } 726b6baeaa4SJames Feist messages::success(response->res); 7275f2caaefSJames Feist }, 7285f2caaefSJames Feist "xyz.openbmc_project.EntityManager", path, iface, "Delete"); 7295f2caaefSJames Feist return CreatePIDRet::del; 7305f2caaefSJames Feist } 7315f2caaefSJames Feist 73273df0db0SJames Feist const dbus::utility::ManagedItem* managedItem = nullptr; 733b6baeaa4SJames Feist if (!createNewObject) 734b6baeaa4SJames Feist { 735b6baeaa4SJames Feist // if we aren't creating a new object, we should be able to find it on 736b6baeaa4SJames Feist // d-bus 73773df0db0SJames Feist managedItem = findChassis(managedObj, it.key(), chassis); 73873df0db0SJames Feist if (managedItem == nullptr) 739b6baeaa4SJames Feist { 740b6baeaa4SJames Feist BMCWEB_LOG_ERROR << "Failed to get chassis from config patch"; 741b6baeaa4SJames Feist messages::invalidObject(response->res, it.key()); 742b6baeaa4SJames Feist return CreatePIDRet::fail; 743b6baeaa4SJames Feist } 744b6baeaa4SJames Feist } 745b6baeaa4SJames Feist 74673df0db0SJames Feist if (profile.size() && 74773df0db0SJames Feist (type == "PidControllers" || type == "FanControllers" || 74873df0db0SJames Feist type == "StepwiseControllers")) 74973df0db0SJames Feist { 75073df0db0SJames Feist if (managedItem == nullptr) 75173df0db0SJames Feist { 75273df0db0SJames Feist output["Profiles"] = std::vector<std::string>{profile}; 75373df0db0SJames Feist } 75473df0db0SJames Feist else 75573df0db0SJames Feist { 75673df0db0SJames Feist std::string interface; 75773df0db0SJames Feist if (type == "StepwiseControllers") 75873df0db0SJames Feist { 75973df0db0SJames Feist interface = stepwiseConfigurationIface; 76073df0db0SJames Feist } 76173df0db0SJames Feist else 76273df0db0SJames Feist { 76373df0db0SJames Feist interface = pidConfigurationIface; 76473df0db0SJames Feist } 76573df0db0SJames Feist auto findConfig = managedItem->second.find(interface); 76673df0db0SJames Feist if (findConfig == managedItem->second.end()) 76773df0db0SJames Feist { 76873df0db0SJames Feist BMCWEB_LOG_ERROR 76973df0db0SJames Feist << "Failed to find interface in managed object"; 77073df0db0SJames Feist messages::internalError(response->res); 77173df0db0SJames Feist return CreatePIDRet::fail; 77273df0db0SJames Feist } 77373df0db0SJames Feist auto findProfiles = findConfig->second.find("Profiles"); 77473df0db0SJames Feist if (findProfiles != findConfig->second.end()) 77573df0db0SJames Feist { 77673df0db0SJames Feist const std::vector<std::string>* curProfiles = 77773df0db0SJames Feist std::get_if<std::vector<std::string>>( 77873df0db0SJames Feist &(findProfiles->second)); 77973df0db0SJames Feist if (curProfiles == nullptr) 78073df0db0SJames Feist { 78173df0db0SJames Feist BMCWEB_LOG_ERROR << "Illegal profiles in managed object"; 78273df0db0SJames Feist messages::internalError(response->res); 78373df0db0SJames Feist return CreatePIDRet::fail; 78473df0db0SJames Feist } 78573df0db0SJames Feist if (std::find(curProfiles->begin(), curProfiles->end(), 78673df0db0SJames Feist profile) == curProfiles->end()) 78773df0db0SJames Feist { 78873df0db0SJames Feist std::vector<std::string> newProfiles = *curProfiles; 78973df0db0SJames Feist newProfiles.push_back(profile); 79073df0db0SJames Feist output["Profiles"] = newProfiles; 79173df0db0SJames Feist } 79273df0db0SJames Feist } 79373df0db0SJames Feist } 79473df0db0SJames Feist } 79573df0db0SJames Feist 79683ff9ab6SJames Feist if (type == "PidControllers" || type == "FanControllers") 79783ff9ab6SJames Feist { 79883ff9ab6SJames Feist if (createNewObject) 79983ff9ab6SJames Feist { 80083ff9ab6SJames Feist output["Class"] = type == "PidControllers" ? std::string("temp") 80183ff9ab6SJames Feist : std::string("fan"); 80283ff9ab6SJames Feist output["Type"] = std::string("Pid"); 80383ff9ab6SJames Feist } 8045f2caaefSJames Feist 8055f2caaefSJames Feist std::optional<std::vector<nlohmann::json>> zones; 8065f2caaefSJames Feist std::optional<std::vector<std::string>> inputs; 8075f2caaefSJames Feist std::optional<std::vector<std::string>> outputs; 8085f2caaefSJames Feist std::map<std::string, std::optional<double>> doubles; 809b943aaefSJames Feist std::optional<std::string> setpointOffset; 8105f2caaefSJames Feist if (!redfish::json_util::readJson( 811b6baeaa4SJames Feist it.value(), response->res, "Inputs", inputs, "Outputs", outputs, 8125f2caaefSJames Feist "Zones", zones, "FFGainCoefficient", 8135f2caaefSJames Feist doubles["FFGainCoefficient"], "FFOffCoefficient", 8145f2caaefSJames Feist doubles["FFOffCoefficient"], "ICoefficient", 8155f2caaefSJames Feist doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"], 8165f2caaefSJames Feist "ILimitMin", doubles["ILimitMin"], "OutLimitMax", 8175f2caaefSJames Feist doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"], 8185f2caaefSJames Feist "PCoefficient", doubles["PCoefficient"], "SetPoint", 819b943aaefSJames Feist doubles["SetPoint"], "SetPointOffset", setpointOffset, 820b943aaefSJames Feist "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"], 821b943aaefSJames Feist "PositiveHysteresis", doubles["PositiveHysteresis"], 822b943aaefSJames Feist "NegativeHysteresis", doubles["NegativeHysteresis"])) 82383ff9ab6SJames Feist { 8245f2caaefSJames Feist BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property " 825b6baeaa4SJames Feist << it.value().dump(); 8265f2caaefSJames Feist return CreatePIDRet::fail; 82783ff9ab6SJames Feist } 8285f2caaefSJames Feist if (zones) 8295f2caaefSJames Feist { 8305f2caaefSJames Feist std::vector<std::string> zonesStr; 8315f2caaefSJames Feist if (!getZonesFromJsonReq(response, *zones, zonesStr)) 8325f2caaefSJames Feist { 8335f2caaefSJames Feist BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones"; 8345f2caaefSJames Feist return CreatePIDRet::fail; 8355f2caaefSJames Feist } 836b6baeaa4SJames Feist if (chassis.empty() && 837b6baeaa4SJames Feist !findChassis(managedObj, zonesStr[0], chassis)) 838b6baeaa4SJames Feist { 839b6baeaa4SJames Feist BMCWEB_LOG_ERROR << "Failed to get chassis from config patch"; 840b6baeaa4SJames Feist messages::invalidObject(response->res, it.key()); 841b6baeaa4SJames Feist return CreatePIDRet::fail; 842b6baeaa4SJames Feist } 843b6baeaa4SJames Feist 8445f2caaefSJames Feist output["Zones"] = std::move(zonesStr); 8455f2caaefSJames Feist } 8465f2caaefSJames Feist if (inputs || outputs) 8475f2caaefSJames Feist { 8485f2caaefSJames Feist std::array<std::optional<std::vector<std::string>>*, 2> containers = 8495f2caaefSJames Feist {&inputs, &outputs}; 8505f2caaefSJames Feist size_t index = 0; 8515f2caaefSJames Feist for (const auto& containerPtr : containers) 8525f2caaefSJames Feist { 8535f2caaefSJames Feist std::optional<std::vector<std::string>>& container = 8545f2caaefSJames Feist *containerPtr; 8555f2caaefSJames Feist if (!container) 8565f2caaefSJames Feist { 8575f2caaefSJames Feist index++; 8585f2caaefSJames Feist continue; 85983ff9ab6SJames Feist } 86083ff9ab6SJames Feist 8615f2caaefSJames Feist for (std::string& value : *container) 86283ff9ab6SJames Feist { 8635f2caaefSJames Feist boost::replace_all(value, "_", " "); 86483ff9ab6SJames Feist } 8655f2caaefSJames Feist std::string key; 8665f2caaefSJames Feist if (index == 0) 8675f2caaefSJames Feist { 8685f2caaefSJames Feist key = "Inputs"; 8695f2caaefSJames Feist } 8705f2caaefSJames Feist else 8715f2caaefSJames Feist { 8725f2caaefSJames Feist key = "Outputs"; 8735f2caaefSJames Feist } 8745f2caaefSJames Feist output[key] = *container; 8755f2caaefSJames Feist index++; 8765f2caaefSJames Feist } 87783ff9ab6SJames Feist } 87883ff9ab6SJames Feist 879b943aaefSJames Feist if (setpointOffset) 880b943aaefSJames Feist { 881b943aaefSJames Feist // translate between redfish and dbus names 882b943aaefSJames Feist if (*setpointOffset == "UpperThresholdNonCritical") 883b943aaefSJames Feist { 884b943aaefSJames Feist output["SetPointOffset"] = std::string("WarningLow"); 885b943aaefSJames Feist } 886b943aaefSJames Feist else if (*setpointOffset == "LowerThresholdNonCritical") 887b943aaefSJames Feist { 888b943aaefSJames Feist output["SetPointOffset"] = std::string("WarningHigh"); 889b943aaefSJames Feist } 890b943aaefSJames Feist else if (*setpointOffset == "LowerThresholdCritical") 891b943aaefSJames Feist { 892b943aaefSJames Feist output["SetPointOffset"] = std::string("CriticalLow"); 893b943aaefSJames Feist } 894b943aaefSJames Feist else if (*setpointOffset == "UpperThresholdCritical") 895b943aaefSJames Feist { 896b943aaefSJames Feist output["SetPointOffset"] = std::string("CriticalHigh"); 897b943aaefSJames Feist } 898b943aaefSJames Feist else 899b943aaefSJames Feist { 900b943aaefSJames Feist BMCWEB_LOG_ERROR << "Invalid setpointoffset " 901b943aaefSJames Feist << *setpointOffset; 902b943aaefSJames Feist messages::invalidObject(response->res, it.key()); 903b943aaefSJames Feist return CreatePIDRet::fail; 904b943aaefSJames Feist } 905b943aaefSJames Feist } 906b943aaefSJames Feist 90783ff9ab6SJames Feist // doubles 9085f2caaefSJames Feist for (const auto& pairs : doubles) 90983ff9ab6SJames Feist { 9105f2caaefSJames Feist if (!pairs.second) 91183ff9ab6SJames Feist { 9125f2caaefSJames Feist continue; 91383ff9ab6SJames Feist } 9145f2caaefSJames Feist BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second; 9155f2caaefSJames Feist output[pairs.first] = *(pairs.second); 9165f2caaefSJames Feist } 91783ff9ab6SJames Feist } 91883ff9ab6SJames Feist 91983ff9ab6SJames Feist else if (type == "FanZones") 92083ff9ab6SJames Feist { 92183ff9ab6SJames Feist output["Type"] = std::string("Pid.Zone"); 92283ff9ab6SJames Feist 9235f2caaefSJames Feist std::optional<nlohmann::json> chassisContainer; 9245f2caaefSJames Feist std::optional<double> failSafePercent; 925d3ec07f8SJames Feist std::optional<double> minThermalOutput; 926b6baeaa4SJames Feist if (!redfish::json_util::readJson(it.value(), response->res, "Chassis", 9275f2caaefSJames Feist chassisContainer, "FailSafePercent", 928d3ec07f8SJames Feist failSafePercent, "MinThermalOutput", 929d3ec07f8SJames Feist minThermalOutput)) 93083ff9ab6SJames Feist { 9315f2caaefSJames Feist BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property " 932b6baeaa4SJames Feist << it.value().dump(); 93383ff9ab6SJames Feist return CreatePIDRet::fail; 93483ff9ab6SJames Feist } 9355f2caaefSJames Feist 9365f2caaefSJames Feist if (chassisContainer) 93783ff9ab6SJames Feist { 9385f2caaefSJames Feist 9395f2caaefSJames Feist std::string chassisId; 9405f2caaefSJames Feist if (!redfish::json_util::readJson(*chassisContainer, response->res, 9415f2caaefSJames Feist "@odata.id", chassisId)) 9425f2caaefSJames Feist { 9435f2caaefSJames Feist BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property " 9445f2caaefSJames Feist << chassisContainer->dump(); 94583ff9ab6SJames Feist return CreatePIDRet::fail; 94683ff9ab6SJames Feist } 94783ff9ab6SJames Feist 948717794d5SAppaRao Puli // /redfish/v1/chassis/chassis_name/ 9495f2caaefSJames Feist if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis)) 95083ff9ab6SJames Feist { 9515f2caaefSJames Feist BMCWEB_LOG_ERROR << "Got invalid path " << chassisId; 9525f2caaefSJames Feist messages::invalidObject(response->res, chassisId); 95383ff9ab6SJames Feist return CreatePIDRet::fail; 95483ff9ab6SJames Feist } 95583ff9ab6SJames Feist } 956d3ec07f8SJames Feist if (minThermalOutput) 95783ff9ab6SJames Feist { 958d3ec07f8SJames Feist output["MinThermalOutput"] = *minThermalOutput; 9595f2caaefSJames Feist } 9605f2caaefSJames Feist if (failSafePercent) 96183ff9ab6SJames Feist { 9625f2caaefSJames Feist output["FailSafePercent"] = *failSafePercent; 9635f2caaefSJames Feist } 9645f2caaefSJames Feist } 9655f2caaefSJames Feist else if (type == "StepwiseControllers") 9665f2caaefSJames Feist { 9675f2caaefSJames Feist output["Type"] = std::string("Stepwise"); 9685f2caaefSJames Feist 9695f2caaefSJames Feist std::optional<std::vector<nlohmann::json>> zones; 9705f2caaefSJames Feist std::optional<std::vector<nlohmann::json>> steps; 9715f2caaefSJames Feist std::optional<std::vector<std::string>> inputs; 9725f2caaefSJames Feist std::optional<double> positiveHysteresis; 9735f2caaefSJames Feist std::optional<double> negativeHysteresis; 974c33a90ecSJames Feist std::optional<std::string> direction; // upper clipping curve vs lower 9755f2caaefSJames Feist if (!redfish::json_util::readJson( 976b6baeaa4SJames Feist it.value(), response->res, "Zones", zones, "Steps", steps, 977b6baeaa4SJames Feist "Inputs", inputs, "PositiveHysteresis", positiveHysteresis, 978c33a90ecSJames Feist "NegativeHysteresis", negativeHysteresis, "Direction", 979c33a90ecSJames Feist direction)) 9805f2caaefSJames Feist { 9815f2caaefSJames Feist BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property " 982b6baeaa4SJames Feist << it.value().dump(); 98383ff9ab6SJames Feist return CreatePIDRet::fail; 98483ff9ab6SJames Feist } 9855f2caaefSJames Feist 9865f2caaefSJames Feist if (zones) 98783ff9ab6SJames Feist { 988b6baeaa4SJames Feist std::vector<std::string> zonesStrs; 989b6baeaa4SJames Feist if (!getZonesFromJsonReq(response, *zones, zonesStrs)) 9905f2caaefSJames Feist { 9915f2caaefSJames Feist BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones"; 99283ff9ab6SJames Feist return CreatePIDRet::fail; 99383ff9ab6SJames Feist } 994b6baeaa4SJames Feist if (chassis.empty() && 995b6baeaa4SJames Feist !findChassis(managedObj, zonesStrs[0], chassis)) 996b6baeaa4SJames Feist { 997b6baeaa4SJames Feist BMCWEB_LOG_ERROR << "Failed to get chassis from config patch"; 998b6baeaa4SJames Feist messages::invalidObject(response->res, it.key()); 999b6baeaa4SJames Feist return CreatePIDRet::fail; 1000b6baeaa4SJames Feist } 1001b6baeaa4SJames Feist output["Zones"] = std::move(zonesStrs); 10025f2caaefSJames Feist } 10035f2caaefSJames Feist if (steps) 10045f2caaefSJames Feist { 10055f2caaefSJames Feist std::vector<double> readings; 10065f2caaefSJames Feist std::vector<double> outputs; 10075f2caaefSJames Feist for (auto& step : *steps) 10085f2caaefSJames Feist { 10095f2caaefSJames Feist double target; 1010b01bf299SEd Tanous double output; 10115f2caaefSJames Feist 10125f2caaefSJames Feist if (!redfish::json_util::readJson(step, response->res, "Target", 1013b01bf299SEd Tanous target, "Output", output)) 10145f2caaefSJames Feist { 10155f2caaefSJames Feist BMCWEB_LOG_ERROR << "Line:" << __LINE__ 1016b6baeaa4SJames Feist << ", Illegal Property " 1017b6baeaa4SJames Feist << it.value().dump(); 10185f2caaefSJames Feist return CreatePIDRet::fail; 10195f2caaefSJames Feist } 10205f2caaefSJames Feist readings.emplace_back(target); 1021b01bf299SEd Tanous outputs.emplace_back(output); 10225f2caaefSJames Feist } 10235f2caaefSJames Feist output["Reading"] = std::move(readings); 10245f2caaefSJames Feist output["Output"] = std::move(outputs); 10255f2caaefSJames Feist } 10265f2caaefSJames Feist if (inputs) 10275f2caaefSJames Feist { 10285f2caaefSJames Feist for (std::string& value : *inputs) 10295f2caaefSJames Feist { 10305f2caaefSJames Feist boost::replace_all(value, "_", " "); 10315f2caaefSJames Feist } 10325f2caaefSJames Feist output["Inputs"] = std::move(*inputs); 10335f2caaefSJames Feist } 10345f2caaefSJames Feist if (negativeHysteresis) 10355f2caaefSJames Feist { 10365f2caaefSJames Feist output["NegativeHysteresis"] = *negativeHysteresis; 10375f2caaefSJames Feist } 10385f2caaefSJames Feist if (positiveHysteresis) 10395f2caaefSJames Feist { 10405f2caaefSJames Feist output["PositiveHysteresis"] = *positiveHysteresis; 104183ff9ab6SJames Feist } 1042c33a90ecSJames Feist if (direction) 1043c33a90ecSJames Feist { 1044c33a90ecSJames Feist constexpr const std::array<const char*, 2> allowedDirections = { 1045c33a90ecSJames Feist "Ceiling", "Floor"}; 1046c33a90ecSJames Feist if (std::find(allowedDirections.begin(), allowedDirections.end(), 1047c33a90ecSJames Feist *direction) == allowedDirections.end()) 1048c33a90ecSJames Feist { 1049c33a90ecSJames Feist messages::propertyValueTypeError(response->res, "Direction", 1050c33a90ecSJames Feist *direction); 1051c33a90ecSJames Feist return CreatePIDRet::fail; 1052c33a90ecSJames Feist } 1053c33a90ecSJames Feist output["Class"] = *direction; 1054c33a90ecSJames Feist } 105583ff9ab6SJames Feist } 105683ff9ab6SJames Feist else 105783ff9ab6SJames Feist { 10585f2caaefSJames Feist BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type " << type; 105935a62c7cSJason M. Bills messages::propertyUnknown(response->res, type); 106083ff9ab6SJames Feist return CreatePIDRet::fail; 106183ff9ab6SJames Feist } 106283ff9ab6SJames Feist return CreatePIDRet::patch; 106383ff9ab6SJames Feist } 106473df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues> 106573df0db0SJames Feist { 106683ff9ab6SJames Feist 106773df0db0SJames Feist GetPIDValues(const std::shared_ptr<AsyncResp>& asyncResp) : 106873df0db0SJames Feist asyncResp(asyncResp) 106973df0db0SJames Feist 10701abe55efSEd Tanous { 10719c310685SBorawski.Lukasz } 10729c310685SBorawski.Lukasz 107373df0db0SJames Feist void run() 10745b4aa86bSJames Feist { 107573df0db0SJames Feist std::shared_ptr<GetPIDValues> self = shared_from_this(); 107673df0db0SJames Feist 107773df0db0SJames Feist // get all configurations 10785b4aa86bSJames Feist crow::connections::systemBus->async_method_call( 107973df0db0SJames Feist [self](const boost::system::error_code ec, 10805b4aa86bSJames Feist const crow::openbmc_mapper::GetSubTreeType& subtree) { 10815b4aa86bSJames Feist if (ec) 10825b4aa86bSJames Feist { 10835b4aa86bSJames Feist BMCWEB_LOG_ERROR << ec; 108473df0db0SJames Feist messages::internalError(self->asyncResp->res); 108573df0db0SJames Feist return; 108673df0db0SJames Feist } 108773df0db0SJames Feist self->subtree = subtree; 108873df0db0SJames Feist }, 108973df0db0SJames Feist "xyz.openbmc_project.ObjectMapper", 109073df0db0SJames Feist "/xyz/openbmc_project/object_mapper", 109173df0db0SJames Feist "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, 109273df0db0SJames Feist std::array<const char*, 4>{ 109373df0db0SJames Feist pidConfigurationIface, pidZoneConfigurationIface, 109473df0db0SJames Feist objectManagerIface, stepwiseConfigurationIface}); 109573df0db0SJames Feist 109673df0db0SJames Feist // at the same time get the selected profile 109773df0db0SJames Feist crow::connections::systemBus->async_method_call( 109873df0db0SJames Feist [self](const boost::system::error_code ec, 109973df0db0SJames Feist const crow::openbmc_mapper::GetSubTreeType& subtree) { 110073df0db0SJames Feist if (ec || subtree.empty()) 110173df0db0SJames Feist { 110273df0db0SJames Feist return; 110373df0db0SJames Feist } 110473df0db0SJames Feist if (subtree[0].second.size() != 1) 110573df0db0SJames Feist { 110673df0db0SJames Feist // invalid mapper response, should never happen 110773df0db0SJames Feist BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error"; 110873df0db0SJames Feist messages::internalError(self->asyncResp->res); 11095b4aa86bSJames Feist return; 11105b4aa86bSJames Feist } 11115b4aa86bSJames Feist 111273df0db0SJames Feist const std::string& path = subtree[0].first; 111373df0db0SJames Feist const std::string& owner = subtree[0].second[0].first; 111473df0db0SJames Feist crow::connections::systemBus->async_method_call( 111573df0db0SJames Feist [path, owner, self]( 111673df0db0SJames Feist const boost::system::error_code ec, 111773df0db0SJames Feist const boost::container::flat_map< 111873df0db0SJames Feist std::string, std::variant<std::vector<std::string>, 111973df0db0SJames Feist std::string>>& resp) { 112073df0db0SJames Feist if (ec) 112173df0db0SJames Feist { 112273df0db0SJames Feist BMCWEB_LOG_ERROR << "GetPIDValues: Can't get " 112373df0db0SJames Feist "thermalModeIface " 112473df0db0SJames Feist << path; 112573df0db0SJames Feist messages::internalError(self->asyncResp->res); 112673df0db0SJames Feist return; 112773df0db0SJames Feist } 1128271584abSEd Tanous const std::string* current = nullptr; 1129271584abSEd Tanous const std::vector<std::string>* supported = nullptr; 113073df0db0SJames Feist for (auto& [key, value] : resp) 113173df0db0SJames Feist { 113273df0db0SJames Feist if (key == "Current") 113373df0db0SJames Feist { 113473df0db0SJames Feist current = std::get_if<std::string>(&value); 113573df0db0SJames Feist if (current == nullptr) 113673df0db0SJames Feist { 113773df0db0SJames Feist BMCWEB_LOG_ERROR 113873df0db0SJames Feist << "GetPIDValues: thermal mode " 113973df0db0SJames Feist "iface invalid " 114073df0db0SJames Feist << path; 114173df0db0SJames Feist messages::internalError( 114273df0db0SJames Feist self->asyncResp->res); 114373df0db0SJames Feist return; 114473df0db0SJames Feist } 114573df0db0SJames Feist } 114673df0db0SJames Feist if (key == "Supported") 114773df0db0SJames Feist { 114873df0db0SJames Feist supported = 114973df0db0SJames Feist std::get_if<std::vector<std::string>>( 115073df0db0SJames Feist &value); 115173df0db0SJames Feist if (supported == nullptr) 115273df0db0SJames Feist { 115373df0db0SJames Feist BMCWEB_LOG_ERROR 115473df0db0SJames Feist << "GetPIDValues: thermal mode " 115573df0db0SJames Feist "iface invalid" 115673df0db0SJames Feist << path; 115773df0db0SJames Feist messages::internalError( 115873df0db0SJames Feist self->asyncResp->res); 115973df0db0SJames Feist return; 116073df0db0SJames Feist } 116173df0db0SJames Feist } 116273df0db0SJames Feist } 116373df0db0SJames Feist if (current == nullptr || supported == nullptr) 116473df0db0SJames Feist { 116573df0db0SJames Feist BMCWEB_LOG_ERROR << "GetPIDValues: thermal mode " 116673df0db0SJames Feist "iface invalid " 116773df0db0SJames Feist << path; 116873df0db0SJames Feist messages::internalError(self->asyncResp->res); 116973df0db0SJames Feist return; 117073df0db0SJames Feist } 117173df0db0SJames Feist self->currentProfile = *current; 117273df0db0SJames Feist self->supportedProfiles = *supported; 117373df0db0SJames Feist }, 117473df0db0SJames Feist owner, path, "org.freedesktop.DBus.Properties", "GetAll", 117573df0db0SJames Feist thermalModeIface); 117673df0db0SJames Feist }, 117773df0db0SJames Feist "xyz.openbmc_project.ObjectMapper", 117873df0db0SJames Feist "/xyz/openbmc_project/object_mapper", 117973df0db0SJames Feist "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, 118073df0db0SJames Feist std::array<const char*, 1>{thermalModeIface}); 118173df0db0SJames Feist } 118273df0db0SJames Feist 118373df0db0SJames Feist ~GetPIDValues() 118473df0db0SJames Feist { 118573df0db0SJames Feist if (asyncResp->res.result() != boost::beast::http::status::ok) 118673df0db0SJames Feist { 118773df0db0SJames Feist return; 118873df0db0SJames Feist } 11895b4aa86bSJames Feist // create map of <connection, path to objMgr>> 119073df0db0SJames Feist boost::container::flat_map<std::string, std::string> objectMgrPaths; 11916bce33bcSJames Feist boost::container::flat_set<std::string> calledConnections; 11925b4aa86bSJames Feist for (const auto& pathGroup : subtree) 11935b4aa86bSJames Feist { 11945b4aa86bSJames Feist for (const auto& connectionGroup : pathGroup.second) 11955b4aa86bSJames Feist { 11966bce33bcSJames Feist auto findConnection = 11976bce33bcSJames Feist calledConnections.find(connectionGroup.first); 11986bce33bcSJames Feist if (findConnection != calledConnections.end()) 11996bce33bcSJames Feist { 12006bce33bcSJames Feist break; 12016bce33bcSJames Feist } 120273df0db0SJames Feist for (const std::string& interface : connectionGroup.second) 12035b4aa86bSJames Feist { 12045b4aa86bSJames Feist if (interface == objectManagerIface) 12055b4aa86bSJames Feist { 120673df0db0SJames Feist objectMgrPaths[connectionGroup.first] = pathGroup.first; 12075b4aa86bSJames Feist } 12085b4aa86bSJames Feist // this list is alphabetical, so we 12095b4aa86bSJames Feist // should have found the objMgr by now 12105b4aa86bSJames Feist if (interface == pidConfigurationIface || 1211b7a08d04SJames Feist interface == pidZoneConfigurationIface || 1212b7a08d04SJames Feist interface == stepwiseConfigurationIface) 12135b4aa86bSJames Feist { 12145b4aa86bSJames Feist auto findObjMgr = 12155b4aa86bSJames Feist objectMgrPaths.find(connectionGroup.first); 12165b4aa86bSJames Feist if (findObjMgr == objectMgrPaths.end()) 12175b4aa86bSJames Feist { 12185b4aa86bSJames Feist BMCWEB_LOG_DEBUG << connectionGroup.first 12195b4aa86bSJames Feist << "Has no Object Manager"; 12205b4aa86bSJames Feist continue; 12215b4aa86bSJames Feist } 12226bce33bcSJames Feist 12236bce33bcSJames Feist calledConnections.insert(connectionGroup.first); 12246bce33bcSJames Feist 122573df0db0SJames Feist asyncPopulatePid(findObjMgr->first, findObjMgr->second, 122673df0db0SJames Feist currentProfile, supportedProfiles, 122773df0db0SJames Feist asyncResp); 12285b4aa86bSJames Feist break; 12295b4aa86bSJames Feist } 12305b4aa86bSJames Feist } 12315b4aa86bSJames Feist } 12325b4aa86bSJames Feist } 123373df0db0SJames Feist } 123473df0db0SJames Feist 123573df0db0SJames Feist std::vector<std::string> supportedProfiles; 123673df0db0SJames Feist std::string currentProfile; 123773df0db0SJames Feist crow::openbmc_mapper::GetSubTreeType subtree; 123873df0db0SJames Feist std::shared_ptr<AsyncResp> asyncResp; 123973df0db0SJames Feist }; 124073df0db0SJames Feist 124173df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues> 124273df0db0SJames Feist { 124373df0db0SJames Feist 1244271584abSEd Tanous SetPIDValues(const std::shared_ptr<AsyncResp>& asyncRespIn, 124573df0db0SJames Feist nlohmann::json& data) : 1246271584abSEd Tanous asyncResp(asyncRespIn) 124773df0db0SJames Feist { 124873df0db0SJames Feist 124973df0db0SJames Feist std::optional<nlohmann::json> pidControllers; 125073df0db0SJames Feist std::optional<nlohmann::json> fanControllers; 125173df0db0SJames Feist std::optional<nlohmann::json> fanZones; 125273df0db0SJames Feist std::optional<nlohmann::json> stepwiseControllers; 125373df0db0SJames Feist 125473df0db0SJames Feist if (!redfish::json_util::readJson( 125573df0db0SJames Feist data, asyncResp->res, "PidControllers", pidControllers, 125673df0db0SJames Feist "FanControllers", fanControllers, "FanZones", fanZones, 125773df0db0SJames Feist "StepwiseControllers", stepwiseControllers, "Profile", profile)) 125873df0db0SJames Feist { 125973df0db0SJames Feist BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property " 126073df0db0SJames Feist << data.dump(); 126173df0db0SJames Feist return; 126273df0db0SJames Feist } 126373df0db0SJames Feist configuration.emplace_back("PidControllers", std::move(pidControllers)); 126473df0db0SJames Feist configuration.emplace_back("FanControllers", std::move(fanControllers)); 126573df0db0SJames Feist configuration.emplace_back("FanZones", std::move(fanZones)); 126673df0db0SJames Feist configuration.emplace_back("StepwiseControllers", 126773df0db0SJames Feist std::move(stepwiseControllers)); 126873df0db0SJames Feist } 126973df0db0SJames Feist void run() 127073df0db0SJames Feist { 127173df0db0SJames Feist if (asyncResp->res.result() != boost::beast::http::status::ok) 127273df0db0SJames Feist { 127373df0db0SJames Feist return; 127473df0db0SJames Feist } 127573df0db0SJames Feist 127673df0db0SJames Feist std::shared_ptr<SetPIDValues> self = shared_from_this(); 127773df0db0SJames Feist 127873df0db0SJames Feist // todo(james): might make sense to do a mapper call here if this 127973df0db0SJames Feist // interface gets more traction 128073df0db0SJames Feist crow::connections::systemBus->async_method_call( 128173df0db0SJames Feist [self](const boost::system::error_code ec, 1282271584abSEd Tanous dbus::utility::ManagedObjectType& mObj) { 128373df0db0SJames Feist if (ec) 128473df0db0SJames Feist { 128573df0db0SJames Feist BMCWEB_LOG_ERROR << "Error communicating to Entity Manager"; 128673df0db0SJames Feist messages::internalError(self->asyncResp->res); 128773df0db0SJames Feist return; 128873df0db0SJames Feist } 1289e69d9de2SJames Feist const std::array<const char*, 3> configurations = { 1290e69d9de2SJames Feist pidConfigurationIface, pidZoneConfigurationIface, 1291e69d9de2SJames Feist stepwiseConfigurationIface}; 1292e69d9de2SJames Feist 129314b0b8d5SJames Feist for (const auto& [path, object] : mObj) 1294e69d9de2SJames Feist { 129514b0b8d5SJames Feist for (const auto& [interface, _] : object) 1296e69d9de2SJames Feist { 1297e69d9de2SJames Feist if (std::find(configurations.begin(), 1298e69d9de2SJames Feist configurations.end(), 1299e69d9de2SJames Feist interface) != configurations.end()) 1300e69d9de2SJames Feist { 130114b0b8d5SJames Feist self->objectCount++; 1302e69d9de2SJames Feist break; 1303e69d9de2SJames Feist } 1304e69d9de2SJames Feist } 1305e69d9de2SJames Feist } 1306271584abSEd Tanous self->managedObj = std::move(mObj); 130773df0db0SJames Feist }, 130873df0db0SJames Feist "xyz.openbmc_project.EntityManager", "/", objectManagerIface, 130973df0db0SJames Feist "GetManagedObjects"); 131073df0db0SJames Feist 131173df0db0SJames Feist // at the same time get the profile information 131273df0db0SJames Feist crow::connections::systemBus->async_method_call( 131373df0db0SJames Feist [self](const boost::system::error_code ec, 131473df0db0SJames Feist const crow::openbmc_mapper::GetSubTreeType& subtree) { 131573df0db0SJames Feist if (ec || subtree.empty()) 131673df0db0SJames Feist { 131773df0db0SJames Feist return; 131873df0db0SJames Feist } 131973df0db0SJames Feist if (subtree[0].second.empty()) 132073df0db0SJames Feist { 132173df0db0SJames Feist // invalid mapper response, should never happen 132273df0db0SJames Feist BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error"; 132373df0db0SJames Feist messages::internalError(self->asyncResp->res); 132473df0db0SJames Feist return; 132573df0db0SJames Feist } 132673df0db0SJames Feist 132773df0db0SJames Feist const std::string& path = subtree[0].first; 132873df0db0SJames Feist const std::string& owner = subtree[0].second[0].first; 132973df0db0SJames Feist crow::connections::systemBus->async_method_call( 133073df0db0SJames Feist [self, path, owner]( 133173df0db0SJames Feist const boost::system::error_code ec, 133273df0db0SJames Feist const boost::container::flat_map< 133373df0db0SJames Feist std::string, std::variant<std::vector<std::string>, 1334271584abSEd Tanous std::string>>& r) { 133573df0db0SJames Feist if (ec) 133673df0db0SJames Feist { 133773df0db0SJames Feist BMCWEB_LOG_ERROR << "SetPIDValues: Can't get " 133873df0db0SJames Feist "thermalModeIface " 133973df0db0SJames Feist << path; 134073df0db0SJames Feist messages::internalError(self->asyncResp->res); 134173df0db0SJames Feist return; 134273df0db0SJames Feist } 1343271584abSEd Tanous const std::string* current = nullptr; 1344271584abSEd Tanous const std::vector<std::string>* supported = nullptr; 1345271584abSEd Tanous for (auto& [key, value] : r) 134673df0db0SJames Feist { 134773df0db0SJames Feist if (key == "Current") 134873df0db0SJames Feist { 134973df0db0SJames Feist current = std::get_if<std::string>(&value); 135073df0db0SJames Feist if (current == nullptr) 135173df0db0SJames Feist { 135273df0db0SJames Feist BMCWEB_LOG_ERROR 135373df0db0SJames Feist << "SetPIDValues: thermal mode " 135473df0db0SJames Feist "iface invalid " 135573df0db0SJames Feist << path; 135673df0db0SJames Feist messages::internalError( 135773df0db0SJames Feist self->asyncResp->res); 135873df0db0SJames Feist return; 135973df0db0SJames Feist } 136073df0db0SJames Feist } 136173df0db0SJames Feist if (key == "Supported") 136273df0db0SJames Feist { 136373df0db0SJames Feist supported = 136473df0db0SJames Feist std::get_if<std::vector<std::string>>( 136573df0db0SJames Feist &value); 136673df0db0SJames Feist if (supported == nullptr) 136773df0db0SJames Feist { 136873df0db0SJames Feist BMCWEB_LOG_ERROR 136973df0db0SJames Feist << "SetPIDValues: thermal mode " 137073df0db0SJames Feist "iface invalid" 137173df0db0SJames Feist << path; 137273df0db0SJames Feist messages::internalError( 137373df0db0SJames Feist self->asyncResp->res); 137473df0db0SJames Feist return; 137573df0db0SJames Feist } 137673df0db0SJames Feist } 137773df0db0SJames Feist } 137873df0db0SJames Feist if (current == nullptr || supported == nullptr) 137973df0db0SJames Feist { 138073df0db0SJames Feist BMCWEB_LOG_ERROR << "SetPIDValues: thermal mode " 138173df0db0SJames Feist "iface invalid " 138273df0db0SJames Feist << path; 138373df0db0SJames Feist messages::internalError(self->asyncResp->res); 138473df0db0SJames Feist return; 138573df0db0SJames Feist } 138673df0db0SJames Feist self->currentProfile = *current; 138773df0db0SJames Feist self->supportedProfiles = *supported; 138873df0db0SJames Feist self->profileConnection = owner; 138973df0db0SJames Feist self->profilePath = path; 139073df0db0SJames Feist }, 139173df0db0SJames Feist owner, path, "org.freedesktop.DBus.Properties", "GetAll", 139273df0db0SJames Feist thermalModeIface); 13935b4aa86bSJames Feist }, 13945b4aa86bSJames Feist "xyz.openbmc_project.ObjectMapper", 13955b4aa86bSJames Feist "/xyz/openbmc_project/object_mapper", 13965b4aa86bSJames Feist "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, 139773df0db0SJames Feist std::array<const char*, 1>{thermalModeIface}); 139873df0db0SJames Feist } 139973df0db0SJames Feist ~SetPIDValues() 140073df0db0SJames Feist { 140173df0db0SJames Feist if (asyncResp->res.result() != boost::beast::http::status::ok) 140273df0db0SJames Feist { 140373df0db0SJames Feist return; 14045b4aa86bSJames Feist } 14055b4aa86bSJames Feist 140673df0db0SJames Feist std::shared_ptr<AsyncResp> response = asyncResp; 140773df0db0SJames Feist 140873df0db0SJames Feist if (profile) 140973df0db0SJames Feist { 141073df0db0SJames Feist if (std::find(supportedProfiles.begin(), supportedProfiles.end(), 141173df0db0SJames Feist *profile) == supportedProfiles.end()) 141273df0db0SJames Feist { 141373df0db0SJames Feist messages::actionParameterUnknown(response->res, "Profile", 141473df0db0SJames Feist *profile); 141573df0db0SJames Feist return; 141673df0db0SJames Feist } 141773df0db0SJames Feist currentProfile = *profile; 141873df0db0SJames Feist crow::connections::systemBus->async_method_call( 141973df0db0SJames Feist [response](const boost::system::error_code ec) { 142073df0db0SJames Feist if (ec) 142173df0db0SJames Feist { 142273df0db0SJames Feist BMCWEB_LOG_ERROR << "Error patching profile" << ec; 142373df0db0SJames Feist messages::internalError(response->res); 142473df0db0SJames Feist } 142573df0db0SJames Feist }, 142673df0db0SJames Feist profileConnection, profilePath, 142773df0db0SJames Feist "org.freedesktop.DBus.Properties", "Set", thermalModeIface, 142873df0db0SJames Feist "Current", std::variant<std::string>(*profile)); 142973df0db0SJames Feist } 143073df0db0SJames Feist 143173df0db0SJames Feist for (auto& containerPair : configuration) 143273df0db0SJames Feist { 143373df0db0SJames Feist auto& container = containerPair.second; 143473df0db0SJames Feist if (!container) 143573df0db0SJames Feist { 143673df0db0SJames Feist continue; 143773df0db0SJames Feist } 14386ee7f774SJames Feist BMCWEB_LOG_DEBUG << *container; 14396ee7f774SJames Feist 144073df0db0SJames Feist std::string& type = containerPair.first; 144173df0db0SJames Feist 144273df0db0SJames Feist for (nlohmann::json::iterator it = container->begin(); 144373df0db0SJames Feist it != container->end(); it++) 144473df0db0SJames Feist { 144573df0db0SJames Feist const auto& name = it.key(); 14466ee7f774SJames Feist BMCWEB_LOG_DEBUG << "looking for " << name; 14476ee7f774SJames Feist 144873df0db0SJames Feist auto pathItr = 144973df0db0SJames Feist std::find_if(managedObj.begin(), managedObj.end(), 145073df0db0SJames Feist [&name](const auto& obj) { 145173df0db0SJames Feist return boost::algorithm::ends_with( 145273df0db0SJames Feist obj.first.str, "/" + name); 145373df0db0SJames Feist }); 145473df0db0SJames Feist boost::container::flat_map<std::string, 145573df0db0SJames Feist dbus::utility::DbusVariantType> 145673df0db0SJames Feist output; 145773df0db0SJames Feist 145873df0db0SJames Feist output.reserve(16); // The pid interface length 145973df0db0SJames Feist 146073df0db0SJames Feist // determines if we're patching entity-manager or 146173df0db0SJames Feist // creating a new object 146273df0db0SJames Feist bool createNewObject = (pathItr == managedObj.end()); 14636ee7f774SJames Feist BMCWEB_LOG_DEBUG << "Found = " << !createNewObject; 14646ee7f774SJames Feist 146573df0db0SJames Feist std::string iface; 146673df0db0SJames Feist if (type == "PidControllers" || type == "FanControllers") 146773df0db0SJames Feist { 146873df0db0SJames Feist iface = pidConfigurationIface; 146973df0db0SJames Feist if (!createNewObject && 147073df0db0SJames Feist pathItr->second.find(pidConfigurationIface) == 147173df0db0SJames Feist pathItr->second.end()) 147273df0db0SJames Feist { 147373df0db0SJames Feist createNewObject = true; 147473df0db0SJames Feist } 147573df0db0SJames Feist } 147673df0db0SJames Feist else if (type == "FanZones") 147773df0db0SJames Feist { 147873df0db0SJames Feist iface = pidZoneConfigurationIface; 147973df0db0SJames Feist if (!createNewObject && 148073df0db0SJames Feist pathItr->second.find(pidZoneConfigurationIface) == 148173df0db0SJames Feist pathItr->second.end()) 148273df0db0SJames Feist { 148373df0db0SJames Feist 148473df0db0SJames Feist createNewObject = true; 148573df0db0SJames Feist } 148673df0db0SJames Feist } 148773df0db0SJames Feist else if (type == "StepwiseControllers") 148873df0db0SJames Feist { 148973df0db0SJames Feist iface = stepwiseConfigurationIface; 149073df0db0SJames Feist if (!createNewObject && 149173df0db0SJames Feist pathItr->second.find(stepwiseConfigurationIface) == 149273df0db0SJames Feist pathItr->second.end()) 149373df0db0SJames Feist { 149473df0db0SJames Feist createNewObject = true; 149573df0db0SJames Feist } 149673df0db0SJames Feist } 14976ee7f774SJames Feist 14986ee7f774SJames Feist if (createNewObject && it.value() == nullptr) 14996ee7f774SJames Feist { 15006ee7f774SJames Feist // can't delete a non-existant object 15016ee7f774SJames Feist messages::invalidObject(response->res, name); 15026ee7f774SJames Feist continue; 15036ee7f774SJames Feist } 15046ee7f774SJames Feist 15056ee7f774SJames Feist std::string path; 15066ee7f774SJames Feist if (pathItr != managedObj.end()) 15076ee7f774SJames Feist { 15086ee7f774SJames Feist path = pathItr->first.str; 15096ee7f774SJames Feist } 15106ee7f774SJames Feist 151173df0db0SJames Feist BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n"; 1512e69d9de2SJames Feist 1513e69d9de2SJames Feist // arbitrary limit to avoid attacks 1514e69d9de2SJames Feist constexpr const size_t controllerLimit = 500; 151514b0b8d5SJames Feist if (createNewObject && objectCount >= controllerLimit) 1516e69d9de2SJames Feist { 1517e69d9de2SJames Feist messages::resourceExhaustion(response->res, type); 1518e69d9de2SJames Feist continue; 1519e69d9de2SJames Feist } 1520e69d9de2SJames Feist 152173df0db0SJames Feist output["Name"] = boost::replace_all_copy(name, "_", " "); 152273df0db0SJames Feist 152373df0db0SJames Feist std::string chassis; 152473df0db0SJames Feist CreatePIDRet ret = createPidInterface( 15256ee7f774SJames Feist response, type, it, path, managedObj, createNewObject, 15266ee7f774SJames Feist output, chassis, currentProfile); 152773df0db0SJames Feist if (ret == CreatePIDRet::fail) 152873df0db0SJames Feist { 152973df0db0SJames Feist return; 153073df0db0SJames Feist } 153173df0db0SJames Feist else if (ret == CreatePIDRet::del) 153273df0db0SJames Feist { 153373df0db0SJames Feist continue; 153473df0db0SJames Feist } 153573df0db0SJames Feist 153673df0db0SJames Feist if (!createNewObject) 153773df0db0SJames Feist { 153873df0db0SJames Feist for (const auto& property : output) 153973df0db0SJames Feist { 154073df0db0SJames Feist crow::connections::systemBus->async_method_call( 154173df0db0SJames Feist [response, 154273df0db0SJames Feist propertyName{std::string(property.first)}]( 154373df0db0SJames Feist const boost::system::error_code ec) { 154473df0db0SJames Feist if (ec) 154573df0db0SJames Feist { 154673df0db0SJames Feist BMCWEB_LOG_ERROR << "Error patching " 154773df0db0SJames Feist << propertyName << ": " 154873df0db0SJames Feist << ec; 154973df0db0SJames Feist messages::internalError(response->res); 155073df0db0SJames Feist return; 155173df0db0SJames Feist } 155273df0db0SJames Feist messages::success(response->res); 155373df0db0SJames Feist }, 15546ee7f774SJames Feist "xyz.openbmc_project.EntityManager", path, 155573df0db0SJames Feist "org.freedesktop.DBus.Properties", "Set", iface, 155673df0db0SJames Feist property.first, property.second); 155773df0db0SJames Feist } 155873df0db0SJames Feist } 155973df0db0SJames Feist else 156073df0db0SJames Feist { 156173df0db0SJames Feist if (chassis.empty()) 156273df0db0SJames Feist { 156373df0db0SJames Feist BMCWEB_LOG_ERROR << "Failed to get chassis from config"; 156473df0db0SJames Feist messages::invalidObject(response->res, name); 156573df0db0SJames Feist return; 156673df0db0SJames Feist } 156773df0db0SJames Feist 156873df0db0SJames Feist bool foundChassis = false; 156973df0db0SJames Feist for (const auto& obj : managedObj) 157073df0db0SJames Feist { 157173df0db0SJames Feist if (boost::algorithm::ends_with(obj.first.str, chassis)) 157273df0db0SJames Feist { 157373df0db0SJames Feist chassis = obj.first.str; 157473df0db0SJames Feist foundChassis = true; 157573df0db0SJames Feist break; 157673df0db0SJames Feist } 157773df0db0SJames Feist } 157873df0db0SJames Feist if (!foundChassis) 157973df0db0SJames Feist { 158073df0db0SJames Feist BMCWEB_LOG_ERROR << "Failed to find chassis on dbus"; 158173df0db0SJames Feist messages::resourceMissingAtURI( 158273df0db0SJames Feist response->res, "/redfish/v1/Chassis/" + chassis); 158373df0db0SJames Feist return; 158473df0db0SJames Feist } 158573df0db0SJames Feist 158673df0db0SJames Feist crow::connections::systemBus->async_method_call( 158773df0db0SJames Feist [response](const boost::system::error_code ec) { 158873df0db0SJames Feist if (ec) 158973df0db0SJames Feist { 159073df0db0SJames Feist BMCWEB_LOG_ERROR << "Error Adding Pid Object " 159173df0db0SJames Feist << ec; 159273df0db0SJames Feist messages::internalError(response->res); 159373df0db0SJames Feist return; 159473df0db0SJames Feist } 159573df0db0SJames Feist messages::success(response->res); 159673df0db0SJames Feist }, 159773df0db0SJames Feist "xyz.openbmc_project.EntityManager", chassis, 159873df0db0SJames Feist "xyz.openbmc_project.AddObject", "AddObject", output); 159973df0db0SJames Feist } 160073df0db0SJames Feist } 160173df0db0SJames Feist } 160273df0db0SJames Feist } 160373df0db0SJames Feist std::shared_ptr<AsyncResp> asyncResp; 160473df0db0SJames Feist std::vector<std::pair<std::string, std::optional<nlohmann::json>>> 160573df0db0SJames Feist configuration; 160673df0db0SJames Feist std::optional<std::string> profile; 160773df0db0SJames Feist dbus::utility::ManagedObjectType managedObj; 160873df0db0SJames Feist std::vector<std::string> supportedProfiles; 160973df0db0SJames Feist std::string currentProfile; 161073df0db0SJames Feist std::string profileConnection; 161173df0db0SJames Feist std::string profilePath; 161214b0b8d5SJames Feist size_t objectCount = 0; 161373df0db0SJames Feist }; 161473df0db0SJames Feist 161573df0db0SJames Feist class Manager : public Node 161673df0db0SJames Feist { 161773df0db0SJames Feist public: 161873df0db0SJames Feist Manager(CrowApp& app) : Node(app, "/redfish/v1/Managers/bmc/") 161973df0db0SJames Feist { 162073df0db0SJames Feist uuid = app.template getMiddleware<crow::persistent_data::Middleware>() 162173df0db0SJames Feist .systemUuid; 162273df0db0SJames Feist entityPrivileges = { 162373df0db0SJames Feist {boost::beast::http::verb::get, {{"Login"}}}, 162473df0db0SJames Feist {boost::beast::http::verb::head, {{"Login"}}}, 162573df0db0SJames Feist {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, 162673df0db0SJames Feist {boost::beast::http::verb::put, {{"ConfigureManager"}}}, 162773df0db0SJames Feist {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, 162873df0db0SJames Feist {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; 162973df0db0SJames Feist } 163073df0db0SJames Feist 163173df0db0SJames Feist private: 163255c7b7a2SEd Tanous void doGet(crow::Response& res, const crow::Request& req, 16331abe55efSEd Tanous const std::vector<std::string>& params) override 16341abe55efSEd Tanous { 16350f74e643SEd Tanous res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc"; 1636*3e40fc74SGunnar Mills res.jsonValue["@odata.type"] = "#Manager.v1_8_0.Manager"; 16370f74e643SEd Tanous res.jsonValue["Id"] = "bmc"; 16380f74e643SEd Tanous res.jsonValue["Name"] = "OpenBmc Manager"; 16390f74e643SEd Tanous res.jsonValue["Description"] = "Baseboard Management Controller"; 16400f74e643SEd Tanous res.jsonValue["PowerState"] = "On"; 1641029573d4SEd Tanous res.jsonValue["Status"] = {{"State", "Enabled"}, {"Health", "OK"}}; 16420f74e643SEd Tanous res.jsonValue["ManagerType"] = "BMC"; 16433602e232SEd Tanous res.jsonValue["UUID"] = systemd_utils::getUuid(); 16443602e232SEd Tanous res.jsonValue["ServiceEntryPointUUID"] = uuid; 16450f74e643SEd Tanous res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model 16460f74e643SEd Tanous 16470f74e643SEd Tanous res.jsonValue["LogServices"] = { 16480f74e643SEd Tanous {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}}; 16490f74e643SEd Tanous 16500f74e643SEd Tanous res.jsonValue["NetworkProtocol"] = { 16510f74e643SEd Tanous {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}}; 16520f74e643SEd Tanous 16530f74e643SEd Tanous res.jsonValue["EthernetInterfaces"] = { 16540f74e643SEd Tanous {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}}; 1655107077deSPrzemyslaw Czarnowski 1656107077deSPrzemyslaw Czarnowski #ifdef BMCWEB_ENABLE_VM_NBDPROXY 1657107077deSPrzemyslaw Czarnowski res.jsonValue["VirtualMedia"] = { 1658107077deSPrzemyslaw Czarnowski {"@odata.id", "/redfish/v1/Managers/bmc/VirtualMedia"}}; 1659107077deSPrzemyslaw Czarnowski #endif // BMCWEB_ENABLE_VM_NBDPROXY 1660107077deSPrzemyslaw Czarnowski 16610f74e643SEd Tanous // default oem data 16620f74e643SEd Tanous nlohmann::json& oem = res.jsonValue["Oem"]; 16630f74e643SEd Tanous nlohmann::json& oemOpenbmc = oem["OpenBmc"]; 16640f74e643SEd Tanous oem["@odata.type"] = "#OemManager.Oem"; 16650f74e643SEd Tanous oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem"; 16660f74e643SEd Tanous oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc"; 16670f74e643SEd Tanous oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc"; 1668cfcd5f6bSMarri Devender Rao oemOpenbmc["Certificates"] = { 1669cfcd5f6bSMarri Devender Rao {"@odata.id", "/redfish/v1/Managers/bmc/Truststore/Certificates"}}; 16700f74e643SEd Tanous 16712a5c4407SGunnar Mills // Manager.Reset (an action) can be many values, OpenBMC only supports 16722a5c4407SGunnar Mills // BMC reboot. 16732a5c4407SGunnar Mills nlohmann::json& managerReset = 16740f74e643SEd Tanous res.jsonValue["Actions"]["#Manager.Reset"]; 16752a5c4407SGunnar Mills managerReset["target"] = 1676ed5befbdSJennifer Lee "/redfish/v1/Managers/bmc/Actions/Manager.Reset"; 16772a5c4407SGunnar Mills managerReset["ResetType@Redfish.AllowableValues"] = {"GracefulRestart"}; 1678ca537928SJennifer Lee 1679*3e40fc74SGunnar Mills // ResetToDefaults (Factory Reset) has values like 1680*3e40fc74SGunnar Mills // PreserveNetworkAndUsers and PreserveNetwork that aren't supported 1681*3e40fc74SGunnar Mills // on OpenBMC 1682*3e40fc74SGunnar Mills nlohmann::json& resetToDefaults = 1683*3e40fc74SGunnar Mills res.jsonValue["Actions"]["#Manager.ResetToDefaults"]; 1684*3e40fc74SGunnar Mills resetToDefaults["target"] = 1685*3e40fc74SGunnar Mills "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults"; 1686*3e40fc74SGunnar Mills resetToDefaults["ResetType@Redfish.AllowableValues"] = {"ResetAll"}; 1687*3e40fc74SGunnar Mills 1688cb92c03bSAndrew Geissler res.jsonValue["DateTime"] = crow::utility::dateTimeNow(); 1689474bfad5SSantosh Puranik 1690f8c3e6f0SKuiying Wang // Fill in SerialConsole info 1691474bfad5SSantosh Puranik res.jsonValue["SerialConsole"]["ServiceEnabled"] = true; 1692f8c3e6f0SKuiying Wang res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15; 1693474bfad5SSantosh Puranik res.jsonValue["SerialConsole"]["ConnectTypesSupported"] = {"IPMI", 1694474bfad5SSantosh Puranik "SSH"}; 1695ef47bb18SSantosh Puranik #ifdef BMCWEB_ENABLE_KVM 1696f8c3e6f0SKuiying Wang // Fill in GraphicalConsole info 1697ef47bb18SSantosh Puranik res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true; 1698704fae6cSJae Hyun Yoo res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4; 1699ef47bb18SSantosh Puranik res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] = {"KVMIP"}; 1700ef47bb18SSantosh Puranik #endif // BMCWEB_ENABLE_KVM 1701474bfad5SSantosh Puranik 1702603a6640SGunnar Mills res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1; 1703603a6640SGunnar Mills res.jsonValue["Links"]["ManagerForServers"] = { 1704603a6640SGunnar Mills {{"@odata.id", "/redfish/v1/Systems/system"}}}; 170526f03899SShawn McCarney 1706ed5befbdSJennifer Lee std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 17075b4aa86bSJames Feist 1708b49ac873SJames Feist auto health = std::make_shared<HealthPopulate>(asyncResp); 1709b49ac873SJames Feist health->isManagersHealth = true; 1710b49ac873SJames Feist health->populate(); 1711b49ac873SJames Feist 1712e90c5052SAndrew Geissler fw_util::getActiveFwVersion(asyncResp, fw_util::bmcPurpose, 1713e90c5052SAndrew Geissler "FirmwareVersion"); 17140f6b00bdSJames Feist 171573df0db0SJames Feist auto pids = std::make_shared<GetPIDValues>(asyncResp); 171673df0db0SJames Feist pids->run(); 1717c5d03ff4SJennifer Lee 1718c5d03ff4SJennifer Lee getMainChassisId(asyncResp, [](const std::string& chassisId, 1719c5d03ff4SJennifer Lee const std::shared_ptr<AsyncResp> aRsp) { 1720c5d03ff4SJennifer Lee aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1; 1721c5d03ff4SJennifer Lee aRsp->res.jsonValue["Links"]["ManagerForChassis"] = { 1722c5d03ff4SJennifer Lee {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}}; 17232c0feb00SJason M. Bills aRsp->res.jsonValue["Links"]["ManagerInChassis"] = { 17242c0feb00SJason M. Bills {"@odata.id", "/redfish/v1/Chassis/" + chassisId}}; 1725c5d03ff4SJennifer Lee }); 17260f6b00bdSJames Feist 17270f6b00bdSJames Feist static bool started = false; 17280f6b00bdSJames Feist 17290f6b00bdSJames Feist if (!started) 17300f6b00bdSJames Feist { 17310f6b00bdSJames Feist crow::connections::systemBus->async_method_call( 17320f6b00bdSJames Feist [asyncResp](const boost::system::error_code ec, 17330f6b00bdSJames Feist const std::variant<double>& resp) { 17340f6b00bdSJames Feist if (ec) 17350f6b00bdSJames Feist { 17360f6b00bdSJames Feist BMCWEB_LOG_ERROR << "Error while getting progress"; 17370f6b00bdSJames Feist messages::internalError(asyncResp->res); 17380f6b00bdSJames Feist return; 17390f6b00bdSJames Feist } 17400f6b00bdSJames Feist const double* val = std::get_if<double>(&resp); 17410f6b00bdSJames Feist if (val == nullptr) 17420f6b00bdSJames Feist { 17430f6b00bdSJames Feist BMCWEB_LOG_ERROR 17440f6b00bdSJames Feist << "Invalid response while getting progress"; 17450f6b00bdSJames Feist messages::internalError(asyncResp->res); 17460f6b00bdSJames Feist return; 17470f6b00bdSJames Feist } 17480f6b00bdSJames Feist if (*val < 1.0) 17490f6b00bdSJames Feist { 17500f6b00bdSJames Feist asyncResp->res.jsonValue["Status"]["State"] = 17510f6b00bdSJames Feist "Starting"; 17520f6b00bdSJames Feist started = true; 17530f6b00bdSJames Feist } 17540f6b00bdSJames Feist }, 17550f6b00bdSJames Feist "org.freedesktop.systemd1", "/org/freedesktop/systemd1", 17560f6b00bdSJames Feist "org.freedesktop.DBus.Properties", "Get", 17570f6b00bdSJames Feist "org.freedesktop.systemd1.Manager", "Progress"); 17580f6b00bdSJames Feist } 175983ff9ab6SJames Feist } 17605b4aa86bSJames Feist 17615b4aa86bSJames Feist void doPatch(crow::Response& res, const crow::Request& req, 17625b4aa86bSJames Feist const std::vector<std::string>& params) override 17635b4aa86bSJames Feist { 17640627a2c7SEd Tanous std::optional<nlohmann::json> oem; 1765af5d6058SSantosh Puranik std::optional<std::string> datetime; 176641352c24SSantosh Puranik std::shared_ptr<AsyncResp> response = std::make_shared<AsyncResp>(res); 17670627a2c7SEd Tanous 176841352c24SSantosh Puranik if (!json_util::readJson(req, response->res, "Oem", oem, "DateTime", 176941352c24SSantosh Puranik datetime)) 177083ff9ab6SJames Feist { 177183ff9ab6SJames Feist return; 177283ff9ab6SJames Feist } 17730627a2c7SEd Tanous 17740627a2c7SEd Tanous if (oem) 177583ff9ab6SJames Feist { 17765f2caaefSJames Feist std::optional<nlohmann::json> openbmc; 177743b761d0SEd Tanous if (!redfish::json_util::readJson(*oem, res, "OpenBmc", openbmc)) 177883ff9ab6SJames Feist { 177943b761d0SEd Tanous BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property " 178043b761d0SEd Tanous << oem->dump(); 178183ff9ab6SJames Feist return; 178283ff9ab6SJames Feist } 17835f2caaefSJames Feist if (openbmc) 178483ff9ab6SJames Feist { 17855f2caaefSJames Feist std::optional<nlohmann::json> fan; 178643b761d0SEd Tanous if (!redfish::json_util::readJson(*openbmc, res, "Fan", fan)) 178783ff9ab6SJames Feist { 17885f2caaefSJames Feist BMCWEB_LOG_ERROR << "Line:" << __LINE__ 17895f2caaefSJames Feist << ", Illegal Property " 17905f2caaefSJames Feist << openbmc->dump(); 179183ff9ab6SJames Feist return; 179283ff9ab6SJames Feist } 17935f2caaefSJames Feist if (fan) 179483ff9ab6SJames Feist { 179573df0db0SJames Feist auto pid = std::make_shared<SetPIDValues>(response, *fan); 179673df0db0SJames Feist pid->run(); 179783ff9ab6SJames Feist } 179883ff9ab6SJames Feist } 179983ff9ab6SJames Feist } 1800af5d6058SSantosh Puranik if (datetime) 1801af5d6058SSantosh Puranik { 1802af5d6058SSantosh Puranik setDateTime(response, std::move(*datetime)); 1803af5d6058SSantosh Puranik } 1804af5d6058SSantosh Puranik } 1805af5d6058SSantosh Puranik 1806af5d6058SSantosh Puranik void setDateTime(std::shared_ptr<AsyncResp> aResp, 1807af5d6058SSantosh Puranik std::string datetime) const 1808af5d6058SSantosh Puranik { 1809af5d6058SSantosh Puranik BMCWEB_LOG_DEBUG << "Set date time: " << datetime; 1810af5d6058SSantosh Puranik 1811af5d6058SSantosh Puranik std::stringstream stream(datetime); 1812af5d6058SSantosh Puranik // Convert from ISO 8601 to boost local_time 1813af5d6058SSantosh Puranik // (BMC only has time in UTC) 1814af5d6058SSantosh Puranik boost::posix_time::ptime posixTime; 1815af5d6058SSantosh Puranik boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1)); 1816af5d6058SSantosh Puranik // Facet gets deleted with the stringsteam 1817af5d6058SSantosh Puranik auto ifc = std::make_unique<boost::local_time::local_time_input_facet>( 1818af5d6058SSantosh Puranik "%Y-%m-%d %H:%M:%S%F %ZP"); 1819af5d6058SSantosh Puranik stream.imbue(std::locale(stream.getloc(), ifc.release())); 1820af5d6058SSantosh Puranik 1821af5d6058SSantosh Puranik boost::local_time::local_date_time ldt( 1822af5d6058SSantosh Puranik boost::local_time::not_a_date_time); 1823af5d6058SSantosh Puranik 1824af5d6058SSantosh Puranik if (stream >> ldt) 1825af5d6058SSantosh Puranik { 1826af5d6058SSantosh Puranik posixTime = ldt.utc_time(); 1827af5d6058SSantosh Puranik boost::posix_time::time_duration dur = posixTime - epoch; 1828af5d6058SSantosh Puranik uint64_t durMicroSecs = 1829af5d6058SSantosh Puranik static_cast<uint64_t>(dur.total_microseconds()); 1830af5d6058SSantosh Puranik crow::connections::systemBus->async_method_call( 1831af5d6058SSantosh Puranik [aResp{std::move(aResp)}, datetime{std::move(datetime)}]( 1832af5d6058SSantosh Puranik const boost::system::error_code ec) { 1833af5d6058SSantosh Puranik if (ec) 1834af5d6058SSantosh Puranik { 1835af5d6058SSantosh Puranik BMCWEB_LOG_DEBUG << "Failed to set elapsed time. " 1836af5d6058SSantosh Puranik "DBUS response error " 1837af5d6058SSantosh Puranik << ec; 1838af5d6058SSantosh Puranik messages::internalError(aResp->res); 1839af5d6058SSantosh Puranik return; 1840af5d6058SSantosh Puranik } 1841af5d6058SSantosh Puranik aResp->res.jsonValue["DateTime"] = datetime; 1842af5d6058SSantosh Puranik }, 1843af5d6058SSantosh Puranik "xyz.openbmc_project.Time.Manager", 1844af5d6058SSantosh Puranik "/xyz/openbmc_project/time/bmc", 1845af5d6058SSantosh Puranik "org.freedesktop.DBus.Properties", "Set", 1846af5d6058SSantosh Puranik "xyz.openbmc_project.Time.EpochTime", "Elapsed", 1847af5d6058SSantosh Puranik std::variant<uint64_t>(durMicroSecs)); 1848af5d6058SSantosh Puranik } 1849af5d6058SSantosh Puranik else 1850af5d6058SSantosh Puranik { 1851af5d6058SSantosh Puranik messages::propertyValueFormatError(aResp->res, datetime, 1852af5d6058SSantosh Puranik "DateTime"); 1853af5d6058SSantosh Puranik return; 1854af5d6058SSantosh Puranik } 185583ff9ab6SJames Feist } 18569c310685SBorawski.Lukasz 18570f74e643SEd Tanous std::string uuid; 18589c310685SBorawski.Lukasz }; 18599c310685SBorawski.Lukasz 18601abe55efSEd Tanous class ManagerCollection : public Node 18611abe55efSEd Tanous { 18629c310685SBorawski.Lukasz public: 18631abe55efSEd Tanous ManagerCollection(CrowApp& app) : Node(app, "/redfish/v1/Managers/") 18641abe55efSEd Tanous { 1865a434f2bdSEd Tanous entityPrivileges = { 1866a434f2bdSEd Tanous {boost::beast::http::verb::get, {{"Login"}}}, 1867e0d918bcSEd Tanous {boost::beast::http::verb::head, {{"Login"}}}, 1868e0d918bcSEd Tanous {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, 1869e0d918bcSEd Tanous {boost::beast::http::verb::put, {{"ConfigureManager"}}}, 1870e0d918bcSEd Tanous {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, 1871e0d918bcSEd Tanous {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; 18729c310685SBorawski.Lukasz } 18739c310685SBorawski.Lukasz 18749c310685SBorawski.Lukasz private: 187555c7b7a2SEd Tanous void doGet(crow::Response& res, const crow::Request& req, 18761abe55efSEd Tanous const std::vector<std::string>& params) override 18771abe55efSEd Tanous { 187883ff9ab6SJames Feist // Collections don't include the static data added by SubRoute 187983ff9ab6SJames Feist // because it has a duplicate entry for members 188055c7b7a2SEd Tanous res.jsonValue["@odata.id"] = "/redfish/v1/Managers"; 188155c7b7a2SEd Tanous res.jsonValue["@odata.type"] = "#ManagerCollection.ManagerCollection"; 188255c7b7a2SEd Tanous res.jsonValue["Name"] = "Manager Collection"; 188355c7b7a2SEd Tanous res.jsonValue["Members@odata.count"] = 1; 188455c7b7a2SEd Tanous res.jsonValue["Members"] = { 18855b4aa86bSJames Feist {{"@odata.id", "/redfish/v1/Managers/bmc"}}}; 18869c310685SBorawski.Lukasz res.end(); 18879c310685SBorawski.Lukasz } 18889c310685SBorawski.Lukasz }; 18899c310685SBorawski.Lukasz } // namespace redfish 1890