pid/zone: Restore PWM when fans returned to auto

This makes use of the improved write() interface, to allow the
PID-loop-determined PWM to be restored, when the fan is returned to
automatic mode.

Without this fix, a fan set to manual mode, then manually set to a
different speed, would not properly return to the correct speed, when
transitioning back to automatic from manual.

This patch also adds a stub to allow the caller to learn the raw PWM
value written as output, another useful write() interface improvement.
Although not the topic of this change, it is included here, to avoid
later patch conflicts.

Tested: I can now correctly toggle between automatic, and manual, fan
control. Upon resuming automatic control, after a few seconds, the fan
PWM is now properly restored, to what the PID loop wanted it to be at.

Signed-off-by: Josh Lehan <krellan@google.com>
Signed-off-by: Jason Ling <jasonling@google.com>
Change-Id: I46fc65d6b931755d51093ea475c64cf5e3e6bacb
diff --git a/pid/fancontroller.cpp b/pid/fancontroller.cpp
index d3c58ca..d7ac0e6 100644
--- a/pid/fancontroller.cpp
+++ b/pid/fancontroller.cpp
@@ -145,7 +145,9 @@
     for (const auto& it : _inputs)
     {
         auto sensor = _owner->getSensor(it);
-        sensor->write(percent);
+        auto redundantWrite = _owner->getRedundantWrite();
+        int64_t rawWritten;
+        sensor->write(percent, redundantWrite, &rawWritten);
     }
 
     return;
diff --git a/pid/zone.cpp b/pid/zone.cpp
index 441031a..af40a2b 100644
--- a/pid/zone.cpp
+++ b/pid/zone.cpp
@@ -82,6 +82,12 @@
 void DbusPidZone::setManualMode(bool mode)
 {
     _manualMode = mode;
+
+    // If returning to automatic mode, need to restore PWM from PID loop
+    if (!mode)
+    {
+        _redundantWrite = true;
+    }
 }
 
 bool DbusPidZone::getFailSafeMode(void) const
@@ -432,6 +438,12 @@
     {
         p->process();
     }
+
+    if (_redundantWrite)
+    {
+        // This is only needed once
+        _redundantWrite = false;
+    }
 }
 
 void DbusPidZone::processThermals(void)
@@ -447,6 +459,11 @@
     return _mgr.getSensor(name);
 }
 
+bool DbusPidZone::getRedundantWrite(void) const
+{
+    return _redundantWrite;
+}
+
 bool DbusPidZone::manual(bool value)
 {
     std::cerr << "manual: " << value << std::endl;
diff --git a/pid/zone.hpp b/pid/zone.hpp
index 3bea9c2..37b49ea 100644
--- a/pid/zone.hpp
+++ b/pid/zone.hpp
@@ -53,6 +53,8 @@
     /* Could put lock around this since it's accessed from two threads, but
      * only one reader/one writer.
      */
+
+    bool getRedundantWrite(void) const override;
     void setManualMode(bool mode);
     bool getFailSafeMode(void) const override;
 
@@ -71,6 +73,7 @@
     void updateSensors(void) override;
     void initializeCache(void) override;
     void dumpCache(void);
+
     void processFans(void) override;
     void processThermals(void) override;
 
@@ -94,6 +97,7 @@
     const int64_t _zoneId;
     double _maximumSetPoint = 0;
     bool _manualMode = false;
+    bool _redundantWrite = false;
     const double _minThermalOutputSetPt;
     const double _failSafePercent;
 
diff --git a/pid/zone_interface.hpp b/pid/zone_interface.hpp
index a024c0e..b8da048 100644
--- a/pid/zone_interface.hpp
+++ b/pid/zone_interface.hpp
@@ -41,6 +41,7 @@
      * starts processing values to control fans.
      */
     virtual void initializeCache(void) = 0;
+
     /** Return cached value for sensor by name. */
     virtual double getCachedValue(const std::string& name) = 0;
 
@@ -76,6 +77,11 @@
      */
     virtual bool getManualMode(void) const = 0;
 
+    /** Returns true if a redundant fan PWM write is needed. Redundant write
+     * is used when returning the fan to automatic mode from manual mode.
+     */
+    virtual bool getRedundantWrite(void) const = 0;
+
     /** For each fan pid, do processing. */
     virtual void processFans(void) = 0;
     /** For each thermal pid, do processing. */