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