| 1 |
package gatt |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"log" |
| 6 |
|
| 7 |
"github.com/paypal/gatt/xpc" |
| 8 |
) |
| 9 |
|
| 10 |
type peripheral struct { |
| 11 |
|
| 12 |
NameChanged func(Peripheral) |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
ServicesModified func(Peripheral, []*Service) |
| 17 |
|
| 18 |
d *device |
| 19 |
svcs []*Service |
| 20 |
|
| 21 |
sub *subscriber |
| 22 |
|
| 23 |
id xpc.UUID |
| 24 |
name string |
| 25 |
|
| 26 |
reqc chan message |
| 27 |
rspc chan message |
| 28 |
quitc chan struct{} |
| 29 |
} |
| 30 |
|
| 31 |
func NewPeripheral(u UUID) Peripheral { return &peripheral{id: xpc.UUID(u.b)} } |
| 32 |
|
| 33 |
func (p *peripheral) Device() Device { return p.d } |
| 34 |
func (p *peripheral) ID() string { return p.id.String() } |
| 35 |
func (p *peripheral) Name() string { return p.name } |
| 36 |
func (p *peripheral) Services() []*Service { return p.svcs } |
| 37 |
|
| 38 |
func (p *peripheral) DiscoverServices(ss []UUID) ([]*Service, error) { |
| 39 |
rsp := p.sendReq(45, xpc.Dict{ |
| 40 |
"kCBMsgArgDeviceUUID": p.id, |
| 41 |
"kCBMsgArgUUIDs": uuidSlice(ss), |
| 42 |
}) |
| 43 |
if res := rsp.MustGetInt("kCBMsgArgResult"); res != 0 { |
| 44 |
return nil, attEcode(res) |
| 45 |
} |
| 46 |
svcs := []*Service{} |
| 47 |
for _, xss := range rsp["kCBMsgArgServices"].(xpc.Array) { |
| 48 |
xs := xss.(xpc.Dict) |
| 49 |
u := MustParseUUID(xs.MustGetHexBytes("kCBMsgArgUUID")) |
| 50 |
h := uint16(xs.MustGetInt("kCBMsgArgServiceStartHandle")) |
| 51 |
endh := uint16(xs.MustGetInt("kCBMsgArgServiceEndHandle")) |
| 52 |
svcs = append(svcs, &Service{uuid: u, h: h, endh: endh}) |
| 53 |
} |
| 54 |
p.svcs = svcs |
| 55 |
return svcs, nil |
| 56 |
} |
| 57 |
|
| 58 |
func (p *peripheral) DiscoverIncludedServices(ss []UUID, s *Service) ([]*Service, error) { |
| 59 |
rsp := p.sendReq(60, xpc.Dict{ |
| 60 |
"kCBMsgArgDeviceUUID": p.id, |
| 61 |
"kCBMsgArgServiceStartHandle": s.h, |
| 62 |
"kCBMsgArgServiceEndHandle": s.endh, |
| 63 |
"kCBMsgArgUUIDs": uuidSlice(ss), |
| 64 |
}) |
| 65 |
if res := rsp.MustGetInt("kCBMsgArgResult"); res != 0 { |
| 66 |
return nil, attEcode(res) |
| 67 |
} |
| 68 |
|
| 69 |
return nil, notImplemented |
| 70 |
} |
| 71 |
|
| 72 |
func (p *peripheral) DiscoverCharacteristics(cs []UUID, s *Service) ([]*Characteristic, error) { |
| 73 |
rsp := p.sendReq(62, xpc.Dict{ |
| 74 |
"kCBMsgArgDeviceUUID": p.id, |
| 75 |
"kCBMsgArgServiceStartHandle": s.h, |
| 76 |
"kCBMsgArgServiceEndHandle": s.endh, |
| 77 |
"kCBMsgArgUUIDs": uuidSlice(cs), |
| 78 |
}) |
| 79 |
if res := rsp.MustGetInt("kCBMsgArgResult"); res != 0 { |
| 80 |
return nil, attEcode(res) |
| 81 |
} |
| 82 |
for _, xcs := range rsp.MustGetArray("kCBMsgArgCharacteristics") { |
| 83 |
xc := xcs.(xpc.Dict) |
| 84 |
u := MustParseUUID(xc.MustGetHexBytes("kCBMsgArgUUID")) |
| 85 |
ch := uint16(xc.MustGetInt("kCBMsgArgCharacteristicHandle")) |
| 86 |
vh := uint16(xc.MustGetInt("kCBMsgArgCharacteristicValueHandle")) |
| 87 |
props := Property(xc.MustGetInt("kCBMsgArgCharacteristicProperties")) |
| 88 |
c := &Characteristic{uuid: u, svc: s, props: props, h: ch, vh: vh} |
| 89 |
s.chars = append(s.chars, c) |
| 90 |
} |
| 91 |
return s.chars, nil |
| 92 |
} |
| 93 |
|
| 94 |
func (p *peripheral) DiscoverDescriptors(ds []UUID, c *Characteristic) ([]*Descriptor, error) { |
| 95 |
rsp := p.sendReq(70, xpc.Dict{ |
| 96 |
"kCBMsgArgDeviceUUID": p.id, |
| 97 |
"kCBMsgArgCharacteristicHandle": c.h, |
| 98 |
"kCBMsgArgCharacteristicValueHandle": c.vh, |
| 99 |
"kCBMsgArgUUIDs": uuidSlice(ds), |
| 100 |
}) |
| 101 |
for _, xds := range rsp.MustGetArray("kCBMsgArgDescriptors") { |
| 102 |
xd := xds.(xpc.Dict) |
| 103 |
u := MustParseUUID(xd.MustGetHexBytes("kCBMsgArgUUID")) |
| 104 |
h := uint16(xd.MustGetInt("kCBMsgArgDescriptorHandle")) |
| 105 |
d := &Descriptor{uuid: u, char: c, h: h} |
| 106 |
c.descs = append(c.descs, d) |
| 107 |
} |
| 108 |
return c.descs, nil |
| 109 |
} |
| 110 |
|
| 111 |
func (p *peripheral) ReadCharacteristic(c *Characteristic) ([]byte, error) { |
| 112 |
rsp := p.sendReq(65, xpc.Dict{ |
| 113 |
"kCBMsgArgDeviceUUID": p.id, |
| 114 |
"kCBMsgArgCharacteristicHandle": c.h, |
| 115 |
"kCBMsgArgCharacteristicValueHandle": c.vh, |
| 116 |
}) |
| 117 |
if res := rsp.MustGetInt("kCBMsgArgResult"); res != 0 { |
| 118 |
return nil, attEcode(res) |
| 119 |
} |
| 120 |
b := rsp.MustGetBytes("kCBMsgArgData") |
| 121 |
return b, nil |
| 122 |
} |
| 123 |
|
| 124 |
func (p *peripheral) ReadLongCharacteristic(c *Characteristic) ([]byte, error) { |
| 125 |
return nil, errors.New("Not implemented") |
| 126 |
} |
| 127 |
|
| 128 |
func (p *peripheral) WriteCharacteristic(c *Characteristic, b []byte, noRsp bool) error { |
| 129 |
args := xpc.Dict{ |
| 130 |
"kCBMsgArgDeviceUUID": p.id, |
| 131 |
"kCBMsgArgCharacteristicHandle": c.h, |
| 132 |
"kCBMsgArgCharacteristicValueHandle": c.vh, |
| 133 |
"kCBMsgArgData": b, |
| 134 |
"kCBMsgArgType": map[bool]int{false: 0, true: 1}[noRsp], |
| 135 |
} |
| 136 |
if noRsp { |
| 137 |
p.sendCmd(66, args) |
| 138 |
return nil |
| 139 |
} |
| 140 |
rsp := p.sendReq(65, args) |
| 141 |
if res := rsp.MustGetInt("kCBMsgArgResult"); res != 0 { |
| 142 |
return attEcode(res) |
| 143 |
} |
| 144 |
return nil |
| 145 |
} |
| 146 |
|
| 147 |
func (p *peripheral) ReadDescriptor(d *Descriptor) ([]byte, error) { |
| 148 |
rsp := p.sendReq(77, xpc.Dict{ |
| 149 |
"kCBMsgArgDeviceUUID": p.id, |
| 150 |
"kCBMsgArgDescriptorHandle": d.h, |
| 151 |
}) |
| 152 |
if res := rsp.MustGetInt("kCBMsgArgResult"); res != 0 { |
| 153 |
return nil, attEcode(res) |
| 154 |
} |
| 155 |
b := rsp.MustGetBytes("kCBMsgArgData") |
| 156 |
return b, nil |
| 157 |
} |
| 158 |
|
| 159 |
func (p *peripheral) WriteDescriptor(d *Descriptor, b []byte) error { |
| 160 |
rsp := p.sendReq(78, xpc.Dict{ |
| 161 |
"kCBMsgArgDeviceUUID": p.id, |
| 162 |
"kCBMsgArgDescriptorHandle": d.h, |
| 163 |
"kCBMsgArgData": b, |
| 164 |
}) |
| 165 |
if res := rsp.MustGetInt("kCBMsgArgResult"); res != 0 { |
| 166 |
return attEcode(res) |
| 167 |
} |
| 168 |
return nil |
| 169 |
} |
| 170 |
|
| 171 |
func (p *peripheral) SetNotifyValue(c *Characteristic, f func(*Characteristic, []byte, error)) error { |
| 172 |
set := 1 |
| 173 |
if f == nil { |
| 174 |
set = 0 |
| 175 |
} |
| 176 |
|
| 177 |
if f != nil { |
| 178 |
|
| 179 |
p.sub.subscribe(c.h, func(b []byte, err error) { f(c, b, err) }) |
| 180 |
} |
| 181 |
rsp := p.sendReq(68, xpc.Dict{ |
| 182 |
"kCBMsgArgDeviceUUID": p.id, |
| 183 |
"kCBMsgArgCharacteristicHandle": c.h, |
| 184 |
"kCBMsgArgCharacteristicValueHandle": c.vh, |
| 185 |
"kCBMsgArgState": set, |
| 186 |
}) |
| 187 |
if res := rsp.MustGetInt("kCBMsgArgResult"); res != 0 { |
| 188 |
return attEcode(res) |
| 189 |
} |
| 190 |
|
| 191 |
if f == nil { |
| 192 |
p.sub.unsubscribe(c.h) |
| 193 |
} |
| 194 |
return nil |
| 195 |
} |
| 196 |
|
| 197 |
func (p *peripheral) SetIndicateValue(c *Characteristic, |
| 198 |
f func(*Characteristic, []byte, error)) error { |
| 199 |
|
| 200 |
return nil |
| 201 |
} |
| 202 |
|
| 203 |
func (p *peripheral) ReadRSSI() int { |
| 204 |
rsp := p.sendReq(43, xpc.Dict{"kCBMsgArgDeviceUUID": p.id}) |
| 205 |
return rsp.MustGetInt("kCBMsgArgData") |
| 206 |
} |
| 207 |
|
| 208 |
func (p *peripheral) SetMTU(mtu uint16) error { |
| 209 |
return errors.New("Not implemented") |
| 210 |
} |
| 211 |
|
| 212 |
func uuidSlice(uu []UUID) [][]byte { |
| 213 |
us := [][]byte{} |
| 214 |
for _, u := range uu { |
| 215 |
us = append(us, reverse(u.b)) |
| 216 |
} |
| 217 |
return us |
| 218 |
} |
| 219 |
|
| 220 |
type message struct { |
| 221 |
id int |
| 222 |
args xpc.Dict |
| 223 |
rspc chan xpc.Dict |
| 224 |
} |
| 225 |
|
| 226 |
func (p *peripheral) sendCmd(id int, args xpc.Dict) { |
| 227 |
p.reqc <- message{id: id, args: args} |
| 228 |
} |
| 229 |
|
| 230 |
func (p *peripheral) sendReq(id int, args xpc.Dict) xpc.Dict { |
| 231 |
m := message{id: id, args: args, rspc: make(chan xpc.Dict)} |
| 232 |
p.reqc <- m |
| 233 |
return <-m.rspc |
| 234 |
} |
| 235 |
|
| 236 |
func (p *peripheral) loop() { |
| 237 |
rspc := make(chan message) |
| 238 |
|
| 239 |
go func() { |
| 240 |
for { |
| 241 |
select { |
| 242 |
case req := <-p.reqc: |
| 243 |
p.d.sendCBMsg(req.id, req.args) |
| 244 |
if req.rspc == nil { |
| 245 |
break |
| 246 |
} |
| 247 |
m := <-rspc |
| 248 |
req.rspc <- m.args |
| 249 |
case <-p.quitc: |
| 250 |
return |
| 251 |
} |
| 252 |
} |
| 253 |
}() |
| 254 |
|
| 255 |
for { |
| 256 |
select { |
| 257 |
case rsp := <-p.rspc: |
| 258 |
|
| 259 |
if rsp.id == 71 && rsp.args.GetInt("kCBMsgArgIsNotification", 0) != 0 { |
| 260 |
|
| 261 |
ch := uint16(rsp.args.MustGetInt("kCBMsgArgCharacteristicHandle")) |
| 262 |
b := rsp.args.MustGetBytes("kCBMsgArgData") |
| 263 |
f := p.sub.fn(ch) |
| 264 |
if f == nil { |
| 265 |
log.Printf("notified by unsubscribed handle") |
| 266 |
|
| 267 |
} else { |
| 268 |
go f(b, nil) |
| 269 |
} |
| 270 |
break |
| 271 |
} |
| 272 |
rspc <- rsp |
| 273 |
case <-p.quitc: |
| 274 |
return |
| 275 |
} |
| 276 |
} |
| 277 |
} |