xref: /openbmc/bmcweb/features/redfish/lib/power_supply.hpp (revision 40e9b92ec19acffb46f83a6e55b18974da5d708e)
1*40e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
2*40e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3a7210020SGeorge Liu #pragma once
4a7210020SGeorge Liu 
5a7210020SGeorge Liu #include "app.hpp"
6a7210020SGeorge Liu #include "dbus_utility.hpp"
7539d8c6bSEd Tanous #include "generated/enums/resource.hpp"
8a7210020SGeorge Liu #include "query.hpp"
9a7210020SGeorge Liu #include "registries/privilege_registry.hpp"
10a7210020SGeorge Liu #include "utils/chassis_utils.hpp"
112b45fb3bSGeorge Liu #include "utils/dbus_utils.hpp"
122b45fb3bSGeorge Liu #include "utils/json_utils.hpp"
13b5190062SHieu Huynh #include "utils/time_utils.hpp"
14a7210020SGeorge Liu 
1534dfcb94SGeorge Liu #include <boost/system/error_code.hpp>
16ef4c65b7SEd Tanous #include <boost/url/format.hpp>
17ef4c65b7SEd Tanous 
18a7210020SGeorge Liu #include <memory>
19a7210020SGeorge Liu #include <optional>
20a7210020SGeorge Liu #include <string>
21a7210020SGeorge Liu 
22a7210020SGeorge Liu namespace redfish
23a7210020SGeorge Liu {
24a7210020SGeorge Liu 
25788fe6cfSLakshmi Yadlapati static constexpr std::array<std::string_view, 1> powerSupplyInterface = {
26788fe6cfSLakshmi Yadlapati     "xyz.openbmc_project.Inventory.Item.PowerSupply"};
27788fe6cfSLakshmi Yadlapati 
28788fe6cfSLakshmi Yadlapati inline void updatePowerSupplyList(
29788fe6cfSLakshmi Yadlapati     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3000ef5dc6SGeorge Liu     const std::string& chassisId,
31788fe6cfSLakshmi Yadlapati     const dbus::utility::MapperGetSubTreePathsResponse& powerSupplyPaths)
32788fe6cfSLakshmi Yadlapati {
33788fe6cfSLakshmi Yadlapati     nlohmann::json& powerSupplyList = asyncResp->res.jsonValue["Members"];
34788fe6cfSLakshmi Yadlapati     for (const std::string& powerSupplyPath : powerSupplyPaths)
35a7210020SGeorge Liu     {
3600ef5dc6SGeorge Liu         std::string powerSupplyName =
3700ef5dc6SGeorge Liu             sdbusplus::message::object_path(powerSupplyPath).filename();
3800ef5dc6SGeorge Liu         if (powerSupplyName.empty())
3900ef5dc6SGeorge Liu         {
40788fe6cfSLakshmi Yadlapati             continue;
4100ef5dc6SGeorge Liu         }
4200ef5dc6SGeorge Liu 
4300ef5dc6SGeorge Liu         nlohmann::json item = nlohmann::json::object();
4400ef5dc6SGeorge Liu         item["@odata.id"] = boost::urls::format(
4500ef5dc6SGeorge Liu             "/redfish/v1/Chassis/{}/PowerSubsystem/PowerSupplies/{}", chassisId,
4600ef5dc6SGeorge Liu             powerSupplyName);
4700ef5dc6SGeorge Liu 
4800ef5dc6SGeorge Liu         powerSupplyList.emplace_back(std::move(item));
49788fe6cfSLakshmi Yadlapati     }
5000ef5dc6SGeorge Liu     asyncResp->res.jsonValue["Members@odata.count"] = powerSupplyList.size();
51a7210020SGeorge Liu }
52a7210020SGeorge Liu 
533e42a329SLakshmi Yadlapati inline void doPowerSupplyCollection(
543e42a329SLakshmi Yadlapati     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
553e42a329SLakshmi Yadlapati     const std::string& chassisId, const boost::system::error_code& ec,
563e42a329SLakshmi Yadlapati     const dbus::utility::MapperGetSubTreePathsResponse& subtreePaths)
57a7210020SGeorge Liu {
583e42a329SLakshmi Yadlapati     if (ec)
59a7210020SGeorge Liu     {
603e42a329SLakshmi Yadlapati         if (ec.value() != EBADR)
613e42a329SLakshmi Yadlapati         {
623e42a329SLakshmi Yadlapati             BMCWEB_LOG_ERROR("DBUS response error{}", ec.value());
633e42a329SLakshmi Yadlapati             messages::internalError(asyncResp->res);
643e42a329SLakshmi Yadlapati         }
65a7210020SGeorge Liu         return;
66a7210020SGeorge Liu     }
67a7210020SGeorge Liu     asyncResp->res.addHeader(
68a7210020SGeorge Liu         boost::beast::http::field::link,
69a7210020SGeorge Liu         "</redfish/v1/JsonSchemas/PowerSupplyCollection/PowerSupplyCollection.json>; rel=describedby");
70a7210020SGeorge Liu     asyncResp->res.jsonValue["@odata.type"] =
71a7210020SGeorge Liu         "#PowerSupplyCollection.PowerSupplyCollection";
72a7210020SGeorge Liu     asyncResp->res.jsonValue["Name"] = "Power Supply Collection";
73ef4c65b7SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
74ef4c65b7SEd Tanous         "/redfish/v1/Chassis/{}/PowerSubsystem/PowerSupplies", chassisId);
75a7210020SGeorge Liu     asyncResp->res.jsonValue["Description"] =
76a7210020SGeorge Liu         "The collection of PowerSupply resource instances.";
777a2bb2c9SGeorge Liu     asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
787a2bb2c9SGeorge Liu     asyncResp->res.jsonValue["Members@odata.count"] = 0;
79a7210020SGeorge Liu 
80788fe6cfSLakshmi Yadlapati     updatePowerSupplyList(asyncResp, chassisId, subtreePaths);
81a7210020SGeorge Liu }
82a7210020SGeorge Liu 
83a7210020SGeorge Liu inline void handlePowerSupplyCollectionHead(
84a7210020SGeorge Liu     App& app, const crow::Request& req,
85a7210020SGeorge Liu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
86a7210020SGeorge Liu     const std::string& chassisId)
87a7210020SGeorge Liu {
88a7210020SGeorge Liu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
89a7210020SGeorge Liu     {
90a7210020SGeorge Liu         return;
91a7210020SGeorge Liu     }
92a7210020SGeorge Liu 
93a7210020SGeorge Liu     redfish::chassis_utils::getValidChassisPath(
94a7210020SGeorge Liu         asyncResp, chassisId,
95a7210020SGeorge Liu         [asyncResp,
96a7210020SGeorge Liu          chassisId](const std::optional<std::string>& validChassisPath) {
97a7210020SGeorge Liu             if (!validChassisPath)
98a7210020SGeorge Liu             {
99bd79bce8SPatrick Williams                 messages::resourceNotFound(asyncResp->res, "Chassis",
100bd79bce8SPatrick Williams                                            chassisId);
101a7210020SGeorge Liu                 return;
102a7210020SGeorge Liu             }
103a7210020SGeorge Liu             asyncResp->res.addHeader(
104a7210020SGeorge Liu                 boost::beast::http::field::link,
105a7210020SGeorge Liu                 "</redfish/v1/JsonSchemas/PowerSupplyCollection/PowerSupplyCollection.json>; rel=describedby");
106a7210020SGeorge Liu         });
107a7210020SGeorge Liu }
108a7210020SGeorge Liu 
109a7210020SGeorge Liu inline void handlePowerSupplyCollectionGet(
110a7210020SGeorge Liu     App& app, const crow::Request& req,
111a7210020SGeorge Liu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
112a7210020SGeorge Liu     const std::string& chassisId)
113a7210020SGeorge Liu {
114a7210020SGeorge Liu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
115a7210020SGeorge Liu     {
116a7210020SGeorge Liu         return;
117a7210020SGeorge Liu     }
118a7210020SGeorge Liu 
1193e42a329SLakshmi Yadlapati     constexpr std::array<std::string_view, 2> chasisInterfaces = {
1203e42a329SLakshmi Yadlapati         "xyz.openbmc_project.Inventory.Item.Board",
1213e42a329SLakshmi Yadlapati         "xyz.openbmc_project.Inventory.Item.Chassis"};
1223e42a329SLakshmi Yadlapati     const std::string reqpath = "/xyz/openbmc_project/inventory";
1233e42a329SLakshmi Yadlapati 
1243e42a329SLakshmi Yadlapati     dbus::utility::getAssociatedSubTreePathsById(
1253e42a329SLakshmi Yadlapati         chassisId, reqpath, chasisInterfaces, "powered_by",
1263e42a329SLakshmi Yadlapati         powerSupplyInterface,
1273e42a329SLakshmi Yadlapati         [asyncResp, chassisId](
1283e42a329SLakshmi Yadlapati             const boost::system::error_code& ec,
1293e42a329SLakshmi Yadlapati             const dbus::utility::MapperGetSubTreePathsResponse& subtreePaths) {
1303e42a329SLakshmi Yadlapati             doPowerSupplyCollection(asyncResp, chassisId, ec, subtreePaths);
1313e42a329SLakshmi Yadlapati         });
132a7210020SGeorge Liu }
133a7210020SGeorge Liu 
134a7210020SGeorge Liu inline void requestRoutesPowerSupplyCollection(App& app)
135a7210020SGeorge Liu {
136a7210020SGeorge Liu     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PowerSubsystem/PowerSupplies/")
137a7210020SGeorge Liu         .privileges(redfish::privileges::headPowerSupplyCollection)
138a7210020SGeorge Liu         .methods(boost::beast::http::verb::head)(
139a7210020SGeorge Liu             std::bind_front(handlePowerSupplyCollectionHead, std::ref(app)));
140a7210020SGeorge Liu 
141a7210020SGeorge Liu     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PowerSubsystem/PowerSupplies/")
142a7210020SGeorge Liu         .privileges(redfish::privileges::getPowerSupplyCollection)
143a7210020SGeorge Liu         .methods(boost::beast::http::verb::get)(
144a7210020SGeorge Liu             std::bind_front(handlePowerSupplyCollectionGet, std::ref(app)));
145a7210020SGeorge Liu }
146a7210020SGeorge Liu 
1473e42a329SLakshmi Yadlapati inline void afterGetValidPowerSupplyPath(
14834dfcb94SGeorge Liu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1493e42a329SLakshmi Yadlapati     const std::string& powerSupplyId, const boost::system::error_code& ec,
1503e42a329SLakshmi Yadlapati     const dbus::utility::MapperGetSubTreeResponse& subtree,
1513e42a329SLakshmi Yadlapati     const std::function<void(const std::string& powerSupplyPath,
1523e42a329SLakshmi Yadlapati                              const std::string& service)>& callback)
15300ef5dc6SGeorge Liu {
15400ef5dc6SGeorge Liu     if (ec)
15500ef5dc6SGeorge Liu     {
15600ef5dc6SGeorge Liu         if (ec.value() != EBADR)
15700ef5dc6SGeorge Liu         {
1583e42a329SLakshmi Yadlapati             BMCWEB_LOG_ERROR("DBUS response error{}", ec.value());
15900ef5dc6SGeorge Liu             messages::internalError(asyncResp->res);
1603e42a329SLakshmi Yadlapati         }
16100ef5dc6SGeorge Liu         return;
16200ef5dc6SGeorge Liu     }
1633e42a329SLakshmi Yadlapati     for (const auto& [objectPath, service] : subtree)
16400ef5dc6SGeorge Liu     {
1653e42a329SLakshmi Yadlapati         sdbusplus::message::object_path path(objectPath);
166d8e2b618SMyung Bae         if (path.filename() == powerSupplyId)
16700ef5dc6SGeorge Liu         {
1683e42a329SLakshmi Yadlapati             callback(path, service.begin()->first);
16900ef5dc6SGeorge Liu             return;
17000ef5dc6SGeorge Liu         }
17100ef5dc6SGeorge Liu     }
17200ef5dc6SGeorge Liu 
17362598e31SEd Tanous     BMCWEB_LOG_WARNING("Power supply not found: {}", powerSupplyId);
1743e42a329SLakshmi Yadlapati     messages::resourceNotFound(asyncResp->res, "PowerSupplies", powerSupplyId);
17500ef5dc6SGeorge Liu }
1763e42a329SLakshmi Yadlapati 
1773e42a329SLakshmi Yadlapati inline void getValidPowerSupplyPath(
1783e42a329SLakshmi Yadlapati     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1793e42a329SLakshmi Yadlapati     const std::string& chassisId, const std::string& powerSupplyId,
1803e42a329SLakshmi Yadlapati     std::function<void(const std::string& powerSupplyPath,
1813e42a329SLakshmi Yadlapati                        const std::string& service)>&& callback)
1823e42a329SLakshmi Yadlapati {
1833e42a329SLakshmi Yadlapati     constexpr std::array<std::string_view, 2> chasisInterfaces = {
1843e42a329SLakshmi Yadlapati         "xyz.openbmc_project.Inventory.Item.Board",
1853e42a329SLakshmi Yadlapati         "xyz.openbmc_project.Inventory.Item.Chassis"};
1863e42a329SLakshmi Yadlapati     const std::string reqpath = "/xyz/openbmc_project/inventory";
1873e42a329SLakshmi Yadlapati 
1883e42a329SLakshmi Yadlapati     dbus::utility::getAssociatedSubTreeById(
1893e42a329SLakshmi Yadlapati         chassisId, reqpath, chasisInterfaces, "powered_by",
1903e42a329SLakshmi Yadlapati         powerSupplyInterface,
1913e42a329SLakshmi Yadlapati         [asyncResp, chassisId, powerSupplyId, callback{std::move(callback)}](
1923e42a329SLakshmi Yadlapati             const boost::system::error_code& ec,
1933e42a329SLakshmi Yadlapati             const dbus::utility::MapperGetSubTreeResponse& subtree) {
1943e42a329SLakshmi Yadlapati             afterGetValidPowerSupplyPath(asyncResp, powerSupplyId, ec, subtree,
1953e42a329SLakshmi Yadlapati                                          callback);
19600ef5dc6SGeorge Liu         });
19700ef5dc6SGeorge Liu }
19800ef5dc6SGeorge Liu 
19900ef5dc6SGeorge Liu inline void
20034dfcb94SGeorge Liu     getPowerSupplyState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
20134dfcb94SGeorge Liu                         const std::string& service, const std::string& path)
20234dfcb94SGeorge Liu {
203deae6a78SEd Tanous     dbus::utility::getProperty<bool>(
204deae6a78SEd Tanous         service, path, "xyz.openbmc_project.Inventory.Item", "Present",
20534dfcb94SGeorge Liu         [asyncResp](const boost::system::error_code& ec, const bool value) {
20634dfcb94SGeorge Liu             if (ec)
20734dfcb94SGeorge Liu             {
20834dfcb94SGeorge Liu                 if (ec.value() != EBADR)
20934dfcb94SGeorge Liu                 {
21062598e31SEd Tanous                     BMCWEB_LOG_ERROR("DBUS response error for State {}",
21162598e31SEd Tanous                                      ec.value());
21234dfcb94SGeorge Liu                     messages::internalError(asyncResp->res);
21334dfcb94SGeorge Liu                 }
21434dfcb94SGeorge Liu                 return;
21534dfcb94SGeorge Liu             }
21634dfcb94SGeorge Liu 
21734dfcb94SGeorge Liu             if (!value)
21834dfcb94SGeorge Liu             {
219539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Status"]["State"] =
220539d8c6bSEd Tanous                     resource::State::Absent;
22134dfcb94SGeorge Liu             }
22234dfcb94SGeorge Liu         });
22334dfcb94SGeorge Liu }
22434dfcb94SGeorge Liu 
22534dfcb94SGeorge Liu inline void
22634dfcb94SGeorge Liu     getPowerSupplyHealth(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
22734dfcb94SGeorge Liu                          const std::string& service, const std::string& path)
22834dfcb94SGeorge Liu {
229deae6a78SEd Tanous     dbus::utility::getProperty<bool>(
230deae6a78SEd Tanous         service, path, "xyz.openbmc_project.State.Decorator.OperationalStatus",
231deae6a78SEd Tanous         "Functional",
23234dfcb94SGeorge Liu         [asyncResp](const boost::system::error_code& ec, const bool value) {
23334dfcb94SGeorge Liu             if (ec)
23434dfcb94SGeorge Liu             {
23534dfcb94SGeorge Liu                 if (ec.value() != EBADR)
23634dfcb94SGeorge Liu                 {
23762598e31SEd Tanous                     BMCWEB_LOG_ERROR("DBUS response error for Health {}",
23862598e31SEd Tanous                                      ec.value());
23934dfcb94SGeorge Liu                     messages::internalError(asyncResp->res);
24034dfcb94SGeorge Liu                 }
24134dfcb94SGeorge Liu                 return;
24234dfcb94SGeorge Liu             }
24334dfcb94SGeorge Liu 
24434dfcb94SGeorge Liu             if (!value)
24534dfcb94SGeorge Liu             {
246539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Status"]["Health"] =
247539d8c6bSEd Tanous                     resource::Health::Critical;
24834dfcb94SGeorge Liu             }
24934dfcb94SGeorge Liu         });
25034dfcb94SGeorge Liu }
25134dfcb94SGeorge Liu 
25234dfcb94SGeorge Liu inline void
2532b45fb3bSGeorge Liu     getPowerSupplyAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2542b45fb3bSGeorge Liu                         const std::string& service, const std::string& path)
2552b45fb3bSGeorge Liu {
256deae6a78SEd Tanous     dbus::utility::getAllProperties(
257deae6a78SEd Tanous         service, path, "xyz.openbmc_project.Inventory.Decorator.Asset",
2582b45fb3bSGeorge Liu         [asyncResp](const boost::system::error_code& ec,
2592b45fb3bSGeorge Liu                     const dbus::utility::DBusPropertiesMap& propertiesList) {
2602b45fb3bSGeorge Liu             if (ec)
2612b45fb3bSGeorge Liu             {
2622b45fb3bSGeorge Liu                 if (ec.value() != EBADR)
2632b45fb3bSGeorge Liu                 {
26462598e31SEd Tanous                     BMCWEB_LOG_ERROR("DBUS response error for Asset {}",
26562598e31SEd Tanous                                      ec.value());
2662b45fb3bSGeorge Liu                     messages::internalError(asyncResp->res);
2672b45fb3bSGeorge Liu                 }
2682b45fb3bSGeorge Liu                 return;
2692b45fb3bSGeorge Liu             }
2702b45fb3bSGeorge Liu 
2712b45fb3bSGeorge Liu             const std::string* partNumber = nullptr;
2722b45fb3bSGeorge Liu             const std::string* serialNumber = nullptr;
2732b45fb3bSGeorge Liu             const std::string* manufacturer = nullptr;
2742b45fb3bSGeorge Liu             const std::string* model = nullptr;
2752b45fb3bSGeorge Liu             const std::string* sparePartNumber = nullptr;
276b5190062SHieu Huynh             const std::string* buildDate = nullptr;
2772b45fb3bSGeorge Liu 
2782b45fb3bSGeorge Liu             const bool success = sdbusplus::unpackPropertiesNoThrow(
2792b45fb3bSGeorge Liu                 dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
2802b45fb3bSGeorge Liu                 partNumber, "SerialNumber", serialNumber, "Manufacturer",
281bd79bce8SPatrick Williams                 manufacturer, "Model", model, "SparePartNumber",
282b5190062SHieu Huynh                 sparePartNumber, "BuildDate", buildDate);
2832b45fb3bSGeorge Liu 
2842b45fb3bSGeorge Liu             if (!success)
2852b45fb3bSGeorge Liu             {
2862b45fb3bSGeorge Liu                 messages::internalError(asyncResp->res);
2872b45fb3bSGeorge Liu                 return;
2882b45fb3bSGeorge Liu             }
2892b45fb3bSGeorge Liu 
2902b45fb3bSGeorge Liu             if (partNumber != nullptr)
2912b45fb3bSGeorge Liu             {
2922b45fb3bSGeorge Liu                 asyncResp->res.jsonValue["PartNumber"] = *partNumber;
2932b45fb3bSGeorge Liu             }
2942b45fb3bSGeorge Liu 
2952b45fb3bSGeorge Liu             if (serialNumber != nullptr)
2962b45fb3bSGeorge Liu             {
2972b45fb3bSGeorge Liu                 asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
2982b45fb3bSGeorge Liu             }
2992b45fb3bSGeorge Liu 
3002b45fb3bSGeorge Liu             if (manufacturer != nullptr)
3012b45fb3bSGeorge Liu             {
3022b45fb3bSGeorge Liu                 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
3032b45fb3bSGeorge Liu             }
3042b45fb3bSGeorge Liu 
3052b45fb3bSGeorge Liu             if (model != nullptr)
3062b45fb3bSGeorge Liu             {
3072b45fb3bSGeorge Liu                 asyncResp->res.jsonValue["Model"] = *model;
3082b45fb3bSGeorge Liu             }
3092b45fb3bSGeorge Liu 
3102b45fb3bSGeorge Liu             // SparePartNumber is optional on D-Bus so skip if it is empty
3112b45fb3bSGeorge Liu             if (sparePartNumber != nullptr && !sparePartNumber->empty())
3122b45fb3bSGeorge Liu             {
3132b45fb3bSGeorge Liu                 asyncResp->res.jsonValue["SparePartNumber"] = *sparePartNumber;
3142b45fb3bSGeorge Liu             }
315b5190062SHieu Huynh 
316b5190062SHieu Huynh             if (buildDate != nullptr)
317b5190062SHieu Huynh             {
318b5190062SHieu Huynh                 time_utils::productionDateReport(asyncResp->res, *buildDate);
319b5190062SHieu Huynh             }
3202b45fb3bSGeorge Liu         });
3212b45fb3bSGeorge Liu }
3222b45fb3bSGeorge Liu 
323a0dba87bSGeorge Liu inline void getPowerSupplyFirmwareVersion(
324a0dba87bSGeorge Liu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
325a0dba87bSGeorge Liu     const std::string& service, const std::string& path)
326a0dba87bSGeorge Liu {
327deae6a78SEd Tanous     dbus::utility::getProperty<std::string>(
328deae6a78SEd Tanous         service, path, "xyz.openbmc_project.Software.Version", "Version",
329a0dba87bSGeorge Liu         [asyncResp](const boost::system::error_code& ec,
330a0dba87bSGeorge Liu                     const std::string& value) {
331a0dba87bSGeorge Liu             if (ec)
332a0dba87bSGeorge Liu             {
333a0dba87bSGeorge Liu                 if (ec.value() != EBADR)
334a0dba87bSGeorge Liu                 {
335bd79bce8SPatrick Williams                     BMCWEB_LOG_ERROR(
336bd79bce8SPatrick Williams                         "DBUS response error for FirmwareVersion {}",
33762598e31SEd Tanous                         ec.value());
338a0dba87bSGeorge Liu                     messages::internalError(asyncResp->res);
339a0dba87bSGeorge Liu                 }
340a0dba87bSGeorge Liu                 return;
341a0dba87bSGeorge Liu             }
342a0dba87bSGeorge Liu             asyncResp->res.jsonValue["FirmwareVersion"] = value;
343a0dba87bSGeorge Liu         });
344a0dba87bSGeorge Liu }
345a0dba87bSGeorge Liu 
3462b45fb3bSGeorge Liu inline void
34744845e5fSGeorge Liu     getPowerSupplyLocation(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
34844845e5fSGeorge Liu                            const std::string& service, const std::string& path)
34944845e5fSGeorge Liu {
350deae6a78SEd Tanous     dbus::utility::getProperty<std::string>(
351deae6a78SEd Tanous         service, path, "xyz.openbmc_project.Inventory.Decorator.LocationCode",
352deae6a78SEd Tanous         "LocationCode",
35344845e5fSGeorge Liu         [asyncResp](const boost::system::error_code& ec,
35444845e5fSGeorge Liu                     const std::string& value) {
35544845e5fSGeorge Liu             if (ec)
35644845e5fSGeorge Liu             {
35744845e5fSGeorge Liu                 if (ec.value() != EBADR)
35844845e5fSGeorge Liu                 {
35962598e31SEd Tanous                     BMCWEB_LOG_ERROR("DBUS response error for Location {}",
36062598e31SEd Tanous                                      ec.value());
36144845e5fSGeorge Liu                     messages::internalError(asyncResp->res);
36244845e5fSGeorge Liu                 }
36344845e5fSGeorge Liu                 return;
36444845e5fSGeorge Liu             }
365bd79bce8SPatrick Williams             asyncResp->res
366bd79bce8SPatrick Williams                 .jsonValue["Location"]["PartLocation"]["ServiceLabel"] = value;
36744845e5fSGeorge Liu         });
36844845e5fSGeorge Liu }
36944845e5fSGeorge Liu 
370ddceee07SGeorge Liu inline void handleGetEfficiencyResponse(
371ddceee07SGeorge Liu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
372ddceee07SGeorge Liu     const boost::system::error_code& ec, uint32_t value)
373ddceee07SGeorge Liu {
374ddceee07SGeorge Liu     if (ec)
375ddceee07SGeorge Liu     {
376ddceee07SGeorge Liu         if (ec.value() != EBADR)
377ddceee07SGeorge Liu         {
37862598e31SEd Tanous             BMCWEB_LOG_ERROR("DBUS response error for DeratingFactor {}",
37962598e31SEd Tanous                              ec.value());
380ddceee07SGeorge Liu             messages::internalError(asyncResp->res);
381ddceee07SGeorge Liu         }
382ddceee07SGeorge Liu         return;
383ddceee07SGeorge Liu     }
384ddceee07SGeorge Liu     // The PDI default value is 0, if it hasn't been set leave off
385ddceee07SGeorge Liu     if (value == 0)
386ddceee07SGeorge Liu     {
387ddceee07SGeorge Liu         return;
388ddceee07SGeorge Liu     }
389ddceee07SGeorge Liu 
390ddceee07SGeorge Liu     nlohmann::json::array_t efficiencyRatings;
391ddceee07SGeorge Liu     nlohmann::json::object_t efficiencyPercent;
392ddceee07SGeorge Liu     efficiencyPercent["EfficiencyPercent"] = value;
393ddceee07SGeorge Liu     efficiencyRatings.emplace_back(std::move(efficiencyPercent));
394ddceee07SGeorge Liu     asyncResp->res.jsonValue["EfficiencyRatings"] =
395ddceee07SGeorge Liu         std::move(efficiencyRatings);
396ddceee07SGeorge Liu }
397ddceee07SGeorge Liu 
398ddceee07SGeorge Liu inline void handlePowerSupplyAttributesSubTreeResponse(
399ddceee07SGeorge Liu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
400ddceee07SGeorge Liu     const boost::system::error_code& ec,
401ddceee07SGeorge Liu     const dbus::utility::MapperGetSubTreeResponse& subtree)
402ddceee07SGeorge Liu {
403ddceee07SGeorge Liu     if (ec)
404ddceee07SGeorge Liu     {
405ddceee07SGeorge Liu         if (ec.value() != EBADR)
406ddceee07SGeorge Liu         {
40762598e31SEd Tanous             BMCWEB_LOG_ERROR("DBUS response error for EfficiencyPercent {}",
40862598e31SEd Tanous                              ec.value());
409ddceee07SGeorge Liu             messages::internalError(asyncResp->res);
410ddceee07SGeorge Liu         }
411ddceee07SGeorge Liu         return;
412ddceee07SGeorge Liu     }
413ddceee07SGeorge Liu 
414ddceee07SGeorge Liu     if (subtree.empty())
415ddceee07SGeorge Liu     {
41662598e31SEd Tanous         BMCWEB_LOG_DEBUG("Can't find Power Supply Attributes!");
417ddceee07SGeorge Liu         return;
418ddceee07SGeorge Liu     }
419ddceee07SGeorge Liu 
420ddceee07SGeorge Liu     if (subtree.size() != 1)
421ddceee07SGeorge Liu     {
42262598e31SEd Tanous         BMCWEB_LOG_ERROR(
42362598e31SEd Tanous             "Unexpected number of paths returned by getSubTree: {}",
42462598e31SEd Tanous             subtree.size());
425ddceee07SGeorge Liu         messages::internalError(asyncResp->res);
426ddceee07SGeorge Liu         return;
427ddceee07SGeorge Liu     }
428ddceee07SGeorge Liu 
429ddceee07SGeorge Liu     const auto& [path, serviceMap] = *subtree.begin();
430ddceee07SGeorge Liu     const auto& [service, interfaces] = *serviceMap.begin();
431deae6a78SEd Tanous     dbus::utility::getProperty<uint32_t>(
432deae6a78SEd Tanous         service, path, "xyz.openbmc_project.Control.PowerSupplyAttributes",
433deae6a78SEd Tanous         "DeratingFactor",
434ddceee07SGeorge Liu         [asyncResp](const boost::system::error_code& ec1, uint32_t value) {
435ddceee07SGeorge Liu             handleGetEfficiencyResponse(asyncResp, ec1, value);
436ddceee07SGeorge Liu         });
437ddceee07SGeorge Liu }
438ddceee07SGeorge Liu 
439ddceee07SGeorge Liu inline void
440ddceee07SGeorge Liu     getEfficiencyPercent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
441ddceee07SGeorge Liu {
442ddceee07SGeorge Liu     constexpr std::array<std::string_view, 1> efficiencyIntf = {
443ddceee07SGeorge Liu         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
444ddceee07SGeorge Liu 
445ddceee07SGeorge Liu     dbus::utility::getSubTree(
446ddceee07SGeorge Liu         "/xyz/openbmc_project", 0, efficiencyIntf,
447ddceee07SGeorge Liu         [asyncResp](const boost::system::error_code& ec,
448ddceee07SGeorge Liu                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
449ddceee07SGeorge Liu             handlePowerSupplyAttributesSubTreeResponse(asyncResp, ec, subtree);
450ddceee07SGeorge Liu         });
451ddceee07SGeorge Liu }
452ddceee07SGeorge Liu 
453bd79bce8SPatrick Williams inline void doPowerSupplyGet(
454bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
455bd79bce8SPatrick Williams     const std::string& chassisId, const std::string& powerSupplyId,
4563e42a329SLakshmi Yadlapati     const std::string& powerSupplyPath, const std::string& service)
45700ef5dc6SGeorge Liu {
45800ef5dc6SGeorge Liu     asyncResp->res.addHeader(
45900ef5dc6SGeorge Liu         boost::beast::http::field::link,
46000ef5dc6SGeorge Liu         "</redfish/v1/JsonSchemas/PowerSupply/PowerSupply.json>; rel=describedby");
4613e42a329SLakshmi Yadlapati     asyncResp->res.jsonValue["@odata.type"] = "#PowerSupply.v1_5_0.PowerSupply";
46200ef5dc6SGeorge Liu     asyncResp->res.jsonValue["Name"] = "Power Supply";
46300ef5dc6SGeorge Liu     asyncResp->res.jsonValue["Id"] = powerSupplyId;
46400ef5dc6SGeorge Liu     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
4653e42a329SLakshmi Yadlapati         "/redfish/v1/Chassis/{}/PowerSubsystem/PowerSupplies/{}", chassisId,
4663e42a329SLakshmi Yadlapati         powerSupplyId);
46734dfcb94SGeorge Liu 
4683e42a329SLakshmi Yadlapati     asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
469539d8c6bSEd Tanous     asyncResp->res.jsonValue["Status"]["Health"] = resource::Health::OK;
47034dfcb94SGeorge Liu 
4713e42a329SLakshmi Yadlapati     getPowerSupplyState(asyncResp, service, powerSupplyPath);
4723e42a329SLakshmi Yadlapati     getPowerSupplyHealth(asyncResp, service, powerSupplyPath);
4733e42a329SLakshmi Yadlapati     getPowerSupplyAsset(asyncResp, service, powerSupplyPath);
4743e42a329SLakshmi Yadlapati     getPowerSupplyFirmwareVersion(asyncResp, service, powerSupplyPath);
4753e42a329SLakshmi Yadlapati     getPowerSupplyLocation(asyncResp, service, powerSupplyPath);
476ddceee07SGeorge Liu     getEfficiencyPercent(asyncResp);
47700ef5dc6SGeorge Liu }
47800ef5dc6SGeorge Liu 
479bd79bce8SPatrick Williams inline void handlePowerSupplyHead(
480bd79bce8SPatrick Williams     App& app, const crow::Request& req,
48100ef5dc6SGeorge Liu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
482bd79bce8SPatrick Williams     const std::string& chassisId, const std::string& powerSupplyId)
48300ef5dc6SGeorge Liu {
48400ef5dc6SGeorge Liu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
48500ef5dc6SGeorge Liu     {
48600ef5dc6SGeorge Liu         return;
48700ef5dc6SGeorge Liu     }
48800ef5dc6SGeorge Liu 
48900ef5dc6SGeorge Liu     // Get the correct Path and Service that match the input parameters
490bd79bce8SPatrick Williams     getValidPowerSupplyPath(
4913e42a329SLakshmi Yadlapati         asyncResp, chassisId, powerSupplyId,
4923e42a329SLakshmi Yadlapati         [asyncResp](const std::string&, const std::string&) {
49300ef5dc6SGeorge Liu             asyncResp->res.addHeader(
49400ef5dc6SGeorge Liu                 boost::beast::http::field::link,
49500ef5dc6SGeorge Liu                 "</redfish/v1/JsonSchemas/PowerSupply/PowerSupply.json>; rel=describedby");
49600ef5dc6SGeorge Liu         });
49700ef5dc6SGeorge Liu }
49800ef5dc6SGeorge Liu 
499bd79bce8SPatrick Williams inline void handlePowerSupplyGet(
500bd79bce8SPatrick Williams     App& app, const crow::Request& req,
50100ef5dc6SGeorge Liu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
502bd79bce8SPatrick Williams     const std::string& chassisId, const std::string& powerSupplyId)
50300ef5dc6SGeorge Liu {
50400ef5dc6SGeorge Liu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
50500ef5dc6SGeorge Liu     {
50600ef5dc6SGeorge Liu         return;
50700ef5dc6SGeorge Liu     }
5083e42a329SLakshmi Yadlapati     getValidPowerSupplyPath(
5093e42a329SLakshmi Yadlapati         asyncResp, chassisId, powerSupplyId,
51000ef5dc6SGeorge Liu         std::bind_front(doPowerSupplyGet, asyncResp, chassisId, powerSupplyId));
51100ef5dc6SGeorge Liu }
51200ef5dc6SGeorge Liu 
51300ef5dc6SGeorge Liu inline void requestRoutesPowerSupply(App& app)
51400ef5dc6SGeorge Liu {
51500ef5dc6SGeorge Liu     BMCWEB_ROUTE(
51600ef5dc6SGeorge Liu         app, "/redfish/v1/Chassis/<str>/PowerSubsystem/PowerSupplies/<str>/")
51700ef5dc6SGeorge Liu         .privileges(redfish::privileges::headPowerSupply)
51800ef5dc6SGeorge Liu         .methods(boost::beast::http::verb::head)(
51900ef5dc6SGeorge Liu             std::bind_front(handlePowerSupplyHead, std::ref(app)));
52000ef5dc6SGeorge Liu 
52100ef5dc6SGeorge Liu     BMCWEB_ROUTE(
52200ef5dc6SGeorge Liu         app, "/redfish/v1/Chassis/<str>/PowerSubsystem/PowerSupplies/<str>/")
52300ef5dc6SGeorge Liu         .privileges(redfish::privileges::getPowerSupply)
52400ef5dc6SGeorge Liu         .methods(boost::beast::http::verb::get)(
52500ef5dc6SGeorge Liu             std::bind_front(handlePowerSupplyGet, std::ref(app)));
52600ef5dc6SGeorge Liu }
52700ef5dc6SGeorge Liu 
528a7210020SGeorge Liu } // namespace redfish
529