xref: /btstack/platform/posix/btstack_link_key_db_fs.c (revision 360243be41f47158adff357b9fead2686419a2df)
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 "btstack_config.h"
46 #include "btstack_link_key_db_fs.h"
47 #include "btstack_debug.h"
48 #include "btstack_util.h"
49 
50 // allow to pre-set LINK_KEY_PATH from btstack_config.h
51 #ifndef LINK_KEY_PATH
52 #ifdef _WIN32
53 #define LINK_KEY_PATH ""
54 #else
55 #define LINK_KEY_PATH "/tmp/"
56 #endif
57 #endif
58 
59 #define LINK_KEY_PREFIX "btstack_at_"
60 #define LINK_KEY_FOR "_link_key_for_"
61 #define LINK_KEY_SUFFIX ".txt"
62 #define LINK_KEY_STRING_LEN 17
63 
64 static bd_addr_t local_addr;
65 // note: sizeof for string literals works at compile time while strlen only works with some optimizations turned on. sizeof inlcudes the  \0
66 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];
67 
68 static char bd_addr_to_dash_str_buffer[6*3];  // 12-45-78-01-34-67\0
69 static char * bd_addr_to_dash_str(bd_addr_t addr){
70     char * p = bd_addr_to_dash_str_buffer;
71     int i;
72     for (i = 0; i < 6 ; i++) {
73         *p++ = char_for_nibble((addr[i] >> 4) & 0x0F);
74         *p++ = char_for_nibble((addr[i] >> 0) & 0x0F);
75         *p++ = '-';
76     }
77     *--p = 0;
78     return (char *) bd_addr_to_dash_str_buffer;
79 }
80 
81 static char link_key_to_str_buffer[LINK_KEY_STR_LEN+1];  // 11223344556677889900112233445566\0
82 static char *link_key_to_str(link_key_t link_key){
83     char * p = link_key_to_str_buffer;
84     int i;
85     for (i = 0; i < LINK_KEY_LEN ; i++) {
86         *p++ = char_for_nibble((link_key[i] >> 4) & 0x0F);
87         *p++ = char_for_nibble((link_key[i] >> 0) & 0x0F);
88     }
89     *p = 0;
90     return (char *) link_key_to_str_buffer;
91 }
92 
93 static char link_key_type_to_str_buffer[2];
94 static char *link_key_type_to_str(link_key_type_t link_key){
95     snprintf(link_key_type_to_str_buffer, sizeof(link_key_type_to_str_buffer), "%d", link_key);
96     return (char *) link_key_type_to_str_buffer;
97 }
98 
99 static int sscanf_link_key(char * addr_string, link_key_t link_key){
100     unsigned int buffer[LINK_KEY_LEN];
101 
102     // reset result buffer
103     memset(&buffer, 0, sizeof(buffer));
104 
105     // parse
106     int result = sscanf( (char *) addr_string, "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
107                                     &buffer[0], &buffer[1], &buffer[2], &buffer[3],
108                                     &buffer[4], &buffer[5], &buffer[6], &buffer[7],
109                                     &buffer[8], &buffer[9], &buffer[10], &buffer[11],
110                                     &buffer[12], &buffer[13], &buffer[14], &buffer[15] );
111 
112     if (result != LINK_KEY_LEN) return 0;
113 
114     // store
115     int i;
116     uint8_t *p = (uint8_t *) link_key;
117     for (i=0; i<LINK_KEY_LEN; i++ ) {
118         *p++ = (uint8_t) buffer[i];
119     }
120     return 1;
121 }
122 
123 static void set_path(bd_addr_t bd_addr){
124     strcpy(keypath, LINK_KEY_PATH);
125     strcat(keypath, LINK_KEY_PREFIX);
126     strcat(keypath, bd_addr_to_dash_str(local_addr));
127     strcat(keypath, LINK_KEY_FOR);
128     strcat(keypath, bd_addr_to_dash_str(bd_addr));
129     strcat(keypath, LINK_KEY_SUFFIX);
130 }
131 
132 // Device info
133 static void db_open(void){
134 }
135 
136 static void db_set_local_bd_addr(bd_addr_t bd_addr){
137     memcpy(local_addr, bd_addr, 6);
138 }
139 
140 static void db_close(void){
141 }
142 
143 static void put_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t link_key_type){
144     set_path(bd_addr);
145     char * link_key_str = link_key_to_str(link_key);
146     char * link_key_type_str = link_key_type_to_str(link_key_type);
147 
148     FILE * wFile = fopen(keypath,"w+");
149     fwrite(link_key_str, strlen(link_key_str), 1, wFile);
150     fwrite(link_key_type_str, strlen(link_key_type_str), 1, wFile);
151     fclose(wFile);
152 }
153 
154 static int get_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * link_key_type) {
155     set_path(bd_addr);
156     if (access(keypath, R_OK)) return 0;
157 
158     char link_key_str[LINK_KEY_STR_LEN + 1];
159     char link_key_type_str[2];
160 
161     FILE * rFile = fopen(keypath,"r+");
162     size_t objects_read = fread(link_key_str, LINK_KEY_STR_LEN, 1, rFile );
163     if (objects_read == 1){
164         link_key_str[LINK_KEY_STR_LEN] = 0;
165         log_info("Found link key %s\n", link_key_str);
166         objects_read = fread(link_key_type_str, 1, 1, rFile );
167     }
168     fclose(rFile);
169 
170     if (objects_read != 1) return 0;
171     link_key_type_str[1] = 0;
172     log_info("Found link key type %s\n", link_key_type_str);
173 
174     int scan_result = sscanf_link_key(link_key_str, link_key);
175     if (scan_result == 0 ) return 0;
176 
177     int link_key_type_buffer;
178     scan_result = sscanf( (char *) link_key_type_str, "%d", &link_key_type_buffer);
179     if (scan_result == 0 ) return 0;
180     *link_key_type = (link_key_type_t) link_key_type_buffer;
181     return 1;
182 }
183 
184 static void delete_link_key(bd_addr_t bd_addr){
185     set_path(bd_addr);
186     if (access(keypath, R_OK)) return;
187 
188     if(remove(keypath) != 0){
189         log_error("File %s could not be deleted.\n", keypath);
190     }
191 }
192 
193 static const btstack_link_key_db_t btstack_link_key_db_fs = {
194     &db_open,
195     &db_set_local_bd_addr,
196     &db_close,
197     &get_link_key,
198     &put_link_key,
199     &delete_link_key,
200 };
201 
202 const btstack_link_key_db_t * btstack_link_key_db_fs_instance(void){
203     return &btstack_link_key_db_fs;
204 }
205 
206 
207