meta-quanta: gbs: fix includes error and add flag explictly

1. Remake the patch to fix includes error:

- Implement sensor "ASYNC_READ_TIMEOUT"
  https://gerrit.openbmc-project.xyz/24337

- Add power on monitor mechanism
  https://gerrit.openbmc-project.xyz/28181

2. Set negative-errno-on-fail flag as false
   explicitly, otherwise it would be true
   conflicts with update-functional-on-fail

Signed-off-by: George Hung <george.hung@quantatw.com>
Change-Id: Id550bc5261e6fa7218e2ff2731e1af51487ee376
diff --git a/meta-quanta/meta-gbs/recipes-phosphor/sensors/phosphor-hwmon/0002-lev-add-sensors-slow-readings.patch b/meta-quanta/meta-gbs/recipes-phosphor/sensors/phosphor-hwmon/0001-sensor-Implement-sensor-ASYNC_READ_TIMEOUT.patch
similarity index 84%
rename from meta-quanta/meta-gbs/recipes-phosphor/sensors/phosphor-hwmon/0002-lev-add-sensors-slow-readings.patch
rename to meta-quanta/meta-gbs/recipes-phosphor/sensors/phosphor-hwmon/0001-sensor-Implement-sensor-ASYNC_READ_TIMEOUT.patch
index 0ab2f20..88f55d0 100644
--- a/meta-quanta/meta-gbs/recipes-phosphor/sensors/phosphor-hwmon/0002-lev-add-sensors-slow-readings.patch
+++ b/meta-quanta/meta-gbs/recipes-phosphor/sensors/phosphor-hwmon/0001-sensor-Implement-sensor-ASYNC_READ_TIMEOUT.patch
@@ -1,18 +1,56 @@
-From 8eafa1b0513dd2f5898182487b4524a485bf1e21 Mon Sep 17 00:00:00 2001
-From: Eddielu <Eddie.Lu@quantatw.com>
-Date: Mon, 27 Jul 2020 20:54:28 +0800
-Subject: [PATCH] Update add sensors slow readings patch.
+From c1b3d32430df12ab01f9cacf132aedff8324d55b Mon Sep 17 00:00:00 2001
+From: Brandon Kim <brandonkim@google.com>
+Date: Fri, 9 Aug 2019 15:38:53 -0700
+Subject: [PATCH] sensor: Implement sensor "ASYNC_READ_TIMEOUT"
 
+This change will prevent sensors from blocking all other sensor reads
+and D-Bus if they do not report failures quickly enough.
+
+If "ASYNC_READ_TIMEOUT" environment variable is defined in the
+sensor's config file for a key_type, the sensor read will be
+asynchronous with timeout set in milliseconds.
+
+For example for "sensor1":
+ASYNC_READ_TIMEOUT_sensor1="1000"  // Timeout will be set to 1 sec
+
+If the read times out, the sensor read will be skipped and the
+sensor's functional property will be set to 'false'. Timed out futures
+will be placed in a map to prevent their destructor from running and
+blocking until the read completes (limiation of std::async).
+
+Change-Id: I3d9ed4d5c9cc87d3196fc281451834f3001d0b48
+Signed-off-by: Brandon Kim <brandonkim@google.com>
 ---
- mainloop.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----
+ Makefile.am  |  2 ++
+ mainloop.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++++++---
  mainloop.hpp |  3 +++
  meson.build  |  1 +
- sensor.cpp   | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------
- sensor.hpp   | 20 ++++++++++++++--
- 5 files changed, 158 insertions(+), 14 deletions(-)
+ sensor.cpp   | 75 ++++++++++++++++++++++++++++++++++++++++++++++------
+ sensor.hpp   | 20 ++++++++++++--
+ 6 files changed, 160 insertions(+), 14 deletions(-)
 
+diff --git a/Makefile.am b/Makefile.am
+index 706a6cc..c620fa4 100644
+--- a/Makefile.am
++++ b/Makefile.am
+@@ -46,6 +46,7 @@ libhwmon_la_LIBADD = \
+ 	$(SDEVENTPLUS_LIBS) \
+ 	$(PHOSPHOR_DBUS_INTERFACES_LIBS) \
+ 	$(PHOSPHOR_LOGGING_LIBS) \
++	$(PTHREAD_LIBS) \
+ 	$(GPIOPLUS_LIBS) \
+ 	$(STDPLUS_LIBS) \
+ 	$(CODE_COVERAGE_LIBS) \
+@@ -55,6 +56,7 @@ libhwmon_la_CXXFLAGS = \
+ 	$(SDEVENTPLUS_CFLAGS) \
+ 	$(PHOSPHOR_DBUS_INTERFACES_CFLAGS) \
+ 	$(PHOSPHOR_LOGGING_CFLAGS) \
++	$(PTHREAD_CFLAGS) \
+ 	$(STDPLUS_CFLAGS) \
+ 	$(CODE_COVERAGE_CXXFLAGS)
+ 
 diff --git a/mainloop.cpp b/mainloop.cpp
-index 4789a80..98d0658 100644
+index ecceee5..29dc26a 100644
 --- a/mainloop.cpp
 +++ b/mainloop.cpp
 @@ -34,6 +34,7 @@
@@ -23,7 +61,7 @@
  #include <iostream>
  #include <memory>
  #include <phosphor-logging/elog-errors.hpp>
-@@ -299,7 +300,7 @@ std::optional<ObjectStateData>
+@@ -242,7 +243,7 @@ std::optional<ObjectStateData>
      {
          // Add status interface based on _fault file being present
          sensorObj->addStatus(info);
@@ -32,7 +70,7 @@
      }
      catch (const std::system_error& e)
      {
-@@ -542,10 +543,74 @@ void MainLoop::read()
+@@ -478,10 +479,74 @@ void MainLoop::read()
                  // RAII object for GPIO unlock / lock
                  auto locker = sensor::gpioUnlock(sensor->getGpio());
  
@@ -144,7 +182,7 @@
      link_with: [
          libaverage,
 diff --git a/sensor.cpp b/sensor.cpp
-index b1cb470..72b45f8 100644
+index 09aeca6..ac2f896 100644
 --- a/sensor.cpp
 +++ b/sensor.cpp
 @@ -15,6 +15,7 @@
@@ -155,7 +193,7 @@
  #include <phosphor-logging/elog-errors.hpp>
  #include <thread>
  #include <xyz/openbmc_project/Common/error.hpp>
-@@ -125,8 +126,9 @@ SensorValueType Sensor::adjustValue(SensorValueType value)
+@@ -116,8 +117,9 @@ SensorValueType Sensor::adjustValue(SensorValueType value)
      return value;
  }
  
@@ -167,7 +205,7 @@
  {
      static constexpr bool deferSignals = true;
  
-@@ -153,12 +155,69 @@ std::shared_ptr<ValueObject> Sensor::addValue(const RetryIO& retryIO,
+@@ -144,12 +146,69 @@ std::shared_ptr<ValueObject> Sensor::addValue(const RetryIO& retryIO,
              // RAII object for GPIO unlock / lock
              auto locker = gpioUnlock(getGpio());
  
@@ -244,7 +282,7 @@
  #ifdef UPDATE_FUNCTIONAL_ON_FAIL
          catch (const std::system_error& e)
 diff --git a/sensor.hpp b/sensor.hpp
-index 369a252..41c0fe7 100644
+index 4b2d281..64d6e48 100644
 --- a/sensor.hpp
 +++ b/sensor.hpp
 @@ -4,6 +4,8 @@
@@ -291,5 +329,5 @@
      /**
       * @brief Add status interface and functional property for sensor
 -- 
-2.7.4
+2.21.0
 
diff --git a/meta-quanta/meta-gbs/recipes-phosphor/sensors/phosphor-hwmon/0001-lev-add-poweron-monitor-feature.patch b/meta-quanta/meta-gbs/recipes-phosphor/sensors/phosphor-hwmon/0002-Add-power-on-monitor-mechanism.patch
similarity index 62%
rename from meta-quanta/meta-gbs/recipes-phosphor/sensors/phosphor-hwmon/0001-lev-add-poweron-monitor-feature.patch
rename to meta-quanta/meta-gbs/recipes-phosphor/sensors/phosphor-hwmon/0002-Add-power-on-monitor-mechanism.patch
index c279e0b..a3718ea 100644
--- a/meta-quanta/meta-gbs/recipes-phosphor/sensors/phosphor-hwmon/0001-lev-add-poweron-monitor-feature.patch
+++ b/meta-quanta/meta-gbs/recipes-phosphor/sensors/phosphor-hwmon/0002-Add-power-on-monitor-mechanism.patch
@@ -1,44 +1,33 @@
-From 2cea5d289b278db4468b16bf6b64102655811fd0 Mon Sep 17 00:00:00 2001
-From: Eddielu <Eddie.Lu@quantatw.com>
-Date: Mon, 27 Jul 2020 20:30:22 +0800
-Subject: [PATCH] Update lev-add-poweron-monitor-feature patch.
+From 5cea18ba476c0cb6ea622be50a09ead00cd47b14 Mon Sep 17 00:00:00 2001
+From: Ivan Li <rli11@lenovo.com>
+Date: Wed, 25 Dec 2019 17:05:00 +0800
+Subject: [PATCH] Add power on monitor mechanism
 
+Summary:
+1. Add "PWRONMON" attribute in sensor configuration file to determine whether it's power-on monitor sensor or not.  (i.e. PWRONMON_temp1 = "ON")
+2. Watching "CurrentHostState" property and then use it to turn on/off threshold alert for power-on monitor sensors.
+
+Test Plan:
+Check if there is any abnormal threshold events occurred in power off state or during power transition
+
+Signed-off-by: Ivan Li <rli11@lenovo.com>
+Change-Id: I76d3a664153141d94636e0011f3a48e4f6dee922
 ---
- Makefile.am    |   2 ++
- mainloop.cpp   | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- sensor.cpp     |  11 ++++++-
- sensor.hpp     |  13 ++++++++
- thresholds.hpp |   2 --
- 5 files changed, 127 insertions(+), 3 deletions(-)
+ mainloop.cpp   | 102 +++++++++++++++++++++++++++++++++++++++++++++++++
+ sensor.cpp     |  11 +++++-
+ sensor.hpp     |  13 +++++++
+ thresholds.hpp |  24 ------------
+ 4 files changed, 125 insertions(+), 25 deletions(-)
 
-diff --git a/Makefile.am b/Makefile.am
-index 706a6cc..c620fa4 100644
---- a/Makefile.am
-+++ b/Makefile.am
-@@ -46,6 +46,7 @@ libhwmon_la_LIBADD = \
- 	$(SDEVENTPLUS_LIBS) \
- 	$(PHOSPHOR_DBUS_INTERFACES_LIBS) \
- 	$(PHOSPHOR_LOGGING_LIBS) \
-+	$(PTHREAD_LIBS) \
- 	$(GPIOPLUS_LIBS) \
- 	$(STDPLUS_LIBS) \
- 	$(CODE_COVERAGE_LIBS) \
-@@ -55,6 +56,7 @@ libhwmon_la_CXXFLAGS = \
- 	$(SDEVENTPLUS_CFLAGS) \
- 	$(PHOSPHOR_DBUS_INTERFACES_CFLAGS) \
- 	$(PHOSPHOR_LOGGING_CFLAGS) \
-+	$(PTHREAD_CFLAGS) \
- 	$(STDPLUS_CFLAGS) \
- 	$(CODE_COVERAGE_CXXFLAGS)
- 
 diff --git a/mainloop.cpp b/mainloop.cpp
-index 3e7e0bf..4789a80 100644
+index 29dc26a..5e27a30 100644
 --- a/mainloop.cpp
 +++ b/mainloop.cpp
-@@ -41,6 +41,12 @@
+@@ -42,6 +42,13 @@
  #include <string>
  #include <unordered_set>
  #include <xyz/openbmc_project/Sensor/Device/error.hpp>
++#include <boost/asio/io_service.hpp>
 +#include <boost/container/flat_map.hpp>
 +#include <boost/algorithm/string/predicate.hpp>
 +#include <sdbusplus/asio/connection.hpp>
@@ -48,9 +37,9 @@
  
  using namespace phosphor::logging;
  
-@@ -76,6 +82,12 @@ decltype(
-     Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
-     &CriticalObject::criticalAlarmHigh;
+@@ -110,6 +117,12 @@ decltype(Thresholds<CriticalObject>::deassertHighSignal)
+     Thresholds<CriticalObject>::deassertHighSignal =
+         &CriticalObject::criticalHighAlarmDeasserted;
  
 +static std::unique_ptr<phosphor::Timer> cacheTimer = nullptr;
 +static std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr;
@@ -61,7 +50,7 @@
  void updateSensorInterfaces(InterfaceMap& ifaces, SensorValueType value)
  {
      for (auto& iface : ifaces)
-@@ -103,6 +115,84 @@ void updateSensorInterfaces(InterfaceMap& ifaces, SensorValueType value)
+@@ -137,6 +150,83 @@ void updateSensorInterfaces(InterfaceMap& ifaces, SensorValueType value)
      }
  }
  
@@ -104,23 +93,23 @@
 +            std::string objectName;
 +            boost::container::flat_map<std::string, std::variant<std::string>>
 +                values;
-+	    message.read(objectName, values);
-+	    auto findState = values.find("CurrentHostState");
-+	    if (findState != values.end())
-+	    {
-+	        bool on = boost::ends_with(
++           message.read(objectName, values);
++           auto findState = values.find("CurrentHostState");
++           if (findState != values.end())
++           {
++               bool on = boost::ends_with(
 +                    std::get<std::string>(findState->second), "Running");
-+		if (!on)
++               if (!on)
 +                {
-+		    cacheTimer->stop();
++                   cacheTimer->stop();
 +                    powerStatusOn = false;
 +                    return;
 +                }
-+		cacheTimer->start(std::chrono::duration_cast<std::chrono::microseconds>(
++               cacheTimer->start(std::chrono::duration_cast<std::chrono::microseconds>(
 +                    std::chrono::seconds(10)));
-+	    }
-+	    else {
-+		powerStatusOn = false;
++           }
++           else {
++               powerStatusOn = false;
 +            }
 +       });
 +
@@ -135,18 +124,17 @@
 +                boost::ends_with(std::get<std::string>(state), "Running");
 +        },
 +        "xyz.openbmc_project.State.Host",
-+	"/xyz/openbmc_project/state/host0",
-+	"org.freedesktop.DBus.Properties", "Get",
++       "/xyz/openbmc_project/state/host0",
++       "org.freedesktop.DBus.Properties", "Get",
 +        "xyz.openbmc_project.State.Host", "CurrentHostState");
 +
 +    createTimer();
 +}
 +
-+
  std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
  {
      std::string id;
-@@ -384,6 +474,7 @@ void MainLoop::init()
+@@ -418,6 +508,7 @@ void MainLoop::init()
              _interval = std::strtoull(interval.c_str(), NULL, 10);
          }
      }
@@ -154,7 +142,7 @@
  }
  
  void MainLoop::read()
-@@ -428,6 +519,12 @@ void MainLoop::read()
+@@ -462,6 +553,12 @@ void MainLoop::read()
  
          try
          {
@@ -167,23 +155,23 @@
              if (sensor->hasFaultFile())
              {
                  auto fault = _ioAccess->read(sensorSysfsType, sensorSysfsNum,
-@@ -490,6 +587,11 @@ void MainLoop::read()
+@@ -588,6 +685,11 @@ void MainLoop::read()
                  }
              }
  
 +            if(sensor->pwrOnMonitor() && !isPowerOn())
 +            {
-+		statusIface->functional(false);
-+	        continue;
++                statusIface->functional(false);
++                continue;
 +            }
              updateSensorInterfaces(obj, value);
          }
          catch (const std::system_error& e)
 diff --git a/sensor.cpp b/sensor.cpp
-index 09aeca6..b1cb470 100644
+index ac2f896..72b45f8 100644
 --- a/sensor.cpp
 +++ b/sensor.cpp
-@@ -31,7 +31,7 @@ Sensor::Sensor(const SensorSet::key_type& sensor,
+@@ -32,7 +32,7 @@ Sensor::Sensor(const SensorSet::key_type& sensor,
                 const hwmonio::HwmonIOInterface* ioAccess,
                 const std::string& devPath) :
      _sensor(sensor),
@@ -192,7 +180,7 @@
  {
      auto chip = env::getEnv("GPIOCHIP", sensor);
      auto access = env::getEnv("GPIO", sensor);
-@@ -60,6 +60,15 @@ Sensor::Sensor(const SensorSet::key_type& sensor,
+@@ -61,6 +61,15 @@ Sensor::Sensor(const SensorSet::key_type& sensor,
      auto senRmRCs = env::getEnv("REMOVERCS", sensor);
      // Add sensor removal return codes defined per sensor
      addRemoveRCs(senRmRCs);
@@ -209,10 +197,10 @@
  
  void Sensor::addRemoveRCs(const std::string& rcList)
 diff --git a/sensor.hpp b/sensor.hpp
-index 4b2d281..369a252 100644
+index 64d6e48..41c0fe7 100644
 --- a/sensor.hpp
 +++ b/sensor.hpp
-@@ -135,6 +135,16 @@ class Sensor
+@@ -151,6 +151,16 @@ class Sensor
          return _hasFaultFile;
      }
  
@@ -229,7 +217,7 @@
    private:
      /** @brief Sensor object's identifiers */
      SensorSet::key_type _sensor;
-@@ -156,6 +166,9 @@ class Sensor
+@@ -172,6 +182,9 @@ class Sensor
  
      /** @brief Tracks whether the sensor has a fault file or not. */
      bool _hasFaultFile;
@@ -240,18 +228,42 @@
  
  /**
 diff --git a/thresholds.hpp b/thresholds.hpp
-index 4d2fcff..972a469 100644
+index 8d557fc..0ffe0ce 100644
 --- a/thresholds.hpp
 +++ b/thresholds.hpp
-@@ -101,8 +101,6 @@ auto addThreshold(const std::string& sensorType, const std::string& sensorID,
+@@ -137,32 +137,8 @@ auto addThreshold(const std::string& sensorType, const std::string& sensorID,
          auto hi = stod(tHi) * std::pow(10, scale);
          (*iface.*Thresholds<T>::setLo)(lo);
          (*iface.*Thresholds<T>::setHi)(hi);
--        (*iface.*Thresholds<T>::alarmLo)(value <= lo);
--        (*iface.*Thresholds<T>::alarmHi)(value >= hi);
+-        auto alarmLowState = (*iface.*Thresholds<T>::getAlarmLow)();
+-        auto alarmHighState = (*iface.*Thresholds<T>::getAlarmHigh)();
+         (*iface.*Thresholds<T>::alarmLo)(value <= lo);
+         (*iface.*Thresholds<T>::alarmHi)(value >= hi);
+-        if (alarmLowState != (value <= lo))
+-        {
+-            if (value <= lo)
+-            {
+-                (*iface.*Thresholds<T>::assertLowSignal)(value);
+-            }
+-            else
+-            {
+-                (*iface.*Thresholds<T>::deassertLowSignal)(value);
+-            }
+-        }
+-        if (alarmHighState != (value >= hi))
+-        {
+-            if (value >= hi)
+-            {
+-                (*iface.*Thresholds<T>::assertHighSignal)(value);
+-            }
+-            else
+-            {
+-                (*iface.*Thresholds<T>::deassertHighSignal)(value);
+-            }
+-        }
          auto type = Thresholds<T>::type;
          obj[type] = iface;
      }
 -- 
-2.7.4
+2.21.0
 
diff --git a/meta-quanta/meta-gbs/recipes-phosphor/sensors/phosphor-hwmon_%.bbappend b/meta-quanta/meta-gbs/recipes-phosphor/sensors/phosphor-hwmon_%.bbappend
index 5f0517a..e6b8e2d 100644
--- a/meta-quanta/meta-gbs/recipes-phosphor/sensors/phosphor-hwmon_%.bbappend
+++ b/meta-quanta/meta-gbs/recipes-phosphor/sensors/phosphor-hwmon_%.bbappend
@@ -1,7 +1,7 @@
 FILESEXTRAPATHS_prepend_gbs := "${THISDIR}/${PN}:"
 
-SRC_URI_append_gbs = " file://0001-lev-add-poweron-monitor-feature.patch \
-                       file://0002-lev-add-sensors-slow-readings.patch \
+SRC_URI_append_gbs = " file://0001-sensor-Implement-sensor-ASYNC_READ_TIMEOUT.patch \
+                       file://0002-Add-power-on-monitor-mechanism.patch \
                      "
 
 GBS_NAMES = " \
@@ -32,4 +32,4 @@
 SYSTEMD_ENVIRONMENT_FILE_${PN}_append_gbs = " ${@compose_list(d, 'FENVS', 'FITEMS')}"
 
 
-EXTRA_OEMESON_append_gbs = " -Dupdate-functional-on-fail=true"
+EXTRA_OEMESON_append_gbs = " -Dupdate-functional-on-fail=true -Dnegative-errno-on-fail=false"