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           30
65 };
66 /*****************************************************************************
67 **  External Function Declarations
68 *****************************************************************************/
69 
70 /*******************************************************************************
71 **
72 ** Function         BTA_HfClientEnable
73 **
74 ** Description      Enable the HF CLient service. When the enable
75 **                  operation is complete the callback function will be
76 **                  called with a BTA_HF_CLIENT_ENABLE_EVT. This function must
77 **                  be called before other function in the HF CLient API are
78 **                  called.
79 **
80 ** Returns          BTA_SUCCESS if OK, BTA_FAILURE otherwise.
81 **
82 *******************************************************************************/
BTA_HfClientEnable(tBTA_HF_CLIENT_CBACK * p_cback)83 tBTA_STATUS BTA_HfClientEnable(tBTA_HF_CLIENT_CBACK *p_cback)
84 {
85     tBTA_HF_CLIENT_API_ENABLE  *p_buf;
86 
87     if (bta_sys_is_register (BTA_ID_HS)) {
88         APPL_TRACE_ERROR("BTA HF Client is already enabled, ignoring ...");
89         return BTA_FAILURE;
90     }
91 
92     /* register with BTA system manager */
93     bta_sys_register(BTA_ID_HS, &bta_hf_client_reg);
94 
95     if ((p_buf = (tBTA_HF_CLIENT_API_ENABLE *) osi_malloc(sizeof(tBTA_HF_CLIENT_API_ENABLE))) != NULL) {
96         p_buf->hdr.event = BTA_HF_CLIENT_API_ENABLE_EVT;
97         p_buf->p_cback = p_cback;
98         bta_sys_sendmsg(p_buf);
99     }
100 
101     return BTA_SUCCESS;
102 }
103 
104 /*******************************************************************************
105 **
106 ** Function         BTA_HfClientDisable
107 **
108 ** Description      Disable the HF Client service
109 **
110 **
111 ** Returns          void
112 **
113 *******************************************************************************/
BTA_HfClientDisable(void)114 void BTA_HfClientDisable(void)
115 {
116     BT_HDR  *p_buf;
117 
118     if ((p_buf = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
119         p_buf->event = BTA_HF_CLIENT_API_DISABLE_EVT;
120         bta_sys_sendmsg(p_buf);
121     }
122 }
123 
124 /*******************************************************************************
125 **
126 ** Function         BTA_HfClientRegister
127 **
128 ** Description      Register an HF Client service.
129 **
130 **
131 ** Returns          void
132 **
133 *******************************************************************************/
BTA_HfClientRegister(tBTA_SEC sec_mask,tBTA_HF_CLIENT_FEAT features,char * p_service_name)134 void BTA_HfClientRegister(tBTA_SEC sec_mask, tBTA_HF_CLIENT_FEAT features,
135                           char *p_service_name)
136 {
137     tBTA_HF_CLIENT_API_REGISTER    *p_buf;
138 
139     if ((p_buf = (tBTA_HF_CLIENT_API_REGISTER *) osi_malloc(sizeof(tBTA_HF_CLIENT_API_REGISTER))) != NULL) {
140         p_buf->hdr.event = BTA_HF_CLIENT_API_REGISTER_EVT;
141         p_buf->features = features;
142         p_buf->sec_mask = sec_mask;
143         if (p_service_name) {
144             BCM_STRNCPY_S(p_buf->name, p_service_name, BTA_SERVICE_NAME_LEN);
145             p_buf->name[BTA_SERVICE_NAME_LEN] = 0;
146         } else {
147             p_buf->name[0] = '\0';
148         }
149         bta_sys_sendmsg(p_buf);
150     }
151 }
152 
153 /*******************************************************************************
154 **
155 ** Function         BTA_HfClientDeregister
156 **
157 ** Description      Deregister an HF Client service.
158 **
159 **
160 ** Returns          void
161 **
162 *******************************************************************************/
BTA_HfClientDeregister(UINT16 handle)163 void BTA_HfClientDeregister(UINT16 handle)
164 {
165     BT_HDR  *p_buf;
166 
167     if ((p_buf = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
168         p_buf->event = BTA_HF_CLIENT_API_DEREGISTER_EVT;
169         p_buf->layer_specific = handle;
170         bta_sys_sendmsg(p_buf);
171     }
172 }
173 
174 /*******************************************************************************
175 **
176 ** Function         BTA_HfClientOpen
177 **
178 ** Description      Opens a connection to an audio gateway.
179 **                  When connection is open callback function is called
180 **                  with a BTA_AG_OPEN_EVT. Only the data connection is
181 **                  opened. The audio connection is not opened.
182 **
183 **
184 ** Returns          void
185 **
186 *******************************************************************************/
BTA_HfClientOpen(UINT16 handle,BD_ADDR bd_addr,tBTA_SEC sec_mask)187 void BTA_HfClientOpen(UINT16 handle, BD_ADDR bd_addr, tBTA_SEC sec_mask)
188 {
189     tBTA_HF_CLIENT_API_OPEN  *p_buf;
190 
191     if ((p_buf = (tBTA_HF_CLIENT_API_OPEN *) osi_malloc(sizeof(tBTA_HF_CLIENT_API_OPEN))) != NULL) {
192         p_buf->hdr.event = BTA_HF_CLIENT_API_OPEN_EVT;
193         p_buf->hdr.layer_specific = handle;
194         bdcpy(p_buf->bd_addr, bd_addr);
195         p_buf->sec_mask = sec_mask;
196         bta_sys_sendmsg(p_buf);
197     }
198 }
199 
200 /*******************************************************************************
201 **
202 ** Function         BTA_HfClientClose
203 **
204 ** Description      Close the current connection to an audio gateway.
205 **                  Any current audio connection will also be closed
206 **
207 **
208 ** Returns          void
209 **
210 *******************************************************************************/
BTA_HfClientClose(UINT16 handle)211 void BTA_HfClientClose(UINT16 handle)
212 {
213     BT_HDR  *p_buf;
214 
215     if ((p_buf = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
216         p_buf->event = BTA_HF_CLIENT_API_CLOSE_EVT;
217         p_buf->layer_specific = handle;
218         bta_sys_sendmsg(p_buf);
219     }
220 }
221 
222 /*******************************************************************************
223 **
224 ** Function         BTA_HfCllientAudioOpen
225 **
226 ** Description      Opens an audio connection to the currently connected
227 **                 audio gateway
228 **
229 **
230 ** Returns          void
231 **
232 *******************************************************************************/
BTA_HfClientAudioOpen(UINT16 handle)233 void BTA_HfClientAudioOpen(UINT16 handle)
234 {
235     BT_HDR  *p_buf;
236 
237     if ((p_buf = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
238         p_buf->event = BTA_HF_CLIENT_API_AUDIO_OPEN_EVT;
239         p_buf->layer_specific = handle;
240         bta_sys_sendmsg(p_buf);
241     }
242 }
243 
244 /*******************************************************************************
245 **
246 ** Function         BTA_HfClientAudioClose
247 **
248 ** Description      Close the currently active audio connection to an audio
249 **                  gateway. The data connection remains open
250 **
251 **
252 ** Returns          void
253 **
254 *******************************************************************************/
BTA_HfClientAudioClose(UINT16 handle)255 void BTA_HfClientAudioClose(UINT16 handle)
256 {
257     BT_HDR  *p_buf;
258 
259     if ((p_buf = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
260         p_buf->event = BTA_HF_CLIENT_API_AUDIO_CLOSE_EVT;
261         p_buf->layer_specific = handle;
262         bta_sys_sendmsg(p_buf);
263     }
264 }
265 
266 /*******************************************************************************
267 **
268 ** Function         BTA_HfClientSendAT
269 **
270 ** Description      send AT command
271 **
272 **
273 ** Returns          void
274 **
275 *******************************************************************************/
BTA_HfClientSendAT(UINT16 handle,tBTA_HF_CLIENT_AT_CMD_TYPE at,UINT32 val1,UINT32 val2,const char * str)276 void BTA_HfClientSendAT(UINT16 handle, tBTA_HF_CLIENT_AT_CMD_TYPE at, UINT32 val1, UINT32 val2, const char *str)
277 {
278     tBTA_HF_CLIENT_DATA_VAL  *p_buf;
279 
280     if ((p_buf = (tBTA_HF_CLIENT_DATA_VAL *) osi_malloc(sizeof(tBTA_HF_CLIENT_DATA_VAL))) != NULL) {
281         p_buf->hdr.event = BTA_HF_CLIENT_SEND_AT_CMD_EVT;
282         p_buf->uint8_val = at;
283         p_buf->uint32_val1 = val1;
284         p_buf->uint32_val2 = val2;
285 
286         if (str) {
287             strlcpy(p_buf->str, str, BTA_HF_CLIENT_NUMBER_LEN + 1);
288             p_buf->str[BTA_HF_CLIENT_NUMBER_LEN] = '\0';
289         } else {
290             p_buf->str[0] = '\0';
291         }
292 
293         p_buf->hdr.layer_specific = handle;
294         bta_sys_sendmsg(p_buf);
295     }
296 }
297 #if (BTM_SCO_HCI_INCLUDED == TRUE )
BTA_HfClientCiData(void)298 void BTA_HfClientCiData(void)
299 {
300     BT_HDR *p_buf;
301     if ((p_buf = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
302         p_buf->event = BTA_HF_CLIENT_CI_SCO_DATA_EVT;
303         bta_sys_sendmsg(p_buf);
304     }
305 }
306 #endif /* #if (BTM_SCO_HCI_INCLUDED == TRUE ) */
307 
BTA_HfClientGetCbDataSize(tBTA_HF_CLIENT_EVT event)308 int BTA_HfClientGetCbDataSize(tBTA_HF_CLIENT_EVT event)
309 {
310     return bta_hf_client_cb_data_size[event];
311 }
312 #endif /* #if (BTA_HF_INCLUDED == TRUE) */
313