• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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       return;
131     }
132   } else if (client_cb != NULL &&
133              port_handle == client_cb->conn_handle) { /* code != PORT_SUC */
134     LOG(ERROR) << __func__ << ": closing port handle " << port_handle << "dev "
135                << client_cb->peer_addr;
136 
137     RFCOMM_RemoveServer(port_handle);
138     p_buf->hdr.event = BTA_HF_CLIENT_RFC_CLOSE_EVT;
139   }
140 
141   p_buf->hdr.layer_specific = client_cb != NULL ? client_cb->handle : 0;
142   bta_sys_sendmsg(p_buf);
143 }
144 
145 /*******************************************************************************
146  *
147  * Function         bta_hf_client_setup_port
148  *
149  * Description      Setup RFCOMM port for use by HF Client.
150  *
151  *
152  * Returns          void
153  *
154  ******************************************************************************/
bta_hf_client_setup_port(uint16_t handle)155 void bta_hf_client_setup_port(uint16_t handle) {
156   PORT_SetEventMask(handle, PORT_EV_RXCHAR);
157   PORT_SetEventCallback(handle, bta_hf_client_port_cback);
158 }
159 
160 /*******************************************************************************
161  *
162  * Function         bta_hf_client_start_server
163  *
164  * Description      Setup RFCOMM server for use by HF Client.
165  *
166  *
167  * Returns          void
168  *
169  ******************************************************************************/
bta_hf_client_start_server()170 void bta_hf_client_start_server() {
171   int port_status;
172 
173   if (bta_hf_client_cb_arr.serv_handle > 0) {
174     APPL_TRACE_DEBUG("%s: already started, handle: %d", __func__,
175                      bta_hf_client_cb_arr.serv_handle);
176     return;
177   }
178 
179   BTM_SetSecurityLevel(false, "", BTM_SEC_SERVICE_HF_HANDSFREE,
180                        bta_hf_client_cb_arr.serv_sec_mask, BT_PSM_RFCOMM,
181                        BTM_SEC_PROTO_RFCOMM, bta_hf_client_cb_arr.scn);
182 
183   port_status = RFCOMM_CreateConnection(
184       UUID_SERVCLASS_HF_HANDSFREE, bta_hf_client_cb_arr.scn, true,
185       BTA_HF_CLIENT_MTU, RawAddress::kAny, &(bta_hf_client_cb_arr.serv_handle),
186       bta_hf_client_mgmt_cback);
187 
188   APPL_TRACE_DEBUG("%s: started rfcomm server with handle %d", __func__,
189                    bta_hf_client_cb_arr.serv_handle);
190 
191   if (port_status == PORT_SUCCESS) {
192     bta_hf_client_setup_port(bta_hf_client_cb_arr.serv_handle);
193   } else {
194     APPL_TRACE_DEBUG("%s: RFCOMM_CreateConnection returned error:%d", __func__,
195                      port_status);
196   }
197 }
198 
199 /*******************************************************************************
200  *
201  * Function         bta_hf_client_close_server
202  *
203  * Description      Close RFCOMM server port for use by HF Client.
204  *
205  *
206  * Returns          void
207  *
208  ******************************************************************************/
bta_hf_client_close_server()209 void bta_hf_client_close_server() {
210   APPL_TRACE_DEBUG("%s: %d", __func__, bta_hf_client_cb_arr.serv_handle);
211 
212   if (bta_hf_client_cb_arr.serv_handle == 0) {
213     APPL_TRACE_DEBUG("%s: already stopped", __func__);
214     return;
215   }
216 
217   RFCOMM_RemoveServer(bta_hf_client_cb_arr.serv_handle);
218   bta_hf_client_cb_arr.serv_handle = 0;
219 }
220 
221 /*******************************************************************************
222  *
223  * Function         bta_hf_client_rfc_do_open
224  *
225  * Description      Open an RFCOMM connection to the peer device.
226  *
227  *
228  * Returns          void
229  *
230  ******************************************************************************/
bta_hf_client_rfc_do_open(tBTA_HF_CLIENT_DATA * p_data)231 void bta_hf_client_rfc_do_open(tBTA_HF_CLIENT_DATA* p_data) {
232   tBTA_HF_CLIENT_CB* client_cb =
233       bta_hf_client_find_cb_by_handle(p_data->hdr.layer_specific);
234   if (client_cb == NULL) {
235     APPL_TRACE_ERROR("%s: cb not found for handle %d", __func__,
236                      p_data->hdr.layer_specific);
237     return;
238   }
239 
240   BTM_SetSecurityLevel(true, "", BTM_SEC_SERVICE_HF_HANDSFREE,
241                        client_cb->cli_sec_mask, BT_PSM_RFCOMM,
242                        BTM_SEC_PROTO_RFCOMM, client_cb->peer_scn);
243   if (RFCOMM_CreateConnection(UUID_SERVCLASS_HF_HANDSFREE, client_cb->peer_scn,
244                               false, BTA_HF_CLIENT_MTU, client_cb->peer_addr,
245                               &(client_cb->conn_handle),
246                               bta_hf_client_mgmt_cback) == PORT_SUCCESS) {
247     bta_hf_client_setup_port(client_cb->conn_handle);
248     APPL_TRACE_DEBUG("bta_hf_client_rfc_do_open : conn_handle = %d",
249                      client_cb->conn_handle);
250   }
251   /* RFCOMM create connection failed; send ourselves RFCOMM close event */
252   else {
253     bta_hf_client_sm_execute(BTA_HF_CLIENT_RFC_CLOSE_EVT, p_data);
254   }
255 }
256 
257 /*******************************************************************************
258  *
259  * Function         bta_hf_client_rfc_do_close
260  *
261  * Description      Close RFCOMM connection.
262  *
263  *
264  * Returns          void
265  *
266  ******************************************************************************/
bta_hf_client_rfc_do_close(tBTA_HF_CLIENT_DATA * p_data)267 void bta_hf_client_rfc_do_close(tBTA_HF_CLIENT_DATA* p_data) {
268   tBTA_HF_CLIENT_CB* client_cb =
269       bta_hf_client_find_cb_by_handle(p_data->hdr.layer_specific);
270   if (client_cb == NULL) {
271     APPL_TRACE_ERROR("%s: cb not found for handle %d", __func__,
272                      p_data->hdr.layer_specific);
273     return;
274   }
275 
276   if (client_cb->conn_handle) {
277     RFCOMM_RemoveConnection(client_cb->conn_handle);
278   } else {
279     /* Close API was called while HF Client is in Opening state.        */
280     /* Need to trigger the state machine to send callback to the app    */
281     /* and move back to INIT state.                                     */
282     tBTA_HF_CLIENT_RFC* p_buf =
283         (tBTA_HF_CLIENT_RFC*)osi_malloc(sizeof(tBTA_HF_CLIENT_RFC));
284     p_buf->hdr.event = BTA_HF_CLIENT_RFC_CLOSE_EVT;
285     bta_sys_sendmsg(p_buf);
286 
287     /* Cancel SDP if it had been started. */
288     if (client_cb->p_disc_db) {
289       (void)SDP_CancelServiceSearch(client_cb->p_disc_db);
290       osi_free_and_reset((void**)&client_cb->p_disc_db);
291     }
292   }
293 }
294