xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision c1a75ebc267a78853fb26a3da8c6b3388e6ee07c)
140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
340e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
49c310685SBorawski.Lukasz #pragma once
59c310685SBorawski.Lukasz 
613451e39SWilly Tu #include "bmcweb_config.h"
713451e39SWilly Tu 
8a51fc2d2SSui Chen #include "app.hpp"
9d7857201SEd Tanous #include "async_resp.hpp"
10d7857201SEd Tanous #include "dbus_singleton.hpp"
11a51fc2d2SSui Chen #include "dbus_utility.hpp"
12d7857201SEd Tanous #include "error_messages.hpp"
13539d8c6bSEd Tanous #include "generated/enums/action_info.hpp"
14539d8c6bSEd Tanous #include "generated/enums/manager.hpp"
15539d8c6bSEd Tanous #include "generated/enums/resource.hpp"
16d7857201SEd Tanous #include "http_request.hpp"
17d7857201SEd Tanous #include "logging.hpp"
18cc67d0a0Srohitpai #include "openbmc/openbmc_managers.hpp"
19d7857201SEd Tanous #include "persistent_data.hpp"
20a51fc2d2SSui Chen #include "query.hpp"
21*c1a75ebcSrohitpai #include "redfish.hpp"
22c5d03ff4SJennifer Lee #include "redfish_util.hpp"
23a51fc2d2SSui Chen #include "registries/privilege_registry.hpp"
24fac6e53bSKrzysztof Grobelny #include "utils/dbus_utils.hpp"
253ccb3adbSEd Tanous #include "utils/json_utils.hpp"
26a51fc2d2SSui Chen #include "utils/sw_utils.hpp"
27a51fc2d2SSui Chen #include "utils/systemd_utils.hpp"
282b82937eSEd Tanous #include "utils/time_utils.hpp"
299c310685SBorawski.Lukasz 
30d7857201SEd Tanous #include <systemd/sd-bus.h>
31d7857201SEd Tanous 
32d7857201SEd Tanous #include <boost/beast/http/status.hpp>
33d7857201SEd Tanous #include <boost/beast/http/verb.hpp>
34e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
35ef4c65b7SEd Tanous #include <boost/url/format.hpp>
36d7857201SEd Tanous #include <boost/url/url.hpp>
37d7857201SEd Tanous #include <nlohmann/json.hpp>
38fac6e53bSKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
39d7857201SEd Tanous #include <sdbusplus/message.hpp>
40d7857201SEd Tanous #include <sdbusplus/message/native_types.hpp>
41fac6e53bSKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
421214b7e7SGunnar Mills 
43e99073f5SGeorge Liu #include <array>
44d7857201SEd Tanous #include <cstddef>
454bfefa74SGunnar Mills #include <cstdint>
46d7857201SEd Tanous #include <format>
47d7857201SEd Tanous #include <functional>
48d7857201SEd Tanous #include <map>
491214b7e7SGunnar Mills #include <memory>
509970e93fSKonstantin Aladyshev #include <optional>
513544d2a7SEd Tanous #include <ranges>
529970e93fSKonstantin Aladyshev #include <string>
53e99073f5SGeorge Liu #include <string_view>
54d7857201SEd Tanous #include <utility>
55d7857201SEd Tanous #include <vector>
565b4aa86bSJames Feist 
571abe55efSEd Tanous namespace redfish
581abe55efSEd Tanous {
59ed5befbdSJennifer Lee 
60d27c31e9SJagpal Singh Gill inline std::string getBMCUpdateServiceName()
61d27c31e9SJagpal Singh Gill {
62d27c31e9SJagpal Singh Gill     if constexpr (BMCWEB_REDFISH_UPDATESERVICE_USE_DBUS)
63d27c31e9SJagpal Singh Gill     {
64d27c31e9SJagpal Singh Gill         return "xyz.openbmc_project.Software.Manager";
65d27c31e9SJagpal Singh Gill     }
66d27c31e9SJagpal Singh Gill     return "xyz.openbmc_project.Software.BMC.Updater";
67d27c31e9SJagpal Singh Gill }
68d27c31e9SJagpal Singh Gill 
69d27c31e9SJagpal Singh Gill inline std::string getBMCUpdateServicePath()
70d27c31e9SJagpal Singh Gill {
71d27c31e9SJagpal Singh Gill     if constexpr (BMCWEB_REDFISH_UPDATESERVICE_USE_DBUS)
72d27c31e9SJagpal Singh Gill     {
73d27c31e9SJagpal Singh Gill         return "/xyz/openbmc_project/software/bmc";
74d27c31e9SJagpal Singh Gill     }
75d27c31e9SJagpal Singh Gill     return "/xyz/openbmc_project/software";
76d27c31e9SJagpal Singh Gill }
77d27c31e9SJagpal Singh Gill 
78ed5befbdSJennifer Lee /**
792a5c4407SGunnar Mills  * Function reboots the BMC.
802a5c4407SGunnar Mills  *
812a5c4407SGunnar Mills  * @param[in] asyncResp - Shared pointer for completing asynchronous calls
82ed5befbdSJennifer Lee  */
83504af5a0SPatrick Williams inline void doBMCGracefulRestart(
84504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
85ed5befbdSJennifer Lee {
86ed5befbdSJennifer Lee     const char* processName = "xyz.openbmc_project.State.BMC";
87ed5befbdSJennifer Lee     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
88ed5befbdSJennifer Lee     const char* interfaceName = "xyz.openbmc_project.State.BMC";
89ed5befbdSJennifer Lee     const std::string& propertyValue =
90ed5befbdSJennifer Lee         "xyz.openbmc_project.State.BMC.Transition.Reboot";
91ed5befbdSJennifer Lee     const char* destProperty = "RequestedBMCTransition";
92ed5befbdSJennifer Lee 
93ed5befbdSJennifer Lee     // Create the D-Bus variant for D-Bus call.
949ae226faSGeorge Liu     sdbusplus::asio::setProperty(
959ae226faSGeorge Liu         *crow::connections::systemBus, processName, objectPath, interfaceName,
969ae226faSGeorge Liu         destProperty, propertyValue,
975e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
98ed5befbdSJennifer Lee             // Use "Set" method to set the property value.
99ed5befbdSJennifer Lee             if (ec)
100ed5befbdSJennifer Lee             {
10162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
102ed5befbdSJennifer Lee                 messages::internalError(asyncResp->res);
103ed5befbdSJennifer Lee                 return;
104ed5befbdSJennifer Lee             }
105ed5befbdSJennifer Lee 
106ed5befbdSJennifer Lee             messages::success(asyncResp->res);
1079ae226faSGeorge Liu         });
108ed5befbdSJennifer Lee }
1092a5c4407SGunnar Mills 
110504af5a0SPatrick Williams inline void doBMCForceRestart(
111504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
112f92af389SJayaprakash Mutyala {
113f92af389SJayaprakash Mutyala     const char* processName = "xyz.openbmc_project.State.BMC";
114f92af389SJayaprakash Mutyala     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
115f92af389SJayaprakash Mutyala     const char* interfaceName = "xyz.openbmc_project.State.BMC";
116f92af389SJayaprakash Mutyala     const std::string& propertyValue =
117f92af389SJayaprakash Mutyala         "xyz.openbmc_project.State.BMC.Transition.HardReboot";
118f92af389SJayaprakash Mutyala     const char* destProperty = "RequestedBMCTransition";
119f92af389SJayaprakash Mutyala 
120f92af389SJayaprakash Mutyala     // Create the D-Bus variant for D-Bus call.
1219ae226faSGeorge Liu     sdbusplus::asio::setProperty(
1229ae226faSGeorge Liu         *crow::connections::systemBus, processName, objectPath, interfaceName,
1239ae226faSGeorge Liu         destProperty, propertyValue,
1245e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
125f92af389SJayaprakash Mutyala             // Use "Set" method to set the property value.
126f92af389SJayaprakash Mutyala             if (ec)
127f92af389SJayaprakash Mutyala             {
12862598e31SEd Tanous                 BMCWEB_LOG_DEBUG("[Set] Bad D-Bus request error: {}", ec);
129f92af389SJayaprakash Mutyala                 messages::internalError(asyncResp->res);
130f92af389SJayaprakash Mutyala                 return;
131f92af389SJayaprakash Mutyala             }
132f92af389SJayaprakash Mutyala 
133f92af389SJayaprakash Mutyala             messages::success(asyncResp->res);
1349ae226faSGeorge Liu         });
135f92af389SJayaprakash Mutyala }
136f92af389SJayaprakash Mutyala 
1372a5c4407SGunnar Mills /**
1382a5c4407SGunnar Mills  * ManagerResetAction class supports the POST method for the Reset (reboot)
1392a5c4407SGunnar Mills  * action.
1402a5c4407SGunnar Mills  */
1417e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetAction(App& app)
1422a5c4407SGunnar Mills {
1432a5c4407SGunnar Mills     /**
1442a5c4407SGunnar Mills      * Function handles POST method request.
1452a5c4407SGunnar Mills      * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
146f92af389SJayaprakash Mutyala      * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
1472a5c4407SGunnar Mills      */
1487e860f15SJohn Edward Broadbent 
149253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/Actions/Manager.Reset/")
150ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
1517e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
15245ca1b86SEd Tanous             [&app](const crow::Request& req,
153253f11b8SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
154253f11b8SEd Tanous                    const std::string& managerId) {
1553ba00073SCarson Labrado                 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
15645ca1b86SEd Tanous                 {
15745ca1b86SEd Tanous                     return;
15845ca1b86SEd Tanous                 }
159253f11b8SEd Tanous                 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
160253f11b8SEd Tanous                 {
161bd79bce8SPatrick Williams                     messages::resourceNotFound(asyncResp->res, "Manager",
162bd79bce8SPatrick Williams                                                managerId);
163253f11b8SEd Tanous                     return;
164253f11b8SEd Tanous                 }
165253f11b8SEd Tanous 
16662598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Post Manager Reset.");
1672a5c4407SGunnar Mills 
1682a5c4407SGunnar Mills                 std::string resetType;
1692a5c4407SGunnar Mills 
17015ed6780SWilly Tu                 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
1717e860f15SJohn Edward Broadbent                                                resetType))
1722a5c4407SGunnar Mills                 {
1732a5c4407SGunnar Mills                     return;
1742a5c4407SGunnar Mills                 }
1752a5c4407SGunnar Mills 
176f92af389SJayaprakash Mutyala                 if (resetType == "GracefulRestart")
177f92af389SJayaprakash Mutyala                 {
17862598e31SEd Tanous                     BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
179f92af389SJayaprakash Mutyala                     doBMCGracefulRestart(asyncResp);
180f92af389SJayaprakash Mutyala                     return;
181f92af389SJayaprakash Mutyala                 }
1823174e4dfSEd Tanous                 if (resetType == "ForceRestart")
183f92af389SJayaprakash Mutyala                 {
18462598e31SEd Tanous                     BMCWEB_LOG_DEBUG("Proceeding with {}", resetType);
185f92af389SJayaprakash Mutyala                     doBMCForceRestart(asyncResp);
186f92af389SJayaprakash Mutyala                     return;
187f92af389SJayaprakash Mutyala                 }
188bd79bce8SPatrick Williams                 BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}",
189bd79bce8SPatrick Williams                                  resetType);
1902a5c4407SGunnar Mills                 messages::actionParameterNotSupported(asyncResp->res, resetType,
1912a5c4407SGunnar Mills                                                       "ResetType");
1922a5c4407SGunnar Mills 
1932a5c4407SGunnar Mills                 return;
1947e860f15SJohn Edward Broadbent             });
1952a5c4407SGunnar Mills }
196ed5befbdSJennifer Lee 
1973e40fc74SGunnar Mills /**
1983e40fc74SGunnar Mills  * ManagerResetToDefaultsAction class supports POST method for factory reset
1993e40fc74SGunnar Mills  * action.
2003e40fc74SGunnar Mills  */
2017e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetToDefaultsAction(App& app)
2023e40fc74SGunnar Mills {
2033e40fc74SGunnar Mills     /**
2043e40fc74SGunnar Mills      * Function handles ResetToDefaults POST method request.
2053e40fc74SGunnar Mills      *
2063e40fc74SGunnar Mills      * Analyzes POST body message and factory resets BMC by calling
2073e40fc74SGunnar Mills      * BMC code updater factory reset followed by a BMC reboot.
2083e40fc74SGunnar Mills      *
2093e40fc74SGunnar Mills      * BMC code updater factory reset wipes the whole BMC read-write
2103e40fc74SGunnar Mills      * filesystem which includes things like the network settings.
2113e40fc74SGunnar Mills      *
2123e40fc74SGunnar Mills      * OpenBMC only supports ResetToDefaultsType "ResetAll".
2133e40fc74SGunnar Mills      */
2147e860f15SJohn Edward Broadbent 
2157e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
216253f11b8SEd Tanous                  "/redfish/v1/Managers/<str>/Actions/Manager.ResetToDefaults/")
217ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
218bd79bce8SPatrick Williams         .methods(
219bd79bce8SPatrick Williams             boost::beast::http::verb::
220bd79bce8SPatrick Williams                 post)([&app](
221bd79bce8SPatrick Williams                           const crow::Request& req,
222253f11b8SEd Tanous                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
223253f11b8SEd Tanous                           const std::string& managerId) {
2243ba00073SCarson Labrado             if (!redfish::setUpRedfishRoute(app, req, asyncResp))
22545ca1b86SEd Tanous             {
22645ca1b86SEd Tanous                 return;
22745ca1b86SEd Tanous             }
228253f11b8SEd Tanous 
229253f11b8SEd Tanous             if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
230253f11b8SEd Tanous             {
231bd79bce8SPatrick Williams                 messages::resourceNotFound(asyncResp->res, "Manager",
232bd79bce8SPatrick Williams                                            managerId);
233253f11b8SEd Tanous                 return;
234253f11b8SEd Tanous             }
235253f11b8SEd Tanous 
23662598e31SEd Tanous             BMCWEB_LOG_DEBUG("Post ResetToDefaults.");
2373e40fc74SGunnar Mills 
2389970e93fSKonstantin Aladyshev             std::optional<std::string> resetType;
2399970e93fSKonstantin Aladyshev             std::optional<std::string> resetToDefaultsType;
2403e40fc74SGunnar Mills 
241afc474aeSMyung Bae             if (!json_util::readJsonAction(                     //
242afc474aeSMyung Bae                     req, asyncResp->res,                        //
243afc474aeSMyung Bae                     "ResetToDefaultsType", resetToDefaultsType, //
244afc474aeSMyung Bae                     "ResetType", resetType                      //
245afc474aeSMyung Bae                     ))
2463e40fc74SGunnar Mills             {
2479970e93fSKonstantin Aladyshev                 BMCWEB_LOG_DEBUG("Missing property ResetType.");
2483e40fc74SGunnar Mills 
249bd79bce8SPatrick Williams                 messages::actionParameterMissing(
250bd79bce8SPatrick Williams                     asyncResp->res, "ResetToDefaults", "ResetType");
2513e40fc74SGunnar Mills                 return;
2523e40fc74SGunnar Mills             }
2533e40fc74SGunnar Mills 
2549970e93fSKonstantin Aladyshev             if (resetToDefaultsType && !resetType)
2559970e93fSKonstantin Aladyshev             {
2569970e93fSKonstantin Aladyshev                 BMCWEB_LOG_WARNING(
2579970e93fSKonstantin Aladyshev                     "Using deprecated ResetToDefaultsType, should be ResetType."
2589970e93fSKonstantin Aladyshev                     "Support for the ResetToDefaultsType will be dropped in 2Q24");
2599970e93fSKonstantin Aladyshev                 resetType = resetToDefaultsType;
2609970e93fSKonstantin Aladyshev             }
2619970e93fSKonstantin Aladyshev 
2623e40fc74SGunnar Mills             if (resetType != "ResetAll")
2633e40fc74SGunnar Mills             {
2649970e93fSKonstantin Aladyshev                 BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}",
2659970e93fSKonstantin Aladyshev                                  *resetType);
266bd79bce8SPatrick Williams                 messages::actionParameterNotSupported(asyncResp->res,
267bd79bce8SPatrick Williams                                                       *resetType, "ResetType");
2683e40fc74SGunnar Mills                 return;
2693e40fc74SGunnar Mills             }
2703e40fc74SGunnar Mills 
2713e40fc74SGunnar Mills             crow::connections::systemBus->async_method_call(
2725e7e2dc5SEd Tanous                 [asyncResp](const boost::system::error_code& ec) {
2733e40fc74SGunnar Mills                     if (ec)
2743e40fc74SGunnar Mills                     {
27562598e31SEd Tanous                         BMCWEB_LOG_DEBUG("Failed to ResetToDefaults: {}", ec);
2763e40fc74SGunnar Mills                         messages::internalError(asyncResp->res);
2773e40fc74SGunnar Mills                         return;
2783e40fc74SGunnar Mills                     }
2793e40fc74SGunnar Mills                     // Factory Reset doesn't actually happen until a reboot
2803e40fc74SGunnar Mills                     // Can't erase what the BMC is running on
2813e40fc74SGunnar Mills                     doBMCGracefulRestart(asyncResp);
2823e40fc74SGunnar Mills                 },
283d27c31e9SJagpal Singh Gill                 getBMCUpdateServiceName(), getBMCUpdateServicePath(),
2843e40fc74SGunnar Mills                 "xyz.openbmc_project.Common.FactoryReset", "Reset");
2857e860f15SJohn Edward Broadbent         });
2863e40fc74SGunnar Mills }
2873e40fc74SGunnar Mills 
2881cb1a9e6SAppaRao Puli /**
2891cb1a9e6SAppaRao Puli  * ManagerResetActionInfo derived class for delivering Manager
2901cb1a9e6SAppaRao Puli  * ResetType AllowableValues using ResetInfo schema.
2911cb1a9e6SAppaRao Puli  */
2927e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetActionInfo(App& app)
2931cb1a9e6SAppaRao Puli {
2941cb1a9e6SAppaRao Puli     /**
2951cb1a9e6SAppaRao Puli      * Functions triggers appropriate requests on DBus
2961cb1a9e6SAppaRao Puli      */
2977e860f15SJohn Edward Broadbent 
298253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/ResetActionInfo/")
299ed398213SEd Tanous         .privileges(redfish::privileges::getActionInfo)
3007e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
30145ca1b86SEd Tanous             [&app](const crow::Request& req,
302253f11b8SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
303253f11b8SEd Tanous                    const std::string& managerId) {
3043ba00073SCarson Labrado                 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
30545ca1b86SEd Tanous                 {
30645ca1b86SEd Tanous                     return;
30745ca1b86SEd Tanous                 }
3081476687dSEd Tanous 
309253f11b8SEd Tanous                 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
310253f11b8SEd Tanous                 {
311bd79bce8SPatrick Williams                     messages::resourceNotFound(asyncResp->res, "Manager",
312bd79bce8SPatrick Williams                                                managerId);
313253f11b8SEd Tanous                     return;
314253f11b8SEd Tanous                 }
315253f11b8SEd Tanous 
3161476687dSEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
3171476687dSEd Tanous                     "#ActionInfo.v1_1_2.ActionInfo";
318bd79bce8SPatrick Williams                 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
319bd79bce8SPatrick Williams                     "/redfish/v1/Managers/{}/ResetActionInfo",
320253f11b8SEd Tanous                     BMCWEB_REDFISH_MANAGER_URI_NAME);
3211476687dSEd Tanous                 asyncResp->res.jsonValue["Name"] = "Reset Action Info";
3221476687dSEd Tanous                 asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
3231476687dSEd Tanous                 nlohmann::json::object_t parameter;
3241476687dSEd Tanous                 parameter["Name"] = "ResetType";
3251476687dSEd Tanous                 parameter["Required"] = true;
326539d8c6bSEd Tanous                 parameter["DataType"] = action_info::ParameterTypes::String;
3271476687dSEd Tanous 
3281476687dSEd Tanous                 nlohmann::json::array_t allowableValues;
329ad539545SPatrick Williams                 allowableValues.emplace_back("GracefulRestart");
330ad539545SPatrick Williams                 allowableValues.emplace_back("ForceRestart");
3311476687dSEd Tanous                 parameter["AllowableValues"] = std::move(allowableValues);
3321476687dSEd Tanous 
3331476687dSEd Tanous                 nlohmann::json::array_t parameters;
334ad539545SPatrick Williams                 parameters.emplace_back(std::move(parameter));
3351476687dSEd Tanous 
3361476687dSEd Tanous                 asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
3377e860f15SJohn Edward Broadbent             });
3381cb1a9e6SAppaRao Puli }
3391cb1a9e6SAppaRao Puli 
340071d8fdfSSunnySrivastava1984 /**
341071d8fdfSSunnySrivastava1984  * @brief Retrieves BMC manager location data over DBus
342071d8fdfSSunnySrivastava1984  *
343ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for completing asynchronous calls
344071d8fdfSSunnySrivastava1984  * @param[in] connectionName - service name
345071d8fdfSSunnySrivastava1984  * @param[in] path - object path
346071d8fdfSSunnySrivastava1984  * @return none
347071d8fdfSSunnySrivastava1984  */
348ac106bf6SEd Tanous inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
349071d8fdfSSunnySrivastava1984                         const std::string& connectionName,
350071d8fdfSSunnySrivastava1984                         const std::string& path)
351071d8fdfSSunnySrivastava1984 {
35262598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get BMC manager Location data.");
353071d8fdfSSunnySrivastava1984 
354deae6a78SEd Tanous     dbus::utility::getProperty<std::string>(
355deae6a78SEd Tanous         connectionName, path,
3561e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
357ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
3581e1e598dSJonathan Doman                     const std::string& property) {
359071d8fdfSSunnySrivastava1984             if (ec)
360071d8fdfSSunnySrivastava1984             {
36162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("DBUS response error for "
36262598e31SEd Tanous                                  "Location");
363ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
364071d8fdfSSunnySrivastava1984                 return;
365071d8fdfSSunnySrivastava1984             }
366071d8fdfSSunnySrivastava1984 
367bd79bce8SPatrick Williams             asyncResp->res
368bd79bce8SPatrick Williams                 .jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
3691e1e598dSJonathan Doman                 property;
3701e1e598dSJonathan Doman         });
371071d8fdfSSunnySrivastava1984 }
3727e860f15SJohn Edward Broadbent // avoid name collision systems.hpp
373504af5a0SPatrick Williams inline void managerGetLastResetTime(
374504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
3754bf2b033SGunnar Mills {
37662598e31SEd Tanous     BMCWEB_LOG_DEBUG("Getting Manager Last Reset Time");
3774bf2b033SGunnar Mills 
378deae6a78SEd Tanous     dbus::utility::getProperty<uint64_t>(
379deae6a78SEd Tanous         "xyz.openbmc_project.State.BMC", "/xyz/openbmc_project/state/bmc0",
380deae6a78SEd Tanous         "xyz.openbmc_project.State.BMC", "LastRebootTime",
381ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
3821e1e598dSJonathan Doman                     const uint64_t lastResetTime) {
3834bf2b033SGunnar Mills             if (ec)
3844bf2b033SGunnar Mills             {
38562598e31SEd Tanous                 BMCWEB_LOG_DEBUG("D-BUS response error {}", ec);
3864bf2b033SGunnar Mills                 return;
3874bf2b033SGunnar Mills             }
3884bf2b033SGunnar Mills 
3894bf2b033SGunnar Mills             // LastRebootTime is epoch time, in milliseconds
3904bf2b033SGunnar Mills             // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
3911e1e598dSJonathan Doman             uint64_t lastResetTimeStamp = lastResetTime / 1000;
3924bf2b033SGunnar Mills 
3934bf2b033SGunnar Mills             // Convert to ISO 8601 standard
394ac106bf6SEd Tanous             asyncResp->res.jsonValue["LastResetTime"] =
3952b82937eSEd Tanous                 redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
3961e1e598dSJonathan Doman         });
3974bf2b033SGunnar Mills }
3984bf2b033SGunnar Mills 
3994bfefa74SGunnar Mills /**
4004bfefa74SGunnar Mills  * @brief Set the running firmware image
4014bfefa74SGunnar Mills  *
402ac106bf6SEd Tanous  * @param[i,o] asyncResp - Async response object
4034bfefa74SGunnar Mills  * @param[i] runningFirmwareTarget - Image to make the running image
4044bfefa74SGunnar Mills  *
4054bfefa74SGunnar Mills  * @return void
4064bfefa74SGunnar Mills  */
407504af5a0SPatrick Williams inline void setActiveFirmwareImage(
408504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
409f23b7296SEd Tanous     const std::string& runningFirmwareTarget)
4104bfefa74SGunnar Mills {
4114bfefa74SGunnar Mills     // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
412f23b7296SEd Tanous     std::string::size_type idPos = runningFirmwareTarget.rfind('/');
4134bfefa74SGunnar Mills     if (idPos == std::string::npos)
4144bfefa74SGunnar Mills     {
415ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
4164bfefa74SGunnar Mills                                          "@odata.id");
41762598e31SEd Tanous         BMCWEB_LOG_DEBUG("Can't parse firmware ID!");
4184bfefa74SGunnar Mills         return;
4194bfefa74SGunnar Mills     }
4204bfefa74SGunnar Mills     idPos++;
4214bfefa74SGunnar Mills     if (idPos >= runningFirmwareTarget.size())
4224bfefa74SGunnar Mills     {
423ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
4244bfefa74SGunnar Mills                                          "@odata.id");
42562598e31SEd Tanous         BMCWEB_LOG_DEBUG("Invalid firmware ID.");
4264bfefa74SGunnar Mills         return;
4274bfefa74SGunnar Mills     }
4284bfefa74SGunnar Mills     std::string firmwareId = runningFirmwareTarget.substr(idPos);
4294bfefa74SGunnar Mills 
4304bfefa74SGunnar Mills     // Make sure the image is valid before setting priority
4315eb468daSGeorge Liu     sdbusplus::message::object_path objPath("/xyz/openbmc_project/software");
4325eb468daSGeorge Liu     dbus::utility::getManagedObjects(
433d27c31e9SJagpal Singh Gill         getBMCUpdateServiceName(), objPath,
4345eb468daSGeorge Liu         [asyncResp, firmwareId, runningFirmwareTarget](
4355eb468daSGeorge Liu             const boost::system::error_code& ec,
4365eb468daSGeorge Liu             const dbus::utility::ManagedObjectType& subtree) {
4374bfefa74SGunnar Mills             if (ec)
4384bfefa74SGunnar Mills             {
43962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("D-Bus response error getting objects.");
440ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
4414bfefa74SGunnar Mills                 return;
4424bfefa74SGunnar Mills             }
4434bfefa74SGunnar Mills 
44426f6976fSEd Tanous             if (subtree.empty())
4454bfefa74SGunnar Mills             {
44662598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Can't find image!");
447ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
4484bfefa74SGunnar Mills                 return;
4494bfefa74SGunnar Mills             }
4504bfefa74SGunnar Mills 
4514bfefa74SGunnar Mills             bool foundImage = false;
45202cad96eSEd Tanous             for (const auto& object : subtree)
4534bfefa74SGunnar Mills             {
4544bfefa74SGunnar Mills                 const std::string& path =
4554bfefa74SGunnar Mills                     static_cast<const std::string&>(object.first);
456f23b7296SEd Tanous                 std::size_t idPos2 = path.rfind('/');
4574bfefa74SGunnar Mills 
4584bfefa74SGunnar Mills                 if (idPos2 == std::string::npos)
4594bfefa74SGunnar Mills                 {
4604bfefa74SGunnar Mills                     continue;
4614bfefa74SGunnar Mills                 }
4624bfefa74SGunnar Mills 
4634bfefa74SGunnar Mills                 idPos2++;
4644bfefa74SGunnar Mills                 if (idPos2 >= path.size())
4654bfefa74SGunnar Mills                 {
4664bfefa74SGunnar Mills                     continue;
4674bfefa74SGunnar Mills                 }
4684bfefa74SGunnar Mills 
4694bfefa74SGunnar Mills                 if (path.substr(idPos2) == firmwareId)
4704bfefa74SGunnar Mills                 {
4714bfefa74SGunnar Mills                     foundImage = true;
4724bfefa74SGunnar Mills                     break;
4734bfefa74SGunnar Mills                 }
4744bfefa74SGunnar Mills             }
4754bfefa74SGunnar Mills 
4764bfefa74SGunnar Mills             if (!foundImage)
4774bfefa74SGunnar Mills             {
478ac106bf6SEd Tanous                 messages::propertyValueNotInList(
479ac106bf6SEd Tanous                     asyncResp->res, runningFirmwareTarget, "@odata.id");
48062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Invalid firmware ID.");
4814bfefa74SGunnar Mills                 return;
4824bfefa74SGunnar Mills             }
4834bfefa74SGunnar Mills 
48462598e31SEd Tanous             BMCWEB_LOG_DEBUG("Setting firmware version {} to priority 0.",
48562598e31SEd Tanous                              firmwareId);
4864bfefa74SGunnar Mills 
4874bfefa74SGunnar Mills             // Only support Immediate
4884bfefa74SGunnar Mills             // An addition could be a Redfish Setting like
4894bfefa74SGunnar Mills             // ActiveSoftwareImageApplyTime and support OnReset
4909ae226faSGeorge Liu             sdbusplus::asio::setProperty(
491d27c31e9SJagpal Singh Gill                 *crow::connections::systemBus, getBMCUpdateServiceName(),
4929ae226faSGeorge Liu                 "/xyz/openbmc_project/software/" + firmwareId,
4939ae226faSGeorge Liu                 "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
4949ae226faSGeorge Liu                 static_cast<uint8_t>(0),
495ac106bf6SEd Tanous                 [asyncResp](const boost::system::error_code& ec2) {
4968a592810SEd Tanous                     if (ec2)
4974bfefa74SGunnar Mills                     {
49862598e31SEd Tanous                         BMCWEB_LOG_DEBUG("D-Bus response error setting.");
499ac106bf6SEd Tanous                         messages::internalError(asyncResp->res);
5004bfefa74SGunnar Mills                         return;
5014bfefa74SGunnar Mills                     }
502ac106bf6SEd Tanous                     doBMCGracefulRestart(asyncResp);
5039ae226faSGeorge Liu                 });
5045eb468daSGeorge Liu         });
5054bfefa74SGunnar Mills }
5064bfefa74SGunnar Mills 
507bd79bce8SPatrick Williams inline void afterSetDateTime(
508bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
509bd79bce8SPatrick Williams     const boost::system::error_code& ec, const sdbusplus::message_t& msg)
510c51afd54SEd Tanous {
511c51afd54SEd Tanous     if (ec)
512c51afd54SEd Tanous     {
513c51afd54SEd Tanous         BMCWEB_LOG_DEBUG("Failed to set elapsed time. DBUS response error {}",
514c51afd54SEd Tanous                          ec);
515c51afd54SEd Tanous         const sd_bus_error* dbusError = msg.get_error();
516c51afd54SEd Tanous         if (dbusError != nullptr)
517c51afd54SEd Tanous         {
518c51afd54SEd Tanous             std::string_view errorName(dbusError->name);
519c51afd54SEd Tanous             if (errorName ==
520c51afd54SEd Tanous                 "org.freedesktop.timedate1.AutomaticTimeSyncEnabled")
521c51afd54SEd Tanous             {
522c51afd54SEd Tanous                 BMCWEB_LOG_DEBUG("Setting conflict");
523c51afd54SEd Tanous                 messages::propertyValueConflict(
524c51afd54SEd Tanous                     asyncResp->res, "DateTime",
525c51afd54SEd Tanous                     "Managers/NetworkProtocol/NTPProcotolEnabled");
526c51afd54SEd Tanous                 return;
527c51afd54SEd Tanous             }
528c51afd54SEd Tanous         }
529c51afd54SEd Tanous         messages::internalError(asyncResp->res);
530c51afd54SEd Tanous         return;
531c51afd54SEd Tanous     }
532c51afd54SEd Tanous     asyncResp->res.result(boost::beast::http::status::no_content);
533c51afd54SEd Tanous }
534c51afd54SEd Tanous 
535c51afd54SEd Tanous inline void setDateTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
536c51afd54SEd Tanous                         const std::string& datetime)
537af5d6058SSantosh Puranik {
53862598e31SEd Tanous     BMCWEB_LOG_DEBUG("Set date time: {}", datetime);
539af5d6058SSantosh Puranik 
540c2e32007SEd Tanous     std::optional<redfish::time_utils::usSinceEpoch> us =
541c2e32007SEd Tanous         redfish::time_utils::dateStringToEpoch(datetime);
542c2e32007SEd Tanous     if (!us)
543af5d6058SSantosh Puranik     {
544ac106bf6SEd Tanous         messages::propertyValueFormatError(asyncResp->res, datetime,
545ac106bf6SEd Tanous                                            "DateTime");
546c2e32007SEd Tanous         return;
547c2e32007SEd Tanous     }
548c51afd54SEd Tanous     // Set the absolute datetime
549c51afd54SEd Tanous     bool relative = false;
550c51afd54SEd Tanous     bool interactive = false;
551c51afd54SEd Tanous     crow::connections::systemBus->async_method_call(
552c51afd54SEd Tanous         [asyncResp](const boost::system::error_code& ec,
553c51afd54SEd Tanous                     const sdbusplus::message_t& msg) {
554c51afd54SEd Tanous             afterSetDateTime(asyncResp, ec, msg);
555c51afd54SEd Tanous         },
556c51afd54SEd Tanous         "org.freedesktop.timedate1", "/org/freedesktop/timedate1",
557c51afd54SEd Tanous         "org.freedesktop.timedate1", "SetTime", us->count(), relative,
558c51afd54SEd Tanous         interactive);
55983ff9ab6SJames Feist }
5609c310685SBorawski.Lukasz 
561504af5a0SPatrick Williams inline void checkForQuiesced(
562504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
56375815e5cSEd Tanous {
564deae6a78SEd Tanous     dbus::utility::getProperty<std::string>(
565deae6a78SEd Tanous         "org.freedesktop.systemd1",
56675815e5cSEd Tanous         "/org/freedesktop/systemd1/unit/obmc-bmc-service-quiesce@0.target",
56775815e5cSEd Tanous         "org.freedesktop.systemd1.Unit", "ActiveState",
56875815e5cSEd Tanous         [asyncResp](const boost::system::error_code& ec,
56975815e5cSEd Tanous                     const std::string& val) {
57075815e5cSEd Tanous             if (!ec)
57175815e5cSEd Tanous             {
57275815e5cSEd Tanous                 if (val == "active")
57375815e5cSEd Tanous                 {
574539d8c6bSEd Tanous                     asyncResp->res.jsonValue["Status"]["Health"] =
575539d8c6bSEd Tanous                         resource::Health::Critical;
576539d8c6bSEd Tanous                     asyncResp->res.jsonValue["Status"]["State"] =
577539d8c6bSEd Tanous                         resource::State::Quiesced;
57875815e5cSEd Tanous                     return;
57975815e5cSEd Tanous                 }
58075815e5cSEd Tanous             }
581539d8c6bSEd Tanous             asyncResp->res.jsonValue["Status"]["Health"] = resource::Health::OK;
582bd79bce8SPatrick Williams             asyncResp->res.jsonValue["Status"]["State"] =
583bd79bce8SPatrick Williams                 resource::State::Enabled;
58475815e5cSEd Tanous         });
58575815e5cSEd Tanous }
58675815e5cSEd Tanous 
5877e860f15SJohn Edward Broadbent inline void requestRoutesManager(App& app)
5887e860f15SJohn Edward Broadbent {
5897e860f15SJohn Edward Broadbent     std::string uuid = persistent_data::getConfig().systemUuid;
5909c310685SBorawski.Lukasz 
591253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/")
592ed398213SEd Tanous         .privileges(redfish::privileges::getManager)
593bd79bce8SPatrick Williams         .methods(
594bd79bce8SPatrick Williams             boost::beast::http::verb::
595bd79bce8SPatrick Williams                 get)([&app,
596bd79bce8SPatrick Williams                       uuid](const crow::Request& req,
597253f11b8SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
598253f11b8SEd Tanous                             const std::string& managerId) {
5993ba00073SCarson Labrado             if (!redfish::setUpRedfishRoute(app, req, asyncResp))
60045ca1b86SEd Tanous             {
60145ca1b86SEd Tanous                 return;
60245ca1b86SEd Tanous             }
603253f11b8SEd Tanous 
604253f11b8SEd Tanous             if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
605253f11b8SEd Tanous             {
606bd79bce8SPatrick Williams                 messages::resourceNotFound(asyncResp->res, "Manager",
607bd79bce8SPatrick Williams                                            managerId);
608253f11b8SEd Tanous                 return;
609253f11b8SEd Tanous             }
610253f11b8SEd Tanous 
611253f11b8SEd Tanous             asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
612253f11b8SEd Tanous                 "/redfish/v1/Managers/{}", BMCWEB_REDFISH_MANAGER_URI_NAME);
613bd79bce8SPatrick Williams             asyncResp->res.jsonValue["@odata.type"] =
614bd79bce8SPatrick Williams                 "#Manager.v1_14_0.Manager";
615253f11b8SEd Tanous             asyncResp->res.jsonValue["Id"] = BMCWEB_REDFISH_MANAGER_URI_NAME;
6167e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
6177e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Description"] =
6187e860f15SJohn Edward Broadbent                 "Baseboard Management Controller";
619539d8c6bSEd Tanous             asyncResp->res.jsonValue["PowerState"] = resource::PowerState::On;
6201476687dSEd Tanous 
621539d8c6bSEd Tanous             asyncResp->res.jsonValue["ManagerType"] = manager::ManagerType::BMC;
6227e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
6237e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
624bd79bce8SPatrick Williams             asyncResp->res.jsonValue["Model"] =
625bd79bce8SPatrick Williams                 "OpenBmc"; // TODO(ed), get model
6267e860f15SJohn Edward Broadbent 
6271476687dSEd Tanous             asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
628253f11b8SEd Tanous                 boost::urls::format("/redfish/v1/Managers/{}/LogServices",
629253f11b8SEd Tanous                                     BMCWEB_REDFISH_MANAGER_URI_NAME);
6301476687dSEd Tanous             asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
631253f11b8SEd Tanous                 boost::urls::format("/redfish/v1/Managers/{}/NetworkProtocol",
632253f11b8SEd Tanous                                     BMCWEB_REDFISH_MANAGER_URI_NAME);
6331476687dSEd Tanous             asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
634bd79bce8SPatrick Williams                 boost::urls::format(
635bd79bce8SPatrick Williams                     "/redfish/v1/Managers/{}/EthernetInterfaces",
636253f11b8SEd Tanous                     BMCWEB_REDFISH_MANAGER_URI_NAME);
6377e860f15SJohn Edward Broadbent 
63825b54dbaSEd Tanous             if constexpr (BMCWEB_VM_NBDPROXY)
63936c0f2a3SEd Tanous             {
6401476687dSEd Tanous                 asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
641253f11b8SEd Tanous                     boost::urls::format("/redfish/v1/Managers/{}/VirtualMedia",
642253f11b8SEd Tanous                                         BMCWEB_REDFISH_MANAGER_URI_NAME);
64336c0f2a3SEd Tanous             }
6447e860f15SJohn Edward Broadbent 
6457e860f15SJohn Edward Broadbent             // Manager.Reset (an action) can be many values, OpenBMC only
6467e860f15SJohn Edward Broadbent             // supports BMC reboot.
6477e860f15SJohn Edward Broadbent             nlohmann::json& managerReset =
6487e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
649bd79bce8SPatrick Williams             managerReset["target"] = boost::urls::format(
650bd79bce8SPatrick Williams                 "/redfish/v1/Managers/{}/Actions/Manager.Reset",
651253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME);
6527e860f15SJohn Edward Broadbent             managerReset["@Redfish.ActionInfo"] =
653253f11b8SEd Tanous                 boost::urls::format("/redfish/v1/Managers/{}/ResetActionInfo",
654253f11b8SEd Tanous                                     BMCWEB_REDFISH_MANAGER_URI_NAME);
6557e860f15SJohn Edward Broadbent 
6567e860f15SJohn Edward Broadbent             // ResetToDefaults (Factory Reset) has values like
6577e860f15SJohn Edward Broadbent             // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
6587e860f15SJohn Edward Broadbent             // on OpenBMC
6597e860f15SJohn Edward Broadbent             nlohmann::json& resetToDefaults =
6607e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
661253f11b8SEd Tanous             resetToDefaults["target"] = boost::urls::format(
662253f11b8SEd Tanous                 "/redfish/v1/Managers/{}/Actions/Manager.ResetToDefaults",
663253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME);
664613dabeaSEd Tanous             resetToDefaults["ResetType@Redfish.AllowableValues"] =
665613dabeaSEd Tanous                 nlohmann::json::array_t({"ResetAll"});
6667e860f15SJohn Edward Broadbent 
6677c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
6682b82937eSEd Tanous                 redfish::time_utils::getDateTimeOffsetNow();
6697c8c4058STejas Patil 
6707c8c4058STejas Patil             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
6717c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
6727c8c4058STejas Patil                 redfishDateTimeOffset.second;
6737e860f15SJohn Edward Broadbent 
67425b54dbaSEd Tanous             if constexpr (BMCWEB_KVM)
67525b54dbaSEd Tanous             {
6767e860f15SJohn Edward Broadbent                 // Fill in GraphicalConsole info
67725b54dbaSEd Tanous                 asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] =
67825b54dbaSEd Tanous                     true;
67925b54dbaSEd Tanous                 asyncResp->res
68025b54dbaSEd Tanous                     .jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4;
68125b54dbaSEd Tanous                 asyncResp->res
68225b54dbaSEd Tanous                     .jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
683613dabeaSEd Tanous                     nlohmann::json::array_t({"KVMIP"});
68425b54dbaSEd Tanous             }
68525b54dbaSEd Tanous             if constexpr (!BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
6867f3e84a1SEd Tanous             {
687bd79bce8SPatrick Williams                 asyncResp->res
688bd79bce8SPatrick Williams                     .jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
6891476687dSEd Tanous 
6901476687dSEd Tanous                 nlohmann::json::array_t managerForServers;
6911476687dSEd Tanous                 nlohmann::json::object_t manager;
692bd79bce8SPatrick Williams                 manager["@odata.id"] = std::format(
693bd79bce8SPatrick Williams                     "/redfish/v1/Systems/{}", BMCWEB_REDFISH_SYSTEM_URI_NAME);
694ad539545SPatrick Williams                 managerForServers.emplace_back(std::move(manager));
6951476687dSEd Tanous 
6961476687dSEd Tanous                 asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
6971476687dSEd Tanous                     std::move(managerForServers);
6987f3e84a1SEd Tanous             }
6997e860f15SJohn Edward Broadbent 
700eee0013eSWilly Tu             sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
7017e860f15SJohn Edward Broadbent                                                  "FirmwareVersion", true);
7027e860f15SJohn Edward Broadbent 
7037e860f15SJohn Edward Broadbent             managerGetLastResetTime(asyncResp);
7047e860f15SJohn Edward Broadbent 
705a51fc2d2SSui Chen             // ManagerDiagnosticData is added for all BMCs.
706a51fc2d2SSui Chen             nlohmann::json& managerDiagnosticData =
707a51fc2d2SSui Chen                 asyncResp->res.jsonValue["ManagerDiagnosticData"];
708bd79bce8SPatrick Williams             managerDiagnosticData["@odata.id"] = boost::urls::format(
709bd79bce8SPatrick Williams                 "/redfish/v1/Managers/{}/ManagerDiagnosticData",
710253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME);
711a51fc2d2SSui Chen 
712bd79bce8SPatrick Williams             getMainChassisId(asyncResp, [](const std::string& chassisId,
713bd79bce8SPatrick Williams                                            const std::shared_ptr<
714bd79bce8SPatrick Williams                                                bmcweb::AsyncResp>& aRsp) {
715bd79bce8SPatrick Williams                 aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] =
716bd79bce8SPatrick Williams                     1;
7171476687dSEd Tanous                 nlohmann::json::array_t managerForChassis;
7188a592810SEd Tanous                 nlohmann::json::object_t managerObj;
719ef4c65b7SEd Tanous                 boost::urls::url chassiUrl =
720ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
721eddfc437SWilly Tu                 managerObj["@odata.id"] = chassiUrl;
722ad539545SPatrick Williams                 managerForChassis.emplace_back(std::move(managerObj));
7231476687dSEd Tanous                 aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
7241476687dSEd Tanous                     std::move(managerForChassis);
7251476687dSEd Tanous                 aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
726eddfc437SWilly Tu                     chassiUrl;
7277e860f15SJohn Edward Broadbent             });
7287e860f15SJohn Edward Broadbent 
729deae6a78SEd Tanous             dbus::utility::getProperty<double>(
730deae6a78SEd Tanous                 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
731deae6a78SEd Tanous                 "org.freedesktop.systemd1.Manager", "Progress",
73275815e5cSEd Tanous                 [asyncResp](const boost::system::error_code& ec, double val) {
7337e860f15SJohn Edward Broadbent                     if (ec)
7341abe55efSEd Tanous                     {
73562598e31SEd Tanous                         BMCWEB_LOG_ERROR("Error while getting progress");
7367e860f15SJohn Edward Broadbent                         messages::internalError(asyncResp->res);
7377e860f15SJohn Edward Broadbent                         return;
7387e860f15SJohn Edward Broadbent                     }
7391e1e598dSJonathan Doman                     if (val < 1.0)
7407e860f15SJohn Edward Broadbent                     {
741539d8c6bSEd Tanous                         asyncResp->res.jsonValue["Status"]["Health"] =
742539d8c6bSEd Tanous                             resource::Health::OK;
743539d8c6bSEd Tanous                         asyncResp->res.jsonValue["Status"]["State"] =
744539d8c6bSEd Tanous                             resource::State::Starting;
74575815e5cSEd Tanous                         return;
7467e860f15SJohn Edward Broadbent                     }
74775815e5cSEd Tanous                     checkForQuiesced(asyncResp);
7481e1e598dSJonathan Doman                 });
7499c310685SBorawski.Lukasz 
750e99073f5SGeorge Liu             constexpr std::array<std::string_view, 1> interfaces = {
751e99073f5SGeorge Liu                 "xyz.openbmc_project.Inventory.Item.Bmc"};
752e99073f5SGeorge Liu             dbus::utility::getSubTree(
753e99073f5SGeorge Liu                 "/xyz/openbmc_project/inventory", 0, interfaces,
7547e860f15SJohn Edward Broadbent                 [asyncResp](
755e99073f5SGeorge Liu                     const boost::system::error_code& ec,
756b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
7577e860f15SJohn Edward Broadbent                     if (ec)
7581abe55efSEd Tanous                     {
759bd79bce8SPatrick Williams                         BMCWEB_LOG_DEBUG(
760bd79bce8SPatrick Williams                             "D-Bus response error on GetSubTree {}", ec);
7617e860f15SJohn Edward Broadbent                         return;
7627e860f15SJohn Edward Broadbent                     }
76326f6976fSEd Tanous                     if (subtree.empty())
7647e860f15SJohn Edward Broadbent                     {
76562598e31SEd Tanous                         BMCWEB_LOG_DEBUG("Can't find bmc D-Bus object!");
7667e860f15SJohn Edward Broadbent                         return;
7677e860f15SJohn Edward Broadbent                     }
7687e860f15SJohn Edward Broadbent                     // Assume only 1 bmc D-Bus object
7697e860f15SJohn Edward Broadbent                     // Throw an error if there is more than 1
7707e860f15SJohn Edward Broadbent                     if (subtree.size() > 1)
7717e860f15SJohn Edward Broadbent                     {
77262598e31SEd Tanous                         BMCWEB_LOG_DEBUG("Found more than 1 bmc D-Bus object!");
7737e860f15SJohn Edward Broadbent                         messages::internalError(asyncResp->res);
7747e860f15SJohn Edward Broadbent                         return;
7757e860f15SJohn Edward Broadbent                     }
7767e860f15SJohn Edward Broadbent 
777bd79bce8SPatrick Williams                     if (subtree[0].first.empty() ||
778bd79bce8SPatrick Williams                         subtree[0].second.size() != 1)
7797e860f15SJohn Edward Broadbent                     {
78062598e31SEd Tanous                         BMCWEB_LOG_DEBUG("Error getting bmc D-Bus object!");
7817e860f15SJohn Edward Broadbent                         messages::internalError(asyncResp->res);
7827e860f15SJohn Edward Broadbent                         return;
7837e860f15SJohn Edward Broadbent                     }
7847e860f15SJohn Edward Broadbent 
7857e860f15SJohn Edward Broadbent                     const std::string& path = subtree[0].first;
786bd79bce8SPatrick Williams                     const std::string& connectionName =
787bd79bce8SPatrick Williams                         subtree[0].second[0].first;
7887e860f15SJohn Edward Broadbent 
789bd79bce8SPatrick Williams                     for (const auto& interfaceName :
790bd79bce8SPatrick Williams                          subtree[0].second[0].second)
7917e860f15SJohn Edward Broadbent                     {
7927e860f15SJohn Edward Broadbent                         if (interfaceName ==
7937e860f15SJohn Edward Broadbent                             "xyz.openbmc_project.Inventory.Decorator.Asset")
7947e860f15SJohn Edward Broadbent                         {
795deae6a78SEd Tanous                             dbus::utility::getAllProperties(
796bd79bce8SPatrick Williams                                 *crow::connections::systemBus, connectionName,
797bd79bce8SPatrick Williams                                 path,
798fac6e53bSKrzysztof Grobelny                                 "xyz.openbmc_project.Inventory.Decorator.Asset",
799bd79bce8SPatrick Williams                                 [asyncResp](
800bd79bce8SPatrick Williams                                     const boost::system::error_code& ec2,
801b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap&
8027e860f15SJohn Edward Broadbent                                         propertiesList) {
8038a592810SEd Tanous                                     if (ec2)
8047e860f15SJohn Edward Broadbent                                     {
805bd79bce8SPatrick Williams                                         BMCWEB_LOG_DEBUG(
806bd79bce8SPatrick Williams                                             "Can't get bmc asset!");
8077e860f15SJohn Edward Broadbent                                         return;
8087e860f15SJohn Edward Broadbent                                     }
8097e860f15SJohn Edward Broadbent 
810fac6e53bSKrzysztof Grobelny                                     const std::string* partNumber = nullptr;
811fac6e53bSKrzysztof Grobelny                                     const std::string* serialNumber = nullptr;
812fac6e53bSKrzysztof Grobelny                                     const std::string* manufacturer = nullptr;
813fac6e53bSKrzysztof Grobelny                                     const std::string* model = nullptr;
814bd79bce8SPatrick Williams                                     const std::string* sparePartNumber =
815bd79bce8SPatrick Williams                                         nullptr;
816fac6e53bSKrzysztof Grobelny 
817bd79bce8SPatrick Williams                                     const bool success =
818bd79bce8SPatrick Williams                                         sdbusplus::unpackPropertiesNoThrow(
819bd79bce8SPatrick Williams                                             dbus_utils::UnpackErrorPrinter(),
820bd79bce8SPatrick Williams                                             propertiesList, "PartNumber",
821bd79bce8SPatrick Williams                                             partNumber, "SerialNumber",
822bd79bce8SPatrick Williams                                             serialNumber, "Manufacturer",
823bd79bce8SPatrick Williams                                             manufacturer, "Model", model,
824bd79bce8SPatrick Williams                                             "SparePartNumber", sparePartNumber);
825fac6e53bSKrzysztof Grobelny 
826fac6e53bSKrzysztof Grobelny                                     if (!success)
8277e860f15SJohn Edward Broadbent                                     {
828002d39b4SEd Tanous                                         messages::internalError(asyncResp->res);
8297e860f15SJohn Edward Broadbent                                         return;
8307e860f15SJohn Edward Broadbent                                     }
831fac6e53bSKrzysztof Grobelny 
832fac6e53bSKrzysztof Grobelny                                     if (partNumber != nullptr)
833fac6e53bSKrzysztof Grobelny                                     {
834fac6e53bSKrzysztof Grobelny                                         asyncResp->res.jsonValue["PartNumber"] =
835fac6e53bSKrzysztof Grobelny                                             *partNumber;
8367e860f15SJohn Edward Broadbent                                     }
837fac6e53bSKrzysztof Grobelny 
838fac6e53bSKrzysztof Grobelny                                     if (serialNumber != nullptr)
839fac6e53bSKrzysztof Grobelny                                     {
840bd79bce8SPatrick Williams                                         asyncResp->res
841bd79bce8SPatrick Williams                                             .jsonValue["SerialNumber"] =
842fac6e53bSKrzysztof Grobelny                                             *serialNumber;
8437e860f15SJohn Edward Broadbent                                     }
844fac6e53bSKrzysztof Grobelny 
845fac6e53bSKrzysztof Grobelny                                     if (manufacturer != nullptr)
846fac6e53bSKrzysztof Grobelny                                     {
847bd79bce8SPatrick Williams                                         asyncResp->res
848bd79bce8SPatrick Williams                                             .jsonValue["Manufacturer"] =
849fac6e53bSKrzysztof Grobelny                                             *manufacturer;
850fac6e53bSKrzysztof Grobelny                                     }
851fac6e53bSKrzysztof Grobelny 
852fac6e53bSKrzysztof Grobelny                                     if (model != nullptr)
853fac6e53bSKrzysztof Grobelny                                     {
854bd79bce8SPatrick Williams                                         asyncResp->res.jsonValue["Model"] =
855bd79bce8SPatrick Williams                                             *model;
856fac6e53bSKrzysztof Grobelny                                     }
857fac6e53bSKrzysztof Grobelny 
858fac6e53bSKrzysztof Grobelny                                     if (sparePartNumber != nullptr)
859fac6e53bSKrzysztof Grobelny                                     {
860bd79bce8SPatrick Williams                                         asyncResp->res
861bd79bce8SPatrick Williams                                             .jsonValue["SparePartNumber"] =
862fac6e53bSKrzysztof Grobelny                                             *sparePartNumber;
863fac6e53bSKrzysztof Grobelny                                     }
864fac6e53bSKrzysztof Grobelny                                 });
8657e860f15SJohn Edward Broadbent                         }
866bd79bce8SPatrick Williams                         else if (
867bd79bce8SPatrick Williams                             interfaceName ==
8680fda0f12SGeorge Liu                             "xyz.openbmc_project.Inventory.Decorator.LocationCode")
8697e860f15SJohn Edward Broadbent                         {
8707e860f15SJohn Edward Broadbent                             getLocation(asyncResp, connectionName, path);
8717e860f15SJohn Edward Broadbent                         }
8727e860f15SJohn Edward Broadbent                     }
873e99073f5SGeorge Liu                 });
874*c1a75ebcSrohitpai 
875*c1a75ebcSrohitpai             RedfishService::getInstance(app).handleSubRoute(req, asyncResp);
8767e860f15SJohn Edward Broadbent         });
8777e860f15SJohn Edward Broadbent 
878253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/")
879ed398213SEd Tanous         .privileges(redfish::privileges::patchManager)
88045ca1b86SEd Tanous         .methods(boost::beast::http::verb::patch)(
88145ca1b86SEd Tanous             [&app](const crow::Request& req,
882253f11b8SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
883253f11b8SEd Tanous                    const std::string& managerId) {
8843ba00073SCarson Labrado                 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
88545ca1b86SEd Tanous                 {
88645ca1b86SEd Tanous                     return;
88745ca1b86SEd Tanous                 }
888253f11b8SEd Tanous 
889253f11b8SEd Tanous                 if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
890253f11b8SEd Tanous                 {
891bd79bce8SPatrick Williams                     messages::resourceNotFound(asyncResp->res, "Manager",
892bd79bce8SPatrick Williams                                                managerId);
893253f11b8SEd Tanous                     return;
894253f11b8SEd Tanous                 }
895253f11b8SEd Tanous 
8969e9b6049SEd Tanous                 std::optional<std::string> activeSoftwareImageOdataId;
8977e860f15SJohn Edward Broadbent                 std::optional<std::string> datetime;
8989e9b6049SEd Tanous                 std::optional<nlohmann::json::object_t> pidControllers;
8999e9b6049SEd Tanous                 std::optional<nlohmann::json::object_t> fanControllers;
9009e9b6049SEd Tanous                 std::optional<nlohmann::json::object_t> fanZones;
9019e9b6049SEd Tanous                 std::optional<nlohmann::json::object_t> stepwiseControllers;
9029e9b6049SEd Tanous                 std::optional<std::string> profile;
9037e860f15SJohn Edward Broadbent 
904afc474aeSMyung Bae                 if (!json_util::readJsonPatch(                            //
905afc474aeSMyung Bae                         req, asyncResp->res,                              //
906afc474aeSMyung Bae                         "DateTime", datetime,                             //
907afc474aeSMyung Bae                         "Links/ActiveSoftwareImage/@odata.id",
908afc474aeSMyung Bae                         activeSoftwareImageOdataId,                       //
909afc474aeSMyung Bae                         "Oem/OpenBmc/Fan/FanControllers", fanControllers, //
910afc474aeSMyung Bae                         "Oem/OpenBmc/Fan/FanZones", fanZones,             //
911afc474aeSMyung Bae                         "Oem/OpenBmc/Fan/PidControllers", pidControllers, //
912afc474aeSMyung Bae                         "Oem/OpenBmc/Fan/Profile", profile,               //
913afc474aeSMyung Bae                         "Oem/OpenBmc/Fan/StepwiseControllers",
914afc474aeSMyung Bae                         stepwiseControllers                               //
9159e9b6049SEd Tanous                         ))
9167e860f15SJohn Edward Broadbent                 {
9177e860f15SJohn Edward Broadbent                     return;
9187e860f15SJohn Edward Broadbent                 }
9197e860f15SJohn Edward Broadbent 
9209e9b6049SEd Tanous                 if (pidControllers || fanControllers || fanZones ||
9219e9b6049SEd Tanous                     stepwiseControllers || profile)
9227e860f15SJohn Edward Broadbent                 {
92325b54dbaSEd Tanous                     if constexpr (BMCWEB_REDFISH_OEM_MANAGER_FAN_DATA)
92425b54dbaSEd Tanous                     {
925bd79bce8SPatrick Williams                         std::vector<
926bd79bce8SPatrick Williams                             std::pair<std::string,
92725b54dbaSEd Tanous                                       std::optional<nlohmann::json::object_t>>>
9289e9b6049SEd Tanous                             configuration;
9299e9b6049SEd Tanous                         if (pidControllers)
9307e860f15SJohn Edward Broadbent                         {
931bd79bce8SPatrick Williams                             configuration.emplace_back(
932bd79bce8SPatrick Williams                                 "PidControllers", std::move(pidControllers));
9337e860f15SJohn Edward Broadbent                         }
9349e9b6049SEd Tanous                         if (fanControllers)
9357e860f15SJohn Edward Broadbent                         {
936bd79bce8SPatrick Williams                             configuration.emplace_back(
937bd79bce8SPatrick Williams                                 "FanControllers", std::move(fanControllers));
9387e860f15SJohn Edward Broadbent                         }
9399e9b6049SEd Tanous                         if (fanZones)
9407e860f15SJohn Edward Broadbent                         {
941bd79bce8SPatrick Williams                             configuration.emplace_back("FanZones",
942bd79bce8SPatrick Williams                                                        std::move(fanZones));
9439e9b6049SEd Tanous                         }
9449e9b6049SEd Tanous                         if (stepwiseControllers)
9459e9b6049SEd Tanous                         {
946bd79bce8SPatrick Williams                             configuration.emplace_back(
947bd79bce8SPatrick Williams                                 "StepwiseControllers",
9489e9b6049SEd Tanous                                 std::move(stepwiseControllers));
9499e9b6049SEd Tanous                         }
9509e9b6049SEd Tanous                         auto pid = std::make_shared<SetPIDValues>(
9519e9b6049SEd Tanous                             asyncResp, std::move(configuration), profile);
9527e860f15SJohn Edward Broadbent                         pid->run();
95325b54dbaSEd Tanous                     }
95425b54dbaSEd Tanous                     else
95525b54dbaSEd Tanous                     {
95654dce7f5SGunnar Mills                         messages::propertyUnknown(asyncResp->res, "Oem");
95754dce7f5SGunnar Mills                         return;
95825b54dbaSEd Tanous                     }
9597e860f15SJohn Edward Broadbent                 }
9609e9b6049SEd Tanous 
9619e9b6049SEd Tanous                 if (activeSoftwareImageOdataId)
9627e860f15SJohn Edward Broadbent                 {
963bd79bce8SPatrick Williams                     setActiveFirmwareImage(asyncResp,
964bd79bce8SPatrick Williams                                            *activeSoftwareImageOdataId);
9657e860f15SJohn Edward Broadbent                 }
9667e860f15SJohn Edward Broadbent 
9677e860f15SJohn Edward Broadbent                 if (datetime)
9687e860f15SJohn Edward Broadbent                 {
969c51afd54SEd Tanous                     setDateTime(asyncResp, *datetime);
9707e860f15SJohn Edward Broadbent                 }
9717e860f15SJohn Edward Broadbent             });
9727e860f15SJohn Edward Broadbent }
9737e860f15SJohn Edward Broadbent 
9747e860f15SJohn Edward Broadbent inline void requestRoutesManagerCollection(App& app)
9757e860f15SJohn Edward Broadbent {
9767e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
977ed398213SEd Tanous         .privileges(redfish::privileges::getManagerCollection)
9787e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
97945ca1b86SEd Tanous             [&app](const crow::Request& req,
9807e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
9813ba00073SCarson Labrado                 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
98245ca1b86SEd Tanous                 {
98345ca1b86SEd Tanous                     return;
98445ca1b86SEd Tanous                 }
98583ff9ab6SJames Feist                 // Collections don't include the static data added by SubRoute
98683ff9ab6SJames Feist                 // because it has a duplicate entry for members
9878d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
9888d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
9898d1b46d7Szhanghch05                     "#ManagerCollection.ManagerCollection";
9908d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "Manager Collection";
9918d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Members@odata.count"] = 1;
9921476687dSEd Tanous                 nlohmann::json::array_t members;
9931476687dSEd Tanous                 nlohmann::json& bmc = members.emplace_back();
994bd79bce8SPatrick Williams                 bmc["@odata.id"] = boost::urls::format(
995bd79bce8SPatrick Williams                     "/redfish/v1/Managers/{}", BMCWEB_REDFISH_MANAGER_URI_NAME);
9961476687dSEd Tanous                 asyncResp->res.jsonValue["Members"] = std::move(members);
9977e860f15SJohn Edward Broadbent             });
9989c310685SBorawski.Lukasz }
9999c310685SBorawski.Lukasz } // namespace redfish
1000