xref: /btstack/platform/posix/btstack_link_key_db_fs.c (revision d08566fb6ea6a752bc4a7c70151e1384b4a55b0a)
1 /*
2  * Copyright (C) 2014 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_fs.c"
39 
40 #include <string.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <unistd.h>
44 
45 #include "tinydir.h"
46 
47 #include "btstack_config.h"
48 #include "btstack_link_key_db_fs.h"
49 #include "btstack_debug.h"
50 #include "btstack_util.h"
51 
52 // allow to pre-set LINK_KEY_PATH from btstack_config.h
53 #ifndef LINK_KEY_PATH
54 #ifdef _WIN32
55 #define LINK_KEY_PATH ""
56 #else
57 #define LINK_KEY_PATH "/tmp/"
58 #endif
59 #endif
60 
61 #define LINK_KEY_PREFIX "btstack_at_"
62 #define LINK_KEY_FOR "_link_key_for_"
63 #define LINK_KEY_SUFFIX ".txt"
64 #define LINK_KEY_STRING_LEN 17
65 
66 static bd_addr_t local_addr;
67 // note: sizeof for string literals works at compile time while strlen only works with some optimizations turned on. sizeof includes the  \0
68 static char keypath[sizeof(LINK_KEY_PATH) + sizeof(LINK_KEY_PREFIX) + LINK_KEY_STRING_LEN + sizeof(LINK_KEY_FOR) + LINK_KEY_STRING_LEN + sizeof(LINK_KEY_SUFFIX) + 1];
69 
70 static char bd_addr_to_dash_str_buffer[6*3];  // 12-45-78-01-34-67\0
71 static char * bd_addr_to_dash_str(bd_addr_t addr){
72     char * p = bd_addr_to_dash_str_buffer;
73     int i;
74     for (i = 0; i < 6 ; i++) {
75         *p++ = char_for_nibble((addr[i] >> 4) & 0x0F);
76         *p++ = char_for_nibble((addr[i] >> 0) & 0x0F);
77         *p++ = '-';
78     }
79     *--p = 0;
80     return (char *) bd_addr_to_dash_str_buffer;
81 }
82 
83 static char link_key_to_str_buffer[LINK_KEY_STR_LEN+1];  // 11223344556677889900112233445566\0
84 static char *link_key_to_str(link_key_t link_key){
85     char * p = link_key_to_str_buffer;
86     int i;
87     for (i = 0; i < LINK_KEY_LEN ; i++) {
88         *p++ = char_for_nibble((link_key[i] >> 4) & 0x0F);
89         *p++ = char_for_nibble((link_key[i] >> 0) & 0x0F);
90     }
91     *p = 0;
92     return (char *) link_key_to_str_buffer;
93 }
94 
95 static char link_key_type_to_str_buffer[2];
96 static char *link_key_type_to_str(link_key_type_t link_key){
97     snprintf(link_key_type_to_str_buffer, sizeof(link_key_type_to_str_buffer), "%d", link_key);
98     return (char *) link_key_type_to_str_buffer;
99 }
100 
101 static int sscanf_link_key(char * addr_string, link_key_t link_key){
102     unsigned int buffer[LINK_KEY_LEN];
103 
104     // reset result buffer
105     memset(&buffer, 0, sizeof(buffer));
106 
107     // parse
108     int result = sscanf( (char *) addr_string, "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
109                                     &buffer[0], &buffer[1], &buffer[2], &buffer[3],
110                                     &buffer[4], &buffer[5], &buffer[6], &buffer[7],
111                                     &buffer[8], &buffer[9], &buffer[10], &buffer[11],
112                                     &buffer[12], &buffer[13], &buffer[14], &buffer[15] );
113 
114     if (result != LINK_KEY_LEN) return 0;
115 
116     // store
117     int i;
118     uint8_t *p = (uint8_t *) link_key;
119     for (i=0; i<LINK_KEY_LEN; i++ ) {
120         *p++ = (uint8_t) buffer[i];
121     }
122     return 1;
123 }
124 
125 static void set_path(bd_addr_t bd_addr){
126     strcpy(keypath, LINK_KEY_PATH);
127     strcat(keypath, LINK_KEY_PREFIX);
128     strcat(keypath, bd_addr_to_dash_str(local_addr));
129     strcat(keypath, LINK_KEY_FOR);
130     strcat(keypath, bd_addr_to_dash_str(bd_addr));
131     strcat(keypath, LINK_KEY_SUFFIX);
132 }
133 
134 // Device info
135 static void db_open(void){
136 }
137 
138 static void db_set_local_bd_addr(bd_addr_t bd_addr){
139     memcpy(local_addr, bd_addr, 6);
140 }
141 
142 static void db_close(void){
143 }
144 
145 static void put_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t link_key_type){
146     set_path(bd_addr);
147     char * link_key_str = link_key_to_str(link_key);
148     char * link_key_type_str = link_key_type_to_str(link_key_type);
149 
150     FILE * wFile = fopen(keypath,"w+");
151     fwrite(link_key_str, strlen(link_key_str), 1, wFile);
152     fwrite(link_key_type_str, strlen(link_key_type_str), 1, wFile);
153     fclose(wFile);
154 }
155 
156 static int read_link_key(const char * path, link_key_t link_key, link_key_type_t * link_key_type){
157     if (access(path, R_OK)) return 0;
158 
159     char link_key_str[LINK_KEY_STR_LEN + 1];
160     char link_key_type_str[2];
161 
162     FILE * rFile = fopen(path,"r+");
163     size_t objects_read = fread(link_key_str, LINK_KEY_STR_LEN, 1, rFile );
164     if (objects_read == 1){
165         link_key_str[LINK_KEY_STR_LEN] = 0;
166         log_info("Found link key %s\n", link_key_str);
167         objects_read = fread(link_key_type_str, 1, 1, rFile );
168     }
169     fclose(rFile);
170 
171     if (objects_read != 1) return 0;
172     link_key_type_str[1] = 0;
173     log_info("Found link key type %s\n", link_key_type_str);
174 
175     int scan_result = sscanf_link_key(link_key_str, link_key);
176     if (scan_result == 0 ) return 0;
177 
178     int link_key_type_buffer;
179     scan_result = sscanf( (char *) link_key_type_str, "%d", &link_key_type_buffer);
180     if (scan_result == 0 ) return 0;
181     *link_key_type = (link_key_type_t) link_key_type_buffer;
182     return 1;
183 }
184 
185 static int get_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * link_key_type) {
186     set_path(bd_addr);
187     return read_link_key(keypath, link_key, link_key_type);
188 }
189 
190 static void delete_link_key(bd_addr_t bd_addr){
191     set_path(bd_addr);
192     if (access(keypath, R_OK)) return;
193     if(remove(keypath) != 0){
194         log_error("File %s could not be deleted.\n", keypath);
195     }
196 }
197 
198 static int iterator_init(btstack_link_key_iterator_t * it){
199     tinydir_dir * dir = malloc(sizeof(tinydir_dir));
200     if (!dir) return 0;
201     it->context = dir;
202     tinydir_open(dir, LINK_KEY_PATH);
203     return 1;
204 }
205 
206 static int  iterator_get_next(btstack_link_key_iterator_t * it, bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * type){
207     (void)bd_addr;
208     (void)link_key;
209     UNUSED(type);
210     tinydir_dir * dir = (tinydir_dir*) it->context;
211 
212     // construct prefix
213     strcpy(keypath, LINK_KEY_PREFIX);
214     strcat(keypath, bd_addr_to_dash_str(local_addr));
215     strcat(keypath, LINK_KEY_FOR);
216 
217     while (dir->has_next) {
218         tinydir_file file;
219         tinydir_readfile(dir, &file);
220         tinydir_next(dir);
221         // compare
222         if (strncmp(keypath, file.name, strlen(keypath)) == 0){
223             // parse bd_addr
224             const int addr_offset = sizeof(LINK_KEY_PREFIX) + LINK_KEY_STRING_LEN + sizeof(LINK_KEY_FOR) - 2;   // -1 for each sizeof
225             sscanf_bd_addr(&file.name[addr_offset], bd_addr);
226             // path found, read file
227             strcpy(keypath, LINK_KEY_PATH);
228             strcat(keypath, file.name);
229             read_link_key(keypath, link_key, type);
230             return 1;
231         }
232     }
233     return 0;
234 }
235 
236 static void iterator_done(btstack_link_key_iterator_t * it){
237     tinydir_close((tinydir_dir*)it->context);
238     free(it->context);
239     it->context = NULL;
240 }
241 
242 static const btstack_link_key_db_t btstack_link_key_db_fs = {
243     &db_open,
244     &db_set_local_bd_addr,
245     &db_close,
246     &get_link_key,
247     &put_link_key,
248     &delete_link_key,
249     &iterator_init,
250     &iterator_get_next,
251     &iterator_done,
252 };
253 
254 const btstack_link_key_db_t * btstack_link_key_db_fs_instance(void){
255     return &btstack_link_key_db_fs;
256 }
257 
258 
259