• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (C) 2015 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_device_interop"
20 
21 #include <assert.h>
22 #include <string.h> // For memcmp
23 
24 #include "btcore/include/module.h"
25 #include "device/include/interop.h"
26 #include "device/include/interop_database.h"
27 #include "osi/include/allocator.h"
28 #include "osi/include/list.h"
29 #include "osi/include/log.h"
30 
31 #define CASE_RETURN_STR(const) case const: return #const;
32 
33 static list_t *interop_list = NULL;
34 
35 static const char* interop_feature_string_(const interop_feature_t feature);
36 static void interop_free_entry_(void *data);
37 static void interop_lazy_init_(void);
38 static bool interop_match_fixed_(const interop_feature_t feature, const bt_bdaddr_t *addr);
39 static bool interop_match_dynamic_(const interop_feature_t feature, const bt_bdaddr_t *addr);
40 
41 // Interface functions
42 
interop_match(const interop_feature_t feature,const bt_bdaddr_t * addr)43 bool interop_match(const interop_feature_t feature, const bt_bdaddr_t *addr) {
44   assert(addr);
45 
46   if (interop_match_fixed_(feature, addr) || interop_match_dynamic_(feature, addr)) {
47     char bdstr[20] = {0};
48     LOG_WARN("%s() Device %s is a match for interop workaround %s.",
49           __func__, bdaddr_to_string(addr, bdstr, sizeof(bdstr)),
50                         interop_feature_string_(feature));
51     return true;
52   }
53 
54   return false;
55 }
56 
interop_database_add(const interop_feature_t feature,const bt_bdaddr_t * addr,size_t length)57 void interop_database_add(const interop_feature_t feature, const bt_bdaddr_t *addr, size_t length) {
58   assert(addr);
59   assert(length > 0);
60   assert(length < sizeof(bt_bdaddr_t));
61 
62   interop_entry_t *entry = osi_calloc(sizeof(interop_entry_t));
63   memcpy(&entry->addr, addr, length);
64   entry->feature = feature;
65   entry->length = length;
66 
67   interop_lazy_init_();
68   list_append(interop_list, entry);
69 }
70 
interop_database_clear()71 void interop_database_clear() {
72   if (interop_list)
73     list_clear(interop_list);
74 }
75 
76 // Module life-cycle functions
77 
interop_clean_up(void)78 static future_t *interop_clean_up(void) {
79   list_free(interop_list);
80   interop_list = NULL;
81   return future_new_immediate(FUTURE_SUCCESS);
82 }
83 
84 const module_t interop_module = {
85   .name = INTEROP_MODULE,
86   .init = NULL,
87   .start_up = NULL,
88   .shut_down = NULL,
89   .clean_up = interop_clean_up,
90   .dependencies = {NULL},
91 };
92 
93 // Local functions
94 
interop_feature_string_(const interop_feature_t feature)95 static const char* interop_feature_string_(const interop_feature_t feature) {
96   switch (feature) {
97     CASE_RETURN_STR(INTEROP_DISABLE_LE_SECURE_CONNECTIONS)
98     CASE_RETURN_STR(INTEROP_AUTO_RETRY_PAIRING)
99   }
100 
101   return "UNKNOWN";
102 }
103 
interop_free_entry_(void * data)104 static void interop_free_entry_(void *data) {
105   interop_entry_t *entry = (interop_entry_t *)data;
106   osi_free(entry);
107 }
108 
interop_lazy_init_(void)109 static void interop_lazy_init_(void) {
110   if (interop_list == NULL) {
111     interop_list = list_new(interop_free_entry_);
112   }
113 }
114 
interop_match_dynamic_(const interop_feature_t feature,const bt_bdaddr_t * addr)115 static bool interop_match_dynamic_(const interop_feature_t feature, const bt_bdaddr_t *addr) {
116   if (interop_list == NULL || list_length(interop_list) == 0)
117     return false;
118 
119   const list_node_t *node = list_begin(interop_list);
120   while (node != list_end(interop_list)) {
121     interop_entry_t *entry = list_node(node);
122     assert(entry);
123 
124     if (feature == entry->feature && memcmp(addr, &entry->addr, entry->length) == 0)
125       return true;
126 
127     node = list_next(node);
128   }
129   return false;
130 }
131 
interop_match_fixed_(const interop_feature_t feature,const bt_bdaddr_t * addr)132 static bool interop_match_fixed_(const interop_feature_t feature, const bt_bdaddr_t *addr) {
133   assert(addr);
134 
135   const size_t db_size = sizeof(interop_database) / sizeof(interop_entry_t);
136   for (size_t i = 0; i != db_size; ++i) {
137     if (feature == interop_database[i].feature &&
138         memcmp(addr, &interop_database[i].addr, interop_database[i].length) == 0) {
139       char bdstr[20] = {0};
140       LOG_WARN("%s() Device %s is a match for interop workaround %s", __func__,
141           bdaddr_to_string(addr, bdstr, sizeof(bdstr)), interop_feature_string_(feature));
142       return true;
143     }
144   }
145 
146   return false;
147 }
148