6262
6363const 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
6768const uint8_t adv_data [] = {
6869 // Flags general discoverable, BR/EDR not supported
@@ -72,7 +73,8 @@ const uint8_t adv_data[] = {
7273};
7374const 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
7880typedef 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+
321355int btstack_main (void );
322356int btstack_main (void )
323357{
0 commit comments