This repository has been archived by the owner on Dec 5, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
hci.go
311 lines (269 loc) · 7.1 KB
/
hci.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
package noblechild
import (
"bufio"
"encoding/hex"
"fmt"
"io"
"os/exec"
"regexp"
"strconv"
"strings"
"syscall"
"time"
log "github.com/Sirupsen/logrus"
"github.com/paypal/gatt"
)
var (
adapterRegex = regexp.MustCompile("^adapterState (.*)$")
eventRegex = regexp.MustCompile("^event (.*)$")
)
// HCI_BLE is a struct to use noble's hci-ble binary.
type HCI_BLE struct {
path string
stdinPipe io.WriteCloser
stdoutPipe io.ReadCloser
command *exec.Cmd
device *device
previousAdapterState string
currentAdapterState string
discoveries map[string]HCIEvent
}
// HCIEvent represents some events from hci.
type HCIEvent struct {
Address string
AddressType string
EIR string
Advertisement *gatt.Advertisement // JSON
RSSI int
Count int
}
func NewHCI(d *device, path string) (*HCI_BLE, error) {
di := make(map[string]HCIEvent)
hci := HCI_BLE{
path: path,
device: d,
discoveries: di,
}
return &hci, nil
}
func (hci *HCI_BLE) Init() error {
cmd := exec.Command(hci.path)
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
hci.stdinPipe = stdin
hci.stdoutPipe = stdout
hci.command = cmd
go hci.Out()
cmd.Start()
go cmd.Wait()
return nil
}
func (hci *HCI_BLE) Close() error {
err := hci.StopScan()
if err != nil {
return fmt.Errorf("hci close: stop scan failed:%s", err)
}
err = hci.command.Process.Signal(syscall.SIGINT)
if err != nil {
return fmt.Errorf("hci close: stop hci failed:%s", err)
}
return nil
}
// Out read stdout from hci
func (hci *HCI_BLE) Out() {
scanner := bufio.NewScanner(hci.stdoutPipe)
for scanner.Scan() {
buf := scanner.Text()
hci.ParseStdout(buf)
}
}
func (hci *HCI_BLE) StartScan() error {
log.Debugf("noblechild: start scan pid: %d", hci.command.Process.Pid)
time.Sleep(3 * time.Second)
return hci.command.Process.Signal(syscall.SIGUSR1)
}
func (hci *HCI_BLE) StartScanFilter() error {
return hci.command.Process.Signal(syscall.SIGUSR2)
}
func (hci *HCI_BLE) StopScan() error {
return hci.command.Process.Signal(syscall.SIGHUP)
}
func (hci *HCI_BLE) ParseStdout(buf string) {
switch {
case adapterRegex.MatchString(buf):
tmp := adapterRegex.FindStringSubmatch(buf)
if len(tmp) != 2 {
log.Println("invalid adapter state line: %s", buf)
return
}
adapterState := tmp[1]
if adapterState == "unauthorized" {
}
if adapterState == "unsupported" {
}
var state gatt.State
switch adapterState {
case "unknown":
state = gatt.StateUnknown
case "unsupported":
log.Error("noble warning: adapter does not support Bluetooth Low Energy (BLE, Bluetooth Smart).")
state = gatt.StateUnsupported
case "unauthorized":
log.Error("warning: adapter state unauthorized, please run as root or with sudo")
state = gatt.StateUnauthorized
case "poweredOff":
state = gatt.StatePoweredOff
case "poweredOn":
state = gatt.StatePoweredOn
}
hci.device.stateChanged(hci.device, state)
case eventRegex.MatchString(buf):
tmp := eventRegex.FindStringSubmatch(buf)
if len(tmp) != 2 {
log.Errorf("invalid event line: %s", buf)
return
}
event := tmp[1]
e, err := parseEvent(event)
if err != nil {
log.Errorf("parse event failed: %s", err)
return
}
prevE, ok := hci.discoveries[e.Address]
if ok {
e.Count = prevE.Count + 1
}
hci.discoveries[e.Address] = e
// only report after an even number of events, so more advertisement data can be collected
if e.Count%2 == 0 {
noble, err := FindNobleModule()
if err != nil {
log.Errorf("could not find l2cap at ParseStdout: %s", err)
return
}
l2cap, err := NewL2CAP(hci.device, noble.L2CAPPath)
if err != nil {
log.Errorf("could not new l2cap at ParseStdout: %s", err)
return
}
p := NewPeripheral(hci.device, l2cap, e.Address)
hci.device.peripheralDiscovered(&p, e.Advertisement, e.RSSI)
}
default:
log.Errorf("unknown output: %s", buf)
}
}
func parseEvent(event string) (HCIEvent, error) {
ret := HCIEvent{}
splitEvent := strings.Split(event, ",")
if len(splitEvent) != 4 {
return ret, fmt.Errorf("invalid event buf len: %s", event)
}
tmp := strings.ToLower(splitEvent[0])
ret.Address = strings.Replace(tmp, ":", "", -1)
ret.AddressType = splitEvent[1]
eir, err := hex.DecodeString(splitEvent[2])
if err != nil {
return ret, fmt.Errorf("invalid event eir: %s, %s", err, event)
}
adv, err := parseEIR(eir)
if err != nil {
return ret, fmt.Errorf("parse EIR failed: %s, %s", err, event)
}
ret.Advertisement = &adv
rssi, err := strconv.Atoi(splitEvent[3])
if err != nil {
return ret, fmt.Errorf("invalid event rssi: %s, %s", err, event)
}
ret.RSSI = rssi
return ret, nil
}
func parseEIR(eir []byte) (gatt.Advertisement, error) {
ret := gatt.Advertisement{}
i := 0
for {
if i+1 > len(eir) {
break
}
length := int(eir[i])
t := eir[i+1]
data := eir[i+2 : i+2+length-1]
switch t {
case 0x02: // Incomplete List of 16-bit Service Class UUID
fallthrough
case 0x03: // Complete List of 16-bit Service Class UUIDs
// TODO
/*
for (j = 0; j < bytes.length; j += 2) {
serviceUuid = bytes.readUInt16LE(j).toString(16);
if (advertisement.serviceUuids.indexOf(serviceUuid) === -1) {
advertisement.serviceUuids.push(serviceUuid);
}
}*/
/*
for j := 0; j < len(data); j += 2 {
tmp := binary.LittleEndian.Uint32([]byte{data[j]})
fmt.Printf("%v", data)
fmt.Printf("%02x", data[j:j+1])
hex := fmt.Sprintf("%02x", data[j:j+1])
uuid, err := gatt.ParseUUID(hex)
if err != nil {
return ret, err
}
if !stringInUUID(uuid, ret.Services) {
ret.Services = append(ret.Services, uuid)
}
}
*/
case 0x06: // Incomplete List of 128-bit Service Class UUIDs
fallthrough
case 0x07: // Complete List of 128-bit Service Class UUIDs
for j := 0; j < len(data); j += 16 {
var hex []string
buf := data[j : j+16]
// hex should be reverse
for i := len(buf) - 1; i >= 0; i-- {
hex = append(hex, fmt.Sprintf("%02x", buf[i]))
}
uuid, err := gatt.ParseUUID(strings.Join(hex, ""))
if err != nil {
return ret, err
}
if !IncludesUUID(uuid, ret.Services) {
ret.Services = append(ret.Services, uuid)
}
}
case 0x08: // Shortened Local Name
fallthrough
case 0x09: // Complete Local Name»
ret.LocalName = string(data)
case 0x0a: // Tx Power Level
ret.TxPowerLevel = int(data[0])
case 0x16: // Service Data, there can be multiple occurences
// TODO
/*
var serviceDataUuid = bytes.slice(0, 2).toString('hex').match(/.{1,2}/g).reverse().join('');
var serviceData = bytes.slice(2, bytes.length);
advertisement.serviceData.push({
uuid: serviceDataUuid,
data: serviceData
});
*/
case 0xff: // Manufacturer Specific Data
ret.ManufacturerData = data
break
}
i = i + length + 1
}
return ret, nil
}
type ByteSlice []byte
func (s ByteSlice) Len() int { return len(s) }
func (s ByteSlice) Less(i, j int) bool { return s[i] < s[j] }
func (s ByteSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }