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