Skip to content

extmod/modbluetooth: Add gap_unpair command. #7845

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions docs/library/bluetooth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,11 @@ Pairing and bonding

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

.. method:: BLE.gap_unpair(key, /)

Removes pairing details from the bond database, where ``key`` is the entry key
as provided in _IRQ_GET_SECRET/_IRQ_SET_SECRET events.

.. method:: BLE.gap_passkey(conn_handle, action, passkey, /)

Respond to a ``_IRQ_PASSKEY_ACTION`` event for the specified *conn_handle*
Expand Down
21 changes: 21 additions & 0 deletions extmod/btstack/modbluetooth_btstack.c
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,27 @@ int mp_bluetooth_gap_pair(uint16_t conn_handle) {
return 0;
}

int mp_bluetooth_gap_unpair(uint8_t *key, size_t key_len) {
DEBUG_printf("mp_bluetooth_gap_unpair\n");
if (BD_ADDR_LEN != key_len) {
mp_raise_ValueError(MP_ERROR_TEXT("Incorrect key length"));
}

int addr_type;
bd_addr_t addr;
sm_key_t irk;
for (int i = 0; i < MAX_NR_LE_DEVICE_DB_ENTRIES; i++) {
le_device_db_info(i, &addr_type, addr, irk);
if (addr_type != BD_ADDR_TYPE_UNKNOWN) {
if (0 == memcmp(key, addr, BD_ADDR_LEN)) {
le_device_db_remove(i);
return 0;
}
}
}
return MP_ENOENT;
}

int mp_bluetooth_gap_passkey(uint16_t conn_handle, uint8_t action, mp_int_t passkey) {
DEBUG_printf("mp_bluetooth_gap_passkey: conn_handle=%d action=%d passkey=%d\n", conn_handle, action, (int)passkey);
return MP_EOPNOTSUPP;
Expand Down
16 changes: 16 additions & 0 deletions extmod/modbluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,21 @@ STATIC mp_obj_t bluetooth_ble_gap_pair(mp_obj_t self_in, mp_obj_t conn_handle_in
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gap_pair_obj, bluetooth_ble_gap_pair);

STATIC mp_obj_t bluetooth_ble_gap_unpair(mp_obj_t self_in, mp_obj_t key_buff) {
(void)self_in;

uint8_t *key = NULL;
size_t key_len = 0;

mp_buffer_info_t key_bufinfo = {0};
mp_get_buffer_raise(key_buff, &key_bufinfo, MP_BUFFER_READ);
key = key_bufinfo.buf;
key_len = key_bufinfo.len;

return bluetooth_handle_errno(mp_bluetooth_gap_unpair(key, key_len));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gap_unpair_obj, bluetooth_ble_gap_unpair);

STATIC mp_obj_t bluetooth_ble_gap_passkey(size_t n_args, const mp_obj_t *args) {
uint16_t conn_handle = mp_obj_get_int(args[1]);
uint8_t action = mp_obj_get_int(args[2]);
Expand Down Expand Up @@ -947,6 +962,7 @@ STATIC const mp_rom_map_elem_t bluetooth_ble_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_gap_disconnect), MP_ROM_PTR(&bluetooth_ble_gap_disconnect_obj) },
#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
{ MP_ROM_QSTR(MP_QSTR_gap_pair), MP_ROM_PTR(&bluetooth_ble_gap_pair_obj) },
{ MP_ROM_QSTR(MP_QSTR_gap_unpair), MP_ROM_PTR(&bluetooth_ble_gap_unpair_obj) },
{ MP_ROM_QSTR(MP_QSTR_gap_passkey), MP_ROM_PTR(&bluetooth_ble_gap_passkey_obj) },
#endif
// GATT Server
Expand Down
3 changes: 3 additions & 0 deletions extmod/modbluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ int mp_bluetooth_set_preferred_mtu(uint16_t mtu);
// Initiate pairing on the specified connection.
int mp_bluetooth_gap_pair(uint16_t conn_handle);

// Remove a specific pairing key from the radio.
int mp_bluetooth_gap_unpair(uint8_t *key, size_t key_len);

// Respond to a pairing request.
int mp_bluetooth_gap_passkey(uint16_t conn_handle, uint8_t action, mp_int_t passkey);
#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
Expand Down
9 changes: 9 additions & 0 deletions extmod/nimble/modbluetooth_nimble.c
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,15 @@ int mp_bluetooth_gap_pair(uint16_t conn_handle) {
return ble_hs_err_to_errno(ble_gap_security_initiate(conn_handle));
}

int mp_bluetooth_gap_unpair(uint8_t *key, size_t key_len) {
if (sizeof(ble_addr_t) != key_len) {
mp_raise_ValueError(MP_ERROR_TEXT("Incorrect key length"));
}

DEBUG_printf("mp_bluetooth_gap_unpair: specific\n");
return ble_hs_err_to_errno(ble_gap_unpair((ble_addr_t *)key));
}

int mp_bluetooth_gap_passkey(uint16_t conn_handle, uint8_t action, mp_int_t passkey) {
struct ble_sm_io io = {0};

Expand Down