extmod/modbluetooth: Add gap_pair(conn_handle) func to intiate pairing.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This commit is contained in:
Jim Mussared 2020-11-25 00:06:16 +11:00 committed by Damien George
parent f822557cbb
commit 801e8ffacf
4 changed files with 32 additions and 0 deletions

View File

@ -1167,6 +1167,14 @@ int mp_bluetooth_gap_disconnect(uint16_t conn_handle) {
return 0;
}
#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
int mp_bluetooth_gap_pair(uint16_t conn_handle) {
DEBUG_printf("mp_bluetooth_gap_pair: conn_handle=%d\n", conn_handle);
sm_request_pairing(conn_handle);
return 0;
}
#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
STATIC btstack_timer_source_t scan_duration_timeout;

View File

@ -682,6 +682,15 @@ STATIC mp_obj_t bluetooth_ble_gap_disconnect(mp_obj_t self_in, mp_obj_t conn_han
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gap_disconnect_obj, bluetooth_ble_gap_disconnect);
#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
STATIC mp_obj_t bluetooth_ble_gap_pair(mp_obj_t self_in, mp_obj_t conn_handle_in) {
(void)self_in;
uint16_t conn_handle = mp_obj_get_int(conn_handle_in);
return bluetooth_handle_errno(mp_bluetooth_gap_pair(conn_handle));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gap_pair_obj, bluetooth_ble_gap_pair);
#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
// ----------------------------------------------------------------------------
// Bluetooth object: GATTS (Peripheral/Advertiser role)
// ----------------------------------------------------------------------------
@ -883,6 +892,9 @@ STATIC const mp_rom_map_elem_t bluetooth_ble_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_gap_scan), MP_ROM_PTR(&bluetooth_ble_gap_scan_obj) },
#endif
{ 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) },
#endif
// GATT Server (i.e. peripheral/advertiser role)
{ MP_ROM_QSTR(MP_QSTR_gatts_register_services), MP_ROM_PTR(&bluetooth_ble_gatts_register_services_obj) },
{ MP_ROM_QSTR(MP_QSTR_gatts_read), MP_ROM_PTR(&bluetooth_ble_gatts_read_obj) },

View File

@ -319,6 +319,11 @@ int mp_bluetooth_gap_disconnect(uint16_t conn_handle);
int mp_bluetooth_get_preferred_mtu(void);
int mp_bluetooth_set_preferred_mtu(uint16_t mtu);
#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
// Initiate pairing on the specified connection.
int mp_bluetooth_gap_pair(uint16_t conn_handle);
#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
// Start a discovery (scan). Set duration to zero to run continuously.
int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_t window_us, bool active_scan);

View File

@ -860,6 +860,13 @@ int mp_bluetooth_set_preferred_mtu(uint16_t mtu) {
return 0;
}
#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
int mp_bluetooth_gap_pair(uint16_t conn_handle) {
DEBUG_printf("mp_bluetooth_gap_pair: conn_handle=%d\n", conn_handle);
return ble_hs_err_to_errno(ble_gap_security_initiate(conn_handle));
}
#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
STATIC void gattc_on_data_available(uint8_t event, uint16_t conn_handle, uint16_t value_handle, const struct os_mbuf *om) {