1 /******************************************************************************
2  *
3  *  Copyright (C) 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 #include <string.h>
19 
20 #include "common/bt_target.h"
21 #if defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE)
22 
23 #include "osi/allocator.h"
24 #include "bta_hh_int.h"
25 
26 /* if SSR max latency is not defined by remote device, set the default value
27    as half of the link supervision timeout */
28 #define BTA_HH_GET_DEF_SSR_MAX_LAT(x)   ((x)>> 1)
29 
30 /*****************************************************************************
31 **  Constants
32 *****************************************************************************/
33 #define BTA_HH_KB_CTRL_MASK         0x11
34 #define BTA_HH_KB_SHIFT_MASK        0x22
35 #define BTA_HH_KB_ALT_MASK          0x44
36 #define BTA_HH_KB_GUI_MASK          0x88
37 
38 #define BTA_HH_KB_CAPS_LOCK      0x39           /* caps lock */
39 #define BTA_HH_KB_NUM_LOCK       0x53           /* num lock */
40 
41 
42 #define BTA_HH_MAX_RPT_CHARS    8
43 
44 static const UINT8 bta_hh_mod_key_mask[BTA_HH_MOD_MAX_KEY] = {
45     BTA_HH_KB_CTRL_MASK,
46     BTA_HH_KB_SHIFT_MASK,
47     BTA_HH_KB_ALT_MASK,
48     BTA_HH_KB_GUI_MASK
49 };
50 
51 
52 /*******************************************************************************
53 **
54 ** Function         bta_hh_find_cb
55 **
56 ** Description      Find best available control block according to BD address.
57 **
58 **
59 ** Returns          void
60 **
61 *******************************************************************************/
bta_hh_find_cb(BD_ADDR bda)62 UINT8  bta_hh_find_cb(BD_ADDR bda)
63 {
64     UINT8 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 ((!bdcmp (bda, bta_hh_cb.kdev[xx].addr) &&
70                 bdcmp(bda, bd_addr_null) != 0) ) {
71 #if BTA_HH_DEBUG
72             APPL_TRACE_DEBUG("found kdev_cb[%d] hid_handle = %d ", xx,
73                              bta_hh_cb.kdev[xx].hid_handle)
74 #endif
75             return xx;
76         }
77 #if BTA_HH_DEBUG
78         else {
79             APPL_TRACE_DEBUG("in_use ? [%d] kdev[%d].hid_handle = %d state = [%d]",
80                              bta_hh_cb.kdev[xx].in_use, xx,
81                              bta_hh_cb.kdev[xx].hid_handle,
82                              bta_hh_cb.kdev[xx].state);
83         }
84 #endif
85     }
86 
87     /* if no active device match, find a spot for it */
88     for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++) {
89         if (!bta_hh_cb.kdev[xx].in_use) {
90             bdcpy(bta_hh_cb.kdev[xx].addr, bda);
91             break;
92         }
93     }
94     /* If device list full, report BTA_HH_IDX_INVALID */
95 #if BTA_HH_DEBUG
96     APPL_TRACE_DEBUG("bta_hh_find_cb:: index = %d while max = %d",
97                      xx, BTA_HH_MAX_DEVICE);
98 #endif
99 
100     if (xx == BTA_HH_MAX_DEVICE) {
101         xx = BTA_HH_IDX_INVALID;
102     }
103 
104     return xx;
105 }
106 
107 /*******************************************************************************
108 **
109 ** Function         bta_hh_clean_up_kdev
110 **
111 ** Description      Clean up device control block when device is removed from
112 **                  manitainace list, and update control block index map.
113 **
114 ** Returns          void
115 **
116 *******************************************************************************/
bta_hh_clean_up_kdev(tBTA_HH_DEV_CB * p_cb)117 void bta_hh_clean_up_kdev(tBTA_HH_DEV_CB *p_cb)
118 {
119     UINT8 index;
120 
121     if (p_cb->hid_handle != BTA_HH_INVALID_HANDLE ) {
122 #if BTA_HH_LE_INCLUDED == TRUE
123         if (p_cb->is_le_device) {
124             bta_hh_cb.le_cb_index[BTA_HH_GET_LE_CB_IDX(p_cb->hid_handle)] = BTA_HH_IDX_INVALID;
125         } else
126 #endif
127         {
128             bta_hh_cb.cb_index[p_cb->hid_handle] = BTA_HH_IDX_INVALID;
129         }
130     }
131 
132     /* reset device control block */
133     index = p_cb->index;                        /* Preserve index for this control block */
134 
135     /* Free buffer for report descriptor info */
136     utl_freebuf((void **)&p_cb->dscp_info.descriptor.dsc_list);
137 
138     memset(p_cb, 0, sizeof (tBTA_HH_DEV_CB));   /* Reset control block */
139 
140     p_cb->index = index;                        /* Restore index for this control block */
141     p_cb->state      = BTA_HH_IDLE_ST;
142     p_cb->hid_handle = BTA_HH_INVALID_HANDLE;
143 
144 }
145 /*******************************************************************************
146 **
147 ** Function         bta_hh_update_di_info
148 **
149 ** Description      Maintain a known device list for BTA HH.
150 **
151 ** Returns          void
152 **
153 *******************************************************************************/
bta_hh_update_di_info(tBTA_HH_DEV_CB * p_cb,UINT16 vendor_id,UINT16 product_id,UINT16 version,UINT8 flag)154 void bta_hh_update_di_info(tBTA_HH_DEV_CB *p_cb, UINT16 vendor_id, UINT16 product_id,
155                            UINT16 version, UINT8 flag)
156 {
157 #if BTA_HH_DEBUG
158     APPL_TRACE_DEBUG("vendor_id = 0x%2x product_id = 0x%2x version = 0x%2x",
159                      vendor_id, product_id, version);
160 #endif
161     p_cb->dscp_info.vendor_id     =   vendor_id;
162     p_cb->dscp_info.product_id    =   product_id;
163     p_cb->dscp_info.version       =   version;
164 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
165     p_cb->dscp_info.flag          =   flag;
166 #else
167     UNUSED(flag);
168 #endif
169 }
170 /*******************************************************************************
171 **
172 ** Function         bta_hh_add_device_to_list
173 **
174 ** Description      Maintain a known device list for BTA HH.
175 **
176 ** Returns          void
177 **
178 *******************************************************************************/
bta_hh_add_device_to_list(tBTA_HH_DEV_CB * p_cb,UINT8 handle,UINT16 attr_mask,tHID_DEV_DSCP_INFO * p_dscp_info,UINT8 sub_class,UINT16 ssr_max_latency,UINT16 ssr_min_tout,UINT8 app_id)179 void bta_hh_add_device_to_list(tBTA_HH_DEV_CB *p_cb, UINT8 handle,
180                                UINT16 attr_mask,
181                                tHID_DEV_DSCP_INFO *p_dscp_info,
182                                UINT8 sub_class,
183                                UINT16 ssr_max_latency,
184                                UINT16 ssr_min_tout,
185                                UINT8 app_id)
186 {
187 #if BTA_HH_DEBUG
188     APPL_TRACE_DEBUG("subclass = 0x%2x", sub_class);
189 #endif
190 
191     p_cb->hid_handle = handle;
192     p_cb->in_use = TRUE;
193     p_cb->attr_mask = attr_mask;
194 
195     p_cb->sub_class = sub_class;
196     p_cb->app_id    = app_id;
197 
198     p_cb->dscp_info.ssr_max_latency = ssr_max_latency;
199     p_cb->dscp_info.ssr_min_tout    = ssr_min_tout;
200 
201     /* store report descriptor info */
202     if ( p_dscp_info) {
203         utl_freebuf((void **)&p_cb->dscp_info.descriptor.dsc_list);
204 
205         if (p_dscp_info->dl_len &&
206                 (p_cb->dscp_info.descriptor.dsc_list =
207                      (UINT8 *)osi_malloc(p_dscp_info->dl_len)) != NULL) {
208             p_cb->dscp_info.descriptor.dl_len = p_dscp_info->dl_len;
209             memcpy(p_cb->dscp_info.descriptor.dsc_list, p_dscp_info->dsc_list,
210                    p_dscp_info->dl_len);
211         }
212     }
213     return;
214 }
215 
216 /*******************************************************************************
217 **
218 ** Function         bta_hh_tod_spt
219 **
220 ** Description      Check to see if this type of device is supported
221 **
222 ** Returns
223 **
224 *******************************************************************************/
bta_hh_tod_spt(tBTA_HH_DEV_CB * p_cb,UINT8 sub_class)225 BOOLEAN bta_hh_tod_spt(tBTA_HH_DEV_CB *p_cb, UINT8 sub_class)
226 {
227     UINT8    xx;
228     UINT8    cod = (sub_class >> 2); /* lower two bits are reserved */
229 
230     for (xx = 0 ; xx < p_bta_hh_cfg->max_devt_spt; xx ++) {
231         if (cod == (UINT8) p_bta_hh_cfg->p_devt_list[xx].tod) {
232             p_cb->app_id = p_bta_hh_cfg->p_devt_list[xx].app_id;
233 #if BTA_HH_DEBUG
234             APPL_TRACE_EVENT("bta_hh_tod_spt sub_class:0x%x supported", sub_class);
235 #endif
236             return TRUE;
237         }
238     }
239 #if BTA_HH_DEBUG
240     APPL_TRACE_ERROR("bta_hh_tod_spt sub_class:0x%x NOT supported", sub_class);
241 #endif
242     return FALSE;
243 }
244 
245 
246 /*******************************************************************************
247 **
248 ** Function         bta_hh_parse_keybd_rpt
249 **
250 ** Description      This utility function parse a boot mode keyboard report.
251 **
252 ** Returns          void
253 **
254 *******************************************************************************/
bta_hh_parse_keybd_rpt(tBTA_HH_BOOT_RPT * p_kb_data,UINT8 * p_report,UINT16 report_len)255 void bta_hh_parse_keybd_rpt(tBTA_HH_BOOT_RPT *p_kb_data, UINT8 *p_report,
256                             UINT16 report_len)
257 {
258     tBTA_HH_KB_CB       *p_kb = &bta_hh_cb.kb_cb;
259     tBTA_HH_KEYBD_RPT   *p_data = &p_kb_data->data_rpt.keybd_rpt;
260 
261     UINT8        this_char, ctl_shift;
262     UINT16       xx, yy, key_idx = 0;
263     UINT8        this_report[BTA_HH_MAX_RPT_CHARS];
264 
265 #if BTA_HH_DEBUG
266     APPL_TRACE_DEBUG("bta_hh_parse_keybd_rpt:  (report=%p, report_len=%d) called",
267                      p_report, report_len);
268 #endif
269 
270     if (report_len < 2) {
271         return;
272     }
273 
274     ctl_shift = *p_report++;
275     report_len--;
276 
277     if (report_len > BTA_HH_MAX_RPT_CHARS) {
278         report_len = BTA_HH_MAX_RPT_CHARS;
279     }
280 
281     memset (this_report, 0, BTA_HH_MAX_RPT_CHARS);
282     memset (p_data, 0, sizeof(tBTA_HH_KEYBD_RPT));
283     memcpy (this_report, p_report, report_len);
284 
285     /* Take care of shift, control, GUI and alt, modifier keys  */
286     for (xx = 0; xx < BTA_HH_MOD_MAX_KEY; xx ++ ) {
287         if (ctl_shift & bta_hh_mod_key_mask[xx]) {
288             APPL_TRACE_DEBUG("Mod Key[%02x] pressed", bta_hh_mod_key_mask[xx] );
289             p_kb->mod_key[xx] = TRUE;
290         } else if (p_kb->mod_key[xx]) {
291             p_kb->mod_key[xx] = FALSE;
292         }
293         /* control key flag is set */
294         p_data->mod_key[xx]       = p_kb->mod_key[xx];
295     }
296 
297     /***************************************************************************/
298     /*  First step is to remove all characters we saw in the last report       */
299     /***************************************************************************/
300     for (xx = 0; xx < report_len; xx++) {
301         for (yy = 0; yy < BTA_HH_MAX_RPT_CHARS; yy++) {
302             if (this_report[xx] == p_kb->last_report[yy]) {
303                 this_report[xx] = 0;
304             }
305         }
306     }
307     /***************************************************************************/
308     /*  Now, process all the characters in the report, up to 6 keycodes        */
309     /***************************************************************************/
310     for (xx = 0; xx < report_len; xx++) {
311 #if BTA_HH_DEBUG
312         APPL_TRACE_DEBUG("this_char = %02x", this_report[xx]);
313 #endif
314         if ((this_char = this_report[xx]) == 0) {
315             continue;
316         }
317         /* take the key code as the report data */
318         if (this_report[xx] == BTA_HH_KB_CAPS_LOCK) {
319             p_kb->caps_lock = p_kb->caps_lock ? FALSE : TRUE;
320         } else if (this_report[xx] == BTA_HH_KB_NUM_LOCK) {
321             p_kb->num_lock = p_kb->num_lock ? FALSE : TRUE;
322         } else {
323             p_data->this_char[key_idx ++] = this_char;
324         }
325 
326 #if BTA_HH_DEBUG
327         APPL_TRACE_DEBUG("found keycode %02x ",  this_report[xx]);
328 #endif
329         p_data->caps_lock   = p_kb->caps_lock;
330         p_data->num_lock      = p_kb->num_lock;
331     }
332 
333     memset (p_kb->last_report, 0, BTA_HH_MAX_RPT_CHARS);
334     memcpy (p_kb->last_report, p_report, report_len);
335 
336     return;
337 }
338 
339 /*******************************************************************************
340 **
341 ** Function         bta_hh_parse_mice_rpt
342 **
343 ** Description      This utility function parse a boot mode mouse report.
344 **
345 ** Returns          void
346 **
347 *******************************************************************************/
bta_hh_parse_mice_rpt(tBTA_HH_BOOT_RPT * p_mice_data,UINT8 * p_report,UINT16 report_len)348 void bta_hh_parse_mice_rpt(tBTA_HH_BOOT_RPT *p_mice_data, UINT8 *p_report,
349                            UINT16 report_len)
350 {
351     tBTA_HH_MICE_RPT   *p_data = &p_mice_data->data_rpt.mice_rpt;
352 #if BTA_HH_DEBUG
353     UINT8       xx;
354 
355     APPL_TRACE_DEBUG("bta_hh_parse_mice_rpt:  bta_keybd_rpt_rcvd(report=%p, \
356                 report_len=%d) called", p_report, report_len);
357 #endif
358 
359     if (report_len < 3) {
360         return;
361     }
362 
363     if (report_len > BTA_HH_MAX_RPT_CHARS) {
364         report_len = BTA_HH_MAX_RPT_CHARS;
365     }
366 
367 #if BTA_HH_DEBUG
368     for (xx = 0; xx < report_len; xx++) {
369         APPL_TRACE_DEBUG("this_char = %02x", p_report[xx]);
370     }
371 #endif
372 
373     /* only first bytes lower 3 bits valid */
374     p_data->mouse_button     = (p_report[0] & 0x07);
375 
376     /* x displacement */
377     p_data->delta_x     = p_report[1];
378 
379     /* y displacement */
380     p_data->delta_y     = p_report[2];
381 
382 #if BTA_HH_DEBUG
383     APPL_TRACE_DEBUG("mice button: 0x%2x", p_data->mouse_button);
384     APPL_TRACE_DEBUG("mice move: x = %d y = %d", p_data->delta_x,
385                      p_data->delta_y );
386 #endif
387 
388     return;
389 
390 }
391 
392 /*******************************************************************************
393 **
394 ** Function         bta_hh_read_ssr_param
395 **
396 ** Description      Read the SSR Parameter for the remote device
397 **
398 ** Returns          tBTA_HH_STATUS  operation status
399 **
400 *******************************************************************************/
bta_hh_read_ssr_param(BD_ADDR bd_addr,UINT16 * p_max_ssr_lat,UINT16 * p_min_ssr_tout)401 tBTA_HH_STATUS bta_hh_read_ssr_param(BD_ADDR bd_addr, UINT16 *p_max_ssr_lat, UINT16 *p_min_ssr_tout)
402 {
403     tBTA_HH_STATUS  status = BTA_HH_ERR;
404     tBTA_HH_CB  *p_cb = &bta_hh_cb;
405     UINT8       i;
406     UINT16      ssr_max_latency;
407     for (i = 0; i < BTA_HH_MAX_KNOWN; i ++) {
408         if (memcmp(p_cb->kdev[i].addr, bd_addr, BD_ADDR_LEN) == 0) {
409 
410             /* if remote device does not have HIDSSRHostMaxLatency attribute in SDP,
411             set SSR max latency default value here.  */
412             if (p_cb->kdev[i].dscp_info.ssr_max_latency == HID_SSR_PARAM_INVALID) {
413                 /* The default is calculated as half of link supervision timeout.*/
414 
415                 BTM_GetLinkSuperTout(p_cb->kdev[i].addr, &ssr_max_latency) ;
416                 ssr_max_latency = BTA_HH_GET_DEF_SSR_MAX_LAT(ssr_max_latency);
417 
418                 /* per 1.1 spec, if the newly calculated max latency is greater than
419                 BTA_HH_SSR_MAX_LATENCY_DEF which is 500ms, use BTA_HH_SSR_MAX_LATENCY_DEF */
420                 if (ssr_max_latency > BTA_HH_SSR_MAX_LATENCY_DEF) {
421                     ssr_max_latency = BTA_HH_SSR_MAX_LATENCY_DEF;
422                 }
423 
424                 * p_max_ssr_lat  = ssr_max_latency;
425             } else {
426                 * p_max_ssr_lat  = p_cb->kdev[i].dscp_info.ssr_max_latency;
427             }
428 
429             if (p_cb->kdev[i].dscp_info.ssr_min_tout == HID_SSR_PARAM_INVALID) {
430                 * p_min_ssr_tout = BTA_HH_SSR_MIN_TOUT_DEF;
431             } else {
432                 * p_min_ssr_tout = p_cb->kdev[i].dscp_info.ssr_min_tout;
433             }
434 
435             status           = BTA_HH_OK;
436 
437             break;
438         }
439     }
440 
441     return status;
442 }
443 
444 /*******************************************************************************
445 **
446 ** Function         bta_hh_cleanup_disable
447 **
448 ** Description      when disable finished, cleanup control block and send callback
449 **
450 **
451 ** Returns          void
452 **
453 *******************************************************************************/
bta_hh_cleanup_disable(tBTA_HH_STATUS status)454 void bta_hh_cleanup_disable(tBTA_HH_STATUS status)
455 {
456     UINT8   xx;
457     /* free buffer in CB holding report descriptors */
458     for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx ++) {
459         utl_freebuf((void **)&bta_hh_cb.kdev[xx].dscp_info.descriptor.dsc_list);
460     }
461     utl_freebuf((void **)&bta_hh_cb.p_disc_db);
462 
463     if (bta_hh_cb.p_cback) {
464         (*bta_hh_cb.p_cback)(BTA_HH_DISABLE_EVT, (tBTA_HH*)&status);
465         /* all connections are down, no waiting for diconnect */
466         memset(&bta_hh_cb, 0, sizeof(tBTA_HH_CB));
467     }
468 }
469 
470 /*******************************************************************************
471 **
472 ** Function         bta_hh_dev_handle_to_cb_idx
473 **
474 ** Description      convert a HID device handle to the device control block index.
475 **
476 **
477 ** Returns          UINT8: index of the device control block.
478 **
479 *******************************************************************************/
bta_hh_dev_handle_to_cb_idx(UINT8 dev_handle)480 UINT8 bta_hh_dev_handle_to_cb_idx(UINT8 dev_handle)
481 {
482     UINT8 index = BTA_HH_IDX_INVALID;
483 
484 #if BTA_HH_LE_INCLUDED == TRUE
485     if (BTA_HH_IS_LE_DEV_HDL(dev_handle)) {
486         if (BTA_HH_IS_LE_DEV_HDL_VALID(dev_handle)) {
487             index = bta_hh_cb.le_cb_index[BTA_HH_GET_LE_CB_IDX(dev_handle)];
488         }
489 #if BTA_HH_DEBUG == TRUE
490         APPL_TRACE_DEBUG("bta_hh_dev_handle_to_cb_idx dev_handle = %d index = %d", dev_handle, index);
491 #endif
492     } else
493 #endif
494     {
495         /* regular HID device checking */
496         if (dev_handle < BTA_HH_MAX_KNOWN ) {
497             index = bta_hh_cb.cb_index[dev_handle];
498         }
499     }
500     return index;
501 
502 }
503 #if BTA_HH_DEBUG
504 /*******************************************************************************
505 **
506 ** Function         bta_hh_trace_dev_db
507 **
508 ** Description      Check to see if this type of device is supported
509 **
510 ** Returns
511 **
512 *******************************************************************************/
bta_hh_trace_dev_db(void)513 void bta_hh_trace_dev_db(void)
514 {
515     UINT8    xx;
516 
517     APPL_TRACE_DEBUG("bta_hh_trace_dev_db:: Device DB list********************");
518 
519     for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++) {
520         APPL_TRACE_DEBUG("kdev[%d] in_use[%d]  handle[%d] ", xx,
521                          bta_hh_cb.kdev[xx].in_use, bta_hh_cb.kdev[xx].hid_handle);
522 
523         APPL_TRACE_DEBUG("\t\t\t attr_mask[%04x] state [%d] sub_class[%02x] index = %d",
524                          bta_hh_cb.kdev[xx].attr_mask, bta_hh_cb.kdev[xx].state,
525                          bta_hh_cb.kdev[xx].sub_class, bta_hh_cb.kdev[xx].index);
526     }
527     APPL_TRACE_DEBUG("*********************************************************");
528 }
529 #endif
530 #endif /* HL_INCLUDED */
531