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