1 /******************************************************************************
3 * Copyright (C) 2009-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 is the implementation file for the MCAP Control Channel Action
24 ******************************************************************************/
26 #include "bt_target.h"
28 #include "bt_common.h"
36 /*****************************************************************************
38 *****************************************************************************/
39 /*******************************************************************************
41 ** Function mca_ccb_rsp_tout
43 ** Description This function processes the response timeout.
47 *******************************************************************************/
48 void mca_ccb_rsp_tout(tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data)
53 mca_ccb_report_event(p_ccb, MCA_RSP_TOUT_IND_EVT, &evt_data);
56 /*******************************************************************************
58 ** Function mca_ccb_report_event
60 ** Description This function reports the given event.
64 *******************************************************************************/
65 void mca_ccb_report_event(tMCA_CCB *p_ccb, UINT8 event, tMCA_CTRL *p_data)
67 if (p_ccb && p_ccb->p_rcb && p_ccb->p_rcb->p_cback)
68 (*p_ccb->p_rcb->p_cback)(mca_rcb_to_handle(p_ccb->p_rcb), mca_ccb_to_hdl(p_ccb), event, p_data);
71 /*******************************************************************************
73 ** Function mca_ccb_free_msg
75 ** Description This function frees the received message.
79 *******************************************************************************/
80 void mca_ccb_free_msg(tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data)
87 /*******************************************************************************
89 ** Function mca_ccb_snd_req
91 ** Description This function builds a request and sends it to the peer.
95 *******************************************************************************/
96 void mca_ccb_snd_req(tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data)
98 tMCA_CCB_MSG *p_msg = (tMCA_CCB_MSG *)p_data;
101 BOOLEAN is_abort = FALSE;
104 MCA_TRACE_DEBUG ("mca_ccb_snd_req cong=%d req=%d", p_ccb->cong, p_msg->op_code);
105 /* check for abort request */
106 if ((p_ccb->status == MCA_CCB_STAT_PENDING) && (p_msg->op_code == MCA_OP_MDL_ABORT_REQ))
108 p_dcb = mca_dcb_by_hdl(p_ccb->p_tx_req->dcb_idx);
109 /* the Abort API does not have the associated mdl_id.
110 * Get the mdl_id in dcb to compose the request */
111 p_msg->mdl_id = p_dcb->mdl_id;
112 mca_dcb_event(p_dcb, MCA_DCB_API_CLOSE_EVT, NULL);
113 mca_free_buf ((void **)&p_ccb->p_tx_req);
114 p_ccb->status = MCA_CCB_STAT_NORM;
118 /* no pending outgoing messages or it's an abort request for a pending data channel */
119 if ((!p_ccb->p_tx_req) || is_abort)
121 p_ccb->p_tx_req = p_msg;
124 p_pkt = (BT_HDR *)osi_getbuf (MCA_CTRL_MTU);
127 p_pkt->offset = L2CAP_MIN_OFFSET;
128 p = p_start = (UINT8*)(p_pkt + 1) + L2CAP_MIN_OFFSET;
129 *p++ = p_msg->op_code;
130 UINT16_TO_BE_STREAM (p, p_msg->mdl_id);
131 if (p_msg->op_code == MCA_OP_MDL_CREATE_REQ)
133 *p++ = p_msg->mdep_id;
136 p_msg->hdr.layer_specific = TRUE; /* mark this message as sent */
137 p_pkt->len = p - p_start;
138 L2CA_DataWrite (p_ccb->lcid, p_pkt);
139 p_ccb->timer_entry.param = (timer_param_t)p_ccb;
140 btu_start_timer(&p_ccb->timer_entry, BTU_TTYPE_MCA_CCB_RSP, p_ccb->p_rcb->reg.rsp_tout);
143 /* else the L2CAP channel is congested. keep the message to be sent later */
147 MCA_TRACE_WARNING ("dropping api req");
148 osi_freebuf (p_data);
152 /*******************************************************************************
154 ** Function mca_ccb_snd_rsp
156 ** Description This function builds a response and sends it to
161 *******************************************************************************/
162 void mca_ccb_snd_rsp(tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data)
164 tMCA_CCB_MSG *p_msg = (tMCA_CCB_MSG *)p_data;
167 BOOLEAN chk_mdl = FALSE;
169 MCA_TRACE_DEBUG ("mca_ccb_snd_rsp cong=%d req=%d", p_ccb->cong, p_msg->op_code);
170 /* assume that API functions verified the parameters */
171 p_pkt = (BT_HDR *)osi_getbuf (MCA_CTRL_MTU);
174 p_pkt->offset = L2CAP_MIN_OFFSET;
175 p = p_start = (UINT8*)(p_pkt + 1) + L2CAP_MIN_OFFSET;
176 *p++ = p_msg->op_code;
177 *p++ = p_msg->rsp_code;
178 UINT16_TO_BE_STREAM (p, p_msg->mdl_id);
179 if (p_msg->op_code == MCA_OP_MDL_CREATE_RSP)
184 else if (p_msg->op_code == MCA_OP_MDL_RECONNECT_RSP)
187 if (chk_mdl && p_msg->rsp_code == MCA_RSP_SUCCESS)
189 mca_dcb_by_hdl(p_msg->dcb_idx);
190 BTM_SetSecurityLevel(FALSE, "", BTM_SEC_SERVICE_MCAP_DATA, p_ccb->sec_mask,
191 p_ccb->p_rcb->reg.data_psm, BTM_SEC_PROTO_MCA, p_msg->dcb_idx);
192 p_ccb->status = MCA_CCB_STAT_PENDING;
193 /* set p_tx_req to block API_REQ/API_RSP before DL is up */
194 mca_free_buf ((void **)&p_ccb->p_tx_req);
195 p_ccb->p_tx_req = p_ccb->p_rx_msg;
196 p_ccb->p_rx_msg = NULL;
197 p_ccb->p_tx_req->dcb_idx = p_msg->dcb_idx;
199 mca_free_buf ((void **)&p_ccb->p_rx_msg);
200 p_pkt->len = p - p_start;
201 L2CA_DataWrite (p_ccb->lcid, p_pkt);
206 /*******************************************************************************
208 ** Function mca_ccb_do_disconn
210 ** Description This function closes a control channel.
214 *******************************************************************************/
215 void mca_ccb_do_disconn (tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data)
219 mca_dcb_close_by_mdl_id (p_ccb, MCA_ALL_MDL_ID);
220 L2CA_DisconnectReq(p_ccb->lcid);
223 /*******************************************************************************
225 ** Function mca_ccb_cong
227 ** Description This function sets the congestion state for the CCB.
231 *******************************************************************************/
232 void mca_ccb_cong(tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data)
234 MCA_TRACE_DEBUG ("mca_ccb_cong cong=%d/%d", p_ccb->cong, p_data->llcong);
235 p_ccb->cong = p_data->llcong;
238 /* if there's a held packet, send it now */
239 if (p_ccb->p_tx_req && !p_ccb->p_tx_req->hdr.layer_specific)
241 p_data = (tMCA_CCB_EVT *)p_ccb->p_tx_req;
242 p_ccb->p_tx_req = NULL;
243 mca_ccb_snd_req (p_ccb, p_data);
248 /*******************************************************************************
250 ** Function mca_ccb_hdl_req
252 ** Description This function is called when a MCAP request is received from
253 ** the peer. It calls the application callback function to
258 *******************************************************************************/
259 void mca_ccb_hdl_req(tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data)
261 BT_HDR *p_pkt = &p_data->hdr;
266 tMCA_CCB_MSG *p_rx_msg = NULL;
267 UINT8 reject_code = MCA_RSP_NO_RESOURCE;
268 BOOLEAN send_rsp = FALSE;
269 BOOLEAN check_req = FALSE;
272 MCA_TRACE_DEBUG ("mca_ccb_hdl_req status:%d", p_ccb->status);
273 p_rx_msg = (tMCA_CCB_MSG *)p_pkt;
274 p = (UINT8 *)(p_pkt + 1) + p_pkt->offset;
275 evt_data.hdr.op_code = *p++;
276 BE_STREAM_TO_UINT16 (evt_data.hdr.mdl_id, p);
277 reject_opcode = evt_data.hdr.op_code+1;
279 MCA_TRACE_DEBUG ("received mdl id: %d ", evt_data.hdr.mdl_id);
280 if (p_ccb->status == MCA_CCB_STAT_PENDING)
282 MCA_TRACE_DEBUG ("received req inpending state");
283 /* allow abort in pending state */
284 if ((p_ccb->status == MCA_CCB_STAT_PENDING) && (evt_data.hdr.op_code == MCA_OP_MDL_ABORT_REQ))
286 reject_code = MCA_RSP_SUCCESS;
288 /* clear the pending status */
289 p_ccb->status = MCA_CCB_STAT_NORM;
290 if (p_ccb->p_tx_req && ((p_dcb = mca_dcb_by_hdl(p_ccb->p_tx_req->dcb_idx))!= NULL))
292 mca_dcb_dealloc (p_dcb, NULL);
293 mca_free_buf ((void **)&p_ccb->p_tx_req);
297 reject_code = MCA_RSP_BAD_OP;
299 else if (p_ccb->p_rx_msg)
301 MCA_TRACE_DEBUG ("still handling prev req");
302 /* still holding previous message, reject this new one ?? */
305 else if (p_ccb->p_tx_req)
307 MCA_TRACE_DEBUG ("still waiting for a response ctrl_vpsm:0x%x", p_ccb->ctrl_vpsm);
308 /* sent a request; waiting for response */
309 if (p_ccb->ctrl_vpsm == 0)
311 MCA_TRACE_DEBUG ("local is ACP. accept the cmd from INT");
312 /* local is acceptor, need to handle the request */
314 reject_code = MCA_RSP_SUCCESS;
315 /* drop the previous request */
316 if ((p_ccb->p_tx_req->op_code == MCA_OP_MDL_CREATE_REQ) &&
317 ((p_dcb = mca_dcb_by_hdl(p_ccb->p_tx_req->dcb_idx)) != NULL))
319 mca_dcb_dealloc(p_dcb, NULL);
321 mca_free_buf ((void **)&p_ccb->p_tx_req);
322 mca_stop_timer(p_ccb);
326 /* local is initiator, ignore the req */
331 else if (p_pkt->layer_specific != MCA_RSP_SUCCESS)
334 reject_code = (UINT8)p_pkt->layer_specific;
335 if (((evt_data.hdr.op_code >= MCA_NUM_STANDARD_OPCODE) &&
336 (evt_data.hdr.op_code < MCA_FIRST_SYNC_OP)) ||
337 (evt_data.hdr.op_code > MCA_LAST_SYNC_OP))
339 /* invalid op code */
340 reject_opcode = MCA_OP_ERROR_RSP;
341 evt_data.hdr.mdl_id = 0;
347 reject_code = MCA_RSP_SUCCESS;
352 if (reject_code == MCA_RSP_SUCCESS)
354 reject_code = MCA_RSP_BAD_MDL;
355 if (MCA_IS_VALID_MDL_ID(evt_data.hdr.mdl_id) ||
356 ((evt_data.hdr.mdl_id == MCA_ALL_MDL_ID) && (evt_data.hdr.op_code == MCA_OP_MDL_DELETE_REQ)))
358 reject_code = MCA_RSP_SUCCESS;
359 /* mdl_id is valid according to the spec */
360 switch (evt_data.hdr.op_code)
362 case MCA_OP_MDL_CREATE_REQ:
363 evt_data.create_ind.dep_id = *p++;
364 evt_data.create_ind.cfg = *p++;
365 p_rx_msg->mdep_id = evt_data.create_ind.dep_id;
366 if (!mca_is_valid_dep_id(p_ccb->p_rcb, p_rx_msg->mdep_id))
368 MCA_TRACE_ERROR ("not a valid local mdep id");
369 reject_code = MCA_RSP_BAD_MDEP;
371 else if (mca_ccb_uses_mdl_id(p_ccb, evt_data.hdr.mdl_id))
373 MCA_TRACE_DEBUG ("the mdl_id is currently used in the CL(create)");
374 mca_dcb_close_by_mdl_id(p_ccb, evt_data.hdr.mdl_id);
378 /* check if this dep still have MDL available */
379 if (mca_dep_free_mdl(p_ccb, evt_data.create_ind.dep_id) == 0)
381 MCA_TRACE_ERROR ("the mdep is currently using max_mdl");
382 reject_code = MCA_RSP_MDEP_BUSY;
387 case MCA_OP_MDL_RECONNECT_REQ:
388 if (mca_ccb_uses_mdl_id(p_ccb, evt_data.hdr.mdl_id))
390 MCA_TRACE_ERROR ("the mdl_id is currently used in the CL(reconn)");
391 reject_code = MCA_RSP_MDL_BUSY;
395 case MCA_OP_MDL_ABORT_REQ:
396 reject_code = MCA_RSP_BAD_OP;
399 case MCA_OP_MDL_DELETE_REQ:
400 /* delete the associated mdl */
401 mca_dcb_close_by_mdl_id(p_ccb, evt_data.hdr.mdl_id);
409 if (((reject_code != MCA_RSP_SUCCESS) && (evt_data.hdr.op_code != MCA_OP_SYNC_INFO_IND))
412 p_buf = (BT_HDR *)osi_getbuf (MCA_CTRL_MTU);
415 p_buf->offset = L2CAP_MIN_OFFSET;
416 p = p_start = (UINT8*)(p_buf + 1) + L2CAP_MIN_OFFSET;
417 *p++ = reject_opcode;
419 UINT16_TO_BE_STREAM (p, evt_data.hdr.mdl_id);
421 if (((*p_start) == MCA_OP_MDL_CREATE_RSP) && (reject_code == MCA_RSP_SUCCESS))
423 *p++ = evt_data.create_ind.cfg;
427 p_buf->len = p - p_start;
428 L2CA_DataWrite (p_ccb->lcid, p_buf);
432 if (reject_code == MCA_RSP_SUCCESS)
434 /* use the received GKI buffer to store information to double check response API */
435 p_rx_msg->op_code = evt_data.hdr.op_code;
436 p_rx_msg->mdl_id = evt_data.hdr.mdl_id;
437 p_ccb->p_rx_msg = p_rx_msg;
441 p_ccb->p_rx_msg = NULL;
443 mca_ccb_report_event(p_ccb, evt_data.hdr.op_code, &evt_data);
449 /*******************************************************************************
451 ** Function mca_ccb_hdl_rsp
453 ** Description This function is called when a MCAP response is received from
454 ** the peer. It calls the application callback function with
459 *******************************************************************************/
460 void mca_ccb_hdl_rsp(tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data)
462 BT_HDR *p_pkt = &p_data->hdr;
465 BOOLEAN chk_mdl = FALSE;
467 tMCA_RESULT result = MCA_BAD_HANDLE;
472 /* verify that the received response matches the sent request */
473 p = (UINT8 *)(p_pkt + 1) + p_pkt->offset;
474 evt_data.hdr.op_code = *p++;
475 if ((evt_data.hdr.op_code == 0) ||
476 ((p_ccb->p_tx_req->op_code + 1) == evt_data.hdr.op_code))
478 evt_data.rsp.rsp_code = *p++;
479 mca_stop_timer(p_ccb);
480 BE_STREAM_TO_UINT16 (evt_data.hdr.mdl_id, p);
481 if (evt_data.hdr.op_code == MCA_OP_MDL_CREATE_RSP)
483 evt_data.create_cfm.cfg = *p++;
486 else if (evt_data.hdr.op_code == MCA_OP_MDL_RECONNECT_RSP)
491 p_dcb = mca_dcb_by_hdl(p_ccb->p_tx_req->dcb_idx);
492 if (evt_data.rsp.rsp_code == MCA_RSP_SUCCESS)
494 if (evt_data.hdr.mdl_id != p_dcb->mdl_id)
496 MCA_TRACE_ERROR ("peer's mdl_id=%d != our mdl_id=%d", evt_data.hdr.mdl_id, p_dcb->mdl_id);
497 /* change the response code to be an error */
498 if (evt_data.rsp.rsp_code == MCA_RSP_SUCCESS)
500 evt_data.rsp.rsp_code = MCA_RSP_BAD_MDL;
502 p_ccb->status = MCA_CCB_STAT_PENDING;
503 MCA_Abort(mca_ccb_to_hdl(p_ccb));
506 else if (p_dcb->p_chnl_cfg)
508 /* the data channel configuration is known. Proceed with data channel initiation */
509 BTM_SetSecurityLevel(TRUE, "", BTM_SEC_SERVICE_MCAP_DATA, p_ccb->sec_mask,
510 p_ccb->data_vpsm, BTM_SEC_PROTO_MCA, p_ccb->p_tx_req->dcb_idx);
511 p_dcb->lcid = mca_l2c_open_req(p_ccb->peer_addr, p_ccb->data_vpsm, p_dcb->p_chnl_cfg);
514 p_tbl = mca_tc_tbl_dalloc(p_dcb);
517 p_tbl->state = MCA_TC_ST_CONN;
518 p_ccb->status = MCA_CCB_STAT_PENDING;
519 result = MCA_SUCCESS;
525 /* mark this MCL as pending and wait for MCA_DataChnlCfg */
526 p_ccb->status = MCA_CCB_STAT_PENDING;
527 result = MCA_SUCCESS;
531 if (result != MCA_SUCCESS && p_dcb)
533 mca_dcb_dealloc(p_dcb, NULL);
535 } /* end of chk_mdl */
537 if (p_ccb->status != MCA_CCB_STAT_PENDING)
538 mca_free_buf ((void **)&p_ccb->p_tx_req);
539 mca_ccb_report_event(p_ccb, evt_data.hdr.op_code, &evt_data);
541 /* else a bad response is received */
545 /* not expecting any response. drop it */
546 MCA_TRACE_WARNING ("dropping received rsp (not expecting a response)");
548 osi_freebuf (p_data);
551 /*******************************************************************************
553 ** Function mca_ccb_ll_open
555 ** Description This function is called to report MCA_CONNECT_IND_EVT event.
556 ** It also clears the congestion flag (ccb.cong).
560 *******************************************************************************/
561 void mca_ccb_ll_open (tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data)
565 evt_data.connect_ind.mtu = p_data->open.peer_mtu;
566 memcpy (evt_data.connect_ind.bd_addr, p_ccb->peer_addr, BD_ADDR_LEN);
567 mca_ccb_report_event (p_ccb, MCA_CONNECT_IND_EVT, &evt_data);
570 /*******************************************************************************
572 ** Function mca_ccb_dl_open
574 ** Description This function is called when data channel is open.
575 ** It clears p_tx_req to allow other message exchage on this CL.
579 *******************************************************************************/
580 void mca_ccb_dl_open (tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data)
584 mca_free_buf ((void **)&p_ccb->p_tx_req);
585 mca_free_buf ((void **)&p_ccb->p_rx_msg);
586 p_ccb->status = MCA_CCB_STAT_NORM;