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.
This commit is contained in:
parent
d74f6a1009
commit
3f8f8a6622
2 changed files with 35 additions and 12 deletions
|
@ -115,6 +115,21 @@ func handleEvent() {
|
||||||
C.sd_ble_gap_phy_update(gapEvent.conn_handle, &phyUpdateRequest.peer_preferred_phys)
|
C.sd_ble_gap_phy_update(gapEvent.conn_handle, &phyUpdateRequest.peer_preferred_phys)
|
||||||
case C.BLE_GAP_EVT_PHY_UPDATE:
|
case C.BLE_GAP_EVT_PHY_UPDATE:
|
||||||
// ignore confirmation of phy successfully updated
|
// ignore confirmation of phy successfully updated
|
||||||
|
case C.BLE_GAP_EVT_TIMEOUT:
|
||||||
|
timeoutEvt := gapEvent.params.unionfield_timeout()
|
||||||
|
switch timeoutEvt.src {
|
||||||
|
case C.BLE_GAP_TIMEOUT_SRC_CONN:
|
||||||
|
// Failed to connect to a peripheral.
|
||||||
|
if debug {
|
||||||
|
println("gap timeout: conn")
|
||||||
|
}
|
||||||
|
connectionAttempt.state.Set(3) // connection timed out
|
||||||
|
default:
|
||||||
|
// For example a scan timeout.
|
||||||
|
if debug {
|
||||||
|
println("gap timeout: other")
|
||||||
|
}
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
if debug {
|
if debug {
|
||||||
println("unknown GAP event:", id)
|
println("unknown GAP event:", id)
|
||||||
|
|
|
@ -16,6 +16,7 @@ import (
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
var errAlreadyConnecting = errors.New("bluetooth: already in a connection attempt")
|
var errAlreadyConnecting = errors.New("bluetooth: already in a connection attempt")
|
||||||
|
var errConnectionTimeout = errors.New("bluetooth: timeout while connecting")
|
||||||
|
|
||||||
// Memory buffers needed by sd_ble_gap_scan_start.
|
// Memory buffers needed by sd_ble_gap_scan_start.
|
||||||
var (
|
var (
|
||||||
|
@ -94,7 +95,7 @@ func (a *Adapter) StopScan() error {
|
||||||
|
|
||||||
// In-progress connection attempt.
|
// In-progress connection attempt.
|
||||||
var connectionAttempt struct {
|
var connectionAttempt struct {
|
||||||
state volatile.Register8 // 0 means unused, 1 means connecting, 2 means ready (connected or timeout)
|
state volatile.Register8 // 0 means unused, 1 means connecting, 2 means connected, 3 means timeout
|
||||||
connectionHandle C.uint16_t
|
connectionHandle C.uint16_t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,18 +166,25 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait until the connection is established.
|
// Wait until the connection is established.
|
||||||
// TODO: use some sort of condition variable once the scheduler supports
|
for {
|
||||||
// them.
|
state := connectionAttempt.state.Get()
|
||||||
for connectionAttempt.state.Get() != 2 {
|
if state == 2 {
|
||||||
arm.Asm("wfe")
|
// Successfully connected.
|
||||||
}
|
|
||||||
connectionHandle := connectionAttempt.connectionHandle
|
|
||||||
connectionAttempt.state.Set(0)
|
connectionAttempt.state.Set(0)
|
||||||
|
connectionHandle := connectionAttempt.connectionHandle
|
||||||
// Connection has been established.
|
|
||||||
return Device{
|
return Device{
|
||||||
connectionHandle: connectionHandle,
|
connectionHandle: connectionHandle,
|
||||||
}, nil
|
}, nil
|
||||||
|
} else if state == 3 {
|
||||||
|
// Timeout while connecting.
|
||||||
|
connectionAttempt.state.Set(0)
|
||||||
|
return Device{}, errConnectionTimeout
|
||||||
|
} else {
|
||||||
|
// TODO: use some sort of condition variable once the scheduler
|
||||||
|
// supports them.
|
||||||
|
arm.Asm("wfe")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disconnect from the BLE device.
|
// Disconnect from the BLE device.
|
||||||
|
|
Loading…
Reference in a new issue