2017-07-12 09:47:44 -06:00
|
|
|
// +build !windows
|
|
|
|
|
2017-02-14 12:04:44 +01:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
2017-04-11 12:29:31 +02:00
|
|
|
"errors"
|
2017-02-14 12:04:44 +01:00
|
|
|
"testing"
|
|
|
|
|
2017-02-27 23:40:14 +01:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
2017-03-20 18:21:12 +01:00
|
|
|
|
2023-01-05 19:04:32 +01:00
|
|
|
"gobot.io/x/gobot"
|
2017-03-20 18:21:12 +01:00
|
|
|
"gobot.io/x/gobot/gobottest"
|
2022-11-20 19:22:26 +01:00
|
|
|
"gobot.io/x/gobot/system"
|
2017-02-14 12:04:44 +01:00
|
|
|
)
|
|
|
|
|
2022-10-26 18:21:34 +02:00
|
|
|
const dev = "/dev/i2c-1"
|
|
|
|
|
2017-02-27 23:40:14 +01:00
|
|
|
func syscallImpl(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno) {
|
2022-11-20 19:22:26 +01:00
|
|
|
if (trap == syscall.SYS_IOCTL) && (a2 == system.I2C_FUNCS) {
|
2017-02-27 23:40:14 +01:00
|
|
|
var funcPtr *uint64 = (*uint64)(unsafe.Pointer(a3))
|
2022-11-20 19:22:26 +01:00
|
|
|
*funcPtr = system.I2C_FUNC_SMBUS_READ_BYTE | system.I2C_FUNC_SMBUS_READ_BYTE_DATA |
|
|
|
|
system.I2C_FUNC_SMBUS_READ_WORD_DATA |
|
|
|
|
system.I2C_FUNC_SMBUS_WRITE_BYTE | system.I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
|
|
|
|
system.I2C_FUNC_SMBUS_WRITE_WORD_DATA
|
2017-02-27 23:40:14 +01:00
|
|
|
}
|
|
|
|
// Let all operations succeed
|
|
|
|
return 0, 0, 0
|
|
|
|
}
|
|
|
|
|
2017-04-11 12:29:31 +02:00
|
|
|
func syscallImplFail(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno) {
|
2022-11-20 19:22:26 +01:00
|
|
|
if (trap == syscall.SYS_IOCTL) && (a2 == system.I2C_FUNCS) {
|
2022-11-05 07:42:28 +01:00
|
|
|
var funcPtr *uint64 = (*uint64)(unsafe.Pointer(a3))
|
2022-11-20 19:22:26 +01:00
|
|
|
*funcPtr = system.I2C_FUNC_SMBUS_READ_BYTE | system.I2C_FUNC_SMBUS_READ_BYTE_DATA |
|
|
|
|
system.I2C_FUNC_SMBUS_READ_WORD_DATA |
|
|
|
|
system.I2C_FUNC_SMBUS_WRITE_BYTE | system.I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
|
|
|
|
system.I2C_FUNC_SMBUS_WRITE_WORD_DATA
|
2022-11-05 07:42:28 +01:00
|
|
|
// retrieve functions call succeed
|
|
|
|
return 0, 0, 0
|
|
|
|
}
|
2017-04-11 12:29:31 +02:00
|
|
|
// Let all operations fail
|
|
|
|
return 0, 0, 1
|
|
|
|
}
|
|
|
|
|
2023-01-05 19:04:32 +01:00
|
|
|
func initI2CDevice() gobot.I2cSystemDevicer {
|
2022-11-20 19:22:26 +01:00
|
|
|
a := system.NewAccesser()
|
2022-11-05 07:42:28 +01:00
|
|
|
a.UseMockFilesystem([]string{dev})
|
|
|
|
msc := a.UseMockSyscall()
|
|
|
|
msc.Impl = syscallImpl
|
2017-02-14 12:04:44 +01:00
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
d, _ := a.NewI2cDevice(dev)
|
|
|
|
return d
|
2022-10-26 18:21:34 +02:00
|
|
|
}
|
|
|
|
|
2023-01-05 19:04:32 +01:00
|
|
|
func initI2CDeviceAddressError() gobot.I2cSystemDevicer {
|
2022-11-20 19:22:26 +01:00
|
|
|
a := system.NewAccesser()
|
2022-11-05 07:42:28 +01:00
|
|
|
a.UseMockFilesystem([]string{dev})
|
|
|
|
msc := a.UseMockSyscall()
|
|
|
|
msc.Impl = syscallImplFail
|
2017-04-11 12:29:31 +02:00
|
|
|
|
2022-11-05 07:42:28 +01:00
|
|
|
d, _ := a.NewI2cDevice(dev)
|
|
|
|
return d
|
2017-04-11 12:29:31 +02:00
|
|
|
}
|
|
|
|
|
2017-02-14 12:04:44 +01:00
|
|
|
func TestI2CAddress(t *testing.T) {
|
|
|
|
c := NewConnection(initI2CDevice(), 0x66)
|
|
|
|
gobottest.Assert(t, c.address, 0x66)
|
|
|
|
}
|
|
|
|
|
2017-03-20 18:21:12 +01:00
|
|
|
func TestI2CClose(t *testing.T) {
|
|
|
|
c := NewConnection(initI2CDevice(), 0x06)
|
|
|
|
gobottest.Assert(t, c.Close(), nil)
|
|
|
|
}
|
|
|
|
|
2017-02-14 12:04:44 +01:00
|
|
|
func TestI2CRead(t *testing.T) {
|
|
|
|
c := NewConnection(initI2CDevice(), 0x06)
|
|
|
|
i, _ := c.Read([]byte{})
|
|
|
|
gobottest.Assert(t, i, 0)
|
|
|
|
}
|
2017-02-14 13:41:01 +01:00
|
|
|
|
2017-04-11 12:29:31 +02:00
|
|
|
func TestI2CReadAddressError(t *testing.T) {
|
|
|
|
c := NewConnection(initI2CDeviceAddressError(), 0x06)
|
|
|
|
_, err := c.Read([]byte{})
|
|
|
|
gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
|
|
|
|
}
|
|
|
|
|
2017-02-14 13:41:01 +01:00
|
|
|
func TestI2CWrite(t *testing.T) {
|
|
|
|
c := NewConnection(initI2CDevice(), 0x06)
|
|
|
|
i, _ := c.Write([]byte{0x01})
|
|
|
|
gobottest.Assert(t, i, 1)
|
|
|
|
}
|
|
|
|
|
2017-04-11 12:29:31 +02:00
|
|
|
func TestI2CWriteAddressError(t *testing.T) {
|
|
|
|
c := NewConnection(initI2CDeviceAddressError(), 0x06)
|
|
|
|
_, err := c.Write([]byte{0x01})
|
|
|
|
gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
|
|
|
|
}
|
|
|
|
|
2017-02-14 13:41:01 +01:00
|
|
|
func TestI2CReadByte(t *testing.T) {
|
|
|
|
c := NewConnection(initI2CDevice(), 0x06)
|
|
|
|
v, _ := c.ReadByte()
|
2022-10-26 18:21:34 +02:00
|
|
|
gobottest.Assert(t, v, uint8(0xFC))
|
2017-02-14 13:41:01 +01:00
|
|
|
}
|
|
|
|
|
2017-04-11 12:29:31 +02:00
|
|
|
func TestI2CReadByteAddressError(t *testing.T) {
|
|
|
|
c := NewConnection(initI2CDeviceAddressError(), 0x06)
|
|
|
|
_, err := c.ReadByte()
|
|
|
|
gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
|
|
|
|
}
|
|
|
|
|
2017-02-14 13:41:01 +01:00
|
|
|
func TestI2CReadByteData(t *testing.T) {
|
|
|
|
c := NewConnection(initI2CDevice(), 0x06)
|
|
|
|
v, _ := c.ReadByteData(0x01)
|
2022-10-26 18:21:34 +02:00
|
|
|
gobottest.Assert(t, v, uint8(0xFD))
|
2017-02-14 13:41:01 +01:00
|
|
|
}
|
|
|
|
|
2017-04-11 12:29:31 +02:00
|
|
|
func TestI2CReadByteDataAddressError(t *testing.T) {
|
|
|
|
c := NewConnection(initI2CDeviceAddressError(), 0x06)
|
|
|
|
_, err := c.ReadByteData(0x01)
|
|
|
|
gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
|
|
|
|
}
|
|
|
|
|
2017-02-14 13:41:01 +01:00
|
|
|
func TestI2CReadWordData(t *testing.T) {
|
|
|
|
c := NewConnection(initI2CDevice(), 0x06)
|
|
|
|
v, _ := c.ReadWordData(0x01)
|
2022-10-26 18:21:34 +02:00
|
|
|
gobottest.Assert(t, v, uint16(0xFFFE))
|
2017-02-14 13:41:01 +01:00
|
|
|
}
|
|
|
|
|
2017-04-11 12:29:31 +02:00
|
|
|
func TestI2CReadWordDataAddressError(t *testing.T) {
|
|
|
|
c := NewConnection(initI2CDeviceAddressError(), 0x06)
|
|
|
|
_, err := c.ReadWordData(0x01)
|
|
|
|
gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
|
|
|
|
}
|
|
|
|
|
2017-02-14 13:41:01 +01:00
|
|
|
func TestI2CWriteByte(t *testing.T) {
|
|
|
|
c := NewConnection(initI2CDevice(), 0x06)
|
|
|
|
err := c.WriteByte(0x01)
|
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
}
|
|
|
|
|
2017-04-11 12:29:31 +02:00
|
|
|
func TestI2CWriteByteAddressError(t *testing.T) {
|
|
|
|
c := NewConnection(initI2CDeviceAddressError(), 0x06)
|
|
|
|
err := c.WriteByte(0x01)
|
|
|
|
gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
|
|
|
|
}
|
|
|
|
|
2017-02-14 13:41:01 +01:00
|
|
|
func TestI2CWriteByteData(t *testing.T) {
|
|
|
|
c := NewConnection(initI2CDevice(), 0x06)
|
|
|
|
err := c.WriteByteData(0x01, 0x01)
|
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
}
|
|
|
|
|
2017-04-11 12:29:31 +02:00
|
|
|
func TestI2CWriteByteDataAddressError(t *testing.T) {
|
|
|
|
c := NewConnection(initI2CDeviceAddressError(), 0x06)
|
|
|
|
err := c.WriteByteData(0x01, 0x01)
|
|
|
|
gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
|
|
|
|
}
|
|
|
|
|
2017-02-14 13:41:01 +01:00
|
|
|
func TestI2CWriteWordData(t *testing.T) {
|
|
|
|
c := NewConnection(initI2CDevice(), 0x06)
|
|
|
|
err := c.WriteWordData(0x01, 0x01)
|
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
}
|
2017-02-14 14:29:51 +01:00
|
|
|
|
2017-04-11 12:29:31 +02:00
|
|
|
func TestI2CWriteWordDataAddressError(t *testing.T) {
|
|
|
|
c := NewConnection(initI2CDeviceAddressError(), 0x06)
|
|
|
|
err := c.WriteWordData(0x01, 0x01)
|
|
|
|
gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
|
|
|
|
}
|
|
|
|
|
2017-02-27 20:53:01 +01:00
|
|
|
func TestI2CWriteBlockData(t *testing.T) {
|
2017-02-14 14:29:51 +01:00
|
|
|
c := NewConnection(initI2CDevice(), 0x06)
|
|
|
|
err := c.WriteBlockData(0x01, []byte{0x01, 0x02})
|
2017-02-27 20:53:01 +01:00
|
|
|
gobottest.Assert(t, err, nil)
|
2017-02-14 14:29:51 +01:00
|
|
|
}
|
2017-04-11 12:29:31 +02:00
|
|
|
|
|
|
|
func TestI2CWriteBlockDataAddressError(t *testing.T) {
|
|
|
|
c := NewConnection(initI2CDeviceAddressError(), 0x06)
|
|
|
|
err := c.WriteBlockData(0x01, []byte{0x01, 0x02})
|
|
|
|
gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
|
|
|
|
}
|
2022-03-19 21:16:31 +01:00
|
|
|
|
|
|
|
func Test_setBit(t *testing.T) {
|
|
|
|
var expectedVal uint8 = 129
|
|
|
|
actualVal := setBit(1, 7)
|
|
|
|
gobottest.Assert(t, expectedVal, actualVal)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_clearBit(t *testing.T) {
|
|
|
|
var expectedVal uint8
|
|
|
|
actualVal := clearBit(128, 7)
|
|
|
|
gobottest.Assert(t, expectedVal, actualVal)
|
|
|
|
}
|