forked from google/periph
/
gpiostreamtest.go
262 lines (228 loc) · 6.56 KB
/
gpiostreamtest.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright 2017 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
// Package gpiostreamtest enables testing device driver using gpiostream.PinIn
// or PinOut.
package gpiostreamtest
import (
"fmt"
"io"
"reflect"
"sync"
"periph.io/x/periph/conn"
"periph.io/x/periph/conn/conntest"
"periph.io/x/periph/conn/gpio"
"periph.io/x/periph/conn/gpio/gpiostream"
)
// InOp represents an expected replay StreamIn operation in PinIn.
type InOp struct {
gpio.Pull
gpiostream.BitStream
}
// PinIn implements gpiostream.PinIn that accepts BitStream only.
//
// Embed in a struct with gpiotest.Pin for more functionality.
type PinIn struct {
// These should be immutable.
N string
DontPanic bool
// Grab the Mutex before accessing the following members.
sync.Mutex
Ops []InOp
Count int
}
// Close verifies that all the expected Ops have been consumed.
func (p *PinIn) Close() error {
p.Lock()
defer p.Unlock()
if len(p.Ops) != p.Count {
return errorf(p.DontPanic, "gpiostreamtest: expected playback to be empty: I/O count %d; expected %d", p.Count, len(p.Ops))
}
return nil
}
// String implements conn.Resource.
func (p *PinIn) String() string {
return p.N
}
// Name implements pin.Pin.
func (p *PinIn) Name() string {
return p.N
}
// Number implements pin.Pin.
func (p *PinIn) Number() int {
return -1
}
// Function implements pin.Pin.
func (p *PinIn) Function() string {
return "StreamIn"
}
// Halt implements conn.Resource.
func (p *PinIn) Halt() error {
return nil
}
// StreamIn implements gpiostream.PinIn.
func (p *PinIn) StreamIn(pull gpio.Pull, b gpiostream.Stream) error {
s, ok := b.(*gpiostream.BitStream)
if !ok {
return errorf(p.DontPanic, "gpiostreamtest: unexpected StreamIn(%t)", b)
}
p.Lock()
defer p.Unlock()
if len(p.Ops) <= p.Count {
return errorf(p.DontPanic, "gpiostreamtest: unexpected StreamIn() (count #%d) expecting %#v", p.Count, b)
}
if s.Freq != p.Ops[p.Count].Freq {
return errorf(p.DontPanic, "gpiostreamtest: unexpected StreamIn() Freq (count #%d) expected %s, got %s", p.Count, p.Ops[p.Count].Freq, s.Freq)
}
if len(s.Bits) != len(p.Ops[p.Count].Bits) {
return errorf(p.DontPanic, "gpiostreamtest: unexpected StreamIn() len(Bits) (count #%d) expected %d, got %d", p.Count, len(p.Ops[p.Count].Bits), len(s.Bits))
}
if s.LSBF != p.Ops[p.Count].LSBF {
return errorf(p.DontPanic, "gpiostreamtest: unexpected StreamIn() LSBF (count #%d) expected %t, got %t", p.Count, p.Ops[p.Count].LSBF, s.LSBF)
}
if pull != p.Ops[p.Count].Pull {
return errorf(p.DontPanic, "gpiostreamtest: unexpected StreamIn() pull (count #%d) expected %s, got %s", p.Count, p.Ops[p.Count].Pull, pull)
}
copy(s.Bits, p.Ops[p.Count].Bits)
p.Count++
return nil
}
// PinOutPlayback implements gpiostream.PinOut.
//
// Embed in a struct with gpiotest.Pin for more functionality.
type PinOutPlayback struct {
// These should be immutable.
N string
DontPanic bool
// Grab the Mutex before accessing the following members.
sync.Mutex
Ops []gpiostream.Stream
Count int
}
// Close verifies that all the expected Ops have been consumed.
func (p *PinOutPlayback) Close() error {
p.Lock()
defer p.Unlock()
if len(p.Ops) != p.Count {
return errorf(p.DontPanic, "gpiostreamtest: expected playback to be empty: I/O count %d; expected %d", p.Count, len(p.Ops))
}
return nil
}
// String implements conn.Resource.
func (p *PinOutPlayback) String() string {
return p.N
}
// Name implements pin.Pin.
func (p *PinOutPlayback) Name() string {
return p.N
}
// Number implements pin.Pin.
func (p *PinOutPlayback) Number() int {
return -1
}
// Function implements pin.Pin.
func (p *PinOutPlayback) Function() string {
return "StreamOutPlayback"
}
// Halt implements conn.Resource.
func (p *PinOutPlayback) Halt() error {
return nil
}
// StreamOut implements gpiostream.PinOut.
func (p *PinOutPlayback) StreamOut(s gpiostream.Stream) error {
p.Lock()
defer p.Unlock()
if len(p.Ops) <= p.Count {
return errorf(p.DontPanic, "gpiostreamtest: unexpected StreamOut() (count #%d) expecting %#v", p.Count, s)
}
if !reflect.DeepEqual(s, p.Ops[p.Count]) {
return errorf(p.DontPanic, "gpiostreamtest: unexpected StreamOut() content (count #%d)\nexpected: %#v\ngot: %#v", p.Count, p.Ops[p.Count], s)
}
p.Count++
return nil
}
// PinOutRecord implements gpiostream.PinOut that records operations.
//
// Embed in a struct with gpiotest.Pin for more functionality.
type PinOutRecord struct {
// These should be immutable.
N string
DontPanic bool
// Grab the Mutex before accessing the following members.
sync.Mutex
Ops []gpiostream.Stream
}
// String implements conn.Resource.
func (p *PinOutRecord) String() string {
return p.N
}
// Name implements pin.Pin.
func (p *PinOutRecord) Name() string {
return p.N
}
// Number implements pin.Pin.
func (p *PinOutRecord) Number() int {
return -1
}
// Function implements pin.Pin.
func (p *PinOutRecord) Function() string {
return "StreamOutRecord"
}
// Halt implements conn.Resource.
func (p *PinOutRecord) Halt() error {
return nil
}
// StreamOut implements gpiostream.PinOut.
func (p *PinOutRecord) StreamOut(s gpiostream.Stream) error {
p.Lock()
defer p.Unlock()
d, err := deepCopy(s)
if err != nil {
return errorf(p.DontPanic, "gpiostreamtest: %s", err)
}
p.Ops = append(p.Ops, d)
return nil
}
//
// errorf is the internal implementation that optionally panic.
//
// If dontPanic is false, it panics instead.
func errorf(dontPanic bool, format string, a ...interface{}) error {
err := conntest.Errorf(format, a...)
if !dontPanic {
panic(err)
}
return err
}
func deepCopy(s gpiostream.Stream) (gpiostream.Stream, error) {
switch t := s.(type) {
case *gpiostream.BitStream:
o := &gpiostream.BitStream{Bits: make([]byte, len(t.Bits)), Freq: t.Freq, LSBF: t.LSBF}
copy(o.Bits, t.Bits)
return o, nil
case *gpiostream.EdgeStream:
o := &gpiostream.EdgeStream{Edges: make([]uint16, len(t.Edges)), Freq: t.Freq}
copy(o.Edges, t.Edges)
return o, nil
case *gpiostream.Program:
o := &gpiostream.Program{Loops: t.Loops}
for _, p := range t.Parts {
x, err := deepCopy(p)
if err != nil {
return nil, err
}
o.Parts = append(o.Parts, x)
}
return o, nil
default:
return nil, fmt.Errorf("invalid type %T", s)
}
}
var _ io.Closer = &PinIn{}
var _ io.Closer = &PinOutPlayback{}
var _ conn.Resource = &PinIn{}
var _ conn.Resource = &PinOutPlayback{}
var _ conn.Resource = &PinOutRecord{}
var _ gpiostream.PinIn = &PinIn{}
var _ gpiostream.PinOut = &PinOutPlayback{}
var _ gpiostream.PinOut = &PinOutRecord{}