| 1 |
package gatt |
| 2 |
|
| 3 |
import ( |
| 4 |
"sync" |
| 5 |
|
| 6 |
"github.com/paypal/gatt/xpc" |
| 7 |
) |
| 8 |
|
| 9 |
type central struct { |
| 10 |
dev *device |
| 11 |
uuid UUID |
| 12 |
mtu int |
| 13 |
notifiers map[uint16]*notifier |
| 14 |
notifiersmu *sync.Mutex |
| 15 |
} |
| 16 |
|
| 17 |
func newCentral(d *device, u UUID) *central { |
| 18 |
return ¢ral{ |
| 19 |
dev: d, |
| 20 |
mtu: 23, |
| 21 |
uuid: u, |
| 22 |
notifiers: make(map[uint16]*notifier), |
| 23 |
notifiersmu: &sync.Mutex{}, |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
func (c *central) ID() string { return c.uuid.String() } |
| 28 |
func (c *central) Close() error { return nil } |
| 29 |
func (c *central) MTU() int { return c.mtu } |
| 30 |
|
| 31 |
func (c *central) sendNotification(a *attr, b []byte) (int, error) { |
| 32 |
data := make([]byte, len(b)) |
| 33 |
copy(data, b) |
| 34 |
c.dev.sendCmd(15, xpc.Dict{ |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
"kCBMsgArgUUIDs": [][]byte{}, |
| 45 |
"kCBMsgArgAttributeID": a.h, |
| 46 |
"kCBMsgArgData": data, |
| 47 |
}) |
| 48 |
return len(b), nil |
| 49 |
} |
| 50 |
|
| 51 |
func (c *central) startNotify(a *attr, maxlen int) { |
| 52 |
c.notifiersmu.Lock() |
| 53 |
defer c.notifiersmu.Unlock() |
| 54 |
if _, found := c.notifiers[a.h]; found { |
| 55 |
return |
| 56 |
} |
| 57 |
n := newNotifier(c, a, maxlen) |
| 58 |
c.notifiers[a.h] = n |
| 59 |
char := a.pvt.(*Characteristic) |
| 60 |
go char.nhandler.ServeNotify(Request{Central: c}, n) |
| 61 |
} |
| 62 |
|
| 63 |
func (c *central) stopNotify(a *attr) { |
| 64 |
c.notifiersmu.Lock() |
| 65 |
defer c.notifiersmu.Unlock() |
| 66 |
if n, found := c.notifiers[a.h]; found { |
| 67 |
n.stop() |
| 68 |
delete(c.notifiers, a.h) |
| 69 |
} |
| 70 |
} |