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 #define BTSTACK_FILE__ "sm_pairing_central.c" 39 40 41 // ***************************************************************************** 42 /* EXAMPLE_START(sm_pairing_central): LE Peripheral - Test pairing combinations 43 * 44 * @text Depending on the Authentication requiremens and IO Capabilities, 45 * the pairing process uses different short and long term key generation method. 46 * This example helps explore the different options incl. LE Secure Connections. 47 * It scans for advertisements and connects to the first device that lists a 48 * random service. 49 */ 50 // ***************************************************************************** 51 52 53 #include <stdint.h> 54 #include <inttypes.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 59 #include "btstack.h" 60 // sm_pairing_central.gatt contains the declaration of the provided GATT Services + Characteristics 61 // sm_pairing_central.h contains the binary representation of sm_pairing_central.gatt 62 // it is generated by the build system by calling: $BTSTACK_ROOT/tool/compile_gatt.py sm_pairing_central.gatt sm_pairing_central.h 63 // it needs to be regenerated when the GATT Database declared in sm_pairing_central.gatt file is modified 64 #include "sm_pairing_central.h" 65 66 67 // We're looking for a remote device that lists this service in the advertisement 68 // LightBlue assigns 0x1111 as the UUID for a Blank service. 69 #define REMOTE_SERVICE 0x1111 70 71 // Fixed passkey - used with sm_pairing_peripheral. Passkey is random in general 72 #define FIXED_PASSKEY 12346 73 74 75 static btstack_packet_callback_registration_t hci_event_callback_registration; 76 static btstack_packet_callback_registration_t sm_event_callback_registration; 77 78 /* @section GAP LE setup for receiving advertisements 79 * 80 * @text GAP LE advertisements are received as custom HCI events of the 81 * GAP_EVENT_ADVERTISING_REPORT type. To receive them, you'll need to register 82 * the HCI packet handler, as shown in Listing GAPLEAdvSetup. 83 */ 84 85 /* LISTING_START(GAPLEAdvSetup): Setting up GAP LE client for receiving advertisements */ 86 static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 87 static void sm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 88 89 static void sm_pairing_central_setup(void){ 90 l2cap_init(); 91 92 // setup le device db 93 le_device_db_init(); 94 95 // setup SM: Display only 96 sm_init(); 97 98 // setup ATT server 99 att_server_init(profile_data, NULL, NULL); 100 101 /** 102 * Choose ONE of the following configurations 103 * Bonding is disabled to allow for repeated testing. It can be enabled by or'ing 104 * SM_AUTHREQ_BONDING to the authentication requirements like this: 105 * sm_set_authentication_requirements( X | SM_AUTHREQ_BONDING) 106 */ 107 108 // register handler 109 hci_event_callback_registration.callback = &hci_packet_handler; 110 hci_add_event_handler(&hci_event_callback_registration); 111 112 sm_event_callback_registration.callback = &sm_packet_handler; 113 sm_add_event_handler(&sm_event_callback_registration); 114 115 // LE Legacy Pairing, Just Works 116 // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO); 117 // sm_set_authentication_requirements(0); 118 119 // LE Legacy Pairing, Passkey entry initiator enter, responder (us) displays 120 // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY); 121 // sm_set_authentication_requirements(SM_AUTHREQ_MITM_PROTECTION); 122 // sm_use_fixed_passkey_in_display_role(FIXED_PASSKEY); 123 124 #ifdef ENABLE_LE_SECURE_CONNECTIONS 125 126 // enable LE Secure Connections Only mode - disables Legacy pairing 127 // sm_set_secure_connections_only_mode(true); 128 129 // LE Secure Connections, Just Works 130 // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO); 131 // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION); 132 133 // LE Secure Connections, Numeric Comparison 134 sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO); 135 sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION); 136 137 // LE Legacy Pairing, Passkey entry initiator enter, responder (us) displays 138 // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY); 139 // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION); 140 // sm_use_fixed_passkey_in_display_role(FIXED_PASSKEY); 141 #endif 142 } 143 144 /* LISTING_END */ 145 146 /* @section HCI packet handler 147 * 148 * @text The HCI packet handler has to start the scanning, 149 * and to handle received advertisements. Advertisements are received 150 * as HCI event packets of the GAP_EVENT_ADVERTISING_REPORT type, 151 * see Listing HCIPacketHandler. 152 */ 153 154 /* LISTING_START(HCIPacketHandler): Scanning and receiving advertisements */ 155 156 static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 157 UNUSED(channel); 158 UNUSED(size); 159 160 if (packet_type != HCI_EVENT_PACKET) return; 161 hci_con_handle_t con_handle; 162 163 switch (hci_event_packet_get_type(packet)) { 164 case BTSTACK_EVENT_STATE: 165 // BTstack activated, get started 166 if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 167 printf("Start scaning!\n"); 168 gap_set_scan_parameters(1,0x0030, 0x0030); 169 gap_start_scan(); 170 } 171 break; 172 case GAP_EVENT_ADVERTISING_REPORT:{ 173 bd_addr_t address; 174 gap_event_advertising_report_get_address(packet, address); 175 uint8_t address_type = gap_event_advertising_report_get_address_type(packet); 176 uint8_t length = gap_event_advertising_report_get_data_length(packet); 177 const uint8_t * data = gap_event_advertising_report_get_data(packet); 178 // printf("Advertisement event: addr-type %u, addr %s, data[%u] ", 179 // address_type, bd_addr_to_str(address), length); 180 // printf_hexdump(data, length); 181 if (!ad_data_contains_uuid16(length, (uint8_t *) data, REMOTE_SERVICE)) break; 182 printf("Found remote with UUID %04x, connecting...\n", REMOTE_SERVICE); 183 gap_stop_scan(); 184 gap_connect(address,address_type); 185 break; 186 } 187 case HCI_EVENT_LE_META: 188 // wait for connection complete 189 if (hci_event_le_meta_get_subevent_code(packet) != HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break; 190 con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 191 printf("Connection complete\n"); 192 // start pairing 193 sm_request_pairing(con_handle); 194 break; 195 case HCI_EVENT_ENCRYPTION_CHANGE: 196 con_handle = hci_event_encryption_change_get_connection_handle(packet); 197 printf("Connection encrypted: %u\n", hci_event_encryption_change_get_encryption_enabled(packet)); 198 break; 199 default: 200 break; 201 } 202 } 203 204 /* @section HCI packet handler 205 * 206 * @text The SM packet handler receives Security Manager Events required for pairing. 207 * It also receives events generated during Identity Resolving 208 * see Listing SMPacketHandler. 209 */ 210 211 /* LISTING_START(SMPacketHandler): Scanning and receiving advertisements */ 212 213 static void sm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 214 UNUSED(channel); 215 UNUSED(size); 216 217 if (packet_type != HCI_EVENT_PACKET) return; 218 219 switch (hci_event_packet_get_type(packet)) { 220 case SM_EVENT_JUST_WORKS_REQUEST: 221 printf("Just works requested\n"); 222 sm_just_works_confirm(sm_event_just_works_request_get_handle(packet)); 223 break; 224 case SM_EVENT_NUMERIC_COMPARISON_REQUEST: 225 printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet)); 226 sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet)); 227 break; 228 case SM_EVENT_PASSKEY_DISPLAY_NUMBER: 229 printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet)); 230 break; 231 case SM_EVENT_PASSKEY_INPUT_NUMBER: 232 printf("Passkey Input requested\n"); 233 printf("Sending fixed passkey %"PRIu32"\n", FIXED_PASSKEY); 234 sm_passkey_input(sm_event_passkey_input_number_get_handle(packet), FIXED_PASSKEY); 235 break; 236 case SM_EVENT_PAIRING_COMPLETE: 237 switch (sm_event_pairing_complete_get_status(packet)){ 238 case ERROR_CODE_SUCCESS: 239 printf("Pairing complete, success\n"); 240 break; 241 case ERROR_CODE_CONNECTION_TIMEOUT: 242 printf("Pairing failed, timeout\n"); 243 break; 244 case ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION: 245 printf("Pairing failed, disconnected\n"); 246 break; 247 case ERROR_CODE_AUTHENTICATION_FAILURE: 248 printf("Pairing failed, reason = %u\n", sm_event_pairing_complete_get_reason(packet)); 249 break; 250 default: 251 break; 252 } 253 break; 254 default: 255 break; 256 } 257 } 258 /* LISTING_END */ 259 260 int btstack_main(void); 261 int btstack_main(void) 262 { 263 sm_pairing_central_setup(); 264 265 // turn on! 266 hci_power_control(HCI_POWER_ON); 267 268 return 0; 269 } 270 271 /* EXAMPLE_END */ 272