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