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