xref: /btstack/src/classic/btstack_link_key_db_static.c (revision cebe3e9ef6f6ce0d36310f60f7b83c0285e9b5b4)
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 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__ "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 "stdint.h"
51 #include "string.h"
52 #include "btstack_debug.h"
53 #include "btstack_util.h"
54 
55 typedef struct {
56 	const char * bd_addr;
57 	const char * link_key;
58 	int          link_key_type;
59 } link_key_entry_t;
60 
61 // fixed link key db
62 static const link_key_entry_t link_key_db[] = {
63 		// Example enry
64 		{ "11:22:33:44:55:66", "11223344556677889900112233445566", 1},
65 		// Add new link keys here..
66 };
67 
68 static char link_key_to_str_buffer[LINK_KEY_STR_LEN+1];  // 11223344556677889900112233445566\0
69 static char *link_key_to_str(link_key_t link_key){
70     char * p = link_key_to_str_buffer;
71     int i;
72     for (i = 0; i < LINK_KEY_LEN ; i++) {
73         *p++ = char_for_nibble((link_key[i] >> 4) & 0x0F);
74         *p++ = char_for_nibble((link_key[i] >> 0) & 0x0F);
75     }
76     *p = 0;
77     return (char *) link_key_to_str_buffer;
78 }
79 
80 static int sscanf_link_key(const char * link_key_string, link_key_t link_key){
81     unsigned int buffer[LINK_KEY_LEN];
82 
83     // reset result buffer
84     memset(&buffer, 0, sizeof(buffer));
85 
86     // parse
87     int result = sscanf( (char *) link_key_string, "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
88                                     &buffer[0], &buffer[1], &buffer[2], &buffer[3],
89                                     &buffer[4], &buffer[5], &buffer[6], &buffer[7],
90                                     &buffer[8], &buffer[9], &buffer[10], &buffer[11],
91                                     &buffer[12], &buffer[13], &buffer[14], &buffer[15] );
92 
93     if (result != LINK_KEY_LEN) return 0;
94 
95     // store
96     int i;
97     uint8_t *p = (uint8_t *) link_key;
98     for (i=0; i<LINK_KEY_LEN; i++ ) {
99         *p++ = (uint8_t) buffer[i];
100     }
101     return 1;
102 }
103 
104 static void link_key_db_init(void){
105 }
106 
107 static void link_key_db_close(void){
108 }
109 
110 
111 // returns 1 if found
112 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) {
113 	int i;
114 	int num_entries = sizeof(link_key_db) / sizeof(link_key_entry_t);
115 
116 	for (i=0;i<num_entries;i++){
117 		if (strcmp(bd_addr_to_str(bd_addr), link_key_db[i].bd_addr)) continue;
118 		*link_key_type = (link_key_type_t) link_key_db[i].link_key_type;
119 		sscanf_link_key(link_key_db[i].link_key, link_key);
120 		return 1;
121 	}
122 	return 0;
123 }
124 
125 static void link_key_db_delete_link_key(bd_addr_t bd_addr){
126     (void) bd_addr;
127 }
128 
129 
130 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){
131 	log_info("Please add the following line to btstack_link_key_db.c");
132 	log_info("{ \"%s\", \"%s\", %u },\n", bd_addr_to_str(bd_addr), link_key_to_str(link_key), (int) link_key_type);
133 }
134 
135 static void link_key_db_set_local_bd_addr(bd_addr_t bd_addr){
136     (void) bd_addr;
137 }
138 
139 static int link_key_db_tlv_iterator_init(btstack_link_key_iterator_t * it){
140     it->context = (void*) 0;
141     return 1;
142 }
143 
144 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){
145     uintptr_t i = (uintptr_t) it->context;
146 
147     unsigned int num_entries = sizeof(link_key_db) / sizeof(link_key_entry_t);
148     if (i >= num_entries) return 0;
149 
150     // fetch values
151     *link_key_type = (link_key_type_t) link_key_db[i].link_key_type;
152     sscanf_link_key(link_key_db[i].link_key, link_key);
153     sscanf_bd_addr(link_key_db[i].bd_addr, bd_addr);
154 
155     // next
156     it->context = (void*) (i+1);
157     return 1;
158 }
159 
160 static void link_key_db_tlv_iterator_done(btstack_link_key_iterator_t * it){
161     UNUSED(it);
162 }
163 
164 static const btstack_link_key_db_t btstack_link_key_db_static = {
165     link_key_db_init,
166     link_key_db_set_local_bd_addr,
167     link_key_db_close,
168     link_key_db_get_link_key,
169     link_key_db_put_link_key,
170     link_key_db_delete_link_key,
171     link_key_db_tlv_iterator_init,
172     link_key_db_tlv_iterator_get_next,
173     link_key_db_tlv_iterator_done,
174 };
175 
176 const btstack_link_key_db_t * btstack_link_key_db_static_instance(void){
177     return &btstack_link_key_db_static;
178 }
179