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 <string.h>
28 
29 #include "bta_hf_client_api.h"
30 #include "bta_hf_client_int.h"
31 #include "osi/include/compat.h"
32 
33 /*****************************************************************************
34  *  External Function Declarations
35  ****************************************************************************/
36 
37 /*******************************************************************************
38  *
39  * Function         BTA_HfClientEnable
40  *
41  * Description      Enable the HF CLient service. It does the following:
42  *                  1. Sets the state to initialized (control blocks)
43  *                  2. Starts the SDP for the client role (HF)
44  *                  3. Starts the RFCOMM server to accept incoming connections
45  *                  The function is synchronous and returns with an error code
46  *                  if anything went wrong. This should be the first call to the
47  *                  API before doing an BTA_HfClientOpen
48  *
49  * Returns          BTA_SUCCESS if OK, BTA_FAILURE otherwise.
50  *
51  ******************************************************************************/
BTA_HfClientEnable(tBTA_HF_CLIENT_CBACK * p_cback,tBTA_SEC sec_mask,tBTA_HF_CLIENT_FEAT features,const char * p_service_name)52 tBTA_STATUS BTA_HfClientEnable(tBTA_HF_CLIENT_CBACK* p_cback, tBTA_SEC sec_mask,
53                                tBTA_HF_CLIENT_FEAT features,
54                                const char* p_service_name) {
55   return bta_hf_client_api_enable(p_cback, sec_mask, features, p_service_name);
56 }
57 
58 /*******************************************************************************
59  *
60  * Function         BTA_HfClientDisable
61  *
62  * Description      Disable the HF Client service
63  *
64  * Returns          void
65  *
66  ******************************************************************************/
BTA_HfClientDisable(void)67 void BTA_HfClientDisable(void) { bta_hf_client_api_disable(); }
68 
69 /*******************************************************************************
70  *
71  * Function         BTA_HfClientOpen
72  *
73  * Description      Opens up a RF connection to the remote device and
74  *                  subsequently set it up for a HF SLC
75  *
76  * Returns          void
77  *
78  ******************************************************************************/
BTA_HfClientOpen(const RawAddress & bd_addr,tBTA_SEC sec_mask,uint16_t * p_handle)79 void BTA_HfClientOpen(const RawAddress& bd_addr, tBTA_SEC sec_mask,
80                       uint16_t* p_handle) {
81   APPL_TRACE_DEBUG("%s", __func__);
82   tBTA_HF_CLIENT_API_OPEN* p_buf =
83       (tBTA_HF_CLIENT_API_OPEN*)osi_malloc(sizeof(tBTA_HF_CLIENT_API_OPEN));
84 
85   if (!bta_hf_client_allocate_handle(bd_addr, p_handle)) {
86     APPL_TRACE_ERROR("%s: could not allocate handle", __func__);
87     return;
88   }
89 
90   p_buf->hdr.event = BTA_HF_CLIENT_API_OPEN_EVT;
91   p_buf->hdr.layer_specific = *p_handle;
92   p_buf->bd_addr = bd_addr;
93   p_buf->sec_mask = sec_mask;
94 
95   bta_sys_sendmsg(p_buf);
96 }
97 
98 /*******************************************************************************
99  *
100  * Function         BTA_HfClientClose
101  *
102  * Description      Close the current connection to an audio gateway.
103  *                  Any current audio connection will also be closed
104  *
105  *
106  * Returns          void
107  *
108  ******************************************************************************/
BTA_HfClientClose(uint16_t handle)109 void BTA_HfClientClose(uint16_t handle) {
110   BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
111 
112   p_buf->event = BTA_HF_CLIENT_API_CLOSE_EVT;
113   p_buf->layer_specific = handle;
114 
115   bta_sys_sendmsg(p_buf);
116 }
117 
118 /*******************************************************************************
119  *
120  * Function         BTA_HfCllientAudioOpen
121  *
122  * Description      Opens an audio connection to the currently connected
123  *                 audio gateway
124  *
125  *
126  * Returns          void
127  *
128  ******************************************************************************/
BTA_HfClientAudioOpen(uint16_t handle)129 void BTA_HfClientAudioOpen(uint16_t handle) {
130   BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
131 
132   p_buf->event = BTA_HF_CLIENT_API_AUDIO_OPEN_EVT;
133   p_buf->layer_specific = handle;
134 
135   bta_sys_sendmsg(p_buf);
136 }
137 
138 /*******************************************************************************
139  *
140  * Function         BTA_HfClientAudioClose
141  *
142  * Description      Close the currently active audio connection to an audio
143  *                  gateway. The data connection remains open
144  *
145  *
146  * Returns          void
147  *
148  ******************************************************************************/
BTA_HfClientAudioClose(uint16_t handle)149 void BTA_HfClientAudioClose(uint16_t handle) {
150   BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
151 
152   p_buf->event = BTA_HF_CLIENT_API_AUDIO_CLOSE_EVT;
153   p_buf->layer_specific = handle;
154 
155   bta_sys_sendmsg(p_buf);
156 }
157 
158 /*******************************************************************************
159  *
160  * Function         BTA_HfClientSendAT
161  *
162  * Description      send AT command
163  *
164  *
165  * Returns          void
166  *
167  ******************************************************************************/
BTA_HfClientSendAT(uint16_t handle,tBTA_HF_CLIENT_AT_CMD_TYPE at,uint32_t val1,uint32_t val2,const char * str)168 void BTA_HfClientSendAT(uint16_t handle, tBTA_HF_CLIENT_AT_CMD_TYPE at,
169                         uint32_t val1, uint32_t val2, const char* str) {
170   tBTA_HF_CLIENT_DATA_VAL* p_buf =
171       (tBTA_HF_CLIENT_DATA_VAL*)osi_malloc(sizeof(tBTA_HF_CLIENT_DATA_VAL));
172 
173   p_buf->hdr.event = BTA_HF_CLIENT_SEND_AT_CMD_EVT;
174   p_buf->uint8_val = at;
175   p_buf->uint32_val1 = val1;
176   p_buf->uint32_val2 = val2;
177 
178   if (str) {
179     strlcpy(p_buf->str, str, BTA_HF_CLIENT_NUMBER_LEN + 1);
180     p_buf->str[BTA_HF_CLIENT_NUMBER_LEN] = '\0';
181   } else {
182     p_buf->str[0] = '\0';
183   }
184 
185   p_buf->hdr.layer_specific = handle;
186 
187   bta_sys_sendmsg(p_buf);
188 }
189 
190 /*******************************************************************************
191  *
192  * Function         BTA_HfClientDumpStatistics
193  *
194  * Description      Dump statistics about the various control blocks
195  *                  and other relevant connection statistics
196  *
197  * Returns          Void
198  *
199  ******************************************************************************/
BTA_HfClientDumpStatistics(int fd)200 void BTA_HfClientDumpStatistics(int fd) { bta_hf_client_dump_statistics(fd); }
201