1 /******************************************************************************
2 *
3 * Copyright (C) 2002-2012 Broadcom Corporation
4 *
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:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 *
21 * This module contains the action functions associated with the stream
22 * control block state machine.
23 *
24 ******************************************************************************/
25
26 #include <cutils/log.h>
27 #include <string.h>
28 #include "a2dp_codec_api.h"
29 #include "avdt_api.h"
30 #include "avdt_int.h"
31 #include "avdtc_api.h"
32 #include "bt_common.h"
33 #include "bt_target.h"
34 #include "bt_types.h"
35 #include "bt_utils.h"
36 #include "btu.h"
37 #include "osi/include/osi.h"
38
39 /* This table is used to lookup the callback event that matches a particular
40 * state machine API request event. Note that state machine API request
41 * events are at the beginning of the event list starting at zero, thus
42 * allowing for this table.
43 */
44 const uint8_t avdt_scb_cback_evt[] = {
45 0, /* API_REMOVE_EVT (no event) */
46 AVDT_WRITE_CFM_EVT, /* API_WRITE_REQ_EVT */
47 0, /* API_GETCONFIG_REQ_EVT (no event) */
48 0, /* API_DELAY_RPT_REQ_EVT (no event) */
49 AVDT_OPEN_CFM_EVT, /* API_SETCONFIG_REQ_EVT */
50 AVDT_OPEN_CFM_EVT, /* API_OPEN_REQ_EVT */
51 AVDT_CLOSE_CFM_EVT, /* API_CLOSE_REQ_EVT */
52 AVDT_RECONFIG_CFM_EVT, /* API_RECONFIG_REQ_EVT */
53 AVDT_SECURITY_CFM_EVT, /* API_SECURITY_REQ_EVT */
54 0 /* API_ABORT_REQ_EVT (no event) */
55 };
56
57 /*******************************************************************************
58 *
59 * Function avdt_scb_gen_ssrc
60 *
61 * Description This function generates a SSRC number unique to the stream.
62 *
63 * Returns SSRC value.
64 *
65 ******************************************************************************/
avdt_scb_gen_ssrc(tAVDT_SCB * p_scb)66 uint32_t avdt_scb_gen_ssrc(tAVDT_SCB* p_scb) {
67 /* combine the value of the media type and codec type of the SCB */
68 return (
69 (uint32_t)(p_scb->cs.cfg.codec_info[1] | p_scb->cs.cfg.codec_info[2]));
70 }
71
72 /*******************************************************************************
73 *
74 * Function avdt_scb_hdl_abort_cmd
75 *
76 * Description This function sends the SCB an AVDT_SCB_API_ABORT_RSP_EVT
77 * to initiate sending of an abort response message.
78 *
79 * Returns Nothing.
80 *
81 ******************************************************************************/
avdt_scb_hdl_abort_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)82 void avdt_scb_hdl_abort_cmd(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
83 p_scb->role = AVDT_CLOSE_ACP;
84 avdt_scb_event(p_scb, AVDT_SCB_API_ABORT_RSP_EVT, p_data);
85 }
86
87 /*******************************************************************************
88 *
89 * Function avdt_scb_hdl_abort_rsp
90 *
91 * Description This function is an empty function; it serves as a
92 * placeholder for a conformance API action function.
93 *
94 * Returns Nothing.
95 *
96 ******************************************************************************/
avdt_scb_hdl_abort_rsp(UNUSED_ATTR tAVDT_SCB * p_scb,UNUSED_ATTR tAVDT_SCB_EVT * p_data)97 void avdt_scb_hdl_abort_rsp(UNUSED_ATTR tAVDT_SCB* p_scb,
98 UNUSED_ATTR tAVDT_SCB_EVT* p_data) {
99 return;
100 }
101
102 /*******************************************************************************
103 *
104 * Function avdt_scb_hdl_close_cmd
105 *
106 * Description This function sends the SCB an AVDT_SCB_API_CLOSE_RSP_EVT
107 * to initiate sending of a close response message.
108 *
109 * Returns Nothing.
110 *
111 ******************************************************************************/
avdt_scb_hdl_close_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)112 void avdt_scb_hdl_close_cmd(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
113 p_scb->role = AVDT_CLOSE_ACP;
114 avdt_scb_event(p_scb, AVDT_SCB_API_CLOSE_RSP_EVT, p_data);
115 }
116
117 /*******************************************************************************
118 *
119 * Function avdt_scb_hdl_close_rsp
120 *
121 * Description This function sets the close_code variable to the error
122 * code returned in the close response.
123 *
124 * Returns Nothing.
125 *
126 ******************************************************************************/
avdt_scb_hdl_close_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)127 void avdt_scb_hdl_close_rsp(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
128 p_scb->close_code = p_data->msg.hdr.err_code;
129 }
130
131 /*******************************************************************************
132 *
133 * Function avdt_scb_hdl_getconfig_cmd
134 *
135 * Description This function retrieves the configuration parameters of
136 * the SCB and sends the SCB an AVDT_SCB_API_GETCONFIG_RSP_EVT
137 * to initiate sending of a get configuration response message.
138 *
139 * Returns Nothing.
140 *
141 ******************************************************************************/
avdt_scb_hdl_getconfig_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)142 void avdt_scb_hdl_getconfig_cmd(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
143 p_data->msg.svccap.p_cfg = &p_scb->curr_cfg;
144
145 avdt_scb_event(p_scb, AVDT_SCB_API_GETCONFIG_RSP_EVT, p_data);
146 }
147
148 /*******************************************************************************
149 *
150 * Function avdt_scb_hdl_getconfig_rsp
151 *
152 * Description This function is an empty function; it serves as a
153 * placeholder for a conformance API action function.
154 *
155 * Returns Nothing.
156 *
157 ******************************************************************************/
avdt_scb_hdl_getconfig_rsp(UNUSED_ATTR tAVDT_SCB * p_scb,UNUSED_ATTR tAVDT_SCB_EVT * p_data)158 void avdt_scb_hdl_getconfig_rsp(UNUSED_ATTR tAVDT_SCB* p_scb,
159 UNUSED_ATTR tAVDT_SCB_EVT* p_data) {
160 return;
161 }
162
163 /*******************************************************************************
164 *
165 * Function avdt_scb_hdl_open_cmd
166 *
167 * Description This function sends the SCB an AVDT_SCB_API_OPEN_RSP_EVT
168 * to initiate sending of an open response message.
169 *
170 * Returns Nothing.
171 *
172 ******************************************************************************/
avdt_scb_hdl_open_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)173 void avdt_scb_hdl_open_cmd(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
174 avdt_scb_event(p_scb, AVDT_SCB_API_OPEN_RSP_EVT, p_data);
175 }
176
177 /*******************************************************************************
178 *
179 * Function avdt_scb_hdl_open_rej
180 *
181 * Description This function calls the application callback function
182 * indicating the open request has failed. It initializes
183 * certain SCB variables and sends a AVDT_CCB_UL_CLOSE_EVT
184 * to the CCB.
185 *
186 * Returns Nothing.
187 *
188 ******************************************************************************/
avdt_scb_hdl_open_rej(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)189 void avdt_scb_hdl_open_rej(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
190 /* do exactly same as setconfig reject */
191 avdt_scb_hdl_setconfig_rej(p_scb, p_data);
192 }
193
194 /*******************************************************************************
195 *
196 * Function avdt_scb_hdl_open_rsp
197 *
198 * Description This function calls avdt_ad_open_req() to initiate
199 * connection of the transport channel for this stream.
200 *
201 * Returns Nothing.
202 *
203 ******************************************************************************/
avdt_scb_hdl_open_rsp(tAVDT_SCB * p_scb,UNUSED_ATTR tAVDT_SCB_EVT * p_data)204 void avdt_scb_hdl_open_rsp(tAVDT_SCB* p_scb,
205 UNUSED_ATTR tAVDT_SCB_EVT* p_data) {
206 /* initiate opening of trans channels for this SEID */
207 p_scb->role = AVDT_OPEN_INT;
208 avdt_ad_open_req(AVDT_CHAN_MEDIA, p_scb->p_ccb, p_scb, AVDT_INT);
209
210 /* start tc connect timer */
211 alarm_set_on_mloop(p_scb->transport_channel_timer,
212 AVDT_SCB_TC_CONN_TIMEOUT_MS,
213 avdt_scb_transport_channel_timer_timeout, p_scb);
214 }
215
216 /*******************************************************************************
217 *
218 * Function avdt_scb_hdl_pkt_no_frag
219 *
220 * Description
221 *
222 * Returns Nothing.
223 *
224 ******************************************************************************/
avdt_scb_hdl_pkt_no_frag(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)225 void avdt_scb_hdl_pkt_no_frag(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
226 uint8_t *p, *p_start;
227 uint8_t o_v, o_p, o_x, o_cc;
228 uint8_t m_pt;
229 uint8_t marker;
230 uint16_t seq;
231 uint32_t time_stamp;
232 uint16_t offset;
233 uint16_t ex_len;
234 uint8_t pad_len = 0;
235 uint16_t len = p_data->p_pkt->len;
236
237 p = p_start = (uint8_t*)(p_data->p_pkt + 1) + p_data->p_pkt->offset;
238
239 /* parse media packet header */
240 offset = 12;
241 // AVDT_MSG_PRS_OCTET1(1) + AVDT_MSG_PRS_M_PT(1) + UINT16(2) + UINT32(4) + 4
242 if (offset > len) goto length_error;
243 AVDT_MSG_PRS_OCTET1(p, o_v, o_p, o_x, o_cc);
244 AVDT_MSG_PRS_M_PT(p, m_pt, marker);
245 BE_STREAM_TO_UINT16(seq, p);
246 BE_STREAM_TO_UINT32(time_stamp, p);
247 p += 4;
248
249 /* skip over any csrc's in packet */
250 offset += o_cc * 4;
251 p += o_cc * 4;
252
253 /* check for and skip over extension header */
254 if (o_x) {
255 offset += 4;
256 if (offset > len) goto length_error;
257 p += 2;
258 BE_STREAM_TO_UINT16(ex_len, p);
259 offset += ex_len * 4;
260 p += ex_len * 4;
261 }
262
263 /* adjust length for any padding at end of packet */
264 if (o_p) {
265 /* padding length in last byte of packet */
266 pad_len = *(p_start + p_data->p_pkt->len);
267 }
268
269 /* do sanity check */
270 if ((offset > p_data->p_pkt->len) ||
271 ((pad_len + offset) > p_data->p_pkt->len)) {
272 AVDT_TRACE_WARNING("Got bad media packet");
273 osi_free_and_reset((void**)&p_data->p_pkt);
274 }
275 /* adjust offset and length and send it up */
276 else {
277 p_data->p_pkt->len -= (offset + pad_len);
278 p_data->p_pkt->offset += offset;
279
280 if (p_scb->cs.p_sink_data_cback != NULL) {
281 /* report sequence number */
282 p_data->p_pkt->layer_specific = seq;
283 (*p_scb->cs.p_sink_data_cback)(avdt_scb_to_hdl(p_scb), p_data->p_pkt,
284 time_stamp,
285 (uint8_t)(m_pt | (marker << 7)));
286 } else {
287 osi_free_and_reset((void**)&p_data->p_pkt);
288 }
289 }
290 return;
291 length_error:
292 android_errorWriteLog(0x534e4554, "111450156");
293 AVDT_TRACE_WARNING("%s: hdl packet length %d too short: must be at least %d",
294 __func__, len, offset);
295 osi_free_and_reset((void**)&p_data->p_pkt);
296 }
297
298 #if (AVDT_REPORTING == TRUE)
299 /*******************************************************************************
300 *
301 * Function avdt_scb_hdl_report
302 *
303 * Description
304 *
305 * Returns Nothing.
306 *
307 ******************************************************************************/
avdt_scb_hdl_report(tAVDT_SCB * p_scb,uint8_t * p,uint16_t len)308 uint8_t* avdt_scb_hdl_report(tAVDT_SCB* p_scb, uint8_t* p, uint16_t len) {
309 uint16_t result = AVDT_SUCCESS;
310 uint8_t* p_start = p;
311 uint32_t ssrc;
312 uint8_t o_v, o_p, o_cc;
313 uint16_t min_len = 0;
314 AVDT_REPORT_TYPE pt;
315 tAVDT_REPORT_DATA report;
316
317 AVDT_TRACE_DEBUG("%s", __func__);
318 if (p_scb->cs.p_report_cback) {
319 /* parse report packet header */
320 min_len += 8;
321 if (min_len > len) {
322 android_errorWriteLog(0x534e4554, "111450156");
323 AVDT_TRACE_WARNING(
324 "%s: hdl packet length %d too short: must be at least %d", __func__,
325 len, min_len);
326 goto avdt_scb_hdl_report_exit;
327 }
328 AVDT_MSG_PRS_RPT_OCTET1(p, o_v, o_p, o_cc);
329 pt = *p++;
330 p += 2;
331 BE_STREAM_TO_UINT32(ssrc, p);
332
333 switch (pt) {
334 case AVDT_RTCP_PT_SR: /* the packet type - SR (Sender Report) */
335 min_len += 20;
336 if (min_len > len) {
337 android_errorWriteLog(0x534e4554, "111450156");
338 AVDT_TRACE_WARNING(
339 "%s: hdl packet length %d too short: must be at least %d",
340 __func__, len, min_len);
341 goto avdt_scb_hdl_report_exit;
342 }
343 BE_STREAM_TO_UINT32(report.sr.ntp_sec, p);
344 BE_STREAM_TO_UINT32(report.sr.ntp_frac, p);
345 BE_STREAM_TO_UINT32(report.sr.rtp_time, p);
346 BE_STREAM_TO_UINT32(report.sr.pkt_count, p);
347 BE_STREAM_TO_UINT32(report.sr.octet_count, p);
348 break;
349
350 case AVDT_RTCP_PT_RR: /* the packet type - RR (Receiver Report) */
351 min_len += 20;
352 if (min_len > len) {
353 android_errorWriteLog(0x534e4554, "111450156");
354 AVDT_TRACE_WARNING(
355 "%s: hdl packet length %d too short: must be at least %d",
356 __func__, len, min_len);
357 goto avdt_scb_hdl_report_exit;
358 }
359 report.rr.frag_lost = *p;
360 BE_STREAM_TO_UINT32(report.rr.packet_lost, p);
361 report.rr.packet_lost &= 0xFFFFFF;
362 BE_STREAM_TO_UINT32(report.rr.seq_num_rcvd, p);
363 BE_STREAM_TO_UINT32(report.rr.jitter, p);
364 BE_STREAM_TO_UINT32(report.rr.lsr, p);
365 BE_STREAM_TO_UINT32(report.rr.dlsr, p);
366 break;
367
368 case AVDT_RTCP_PT_SDES: /* the packet type - SDES (Source Description) */
369 uint8_t sdes_type;
370 min_len += 1;
371 if (min_len > len) {
372 android_errorWriteLog(0x534e4554, "111450156");
373 AVDT_TRACE_WARNING(
374 "%s: hdl packet length %d too short: must be at least %d",
375 __func__, len, min_len);
376 goto avdt_scb_hdl_report_exit;
377 }
378 BE_STREAM_TO_UINT8(sdes_type, p);
379 if (sdes_type == AVDT_RTCP_SDES_CNAME) {
380 uint8_t name_length;
381 min_len += 1;
382 if (min_len > len) {
383 android_errorWriteLog(0x534e4554, "111450156");
384 AVDT_TRACE_WARNING(
385 "%s: hdl packet length %d too short: must be at least %d",
386 __func__, len, min_len);
387 goto avdt_scb_hdl_report_exit;
388 }
389 BE_STREAM_TO_UINT8(name_length, p);
390 if (name_length > len - 2 || name_length > AVDT_MAX_CNAME_SIZE) {
391 result = AVDT_BAD_PARAMS;
392 } else {
393 BE_STREAM_TO_ARRAY(p, &(report.cname[0]), name_length);
394 }
395 } else {
396 if (min_len + 1 > len) {
397 android_errorWriteLog(0x534e4554, "111450156");
398 AVDT_TRACE_WARNING(
399 "%s: hdl packet length %d too short: must be at least %d",
400 __func__, len, min_len + 2);
401 goto avdt_scb_hdl_report_exit;
402 }
403 AVDT_TRACE_WARNING(" - SDES SSRC=0x%08x sc=%d %d len=%d %s", ssrc,
404 o_cc, *p, *(p + 1), p + 2);
405 result = AVDT_BUSY;
406 }
407 break;
408
409 default:
410 AVDT_TRACE_ERROR("Bad Report pkt - packet type: %d", pt);
411 result = AVDT_BAD_PARAMS;
412 }
413
414 if (result == AVDT_SUCCESS)
415 (*p_scb->cs.p_report_cback)(avdt_scb_to_hdl(p_scb), pt, &report);
416 }
417 avdt_scb_hdl_report_exit:
418 p_start += len;
419 return p_start;
420 }
421 #endif
422
423 /*******************************************************************************
424 *
425 * Function avdt_scb_hdl_pkt
426 *
427 * Description
428 *
429 * Returns Nothing.
430 *
431 ******************************************************************************/
avdt_scb_hdl_pkt(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)432 void avdt_scb_hdl_pkt(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
433 #if (AVDT_REPORTING == TRUE)
434 if (p_data->p_pkt->layer_specific == AVDT_CHAN_REPORT) {
435 uint8_t* p = (uint8_t*)(p_data->p_pkt + 1) + p_data->p_pkt->offset;
436 avdt_scb_hdl_report(p_scb, p, p_data->p_pkt->len);
437 osi_free_and_reset((void**)&p_data->p_pkt);
438 } else
439 #endif
440 avdt_scb_hdl_pkt_no_frag(p_scb, p_data);
441 }
442
443 /*******************************************************************************
444 *
445 * Function avdt_scb_drop_pkt
446 *
447 * Description Drop an incoming media packet. This function is called if
448 * a media packet is received in any state besides streaming.
449 *
450 * Returns Nothing.
451 *
452 ******************************************************************************/
avdt_scb_drop_pkt(UNUSED_ATTR tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)453 void avdt_scb_drop_pkt(UNUSED_ATTR tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
454 AVDT_TRACE_ERROR("%s dropped incoming media packet", __func__);
455 osi_free_and_reset((void**)&p_data->p_pkt);
456 }
457
458 /*******************************************************************************
459 *
460 * Function avdt_scb_hdl_reconfig_cmd
461 *
462 * Description This function calls the application callback function
463 * with a reconfiguration indication.
464 *
465 * Returns Nothing.
466 *
467 ******************************************************************************/
avdt_scb_hdl_reconfig_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)468 void avdt_scb_hdl_reconfig_cmd(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
469 /* if command not supported */
470 if (p_scb->cs.nsc_mask & AVDT_NSC_RECONFIG) {
471 /* send reject */
472 p_data->msg.hdr.err_code = AVDT_ERR_NSC;
473 p_data->msg.hdr.err_param = 0;
474 avdt_scb_event(p_scb, AVDT_SCB_API_RECONFIG_RSP_EVT, p_data);
475 } else {
476 /* store requested configuration */
477 memcpy(&p_scb->req_cfg, p_data->msg.reconfig_cmd.p_cfg, sizeof(tAVDT_CFG));
478
479 /* call application callback */
480 (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb), NULL,
481 AVDT_RECONFIG_IND_EVT,
482 (tAVDT_CTRL*)&p_data->msg.reconfig_cmd);
483 }
484 }
485
486 /*******************************************************************************
487 *
488 * Function avdt_scb_hdl_reconfig_rsp
489 *
490 * Description This function calls the application callback function
491 * with a reconfiguration confirm.
492 *
493 * Returns Nothing.
494 *
495 ******************************************************************************/
avdt_scb_hdl_reconfig_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)496 void avdt_scb_hdl_reconfig_rsp(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
497 if (p_data->msg.hdr.err_code == 0) {
498 /* store new configuration */
499 if (p_scb->req_cfg.num_codec > 0) {
500 p_scb->curr_cfg.num_codec = p_scb->req_cfg.num_codec;
501 memcpy(p_scb->curr_cfg.codec_info, p_scb->req_cfg.codec_info,
502 AVDT_CODEC_SIZE);
503 }
504 if (p_scb->req_cfg.num_protect > 0) {
505 p_scb->curr_cfg.num_protect = p_scb->req_cfg.num_protect;
506 memcpy(p_scb->curr_cfg.protect_info, p_scb->req_cfg.protect_info,
507 AVDT_PROTECT_SIZE);
508 }
509 }
510
511 p_data->msg.svccap.p_cfg = &p_scb->curr_cfg;
512
513 /* call application callback */
514 (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb), NULL, AVDT_RECONFIG_CFM_EVT,
515 (tAVDT_CTRL*)&p_data->msg.svccap);
516 }
517
518 /*******************************************************************************
519 *
520 * Function avdt_scb_hdl_security_cmd
521 *
522 * Description This function calls the application callback with a
523 * security indication.
524 *
525 * Returns Nothing.
526 *
527 ******************************************************************************/
avdt_scb_hdl_security_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)528 void avdt_scb_hdl_security_cmd(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
529 /* if command not supported */
530 if (p_scb->cs.nsc_mask & AVDT_NSC_SECURITY) {
531 /* send reject */
532 p_data->msg.hdr.err_code = AVDT_ERR_NSC;
533 avdt_scb_event(p_scb, AVDT_SCB_API_SECURITY_RSP_EVT, p_data);
534 } else {
535 /* call application callback */
536 (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb), NULL,
537 AVDT_SECURITY_IND_EVT,
538 (tAVDT_CTRL*)&p_data->msg.security_cmd);
539 }
540 }
541
542 /*******************************************************************************
543 *
544 * Function avdt_scb_hdl_security_rsp
545 *
546 * Description This function calls the application callback with a
547 * security confirm.
548 *
549 * Returns Nothing.
550 *
551 ******************************************************************************/
avdt_scb_hdl_security_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)552 void avdt_scb_hdl_security_rsp(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
553 /* call application callback */
554 (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb), NULL, AVDT_SECURITY_CFM_EVT,
555 (tAVDT_CTRL*)&p_data->msg.security_cmd);
556 }
557
558 /*******************************************************************************
559 *
560 * Function avdt_scb_hdl_setconfig_cmd
561 *
562 * Description This function marks the SCB as in use and copies the
563 * configuration and peer SEID to the SCB. It then calls
564 * the application callback with a configuration indication.
565 *
566 * Returns Nothing.
567 *
568 ******************************************************************************/
avdt_scb_hdl_setconfig_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)569 void avdt_scb_hdl_setconfig_cmd(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
570 tAVDT_CFG* p_cfg;
571
572 AVDT_TRACE_DEBUG("%s: p_scb->in_use=%d", __func__, p_scb->in_use);
573
574 if (!p_scb->in_use) {
575 A2DP_DumpCodecInfo(p_scb->cs.cfg.codec_info);
576 A2DP_DumpCodecInfo(p_data->msg.config_cmd.p_cfg->codec_info);
577 p_cfg = p_data->msg.config_cmd.p_cfg;
578 if (A2DP_GetCodecType(p_scb->cs.cfg.codec_info) ==
579 A2DP_GetCodecType(p_cfg->codec_info)) {
580 /* set sep as in use */
581 p_scb->in_use = true;
582
583 /* copy info to scb */
584 p_scb->p_ccb = avdt_ccb_by_idx(p_data->msg.config_cmd.hdr.ccb_idx);
585 p_scb->peer_seid = p_data->msg.config_cmd.int_seid;
586 memcpy(&p_scb->req_cfg, p_cfg, sizeof(tAVDT_CFG));
587 /* call app callback */
588 /* handle of scb- which is same as sep handle of bta_av_cb.p_scb*/
589 (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
590 p_scb->p_ccb ? &p_scb->p_ccb->peer_addr : NULL,
591 AVDT_CONFIG_IND_EVT,
592 (tAVDT_CTRL*)&p_data->msg.config_cmd);
593 } else {
594 p_data->msg.hdr.err_code = AVDT_ERR_UNSUP_CFG;
595 p_data->msg.hdr.err_param = 0;
596 avdt_msg_send_rej(avdt_ccb_by_idx(p_data->msg.hdr.ccb_idx),
597 p_data->msg.hdr.sig_id, &p_data->msg);
598 }
599 } else {
600 AVDT_TRACE_DEBUG("%s: calling avdt_scb_rej_in_use()", __func__);
601 avdt_scb_rej_in_use(p_scb, p_data);
602 }
603 }
604
605 /*******************************************************************************
606 *
607 * Function avdt_scb_hdl_setconfig_rej
608 *
609 * Description This function marks the SCB as not in use and calls the
610 * application callback with an open confirm indicating
611 * failure.
612 *
613 * Returns Nothing.
614 *
615 ******************************************************************************/
avdt_scb_hdl_setconfig_rej(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)616 void avdt_scb_hdl_setconfig_rej(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
617 /* clear scb variables */
618 avdt_scb_clr_vars(p_scb, p_data);
619
620 /* tell ccb we're done with signaling channel */
621 avdt_ccb_event(avdt_ccb_by_idx(p_data->msg.hdr.ccb_idx),
622 AVDT_CCB_UL_CLOSE_EVT, NULL);
623
624 /* call application callback */
625 (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb), NULL, AVDT_OPEN_CFM_EVT,
626 (tAVDT_CTRL*)&p_data->msg.hdr);
627 }
628
629 /*******************************************************************************
630 *
631 * Function avdt_scb_hdl_setconfig_rsp
632 *
633 * Description This function sends the SCB an AVDT_SCB_API_OPEN_REQ_EVT
634 * to initiate sending of an open command message.
635 *
636 * Returns Nothing.
637 *
638 ******************************************************************************/
avdt_scb_hdl_setconfig_rsp(tAVDT_SCB * p_scb,UNUSED_ATTR tAVDT_SCB_EVT * p_data)639 void avdt_scb_hdl_setconfig_rsp(tAVDT_SCB* p_scb,
640 UNUSED_ATTR tAVDT_SCB_EVT* p_data) {
641 tAVDT_EVT_HDR single;
642
643 if (p_scb->p_ccb != NULL) {
644 /* save configuration */
645 memcpy(&p_scb->curr_cfg, &p_scb->req_cfg, sizeof(tAVDT_CFG));
646
647 /* initiate open */
648 single.seid = p_scb->peer_seid;
649 tAVDT_SCB_EVT avdt_scb_evt;
650 avdt_scb_evt.msg.single = single;
651 avdt_scb_event(p_scb, AVDT_SCB_API_OPEN_REQ_EVT, &avdt_scb_evt);
652 }
653 }
654
655 /*******************************************************************************
656 *
657 * Function avdt_scb_hdl_start_cmd
658 *
659 * Description This function calls the application callback with a
660 * start indication.
661 *
662 * Returns Nothing.
663 *
664 ******************************************************************************/
avdt_scb_hdl_start_cmd(tAVDT_SCB * p_scb,UNUSED_ATTR tAVDT_SCB_EVT * p_data)665 void avdt_scb_hdl_start_cmd(tAVDT_SCB* p_scb,
666 UNUSED_ATTR tAVDT_SCB_EVT* p_data) {
667 (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
668 p_scb->p_ccb ? &p_scb->p_ccb->peer_addr : NULL,
669 AVDT_START_IND_EVT, NULL);
670 }
671
672 /*******************************************************************************
673 *
674 * Function avdt_scb_hdl_start_rsp
675 *
676 * Description This function calls the application callback with a
677 * start confirm.
678 *
679 * Returns Nothing.
680 *
681 ******************************************************************************/
avdt_scb_hdl_start_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)682 void avdt_scb_hdl_start_rsp(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
683 (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
684 p_scb->p_ccb ? &p_scb->p_ccb->peer_addr : NULL,
685 AVDT_START_CFM_EVT, (tAVDT_CTRL*)&p_data->msg.hdr);
686 }
687
688 /*******************************************************************************
689 *
690 * Function avdt_scb_hdl_suspend_cmd
691 *
692 * Description This function calls the application callback with a suspend
693 * indication.
694 *
695 * Returns Nothing.
696 *
697 ******************************************************************************/
avdt_scb_hdl_suspend_cmd(tAVDT_SCB * p_scb,UNUSED_ATTR tAVDT_SCB_EVT * p_data)698 void avdt_scb_hdl_suspend_cmd(tAVDT_SCB* p_scb,
699 UNUSED_ATTR tAVDT_SCB_EVT* p_data) {
700 (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
701 p_scb->p_ccb ? &p_scb->p_ccb->peer_addr : NULL,
702 AVDT_SUSPEND_IND_EVT, NULL);
703 }
704
705 /*******************************************************************************
706 *
707 * Function avdt_scb_hdl_suspend_rsp
708 *
709 * Description This function calls the application callback with a suspend
710 * confirm.
711 *
712 * Returns Nothing.
713 *
714 ******************************************************************************/
avdt_scb_hdl_suspend_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)715 void avdt_scb_hdl_suspend_rsp(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
716 (*p_scb->cs.p_ctrl_cback)(
717 avdt_scb_to_hdl(p_scb), p_scb->p_ccb ? &p_scb->p_ccb->peer_addr : NULL,
718 AVDT_SUSPEND_CFM_EVT, (tAVDT_CTRL*)&p_data->msg.hdr);
719 }
720
721 /*******************************************************************************
722 *
723 * Function avdt_scb_hdl_tc_close
724 *
725 * Description This function is called when the transport channel is
726 * closed. It marks the SCB as not in use and
727 * initializes certain SCB parameters. It then sends
728 * an AVDT_CCB_UL_CLOSE_EVT to the CCB if the SCB
729 * initiated the close. It then checks to see if the SCB
730 * is to be removed. If it is it deallocates the SCB.
731 * Finally, it calls the application callback with a close
732 * indication.
733 *
734 * Returns Nothing.
735 *
736 ******************************************************************************/
avdt_scb_hdl_tc_close(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)737 void avdt_scb_hdl_tc_close(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
738 uint8_t hdl = avdt_scb_to_hdl(p_scb);
739 tAVDT_CTRL_CBACK* p_ctrl_cback = p_scb->cs.p_ctrl_cback;
740 tAVDT_CTRL avdt_ctrl;
741 uint8_t event;
742 tAVDT_CCB* p_ccb = p_scb->p_ccb;
743 RawAddress remote_addr = p_ccb->peer_addr;
744
745 /* set up hdr */
746 avdt_ctrl.hdr.err_code = p_scb->close_code;
747
748 /* clear sep variables */
749 avdt_scb_clr_vars(p_scb, p_data);
750 p_scb->media_seq = 0;
751 p_scb->cong = false;
752
753 /* free pkt we're holding, if any */
754 osi_free_and_reset((void**)&p_scb->p_pkt);
755
756 alarm_cancel(p_scb->transport_channel_timer);
757
758 if ((p_scb->role == AVDT_CLOSE_INT) || (p_scb->role == AVDT_OPEN_INT)) {
759 /* tell ccb we're done with signaling channel */
760 avdt_ccb_event(p_ccb, AVDT_CCB_UL_CLOSE_EVT, NULL);
761 }
762 event =
763 (p_scb->role == AVDT_CLOSE_INT) ? AVDT_CLOSE_CFM_EVT : AVDT_CLOSE_IND_EVT;
764 p_scb->role = AVDT_CLOSE_ACP;
765
766 if (p_scb->remove) {
767 avdt_scb_dealloc(p_scb, NULL);
768 }
769
770 /* call app callback */
771 (*p_ctrl_cback)(hdl, &remote_addr, event, &avdt_ctrl);
772 }
773
774 /*******************************************************************************
775 *
776 * Function avdt_scb_snd_delay_rpt_req
777 *
778 * Description This function calls the application callback with a delay
779 * report.
780 *
781 * Returns Nothing.
782 *
783 ******************************************************************************/
avdt_scb_snd_delay_rpt_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)784 void avdt_scb_snd_delay_rpt_req(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
785 avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_DELAY_RPT,
786 (tAVDT_MSG*)&p_data->apidelay);
787 }
788
789 /*******************************************************************************
790 *
791 * Function avdt_scb_hdl_delay_rpt_cmd
792 *
793 * Description This function calls the application callback with a delay
794 * report.
795 *
796 * Returns Nothing.
797 *
798 ******************************************************************************/
avdt_scb_hdl_delay_rpt_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)799 void avdt_scb_hdl_delay_rpt_cmd(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
800 (*p_scb->cs.p_ctrl_cback)(
801 avdt_scb_to_hdl(p_scb), p_scb->p_ccb ? &p_scb->p_ccb->peer_addr : NULL,
802 AVDT_DELAY_REPORT_EVT, (tAVDT_CTRL*)&p_data->msg.hdr);
803
804 if (p_scb->p_ccb)
805 avdt_msg_send_rsp(p_scb->p_ccb, AVDT_SIG_DELAY_RPT, &p_data->msg);
806 else
807 avdt_scb_rej_not_in_use(p_scb, p_data);
808 }
809
810 /*******************************************************************************
811 *
812 * Function avdt_scb_hdl_delay_rpt_rsp
813 *
814 * Description This function calls the application callback with a delay
815 * report.
816 *
817 * Returns Nothing.
818 *
819 ******************************************************************************/
avdt_scb_hdl_delay_rpt_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)820 void avdt_scb_hdl_delay_rpt_rsp(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
821 (*p_scb->cs.p_ctrl_cback)(
822 avdt_scb_to_hdl(p_scb), p_scb->p_ccb ? &p_scb->p_ccb->peer_addr : NULL,
823 AVDT_DELAY_REPORT_CFM_EVT, (tAVDT_CTRL*)&p_data->msg.hdr);
824 }
825
826 #if (AVDT_REPORTING == TRUE)
827 /*******************************************************************************
828 *
829 * Function avdt_scb_hdl_tc_close_sto
830 *
831 * Description This function is called when a channel is closed in OPEN
832 * state. Check the channel type and process accordingly.
833 *
834 * Returns Nothing.
835 *
836 ******************************************************************************/
avdt_scb_hdl_tc_close_sto(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)837 void avdt_scb_hdl_tc_close_sto(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
838 tAVDT_CTRL avdt_ctrl;
839 /* AVDT_CHAN_SIG does not visit this action */
840 if (p_data && p_data->close.type != AVDT_CHAN_MEDIA) {
841 /* it's reporting or recovery channel,
842 * the channel close in open state means the peer does not support it */
843 if (p_data->close.old_tc_state == AVDT_AD_ST_OPEN) {
844 avdt_ctrl.hdr.err_code = 0;
845 avdt_ctrl.hdr.err_param = 0;
846 /* call app callback */
847 (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
848 p_scb->p_ccb ? &p_scb->p_ccb->peer_addr : NULL,
849 AVDT_REPORT_DISCONN_EVT, &avdt_ctrl);
850 }
851 } else {
852 /* must be in OPEN state. need to go back to idle */
853 avdt_scb_event(p_scb, AVDT_SCB_MSG_ABORT_RSP_EVT, NULL);
854 avdt_scb_hdl_tc_close(p_scb, p_data);
855 }
856 }
857 #endif
858
859 /*******************************************************************************
860 *
861 * Function avdt_scb_hdl_tc_open
862 *
863 * Description This function is called when the transport channel is
864 * opened while in the opening state. It calls the
865 * application callback with an open indication or open
866 * confirm depending on who initiated the open procedure.
867 *
868 * Returns Nothing.
869 *
870 ******************************************************************************/
avdt_scb_hdl_tc_open(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)871 void avdt_scb_hdl_tc_open(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
872 uint8_t event;
873 #if (AVDT_REPORTING == TRUE)
874 uint8_t role;
875 #endif
876
877 alarm_cancel(p_scb->transport_channel_timer);
878
879 event =
880 (p_scb->role == AVDT_OPEN_INT) ? AVDT_OPEN_CFM_EVT : AVDT_OPEN_IND_EVT;
881 p_data->open.hdr.err_code = 0;
882
883 AVDT_TRACE_DEBUG("psc_mask: cfg: 0x%x, req:0x%x, cur: 0x%x",
884 p_scb->cs.cfg.psc_mask, p_scb->req_cfg.psc_mask,
885 p_scb->curr_cfg.psc_mask);
886 #if (AVDT_REPORTING == TRUE)
887 if (p_scb->curr_cfg.psc_mask & AVDT_PSC_REPORT) {
888 /* open the reporting channel, if both devices support it */
889 role = (p_scb->role == AVDT_OPEN_INT) ? AVDT_INT : AVDT_ACP;
890 avdt_ad_open_req(AVDT_CHAN_REPORT, p_scb->p_ccb, p_scb, role);
891 }
892 #endif
893
894 /* call app callback */
895 (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
896 p_scb->p_ccb ? &p_scb->p_ccb->peer_addr : NULL,
897 event, (tAVDT_CTRL*)&p_data->open);
898 }
899
900 #if (AVDT_REPORTING == TRUE)
901 /*******************************************************************************
902 *
903 * Function avdt_scb_hdl_tc_open_sto
904 *
905 * Description This function is called when the transport channel is
906 * opened while in the opening state. It calls the
907 * application callback with an open indication or open
908 * confirm depending on who initiated the open procedure.
909 *
910 * Returns Nothing.
911 *
912 ******************************************************************************/
avdt_scb_hdl_tc_open_sto(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)913 void avdt_scb_hdl_tc_open_sto(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
914 tAVDT_CTRL avdt_ctrl;
915 /* open reporting channel here, when it is implemented */
916
917 /* call app callback */
918 if (p_data->open.hdr.err_code == AVDT_CHAN_REPORT) {
919 avdt_ctrl.hdr.err_code = 0;
920 avdt_ctrl.hdr.err_param = 1;
921 (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
922 p_scb->p_ccb ? &p_scb->p_ccb->peer_addr : NULL,
923 AVDT_REPORT_CONN_EVT, &avdt_ctrl);
924 }
925 }
926 #endif
927
928 /*******************************************************************************
929 *
930 * Function avdt_scb_hdl_write_req
931 *
932 * Description This function frees the media packet currently stored in
933 * the SCB, if any. Then it builds a new media packet from
934 * with the passed in buffer and stores it in the SCB.
935 *
936 * Returns Nothing.
937 *
938 ******************************************************************************/
avdt_scb_hdl_write_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)939 void avdt_scb_hdl_write_req(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
940 uint8_t* p;
941 uint32_t ssrc;
942 bool add_rtp_header = !(p_data->apiwrite.opt & AVDT_DATA_OPT_NO_RTP);
943
944 /* free packet we're holding, if any; to be replaced with new */
945 if (p_scb->p_pkt != NULL) {
946 /* this shouldn't be happening */
947 AVDT_TRACE_WARNING("Dropped media packet; congested");
948 }
949 osi_free_and_reset((void**)&p_scb->p_pkt);
950
951 /* Recompute only if the RTP header wasn't disabled by the API */
952 if (add_rtp_header) {
953 bool is_content_protection = (p_scb->curr_cfg.num_protect > 0);
954 add_rtp_header =
955 A2DP_UsesRtpHeader(is_content_protection, p_scb->curr_cfg.codec_info);
956 }
957
958 /* Build a media packet, and add an RTP header if required. */
959 if (add_rtp_header) {
960 ssrc = avdt_scb_gen_ssrc(p_scb);
961
962 p_data->apiwrite.p_buf->len += AVDT_MEDIA_HDR_SIZE;
963 p_data->apiwrite.p_buf->offset -= AVDT_MEDIA_HDR_SIZE;
964 p_scb->media_seq++;
965 p = (uint8_t*)(p_data->apiwrite.p_buf + 1) + p_data->apiwrite.p_buf->offset;
966
967 UINT8_TO_BE_STREAM(p, AVDT_MEDIA_OCTET1);
968 UINT8_TO_BE_STREAM(p, p_data->apiwrite.m_pt);
969 UINT16_TO_BE_STREAM(p, p_scb->media_seq);
970 UINT32_TO_BE_STREAM(p, p_data->apiwrite.time_stamp);
971 UINT32_TO_BE_STREAM(p, ssrc);
972 }
973
974 /* store it */
975 p_scb->p_pkt = p_data->apiwrite.p_buf;
976 }
977
978 /*******************************************************************************
979 *
980 * Function avdt_scb_snd_abort_req
981 *
982 * Description This function sends an abort command message.
983 *
984 * Returns Nothing.
985 *
986 ******************************************************************************/
avdt_scb_snd_abort_req(tAVDT_SCB * p_scb,UNUSED_ATTR tAVDT_SCB_EVT * p_data)987 void avdt_scb_snd_abort_req(tAVDT_SCB* p_scb,
988 UNUSED_ATTR tAVDT_SCB_EVT* p_data) {
989 tAVDT_EVT_HDR hdr;
990
991 AVDT_TRACE_DEBUG("%s: p_scb->p_ccb=%p", __func__, p_scb->p_ccb);
992
993 if (p_scb->p_ccb != NULL) {
994 p_scb->role = AVDT_CLOSE_INT;
995
996 hdr.seid = p_scb->peer_seid;
997
998 tAVDT_MSG avdt_msg;
999 avdt_msg.hdr = hdr;
1000 avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_ABORT, &avdt_msg);
1001 }
1002 }
1003
1004 /*******************************************************************************
1005 *
1006 * Function avdt_scb_snd_abort_rsp
1007 *
1008 * Description This function sends an abort response message.
1009 *
1010 * Returns Nothing.
1011 *
1012 ******************************************************************************/
avdt_scb_snd_abort_rsp(UNUSED_ATTR tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1013 void avdt_scb_snd_abort_rsp(UNUSED_ATTR tAVDT_SCB* p_scb,
1014 tAVDT_SCB_EVT* p_data) {
1015 avdt_msg_send_rsp(avdt_ccb_by_idx(p_data->msg.hdr.ccb_idx), AVDT_SIG_ABORT,
1016 &p_data->msg);
1017 }
1018
1019 /*******************************************************************************
1020 *
1021 * Function avdt_scb_snd_close_req
1022 *
1023 * Description This function sends a close command message.
1024 *
1025 * Returns Nothing.
1026 *
1027 ******************************************************************************/
avdt_scb_snd_close_req(tAVDT_SCB * p_scb,UNUSED_ATTR tAVDT_SCB_EVT * p_data)1028 void avdt_scb_snd_close_req(tAVDT_SCB* p_scb,
1029 UNUSED_ATTR tAVDT_SCB_EVT* p_data) {
1030 tAVDT_EVT_HDR hdr;
1031
1032 p_scb->role = AVDT_CLOSE_INT;
1033
1034 hdr.seid = p_scb->peer_seid;
1035
1036 tAVDT_MSG avdt_msg;
1037 avdt_msg.hdr = hdr;
1038 avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_CLOSE, &avdt_msg);
1039 }
1040
1041 /*******************************************************************************
1042 *
1043 * Function avdt_scb_snd_stream_close
1044 *
1045 * Description This function sends a close command message.
1046 *
1047 * Returns Nothing.
1048 *
1049 ******************************************************************************/
avdt_scb_snd_stream_close(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1050 void avdt_scb_snd_stream_close(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
1051 osi_free_and_reset((void**)&p_scb->p_pkt);
1052 avdt_scb_snd_close_req(p_scb, p_data);
1053 }
1054
1055 /*******************************************************************************
1056 *
1057 * Function avdt_scb_snd_close_rsp
1058 *
1059 * Description This function sends a close response message.
1060 *
1061 * Returns Nothing.
1062 *
1063 ******************************************************************************/
avdt_scb_snd_close_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1064 void avdt_scb_snd_close_rsp(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
1065 avdt_msg_send_rsp(p_scb->p_ccb, AVDT_SIG_CLOSE, &p_data->msg);
1066 }
1067
1068 /*******************************************************************************
1069 *
1070 * Function avdt_scb_snd_getconfig_req
1071 *
1072 * Description This function sends a get configuration command message.
1073 *
1074 * Returns Nothing.
1075 *
1076 ******************************************************************************/
avdt_scb_snd_getconfig_req(tAVDT_SCB * p_scb,UNUSED_ATTR tAVDT_SCB_EVT * p_data)1077 void avdt_scb_snd_getconfig_req(tAVDT_SCB* p_scb,
1078 UNUSED_ATTR tAVDT_SCB_EVT* p_data) {
1079 tAVDT_EVT_HDR hdr;
1080
1081 hdr.seid = p_scb->peer_seid;
1082
1083 tAVDT_MSG avdt_msg;
1084 avdt_msg.hdr = hdr;
1085 avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_GETCONFIG, &avdt_msg);
1086 }
1087
1088 /*******************************************************************************
1089 *
1090 * Function avdt_scb_snd_getconfig_rsp
1091 *
1092 * Description This function sends a get configuration response message.
1093 *
1094 * Returns Nothing.
1095 *
1096 ******************************************************************************/
avdt_scb_snd_getconfig_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1097 void avdt_scb_snd_getconfig_rsp(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
1098 avdt_msg_send_rsp(p_scb->p_ccb, AVDT_SIG_GETCONFIG, &p_data->msg);
1099 }
1100
1101 /*******************************************************************************
1102 *
1103 * Function avdt_scb_snd_open_req
1104 *
1105 * Description This function sends an open command message.
1106 *
1107 * Returns Nothing.
1108 *
1109 ******************************************************************************/
avdt_scb_snd_open_req(tAVDT_SCB * p_scb,UNUSED_ATTR tAVDT_SCB_EVT * p_data)1110 void avdt_scb_snd_open_req(tAVDT_SCB* p_scb,
1111 UNUSED_ATTR tAVDT_SCB_EVT* p_data) {
1112 tAVDT_EVT_HDR hdr;
1113
1114 hdr.seid = p_scb->peer_seid;
1115
1116 tAVDT_MSG avdt_msg;
1117 avdt_msg.hdr = hdr;
1118 avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_OPEN, &avdt_msg);
1119 }
1120
1121 /*******************************************************************************
1122 *
1123 * Function avdt_scb_snd_open_rsp
1124 *
1125 * Description This function sends an open response message. It also
1126 * calls avdt_ad_open_req() to accept a transport channel
1127 * connection.
1128 *
1129 * Returns Nothing.
1130 *
1131 ******************************************************************************/
avdt_scb_snd_open_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1132 void avdt_scb_snd_open_rsp(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
1133 /* notify adaption that we're waiting for transport channel open */
1134 p_scb->role = AVDT_OPEN_ACP;
1135 avdt_ad_open_req(AVDT_CHAN_MEDIA, p_scb->p_ccb, p_scb, AVDT_ACP);
1136
1137 /* send response */
1138 avdt_msg_send_rsp(p_scb->p_ccb, AVDT_SIG_OPEN, &p_data->msg);
1139
1140 alarm_set_on_mloop(p_scb->transport_channel_timer,
1141 AVDT_SCB_TC_CONN_TIMEOUT_MS,
1142 avdt_scb_transport_channel_timer_timeout, p_scb);
1143 }
1144
1145 /*******************************************************************************
1146 *
1147 * Function avdt_scb_snd_reconfig_req
1148 *
1149 * Description This function stores the configuration parameters in the
1150 * SCB and sends a reconfiguration command message.
1151 *
1152 * Returns Nothing.
1153 *
1154 ******************************************************************************/
avdt_scb_snd_reconfig_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1155 void avdt_scb_snd_reconfig_req(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
1156 AVDT_TRACE_DEBUG("%s: p_scb->peer_seid=%d p_data->msg.hdr.seid=%d", __func__,
1157 p_scb->peer_seid, p_data->msg.hdr.seid);
1158 A2DP_DumpCodecInfo(p_data->msg.config_cmd.p_cfg->codec_info);
1159
1160 memcpy(&p_scb->req_cfg, p_data->msg.config_cmd.p_cfg, sizeof(tAVDT_CFG));
1161 p_data->msg.hdr.seid = p_scb->peer_seid;
1162 avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_RECONFIG, &p_data->msg);
1163 }
1164
1165 /*******************************************************************************
1166 *
1167 * Function avdt_scb_snd_reconfig_rsp
1168 *
1169 * Description This function stores the configuration parameters in the
1170 * SCB and sends a reconfiguration response message.
1171 *
1172 * Returns Nothing.
1173 *
1174 ******************************************************************************/
avdt_scb_snd_reconfig_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1175 void avdt_scb_snd_reconfig_rsp(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
1176 if (p_data->msg.hdr.err_code == 0) {
1177 /* store new configuration */
1178 if (p_scb->req_cfg.num_codec > 0) {
1179 p_scb->curr_cfg.num_codec = p_scb->req_cfg.num_codec;
1180 memcpy(p_scb->curr_cfg.codec_info, p_scb->req_cfg.codec_info,
1181 AVDT_CODEC_SIZE);
1182 }
1183 if (p_scb->req_cfg.num_protect > 0) {
1184 p_scb->curr_cfg.num_protect = p_scb->req_cfg.num_protect;
1185 memcpy(p_scb->curr_cfg.protect_info, p_scb->req_cfg.protect_info,
1186 AVDT_PROTECT_SIZE);
1187 }
1188
1189 /* send response */
1190 avdt_msg_send_rsp(p_scb->p_ccb, AVDT_SIG_RECONFIG, &p_data->msg);
1191 } else {
1192 /* send reject */
1193 avdt_msg_send_rej(p_scb->p_ccb, AVDT_SIG_RECONFIG, &p_data->msg);
1194 }
1195 }
1196
1197 /*******************************************************************************
1198 *
1199 * Function avdt_scb_snd_security_req
1200 *
1201 * Description This function sends a security command message.
1202 *
1203 * Returns Nothing.
1204 *
1205 ******************************************************************************/
avdt_scb_snd_security_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1206 void avdt_scb_snd_security_req(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
1207 p_data->msg.hdr.seid = p_scb->peer_seid;
1208 avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_SECURITY, &p_data->msg);
1209 }
1210
1211 /*******************************************************************************
1212 *
1213 * Function avdt_scb_snd_security_rsp
1214 *
1215 * Description This function sends a security response message.
1216 *
1217 * Returns Nothing.
1218 *
1219 ******************************************************************************/
avdt_scb_snd_security_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1220 void avdt_scb_snd_security_rsp(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
1221 if (p_data->msg.hdr.err_code == 0) {
1222 avdt_msg_send_rsp(p_scb->p_ccb, AVDT_SIG_SECURITY, &p_data->msg);
1223 } else {
1224 avdt_msg_send_rej(p_scb->p_ccb, AVDT_SIG_SECURITY, &p_data->msg);
1225 }
1226 }
1227
1228 /*******************************************************************************
1229 *
1230 * Function avdt_scb_snd_setconfig_rej
1231 *
1232 * Description This function marks the SCB as not in use and sends a
1233 * set configuration reject message.
1234 *
1235 * Returns Nothing.
1236 *
1237 ******************************************************************************/
avdt_scb_snd_setconfig_rej(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1238 void avdt_scb_snd_setconfig_rej(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
1239 if (p_scb->p_ccb != NULL) {
1240 avdt_msg_send_rej(p_scb->p_ccb, AVDT_SIG_SETCONFIG, &p_data->msg);
1241
1242 /* clear scb variables */
1243 avdt_scb_clr_vars(p_scb, p_data);
1244 }
1245 }
1246
1247 /*******************************************************************************
1248 *
1249 * Function avdt_scb_snd_setconfig_req
1250 *
1251 * Description This function marks the SCB as in use and copies the
1252 * configuration parameters to the SCB. Then the function
1253 * sends a set configuration command message and initiates
1254 * opening of the signaling channel.
1255 *
1256 * Returns Nothing.
1257 *
1258 ******************************************************************************/
avdt_scb_snd_setconfig_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1259 void avdt_scb_snd_setconfig_req(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
1260 tAVDT_CFG *p_req, *p_cfg;
1261
1262 AVDT_TRACE_DEBUG("%s", __func__);
1263 A2DP_DumpCodecInfo(p_data->msg.config_cmd.p_cfg->codec_info);
1264
1265 /* copy API parameters to scb, set scb as in use */
1266 p_scb->in_use = true;
1267 p_scb->p_ccb = avdt_ccb_by_idx(p_data->msg.config_cmd.hdr.ccb_idx);
1268 p_scb->peer_seid = p_data->msg.config_cmd.hdr.seid;
1269 p_req = p_data->msg.config_cmd.p_cfg;
1270 p_cfg = &p_scb->cs.cfg;
1271 memcpy(&p_scb->req_cfg, p_data->msg.config_cmd.p_cfg, sizeof(tAVDT_CFG));
1272
1273 avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_SETCONFIG, &p_data->msg);
1274
1275 /* tell ccb to open channel */
1276 avdt_ccb_event(p_scb->p_ccb, AVDT_CCB_UL_OPEN_EVT, NULL);
1277 }
1278
1279 /*******************************************************************************
1280 *
1281 * Function avdt_scb_snd_setconfig_rsp
1282 *
1283 * Description This function copies the requested configuration into the
1284 * current configuration and sends a set configuration
1285 * response message.
1286 *
1287 * Returns Nothing.
1288 *
1289 ******************************************************************************/
avdt_scb_snd_setconfig_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1290 void avdt_scb_snd_setconfig_rsp(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
1291 if (p_scb->p_ccb != NULL) {
1292 memcpy(&p_scb->curr_cfg, &p_scb->req_cfg, sizeof(tAVDT_CFG));
1293
1294 avdt_msg_send_rsp(p_scb->p_ccb, AVDT_SIG_SETCONFIG, &p_data->msg);
1295 }
1296 }
1297
1298 /*******************************************************************************
1299 *
1300 * Function avdt_scb_snd_tc_close
1301 *
1302 * Description This function calls avdt_ad_close_req() to close the
1303 * transport channel for this SCB.
1304 *
1305 * Returns Nothing.
1306 *
1307 ******************************************************************************/
avdt_scb_snd_tc_close(tAVDT_SCB * p_scb,UNUSED_ATTR tAVDT_SCB_EVT * p_data)1308 void avdt_scb_snd_tc_close(tAVDT_SCB* p_scb,
1309 UNUSED_ATTR tAVDT_SCB_EVT* p_data) {
1310 #if (AVDT_REPORTING == TRUE)
1311 if (p_scb->curr_cfg.psc_mask & AVDT_PSC_REPORT)
1312 avdt_ad_close_req(AVDT_CHAN_REPORT, p_scb->p_ccb, p_scb);
1313 #endif
1314 avdt_ad_close_req(AVDT_CHAN_MEDIA, p_scb->p_ccb, p_scb);
1315 }
1316
1317 /*******************************************************************************
1318 *
1319 * Function avdt_scb_cb_err
1320 *
1321 * Description This function calls the application callback function
1322 * indicating an error.
1323 *
1324 * Returns Nothing.
1325 *
1326 ******************************************************************************/
avdt_scb_cb_err(tAVDT_SCB * p_scb,UNUSED_ATTR tAVDT_SCB_EVT * p_data)1327 void avdt_scb_cb_err(tAVDT_SCB* p_scb, UNUSED_ATTR tAVDT_SCB_EVT* p_data) {
1328 tAVDT_CTRL avdt_ctrl;
1329
1330 /* set error code and parameter */
1331 avdt_ctrl.hdr.err_code = AVDT_ERR_BAD_STATE;
1332 avdt_ctrl.hdr.err_param = 0;
1333
1334 /* call callback, using lookup table to get callback event */
1335 (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb), NULL,
1336 avdt_scb_cback_evt[p_scb->curr_evt], &avdt_ctrl);
1337 }
1338
1339 /*******************************************************************************
1340 *
1341 * Function avdt_scb_cong_state
1342 *
1343 * Description This function sets the congestion state of the SCB media
1344 * transport channel.
1345 *
1346 * Returns Nothing.
1347 *
1348 ******************************************************************************/
avdt_scb_cong_state(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1349 void avdt_scb_cong_state(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
1350 p_scb->cong = p_data->llcong;
1351 }
1352
1353 /*******************************************************************************
1354 *
1355 * Function avdt_scb_rej_state
1356 *
1357 * Description This function sends a reject message to the peer indicating
1358 * incorrect state for the received command message.
1359 *
1360 * Returns Nothing.
1361 *
1362 ******************************************************************************/
avdt_scb_rej_state(UNUSED_ATTR tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1363 void avdt_scb_rej_state(UNUSED_ATTR tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
1364 p_data->msg.hdr.err_code = AVDT_ERR_BAD_STATE;
1365 p_data->msg.hdr.err_param = 0;
1366 avdt_msg_send_rej(avdt_ccb_by_idx(p_data->msg.hdr.ccb_idx),
1367 p_data->msg.hdr.sig_id, &p_data->msg);
1368 }
1369
1370 /*******************************************************************************
1371 *
1372 * Function avdt_scb_rej_in_use
1373 *
1374 * Description This function sends a reject message to the peer indicating
1375 * the stream is in use.
1376 *
1377 * Returns Nothing.
1378 *
1379 ******************************************************************************/
avdt_scb_rej_in_use(UNUSED_ATTR tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1380 void avdt_scb_rej_in_use(UNUSED_ATTR tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
1381 p_data->msg.hdr.err_code = AVDT_ERR_IN_USE;
1382 p_data->msg.hdr.err_param = 0;
1383 avdt_msg_send_rej(avdt_ccb_by_idx(p_data->msg.hdr.ccb_idx),
1384 p_data->msg.hdr.sig_id, &p_data->msg);
1385 }
1386
1387 /*******************************************************************************
1388 *
1389 * Function avdt_scb_rej_not_in_use
1390 *
1391 * Description This function sends a reject message to the peer indicating
1392 * the stream is in use.
1393 *
1394 * Returns Nothing.
1395 *
1396 ******************************************************************************/
avdt_scb_rej_not_in_use(UNUSED_ATTR tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1397 void avdt_scb_rej_not_in_use(UNUSED_ATTR tAVDT_SCB* p_scb,
1398 tAVDT_SCB_EVT* p_data) {
1399 p_data->msg.hdr.err_code = AVDT_ERR_NOT_IN_USE;
1400 p_data->msg.hdr.err_param = 0;
1401 avdt_msg_send_rej(avdt_ccb_by_idx(p_data->msg.hdr.ccb_idx),
1402 p_data->msg.hdr.sig_id, &p_data->msg);
1403 }
1404
1405 /*******************************************************************************
1406 *
1407 * Function avdt_scb_set_remove
1408 *
1409 * Description This function marks an SCB to be removed.
1410 *
1411 * Returns Nothing.
1412 *
1413 ******************************************************************************/
avdt_scb_set_remove(tAVDT_SCB * p_scb,UNUSED_ATTR tAVDT_SCB_EVT * p_data)1414 void avdt_scb_set_remove(tAVDT_SCB* p_scb, UNUSED_ATTR tAVDT_SCB_EVT* p_data) {
1415 p_scb->remove = true;
1416 }
1417
1418 /*******************************************************************************
1419 *
1420 * Function avdt_scb_free_pkt
1421 *
1422 * Description This function frees the media packet passed in.
1423 *
1424 * Returns Nothing.
1425 *
1426 ******************************************************************************/
avdt_scb_free_pkt(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1427 void avdt_scb_free_pkt(tAVDT_SCB* p_scb, tAVDT_SCB_EVT* p_data) {
1428 tAVDT_CTRL avdt_ctrl;
1429
1430 /* set error code and parameter */
1431 avdt_ctrl.hdr.err_code = AVDT_ERR_BAD_STATE;
1432 avdt_ctrl.hdr.err_param = 0;
1433
1434 osi_free_and_reset((void**)&p_data->apiwrite.p_buf);
1435
1436 AVDT_TRACE_WARNING("Dropped media packet");
1437
1438 /* we need to call callback to keep data flow going */
1439 (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb), NULL, AVDT_WRITE_CFM_EVT,
1440 &avdt_ctrl);
1441 }
1442
1443 /*******************************************************************************
1444 *
1445 * Function avdt_scb_clr_pkt
1446 *
1447 * Description This function frees the media packet stored in the SCB.
1448 *
1449 * Returns Nothing.
1450 *
1451 ******************************************************************************/
avdt_scb_clr_pkt(tAVDT_SCB * p_scb,UNUSED_ATTR tAVDT_SCB_EVT * p_data)1452 void avdt_scb_clr_pkt(tAVDT_SCB* p_scb, UNUSED_ATTR tAVDT_SCB_EVT* p_data) {
1453 tAVDT_CTRL avdt_ctrl;
1454 tAVDT_CCB* p_ccb;
1455 uint8_t tcid;
1456 uint16_t lcid;
1457
1458 /* set error code and parameter */
1459 avdt_ctrl.hdr.err_code = AVDT_ERR_BAD_STATE;
1460 avdt_ctrl.hdr.err_param = 0;
1461 /* flush the media data queued at L2CAP */
1462 p_ccb = p_scb->p_ccb;
1463 if (p_ccb != NULL) {
1464 /* get tcid from type, scb */
1465 tcid = avdt_ad_type_to_tcid(AVDT_CHAN_MEDIA, p_scb);
1466
1467 lcid = avdt_cb.ad.rt_tbl[avdt_ccb_to_idx(p_ccb)][tcid].lcid;
1468 L2CA_FlushChannel(lcid, L2CAP_FLUSH_CHANS_ALL);
1469 }
1470
1471 if (p_scb->p_pkt != NULL) {
1472 osi_free_and_reset((void**)&p_scb->p_pkt);
1473
1474 AVDT_TRACE_DEBUG("Dropped stored media packet");
1475
1476 /* we need to call callback to keep data flow going */
1477 (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb), NULL, AVDT_WRITE_CFM_EVT,
1478 &avdt_ctrl);
1479 }
1480 }
1481
1482 /*******************************************************************************
1483 *
1484 * Function avdt_scb_chk_snd_pkt
1485 *
1486 * Description This function checks if the SCB is congested, and if not
1487 * congested it sends a stored media packet, if any. After it
1488 * sends the packet it calls the application callback function
1489 * with a write confirm.
1490 *
1491 * Returns Nothing.
1492 *
1493 ******************************************************************************/
avdt_scb_chk_snd_pkt(tAVDT_SCB * p_scb,UNUSED_ATTR tAVDT_SCB_EVT * p_data)1494 void avdt_scb_chk_snd_pkt(tAVDT_SCB* p_scb, UNUSED_ATTR tAVDT_SCB_EVT* p_data) {
1495 tAVDT_CTRL avdt_ctrl;
1496 BT_HDR* p_pkt;
1497
1498 avdt_ctrl.hdr.err_code = 0;
1499
1500 if (!p_scb->cong) {
1501 if (p_scb->p_pkt != NULL) {
1502 p_pkt = p_scb->p_pkt;
1503 p_scb->p_pkt = NULL;
1504 avdt_ad_write_req(AVDT_CHAN_MEDIA, p_scb->p_ccb, p_scb, p_pkt);
1505
1506 (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb), NULL,
1507 AVDT_WRITE_CFM_EVT, &avdt_ctrl);
1508 }
1509 }
1510 }
1511
1512 /*******************************************************************************
1513 *
1514 * Function avdt_scb_transport_channel_timer
1515 *
1516 * Description This function is called to start a timer when the peer
1517 * initiates closing of the stream. The timer verifies that
1518 * the peer disconnects the transport channel.
1519 *
1520 * Returns Nothing.
1521 *
1522 ******************************************************************************/
avdt_scb_transport_channel_timer(tAVDT_SCB * p_scb,UNUSED_ATTR tAVDT_SCB_EVT * p_data)1523 void avdt_scb_transport_channel_timer(tAVDT_SCB* p_scb,
1524 UNUSED_ATTR tAVDT_SCB_EVT* p_data) {
1525 alarm_set_on_mloop(p_scb->transport_channel_timer,
1526 AVDT_SCB_TC_DISC_TIMEOUT_MS,
1527 avdt_scb_transport_channel_timer_timeout, p_scb);
1528 }
1529
1530 /*******************************************************************************
1531 *
1532 * Function avdt_scb_clr_vars
1533 *
1534 * Description This function initializes certain SCB variables.
1535 *
1536 * Returns Nothing.
1537 *
1538 ******************************************************************************/
avdt_scb_clr_vars(tAVDT_SCB * p_scb,UNUSED_ATTR tAVDT_SCB_EVT * p_data)1539 void avdt_scb_clr_vars(tAVDT_SCB* p_scb, UNUSED_ATTR tAVDT_SCB_EVT* p_data) {
1540 p_scb->in_use = false;
1541 p_scb->p_ccb = NULL;
1542 p_scb->peer_seid = 0;
1543 }
1544