1 /******************************************************************************
3 * Copyright (C) 2005-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
20 #include "bt_target.h"
21 #if (BTA_HH_INCLUDED == TRUE)
24 #include "bta_hh_int.h"
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)
30 /*****************************************************************************
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
38 #define BTA_HH_KB_CAPS_LOCK 0x39 /* caps lock */
39 #define BTA_HH_KB_NUM_LOCK 0x53 /* num lock */
42 #define BTA_HH_MAX_RPT_CHARS 8
44 static const uint8_t bta_hh_mod_key_mask[BTA_HH_MOD_MAX_KEY] =
53 /*******************************************************************************
55 ** Function bta_hh_find_cb
57 ** Description Find best available control block according to BD address.
62 *******************************************************************************/
63 uint8_t bta_hh_find_cb(BD_ADDR bda)
67 /* See how many active devices there are. */
68 for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++)
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) )
74 #if (BTA_HH_DEBUG == TRUE)
75 APPL_TRACE_DEBUG("found kdev_cb[%d] hid_handle = %d ", xx,
76 bta_hh_cb.kdev[xx].hid_handle)
80 #if (BTA_HH_DEBUG == TRUE)
82 APPL_TRACE_DEBUG("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);
89 /* if no active device match, find a spot for it */
90 for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++)
92 if (!bta_hh_cb.kdev[xx].in_use)
94 bdcpy(bta_hh_cb.kdev[xx].addr, bda);
98 /* If device list full, report BTA_HH_IDX_INVALID */
99 #if (BTA_HH_DEBUG == TRUE)
100 APPL_TRACE_DEBUG("bta_hh_find_cb:: index = %d while max = %d",
101 xx, BTA_HH_MAX_DEVICE);
104 if (xx == BTA_HH_MAX_DEVICE)
105 xx = BTA_HH_IDX_INVALID;
110 /*******************************************************************************
112 ** Function bta_hh_clean_up_kdev
114 ** Description Clean up device control block when device is removed from
115 ** manitainace list, and update control block index map.
119 *******************************************************************************/
120 void bta_hh_clean_up_kdev(tBTA_HH_DEV_CB *p_cb)
124 if (p_cb->hid_handle != BTA_HH_INVALID_HANDLE )
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;
131 bta_hh_cb.cb_index[p_cb->hid_handle] = BTA_HH_IDX_INVALID;
134 /* reset device control block */
135 index = p_cb->index; /* Preserve index for this control block */
137 /* Free buffer for report descriptor info */
138 osi_free_and_reset((void **)&p_cb->dscp_info.descriptor.dsc_list);
140 memset(p_cb, 0, sizeof(tBTA_HH_DEV_CB)); /* Reset control block */
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;
147 /*******************************************************************************
149 ** Function bta_hh_update_di_info
151 ** Description Maintain a known device list for BTA HH.
155 *******************************************************************************/
156 void bta_hh_update_di_info(tBTA_HH_DEV_CB *p_cb, uint16_t vendor_id, uint16_t product_id,
157 uint16_t version, uint8_t flag)
159 #if (BTA_HH_DEBUG == TRUE)
160 APPL_TRACE_DEBUG("vendor_id = 0x%2x product_id = 0x%2x version = 0x%2x",
161 vendor_id, product_id, version);
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 (BTA_HH_LE_INCLUDED == TRUE)
167 p_cb->dscp_info.flag = flag;
172 /*******************************************************************************
174 ** Function bta_hh_add_device_to_list
176 ** Description Maintain a known device list for BTA HH.
180 *******************************************************************************/
181 void bta_hh_add_device_to_list(tBTA_HH_DEV_CB *p_cb, uint8_t handle,
183 tHID_DEV_DSCP_INFO *p_dscp_info,
185 uint16_t ssr_max_latency,
186 uint16_t ssr_min_tout,
189 #if (BTA_HH_DEBUG == TRUE)
190 APPL_TRACE_DEBUG("subclass = 0x%2x", sub_class);
193 p_cb->hid_handle = handle;
195 p_cb->attr_mask = attr_mask;
197 p_cb->sub_class = sub_class;
198 p_cb->app_id = app_id;
200 p_cb->dscp_info.ssr_max_latency = ssr_max_latency;
201 p_cb->dscp_info.ssr_min_tout = ssr_min_tout;
203 /* store report descriptor info */
205 osi_free_and_reset((void **)&p_cb->dscp_info.descriptor.dsc_list);
207 if (p_dscp_info->dl_len) {
208 p_cb->dscp_info.descriptor.dsc_list =
209 (uint8_t *)osi_malloc(p_dscp_info->dl_len);
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);
217 /*******************************************************************************
219 ** Function bta_hh_tod_spt
221 ** Description Check to see if this type of device is supported
225 *******************************************************************************/
226 bool bta_hh_tod_spt(tBTA_HH_DEV_CB *p_cb,uint8_t sub_class)
229 uint8_t cod = (sub_class >> 2); /* lower two bits are reserved */
231 for (xx = 0 ; xx < p_bta_hh_cfg->max_devt_spt; xx ++)
233 if (cod == (uint8_t) p_bta_hh_cfg->p_devt_list[xx].tod)
235 p_cb->app_id = p_bta_hh_cfg->p_devt_list[xx].app_id;
236 #if (BTA_HH_DEBUG == TRUE)
237 APPL_TRACE_EVENT("bta_hh_tod_spt sub_class:0x%x supported", sub_class);
242 #if (BTA_HH_DEBUG == TRUE)
243 APPL_TRACE_EVENT("bta_hh_tod_spt sub_class:0x%x NOT supported", sub_class);
249 /*******************************************************************************
251 ** Function bta_hh_parse_keybd_rpt
253 ** Description This utility function parse a boot mode keyboard report.
257 *******************************************************************************/
258 void bta_hh_parse_keybd_rpt(tBTA_HH_BOOT_RPT *p_kb_data, uint8_t *p_report,
261 tBTA_HH_KB_CB *p_kb = &bta_hh_cb.kb_cb;
262 tBTA_HH_KEYBD_RPT *p_data = &p_kb_data->data_rpt.keybd_rpt;
264 uint8_t this_char, ctl_shift;
265 uint16_t xx, yy, key_idx = 0;
266 uint8_t this_report[BTA_HH_MAX_RPT_CHARS];
268 #if (BTA_HH_DEBUG == TRUE)
269 APPL_TRACE_DEBUG("bta_hh_parse_keybd_rpt: (report=%p, report_len=%d) called",
270 p_report, report_len);
276 ctl_shift = *p_report++;
279 if (report_len > BTA_HH_MAX_RPT_CHARS)
280 report_len = BTA_HH_MAX_RPT_CHARS;
282 memset (this_report, 0, BTA_HH_MAX_RPT_CHARS);
283 memset (p_data, 0, sizeof(tBTA_HH_KEYBD_RPT));
284 memcpy (this_report, p_report, report_len);
286 /* Take care of shift, control, GUI and alt, modifier keys */
287 for (xx = 0; xx < BTA_HH_MOD_MAX_KEY; xx ++ )
289 if (ctl_shift & bta_hh_mod_key_mask[xx])
291 APPL_TRACE_DEBUG("Mod Key[%02x] pressed", bta_hh_mod_key_mask[xx] );
292 p_kb->mod_key[xx] = true;
294 else if (p_kb->mod_key[xx])
296 p_kb->mod_key[xx] = false;
298 /* control key flag is set */
299 p_data->mod_key[xx] = p_kb->mod_key[xx];
302 /***************************************************************************/
303 /* First step is to remove all characters we saw in the last report */
304 /***************************************************************************/
305 for (xx = 0; xx < report_len; xx++)
307 for (yy = 0; yy < BTA_HH_MAX_RPT_CHARS; yy++)
309 if (this_report[xx] == p_kb->last_report[yy])
315 /***************************************************************************/
316 /* Now, process all the characters in the report, up to 6 keycodes */
317 /***************************************************************************/
318 for (xx = 0; xx < report_len; xx++)
320 #if (BTA_HH_DEBUG == TRUE)
321 APPL_TRACE_DEBUG("this_char = %02x", this_report[xx]);
323 if ((this_char = this_report[xx]) == 0)
325 /* take the key code as the report data */
326 if (this_report[xx] == BTA_HH_KB_CAPS_LOCK)
327 p_kb->caps_lock = p_kb->caps_lock ? false : true;
328 else if (this_report[xx] == BTA_HH_KB_NUM_LOCK)
329 p_kb->num_lock = p_kb->num_lock ? false : true;
331 p_data->this_char[key_idx ++] = this_char;
333 #if (BTA_HH_DEBUG == TRUE)
334 APPL_TRACE_DEBUG("found keycode %02x ", this_report[xx]);
336 p_data->caps_lock = p_kb->caps_lock;
337 p_data->num_lock = p_kb->num_lock;
340 memset (p_kb->last_report, 0, BTA_HH_MAX_RPT_CHARS);
341 memcpy (p_kb->last_report, p_report, report_len);
346 /*******************************************************************************
348 ** Function bta_hh_parse_mice_rpt
350 ** Description This utility function parse a boot mode mouse report.
354 *******************************************************************************/
355 void bta_hh_parse_mice_rpt(tBTA_HH_BOOT_RPT *p_mice_data, uint8_t *p_report,
358 tBTA_HH_MICE_RPT *p_data = &p_mice_data->data_rpt.mice_rpt;
359 #if (BTA_HH_DEBUG == TRUE)
362 APPL_TRACE_DEBUG("bta_hh_parse_mice_rpt: bta_keybd_rpt_rcvd(report=%p, \
363 report_len=%d) called", p_report, report_len);
369 if (report_len > BTA_HH_MAX_RPT_CHARS)
370 report_len = BTA_HH_MAX_RPT_CHARS;
372 #if (BTA_HH_DEBUG == TRUE)
373 for (xx = 0; xx < report_len; xx++)
375 APPL_TRACE_DEBUG("this_char = %02x", p_report[xx]);
379 /* only first bytes lower 3 bits valid */
380 p_data->mouse_button = (p_report[0] & 0x07);
383 p_data->delta_x = p_report[1];
386 p_data->delta_y = p_report[2];
388 #if (BTA_HH_DEBUG == TRUE)
389 APPL_TRACE_DEBUG("mice button: 0x%2x", p_data->mouse_button);
390 APPL_TRACE_DEBUG("mice move: x = %d y = %d", p_data->delta_x,
398 /*******************************************************************************
400 ** Function bta_hh_read_ssr_param
402 ** Description Read the SSR Parameter for the remote device
404 ** Returns tBTA_HH_STATUS operation status
406 *******************************************************************************/
407 tBTA_HH_STATUS bta_hh_read_ssr_param(BD_ADDR bd_addr, uint16_t *p_max_ssr_lat, uint16_t *p_min_ssr_tout)
409 tBTA_HH_STATUS status = BTA_HH_ERR;
410 tBTA_HH_CB *p_cb = &bta_hh_cb;
412 uint16_t ssr_max_latency;
413 for (i = 0; i < BTA_HH_MAX_KNOWN; i ++)
415 if (memcmp(p_cb->kdev[i].addr, bd_addr, BD_ADDR_LEN) == 0)
418 /* if remote device does not have HIDSSRHostMaxLatency attribute in SDP,
419 set SSR max latency default value here. */
420 if (p_cb->kdev[i].dscp_info.ssr_max_latency == HID_SSR_PARAM_INVALID)
422 /* The default is calculated as half of link supervision timeout.*/
424 BTM_GetLinkSuperTout(p_cb->kdev[i].addr, &ssr_max_latency) ;
425 ssr_max_latency = BTA_HH_GET_DEF_SSR_MAX_LAT(ssr_max_latency);
427 /* per 1.1 spec, if the newly calculated max latency is greater than
428 BTA_HH_SSR_MAX_LATENCY_DEF which is 500ms, use BTA_HH_SSR_MAX_LATENCY_DEF */
429 if (ssr_max_latency > BTA_HH_SSR_MAX_LATENCY_DEF)
430 ssr_max_latency = BTA_HH_SSR_MAX_LATENCY_DEF;
432 * p_max_ssr_lat = ssr_max_latency;
435 * p_max_ssr_lat = p_cb->kdev[i].dscp_info.ssr_max_latency;
437 if (p_cb->kdev[i].dscp_info.ssr_min_tout == HID_SSR_PARAM_INVALID)
438 * p_min_ssr_tout = BTA_HH_SSR_MIN_TOUT_DEF;
440 * p_min_ssr_tout = p_cb->kdev[i].dscp_info.ssr_min_tout;
451 /*******************************************************************************
453 ** Function bta_hh_cleanup_disable
455 ** Description when disable finished, cleanup control block and send callback
460 *******************************************************************************/
461 void bta_hh_cleanup_disable(tBTA_HH_STATUS status)
464 /* free buffer in CB holding report descriptors */
465 for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx ++) {
466 osi_free_and_reset((void **)&bta_hh_cb.kdev[xx].dscp_info.descriptor.dsc_list);
468 osi_free_and_reset((void **)&bta_hh_cb.p_disc_db);
470 (* bta_hh_cb.p_cback)(BTA_HH_DISABLE_EVT, (tBTA_HH *)&status);
471 /* all connections are down, no waiting for diconnect */
472 memset(&bta_hh_cb, 0, sizeof(tBTA_HH_CB));
475 /*******************************************************************************
477 ** Function bta_hh_dev_handle_to_cb_idx
479 ** Description convert a HID device handle to the device control block index.
482 ** Returns uint8_t: index of the device control block.
484 *******************************************************************************/
485 uint8_t bta_hh_dev_handle_to_cb_idx(uint8_t dev_handle)
487 uint8_t index = BTA_HH_IDX_INVALID;
489 #if (BTA_HH_LE_INCLUDED == TRUE)
490 if (BTA_HH_IS_LE_DEV_HDL(dev_handle))
492 if (BTA_HH_IS_LE_DEV_HDL_VALID(dev_handle))
493 index = bta_hh_cb.le_cb_index[BTA_HH_GET_LE_CB_IDX(dev_handle)];
494 #if (BTA_HH_DEBUG == TRUE)
495 APPL_TRACE_DEBUG("bta_hh_dev_handle_to_cb_idx dev_handle = %d index = %d", dev_handle, index);
500 /* regular HID device checking */
501 if (dev_handle < BTA_HH_MAX_KNOWN )
502 index = bta_hh_cb.cb_index[dev_handle];
507 #if (BTA_HH_DEBUG == TRUE)
508 /*******************************************************************************
510 ** Function bta_hh_trace_dev_db
512 ** Description Check to see if this type of device is supported
516 *******************************************************************************/
517 void bta_hh_trace_dev_db(void)
521 APPL_TRACE_DEBUG("bta_hh_trace_dev_db:: Device DB list********************");
523 for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++)
525 APPL_TRACE_DEBUG("kdev[%d] in_use[%d] handle[%d] ",xx,
526 bta_hh_cb.kdev[xx].in_use, bta_hh_cb.kdev[xx].hid_handle);
528 APPL_TRACE_DEBUG("\t\t\t attr_mask[%04x] state [%d] sub_class[%02x] index = %d",
529 bta_hh_cb.kdev[xx].attr_mask, bta_hh_cb.kdev[xx].state,
530 bta_hh_cb.kdev[xx].sub_class, bta_hh_cb.kdev[xx].index);
532 APPL_TRACE_DEBUG("*********************************************************");
535 #endif /* HL_INCLUDED */