LE Data Channel examples: add SM handler to accept pairing

This commit is contained in:
Matthias Ringwald 2020-10-30 11:44:35 +01:00
parent f060f1083e
commit 06c14afda2
2 changed files with 71 additions and 5 deletions

View File

@ -76,6 +76,7 @@ static bd_addr_type_t le_data_channel_addr_type;
static hci_con_handle_t connection_handle;
static btstack_packet_callback_registration_t hci_event_callback_registration;
static btstack_packet_callback_registration_t sm_event_callback_registration;
static uint8_t data_channel_buffer[TEST_PACKET_SIZE];
@ -306,6 +307,34 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
}
}
/*
* @section SM Packet Handler
*
* @text The packet handler is used to handle pairing requests
*/
static void sm_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
UNUSED(channel);
UNUSED(size);
if (packet_type != HCI_EVENT_PACKET) return;
switch (hci_event_packet_get_type(packet)) {
case SM_EVENT_JUST_WORKS_REQUEST:
printf("Just Works requested\n");
sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
break;
case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet));
sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet));
break;
case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet));
break;
default:
break;
}
}
#ifdef HAVE_BTSTACK_STDIN
static void usage(const char *name){
fprintf(stderr, "Usage: %s [-a|--address aa:bb:cc:dd:ee:ff]\n", name);
@ -345,6 +374,9 @@ int btstack_main(int argc, const char * argv[]){
hci_event_callback_registration.callback = &packet_handler;
hci_add_event_handler(&hci_event_callback_registration);
sm_event_callback_registration.callback = &sm_packet_handler;
sm_add_event_handler(&sm_event_callback_registration);
// turn on!
hci_power_control(HCI_POWER_ON);

View File

@ -62,7 +62,8 @@
const uint16_t TSPX_le_psm = 0x25;
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
static void sm_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
const uint8_t adv_data[] = {
// Flags general discoverable, BR/EDR not supported
@ -72,7 +73,8 @@ const uint8_t adv_data[] = {
};
const uint8_t adv_data_len = sizeof(adv_data);
static btstack_packet_callback_registration_t hci_event_callback_registration;
static btstack_packet_callback_registration_t event_callback_registration;
static btstack_packet_callback_registration_t sm_event_callback_registration;
// support for multiple clients
typedef struct {
@ -115,8 +117,12 @@ static void le_data_channel_setup(void){
att_server_init(profile_data, NULL, NULL);
// register for HCI events
hci_event_callback_registration.callback = &packet_handler;
hci_add_event_handler(&hci_event_callback_registration);
event_callback_registration.callback = &packet_handler;
hci_add_event_handler(&event_callback_registration);
// register for SM events
sm_event_callback_registration.callback = &sm_packet_handler;
sm_add_event_handler(&sm_event_callback_registration);
l2cap_register_packet_handler(&packet_handler);
@ -198,7 +204,7 @@ static void streamer(void){
/*
* @section Packet Handler
* @section HCI + L2CAP Packet Handler
*
* @text The packet handler is used to stop the notifications and reset the MTU on connect
* It would also be a good place to request the connection parameter update as indicated
@ -318,6 +324,34 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
}
}
/*
* @section SM Packet Handler
*
* @text The packet handler is used to handle pairing requests
*/
static void sm_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
UNUSED(channel);
UNUSED(size);
if (packet_type != HCI_EVENT_PACKET) return;
switch (hci_event_packet_get_type(packet)) {
case SM_EVENT_JUST_WORKS_REQUEST:
printf("Just Works requested\n");
sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
break;
case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet));
sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet));
break;
case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet));
break;
default:
break;
}
}
int btstack_main(void);
int btstack_main(void)
{