OSDN Git Service

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