Add efficiency percent for PowerSupply

This commit is to add EfficiencyRatings/EfficiencyPercent information
according to the Redfish PowerSupply schema.

In the PDI model, it is assumed that all power supplies have the same
Efficiency Rating. This implementation uses the
xyz.openbmc_project.Control.PowerSupplyAttributes interface for all
power supplies, similar to power.hpp.

ref: http://redfish.dmtf.org/schemas/v1/PowerSupply.v1_5_0.json

Tested: Validator passes
curl -k -H "X-Auth-Token: $token" -X GET
https://${bmc}/redfish/v1/Chassis/chassis/PowerSubsystem/
PowerSupplies/powersupply0
{
"@odata.id": "/redfish/v1/Chassis/chassis/PowerSubsystem/
              PowerSupplies/powersupply0",
"@odata.type": "#PowerSupply.v1_5_0.PowerSupply",
"EfficiencyRatings": [
{
"EfficiencyPercent": 90
}
],
"FirmwareVersion": "383239323732",
"Id": "powersupply0",
"Location": {
"PartLocation": {
"ServiceLabel": "U78DA.ND0.WZS00F6-E0"
}
},
"Manufacturer": "",
"Model": "51E9",
"Name": "powersupply0",
"PartNumber": "03KP466",
"SerialNumber": "YL10KY26E073",
"SparePartNumber": "03FP378",
"Status": {
"Health": "OK",
"State": "Enabled"
}
}

Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: Ic782676f9efb8434f1076e726ff0656d1c27d310
Signed-off-by: Lakshmi Yadlapati <lakshmiy@us.ibm.com>
diff --git a/redfish-core/lib/power_supply.hpp b/redfish-core/lib/power_supply.hpp
index 4a766d3..09f731b 100644
--- a/redfish-core/lib/power_supply.hpp
+++ b/redfish-core/lib/power_supply.hpp
@@ -365,6 +365,89 @@
         });
 }
 
+inline void handleGetEfficiencyResponse(
+    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+    const boost::system::error_code& ec, uint32_t value)
+{
+    if (ec)
+    {
+        if (ec.value() != EBADR)
+        {
+            BMCWEB_LOG_ERROR << "DBUS response error for DeratingFactor "
+                             << ec.value();
+            messages::internalError(asyncResp->res);
+        }
+        return;
+    }
+    // The PDI default value is 0, if it hasn't been set leave off
+    if (value == 0)
+    {
+        return;
+    }
+
+    nlohmann::json::array_t efficiencyRatings;
+    nlohmann::json::object_t efficiencyPercent;
+    efficiencyPercent["EfficiencyPercent"] = value;
+    efficiencyRatings.emplace_back(std::move(efficiencyPercent));
+    asyncResp->res.jsonValue["EfficiencyRatings"] =
+        std::move(efficiencyRatings);
+}
+
+inline void handlePowerSupplyAttributesSubTreeResponse(
+    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+    const boost::system::error_code& ec,
+    const dbus::utility::MapperGetSubTreeResponse& subtree)
+{
+    if (ec)
+    {
+        if (ec.value() != EBADR)
+        {
+            BMCWEB_LOG_ERROR << "DBUS response error for EfficiencyPercent "
+                             << ec.value();
+            messages::internalError(asyncResp->res);
+        }
+        return;
+    }
+
+    if (subtree.empty())
+    {
+        BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
+        return;
+    }
+
+    if (subtree.size() != 1)
+    {
+        BMCWEB_LOG_ERROR
+            << "Unexpected number of paths returned by getSubTree: "
+            << subtree.size();
+        messages::internalError(asyncResp->res);
+        return;
+    }
+
+    const auto& [path, serviceMap] = *subtree.begin();
+    const auto& [service, interfaces] = *serviceMap.begin();
+    sdbusplus::asio::getProperty<uint32_t>(
+        *crow::connections::systemBus, service, path,
+        "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
+        [asyncResp](const boost::system::error_code& ec1, uint32_t value) {
+        handleGetEfficiencyResponse(asyncResp, ec1, value);
+        });
+}
+
+inline void
+    getEfficiencyPercent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
+{
+    constexpr std::array<std::string_view, 1> efficiencyIntf = {
+        "xyz.openbmc_project.Control.PowerSupplyAttributes"};
+
+    dbus::utility::getSubTree(
+        "/xyz/openbmc_project", 0, efficiencyIntf,
+        [asyncResp](const boost::system::error_code& ec,
+                    const dbus::utility::MapperGetSubTreeResponse& subtree) {
+        handlePowerSupplyAttributesSubTreeResponse(asyncResp, ec, subtree);
+        });
+}
+
 inline void
     doPowerSupplyGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
                      const std::string& chassisId,
@@ -417,6 +500,8 @@
             getPowerSupplyLocation(asyncResp, object.begin()->first,
                                    powerSupplyPath);
             });
+
+        getEfficiencyPercent(asyncResp);
     });
 }