• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (c) 2014 The Android Open Source Project
4  *  Copyright (C) 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 <string.h>
28 
29 #include "bt_utils.h"
30 #include "bta_api.h"
31 #include "bta_hf_client_int.h"
32 #include "osi/include/osi.h"
33 #include "port_api.h"
34 
35 /*******************************************************************************
36  *
37  * Function         bta_hf_client_port_cback
38  *
39  * Description      RFCOMM Port callback. The handle in this function is
40  *                  specified by BTA layer via the PORT_SetEventCallback
41  *                  method
42  *
43  * Returns          void
44  *
45  ******************************************************************************/
bta_hf_client_port_cback(UNUSED_ATTR uint32_t code,uint16_t port_handle)46 static void bta_hf_client_port_cback(UNUSED_ATTR uint32_t code,
47                                      uint16_t port_handle) {
48   /* ignore port events for port handles other than connected handle */
49   tBTA_HF_CLIENT_CB* client_cb =
50       bta_hf_client_find_cb_by_rfc_handle(port_handle);
51   if (client_cb == NULL) {
52     APPL_TRACE_ERROR("%s: cb not found for handle %d", __func__, port_handle);
53     return;
54   }
55 
56   tBTA_HF_CLIENT_RFC* p_buf =
57       (tBTA_HF_CLIENT_RFC*)osi_malloc(sizeof(tBTA_HF_CLIENT_RFC));
58   p_buf->hdr.event = BTA_HF_CLIENT_RFC_DATA_EVT;
59   p_buf->hdr.layer_specific = client_cb->handle;
60   bta_sys_sendmsg(p_buf);
61 }
62 
63 /*******************************************************************************
64  *
65  * Function         bta_hf_client_mgmt_cback
66  *
67  * Description      RFCOMM management callback
68  *
69  *
70  * Returns          void
71  *
72  ******************************************************************************/
bta_hf_client_mgmt_cback(uint32_t code,uint16_t port_handle)73 static void bta_hf_client_mgmt_cback(uint32_t code, uint16_t port_handle) {
74   tBTA_HF_CLIENT_CB* client_cb =
75       bta_hf_client_find_cb_by_rfc_handle(port_handle);
76 
77   APPL_TRACE_DEBUG("%s: code = %d, port_handle = %d serv = %d", __func__, code,
78                    port_handle, bta_hf_client_cb_arr.serv_handle);
79 
80   /* ignore close event for port handles other than connected handle */
81   if (code != PORT_SUCCESS && client_cb != NULL &&
82       port_handle != client_cb->conn_handle) {
83     APPL_TRACE_DEBUG("bta_hf_client_mgmt_cback ignoring handle:%d",
84                      port_handle);
85     return;
86   }
87 
88   tBTA_HF_CLIENT_RFC* p_buf =
89       (tBTA_HF_CLIENT_RFC*)osi_malloc(sizeof(tBTA_HF_CLIENT_RFC));
90 
91   if (code == PORT_SUCCESS) {
92     if (client_cb && port_handle == client_cb->conn_handle) { /* out conn */
93       p_buf->hdr.event = BTA_HF_CLIENT_RFC_OPEN_EVT;
94     } else if (port_handle == bta_hf_client_cb_arr.serv_handle) {
95       p_buf->hdr.event = BTA_HF_CLIENT_RFC_OPEN_EVT;
96 
97       APPL_TRACE_DEBUG("%s: allocating a new CB for incoming connection",
98                        __func__);
99       // Find the BDADDR of the peer device
100       BD_ADDR peer_addr;
101       uint16_t lcid;
102       PORT_CheckConnection(port_handle, peer_addr, &lcid);
103 
104       // Since we accepted a remote request we should allocate a handle first.
105       uint16_t tmp_handle = -1;
106       bta_hf_client_allocate_handle(peer_addr, &tmp_handle);
107       client_cb = bta_hf_client_find_cb_by_handle(tmp_handle);
108 
109       // If allocation fails then we abort.
110       if (client_cb == NULL) {
111         APPL_TRACE_ERROR("%s: error allocating a new handle", __func__);
112         p_buf->hdr.event = BTA_HF_CLIENT_RFC_CLOSE_EVT;
113       } else {
114         // Set the connection fields for this new CB
115         client_cb->conn_handle = port_handle;
116 
117         // Since we have accepted an incoming RFCOMM connection:
118         // a) Release the current server from it duties
119         // b) Start a new server for more new incoming connection
120         bta_hf_client_cb_arr.serv_handle = 0;
121         bta_hf_client_start_server();
122       }
123     } else {
124       APPL_TRACE_ERROR("%s: PORT_SUCCESS, ignoring handle = %d", __func__,
125                        port_handle);
126       return;
127     }
128   } else if (client_cb != NULL &&
129              port_handle == client_cb->conn_handle) { /* code != PORT_SUC */
130     APPL_TRACE_ERROR(
131         "%s: closing port handle %d "
132         "dev %02x:%02x:%02x:%02x:%02x:%02x",
133         __func__, port_handle, client_cb->peer_addr[0], client_cb->peer_addr[1],
134         client_cb->peer_addr[2], client_cb->peer_addr[3],
135         client_cb->peer_addr[4], client_cb->peer_addr[5]);
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, (uint8_t*)bd_addr_any,
186       &(bta_hf_client_cb_arr.serv_handle), 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       bta_hf_client_free_db(NULL);
291     }
292   }
293 }
294