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__ "gap_link_keys.c" 39 40 // ***************************************************************************** 41 /* EXAMPLE_START(gap_link_keys): GAP Link Key Management Example 42 * @text Shows how to iterate over the Classic Link Keys stored in NVS 43 * Link Keys are per device-device bonding. If the Bluetooth Controller can be swapped, 44 * e.g. on desktop systems, a Link Key DB for each Controller is needed. 45 * We need to wait until the Bluetooth Stack has started up and selected 46 * the correct Link Key DB based on the Controller's BD_ADDR. 47 */ 48 // ***************************************************************************** 49 50 #include <stdint.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 55 #include "btstack.h" 56 57 static btstack_packet_callback_registration_t hci_event_callback_registration; 58 59 /* @section GAP Link Key Logic 60 * 61 * @text List stored link keys 62 */ 63 static void list_link_keys(void){ 64 bd_addr_t addr; 65 link_key_t link_key; 66 link_key_type_t type; 67 btstack_link_key_iterator_t it; 68 69 int ok = gap_link_key_iterator_init(&it); 70 if (!ok) { 71 printf("Link key iterator not implemented\n"); 72 return; 73 } 74 printf("Stored link keys: \n"); 75 while (gap_link_key_iterator_get_next(&it, addr, link_key, &type)){ 76 printf("%s - type %u, key: ", bd_addr_to_str(addr), (int) type); 77 printf_hexdump(link_key, 16); 78 } 79 printf(".\n"); 80 gap_link_key_iterator_done(&it); 81 } 82 83 /* @section Bluetooth Logic 84 * 85 * @text Wait for Bluetooth startup before listing the stored link keys 86 */ 87 88 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 89 UNUSED(channel); 90 UNUSED(size); 91 92 if (packet_type != HCI_EVENT_PACKET) return; 93 switch(hci_event_packet_get_type(packet)){ 94 case BTSTACK_EVENT_STATE: 95 if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 96 list_link_keys(); 97 break; 98 } 99 break; 100 default: 101 break; 102 } 103 } 104 105 /* @section Main Application Setup 106 * 107 * @text Listing MainConfiguration shows main application code. 108 * It registers the HCI packet handler and starts the Bluetooth stack. 109 */ 110 111 /* LISTING_START(MainConfiguration): Setup packet handler for GAP inquiry */ 112 int btstack_main(int argc, const char * argv[]); 113 int btstack_main(int argc, const char * argv[]) { 114 (void)argc; 115 (void)argv; 116 117 hci_event_callback_registration.callback = &packet_handler; 118 hci_add_event_handler(&hci_event_callback_registration); 119 120 // turn on! 121 hci_power_control(HCI_POWER_ON); 122 123 return 0; 124 } 125 /* LISTING_END */ 126 /* EXAMPLE_END */ 127