1 /******************************************************************************
2  *
3  *  Copyright (c) 2014 The Android Open Source Project
4  *  Copyright 2004-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 file contains the audio gateway functions controlling the RFCOMM
23  *  connections.
24  *
25  ******************************************************************************/
26 
27 #include <base/logging.h>
28 #include <string.h>
29 
30 #include "bt_utils.h"
31 #include "bta_api.h"
32 #include "bta_hf_client_int.h"
33 #include "osi/include/osi.h"
34 #include "port_api.h"
35 
36 /*******************************************************************************
37  *
38  * Function         bta_hf_client_port_cback
39  *
40  * Description      RFCOMM Port callback. The handle in this function is
41  *                  specified by BTA layer via the PORT_SetEventCallback
42  *                  method
43  *
44  * Returns          void
45  *
46  ******************************************************************************/
bta_hf_client_port_cback(UNUSED_ATTR uint32_t code,uint16_t port_handle)47 static void bta_hf_client_port_cback(UNUSED_ATTR uint32_t code,
48                                      uint16_t port_handle) {
49   /* ignore port events for port handles other than connected handle */
50   tBTA_HF_CLIENT_CB* client_cb =
51       bta_hf_client_find_cb_by_rfc_handle(port_handle);
52   if (client_cb == NULL) {
53     APPL_TRACE_ERROR("%s: cb not found for handle %d", __func__, port_handle);
54     return;
55   }
56 
57   tBTA_HF_CLIENT_RFC* p_buf =
58       (tBTA_HF_CLIENT_RFC*)osi_malloc(sizeof(tBTA_HF_CLIENT_RFC));
59   p_buf->hdr.event = BTA_HF_CLIENT_RFC_DATA_EVT;
60   p_buf->hdr.layer_specific = client_cb->handle;
61   bta_sys_sendmsg(p_buf);
62 }
63 
64 /*******************************************************************************
65  *
66  * Function         bta_hf_client_mgmt_cback
67  *
68  * Description      RFCOMM management callback
69  *
70  *
71  * Returns          void
72  *
73  ******************************************************************************/
bta_hf_client_mgmt_cback(uint32_t code,uint16_t port_handle)74 static void bta_hf_client_mgmt_cback(uint32_t code, uint16_t port_handle) {
75   tBTA_HF_CLIENT_CB* client_cb =
76       bta_hf_client_find_cb_by_rfc_handle(port_handle);
77 
78   APPL_TRACE_DEBUG("%s: code = %d, port_handle = %d serv = %d", __func__, code,
79                    port_handle, bta_hf_client_cb_arr.serv_handle);
80 
81   /* ignore close event for port handles other than connected handle */
82   if (code != PORT_SUCCESS && client_cb != NULL &&
83       port_handle != client_cb->conn_handle) {
84     APPL_TRACE_DEBUG("bta_hf_client_mgmt_cback ignoring handle:%d",
85                      port_handle);
86     return;
87   }
88 
89   tBTA_HF_CLIENT_RFC* p_buf =
90       (tBTA_HF_CLIENT_RFC*)osi_malloc(sizeof(tBTA_HF_CLIENT_RFC));
91 
92   if (code == PORT_SUCCESS) {
93     if (client_cb && port_handle == client_cb->conn_handle) { /* out conn */
94       p_buf->hdr.event = BTA_HF_CLIENT_RFC_OPEN_EVT;
95     } else if (port_handle == bta_hf_client_cb_arr.serv_handle) {
96       p_buf->hdr.event = BTA_HF_CLIENT_RFC_OPEN_EVT;
97 
98       APPL_TRACE_DEBUG("%s: allocating a new CB for incoming connection",
99                        __func__);
100       // Find the BDADDR of the peer device
101       RawAddress peer_addr = RawAddress::kEmpty;
102       uint16_t lcid = 0;
103       int status = PORT_CheckConnection(port_handle, &peer_addr, &lcid);
104       if (status != PORT_SUCCESS) {
105         LOG(ERROR) << __func__ << ": PORT_CheckConnection returned " << status;
106       }
107 
108       // Since we accepted a remote request we should allocate a handle first.
109       uint16_t tmp_handle = -1;
110       bta_hf_client_allocate_handle(peer_addr, &tmp_handle);
111       client_cb = bta_hf_client_find_cb_by_handle(tmp_handle);
112 
113       // If allocation fails then we abort.
114       if (client_cb == NULL) {
115         APPL_TRACE_ERROR("%s: error allocating a new handle", __func__);
116         p_buf->hdr.event = BTA_HF_CLIENT_RFC_CLOSE_EVT;
117       } else {
118         // Set the connection fields for this new CB
119         client_cb->conn_handle = port_handle;
120 
121         // Since we have accepted an incoming RFCOMM connection:
122         // a) Release the current server from it duties
123         // b) Start a new server for more new incoming connection
124         bta_hf_client_cb_arr.serv_handle = 0;
125         bta_hf_client_start_server();
126       }
127     } else {
128       APPL_TRACE_ERROR("%s: PORT_SUCCESS, ignoring handle = %d", __func__,
129                        port_handle);
130       osi_free(p_buf);
131       return;
132     }
133   } else if (client_cb != NULL &&
134              port_handle == client_cb->conn_handle) { /* code != PORT_SUC */
135     LOG(ERROR) << __func__ << ": closing port handle " << port_handle << "dev "
136                << client_cb->peer_addr;
137 
138     RFCOMM_RemoveServer(port_handle);
139     p_buf->hdr.event = BTA_HF_CLIENT_RFC_CLOSE_EVT;
140   } else if (client_cb == NULL) {
141     // client_cb is already cleaned due to hfp client disabled.
142     // Assigned a valid event value to header and send this message anyway.
143     p_buf->hdr.event = BTA_HF_CLIENT_RFC_CLOSE_EVT;
144   }
145 
146   p_buf->hdr.layer_specific = client_cb != NULL ? client_cb->handle : 0;
147   bta_sys_sendmsg(p_buf);
148 }
149 
150 /*******************************************************************************
151  *
152  * Function         bta_hf_client_setup_port
153  *
154  * Description      Setup RFCOMM port for use by HF Client.
155  *
156  *
157  * Returns          void
158  *
159  ******************************************************************************/
bta_hf_client_setup_port(uint16_t handle)160 void bta_hf_client_setup_port(uint16_t handle) {
161   PORT_SetEventMask(handle, PORT_EV_RXCHAR);
162   PORT_SetEventCallback(handle, bta_hf_client_port_cback);
163 }
164 
165 /*******************************************************************************
166  *
167  * Function         bta_hf_client_start_server
168  *
169  * Description      Setup RFCOMM server for use by HF Client.
170  *
171  *
172  * Returns          void
173  *
174  ******************************************************************************/
bta_hf_client_start_server()175 void bta_hf_client_start_server() {
176   int port_status;
177 
178   if (bta_hf_client_cb_arr.serv_handle > 0) {
179     APPL_TRACE_DEBUG("%s: already started, handle: %d", __func__,
180                      bta_hf_client_cb_arr.serv_handle);
181     return;
182   }
183 
184   BTM_SetSecurityLevel(false, "", BTM_SEC_SERVICE_HF_HANDSFREE,
185                        bta_hf_client_cb_arr.serv_sec_mask, BT_PSM_RFCOMM,
186                        BTM_SEC_PROTO_RFCOMM, bta_hf_client_cb_arr.scn);
187 
188   port_status = RFCOMM_CreateConnection(
189       UUID_SERVCLASS_HF_HANDSFREE, bta_hf_client_cb_arr.scn, true,
190       BTA_HF_CLIENT_MTU, RawAddress::kAny, &(bta_hf_client_cb_arr.serv_handle),
191       bta_hf_client_mgmt_cback);
192 
193   APPL_TRACE_DEBUG("%s: started rfcomm server with handle %d", __func__,
194                    bta_hf_client_cb_arr.serv_handle);
195 
196   if (port_status == PORT_SUCCESS) {
197     bta_hf_client_setup_port(bta_hf_client_cb_arr.serv_handle);
198   } else {
199     APPL_TRACE_DEBUG("%s: RFCOMM_CreateConnection returned error:%d", __func__,
200                      port_status);
201   }
202 }
203 
204 /*******************************************************************************
205  *
206  * Function         bta_hf_client_close_server
207  *
208  * Description      Close RFCOMM server port for use by HF Client.
209  *
210  *
211  * Returns          void
212  *
213  ******************************************************************************/
bta_hf_client_close_server()214 void bta_hf_client_close_server() {
215   APPL_TRACE_DEBUG("%s: %d", __func__, bta_hf_client_cb_arr.serv_handle);
216 
217   if (bta_hf_client_cb_arr.serv_handle == 0) {
218     APPL_TRACE_DEBUG("%s: already stopped", __func__);
219     return;
220   }
221 
222   RFCOMM_RemoveServer(bta_hf_client_cb_arr.serv_handle);
223   bta_hf_client_cb_arr.serv_handle = 0;
224 }
225 
226 /*******************************************************************************
227  *
228  * Function         bta_hf_client_rfc_do_open
229  *
230  * Description      Open an RFCOMM connection to the peer device.
231  *
232  *
233  * Returns          void
234  *
235  ******************************************************************************/
bta_hf_client_rfc_do_open(tBTA_HF_CLIENT_DATA * p_data)236 void bta_hf_client_rfc_do_open(tBTA_HF_CLIENT_DATA* p_data) {
237   tBTA_HF_CLIENT_CB* client_cb =
238       bta_hf_client_find_cb_by_handle(p_data->hdr.layer_specific);
239   if (client_cb == NULL) {
240     APPL_TRACE_ERROR("%s: cb not found for handle %d", __func__,
241                      p_data->hdr.layer_specific);
242     return;
243   }
244 
245   BTM_SetSecurityLevel(true, "", BTM_SEC_SERVICE_HF_HANDSFREE,
246                        client_cb->cli_sec_mask, BT_PSM_RFCOMM,
247                        BTM_SEC_PROTO_RFCOMM, client_cb->peer_scn);
248   if (RFCOMM_CreateConnection(UUID_SERVCLASS_HF_HANDSFREE, client_cb->peer_scn,
249                               false, BTA_HF_CLIENT_MTU, client_cb->peer_addr,
250                               &(client_cb->conn_handle),
251                               bta_hf_client_mgmt_cback) == PORT_SUCCESS) {
252     bta_hf_client_setup_port(client_cb->conn_handle);
253     APPL_TRACE_DEBUG("bta_hf_client_rfc_do_open : conn_handle = %d",
254                      client_cb->conn_handle);
255   }
256   /* RFCOMM create connection failed; send ourselves RFCOMM close event */
257   else {
258     bta_hf_client_sm_execute(BTA_HF_CLIENT_RFC_CLOSE_EVT, p_data);
259   }
260 }
261 
262 /*******************************************************************************
263  *
264  * Function         bta_hf_client_rfc_do_close
265  *
266  * Description      Close RFCOMM connection.
267  *
268  *
269  * Returns          void
270  *
271  ******************************************************************************/
bta_hf_client_rfc_do_close(tBTA_HF_CLIENT_DATA * p_data)272 void bta_hf_client_rfc_do_close(tBTA_HF_CLIENT_DATA* p_data) {
273   tBTA_HF_CLIENT_CB* client_cb =
274       bta_hf_client_find_cb_by_handle(p_data->hdr.layer_specific);
275   if (client_cb == NULL) {
276     APPL_TRACE_ERROR("%s: cb not found for handle %d", __func__,
277                      p_data->hdr.layer_specific);
278     return;
279   }
280 
281   if (client_cb->conn_handle) {
282     RFCOMM_RemoveConnection(client_cb->conn_handle);
283   } else {
284     /* Close API was called while HF Client is in Opening state.        */
285     /* Need to trigger the state machine to send callback to the app    */
286     /* and move back to INIT state.                                     */
287     tBTA_HF_CLIENT_RFC* p_buf =
288         (tBTA_HF_CLIENT_RFC*)osi_malloc(sizeof(tBTA_HF_CLIENT_RFC));
289     p_buf->hdr.event = BTA_HF_CLIENT_RFC_CLOSE_EVT;
290     bta_sys_sendmsg(p_buf);
291 
292     /* Cancel SDP if it had been started. */
293     if (client_cb->p_disc_db) {
294       (void)SDP_CancelServiceSearch(client_cb->p_disc_db);
295       osi_free_and_reset((void**)&client_cb->p_disc_db);
296     }
297   }
298 }
299