1 /******************************************************************************
2  *
3  *  Copyright 2005-2012 Broadcom Corporation
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 #define LOG_TAG "bt_bta_hh"
19 
20 #include <bluetooth/log.h>
21 #include <string.h>  // memset
22 
23 #include <cstring>
24 
25 #include "bta/hh/bta_hh_int.h"
26 #include "btif/include/btif_storage.h"
27 #include "device/include/interop.h"
28 #include "internal_include/bt_target.h"
29 #include "os/log.h"
30 #include "osi/include/allocator.h"
31 #include "stack/include/btm_client_interface.h"
32 #include "stack/include/sdp_api.h"
33 #include "types/raw_address.h"
34 
35 using namespace bluetooth::legacy::stack::sdp;
36 using namespace bluetooth;
37 
38 /* if SSR max latency is not defined by remote device, set the default value
39    as half of the link supervision timeout */
40 #define BTA_HH_GET_DEF_SSR_MAX_LAT(x) ((x) >> 1)
41 
42 /*****************************************************************************
43  *  Constants
44  ****************************************************************************/
45 
46 namespace {
47 
48 constexpr uint16_t kSsrMaxLatency = 18; /* slots * 0.625ms */
49 
50 }  // namespace
51 
52 /*******************************************************************************
53  *
54  * Function         bta_hh_find_cb
55  *
56  * Description      Find best available control block according to ACL link
57  *                  specification.
58  *
59  *
60  * Returns          void
61  *
62  ******************************************************************************/
bta_hh_find_cb(const tAclLinkSpec & link_spec)63 uint8_t bta_hh_find_cb(const tAclLinkSpec& link_spec) {
64   uint8_t xx;
65 
66   /* See how many active devices there are. */
67   for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++) {
68     /* check if any active/known devices is a match */
69     if ((link_spec == bta_hh_cb.kdev[xx].link_spec &&
70          !link_spec.addrt.bda.IsEmpty())) {
71 #if (BTA_HH_DEBUG == TRUE)
72       log::verbose("found kdev_cb[{}] hid_handle={}", xx,
73                    bta_hh_cb.kdev[xx].hid_handle);
74 #endif
75       return xx;
76     }
77 #if (BTA_HH_DEBUG == TRUE)
78     else
79       log::verbose("in_use ? [{}] kdev[{}].hid_handle={} state=[{}]",
80                    bta_hh_cb.kdev[xx].in_use, xx, bta_hh_cb.kdev[xx].hid_handle,
81                    bta_hh_cb.kdev[xx].state);
82 #endif
83   }
84 
85   /* if no active device match, find a spot for it */
86   for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++) {
87     if (!bta_hh_cb.kdev[xx].in_use) {
88       bta_hh_cb.kdev[xx].link_spec = link_spec;
89       break;
90     }
91   }
92 /* If device list full, report BTA_HH_IDX_INVALID */
93 #if (BTA_HH_DEBUG == TRUE)
94   log::verbose("index={} while max={}", xx, BTA_HH_MAX_DEVICE);
95 #endif
96 
97   if (xx == BTA_HH_MAX_DEVICE) xx = BTA_HH_IDX_INVALID;
98 
99   return xx;
100 }
101 
bta_hh_get_cb(const tAclLinkSpec & link_spec)102 tBTA_HH_DEV_CB* bta_hh_get_cb(const tAclLinkSpec& link_spec) {
103   uint8_t idx = bta_hh_find_cb(link_spec);
104   if (idx == BTA_HH_IDX_INVALID) {
105     return nullptr;
106   }
107   return &bta_hh_cb.kdev[idx];
108 }
109 
110 /*******************************************************************************
111  *
112  * Function         bta_hh_clean_up_kdev
113  *
114  * Description      Clean up device control block when device is removed from
115  *                  manitainace list, and update control block index map.
116  *
117  * Returns          void
118  *
119  ******************************************************************************/
bta_hh_clean_up_kdev(tBTA_HH_DEV_CB * p_cb)120 void bta_hh_clean_up_kdev(tBTA_HH_DEV_CB* p_cb) {
121   uint8_t index;
122 
123   if (p_cb->link_spec.transport == BT_TRANSPORT_LE) {
124     uint8_t le_hid_handle = BTA_HH_GET_LE_CB_IDX(p_cb->hid_handle);
125     if (le_hid_handle >= BTA_HH_LE_MAX_KNOWN) {
126       log::warn("Invalid LE hid_handle {}", p_cb->hid_handle);
127     } else {
128       bta_hh_cb.le_cb_index[le_hid_handle] = BTA_HH_IDX_INVALID;
129     }
130   } else {
131     if (p_cb->hid_handle >= BTA_HH_MAX_KNOWN) {
132       log::warn("Invalid hid_handle {}", p_cb->hid_handle);
133     } else {
134       bta_hh_cb.cb_index[p_cb->hid_handle] = BTA_HH_IDX_INVALID;
135     }
136   }
137 
138   /* reset device control block */
139   index = p_cb->index; /* Preserve index for this control block */
140 
141   /* Free buffer for report descriptor info */
142   osi_free_and_reset((void**)&p_cb->dscp_info.descriptor.dsc_list);
143 
144   memset(p_cb, 0, sizeof(tBTA_HH_DEV_CB)); /* Reset control block */
145 
146   p_cb->index = index; /* Restore index for this control block */
147   p_cb->state = BTA_HH_IDLE_ST;
148   p_cb->hid_handle = BTA_HH_INVALID_HANDLE;
149 }
150 /*******************************************************************************
151  *
152  * Function         bta_hh_update_di_info
153  *
154  * Description      Maintain a known device list for BTA HH.
155  *
156  * Returns          void
157  *
158  ******************************************************************************/
bta_hh_update_di_info(tBTA_HH_DEV_CB * p_cb,uint16_t vendor_id,uint16_t product_id,uint16_t version,uint8_t flag,uint8_t ctry_code)159 void bta_hh_update_di_info(tBTA_HH_DEV_CB* p_cb, uint16_t vendor_id,
160                            uint16_t product_id, uint16_t version, uint8_t flag,
161                            uint8_t ctry_code) {
162 #if (BTA_HH_DEBUG == TRUE)
163   log::verbose("vendor_id=0x{:2x} product_id=0x{:2x} version=0x{:2x}",
164                vendor_id, product_id, version);
165 #endif
166   p_cb->dscp_info.vendor_id = vendor_id;
167   p_cb->dscp_info.product_id = product_id;
168   p_cb->dscp_info.version = version;
169   p_cb->dscp_info.flag = flag;
170   p_cb->dscp_info.ctry_code = ctry_code;
171 }
172 /*******************************************************************************
173  *
174  * Function         bta_hh_add_device_to_list
175  *
176  * Description      Maintain a known device list for BTA HH.
177  *
178  * Returns          void
179  *
180  ******************************************************************************/
bta_hh_add_device_to_list(tBTA_HH_DEV_CB * p_cb,uint8_t handle,uint16_t attr_mask,const tHID_DEV_DSCP_INFO * p_dscp_info,uint8_t sub_class,uint16_t ssr_max_latency,uint16_t ssr_min_tout,uint8_t app_id)181 void bta_hh_add_device_to_list(tBTA_HH_DEV_CB* p_cb, uint8_t handle,
182                                uint16_t attr_mask,
183                                const tHID_DEV_DSCP_INFO* p_dscp_info,
184                                uint8_t sub_class, uint16_t ssr_max_latency,
185                                uint16_t ssr_min_tout, uint8_t app_id) {
186 #if (BTA_HH_DEBUG == TRUE)
187   log::verbose("subclass=0x{:2x}", sub_class);
188 #endif
189 
190   p_cb->hid_handle = handle;
191   p_cb->in_use = true;
192   p_cb->attr_mask = attr_mask;
193 
194   p_cb->sub_class = sub_class;
195   p_cb->app_id = app_id;
196 
197   p_cb->dscp_info.ssr_max_latency = ssr_max_latency;
198   p_cb->dscp_info.ssr_min_tout = ssr_min_tout;
199 
200   /* store report descriptor info */
201   if (p_dscp_info) {
202     osi_free_and_reset((void**)&p_cb->dscp_info.descriptor.dsc_list);
203 
204     if (p_dscp_info->dl_len) {
205       p_cb->dscp_info.descriptor.dsc_list =
206           (uint8_t*)osi_malloc(p_dscp_info->dl_len);
207       p_cb->dscp_info.descriptor.dl_len = p_dscp_info->dl_len;
208       memcpy(p_cb->dscp_info.descriptor.dsc_list, p_dscp_info->dsc_list,
209              p_dscp_info->dl_len);
210     }
211   }
212 }
213 
214 /*******************************************************************************
215  *
216  * Function         bta_hh_tod_spt
217  *
218  * Description      Check to see if this type of device is supported
219  *
220  * Returns
221  *
222  ******************************************************************************/
bta_hh_tod_spt(tBTA_HH_DEV_CB * p_cb,uint8_t sub_class)223 bool bta_hh_tod_spt(tBTA_HH_DEV_CB* p_cb, uint8_t sub_class) {
224   uint8_t xx;
225   uint8_t cod = (sub_class >> 2); /* lower two bits are reserved */
226 
227   for (xx = 0; xx < p_bta_hh_cfg->max_devt_spt; xx++) {
228     if (cod == (uint8_t)p_bta_hh_cfg->p_devt_list[xx].tod) {
229       p_cb->app_id = p_bta_hh_cfg->p_devt_list[xx].app_id;
230 #if (BTA_HH_DEBUG == TRUE)
231       log::verbose("sub_class:0x{:x} supported", sub_class);
232 #endif
233       return true;
234     }
235   }
236 #if (BTA_HH_DEBUG == TRUE)
237   log::verbose("sub_class:0x{:x} NOT supported", sub_class);
238 #endif
239   return false;
240 }
241 
242 
243 /*******************************************************************************
244  *
245  * Function         bta_hh_read_ssr_param
246  *
247  * Description      Read the SSR Parameter for the remote device
248  *
249  * Returns          tBTA_HH_STATUS  operation status
250  *
251  ******************************************************************************/
bta_hh_read_ssr_param(const tAclLinkSpec & link_spec,uint16_t * p_max_ssr_lat,uint16_t * p_min_ssr_tout)252 tBTA_HH_STATUS bta_hh_read_ssr_param(const tAclLinkSpec& link_spec,
253                                      uint16_t* p_max_ssr_lat,
254                                      uint16_t* p_min_ssr_tout) {
255   tBTA_HH_DEV_CB* p_cb = bta_hh_get_cb(link_spec);
256   if (p_cb == nullptr) {
257     log::warn("Unable to find device:{}", link_spec);
258     return BTA_HH_ERR;
259   }
260 
261   /* if remote device does not have HIDSSRHostMaxLatency attribute in SDP,
262      set SSR max latency default value here.  */
263   if (p_cb->dscp_info.ssr_max_latency == HID_SSR_PARAM_INVALID) {
264     /* The default is calculated as half of link supervision timeout.*/
265 
266     uint16_t ssr_max_latency;
267     if (get_btm_client_interface().link_controller.BTM_GetLinkSuperTout(
268             p_cb->link_spec.addrt.bda, &ssr_max_latency) != BTM_SUCCESS) {
269       log::warn("Unable to get supervision timeout for peer:{}",
270                 p_cb->link_spec);
271       return BTA_HH_ERR;
272     }
273     ssr_max_latency = BTA_HH_GET_DEF_SSR_MAX_LAT(ssr_max_latency);
274 
275     /* per 1.1 spec, if the newly calculated max latency is greater than
276        BTA_HH_SSR_MAX_LATENCY_DEF which is 500ms, use
277        BTA_HH_SSR_MAX_LATENCY_DEF */
278     if (ssr_max_latency > BTA_HH_SSR_MAX_LATENCY_DEF)
279       ssr_max_latency = BTA_HH_SSR_MAX_LATENCY_DEF;
280 
281     char remote_name[BD_NAME_LEN] = "";
282     if (btif_storage_get_stored_remote_name(link_spec.addrt.bda, remote_name)) {
283       if (interop_match_name(INTEROP_HID_HOST_LIMIT_SNIFF_INTERVAL,
284                              remote_name)) {
285         if (ssr_max_latency > kSsrMaxLatency /* slots * 0.625ms */) {
286           ssr_max_latency = kSsrMaxLatency;
287         }
288       }
289     }
290 
291     *p_max_ssr_lat = ssr_max_latency;
292   } else
293     *p_max_ssr_lat = p_cb->dscp_info.ssr_max_latency;
294 
295   if (p_cb->dscp_info.ssr_min_tout == HID_SSR_PARAM_INVALID)
296     *p_min_ssr_tout = BTA_HH_SSR_MIN_TOUT_DEF;
297   else
298     *p_min_ssr_tout = p_cb->dscp_info.ssr_min_tout;
299 
300   return BTA_HH_OK;
301 }
302 
303 /*******************************************************************************
304  *
305  * Function         bta_hh_cleanup_disable
306  *
307  * Description      when disable finished, cleanup control block and send
308  *                  callback
309  *
310  *
311  * Returns          void
312  *
313  ******************************************************************************/
bta_hh_cleanup_disable(tBTA_HH_STATUS status)314 void bta_hh_cleanup_disable(tBTA_HH_STATUS status) {
315   uint8_t xx;
316   /* free buffer in CB holding report descriptors */
317   for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++) {
318     osi_free_and_reset(
319         (void**)&bta_hh_cb.kdev[xx].dscp_info.descriptor.dsc_list);
320   }
321 
322   if (bta_hh_cb.p_disc_db) {
323     /* Cancel SDP if it had been started. */
324     (void)get_legacy_stack_sdp_api()->service.SDP_CancelServiceSearch(
325         bta_hh_cb.p_disc_db);
326     osi_free_and_reset((void**)&bta_hh_cb.p_disc_db);
327   }
328 
329   if (bta_hh_cb.p_cback) {
330     tBTA_HH bta_hh;
331     bta_hh.status = status;
332     (*bta_hh_cb.p_cback)(BTA_HH_DISABLE_EVT, &bta_hh);
333     /* all connections are down, no waiting for diconnect */
334     memset(&bta_hh_cb, 0, sizeof(tBTA_HH_CB));
335   }
336 }
337 
338 /*******************************************************************************
339  *
340  * Function         bta_hh_dev_handle_to_cb_idx
341  *
342  * Description      convert a HID device handle to the device control block
343  *                  index.
344  *
345  *
346  * Returns          uint8_t: index of the device control block.
347  *
348  ******************************************************************************/
bta_hh_dev_handle_to_cb_idx(uint8_t dev_handle)349 uint8_t bta_hh_dev_handle_to_cb_idx(uint8_t dev_handle) {
350   uint8_t index = BTA_HH_IDX_INVALID;
351 
352   if (BTA_HH_IS_LE_DEV_HDL(dev_handle)) {
353     if (BTA_HH_IS_LE_DEV_HDL_VALID(dev_handle))
354       index = bta_hh_cb.le_cb_index[BTA_HH_GET_LE_CB_IDX(dev_handle)];
355 #if (BTA_HH_DEBUG == TRUE)
356     log::verbose("dev_handle={} index={}", dev_handle, index);
357 #endif
358   } else
359       /* regular HID device checking */
360       if (dev_handle < BTA_HH_MAX_KNOWN)
361     index = bta_hh_cb.cb_index[dev_handle];
362 
363   return index;
364 }
365 #if (BTA_HH_DEBUG == TRUE)
366 /*******************************************************************************
367  *
368  * Function         bta_hh_trace_dev_db
369  *
370  * Description      Check to see if this type of device is supported
371  *
372  * Returns
373  *
374  ******************************************************************************/
bta_hh_trace_dev_db(void)375 void bta_hh_trace_dev_db(void) {
376   uint8_t xx;
377 
378   log::verbose("bta_hh_trace_dev_db:: Device DB list********************");
379 
380   for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++) {
381     log::verbose("kdev[{}] in_use[{}]  handle[{}]", xx,
382                  bta_hh_cb.kdev[xx].in_use, bta_hh_cb.kdev[xx].hid_handle);
383 
384     log::verbose(
385         "\t\t\t attr_mask[{:04x}] state [{}] sub_class[{:02x}] index = {}",
386         bta_hh_cb.kdev[xx].attr_mask, bta_hh_cb.kdev[xx].state,
387         bta_hh_cb.kdev[xx].sub_class, bta_hh_cb.kdev[xx].index);
388   }
389   log::verbose("*********************************************************");
390 }
391 #endif
392