-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
sensor_test.go
1049 lines (914 loc) · 26.2 KB
/
sensor_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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright ©2016 The ev3go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ev3dev_test
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sort"
"strconv"
"strings"
"sync"
"syscall"
"testing"
"time"
"github.com/ev3go/sisyphus"
. "github.com/ev3go/ev3dev"
)
// sensor is a sensor sysfs directory.
type sensor struct {
address string
driver string
// mu protects the underscore
// prefix attributes below.
mu sync.Mutex
_lastCommand string
_commands []string
_direct []byte
_firmwareVersion string
_mode string
_modes []string
_units map[string]string
_decimals map[string]int
_binData []byte
_binDataFormat string
_values []string
_pollRate int
_uevent map[string]string
t *testing.T
}
func (s *sensor) commands() []string {
s.mu.Lock()
defer s.mu.Unlock()
return s._commands
}
func (s *sensor) lastCommand() string {
s.mu.Lock()
defer s.mu.Unlock()
return s._lastCommand
}
func (s *sensor) direct() []byte {
s.mu.Lock()
defer s.mu.Unlock()
bytes := make([]byte, len(s._direct))
copy(bytes, s._direct)
return bytes
}
func (s *sensor) modes() []string {
s.mu.Lock()
defer s.mu.Unlock()
return s._modes
}
func (s *sensor) units() string {
s.mu.Lock()
defer s.mu.Unlock()
return s._units[s._mode]
}
func (s *sensor) decimals() int {
s.mu.Lock()
defer s.mu.Unlock()
return s._decimals[s._mode]
}
func (s *sensor) binData() []byte {
s.mu.Lock()
defer s.mu.Unlock()
return s._binData
}
func (s *sensor) binDataFormat() string {
s.mu.Lock()
defer s.mu.Unlock()
return s._binDataFormat
}
func (s *sensor) values() []string {
s.mu.Lock()
defer s.mu.Unlock()
return s._values
}
func (s *sensor) uevent() map[string]string {
s.mu.Lock()
defer s.mu.Unlock()
return s._uevent
}
// sensorAddress is the address attribute.
type sensorAddress sensor
// ReadAt satisfies the io.ReaderAt interface.
func (s *sensorAddress) ReadAt(b []byte, offset int64) (int, error) {
return readAt(b, offset, s.address)
}
// Size returns the length of the backing data and a nil error.
func (s *sensorAddress) Size() (int64, error) {
return size(s.address), nil
}
// sensorDriver is the driver_name attribute.
type sensorDriver sensor
// ReadAt satisfies the io.ReaderAt interface.
func (s *sensorDriver) ReadAt(b []byte, offset int64) (int, error) {
return readAt(b, offset, s.driver)
}
// Size returns the length of the backing data and a nil error.
func (s *sensorDriver) Size() (int64, error) {
return size(s.driver), nil
}
// sensorCommands is the commands attribute.
type sensorCommands sensor
// ReadAt satisfies the io.ReaderAt interface.
func (s *sensorCommands) ReadAt(b []byte, offset int64) (int, error) {
return readAt(b, offset, s)
}
// Size returns the length of the backing data and a nil error.
func (s *sensorCommands) Size() (int64, error) {
return size(s), nil
}
// String returns a string representation of the attribute.
func (s *sensorCommands) String() string {
s.mu.Lock()
defer s.mu.Unlock()
sort.Strings(s._commands)
return strings.Join(s._commands, " ")
}
// sensorCommand is the command attribute.
type sensorCommand sensor
// Truncate is a no-op.
func (s *sensorCommand) Truncate(_ int64) error { return nil }
// WriteAt satisfies the io.WriterAt interface.
func (s *sensorCommand) WriteAt(b []byte, off int64) (int, error) {
s.mu.Lock()
defer s.mu.Unlock()
command := string(chomp(b))
for _, c := range s._commands {
if command == c {
s._lastCommand = command
return len(b), nil
}
}
return len(b), syscall.EINVAL
}
// Size returns the length of the backing data and a nil error.
func (s *sensorCommand) Size() (int64, error) {
return size(s._lastCommand), nil
}
// sensorModes is the modes attribute.
type sensorModes sensor
// ReadAt satisfies the io.ReaderAt interface.
func (s *sensorModes) ReadAt(b []byte, offset int64) (int, error) {
return readAt(b, offset, s)
}
// Size returns the length of the backing data and a nil error.
func (s *sensorModes) Size() (int64, error) {
return size(s), nil
}
// String returns a string representation of the attribute.
func (s *sensorModes) String() string {
s.mu.Lock()
defer s.mu.Unlock()
sort.Strings(s._modes)
return strings.Join(s._modes, " ")
}
// sensorFirmwareVersion is the fw_version attribute.
type sensorFirmwareVersion sensor
// ReadAt satisfies the io.ReaderAt interface.
func (s *sensorFirmwareVersion) ReadAt(b []byte, offset int64) (int, error) {
return readAt(b, offset, s._firmwareVersion)
}
// Size returns the length of the backing data and a nil error.
func (s *sensorFirmwareVersion) Size() (int64, error) {
return size(s._firmwareVersion), nil
}
// sensorMode is the mode attribute.
type sensorMode sensor
// ReadAt satisfies the io.ReaderAt interface.
func (s *sensorMode) ReadAt(b []byte, offset int64) (int, error) {
return readAt(b, offset, s)
}
// Truncate is a no-op.
func (s *sensorMode) Truncate(_ int64) error { return nil }
// WriteAt satisfies the io.WriterAt interface.
func (s *sensorMode) WriteAt(b []byte, off int64) (int, error) {
s.mu.Lock()
defer s.mu.Unlock()
mode := string(chomp(b))
for _, c := range s._modes {
if mode == c {
s._mode = mode
return len(b), nil
}
}
return len(b), syscall.EINVAL
}
// Size returns the length of the backing data and a nil error.
func (s *sensorMode) Size() (int64, error) {
return size(s), nil
}
// String returns a string representation of the attribute.
func (s *sensorMode) String() string {
s.mu.Lock()
defer s.mu.Unlock()
return s._mode
}
// sensorBinDataFormat is the bin_data_format attribute.
type sensorBinDataFormat sensor
// ReadAt satisfies the io.ReaderAt interface.
func (s *sensorBinDataFormat) ReadAt(b []byte, offset int64) (int, error) {
return readAt(b, offset, s)
}
// Size returns the length of the backing data and a nil error.
func (s *sensorBinDataFormat) Size() (int64, error) {
return size(s), nil
}
// String returns a string representation of the attribute.
func (s *sensorBinDataFormat) String() string {
s.mu.Lock()
defer s.mu.Unlock()
return s._binDataFormat
}
// sensorBinData is the bin_datas attribute.
type sensorBinData sensor
// ReadAt satisfies the io.ReaderAt interface.
func (s *sensorBinData) ReadAt(b []byte, offset int64) (int, error) {
if len(b) == 0 {
return 0, nil
}
d := s.String()
if offset >= int64(len(d)) {
return 0, io.EOF
}
n := copy(b, d[offset:])
if n <= len(b) {
return n, io.EOF
}
return n, nil
}
// Size returns the length of the backing data and a nil error.
func (s *sensorBinData) Size() (int64, error) {
return size(s), nil
}
// String returns a string representation of the attribute.
func (s *sensorBinData) String() string {
s.mu.Lock()
defer s.mu.Unlock()
return string(s._binData)
}
// sensorDirect is the direct attribute.
type sensorDirect sensor
// ReadAt satisfies the io.ReaderAt interface.
func (s *sensorDirect) ReadAt(b []byte, offset int64) (int, error) {
if len(b) == 0 {
return 0, nil
}
d := s.String()
if offset >= int64(len(d)) {
return 0, io.EOF
}
n := copy(b, d[offset:])
if n <= len(b) {
return n, io.EOF
}
return n, nil
}
// Truncate truncates the Bytes at n bytes from the beginning of the slice.
func (s *sensorDirect) Truncate(n int64) error {
s.mu.Lock()
defer s.mu.Unlock()
if n < 0 || n > int64(len(s._direct)) {
return syscall.EINVAL
}
tail := s._direct[n:cap(s._direct)]
for i := range tail {
tail[i] = 0
}
s._direct = s._direct[:n]
return nil
}
// WriteAt satisfies the io.WriterAt interface.
func (s *sensorDirect) WriteAt(b []byte, off int64) (int, error) {
s.mu.Lock()
defer s.mu.Unlock()
if off >= int64(cap(s._direct)) {
t := make([]byte, off+int64(len(b)))
copy(t, s._direct)
s._direct = t
}
s._direct = s._direct[:off]
s._direct = append(s._direct, b...)
return len(b), nil
}
// Size returns the length of the backing data and a nil error.
func (s *sensorDirect) Size() (int64, error) {
return size(s), nil
}
// String returns a string representation of the attribute.
func (s *sensorDirect) String() string {
s.mu.Lock()
defer s.mu.Unlock()
return string(s._direct)
}
// sensorUnits is the units attribute.
type sensorUnits sensor
// ReadAt satisfies the io.ReaderAt interface.
func (s *sensorUnits) ReadAt(b []byte, offset int64) (int, error) {
return readAt(b, offset, s)
}
// Size returns the length of the backing data and a nil error.
func (s *sensorUnits) Size() (int64, error) {
return size(s), nil
}
// String returns a string representation of the attribute.
func (s *sensorUnits) String() string {
s.mu.Lock()
defer s.mu.Unlock()
return s._units[s._mode]
}
// sensorPollRate is the poll_rate attribute.
type sensorPollRate sensor
// ReadAt satisfies the io.ReaderAt interface.
func (s *sensorPollRate) ReadAt(b []byte, offset int64) (int, error) {
return readAt(b, offset, s)
}
// Truncate is a no-op.
func (s *sensorPollRate) Truncate(_ int64) error { return nil }
// WriteAt satisfies the io.WriterAt interface.
func (s *sensorPollRate) WriteAt(b []byte, off int64) (int, error) {
s.mu.Lock()
defer s.mu.Unlock()
i, err := strconv.Atoi(string(chomp(b)))
if err != nil {
s.t.Errorf("unexpected error: %v", err)
return len(b), syscall.EINVAL
}
s._pollRate = i
return len(b), nil
}
// Size returns the length of the backing data and a nil error.
func (s *sensorPollRate) Size() (int64, error) {
return size(s), nil
}
// String returns a string representation of the attribute.
func (s *sensorPollRate) String() string {
s.mu.Lock()
defer s.mu.Unlock()
return strconv.Itoa(s._pollRate)
}
// sensorDecimals is the decimals attribute.
type sensorDecimals sensor
// ReadAt satisfies the io.ReaderAt interface.
func (s *sensorDecimals) ReadAt(b []byte, offset int64) (int, error) {
return readAt(b, offset, s)
}
// Size returns the length of the backing data and a nil error.
func (s *sensorDecimals) Size() (int64, error) {
return size(s), nil
}
// String returns a string representation of the attribute.
func (s *sensorDecimals) String() string {
s.mu.Lock()
defer s.mu.Unlock()
return strconv.Itoa(s._decimals[s._mode])
}
// sensorNumValues is the num_values attribute.
type sensorNumValues sensor
// ReadAt satisfies the io.ReaderAt interface.
func (s *sensorNumValues) ReadAt(b []byte, offset int64) (int, error) {
return readAt(b, offset, s)
}
// Size returns the length of the backing data and a nil error.
func (s *sensorNumValues) Size() (int64, error) {
return size(s), nil
}
// String returns a string representation of the attribute.
func (s *sensorNumValues) String() string {
s.mu.Lock()
defer s.mu.Unlock()
return strconv.Itoa(len(s._values))
}
// sensorValue is the valueN attribute.
type sensorValue struct {
n int
*sensor
}
// ReadAt satisfies the io.ReaderAt interface.
func (s sensorValue) ReadAt(b []byte, offset int64) (int, error) {
return readAt(b, offset, s)
}
// Size returns the length of the backing data and a nil error.
func (s sensorValue) Size() (int64, error) {
return size(s), nil
}
// String returns a string representation of the attribute.
func (s sensorValue) String() string {
s.mu.Lock()
defer s.mu.Unlock()
if len(s._values) <= s.n {
return "\n"
}
return s._values[s.n]
}
// sensorTextValues is the text_values attribute.
type sensorTextValues sensor
// ReadAt satisfies the io.ReaderAt interface.
func (s *sensorTextValues) ReadAt(b []byte, offset int64) (int, error) {
return readAt(b, offset, s)
}
// Size returns the length of the backing data and a nil error.
func (s *sensorTextValues) Size() (int64, error) {
return size(s), nil
}
// String returns a string representation of the attribute.
func (s *sensorTextValues) String() string {
s.mu.Lock()
defer s.mu.Unlock()
return strings.Join(s._values, " ")
}
// sensorUevent is the uevent attribute.
type sensorUevent sensor
// ReadAt satisfies the io.ReaderAt interface.
func (s *sensorUevent) ReadAt(b []byte, offset int64) (int, error) {
return readAt(b, offset, s)
}
// Size returns the length of the backing data and a nil error.
func (s *sensorUevent) Size() (int64, error) {
return size(s), nil
}
// String returns a string representation of the attribute.
func (s *sensorUevent) String() string {
s.mu.Lock()
defer s.mu.Unlock()
e := make([]string, 0, len(s._uevent))
for k, v := range s._uevent {
e = append(e, fmt.Sprintf("%s=%s", k, v))
}
sort.Strings(e)
return strings.Join(e, "\n")
}
type sensorConn struct {
id int
sensor *sensor
}
func connectedSensors(c ...sensorConn) []sisyphus.Node {
n := make([]sisyphus.Node, len(c))
for i, s := range c {
n[i] = d(fmt.Sprintf("sensor%d", s.id), 0775).With(
ro(AddressName, 0444, (*sensorAddress)(s.sensor)),
ro(DriverNameName, 0444, (*sensorDriver)(s.sensor)),
ro(FirmwareVersion, 0444, (*sensorFirmwareVersion)(s.sensor)),
ro(ModesName, 0444, (*sensorModes)(s.sensor)),
rw(ModeName, 0666, (*sensorMode)(s.sensor)),
ro(CommandsName, 0444, (*sensorCommands)(s.sensor)),
wo(CommandName, 0222, (*sensorCommand)(s.sensor)),
ro(BinDataFormatName, 0444, (*sensorBinDataFormat)(s.sensor)),
ro(BinDataName, 0444, (*sensorBinData)(s.sensor)),
rw(DirectName, 0666, (*sensorDirect)(s.sensor)),
rw(PollRateName, 0666, (*sensorPollRate)(s.sensor)),
ro(UnitsName, 0444, (*sensorUnits)(s.sensor)),
ro(DecimalsName, 0444, (*sensorDecimals)(s.sensor)),
ro(NumValuesName, 0444, (*sensorNumValues)(s.sensor)),
ro(ValueName+"0", 0444, sensorValue{0, s.sensor}),
ro(ValueName+"1", 0444, sensorValue{1, s.sensor}),
ro(TextValuesName, 0444, (*sensorTextValues)(s.sensor)),
ro(UeventName, 0444, (*sensorUevent)(s.sensor)),
)
}
return n
}
func sensorsysfs(s ...sensorConn) *sisyphus.FileSystem {
return sisyphus.NewFileSystem(0775, clock).With(
d("sys", 0775).With(
d("class", 0775).With(
d("lego-sensor", 0775).With(
connectedSensors(s...)...,
),
),
),
).Sync()
}
func TestSensor(t *testing.T) {
const driver = "lego-ev3-gyro"
conn := []sensorConn{
{
id: 5,
sensor: &sensor{
address: "in2",
driver: driver,
_firmwareVersion: "v1",
_modes: []string{"GYRO-ANG", "GYRO-RATE", "GYRO-FAS", "GYRO-G&A", "GYRO-CAL"},
_mode: "GYRO-ANG",
_units: map[string]string{"GYRO-ANG": "deg", "GYRO-RATE": "d/s"},
_decimals: map[string]int{"GYRO-ANG": 0, "GYRO-RATE": 1},
_commands: []string{"operate", "twirl"},
_binDataFormat: "s16",
_binData: []byte{0x01, 0x00, 0x02, 0x00},
_values: []string{"1", "2"},
_direct: []byte("initial state"),
_uevent: map[string]string{
"LEGO_ADDRESS": "in2",
"LEGO_DRIVER_NAME": driver,
},
t: t,
},
},
{
id: 7,
sensor: &sensor{
address: "in4",
driver: driver,
_modes: []string{"GYRO-ANG", "GYRO-RATE", "GYRO-FAS", "GYRO-G&A", "GYRO-CAL"},
_mode: "GYRO-ANG",
_units: map[string]string{"GYRO-ANG": "deg", "GYRO-RATE": "d/s"},
_binDataFormat: "s16",
_binData: []byte{0x01, 0x00, 0x02, '\n'},
t: t,
},
},
}
fs := sensorsysfs(conn...)
unmount := serve(fs, t)
defer unmount()
t.Run("new Sensor", func(t *testing.T) {
for _, r := range []struct{ port, driver string }{
{port: "", driver: conn[0].sensor.driver},
{port: conn[0].sensor.address, driver: conn[0].sensor.driver},
{port: conn[0].sensor.address, driver: ""},
} {
got, err := SensorFor(r.port, r.driver)
if r.driver == driver {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
} else {
merr, ok := err.(DriverMismatch)
if !ok {
t.Errorf("unexpected error type for driver mismatch: got:%T want:%T", err, merr)
}
if merr.Have != driver {
t.Errorf("unexpected value for have driver error: got:%q want:%q", merr.Have, conn[0].sensor.driver)
}
}
ok, err := IsConnected(got)
if err != nil {
t.Errorf("unexpected error getting connection status:%v", err)
}
if !ok {
t.Error("expected device to be connected")
}
gotAddr, err := AddressOf(got)
if err != nil {
t.Errorf("unexpected error getting address: %v", err)
}
wantAddr := conn[0].sensor.address
if gotAddr != wantAddr {
t.Errorf("unexpected value for address: got:%q want:%q", gotAddr, wantAddr)
}
gotDriver, err := DriverFor(got)
if err != nil {
t.Errorf("unexpected error getting driver name:%v", err)
}
wantDriver := conn[0].sensor.driver
if gotDriver != wantDriver {
t.Errorf("unexpected value for driver name: got:%q want:%q", gotDriver, wantDriver)
}
methodDriver := got.Driver()
if methodDriver != wantDriver {
t.Errorf("unexpected value for driver name: got:%q want:%q", methodDriver, wantDriver)
}
}
})
t.Run("Next", func(t *testing.T) {
s, err := SensorFor(conn[0].sensor.address, conn[0].sensor.driver)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
got, err := s.Next()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
ok, err := IsConnected(got)
if err != nil {
t.Errorf("unexpected error getting connection status:%v", err)
}
if !ok {
t.Error("expected device to be connected")
}
gotAddr, err := AddressOf(got)
if err != nil {
t.Errorf("unexpected error getting address: %v", err)
}
wantAddr := conn[1].sensor.address
if gotAddr != wantAddr {
t.Errorf("unexpected value for address: got:%q want:%q", gotAddr, wantAddr)
}
gotDriver, err := DriverFor(got)
if err != nil {
t.Errorf("unexpected error getting driver name:%v", err)
}
wantDriver := conn[1].sensor.driver
if gotDriver != wantDriver {
t.Errorf("unexpected value for driver name: got:%q want:%q", gotDriver, wantDriver)
}
})
t.Run("FindAfter", func(t *testing.T) {
var last *Sensor
for _, c := range conn {
got := new(Sensor)
err := FindAfter(last, got, driver)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
last = got
ok, err := IsConnected(got)
if err != nil {
t.Errorf("unexpected error getting connection status:%v", err)
}
if !ok {
t.Error("expected device to be connected")
}
gotAddr, err := AddressOf(got)
if err != nil {
t.Errorf("unexpected error getting address: %v", err)
}
wantAddr := c.sensor.address
if gotAddr != wantAddr {
t.Errorf("unexpected value for address: got:%q want:%q", gotAddr, wantAddr)
}
gotDriver, err := DriverFor(got)
if err != nil {
t.Errorf("unexpected error getting driver name:%v", err)
}
wantDriver := c.sensor.driver
if gotDriver != wantDriver {
t.Errorf("unexpected value for driver name: got:%q want:%q", gotDriver, wantDriver)
}
gotFirmwareVersion := got.FirmwareVersion()
wantFirmwareVersion := c.sensor._firmwareVersion
if gotFirmwareVersion != wantFirmwareVersion {
t.Errorf("unexpected value for firmware version: got:%q want:%q", gotFirmwareVersion, wantFirmwareVersion)
}
}
})
t.Run("Mode", func(t *testing.T) {
s, err := SensorFor(conn[0].sensor.address, conn[0].sensor.driver)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
modes := s.Modes()
want := conn[0].sensor.modes()
if !reflect.DeepEqual(modes, want) {
t.Errorf("unexpected modes value: got:%q want:%q", modes, want)
}
for _, mode := range modes {
err := s.SetMode(mode).Err()
if err != nil {
t.Errorf("unexpected error for mode %q: %v", mode, err)
}
got, err := s.Mode()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
want := mode
if got != want {
t.Errorf("unexpected mode value: got:%q want:%q", got, want)
}
}
for _, mode := range []string{"invalid", "another"} {
err := s.SetMode(mode).Err()
if err == nil {
t.Errorf("expected error for mode %q", mode)
}
got, err := s.Mode()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
dontwant := mode
if got == dontwant {
t.Errorf("unexpected invalid mode value: got:%q, don't want:%q", got, dontwant)
}
}
})
t.Run("Command", func(t *testing.T) {
for _, c := range conn {
s, err := SensorFor(c.sensor.address, c.sensor.driver)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
commands := s.Commands()
want := c.sensor.commands()
if !reflect.DeepEqual(commands, want) {
t.Errorf("unexpected commands value: got:%q want:%q", commands, want)
}
for _, command := range commands {
err := s.Command(command).Err()
if err != nil {
t.Errorf("unexpected error for command %q: %v", command, err)
}
got := c.sensor.lastCommand()
want := command
if got != want {
t.Errorf("unexpected command value: got:%q want:%q", got, want)
}
}
for _, command := range []string{"invalid", "another"} {
err := s.Command(command).Err()
if err == nil {
t.Errorf("expected error for command %q", command)
}
got := c.sensor.lastCommand()
dontwant := command
if got == dontwant {
t.Errorf("unexpected invalid command value: got:%q don't want:%q", got, dontwant)
}
}
}
})
t.Run("Binary data", func(t *testing.T) {
for _, c := range conn {
s, err := SensorFor(c.sensor.address, c.sensor.driver)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
format := s.BinDataFormat()
wantFmt := c.sensor.binDataFormat()
if format != wantFmt {
t.Errorf("unexpected bin data format value: got:%q want:%q", format, wantFmt)
}
data, err := s.BinData()
if err != nil {
t.Fatalf("unexpected error getting bin data: %v", err)
}
wantData := c.sensor.binData()
if !reflect.DeepEqual(data, wantData) {
t.Errorf("unexpected bin data value: got:%#x want:%#x", data, wantData)
}
}
})
t.Run("Direct", func(t *testing.T) {
s, err := SensorFor(conn[0].sensor.address, conn[0].sensor.driver)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
f, err := s.Direct(os.O_RDWR)
if err != nil {
t.Fatalf("unexpected error getting direct connection: %v", err)
}
defer f.Close()
got, err := ioutil.ReadAll(f)
if err != nil {
t.Fatalf("unexpected error reading direct connection: %v", err)
}
want := conn[0].sensor.direct()
if !reflect.DeepEqual(got, want) {
t.Errorf("unexpected direct value: got:%q want:%q", got, want)
}
err = f.Truncate(0)
if err != nil {
t.Fatalf("unexpected error truncating: %v", err)
}
_, err = f.Seek(0, io.SeekStart)
if err != nil {
t.Fatalf("unexpected error seeking to start: %v", err)
}
new := []byte("new data")
_, err = f.Write(new)
if err != nil {
t.Fatalf("unexpected error writing new data: %v", err)
}
_, err = f.Seek(0, io.SeekStart)
if err != nil {
t.Fatalf("unexpected error seeking to start: %v", err)
}
got, err = ioutil.ReadAll(f)
if err != nil {
t.Fatalf("unexpected error reading direct connection: %v", err)
}
if !reflect.DeepEqual(got, new) {
t.Errorf("unexpected direct value: got:%q want:%q", got, new)
}
})
t.Run("Poll rate", func(t *testing.T) {
s, err := SensorFor(conn[0].sensor.address, conn[0].sensor.driver)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
for _, r := range []time.Duration{1 * time.Millisecond, 100 * time.Millisecond, 1 * time.Second} {
err := s.SetPollRate(r).Err()
if err != nil {
t.Errorf("unexpected error for set poll rate %d: %v", r, err)
}
got, err := s.PollRate()
if err != nil {
t.Errorf("unexpected error getting poll rate: %v", err)
}
want := r
if got != want {
t.Errorf("unexpected poll rate value: got:%d want:%d", got, want)
}
}
})
t.Run("Units", func(t *testing.T) {
s, err := SensorFor(conn[0].sensor.address, conn[0].sensor.driver)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
for _, mode := range s.Modes() {
err := s.SetMode(mode).Err()
if err != nil {
t.Errorf("unexpected error for mode %q: %v", mode, err)
}
err = fs.InvalidatePath(filepath.Join(s.Path(), s.String(), UnitsName))
if err != nil {
t.Fatalf("unexpected error invalidating units: %v", err)
}
got := s.Units()
want := conn[0].sensor.units()
if got != want {
t.Errorf("unexpected mode value: got:%q want:%q", got, want)
}
}
})
t.Run("Decimals", func(t *testing.T) {
s, err := SensorFor(conn[0].sensor.address, conn[0].sensor.driver)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
for _, mode := range s.Modes() {
err := s.SetMode(mode).Err()
if err != nil {
t.Errorf("unexpected error for mode %q: %v", mode, err)
}
err = fs.InvalidatePath(filepath.Join(s.Path(), s.String(), DecimalsName))
if err != nil {
t.Fatalf("unexpected error invalidating decimals: %v", err)
}
got := s.Decimals()
want := conn[0].sensor.decimals()
if got != want {
t.Errorf("unexpected decimals value: got:%d want:%d", got, want)
}
}
})
t.Run("Number of values", func(t *testing.T) {
for _, c := range conn {
s, err := SensorFor(c.sensor.address, c.sensor.driver)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
got := s.NumValues()
want := len(c.sensor.values())
if got != want {
t.Errorf("unexpected num values: got:%d want:%d", got, want)
}
}
})
t.Run("Value", func(t *testing.T) {
for _, c := range conn {
s, err := SensorFor(c.sensor.address, c.sensor.driver)
if err != nil {
t.Fatalf("unexpected error: %v", err)