-
Notifications
You must be signed in to change notification settings - Fork 14
/
socket_example_test.go
207 lines (178 loc) · 4.17 KB
/
socket_example_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
// Copyright 2022 The go-daq 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 canbus_test
import (
"fmt"
"log"
"github.com/go-daq/canbus"
"golang.org/x/sys/unix"
)
func ExampleSocket() {
recv, err := canbus.New()
if err != nil {
log.Fatal(err)
}
defer recv.Close()
send, err := canbus.New()
if err != nil {
log.Fatal(err)
}
defer send.Close()
err = recv.Bind("vcan0")
if err != nil {
log.Fatalf("could not bind recv socket: %+v", err)
}
err = send.Bind("vcan0")
if err != nil {
log.Fatalf("could not bind send socket: %+v", err)
}
for i := 0; i < 5; i++ {
_, err := send.Send(canbus.Frame{
ID: 0x123,
Data: []byte(fmt.Sprintf("data-%02d", i)),
Kind: canbus.SFF,
})
if err != nil {
log.Fatalf("could not send frame %d: %+v", i, err)
}
}
for i := 0; i < 5; i++ {
frame, err := recv.Recv()
if err != nil {
log.Fatalf("could not recv frame %d: %+v", i, err)
}
fmt.Printf("frame-%02d: %q (id=0x%x)\n", i, frame.Data, frame.ID)
}
// Output:
// frame-00: "data-00" (id=0x123)
// frame-01: "data-01" (id=0x123)
// frame-02: "data-02" (id=0x123)
// frame-03: "data-03" (id=0x123)
// frame-04: "data-04" (id=0x123)
}
func ExampleSocket_eFF() {
recv, err := canbus.New()
if err != nil {
log.Fatal(err)
}
defer recv.Close()
send, err := canbus.New()
if err != nil {
log.Fatal(err)
}
defer send.Close()
err = recv.Bind("vcan0")
if err != nil {
log.Fatalf("could not bind recv socket: %+v", err)
}
err = send.Bind("vcan0")
if err != nil {
log.Fatalf("could not bind send socket: %+v", err)
}
for i := 0; i < 5; i++ {
_, err := send.Send(canbus.Frame{
ID: 0x1234567,
Data: []byte(fmt.Sprintf("data-%02d", i)),
Kind: canbus.EFF,
})
if err != nil {
log.Fatalf("could not send frame %d: %+v", i, err)
}
}
for i := 0; i < 5; i++ {
frame, err := recv.Recv()
if err != nil {
log.Fatalf("could not recv frame %d: %+v", i, err)
}
fmt.Printf("frame-%02d: %q (id=0x%x)\n", i, frame.Data, frame.ID)
}
// Output:
// frame-00: "data-00" (id=0x1234567)
// frame-01: "data-01" (id=0x1234567)
// frame-02: "data-02" (id=0x1234567)
// frame-03: "data-03" (id=0x1234567)
// frame-04: "data-04" (id=0x1234567)
}
func ExampleSocket_setFilters() {
recv1, err := canbus.New()
if err != nil {
log.Fatal(err)
}
defer recv1.Close()
err = recv1.SetFilters([]unix.CanFilter{
{Id: 0x123, Mask: unix.CAN_SFF_MASK},
})
if err != nil {
log.Fatalf("could not set CAN filters: %+v", err)
}
recv2, err := canbus.New()
if err != nil {
log.Fatal(err)
}
defer recv2.Close()
err = recv2.SetFilters([]unix.CanFilter{
{Id: 0x123 | unix.CAN_INV_FILTER, Mask: unix.CAN_SFF_MASK},
})
if err != nil {
log.Fatalf("could not set CAN filters: %+v", err)
}
send, err := canbus.New()
if err != nil {
log.Fatal(err)
}
defer send.Close()
err = recv1.Bind("vcan0")
if err != nil {
log.Fatalf("could not bind recv socket: %+v", err)
}
err = recv2.Bind("vcan0")
if err != nil {
log.Fatalf("could not bind recv socket: %+v", err)
}
err = send.Bind("vcan0")
if err != nil {
log.Fatalf("could not bind send socket: %+v", err)
}
for i := 0; i < 6; i++ {
var id uint32 = 0x123
if i%2 == 0 {
id = 0x321
}
_, err := send.Send(canbus.Frame{
ID: id,
Data: []byte(fmt.Sprintf("data-%02d", i)),
Kind: canbus.SFF,
})
if err != nil {
log.Fatalf("could not send frame %d: %+v", i, err)
}
}
fmt.Printf("recv1:\n")
for i := 0; i < 3; i++ {
frame, err := recv1.Recv()
if err != nil {
log.Fatalf("could not recv frame %d: %+v", i, err)
}
fmt.Printf("frame-%02d: %q (id=0x%x)\n", i, frame.Data, frame.ID)
}
fmt.Printf("\n")
fmt.Printf("recv2:\n")
for i := 0; i < 3; i++ {
frame, err := recv2.Recv()
if err != nil {
log.Fatalf("could not recv frame %d: %+v", i, err)
}
fmt.Printf("frame-%02d: %q (id=0x%x)\n", i, frame.Data, frame.ID)
}
// Output:
// recv1:
// frame-00: "data-01" (id=0x123)
// frame-01: "data-03" (id=0x123)
// frame-02: "data-05" (id=0x123)
//
// recv2:
// frame-00: "data-00" (id=0x321)
// frame-01: "data-02" (id=0x321)
// frame-02: "data-04" (id=0x321)
}