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