OSDN Git Service

Refactor the Bluetooth timers
[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 <unistd.h>
29
30 #include "bt_types.h"
31 #include "btcore/include/bdaddr.h"
32 #include "btcore/include/module.h"
33 #include "btif_common.h"
34 #include "btif_config.h"
35 #include "btif_config_transcode.h"
36 #include "btif_util.h"
37 #include "osi/include/alarm.h"
38 #include "osi/include/allocator.h"
39 #include "osi/include/compat.h"
40 #include "osi/include/config.h"
41 #include "osi/include/log.h"
42 #include "osi/include/osi.h"
43
44 // TODO(armansito): Find a better way than searching by a hardcoded path.
45 #if defined(OS_GENERIC)
46 static const char *CONFIG_FILE_PATH = "bt_config.conf";
47 #else  // !defined(OS_GENERIC)
48 static const char *CONFIG_FILE_PATH = "/data/misc/bluedroid/bt_config.conf";
49 #endif  // defined(OS_GENERIC)
50 static const char *LEGACY_CONFIG_FILE_PATH = "/data/misc/bluedroid/bt_config.xml";
51 static const period_ms_t CONFIG_SETTLE_PERIOD_MS = 3000;
52
53 static void timer_config_save_cb(void *data);
54 static void btif_config_write(UINT16 event, char *p_param);
55 static void btif_config_devcache_cleanup(void);
56
57 // TODO(zachoverflow): Move these two functions out, because they are too specific for this file
58 // {grumpy-cat/no, monty-python/you-make-me-sad}
59 bool btif_get_device_type(const BD_ADDR bd_addr, int *p_device_type)
60 {
61     if (p_device_type == NULL)
62         return FALSE;
63
64     bt_bdaddr_t bda;
65     bdcpy(bda.address, bd_addr);
66
67     bdstr_t bd_addr_str;
68     bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str));
69
70     if (!btif_config_get_int(bd_addr_str, "DevType", p_device_type))
71         return FALSE;
72
73     LOG_DEBUG(LOG_TAG, "%s: Device [%s] type %d", __FUNCTION__, bd_addr_str, *p_device_type);
74     return TRUE;
75 }
76
77 bool btif_get_address_type(const BD_ADDR bd_addr, int *p_addr_type)
78 {
79     if (p_addr_type == NULL)
80         return FALSE;
81
82     bt_bdaddr_t bda;
83     bdcpy(bda.address, bd_addr);
84
85     bdstr_t bd_addr_str;
86     bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str));
87
88     if (!btif_config_get_int(bd_addr_str, "AddrType", p_addr_type))
89         return FALSE;
90
91     LOG_DEBUG(LOG_TAG, "%s: Device [%s] address type %d", __FUNCTION__, bd_addr_str, *p_addr_type);
92     return TRUE;
93 }
94
95 static pthread_mutex_t lock;  // protects operations on |config|.
96 static config_t *config;
97 static alarm_t *config_timer;
98
99 // Module lifecycle functions
100
101 static future_t *init(void) {
102   pthread_mutex_init(&lock, NULL);
103   config = config_new(CONFIG_FILE_PATH);
104   if (!config) {
105     LOG_WARN(LOG_TAG, "%s unable to load config file; attempting to transcode legacy file.", __func__);
106     config = btif_config_transcode(LEGACY_CONFIG_FILE_PATH);
107     if (!config) {
108       LOG_WARN(LOG_TAG, "%s unable to transcode legacy file, starting unconfigured.", __func__);
109       config = config_new_empty();
110       if (!config) {
111         LOG_ERROR(LOG_TAG, "%s unable to allocate a config object.", __func__);
112         goto error;
113       }
114     }
115
116     if (config_save(config, CONFIG_FILE_PATH))
117       unlink(LEGACY_CONFIG_FILE_PATH);
118   }
119
120   btif_config_devcache_cleanup();
121
122   // TODO(sharvil): use a non-wake alarm for this once we have
123   // API support for it. There's no need to wake the system to
124   // write back to disk.
125   config_timer = alarm_new("btif.config");
126   if (!config_timer) {
127     LOG_ERROR(LOG_TAG, "%s unable to create alarm.", __func__);
128     goto error;
129   }
130
131   return future_new_immediate(FUTURE_SUCCESS);
132
133 error:
134   alarm_free(config_timer);
135   config_free(config);
136   pthread_mutex_destroy(&lock);
137   config_timer = NULL;
138   config = NULL;
139   return future_new_immediate(FUTURE_FAIL);
140 }
141
142 static future_t *shut_down(void) {
143   btif_config_flush();
144   return future_new_immediate(FUTURE_SUCCESS);
145 }
146
147 static future_t *clean_up(void) {
148   btif_config_flush();
149
150   alarm_free(config_timer);
151   config_free(config);
152   pthread_mutex_destroy(&lock);
153   config_timer = NULL;
154   config = NULL;
155   return future_new_immediate(FUTURE_SUCCESS);
156 }
157
158 EXPORT_SYMBOL const module_t btif_config_module = {
159   .name = BTIF_CONFIG_MODULE,
160   .init = init,
161   .start_up = NULL,
162   .shut_down = shut_down,
163   .clean_up = clean_up,
164   .dependencies = {
165     NULL
166   }
167 };
168
169 bool btif_config_has_section(const char *section) {
170   assert(config != NULL);
171   assert(section != NULL);
172
173   pthread_mutex_lock(&lock);
174   bool ret = config_has_section(config, section);
175   pthread_mutex_unlock(&lock);
176
177   return ret;
178 }
179
180 bool btif_config_exist(const char *section, const char *key) {
181   assert(config != NULL);
182   assert(section != NULL);
183   assert(key != NULL);
184
185   pthread_mutex_lock(&lock);
186   bool ret = config_has_key(config, section, key);
187   pthread_mutex_unlock(&lock);
188
189   return ret;
190 }
191
192 bool btif_config_get_int(const char *section, const char *key, int *value) {
193   assert(config != NULL);
194   assert(section != NULL);
195   assert(key != NULL);
196   assert(value != NULL);
197
198   pthread_mutex_lock(&lock);
199   bool ret = config_has_key(config, section, key);
200   if (ret)
201     *value = config_get_int(config, section, key, *value);
202   pthread_mutex_unlock(&lock);
203
204   return ret;
205 }
206
207 bool btif_config_set_int(const char *section, const char *key, int value) {
208   assert(config != NULL);
209   assert(section != NULL);
210   assert(key != NULL);
211
212   pthread_mutex_lock(&lock);
213   config_set_int(config, section, key, value);
214   pthread_mutex_unlock(&lock);
215
216   return true;
217 }
218
219 bool btif_config_get_str(const char *section, const char *key, char *value, int *size_bytes) {
220   assert(config != NULL);
221   assert(section != NULL);
222   assert(key != NULL);
223   assert(value != NULL);
224   assert(size_bytes != NULL);
225
226   pthread_mutex_lock(&lock);
227   const char *stored_value = config_get_string(config, section, key, NULL);
228   pthread_mutex_unlock(&lock);
229
230   if (!stored_value)
231     return false;
232
233   strlcpy(value, stored_value, *size_bytes);
234   *size_bytes = strlen(value) + 1;
235
236   return true;
237 }
238
239 bool btif_config_set_str(const char *section, const char *key, const char *value) {
240   assert(config != NULL);
241   assert(section != NULL);
242   assert(key != NULL);
243   assert(value != NULL);
244
245   pthread_mutex_lock(&lock);
246   config_set_string(config, section, key, value);
247   pthread_mutex_unlock(&lock);
248
249   return true;
250 }
251
252 bool btif_config_get_bin(const char *section, const char *key, uint8_t *value, size_t *length) {
253   assert(config != NULL);
254   assert(section != NULL);
255   assert(key != NULL);
256   assert(value != NULL);
257   assert(length != NULL);
258
259   pthread_mutex_lock(&lock);
260   const char *value_str = config_get_string(config, section, key, NULL);
261   pthread_mutex_unlock(&lock);
262
263   if (!value_str)
264     return false;
265
266   size_t value_len = strlen(value_str);
267   if ((value_len % 2) != 0 || *length < (value_len / 2))
268     return false;
269
270   for (size_t i = 0; i < value_len; ++i)
271     if (!isxdigit(value_str[i]))
272       return false;
273
274   for (*length = 0; *value_str; value_str += 2, *length += 1)
275     sscanf(value_str, "%02hhx", &value[*length]);
276
277   return true;
278 }
279
280 size_t btif_config_get_bin_length(const char *section, const char *key) {
281   assert(config != NULL);
282   assert(section != NULL);
283   assert(key != NULL);
284
285   pthread_mutex_lock(&lock);
286   const char *value_str = config_get_string(config, section, key, NULL);
287   pthread_mutex_unlock(&lock);
288
289   if (!value_str)
290     return 0;
291
292   size_t value_len = strlen(value_str);
293   return ((value_len % 2) != 0) ? 0 : (value_len / 2);
294 }
295
296 bool btif_config_set_bin(const char *section, const char *key, const uint8_t *value, size_t length) {
297   const char *lookup = "0123456789abcdef";
298
299   assert(config != NULL);
300   assert(section != NULL);
301   assert(key != NULL);
302
303   if (length > 0)
304       assert(value != NULL);
305
306   char *str = (char *)osi_calloc(length * 2 + 1);
307   if (!str)
308     return false;
309
310   for (size_t i = 0; i < length; ++i) {
311     str[(i * 2) + 0] = lookup[(value[i] >> 4) & 0x0F];
312     str[(i * 2) + 1] = lookup[value[i] & 0x0F];
313   }
314
315   pthread_mutex_lock(&lock);
316   config_set_string(config, section, key, str);
317   pthread_mutex_unlock(&lock);
318
319   osi_free(str);
320   return true;
321 }
322
323 const btif_config_section_iter_t *btif_config_section_begin(void) {
324   assert(config != NULL);
325   return (const btif_config_section_iter_t *)config_section_begin(config);
326 }
327
328 const btif_config_section_iter_t *btif_config_section_end(void) {
329   assert(config != NULL);
330   return (const btif_config_section_iter_t *)config_section_end(config);
331 }
332
333 const btif_config_section_iter_t *btif_config_section_next(const btif_config_section_iter_t *section) {
334   assert(config != NULL);
335   assert(section != NULL);
336   return (const btif_config_section_iter_t *)config_section_next((const config_section_node_t *)section);
337 }
338
339 const char *btif_config_section_name(const btif_config_section_iter_t *section) {
340   assert(config != NULL);
341   assert(section != NULL);
342   return config_section_name((const config_section_node_t *)section);
343 }
344
345 bool btif_config_remove(const char *section, const char *key) {
346   assert(config != NULL);
347   assert(section != NULL);
348   assert(key != NULL);
349
350   pthread_mutex_lock(&lock);
351   bool ret = config_remove_key(config, section, key);
352   pthread_mutex_unlock(&lock);
353
354   return ret;
355 }
356
357 void btif_config_save(void) {
358   assert(config_timer != NULL);
359   assert(config != NULL);
360
361   alarm_set(config_timer, CONFIG_SETTLE_PERIOD_MS, timer_config_save_cb, NULL);
362 }
363
364 void btif_config_flush(void) {
365   assert(config != NULL);
366   assert(config_timer != NULL);
367
368   alarm_cancel(config_timer);
369   btif_config_write(0, NULL);
370
371   pthread_mutex_lock(&lock);
372   config_save(config, CONFIG_FILE_PATH);
373   pthread_mutex_unlock(&lock);
374 }
375
376 int btif_config_clear(void){
377   assert(config != NULL);
378   assert(config_timer != NULL);
379
380   alarm_cancel(config_timer);
381
382   pthread_mutex_lock(&lock);
383   config_free(config);
384
385   config = config_new_empty();
386   if (config == NULL) {
387     pthread_mutex_unlock(&lock);
388     return false;
389   }
390
391   int ret = config_save(config, CONFIG_FILE_PATH);
392   pthread_mutex_unlock(&lock);
393   return ret;
394 }
395
396 static void timer_config_save_cb(UNUSED_ATTR void *data) {
397   // Moving file I/O to btif context instead of timer callback because
398   // it usually takes a lot of time to be completed, introducing
399   // delays during A2DP playback causing blips or choppiness.
400   btif_transfer_context(btif_config_write, 0, NULL, 0, NULL);
401 }
402
403 static void btif_config_write(UNUSED_ATTR UINT16 event, UNUSED_ATTR char *p_param) {
404   assert(config != NULL);
405   assert(config_timer != NULL);
406
407   btif_config_devcache_cleanup();
408
409   pthread_mutex_lock(&lock);
410   config_save(config, CONFIG_FILE_PATH);
411   pthread_mutex_unlock(&lock);
412 }
413
414 static void btif_config_devcache_cleanup(void) {
415   assert(config != NULL);
416
417   // The config accumulates cached information about remote
418   // devices during regular inquiry scans. We remove some of these
419   // so the cache doesn't grow indefinitely.
420   // We don't remove information about bonded devices (which have link keys).
421   static const size_t ADDRS_MAX = 512;
422   size_t total_addrs = 0;
423
424   pthread_mutex_lock(&lock);
425   const config_section_node_t *snode = config_section_begin(config);
426   while (snode != config_section_end(config)) {
427     const char *section = config_section_name(snode);
428     if (string_is_bdaddr(section)) {
429       ++total_addrs;
430
431       if ((total_addrs > ADDRS_MAX) &&
432           !config_has_key(config, section, "LinkKey") &&
433           !config_has_key(config, section, "LE_KEY_PENC") &&
434           !config_has_key(config, section, "LE_KEY_PID") &&
435           !config_has_key(config, section, "LE_KEY_PCSRK") &&
436           !config_has_key(config, section, "LE_KEY_LENC") &&
437           !config_has_key(config, section, "LE_KEY_LCSRK")) {
438         snode = config_section_next(snode);
439         config_remove_section(config, section);
440         continue;
441       }
442     }
443     snode = config_section_next(snode);
444   }
445   pthread_mutex_unlock(&lock);
446 }