xref: /btstack/src/classic/btstack_link_key_db_static.c (revision b28dc8004dd8d4fb9020a6dcd2bc81f05d36a008)
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     unsigned int buffer[LINK_KEY_LEN];
84 
85     // reset result buffer
86     memset(&buffer, 0, sizeof(buffer));
87 
88     // parse
89     int result = sscanf( (char *) link_key_string, "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
90                                     &buffer[0], &buffer[1], &buffer[2], &buffer[3],
91                                     &buffer[4], &buffer[5], &buffer[6], &buffer[7],
92                                     &buffer[8], &buffer[9], &buffer[10], &buffer[11],
93                                     &buffer[12], &buffer[13], &buffer[14], &buffer[15] );
94 
95     if (result != LINK_KEY_LEN) return 0;
96 
97     // store
98     int i;
99     uint8_t *p = (uint8_t *) link_key;
100     for (i=0; i<LINK_KEY_LEN; i++ ) {
101         *p++ = (uint8_t) buffer[i];
102     }
103     return 1;
104 }
105 
106 static void link_key_db_init(void){
107 }
108 
109 static void link_key_db_close(void){
110 }
111 
112 
113 // returns 1 if found
114 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) {
115 	int i;
116 	int num_entries = sizeof(link_key_db) / sizeof(link_key_entry_t);
117 
118 	for (i=0;i<num_entries;i++){
119 		if (strcmp(bd_addr_to_str(bd_addr), link_key_db[i].bd_addr)) continue;
120 		*link_key_type = (link_key_type_t) link_key_db[i].link_key_type;
121 		sscanf_link_key(link_key_db[i].link_key, link_key);
122 		return 1;
123 	}
124 	return 0;
125 }
126 
127 static void link_key_db_delete_link_key(bd_addr_t bd_addr){
128     (void) bd_addr;
129 }
130 
131 
132 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){
133 	log_info("Please add the following line to btstack_link_key_db.c");
134 	log_info("{ \"%s\", \"%s\", %u },\n", bd_addr_to_str(bd_addr), link_key_to_str(link_key), (int) link_key_type);
135 }
136 
137 static void link_key_db_set_local_bd_addr(bd_addr_t bd_addr){
138     (void) bd_addr;
139 }
140 
141 static int link_key_db_tlv_iterator_init(btstack_link_key_iterator_t * it){
142     it->context = (void*) 0;
143     return 1;
144 }
145 
146 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){
147     uintptr_t i = (uintptr_t) it->context;
148 
149     unsigned int num_entries = sizeof(link_key_db) / sizeof(link_key_entry_t);
150     if (i >= num_entries) return 0;
151 
152     // fetch values
153     *link_key_type = (link_key_type_t) link_key_db[i].link_key_type;
154     sscanf_link_key(link_key_db[i].link_key, link_key);
155     sscanf_bd_addr(link_key_db[i].bd_addr, bd_addr);
156 
157     // next
158     it->context = (void*) (i+1);
159     return 1;
160 }
161 
162 static void link_key_db_tlv_iterator_done(btstack_link_key_iterator_t * it){
163     UNUSED(it);
164 }
165 
166 static const btstack_link_key_db_t btstack_link_key_db_static = {
167     link_key_db_init,
168     link_key_db_set_local_bd_addr,
169     link_key_db_close,
170     link_key_db_get_link_key,
171     link_key_db_put_link_key,
172     link_key_db_delete_link_key,
173     link_key_db_tlv_iterator_init,
174     link_key_db_tlv_iterator_get_next,
175     link_key_db_tlv_iterator_done,
176 };
177 
178 const btstack_link_key_db_t * btstack_link_key_db_static_instance(void){
179     return &btstack_link_key_db_static;
180 }
181