xref: /btstack/example/sm_pairing_central.c (revision bba48196553c8cb4568b4278b9e099c548e7c432)
17c485f8bSMatthias Ringwald /*
27c485f8bSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
37c485f8bSMatthias Ringwald  *
47c485f8bSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
57c485f8bSMatthias Ringwald  * modification, are permitted provided that the following conditions
67c485f8bSMatthias Ringwald  * are met:
77c485f8bSMatthias Ringwald  *
87c485f8bSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
97c485f8bSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
107c485f8bSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
117c485f8bSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
127c485f8bSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
137c485f8bSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
147c485f8bSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
157c485f8bSMatthias Ringwald  *    from this software without specific prior written permission.
167c485f8bSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
177c485f8bSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
187c485f8bSMatthias Ringwald  *    monetary gain.
197c485f8bSMatthias Ringwald  *
207c485f8bSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
217c485f8bSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
227c485f8bSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
257c485f8bSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
267c485f8bSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
277c485f8bSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
287c485f8bSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
297c485f8bSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
307c485f8bSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
317c485f8bSMatthias Ringwald  * SUCH DAMAGE.
327c485f8bSMatthias Ringwald  *
337c485f8bSMatthias Ringwald  * Please inquire about commercial licensing options at
347c485f8bSMatthias Ringwald  * [email protected]
357c485f8bSMatthias Ringwald  *
367c485f8bSMatthias Ringwald  */
37ab2c6ae4SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "sm_pairing_central.c"
397c485f8bSMatthias Ringwald 
407c485f8bSMatthias Ringwald 
417c485f8bSMatthias Ringwald // *****************************************************************************
42ec8ae085SMilanka Ringwald /* EXAMPLE_START(sm_pairing_central): LE Central - Test Pairing Methods
437c485f8bSMatthias Ringwald  *
447c485f8bSMatthias Ringwald  * @text Depending on the Authentication requiremens and IO Capabilities,
457c485f8bSMatthias Ringwald  * the pairing process uses different short and long term key generation method.
467c485f8bSMatthias Ringwald  * This example helps explore the different options incl. LE Secure Connections.
477c485f8bSMatthias Ringwald  * It scans for advertisements and connects to the first device that lists a
487c485f8bSMatthias Ringwald  * random service.
497c485f8bSMatthias Ringwald  */
507c485f8bSMatthias Ringwald  // *****************************************************************************
517c485f8bSMatthias Ringwald 
527c485f8bSMatthias Ringwald 
537c485f8bSMatthias Ringwald #include <stdint.h>
547c485f8bSMatthias Ringwald #include <inttypes.h>
557c485f8bSMatthias Ringwald #include <stdio.h>
567c485f8bSMatthias Ringwald #include <stdlib.h>
577c485f8bSMatthias Ringwald #include <string.h>
587c485f8bSMatthias Ringwald 
597c485f8bSMatthias Ringwald #include "btstack.h"
60a63a688aSMatthias Ringwald // sm_pairing_central.gatt contains the declaration of the provided GATT Services + Characteristics
61a63a688aSMatthias Ringwald // sm_pairing_central.h    contains the binary representation of sm_pairing_central.gatt
62a63a688aSMatthias Ringwald // it is generated by the build system by calling: $BTSTACK_ROOT/tool/compile_gatt.py sm_pairing_central.gatt sm_pairing_central.h
63a63a688aSMatthias Ringwald // it needs to be regenerated when the GATT Database declared in sm_pairing_central.gatt file is modified
643c90b2d9SMatthias Ringwald #include "sm_pairing_central.h"
657c485f8bSMatthias Ringwald 
667c485f8bSMatthias Ringwald 
677c485f8bSMatthias Ringwald // We're looking for a remote device that lists this service in the advertisement
687c485f8bSMatthias Ringwald // LightBlue assigns 0x1111 as the UUID for a Blank service.
697c485f8bSMatthias Ringwald #define REMOTE_SERVICE 0x1111
707c485f8bSMatthias Ringwald 
7198c0024dSMatthias Ringwald // Fixed passkey - used with sm_pairing_peripheral. Passkey is random in general
72e883851fSMatthias Ringwald #define FIXED_PASSKEY 123456U
7398c0024dSMatthias Ringwald 
747c485f8bSMatthias Ringwald 
757c485f8bSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
767c485f8bSMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
777c485f8bSMatthias Ringwald 
787c485f8bSMatthias Ringwald /* @section GAP LE setup for receiving advertisements
797c485f8bSMatthias Ringwald  *
807c485f8bSMatthias Ringwald  * @text GAP LE advertisements are received as custom HCI events of the
817c485f8bSMatthias Ringwald  * GAP_EVENT_ADVERTISING_REPORT type. To receive them, you'll need to register
827c485f8bSMatthias Ringwald  * the HCI packet handler, as shown in Listing GAPLEAdvSetup.
837c485f8bSMatthias Ringwald  */
847c485f8bSMatthias Ringwald 
857c485f8bSMatthias Ringwald /* LISTING_START(GAPLEAdvSetup): Setting up GAP LE client for receiving advertisements */
8698c0024dSMatthias Ringwald static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
8798c0024dSMatthias Ringwald static void sm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
887c485f8bSMatthias Ringwald 
sm_pairing_central_setup(void)897c485f8bSMatthias Ringwald static void sm_pairing_central_setup(void){
907c485f8bSMatthias Ringwald     l2cap_init();
917c485f8bSMatthias Ringwald 
927c485f8bSMatthias Ringwald     // setup SM: Display only
937c485f8bSMatthias Ringwald     sm_init();
947c485f8bSMatthias Ringwald 
95e5779dd2SMatthias Ringwald     // setup ATT server
96e5779dd2SMatthias Ringwald     att_server_init(profile_data, NULL, NULL);
97e5779dd2SMatthias Ringwald 
983c3e5a84SMatthias Ringwald     // setup GATT Client
993c3e5a84SMatthias Ringwald     gatt_client_init();
1007c485f8bSMatthias Ringwald 
101a4fe6467SMatthias Ringwald     // register handler
10298c0024dSMatthias Ringwald     hci_event_callback_registration.callback = &hci_packet_handler;
103a4fe6467SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
104a4fe6467SMatthias Ringwald 
10598c0024dSMatthias Ringwald     sm_event_callback_registration.callback = &sm_packet_handler;
106a4fe6467SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
107a4fe6467SMatthias Ringwald 
1083c3e5a84SMatthias Ringwald 
1093c3e5a84SMatthias Ringwald     // Configuration
1103c3e5a84SMatthias Ringwald 
1113c3e5a84SMatthias Ringwald     // Enable mandatory authentication for GATT Client
1123c3e5a84SMatthias Ringwald     // - if un-encrypted connections are not supported, e.g. when connecting to own device, this enforces authentication
1133c3e5a84SMatthias Ringwald     // gatt_client_set_required_security_level(LEVEL_2);
1143c3e5a84SMatthias Ringwald 
1153c3e5a84SMatthias Ringwald     /**
1163c3e5a84SMatthias Ringwald      * Choose ONE of the following configurations
1173c3e5a84SMatthias Ringwald      * Bonding is disabled to allow for repeated testing. It can be enabled by or'ing
1183c3e5a84SMatthias Ringwald      * SM_AUTHREQ_BONDING to the authentication requirements like this:
1193c3e5a84SMatthias Ringwald      * sm_set_authentication_requirements( X | SM_AUTHREQ_BONDING)
1203c3e5a84SMatthias Ringwald      */
1213c3e5a84SMatthias Ringwald 
1227c485f8bSMatthias Ringwald     // LE Legacy Pairing, Just Works
12398c0024dSMatthias Ringwald     // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO);
1243cdbe9dbSMatthias Ringwald     // sm_set_authentication_requirements(0);
1257c485f8bSMatthias Ringwald 
1267c485f8bSMatthias Ringwald     // LE Legacy Pairing, Passkey entry initiator enter, responder (us) displays
1277c485f8bSMatthias Ringwald     // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
1287c485f8bSMatthias Ringwald     // sm_set_authentication_requirements(SM_AUTHREQ_MITM_PROTECTION);
12998c0024dSMatthias Ringwald     // sm_use_fixed_passkey_in_display_role(FIXED_PASSKEY);
1307c485f8bSMatthias Ringwald 
1317c485f8bSMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS
1323cdbe9dbSMatthias Ringwald 
1333cdbe9dbSMatthias Ringwald     // enable LE Secure Connections Only mode - disables Legacy pairing
1343cdbe9dbSMatthias Ringwald     // sm_set_secure_connections_only_mode(true);
1353cdbe9dbSMatthias Ringwald 
1363cdbe9dbSMatthias Ringwald     // LE Secure Connections, Just Works
1377c485f8bSMatthias Ringwald     // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO);
1387c485f8bSMatthias Ringwald     // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION);
1397c485f8bSMatthias Ringwald 
1407c485f8bSMatthias Ringwald     // LE Secure Connections, Numeric Comparison
1413c3e5a84SMatthias Ringwald     // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO);
1423c3e5a84SMatthias Ringwald     // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION);
1437c485f8bSMatthias Ringwald 
14432112218SMatthias Ringwald     // LE Secure Pairing, Passkey entry initiator (us) enters, responder displays
14532112218SMatthias Ringwald     // sm_set_io_capabilities(IO_CAPABILITY_KEYBOARD_ONLY);
1467c485f8bSMatthias Ringwald     // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION);
14798c0024dSMatthias Ringwald     // sm_use_fixed_passkey_in_display_role(FIXED_PASSKEY);
14832112218SMatthias Ringwald 
14932112218SMatthias Ringwald     // LE Secure Pairing, Passkey entry initiator (us) displays, responder enters
15032112218SMatthias Ringwald     // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
15132112218SMatthias Ringwald     // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION);
1527c485f8bSMatthias Ringwald #endif
1537c485f8bSMatthias Ringwald }
1547c485f8bSMatthias Ringwald 
1557c485f8bSMatthias Ringwald /* LISTING_END */
1567c485f8bSMatthias Ringwald 
1577c485f8bSMatthias Ringwald /* @section HCI packet handler
1587c485f8bSMatthias Ringwald  *
1597c485f8bSMatthias Ringwald  * @text The HCI packet handler has to start the scanning,
1607c485f8bSMatthias Ringwald  * and to handle received advertisements. Advertisements are received
1617c485f8bSMatthias Ringwald  * as HCI event packets of the GAP_EVENT_ADVERTISING_REPORT type,
16298c0024dSMatthias Ringwald  * see Listing HCIPacketHandler.
1637c485f8bSMatthias Ringwald  */
1647c485f8bSMatthias Ringwald 
16598c0024dSMatthias Ringwald /* LISTING_START(HCIPacketHandler): Scanning and receiving advertisements */
1667c485f8bSMatthias Ringwald 
hci_packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)16798c0024dSMatthias Ringwald static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1689ec2630cSMatthias Ringwald     UNUSED(channel);
1699ec2630cSMatthias Ringwald     UNUSED(size);
1709ec2630cSMatthias Ringwald 
1717c485f8bSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
1727c485f8bSMatthias Ringwald     hci_con_handle_t con_handle;
1733c3e5a84SMatthias Ringwald     uint8_t status;
1747c485f8bSMatthias Ringwald 
1757c485f8bSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
1767c485f8bSMatthias Ringwald         case BTSTACK_EVENT_STATE:
1777c485f8bSMatthias Ringwald             // BTstack activated, get started
1787c485f8bSMatthias Ringwald             if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
1797c485f8bSMatthias Ringwald                 printf("Start scaning!\n");
1807c485f8bSMatthias Ringwald                 gap_set_scan_parameters(1,0x0030, 0x0030);
1817c485f8bSMatthias Ringwald                 gap_start_scan();
1827c485f8bSMatthias Ringwald             }
1837c485f8bSMatthias Ringwald             break;
1847c485f8bSMatthias Ringwald         case GAP_EVENT_ADVERTISING_REPORT:{
1857c485f8bSMatthias Ringwald             bd_addr_t address;
1867c485f8bSMatthias Ringwald             gap_event_advertising_report_get_address(packet, address);
1877c485f8bSMatthias Ringwald             uint8_t address_type = gap_event_advertising_report_get_address_type(packet);
1887c485f8bSMatthias Ringwald             uint8_t length = gap_event_advertising_report_get_data_length(packet);
1897c485f8bSMatthias Ringwald             const uint8_t * data = gap_event_advertising_report_get_data(packet);
19098c0024dSMatthias Ringwald             // printf("Advertisement event: addr-type %u, addr %s, data[%u] ",
19198c0024dSMatthias Ringwald             //   address_type, bd_addr_to_str(address), length);
19298c0024dSMatthias Ringwald             // printf_hexdump(data, length);
1937c485f8bSMatthias Ringwald             if (!ad_data_contains_uuid16(length, (uint8_t *) data, REMOTE_SERVICE)) break;
1947c485f8bSMatthias Ringwald             printf("Found remote with UUID %04x, connecting...\n", REMOTE_SERVICE);
1957c485f8bSMatthias Ringwald             gap_stop_scan();
1967c485f8bSMatthias Ringwald             gap_connect(address,address_type);
1977c485f8bSMatthias Ringwald             break;
1987c485f8bSMatthias Ringwald         }
199*bba48196SMatthias Ringwald         case HCI_EVENT_META_GAP:
20098c0024dSMatthias Ringwald             // wait for connection complete
201*bba48196SMatthias Ringwald             if (hci_event_gap_meta_get_subevent_code(packet) != GAP_SUBEVENT_LE_CONNECTION_COMPLETE) break;
202*bba48196SMatthias Ringwald             con_handle = gap_subevent_le_connection_complete_get_connection_handle(packet);
20398c0024dSMatthias Ringwald             printf("Connection complete\n");
2043c3e5a84SMatthias Ringwald 
2053c3e5a84SMatthias Ringwald             // for testing, choose one of the following actions
2063c3e5a84SMatthias Ringwald 
2073c3e5a84SMatthias Ringwald             // manually start pairing
20898c0024dSMatthias Ringwald             sm_request_pairing(con_handle);
2093c3e5a84SMatthias Ringwald 
2103c3e5a84SMatthias Ringwald             // gatt client request to authenticated characteristic in sm_pairing_peripheral (short cut, uses hard-coded value handle)
2113c3e5a84SMatthias Ringwald             // gatt_client_read_value_of_characteristic_using_value_handle(&hci_packet_handler, con_handle, 0x0009);
2123c3e5a84SMatthias Ringwald 
2133c3e5a84SMatthias Ringwald             // general gatt client request to trigger mandatory authentication
2143c3e5a84SMatthias Ringwald             // gatt_client_discover_primary_services(&hci_packet_handler, con_handle);
21598c0024dSMatthias Ringwald             break;
2163c3e5a84SMatthias Ringwald         case GATT_EVENT_QUERY_COMPLETE:
2173c3e5a84SMatthias Ringwald             status = gatt_event_query_complete_get_att_status(packet);
2183c3e5a84SMatthias Ringwald             switch (status){
2193c3e5a84SMatthias Ringwald                 case ATT_ERROR_INSUFFICIENT_ENCRYPTION:
2203c3e5a84SMatthias Ringwald                     printf("GATT Query result: Insufficient Encryption\n");
2213c3e5a84SMatthias Ringwald                     break;
2223c3e5a84SMatthias Ringwald                 case ATT_ERROR_INSUFFICIENT_AUTHENTICATION:
2233c3e5a84SMatthias Ringwald                     printf("GATT Query result: Insufficient Authentication\n");
2243c3e5a84SMatthias Ringwald                     break;
2253c3e5a84SMatthias Ringwald                 case ATT_ERROR_BONDING_INFORMATION_MISSING:
2263c3e5a84SMatthias Ringwald                     printf("GATT Query result: Bonding Information Missing\n");
2273c3e5a84SMatthias Ringwald                     break;
2283c3e5a84SMatthias Ringwald                 case ATT_ERROR_SUCCESS:
2293c3e5a84SMatthias Ringwald                     printf("GATT Query result: OK\n");
2303c3e5a84SMatthias Ringwald                     break;
2313c3e5a84SMatthias Ringwald                 default:
2323c3e5a84SMatthias Ringwald                     printf("GATT Query result: 0x%02x\n", gatt_event_query_complete_get_att_status(packet));
2333c3e5a84SMatthias Ringwald                     break;
2343c3e5a84SMatthias Ringwald             }
2353c3e5a84SMatthias Ringwald             break;
23698c0024dSMatthias Ringwald         default:
23798c0024dSMatthias Ringwald             break;
23898c0024dSMatthias Ringwald     }
23998c0024dSMatthias Ringwald }
24098c0024dSMatthias Ringwald 
24198c0024dSMatthias Ringwald /* @section HCI packet handler
24298c0024dSMatthias Ringwald  *
24398c0024dSMatthias Ringwald  * @text The SM packet handler receives Security Manager Events required for pairing.
24498c0024dSMatthias Ringwald  * It also receives events generated during Identity Resolving
24598c0024dSMatthias Ringwald  * see Listing SMPacketHandler.
24698c0024dSMatthias Ringwald  */
24798c0024dSMatthias Ringwald 
24898c0024dSMatthias Ringwald /* LISTING_START(SMPacketHandler): Scanning and receiving advertisements */
24998c0024dSMatthias Ringwald 
sm_packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)25098c0024dSMatthias Ringwald static void sm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
25198c0024dSMatthias Ringwald     UNUSED(channel);
25298c0024dSMatthias Ringwald     UNUSED(size);
25398c0024dSMatthias Ringwald 
25498c0024dSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
25598c0024dSMatthias Ringwald 
2567e65711bSMatthias Ringwald     bd_addr_t addr;
257e8edf5acSMatthias Ringwald     bd_addr_type_t addr_type;
258e8edf5acSMatthias Ringwald 
25998c0024dSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
2607c485f8bSMatthias Ringwald         case SM_EVENT_JUST_WORKS_REQUEST:
2617c485f8bSMatthias Ringwald             printf("Just works requested\n");
2627c485f8bSMatthias Ringwald             sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
2637c485f8bSMatthias Ringwald             break;
2647c485f8bSMatthias Ringwald         case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
265bace42efSMatthias Ringwald             printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet));
2667c485f8bSMatthias Ringwald             sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet));
2677c485f8bSMatthias Ringwald             break;
2687c485f8bSMatthias Ringwald         case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
269bace42efSMatthias Ringwald             printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet));
2707c485f8bSMatthias Ringwald             break;
27198c0024dSMatthias Ringwald         case SM_EVENT_PASSKEY_INPUT_NUMBER:
27298c0024dSMatthias Ringwald             printf("Passkey Input requested\n");
273e883851fSMatthias Ringwald             printf("Sending fixed passkey %"PRIu32"\n", (uint32_t) FIXED_PASSKEY);
27498c0024dSMatthias Ringwald             sm_passkey_input(sm_event_passkey_input_number_get_handle(packet), FIXED_PASSKEY);
27598c0024dSMatthias Ringwald             break;
2767c4db9dcSMatthias Ringwald         case SM_EVENT_PAIRING_STARTED:
2777c4db9dcSMatthias Ringwald             printf("Pairing started\n");
2787c4db9dcSMatthias Ringwald             break;
279eee18978SMatthias Ringwald         case SM_EVENT_PAIRING_COMPLETE:
280eee18978SMatthias Ringwald             switch (sm_event_pairing_complete_get_status(packet)){
281eee18978SMatthias Ringwald                 case ERROR_CODE_SUCCESS:
282eee18978SMatthias Ringwald                     printf("Pairing complete, success\n");
283eee18978SMatthias Ringwald                     break;
284eee18978SMatthias Ringwald                 case ERROR_CODE_CONNECTION_TIMEOUT:
285eee18978SMatthias Ringwald                     printf("Pairing failed, timeout\n");
286eee18978SMatthias Ringwald                     break;
287eee18978SMatthias Ringwald                 case ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION:
2883cdbe9dbSMatthias Ringwald                     printf("Pairing failed, disconnected\n");
289eee18978SMatthias Ringwald                     break;
290eee18978SMatthias Ringwald                 case ERROR_CODE_AUTHENTICATION_FAILURE:
29139620ea5SMatthias Ringwald                     printf("Pairing failed, authentication failure with reason = %u\n", sm_event_pairing_complete_get_reason(packet));
292eee18978SMatthias Ringwald                     break;
293eee18978SMatthias Ringwald                 default:
294eee18978SMatthias Ringwald                     break;
295eee18978SMatthias Ringwald             }
296eee18978SMatthias Ringwald             break;
2977e65711bSMatthias Ringwald         case SM_EVENT_REENCRYPTION_STARTED:
2987e65711bSMatthias Ringwald             sm_event_reencryption_complete_get_address(packet, addr);
2997e65711bSMatthias Ringwald             printf("Bonding information exists for addr type %u, identity addr %s -> start re-encryption\n",
3007e65711bSMatthias Ringwald                    sm_event_reencryption_started_get_addr_type(packet), bd_addr_to_str(addr));
3017e65711bSMatthias Ringwald             break;
3027e65711bSMatthias Ringwald         case SM_EVENT_REENCRYPTION_COMPLETE:
3037e65711bSMatthias Ringwald             switch (sm_event_reencryption_complete_get_status(packet)){
3047e65711bSMatthias Ringwald                 case ERROR_CODE_SUCCESS:
3057e65711bSMatthias Ringwald                     printf("Re-encryption complete, success\n");
3067e65711bSMatthias Ringwald                     break;
3077e65711bSMatthias Ringwald                 case ERROR_CODE_CONNECTION_TIMEOUT:
3087e65711bSMatthias Ringwald                     printf("Re-encryption failed, timeout\n");
3097e65711bSMatthias Ringwald                     break;
3107e65711bSMatthias Ringwald                 case ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION:
3117e65711bSMatthias Ringwald                     printf("Re-encryption failed, disconnected\n");
3127e65711bSMatthias Ringwald                     break;
313bd37f2bfSMatthias Ringwald                 case ERROR_CODE_PIN_OR_KEY_MISSING:
314bd37f2bfSMatthias Ringwald                     printf("Re-encryption failed, bonding information missing\n\n");
315e8edf5acSMatthias Ringwald                     printf("Assuming remote lost bonding information\n");
316e8edf5acSMatthias Ringwald                     printf("Deleting local bonding information and start new pairing...\n");
317e8edf5acSMatthias Ringwald                     sm_event_reencryption_complete_get_address(packet, addr);
318e8edf5acSMatthias Ringwald                     addr_type = sm_event_reencryption_started_get_addr_type(packet);
319e8edf5acSMatthias Ringwald                     gap_delete_bonding(addr_type, addr);
320e8edf5acSMatthias Ringwald                     sm_request_pairing(sm_event_reencryption_complete_get_handle(packet));
3217e65711bSMatthias Ringwald                     break;
3227e65711bSMatthias Ringwald                 default:
3237e65711bSMatthias Ringwald                     break;
3247e65711bSMatthias Ringwald             }
3257e65711bSMatthias Ringwald             break;
3267c485f8bSMatthias Ringwald         default:
3277c485f8bSMatthias Ringwald             break;
3287c485f8bSMatthias Ringwald     }
3297c485f8bSMatthias Ringwald }
3307c485f8bSMatthias Ringwald /* LISTING_END */
3317c485f8bSMatthias Ringwald 
3327c485f8bSMatthias Ringwald int btstack_main(void);
btstack_main(void)3337c485f8bSMatthias Ringwald int btstack_main(void)
3347c485f8bSMatthias Ringwald {
3357c485f8bSMatthias Ringwald     sm_pairing_central_setup();
3367c485f8bSMatthias Ringwald 
3377c485f8bSMatthias Ringwald     // turn on!
3387c485f8bSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
3397c485f8bSMatthias Ringwald 
3407c485f8bSMatthias Ringwald     return 0;
3417c485f8bSMatthias Ringwald }
3427c485f8bSMatthias Ringwald 
3437c485f8bSMatthias Ringwald /* EXAMPLE_END */
344