Skip to content

Commit 3f8f8a6

Browse files
aykevldeadprogram
authored andcommitted
softdevice: return an error on a connection timeout
This makes sure an error is reported on a connection timeout. Previously it would just block forever.
1 parent d74f6a1 commit 3f8f8a6

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

adapter_nrf528xx-full.go

+15
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ func handleEvent() {
115115
C.sd_ble_gap_phy_update(gapEvent.conn_handle, &phyUpdateRequest.peer_preferred_phys)
116116
case C.BLE_GAP_EVT_PHY_UPDATE:
117117
// ignore confirmation of phy successfully updated
118+
case C.BLE_GAP_EVT_TIMEOUT:
119+
timeoutEvt := gapEvent.params.unionfield_timeout()
120+
switch timeoutEvt.src {
121+
case C.BLE_GAP_TIMEOUT_SRC_CONN:
122+
// Failed to connect to a peripheral.
123+
if debug {
124+
println("gap timeout: conn")
125+
}
126+
connectionAttempt.state.Set(3) // connection timed out
127+
default:
128+
// For example a scan timeout.
129+
if debug {
130+
println("gap timeout: other")
131+
}
132+
}
118133
default:
119134
if debug {
120135
println("unknown GAP event:", id)

gap_nrf528xx-central.go

+20-12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
import "C"
1717

1818
var errAlreadyConnecting = errors.New("bluetooth: already in a connection attempt")
19+
var errConnectionTimeout = errors.New("bluetooth: timeout while connecting")
1920

2021
// Memory buffers needed by sd_ble_gap_scan_start.
2122
var (
@@ -94,7 +95,7 @@ func (a *Adapter) StopScan() error {
9495

9596
// In-progress connection attempt.
9697
var connectionAttempt struct {
97-
state volatile.Register8 // 0 means unused, 1 means connecting, 2 means ready (connected or timeout)
98+
state volatile.Register8 // 0 means unused, 1 means connecting, 2 means connected, 3 means timeout
9899
connectionHandle C.uint16_t
99100
}
100101

@@ -165,18 +166,25 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
165166
}
166167

167168
// Wait until the connection is established.
168-
// TODO: use some sort of condition variable once the scheduler supports
169-
// them.
170-
for connectionAttempt.state.Get() != 2 {
171-
arm.Asm("wfe")
169+
for {
170+
state := connectionAttempt.state.Get()
171+
if state == 2 {
172+
// Successfully connected.
173+
connectionAttempt.state.Set(0)
174+
connectionHandle := connectionAttempt.connectionHandle
175+
return Device{
176+
connectionHandle: connectionHandle,
177+
}, nil
178+
} else if state == 3 {
179+
// Timeout while connecting.
180+
connectionAttempt.state.Set(0)
181+
return Device{}, errConnectionTimeout
182+
} else {
183+
// TODO: use some sort of condition variable once the scheduler
184+
// supports them.
185+
arm.Asm("wfe")
186+
}
172187
}
173-
connectionHandle := connectionAttempt.connectionHandle
174-
connectionAttempt.state.Set(0)
175-
176-
// Connection has been established.
177-
return Device{
178-
connectionHandle: connectionHandle,
179-
}, nil
180188
}
181189

182190
// Disconnect from the BLE device.

0 commit comments

Comments
 (0)