All sensors should return a target speed value

All tach sensors associated with a fan should return a target speed
sensor from its getTarget function. In the case where a target speed
sensor does not exist for the tach sensor, it retrieves and returns the
target speed value from the fan where the fan finds the target speed
value from a tach sensor the fan contains that provides it.

Resolves openbmc/openbmc#2784

Change-Id: Iea5561b0aad6942be52af262c7255c60e5e75c7a
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/monitor/fan.cpp b/monitor/fan.cpp
index a6af74a..f24312d 100644
--- a/monitor/fan.cpp
+++ b/monitor/fan.cpp
@@ -134,34 +134,32 @@
 }
 
 
-uint64_t Fan::getTargetSpeed(const TachSensor& sensor)
+uint64_t Fan::findTargetSpeed()
 {
     uint64_t target = 0;
+    //The sensor doesn't support a target,
+    //so get it from another sensor.
+    auto s = std::find_if(_sensors.begin(), _sensors.end(),
+                          [](const auto& s)
+                          {
+                              return s->hasTarget();
+                          });
 
-    if (sensor.hasTarget())
+    if (s != _sensors.end())
     {
-        target = sensor.getTarget();
-    }
-    else
-    {
-        //The sensor doesn't support a target,
-        //so get it from another sensor.
-        auto s = std::find_if(_sensors.begin(), _sensors.end(),
-                              [](const auto& s)
-                              {
-                                  return s->hasTarget();
-                              });
-
-        if (s != _sensors.end())
-        {
-            target = (*s)->getTarget();
-        }
+        target = (*s)->getTarget();
     }
 
     return target;
 }
 
 
+uint64_t Fan::getTargetSpeed(const TachSensor& sensor)
+{
+    return sensor.getTarget();
+}
+
+
 bool Fan::tooManySensorsNonfunctional()
 {
     size_t numFailed =  std::count_if(_sensors.begin(), _sensors.end(),
diff --git a/monitor/fan.hpp b/monitor/fan.hpp
index f22bd9f..d765650 100644
--- a/monitor/fan.hpp
+++ b/monitor/fan.hpp
@@ -134,15 +134,28 @@
             return _name;
         }
 
+        /**
+         * @brief Finds the target speed of this fan
+         *
+         * Finds the target speed from the list of sensors that make up this
+         * fan. At least one sensor should contain a target speed value.
+         *
+         * @return - The target speed found from the list of sensors on the fan
+         */
+        uint64_t findTargetSpeed();
+
     private:
 
         /**
-         * @brief Returns the target speed of the sensor
+         * @brief Returns the target speed of the fan
          *
-         * If the sensor itself doesn't have a target, it finds
-         * the target speed from another sensor.
+         * Retrieves the target speed using the given sensor which may or may
+         * not contain a target speed value. The sensor determines what its
+         * target speed is.
          *
-         * @param[in] sensor - the sensor to get the target speed for
+         * @param[in] sensor - The sensor to use in getting the target speed
+         *
+         * @return - The target speed of the fan
          */
         uint64_t getTargetSpeed(const TachSensor& sensor);
 
diff --git a/monitor/tach_sensor.cpp b/monitor/tach_sensor.cpp
index 8ae8583..1069313 100644
--- a/monitor/tach_sensor.cpp
+++ b/monitor/tach_sensor.cpp
@@ -126,13 +126,21 @@
 
 }
 
-
 std::string TachSensor::getMatchString(const std::string& interface)
 {
     return sdbusplus::bus::match::rules::propertiesChanged(
             _name, interface);
 }
 
+uint64_t TachSensor::getTarget() const
+{
+    if (!_hasTarget)
+    {
+        return _fan.findTargetSpeed();
+    }
+    return _tachTarget;
+}
+
 void TachSensor::setFunctional(bool functional)
 {
     _functional = functional;
diff --git a/monitor/tach_sensor.hpp b/monitor/tach_sensor.hpp
index ba3465a..4e57ef5 100644
--- a/monitor/tach_sensor.hpp
+++ b/monitor/tach_sensor.hpp
@@ -63,10 +63,7 @@
         /**
          * @brief Returns the target speed value
          */
-        inline uint64_t getTarget() const
-        {
-            return _tachTarget;
-        }
+        uint64_t getTarget() const;
 
         /**
          * @brief Returns the input speed value