1 /******************************************************************************
2 *
3 * Copyright 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 <base/logging.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) \
32 case const: \
33 return #const;
34
35 static list_t* interop_list = NULL;
36
37 static const char* interop_feature_string_(const interop_feature_t feature);
38 static void interop_free_entry_(void* data);
39 static void interop_lazy_init_(void);
40 static bool interop_match_fixed_(const interop_feature_t feature,
41 const RawAddress* addr);
42 static bool interop_match_dynamic_(const interop_feature_t feature,
43 const RawAddress* addr);
44
45 // Interface functions
46
interop_match_addr(const interop_feature_t feature,const RawAddress * addr)47 bool interop_match_addr(const interop_feature_t feature,
48 const RawAddress* addr) {
49 CHECK(addr);
50
51 if (interop_match_fixed_(feature, addr) ||
52 interop_match_dynamic_(feature, addr)) {
53 LOG_INFO("%s() Device %s is a match for interop workaround %s.", __func__,
54 addr->ToString().c_str(), interop_feature_string_(feature));
55 return true;
56 }
57
58 return false;
59 }
60
interop_match_name(const interop_feature_t feature,const char * name)61 bool interop_match_name(const interop_feature_t feature, const char* name) {
62 CHECK(name);
63
64 const size_t db_size =
65 sizeof(interop_name_database) / sizeof(interop_name_entry_t);
66 for (size_t i = 0; i != db_size; ++i) {
67 if (feature == interop_name_database[i].feature &&
68 strlen(name) >= interop_name_database[i].length &&
69 strncmp(name, interop_name_database[i].name,
70 interop_name_database[i].length) == 0) {
71 LOG_INFO("%s() Device %s is a match for interop workaround %s.", __func__,
72 name, interop_feature_string_(feature));
73 return true;
74 }
75 }
76
77 return false;
78 }
79
interop_database_add(uint16_t feature,const RawAddress * addr,size_t length)80 void interop_database_add(uint16_t feature, const RawAddress* addr,
81 size_t length) {
82 CHECK(addr);
83 CHECK(length > 0);
84 CHECK(length < RawAddress::kLength);
85
86 interop_addr_entry_t* entry = static_cast<interop_addr_entry_t*>(
87 osi_calloc(sizeof(interop_addr_entry_t)));
88 memcpy(&entry->addr, addr, length);
89 entry->feature = static_cast<interop_feature_t>(feature);
90 entry->length = length;
91
92 interop_lazy_init_();
93 list_append(interop_list, entry);
94 }
95
interop_database_clear()96 void interop_database_clear() {
97 if (interop_list) list_clear(interop_list);
98 }
99
100 // Module life-cycle functions
101
interop_clean_up(void)102 static future_t* interop_clean_up(void) {
103 list_free(interop_list);
104 interop_list = NULL;
105 return future_new_immediate(FUTURE_SUCCESS);
106 }
107
108 EXPORT_SYMBOL module_t interop_module = {
109 .name = INTEROP_MODULE,
110 .init = NULL,
111 .start_up = NULL,
112 .shut_down = NULL,
113 .clean_up = interop_clean_up,
114 .dependencies = {NULL},
115 };
116
117 // Local functions
118
interop_feature_string_(const interop_feature_t feature)119 static const char* interop_feature_string_(const interop_feature_t feature) {
120 switch (feature) {
121 CASE_RETURN_STR(INTEROP_DISABLE_LE_SECURE_CONNECTIONS)
122 CASE_RETURN_STR(INTEROP_AUTO_RETRY_PAIRING)
123 CASE_RETURN_STR(INTEROP_DISABLE_ABSOLUTE_VOLUME)
124 CASE_RETURN_STR(INTEROP_DISABLE_AUTO_PAIRING)
125 CASE_RETURN_STR(INTEROP_KEYBOARD_REQUIRES_FIXED_PIN)
126 CASE_RETURN_STR(INTEROP_2MBPS_LINK_ONLY)
127 CASE_RETURN_STR(INTEROP_HID_PREF_CONN_SUP_TIMEOUT_3S)
128 CASE_RETURN_STR(INTEROP_GATTC_NO_SERVICE_CHANGED_IND)
129 CASE_RETURN_STR(INTEROP_DISABLE_AVDTP_RECONFIGURE)
130 CASE_RETURN_STR(INTEROP_DYNAMIC_ROLE_SWITCH)
131 CASE_RETURN_STR(INTEROP_DISABLE_ROLE_SWITCH)
132 CASE_RETURN_STR(INTEROP_HID_HOST_LIMIT_SNIFF_INTERVAL)
133 CASE_RETURN_STR(INTEROP_DISABLE_NAME_REQUEST)
134 CASE_RETURN_STR(INTEROP_AVRCP_1_4_ONLY)
135 CASE_RETURN_STR(INTEROP_DISABLE_SNIFF)
136 CASE_RETURN_STR(INTEROP_DISABLE_AVDTP_SUSPEND)
137 CASE_RETURN_STR(INTEROP_SLC_SKIP_BIND_COMMAND);
138 }
139
140 return "UNKNOWN";
141 }
142
interop_free_entry_(void * data)143 static void interop_free_entry_(void* data) {
144 interop_addr_entry_t* entry = (interop_addr_entry_t*)data;
145 osi_free(entry);
146 }
147
interop_lazy_init_(void)148 static void interop_lazy_init_(void) {
149 if (interop_list == NULL) {
150 interop_list = list_new(interop_free_entry_);
151 }
152 }
153
interop_match_dynamic_(const interop_feature_t feature,const RawAddress * addr)154 static bool interop_match_dynamic_(const interop_feature_t feature,
155 const RawAddress* addr) {
156 if (interop_list == NULL || list_length(interop_list) == 0) return false;
157
158 const list_node_t* node = list_begin(interop_list);
159 while (node != list_end(interop_list)) {
160 interop_addr_entry_t* entry =
161 static_cast<interop_addr_entry_t*>(list_node(node));
162 CHECK(entry);
163
164 if (feature == entry->feature &&
165 memcmp(addr, &entry->addr, entry->length) == 0)
166 return true;
167
168 node = list_next(node);
169 }
170 return false;
171 }
172
interop_match_fixed_(const interop_feature_t feature,const RawAddress * addr)173 static bool interop_match_fixed_(const interop_feature_t feature,
174 const RawAddress* addr) {
175 CHECK(addr);
176
177 const size_t db_size =
178 sizeof(interop_addr_database) / sizeof(interop_addr_entry_t);
179 for (size_t i = 0; i != db_size; ++i) {
180 if (feature == interop_addr_database[i].feature &&
181 memcmp(addr, &interop_addr_database[i].addr,
182 interop_addr_database[i].length) == 0) {
183 return true;
184 }
185 }
186
187 return false;
188 }
189