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 <string.h>
27 #include "stack/bt_types.h"
28 #include "common/bt_target.h"
29 #include "common/bt_defs.h"
30 #include "stack/avdt_api.h"
31 #include "stack/avdtc_api.h"
32 #include "avdt_int.h"
33 #include "stack/btu.h"
34 #include "osi/allocator.h"
35 
36 #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
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 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 /* This table is used to look up the callback event based on the signaling
57 ** role when the stream is closed.
58 */
59 const UINT8 avdt_scb_role_evt[] = {
60     AVDT_CLOSE_IND_EVT,         /* AVDT_CLOSE_ACP */
61     AVDT_CLOSE_CFM_EVT,         /* AVDT_CLOSE_INT */
62     AVDT_CLOSE_IND_EVT,         /* AVDT_OPEN_ACP */
63     AVDT_OPEN_CFM_EVT           /* AVDT_OPEN_INT */
64 };
65 
66 /*******************************************************************************
67 **
68 ** Function         avdt_scb_gen_ssrc
69 **
70 ** Description      This function generates a SSRC number unique to the stream.
71 **
72 ** Returns          SSRC value.
73 **
74 *******************************************************************************/
avdt_scb_gen_ssrc(tAVDT_SCB * p_scb)75 UINT32 avdt_scb_gen_ssrc(tAVDT_SCB *p_scb)
76 {
77     /* combine the value of the media type and codec type of the SCB */
78     return ((UINT32)(p_scb->cs.cfg.codec_info[1] | p_scb->cs.cfg.codec_info[2]));
79 }
80 
81 /*******************************************************************************
82 **
83 ** Function         avdt_scb_hdl_abort_cmd
84 **
85 ** Description      This function sends the SCB an AVDT_SCB_API_ABORT_RSP_EVT
86 **                  to initiate sending of an abort response message.
87 **
88 ** Returns          Nothing.
89 **
90 *******************************************************************************/
avdt_scb_hdl_abort_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)91 void avdt_scb_hdl_abort_cmd(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
92 {
93     p_scb->role = AVDT_CLOSE_ACP;
94     avdt_scb_event(p_scb, AVDT_SCB_API_ABORT_RSP_EVT, p_data);
95 }
96 
97 /*******************************************************************************
98 **
99 ** Function         avdt_scb_hdl_abort_rsp
100 **
101 ** Description      This function is an empty function; it serves as a
102 **                  placeholder for a conformance API action function.
103 **
104 ** Returns          Nothing.
105 **
106 *******************************************************************************/
avdt_scb_hdl_abort_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)107 void avdt_scb_hdl_abort_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
108 {
109     UNUSED(p_scb);
110     UNUSED(p_data);
111     return;
112 }
113 
114 /*******************************************************************************
115 **
116 ** Function         avdt_scb_hdl_close_cmd
117 **
118 ** Description      This function sends the SCB an AVDT_SCB_API_CLOSE_RSP_EVT
119 **                  to initiate sending of a close response message.
120 **
121 ** Returns          Nothing.
122 **
123 *******************************************************************************/
avdt_scb_hdl_close_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)124 void avdt_scb_hdl_close_cmd(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
125 {
126     p_scb->role = AVDT_CLOSE_ACP;
127     avdt_scb_event(p_scb, AVDT_SCB_API_CLOSE_RSP_EVT, p_data);
128 }
129 
130 /*******************************************************************************
131 **
132 ** Function         avdt_scb_hdl_close_rsp
133 **
134 ** Description      This function sets the close_code variable to the error
135 **                  code returned in the close response.
136 **
137 ** Returns          Nothing.
138 **
139 *******************************************************************************/
avdt_scb_hdl_close_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)140 void avdt_scb_hdl_close_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
141 {
142     p_scb->close_code = p_data->msg.hdr.err_code;
143 }
144 
145 /*******************************************************************************
146 **
147 ** Function         avdt_scb_hdl_getconfig_cmd
148 **
149 ** Description      This function retrieves the configuration parameters of
150 **                  the SCB and sends the SCB an AVDT_SCB_API_GETCONFIG_RSP_EVT
151 **                  to initiate sending of a get configuration response message.
152 **
153 ** Returns          Nothing.
154 **
155 *******************************************************************************/
avdt_scb_hdl_getconfig_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)156 void avdt_scb_hdl_getconfig_cmd(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
157 {
158     p_data->msg.svccap.p_cfg = &p_scb->curr_cfg;
159 
160     avdt_scb_event(p_scb, AVDT_SCB_API_GETCONFIG_RSP_EVT, p_data);
161 }
162 
163 /*******************************************************************************
164 **
165 ** Function         avdt_scb_hdl_getconfig_rsp
166 **
167 ** Description      This function is an empty function; it serves as a
168 **                  placeholder for a conformance API action function.
169 **
170 ** Returns          Nothing.
171 **
172 *******************************************************************************/
avdt_scb_hdl_getconfig_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)173 void avdt_scb_hdl_getconfig_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
174 {
175     UNUSED(p_scb);
176     UNUSED(p_data);
177     return;
178 }
179 
180 /*******************************************************************************
181 **
182 ** Function         avdt_scb_hdl_open_cmd
183 **
184 ** Description      This function sends the SCB an AVDT_SCB_API_OPEN_RSP_EVT
185 **                  to initiate sending of an open response message.
186 **
187 ** Returns          Nothing.
188 **
189 *******************************************************************************/
avdt_scb_hdl_open_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)190 void avdt_scb_hdl_open_cmd(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
191 {
192     avdt_scb_event(p_scb, AVDT_SCB_API_OPEN_RSP_EVT, p_data);
193 }
194 
195 /*******************************************************************************
196 **
197 ** Function         avdt_scb_hdl_open_rej
198 **
199 ** Description      This function calls the application callback function
200 **                  indicating the open request has failed.  It initializes
201 **                  certain SCB variables and sends a AVDT_CCB_UL_CLOSE_EVT
202 **                  to the CCB.
203 **
204 ** Returns          Nothing.
205 **
206 *******************************************************************************/
avdt_scb_hdl_open_rej(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)207 void avdt_scb_hdl_open_rej(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
208 {
209     /* do exactly same as setconfig reject */
210     avdt_scb_hdl_setconfig_rej(p_scb, p_data);
211 }
212 
213 /*******************************************************************************
214 **
215 ** Function         avdt_scb_hdl_open_rsp
216 **
217 ** Description      This function calls avdt_ad_open_req() to initiate
218 **                  connection of the transport channel for this stream.
219 **
220 ** Returns          Nothing.
221 **
222 *******************************************************************************/
avdt_scb_hdl_open_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)223 void avdt_scb_hdl_open_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
224 {
225     UNUSED(p_data);
226 
227     /* initiate opening of trans channels for this SEID */
228     p_scb->role = AVDT_OPEN_INT;
229     avdt_ad_open_req(AVDT_CHAN_MEDIA, p_scb->p_ccb, p_scb, AVDT_INT);
230 
231     /* start tc connect timer */
232     btu_start_timer(&p_scb->timer_entry, BTU_TTYPE_AVDT_SCB_TC, AVDT_SCB_TC_CONN_TOUT);
233 }
234 
235 /*******************************************************************************
236 **
237 ** Function         avdt_scb_hdl_pkt_no_frag
238 **
239 ** Description
240 **
241 ** Returns          Nothing.
242 **
243 *******************************************************************************/
avdt_scb_hdl_pkt_no_frag(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)244 void avdt_scb_hdl_pkt_no_frag(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
245 {
246     UINT8   *p, *p_start;
247     UINT8   o_v, o_p, o_x, o_cc;
248     UINT8   m_pt;
249     UINT8   marker;
250     UINT16  seq;
251     UINT32  time_stamp;
252     UINT16  offset;
253     UINT16  ex_len;
254     UINT8   pad_len = 0;
255 
256     p = p_start = (UINT8 *)(p_data->p_pkt + 1) + p_data->p_pkt->offset;
257 
258     /* parse media packet header */
259     AVDT_MSG_PRS_OCTET1(p, o_v, o_p, o_x, o_cc);
260     AVDT_MSG_PRS_M_PT(p, m_pt, marker);
261     BE_STREAM_TO_UINT16(seq, p);
262     BE_STREAM_TO_UINT32(time_stamp, p);
263     p += 4;
264 
265     UNUSED(o_v);
266 
267     /* skip over any csrc's in packet */
268     p += o_cc * 4;
269 
270     /* check for and skip over extension header */
271     if (o_x) {
272         p += 2;
273         BE_STREAM_TO_UINT16(ex_len, p);
274         p += ex_len * 4;
275     }
276 
277     /* save our new offset */
278     offset = (UINT16) (p - p_start);
279 
280     /* adjust length for any padding at end of packet */
281     if (o_p) {
282         /* padding length in last byte of packet */
283         pad_len =  *(p_start + p_data->p_pkt->len);
284     }
285 
286     /* do sanity check */
287     if ((offset > p_data->p_pkt->len) || ((pad_len + offset) > p_data->p_pkt->len)) {
288         AVDT_TRACE_WARNING("Got bad media packet");
289         osi_free(p_data->p_pkt);
290         p_data->p_pkt = NULL;
291     }
292     /* adjust offset and length and send it up */
293     else {
294         p_data->p_pkt->len -= (offset + pad_len);
295         p_data->p_pkt->offset += offset;
296 
297         if (p_scb->cs.p_data_cback != NULL) {
298             /* report sequence number */
299             p_data->p_pkt->layer_specific = seq;
300             (*p_scb->cs.p_data_cback)(avdt_scb_to_hdl(p_scb), p_data->p_pkt,
301                                       time_stamp, (UINT8)(m_pt | (marker << 7)));
302         } else {
303 #if AVDT_MULTIPLEXING == TRUE
304             if ((p_scb->cs.p_media_cback != NULL)
305                     && (p_scb->p_media_buf != NULL)
306                     && (p_scb->media_buf_len > p_data->p_pkt->len)) {
307                 /* media buffer enough length is assigned by application. Lets use it*/
308                 memcpy(p_scb->p_media_buf, (UINT8 *)(p_data->p_pkt + 1) + p_data->p_pkt->offset,
309                        p_data->p_pkt->len);
310                 (*p_scb->cs.p_media_cback)(avdt_scb_to_hdl(p_scb), p_scb->p_media_buf,
311                                            p_scb->media_buf_len, time_stamp, seq, m_pt, marker);
312             }
313 #endif
314             osi_free(p_data->p_pkt);
315             p_data->p_pkt = NULL;
316         }
317     }
318 }
319 
320 #if AVDT_REPORTING == TRUE
321 /*******************************************************************************
322 **
323 ** Function         avdt_scb_hdl_report
324 **
325 ** Description
326 **
327 ** Returns          Nothing.
328 **
329 *******************************************************************************/
avdt_scb_hdl_report(tAVDT_SCB * p_scb,UINT8 * p,UINT16 len)330 UINT8 *avdt_scb_hdl_report(tAVDT_SCB *p_scb, UINT8 *p, UINT16 len)
331 {
332     UINT16  result = AVDT_SUCCESS;
333     UINT8   *p_start = p;
334     UINT32  ssrc;
335     UINT8   o_v, o_p, o_cc;
336     AVDT_REPORT_TYPE    pt;
337     tAVDT_REPORT_DATA   report, *p_rpt;
338 
339     AVDT_TRACE_DEBUG( "avdt_scb_hdl_report");
340     if (p_scb->cs.p_report_cback) {
341         p_rpt = &report;
342         /* parse report packet header */
343         AVDT_MSG_PRS_RPT_OCTET1(p, o_v, o_p, o_cc);
344         pt = *p++;
345         p += 2;
346         BE_STREAM_TO_UINT32(ssrc, p);
347 
348         UNUSED(ssrc);
349         UNUSED(o_p);
350         UNUSED(o_v);
351         UNUSED(o_cc);
352 
353         switch (pt) {
354         case AVDT_RTCP_PT_SR:   /* the packet type - SR (Sender Report) */
355             BE_STREAM_TO_UINT32(report.sr.ntp_sec, p);
356             BE_STREAM_TO_UINT32(report.sr.ntp_frac, p);
357             BE_STREAM_TO_UINT32(report.sr.rtp_time, p);
358             BE_STREAM_TO_UINT32(report.sr.pkt_count, p);
359             BE_STREAM_TO_UINT32(report.sr.octet_count, p);
360             break;
361 
362         case AVDT_RTCP_PT_RR:   /* the packet type - RR (Receiver Report) */
363             report.rr.frag_lost = *p;
364             BE_STREAM_TO_UINT32(report.rr.packet_lost, p);
365             report.rr.packet_lost &= 0xFFFFFF;
366             BE_STREAM_TO_UINT32(report.rr.seq_num_rcvd, p);
367             BE_STREAM_TO_UINT32(report.rr.jitter, p);
368             BE_STREAM_TO_UINT32(report.rr.lsr, p);
369             BE_STREAM_TO_UINT32(report.rr.dlsr, p);
370             break;
371 
372         case AVDT_RTCP_PT_SDES: /* the packet type - SDES (Source Description) */
373             if (*p == AVDT_RTCP_SDES_CNAME) {
374                 p_rpt = (tAVDT_REPORT_DATA *)(p + 2);
375             } else {
376                 AVDT_TRACE_WARNING( " - SDES SSRC=0x%08x sc=%d %d len=%d %s\n",
377                                     ssrc, o_cc, *p, *(p + 1), p + 2);
378                 result = AVDT_BUSY;
379             }
380             break;
381 
382         default:
383             AVDT_TRACE_ERROR( "Bad Report pkt - packet type: %d\n", pt);
384             result = AVDT_BAD_PARAMS;
385         }
386 
387         if (result == AVDT_SUCCESS) {
388             (*p_scb->cs.p_report_cback)(avdt_scb_to_hdl(p_scb), pt, p_rpt);
389         }
390 
391     }
392     p_start += len;
393     return p_start;
394 }
395 #endif
396 
397 #if AVDT_MULTIPLEXING == TRUE
398 /*******************************************************************************
399 **
400 ** Function         avdt_scb_hdl_pkt_frag
401 **
402 ** Description
403 **
404 ** Returns          Nothing.
405 **
406 *******************************************************************************/
avdt_scb_hdl_pkt_frag(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)407 void avdt_scb_hdl_pkt_frag(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
408 {
409     /* Fields of Adaptation Layer Header */
410     UINT8   al_tsid, al_frag, al_lcode;
411     UINT16  al_len;
412     /* media header fields */
413     UINT8   o_v, o_p, o_x, o_cc;
414     UINT8   m_pt;
415     UINT8   marker;
416     UINT16  seq;
417     UINT32  time_stamp;
418     UINT32  ssrc;
419     UINT16  ex_len;
420     UINT8   pad_len;
421     /* other variables */
422     UINT8   *p; /* current pointer */
423     UINT8   *p_end; /* end of all packet */
424     UINT8   *p_payload; /* pointer to media fragment payload in the buffer */
425     UINT32  payload_len; /* payload length */
426     UINT16  frag_len; /* fragment length */
427 
428     p = (UINT8 *)(p_data->p_pkt + 1) + p_data->p_pkt->offset;
429     p_end = p + p_data->p_pkt->len;
430     /* parse all fragments */
431     while (p < p_end) {
432         if (p_end - p < 4) { /* length check. maximum length of AL header = 4 */
433             AVDT_TRACE_WARNING("p_end: %p - p:%p < 4\n", p_end, p);
434             break;
435         }
436 
437         /* parse first byte */
438         al_tsid = (*p) >> 3;
439         al_frag = ( (*p) >> 2 ) & 0x01;
440         al_lcode = (*p++) & AVDT_ALH_LCODE_MASK;
441 
442         /* in case of TSID=00000, a second AL header byte, before the length field,
443         ** is expected and contains the actual TSID, aligned with MSB */
444         if (al_tsid == 0) {
445             al_tsid = *p++;
446         }
447 
448         /* get remaining media length on base of lcode */
449         switch (al_lcode) {
450         case AVDT_ALH_LCODE_NONE:  /* No length field present. Take length from l2cap */
451             al_len = (UINT16)(p_end - p);
452             break;
453         case AVDT_ALH_LCODE_16BIT:  /* 16 bit length field */
454             BE_STREAM_TO_UINT16(al_len, p);
455             break;
456         case AVDT_ALH_LCODE_9BITM0:  /* 9 bit length field, MSB = 0, 8 LSBs in 1 octet following */
457             al_len = *p++;
458             break;
459         default:    /* 9 bit length field, MSB = 1, 8 LSBs in 1 octet following */
460             al_len = (UINT16) * p++ + 0x100;
461         }
462 
463         /* max fragment length */
464         frag_len = (UINT16)(p_end - p);
465         /* if it isn't last fragment */
466         if (frag_len >= al_len) {
467             frag_len = al_len;
468         }
469 
470         /* check TSID corresponds to config */
471         if (al_tsid != p_scb->curr_cfg.mux_tsid_media) {
472 #if AVDT_REPORTING == TRUE
473             if ((p_scb->curr_cfg.psc_mask & AVDT_PSC_REPORT) &&
474                     (al_tsid == p_scb->curr_cfg.mux_tsid_report)) {
475                 /* parse reporting packet */
476                 p = avdt_scb_hdl_report(p_scb, p, frag_len);
477                 continue;
478             } else
479 #endif
480             {
481                 AVDT_TRACE_WARNING("bad tsid: %d, mux_tsid_media:%d\n", al_tsid, p_scb->curr_cfg.mux_tsid_media);
482                 break;
483             }
484         }
485         /* check are buffer for assembling and related callback set */
486         else if ((p_scb->p_media_buf == NULL) || (p_scb->cs.p_media_cback == NULL)) {
487             AVDT_TRACE_WARNING("NULL p_media_buf or p_media_cback");
488             break;
489         }
490 
491 
492         /* it is media fragment beginning */
493         if (!al_frag) { /* is it first fragment of original media packet */
494             AVDT_TRACE_DEBUG("al:%d media:%d\n",
495                              al_len, p_scb->media_buf_len);
496 
497             p_scb->frag_off = 0;
498             p_scb->frag_org_len = al_len; /* total length of original media packet */
499             /* length check: minimum length of media header is 12 */
500             if (p_scb->frag_org_len < 12) {
501                 AVDT_TRACE_WARNING("bad al_len: %d(<12)\n", al_len);
502                 break;
503             }
504             /* check that data fit into buffer */
505             if (al_len > p_scb->media_buf_len) {
506                 AVDT_TRACE_WARNING("bad al_len: %d(>%d)\n", al_len, p_scb->media_buf_len);
507                 break;
508             }
509             /* make sure it is the last fragment in l2cap packet */
510             if (p + al_len < p_end) {
511                 AVDT_TRACE_WARNING("bad al_len: %d(>%d)\n", al_len, p_scb->media_buf_len);
512                 break;
513             }
514         } else {
515             AVDT_TRACE_DEBUG("al:%d media:%d frag_org_len:%d frag_off:%d\n",
516                              al_len, p_scb->media_buf_len, p_scb->frag_org_len, p_scb->frag_off);
517 
518             /* check that remaining length from AL header equals to original len - length of already received fragments */
519             if (al_len != p_scb->frag_org_len - p_scb->frag_off) {
520                 AVDT_TRACE_WARNING("al_len:%d != (frag_org_len:%d - frag_off:%d) %d\n",
521                                    al_len, p_scb->frag_org_len, p_scb->frag_off,
522                                    (p_scb->frag_org_len - p_scb->frag_off));
523                 break;
524             }
525 
526             /* do sanity check */
527             if (p_scb->frag_off == 0) {
528                 AVDT_TRACE_WARNING("frag_off=0");
529                 break;
530             }
531         }
532         /* do common sanity check */
533         if ((p_scb->frag_org_len <= p_scb->frag_off) || (p_scb->frag_org_len >= p_scb->media_buf_len)) {
534             AVDT_TRACE_WARNING("common sanity frag_off:%d frag_org_len:%d media_buf_len:%d\n",
535                                p_scb->frag_off, p_scb->frag_org_len, p_scb->media_buf_len);
536             break;
537         }
538 
539         AVDT_TRACE_DEBUG("Received fragment org_len=%d off=%d al_len=%d frag_len=%d\n",
540                          p_scb->frag_org_len, p_scb->frag_off, al_len, frag_len);
541 
542         /* copy fragment into buffer */
543         memcpy(p_scb->p_media_buf + p_scb->frag_off, p, frag_len);
544         p_scb->frag_off += frag_len;
545         /* move to the next fragment */
546         p += frag_len;
547         /* if it is last fragment in original media packet then process total media pocket */
548         if (p_scb->frag_off == p_scb->frag_org_len) {
549             p_payload = p_scb->p_media_buf;
550 
551             /* media header */
552             AVDT_MSG_PRS_OCTET1(p_payload, o_v, o_p, o_x, o_cc);
553             AVDT_MSG_PRS_M_PT(p_payload, m_pt, marker);
554             BE_STREAM_TO_UINT16(seq, p_payload);
555             BE_STREAM_TO_UINT32(time_stamp, p_payload);
556             BE_STREAM_TO_UINT32(ssrc, p_payload);
557 
558             UNUSED(o_v);
559             UNUSED(ssrc);
560 
561             /* skip over any csrc's in packet */
562             p_payload += o_cc * 4;
563 
564             /* check for and skip over extension header */
565             if (o_x) {
566                 if (p_scb->p_media_buf + p_scb->frag_off - p_payload < 4) {
567                     AVDT_TRACE_WARNING("length check frag_off:%d p_media_buf:%p p_payload:%p\n",
568                                        p_scb->frag_off, p_scb->p_media_buf, p_payload);
569                     break;/* length check */
570                 }
571                 p_payload += 2;
572                 BE_STREAM_TO_UINT16(ex_len, p_payload);
573                 p_payload += ex_len * 4;
574             }
575 
576             if (p_payload >= p_scb->p_media_buf + p_scb->frag_off) {
577                 AVDT_TRACE_WARNING("length check2 frag_off:%d p_media_buf:%p p_payload:%p\n",
578                                    p_scb->frag_off, p_scb->p_media_buf, p_payload);
579                 break;/* length check */
580             }
581 
582             /* adjust length for any padding at end of packet */
583             if (o_p) {
584                 /* padding length in last byte of packet */
585                 pad_len =  *(p_scb->p_media_buf + p_scb->frag_off - 1);
586             } else {
587                 pad_len =  0;
588             }
589             /* payload length */
590             payload_len = (UINT32)(p_scb->p_media_buf + p_scb->frag_off - pad_len - p_payload);
591 
592             AVDT_TRACE_DEBUG("Received last fragment header=%d len=%d\n",
593                              p_payload - p_scb->p_media_buf, payload_len);
594 
595             /* send total media packet up */
596             if (p_scb->cs.p_media_cback != NULL) {
597                 (*p_scb->cs.p_media_cback)(avdt_scb_to_hdl(p_scb), p_payload,
598                                            payload_len, time_stamp, seq, m_pt, marker);
599             }
600         }
601     } /* while(p < p_end) */
602 
603     if (p < p_end) {
604         AVDT_TRACE_WARNING("*** Got bad media packet");
605     }
606     osi_free(p_data->p_pkt);
607     p_data->p_pkt = NULL;
608 }
609 #endif
610 
611 /*******************************************************************************
612 **
613 ** Function         avdt_scb_hdl_pkt
614 **
615 ** Description
616 **
617 ** Returns          Nothing.
618 **
619 *******************************************************************************/
avdt_scb_hdl_pkt(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)620 void avdt_scb_hdl_pkt(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
621 {
622 #if AVDT_REPORTING == TRUE
623     UINT8 *p;
624 #endif
625 
626 #if AVDT_MULTIPLEXING == TRUE
627     /* select right function in dependance of is fragmentation supported or not */
628     if ( 0 != (p_scb->curr_cfg.psc_mask & AVDT_PSC_MUX)) {
629         avdt_scb_hdl_pkt_frag(p_scb, p_data);
630     } else
631 #endif
632     {
633 #if AVDT_REPORTING == TRUE
634         if (p_data->p_pkt->layer_specific == AVDT_CHAN_REPORT) {
635             p = (UINT8 *)(p_data->p_pkt + 1) + p_data->p_pkt->offset;
636             avdt_scb_hdl_report(p_scb, p, p_data->p_pkt->len);
637             osi_free(p_data->p_pkt);
638             p_data->p_pkt = NULL;
639         } else
640 #endif
641         {
642             avdt_scb_hdl_pkt_no_frag(p_scb, p_data);
643         }
644     }
645 }
646 
647 /*******************************************************************************
648 **
649 ** Function         avdt_scb_drop_pkt
650 **
651 ** Description      Drop an incoming media packet.  This function is called if
652 **                  a media packet is received in any state besides streaming.
653 **
654 ** Returns          Nothing.
655 **
656 *******************************************************************************/
avdt_scb_drop_pkt(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)657 void avdt_scb_drop_pkt(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
658 {
659     UNUSED(p_scb);
660 
661     AVDT_TRACE_ERROR("%s dropped incoming media packet", __func__);
662     osi_free(p_data->p_pkt);
663     p_data->p_pkt = NULL;
664 }
665 
666 /*******************************************************************************
667 **
668 ** Function         avdt_scb_hdl_reconfig_cmd
669 **
670 ** Description      This function calls the application callback function
671 **                  with a reconfiguration indication.
672 **
673 ** Returns          Nothing.
674 **
675 *******************************************************************************/
avdt_scb_hdl_reconfig_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)676 void avdt_scb_hdl_reconfig_cmd(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
677 {
678     /* if command not supported */
679     if (p_scb->cs.nsc_mask & AVDT_NSC_RECONFIG) {
680         /* send reject */
681         p_data->msg.hdr.err_code = AVDT_ERR_NSC;
682         p_data->msg.hdr.err_param = 0;
683         avdt_scb_event(p_scb, AVDT_SCB_API_RECONFIG_RSP_EVT, p_data);
684     } else {
685         /* store requested configuration */
686         memcpy(&p_scb->req_cfg, p_data->msg.reconfig_cmd.p_cfg, sizeof(tAVDT_CFG));
687 
688         /* call application callback */
689         (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
690                                   NULL,
691                                   AVDT_RECONFIG_IND_EVT,
692                                   (tAVDT_CTRL *) &p_data->msg.reconfig_cmd);
693     }
694 }
695 
696 /*******************************************************************************
697 **
698 ** Function         avdt_scb_hdl_reconfig_rsp
699 **
700 ** Description      This function calls the application callback function
701 **                  with a reconfiguration confirm.
702 **
703 ** Returns          Nothing.
704 **
705 *******************************************************************************/
avdt_scb_hdl_reconfig_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)706 void avdt_scb_hdl_reconfig_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
707 {
708     if (p_data->msg.hdr.err_code == 0) {
709         /* store new configuration */
710         if (p_scb->req_cfg.num_codec > 0) {
711             p_scb->curr_cfg.num_codec = p_scb->req_cfg.num_codec;
712             memcpy(p_scb->curr_cfg.codec_info, p_scb->req_cfg.codec_info, AVDT_CODEC_SIZE);
713         }
714         if (p_scb->req_cfg.num_protect > 0) {
715             p_scb->curr_cfg.num_protect = p_scb->req_cfg.num_protect;
716             memcpy(p_scb->curr_cfg.protect_info, p_scb->req_cfg.protect_info, AVDT_PROTECT_SIZE);
717         }
718     }
719 
720     p_data->msg.svccap.p_cfg = &p_scb->curr_cfg;
721 
722     /* call application callback */
723     (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
724                               NULL,
725                               AVDT_RECONFIG_CFM_EVT,
726                               (tAVDT_CTRL *) &p_data->msg.svccap);
727 }
728 
729 /*******************************************************************************
730 **
731 ** Function         avdt_scb_hdl_security_cmd
732 **
733 ** Description      This function calls the application callback with a
734 **                  security indication.
735 **
736 ** Returns          Nothing.
737 **
738 *******************************************************************************/
avdt_scb_hdl_security_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)739 void avdt_scb_hdl_security_cmd(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
740 {
741     /* if command not supported */
742     if (p_scb->cs.nsc_mask & AVDT_NSC_SECURITY) {
743         /* send reject */
744         p_data->msg.hdr.err_code = AVDT_ERR_NSC;
745         avdt_scb_event(p_scb, AVDT_SCB_API_SECURITY_RSP_EVT, p_data);
746     } else {
747         /* call application callback */
748         (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
749                                   NULL,
750                                   AVDT_SECURITY_IND_EVT,
751                                   (tAVDT_CTRL *) &p_data->msg.security_cmd);
752     }
753 }
754 
755 /*******************************************************************************
756 **
757 ** Function         avdt_scb_hdl_security_rsp
758 **
759 ** Description      This function calls the application callback with a
760 **                  security confirm.
761 **
762 ** Returns          Nothing.
763 **
764 *******************************************************************************/
avdt_scb_hdl_security_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)765 void avdt_scb_hdl_security_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
766 {
767     /* call application callback */
768     (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
769                               NULL,
770                               AVDT_SECURITY_CFM_EVT,
771                               (tAVDT_CTRL *) &p_data->msg.security_cmd);
772 }
773 
774 /*******************************************************************************
775 **
776 ** Function         avdt_scb_hdl_setconfig_cmd
777 **
778 ** Description      This function marks the SCB as in use and copies the
779 **                  configuration and peer SEID to the SCB.  It then calls
780 **                  the application callback with a configuration indication.
781 **
782 ** Returns          Nothing.
783 **
784 *******************************************************************************/
avdt_scb_hdl_setconfig_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)785 void avdt_scb_hdl_setconfig_cmd(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
786 {
787     tAVDT_CFG *p_cfg;
788 
789     if (!p_scb->in_use) {
790         p_cfg = p_data->msg.config_cmd.p_cfg;
791         /* set sep as in use */
792         p_scb->in_use = TRUE;
793 
794         /* copy info to scb */
795         p_scb->p_ccb = avdt_ccb_by_idx(p_data->msg.config_cmd.hdr.ccb_idx);
796         p_scb->peer_seid = p_data->msg.config_cmd.int_seid;
797         memcpy(&p_scb->req_cfg, p_cfg, sizeof(tAVDT_CFG));
798         /* call app callback */
799         (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb), /* handle of scb- which is same as sep handle of bta_av_cb.p_scb*/
800                                     p_scb->p_ccb ? p_scb->p_ccb->peer_addr : NULL,
801                                     AVDT_CONFIG_IND_EVT,
802                                     (tAVDT_CTRL *) &p_data->msg.config_cmd);
803     } else {
804         avdt_scb_rej_in_use(p_scb, p_data);
805     }
806 }
807 
808 /*******************************************************************************
809 **
810 ** Function         avdt_scb_hdl_setconfig_rej
811 **
812 ** Description      This function marks the SCB as not in use and calls the
813 **                  application callback with an open confirm indicating failure.
814 **
815 ** Returns          Nothing.
816 **
817 *******************************************************************************/
avdt_scb_hdl_setconfig_rej(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)818 void avdt_scb_hdl_setconfig_rej(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
819 {
820     /* clear scb variables */
821     avdt_scb_clr_vars(p_scb, p_data);
822 
823     /* tell ccb we're done with signaling channel */
824     avdt_ccb_event(avdt_ccb_by_idx(p_data->msg.hdr.ccb_idx), AVDT_CCB_UL_CLOSE_EVT, NULL);
825 
826     /* call application callback */
827     (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
828                               NULL,
829                               AVDT_OPEN_CFM_EVT,
830                               (tAVDT_CTRL *) &p_data->msg.hdr);
831 }
832 
833 /*******************************************************************************
834 **
835 ** Function         avdt_scb_send_delay_report_cmd
836 **
837 ** Description      This function is to initiate the delay reporting command.
838 **
839 ** Returns          Nothing.
840 **
841 *******************************************************************************/
avdt_scb_send_delay_report_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)842 void avdt_scb_send_delay_report_cmd(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
843 {
844     UINT16          delay_value;
845     UNUSED(p_data);
846 
847     if ((p_scb->cs.tsep == AVDT_TSEP_SNK) && (p_scb->curr_cfg.psc_mask & AVDT_PSC_DELAY_RPT)) {
848         delay_value = AVDT_GetDelayValue();
849         AVDT_DelayReport(avdt_scb_to_hdl(p_scb), p_scb->peer_seid, delay_value);
850     }
851 }
852 
853 /*******************************************************************************
854 **
855 ** Function         avdt_scb_init_open_req
856 **
857 ** Description      This function sends the SCB an AVDT_SCB_API_OPEN_REQ_EVT
858 **                  to initiate sending of an open command message.
859 **
860 ** Returns          Nothing.
861 **
862 *******************************************************************************/
avdt_scb_init_open_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)863 void avdt_scb_init_open_req(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
864 {
865     tAVDT_EVT_HDR   single;
866     UNUSED(p_data);
867 
868     if (p_scb->p_ccb != NULL && p_scb->role == AVDT_CONF_INT) {
869         if (!(p_scb->curr_cfg.psc_mask & AVDT_PSC_DELAY_RPT)) {
870             /* initiate open */
871             single.seid = p_scb->peer_seid;
872             avdt_scb_event(p_scb, AVDT_SCB_API_OPEN_REQ_EVT, (tAVDT_SCB_EVT *) &single);
873         } else {
874             btu_start_timer(&p_scb->timer_entry, BTU_TTYPE_AVDT_SCB_DELAY_RPT, AVDT_SCB_TC_DELAY_RPT_TOUT);
875         }
876     }
877 }
878 
879 /*******************************************************************************
880 **
881 ** Function         avdt_scb_hdl_setconfig_rsp
882 **
883 ** Description      This function sends the SCB an AVDT_SCB_API_OPEN_REQ_EVT
884 **                  to initiate sending of an open command message.
885 **
886 ** Returns          Nothing.
887 **
888 *******************************************************************************/
avdt_scb_hdl_setconfig_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)889 void avdt_scb_hdl_setconfig_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
890 {
891     UNUSED(p_data);
892 
893     if (p_scb->p_ccb != NULL) {
894         /* save configuration */
895         memcpy(&p_scb->curr_cfg, &p_scb->req_cfg, sizeof(tAVDT_CFG));
896         p_scb->role = AVDT_CONF_INT;
897 
898         /* send delay reporting command */
899         avdt_scb_send_delay_report_cmd(p_scb, p_data);
900     }
901 }
902 
903 /*******************************************************************************
904 **
905 ** Function         avdt_scb_hdl_start_cmd
906 **
907 ** Description      This function calls the application callback with a
908 **                  start indication.
909 **
910 ** Returns          Nothing.
911 **
912 *******************************************************************************/
avdt_scb_hdl_start_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)913 void avdt_scb_hdl_start_cmd(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
914 {
915     UNUSED(p_data);
916 
917     (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
918                               p_scb->p_ccb ? p_scb->p_ccb->peer_addr : NULL,
919                               AVDT_START_IND_EVT,
920                               NULL);
921 }
922 
923 /*******************************************************************************
924 **
925 ** Function         avdt_scb_hdl_start_rsp
926 **
927 ** Description      This function calls the application callback with a
928 **                  start confirm.
929 **
930 ** Returns          Nothing.
931 **
932 *******************************************************************************/
avdt_scb_hdl_start_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)933 void avdt_scb_hdl_start_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
934 {
935     (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
936                               p_scb->p_ccb ? p_scb->p_ccb->peer_addr : NULL,
937                               AVDT_START_CFM_EVT,
938                               (tAVDT_CTRL *) &p_data->msg.hdr);
939 }
940 
941 /*******************************************************************************
942 **
943 ** Function         avdt_scb_hdl_suspend_cmd
944 **
945 ** Description      This function calls the application callback with a suspend
946 **                  indication.
947 **
948 ** Returns          Nothing.
949 **
950 *******************************************************************************/
avdt_scb_hdl_suspend_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)951 void avdt_scb_hdl_suspend_cmd(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
952 {
953     UNUSED(p_data);
954 
955     (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
956                               p_scb->p_ccb ? p_scb->p_ccb->peer_addr : NULL,
957                               AVDT_SUSPEND_IND_EVT,
958                               NULL);
959 }
960 
961 /*******************************************************************************
962 **
963 ** Function         avdt_scb_hdl_suspend_rsp
964 **
965 ** Description      This function calls the application callback with a suspend
966 **                  confirm.
967 **
968 ** Returns          Nothing.
969 **
970 *******************************************************************************/
avdt_scb_hdl_suspend_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)971 void avdt_scb_hdl_suspend_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
972 {
973     (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
974                               p_scb->p_ccb ? p_scb->p_ccb->peer_addr : NULL,
975                               AVDT_SUSPEND_CFM_EVT,
976                               (tAVDT_CTRL *) &p_data->msg.hdr);
977 }
978 
979 /*******************************************************************************
980 **
981 ** Function         avdt_scb_hdl_tc_close
982 **
983 ** Description      This function is called when the transport channel is
984 **                  closed.  It marks the SCB as not in use and
985 **                  initializes certain SCB parameters.  It then sends
986 **                  an AVDT_CCB_UL_CLOSE_EVT to the CCB if the SCB
987 **                  initiated the close.  It then checks to see if the SCB
988 **                  is to be removed.  If it is it deallocates the SCB.  Finally,
989 **                  it calls the application callback with a close indication.
990 **
991 ** Returns          Nothing.
992 **
993 *******************************************************************************/
avdt_scb_hdl_tc_close(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)994 void avdt_scb_hdl_tc_close(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
995 {
996     UINT8               hdl = avdt_scb_to_hdl(p_scb);
997     tAVDT_CTRL_CBACK    *p_ctrl_cback = p_scb->cs.p_ctrl_cback;
998     tAVDT_CTRL          avdt_ctrl;
999     UINT8               event;
1000     tAVDT_CCB           *p_ccb = p_scb->p_ccb;
1001     BD_ADDR remote_addr;
1002 
1003 
1004     memcpy (remote_addr, p_ccb->peer_addr, BD_ADDR_LEN);
1005 
1006     /* set up hdr */
1007     avdt_ctrl.hdr.err_code = p_scb->close_code;
1008     if (p_data) {
1009         avdt_ctrl.hdr.err_param = p_data->close.disc_rsn;
1010     } else {
1011         avdt_ctrl.hdr.err_param = AVDT_DISC_RSN_NORMAL;
1012     }
1013 
1014     /* clear sep variables */
1015     avdt_scb_clr_vars(p_scb, p_data);
1016     p_scb->media_seq = 0;
1017     p_scb->cong = FALSE;
1018 
1019     /* free pkt we're holding, if any */
1020     if (p_scb->p_pkt != NULL) {
1021         osi_free(p_scb->p_pkt);
1022         p_scb->p_pkt = NULL;
1023     }
1024 
1025     /* stop transport channel timer */
1026     btu_stop_timer(&p_scb->timer_entry);
1027 
1028     if ((p_scb->role == AVDT_CLOSE_INT) || (p_scb->role == AVDT_OPEN_INT)) {
1029         /* tell ccb we're done with signaling channel */
1030         avdt_ccb_event(p_ccb, AVDT_CCB_UL_CLOSE_EVT, NULL);
1031     }
1032     event = (p_scb->role == AVDT_CLOSE_INT) ? AVDT_CLOSE_CFM_EVT : AVDT_CLOSE_IND_EVT;
1033     p_scb->role = AVDT_CLOSE_ACP;
1034 
1035     if (p_scb->remove) {
1036         avdt_scb_dealloc(p_scb, NULL);
1037     }
1038 
1039     /* call app callback */
1040     (*p_ctrl_cback)(hdl, remote_addr, event, &avdt_ctrl);
1041 }
1042 
1043 /*******************************************************************************
1044 **
1045 ** Function         avdt_scb_snd_delay_rpt_req
1046 **
1047 ** Description      This function calls the application callback with a delay
1048 **                  report.
1049 **
1050 ** Returns          Nothing.
1051 **
1052 *******************************************************************************/
avdt_scb_snd_delay_rpt_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1053 void avdt_scb_snd_delay_rpt_req (tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1054 {
1055     avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_DELAY_RPT, (tAVDT_MSG *) &p_data->apidelay);
1056 }
1057 
1058 /*******************************************************************************
1059 **
1060 ** Function         avdt_scb_hdl_delay_rpt_cmd
1061 **
1062 ** Description      This function calls the application callback with a delay
1063 **                  report.
1064 **
1065 ** Returns          Nothing.
1066 **
1067 *******************************************************************************/
avdt_scb_hdl_delay_rpt_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1068 void avdt_scb_hdl_delay_rpt_cmd (tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1069 {
1070     tAVDT_EVT_HDR single;
1071 
1072     (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
1073                               p_scb->p_ccb ? p_scb->p_ccb->peer_addr : NULL,
1074                               AVDT_DELAY_REPORT_EVT,
1075                               (tAVDT_CTRL *) &p_data->msg.hdr);
1076 
1077     if (p_scb->p_ccb) {
1078         if (p_scb->cs.cfg.psc_mask & AVDT_PSC_DELAY_RPT) {
1079             avdt_msg_send_rsp(p_scb->p_ccb, AVDT_SIG_DELAY_RPT, &p_data->msg);
1080             if(p_scb->role == AVDT_CONF_INT) {
1081                 btu_stop_timer(&p_scb->timer_entry);
1082                 /* initiate open */
1083                 single.seid = p_scb->peer_seid;
1084                 avdt_scb_event(p_scb, AVDT_SCB_API_OPEN_REQ_EVT, (tAVDT_SCB_EVT *) &single);
1085             }
1086         } else {
1087             p_data->msg.hdr.err_code = AVDT_ERR_NSC;
1088             avdt_msg_send_rej(p_scb->p_ccb, AVDT_SIG_DELAY_RPT, &p_data->msg);
1089         }
1090     } else {
1091         avdt_scb_rej_not_in_use(p_scb, p_data);
1092     }
1093 }
1094 
1095 /*******************************************************************************
1096 **
1097 ** Function         avdt_scb_hdl_delay_rpt_rsp
1098 **
1099 ** Description      This function calls the application callback with a delay
1100 **                  report.
1101 **
1102 ** Returns          Nothing.
1103 **
1104 *******************************************************************************/
avdt_scb_hdl_delay_rpt_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1105 void avdt_scb_hdl_delay_rpt_rsp (tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1106 {
1107     tAVDT_EVT_HDR single;
1108 
1109     if ((p_scb->cs.tsep == AVDT_TSEP_SNK) &&
1110             (p_scb->state == AVDT_SCB_CONF_ST) && (p_scb->role == AVDT_CONF_INT)) {
1111         btu_stop_timer(&p_scb->timer_entry);
1112         /* initiate open */
1113         single.seid = p_scb->peer_seid;
1114         avdt_scb_event(p_scb, AVDT_SCB_API_OPEN_REQ_EVT, (tAVDT_SCB_EVT *) &single);
1115     }
1116 
1117     (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
1118                               p_scb->p_ccb ? p_scb->p_ccb->peer_addr : NULL,
1119                               AVDT_DELAY_REPORT_CFM_EVT,
1120                               (tAVDT_CTRL *) &p_data->msg.hdr);
1121 }
1122 
1123 #if AVDT_REPORTING == TRUE
1124 /*******************************************************************************
1125 **
1126 ** Function         avdt_scb_hdl_tc_close_sto
1127 **
1128 ** Description      This function is called when a channel is closed in OPEN
1129 **                  state.  Check the channel type and process accordingly.
1130 **
1131 ** Returns          Nothing.
1132 **
1133 *******************************************************************************/
avdt_scb_hdl_tc_close_sto(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1134 void avdt_scb_hdl_tc_close_sto(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1135 {
1136     tAVDT_CTRL          avdt_ctrl;
1137     /* AVDT_CHAN_SIG does not visit this action */
1138     if (p_data && p_data->close.type != AVDT_CHAN_MEDIA) {
1139         /* it's reporting or recovery channel,
1140          * the channel close in open state means the peer does not support it */
1141         if (p_data->close.old_tc_state == AVDT_AD_ST_OPEN) {
1142             avdt_ctrl.hdr.err_code = 0;
1143             avdt_ctrl.hdr.err_param = 0;
1144             /* call app callback */
1145             (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
1146                                       p_scb->p_ccb ? p_scb->p_ccb->peer_addr : NULL,
1147                                       AVDT_REPORT_DISCONN_EVT, &avdt_ctrl);
1148         }
1149     } else {
1150         /* must be in OPEN state. need to go back to idle */
1151         avdt_scb_event(p_scb, AVDT_SCB_MSG_ABORT_RSP_EVT, NULL);
1152         avdt_scb_hdl_tc_close(p_scb, p_data);
1153     }
1154 }
1155 #endif
1156 
1157 /*******************************************************************************
1158  *
1159  * Function         avdt_scb_hdl_delay_rpt_tout
1160  *
1161  * Description      The timer triggers the sending of AVDT open_req.
1162  *                  This function is theoretically not called.
1163  *
1164  * Returns          Nothing.
1165  *
1166  ******************************************************************************/
avdt_scb_hdl_delay_rpt_tout(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1167 void avdt_scb_hdl_delay_rpt_tout(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1168 {
1169     tAVDT_EVT_HDR   single;
1170     UNUSED(p_data);
1171 
1172     /* initiate open */
1173     single.seid = p_scb->peer_seid;
1174     avdt_scb_event(p_scb, AVDT_SCB_API_OPEN_REQ_EVT, (tAVDT_SCB_EVT *) &single);
1175 }
1176 
1177 /*******************************************************************************
1178 **
1179 ** Function         avdt_scb_hdl_tc_open
1180 **
1181 ** Description      This function is called when the transport channel is
1182 **                  opened while in the opening state.  It calls the
1183 **                  application callback with an open indication or open
1184 **                  confirm depending on who initiated the open procedure.
1185 **
1186 ** Returns          Nothing.
1187 **
1188 *******************************************************************************/
avdt_scb_hdl_tc_open(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1189 void avdt_scb_hdl_tc_open(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1190 {
1191     UINT8   event;
1192 #if AVDT_REPORTING == TRUE
1193     UINT8   role;
1194 #endif
1195 
1196     /* stop transport channel connect timer */
1197     btu_stop_timer(&p_scb->timer_entry);
1198 
1199     event = (p_scb->role == AVDT_OPEN_INT) ? AVDT_OPEN_CFM_EVT : AVDT_OPEN_IND_EVT;
1200     p_data->open.hdr.err_code = 0;
1201 
1202     AVDT_TRACE_DEBUG("psc_mask: cfg: 0x%x, req:0x%x, cur: 0x%x\n",
1203                      p_scb->cs.cfg.psc_mask, p_scb->req_cfg.psc_mask, p_scb->curr_cfg.psc_mask);
1204 #if AVDT_REPORTING == TRUE
1205     if (p_scb->curr_cfg.psc_mask & AVDT_PSC_REPORT) {
1206         /* open the reporting channel, if both devices support it */
1207         role = (p_scb->role == AVDT_OPEN_INT) ? AVDT_INT : AVDT_ACP;
1208         avdt_ad_open_req(AVDT_CHAN_REPORT, p_scb->p_ccb, p_scb, role);
1209     }
1210 #endif
1211 
1212     /* call app callback */
1213     (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
1214                               p_scb->p_ccb ? p_scb->p_ccb->peer_addr : NULL,
1215                               event,
1216                               (tAVDT_CTRL *) &p_data->open);
1217 }
1218 
1219 #if AVDT_REPORTING == TRUE
1220 /*******************************************************************************
1221 **
1222 ** Function         avdt_scb_hdl_tc_open_sto
1223 **
1224 ** Description      This function is called when the transport channel is
1225 **                  opened while in the opening state.  It calls the
1226 **                  application callback with an open indication or open
1227 **                  confirm depending on who initiated the open procedure.
1228 **
1229 ** Returns          Nothing.
1230 **
1231 *******************************************************************************/
avdt_scb_hdl_tc_open_sto(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1232 void avdt_scb_hdl_tc_open_sto(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1233 {
1234     tAVDT_CTRL          avdt_ctrl;
1235     /* open reporting channel here, when it is implemented */
1236 
1237     /* call app callback */
1238     if (p_data->open.hdr.err_code == AVDT_CHAN_REPORT) {
1239         avdt_ctrl.hdr.err_code = 0;
1240         avdt_ctrl.hdr.err_param = 1;
1241         (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
1242                                   p_scb->p_ccb ? p_scb->p_ccb->peer_addr : NULL,
1243                                   AVDT_REPORT_CONN_EVT, &avdt_ctrl);
1244     }
1245 }
1246 #endif
1247 
1248 /*******************************************************************************
1249 **
1250 ** Function         avdt_scb_hdl_write_req_no_frag
1251 **
1252 ** Description      This function frees the media packet currently stored in
1253 **                  the SCB, if any.  Then it builds a new media packet from
1254 **                  with the passed in buffer and stores it in the SCB.
1255 **
1256 ** Returns          Nothing.
1257 **
1258 *******************************************************************************/
avdt_scb_hdl_write_req_no_frag(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1259 void avdt_scb_hdl_write_req_no_frag(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1260 {
1261     UINT8   *p;
1262     UINT32  ssrc;
1263 
1264     /* free packet we're holding, if any; to be replaced with new */
1265     if (p_scb->p_pkt != NULL) {
1266         osi_free(p_scb->p_pkt);
1267         p_scb->p_pkt = NULL;
1268 
1269         /* this shouldn't be happening */
1270         AVDT_TRACE_WARNING("Dropped media packet; congested");
1271     }
1272 
1273     /* build a media packet */
1274     /* Add RTP header if required */
1275     if ( !(p_data->apiwrite.opt & AVDT_DATA_OPT_NO_RTP) ) {
1276         ssrc = avdt_scb_gen_ssrc(p_scb);
1277 
1278         p_data->apiwrite.p_buf->len += AVDT_MEDIA_HDR_SIZE;
1279         p_data->apiwrite.p_buf->offset -= AVDT_MEDIA_HDR_SIZE;
1280         p_scb->media_seq++;
1281         p = (UINT8 *)(p_data->apiwrite.p_buf + 1) + p_data->apiwrite.p_buf->offset;
1282 
1283         UINT8_TO_BE_STREAM(p, AVDT_MEDIA_OCTET1);
1284         UINT8_TO_BE_STREAM(p, p_data->apiwrite.m_pt);
1285         UINT16_TO_BE_STREAM(p, p_scb->media_seq);
1286         UINT32_TO_BE_STREAM(p, p_data->apiwrite.time_stamp);
1287         UINT32_TO_BE_STREAM(p, ssrc);
1288     }
1289 
1290     /* store it */
1291     p_scb->p_pkt = p_data->apiwrite.p_buf;
1292 }
1293 
1294 #if AVDT_MULTIPLEXING == TRUE
1295 /*******************************************************************************
1296 **
1297 ** Function         avdt_scb_hdl_write_req_frag
1298 **
1299 ** Description      This function builds a new fragments of media packet from
1300 **                  the passed in buffers and stores them in the SCB.
1301 **
1302 ** Returns          Nothing.
1303 **
1304 *******************************************************************************/
avdt_scb_hdl_write_req_frag(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1305 void avdt_scb_hdl_write_req_frag(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1306 {
1307     UINT8   *p;
1308     UINT32  ssrc;
1309 
1310     /* free fragments we're holding, if any; it shouldn't happen */
1311     if (!fixed_queue_is_empty(p_scb->frag_q))
1312     {
1313         /* this shouldn't be happening */
1314         AVDT_TRACE_WARNING("*** Dropped media packet; congested");
1315         BT_HDR *p_frag;
1316         while ((p_frag = (BT_HDR*)fixed_queue_dequeue(p_scb->frag_q, 0)) != NULL)
1317              osi_free(p_frag);
1318     }
1319 
1320     /* build a media fragments */
1321     p_scb->frag_off = p_data->apiwrite.data_len;
1322     p_scb->p_next_frag = p_data->apiwrite.p_data;
1323 
1324     ssrc = avdt_scb_gen_ssrc(p_scb);
1325 
1326     if (! fixed_queue_is_empty(p_scb->frag_q)) {
1327         list_t *list = fixed_queue_get_list(p_scb->frag_q);
1328         const list_node_t *node = list_begin(list);
1329         if (node != list_end(list)) {
1330             BT_HDR *p_frag = (BT_HDR *)list_node(node);
1331             node = list_next(node);
1332 
1333             /* get first packet */
1334             /* posit on Adaptation Layer header */
1335             p_frag->len += AVDT_AL_HDR_SIZE + AVDT_MEDIA_HDR_SIZE;
1336             p_frag->offset -= AVDT_AL_HDR_SIZE + AVDT_MEDIA_HDR_SIZE;
1337             p = (UINT8 *)(p_frag + 1) + p_frag->offset;
1338 
1339             /* Adaptation Layer header */
1340             /* TSID, no-fragment bit and coding of length (in 2 length octets
1341              * following)
1342              */
1343             *p++ = (p_scb->curr_cfg.mux_tsid_media<<3) | AVDT_ALH_LCODE_16BIT;
1344 
1345             /* length of all remaining transport packet */
1346             UINT16_TO_BE_STREAM(p, p_frag->layer_specific + AVDT_MEDIA_HDR_SIZE );
1347             /* media header */
1348             UINT8_TO_BE_STREAM(p, AVDT_MEDIA_OCTET1);
1349             UINT8_TO_BE_STREAM(p, p_data->apiwrite.m_pt);
1350             UINT16_TO_BE_STREAM(p, p_scb->media_seq);
1351             UINT32_TO_BE_STREAM(p, p_data->apiwrite.time_stamp);
1352             UINT32_TO_BE_STREAM(p, ssrc);
1353             p_scb->media_seq++;
1354         }
1355 
1356         for ( ; node != list_end(list); node = list_next(node)) {
1357             BT_HDR *p_frag = (BT_HDR *)list_node(node);
1358 
1359             /* posit on Adaptation Layer header */
1360             p_frag->len += AVDT_AL_HDR_SIZE;
1361             p_frag->offset -= AVDT_AL_HDR_SIZE;
1362             p = (UINT8 *)(p_frag + 1) + p_frag->offset;
1363             /* Adaptation Layer header */
1364             /* TSID, fragment bit and coding of length (in 2 length octets
1365              * following)
1366              */
1367             *p++ = (p_scb->curr_cfg.mux_tsid_media << 3) |
1368                 (AVDT_ALH_FRAG_MASK | AVDT_ALH_LCODE_16BIT);
1369 
1370             /* length of all remaining transport packet */
1371             UINT16_TO_BE_STREAM(p, p_frag->layer_specific);
1372         }
1373     }
1374 }
1375 #endif
1376 
1377 
1378 /*******************************************************************************
1379 **
1380 ** Function         avdt_scb_hdl_write_req
1381 **
1382 ** Description      This function calls one of the two versions of building functions
1383 **                  for case with and without fragmentation
1384 **
1385 ** Returns          Nothing.
1386 **
1387 *******************************************************************************/
avdt_scb_hdl_write_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1388 void avdt_scb_hdl_write_req(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1389 {
1390 #if AVDT_MULTIPLEXING == TRUE
1391     if (fixed_queue_is_empty(p_scb->frag_q))
1392 #endif
1393     {
1394         avdt_scb_hdl_write_req_no_frag(p_scb, p_data);
1395     }
1396 #if AVDT_MULTIPLEXING == TRUE
1397     else {
1398         avdt_scb_hdl_write_req_frag(p_scb, p_data);
1399     }
1400 #endif
1401 }
1402 
1403 /*******************************************************************************
1404 **
1405 ** Function         avdt_scb_snd_abort_req
1406 **
1407 ** Description      This function sends an abort command message.
1408 **
1409 ** Returns          Nothing.
1410 **
1411 *******************************************************************************/
avdt_scb_snd_abort_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1412 void avdt_scb_snd_abort_req(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1413 {
1414     tAVDT_EVT_HDR   hdr;
1415     UNUSED(p_data);
1416 
1417     if (p_scb->p_ccb != NULL) {
1418         p_scb->role = AVDT_CLOSE_INT;
1419 
1420         hdr.seid = p_scb->peer_seid;
1421 
1422         avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_ABORT, (tAVDT_MSG *) &hdr);
1423     }
1424 }
1425 
1426 /*******************************************************************************
1427 **
1428 ** Function         avdt_scb_snd_abort_rsp
1429 **
1430 ** Description      This function sends an abort response message.
1431 **
1432 ** Returns          Nothing.
1433 **
1434 *******************************************************************************/
avdt_scb_snd_abort_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1435 void avdt_scb_snd_abort_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1436 {
1437     UNUSED(p_scb);
1438 
1439     avdt_msg_send_rsp(avdt_ccb_by_idx(p_data->msg.hdr.ccb_idx), AVDT_SIG_ABORT,
1440                       &p_data->msg);
1441 }
1442 
1443 /*******************************************************************************
1444 **
1445 ** Function         avdt_scb_snd_close_req
1446 **
1447 ** Description      This function sends a close command message.
1448 **
1449 ** Returns          Nothing.
1450 **
1451 *******************************************************************************/
avdt_scb_snd_close_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1452 void avdt_scb_snd_close_req(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1453 {
1454     tAVDT_EVT_HDR   hdr;
1455     UNUSED(p_data);
1456 
1457     p_scb->role = AVDT_CLOSE_INT;
1458 
1459     hdr.seid = p_scb->peer_seid;
1460 
1461     avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_CLOSE, (tAVDT_MSG *) &hdr);
1462 }
1463 
1464 /*******************************************************************************
1465 **
1466 ** Function         avdt_scb_snd_stream_close
1467 **
1468 ** Description      This function sends a close command message.
1469 **
1470 ** Returns          Nothing.
1471 **
1472 *******************************************************************************/
avdt_scb_snd_stream_close(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1473 void avdt_scb_snd_stream_close(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1474 {
1475 #if AVDT_MULTIPLEXING == TRUE
1476     AVDT_TRACE_WARNING("%s c:%d, off:%d", __func__,
1477         fixed_queue_length(p_scb->frag_q), p_scb->frag_off);
1478 
1479     /* clean fragments queue */
1480     BT_HDR *p_frag;
1481     while ((p_frag = (BT_HDR*)fixed_queue_dequeue(p_scb->frag_q, 0)) != NULL) {
1482         osi_free(p_frag);
1483     }
1484     p_scb->frag_off = 0;
1485 #endif
1486     if (p_scb->p_pkt) {
1487         osi_free(p_scb->p_pkt);
1488         p_scb->p_pkt = NULL;
1489     }
1490 
1491     avdt_scb_snd_close_req(p_scb, p_data);
1492 }
1493 
1494 /*******************************************************************************
1495 **
1496 ** Function         avdt_scb_snd_close_rsp
1497 **
1498 ** Description      This function sends a close response message.
1499 **
1500 ** Returns          Nothing.
1501 **
1502 *******************************************************************************/
avdt_scb_snd_close_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1503 void avdt_scb_snd_close_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1504 {
1505     avdt_msg_send_rsp(p_scb->p_ccb, AVDT_SIG_CLOSE, &p_data->msg);
1506 }
1507 
1508 /*******************************************************************************
1509 **
1510 ** Function         avdt_scb_snd_getconfig_req
1511 **
1512 ** Description      This function sends a get configuration command message.
1513 **
1514 ** Returns          Nothing.
1515 **
1516 *******************************************************************************/
avdt_scb_snd_getconfig_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1517 void avdt_scb_snd_getconfig_req(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1518 {
1519     tAVDT_EVT_HDR   hdr;
1520     UNUSED(p_data);
1521 
1522     hdr.seid = p_scb->peer_seid;
1523 
1524     avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_GETCONFIG, (tAVDT_MSG *) &hdr);
1525 }
1526 
1527 /*******************************************************************************
1528 **
1529 ** Function         avdt_scb_snd_getconfig_rsp
1530 **
1531 ** Description      This function sends a get configuration response message.
1532 **
1533 ** Returns          Nothing.
1534 **
1535 *******************************************************************************/
avdt_scb_snd_getconfig_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1536 void avdt_scb_snd_getconfig_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1537 {
1538     avdt_msg_send_rsp(p_scb->p_ccb, AVDT_SIG_GETCONFIG, &p_data->msg);
1539 }
1540 
1541 /*******************************************************************************
1542 **
1543 ** Function         avdt_scb_snd_open_req
1544 **
1545 ** Description      This function sends an open command message.
1546 **
1547 ** Returns          Nothing.
1548 **
1549 *******************************************************************************/
avdt_scb_snd_open_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1550 void avdt_scb_snd_open_req(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1551 {
1552     tAVDT_EVT_HDR   hdr;
1553     UNUSED(p_data);
1554 
1555     hdr.seid = p_scb->peer_seid;
1556 
1557     avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_OPEN, (tAVDT_MSG *) &hdr);
1558 }
1559 
1560 /*******************************************************************************
1561 **
1562 ** Function         avdt_scb_snd_open_rsp
1563 **
1564 ** Description      This function sends an open response message.  It also
1565 **                  calls avdt_ad_open_req() to accept a transport channel
1566 **                  connection.
1567 **
1568 ** Returns          Nothing.
1569 **
1570 *******************************************************************************/
avdt_scb_snd_open_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1571 void avdt_scb_snd_open_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1572 {
1573     /* notify adaption that we're waiting for transport channel open */
1574     p_scb->role = AVDT_OPEN_ACP;
1575     avdt_ad_open_req(AVDT_CHAN_MEDIA, p_scb->p_ccb, p_scb, AVDT_ACP);
1576 
1577     /* send response */
1578     avdt_msg_send_rsp(p_scb->p_ccb, AVDT_SIG_OPEN, &p_data->msg);
1579 
1580     /* start tc connect timer */
1581     btu_start_timer(&p_scb->timer_entry, BTU_TTYPE_AVDT_SCB_TC, AVDT_SCB_TC_CONN_TOUT);
1582 }
1583 
1584 /*******************************************************************************
1585 **
1586 ** Function         avdt_scb_snd_reconfig_req
1587 **
1588 ** Description      This function stores the configuration parameters in the
1589 **                  SCB and sends a reconfiguration command message.
1590 **
1591 ** Returns          Nothing.
1592 **
1593 *******************************************************************************/
avdt_scb_snd_reconfig_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1594 void avdt_scb_snd_reconfig_req(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1595 {
1596     memcpy(&p_scb->req_cfg, p_data->msg.config_cmd.p_cfg, sizeof(tAVDT_CFG));
1597     p_data->msg.hdr.seid = p_scb->peer_seid;
1598     avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_RECONFIG, &p_data->msg);
1599 }
1600 
1601 /*******************************************************************************
1602 **
1603 ** Function         avdt_scb_snd_reconfig_rsp
1604 **
1605 ** Description      This function stores the configuration parameters in the
1606 **                  SCB and sends a reconfiguration response message.
1607 **
1608 ** Returns          Nothing.
1609 **
1610 *******************************************************************************/
avdt_scb_snd_reconfig_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1611 void avdt_scb_snd_reconfig_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1612 {
1613     if (p_data->msg.hdr.err_code == 0) {
1614         /* store new configuration */
1615         if (p_scb->req_cfg.num_codec > 0) {
1616             p_scb->curr_cfg.num_codec = p_scb->req_cfg.num_codec;
1617             memcpy(p_scb->curr_cfg.codec_info, p_scb->req_cfg.codec_info, AVDT_CODEC_SIZE);
1618         }
1619         if (p_scb->req_cfg.num_protect > 0) {
1620             p_scb->curr_cfg.num_protect = p_scb->req_cfg.num_protect;
1621             memcpy(p_scb->curr_cfg.protect_info, p_scb->req_cfg.protect_info, AVDT_PROTECT_SIZE);
1622         }
1623 
1624         /* send response */
1625         avdt_msg_send_rsp(p_scb->p_ccb, AVDT_SIG_RECONFIG, &p_data->msg);
1626     } else {
1627         /* send reject */
1628         avdt_msg_send_rej(p_scb->p_ccb, AVDT_SIG_RECONFIG, &p_data->msg);
1629     }
1630 }
1631 
1632 /*******************************************************************************
1633 **
1634 ** Function         avdt_scb_snd_security_req
1635 **
1636 ** Description      This function sends a security command message.
1637 **
1638 ** Returns          Nothing.
1639 **
1640 *******************************************************************************/
avdt_scb_snd_security_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1641 void avdt_scb_snd_security_req(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1642 {
1643     p_data->msg.hdr.seid = p_scb->peer_seid;
1644     avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_SECURITY, &p_data->msg);
1645 }
1646 
1647 /*******************************************************************************
1648 **
1649 ** Function         avdt_scb_snd_security_rsp
1650 **
1651 ** Description      This function sends a security response message.
1652 **
1653 ** Returns          Nothing.
1654 **
1655 *******************************************************************************/
avdt_scb_snd_security_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1656 void avdt_scb_snd_security_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1657 {
1658     if (p_data->msg.hdr.err_code == 0) {
1659         avdt_msg_send_rsp(p_scb->p_ccb, AVDT_SIG_SECURITY, &p_data->msg);
1660     } else {
1661         avdt_msg_send_rej(p_scb->p_ccb, AVDT_SIG_SECURITY, &p_data->msg);
1662     }
1663 }
1664 
1665 /*******************************************************************************
1666 **
1667 ** Function         avdt_scb_snd_setconfig_rej
1668 **
1669 ** Description      This function marks the SCB as not in use and sends a
1670 **                  set configuration reject message.
1671 **
1672 ** Returns          Nothing.
1673 **
1674 *******************************************************************************/
avdt_scb_snd_setconfig_rej(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1675 void avdt_scb_snd_setconfig_rej(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1676 {
1677     if (p_scb->p_ccb != NULL) {
1678         avdt_msg_send_rej(p_scb->p_ccb, AVDT_SIG_SETCONFIG, &p_data->msg);
1679 
1680         /* clear scb variables */
1681         avdt_scb_clr_vars(p_scb, p_data);
1682     }
1683 }
1684 
1685 /*******************************************************************************
1686 **
1687 ** Function         avdt_scb_snd_setconfig_req
1688 **
1689 ** Description      This function marks the SCB as in use and copies the
1690 **                  configuration parameters to the SCB.  Then the function
1691 **                  sends a set configuration command message and initiates
1692 **                  opening of the signaling channel.
1693 **
1694 ** Returns          Nothing.
1695 **
1696 *******************************************************************************/
avdt_scb_snd_setconfig_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1697 void avdt_scb_snd_setconfig_req(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1698 {
1699     tAVDT_CFG *p_req, *p_cfg;
1700 
1701     /* copy API parameters to scb, set scb as in use */
1702     p_scb->in_use = TRUE;
1703     p_scb->p_ccb = avdt_ccb_by_idx(p_data->msg.config_cmd.hdr.ccb_idx);
1704     p_scb->peer_seid = p_data->msg.config_cmd.hdr.seid;
1705     p_req = p_data->msg.config_cmd.p_cfg;
1706     p_cfg = &p_scb->cs.cfg;
1707 #if AVDT_MULTIPLEXING == TRUE
1708     p_req->mux_tsid_media = p_cfg->mux_tsid_media;
1709     p_req->mux_tcid_media = p_cfg->mux_tcid_media;
1710     if (p_req->psc_mask & AVDT_PSC_REPORT) {
1711         p_req->mux_tsid_report = p_cfg->mux_tsid_report;
1712         p_req->mux_tcid_report = p_cfg->mux_tcid_report;
1713     }
1714 #endif
1715     memcpy(&p_scb->req_cfg, p_data->msg.config_cmd.p_cfg, sizeof(tAVDT_CFG));
1716 
1717     avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_SETCONFIG, &p_data->msg);
1718 
1719     /* tell ccb to open channel */
1720     avdt_ccb_event(p_scb->p_ccb, AVDT_CCB_UL_OPEN_EVT, NULL);
1721 }
1722 
1723 /*******************************************************************************
1724 **
1725 ** Function         avdt_scb_snd_setconfig_rsp
1726 **
1727 ** Description      This function copies the requested configuration into the
1728 **                  current configuration and sends a set configuration
1729 **                  response message.
1730 **
1731 ** Returns          Nothing.
1732 **
1733 *******************************************************************************/
avdt_scb_snd_setconfig_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1734 void avdt_scb_snd_setconfig_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1735 {
1736     if (p_scb->p_ccb != NULL) {
1737         memcpy(&p_scb->curr_cfg, &p_scb->req_cfg, sizeof(tAVDT_CFG));
1738         p_scb->role = AVDT_CONF_ACP;
1739 
1740         avdt_msg_send_rsp(p_scb->p_ccb, AVDT_SIG_SETCONFIG, &p_data->msg);
1741     }
1742 }
1743 
1744 /*******************************************************************************
1745 **
1746 ** Function         avdt_scb_snd_tc_close
1747 **
1748 ** Description      This function calls avdt_ad_close_req() to close the
1749 **                  transport channel for this SCB.
1750 **
1751 ** Returns          Nothing.
1752 **
1753 *******************************************************************************/
avdt_scb_snd_tc_close(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1754 void avdt_scb_snd_tc_close(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1755 {
1756     UNUSED(p_data);
1757 
1758 #if AVDT_REPORTING == TRUE
1759     if (p_scb->curr_cfg.psc_mask & AVDT_PSC_REPORT) {
1760         avdt_ad_close_req(AVDT_CHAN_REPORT, p_scb->p_ccb, p_scb);
1761     }
1762 #endif
1763     avdt_ad_close_req(AVDT_CHAN_MEDIA, p_scb->p_ccb, p_scb);
1764 }
1765 
1766 /*******************************************************************************
1767 **
1768 ** Function         avdt_scb_cb_err
1769 **
1770 ** Description      This function calls the application callback function
1771 **                  indicating an error.
1772 **
1773 ** Returns          Nothing.
1774 **
1775 *******************************************************************************/
avdt_scb_cb_err(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1776 void avdt_scb_cb_err(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1777 {
1778     tAVDT_CTRL          avdt_ctrl;
1779     UNUSED(p_data);
1780 
1781     /* set error code and parameter */
1782     avdt_ctrl.hdr.err_code = AVDT_ERR_BAD_STATE;
1783     avdt_ctrl.hdr.err_param = 0;
1784 
1785     /* call callback, using lookup table to get callback event */
1786     (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
1787                               NULL,
1788                               avdt_scb_cback_evt[p_scb->curr_evt],
1789                               &avdt_ctrl);
1790 }
1791 
1792 /*******************************************************************************
1793 **
1794 ** Function         avdt_scb_cong_state
1795 **
1796 ** Description      This function sets the congestion state of the SCB media
1797 **                  transport channel.
1798 **
1799 ** Returns          Nothing.
1800 **
1801 *******************************************************************************/
avdt_scb_cong_state(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1802 void avdt_scb_cong_state(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1803 {
1804     p_scb->cong = p_data->llcong;
1805 }
1806 
1807 /*******************************************************************************
1808 **
1809 ** Function         avdt_scb_rej_state
1810 **
1811 ** Description      This function sends a reject message to the peer indicating
1812 **                  incorrect state for the received command message.
1813 **
1814 ** Returns          Nothing.
1815 **
1816 *******************************************************************************/
avdt_scb_rej_state(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1817 void avdt_scb_rej_state(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1818 {
1819     UNUSED(p_scb);
1820 
1821     p_data->msg.hdr.err_code = AVDT_ERR_BAD_STATE;
1822     p_data->msg.hdr.err_param = 0;
1823     avdt_msg_send_rej(avdt_ccb_by_idx(p_data->msg.hdr.ccb_idx),
1824                       p_data->msg.hdr.sig_id, &p_data->msg);
1825 }
1826 
1827 /*******************************************************************************
1828 **
1829 ** Function         avdt_scb_rej_in_use
1830 **
1831 ** Description      This function sends a reject message to the peer indicating
1832 **                  the stream is in use.
1833 **
1834 ** Returns          Nothing.
1835 **
1836 *******************************************************************************/
avdt_scb_rej_in_use(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1837 void avdt_scb_rej_in_use(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1838 {
1839     UNUSED(p_scb);
1840 
1841     p_data->msg.hdr.err_code = AVDT_ERR_IN_USE;
1842     p_data->msg.hdr.err_param = 0;
1843     avdt_msg_send_rej(avdt_ccb_by_idx(p_data->msg.hdr.ccb_idx),
1844                       p_data->msg.hdr.sig_id, &p_data->msg);
1845 }
1846 
1847 /*******************************************************************************
1848 **
1849 ** Function         avdt_scb_rej_not_in_use
1850 **
1851 ** Description      This function sends a reject message to the peer indicating
1852 **                  the stream is in use.
1853 **
1854 ** Returns          Nothing.
1855 **
1856 *******************************************************************************/
avdt_scb_rej_not_in_use(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1857 void avdt_scb_rej_not_in_use(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1858 {
1859     UNUSED(p_scb);
1860 
1861     p_data->msg.hdr.err_code = AVDT_ERR_NOT_IN_USE;
1862     p_data->msg.hdr.err_param = 0;
1863     avdt_msg_send_rej(avdt_ccb_by_idx(p_data->msg.hdr.ccb_idx),
1864                       p_data->msg.hdr.sig_id, &p_data->msg);
1865 }
1866 
1867 /*******************************************************************************
1868 **
1869 ** Function         avdt_scb_set_remove
1870 **
1871 ** Description      This function marks an SCB to be removed.
1872 **
1873 ** Returns          Nothing.
1874 **
1875 *******************************************************************************/
avdt_scb_set_remove(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1876 void avdt_scb_set_remove(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1877 {
1878     UNUSED(p_data);
1879 
1880     p_scb->remove = TRUE;
1881 }
1882 
1883 /*******************************************************************************
1884 **
1885 ** Function         avdt_scb_free_pkt
1886 **
1887 ** Description      This function frees the media packet passed in.
1888 **
1889 ** Returns          Nothing.
1890 **
1891 *******************************************************************************/
avdt_scb_free_pkt(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1892 void avdt_scb_free_pkt(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1893 {
1894     tAVDT_CTRL      avdt_ctrl;
1895 
1896     /* set error code and parameter */
1897     avdt_ctrl.hdr.err_code = AVDT_ERR_BAD_STATE;
1898     avdt_ctrl.hdr.err_param = 0;
1899 
1900     /* p_buf can be NULL in case using of fragments queue frag_q */
1901     if (p_data->apiwrite.p_buf) {
1902         osi_free(p_data->apiwrite.p_buf);
1903         p_data->apiwrite.p_buf = NULL;
1904     }
1905 
1906 #if AVDT_MULTIPLEXING == TRUE
1907     /* clean fragments queue */
1908     BT_HDR          *p_frag;
1909     while ((p_frag = (BT_HDR*)fixed_queue_dequeue(p_scb->frag_q, 0)) != NULL) {
1910          osi_free(p_frag);
1911 	}
1912 #endif
1913 
1914     AVDT_TRACE_WARNING("Dropped media packet");
1915 
1916     /* we need to call callback to keep data flow going */
1917     (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb), NULL, AVDT_WRITE_CFM_EVT,
1918                               &avdt_ctrl);
1919 }
1920 
1921 /*******************************************************************************
1922 **
1923 ** Function         avdt_scb_clr_pkt
1924 **
1925 ** Description      This function frees the media packet stored in the SCB.
1926 **
1927 ** Returns          Nothing.
1928 **
1929 *******************************************************************************/
avdt_scb_clr_pkt(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1930 void avdt_scb_clr_pkt(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1931 {
1932     tAVDT_CTRL      avdt_ctrl;
1933     tAVDT_CCB       *p_ccb;
1934     UINT8           tcid;
1935     UINT16          lcid;
1936     UNUSED(p_data);
1937 
1938     /* set error code and parameter */
1939     avdt_ctrl.hdr.err_code = AVDT_ERR_BAD_STATE;
1940     avdt_ctrl.hdr.err_param = 0;
1941     /* flush the media data queued at L2CAP */
1942     if ((p_ccb = p_scb->p_ccb) != NULL) {
1943         /* get tcid from type, scb */
1944         tcid = avdt_ad_type_to_tcid(AVDT_CHAN_MEDIA, p_scb);
1945 
1946         lcid = avdt_cb.ad.rt_tbl[avdt_ccb_to_idx(p_ccb)][tcid].lcid;
1947         L2CA_FlushChannel (lcid, L2CAP_FLUSH_CHANS_ALL);
1948     }
1949 
1950     if (p_scb->p_pkt != NULL) {
1951         osi_free(p_scb->p_pkt);
1952         p_scb->p_pkt = NULL;
1953 
1954         AVDT_TRACE_DEBUG("Dropped stored media packet");
1955 
1956         /* we need to call callback to keep data flow going */
1957         (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb), NULL, AVDT_WRITE_CFM_EVT,
1958                                   &avdt_ctrl);
1959     }
1960 #if AVDT_MULTIPLEXING == TRUE
1961     else if (!fixed_queue_is_empty(p_scb->frag_q)) {
1962         AVDT_TRACE_DEBUG("Dropped fragments queue");
1963         /* clean fragments queue */
1964         BT_HDR *p_frag;
1965         while ((p_frag = (BT_HDR*)fixed_queue_dequeue(p_scb->frag_q, 0)) != NULL) {
1966              osi_free(p_frag);
1967 		}
1968         p_scb->frag_off = 0;
1969 
1970         /* we need to call callback to keep data flow going */
1971         (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb), NULL, AVDT_WRITE_CFM_EVT,
1972                                   &avdt_ctrl);
1973     }
1974 #endif
1975 }
1976 
1977 
1978 /*******************************************************************************
1979 **
1980 ** Function         avdt_scb_chk_snd_pkt
1981 **
1982 ** Description      This function checks if the SCB is congested, and if not
1983 **                  congested it sends a stored media packet, if any.  After it
1984 **                  sends the packet it calls the application callback function
1985 **                  with a write confirm.
1986 **
1987 ** Returns          Nothing.
1988 **
1989 *******************************************************************************/
avdt_scb_chk_snd_pkt(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1990 void avdt_scb_chk_snd_pkt(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1991 {
1992     tAVDT_CTRL      avdt_ctrl;
1993     BT_HDR          *p_pkt;
1994 #if AVDT_MULTIPLEXING == TRUE
1995     BOOLEAN         sent = FALSE;
1996     UINT8   res = AVDT_AD_SUCCESS;
1997     tAVDT_SCB_EVT data;
1998 #endif
1999     UNUSED(p_data);
2000 
2001     avdt_ctrl.hdr.err_code = 0;
2002 
2003     if (!p_scb->cong) {
2004         if (p_scb->p_pkt != NULL) {
2005             p_pkt = p_scb->p_pkt;
2006             p_scb->p_pkt = NULL;
2007             avdt_ad_write_req(AVDT_CHAN_MEDIA, p_scb->p_ccb, p_scb, p_pkt);
2008 
2009             (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb), NULL, AVDT_WRITE_CFM_EVT, &avdt_ctrl);
2010         }
2011 #if AVDT_MULTIPLEXING == TRUE
2012         else {
2013 #if 0
2014             AVDT_TRACE_DEBUG("num_q=%d\n",
2015                              L2CA_FlushChannel(avdt_cb.ad.rt_tbl[avdt_ccb_to_idx(p_scb->p_ccb)][avdt_ad_type_to_tcid(AVDT_CHAN_MEDIA, p_scb)].lcid),
2016                              L2CAP_FLUSH_CHANS_GET);
2017 #endif
2018             while ((p_pkt = (BT_HDR*)fixed_queue_dequeue(p_scb->frag_q, 0)) != NULL) {
2019                 sent = TRUE;
2020                 AVDT_TRACE_DEBUG("Send fragment len=%d\n", p_pkt->len);
2021                 /* fragments queue contains fragment to send */
2022                 res = avdt_ad_write_req(AVDT_CHAN_MEDIA, p_scb->p_ccb, p_scb, p_pkt);
2023                 if (AVDT_AD_CONGESTED == res) {
2024                     p_scb->cong = TRUE;
2025                     AVDT_TRACE_DEBUG("avdt/l2c congested!!");
2026                     break;/* exit loop if channel became congested */
2027                 }
2028             }
2029             AVDT_TRACE_DEBUG("res=%d left=%d\n", res, p_scb->frag_off);
2030 
2031             if (p_scb->frag_off) {
2032                 if (AVDT_AD_SUCCESS == res || fixed_queue_is_empty(p_scb->frag_q)) {
2033                     /* all buffers were sent to L2CAP, compose more to queue */
2034                     avdt_scb_queue_frags(p_scb, &p_scb->p_next_frag, &p_scb->frag_off, p_scb->frag_q);
2035                     if (!fixed_queue_is_empty(p_scb->frag_q)) {
2036                         data.llcong = p_scb->cong;
2037                         avdt_scb_event(p_scb, AVDT_SCB_TC_CONG_EVT, &data);
2038                     }
2039                 }
2040             }
2041 
2042             /* Send event AVDT_WRITE_CFM_EVT if it was last fragment */
2043             else if (sent && fixed_queue_is_empty(p_scb->frag_q)) {
2044                 (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb), NULL, AVDT_WRITE_CFM_EVT, &avdt_ctrl);
2045             }
2046         }
2047 #endif
2048     }
2049 }
2050 
2051 /*******************************************************************************
2052 **
2053 ** Function         avdt_scb_tc_timer
2054 **
2055 ** Description      This function is called to start a timer when the peer
2056 **                  initiates closing of the stream.  The timer verifies that
2057 **                  the peer disconnects the transport channel.
2058 **
2059 ** Returns          Nothing.
2060 **
2061 *******************************************************************************/
avdt_scb_tc_timer(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)2062 void avdt_scb_tc_timer(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
2063 {
2064     UNUSED(p_data);
2065 
2066     btu_start_timer(&p_scb->timer_entry, BTU_TTYPE_AVDT_SCB_TC, AVDT_SCB_TC_DISC_TOUT);
2067 }
2068 
2069 /*******************************************************************************
2070 **
2071 ** Function         avdt_scb_clr_vars
2072 **
2073 ** Description      This function initializes certain SCB variables.
2074 **
2075 ** Returns          Nothing.
2076 **
2077 *******************************************************************************/
avdt_scb_clr_vars(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)2078 void avdt_scb_clr_vars(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
2079 {
2080     UNUSED(p_data);
2081 
2082     if ((p_scb->cs.tsep == AVDT_TSEP_SNK) && (!p_scb->sink_activated)) {
2083         p_scb->in_use = TRUE;
2084     } else {
2085         p_scb->in_use = FALSE;
2086     }
2087     p_scb->p_ccb = NULL;
2088     p_scb->peer_seid = 0;
2089 }
2090 
2091 #if AVDT_MULTIPLEXING == TRUE
2092 /*******************************************************************************
2093 **
2094 ** Function         avdt_scb_queue_frags
2095 **
2096 ** Description      This function breaks media payload into fragments
2097 **                  and put the fragments in the given queue.
2098 **
2099 ** Returns          Nothing.
2100 **
2101 *******************************************************************************/
avdt_scb_queue_frags(tAVDT_SCB * p_scb,UINT8 ** pp_data,UINT32 * p_data_len,fixed_queue_t * pq)2102 void avdt_scb_queue_frags(tAVDT_SCB *p_scb, UINT8 **pp_data, UINT32 *p_data_len, fixed_queue_t *pq)
2103 {
2104     UINT16  lcid;
2105     UINT16  num_frag;
2106     UINT16  mtu_used;
2107     UINT8   *p;
2108     BOOLEAN al_hdr = FALSE;
2109     UINT8   tcid;
2110     tAVDT_TC_TBL    *p_tbl;
2111     UINT16          buf_size;
2112     UINT16          offset = AVDT_MEDIA_OFFSET + AVDT_AL_HDR_SIZE;
2113     UINT16          cont_offset = offset - AVDT_MEDIA_HDR_SIZE;
2114     BT_HDR          *p_frag;
2115 
2116     tcid = avdt_ad_type_to_tcid(AVDT_CHAN_MEDIA, p_scb);
2117     lcid = avdt_cb.ad.rt_tbl[avdt_ccb_to_idx(p_scb->p_ccb)][tcid].lcid;
2118 
2119     if ( p_scb->frag_off != 0) {
2120         /* continuing process is usually triggered by un-congest event.
2121          * the number of buffers at L2CAP is very small (if not 0).
2122          * we do not need to L2CA_FlushChannel() */
2123         offset = cont_offset;
2124         al_hdr = TRUE;
2125         num_frag = AVDT_MAX_FRAG_COUNT;
2126     } else {
2127         num_frag = L2CA_FlushChannel(lcid, L2CAP_FLUSH_CHANS_GET);
2128         AVDT_TRACE_DEBUG("num_q=%d lcid=%d\n", num_frag, lcid);
2129         if (num_frag >= AVDT_MAX_FRAG_COUNT) {
2130             num_frag = 0;
2131         } else {
2132             num_frag = AVDT_MAX_FRAG_COUNT - num_frag;
2133         }
2134     }
2135 
2136     /* look up transport channel table entry to get peer mtu */
2137     p_tbl = avdt_ad_tc_tbl_by_type(AVDT_CHAN_MEDIA, p_scb->p_ccb, p_scb);
2138     buf_size = p_tbl->peer_mtu + BT_HDR_SIZE;
2139     AVDT_TRACE_DEBUG("peer_mtu: %d, buf_size: %d num_frag=%d\n",
2140                      p_tbl->peer_mtu, buf_size, num_frag);
2141 
2142     if (buf_size > AVDT_DATA_BUF_SIZE) {
2143         buf_size = AVDT_DATA_BUF_SIZE;
2144     }
2145 
2146     mtu_used = buf_size - BT_HDR_SIZE;
2147 
2148     while (*p_data_len && num_frag) {
2149         /* allocate buffer for fragment */
2150         if (NULL == (p_frag = (BT_HDR *)osi_malloc(buf_size))) {
2151             AVDT_TRACE_WARNING("avdt_scb_queue_frags len=%d(out of GKI buffers)\n", *p_data_len);
2152             break;
2153         }
2154         /* fill fragment by chunk of media payload */
2155         p_frag->layer_specific = *p_data_len;/* length of all remaining transport packet */
2156         p_frag->offset = offset;
2157         /* adjust packet offset for continuing packets */
2158         offset = cont_offset;
2159 
2160         p_frag->len = mtu_used - p_frag->offset;
2161         if (p_frag->len > *p_data_len) {
2162             p_frag->len = *p_data_len;
2163         }
2164         memcpy((UINT8 *)(p_frag + 1) + p_frag->offset, *pp_data, p_frag->len);
2165         *pp_data += p_frag->len;
2166         *p_data_len -= p_frag->len;
2167         AVDT_TRACE_DEBUG("Prepared fragment len=%d\n", p_frag->len);
2168 
2169         if (al_hdr) {
2170             /* Adaptation Layer header */
2171             p_frag->len += AVDT_AL_HDR_SIZE;
2172             p_frag->offset -= AVDT_AL_HDR_SIZE;
2173             p = (UINT8 *)(p_frag + 1) + p_frag->offset;
2174             /* TSID, fragment bit and coding of length(in 2 length octets following) */
2175             *p++ = (p_scb->curr_cfg.mux_tsid_media << 3) | (AVDT_ALH_FRAG_MASK | AVDT_ALH_LCODE_16BIT);
2176 
2177             /* length of all remaining transport packet */
2178             UINT16_TO_BE_STREAM(p, p_frag->layer_specific );
2179         }
2180         /* put fragment into gueue */
2181         fixed_queue_enqueue(p_scb->frag_q, p_frag, FIXED_QUEUE_MAX_TIMEOUT);
2182         num_frag--;
2183     }
2184 }
2185 #endif
2186 
2187 #endif /* #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE) */
2188