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
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 RawAddress peer_addr = RawAddress::kEmpty;
101 uint16_t lcid = 0;
102 int status = PORT_CheckConnection(port_handle, &peer_addr, &lcid);
103 if (status != PORT_SUCCESS) {
104 LOG(ERROR) << __func__ << ": PORT_CheckConnection returned " << status;
105 }
106
107 // Since we accepted a remote request we should allocate a handle first.
108 uint16_t tmp_handle = -1;
109 bta_hf_client_allocate_handle(peer_addr, &tmp_handle);
110 client_cb = bta_hf_client_find_cb_by_handle(tmp_handle);
111
112 // If allocation fails then we abort.
113 if (client_cb == NULL) {
114 APPL_TRACE_ERROR("%s: error allocating a new handle", __func__);
115 p_buf->hdr.event = BTA_HF_CLIENT_RFC_CLOSE_EVT;
116 } else {
117 // Set the connection fields for this new CB
118 client_cb->conn_handle = port_handle;
119
120 // Since we have accepted an incoming RFCOMM connection:
121 // a) Release the current server from it duties
122 // b) Start a new server for more new incoming connection
123 bta_hf_client_cb_arr.serv_handle = 0;
124 bta_hf_client_start_server();
125 }
126 } else {
127 APPL_TRACE_ERROR("%s: PORT_SUCCESS, ignoring handle = %d", __func__,
128 port_handle);
129 osi_free(p_buf);
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 } else if (client_cb == NULL) {
140 // client_cb is already cleaned due to hfp client disabled.
141 // Assigned a valid event value to header and send this message anyway.
142 p_buf->hdr.event = BTA_HF_CLIENT_RFC_CLOSE_EVT;
143 }
144
145 p_buf->hdr.layer_specific = client_cb != NULL ? client_cb->handle : 0;
146 bta_sys_sendmsg(p_buf);
147 }
148
149 /*******************************************************************************
150 *
151 * Function bta_hf_client_setup_port
152 *
153 * Description Setup RFCOMM port for use by HF Client.
154 *
155 *
156 * Returns void
157 *
158 ******************************************************************************/
bta_hf_client_setup_port(uint16_t handle)159 void bta_hf_client_setup_port(uint16_t handle) {
160 PORT_SetEventMask(handle, PORT_EV_RXCHAR);
161 PORT_SetEventCallback(handle, bta_hf_client_port_cback);
162 }
163
164 /*******************************************************************************
165 *
166 * Function bta_hf_client_start_server
167 *
168 * Description Setup RFCOMM server for use by HF Client.
169 *
170 *
171 * Returns void
172 *
173 ******************************************************************************/
bta_hf_client_start_server()174 void bta_hf_client_start_server() {
175 int port_status;
176
177 if (bta_hf_client_cb_arr.serv_handle > 0) {
178 APPL_TRACE_DEBUG("%s: already started, handle: %d", __func__,
179 bta_hf_client_cb_arr.serv_handle);
180 return;
181 }
182
183 port_status = RFCOMM_CreateConnectionWithSecurity(
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, BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT);
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 if (RFCOMM_CreateConnectionWithSecurity(
241 UUID_SERVCLASS_HF_HANDSFREE, client_cb->peer_scn, false,
242 BTA_HF_CLIENT_MTU, client_cb->peer_addr, &(client_cb->conn_handle),
243 bta_hf_client_mgmt_cback,
244 BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT) == PORT_SUCCESS) {
245 bta_hf_client_setup_port(client_cb->conn_handle);
246 APPL_TRACE_DEBUG("bta_hf_client_rfc_do_open : conn_handle = %d",
247 client_cb->conn_handle);
248 }
249 /* RFCOMM create connection failed; send ourselves RFCOMM close event */
250 else {
251 bta_hf_client_sm_execute(BTA_HF_CLIENT_RFC_CLOSE_EVT, p_data);
252 }
253 }
254
255 /*******************************************************************************
256 *
257 * Function bta_hf_client_rfc_do_close
258 *
259 * Description Close RFCOMM connection.
260 *
261 *
262 * Returns void
263 *
264 ******************************************************************************/
bta_hf_client_rfc_do_close(tBTA_HF_CLIENT_DATA * p_data)265 void bta_hf_client_rfc_do_close(tBTA_HF_CLIENT_DATA* p_data) {
266 tBTA_HF_CLIENT_CB* client_cb =
267 bta_hf_client_find_cb_by_handle(p_data->hdr.layer_specific);
268 if (client_cb == NULL) {
269 APPL_TRACE_ERROR("%s: cb not found for handle %d", __func__,
270 p_data->hdr.layer_specific);
271 return;
272 }
273
274 if (client_cb->conn_handle) {
275 RFCOMM_RemoveConnection(client_cb->conn_handle);
276 } else {
277 /* Close API was called while HF Client is in Opening state. */
278 /* Need to trigger the state machine to send callback to the app */
279 /* and move back to INIT state. */
280 tBTA_HF_CLIENT_RFC* p_buf =
281 (tBTA_HF_CLIENT_RFC*)osi_malloc(sizeof(tBTA_HF_CLIENT_RFC));
282 p_buf->hdr.event = BTA_HF_CLIENT_RFC_CLOSE_EVT;
283 bta_sys_sendmsg(p_buf);
284
285 /* Cancel SDP if it had been started. */
286 if (client_cb->p_disc_db) {
287 (void)SDP_CancelServiceSearch(client_cb->p_disc_db);
288 osi_free_and_reset((void**)&client_cb->p_disc_db);
289 }
290 }
291 }
292