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