1 /******************************************************************************
2  *
3  *  Copyright (C) 2016 The Android Open Source Project
4  *  Copyright (C) 2009-2012 Broadcom Corporation
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at:
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  ******************************************************************************/
19 
20 #define LOG_TAG "bt_btif_a2dp"
21 
22 #include <stdbool.h>
23 
24 #include "audio_a2dp_hw/include/audio_a2dp_hw.h"
25 #include "bt_common.h"
26 #include "bta_av_api.h"
27 #include "btif_a2dp.h"
28 #include "btif_a2dp_control.h"
29 #include "btif_a2dp_sink.h"
30 #include "btif_a2dp_source.h"
31 #include "btif_av.h"
32 #include "btif_util.h"
33 #include "osi/include/log.h"
34 
btif_a2dp_on_idle(void)35 void btif_a2dp_on_idle(void) {
36   APPL_TRACE_EVENT("## ON A2DP IDLE ## peer_sep = %d", btif_av_get_peer_sep());
37   if (btif_av_get_peer_sep() == AVDT_TSEP_SNK) {
38     btif_a2dp_source_on_idle();
39   } else if (btif_av_get_peer_sep() == AVDT_TSEP_SRC) {
40     btif_a2dp_sink_on_idle();
41   }
42 }
43 
btif_a2dp_on_started(tBTA_AV_START * p_av_start,bool pending_start)44 bool btif_a2dp_on_started(tBTA_AV_START* p_av_start, bool pending_start) {
45   bool ack = false;
46 
47   APPL_TRACE_EVENT("## ON A2DP STARTED ##");
48 
49   if (p_av_start == NULL) {
50     /* ack back a local start request */
51     btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
52     return true;
53   }
54 
55   if (p_av_start->status == BTA_AV_SUCCESS) {
56     if (!p_av_start->suspending) {
57       if (p_av_start->initiator) {
58         if (pending_start) {
59           btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
60           ack = true;
61         }
62       } else {
63         /* We were remotely started, make sure codec
64          * is setup before datapath is started.
65          */
66         btif_a2dp_source_setup_codec();
67       }
68 
69       /* media task is autostarted upon a2dp audiopath connection */
70     }
71   } else if (pending_start) {
72     APPL_TRACE_WARNING("%s: A2DP start request failed: status = %d", __func__,
73                        p_av_start->status);
74     btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE);
75     ack = true;
76   }
77   return ack;
78 }
79 
btif_a2dp_on_stopped(tBTA_AV_SUSPEND * p_av_suspend)80 void btif_a2dp_on_stopped(tBTA_AV_SUSPEND* p_av_suspend) {
81   APPL_TRACE_EVENT("## ON A2DP STOPPED ##");
82 
83   if (btif_av_get_peer_sep() == AVDT_TSEP_SRC) {
84     btif_a2dp_sink_on_stopped(p_av_suspend);
85     return;
86   }
87 
88   btif_a2dp_source_on_stopped(p_av_suspend);
89 }
90 
btif_a2dp_on_suspended(tBTA_AV_SUSPEND * p_av_suspend)91 void btif_a2dp_on_suspended(tBTA_AV_SUSPEND* p_av_suspend) {
92   APPL_TRACE_EVENT("## ON A2DP SUSPENDED ##");
93   if (btif_av_get_peer_sep() == AVDT_TSEP_SRC) {
94     btif_a2dp_sink_on_suspended(p_av_suspend);
95   } else {
96     btif_a2dp_source_on_suspended(p_av_suspend);
97   }
98 }
99 
btif_a2dp_on_offload_started(tBTA_AV_STATUS status)100 void btif_a2dp_on_offload_started(tBTA_AV_STATUS status) {
101   tA2DP_CTRL_ACK ack;
102   APPL_TRACE_EVENT("%s status %d", __func__, status);
103 
104   switch (status) {
105     case BTA_AV_SUCCESS:
106       ack = A2DP_CTRL_ACK_SUCCESS;
107       break;
108     case BTA_AV_FAIL_RESOURCES:
109       APPL_TRACE_ERROR("%s FAILED UNSUPPORTED", __func__);
110       ack = A2DP_CTRL_ACK_UNSUPPORTED;
111       break;
112     default:
113       APPL_TRACE_ERROR("%s FAILED: status = %d", __func__, status);
114       ack = A2DP_CTRL_ACK_FAILURE;
115       break;
116   }
117   btif_a2dp_command_ack(ack);
118 }
119 
btif_debug_a2dp_dump(int fd)120 void btif_debug_a2dp_dump(int fd) {
121   btif_a2dp_source_debug_dump(fd);
122   btif_a2dp_sink_debug_dump(fd);
123 }
124