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