gobot.io/x/gobot@v1.16.0/drivers/i2c/i2c_test.go (about)

     1  // +build !windows
     2  
     3  package i2c
     4  
     5  import (
     6  	"errors"
     7  	"testing"
     8  
     9  	"syscall"
    10  	"unsafe"
    11  
    12  	"gobot.io/x/gobot/gobottest"
    13  	"gobot.io/x/gobot/sysfs"
    14  )
    15  
    16  func syscallImpl(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno) {
    17  	if (trap == syscall.SYS_IOCTL) && (a2 == sysfs.I2C_FUNCS) {
    18  		var funcPtr *uint64 = (*uint64)(unsafe.Pointer(a3))
    19  		*funcPtr = sysfs.I2C_FUNC_SMBUS_READ_BYTE | sysfs.I2C_FUNC_SMBUS_READ_BYTE_DATA |
    20  			sysfs.I2C_FUNC_SMBUS_READ_WORD_DATA |
    21  			sysfs.I2C_FUNC_SMBUS_WRITE_BYTE | sysfs.I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
    22  			sysfs.I2C_FUNC_SMBUS_WRITE_WORD_DATA
    23  	}
    24  	// Let all operations succeed
    25  	return 0, 0, 0
    26  }
    27  
    28  func syscallImplFail(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno) {
    29  	// Let all operations fail
    30  	return 0, 0, 1
    31  }
    32  
    33  func initI2CDevice() I2cDevice {
    34  	fs := sysfs.NewMockFilesystem([]string{
    35  		"/dev/i2c-1",
    36  	})
    37  	sysfs.SetFilesystem(fs)
    38  
    39  	sysfs.SetSyscall(&sysfs.MockSyscall{
    40  		Impl: syscallImpl,
    41  	})
    42  	i, _ := sysfs.NewI2cDevice("/dev/i2c-1")
    43  	return i
    44  }
    45  
    46  func initI2CDeviceAddressError() I2cDevice {
    47  	fs := sysfs.NewMockFilesystem([]string{
    48  		"/dev/i2c-1",
    49  	})
    50  	sysfs.SetFilesystem(fs)
    51  
    52  	sysfs.SetSyscall(&sysfs.MockSyscall{
    53  		Impl: syscallImplFail,
    54  	})
    55  	i, _ := sysfs.NewI2cDevice("/dev/i2c-1")
    56  	return i
    57  }
    58  
    59  func TestI2CAddress(t *testing.T) {
    60  	c := NewConnection(initI2CDevice(), 0x66)
    61  	gobottest.Assert(t, c.address, 0x66)
    62  }
    63  
    64  func TestI2CClose(t *testing.T) {
    65  	c := NewConnection(initI2CDevice(), 0x06)
    66  	gobottest.Assert(t, c.Close(), nil)
    67  }
    68  
    69  func TestI2CRead(t *testing.T) {
    70  	c := NewConnection(initI2CDevice(), 0x06)
    71  	i, _ := c.Read([]byte{})
    72  	gobottest.Assert(t, i, 0)
    73  }
    74  
    75  func TestI2CReadAddressError(t *testing.T) {
    76  	c := NewConnection(initI2CDeviceAddressError(), 0x06)
    77  	_, err := c.Read([]byte{})
    78  	gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
    79  }
    80  
    81  func TestI2CWrite(t *testing.T) {
    82  	c := NewConnection(initI2CDevice(), 0x06)
    83  	i, _ := c.Write([]byte{0x01})
    84  	gobottest.Assert(t, i, 1)
    85  }
    86  
    87  func TestI2CWriteAddressError(t *testing.T) {
    88  	c := NewConnection(initI2CDeviceAddressError(), 0x06)
    89  	_, err := c.Write([]byte{0x01})
    90  	gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
    91  }
    92  
    93  func TestI2CReadByte(t *testing.T) {
    94  	c := NewConnection(initI2CDevice(), 0x06)
    95  	v, _ := c.ReadByte()
    96  	gobottest.Assert(t, v, uint8(0))
    97  }
    98  
    99  func TestI2CReadByteAddressError(t *testing.T) {
   100  	c := NewConnection(initI2CDeviceAddressError(), 0x06)
   101  	_, err := c.ReadByte()
   102  	gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
   103  }
   104  
   105  func TestI2CReadByteData(t *testing.T) {
   106  	c := NewConnection(initI2CDevice(), 0x06)
   107  	v, _ := c.ReadByteData(0x01)
   108  	gobottest.Assert(t, v, uint8(0))
   109  }
   110  
   111  func TestI2CReadByteDataAddressError(t *testing.T) {
   112  	c := NewConnection(initI2CDeviceAddressError(), 0x06)
   113  	_, err := c.ReadByteData(0x01)
   114  	gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
   115  }
   116  
   117  func TestI2CReadWordData(t *testing.T) {
   118  	c := NewConnection(initI2CDevice(), 0x06)
   119  	v, _ := c.ReadWordData(0x01)
   120  	gobottest.Assert(t, v, uint16(0))
   121  }
   122  
   123  func TestI2CReadWordDataAddressError(t *testing.T) {
   124  	c := NewConnection(initI2CDeviceAddressError(), 0x06)
   125  	_, err := c.ReadWordData(0x01)
   126  	gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
   127  }
   128  
   129  func TestI2CWriteByte(t *testing.T) {
   130  	c := NewConnection(initI2CDevice(), 0x06)
   131  	err := c.WriteByte(0x01)
   132  	gobottest.Assert(t, err, nil)
   133  }
   134  
   135  func TestI2CWriteByteAddressError(t *testing.T) {
   136  	c := NewConnection(initI2CDeviceAddressError(), 0x06)
   137  	err := c.WriteByte(0x01)
   138  	gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
   139  }
   140  
   141  func TestI2CWriteByteData(t *testing.T) {
   142  	c := NewConnection(initI2CDevice(), 0x06)
   143  	err := c.WriteByteData(0x01, 0x01)
   144  	gobottest.Assert(t, err, nil)
   145  }
   146  
   147  func TestI2CWriteByteDataAddressError(t *testing.T) {
   148  	c := NewConnection(initI2CDeviceAddressError(), 0x06)
   149  	err := c.WriteByteData(0x01, 0x01)
   150  	gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
   151  }
   152  
   153  func TestI2CWriteWordData(t *testing.T) {
   154  	c := NewConnection(initI2CDevice(), 0x06)
   155  	err := c.WriteWordData(0x01, 0x01)
   156  	gobottest.Assert(t, err, nil)
   157  }
   158  
   159  func TestI2CWriteWordDataAddressError(t *testing.T) {
   160  	c := NewConnection(initI2CDeviceAddressError(), 0x06)
   161  	err := c.WriteWordData(0x01, 0x01)
   162  	gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
   163  }
   164  
   165  func TestI2CWriteBlockData(t *testing.T) {
   166  	c := NewConnection(initI2CDevice(), 0x06)
   167  	err := c.WriteBlockData(0x01, []byte{0x01, 0x02})
   168  	gobottest.Assert(t, err, nil)
   169  }
   170  
   171  func TestI2CWriteBlockDataAddressError(t *testing.T) {
   172  	c := NewConnection(initI2CDeviceAddressError(), 0x06)
   173  	err := c.WriteBlockData(0x01, []byte{0x01, 0x02})
   174  	gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
   175  }
   176  
   177  func Test_setBit(t *testing.T) {
   178  	var expectedVal uint8 = 129
   179  	actualVal := setBit(1, 7)
   180  	gobottest.Assert(t, expectedVal, actualVal)
   181  }
   182  
   183  func Test_clearBit(t *testing.T) {
   184  	var expectedVal uint8
   185  	actualVal := clearBit(128, 7)
   186  	gobottest.Assert(t, expectedVal, actualVal)
   187  }