1 /******************************************************************************
2  *
3  *  Copyright (c) 2014 The Android Open Source Project
4  *  Copyright 2003-2012 Broadcom Corporation
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at:
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  ******************************************************************************/
19 
20 /******************************************************************************
21  *
22  *  This is the implementation of the API for the handsfree (HF role)
23  *  subsystem of BTA
24  *
25  ******************************************************************************/
26 
27 #include "bta/include/bta_hf_client_api.h"
28 
29 #include <android_bluetooth_sysprop.h>
30 #include <bluetooth/log.h>
31 
32 #include <cstdint>
33 
34 #include "bta/hf_client/bta_hf_client_int.h"
35 #include "bta/sys/bta_sys.h"
36 #include "internal_include/bt_trace.h"
37 #include "osi/include/allocator.h"
38 #include "osi/include/compat.h"
39 #include "stack/include/bt_hdr.h"
40 #include "types/raw_address.h"
41 
42 using namespace bluetooth;
43 
44 /*****************************************************************************
45  *  External Function Declarations
46  ****************************************************************************/
47 
48 /*******************************************************************************
49  *
50  * Function         BTA_HfClientEnable
51  *
52  * Description      Enable the HF CLient service. It does the following:
53  *                  1. Sets the state to initialized (control blocks)
54  *                  2. Starts the SDP for the client role (HF)
55  *                  3. Starts the RFCOMM server to accept incoming connections
56  *                  The function is synchronous and returns with an error code
57  *                  if anything went wrong. This should be the first call to the
58  *                  API before doing an BTA_HfClientOpen
59  *
60  * Returns          BTA_SUCCESS if OK, BTA_FAILURE otherwise.
61  *
62  ******************************************************************************/
BTA_HfClientEnable(tBTA_HF_CLIENT_CBACK * p_cback,tBTA_HF_CLIENT_FEAT features,const char * p_service_name)63 tBTA_STATUS BTA_HfClientEnable(tBTA_HF_CLIENT_CBACK* p_cback,
64                                tBTA_HF_CLIENT_FEAT features,
65                                const char* p_service_name) {
66   return bta_hf_client_api_enable(p_cback, features, p_service_name);
67 }
68 
69 /*******************************************************************************
70  *
71  * Function         BTA_HfClientDisable
72  *
73  * Description      Disable the HF Client service
74  *
75  * Returns          void
76  *
77  ******************************************************************************/
BTA_HfClientDisable(void)78 void BTA_HfClientDisable(void) { bta_hf_client_api_disable(); }
79 
80 /*******************************************************************************
81  *
82  * Function         BTA_HfClientOpen
83  *
84  * Description      Opens up a RF connection to the remote device and
85  *                  subsequently set it up for a HF SLC
86  *
87  * Returns          bt_status_t
88  *
89  ******************************************************************************/
BTA_HfClientOpen(const RawAddress & bd_addr,uint16_t * p_handle)90 bt_status_t BTA_HfClientOpen(const RawAddress& bd_addr, uint16_t* p_handle) {
91   log::verbose("");
92   tBTA_HF_CLIENT_API_OPEN* p_buf =
93       (tBTA_HF_CLIENT_API_OPEN*)osi_malloc(sizeof(tBTA_HF_CLIENT_API_OPEN));
94 
95   if (!bta_hf_client_allocate_handle(bd_addr, p_handle)) {
96     log::error("could not allocate handle");
97     return BT_STATUS_NOMEM;
98   }
99 
100   p_buf->hdr.event = BTA_HF_CLIENT_API_OPEN_EVT;
101   p_buf->hdr.layer_specific = *p_handle;
102   p_buf->bd_addr = bd_addr;
103 
104   bta_sys_sendmsg(p_buf);
105   return BT_STATUS_SUCCESS;
106 }
107 
108 /*******************************************************************************
109  *
110  * Function         BTA_HfClientClose
111  *
112  * Description      Close the current connection to an audio gateway.
113  *                  Any current audio connection will also be closed
114  *
115  *
116  * Returns          void
117  *
118  ******************************************************************************/
BTA_HfClientClose(uint16_t handle)119 void BTA_HfClientClose(uint16_t handle) {
120   BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
121 
122   p_buf->event = BTA_HF_CLIENT_API_CLOSE_EVT;
123   p_buf->layer_specific = handle;
124 
125   bta_sys_sendmsg(p_buf);
126 }
127 
128 /*******************************************************************************
129  *
130  * Function         BTA_HfCllientAudioOpen
131  *
132  * Description      Opens an audio connection to the currently connected
133  *                 audio gateway
134  *
135  *
136  * Returns          void
137  *
138  ******************************************************************************/
BTA_HfClientAudioOpen(uint16_t handle)139 void BTA_HfClientAudioOpen(uint16_t handle) {
140   BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
141 
142   p_buf->event = BTA_HF_CLIENT_API_AUDIO_OPEN_EVT;
143   p_buf->layer_specific = handle;
144 
145   bta_sys_sendmsg(p_buf);
146 }
147 
148 /*******************************************************************************
149  *
150  * Function         BTA_HfClientAudioClose
151  *
152  * Description      Close the currently active audio connection to an audio
153  *                  gateway. The data connection remains open
154  *
155  *
156  * Returns          void
157  *
158  ******************************************************************************/
BTA_HfClientAudioClose(uint16_t handle)159 void BTA_HfClientAudioClose(uint16_t handle) {
160   BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
161 
162   p_buf->event = BTA_HF_CLIENT_API_AUDIO_CLOSE_EVT;
163   p_buf->layer_specific = handle;
164 
165   bta_sys_sendmsg(p_buf);
166 }
167 
168 /*******************************************************************************
169  *
170  * Function         BTA_HfClientSendAT
171  *
172  * Description      send AT command
173  *
174  *
175  * Returns          void
176  *
177  ******************************************************************************/
BTA_HfClientSendAT(uint16_t handle,tBTA_HF_CLIENT_AT_CMD_TYPE at,uint32_t val1,uint32_t val2,const char * str)178 void BTA_HfClientSendAT(uint16_t handle, tBTA_HF_CLIENT_AT_CMD_TYPE at,
179                         uint32_t val1, uint32_t val2, const char* str) {
180   tBTA_HF_CLIENT_DATA_VAL* p_buf =
181       (tBTA_HF_CLIENT_DATA_VAL*)osi_malloc(sizeof(tBTA_HF_CLIENT_DATA_VAL));
182 
183   p_buf->hdr.event = BTA_HF_CLIENT_SEND_AT_CMD_EVT;
184   p_buf->uint8_val = at;
185   p_buf->uint32_val1 = val1;
186   p_buf->uint32_val2 = val2;
187 
188   if (str) {
189     strlcpy(p_buf->str, str, BTA_HF_CLIENT_NUMBER_LEN + 1);
190     p_buf->str[BTA_HF_CLIENT_NUMBER_LEN] = '\0';
191   } else {
192     p_buf->str[0] = '\0';
193   }
194 
195   p_buf->hdr.layer_specific = handle;
196 
197   bta_sys_sendmsg(p_buf);
198 }
199 
200 /*******************************************************************************
201  *
202  * Function         BTA_HfClientDumpStatistics
203  *
204  * Description      Dump statistics about the various control blocks
205  *                  and other relevant connection statistics
206  *
207  * Returns          Void
208  *
209  ******************************************************************************/
BTA_HfClientDumpStatistics(int fd)210 void BTA_HfClientDumpStatistics(int fd) { bta_hf_client_dump_statistics(fd); }
211 
212 /*******************************************************************************
213  *
214  * function         get_default_hf_client_features
215  *
216  * description      return the hf_client features.
217  *                  value can be override via system property
218  *
219  * returns          int
220  *
221  ******************************************************************************/
get_default_hf_client_features()222 int get_default_hf_client_features() {
223 #define DEFAULT_BTIF_HF_CLIENT_FEATURES                                        \
224   (BTA_HF_CLIENT_FEAT_ECNR | BTA_HF_CLIENT_FEAT_3WAY |                         \
225    BTA_HF_CLIENT_FEAT_CLI | BTA_HF_CLIENT_FEAT_VREC | BTA_HF_CLIENT_FEAT_VOL | \
226    BTA_HF_CLIENT_FEAT_ECS | BTA_HF_CLIENT_FEAT_ECC | BTA_HF_CLIENT_FEAT_CODEC)
227 
228   return GET_SYSPROP(Hfp, hf_client_features, DEFAULT_BTIF_HF_CLIENT_FEATURES);
229 }
230