Skip to content

Commit 66b2f71

Browse files
committed
LE Data Channel examples: add SM handler to accept pairing
1 parent ff40ba6 commit 66b2f71

2 files changed

Lines changed: 71 additions & 5 deletions

File tree

example/le_data_channel_client.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ static bd_addr_type_t le_data_channel_addr_type;
7676

7777
static hci_con_handle_t connection_handle;
7878
static btstack_packet_callback_registration_t hci_event_callback_registration;
79+
static btstack_packet_callback_registration_t sm_event_callback_registration;
7980

8081
static uint8_t data_channel_buffer[TEST_PACKET_SIZE];
8182

@@ -306,6 +307,34 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
306307
}
307308
}
308309

310+
/*
311+
* @section SM Packet Handler
312+
*
313+
* @text The packet handler is used to handle pairing requests
314+
*/
315+
static void sm_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
316+
UNUSED(channel);
317+
UNUSED(size);
318+
319+
if (packet_type != HCI_EVENT_PACKET) return;
320+
321+
switch (hci_event_packet_get_type(packet)) {
322+
case SM_EVENT_JUST_WORKS_REQUEST:
323+
printf("Just Works requested\n");
324+
sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
325+
break;
326+
case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
327+
printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet));
328+
sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet));
329+
break;
330+
case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
331+
printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet));
332+
break;
333+
default:
334+
break;
335+
}
336+
}
337+
309338
#ifdef HAVE_BTSTACK_STDIN
310339
static void usage(const char *name){
311340
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[]){
345374
hci_event_callback_registration.callback = &packet_handler;
346375
hci_add_event_handler(&hci_event_callback_registration);
347376

377+
sm_event_callback_registration.callback = &sm_packet_handler;
378+
sm_add_event_handler(&sm_event_callback_registration);
379+
348380
// turn on!
349381
hci_power_control(HCI_POWER_ON);
350382

example/le_data_channel_server.c

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262

6363
const uint16_t TSPX_le_psm = 0x25;
6464

65-
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
65+
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
66+
static void sm_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
6667

6768
const uint8_t adv_data[] = {
6869
// Flags general discoverable, BR/EDR not supported
@@ -72,7 +73,8 @@ const uint8_t adv_data[] = {
7273
};
7374
const uint8_t adv_data_len = sizeof(adv_data);
7475

75-
static btstack_packet_callback_registration_t hci_event_callback_registration;
76+
static btstack_packet_callback_registration_t event_callback_registration;
77+
static btstack_packet_callback_registration_t sm_event_callback_registration;
7678

7779
// support for multiple clients
7880
typedef struct {
@@ -115,8 +117,12 @@ static void le_data_channel_setup(void){
115117
att_server_init(profile_data, NULL, NULL);
116118

117119
// register for HCI events
118-
hci_event_callback_registration.callback = &packet_handler;
119-
hci_add_event_handler(&hci_event_callback_registration);
120+
event_callback_registration.callback = &packet_handler;
121+
hci_add_event_handler(&event_callback_registration);
122+
123+
// register for SM events
124+
sm_event_callback_registration.callback = &sm_packet_handler;
125+
sm_add_event_handler(&sm_event_callback_registration);
120126

121127
l2cap_register_packet_handler(&packet_handler);
122128

@@ -198,7 +204,7 @@ static void streamer(void){
198204

199205

200206
/*
201-
* @section Packet Handler
207+
* @section HCI + L2CAP Packet Handler
202208
*
203209
* @text The packet handler is used to stop the notifications and reset the MTU on connect
204210
* 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
318324
}
319325
}
320326

327+
/*
328+
* @section SM Packet Handler
329+
*
330+
* @text The packet handler is used to handle pairing requests
331+
*/
332+
static void sm_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
333+
UNUSED(channel);
334+
UNUSED(size);
335+
336+
if (packet_type != HCI_EVENT_PACKET) return;
337+
338+
switch (hci_event_packet_get_type(packet)) {
339+
case SM_EVENT_JUST_WORKS_REQUEST:
340+
printf("Just Works requested\n");
341+
sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
342+
break;
343+
case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
344+
printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet));
345+
sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet));
346+
break;
347+
case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
348+
printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet));
349+
break;
350+
default:
351+
break;
352+
}
353+
}
354+
321355
int btstack_main(void);
322356
int btstack_main(void)
323357
{

0 commit comments

Comments
 (0)