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