File: central_darwin.go

package info (click to toggle)
golang-github-bettercap-gatt 0.0~git20240808.ec4935e-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 568 kB
  • sloc: ansic: 82; asm: 18; makefile: 3
file content (70 lines) | stat: -rw-r--r-- 1,845 bytes parent folder | download
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
package gatt

import (
	"sync"

	"github.com/bettercap/gatt/xpc"
)

type central struct {
	dev         *device
	uuid        UUID
	mtu         int
	notifiers   map[uint16]*notifier
	notifiersmu *sync.Mutex
}

func newCentral(d *device, u UUID) *central {
	return &central{
		dev:         d,
		mtu:         23,
		uuid:        u,
		notifiers:   make(map[uint16]*notifier),
		notifiersmu: &sync.Mutex{},
	}
}

func (c *central) ID() string   { return c.uuid.String() }
func (c *central) Close() error { return nil }
func (c *central) MTU() int     { return c.mtu }

func (c *central) sendNotification(a *attr, b []byte) (int, error) {
	data := make([]byte, len(b))
	copy(data, b) // have to make a copy, why?
	c.dev.sendCmd(15, xpc.Dict{
		// "kCBMsgArgUUIDs": [][]byte{reverse(c.uuid.b)}, // connection interrupted
		// "kCBMsgArgUUIDs": [][]byte{c.uuid.b}, // connection interrupted
		// "kCBMsgArgUUIDs": []xpc.UUID{xpc.UUID(reverse(c.uuid.b))},
		// "kCBMsgArgUUIDs": []xpc.UUID{xpc.UUID(c.uuid.b)},
		// "kCBMsgArgUUIDs": reverse(c.uuid.b),
		//
		// FIXME: Sigh... tried to targeting the central, but couldn't get work.
		// So, broadcast to all subscribed centrals. Either of the following works.
		// "kCBMsgArgUUIDs": []xpc.UUID{},
		"kCBMsgArgUUIDs":       [][]byte{},
		"kCBMsgArgAttributeID": a.h,
		"kCBMsgArgData":        data,
	})
	return len(b), nil
}

func (c *central) startNotify(a *attr, maxlen int) {
	c.notifiersmu.Lock()
	defer c.notifiersmu.Unlock()
	if _, found := c.notifiers[a.h]; found {
		return
	}
	n := newNotifier(c, a, maxlen)
	c.notifiers[a.h] = n
	char := a.pvt.(*Characteristic)
	go char.nhandler.ServeNotify(Request{Central: c}, n)
}

func (c *central) stopNotify(a *attr) {
	c.notifiersmu.Lock()
	defer c.notifiersmu.Unlock()
	if n, found := c.notifiers[a.h]; found {
		n.stop()
		delete(c.notifiers, a.h)
	}
}