Implement fan PWM reading through redfish

This patchset implements the ability to be able to read the PWM output
values from redfish under the Thermal schema. to be able to map fan
outputs to redfish.

Change-Id: I858d81ef61183722390a073fa925416d17da5fec
Signed-off-by: Ed Tanous <ed.tanous@intel.com>
diff --git a/redfish-core/lib/sensors.hpp b/redfish-core/lib/sensors.hpp
index c390cd7..da05fd6 100644
--- a/redfish-core/lib/sensors.hpp
+++ b/redfish-core/lib/sensors.hpp
@@ -307,6 +307,13 @@
         sensor_json["@odata.type"] = "#Thermal.v1_3_0.Fan";
         forceToInt = true;
     }
+    else if (sensorType == "fan_pwm")
+    {
+        unit = "Reading";
+        sensor_json["ReadingUnits"] = "Percent";
+        sensor_json["@odata.type"] = "#Thermal.v1_3_0.Fan";
+        forceToInt = true;
+    }
     else if (sensorType == "voltage")
     {
         unit = "ReadingVolts";
@@ -357,41 +364,35 @@
             {
                 const SensorVariant& valueVariant = valueIt->second;
                 nlohmann::json& valueIt = sensor_json[std::get<2>(p)];
-
                 // Attempt to pull the int64 directly
                 const int64_t* int64Value =
                     mapbox::getPtr<const int64_t>(valueVariant);
 
-                if (int64Value != nullptr)
-                {
-                    if (forceToInt || scaleMultiplier >= 0)
-                    {
-                        valueIt = *int64Value * std::pow(10, scaleMultiplier);
-                    }
-                    else
-                    {
-                        valueIt =
-                            *int64Value *
-                            std::pow(10, static_cast<double>(scaleMultiplier));
-                    }
-                }
-                // Attempt to pull the float directly
                 const double* doubleValue =
                     mapbox::getPtr<const double>(valueVariant);
-
-                if (doubleValue != nullptr)
+                double temp = 0.0;
+                if (int64Value != nullptr)
                 {
-                    if (!forceToInt)
-                    {
-                        valueIt =
-                            *doubleValue *
-                            std::pow(10, static_cast<double>(scaleMultiplier));
-                    }
-                    else
-                    {
-                        valueIt = static_cast<int64_t>(
-                            *doubleValue * std::pow(10, scaleMultiplier));
-                    }
+                    temp = *int64Value;
+                }
+                else if (doubleValue != nullptr)
+                {
+                    temp = *doubleValue;
+                }
+                else
+                {
+                    BMCWEB_LOG_ERROR
+                        << "Got value interface that wasn't int or double";
+                    continue;
+                }
+                temp = temp * std::pow(10, scaleMultiplier);
+                if (forceToInt)
+                {
+                    valueIt = static_cast<int64_t>(temp);
+                }
+                else
+                {
+                    valueIt = temp;
                 }
             }
         }
@@ -477,7 +478,8 @@
                                     fieldName = "Temperatures";
                                 }
                                 else if (sensorType == "fan" ||
-                                         sensorType == "fan_tach")
+                                         sensorType == "fan_tach" ||
+                                         sensorType == "fan_pwm")
                                 {
                                     fieldName = "Fans";
                                 }
@@ -504,18 +506,14 @@
                                 nlohmann::json& tempArray =
                                     SensorsAsyncResp->res.jsonValue[fieldName];
 
-                                // Create the array if it doesn't yet exist
-                                if (tempArray.is_array() == false)
-                                {
-                                    tempArray = nlohmann::json::array();
-                                }
-
                                 tempArray.push_back(
                                     {{"@odata.id",
                                       "/redfish/v1/Chassis/" +
                                           SensorsAsyncResp->chassisId +
-                                          "/Thermal#/" + sensorName}});
+                                          "/Thermal#/" + fieldName + "/" +
+                                          std::to_string(tempArray.size())}});
                                 nlohmann::json& sensorJson = tempArray.back();
+
                                 objectInterfacesToJson(sensorName, sensorType,
                                                        objDictEntry.second,
                                                        sensorJson);
diff --git a/redfish-core/lib/thermal.hpp b/redfish-core/lib/thermal.hpp
index 37b0f43..a845385 100644
--- a/redfish-core/lib/thermal.hpp
+++ b/redfish-core/lib/thermal.hpp
@@ -58,7 +58,8 @@
             res, chassisName,
             std::initializer_list<const char*>{
                 "/xyz/openbmc_project/sensors/fan",
-                "/xyz/openbmc_project/sensors/temperature"});
+                "/xyz/openbmc_project/sensors/temperature",
+                "/xyz/openbmc_project/sensors/fan_pwm"});
         getChassisData(asyncResp);
     }
 };