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"
46 #include "osi/include/osi.h"
49 extern fixed_queue_t *btu_general_alarm_queue;
51 static UINT8 find_conn_by_cid (UINT16 cid);
52 static void hidh_conn_retry (UINT8 dhandle);
54 /********************************************************************************/
55 /* L O C A L F U N C T I O N P R O T O T Y P E S */
56 /********************************************************************************/
57 static void hidh_l2cif_connect_ind (BD_ADDR bd_addr, UINT16 l2cap_cid,
58 UINT16 psm, UINT8 l2cap_id);
59 static void hidh_l2cif_connect_cfm (UINT16 l2cap_cid, UINT16 result);
60 static void hidh_l2cif_config_ind (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg);
61 static void hidh_l2cif_config_cfm (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg);
62 static void hidh_l2cif_disconnect_ind (UINT16 l2cap_cid, BOOLEAN ack_needed);
63 static void hidh_l2cif_data_ind (UINT16 l2cap_cid, BT_HDR *p_msg);
64 static void hidh_l2cif_disconnect_cfm (UINT16 l2cap_cid, UINT16 result);
65 static void hidh_l2cif_cong_ind (UINT16 l2cap_cid, BOOLEAN congested);
67 static const tL2CAP_APPL_INFO hst_reg_info =
69 hidh_l2cif_connect_ind,
70 hidh_l2cif_connect_cfm,
72 hidh_l2cif_config_ind,
73 hidh_l2cif_config_cfm,
74 hidh_l2cif_disconnect_ind,
75 hidh_l2cif_disconnect_cfm,
79 NULL /* tL2CA_TX_COMPLETE_CB */
82 /*******************************************************************************
84 ** Function hidh_l2cif_reg
86 ** Description This function initializes the SDP unit.
90 *******************************************************************************/
91 tHID_STATUS hidh_conn_reg (void)
95 /* Initialize the L2CAP configuration. We only care about MTU and flush */
96 memset(&hh_cb.l2cap_cfg, 0, sizeof(tL2CAP_CFG_INFO));
98 hh_cb.l2cap_cfg.mtu_present = TRUE;
99 hh_cb.l2cap_cfg.mtu = HID_HOST_MTU;
100 hh_cb.l2cap_cfg.flush_to_present = TRUE;
101 hh_cb.l2cap_cfg.flush_to = HID_HOST_FLUSH_TO;
103 /* Now, register with L2CAP */
104 if (!L2CA_Register (HID_PSM_CONTROL, (tL2CAP_APPL_INFO *) &hst_reg_info))
106 HIDH_TRACE_ERROR ("HID-Host Control Registration failed");
107 return (HID_ERR_L2CAP_FAILED) ;
109 if (!L2CA_Register (HID_PSM_INTERRUPT, (tL2CAP_APPL_INFO *) &hst_reg_info))
111 L2CA_Deregister( HID_PSM_CONTROL ) ;
112 HIDH_TRACE_ERROR ("HID-Host Interrupt Registration failed");
113 return (HID_ERR_L2CAP_FAILED) ;
116 for (xx = 0; xx < HID_HOST_MAX_DEVICES; xx++)
118 hh_cb.devices[xx].in_use = FALSE ;
119 hh_cb.devices[xx].conn.conn_state = HID_CONN_STATE_UNUSED;
122 return (HID_SUCCESS);
125 /*******************************************************************************
127 ** Function hidh_conn_disconnect
129 ** Description This function disconnects a connection.
131 ** Returns TRUE if disconnect started, FALSE if already disconnected
133 *******************************************************************************/
134 tHID_STATUS hidh_conn_disconnect (UINT8 dhandle)
136 tHID_CONN *p_hcon = &hh_cb.devices[dhandle].conn;
138 HIDH_TRACE_EVENT ("HID-Host disconnect");
140 if ((p_hcon->ctrl_cid != 0) || (p_hcon->intr_cid != 0))
142 p_hcon->conn_state = HID_CONN_STATE_DISCONNECTING;
144 /* Set l2cap idle timeout to 0 (so ACL link is disconnected
145 * immediately after last channel is closed) */
146 L2CA_SetIdleTimeoutByBdAddr(hh_cb.devices[dhandle].addr, 0, BT_TRANSPORT_BR_EDR);
147 /* Disconnect both interrupt and control channels */
148 if (p_hcon->intr_cid)
149 L2CA_DisconnectReq (p_hcon->intr_cid);
150 else if (p_hcon->ctrl_cid)
151 L2CA_DisconnectReq (p_hcon->ctrl_cid);
155 p_hcon->conn_state = HID_CONN_STATE_UNUSED;
158 return (HID_SUCCESS);
161 /*******************************************************************************
163 ** Function hidh_sec_check_complete_term
165 ** Description HID security check complete callback function.
167 ** Returns Send L2CA_ConnectRsp OK if secutiry check succeed; otherwise
168 ** send security block L2C connection response.
170 *******************************************************************************/
171 void hidh_sec_check_complete_term (BD_ADDR bd_addr, tBT_TRANSPORT transport, void *p_ref_data, UINT8 res)
173 tHID_HOST_DEV_CTB *p_dev= (tHID_HOST_DEV_CTB *) p_ref_data;
177 if( res == BTM_SUCCESS && p_dev->conn.conn_state == HID_CONN_STATE_SECURITY )
179 p_dev->conn.disc_reason = HID_SUCCESS; /* Authentication passed. Reset disc_reason (from HID_ERR_AUTH_FAILED) */
181 p_dev->conn.conn_state = HID_CONN_STATE_CONNECTING_INTR;
183 /* Send response to the L2CAP layer. */
184 L2CA_ConnectRsp (p_dev->addr, p_dev->conn.ctrl_id, p_dev->conn.ctrl_cid, L2CAP_CONN_OK, L2CAP_CONN_OK);
186 /* Send a Configuration Request. */
187 L2CA_ConfigReq (p_dev->conn.ctrl_cid, &hh_cb.l2cap_cfg);
190 /* security check fail */
191 else if (res != BTM_SUCCESS)
193 p_dev->conn.disc_reason = HID_ERR_AUTH_FAILED; /* Save reason for disconnecting */
194 p_dev->conn.conn_state = HID_CONN_STATE_UNUSED;
195 L2CA_ConnectRsp (p_dev->addr, p_dev->conn.ctrl_id, p_dev->conn.ctrl_cid, L2CAP_CONN_SECURITY_BLOCK, L2CAP_CONN_OK);
199 /*******************************************************************************
201 ** Function hidh_l2cif_connect_ind
203 ** Description This function handles an inbound connection indication
204 ** from L2CAP. This is the case where we are acting as a
209 *******************************************************************************/
210 static void hidh_l2cif_connect_ind (BD_ADDR bd_addr, UINT16 l2cap_cid, UINT16 psm, UINT8 l2cap_id)
213 BOOLEAN bAccept = TRUE;
214 UINT8 i = HID_HOST_MAX_DEVICES;
215 tHID_HOST_DEV_CTB *p_dev;
217 HIDH_TRACE_EVENT ("HID-Host Rcvd L2CAP conn ind, PSM: 0x%04x CID 0x%x", psm, l2cap_cid);
219 /* always add incoming connection device into HID database by default */
220 if (HID_HostAddDev(bd_addr, HID_SEC_REQUIRED, &i) != HID_SUCCESS)
222 L2CA_ConnectRsp (bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_SECURITY_BLOCK, 0);
226 p_hcon = &hh_cb.devices[i].conn;
227 p_dev = &hh_cb.devices[i];
229 /* Check we are in the correct state for this */
230 if (psm == HID_PSM_INTERRUPT)
232 if (p_hcon->ctrl_cid == 0)
234 HIDH_TRACE_WARNING ("HID-Host Rcvd INTR L2CAP conn ind, but no CTL channel");
237 if (p_hcon->conn_state != HID_CONN_STATE_CONNECTING_INTR)
239 HIDH_TRACE_WARNING ("HID-Host Rcvd INTR L2CAP conn ind, wrong state: %d",
244 else /* CTRL channel */
246 #if defined(HID_HOST_ACPT_NEW_CONN) && (HID_HOST_ACPT_NEW_CONN == TRUE)
247 p_hcon->ctrl_cid = p_hcon->intr_cid = 0;
248 p_hcon->conn_state = HID_CONN_STATE_UNUSED;
250 if (p_hcon->conn_state != HID_CONN_STATE_UNUSED)
252 HIDH_TRACE_WARNING ("HID-Host - Rcvd CTL L2CAP conn ind, wrong state: %d",
261 L2CA_ConnectRsp (bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_NO_RESOURCES, 0);
265 if (psm == HID_PSM_CONTROL)
267 p_hcon->conn_flags = 0;
268 p_hcon->ctrl_cid = l2cap_cid;
269 p_hcon->ctrl_id = l2cap_id;
270 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' */
272 p_hcon->conn_state = HID_CONN_STATE_SECURITY;
273 if(btm_sec_mx_access_request (p_dev->addr, HID_PSM_CONTROL,
274 FALSE, BTM_SEC_PROTO_HID,
275 (p_dev->attr_mask & HID_SEC_REQUIRED) ? HID_SEC_CHN : HID_NOSEC_CHN,
276 &hidh_sec_check_complete_term, p_dev) == BTM_CMD_STARTED)
278 L2CA_ConnectRsp (bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_PENDING, L2CAP_CONN_OK);
284 /* Transition to the next appropriate state, configuration */
285 p_hcon->conn_state = HID_CONN_STATE_CONFIG;
286 p_hcon->intr_cid = l2cap_cid;
288 /* Send response to the L2CAP layer. */
289 L2CA_ConnectRsp (bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_OK, L2CAP_CONN_OK);
291 /* Send a Configuration Request. */
292 L2CA_ConfigReq (l2cap_cid, &hh_cb.l2cap_cfg);
294 HIDH_TRACE_EVENT ("HID-Host Rcvd L2CAP conn ind, sent config req, PSM: 0x%04x CID 0x%x",
298 void hidh_process_repage_timer_timeout(void *data)
300 uint8_t dhandle = PTR_TO_UINT(data);
301 hidh_try_repage(dhandle);
304 /*******************************************************************************
306 ** Function hidh_try_repage
308 ** Description This function processes timeout (to page device).
312 *******************************************************************************/
313 void hidh_try_repage(UINT8 dhandle)
315 tHID_HOST_DEV_CTB *device;
317 hidh_conn_initiate(dhandle);
319 device = &hh_cb.devices[dhandle];
320 device->conn_tries++;
322 hh_cb.callback(dhandle, device->addr, HID_HDEV_EVT_RETRYING,
323 device->conn_tries, NULL ) ;
326 /*******************************************************************************
328 ** Function hidh_sec_check_complete_orig
330 ** Description This function checks to see if security procedures are being
331 ** carried out or not..
335 *******************************************************************************/
336 void hidh_sec_check_complete_orig (BD_ADDR bd_addr, tBT_TRANSPORT transport, void *p_ref_data, UINT8 res)
338 tHID_HOST_DEV_CTB *p_dev = (tHID_HOST_DEV_CTB *) p_ref_data;
343 // TODO(armansito): This kind of math to determine a device handle is way
344 // too dirty and unnecessary. Why can't |p_dev| store it's handle?
345 dhandle = (PTR_TO_UINT(p_dev) - PTR_TO_UINT(&(hh_cb.devices[0])))/ sizeof(tHID_HOST_DEV_CTB);
346 if( res == BTM_SUCCESS && p_dev->conn.conn_state == HID_CONN_STATE_SECURITY )
348 HIDH_TRACE_EVENT ("HID-Host Originator security pass.");
349 p_dev->conn.disc_reason = HID_SUCCESS; /* Authentication passed. Reset disc_reason (from HID_ERR_AUTH_FAILED) */
351 /* Transition to the next appropriate state, configuration */
352 p_dev->conn.conn_state = HID_CONN_STATE_CONFIG;
353 L2CA_ConfigReq (p_dev->conn.ctrl_cid, &hh_cb.l2cap_cfg);
354 HIDH_TRACE_EVENT ("HID-Host Got Control conn cnf, sent cfg req, CID: 0x%x", p_dev->conn.ctrl_cid);
358 if( res != BTM_SUCCESS && p_dev->conn.conn_state == HID_CONN_STATE_SECURITY )
360 #if (HID_HOST_MAX_CONN_RETRY > 0)
361 if( res == BTM_DEVICE_TIMEOUT )
363 if( p_dev->conn_tries <= HID_HOST_MAX_CONN_RETRY )
365 hidh_conn_retry (dhandle);
370 p_dev->conn.disc_reason = HID_ERR_AUTH_FAILED; /* Save reason for disconnecting */
371 hidh_conn_disconnect(dhandle);
376 /*******************************************************************************
378 ** Function hidh_l2cif_connect_cfm
380 ** Description This function handles the connect confirm events
381 ** from L2CAP. This is the case when we are acting as a
382 ** client and have sent a connect request.
386 *******************************************************************************/
387 static void hidh_l2cif_connect_cfm (UINT16 l2cap_cid, UINT16 result)
390 tHID_CONN *p_hcon = NULL;
392 tHID_HOST_DEV_CTB *p_dev = NULL;
394 /* Find CCB based on CID, and verify we are in a state to accept this message */
395 if( (dhandle = find_conn_by_cid(l2cap_cid)) < HID_HOST_MAX_DEVICES )
397 p_dev = &hh_cb.devices[dhandle];
398 p_hcon = &hh_cb.devices[dhandle].conn;
402 || (!(p_hcon->conn_flags & HID_CONN_FLAGS_IS_ORIG))
403 || ((l2cap_cid == p_hcon->ctrl_cid) && (p_hcon->conn_state != HID_CONN_STATE_CONNECTING_CTRL))
404 || ((l2cap_cid == p_hcon->intr_cid) && (p_hcon->conn_state != HID_CONN_STATE_CONNECTING_INTR)
405 && (p_hcon->conn_state != HID_CONN_STATE_DISCONNECTING)))
407 HIDH_TRACE_WARNING ("HID-Host Rcvd unexpected conn cnf, CID 0x%x ", l2cap_cid);
411 if (result != L2CAP_CONN_OK)
413 if (l2cap_cid == p_hcon->ctrl_cid)
414 p_hcon->ctrl_cid = 0;
416 p_hcon->intr_cid = 0;
418 hidh_conn_disconnect(dhandle);
420 #if (HID_HOST_MAX_CONN_RETRY > 0)
421 if( (hh_cb.devices[dhandle].conn_tries <= HID_HOST_MAX_CONN_RETRY) &&
422 (result == HCI_ERR_CONNECTION_TOUT || result == HCI_ERR_UNSPECIFIED ||
423 result == HCI_ERR_PAGE_TIMEOUT) )
425 hidh_conn_retry(dhandle);
430 reason = HID_L2CAP_CONN_FAIL | (UINT32) result ;
431 hh_cb.callback( dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_CLOSE, reason, NULL ) ;
435 /* receive Control Channel connect confirmation */
436 if (l2cap_cid == p_hcon->ctrl_cid)
438 /* check security requirement */
439 p_hcon->conn_state = HID_CONN_STATE_SECURITY;
440 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" */
442 btm_sec_mx_access_request (p_dev->addr, HID_PSM_CONTROL,
443 TRUE, BTM_SEC_PROTO_HID,
444 (p_dev->attr_mask & HID_SEC_REQUIRED) ? HID_SEC_CHN : HID_NOSEC_CHN,
445 &hidh_sec_check_complete_orig, p_dev);
449 p_hcon->conn_state = HID_CONN_STATE_CONFIG;
450 /* Send a Configuration Request. */
451 L2CA_ConfigReq (l2cap_cid, &hh_cb.l2cap_cfg);
452 HIDH_TRACE_EVENT ("HID-Host got Interrupt conn cnf, sent cfg req, CID: 0x%x", l2cap_cid);
458 /*******************************************************************************
460 ** Function hidh_l2cif_config_ind
462 ** Description This function processes the L2CAP configuration indication
467 *******************************************************************************/
468 static void hidh_l2cif_config_ind (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg)
471 tHID_CONN *p_hcon = NULL;
474 /* Find CCB based on CID */
475 if( (dhandle = find_conn_by_cid(l2cap_cid)) < HID_HOST_MAX_DEVICES )
477 p_hcon = &hh_cb.devices[dhandle].conn;
482 HIDH_TRACE_WARNING ("HID-Host Rcvd L2CAP cfg ind, unknown CID: 0x%x", l2cap_cid);
486 HIDH_TRACE_EVENT ("HID-Host Rcvd cfg ind, sent cfg cfm, CID: 0x%x", l2cap_cid);
488 /* Remember the remote MTU size */
489 if ((!p_cfg->mtu_present) || (p_cfg->mtu > HID_HOST_MTU))
490 p_hcon->rem_mtu_size = HID_HOST_MTU;
492 p_hcon->rem_mtu_size = p_cfg->mtu;
494 /* For now, always accept configuration from the other side */
495 p_cfg->flush_to_present = FALSE;
496 p_cfg->mtu_present = FALSE;
497 p_cfg->result = L2CAP_CFG_OK;
499 L2CA_ConfigRsp (l2cap_cid, p_cfg);
501 if (l2cap_cid == p_hcon->ctrl_cid)
503 p_hcon->conn_flags |= HID_CONN_FLAGS_HIS_CTRL_CFG_DONE;
504 if ((p_hcon->conn_flags & HID_CONN_FLAGS_IS_ORIG) &&
505 (p_hcon->conn_flags & HID_CONN_FLAGS_MY_CTRL_CFG_DONE))
507 /* Connect interrupt channel */
508 p_hcon->disc_reason = HID_L2CAP_CONN_FAIL; /* Reset initial reason for CLOSE_EVT: Connection Attempt was made but failed */
509 if ((p_hcon->intr_cid = L2CA_ConnectReq (HID_PSM_INTERRUPT, hh_cb.devices[dhandle].addr)) == 0)
511 HIDH_TRACE_WARNING ("HID-Host INTR Originate failed");
512 reason = HID_L2CAP_REQ_FAIL ;
513 p_hcon->conn_state = HID_CONN_STATE_UNUSED;
514 hidh_conn_disconnect (dhandle);
515 hh_cb.callback( dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_CLOSE, reason, NULL ) ;
520 /* Transition to the next appropriate state, waiting for connection confirm on interrupt channel. */
521 p_hcon->conn_state = HID_CONN_STATE_CONNECTING_INTR;
526 p_hcon->conn_flags |= HID_CONN_FLAGS_HIS_INTR_CFG_DONE;
528 /* If all configuration is complete, change state and tell management we are up */
529 if (((p_hcon->conn_flags & HID_CONN_FLAGS_ALL_CONFIGURED) == HID_CONN_FLAGS_ALL_CONFIGURED)
530 && (p_hcon->conn_state == HID_CONN_STATE_CONFIG))
532 p_hcon->conn_state = HID_CONN_STATE_CONNECTED;
533 /* Reset disconnect reason to success, as connection successful */
534 p_hcon->disc_reason = HID_SUCCESS;
536 hh_cb.devices[dhandle].state = HID_DEV_CONNECTED;
537 hh_cb.callback( dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_OPEN, 0, NULL ) ;
542 /*******************************************************************************
544 ** Function hidh_l2cif_config_cfm
546 ** Description This function processes the L2CAP configuration confirmation
551 *******************************************************************************/
552 static void hidh_l2cif_config_cfm (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg)
555 tHID_CONN *p_hcon = NULL;
558 HIDH_TRACE_EVENT ("HID-Host Rcvd cfg cfm, CID: 0x%x Result: %d", l2cap_cid, p_cfg->result);
560 /* Find CCB based on CID */
561 if( (dhandle = find_conn_by_cid(l2cap_cid)) < HID_HOST_MAX_DEVICES )
562 p_hcon = &hh_cb.devices[dhandle].conn;
566 HIDH_TRACE_WARNING ("HID-Host Rcvd L2CAP cfg ind, unknown CID: 0x%x", l2cap_cid);
570 /* If configuration failed, disconnect the channel(s) */
571 if (p_cfg->result != L2CAP_CFG_OK)
573 hidh_conn_disconnect (dhandle);
574 reason = HID_L2CAP_CFG_FAIL | (UINT32) p_cfg->result ;
575 hh_cb.callback( dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_CLOSE, reason, NULL ) ;
579 if (l2cap_cid == p_hcon->ctrl_cid)
581 p_hcon->conn_flags |= HID_CONN_FLAGS_MY_CTRL_CFG_DONE;
582 if ((p_hcon->conn_flags & HID_CONN_FLAGS_IS_ORIG) &&
583 (p_hcon->conn_flags & HID_CONN_FLAGS_HIS_CTRL_CFG_DONE))
585 /* Connect interrupt channel */
586 p_hcon->disc_reason = HID_L2CAP_CONN_FAIL; /* Reset initial reason for CLOSE_EVT: Connection Attempt was made but failed */
587 if ((p_hcon->intr_cid = L2CA_ConnectReq (HID_PSM_INTERRUPT, hh_cb.devices[dhandle].addr)) == 0)
589 HIDH_TRACE_WARNING ("HID-Host INTR Originate failed");
590 reason = HID_L2CAP_REQ_FAIL ;
591 p_hcon->conn_state = HID_CONN_STATE_UNUSED;
592 hidh_conn_disconnect (dhandle);
593 hh_cb.callback( dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_CLOSE, reason, NULL ) ;
598 /* Transition to the next appropriate state, waiting for connection confirm on interrupt channel. */
599 p_hcon->conn_state = HID_CONN_STATE_CONNECTING_INTR;
604 p_hcon->conn_flags |= HID_CONN_FLAGS_MY_INTR_CFG_DONE;
606 /* If all configuration is complete, change state and tell management we are up */
607 if (((p_hcon->conn_flags & HID_CONN_FLAGS_ALL_CONFIGURED) == HID_CONN_FLAGS_ALL_CONFIGURED)
608 && (p_hcon->conn_state == HID_CONN_STATE_CONFIG))
610 p_hcon->conn_state = HID_CONN_STATE_CONNECTED;
611 /* Reset disconnect reason to success, as connection successful */
612 p_hcon->disc_reason = HID_SUCCESS;
614 hh_cb.devices[dhandle].state = HID_DEV_CONNECTED;
615 hh_cb.callback( dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_OPEN, 0, NULL ) ;
620 /*******************************************************************************
622 ** Function hidh_l2cif_disconnect_ind
624 ** Description This function handles a disconnect event from L2CAP. If
625 ** requested to, we ack the disconnect before dropping the CCB
629 *******************************************************************************/
630 static void hidh_l2cif_disconnect_ind (UINT16 l2cap_cid, BOOLEAN ack_needed)
633 tHID_CONN *p_hcon = NULL;
634 UINT16 disc_res = HCI_SUCCESS;
635 UINT16 hid_close_evt_reason;
637 /* Find CCB based on CID */
638 if( (dhandle = find_conn_by_cid(l2cap_cid)) < HID_HOST_MAX_DEVICES )
639 p_hcon = &hh_cb.devices[dhandle].conn;
643 HIDH_TRACE_WARNING ("HID-Host Rcvd L2CAP disc, unknown CID: 0x%x", l2cap_cid);
648 L2CA_DisconnectRsp (l2cap_cid);
650 HIDH_TRACE_EVENT ("HID-Host Rcvd L2CAP disc, CID: 0x%x", l2cap_cid);
652 p_hcon->conn_state = HID_CONN_STATE_DISCONNECTING;
654 if (l2cap_cid == p_hcon->ctrl_cid)
655 p_hcon->ctrl_cid = 0;
657 p_hcon->intr_cid = 0;
659 if ((p_hcon->ctrl_cid == 0) && (p_hcon->intr_cid == 0))
661 hh_cb.devices[dhandle].state = HID_DEV_NO_CONN;
662 p_hcon->conn_state = HID_CONN_STATE_UNUSED;
665 disc_res = btm_get_acl_disc_reason_code();
667 #if (HID_HOST_MAX_CONN_RETRY > 0)
668 if( (disc_res == HCI_ERR_CONNECTION_TOUT || disc_res == HCI_ERR_UNSPECIFIED) &&
669 (!(hh_cb.devices[dhandle].attr_mask & HID_RECONN_INIT)) &&
670 (hh_cb.devices[dhandle].attr_mask & HID_NORMALLY_CONNECTABLE))
672 hh_cb.devices[dhandle].conn_tries = 0;
673 period_ms_t interval_ms = HID_HOST_REPAGE_WIN * 1000;
674 alarm_set_on_queue(hh_cb.devices[dhandle].conn.process_repage_timer,
675 interval_ms, hidh_process_repage_timer_timeout,
676 UINT_TO_PTR(dhandle), btu_general_alarm_queue);
677 hh_cb.callback( dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_CLOSE, disc_res, NULL);
682 /* Set reason code for HID_HDEV_EVT_CLOSE */
683 hid_close_evt_reason = p_hcon->disc_reason;
685 /* If we got baseband sent HCI_DISCONNECT_COMPLETE_EVT due to security failure, then set reason to HID_ERR_AUTH_FAILED */
686 if ((disc_res == HCI_ERR_AUTH_FAILURE) ||
687 (disc_res == HCI_ERR_KEY_MISSING) ||
688 (disc_res == HCI_ERR_HOST_REJECT_SECURITY) ||
689 (disc_res == HCI_ERR_PAIRING_NOT_ALLOWED) ||
690 (disc_res == HCI_ERR_UNIT_KEY_USED) ||
691 (disc_res == HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED) ||
692 (disc_res == HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE) ||
693 (disc_res == HCI_ERR_REPEATED_ATTEMPTS))
695 hid_close_evt_reason = HID_ERR_AUTH_FAILED;
698 hh_cb.callback( dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_CLOSE, hid_close_evt_reason, NULL ) ;
704 /*******************************************************************************
706 ** Function hidh_l2cif_disconnect_cfm
708 ** Description This function handles a disconnect confirm event from L2CAP.
712 *******************************************************************************/
713 static void hidh_l2cif_disconnect_cfm (UINT16 l2cap_cid, UINT16 result)
716 tHID_CONN *p_hcon = NULL;
719 /* Find CCB based on CID */
720 if( (dhandle = find_conn_by_cid(l2cap_cid)) < HID_HOST_MAX_DEVICES )
721 p_hcon = &hh_cb.devices[dhandle].conn;
725 HIDH_TRACE_WARNING ("HID-Host Rcvd L2CAP disc cfm, unknown CID: 0x%x", l2cap_cid);
729 HIDH_TRACE_EVENT ("HID-Host Rcvd L2CAP disc cfm, CID: 0x%x", l2cap_cid);
731 if (l2cap_cid == p_hcon->ctrl_cid)
732 p_hcon->ctrl_cid = 0;
735 p_hcon->intr_cid = 0;
736 if (p_hcon->ctrl_cid)
738 HIDH_TRACE_EVENT ("HID-Host Initiating L2CAP Ctrl disconnection");
739 L2CA_DisconnectReq (p_hcon->ctrl_cid);
743 if ((p_hcon->ctrl_cid == 0) && (p_hcon->intr_cid == 0))
745 hh_cb.devices[dhandle].state = HID_DEV_NO_CONN;
746 p_hcon->conn_state = HID_CONN_STATE_UNUSED;
747 hh_cb.callback( dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_CLOSE, p_hcon->disc_reason, NULL ) ;
752 /*******************************************************************************
754 ** Function hidh_l2cif_cong_ind
756 ** Description This function handles a congestion status event from L2CAP.
760 *******************************************************************************/
761 static void hidh_l2cif_cong_ind (UINT16 l2cap_cid, BOOLEAN congested)
764 tHID_CONN *p_hcon = NULL;
766 /* Find CCB based on CID */
767 if( (dhandle = find_conn_by_cid(l2cap_cid)) < HID_HOST_MAX_DEVICES )
768 p_hcon = &hh_cb.devices[dhandle].conn;
772 HIDH_TRACE_WARNING ("HID-Host Rcvd L2CAP congestion status, unknown CID: 0x%x", l2cap_cid);
776 HIDH_TRACE_EVENT ("HID-Host Rcvd L2CAP congestion status, CID: 0x%x Cong: %d", l2cap_cid, congested);
779 p_hcon->conn_flags |= HID_CONN_FLAGS_CONGESTED;
782 p_hcon->conn_flags &= ~HID_CONN_FLAGS_CONGESTED;
788 /*******************************************************************************
790 ** Function hidh_l2cif_data_ind
792 ** Description This function is called when data is received from L2CAP.
793 ** if we are the originator of the connection, we are the SDP
794 ** client, and the received message is queued up for the client.
796 ** If we are the destination of the connection, we are the SDP
797 ** server, so the message is passed to the server processing
802 *******************************************************************************/
803 static void hidh_l2cif_data_ind (UINT16 l2cap_cid, BT_HDR *p_msg)
805 UINT8 *p_data = (UINT8 *)(p_msg + 1) + p_msg->offset;
806 UINT8 ttype, param, rep_type, evt;
808 tHID_CONN *p_hcon = NULL;
810 HIDH_TRACE_DEBUG ("HID-Host hidh_l2cif_data_ind [l2cap_cid=0x%04x]", l2cap_cid);
812 /* Find CCB based on CID */
813 if ((dhandle = find_conn_by_cid(l2cap_cid)) < HID_HOST_MAX_DEVICES)
814 p_hcon = &hh_cb.devices[dhandle].conn;
818 HIDH_TRACE_WARNING ("HID-Host Rcvd L2CAP data, unknown CID: 0x%x", l2cap_cid);
824 ttype = HID_GET_TRANS_FROM_HDR(*p_data);
825 param = HID_GET_PARAM_FROM_HDR(*p_data);
826 rep_type = param & HID_PAR_REP_TYPE_MASK;
829 /* Get rid of the data type */
835 case HID_TRANS_HANDSHAKE:
836 hh_cb.callback(dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_HANDSHAKE, param, NULL);
840 case HID_TRANS_CONTROL:
843 case HID_PAR_CONTROL_VIRTUAL_CABLE_UNPLUG:
844 hidh_conn_disconnect( dhandle ) ;
845 /* Device is unplugging from us. Tell USB */
846 hh_cb.callback(dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_VC_UNPLUG, 0, NULL);
857 evt = (hh_cb.devices[dhandle].conn.intr_cid == l2cap_cid) ?
858 HID_HDEV_EVT_INTR_DATA : HID_HDEV_EVT_CTRL_DATA;
859 hh_cb.callback(dhandle, hh_cb.devices[dhandle].addr, evt, rep_type, p_msg);
862 case HID_TRANS_DATAC:
863 evt = (hh_cb.devices[dhandle].conn.intr_cid == l2cap_cid) ?
864 HID_HDEV_EVT_INTR_DATC : HID_HDEV_EVT_CTRL_DATC;
865 hh_cb.callback(dhandle, hh_cb.devices[dhandle].addr, evt, rep_type, p_msg);
875 /*******************************************************************************
877 ** Function hidh_conn_snd_data
879 ** Description This function is sends out data.
881 ** Returns tHID_STATUS
883 *******************************************************************************/
884 tHID_STATUS hidh_conn_snd_data (UINT8 dhandle, UINT8 trans_type, UINT8 param,
885 UINT16 data, UINT8 report_id, BT_HDR *buf)
887 tHID_CONN *p_hcon = &hh_cb.devices[dhandle].conn;
891 BOOLEAN seg_req = FALSE;
896 BOOLEAN blank_datc = FALSE;
898 if (!BTM_IsAclConnectionUp(hh_cb.devices[dhandle].addr, BT_TRANSPORT_BR_EDR))
901 osi_freebuf ((void *)buf);
902 return( HID_ERR_NO_CONNECTION );
905 if (p_hcon->conn_flags & HID_CONN_FLAGS_CONGESTED)
908 osi_freebuf ((void *)buf);
909 return( HID_ERR_CONGESTED );
914 case HID_TRANS_CONTROL:
915 case HID_TRANS_GET_REPORT:
916 case HID_TRANS_SET_REPORT:
917 case HID_TRANS_GET_PROTOCOL:
918 case HID_TRANS_SET_PROTOCOL:
919 case HID_TRANS_GET_IDLE:
920 case HID_TRANS_SET_IDLE:
921 cid = p_hcon->ctrl_cid;
922 buf_size = HID_CONTROL_BUF_SIZE;
925 cid = p_hcon->intr_cid;
926 buf_size = HID_INTERRUPT_BUF_SIZE;
929 return (HID_ERR_INVALID_PARAM) ;
932 if( trans_type == HID_TRANS_SET_IDLE )
934 else if( (trans_type == HID_TRANS_GET_REPORT) && (param & 0x08) )
939 if ( buf == NULL || blank_datc )
941 p_buf = (BT_HDR *)osi_getbuf(buf_size);
943 return (HID_ERR_NO_RESOURCES);
945 p_buf->offset = L2CAP_MIN_OFFSET;
951 else if ( (buf->len > (p_hcon->rem_mtu_size - 1)))
953 p_buf = (BT_HDR *)osi_getbuf(buf_size);
955 return (HID_ERR_NO_RESOURCES);
957 p_buf->offset = L2CAP_MIN_OFFSET;
959 data_size = buf->len;
960 bytes_copied = p_hcon->rem_mtu_size - 1;
967 data_size = buf->len;
968 bytes_copied = buf->len;
971 p_out = (UINT8 *)(p_buf + 1) + p_buf->offset;
972 *p_out++ = HID_BUILD_HDR(trans_type, param);
974 /* If report ID required for this device */
975 if( (trans_type == HID_TRANS_GET_REPORT) && (report_id != 0) )
978 data_size = bytes_copied = 1;
984 memcpy (p_out, (((UINT8 *)(buf+1)) + buf->offset), bytes_copied);
985 buf->offset += bytes_copied;
986 buf->len -= bytes_copied;
988 else if( use_data == 1)
990 *(p_out+bytes_copied) = data & 0xff;
992 else if( use_data == 2 )
994 *(p_out+bytes_copied) = data & 0xff;
995 *(p_out+bytes_copied+1) = (data >> 8) & 0xff ;
998 p_buf->len = bytes_copied + 1 + use_data;
999 data_size -= bytes_copied;
1001 /* Send the buffer through L2CAP */
1002 if ((p_hcon->conn_flags & HID_CONN_FLAGS_CONGESTED) || (!L2CA_DataWrite (cid, p_buf)))
1003 return (HID_ERR_CONGESTED);
1006 trans_type = HID_TRANS_DATAC;
1007 else if( bytes_copied == (p_hcon->rem_mtu_size - 1) )
1009 trans_type = HID_TRANS_DATAC;
1013 } while ((data_size != 0) || blank_datc ) ;
1015 return (HID_SUCCESS);
1017 /*******************************************************************************
1019 ** Function hidh_conn_initiate
1021 ** Description This function is called by the management to create a connection.
1025 *******************************************************************************/
1026 tHID_STATUS hidh_conn_initiate (UINT8 dhandle)
1028 UINT8 service_id = BTM_SEC_SERVICE_HIDH_NOSEC_CTRL;
1029 UINT32 mx_chan_id = HID_NOSEC_CHN;
1031 tHID_HOST_DEV_CTB *p_dev = &hh_cb.devices[dhandle];
1033 if( p_dev->conn.conn_state != HID_CONN_STATE_UNUSED )
1034 return( HID_ERR_CONN_IN_PROCESS );
1036 p_dev->conn.ctrl_cid = 0;
1037 p_dev->conn.intr_cid = 0;
1038 p_dev->conn.disc_reason = HID_L2CAP_CONN_FAIL; /* Reset initial reason for CLOSE_EVT: Connection Attempt was made but failed */
1040 /* We are the originator of this connection */
1041 p_dev->conn.conn_flags = HID_CONN_FLAGS_IS_ORIG;
1043 if(p_dev->attr_mask & HID_SEC_REQUIRED)
1045 service_id = BTM_SEC_SERVICE_HIDH_SEC_CTRL;
1046 mx_chan_id = HID_SEC_CHN;
1048 BTM_SetOutService (p_dev->addr, service_id, mx_chan_id);
1050 /* Check if L2CAP started the connection process */
1051 if ((p_dev->conn.ctrl_cid = L2CA_ConnectReq (HID_PSM_CONTROL, p_dev->addr)) == 0)
1053 HIDH_TRACE_WARNING ("HID-Host Originate failed");
1054 hh_cb.callback( dhandle, hh_cb.devices[dhandle].addr, HID_HDEV_EVT_CLOSE,
1055 HID_ERR_L2CAP_FAILED, NULL ) ;
1059 /* Transition to the next appropriate state, waiting for connection confirm on control channel. */
1060 p_dev->conn.conn_state = HID_CONN_STATE_CONNECTING_CTRL;
1063 return( HID_SUCCESS );
1067 /*******************************************************************************
1069 ** Function find_conn_by_cid
1071 ** Description This function finds a connection control block based on CID
1073 ** Returns address of control block, or NULL if not found
1075 *******************************************************************************/
1076 static UINT8 find_conn_by_cid (UINT16 cid)
1080 for (xx = 0; xx < HID_HOST_MAX_DEVICES; xx++)
1082 if ((hh_cb.devices[xx].in_use) && (hh_cb.devices[xx].conn.conn_state != HID_CONN_STATE_UNUSED)
1083 && ((hh_cb.devices[xx].conn.ctrl_cid == cid) || (hh_cb.devices[xx].conn.intr_cid == cid)))
1090 void hidh_conn_dereg( void )
1092 L2CA_Deregister (HID_PSM_CONTROL);
1093 L2CA_Deregister (HID_PSM_INTERRUPT);
1096 /*******************************************************************************
1098 ** Function hidh_conn_retry
1100 ** Description This function is called to retry a failed connection.
1104 *******************************************************************************/
1105 static void hidh_conn_retry( UINT8 dhandle )
1107 tHID_HOST_DEV_CTB *p_dev = &hh_cb.devices[dhandle];
1109 p_dev->conn.conn_state = HID_CONN_STATE_UNUSED;
1110 #if (HID_HOST_REPAGE_WIN > 0)
1111 period_ms_t interval_ms = HID_HOST_REPAGE_WIN * 1000;
1112 alarm_set_on_queue(p_dev->conn.process_repage_timer,
1113 interval_ms, hidh_process_repage_timer_timeout,
1114 UINT_TO_PTR(dhandle), btu_general_alarm_queue);
1116 hidh_process_repage_process(dhandle);