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 acknowledgement.
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;
655 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
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;
663 if (p_cb->w4_evt == BTA_HH_GET_RPT_EVT)
664 bta_hh_co_get_rpt_rsp(bta_hh.dev_status.handle, bta_hh.hs_data.status,
666 (*bta_hh_cb.p_cback)(p_cb->w4_evt, &bta_hh);
670 /* acknoledgement from HID device for SET_ transaction */
671 case BTA_HH_SET_RPT_EVT:
672 case BTA_HH_SET_PROTO_EVT:
673 case BTA_HH_SET_IDLE_EVT:
674 bta_hh.dev_status.handle = p_cb->hid_handle;
675 bta_hh.dev_status.status =
676 bta_hh_get_trans_status(p_data->hid_cback.data);
677 if (p_cb->w4_evt == BTA_HH_SET_RPT_EVT)
678 bta_hh_co_set_rpt_rsp(bta_hh.dev_status.handle,
679 bta_hh.dev_status.status);
680 (*bta_hh_cb.p_cback)(p_cb->w4_evt, &bta_hh);
684 /* SET_PROTOCOL when open connection */
685 case BTA_HH_OPEN_EVT:
687 p_data->hid_cback.data ? BTA_HH_ERR_PROTO : BTA_HH_OK;
688 bta_hh.conn.handle = p_cb->hid_handle;
689 bta_hh.conn.bda = p_cb->addr;
690 (*bta_hh_cb.p_cback)(p_cb->w4_evt, &bta_hh);
691 #if (BTA_HH_DEBUG == TRUE)
692 bta_hh_trace_dev_db();
698 /* unknow transaction handshake response */
699 APPL_TRACE_DEBUG("unknown transaction type");
703 /* transaction achknoledgement received, inform PM for mode change */
704 bta_sys_idle(BTA_ID_HH, p_cb->app_id, p_cb->addr);
707 /*******************************************************************************
709 * Function bta_hh_ctrl_dat_act
711 * Description HID Host process a data report from control channel.
716 ******************************************************************************/
717 void bta_hh_ctrl_dat_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
718 BT_HDR* pdata = p_data->hid_cback.p_data;
719 uint8_t* data = (uint8_t*)(pdata + 1) + pdata->offset;
720 tBTA_HH_HSDATA hs_data;
722 #if (BTA_HH_DEBUG == TRUE)
723 APPL_TRACE_DEBUG("Ctrl DATA received w4: event[%s]",
724 bta_hh_get_w4_event(p_cb->w4_evt));
726 if (pdata->len == 0) {
727 android_errorWriteLog(0x534e4554, "116108738");
729 osi_free_and_reset((void**)&pdata);
732 hs_data.status = BTA_HH_OK;
733 hs_data.handle = p_cb->hid_handle;
735 switch (p_cb->w4_evt) {
736 case BTA_HH_GET_IDLE_EVT:
737 hs_data.rsp_data.idle_rate = *data;
739 case BTA_HH_GET_RPT_EVT:
740 hs_data.rsp_data.p_rpt_data = pdata;
741 bta_hh_co_get_rpt_rsp(hs_data.handle, hs_data.status, pdata->data,
744 case BTA_HH_GET_PROTO_EVT:
745 /* match up BTE/BTA report/boot mode def*/
746 hs_data.rsp_data.proto_mode = ((*data) == HID_PAR_PROTOCOL_REPORT)
747 ? BTA_HH_PROTO_RPT_MODE
748 : BTA_HH_PROTO_BOOT_MODE;
749 #if (BTA_HH_DEBUG == TRUE)
750 APPL_TRACE_DEBUG("GET_PROTOCOL Mode = [%s]",
751 (hs_data.rsp_data.proto_mode == BTA_HH_PROTO_RPT_MODE)
756 /* should not expect control DATA for SET_ transaction */
757 case BTA_HH_SET_PROTO_EVT:
758 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
759 case BTA_HH_SET_RPT_EVT:
760 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
761 case BTA_HH_SET_IDLE_EVT:
762 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
764 #if (BTA_HH_DEBUG == TRUE)
765 APPL_TRACE_DEBUG("invalid transaction type for DATA payload: 4_evt[%s]",
766 bta_hh_get_w4_event(p_cb->w4_evt));
771 /* inform PM for mode change */
772 bta_sys_busy(BTA_ID_HH, p_cb->app_id, p_cb->addr);
773 bta_sys_idle(BTA_ID_HH, p_cb->app_id, p_cb->addr);
775 (*bta_hh_cb.p_cback)(p_cb->w4_evt, (tBTA_HH*)&hs_data);
778 osi_free_and_reset((void**)&pdata);
781 /*******************************************************************************
783 * Function bta_hh_open_failure
785 * Description report HID open failure when at wait for connection state
786 * and receive device close event.
791 ******************************************************************************/
792 void bta_hh_open_failure(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
793 tBTA_HH_CONN conn_dat;
794 uint32_t reason = p_data->hid_cback.data; /* Reason for closing (32-bit) */
796 memset(&conn_dat, 0, sizeof(tBTA_HH_CONN));
797 conn_dat.handle = p_cb->hid_handle;
799 (reason == HID_ERR_AUTH_FAILED) ? BTA_HH_ERR_AUTH_FAILED : BTA_HH_ERR;
800 conn_dat.bda = p_cb->addr;
801 HID_HostCloseDev(p_cb->hid_handle);
803 /* Report OPEN fail event */
804 (*bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH*)&conn_dat);
806 #if (BTA_HH_DEBUG == TRUE)
807 bta_hh_trace_dev_db();
809 /* clean up control block, but retain SDP info and device handle */
813 /* if no connection is active and HH disable is signaled, disable service */
814 if (bta_hh_cb.cnt_num == 0 && bta_hh_cb.w4_disable) {
818 /* Error in opening hid connection, reset flags */
819 p_cb->incoming_conn = false;
820 p_cb->incoming_hid_handle = BTA_HH_INVALID_HANDLE;
823 /*******************************************************************************
825 * Function bta_hh_close_act
827 * Description HID Host process a close event
832 ******************************************************************************/
833 void bta_hh_close_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
834 tBTA_HH_CONN conn_dat;
835 tBTA_HH_CBDATA disc_dat = {BTA_HH_OK, 0};
836 uint32_t reason = p_data->hid_cback.data; /* Reason for closing (32-bit) */
838 /* if HID_HDEV_EVT_VC_UNPLUG was received, report BTA_HH_VC_UNPLUG_EVT */
839 uint16_t event = p_cb->vp ? BTA_HH_VC_UNPLUG_EVT : BTA_HH_CLOSE_EVT;
841 disc_dat.handle = p_cb->hid_handle;
842 disc_dat.status = p_data->hid_cback.data;
844 /* Check reason for closing */
845 if ((reason & (HID_L2CAP_CONN_FAIL |
846 HID_L2CAP_REQ_FAIL)) || /* Failure to initialize connection
847 (page timeout or l2cap error) */
849 HID_ERR_AUTH_FAILED) || /* Authenication error (while initiating) */
850 (reason == HID_ERR_L2CAP_FAILED)) /* Failure creating l2cap connection */
852 /* Failure in opening connection */
853 conn_dat.handle = p_cb->hid_handle;
855 (reason == HID_ERR_AUTH_FAILED) ? BTA_HH_ERR_AUTH_FAILED : BTA_HH_ERR;
856 conn_dat.bda = p_cb->addr;
857 HID_HostCloseDev(p_cb->hid_handle);
859 /* Report OPEN fail event */
860 (*bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH*)&conn_dat);
862 #if (BTA_HH_DEBUG == TRUE)
863 bta_hh_trace_dev_db();
867 /* otherwise report CLOSE/VC_UNPLUG event */
869 /* finaliza device driver */
870 bta_hh_co_close(p_cb->hid_handle, p_cb->app_id);
871 /* inform role manager */
872 bta_sys_conn_close(BTA_ID_HH, p_cb->app_id, p_cb->addr);
873 /* update total conn number */
876 if (disc_dat.status) disc_dat.status = BTA_HH_ERR;
878 (*bta_hh_cb.p_cback)(event, (tBTA_HH*)&disc_dat);
880 /* if virtually unplug, remove device */
882 HID_HostRemoveDev(p_cb->hid_handle);
883 bta_hh_clean_up_kdev(p_cb);
886 #if (BTA_HH_DEBUG == TRUE)
887 bta_hh_trace_dev_db();
891 /* clean up control block, but retain SDP info and device handle */
895 /* if no connection is active and HH disable is signaled, disable service */
896 if (bta_hh_cb.cnt_num == 0 && bta_hh_cb.w4_disable) {
903 /*******************************************************************************
905 * Function bta_hh_get_dscp_act
907 * Description Get device report descriptor
912 ******************************************************************************/
913 void bta_hh_get_dscp_act(tBTA_HH_DEV_CB* p_cb,
914 UNUSED_ATTR tBTA_HH_DATA* p_data) {
915 #if (BTA_HH_LE_INCLUDED == TRUE)
916 if (p_cb->is_le_device) {
917 bta_hh_le_get_dscp_act(p_cb);
920 (*bta_hh_cb.p_cback)(BTA_HH_GET_DSCP_EVT, (tBTA_HH*)&p_cb->dscp_info);
923 /*******************************************************************************
925 * Function bta_hh_maint_dev_act
927 * Description HID Host maintain device list.
932 ******************************************************************************/
933 void bta_hh_maint_dev_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
934 tBTA_HH_MAINT_DEV* p_dev_info = &p_data->api_maintdev;
935 tBTA_HH_DEV_INFO dev_info;
938 dev_info.status = BTA_HH_ERR;
939 dev_info.handle = BTA_HH_INVALID_HANDLE;
941 switch (p_dev_info->sub_event) {
942 case BTA_HH_ADD_DEV_EVT: /* add a device */
943 dev_info.bda = p_dev_info->bda;
944 /* initialize callback data */
945 if (p_cb->hid_handle == BTA_HH_INVALID_HANDLE) {
946 #if (BTA_HH_LE_INCLUDED == TRUE)
947 if (bta_hh_is_le_device(p_cb, p_data->api_conn.bd_addr)) {
948 dev_info.handle = bta_hh_le_add_device(p_cb, p_dev_info);
949 if (dev_info.handle != BTA_HH_INVALID_HANDLE)
950 dev_info.status = BTA_HH_OK;
954 if (HID_HostAddDev(p_dev_info->bda, p_dev_info->attr_mask,
955 &dev_handle) == HID_SUCCESS) {
956 dev_info.handle = dev_handle;
957 dev_info.status = BTA_HH_OK;
959 #if (BTA_HH_LE_INCLUDED == TRUE)
960 /* update DI information */
961 bta_hh_update_di_info(p_cb, p_dev_info->dscp_info.vendor_id,
962 p_dev_info->dscp_info.product_id,
963 p_dev_info->dscp_info.version,
964 p_dev_info->dscp_info.flag);
966 bta_hh_update_di_info(p_cb, p_dev_info->dscp_info.vendor_id,
967 p_dev_info->dscp_info.product_id,
968 p_dev_info->dscp_info.version, 0);
971 /* add to BTA device list */
972 bta_hh_add_device_to_list(
973 p_cb, dev_handle, p_dev_info->attr_mask,
974 &p_dev_info->dscp_info.descriptor, p_dev_info->sub_class,
975 p_dev_info->dscp_info.ssr_max_latency,
976 p_dev_info->dscp_info.ssr_min_tout, p_dev_info->app_id);
977 /* update cb_index[] map */
978 bta_hh_cb.cb_index[dev_handle] = p_cb->index;
980 } else /* device already been added */
982 dev_info.handle = p_cb->hid_handle;
983 dev_info.status = BTA_HH_OK;
985 #if (BTA_HH_DEBUG == TRUE)
986 bta_hh_trace_dev_db();
990 case BTA_HH_RMV_DEV_EVT: /* remove device */
991 dev_info.handle = (uint8_t)p_dev_info->hdr.layer_specific;
992 dev_info.bda = p_cb->addr;
994 #if (BTA_HH_LE_INCLUDED == TRUE)
995 if (p_cb->is_le_device) {
996 bta_hh_le_remove_dev_bg_conn(p_cb);
997 bta_hh_sm_execute(p_cb, BTA_HH_API_CLOSE_EVT, NULL);
998 bta_hh_clean_up_kdev(p_cb);
1002 if (HID_HostRemoveDev(dev_info.handle) == HID_SUCCESS) {
1003 dev_info.status = BTA_HH_OK;
1005 /* remove from known device list in BTA */
1006 bta_hh_clean_up_kdev(p_cb);
1012 APPL_TRACE_DEBUG("invalid command");
1016 (*bta_hh_cb.p_cback)(p_dev_info->sub_event, (tBTA_HH*)&dev_info);
1018 /*******************************************************************************
1020 * Function bta_hh_write_dev_act
1022 * Description Write device action. can be SET/GET/DATA transaction.
1026 ******************************************************************************/
1027 void bta_hh_write_dev_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
1028 tBTA_HH_CBDATA cbdata = {BTA_HH_OK, 0};
1029 uint16_t event = (p_data->api_sndcmd.t_type - BTA_HH_FST_BTE_TRANS_EVT) +
1030 BTA_HH_FST_TRANS_CB_EVT;
1032 #if (BTA_HH_LE_INCLUDED == TRUE)
1033 if (p_cb->is_le_device)
1034 bta_hh_le_write_dev_act(p_cb, p_data);
1039 cbdata.handle = p_cb->hid_handle;
1041 /* match up BTE/BTA report/boot mode def */
1042 if (p_data->api_sndcmd.t_type == HID_TRANS_SET_PROTOCOL) {
1043 p_data->api_sndcmd.param =
1044 (p_data->api_sndcmd.param == BTA_HH_PROTO_RPT_MODE)
1045 ? HID_PAR_PROTOCOL_REPORT
1046 : HID_PAR_PROTOCOL_BOOT_MODE;
1049 if (HID_HostWriteDev(p_cb->hid_handle, p_data->api_sndcmd.t_type,
1050 p_data->api_sndcmd.param, p_data->api_sndcmd.data,
1051 p_data->api_sndcmd.rpt_id,
1052 p_data->api_sndcmd.p_data) != HID_SUCCESS) {
1053 APPL_TRACE_ERROR("HID_HostWriteDev Error ");
1054 cbdata.status = BTA_HH_ERR;
1056 if (p_data->api_sndcmd.t_type != HID_TRANS_CONTROL &&
1057 p_data->api_sndcmd.t_type != HID_TRANS_DATA)
1058 (*bta_hh_cb.p_cback)(event, (tBTA_HH*)&cbdata);
1059 else if (p_data->api_sndcmd.param == BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG)
1060 (*bta_hh_cb.p_cback)(BTA_HH_VC_UNPLUG_EVT, (tBTA_HH*)&cbdata);
1062 switch (p_data->api_sndcmd.t_type) {
1063 case HID_TRANS_SET_PROTOCOL:
1064 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
1065 case HID_TRANS_GET_REPORT:
1066 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
1067 case HID_TRANS_SET_REPORT:
1068 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
1069 case HID_TRANS_GET_PROTOCOL:
1070 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
1071 case HID_TRANS_GET_IDLE:
1072 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
1073 case HID_TRANS_SET_IDLE: /* set w4_handsk event name for callback
1075 p_cb->w4_evt = event;
1077 case HID_TRANS_DATA: /* output report */
1078 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
1079 case HID_TRANS_CONTROL:
1080 /* no handshake event will be generated */
1081 /* if VC_UNPLUG is issued, set flag */
1082 if (p_data->api_sndcmd.param == BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG)
1086 /* currently not expected */
1087 case HID_TRANS_DATAC:
1089 APPL_TRACE_DEBUG("%s: cmd type = %d", __func__,
1090 p_data->api_sndcmd.t_type);
1094 /* if not control type transaction, notify PM for energy control */
1095 if (p_data->api_sndcmd.t_type != HID_TRANS_CONTROL) {
1096 /* inform PM for mode change */
1097 bta_sys_busy(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1098 bta_sys_idle(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1099 } else if (p_data->api_sndcmd.param == BTA_HH_CTRL_SUSPEND) {
1100 bta_sys_sco_close(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1101 } else if (p_data->api_sndcmd.param == BTA_HH_CTRL_EXIT_SUSPEND) {
1102 bta_sys_busy(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1109 /*****************************************************************************
1111 ****************************************************************************/
1112 /*******************************************************************************
1114 * Function bta_hh_cback
1116 * Description BTA HH callback function.
1121 ******************************************************************************/
1122 static void bta_hh_cback(uint8_t dev_handle, const RawAddress& addr,
1123 uint8_t event, uint32_t data, BT_HDR* pdata) {
1124 uint16_t sm_event = BTA_HH_INVALID_EVT;
1127 #if (BTA_HH_DEBUG == TRUE)
1128 APPL_TRACE_DEBUG("%s::HID_event [%s]", __func__,
1129 bta_hh_hid_event_name(event));
1133 case HID_HDEV_EVT_OPEN:
1134 sm_event = BTA_HH_INT_OPEN_EVT;
1136 case HID_HDEV_EVT_CLOSE:
1137 sm_event = BTA_HH_INT_CLOSE_EVT;
1139 case HID_HDEV_EVT_INTR_DATA:
1140 sm_event = BTA_HH_INT_DATA_EVT;
1142 case HID_HDEV_EVT_HANDSHAKE:
1143 sm_event = BTA_HH_INT_HANDSK_EVT;
1145 case HID_HDEV_EVT_CTRL_DATA:
1146 sm_event = BTA_HH_INT_CTRL_DATA;
1148 case HID_HDEV_EVT_RETRYING:
1150 case HID_HDEV_EVT_INTR_DATC:
1151 case HID_HDEV_EVT_CTRL_DATC:
1152 /* Unhandled events: Free buffer for DATAC */
1153 osi_free_and_reset((void**)&pdata);
1155 case HID_HDEV_EVT_VC_UNPLUG:
1156 for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++) {
1157 if (bta_hh_cb.kdev[xx].hid_handle == dev_handle) {
1158 bta_hh_cb.kdev[xx].vp = true;
1165 if (sm_event != BTA_HH_INVALID_EVT) {
1166 tBTA_HH_CBACK_DATA* p_buf = (tBTA_HH_CBACK_DATA*)osi_malloc(
1167 sizeof(tBTA_HH_CBACK_DATA) + sizeof(BT_HDR));
1168 p_buf->hdr.event = sm_event;
1169 p_buf->hdr.layer_specific = (uint16_t)dev_handle;
1172 p_buf->p_data = pdata;
1174 bta_sys_sendmsg(p_buf);
1178 /*******************************************************************************
1180 * Function bta_hh_get_trans_status
1182 * Description translate a handshake result code into BTA HH
1185 ******************************************************************************/
1186 static tBTA_HH_STATUS bta_hh_get_trans_status(uint32_t result) {
1188 case HID_PAR_HANDSHAKE_RSP_SUCCESS: /* (0) */
1190 case HID_PAR_HANDSHAKE_RSP_NOT_READY: /* (1) */
1191 case HID_PAR_HANDSHAKE_RSP_ERR_INVALID_REP_ID: /* (2) */
1192 case HID_PAR_HANDSHAKE_RSP_ERR_UNSUPPORTED_REQ: /* (3) */
1193 case HID_PAR_HANDSHAKE_RSP_ERR_INVALID_PARAM: /* (4) */
1194 return (tBTA_HH_STATUS)result;
1195 case HID_PAR_HANDSHAKE_RSP_ERR_UNKNOWN: /* (14) */
1196 case HID_PAR_HANDSHAKE_RSP_ERR_FATAL: /* (15) */
1198 return BTA_HH_HS_ERROR;
1202 /*****************************************************************************
1204 ****************************************************************************/
1206 #if (BTA_HH_DEBUG == TRUE)
1207 static const char* bta_hh_get_w4_event(uint16_t event) {
1209 case BTA_HH_GET_RPT_EVT:
1210 return "BTA_HH_GET_RPT_EVT";
1211 case BTA_HH_SET_RPT_EVT:
1212 return "BTA_HH_SET_RPT_EVT";
1213 case BTA_HH_GET_PROTO_EVT:
1214 return "BTA_HH_GET_PROTO_EVT";
1215 case BTA_HH_SET_PROTO_EVT:
1216 return "BTA_HH_SET_PROTO_EVT";
1217 case BTA_HH_GET_IDLE_EVT:
1218 return "BTA_HH_GET_IDLE_EVT";
1219 case BTA_HH_SET_IDLE_EVT:
1220 return "BTA_HH_SET_IDLE_EVT";
1221 case BTA_HH_OPEN_EVT:
1222 return "BTA_HH_OPEN_EVT";
1224 return "Unknown event";
1228 static const char* bta_hh_hid_event_name(uint16_t event) {
1230 case HID_HDEV_EVT_OPEN:
1231 return "HID_HDEV_EVT_OPEN";
1232 case HID_HDEV_EVT_CLOSE:
1233 return "HID_HDEV_EVT_CLOSE";
1234 case HID_HDEV_EVT_RETRYING:
1235 return "HID_HDEV_EVT_RETRYING";
1236 case HID_HDEV_EVT_INTR_DATA:
1237 return "HID_HDEV_EVT_INTR_DATA";
1238 case HID_HDEV_EVT_INTR_DATC:
1239 return "HID_HDEV_EVT_INTR_DATC";
1240 case HID_HDEV_EVT_CTRL_DATA:
1241 return "HID_HDEV_EVT_CTRL_DATA";
1242 case HID_HDEV_EVT_CTRL_DATC:
1243 return "HID_HDEV_EVT_CTRL_DATC";
1244 case HID_HDEV_EVT_HANDSHAKE:
1245 return "HID_HDEV_EVT_HANDSHAKE";
1246 case HID_HDEV_EVT_VC_UNPLUG:
1247 return "HID_HDEV_EVT_VC_UNPLUG";
1249 return "Unknown HID event";
1253 #endif /* BTA_HH_INCLUDED */