1d7ee901aSMatthias Ringwald /* 2d7ee901aSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3d7ee901aSMatthias Ringwald * 4d7ee901aSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5d7ee901aSMatthias Ringwald * modification, are permitted provided that the following conditions 6d7ee901aSMatthias Ringwald * are met: 7d7ee901aSMatthias Ringwald * 8d7ee901aSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9d7ee901aSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10d7ee901aSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11d7ee901aSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12d7ee901aSMatthias Ringwald * documentation and/or other materials provided with the distribution. 13d7ee901aSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14d7ee901aSMatthias Ringwald * contributors may be used to endorse or promote products derived 15d7ee901aSMatthias Ringwald * from this software without specific prior written permission. 16d7ee901aSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17d7ee901aSMatthias Ringwald * personal benefit and not for any commercial purpose or for 18d7ee901aSMatthias Ringwald * monetary gain. 19d7ee901aSMatthias Ringwald * 20d7ee901aSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21d7ee901aSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22d7ee901aSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23d7ee901aSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24d7ee901aSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25d7ee901aSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26d7ee901aSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27d7ee901aSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28d7ee901aSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29d7ee901aSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30d7ee901aSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31d7ee901aSMatthias Ringwald * SUCH DAMAGE. 32d7ee901aSMatthias Ringwald * 33d7ee901aSMatthias Ringwald * Please inquire about commercial licensing options at 34d7ee901aSMatthias Ringwald * [email protected] 35d7ee901aSMatthias Ringwald * 36d7ee901aSMatthias Ringwald */ 37d7ee901aSMatthias Ringwald 38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "sm_pairing_peripheral.c" 39ab2c6ae4SMatthias Ringwald 40d7ee901aSMatthias Ringwald // ***************************************************************************** 41d7ee901aSMatthias Ringwald /* EXAMPLE_START(sm_pairing_peripheral): LE Peripheral - Test pairing combinations 42d7ee901aSMatthias Ringwald * 43d7ee901aSMatthias Ringwald * @text Depending on the Authentication requiremens and IO Capabilities, 44d7ee901aSMatthias Ringwald * the pairing process uses different short and long term key generation method. 45d7ee901aSMatthias Ringwald * This example helps explore the different options incl. LE Secure Connections. 46d7ee901aSMatthias Ringwald */ 47d7ee901aSMatthias Ringwald // ***************************************************************************** 48d7ee901aSMatthias Ringwald 49d7ee901aSMatthias Ringwald #include <stdint.h> 50d7ee901aSMatthias Ringwald #include <stdio.h> 51d7ee901aSMatthias Ringwald #include <stdlib.h> 52d7ee901aSMatthias Ringwald #include <string.h> 53bace42efSMatthias Ringwald #include <inttypes.h> 54d7ee901aSMatthias Ringwald 55d7ee901aSMatthias Ringwald #include "sm_pairing_peripheral.h" 56d7ee901aSMatthias Ringwald #include "btstack.h" 57d7ee901aSMatthias Ringwald 58d7ee901aSMatthias Ringwald /* @section Main Application Setup 59d7ee901aSMatthias Ringwald * 60d7ee901aSMatthias Ringwald * @text Listing MainConfiguration shows main application code. 61d7ee901aSMatthias Ringwald * It initializes L2CAP, the Security Manager and configures the ATT Server with the pre-compiled 62d7ee901aSMatthias Ringwald * ATT Database generated from $sm_pairing_peripheral.gatt$. Finally, it configures the advertisements 63d7ee901aSMatthias Ringwald * and boots the Bluetooth stack. 64d7ee901aSMatthias Ringwald * In this example, the Advertisement contains the Flags attribute and the device name. 65d7ee901aSMatthias Ringwald * The flag 0x06 indicates: LE General Discoverable Mode and BR/EDR not supported. 66d7ee901aSMatthias Ringwald * Various examples for IO Capabilites and Authentication Requirements are given below. 67d7ee901aSMatthias Ringwald */ 68d7ee901aSMatthias Ringwald 69d7ee901aSMatthias Ringwald /* LISTING_START(MainConfiguration): Init L2CAP SM ATT Server and start heartbeat timer */ 70d7ee901aSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 71d7ee901aSMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration; 72d7ee901aSMatthias Ringwald // static hci_con_handle_t con_handle; 73d7ee901aSMatthias Ringwald 74d7ee901aSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 75d7ee901aSMatthias Ringwald 76d7ee901aSMatthias Ringwald const uint8_t adv_data[] = { 77d7ee901aSMatthias Ringwald // Flags general discoverable, BR/EDR not supported 78bf85c285SMatthias Ringwald 0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06, 79d7ee901aSMatthias Ringwald // Name 80bf85c285SMatthias Ringwald 0x0b, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'S', 'M', ' ', 'P', 'a', 'i', 'r', 'i', 'n', 'g', 81d7ee901aSMatthias Ringwald }; 82d7ee901aSMatthias Ringwald const uint8_t adv_data_len = sizeof(adv_data); 83d7ee901aSMatthias Ringwald 84d7ee901aSMatthias Ringwald static void sm_peripheral_setup(void){ 85d7ee901aSMatthias Ringwald 86d7ee901aSMatthias Ringwald l2cap_init(); 87d7ee901aSMatthias Ringwald 88d7ee901aSMatthias Ringwald // setup le device db 89d7ee901aSMatthias Ringwald le_device_db_init(); 90d7ee901aSMatthias Ringwald 91d7ee901aSMatthias Ringwald // setup SM: Display only 92d7ee901aSMatthias Ringwald sm_init(); 93d7ee901aSMatthias Ringwald 94d7ee901aSMatthias Ringwald /** 95d7ee901aSMatthias Ringwald * Choose ONE of the following configurations 96d7ee901aSMatthias Ringwald */ 97d7ee901aSMatthias Ringwald 98d7ee901aSMatthias Ringwald // LE Legacy Pairing, Just Works 994b8c611fSMatthias Ringwald // sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT); 100*a4fe6467SMatthias Ringwald // sm_set_authentication_requirements(SM_AUTHREQ_BONDING); 101d7ee901aSMatthias Ringwald 102d7ee901aSMatthias Ringwald // LE Legacy Pairing, Passkey entry initiator enter, responder (us) displays 103d7ee901aSMatthias Ringwald // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY); 104d7ee901aSMatthias Ringwald // sm_set_authentication_requirements(SM_AUTHREQ_MITM_PROTECTION); 1054b8c611fSMatthias Ringwald // sm_use_fixed_passkey_in_display_role(123456); 106d7ee901aSMatthias Ringwald 107d7ee901aSMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS 108d7ee901aSMatthias Ringwald // LE Secure Connetions, Just Works 109d7ee901aSMatthias Ringwald // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO); 110d7ee901aSMatthias Ringwald // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION); 111d7ee901aSMatthias Ringwald 112d7ee901aSMatthias Ringwald // LE Secure Connections, Numeric Comparison 113*a4fe6467SMatthias Ringwald sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY); 114*a4fe6467SMatthias Ringwald sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION); 115d7ee901aSMatthias Ringwald 116d7ee901aSMatthias Ringwald // LE Legacy Pairing, Passkey entry initiator enter, responder (us) displays 117d7ee901aSMatthias Ringwald // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY); 118d7ee901aSMatthias Ringwald // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION); 1194b8c611fSMatthias Ringwald // sm_use_fixed_passkey_in_display_role(123456); 120d7ee901aSMatthias Ringwald #endif 121d7ee901aSMatthias Ringwald 122d7ee901aSMatthias Ringwald // setup ATT server 123d7ee901aSMatthias Ringwald att_server_init(profile_data, NULL, NULL); 124d7ee901aSMatthias Ringwald 125d7ee901aSMatthias Ringwald // setup advertisements 126d7ee901aSMatthias Ringwald uint16_t adv_int_min = 0x0030; 127d7ee901aSMatthias Ringwald uint16_t adv_int_max = 0x0030; 128d7ee901aSMatthias Ringwald uint8_t adv_type = 0; 129d7ee901aSMatthias Ringwald bd_addr_t null_addr; 130d7ee901aSMatthias Ringwald memset(null_addr, 0, 6); 131d7ee901aSMatthias Ringwald gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00); 132d7ee901aSMatthias Ringwald gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data); 133d7ee901aSMatthias Ringwald gap_advertisements_enable(1); 134*a4fe6467SMatthias Ringwald 135*a4fe6467SMatthias Ringwald // register for HCI events 136*a4fe6467SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 137*a4fe6467SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 138*a4fe6467SMatthias Ringwald 139*a4fe6467SMatthias Ringwald // register for SM events 140*a4fe6467SMatthias Ringwald sm_event_callback_registration.callback = &packet_handler; 141*a4fe6467SMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 142*a4fe6467SMatthias Ringwald 143*a4fe6467SMatthias Ringwald // register for ATT 144*a4fe6467SMatthias Ringwald att_server_register_packet_handler(packet_handler); 145d7ee901aSMatthias Ringwald } 146d7ee901aSMatthias Ringwald 147d7ee901aSMatthias Ringwald /* LISTING_END */ 148d7ee901aSMatthias Ringwald 149d7ee901aSMatthias Ringwald /* 150d7ee901aSMatthias Ringwald * @section Packet Handler 151d7ee901aSMatthias Ringwald * 152d7ee901aSMatthias Ringwald * @text The packet handler is used to: 153d7ee901aSMatthias Ringwald * - stop the counter after a disconnect 154d7ee901aSMatthias Ringwald * - send a notification when the requested ATT_EVENT_CAN_SEND_NOW is received 155d7ee901aSMatthias Ringwald */ 156d7ee901aSMatthias Ringwald 157d7ee901aSMatthias Ringwald /* LISTING_START(packetHandler): Packet Handler */ 158d7ee901aSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1599ec2630cSMatthias Ringwald UNUSED(channel); 1609ec2630cSMatthias Ringwald UNUSED(size); 161*a4fe6467SMatthias Ringwald hci_con_handle_t con_handle; 1629c22b849SMatthias Ringwald bd_addr_t addr; 163d7ee901aSMatthias Ringwald switch (packet_type) { 164d7ee901aSMatthias Ringwald case HCI_EVENT_PACKET: 165d7ee901aSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 166*a4fe6467SMatthias Ringwald case HCI_EVENT_LE_META: 167*a4fe6467SMatthias Ringwald switch (hci_event_le_meta_get_subevent_code(packet)) { 168*a4fe6467SMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 169*a4fe6467SMatthias Ringwald // setup new 170*a4fe6467SMatthias Ringwald con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 171*a4fe6467SMatthias Ringwald sm_send_security_request(con_handle); 172*a4fe6467SMatthias Ringwald break; 173*a4fe6467SMatthias Ringwald default: 174*a4fe6467SMatthias Ringwald break; 175*a4fe6467SMatthias Ringwald } 176*a4fe6467SMatthias Ringwald break; 177d7ee901aSMatthias Ringwald case SM_EVENT_JUST_WORKS_REQUEST: 178d7ee901aSMatthias Ringwald printf("Just Works requested\n"); 179d7ee901aSMatthias Ringwald sm_just_works_confirm(sm_event_just_works_request_get_handle(packet)); 180d7ee901aSMatthias Ringwald break; 181d7ee901aSMatthias Ringwald case SM_EVENT_NUMERIC_COMPARISON_REQUEST: 182bace42efSMatthias Ringwald printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet)); 183d7ee901aSMatthias Ringwald sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet)); 184d7ee901aSMatthias Ringwald break; 185d7ee901aSMatthias Ringwald case SM_EVENT_PASSKEY_DISPLAY_NUMBER: 186bace42efSMatthias Ringwald printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet)); 187d7ee901aSMatthias Ringwald break; 1889c22b849SMatthias Ringwald case SM_EVENT_IDENTITY_CREATED: 1899c22b849SMatthias Ringwald sm_event_identity_created_get_identity_address(packet, addr); 1909c22b849SMatthias Ringwald printf("Identity created: type %u address %s\n", sm_event_identity_created_get_identity_addr_type(packet), bd_addr_to_str(addr)); 1919c22b849SMatthias Ringwald break; 1929c22b849SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 1939c22b849SMatthias Ringwald sm_event_identity_resolving_succeeded_get_identity_address(packet, addr); 1949c22b849SMatthias Ringwald printf("Identity resolved: type %u address %s\n", sm_event_identity_resolving_succeeded_get_identity_addr_type(packet), bd_addr_to_str(addr)); 1959c22b849SMatthias Ringwald break; 1969c22b849SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_FAILED: 1979c22b849SMatthias Ringwald sm_event_identity_created_get_address(packet, addr); 1989c22b849SMatthias Ringwald printf("Identity resolving failed\n"); 1999c22b849SMatthias Ringwald break; 2000614d679SMatthias Ringwald case SM_EVENT_PAIRING_COMPLETE: 2010614d679SMatthias Ringwald switch (sm_event_pairing_complete_get_status(packet)){ 2020614d679SMatthias Ringwald case ERROR_CODE_SUCCESS: 2030614d679SMatthias Ringwald printf("Pairing complete, success\n"); 2040614d679SMatthias Ringwald break; 2050614d679SMatthias Ringwald case ERROR_CODE_CONNECTION_TIMEOUT: 2060614d679SMatthias Ringwald printf("Pairing failed, timeout\n"); 2070614d679SMatthias Ringwald break; 2080614d679SMatthias Ringwald case ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION: 2090614d679SMatthias Ringwald printf("Pairing faileed, disconnected\n"); 2100614d679SMatthias Ringwald break; 2110614d679SMatthias Ringwald case ERROR_CODE_AUTHENTICATION_FAILURE: 2120614d679SMatthias Ringwald printf("Pairing failed, reason = %u\n", sm_event_pairing_complete_get_reason(packet)); 2130614d679SMatthias Ringwald break; 2140614d679SMatthias Ringwald default: 2150614d679SMatthias Ringwald break; 2160614d679SMatthias Ringwald } 2170614d679SMatthias Ringwald break; 2180614d679SMatthias Ringwald default: 2190614d679SMatthias Ringwald break; 220d7ee901aSMatthias Ringwald } 221d7ee901aSMatthias Ringwald break; 222d7ee901aSMatthias Ringwald } 223d7ee901aSMatthias Ringwald } 224d7ee901aSMatthias Ringwald /* LISTING_END */ 225d7ee901aSMatthias Ringwald 226d7ee901aSMatthias Ringwald int btstack_main(void); 227d7ee901aSMatthias Ringwald int btstack_main(void) 228d7ee901aSMatthias Ringwald { 229d7ee901aSMatthias Ringwald sm_peripheral_setup(); 230d7ee901aSMatthias Ringwald 231d7ee901aSMatthias Ringwald // turn on! 232d7ee901aSMatthias Ringwald hci_power_control(HCI_POWER_ON); 233d7ee901aSMatthias Ringwald 234d7ee901aSMatthias Ringwald return 0; 235d7ee901aSMatthias Ringwald } 236d7ee901aSMatthias Ringwald /* EXAMPLE_END */ 237