forked from go-ble/ble
/
conn.go
225 lines (175 loc) · 4.62 KB
/
conn.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
package darwin
import (
"context"
"fmt"
"log"
"sync"
"github.com/JuulLabs-OSS/ble"
"github.com/JuulLabs-OSS/cbgo"
)
// newGenConn creates a new generic (role-less) connection. This should not be
// called directly; use newCentralConn or newPeripheralConn instead.
func newGenConn(d *Device, a ble.Addr) (*conn, error) {
c := &conn{
dev: d,
rxMTU: 23,
txMTU: 23,
addr: a,
done: make(chan struct{}),
notifiers: make(map[cbgo.Characteristic]ble.Notifier),
subs: make(map[string]*sub),
chrReads: make(map[string]chan error),
}
err := d.addConn(c)
if err != nil {
return nil, err
}
go func() {
<-c.done
d.delConn(c.addr)
}()
return c, nil
}
// newCentralConn creates a new connection with us acting as central
// (peer=peripheral).
func newCentralConn(d *Device, prph cbgo.Peripheral) (*conn, error) {
c, err := newGenConn(d, ble.NewAddr(prph.Identifier().String()))
if err != nil {
return nil, err
}
// -3 to account for WriteCommand base.
c.txMTU = prph.MaximumWriteValueLength(false) - 3
c.prph = prph
return c, nil
}
// newCentralConn creates a new connection with us acting as peripheral
// (peer=central).
func newPeripheralConn(d *Device, cent cbgo.Central) (*conn, error) {
c, err := newGenConn(d, ble.NewAddr(cent.Identifier().String()))
if err != nil {
return nil, err
}
// -3 to account for ATT_HANDLE_VALUE_NTF base.
c.txMTU = cent.MaximumUpdateValueLength() - 3
c.cent = cent
return c, nil
}
type conn struct {
sync.RWMutex
dev *Device
ctx context.Context
rxMTU int
txMTU int
addr ble.Addr
done chan struct{}
evl clientEventListener
prph cbgo.Peripheral
cent cbgo.Central
notifiers map[cbgo.Characteristic]ble.Notifier // central connection only
subs map[string]*sub
chrReads map[string](chan error)
}
func (c *conn) Context() context.Context {
return c.ctx
}
func (c *conn) SetContext(ctx context.Context) {
c.ctx = ctx
}
func (c *conn) LocalAddr() ble.Addr {
// return c.dev.Address()
return c.addr // FIXME
}
func (c *conn) RemoteAddr() ble.Addr {
return c.addr
}
func (c *conn) RxMTU() int {
return c.rxMTU
}
func (c *conn) SetRxMTU(mtu int) {
c.rxMTU = mtu
}
func (c *conn) TxMTU() int {
return c.txMTU
}
func (c *conn) SetTxMTU(mtu int) {
c.Lock()
c.txMTU = mtu
c.Unlock()
}
func (c *conn) Read(b []byte) (int, error) {
return 0, nil
}
func (c *conn) Write(b []byte) (int, error) {
return 0, nil
}
func (c *conn) Close() error {
c.evl.Close()
return nil
}
// Disconnected returns a receiving channel, which is closed when the connection disconnects.
func (c *conn) Disconnected() <-chan struct{} {
return c.done
}
// processChrRead handles an incoming read response. CoreBluetooth does not
// distinguish explicit reads from unsolicited notifications. This function
// identifies which type the incoming message is.
func (c *conn) processChrRead(err error, cbchr cbgo.Characteristic) {
c.RLock()
defer c.RUnlock()
uuidStr := uuidStrWithDashes(cbchr.UUID().String())
found := false
ch := c.chrReads[uuidStr]
if ch != nil {
ch <- err
found = true
}
s := c.subs[uuidStr]
if s != nil {
s.fn(cbchr.Value())
found = true
}
if !found {
log.Printf("received characteristic read response without corresponding request: uuid=%s", uuidStr)
}
}
// addChrReader starts listening for a solicited read response.
func (c *conn) addChrReader(char *ble.Characteristic) (chan error, error) {
uuidStr := uuidStrWithDashes(char.UUID.String())
c.Lock()
defer c.Unlock()
if c.chrReads[uuidStr] != nil {
return nil, fmt.Errorf("cannot read from the same attribute twice: uuid=%s", uuidStr)
}
ch := make(chan error)
c.chrReads[uuidStr] = ch
return ch, nil
}
// delChrReader stops listening for a solicited read response.
func (c *conn) delChrReader(char *ble.Characteristic) {
c.Lock()
defer c.Unlock()
uuidStr := uuidStrWithDashes(char.UUID.String())
delete(c.chrReads, uuidStr)
}
// addSub starts listening for unsolicited notifications and indications for a
// particular characteristic.
func (c *conn) addSub(char *ble.Characteristic, fn ble.NotificationHandler) {
uuidStr := uuidStrWithDashes(char.UUID.String())
c.Lock()
defer c.Unlock()
// It feels like we should return an error if we are already subscribed to
// this characteristic. Just quietly overwrite the existing handler to
// preserve backwards compatibility.
c.subs[uuidStr] = &sub{
fn: fn,
char: char,
}
}
// delSub stops listening for unsolicited notifications and indications for a
// particular characteristic.
func (c *conn) delSub(char *ble.Characteristic) {
uuidStr := uuidStrWithDashes(char.UUID.String())
c.Lock()
defer c.Unlock()
delete(c.subs, uuidStr)
}