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_control"
21 
22 #include <base/logging.h>
23 #include <stdbool.h>
24 #include <stdint.h>
25 
26 #include "audio_a2dp_hw/include/audio_a2dp_hw.h"
27 #include "bt_common.h"
28 #include "btif_a2dp.h"
29 #include "btif_a2dp_control.h"
30 #include "btif_a2dp_sink.h"
31 #include "btif_a2dp_source.h"
32 #include "btif_av.h"
33 #include "btif_av_co.h"
34 #include "btif_hf.h"
35 #include "osi/include/osi.h"
36 #include "uipc.h"
37 
38 #define A2DP_DATA_READ_POLL_MS 10
39 
40 static void btif_a2dp_data_cb(tUIPC_CH_ID ch_id, tUIPC_EVENT event);
41 static void btif_a2dp_ctrl_cb(tUIPC_CH_ID ch_id, tUIPC_EVENT event);
42 
43 /* We can have max one command pending */
44 static tA2DP_CTRL_CMD a2dp_cmd_pending = A2DP_CTRL_CMD_NONE;
45 
btif_a2dp_control_init(void)46 void btif_a2dp_control_init(void) {
47   UIPC_Init(NULL);
48   UIPC_Open(UIPC_CH_ID_AV_CTRL, btif_a2dp_ctrl_cb);
49 }
50 
btif_a2dp_control_cleanup(void)51 void btif_a2dp_control_cleanup(void) {
52   /* This calls blocks until UIPC is fully closed */
53   UIPC_Close(UIPC_CH_ID_ALL);
54 }
55 
btif_a2dp_recv_ctrl_data(void)56 static void btif_a2dp_recv_ctrl_data(void) {
57   tA2DP_CTRL_CMD cmd = A2DP_CTRL_CMD_NONE;
58   int n;
59 
60   uint8_t read_cmd = 0; /* The read command size is one octet */
61   n = UIPC_Read(UIPC_CH_ID_AV_CTRL, NULL, &read_cmd, 1);
62   cmd = static_cast<tA2DP_CTRL_CMD>(read_cmd);
63 
64   /* detach on ctrl channel means audioflinger process was terminated */
65   if (n == 0) {
66     APPL_TRACE_EVENT("CTRL CH DETACHED");
67     UIPC_Close(UIPC_CH_ID_AV_CTRL);
68     return;
69   }
70 
71   APPL_TRACE_DEBUG("a2dp-ctrl-cmd : %s", audio_a2dp_hw_dump_ctrl_event(cmd));
72   a2dp_cmd_pending = cmd;
73 
74   switch (cmd) {
75     case A2DP_CTRL_CMD_CHECK_READY:
76       if (btif_a2dp_source_media_task_is_shutting_down()) {
77         APPL_TRACE_WARNING("%s: A2DP command %s while media task shutting down",
78                            __func__, audio_a2dp_hw_dump_ctrl_event(cmd));
79         btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE);
80         return;
81       }
82 
83       /* check whether AV is ready to setup A2DP datapath */
84       if (btif_av_stream_ready() || btif_av_stream_started_ready()) {
85         btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
86       } else {
87         APPL_TRACE_WARNING("%s: A2DP command %s while AV stream is not ready",
88                            __func__, audio_a2dp_hw_dump_ctrl_event(cmd));
89         btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE);
90       }
91       break;
92 
93     case A2DP_CTRL_CMD_START:
94       /*
95        * Don't send START request to stack while we are in a call.
96        * Some headsets such as "Sony MW600", don't allow AVDTP START
97        * while in a call, and respond with BAD_STATE.
98        */
99       if (!btif_hf_is_call_idle()) {
100         btif_a2dp_command_ack(A2DP_CTRL_ACK_INCALL_FAILURE);
101         break;
102       }
103 
104       if (btif_a2dp_source_is_streaming()) {
105         APPL_TRACE_WARNING("%s: A2DP command %s while source is streaming",
106                            __func__, audio_a2dp_hw_dump_ctrl_event(cmd));
107         btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE);
108         break;
109       }
110 
111       if (btif_av_stream_ready()) {
112         /* Setup audio data channel listener */
113         UIPC_Open(UIPC_CH_ID_AV_AUDIO, btif_a2dp_data_cb);
114 
115         /*
116          * Post start event and wait for audio path to open.
117          * If we are the source, the ACK will be sent after the start
118          * procedure is completed, othewise send it now.
119          */
120         btif_dispatch_sm_event(BTIF_AV_START_STREAM_REQ_EVT, NULL, 0);
121         if (btif_av_get_peer_sep() == AVDT_TSEP_SRC)
122           btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
123         break;
124       }
125 
126       if (btif_av_stream_started_ready()) {
127         /*
128          * Already started, setup audio data channel listener and ACK
129          * back immediately.
130          */
131         UIPC_Open(UIPC_CH_ID_AV_AUDIO, btif_a2dp_data_cb);
132         btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
133         break;
134       }
135       APPL_TRACE_WARNING("%s: A2DP command %s while AV stream is not ready",
136                          __func__, audio_a2dp_hw_dump_ctrl_event(cmd));
137       btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE);
138       break;
139 
140     case A2DP_CTRL_CMD_STOP:
141       if (btif_av_get_peer_sep() == AVDT_TSEP_SNK &&
142           !btif_a2dp_source_is_streaming()) {
143         /* We are already stopped, just ack back */
144         btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
145         break;
146       }
147 
148       btif_dispatch_sm_event(BTIF_AV_STOP_STREAM_REQ_EVT, NULL, 0);
149       btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
150       break;
151 
152     case A2DP_CTRL_CMD_SUSPEND:
153       /* Local suspend */
154       if (btif_av_stream_started_ready()) {
155         btif_dispatch_sm_event(BTIF_AV_SUSPEND_STREAM_REQ_EVT, NULL, 0);
156         break;
157       }
158       /* If we are not in started state, just ack back ok and let
159        * audioflinger close the channel. This can happen if we are
160        * remotely suspended, clear REMOTE SUSPEND flag.
161        */
162       btif_av_clear_remote_suspend_flag();
163       btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
164       break;
165 
166     case A2DP_CTRL_GET_INPUT_AUDIO_CONFIG: {
167       tA2DP_SAMPLE_RATE sample_rate = btif_a2dp_sink_get_sample_rate();
168       tA2DP_CHANNEL_COUNT channel_count = btif_a2dp_sink_get_channel_count();
169 
170       btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
171       UIPC_Send(UIPC_CH_ID_AV_CTRL, 0, reinterpret_cast<uint8_t*>(&sample_rate),
172                 sizeof(tA2DP_SAMPLE_RATE));
173       UIPC_Send(UIPC_CH_ID_AV_CTRL, 0, &channel_count,
174                 sizeof(tA2DP_CHANNEL_COUNT));
175       break;
176     }
177 
178     case A2DP_CTRL_GET_OUTPUT_AUDIO_CONFIG: {
179       btav_a2dp_codec_config_t codec_config;
180       btav_a2dp_codec_config_t codec_capability;
181       codec_config.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
182       codec_config.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
183       codec_config.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
184       codec_capability.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
185       codec_capability.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
186       codec_capability.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
187 
188       A2dpCodecConfig* current_codec = bta_av_get_a2dp_current_codec();
189       if (current_codec != nullptr) {
190         codec_config = current_codec->getCodecConfig();
191         codec_capability = current_codec->getCodecCapability();
192       }
193 
194       btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
195       // Send the current codec config
196       UIPC_Send(UIPC_CH_ID_AV_CTRL, 0,
197                 reinterpret_cast<const uint8_t*>(&codec_config.sample_rate),
198                 sizeof(btav_a2dp_codec_sample_rate_t));
199       UIPC_Send(UIPC_CH_ID_AV_CTRL, 0,
200                 reinterpret_cast<const uint8_t*>(&codec_config.bits_per_sample),
201                 sizeof(btav_a2dp_codec_bits_per_sample_t));
202       UIPC_Send(UIPC_CH_ID_AV_CTRL, 0,
203                 reinterpret_cast<const uint8_t*>(&codec_config.channel_mode),
204                 sizeof(btav_a2dp_codec_channel_mode_t));
205       // Send the current codec capability
206       UIPC_Send(UIPC_CH_ID_AV_CTRL, 0,
207                 reinterpret_cast<const uint8_t*>(&codec_capability.sample_rate),
208                 sizeof(btav_a2dp_codec_sample_rate_t));
209       UIPC_Send(UIPC_CH_ID_AV_CTRL, 0, reinterpret_cast<const uint8_t*>(
210                                            &codec_capability.bits_per_sample),
211                 sizeof(btav_a2dp_codec_bits_per_sample_t));
212       UIPC_Send(UIPC_CH_ID_AV_CTRL, 0, reinterpret_cast<const uint8_t*>(
213                                            &codec_capability.channel_mode),
214                 sizeof(btav_a2dp_codec_channel_mode_t));
215       break;
216     }
217 
218     case A2DP_CTRL_SET_OUTPUT_AUDIO_CONFIG: {
219       btav_a2dp_codec_config_t codec_config;
220       codec_config.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
221       codec_config.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
222       codec_config.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
223 
224       btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
225       // Send the current codec config
226       if (UIPC_Read(UIPC_CH_ID_AV_CTRL, 0,
227                     reinterpret_cast<uint8_t*>(&codec_config.sample_rate),
228                     sizeof(btav_a2dp_codec_sample_rate_t)) !=
229           sizeof(btav_a2dp_codec_sample_rate_t)) {
230         APPL_TRACE_ERROR("Error reading sample rate from audio HAL");
231         break;
232       }
233       if (UIPC_Read(UIPC_CH_ID_AV_CTRL, 0,
234                     reinterpret_cast<uint8_t*>(&codec_config.bits_per_sample),
235                     sizeof(btav_a2dp_codec_bits_per_sample_t)) !=
236           sizeof(btav_a2dp_codec_bits_per_sample_t)) {
237         APPL_TRACE_ERROR("Error reading bits per sample from audio HAL");
238         break;
239       }
240       if (UIPC_Read(UIPC_CH_ID_AV_CTRL, 0,
241                     reinterpret_cast<uint8_t*>(&codec_config.channel_mode),
242                     sizeof(btav_a2dp_codec_channel_mode_t)) !=
243           sizeof(btav_a2dp_codec_channel_mode_t)) {
244         APPL_TRACE_ERROR("Error reading channel mode from audio HAL");
245         break;
246       }
247       APPL_TRACE_DEBUG(
248           "%s: A2DP_CTRL_SET_OUTPUT_AUDIO_CONFIG: "
249           "sample_rate=0x%x bits_per_sample=0x%x "
250           "channel_mode=0x%x",
251           __func__, codec_config.sample_rate, codec_config.bits_per_sample,
252           codec_config.channel_mode);
253       btif_a2dp_source_feeding_update_req(codec_config);
254       break;
255     }
256 
257     case A2DP_CTRL_CMD_OFFLOAD_START:
258       btif_dispatch_sm_event(BTIF_AV_OFFLOAD_START_REQ_EVT, NULL, 0);
259       break;
260 
261     default:
262       APPL_TRACE_ERROR("UNSUPPORTED CMD (%d)", cmd);
263       btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE);
264       break;
265   }
266   APPL_TRACE_DEBUG("a2dp-ctrl-cmd : %s DONE",
267                    audio_a2dp_hw_dump_ctrl_event(cmd));
268 }
269 
btif_a2dp_ctrl_cb(UNUSED_ATTR tUIPC_CH_ID ch_id,tUIPC_EVENT event)270 static void btif_a2dp_ctrl_cb(UNUSED_ATTR tUIPC_CH_ID ch_id,
271                               tUIPC_EVENT event) {
272   APPL_TRACE_DEBUG("A2DP-CTRL-CHANNEL EVENT %s", dump_uipc_event(event));
273 
274   switch (event) {
275     case UIPC_OPEN_EVT:
276       break;
277 
278     case UIPC_CLOSE_EVT:
279       /* restart ctrl server unless we are shutting down */
280       if (btif_a2dp_source_media_task_is_running())
281         UIPC_Open(UIPC_CH_ID_AV_CTRL, btif_a2dp_ctrl_cb);
282       break;
283 
284     case UIPC_RX_DATA_READY_EVT:
285       btif_a2dp_recv_ctrl_data();
286       break;
287 
288     default:
289       APPL_TRACE_ERROR("### A2DP-CTRL-CHANNEL EVENT %d NOT HANDLED ###", event);
290       break;
291   }
292 }
293 
btif_a2dp_data_cb(UNUSED_ATTR tUIPC_CH_ID ch_id,tUIPC_EVENT event)294 static void btif_a2dp_data_cb(UNUSED_ATTR tUIPC_CH_ID ch_id,
295                               tUIPC_EVENT event) {
296   APPL_TRACE_DEBUG("BTIF MEDIA (A2DP-DATA) EVENT %s", dump_uipc_event(event));
297 
298   switch (event) {
299     case UIPC_OPEN_EVT:
300       /*
301        * Read directly from media task from here on (keep callback for
302        * connection events.
303        */
304       UIPC_Ioctl(UIPC_CH_ID_AV_AUDIO, UIPC_REG_REMOVE_ACTIVE_READSET, NULL);
305       UIPC_Ioctl(UIPC_CH_ID_AV_AUDIO, UIPC_SET_READ_POLL_TMO,
306                  reinterpret_cast<void*>(A2DP_DATA_READ_POLL_MS));
307 
308       if (btif_av_get_peer_sep() == AVDT_TSEP_SNK) {
309         /* Start the media task to encode the audio */
310         btif_a2dp_source_start_audio_req();
311       }
312 
313       /* ACK back when media task is fully started */
314       break;
315 
316     case UIPC_CLOSE_EVT:
317       APPL_TRACE_EVENT("## AUDIO PATH DETACHED ##");
318       btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
319       /*
320        * Send stop request only if we are actively streaming and haven't
321        * received a stop request. Potentially, the audioflinger detached
322        * abnormally.
323        */
324       if (btif_a2dp_source_is_streaming()) {
325         /* Post stop event and wait for audio path to stop */
326         btif_dispatch_sm_event(BTIF_AV_STOP_STREAM_REQ_EVT, NULL, 0);
327       }
328       break;
329 
330     default:
331       APPL_TRACE_ERROR("### A2DP-DATA EVENT %d NOT HANDLED ###", event);
332       break;
333   }
334 }
335 
btif_a2dp_command_ack(tA2DP_CTRL_ACK status)336 void btif_a2dp_command_ack(tA2DP_CTRL_ACK status) {
337   uint8_t ack = status;
338 
339   APPL_TRACE_EVENT("## a2dp ack : %s, status %d ##",
340                    audio_a2dp_hw_dump_ctrl_event(a2dp_cmd_pending), status);
341 
342   /* Sanity check */
343   if (a2dp_cmd_pending == A2DP_CTRL_CMD_NONE) {
344     APPL_TRACE_ERROR("warning : no command pending, ignore ack");
345     return;
346   }
347 
348   /* Clear pending */
349   a2dp_cmd_pending = A2DP_CTRL_CMD_NONE;
350 
351   /* Acknowledge start request */
352   UIPC_Send(UIPC_CH_ID_AV_CTRL, 0, &ack, sizeof(ack));
353 }
354