Skip to content

Fixes and improvements to connection timeouts on SoftDevices #221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions adapter_nrf528xx-full.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ func handleEvent() {
C.sd_ble_gap_phy_update(gapEvent.conn_handle, &phyUpdateRequest.peer_preferred_phys)
case C.BLE_GAP_EVT_PHY_UPDATE:
// 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:
if debug {
println("unknown GAP event:", id)
Expand Down
37 changes: 24 additions & 13 deletions gap_nrf528xx-central.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
import "C"

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.
var (
Expand Down Expand Up @@ -94,7 +95,7 @@ func (a *Adapter) StopScan() error {

// In-progress connection attempt.
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
}

Expand Down Expand Up @@ -140,14 +141,17 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
scanParams.set_bitfield_active(0)
scanParams.interval = C.uint16_t(NewDuration(40 * time.Millisecond))
scanParams.window = C.uint16_t(NewDuration(30 * time.Millisecond))
scanParams.timeout = C.uint16_t(params.ConnectionTimeout)
scanParams.timeout = C.uint16_t(params.ConnectionTimeout / 16) // timeout in 10ms units

connectionParams := C.ble_gap_conn_params_t{
min_conn_interval: C.uint16_t(params.MinInterval) / 2,
max_conn_interval: C.uint16_t(params.MaxInterval) / 2,
slave_latency: 0, // mostly relevant to connected keyboards etc
conn_sup_timeout: 200, // 2 seconds (in 10ms units), the minimum recommended by Apple
}
if params.Timeout != 0 {
connectionParams.conn_sup_timeout = uint16(params.Timeout / 16)
}

// Flag to the event handler that we are waiting for incoming connections.
// This should be safe as long as Connect is not called concurrently. And
Expand All @@ -165,18 +169,25 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
}

// Wait until the connection is established.
// TODO: use some sort of condition variable once the scheduler supports
// them.
for connectionAttempt.state.Get() != 2 {
arm.Asm("wfe")
for {
state := connectionAttempt.state.Get()
if state == 2 {
// Successfully connected.
connectionAttempt.state.Set(0)
connectionHandle := connectionAttempt.connectionHandle
return Device{
connectionHandle: connectionHandle,
}, 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")
}
}
connectionHandle := connectionAttempt.connectionHandle
connectionAttempt.state.Set(0)

// Connection has been established.
return Device{
connectionHandle: connectionHandle,
}, nil
}

// Disconnect from the BLE device.
Expand Down