Skip to content

Commit 78869b0

Browse files
committed
fix: correct race condition from connection params in HCI implementation.
Basically it was responding by calling a function that retriggered asking to change the parameters yet again. Yet we are not actually doing anything about this request at present. Signed-off-by: deadprogram <[email protected]>
1 parent fb043fd commit 78869b0

File tree

5 files changed

+38
-17
lines changed

5 files changed

+38
-17
lines changed

att_hci.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ var (
7272
ErrATTUnknown = errors.New("bluetooth: ATT unknown error")
7373
ErrATTOp = errors.New("bluetooth: ATT OP error")
7474
ErrATTUnknownConnection = errors.New("bluetooth: ATT unknown connection")
75+
ErrATTAttributeNotFound = errors.New("bluetooth: ATT attribute not found")
7576
)
7677

7778
const defaultTimeoutSeconds = 10
@@ -528,7 +529,12 @@ func (a *att) handleData(handle uint16, buf []byte) error {
528529
println("att.handleData: attOpERROR", handle, cd.lastErrorOpcode, cd.lastErrorCode)
529530
}
530531

531-
return ErrATTOp
532+
switch cd.lastErrorCode {
533+
case attErrorAttrNotFound:
534+
return ErrATTAttributeNotFound
535+
default:
536+
return ErrATTOp
537+
}
532538

533539
case attOpMTUReq:
534540
if debug {

gap_hci.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,18 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
148148
if address.isRandom {
149149
random = 1
150150
}
151-
if err := a.hci.leCreateConn(0x0060, 0x0030, 0x00,
152-
random, makeNINAAddress(address.MAC),
153-
0x00, 0x0006, 0x000c, 0x0000, 0x00c8, 0x0004, 0x0006); err != nil {
151+
if err := a.hci.leCreateConn(0x0060, // interval
152+
0x0030, // window
153+
0x00, // initiatorFilter
154+
random, // peerBdaddrType
155+
makeNINAAddress(address.MAC), // peerBdaddr
156+
0x00, // ownBdaddrType
157+
0x0006, // minInterval
158+
0x000c, // maxInterval
159+
0x0000, // latency
160+
0x00c8, // supervisionTimeout
161+
0x0004, // minCeLength
162+
0x0006); err != nil { // maxCeLength
154163
return Device{}, err
155164
}
156165

gattc_hci.go

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ func (d Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) {
6969
for endHandle == uint16(0xffff) {
7070
err := d.adapter.att.readByGroupReq(d.handle, startHandle, endHandle, gattServiceUUID)
7171
if err != nil {
72+
if err == ErrATTAttributeNotFound {
73+
break
74+
}
7275
return nil, err
7376
}
7477

hci.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func (h *hci) processPacket() (bool, error) {
262262
return false, nil
263263
case h.pos >= hciACLLenPos+pktlen:
264264
if debug {
265-
println("hci acl data:", h.pos, hex.EncodeToString(h.buf[:hciACLLenPos+pktlen+1]))
265+
println("hci acl data recv:", h.pos, hex.EncodeToString(h.buf[:hciACLLenPos+pktlen+1]))
266266
}
267267

268268
h.pos = hciACLLenPos + pktlen + 1
@@ -280,7 +280,7 @@ func (h *hci) processPacket() (bool, error) {
280280
return false, nil
281281
case h.pos >= hciEvtLenPos+pktlen:
282282
if debug {
283-
println("hci event data:", h.pos, hex.EncodeToString(h.buf[:hciEvtLenPos+pktlen+1]))
283+
println("hci event data recv:", h.pos, hex.EncodeToString(h.buf[:hciEvtLenPos+pktlen+1]))
284284
}
285285

286286
h.pos = hciEvtLenPos + pktlen + 1
@@ -293,7 +293,7 @@ func (h *hci) processPacket() (bool, error) {
293293
if h.pos > 3 {
294294
pktlen := int(h.buf[3])
295295
if debug {
296-
println("hci synchronous data:", h.pos, pktlen, hex.EncodeToString(h.buf[:1+3+pktlen]))
296+
println("hci synchronous data recv:", h.pos, pktlen, hex.EncodeToString(h.buf[:1+3+pktlen]))
297297
}
298298

299299
// move to next packet
@@ -304,7 +304,7 @@ func (h *hci) processPacket() (bool, error) {
304304

305305
default:
306306
if debug {
307-
println("unknown packet data:", h.pos, h.end, hex.EncodeToString(h.buf[:h.pos]))
307+
println("unknown packet data recv:", h.pos, h.end, hex.EncodeToString(h.buf[:h.pos]))
308308
}
309309
return true, ErrHCIUnknown
310310
}
@@ -678,7 +678,11 @@ func (h *hci) handleEventData(buf []byte) error {
678678
switch buf[2] {
679679
case leMetaEventConnComplete, leMetaEventEnhancedConnectionComplete:
680680
if debug {
681-
println("leMetaEventConnComplete")
681+
if buf[2] == leMetaEventConnComplete {
682+
println("leMetaEventConnComplete", hex.EncodeToString(buf))
683+
} else {
684+
println("leMetaEventEnhancedConnectionComplete", hex.EncodeToString(buf))
685+
}
682686
}
683687

684688
h.connectData.connected = true
@@ -691,10 +695,10 @@ func (h *hci) handleEventData(buf []byte) error {
691695
switch buf[2] {
692696
case leMetaEventConnComplete:
693697
h.connectData.interval = binary.LittleEndian.Uint16(buf[14:])
694-
h.connectData.timeout = binary.LittleEndian.Uint16(buf[16:])
698+
h.connectData.timeout = binary.LittleEndian.Uint16(buf[18:])
695699
case leMetaEventEnhancedConnectionComplete:
696700
h.connectData.interval = binary.LittleEndian.Uint16(buf[26:])
697-
h.connectData.timeout = binary.LittleEndian.Uint16(buf[28:])
701+
h.connectData.timeout = binary.LittleEndian.Uint16(buf[30:])
698702
}
699703

700704
h.att.addConnection(h.connectData.handle)

l2cap_hci.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ func (l *l2cap) addConnection(handle uint16, role uint8, interval, timeout uint1
6868
return nil
6969
}
7070

71+
if timeout == 0 {
72+
timeout = 0x00c8
73+
}
74+
7175
var b [12]byte
7276
b[0] = connectionParamUpdateRequest
7377
b[1] = 0x01
@@ -108,7 +112,7 @@ func (l *l2cap) handleData(handle uint16, buf []byte) error {
108112

109113
func (l *l2cap) handleParameterUpdateRequest(connectionHandle uint16, identifier uint8, data []byte) error {
110114
if debug {
111-
println("l2cap.handleParameterUpdateRequest:", connectionHandle, "data:", hex.EncodeToString(data))
115+
println("l2cap.handleParameterUpdateRequest:", connectionHandle, "identifier:", identifier, "data:", hex.EncodeToString(data))
112116
}
113117

114118
req := l2capConnectionParamReqPkt{}
@@ -130,11 +134,6 @@ func (l *l2cap) handleParameterUpdateRequest(connectionHandle uint16, identifier
130134
return err
131135
}
132136

133-
// valid so update connection parameters
134-
if resp.value == 0 {
135-
return l.hci.leConnUpdate(connectionHandle, req.minInterval, req.maxInterval, req.latency, req.timeout)
136-
}
137-
138137
return nil
139138
}
140139

0 commit comments

Comments
 (0)