• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 2002-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  *
21  *  This module contains API of the audio/video distribution transport
22  *  protocol.
23  *
24  ******************************************************************************/
25 
26 #include "avdt_api.h"
27 #include <string.h>
28 #include "avdt_int.h"
29 #include "avdtc_api.h"
30 #include "bt_target.h"
31 #include "bt_types.h"
32 #include "btm_api.h"
33 #include "btu.h"
34 #include "l2c_api.h"
35 #include "stack/include/a2dp_codec_api.h"
36 
37 /* Control block for AVDTP */
38 AvdtpCb avdtp_cb;
39 
avdt_ccb_idle_ccb_timer_timeout(void * data)40 void avdt_ccb_idle_ccb_timer_timeout(void* data) {
41   AvdtpCcb* p_ccb = (AvdtpCcb*)data;
42   uint8_t avdt_event = AVDT_CCB_IDLE_TOUT_EVT;
43   uint8_t err_code = AVDT_ERR_TIMEOUT;
44 
45   tAVDT_CCB_EVT avdt_ccb_evt;
46   avdt_ccb_evt.err_code = err_code;
47   avdt_ccb_event(p_ccb, avdt_event, &avdt_ccb_evt);
48 }
49 
avdt_ccb_ret_ccb_timer_timeout(void * data)50 void avdt_ccb_ret_ccb_timer_timeout(void* data) {
51   AvdtpCcb* p_ccb = (AvdtpCcb*)data;
52   uint8_t avdt_event = AVDT_CCB_RET_TOUT_EVT;
53   uint8_t err_code = AVDT_ERR_TIMEOUT;
54 
55   tAVDT_CCB_EVT avdt_ccb_evt;
56   avdt_ccb_evt.err_code = err_code;
57   avdt_ccb_event(p_ccb, avdt_event, &avdt_ccb_evt);
58 }
59 
avdt_ccb_rsp_ccb_timer_timeout(void * data)60 void avdt_ccb_rsp_ccb_timer_timeout(void* data) {
61   AvdtpCcb* p_ccb = (AvdtpCcb*)data;
62   uint8_t avdt_event = AVDT_CCB_RSP_TOUT_EVT;
63   uint8_t err_code = AVDT_ERR_TIMEOUT;
64 
65   tAVDT_CCB_EVT avdt_ccb_evt;
66   avdt_ccb_evt.err_code = err_code;
67   avdt_ccb_event(p_ccb, avdt_event, &avdt_ccb_evt);
68 }
69 
avdt_scb_transport_channel_timer_timeout(void * data)70 void avdt_scb_transport_channel_timer_timeout(void* data) {
71   AvdtpScb* p_scb = (AvdtpScb*)data;
72   uint8_t avdt_event = AVDT_SCB_TC_TOUT_EVT;
73 
74   avdt_scb_event(p_scb, avdt_event, NULL);
75 }
76 
77 /*******************************************************************************
78  *
79  * Function         AVDT_Register
80  *
81  * Description      This is the system level registration function for the
82  *                  AVDTP protocol.  This function initializes AVDTP and
83  *                  prepares the protocol stack for its use.  This function
84  *                  must be called once by the system or platform using AVDTP
85  *                  before the other functions of the API an be used.
86  *
87  *
88  * Returns          void
89  *
90  ******************************************************************************/
AVDT_Register(AvdtpRcb * p_reg,tAVDT_CTRL_CBACK * p_cback)91 void AVDT_Register(AvdtpRcb* p_reg, tAVDT_CTRL_CBACK* p_cback) {
92   /* register PSM with L2CAP */
93   L2CA_Register(AVDT_PSM, (tL2CAP_APPL_INFO*)&avdt_l2c_appl,
94                 true /* enable_snoop */);
95 
96   /* set security level */
97   BTM_SetSecurityLevel(true, "", BTM_SEC_SERVICE_AVDTP, p_reg->sec_mask,
98                        AVDT_PSM, BTM_SEC_PROTO_AVDT, AVDT_CHAN_SIG);
99   BTM_SetSecurityLevel(false, "", BTM_SEC_SERVICE_AVDTP, p_reg->sec_mask,
100                        AVDT_PSM, BTM_SEC_PROTO_AVDT, AVDT_CHAN_SIG);
101 
102   /* do not use security on the media channel */
103   BTM_SetSecurityLevel(true, "", BTM_SEC_SERVICE_AVDTP_NOSEC, BTM_SEC_NONE,
104                        AVDT_PSM, BTM_SEC_PROTO_AVDT, AVDT_CHAN_MEDIA);
105   BTM_SetSecurityLevel(false, "", BTM_SEC_SERVICE_AVDTP_NOSEC, BTM_SEC_NONE,
106                        AVDT_PSM, BTM_SEC_PROTO_AVDT, AVDT_CHAN_MEDIA);
107 
108   /* do not use security on the reporting channel */
109   BTM_SetSecurityLevel(true, "", BTM_SEC_SERVICE_AVDTP_NOSEC, BTM_SEC_NONE,
110                        AVDT_PSM, BTM_SEC_PROTO_AVDT, AVDT_CHAN_REPORT);
111   BTM_SetSecurityLevel(false, "", BTM_SEC_SERVICE_AVDTP_NOSEC, BTM_SEC_NONE,
112                        AVDT_PSM, BTM_SEC_PROTO_AVDT, AVDT_CHAN_REPORT);
113 
114   /* initialize AVDTP data structures */
115   avdt_scb_init();
116   avdt_ccb_init();
117   avdt_ad_init();
118 
119   /* copy registration struct */
120   avdtp_cb.rcb = *p_reg;
121   avdtp_cb.p_conn_cback = p_cback;
122 }
123 
124 /*******************************************************************************
125  *
126  * Function         AVDT_Deregister
127  *
128  * Description      This function is called to deregister use AVDTP protocol.
129  *                  It is called when AVDTP is no longer being used by any
130  *                  application in the system.  Before this function can be
131  *                  called, all streams must be removed with
132  *                  AVDT_RemoveStream().
133  *
134  *
135  * Returns          void
136  *
137  ******************************************************************************/
AVDT_Deregister(void)138 void AVDT_Deregister(void) {
139   /* deregister PSM with L2CAP */
140   L2CA_Deregister(AVDT_PSM);
141 }
142 
AVDT_AbortReq(uint8_t handle)143 void AVDT_AbortReq(uint8_t handle) {
144   AVDT_TRACE_WARNING("%s: handle=%d", __func__, handle);
145 
146   AvdtpScb* p_scb = avdt_scb_by_hdl(handle);
147   if (p_scb != NULL) {
148     avdt_scb_event(p_scb, AVDT_SCB_API_ABORT_REQ_EVT, NULL);
149   } else {
150     AVDT_TRACE_ERROR("%s Improper SCB, can not abort the stream", __func__);
151   }
152 }
153 
154 /*******************************************************************************
155  *
156  * Function         AVDT_CreateStream
157  *
158  * Description      Create a stream endpoint.  After a stream endpoint is
159  *                  created an application can initiate a connection between
160  *                  this endpoint and an endpoint on a peer device.  In
161  *                  addition, a peer device can discover, get the capabilities,
162  *                  and connect to this endpoint.
163  *
164  *
165  * Returns          AVDT_SUCCESS if successful, otherwise error.
166  *
167  ******************************************************************************/
AVDT_CreateStream(uint8_t peer_id,uint8_t * p_handle,const AvdtpStreamConfig & avdtp_stream_config)168 uint16_t AVDT_CreateStream(uint8_t peer_id, uint8_t* p_handle,
169                            const AvdtpStreamConfig& avdtp_stream_config) {
170   uint16_t result = AVDT_SUCCESS;
171   AvdtpScb* p_scb;
172 
173   AVDT_TRACE_DEBUG("%s: peer_id=%d", __func__, peer_id);
174 
175   /* Verify parameters; if invalid, return failure */
176   if (((avdtp_stream_config.cfg.psc_mask & (~AVDT_PSC)) != 0) ||
177       (avdtp_stream_config.p_avdt_ctrl_cback == NULL)) {
178     result = AVDT_BAD_PARAMS;
179   }
180   /* Allocate scb; if no scbs, return failure */
181   else {
182     p_scb = avdt_scb_alloc(peer_id, avdtp_stream_config);
183     if (p_scb == NULL) {
184       result = AVDT_NO_RESOURCES;
185     } else {
186       *p_handle = avdt_scb_to_hdl(p_scb);
187     }
188   }
189 
190   AVDT_TRACE_DEBUG("%s: result=%d handle=%d scb_index=%d", __func__, result,
191                    *p_handle, avdtp_stream_config.scb_index);
192 
193   return result;
194 }
195 
196 /*******************************************************************************
197  *
198  * Function         AVDT_RemoveStream
199  *
200  * Description      Remove a stream endpoint.  This function is called when
201  *                  the application is no longer using a stream endpoint.
202  *                  If this function is called when the endpoint is connected
203  *                  the connection is closed and then the stream endpoint
204  *                  is removed.
205  *
206  *
207  * Returns          AVDT_SUCCESS if successful, otherwise error.
208  *
209  ******************************************************************************/
AVDT_RemoveStream(uint8_t handle)210 uint16_t AVDT_RemoveStream(uint8_t handle) {
211   uint16_t result = AVDT_SUCCESS;
212   AvdtpScb* p_scb;
213 
214   AVDT_TRACE_DEBUG("%s: handle=%d", __func__, handle);
215 
216   /* look up scb */
217   p_scb = avdt_scb_by_hdl(handle);
218   if (p_scb == NULL) {
219     result = AVDT_BAD_HANDLE;
220   } else {
221     /* send remove event to scb */
222     avdt_scb_event(p_scb, AVDT_SCB_API_REMOVE_EVT, NULL);
223   }
224 
225   AVDT_TRACE_DEBUG("%s: result=%d", __func__, result);
226 
227   return result;
228 }
229 
230 /*******************************************************************************
231  *
232  * Function         AVDT_DiscoverReq
233  *
234  * Description      This function initiates a connection to the AVDTP service
235  *                  on the peer device, if not already present, and discovers
236  *                  the stream endpoints on the peer device.  (Please note
237  *                  that AVDTP discovery is unrelated to SDP discovery).
238  *                  This function can be called at any time regardless of
239  *                  whether there is an AVDTP connection to the peer device.
240  *
241  *                  When discovery is complete, an AVDT_DISCOVER_CFM_EVT
242  *                  is sent to the application via its callback function.
243  *                  The application must not call AVDT_GetCapReq() or
244  *                  AVDT_DiscoverReq() again to the same device until
245  *                  discovery is complete.
246  *
247  *                  The memory addressed by sep_info is allocated by the
248  *                  application.  This memory is written to by AVDTP as part
249  *                  of the discovery procedure.  This memory must remain
250  *                  accessible until the application receives the
251  *                  AVDT_DISCOVER_CFM_EVT.
252  *
253  * Returns          AVDT_SUCCESS if successful, otherwise error.
254  *
255  ******************************************************************************/
AVDT_DiscoverReq(const RawAddress & bd_addr,uint8_t channel_index,tAVDT_SEP_INFO * p_sep_info,uint8_t max_seps,tAVDT_CTRL_CBACK * p_cback)256 uint16_t AVDT_DiscoverReq(const RawAddress& bd_addr, uint8_t channel_index,
257                           tAVDT_SEP_INFO* p_sep_info, uint8_t max_seps,
258                           tAVDT_CTRL_CBACK* p_cback) {
259   AvdtpCcb* p_ccb;
260   uint16_t result = AVDT_SUCCESS;
261   tAVDT_CCB_EVT evt;
262 
263   AVDT_TRACE_DEBUG("%s", __func__);
264 
265   /* find channel control block for this bd addr; if none, allocate one */
266   p_ccb = avdt_ccb_by_bd(bd_addr);
267   if (p_ccb == NULL) {
268     p_ccb = avdt_ccb_alloc_by_channel_index(bd_addr, channel_index);
269     if (p_ccb == NULL) {
270       /* could not allocate channel control block */
271       result = AVDT_NO_RESOURCES;
272     }
273   }
274 
275   if (result == AVDT_SUCCESS) {
276     /* make sure no discovery or get capabilities req already in progress */
277     if (p_ccb->proc_busy) {
278       result = AVDT_BUSY;
279     }
280     /* send event to ccb */
281     else {
282       evt.discover.p_sep_info = p_sep_info;
283       evt.discover.num_seps = max_seps;
284       evt.discover.p_cback = p_cback;
285       avdt_ccb_event(p_ccb, AVDT_CCB_API_DISCOVER_REQ_EVT, &evt);
286     }
287   }
288 
289   AVDT_TRACE_DEBUG("%s: result=%d", __func__, result);
290 
291   return result;
292 }
293 
294 /*******************************************************************************
295  *
296  * Function         avdt_get_cap_req
297  *
298  * Description      internal function to serve AVDT_GetCapReq
299  *
300  * Returns          AVDT_SUCCESS if successful, otherwise error.
301  *
302  ******************************************************************************/
avdt_get_cap_req(const RawAddress & bd_addr,uint8_t channel_index,tAVDT_CCB_API_GETCAP * p_evt)303 static uint16_t avdt_get_cap_req(const RawAddress& bd_addr,
304                                  uint8_t channel_index,
305                                  tAVDT_CCB_API_GETCAP* p_evt) {
306   AvdtpCcb* p_ccb = NULL;
307   uint16_t result = AVDT_SUCCESS;
308 
309   AVDT_TRACE_DEBUG("%s", __func__);
310 
311   /* verify SEID */
312   if ((p_evt->single.seid < AVDT_SEID_MIN) ||
313       (p_evt->single.seid > AVDT_SEID_MAX)) {
314     AVDT_TRACE_ERROR("seid: %d", p_evt->single.seid);
315     result = AVDT_BAD_PARAMS;
316   }
317   /* find channel control block for this bd addr; if none, allocate one */
318   else {
319     p_ccb = avdt_ccb_by_bd(bd_addr);
320     if (p_ccb == NULL) {
321       p_ccb = avdt_ccb_alloc_by_channel_index(bd_addr, channel_index);
322       if (p_ccb == NULL) {
323         /* could not allocate channel control block */
324         result = AVDT_NO_RESOURCES;
325       }
326     }
327   }
328 
329   if (result == AVDT_SUCCESS) {
330     /* make sure no discovery or get capabilities req already in progress */
331     if (p_ccb->proc_busy) {
332       result = AVDT_BUSY;
333     }
334     /* send event to ccb */
335     else {
336       avdt_ccb_event(p_ccb, AVDT_CCB_API_GETCAP_REQ_EVT, (tAVDT_CCB_EVT*)p_evt);
337     }
338   }
339 
340   AVDT_TRACE_DEBUG("%s: result=%d", __func__, result);
341 
342   return result;
343 }
344 
345 /*******************************************************************************
346  *
347  * Function         AVDT_GetCapReq
348  *
349  * Description      This function initiates a connection to the AVDTP service
350  *                  on the peer device, if not already present, and gets the
351  *                  capabilities of a stream endpoint on the peer device.
352  *                  This function can be called at any time regardless of
353  *                  whether there is an AVDTP connection to the peer device.
354  *
355  *                  When the procedure is complete, an AVDT_GETCAP_CFM_EVT is
356  *                  sent to the application via its callback function.  The
357  *                  application must not call AVDT_GetCapReq() or
358  *                  AVDT_DiscoverReq() again until the procedure is complete.
359  *
360  *                  The memory pointed to by p_cfg is allocated by the
361  *                  application.  This memory is written to by AVDTP as part
362  *                  of the get capabilities procedure.  This memory must
363  *                  remain accessible until the application receives
364  *                  the AVDT_GETCAP_CFM_EVT.
365  *
366  * Returns          AVDT_SUCCESS if successful, otherwise error.
367  *
368  ******************************************************************************/
AVDT_GetCapReq(const RawAddress & bd_addr,uint8_t channel_index,uint8_t seid,AvdtpSepConfig * p_cfg,tAVDT_CTRL_CBACK * p_cback,bool get_all_cap)369 uint16_t AVDT_GetCapReq(const RawAddress& bd_addr, uint8_t channel_index,
370                         uint8_t seid, AvdtpSepConfig* p_cfg,
371                         tAVDT_CTRL_CBACK* p_cback, bool get_all_cap) {
372   tAVDT_CCB_API_GETCAP getcap;
373   uint16_t result = AVDT_SUCCESS;
374 
375   AVDT_TRACE_DEBUG("%s", __func__);
376 
377   getcap.single.seid = seid;
378   if (get_all_cap) {
379     getcap.single.sig_id = AVDT_SIG_GET_ALLCAP;
380   } else {
381     getcap.single.sig_id = AVDT_SIG_GETCAP;
382   }
383   getcap.p_cfg = p_cfg;
384   getcap.p_cback = p_cback;
385   result = avdt_get_cap_req(bd_addr, channel_index, &getcap);
386 
387   AVDT_TRACE_DEBUG("%s: result=%d", __func__, result);
388 
389   return result;
390 }
391 
392 /*******************************************************************************
393  *
394  * Function         AVDT_DelayReport
395  *
396  * Description      This functions sends a Delay Report to the peer device
397  *                  that is associated with a particular SEID.
398  *                  This function is called by SNK device.
399  *
400  * Returns          AVDT_SUCCESS if successful, otherwise error.
401  *
402  ******************************************************************************/
AVDT_DelayReport(uint8_t handle,uint8_t seid,uint16_t delay)403 uint16_t AVDT_DelayReport(uint8_t handle, uint8_t seid, uint16_t delay) {
404   AvdtpScb* p_scb;
405   uint16_t result = AVDT_SUCCESS;
406   tAVDT_SCB_EVT evt;
407 
408   AVDT_TRACE_DEBUG("%s: handle=%d ceid=%d delay=%d", __func__, handle, seid,
409                    delay);
410 
411   /* map handle to scb */
412   p_scb = avdt_scb_by_hdl(handle);
413   if (p_scb == NULL) {
414     result = AVDT_BAD_HANDLE;
415   } else
416   /* send event to scb */
417   {
418     evt.apidelay.hdr.seid = seid;
419     evt.apidelay.delay = delay;
420     avdt_scb_event(p_scb, AVDT_SCB_API_DELAY_RPT_REQ_EVT, &evt);
421   }
422 
423   AVDT_TRACE_DEBUG("%s: result=%d", __func__, result);
424 
425   return result;
426 }
427 
428 /*******************************************************************************
429  *
430  * Function         AVDT_OpenReq
431  *
432  * Description      This function initiates a connection to the AVDTP service
433  *                  on the peer device, if not already present, and connects
434  *                  to a stream endpoint on a peer device.  When the connection
435  *                  is completed, an AVDT_OPEN_CFM_EVT is sent to the
436  *                  application via the control callback function for this
437  *                  handle.
438  *
439  * Returns          AVDT_SUCCESS if successful, otherwise error.
440  *
441  ******************************************************************************/
AVDT_OpenReq(uint8_t handle,const RawAddress & bd_addr,uint8_t channel_index,uint8_t seid,AvdtpSepConfig * p_cfg)442 uint16_t AVDT_OpenReq(uint8_t handle, const RawAddress& bd_addr,
443                       uint8_t channel_index, uint8_t seid,
444                       AvdtpSepConfig* p_cfg) {
445   AvdtpCcb* p_ccb = NULL;
446   AvdtpScb* p_scb = NULL;
447   uint16_t result = AVDT_SUCCESS;
448   tAVDT_SCB_EVT evt;
449 
450   AVDT_TRACE_DEBUG("%s: handle=%d seid=%d", __func__, handle, seid);
451 
452   /* verify SEID */
453   if ((seid < AVDT_SEID_MIN) || (seid > AVDT_SEID_MAX)) {
454     result = AVDT_BAD_PARAMS;
455   }
456   /* map handle to scb */
457   else {
458     p_scb = avdt_scb_by_hdl(handle);
459     if (p_scb == NULL) {
460       result = AVDT_BAD_HANDLE;
461     }
462     /* find channel control block for this bd addr; if none, allocate one */
463     else {
464       p_ccb = avdt_ccb_by_bd(bd_addr);
465       if (p_ccb == NULL) {
466         p_ccb = avdt_ccb_alloc_by_channel_index(bd_addr, channel_index);
467         if (p_ccb == NULL) {
468           /* could not allocate channel control block */
469           result = AVDT_NO_RESOURCES;
470         }
471       }
472     }
473   }
474 
475   /* send event to scb */
476   if (result == AVDT_SUCCESS) {
477     AVDT_TRACE_DEBUG("%s: codec: %s", __func__,
478                      A2DP_CodecInfoString(p_cfg->codec_info).c_str());
479 
480     evt.msg.config_cmd.hdr.seid = seid;
481     evt.msg.config_cmd.hdr.ccb_idx = avdt_ccb_to_idx(p_ccb);
482     evt.msg.config_cmd.int_seid = handle;
483     evt.msg.config_cmd.p_cfg = p_cfg;
484     avdt_scb_event(p_scb, AVDT_SCB_API_SETCONFIG_REQ_EVT, &evt);
485   }
486 
487   AVDT_TRACE_DEBUG("%s: result=%d", __func__, result);
488 
489   return result;
490 }
491 
492 /*******************************************************************************
493  *
494  * Function         AVDT_ConfigRsp
495  *
496  * Description      Respond to a configure request from the peer device.  This
497  *                  function must be called if the application receives an
498  *                  AVDT_CONFIG_IND_EVT through its control callback.
499  *
500  *
501  * Returns          AVDT_SUCCESS if successful, otherwise error.
502  *
503  ******************************************************************************/
AVDT_ConfigRsp(uint8_t handle,uint8_t label,uint8_t error_code,uint8_t category)504 uint16_t AVDT_ConfigRsp(uint8_t handle, uint8_t label, uint8_t error_code,
505                         uint8_t category) {
506   AvdtpScb* p_scb;
507   tAVDT_SCB_EVT evt;
508   uint16_t result = AVDT_SUCCESS;
509   uint8_t event_code;
510 
511   AVDT_TRACE_DEBUG("%s: handle=%d label=%d error_code=0x%x category=%d",
512                    __func__, handle, label, error_code, category);
513 
514   /* map handle to scb */
515   p_scb = avdt_scb_by_hdl(handle);
516   if (p_scb == NULL) {
517     result = AVDT_BAD_HANDLE;
518   }
519   /* handle special case when this function is called but peer has not send
520   ** a configuration cmd; ignore and return error result
521   */
522   else if (!p_scb->in_use) {
523     result = AVDT_BAD_HANDLE;
524   }
525   /* send event to scb */
526   else {
527     evt.msg.hdr.err_code = error_code;
528     evt.msg.hdr.err_param = category;
529     evt.msg.hdr.label = label;
530     if (error_code == 0) {
531       event_code = AVDT_SCB_API_SETCONFIG_RSP_EVT;
532     } else {
533       event_code = AVDT_SCB_API_SETCONFIG_REJ_EVT;
534     }
535     avdt_scb_event(p_scb, event_code, &evt);
536   }
537 
538   AVDT_TRACE_DEBUG("%s: result=%d", __func__, result);
539 
540   return result;
541 }
542 
543 /*******************************************************************************
544  *
545  * Function         AVDT_StartReq
546  *
547  * Description      Start one or more stream endpoints.  This initiates the
548  *                  transfer of media packets for the streams.  All stream
549  *                  endpoints must previously be opened.  When the streams
550  *                  are started, an AVDT_START_CFM_EVT is sent to the
551  *                  application via the control callback function for each
552  *                  stream.
553  *
554  *
555  * Returns          AVDT_SUCCESS if successful, otherwise error.
556  *
557  ******************************************************************************/
AVDT_StartReq(uint8_t * p_handles,uint8_t num_handles)558 uint16_t AVDT_StartReq(uint8_t* p_handles, uint8_t num_handles) {
559   AvdtpScb* p_scb = NULL;
560   tAVDT_CCB_EVT evt;
561   uint16_t result = AVDT_SUCCESS;
562   int i;
563 
564   AVDT_TRACE_DEBUG("%s: num_handles=%d", __func__, num_handles);
565 
566   if ((num_handles == 0) || (num_handles > AVDT_NUM_SEPS)) {
567     result = AVDT_BAD_PARAMS;
568   } else {
569     /* verify handles */
570     for (i = 0; i < num_handles; i++) {
571       p_scb = avdt_scb_by_hdl(p_handles[i]);
572       if (p_scb == NULL) {
573         result = AVDT_BAD_HANDLE;
574         break;
575       }
576     }
577   }
578 
579   if (result == AVDT_SUCCESS) {
580     if (p_scb->p_ccb == NULL) {
581       result = AVDT_BAD_HANDLE;
582     } else {
583       /* send event to ccb */
584       memcpy(evt.msg.multi.seid_list, p_handles, num_handles);
585       evt.msg.multi.num_seps = num_handles;
586       avdt_ccb_event(p_scb->p_ccb, AVDT_CCB_API_START_REQ_EVT, &evt);
587     }
588   }
589 
590   AVDT_TRACE_DEBUG("%s: result=%d", __func__, result);
591 
592   return result;
593 }
594 
595 /*******************************************************************************
596  *
597  * Function         AVDT_SuspendReq
598  *
599  * Description      Suspend one or more stream endpoints. This suspends the
600  *                  transfer of media packets for the streams.  All stream
601  *                  endpoints must previously be open and started.  When the
602  *                  streams are suspended, an AVDT_SUSPEND_CFM_EVT is sent to
603  *                  the application via the control callback function for
604  *                  each stream.
605  *
606  *
607  * Returns          AVDT_SUCCESS if successful, otherwise error.
608  *
609  ******************************************************************************/
AVDT_SuspendReq(uint8_t * p_handles,uint8_t num_handles)610 uint16_t AVDT_SuspendReq(uint8_t* p_handles, uint8_t num_handles) {
611   AvdtpScb* p_scb = NULL;
612   tAVDT_CCB_EVT evt;
613   uint16_t result = AVDT_SUCCESS;
614   int i;
615 
616   AVDT_TRACE_DEBUG("%s: num_handles=%d", __func__, num_handles);
617 
618   if ((num_handles == 0) || (num_handles > AVDT_NUM_SEPS)) {
619     result = AVDT_BAD_PARAMS;
620   } else {
621     /* verify handles */
622     for (i = 0; i < num_handles; i++) {
623       p_scb = avdt_scb_by_hdl(p_handles[i]);
624       if (p_scb == NULL) {
625         result = AVDT_BAD_HANDLE;
626         break;
627       }
628     }
629   }
630 
631   if (result == AVDT_SUCCESS) {
632     if (p_scb->p_ccb == NULL) {
633       result = AVDT_BAD_HANDLE;
634     } else {
635       /* send event to ccb */
636       memcpy(evt.msg.multi.seid_list, p_handles, num_handles);
637       evt.msg.multi.num_seps = num_handles;
638       avdt_ccb_event(p_scb->p_ccb, AVDT_CCB_API_SUSPEND_REQ_EVT, &evt);
639     }
640   }
641 
642   AVDT_TRACE_DEBUG("%s: result=%d", __func__, result);
643 
644   return result;
645 }
646 
647 /*******************************************************************************
648  *
649  * Function         AVDT_CloseReq
650  *
651  * Description      Close a stream endpoint.  This stops the transfer of media
652  *                  packets and closes the transport channel associated with
653  *                  this stream endpoint.  When the stream is closed, an
654  *                  AVDT_CLOSE_CFM_EVT is sent to the application via the
655  *                  control callback function for this handle.
656  *
657  *
658  * Returns          AVDT_SUCCESS if successful, otherwise error.
659  *
660  ******************************************************************************/
AVDT_CloseReq(uint8_t handle)661 uint16_t AVDT_CloseReq(uint8_t handle) {
662   AvdtpScb* p_scb;
663   uint16_t result = AVDT_SUCCESS;
664 
665   AVDT_TRACE_DEBUG("%s: handle=%d", __func__, handle);
666 
667   /* map handle to scb */
668   p_scb = avdt_scb_by_hdl(handle);
669   if (p_scb == NULL) {
670     result = AVDT_BAD_HANDLE;
671   } else
672   /* send event to scb */
673   {
674     avdt_scb_event(p_scb, AVDT_SCB_API_CLOSE_REQ_EVT, NULL);
675   }
676 
677   AVDT_TRACE_DEBUG("%s: result=%d", __func__, result);
678 
679   return result;
680 }
681 
682 /*******************************************************************************
683  *
684  * Function         AVDT_ReconfigReq
685  *
686  * Description      Reconfigure a stream endpoint.  This allows the application
687  *                  to change the codec or content protection capabilities of
688  *                  a stream endpoint after it has been opened.  This function
689  *                  can only be called if the stream is opened but not started
690  *                  or if the stream has been suspended.  When the procedure
691  *                  is completed, an AVDT_RECONFIG_CFM_EVT is sent to the
692  *                  application via the control callback function for this
693  *                  handle.
694  *
695  *
696  * Returns          AVDT_SUCCESS if successful, otherwise error.
697  *
698  ******************************************************************************/
AVDT_ReconfigReq(uint8_t handle,AvdtpSepConfig * p_cfg)699 uint16_t AVDT_ReconfigReq(uint8_t handle, AvdtpSepConfig* p_cfg) {
700   AvdtpScb* p_scb;
701   uint16_t result = AVDT_SUCCESS;
702   tAVDT_SCB_EVT evt;
703 
704   AVDT_TRACE_DEBUG("%s: handle=%d", __func__, handle);
705 
706   /* map handle to scb */
707   p_scb = avdt_scb_by_hdl(handle);
708   if (p_scb == NULL) {
709     result = AVDT_BAD_HANDLE;
710   }
711   /* send event to scb */
712   else {
713     /* force psc_mask to zero */
714     p_cfg->psc_mask = 0;
715     evt.msg.reconfig_cmd.p_cfg = p_cfg;
716     avdt_scb_event(p_scb, AVDT_SCB_API_RECONFIG_REQ_EVT, &evt);
717   }
718 
719   AVDT_TRACE_DEBUG("%s: result=%d", __func__, result);
720 
721   return result;
722 }
723 
724 /*******************************************************************************
725  *
726  * Function         AVDT_ReconfigRsp
727  *
728  * Description      Respond to a reconfigure request from the peer device.
729  *                  This function must be called if the application receives
730  *                  an AVDT_RECONFIG_IND_EVT through its control callback.
731  *
732  *
733  * Returns          AVDT_SUCCESS if successful, otherwise error.
734  *
735  ******************************************************************************/
AVDT_ReconfigRsp(uint8_t handle,uint8_t label,uint8_t error_code,uint8_t category)736 uint16_t AVDT_ReconfigRsp(uint8_t handle, uint8_t label, uint8_t error_code,
737                           uint8_t category) {
738   AvdtpScb* p_scb;
739   tAVDT_SCB_EVT evt;
740   uint16_t result = AVDT_SUCCESS;
741 
742   AVDT_TRACE_DEBUG("%s: handle=%d label=%d error_code=0x%x category=%d",
743                    __func__, handle, label, error_code, category);
744 
745   /* map handle to scb */
746   p_scb = avdt_scb_by_hdl(handle);
747   if (p_scb == NULL) {
748     result = AVDT_BAD_HANDLE;
749   }
750   /* send event to scb */
751   else {
752     evt.msg.hdr.err_code = error_code;
753     evt.msg.hdr.err_param = category;
754     evt.msg.hdr.label = label;
755     avdt_scb_event(p_scb, AVDT_SCB_API_RECONFIG_RSP_EVT, &evt);
756   }
757 
758   AVDT_TRACE_DEBUG("%s: result=%d", __func__, result);
759 
760   return result;
761 }
762 
763 /*******************************************************************************
764  *
765  * Function         AVDT_SecurityReq
766  *
767  * Description      Send a security request to the peer device.  When the
768  *                  security procedure is completed, an AVDT_SECURITY_CFM_EVT
769  *                  is sent to the application via the control callback function
770  *                  for this handle.  (Please note that AVDTP security
771  *                  procedures are unrelated to Bluetooth link level security.)
772  *
773  *
774  * Returns          AVDT_SUCCESS if successful, otherwise error.
775  *
776  ******************************************************************************/
AVDT_SecurityReq(uint8_t handle,uint8_t * p_data,uint16_t len)777 uint16_t AVDT_SecurityReq(uint8_t handle, uint8_t* p_data, uint16_t len) {
778   AvdtpScb* p_scb;
779   uint16_t result = AVDT_SUCCESS;
780   tAVDT_SCB_EVT evt;
781 
782   AVDT_TRACE_DEBUG("%s: handle=%d len=%d", __func__, handle, len);
783 
784   /* map handle to scb */
785   p_scb = avdt_scb_by_hdl(handle);
786   if (p_scb == NULL) {
787     result = AVDT_BAD_HANDLE;
788   }
789   /* send event to scb */
790   else {
791     evt.msg.security_rsp.p_data = p_data;
792     evt.msg.security_rsp.len = len;
793     avdt_scb_event(p_scb, AVDT_SCB_API_SECURITY_REQ_EVT, &evt);
794   }
795 
796   AVDT_TRACE_DEBUG("%s: result=%d", __func__, result);
797 
798   return result;
799 }
800 
801 /*******************************************************************************
802  *
803  * Function         AVDT_SecurityRsp
804  *
805  * Description      Respond to a security request from the peer device.
806  *                  This function must be called if the application receives
807  *                  an AVDT_SECURITY_IND_EVT through its control callback.
808  *                  (Please note that AVDTP security procedures are unrelated
809  *                  to Bluetooth link level security.)
810  *
811  *
812  * Returns          AVDT_SUCCESS if successful, otherwise error.
813  *
814  ******************************************************************************/
AVDT_SecurityRsp(uint8_t handle,uint8_t label,uint8_t error_code,uint8_t * p_data,uint16_t len)815 uint16_t AVDT_SecurityRsp(uint8_t handle, uint8_t label, uint8_t error_code,
816                           uint8_t* p_data, uint16_t len) {
817   AvdtpScb* p_scb;
818   uint16_t result = AVDT_SUCCESS;
819   tAVDT_SCB_EVT evt;
820 
821   AVDT_TRACE_DEBUG("%s: handle=%d label=%d error_code=0x%x len=%d", __func__,
822                    handle, label, error_code, len);
823 
824   /* map handle to scb */
825   p_scb = avdt_scb_by_hdl(handle);
826   if (p_scb == NULL) {
827     result = AVDT_BAD_HANDLE;
828   }
829   /* send event to scb */
830   else {
831     evt.msg.security_rsp.hdr.err_code = error_code;
832     evt.msg.security_rsp.hdr.label = label;
833     evt.msg.security_rsp.p_data = p_data;
834     evt.msg.security_rsp.len = len;
835     avdt_scb_event(p_scb, AVDT_SCB_API_SECURITY_RSP_EVT, &evt);
836   }
837 
838   AVDT_TRACE_DEBUG("%s: result=%d", __func__, result);
839 
840   return result;
841 }
842 
843 /*******************************************************************************
844  *
845  * Function         AVDT_WriteReqOpt
846  *
847  * Description      Send a media packet to the peer device.  The stream must
848  *                  be started before this function is called.  Also, this
849  *                  function can only be called if the stream is a SRC.
850  *
851  *                  When AVDTP has sent the media packet and is ready for the
852  *                  next packet, an AVDT_WRITE_CFM_EVT is sent to the
853  *                  application via the control callback.  The application must
854  *                  wait for the AVDT_WRITE_CFM_EVT before it makes the next
855  *                  call to AVDT_WriteReq().  If the applications calls
856  *                  AVDT_WriteReq() before it receives the event the packet
857  *                  will not be sent.  The application may make its first call
858  *                  to AVDT_WriteReq() after it receives an AVDT_START_CFM_EVT
859  *                  or AVDT_START_IND_EVT.
860  *
861  *                  The application passes the packet using the BT_HDR
862  *                  structure.
863  *                  This structure is described in section 2.1.  The offset
864  *                  field must be equal to or greater than AVDT_MEDIA_OFFSET
865  *                  (if NO_RTP is specified, L2CAP_MIN_OFFSET can be used).
866  *                  This allows enough space in the buffer for the L2CAP and
867  *                  AVDTP headers.
868  *
869  *                  The memory pointed to by p_pkt must be a GKI buffer
870  *                  allocated by the application.  This buffer will be freed
871  *                  by the protocol stack; the application must not free
872  *                  this buffer.
873  *
874  *                  The opt parameter allows passing specific options like:
875  *                  - NO_RTP : do not add the RTP header to buffer
876  *
877  * Returns          AVDT_SUCCESS if successful, otherwise error.
878  *
879  ******************************************************************************/
AVDT_WriteReqOpt(uint8_t handle,BT_HDR * p_pkt,uint32_t time_stamp,uint8_t m_pt,tAVDT_DATA_OPT_MASK opt)880 uint16_t AVDT_WriteReqOpt(uint8_t handle, BT_HDR* p_pkt, uint32_t time_stamp,
881                           uint8_t m_pt, tAVDT_DATA_OPT_MASK opt) {
882   AvdtpScb* p_scb;
883   tAVDT_SCB_EVT evt;
884   uint16_t result = AVDT_SUCCESS;
885 
886   AVDT_TRACE_DEBUG("%s: handle=%d timestamp=%d m_pt=0x%x opt=0x%x", __func__,
887                    handle, time_stamp, m_pt, opt);
888 
889   /* map handle to scb */
890   p_scb = avdt_scb_by_hdl(handle);
891   if (p_scb == NULL) {
892     result = AVDT_BAD_HANDLE;
893   } else {
894     evt.apiwrite.p_buf = p_pkt;
895     evt.apiwrite.time_stamp = time_stamp;
896     evt.apiwrite.m_pt = m_pt;
897     evt.apiwrite.opt = opt;
898     avdt_scb_event(p_scb, AVDT_SCB_API_WRITE_REQ_EVT, &evt);
899   }
900 
901   AVDT_TRACE_DEBUG("%s: result=%d", __func__, result);
902 
903   return result;
904 }
905 
906 /*******************************************************************************
907  *
908  * Function         AVDT_WriteReq
909  *
910  * Description      Send a media packet to the peer device.  The stream must
911  *                  be started before this function is called.  Also, this
912  *                  function can only be called if the stream is a SRC.
913  *
914  *                  When AVDTP has sent the media packet and is ready for the
915  *                  next packet, an AVDT_WRITE_CFM_EVT is sent to the
916  *                  application via the control callback.  The application must
917  *                  wait for the AVDT_WRITE_CFM_EVT before it makes the next
918  *                  call to AVDT_WriteReq().  If the applications calls
919  *                  AVDT_WriteReq() before it receives the event the packet
920  *                  will not be sent.  The application may make its first call
921  *                  to AVDT_WriteReq() after it receives an AVDT_START_CFM_EVT
922  *                  or AVDT_START_IND_EVT.
923  *
924  *                  The application passes the packet using the BT_HDR
925  *                  structure.
926  *                  This structure is described in section 2.1.  The offset
927  *                  field must be equal to or greater than AVDT_MEDIA_OFFSET.
928  *                  This allows enough space in the buffer for the L2CAP and
929  *                  AVDTP headers.
930  *
931  *                  The memory pointed to by p_pkt must be a GKI buffer
932  *                  allocated by the application.  This buffer will be freed
933  *                  by the protocol stack; the application must not free
934  *                  this buffer.
935  *
936  *
937  * Returns          AVDT_SUCCESS if successful, otherwise error.
938  *
939  ******************************************************************************/
AVDT_WriteReq(uint8_t handle,BT_HDR * p_pkt,uint32_t time_stamp,uint8_t m_pt)940 uint16_t AVDT_WriteReq(uint8_t handle, BT_HDR* p_pkt, uint32_t time_stamp,
941                        uint8_t m_pt) {
942   return AVDT_WriteReqOpt(handle, p_pkt, time_stamp, m_pt, AVDT_DATA_OPT_NONE);
943 }
944 
945 /*******************************************************************************
946  *
947  * Function         AVDT_ConnectReq
948  *
949  * Description      This function initiates an AVDTP signaling connection
950  *                  to the peer device.  When the connection is completed, an
951  *                  AVDT_CONNECT_IND_EVT is sent to the application via its
952  *                  control callback function.  If the connection attempt fails
953  *                  an AVDT_DISCONNECT_IND_EVT is sent.  The security mask
954  *                  parameter overrides the outgoing security mask set in
955  *                  AVDT_Register().
956  *
957  * Returns          AVDT_SUCCESS if successful, otherwise error.
958  *
959  ******************************************************************************/
AVDT_ConnectReq(const RawAddress & bd_addr,uint8_t channel_index,uint8_t sec_mask,tAVDT_CTRL_CBACK * p_cback)960 uint16_t AVDT_ConnectReq(const RawAddress& bd_addr, uint8_t channel_index,
961                          uint8_t sec_mask, tAVDT_CTRL_CBACK* p_cback) {
962   AvdtpCcb* p_ccb = NULL;
963   uint16_t result = AVDT_SUCCESS;
964   tAVDT_CCB_EVT evt;
965 
966   AVDT_TRACE_WARNING("%s: address=%s channel_index=%d sec_mask=0x%x", __func__,
967                      bd_addr.ToString().c_str(), channel_index, sec_mask);
968 
969   /* find channel control block for this bd addr; if none, allocate one */
970   p_ccb = avdt_ccb_by_bd(bd_addr);
971   if (p_ccb == NULL) {
972     p_ccb = avdt_ccb_alloc_by_channel_index(bd_addr, channel_index);
973     if (p_ccb == NULL) {
974       /* could not allocate channel control block */
975       result = AVDT_NO_RESOURCES;
976     }
977   } else if (!p_ccb->ll_opened) {
978     AVDT_TRACE_WARNING("AVDT_ConnectReq: CCB LL is in the middle of opening");
979 
980     /* ccb was already allocated for the incoming signalling. */
981     result = AVDT_BUSY;
982   }
983 
984   if (result == AVDT_SUCCESS) {
985     /* send event to ccb */
986     evt.connect.p_cback = p_cback;
987     evt.connect.sec_mask = sec_mask;
988     avdt_ccb_event(p_ccb, AVDT_CCB_API_CONNECT_REQ_EVT, &evt);
989   }
990 
991   AVDT_TRACE_WARNING("%s: address=%s result=%d", __func__,
992                      bd_addr.ToString().c_str(), result);
993 
994   return result;
995 }
996 
997 /*******************************************************************************
998  *
999  * Function         AVDT_DisconnectReq
1000  *
1001  * Description      This function disconnect an AVDTP signaling connection
1002  *                  to the peer device.  When disconnected an
1003  *                  AVDT_DISCONNECT_IND_EVT is sent to the application via its
1004  *                  control callback function.
1005  *
1006  * Returns          AVDT_SUCCESS if successful, otherwise error.
1007  *
1008  ******************************************************************************/
AVDT_DisconnectReq(const RawAddress & bd_addr,tAVDT_CTRL_CBACK * p_cback)1009 uint16_t AVDT_DisconnectReq(const RawAddress& bd_addr,
1010                             tAVDT_CTRL_CBACK* p_cback) {
1011   AvdtpCcb* p_ccb = NULL;
1012   uint16_t result = AVDT_SUCCESS;
1013   tAVDT_CCB_EVT evt;
1014 
1015   AVDT_TRACE_WARNING("%s: address=%s", __func__, bd_addr.ToString().c_str());
1016 
1017   /* find channel control block for this bd addr; if none, error */
1018   p_ccb = avdt_ccb_by_bd(bd_addr);
1019   if (p_ccb == NULL) {
1020     result = AVDT_BAD_PARAMS;
1021   }
1022 
1023   if (result == AVDT_SUCCESS) {
1024     /* send event to ccb */
1025     evt.disconnect.p_cback = p_cback;
1026     avdt_ccb_event(p_ccb, AVDT_CCB_API_DISCONNECT_REQ_EVT, &evt);
1027   }
1028 
1029   AVDT_TRACE_DEBUG("%s: address=%s result=%d", __func__,
1030                    bd_addr.ToString().c_str(), result);
1031 
1032   return result;
1033 }
1034 
1035 /*******************************************************************************
1036  *
1037  * Function         AVDT_GetL2CapChannel
1038  *
1039  * Description      Get the L2CAP CID used by the handle.
1040  *
1041  * Returns          CID if successful, otherwise 0.
1042  *
1043  ******************************************************************************/
AVDT_GetL2CapChannel(uint8_t handle)1044 uint16_t AVDT_GetL2CapChannel(uint8_t handle) {
1045   AvdtpScb* p_scb;
1046   AvdtpCcb* p_ccb;
1047   uint8_t tcid;
1048   uint16_t lcid = 0;
1049 
1050   /* map handle to scb */
1051   if (((p_scb = avdt_scb_by_hdl(handle)) != NULL) &&
1052       ((p_ccb = p_scb->p_ccb) != NULL)) {
1053     /* get tcid from type, scb */
1054     tcid = avdt_ad_type_to_tcid(AVDT_CHAN_MEDIA, p_scb);
1055 
1056     lcid = avdtp_cb.ad.rt_tbl[avdt_ccb_to_idx(p_ccb)][tcid].lcid;
1057   }
1058 
1059   return (lcid);
1060 }
1061 
1062 /*******************************************************************************
1063  *
1064  * Function         AVDT_GetSignalChannel
1065  *
1066  * Description      Get the L2CAP CID used by the signal channel of the given
1067  *                  handle.
1068  *
1069  * Returns          CID if successful, otherwise 0.
1070  *
1071  ******************************************************************************/
AVDT_GetSignalChannel(uint8_t handle,const RawAddress & bd_addr)1072 uint16_t AVDT_GetSignalChannel(uint8_t handle, const RawAddress& bd_addr) {
1073   AvdtpScb* p_scb;
1074   AvdtpCcb* p_ccb;
1075   uint8_t tcid = 0; /* tcid is always 0 for signal channel */
1076   uint16_t lcid = 0;
1077 
1078   /* map handle to scb */
1079   if (((p_scb = avdt_scb_by_hdl(handle)) != NULL) &&
1080       ((p_ccb = p_scb->p_ccb) != NULL)) {
1081     lcid = avdtp_cb.ad.rt_tbl[avdt_ccb_to_idx(p_ccb)][tcid].lcid;
1082   } else {
1083     p_ccb = avdt_ccb_by_bd(bd_addr);
1084     if (p_ccb != NULL) {
1085       lcid = avdtp_cb.ad.rt_tbl[avdt_ccb_to_idx(p_ccb)][tcid].lcid;
1086     }
1087   }
1088 
1089   return (lcid);
1090 }
1091 
1092 /*******************************************************************************
1093  *
1094  * Function         AVDT_SendReport
1095  *
1096  * Description
1097  *
1098  *
1099  *
1100  * Returns
1101  *
1102  ******************************************************************************/
AVDT_SendReport(uint8_t handle,AVDT_REPORT_TYPE type,tAVDT_REPORT_DATA * p_data)1103 uint16_t AVDT_SendReport(uint8_t handle, AVDT_REPORT_TYPE type,
1104                          tAVDT_REPORT_DATA* p_data) {
1105   AvdtpScb* p_scb;
1106   uint16_t result = AVDT_BAD_PARAMS;
1107   AvdtpTransportChannel* p_tbl;
1108   uint8_t *p, *plen, *pm1, *p_end;
1109   uint32_t ssrc;
1110   uint16_t len;
1111 
1112   AVDT_TRACE_DEBUG("%s: handle=%d type=%d", __func__, handle, type);
1113 
1114   /* map handle to scb && verify parameters */
1115   if (((p_scb = avdt_scb_by_hdl(handle)) != NULL) && (p_scb->p_ccb != NULL) &&
1116       (((type == AVDT_RTCP_PT_SR) &&
1117         (p_scb->stream_config.tsep == AVDT_TSEP_SRC)) ||
1118        ((type == AVDT_RTCP_PT_RR) &&
1119         (p_scb->stream_config.tsep == AVDT_TSEP_SNK)) ||
1120        (type == AVDT_RTCP_PT_SDES))) {
1121     result = AVDT_NO_RESOURCES;
1122 
1123     /* build SR - assume fit in one packet */
1124     p_tbl = avdt_ad_tc_tbl_by_type(AVDT_CHAN_REPORT, p_scb->p_ccb, p_scb);
1125     if (p_tbl->state == AVDT_AD_ST_OPEN) {
1126       BT_HDR* p_pkt = (BT_HDR*)osi_malloc(p_tbl->peer_mtu + sizeof(BT_HDR));
1127 
1128       p_pkt->offset = L2CAP_MIN_OFFSET;
1129       p = (uint8_t*)(p_pkt + 1) + p_pkt->offset;
1130       pm1 = p;
1131       *p++ = AVDT_MEDIA_OCTET1 | 1;
1132       *p++ = type;
1133       /* save the location for length */
1134       plen = p;
1135       p += 2;
1136       ssrc = avdt_scb_gen_ssrc(p_scb);
1137       UINT32_TO_BE_STREAM(p, ssrc);
1138 
1139       switch (type) {
1140         case AVDT_RTCP_PT_SR: /* Sender Report */
1141           *pm1 = AVDT_MEDIA_OCTET1;
1142           UINT32_TO_BE_STREAM(p, p_data->sr.ntp_sec);
1143           UINT32_TO_BE_STREAM(p, p_data->sr.ntp_frac);
1144           UINT32_TO_BE_STREAM(p, p_data->sr.rtp_time);
1145           UINT32_TO_BE_STREAM(p, p_data->sr.pkt_count);
1146           UINT32_TO_BE_STREAM(p, p_data->sr.octet_count);
1147           break;
1148 
1149         case AVDT_RTCP_PT_RR: /* Receiver Report */
1150           *p++ = p_data->rr.frag_lost;
1151           AVDT_TRACE_API("packet_lost: %d", p_data->rr.packet_lost);
1152           p_data->rr.packet_lost &= 0xFFFFFF;
1153           AVDT_TRACE_API("packet_lost: %d", p_data->rr.packet_lost);
1154           UINT24_TO_BE_STREAM(p, p_data->rr.packet_lost);
1155           UINT32_TO_BE_STREAM(p, p_data->rr.seq_num_rcvd);
1156           UINT32_TO_BE_STREAM(p, p_data->rr.jitter);
1157           UINT32_TO_BE_STREAM(p, p_data->rr.lsr);
1158           UINT32_TO_BE_STREAM(p, p_data->rr.dlsr);
1159           break;
1160 
1161         case AVDT_RTCP_PT_SDES: /* Source Description */
1162           *p++ = AVDT_RTCP_SDES_CNAME;
1163           len = strlen((char*)p_data->cname);
1164           if (len > AVDT_MAX_CNAME_SIZE) len = AVDT_MAX_CNAME_SIZE;
1165           *p++ = (uint8_t)len;
1166           strlcpy((char*)p, (char*)p_data->cname, len + 1);
1167           p += len;
1168           break;
1169       }
1170       p_end = p;
1171       len = p - pm1 - 1;
1172       UINT16_TO_BE_STREAM(plen, len);
1173 
1174       /* set the actual payload length */
1175       p_pkt->len = p_end - p;
1176       /* send the packet */
1177       if (L2CAP_DW_FAILED !=
1178           avdt_ad_write_req(AVDT_CHAN_REPORT, p_scb->p_ccb, p_scb, p_pkt))
1179         result = AVDT_SUCCESS;
1180     }
1181   }
1182 
1183   AVDT_TRACE_DEBUG("%s: result=%d", __func__, result);
1184 
1185   return result;
1186 }
1187 
1188 /******************************************************************************
1189  *
1190  * Function         AVDT_SetTraceLevel
1191  *
1192  * Description      Sets the trace level for AVDT. If 0xff is passed, the
1193  *                  current trace level is returned.
1194  *
1195  *                  Input Parameters:
1196  *                      new_level:  The level to set the AVDT tracing to:
1197  *                      0xff-returns the current setting.
1198  *                      0-turns off tracing.
1199  *                      >= 1-Errors.
1200  *                      >= 2-Warnings.
1201  *                      >= 3-APIs.
1202  *                      >= 4-Events.
1203  *                      >= 5-Debug.
1204  *
1205  * Returns          The new trace level or current trace level if
1206  *                  the input parameter is 0xff.
1207  *
1208  *****************************************************************************/
AVDT_SetTraceLevel(uint8_t new_level)1209 uint8_t AVDT_SetTraceLevel(uint8_t new_level) {
1210   if (new_level != 0xFF) avdtp_cb.SetTraceLevel(new_level);
1211 
1212   return avdtp_cb.TraceLevel();
1213 }
1214 
stack_debug_avdtp_api_dump(int fd)1215 void stack_debug_avdtp_api_dump(int fd) {
1216   if (appl_trace_level < BT_TRACE_LEVEL_DEBUG) return;
1217 
1218   dprintf(fd, "\nAVDTP Stack State:\n");
1219   dprintf(fd, "  AVDTP signalling L2CAP channel MTU: %d\n",
1220           avdtp_cb.rcb.ctrl_mtu);
1221   dprintf(fd, "  Security mask: 0x%x\n", avdtp_cb.rcb.sec_mask);
1222 
1223   for (size_t i = 0; i < AVDT_NUM_LINKS; i++) {
1224     const AvdtpCcb& ccb = avdtp_cb.ccb[i];
1225     if (ccb.peer_addr.IsEmpty()) {
1226       continue;
1227     }
1228     dprintf(fd, "\n  Channel control block: %zu peer: %s\n", i,
1229             ccb.peer_addr.ToString().c_str());
1230     dprintf(fd, "    Allocated: %s\n", ccb.allocated ? "true" : "false");
1231     dprintf(fd, "    State: %d\n", ccb.state);
1232     dprintf(fd, "    Link-layer opened: %s\n",
1233             ccb.ll_opened ? "true" : "false");
1234     dprintf(fd, "    Discover in progress: %s\n",
1235             ccb.proc_busy ? "true" : "false");
1236     dprintf(fd, "    Congested: %s\n", ccb.cong ? "true" : "false");
1237     dprintf(fd, "    Reinitiate connection on idle: %s\n",
1238             ccb.reconn ? "true" : "false");
1239     dprintf(fd, "    Command retransmission count: %d\n", ccb.ret_count);
1240     dprintf(fd, "    BTA AV SCB index: %d\n", ccb.BtaAvScbIndex());
1241 
1242     for (size_t i = 0; i < AVDT_NUM_SEPS; i++) {
1243       const AvdtpScb& scb = ccb.scb[i];
1244       if (!scb.in_use) {
1245         continue;
1246       }
1247       dprintf(fd, "\n    Stream control block: %zu\n", i);
1248       dprintf(fd, "      SEP codec: %s\n",
1249               A2DP_CodecName(scb.stream_config.cfg.codec_info));
1250       dprintf(fd, "      SEP protocol service capabilities: 0x%x\n",
1251               scb.stream_config.cfg.psc_mask);
1252       dprintf(fd, "      SEP type: 0x%x\n", scb.stream_config.tsep);
1253       dprintf(fd, "      Media type: 0x%x\n", scb.stream_config.media_type);
1254       dprintf(fd, "      MTU: %d\n", scb.stream_config.mtu);
1255       dprintf(fd, "      SCB handle: %d\n", scb.ScbHandle());
1256       dprintf(fd, "      SCB index: %d\n", scb.stream_config.scb_index);
1257       dprintf(fd, "      Configured codec: %s\n",
1258               A2DP_CodecName(scb.curr_cfg.codec_info));
1259       dprintf(fd, "      Requested codec: %s\n",
1260               A2DP_CodecName(scb.req_cfg.codec_info));
1261       dprintf(fd, "      Transport channel connect timer: %s\n",
1262               alarm_is_scheduled(scb.transport_channel_timer)
1263                   ? "Scheduled"
1264                   : "Not scheduled");
1265       dprintf(fd, "      Channel control block peer: %s\n",
1266               (scb.p_ccb != nullptr) ? scb.p_ccb->peer_addr.ToString().c_str()
1267                                      : "null");
1268       dprintf(fd, "      Allocated: %s\n", scb.allocated ? "true" : "false");
1269       dprintf(fd, "      In use: %s\n", scb.in_use ? "true" : "false");
1270       dprintf(fd, "      Role: 0x%x\n", scb.role);
1271       dprintf(fd, "      Remove: %s\n", scb.remove ? "true" : "false");
1272       dprintf(fd, "      State: %d\n", scb.state);
1273       dprintf(fd, "      Peer SEID: %d\n", scb.peer_seid);
1274       dprintf(fd, "      Current event: %d\n", scb.curr_evt);
1275       dprintf(fd, "      Congested: %s\n", scb.cong ? "true" : "false");
1276       dprintf(fd, "      Close response code: %d\n", scb.close_code);
1277     }
1278   }
1279 }
1280