i2c: Support explicit open/close and re-open

Add the ability to explicitly open and close the I2CInterface/I2CDevice.
Also add support to re-open the I2CInterface/I2CDevice after it was
closed.

This support is needed for use cases like the following:

* I2C device information (bus and address) is parsed from a
  configuration file at standby.  However, an I2C connection to the
  device should not be opened yet.   The device may not have power until
  the system has been booted.  Additionally, if the device is a FRU,
  it could be replaced (remove + add) while at standby, leading to
  communication errors.

* The device is sometimes bound to a device driver.  The I2C connection
  should only be open during the time periods when the device driver
  is not bound.

Tested:
* Verified create() function with default value of OPEN, explicit value
  of OPEN, and explicit value of CLOSED.
* Verified device interface can be explicitly opened, closed, and
  re-opened.
* Verified read() and write() functions still work when device interface
  is open.
* Verified open() fails with appropriate error if already open.
* Verified read() fails with appropriate error if not open.
* Verified write() fails with appropriate error if not open.
* Verified close() fails with appropriate error if not open.

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I0182966f0b0614eac0de69eb95d960035f9d0426
diff --git a/tools/i2c/i2c_interface.hpp b/tools/i2c/i2c_interface.hpp
index 152f1e9..eaaf9f1 100644
--- a/tools/i2c/i2c_interface.hpp
+++ b/tools/i2c/i2c_interface.hpp
@@ -45,8 +45,19 @@
 class I2CInterface
 {
   public:
+    /** @brief Destructor
+     *
+     * Closes the I2C interface to the device if necessary.
+     */
     virtual ~I2CInterface() = default;
 
+    /** @brief Initial state when an I2CInterface object is created */
+    enum class InitialState
+    {
+        OPEN,
+        CLOSED
+    };
+
     /** @brief The block transaction mode */
     enum class Mode
     {
@@ -54,6 +65,33 @@
         I2C,
     };
 
+    /** @brief Open the I2C interface to the device
+     *
+     * Throws an I2CException if the interface is already open.  See isOpen().
+     *
+     * @throw I2CException on error
+     */
+    virtual void open() = 0;
+
+    /** @brief Indicates whether the I2C interface to the device is open
+     *
+     * @return true if interface is open, false otherwise
+     */
+    virtual bool isOpen() const = 0;
+
+    /** @brief Close the I2C interface to the device
+     *
+     * The interface can later be re-opened by calling open().
+     *
+     * Note that the destructor will automatically close the I2C interface if
+     * necessary.
+     *
+     * Throws an I2CException if the interface is not open.  See isOpen().
+     *
+     * @throw I2CException on error
+     */
+    virtual void close() = 0;
+
     /** @brief Read byte data from i2c
      *
      * @param[out] data - The data read from the i2c device
@@ -145,11 +183,16 @@
 
 /** @brief Create an I2CInterface instance
  *
+ * Automatically opens the I2CInterface if initialState is OPEN.
+ *
  * @param[in] busId - The i2c bus ID
  * @param[in] devAddr - The device address of the i2c
+ * @param[in] initialState - Initial state of the I2CInterface object
  *
  * @return The unique_ptr holding the I2CInterface
  */
-std::unique_ptr<I2CInterface> create(uint8_t busId, uint8_t devAddr);
+std::unique_ptr<I2CInterface> create(
+    uint8_t busId, uint8_t devAddr,
+    I2CInterface::InitialState initialState = I2CInterface::InitialState::OPEN);
 
 } // namespace i2c