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