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 <com_android_bluetooth_flags.h>
32 #include <stdio.h>
33 #include <string.h>
34 
35 #include <cstdint>
36 
37 #include "avdt_defs.h"
38 #include "avdt_int.h"
39 #include "avdtc_api.h"
40 #include "bta/include/bta_sec_api.h"
41 #include "internal_include/bt_target.h"
42 #include "os/logging/log_adapter.h"
43 #include "osi/include/alarm.h"
44 #include "stack/include/a2dp_codec_api.h"
45 #include "stack/include/bt_hdr.h"
46 #include "stack/include/l2cap_interface.h"
47 #include "types/raw_address.h"
48 
49 using namespace bluetooth;
50 
51 /* Control block for AVDTP */
52 AvdtpCb avdtp_cb;
53 
avdt_ccb_idle_ccb_timer_timeout(void * data)54 void avdt_ccb_idle_ccb_timer_timeout(void* data) {
55   AvdtpCcb* p_ccb = (AvdtpCcb*)data;
56   uint8_t avdt_event = AVDT_CCB_IDLE_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_ret_ccb_timer_timeout(void * data)64 void avdt_ccb_ret_ccb_timer_timeout(void* data) {
65   AvdtpCcb* p_ccb = (AvdtpCcb*)data;
66   uint8_t avdt_event = AVDT_CCB_RET_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_ccb_rsp_ccb_timer_timeout(void * data)74 void avdt_ccb_rsp_ccb_timer_timeout(void* data) {
75   AvdtpCcb* p_ccb = (AvdtpCcb*)data;
76   uint8_t avdt_event = AVDT_CCB_RSP_TOUT_EVT;
77   uint8_t err_code = AVDT_ERR_TIMEOUT;
78 
79   tAVDT_CCB_EVT avdt_ccb_evt;
80   avdt_ccb_evt.err_code = err_code;
81   avdt_ccb_event(p_ccb, avdt_event, &avdt_ccb_evt);
82 }
83 
avdt_scb_transport_channel_timer_timeout(void * data)84 void avdt_scb_transport_channel_timer_timeout(void* data) {
85   AvdtpScb* p_scb = (AvdtpScb*)data;
86   uint8_t avdt_event = AVDT_SCB_TC_TOUT_EVT;
87 
88   avdt_scb_event(p_scb, avdt_event, NULL);
89 }
90 
91 /*******************************************************************************
92  *
93  * Function         AVDT_Register
94  *
95  * Description      This is the system level registration function for the
96  *                  AVDTP protocol.  This function initializes AVDTP and
97  *                  prepares the protocol stack for its use.  This function
98  *                  must be called once by the system or platform using AVDTP
99  *                  before the other functions of the API an be used.
100  *
101  *
102  * Returns          void
103  *
104  ******************************************************************************/
AVDT_Register(AvdtpRcb * p_reg,tAVDT_CTRL_CBACK * p_cback)105 void AVDT_Register(AvdtpRcb* p_reg, tAVDT_CTRL_CBACK* p_cback) {
106   uint16_t sec = BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT;
107   /* register PSM with L2CAP */
108   if (!stack::l2cap::get_interface().L2CA_RegisterWithSecurity(
109               AVDT_PSM, avdt_l2c_appl, true /* enable_snoop */, nullptr, kAvdtpMtu, 0, sec)) {
110     log::error("Unable to register with L2CAP profile AVDT psm:AVDT_PSM[0x0019]");
111   }
112 
113   /* initialize AVDTP data structures */
114   avdt_scb_init();
115   avdt_ccb_init();
116   avdt_ad_init();
117 
118   /* copy registration struct */
119   avdtp_cb.rcb = *p_reg;
120   avdtp_cb.p_conn_cback = p_cback;
121 }
122 
123 /*******************************************************************************
124  *
125  * Function         AVDT_Deregister
126  *
127  * Description      This function is called to deregister use AVDTP protocol.
128  *                  It is called when AVDTP is no longer being used by any
129  *                  application in the system.  Before this function can be
130  *                  called, all streams must be removed with
131  *                  AVDT_RemoveStream().
132  *
133  *
134  * Returns          void
135  *
136  ******************************************************************************/
AVDT_Deregister(void)137 void AVDT_Deregister(void) {
138   /* deregister PSM with L2CAP */
139   stack::l2cap::get_interface().L2CA_Deregister(AVDT_PSM);
140 }
141 
AVDT_AbortReq(uint8_t handle)142 void AVDT_AbortReq(uint8_t handle) {
143   log::warn("avdt_handle={}", handle);
144 
145   AvdtpScb* p_scb = avdt_scb_by_hdl(handle);
146   if (p_scb != NULL) {
147     avdt_scb_event(p_scb, AVDT_SCB_API_ABORT_REQ_EVT, NULL);
148   } else {
149     log::error("Improper avdp_handle={}, can not abort the stream", handle);
150   }
151 }
152 
153 /*******************************************************************************
154  *
155  * Function         AVDT_CreateStream
156  *
157  * Description      Create a stream endpoint.  After a stream endpoint is
158  *                  created an application can initiate a connection between
159  *                  this endpoint and an endpoint on a peer device.  In
160  *                  addition, a peer device can discover, get the capabilities,
161  *                  and connect to this endpoint.
162  *
163  *
164  * Returns          AVDT_SUCCESS if successful, otherwise error.
165  *
166  ******************************************************************************/
AVDT_CreateStream(uint8_t peer_id,uint8_t * p_handle,const AvdtpStreamConfig & avdtp_stream_config)167 uint16_t AVDT_CreateStream(uint8_t peer_id, uint8_t* p_handle,
168                            const AvdtpStreamConfig& avdtp_stream_config) {
169   tAVDT_RESULT result = AVDT_SUCCESS;
170   AvdtpScb* p_scb;
171 
172   /* Verify parameters; if invalid, return failure */
173   if (((avdtp_stream_config.cfg.psc_mask & (~AVDT_PSC)) != 0) ||
174       (avdtp_stream_config.p_avdt_ctrl_cback == NULL)) {
175     result = AVDT_BAD_PARAMS;
176     log::error("Invalid AVDT stream endpoint parameters peer_id={} scb_index={}", peer_id,
177                avdtp_stream_config.scb_index);
178   } else {
179     /* Allocate scb; if no scbs, return failure */
180     p_scb = avdt_scb_alloc(peer_id, avdtp_stream_config);
181     if (p_scb == NULL) {
182       log::error("Unable to create AVDT stream endpoint peer_id={} scb_index={}", peer_id,
183                  avdtp_stream_config.scb_index);
184       result = AVDT_NO_RESOURCES;
185     } else {
186       *p_handle = avdt_scb_to_hdl(p_scb);
187       log::debug("Created stream endpoint peer_id={} handle={}", peer_id, *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, tAVDT_CTRL_CBACK* p_cback) {
257   AvdtpCcb* p_ccb;
258   uint16_t result = AVDT_SUCCESS;
259   tAVDT_CCB_EVT evt;
260 
261   log::info("bd_addr={} channel_index={}", bd_addr, channel_index);
262 
263   /* find channel control block for this bd addr; if none, allocate one */
264   p_ccb = avdt_ccb_by_bd(bd_addr);
265   if (p_ccb == NULL) {
266     p_ccb = avdt_ccb_alloc_by_channel_index(bd_addr, channel_index);
267     if (p_ccb == NULL) {
268       /* could not allocate channel control block */
269       result = AVDT_NO_RESOURCES;
270     }
271   }
272 
273   if (result == AVDT_SUCCESS) {
274     /* make sure no discovery or get capabilities req already in progress */
275     if (p_ccb->proc_busy) {
276       result = AVDT_BUSY;
277     } else {
278       /* send event to ccb */
279       evt.discover.p_sep_info = p_sep_info;
280       evt.discover.num_seps = max_seps;
281       evt.discover.p_cback = p_cback;
282       avdt_ccb_event(p_ccb, AVDT_CCB_API_DISCOVER_REQ_EVT, &evt);
283     }
284   }
285 
286   if (result != AVDT_SUCCESS) {
287     log::error("result={} address={}", result, bd_addr);
288   }
289   return result;
290 }
291 
292 /*******************************************************************************
293  *
294  * Function         avdt_get_cap_req
295  *
296  * Description      internal function to serve AVDT_GetCapReq
297  *
298  * Returns          AVDT_SUCCESS if successful, otherwise error.
299  *
300  ******************************************************************************/
avdt_get_cap_req(const RawAddress & bd_addr,uint8_t channel_index,tAVDT_CCB_API_GETCAP * p_evt)301 static uint16_t avdt_get_cap_req(const RawAddress& bd_addr, uint8_t channel_index,
302                                  tAVDT_CCB_API_GETCAP* p_evt) {
303   AvdtpCcb* p_ccb = NULL;
304   uint16_t result = AVDT_SUCCESS;
305 
306   /* verify SEID */
307   if ((p_evt->single.seid < AVDT_SEID_MIN) || (p_evt->single.seid > AVDT_SEID_MAX)) {
308     log::error("seid: {}", p_evt->single.seid);
309     result = AVDT_BAD_PARAMS;
310   } else {
311     /* find channel control block for this bd addr; if none, allocate one */
312     p_ccb = avdt_ccb_by_bd(bd_addr);
313     if (p_ccb == NULL) {
314       p_ccb = avdt_ccb_alloc_by_channel_index(bd_addr, channel_index);
315       if (p_ccb == NULL) {
316         /* could not allocate channel control block */
317         result = AVDT_NO_RESOURCES;
318       }
319     }
320   }
321 
322   if (result == AVDT_SUCCESS) {
323     /* make sure no discovery or get capabilities req already in progress */
324     if (p_ccb->proc_busy) {
325       result = AVDT_BUSY;
326     } else {
327       /* send event to ccb */
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     log::error("result={} address={}", result, bd_addr);
334   }
335   return result;
336 }
337 
338 /*******************************************************************************
339  *
340  * Function         AVDT_GetCapReq
341  *
342  * Description      This function initiates a connection to the AVDTP service
343  *                  on the peer device, if not already present, and gets the
344  *                  capabilities of a stream endpoint on the peer device.
345  *                  This function can be called at any time regardless of
346  *                  whether there is an AVDTP connection to the peer device.
347  *
348  *                  When the procedure is complete, an AVDT_GETCAP_CFM_EVT is
349  *                  sent to the application via its callback function.  The
350  *                  application must not call AVDT_GetCapReq() or
351  *                  AVDT_DiscoverReq() again until the procedure is complete.
352  *
353  *                  The memory pointed to by p_cfg is allocated by the
354  *                  application.  This memory is written to by AVDTP as part
355  *                  of the get capabilities procedure.  This memory must
356  *                  remain accessible until the application receives
357  *                  the AVDT_GETCAP_CFM_EVT.
358  *
359  * Returns          AVDT_SUCCESS if successful, otherwise error.
360  *
361  ******************************************************************************/
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)362 uint16_t AVDT_GetCapReq(const RawAddress& bd_addr, uint8_t channel_index, uint8_t seid,
363                         AvdtpSepConfig* p_cfg, tAVDT_CTRL_CBACK* p_cback, bool get_all_cap) {
364   tAVDT_CCB_API_GETCAP getcap;
365   uint16_t result = AVDT_SUCCESS;
366 
367   log::info("bd_addr={} channel_index={} seid=0x{:x} get_all_capabilities={}", bd_addr,
368             channel_index, seid, get_all_cap);
369 
370   getcap.single.seid = seid;
371   if (get_all_cap) {
372     getcap.single.sig_id = AVDT_SIG_GET_ALLCAP;
373   } else {
374     getcap.single.sig_id = AVDT_SIG_GETCAP;
375   }
376   getcap.p_cfg = p_cfg;
377   getcap.p_cback = p_cback;
378   result = avdt_get_cap_req(bd_addr, channel_index, &getcap);
379 
380   if (result != AVDT_SUCCESS) {
381     log::error("result={} address={}", result, bd_addr);
382   }
383   return result;
384 }
385 
386 /*******************************************************************************
387  *
388  * Function         AVDT_DelayReport
389  *
390  * Description      This functions sends a Delay Report to the peer device
391  *                  that is associated with a particular SEID.
392  *                  This function is called by SNK device.
393  *
394  * Returns          AVDT_SUCCESS if successful, otherwise error.
395  *
396  ******************************************************************************/
AVDT_DelayReport(uint8_t handle,uint8_t seid,uint16_t delay)397 uint16_t AVDT_DelayReport(uint8_t handle, uint8_t seid, uint16_t delay) {
398   AvdtpScb* p_scb;
399   uint16_t result = AVDT_SUCCESS;
400   tAVDT_SCB_EVT evt;
401 
402   log::info("avdt_handle={} seid={} delay={}", handle, seid, delay);
403 
404   /* map handle to scb */
405   p_scb = avdt_scb_by_hdl(handle);
406   if (p_scb == NULL) {
407     result = AVDT_BAD_HANDLE;
408   } else
409   /* send event to scb */
410   {
411     evt.apidelay.hdr.seid = seid;
412     evt.apidelay.delay = delay;
413     avdt_scb_event(p_scb, AVDT_SCB_API_DELAY_RPT_REQ_EVT, &evt);
414   }
415 
416   if (result != AVDT_SUCCESS) {
417     log::error("result={} avdt_handle={} seid={}", result, handle, seid);
418   }
419   return result;
420 }
421 
422 /*******************************************************************************
423  *
424  * Function         AVDT_OpenReq
425  *
426  * Description      This function initiates a connection to the AVDTP service
427  *                  on the peer device, if not already present, and connects
428  *                  to a stream endpoint on a peer device.  When the connection
429  *                  is completed, an AVDT_OPEN_CFM_EVT is sent to the
430  *                  application via the control callback function for this
431  *                  handle.
432  *
433  * Returns          AVDT_SUCCESS if successful, otherwise error.
434  *
435  ******************************************************************************/
AVDT_OpenReq(uint8_t handle,const RawAddress & bd_addr,uint8_t channel_index,uint8_t seid,AvdtpSepConfig * p_cfg)436 uint16_t AVDT_OpenReq(uint8_t handle, const RawAddress& bd_addr, uint8_t channel_index,
437                       uint8_t seid, AvdtpSepConfig* p_cfg) {
438   AvdtpCcb* p_ccb = NULL;
439   AvdtpScb* p_scb = NULL;
440   uint16_t result = AVDT_SUCCESS;
441   tAVDT_SCB_EVT evt;
442 
443   log::info("bd_addr={} avdt_handle={} seid=0x{:x}", bd_addr, handle, seid);
444 
445   /* verify SEID */
446   if ((seid < AVDT_SEID_MIN) || (seid > AVDT_SEID_MAX)) {
447     result = AVDT_BAD_PARAMS;
448   } else {
449     /* map handle to scb */
450     p_scb = avdt_scb_by_hdl(handle);
451     if (p_scb == NULL) {
452       result = AVDT_BAD_HANDLE;
453     } else {
454       /* find channel control block for this bd addr; if none, allocate one */
455       p_ccb = avdt_ccb_by_bd(bd_addr);
456       if (p_ccb == NULL) {
457         p_ccb = avdt_ccb_alloc_by_channel_index(bd_addr, channel_index);
458         if (p_ccb == NULL) {
459           /* could not allocate channel control block */
460           result = AVDT_NO_RESOURCES;
461         }
462       }
463     }
464   }
465 
466   /* send event to scb */
467   if (result == AVDT_SUCCESS) {
468     log::verbose("codec: {}", A2DP_CodecInfoString(p_cfg->codec_info));
469 
470     evt.msg.config_cmd.hdr.seid = seid;
471     evt.msg.config_cmd.hdr.ccb_idx = avdt_ccb_to_idx(p_ccb);
472     evt.msg.config_cmd.int_seid = handle;
473     evt.msg.config_cmd.p_cfg = p_cfg;
474     avdt_scb_event(p_scb, AVDT_SCB_API_SETCONFIG_REQ_EVT, &evt);
475   } else {
476     log::error("result={} address={} avdt_handle={}", result, bd_addr, handle);
477   }
478 
479   return result;
480 }
481 
482 /*******************************************************************************
483  *
484  * Function         AVDT_ConfigRsp
485  *
486  * Description      Respond to a configure request from the peer device.  This
487  *                  function must be called if the application receives an
488  *                  AVDT_CONFIG_IND_EVT through its control callback.
489  *
490  *
491  * Returns          AVDT_SUCCESS if successful, otherwise error.
492  *
493  ******************************************************************************/
AVDT_ConfigRsp(uint8_t handle,uint8_t label,uint8_t error_code,uint8_t category)494 uint16_t AVDT_ConfigRsp(uint8_t handle, uint8_t label, uint8_t error_code, uint8_t category) {
495   AvdtpScb* p_scb;
496   tAVDT_SCB_EVT evt;
497   uint16_t result = AVDT_SUCCESS;
498   uint8_t event_code;
499 
500   log::info("avdt_handle={} label={} error_code=0x{:x} category={}", handle, label, error_code,
501             category);
502 
503   /* map handle to scb */
504   p_scb = avdt_scb_by_hdl(handle);
505   if (p_scb == NULL) {
506     result = AVDT_BAD_HANDLE;
507   } else if (!p_scb->in_use) {
508     /* handle special case when this function is called but peer has not send
509      * a configuration cmd; ignore and return error result */
510     result = AVDT_BAD_HANDLE;
511   } else {
512     /* send event to scb */
513     evt.msg.hdr.err_code = error_code;
514     evt.msg.hdr.err_param = category;
515     evt.msg.hdr.label = label;
516     if (error_code == 0) {
517       event_code = AVDT_SCB_API_SETCONFIG_RSP_EVT;
518     } else {
519       event_code = AVDT_SCB_API_SETCONFIG_REJ_EVT;
520     }
521     avdt_scb_event(p_scb, event_code, &evt);
522   }
523 
524   if (result != AVDT_SUCCESS) {
525     log::error("result={} avdt_handle={}", result, handle);
526   }
527   return result;
528 }
529 
530 /*******************************************************************************
531  *
532  * Function         AVDT_StartReq
533  *
534  * Description      Start one or more stream endpoints.  This initiates the
535  *                  transfer of media packets for the streams.  All stream
536  *                  endpoints must previously be opened.  When the streams
537  *                  are started, an AVDT_START_CFM_EVT is sent to the
538  *                  application via the control callback function for each
539  *                  stream.
540  *
541  *
542  * Returns          AVDT_SUCCESS if successful, otherwise error.
543  *
544  ******************************************************************************/
AVDT_StartReq(uint8_t * p_handles,uint8_t num_handles)545 uint16_t AVDT_StartReq(uint8_t* p_handles, uint8_t num_handles) {
546   AvdtpScb* p_scb = NULL;
547   tAVDT_CCB_EVT evt;
548   uint16_t result = AVDT_SUCCESS;
549   int i;
550 
551   log::info("num_handles={}", num_handles);
552 
553   if ((num_handles == 0) || (num_handles > AVDT_NUM_SEPS)) {
554     result = AVDT_BAD_PARAMS;
555   } else {
556     /* verify handles */
557     for (i = 0; i < num_handles; i++) {
558       p_scb = avdt_scb_by_hdl(p_handles[i]);
559       if (p_scb == NULL) {
560         result = AVDT_BAD_HANDLE;
561         break;
562       }
563     }
564   }
565 
566   if (result == AVDT_SUCCESS) {
567     if (p_scb->p_ccb == NULL) {
568       result = AVDT_BAD_HANDLE;
569     } else {
570       /* send event to ccb */
571       memcpy(evt.msg.multi.seid_list, p_handles, num_handles);
572       evt.msg.multi.num_seps = num_handles;
573       avdt_ccb_event(p_scb->p_ccb, AVDT_CCB_API_START_REQ_EVT, &evt);
574     }
575   }
576 
577   if (result != AVDT_SUCCESS) {
578     if ((num_handles == 0) || (num_handles > AVDT_NUM_SEPS)) {
579       log::error("result={} num_handles={} invalid", result, num_handles);
580     } else {
581       log::error("result={} avdt_handle={}", result,
582                  i < num_handles ? p_handles[i] : p_handles[num_handles - 1]);
583     }
584   }
585   return result;
586 }
587 
588 /*******************************************************************************
589  *
590  * Function         AVDT_SuspendReq
591  *
592  * Description      Suspend one or more stream endpoints. This suspends the
593  *                  transfer of media packets for the streams.  All stream
594  *                  endpoints must previously be open and started.  When the
595  *                  streams are suspended, an AVDT_SUSPEND_CFM_EVT is sent to
596  *                  the application via the control callback function for
597  *                  each stream.
598  *
599  *
600  * Returns          AVDT_SUCCESS if successful, otherwise error.
601  *
602  ******************************************************************************/
AVDT_SuspendReq(uint8_t * p_handles,uint8_t num_handles)603 uint16_t AVDT_SuspendReq(uint8_t* p_handles, uint8_t num_handles) {
604   AvdtpScb* p_scb = NULL;
605   tAVDT_CCB_EVT evt;
606   uint16_t result = AVDT_SUCCESS;
607   int i;
608 
609   log::info("num_handles={}", num_handles);
610 
611   if ((num_handles == 0) || (num_handles > AVDT_NUM_SEPS)) {
612     result = AVDT_BAD_PARAMS;
613   } else {
614     /* verify handles */
615     for (i = 0; i < num_handles; i++) {
616       p_scb = avdt_scb_by_hdl(p_handles[i]);
617       if (p_scb == NULL) {
618         result = AVDT_BAD_HANDLE;
619         break;
620       }
621     }
622   }
623 
624   if (result == AVDT_SUCCESS) {
625     if (p_scb->p_ccb == NULL) {
626       result = AVDT_BAD_HANDLE;
627     } else {
628       /* send event to ccb */
629       memcpy(evt.msg.multi.seid_list, p_handles, num_handles);
630       evt.msg.multi.num_seps = num_handles;
631       avdt_ccb_event(p_scb->p_ccb, AVDT_CCB_API_SUSPEND_REQ_EVT, &evt);
632     }
633   }
634 
635   if (result != AVDT_SUCCESS) {
636     if ((num_handles == 0) || (num_handles > AVDT_NUM_SEPS)) {
637       log::error("result={} num_handles={} invalid", result, num_handles);
638     } else {
639       log::error("result={} avdt_handle={}", result,
640                  i < num_handles ? p_handles[i] : p_handles[num_handles - 1]);
641     }
642   }
643   return result;
644 }
645 
646 /*******************************************************************************
647  *
648  * Function         AVDT_CloseReq
649  *
650  * Description      Close a stream endpoint.  This stops the transfer of media
651  *                  packets and closes the transport channel associated with
652  *                  this stream endpoint.  When the stream is closed, an
653  *                  AVDT_CLOSE_CFM_EVT is sent to the application via the
654  *                  control callback function for this handle.
655  *
656  *
657  * Returns          AVDT_SUCCESS if successful, otherwise error.
658  *
659  ******************************************************************************/
AVDT_CloseReq(uint8_t handle)660 uint16_t AVDT_CloseReq(uint8_t handle) {
661   AvdtpScb* p_scb;
662   uint16_t result = AVDT_SUCCESS;
663 
664   log::info("avdt_handle={}", handle);
665 
666   /* map handle to scb */
667   p_scb = avdt_scb_by_hdl(handle);
668   if (p_scb == NULL) {
669     result = AVDT_BAD_HANDLE;
670   } else
671   /* send event to scb */
672   {
673     avdt_scb_event(p_scb, AVDT_SCB_API_CLOSE_REQ_EVT, NULL);
674   }
675 
676   if (result != AVDT_SUCCESS) {
677     log::error("result={} avdt_handle={}", result, handle);
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   log::info("avdt_handle={}", 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   } else {
711     /* send event to scb */
712     /* force psc_mask to zero */
713     p_cfg->psc_mask = 0;
714     evt.msg.reconfig_cmd.p_cfg = p_cfg;
715     avdt_scb_event(p_scb, AVDT_SCB_API_RECONFIG_REQ_EVT, &evt);
716   }
717 
718   if (result != AVDT_SUCCESS) {
719     log::error("result={} avdt_handle={}", result, handle);
720   }
721   return result;
722 }
723 
724 /*******************************************************************************
725  *
726  * Function         AVDT_SecurityReq
727  *
728  * Description      Send a security request to the peer device.  When the
729  *                  security procedure is completed, an AVDT_SECURITY_CFM_EVT
730  *                  is sent to the application via the control callback function
731  *                  for this handle.  (Please note that AVDTP security
732  *                  procedures are unrelated to Bluetooth link level security.)
733  *
734  *
735  * Returns          AVDT_SUCCESS if successful, otherwise error.
736  *
737  ******************************************************************************/
AVDT_SecurityReq(uint8_t handle,uint8_t * p_data,uint16_t len)738 uint16_t AVDT_SecurityReq(uint8_t handle, uint8_t* p_data, uint16_t len) {
739   AvdtpScb* p_scb;
740   uint16_t result = AVDT_SUCCESS;
741   tAVDT_SCB_EVT evt;
742 
743   log::info("avdt_handle={} len={}", handle, len);
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   } else {
750     /* send event to scb */
751     evt.msg.security_rsp.p_data = p_data;
752     evt.msg.security_rsp.len = len;
753     avdt_scb_event(p_scb, AVDT_SCB_API_SECURITY_REQ_EVT, &evt);
754   }
755 
756   if (result != AVDT_SUCCESS) {
757     log::error("result={} avdt_handle={}", result, handle);
758   }
759   return result;
760 }
761 
762 /*******************************************************************************
763  *
764  * Function         AVDT_SecurityRsp
765  *
766  * Description      Respond to a security request from the peer device.
767  *                  This function must be called if the application receives
768  *                  an AVDT_SECURITY_IND_EVT through its control callback.
769  *                  (Please note that AVDTP security procedures are unrelated
770  *                  to Bluetooth link level security.)
771  *
772  *
773  * Returns          AVDT_SUCCESS if successful, otherwise error.
774  *
775  ******************************************************************************/
AVDT_SecurityRsp(uint8_t handle,uint8_t label,uint8_t error_code,uint8_t * p_data,uint16_t len)776 uint16_t AVDT_SecurityRsp(uint8_t handle, uint8_t label, uint8_t error_code, uint8_t* p_data,
777                           uint16_t len) {
778   AvdtpScb* p_scb;
779   uint16_t result = AVDT_SUCCESS;
780   tAVDT_SCB_EVT evt;
781 
782   log::info("avdt_handle={} label={} error_code=0x{:x} len={}", handle, label, error_code, 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   } else {
789     /* send event to scb */
790     evt.msg.security_rsp.hdr.err_code = error_code;
791     evt.msg.security_rsp.hdr.label = label;
792     evt.msg.security_rsp.p_data = p_data;
793     evt.msg.security_rsp.len = len;
794     avdt_scb_event(p_scb, AVDT_SCB_API_SECURITY_RSP_EVT, &evt);
795   }
796 
797   if (result != AVDT_SUCCESS) {
798     log::error("result={} avdt_handle={}", result, handle);
799   }
800   return result;
801 }
802 
803 /*******************************************************************************
804  *
805  * Function         AVDT_WriteReqOpt
806  *
807  * Description      Send a media packet to the peer device.  The stream must
808  *                  be started before this function is called.  Also, this
809  *                  function can only be called if the stream is a SRC.
810  *
811  *                  When AVDTP has sent the media packet and is ready for the
812  *                  next packet, an AVDT_WRITE_CFM_EVT is sent to the
813  *                  application via the control callback.  The application must
814  *                  wait for the AVDT_WRITE_CFM_EVT before it makes the next
815  *                  call to AVDT_WriteReq().  If the applications calls
816  *                  AVDT_WriteReq() before it receives the event the packet
817  *                  will not be sent.  The application may make its first call
818  *                  to AVDT_WriteReq() after it receives an AVDT_START_CFM_EVT
819  *                  or AVDT_START_IND_EVT.
820  *
821  *                  The application passes the packet using the BT_HDR
822  *                  structure.
823  *                  This structure is described in section 2.1.  The offset
824  *                  field must be equal to or greater than AVDT_MEDIA_OFFSET
825  *                  (if NO_RTP is specified, L2CAP_MIN_OFFSET can be used).
826  *                  This allows enough space in the buffer for the L2CAP and
827  *                  AVDTP headers.
828  *
829  *                  The memory pointed to by p_pkt must be a GKI buffer
830  *                  allocated by the application.  This buffer will be freed
831  *                  by the protocol stack; the application must not free
832  *                  this buffer.
833  *
834  *                  The opt parameter allows passing specific options like:
835  *                  - NO_RTP : do not add the RTP header to buffer
836  *
837  * Returns          AVDT_SUCCESS if successful, otherwise error.
838  *
839  ******************************************************************************/
AVDT_WriteReqOpt(uint8_t handle,BT_HDR * p_pkt,uint32_t time_stamp,uint8_t m_pt,tAVDT_DATA_OPT_MASK opt)840 uint16_t AVDT_WriteReqOpt(uint8_t handle, BT_HDR* p_pkt, uint32_t time_stamp, uint8_t m_pt,
841                           tAVDT_DATA_OPT_MASK opt) {
842   AvdtpScb* p_scb;
843   tAVDT_SCB_EVT evt;
844   uint16_t result = AVDT_SUCCESS;
845 
846   log::verbose("avdt_handle={} timestamp={} m_pt=0x{:x} opt=0x{:x}", handle, time_stamp, m_pt, opt);
847 
848   /* map handle to scb */
849   p_scb = avdt_scb_by_hdl(handle);
850   if (p_scb == NULL) {
851     result = AVDT_BAD_HANDLE;
852   } else {
853     evt.apiwrite.p_buf = p_pkt;
854     evt.apiwrite.time_stamp = time_stamp;
855     evt.apiwrite.m_pt = m_pt;
856     evt.apiwrite.opt = opt;
857     avdt_scb_event(p_scb, AVDT_SCB_API_WRITE_REQ_EVT, &evt);
858   }
859 
860   log::verbose("result={} avdt_handle={}", result, handle);
861   return result;
862 }
863 
864 /*******************************************************************************
865  *
866  * Function         AVDT_ConnectReq
867  *
868  * Description      This function initiates an AVDTP signaling connection
869  *                  to the peer device.  When the connection is completed, an
870  *                  AVDT_CONNECT_IND_EVT is sent to the application via its
871  *                  control callback function.  If the connection attempt fails
872  *                  an AVDT_DISCONNECT_IND_EVT is sent.  The security mask
873  *                  parameter overrides the outgoing security mask set in
874  *                  AVDT_Register().
875  *
876  * Returns          AVDT_SUCCESS if successful, otherwise error.
877  *
878  ******************************************************************************/
AVDT_ConnectReq(const RawAddress & bd_addr,uint8_t channel_index,tAVDT_CTRL_CBACK * p_cback)879 uint16_t AVDT_ConnectReq(const RawAddress& bd_addr, uint8_t channel_index,
880                          tAVDT_CTRL_CBACK* p_cback) {
881   AvdtpCcb* p_ccb = NULL;
882   uint16_t result = AVDT_SUCCESS;
883   tAVDT_CCB_EVT evt;
884 
885   log::info("bd_addr={} channel_index={}", bd_addr, channel_index);
886 
887   /* find channel control block for this bd addr; if none, allocate one */
888   p_ccb = avdt_ccb_by_bd(bd_addr);
889   if (p_ccb == NULL) {
890     p_ccb = avdt_ccb_alloc_by_channel_index(bd_addr, channel_index);
891     if (p_ccb == NULL) {
892       /* could not allocate channel control block */
893       result = AVDT_NO_RESOURCES;
894     }
895   } else if (!p_ccb->ll_opened) {
896     log::warn("CCB LL is in the middle of opening");
897 
898     /* ccb was already allocated for the incoming signalling. */
899     result = AVDT_BUSY;
900   }
901 
902   if (result == AVDT_SUCCESS) {
903     /* send event to ccb */
904     evt.connect.p_cback = p_cback;
905     avdt_ccb_event(p_ccb, AVDT_CCB_API_CONNECT_REQ_EVT, &evt);
906   }
907 
908   log::info("completed; bd_addr={} result={}", bd_addr, result);
909 
910   return result;
911 }
912 
913 /*******************************************************************************
914  *
915  * Function         AVDT_DisconnectReq
916  *
917  * Description      This function disconnect an AVDTP signaling connection
918  *                  to the peer device.  When disconnected an
919  *                  AVDT_DISCONNECT_IND_EVT is sent to the application via its
920  *                  control callback function.
921  *
922  * Returns          AVDT_SUCCESS if successful, otherwise error.
923  *
924  ******************************************************************************/
AVDT_DisconnectReq(const RawAddress & bd_addr,tAVDT_CTRL_CBACK * p_cback)925 uint16_t AVDT_DisconnectReq(const RawAddress& bd_addr, tAVDT_CTRL_CBACK* p_cback) {
926   AvdtpCcb* p_ccb = NULL;
927   tAVDT_RESULT result = AVDT_SUCCESS;
928   tAVDT_CCB_EVT evt;
929 
930   log::info("bd_addr={}", bd_addr);
931 
932   /* find channel control block for this bd addr; if none, error */
933   p_ccb = avdt_ccb_by_bd(bd_addr);
934   if (p_ccb == NULL) {
935     log::error("Unable to find AVDT stream endpoint peer:{}", bd_addr);
936     result = AVDT_BAD_PARAMS;
937   } else {
938     log::debug("Sending disconnect request to ccb peer:{}", bd_addr);
939     evt.disconnect.p_cback = p_cback;
940     avdt_ccb_event(p_ccb, AVDT_CCB_API_DISCONNECT_REQ_EVT, &evt);
941   }
942   return static_cast<uint16_t>(result);
943 }
944 
945 /*******************************************************************************
946  *
947  * Function         AVDT_GetL2CapChannel
948  *
949  * Description      Get the L2CAP CID used by the handle.
950  *
951  * Returns          CID if successful, otherwise 0.
952  *
953  ******************************************************************************/
AVDT_GetL2CapChannel(uint8_t handle)954 uint16_t AVDT_GetL2CapChannel(uint8_t handle) {
955   AvdtpScb* p_scb;
956   AvdtpCcb* p_ccb;
957   uint8_t tcid;
958   uint16_t lcid = 0;
959 
960   /* map handle to scb */
961   if (((p_scb = avdt_scb_by_hdl(handle)) != NULL) && ((p_ccb = p_scb->p_ccb) != NULL)) {
962     /* get tcid from type, scb */
963     tcid = avdt_ad_type_to_tcid(AVDT_CHAN_MEDIA, p_scb);
964 
965     lcid = avdtp_cb.ad.rt_tbl[avdt_ccb_to_idx(p_ccb)][tcid].lcid;
966   }
967 
968   return lcid;
969 }
970 
stack_debug_avdtp_api_dump(int fd)971 void stack_debug_avdtp_api_dump(int fd) {
972   dprintf(fd, "\nAVDTP Stack State:\n");
973   dprintf(fd, "  AVDTP signalling L2CAP channel MTU: %d\n", avdtp_cb.rcb.ctrl_mtu);
974 
975   for (size_t i = 0; i < AVDT_NUM_LINKS; i++) {
976     const AvdtpCcb& ccb = avdtp_cb.ccb[i];
977     if (ccb.peer_addr.IsEmpty()) {
978       continue;
979     }
980     dprintf(fd, "\n  Channel control block: %zu peer: %s\n", i,
981             ADDRESS_TO_LOGGABLE_CSTR(ccb.peer_addr));
982     dprintf(fd, "    Allocated: %s\n", ccb.allocated ? "true" : "false");
983     dprintf(fd, "    State: %d\n", ccb.state);
984     dprintf(fd, "    Link-layer opened: %s\n", ccb.ll_opened ? "true" : "false");
985     dprintf(fd, "    Discover in progress: %s\n", ccb.proc_busy ? "true" : "false");
986     dprintf(fd, "    Congested: %s\n", ccb.cong ? "true" : "false");
987     dprintf(fd, "    Reinitiate connection on idle: %s\n", ccb.reconn ? "true" : "false");
988     dprintf(fd, "    Command retransmission count: %d\n", ccb.ret_count);
989     dprintf(fd, "    BTA AV SCB index: %d\n", ccb.BtaAvScbIndex());
990 
991     for (size_t i = 0; i < AVDT_NUM_SEPS; i++) {
992       const AvdtpScb& scb = ccb.scb[i];
993       if (!scb.in_use) {
994         continue;
995       }
996       dprintf(fd, "\n    Stream control block: %zu\n", i);
997       dprintf(fd, "      SEP codec: %s\n", A2DP_CodecName(scb.stream_config.cfg.codec_info));
998       dprintf(fd, "      SEP protocol service capabilities: 0x%x\n",
999               scb.stream_config.cfg.psc_mask);
1000       dprintf(fd, "      SEP type: 0x%x\n", scb.stream_config.tsep);
1001       dprintf(fd, "      Media type: 0x%x\n", scb.stream_config.media_type);
1002       dprintf(fd, "      MTU: %d\n", scb.stream_config.mtu);
1003       dprintf(fd, "      AVDT SCB handle: %d\n", scb.ScbHandle());
1004       dprintf(fd, "      SCB index: %d\n", scb.stream_config.scb_index);
1005       dprintf(fd, "      Configured codec: %s\n", A2DP_CodecName(scb.curr_cfg.codec_info));
1006       dprintf(fd, "      Requested codec: %s\n", A2DP_CodecName(scb.req_cfg.codec_info));
1007       dprintf(fd, "      Transport channel connect timer: %s\n",
1008               alarm_is_scheduled(scb.transport_channel_timer) ? "Scheduled" : "Not scheduled");
1009       dprintf(fd, "      Channel control block peer: %s\n",
1010               (scb.p_ccb != nullptr) ? ADDRESS_TO_LOGGABLE_CSTR(scb.p_ccb->peer_addr) : "null");
1011       dprintf(fd, "      Allocated: %s\n", scb.allocated ? "true" : "false");
1012       dprintf(fd, "      In use: %s\n", scb.in_use ? "true" : "false");
1013       dprintf(fd, "      Role: 0x%x\n", scb.role);
1014       dprintf(fd, "      Remove: %s\n", scb.remove ? "true" : "false");
1015       dprintf(fd, "      State: %d\n", scb.state);
1016       dprintf(fd, "      Peer SEID: %d\n", scb.peer_seid);
1017       dprintf(fd, "      Current event: %d\n", scb.curr_evt);
1018       dprintf(fd, "      Congested: %s\n", scb.cong ? "true" : "false");
1019       dprintf(fd, "      Close response code: %d\n", scb.close_code);
1020     }
1021   }
1022 }
1023