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 237c485f8bSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 247c485f8bSMatthias Ringwald * RINGWALD 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 38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "sm_pairing_central.c" 397c485f8bSMatthias Ringwald 407c485f8bSMatthias Ringwald 417c485f8bSMatthias Ringwald // ***************************************************************************** 427c485f8bSMatthias Ringwald /* EXAMPLE_START(sm_pairing_central): LE Peripheral - Test pairing combinations 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" 603c90b2d9SMatthias Ringwald #include "sm_pairing_central.h" 617c485f8bSMatthias Ringwald 627c485f8bSMatthias Ringwald 637c485f8bSMatthias Ringwald // We're looking for a remote device that lists this service in the advertisement 647c485f8bSMatthias Ringwald // LightBlue assigns 0x1111 as the UUID for a Blank service. 657c485f8bSMatthias Ringwald #define REMOTE_SERVICE 0x1111 667c485f8bSMatthias Ringwald 67*98c0024dSMatthias Ringwald // Fixed passkey - used with sm_pairing_peripheral. Passkey is random in general 68*98c0024dSMatthias Ringwald #define FIXED_PASSKEY 12346 69*98c0024dSMatthias Ringwald 707c485f8bSMatthias Ringwald 717c485f8bSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 727c485f8bSMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration; 737c485f8bSMatthias Ringwald 747c485f8bSMatthias Ringwald /* @section GAP LE setup for receiving advertisements 757c485f8bSMatthias Ringwald * 767c485f8bSMatthias Ringwald * @text GAP LE advertisements are received as custom HCI events of the 777c485f8bSMatthias Ringwald * GAP_EVENT_ADVERTISING_REPORT type. To receive them, you'll need to register 787c485f8bSMatthias Ringwald * the HCI packet handler, as shown in Listing GAPLEAdvSetup. 797c485f8bSMatthias Ringwald */ 807c485f8bSMatthias Ringwald 817c485f8bSMatthias Ringwald /* LISTING_START(GAPLEAdvSetup): Setting up GAP LE client for receiving advertisements */ 82*98c0024dSMatthias Ringwald static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 83*98c0024dSMatthias Ringwald static void sm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 847c485f8bSMatthias Ringwald 857c485f8bSMatthias Ringwald static void sm_pairing_central_setup(void){ 867c485f8bSMatthias Ringwald l2cap_init(); 877c485f8bSMatthias Ringwald 887c485f8bSMatthias Ringwald // setup le device db 897c485f8bSMatthias Ringwald le_device_db_init(); 907c485f8bSMatthias Ringwald 917c485f8bSMatthias Ringwald // setup SM: Display only 927c485f8bSMatthias Ringwald sm_init(); 937c485f8bSMatthias Ringwald 94e5779dd2SMatthias Ringwald // setup ATT server 95e5779dd2SMatthias Ringwald att_server_init(profile_data, NULL, NULL); 96e5779dd2SMatthias Ringwald 977c485f8bSMatthias Ringwald /** 987c485f8bSMatthias Ringwald * Choose ONE of the following configurations 99*98c0024dSMatthias Ringwald * Bonding is disabled to allow for repeated testing. It can be enabled with SM_AUTHREQ_BONDING 1007c485f8bSMatthias Ringwald */ 1017c485f8bSMatthias Ringwald 102a4fe6467SMatthias Ringwald // register handler 103*98c0024dSMatthias Ringwald hci_event_callback_registration.callback = &hci_packet_handler; 104a4fe6467SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 105a4fe6467SMatthias Ringwald 106*98c0024dSMatthias Ringwald sm_event_callback_registration.callback = &sm_packet_handler; 107a4fe6467SMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 108a4fe6467SMatthias Ringwald 1097c485f8bSMatthias Ringwald // LE Legacy Pairing, Just Works 110*98c0024dSMatthias Ringwald // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO); 111*98c0024dSMatthias Ringwald // sm_set_authentication_requirements(SM_AUTHREQ_ NO_BONDING); 1127c485f8bSMatthias Ringwald 1137c485f8bSMatthias Ringwald // LE Legacy Pairing, Passkey entry initiator enter, responder (us) displays 1147c485f8bSMatthias Ringwald // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY); 1157c485f8bSMatthias Ringwald // sm_set_authentication_requirements(SM_AUTHREQ_MITM_PROTECTION); 116*98c0024dSMatthias Ringwald // sm_use_fixed_passkey_in_display_role(FIXED_PASSKEY); 1177c485f8bSMatthias Ringwald 1187c485f8bSMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 1197c485f8bSMatthias Ringwald // LE Secure Connetions, Just Works 1207c485f8bSMatthias Ringwald // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO); 1217c485f8bSMatthias Ringwald // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION); 1227c485f8bSMatthias Ringwald 1237c485f8bSMatthias Ringwald // LE Secure Connections, Numeric Comparison 124*98c0024dSMatthias Ringwald // sm_set_io_capabilities(IO_CAPABILITY_KEYBOARD_ONLY); 125*98c0024dSMatthias Ringwald sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO); 126*98c0024dSMatthias Ringwald sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION); 1277c485f8bSMatthias Ringwald 1287c485f8bSMatthias Ringwald // LE Legacy Pairing, Passkey entry initiator enter, responder (us) displays 1297c485f8bSMatthias Ringwald // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY); 1307c485f8bSMatthias Ringwald // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION); 131*98c0024dSMatthias Ringwald // sm_use_fixed_passkey_in_display_role(FIXED_PASSKEY); 1327c485f8bSMatthias Ringwald #endif 1337c485f8bSMatthias Ringwald } 1347c485f8bSMatthias Ringwald 1357c485f8bSMatthias Ringwald /* LISTING_END */ 1367c485f8bSMatthias Ringwald 1377c485f8bSMatthias Ringwald /* @section HCI packet handler 1387c485f8bSMatthias Ringwald * 1397c485f8bSMatthias Ringwald * @text The HCI packet handler has to start the scanning, 1407c485f8bSMatthias Ringwald * and to handle received advertisements. Advertisements are received 1417c485f8bSMatthias Ringwald * as HCI event packets of the GAP_EVENT_ADVERTISING_REPORT type, 142*98c0024dSMatthias Ringwald * see Listing HCIPacketHandler. 1437c485f8bSMatthias Ringwald */ 1447c485f8bSMatthias Ringwald 145*98c0024dSMatthias Ringwald /* LISTING_START(HCIPacketHandler): Scanning and receiving advertisements */ 1467c485f8bSMatthias Ringwald 147*98c0024dSMatthias Ringwald static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1489ec2630cSMatthias Ringwald UNUSED(channel); 1499ec2630cSMatthias Ringwald UNUSED(size); 1509ec2630cSMatthias Ringwald 1517c485f8bSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 1527c485f8bSMatthias Ringwald hci_con_handle_t con_handle; 1537c485f8bSMatthias Ringwald 1547c485f8bSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 1557c485f8bSMatthias Ringwald case BTSTACK_EVENT_STATE: 1567c485f8bSMatthias Ringwald // BTstack activated, get started 1577c485f8bSMatthias Ringwald if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 1587c485f8bSMatthias Ringwald printf("Start scaning!\n"); 1597c485f8bSMatthias Ringwald gap_set_scan_parameters(1,0x0030, 0x0030); 1607c485f8bSMatthias Ringwald gap_start_scan(); 1617c485f8bSMatthias Ringwald } 1627c485f8bSMatthias Ringwald break; 1637c485f8bSMatthias Ringwald case GAP_EVENT_ADVERTISING_REPORT:{ 1647c485f8bSMatthias Ringwald bd_addr_t address; 1657c485f8bSMatthias Ringwald gap_event_advertising_report_get_address(packet, address); 1667c485f8bSMatthias Ringwald uint8_t address_type = gap_event_advertising_report_get_address_type(packet); 1677c485f8bSMatthias Ringwald uint8_t length = gap_event_advertising_report_get_data_length(packet); 1687c485f8bSMatthias Ringwald const uint8_t * data = gap_event_advertising_report_get_data(packet); 169*98c0024dSMatthias Ringwald // printf("Advertisement event: addr-type %u, addr %s, data[%u] ", 170*98c0024dSMatthias Ringwald // address_type, bd_addr_to_str(address), length); 171*98c0024dSMatthias Ringwald // printf_hexdump(data, length); 1727c485f8bSMatthias Ringwald if (!ad_data_contains_uuid16(length, (uint8_t *) data, REMOTE_SERVICE)) break; 1737c485f8bSMatthias Ringwald printf("Found remote with UUID %04x, connecting...\n", REMOTE_SERVICE); 1747c485f8bSMatthias Ringwald gap_stop_scan(); 1757c485f8bSMatthias Ringwald gap_connect(address,address_type); 1767c485f8bSMatthias Ringwald break; 1777c485f8bSMatthias Ringwald } 178*98c0024dSMatthias Ringwald case HCI_EVENT_LE_META: 179*98c0024dSMatthias Ringwald // wait for connection complete 180*98c0024dSMatthias Ringwald if (hci_event_le_meta_get_subevent_code(packet) != HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break; 181*98c0024dSMatthias Ringwald con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 182*98c0024dSMatthias Ringwald printf("Connection complete\n"); 183*98c0024dSMatthias Ringwald // start pairing 184*98c0024dSMatthias Ringwald sm_request_pairing(con_handle); 185*98c0024dSMatthias Ringwald break; 186*98c0024dSMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE: 187*98c0024dSMatthias Ringwald con_handle = hci_event_encryption_change_get_connection_handle(packet); 188*98c0024dSMatthias Ringwald printf("Connection encrypted: %u\n", hci_event_encryption_change_get_encryption_enabled(packet)); 189*98c0024dSMatthias Ringwald break; 190*98c0024dSMatthias Ringwald default: 191*98c0024dSMatthias Ringwald break; 192*98c0024dSMatthias Ringwald } 193*98c0024dSMatthias Ringwald } 194*98c0024dSMatthias Ringwald 195*98c0024dSMatthias Ringwald /* @section HCI packet handler 196*98c0024dSMatthias Ringwald * 197*98c0024dSMatthias Ringwald * @text The SM packet handler receives Security Manager Events required for pairing. 198*98c0024dSMatthias Ringwald * It also receives events generated during Identity Resolving 199*98c0024dSMatthias Ringwald * see Listing SMPacketHandler. 200*98c0024dSMatthias Ringwald */ 201*98c0024dSMatthias Ringwald 202*98c0024dSMatthias Ringwald /* LISTING_START(SMPacketHandler): Scanning and receiving advertisements */ 203*98c0024dSMatthias Ringwald 204*98c0024dSMatthias Ringwald static void sm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 205*98c0024dSMatthias Ringwald UNUSED(channel); 206*98c0024dSMatthias Ringwald UNUSED(size); 207*98c0024dSMatthias Ringwald 208*98c0024dSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 209*98c0024dSMatthias Ringwald 210*98c0024dSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 2117c485f8bSMatthias Ringwald case SM_EVENT_JUST_WORKS_REQUEST: 2127c485f8bSMatthias Ringwald printf("Just works requested\n"); 2137c485f8bSMatthias Ringwald sm_just_works_confirm(sm_event_just_works_request_get_handle(packet)); 2147c485f8bSMatthias Ringwald break; 2157c485f8bSMatthias Ringwald case SM_EVENT_NUMERIC_COMPARISON_REQUEST: 216bace42efSMatthias Ringwald printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet)); 2177c485f8bSMatthias Ringwald sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet)); 2187c485f8bSMatthias Ringwald break; 2197c485f8bSMatthias Ringwald case SM_EVENT_PASSKEY_DISPLAY_NUMBER: 220bace42efSMatthias Ringwald printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet)); 2217c485f8bSMatthias Ringwald break; 222*98c0024dSMatthias Ringwald case SM_EVENT_PASSKEY_INPUT_NUMBER: 223*98c0024dSMatthias Ringwald printf("Passkey Input requested\n"); 224*98c0024dSMatthias Ringwald printf("Sending fixed passkey %"PRIu32"\n", FIXED_PASSKEY); 225*98c0024dSMatthias Ringwald sm_passkey_input(sm_event_passkey_input_number_get_handle(packet), FIXED_PASSKEY); 226*98c0024dSMatthias Ringwald break; 227eee18978SMatthias Ringwald case SM_EVENT_PAIRING_COMPLETE: 228eee18978SMatthias Ringwald switch (sm_event_pairing_complete_get_status(packet)){ 229eee18978SMatthias Ringwald case ERROR_CODE_SUCCESS: 230eee18978SMatthias Ringwald printf("Pairing complete, success\n"); 231eee18978SMatthias Ringwald break; 232eee18978SMatthias Ringwald case ERROR_CODE_CONNECTION_TIMEOUT: 233eee18978SMatthias Ringwald printf("Pairing failed, timeout\n"); 234eee18978SMatthias Ringwald break; 235eee18978SMatthias Ringwald case ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION: 236eee18978SMatthias Ringwald printf("Pairing faileed, disconnected\n"); 237eee18978SMatthias Ringwald break; 238eee18978SMatthias Ringwald case ERROR_CODE_AUTHENTICATION_FAILURE: 239eee18978SMatthias Ringwald printf("Pairing failed, reason = %u\n", sm_event_pairing_complete_get_reason(packet)); 240eee18978SMatthias Ringwald break; 241eee18978SMatthias Ringwald default: 242eee18978SMatthias Ringwald break; 243eee18978SMatthias Ringwald } 244eee18978SMatthias Ringwald break; 2457c485f8bSMatthias Ringwald default: 2467c485f8bSMatthias Ringwald break; 2477c485f8bSMatthias Ringwald } 2487c485f8bSMatthias Ringwald } 2497c485f8bSMatthias Ringwald /* LISTING_END */ 2507c485f8bSMatthias Ringwald 2517c485f8bSMatthias Ringwald int btstack_main(void); 2527c485f8bSMatthias Ringwald int btstack_main(void) 2537c485f8bSMatthias Ringwald { 2547c485f8bSMatthias Ringwald sm_pairing_central_setup(); 2557c485f8bSMatthias Ringwald 2567c485f8bSMatthias Ringwald // turn on! 2577c485f8bSMatthias Ringwald hci_power_control(HCI_POWER_ON); 2587c485f8bSMatthias Ringwald 2597c485f8bSMatthias Ringwald return 0; 2607c485f8bSMatthias Ringwald } 2617c485f8bSMatthias Ringwald 2627c485f8bSMatthias Ringwald /* EXAMPLE_END */ 263