xref: /btstack/example/sm_pairing_peripheral.c (revision 8f19f013a14580c1f0d0c6803bf97784b9493d28)
1d7ee901aSMatthias Ringwald /*
2d7ee901aSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3d7ee901aSMatthias Ringwald  *
4d7ee901aSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5d7ee901aSMatthias Ringwald  * modification, are permitted provided that the following conditions
6d7ee901aSMatthias Ringwald  * are met:
7d7ee901aSMatthias Ringwald  *
8d7ee901aSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9d7ee901aSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10d7ee901aSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11d7ee901aSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12d7ee901aSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13d7ee901aSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14d7ee901aSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15d7ee901aSMatthias Ringwald  *    from this software without specific prior written permission.
16d7ee901aSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17d7ee901aSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18d7ee901aSMatthias Ringwald  *    monetary gain.
19d7ee901aSMatthias Ringwald  *
20d7ee901aSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21d7ee901aSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22d7ee901aSMatthias 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,
25d7ee901aSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26d7ee901aSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27d7ee901aSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28d7ee901aSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29d7ee901aSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30d7ee901aSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31d7ee901aSMatthias Ringwald  * SUCH DAMAGE.
32d7ee901aSMatthias Ringwald  *
33d7ee901aSMatthias Ringwald  * Please inquire about commercial licensing options at
34d7ee901aSMatthias Ringwald  * [email protected]
35d7ee901aSMatthias Ringwald  *
36d7ee901aSMatthias Ringwald  */
37d7ee901aSMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "sm_pairing_peripheral.c"
39ab2c6ae4SMatthias Ringwald 
40d7ee901aSMatthias Ringwald // *****************************************************************************
41ec8ae085SMilanka Ringwald /* EXAMPLE_START(sm_pairing_peripheral): LE Peripheral - Test Pairing Methods
42d7ee901aSMatthias Ringwald  *
43d7ee901aSMatthias Ringwald  * @text Depending on the Authentication requiremens and IO Capabilities,
44d7ee901aSMatthias Ringwald  * the pairing process uses different short and long term key generation method.
45d7ee901aSMatthias Ringwald  * This example helps explore the different options incl. LE Secure Connections.
46d7ee901aSMatthias Ringwald  */
47d7ee901aSMatthias Ringwald  // *****************************************************************************
48d7ee901aSMatthias Ringwald 
49d7ee901aSMatthias Ringwald #include <stdint.h>
50d7ee901aSMatthias Ringwald #include <stdio.h>
51d7ee901aSMatthias Ringwald #include <stdlib.h>
52d7ee901aSMatthias Ringwald #include <string.h>
53bace42efSMatthias Ringwald #include <inttypes.h>
54d7ee901aSMatthias Ringwald 
55d7ee901aSMatthias Ringwald #include "sm_pairing_peripheral.h"
56d7ee901aSMatthias Ringwald #include "btstack.h"
57d7ee901aSMatthias Ringwald 
58d7ee901aSMatthias Ringwald /* @section Main Application Setup
59d7ee901aSMatthias Ringwald  *
60d7ee901aSMatthias Ringwald  * @text Listing MainConfiguration shows main application code.
61d7ee901aSMatthias Ringwald  * It initializes L2CAP, the Security Manager and configures the ATT Server with the pre-compiled
62d7ee901aSMatthias Ringwald  * ATT Database generated from $sm_pairing_peripheral.gatt$. Finally, it configures the advertisements
63d7ee901aSMatthias Ringwald  * and boots the Bluetooth stack.
6473704aa9SMatthias Ringwald  * In this example, the Advertisement contains the Flags attribute, the device name, and a 16-bit (test) service 0x1111
65d7ee901aSMatthias Ringwald  * The flag 0x06 indicates: LE General Discoverable Mode and BR/EDR not supported.
66d7ee901aSMatthias Ringwald  * Various examples for IO Capabilites and Authentication Requirements are given below.
67d7ee901aSMatthias Ringwald  */
68d7ee901aSMatthias Ringwald 
6973704aa9SMatthias Ringwald /* LISTING_START(MainConfiguration): Setup stack to advertise */
70d7ee901aSMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
71*8f19f013SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
72d7ee901aSMatthias Ringwald 
73*8f19f013SMatthias Ringwald static void sm_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
74*8f19f013SMatthias Ringwald static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
75d7ee901aSMatthias Ringwald 
76d7ee901aSMatthias Ringwald const uint8_t adv_data[] = {
77d7ee901aSMatthias Ringwald     // Flags general discoverable, BR/EDR not supported
78bf85c285SMatthias Ringwald     0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06,
79d7ee901aSMatthias Ringwald     // Name
80bf85c285SMatthias Ringwald     0x0b, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'S', 'M', ' ', 'P', 'a', 'i', 'r', 'i', 'n', 'g',
81bde654efSMatthias Ringwald     // Incomplete List of 16-bit Service Class UUIDs -- 1111 - only valid for testing!
82bde654efSMatthias Ringwald     0x03, BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS, 0x11, 0x11,
83d7ee901aSMatthias Ringwald };
84d7ee901aSMatthias Ringwald const uint8_t adv_data_len = sizeof(adv_data);
85d7ee901aSMatthias Ringwald 
sm_peripheral_setup(void)86d7ee901aSMatthias Ringwald static void sm_peripheral_setup(void){
87d7ee901aSMatthias Ringwald 
88d7ee901aSMatthias Ringwald     l2cap_init();
89d7ee901aSMatthias Ringwald 
90d7ee901aSMatthias Ringwald     // setup SM: Display only
91d7ee901aSMatthias Ringwald     sm_init();
92d7ee901aSMatthias Ringwald 
933c3e5a84SMatthias Ringwald     // setup ATT server
943c3e5a84SMatthias Ringwald     att_server_init(profile_data, NULL, NULL);
953c3e5a84SMatthias Ringwald 
963c3e5a84SMatthias Ringwald     // setup GATT Client
973c3e5a84SMatthias Ringwald     gatt_client_init();
983c3e5a84SMatthias Ringwald 
993c3e5a84SMatthias Ringwald     // setup advertisements
1003c3e5a84SMatthias Ringwald     uint16_t adv_int_min = 0x0030;
1013c3e5a84SMatthias Ringwald     uint16_t adv_int_max = 0x0030;
1023c3e5a84SMatthias Ringwald     uint8_t adv_type = 0;
1033c3e5a84SMatthias Ringwald     bd_addr_t null_addr;
1043c3e5a84SMatthias Ringwald     memset(null_addr, 0, 6);
1053c3e5a84SMatthias Ringwald     gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
1063c3e5a84SMatthias Ringwald     gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
1073c3e5a84SMatthias Ringwald     gap_advertisements_enable(1);
1083c3e5a84SMatthias Ringwald 
109*8f19f013SMatthias Ringwald     // register handler
110*8f19f013SMatthias Ringwald     hci_event_callback_registration.callback = &hci_packet_handler;
111*8f19f013SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
112*8f19f013SMatthias Ringwald 
113*8f19f013SMatthias Ringwald     sm_event_callback_registration.callback = &sm_packet_handler;
1143c3e5a84SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
1153c3e5a84SMatthias Ringwald 
1163c3e5a84SMatthias Ringwald     // Configuration
1173c3e5a84SMatthias Ringwald 
1183c3e5a84SMatthias Ringwald     // Enable mandatory authentication for GATT Client
1193c3e5a84SMatthias Ringwald     // - if un-encrypted connections are not supported, e.g. when connecting to own device, this enforces authentication
1203c3e5a84SMatthias Ringwald     // gatt_client_set_required_security_level(LEVEL_2);
1213c3e5a84SMatthias Ringwald 
122d7ee901aSMatthias Ringwald     /**
123d7ee901aSMatthias Ringwald      * Choose ONE of the following configurations
1243cdbe9dbSMatthias Ringwald      * Bonding is disabled to allow for repeated testing. It can be enabled by or'ing
1253cdbe9dbSMatthias Ringwald      * SM_AUTHREQ_BONDING to the authentication requirements like this:
1263cdbe9dbSMatthias Ringwald      * sm_set_authentication_requirements( X | SM_AUTHREQ_BONDING)
127d7ee901aSMatthias Ringwald      */
128d7ee901aSMatthias Ringwald 
129d7ee901aSMatthias Ringwald     // LE Legacy Pairing, Just Works
1304b8c611fSMatthias Ringwald     // sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
1313cdbe9dbSMatthias Ringwald     // sm_set_authentication_requirements(0);
132d7ee901aSMatthias Ringwald 
133d7ee901aSMatthias Ringwald     // LE Legacy Pairing, Passkey entry initiator enter, responder (us) displays
134d7ee901aSMatthias Ringwald     // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
135d7ee901aSMatthias Ringwald     // sm_set_authentication_requirements(SM_AUTHREQ_MITM_PROTECTION);
1364b8c611fSMatthias Ringwald     // sm_use_fixed_passkey_in_display_role(123456);
137d7ee901aSMatthias Ringwald 
138d7ee901aSMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS
1393cdbe9dbSMatthias Ringwald 
1403cdbe9dbSMatthias Ringwald     // enable LE Secure Connections Only mode - disables Legacy pairing
14132112218SMatthias Ringwald     // sm_set_secure_connections_only_mode(true);
1423cdbe9dbSMatthias Ringwald 
1433cdbe9dbSMatthias Ringwald     // LE Secure Connections, Just Works
14499c44ab2SMatthias Ringwald     // sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
145d7ee901aSMatthias Ringwald     // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION);
146d7ee901aSMatthias Ringwald 
147d7ee901aSMatthias Ringwald     // LE Secure Connections, Numeric Comparison
14899c44ab2SMatthias Ringwald     // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO);
14999c44ab2SMatthias Ringwald     // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION);
150d7ee901aSMatthias Ringwald 
15132112218SMatthias Ringwald     // LE Secure Pairing, Passkey entry initiator enter, responder (us) displays
152d7ee901aSMatthias Ringwald     // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
153d7ee901aSMatthias Ringwald     // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION);
1544b8c611fSMatthias Ringwald     // sm_use_fixed_passkey_in_display_role(123456);
15532112218SMatthias Ringwald 
15632112218SMatthias Ringwald     // LE Secure Pairing, Passkey entry initiator displays, responder (us) enter
15732112218SMatthias Ringwald     // sm_set_io_capabilities(IO_CAPABILITY_KEYBOARD_ONLY);
15832112218SMatthias Ringwald     // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION);
159d7ee901aSMatthias Ringwald #endif
160d7ee901aSMatthias Ringwald }
161d7ee901aSMatthias Ringwald 
162d7ee901aSMatthias Ringwald /* LISTING_END */
163d7ee901aSMatthias Ringwald 
164d7ee901aSMatthias Ringwald /*
165*8f19f013SMatthias Ringwald  * @section Security Manager Packet Handler
166d7ee901aSMatthias Ringwald  *
167*8f19f013SMatthias Ringwald  * @text The packet handler is used to handle Security Manager events
168d7ee901aSMatthias Ringwald  */
169d7ee901aSMatthias Ringwald 
170*8f19f013SMatthias Ringwald /* LISTING_START(packetHandler): Security Manager Packet Handler */
sm_packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)171*8f19f013SMatthias Ringwald static void sm_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1729ec2630cSMatthias Ringwald     UNUSED(channel);
1739ec2630cSMatthias Ringwald     UNUSED(size);
174414eb680SMatthias Ringwald 
1757bbeb3adSMilanka Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
1767bbeb3adSMilanka Ringwald 
177a4fe6467SMatthias Ringwald     hci_con_handle_t con_handle;
1789c22b849SMatthias Ringwald     bd_addr_t addr;
179bd37f2bfSMatthias Ringwald     bd_addr_type_t addr_type;
1803c3e5a84SMatthias Ringwald     uint8_t status;
181414eb680SMatthias Ringwald 
182d7ee901aSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
183bba48196SMatthias Ringwald         case HCI_EVENT_META_GAP:
184bba48196SMatthias Ringwald             switch (hci_event_gap_meta_get_subevent_code(packet)) {
185bba48196SMatthias Ringwald                 case GAP_SUBEVENT_LE_CONNECTION_COMPLETE:
18673704aa9SMatthias Ringwald                     printf("Connection complete\n");
187bba48196SMatthias Ringwald                     con_handle = gap_subevent_le_connection_complete_get_connection_handle(packet);
188414eb680SMatthias Ringwald                     UNUSED(con_handle);
1893c3e5a84SMatthias Ringwald 
1903c3e5a84SMatthias Ringwald                     // for testing, choose one of the following actions
1913c3e5a84SMatthias Ringwald 
1923c3e5a84SMatthias Ringwald                     // manually start pairing
193e8bde1e1SMatthias Ringwald                     // sm_request_pairing(con_handle);
1943c3e5a84SMatthias Ringwald 
1953c3e5a84SMatthias Ringwald                     // gatt client request to authenticated characteristic in sm_pairing_central (short cut, uses hard-coded value handle)
1963c3e5a84SMatthias Ringwald                     // gatt_client_read_value_of_characteristic_using_value_handle(&packet_handler, con_handle, 0x0009);
1973c3e5a84SMatthias Ringwald 
1983c3e5a84SMatthias Ringwald                     // general gatt client request to trigger mandatory authentication
1993c3e5a84SMatthias Ringwald                     // gatt_client_discover_primary_services(&packet_handler, con_handle);
200a4fe6467SMatthias Ringwald                     break;
201a4fe6467SMatthias Ringwald                 default:
202a4fe6467SMatthias Ringwald                     break;
203a4fe6467SMatthias Ringwald             }
204a4fe6467SMatthias Ringwald             break;
205d7ee901aSMatthias Ringwald         case SM_EVENT_JUST_WORKS_REQUEST:
206d7ee901aSMatthias Ringwald             printf("Just Works requested\n");
207d7ee901aSMatthias Ringwald             sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
208d7ee901aSMatthias Ringwald             break;
209d7ee901aSMatthias Ringwald         case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
210bace42efSMatthias Ringwald             printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet));
211d7ee901aSMatthias Ringwald             sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet));
212d7ee901aSMatthias Ringwald             break;
213d7ee901aSMatthias Ringwald         case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
214bace42efSMatthias Ringwald             printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet));
215d7ee901aSMatthias Ringwald             break;
2169c22b849SMatthias Ringwald         case SM_EVENT_IDENTITY_CREATED:
2179c22b849SMatthias Ringwald             sm_event_identity_created_get_identity_address(packet, addr);
2189c22b849SMatthias Ringwald             printf("Identity created: type %u address %s\n", sm_event_identity_created_get_identity_addr_type(packet), bd_addr_to_str(addr));
2199c22b849SMatthias Ringwald             break;
2209c22b849SMatthias Ringwald         case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
2219c22b849SMatthias Ringwald             sm_event_identity_resolving_succeeded_get_identity_address(packet, addr);
2229c22b849SMatthias Ringwald             printf("Identity resolved: type %u address %s\n", sm_event_identity_resolving_succeeded_get_identity_addr_type(packet), bd_addr_to_str(addr));
2239c22b849SMatthias Ringwald             break;
2249c22b849SMatthias Ringwald         case SM_EVENT_IDENTITY_RESOLVING_FAILED:
2259c22b849SMatthias Ringwald             sm_event_identity_created_get_address(packet, addr);
2269c22b849SMatthias Ringwald             printf("Identity resolving failed\n");
2279c22b849SMatthias Ringwald             break;
2287c4db9dcSMatthias Ringwald         case SM_EVENT_PAIRING_STARTED:
2297c4db9dcSMatthias Ringwald             printf("Pairing started\n");
2307c4db9dcSMatthias Ringwald             break;
2310614d679SMatthias Ringwald         case SM_EVENT_PAIRING_COMPLETE:
2320614d679SMatthias Ringwald             switch (sm_event_pairing_complete_get_status(packet)){
2330614d679SMatthias Ringwald                 case ERROR_CODE_SUCCESS:
2340614d679SMatthias Ringwald                     printf("Pairing complete, success\n");
2350614d679SMatthias Ringwald                     break;
2360614d679SMatthias Ringwald                 case ERROR_CODE_CONNECTION_TIMEOUT:
2370614d679SMatthias Ringwald                     printf("Pairing failed, timeout\n");
2380614d679SMatthias Ringwald                     break;
2390614d679SMatthias Ringwald                 case ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION:
2403cdbe9dbSMatthias Ringwald                     printf("Pairing failed, disconnected\n");
2410614d679SMatthias Ringwald                     break;
2420614d679SMatthias Ringwald                 case ERROR_CODE_AUTHENTICATION_FAILURE:
24339620ea5SMatthias Ringwald                     printf("Pairing failed, authentication failure with reason = %u\n", sm_event_pairing_complete_get_reason(packet));
2440614d679SMatthias Ringwald                     break;
2450614d679SMatthias Ringwald                 default:
2460614d679SMatthias Ringwald                     break;
2470614d679SMatthias Ringwald             }
2480614d679SMatthias Ringwald             break;
2497e65711bSMatthias Ringwald         case SM_EVENT_REENCRYPTION_STARTED:
2507e65711bSMatthias Ringwald             sm_event_reencryption_complete_get_address(packet, addr);
2517e65711bSMatthias Ringwald             printf("Bonding information exists for addr type %u, identity addr %s -> re-encryption started\n",
2527e65711bSMatthias Ringwald                    sm_event_reencryption_started_get_addr_type(packet), bd_addr_to_str(addr));
2537e65711bSMatthias Ringwald             break;
2547e65711bSMatthias Ringwald         case SM_EVENT_REENCRYPTION_COMPLETE:
2557e65711bSMatthias Ringwald             switch (sm_event_reencryption_complete_get_status(packet)){
2567e65711bSMatthias Ringwald                 case ERROR_CODE_SUCCESS:
2577e65711bSMatthias Ringwald                     printf("Re-encryption complete, success\n");
2587e65711bSMatthias Ringwald                     break;
2597e65711bSMatthias Ringwald                 case ERROR_CODE_CONNECTION_TIMEOUT:
2607e65711bSMatthias Ringwald                     printf("Re-encryption failed, timeout\n");
2617e65711bSMatthias Ringwald                     break;
2627e65711bSMatthias Ringwald                 case ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION:
2637e65711bSMatthias Ringwald                     printf("Re-encryption failed, disconnected\n");
2647e65711bSMatthias Ringwald                     break;
265bd37f2bfSMatthias Ringwald                 case ERROR_CODE_PIN_OR_KEY_MISSING:
266bd37f2bfSMatthias Ringwald                     printf("Re-encryption failed, bonding information missing\n\n");
267bd37f2bfSMatthias Ringwald                     printf("Assuming remote lost bonding information\n");
268bd37f2bfSMatthias Ringwald                     printf("Deleting local bonding information to allow for new pairing...\n");
269bd37f2bfSMatthias Ringwald                     sm_event_reencryption_complete_get_address(packet, addr);
270bd37f2bfSMatthias Ringwald                     addr_type = sm_event_reencryption_started_get_addr_type(packet);
271bd37f2bfSMatthias Ringwald                     gap_delete_bonding(addr_type, addr);
2727e65711bSMatthias Ringwald                     break;
2737e65711bSMatthias Ringwald                 default:
2747e65711bSMatthias Ringwald                     break;
2757e65711bSMatthias Ringwald             }
2767e65711bSMatthias Ringwald             break;
2773c3e5a84SMatthias Ringwald         case GATT_EVENT_QUERY_COMPLETE:
2783c3e5a84SMatthias Ringwald             status = gatt_event_query_complete_get_att_status(packet);
2793c3e5a84SMatthias Ringwald             switch (status){
2803c3e5a84SMatthias Ringwald                 case ATT_ERROR_INSUFFICIENT_ENCRYPTION:
281fcd55a0bSMilanka Ringwald                     printf("GATT Query failed, Insufficient Encryption\n");
2823c3e5a84SMatthias Ringwald                     break;
2833c3e5a84SMatthias Ringwald                 case ATT_ERROR_INSUFFICIENT_AUTHENTICATION:
284fcd55a0bSMilanka Ringwald                     printf("GATT Query failed, Insufficient Authentication\n");
2853c3e5a84SMatthias Ringwald                     break;
2863c3e5a84SMatthias Ringwald                 case ATT_ERROR_BONDING_INFORMATION_MISSING:
287fcd55a0bSMilanka Ringwald                     printf("GATT Query failed, Bonding Information Missing\n");
2883c3e5a84SMatthias Ringwald                     break;
2893c3e5a84SMatthias Ringwald                 case ATT_ERROR_SUCCESS:
290fcd55a0bSMilanka Ringwald                     printf("GATT Query successful\n");
2913c3e5a84SMatthias Ringwald                     break;
2923c3e5a84SMatthias Ringwald                 default:
293fcd55a0bSMilanka Ringwald                     printf("GATT Query failed, status 0x%02x\n", gatt_event_query_complete_get_att_status(packet));
2943c3e5a84SMatthias Ringwald                     break;
2953c3e5a84SMatthias Ringwald             }
2963c3e5a84SMatthias Ringwald             break;
2970614d679SMatthias Ringwald         default:
2980614d679SMatthias Ringwald             break;
299d7ee901aSMatthias Ringwald     }
300d7ee901aSMatthias Ringwald }
301*8f19f013SMatthias Ringwald 
302*8f19f013SMatthias Ringwald /*
303*8f19f013SMatthias Ringwald  * @section HCI Packet Handler
304*8f19f013SMatthias Ringwald  *
305*8f19f013SMatthias Ringwald  * @text The packet handler is used to handle new connections, can trigger Security Request
306*8f19f013SMatthias Ringwald  */
hci_packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)307*8f19f013SMatthias Ringwald static void hci_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
308*8f19f013SMatthias Ringwald     UNUSED(channel);
309*8f19f013SMatthias Ringwald     UNUSED(size);
310*8f19f013SMatthias Ringwald 
311*8f19f013SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
312*8f19f013SMatthias Ringwald 
313*8f19f013SMatthias Ringwald     hci_con_handle_t con_handle;
314*8f19f013SMatthias Ringwald 
315*8f19f013SMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
316*8f19f013SMatthias Ringwald         case HCI_EVENT_META_GAP:
317*8f19f013SMatthias Ringwald             switch (hci_event_gap_meta_get_subevent_code(packet)) {
318*8f19f013SMatthias Ringwald                 case GAP_SUBEVENT_LE_CONNECTION_COMPLETE:
319*8f19f013SMatthias Ringwald                     printf("Connection complete\n");
320*8f19f013SMatthias Ringwald                     con_handle = gap_subevent_le_connection_complete_get_connection_handle(packet);
321*8f19f013SMatthias Ringwald                     UNUSED(con_handle);
322*8f19f013SMatthias Ringwald 
323*8f19f013SMatthias Ringwald                     // for testing, choose one of the following actions
324*8f19f013SMatthias Ringwald 
325*8f19f013SMatthias Ringwald                     // manually start pairing
326*8f19f013SMatthias Ringwald                     // sm_request_pairing(con_handle);
327*8f19f013SMatthias Ringwald 
328*8f19f013SMatthias Ringwald                     // gatt client request to authenticated characteristic in sm_pairing_central (short cut, uses hard-coded value handle)
329*8f19f013SMatthias Ringwald                     // gatt_client_read_value_of_characteristic_using_value_handle(&packet_handler, con_handle, 0x0009);
330*8f19f013SMatthias Ringwald 
331*8f19f013SMatthias Ringwald                     // general gatt client request to trigger mandatory authentication
332*8f19f013SMatthias Ringwald                     // gatt_client_discover_primary_services(&packet_handler, con_handle);
333*8f19f013SMatthias Ringwald                     break;
334*8f19f013SMatthias Ringwald                 default:
335*8f19f013SMatthias Ringwald                     break;
336*8f19f013SMatthias Ringwald             }
337*8f19f013SMatthias Ringwald             break;
338*8f19f013SMatthias Ringwald         default:
339*8f19f013SMatthias Ringwald             break;
340*8f19f013SMatthias Ringwald     }
341*8f19f013SMatthias Ringwald }
342*8f19f013SMatthias Ringwald 
343d7ee901aSMatthias Ringwald /* LISTING_END */
344d7ee901aSMatthias Ringwald 
345d7ee901aSMatthias Ringwald int btstack_main(void);
btstack_main(void)346d7ee901aSMatthias Ringwald int btstack_main(void)
347d7ee901aSMatthias Ringwald {
348d7ee901aSMatthias Ringwald     sm_peripheral_setup();
349d7ee901aSMatthias Ringwald 
350d7ee901aSMatthias Ringwald     // turn on!
351d7ee901aSMatthias Ringwald 	hci_power_control(HCI_POWER_ON);
352d7ee901aSMatthias Ringwald 
353d7ee901aSMatthias Ringwald     return 0;
354d7ee901aSMatthias Ringwald }
355d7ee901aSMatthias Ringwald /* EXAMPLE_END */
356