-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscanner_linux.go
278 lines (238 loc) · 5.92 KB
/
scanner_linux.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
package bluetooth
import (
"fmt"
"os/exec"
"strings"
"unsafe"
"golang.org/x/sys/unix"
)
var _ Scanner = scanner{}
type scanner struct{}
const (
ioctlSize = 4
typHCI = 72 // 'H'
ogfLinkCtl = 0x01
ocfRemoteNameReq = 0x0019
eventRemoteNameResponse = 0x07
remoteNameRequestSize = 10
eventRemoteNameResponseSize = 255
evtRemoteNameReqComplete = 0x07
evtCmdStatus = 0x0f
evtCmdComplete = 0x0e
evtLeMetaEvent = 0x3e
solHci = 0
hciFilterOpt = 2
hciCommandPkt uint8 = 0x01
hciCommandHdrSize = 3
hciMaxEventSize = 260
hciEventHdrSize = 2
)
var hciInquiry = ioR(typHCI, 240, ioctlSize)
func NewScanner() Scanner {
return scanner{}
}
// ScanDeprecated is the old and deprecated implementation, I would like to keep it for a while as a fallback
func (scanner) ScanDeprecated() ([]Device, error) {
out, err := exec.Command("hcitool", "scan").Output()
if err != nil {
return nil, fmt.Errorf("hcitool scan: %s", err.Error())
}
var devices []Device
lines := strings.Split(string(out), "\n")
if len(lines) > 1 {
for i, line := range lines {
if i == 0 {
continue
}
lineElems := strings.Split(line, string([]byte{9}))
if len(lineElems) > 2 {
devices = append(devices, Device{
Name: lineElems[2],
MACAddress: lineElems[1],
})
}
}
}
return devices, nil
}
func (scanner) Scan() ([]Device, error) {
resp, err := inquiry()
if err != nil {
return nil, err
}
fd, err := openDevice()
if err != nil {
return nil, err
}
var devices []Device
for i := 0; i < int(resp.numberOfResponses); i++ {
name, err := readRemoteName(fd, resp.response[i])
if err != nil {
return nil, err
}
devices = append(devices, Device{
Name: string(name),
MACAddress: byteArrayToAddress(resp.response[i].bluetoothDeviceAddress),
})
}
return devices, nil
}
func inquiry() (hciInquiryRequest, error) {
fd, err := unix.Socket(unix.AF_BLUETOOTH, unix.SOCK_RAW|unix.SOCK_CLOEXEC, unix.BTPROTO_HCI)
if err != nil {
return hciInquiryRequest{}, fmt.Errorf("inquiry: open unix socket - %s", err.Error())
}
req := hciInquiryRequest{
length: 8,
}
req.lap[0] = 0x33
req.lap[1] = 0x8b
req.lap[2] = 0x9e
if err = ioctl(fd, hciInquiry, unsafe.Pointer(&req)); err != nil {
return hciInquiryRequest{}, fmt.Errorf("inquiry: hciInquiry ioctl call - %s", err.Error())
}
return req, nil
}
func openDevice() (int, error) {
fd, err := unix.Socket(unix.AF_BLUETOOTH, unix.SOCK_RAW|unix.SOCK_CLOEXEC, unix.BTPROTO_HCI)
if err != nil {
return 0, fmt.Errorf("openDevice: open unix socket - %s", err.Error())
}
sock := unix.SockaddrHCI{
Dev: 0,
Channel: 0,
}
err = unix.Bind(fd, &sock)
if err != nil {
return 0, fmt.Errorf("openDevice: bind hci to unix socket - %s", err.Error())
}
return fd, nil
}
func readRemoteName(fd int, info inquiryInfo) ([]byte, error) {
var response = remoteNameResponse{}
var request = remoteNameRequest{
bluetoothDeviceAddress: addressCopy(info.bluetoothDeviceAddress),
scanRepMode: info.scanRepMode,
clockOffset: info.clockOffset,
}
var hr = hciRequest{
ogf: ogfLinkCtl,
ocf: ocfRemoteNameReq,
event: eventRemoteNameResponse,
request: &request,
requestLen: remoteNameRequestSize,
response: &response,
responseLen: eventRemoteNameResponseSize,
}
opcode := opcodePack(hr.ogf, hr.ocf)
// TODO add better implementation for that filter
var filter = hciFilter{
typeMask: 16,
eventMask: [2]uint32{
0: 49280,
1: 1073741824,
},
opcode: opcode,
}
err := setsockopt(fd, solHci, hciFilterOpt, unsafe.Pointer(&filter), unsafe.Sizeof(filter))
if err != nil {
return nil, fmt.Errorf("readRemoteName: call setsockopt with hciFilterOpt - %s", err.Error())
}
err = sendCommand(fd, hr.ogf, hr.ocf, uint8(hr.requestLen), hr.request)
if err != nil {
return nil, fmt.Errorf("readRemoteName: call sendCommand - %s", err.Error())
}
err = pollSocket(fd)
if err != nil {
return nil, fmt.Errorf("readRemoteName: call pollSocket - %s", err.Error())
}
var try = 10
for i := 0; i < try; i++ {
name, ok, err := readEvent(fd)
if err != nil {
return nil, err
}
if ok {
return name, nil
}
}
return nil, nil
}
func readEvent(fd int) ([]byte, bool, error) {
var buf = make([]byte, hciMaxEventSize)
_, err := unix.Read(fd, buf)
if err != nil {
return nil, false, err
}
hdr := (*hciEventHdr)(unsafe.Pointer(&buf[1]))
ptr := 1 + hciEventHdrSize
switch hdr.event {
case evtRemoteNameReqComplete:
data := (*remoteNameResponse)(unsafe.Pointer(&buf[ptr]))
return normalizeName(data.name), true, nil
case evtCmdStatus:
return nil, false, nil
case evtCmdComplete:
return nil, false, nil
case evtLeMetaEvent:
return nil, false, nil
}
return nil, false, nil
}
func pollSocket(fd int) error {
timeout := 100000
pollFd := unix.PollFd{
Fd: int32(fd),
Events: unix.POLLIN,
}
_, err := unix.Poll([]unix.PollFd{pollFd}, timeout)
return err
}
func sendCommand(fd int, ogf uint16, ocf uint16, reqLen uint8, req *remoteNameRequest) error {
var commandType = hciCommandPkt
var hc hciCommandHdr
hc.opcode = opcodePack(ogf, ocf)
hc.pLen = reqLen
var iv = []unix.Iovec{
{
Base: (*byte)(unsafe.Pointer(&commandType)),
Len: 1,
},
{
Base: (*byte)(unsafe.Pointer(&hc)),
Len: hciCommandHdrSize,
},
}
if reqLen > 0 {
iv = append(iv, unix.Iovec{
Base: (*byte)(unsafe.Pointer(req)),
Len: uint64(reqLen),
})
}
_, err := writev(fd, iv)
return err
}
func opcodePack(ogf, ocf uint16) uint16 {
return (ocf & 0x03ff) | (ogf << 10)
}
func addressCopy(in [6]uint8) [6]uint8 {
var out [6]uint8
//nolint:gosimple
for i := range in {
out[i] = in[i]
}
return out
}
func normalizeName(in [248]uint8) []byte {
var out []byte
for i := range in {
if in[i] != 0 {
if in[i] < 32 || in[i] == 127 {
out = append(out, '.')
} else {
out = append(out, in[i])
}
}
}
return out
}