• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (C) 2014 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #define LOG_TAG "bt_btif_config"
20 
21 #include <assert.h>
22 #include <ctype.h>
23 #include <pthread.h>
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include "osi/include/alarm.h"
28 #include "osi/include/allocator.h"
29 #include "btcore/include/bdaddr.h"
30 #include "btif_api.h"
31 #include "btif_config.h"
32 #include "btif_config_transcode.h"
33 #include "btif_util.h"
34 #include "osi/include/compat.h"
35 #include "osi/include/config.h"
36 #include "btcore/include/module.h"
37 #include "osi/include/osi.h"
38 #include "osi/include/log.h"
39 
40 #include "bt_types.h"
41 
42 static const char *CONFIG_FILE_PATH = "/data/misc/bluedroid/bt_config.conf";
43 static const char *LEGACY_CONFIG_FILE_PATH = "/data/misc/bluedroid/bt_config.xml";
44 static const period_ms_t CONFIG_SETTLE_PERIOD_MS = 3000;
45 
46 static void timer_config_save_cb(void *data);
47 static void btif_config_write(void);
48 static void btif_config_remove_unpaired(config_t *config);
49 static void btif_config_remove_restricted(config_t *config);
50 
51 // TODO(zachoverflow): Move these two functions out, because they are too specific for this file
52 // {grumpy-cat/no, monty-python/you-make-me-sad}
btif_get_device_type(const BD_ADDR bd_addr,int * p_device_type)53 bool btif_get_device_type(const BD_ADDR bd_addr, int *p_device_type)
54 {
55     if (p_device_type == NULL)
56         return FALSE;
57 
58     bt_bdaddr_t bda;
59     bdcpy(bda.address, bd_addr);
60 
61     bdstr_t bd_addr_str;
62     bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str));
63 
64     if (!btif_config_get_int(bd_addr_str, "DevType", p_device_type))
65         return FALSE;
66 
67     LOG_DEBUG("%s: Device [%s] type %d", __FUNCTION__, bd_addr_str, *p_device_type);
68     return TRUE;
69 }
70 
btif_get_address_type(const BD_ADDR bd_addr,int * p_addr_type)71 bool btif_get_address_type(const BD_ADDR bd_addr, int *p_addr_type)
72 {
73     if (p_addr_type == NULL)
74         return FALSE;
75 
76     bt_bdaddr_t bda;
77     bdcpy(bda.address, bd_addr);
78 
79     bdstr_t bd_addr_str;
80     bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str));
81 
82     if (!btif_config_get_int(bd_addr_str, "AddrType", p_addr_type))
83         return FALSE;
84 
85     LOG_DEBUG("%s: Device [%s] address type %d", __FUNCTION__, bd_addr_str, *p_addr_type);
86     return TRUE;
87 }
88 
89 static pthread_mutex_t lock;  // protects operations on |config|.
90 static config_t *config;
91 static alarm_t *alarm_timer;
92 
93 // Module lifecycle functions
94 
init(void)95 static future_t *init(void) {
96   pthread_mutex_init(&lock, NULL);
97   config = config_new(CONFIG_FILE_PATH);
98   if (!config) {
99     LOG_WARN("%s unable to load config file; attempting to transcode legacy file.", __func__);
100     config = btif_config_transcode(LEGACY_CONFIG_FILE_PATH);
101     if (!config) {
102       LOG_WARN("%s unable to transcode legacy file, starting unconfigured.", __func__);
103       config = config_new_empty();
104       if (!config) {
105         LOG_ERROR("%s unable to allocate a config object.", __func__);
106         goto error;
107       }
108     }
109 
110     if (config_save(config, CONFIG_FILE_PATH))
111       unlink(LEGACY_CONFIG_FILE_PATH);
112   }
113 
114   btif_config_remove_unpaired(config);
115 
116   // Cleanup temporary pairings if we have left guest mode
117   if (!is_restricted_mode())
118     btif_config_remove_restricted(config);
119 
120   // TODO(sharvil): use a non-wake alarm for this once we have
121   // API support for it. There's no need to wake the system to
122   // write back to disk.
123   alarm_timer = alarm_new();
124   if (!alarm_timer) {
125     LOG_ERROR("%s unable to create alarm.", __func__);
126     goto error;
127   }
128 
129   return future_new_immediate(FUTURE_SUCCESS);
130 
131 error:;
132   alarm_free(alarm_timer);
133   config_free(config);
134   pthread_mutex_destroy(&lock);
135   alarm_timer = NULL;
136   config = NULL;
137   return future_new_immediate(FUTURE_FAIL);
138 }
139 
shut_down(void)140 static future_t *shut_down(void) {
141   btif_config_flush();
142   return future_new_immediate(FUTURE_SUCCESS);
143 }
144 
clean_up(void)145 static future_t *clean_up(void) {
146   btif_config_flush();
147 
148   alarm_free(alarm_timer);
149   config_free(config);
150   pthread_mutex_destroy(&lock);
151   alarm_timer = NULL;
152   config = NULL;
153   return future_new_immediate(FUTURE_SUCCESS);
154 }
155 
156 const module_t btif_config_module = {
157   .name = BTIF_CONFIG_MODULE,
158   .init = init,
159   .start_up = NULL,
160   .shut_down = shut_down,
161   .clean_up = clean_up,
162   .dependencies = {
163     NULL
164   }
165 };
166 
btif_config_has_section(const char * section)167 bool btif_config_has_section(const char *section) {
168   assert(config != NULL);
169   assert(section != NULL);
170 
171   pthread_mutex_lock(&lock);
172   bool ret = config_has_section(config, section);
173   pthread_mutex_unlock(&lock);
174 
175   return ret;
176 }
177 
btif_config_exist(const char * section,const char * key)178 bool btif_config_exist(const char *section, const char *key) {
179   assert(config != NULL);
180   assert(section != NULL);
181   assert(key != NULL);
182 
183   pthread_mutex_lock(&lock);
184   bool ret = config_has_key(config, section, key);
185   pthread_mutex_unlock(&lock);
186 
187   return ret;
188 }
189 
btif_config_get_int(const char * section,const char * key,int * value)190 bool btif_config_get_int(const char *section, const char *key, int *value) {
191   assert(config != NULL);
192   assert(section != NULL);
193   assert(key != NULL);
194   assert(value != NULL);
195 
196   pthread_mutex_lock(&lock);
197   bool ret = config_has_key(config, section, key);
198   if (ret)
199     *value = config_get_int(config, section, key, *value);
200   pthread_mutex_unlock(&lock);
201 
202   return ret;
203 }
204 
btif_config_set_int(const char * section,const char * key,int value)205 bool btif_config_set_int(const char *section, const char *key, int value) {
206   assert(config != NULL);
207   assert(section != NULL);
208   assert(key != NULL);
209 
210   pthread_mutex_lock(&lock);
211   config_set_int(config, section, key, value);
212   pthread_mutex_unlock(&lock);
213 
214   return true;
215 }
216 
btif_config_get_str(const char * section,const char * key,char * value,int * size_bytes)217 bool btif_config_get_str(const char *section, const char *key, char *value, int *size_bytes) {
218   assert(config != NULL);
219   assert(section != NULL);
220   assert(key != NULL);
221   assert(value != NULL);
222   assert(size_bytes != NULL);
223 
224   pthread_mutex_lock(&lock);
225   const char *stored_value = config_get_string(config, section, key, NULL);
226   pthread_mutex_unlock(&lock);
227 
228   if (!stored_value)
229     return false;
230 
231   strlcpy(value, stored_value, *size_bytes);
232   *size_bytes = strlen(value) + 1;
233 
234   return true;
235 }
236 
btif_config_set_str(const char * section,const char * key,const char * value)237 bool btif_config_set_str(const char *section, const char *key, const char *value) {
238   assert(config != NULL);
239   assert(section != NULL);
240   assert(key != NULL);
241   assert(value != NULL);
242 
243   pthread_mutex_lock(&lock);
244   config_set_string(config, section, key, value);
245   pthread_mutex_unlock(&lock);
246 
247   return true;
248 }
249 
btif_config_get_bin(const char * section,const char * key,uint8_t * value,size_t * length)250 bool btif_config_get_bin(const char *section, const char *key, uint8_t *value, size_t *length) {
251   assert(config != NULL);
252   assert(section != NULL);
253   assert(key != NULL);
254   assert(value != NULL);
255   assert(length != NULL);
256 
257   pthread_mutex_lock(&lock);
258   const char *value_str = config_get_string(config, section, key, NULL);
259   pthread_mutex_unlock(&lock);
260 
261   if (!value_str)
262     return false;
263 
264   size_t value_len = strlen(value_str);
265   if ((value_len % 2) != 0 || *length < (value_len / 2))
266     return false;
267 
268   for (size_t i = 0; i < value_len; ++i)
269     if (!isxdigit(value_str[i]))
270       return false;
271 
272   for (*length = 0; *value_str; value_str += 2, *length += 1)
273     sscanf(value_str, "%02hhx", &value[*length]);
274 
275   return true;
276 }
277 
btif_config_get_bin_length(const char * section,const char * key)278 size_t btif_config_get_bin_length(const char *section, const char *key) {
279   assert(config != NULL);
280   assert(section != NULL);
281   assert(key != NULL);
282 
283   pthread_mutex_lock(&lock);
284   const char *value_str = config_get_string(config, section, key, NULL);
285   pthread_mutex_unlock(&lock);
286 
287   if (!value_str)
288     return 0;
289 
290   size_t value_len = strlen(value_str);
291   return ((value_len % 2) != 0) ? 0 : (value_len / 2);
292 }
293 
btif_config_set_bin(const char * section,const char * key,const uint8_t * value,size_t length)294 bool btif_config_set_bin(const char *section, const char *key, const uint8_t *value, size_t length) {
295   const char *lookup = "0123456789abcdef";
296 
297   assert(config != NULL);
298   assert(section != NULL);
299   assert(key != NULL);
300 
301   if (length > 0)
302       assert(value != NULL);
303 
304   char *str = (char *)osi_calloc(length * 2 + 1);
305   if (!str)
306     return false;
307 
308   for (size_t i = 0; i < length; ++i) {
309     str[(i * 2) + 0] = lookup[(value[i] >> 4) & 0x0F];
310     str[(i * 2) + 1] = lookup[value[i] & 0x0F];
311   }
312 
313   pthread_mutex_lock(&lock);
314   config_set_string(config, section, key, str);
315   pthread_mutex_unlock(&lock);
316 
317   osi_free(str);
318   return true;
319 }
320 
btif_config_section_begin(void)321 const btif_config_section_iter_t *btif_config_section_begin(void) {
322   assert(config != NULL);
323   return (const btif_config_section_iter_t *)config_section_begin(config);
324 }
325 
btif_config_section_end(void)326 const btif_config_section_iter_t *btif_config_section_end(void) {
327   assert(config != NULL);
328   return (const btif_config_section_iter_t *)config_section_end(config);
329 }
330 
btif_config_section_next(const btif_config_section_iter_t * section)331 const btif_config_section_iter_t *btif_config_section_next(const btif_config_section_iter_t *section) {
332   assert(config != NULL);
333   assert(section != NULL);
334   return (const btif_config_section_iter_t *)config_section_next((const config_section_node_t *)section);
335 }
336 
btif_config_section_name(const btif_config_section_iter_t * section)337 const char *btif_config_section_name(const btif_config_section_iter_t *section) {
338   assert(config != NULL);
339   assert(section != NULL);
340   return config_section_name((const config_section_node_t *)section);
341 }
342 
btif_config_remove(const char * section,const char * key)343 bool btif_config_remove(const char *section, const char *key) {
344   assert(config != NULL);
345   assert(section != NULL);
346   assert(key != NULL);
347 
348   pthread_mutex_lock(&lock);
349   bool ret = config_remove_key(config, section, key);
350   pthread_mutex_unlock(&lock);
351 
352   return ret;
353 }
354 
btif_config_save(void)355 void btif_config_save(void) {
356   assert(alarm_timer != NULL);
357   assert(config != NULL);
358 
359   alarm_set(alarm_timer, CONFIG_SETTLE_PERIOD_MS, timer_config_save_cb, NULL);
360 }
361 
btif_config_flush(void)362 void btif_config_flush(void) {
363   assert(config != NULL);
364   assert(alarm_timer != NULL);
365 
366   alarm_cancel(alarm_timer);
367   btif_config_write();
368 }
369 
btif_config_clear(void)370 int btif_config_clear(void){
371   assert(config != NULL);
372   assert(alarm_timer != NULL);
373 
374   alarm_cancel(alarm_timer);
375 
376   pthread_mutex_lock(&lock);
377   config_free(config);
378 
379   config = config_new_empty();
380   if (config == NULL) {
381     pthread_mutex_unlock(&lock);
382     return false;
383   }
384 
385   int ret = config_save(config, CONFIG_FILE_PATH);
386   pthread_mutex_unlock(&lock);
387   return ret;
388 }
389 
timer_config_save_cb(UNUSED_ATTR void * data)390 static void timer_config_save_cb(UNUSED_ATTR void *data) {
391   btif_config_write();
392 }
393 
btif_config_write(void)394 static void btif_config_write(void) {
395   assert(config != NULL);
396   assert(alarm_timer != NULL);
397 
398   pthread_mutex_lock(&lock);
399   config_t *config_paired = config_new_clone(config);
400   btif_config_remove_unpaired(config_paired);
401   config_save(config_paired, CONFIG_FILE_PATH);
402   config_free(config_paired);
403   pthread_mutex_unlock(&lock);
404 }
405 
btif_config_remove_unpaired(config_t * conf)406 static void btif_config_remove_unpaired(config_t *conf) {
407   assert(conf != NULL);
408 
409   // The paired config used to carry information about
410   // discovered devices during regular inquiry scans.
411   // We remove these now and cache them in memory instead.
412   const config_section_node_t *snode = config_section_begin(conf);
413   while (snode != config_section_end(conf)) {
414     const char *section = config_section_name(snode);
415     if (string_is_bdaddr(section)) {
416       if (!config_has_key(conf, section, "LinkKey") &&
417           !config_has_key(conf, section, "LE_KEY_PENC") &&
418           !config_has_key(conf, section, "LE_KEY_PID") &&
419           !config_has_key(conf, section, "LE_KEY_PCSRK") &&
420           !config_has_key(conf, section, "LE_KEY_LENC") &&
421           !config_has_key(conf, section, "LE_KEY_LCSRK")) {
422         snode = config_section_next(snode);
423         config_remove_section(conf, section);
424         continue;
425       }
426     }
427     snode = config_section_next(snode);
428   }
429 }
430 
btif_config_remove_restricted(config_t * config)431 static void btif_config_remove_restricted(config_t* config) {
432   assert(config != NULL);
433 
434   pthread_mutex_lock(&lock);
435   const config_section_node_t *snode = config_section_begin(config);
436   while (snode != config_section_end(config)) {
437     const char *section = config_section_name(snode);
438     if (string_is_bdaddr(section) && config_has_key(config, section, "Restricted")) {
439         BTIF_TRACE_DEBUG("%s: Removing restricted device %s", __func__, section);
440         config_remove_section(config, section);
441     }
442     snode = config_section_next(snode);
443   }
444   pthread_mutex_unlock(&lock);
445 }
446