Skip to content

Implement pairing with btstack. #14291

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 2 commits 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
42 changes: 21 additions & 21 deletions extmod/btstack/btstack.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,45 @@ target_include_directories(micropy_extmod_btstack INTERFACE

target_sources(micropy_extmod_btstack INTERFACE
${BTSTACK_LIB_DIR}/platform/embedded/hci_dump_embedded_stdout.c
${BTSTACK_LIB_DIR}/src/ad_parser.c
#${BTSTACK_LIB_DIR}/src/ad_parser.c
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to just remove these unused lines, rather than comment them out.

${BTSTACK_LIB_DIR}/src/ble/gatt-service/ancs_client.c
${BTSTACK_LIB_DIR}/src/ble/att_db.c
${BTSTACK_LIB_DIR}/src/ble/att_db_util.c
${BTSTACK_LIB_DIR}/src/ble/att_dispatch.c
${BTSTACK_LIB_DIR}/src/ble/att_server.c
${BTSTACK_LIB_DIR}/src/ble/gatt-service/battery_service_server.c
${BTSTACK_LIB_DIR}/src/ble/gatt-service/cycling_power_service_server.c
${BTSTACK_LIB_DIR}/src/ble/gatt-service/cycling_speed_and_cadence_service_server.c
${BTSTACK_LIB_DIR}/src/ble/gatt-service/device_information_service_server.c
${BTSTACK_LIB_DIR}/src/ble/gatt-service/heart_rate_service_server.c
${BTSTACK_LIB_DIR}/src/ble/gatt-service/hids_device.c
${BTSTACK_LIB_DIR}/src/mesh/gatt-service/mesh_provisioning_service_server.c
${BTSTACK_LIB_DIR}/src/mesh/gatt-service/mesh_proxy_service_server.c
${BTSTACK_LIB_DIR}/src/ble/gatt-service/nordic_spp_service_server.c
${BTSTACK_LIB_DIR}/src/ble/gatt-service/ublox_spp_service_server.c
#${BTSTACK_LIB_DIR}/src/ble/gatt-service/battery_service_server.c
#${BTSTACK_LIB_DIR}/src/ble/gatt-service/cycling_power_service_server.c
#${BTSTACK_LIB_DIR}/src/ble/gatt-service/cycling_speed_and_cadence_service_server.c
#${BTSTACK_LIB_DIR}/src/ble/gatt-service/device_information_service_server.c
#${BTSTACK_LIB_DIR}/src/ble/gatt-service/heart_rate_service_server.c
#${BTSTACK_LIB_DIR}/src/ble/gatt-service/hids_device.c
#${BTSTACK_LIB_DIR}/src/mesh/gatt-service/mesh_provisioning_service_server.c
#${BTSTACK_LIB_DIR}/src/mesh/gatt-service/mesh_proxy_service_server.c
#${BTSTACK_LIB_DIR}/src/ble/gatt-service/nordic_spp_service_server.c
#${BTSTACK_LIB_DIR}/src/ble/gatt-service/ublox_spp_service_server.c
${BTSTACK_LIB_DIR}/src/ble/gatt_client.c
${BTSTACK_LIB_DIR}/src/ble/le_device_db_memory.c
${BTSTACK_LIB_DIR}/src/ble/le_device_db_tlv.c
${BTSTACK_LIB_DIR}/src/ble/sm.c
${BTSTACK_LIB_DIR}/src/btstack_audio.c
${BTSTACK_LIB_DIR}/src/btstack_base64_decoder.c
#${BTSTACK_LIB_DIR}/src/btstack_audio.c
#${BTSTACK_LIB_DIR}/src/btstack_base64_decoder.c
${BTSTACK_LIB_DIR}/src/btstack_crypto.c
${BTSTACK_LIB_DIR}/src/btstack_hid_parser.c
#${BTSTACK_LIB_DIR}/src/btstack_hid_parser.c
${BTSTACK_LIB_DIR}/src/btstack_linked_list.c
${BTSTACK_LIB_DIR}/src/btstack_memory.c
${BTSTACK_LIB_DIR}/src/btstack_memory_pool.c
${BTSTACK_LIB_DIR}/src/btstack_resample.c
${BTSTACK_LIB_DIR}/src/btstack_ring_buffer.c
#${BTSTACK_LIB_DIR}/src/btstack_resample.c
#${BTSTACK_LIB_DIR}/src/btstack_ring_buffer.c
${BTSTACK_LIB_DIR}/src/btstack_run_loop.c
${BTSTACK_LIB_DIR}/src/btstack_run_loop_base.c
${BTSTACK_LIB_DIR}/src/btstack_slip.c
#${BTSTACK_LIB_DIR}/src/btstack_slip.c
${BTSTACK_LIB_DIR}/src/btstack_tlv.c
${BTSTACK_LIB_DIR}/src/btstack_tlv_none.c
#${BTSTACK_LIB_DIR}/src/btstack_tlv_none.c
${BTSTACK_LIB_DIR}/src/btstack_util.c
${BTSTACK_LIB_DIR}/src/hci.c
${BTSTACK_LIB_DIR}/src/hci_cmd.c
${BTSTACK_LIB_DIR}/src/hci_dump.c
${BTSTACK_LIB_DIR}/src/hci_transport_em9304_spi.c
${BTSTACK_LIB_DIR}/src/hci_transport_h4.c
#${BTSTACK_LIB_DIR}/src/hci_transport_em9304_spi.c
#${BTSTACK_LIB_DIR}/src/hci_transport_h4.c
${BTSTACK_LIB_DIR}/src/l2cap.c
${BTSTACK_LIB_DIR}/src/l2cap_signaling.c
)
2 changes: 1 addition & 1 deletion extmod/btstack/btstack.mk
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ INC += -I$(BTSTACK_DIR)/3rd-party/yxml

SRC_BTSTACK_C = \
$(addprefix lib/btstack/src/, $(SRC_FILES)) \
$(addprefix lib/btstack/src/ble/, $(filter-out %_tlv.c, $(SRC_BLE_FILES))) \
$(addprefix lib/btstack/src/ble/, $(filter-out le_device_db_memory.c, $(SRC_BLE_FILES))) \
lib/btstack/platform/embedded/hci_dump_embedded_stdout.c \

ifeq ($(MICROPY_BLUETOOTH_BTSTACK_USB),1)
Expand Down
2 changes: 1 addition & 1 deletion extmod/btstack/btstack_config_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#define MAX_NR_LE_DEVICE_DB_ENTRIES 4

// Link Key DB and LE Device DB using TLV on top of Flash Sector interface
// #define NVM_NUM_DEVICE_DB_ENTRIES 16
#define NVM_NUM_DEVICE_DB_ENTRIES 16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this here, the event _IRQ_GET_SECRET is called 16 times, with the string <n>DBT for n in 0..15 inclusive. I'm not sure exactly what this is needed for, do you know?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the le_device_db implementation of btstack: https://github.com/bluekitchen/btstack/blob/2b49e57bd1fae85ac32ac1f41cdb7c794de335f6/src/ble/le_device_db_tlv.c#L208. Basically it implements the device db as a (fixed) list of device, and this code scans through all slots to find the least recently used empty slot.


// We don't give btstack a malloc, so use a fixed-size ATT DB.
#define MAX_ATT_DB_SIZE 512
Expand Down
92 changes: 85 additions & 7 deletions extmod/btstack/modbluetooth_btstack.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include "extmod/modbluetooth.h"

#include "lib/btstack/src/btstack.h"
#include "lib/btstack/src/ble/le_device_db_tlv.h"
#include "lib/btstack/src/btstack_tlv.h"

#define DEBUG_printf(...) // printf("btstack: " __VA_ARGS__)

Expand Down Expand Up @@ -300,6 +302,12 @@ static void btstack_packet_handler_generic(uint8_t packet_type, uint16_t channel
desc->sm_connection_authenticated,
desc->sm_le_db_index != -1,
desc->sm_actual_encryption_key_size);
} else if (event_type == SM_EVENT_PASSKEY_DISPLAY_NUMBER) {
mp_bluetooth_gap_on_passkey_action(sm_event_passkey_display_number_get_handle(packet), MP_BLUETOOTH_PASSKEY_ACTION_DISPLAY, sm_event_passkey_display_number_get_passkey(packet));
} else if (event_type == SM_EVENT_PASSKEY_INPUT_NUMBER) {
mp_bluetooth_gap_on_passkey_action(sm_event_passkey_input_number_get_handle(packet), MP_BLUETOOTH_PASSKEY_ACTION_INPUT, 0);
} else if (event_type == SM_EVENT_NUMERIC_COMPARISON_REQUEST) {
mp_bluetooth_gap_on_passkey_action(sm_event_numeric_comparison_request_get_handle(packet), MP_BLUETOOTH_PASSKEY_ACTION_NUMERIC_COMPARISON, sm_event_numeric_comparison_request_get_passkey(packet));
#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
} else if (event_type == HCI_EVENT_DISCONNECTION_COMPLETE) {
DEBUG_printf(" --> hci disconnect complete\n");
Expand Down Expand Up @@ -372,7 +380,10 @@ static void btstack_packet_handler_generic(uint8_t packet_type, uint16_t channel
}
}

static btstack_packet_callback_registration_t hci_event_callback_registration = {
static btstack_packet_callback_registration_t mp_hci_event_callback_registration = {
.callback = &btstack_packet_handler_generic
};
static btstack_packet_callback_registration_t mp_sm_event_callback_registration = {
.callback = &btstack_packet_handler_generic
};

Expand Down Expand Up @@ -590,10 +601,13 @@ static void deinit_stack(void) {
hci_deinit();
btstack_memory_deinit();
btstack_run_loop_deinit();
btstack_crypto_deinit();

MP_STATE_PORT(bluetooth_btstack_root_pointers) = NULL;
}

static const btstack_tlv_t btstack_tlv_mp;

int mp_bluetooth_init(void) {
DEBUG_printf("mp_bluetooth_init\n");

Expand Down Expand Up @@ -623,6 +637,8 @@ int mp_bluetooth_init(void) {
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_STARTING;

l2cap_init();
btstack_tlv_set_instance(&btstack_tlv_mp, NULL);
le_device_db_tlv_configure(&btstack_tlv_mp, NULL);
le_device_db_init();
sm_init();

Expand All @@ -641,7 +657,9 @@ int mp_bluetooth_init(void) {
#endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT

// Register for HCI events.
hci_add_event_handler(&hci_event_callback_registration);
hci_add_event_handler(&mp_hci_event_callback_registration);

sm_add_event_handler(&mp_sm_event_callback_registration);

// Register for ATT server events.
att_server_register_packet_handler(&btstack_packet_handler_att_server);
Expand Down Expand Up @@ -771,10 +789,14 @@ void mp_bluetooth_set_address_mode(uint8_t addr_mode) {
set_random_address();
break;
}
case MP_BLUETOOTH_ADDRESS_MODE_RPA:
case MP_BLUETOOTH_ADDRESS_MODE_NRPA:
// Not yet supported.
mp_raise_OSError(MP_EINVAL);
case MP_BLUETOOTH_ADDRESS_MODE_RPA: {
gap_random_address_set_mode(GAP_RANDOM_ADDRESS_RESOLVABLE);
break;
}
case MP_BLUETOOTH_ADDRESS_MODE_NRPA: {
gap_random_address_set_mode(GAP_RANDOM_ADDRESS_NON_RESOLVABLE);
break;
}
}
}

Expand Down Expand Up @@ -1264,7 +1286,26 @@ int mp_bluetooth_gap_pair(uint16_t conn_handle) {

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;
switch (action) {
case MP_BLUETOOTH_PASSKEY_ACTION_INPUT: {
sm_passkey_input(conn_handle, passkey);
break;
}
case MP_BLUETOOTH_PASSKEY_ACTION_DISPLAY: {
sm_use_fixed_passkey_in_display_role(passkey);
break;
}
case MP_BLUETOOTH_PASSKEY_ACTION_NUMERIC_COMPARISON: {
if (passkey != 0) {
sm_numeric_comparison_confirm(conn_handle);
}
break;
}
default: {
return MP_EINVAL;
}
}
return 0;
}

#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
Expand Down Expand Up @@ -1545,4 +1586,41 @@ int mp_bluetooth_l2cap_recvinto(uint16_t conn_handle, uint16_t cid, uint8_t *buf

MP_REGISTER_ROOT_POINTER(struct _mp_bluetooth_btstack_root_pointers_t *bluetooth_btstack_root_pointers);

#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING

static int btstack_tlv_mp_get_tag(void *context, uint32_t tag, uint8_t *buffer, uint32_t buffer_size) {
UNUSED(context);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please just use (void)context; to match the rest of the code

const uint8_t *data;
size_t data_len;
if (!mp_bluetooth_gap_on_get_secret(0, 0, (uint8_t *)&tag, sizeof(tag), &data, &data_len)) {
return -1;
}
if (data_len > buffer_size) {
return -1;
}
memcpy(buffer, data, data_len);
return data_len;
}

static int btstack_tlv_mp_store_tag(void *context, uint32_t tag, const uint8_t *data, uint32_t data_size) {
UNUSED(context);
if (mp_bluetooth_gap_on_set_secret(0, (uint8_t *)&tag, sizeof(tag), (uint8_t *)data, data_size)) {
return 0;
} else {
return 1;
}
}

static void btstack_tlv_mp_delete_tag(void *context, uint32_t tag) {
mp_bluetooth_gap_on_set_secret(0, (uint8_t *)&tag, sizeof(tag), NULL, 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could do with a (void)context; here

}

static const btstack_tlv_t btstack_tlv_mp = {
&btstack_tlv_mp_get_tag,
&btstack_tlv_mp_store_tag,
&btstack_tlv_mp_delete_tag,
};

#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING

#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK
1 change: 1 addition & 0 deletions ports/rp2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ if(MICROPY_PY_BLUETOOTH)
MICROPY_PY_BLUETOOTH=1
MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS=1
MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE=1
MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING=1
)
endif()

Expand Down
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