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