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