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.cpp b/tools/i2c/i2c.cpp
index 624bc60..04b9c27 100644
--- a/tools/i2c/i2c.cpp
+++ b/tools/i2c/i2c.cpp
@@ -17,25 +17,6 @@
 namespace i2c
 {
 
-void I2CDevice::open()
-{
-    fd = ::open(busStr.c_str(), O_RDWR);
-    if (fd == -1)
-    {
-        throw I2CException("Failed to open", busStr, devAddr, errno);
-    }
-
-    if (ioctl(fd, I2C_SLAVE, devAddr) < 0)
-    {
-        throw I2CException("Failed to set I2C_SLAVE", busStr, devAddr, errno);
-    }
-}
-
-void I2CDevice::close()
-{
-    ::close(fd);
-}
-
 void I2CDevice::checkReadFuncs(int type)
 {
     unsigned long funcs;
@@ -143,8 +124,41 @@
     }
 }
 
+void I2CDevice::open()
+{
+    if (isOpen())
+    {
+        throw I2CException("Device already open", busStr, devAddr);
+    }
+
+    fd = ::open(busStr.c_str(), O_RDWR);
+    if (fd == -1)
+    {
+        throw I2CException("Failed to open", busStr, devAddr, errno);
+    }
+
+    if (ioctl(fd, I2C_SLAVE, devAddr) < 0)
+    {
+        // Close device since setting slave address failed
+        closeWithoutException();
+
+        throw I2CException("Failed to set I2C_SLAVE", busStr, devAddr, errno);
+    }
+}
+
+void I2CDevice::close()
+{
+    checkIsOpen();
+    if (::close(fd) == -1)
+    {
+        throw I2CException("Failed to close", busStr, devAddr, errno);
+    }
+    fd = INVALID_FD;
+}
+
 void I2CDevice::read(uint8_t& data)
 {
+    checkIsOpen();
     checkReadFuncs(I2C_SMBUS_BYTE);
 
     int ret = i2c_smbus_read_byte(fd);
@@ -157,6 +171,7 @@
 
 void I2CDevice::read(uint8_t addr, uint8_t& data)
 {
+    checkIsOpen();
     checkReadFuncs(I2C_SMBUS_BYTE_DATA);
 
     int ret = i2c_smbus_read_byte_data(fd, addr);
@@ -169,6 +184,7 @@
 
 void I2CDevice::read(uint8_t addr, uint16_t& data)
 {
+    checkIsOpen();
     checkReadFuncs(I2C_SMBUS_WORD_DATA);
     int ret = i2c_smbus_read_word_data(fd, addr);
     if (ret < 0)
@@ -180,6 +196,7 @@
 
 void I2CDevice::read(uint8_t addr, uint8_t& size, uint8_t* data, Mode mode)
 {
+    checkIsOpen();
     int ret;
     switch (mode)
     {
@@ -206,6 +223,7 @@
 
 void I2CDevice::write(uint8_t data)
 {
+    checkIsOpen();
     checkWriteFuncs(I2C_SMBUS_BYTE);
 
     if (i2c_smbus_write_byte(fd, data) < 0)
@@ -216,6 +234,7 @@
 
 void I2CDevice::write(uint8_t addr, uint8_t data)
 {
+    checkIsOpen();
     checkWriteFuncs(I2C_SMBUS_BYTE_DATA);
 
     if (i2c_smbus_write_byte_data(fd, addr, data) < 0)
@@ -226,6 +245,7 @@
 
 void I2CDevice::write(uint8_t addr, uint16_t data)
 {
+    checkIsOpen();
     checkWriteFuncs(I2C_SMBUS_WORD_DATA);
 
     if (i2c_smbus_write_word_data(fd, addr, data) < 0)
@@ -237,6 +257,7 @@
 void I2CDevice::write(uint8_t addr, uint8_t size, const uint8_t* data,
                       Mode mode)
 {
+    checkIsOpen();
     int ret;
     switch (mode)
     {
@@ -256,15 +277,17 @@
     }
 }
 
-std::unique_ptr<I2CInterface> I2CDevice::create(uint8_t busId, uint8_t devAddr)
+std::unique_ptr<I2CInterface> I2CDevice::create(uint8_t busId, uint8_t devAddr,
+                                                InitialState initialState)
 {
-    std::unique_ptr<I2CDevice> dev(new I2CDevice(busId, devAddr));
+    std::unique_ptr<I2CDevice> dev(new I2CDevice(busId, devAddr, initialState));
     return dev;
 }
 
-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)
 {
-    return I2CDevice::create(busId, devAddr);
+    return I2CDevice::create(busId, devAddr, initialState);
 }
 
 } // namespace i2c
diff --git a/tools/i2c/i2c.hpp b/tools/i2c/i2c.hpp
index 05b02b5..b5ddcb8 100644
--- a/tools/i2c/i2c.hpp
+++ b/tools/i2c/i2c.hpp
@@ -14,21 +14,26 @@
      *
      * Construct I2CDevice object from the bus id and device address
      *
+     * Automatically opens the I2CDevice if initialState is OPEN.
+     *
      * @param[in] busId - The i2c bus ID
      * @param[in] devAddr - The device address of the I2C device
+     * @param[in] initialState - Initial state of the I2CDevice object
      */
-    explicit I2CDevice(uint8_t busId, uint8_t devAddr) :
-        busId(busId), devAddr(devAddr)
+    explicit I2CDevice(uint8_t busId, uint8_t devAddr,
+                       InitialState initialState = InitialState::OPEN) :
+        busId(busId),
+        devAddr(devAddr), fd(INVALID_FD)
     {
         busStr = "/dev/i2c-" + std::to_string(busId);
-        open();
+        if (initialState == InitialState::OPEN)
+        {
+            open();
+        }
     }
 
-    /** @brief Open the i2c device */
-    void open();
-
-    /** @brief Close the i2c device */
-    void close();
+    /** @brief Invalid file descriptor */
+    static constexpr int INVALID_FD = -1;
 
     /** @brief The I2C bus ID */
     uint8_t busId;
@@ -42,6 +47,30 @@
     /** @brief The i2c bus path in /dev */
     std::string busStr;
 
+    /** @brief Check that device interface is open
+     *
+     * @throw I2CException if device is not open
+     */
+    void checkIsOpen() const
+    {
+        if (!isOpen())
+        {
+            throw I2CException("Device not open", busStr, devAddr);
+        }
+    }
+
+    /** @brief Close device without throwing an exception if an error occurs */
+    void closeWithoutException() noexcept
+    {
+        try
+        {
+            close();
+        }
+        catch (...)
+        {
+        }
+    }
+
     /** @brief Check i2c adapter read functionality
      *
      * Check if the i2c adapter has the functionality specified by the SMBus
@@ -65,11 +94,28 @@
     void checkWriteFuncs(int type);
 
   public:
+    /** @copydoc I2CInterface::~I2CInterface() */
     ~I2CDevice()
     {
-        close();
+        if (isOpen())
+        {
+            // Note: destructors must not throw exceptions
+            closeWithoutException();
+        }
     }
 
+    /** @copydoc I2CInterface::open() */
+    void open();
+
+    /** @copydoc I2CInterface::isOpen() */
+    bool isOpen() const
+    {
+        return (fd != INVALID_FD);
+    }
+
+    /** @copydoc I2CInterface::close() */
+    void close();
+
     /** @copydoc I2CInterface::read(uint8_t&) */
     void read(uint8_t& data) override;
 
@@ -98,12 +144,17 @@
 
     /** @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
      */
-    static std::unique_ptr<I2CInterface> create(uint8_t busId, uint8_t devAddr);
+    static std::unique_ptr<I2CInterface>
+        create(uint8_t busId, uint8_t devAddr,
+               InitialState initialState = InitialState::OPEN);
 };
 
 } // namespace i2c
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
diff --git a/tools/i2c/test/mocked_i2c_interface.hpp b/tools/i2c/test/mocked_i2c_interface.hpp
index 5bb0a3a..5f3b3a3 100644
--- a/tools/i2c/test/mocked_i2c_interface.hpp
+++ b/tools/i2c/test/mocked_i2c_interface.hpp
@@ -12,6 +12,10 @@
   public:
     virtual ~MockedI2CInterface() = default;
 
+    MOCK_METHOD(void, open, (), (override));
+    MOCK_METHOD(bool, isOpen, (), (const, override));
+    MOCK_METHOD(void, close, (), (override));
+
     MOCK_METHOD(void, read, (uint8_t & data), (override));
     MOCK_METHOD(void, read, (uint8_t addr, uint8_t& data), (override));
     MOCK_METHOD(void, read, (uint8_t addr, uint16_t& data), (override));
@@ -27,7 +31,9 @@
                 (override));
 };
 
-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*/)
 {
     return std::make_unique<MockedI2CInterface>();
 }