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 API of the audio/video distribution transport
22 *  protocol.
23 *
24 ******************************************************************************/
25
26#include <string.h>
27#include "data_types.h"
28#include "bt_target.h"
29#include "avdt_api.h"
30#include "avdtc_api.h"
31#include "avdt_int.h"
32#include "l2c_api.h"
33#include "btm_api.h"
34#include "btu.h"
35
36
37#if (defined BTTRC_INCLUDED && BTTRC_INCLUDED == TRUE)
38#include "bttrc_str_ids.h"
39#endif
40
41/* Control block for AVDT */
42#if AVDT_DYNAMIC_MEMORY == FALSE
43tAVDT_CB avdt_cb;
44#endif
45
46
47/*******************************************************************************
48**
49** Function         avdt_process_timeout
50**
51** Description      This function is called by BTU when an AVDTP timer
52**                  expires.  The function sends a timer event to the
53**                  appropriate CCB or SCB state machine.
54**
55**                  This function is for use internal to the stack only.
56**
57**
58** Returns          void
59**
60*******************************************************************************/
61void avdt_process_timeout(TIMER_LIST_ENT *p_tle)
62{
63    UINT8   event = 0;
64    UINT8   err_code = AVDT_ERR_TIMEOUT;
65
66    switch (p_tle->event)
67    {
68        case BTU_TTYPE_AVDT_CCB_RET:
69            event = AVDT_CCB_RET_TOUT_EVT + AVDT_CCB_MKR;
70            break;
71
72        case BTU_TTYPE_AVDT_CCB_RSP:
73            event = AVDT_CCB_RSP_TOUT_EVT + AVDT_CCB_MKR;
74            break;
75
76        case BTU_TTYPE_AVDT_CCB_IDLE:
77            event = AVDT_CCB_IDLE_TOUT_EVT + AVDT_CCB_MKR;
78            break;
79
80        case BTU_TTYPE_AVDT_SCB_TC:
81            event = AVDT_SCB_TC_TOUT_EVT;
82            break;
83
84        default:
85            break;
86    }
87
88    if (event & AVDT_CCB_MKR)
89    {
90        avdt_ccb_event((tAVDT_CCB *) p_tle->param, (UINT8) (event & ~AVDT_CCB_MKR),
91                       (tAVDT_CCB_EVT *) &err_code);
92    }
93    else
94    {
95        avdt_scb_event((tAVDT_SCB *) p_tle->param, event, NULL);
96    }
97}
98
99/*******************************************************************************
100**
101** Function         AVDT_Register
102**
103** Description      This is the system level registration function for the
104**                  AVDTP protocol.  This function initializes AVDTP and
105**                  prepares the protocol stack for its use.  This function
106**                  must be called once by the system or platform using AVDTP
107**                  before the other functions of the API an be used.
108**
109**
110** Returns          void
111**
112*******************************************************************************/
113void AVDT_Register(tAVDT_REG *p_reg, tAVDT_CTRL_CBACK *p_cback)
114{
115
116    BTTRC_AVDT_API0(AVDT_TRACE_API_REGISTER);
117
118    /* register PSM with L2CAP */
119    L2CA_Register(AVDT_PSM, (tL2CAP_APPL_INFO *) &avdt_l2c_appl);
120
121    /* set security level */
122    BTM_SetSecurityLevel(TRUE, "", BTM_SEC_SERVICE_AVDTP, p_reg->sec_mask,
123        AVDT_PSM, BTM_SEC_PROTO_AVDT, AVDT_CHAN_SIG);
124    BTM_SetSecurityLevel(FALSE, "", BTM_SEC_SERVICE_AVDTP, p_reg->sec_mask,
125        AVDT_PSM, BTM_SEC_PROTO_AVDT, AVDT_CHAN_SIG);
126
127    /* do not use security on the media channel */
128    BTM_SetSecurityLevel(TRUE, "", BTM_SEC_SERVICE_AVDTP_NOSEC, BTM_SEC_NONE,
129        AVDT_PSM, BTM_SEC_PROTO_AVDT, AVDT_CHAN_MEDIA);
130    BTM_SetSecurityLevel(FALSE, "", BTM_SEC_SERVICE_AVDTP_NOSEC, BTM_SEC_NONE,
131        AVDT_PSM, BTM_SEC_PROTO_AVDT, AVDT_CHAN_MEDIA);
132
133#if AVDT_REPORTING == TRUE
134    /* do not use security on the reporting channel */
135    BTM_SetSecurityLevel(TRUE, "", BTM_SEC_SERVICE_AVDTP_NOSEC, BTM_SEC_NONE,
136        AVDT_PSM, BTM_SEC_PROTO_AVDT, AVDT_CHAN_REPORT);
137    BTM_SetSecurityLevel(FALSE, "", BTM_SEC_SERVICE_AVDTP_NOSEC, BTM_SEC_NONE,
138        AVDT_PSM, BTM_SEC_PROTO_AVDT, AVDT_CHAN_REPORT);
139#endif
140
141    /* initialize AVDTP data structures */
142    avdt_scb_init();
143    avdt_ccb_init();
144    avdt_ad_init();
145#if defined(AVDT_INITIAL_TRACE_LEVEL)
146    avdt_cb.trace_level = AVDT_INITIAL_TRACE_LEVEL;
147#else
148    avdt_cb.trace_level = BT_TRACE_LEVEL_NONE;
149#endif
150
151    /* copy registration struct */
152    memcpy(&avdt_cb.rcb, p_reg, sizeof(tAVDT_REG));
153    avdt_cb.p_conn_cback = p_cback;
154}
155
156/*******************************************************************************
157**
158** Function         AVDT_Deregister
159**
160** Description      This function is called to deregister use AVDTP protocol.
161**                  It is called when AVDTP is no longer being used by any
162**                  application in the system.  Before this function can be
163**                  called, all streams must be removed with AVDT_RemoveStream().
164**
165**
166** Returns          void
167**
168*******************************************************************************/
169void AVDT_Deregister(void)
170{
171    BTTRC_AVDT_API0(AVDT_TRACE_API_DEREGISTER);
172
173    /* deregister PSM with L2CAP */
174    L2CA_Deregister(AVDT_PSM);
175}
176
177/*******************************************************************************
178**
179** Function         AVDT_CreateStream
180**
181** Description      Create a stream endpoint.  After a stream endpoint is
182**                  created an application can initiate a connection between
183**                  this endpoint and an endpoint on a peer device.  In
184**                  addition, a peer device can discover, get the capabilities,
185**                  and connect to this endpoint.
186**
187**
188** Returns          AVDT_SUCCESS if successful, otherwise error.
189**
190*******************************************************************************/
191UINT16 AVDT_CreateStream(UINT8 *p_handle, tAVDT_CS *p_cs)
192{
193    UINT16      result = AVDT_SUCCESS;
194    tAVDT_SCB   *p_scb;
195
196    BTTRC_AVDT_API0(AVDT_TRACE_API_CREATESTREAM);
197
198    /* Verify parameters; if invalid, return failure */
199    if (((p_cs->cfg.psc_mask & (~AVDT_PSC)) != 0) || (p_cs->p_ctrl_cback == NULL))
200    {
201        result = AVDT_BAD_PARAMS;
202    }
203    /* Allocate scb; if no scbs, return failure */
204    else if ((p_scb = avdt_scb_alloc(p_cs)) == NULL)
205    {
206        result = AVDT_NO_RESOURCES;
207    }
208    else
209    {
210        *p_handle = avdt_scb_to_hdl(p_scb);
211    }
212    return result;
213}
214
215/*******************************************************************************
216**
217** Function         AVDT_RemoveStream
218**
219** Description      Remove a stream endpoint.  This function is called when
220**                  the application is no longer using a stream endpoint.
221**                  If this function is called when the endpoint is connected
222**                  the connection is closed and then the stream endpoint
223**                  is removed.
224**
225**
226** Returns          AVDT_SUCCESS if successful, otherwise error.
227**
228*******************************************************************************/
229UINT16 AVDT_RemoveStream(UINT8 handle)
230{
231    UINT16      result = AVDT_SUCCESS;
232    tAVDT_SCB   *p_scb;
233
234
235    BTTRC_AVDT_API0(AVDT_TRACE_API_REMOVESTREAM);
236
237    /* look up scb */
238    if ((p_scb = avdt_scb_by_hdl(handle)) == NULL)
239    {
240        result = AVDT_BAD_HANDLE;
241    }
242    else
243    {
244        /* send remove event to scb */
245        avdt_scb_event(p_scb, AVDT_SCB_API_REMOVE_EVT, NULL);
246    }
247    return result;
248}
249
250/*******************************************************************************
251**
252** Function         AVDT_DiscoverReq
253**
254** Description      This function initiates a connection to the AVDTP service
255**                  on the peer device, if not already present, and discovers
256**                  the stream endpoints on the peer device.  (Please note
257**                  that AVDTP discovery is unrelated to SDP discovery).
258**                  This function can be called at any time regardless of whether
259**                  there is an AVDTP connection to the peer device.
260**
261**                  When discovery is complete, an AVDT_DISCOVER_CFM_EVT
262**                  is sent to the application via its callback function.
263**                  The application must not call AVDT_GetCapReq() or
264**                  AVDT_DiscoverReq() again to the same device until
265**                  discovery is complete.
266**
267**                  The memory addressed by sep_info is allocated by the
268**                  application.  This memory is written to by AVDTP as part
269**                  of the discovery procedure.  This memory must remain
270**                  accessible until the application receives the
271**                  AVDT_DISCOVER_CFM_EVT.
272**
273** Returns          AVDT_SUCCESS if successful, otherwise error.
274**
275*******************************************************************************/
276UINT16 AVDT_DiscoverReq(BD_ADDR bd_addr, tAVDT_SEP_INFO *p_sep_info,
277                        UINT8 max_seps, tAVDT_CTRL_CBACK *p_cback)
278{
279    tAVDT_CCB       *p_ccb;
280    UINT16          result = AVDT_SUCCESS;
281    tAVDT_CCB_EVT   evt;
282
283    BTTRC_AVDT_API0(AVDT_TRACE_API_DISCOVER_REQ);
284
285    /* find channel control block for this bd addr; if none, allocate one */
286    if ((p_ccb = avdt_ccb_by_bd(bd_addr)) == NULL)
287    {
288        if ((p_ccb = avdt_ccb_alloc(bd_addr)) == NULL)
289        {
290            /* could not allocate channel control block */
291            result = AVDT_NO_RESOURCES;
292        }
293    }
294
295    if (result == AVDT_SUCCESS)
296    {
297        /* make sure no discovery or get capabilities req already in progress */
298        if (p_ccb->proc_busy)
299        {
300            result = AVDT_BUSY;
301        }
302        /* send event to ccb */
303        else
304        {
305            evt.discover.p_sep_info = p_sep_info;
306            evt.discover.num_seps = max_seps;
307            evt.discover.p_cback = p_cback;
308            avdt_ccb_event(p_ccb, AVDT_CCB_API_DISCOVER_REQ_EVT, &evt);
309        }
310    }
311    return result;
312}
313
314/*******************************************************************************
315**
316** Function         avdt_get_cap_req
317**
318** Description      internal function to serve both AVDT_GetCapReq and
319**                  AVDT_GetAllCapReq
320**
321** Returns          AVDT_SUCCESS if successful, otherwise error.
322**
323*******************************************************************************/
324static UINT16 avdt_get_cap_req(BD_ADDR bd_addr, tAVDT_CCB_API_GETCAP *p_evt)
325{
326    tAVDT_CCB       *p_ccb = NULL;
327    UINT16          result = AVDT_SUCCESS;
328
329    /* verify SEID */
330    if ((p_evt->single.seid < AVDT_SEID_MIN) || (p_evt->single.seid > AVDT_SEID_MAX))
331    {
332        AVDT_TRACE_ERROR1("seid: %d", p_evt->single.seid);
333        result = AVDT_BAD_PARAMS;
334    }
335    /* find channel control block for this bd addr; if none, allocate one */
336    else if ((p_ccb = avdt_ccb_by_bd(bd_addr)) == NULL)
337    {
338        if ((p_ccb = avdt_ccb_alloc(bd_addr)) == NULL)
339        {
340            /* could not allocate channel control block */
341            result = AVDT_NO_RESOURCES;
342        }
343    }
344
345    if (result == AVDT_SUCCESS)
346    {
347        /* make sure no discovery or get capabilities req already in progress */
348        if (p_ccb->proc_busy)
349        {
350            result = AVDT_BUSY;
351        }
352        /* send event to ccb */
353        else
354        {
355            avdt_ccb_event(p_ccb, AVDT_CCB_API_GETCAP_REQ_EVT, (tAVDT_CCB_EVT *)p_evt);
356        }
357    }
358    return result;
359}
360
361/*******************************************************************************
362**
363** Function         AVDT_GetCapReq
364**
365** Description      This function initiates a connection to the AVDTP service
366**                  on the peer device, if not already present, and gets the
367**                  capabilities of a stream endpoint on the peer device.
368**                  This function can be called at any time regardless of
369**                  whether there is an AVDTP connection to the peer device.
370**
371**                  When the procedure is complete, an AVDT_GETCAP_CFM_EVT is
372**                  sent to the application via its callback function.  The
373**                  application must not call AVDT_GetCapReq() or
374**                  AVDT_DiscoverReq() again until the procedure is complete.
375**
376**                  The memory pointed to by p_cfg is allocated by the
377**                  application.  This memory is written to by AVDTP as part
378**                  of the get capabilities procedure.  This memory must
379**                  remain accessible until the application receives
380**                  the AVDT_GETCAP_CFM_EVT.
381**
382** Returns          AVDT_SUCCESS if successful, otherwise error.
383**
384*******************************************************************************/
385UINT16 AVDT_GetCapReq(BD_ADDR bd_addr, UINT8 seid, tAVDT_CFG *p_cfg, tAVDT_CTRL_CBACK *p_cback)
386{
387    tAVDT_CCB_API_GETCAP    getcap;
388
389    BTTRC_AVDT_API1(AVDT_TRACE_API_GETCAP_REQ, BTTRC_PARAM_UINT8, seid);
390
391    getcap.single.seid = seid;
392    getcap.single.sig_id = AVDT_SIG_GETCAP;
393    getcap.p_cfg = p_cfg;
394    getcap.p_cback = p_cback;
395    return avdt_get_cap_req (bd_addr, &getcap);
396}
397
398/*******************************************************************************
399**
400** Function         AVDT_GetAllCapReq
401**
402** Description      This function initiates a connection to the AVDTP service
403**                  on the peer device, if not already present, and gets the
404**                  capabilities of a stream endpoint on the peer device.
405**                  This function can be called at any time regardless of
406**                  whether there is an AVDTP connection to the peer device.
407**
408**                  When the procedure is complete, an AVDT_GETCAP_CFM_EVT is
409**                  sent to the application via its callback function.  The
410**                  application must not call AVDT_GetCapReq() or
411**                  AVDT_DiscoverReq() again until the procedure is complete.
412**
413**                  The memory pointed to by p_cfg is allocated by the
414**                  application.  This memory is written to by AVDTP as part
415**                  of the get capabilities procedure.  This memory must
416**                  remain accessible until the application receives
417**                  the AVDT_GETCAP_CFM_EVT.
418**
419** Returns          AVDT_SUCCESS if successful, otherwise error.
420**
421*******************************************************************************/
422UINT16 AVDT_GetAllCapReq(BD_ADDR bd_addr, UINT8 seid, tAVDT_CFG *p_cfg, tAVDT_CTRL_CBACK *p_cback)
423{
424    tAVDT_CCB_API_GETCAP    getcap;
425
426    BTTRC_AVDT_API1(AVDT_TRACE_API_GET_ALLCAP_REQ, BTTRC_PARAM_UINT8, seid);
427
428    getcap.single.seid = seid;
429    getcap.single.sig_id = AVDT_SIG_GET_ALLCAP;
430    getcap.p_cfg = p_cfg;
431    getcap.p_cback = p_cback;
432    return avdt_get_cap_req (bd_addr, &getcap);
433}
434
435/*******************************************************************************
436**
437** Function         AVDT_DelayReport
438**
439** Description      This functions sends a Delay Report to the peer device
440**                  that is associated with a particular SEID.
441**                  This function is called by SNK device.
442**
443** Returns          AVDT_SUCCESS if successful, otherwise error.
444**
445*******************************************************************************/
446UINT16 AVDT_DelayReport(UINT8 handle, UINT8 seid, UINT16 delay)
447{
448    tAVDT_SCB       *p_scb;
449    UINT16          result = AVDT_SUCCESS;
450    tAVDT_SCB_EVT   evt;
451
452    BTTRC_AVDT_API0(AVDT_TRACE_API_DELAY_REPORT);
453
454    /* map handle to scb */
455    if ((p_scb = avdt_scb_by_hdl(handle)) == NULL)
456    {
457        result = AVDT_BAD_HANDLE;
458    }
459    else
460    /* send event to scb */
461    {
462        evt.apidelay.hdr.seid   = seid;
463        evt.apidelay.delay      = delay;
464        avdt_scb_event(p_scb, AVDT_SCB_API_DELAY_RPT_REQ_EVT, &evt);
465    }
466
467    return result;
468}
469
470/*******************************************************************************
471**
472** Function         AVDT_OpenReq
473**
474** Description      This function initiates a connection to the AVDTP service
475**                  on the peer device, if not already present, and connects
476**                  to a stream endpoint on a peer device.  When the connection
477**                  is completed, an AVDT_OPEN_CFM_EVT is sent to the
478**                  application via the control callback function for this handle.
479**
480** Returns          AVDT_SUCCESS if successful, otherwise error.
481**
482*******************************************************************************/
483UINT16 AVDT_OpenReq(UINT8 handle, BD_ADDR bd_addr, UINT8 seid, tAVDT_CFG *p_cfg)
484{
485    tAVDT_CCB       *p_ccb = NULL;
486    tAVDT_SCB       *p_scb = NULL;
487    UINT16          result = AVDT_SUCCESS;
488    tAVDT_SCB_EVT   evt;
489
490    BTTRC_AVDT_API0(AVDT_TRACE_API_OPEN_REQ);
491
492
493    /* verify SEID */
494    if ((seid < AVDT_SEID_MIN) || (seid > AVDT_SEID_MAX))
495    {
496        result = AVDT_BAD_PARAMS;
497    }
498    /* map handle to scb */
499    else if ((p_scb = avdt_scb_by_hdl(handle)) == NULL)
500    {
501        result = AVDT_BAD_HANDLE;
502    }
503    /* find channel control block for this bd addr; if none, allocate one */
504    else if ((p_ccb = avdt_ccb_by_bd(bd_addr)) == NULL)
505    {
506        if ((p_ccb = avdt_ccb_alloc(bd_addr)) == NULL)
507        {
508            /* could not allocate channel control block */
509            result = AVDT_NO_RESOURCES;
510        }
511    }
512
513    /* send event to scb */
514    if (result == AVDT_SUCCESS)
515    {
516        evt.msg.config_cmd.hdr.seid = seid;
517        evt.msg.config_cmd.hdr.ccb_idx = avdt_ccb_to_idx(p_ccb);
518        evt.msg.config_cmd.int_seid = handle;
519        evt.msg.config_cmd.p_cfg = p_cfg;
520        avdt_scb_event(p_scb, AVDT_SCB_API_SETCONFIG_REQ_EVT, &evt);
521    }
522    return result;
523}
524
525/*******************************************************************************
526**
527** Function         AVDT_ConfigRsp
528**
529** Description      Respond to a configure request from the peer device.  This
530**                  function must be called if the application receives an
531**                  AVDT_CONFIG_IND_EVT through its control callback.
532**
533**
534** Returns          AVDT_SUCCESS if successful, otherwise error.
535**
536*******************************************************************************/
537UINT16 AVDT_ConfigRsp(UINT8 handle, UINT8 label, UINT8 error_code, UINT8 category)
538{
539    tAVDT_SCB       *p_scb;
540    tAVDT_SCB_EVT   evt;
541    UINT16          result = AVDT_SUCCESS;
542    UINT8           event_code;
543
544    BTTRC_AVDT_API0(AVDT_TRACE_API_CONFIG_RSP);
545
546    /* map handle to scb */
547    if ((p_scb = avdt_scb_by_hdl(handle)) == NULL)
548    {
549        result = AVDT_BAD_HANDLE;
550    }
551    /* handle special case when this function is called but peer has not send
552    ** a configuration cmd; ignore and return error result
553    */
554    else if (!p_scb->in_use)
555    {
556        result = AVDT_BAD_HANDLE;
557    }
558    /* send event to scb */
559    else
560    {
561        evt.msg.hdr.err_code = error_code;
562        evt.msg.hdr.err_param = category;
563        evt.msg.hdr.label = label;
564        if (error_code == 0)
565        {
566            event_code = AVDT_SCB_API_SETCONFIG_RSP_EVT;
567        }
568        else
569        {
570            event_code = AVDT_SCB_API_SETCONFIG_REJ_EVT;
571        }
572        avdt_scb_event(p_scb, event_code, &evt);
573    }
574
575    return result;
576}
577
578/*******************************************************************************
579**
580** Function         AVDT_StartReq
581**
582** Description      Start one or more stream endpoints.  This initiates the
583**                  transfer of media packets for the streams.  All stream
584**                  endpoints must previously be opened.  When the streams
585**                  are started, an AVDT_START_CFM_EVT is sent to the
586**                  application via the control callback function for each stream.
587**
588**
589** Returns          AVDT_SUCCESS if successful, otherwise error.
590**
591*******************************************************************************/
592UINT16 AVDT_StartReq(UINT8 *p_handles, UINT8 num_handles)
593{
594    tAVDT_SCB       *p_scb = NULL;
595    tAVDT_CCB_EVT   evt;
596    UINT16          result = AVDT_SUCCESS;
597    int             i;
598
599    BTTRC_AVDT_API0(AVDT_TRACE_API_START_REQ);
600
601    if ((num_handles == 0) || (num_handles > AVDT_NUM_SEPS))
602    {
603        result = AVDT_BAD_PARAMS;
604    }
605    else
606    {
607        /* verify handles */
608        for (i = 0; i < num_handles; i++)
609        {
610            if ((p_scb = avdt_scb_by_hdl(p_handles[i])) == NULL)
611            {
612                result = AVDT_BAD_HANDLE;
613                break;
614            }
615        }
616    }
617
618    if (result == AVDT_SUCCESS)
619    {
620        if (p_scb->p_ccb == NULL)
621        {
622            result = AVDT_BAD_HANDLE;
623        }
624        else
625        {
626            /* send event to ccb */
627            memcpy(evt.msg.multi.seid_list, p_handles, num_handles);
628            evt.msg.multi.num_seps = num_handles;
629            avdt_ccb_event(p_scb->p_ccb, AVDT_CCB_API_START_REQ_EVT, &evt);
630        }
631    }
632    return result;
633}
634
635/*******************************************************************************
636**
637** Function         AVDT_SuspendReq
638**
639** Description      Suspend one or more stream endpoints. This suspends the
640**                  transfer of media packets for the streams.  All stream
641**                  endpoints must previously be open and started.  When the
642**                  streams are suspended, an AVDT_SUSPEND_CFM_EVT is sent to
643**                  the application via the control callback function for
644**                  each stream.
645**
646**
647** Returns          AVDT_SUCCESS if successful, otherwise error.
648**
649*******************************************************************************/
650UINT16 AVDT_SuspendReq(UINT8 *p_handles, UINT8 num_handles)
651{
652    tAVDT_SCB       *p_scb = NULL;
653    tAVDT_CCB_EVT   evt;
654    UINT16          result = AVDT_SUCCESS;
655    int             i;
656
657    BTTRC_AVDT_API0(AVDT_TRACE_API_SUSPEND_REQ);
658
659    if ((num_handles == 0) || (num_handles > AVDT_NUM_SEPS))
660    {
661        result = AVDT_BAD_PARAMS;
662    }
663    else
664    {
665        /* verify handles */
666        for (i = 0; i < num_handles; i++)
667        {
668            if ((p_scb = avdt_scb_by_hdl(p_handles[i])) == NULL)
669            {
670                result = AVDT_BAD_HANDLE;
671                break;
672            }
673        }
674    }
675
676    if (result == AVDT_SUCCESS)
677    {
678        if (p_scb->p_ccb == NULL)
679        {
680            result = AVDT_BAD_HANDLE;
681        }
682        else
683        {
684            /* send event to ccb */
685            memcpy(evt.msg.multi.seid_list, p_handles, num_handles);
686            evt.msg.multi.num_seps = num_handles;
687            avdt_ccb_event(p_scb->p_ccb, AVDT_CCB_API_SUSPEND_REQ_EVT, &evt);
688        }
689    }
690
691    return result;
692}
693
694/*******************************************************************************
695**
696** Function         AVDT_CloseReq
697**
698** Description      Close a stream endpoint.  This stops the transfer of media
699**                  packets and closes the transport channel associated with
700**                  this stream endpoint.  When the stream is closed, an
701**                  AVDT_CLOSE_CFM_EVT is sent to the application via the
702**                  control callback function for this handle.
703**
704**
705** Returns          AVDT_SUCCESS if successful, otherwise error.
706**
707*******************************************************************************/
708UINT16 AVDT_CloseReq(UINT8 handle)
709{
710    tAVDT_SCB       *p_scb;
711    UINT16          result = AVDT_SUCCESS;
712
713    BTTRC_AVDT_API0(AVDT_TRACE_API_CLOSE_REQ);
714
715    /* map handle to scb */
716    if ((p_scb = avdt_scb_by_hdl(handle)) == NULL)
717    {
718        result = AVDT_BAD_HANDLE;
719    }
720    else
721    /* send event to scb */
722    {
723        avdt_scb_event(p_scb, AVDT_SCB_API_CLOSE_REQ_EVT, NULL);
724    }
725
726    return result;
727}
728
729/*******************************************************************************
730**
731** Function         AVDT_ReconfigReq
732**
733** Description      Reconfigure a stream endpoint.  This allows the application
734**                  to change the codec or content protection capabilities of
735**                  a stream endpoint after it has been opened.  This function
736**                  can only be called if the stream is opened but not started
737**                  or if the stream has been suspended.  When the procedure
738**                  is completed, an AVDT_RECONFIG_CFM_EVT is sent to the
739**                  application via the control callback function for this handle.
740**
741**
742** Returns          AVDT_SUCCESS if successful, otherwise error.
743**
744*******************************************************************************/
745UINT16 AVDT_ReconfigReq(UINT8 handle, tAVDT_CFG *p_cfg)
746{
747    tAVDT_SCB       *p_scb;
748    UINT16          result = AVDT_SUCCESS;
749    tAVDT_SCB_EVT   evt;
750
751    BTTRC_AVDT_API0(AVDT_TRACE_API_RECONFIG_REQ);
752
753    /* map handle to scb */
754    if ((p_scb = avdt_scb_by_hdl(handle)) == NULL)
755    {
756        result = AVDT_BAD_HANDLE;
757    }
758    /* send event to scb */
759    else
760    {
761        /* force psc_mask to zero */
762        p_cfg->psc_mask = 0;
763
764        evt.msg.reconfig_cmd.p_cfg = p_cfg;
765        avdt_scb_event(p_scb, AVDT_SCB_API_RECONFIG_REQ_EVT, &evt);
766    }
767    return result;
768}
769
770/*******************************************************************************
771**
772** Function         AVDT_ReconfigRsp
773**
774** Description      Respond to a reconfigure request from the peer device.
775**                  This function must be called if the application receives
776**                  an AVDT_RECONFIG_IND_EVT through its control callback.
777**
778**
779** Returns          AVDT_SUCCESS if successful, otherwise error.
780**
781*******************************************************************************/
782UINT16 AVDT_ReconfigRsp(UINT8 handle, UINT8 label, UINT8 error_code, UINT8 category)
783{
784    tAVDT_SCB       *p_scb;
785    tAVDT_SCB_EVT   evt;
786    UINT16          result = AVDT_SUCCESS;
787
788    BTTRC_AVDT_API0(AVDT_TRACE_API_RECONFIG_RSP);
789
790    /* map handle to scb */
791    if ((p_scb = avdt_scb_by_hdl(handle)) == NULL)
792    {
793        result = AVDT_BAD_HANDLE;
794    }
795    /* send event to scb */
796    else
797    {
798        evt.msg.hdr.err_code = error_code;
799        evt.msg.hdr.err_param = category;
800        evt.msg.hdr.label = label;
801        avdt_scb_event(p_scb, AVDT_SCB_API_RECONFIG_RSP_EVT, &evt);
802    }
803
804    return result;
805}
806
807/*******************************************************************************
808**
809** Function         AVDT_SecurityReq
810**
811** Description      Send a security request to the peer device.  When the
812**                  security procedure is completed, an AVDT_SECURITY_CFM_EVT
813**                  is sent to the application via the control callback function
814**                  for this handle.  (Please note that AVDTP security procedures
815**                  are unrelated to Bluetooth link level security.)
816**
817**
818** Returns          AVDT_SUCCESS if successful, otherwise error.
819**
820*******************************************************************************/
821UINT16 AVDT_SecurityReq(UINT8 handle, UINT8 *p_data, UINT16 len)
822{
823    tAVDT_SCB       *p_scb;
824    UINT16          result = AVDT_SUCCESS;
825    tAVDT_SCB_EVT   evt;
826
827    BTTRC_AVDT_API0(AVDT_TRACE_API_SECURITY_REQ);
828
829    /* map handle to scb */
830    if ((p_scb = avdt_scb_by_hdl(handle)) == NULL)
831    {
832        result = AVDT_BAD_HANDLE;
833    }
834    /* send event to scb */
835    else
836    {
837        evt.msg.security_rsp.p_data = p_data;
838        evt.msg.security_rsp.len = len;
839        avdt_scb_event(p_scb, AVDT_SCB_API_SECURITY_REQ_EVT, &evt);
840    }
841    return result;
842}
843
844/*******************************************************************************
845**
846** Function         AVDT_SecurityRsp
847**
848** Description      Respond to a security request from the peer device.
849**                  This function must be called if the application receives
850**                  an AVDT_SECURITY_IND_EVT through its control callback.
851**                  (Please note that AVDTP security procedures are unrelated
852**                  to Bluetooth link level security.)
853**
854**
855** Returns          AVDT_SUCCESS if successful, otherwise error.
856**
857*******************************************************************************/
858UINT16 AVDT_SecurityRsp(UINT8 handle, UINT8 label, UINT8 error_code,
859                        UINT8 *p_data, UINT16 len)
860{
861    tAVDT_SCB       *p_scb;
862    UINT16          result = AVDT_SUCCESS;
863    tAVDT_SCB_EVT   evt;
864
865    BTTRC_AVDT_API0(AVDT_TRACE_API_SECURITY_RSP);
866
867    /* map handle to scb */
868    if ((p_scb = avdt_scb_by_hdl(handle)) == NULL)
869    {
870        result = AVDT_BAD_HANDLE;
871    }
872    /* send event to scb */
873    else
874    {
875        evt.msg.security_rsp.hdr.err_code = error_code;
876        evt.msg.security_rsp.hdr.label = label;
877        evt.msg.security_rsp.p_data = p_data;
878        evt.msg.security_rsp.len = len;
879        avdt_scb_event(p_scb, AVDT_SCB_API_SECURITY_RSP_EVT, &evt);
880    }
881    return result;
882}
883
884/*******************************************************************************
885**
886** Function         AVDT_WriteReqOpt
887**
888** Description      Send a media packet to the peer device.  The stream must
889**                  be started before this function is called.  Also, this
890**                  function can only be called if the stream is a SRC.
891**
892**                  When AVDTP has sent the media packet and is ready for the
893**                  next packet, an AVDT_WRITE_CFM_EVT is sent to the
894**                  application via the control callback.  The application must
895**                  wait for the AVDT_WRITE_CFM_EVT before it makes the next
896**                  call to AVDT_WriteReq().  If the applications calls
897**                  AVDT_WriteReq() before it receives the event the packet
898**                  will not be sent.  The application may make its first call
899**                  to AVDT_WriteReq() after it receives an AVDT_START_CFM_EVT
900**                  or AVDT_START_IND_EVT.
901**
902**                  The application passes the packet using the BT_HDR structure.
903**                  This structure is described in section 2.1.  The offset
904**                  field must be equal to or greater than AVDT_MEDIA_OFFSET
905**                  (if NO_RTP is specified, L2CAP_MIN_OFFSET can be used).
906**                  This allows enough space in the buffer for the L2CAP and
907**                  AVDTP headers.
908**
909**                  The memory pointed to by p_pkt must be a GKI buffer
910**                  allocated by the application.  This buffer will be freed
911**                  by the protocol stack; the application must not free
912**                  this buffer.
913**
914**                  The opt parameter allows passing specific options like:
915**                  - NO_RTP : do not add the RTP header to buffer
916**
917** Returns          AVDT_SUCCESS if successful, otherwise error.
918**
919*******************************************************************************/
920UINT16 AVDT_WriteReqOpt(UINT8 handle, BT_HDR *p_pkt, UINT32 time_stamp, UINT8 m_pt, tAVDT_DATA_OPT_MASK opt)
921{
922    tAVDT_SCB       *p_scb;
923    tAVDT_SCB_EVT   evt;
924    UINT16          result = AVDT_SUCCESS;
925
926    BTTRC_AVDT_API0(AVDT_TRACE_API_WRITE_REQ);
927
928    /* map handle to scb */
929    if ((p_scb = avdt_scb_by_hdl(handle)) == NULL)
930    {
931        result = AVDT_BAD_HANDLE;
932    }
933    else
934    {
935        evt.apiwrite.p_buf = p_pkt;
936        evt.apiwrite.time_stamp = time_stamp;
937        evt.apiwrite.m_pt = m_pt;
938        evt.apiwrite.opt = opt;
939#if AVDT_MULTIPLEXING == TRUE
940        GKI_init_q (&evt.apiwrite.frag_q);
941#endif
942        avdt_scb_event(p_scb, AVDT_SCB_API_WRITE_REQ_EVT, &evt);
943    }
944
945    return result;
946}
947
948/*******************************************************************************
949**
950** Function         AVDT_WriteReq
951**
952** Description      Send a media packet to the peer device.  The stream must
953**                  be started before this function is called.  Also, this
954**                  function can only be called if the stream is a SRC.
955**
956**                  When AVDTP has sent the media packet and is ready for the
957**                  next packet, an AVDT_WRITE_CFM_EVT is sent to the
958**                  application via the control callback.  The application must
959**                  wait for the AVDT_WRITE_CFM_EVT before it makes the next
960**                  call to AVDT_WriteReq().  If the applications calls
961**                  AVDT_WriteReq() before it receives the event the packet
962**                  will not be sent.  The application may make its first call
963**                  to AVDT_WriteReq() after it receives an AVDT_START_CFM_EVT
964**                  or AVDT_START_IND_EVT.
965**
966**                  The application passes the packet using the BT_HDR structure.
967**                  This structure is described in section 2.1.  The offset
968**                  field must be equal to or greater than AVDT_MEDIA_OFFSET.
969**                  This allows enough space in the buffer for the L2CAP and
970**                  AVDTP headers.
971**
972**                  The memory pointed to by p_pkt must be a GKI buffer
973**                  allocated by the application.  This buffer will be freed
974**                  by the protocol stack; the application must not free
975**                  this buffer.
976**
977**
978** Returns          AVDT_SUCCESS if successful, otherwise error.
979**
980*******************************************************************************/
981UINT16 AVDT_WriteReq(UINT8 handle, BT_HDR *p_pkt, UINT32 time_stamp, UINT8 m_pt)
982{
983    return AVDT_WriteReqOpt(handle, p_pkt, time_stamp, m_pt, AVDT_DATA_OPT_NONE);
984}
985
986/*******************************************************************************
987**
988** Function         AVDT_ConnectReq
989**
990** Description      This function initiates an AVDTP signaling connection
991**                  to the peer device.  When the connection is completed, an
992**                  AVDT_CONNECT_IND_EVT is sent to the application via its
993**                  control callback function.  If the connection attempt fails
994**                  an AVDT_DISCONNECT_IND_EVT is sent.  The security mask
995**                  parameter overrides the outgoing security mask set in
996**                  AVDT_Register().
997**
998** Returns          AVDT_SUCCESS if successful, otherwise error.
999**
1000*******************************************************************************/
1001UINT16 AVDT_ConnectReq(BD_ADDR bd_addr, UINT8 sec_mask, tAVDT_CTRL_CBACK *p_cback)
1002{
1003    tAVDT_CCB       *p_ccb = NULL;
1004    UINT16          result = AVDT_SUCCESS;
1005    tAVDT_CCB_EVT   evt;
1006
1007    BTTRC_AVDT_API0(AVDT_TRACE_API_CONNECT_REQ);
1008
1009    /* find channel control block for this bd addr; if none, allocate one */
1010    if ((p_ccb = avdt_ccb_by_bd(bd_addr)) == NULL)
1011    {
1012        if ((p_ccb = avdt_ccb_alloc(bd_addr)) == NULL)
1013        {
1014            /* could not allocate channel control block */
1015            result = AVDT_NO_RESOURCES;
1016        }
1017    }
1018    else if (p_ccb->ll_opened == FALSE)
1019    {
1020        AVDT_TRACE_WARNING0("AVDT_ConnectReq: CCB LL is in the middle of opening");
1021
1022        /* ccb was already allocated for the incoming signalling. */
1023        result = AVDT_BUSY;
1024    }
1025
1026    if (result == AVDT_SUCCESS)
1027    {
1028        /* send event to ccb */
1029        evt.connect.p_cback = p_cback;
1030        evt.connect.sec_mask = sec_mask;
1031        avdt_ccb_event(p_ccb, AVDT_CCB_API_CONNECT_REQ_EVT, &evt);
1032    }
1033    return result;
1034}
1035
1036/*******************************************************************************
1037**
1038** Function         AVDT_DisconnectReq
1039**
1040** Description      This function disconnect an AVDTP signaling connection
1041**                  to the peer device.  When disconnected an
1042**                  AVDT_DISCONNECT_IND_EVT is sent to the application via its
1043**                  control callback function.
1044**
1045** Returns          AVDT_SUCCESS if successful, otherwise error.
1046**
1047*******************************************************************************/
1048UINT16 AVDT_DisconnectReq(BD_ADDR bd_addr, tAVDT_CTRL_CBACK *p_cback)
1049{
1050    tAVDT_CCB       *p_ccb = NULL;
1051    UINT16          result = AVDT_SUCCESS;
1052    tAVDT_CCB_EVT   evt;
1053
1054    BTTRC_AVDT_API0(AVDT_TRACE_API_DISCONNECT_REQ);
1055
1056
1057    /* find channel control block for this bd addr; if none, error */
1058    if ((p_ccb = avdt_ccb_by_bd(bd_addr)) == NULL)
1059    {
1060        result = AVDT_BAD_PARAMS;
1061    }
1062
1063    if (result == AVDT_SUCCESS)
1064    {
1065        /* send event to ccb */
1066        evt.disconnect.p_cback = p_cback;
1067        avdt_ccb_event(p_ccb, AVDT_CCB_API_DISCONNECT_REQ_EVT, &evt);
1068    }
1069    return result;
1070}
1071
1072/*******************************************************************************
1073**
1074** Function         AVDT_GetL2CapChannel
1075**
1076** Description      Get the L2CAP CID used by the handle.
1077**
1078** Returns          CID if successful, otherwise 0.
1079**
1080*******************************************************************************/
1081UINT16 AVDT_GetL2CapChannel(UINT8 handle)
1082{
1083    tAVDT_SCB       *p_scb;
1084    tAVDT_CCB       *p_ccb;
1085    UINT8           tcid;
1086    UINT16          lcid = 0;
1087
1088    /* map handle to scb */
1089    if (((p_scb = avdt_scb_by_hdl(handle)) != NULL)
1090     && ((p_ccb = p_scb->p_ccb) != NULL))
1091    {
1092        /* get tcid from type, scb */
1093        tcid = avdt_ad_type_to_tcid(AVDT_CHAN_MEDIA, p_scb);
1094
1095        lcid = avdt_cb.ad.rt_tbl[avdt_ccb_to_idx(p_ccb)][tcid].lcid;
1096    }
1097
1098    BTTRC_AVDT_API1(AVDT_TRACE_API_GET_L2CAP_CHAN, BTTRC_PARAM_UINT16, lcid);
1099
1100    return (lcid);
1101}
1102
1103/*******************************************************************************
1104**
1105** Function         AVDT_GetSignalChannel
1106**
1107** Description      Get the L2CAP CID used by the signal channel of the given handle.
1108**
1109** Returns          CID if successful, otherwise 0.
1110**
1111*******************************************************************************/
1112UINT16 AVDT_GetSignalChannel(UINT8 handle, BD_ADDR bd_addr)
1113{
1114    tAVDT_SCB       *p_scb;
1115    tAVDT_CCB       *p_ccb;
1116    UINT8           tcid = 0; /* tcid is always 0 for signal channel */
1117    UINT16          lcid = 0;
1118
1119    /* map handle to scb */
1120    if (((p_scb = avdt_scb_by_hdl(handle)) != NULL)
1121     && ((p_ccb = p_scb->p_ccb) != NULL))
1122    {
1123        lcid = avdt_cb.ad.rt_tbl[avdt_ccb_to_idx(p_ccb)][tcid].lcid;
1124    }
1125    else if ((p_ccb = avdt_ccb_by_bd(bd_addr)) != NULL)
1126    {
1127        lcid = avdt_cb.ad.rt_tbl[avdt_ccb_to_idx(p_ccb)][tcid].lcid;
1128    }
1129
1130    BTTRC_AVDT_API1(AVDT_TRACE_API_GET_SIGNAL_CHAN, BTTRC_PARAM_UINT16, lcid);
1131
1132    return (lcid);
1133}
1134
1135#if AVDT_MULTIPLEXING == TRUE
1136/*******************************************************************************
1137**
1138** Function         AVDT_WriteDataReq
1139**
1140** Description      Send a media packet to the peer device.  The stream must
1141**                  be started before this function is called.  Also, this
1142**                  function can only be called if the stream is a SRC.
1143**
1144**                  When AVDTP has sent the media packet and is ready for the
1145**                  next packet, an AVDT_WRITE_CFM_EVT is sent to the
1146**                  application via the control callback.  The application must
1147**                  wait for the AVDT_WRITE_CFM_EVT before it makes the next
1148**                  call to AVDT_WriteDataReq().  If the applications calls
1149**                  AVDT_WriteDataReq() before it receives the event the packet
1150**                  will not be sent.  The application may make its first call
1151**                  to AVDT_WriteDataReq() after it receives an
1152**                  AVDT_START_CFM_EVT or AVDT_START_IND_EVT.
1153**
1154** Returns          AVDT_SUCCESS if successful, otherwise error.
1155**
1156*******************************************************************************/
1157AVDT_API extern UINT16 AVDT_WriteDataReq(UINT8 handle, UINT8 *p_data, UINT32 data_len,
1158                                     UINT32 time_stamp, UINT8 m_pt, UINT8 marker)
1159{
1160
1161    tAVDT_SCB       *p_scb;
1162    tAVDT_SCB_EVT   evt;
1163    UINT16          result = AVDT_SUCCESS;
1164
1165    BTTRC_AVDT_API1(AVDT_TRACE_API_WRITE_DATA_REQ, BTTRC_PARAM_UINT32, data_len);
1166
1167    do
1168    {
1169        /* check length of media frame */
1170        if(data_len > AVDT_MAX_MEDIA_SIZE)
1171        {
1172            result = AVDT_BAD_PARAMS;
1173            break;
1174        }
1175        /* map handle to scb */
1176        if ((p_scb = avdt_scb_by_hdl(handle)) == NULL)
1177        {
1178            result = AVDT_BAD_HANDLE;
1179            break;
1180        }
1181        AVDT_TRACE_WARNING1("mux_tsid_media:%d", p_scb->curr_cfg.mux_tsid_media);
1182
1183        if (p_scb->p_pkt != NULL
1184            || p_scb->p_ccb == NULL
1185            || !GKI_queue_is_empty(&p_scb->frag_q)
1186            || p_scb->frag_off != 0
1187            || p_scb->curr_cfg.mux_tsid_media == 0)
1188        {
1189            result = AVDT_ERR_BAD_STATE;
1190            AVDT_TRACE_WARNING4("p_scb->p_pkt=%x, p_scb->p_ccb=%x, IsQueueEmpty=%x, p_scb->frag_off=%x",
1191                p_scb->p_pkt, p_scb->p_ccb, GKI_queue_is_empty(&p_scb->frag_q), p_scb->frag_off);
1192            break;
1193        }
1194        evt.apiwrite.p_buf = 0; /* it will indicate using of fragments queue frag_q */
1195        /* create queue of media fragments */
1196        GKI_init_q (&evt.apiwrite.frag_q);
1197
1198        /* compose fragments from media payload and put fragments into gueue */
1199        avdt_scb_queue_frags(p_scb, &p_data, &data_len, &evt.apiwrite.frag_q);
1200
1201        if(GKI_queue_is_empty(&evt.apiwrite.frag_q))
1202        {
1203            AVDT_TRACE_WARNING0("AVDT_WriteDataReq out of GKI buffers");
1204            result = AVDT_ERR_RESOURCE;
1205            break;
1206        }
1207        evt.apiwrite.data_len = data_len;
1208        evt.apiwrite.p_data = p_data;
1209
1210        /* process the fragments queue */
1211        evt.apiwrite.time_stamp = time_stamp;
1212        evt.apiwrite.m_pt = m_pt | (marker<<7);
1213        avdt_scb_event(p_scb, AVDT_SCB_API_WRITE_REQ_EVT, &evt);
1214    } while (0);
1215
1216#if (BT_USE_TRACES == TRUE)
1217    if(result != AVDT_SUCCESS)
1218    {
1219        AVDT_TRACE_WARNING1("*** AVDT_WriteDataReq failed result=%d",result);
1220    }
1221#endif
1222    return result;
1223}
1224#endif
1225
1226#if AVDT_MULTIPLEXING == TRUE
1227/*******************************************************************************
1228**
1229** Function         AVDT_SetMediaBuf
1230**
1231** Description      Assigns buffer for media packets or forbids using of assigned
1232**                  buffer if argument p_buf is NULL. This function can only
1233**                  be called if the stream is a SNK.
1234**
1235**                  AVDTP uses this buffer to reassemble fragmented media packets.
1236**                  When AVDTP receives a complete media packet, it calls the
1237**                  p_media_cback assigned by AVDT_CreateStream().
1238**                  This function can be called during callback to assign a
1239**                  different buffer for next media packet or can leave the current
1240**                  buffer for next packet.
1241**
1242** Returns          AVDT_SUCCESS if successful, otherwise error.
1243**
1244*******************************************************************************/
1245AVDT_API extern UINT16 AVDT_SetMediaBuf(UINT8 handle, UINT8 *p_buf, UINT32 buf_len)
1246{
1247    tAVDT_SCB       *p_scb;
1248    UINT16          result = AVDT_SUCCESS;
1249
1250    BTTRC_AVDT_API0(AVDT_TRACE_API_SET_MEDIABUF);
1251
1252    /* map handle to scb */
1253    if ((p_scb = avdt_scb_by_hdl(handle)) == NULL)
1254    {
1255        result = AVDT_BAD_HANDLE;
1256    }
1257    else
1258    {
1259        if(p_buf && p_scb->cs.p_media_cback == NULL)
1260            result = AVDT_NO_RESOURCES;
1261        else
1262        {
1263            p_scb->p_media_buf = p_buf;
1264            p_scb->media_buf_len = buf_len;
1265        }
1266    }
1267
1268    return result;
1269}
1270#endif
1271
1272#if AVDT_REPORTING == TRUE
1273/*******************************************************************************
1274**
1275** Function         AVDT_SendReport
1276**
1277** Description
1278**
1279**
1280**
1281** Returns
1282**
1283*******************************************************************************/
1284UINT16 AVDT_SendReport(UINT8 handle, AVDT_REPORT_TYPE type,
1285                       tAVDT_REPORT_DATA *p_data)
1286{
1287    tAVDT_SCB       *p_scb;
1288    UINT16          result = AVDT_BAD_PARAMS;
1289    BT_HDR          *p_pkt;
1290    tAVDT_TC_TBL    *p_tbl;
1291    UINT8           *p, *plen, *pm1, *p_end;
1292#if AVDT_MULTIPLEXING == TRUE
1293    UINT8           *p_al=NULL, u;
1294#endif
1295    UINT32  ssrc;
1296    UINT16  len;
1297
1298    BTTRC_AVDT_API0(AVDT_TRACE_API_SEND_REPORT);
1299
1300    /* map handle to scb && verify parameters */
1301    if (((p_scb = avdt_scb_by_hdl(handle)) != NULL)
1302     && (p_scb->p_ccb != NULL)
1303     && (((type == AVDT_RTCP_PT_SR) && (p_scb->cs.tsep == AVDT_TSEP_SRC)) ||
1304        ((type == AVDT_RTCP_PT_RR) && (p_scb->cs.tsep == AVDT_TSEP_SNK)) ||
1305        (type == AVDT_RTCP_PT_SDES)) )
1306    {
1307        result = AVDT_NO_RESOURCES;
1308
1309        /* build SR - assume fit in one packet */
1310        p_tbl = avdt_ad_tc_tbl_by_type(AVDT_CHAN_REPORT, p_scb->p_ccb, p_scb);
1311        if((p_tbl->state == AVDT_AD_ST_OPEN) &&
1312            (p_pkt = (BT_HDR *)GKI_getbuf(p_tbl->peer_mtu)) != NULL)
1313        {
1314            p_pkt->offset = L2CAP_MIN_OFFSET;
1315            p = (UINT8 *)(p_pkt + 1) + p_pkt->offset;
1316#if AVDT_MULTIPLEXING == TRUE
1317            if(p_scb->curr_cfg.psc_mask & AVDT_PSC_MUX)
1318            {
1319                /* Adaptation Layer header later */
1320                p_al = p;
1321                p += 2;
1322            }
1323#endif
1324            pm1 = p;
1325            *p++ = AVDT_MEDIA_OCTET1 | 1;
1326            *p++ = type;
1327            /* save the location for length */
1328            plen = p;
1329            p+= 2;
1330            ssrc = avdt_scb_gen_ssrc(p_scb);
1331            UINT32_TO_BE_STREAM(p, ssrc);
1332
1333            switch(type)
1334            {
1335            case AVDT_RTCP_PT_SR:   /* Sender Report */
1336                *pm1 = AVDT_MEDIA_OCTET1;
1337                UINT32_TO_BE_STREAM(p, p_data->sr.ntp_sec);
1338                UINT32_TO_BE_STREAM(p, p_data->sr.ntp_frac);
1339                UINT32_TO_BE_STREAM(p, p_data->sr.rtp_time);
1340                UINT32_TO_BE_STREAM(p, p_data->sr.pkt_count);
1341                UINT32_TO_BE_STREAM(p, p_data->sr.octet_count);
1342                break;
1343
1344            case AVDT_RTCP_PT_RR:   /* Receiver Report */
1345                *p++ = p_data->rr.frag_lost;
1346                AVDT_TRACE_API1("packet_lost: %d", p_data->rr.packet_lost);
1347                p_data->rr.packet_lost &= 0xFFFFFF;
1348                AVDT_TRACE_API1("packet_lost: %d", p_data->rr.packet_lost);
1349                UINT24_TO_BE_STREAM(p, p_data->rr.packet_lost);
1350                UINT32_TO_BE_STREAM(p, p_data->rr.seq_num_rcvd);
1351                UINT32_TO_BE_STREAM(p, p_data->rr.jitter);
1352                UINT32_TO_BE_STREAM(p, p_data->rr.lsr);
1353                UINT32_TO_BE_STREAM(p, p_data->rr.dlsr);
1354                break;
1355
1356            case AVDT_RTCP_PT_SDES: /* Source Description */
1357                *p++ = AVDT_RTCP_SDES_CNAME;
1358                len = strlen((char *)p_data->cname);
1359                if(len > AVDT_MAX_CNAME_SIZE)
1360                    len = AVDT_MAX_CNAME_SIZE;
1361                *p++ = (UINT8)len;
1362                BCM_STRNCPY_S((char *)p, len+1, (char *)p_data->cname, len+1);
1363                p += len;
1364                break;
1365            }
1366            p_end = p;
1367            len = p - pm1 - 1;
1368            UINT16_TO_BE_STREAM(plen, len);
1369
1370#if AVDT_MULTIPLEXING == TRUE
1371            if(p_scb->curr_cfg.psc_mask & AVDT_PSC_MUX)
1372            {
1373                /* Adaptation Layer header */
1374                p = p_al;
1375                len++;
1376                UINT16_TO_BE_STREAM(p_al, len );
1377                /* TSID, no-fragment bit and coding of length(9-bit length field) */
1378                u = *p;
1379                *p = (p_scb->curr_cfg.mux_tsid_report<<3) | AVDT_ALH_LCODE_9BITM0;
1380                if(u)
1381                    *p |= AVDT_ALH_LCODE_9BITM1;
1382            }
1383#endif
1384
1385            /* set the actual payload length */
1386            p_pkt->len = p_end - p;
1387            /* send the packet */
1388            if(L2CAP_DW_FAILED != avdt_ad_write_req(AVDT_CHAN_REPORT, p_scb->p_ccb, p_scb, p_pkt))
1389                result = AVDT_SUCCESS;
1390        }
1391    }
1392
1393    return result;
1394}
1395#endif
1396
1397/******************************************************************************
1398**
1399** Function         AVDT_SetTraceLevel
1400**
1401** Description      Sets the trace level for AVDT. If 0xff is passed, the
1402**                  current trace level is returned.
1403**
1404**                  Input Parameters:
1405**                      new_level:  The level to set the AVDT tracing to:
1406**                      0xff-returns the current setting.
1407**                      0-turns off tracing.
1408**                      >= 1-Errors.
1409**                      >= 2-Warnings.
1410**                      >= 3-APIs.
1411**                      >= 4-Events.
1412**                      >= 5-Debug.
1413**
1414** Returns          The new trace level or current trace level if
1415**                  the input parameter is 0xff.
1416**
1417******************************************************************************/
1418UINT8 AVDT_SetTraceLevel (UINT8 new_level)
1419{
1420    if (new_level != 0xFF)
1421        avdt_cb.trace_level = new_level;
1422
1423    return (avdt_cb.trace_level);
1424}
1425
1426