Add logging for Fan Presence / Redundancy

Add logging and fix bug with shared_ptr usage. We
now use a std::optional * to the shared redundancy
object, so that it can 1. point to object / null and 2.
exist in config or not.

Tested: cat /var/log/redfish to see logs made by removing
and adding fans, and using sensor override to change
redundancy.

Change-Id: Ic4b319cf7484cbbd7ce7dbdf7556f48bebe11cb0
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/src/TachSensor.cpp b/src/TachSensor.cpp
index 03ff4a5..295c7e6 100644
--- a/src/TachSensor.cpp
+++ b/src/TachSensor.cpp
@@ -35,7 +35,7 @@
                        sdbusplus::asio::object_server& objectServer,
                        std::shared_ptr<sdbusplus::asio::connection>& conn,
                        std::unique_ptr<PresenceSensor>&& presenceSensor,
-                       const std::shared_ptr<RedundancySensor>& redundancy,
+                       std::optional<RedundancySensor>* redundancy,
                        boost::asio::io_service& io, const std::string& fanName,
                        std::vector<thresholds::Threshold>&& _thresholds,
                        const std::string& sensorConfiguration,
@@ -187,17 +187,18 @@
 void TachSensor::checkThresholds(void)
 {
     bool status = thresholds::checkThresholds(this);
-    if (redundancy)
+    if (redundancy && *redundancy)
     {
-        redundancy->update("/xyz/openbmc_project/sensors/fan_tach/" + name,
-                           !status);
+        (*redundancy)
+            ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
     }
 }
 
 PresenceSensor::PresenceSensor(const size_t index, bool inverted,
-                               boost::asio::io_service& io) :
+                               boost::asio::io_service& io,
+                               const std::string& name) :
     inverted(inverted),
-    inputDev(io)
+    inputDev(io), name(name)
 {
     // todo: use gpiodaemon
     std::string device = gpioPath + std::string("gpio") + std::to_string(index);
@@ -267,7 +268,18 @@
         {
             value = !value;
         }
-        status = value;
+        if (value != status)
+        {
+            status = value;
+            if (status)
+            {
+                logFanInserted(name);
+            }
+            else
+            {
+                logFanRemoved(name);
+            }
+        }
     }
 }
 
@@ -305,7 +317,7 @@
     statuses[name] = failed;
     size_t failedCount = 0;
 
-    std::string state = "Full";
+    std::string newState = redundancy::full;
     for (const auto& status : statuses)
     {
         if (status.second)
@@ -314,13 +326,25 @@
         }
         if (failedCount > count)
         {
-            state = "Failed";
+            newState = redundancy::failed;
             break;
         }
         else if (failedCount)
         {
-            state = "Degraded";
+            newState = redundancy::degraded;
         }
     }
-    iface->set_property("Status", state);
+    if (state != newState)
+    {
+        if (state == redundancy::full)
+        {
+            logFanRedundancyLost();
+        }
+        else if (newState == redundancy::full)
+        {
+            logFanRedundancyRestored();
+        }
+        state = newState;
+        iface->set_property("Status", state);
+    }
 }