Add basic PowerControl and PowerLimit properties

Add code in the power-specific response handler to fetch the Power Limit
value for the chassis that implements the Chassis inventory item. Add a
special case to the generic sensor handling code to place the
total_power value into the PowerControl PowerConsumedWatts field.

curl -k https://${bmc}/redfish/v1/Chassis/chassis/Power
{
  "@odata.context": "/redfish/v1/$metadata#Power.Power",
  "@odata.id": "/redfish/v1/Chassis/chassis/Power",
  "@odata.type": "#Power.v1_5_2.Power",
  "Id": "Power",
  "Name": "Power",
  "PowerControl": [
    {
      "@odata.id": "/redfish/v1/Chassis/chassis/Power#/PowerControl/",
      "MemberId": "total_power",
      "Name": "total power",
      "PowerConsumedWatts": 269.0,
      "Status": {
        "Health": "OK",
        "State": "Enabled"
      }
    }
  ],
  "PowerLimit": [
    {
      "LimitInWatts": null
    }
  ],

Signed-off-by: Eddie James <eajames@linux.ibm.com>
Change-Id: I447de59fb44a4ecbe7b47610d915ac22aef90250
diff --git a/redfish-core/lib/power.hpp b/redfish-core/lib/power.hpp
index 2184114..b5951d5 100644
--- a/redfish-core/lib/power.hpp
+++ b/redfish-core/lib/power.hpp
@@ -54,8 +54,163 @@
 
         auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
             res, chassis_name, typeList, "Power");
-        // TODO Need to retrieve Power Control information.
+
         getChassisData(sensorAsyncResp);
+
+        // This callback verifies that the power limit is only provided for the
+        // chassis that implements the Chassis inventory item. This prevents
+        // things like power supplies providing the chassis power limit
+        auto chassisHandler = [sensorAsyncResp](
+                                  const boost::system::error_code ec,
+                                  const std::vector<std::string>&
+                                      chassisPaths) {
+            if (ec)
+            {
+                BMCWEB_LOG_ERROR
+                    << "Power Limit GetSubTreePaths handler Dbus error " << ec;
+                return;
+            }
+
+            bool found = false;
+            for (const std::string& chassis : chassisPaths)
+            {
+                size_t len = std::string::npos;
+                size_t lastPos = chassis.rfind("/");
+                if (lastPos == std::string::npos)
+                {
+                    continue;
+                }
+
+                if (lastPos == chassis.size() - 1)
+                {
+                    size_t end = lastPos;
+                    lastPos = chassis.rfind("/", lastPos - 1);
+                    if (lastPos == std::string::npos)
+                    {
+                        continue;
+                    }
+
+                    len = end - (lastPos + 1);
+                }
+
+                std::string interfaceChassisName =
+                    chassis.substr(lastPos + 1, len);
+                if (!interfaceChassisName.compare(sensorAsyncResp->chassisId))
+                {
+                    found = true;
+                    break;
+                }
+            }
+
+            if (!found)
+            {
+                BMCWEB_LOG_DEBUG << "Power Limit not present for "
+                                 << sensorAsyncResp->chassisId;
+                return;
+            }
+
+            auto valueHandler =
+                [sensorAsyncResp](
+                    const boost::system::error_code ec,
+                    const std::vector<std::pair<std::string, SensorVariant>>&
+                        properties) {
+                    if (ec)
+                    {
+                        messages::internalError(sensorAsyncResp->res);
+                        BMCWEB_LOG_ERROR
+                            << "Power Limit GetAll handler: Dbus error " << ec;
+                        return;
+                    }
+
+                    nlohmann::json& tempArray =
+                        sensorAsyncResp->res.jsonValue["PowerLimit"];
+
+                    if (tempArray.empty())
+                    {
+                        tempArray.push_back({});
+                    }
+
+                    nlohmann::json& sensorJson = tempArray.back();
+                    bool enabled = false;
+                    double powerCap = 0.0;
+                    int64_t scale = 0;
+
+                    for (const std::pair<std::string, SensorVariant>& property :
+                         properties)
+                    {
+                        if (!property.first.compare("Scale"))
+                        {
+                            const int64_t* i =
+                                sdbusplus::message::variant_ns::get_if<int64_t>(
+                                    &property.second);
+
+                            if (i)
+                            {
+                                scale = *i;
+                            }
+                        }
+                        else if (!property.first.compare("PowerCap"))
+                        {
+                            const double* d =
+                                sdbusplus::message::variant_ns::get_if<double>(
+                                    &property.second);
+                            const int64_t* i =
+                                sdbusplus::message::variant_ns::get_if<int64_t>(
+                                    &property.second);
+                            const uint32_t* u =
+                                sdbusplus::message::variant_ns::get_if<
+                                    uint32_t>(&property.second);
+
+                            if (d)
+                            {
+                                powerCap = *d;
+                            }
+                            else if (i)
+                            {
+                                powerCap = *i;
+                            }
+                            else if (u)
+                            {
+                                powerCap = *u;
+                            }
+                        }
+                        else if (!property.first.compare("PowerCapEnable"))
+                        {
+                            const bool* b =
+                                sdbusplus::message::variant_ns::get_if<bool>(
+                                    &property.second);
+
+                            if (b)
+                            {
+                                enabled = *b;
+                            }
+                        }
+                    }
+
+                    nlohmann::json& value = sensorJson["LimitInWatts"];
+
+                    if (enabled)
+                    {
+                        // Redfish specification indicates PowerLimit should be
+                        // null if the limit is not enabled.
+                        value = powerCap * std::pow(10, scale);
+                    }
+                };
+
+            crow::connections::systemBus->async_method_call(
+                std::move(valueHandler), "xyz.openbmc_project.Settings",
+                "/xyz/openbmc_project/control/host0/power_cap",
+                "org.freedesktop.DBus.Properties", "GetAll",
+                "xyz.openbmc_project.Control.Power.Cap");
+        };
+
+        crow::connections::systemBus->async_method_call(
+            std::move(chassisHandler), "xyz.openbmc_project.ObjectMapper",
+            "/xyz/openbmc_project/object_mapper",
+            "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
+            "/xyz/openbmc_project/inventory", int32_t(0),
+            std::array<const char*, 1>{
+                "xyz.openbmc_project.Inventory.Item.Chassis"});
     }
     void doPatch(crow::Response& res, const crow::Request& req,
                  const std::vector<std::string>& params) override
diff --git a/redfish-core/lib/sensors.hpp b/redfish-core/lib/sensors.hpp
index c44ff71..86e5a07 100644
--- a/redfish-core/lib/sensors.hpp
+++ b/redfish-core/lib/sensors.hpp
@@ -32,7 +32,7 @@
     std::pair<std::string,
               std::vector<std::pair<std::string, std::vector<std::string>>>>>;
 
-using SensorVariant = std::variant<int64_t, double>;
+using SensorVariant = std::variant<int64_t, double, uint32_t, bool>;
 
 using ManagedObjectsVectorType = std::vector<std::pair<
     sdbusplus::message::object_path,
@@ -484,7 +484,11 @@
         std::string sensorNameLower =
             boost::algorithm::to_lower_copy(sensorName);
 
-        if (sensorNameLower.find("input") != std::string::npos)
+        if (!sensorName.compare("total_power"))
+        {
+            unit = "PowerConsumedWatts";
+        }
+        else if (sensorNameLower.find("input") != std::string::npos)
         {
             unit = "PowerInputWatts";
         }
@@ -550,6 +554,7 @@
                 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
 
                 const double* doubleValue = std::get_if<double>(&valueVariant);
+                const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
                 double temp = 0.0;
                 if (int64Value != nullptr)
                 {
@@ -559,6 +564,10 @@
                 {
                     temp = *doubleValue;
                 }
+                else if (uValue != nullptr)
+                {
+                    temp = *uValue;
+                }
                 else
                 {
                     BMCWEB_LOG_ERROR
@@ -920,7 +929,14 @@
                 }
                 else if (sensorType == "power")
                 {
-                    fieldName = "PowerSupplies";
+                    if (!sensorName.compare("total_power"))
+                    {
+                        fieldName = "PowerControl";
+                    }
+                    else
+                    {
+                        fieldName = "PowerSupplies";
+                    }
                 }
                 else
                 {