Skip to content

Commit 0999f14

Browse files
committed
extmod/modbluetooth: Add gap_unpair command.
This function deletes the pairing details from the DB on both the application and the radio, so can be used to free up IRK slots on the radio.
1 parent a61c446 commit 0999f14

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

docs/library/bluetooth.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,11 @@ Pairing and bonding
733733

734734
On successful pairing, the ``_IRQ_ENCRYPTION_UPDATE`` event will be raised.
735735

736+
.. method:: BLE.gap_unpair(key, /)
737+
738+
Removes pairing details from the bond database, where ``key`` is the entry key
739+
as provided in _IRQ_GET_SECRET/_IRQ_SET_SECRET events.
740+
736741
.. method:: BLE.gap_passkey(conn_handle, action, passkey, /)
737742

738743
Respond to a ``_IRQ_PASSKEY_ACTION`` event for the specified *conn_handle*

extmod/btstack/modbluetooth_btstack.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,27 @@ int mp_bluetooth_gap_pair(uint16_t conn_handle) {
12621262
return 0;
12631263
}
12641264

1265+
int mp_bluetooth_gap_unpair(uint8_t *key, size_t key_len) {
1266+
DEBUG_printf("mp_bluetooth_gap_unpair\n");
1267+
if (BD_ADDR_LEN != key_len) {
1268+
mp_raise_ValueError(MP_ERROR_TEXT("Incorrect key length"));
1269+
}
1270+
1271+
int addr_type;
1272+
bd_addr_t addr;
1273+
sm_key_t irk;
1274+
for (int i = 0; i < MAX_NR_LE_DEVICE_DB_ENTRIES; i++) {
1275+
le_device_db_info(i, &addr_type, addr, irk);
1276+
if (addr_type != BD_ADDR_TYPE_UNKNOWN) {
1277+
if (0 == memcmp(key, addr, BD_ADDR_LEN)) {
1278+
le_device_db_remove(i);
1279+
return 0;
1280+
}
1281+
}
1282+
}
1283+
return MP_ENOENT;
1284+
}
1285+
12651286
int mp_bluetooth_gap_passkey(uint16_t conn_handle, uint8_t action, mp_int_t passkey) {
12661287
DEBUG_printf("mp_bluetooth_gap_passkey: conn_handle=%d action=%d passkey=%d\n", conn_handle, action, (int)passkey);
12671288
return MP_EOPNOTSUPP;

extmod/modbluetooth.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,21 @@ static mp_obj_t bluetooth_ble_gap_pair(mp_obj_t self_in, mp_obj_t conn_handle_in
716716
}
717717
static MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gap_pair_obj, bluetooth_ble_gap_pair);
718718

719+
static mp_obj_t bluetooth_ble_gap_unpair(mp_obj_t self_in, mp_obj_t key_buff) {
720+
(void)self_in;
721+
722+
uint8_t *key = NULL;
723+
size_t key_len = 0;
724+
725+
mp_buffer_info_t key_bufinfo = {0};
726+
mp_get_buffer_raise(key_buff, &key_bufinfo, MP_BUFFER_READ);
727+
key = key_bufinfo.buf;
728+
key_len = key_bufinfo.len;
729+
730+
return bluetooth_handle_errno(mp_bluetooth_gap_unpair(key, key_len));
731+
}
732+
static MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gap_unpair_obj, bluetooth_ble_gap_unpair);
733+
719734
static mp_obj_t bluetooth_ble_gap_passkey(size_t n_args, const mp_obj_t *args) {
720735
uint16_t conn_handle = mp_obj_get_int(args[1]);
721736
uint8_t action = mp_obj_get_int(args[2]);
@@ -944,6 +959,7 @@ static const mp_rom_map_elem_t bluetooth_ble_locals_dict_table[] = {
944959
{ MP_ROM_QSTR(MP_QSTR_gap_disconnect), MP_ROM_PTR(&bluetooth_ble_gap_disconnect_obj) },
945960
#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
946961
{ MP_ROM_QSTR(MP_QSTR_gap_pair), MP_ROM_PTR(&bluetooth_ble_gap_pair_obj) },
962+
{ MP_ROM_QSTR(MP_QSTR_gap_unpair), MP_ROM_PTR(&bluetooth_ble_gap_unpair_obj) },
947963
{ MP_ROM_QSTR(MP_QSTR_gap_passkey), MP_ROM_PTR(&bluetooth_ble_gap_passkey_obj) },
948964
#endif
949965
// GATT Server

extmod/modbluetooth.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,9 @@ int mp_bluetooth_set_preferred_mtu(uint16_t mtu);
358358
// Initiate pairing on the specified connection.
359359
int mp_bluetooth_gap_pair(uint16_t conn_handle);
360360

361+
// Remove a specific pairing key from the radio.
362+
int mp_bluetooth_gap_unpair(uint8_t *key, size_t key_len);
363+
361364
// Respond to a pairing request.
362365
int mp_bluetooth_gap_passkey(uint16_t conn_handle, uint8_t action, mp_int_t passkey);
363366
#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING

extmod/nimble/modbluetooth_nimble.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,15 @@ int mp_bluetooth_gap_pair(uint16_t conn_handle) {
11001100
return ble_hs_err_to_errno(ble_gap_security_initiate(conn_handle));
11011101
}
11021102

1103+
int mp_bluetooth_gap_unpair(uint8_t *key, size_t key_len) {
1104+
if (sizeof(ble_addr_t) != key_len) {
1105+
mp_raise_ValueError(MP_ERROR_TEXT("Incorrect key length"));
1106+
}
1107+
1108+
DEBUG_printf("mp_bluetooth_gap_unpair: specific\n");
1109+
return ble_hs_err_to_errno(ble_gap_unpair((ble_addr_t *)key));
1110+
}
1111+
11031112
int mp_bluetooth_gap_passkey(uint16_t conn_handle, uint8_t action, mp_int_t passkey) {
11041113
struct ble_sm_io io = {0};
11051114

0 commit comments

Comments
 (0)