• 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 "bta/include/bta_api.h"
33 #include "btm_api.h"
34 #include "btu.h"
35 #include "l2c_api.h"
36 #include "main/shim/dumpsys.h"
37 #include "osi/include/log.h"
38 #include "stack/btm/btm_sec.h"
39 #include "stack/include/a2dp_codec_api.h"
40 
41 /* Control block for AVDTP */
42 AvdtpCb avdtp_cb;
43 
avdt_ccb_idle_ccb_timer_timeout(void * data)44 void avdt_ccb_idle_ccb_timer_timeout(void* data) {
45   AvdtpCcb* p_ccb = (AvdtpCcb*)data;
46   uint8_t avdt_event = AVDT_CCB_IDLE_TOUT_EVT;
47   uint8_t err_code = AVDT_ERR_TIMEOUT;
48 
49   tAVDT_CCB_EVT avdt_ccb_evt;
50   avdt_ccb_evt.err_code = err_code;
51   avdt_ccb_event(p_ccb, avdt_event, &avdt_ccb_evt);
52 }
53 
avdt_ccb_ret_ccb_timer_timeout(void * data)54 void avdt_ccb_ret_ccb_timer_timeout(void* data) {
55   AvdtpCcb* p_ccb = (AvdtpCcb*)data;
56   uint8_t avdt_event = AVDT_CCB_RET_TOUT_EVT;
57   uint8_t err_code = AVDT_ERR_TIMEOUT;
58 
59   tAVDT_CCB_EVT avdt_ccb_evt;
60   avdt_ccb_evt.err_code = err_code;
61   avdt_ccb_event(p_ccb, avdt_event, &avdt_ccb_evt);
62 }
63 
avdt_ccb_rsp_ccb_timer_timeout(void * data)64 void avdt_ccb_rsp_ccb_timer_timeout(void* data) {
65   AvdtpCcb* p_ccb = (AvdtpCcb*)data;
66   uint8_t avdt_event = AVDT_CCB_RSP_TOUT_EVT;
67   uint8_t err_code = AVDT_ERR_TIMEOUT;
68 
69   tAVDT_CCB_EVT avdt_ccb_evt;
70   avdt_ccb_evt.err_code = err_code;
71   avdt_ccb_event(p_ccb, avdt_event, &avdt_ccb_evt);
72 }
73 
avdt_scb_transport_channel_timer_timeout(void * data)74 void avdt_scb_transport_channel_timer_timeout(void* data) {
75   AvdtpScb* p_scb = (AvdtpScb*)data;
76   uint8_t avdt_event = AVDT_SCB_TC_TOUT_EVT;
77 
78   avdt_scb_event(p_scb, avdt_event, NULL);
79 }
80 
81 /*******************************************************************************
82  *
83  * Function         AVDT_Register
84  *
85  * Description      This is the system level registration function for the
86  *                  AVDTP protocol.  This function initializes AVDTP and
87  *                  prepares the protocol stack for its use.  This function
88  *                  must be called once by the system or platform using AVDTP
89  *                  before the other functions of the API an be used.
90  *
91  *
92  * Returns          void
93  *
94  ******************************************************************************/
AVDT_Register(AvdtpRcb * p_reg,tAVDT_CTRL_CBACK * p_cback)95 void AVDT_Register(AvdtpRcb* p_reg, tAVDT_CTRL_CBACK* p_cback) {
96   /* register PSM with L2CAP */
97   L2CA_Register2(AVDT_PSM, avdt_l2c_appl, true /* enable_snoop */, nullptr,
98                  kAvdtpMtu, 0, BTA_SEC_AUTHENTICATE);
99 
100   /* initialize AVDTP data structures */
101   avdt_scb_init();
102   avdt_ccb_init();
103   avdt_ad_init();
104 
105   /* copy registration struct */
106   avdtp_cb.rcb = *p_reg;
107   avdtp_cb.p_conn_cback = p_cback;
108 }
109 
110 /*******************************************************************************
111  *
112  * Function         AVDT_Deregister
113  *
114  * Description      This function is called to deregister use AVDTP protocol.
115  *                  It is called when AVDTP is no longer being used by any
116  *                  application in the system.  Before this function can be
117  *                  called, all streams must be removed with
118  *                  AVDT_RemoveStream().
119  *
120  *
121  * Returns          void
122  *
123  ******************************************************************************/
AVDT_Deregister(void)124 void AVDT_Deregister(void) {
125   /* deregister PSM with L2CAP */
126   L2CA_Deregister(AVDT_PSM);
127 }
128 
AVDT_AbortReq(uint8_t handle)129 void AVDT_AbortReq(uint8_t handle) {
130   AVDT_TRACE_WARNING("%s: avdt_handle=%d", __func__, handle);
131 
132   AvdtpScb* p_scb = avdt_scb_by_hdl(handle);
133   if (p_scb != NULL) {
134     avdt_scb_event(p_scb, AVDT_SCB_API_ABORT_REQ_EVT, NULL);
135   } else {
136     AVDT_TRACE_ERROR("%s Improper avdp_handle=%d, can not abort the stream",
137                      __func__, handle);
138   }
139 }
140 
141 /*******************************************************************************
142  *
143  * Function         AVDT_CreateStream
144  *
145  * Description      Create a stream endpoint.  After a stream endpoint is
146  *                  created an application can initiate a connection between
147  *                  this endpoint and an endpoint on a peer device.  In
148  *                  addition, a peer device can discover, get the capabilities,
149  *                  and connect to this endpoint.
150  *
151  *
152  * Returns          AVDT_SUCCESS if successful, otherwise error.
153  *
154  ******************************************************************************/
AVDT_CreateStream(uint8_t peer_id,uint8_t * p_handle,const AvdtpStreamConfig & avdtp_stream_config)155 uint16_t AVDT_CreateStream(uint8_t peer_id, uint8_t* p_handle,
156                            const AvdtpStreamConfig& avdtp_stream_config) {
157   tAVDT_RESULT result = AVDT_SUCCESS;
158   AvdtpScb* p_scb;
159 
160   /* Verify parameters; if invalid, return failure */
161   if (((avdtp_stream_config.cfg.psc_mask & (~AVDT_PSC)) != 0) ||
162       (avdtp_stream_config.p_avdt_ctrl_cback == NULL)) {
163     result = AVDT_BAD_PARAMS;
164     LOG_ERROR("Invalid AVDT stream endpoint parameters peer_id=%d scb_index=%d",
165               peer_id, avdtp_stream_config.scb_index);
166   }
167 
168   /* Allocate scb; if no scbs, return failure */
169   else {
170     p_scb = avdt_scb_alloc(peer_id, avdtp_stream_config);
171     if (p_scb == NULL) {
172       LOG_ERROR("Unable to create AVDT stream endpoint peer_id=%d scb_index=%d",
173                 peer_id, avdtp_stream_config.scb_index);
174       result = AVDT_NO_RESOURCES;
175     } else {
176       *p_handle = avdt_scb_to_hdl(p_scb);
177       LOG_DEBUG("Created stream endpoint peer_id=%d handle=%hhu", peer_id,
178                 *p_handle);
179     }
180   }
181   return static_cast<uint16_t>(result);
182 }
183 
184 /*******************************************************************************
185  *
186  * Function         AVDT_RemoveStream
187  *
188  * Description      Remove a stream endpoint.  This function is called when
189  *                  the application is no longer using a stream endpoint.
190  *                  If this function is called when the endpoint is connected
191  *                  the connection is closed and then the stream endpoint
192  *                  is removed.
193  *
194  *
195  * Returns          AVDT_SUCCESS if successful, otherwise error.
196  *
197  ******************************************************************************/
AVDT_RemoveStream(uint8_t handle)198 uint16_t AVDT_RemoveStream(uint8_t handle) {
199   uint16_t result = AVDT_SUCCESS;
200   AvdtpScb* p_scb;
201 
202   AVDT_TRACE_DEBUG("%s: avdt_handle=%d", __func__, handle);
203 
204   /* look up scb */
205   p_scb = avdt_scb_by_hdl(handle);
206   if (p_scb == NULL) {
207     result = AVDT_BAD_HANDLE;
208   } else {
209     /* send remove event to scb */
210     avdt_scb_event(p_scb, AVDT_SCB_API_REMOVE_EVT, NULL);
211   }
212 
213   if (result != AVDT_SUCCESS) {
214     AVDT_TRACE_ERROR("%s: result=%d avdt_handle=%d", __func__, result, handle);
215   }
216 
217   return result;
218 }
219 
220 /*******************************************************************************
221  *
222  * Function         AVDT_DiscoverReq
223  *
224  * Description      This function initiates a connection to the AVDTP service
225  *                  on the peer device, if not already present, and discovers
226  *                  the stream endpoints on the peer device.  (Please note
227  *                  that AVDTP discovery is unrelated to SDP discovery).
228  *                  This function can be called at any time regardless of
229  *                  whether there is an AVDTP connection to the peer device.
230  *
231  *                  When discovery is complete, an AVDT_DISCOVER_CFM_EVT
232  *                  is sent to the application via its callback function.
233  *                  The application must not call AVDT_GetCapReq() or
234  *                  AVDT_DiscoverReq() again to the same device until
235  *                  discovery is complete.
236  *
237  *                  The memory addressed by sep_info is allocated by the
238  *                  application.  This memory is written to by AVDTP as part
239  *                  of the discovery procedure.  This memory must remain
240  *                  accessible until the application receives the
241  *                  AVDT_DISCOVER_CFM_EVT.
242  *
243  * Returns          AVDT_SUCCESS if successful, otherwise error.
244  *
245  ******************************************************************************/
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)246 uint16_t AVDT_DiscoverReq(const RawAddress& bd_addr, uint8_t channel_index,
247                           tAVDT_SEP_INFO* p_sep_info, uint8_t max_seps,
248                           tAVDT_CTRL_CBACK* p_cback) {
249   AvdtpCcb* p_ccb;
250   uint16_t result = AVDT_SUCCESS;
251   tAVDT_CCB_EVT evt;
252 
253   AVDT_TRACE_DEBUG("%s", __func__);
254 
255   /* find channel control block for this bd addr; if none, allocate one */
256   p_ccb = avdt_ccb_by_bd(bd_addr);
257   if (p_ccb == NULL) {
258     p_ccb = avdt_ccb_alloc_by_channel_index(bd_addr, channel_index);
259     if (p_ccb == NULL) {
260       /* could not allocate channel control block */
261       result = AVDT_NO_RESOURCES;
262     }
263   }
264 
265   if (result == AVDT_SUCCESS) {
266     /* make sure no discovery or get capabilities req already in progress */
267     if (p_ccb->proc_busy) {
268       result = AVDT_BUSY;
269     }
270     /* send event to ccb */
271     else {
272       evt.discover.p_sep_info = p_sep_info;
273       evt.discover.num_seps = max_seps;
274       evt.discover.p_cback = p_cback;
275       avdt_ccb_event(p_ccb, AVDT_CCB_API_DISCOVER_REQ_EVT, &evt);
276     }
277   }
278 
279   if (result != AVDT_SUCCESS) {
280     AVDT_TRACE_ERROR("%s: result=%d address=%s", __func__, result,
281                      bd_addr.ToString().c_str());
282   }
283   return result;
284 }
285 
286 /*******************************************************************************
287  *
288  * Function         avdt_get_cap_req
289  *
290  * Description      internal function to serve AVDT_GetCapReq
291  *
292  * Returns          AVDT_SUCCESS if successful, otherwise error.
293  *
294  ******************************************************************************/
avdt_get_cap_req(const RawAddress & bd_addr,uint8_t channel_index,tAVDT_CCB_API_GETCAP * p_evt)295 static uint16_t avdt_get_cap_req(const RawAddress& bd_addr,
296                                  uint8_t channel_index,
297                                  tAVDT_CCB_API_GETCAP* p_evt) {
298   AvdtpCcb* p_ccb = NULL;
299   uint16_t result = AVDT_SUCCESS;
300 
301   AVDT_TRACE_DEBUG("%s", __func__);
302 
303   /* verify SEID */
304   if ((p_evt->single.seid < AVDT_SEID_MIN) ||
305       (p_evt->single.seid > AVDT_SEID_MAX)) {
306     AVDT_TRACE_ERROR("seid: %d", p_evt->single.seid);
307     result = AVDT_BAD_PARAMS;
308   }
309   /* find channel control block for this bd addr; if none, allocate one */
310   else {
311     p_ccb = avdt_ccb_by_bd(bd_addr);
312     if (p_ccb == NULL) {
313       p_ccb = avdt_ccb_alloc_by_channel_index(bd_addr, channel_index);
314       if (p_ccb == NULL) {
315         /* could not allocate channel control block */
316         result = AVDT_NO_RESOURCES;
317       }
318     }
319   }
320 
321   if (result == AVDT_SUCCESS) {
322     /* make sure no discovery or get capabilities req already in progress */
323     if (p_ccb->proc_busy) {
324       result = AVDT_BUSY;
325     }
326     /* send event to ccb */
327     else {
328       avdt_ccb_event(p_ccb, AVDT_CCB_API_GETCAP_REQ_EVT, (tAVDT_CCB_EVT*)p_evt);
329     }
330   }
331 
332   if (result != AVDT_SUCCESS) {
333     AVDT_TRACE_ERROR("%s: result=%d address=%s", __func__, result,
334                      bd_addr.ToString().c_str());
335   }
336   return result;
337 }
338 
339 /*******************************************************************************
340  *
341  * Function         AVDT_GetCapReq
342  *
343  * Description      This function initiates a connection to the AVDTP service
344  *                  on the peer device, if not already present, and gets the
345  *                  capabilities of a stream endpoint on the peer device.
346  *                  This function can be called at any time regardless of
347  *                  whether there is an AVDTP connection to the peer device.
348  *
349  *                  When the procedure is complete, an AVDT_GETCAP_CFM_EVT is
350  *                  sent to the application via its callback function.  The
351  *                  application must not call AVDT_GetCapReq() or
352  *                  AVDT_DiscoverReq() again until the procedure is complete.
353  *
354  *                  The memory pointed to by p_cfg is allocated by the
355  *                  application.  This memory is written to by AVDTP as part
356  *                  of the get capabilities procedure.  This memory must
357  *                  remain accessible until the application receives
358  *                  the AVDT_GETCAP_CFM_EVT.
359  *
360  * Returns          AVDT_SUCCESS if successful, otherwise error.
361  *
362  ******************************************************************************/
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)363 uint16_t AVDT_GetCapReq(const RawAddress& bd_addr, uint8_t channel_index,
364                         uint8_t seid, AvdtpSepConfig* p_cfg,
365                         tAVDT_CTRL_CBACK* p_cback, bool get_all_cap) {
366   tAVDT_CCB_API_GETCAP getcap;
367   uint16_t result = AVDT_SUCCESS;
368 
369   AVDT_TRACE_DEBUG("%s", __func__);
370 
371   getcap.single.seid = seid;
372   if (get_all_cap) {
373     getcap.single.sig_id = AVDT_SIG_GET_ALLCAP;
374   } else {
375     getcap.single.sig_id = AVDT_SIG_GETCAP;
376   }
377   getcap.p_cfg = p_cfg;
378   getcap.p_cback = p_cback;
379   result = avdt_get_cap_req(bd_addr, channel_index, &getcap);
380 
381   if (result != AVDT_SUCCESS) {
382     AVDT_TRACE_ERROR("%s: result=%d address=%s", __func__, result,
383                      bd_addr.ToString().c_str());
384   }
385   return result;
386 }
387 
388 /*******************************************************************************
389  *
390  * Function         AVDT_DelayReport
391  *
392  * Description      This functions sends a Delay Report to the peer device
393  *                  that is associated with a particular SEID.
394  *                  This function is called by SNK device.
395  *
396  * Returns          AVDT_SUCCESS if successful, otherwise error.
397  *
398  ******************************************************************************/
AVDT_DelayReport(uint8_t handle,uint8_t seid,uint16_t delay)399 uint16_t AVDT_DelayReport(uint8_t handle, uint8_t seid, uint16_t delay) {
400   AvdtpScb* p_scb;
401   uint16_t result = AVDT_SUCCESS;
402   tAVDT_SCB_EVT evt;
403 
404   AVDT_TRACE_DEBUG("%s: avdt_handle=%d seid=%d delay=%d", __func__, handle,
405                    seid, delay);
406 
407   /* map handle to scb */
408   p_scb = avdt_scb_by_hdl(handle);
409   if (p_scb == NULL) {
410     result = AVDT_BAD_HANDLE;
411   } else
412   /* send event to scb */
413   {
414     evt.apidelay.hdr.seid = seid;
415     evt.apidelay.delay = delay;
416     avdt_scb_event(p_scb, AVDT_SCB_API_DELAY_RPT_REQ_EVT, &evt);
417   }
418 
419   if (result != AVDT_SUCCESS) {
420     AVDT_TRACE_ERROR("%s: result=%d avdt_handle=%d seid=%d", __func__, result,
421                      handle, seid);
422   }
423   return result;
424 }
425 
426 /*******************************************************************************
427  *
428  * Function         AVDT_OpenReq
429  *
430  * Description      This function initiates a connection to the AVDTP service
431  *                  on the peer device, if not already present, and connects
432  *                  to a stream endpoint on a peer device.  When the connection
433  *                  is completed, an AVDT_OPEN_CFM_EVT is sent to the
434  *                  application via the control callback function for this
435  *                  handle.
436  *
437  * Returns          AVDT_SUCCESS if successful, otherwise error.
438  *
439  ******************************************************************************/
AVDT_OpenReq(uint8_t handle,const RawAddress & bd_addr,uint8_t channel_index,uint8_t seid,AvdtpSepConfig * p_cfg)440 uint16_t AVDT_OpenReq(uint8_t handle, const RawAddress& bd_addr,
441                       uint8_t channel_index, uint8_t seid,
442                       AvdtpSepConfig* p_cfg) {
443   AvdtpCcb* p_ccb = NULL;
444   AvdtpScb* p_scb = NULL;
445   uint16_t result = AVDT_SUCCESS;
446   tAVDT_SCB_EVT evt;
447 
448   AVDT_TRACE_API("%s: address=%s avdt_handle=%d seid=%d", __func__,
449                  bd_addr.ToString().c_str(), handle, seid);
450 
451   /* verify SEID */
452   if ((seid < AVDT_SEID_MIN) || (seid > AVDT_SEID_MAX)) {
453     result = AVDT_BAD_PARAMS;
454   }
455   /* map handle to scb */
456   else {
457     p_scb = avdt_scb_by_hdl(handle);
458     if (p_scb == NULL) {
459       result = AVDT_BAD_HANDLE;
460     }
461     /* find channel control block for this bd addr; if none, allocate one */
462     else {
463       p_ccb = avdt_ccb_by_bd(bd_addr);
464       if (p_ccb == NULL) {
465         p_ccb = avdt_ccb_alloc_by_channel_index(bd_addr, channel_index);
466         if (p_ccb == NULL) {
467           /* could not allocate channel control block */
468           result = AVDT_NO_RESOURCES;
469         }
470       }
471     }
472   }
473 
474   /* send event to scb */
475   if (result == AVDT_SUCCESS) {
476     AVDT_TRACE_DEBUG("%s: codec: %s", __func__,
477                      A2DP_CodecInfoString(p_cfg->codec_info).c_str());
478 
479     evt.msg.config_cmd.hdr.seid = seid;
480     evt.msg.config_cmd.hdr.ccb_idx = avdt_ccb_to_idx(p_ccb);
481     evt.msg.config_cmd.int_seid = handle;
482     evt.msg.config_cmd.p_cfg = p_cfg;
483     avdt_scb_event(p_scb, AVDT_SCB_API_SETCONFIG_REQ_EVT, &evt);
484   } else {
485     AVDT_TRACE_ERROR("%s: result=%d address=%s avdt_handle=%d", __func__,
486                      result, bd_addr.ToString().c_str(), handle);
487   }
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: avdt_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   if (result != AVDT_SUCCESS) {
539     AVDT_TRACE_ERROR("%s: result=%d avdt_handle=%d", __func__, result, handle);
540   }
541   return result;
542 }
543 
544 /*******************************************************************************
545  *
546  * Function         AVDT_StartReq
547  *
548  * Description      Start one or more stream endpoints.  This initiates the
549  *                  transfer of media packets for the streams.  All stream
550  *                  endpoints must previously be opened.  When the streams
551  *                  are started, an AVDT_START_CFM_EVT is sent to the
552  *                  application via the control callback function for each
553  *                  stream.
554  *
555  *
556  * Returns          AVDT_SUCCESS if successful, otherwise error.
557  *
558  ******************************************************************************/
AVDT_StartReq(uint8_t * p_handles,uint8_t num_handles)559 uint16_t AVDT_StartReq(uint8_t* p_handles, uint8_t num_handles) {
560   AvdtpScb* p_scb = NULL;
561   tAVDT_CCB_EVT evt;
562   uint16_t result = AVDT_SUCCESS;
563   int i;
564 
565   AVDT_TRACE_DEBUG("%s: num_handles=%d", __func__, num_handles);
566 
567   if ((num_handles == 0) || (num_handles > AVDT_NUM_SEPS)) {
568     result = AVDT_BAD_PARAMS;
569   } else {
570     /* verify handles */
571     for (i = 0; i < num_handles; i++) {
572       p_scb = avdt_scb_by_hdl(p_handles[i]);
573       if (p_scb == NULL) {
574         result = AVDT_BAD_HANDLE;
575         break;
576       }
577     }
578   }
579 
580   if (result == AVDT_SUCCESS) {
581     if (p_scb->p_ccb == NULL) {
582       result = AVDT_BAD_HANDLE;
583     } else {
584       /* send event to ccb */
585       memcpy(evt.msg.multi.seid_list, p_handles, num_handles);
586       evt.msg.multi.num_seps = num_handles;
587       avdt_ccb_event(p_scb->p_ccb, AVDT_CCB_API_START_REQ_EVT, &evt);
588     }
589   }
590 
591   if (result != AVDT_SUCCESS) {
592     if ((num_handles == 0) || (num_handles > AVDT_NUM_SEPS)) {
593       AVDT_TRACE_ERROR("%s: result=%d num_handles=%d invalid", __func__, result,
594                        num_handles);
595     } else {
596       AVDT_TRACE_ERROR(
597           "%s: result=%d avdt_handle=%d", __func__, result,
598           (i < num_handles ? p_handles[i] : p_handles[num_handles - 1]));
599     }
600   }
601   return result;
602 }
603 
604 /*******************************************************************************
605  *
606  * Function         AVDT_SuspendReq
607  *
608  * Description      Suspend one or more stream endpoints. This suspends the
609  *                  transfer of media packets for the streams.  All stream
610  *                  endpoints must previously be open and started.  When the
611  *                  streams are suspended, an AVDT_SUSPEND_CFM_EVT is sent to
612  *                  the application via the control callback function for
613  *                  each stream.
614  *
615  *
616  * Returns          AVDT_SUCCESS if successful, otherwise error.
617  *
618  ******************************************************************************/
AVDT_SuspendReq(uint8_t * p_handles,uint8_t num_handles)619 uint16_t AVDT_SuspendReq(uint8_t* p_handles, uint8_t num_handles) {
620   AvdtpScb* p_scb = NULL;
621   tAVDT_CCB_EVT evt;
622   uint16_t result = AVDT_SUCCESS;
623   int i;
624 
625   AVDT_TRACE_DEBUG("%s: num_handles=%d", __func__, num_handles);
626 
627   if ((num_handles == 0) || (num_handles > AVDT_NUM_SEPS)) {
628     result = AVDT_BAD_PARAMS;
629   } else {
630     /* verify handles */
631     for (i = 0; i < num_handles; i++) {
632       p_scb = avdt_scb_by_hdl(p_handles[i]);
633       if (p_scb == NULL) {
634         result = AVDT_BAD_HANDLE;
635         break;
636       }
637     }
638   }
639 
640   if (result == AVDT_SUCCESS) {
641     if (p_scb->p_ccb == NULL) {
642       result = AVDT_BAD_HANDLE;
643     } else {
644       /* send event to ccb */
645       memcpy(evt.msg.multi.seid_list, p_handles, num_handles);
646       evt.msg.multi.num_seps = num_handles;
647       avdt_ccb_event(p_scb->p_ccb, AVDT_CCB_API_SUSPEND_REQ_EVT, &evt);
648     }
649   }
650 
651   if (result != AVDT_SUCCESS) {
652     if ((num_handles == 0) || (num_handles > AVDT_NUM_SEPS)) {
653       AVDT_TRACE_ERROR("%s: result=%d num_handles=%d invalid", __func__, result,
654                        num_handles);
655     } else {
656       AVDT_TRACE_ERROR(
657           "%s: result=%d avdt_handle=%d", __func__, result,
658           (i < num_handles ? p_handles[i] : p_handles[num_handles - 1]));
659     }
660   }
661   return result;
662 }
663 
664 /*******************************************************************************
665  *
666  * Function         AVDT_CloseReq
667  *
668  * Description      Close a stream endpoint.  This stops the transfer of media
669  *                  packets and closes the transport channel associated with
670  *                  this stream endpoint.  When the stream is closed, an
671  *                  AVDT_CLOSE_CFM_EVT is sent to the application via the
672  *                  control callback function for this handle.
673  *
674  *
675  * Returns          AVDT_SUCCESS if successful, otherwise error.
676  *
677  ******************************************************************************/
AVDT_CloseReq(uint8_t handle)678 uint16_t AVDT_CloseReq(uint8_t handle) {
679   AvdtpScb* p_scb;
680   uint16_t result = AVDT_SUCCESS;
681 
682   AVDT_TRACE_API("%s: avdt_handle=%d", __func__, handle);
683 
684   /* map handle to scb */
685   p_scb = avdt_scb_by_hdl(handle);
686   if (p_scb == NULL) {
687     result = AVDT_BAD_HANDLE;
688   } else
689   /* send event to scb */
690   {
691     avdt_scb_event(p_scb, AVDT_SCB_API_CLOSE_REQ_EVT, NULL);
692   }
693 
694   if (result != AVDT_SUCCESS) {
695     AVDT_TRACE_ERROR("%s: result=%d avdt_handle=%d", __func__, result, handle);
696   }
697   return result;
698 }
699 
700 /*******************************************************************************
701  *
702  * Function         AVDT_ReconfigReq
703  *
704  * Description      Reconfigure a stream endpoint.  This allows the application
705  *                  to change the codec or content protection capabilities of
706  *                  a stream endpoint after it has been opened.  This function
707  *                  can only be called if the stream is opened but not started
708  *                  or if the stream has been suspended.  When the procedure
709  *                  is completed, an AVDT_RECONFIG_CFM_EVT is sent to the
710  *                  application via the control callback function for this
711  *                  handle.
712  *
713  *
714  * Returns          AVDT_SUCCESS if successful, otherwise error.
715  *
716  ******************************************************************************/
AVDT_ReconfigReq(uint8_t handle,AvdtpSepConfig * p_cfg)717 uint16_t AVDT_ReconfigReq(uint8_t handle, AvdtpSepConfig* p_cfg) {
718   AvdtpScb* p_scb;
719   uint16_t result = AVDT_SUCCESS;
720   tAVDT_SCB_EVT evt;
721 
722   AVDT_TRACE_DEBUG("%s: avdt_handle=%d", __func__, handle);
723 
724   /* map handle to scb */
725   p_scb = avdt_scb_by_hdl(handle);
726   if (p_scb == NULL) {
727     result = AVDT_BAD_HANDLE;
728   }
729   /* send event to scb */
730   else {
731     /* force psc_mask to zero */
732     p_cfg->psc_mask = 0;
733     evt.msg.reconfig_cmd.p_cfg = p_cfg;
734     avdt_scb_event(p_scb, AVDT_SCB_API_RECONFIG_REQ_EVT, &evt);
735   }
736 
737   if (result != AVDT_SUCCESS) {
738     AVDT_TRACE_ERROR("%s: result=%d avdt_handle=%d", __func__, result, handle);
739   }
740   return result;
741 }
742 
743 /*******************************************************************************
744  *
745  * Function         AVDT_SecurityReq
746  *
747  * Description      Send a security request to the peer device.  When the
748  *                  security procedure is completed, an AVDT_SECURITY_CFM_EVT
749  *                  is sent to the application via the control callback function
750  *                  for this handle.  (Please note that AVDTP security
751  *                  procedures are unrelated to Bluetooth link level security.)
752  *
753  *
754  * Returns          AVDT_SUCCESS if successful, otherwise error.
755  *
756  ******************************************************************************/
AVDT_SecurityReq(uint8_t handle,uint8_t * p_data,uint16_t len)757 uint16_t AVDT_SecurityReq(uint8_t handle, uint8_t* p_data, uint16_t len) {
758   AvdtpScb* p_scb;
759   uint16_t result = AVDT_SUCCESS;
760   tAVDT_SCB_EVT evt;
761 
762   AVDT_TRACE_DEBUG("%s: avdt_handle=%d len=%d", __func__, handle, len);
763 
764   /* map handle to scb */
765   p_scb = avdt_scb_by_hdl(handle);
766   if (p_scb == NULL) {
767     result = AVDT_BAD_HANDLE;
768   }
769   /* send event to scb */
770   else {
771     evt.msg.security_rsp.p_data = p_data;
772     evt.msg.security_rsp.len = len;
773     avdt_scb_event(p_scb, AVDT_SCB_API_SECURITY_REQ_EVT, &evt);
774   }
775 
776   if (result != AVDT_SUCCESS) {
777     AVDT_TRACE_ERROR("%s: result=%d avdt_handle=%d", __func__, result, handle);
778   }
779   return result;
780 }
781 
782 /*******************************************************************************
783  *
784  * Function         AVDT_SecurityRsp
785  *
786  * Description      Respond to a security request from the peer device.
787  *                  This function must be called if the application receives
788  *                  an AVDT_SECURITY_IND_EVT through its control callback.
789  *                  (Please note that AVDTP security procedures are unrelated
790  *                  to Bluetooth link level security.)
791  *
792  *
793  * Returns          AVDT_SUCCESS if successful, otherwise error.
794  *
795  ******************************************************************************/
AVDT_SecurityRsp(uint8_t handle,uint8_t label,uint8_t error_code,uint8_t * p_data,uint16_t len)796 uint16_t AVDT_SecurityRsp(uint8_t handle, uint8_t label, uint8_t error_code,
797                           uint8_t* p_data, uint16_t len) {
798   AvdtpScb* p_scb;
799   uint16_t result = AVDT_SUCCESS;
800   tAVDT_SCB_EVT evt;
801 
802   AVDT_TRACE_DEBUG("%s: avdt_handle=%d label=%d error_code=0x%x len=%d",
803                    __func__, handle, label, error_code, len);
804 
805   /* map handle to scb */
806   p_scb = avdt_scb_by_hdl(handle);
807   if (p_scb == NULL) {
808     result = AVDT_BAD_HANDLE;
809   }
810   /* send event to scb */
811   else {
812     evt.msg.security_rsp.hdr.err_code = error_code;
813     evt.msg.security_rsp.hdr.label = label;
814     evt.msg.security_rsp.p_data = p_data;
815     evt.msg.security_rsp.len = len;
816     avdt_scb_event(p_scb, AVDT_SCB_API_SECURITY_RSP_EVT, &evt);
817   }
818 
819   if (result != AVDT_SUCCESS) {
820     AVDT_TRACE_ERROR("%s: result=%d avdt_handle=%d", __func__, result, handle);
821   }
822   return result;
823 }
824 
825 /*******************************************************************************
826  *
827  * Function         AVDT_WriteReqOpt
828  *
829  * Description      Send a media packet to the peer device.  The stream must
830  *                  be started before this function is called.  Also, this
831  *                  function can only be called if the stream is a SRC.
832  *
833  *                  When AVDTP has sent the media packet and is ready for the
834  *                  next packet, an AVDT_WRITE_CFM_EVT is sent to the
835  *                  application via the control callback.  The application must
836  *                  wait for the AVDT_WRITE_CFM_EVT before it makes the next
837  *                  call to AVDT_WriteReq().  If the applications calls
838  *                  AVDT_WriteReq() before it receives the event the packet
839  *                  will not be sent.  The application may make its first call
840  *                  to AVDT_WriteReq() after it receives an AVDT_START_CFM_EVT
841  *                  or AVDT_START_IND_EVT.
842  *
843  *                  The application passes the packet using the BT_HDR
844  *                  structure.
845  *                  This structure is described in section 2.1.  The offset
846  *                  field must be equal to or greater than AVDT_MEDIA_OFFSET
847  *                  (if NO_RTP is specified, L2CAP_MIN_OFFSET can be used).
848  *                  This allows enough space in the buffer for the L2CAP and
849  *                  AVDTP headers.
850  *
851  *                  The memory pointed to by p_pkt must be a GKI buffer
852  *                  allocated by the application.  This buffer will be freed
853  *                  by the protocol stack; the application must not free
854  *                  this buffer.
855  *
856  *                  The opt parameter allows passing specific options like:
857  *                  - NO_RTP : do not add the RTP header to buffer
858  *
859  * Returns          AVDT_SUCCESS if successful, otherwise error.
860  *
861  ******************************************************************************/
AVDT_WriteReqOpt(uint8_t handle,BT_HDR * p_pkt,uint32_t time_stamp,uint8_t m_pt,tAVDT_DATA_OPT_MASK opt)862 uint16_t AVDT_WriteReqOpt(uint8_t handle, BT_HDR* p_pkt, uint32_t time_stamp,
863                           uint8_t m_pt, tAVDT_DATA_OPT_MASK opt) {
864   AvdtpScb* p_scb;
865   tAVDT_SCB_EVT evt;
866   uint16_t result = AVDT_SUCCESS;
867 
868   AVDT_TRACE_DEBUG("%s: avdt_handle=%d timestamp=%d m_pt=0x%x opt=0x%x",
869                    __func__, handle, time_stamp, m_pt, opt);
870 
871   /* map handle to scb */
872   p_scb = avdt_scb_by_hdl(handle);
873   if (p_scb == NULL) {
874     result = AVDT_BAD_HANDLE;
875   } else {
876     evt.apiwrite.p_buf = p_pkt;
877     evt.apiwrite.time_stamp = time_stamp;
878     evt.apiwrite.m_pt = m_pt;
879     evt.apiwrite.opt = opt;
880     avdt_scb_event(p_scb, AVDT_SCB_API_WRITE_REQ_EVT, &evt);
881   }
882 
883   AVDT_TRACE_DEBUG("%s: result=%d avdt_handle=%d", __func__, result, handle);
884   return result;
885 }
886 
887 /*******************************************************************************
888  *
889  * Function         AVDT_ConnectReq
890  *
891  * Description      This function initiates an AVDTP signaling connection
892  *                  to the peer device.  When the connection is completed, an
893  *                  AVDT_CONNECT_IND_EVT is sent to the application via its
894  *                  control callback function.  If the connection attempt fails
895  *                  an AVDT_DISCONNECT_IND_EVT is sent.  The security mask
896  *                  parameter overrides the outgoing security mask set in
897  *                  AVDT_Register().
898  *
899  * Returns          AVDT_SUCCESS if successful, otherwise error.
900  *
901  ******************************************************************************/
AVDT_ConnectReq(const RawAddress & bd_addr,uint8_t channel_index,tAVDT_CTRL_CBACK * p_cback)902 uint16_t AVDT_ConnectReq(const RawAddress& bd_addr, uint8_t channel_index,
903                          tAVDT_CTRL_CBACK* p_cback) {
904   AvdtpCcb* p_ccb = NULL;
905   uint16_t result = AVDT_SUCCESS;
906   tAVDT_CCB_EVT evt;
907 
908   AVDT_TRACE_WARNING("%s: address=%s channel_index=%d", __func__,
909                      bd_addr.ToString().c_str(), channel_index);
910 
911   /* find channel control block for this bd addr; if none, allocate one */
912   p_ccb = avdt_ccb_by_bd(bd_addr);
913   if (p_ccb == NULL) {
914     p_ccb = avdt_ccb_alloc_by_channel_index(bd_addr, channel_index);
915     if (p_ccb == NULL) {
916       /* could not allocate channel control block */
917       result = AVDT_NO_RESOURCES;
918     }
919   } else if (!p_ccb->ll_opened) {
920     AVDT_TRACE_WARNING("AVDT_ConnectReq: CCB LL is in the middle of opening");
921 
922     /* ccb was already allocated for the incoming signalling. */
923     result = AVDT_BUSY;
924   }
925 
926   if (result == AVDT_SUCCESS) {
927     /* send event to ccb */
928     evt.connect.p_cback = p_cback;
929     avdt_ccb_event(p_ccb, AVDT_CCB_API_CONNECT_REQ_EVT, &evt);
930   }
931 
932   AVDT_TRACE_WARNING("%s: address=%s result=%d", __func__,
933                      bd_addr.ToString().c_str(), result);
934 
935   return result;
936 }
937 
938 /*******************************************************************************
939  *
940  * Function         AVDT_DisconnectReq
941  *
942  * Description      This function disconnect an AVDTP signaling connection
943  *                  to the peer device.  When disconnected an
944  *                  AVDT_DISCONNECT_IND_EVT is sent to the application via its
945  *                  control callback function.
946  *
947  * Returns          AVDT_SUCCESS if successful, otherwise error.
948  *
949  ******************************************************************************/
AVDT_DisconnectReq(const RawAddress & bd_addr,tAVDT_CTRL_CBACK * p_cback)950 uint16_t AVDT_DisconnectReq(const RawAddress& bd_addr,
951                             tAVDT_CTRL_CBACK* p_cback) {
952   AvdtpCcb* p_ccb = NULL;
953   tAVDT_RESULT result = AVDT_SUCCESS;
954   tAVDT_CCB_EVT evt;
955 
956   /* find channel control block for this bd addr; if none, error */
957   p_ccb = avdt_ccb_by_bd(bd_addr);
958   if (p_ccb == NULL) {
959     LOG_ERROR("Unable to find AVDT stream endpoint peer:%s",
960               PRIVATE_ADDRESS(bd_addr));
961     result = AVDT_BAD_PARAMS;
962   } else {
963     LOG_DEBUG("Sending disconnect request to ccb peer:%s",
964               PRIVATE_ADDRESS(bd_addr));
965     evt.disconnect.p_cback = p_cback;
966     avdt_ccb_event(p_ccb, AVDT_CCB_API_DISCONNECT_REQ_EVT, &evt);
967   }
968   return static_cast<uint16_t>(result);
969 }
970 
971 /*******************************************************************************
972  *
973  * Function         AVDT_GetL2CapChannel
974  *
975  * Description      Get the L2CAP CID used by the handle.
976  *
977  * Returns          CID if successful, otherwise 0.
978  *
979  ******************************************************************************/
AVDT_GetL2CapChannel(uint8_t handle)980 uint16_t AVDT_GetL2CapChannel(uint8_t handle) {
981   AvdtpScb* p_scb;
982   AvdtpCcb* p_ccb;
983   uint8_t tcid;
984   uint16_t lcid = 0;
985 
986   /* map handle to scb */
987   if (((p_scb = avdt_scb_by_hdl(handle)) != NULL) &&
988       ((p_ccb = p_scb->p_ccb) != NULL)) {
989     /* get tcid from type, scb */
990     tcid = avdt_ad_type_to_tcid(AVDT_CHAN_MEDIA, p_scb);
991 
992     lcid = avdtp_cb.ad.rt_tbl[avdt_ccb_to_idx(p_ccb)][tcid].lcid;
993   }
994 
995   return (lcid);
996 }
997 
998 /******************************************************************************
999  *
1000  * Function         AVDT_SetTraceLevel
1001  *
1002  * Description      Sets the trace level for AVDT. If 0xff is passed, the
1003  *                  current trace level is returned.
1004  *
1005  *                  Input Parameters:
1006  *                      new_level:  The level to set the AVDT tracing to:
1007  *                      0xff-returns the current setting.
1008  *                      0-turns off tracing.
1009  *                      >= 1-Errors.
1010  *                      >= 2-Warnings.
1011  *                      >= 3-APIs.
1012  *                      >= 4-Events.
1013  *                      >= 5-Debug.
1014  *
1015  * Returns          The new trace level or current trace level if
1016  *                  the input parameter is 0xff.
1017  *
1018  *****************************************************************************/
AVDT_SetTraceLevel(uint8_t new_level)1019 uint8_t AVDT_SetTraceLevel(uint8_t new_level) {
1020   if (new_level != 0xFF) avdtp_cb.SetTraceLevel(new_level);
1021 
1022   return avdtp_cb.TraceLevel();
1023 }
1024 
stack_debug_avdtp_api_dump(int fd)1025 void stack_debug_avdtp_api_dump(int fd) {
1026   if (appl_trace_level < BT_TRACE_LEVEL_DEBUG) return;
1027 
1028   dprintf(fd, "\nAVDTP Stack State:\n");
1029   dprintf(fd, "  AVDTP signalling L2CAP channel MTU: %d\n",
1030           avdtp_cb.rcb.ctrl_mtu);
1031 
1032   for (size_t i = 0; i < AVDT_NUM_LINKS; i++) {
1033     const AvdtpCcb& ccb = avdtp_cb.ccb[i];
1034     if (ccb.peer_addr.IsEmpty()) {
1035       continue;
1036     }
1037     dprintf(fd, "\n  Channel control block: %zu peer: %s\n", i,
1038             ccb.peer_addr.ToString().c_str());
1039     dprintf(fd, "    Allocated: %s\n", ccb.allocated ? "true" : "false");
1040     dprintf(fd, "    State: %d\n", ccb.state);
1041     dprintf(fd, "    Link-layer opened: %s\n",
1042             ccb.ll_opened ? "true" : "false");
1043     dprintf(fd, "    Discover in progress: %s\n",
1044             ccb.proc_busy ? "true" : "false");
1045     dprintf(fd, "    Congested: %s\n", ccb.cong ? "true" : "false");
1046     dprintf(fd, "    Reinitiate connection on idle: %s\n",
1047             ccb.reconn ? "true" : "false");
1048     dprintf(fd, "    Command retransmission count: %d\n", ccb.ret_count);
1049     dprintf(fd, "    BTA AV SCB index: %d\n", ccb.BtaAvScbIndex());
1050 
1051     for (size_t i = 0; i < AVDT_NUM_SEPS; i++) {
1052       const AvdtpScb& scb = ccb.scb[i];
1053       if (!scb.in_use) {
1054         continue;
1055       }
1056       dprintf(fd, "\n    Stream control block: %zu\n", i);
1057       dprintf(fd, "      SEP codec: %s\n",
1058               A2DP_CodecName(scb.stream_config.cfg.codec_info));
1059       dprintf(fd, "      SEP protocol service capabilities: 0x%x\n",
1060               scb.stream_config.cfg.psc_mask);
1061       dprintf(fd, "      SEP type: 0x%x\n", scb.stream_config.tsep);
1062       dprintf(fd, "      Media type: 0x%x\n", scb.stream_config.media_type);
1063       dprintf(fd, "      MTU: %d\n", scb.stream_config.mtu);
1064       dprintf(fd, "      AVDT SCB handle: %d\n", scb.ScbHandle());
1065       dprintf(fd, "      SCB index: %d\n", scb.stream_config.scb_index);
1066       dprintf(fd, "      Configured codec: %s\n",
1067               A2DP_CodecName(scb.curr_cfg.codec_info));
1068       dprintf(fd, "      Requested codec: %s\n",
1069               A2DP_CodecName(scb.req_cfg.codec_info));
1070       dprintf(fd, "      Transport channel connect timer: %s\n",
1071               alarm_is_scheduled(scb.transport_channel_timer)
1072                   ? "Scheduled"
1073                   : "Not scheduled");
1074       dprintf(fd, "      Channel control block peer: %s\n",
1075               (scb.p_ccb != nullptr) ? scb.p_ccb->peer_addr.ToString().c_str()
1076                                      : "null");
1077       dprintf(fd, "      Allocated: %s\n", scb.allocated ? "true" : "false");
1078       dprintf(fd, "      In use: %s\n", scb.in_use ? "true" : "false");
1079       dprintf(fd, "      Role: 0x%x\n", scb.role);
1080       dprintf(fd, "      Remove: %s\n", scb.remove ? "true" : "false");
1081       dprintf(fd, "      State: %d\n", scb.state);
1082       dprintf(fd, "      Peer SEID: %d\n", scb.peer_seid);
1083       dprintf(fd, "      Current event: %d\n", scb.curr_evt);
1084       dprintf(fd, "      Congested: %s\n", scb.cong ? "true" : "false");
1085       dprintf(fd, "      Close response code: %d\n", scb.close_code);
1086     }
1087   }
1088 }
1089