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