GPIO test signal safe fallback + refactoring.

This addresses the problem of spurious GPIO signals indicating a test
scenario whereas in fact this isn't one, which may lead to hosts unable
to get an update (ZGB, Lumpy). This is also a partial fix to a problem
with Stumpy, where such spurious signals are inherent to the board
implementation.

* Safe fallback: a GPIO-signaled test scenario will be ignored other
  than on the first time, in both places it is being checked
  (UpdateAttempter::Update() and UpdateCheckScheduler::StaticCheck()).
  This will ensure that we do not (a) override EULA/OOBE-complete flag
  more than once; and (b) we do not attempt to update against a local
  test server more than once.  This generally covers against spurious
  GPIO, as long as a user cannot trigger an update check on
  a non-OOBE-complete system (appears to be a safe assumption).

* The retry timeout after failing an update with the test server is
  shortened to 1 minute (compared to the default 45 minute). This
  substantially increases the chances for a system exhibiting spurious
  GPIO signals to get updates.

* Moved the GPIO functionality into a separate module/class. This makes
  more sense now that it is being used by more than one class
  (UpdateAttempter and UpdateCheckScheduler). The implementation of
  GpioHandler has no instance data members and so behaves like
  a singleton, but otherwise looks and feels like a normal class.

* Also changing the private test server URL to use an unregistered TCP
  port (further reduces the chances of anything responding on the LAN).

* Some minor fixes.

BUG=chromium-os:27077, chromium-os:27109, chromium-os:25397,
chromium-os:27157

TEST=Unittests passed; GPIO reading + fallback work on x86-alex.

Change-Id: Ide1a60a690f1263efd47872360470347e56eeb45
Reviewed-on: https://gerrit.chromium.org/gerrit/17344
Commit-Ready: Gilad Arnold <garnold@chromium.org>
Reviewed-by: Gilad Arnold <garnold@chromium.org>
Tested-by: Gilad Arnold <garnold@chromium.org>
diff --git a/gpio_handler.h b/gpio_handler.h
new file mode 100644
index 0000000..c3d5a38
--- /dev/null
+++ b/gpio_handler.h
@@ -0,0 +1,71 @@
+// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_GPIO_HANDLER_H__
+#define CHROMEOS_PLATFORM_UPDATE_ENGINE_GPIO_HANDLER_H__
+
+#include <libudev.h>
+
+#include <string>
+
+#include <base/basictypes.h>
+
+namespace chromeos_update_engine {
+
+// Handles GPIO signals, which are used for indicating a lab test environment.
+// This class detects, reads and decides whether a test scenario is in effect,
+// which can be used by client code to trigger test-specific behavior.  This
+// class has only static members/methods and cannot be instantiated.
+class GpioHandler {
+ public:
+  // Returns true iff GPIOs have been signaled to indicate an automated test
+  // case. This triggers a discovery and reading of the dutflaga/b GPIOs.
+  static bool IsGpioSignalingTest();
+
+ private:
+  // This class cannot be instantiated.
+  GpioHandler() {}
+
+  // Enumerator for dutflag GPIOs.
+  enum DutflagGpioId {
+    kDutflagaGpio,
+    kDutflagbGpio,
+  };
+
+  // Gets the fully qualified sysfs name of a dutflag device.  |udev| is a live
+  // libudev instance; |gpio_dutflag_str| is the identifier for the requested
+  // dutflag GPIO. The output is stored in the string pointed to by
+  // |dutflag_dev_name_p|.  Returns true upon success, false otherwise.
+  static bool GetDutflagGpioDevName(struct udev* udev,
+                                    const std::string& gpio_dutflag_str,
+                                    const char** dutflag_dev_name_p);
+
+  // Gets the dut_flaga/b GPIO device names and copies them into the two string
+  // arguments, respectively. A string pointer may be null, in which case
+  // discovery will not be attempted for the corresponding device. The function
+  // caches these strings, which are assumed to be hardware constants. Returns
+  // true upon success, false otherwise.
+  static bool GetDutflagGpioDevNames(std::string* dutflaga_dev_name_p,
+                                     std::string* dutflagb_dev_name_p);
+
+  // Writes the dut_flaga GPIO status into its argument, where true/false stand
+  // for "on"/"off", respectively. Returns true upon success, false otherwise
+  // (in which case no value is written to |status|).
+  static bool GetDutflagaGpio(bool* status);
+
+  // Reads the value of a dut_flag GPIO |id| and stores it in |status_p|.
+  // Returns true upon success, false otherwise (which also means that the GPIO
+  // value was not stored and should not be used).
+  static bool GetDutflagGpioStatus(DutflagGpioId id, bool* status_p);
+
+  // Dutflaga/b GPIO device names.
+  static const char* dutflaga_dev_name_;
+  static const char* dutflagb_dev_name_;
+
+  DISALLOW_COPY_AND_ASSIGN(GpioHandler);
+};
+
+}  // namespace chromeos_update_engine
+
+#endif  // CHROMEOS_PLATFORM_UPDATE_ENGINE_GPIO_HANDLER_H__