lpc_handler: add initialized state

The LPC handler mechanism requires an initialization step with input
from the host.  Therefore, add a state to track whether this step has
happened.

Change-Id: I03f05df5fd6ae511836f324c866afcc9856ae8ab
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/lpc_handler.cpp b/lpc_handler.cpp
index 108f01d..4a584ad 100644
--- a/lpc_handler.cpp
+++ b/lpc_handler.cpp
@@ -34,7 +34,8 @@
 bool LpcDataHandler::close()
 {
     /* TODO: implement ioctl call to close window. */
-    return false;
+
+    return setInitializedAndReturn(false);
 }
 
 std::vector<std::uint8_t> LpcDataHandler::copyFrom(std::uint32_t length)
@@ -47,6 +48,14 @@
      * automatically read data, but rather must perform some ioctl or other
      * access to get the data from the driver.
      */
+    if (!initialized)
+    {
+        /* TODO: Consider designing some exceptions we can catch for when there
+         * is an error.
+         */
+        return {};
+    }
+
     return {};
 }
 
@@ -71,9 +80,10 @@
     if (windowSize == 0)
     {
         /* Failed to map region. */
+        return false;
     }
 
-    return false;
+    return setInitializedAndReturn(true);
 }
 
 std::vector<std::uint8_t> LpcDataHandler::read()
diff --git a/lpc_handler.hpp b/lpc_handler.hpp
index 52073da..8431730 100644
--- a/lpc_handler.hpp
+++ b/lpc_handler.hpp
@@ -36,7 +36,7 @@
     LpcDataHandler(std::uint32_t regionAddress,
                    std::unique_ptr<LpcMapperInterface> mapper) :
         regionAddress(regionAddress),
-        mapper(std::move(mapper))
+        mapper(std::move(mapper)), initialized(false)
     {
     }
 
@@ -47,8 +47,15 @@
     std::vector<std::uint8_t> read() override;
 
   private:
+    bool setInitializedAndReturn(bool value)
+    {
+        initialized = value;
+        return value;
+    }
+
     std::uint32_t regionAddress;
     std::unique_ptr<LpcMapperInterface> mapper;
+    bool initialized;
 };
 
 } // namespace blobs