1 /******************************************************************************
2  *
3  *  Copyright 2003-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 is the implementation of the API for the audio gateway (AG)
22  *  subsystem of BTA, Broadcom's Bluetooth application layer for mobile
23  *  phones.
24  *
25  ******************************************************************************/
26 
27 #include "bta/include/bta_ag_api.h"
28 
29 #include <base/functional/bind.h>
30 #include <base/location.h>
31 #include <bluetooth/log.h>
32 
33 #include <cstdint>
34 #include <cstring>
35 #include <vector>
36 
37 #include "bta/ag/bta_ag_int.h"
38 #include "stack/include/main_thread.h"
39 #include "types/raw_address.h"
40 
41 using namespace bluetooth;
42 
43 /*****************************************************************************
44  *  Constants
45  ****************************************************************************/
46 
47 static const tBTA_SYS_REG bta_ag_reg = {bta_ag_hdl_event, BTA_AgDisable};
48 const tBTA_AG_RES_DATA tBTA_AG_RES_DATA::kEmpty = {};
49 
50 /*******************************************************************************
51  *
52  * Function         BTA_AgEnable
53  *
54  * Description      Enable the audio gateway service. When the enable
55  *                  operation is complete the callback function will be
56  *                  called with a BTA_AG_ENABLE_EVT. This function must
57  *                  be called before other function in the AG API are
58  *                  called.
59  *
60  * Returns          BTA_SUCCESS if OK, BTA_FAILURE otherwise.
61  *
62  ******************************************************************************/
BTA_AgEnable(tBTA_AG_CBACK * p_cback)63 tBTA_STATUS BTA_AgEnable(tBTA_AG_CBACK* p_cback) {
64   /* Error if AG is already enabled, or AG is in the middle of disabling. */
65   for (const tBTA_AG_SCB& scb : bta_ag_cb.scb) {
66     if (scb.in_use) {
67       log::error("BTA_AgEnable: FAILED, AG already enabled.");
68       return BTA_FAILURE;
69     }
70   }
71   bta_sys_register(BTA_ID_AG, &bta_ag_reg);
72   do_in_main_thread(FROM_HERE, base::BindOnce(&bta_ag_api_enable, p_cback));
73   return BTA_SUCCESS;
74 }
75 
76 /*******************************************************************************
77  *
78  * Function         BTA_AgDisable
79  *
80  * Description      Disable the audio gateway service
81  *
82  *
83  * Returns          void
84  *
85  ******************************************************************************/
BTA_AgDisable()86 void BTA_AgDisable() {
87   do_in_main_thread(FROM_HERE, base::BindOnce(&bta_ag_api_disable));
88 }
89 
90 /*******************************************************************************
91  *
92  * Function         BTA_AgRegister
93  *
94  * Description      Register an Audio Gateway service.
95  *
96  *
97  * Returns          void
98  *
99  ******************************************************************************/
BTA_AgRegister(tBTA_SERVICE_MASK services,tBTA_AG_FEAT features,const std::vector<std::string> & service_names,uint8_t app_id)100 void BTA_AgRegister(tBTA_SERVICE_MASK services, tBTA_AG_FEAT features,
101                     const std::vector<std::string>& service_names,
102                     uint8_t app_id) {
103   do_in_main_thread(FROM_HERE, base::BindOnce(&bta_ag_api_register, services,
104                                               features, service_names, app_id));
105 }
106 
107 /*******************************************************************************
108  *
109  * Function         BTA_AgDeregister
110  *
111  * Description      Deregister an audio gateway service.
112  *
113  *
114  * Returns          void
115  *
116  ******************************************************************************/
BTA_AgDeregister(uint16_t handle)117 void BTA_AgDeregister(uint16_t handle) {
118   do_in_main_thread(FROM_HERE, base::BindOnce(&bta_ag_sm_execute_by_handle,
119                                               handle, BTA_AG_API_DEREGISTER_EVT,
120                                               tBTA_AG_DATA::kEmpty));
121 }
122 
123 /*******************************************************************************
124  *
125  * Function         BTA_AgOpen
126  *
127  * Description      Opens a connection to a headset or hands-free device.
128  *                  When connection is open callback function is called
129  *                  with a BTA_AG_OPEN_EVT. Only the data connection is
130  *                  opened. The audio connection is not opened.
131  *
132  *
133  * Returns          void
134  *
135  ******************************************************************************/
BTA_AgOpen(uint16_t handle,const RawAddress & bd_addr)136 void BTA_AgOpen(uint16_t handle, const RawAddress& bd_addr) {
137   tBTA_AG_DATA data = {};
138   data.api_open.bd_addr = bd_addr;
139   do_in_main_thread(FROM_HERE,
140                     base::BindOnce(&bta_ag_sm_execute_by_handle, handle,
141                                    BTA_AG_API_OPEN_EVT, data));
142 }
143 
144 /*******************************************************************************
145  *
146  * Function         BTA_AgClose
147  *
148  * Description      Close the current connection to a headset or a handsfree
149  *                  Any current audio connection will also be closed.
150  *
151  *
152  * Returns          void
153  *
154  ******************************************************************************/
BTA_AgClose(uint16_t handle)155 void BTA_AgClose(uint16_t handle) {
156   do_in_main_thread(FROM_HERE,
157                     base::BindOnce(&bta_ag_sm_execute_by_handle, handle,
158                                    BTA_AG_API_CLOSE_EVT, tBTA_AG_DATA::kEmpty));
159 }
160 
161 /*******************************************************************************
162  *
163  * Function         BTA_AgAudioOpen
164  *
165  * Description      Opens an audio connection to the currently connected
166  *                  headset or handsfree. Specify `disabled_codecs` to
167  *                  force the stack to avoid using certain codecs.
168  *
169  *                  Note that CVSD is a mandatory codec and cannot be disabled.
170  *
171  *
172  * Returns          void
173  *
174  ******************************************************************************/
BTA_AgAudioOpen(uint16_t handle,tBTA_AG_PEER_CODEC disabled_codecs)175 void BTA_AgAudioOpen(uint16_t handle, tBTA_AG_PEER_CODEC disabled_codecs) {
176   tBTA_AG_DATA data = {};
177   data.api_audio_open.disabled_codecs = disabled_codecs;
178   do_in_main_thread(FROM_HERE,
179                     base::BindOnce(&bta_ag_sm_execute_by_handle, handle,
180                                    BTA_AG_API_AUDIO_OPEN_EVT, data));
181 }
182 
183 /*******************************************************************************
184  *
185  * Function         BTA_AgAudioClose
186  *
187  * Description      Close the currently active audio connection to a headset
188  *                  or handsfree. The data connection remains open
189  *
190  *
191  * Returns          void
192  *
193  ******************************************************************************/
BTA_AgAudioClose(uint16_t handle)194 void BTA_AgAudioClose(uint16_t handle) {
195   do_in_main_thread(
196       FROM_HERE,
197       base::BindOnce(&bta_ag_sm_execute_by_handle, handle,
198                      BTA_AG_API_AUDIO_CLOSE_EVT, tBTA_AG_DATA::kEmpty));
199 }
200 
201 /*******************************************************************************
202  *
203  * Function         BTA_AgResult
204  *
205  * Description      Send an AT result code to a headset or hands-free device.
206  *                  This function is only used when the AG parse mode is set
207  *                  to BTA_AG_PARSE.
208  *
209  *
210  * Returns          void
211  *
212  ******************************************************************************/
BTA_AgResult(uint16_t handle,tBTA_AG_RES result,const tBTA_AG_RES_DATA & data)213 void BTA_AgResult(uint16_t handle, tBTA_AG_RES result,
214                   const tBTA_AG_RES_DATA& data) {
215   do_in_main_thread(FROM_HERE,
216                     base::BindOnce(&bta_ag_api_result, handle, result, data));
217 }
218 
219 /*******************************************************************************
220  *
221  * Function         BTA_AgSetCodec
222  *
223  * Description      Specify the codec type to be used for the subsequent
224  *                  audio connection.
225  *
226  *
227  *
228  * Returns          void
229  *
230  ******************************************************************************/
BTA_AgSetCodec(uint16_t handle,tBTA_AG_PEER_CODEC codec)231 void BTA_AgSetCodec(uint16_t handle, tBTA_AG_PEER_CODEC codec) {
232   tBTA_AG_DATA data = {};
233   data.api_setcodec.codec = codec;
234   do_in_main_thread(FROM_HERE,
235                     base::BindOnce(&bta_ag_sm_execute_by_handle, handle,
236                                    BTA_AG_API_SETCODEC_EVT, data));
237 }
238 
BTA_AgSetScoOffloadEnabled(bool value)239 void BTA_AgSetScoOffloadEnabled(bool value) {
240   do_in_main_thread(FROM_HERE,
241                     base::BindOnce(&bta_ag_set_sco_offload_enabled, value));
242 }
243 
BTA_AgSetScoAllowed(bool value)244 void BTA_AgSetScoAllowed(bool value) {
245   do_in_main_thread(FROM_HERE, base::BindOnce(&bta_ag_set_sco_allowed, value));
246 }
247 
BTA_AgSetActiveDevice(const RawAddress & active_device_addr)248 void BTA_AgSetActiveDevice(const RawAddress& active_device_addr) {
249   if (active_device_addr.IsEmpty()) {
250     do_in_main_thread(FROM_HERE, base::BindOnce(&bta_clear_active_device));
251   } else {
252     do_in_main_thread(FROM_HERE, base::BindOnce(&bta_ag_api_set_active_device,
253                                                 active_device_addr));
254   }
255 }
256