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_peripheral.c" 39 40 // ***************************************************************************** 41 /* EXAMPLE_START(sm_pairing_peripheral): LE Peripheral - Test pairing combinations 42 * 43 * @text Depending on the Authentication requiremens and IO Capabilities, 44 * the pairing process uses different short and long term key generation method. 45 * This example helps explore the different options incl. LE Secure Connections. 46 */ 47 // ***************************************************************************** 48 49 #include <stdint.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <inttypes.h> 54 55 #include "sm_pairing_peripheral.h" 56 #include "btstack.h" 57 58 /* @section Main Application Setup 59 * 60 * @text Listing MainConfiguration shows main application code. 61 * It initializes L2CAP, the Security Manager and configures the ATT Server with the pre-compiled 62 * ATT Database generated from $sm_pairing_peripheral.gatt$. Finally, it configures the advertisements 63 * and boots the Bluetooth stack. 64 * In this example, the Advertisement contains the Flags attribute and the device name. 65 * The flag 0x06 indicates: LE General Discoverable Mode and BR/EDR not supported. 66 * Various examples for IO Capabilites and Authentication Requirements are given below. 67 */ 68 69 /* LISTING_START(MainConfiguration): Init L2CAP SM ATT Server and start heartbeat timer */ 70 static btstack_packet_callback_registration_t hci_event_callback_registration; 71 static btstack_packet_callback_registration_t sm_event_callback_registration; 72 // static hci_con_handle_t con_handle; 73 74 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 75 76 const uint8_t adv_data[] = { 77 // Flags general discoverable, BR/EDR not supported 78 0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06, 79 // Name 80 0x0b, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'S', 'M', ' ', 'P', 'a', 'i', 'r', 'i', 'n', 'g', 81 }; 82 const uint8_t adv_data_len = sizeof(adv_data); 83 84 static void sm_peripheral_setup(void){ 85 86 l2cap_init(); 87 88 // setup le device db 89 le_device_db_init(); 90 91 // setup SM: Display only 92 sm_init(); 93 94 /** 95 * Choose ONE of the following configurations 96 */ 97 98 // LE Legacy Pairing, Just Works 99 // sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT); 100 // sm_set_authentication_requirements(SM_AUTHREQ_BONDING); 101 102 // LE Legacy Pairing, Passkey entry initiator enter, responder (us) displays 103 // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY); 104 // sm_set_authentication_requirements(SM_AUTHREQ_MITM_PROTECTION); 105 // sm_use_fixed_passkey_in_display_role(123456); 106 107 #ifdef ENABLE_LE_SECURE_CONNECTIONS 108 // LE Secure Connetions, Just Works 109 // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO); 110 // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION); 111 112 // LE Secure Connections, Numeric Comparison 113 sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY); 114 sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION); 115 116 // LE Legacy Pairing, Passkey entry initiator enter, responder (us) displays 117 // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY); 118 // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION); 119 // sm_use_fixed_passkey_in_display_role(123456); 120 #endif 121 122 // setup ATT server 123 att_server_init(profile_data, NULL, NULL); 124 125 // setup advertisements 126 uint16_t adv_int_min = 0x0030; 127 uint16_t adv_int_max = 0x0030; 128 uint8_t adv_type = 0; 129 bd_addr_t null_addr; 130 memset(null_addr, 0, 6); 131 gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00); 132 gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data); 133 gap_advertisements_enable(1); 134 135 // register for HCI events 136 hci_event_callback_registration.callback = &packet_handler; 137 hci_add_event_handler(&hci_event_callback_registration); 138 139 // register for SM events 140 sm_event_callback_registration.callback = &packet_handler; 141 sm_add_event_handler(&sm_event_callback_registration); 142 143 // register for ATT 144 att_server_register_packet_handler(packet_handler); 145 } 146 147 /* LISTING_END */ 148 149 /* 150 * @section Packet Handler 151 * 152 * @text The packet handler is used to: 153 * - stop the counter after a disconnect 154 * - send a notification when the requested ATT_EVENT_CAN_SEND_NOW is received 155 */ 156 157 /* LISTING_START(packetHandler): Packet Handler */ 158 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 159 UNUSED(channel); 160 UNUSED(size); 161 hci_con_handle_t con_handle; 162 bd_addr_t addr; 163 switch (packet_type) { 164 case HCI_EVENT_PACKET: 165 switch (hci_event_packet_get_type(packet)) { 166 case HCI_EVENT_LE_META: 167 switch (hci_event_le_meta_get_subevent_code(packet)) { 168 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 169 // setup new 170 con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 171 sm_send_security_request(con_handle); 172 break; 173 default: 174 break; 175 } 176 break; 177 case SM_EVENT_JUST_WORKS_REQUEST: 178 printf("Just Works requested\n"); 179 sm_just_works_confirm(sm_event_just_works_request_get_handle(packet)); 180 break; 181 case SM_EVENT_NUMERIC_COMPARISON_REQUEST: 182 printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet)); 183 sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet)); 184 break; 185 case SM_EVENT_PASSKEY_DISPLAY_NUMBER: 186 printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet)); 187 break; 188 case SM_EVENT_IDENTITY_CREATED: 189 sm_event_identity_created_get_identity_address(packet, addr); 190 printf("Identity created: type %u address %s\n", sm_event_identity_created_get_identity_addr_type(packet), bd_addr_to_str(addr)); 191 break; 192 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 193 sm_event_identity_resolving_succeeded_get_identity_address(packet, addr); 194 printf("Identity resolved: type %u address %s\n", sm_event_identity_resolving_succeeded_get_identity_addr_type(packet), bd_addr_to_str(addr)); 195 break; 196 case SM_EVENT_IDENTITY_RESOLVING_FAILED: 197 sm_event_identity_created_get_address(packet, addr); 198 printf("Identity resolving failed\n"); 199 break; 200 case SM_EVENT_PAIRING_COMPLETE: 201 switch (sm_event_pairing_complete_get_status(packet)){ 202 case ERROR_CODE_SUCCESS: 203 printf("Pairing complete, success\n"); 204 break; 205 case ERROR_CODE_CONNECTION_TIMEOUT: 206 printf("Pairing failed, timeout\n"); 207 break; 208 case ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION: 209 printf("Pairing faileed, disconnected\n"); 210 break; 211 case ERROR_CODE_AUTHENTICATION_FAILURE: 212 printf("Pairing failed, reason = %u\n", sm_event_pairing_complete_get_reason(packet)); 213 break; 214 default: 215 break; 216 } 217 break; 218 default: 219 break; 220 } 221 break; 222 } 223 } 224 /* LISTING_END */ 225 226 int btstack_main(void); 227 int btstack_main(void) 228 { 229 sm_peripheral_setup(); 230 231 // turn on! 232 hci_power_control(HCI_POWER_ON); 233 234 return 0; 235 } 236 /* EXAMPLE_END */ 237