Move sensor adjust within sensor object

All sensor adjustments should be contained within the sensor object for
the sensor. This allows the adjustments to be retrieved for a given
sensor and applied accordingly.

Tested:
    No change in adjusting a sensor value
    No change in removing a sensor given a removal return code

Change-Id: I5e1e40fe41b4064422a47178aec1f297b781566d
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/mainloop.cpp b/mainloop.cpp
index 595432d..02c3ebb 100644
--- a/mainloop.cpp
+++ b/mainloop.cpp
@@ -67,17 +67,6 @@
 decltype(Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
     &CriticalObject::criticalAlarmHigh;
 
-// The gain and offset to adjust a value
-struct valueAdjust
-{
-    double gain = 1.0;
-    int offset = 0;
-    std::unordered_set<int> rmRCs;
-};
-
-// Store the valueAdjust for sensors
-std::map<SensorSet::key_type, valueAdjust> sensorAdjusts;
-
 std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
 {
     std::string id;
@@ -196,24 +185,20 @@
                 hwmon::entry::cinput);
 #ifndef REMOVE_ON_FAIL
         // Check sensorAdjusts for sensor removal RCs
-        const auto& it = sensorAdjusts.find(sensor.first);
-        if (it != sensorAdjusts.end())
+        auto& sAdjusts = sensorObj->getAdjusts();
+        if (sAdjusts.rmRCs.count(e.code().value()) > 0)
         {
-            auto rmRCit = it->second.rmRCs.find(e.code().value());
-            if (rmRCit != std::end(it->second.rmRCs))
+            // Return code found in sensor return code removal list
+            if (rmSensors.find(sensor.first) == rmSensors.end())
             {
-                // Return code found in sensor return code removal list
-                if (rmSensors.find(sensor.first) == rmSensors.end())
-                {
-                    // Trace for sensor not already removed from dbus
-                    log<level::INFO>("Sensor not added to dbus for read fail",
-                            entry("FILE=%s", file.c_str()),
-                            entry("RC=%d", e.code().value()));
-                    rmSensors[std::move(sensor.first)] =
-                            std::move(sensor.second);
-                }
-                return {};
+                // Trace for sensor not already removed from dbus
+                log<level::INFO>("Sensor not added to dbus for read fail",
+                        entry("FILE=%s", file.c_str()),
+                        entry("RC=%d", e.code().value()));
+                rmSensors[std::move(sensor.first)] =
+                        std::move(sensor.second);
             }
+            return {};
         }
 #endif
         using namespace sdbusplus::xyz::openbmc_project::
@@ -469,25 +454,21 @@
                         hwmon::entry::cinput);
 #ifndef REMOVE_ON_FAIL
                 // Check sensorAdjusts for sensor removal RCs
-                const auto& it = sensorAdjusts.find(i.first);
-                if (it != sensorAdjusts.end())
+                auto& sAdjusts = sensorObjects[i.first]->getAdjusts();
+                if (sAdjusts.rmRCs.count(e.code().value()) > 0)
                 {
-                    auto rmRCit = it->second.rmRCs.find(e.code().value());
-                    if (rmRCit != std::end(it->second.rmRCs))
+                    // Return code found in sensor return code removal list
+                    if (rmSensors.find(i.first) == rmSensors.end())
                     {
-                        // Return code found in sensor return code removal list
-                        if (rmSensors.find(i.first) == rmSensors.end())
-                        {
-                            // Trace for sensor not already removed from dbus
-                            log<level::INFO>(
-                                    "Remove sensor from dbus for read fail",
-                                    entry("FILE=%s", file.c_str()),
-                                    entry("RC=%d", e.code().value()));
-                            // Mark this sensor to be removed from dbus
-                            rmSensors[i.first] = std::get<0>(i.second);
-                        }
-                        continue;
+                        // Trace for sensor not already removed from dbus
+                        log<level::INFO>(
+                                "Remove sensor from dbus for read fail",
+                                entry("FILE=%s", file.c_str()),
+                                entry("RC=%d", e.code().value()));
+                        // Mark this sensor to be removed from dbus
+                        rmSensors[i.first] = std::get<0>(i.second);
                     }
+                    continue;
                 }
 #endif
                 using namespace sdbusplus::xyz::openbmc_project::
diff --git a/sensor.cpp b/sensor.cpp
index 155b8bd..6191688 100644
--- a/sensor.cpp
+++ b/sensor.cpp
@@ -23,6 +23,20 @@
     ioAccess(ioAccess),
     devPath(devPath)
 {
+    auto gain = env::getEnv("GAIN", sensor);
+    if (!gain.empty())
+    {
+        sensorAdjusts.gain = std::stod(gain);
+    }
+
+    auto offset = env::getEnv("OFFSET", sensor);
+    if (!offset.empty())
+    {
+        sensorAdjusts.offset = std::stoi(offset);
+    }
+    auto senRmRCs = env::getEnv("REMOVERCS", sensor);
+    // Add sensor removal return codes defined per sensor
+    addRemoveRCs(senRmRCs);
 }
 
 void Sensor::addRemoveRCs(const std::string& rcList)
@@ -172,7 +186,6 @@
         }
         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::
diff --git a/sensor.hpp b/sensor.hpp
index 565fd01..9d48483 100644
--- a/sensor.hpp
+++ b/sensor.hpp
@@ -52,6 +52,16 @@
         void addRemoveRCs(const std::string& rcList);
 
         /**
+         * @brief Get the adjustments struct for the sensor
+         *
+         * @return - Sensor adjustment struct
+         */
+        inline const valueAdjust& getAdjusts()
+        {
+            return sensorAdjusts;
+        }
+
+        /**
          * @brief Adjusts a sensor value
          * @details Adjusts the value given by any gain and/or offset defined
          * for this sensor object and returns that adjusted value.