OSDN Git Service

Fix double mutex unlock and if() condition
[android-x86/system-bt.git] / btif / src / btif_config.c
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 "btif_config.h"
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <pthread.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <time.h>
29 #include <unistd.h>
30
31 #include "bt_types.h"
32 #include "btcore/include/bdaddr.h"
33 #include "btcore/include/module.h"
34 #include "btif_api.h"
35 #include "btif_common.h"
36 #include "btif_config.h"
37 #include "btif_config_transcode.h"
38 #include "btif_util.h"
39 #include "osi/include/alarm.h"
40 #include "osi/include/allocator.h"
41 #include "osi/include/compat.h"
42 #include "osi/include/config.h"
43 #include "osi/include/log.h"
44 #include "osi/include/osi.h"
45
46 /**
47  * TODO(apanicke): cutils/properties.h is only being used to pull-in runtime
48  * settings on Android. Remove this conditional include once we have a generic
49  * way to obtain system properties.
50  */
51 #if !defined(OS_GENERIC)
52 #include <cutils/properties.h>
53 #endif  /* !defined(OS_GENERIC) */
54
55 #define INFO_SECTION "Info"
56 #define FILE_TIMESTAMP "TimeCreated"
57 #define TIME_STRING_LENGTH sizeof("YYYY-MM-DD HH:MM:SS")
58 static const char* TIME_STRING_FORMAT = "%Y-%m-%d %H:%M:%S";
59
60 // TODO(armansito): Find a better way than searching by a hardcoded path.
61 #if defined(OS_GENERIC)
62 static const char *CONFIG_FILE_PATH = "bt_config.conf";
63 static const char *CONFIG_BACKUP_PATH = "bt_config.bak";
64 static const char *CONFIG_LEGACY_FILE_PATH = "bt_config.xml";
65 #else  // !defined(OS_GENERIC)
66 static const char *CONFIG_FILE_PATH = "/data/misc/bluedroid/bt_config.conf";
67 static const char *CONFIG_BACKUP_PATH = "/data/misc/bluedroid/bt_config.bak";
68 static const char *CONFIG_LEGACY_FILE_PATH = "/data/misc/bluedroid/bt_config.xml";
69 #endif  // defined(OS_GENERIC)
70 static const period_ms_t CONFIG_SETTLE_PERIOD_MS = 3000;
71
72 static void timer_config_save_cb(void *data);
73 static void btif_config_write(UINT16 event, char *p_param);
74 static bool is_factory_reset(void);
75 static void delete_config_files(void);
76 static void btif_config_remove_unpaired(config_t *config);
77 static void btif_config_remove_restricted(config_t *config);
78
79 static enum ConfigSource {
80   NOT_LOADED,
81   ORIGINAL,
82   BACKUP,
83   LEGACY,
84   NEW_FILE,
85   RESET
86 } btif_config_source = NOT_LOADED;
87
88 static int btif_config_devices_loaded = -1;
89 static char btif_config_time_created[TIME_STRING_LENGTH];
90
91 // TODO(zachoverflow): Move these two functions out, because they are too specific for this file
92 // {grumpy-cat/no, monty-python/you-make-me-sad}
93 bool btif_get_device_type(const BD_ADDR bd_addr, int *p_device_type)
94 {
95     if (p_device_type == NULL)
96         return FALSE;
97
98     bt_bdaddr_t bda;
99     bdcpy(bda.address, bd_addr);
100
101     bdstr_t bd_addr_str;
102     bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str));
103
104     if (!btif_config_get_int(bd_addr_str, "DevType", p_device_type))
105         return FALSE;
106
107     LOG_DEBUG(LOG_TAG, "%s: Device [%s] type %d", __FUNCTION__, bd_addr_str, *p_device_type);
108     return TRUE;
109 }
110
111 bool btif_get_address_type(const BD_ADDR bd_addr, int *p_addr_type)
112 {
113     if (p_addr_type == NULL)
114         return FALSE;
115
116     bt_bdaddr_t bda;
117     bdcpy(bda.address, bd_addr);
118
119     bdstr_t bd_addr_str;
120     bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str));
121
122     if (!btif_config_get_int(bd_addr_str, "AddrType", p_addr_type))
123         return FALSE;
124
125     LOG_DEBUG(LOG_TAG, "%s: Device [%s] address type %d", __FUNCTION__, bd_addr_str, *p_addr_type);
126     return TRUE;
127 }
128
129 static pthread_mutex_t lock;  // protects operations on |config|.
130 static config_t *config;
131 static alarm_t *config_timer;
132
133 // Module lifecycle functions
134
135 static future_t *init(void) {
136   pthread_mutex_init(&lock, NULL);
137   pthread_mutex_lock(&lock);
138
139   if (is_factory_reset())
140     delete_config_files();
141
142   config = config_new(CONFIG_FILE_PATH);
143   btif_config_source = ORIGINAL;
144   if (!config) {
145     LOG_WARN("%s unable to load config file: %s; using backup.",
146               __func__, CONFIG_FILE_PATH);
147     config = config_new(CONFIG_BACKUP_PATH);
148     btif_config_source = BACKUP;
149   }
150   if (!config) {
151     LOG_WARN("%s unable to load backup; attempting to transcode legacy file.", __func__);
152     config = btif_config_transcode(CONFIG_LEGACY_FILE_PATH);
153     btif_config_source = LEGACY;
154   }
155   if (!config) {
156     LOG_ERROR("%s unable to transcode legacy file; creating empty config.", __func__);
157     config = config_new_empty();
158     btif_config_source = NEW_FILE;
159   }
160   if (!config) {
161     LOG_ERROR("%s unable to allocate a config object.", __func__);
162     goto error;
163   }
164
165   btif_config_remove_unpaired(config);
166
167   // Cleanup temporary pairings if we have left guest mode
168   if (!is_restricted_mode())
169     btif_config_remove_restricted(config);
170
171   // Read or set config file creation timestamp
172   const char* time_str = config_get_string(config, INFO_SECTION, FILE_TIMESTAMP, NULL);
173   if (time_str != NULL) {
174     strlcpy(btif_config_time_created, time_str, TIME_STRING_LENGTH);
175   } else {
176     time_t current_time = time(NULL);
177     struct tm* time_created = localtime(&current_time);
178     strftime(btif_config_time_created, TIME_STRING_LENGTH, TIME_STRING_FORMAT, time_created);
179     config_set_string(config, INFO_SECTION, FILE_TIMESTAMP, btif_config_time_created);
180   }
181
182   // TODO(sharvil): use a non-wake alarm for this once we have
183   // API support for it. There's no need to wake the system to
184   // write back to disk.
185   config_timer = alarm_new("btif.config");
186   if (!config_timer) {
187     LOG_ERROR(LOG_TAG, "%s unable to create alarm.", __func__);
188     goto error;
189   }
190
191   pthread_mutex_unlock(&lock);
192   return future_new_immediate(FUTURE_SUCCESS);
193
194 error:
195   alarm_free(config_timer);
196   config_free(config);
197   pthread_mutex_unlock(&lock);
198   pthread_mutex_destroy(&lock);
199   config_timer = NULL;
200   config = NULL;
201   btif_config_source = NOT_LOADED;
202   return future_new_immediate(FUTURE_FAIL);
203 }
204
205 static future_t *shut_down(void) {
206   btif_config_flush();
207   return future_new_immediate(FUTURE_SUCCESS);
208 }
209
210 static future_t *clean_up(void) {
211   btif_config_flush();
212
213   alarm_free(config_timer);
214   config_free(config);
215   pthread_mutex_destroy(&lock);
216   config_timer = NULL;
217   config = NULL;
218   return future_new_immediate(FUTURE_SUCCESS);
219 }
220
221 EXPORT_SYMBOL const module_t btif_config_module = {
222   .name = BTIF_CONFIG_MODULE,
223   .init = init,
224   .start_up = NULL,
225   .shut_down = shut_down,
226   .clean_up = clean_up,
227   .dependencies = {
228     NULL
229   }
230 };
231
232 bool btif_config_has_section(const char *section) {
233   assert(config != NULL);
234   assert(section != NULL);
235
236   pthread_mutex_lock(&lock);
237   bool ret = config_has_section(config, section);
238   pthread_mutex_unlock(&lock);
239
240   return ret;
241 }
242
243 bool btif_config_exist(const char *section, const char *key) {
244   assert(config != NULL);
245   assert(section != NULL);
246   assert(key != NULL);
247
248   pthread_mutex_lock(&lock);
249   bool ret = config_has_key(config, section, key);
250   pthread_mutex_unlock(&lock);
251
252   return ret;
253 }
254
255 bool btif_config_get_int(const char *section, const char *key, int *value) {
256   assert(config != NULL);
257   assert(section != NULL);
258   assert(key != NULL);
259   assert(value != NULL);
260
261   pthread_mutex_lock(&lock);
262   bool ret = config_has_key(config, section, key);
263   if (ret)
264     *value = config_get_int(config, section, key, *value);
265   pthread_mutex_unlock(&lock);
266
267   return ret;
268 }
269
270 bool btif_config_set_int(const char *section, const char *key, int value) {
271   assert(config != NULL);
272   assert(section != NULL);
273   assert(key != NULL);
274
275   pthread_mutex_lock(&lock);
276   config_set_int(config, section, key, value);
277   pthread_mutex_unlock(&lock);
278
279   return true;
280 }
281
282 bool btif_config_get_str(const char *section, const char *key, char *value, int *size_bytes) {
283   assert(config != NULL);
284   assert(section != NULL);
285   assert(key != NULL);
286   assert(value != NULL);
287   assert(size_bytes != NULL);
288
289   pthread_mutex_lock(&lock);
290   const char *stored_value = config_get_string(config, section, key, NULL);
291   pthread_mutex_unlock(&lock);
292
293   if (!stored_value)
294     return false;
295
296   strlcpy(value, stored_value, *size_bytes);
297   *size_bytes = strlen(value) + 1;
298
299   return true;
300 }
301
302 bool btif_config_set_str(const char *section, const char *key, const char *value) {
303   assert(config != NULL);
304   assert(section != NULL);
305   assert(key != NULL);
306   assert(value != NULL);
307
308   pthread_mutex_lock(&lock);
309   config_set_string(config, section, key, value);
310   pthread_mutex_unlock(&lock);
311
312   return true;
313 }
314
315 bool btif_config_get_bin(const char *section, const char *key, uint8_t *value, size_t *length) {
316   assert(config != NULL);
317   assert(section != NULL);
318   assert(key != NULL);
319   assert(value != NULL);
320   assert(length != NULL);
321
322   pthread_mutex_lock(&lock);
323   const char *value_str = config_get_string(config, section, key, NULL);
324   pthread_mutex_unlock(&lock);
325
326   if (!value_str)
327     return false;
328
329   size_t value_len = strlen(value_str);
330   if ((value_len % 2) != 0 || *length < (value_len / 2))
331     return false;
332
333   for (size_t i = 0; i < value_len; ++i)
334     if (!isxdigit(value_str[i]))
335       return false;
336
337   for (*length = 0; *value_str; value_str += 2, *length += 1)
338     sscanf(value_str, "%02hhx", &value[*length]);
339
340   return true;
341 }
342
343 size_t btif_config_get_bin_length(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   const char *value_str = config_get_string(config, section, key, NULL);
350   pthread_mutex_unlock(&lock);
351
352   if (!value_str)
353     return 0;
354
355   size_t value_len = strlen(value_str);
356   return ((value_len % 2) != 0) ? 0 : (value_len / 2);
357 }
358
359 bool btif_config_set_bin(const char *section, const char *key, const uint8_t *value, size_t length) {
360   const char *lookup = "0123456789abcdef";
361
362   assert(config != NULL);
363   assert(section != NULL);
364   assert(key != NULL);
365
366   if (length > 0)
367       assert(value != NULL);
368
369   char *str = (char *)osi_calloc(length * 2 + 1);
370
371   for (size_t i = 0; i < length; ++i) {
372     str[(i * 2) + 0] = lookup[(value[i] >> 4) & 0x0F];
373     str[(i * 2) + 1] = lookup[value[i] & 0x0F];
374   }
375
376   pthread_mutex_lock(&lock);
377   config_set_string(config, section, key, str);
378   pthread_mutex_unlock(&lock);
379
380   osi_free(str);
381   return true;
382 }
383
384 const btif_config_section_iter_t *btif_config_section_begin(void) {
385   assert(config != NULL);
386   return (const btif_config_section_iter_t *)config_section_begin(config);
387 }
388
389 const btif_config_section_iter_t *btif_config_section_end(void) {
390   assert(config != NULL);
391   return (const btif_config_section_iter_t *)config_section_end(config);
392 }
393
394 const btif_config_section_iter_t *btif_config_section_next(const btif_config_section_iter_t *section) {
395   assert(config != NULL);
396   assert(section != NULL);
397   return (const btif_config_section_iter_t *)config_section_next((const config_section_node_t *)section);
398 }
399
400 const char *btif_config_section_name(const btif_config_section_iter_t *section) {
401   assert(config != NULL);
402   assert(section != NULL);
403   return config_section_name((const config_section_node_t *)section);
404 }
405
406 bool btif_config_remove(const char *section, const char *key) {
407   assert(config != NULL);
408   assert(section != NULL);
409   assert(key != NULL);
410
411   pthread_mutex_lock(&lock);
412   bool ret = config_remove_key(config, section, key);
413   pthread_mutex_unlock(&lock);
414
415   return ret;
416 }
417
418 void btif_config_save(void) {
419   assert(config != NULL);
420   assert(config_timer != NULL);
421
422   alarm_set(config_timer, CONFIG_SETTLE_PERIOD_MS, timer_config_save_cb, NULL);
423 }
424
425 void btif_config_flush(void) {
426   assert(config != NULL);
427   assert(config_timer != NULL);
428
429   alarm_cancel(config_timer);
430   btif_config_write(0, NULL);
431 }
432
433 bool btif_config_clear(void) {
434   assert(config != NULL);
435   assert(config_timer != NULL);
436
437   alarm_cancel(config_timer);
438
439   pthread_mutex_lock(&lock);
440   config_free(config);
441
442   config = config_new_empty();
443   if (config == NULL) {
444     pthread_mutex_unlock(&lock);
445     return false;
446   }
447
448   bool ret = config_save(config, CONFIG_FILE_PATH);
449   btif_config_source = RESET;
450   pthread_mutex_unlock(&lock);
451   return ret;
452 }
453
454 static void timer_config_save_cb(UNUSED_ATTR void *data) {
455   // Moving file I/O to btif context instead of timer callback because
456   // it usually takes a lot of time to be completed, introducing
457   // delays during A2DP playback causing blips or choppiness.
458   btif_transfer_context(btif_config_write, 0, NULL, 0, NULL);
459 }
460
461 static void btif_config_write(UNUSED_ATTR UINT16 event, UNUSED_ATTR char *p_param) {
462   assert(config != NULL);
463   assert(config_timer != NULL);
464
465   pthread_mutex_lock(&lock);
466   rename(CONFIG_FILE_PATH, CONFIG_BACKUP_PATH);
467   sync();
468   config_t *config_paired = config_new_clone(config);
469   btif_config_remove_unpaired(config_paired);
470   config_save(config_paired, CONFIG_FILE_PATH);
471   config_free(config_paired);
472   pthread_mutex_unlock(&lock);
473 }
474
475 static void btif_config_remove_unpaired(config_t *conf) {
476   assert(conf != NULL);
477   int paired_devices = 0;
478
479   // The paired config used to carry information about
480   // discovered devices during regular inquiry scans.
481   // We remove these now and cache them in memory instead.
482   const config_section_node_t *snode = config_section_begin(conf);
483   while (snode != config_section_end(conf)) {
484     const char *section = config_section_name(snode);
485     if (string_is_bdaddr(section)) {
486       if (!config_has_key(conf, section, "LinkKey") &&
487           !config_has_key(conf, section, "LE_KEY_PENC") &&
488           !config_has_key(conf, section, "LE_KEY_PID") &&
489           !config_has_key(conf, section, "LE_KEY_PCSRK") &&
490           !config_has_key(conf, section, "LE_KEY_LENC") &&
491           !config_has_key(conf, section, "LE_KEY_LCSRK")) {
492         snode = config_section_next(snode);
493         config_remove_section(conf, section);
494         continue;
495       }
496       paired_devices++;
497     }
498     snode = config_section_next(snode);
499   }
500
501   // should only happen once, at initial load time
502   if (btif_config_devices_loaded == -1)
503     btif_config_devices_loaded = paired_devices;
504 }
505
506 void btif_debug_config_dump(int fd) {
507     dprintf(fd, "\nBluetooth Config:\n");
508
509     dprintf(fd, "  Config Source: ");
510     switch(btif_config_source) {
511         case NOT_LOADED:
512             dprintf(fd, "Not loaded\n");
513             break;
514         case ORIGINAL:
515             dprintf(fd, "Original file\n");
516             break;
517         case BACKUP:
518             dprintf(fd, "Backup file\n");
519             break;
520         case LEGACY:
521             dprintf(fd, "Legacy file\n");
522             break;
523         case NEW_FILE:
524             dprintf(fd, "New file\n");
525             break;
526         case RESET:
527             dprintf(fd, "Reset file\n");
528             break;
529     }
530
531     dprintf(fd, "  Devices loaded: %d\n", btif_config_devices_loaded);
532     dprintf(fd, "  File created/tagged: %s\n", btif_config_time_created);
533 }
534
535 static void btif_config_remove_restricted(config_t* config) {
536   assert(config != NULL);
537
538   const config_section_node_t *snode = config_section_begin(config);
539   while (snode != config_section_end(config)) {
540     const char *section = config_section_name(snode);
541     if (string_is_bdaddr(section) && config_has_key(config, section, "Restricted")) {
542         BTIF_TRACE_DEBUG("%s: Removing restricted device %s", __func__, section);
543         config_remove_section(config, section);
544     }
545     snode = config_section_next(snode);
546   }
547 }
548
549 static bool is_factory_reset(void) {
550   char factory_reset[PROPERTY_VALUE_MAX] = {0};
551   property_get("persist.bluetooth.factoryreset", factory_reset, "false");
552   return strncmp(factory_reset, "true", 4) == 0;
553 }
554
555 static void delete_config_files(void) {
556   remove(CONFIG_FILE_PATH);
557   remove(CONFIG_BACKUP_PATH);
558   property_set("persist.bluetooth.factoryreset", "false");
559 }