1 /******************************************************************************
2 *
3 * Copyright 2006-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 *
21 * This file contains action functions for BTA JV APIs.
22 *
23 ******************************************************************************/
24
25 #define LOG_TAG "bluetooth"
26
27 #include <bluetooth/log.h>
28 #include <com_android_bluetooth_flags.h>
29
30 #include <cstdint>
31 #include <unordered_set>
32
33 #include "bta/include/bta_jv_co.h"
34 #include "bta/include/bta_rfcomm_scn.h"
35 #include "bta/jv/bta_jv_int.h"
36 #include "bta/sys/bta_sys.h"
37 #include "internal_include/bt_target.h"
38 #include "internal_include/bt_trace.h"
39 #include "os/logging/log_adapter.h"
40 #include "osi/include/allocator.h"
41 #include "osi/include/properties.h"
42 #include "stack/btm/btm_sec.h"
43 #include "stack/include/avct_api.h" // AVCT_PSM
44 #include "stack/include/avdt_api.h" // AVDT_PSM
45 #include "stack/include/bt_hdr.h"
46 #include "stack/include/bt_psm_types.h"
47 #include "stack/include/bt_types.h"
48 #include "stack/include/bt_uuid16.h"
49 #include "stack/include/btm_client_interface.h"
50 #include "stack/include/gap_api.h"
51 #include "stack/include/l2cdefs.h"
52 #include "stack/include/port_api.h"
53 #include "stack/include/sdp_api.h"
54 #include "types/bluetooth/uuid.h"
55 #include "types/raw_address.h"
56
57 using namespace bluetooth::legacy::stack::sdp;
58 using namespace bluetooth;
59
60 tBTA_JV_CB bta_jv_cb;
61 std::unordered_set<uint16_t> used_l2cap_classic_dynamic_psm;
62
63 static tBTA_JV_PCB* bta_jv_add_rfc_port(tBTA_JV_RFC_CB* p_cb,
64 tBTA_JV_PCB* p_pcb_open);
65 static tBTA_JV_STATUS bta_jv_free_set_pm_profile_cb(uint32_t jv_handle);
66 static void bta_jv_pm_conn_busy(tBTA_JV_PM_CB* p_cb);
67 static void bta_jv_pm_conn_idle(tBTA_JV_PM_CB* p_cb);
68 static void bta_jv_pm_state_change(tBTA_JV_PM_CB* p_cb,
69 const tBTA_JV_CONN_STATE state);
70 static void bta_jv_reset_sniff_timer(tBTA_JV_PM_CB* p_cb);
71
72 #ifndef BTA_JV_SDP_DB_SIZE
73 #define BTA_JV_SDP_DB_SIZE 4500
74 #endif
75
76 #ifndef BTA_JV_SDP_RAW_DATA_SIZE
77 #define BTA_JV_SDP_RAW_DATA_SIZE 1800
78 #endif
79
80 static uint8_t bta_jv_sdp_raw_data[BTA_JV_SDP_RAW_DATA_SIZE];
81 static tSDP_DISCOVERY_DB
82 bta_jv_sdp_db_data[BTA_JV_SDP_DB_SIZE / sizeof(tSDP_DISCOVERY_DB)];
83
84 /* JV configuration structure */
85 struct tBTA_JV_CFG {
86 uint16_t sdp_raw_size; /* The size of p_sdp_raw_data */
87 uint16_t sdp_db_size; /* The size of p_sdp_db */
88 uint8_t* p_sdp_raw_data; /* The data buffer to keep raw data */
89 tSDP_DISCOVERY_DB* p_sdp_db; /* The data buffer to keep SDP database */
90 } bta_jv_cfg = {
91 BTA_JV_SDP_RAW_DATA_SIZE, /* The size of p_sdp_raw_data */
92 (BTA_JV_SDP_DB_SIZE / sizeof(tSDP_DISCOVERY_DB)) *
93 sizeof(tSDP_DISCOVERY_DB), /* The size of p_sdp_db_data */
94 bta_jv_sdp_raw_data, /* The data buffer to keep raw data */
95 bta_jv_sdp_db_data /* The data buffer to keep SDP database */
96 };
97
98 tBTA_JV_CFG* p_bta_jv_cfg = &bta_jv_cfg;
99
100 /*******************************************************************************
101 *
102 * Function bta_jv_alloc_sec_id
103 *
104 * Description allocate a security id
105 *
106 * Returns
107 *
108 ******************************************************************************/
bta_jv_alloc_sec_id(void)109 uint8_t bta_jv_alloc_sec_id(void) {
110 uint8_t ret = 0;
111 int i;
112 for (i = 0; i < BTA_JV_NUM_SERVICE_ID; i++) {
113 if (0 == bta_jv_cb.sec_id[i]) {
114 bta_jv_cb.sec_id[i] = BTA_JV_FIRST_SERVICE_ID + i;
115 ret = bta_jv_cb.sec_id[i];
116 break;
117 }
118 }
119 return ret;
120 }
get_sec_id_used(void)121 static int get_sec_id_used(void) {
122 int i;
123 int used = 0;
124 for (i = 0; i < BTA_JV_NUM_SERVICE_ID; i++) {
125 if (bta_jv_cb.sec_id[i]) used++;
126 }
127 if (used == BTA_JV_NUM_SERVICE_ID)
128 log::error("sec id exceeds the limit={}", BTA_JV_NUM_SERVICE_ID);
129 return used;
130 }
get_rfc_cb_used(void)131 static int get_rfc_cb_used(void) {
132 int i;
133 int used = 0;
134 for (i = 0; i < BTA_JV_MAX_RFC_CONN; i++) {
135 if (bta_jv_cb.rfc_cb[i].handle) used++;
136 }
137 if (used == BTA_JV_MAX_RFC_CONN)
138 log::error("rfc ctrl block exceeds the limit={}", BTA_JV_MAX_RFC_CONN);
139 return used;
140 }
141
142 /*******************************************************************************
143 *
144 * Function bta_jv_free_sec_id
145 *
146 * Description free the given security id
147 *
148 * Returns
149 *
150 ******************************************************************************/
bta_jv_free_sec_id(uint8_t * p_sec_id)151 static void bta_jv_free_sec_id(uint8_t* p_sec_id) {
152 uint8_t sec_id = *p_sec_id;
153 *p_sec_id = 0;
154 if (sec_id >= BTA_JV_FIRST_SERVICE_ID && sec_id <= BTA_JV_LAST_SERVICE_ID) {
155 BTM_SecClrService(sec_id);
156 bta_jv_cb.sec_id[sec_id - BTA_JV_FIRST_SERVICE_ID] = 0;
157 }
158 }
159
160 /*******************************************************************************
161 *
162 * Function bta_jv_from_gap_l2cap_err
163 *
164 * Description Convert the L2CAP error result propagated from GAP to BTA JV
165 * L2CAP close reason code.
166 *
167 * Params l2cap_result: The L2CAP result propagated from GAP error.
168 *
169 * Returns Appropriate l2cap error reason value
170 * or BTA_JV_L2CAP_REASON_UNKNOWN if reason isn't defined yet.
171 *
172 ******************************************************************************/
bta_jv_from_gap_l2cap_err(uint16_t l2cap_result)173 static tBTA_JV_L2CAP_REASON bta_jv_from_gap_l2cap_err(uint16_t l2cap_result) {
174 switch (l2cap_result) {
175 case L2CAP_CONN_ACL_CONNECTION_FAILED:
176 return BTA_JV_L2CAP_REASON_ACL_FAILURE;
177 case L2CAP_CONN_CLIENT_SECURITY_CLEARANCE_FAILED:
178 return BTA_JV_L2CAP_REASON_CL_SEC_FAILURE;
179 case L2CAP_CONN_INSUFFICIENT_AUTHENTICATION:
180 return BTA_JV_L2CAP_REASON_INSUFFICIENT_AUTHENTICATION;
181 case L2CAP_CONN_INSUFFICIENT_AUTHORIZATION:
182 return BTA_JV_L2CAP_REASON_INSUFFICIENT_AUTHORIZATION;
183 case L2CAP_CONN_INSUFFICIENT_ENCRYP_KEY_SIZE:
184 return BTA_JV_L2CAP_REASON_INSUFFICIENT_ENCRYP_KEY_SIZE;
185 case L2CAP_CONN_INSUFFICIENT_ENCRYP:
186 return BTA_JV_L2CAP_REASON_INSUFFICIENT_ENCRYP;
187 case L2CAP_CONN_INVALID_SOURCE_CID:
188 return BTA_JV_L2CAP_REASON_INVALID_SOURCE_CID;
189 case L2CAP_CONN_SOURCE_CID_ALREADY_ALLOCATED:
190 return BTA_JV_L2CAP_REASON_SOURCE_CID_ALREADY_ALLOCATED;
191 case L2CAP_CONN_UNACCEPTABLE_PARAMETERS:
192 return BTA_JV_L2CAP_REASON_UNACCEPTABLE_PARAMETERS;
193 case L2CAP_CONN_INVALID_PARAMETERS:
194 return BTA_JV_L2CAP_REASON_INVALID_PARAMETERS;
195 case L2CAP_CONN_NO_RESOURCES:
196 return BTA_JV_L2CAP_REASON_NO_RESOURCES;
197 case L2CAP_CONN_NO_PSM:
198 return BTA_JV_L2CAP_REASON_NO_PSM;
199 case L2CAP_CONN_TIMEOUT:
200 return BTA_JV_L2CAP_REASON_TIMEOUT;
201 default:
202 return BTA_JV_L2CAP_REASON_UNKNOWN;
203 }
204 }
205 /******************************************************************************/
206
207 /*******************************************************************************
208 *
209 * Function bta_jv_alloc_rfc_cb
210 *
211 * Description allocate a control block for the given port handle
212 *
213 * Returns
214 *
215 ******************************************************************************/
bta_jv_alloc_rfc_cb(uint16_t port_handle,tBTA_JV_PCB ** pp_pcb)216 tBTA_JV_RFC_CB* bta_jv_alloc_rfc_cb(uint16_t port_handle,
217 tBTA_JV_PCB** pp_pcb) {
218 tBTA_JV_RFC_CB* p_cb = NULL;
219 tBTA_JV_PCB* p_pcb;
220 int i, j;
221 for (i = 0; i < BTA_JV_MAX_RFC_CONN; i++) {
222 if (0 == bta_jv_cb.rfc_cb[i].handle) {
223 p_cb = &bta_jv_cb.rfc_cb[i];
224 /* mask handle to distinguish it with L2CAP handle */
225 p_cb->handle = (i + 1) | BTA_JV_RFCOMM_MASK;
226
227 p_cb->max_sess = 1;
228 p_cb->curr_sess = 1;
229 for (j = 0; j < BTA_JV_MAX_RFC_SR_SESSION; j++) p_cb->rfc_hdl[j] = 0;
230 p_cb->rfc_hdl[0] = port_handle;
231 log::verbose("port_handle={}, handle=0x{:x}", port_handle, p_cb->handle);
232
233 p_pcb = &bta_jv_cb.port_cb[port_handle - 1];
234 p_pcb->handle = p_cb->handle;
235 p_pcb->port_handle = port_handle;
236 p_pcb->p_pm_cb = NULL;
237 *pp_pcb = p_pcb;
238 break;
239 }
240 }
241 if (p_cb == NULL) {
242 log::error("port_handle={} ctrl block exceeds limit:{}", port_handle,
243 BTA_JV_MAX_RFC_CONN);
244 }
245 return p_cb;
246 }
247
248 /*******************************************************************************
249 *
250 * Function bta_jv_rfc_port_to_pcb
251 *
252 * Description find the port control block associated with the given port
253 * handle
254 *
255 * Returns
256 *
257 ******************************************************************************/
bta_jv_rfc_port_to_pcb(uint16_t port_handle)258 tBTA_JV_PCB* bta_jv_rfc_port_to_pcb(uint16_t port_handle) {
259 tBTA_JV_PCB* p_pcb = NULL;
260
261 if ((port_handle > 0) && (port_handle <= MAX_RFC_PORTS) &&
262 bta_jv_cb.port_cb[port_handle - 1].handle) {
263 p_pcb = &bta_jv_cb.port_cb[port_handle - 1];
264 }
265
266 return p_pcb;
267 }
268
269 /*******************************************************************************
270 *
271 * Function bta_jv_rfc_port_to_cb
272 *
273 * Description find the RFCOMM control block associated with the given port
274 * handle
275 *
276 * Returns
277 *
278 ******************************************************************************/
bta_jv_rfc_port_to_cb(uint16_t port_handle)279 tBTA_JV_RFC_CB* bta_jv_rfc_port_to_cb(uint16_t port_handle) {
280 tBTA_JV_RFC_CB* p_cb = NULL;
281 uint32_t handle;
282
283 if ((port_handle > 0) && (port_handle <= MAX_RFC_PORTS) &&
284 bta_jv_cb.port_cb[port_handle - 1].handle) {
285 handle = bta_jv_cb.port_cb[port_handle - 1].handle;
286 handle &= BTA_JV_RFC_HDL_MASK;
287 handle &= ~BTA_JV_RFCOMM_MASK;
288 if (handle) p_cb = &bta_jv_cb.rfc_cb[handle - 1];
289 } else {
290 log::warn("jv handle not found port_handle:{}", port_handle);
291 }
292 return p_cb;
293 }
294
bta_jv_free_rfc_cb(tBTA_JV_RFC_CB * p_cb,tBTA_JV_PCB * p_pcb)295 static tBTA_JV_STATUS bta_jv_free_rfc_cb(tBTA_JV_RFC_CB* p_cb,
296 tBTA_JV_PCB* p_pcb) {
297 tBTA_JV_STATUS status = tBTA_JV_STATUS::SUCCESS;
298 bool remove_server = false;
299 int close_pending = 0;
300
301 if (!p_cb || !p_pcb) {
302 log::error("p_cb or p_pcb cannot be null");
303 return tBTA_JV_STATUS::FAILURE;
304 }
305 log::verbose(
306 "max_sess={}, curr_sess={}, p_pcb={}, user={}, state={}, jv "
307 "handle=0x{:x}",
308 p_cb->max_sess, p_cb->curr_sess, fmt::ptr(p_pcb), p_pcb->rfcomm_slot_id,
309 p_pcb->state, p_pcb->handle);
310
311 if (p_cb->curr_sess <= 0) return tBTA_JV_STATUS::SUCCESS;
312
313 switch (p_pcb->state) {
314 case BTA_JV_ST_CL_CLOSING:
315 case BTA_JV_ST_SR_CLOSING:
316 log::warn(
317 "return on closing, port state={}, scn={}, p_pcb={}, user_data={}",
318 p_pcb->state, p_cb->scn, fmt::ptr(p_pcb), p_pcb->rfcomm_slot_id);
319 status = tBTA_JV_STATUS::FAILURE;
320 return status;
321 case BTA_JV_ST_CL_OPEN:
322 case BTA_JV_ST_CL_OPENING:
323 log::verbose("state={}, scn={}, user_data={}", p_pcb->state, p_cb->scn,
324 p_pcb->rfcomm_slot_id);
325 p_pcb->state = BTA_JV_ST_CL_CLOSING;
326 break;
327 case BTA_JV_ST_SR_LISTEN:
328 p_pcb->state = BTA_JV_ST_SR_CLOSING;
329 remove_server = true;
330 log::verbose("state: BTA_JV_ST_SR_LISTEN, scn={}, user_data={}",
331 p_cb->scn, p_pcb->rfcomm_slot_id);
332 break;
333 case BTA_JV_ST_SR_OPEN:
334 p_pcb->state = BTA_JV_ST_SR_CLOSING;
335 log::verbose(": state: BTA_JV_ST_SR_OPEN, scn={} user_data={}", p_cb->scn,
336 p_pcb->rfcomm_slot_id);
337 break;
338 default:
339 log::warn(
340 "failed, ignore port state= {}, scn={}, p_pcb= {}, jv handle=0x{:x}, "
341 "port_handle={}, user_data={}",
342 p_pcb->state, p_cb->scn, fmt::ptr(p_pcb), p_pcb->handle,
343 p_pcb->port_handle, p_pcb->rfcomm_slot_id);
344 status = tBTA_JV_STATUS::FAILURE;
345 break;
346 }
347 if (tBTA_JV_STATUS::SUCCESS == status) {
348 int port_status;
349
350 if (!remove_server)
351 port_status = RFCOMM_RemoveConnection(p_pcb->port_handle);
352 else
353 port_status = RFCOMM_RemoveServer(p_pcb->port_handle);
354 if (port_status != PORT_SUCCESS) {
355 status = tBTA_JV_STATUS::FAILURE;
356 log::warn(
357 "Remove jv handle=0x{:x}, state={}, port_status={}, port_handle={}, "
358 "close_pending={}",
359 p_pcb->handle, p_pcb->state, port_status, p_pcb->port_handle,
360 close_pending);
361 }
362 }
363 if (!close_pending) {
364 p_pcb->port_handle = 0;
365 p_pcb->state = BTA_JV_ST_NONE;
366 bta_jv_free_set_pm_profile_cb(p_pcb->handle);
367
368 // Initialize congestion flags
369 p_pcb->cong = false;
370 p_pcb->rfcomm_slot_id = 0;
371 int si = BTA_JV_RFC_HDL_TO_SIDX(p_pcb->handle);
372 if (0 <= si && si < BTA_JV_MAX_RFC_SR_SESSION) p_cb->rfc_hdl[si] = 0;
373 p_pcb->handle = 0;
374 p_cb->curr_sess--;
375 if (p_cb->curr_sess == 0) {
376 p_cb->scn = 0;
377 p_cb->p_cback = NULL;
378 p_cb->handle = 0;
379 p_cb->curr_sess = -1;
380 }
381 }
382 return status;
383 }
384
385 /*******************************************************************************
386 *
387 * Function bta_jv_free_l2c_cb
388 *
389 * Description free the given L2CAP control block
390 *
391 * Returns
392 *
393 ******************************************************************************/
bta_jv_free_l2c_cb(tBTA_JV_L2C_CB * p_cb)394 tBTA_JV_STATUS bta_jv_free_l2c_cb(tBTA_JV_L2C_CB* p_cb) {
395 tBTA_JV_STATUS status = tBTA_JV_STATUS::SUCCESS;
396
397 if (BTA_JV_ST_NONE != p_cb->state) {
398 bta_jv_free_set_pm_profile_cb((uint32_t)p_cb->handle);
399 if (GAP_ConnClose(p_cb->handle) != BT_PASS)
400 status = tBTA_JV_STATUS::FAILURE;
401 }
402 p_cb->psm = 0;
403 p_cb->state = BTA_JV_ST_NONE;
404 p_cb->cong = false;
405 bta_jv_free_sec_id(&p_cb->sec_id);
406 p_cb->p_cback = NULL;
407 p_cb->handle = 0;
408 p_cb->l2cap_socket_id = 0;
409 return status;
410 }
411
412 /*******************************************************************************
413 *
414 *
415 * Function bta_jv_clear_pm_cb
416 *
417 * Description clears jv pm control block and optionally calls
418 * bta_sys_conn_close()
419 * In general close_conn should be set to true to remove registering
420 * with dm pm!
421 *
422 * WARNING: Make sure to clear pointer form port or l2c to this control block
423 * too!
424 *
425 ******************************************************************************/
bta_jv_clear_pm_cb(tBTA_JV_PM_CB * p_pm_cb,bool close_conn)426 static void bta_jv_clear_pm_cb(tBTA_JV_PM_CB* p_pm_cb, bool close_conn) {
427 /* needs to be called if registered with bta pm, otherwise we may run out of
428 * dm pm slots! */
429 if (close_conn)
430 bta_sys_conn_close(BTA_ID_JV, p_pm_cb->app_id, p_pm_cb->peer_bd_addr);
431 p_pm_cb->state = BTA_JV_PM_FREE_ST;
432 p_pm_cb->app_id = BTA_JV_PM_ALL;
433 p_pm_cb->handle = BTA_JV_PM_HANDLE_CLEAR;
434 p_pm_cb->peer_bd_addr = RawAddress::kEmpty;
435 }
436
437 /*******************************************************************************
438 *
439 * Function bta_jv_free_set_pm_profile_cb
440 *
441 * Description free pm profile control block
442 *
443 * Returns tBTA_JV_STATUS::SUCCESS if cb has been freed correctly,
444 * tBTA_JV_STATUS::FAILURE in case of no profile has been registered
445 * or already freed
446 *
447 ******************************************************************************/
bta_jv_free_set_pm_profile_cb(uint32_t jv_handle)448 static tBTA_JV_STATUS bta_jv_free_set_pm_profile_cb(uint32_t jv_handle) {
449 tBTA_JV_STATUS status = tBTA_JV_STATUS::FAILURE;
450 tBTA_JV_PM_CB** p_cb;
451 int i, j, bd_counter = 0, appid_counter = 0;
452
453 for (i = 0; i < BTA_JV_PM_MAX_NUM; i++) {
454 p_cb = NULL;
455 if ((bta_jv_cb.pm_cb[i].state != BTA_JV_PM_FREE_ST) &&
456 (jv_handle == bta_jv_cb.pm_cb[i].handle)) {
457 for (j = 0; j < BTA_JV_PM_MAX_NUM; j++) {
458 if (bta_jv_cb.pm_cb[j].peer_bd_addr == bta_jv_cb.pm_cb[i].peer_bd_addr)
459 bd_counter++;
460 if (bta_jv_cb.pm_cb[j].app_id == bta_jv_cb.pm_cb[i].app_id)
461 appid_counter++;
462 }
463
464 log::verbose(
465 "jv_handle=0x{:x}, idx={}app_id={}, bd_counter={}, appid_counter={}",
466 jv_handle, i, bta_jv_cb.pm_cb[i].app_id, bd_counter, appid_counter);
467 if (bd_counter > 1) {
468 bta_jv_pm_conn_idle(&bta_jv_cb.pm_cb[i]);
469 }
470
471 if (bd_counter <= 1 || (appid_counter <= 1)) {
472 bta_jv_clear_pm_cb(&bta_jv_cb.pm_cb[i], true);
473 } else {
474 bta_jv_clear_pm_cb(&bta_jv_cb.pm_cb[i], false);
475 }
476
477 if (BTA_JV_RFCOMM_MASK & jv_handle) {
478 uint32_t hi =
479 ((jv_handle & BTA_JV_RFC_HDL_MASK) & ~BTA_JV_RFCOMM_MASK) - 1;
480 uint32_t si = BTA_JV_RFC_HDL_TO_SIDX(jv_handle);
481 if (hi < BTA_JV_MAX_RFC_CONN && bta_jv_cb.rfc_cb[hi].p_cback &&
482 si < BTA_JV_MAX_RFC_SR_SESSION &&
483 bta_jv_cb.rfc_cb[hi].rfc_hdl[si]) {
484 tBTA_JV_PCB* p_pcb =
485 bta_jv_rfc_port_to_pcb(bta_jv_cb.rfc_cb[hi].rfc_hdl[si]);
486 if (p_pcb) {
487 if (NULL == p_pcb->p_pm_cb)
488 log::warn(
489 "jv_handle=0x{:x}, port_handle={}, i={}, no link to pm_cb?",
490 jv_handle, p_pcb->port_handle, i);
491 p_cb = &p_pcb->p_pm_cb;
492 }
493 }
494 } else {
495 if (jv_handle < BTA_JV_MAX_L2C_CONN) {
496 tBTA_JV_L2C_CB* p_l2c_cb = &bta_jv_cb.l2c_cb[jv_handle];
497 if (NULL == p_l2c_cb->p_pm_cb)
498 log::warn("jv_handle=0x{:x}, i={} no link to pm_cb?", jv_handle, i);
499 p_cb = &p_l2c_cb->p_pm_cb;
500 }
501 }
502 if (p_cb) {
503 *p_cb = NULL;
504 status = tBTA_JV_STATUS::SUCCESS;
505 }
506 }
507 }
508 return status;
509 }
510
511 /*******************************************************************************
512 *
513 * Function bta_jv_alloc_set_pm_profile_cb
514 *
515 * Description set PM profile control block
516 *
517 * Returns pointer to allocated cb or NULL in case of failure
518 *
519 ******************************************************************************/
bta_jv_alloc_set_pm_profile_cb(uint32_t jv_handle,tBTA_JV_PM_ID app_id)520 static tBTA_JV_PM_CB* bta_jv_alloc_set_pm_profile_cb(uint32_t jv_handle,
521 tBTA_JV_PM_ID app_id) {
522 bool bRfcHandle = (jv_handle & BTA_JV_RFCOMM_MASK) != 0;
523 RawAddress peer_bd_addr = RawAddress::kEmpty;
524 int i, j;
525 tBTA_JV_PM_CB** pp_cb;
526
527 for (i = 0; i < BTA_JV_PM_MAX_NUM; i++) {
528 pp_cb = NULL;
529 if (bta_jv_cb.pm_cb[i].state == BTA_JV_PM_FREE_ST) {
530 /* rfc handle bd addr retrieval requires core stack handle */
531 if (bRfcHandle) {
532 for (j = 0; j < BTA_JV_MAX_RFC_CONN; j++) {
533 if (jv_handle == bta_jv_cb.port_cb[j].handle) {
534 pp_cb = &bta_jv_cb.port_cb[j].p_pm_cb;
535 if (PORT_SUCCESS !=
536 PORT_CheckConnection(bta_jv_cb.port_cb[j].port_handle,
537 &peer_bd_addr, NULL)) {
538 i = BTA_JV_PM_MAX_NUM;
539 }
540 break;
541 }
542 }
543 } else {
544 /* use jv handle for l2cap bd address retrieval */
545 for (j = 0; j < BTA_JV_MAX_L2C_CONN; j++) {
546 if (jv_handle == bta_jv_cb.l2c_cb[j].handle) {
547 pp_cb = &bta_jv_cb.l2c_cb[j].p_pm_cb;
548 const RawAddress* p_bd_addr =
549 GAP_ConnGetRemoteAddr((uint16_t)jv_handle);
550 if (p_bd_addr)
551 peer_bd_addr = *p_bd_addr;
552 else
553 i = BTA_JV_PM_MAX_NUM;
554 break;
555 }
556 }
557 }
558 log::verbose(
559 "handle=0x{:x}, app_id={}, idx={}, BTA_JV_PM_MAX_NUM={}, pp_cb={}",
560 jv_handle, app_id, i, BTA_JV_PM_MAX_NUM, fmt::ptr(pp_cb));
561 break;
562 }
563 }
564
565 if ((i != BTA_JV_PM_MAX_NUM) && (NULL != pp_cb)) {
566 *pp_cb = &bta_jv_cb.pm_cb[i];
567 bta_jv_cb.pm_cb[i].handle = jv_handle;
568 bta_jv_cb.pm_cb[i].app_id = app_id;
569 bta_jv_cb.pm_cb[i].peer_bd_addr = peer_bd_addr;
570 bta_jv_cb.pm_cb[i].state = BTA_JV_PM_IDLE_ST;
571 return &bta_jv_cb.pm_cb[i];
572 }
573 log::warn("handle=0x{:x}, app_id={}, return NULL", jv_handle, app_id);
574 return NULL;
575 }
576
577 /*******************************************************************************
578 *
579 * Function bta_jv_check_psm
580 *
581 * Description for now use only the legal PSM per JSR82 spec
582 *
583 * Returns true, if allowed
584 *
585 ******************************************************************************/
bta_jv_check_psm(uint16_t psm)586 bool bta_jv_check_psm(uint16_t psm) {
587 bool ret = false;
588
589 if (L2C_IS_VALID_PSM(psm)) {
590 if (psm < 0x1001) {
591 /* see if this is defined by spec */
592 switch (psm) {
593 case BT_PSM_SDP:
594 case BT_PSM_RFCOMM: /* 3 */
595 /* do not allow java app to use these 2 PSMs */
596 break;
597
598 case BT_PSM_TCS:
599 case BT_PSM_CTP:
600 if (!bta_sys_is_register(BTA_ID_CT) &&
601 !bta_sys_is_register(BTA_ID_CG))
602 ret = true;
603 break;
604
605 case BT_PSM_BNEP: /* F */
606 if (!bta_sys_is_register(BTA_ID_PAN)) ret = true;
607 break;
608
609 case BT_PSM_HIDC:
610 case BT_PSM_HIDI:
611 // FIX: allow HID Device and HID Host to coexist
612 if (!bta_sys_is_register(BTA_ID_HD) ||
613 !bta_sys_is_register(BTA_ID_HH))
614 ret = true;
615 break;
616
617 case AVCT_PSM: /* 0x17 */
618 case AVDT_PSM: /* 0x19 */
619 if (!bta_sys_is_register(BTA_ID_AV)) ret = true;
620 break;
621
622 default:
623 ret = true;
624 break;
625 }
626 } else {
627 ret = true;
628 }
629 }
630 return ret;
631 }
632
633 /* Initialises the JAVA I/F */
bta_jv_enable(tBTA_JV_DM_CBACK * p_cback)634 void bta_jv_enable(tBTA_JV_DM_CBACK* p_cback) {
635 bta_jv_cb.p_dm_cback = p_cback;
636 if (bta_jv_cb.p_dm_cback) {
637 tBTA_JV bta_jv = {
638 .status = tBTA_JV_STATUS::SUCCESS,
639 };
640 bta_jv_cb.p_dm_cback(BTA_JV_ENABLE_EVT, &bta_jv, 0);
641 }
642 memset(bta_jv_cb.free_psm_list, 0, sizeof(bta_jv_cb.free_psm_list));
643 memset(bta_jv_cb.scn_in_use, 0, sizeof(bta_jv_cb.scn_in_use));
644 bta_jv_cb.scn_search_index = 1;
645 }
646
647 /** Disables the BT device manager free the resources used by java */
bta_jv_disable()648 void bta_jv_disable() { log::info(""); }
649
650 /**
651 * We keep a list of PSM's that have been freed from JAVA, for reuse.
652 * This function will return a free PSM, and delete it from the free
653 * list.
654 * If no free PSMs exist, 0 will be returned.
655 */
bta_jv_get_free_psm()656 static uint16_t bta_jv_get_free_psm() {
657 const int cnt =
658 sizeof(bta_jv_cb.free_psm_list) / sizeof(bta_jv_cb.free_psm_list[0]);
659 for (int i = 0; i < cnt; i++) {
660 uint16_t psm = bta_jv_cb.free_psm_list[i];
661 if (psm != 0) {
662 log::verbose("Reusing PSM=0x{:x}", psm);
663 bta_jv_cb.free_psm_list[i] = 0;
664 return psm;
665 }
666 }
667 return 0;
668 }
669
bta_jv_set_free_psm(uint16_t psm)670 static void bta_jv_set_free_psm(uint16_t psm) {
671 int free_index = -1;
672 const int cnt =
673 sizeof(bta_jv_cb.free_psm_list) / sizeof(bta_jv_cb.free_psm_list[0]);
674 for (int i = 0; i < cnt; i++) {
675 if (bta_jv_cb.free_psm_list[i] == 0) {
676 free_index = i;
677 } else if (psm == bta_jv_cb.free_psm_list[i]) {
678 return; // PSM already freed?
679 }
680 }
681 if (free_index != -1) {
682 bta_jv_cb.free_psm_list[free_index] = psm;
683 log::verbose("Recycling PSM=0x{:x}", psm);
684 } else {
685 log::error("unable to free psm=0x{:x} no more free slots", psm);
686 }
687 }
688
bta_jv_allocate_l2cap_classic_psm()689 static uint16_t bta_jv_allocate_l2cap_classic_psm() {
690 bool done = false;
691 uint16_t psm = bta_jv_cb.dyn_psm;
692
693 while (!done) {
694 psm += 2;
695 if (psm > 0xfeff) {
696 psm = 0x1001;
697 } else if (psm & 0x0100) {
698 /* the upper byte must be even */
699 psm += 0x0100;
700 }
701
702 /* if psm is in range of reserved BRCM Aware features */
703 if ((BRCM_RESERVED_PSM_START <= psm) && (psm <= BRCM_RESERVED_PSM_END))
704 continue;
705
706 /* make sure the newlly allocated psm is not used right now */
707 if (used_l2cap_classic_dynamic_psm.count(psm) == 0) done = true;
708 }
709 bta_jv_cb.dyn_psm = psm;
710
711 return (psm);
712 }
713
714 /** Obtain a free SCN (Server Channel Number) (RFCOMM channel or L2CAP PSM) */
bta_jv_get_channel_id(tBTA_JV_CONN_TYPE type,int32_t channel,uint32_t l2cap_socket_id,uint32_t rfcomm_slot_id)715 void bta_jv_get_channel_id(
716 tBTA_JV_CONN_TYPE type /* One of BTA_JV_CONN_TYPE_ */,
717 int32_t channel /* optionally request a specific channel */,
718 uint32_t l2cap_socket_id, uint32_t rfcomm_slot_id) {
719 uint16_t psm = 0;
720
721 switch (type) {
722 case tBTA_JV_CONN_TYPE::RFCOMM: {
723 uint8_t scn = 0;
724 if (channel > 0) {
725 if (BTA_TryAllocateSCN(channel)) {
726 scn = static_cast<uint8_t>(channel);
727 } else {
728 log::error("rfc channel {} already in use or invalid", channel);
729 }
730 } else {
731 scn = BTA_AllocateSCN();
732 if (scn == 0) {
733 log::error("out of rfc channels");
734 }
735 }
736 if (bta_jv_cb.p_dm_cback) {
737 tBTA_JV bta_jv;
738 bta_jv.scn = scn;
739 bta_jv_cb.p_dm_cback(BTA_JV_GET_SCN_EVT, &bta_jv, rfcomm_slot_id);
740 }
741 return;
742 }
743 case tBTA_JV_CONN_TYPE::L2CAP:
744 psm = bta_jv_get_free_psm();
745 if (psm == 0) {
746 psm = bta_jv_allocate_l2cap_classic_psm();
747 log::verbose("returned PSM=0x{:x}", psm);
748 }
749 break;
750 case tBTA_JV_CONN_TYPE::L2CAP_LE:
751 psm = L2CA_AllocateLePSM();
752 if (psm == 0) {
753 log::error("Error: No free LE PSM available");
754 }
755 break;
756 default:
757 break;
758 }
759
760 if (bta_jv_cb.p_dm_cback) {
761 tBTA_JV bta_jv;
762 bta_jv.psm = psm;
763 bta_jv_cb.p_dm_cback(BTA_JV_GET_PSM_EVT, &bta_jv, l2cap_socket_id);
764 }
765 }
766
767 /** free a SCN */
bta_jv_free_scn(tBTA_JV_CONN_TYPE type,uint16_t scn)768 void bta_jv_free_scn(tBTA_JV_CONN_TYPE type /* One of BTA_JV_CONN_TYPE_ */,
769 uint16_t scn) {
770 switch (type) {
771 case tBTA_JV_CONN_TYPE::RFCOMM:
772 BTA_FreeSCN(scn);
773 break;
774 case tBTA_JV_CONN_TYPE::L2CAP:
775 bta_jv_set_free_psm(scn);
776 break;
777 case tBTA_JV_CONN_TYPE::L2CAP_LE:
778 log::verbose("type=BTA_JV_CONN_TYPE::L2CAP_LE. psm={}", scn);
779 L2CA_FreeLePSM(scn);
780 break;
781 default:
782 break;
783 }
784 }
785
786 /*******************************************************************************
787 *
788 * Function bta_jv_start_discovery_cback
789 *
790 * Description Callback for Start Discovery
791 *
792 * Returns void
793 *
794 ******************************************************************************/
bta_jv_start_discovery_cback(uint32_t rfcomm_slot_id,const RawAddress & bd_addr,tSDP_RESULT result)795 static void bta_jv_start_discovery_cback(uint32_t rfcomm_slot_id,
796 const RawAddress& bd_addr,
797 tSDP_RESULT result) {
798 if (!bta_jv_cb.sdp_cb.sdp_active) {
799 log::warn(
800 "Received unexpected service discovery callback bd_addr:{} result:{}",
801 bd_addr, sdp_result_text(result), bta_jv_cb.sdp_cb.sdp_active);
802 }
803 if (bta_jv_cb.sdp_cb.bd_addr != bta_jv_cb.sdp_cb.bd_addr) {
804 log::warn(
805 "Received incorrect service discovery callback expected_bd_addr:{} "
806 "actual_bd_addr:{} result:{}",
807 bta_jv_cb.sdp_cb.bd_addr, bd_addr, sdp_result_text(result),
808 bta_jv_cb.sdp_cb.sdp_active);
809 }
810
811 if (bta_jv_cb.p_dm_cback) {
812 tBTA_JV bta_jv = {
813 .disc_comp =
814 {
815 .status = tBTA_JV_STATUS::FAILURE,
816 .scn = 0,
817 },
818 };
819 if (result == SDP_SUCCESS || result == SDP_DB_FULL) {
820 log::info(
821 "Received service discovery callback success bd_addr:{} result:{}",
822 bd_addr, sdp_result_text(result));
823 tSDP_PROTOCOL_ELEM pe;
824 tSDP_DISC_REC* p_sdp_rec = NULL;
825 p_sdp_rec = get_legacy_stack_sdp_api()->db.SDP_FindServiceUUIDInDb(
826 p_bta_jv_cfg->p_sdp_db, bta_jv_cb.sdp_cb.uuid, p_sdp_rec);
827 log::verbose("bta_jv_cb.uuid={} p_sdp_rec={}", bta_jv_cb.sdp_cb.uuid,
828 fmt::ptr(p_sdp_rec));
829 if (p_sdp_rec &&
830 get_legacy_stack_sdp_api()->record.SDP_FindProtocolListElemInRec(
831 p_sdp_rec, UUID_PROTOCOL_RFCOMM, &pe)) {
832 bta_jv = {
833 .disc_comp =
834 {
835 .status = tBTA_JV_STATUS::SUCCESS,
836 .scn = (uint8_t)pe.params[0],
837 },
838 };
839 }
840 } else {
841 log::warn(
842 "Received service discovery callback failed bd_addr:{} result:{}",
843 bd_addr, sdp_result_text(result));
844 }
845 log::info(
846 "Issuing service discovery complete callback bd_addr:{} result:{} "
847 "status:{} scn:{}",
848 bd_addr, sdp_result_text(result),
849 bta_jv_status_text(bta_jv.disc_comp.status), bta_jv.disc_comp.scn);
850 bta_jv_cb.p_dm_cback(BTA_JV_DISCOVERY_COMP_EVT, &bta_jv, rfcomm_slot_id);
851 } else {
852 log::warn(
853 "Received service discovery callback when disabled bd_addr:{} "
854 "result:{}",
855 bd_addr, sdp_result_text(result));
856 }
857 bta_jv_cb.sdp_cb = {};
858 }
859
860 /* Discovers services on a remote device */
bta_jv_start_discovery(const RawAddress & bd_addr,uint16_t num_uuid,bluetooth::Uuid * uuid_list,uint32_t rfcomm_slot_id)861 void bta_jv_start_discovery(const RawAddress& bd_addr, uint16_t num_uuid,
862 bluetooth::Uuid* uuid_list,
863 uint32_t rfcomm_slot_id) {
864 log::assert_that(uuid_list != nullptr, "assert failed: uuid_list != nullptr");
865 if (bta_jv_cb.sdp_cb.sdp_active) {
866 log::warn(
867 "Unable to start discovery as already in progress active_bd_addr{} "
868 "request_bd_addr:{} num:uuid:{} rfcomm_slot_id:{}",
869 bta_jv_cb.sdp_cb.bd_addr, bd_addr, num_uuid, rfcomm_slot_id);
870 if (bta_jv_cb.p_dm_cback) {
871 tBTA_JV bta_jv = {
872 .status = tBTA_JV_STATUS::BUSY,
873 };
874 bta_jv_cb.p_dm_cback(BTA_JV_DISCOVERY_COMP_EVT, &bta_jv, rfcomm_slot_id);
875 } else {
876 log::warn(
877 "bta::jv module DISABLED so unable to inform caller service "
878 "discovery is "
879 "unavailable");
880 }
881 return;
882 }
883
884 /* init the database/set up the filter */
885 if (!get_legacy_stack_sdp_api()->service.SDP_InitDiscoveryDb(
886 p_bta_jv_cfg->p_sdp_db, p_bta_jv_cfg->sdp_db_size, num_uuid,
887 uuid_list, 0, NULL)) {
888 log::warn(
889 "Unable to initialize service discovery db bd_addr:{} num:uuid:{} "
890 "rfcomm_slot_id:{}",
891 bd_addr, num_uuid, rfcomm_slot_id);
892 }
893
894 /* tell SDP to keep the raw data */
895 p_bta_jv_cfg->p_sdp_db->raw_data = p_bta_jv_cfg->p_sdp_raw_data;
896 p_bta_jv_cfg->p_sdp_db->raw_size = p_bta_jv_cfg->sdp_raw_size;
897
898 // Optimistically set this as active
899 bta_jv_cb.sdp_cb = {
900 .sdp_active = true,
901 .bd_addr = bd_addr,
902 .uuid = uuid_list[0],
903 };
904
905 if (!get_legacy_stack_sdp_api()->service.SDP_ServiceSearchAttributeRequest2(
906 bd_addr, p_bta_jv_cfg->p_sdp_db,
907 base::BindRepeating(&bta_jv_start_discovery_cback, rfcomm_slot_id))) {
908 bta_jv_cb.sdp_cb = {};
909 log::warn(
910 "Unable to original service discovery bd_addr:{} num:uuid:{} "
911 "rfcomm_slot_id:{}",
912 bd_addr, num_uuid, rfcomm_slot_id);
913 /* failed to start SDP. report the failure right away */
914 if (bta_jv_cb.p_dm_cback) {
915 tBTA_JV bta_jv = {
916 .status = tBTA_JV_STATUS::FAILURE,
917 };
918 bta_jv_cb.p_dm_cback(BTA_JV_DISCOVERY_COMP_EVT, &bta_jv, rfcomm_slot_id);
919 } else {
920 log::warn("No callback set for discovery complete event");
921 }
922 } else {
923 log::info(
924 "Started service discovery bd_addr:{} num_uuid:{} rfcomm_slot_id:{}",
925 bd_addr, num_uuid, rfcomm_slot_id);
926 }
927 }
928
929 /* Create an SDP record with the given attributes */
bta_jv_create_record(uint32_t rfcomm_slot_id)930 void bta_jv_create_record(uint32_t rfcomm_slot_id) {
931 tBTA_JV_CREATE_RECORD evt_data;
932 evt_data.status = tBTA_JV_STATUS::SUCCESS;
933 if (bta_jv_cb.p_dm_cback) {
934 // callback immediately to create the sdp record in stack thread context
935 tBTA_JV bta_jv;
936 bta_jv.create_rec = evt_data;
937 bta_jv_cb.p_dm_cback(BTA_JV_CREATE_RECORD_EVT, &bta_jv, rfcomm_slot_id);
938 }
939 }
940
941 /* Delete an SDP record */
bta_jv_delete_record(uint32_t handle)942 void bta_jv_delete_record(uint32_t handle) {
943 if (handle) {
944 /* this is a record created by btif layer*/
945 if (!get_legacy_stack_sdp_api()->handle.SDP_DeleteRecord(handle)) {
946 log::warn("Unable to delete SDP record handle:{}", handle);
947 }
948 }
949 }
950
951 /*******************************************************************************
952 *
953 * Function bta_jv_l2cap_client_cback
954 *
955 * Description handles the l2cap client events
956 *
957 * Returns void
958 *
959 ******************************************************************************/
bta_jv_l2cap_client_cback(uint16_t gap_handle,uint16_t event,tGAP_CB_DATA * data)960 static void bta_jv_l2cap_client_cback(uint16_t gap_handle, uint16_t event,
961 tGAP_CB_DATA* data) {
962 tBTA_JV_L2C_CB* p_cb = &bta_jv_cb.l2c_cb[gap_handle];
963 tBTA_JV evt_data;
964
965 if (gap_handle >= BTA_JV_MAX_L2C_CONN && !p_cb->p_cback) return;
966
967 log::verbose("gap_handle={}, evt=0x{:x}", gap_handle, event);
968 evt_data.l2c_open.status = tBTA_JV_STATUS::SUCCESS;
969 evt_data.l2c_open.handle = gap_handle;
970
971 switch (event) {
972 case GAP_EVT_CONN_OPENED:
973 evt_data.l2c_open.rem_bda = *GAP_ConnGetRemoteAddr(gap_handle);
974 evt_data.l2c_open.tx_mtu = GAP_ConnGetRemMtuSize(gap_handle);
975 if (data != nullptr) {
976 evt_data.l2c_open.local_cid = data->l2cap_cids.local_cid;
977 evt_data.l2c_open.remote_cid = data->l2cap_cids.remote_cid;
978 }
979 p_cb->state = BTA_JV_ST_CL_OPEN;
980 p_cb->p_cback(BTA_JV_L2CAP_OPEN_EVT, &evt_data, p_cb->l2cap_socket_id);
981 break;
982
983 case GAP_EVT_CONN_CLOSED:
984 p_cb->state = BTA_JV_ST_NONE;
985 bta_jv_free_sec_id(&p_cb->sec_id);
986 evt_data.l2c_close.async = true;
987 evt_data.l2c_close.reason =
988 data != nullptr ? bta_jv_from_gap_l2cap_err(data->l2cap_result)
989 : BTA_JV_L2CAP_REASON_EMPTY;
990 p_cb->p_cback(BTA_JV_L2CAP_CLOSE_EVT, &evt_data, p_cb->l2cap_socket_id);
991 p_cb->p_cback = NULL;
992 break;
993
994 case GAP_EVT_CONN_DATA_AVAIL:
995 evt_data.data_ind.handle = gap_handle;
996 /* Reset idle timer to avoid requesting sniff mode while receiving data */
997 bta_jv_pm_conn_busy(p_cb->p_pm_cb);
998 p_cb->p_cback(BTA_JV_L2CAP_DATA_IND_EVT, &evt_data,
999 p_cb->l2cap_socket_id);
1000 bta_jv_pm_conn_idle(p_cb->p_pm_cb);
1001 break;
1002
1003 case GAP_EVT_TX_EMPTY:
1004 bta_jv_pm_conn_idle(p_cb->p_pm_cb);
1005 break;
1006
1007 case GAP_EVT_CONN_CONGESTED:
1008 case GAP_EVT_CONN_UNCONGESTED:
1009 p_cb->cong = (event == GAP_EVT_CONN_CONGESTED) ? true : false;
1010 evt_data.l2c_cong.cong = p_cb->cong;
1011 p_cb->p_cback(BTA_JV_L2CAP_CONG_EVT, &evt_data, p_cb->l2cap_socket_id);
1012 break;
1013
1014 default:
1015 break;
1016 }
1017 }
1018
1019 /* makes an l2cap client connection */
bta_jv_l2cap_connect(tBTA_JV_CONN_TYPE type,tBTA_SEC sec_mask,uint16_t remote_psm,uint16_t rx_mtu,const RawAddress & peer_bd_addr,std::unique_ptr<tL2CAP_CFG_INFO> cfg_param,std::unique_ptr<tL2CAP_ERTM_INFO> ertm_info,tBTA_JV_L2CAP_CBACK * p_cback,uint32_t l2cap_socket_id)1020 void bta_jv_l2cap_connect(tBTA_JV_CONN_TYPE type, tBTA_SEC sec_mask,
1021 uint16_t remote_psm, uint16_t rx_mtu,
1022 const RawAddress& peer_bd_addr,
1023 std::unique_ptr<tL2CAP_CFG_INFO> cfg_param,
1024 std::unique_ptr<tL2CAP_ERTM_INFO> ertm_info,
1025 tBTA_JV_L2CAP_CBACK* p_cback,
1026 uint32_t l2cap_socket_id) {
1027 uint16_t handle = GAP_INVALID_HANDLE;
1028
1029 tL2CAP_CFG_INFO cfg;
1030 memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO));
1031 if (cfg_param) {
1032 cfg = *cfg_param;
1033 }
1034
1035 /* We need to use this value for MTU to be able to handle cases where cfg is
1036 * not set in req. */
1037 cfg.mtu_present = true;
1038 cfg.mtu = rx_mtu;
1039
1040 uint8_t sec_id = bta_jv_alloc_sec_id();
1041 tBTA_JV_L2CAP_CL_INIT evt_data;
1042 evt_data.sec_id = sec_id;
1043 evt_data.status = tBTA_JV_STATUS::FAILURE;
1044
1045 if (sec_id) {
1046 /* PSM checking is not required for LE COC */
1047 if ((type != tBTA_JV_CONN_TYPE::L2CAP) ||
1048 (bta_jv_check_psm(remote_psm))) /* allowed */
1049 {
1050 // Given a client socket type
1051 // return the associated transport
1052 const tBT_TRANSPORT transport =
1053 [](tBTA_JV_CONN_TYPE type) -> tBT_TRANSPORT {
1054 switch (type) {
1055 case tBTA_JV_CONN_TYPE::L2CAP:
1056 return BT_TRANSPORT_BR_EDR;
1057 case tBTA_JV_CONN_TYPE::L2CAP_LE:
1058 return BT_TRANSPORT_LE;
1059 case tBTA_JV_CONN_TYPE::RFCOMM:
1060 default:
1061 break;
1062 }
1063 log::warn("Unexpected socket type:{}", bta_jv_conn_type_text(type));
1064 return BT_TRANSPORT_AUTO;
1065 }(type);
1066
1067 uint16_t max_mps = 0xffff; // Let GAP_ConnOpen set the max_mps.
1068 handle = GAP_ConnOpen("", sec_id, 0, &peer_bd_addr, remote_psm, max_mps,
1069 &cfg, ertm_info.get(), sec_mask,
1070 bta_jv_l2cap_client_cback, transport);
1071 if (handle != GAP_INVALID_HANDLE) {
1072 evt_data.status = tBTA_JV_STATUS::SUCCESS;
1073 }
1074 }
1075 }
1076
1077 if (evt_data.status == tBTA_JV_STATUS::SUCCESS) {
1078 tBTA_JV_L2C_CB* p_cb;
1079 p_cb = &bta_jv_cb.l2c_cb[handle];
1080 p_cb->handle = handle;
1081 p_cb->p_cback = p_cback;
1082 p_cb->l2cap_socket_id = l2cap_socket_id;
1083 p_cb->psm = 0; /* not a server */
1084 p_cb->sec_id = sec_id;
1085 p_cb->state = BTA_JV_ST_CL_OPENING;
1086 } else {
1087 bta_jv_free_sec_id(&sec_id);
1088 }
1089
1090 evt_data.handle = handle;
1091 if (p_cback) {
1092 tBTA_JV bta_jv;
1093 bta_jv.l2c_cl_init = evt_data;
1094 p_cback(BTA_JV_L2CAP_CL_INIT_EVT, &bta_jv, l2cap_socket_id);
1095 }
1096 }
1097
1098 /** Close an L2CAP client connection */
bta_jv_l2cap_close(uint32_t handle,tBTA_JV_L2C_CB * p_cb)1099 void bta_jv_l2cap_close(uint32_t handle, tBTA_JV_L2C_CB* p_cb) {
1100 tBTA_JV_L2CAP_CLOSE evt_data;
1101 tBTA_JV_L2CAP_CBACK* p_cback = p_cb->p_cback;
1102 uint32_t l2cap_socket_id = p_cb->l2cap_socket_id;
1103
1104 evt_data.handle = handle;
1105 evt_data.status = bta_jv_free_l2c_cb(p_cb);
1106 evt_data.async = false;
1107
1108 if (p_cback) {
1109 tBTA_JV bta_jv;
1110 bta_jv.l2c_close = evt_data;
1111 p_cback(BTA_JV_L2CAP_CLOSE_EVT, &bta_jv, l2cap_socket_id);
1112 }
1113 }
1114
1115 /*******************************************************************************
1116 *
1117 * Function bta_jv_l2cap_server_cback
1118 *
1119 * Description handles the l2cap server callback
1120 *
1121 * Returns void
1122 *
1123 ******************************************************************************/
bta_jv_l2cap_server_cback(uint16_t gap_handle,uint16_t event,tGAP_CB_DATA * data)1124 static void bta_jv_l2cap_server_cback(uint16_t gap_handle, uint16_t event,
1125 tGAP_CB_DATA* data) {
1126 tBTA_JV_L2C_CB* p_cb = &bta_jv_cb.l2c_cb[gap_handle];
1127 tBTA_JV evt_data;
1128 tBTA_JV_L2CAP_CBACK* p_cback;
1129 uint32_t socket_id;
1130
1131 if (gap_handle >= BTA_JV_MAX_L2C_CONN && !p_cb->p_cback) return;
1132
1133 log::verbose("gap_handle={}, evt=0x{:x}", gap_handle, event);
1134 evt_data.l2c_open.status = tBTA_JV_STATUS::SUCCESS;
1135 evt_data.l2c_open.handle = gap_handle;
1136
1137 switch (event) {
1138 case GAP_EVT_CONN_OPENED:
1139 evt_data.l2c_open.rem_bda = *GAP_ConnGetRemoteAddr(gap_handle);
1140 evt_data.l2c_open.tx_mtu = GAP_ConnGetRemMtuSize(gap_handle);
1141 if (data != nullptr) {
1142 evt_data.l2c_open.local_cid = data->l2cap_cids.local_cid;
1143 evt_data.l2c_open.remote_cid = data->l2cap_cids.remote_cid;
1144 }
1145 p_cb->state = BTA_JV_ST_SR_OPEN;
1146 p_cb->p_cback(BTA_JV_L2CAP_OPEN_EVT, &evt_data, p_cb->l2cap_socket_id);
1147 break;
1148
1149 case GAP_EVT_CONN_CLOSED:
1150 evt_data.l2c_close.async = true;
1151 evt_data.l2c_close.handle = p_cb->handle;
1152 p_cback = p_cb->p_cback;
1153 socket_id = p_cb->l2cap_socket_id;
1154 evt_data.l2c_close.status = bta_jv_free_l2c_cb(p_cb);
1155 p_cback(BTA_JV_L2CAP_CLOSE_EVT, &evt_data, socket_id);
1156 break;
1157
1158 case GAP_EVT_CONN_DATA_AVAIL:
1159 evt_data.data_ind.handle = gap_handle;
1160 /* Reset idle timer to avoid requesting sniff mode while receiving data */
1161 bta_jv_pm_conn_busy(p_cb->p_pm_cb);
1162 p_cb->p_cback(BTA_JV_L2CAP_DATA_IND_EVT, &evt_data,
1163 p_cb->l2cap_socket_id);
1164 bta_jv_pm_conn_idle(p_cb->p_pm_cb);
1165 break;
1166
1167 case GAP_EVT_TX_EMPTY:
1168 bta_jv_pm_conn_idle(p_cb->p_pm_cb);
1169 break;
1170
1171 case GAP_EVT_CONN_CONGESTED:
1172 case GAP_EVT_CONN_UNCONGESTED:
1173 p_cb->cong = (event == GAP_EVT_CONN_CONGESTED) ? true : false;
1174 evt_data.l2c_cong.cong = p_cb->cong;
1175 p_cb->p_cback(BTA_JV_L2CAP_CONG_EVT, &evt_data, p_cb->l2cap_socket_id);
1176 break;
1177
1178 default:
1179 break;
1180 }
1181 }
1182
1183 /** starts an L2CAP server */
bta_jv_l2cap_start_server(tBTA_JV_CONN_TYPE type,tBTA_SEC sec_mask,uint16_t local_psm,uint16_t rx_mtu,std::unique_ptr<tL2CAP_CFG_INFO> cfg_param,std::unique_ptr<tL2CAP_ERTM_INFO> ertm_info,tBTA_JV_L2CAP_CBACK * p_cback,uint32_t l2cap_socket_id)1184 void bta_jv_l2cap_start_server(tBTA_JV_CONN_TYPE type, tBTA_SEC sec_mask,
1185 uint16_t local_psm, uint16_t rx_mtu,
1186 std::unique_ptr<tL2CAP_CFG_INFO> cfg_param,
1187 std::unique_ptr<tL2CAP_ERTM_INFO> ertm_info,
1188 tBTA_JV_L2CAP_CBACK* p_cback,
1189 uint32_t l2cap_socket_id) {
1190 uint16_t handle;
1191 tBTA_JV_L2CAP_START evt_data;
1192
1193 tL2CAP_CFG_INFO cfg;
1194 memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO));
1195 if (cfg_param) {
1196 cfg = *cfg_param;
1197 }
1198
1199 // FIX: MTU=0 means not present
1200 if (rx_mtu > 0) {
1201 cfg.mtu_present = true;
1202 cfg.mtu = rx_mtu;
1203 } else {
1204 cfg.mtu_present = false;
1205 cfg.mtu = 0;
1206 }
1207
1208 uint8_t sec_id = bta_jv_alloc_sec_id();
1209 uint16_t max_mps = 0xffff; // Let GAP_ConnOpen set the max_mps.
1210 /* PSM checking is not required for LE COC */
1211
1212 // Given a server socket type
1213 // return the associated transport
1214 const tBT_TRANSPORT transport = [](tBTA_JV_CONN_TYPE type) -> tBT_TRANSPORT {
1215 switch (type) {
1216 case tBTA_JV_CONN_TYPE::L2CAP:
1217 return BT_TRANSPORT_BR_EDR;
1218 case tBTA_JV_CONN_TYPE::L2CAP_LE:
1219 return BT_TRANSPORT_LE;
1220 case tBTA_JV_CONN_TYPE::RFCOMM:
1221 default:
1222 break;
1223 }
1224 log::warn("Unexpected socket type:{}", bta_jv_conn_type_text(type));
1225 return BT_TRANSPORT_AUTO;
1226 }(type);
1227
1228 if (0 == sec_id ||
1229 ((type == tBTA_JV_CONN_TYPE::L2CAP) && (!bta_jv_check_psm(local_psm))) ||
1230 (handle = GAP_ConnOpen("JV L2CAP", sec_id, 1, nullptr, local_psm, max_mps,
1231 &cfg, ertm_info.get(), sec_mask,
1232 bta_jv_l2cap_server_cback, transport)) ==
1233 GAP_INVALID_HANDLE) {
1234 bta_jv_free_sec_id(&sec_id);
1235 evt_data.status = tBTA_JV_STATUS::FAILURE;
1236 } else {
1237 tBTA_JV_L2C_CB* p_cb = &bta_jv_cb.l2c_cb[handle];
1238 evt_data.status = tBTA_JV_STATUS::SUCCESS;
1239 evt_data.handle = handle;
1240 evt_data.sec_id = sec_id;
1241 p_cb->p_cback = p_cback;
1242 p_cb->l2cap_socket_id = l2cap_socket_id;
1243 p_cb->handle = handle;
1244 p_cb->sec_id = sec_id;
1245 p_cb->state = BTA_JV_ST_SR_LISTEN;
1246 p_cb->psm = local_psm;
1247 }
1248
1249 if (p_cback) {
1250 tBTA_JV bta_jv;
1251 bta_jv.l2c_start = evt_data;
1252 p_cback(BTA_JV_L2CAP_START_EVT, &bta_jv, l2cap_socket_id);
1253 }
1254 }
1255
1256 /* stops an L2CAP server */
bta_jv_l2cap_stop_server(uint16_t,uint32_t l2cap_socket_id)1257 void bta_jv_l2cap_stop_server(uint16_t /* local_psm */,
1258 uint32_t l2cap_socket_id) {
1259 for (int i = 0; i < BTA_JV_MAX_L2C_CONN; i++) {
1260 if (bta_jv_cb.l2c_cb[i].l2cap_socket_id == l2cap_socket_id) {
1261 tBTA_JV_L2C_CB* p_cb = &bta_jv_cb.l2c_cb[i];
1262 tBTA_JV_L2CAP_CBACK* p_cback = p_cb->p_cback;
1263 tBTA_JV_L2CAP_CLOSE evt_data;
1264 evt_data.handle = p_cb->handle;
1265 evt_data.status = bta_jv_free_l2c_cb(p_cb);
1266 evt_data.async = false;
1267 if (p_cback) {
1268 tBTA_JV bta_jv;
1269 bta_jv.l2c_close = evt_data;
1270 p_cback(BTA_JV_L2CAP_CLOSE_EVT, &bta_jv, l2cap_socket_id);
1271 }
1272 break;
1273 }
1274 }
1275 }
1276
1277 /* Write data to an L2CAP connection */
bta_jv_l2cap_write(uint32_t handle,uint32_t req_id,BT_HDR * msg,uint32_t user_id,tBTA_JV_L2C_CB * p_cb)1278 void bta_jv_l2cap_write(uint32_t handle, uint32_t req_id, BT_HDR* msg,
1279 uint32_t user_id, tBTA_JV_L2C_CB* p_cb) {
1280 /* As we check this callback exists before the tBTA_JV_API_L2CAP_WRITE can be
1281 * send through the API this check should not be needed. But the API is not
1282 * designed to be used (safely at least) in a multi-threaded scheduler, hence
1283 * if the peer device disconnects the l2cap link after the API is called, but
1284 * before this message is handled, the ->p_cback will be cleared at this
1285 * point. At first glanch this seems highly unlikely, but for all
1286 * obex-profiles with two channels connected - e.g. MAP, this happens around 1
1287 * of 4 disconnects, as a disconnect on the server channel causes a disconnect
1288 * to be send on the client (notification) channel, but at the peer typically
1289 * disconnects both the OBEX disconnect request crosses the incoming l2cap
1290 * disconnect. If p_cback is cleared, we simply discard the data. RISK: The
1291 * caller must handle any cleanup based on another signal than
1292 * BTA_JV_L2CAP_WRITE_EVT, which is typically not possible, as the pointer to
1293 * the allocated buffer is stored in this message, and can therefore not be
1294 * freed, hence we have a mem-leak-by-design.*/
1295 if (!p_cb->p_cback) {
1296 /* As this pointer is checked in the API function, this occurs only when the
1297 * channel is disconnected after the API function is called, but before the
1298 * message is handled. */
1299 log::error("p_cb->p_cback == NULL");
1300 osi_free(msg);
1301 return;
1302 }
1303
1304 tBTA_JV_L2CAP_WRITE evt_data;
1305 evt_data.status = tBTA_JV_STATUS::FAILURE;
1306 evt_data.handle = handle;
1307 evt_data.req_id = req_id;
1308 evt_data.cong = p_cb->cong;
1309 evt_data.len = msg->len;
1310
1311 bta_jv_pm_conn_busy(p_cb->p_pm_cb);
1312
1313 // TODO: this was set only for non-fixed channel packets. Is that needed ?
1314 msg->event = BT_EVT_TO_BTU_SP_DATA;
1315
1316 if (evt_data.cong) {
1317 osi_free(msg);
1318 } else {
1319 if (GAP_ConnWriteData(handle, msg) == BT_PASS)
1320 evt_data.status = tBTA_JV_STATUS::SUCCESS;
1321 }
1322
1323 tBTA_JV bta_jv;
1324 bta_jv.l2c_write = evt_data;
1325 p_cb->p_cback(BTA_JV_L2CAP_WRITE_EVT, &bta_jv, user_id);
1326 }
1327
1328 /*******************************************************************************
1329 *
1330 * Function bta_jv_port_data_co_cback
1331 *
1332 * Description port data callback function of rfcomm
1333 * connections
1334 *
1335 * Returns void
1336 *
1337 ******************************************************************************/
bta_jv_port_data_co_cback(uint16_t port_handle,uint8_t * buf,uint16_t len,int type)1338 static int bta_jv_port_data_co_cback(uint16_t port_handle, uint8_t* buf,
1339 uint16_t len, int type) {
1340 tBTA_JV_RFC_CB* p_cb = bta_jv_rfc_port_to_cb(port_handle);
1341 tBTA_JV_PCB* p_pcb = bta_jv_rfc_port_to_pcb(port_handle);
1342 log::verbose("p_cb={}, p_pcb={}, len={}, type={}", fmt::ptr(p_cb),
1343 fmt::ptr(p_pcb), len, type);
1344 if (p_pcb != NULL) {
1345 switch (type) {
1346 case DATA_CO_CALLBACK_TYPE_INCOMING:
1347 // Reset sniff timer when receiving data by sysproxy
1348 if (osi_property_get_bool("bluetooth.rfcomm.sysproxy.rx.exit_sniff",
1349 false)) {
1350 bta_jv_reset_sniff_timer(p_pcb->p_pm_cb);
1351 }
1352 return bta_co_rfc_data_incoming(p_pcb->rfcomm_slot_id, (BT_HDR*)buf);
1353 case DATA_CO_CALLBACK_TYPE_OUTGOING_SIZE:
1354 return bta_co_rfc_data_outgoing_size(p_pcb->rfcomm_slot_id, (int*)buf);
1355 case DATA_CO_CALLBACK_TYPE_OUTGOING:
1356 return bta_co_rfc_data_outgoing(p_pcb->rfcomm_slot_id, buf, len);
1357 default:
1358 log::error("unknown callout type={}", type);
1359 break;
1360 }
1361 }
1362 return 0;
1363 }
1364
1365 /*******************************************************************************
1366 *
1367 * Function bta_jv_port_mgmt_cl_cback
1368 *
1369 * Description callback for port mamangement function of rfcomm
1370 * client connections
1371 *
1372 * Returns void
1373 *
1374 ******************************************************************************/
bta_jv_port_mgmt_cl_cback(const tPORT_RESULT code,uint16_t port_handle)1375 static void bta_jv_port_mgmt_cl_cback(const tPORT_RESULT code,
1376 uint16_t port_handle) {
1377 tBTA_JV_RFC_CB* p_cb = bta_jv_rfc_port_to_cb(port_handle);
1378 tBTA_JV_PCB* p_pcb = bta_jv_rfc_port_to_pcb(port_handle);
1379 RawAddress rem_bda = RawAddress::kEmpty;
1380 uint16_t lcid;
1381 tBTA_JV_RFCOMM_CBACK* p_cback; /* the callback function */
1382
1383 if (p_cb == NULL) {
1384 log::warn("p_cb is NULL, code={}, port_handle={}", code, port_handle);
1385 return;
1386 } else if (p_cb->p_cback == NULL) {
1387 log::warn("p_cb->p_cback is null, code={}, port_handle={}", code,
1388 port_handle);
1389 return;
1390 }
1391
1392 log::verbose("code={}, port_handle={}, handle={}", code, port_handle,
1393 p_cb->handle);
1394
1395 if (PORT_CheckConnection(port_handle, &rem_bda, &lcid) != PORT_SUCCESS) {
1396 log::warn("Unable to check RFCOMM connection peer:{} handle:{}", rem_bda,
1397 port_handle);
1398 }
1399
1400 if (code == PORT_SUCCESS) {
1401 tBTA_JV evt_data = {
1402 .rfc_open =
1403 {
1404 .status = tBTA_JV_STATUS::SUCCESS,
1405 .handle = p_cb->handle,
1406 .rem_bda = rem_bda,
1407 },
1408 };
1409 p_pcb->state = BTA_JV_ST_CL_OPEN;
1410 p_cb->p_cback(BTA_JV_RFCOMM_OPEN_EVT, &evt_data, p_pcb->rfcomm_slot_id);
1411 } else {
1412 tBTA_JV evt_data = {
1413 .rfc_close =
1414 {
1415 .status = tBTA_JV_STATUS::FAILURE,
1416 .port_status = code,
1417 .handle = p_cb->handle,
1418 .async = (p_pcb->state == BTA_JV_ST_CL_CLOSING) ? false : true,
1419 },
1420 };
1421 // p_pcb->state = BTA_JV_ST_NONE;
1422 // p_pcb->cong = false;
1423 p_cback = p_cb->p_cback;
1424 p_cback(BTA_JV_RFCOMM_CLOSE_EVT, &evt_data, p_pcb->rfcomm_slot_id);
1425 // bta_jv_free_rfc_cb(p_cb, p_pcb);
1426 }
1427 }
1428
1429 /*******************************************************************************
1430 *
1431 * Function bta_jv_port_event_cl_cback
1432 *
1433 * Description Callback for RFCOMM client port events
1434 *
1435 * Returns void
1436 *
1437 ******************************************************************************/
bta_jv_port_event_cl_cback(uint32_t code,uint16_t port_handle)1438 static void bta_jv_port_event_cl_cback(uint32_t code, uint16_t port_handle) {
1439 tBTA_JV_RFC_CB* p_cb = bta_jv_rfc_port_to_cb(port_handle);
1440 tBTA_JV_PCB* p_pcb = bta_jv_rfc_port_to_pcb(port_handle);
1441 tBTA_JV evt_data;
1442
1443 log::verbose("port_handle={}", port_handle);
1444 if (NULL == p_cb || NULL == p_cb->p_cback) return;
1445
1446 log::verbose("code=0x{:x}, port_handle={}, handle={}", code, port_handle,
1447 p_cb->handle);
1448 if (code & PORT_EV_RXCHAR) {
1449 evt_data.data_ind.handle = p_cb->handle;
1450 p_cb->p_cback(BTA_JV_RFCOMM_DATA_IND_EVT, &evt_data, p_pcb->rfcomm_slot_id);
1451 }
1452
1453 if (code & PORT_EV_FC) {
1454 p_pcb->cong = (code & PORT_EV_FCS) ? false : true;
1455 evt_data.rfc_cong.cong = p_pcb->cong;
1456 evt_data.rfc_cong.handle = p_cb->handle;
1457 evt_data.rfc_cong.status = tBTA_JV_STATUS::SUCCESS;
1458 p_cb->p_cback(BTA_JV_RFCOMM_CONG_EVT, &evt_data, p_pcb->rfcomm_slot_id);
1459 }
1460
1461 if (code & PORT_EV_TXEMPTY) {
1462 bta_jv_pm_conn_idle(p_pcb->p_pm_cb);
1463 }
1464 }
1465
1466 /* Client initiates an RFCOMM connection */
bta_jv_rfcomm_connect(tBTA_SEC sec_mask,uint8_t remote_scn,const RawAddress & peer_bd_addr,tBTA_JV_RFCOMM_CBACK * p_cback,uint32_t rfcomm_slot_id)1467 void bta_jv_rfcomm_connect(tBTA_SEC sec_mask, uint8_t remote_scn,
1468 const RawAddress& peer_bd_addr,
1469 tBTA_JV_RFCOMM_CBACK* p_cback,
1470 uint32_t rfcomm_slot_id) {
1471 uint16_t handle = 0;
1472 uint32_t event_mask = BTA_JV_RFC_EV_MASK;
1473 tPORT_STATE port_state;
1474
1475 tBTA_JV bta_jv = {
1476 .rfc_cl_init =
1477 {
1478 .status = tBTA_JV_STATUS::SUCCESS,
1479 .handle = 0,
1480 .sec_id = 0,
1481 .use_co = false,
1482 },
1483 };
1484
1485 if (com::android::bluetooth::flags::rfcomm_always_use_mitm()) {
1486 // Update security service record for RFCOMM client so that
1487 // secure RFCOMM connection will be authenticated with MTIM protection
1488 // while creating the L2CAP connection.
1489 get_btm_client_interface().security.BTM_SetSecurityLevel(
1490 true, "RFC_MUX", BTM_SEC_SERVICE_RFC_MUX, sec_mask, BT_PSM_RFCOMM,
1491 BTM_SEC_PROTO_RFCOMM, 0);
1492 }
1493
1494 if (RFCOMM_CreateConnectionWithSecurity(
1495 UUID_SERVCLASS_SERIAL_PORT, remote_scn, false, BTA_JV_DEF_RFC_MTU,
1496 peer_bd_addr, &handle, bta_jv_port_mgmt_cl_cback,
1497 sec_mask) != PORT_SUCCESS) {
1498 log::error("RFCOMM_CreateConnection failed");
1499 bta_jv.rfc_cl_init.status = tBTA_JV_STATUS::FAILURE;
1500 } else {
1501 tBTA_JV_PCB* p_pcb;
1502 tBTA_JV_RFC_CB* p_cb = bta_jv_alloc_rfc_cb(handle, &p_pcb);
1503 if (p_cb) {
1504 p_cb->p_cback = p_cback;
1505 p_cb->scn = 0;
1506 p_pcb->state = BTA_JV_ST_CL_OPENING;
1507 p_pcb->rfcomm_slot_id = rfcomm_slot_id;
1508 bta_jv.rfc_cl_init.use_co = true;
1509
1510 if (PORT_SetEventMaskAndCallback(
1511 handle, event_mask, bta_jv_port_event_cl_cback) != PORT_SUCCESS) {
1512 log::warn(
1513 "Unable to set RFCOMM client event mask and callback handle:{}",
1514 handle);
1515 }
1516 if (PORT_SetDataCOCallback(handle, bta_jv_port_data_co_cback) !=
1517 PORT_SUCCESS) {
1518 log::warn("Unable to set RFCOMM client data callback handle:{}",
1519 handle);
1520 }
1521 if (PORT_GetState(handle, &port_state) != PORT_SUCCESS) {
1522 log::warn("Unable to get RFCOMM client state handle:{}", handle);
1523 }
1524
1525 port_state.fc_type = (PORT_FC_CTS_ON_INPUT | PORT_FC_CTS_ON_OUTPUT);
1526
1527 if (PORT_SetState(handle, &port_state) != PORT_SUCCESS) {
1528 log::warn("Unable to set RFCOMM client state handle:{}", handle);
1529 }
1530
1531 bta_jv.rfc_cl_init.handle = p_cb->handle;
1532 } else {
1533 bta_jv.rfc_cl_init.status = tBTA_JV_STATUS::FAILURE;
1534 log::error("run out of rfc control block");
1535 }
1536 }
1537
1538 p_cback(BTA_JV_RFCOMM_CL_INIT_EVT, &bta_jv, rfcomm_slot_id);
1539 if (bta_jv.rfc_cl_init.status == tBTA_JV_STATUS::FAILURE) {
1540 if (handle) {
1541 if (RFCOMM_RemoveConnection(handle) != PORT_SUCCESS) {
1542 log::warn("Unable to remove RFCOMM connection handle:{}", handle);
1543 }
1544 }
1545 }
1546 }
1547
find_rfc_pcb(uint32_t rfcomm_slot_id,tBTA_JV_RFC_CB ** cb,tBTA_JV_PCB ** pcb)1548 static int find_rfc_pcb(uint32_t rfcomm_slot_id, tBTA_JV_RFC_CB** cb,
1549 tBTA_JV_PCB** pcb) {
1550 *cb = NULL;
1551 *pcb = NULL;
1552 int i;
1553 for (i = 0; i < MAX_RFC_PORTS; i++) {
1554 uint32_t rfc_handle = bta_jv_cb.port_cb[i].handle & BTA_JV_RFC_HDL_MASK;
1555 rfc_handle &= ~BTA_JV_RFCOMM_MASK;
1556 if (rfc_handle && bta_jv_cb.port_cb[i].rfcomm_slot_id == rfcomm_slot_id) {
1557 *pcb = &bta_jv_cb.port_cb[i];
1558 *cb = &bta_jv_cb.rfc_cb[rfc_handle - 1];
1559 log::verbose(
1560 "FOUND rfc_cb_handle=0x{:x}, port.jv_handle=0x{:x}, state={}, "
1561 "rfc_cb->handle=0x{:x}",
1562 rfc_handle, (*pcb)->handle, (*pcb)->state, (*cb)->handle);
1563 return 1;
1564 }
1565 }
1566 log::verbose("cannot find rfc_cb from user data:{}", rfcomm_slot_id);
1567 return 0;
1568 }
1569
1570 /* Close an RFCOMM connection */
bta_jv_rfcomm_close(uint32_t handle,uint32_t rfcomm_slot_id)1571 void bta_jv_rfcomm_close(uint32_t handle, uint32_t rfcomm_slot_id) {
1572 if (!handle) {
1573 log::error("rfc handle is null");
1574 return;
1575 }
1576
1577 log::verbose("rfc handle={}", handle);
1578
1579 tBTA_JV_RFC_CB* p_cb = NULL;
1580 tBTA_JV_PCB* p_pcb = NULL;
1581
1582 if (!find_rfc_pcb(rfcomm_slot_id, &p_cb, &p_pcb)) return;
1583 bta_jv_free_rfc_cb(p_cb, p_pcb);
1584 }
1585
1586 /*******************************************************************************
1587 *
1588 * Function bta_jv_port_mgmt_sr_cback
1589 *
1590 * Description callback for port mamangement function of rfcomm
1591 * server connections
1592 *
1593 * Returns void
1594 *
1595 ******************************************************************************/
bta_jv_port_mgmt_sr_cback(const tPORT_RESULT code,uint16_t port_handle)1596 static void bta_jv_port_mgmt_sr_cback(const tPORT_RESULT code,
1597 uint16_t port_handle) {
1598 tBTA_JV_PCB* p_pcb = bta_jv_rfc_port_to_pcb(port_handle);
1599 tBTA_JV_RFC_CB* p_cb = bta_jv_rfc_port_to_cb(port_handle);
1600 tBTA_JV evt_data;
1601 RawAddress rem_bda = RawAddress::kEmpty;
1602 uint16_t lcid;
1603 log::verbose("code={}, port_handle={}", code, port_handle);
1604 if (NULL == p_cb || NULL == p_cb->p_cback) {
1605 log::error("p_cb={}, p_cb->p_cback={}", fmt::ptr(p_cb),
1606 fmt::ptr(p_cb ? p_cb->p_cback : nullptr));
1607 return;
1608 }
1609 uint32_t rfcomm_slot_id = p_pcb->rfcomm_slot_id;
1610 log::verbose("code={}, port_handle=0x{:x}, handle=0x{:x}, p_pcb{}, user={}",
1611 code, port_handle, p_cb->handle, fmt::ptr(p_pcb),
1612 p_pcb->rfcomm_slot_id);
1613
1614 int status = PORT_CheckConnection(port_handle, &rem_bda, &lcid);
1615 int failed = true;
1616 if (code == PORT_SUCCESS) {
1617 if (status != PORT_SUCCESS) {
1618 log::error(
1619 "PORT_CheckConnection returned {}, although port is supposed to be "
1620 "connected",
1621 status);
1622 }
1623 evt_data.rfc_srv_open.handle = p_pcb->handle;
1624 evt_data.rfc_srv_open.status = tBTA_JV_STATUS::SUCCESS;
1625 evt_data.rfc_srv_open.rem_bda = rem_bda;
1626 tBTA_JV_PCB* p_pcb_new_listen = bta_jv_add_rfc_port(p_cb, p_pcb);
1627 if (p_pcb_new_listen) {
1628 evt_data.rfc_srv_open.new_listen_handle = p_pcb_new_listen->handle;
1629 p_pcb_new_listen->rfcomm_slot_id =
1630 p_cb->p_cback(BTA_JV_RFCOMM_SRV_OPEN_EVT, &evt_data, rfcomm_slot_id);
1631 if (p_pcb_new_listen->rfcomm_slot_id == 0) {
1632 log::error("rfcomm_slot_id == {}", p_pcb_new_listen->rfcomm_slot_id);
1633 } else {
1634 log::verbose("curr_sess={}, max_sess={}", p_cb->curr_sess,
1635 p_cb->max_sess);
1636 failed = false;
1637 }
1638 } else
1639 log::error("failed to create new listen port");
1640 }
1641 if (failed) {
1642 evt_data.rfc_close.handle = p_cb->handle;
1643 evt_data.rfc_close.status = tBTA_JV_STATUS::FAILURE;
1644 evt_data.rfc_close.async = true;
1645 evt_data.rfc_close.port_status = code;
1646 p_pcb->cong = false;
1647
1648 tBTA_JV_RFCOMM_CBACK* p_cback = p_cb->p_cback;
1649 log::verbose(
1650 "PORT_CLOSED before BTA_JV_RFCOMM_CLOSE_EVT: curr_sess={}, max_sess={}",
1651 p_cb->curr_sess, p_cb->max_sess);
1652 if (BTA_JV_ST_SR_CLOSING == p_pcb->state) {
1653 evt_data.rfc_close.async = false;
1654 evt_data.rfc_close.status = tBTA_JV_STATUS::SUCCESS;
1655 }
1656 // p_pcb->state = BTA_JV_ST_NONE;
1657 p_cback(BTA_JV_RFCOMM_CLOSE_EVT, &evt_data, rfcomm_slot_id);
1658 // bta_jv_free_rfc_cb(p_cb, p_pcb);
1659
1660 log::verbose(
1661 "PORT_CLOSED after BTA_JV_RFCOMM_CLOSE_EVT: curr_sess={}, max_sess={}",
1662 p_cb->curr_sess, p_cb->max_sess);
1663 }
1664 }
1665
1666 /*******************************************************************************
1667 *
1668 * Function bta_jv_port_event_sr_cback
1669 *
1670 * Description Callback for RFCOMM server port events
1671 *
1672 * Returns void
1673 *
1674 ******************************************************************************/
bta_jv_port_event_sr_cback(uint32_t code,uint16_t port_handle)1675 static void bta_jv_port_event_sr_cback(uint32_t code, uint16_t port_handle) {
1676 tBTA_JV_PCB* p_pcb = bta_jv_rfc_port_to_pcb(port_handle);
1677 tBTA_JV_RFC_CB* p_cb = bta_jv_rfc_port_to_cb(port_handle);
1678 tBTA_JV evt_data;
1679
1680 if (NULL == p_cb || NULL == p_cb->p_cback) {
1681 log::error("p_cb={}, p_cb->p_cback={}", fmt::ptr(p_cb),
1682 fmt::ptr(p_cb ? p_cb->p_cback : nullptr));
1683 return;
1684 }
1685
1686 log::verbose("code=0x{:x}, port_handle={}, handle={}", code, port_handle,
1687 p_cb->handle);
1688
1689 uint32_t user_data = p_pcb->rfcomm_slot_id;
1690 if (code & PORT_EV_RXCHAR) {
1691 evt_data.data_ind.handle = p_cb->handle;
1692 p_cb->p_cback(BTA_JV_RFCOMM_DATA_IND_EVT, &evt_data, user_data);
1693 }
1694
1695 if (code & PORT_EV_FC) {
1696 p_pcb->cong = (code & PORT_EV_FCS) ? false : true;
1697 evt_data.rfc_cong.cong = p_pcb->cong;
1698 evt_data.rfc_cong.handle = p_cb->handle;
1699 evt_data.rfc_cong.status = tBTA_JV_STATUS::SUCCESS;
1700 p_cb->p_cback(BTA_JV_RFCOMM_CONG_EVT, &evt_data, user_data);
1701 }
1702
1703 if (code & PORT_EV_TXEMPTY) {
1704 bta_jv_pm_conn_idle(p_pcb->p_pm_cb);
1705 }
1706 }
1707
1708 /*******************************************************************************
1709 *
1710 * Function bta_jv_add_rfc_port
1711 *
1712 * Description add a port for server when the existing posts is open
1713 *
1714 * Returns return a pointer to tBTA_JV_PCB just added
1715 *
1716 ******************************************************************************/
bta_jv_add_rfc_port(tBTA_JV_RFC_CB * p_cb,tBTA_JV_PCB * p_pcb_open)1717 static tBTA_JV_PCB* bta_jv_add_rfc_port(tBTA_JV_RFC_CB* p_cb,
1718 tBTA_JV_PCB* p_pcb_open) {
1719 uint8_t used = 0, i, listen = 0;
1720 uint32_t si = 0;
1721 tPORT_STATE port_state;
1722 uint32_t event_mask = BTA_JV_RFC_EV_MASK;
1723 tBTA_JV_PCB* p_pcb = NULL;
1724 tBTA_SEC sec_mask;
1725 if (p_cb->max_sess > 1) {
1726 for (i = 0; i < p_cb->max_sess; i++) {
1727 if (p_cb->rfc_hdl[i] != 0) {
1728 p_pcb = &bta_jv_cb.port_cb[p_cb->rfc_hdl[i] - 1];
1729 if (p_pcb->state == BTA_JV_ST_SR_LISTEN) {
1730 listen++;
1731 if (p_pcb_open == p_pcb) {
1732 log::verbose("port_handle={}, change the listen port to open state",
1733 p_pcb->port_handle);
1734 p_pcb->state = BTA_JV_ST_SR_OPEN;
1735
1736 } else {
1737 log::error(
1738 "open pcb not matching listen one, count={}, listen pcb "
1739 "handle={}, open pcb={}",
1740 listen, p_pcb->port_handle, p_pcb_open->handle);
1741 return NULL;
1742 }
1743 }
1744 used++;
1745 } else if (si == 0) {
1746 si = i + 1;
1747 }
1748 }
1749
1750 log::verbose("max_sess={}, used={}, curr_sess={}, listen={}, si={}",
1751 p_cb->max_sess, used, p_cb->curr_sess, listen, si);
1752 if (used < p_cb->max_sess && listen == 1 && si) {
1753 si--;
1754 if (PORT_GetSecurityMask(p_pcb_open->port_handle, &sec_mask) !=
1755 PORT_SUCCESS) {
1756 log::error("RFCOMM_CreateConnection failed: invalid port_handle");
1757 }
1758
1759 if (RFCOMM_CreateConnectionWithSecurity(
1760 p_cb->sec_id, p_cb->scn, true, BTA_JV_DEF_RFC_MTU,
1761 RawAddress::kAny, &(p_cb->rfc_hdl[si]), bta_jv_port_mgmt_sr_cback,
1762 sec_mask) == PORT_SUCCESS) {
1763 p_cb->curr_sess++;
1764 p_pcb = &bta_jv_cb.port_cb[p_cb->rfc_hdl[si] - 1];
1765 p_pcb->state = BTA_JV_ST_SR_LISTEN;
1766 p_pcb->port_handle = p_cb->rfc_hdl[si];
1767 p_pcb->rfcomm_slot_id = p_pcb_open->rfcomm_slot_id;
1768
1769 if (PORT_ClearKeepHandleFlag(p_pcb->port_handle) != PORT_SUCCESS) {
1770 log::warn("Unable to clear RFCOMM server keep handle flag handle:{}",
1771 p_pcb->port_handle);
1772 }
1773 if (PORT_SetEventMaskAndCallback(p_pcb->port_handle, event_mask,
1774 bta_jv_port_event_sr_cback) !=
1775 PORT_SUCCESS) {
1776 log::warn(
1777 "Unable to set RFCOMM server event mask and callback handle:{}",
1778 p_pcb->port_handle);
1779 }
1780 if (PORT_SetDataCOCallback(p_pcb->port_handle,
1781 bta_jv_port_data_co_cback) != PORT_SUCCESS) {
1782 log::warn("Unable to set RFCOMM server data callback handle:{}",
1783 p_pcb->port_handle);
1784 }
1785 if (PORT_GetState(p_pcb->port_handle, &port_state) != PORT_SUCCESS) {
1786 log::warn("Unable to get RFCOMM server state handle:{}",
1787 p_pcb->port_handle);
1788 }
1789
1790 port_state.fc_type = (PORT_FC_CTS_ON_INPUT | PORT_FC_CTS_ON_OUTPUT);
1791
1792 if (PORT_SetState(p_pcb->port_handle, &port_state) != PORT_SUCCESS) {
1793 }
1794 p_pcb->handle = BTA_JV_RFC_H_S_TO_HDL(p_cb->handle, si);
1795 log::verbose("p_pcb->handle=0x{:x}, curr_sess={}", p_pcb->handle,
1796 p_cb->curr_sess);
1797 } else {
1798 log::error("RFCOMM_CreateConnection failed");
1799 return NULL;
1800 }
1801 } else {
1802 log::error("cannot create new rfc listen port");
1803 return NULL;
1804 }
1805 }
1806 log::verbose("sec id in use={}, rfc_cb in use={}", get_sec_id_used(),
1807 get_rfc_cb_used());
1808 return p_pcb;
1809 }
1810
1811 /* waits for an RFCOMM client to connect */
bta_jv_rfcomm_start_server(tBTA_SEC sec_mask,uint8_t local_scn,uint8_t max_session,tBTA_JV_RFCOMM_CBACK * p_cback,uint32_t rfcomm_slot_id)1812 void bta_jv_rfcomm_start_server(tBTA_SEC sec_mask, uint8_t local_scn,
1813 uint8_t max_session,
1814 tBTA_JV_RFCOMM_CBACK* p_cback,
1815 uint32_t rfcomm_slot_id) {
1816 uint16_t handle = 0;
1817 uint32_t event_mask = BTA_JV_RFC_EV_MASK;
1818 tPORT_STATE port_state;
1819 tBTA_JV_RFC_CB* p_cb = NULL;
1820 tBTA_JV_PCB* p_pcb;
1821 tBTA_JV_RFCOMM_START evt_data;
1822
1823 memset(&evt_data, 0, sizeof(evt_data));
1824 evt_data.status = tBTA_JV_STATUS::FAILURE;
1825
1826 do {
1827 if (RFCOMM_CreateConnectionWithSecurity(
1828 0, local_scn, true, BTA_JV_DEF_RFC_MTU, RawAddress::kAny, &handle,
1829 bta_jv_port_mgmt_sr_cback, sec_mask) != PORT_SUCCESS) {
1830 log::error("RFCOMM_CreateConnection failed");
1831 break;
1832 }
1833
1834 p_cb = bta_jv_alloc_rfc_cb(handle, &p_pcb);
1835 if (!p_cb) {
1836 log::error("run out of rfc control block");
1837 break;
1838 }
1839
1840 p_cb->max_sess = max_session;
1841 p_cb->p_cback = p_cback;
1842 p_cb->scn = local_scn;
1843 p_pcb->state = BTA_JV_ST_SR_LISTEN;
1844 p_pcb->rfcomm_slot_id = rfcomm_slot_id;
1845 evt_data.status = tBTA_JV_STATUS::SUCCESS;
1846 evt_data.handle = p_cb->handle;
1847 evt_data.use_co = true;
1848
1849 if (PORT_ClearKeepHandleFlag(handle) != PORT_SUCCESS) {
1850 log::warn("Unable to clear RFCOMM server keep handle flag handle:{}",
1851 handle);
1852 }
1853 if (PORT_SetEventMaskAndCallback(
1854 handle, event_mask, bta_jv_port_event_sr_cback) != PORT_SUCCESS) {
1855 log::warn("Unable to set RFCOMM server event mask and callback handle:{}",
1856 handle);
1857 }
1858 if (PORT_GetState(handle, &port_state) != PORT_SUCCESS) {
1859 log::warn("Unable to get RFCOMM server state handle:{}", handle);
1860 }
1861
1862 port_state.fc_type = (PORT_FC_CTS_ON_INPUT | PORT_FC_CTS_ON_OUTPUT);
1863
1864 if (PORT_SetState(handle, &port_state) != PORT_SUCCESS) {
1865 log::warn("Unable to set RFCOMM port state handle:{}", handle);
1866 };
1867 } while (0);
1868
1869 tBTA_JV bta_jv;
1870 bta_jv.rfc_start = evt_data;
1871 p_cback(BTA_JV_RFCOMM_START_EVT, &bta_jv, rfcomm_slot_id);
1872 if (bta_jv.rfc_start.status == tBTA_JV_STATUS::SUCCESS) {
1873 if (PORT_SetDataCOCallback(handle, bta_jv_port_data_co_cback) !=
1874 PORT_SUCCESS) {
1875 log::error("Unable to set RFCOMM server data callback handle:{}", handle);
1876 }
1877 } else {
1878 if (handle) {
1879 if (RFCOMM_RemoveConnection(handle) != PORT_SUCCESS) {
1880 log::warn("Unable to remote RFCOMM server connection handle:{}",
1881 handle);
1882 }
1883 }
1884 }
1885 }
1886
1887 /* stops an RFCOMM server */
bta_jv_rfcomm_stop_server(uint32_t handle,uint32_t rfcomm_slot_id)1888 void bta_jv_rfcomm_stop_server(uint32_t handle, uint32_t rfcomm_slot_id) {
1889 if (!handle) {
1890 log::error("jv handle is null");
1891 return;
1892 }
1893
1894 log::verbose("");
1895 tBTA_JV_RFC_CB* p_cb = NULL;
1896 tBTA_JV_PCB* p_pcb = NULL;
1897
1898 if (!find_rfc_pcb(rfcomm_slot_id, &p_cb, &p_pcb)) return;
1899 log::verbose("p_pcb={}, p_pcb->port_handle={}", fmt::ptr(p_pcb),
1900 p_pcb->port_handle);
1901 bta_jv_free_rfc_cb(p_cb, p_pcb);
1902 }
1903
1904 /* write data to an RFCOMM connection */
bta_jv_rfcomm_write(uint32_t handle,uint32_t req_id,tBTA_JV_RFC_CB * p_cb,tBTA_JV_PCB * p_pcb)1905 void bta_jv_rfcomm_write(uint32_t handle, uint32_t req_id, tBTA_JV_RFC_CB* p_cb,
1906 tBTA_JV_PCB* p_pcb) {
1907 if (p_pcb->state == BTA_JV_ST_NONE) {
1908 log::error("in state BTA_JV_ST_NONE - cannot write");
1909 return;
1910 }
1911
1912 tBTA_JV_RFCOMM_WRITE evt_data;
1913 evt_data.status = tBTA_JV_STATUS::FAILURE;
1914 evt_data.handle = handle;
1915 evt_data.req_id = req_id;
1916 evt_data.cong = p_pcb->cong;
1917 evt_data.len = 0;
1918
1919 bta_jv_pm_conn_busy(p_pcb->p_pm_cb);
1920
1921 if (!evt_data.cong &&
1922 PORT_WriteDataCO(p_pcb->port_handle, &evt_data.len) == PORT_SUCCESS) {
1923 evt_data.status = tBTA_JV_STATUS::SUCCESS;
1924 }
1925
1926 // Update congestion flag
1927 evt_data.cong = p_pcb->cong;
1928
1929 if (!p_cb->p_cback) {
1930 log::error("No JV callback set");
1931 return;
1932 }
1933
1934 tBTA_JV bta_jv;
1935 bta_jv.rfc_write = evt_data;
1936 p_cb->p_cback(BTA_JV_RFCOMM_WRITE_EVT, &bta_jv, p_pcb->rfcomm_slot_id);
1937 }
1938
1939 /* Set or free power mode profile for a JV application */
bta_jv_set_pm_profile(uint32_t handle,tBTA_JV_PM_ID app_id,tBTA_JV_CONN_STATE init_st)1940 void bta_jv_set_pm_profile(uint32_t handle, tBTA_JV_PM_ID app_id,
1941 tBTA_JV_CONN_STATE init_st) {
1942 tBTA_JV_STATUS status;
1943 tBTA_JV_PM_CB* p_cb;
1944
1945 log::verbose("handle=0x{:x}, app_id={}, init_st={}", handle, app_id,
1946 bta_jv_conn_state_text(init_st));
1947
1948 /* clear PM control block */
1949 if (app_id == BTA_JV_PM_ID_CLEAR) {
1950 status = bta_jv_free_set_pm_profile_cb(handle);
1951
1952 if (status != tBTA_JV_STATUS::SUCCESS) {
1953 log::warn("free pm cb failed: reason={}", bta_jv_status_text(status));
1954 }
1955 } else /* set PM control block */
1956 {
1957 p_cb = bta_jv_alloc_set_pm_profile_cb(handle, app_id);
1958
1959 if (NULL != p_cb)
1960 bta_jv_pm_state_change(p_cb, init_st);
1961 else
1962 log::warn("failed");
1963 }
1964 }
1965
1966 /*******************************************************************************
1967 *
1968 * Function bta_jv_pm_conn_busy
1969 *
1970 * Description set pm connection busy state (input param safe)
1971 *
1972 * Params p_cb: pm control block of jv connection
1973 *
1974 * Returns void
1975 *
1976 ******************************************************************************/
bta_jv_pm_conn_busy(tBTA_JV_PM_CB * p_cb)1977 static void bta_jv_pm_conn_busy(tBTA_JV_PM_CB* p_cb) {
1978 if ((NULL != p_cb) && (BTA_JV_PM_IDLE_ST == p_cb->state))
1979 bta_jv_pm_state_change(p_cb, BTA_JV_CONN_BUSY);
1980 }
1981
1982 /*******************************************************************************
1983 *
1984 * Function bta_jv_pm_conn_idle
1985 *
1986 * Description set pm connection idle state (input param safe)
1987 *
1988 * Params p_cb: pm control block of jv connection
1989 *
1990 * Returns void
1991 *
1992 ******************************************************************************/
bta_jv_pm_conn_idle(tBTA_JV_PM_CB * p_cb)1993 static void bta_jv_pm_conn_idle(tBTA_JV_PM_CB* p_cb) {
1994 if ((NULL != p_cb) && (BTA_JV_PM_IDLE_ST != p_cb->state))
1995 bta_jv_pm_state_change(p_cb, BTA_JV_CONN_IDLE);
1996 }
1997
1998 /*******************************************************************************
1999 *
2000 * Function bta_jv_pm_state_change
2001 *
2002 * Description Notify power manager there is state change
2003 *
2004 * Params p_cb: must be NONE NULL
2005 *
2006 * Returns void
2007 *
2008 ******************************************************************************/
bta_jv_pm_state_change(tBTA_JV_PM_CB * p_cb,const tBTA_JV_CONN_STATE state)2009 static void bta_jv_pm_state_change(tBTA_JV_PM_CB* p_cb,
2010 const tBTA_JV_CONN_STATE state) {
2011 log::verbose(
2012 "p_cb={}, handle=0x{:x}, busy/idle_state={}, app_id={}, conn_state={}",
2013 fmt::ptr(p_cb), p_cb->handle, p_cb->state, p_cb->app_id,
2014 bta_jv_conn_state_text(state));
2015
2016 switch (state) {
2017 case BTA_JV_CONN_OPEN:
2018 bta_sys_conn_open(BTA_ID_JV, p_cb->app_id, p_cb->peer_bd_addr);
2019 break;
2020
2021 case BTA_JV_CONN_CLOSE:
2022 bta_sys_conn_close(BTA_ID_JV, p_cb->app_id, p_cb->peer_bd_addr);
2023 break;
2024
2025 case BTA_JV_APP_OPEN:
2026 bta_sys_app_open(BTA_ID_JV, p_cb->app_id, p_cb->peer_bd_addr);
2027 break;
2028
2029 case BTA_JV_APP_CLOSE:
2030 bta_sys_app_close(BTA_ID_JV, p_cb->app_id, p_cb->peer_bd_addr);
2031 break;
2032
2033 case BTA_JV_SCO_OPEN:
2034 bta_sys_sco_open(BTA_ID_JV, p_cb->app_id, p_cb->peer_bd_addr);
2035 break;
2036
2037 case BTA_JV_SCO_CLOSE:
2038 bta_sys_sco_close(BTA_ID_JV, p_cb->app_id, p_cb->peer_bd_addr);
2039 break;
2040
2041 case BTA_JV_CONN_IDLE:
2042 p_cb->state = BTA_JV_PM_IDLE_ST;
2043 bta_sys_idle(BTA_ID_JV, p_cb->app_id, p_cb->peer_bd_addr);
2044 break;
2045
2046 case BTA_JV_CONN_BUSY:
2047 p_cb->state = BTA_JV_PM_BUSY_ST;
2048 bta_sys_busy(BTA_ID_JV, p_cb->app_id, p_cb->peer_bd_addr);
2049 break;
2050
2051 default:
2052 log::warn("Invalid state={}", bta_jv_conn_state_text(state));
2053 break;
2054 }
2055 }
2056
2057 /*******************************************************************************
2058 *
2059 * Function bta_jv_reset_sniff_timer
2060 *
2061 * Description reset pm sniff timer state (input param safe)
2062 *
2063 * Params p_cb: pm control block of jv connection
2064 *
2065 * Returns void
2066 *
2067 ******************************************************************************/
bta_jv_reset_sniff_timer(tBTA_JV_PM_CB * p_cb)2068 static void bta_jv_reset_sniff_timer(tBTA_JV_PM_CB* p_cb) {
2069 if (NULL != p_cb) {
2070 p_cb->state = BTA_JV_PM_IDLE_ST;
2071 bta_sys_reset_sniff(BTA_ID_JV, p_cb->app_id, p_cb->peer_bd_addr);
2072 }
2073 }
2074 /******************************************************************************/
2075
2076 namespace bluetooth::legacy::testing {
2077
bta_jv_start_discovery_cback(uint32_t rfcomm_slot_id,const RawAddress & bd_addr,tSDP_RESULT result)2078 void bta_jv_start_discovery_cback(uint32_t rfcomm_slot_id,
2079 const RawAddress& bd_addr,
2080 tSDP_RESULT result) {
2081 ::bta_jv_start_discovery_cback(rfcomm_slot_id, bd_addr, result);
2082 }
2083
2084 } // namespace bluetooth::legacy::testing
2085