sensor: Always add status interface, track fault file with class member

Always add the status interface regardless of whether there is a
fault file or not and update the functional property accordingly.

Added a new sensor class member to track the presence of fault file.

Resolves: openbmc/phosphor-hwmon#10
Signed-off-by: Brandon Kim <brandonkim@google.com>
Change-Id: I4480541691ccfa853d85151965428c564db8ba52
diff --git a/mainloop.cpp b/mainloop.cpp
index a5a818f..6908d78 100644
--- a/mainloop.cpp
+++ b/mainloop.cpp
@@ -377,31 +377,35 @@
                 int64_t value;
                 auto& objInfo = std::get<ObjectInfo>(i.second);
                 auto& obj = std::get<InterfaceMap>(objInfo);
+                std::unique_ptr<sensor::Sensor>& sensor =
+                    _sensorObjects[i.first];
 
-                auto it = obj.find(InterfaceType::STATUS);
-                if (it != obj.end())
+                auto& statusIface =
+                    std::any_cast<std::shared_ptr<StatusObject>&>(
+                        obj[InterfaceType::STATUS]);
+                // As long as addStatus is called before addValue, statusIface
+                // should never be nullptr.
+                assert(statusIface);
+
+                if (sensor->hasFaultFile())
                 {
                     auto fault = _ioAccess->read(
                         i.first.first, i.first.second, hwmon::entry::fault,
                         hwmonio::retries, hwmonio::delay);
-                    auto statusIface =
-                        std::any_cast<std::shared_ptr<StatusObject>>(
-                            it->second);
+                    // Skip reading from a sensor with a valid fault file
+                    // and set the functional property accordingly
                     if (!statusIface->functional((fault == 0) ? true : false))
                     {
                         continue;
                     }
                 }
 
-                // Retry for up to a second if device is busy
-                // or has a transient error.
-                std::unique_ptr<sensor::Sensor>& sensor =
-                    _sensorObjects[i.first];
-
                 {
                     // RAII object for GPIO unlock / lock
                     sensor::GpioLock gpioLock(sensor->getGpio());
 
+                    // Retry for up to a second if device is busy
+                    // or has a transient error.
                     value =
                         _ioAccess->read(i.first.first, i.first.second, input,
                                         hwmonio::retries, hwmonio::delay);