1caa3fd52SMatthias Ringwald /*
2caa3fd52SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH
3caa3fd52SMatthias Ringwald *
4caa3fd52SMatthias Ringwald * Redistribution and use in source and binary forms, with or without
5caa3fd52SMatthias Ringwald * modification, are permitted provided that the following conditions
6caa3fd52SMatthias Ringwald * are met:
7caa3fd52SMatthias Ringwald *
8caa3fd52SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright
9caa3fd52SMatthias Ringwald * notice, this list of conditions and the following disclaimer.
10caa3fd52SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright
11caa3fd52SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the
12caa3fd52SMatthias Ringwald * documentation and/or other materials provided with the distribution.
13caa3fd52SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of
14caa3fd52SMatthias Ringwald * contributors may be used to endorse or promote products derived
15caa3fd52SMatthias Ringwald * from this software without specific prior written permission.
16caa3fd52SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for
17caa3fd52SMatthias Ringwald * personal benefit and not for any commercial purpose or for
18caa3fd52SMatthias Ringwald * monetary gain.
19caa3fd52SMatthias Ringwald *
20caa3fd52SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21caa3fd52SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22caa3fd52SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*2fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24*2fca4dadSMilanka Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25caa3fd52SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26caa3fd52SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27caa3fd52SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28caa3fd52SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29caa3fd52SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30caa3fd52SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31caa3fd52SMatthias Ringwald * SUCH DAMAGE.
32caa3fd52SMatthias Ringwald *
33caa3fd52SMatthias Ringwald * Please inquire about commercial licensing options at
34caa3fd52SMatthias Ringwald * [email protected]
35caa3fd52SMatthias Ringwald *
36caa3fd52SMatthias Ringwald */
37caa3fd52SMatthias Ringwald
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "gap_link_keys.c"
39caa3fd52SMatthias Ringwald
40caa3fd52SMatthias Ringwald // *****************************************************************************
41ec8ae085SMilanka Ringwald /* EXAMPLE_START(gap_link_keys): GAP Link Key Management (Classic)
42ec8ae085SMilanka Ringwald *
43caa3fd52SMatthias Ringwald * @text Shows how to iterate over the Classic Link Keys stored in NVS
44caa3fd52SMatthias Ringwald * Link Keys are per device-device bonding. If the Bluetooth Controller can be swapped,
45caa3fd52SMatthias Ringwald * e.g. on desktop systems, a Link Key DB for each Controller is needed.
46caa3fd52SMatthias Ringwald * We need to wait until the Bluetooth Stack has started up and selected
47caa3fd52SMatthias Ringwald * the correct Link Key DB based on the Controller's BD_ADDR.
48caa3fd52SMatthias Ringwald */
49caa3fd52SMatthias Ringwald // *****************************************************************************
50caa3fd52SMatthias Ringwald
51caa3fd52SMatthias Ringwald #include <stdint.h>
52caa3fd52SMatthias Ringwald #include <stdio.h>
53caa3fd52SMatthias Ringwald #include <stdlib.h>
54caa3fd52SMatthias Ringwald #include <string.h>
55caa3fd52SMatthias Ringwald
56caa3fd52SMatthias Ringwald #include "btstack.h"
57caa3fd52SMatthias Ringwald
58caa3fd52SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
59caa3fd52SMatthias Ringwald
60caa3fd52SMatthias Ringwald /* @section GAP Link Key Logic
61caa3fd52SMatthias Ringwald *
62caa3fd52SMatthias Ringwald * @text List stored link keys
63caa3fd52SMatthias Ringwald */
list_link_keys(void)64caa3fd52SMatthias Ringwald static void list_link_keys(void){
65caa3fd52SMatthias Ringwald bd_addr_t addr;
66caa3fd52SMatthias Ringwald link_key_t link_key;
67caa3fd52SMatthias Ringwald link_key_type_t type;
68caa3fd52SMatthias Ringwald btstack_link_key_iterator_t it;
69caa3fd52SMatthias Ringwald
70caa3fd52SMatthias Ringwald int ok = gap_link_key_iterator_init(&it);
71caa3fd52SMatthias Ringwald if (!ok) {
72caa3fd52SMatthias Ringwald printf("Link key iterator not implemented\n");
73caa3fd52SMatthias Ringwald return;
74caa3fd52SMatthias Ringwald }
75caa3fd52SMatthias Ringwald printf("Stored link keys: \n");
76caa3fd52SMatthias Ringwald while (gap_link_key_iterator_get_next(&it, addr, link_key, &type)){
77caa3fd52SMatthias Ringwald printf("%s - type %u, key: ", bd_addr_to_str(addr), (int) type);
78caa3fd52SMatthias Ringwald printf_hexdump(link_key, 16);
79caa3fd52SMatthias Ringwald }
80caa3fd52SMatthias Ringwald printf(".\n");
81caa3fd52SMatthias Ringwald gap_link_key_iterator_done(&it);
82caa3fd52SMatthias Ringwald }
83caa3fd52SMatthias Ringwald
84caa3fd52SMatthias Ringwald /* @section Bluetooth Logic
85caa3fd52SMatthias Ringwald *
86caa3fd52SMatthias Ringwald * @text Wait for Bluetooth startup before listing the stored link keys
87caa3fd52SMatthias Ringwald */
88caa3fd52SMatthias Ringwald
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)89caa3fd52SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
90caa3fd52SMatthias Ringwald UNUSED(channel);
91caa3fd52SMatthias Ringwald UNUSED(size);
92caa3fd52SMatthias Ringwald
93caa3fd52SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return;
94caa3fd52SMatthias Ringwald switch(hci_event_packet_get_type(packet)){
95caa3fd52SMatthias Ringwald case BTSTACK_EVENT_STATE:
96caa3fd52SMatthias Ringwald if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
97caa3fd52SMatthias Ringwald list_link_keys();
98caa3fd52SMatthias Ringwald break;
99caa3fd52SMatthias Ringwald }
100caa3fd52SMatthias Ringwald break;
101caa3fd52SMatthias Ringwald default:
102caa3fd52SMatthias Ringwald break;
103caa3fd52SMatthias Ringwald }
104caa3fd52SMatthias Ringwald }
105caa3fd52SMatthias Ringwald
106caa3fd52SMatthias Ringwald /* @section Main Application Setup
107caa3fd52SMatthias Ringwald *
108caa3fd52SMatthias Ringwald * @text Listing MainConfiguration shows main application code.
109caa3fd52SMatthias Ringwald * It registers the HCI packet handler and starts the Bluetooth stack.
110caa3fd52SMatthias Ringwald */
111caa3fd52SMatthias Ringwald
112caa3fd52SMatthias Ringwald /* LISTING_START(MainConfiguration): Setup packet handler for GAP inquiry */
113caa3fd52SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
btstack_main(int argc,const char * argv[])114caa3fd52SMatthias Ringwald int btstack_main(int argc, const char * argv[]) {
115caa3fd52SMatthias Ringwald (void)argc;
116caa3fd52SMatthias Ringwald (void)argv;
117caa3fd52SMatthias Ringwald
118caa3fd52SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler;
119caa3fd52SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration);
120caa3fd52SMatthias Ringwald
121caa3fd52SMatthias Ringwald // turn on!
122caa3fd52SMatthias Ringwald hci_power_control(HCI_POWER_ON);
123caa3fd52SMatthias Ringwald
124caa3fd52SMatthias Ringwald return 0;
125caa3fd52SMatthias Ringwald }
126caa3fd52SMatthias Ringwald /* LISTING_END */
127caa3fd52SMatthias Ringwald /* EXAMPLE_END */
128