1*7c485f8bSMatthias Ringwald /* 2*7c485f8bSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3*7c485f8bSMatthias Ringwald * 4*7c485f8bSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5*7c485f8bSMatthias Ringwald * modification, are permitted provided that the following conditions 6*7c485f8bSMatthias Ringwald * are met: 7*7c485f8bSMatthias Ringwald * 8*7c485f8bSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9*7c485f8bSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10*7c485f8bSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11*7c485f8bSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12*7c485f8bSMatthias Ringwald * documentation and/or other materials provided with the distribution. 13*7c485f8bSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14*7c485f8bSMatthias Ringwald * contributors may be used to endorse or promote products derived 15*7c485f8bSMatthias Ringwald * from this software without specific prior written permission. 16*7c485f8bSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17*7c485f8bSMatthias Ringwald * personal benefit and not for any commercial purpose or for 18*7c485f8bSMatthias Ringwald * monetary gain. 19*7c485f8bSMatthias Ringwald * 20*7c485f8bSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21*7c485f8bSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22*7c485f8bSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23*7c485f8bSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24*7c485f8bSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25*7c485f8bSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26*7c485f8bSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27*7c485f8bSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28*7c485f8bSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29*7c485f8bSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30*7c485f8bSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*7c485f8bSMatthias Ringwald * SUCH DAMAGE. 32*7c485f8bSMatthias Ringwald * 33*7c485f8bSMatthias Ringwald * Please inquire about commercial licensing options at 34*7c485f8bSMatthias Ringwald * [email protected] 35*7c485f8bSMatthias Ringwald * 36*7c485f8bSMatthias Ringwald */ 37*7c485f8bSMatthias Ringwald 38*7c485f8bSMatthias Ringwald 39*7c485f8bSMatthias Ringwald // ***************************************************************************** 40*7c485f8bSMatthias Ringwald /* EXAMPLE_START(sm_pairing_central): LE Peripheral - Test pairing combinations 41*7c485f8bSMatthias Ringwald * 42*7c485f8bSMatthias Ringwald * @text Depending on the Authentication requiremens and IO Capabilities, 43*7c485f8bSMatthias Ringwald * the pairing process uses different short and long term key generation method. 44*7c485f8bSMatthias Ringwald * This example helps explore the different options incl. LE Secure Connections. 45*7c485f8bSMatthias Ringwald * It scans for advertisements and connects to the first device that lists a 46*7c485f8bSMatthias Ringwald * random service. 47*7c485f8bSMatthias Ringwald */ 48*7c485f8bSMatthias Ringwald // ***************************************************************************** 49*7c485f8bSMatthias Ringwald 50*7c485f8bSMatthias Ringwald 51*7c485f8bSMatthias Ringwald #include <stdint.h> 52*7c485f8bSMatthias Ringwald #include <inttypes.h> 53*7c485f8bSMatthias Ringwald #include <stdio.h> 54*7c485f8bSMatthias Ringwald #include <stdlib.h> 55*7c485f8bSMatthias Ringwald #include <string.h> 56*7c485f8bSMatthias Ringwald 57*7c485f8bSMatthias Ringwald #include "btstack.h" 58*7c485f8bSMatthias Ringwald 59*7c485f8bSMatthias Ringwald 60*7c485f8bSMatthias Ringwald // We're looking for a remote device that lists this service in the advertisement 61*7c485f8bSMatthias Ringwald // LightBlue assigns 0x1111 as the UUID for a Blank service. 62*7c485f8bSMatthias Ringwald #define REMOTE_SERVICE 0x1111 63*7c485f8bSMatthias Ringwald 64*7c485f8bSMatthias Ringwald 65*7c485f8bSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 66*7c485f8bSMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration; 67*7c485f8bSMatthias Ringwald 68*7c485f8bSMatthias Ringwald /* @section GAP LE setup for receiving advertisements 69*7c485f8bSMatthias Ringwald * 70*7c485f8bSMatthias Ringwald * @text GAP LE advertisements are received as custom HCI events of the 71*7c485f8bSMatthias Ringwald * GAP_EVENT_ADVERTISING_REPORT type. To receive them, you'll need to register 72*7c485f8bSMatthias Ringwald * the HCI packet handler, as shown in Listing GAPLEAdvSetup. 73*7c485f8bSMatthias Ringwald */ 74*7c485f8bSMatthias Ringwald 75*7c485f8bSMatthias Ringwald /* LISTING_START(GAPLEAdvSetup): Setting up GAP LE client for receiving advertisements */ 76*7c485f8bSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 77*7c485f8bSMatthias Ringwald 78*7c485f8bSMatthias Ringwald static void sm_pairing_central_setup(void){ 79*7c485f8bSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 80*7c485f8bSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 81*7c485f8bSMatthias Ringwald 82*7c485f8bSMatthias Ringwald l2cap_init(); 83*7c485f8bSMatthias Ringwald 84*7c485f8bSMatthias Ringwald // setup le device db 85*7c485f8bSMatthias Ringwald le_device_db_init(); 86*7c485f8bSMatthias Ringwald 87*7c485f8bSMatthias Ringwald // setup SM: Display only 88*7c485f8bSMatthias Ringwald sm_init(); 89*7c485f8bSMatthias Ringwald sm_event_callback_registration.callback = &packet_handler; 90*7c485f8bSMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 91*7c485f8bSMatthias Ringwald 92*7c485f8bSMatthias Ringwald /** 93*7c485f8bSMatthias Ringwald * Choose ONE of the following configurations 94*7c485f8bSMatthias Ringwald */ 95*7c485f8bSMatthias Ringwald 96*7c485f8bSMatthias Ringwald // LE Legacy Pairing, Just Works 97*7c485f8bSMatthias Ringwald sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO); 98*7c485f8bSMatthias Ringwald sm_set_authentication_requirements(0); 99*7c485f8bSMatthias Ringwald 100*7c485f8bSMatthias Ringwald // LE Legacy Pairing, Passkey entry initiator enter, responder (us) displays 101*7c485f8bSMatthias Ringwald // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY); 102*7c485f8bSMatthias Ringwald // sm_set_authentication_requirements(SM_AUTHREQ_MITM_PROTECTION); 103*7c485f8bSMatthias Ringwald 104*7c485f8bSMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 105*7c485f8bSMatthias Ringwald // LE Secure Connetions, Just Works 106*7c485f8bSMatthias Ringwald // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO); 107*7c485f8bSMatthias Ringwald // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION); 108*7c485f8bSMatthias Ringwald 109*7c485f8bSMatthias Ringwald // LE Secure Connections, Numeric Comparison 110*7c485f8bSMatthias Ringwald // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO); 111*7c485f8bSMatthias Ringwald // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION); 112*7c485f8bSMatthias Ringwald 113*7c485f8bSMatthias Ringwald // LE Legacy Pairing, Passkey entry initiator enter, responder (us) displays 114*7c485f8bSMatthias Ringwald // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY); 115*7c485f8bSMatthias Ringwald // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION); 116*7c485f8bSMatthias Ringwald #endif 117*7c485f8bSMatthias Ringwald } 118*7c485f8bSMatthias Ringwald 119*7c485f8bSMatthias Ringwald /* LISTING_END */ 120*7c485f8bSMatthias Ringwald 121*7c485f8bSMatthias Ringwald /* @section HCI packet handler 122*7c485f8bSMatthias Ringwald * 123*7c485f8bSMatthias Ringwald * @text The HCI packet handler has to start the scanning, 124*7c485f8bSMatthias Ringwald * and to handle received advertisements. Advertisements are received 125*7c485f8bSMatthias Ringwald * as HCI event packets of the GAP_EVENT_ADVERTISING_REPORT type, 126*7c485f8bSMatthias Ringwald * see Listing GAPLEAdvPacketHandler. 127*7c485f8bSMatthias Ringwald */ 128*7c485f8bSMatthias Ringwald 129*7c485f8bSMatthias Ringwald /* LISTING_START(GAPLEAdvPacketHandler): Scanning and receiving advertisements */ 130*7c485f8bSMatthias Ringwald 131*7c485f8bSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 132*7c485f8bSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 133*7c485f8bSMatthias Ringwald hci_con_handle_t con_handle; 134*7c485f8bSMatthias Ringwald 135*7c485f8bSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 136*7c485f8bSMatthias Ringwald case BTSTACK_EVENT_STATE: 137*7c485f8bSMatthias Ringwald // BTstack activated, get started 138*7c485f8bSMatthias Ringwald if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 139*7c485f8bSMatthias Ringwald printf("Start scaning!\n"); 140*7c485f8bSMatthias Ringwald gap_set_scan_parameters(1,0x0030, 0x0030); 141*7c485f8bSMatthias Ringwald gap_start_scan(); 142*7c485f8bSMatthias Ringwald } 143*7c485f8bSMatthias Ringwald break; 144*7c485f8bSMatthias Ringwald case GAP_EVENT_ADVERTISING_REPORT:{ 145*7c485f8bSMatthias Ringwald bd_addr_t address; 146*7c485f8bSMatthias Ringwald gap_event_advertising_report_get_address(packet, address); 147*7c485f8bSMatthias Ringwald uint8_t address_type = gap_event_advertising_report_get_address_type(packet); 148*7c485f8bSMatthias Ringwald uint8_t length = gap_event_advertising_report_get_data_length(packet); 149*7c485f8bSMatthias Ringwald const uint8_t * data = gap_event_advertising_report_get_data(packet); 150*7c485f8bSMatthias Ringwald printf("Advertisement event: addr-type %u, addr %s, data[%u] ", 151*7c485f8bSMatthias Ringwald address_type, bd_addr_to_str(address), length); 152*7c485f8bSMatthias Ringwald printf_hexdump(data, length); 153*7c485f8bSMatthias Ringwald if (!ad_data_contains_uuid16(length, (uint8_t *) data, REMOTE_SERVICE)) break; 154*7c485f8bSMatthias Ringwald printf("Found remote with UUID %04x, connecting...\n", REMOTE_SERVICE); 155*7c485f8bSMatthias Ringwald gap_stop_scan(); 156*7c485f8bSMatthias Ringwald gap_connect(address,address_type); 157*7c485f8bSMatthias Ringwald break; 158*7c485f8bSMatthias Ringwald } 159*7c485f8bSMatthias Ringwald case SM_EVENT_JUST_WORKS_REQUEST: 160*7c485f8bSMatthias Ringwald printf("Just works requested\n"); 161*7c485f8bSMatthias Ringwald sm_just_works_confirm(sm_event_just_works_request_get_handle(packet)); 162*7c485f8bSMatthias Ringwald break; 163*7c485f8bSMatthias Ringwald case SM_EVENT_NUMERIC_COMPARISON_REQUEST: 164*7c485f8bSMatthias Ringwald printf("Confirming numeric comparison: %u\n", sm_event_numeric_comparison_request_get_passkey(packet)); 165*7c485f8bSMatthias Ringwald sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet)); 166*7c485f8bSMatthias Ringwald break; 167*7c485f8bSMatthias Ringwald case SM_EVENT_PASSKEY_DISPLAY_NUMBER: 168*7c485f8bSMatthias Ringwald printf("Display Passkey: %u\n", sm_event_passkey_display_number_get_passkey(packet)); 169*7c485f8bSMatthias Ringwald break; 170*7c485f8bSMatthias Ringwald 171*7c485f8bSMatthias Ringwald case HCI_EVENT_LE_META: 172*7c485f8bSMatthias Ringwald // wait for connection complete 173*7c485f8bSMatthias Ringwald if (hci_event_le_meta_get_subevent_code(packet) != HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break; 174*7c485f8bSMatthias Ringwald con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 175*7c485f8bSMatthias Ringwald // start pairing 176*7c485f8bSMatthias Ringwald sm_request_pairing(con_handle); 177*7c485f8bSMatthias Ringwald break; 178*7c485f8bSMatthias Ringwald default: 179*7c485f8bSMatthias Ringwald break; 180*7c485f8bSMatthias Ringwald } 181*7c485f8bSMatthias Ringwald } 182*7c485f8bSMatthias Ringwald /* LISTING_END */ 183*7c485f8bSMatthias Ringwald 184*7c485f8bSMatthias Ringwald int btstack_main(void); 185*7c485f8bSMatthias Ringwald int btstack_main(void) 186*7c485f8bSMatthias Ringwald { 187*7c485f8bSMatthias Ringwald sm_pairing_central_setup(); 188*7c485f8bSMatthias Ringwald 189*7c485f8bSMatthias Ringwald // turn on! 190*7c485f8bSMatthias Ringwald hci_power_control(HCI_POWER_ON); 191*7c485f8bSMatthias Ringwald 192*7c485f8bSMatthias Ringwald return 0; 193*7c485f8bSMatthias Ringwald } 194*7c485f8bSMatthias Ringwald 195*7c485f8bSMatthias Ringwald /* EXAMPLE_END */ 196