1 /******************************************************************************
3 * Copyright 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 ******************************************************************************/
19 /******************************************************************************
21 * This file contains the HID host action functions.
23 ******************************************************************************/
25 #include "bt_target.h"
27 #if (BTA_HH_INCLUDED == TRUE)
32 #include "bta_hh_co.h"
33 #include "bta_hh_int.h"
37 #include "osi/include/osi.h"
40 /*****************************************************************************
42 ****************************************************************************/
44 /*****************************************************************************
45 * Local Function prototypes
46 ****************************************************************************/
47 static void bta_hh_cback(uint8_t dev_handle, const RawAddress& addr,
48 uint8_t event, uint32_t data, BT_HDR* pdata);
49 static tBTA_HH_STATUS bta_hh_get_trans_status(uint32_t result);
51 #if (BTA_HH_DEBUG == TRUE)
52 static const char* bta_hh_get_w4_event(uint16_t event);
53 static const char* bta_hh_hid_event_name(uint16_t event);
56 /*****************************************************************************
58 ****************************************************************************/
59 /*******************************************************************************
61 * Function bta_hh_api_enable
63 * Description Perform necessary operations to enable HID host.
68 ******************************************************************************/
69 void bta_hh_api_enable(tBTA_HH_DATA* p_data) {
70 tBTA_HH_STATUS status = BTA_HH_ERR;
73 /* initialize BTE HID */
76 memset(&bta_hh_cb, 0, sizeof(tBTA_HH_CB));
78 HID_HostSetSecurityLevel("", p_data->api_enable.sec_mask);
80 /* Register with L2CAP */
81 if (HID_HostRegister(bta_hh_cback) == HID_SUCCESS) {
82 /* store parameters */
83 bta_hh_cb.p_cback = p_data->api_enable.p_cback;
86 /* initialize device CB */
87 for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++) {
88 bta_hh_cb.kdev[xx].state = BTA_HH_IDLE_ST;
89 bta_hh_cb.kdev[xx].hid_handle = BTA_HH_INVALID_HANDLE;
90 bta_hh_cb.kdev[xx].index = xx;
93 /* initialize control block map */
94 for (xx = 0; xx < BTA_HH_MAX_KNOWN; xx++)
95 bta_hh_cb.cb_index[xx] = BTA_HH_IDX_INVALID;
98 #if (BTA_HH_LE_INCLUDED == TRUE)
99 if (status == BTA_HH_OK) {
104 /* signal BTA call back event */
106 bta_hh.status = status;
107 (*bta_hh_cb.p_cback)(BTA_HH_ENABLE_EVT, &bta_hh);
110 /*******************************************************************************
112 * Function bta_hh_api_disable
114 * Description Perform necessary operations to disable HID host.
119 ******************************************************************************/
120 void bta_hh_api_disable(void) {
123 /* service is not enabled */
124 if (bta_hh_cb.p_cback == NULL) return;
126 /* no live connection, signal DISC_CMPL_EVT directly */
127 if (!bta_hh_cb.cnt_num) {
129 } else /* otherwise, disconnect all live connections */
131 bta_hh_cb.w4_disable = true;
133 for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++) {
134 /* send API_CLOSE event to every connected device */
135 if (bta_hh_cb.kdev[xx].state == BTA_HH_CONN_ST) {
136 /* disconnect all connected devices */
137 bta_hh_sm_execute(&bta_hh_cb.kdev[xx], BTA_HH_API_CLOSE_EVT, NULL);
145 /*******************************************************************************
147 * Function bta_hh_disc_cmpl
149 * Description All connections have been closed, disable service.
154 ******************************************************************************/
155 void bta_hh_disc_cmpl(void) {
156 #if (BTA_HH_LE_INCLUDED == TRUE)
157 HID_HostDeregister();
158 bta_hh_le_deregister();
160 tBTA_HH_STATUS status = BTA_HH_OK;
162 /* Deregister with lower layer */
163 if (HID_HostDeregister() != HID_SUCCESS) status = BTA_HH_ERR;
165 bta_hh_cleanup_disable(status);
169 /*******************************************************************************
171 * Function bta_hh_sdp_cback
173 * Description SDP callback function.
177 ******************************************************************************/
178 static void bta_hh_sdp_cback(uint16_t result, uint16_t attr_mask,
179 tHID_DEV_SDP_INFO* sdp_rec) {
180 tBTA_HH_DEV_CB* p_cb = bta_hh_cb.p_cur;
182 tBTA_HH_STATUS status = BTA_HH_ERR_SDP;
184 /* make sure sdp succeeded and hh has not been disabled */
185 if ((result == SDP_SUCCESS) && (p_cb != NULL)) {
186 /* security is required for the connection, add attr_mask bit*/
187 if (p_cb->sec_mask) attr_mask |= HID_SEC_REQUIRED;
189 #if (BTA_HH_DEBUG == TRUE)
190 APPL_TRACE_EVENT("%s: p_cb: %d result 0x%02x, attr_mask 0x%02x, handle %x",
191 __func__, p_cb, result, attr_mask, p_cb->hid_handle);
194 /* check to see type of device is supported , and should not been added
196 if (bta_hh_tod_spt(p_cb, sdp_rec->sub_class)) {
197 /* if not added before */
198 if (p_cb->hid_handle == BTA_HH_INVALID_HANDLE) {
199 /* add device/update attr_mask information */
200 if (HID_HostAddDev(p_cb->addr, attr_mask, &hdl) == HID_SUCCESS) {
202 /* update cb_index[] map */
203 bta_hh_cb.cb_index[hdl] = p_cb->index;
208 hdl = p_cb->hid_handle;
210 /* else : incoming connection after SDP should update the SDP information
213 if (p_cb->app_id != 0) {
214 /* update cb information with attr_mask, dscp_info etc. */
215 bta_hh_add_device_to_list(p_cb, hdl, attr_mask, &sdp_rec->dscp_info,
216 sdp_rec->sub_class, sdp_rec->ssr_max_latency,
217 sdp_rec->ssr_min_tout, p_cb->app_id);
219 p_cb->dscp_info.ctry_code = sdp_rec->ctry_code;
224 } else /* type of device is not supported */
225 status = BTA_HH_ERR_TOD_UNSPT;
228 /* free disc_db when SDP is completed */
229 osi_free_and_reset((void**)&bta_hh_cb.p_disc_db);
231 /* send SDP_CMPL_EVT into state machine */
232 tBTA_HH_DATA bta_hh_data;
233 bta_hh_data.status = status;
234 bta_hh_sm_execute(p_cb, BTA_HH_SDP_CMPL_EVT, &bta_hh_data);
238 /*******************************************************************************
240 * Function bta_hh_di_sdp_cback
242 * Description SDP DI callback function.
246 ******************************************************************************/
247 static void bta_hh_di_sdp_cback(uint16_t result) {
248 tBTA_HH_DEV_CB* p_cb = bta_hh_cb.p_cur;
249 tBTA_HH_STATUS status = BTA_HH_ERR_SDP;
250 tSDP_DI_GET_RECORD di_rec;
252 #if (BTA_HH_DEBUG == TRUE)
253 APPL_TRACE_EVENT("%s: p_cb: %d result 0x%02x", __func__, p_cb, result);
256 /* if DI record does not exist on remote device, vendor_id in
257 * tBTA_HH_DEV_DSCP_INFO will be set to 0xffff and we will allow the
258 * connection to go through. Spec mandates that DI record be set, but many
259 * HID devices do not set this. So for IOP purposes, we allow the connection
260 * to go through and update the DI record to invalid DI entry.
262 if (((result == SDP_SUCCESS) || (result == SDP_NO_RECS_MATCH)) &&
264 if (result == SDP_SUCCESS &&
265 SDP_GetNumDiRecords(bta_hh_cb.p_disc_db) != 0) {
266 /* always update information with primary DI record */
267 if (SDP_GetDiRecord(1, &di_rec, bta_hh_cb.p_disc_db) == SDP_SUCCESS) {
268 bta_hh_update_di_info(p_cb, di_rec.rec.vendor, di_rec.rec.product,
269 di_rec.rec.version, 0);
272 } else /* no DI recrod available */
274 bta_hh_update_di_info(p_cb, BTA_HH_VENDOR_ID_INVALID, 0, 0, 0);
277 ret = HID_HostGetSDPRecord(p_cb->addr, bta_hh_cb.p_disc_db,
278 p_bta_hh_cfg->sdp_db_size, bta_hh_sdp_cback);
279 if (ret == HID_SUCCESS) {
282 #if (BTA_HH_DEBUG == TRUE)
283 APPL_TRACE_DEBUG("%s: HID_HostGetSDPRecord failed: Status 0x%2x",
289 if (status != BTA_HH_OK) {
290 osi_free_and_reset((void**)&bta_hh_cb.p_disc_db);
291 /* send SDP_CMPL_EVT into state machine */
292 tBTA_HH_DATA bta_hh_data;
293 bta_hh_data.status = status;
294 bta_hh_sm_execute(p_cb, BTA_HH_SDP_CMPL_EVT, &bta_hh_data);
299 /*******************************************************************************
301 * Function bta_hh_start_sdp
303 * Description Start SDP service search, and obtain necessary SDP records.
304 * Only one SDP service search request is allowed at the same
305 * time. For every BTA_HhOpen API call, do SDP first unless SDP
306 * has been done previously.
310 ******************************************************************************/
311 void bta_hh_start_sdp(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
312 tBTA_HH_STATUS status = BTA_HH_ERR_SDP;
315 p_cb->sec_mask = p_data->api_conn.sec_mask;
316 p_cb->mode = p_data->api_conn.mode;
317 bta_hh_cb.p_cur = p_cb;
319 #if (BTA_HH_LE_INCLUDED == TRUE)
320 if (bta_hh_is_le_device(p_cb, p_data->api_conn.bd_addr)) {
321 bta_hh_le_open_conn(p_cb, p_data->api_conn.bd_addr);
326 /* if previously virtually cabled device, skip SDP */
329 #if (BTA_HH_DEBUG == TRUE)
330 APPL_TRACE_DEBUG("%s: skip SDP for known devices", __func__);
332 if (p_cb->hid_handle == BTA_HH_INVALID_HANDLE) {
333 if (HID_HostAddDev(p_cb->addr, p_cb->attr_mask, &hdl) == HID_SUCCESS) {
334 /* update device CB with newly register device handle */
335 bta_hh_add_device_to_list(p_cb, hdl, p_cb->attr_mask, NULL,
337 p_cb->dscp_info.ssr_max_latency,
338 p_cb->dscp_info.ssr_min_tout, p_cb->app_id);
339 /* update cb_index[] map */
340 bta_hh_cb.cb_index[hdl] = p_cb->index;
342 status = BTA_HH_ERR_NO_RES;
344 tBTA_HH_DATA bta_hh_data;
345 bta_hh_data.status = status;
346 bta_hh_sm_execute(p_cb, BTA_HH_SDP_CMPL_EVT, &bta_hh_data);
350 /* GetSDPRecord. at one time only one SDP precedure can be active */
351 else if (!bta_hh_cb.p_disc_db) {
352 bta_hh_cb.p_disc_db =
353 (tSDP_DISCOVERY_DB*)osi_malloc(p_bta_hh_cfg->sdp_db_size);
354 bta_hh_cb.p_cur = p_cb;
355 /* do DI discovery first */
356 if (SDP_DiDiscover(p_data->api_conn.bd_addr, bta_hh_cb.p_disc_db,
357 p_bta_hh_cfg->sdp_db_size,
358 bta_hh_di_sdp_cback) != SDP_SUCCESS) {
359 #if (BTA_HH_DEBUG == TRUE)
360 APPL_TRACE_DEBUG("%s: SDP_DiDiscover failed: Status 0x%2X", __func__,
363 status = BTA_HH_ERR_SDP;
364 osi_free_and_reset((void**)&bta_hh_cb.p_disc_db);
368 } else if (bta_hh_cb.p_disc_db) {
369 /* It is possible that there is incoming/outgoing collision case. DUT
371 * HID connection at same time remote has connected L2CAP for HID control,
372 * so SDP would be in progress, when this flow reaches here. Just do nothing
373 * when the code reaches here, and ongoing SDP completion or failure will
376 APPL_TRACE_DEBUG("%s: ignoring as SDP already in progress", __func__);
380 if (status != BTA_HH_OK) {
381 tBTA_HH_DATA bta_hh_data;
382 bta_hh_data.status = status;
383 bta_hh_sm_execute(p_cb, BTA_HH_SDP_CMPL_EVT, &bta_hh_data);
388 /*******************************************************************************
390 * Function bta_hh_sdp_cmpl
392 * Description When SDP completes, initiate a connection or report an error
393 * depending on the SDP result.
398 ******************************************************************************/
399 void bta_hh_sdp_cmpl(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
400 tBTA_HH_CONN conn_dat;
401 tBTA_HH_STATUS status = p_data->status;
403 #if (BTA_HH_DEBUG == TRUE)
404 APPL_TRACE_DEBUG("%s: status 0x%2X", __func__, p_data->status);
407 /* initialize call back data */
408 memset((void*)&conn_dat, 0, sizeof(tBTA_HH_CONN));
409 conn_dat.handle = p_cb->hid_handle;
410 conn_dat.bda = p_cb->addr;
412 /* if SDP compl success */
413 if (status == BTA_HH_OK) {
414 /* not incoming connection doing SDP, initiate a HID connection */
415 if (!p_cb->incoming_conn) {
417 /* set security level */
418 HID_HostSetSecurityLevel("", p_cb->sec_mask);
420 /* open HID connection */
421 ret = HID_HostOpenDev(p_cb->hid_handle);
422 APPL_TRACE_DEBUG("%s: HID_HostOpenDev returned=%d", __func__, ret);
423 if (ret == HID_SUCCESS || ret == HID_ERR_ALREADY_CONN) {
425 } else if (ret == HID_ERR_CONN_IN_PROCESS) {
426 /* Connection already in progress, return from here, SDP
427 * will be performed after connection is completed.
429 APPL_TRACE_DEBUG("%s: connection already in progress", __func__);
432 #if (BTA_HH_DEBUG == TRUE)
433 APPL_TRACE_DEBUG("%s: HID_HostOpenDev failed: Status 0x%2X", __func__,
436 /* open fail, remove device from management device list */
437 HID_HostRemoveDev(p_cb->hid_handle);
440 } else /* incoming connection SDP finish */
442 bta_hh_sm_execute(p_cb, BTA_HH_OPEN_CMPL_EVT, NULL);
446 if (status != BTA_HH_OK) {
447 /* Check if this was incoming connection request from an unknown device
448 * and connection failed due to missing HID Device SDP UUID
449 * In above condition, disconnect the link as well as remove the
450 * device from list of HID devices
452 if ((status == BTA_HH_ERR_SDP) && (p_cb->incoming_conn) &&
453 (p_cb->app_id == 0)) {
454 APPL_TRACE_DEBUG("%s: SDP failed for incoming conn :hndl %d", __func__,
455 p_cb->incoming_hid_handle);
456 HID_HostRemoveDev(p_cb->incoming_hid_handle);
458 conn_dat.status = status;
459 (*bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH*)&conn_dat);
461 /* move state machine W4_CONN ->IDLE */
462 bta_hh_sm_execute(p_cb, BTA_HH_API_CLOSE_EVT, NULL);
464 /* if this is an outgoing connection to an unknown device, clean up cb */
465 if (p_cb->app_id == 0 && !p_cb->incoming_conn) {
466 /* clean up device control block */
467 bta_hh_clean_up_kdev(p_cb);
469 #if (BTA_HH_DEBUG == TRUE)
470 bta_hh_trace_dev_db();
476 /*******************************************************************************
478 * Function bta_hh_api_disc_act
480 * Description HID Host initiate a disconnection.
485 ******************************************************************************/
486 void bta_hh_api_disc_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
487 tBTA_HH_CBDATA disc_dat;
490 #if (BTA_HH_LE_INCLUDED == TRUE)
491 if (p_cb->is_le_device)
492 bta_hh_le_api_disc_act(p_cb);
496 /* found an active connection */
498 p_data ? (uint8_t)p_data->hdr.layer_specific : p_cb->hid_handle;
499 disc_dat.status = BTA_HH_ERR;
501 status = HID_HostCloseDev(disc_dat.handle);
505 bta_hh.dev_status = disc_dat;
506 (*bta_hh_cb.p_cback)(BTA_HH_CLOSE_EVT, &bta_hh);
512 /*******************************************************************************
514 * Function bta_hh_open_cmpl_act
516 * Description HID host connection completed
521 ******************************************************************************/
522 void bta_hh_open_cmpl_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
525 p_data ? (uint8_t)p_data->hid_cback.hdr.layer_specific : p_cb->hid_handle;
527 memset((void*)&conn, 0, sizeof(tBTA_HH_CONN));
528 conn.handle = dev_handle;
529 conn.bda = p_cb->addr;
531 /* increase connection number */
534 /* initialize device driver */
535 bta_hh_co_open(p_cb->hid_handle, p_cb->sub_class, p_cb->attr_mask,
538 #if (BTA_HH_LE_INCLUDED == TRUE)
539 conn.status = p_cb->status;
540 conn.le_hid = p_cb->is_le_device;
541 conn.scps_supported = p_cb->scps_supported;
543 if (!p_cb->is_le_device)
546 /* inform role manager */
547 bta_sys_conn_open(BTA_ID_HH, p_cb->app_id, p_cb->addr);
549 /* set protocol mode when not default report mode */
550 if (p_cb->mode != BTA_HH_PROTO_RPT_MODE
551 #if (BTA_HH_LE_INCLUDED == TRUE)
552 && !p_cb->is_le_device
555 if ((HID_HostWriteDev(dev_handle, HID_TRANS_SET_PROTOCOL,
556 HID_PAR_PROTOCOL_BOOT_MODE, 0, 0, NULL)) !=
558 /* HID connection is up, while SET_PROTO fail */
559 conn.status = BTA_HH_ERR_PROTO;
560 (*bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH*)&conn);
562 conn.status = BTA_HH_OK;
563 p_cb->w4_evt = BTA_HH_OPEN_EVT;
566 (*bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH*)&conn);
568 p_cb->incoming_conn = false;
569 p_cb->incoming_hid_handle = BTA_HH_INVALID_HANDLE;
571 /*******************************************************************************
573 * Function bta_hh_open_act
575 * Description HID host receive HID_OPEN_EVT .
580 ******************************************************************************/
581 void bta_hh_open_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
582 tBTA_HH_API_CONN conn_data;
585 p_data ? (uint8_t)p_data->hid_cback.hdr.layer_specific : p_cb->hid_handle;
587 #if (BTA_HH_DEBUG == TRUE)
588 APPL_TRACE_EVENT("%s: Device[%d] connected", __func__, dev_handle);
591 /* SDP has been done */
592 if (p_cb->app_id != 0) {
593 bta_hh_sm_execute(p_cb, BTA_HH_OPEN_CMPL_EVT, p_data);
595 /* app_id == 0 indicates an incoming conenction request arrives without SDP
596 * performed, do it first
599 p_cb->incoming_conn = true;
600 /* store the handle here in case sdp fails - need to disconnect */
601 p_cb->incoming_hid_handle = dev_handle;
603 memset(&conn_data, 0, sizeof(tBTA_HH_API_CONN));
604 conn_data.bd_addr = p_cb->addr;
605 bta_hh_start_sdp(p_cb, (tBTA_HH_DATA*)&conn_data);
611 /*******************************************************************************
613 * Function bta_hh_data_act
615 * Description HID Host process a data report
620 ******************************************************************************/
621 void bta_hh_data_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
622 BT_HDR* pdata = p_data->hid_cback.p_data;
623 uint8_t* p_rpt = (uint8_t*)(pdata + 1) + pdata->offset;
625 bta_hh_co_data((uint8_t)p_data->hid_cback.hdr.layer_specific, p_rpt,
626 pdata->len, p_cb->mode, p_cb->sub_class,
627 p_cb->dscp_info.ctry_code, p_cb->addr, p_cb->app_id);
629 osi_free_and_reset((void**)&pdata);
632 /*******************************************************************************
634 * Function bta_hh_handsk_act
636 * Description HID Host process a handshake acknoledgement.
641 ******************************************************************************/
642 void bta_hh_handsk_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
643 #if (BTA_HH_DEBUG == TRUE)
644 APPL_TRACE_DEBUG("HANDSHAKE received for: event = %s data= %d",
645 bta_hh_get_w4_event(p_cb->w4_evt), p_data->hid_cback.data);
649 memset(&bta_hh, 0, sizeof(tBTA_HH));
651 switch (p_cb->w4_evt) {
652 /* GET_ transsaction, handshake indicate unsupported request */
653 case BTA_HH_GET_PROTO_EVT:
654 bta_hh.hs_data.rsp_data.proto_mode = BTA_HH_PROTO_UNKNOWN;
656 case BTA_HH_GET_RPT_EVT:
657 case BTA_HH_GET_IDLE_EVT:
658 bta_hh.hs_data.handle = p_cb->hid_handle;
659 /* if handshake gives an OK code for these transaction, fill in UNSUPT */
660 bta_hh.hs_data.status = bta_hh_get_trans_status(p_data->hid_cback.data);
661 if (bta_hh.hs_data.status == BTA_HH_OK)
662 bta_hh.hs_data.status = BTA_HH_HS_TRANS_NOT_SPT;
664 (*bta_hh_cb.p_cback)(p_cb->w4_evt, &bta_hh);
668 /* acknoledgement from HID device for SET_ transaction */
669 case BTA_HH_SET_RPT_EVT:
670 case BTA_HH_SET_PROTO_EVT:
671 case BTA_HH_SET_IDLE_EVT:
672 bta_hh.dev_status.handle = p_cb->hid_handle;
673 bta_hh.dev_status.status =
674 bta_hh_get_trans_status(p_data->hid_cback.data);
675 (*bta_hh_cb.p_cback)(p_cb->w4_evt, &bta_hh);
679 /* SET_PROTOCOL when open connection */
680 case BTA_HH_OPEN_EVT:
682 p_data->hid_cback.data ? BTA_HH_ERR_PROTO : BTA_HH_OK;
683 bta_hh.conn.handle = p_cb->hid_handle;
684 bta_hh.conn.bda = p_cb->addr;
685 (*bta_hh_cb.p_cback)(p_cb->w4_evt, &bta_hh);
686 #if (BTA_HH_DEBUG == TRUE)
687 bta_hh_trace_dev_db();
693 /* unknow transaction handshake response */
694 APPL_TRACE_DEBUG("unknown transaction type");
698 /* transaction achknoledgement received, inform PM for mode change */
699 bta_sys_idle(BTA_ID_HH, p_cb->app_id, p_cb->addr);
702 /*******************************************************************************
704 * Function bta_hh_ctrl_dat_act
706 * Description HID Host process a data report from control channel.
711 ******************************************************************************/
712 void bta_hh_ctrl_dat_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
713 BT_HDR* pdata = p_data->hid_cback.p_data;
714 uint8_t* data = (uint8_t*)(pdata + 1) + pdata->offset;
715 tBTA_HH_HSDATA hs_data;
717 #if (BTA_HH_DEBUG == TRUE)
718 APPL_TRACE_DEBUG("Ctrl DATA received w4: event[%s]",
719 bta_hh_get_w4_event(p_cb->w4_evt));
721 if (pdata->len == 0) {
722 android_errorWriteLog(0x534e4554, "116108738");
724 osi_free_and_reset((void**)&pdata);
727 hs_data.status = BTA_HH_OK;
728 hs_data.handle = p_cb->hid_handle;
730 switch (p_cb->w4_evt) {
731 case BTA_HH_GET_IDLE_EVT:
732 hs_data.rsp_data.idle_rate = *data;
734 case BTA_HH_GET_RPT_EVT:
735 hs_data.rsp_data.p_rpt_data = pdata;
737 case BTA_HH_GET_PROTO_EVT:
738 /* match up BTE/BTA report/boot mode def*/
739 hs_data.rsp_data.proto_mode = ((*data) == HID_PAR_PROTOCOL_REPORT)
740 ? BTA_HH_PROTO_RPT_MODE
741 : BTA_HH_PROTO_BOOT_MODE;
742 #if (BTA_HH_DEBUG == TRUE)
743 APPL_TRACE_DEBUG("GET_PROTOCOL Mode = [%s]",
744 (hs_data.rsp_data.proto_mode == BTA_HH_PROTO_RPT_MODE)
749 /* should not expect control DATA for SET_ transaction */
750 case BTA_HH_SET_PROTO_EVT:
752 case BTA_HH_SET_RPT_EVT:
754 case BTA_HH_SET_IDLE_EVT:
757 #if (BTA_HH_DEBUG == TRUE)
758 APPL_TRACE_DEBUG("invalid transaction type for DATA payload: 4_evt[%s]",
759 bta_hh_get_w4_event(p_cb->w4_evt));
764 /* inform PM for mode change */
765 bta_sys_busy(BTA_ID_HH, p_cb->app_id, p_cb->addr);
766 bta_sys_idle(BTA_ID_HH, p_cb->app_id, p_cb->addr);
768 (*bta_hh_cb.p_cback)(p_cb->w4_evt, (tBTA_HH*)&hs_data);
771 osi_free_and_reset((void**)&pdata);
774 /*******************************************************************************
776 * Function bta_hh_open_failure
778 * Description report HID open failure when at wait for connection state
779 * and receive device close event.
784 ******************************************************************************/
785 void bta_hh_open_failure(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
786 tBTA_HH_CONN conn_dat;
787 uint32_t reason = p_data->hid_cback.data; /* Reason for closing (32-bit) */
789 memset(&conn_dat, 0, sizeof(tBTA_HH_CONN));
790 conn_dat.handle = p_cb->hid_handle;
792 (reason == HID_ERR_AUTH_FAILED) ? BTA_HH_ERR_AUTH_FAILED : BTA_HH_ERR;
793 conn_dat.bda = p_cb->addr;
794 HID_HostCloseDev(p_cb->hid_handle);
796 /* Report OPEN fail event */
797 (*bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH*)&conn_dat);
799 #if (BTA_HH_DEBUG == TRUE)
800 bta_hh_trace_dev_db();
802 /* clean up control block, but retain SDP info and device handle */
806 /* if no connection is active and HH disable is signaled, disable service */
807 if (bta_hh_cb.cnt_num == 0 && bta_hh_cb.w4_disable) {
811 /* Error in opening hid connection, reset flags */
812 p_cb->incoming_conn = false;
813 p_cb->incoming_hid_handle = BTA_HH_INVALID_HANDLE;
816 /*******************************************************************************
818 * Function bta_hh_close_act
820 * Description HID Host process a close event
825 ******************************************************************************/
826 void bta_hh_close_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
827 tBTA_HH_CONN conn_dat;
828 tBTA_HH_CBDATA disc_dat = {BTA_HH_OK, 0};
829 uint32_t reason = p_data->hid_cback.data; /* Reason for closing (32-bit) */
831 /* if HID_HDEV_EVT_VC_UNPLUG was received, report BTA_HH_VC_UNPLUG_EVT */
832 uint16_t event = p_cb->vp ? BTA_HH_VC_UNPLUG_EVT : BTA_HH_CLOSE_EVT;
834 disc_dat.handle = p_cb->hid_handle;
835 disc_dat.status = p_data->hid_cback.data;
837 /* Check reason for closing */
838 if ((reason & (HID_L2CAP_CONN_FAIL |
839 HID_L2CAP_REQ_FAIL)) || /* Failure to initialize connection
840 (page timeout or l2cap error) */
842 HID_ERR_AUTH_FAILED) || /* Authenication error (while initiating) */
843 (reason == HID_ERR_L2CAP_FAILED)) /* Failure creating l2cap connection */
845 /* Failure in opening connection */
846 conn_dat.handle = p_cb->hid_handle;
848 (reason == HID_ERR_AUTH_FAILED) ? BTA_HH_ERR_AUTH_FAILED : BTA_HH_ERR;
849 conn_dat.bda = p_cb->addr;
850 HID_HostCloseDev(p_cb->hid_handle);
852 /* Report OPEN fail event */
853 (*bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH*)&conn_dat);
855 #if (BTA_HH_DEBUG == TRUE)
856 bta_hh_trace_dev_db();
860 /* otherwise report CLOSE/VC_UNPLUG event */
862 /* finaliza device driver */
863 bta_hh_co_close(p_cb->hid_handle, p_cb->app_id);
864 /* inform role manager */
865 bta_sys_conn_close(BTA_ID_HH, p_cb->app_id, p_cb->addr);
866 /* update total conn number */
869 if (disc_dat.status) disc_dat.status = BTA_HH_ERR;
871 (*bta_hh_cb.p_cback)(event, (tBTA_HH*)&disc_dat);
873 /* if virtually unplug, remove device */
875 HID_HostRemoveDev(p_cb->hid_handle);
876 bta_hh_clean_up_kdev(p_cb);
879 #if (BTA_HH_DEBUG == TRUE)
880 bta_hh_trace_dev_db();
884 /* clean up control block, but retain SDP info and device handle */
888 /* if no connection is active and HH disable is signaled, disable service */
889 if (bta_hh_cb.cnt_num == 0 && bta_hh_cb.w4_disable) {
896 /*******************************************************************************
898 * Function bta_hh_get_dscp_act
900 * Description Get device report descriptor
905 ******************************************************************************/
906 void bta_hh_get_dscp_act(tBTA_HH_DEV_CB* p_cb,
907 UNUSED_ATTR tBTA_HH_DATA* p_data) {
908 #if (BTA_HH_LE_INCLUDED == TRUE)
909 if (p_cb->is_le_device) {
910 bta_hh_le_get_dscp_act(p_cb);
913 (*bta_hh_cb.p_cback)(BTA_HH_GET_DSCP_EVT, (tBTA_HH*)&p_cb->dscp_info);
916 /*******************************************************************************
918 * Function bta_hh_maint_dev_act
920 * Description HID Host maintain device list.
925 ******************************************************************************/
926 void bta_hh_maint_dev_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
927 tBTA_HH_MAINT_DEV* p_dev_info = &p_data->api_maintdev;
928 tBTA_HH_DEV_INFO dev_info;
931 dev_info.status = BTA_HH_ERR;
932 dev_info.handle = BTA_HH_INVALID_HANDLE;
934 switch (p_dev_info->sub_event) {
935 case BTA_HH_ADD_DEV_EVT: /* add a device */
936 dev_info.bda = p_dev_info->bda;
937 /* initialize callback data */
938 if (p_cb->hid_handle == BTA_HH_INVALID_HANDLE) {
939 #if (BTA_HH_LE_INCLUDED == TRUE)
940 if (bta_hh_is_le_device(p_cb, p_data->api_conn.bd_addr)) {
941 dev_info.handle = bta_hh_le_add_device(p_cb, p_dev_info);
942 if (dev_info.handle != BTA_HH_INVALID_HANDLE)
943 dev_info.status = BTA_HH_OK;
947 if (HID_HostAddDev(p_dev_info->bda, p_dev_info->attr_mask,
948 &dev_handle) == HID_SUCCESS) {
949 dev_info.handle = dev_handle;
950 dev_info.status = BTA_HH_OK;
952 #if (BTA_HH_LE_INCLUDED == TRUE)
953 /* update DI information */
954 bta_hh_update_di_info(p_cb, p_dev_info->dscp_info.vendor_id,
955 p_dev_info->dscp_info.product_id,
956 p_dev_info->dscp_info.version,
957 p_dev_info->dscp_info.flag);
959 bta_hh_update_di_info(p_cb, p_dev_info->dscp_info.vendor_id,
960 p_dev_info->dscp_info.product_id,
961 p_dev_info->dscp_info.version, 0);
964 /* add to BTA device list */
965 bta_hh_add_device_to_list(
966 p_cb, dev_handle, p_dev_info->attr_mask,
967 &p_dev_info->dscp_info.descriptor, p_dev_info->sub_class,
968 p_dev_info->dscp_info.ssr_max_latency,
969 p_dev_info->dscp_info.ssr_min_tout, p_dev_info->app_id);
970 /* update cb_index[] map */
971 bta_hh_cb.cb_index[dev_handle] = p_cb->index;
973 } else /* device already been added */
975 dev_info.handle = p_cb->hid_handle;
976 dev_info.status = BTA_HH_OK;
978 #if (BTA_HH_DEBUG == TRUE)
979 bta_hh_trace_dev_db();
983 case BTA_HH_RMV_DEV_EVT: /* remove device */
984 dev_info.handle = (uint8_t)p_dev_info->hdr.layer_specific;
985 dev_info.bda = p_cb->addr;
987 #if (BTA_HH_LE_INCLUDED == TRUE)
988 if (p_cb->is_le_device) {
989 bta_hh_le_remove_dev_bg_conn(p_cb);
990 bta_hh_sm_execute(p_cb, BTA_HH_API_CLOSE_EVT, NULL);
991 bta_hh_clean_up_kdev(p_cb);
995 if (HID_HostRemoveDev(dev_info.handle) == HID_SUCCESS) {
996 dev_info.status = BTA_HH_OK;
998 /* remove from known device list in BTA */
999 bta_hh_clean_up_kdev(p_cb);
1005 APPL_TRACE_DEBUG("invalid command");
1009 (*bta_hh_cb.p_cback)(p_dev_info->sub_event, (tBTA_HH*)&dev_info);
1011 /*******************************************************************************
1013 * Function bta_hh_write_dev_act
1015 * Description Write device action. can be SET/GET/DATA transaction.
1019 ******************************************************************************/
1020 void bta_hh_write_dev_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
1021 tBTA_HH_CBDATA cbdata = {BTA_HH_OK, 0};
1022 uint16_t event = (p_data->api_sndcmd.t_type - BTA_HH_FST_BTE_TRANS_EVT) +
1023 BTA_HH_FST_TRANS_CB_EVT;
1025 #if (BTA_HH_LE_INCLUDED == TRUE)
1026 if (p_cb->is_le_device)
1027 bta_hh_le_write_dev_act(p_cb, p_data);
1032 cbdata.handle = p_cb->hid_handle;
1034 /* match up BTE/BTA report/boot mode def */
1035 if (p_data->api_sndcmd.t_type == HID_TRANS_SET_PROTOCOL) {
1036 p_data->api_sndcmd.param =
1037 (p_data->api_sndcmd.param == BTA_HH_PROTO_RPT_MODE)
1038 ? HID_PAR_PROTOCOL_REPORT
1039 : HID_PAR_PROTOCOL_BOOT_MODE;
1042 if (HID_HostWriteDev(p_cb->hid_handle, p_data->api_sndcmd.t_type,
1043 p_data->api_sndcmd.param, p_data->api_sndcmd.data,
1044 p_data->api_sndcmd.rpt_id,
1045 p_data->api_sndcmd.p_data) != HID_SUCCESS) {
1046 APPL_TRACE_ERROR("HID_HostWriteDev Error ");
1047 cbdata.status = BTA_HH_ERR;
1049 if (p_data->api_sndcmd.t_type != HID_TRANS_CONTROL &&
1050 p_data->api_sndcmd.t_type != HID_TRANS_DATA)
1051 (*bta_hh_cb.p_cback)(event, (tBTA_HH*)&cbdata);
1052 else if (p_data->api_sndcmd.param == BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG)
1053 (*bta_hh_cb.p_cback)(BTA_HH_VC_UNPLUG_EVT, (tBTA_HH*)&cbdata);
1055 switch (p_data->api_sndcmd.t_type) {
1056 case HID_TRANS_SET_PROTOCOL:
1058 case HID_TRANS_GET_REPORT:
1060 case HID_TRANS_SET_REPORT:
1062 case HID_TRANS_GET_PROTOCOL:
1064 case HID_TRANS_GET_IDLE:
1066 case HID_TRANS_SET_IDLE: /* set w4_handsk event name for callback
1068 p_cb->w4_evt = event;
1070 case HID_TRANS_DATA: /* output report */
1072 case HID_TRANS_CONTROL:
1073 /* no handshake event will be generated */
1074 /* if VC_UNPLUG is issued, set flag */
1075 if (p_data->api_sndcmd.param == BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG)
1079 /* currently not expected */
1080 case HID_TRANS_DATAC:
1082 APPL_TRACE_DEBUG("%s: cmd type = %d", __func__,
1083 p_data->api_sndcmd.t_type);
1087 /* if not control type transaction, notify PM for energy control */
1088 if (p_data->api_sndcmd.t_type != HID_TRANS_CONTROL) {
1089 /* inform PM for mode change */
1090 bta_sys_busy(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1091 bta_sys_idle(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1092 } else if (p_data->api_sndcmd.param == BTA_HH_CTRL_SUSPEND) {
1093 bta_sys_sco_close(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1094 } else if (p_data->api_sndcmd.param == BTA_HH_CTRL_EXIT_SUSPEND) {
1095 bta_sys_busy(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1102 /*****************************************************************************
1104 ****************************************************************************/
1105 /*******************************************************************************
1107 * Function bta_hh_cback
1109 * Description BTA HH callback function.
1114 ******************************************************************************/
1115 static void bta_hh_cback(uint8_t dev_handle, const RawAddress& addr,
1116 uint8_t event, uint32_t data, BT_HDR* pdata) {
1117 uint16_t sm_event = BTA_HH_INVALID_EVT;
1120 #if (BTA_HH_DEBUG == TRUE)
1121 APPL_TRACE_DEBUG("%s::HID_event [%s]", __func__,
1122 bta_hh_hid_event_name(event));
1126 case HID_HDEV_EVT_OPEN:
1127 sm_event = BTA_HH_INT_OPEN_EVT;
1129 case HID_HDEV_EVT_CLOSE:
1130 sm_event = BTA_HH_INT_CLOSE_EVT;
1132 case HID_HDEV_EVT_INTR_DATA:
1133 sm_event = BTA_HH_INT_DATA_EVT;
1135 case HID_HDEV_EVT_HANDSHAKE:
1136 sm_event = BTA_HH_INT_HANDSK_EVT;
1138 case HID_HDEV_EVT_CTRL_DATA:
1139 sm_event = BTA_HH_INT_CTRL_DATA;
1141 case HID_HDEV_EVT_RETRYING:
1143 case HID_HDEV_EVT_INTR_DATC:
1144 case HID_HDEV_EVT_CTRL_DATC:
1145 /* Unhandled events: Free buffer for DATAC */
1146 osi_free_and_reset((void**)&pdata);
1148 case HID_HDEV_EVT_VC_UNPLUG:
1149 for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++) {
1150 if (bta_hh_cb.kdev[xx].hid_handle == dev_handle) {
1151 bta_hh_cb.kdev[xx].vp = true;
1158 if (sm_event != BTA_HH_INVALID_EVT) {
1159 tBTA_HH_CBACK_DATA* p_buf = (tBTA_HH_CBACK_DATA*)osi_malloc(
1160 sizeof(tBTA_HH_CBACK_DATA) + sizeof(BT_HDR));
1161 p_buf->hdr.event = sm_event;
1162 p_buf->hdr.layer_specific = (uint16_t)dev_handle;
1165 p_buf->p_data = pdata;
1167 bta_sys_sendmsg(p_buf);
1171 /*******************************************************************************
1173 * Function bta_hh_get_trans_status
1175 * Description translate a handshake result code into BTA HH
1178 ******************************************************************************/
1179 static tBTA_HH_STATUS bta_hh_get_trans_status(uint32_t result) {
1181 case HID_PAR_HANDSHAKE_RSP_SUCCESS: /* (0) */
1183 case HID_PAR_HANDSHAKE_RSP_NOT_READY: /* (1) */
1184 case HID_PAR_HANDSHAKE_RSP_ERR_INVALID_REP_ID: /* (2) */
1185 case HID_PAR_HANDSHAKE_RSP_ERR_UNSUPPORTED_REQ: /* (3) */
1186 case HID_PAR_HANDSHAKE_RSP_ERR_INVALID_PARAM: /* (4) */
1187 return (tBTA_HH_STATUS)result;
1188 case HID_PAR_HANDSHAKE_RSP_ERR_UNKNOWN: /* (14) */
1189 case HID_PAR_HANDSHAKE_RSP_ERR_FATAL: /* (15) */
1191 return BTA_HH_HS_ERROR;
1195 /*****************************************************************************
1197 ****************************************************************************/
1199 #if (BTA_HH_DEBUG == TRUE)
1200 static const char* bta_hh_get_w4_event(uint16_t event) {
1202 case BTA_HH_GET_RPT_EVT:
1203 return "BTA_HH_GET_RPT_EVT";
1204 case BTA_HH_SET_RPT_EVT:
1205 return "BTA_HH_SET_RPT_EVT";
1206 case BTA_HH_GET_PROTO_EVT:
1207 return "BTA_HH_GET_PROTO_EVT";
1208 case BTA_HH_SET_PROTO_EVT:
1209 return "BTA_HH_SET_PROTO_EVT";
1210 case BTA_HH_GET_IDLE_EVT:
1211 return "BTA_HH_GET_IDLE_EVT";
1212 case BTA_HH_SET_IDLE_EVT:
1213 return "BTA_HH_SET_IDLE_EVT";
1214 case BTA_HH_OPEN_EVT:
1215 return "BTA_HH_OPEN_EVT";
1217 return "Unknown event";
1221 static const char* bta_hh_hid_event_name(uint16_t event) {
1223 case HID_HDEV_EVT_OPEN:
1224 return "HID_HDEV_EVT_OPEN";
1225 case HID_HDEV_EVT_CLOSE:
1226 return "HID_HDEV_EVT_CLOSE";
1227 case HID_HDEV_EVT_RETRYING:
1228 return "HID_HDEV_EVT_RETRYING";
1229 case HID_HDEV_EVT_INTR_DATA:
1230 return "HID_HDEV_EVT_INTR_DATA";
1231 case HID_HDEV_EVT_INTR_DATC:
1232 return "HID_HDEV_EVT_INTR_DATC";
1233 case HID_HDEV_EVT_CTRL_DATA:
1234 return "HID_HDEV_EVT_CTRL_DATA";
1235 case HID_HDEV_EVT_CTRL_DATC:
1236 return "HID_HDEV_EVT_CTRL_DATC";
1237 case HID_HDEV_EVT_HANDSHAKE:
1238 return "HID_HDEV_EVT_HANDSHAKE";
1239 case HID_HDEV_EVT_VC_UNPLUG:
1240 return "HID_HDEV_EVT_VC_UNPLUG";
1242 return "Unknown HID event";
1246 #endif /* BTA_HH_INCLUDED */