Skip to content

Commit 7bd9b96

Browse files
committed
pbio/drv/bluetooth_cc2640: Fix attempting to bond with host.
Fixes pybricks/support#2522
1 parent 10af94a commit 7bd9b96

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@
2626
- Fixed internal rounding error that could could cause a Drive Base to be 1 mm
2727
off after driving 3 meters, depending on configuration parameters ([support#2500]).
2828
- Fixed Powered Up remote light getting the wrong color ([support#2497]).
29-
- Fix shutdown animation not visible when shutting down due to Bluetooth glitches.
30-
- Fixed Powered Up remote raising the wrong exception on timeout.
31-
- Fixed Xbox Controller connection waiting indefinitely if it isn't in pairing mode.
29+
- Fix shutdown animation not visible when shutting down due to Bluetooth glitches ([support#2521]).
30+
- Fixed Powered Up remote raising the wrong exception on timeout ([support#2521]).
31+
- Fixed Xbox Controller connection waiting indefinitely if it isn't in pairing mode ([support#2521]).
32+
- Fixed Xbox Controller attempting to pair with host ([support#2522]).
3233

3334
[support#1962]: https://github.com/pybricks/support/issues/1962
3435
[support#2468]: https://github.com/pybricks/support/issues/2468
3536
[support#2497]: https://github.com/pybricks/support/issues/2497
3637
[support#2500]: https://github.com/pybricks/support/issues/2500
38+
[support#2521]: https://github.com/pybricks/support/issues/2521
39+
[support#2522]: https://github.com/pybricks/support/issues/2522
3740
[pybricks-micropython#405]: https://github.com/pybricks/pybricks-micropython/pull/405
3841
[pybricks-micropython#421]: https://github.com/pybricks/pybricks-micropython/pull/421
3942
[pybricks-micropython#425]: https://github.com/pybricks/pybricks-micropython/pull/425

lib/pbio/drv/bluetooth/bluetooth_stm32_cc2640.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ static uint16_t conn_mtu;
118118

119119
// Bonding status of the peripheral.
120120
static uint16_t bond_auth_err = NO_AUTH;
121+
static uint8_t bond_auth_mode_last = GAPBOND_PAIRING_MODE_NO_PAIRING;
121122

122123
// GATT service handles
123124
static uint16_t gatt_service_handle, gatt_service_end_handle;
@@ -241,6 +242,15 @@ pbio_error_t pbdrv_bluetooth_start_advertising_func(pbio_os_state_t *state, void
241242

242243
DEBUG_PRINT("set_discoverable\n");
243244

245+
// If we were set to initiate pairing because we bonded with a peripheral
246+
// previously, unset it to avoid trying to bond with a PC host on connect.
247+
if (bond_auth_mode_last != GAPBOND_PAIRING_MODE_NO_PAIRING) {
248+
bond_auth_mode_last = GAPBOND_PAIRING_MODE_NO_PAIRING;
249+
PBIO_OS_AWAIT_WHILE(state, write_xfer_size);
250+
GAP_BondMgrSetParameter(GAPBOND_PAIRING_MODE, sizeof(bond_auth_mode_last), &bond_auth_mode_last);
251+
PBIO_OS_AWAIT_UNTIL(state, hci_command_status);
252+
}
253+
244254
static int8_t tx_power;
245255
PBIO_OS_AWAIT_WHILE(state, write_xfer_size);
246256
HCI_LE_readAdvertisingChannelTxPower();
@@ -366,8 +376,6 @@ pbio_error_t pbdrv_bluetooth_send_pybricks_value_notification(pbio_os_state_t *s
366376
pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect_func(pbio_os_state_t *state, void *context) {
367377
pbdrv_bluetooth_peripheral_t *peri = context;
368378

369-
static uint8_t buf[1];
370-
371379
static pbio_os_timer_t peripheral_scan_restart_timer;
372380

373381
// Scan and connect timeout, if applicable.
@@ -516,14 +524,12 @@ pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect_func(pbio_os_state_t *s
516524

517525
// Configure to initiate pairing right after connect if bonding required.
518526
// NB: We must unset "initiate" before we allow a new connection to
519-
// Pybricks Code or it will try to pair with the PC. However, this happens
520-
// automatically since gap_init runs again on disconnect, which resets it.
521-
// It won't unset automatically if Pybricks Code is already connected, but
522-
// then it doesn't matter since we're already connected.
527+
// Pybricks Code or it will try to pair with the PC. We do this just before
528+
// starting to advertise again, which covers all our use cases.
523529
PBIO_OS_AWAIT_WHILE(state, write_xfer_size);
524-
buf[0] = (peri->config->options & PBDRV_BLUETOOTH_PERIPHERAL_OPTIONS_PAIR) ?
530+
bond_auth_mode_last = (peri->config->options & PBDRV_BLUETOOTH_PERIPHERAL_OPTIONS_PAIR) ?
525531
GAPBOND_PAIRING_MODE_INITIATE : GAPBOND_PAIRING_MODE_NO_PAIRING;
526-
GAP_BondMgrSetParameter(GAPBOND_PAIRING_MODE, 1, buf);
532+
GAP_BondMgrSetParameter(GAPBOND_PAIRING_MODE, sizeof(bond_auth_mode_last), &bond_auth_mode_last);
527533
PBIO_OS_AWAIT_UNTIL(state, hci_command_status);
528534

529535
PBIO_OS_AWAIT_WHILE(state, write_xfer_size);
@@ -1571,7 +1577,7 @@ static const struct {
15711577
{GAPBOND_PAIRING_MODE, GAPBOND_PAIRING_MODE_NO_PAIRING},
15721578
{GAPBOND_MITM_PROTECTION, 0}, // disabled
15731579
{GAPBOND_IO_CAPABILITIES, GAPBOND_IO_CAP_NO_INPUT_NO_OUTPUT},
1574-
{GAPBOND_BONDING_ENABLED, 1}, // // enabled, as in allowed. It won't happen by default.
1580+
{GAPBOND_BONDING_ENABLED, 1}, // enabled, as in allowed. It won't happen by default.
15751581
{GAPBOND_SECURE_CONNECTION, GAPBOND_SECURE_CONNECTION_NONE},
15761582
};
15771583

0 commit comments

Comments
 (0)