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   }
135 
136   return "UNKNOWN";
137 }
138 
interop_free_entry_(void * data)139 static void interop_free_entry_(void* data) {
140   interop_addr_entry_t* entry = (interop_addr_entry_t*)data;
141   osi_free(entry);
142 }
143 
interop_lazy_init_(void)144 static void interop_lazy_init_(void) {
145   if (interop_list == NULL) {
146     interop_list = list_new(interop_free_entry_);
147   }
148 }
149 
interop_match_dynamic_(const interop_feature_t feature,const RawAddress * addr)150 static bool interop_match_dynamic_(const interop_feature_t feature,
151                                    const RawAddress* addr) {
152   if (interop_list == NULL || list_length(interop_list) == 0) return false;
153 
154   const list_node_t* node = list_begin(interop_list);
155   while (node != list_end(interop_list)) {
156     interop_addr_entry_t* entry =
157         static_cast<interop_addr_entry_t*>(list_node(node));
158     CHECK(entry);
159 
160     if (feature == entry->feature &&
161         memcmp(addr, &entry->addr, entry->length) == 0)
162       return true;
163 
164     node = list_next(node);
165   }
166   return false;
167 }
168 
interop_match_fixed_(const interop_feature_t feature,const RawAddress * addr)169 static bool interop_match_fixed_(const interop_feature_t feature,
170                                  const RawAddress* addr) {
171   CHECK(addr);
172 
173   const size_t db_size =
174       sizeof(interop_addr_database) / sizeof(interop_addr_entry_t);
175   for (size_t i = 0; i != db_size; ++i) {
176     if (feature == interop_addr_database[i].feature &&
177         memcmp(addr, &interop_addr_database[i].addr,
178                interop_addr_database[i].length) == 0) {
179       return true;
180     }
181   }
182 
183   return false;
184 }
185