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 is the implementation of the JAVA API for Bluetooth Wireless
22  *  Technology (JABWT) as specified by the JSR82 specificiation
23  *
24  ******************************************************************************/
25 
26 #include <base/functional/bind.h>
27 #include <base/location.h>
28 #include <bluetooth/log.h>
29 
30 #include <cstdint>
31 #include <memory>
32 
33 #include "bta/jv/bta_jv_int.h"
34 #include "internal_include/bt_target.h"
35 #include "internal_include/bt_trace.h"
36 #include "osi/include/allocator.h"
37 #include "stack/include/bt_hdr.h"
38 #include "stack/include/gap_api.h"
39 #include "stack/include/main_thread.h"
40 #include "types/bluetooth/uuid.h"
41 #include "types/raw_address.h"
42 
43 using base::Bind;
44 using bluetooth::Uuid;
45 using namespace bluetooth;
46 
47 namespace {
48 bool bta_jv_enabled = false;
49 }
50 
51 /*******************************************************************************
52  *
53  * Function         BTA_JvEnable
54  *
55  * Description      Enable the Java I/F service. When the enable
56  *                  operation is complete the callback function will be
57  *                  called with a BTA_JV_ENABLE_EVT. This function must
58  *                  be called before other function in the JV API are
59  *                  called.
60  *
61  * Returns          tBTA_JV_STATUS::SUCCESS if successful.
62  *                  tBTA_JV_STATUS::FAILURE if internal failure.
63  *
64  ******************************************************************************/
BTA_JvEnable(tBTA_JV_DM_CBACK * p_cback)65 tBTA_JV_STATUS BTA_JvEnable(tBTA_JV_DM_CBACK* p_cback) {
66   log::verbose("");
67   if (!p_cback || bta_jv_enabled) {
68     log::error("failure");
69     return tBTA_JV_STATUS::FAILURE;
70   }
71 
72   memset(&bta_jv_cb, 0, sizeof(tBTA_JV_CB));
73   /* set handle to invalid value by default */
74   for (int i = 0; i < BTA_JV_PM_MAX_NUM; i++) {
75     bta_jv_cb.pm_cb[i].handle = BTA_JV_PM_HANDLE_CLEAR;
76   }
77   bta_jv_cb.dyn_psm = 0xfff;
78   used_l2cap_classic_dynamic_psm = {};
79 
80   bta_jv_enabled = true;
81 
82   do_in_main_thread(FROM_HERE, Bind(&bta_jv_enable, p_cback));
83   return tBTA_JV_STATUS::SUCCESS;
84 }
85 
86 /** Disable the Java I/F */
BTA_JvDisable(void)87 void BTA_JvDisable(void) {
88   log::verbose("");
89 
90   bta_jv_enabled = false;
91 
92   do_in_main_thread(FROM_HERE, Bind(&bta_jv_disable));
93 }
94 
95 /*******************************************************************************
96  *
97  * Function         BTA_JvGetChannelId
98  *
99  * Description      This function reserves a SCN (server channel number) for
100  *                  applications running over RFCOMM, L2CAP of L2CAP_LE.
101  *                  It is primarily called by server profiles/applications to
102  *                  register their SCN into the SDP database. The SCN is
103  *                  reported by the tBTA_JV_DM_CBACK callback with a
104  *                  BTA_JV_GET_SCN_EVT for RFCOMM channels and
105  *                  BTA_JV_GET_PSM_EVT for L2CAP and LE.
106  *                  If the SCN/PSM reported is 0, that means all resources are
107  *                  exhausted.
108  * Parameters
109  *   conn_type      one of BTA_JV_CONN_TYPE
110  *   user_data      Any uservalue - will be returned in the resulting event.
111  *   channel        Only used for RFCOMM - to try to allocate a specific RFCOMM
112  *                  channel.
113  *
114  * Returns          void
115  *
116  ******************************************************************************/
BTA_JvGetChannelId(tBTA_JV_CONN_TYPE conn_type,uint32_t id,int32_t channel)117 void BTA_JvGetChannelId(tBTA_JV_CONN_TYPE conn_type, uint32_t id,
118                         int32_t channel) {
119   log::verbose("conn_type:{}, id:{}, channel:{}",
120                bta_jv_conn_type_text(conn_type), id, channel);
121 
122   do_in_main_thread(FROM_HERE,
123                     Bind(&bta_jv_get_channel_id, conn_type, channel, id, id));
124 }
125 
126 /*******************************************************************************
127  *
128  * Function         BTA_JvFreeChannel
129  *
130  * Description      This function frees a server channel number that was used
131  *                  by an application running over RFCOMM.
132  * Parameters
133  *   channel        The channel to free
134  *   conn_type      one of BTA_JV_CONN_TYPE
135  *
136  * Returns          tBTA_JV_STATUS::SUCCESS, if the request is being processed.
137  *                  tBTA_JV_STATUS::FAILURE, otherwise.
138  *
139  ******************************************************************************/
BTA_JvFreeChannel(uint16_t channel,tBTA_JV_CONN_TYPE conn_type)140 tBTA_JV_STATUS BTA_JvFreeChannel(uint16_t channel,
141                                  tBTA_JV_CONN_TYPE conn_type) {
142   log::verbose("channel:{}, conn_type:{}", channel,
143                bta_jv_conn_type_text(conn_type));
144 
145   do_in_main_thread(FROM_HERE, Bind(&bta_jv_free_scn, conn_type, channel));
146   return tBTA_JV_STATUS::SUCCESS;
147 }
148 
149 /*******************************************************************************
150  *
151  * Function         BTA_JvStartDiscovery
152  *
153  * Description      This function performs service discovery for the services
154  *                  provided by the given peer device. When the operation is
155  *                  complete the tBTA_JV_DM_CBACK callback function will be
156  *                  called with a BTA_JV_DISCOVERY_COMP_EVT.
157  *
158  * Returns          tBTA_JV_STATUS::SUCCESS, if the request is being processed.
159  *                  tBTA_JV_STATUS::FAILURE, otherwise.
160  *
161  ******************************************************************************/
BTA_JvStartDiscovery(const RawAddress & bd_addr,uint16_t num_uuid,const Uuid * p_uuid_list,uint32_t rfcomm_slot_id)162 tBTA_JV_STATUS BTA_JvStartDiscovery(const RawAddress& bd_addr,
163                                     uint16_t num_uuid, const Uuid* p_uuid_list,
164                                     uint32_t rfcomm_slot_id) {
165   log::verbose("bd_addr:{}, rfcomm_slot_id:{}, num_uuid:{}", bd_addr,
166                rfcomm_slot_id, num_uuid);
167 
168   Uuid* uuid_list_copy = new Uuid[num_uuid];
169   memcpy(uuid_list_copy, p_uuid_list, num_uuid * sizeof(Uuid));
170 
171   do_in_main_thread(FROM_HERE,
172                     Bind(&bta_jv_start_discovery, bd_addr, num_uuid,
173                          base::Owned(uuid_list_copy), rfcomm_slot_id));
174   return tBTA_JV_STATUS::SUCCESS;
175 }
176 
177 /*******************************************************************************
178  *
179  * Function         BTA_JvCreateRecord
180  *
181  * Description      Create a service record in the local SDP database.
182  *                  When the operation is complete the tBTA_JV_DM_CBACK callback
183  *                  function will be called with a BTA_JV_CREATE_RECORD_EVT.
184  *
185  * Returns          tBTA_JV_STATUS::SUCCESS, if the request is being processed.
186  *                  tBTA_JV_STATUS::FAILURE, otherwise.
187  *
188  ******************************************************************************/
BTA_JvCreateRecordByUser(uint32_t rfcomm_slot_id)189 tBTA_JV_STATUS BTA_JvCreateRecordByUser(uint32_t rfcomm_slot_id) {
190   log::verbose("rfcomm_slot_id: {}", rfcomm_slot_id);
191 
192   do_in_main_thread(FROM_HERE, Bind(&bta_jv_create_record, rfcomm_slot_id));
193   return tBTA_JV_STATUS::SUCCESS;
194 }
195 
196 /*******************************************************************************
197  *
198  * Function         BTA_JvDeleteRecord
199  *
200  * Description      Delete a service record in the local SDP database.
201  *
202  * Returns          tBTA_JV_STATUS::SUCCESS, if the request is being processed.
203  *                  tBTA_JV_STATUS::FAILURE, otherwise.
204  *
205  ******************************************************************************/
BTA_JvDeleteRecord(uint32_t handle)206 tBTA_JV_STATUS BTA_JvDeleteRecord(uint32_t handle) {
207   log::verbose("handle:{}", handle);
208 
209   do_in_main_thread(FROM_HERE, Bind(&bta_jv_delete_record, handle));
210   return tBTA_JV_STATUS::SUCCESS;
211 }
212 
213 /*******************************************************************************
214  *
215  * Function         BTA_JvL2capConnect
216  *
217  * Description      Initiate a connection as a L2CAP client to the given BD
218  *                  Address.
219  *                  When the connection is initiated or failed to initiate,
220  *                  tBTA_JV_L2CAP_CBACK is called with BTA_JV_L2CAP_CL_INIT_EVT
221  *                  When the connection is established or failed,
222  *                  tBTA_JV_L2CAP_CBACK is called with BTA_JV_L2CAP_OPEN_EVT
223  *
224  ******************************************************************************/
BTA_JvL2capConnect(tBTA_JV_CONN_TYPE conn_type,tBTA_SEC sec_mask,std::unique_ptr<tL2CAP_ERTM_INFO> ertm_info,uint16_t remote_psm,uint16_t rx_mtu,std::unique_ptr<tL2CAP_CFG_INFO> cfg,const RawAddress & peer_bd_addr,tBTA_JV_L2CAP_CBACK * p_cback,uint32_t l2cap_socket_id)225 void BTA_JvL2capConnect(tBTA_JV_CONN_TYPE conn_type, tBTA_SEC sec_mask,
226                         std::unique_ptr<tL2CAP_ERTM_INFO> ertm_info,
227                         uint16_t remote_psm, uint16_t rx_mtu,
228                         std::unique_ptr<tL2CAP_CFG_INFO> cfg,
229                         const RawAddress& peer_bd_addr,
230                         tBTA_JV_L2CAP_CBACK* p_cback,
231                         uint32_t l2cap_socket_id) {
232   log::verbose(
233       "conn_type:{}, remote_psm:{}, peer_bd_addr:{}, "
234       "l2cap_socket_id:{}",
235       bta_jv_conn_type_text(conn_type), remote_psm, peer_bd_addr,
236       l2cap_socket_id);
237   log::assert_that(p_cback != nullptr, "assert failed: p_cback != nullptr");
238 
239   do_in_main_thread(FROM_HERE,
240                     Bind(&bta_jv_l2cap_connect, conn_type, sec_mask, remote_psm,
241                          rx_mtu, peer_bd_addr, base::Passed(&cfg),
242                          base::Passed(&ertm_info), p_cback, l2cap_socket_id));
243 }
244 
245 /*******************************************************************************
246  *
247  * Function         BTA_JvL2capClose
248  *
249  * Description      This function closes an L2CAP client connection
250  *
251  * Returns          tBTA_JV_STATUS::SUCCESS, if the request is being processed.
252  *                  tBTA_JV_STATUS::FAILURE, otherwise.
253  *
254  ******************************************************************************/
BTA_JvL2capClose(uint32_t handle)255 tBTA_JV_STATUS BTA_JvL2capClose(uint32_t handle) {
256   log::verbose("handle:{}", handle);
257 
258   if (handle >= BTA_JV_MAX_L2C_CONN || !bta_jv_cb.l2c_cb[handle].p_cback)
259     return tBTA_JV_STATUS::FAILURE;
260 
261   do_in_main_thread(
262       FROM_HERE, Bind(&bta_jv_l2cap_close, handle, &bta_jv_cb.l2c_cb[handle]));
263   return tBTA_JV_STATUS::SUCCESS;
264 }
265 
266 /*******************************************************************************
267  *
268  * Function         BTA_JvL2capStartServer
269  *
270  * Description      This function starts an L2CAP server and listens for an
271  *                  L2CAP connection from a remote Bluetooth device.  When the
272  *                  server is started successfully, tBTA_JV_L2CAP_CBACK is
273  *                  called with BTA_JV_L2CAP_START_EVT.  When the connection is
274  *                  established tBTA_JV_L2CAP_CBACK is called with
275  *                  BTA_JV_L2CAP_OPEN_EVT.
276  *
277  * Returns          void
278  *
279  ******************************************************************************/
BTA_JvL2capStartServer(tBTA_JV_CONN_TYPE conn_type,tBTA_SEC sec_mask,std::unique_ptr<tL2CAP_ERTM_INFO> ertm_info,uint16_t local_psm,uint16_t rx_mtu,std::unique_ptr<tL2CAP_CFG_INFO> cfg,tBTA_JV_L2CAP_CBACK * p_cback,uint32_t l2cap_socket_id)280 void BTA_JvL2capStartServer(tBTA_JV_CONN_TYPE conn_type, tBTA_SEC sec_mask,
281                             std::unique_ptr<tL2CAP_ERTM_INFO> ertm_info,
282                             uint16_t local_psm, uint16_t rx_mtu,
283                             std::unique_ptr<tL2CAP_CFG_INFO> cfg,
284                             tBTA_JV_L2CAP_CBACK* p_cback,
285                             uint32_t l2cap_socket_id) {
286   log::verbose("conn_type:{}, local_psm:{}, l2cap_socket_id:{}",
287                bta_jv_conn_type_text(conn_type), local_psm, l2cap_socket_id);
288   CHECK(p_cback);
289 
290   do_in_main_thread(FROM_HERE,
291                     Bind(&bta_jv_l2cap_start_server, conn_type, sec_mask,
292                          local_psm, rx_mtu, base::Passed(&cfg),
293                          base::Passed(&ertm_info), p_cback, l2cap_socket_id));
294 }
295 
296 /*******************************************************************************
297  *
298  * Function         BTA_JvL2capStopServer
299  *
300  * Description      This function stops the L2CAP server. If the server has an
301  *                  active connection, it would be closed.
302  *
303  * Returns          tBTA_JV_STATUS::SUCCESS, if the request is being processed.
304  *                  tBTA_JV_STATUS::FAILURE, otherwise.
305  *
306  ******************************************************************************/
BTA_JvL2capStopServer(uint16_t local_psm,uint32_t l2cap_socket_id)307 tBTA_JV_STATUS BTA_JvL2capStopServer(uint16_t local_psm,
308                                      uint32_t l2cap_socket_id) {
309   log::verbose("local_psm:{}, l2cap_socket_id:{}", local_psm, l2cap_socket_id);
310 
311   do_in_main_thread(
312       FROM_HERE, Bind(&bta_jv_l2cap_stop_server, local_psm, l2cap_socket_id));
313   return tBTA_JV_STATUS::SUCCESS;
314 }
315 
316 /*******************************************************************************
317  *
318  * Function         BTA_JvL2capRead
319  *
320  * Description      This function reads data from an L2CAP connection
321  *                  When the operation is complete, tBTA_JV_L2CAP_CBACK is
322  *                  called with BTA_JV_L2CAP_READ_EVT.
323  *
324  * Returns          tBTA_JV_STATUS::SUCCESS, if the request is being processed.
325  *                  tBTA_JV_STATUS::FAILURE, otherwise.
326  *
327  ******************************************************************************/
BTA_JvL2capRead(uint32_t handle,uint32_t req_id,uint8_t * p_data,uint16_t len)328 tBTA_JV_STATUS BTA_JvL2capRead(uint32_t handle, uint32_t req_id,
329                                uint8_t* p_data, uint16_t len) {
330   log::verbose("handle:{}, req_id:{}, len:{}", handle, req_id, len);
331 
332   if (handle >= BTA_JV_MAX_L2C_CONN || !bta_jv_cb.l2c_cb[handle].p_cback)
333     return tBTA_JV_STATUS::FAILURE;
334 
335   tBTA_JV_L2CAP_READ evt_data;
336   evt_data.status = tBTA_JV_STATUS::FAILURE;
337   evt_data.handle = handle;
338   evt_data.req_id = req_id;
339   evt_data.p_data = p_data;
340   evt_data.len = 0;
341 
342   if (BT_PASS ==
343       GAP_ConnReadData((uint16_t)handle, p_data, len, &evt_data.len)) {
344     evt_data.status = tBTA_JV_STATUS::SUCCESS;
345   }
346   bta_jv_cb.l2c_cb[handle].p_cback(BTA_JV_L2CAP_READ_EVT, (tBTA_JV*)&evt_data,
347                                    bta_jv_cb.l2c_cb[handle].l2cap_socket_id);
348   return tBTA_JV_STATUS::SUCCESS;
349 }
350 
351 /*******************************************************************************
352  *
353  * Function         BTA_JvL2capReady
354  *
355  * Description      This function determined if there is data to read from
356  *                    an L2CAP connection
357  *
358  * Returns          tBTA_JV_STATUS::SUCCESS, if data queue size is in
359  *                  *p_data_size.
360  *                  tBTA_JV_STATUS::FAILURE, if error.
361  *
362  ******************************************************************************/
BTA_JvL2capReady(uint32_t handle,uint32_t * p_data_size)363 tBTA_JV_STATUS BTA_JvL2capReady(uint32_t handle, uint32_t* p_data_size) {
364   tBTA_JV_STATUS status = tBTA_JV_STATUS::FAILURE;
365 
366   log::verbose("handle:{}", handle);
367   if (p_data_size && handle < BTA_JV_MAX_L2C_CONN &&
368       bta_jv_cb.l2c_cb[handle].p_cback) {
369     *p_data_size = 0;
370     if (BT_PASS == GAP_GetRxQueueCnt((uint16_t)handle, p_data_size)) {
371       status = tBTA_JV_STATUS::SUCCESS;
372     }
373   }
374 
375   return (status);
376 }
377 
378 /*******************************************************************************
379  *
380  * Function         BTA_JvL2capWrite
381  *
382  * Description      This function writes data to an L2CAP connection
383  *                  When the operation is complete, tBTA_JV_L2CAP_CBACK is
384  *                  called with BTA_JV_L2CAP_WRITE_EVT. Works for
385  *                  PSM-based connections. This function takes ownership of
386  *                  p_data, and will osi_free it. Data length must be smaller
387  *                  than remote maximum SDU size.
388  *
389  * Returns          tBTA_JV_STATUS::SUCCESS, if the request is being processed.
390  *                  tBTA_JV_STATUS::FAILURE, otherwise.
391  *
392  ******************************************************************************/
BTA_JvL2capWrite(uint32_t handle,uint32_t req_id,BT_HDR * msg,uint32_t user_id)393 tBTA_JV_STATUS BTA_JvL2capWrite(uint32_t handle, uint32_t req_id, BT_HDR* msg,
394                                 uint32_t user_id) {
395   log::verbose("handle:{}, user_id:{}", handle, user_id);
396 
397   if (handle >= BTA_JV_MAX_L2C_CONN || !bta_jv_cb.l2c_cb[handle].p_cback) {
398     osi_free(msg);
399     return tBTA_JV_STATUS::FAILURE;
400   }
401 
402   do_in_main_thread(FROM_HERE, Bind(&bta_jv_l2cap_write, handle, req_id, msg,
403                                     user_id, &bta_jv_cb.l2c_cb[handle]));
404   return tBTA_JV_STATUS::SUCCESS;
405 }
406 
407 /*******************************************************************************
408  *
409  * Function         BTA_JvRfcommConnect
410  *
411  * Description      This function makes an RFCOMM conection to a remote BD
412  *                  Address.
413  *                  When the connection is initiated or failed to initiate,
414  *                  tBTA_JV_RFCOMM_CBACK is called with
415  *                  BTA_JV_RFCOMM_CL_INIT_EVT
416  *                  When the connection is established or failed,
417  *                  tBTA_JV_RFCOMM_CBACK is called with BTA_JV_RFCOMM_OPEN_EVT
418  *
419  * Returns          tBTA_JV_STATUS::SUCCESS, if the request is being processed.
420  *                  tBTA_JV_STATUS::FAILURE, otherwise.
421  *
422  ******************************************************************************/
BTA_JvRfcommConnect(tBTA_SEC sec_mask,uint8_t remote_scn,const RawAddress & peer_bd_addr,tBTA_JV_RFCOMM_CBACK * p_cback,uint32_t rfcomm_slot_id)423 tBTA_JV_STATUS BTA_JvRfcommConnect(tBTA_SEC sec_mask, uint8_t remote_scn,
424                                    const RawAddress& peer_bd_addr,
425                                    tBTA_JV_RFCOMM_CBACK* p_cback,
426                                    uint32_t rfcomm_slot_id) {
427   log::verbose("remote_scn:{}, peer_bd_addr:{}, rfcomm_slot_id:{}", remote_scn,
428                peer_bd_addr, rfcomm_slot_id);
429 
430   if (!p_cback) return tBTA_JV_STATUS::FAILURE; /* Nothing to do */
431 
432   do_in_main_thread(FROM_HERE,
433                     Bind(&bta_jv_rfcomm_connect, sec_mask, remote_scn,
434                          peer_bd_addr, p_cback, rfcomm_slot_id));
435   return tBTA_JV_STATUS::SUCCESS;
436 }
437 
438 /*******************************************************************************
439  *
440  * Function         BTA_JvRfcommClose
441  *
442  * Description      This function closes an RFCOMM connection
443  *
444  * Returns          tBTA_JV_STATUS::SUCCESS, if the request is being processed.
445  *                  tBTA_JV_STATUS::FAILURE, otherwise.
446  *
447  ******************************************************************************/
BTA_JvRfcommClose(uint32_t handle,uint32_t rfcomm_slot_id)448 tBTA_JV_STATUS BTA_JvRfcommClose(uint32_t handle, uint32_t rfcomm_slot_id) {
449   uint32_t hi = ((handle & BTA_JV_RFC_HDL_MASK) & ~BTA_JV_RFCOMM_MASK) - 1;
450   uint32_t si = BTA_JV_RFC_HDL_TO_SIDX(handle);
451 
452   log::verbose("handle:{}, rfcomm_slot_id:{}", handle, rfcomm_slot_id);
453 
454   if (hi >= BTA_JV_MAX_RFC_CONN || !bta_jv_cb.rfc_cb[hi].p_cback ||
455       si >= BTA_JV_MAX_RFC_SR_SESSION || !bta_jv_cb.rfc_cb[hi].rfc_hdl[si])
456     return tBTA_JV_STATUS::FAILURE;
457 
458   do_in_main_thread(FROM_HERE,
459                     Bind(&bta_jv_rfcomm_close, handle, rfcomm_slot_id));
460   return tBTA_JV_STATUS::SUCCESS;
461 }
462 
463 /*******************************************************************************
464  *
465  * Function         BTA_JvRfcommStartServer
466  *
467  * Description      This function starts listening for an RFCOMM connection
468  *                  request from a remote Bluetooth device.  When the server is
469  *                  started successfully, tBTA_JV_RFCOMM_CBACK is called
470  *                  with BTA_JV_RFCOMM_START_EVT.
471  *                  When the connection is established, tBTA_JV_RFCOMM_CBACK
472  *                  is called with BTA_JV_RFCOMM_OPEN_EVT.
473  *
474  * Returns          tBTA_JV_STATUS::SUCCESS, if the request is being processed.
475  *                  tBTA_JV_STATUS::FAILURE, otherwise.
476  *
477  ******************************************************************************/
BTA_JvRfcommStartServer(tBTA_SEC sec_mask,uint8_t local_scn,uint8_t max_session,tBTA_JV_RFCOMM_CBACK * p_cback,uint32_t rfcomm_slot_id)478 tBTA_JV_STATUS BTA_JvRfcommStartServer(tBTA_SEC sec_mask, uint8_t local_scn,
479                                        uint8_t max_session,
480                                        tBTA_JV_RFCOMM_CBACK* p_cback,
481                                        uint32_t rfcomm_slot_id) {
482   log::verbose("local_scn:{}, rfcomm_slot_id:{}", local_scn, rfcomm_slot_id);
483 
484   if (p_cback == NULL) return tBTA_JV_STATUS::FAILURE; /* Nothing to do */
485 
486   if (max_session == 0) max_session = 1;
487   if (max_session > BTA_JV_MAX_RFC_SR_SESSION) {
488     log::info("max_session is too big. use max {}", BTA_JV_MAX_RFC_SR_SESSION);
489     max_session = BTA_JV_MAX_RFC_SR_SESSION;
490   }
491 
492   do_in_main_thread(FROM_HERE,
493                     Bind(&bta_jv_rfcomm_start_server, sec_mask, local_scn,
494                          max_session, p_cback, rfcomm_slot_id));
495   return tBTA_JV_STATUS::SUCCESS;
496 }
497 
498 /*******************************************************************************
499  *
500  * Function         BTA_JvRfcommStopServer
501  *
502  * Description      This function stops the RFCOMM server. If the server has an
503  *                  active connection, it would be closed.
504  *
505  * Returns          tBTA_JV_STATUS::SUCCESS, if the request is being processed.
506  *                  tBTA_JV_STATUS::FAILURE, otherwise.
507  *
508  ******************************************************************************/
BTA_JvRfcommStopServer(uint32_t handle,uint32_t rfcomm_slot_id)509 tBTA_JV_STATUS BTA_JvRfcommStopServer(uint32_t handle,
510                                       uint32_t rfcomm_slot_id) {
511   log::verbose("handle:{}, rfcomm_slot_id:{}", handle, rfcomm_slot_id);
512 
513   do_in_main_thread(FROM_HERE,
514                     Bind(&bta_jv_rfcomm_stop_server, handle, rfcomm_slot_id));
515   return tBTA_JV_STATUS::SUCCESS;
516 }
517 
518 /*******************************************************************************
519  *
520  * Function         BTA_JvRfcommGetPortHdl
521  *
522  * Description      This function fetches the rfcomm port handle
523  *
524  * Returns
525  *
526  ******************************************************************************/
BTA_JvRfcommGetPortHdl(uint32_t handle)527 uint16_t BTA_JvRfcommGetPortHdl(uint32_t handle) {
528   uint32_t hi = ((handle & BTA_JV_RFC_HDL_MASK) & ~BTA_JV_RFCOMM_MASK) - 1;
529   uint32_t si = BTA_JV_RFC_HDL_TO_SIDX(handle);
530 
531   if (hi < BTA_JV_MAX_RFC_CONN && si < BTA_JV_MAX_RFC_SR_SESSION &&
532       bta_jv_cb.rfc_cb[hi].rfc_hdl[si])
533     return bta_jv_cb.port_cb[bta_jv_cb.rfc_cb[hi].rfc_hdl[si] - 1].port_handle;
534   else
535     return 0xffff;
536 }
537 
538 /*******************************************************************************
539  *
540  * Function         BTA_JvRfcommWrite
541  *
542  * Description      This function writes data to an RFCOMM connection
543  *
544  * Returns          tBTA_JV_STATUS::SUCCESS, if the request is being processed.
545  *                  tBTA_JV_STATUS::FAILURE, otherwise.
546  *
547  ******************************************************************************/
BTA_JvRfcommWrite(uint32_t handle,uint32_t req_id)548 tBTA_JV_STATUS BTA_JvRfcommWrite(uint32_t handle, uint32_t req_id) {
549   uint32_t hi = ((handle & BTA_JV_RFC_HDL_MASK) & ~BTA_JV_RFCOMM_MASK) - 1;
550   uint32_t si = BTA_JV_RFC_HDL_TO_SIDX(handle);
551 
552   log::verbose("handle:{}, req_id:{}, hi:{}, si:{}", handle, req_id, hi, si);
553   if (hi >= BTA_JV_MAX_RFC_CONN || !bta_jv_cb.rfc_cb[hi].p_cback ||
554       si >= BTA_JV_MAX_RFC_SR_SESSION || !bta_jv_cb.rfc_cb[hi].rfc_hdl[si]) {
555     return tBTA_JV_STATUS::FAILURE;
556   }
557 
558   log::verbose("write ok");
559 
560   tBTA_JV_RFC_CB* p_cb = &bta_jv_cb.rfc_cb[hi];
561   do_in_main_thread(FROM_HERE, Bind(&bta_jv_rfcomm_write, handle, req_id, p_cb,
562                                     &bta_jv_cb.port_cb[p_cb->rfc_hdl[si] - 1]));
563   return tBTA_JV_STATUS::SUCCESS;
564 }
565 
566 /*******************************************************************************
567  *
568  * Function    BTA_JVSetPmProfile
569  *
570  * Description: This function set or free power mode profile for different JV
571  *              application.
572  *
573  * Parameters:  handle,  JV handle from RFCOMM or L2CAP
574  *              app_id:  app specific pm ID, can be BTA_JV_PM_ALL, see
575  *                       bta_dm_cfg.c for details
576  *              BTA_JV_PM_ID_CLEAR: removes pm management on the handle. init_st
577  *              is ignored and BTA_JV_CONN_CLOSE is called implicitly
578  *              init_st:  state after calling this API. typically it should be
579  *                        BTA_JV_CONN_OPEN
580  *
581  * Returns      tBTA_JV_STATUS::SUCCESS, if the request is being processed.
582  *              tBTA_JV_STATUS::FAILURE, otherwise.
583  *
584  * NOTE:        BTA_JV_PM_ID_CLEAR: In general no need to be called as jv pm
585  *                                  calls automatically
586  *              BTA_JV_CONN_CLOSE to remove in case of connection close!
587  *
588  ******************************************************************************/
BTA_JvSetPmProfile(uint32_t handle,tBTA_JV_PM_ID app_id,tBTA_JV_CONN_STATE init_st)589 tBTA_JV_STATUS BTA_JvSetPmProfile(uint32_t handle, tBTA_JV_PM_ID app_id,
590                                   tBTA_JV_CONN_STATE init_st) {
591   log::verbose("handle:{}, app_id:{}, init_st:{}", handle, app_id, handle);
592 
593   do_in_main_thread(FROM_HERE,
594                     Bind(&bta_jv_set_pm_profile, handle, app_id, init_st));
595   return tBTA_JV_STATUS::SUCCESS;
596 }
597