forked from paypal/gatt
-
Notifications
You must be signed in to change notification settings - Fork 11
/
peripheral_linux.go
453 lines (395 loc) · 9.64 KB
/
peripheral_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
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
package gatt
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"log"
"net"
"strings"
"github.com/currantlabs/gatt/linux"
)
type peripheral struct {
// NameChanged is called whenever the peripheral GAP device name has changed.
NameChanged func(*peripheral)
// ServicedModified is called when one or more service of a peripheral have changed.
// A list of invalid service is provided in the parameter.
ServicesModified func(*peripheral, []*Service)
d *device
svcs []*Service
sub *subscriber
mtu uint16
l2c io.ReadWriteCloser
reqc chan message
quitc chan struct{}
pd *linux.PlatData // platform specific data
}
func (p *peripheral) Device() Device { return p.d }
func (p *peripheral) ID() string { return strings.ToUpper(net.HardwareAddr(p.pd.Address[:]).String()) }
func (p *peripheral) Name() string { return p.pd.Name }
func (p *peripheral) Services() []*Service { return p.svcs }
func finish(op byte, h uint16, b []byte) bool {
done := b[0] == attOpError && b[1] == op && b[2] == byte(h) && b[3] == byte(h>>8)
e := attEcode(b[4])
if e != attEcodeAttrNotFound {
// log.Printf("unexpected protocol error: %s", e)
// FIXME: terminate the connection
}
return done
}
func (p *peripheral) DiscoverServices(ds []UUID) ([]*Service, error) {
// p.pd.Conn.Write([]byte{0x02, 0x87, 0x00}) // MTU
done := false
start := uint16(0x0001)
for !done {
op := byte(attOpReadByGroupReq)
b := make([]byte, 7)
b[0] = op
binary.LittleEndian.PutUint16(b[1:3], start)
binary.LittleEndian.PutUint16(b[3:5], 0xFFFF)
binary.LittleEndian.PutUint16(b[5:7], 0x2800)
b = p.sendReq(op, b)
if finish(op, start, b) {
break
}
b = b[1:]
l, b := int(b[0]), b[1:]
switch {
case l == 6 && (len(b)%6 == 0):
case l == 20 && (len(b)%20 == 0):
default:
return nil, ErrInvalidLength
}
for len(b) != 0 {
endh := binary.LittleEndian.Uint16(b[2:4])
u := UUID{b[4:l]}
if UUIDContains(ds, u) {
s := &Service{
uuid: u,
h: binary.LittleEndian.Uint16(b[:2]),
endh: endh,
}
p.svcs = append(p.svcs, s)
}
b = b[l:]
done = endh == 0xFFFF
start = endh + 1
}
}
return p.svcs, nil
}
func (p *peripheral) DiscoverIncludedServices(ss []UUID, s *Service) ([]*Service, error) {
// TODO
return nil, nil
}
func (p *peripheral) DiscoverCharacteristics(cs []UUID, s *Service) ([]*Characteristic, error) {
done := false
start := s.h
var prev *Characteristic
for !done {
op := byte(attOpReadByTypeReq)
b := make([]byte, 7)
b[0] = op
binary.LittleEndian.PutUint16(b[1:3], start)
binary.LittleEndian.PutUint16(b[3:5], s.endh)
binary.LittleEndian.PutUint16(b[5:7], 0x2803)
b = p.sendReq(op, b)
if finish(op, start, b) {
break
}
b = b[1:]
l, b := int(b[0]), b[1:]
switch {
case l == 7 && (len(b)%7 == 0):
case l == 21 && (len(b)%21 == 0):
default:
return nil, ErrInvalidLength
}
for len(b) != 0 {
h := binary.LittleEndian.Uint16(b[:2])
props := Property(b[2])
vh := binary.LittleEndian.Uint16(b[3:5])
u := UUID{b[5:l]}
s := searchService(p.svcs, h, vh)
if s == nil {
log.Printf("Can't find service range that contains 0x%04X - 0x%04X", h, vh)
return nil, fmt.Errorf("Can't find service range that contains 0x%04X - 0x%04X", h, vh)
}
c := &Characteristic{
uuid: u,
svc: s,
props: props,
h: h,
vh: vh,
}
if UUIDContains(cs, u) {
s.chars = append(s.chars, c)
}
b = b[l:]
done = vh == s.endh
start = vh + 1
if prev != nil {
prev.endh = c.h - 1
}
prev = c
}
}
if len(s.chars) > 1 {
s.chars[len(s.chars)-1].endh = s.endh
}
return s.chars, nil
}
func (p *peripheral) DiscoverDescriptors(ds []UUID, c *Characteristic) ([]*Descriptor, error) {
done := false
start := c.vh + 1
for !done {
if c.endh == 0 {
c.endh = c.svc.endh
}
op := byte(attOpFindInfoReq)
b := make([]byte, 5)
b[0] = op
binary.LittleEndian.PutUint16(b[1:3], start)
binary.LittleEndian.PutUint16(b[3:5], c.endh)
b = p.sendReq(op, b)
if finish(attOpFindInfoReq, start, b) {
break
}
b = b[1:]
var l int
f, b := int(b[0]), b[1:]
switch {
case f == 1 && (len(b)%4 == 0):
l = 4
case f == 2 && (len(b)%18 == 0):
l = 18
default:
return nil, ErrInvalidLength
}
for len(b) != 0 {
h := binary.LittleEndian.Uint16(b[:2])
u := UUID{b[2:l]}
d := &Descriptor{uuid: u, h: h, char: c}
if UUIDContains(ds, u) {
c.descs = append(c.descs, d)
}
if u.Equal(attrClientCharacteristicConfigUUID) {
c.cccd = d
}
b = b[l:]
done = h == c.endh
start = h + 1
}
}
return c.descs, nil
}
func (p *peripheral) ReadCharacteristic(c *Characteristic) ([]byte, error) {
b := make([]byte, 3)
op := byte(attOpReadReq)
b[0] = op
binary.LittleEndian.PutUint16(b[1:3], c.vh)
b = p.sendReq(op, b)
b = b[1:]
return b, nil
}
func (p *peripheral) ReadLongCharacteristic(c *Characteristic) ([]byte, error) {
// The spec says that a read blob request should fail if the characteristic
// is smaller than mtu - 1. To simplify the API, the first read is done
// with a regular read request. If the buffer received is equal to mtu -1,
// then we read the rest of the data using read blob.
firstRead, err := p.ReadCharacteristic(c)
if err != nil {
return nil, err
}
if len(firstRead) < int(p.mtu)-1 {
return firstRead, nil
}
var buf bytes.Buffer
buf.Write(firstRead)
off := uint16(len(firstRead))
for {
b := make([]byte, 5)
op := byte(attOpReadBlobReq)
b[0] = op
binary.LittleEndian.PutUint16(b[1:3], c.vh)
binary.LittleEndian.PutUint16(b[3:5], off)
b = p.sendReq(op, b)
b = b[1:]
if len(b) == 0 {
break
}
buf.Write(b)
off += uint16(len(b))
if len(b) < int(p.mtu)-1 {
break
}
}
return buf.Bytes(), nil
}
func (p *peripheral) WriteCharacteristic(c *Characteristic, value []byte, noRsp bool) error {
b := make([]byte, 3+len(value))
op := byte(attOpWriteReq)
b[0] = op
if noRsp {
b[0] = attOpWriteCmd
}
binary.LittleEndian.PutUint16(b[1:3], c.vh)
copy(b[3:], value)
if noRsp {
p.sendCmd(op, b)
return nil
}
b = p.sendReq(op, b)
// TODO: error handling
b = b[1:]
return nil
}
func (p *peripheral) ReadDescriptor(d *Descriptor) ([]byte, error) {
b := make([]byte, 3)
op := byte(attOpReadReq)
b[0] = op
binary.LittleEndian.PutUint16(b[1:3], d.h)
b = p.sendReq(op, b)
b = b[1:]
// TODO: error handling
return b, nil
}
func (p *peripheral) WriteDescriptor(d *Descriptor, value []byte) error {
b := make([]byte, 3+len(value))
op := byte(attOpWriteReq)
b[0] = op
binary.LittleEndian.PutUint16(b[1:3], d.h)
copy(b[3:], value)
b = p.sendReq(op, b)
b = b[1:]
// TODO: error handling
return nil
}
func (p *peripheral) setNotifyValue(c *Characteristic, flag uint16,
f func(*Characteristic, []byte, error)) error {
if c.cccd == nil {
return errors.New("no cccd") // FIXME
}
ccc := uint16(0)
if f != nil {
ccc = flag
p.sub.subscribe(c.vh, func(b []byte, err error) { f(c, b, err) })
}
b := make([]byte, 5)
op := byte(attOpWriteReq)
b[0] = op
binary.LittleEndian.PutUint16(b[1:3], c.cccd.h)
binary.LittleEndian.PutUint16(b[3:5], ccc)
b = p.sendReq(op, b)
b = b[1:]
// TODO: error handling
if f == nil {
p.sub.unsubscribe(c.vh)
}
return nil
}
func (p *peripheral) SetNotifyValue(c *Characteristic,
f func(*Characteristic, []byte, error)) error {
return p.setNotifyValue(c, gattCCCNotifyFlag, f)
}
func (p *peripheral) SetIndicateValue(c *Characteristic,
f func(*Characteristic, []byte, error)) error {
return p.setNotifyValue(c, gattCCCIndicateFlag, f)
}
func (p *peripheral) ReadRSSI() int {
// TODO: implement
return -1
}
func searchService(ss []*Service, start, end uint16) *Service {
for _, s := range ss {
if s.h < start && s.endh >= end {
return s
}
}
return nil
}
// TODO: unifiy the message with OS X pots and refactor
type message struct {
op byte
b []byte
rspc chan []byte
}
func (p *peripheral) sendCmd(op byte, b []byte) {
p.reqc <- message{op: op, b: b}
}
func (p *peripheral) sendReq(op byte, b []byte) []byte {
m := message{op: op, b: b, rspc: make(chan []byte)}
p.reqc <- m
return <-m.rspc
}
func (p *peripheral) loop() {
// Serialize the request.
rspc := make(chan []byte)
// Dequeue request loop
go func() {
for {
select {
case req := <-p.reqc:
p.l2c.Write(req.b)
if req.rspc == nil {
break
}
for {
r := <-rspc
reqOp, rspOp := req.b[0], r[0]
if rspOp == attRspFor[reqOp] || (rspOp == attOpError && r[1] == reqOp) {
req.rspc <- r
break
}
log.Printf("Request 0x%02x got a mismatched response: 0x%02x", reqOp, rspOp)
p.l2c.Write(attErrorRsp(rspOp, 0x0000, attEcodeReqNotSupp))
}
case <-p.quitc:
return
}
}
}()
// L2CAP implementations shall support a minimum MTU size of 48 bytes.
// The default value is 672 bytes
buf := make([]byte, 672)
// Handling response or notification/indication
for {
n, err := p.l2c.Read(buf)
if n == 0 || err != nil {
close(p.quitc)
return
}
b := make([]byte, n)
copy(b, buf)
if (b[0] != attOpHandleNotify) && (b[0] != attOpHandleInd) {
rspc <- b
continue
}
h := binary.LittleEndian.Uint16(b[1:3])
f := p.sub.fn(h)
if f == nil {
log.Printf("notified by unsubscribed handle")
// FIXME: terminate the connection?
} else {
go f(b[3:], nil)
}
if b[0] == attOpHandleInd {
// write aknowledgement for indication
p.l2c.Write([]byte{attOpHandleCnf})
}
}
}
func (p *peripheral) SetMTU(mtu uint16) error {
b := make([]byte, 3)
op := byte(attOpMtuReq)
b[0] = op
binary.LittleEndian.PutUint16(b[1:3], uint16(mtu))
b = p.sendReq(op, b)
serverMTU := binary.LittleEndian.Uint16(b[1:3])
if serverMTU < mtu {
mtu = serverMTU
}
p.mtu = mtu
return nil
}