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         if (p_scb->cs.cfg.codec_info[AVDT_CODEC_TYPE_INDEX] == p_cfg->codec_info[AVDT_CODEC_TYPE_INDEX]) {
792             /* set sep as in use */
793             p_scb->in_use = TRUE;
794 
795             /* copy info to scb */
796             p_scb->p_ccb = avdt_ccb_by_idx(p_data->msg.config_cmd.hdr.ccb_idx);
797             p_scb->peer_seid = p_data->msg.config_cmd.int_seid;
798             memcpy(&p_scb->req_cfg, p_cfg, sizeof(tAVDT_CFG));
799             /* call app callback */
800             (*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*/
801                                       p_scb->p_ccb ? p_scb->p_ccb->peer_addr : NULL,
802                                       AVDT_CONFIG_IND_EVT,
803                                       (tAVDT_CTRL *) &p_data->msg.config_cmd);
804         } else {
805             p_data->msg.hdr.err_code = AVDT_ERR_UNSUP_CFG;
806             p_data->msg.hdr.err_param = 0;
807             avdt_msg_send_rej(avdt_ccb_by_idx(p_data->msg.hdr.ccb_idx),
808                               p_data->msg.hdr.sig_id, &p_data->msg);
809         }
810     } else {
811         avdt_scb_rej_in_use(p_scb, p_data);
812     }
813 }
814 
815 /*******************************************************************************
816 **
817 ** Function         avdt_scb_hdl_setconfig_rej
818 **
819 ** Description      This function marks the SCB as not in use and calls the
820 **                  application callback with an open confirm indicating failure.
821 **
822 ** Returns          Nothing.
823 **
824 *******************************************************************************/
avdt_scb_hdl_setconfig_rej(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)825 void avdt_scb_hdl_setconfig_rej(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
826 {
827     /* clear scb variables */
828     avdt_scb_clr_vars(p_scb, p_data);
829 
830     /* tell ccb we're done with signaling channel */
831     avdt_ccb_event(avdt_ccb_by_idx(p_data->msg.hdr.ccb_idx), AVDT_CCB_UL_CLOSE_EVT, NULL);
832 
833     /* call application callback */
834     (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
835                               NULL,
836                               AVDT_OPEN_CFM_EVT,
837                               (tAVDT_CTRL *) &p_data->msg.hdr);
838 }
839 
840 /*******************************************************************************
841 **
842 ** Function         avdt_scb_hdl_setconfig_rsp
843 **
844 ** Description      This function sends the SCB an AVDT_SCB_API_OPEN_REQ_EVT
845 **                  to initiate sending of an open command message.
846 **
847 ** Returns          Nothing.
848 **
849 *******************************************************************************/
avdt_scb_hdl_setconfig_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)850 void avdt_scb_hdl_setconfig_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
851 {
852     tAVDT_EVT_HDR   single;
853     UNUSED(p_data);
854 
855     if (p_scb->p_ccb != NULL) {
856         /* save configuration */
857         memcpy(&p_scb->curr_cfg, &p_scb->req_cfg, sizeof(tAVDT_CFG));
858 
859         /* initiate open */
860         single.seid = p_scb->peer_seid;
861         avdt_scb_event(p_scb, AVDT_SCB_API_OPEN_REQ_EVT, (tAVDT_SCB_EVT *) &single);
862     }
863 }
864 
865 /*******************************************************************************
866 **
867 ** Function         avdt_scb_hdl_start_cmd
868 **
869 ** Description      This function calls the application callback with a
870 **                  start indication.
871 **
872 ** Returns          Nothing.
873 **
874 *******************************************************************************/
avdt_scb_hdl_start_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)875 void avdt_scb_hdl_start_cmd(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
876 {
877     UNUSED(p_data);
878 
879     (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
880                               p_scb->p_ccb ? p_scb->p_ccb->peer_addr : NULL,
881                               AVDT_START_IND_EVT,
882                               NULL);
883 }
884 
885 /*******************************************************************************
886 **
887 ** Function         avdt_scb_hdl_start_rsp
888 **
889 ** Description      This function calls the application callback with a
890 **                  start confirm.
891 **
892 ** Returns          Nothing.
893 **
894 *******************************************************************************/
avdt_scb_hdl_start_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)895 void avdt_scb_hdl_start_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
896 {
897     (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
898                               p_scb->p_ccb ? p_scb->p_ccb->peer_addr : NULL,
899                               AVDT_START_CFM_EVT,
900                               (tAVDT_CTRL *) &p_data->msg.hdr);
901 }
902 
903 /*******************************************************************************
904 **
905 ** Function         avdt_scb_hdl_suspend_cmd
906 **
907 ** Description      This function calls the application callback with a suspend
908 **                  indication.
909 **
910 ** Returns          Nothing.
911 **
912 *******************************************************************************/
avdt_scb_hdl_suspend_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)913 void avdt_scb_hdl_suspend_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_SUSPEND_IND_EVT,
920                               NULL);
921 }
922 
923 /*******************************************************************************
924 **
925 ** Function         avdt_scb_hdl_suspend_rsp
926 **
927 ** Description      This function calls the application callback with a suspend
928 **                  confirm.
929 **
930 ** Returns          Nothing.
931 **
932 *******************************************************************************/
avdt_scb_hdl_suspend_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)933 void avdt_scb_hdl_suspend_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_SUSPEND_CFM_EVT,
938                               (tAVDT_CTRL *) &p_data->msg.hdr);
939 }
940 
941 /*******************************************************************************
942 **
943 ** Function         avdt_scb_hdl_tc_close
944 **
945 ** Description      This function is called when the transport channel is
946 **                  closed.  It marks the SCB as not in use and
947 **                  initializes certain SCB parameters.  It then sends
948 **                  an AVDT_CCB_UL_CLOSE_EVT to the CCB if the SCB
949 **                  initiated the close.  It then checks to see if the SCB
950 **                  is to be removed.  If it is it deallocates the SCB.  Finally,
951 **                  it calls the application callback with a close indication.
952 **
953 ** Returns          Nothing.
954 **
955 *******************************************************************************/
avdt_scb_hdl_tc_close(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)956 void avdt_scb_hdl_tc_close(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
957 {
958     UINT8               hdl = avdt_scb_to_hdl(p_scb);
959     tAVDT_CTRL_CBACK    *p_ctrl_cback = p_scb->cs.p_ctrl_cback;
960     tAVDT_CTRL          avdt_ctrl;
961     UINT8               event;
962     tAVDT_CCB           *p_ccb = p_scb->p_ccb;
963     BD_ADDR remote_addr;
964 
965 
966     memcpy (remote_addr, p_ccb->peer_addr, BD_ADDR_LEN);
967 
968     /* set up hdr */
969     avdt_ctrl.hdr.err_code = p_scb->close_code;
970     if (p_data) {
971         avdt_ctrl.hdr.err_param = p_data->close.disc_rsn;
972     } else {
973         avdt_ctrl.hdr.err_param = AVDT_DISC_RSN_NORMAL;
974     }
975 
976     /* clear sep variables */
977     avdt_scb_clr_vars(p_scb, p_data);
978     p_scb->media_seq = 0;
979     p_scb->cong = FALSE;
980 
981     /* free pkt we're holding, if any */
982     if (p_scb->p_pkt != NULL) {
983         osi_free(p_scb->p_pkt);
984         p_scb->p_pkt = NULL;
985     }
986 
987     /* stop transport channel timer */
988     btu_stop_timer(&p_scb->timer_entry);
989 
990     if ((p_scb->role == AVDT_CLOSE_INT) || (p_scb->role == AVDT_OPEN_INT)) {
991         /* tell ccb we're done with signaling channel */
992         avdt_ccb_event(p_ccb, AVDT_CCB_UL_CLOSE_EVT, NULL);
993     }
994     event = (p_scb->role == AVDT_CLOSE_INT) ? AVDT_CLOSE_CFM_EVT : AVDT_CLOSE_IND_EVT;
995     p_scb->role = AVDT_CLOSE_ACP;
996 
997     if (p_scb->remove) {
998         avdt_scb_dealloc(p_scb, NULL);
999     }
1000 
1001     /* call app callback */
1002     (*p_ctrl_cback)(hdl, remote_addr, event, &avdt_ctrl);
1003 }
1004 
1005 /*******************************************************************************
1006 **
1007 ** Function         avdt_scb_snd_delay_rpt_req
1008 **
1009 ** Description      This function calls the application callback with a delay
1010 **                  report.
1011 **
1012 ** Returns          Nothing.
1013 **
1014 *******************************************************************************/
avdt_scb_snd_delay_rpt_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1015 void avdt_scb_snd_delay_rpt_req (tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1016 {
1017     avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_DELAY_RPT, (tAVDT_MSG *) &p_data->apidelay);
1018 }
1019 
1020 /*******************************************************************************
1021 **
1022 ** Function         avdt_scb_hdl_delay_rpt_cmd
1023 **
1024 ** Description      This function calls the application callback with a delay
1025 **                  report.
1026 **
1027 ** Returns          Nothing.
1028 **
1029 *******************************************************************************/
avdt_scb_hdl_delay_rpt_cmd(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1030 void avdt_scb_hdl_delay_rpt_cmd (tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1031 {
1032     (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
1033                               p_scb->p_ccb ? p_scb->p_ccb->peer_addr : NULL,
1034                               AVDT_DELAY_REPORT_EVT,
1035                               (tAVDT_CTRL *) &p_data->msg.hdr);
1036 
1037     if (p_scb->p_ccb) {
1038         avdt_msg_send_rsp(p_scb->p_ccb, AVDT_SIG_DELAY_RPT, &p_data->msg);
1039     } else {
1040         avdt_scb_rej_not_in_use(p_scb, p_data);
1041     }
1042 }
1043 
1044 /*******************************************************************************
1045 **
1046 ** Function         avdt_scb_hdl_delay_rpt_rsp
1047 **
1048 ** Description      This function calls the application callback with a delay
1049 **                  report.
1050 **
1051 ** Returns          Nothing.
1052 **
1053 *******************************************************************************/
avdt_scb_hdl_delay_rpt_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1054 void avdt_scb_hdl_delay_rpt_rsp (tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1055 {
1056     (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
1057                               p_scb->p_ccb ? p_scb->p_ccb->peer_addr : NULL,
1058                               AVDT_DELAY_REPORT_CFM_EVT,
1059                               (tAVDT_CTRL *) &p_data->msg.hdr);
1060 }
1061 
1062 #if AVDT_REPORTING == TRUE
1063 /*******************************************************************************
1064 **
1065 ** Function         avdt_scb_hdl_tc_close_sto
1066 **
1067 ** Description      This function is called when a channel is closed in OPEN
1068 **                  state.  Check the channel type and process accordingly.
1069 **
1070 ** Returns          Nothing.
1071 **
1072 *******************************************************************************/
avdt_scb_hdl_tc_close_sto(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1073 void avdt_scb_hdl_tc_close_sto(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1074 {
1075     tAVDT_CTRL          avdt_ctrl;
1076     /* AVDT_CHAN_SIG does not visit this action */
1077     if (p_data && p_data->close.type != AVDT_CHAN_MEDIA) {
1078         /* it's reporting or recovery channel,
1079          * the channel close in open state means the peer does not support it */
1080         if (p_data->close.old_tc_state == AVDT_AD_ST_OPEN) {
1081             avdt_ctrl.hdr.err_code = 0;
1082             avdt_ctrl.hdr.err_param = 0;
1083             /* call app callback */
1084             (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
1085                                       p_scb->p_ccb ? p_scb->p_ccb->peer_addr : NULL,
1086                                       AVDT_REPORT_DISCONN_EVT, &avdt_ctrl);
1087         }
1088     } else {
1089         /* must be in OPEN state. need to go back to idle */
1090         avdt_scb_event(p_scb, AVDT_SCB_MSG_ABORT_RSP_EVT, NULL);
1091         avdt_scb_hdl_tc_close(p_scb, p_data);
1092     }
1093 }
1094 #endif
1095 
1096 /*******************************************************************************
1097 **
1098 ** Function         avdt_scb_hdl_tc_open
1099 **
1100 ** Description      This function is called when the transport channel is
1101 **                  opened while in the opening state.  It calls the
1102 **                  application callback with an open indication or open
1103 **                  confirm depending on who initiated the open procedure.
1104 **
1105 ** Returns          Nothing.
1106 **
1107 *******************************************************************************/
avdt_scb_hdl_tc_open(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1108 void avdt_scb_hdl_tc_open(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1109 {
1110     UINT8   event;
1111 #if AVDT_REPORTING == TRUE
1112     UINT8   role;
1113 #endif
1114 
1115     /* stop transport channel connect timer */
1116     btu_stop_timer(&p_scb->timer_entry);
1117 
1118     event = (p_scb->role == AVDT_OPEN_INT) ? AVDT_OPEN_CFM_EVT : AVDT_OPEN_IND_EVT;
1119     p_data->open.hdr.err_code = 0;
1120 
1121     AVDT_TRACE_DEBUG("psc_mask: cfg: 0x%x, req:0x%x, cur: 0x%x\n",
1122                      p_scb->cs.cfg.psc_mask, p_scb->req_cfg.psc_mask, p_scb->curr_cfg.psc_mask);
1123 #if AVDT_REPORTING == TRUE
1124     if (p_scb->curr_cfg.psc_mask & AVDT_PSC_REPORT) {
1125         /* open the reporting channel, if both devices support it */
1126         role = (p_scb->role == AVDT_OPEN_INT) ? AVDT_INT : AVDT_ACP;
1127         avdt_ad_open_req(AVDT_CHAN_REPORT, p_scb->p_ccb, p_scb, role);
1128     }
1129 #endif
1130 
1131     /* call app callback */
1132     (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
1133                               p_scb->p_ccb ? p_scb->p_ccb->peer_addr : NULL,
1134                               event,
1135                               (tAVDT_CTRL *) &p_data->open);
1136 }
1137 
1138 #if AVDT_REPORTING == TRUE
1139 /*******************************************************************************
1140 **
1141 ** Function         avdt_scb_hdl_tc_open_sto
1142 **
1143 ** Description      This function is called when the transport channel is
1144 **                  opened while in the opening state.  It calls the
1145 **                  application callback with an open indication or open
1146 **                  confirm depending on who initiated the open procedure.
1147 **
1148 ** Returns          Nothing.
1149 **
1150 *******************************************************************************/
avdt_scb_hdl_tc_open_sto(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1151 void avdt_scb_hdl_tc_open_sto(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1152 {
1153     tAVDT_CTRL          avdt_ctrl;
1154     /* open reporting channel here, when it is implemented */
1155 
1156     /* call app callback */
1157     if (p_data->open.hdr.err_code == AVDT_CHAN_REPORT) {
1158         avdt_ctrl.hdr.err_code = 0;
1159         avdt_ctrl.hdr.err_param = 1;
1160         (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
1161                                   p_scb->p_ccb ? p_scb->p_ccb->peer_addr : NULL,
1162                                   AVDT_REPORT_CONN_EVT, &avdt_ctrl);
1163     }
1164 }
1165 #endif
1166 
1167 /*******************************************************************************
1168 **
1169 ** Function         avdt_scb_hdl_write_req_no_frag
1170 **
1171 ** Description      This function frees the media packet currently stored in
1172 **                  the SCB, if any.  Then it builds a new media packet from
1173 **                  with the passed in buffer and stores it in the SCB.
1174 **
1175 ** Returns          Nothing.
1176 **
1177 *******************************************************************************/
avdt_scb_hdl_write_req_no_frag(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1178 void avdt_scb_hdl_write_req_no_frag(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1179 {
1180     UINT8   *p;
1181     UINT32  ssrc;
1182 
1183     /* free packet we're holding, if any; to be replaced with new */
1184     if (p_scb->p_pkt != NULL) {
1185         osi_free(p_scb->p_pkt);
1186         p_scb->p_pkt = NULL;
1187 
1188         /* this shouldn't be happening */
1189         AVDT_TRACE_WARNING("Dropped media packet; congested");
1190     }
1191 
1192     /* build a media packet */
1193     /* Add RTP header if required */
1194     if ( !(p_data->apiwrite.opt & AVDT_DATA_OPT_NO_RTP) ) {
1195         ssrc = avdt_scb_gen_ssrc(p_scb);
1196 
1197         p_data->apiwrite.p_buf->len += AVDT_MEDIA_HDR_SIZE;
1198         p_data->apiwrite.p_buf->offset -= AVDT_MEDIA_HDR_SIZE;
1199         p_scb->media_seq++;
1200         p = (UINT8 *)(p_data->apiwrite.p_buf + 1) + p_data->apiwrite.p_buf->offset;
1201 
1202         UINT8_TO_BE_STREAM(p, AVDT_MEDIA_OCTET1);
1203         UINT8_TO_BE_STREAM(p, p_data->apiwrite.m_pt);
1204         UINT16_TO_BE_STREAM(p, p_scb->media_seq);
1205         UINT32_TO_BE_STREAM(p, p_data->apiwrite.time_stamp);
1206         UINT32_TO_BE_STREAM(p, ssrc);
1207     }
1208 
1209     /* store it */
1210     p_scb->p_pkt = p_data->apiwrite.p_buf;
1211 }
1212 
1213 #if AVDT_MULTIPLEXING == TRUE
1214 /*******************************************************************************
1215 **
1216 ** Function         avdt_scb_hdl_write_req_frag
1217 **
1218 ** Description      This function builds a new fragments of media packet from
1219 **                  the passed in buffers and stores them in the SCB.
1220 **
1221 ** Returns          Nothing.
1222 **
1223 *******************************************************************************/
avdt_scb_hdl_write_req_frag(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1224 void avdt_scb_hdl_write_req_frag(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1225 {
1226     UINT8   *p;
1227     UINT32  ssrc;
1228 
1229     /* free fragments we're holding, if any; it shouldn't happen */
1230     if (!fixed_queue_is_empty(p_scb->frag_q))
1231     {
1232         /* this shouldn't be happening */
1233         AVDT_TRACE_WARNING("*** Dropped media packet; congested");
1234         BT_HDR *p_frag;
1235         while ((p_frag = (BT_HDR*)fixed_queue_dequeue(p_scb->frag_q, 0)) != NULL)
1236              osi_free(p_frag);
1237     }
1238 
1239     /* build a media fragments */
1240     p_scb->frag_off = p_data->apiwrite.data_len;
1241     p_scb->p_next_frag = p_data->apiwrite.p_data;
1242 
1243     ssrc = avdt_scb_gen_ssrc(p_scb);
1244 
1245     if (! fixed_queue_is_empty(p_scb->frag_q)) {
1246         list_t *list = fixed_queue_get_list(p_scb->frag_q);
1247         const list_node_t *node = list_begin(list);
1248         if (node != list_end(list)) {
1249             BT_HDR *p_frag = (BT_HDR *)list_node(node);
1250             node = list_next(node);
1251 
1252             /* get first packet */
1253             /* posit on Adaptation Layer header */
1254             p_frag->len += AVDT_AL_HDR_SIZE + AVDT_MEDIA_HDR_SIZE;
1255             p_frag->offset -= AVDT_AL_HDR_SIZE + AVDT_MEDIA_HDR_SIZE;
1256             p = (UINT8 *)(p_frag + 1) + p_frag->offset;
1257 
1258             /* Adaptation Layer header */
1259             /* TSID, no-fragment bit and coding of length (in 2 length octets
1260              * following)
1261              */
1262             *p++ = (p_scb->curr_cfg.mux_tsid_media<<3) | AVDT_ALH_LCODE_16BIT;
1263 
1264             /* length of all remaining transport packet */
1265             UINT16_TO_BE_STREAM(p, p_frag->layer_specific + AVDT_MEDIA_HDR_SIZE );
1266             /* media header */
1267             UINT8_TO_BE_STREAM(p, AVDT_MEDIA_OCTET1);
1268             UINT8_TO_BE_STREAM(p, p_data->apiwrite.m_pt);
1269             UINT16_TO_BE_STREAM(p, p_scb->media_seq);
1270             UINT32_TO_BE_STREAM(p, p_data->apiwrite.time_stamp);
1271             UINT32_TO_BE_STREAM(p, ssrc);
1272             p_scb->media_seq++;
1273         }
1274 
1275         for ( ; node != list_end(list); node = list_next(node)) {
1276             BT_HDR *p_frag = (BT_HDR *)list_node(node);
1277 
1278             /* posit on Adaptation Layer header */
1279             p_frag->len += AVDT_AL_HDR_SIZE;
1280             p_frag->offset -= AVDT_AL_HDR_SIZE;
1281             p = (UINT8 *)(p_frag + 1) + p_frag->offset;
1282             /* Adaptation Layer header */
1283             /* TSID, fragment bit and coding of length (in 2 length octets
1284              * following)
1285              */
1286             *p++ = (p_scb->curr_cfg.mux_tsid_media << 3) |
1287                 (AVDT_ALH_FRAG_MASK | AVDT_ALH_LCODE_16BIT);
1288 
1289             /* length of all remaining transport packet */
1290             UINT16_TO_BE_STREAM(p, p_frag->layer_specific);
1291         }
1292     }
1293 }
1294 #endif
1295 
1296 
1297 /*******************************************************************************
1298 **
1299 ** Function         avdt_scb_hdl_write_req
1300 **
1301 ** Description      This function calls one of the two versions of building functions
1302 **                  for case with and without fragmentation
1303 **
1304 ** Returns          Nothing.
1305 **
1306 *******************************************************************************/
avdt_scb_hdl_write_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1307 void avdt_scb_hdl_write_req(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1308 {
1309 #if AVDT_MULTIPLEXING == TRUE
1310     if (fixed_queue_is_empty(p_scb->frag_q))
1311 #endif
1312     {
1313         avdt_scb_hdl_write_req_no_frag(p_scb, p_data);
1314     }
1315 #if AVDT_MULTIPLEXING == TRUE
1316     else {
1317         avdt_scb_hdl_write_req_frag(p_scb, p_data);
1318     }
1319 #endif
1320 }
1321 
1322 /*******************************************************************************
1323 **
1324 ** Function         avdt_scb_snd_abort_req
1325 **
1326 ** Description      This function sends an abort command message.
1327 **
1328 ** Returns          Nothing.
1329 **
1330 *******************************************************************************/
avdt_scb_snd_abort_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1331 void avdt_scb_snd_abort_req(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1332 {
1333     tAVDT_EVT_HDR   hdr;
1334     UNUSED(p_data);
1335 
1336     if (p_scb->p_ccb != NULL) {
1337         p_scb->role = AVDT_CLOSE_INT;
1338 
1339         hdr.seid = p_scb->peer_seid;
1340 
1341         avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_ABORT, (tAVDT_MSG *) &hdr);
1342     }
1343 }
1344 
1345 /*******************************************************************************
1346 **
1347 ** Function         avdt_scb_snd_abort_rsp
1348 **
1349 ** Description      This function sends an abort response message.
1350 **
1351 ** Returns          Nothing.
1352 **
1353 *******************************************************************************/
avdt_scb_snd_abort_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1354 void avdt_scb_snd_abort_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1355 {
1356     UNUSED(p_scb);
1357 
1358     avdt_msg_send_rsp(avdt_ccb_by_idx(p_data->msg.hdr.ccb_idx), AVDT_SIG_ABORT,
1359                       &p_data->msg);
1360 }
1361 
1362 /*******************************************************************************
1363 **
1364 ** Function         avdt_scb_snd_close_req
1365 **
1366 ** Description      This function sends a close command message.
1367 **
1368 ** Returns          Nothing.
1369 **
1370 *******************************************************************************/
avdt_scb_snd_close_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1371 void avdt_scb_snd_close_req(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1372 {
1373     tAVDT_EVT_HDR   hdr;
1374     UNUSED(p_data);
1375 
1376     p_scb->role = AVDT_CLOSE_INT;
1377 
1378     hdr.seid = p_scb->peer_seid;
1379 
1380     avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_CLOSE, (tAVDT_MSG *) &hdr);
1381 }
1382 
1383 /*******************************************************************************
1384 **
1385 ** Function         avdt_scb_snd_stream_close
1386 **
1387 ** Description      This function sends a close command message.
1388 **
1389 ** Returns          Nothing.
1390 **
1391 *******************************************************************************/
avdt_scb_snd_stream_close(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1392 void avdt_scb_snd_stream_close(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1393 {
1394 #if AVDT_MULTIPLEXING == TRUE
1395     AVDT_TRACE_WARNING("%s c:%d, off:%d", __func__,
1396         fixed_queue_length(p_scb->frag_q), p_scb->frag_off);
1397 
1398     /* clean fragments queue */
1399     BT_HDR *p_frag;
1400     while ((p_frag = (BT_HDR*)fixed_queue_dequeue(p_scb->frag_q, 0)) != NULL) {
1401         osi_free(p_frag);
1402     }
1403     p_scb->frag_off = 0;
1404 #endif
1405     if (p_scb->p_pkt) {
1406         osi_free(p_scb->p_pkt);
1407         p_scb->p_pkt = NULL;
1408     }
1409 
1410     avdt_scb_snd_close_req(p_scb, p_data);
1411 }
1412 
1413 /*******************************************************************************
1414 **
1415 ** Function         avdt_scb_snd_close_rsp
1416 **
1417 ** Description      This function sends a close response message.
1418 **
1419 ** Returns          Nothing.
1420 **
1421 *******************************************************************************/
avdt_scb_snd_close_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1422 void avdt_scb_snd_close_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1423 {
1424     avdt_msg_send_rsp(p_scb->p_ccb, AVDT_SIG_CLOSE, &p_data->msg);
1425 }
1426 
1427 /*******************************************************************************
1428 **
1429 ** Function         avdt_scb_snd_getconfig_req
1430 **
1431 ** Description      This function sends a get configuration command message.
1432 **
1433 ** Returns          Nothing.
1434 **
1435 *******************************************************************************/
avdt_scb_snd_getconfig_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1436 void avdt_scb_snd_getconfig_req(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1437 {
1438     tAVDT_EVT_HDR   hdr;
1439     UNUSED(p_data);
1440 
1441     hdr.seid = p_scb->peer_seid;
1442 
1443     avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_GETCONFIG, (tAVDT_MSG *) &hdr);
1444 }
1445 
1446 /*******************************************************************************
1447 **
1448 ** Function         avdt_scb_snd_getconfig_rsp
1449 **
1450 ** Description      This function sends a get configuration response message.
1451 **
1452 ** Returns          Nothing.
1453 **
1454 *******************************************************************************/
avdt_scb_snd_getconfig_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1455 void avdt_scb_snd_getconfig_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1456 {
1457     avdt_msg_send_rsp(p_scb->p_ccb, AVDT_SIG_GETCONFIG, &p_data->msg);
1458 }
1459 
1460 /*******************************************************************************
1461 **
1462 ** Function         avdt_scb_snd_open_req
1463 **
1464 ** Description      This function sends an open command message.
1465 **
1466 ** Returns          Nothing.
1467 **
1468 *******************************************************************************/
avdt_scb_snd_open_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1469 void avdt_scb_snd_open_req(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1470 {
1471     tAVDT_EVT_HDR   hdr;
1472     UNUSED(p_data);
1473 
1474     hdr.seid = p_scb->peer_seid;
1475 
1476     avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_OPEN, (tAVDT_MSG *) &hdr);
1477 }
1478 
1479 /*******************************************************************************
1480 **
1481 ** Function         avdt_scb_snd_open_rsp
1482 **
1483 ** Description      This function sends an open response message.  It also
1484 **                  calls avdt_ad_open_req() to accept a transport channel
1485 **                  connection.
1486 **
1487 ** Returns          Nothing.
1488 **
1489 *******************************************************************************/
avdt_scb_snd_open_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1490 void avdt_scb_snd_open_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1491 {
1492     /* notify adaption that we're waiting for transport channel open */
1493     p_scb->role = AVDT_OPEN_ACP;
1494     avdt_ad_open_req(AVDT_CHAN_MEDIA, p_scb->p_ccb, p_scb, AVDT_ACP);
1495 
1496     /* send response */
1497     avdt_msg_send_rsp(p_scb->p_ccb, AVDT_SIG_OPEN, &p_data->msg);
1498 
1499     /* start tc connect timer */
1500     btu_start_timer(&p_scb->timer_entry, BTU_TTYPE_AVDT_SCB_TC, AVDT_SCB_TC_CONN_TOUT);
1501 }
1502 
1503 /*******************************************************************************
1504 **
1505 ** Function         avdt_scb_snd_reconfig_req
1506 **
1507 ** Description      This function stores the configuration parameters in the
1508 **                  SCB and sends a reconfiguration command message.
1509 **
1510 ** Returns          Nothing.
1511 **
1512 *******************************************************************************/
avdt_scb_snd_reconfig_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1513 void avdt_scb_snd_reconfig_req(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1514 {
1515     memcpy(&p_scb->req_cfg, p_data->msg.config_cmd.p_cfg, sizeof(tAVDT_CFG));
1516     p_data->msg.hdr.seid = p_scb->peer_seid;
1517     avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_RECONFIG, &p_data->msg);
1518 }
1519 
1520 /*******************************************************************************
1521 **
1522 ** Function         avdt_scb_snd_reconfig_rsp
1523 **
1524 ** Description      This function stores the configuration parameters in the
1525 **                  SCB and sends a reconfiguration response message.
1526 **
1527 ** Returns          Nothing.
1528 **
1529 *******************************************************************************/
avdt_scb_snd_reconfig_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1530 void avdt_scb_snd_reconfig_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1531 {
1532     if (p_data->msg.hdr.err_code == 0) {
1533         /* store new configuration */
1534         if (p_scb->req_cfg.num_codec > 0) {
1535             p_scb->curr_cfg.num_codec = p_scb->req_cfg.num_codec;
1536             memcpy(p_scb->curr_cfg.codec_info, p_scb->req_cfg.codec_info, AVDT_CODEC_SIZE);
1537         }
1538         if (p_scb->req_cfg.num_protect > 0) {
1539             p_scb->curr_cfg.num_protect = p_scb->req_cfg.num_protect;
1540             memcpy(p_scb->curr_cfg.protect_info, p_scb->req_cfg.protect_info, AVDT_PROTECT_SIZE);
1541         }
1542 
1543         /* send response */
1544         avdt_msg_send_rsp(p_scb->p_ccb, AVDT_SIG_RECONFIG, &p_data->msg);
1545     } else {
1546         /* send reject */
1547         avdt_msg_send_rej(p_scb->p_ccb, AVDT_SIG_RECONFIG, &p_data->msg);
1548     }
1549 }
1550 
1551 /*******************************************************************************
1552 **
1553 ** Function         avdt_scb_snd_security_req
1554 **
1555 ** Description      This function sends a security command message.
1556 **
1557 ** Returns          Nothing.
1558 **
1559 *******************************************************************************/
avdt_scb_snd_security_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1560 void avdt_scb_snd_security_req(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1561 {
1562     p_data->msg.hdr.seid = p_scb->peer_seid;
1563     avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_SECURITY, &p_data->msg);
1564 }
1565 
1566 /*******************************************************************************
1567 **
1568 ** Function         avdt_scb_snd_security_rsp
1569 **
1570 ** Description      This function sends a security response message.
1571 **
1572 ** Returns          Nothing.
1573 **
1574 *******************************************************************************/
avdt_scb_snd_security_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1575 void avdt_scb_snd_security_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1576 {
1577     if (p_data->msg.hdr.err_code == 0) {
1578         avdt_msg_send_rsp(p_scb->p_ccb, AVDT_SIG_SECURITY, &p_data->msg);
1579     } else {
1580         avdt_msg_send_rej(p_scb->p_ccb, AVDT_SIG_SECURITY, &p_data->msg);
1581     }
1582 }
1583 
1584 /*******************************************************************************
1585 **
1586 ** Function         avdt_scb_snd_setconfig_rej
1587 **
1588 ** Description      This function marks the SCB as not in use and sends a
1589 **                  set configuration reject message.
1590 **
1591 ** Returns          Nothing.
1592 **
1593 *******************************************************************************/
avdt_scb_snd_setconfig_rej(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1594 void avdt_scb_snd_setconfig_rej(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1595 {
1596     if (p_scb->p_ccb != NULL) {
1597         avdt_msg_send_rej(p_scb->p_ccb, AVDT_SIG_SETCONFIG, &p_data->msg);
1598 
1599         /* clear scb variables */
1600         avdt_scb_clr_vars(p_scb, p_data);
1601     }
1602 }
1603 
1604 /*******************************************************************************
1605 **
1606 ** Function         avdt_scb_snd_setconfig_req
1607 **
1608 ** Description      This function marks the SCB as in use and copies the
1609 **                  configuration parameters to the SCB.  Then the function
1610 **                  sends a set configuration command message and initiates
1611 **                  opening of the signaling channel.
1612 **
1613 ** Returns          Nothing.
1614 **
1615 *******************************************************************************/
avdt_scb_snd_setconfig_req(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1616 void avdt_scb_snd_setconfig_req(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1617 {
1618     tAVDT_CFG *p_req, *p_cfg;
1619 
1620     /* copy API parameters to scb, set scb as in use */
1621     p_scb->in_use = TRUE;
1622     p_scb->p_ccb = avdt_ccb_by_idx(p_data->msg.config_cmd.hdr.ccb_idx);
1623     p_scb->peer_seid = p_data->msg.config_cmd.hdr.seid;
1624     p_req = p_data->msg.config_cmd.p_cfg;
1625     p_cfg = &p_scb->cs.cfg;
1626 #if AVDT_MULTIPLEXING == TRUE
1627     p_req->mux_tsid_media = p_cfg->mux_tsid_media;
1628     p_req->mux_tcid_media = p_cfg->mux_tcid_media;
1629     if (p_req->psc_mask & AVDT_PSC_REPORT) {
1630         p_req->mux_tsid_report = p_cfg->mux_tsid_report;
1631         p_req->mux_tcid_report = p_cfg->mux_tcid_report;
1632     }
1633 #endif
1634     memcpy(&p_scb->req_cfg, p_data->msg.config_cmd.p_cfg, sizeof(tAVDT_CFG));
1635 
1636     avdt_msg_send_cmd(p_scb->p_ccb, p_scb, AVDT_SIG_SETCONFIG, &p_data->msg);
1637 
1638     /* tell ccb to open channel */
1639     avdt_ccb_event(p_scb->p_ccb, AVDT_CCB_UL_OPEN_EVT, NULL);
1640 }
1641 
1642 /*******************************************************************************
1643 **
1644 ** Function         avdt_scb_snd_setconfig_rsp
1645 **
1646 ** Description      This function copies the requested configuration into the
1647 **                  current configuration and sends a set configuration
1648 **                  response message.
1649 **
1650 ** Returns          Nothing.
1651 **
1652 *******************************************************************************/
avdt_scb_snd_setconfig_rsp(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1653 void avdt_scb_snd_setconfig_rsp(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1654 {
1655     if (p_scb->p_ccb != NULL) {
1656         memcpy(&p_scb->curr_cfg, &p_scb->req_cfg, sizeof(tAVDT_CFG));
1657 
1658         avdt_msg_send_rsp(p_scb->p_ccb, AVDT_SIG_SETCONFIG, &p_data->msg);
1659     }
1660 }
1661 
1662 /*******************************************************************************
1663 **
1664 ** Function         avdt_scb_snd_tc_close
1665 **
1666 ** Description      This function calls avdt_ad_close_req() to close the
1667 **                  transport channel for this SCB.
1668 **
1669 ** Returns          Nothing.
1670 **
1671 *******************************************************************************/
avdt_scb_snd_tc_close(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1672 void avdt_scb_snd_tc_close(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1673 {
1674     UNUSED(p_data);
1675 
1676 #if AVDT_REPORTING == TRUE
1677     if (p_scb->curr_cfg.psc_mask & AVDT_PSC_REPORT) {
1678         avdt_ad_close_req(AVDT_CHAN_REPORT, p_scb->p_ccb, p_scb);
1679     }
1680 #endif
1681     avdt_ad_close_req(AVDT_CHAN_MEDIA, p_scb->p_ccb, p_scb);
1682 }
1683 
1684 /*******************************************************************************
1685 **
1686 ** Function         avdt_scb_cb_err
1687 **
1688 ** Description      This function calls the application callback function
1689 **                  indicating an error.
1690 **
1691 ** Returns          Nothing.
1692 **
1693 *******************************************************************************/
avdt_scb_cb_err(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1694 void avdt_scb_cb_err(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1695 {
1696     tAVDT_CTRL          avdt_ctrl;
1697     UNUSED(p_data);
1698 
1699     /* set error code and parameter */
1700     avdt_ctrl.hdr.err_code = AVDT_ERR_BAD_STATE;
1701     avdt_ctrl.hdr.err_param = 0;
1702 
1703     /* call callback, using lookup table to get callback event */
1704     (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb),
1705                               NULL,
1706                               avdt_scb_cback_evt[p_scb->curr_evt],
1707                               &avdt_ctrl);
1708 }
1709 
1710 /*******************************************************************************
1711 **
1712 ** Function         avdt_scb_cong_state
1713 **
1714 ** Description      This function sets the congestion state of the SCB media
1715 **                  transport channel.
1716 **
1717 ** Returns          Nothing.
1718 **
1719 *******************************************************************************/
avdt_scb_cong_state(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1720 void avdt_scb_cong_state(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1721 {
1722     p_scb->cong = p_data->llcong;
1723 }
1724 
1725 /*******************************************************************************
1726 **
1727 ** Function         avdt_scb_rej_state
1728 **
1729 ** Description      This function sends a reject message to the peer indicating
1730 **                  incorrect state for the received command message.
1731 **
1732 ** Returns          Nothing.
1733 **
1734 *******************************************************************************/
avdt_scb_rej_state(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1735 void avdt_scb_rej_state(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1736 {
1737     UNUSED(p_scb);
1738 
1739     p_data->msg.hdr.err_code = AVDT_ERR_BAD_STATE;
1740     p_data->msg.hdr.err_param = 0;
1741     avdt_msg_send_rej(avdt_ccb_by_idx(p_data->msg.hdr.ccb_idx),
1742                       p_data->msg.hdr.sig_id, &p_data->msg);
1743 }
1744 
1745 /*******************************************************************************
1746 **
1747 ** Function         avdt_scb_rej_in_use
1748 **
1749 ** Description      This function sends a reject message to the peer indicating
1750 **                  the stream is in use.
1751 **
1752 ** Returns          Nothing.
1753 **
1754 *******************************************************************************/
avdt_scb_rej_in_use(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1755 void avdt_scb_rej_in_use(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1756 {
1757     UNUSED(p_scb);
1758 
1759     p_data->msg.hdr.err_code = AVDT_ERR_IN_USE;
1760     p_data->msg.hdr.err_param = 0;
1761     avdt_msg_send_rej(avdt_ccb_by_idx(p_data->msg.hdr.ccb_idx),
1762                       p_data->msg.hdr.sig_id, &p_data->msg);
1763 }
1764 
1765 /*******************************************************************************
1766 **
1767 ** Function         avdt_scb_rej_not_in_use
1768 **
1769 ** Description      This function sends a reject message to the peer indicating
1770 **                  the stream is in use.
1771 **
1772 ** Returns          Nothing.
1773 **
1774 *******************************************************************************/
avdt_scb_rej_not_in_use(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1775 void avdt_scb_rej_not_in_use(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1776 {
1777     UNUSED(p_scb);
1778 
1779     p_data->msg.hdr.err_code = AVDT_ERR_NOT_IN_USE;
1780     p_data->msg.hdr.err_param = 0;
1781     avdt_msg_send_rej(avdt_ccb_by_idx(p_data->msg.hdr.ccb_idx),
1782                       p_data->msg.hdr.sig_id, &p_data->msg);
1783 }
1784 
1785 /*******************************************************************************
1786 **
1787 ** Function         avdt_scb_set_remove
1788 **
1789 ** Description      This function marks an SCB to be removed.
1790 **
1791 ** Returns          Nothing.
1792 **
1793 *******************************************************************************/
avdt_scb_set_remove(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1794 void avdt_scb_set_remove(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1795 {
1796     UNUSED(p_data);
1797 
1798     p_scb->remove = TRUE;
1799 }
1800 
1801 /*******************************************************************************
1802 **
1803 ** Function         avdt_scb_free_pkt
1804 **
1805 ** Description      This function frees the media packet passed in.
1806 **
1807 ** Returns          Nothing.
1808 **
1809 *******************************************************************************/
avdt_scb_free_pkt(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1810 void avdt_scb_free_pkt(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1811 {
1812     tAVDT_CTRL      avdt_ctrl;
1813 
1814     /* set error code and parameter */
1815     avdt_ctrl.hdr.err_code = AVDT_ERR_BAD_STATE;
1816     avdt_ctrl.hdr.err_param = 0;
1817 
1818     /* p_buf can be NULL in case using of fragments queue frag_q */
1819     if (p_data->apiwrite.p_buf) {
1820         osi_free(p_data->apiwrite.p_buf);
1821         p_data->apiwrite.p_buf = NULL;
1822     }
1823 
1824 #if AVDT_MULTIPLEXING == TRUE
1825     /* clean fragments queue */
1826     BT_HDR          *p_frag;
1827     while ((p_frag = (BT_HDR*)fixed_queue_dequeue(p_scb->frag_q, 0)) != NULL) {
1828          osi_free(p_frag);
1829 	}
1830 #endif
1831 
1832     AVDT_TRACE_WARNING("Dropped media packet");
1833 
1834     /* we need to call callback to keep data flow going */
1835     (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb), NULL, AVDT_WRITE_CFM_EVT,
1836                               &avdt_ctrl);
1837 }
1838 
1839 /*******************************************************************************
1840 **
1841 ** Function         avdt_scb_clr_pkt
1842 **
1843 ** Description      This function frees the media packet stored in the SCB.
1844 **
1845 ** Returns          Nothing.
1846 **
1847 *******************************************************************************/
avdt_scb_clr_pkt(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1848 void avdt_scb_clr_pkt(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1849 {
1850     tAVDT_CTRL      avdt_ctrl;
1851     tAVDT_CCB       *p_ccb;
1852     UINT8           tcid;
1853     UINT16          lcid;
1854     UNUSED(p_data);
1855 
1856     /* set error code and parameter */
1857     avdt_ctrl.hdr.err_code = AVDT_ERR_BAD_STATE;
1858     avdt_ctrl.hdr.err_param = 0;
1859     /* flush the media data queued at L2CAP */
1860     if ((p_ccb = p_scb->p_ccb) != NULL) {
1861         /* get tcid from type, scb */
1862         tcid = avdt_ad_type_to_tcid(AVDT_CHAN_MEDIA, p_scb);
1863 
1864         lcid = avdt_cb.ad.rt_tbl[avdt_ccb_to_idx(p_ccb)][tcid].lcid;
1865         L2CA_FlushChannel (lcid, L2CAP_FLUSH_CHANS_ALL);
1866     }
1867 
1868     if (p_scb->p_pkt != NULL) {
1869         osi_free(p_scb->p_pkt);
1870         p_scb->p_pkt = NULL;
1871 
1872         AVDT_TRACE_DEBUG("Dropped stored media packet");
1873 
1874         /* we need to call callback to keep data flow going */
1875         (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb), NULL, AVDT_WRITE_CFM_EVT,
1876                                   &avdt_ctrl);
1877     }
1878 #if AVDT_MULTIPLEXING == TRUE
1879     else if (!fixed_queue_is_empty(p_scb->frag_q)) {
1880         AVDT_TRACE_DEBUG("Dropped fragments queue");
1881         /* clean fragments queue */
1882         BT_HDR *p_frag;
1883         while ((p_frag = (BT_HDR*)fixed_queue_dequeue(p_scb->frag_q, 0)) != NULL) {
1884              osi_free(p_frag);
1885 		}
1886         p_scb->frag_off = 0;
1887 
1888         /* we need to call callback to keep data flow going */
1889         (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb), NULL, AVDT_WRITE_CFM_EVT,
1890                                   &avdt_ctrl);
1891     }
1892 #endif
1893 }
1894 
1895 
1896 /*******************************************************************************
1897 **
1898 ** Function         avdt_scb_chk_snd_pkt
1899 **
1900 ** Description      This function checks if the SCB is congested, and if not
1901 **                  congested it sends a stored media packet, if any.  After it
1902 **                  sends the packet it calls the application callback function
1903 **                  with a write confirm.
1904 **
1905 ** Returns          Nothing.
1906 **
1907 *******************************************************************************/
avdt_scb_chk_snd_pkt(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1908 void avdt_scb_chk_snd_pkt(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1909 {
1910     tAVDT_CTRL      avdt_ctrl;
1911     BT_HDR          *p_pkt;
1912 #if AVDT_MULTIPLEXING == TRUE
1913     BOOLEAN         sent = FALSE;
1914     UINT8   res = AVDT_AD_SUCCESS;
1915     tAVDT_SCB_EVT data;
1916 #endif
1917     UNUSED(p_data);
1918 
1919     avdt_ctrl.hdr.err_code = 0;
1920 
1921     if (!p_scb->cong) {
1922         if (p_scb->p_pkt != NULL) {
1923             p_pkt = p_scb->p_pkt;
1924             p_scb->p_pkt = NULL;
1925             avdt_ad_write_req(AVDT_CHAN_MEDIA, p_scb->p_ccb, p_scb, p_pkt);
1926 
1927             (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb), NULL, AVDT_WRITE_CFM_EVT, &avdt_ctrl);
1928         }
1929 #if AVDT_MULTIPLEXING == TRUE
1930         else {
1931 #if 0
1932             AVDT_TRACE_DEBUG("num_q=%d\n",
1933                              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),
1934                              L2CAP_FLUSH_CHANS_GET);
1935 #endif
1936             while ((p_pkt = (BT_HDR*)fixed_queue_dequeue(p_scb->frag_q, 0)) != NULL) {
1937                 sent = TRUE;
1938                 AVDT_TRACE_DEBUG("Send fragment len=%d\n", p_pkt->len);
1939                 /* fragments queue contains fragment to send */
1940                 res = avdt_ad_write_req(AVDT_CHAN_MEDIA, p_scb->p_ccb, p_scb, p_pkt);
1941                 if (AVDT_AD_CONGESTED == res) {
1942                     p_scb->cong = TRUE;
1943                     AVDT_TRACE_DEBUG("avdt/l2c congested!!");
1944                     break;/* exit loop if channel became congested */
1945                 }
1946             }
1947             AVDT_TRACE_DEBUG("res=%d left=%d\n", res, p_scb->frag_off);
1948 
1949             if (p_scb->frag_off) {
1950                 if (AVDT_AD_SUCCESS == res || fixed_queue_is_empty(p_scb->frag_q)) {
1951                     /* all buffers were sent to L2CAP, compose more to queue */
1952                     avdt_scb_queue_frags(p_scb, &p_scb->p_next_frag, &p_scb->frag_off, p_scb->frag_q);
1953                     if (!fixed_queue_is_empty(p_scb->frag_q)) {
1954                         data.llcong = p_scb->cong;
1955                         avdt_scb_event(p_scb, AVDT_SCB_TC_CONG_EVT, &data);
1956                     }
1957                 }
1958             }
1959 
1960             /* Send event AVDT_WRITE_CFM_EVT if it was last fragment */
1961             else if (sent && fixed_queue_is_empty(p_scb->frag_q)) {
1962                 (*p_scb->cs.p_ctrl_cback)(avdt_scb_to_hdl(p_scb), NULL, AVDT_WRITE_CFM_EVT, &avdt_ctrl);
1963             }
1964         }
1965 #endif
1966     }
1967 }
1968 
1969 /*******************************************************************************
1970 **
1971 ** Function         avdt_scb_tc_timer
1972 **
1973 ** Description      This function is called to start a timer when the peer
1974 **                  initiates closing of the stream.  The timer verifies that
1975 **                  the peer disconnects the transport channel.
1976 **
1977 ** Returns          Nothing.
1978 **
1979 *******************************************************************************/
avdt_scb_tc_timer(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1980 void avdt_scb_tc_timer(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1981 {
1982     UNUSED(p_data);
1983 
1984     btu_start_timer(&p_scb->timer_entry, BTU_TTYPE_AVDT_SCB_TC, AVDT_SCB_TC_DISC_TOUT);
1985 }
1986 
1987 /*******************************************************************************
1988 **
1989 ** Function         avdt_scb_clr_vars
1990 **
1991 ** Description      This function initializes certain SCB variables.
1992 **
1993 ** Returns          Nothing.
1994 **
1995 *******************************************************************************/
avdt_scb_clr_vars(tAVDT_SCB * p_scb,tAVDT_SCB_EVT * p_data)1996 void avdt_scb_clr_vars(tAVDT_SCB *p_scb, tAVDT_SCB_EVT *p_data)
1997 {
1998     UNUSED(p_data);
1999 
2000     if ((p_scb->cs.tsep == AVDT_TSEP_SNK) && (!p_scb->sink_activated)) {
2001         p_scb->in_use = TRUE;
2002     } else {
2003         p_scb->in_use = FALSE;
2004     }
2005     p_scb->p_ccb = NULL;
2006     p_scb->peer_seid = 0;
2007 }
2008 
2009 #if AVDT_MULTIPLEXING == TRUE
2010 /*******************************************************************************
2011 **
2012 ** Function         avdt_scb_queue_frags
2013 **
2014 ** Description      This function breaks media payload into fragments
2015 **                  and put the fragments in the given queue.
2016 **
2017 ** Returns          Nothing.
2018 **
2019 *******************************************************************************/
avdt_scb_queue_frags(tAVDT_SCB * p_scb,UINT8 ** pp_data,UINT32 * p_data_len,fixed_queue_t * pq)2020 void avdt_scb_queue_frags(tAVDT_SCB *p_scb, UINT8 **pp_data, UINT32 *p_data_len, fixed_queue_t *pq)
2021 {
2022     UINT16  lcid;
2023     UINT16  num_frag;
2024     UINT16  mtu_used;
2025     UINT8   *p;
2026     BOOLEAN al_hdr = FALSE;
2027     UINT8   tcid;
2028     tAVDT_TC_TBL    *p_tbl;
2029     UINT16          buf_size;
2030     UINT16          offset = AVDT_MEDIA_OFFSET + AVDT_AL_HDR_SIZE;
2031     UINT16          cont_offset = offset - AVDT_MEDIA_HDR_SIZE;
2032     BT_HDR          *p_frag;
2033 
2034     tcid = avdt_ad_type_to_tcid(AVDT_CHAN_MEDIA, p_scb);
2035     lcid = avdt_cb.ad.rt_tbl[avdt_ccb_to_idx(p_scb->p_ccb)][tcid].lcid;
2036 
2037     if ( p_scb->frag_off != 0) {
2038         /* continuing process is usually triggered by un-congest event.
2039          * the number of buffers at L2CAP is very small (if not 0).
2040          * we do not need to L2CA_FlushChannel() */
2041         offset = cont_offset;
2042         al_hdr = TRUE;
2043         num_frag = AVDT_MAX_FRAG_COUNT;
2044     } else {
2045         num_frag = L2CA_FlushChannel(lcid, L2CAP_FLUSH_CHANS_GET);
2046         AVDT_TRACE_DEBUG("num_q=%d lcid=%d\n", num_frag, lcid);
2047         if (num_frag >= AVDT_MAX_FRAG_COUNT) {
2048             num_frag = 0;
2049         } else {
2050             num_frag = AVDT_MAX_FRAG_COUNT - num_frag;
2051         }
2052     }
2053 
2054     /* look up transport channel table entry to get peer mtu */
2055     p_tbl = avdt_ad_tc_tbl_by_type(AVDT_CHAN_MEDIA, p_scb->p_ccb, p_scb);
2056     buf_size = p_tbl->peer_mtu + BT_HDR_SIZE;
2057     AVDT_TRACE_DEBUG("peer_mtu: %d, buf_size: %d num_frag=%d\n",
2058                      p_tbl->peer_mtu, buf_size, num_frag);
2059 
2060     if (buf_size > AVDT_DATA_BUF_SIZE) {
2061         buf_size = AVDT_DATA_BUF_SIZE;
2062     }
2063 
2064     mtu_used = buf_size - BT_HDR_SIZE;
2065 
2066     while (*p_data_len && num_frag) {
2067         /* allocate buffer for fragment */
2068         if (NULL == (p_frag = (BT_HDR *)osi_malloc(buf_size))) {
2069             AVDT_TRACE_WARNING("avdt_scb_queue_frags len=%d(out of GKI buffers)\n", *p_data_len);
2070             break;
2071         }
2072         /* fill fragment by chunk of media payload */
2073         p_frag->layer_specific = *p_data_len;/* length of all remaining transport packet */
2074         p_frag->offset = offset;
2075         /* adjust packet offset for continuing packets */
2076         offset = cont_offset;
2077 
2078         p_frag->len = mtu_used - p_frag->offset;
2079         if (p_frag->len > *p_data_len) {
2080             p_frag->len = *p_data_len;
2081         }
2082         memcpy((UINT8 *)(p_frag + 1) + p_frag->offset, *pp_data, p_frag->len);
2083         *pp_data += p_frag->len;
2084         *p_data_len -= p_frag->len;
2085         AVDT_TRACE_DEBUG("Prepared fragment len=%d\n", p_frag->len);
2086 
2087         if (al_hdr) {
2088             /* Adaptation Layer header */
2089             p_frag->len += AVDT_AL_HDR_SIZE;
2090             p_frag->offset -= AVDT_AL_HDR_SIZE;
2091             p = (UINT8 *)(p_frag + 1) + p_frag->offset;
2092             /* TSID, fragment bit and coding of length(in 2 length octets following) */
2093             *p++ = (p_scb->curr_cfg.mux_tsid_media << 3) | (AVDT_ALH_FRAG_MASK | AVDT_ALH_LCODE_16BIT);
2094 
2095             /* length of all remaining transport packet */
2096             UINT16_TO_BE_STREAM(p, p_frag->layer_specific );
2097         }
2098         /* put fragment into gueue */
2099         fixed_queue_enqueue(p_scb->frag_q, p_frag, FIXED_QUEUE_MAX_TIMEOUT);
2100         num_frag--;
2101     }
2102 }
2103 #endif
2104 
2105 #endif /* #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE) */
2106