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