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