Check TARGET_MODE on target sensors

Move the TARGET_MODE config entry to be done on target specific sensors.

Tested:
    Target sensors are still created for the mode given in TARGET_MODE

Change-Id: I7731b73e14495360ccb5b8fb8ada59176e9125d3
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/targets.hpp b/targets.hpp
index ecef77b..3c6dc0d 100644
--- a/targets.hpp
+++ b/targets.hpp
@@ -101,42 +101,77 @@
                                            entry);
     if (fs::exists(sysfsFullPath))
     {
-        uint32_t targetSpeed = 0;
-
-        try
+        auto useTarget = true;
+        auto tmEnv = getenv("TARGET_MODE");
+        if (tmEnv)
         {
-            targetSpeed = ioAccess.read(
-                targetName,
-                targetId,
-                entry,
-                sysfs::hwmonio::retries,
-                sysfs::hwmonio::delay);
-        }
-        catch (const std::system_error& e)
-        {
-            using namespace phosphor::logging;
-            using namespace sdbusplus::xyz::openbmc_project::
-                Sensor::Device::Error;
-            using metadata = xyz::openbmc_project::Sensor::
-                Device::ReadFailure;
+            std::string mode{tmEnv};
+            std::transform(mode.begin(), mode.end(), mode.begin(), toupper);
 
-            report<ReadFailure>(
-                    metadata::CALLOUT_ERRNO(e.code().value()),
-                    metadata::CALLOUT_DEVICE_PATH(devPath.c_str()));
-
-            log<level::INFO>("Logging failing sysfs file",
-                    phosphor::logging::entry(
-                            "FILE=%s", sysfsFullPath.c_str()));
+            if (mode == RPM_TARGET)
+            {
+                if (type != InterfaceType::FAN_SPEED)
+                {
+                    useTarget = false;
+                }
+            }
+            else if (mode == PWM_TARGET)
+            {
+                if (type != InterfaceType::FAN_PWM)
+                {
+                    useTarget = false;
+                }
+            }
+            else
+            {
+                using namespace phosphor::logging;
+                log<level::ERR>("Invalid TARGET_MODE env var found",
+                        phosphor::logging::entry(
+                                "TARGET_MODE=%s", tmEnv),
+                        phosphor::logging::entry(
+                                "DEVPATH=%s", devPath.c_str()));
+            }
         }
 
-        target = std::make_shared<T>(ioAccess.path(),
-                                     devPath,
-                                     targetId,
-                                     bus,
-                                     objPath.c_str(),
-                                     deferSignals,
-                                     targetSpeed);
-        obj[type] = target;
+        if (useTarget)
+        {
+            uint32_t targetSpeed = 0;
+
+            try
+            {
+                targetSpeed = ioAccess.read(
+                    targetName,
+                    targetId,
+                    entry,
+                    sysfs::hwmonio::retries,
+                    sysfs::hwmonio::delay);
+            }
+            catch (const std::system_error& e)
+            {
+                using namespace phosphor::logging;
+                using namespace sdbusplus::xyz::openbmc_project::
+                    Sensor::Device::Error;
+                using metadata = xyz::openbmc_project::Sensor::
+                    Device::ReadFailure;
+
+                report<ReadFailure>(
+                        metadata::CALLOUT_ERRNO(e.code().value()),
+                        metadata::CALLOUT_DEVICE_PATH(devPath.c_str()));
+
+                log<level::INFO>("Logging failing sysfs file",
+                        phosphor::logging::entry(
+                                "FILE=%s", sysfsFullPath.c_str()));
+            }
+
+            target = std::make_shared<T>(ioAccess.path(),
+                                         devPath,
+                                         targetId,
+                                         bus,
+                                         objPath.c_str(),
+                                         deferSignals,
+                                         targetSpeed);
+            obj[type] = target;
+        }
     }
 
     return target;