1 /*
2 * Copyright (C) 2016 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__ "btstack_link_key_db_static.c"
39
40 /*
41 * btstack_link_key_db_static.c
42 *
43 * Static Link Key implementation to use during development/porting:
44 * - Link keys have to be manually added to this file to make them usable
45 * + Link keys are preserved on reflash in constrast to the program flash based link key store
46 */
47
48 #include "classic/btstack_link_key_db.h"
49
50 #include "btstack_debug.h"
51 #include "btstack_util.h"
52
53 #include <stdint.h>
54 #include <stdio.h>
55 #include <string.h>
56
57 typedef struct {
58 const char * bd_addr;
59 const char * link_key;
60 int link_key_type;
61 } link_key_entry_t;
62
63 // fixed link key db
64 static const link_key_entry_t link_key_db[] = {
65 // Example enry
66 { "11:22:33:44:55:66", "11223344556677889900112233445566", 1},
67 // Add new link keys here..
68 };
69
70 #ifdef ENABLE_LOG_INFO
71 static char link_key_to_str_buffer[LINK_KEY_STR_LEN+1]; // 11223344556677889900112233445566\0
link_key_to_str(link_key_t link_key)72 static char *link_key_to_str(link_key_t link_key){
73 char * p = link_key_to_str_buffer;
74 int i;
75 for (i = 0; i < LINK_KEY_LEN ; i++) {
76 *p++ = char_for_nibble((link_key[i] >> 4) & 0x0F);
77 *p++ = char_for_nibble((link_key[i] >> 0) & 0x0F);
78 }
79 *p = 0;
80 return (char *) link_key_to_str_buffer;
81 }
82 #endif
83
sscanf_link_key(const char * link_key_string,link_key_t link_key)84 static int sscanf_link_key(const char * link_key_string, link_key_t link_key){
85 uint16_t pos;
86 for (pos = 0 ; pos < LINK_KEY_LEN; pos++){
87 int high = nibble_for_char(*link_key_string++);
88 if (high < 0) return 0;
89 int low = nibble_for_char(*link_key_string++);
90 if (low < 0) return 0;
91 link_key[pos] = (((uint8_t) high) << 4) | ((uint8_t) low);
92 }
93 return 1;
94 }
95
link_key_db_init(void)96 static void link_key_db_init(void){
97 }
98
link_key_db_close(void)99 static void link_key_db_close(void){
100 }
101
102
103 // returns 1 if found
link_key_db_get_link_key(bd_addr_t bd_addr,link_key_t link_key,link_key_type_t * link_key_type)104 static int link_key_db_get_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * link_key_type) {
105 int i;
106 int num_entries = sizeof(link_key_db) / sizeof(link_key_entry_t);
107
108 for (i=0;i<num_entries;i++){
109 if (strcmp(bd_addr_to_str(bd_addr), link_key_db[i].bd_addr)) continue;
110 *link_key_type = (link_key_type_t) link_key_db[i].link_key_type;
111 sscanf_link_key(link_key_db[i].link_key, link_key);
112 return 1;
113 }
114 return 0;
115 }
116
link_key_db_delete_link_key(bd_addr_t bd_addr)117 static void link_key_db_delete_link_key(bd_addr_t bd_addr){
118 (void) bd_addr;
119 }
120
121
link_key_db_put_link_key(bd_addr_t bd_addr,link_key_t link_key,link_key_type_t link_key_type)122 static void link_key_db_put_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t link_key_type){
123 UNUSED(bd_addr);
124 UNUSED(link_key);
125 UNUSED(link_key_type);
126 log_info("Please add the following line to btstack_link_key_db.c");
127 log_info("{ \"%s\", \"%s\", %u },\n", bd_addr_to_str(bd_addr), link_key_to_str(link_key), (int) link_key_type);
128 }
129
link_key_db_set_local_bd_addr(bd_addr_t bd_addr)130 static void link_key_db_set_local_bd_addr(bd_addr_t bd_addr){
131 (void) bd_addr;
132 }
133
link_key_db_tlv_iterator_init(btstack_link_key_iterator_t * it)134 static int link_key_db_tlv_iterator_init(btstack_link_key_iterator_t * it){
135 it->context = (void*) 0;
136 return 1;
137 }
138
link_key_db_tlv_iterator_get_next(btstack_link_key_iterator_t * it,bd_addr_t bd_addr,link_key_t link_key,link_key_type_t * link_key_type)139 static int link_key_db_tlv_iterator_get_next(btstack_link_key_iterator_t * it, bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * link_key_type){
140 uintptr_t i = (uintptr_t) it->context;
141
142 unsigned int num_entries = sizeof(link_key_db) / sizeof(link_key_entry_t);
143 if (i >= num_entries) return 0;
144
145 // fetch values
146 *link_key_type = (link_key_type_t) link_key_db[i].link_key_type;
147 sscanf_link_key(link_key_db[i].link_key, link_key);
148 sscanf_bd_addr(link_key_db[i].bd_addr, bd_addr);
149
150 // next
151 it->context = (void*) (i+1);
152 return 1;
153 }
154
link_key_db_tlv_iterator_done(btstack_link_key_iterator_t * it)155 static void link_key_db_tlv_iterator_done(btstack_link_key_iterator_t * it){
156 UNUSED(it);
157 }
158
159 static const btstack_link_key_db_t btstack_link_key_db_static = {
160 link_key_db_init,
161 link_key_db_set_local_bd_addr,
162 link_key_db_close,
163 link_key_db_get_link_key,
164 link_key_db_put_link_key,
165 link_key_db_delete_link_key,
166 link_key_db_tlv_iterator_init,
167 link_key_db_tlv_iterator_get_next,
168 link_key_db_tlv_iterator_done,
169 };
170
btstack_link_key_db_static_instance(void)171 const btstack_link_key_db_t * btstack_link_key_db_static_instance(void){
172 return &btstack_link_key_db_static;
173 }
174