xref: /btstack/src/classic/btstack_link_key_db_static.c (revision cbf509016daa1295a6a8b22d7314db50b88c6c9a)
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 static char link_key_to_str_buffer[LINK_KEY_STR_LEN+1];  // 11223344556677889900112233445566\0
71 static char *link_key_to_str(link_key_t link_key){
72     char * p = link_key_to_str_buffer;
73     int i;
74     for (i = 0; i < LINK_KEY_LEN ; i++) {
75         *p++ = char_for_nibble((link_key[i] >> 4) & 0x0F);
76         *p++ = char_for_nibble((link_key[i] >> 0) & 0x0F);
77     }
78     *p = 0;
79     return (char *) link_key_to_str_buffer;
80 }
81 
82 static int sscanf_link_key(const char * link_key_string, link_key_t link_key){
83     uint16_t pos;
84     for (pos = 0 ; pos < LINK_KEY_LEN; pos++){
85         int high = nibble_for_char(*link_key_string++);
86         if (high < 0) return 0;
87         int low = nibble_for_char(*link_key_string++);
88         if (low < 0) return 0;
89         link_key[pos] = (((uint8_t) high) << 4) | ((uint8_t) low);
90     }
91     return 1;
92 }
93 
94 static void link_key_db_init(void){
95 }
96 
97 static void link_key_db_close(void){
98 }
99 
100 
101 // returns 1 if found
102 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) {
103 	int i;
104 	int num_entries = sizeof(link_key_db) / sizeof(link_key_entry_t);
105 
106 	for (i=0;i<num_entries;i++){
107 		if (strcmp(bd_addr_to_str(bd_addr), link_key_db[i].bd_addr)) continue;
108 		*link_key_type = (link_key_type_t) link_key_db[i].link_key_type;
109 		sscanf_link_key(link_key_db[i].link_key, link_key);
110 		return 1;
111 	}
112 	return 0;
113 }
114 
115 static void link_key_db_delete_link_key(bd_addr_t bd_addr){
116     (void) bd_addr;
117 }
118 
119 
120 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){
121 	log_info("Please add the following line to btstack_link_key_db.c");
122 	log_info("{ \"%s\", \"%s\", %u },\n", bd_addr_to_str(bd_addr), link_key_to_str(link_key), (int) link_key_type);
123 }
124 
125 static void link_key_db_set_local_bd_addr(bd_addr_t bd_addr){
126     (void) bd_addr;
127 }
128 
129 static int link_key_db_tlv_iterator_init(btstack_link_key_iterator_t * it){
130     it->context = (void*) 0;
131     return 1;
132 }
133 
134 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){
135     uintptr_t i = (uintptr_t) it->context;
136 
137     unsigned int num_entries = sizeof(link_key_db) / sizeof(link_key_entry_t);
138     if (i >= num_entries) return 0;
139 
140     // fetch values
141     *link_key_type = (link_key_type_t) link_key_db[i].link_key_type;
142     sscanf_link_key(link_key_db[i].link_key, link_key);
143     sscanf_bd_addr(link_key_db[i].bd_addr, bd_addr);
144 
145     // next
146     it->context = (void*) (i+1);
147     return 1;
148 }
149 
150 static void link_key_db_tlv_iterator_done(btstack_link_key_iterator_t * it){
151     UNUSED(it);
152 }
153 
154 static const btstack_link_key_db_t btstack_link_key_db_static = {
155     link_key_db_init,
156     link_key_db_set_local_bd_addr,
157     link_key_db_close,
158     link_key_db_get_link_key,
159     link_key_db_put_link_key,
160     link_key_db_delete_link_key,
161     link_key_db_tlv_iterator_init,
162     link_key_db_tlv_iterator_get_next,
163     link_key_db_tlv_iterator_done,
164 };
165 
166 const btstack_link_key_db_t * btstack_link_key_db_static_instance(void){
167     return &btstack_link_key_db_static;
168 }
169