presence: Get DRIVER_BIND_DELAY_MS value from env

Add a delay variable to the Presence class, defaulted to 0. Attempt to
set this value from the DRIVER_BIND_DELAY_MS environment variable. This
delay is then used between seeing the power supply present and binding
the device driver.

Signed-off-by: Brandon Wyman <bjwyman@gmail.com>
Change-Id: I62757d0585c2e34879f6b93b353bcef650aba52c
diff --git a/presence/gpio_presence.cpp b/presence/gpio_presence.cpp
index d2d2892..2c448da 100644
--- a/presence/gpio_presence.cpp
+++ b/presence/gpio_presence.cpp
@@ -125,9 +125,16 @@
                 if (ev.value > 0)
                 {
                     present = true;
+                    std::this_thread::sleep_for(
+                        std::chrono::milliseconds(delay));
+                    bindOrUnbindDrivers(present);
+                    updateInventory(present);
                 }
-                updateInventory(present);
-                bindOrUnbindDrivers(present);
+                else
+                {
+                    updateInventory(present);
+                    bindOrUnbindDrivers(present);
+                }
             }
         }
     }
diff --git a/presence/gpio_presence.hpp b/presence/gpio_presence.hpp
index fbe4fa9..ec26fde 100644
--- a/presence/gpio_presence.hpp
+++ b/presence/gpio_presence.hpp
@@ -3,6 +3,7 @@
 
 #include <systemd/sd-event.h>
 
+#include <cstdlib>
 #include <experimental/filesystem>
 #include <string>
 
@@ -73,6 +74,15 @@
         bus(bus), inventory(inventory), name(name), drivers(drivers),
         ifaces(ifaces)
     {
+        // See if the environment (from configuration file?) has a
+        // DRIVER_BIND_DELAY_MS set.
+        if (char* envDelay = std::getenv("DRIVER_BIND_DELAY_MS"))
+        {
+            // DRIVER_BIND_DELAY_MS environment variable is set.
+            // Update the bind delay (in milliseconds) to the value from the
+            // environment.
+            delay = std::strtoull(envDelay, NULL, 10);
+        }
         determinePresence();
     }
 
@@ -118,6 +128,9 @@
     /** @brief Object path under inventory to display this inventory item */
     const std::string inventory;
 
+    /** @brief Delay in milliseconds from present to bind device driver */
+    unsigned int delay = 0;
+
     /** @brief Pretty name of the inventory item*/
     const std::string name;