1 /******************************************************************************
3 * Copyright 2016 The Android Open Source Project
4 * Copyright 2002-2012 Broadcom Corporation
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 ******************************************************************************/
20 /******************************************************************************
22 * this file contains the connection interface functions
24 ******************************************************************************/
45 #include "osi/include/osi.h"
47 static void hidd_l2cif_connect_ind(const RawAddress& bd_addr, uint16_t cid,
48 uint16_t psm, uint8_t id);
49 static void hidd_l2cif_connect_cfm(uint16_t cid, uint16_t result);
50 static void hidd_l2cif_config_ind(uint16_t cid, tL2CAP_CFG_INFO* p_cfg);
51 static void hidd_l2cif_config_cfm(uint16_t cid, tL2CAP_CFG_INFO* p_cfg);
52 static void hidd_l2cif_disconnect_ind(uint16_t cid, bool ack_needed);
53 static void hidd_l2cif_disconnect_cfm(uint16_t cid, uint16_t result);
54 static void hidd_l2cif_data_ind(uint16_t cid, BT_HDR* p_msg);
55 static void hidd_l2cif_cong_ind(uint16_t cid, bool congested);
57 static const tL2CAP_APPL_INFO dev_reg_info = {
58 hidd_l2cif_connect_ind,
59 hidd_l2cif_connect_cfm,
61 hidd_l2cif_config_ind,
62 hidd_l2cif_config_cfm,
63 hidd_l2cif_disconnect_ind,
64 hidd_l2cif_disconnect_cfm,
69 NULL /* tL2CA_CREDITS_RECEIVED_CB */};
71 /*******************************************************************************
73 * Function hidd_check_config_done
75 * Description Checks if connection is configured and callback can be fired
79 ******************************************************************************/
80 static void hidd_check_config_done() {
83 p_hcon = &hd_cb.device.conn;
85 if (((p_hcon->conn_flags & HID_CONN_FLAGS_ALL_CONFIGURED) ==
86 HID_CONN_FLAGS_ALL_CONFIGURED) &&
87 (p_hcon->conn_state == HID_CONN_STATE_CONFIG)) {
88 p_hcon->conn_state = HID_CONN_STATE_CONNECTED;
90 hd_cb.device.state = HIDD_DEV_CONNECTED;
92 hd_cb.callback(hd_cb.device.addr, HID_DHOST_EVT_OPEN, 0, NULL);
94 // send outstanding data on intr
95 if (hd_cb.pending_data) {
96 L2CA_DataWrite(p_hcon->intr_cid, hd_cb.pending_data);
97 hd_cb.pending_data = NULL;
102 /*******************************************************************************
104 * Function hidh_sec_check_complete_term
106 * Description HID security check complete callback function.
108 * Returns Send L2CA_ConnectRsp OK if secutiry check succeed; otherwise
109 * send security block L2C connection response.
111 ******************************************************************************/
112 static void hidd_sec_check_complete(UNUSED_ATTR const RawAddress* bd_addr,
113 UNUSED_ATTR tBT_TRANSPORT transport,
114 void* p_ref_data, uint8_t res) {
115 tHID_DEV_DEV_CTB* p_dev = (tHID_DEV_DEV_CTB*)p_ref_data;
117 if (res == BTM_SUCCESS && p_dev->conn.conn_state == HID_CONN_STATE_SECURITY) {
118 p_dev->conn.disc_reason = HID_SUCCESS;
119 p_dev->conn.conn_state = HID_CONN_STATE_CONNECTING_INTR;
121 L2CA_ConnectRsp(p_dev->addr, p_dev->conn.ctrl_id, p_dev->conn.ctrl_cid,
122 L2CAP_CONN_OK, L2CAP_CONN_OK);
123 L2CA_ConfigReq(p_dev->conn.ctrl_cid, &hd_cb.l2cap_cfg);
124 } else if (res != BTM_SUCCESS) {
125 HIDD_TRACE_WARNING("%s: connection rejected by security", __func__);
127 p_dev->conn.disc_reason = HID_ERR_AUTH_FAILED;
128 p_dev->conn.conn_state = HID_CONN_STATE_UNUSED;
129 L2CA_ConnectRsp(p_dev->addr, p_dev->conn.ctrl_id, p_dev->conn.ctrl_cid,
130 L2CAP_CONN_SECURITY_BLOCK, L2CAP_CONN_OK);
135 /*******************************************************************************
137 * Function hidd_sec_check_complete_orig
139 * Description HID security check complete callback function (device
144 ******************************************************************************/
145 void hidd_sec_check_complete_orig(UNUSED_ATTR const RawAddress* bd_addr,
146 UNUSED_ATTR tBT_TRANSPORT transport,
147 void* p_ref_data, uint8_t res) {
148 tHID_DEV_DEV_CTB* p_dev = (tHID_DEV_DEV_CTB*)p_ref_data;
150 if (p_dev->conn.conn_state != HID_CONN_STATE_SECURITY) {
151 HIDD_TRACE_WARNING("%s: invalid state (%02x)", __func__,
152 p_dev->conn.conn_state);
156 if (res == BTM_SUCCESS) {
157 HIDD_TRACE_EVENT("%s: security ok", __func__);
158 p_dev->conn.disc_reason = HID_SUCCESS;
160 p_dev->conn.conn_state = HID_CONN_STATE_CONFIG;
161 L2CA_ConfigReq(p_dev->conn.ctrl_cid, &hd_cb.l2cap_cfg);
163 HIDD_TRACE_WARNING("%s: security check failed (%02x)", __func__, res);
164 p_dev->conn.disc_reason = HID_ERR_AUTH_FAILED;
165 hidd_conn_disconnect();
169 /*******************************************************************************
171 * Function hidd_l2cif_connect_ind
173 * Description Handles incoming L2CAP connection (we act as server)
177 ******************************************************************************/
178 static void hidd_l2cif_connect_ind(const RawAddress& bd_addr, uint16_t cid,
179 uint16_t psm, uint8_t id) {
181 tHID_DEV_DEV_CTB* p_dev;
182 bool accept = TRUE; // accept by default
184 HIDD_TRACE_EVENT("%s: psm=%04x cid=%04x id=%02x", __func__, psm, cid, id);
186 p_dev = &hd_cb.device;
188 if (!hd_cb.allow_incoming) {
189 HIDD_TRACE_WARNING("%s: incoming connections not allowed, rejecting",
191 L2CA_ConnectRsp(bd_addr, id, cid, L2CAP_CONN_NO_RESOURCES, 0);
195 p_hcon = &hd_cb.device.conn;
198 case HID_PSM_INTERRUPT:
199 if (p_hcon->ctrl_cid == 0) {
201 HIDD_TRACE_WARNING("%s: incoming INTR without CTRL, rejecting",
205 if (p_hcon->conn_state != HID_CONN_STATE_CONNECTING_INTR) {
207 HIDD_TRACE_WARNING("%s: incoming INTR in invalid state (%d), rejecting",
208 __func__, p_hcon->conn_state);
213 case HID_PSM_CONTROL:
214 if (p_hcon->conn_state != HID_CONN_STATE_UNUSED) {
216 HIDD_TRACE_WARNING("%s: incoming CTRL in invalid state (%d), rejecting",
217 __func__, p_hcon->conn_state);
224 HIDD_TRACE_ERROR("%s: received invalid PSM, rejecting", __func__);
229 L2CA_ConnectRsp(bd_addr, id, cid, L2CAP_CONN_NO_RESOURCES, 0);
233 // for CTRL we need to go through security and we reply in callback from there
234 if (psm == HID_PSM_CONTROL) {
235 // We are ready to accept connection from this device, since we aren't
236 // connected to anything and are in the correct state.
237 p_dev->in_use = TRUE;
238 p_dev->addr = bd_addr;
239 p_dev->state = HIDD_DEV_NO_CONN;
241 p_hcon->conn_flags = 0;
242 p_hcon->ctrl_cid = cid;
243 p_hcon->ctrl_id = id;
244 p_hcon->disc_reason = HID_L2CAP_CONN_FAIL;
246 p_hcon->conn_state = HID_CONN_STATE_SECURITY;
247 if (btm_sec_mx_access_request(p_dev->addr, HID_PSM_CONTROL, FALSE,
248 BTM_SEC_PROTO_HID, HIDD_NOSEC_CHN,
249 &hidd_sec_check_complete,
250 p_dev) == BTM_CMD_STARTED) {
251 L2CA_ConnectRsp(bd_addr, id, cid, L2CAP_CONN_PENDING, L2CAP_CONN_OK);
257 // for INTR we go directly to config state
258 p_hcon->conn_state = HID_CONN_STATE_CONFIG;
259 p_hcon->intr_cid = cid;
261 L2CA_ConnectRsp(bd_addr, id, cid, L2CAP_CONN_OK, L2CAP_CONN_OK);
262 L2CA_ConfigReq(cid, &hd_cb.l2cap_intr_cfg);
265 /*******************************************************************************
267 * Function hidd_l2cif_connect_cfm
269 * Description Handles L2CAP connection response (we act as client)
273 ******************************************************************************/
274 static void hidd_l2cif_connect_cfm(uint16_t cid, uint16_t result) {
275 tHID_DEV_DEV_CTB* p_dev = &hd_cb.device;
276 tHID_CONN* p_hcon = &hd_cb.device.conn;
278 HIDD_TRACE_EVENT("%s: cid=%04x result=%d", __func__, cid, result);
280 if (p_hcon->ctrl_cid != cid && p_hcon->intr_cid != cid) {
281 HIDD_TRACE_WARNING("%s: unknown cid", __func__);
285 if (!(p_hcon->conn_flags & HID_CONN_FLAGS_IS_ORIG) ||
286 ((cid == p_hcon->ctrl_cid) &&
287 (p_hcon->conn_state != HID_CONN_STATE_CONNECTING_CTRL)) ||
288 ((cid == p_hcon->intr_cid) &&
289 (p_hcon->conn_state != HID_CONN_STATE_CONNECTING_INTR))) {
290 HIDD_TRACE_WARNING("%s: unexpected", __func__);
294 if (result != L2CAP_CONN_OK) {
295 HIDD_TRACE_WARNING("%s: connection failed, now disconnect", __func__);
297 if (cid == p_hcon->ctrl_cid)
298 p_hcon->ctrl_cid = 0;
300 p_hcon->intr_cid = 0;
302 hidd_conn_disconnect();
304 hd_cb.callback(hd_cb.device.addr, HID_DHOST_EVT_CLOSE,
305 HID_L2CAP_CONN_FAIL | (uint32_t)result, NULL);
309 /* CTRL connect conf */
310 if (cid == p_hcon->ctrl_cid) {
311 p_hcon->conn_state = HID_CONN_STATE_SECURITY;
312 p_hcon->disc_reason =
313 HID_L2CAP_CONN_FAIL; /* in case disconnected before sec completed */
315 btm_sec_mx_access_request(p_dev->addr, HID_PSM_CONTROL, TRUE,
316 BTM_SEC_PROTO_HID, HIDD_SEC_CHN,
317 &hidd_sec_check_complete_orig, p_dev);
319 p_hcon->conn_state = HID_CONN_STATE_CONFIG;
320 L2CA_ConfigReq(cid, &hd_cb.l2cap_intr_cfg);
326 /*******************************************************************************
328 * Function hidd_l2cif_config_ind
330 * Description Handles incoming L2CAP configuration request
334 ******************************************************************************/
335 static void hidd_l2cif_config_ind(uint16_t cid, tL2CAP_CFG_INFO* p_cfg) {
338 HIDD_TRACE_EVENT("%s: cid=%04x", __func__, cid);
340 p_hcon = &hd_cb.device.conn;
342 if (p_hcon->ctrl_cid != cid && p_hcon->intr_cid != cid) {
343 HIDD_TRACE_WARNING("%s: unknown cid", __func__);
347 if ((!p_cfg->mtu_present) || (p_cfg->mtu > HID_DEV_MTU_SIZE))
348 p_hcon->rem_mtu_size = HID_DEV_MTU_SIZE;
350 p_hcon->rem_mtu_size = p_cfg->mtu;
352 // accept without changes
353 p_cfg->flush_to_present = FALSE;
354 p_cfg->mtu_present = FALSE;
355 p_cfg->result = L2CAP_CFG_OK;
357 if (cid == p_hcon->intr_cid && hd_cb.use_in_qos && !p_cfg->qos_present) {
358 p_cfg->qos_present = TRUE;
359 memcpy(&p_cfg->qos, &hd_cb.in_qos, sizeof(FLOW_SPEC));
362 L2CA_ConfigRsp(cid, p_cfg);
365 if (cid == p_hcon->ctrl_cid) {
366 p_hcon->conn_flags |= HID_CONN_FLAGS_HIS_CTRL_CFG_DONE;
368 if ((p_hcon->conn_flags & HID_CONN_FLAGS_IS_ORIG) &&
369 (p_hcon->conn_flags & HID_CONN_FLAGS_MY_CTRL_CFG_DONE)) {
370 p_hcon->disc_reason = HID_L2CAP_CONN_FAIL;
371 if ((p_hcon->intr_cid =
372 L2CA_ConnectReq(HID_PSM_INTERRUPT, hd_cb.device.addr)) == 0) {
373 hidd_conn_disconnect();
374 p_hcon->conn_state = HID_CONN_STATE_UNUSED;
376 HIDD_TRACE_WARNING("%s: could not start L2CAP connection for INTR",
378 hd_cb.callback(hd_cb.device.addr, HID_DHOST_EVT_CLOSE,
379 HID_ERR_L2CAP_FAILED, NULL);
382 p_hcon->conn_state = HID_CONN_STATE_CONNECTING_INTR;
386 p_hcon->conn_flags |= HID_CONN_FLAGS_HIS_INTR_CFG_DONE;
389 hidd_check_config_done();
392 /*******************************************************************************
394 * Function hidd_l2cif_config_cfm
396 * Description Handles incoming L2CAP configuration response
400 ******************************************************************************/
401 static void hidd_l2cif_config_cfm(uint16_t cid, tL2CAP_CFG_INFO* p_cfg) {
405 HIDD_TRACE_EVENT("%s: cid=%04x pcfg->result=%d", __func__, cid,
408 p_hcon = &hd_cb.device.conn;
410 if (p_hcon->ctrl_cid != cid && p_hcon->intr_cid != cid) {
411 HIDD_TRACE_WARNING("%s: unknown cid", __func__);
415 if (p_hcon->intr_cid == cid &&
416 p_cfg->result == L2CAP_CFG_UNACCEPTABLE_PARAMS && p_cfg->qos_present) {
417 tL2CAP_CFG_INFO new_qos;
419 // QoS parameters not accepted for intr, try again with host proposal
421 memcpy(&new_qos, &hd_cb.l2cap_intr_cfg, sizeof(new_qos));
422 memcpy(&new_qos.qos, &p_cfg->qos, sizeof(FLOW_SPEC));
423 new_qos.qos_present = TRUE;
425 HIDD_TRACE_WARNING("%s: config failed, retry", __func__);
427 L2CA_ConfigReq(cid, &new_qos);
429 } else if (p_hcon->intr_cid == cid &&
430 p_cfg->result == L2CAP_CFG_UNKNOWN_OPTIONS) {
431 // QoS not understood by remote device, try configuring without QoS
433 HIDD_TRACE_WARNING("%s: config failed, retry without QoS", __func__);
435 L2CA_ConfigReq(cid, &hd_cb.l2cap_cfg);
437 } else if (p_cfg->result != L2CAP_CFG_OK) {
438 HIDD_TRACE_WARNING("%s: config failed, disconnecting", __func__);
440 hidd_conn_disconnect();
441 reason = HID_L2CAP_CFG_FAIL | (uint32_t)p_cfg->result;
443 hd_cb.callback(hd_cb.device.addr, HID_DHOST_EVT_CLOSE, reason, NULL);
448 if (cid == p_hcon->ctrl_cid) {
449 p_hcon->conn_flags |= HID_CONN_FLAGS_MY_CTRL_CFG_DONE;
451 if ((p_hcon->conn_flags & HID_CONN_FLAGS_IS_ORIG) &&
452 (p_hcon->conn_flags & HID_CONN_FLAGS_HIS_CTRL_CFG_DONE)) {
453 p_hcon->disc_reason = HID_L2CAP_CONN_FAIL;
454 if ((p_hcon->intr_cid =
455 L2CA_ConnectReq(HID_PSM_INTERRUPT, hd_cb.device.addr)) == 0) {
456 hidd_conn_disconnect();
457 p_hcon->conn_state = HID_CONN_STATE_UNUSED;
459 HIDD_TRACE_WARNING("%s: could not start L2CAP connection for INTR",
461 hd_cb.callback(hd_cb.device.addr, HID_DHOST_EVT_CLOSE,
462 HID_ERR_L2CAP_FAILED, NULL);
465 p_hcon->conn_state = HID_CONN_STATE_CONNECTING_INTR;
469 p_hcon->conn_flags |= HID_CONN_FLAGS_MY_INTR_CFG_DONE;
472 hidd_check_config_done();
475 /*******************************************************************************
477 * Function hidd_l2cif_disconnect_ind
479 * Description Handler incoming L2CAP disconnection request
483 ******************************************************************************/
484 static void hidd_l2cif_disconnect_ind(uint16_t cid, bool ack_needed) {
487 HIDD_TRACE_EVENT("%s: cid=%04x ack_needed=%d", __func__, cid, ack_needed);
489 p_hcon = &hd_cb.device.conn;
491 if (p_hcon->conn_state == HID_CONN_STATE_UNUSED ||
492 (p_hcon->ctrl_cid != cid && p_hcon->intr_cid != cid)) {
493 HIDD_TRACE_WARNING("%s: unknown cid", __func__);
497 if (ack_needed) L2CA_DisconnectRsp(cid);
499 p_hcon->conn_state = HID_CONN_STATE_DISCONNECTING;
501 if (cid == p_hcon->ctrl_cid)
502 p_hcon->ctrl_cid = 0;
504 p_hcon->intr_cid = 0;
506 if ((p_hcon->ctrl_cid == 0) && (p_hcon->intr_cid == 0)) {
507 HIDD_TRACE_EVENT("%s: INTR and CTRL disconnected", __func__);
509 // clean any outstanding data on intr
510 if (hd_cb.pending_data) {
511 osi_free(hd_cb.pending_data);
512 hd_cb.pending_data = NULL;
515 hd_cb.device.state = HIDD_DEV_NO_CONN;
516 p_hcon->conn_state = HID_CONN_STATE_UNUSED;
518 hd_cb.callback(hd_cb.device.addr, HID_DHOST_EVT_CLOSE, p_hcon->disc_reason,
523 /*******************************************************************************
525 * Function hidd_l2cif_disconnect_cfm
527 * Description Handles L2CAP disconection response
531 ******************************************************************************/
532 static void hidd_l2cif_disconnect_cfm(uint16_t cid, uint16_t result) {
535 HIDD_TRACE_EVENT("%s: cid=%04x result=%d", __func__, cid, result);
537 p_hcon = &hd_cb.device.conn;
539 if (p_hcon->conn_state == HID_CONN_STATE_UNUSED ||
540 (p_hcon->ctrl_cid != cid && p_hcon->intr_cid != cid)) {
541 HIDD_TRACE_WARNING("%s: unknown cid", __func__);
545 if (cid == p_hcon->ctrl_cid) {
546 p_hcon->ctrl_cid = 0;
548 p_hcon->intr_cid = 0;
550 // now disconnect CTRL
551 L2CA_DisconnectReq(p_hcon->ctrl_cid);
554 if ((p_hcon->ctrl_cid == 0) && (p_hcon->intr_cid == 0)) {
555 HIDD_TRACE_EVENT("%s: INTR and CTRL disconnected", __func__);
557 hd_cb.device.state = HIDD_DEV_NO_CONN;
558 p_hcon->conn_state = HID_CONN_STATE_UNUSED;
560 if (hd_cb.pending_vc_unplug) {
561 hd_cb.callback(hd_cb.device.addr, HID_DHOST_EVT_VC_UNPLUG,
562 p_hcon->disc_reason, NULL);
563 hd_cb.pending_vc_unplug = FALSE;
565 hd_cb.callback(hd_cb.device.addr, HID_DHOST_EVT_CLOSE,
566 p_hcon->disc_reason, NULL);
571 /*******************************************************************************
573 * Function hidd_l2cif_cong_ind
575 * Description Handles L2CAP congestion status event
579 ******************************************************************************/
580 static void hidd_l2cif_cong_ind(uint16_t cid, bool congested) {
583 HIDD_TRACE_EVENT("%s: cid=%04x congested=%d", __func__, cid, congested);
585 p_hcon = &hd_cb.device.conn;
587 if (p_hcon->conn_state == HID_CONN_STATE_UNUSED ||
588 (p_hcon->ctrl_cid != cid && p_hcon->intr_cid != cid)) {
589 HIDD_TRACE_WARNING("%s: unknown cid", __func__);
594 p_hcon->conn_flags |= HID_CONN_FLAGS_CONGESTED;
596 p_hcon->conn_flags &= ~HID_CONN_FLAGS_CONGESTED;
600 /*******************************************************************************
602 * Function hidd_l2cif_data_ind
604 * Description Handler incoming data on L2CAP channel
608 ******************************************************************************/
609 static void hidd_l2cif_data_ind(uint16_t cid, BT_HDR* p_msg) {
611 uint8_t* p_data = (uint8_t*)(p_msg + 1) + p_msg->offset;
612 uint8_t msg_type, param;
615 HIDD_TRACE_EVENT("%s: cid=%04x", __func__, cid);
617 if (p_msg->len < 1) {
618 HIDD_TRACE_ERROR("Invalid data length, ignore");
623 p_hcon = &hd_cb.device.conn;
625 if (p_hcon->conn_state == HID_CONN_STATE_UNUSED ||
626 (p_hcon->ctrl_cid != cid && p_hcon->intr_cid != cid)) {
627 HIDD_TRACE_WARNING("%s: unknown cid", __func__);
632 msg_type = HID_GET_TRANS_FROM_HDR(*p_data);
633 param = HID_GET_PARAM_FROM_HDR(*p_data);
635 if (msg_type == HID_TRANS_DATA && cid == p_hcon->intr_cid) {
640 hd_cb.callback(hd_cb.device.addr, HID_DHOST_EVT_INTR_DATA, 0, p_msg);
645 case HID_TRANS_GET_REPORT:
646 // at this stage we don't know if Report Id shall be included in request
647 // so we pass complete packet in callback and let other code analyze this
648 hd_cb.callback(hd_cb.device.addr, HID_DHOST_EVT_GET_REPORT,
649 !!(param & HID_PAR_GET_REP_BUFSIZE_FOLLOWS), p_msg);
652 case HID_TRANS_SET_REPORT:
654 hd_cb.callback(hd_cb.device.addr, HID_DHOST_EVT_SET_REPORT, 0, p_msg);
657 case HID_TRANS_GET_IDLE:
658 hidd_conn_send_data(HID_CHANNEL_CTRL, HID_TRANS_DATA,
659 HID_PAR_REP_TYPE_OTHER, hd_cb.device.idle_time, 0,
664 case HID_TRANS_SET_IDLE:
665 if (p_msg->len != 2) {
666 HIDD_TRACE_ERROR("%s: invalid len (%d) set idle request received",
667 __func__, p_msg->len);
670 hd_cb.device.idle_time = p_data[1];
671 HIDD_TRACE_DEBUG("%s: idle_time = %d", __func__,
672 hd_cb.device.idle_time);
673 if (hd_cb.device.idle_time) {
675 "%s: idle_time of %d ms not supported by HID Device", __func__,
676 (hd_cb.device.idle_time * 4));
681 hidd_conn_send_data(0, HID_TRANS_HANDSHAKE,
682 HID_PAR_HANDSHAKE_RSP_SUCCESS, 0, 0, NULL);
684 hidd_conn_send_data(0, HID_TRANS_HANDSHAKE,
685 HID_PAR_HANDSHAKE_RSP_ERR_INVALID_PARAM, 0, 0,
691 case HID_TRANS_GET_PROTOCOL:
692 hidd_conn_send_data(HID_CHANNEL_CTRL, HID_TRANS_DATA,
693 HID_PAR_REP_TYPE_OTHER, !hd_cb.device.boot_mode, 0,
698 case HID_TRANS_SET_PROTOCOL:
699 hd_cb.device.boot_mode = !(param & HID_PAR_PROTOCOL_MASK);
700 hd_cb.callback(hd_cb.device.addr, HID_DHOST_EVT_SET_PROTOCOL,
701 param & HID_PAR_PROTOCOL_MASK, NULL);
702 hidd_conn_send_data(0, HID_TRANS_HANDSHAKE, HID_PAR_HANDSHAKE_RSP_SUCCESS,
707 case HID_TRANS_CONTROL:
709 case HID_PAR_CONTROL_SUSPEND:
710 hd_cb.callback(hd_cb.device.addr, HID_DHOST_EVT_SUSPEND, 0, NULL);
713 case HID_PAR_CONTROL_EXIT_SUSPEND:
714 hd_cb.callback(hd_cb.device.addr, HID_DHOST_EVT_EXIT_SUSPEND, 0,
718 case HID_PAR_CONTROL_VIRTUAL_CABLE_UNPLUG:
719 hidd_conn_disconnect();
721 // set flag so we can notify properly when disconnected
722 hd_cb.pending_vc_unplug = TRUE;
731 HIDD_TRACE_WARNING("%s: got unsupported msg (%d)", __func__, msg_type);
732 hidd_conn_send_data(0, HID_TRANS_HANDSHAKE,
733 HID_PAR_HANDSHAKE_RSP_ERR_UNSUPPORTED_REQ, 0, 0,
740 /*******************************************************************************
742 * Function hidd_conn_reg
744 * Description Registers L2CAP channels
748 ******************************************************************************/
749 tHID_STATUS hidd_conn_reg(void) {
750 HIDD_TRACE_API("%s", __func__);
752 memset(&hd_cb.l2cap_cfg, 0, sizeof(tL2CAP_CFG_INFO));
754 hd_cb.l2cap_cfg.mtu_present = TRUE;
755 hd_cb.l2cap_cfg.mtu = HID_DEV_MTU_SIZE;
756 hd_cb.l2cap_cfg.flush_to_present = TRUE;
757 hd_cb.l2cap_cfg.flush_to = HID_DEV_FLUSH_TO;
759 memset(&hd_cb.l2cap_intr_cfg, 0, sizeof(tL2CAP_CFG_INFO));
760 hd_cb.l2cap_intr_cfg.mtu_present = TRUE;
761 hd_cb.l2cap_intr_cfg.mtu = HID_DEV_MTU_SIZE;
762 hd_cb.l2cap_intr_cfg.flush_to_present = TRUE;
763 hd_cb.l2cap_intr_cfg.flush_to = HID_DEV_FLUSH_TO;
765 if (!L2CA_Register(HID_PSM_CONTROL, (tL2CAP_APPL_INFO*)&dev_reg_info,
766 false /* enable_snoop */)) {
767 HIDD_TRACE_ERROR("HID Control (device) registration failed");
768 return (HID_ERR_L2CAP_FAILED);
771 if (!L2CA_Register(HID_PSM_INTERRUPT, (tL2CAP_APPL_INFO*)&dev_reg_info,
772 false /* enable_snoop */)) {
773 L2CA_Deregister(HID_PSM_CONTROL);
774 HIDD_TRACE_ERROR("HID Interrupt (device) registration failed");
775 return (HID_ERR_L2CAP_FAILED);
778 return (HID_SUCCESS);
781 /*******************************************************************************
783 * Function hidd_conn_dereg
785 * Description Deregisters L2CAP channels
789 ******************************************************************************/
790 void hidd_conn_dereg(void) {
791 HIDD_TRACE_API("%s", __func__);
793 L2CA_Deregister(HID_PSM_CONTROL);
794 L2CA_Deregister(HID_PSM_INTERRUPT);
797 /*******************************************************************************
799 * Function hidd_conn_initiate
801 * Description Initiates HID connection to plugged device
803 * Returns HID_SUCCESS
805 ******************************************************************************/
806 tHID_STATUS hidd_conn_initiate(void) {
807 tHID_DEV_DEV_CTB* p_dev = &hd_cb.device;
809 HIDD_TRACE_API("%s", __func__);
811 if (!p_dev->in_use) {
812 HIDD_TRACE_WARNING("%s: no virtual cable established", __func__);
813 return (HID_ERR_NOT_REGISTERED);
816 if (p_dev->conn.conn_state != HID_CONN_STATE_UNUSED) {
817 HIDD_TRACE_WARNING("%s: connection already in progress", __func__);
818 return (HID_ERR_CONN_IN_PROCESS);
821 p_dev->conn.ctrl_cid = 0;
822 p_dev->conn.intr_cid = 0;
823 p_dev->conn.disc_reason = HID_L2CAP_CONN_FAIL;
825 p_dev->conn.conn_flags = HID_CONN_FLAGS_IS_ORIG;
827 BTM_SetOutService(p_dev->addr, BTM_SEC_SERVICE_HIDD_SEC_CTRL, HIDD_SEC_CHN);
829 /* Check if L2CAP started the connection process */
830 if ((p_dev->conn.ctrl_cid = L2CA_ConnectReq(HID_PSM_CONTROL, p_dev->addr)) ==
832 HIDD_TRACE_WARNING("%s: could not start L2CAP connection", __func__);
833 hd_cb.callback(hd_cb.device.addr, HID_DHOST_EVT_CLOSE, HID_ERR_L2CAP_FAILED,
836 p_dev->conn.conn_state = HID_CONN_STATE_CONNECTING_CTRL;
839 return (HID_SUCCESS);
842 /*******************************************************************************
844 * Function hidd_conn_disconnect
846 * Description Disconnects existing HID connection
848 * Returns HID_SUCCESS
850 ******************************************************************************/
851 tHID_STATUS hidd_conn_disconnect(void) {
854 HIDD_TRACE_API("%s", __func__);
856 // clean any outstanding data on intr
857 if (hd_cb.pending_data) {
858 osi_free(hd_cb.pending_data);
859 hd_cb.pending_data = NULL;
862 p_hcon = &hd_cb.device.conn;
864 if ((p_hcon->ctrl_cid != 0) || (p_hcon->intr_cid != 0)) {
865 p_hcon->conn_state = HID_CONN_STATE_DISCONNECTING;
867 /* Set l2cap idle timeout to 0 (so ACL link is disconnected
868 * immediately after last channel is closed) */
869 L2CA_SetIdleTimeoutByBdAddr(hd_cb.device.addr, 0, BT_TRANSPORT_BR_EDR);
871 if (p_hcon->intr_cid) {
872 L2CA_DisconnectReq(p_hcon->intr_cid);
873 } else if (p_hcon->ctrl_cid) {
874 L2CA_DisconnectReq(p_hcon->ctrl_cid);
877 HIDD_TRACE_WARNING("%s: already disconnected", __func__);
878 p_hcon->conn_state = HID_CONN_STATE_UNUSED;
881 return (HID_SUCCESS);
884 /*******************************************************************************
886 * Function hidd_conn_send_data
888 * Description Sends data to host
890 * Returns tHID_STATUS
892 ******************************************************************************/
893 tHID_STATUS hidd_conn_send_data(uint8_t channel, uint8_t msg_type,
894 uint8_t param, uint8_t data, uint16_t len,
902 HIDD_TRACE_VERBOSE("%s: channel(%d), msg_type(%d), len(%d)", __func__,
903 channel, msg_type, len);
905 p_hcon = &hd_cb.device.conn;
907 if (p_hcon->conn_flags & HID_CONN_FLAGS_CONGESTED) {
908 return HID_ERR_CONGESTED;
912 case HID_TRANS_HANDSHAKE:
913 case HID_TRANS_CONTROL:
914 cid = p_hcon->ctrl_cid;
915 buf_size = HID_CONTROL_BUF_SIZE;
918 if (channel == HID_CHANNEL_CTRL) {
919 cid = p_hcon->ctrl_cid;
920 buf_size = HID_CONTROL_BUF_SIZE;
922 cid = p_hcon->intr_cid;
923 buf_size = HID_INTERRUPT_BUF_SIZE;
927 return (HID_ERR_INVALID_PARAM);
930 p_buf = (BT_HDR*)osi_malloc(buf_size);
931 if (p_buf == NULL) return (HID_ERR_NO_RESOURCES);
933 p_buf->offset = L2CAP_MIN_OFFSET;
935 p_out = (uint8_t*)(p_buf + 1) + p_buf->offset;
937 *p_out = HID_BUILD_HDR(msg_type, param);
940 p_buf->len = 1; // start with header only
942 // add report id prefix only if non-zero (which is reserved)
943 if (msg_type == HID_TRANS_DATA && (data || param == HID_PAR_REP_TYPE_OTHER)) {
944 *p_out = data; // report_id
949 if (len > 0 && p_data != NULL) {
950 memcpy(p_out, p_data, len);
954 // check if connected
955 if (hd_cb.device.state != HIDD_DEV_CONNECTED) {
956 // for DATA on intr we hold transfer and try to reconnect
957 if (msg_type == HID_TRANS_DATA && cid == p_hcon->intr_cid) {
958 // drop previous data, we do not queue it for now
959 if (hd_cb.pending_data) {
960 osi_free(hd_cb.pending_data);
963 hd_cb.pending_data = p_buf;
965 if (hd_cb.device.conn.conn_state == HID_CONN_STATE_UNUSED) {
966 hidd_conn_initiate();
972 return HID_ERR_NO_CONNECTION;
975 #ifdef REPORT_TRANSFER_TIMESTAMP
976 if (report_transfer) {
977 HIDD_TRACE_ERROR("%s: report sent", __func__);
980 HIDD_TRACE_VERBOSE("%s: report sent", __func__);
982 if (!L2CA_DataWrite(cid, p_buf)) return (HID_ERR_CONGESTED);
984 return (HID_SUCCESS);