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