forked from paypal/gatt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peripheral_darwin.go
277 lines (247 loc) · 7.57 KB
/
peripheral_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
package gatt
import (
"errors"
"log"
"github.com/paypal/gatt/xpc"
)
type peripheral struct {
// NameChanged is called whenever the peripheral GAP Device name has changed.
NameChanged func(Peripheral)
// ServicesModified 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
id xpc.UUID
name string
reqc chan message
rspc chan message
quitc chan struct{}
}
func NewPeripheral(u UUID) Peripheral { return &peripheral{id: xpc.UUID(u.b)} }
func (p *peripheral) Device() Device { return p.d }
func (p *peripheral) ID() string { return p.id.String() }
func (p *peripheral) Name() string { return p.name }
func (p *peripheral) Services() []*Service { return p.svcs }
func (p *peripheral) DiscoverServices(ss []UUID) ([]*Service, error) {
rsp := p.sendReq(45, xpc.Dict{
"kCBMsgArgDeviceUUID": p.id,
"kCBMsgArgUUIDs": uuidSlice(ss),
})
if res := rsp.MustGetInt("kCBMsgArgResult"); res != 0 {
return nil, attEcode(res)
}
svcs := []*Service{}
for _, xss := range rsp["kCBMsgArgServices"].(xpc.Array) {
xs := xss.(xpc.Dict)
u := MustParseUUID(xs.MustGetHexBytes("kCBMsgArgUUID"))
h := uint16(xs.MustGetInt("kCBMsgArgServiceStartHandle"))
endh := uint16(xs.MustGetInt("kCBMsgArgServiceEndHandle"))
svcs = append(svcs, &Service{uuid: u, h: h, endh: endh})
}
p.svcs = svcs
return svcs, nil
}
func (p *peripheral) DiscoverIncludedServices(ss []UUID, s *Service) ([]*Service, error) {
rsp := p.sendReq(60, xpc.Dict{
"kCBMsgArgDeviceUUID": p.id,
"kCBMsgArgServiceStartHandle": s.h,
"kCBMsgArgServiceEndHandle": s.endh,
"kCBMsgArgUUIDs": uuidSlice(ss),
})
if res := rsp.MustGetInt("kCBMsgArgResult"); res != 0 {
return nil, attEcode(res)
}
// TODO
return nil, notImplemented
}
func (p *peripheral) DiscoverCharacteristics(cs []UUID, s *Service) ([]*Characteristic, error) {
rsp := p.sendReq(62, xpc.Dict{
"kCBMsgArgDeviceUUID": p.id,
"kCBMsgArgServiceStartHandle": s.h,
"kCBMsgArgServiceEndHandle": s.endh,
"kCBMsgArgUUIDs": uuidSlice(cs),
})
if res := rsp.MustGetInt("kCBMsgArgResult"); res != 0 {
return nil, attEcode(res)
}
for _, xcs := range rsp.MustGetArray("kCBMsgArgCharacteristics") {
xc := xcs.(xpc.Dict)
u := MustParseUUID(xc.MustGetHexBytes("kCBMsgArgUUID"))
ch := uint16(xc.MustGetInt("kCBMsgArgCharacteristicHandle"))
vh := uint16(xc.MustGetInt("kCBMsgArgCharacteristicValueHandle"))
props := Property(xc.MustGetInt("kCBMsgArgCharacteristicProperties"))
c := &Characteristic{uuid: u, svc: s, props: props, h: ch, vh: vh}
s.chars = append(s.chars, c)
}
return s.chars, nil
}
func (p *peripheral) DiscoverDescriptors(ds []UUID, c *Characteristic) ([]*Descriptor, error) {
rsp := p.sendReq(70, xpc.Dict{
"kCBMsgArgDeviceUUID": p.id,
"kCBMsgArgCharacteristicHandle": c.h,
"kCBMsgArgCharacteristicValueHandle": c.vh,
"kCBMsgArgUUIDs": uuidSlice(ds),
})
for _, xds := range rsp.MustGetArray("kCBMsgArgDescriptors") {
xd := xds.(xpc.Dict)
u := MustParseUUID(xd.MustGetHexBytes("kCBMsgArgUUID"))
h := uint16(xd.MustGetInt("kCBMsgArgDescriptorHandle"))
d := &Descriptor{uuid: u, char: c, h: h}
c.descs = append(c.descs, d)
}
return c.descs, nil
}
func (p *peripheral) ReadCharacteristic(c *Characteristic) ([]byte, error) {
rsp := p.sendReq(65, xpc.Dict{
"kCBMsgArgDeviceUUID": p.id,
"kCBMsgArgCharacteristicHandle": c.h,
"kCBMsgArgCharacteristicValueHandle": c.vh,
})
if res := rsp.MustGetInt("kCBMsgArgResult"); res != 0 {
return nil, attEcode(res)
}
b := rsp.MustGetBytes("kCBMsgArgData")
return b, nil
}
func (p *peripheral) ReadLongCharacteristic(c *Characteristic) ([]byte, error) {
return nil, errors.New("Not implemented")
}
func (p *peripheral) WriteCharacteristic(c *Characteristic, b []byte, noRsp bool) error {
args := xpc.Dict{
"kCBMsgArgDeviceUUID": p.id,
"kCBMsgArgCharacteristicHandle": c.h,
"kCBMsgArgCharacteristicValueHandle": c.vh,
"kCBMsgArgData": b,
"kCBMsgArgType": map[bool]int{false: 0, true: 1}[noRsp],
}
if noRsp {
p.sendCmd(66, args)
return nil
}
rsp := p.sendReq(65, args)
if res := rsp.MustGetInt("kCBMsgArgResult"); res != 0 {
return attEcode(res)
}
return nil
}
func (p *peripheral) ReadDescriptor(d *Descriptor) ([]byte, error) {
rsp := p.sendReq(77, xpc.Dict{
"kCBMsgArgDeviceUUID": p.id,
"kCBMsgArgDescriptorHandle": d.h,
})
if res := rsp.MustGetInt("kCBMsgArgResult"); res != 0 {
return nil, attEcode(res)
}
b := rsp.MustGetBytes("kCBMsgArgData")
return b, nil
}
func (p *peripheral) WriteDescriptor(d *Descriptor, b []byte) error {
rsp := p.sendReq(78, xpc.Dict{
"kCBMsgArgDeviceUUID": p.id,
"kCBMsgArgDescriptorHandle": d.h,
"kCBMsgArgData": b,
})
if res := rsp.MustGetInt("kCBMsgArgResult"); res != 0 {
return attEcode(res)
}
return nil
}
func (p *peripheral) SetNotifyValue(c *Characteristic, f func(*Characteristic, []byte, error)) error {
set := 1
if f == nil {
set = 0
}
// To avoid race condition, registeration is handled before requesting the server.
if f != nil {
// Note: when notified, core bluetooth reports characteristic handle, not value's handle.
p.sub.subscribe(c.h, func(b []byte, err error) { f(c, b, err) })
}
rsp := p.sendReq(68, xpc.Dict{
"kCBMsgArgDeviceUUID": p.id,
"kCBMsgArgCharacteristicHandle": c.h,
"kCBMsgArgCharacteristicValueHandle": c.vh,
"kCBMsgArgState": set,
})
if res := rsp.MustGetInt("kCBMsgArgResult"); res != 0 {
return attEcode(res)
}
// To avoid race condition, unregisteration is handled after server responses.
if f == nil {
p.sub.unsubscribe(c.h)
}
return nil
}
func (p *peripheral) SetIndicateValue(c *Characteristic,
f func(*Characteristic, []byte, error)) error {
// TODO: Implement set indications logic for darwin (https://github.com/paypal/gatt/issues/32)
return nil
}
func (p *peripheral) ReadRSSI() int {
rsp := p.sendReq(43, xpc.Dict{"kCBMsgArgDeviceUUID": p.id})
return rsp.MustGetInt("kCBMsgArgData")
}
func (p *peripheral) SetMTU(mtu uint16) error {
return errors.New("Not implemented")
}
func uuidSlice(uu []UUID) [][]byte {
us := [][]byte{}
for _, u := range uu {
us = append(us, reverse(u.b))
}
return us
}
type message struct {
id int
args xpc.Dict
rspc chan xpc.Dict
}
func (p *peripheral) sendCmd(id int, args xpc.Dict) {
p.reqc <- message{id: id, args: args}
}
func (p *peripheral) sendReq(id int, args xpc.Dict) xpc.Dict {
m := message{id: id, args: args, rspc: make(chan xpc.Dict)}
p.reqc <- m
return <-m.rspc
}
func (p *peripheral) loop() {
rspc := make(chan message)
go func() {
for {
select {
case req := <-p.reqc:
p.d.sendCBMsg(req.id, req.args)
if req.rspc == nil {
break
}
m := <-rspc
req.rspc <- m.args
case <-p.quitc:
return
}
}
}()
for {
select {
case rsp := <-p.rspc:
// Notification
if rsp.id == 71 && rsp.args.GetInt("kCBMsgArgIsNotification", 0) != 0 {
// While we're notified with the value's handle, blued reports the characteristic handle.
ch := uint16(rsp.args.MustGetInt("kCBMsgArgCharacteristicHandle"))
b := rsp.args.MustGetBytes("kCBMsgArgData")
f := p.sub.fn(ch)
if f == nil {
log.Printf("notified by unsubscribed handle")
// FIXME: should terminate the connection?
} else {
go f(b, nil)
}
break
}
rspc <- rsp
case <-p.quitc:
return
}
}
}