Skip to content

Commit de857fc

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 35fb90b commit de857fc

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
@@ -713,6 +713,11 @@ Pairing and bonding
713713

714714
On successful pairing, the ``_IRQ_ENCRYPTION_UPDATE`` event will be raised.
715715

716+
.. method:: BLE.gap_unpair(key, /)
717+
718+
Removes pairing details from the bond database, where ``key`` is the entry key
719+
as provided in _IRQ_GET_SECRET/_IRQ_SET_SECRET events.
720+
716721
.. method:: BLE.gap_passkey(conn_handle, action, passkey, /)
717722

718723
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
@@ -1220,6 +1220,27 @@ int mp_bluetooth_gap_pair(uint16_t conn_handle) {
12201220
return 0;
12211221
}
12221222

1223+
int mp_bluetooth_gap_unpair(uint8_t *key, size_t key_len) {
1224+
DEBUG_printf("mp_bluetooth_gap_unpair\n");
1225+
if (BD_ADDR_LEN != key_len) {
1226+
mp_raise_ValueError(MP_ERROR_TEXT("Incorrect key length"));
1227+
}
1228+
1229+
int addr_type;
1230+
bd_addr_t addr;
1231+
sm_key_t irk;
1232+
for (int i = 0; i < MAX_NR_LE_DEVICE_DB_ENTRIES; i++) {
1233+
le_device_db_info(i, &addr_type, addr, irk);
1234+
if (addr_type != BD_ADDR_TYPE_UNKNOWN) {
1235+
if (0 == memcmp(key, addr, BD_ADDR_LEN)) {
1236+
le_device_db_remove(i);
1237+
return 0;
1238+
}
1239+
}
1240+
}
1241+
return MP_ENOENT;
1242+
}
1243+
12231244
int mp_bluetooth_gap_passkey(uint16_t conn_handle, uint8_t action, mp_int_t passkey) {
12241245
DEBUG_printf("mp_bluetooth_gap_passkey: conn_handle=%d action=%d passkey=%d\n", conn_handle, action, (int)passkey);
12251246
return MP_EOPNOTSUPP;

extmod/modbluetooth.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,21 @@ STATIC mp_obj_t bluetooth_ble_gap_pair(mp_obj_t self_in, mp_obj_t conn_handle_in
695695
}
696696
STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gap_pair_obj, bluetooth_ble_gap_pair);
697697

698+
STATIC mp_obj_t bluetooth_ble_gap_unpair(mp_obj_t self_in, mp_obj_t key_buff) {
699+
(void)self_in;
700+
701+
uint8_t *key = NULL;
702+
size_t key_len = 0;
703+
704+
mp_buffer_info_t key_bufinfo = {0};
705+
mp_get_buffer_raise(key_buff, &key_bufinfo, MP_BUFFER_READ);
706+
key = key_bufinfo.buf;
707+
key_len = key_bufinfo.len;
708+
709+
return bluetooth_handle_errno(mp_bluetooth_gap_unpair(key, key_len));
710+
}
711+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gap_unpair_obj, bluetooth_ble_gap_unpair);
712+
698713
STATIC mp_obj_t bluetooth_ble_gap_passkey(size_t n_args, const mp_obj_t *args) {
699714
uint16_t conn_handle = mp_obj_get_int(args[1]);
700715
uint8_t action = mp_obj_get_int(args[2]);
@@ -926,6 +941,7 @@ STATIC const mp_rom_map_elem_t bluetooth_ble_locals_dict_table[] = {
926941
{ MP_ROM_QSTR(MP_QSTR_gap_disconnect), MP_ROM_PTR(&bluetooth_ble_gap_disconnect_obj) },
927942
#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
928943
{ MP_ROM_QSTR(MP_QSTR_gap_pair), MP_ROM_PTR(&bluetooth_ble_gap_pair_obj) },
944+
{ MP_ROM_QSTR(MP_QSTR_gap_unpair), MP_ROM_PTR(&bluetooth_ble_gap_unpair_obj) },
929945
{ MP_ROM_QSTR(MP_QSTR_gap_passkey), MP_ROM_PTR(&bluetooth_ble_gap_passkey_obj) },
930946
#endif
931947
// 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
@@ -1081,6 +1081,15 @@ int mp_bluetooth_gap_pair(uint16_t conn_handle) {
10811081
return ble_hs_err_to_errno(ble_gap_security_initiate(conn_handle));
10821082
}
10831083

1084+
int mp_bluetooth_gap_unpair(uint8_t *key, size_t key_len) {
1085+
if (sizeof(ble_addr_t) != key_len) {
1086+
mp_raise_ValueError(MP_ERROR_TEXT("Incorrect key length"));
1087+
}
1088+
1089+
DEBUG_printf("mp_bluetooth_gap_unpair: specific\n");
1090+
return ble_hs_err_to_errno(ble_gap_unpair((ble_addr_t *)key));
1091+
}
1092+
10841093
int mp_bluetooth_gap_passkey(uint16_t conn_handle, uint8_t action, mp_int_t passkey) {
10851094
struct ble_sm_io io = {0};
10861095

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy