forked from paypal/gatt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device_darwin.go
505 lines (442 loc) · 11.6 KB
/
device_darwin.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
package gatt
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"log"
"sync"
"time"
"github.com/paypal/gatt/xpc"
)
const (
peripheralDiscovered = 37
peripheralConnected = 38
peripheralDisconnected = 40
// below constants for Yosemite
rssiRead = 55
includedServicesDiscovered = 63
serviceDiscovered = 56
characteristicsDiscovered = 64
characteristicRead = 71
characteristicWritten = 72
notificationValueSet = 74
descriptorsDiscovered = 76
descriptorRead = 79
descriptorWritten = 80
)
type device struct {
deviceHandler
conn xpc.XPC
role int // 1: peripheralManager (server), 0: centralManager (client)
reqc chan message
rspc chan message
// Only used in client/centralManager implementation
plist map[string]*peripheral
plistmu *sync.Mutex
// Only used in server/peripheralManager implementation
attrN int
attrs map[int]*attr
subscribers map[string]*central
}
func NewDevice(opts ...Option) (Device, error) {
d := &device{
reqc: make(chan message),
rspc: make(chan message),
plist: map[string]*peripheral{},
plistmu: &sync.Mutex{},
attrN: 1,
attrs: make(map[int]*attr),
subscribers: make(map[string]*central),
}
d.Option(opts...)
d.conn = xpc.XpcConnect("com.apple.blued", d)
return d, nil
}
func (d *device) Init(f func(Device, State)) error {
go d.loop()
rsp := d.sendReq(1, xpc.Dict{
"kCBMsgArgName": fmt.Sprintf("gopher-%v", time.Now().Unix()),
"kCBMsgArgOptions": xpc.Dict{"kCBInitOptionShowPowerAlert": 1},
"kCBMsgArgType": d.role,
})
d.stateChanged = f
go d.stateChanged(d, State(rsp.MustGetInt("kCBMsgArgState")))
return nil
}
func (d *device) Advertise(a *AdvPacket) error {
rsp := d.sendReq(8, xpc.Dict{
"kCBAdvDataAppleMfgData": a.b, // not a.Bytes(). should be slice
})
if res := rsp.MustGetInt("kCBMsgArgResult"); res != 0 {
return errors.New("FIXME: Advertise error")
}
return nil
}
func (d *device) AdvertiseNameAndServices(name string, ss []UUID) error {
us := uuidSlice(ss)
rsp := d.sendReq(8, xpc.Dict{
"kCBAdvDataLocalName": name,
"kCBAdvDataServiceUUIDs": us},
)
if res := rsp.MustGetInt("kCBMsgArgResult"); res != 0 {
return errors.New("FIXME: Advertise error")
}
return nil
}
func (d *device) AdvertiseIBeaconData(data []byte) error {
var utsname xpc.Utsname
xpc.Uname(&utsname)
var rsp xpc.Dict
if utsname.Release >= "14." {
l := len(data)
buf := bytes.NewBuffer([]byte{byte(l + 5), 0xFF, 0x4C, 0x00, 0x02, byte(l)})
buf.Write(data)
rsp = d.sendReq(8, xpc.Dict{"kCBAdvDataAppleMfgData": buf.Bytes()})
} else {
rsp = d.sendReq(8, xpc.Dict{"kCBAdvDataAppleBeaconKey": data})
}
if res := rsp.MustGetInt("kCBMsgArgResult"); res != 0 {
return errors.New("FIXME: Advertise error")
}
return nil
}
func (d *device) AdvertiseIBeacon(u UUID, major, minor uint16, pwr int8) error {
b := make([]byte, 21)
copy(b, reverse(u.b)) // Big endian
binary.BigEndian.PutUint16(b[16:], major) // Big endian
binary.BigEndian.PutUint16(b[18:], minor) // Big endian
b[20] = uint8(pwr) // Measured Tx Power
return d.AdvertiseIBeaconData(b)
}
func (d *device) StopAdvertising() error {
rsp := d.sendReq(9, nil)
if res := rsp.MustGetInt("kCBMsgArgResult"); res != 0 {
return errors.New("FIXME: Stop Advertise error")
}
return nil
}
func (d *device) RemoveAllServices() error {
d.sendCmd(12, nil)
return nil
}
func (d *device) AddService(s *Service) error {
if s.uuid.Equal(attrGAPUUID) || s.uuid.Equal(attrGATTUUID) {
// skip GATT and GAP services
return nil
}
xs := xpc.Dict{
"kCBMsgArgAttributeID": d.attrN,
"kCBMsgArgAttributeIDs": []int{},
"kCBMsgArgCharacteristics": nil,
"kCBMsgArgType": 1, // 1 => primary, 0 => excluded
"kCBMsgArgUUID": reverse(s.uuid.b),
}
d.attrN++
xcs := xpc.Array{}
for _, c := range s.Characteristics() {
props := 0
perm := 0
if c.props&CharRead != 0 {
props |= 0x02
if CharRead&c.secure != 0 {
perm |= 0x04
} else {
perm |= 0x01
}
}
if c.props&CharWriteNR != 0 {
props |= 0x04
if c.secure&CharWriteNR != 0 {
perm |= 0x08
} else {
perm |= 0x02
}
}
if c.props&CharWrite != 0 {
props |= 0x08
if c.secure&CharWrite != 0 {
perm |= 0x08
} else {
perm |= 0x02
}
}
if c.props&CharNotify != 0 {
if c.secure&CharNotify != 0 {
props |= 0x100
} else {
props |= 0x10
}
}
if c.props&CharIndicate != 0 {
if c.secure&CharIndicate != 0 {
props |= 0x200
} else {
props |= 0x20
}
}
xc := xpc.Dict{
"kCBMsgArgAttributeID": d.attrN,
"kCBMsgArgUUID": reverse(c.uuid.b),
"kCBMsgArgAttributePermissions": perm,
"kCBMsgArgCharacteristicProperties": props,
"kCBMsgArgData": c.value,
}
d.attrs[d.attrN] = &attr{h: uint16(d.attrN), value: c.value, pvt: c}
d.attrN++
xds := xpc.Array{}
for _, d := range c.Descriptors() {
if d.uuid.Equal(attrClientCharacteristicConfigUUID) {
// skip CCCD
continue
}
xd := xpc.Dict{
"kCBMsgArgData": d.value,
"kCBMsgArgUUID": reverse(d.uuid.b),
}
xds = append(xds, xd)
}
xc["kCBMsgArgDescriptors"] = xds
xcs = append(xcs, xc)
}
xs["kCBMsgArgCharacteristics"] = xcs
rsp := d.sendReq(10, xs)
if res := rsp.MustGetInt("kCBMsgArgResult"); res != 0 {
return errors.New("FIXME: Add Srvice error")
}
return nil
}
func (d *device) SetServices(ss []*Service) error {
d.RemoveAllServices()
for _, s := range ss {
d.AddService(s)
}
return nil
}
func (d *device) Scan(ss []UUID, dup bool) {
args := xpc.Dict{
"kCBMsgArgUUIDs": uuidSlice(ss),
"kCBMsgArgOptions": xpc.Dict{
"kCBScanOptionAllowDuplicates": map[bool]int{true: 1, false: 0}[dup],
},
}
d.sendCmd(29, args)
}
func (d *device) StopScanning() {
d.sendCmd(30, nil)
}
func (d *device) Connect(p Peripheral) {
pp := p.(*peripheral)
d.plist[pp.id.String()] = pp
d.sendCmd(31,
xpc.Dict{
"kCBMsgArgDeviceUUID": pp.id,
"kCBMsgArgOptions": xpc.Dict{
"kCBConnectOptionNotifyOnDisconnection": 1,
},
})
}
func (d *device) respondToRequest(id int, args xpc.Dict) {
switch id {
case 19: // ReadRequest
u := UUID{args.MustGetUUID("kCBMsgArgDeviceUUID")}
t := args.MustGetInt("kCBMsgArgTransactionID")
a := args.MustGetInt("kCBMsgArgAttributeID")
o := args.MustGetInt("kCBMsgArgOffset")
attr := d.attrs[a]
v := attr.value
if v == nil {
c := newCentral(d, u)
req := &ReadRequest{
Request: Request{Central: c},
Cap: int(c.mtu - 1),
Offset: o,
}
rsp := newResponseWriter(int(c.mtu - 1))
if c, ok := attr.pvt.(*Characteristic); ok {
c.rhandler.ServeRead(rsp, req)
v = rsp.bytes()
}
}
d.sendCmd(13, xpc.Dict{
"kCBMsgArgAttributeID": a,
"kCBMsgArgData": v,
"kCBMsgArgTransactionID": t,
"kCBMsgArgResult": 0,
})
case 20: // WriteRequest
u := UUID{args.MustGetUUID("kCBMsgArgDeviceUUID")}
t := args.MustGetInt("kCBMsgArgTransactionID")
a := 0
noRsp := false
xxws := args.MustGetArray("kCBMsgArgATTWrites")
for _, xxw := range xxws {
xw := xxw.(xpc.Dict)
if a == 0 {
a = xw.MustGetInt("kCBMsgArgAttributeID")
}
o := xw.MustGetInt("kCBMsgArgOffset")
i := xw.MustGetInt("kCBMsgArgIgnoreResponse")
b := xw.MustGetBytes("kCBMsgArgData")
_ = o
attr := d.attrs[a]
c := newCentral(d, u)
r := Request{Central: c}
attr.pvt.(*Characteristic).whandler.ServeWrite(r, b)
if i == 1 {
noRsp = true
}
}
if noRsp {
break
}
d.sendCmd(13, xpc.Dict{
"kCBMsgArgAttributeID": a,
"kCBMsgArgData": nil,
"kCBMsgArgTransactionID": t,
"kCBMsgArgResult": 0,
})
case 21: // subscribed
u := UUID{args.MustGetUUID("kCBMsgArgDeviceUUID")}
a := args.MustGetInt("kCBMsgArgAttributeID")
attr := d.attrs[a]
c := newCentral(d, u)
d.subscribers[u.String()] = c
c.startNotify(attr, c.mtu)
case 22: // unubscribed
u := UUID{args.MustGetUUID("kCBMsgArgDeviceUUID")}
a := args.MustGetInt("kCBMsgArgAttributeID")
attr := d.attrs[a]
if c := d.subscribers[u.String()]; c != nil {
c.stopNotify(attr)
}
case 23: // notificationSent
}
}
func (d *device) CancelConnection(p Peripheral) {
d.sendCmd(32, xpc.Dict{"kCBMsgArgDeviceUUID": p.(*peripheral).id})
}
// process device events and asynchronous errors
// (implements XpcEventHandler)
func (d *device) HandleXpcEvent(event xpc.Dict, err error) {
if err != nil {
log.Println("error:", err)
return
}
id := event.MustGetInt("kCBMsgId")
args := event.MustGetDict("kCBMsgArgs")
//log.Printf(">> %d, %v", id, args)
switch id {
case // device event
6, // StateChanged
16, // AdvertisingStarted
17, // AdvertisingStopped
18: // ServiceAdded
d.rspc <- message{id: id, args: args}
case
19, // ReadRequest
20, // WriteRequest
21, // Subscribe
22, // Unubscribe
23: // Confirmation
d.respondToRequest(id, args)
case peripheralDiscovered:
xa := args.MustGetDict("kCBMsgArgAdvertisementData")
if len(xa) == 0 {
return
}
u := UUID{args.MustGetUUID("kCBMsgArgDeviceUUID")}
a := &Advertisement{
LocalName: xa.GetString("kCBAdvDataLocalName", args.GetString("kCBMsgArgName", "")),
TxPowerLevel: xa.GetInt("kCBAdvDataTxPowerLevel", 0),
ManufacturerData: xa.GetBytes("kCBAdvDataManufacturerData", nil),
}
rssi := args.MustGetInt("kCBMsgArgRssi")
if xu, ok := xa["kCBAdvDataServiceUUIDs"]; ok {
for _, xs := range xu.(xpc.Array) {
s := UUID{reverse(xs.([]byte))}
a.Services = append(a.Services, s)
}
}
if xsds, ok := xa["kCBAdvDataServiceData"]; ok {
xsd := xsds.(xpc.Array)
for i := 0; i < len(xsd); i += 2 {
sd := ServiceData{
UUID: UUID{xsd[i].([]byte)},
Data: xsd[i+1].([]byte),
}
a.ServiceData = append(a.ServiceData, sd)
}
}
if d.peripheralDiscovered != nil {
go d.peripheralDiscovered(&peripheral{id: xpc.UUID(u.b), d: d}, a, rssi)
}
case peripheralConnected:
u := UUID{args.MustGetUUID("kCBMsgArgDeviceUUID")}
p := &peripheral{
id: xpc.UUID(u.b),
d: d,
reqc: make(chan message),
rspc: make(chan message),
quitc: make(chan struct{}),
sub: newSubscriber(),
}
d.plistmu.Lock()
d.plist[u.String()] = p
d.plistmu.Unlock()
go p.loop()
if d.peripheralConnected != nil {
go d.peripheralConnected(p, nil)
}
case peripheralDisconnected:
u := UUID{args.MustGetUUID("kCBMsgArgDeviceUUID")}
d.plistmu.Lock()
p := d.plist[u.String()]
delete(d.plist, u.String())
d.plistmu.Unlock()
if d.peripheralDisconnected != nil {
d.peripheralDisconnected(p, nil) // TODO: Get Result as error?
}
close(p.quitc)
case // Peripheral events
rssiRead,
serviceDiscovered,
includedServicesDiscovered,
characteristicsDiscovered,
characteristicRead,
characteristicWritten,
notificationValueSet,
descriptorsDiscovered,
descriptorRead,
descriptorWritten:
u := UUID{args.MustGetUUID("kCBMsgArgDeviceUUID")}
d.plistmu.Lock()
p := d.plist[u.String()]
d.plistmu.Unlock()
p.rspc <- message{id: id, args: args}
default:
log.Printf("Unhandled event: %#v", event)
}
}
func (d *device) sendReq(id int, args xpc.Dict) xpc.Dict {
m := message{id: id, args: args, rspc: make(chan xpc.Dict)}
d.reqc <- m
return <-m.rspc
}
func (d *device) sendCmd(id int, args xpc.Dict) {
d.reqc <- message{id: id, args: args}
}
func (d *device) loop() {
for req := range d.reqc {
d.sendCBMsg(req.id, req.args)
if req.rspc == nil {
continue
}
m := <-d.rspc
req.rspc <- m.args
}
}
func (d *device) sendCBMsg(id int, args xpc.Dict) {
// log.Printf("<< %d, %v", id, args)
d.conn.Send(xpc.Dict{"kCBMsgId": id, "kCBMsgArgs": args}, false)
}