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