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