Skip to content

Commit

Permalink
LE Data Channel examples: add SM handler to accept pairing
Browse files Browse the repository at this point in the history
  • Loading branch information
mringwal committed Oct 30, 2020
1 parent ff40ba6 commit 66b2f71
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
32 changes: 32 additions & 0 deletions example/le_data_channel_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
44 changes: 39 additions & 5 deletions example/le_data_channel_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 66b2f71

Please sign in to comment.