1 /******************************************************************************
2  *
3  *  Copyright (c) 2014 The Android Open Source Project
4  *  Copyright (C) 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 <string.h>
28 #include "bta/bta_hf_client_api.h"
29 #include "bta_hf_client_int.h"
30 #include "osi/allocator.h"
31 
32 #if (BTA_HF_INCLUDED == TRUE)
33 /*****************************************************************************
34 **  Constants and data types
35 *****************************************************************************/
36 static const tBTA_SYS_REG bta_hf_client_reg = {
37     bta_hf_client_hdl_event,
38     BTA_HfClientDisable
39 };
40 
41 static const uint8_t bta_hf_client_cb_data_size[] = {
42     0,                                  // #define BTA_HF_CLIENT_ENABLE_EVT            0
43     sizeof(tBTA_HF_CLIENT_REGISTER),    // #define BTA_HF_CLIENT_REGISTER_EVT          1
44     sizeof(tBTA_HF_CLIENT_OPEN),        // #define BTA_HF_CLIENT_OPEN_EVT              2
45     0,                                  // #define BTA_HF_CLIENT_CLOSE_EVT             3
46     sizeof(tBTA_HF_CLIENT_CONN),        // #define BTA_HF_CLIENT_CONN_EVT              4
47     sizeof(tBTA_HF_CLIENT_HDR),         // #define BTA_HF_CLIENT_AUDIO_OPEN_EVT        5
48     sizeof(tBTA_HF_CLIENT_HDR),         //#define BTA_HF_CLIENT_AUDIO_MSBC_OPEN_EVT   6
49     sizeof(tBTA_HF_CLIENT_HDR),         // #define BTA_HF_CLIENT_AUDIO_CLOSE_EVT       7
50     sizeof(tBTA_HF_CLIENT_VAL),         // #define BTA_HF_CLIENT_SPK_EVT               8
51     sizeof(tBTA_HF_CLIENT_VAL),         // #define BTA_HF_CLIENT_MIC_EVT               9
52     sizeof(tBTA_HF_CLIENT_IND),         //#define BTA_HF_CLIENT_IND_EVT               10
53     sizeof(tBTA_HF_CLIENT_VAL),         // #define BTA_HF_CLIENT_VOICE_REC_EVT         11
54     sizeof(tBTA_HF_CLIENT_OPERATOR_NAME),    // #define BTA_HF_CLIENT_OPERATOR_NAME_EVT     12
55     sizeof(tBTA_HF_CLIENT_NUMBER),           // #define BTA_HF_CLIENT_CLIP_EVT              13
56     sizeof(tBTA_HF_CLIENT_NUMBER),      // #define BTA_HF_CLIENT_CCWA_EVT              14
57     sizeof(tBTA_HF_CLIENT_AT_RESULT),   // #define BTA_HF_CLIENT_AT_RESULT_EVT         15
58     sizeof(tBTA_HF_CLIENT_CLCC),        // #define BTA_HF_CLIENT_CLCC_EVT              16
59     sizeof(tBTA_HF_CLIENT_CNUM),        //#define BTA_HF_CLIENT_CNUM_EVT              17
60     sizeof(tBTA_HF_CLIENT_VAL),         // #define BTA_HF_CLIENT_BTRH_EVT              18
61     sizeof(tBTA_HF_CLIENT_VAL),         // #define BTA_HF_CLIENT_BSIR_EVT              19
62     sizeof(tBTA_HF_CLIENT_NUMBER),      // #define BTA_HF_CLIENT_BINP_EVT              20
63     sizeof(tBTA_HF_CLIENT_VAL),         // #define BTA_HF_CLIENT_RING_INDICATION       21
64     0,                                  // #define BTA_HF_CLIENT_DISABLE_EVT           22
65     sizeof(tBTA_SCO_PKT_STAT_NUMS),     // #define BTA_HF_CLIENT_PKT_STAT_NUMS_GET_EVT      23
66 };
67 /*****************************************************************************
68 **  External Function Declarations
69 *****************************************************************************/
70 
71 /*******************************************************************************
72 **
73 ** Function         BTA_HfClientEnable
74 **
75 ** Description      Enable the HF CLient service. When the enable
76 **                  operation is complete the callback function will be
77 **                  called with a BTA_HF_CLIENT_ENABLE_EVT. This function must
78 **                  be called before other function in the HF CLient API are
79 **                  called.
80 **
81 ** Returns          BTA_SUCCESS if OK, BTA_FAILURE otherwise.
82 **
83 *******************************************************************************/
BTA_HfClientEnable(tBTA_HF_CLIENT_CBACK * p_cback)84 tBTA_STATUS BTA_HfClientEnable(tBTA_HF_CLIENT_CBACK *p_cback)
85 {
86     tBTA_HF_CLIENT_API_ENABLE  *p_buf;
87 
88     if (bta_sys_is_register (BTA_ID_HS)) {
89         APPL_TRACE_ERROR("BTA HF Client is already enabled, ignoring ...");
90         return BTA_FAILURE;
91     }
92 
93     /* register with BTA system manager */
94     bta_sys_register(BTA_ID_HS, &bta_hf_client_reg);
95 
96     if ((p_buf = (tBTA_HF_CLIENT_API_ENABLE *) osi_malloc(sizeof(tBTA_HF_CLIENT_API_ENABLE))) != NULL) {
97         p_buf->hdr.event = BTA_HF_CLIENT_API_ENABLE_EVT;
98         p_buf->p_cback = p_cback;
99         bta_sys_sendmsg(p_buf);
100     }
101 
102     return BTA_SUCCESS;
103 }
104 
105 /*******************************************************************************
106 **
107 ** Function         BTA_HfClientDisable
108 **
109 ** Description      Disable the HF Client service
110 **
111 **
112 ** Returns          void
113 **
114 *******************************************************************************/
BTA_HfClientDisable(void)115 void BTA_HfClientDisable(void)
116 {
117     BT_HDR  *p_buf;
118 
119     if ((p_buf = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
120         p_buf->event = BTA_HF_CLIENT_API_DISABLE_EVT;
121         bta_sys_sendmsg(p_buf);
122     }
123 }
124 
125 /*******************************************************************************
126 **
127 ** Function         BTA_HfClientRegister
128 **
129 ** Description      Register an HF Client service.
130 **
131 **
132 ** Returns          void
133 **
134 *******************************************************************************/
BTA_HfClientRegister(tBTA_SEC sec_mask,tBTA_HF_CLIENT_FEAT features,char * p_service_name)135 void BTA_HfClientRegister(tBTA_SEC sec_mask, tBTA_HF_CLIENT_FEAT features,
136                           char *p_service_name)
137 {
138     tBTA_HF_CLIENT_API_REGISTER    *p_buf;
139 
140     if ((p_buf = (tBTA_HF_CLIENT_API_REGISTER *) osi_malloc(sizeof(tBTA_HF_CLIENT_API_REGISTER))) != NULL) {
141         p_buf->hdr.event = BTA_HF_CLIENT_API_REGISTER_EVT;
142         p_buf->features = features;
143         p_buf->sec_mask = sec_mask;
144         if (p_service_name) {
145             BCM_STRNCPY_S(p_buf->name, p_service_name, BTA_SERVICE_NAME_LEN);
146             p_buf->name[BTA_SERVICE_NAME_LEN] = '\0';
147         } else {
148             p_buf->name[0] = '\0';
149         }
150         bta_sys_sendmsg(p_buf);
151     }
152 }
153 
154 /*******************************************************************************
155 **
156 ** Function         BTA_HfClientDeregister
157 **
158 ** Description      Deregister an HF Client service.
159 **
160 **
161 ** Returns          void
162 **
163 *******************************************************************************/
BTA_HfClientDeregister(UINT16 handle)164 void BTA_HfClientDeregister(UINT16 handle)
165 {
166     BT_HDR  *p_buf;
167 
168     if ((p_buf = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
169         p_buf->event = BTA_HF_CLIENT_API_DEREGISTER_EVT;
170         p_buf->layer_specific = handle;
171         bta_sys_sendmsg(p_buf);
172     }
173 }
174 
175 /*******************************************************************************
176 **
177 ** Function         BTA_HfClientOpen
178 **
179 ** Description      Opens a connection to an audio gateway.
180 **                  When connection is open callback function is called
181 **                  with a BTA_AG_OPEN_EVT. Only the data connection is
182 **                  opened. The audio connection is not opened.
183 **
184 **
185 ** Returns          void
186 **
187 *******************************************************************************/
BTA_HfClientOpen(UINT16 handle,BD_ADDR bd_addr,tBTA_SEC sec_mask)188 void BTA_HfClientOpen(UINT16 handle, BD_ADDR bd_addr, tBTA_SEC sec_mask)
189 {
190     tBTA_HF_CLIENT_API_OPEN  *p_buf;
191 
192     if ((p_buf = (tBTA_HF_CLIENT_API_OPEN *) osi_malloc(sizeof(tBTA_HF_CLIENT_API_OPEN))) != NULL) {
193         p_buf->hdr.event = BTA_HF_CLIENT_API_OPEN_EVT;
194         p_buf->hdr.layer_specific = handle;
195         bdcpy(p_buf->bd_addr, bd_addr);
196         p_buf->sec_mask = sec_mask;
197         bta_sys_sendmsg(p_buf);
198     }
199 }
200 
201 /*******************************************************************************
202 **
203 ** Function         BTA_HfClientClose
204 **
205 ** Description      Close the current connection to an audio gateway.
206 **                  Any current audio connection will also be closed
207 **
208 **
209 ** Returns          void
210 **
211 *******************************************************************************/
BTA_HfClientClose(UINT16 handle)212 void BTA_HfClientClose(UINT16 handle)
213 {
214     BT_HDR  *p_buf;
215 
216     if ((p_buf = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
217         p_buf->event = BTA_HF_CLIENT_API_CLOSE_EVT;
218         p_buf->layer_specific = handle;
219         bta_sys_sendmsg(p_buf);
220     }
221 }
222 
223 /*******************************************************************************
224 **
225 ** Function         BTA_HfCllientAudioOpen
226 **
227 ** Description      Opens an audio connection to the currently connected
228 **                 audio gateway
229 **
230 **
231 ** Returns          void
232 **
233 *******************************************************************************/
BTA_HfClientAudioOpen(UINT16 handle)234 void BTA_HfClientAudioOpen(UINT16 handle)
235 {
236     BT_HDR  *p_buf;
237 
238     if ((p_buf = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
239         p_buf->event = BTA_HF_CLIENT_API_AUDIO_OPEN_EVT;
240         p_buf->layer_specific = handle;
241         bta_sys_sendmsg(p_buf);
242     }
243 }
244 
245 /*******************************************************************************
246 **
247 ** Function         BTA_HfClientAudioClose
248 **
249 ** Description      Close the currently active audio connection to an audio
250 **                  gateway. The data connection remains open
251 **
252 **
253 ** Returns          void
254 **
255 *******************************************************************************/
BTA_HfClientAudioClose(UINT16 handle)256 void BTA_HfClientAudioClose(UINT16 handle)
257 {
258     BT_HDR  *p_buf;
259 
260     if ((p_buf = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
261         p_buf->event = BTA_HF_CLIENT_API_AUDIO_CLOSE_EVT;
262         p_buf->layer_specific = handle;
263         bta_sys_sendmsg(p_buf);
264     }
265 }
266 
267 /*******************************************************************************
268 **
269 ** Function         BTA_HfClientSendAT
270 **
271 ** Description      send AT command
272 **
273 **
274 ** Returns          void
275 **
276 *******************************************************************************/
BTA_HfClientSendAT(UINT16 handle,tBTA_HF_CLIENT_AT_CMD_TYPE at,UINT32 val1,UINT32 val2,const char * str)277 void BTA_HfClientSendAT(UINT16 handle, tBTA_HF_CLIENT_AT_CMD_TYPE at, UINT32 val1, UINT32 val2, const char *str)
278 {
279     tBTA_HF_CLIENT_DATA_VAL  *p_buf;
280 
281     if ((p_buf = (tBTA_HF_CLIENT_DATA_VAL *) osi_malloc(sizeof(tBTA_HF_CLIENT_DATA_VAL))) != NULL) {
282         p_buf->hdr.event = BTA_HF_CLIENT_SEND_AT_CMD_EVT;
283         p_buf->uint8_val = at;
284         p_buf->uint32_val1 = val1;
285         p_buf->uint32_val2 = val2;
286 
287         if (str) {
288             UINT32 str_len = strlen(str);
289             if (str_len > BTA_HF_CLIENT_MAX_LEN) {
290                 APPL_TRACE_WARNING("%s, str length(%d) is more than %d, truncate it.", __FUNCTION__, str_len, BTA_HF_CLIENT_MAX_LEN);
291                 str_len = BTA_HF_CLIENT_MAX_LEN;
292             }
293 
294             strlcpy(p_buf->str, str, str_len + 1);
295         } else {
296             p_buf->str[0] = '\0';
297         }
298 
299         p_buf->hdr.layer_specific = handle;
300         bta_sys_sendmsg(p_buf);
301     }
302 }
303 
304 #if (BTM_SCO_HCI_INCLUDED == TRUE)
305 /*******************************************************************************
306 **
307 ** Function         BTA_HfClientPktStatsNumsGet
308 **
309 ** Description      Get the packet ststus numbers received and send for a specific (e)SCO connection handle.
310 **
311 **
312 ** Returns          void
313 **
314 *******************************************************************************/
BTA_HfClientPktStatsNumsGet(UINT16 sync_conn_handle)315 void BTA_HfClientPktStatsNumsGet(UINT16 sync_conn_handle)
316 {
317     tBTA_HF_CLIENT_PKT_STAT_GET *p_buf;
318     if ((p_buf = (tBTA_HF_CLIENT_PKT_STAT_GET *) osi_malloc(sizeof(tBTA_HF_CLIENT_PKT_STAT_GET))) != NULL) {
319         p_buf->hdr.event = BTA_HF_CLIENT_PKT_NUMS_GET_EVT;
320         p_buf->sync_conn_handle = sync_conn_handle;
321         bta_sys_sendmsg(p_buf);
322     }
323 }
324 
BTA_HfClientCiData(void)325 void BTA_HfClientCiData(void)
326 {
327     BT_HDR *p_buf;
328     if ((p_buf = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
329         p_buf->event = BTA_HF_CLIENT_CI_SCO_DATA_EVT;
330         bta_sys_sendmsg(p_buf);
331     }
332 }
333 #endif /* #if (BTM_SCO_HCI_INCLUDED == TRUE ) */
334 
BTA_HfClientGetCbDataSize(tBTA_HF_CLIENT_EVT event)335 int BTA_HfClientGetCbDataSize(tBTA_HF_CLIENT_EVT event)
336 {
337     return bta_hf_client_cb_data_size[event];
338 }
339 #endif /* #if (BTA_HF_INCLUDED == TRUE) */
340