1 /******************************************************************************
3 * Copyright (C) 2002-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 connection interface functions
23 ******************************************************************************/
30 #include "bt_common.h"
47 #include "osi/include/osi.h"
50 extern fixed_queue_t *btu_general_alarm_queue;
52 static UINT8 find_conn_by_cid (UINT16 cid);
53 static void hidh_conn_retry (UINT8 dhandle);
55 /********************************************************************************/
56 /* L O C A L F U N C T I O N P R O T O T Y P E S */
57 /********************************************************************************/
58 static void hidh_l2cif_connect_ind (BD_ADDR bd_addr, UINT16 l2cap_cid,
59 UINT16 psm, UINT8 l2cap_id);
60 static void hidh_l2cif_connect_cfm (UINT16 l2cap_cid, UINT16 result);
61 static void hidh_l2cif_config_ind (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg);
62 static void hidh_l2cif_config_cfm (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg);
63 static void hidh_l2cif_disconnect_ind (UINT16 l2cap_cid, BOOLEAN ack_needed);
64 static void hidh_l2cif_data_ind (UINT16 l2cap_cid, BT_HDR *p_msg);
65 static void hidh_l2cif_disconnect_cfm (UINT16 l2cap_cid, UINT16 result);
66 static void hidh_l2cif_cong_ind (UINT16 l2cap_cid, BOOLEAN congested);
68 static const tL2CAP_APPL_INFO hst_reg_info =
70 hidh_l2cif_connect_ind,
71 hidh_l2cif_connect_cfm,
73 hidh_l2cif_config_ind,
74 hidh_l2cif_config_cfm,
75 hidh_l2cif_disconnect_ind,
76 hidh_l2cif_disconnect_cfm,
80 NULL /* tL2CA_TX_COMPLETE_CB */
83 /*******************************************************************************
85 ** Function hidh_l2cif_reg
87 ** Description This function initializes the SDP unit.
91 *******************************************************************************/
92 tHID_STATUS hidh_conn_reg (void)
96 /* Initialize the L2CAP configuration. We only care about MTU and flush */
97 memset(&hh_cb.l2cap_cfg, 0, sizeof(tL2CAP_CFG_INFO));
99 hh_cb.l2cap_cfg.mtu_present = TRUE;
100 hh_cb.l2cap_cfg.mtu = HID_HOST_MTU;
101 hh_cb.l2cap_cfg.flush_to_present = TRUE;
102 hh_cb.l2cap_cfg.flush_to = HID_HOST_FLUSH_TO;
104 /* Now, register with L2CAP */
105 if (!L2CA_Register (HID_PSM_CONTROL, (tL2CAP_APPL_INFO *) &hst_reg_info))
107 HIDH_TRACE_ERROR ("HID-Host Control Registration failed");
108 return (HID_ERR_L2CAP_FAILED) ;
110 if (!L2CA_Register (HID_PSM_INTERRUPT, (tL2CAP_APPL_INFO *) &hst_reg_info))
112 L2CA_Deregister( HID_PSM_CONTROL ) ;
113 HIDH_TRACE_ERROR ("HID-Host Interrupt Registration failed");
114 return (HID_ERR_L2CAP_FAILED) ;
117 for (xx = 0; xx < HID_HOST_MAX_DEVICES; xx++)
119 hh_cb.devices[xx].in_use = FALSE ;
120 hh_cb.devices[xx].conn.conn_state = HID_CONN_STATE_UNUSED;
123 return (HID_SUCCESS);
126 /*******************************************************************************
128 ** Function hidh_conn_disconnect
130 ** Description This function disconnects a connection.
132 ** Returns TRUE if disconnect started, FALSE if already disconnected
134 *******************************************************************************/
135 tHID_STATUS hidh_conn_disconnect (UINT8 dhandle)
137 tHID_CONN *p_hcon = &hh_cb.devices[dhandle].conn;
139 HIDH_TRACE_EVENT ("HID-Host disconnect");
141 if ((p_hcon->ctrl_cid != 0) || (p_hcon->intr_cid != 0))
143 p_hcon->conn_state = HID_CONN_STATE_DISCONNECTING;
145 /* Set l2cap idle timeout to 0 (so ACL link is disconnected
146 * immediately after last channel is closed) */
147 L2CA_SetIdleTimeoutByBdAddr(hh_cb.devices[dhandle].addr, 0, BT_TRANSPORT_BR_EDR);
148 /* Disconnect both interrupt and control channels */
149 if (p_hcon->intr_cid)
150 L2CA_DisconnectReq (p_hcon->intr_cid);
151 else if (p_hcon->ctrl_cid)
152 L2CA_DisconnectReq (p_hcon->ctrl_cid);
156 p_hcon->conn_state = HID_CONN_STATE_UNUSED;
159 return (HID_SUCCESS);
162 /*******************************************************************************
164 ** Function hidh_sec_check_complete_term
166 ** Description HID security check complete callback function.
168 ** Returns Send L2CA_ConnectRsp OK if secutiry check succeed; otherwise
169 ** send security block L2C connection response.
171 *******************************************************************************/
172 void hidh_sec_check_complete_term (BD_ADDR bd_addr, tBT_TRANSPORT transport, void *p_ref_data, UINT8 res)
174 tHID_HOST_DEV_CTB *p_dev= (tHID_HOST_DEV_CTB *) p_ref_data;
178 if( res == BTM_SUCCESS && p_dev->conn.conn_state == HID_CONN_STATE_SECURITY )
180 p_dev->conn.disc_reason = HID_SUCCESS; /* Authentication passed. Reset disc_reason (from HID_ERR_AUTH_FAILED) */
182 p_dev->conn.conn_state = HID_CONN_STATE_CONNECTING_INTR;
184 /* Send response to the L2CAP layer. */
185 L2CA_ConnectRsp (p_dev->addr, p_dev->conn.ctrl_id, p_dev->conn.ctrl_cid, L2CAP_CONN_OK, L2CAP_CONN_OK);
187 /* Send a Configuration Request. */
188 L2CA_ConfigReq (p_dev->conn.ctrl_cid, &hh_cb.l2cap_cfg);
191 /* security check fail */
192 else if (res != BTM_SUCCESS)
194 p_dev->conn.disc_reason = HID_ERR_AUTH_FAILED; /* Save reason for disconnecting */
195 p_dev->conn.conn_state = HID_CONN_STATE_UNUSED;
196 L2CA_ConnectRsp (p_dev->addr, p_dev->conn.ctrl_id, p_dev->conn.ctrl_cid, L2CAP_CONN_SECURITY_BLOCK, L2CAP_CONN_OK);
200 /*******************************************************************************
202 ** Function hidh_l2cif_connect_ind
204 ** Description This function handles an inbound connection indication
205 ** from L2CAP. This is the case where we are acting as a
210 *******************************************************************************/
211 static void hidh_l2cif_connect_ind (BD_ADDR bd_addr, UINT16 l2cap_cid, UINT16 psm, UINT8 l2cap_id)
214 BOOLEAN bAccept = TRUE;
215 UINT8 i = HID_HOST_MAX_DEVICES;
216 tHID_HOST_DEV_CTB *p_dev;
218 HIDH_TRACE_EVENT ("HID-Host Rcvd L2CAP conn ind, PSM: 0x%04x CID 0x%x", psm, l2cap_cid);
220 /* always add incoming connection device into HID database by default */
221 if (HID_HostAddDev(bd_addr, HID_SEC_REQUIRED, &i) != HID_SUCCESS)
223 L2CA_ConnectRsp (bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_SECURITY_BLOCK, 0);
227 p_hcon = &hh_cb.devices[i].conn;
228 p_dev = &hh_cb.devices[i];
230 /* Check we are in the correct state for this */
231 if (psm == HID_PSM_INTERRUPT)
233 if (p_hcon->ctrl_cid == 0)
235 HIDH_TRACE_WARNING ("HID-Host Rcvd INTR L2CAP conn ind, but no CTL channel");
238 if (p_hcon->conn_state != HID_CONN_STATE_CONNECTING_INTR)
240 HIDH_TRACE_WARNING ("HID-Host Rcvd INTR L2CAP conn ind, wrong state: %d",
245 else /* CTRL channel */
247 #if defined(HID_HOST_ACPT_NEW_CONN) && (HID_HOST_ACPT_NEW_CONN == TRUE)
248 p_hcon->ctrl_cid = p_hcon->intr_cid = 0;
249 p_hcon->conn_state = HID_CONN_STATE_UNUSED;
251 if (p_hcon->conn_state != HID_CONN_STATE_UNUSED)
253 HIDH_TRACE_WARNING ("HID-Host - Rcvd CTL L2CAP conn ind, wrong state: %d",
262 L2CA_ConnectRsp (bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_NO_RESOURCES, 0);
266 if (psm == HID_PSM_CONTROL)
268 p_hcon->conn_flags = 0;
269 p_hcon->ctrl_cid = l2cap_cid;
270 p_hcon->ctrl_id = l2cap_id;
271 p_hcon->disc_reason = HID_L2CAP_CONN_FAIL; /* In case disconnection occurs before security is completed, then set CLOSE_EVT reason code to 'connection failure' */
273 p_hcon->conn_state = HID_CONN_STATE_SECURITY;
274 if(btm_sec_mx_access_request (p_dev->addr, HID_PSM_CONTROL,
275 FALSE, BTM_SEC_PROTO_HID,
276 (p_dev->attr_mask & HID_SEC_REQUIRED) ? HID_SEC_CHN : HID_NOSEC_CHN,
277 &hidh_sec_check_complete_term, p_dev) == BTM_CMD_STARTED)
279 L2CA_ConnectRsp (bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_PENDING, L2CAP_CONN_OK);
285 /* Transition to the next appropriate state, configuration */
286 p_hcon->conn_state = HID_CONN_STATE_CONFIG;
287 p_hcon->intr_cid = l2cap_cid;
289 /* Send response to the L2CAP layer. */
290 L2CA_ConnectRsp (bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_OK, L2CAP_CONN_OK);
292 /* Send a Configuration Request. */
293 L2CA_ConfigReq (l2cap_cid, &hh_cb.l2cap_cfg);
295 HIDH_TRACE_EVENT ("HID-Host Rcvd L2CAP conn ind, sent config req, PSM: 0x%04x CID 0x%x",
299 void hidh_process_repage_timer_timeout(void *data)
301 uint8_t dhandle = PTR_TO_UINT(data);
302 hidh_try_repage(dhandle);
305 /*******************************************************************************
307 ** Function hidh_try_repage
309 ** Description This function processes timeout (to page device).
313 *******************************************************************************/
314 void hidh_try_repage(UINT8 dhandle)
316 tHID_HOST_DEV_CTB *device;
318 hidh_conn_initiate(dhandle);
320 device = &hh_cb.devices[dhandle];
321 device->conn_tries++;
323 hh_cb.callback(dhandle, device->addr, HID_HDEV_EVT_RETRYING,
324 device->conn_tries, NULL ) ;
327 /*******************************************************************************
329 ** Function hidh_sec_check_complete_orig
331 ** Description This function checks to see if security procedures are being
332 ** carried out or not..
336 *******************************************************************************/
337 void hidh_sec_check_complete_orig (BD_ADDR bd_addr, tBT_TRANSPORT transport, void *p_ref_data, UINT8 res)
339 tHID_HOST_DEV_CTB *p_dev = (tHID_HOST_DEV_CTB *) p_ref_data;
344 // TODO(armansito): This kind of math to determine a device handle is way
345 // too dirty and unnecessary. Why can't |p_dev| store it's handle?
346 dhandle = (PTR_TO_UINT(p_dev) - PTR_TO_UINT(&(hh_cb.devices[0])))/ sizeof(tHID_HOST_DEV_CTB);
347 if( res == BTM_SUCCESS && p_dev->conn.conn_state == HID_CONN_STATE_SECURITY )
349 HIDH_TRACE_EVENT ("HID-Host Originator security pass.");
350 p_dev->conn.disc_reason = HID_SUCCESS; /* Authentication passed. Reset disc_reason (from HID_ERR_AUTH_FAILED) */
352 /* Transition to the next appropriate state, configuration */
353 p_dev->conn.conn_state = HID_CONN_STATE_CONFIG;
354 L2CA_ConfigReq (p_dev->conn.ctrl_cid, &hh_cb.l2cap_cfg);
355 HIDH_TRACE_EVENT ("HID-Host Got Control conn cnf, sent cfg req, CID: 0x%x", p_dev->conn.ctrl_cid);
359 if( res != BTM_SUCCESS && p_dev->conn.conn_state == HID_CONN_STATE_SECURITY )
361 #if (HID_HOST_MAX_CONN_RETRY > 0)
362 if( res == BTM_DEVICE_TIMEOUT )
364 if( p_dev->conn_tries <= HID_HOST_MAX_CONN_RETRY )
366 hidh_conn_retry (dhandle);
371 p_dev->conn.disc_reason = HID_ERR_AUTH_FAILED; /* Save reason for disconnecting */
372 hidh_conn_disconnect(dhandle);
377 /*******************************************************************************
379 ** Function hidh_l2cif_connect_cfm
381 ** Description This function handles the connect confirm events
382 ** from L2CAP. This is the case when we are acting as a
383 ** client and have sent a connect request.
387 *******************************************************************************/
388 static void hidh_l2cif_connect_cfm (UINT16 l2cap_cid, UINT16 result)
391 tHID_CONN *p_hcon = NULL;
393 tHID_HOST_DEV_CTB *p_dev = NULL;
395 /* Find CCB based on CID, and verify we are in a state to accept this message */
396 if( (dhandle = find_conn_by_cid(l2cap_cid)) < HID_HOST_MAX_DEVICES )
398 p_dev = &hh_cb.devices[dhandle];
399 p_hcon = &hh_cb.devices[dhandle].conn;
403 || (!(p_hcon->conn_flags & HID_CONN_FLAGS_IS_ORIG))
404 || ((l2cap_cid == p_hcon->ctrl_cid) && (p_hcon->conn_state != HID_CONN_STATE_CONNECTING_CTRL))
405 || ((l2cap_cid == p_hcon->intr_cid) && (p_hcon->conn_state != HID_CONN_STATE_CONNECTING_INTR)
406 && (p_hcon->conn_state != HID_CONN_STATE_DISCONNECTING)))
408 HIDH_TRACE_WARNING ("HID-Host Rcvd unexpected conn cnf, CID 0x%x ", l2cap_cid);
412 if (result != L2CAP_CONN_OK)
414 if (l2cap_cid == p_hcon->ctrl_cid)
415 p_hcon->ctrl_cid = 0;
417 p_hcon->intr_cid = 0;
419 hidh_conn_disconnect(dhandle);
421 #if (HID_HOST_MAX_CONN_RETRY > 0)
422 if( (hh_cb.devices[dhandle].conn_tries <= HID_HOST_MAX_CONN_RETRY) &&
423 (result == HCI_ERR_CONNECTION_TOUT || result == HCI_ERR_UNSPECIFIED ||
424 result == HCI_ERR_PAGE_TIMEOUT) )
426 hidh_conn_retry(dhandle);
431 reason = HID_L2CAP_CONN_FAIL | (UINT32) result ;
432 hh_cb.callback( dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_CLOSE, reason, NULL ) ;
436 /* receive Control Channel connect confirmation */
437 if (l2cap_cid == p_hcon->ctrl_cid)
439 /* check security requirement */
440 p_hcon->conn_state = HID_CONN_STATE_SECURITY;
441 p_hcon->disc_reason = HID_L2CAP_CONN_FAIL; /* In case disconnection occurs before security is completed, then set CLOSE_EVT reason code to "connection failure" */
443 btm_sec_mx_access_request (p_dev->addr, HID_PSM_CONTROL,
444 TRUE, BTM_SEC_PROTO_HID,
445 (p_dev->attr_mask & HID_SEC_REQUIRED) ? HID_SEC_CHN : HID_NOSEC_CHN,
446 &hidh_sec_check_complete_orig, p_dev);
450 p_hcon->conn_state = HID_CONN_STATE_CONFIG;
451 /* Send a Configuration Request. */
452 L2CA_ConfigReq (l2cap_cid, &hh_cb.l2cap_cfg);
453 HIDH_TRACE_EVENT ("HID-Host got Interrupt conn cnf, sent cfg req, CID: 0x%x", l2cap_cid);
459 /*******************************************************************************
461 ** Function hidh_l2cif_config_ind
463 ** Description This function processes the L2CAP configuration indication
468 *******************************************************************************/
469 static void hidh_l2cif_config_ind (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg)
472 tHID_CONN *p_hcon = NULL;
475 /* Find CCB based on CID */
476 if( (dhandle = find_conn_by_cid(l2cap_cid)) < HID_HOST_MAX_DEVICES )
478 p_hcon = &hh_cb.devices[dhandle].conn;
483 HIDH_TRACE_WARNING ("HID-Host Rcvd L2CAP cfg ind, unknown CID: 0x%x", l2cap_cid);
487 HIDH_TRACE_EVENT ("HID-Host Rcvd cfg ind, sent cfg cfm, CID: 0x%x", l2cap_cid);
489 /* Remember the remote MTU size */
490 if ((!p_cfg->mtu_present) || (p_cfg->mtu > HID_HOST_MTU))
491 p_hcon->rem_mtu_size = HID_HOST_MTU;
493 p_hcon->rem_mtu_size = p_cfg->mtu;
495 /* For now, always accept configuration from the other side */
496 p_cfg->flush_to_present = FALSE;
497 p_cfg->mtu_present = FALSE;
498 p_cfg->result = L2CAP_CFG_OK;
500 L2CA_ConfigRsp (l2cap_cid, p_cfg);
502 if (l2cap_cid == p_hcon->ctrl_cid)
504 p_hcon->conn_flags |= HID_CONN_FLAGS_HIS_CTRL_CFG_DONE;
505 if ((p_hcon->conn_flags & HID_CONN_FLAGS_IS_ORIG) &&
506 (p_hcon->conn_flags & HID_CONN_FLAGS_MY_CTRL_CFG_DONE))
508 /* Connect interrupt channel */
509 p_hcon->disc_reason = HID_L2CAP_CONN_FAIL; /* Reset initial reason for CLOSE_EVT: Connection Attempt was made but failed */
510 if ((p_hcon->intr_cid = L2CA_ConnectReq (HID_PSM_INTERRUPT, hh_cb.devices[dhandle].addr)) == 0)
512 HIDH_TRACE_WARNING ("HID-Host INTR Originate failed");
513 reason = HID_L2CAP_REQ_FAIL ;
514 p_hcon->conn_state = HID_CONN_STATE_UNUSED;
515 hidh_conn_disconnect (dhandle);
516 hh_cb.callback( dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_CLOSE, reason, NULL ) ;
521 /* Transition to the next appropriate state, waiting for connection confirm on interrupt channel. */
522 p_hcon->conn_state = HID_CONN_STATE_CONNECTING_INTR;
527 p_hcon->conn_flags |= HID_CONN_FLAGS_HIS_INTR_CFG_DONE;
529 /* If all configuration is complete, change state and tell management we are up */
530 if (((p_hcon->conn_flags & HID_CONN_FLAGS_ALL_CONFIGURED) == HID_CONN_FLAGS_ALL_CONFIGURED)
531 && (p_hcon->conn_state == HID_CONN_STATE_CONFIG))
533 p_hcon->conn_state = HID_CONN_STATE_CONNECTED;
534 /* Reset disconnect reason to success, as connection successful */
535 p_hcon->disc_reason = HID_SUCCESS;
537 hh_cb.devices[dhandle].state = HID_DEV_CONNECTED;
538 hh_cb.callback( dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_OPEN, 0, NULL ) ;
543 /*******************************************************************************
545 ** Function hidh_l2cif_config_cfm
547 ** Description This function processes the L2CAP configuration confirmation
552 *******************************************************************************/
553 static void hidh_l2cif_config_cfm (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg)
556 tHID_CONN *p_hcon = NULL;
559 HIDH_TRACE_EVENT ("HID-Host Rcvd cfg cfm, CID: 0x%x Result: %d", l2cap_cid, p_cfg->result);
561 /* Find CCB based on CID */
562 if( (dhandle = find_conn_by_cid(l2cap_cid)) < HID_HOST_MAX_DEVICES )
563 p_hcon = &hh_cb.devices[dhandle].conn;
567 HIDH_TRACE_WARNING ("HID-Host Rcvd L2CAP cfg ind, unknown CID: 0x%x", l2cap_cid);
571 /* If configuration failed, disconnect the channel(s) */
572 if (p_cfg->result != L2CAP_CFG_OK)
574 hidh_conn_disconnect (dhandle);
575 reason = HID_L2CAP_CFG_FAIL | (UINT32) p_cfg->result ;
576 hh_cb.callback( dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_CLOSE, reason, NULL ) ;
580 if (l2cap_cid == p_hcon->ctrl_cid)
582 p_hcon->conn_flags |= HID_CONN_FLAGS_MY_CTRL_CFG_DONE;
583 if ((p_hcon->conn_flags & HID_CONN_FLAGS_IS_ORIG) &&
584 (p_hcon->conn_flags & HID_CONN_FLAGS_HIS_CTRL_CFG_DONE))
586 /* Connect interrupt channel */
587 p_hcon->disc_reason = HID_L2CAP_CONN_FAIL; /* Reset initial reason for CLOSE_EVT: Connection Attempt was made but failed */
588 if ((p_hcon->intr_cid = L2CA_ConnectReq (HID_PSM_INTERRUPT, hh_cb.devices[dhandle].addr)) == 0)
590 HIDH_TRACE_WARNING ("HID-Host INTR Originate failed");
591 reason = HID_L2CAP_REQ_FAIL ;
592 p_hcon->conn_state = HID_CONN_STATE_UNUSED;
593 hidh_conn_disconnect (dhandle);
594 hh_cb.callback( dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_CLOSE, reason, NULL ) ;
599 /* Transition to the next appropriate state, waiting for connection confirm on interrupt channel. */
600 p_hcon->conn_state = HID_CONN_STATE_CONNECTING_INTR;
605 p_hcon->conn_flags |= HID_CONN_FLAGS_MY_INTR_CFG_DONE;
607 /* If all configuration is complete, change state and tell management we are up */
608 if (((p_hcon->conn_flags & HID_CONN_FLAGS_ALL_CONFIGURED) == HID_CONN_FLAGS_ALL_CONFIGURED)
609 && (p_hcon->conn_state == HID_CONN_STATE_CONFIG))
611 p_hcon->conn_state = HID_CONN_STATE_CONNECTED;
612 /* Reset disconnect reason to success, as connection successful */
613 p_hcon->disc_reason = HID_SUCCESS;
615 hh_cb.devices[dhandle].state = HID_DEV_CONNECTED;
616 hh_cb.callback( dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_OPEN, 0, NULL ) ;
621 /*******************************************************************************
623 ** Function hidh_l2cif_disconnect_ind
625 ** Description This function handles a disconnect event from L2CAP. If
626 ** requested to, we ack the disconnect before dropping the CCB
630 *******************************************************************************/
631 static void hidh_l2cif_disconnect_ind (UINT16 l2cap_cid, BOOLEAN ack_needed)
634 tHID_CONN *p_hcon = NULL;
635 UINT16 disc_res = HCI_SUCCESS;
636 UINT16 hid_close_evt_reason;
638 /* Find CCB based on CID */
639 if( (dhandle = find_conn_by_cid(l2cap_cid)) < HID_HOST_MAX_DEVICES )
640 p_hcon = &hh_cb.devices[dhandle].conn;
644 HIDH_TRACE_WARNING ("HID-Host Rcvd L2CAP disc, unknown CID: 0x%x", l2cap_cid);
649 L2CA_DisconnectRsp (l2cap_cid);
651 HIDH_TRACE_EVENT ("HID-Host Rcvd L2CAP disc, CID: 0x%x", l2cap_cid);
653 p_hcon->conn_state = HID_CONN_STATE_DISCONNECTING;
655 if (l2cap_cid == p_hcon->ctrl_cid)
656 p_hcon->ctrl_cid = 0;
658 p_hcon->intr_cid = 0;
660 if ((p_hcon->ctrl_cid == 0) && (p_hcon->intr_cid == 0))
662 hh_cb.devices[dhandle].state = HID_DEV_NO_CONN;
663 p_hcon->conn_state = HID_CONN_STATE_UNUSED;
666 disc_res = btm_get_acl_disc_reason_code();
668 #if (HID_HOST_MAX_CONN_RETRY > 0)
669 if( (disc_res == HCI_ERR_CONNECTION_TOUT || disc_res == HCI_ERR_UNSPECIFIED) &&
670 (!(hh_cb.devices[dhandle].attr_mask & HID_RECONN_INIT)) &&
671 (hh_cb.devices[dhandle].attr_mask & HID_NORMALLY_CONNECTABLE))
673 hh_cb.devices[dhandle].conn_tries = 0;
674 period_ms_t interval_ms = HID_HOST_REPAGE_WIN * 1000;
675 alarm_set_on_queue(hh_cb.devices[dhandle].conn.process_repage_timer,
676 interval_ms, hidh_process_repage_timer_timeout,
677 UINT_TO_PTR(dhandle), btu_general_alarm_queue);
678 hh_cb.callback( dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_CLOSE, disc_res, NULL);
683 /* Set reason code for HID_HDEV_EVT_CLOSE */
684 hid_close_evt_reason = p_hcon->disc_reason;
686 /* If we got baseband sent HCI_DISCONNECT_COMPLETE_EVT due to security failure, then set reason to HID_ERR_AUTH_FAILED */
687 if ((disc_res == HCI_ERR_AUTH_FAILURE) ||
688 (disc_res == HCI_ERR_KEY_MISSING) ||
689 (disc_res == HCI_ERR_HOST_REJECT_SECURITY) ||
690 (disc_res == HCI_ERR_PAIRING_NOT_ALLOWED) ||
691 (disc_res == HCI_ERR_UNIT_KEY_USED) ||
692 (disc_res == HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED) ||
693 (disc_res == HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE) ||
694 (disc_res == HCI_ERR_REPEATED_ATTEMPTS))
696 hid_close_evt_reason = HID_ERR_AUTH_FAILED;
699 hh_cb.callback( dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_CLOSE, hid_close_evt_reason, NULL ) ;
705 /*******************************************************************************
707 ** Function hidh_l2cif_disconnect_cfm
709 ** Description This function handles a disconnect confirm event from L2CAP.
713 *******************************************************************************/
714 static void hidh_l2cif_disconnect_cfm (UINT16 l2cap_cid, UINT16 result)
717 tHID_CONN *p_hcon = NULL;
720 /* Find CCB based on CID */
721 if( (dhandle = find_conn_by_cid(l2cap_cid)) < HID_HOST_MAX_DEVICES )
722 p_hcon = &hh_cb.devices[dhandle].conn;
726 HIDH_TRACE_WARNING ("HID-Host Rcvd L2CAP disc cfm, unknown CID: 0x%x", l2cap_cid);
730 HIDH_TRACE_EVENT ("HID-Host Rcvd L2CAP disc cfm, CID: 0x%x", l2cap_cid);
732 if (l2cap_cid == p_hcon->ctrl_cid)
733 p_hcon->ctrl_cid = 0;
736 p_hcon->intr_cid = 0;
737 if (p_hcon->ctrl_cid)
739 HIDH_TRACE_EVENT ("HID-Host Initiating L2CAP Ctrl disconnection");
740 L2CA_DisconnectReq (p_hcon->ctrl_cid);
744 if ((p_hcon->ctrl_cid == 0) && (p_hcon->intr_cid == 0))
746 hh_cb.devices[dhandle].state = HID_DEV_NO_CONN;
747 p_hcon->conn_state = HID_CONN_STATE_UNUSED;
748 hh_cb.callback( dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_CLOSE, p_hcon->disc_reason, NULL ) ;
753 /*******************************************************************************
755 ** Function hidh_l2cif_cong_ind
757 ** Description This function handles a congestion status event from L2CAP.
761 *******************************************************************************/
762 static void hidh_l2cif_cong_ind (UINT16 l2cap_cid, BOOLEAN congested)
765 tHID_CONN *p_hcon = NULL;
767 /* Find CCB based on CID */
768 if( (dhandle = find_conn_by_cid(l2cap_cid)) < HID_HOST_MAX_DEVICES )
769 p_hcon = &hh_cb.devices[dhandle].conn;
773 HIDH_TRACE_WARNING ("HID-Host Rcvd L2CAP congestion status, unknown CID: 0x%x", l2cap_cid);
777 HIDH_TRACE_EVENT ("HID-Host Rcvd L2CAP congestion status, CID: 0x%x Cong: %d", l2cap_cid, congested);
780 p_hcon->conn_flags |= HID_CONN_FLAGS_CONGESTED;
783 p_hcon->conn_flags &= ~HID_CONN_FLAGS_CONGESTED;
789 /*******************************************************************************
791 ** Function hidh_l2cif_data_ind
793 ** Description This function is called when data is received from L2CAP.
794 ** if we are the originator of the connection, we are the SDP
795 ** client, and the received message is queued up for the client.
797 ** If we are the destination of the connection, we are the SDP
798 ** server, so the message is passed to the server processing
803 *******************************************************************************/
804 static void hidh_l2cif_data_ind (UINT16 l2cap_cid, BT_HDR *p_msg)
806 UINT8 *p_data = (UINT8 *)(p_msg + 1) + p_msg->offset;
807 UINT8 ttype, param, rep_type, evt;
809 tHID_CONN *p_hcon = NULL;
811 HIDH_TRACE_DEBUG ("HID-Host hidh_l2cif_data_ind [l2cap_cid=0x%04x]", l2cap_cid);
813 /* Find CCB based on CID */
814 if ((dhandle = find_conn_by_cid(l2cap_cid)) < HID_HOST_MAX_DEVICES)
815 p_hcon = &hh_cb.devices[dhandle].conn;
819 HIDH_TRACE_WARNING ("HID-Host Rcvd L2CAP data, unknown CID: 0x%x", l2cap_cid);
826 HIDH_TRACE_WARNING ("HID-Host Rcvd L2CAP data, invalid length");
828 android_errorWriteLog(0x534e4554, "80493272");
832 ttype = HID_GET_TRANS_FROM_HDR(*p_data);
833 param = HID_GET_PARAM_FROM_HDR(*p_data);
834 rep_type = param & HID_PAR_REP_TYPE_MASK;
837 /* Get rid of the data type */
843 case HID_TRANS_HANDSHAKE:
844 hh_cb.callback(dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_HANDSHAKE, param, NULL);
848 case HID_TRANS_CONTROL:
851 case HID_PAR_CONTROL_VIRTUAL_CABLE_UNPLUG:
852 hidh_conn_disconnect( dhandle ) ;
853 /* Device is unplugging from us. Tell USB */
854 hh_cb.callback(dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_VC_UNPLUG, 0, NULL);
865 evt = (hh_cb.devices[dhandle].conn.intr_cid == l2cap_cid) ?
866 HID_HDEV_EVT_INTR_DATA : HID_HDEV_EVT_CTRL_DATA;
867 hh_cb.callback(dhandle, hh_cb.devices[dhandle].addr, evt, rep_type, p_msg);
870 case HID_TRANS_DATAC:
871 evt = (hh_cb.devices[dhandle].conn.intr_cid == l2cap_cid) ?
872 HID_HDEV_EVT_INTR_DATC : HID_HDEV_EVT_CTRL_DATC;
873 hh_cb.callback(dhandle, hh_cb.devices[dhandle].addr, evt, rep_type, p_msg);
882 /*******************************************************************************
884 ** Function hidh_conn_snd_data
886 ** Description This function is sends out data.
888 ** Returns tHID_STATUS
890 *******************************************************************************/
891 tHID_STATUS hidh_conn_snd_data (UINT8 dhandle, UINT8 trans_type, UINT8 param,
892 UINT16 data, UINT8 report_id, BT_HDR *buf)
894 tHID_CONN *p_hcon = &hh_cb.devices[dhandle].conn;
898 BOOLEAN seg_req = FALSE;
903 BOOLEAN blank_datc = FALSE;
905 if (!BTM_IsAclConnectionUp(hh_cb.devices[dhandle].addr, BT_TRANSPORT_BR_EDR))
908 return HID_ERR_NO_CONNECTION;
911 if (p_hcon->conn_flags & HID_CONN_FLAGS_CONGESTED)
914 return HID_ERR_CONGESTED;
919 case HID_TRANS_CONTROL:
920 case HID_TRANS_GET_REPORT:
921 case HID_TRANS_SET_REPORT:
922 case HID_TRANS_GET_PROTOCOL:
923 case HID_TRANS_SET_PROTOCOL:
924 case HID_TRANS_GET_IDLE:
925 case HID_TRANS_SET_IDLE:
926 cid = p_hcon->ctrl_cid;
927 buf_size = HID_CONTROL_BUF_SIZE;
930 cid = p_hcon->intr_cid;
931 buf_size = HID_INTERRUPT_BUF_SIZE;
934 return (HID_ERR_INVALID_PARAM) ;
937 if( trans_type == HID_TRANS_SET_IDLE )
939 else if( (trans_type == HID_TRANS_GET_REPORT) && (param & 0x08) )
944 if ( buf == NULL || blank_datc )
946 p_buf = (BT_HDR *)osi_malloc(buf_size);
948 p_buf->offset = L2CAP_MIN_OFFSET;
954 else if ( (buf->len > (p_hcon->rem_mtu_size - 1)))
956 p_buf = (BT_HDR *)osi_malloc(buf_size);
958 p_buf->offset = L2CAP_MIN_OFFSET;
960 data_size = buf->len;
961 bytes_copied = p_hcon->rem_mtu_size - 1;
968 data_size = buf->len;
969 bytes_copied = buf->len;
972 p_out = (UINT8 *)(p_buf + 1) + p_buf->offset;
973 *p_out++ = HID_BUILD_HDR(trans_type, param);
975 /* If report ID required for this device */
976 if( (trans_type == HID_TRANS_GET_REPORT) && (report_id != 0) )
979 data_size = bytes_copied = 1;
985 memcpy (p_out, (((UINT8 *)(buf+1)) + buf->offset), bytes_copied);
986 buf->offset += bytes_copied;
987 buf->len -= bytes_copied;
989 else if( use_data == 1)
991 *(p_out+bytes_copied) = data & 0xff;
993 else if( use_data == 2 )
995 *(p_out+bytes_copied) = data & 0xff;
996 *(p_out+bytes_copied+1) = (data >> 8) & 0xff ;
999 p_buf->len = bytes_copied + 1 + use_data;
1000 data_size -= bytes_copied;
1002 /* Send the buffer through L2CAP */
1003 if ((p_hcon->conn_flags & HID_CONN_FLAGS_CONGESTED) || (!L2CA_DataWrite (cid, p_buf)))
1004 return (HID_ERR_CONGESTED);
1007 trans_type = HID_TRANS_DATAC;
1008 else if( bytes_copied == (p_hcon->rem_mtu_size - 1) )
1010 trans_type = HID_TRANS_DATAC;
1014 } while ((data_size != 0) || blank_datc ) ;
1016 return (HID_SUCCESS);
1018 /*******************************************************************************
1020 ** Function hidh_conn_initiate
1022 ** Description This function is called by the management to create a connection.
1026 *******************************************************************************/
1027 tHID_STATUS hidh_conn_initiate (UINT8 dhandle)
1029 UINT8 service_id = BTM_SEC_SERVICE_HIDH_NOSEC_CTRL;
1030 UINT32 mx_chan_id = HID_NOSEC_CHN;
1032 tHID_HOST_DEV_CTB *p_dev = &hh_cb.devices[dhandle];
1034 if( p_dev->conn.conn_state != HID_CONN_STATE_UNUSED )
1035 return( HID_ERR_CONN_IN_PROCESS );
1037 p_dev->conn.ctrl_cid = 0;
1038 p_dev->conn.intr_cid = 0;
1039 p_dev->conn.disc_reason = HID_L2CAP_CONN_FAIL; /* Reset initial reason for CLOSE_EVT: Connection Attempt was made but failed */
1041 /* We are the originator of this connection */
1042 p_dev->conn.conn_flags = HID_CONN_FLAGS_IS_ORIG;
1044 if(p_dev->attr_mask & HID_SEC_REQUIRED)
1046 service_id = BTM_SEC_SERVICE_HIDH_SEC_CTRL;
1047 mx_chan_id = HID_SEC_CHN;
1049 BTM_SetOutService (p_dev->addr, service_id, mx_chan_id);
1051 /* Check if L2CAP started the connection process */
1052 if ((p_dev->conn.ctrl_cid = L2CA_ConnectReq (HID_PSM_CONTROL, p_dev->addr)) == 0)
1054 HIDH_TRACE_WARNING ("HID-Host Originate failed");
1055 hh_cb.callback( dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_CLOSE,
1056 HID_ERR_L2CAP_FAILED, NULL ) ;
1060 /* Transition to the next appropriate state, waiting for connection confirm on control channel. */
1061 p_dev->conn.conn_state = HID_CONN_STATE_CONNECTING_CTRL;
1064 return( HID_SUCCESS );
1068 /*******************************************************************************
1070 ** Function find_conn_by_cid
1072 ** Description This function finds a connection control block based on CID
1074 ** Returns address of control block, or NULL if not found
1076 *******************************************************************************/
1077 static UINT8 find_conn_by_cid (UINT16 cid)
1081 for (xx = 0; xx < HID_HOST_MAX_DEVICES; xx++)
1083 if ((hh_cb.devices[xx].in_use) && (hh_cb.devices[xx].conn.conn_state != HID_CONN_STATE_UNUSED)
1084 && ((hh_cb.devices[xx].conn.ctrl_cid == cid) || (hh_cb.devices[xx].conn.intr_cid == cid)))
1091 void hidh_conn_dereg( void )
1093 L2CA_Deregister (HID_PSM_CONTROL);
1094 L2CA_Deregister (HID_PSM_INTERRUPT);
1097 /*******************************************************************************
1099 ** Function hidh_conn_retry
1101 ** Description This function is called to retry a failed connection.
1105 *******************************************************************************/
1106 static void hidh_conn_retry( UINT8 dhandle )
1108 tHID_HOST_DEV_CTB *p_dev = &hh_cb.devices[dhandle];
1110 p_dev->conn.conn_state = HID_CONN_STATE_UNUSED;
1111 #if (HID_HOST_REPAGE_WIN > 0)
1112 period_ms_t interval_ms = HID_HOST_REPAGE_WIN * 1000;
1113 alarm_set_on_queue(p_dev->conn.process_repage_timer,
1114 interval_ms, hidh_process_repage_timer_timeout,
1115 UINT_TO_PTR(dhandle), btu_general_alarm_queue);
1117 hidh_process_repage_process(dhandle);