1/******************************************************************************
2 *
3 *  Copyright (C) 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 <string.h>
26
27#include "bt_common.h"
28#include "bta_api.h"
29#include "bta_jv_api.h"
30#include "bta_jv_int.h"
31#include "bta_sys.h"
32#include "gap_api.h"
33#include "port_api.h"
34#include "sdp_api.h"
35#include "utl.h"
36
37/*****************************************************************************
38 *  Constants
39 ****************************************************************************/
40
41static const tBTA_SYS_REG bta_jv_reg = {bta_jv_sm_execute, NULL};
42
43/*******************************************************************************
44 *
45 * Function         BTA_JvEnable
46 *
47 * Description      Enable the Java I/F service. When the enable
48 *                  operation is complete the callback function will be
49 *                  called with a BTA_JV_ENABLE_EVT. This function must
50 *                  be called before other function in the JV API are
51 *                  called.
52 *
53 * Returns          BTA_JV_SUCCESS if successful.
54 *                  BTA_JV_FAIL if internal failure.
55 *
56 ******************************************************************************/
57tBTA_JV_STATUS BTA_JvEnable(tBTA_JV_DM_CBACK* p_cback) {
58  tBTA_JV_STATUS status = BTA_JV_FAILURE;
59  int i;
60
61  APPL_TRACE_API("BTA_JvEnable");
62  if (p_cback && false == bta_sys_is_register(BTA_ID_JV)) {
63    memset(&bta_jv_cb, 0, sizeof(tBTA_JV_CB));
64    /* set handle to invalid value by default */
65    for (i = 0; i < BTA_JV_PM_MAX_NUM; i++) {
66      bta_jv_cb.pm_cb[i].handle = BTA_JV_PM_HANDLE_CLEAR;
67    }
68
69    /* register with BTA system manager */
70    bta_sys_register(BTA_ID_JV, &bta_jv_reg);
71
72    if (p_cback) {
73      tBTA_JV_API_ENABLE* p_buf =
74          (tBTA_JV_API_ENABLE*)osi_malloc(sizeof(tBTA_JV_API_ENABLE));
75      p_buf->hdr.event = BTA_JV_API_ENABLE_EVT;
76      p_buf->p_cback = p_cback;
77      bta_sys_sendmsg(p_buf);
78      status = BTA_JV_SUCCESS;
79    }
80  } else {
81    APPL_TRACE_ERROR("JVenable fails");
82  }
83  return (status);
84}
85
86/*******************************************************************************
87 *
88 * Function         BTA_JvDisable
89 *
90 * Description      Disable the Java I/F
91 *
92 * Returns          void
93 *
94 ******************************************************************************/
95void BTA_JvDisable(void) {
96  BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
97
98  APPL_TRACE_API("%s", __func__);
99
100  bta_sys_deregister(BTA_ID_JV);
101  p_buf->event = BTA_JV_API_DISABLE_EVT;
102
103  bta_sys_sendmsg(p_buf);
104}
105
106/*******************************************************************************
107 *
108 * Function         BTA_JvIsEncrypted
109 *
110 * Description      This function checks if the link to peer device is encrypted
111 *
112 * Returns          true if encrypted.
113 *                  false if not.
114 *
115 ******************************************************************************/
116bool BTA_JvIsEncrypted(const RawAddress& bd_addr) {
117  bool is_encrypted = false;
118  uint8_t sec_flags, le_flags;
119
120  if (BTM_GetSecurityFlags(bd_addr, &sec_flags) &&
121      BTM_GetSecurityFlagsByTransport(bd_addr, &le_flags, BT_TRANSPORT_LE)) {
122    if (sec_flags & BTM_SEC_FLAG_ENCRYPTED || le_flags & BTM_SEC_FLAG_ENCRYPTED)
123      is_encrypted = true;
124  }
125  return is_encrypted;
126}
127/*******************************************************************************
128 *
129 * Function         BTA_JvGetChannelId
130 *
131 * Description      This function reserves a SCN (server channel number) for
132 *                  applications running over RFCOMM, L2CAP of L2CAP_LE.
133 *                  It is primarily called by server profiles/applications to
134 *                  register their SCN into the SDP database. The SCN is
135 *                  reported by the tBTA_JV_DM_CBACK callback with a
136 *                  BTA_JV_GET_SCN_EVT for RFCOMM channels and
137 *                  BTA_JV_GET_PSM_EVT for L2CAP and LE.
138 *                  If the SCN/PSM reported is 0, that means all resources are
139 *                  exhausted.
140 * Parameters
141 *   conn_type      one of BTA_JV_CONN_TYPE_
142 *   user_data      Any uservalue - will be returned in the resulting event.
143 *   channel        Only used for RFCOMM - to try to allocate a specific RFCOMM
144 *                  channel.
145 *
146 * Returns          BTA_JV_SUCCESS, if the request is being processed.
147 *                  BTA_JV_FAILURE, otherwise.
148 *
149 ******************************************************************************/
150tBTA_JV_STATUS BTA_JvGetChannelId(int conn_type, uint32_t id, int32_t channel) {
151  tBTA_JV_API_ALLOC_CHANNEL* p_msg =
152      (tBTA_JV_API_ALLOC_CHANNEL*)osi_malloc(sizeof(tBTA_JV_API_ALLOC_CHANNEL));
153
154  APPL_TRACE_API("%s", __func__);
155
156  p_msg->hdr.event = BTA_JV_API_GET_CHANNEL_EVT;
157  p_msg->type = conn_type;
158  p_msg->channel = channel;
159  if (conn_type == BTA_JV_CONN_TYPE_RFCOMM) {
160    p_msg->rfcomm_slot_id = id;
161  } else if (conn_type == BTA_JV_CONN_TYPE_L2CAP ||
162             conn_type == BTA_JV_CONN_TYPE_L2CAP_LE) {
163    p_msg->l2cap_socket_id = id;
164  } else {
165    APPL_TRACE_ERROR("%s: Invalid connection type");
166    return BTA_JV_FAILURE;
167  }
168
169  bta_sys_sendmsg(p_msg);
170
171  return BTA_JV_SUCCESS;
172}
173
174/*******************************************************************************
175 *
176 * Function         BTA_JvFreeChannel
177 *
178 * Description      This function frees a server channel number that was used
179 *                  by an application running over RFCOMM.
180 * Parameters
181 *   channel        The channel to free
182 *   conn_type      one of BTA_JV_CONN_TYPE_
183 *
184 * Returns          BTA_JV_SUCCESS, if the request is being processed.
185 *                  BTA_JV_FAILURE, otherwise.
186 *
187 ******************************************************************************/
188tBTA_JV_STATUS BTA_JvFreeChannel(uint16_t channel, int conn_type) {
189  tBTA_JV_API_FREE_CHANNEL* p_msg =
190      (tBTA_JV_API_FREE_CHANNEL*)osi_malloc(sizeof(tBTA_JV_API_FREE_CHANNEL));
191
192  APPL_TRACE_API("%s", __func__);
193
194  p_msg->hdr.event = BTA_JV_API_FREE_SCN_EVT;
195  p_msg->scn = channel;
196  p_msg->type = conn_type;
197
198  bta_sys_sendmsg(p_msg);
199
200  return BTA_JV_SUCCESS;
201}
202
203/*******************************************************************************
204 *
205 * Function         BTA_JvStartDiscovery
206 *
207 * Description      This function performs service discovery for the services
208 *                  provided by the given peer device. When the operation is
209 *                  complete the tBTA_JV_DM_CBACK callback function will be
210 *                  called with a BTA_JV_DISCOVERY_COMP_EVT.
211 *
212 * Returns          BTA_JV_SUCCESS, if the request is being processed.
213 *                  BTA_JV_FAILURE, otherwise.
214 *
215 ******************************************************************************/
216tBTA_JV_STATUS BTA_JvStartDiscovery(const RawAddress& bd_addr,
217                                    uint16_t num_uuid, tSDP_UUID* p_uuid_list,
218                                    uint32_t rfcomm_slot_id) {
219  tBTA_JV_API_START_DISCOVERY* p_msg = (tBTA_JV_API_START_DISCOVERY*)osi_malloc(
220      sizeof(tBTA_JV_API_START_DISCOVERY));
221
222  APPL_TRACE_API("%s", __func__);
223
224  p_msg->hdr.event = BTA_JV_API_START_DISCOVERY_EVT;
225  p_msg->bd_addr = bd_addr;
226  p_msg->num_uuid = num_uuid;
227  memcpy(p_msg->uuid_list, p_uuid_list, num_uuid * sizeof(tSDP_UUID));
228  p_msg->num_attr = 0;
229  p_msg->rfcomm_slot_id = rfcomm_slot_id;
230
231  bta_sys_sendmsg(p_msg);
232
233  return BTA_JV_SUCCESS;
234}
235
236/*******************************************************************************
237 *
238 * Function         BTA_JvCreateRecord
239 *
240 * Description      Create a service record in the local SDP database.
241 *                  When the operation is complete the tBTA_JV_DM_CBACK callback
242 *                  function will be called with a BTA_JV_CREATE_RECORD_EVT.
243 *
244 * Returns          BTA_JV_SUCCESS, if the request is being processed.
245 *                  BTA_JV_FAILURE, otherwise.
246 *
247 ******************************************************************************/
248tBTA_JV_STATUS BTA_JvCreateRecordByUser(uint32_t rfcomm_slot_id) {
249  tBTA_JV_API_CREATE_RECORD* p_msg =
250      (tBTA_JV_API_CREATE_RECORD*)osi_malloc(sizeof(tBTA_JV_API_CREATE_RECORD));
251
252  APPL_TRACE_API("%s", __func__);
253
254  p_msg->hdr.event = BTA_JV_API_CREATE_RECORD_EVT;
255  p_msg->rfcomm_slot_id = rfcomm_slot_id;
256
257  bta_sys_sendmsg(p_msg);
258
259  return BTA_JV_SUCCESS;
260}
261
262/*******************************************************************************
263 *
264 * Function         BTA_JvDeleteRecord
265 *
266 * Description      Delete a service record in the local SDP database.
267 *
268 * Returns          BTA_JV_SUCCESS, if the request is being processed.
269 *                  BTA_JV_FAILURE, otherwise.
270 *
271 ******************************************************************************/
272tBTA_JV_STATUS BTA_JvDeleteRecord(uint32_t handle) {
273  tBTA_JV_API_ADD_ATTRIBUTE* p_msg =
274      (tBTA_JV_API_ADD_ATTRIBUTE*)osi_malloc(sizeof(tBTA_JV_API_ADD_ATTRIBUTE));
275
276  APPL_TRACE_API("%s", __func__);
277
278  p_msg->hdr.event = BTA_JV_API_DELETE_RECORD_EVT;
279  p_msg->handle = handle;
280
281  bta_sys_sendmsg(p_msg);
282
283  return BTA_JV_SUCCESS;
284}
285
286/*******************************************************************************
287 *
288 * Function         BTA_JvL2capConnectLE
289 *
290 * Description      Initiate an LE connection as a L2CAP client to the given BD
291 *                  Address.
292 *                  When the connection is initiated or failed to initiate,
293 *                  tBTA_JV_L2CAP_CBACK is called with BTA_JV_L2CAP_CL_INIT_EVT
294 *                  When the connection is established or failed,
295 *                  tBTA_JV_L2CAP_CBACK is called with BTA_JV_L2CAP_OPEN_EVT
296 *
297 * Returns          BTA_JV_SUCCESS, if the request is being processed.
298 *                  BTA_JV_FAILURE, otherwise.
299 *
300 ******************************************************************************/
301tBTA_JV_STATUS BTA_JvL2capConnectLE(tBTA_SEC sec_mask, tBTA_JV_ROLE role,
302                                    const tL2CAP_ERTM_INFO* ertm_info,
303                                    uint16_t remote_chan, uint16_t rx_mtu,
304                                    tL2CAP_CFG_INFO* cfg,
305                                    const RawAddress& peer_bd_addr,
306                                    tBTA_JV_L2CAP_CBACK* p_cback,
307                                    uint32_t l2cap_socket_id) {
308  APPL_TRACE_API("%s", __func__);
309
310  if (p_cback == NULL) return BTA_JV_FAILURE; /* Nothing to do */
311
312  tBTA_JV_API_L2CAP_CONNECT* p_msg =
313      (tBTA_JV_API_L2CAP_CONNECT*)osi_malloc(sizeof(tBTA_JV_API_L2CAP_CONNECT));
314  p_msg->hdr.event = BTA_JV_API_L2CAP_CONNECT_LE_EVT;
315  p_msg->sec_mask = sec_mask;
316  p_msg->role = role;
317  p_msg->remote_chan = remote_chan;
318  p_msg->rx_mtu = rx_mtu;
319  if (cfg != NULL) {
320    p_msg->has_cfg = true;
321    p_msg->cfg = *cfg;
322  } else {
323    p_msg->has_cfg = false;
324  }
325  if (ertm_info != NULL) {
326    p_msg->has_ertm_info = true;
327    p_msg->ertm_info = *ertm_info;
328  } else {
329    p_msg->has_ertm_info = false;
330  }
331  p_msg->peer_bd_addr = peer_bd_addr;
332  p_msg->p_cback = p_cback;
333  p_msg->l2cap_socket_id = l2cap_socket_id;
334
335  bta_sys_sendmsg(p_msg);
336
337  return BTA_JV_SUCCESS;
338}
339
340/*******************************************************************************
341 *
342 * Function         BTA_JvL2capConnect
343 *
344 * Description      Initiate a connection as a L2CAP client to the given BD
345 *                  Address.
346 *                  When the connection is initiated or failed to initiate,
347 *                  tBTA_JV_L2CAP_CBACK is called with BTA_JV_L2CAP_CL_INIT_EVT
348 *                  When the connection is established or failed,
349 *                  tBTA_JV_L2CAP_CBACK is called with BTA_JV_L2CAP_OPEN_EVT
350 *
351 * Returns          BTA_JV_SUCCESS, if the request is being processed.
352 *                  BTA_JV_FAILURE, otherwise.
353 *
354 ******************************************************************************/
355tBTA_JV_STATUS BTA_JvL2capConnect(
356    int conn_type, tBTA_SEC sec_mask, tBTA_JV_ROLE role,
357    const tL2CAP_ERTM_INFO* ertm_info, uint16_t remote_psm, uint16_t rx_mtu,
358    tL2CAP_CFG_INFO* cfg, const RawAddress& peer_bd_addr,
359    tBTA_JV_L2CAP_CBACK* p_cback, uint32_t l2cap_socket_id) {
360  APPL_TRACE_API("%s", __func__);
361
362  if (p_cback == NULL) return BTA_JV_FAILURE; /* Nothing to do */
363
364  tBTA_JV_API_L2CAP_CONNECT* p_msg =
365      (tBTA_JV_API_L2CAP_CONNECT*)osi_malloc(sizeof(tBTA_JV_API_L2CAP_CONNECT));
366  p_msg->hdr.event = BTA_JV_API_L2CAP_CONNECT_EVT;
367  p_msg->type = conn_type;
368  p_msg->sec_mask = sec_mask;
369  p_msg->role = role;
370  p_msg->remote_psm = remote_psm;
371  p_msg->rx_mtu = rx_mtu;
372  if (cfg != NULL) {
373    p_msg->has_cfg = true;
374    p_msg->cfg = *cfg;
375  } else {
376    p_msg->has_cfg = false;
377  }
378  if (ertm_info != NULL) {
379    p_msg->has_ertm_info = true;
380    p_msg->ertm_info = *ertm_info;
381  } else {
382    p_msg->has_ertm_info = false;
383  }
384  p_msg->peer_bd_addr = peer_bd_addr;
385  p_msg->p_cback = p_cback;
386  p_msg->l2cap_socket_id = l2cap_socket_id;
387
388  bta_sys_sendmsg(p_msg);
389
390  return BTA_JV_SUCCESS;
391}
392
393/*******************************************************************************
394 *
395 * Function         BTA_JvL2capClose
396 *
397 * Description      This function closes an L2CAP client connection
398 *
399 * Returns          BTA_JV_SUCCESS, if the request is being processed.
400 *                  BTA_JV_FAILURE, otherwise.
401 *
402 ******************************************************************************/
403tBTA_JV_STATUS BTA_JvL2capClose(uint32_t handle) {
404  tBTA_JV_STATUS status = BTA_JV_FAILURE;
405
406  APPL_TRACE_API("%s", __func__);
407
408  if (handle < BTA_JV_MAX_L2C_CONN && bta_jv_cb.l2c_cb[handle].p_cback) {
409    tBTA_JV_API_L2CAP_CLOSE* p_msg =
410        (tBTA_JV_API_L2CAP_CLOSE*)osi_malloc(sizeof(tBTA_JV_API_L2CAP_CLOSE));
411    p_msg->hdr.event = BTA_JV_API_L2CAP_CLOSE_EVT;
412    p_msg->handle = handle;
413    p_msg->p_cb = &bta_jv_cb.l2c_cb[handle];
414
415    bta_sys_sendmsg(p_msg);
416    status = BTA_JV_SUCCESS;
417  }
418
419  return status;
420}
421
422/*******************************************************************************
423 *
424 * Function         BTA_JvL2capCloseLE
425 *
426 * Description      This function closes an L2CAP client connection for Fixed
427 *                  Channels Function is idempotent and no callbacks are called!
428 *
429 * Returns          BTA_JV_SUCCESS, if the request is being processed.
430 *                  BTA_JV_FAILURE, otherwise.
431 *
432 ******************************************************************************/
433tBTA_JV_STATUS BTA_JvL2capCloseLE(uint32_t handle) {
434  tBTA_JV_API_L2CAP_CLOSE* p_msg =
435      (tBTA_JV_API_L2CAP_CLOSE*)osi_malloc(sizeof(tBTA_JV_API_L2CAP_CLOSE));
436
437  APPL_TRACE_API("%s", __func__);
438
439  p_msg->hdr.event = BTA_JV_API_L2CAP_CLOSE_FIXED_EVT;
440  p_msg->handle = handle;
441
442  bta_sys_sendmsg(p_msg);
443
444  return BTA_JV_SUCCESS;
445}
446
447/*******************************************************************************
448 *
449 * Function         BTA_JvL2capStartServer
450 *
451 * Description      This function starts an L2CAP server and listens for an
452 *                  L2CAP connection from a remote Bluetooth device.  When the
453 *                  server is started successfully, tBTA_JV_L2CAP_CBACK is
454 *                  called with BTA_JV_L2CAP_START_EVT.  When the connection is
455 *                  established tBTA_JV_L2CAP_CBACK is called with
456 *                  BTA_JV_L2CAP_OPEN_EVT.
457 *
458 * Returns          BTA_JV_SUCCESS, if the request is being processed.
459 *                  BTA_JV_FAILURE, otherwise.
460 *
461 ******************************************************************************/
462tBTA_JV_STATUS BTA_JvL2capStartServer(int conn_type, tBTA_SEC sec_mask,
463                                      tBTA_JV_ROLE role,
464                                      const tL2CAP_ERTM_INFO* ertm_info,
465                                      uint16_t local_psm, uint16_t rx_mtu,
466                                      tL2CAP_CFG_INFO* cfg,
467                                      tBTA_JV_L2CAP_CBACK* p_cback,
468                                      uint32_t l2cap_socket_id) {
469  APPL_TRACE_API("%s", __func__);
470
471  if (p_cback == NULL) return BTA_JV_FAILURE; /* Nothing to do */
472
473  tBTA_JV_API_L2CAP_SERVER* p_msg =
474      (tBTA_JV_API_L2CAP_SERVER*)osi_malloc(sizeof(tBTA_JV_API_L2CAP_SERVER));
475  p_msg->hdr.event = BTA_JV_API_L2CAP_START_SERVER_EVT;
476  p_msg->type = conn_type;
477  p_msg->sec_mask = sec_mask;
478  p_msg->role = role;
479  p_msg->local_psm = local_psm;
480  p_msg->rx_mtu = rx_mtu;
481  if (cfg != NULL) {
482    p_msg->has_cfg = true;
483    p_msg->cfg = *cfg;
484  } else {
485    p_msg->has_cfg = false;
486  }
487  if (ertm_info != NULL) {
488    p_msg->has_ertm_info = true;
489    p_msg->ertm_info = *ertm_info;
490  } else {
491    p_msg->has_ertm_info = false;
492  }
493  p_msg->p_cback = p_cback;
494  p_msg->l2cap_socket_id = l2cap_socket_id;
495
496  bta_sys_sendmsg(p_msg);
497
498  return BTA_JV_SUCCESS;
499}
500
501/*******************************************************************************
502 *
503 * Function         BTA_JvL2capStartServerLE
504 *
505 * Description      This function starts an LE L2CAP server and listens for an
506 *                  L2CAP connection from a remote Bluetooth device.  When the
507 *                  server is started successfully, tBTA_JV_L2CAP_CBACK is
508 *                  called with BTA_JV_L2CAP_START_EVT.  When the connection is
509 *                  established, tBTA_JV_L2CAP_CBACK is called with
510 *                  BTA_JV_L2CAP_OPEN_EVT.
511 *
512 * Returns          BTA_JV_SUCCESS, if the request is being processed.
513 *                  BTA_JV_FAILURE, otherwise.
514 *
515 ******************************************************************************/
516tBTA_JV_STATUS BTA_JvL2capStartServerLE(tBTA_SEC sec_mask, tBTA_JV_ROLE role,
517                                        const tL2CAP_ERTM_INFO* ertm_info,
518                                        uint16_t local_chan, uint16_t rx_mtu,
519                                        tL2CAP_CFG_INFO* cfg,
520                                        tBTA_JV_L2CAP_CBACK* p_cback,
521                                        uint32_t l2cap_socket_id) {
522  APPL_TRACE_API("%s", __func__);
523
524  if (p_cback == NULL) return BTA_JV_FAILURE; /* Nothing to do */
525
526  tBTA_JV_API_L2CAP_SERVER* p_msg =
527      (tBTA_JV_API_L2CAP_SERVER*)osi_malloc(sizeof(tBTA_JV_API_L2CAP_SERVER));
528  p_msg->hdr.event = BTA_JV_API_L2CAP_START_SERVER_LE_EVT;
529  p_msg->sec_mask = sec_mask;
530  p_msg->role = role;
531  p_msg->local_chan = local_chan;
532  p_msg->rx_mtu = rx_mtu;
533  if (cfg != NULL) {
534    p_msg->has_cfg = true;
535    p_msg->cfg = *cfg;
536  } else {
537    p_msg->has_cfg = false;
538  }
539  if (ertm_info != NULL) {
540    p_msg->has_ertm_info = true;
541    p_msg->ertm_info = *ertm_info;
542  } else {
543    p_msg->has_ertm_info = false;
544  }
545  p_msg->p_cback = p_cback;
546  p_msg->l2cap_socket_id = l2cap_socket_id;
547
548  bta_sys_sendmsg(p_msg);
549
550  return BTA_JV_SUCCESS;
551}
552
553/*******************************************************************************
554 *
555 * Function         BTA_JvL2capStopServer
556 *
557 * Description      This function stops the L2CAP server. If the server has an
558 *                  active connection, it would be closed.
559 *
560 * Returns          BTA_JV_SUCCESS, if the request is being processed.
561 *                  BTA_JV_FAILURE, otherwise.
562 *
563 ******************************************************************************/
564tBTA_JV_STATUS BTA_JvL2capStopServer(uint16_t local_psm,
565                                     uint32_t l2cap_socket_id) {
566  APPL_TRACE_API("%s", __func__);
567
568  tBTA_JV_API_L2CAP_SERVER* p_msg =
569      (tBTA_JV_API_L2CAP_SERVER*)osi_malloc(sizeof(tBTA_JV_API_L2CAP_SERVER));
570  p_msg->hdr.event = BTA_JV_API_L2CAP_STOP_SERVER_EVT;
571  p_msg->local_psm = local_psm;
572  p_msg->l2cap_socket_id = l2cap_socket_id;
573
574  bta_sys_sendmsg(p_msg);
575
576  return BTA_JV_SUCCESS;
577}
578
579/*******************************************************************************
580 *
581 * Function         BTA_JvL2capStopServerLE
582 *
583 * Description      This function stops the LE L2CAP server. If the server has
584 *                  an active connection, it would be closed.
585 *
586 * Returns          BTA_JV_SUCCESS, if the request is being processed.
587 *                  BTA_JV_FAILURE, otherwise.
588 *
589 ******************************************************************************/
590tBTA_JV_STATUS BTA_JvL2capStopServerLE(uint16_t local_chan,
591                                       uint32_t l2cap_socket_id) {
592  APPL_TRACE_API("%s", __func__);
593
594  tBTA_JV_API_L2CAP_SERVER* p_msg =
595      (tBTA_JV_API_L2CAP_SERVER*)osi_malloc(sizeof(tBTA_JV_API_L2CAP_SERVER));
596  p_msg->hdr.event = BTA_JV_API_L2CAP_STOP_SERVER_LE_EVT;
597  p_msg->local_chan = local_chan;
598  p_msg->l2cap_socket_id = l2cap_socket_id;
599
600  bta_sys_sendmsg(p_msg);
601
602  return BTA_JV_SUCCESS;
603}
604
605/*******************************************************************************
606 *
607 * Function         BTA_JvL2capRead
608 *
609 * Description      This function reads data from an L2CAP connecti;
610    tBTA_JV_RFC_CB  *p_cb = rc->p_cb;
611on
612 *                  When the operation is complete, tBTA_JV_L2CAP_CBACK is
613 *                  called with BTA_JV_L2CAP_READ_EVT.
614 *
615 * Returns          BTA_JV_SUCCESS, if the request is being processed.
616 *                  BTA_JV_FAILURE, otherwise.
617 *
618 ******************************************************************************/
619tBTA_JV_STATUS BTA_JvL2capRead(uint32_t handle, uint32_t req_id,
620                               uint8_t* p_data, uint16_t len) {
621  tBTA_JV_STATUS status = BTA_JV_FAILURE;
622  tBTA_JV_L2CAP_READ evt_data;
623
624  APPL_TRACE_API("%s", __func__);
625
626  if (handle < BTA_JV_MAX_L2C_CONN && bta_jv_cb.l2c_cb[handle].p_cback) {
627    status = BTA_JV_SUCCESS;
628    evt_data.status = BTA_JV_FAILURE;
629    evt_data.handle = handle;
630    evt_data.req_id = req_id;
631    evt_data.p_data = p_data;
632    evt_data.len = 0;
633
634    if (BT_PASS ==
635        GAP_ConnReadData((uint16_t)handle, p_data, len, &evt_data.len)) {
636      evt_data.status = BTA_JV_SUCCESS;
637    }
638    bta_jv_cb.l2c_cb[handle].p_cback(BTA_JV_L2CAP_READ_EVT, (tBTA_JV*)&evt_data,
639                                     bta_jv_cb.l2c_cb[handle].l2cap_socket_id);
640  }
641
642  return (status);
643}
644
645/*******************************************************************************
646 *
647 * Function         BTA_JvL2capReady
648 *
649 * Description      This function determined if there is data to read from
650 *                    an L2CAP connection
651 *
652 * Returns          BTA_JV_SUCCESS, if data queue size is in *p_data_size.
653 *                  BTA_JV_FAILURE, if error.
654 *
655 ******************************************************************************/
656tBTA_JV_STATUS BTA_JvL2capReady(uint32_t handle, uint32_t* p_data_size) {
657  tBTA_JV_STATUS status = BTA_JV_FAILURE;
658
659  APPL_TRACE_API("%s: %d", __func__, handle);
660  if (p_data_size && handle < BTA_JV_MAX_L2C_CONN &&
661      bta_jv_cb.l2c_cb[handle].p_cback) {
662    *p_data_size = 0;
663    if (BT_PASS == GAP_GetRxQueueCnt((uint16_t)handle, p_data_size)) {
664      status = BTA_JV_SUCCESS;
665    }
666  }
667
668  return (status);
669}
670
671/*******************************************************************************
672 *
673 * Function         BTA_JvL2capWrite
674 *
675 * Description      This function writes data to an L2CAP connection
676 *                  When the operation is complete, tBTA_JV_L2CAP_CBACK is
677 *                  called with BTA_JV_L2CAP_WRITE_EVT. Works for
678 *                  PSM-based connections
679 *
680 * Returns          BTA_JV_SUCCESS, if the request is being processed.
681 *                  BTA_JV_FAILURE, otherwise.
682 *
683 ******************************************************************************/
684tBTA_JV_STATUS BTA_JvL2capWrite(uint32_t handle, uint32_t req_id,
685                                uint8_t* p_data, uint16_t len,
686                                uint32_t user_id) {
687  tBTA_JV_STATUS status = BTA_JV_FAILURE;
688
689  APPL_TRACE_API("%s", __func__);
690
691  if (handle < BTA_JV_MAX_L2C_CONN && bta_jv_cb.l2c_cb[handle].p_cback) {
692    tBTA_JV_API_L2CAP_WRITE* p_msg =
693        (tBTA_JV_API_L2CAP_WRITE*)osi_malloc(sizeof(tBTA_JV_API_L2CAP_WRITE));
694    p_msg->hdr.event = BTA_JV_API_L2CAP_WRITE_EVT;
695    p_msg->handle = handle;
696    p_msg->req_id = req_id;
697    p_msg->p_data = p_data;
698    p_msg->p_cb = &bta_jv_cb.l2c_cb[handle];
699    p_msg->len = len;
700    p_msg->user_id = user_id;
701
702    bta_sys_sendmsg(p_msg);
703
704    status = BTA_JV_SUCCESS;
705  }
706
707  return status;
708}
709
710/*******************************************************************************
711 *
712 * Function         BTA_JvL2capWriteFixed
713 *
714 * Description      This function writes data to an L2CAP connection
715 *                  When the operation is complete, tBTA_JV_L2CAP_CBACK is
716 *                  called with BTA_JV_L2CAP_WRITE_EVT. Works for
717 *                  fixed-channel connections
718 *
719 * Returns          BTA_JV_SUCCESS, if the request is being processed.
720 *                  BTA_JV_FAILURE, otherwise.
721 *
722 ******************************************************************************/
723tBTA_JV_STATUS BTA_JvL2capWriteFixed(uint16_t channel, const RawAddress& addr,
724                                     uint32_t req_id,
725                                     tBTA_JV_L2CAP_CBACK* p_cback,
726                                     uint8_t* p_data, uint16_t len,
727                                     uint32_t user_id) {
728  tBTA_JV_API_L2CAP_WRITE_FIXED* p_msg =
729      (tBTA_JV_API_L2CAP_WRITE_FIXED*)osi_malloc(
730          sizeof(tBTA_JV_API_L2CAP_WRITE_FIXED));
731
732  APPL_TRACE_API("%s", __func__);
733
734  p_msg->hdr.event = BTA_JV_API_L2CAP_WRITE_FIXED_EVT;
735  p_msg->channel = channel;
736  p_msg->addr = addr;
737  p_msg->req_id = req_id;
738  p_msg->p_data = p_data;
739  p_msg->p_cback = p_cback;
740  p_msg->len = len;
741  p_msg->user_id = user_id;
742
743  bta_sys_sendmsg(p_msg);
744
745  return BTA_JV_SUCCESS;
746}
747
748/*******************************************************************************
749 *
750 * Function         BTA_JvRfcommConnect
751 *
752 * Description      This function makes an RFCOMM conection to a remote BD
753 *                  Address.
754 *                  When the connection is initiated or failed to initiate,
755 *                  tBTA_JV_RFCOMM_CBACK is called with
756 *                  BTA_JV_RFCOMM_CL_INIT_EVT
757 *                  When the connection is established or failed,
758 *                  tBTA_JV_RFCOMM_CBACK is called with BTA_JV_RFCOMM_OPEN_EVT
759 *
760 * Returns          BTA_JV_SUCCESS, if the request is being processed.
761 *                  BTA_JV_FAILURE, otherwise.
762 *
763 ******************************************************************************/
764tBTA_JV_STATUS BTA_JvRfcommConnect(tBTA_SEC sec_mask, tBTA_JV_ROLE role,
765                                   uint8_t remote_scn,
766                                   const RawAddress& peer_bd_addr,
767                                   tBTA_JV_RFCOMM_CBACK* p_cback,
768                                   uint32_t rfcomm_slot_id) {
769  APPL_TRACE_API("%s", __func__);
770
771  if (p_cback == NULL) return BTA_JV_FAILURE; /* Nothing to do */
772
773  tBTA_JV_API_RFCOMM_CONNECT* p_msg = (tBTA_JV_API_RFCOMM_CONNECT*)osi_malloc(
774      sizeof(tBTA_JV_API_RFCOMM_CONNECT));
775  p_msg->hdr.event = BTA_JV_API_RFCOMM_CONNECT_EVT;
776  p_msg->sec_mask = sec_mask;
777  p_msg->role = role;
778  p_msg->remote_scn = remote_scn;
779  p_msg->peer_bd_addr = peer_bd_addr;
780  p_msg->p_cback = p_cback;
781  p_msg->rfcomm_slot_id = rfcomm_slot_id;
782
783  bta_sys_sendmsg(p_msg);
784
785  return BTA_JV_SUCCESS;
786}
787
788/*******************************************************************************
789 *
790 * Function         BTA_JvRfcommClose
791 *
792 * Description      This function closes an RFCOMM connection
793 *
794 * Returns          BTA_JV_SUCCESS, if the request is being processed.
795 *                  BTA_JV_FAILURE, otherwise.
796 *
797 ******************************************************************************/
798tBTA_JV_STATUS BTA_JvRfcommClose(uint32_t handle, uint32_t rfcomm_slot_id) {
799  tBTA_JV_STATUS status = BTA_JV_FAILURE;
800  uint32_t hi = ((handle & BTA_JV_RFC_HDL_MASK) & ~BTA_JV_RFCOMM_MASK) - 1;
801  uint32_t si = BTA_JV_RFC_HDL_TO_SIDX(handle);
802
803  APPL_TRACE_API("%s", __func__);
804
805  if (hi < BTA_JV_MAX_RFC_CONN && bta_jv_cb.rfc_cb[hi].p_cback &&
806      si < BTA_JV_MAX_RFC_SR_SESSION && bta_jv_cb.rfc_cb[hi].rfc_hdl[si]) {
807    tBTA_JV_API_RFCOMM_CLOSE* p_msg =
808        (tBTA_JV_API_RFCOMM_CLOSE*)osi_malloc(sizeof(tBTA_JV_API_RFCOMM_CLOSE));
809    p_msg->hdr.event = BTA_JV_API_RFCOMM_CLOSE_EVT;
810    p_msg->handle = handle;
811    p_msg->p_cb = &bta_jv_cb.rfc_cb[hi];
812    p_msg->p_pcb = &bta_jv_cb.port_cb[p_msg->p_cb->rfc_hdl[si] - 1];
813    p_msg->rfcomm_slot_id = rfcomm_slot_id;
814
815    bta_sys_sendmsg(p_msg);
816
817    status = BTA_JV_SUCCESS;
818  }
819
820  return status;
821}
822
823/*******************************************************************************
824 *
825 * Function         BTA_JvRfcommStartServer
826 *
827 * Description      This function starts listening for an RFCOMM connection
828 *                  request from a remote Bluetooth device.  When the server is
829 *                  started successfully, tBTA_JV_RFCOMM_CBACK is called
830 *                  with BTA_JV_RFCOMM_START_EVT.
831 *                  When the connection is established, tBTA_JV_RFCOMM_CBACK
832 *                  is called with BTA_JV_RFCOMM_OPEN_EVT.
833 *
834 * Returns          BTA_JV_SUCCESS, if the request is being processed.
835 *                  BTA_JV_FAILURE, otherwise.
836 *
837 ******************************************************************************/
838tBTA_JV_STATUS BTA_JvRfcommStartServer(tBTA_SEC sec_mask, tBTA_JV_ROLE role,
839                                       uint8_t local_scn, uint8_t max_session,
840                                       tBTA_JV_RFCOMM_CBACK* p_cback,
841                                       uint32_t rfcomm_slot_id) {
842  APPL_TRACE_API("%s", __func__);
843
844  if (p_cback == NULL) return BTA_JV_FAILURE; /* Nothing to do */
845
846  tBTA_JV_API_RFCOMM_SERVER* p_msg =
847      (tBTA_JV_API_RFCOMM_SERVER*)osi_malloc(sizeof(tBTA_JV_API_RFCOMM_SERVER));
848  if (max_session == 0) max_session = 1;
849  if (max_session > BTA_JV_MAX_RFC_SR_SESSION) {
850    APPL_TRACE_DEBUG("max_session is too big. use max (%d)", max_session,
851                     BTA_JV_MAX_RFC_SR_SESSION);
852    max_session = BTA_JV_MAX_RFC_SR_SESSION;
853  }
854  p_msg->hdr.event = BTA_JV_API_RFCOMM_START_SERVER_EVT;
855  p_msg->sec_mask = sec_mask;
856  p_msg->role = role;
857  p_msg->local_scn = local_scn;
858  p_msg->max_session = max_session;
859  p_msg->p_cback = p_cback;
860  p_msg->rfcomm_slot_id = rfcomm_slot_id;  // caller's private data
861
862  bta_sys_sendmsg(p_msg);
863
864  return BTA_JV_SUCCESS;
865}
866
867/*******************************************************************************
868 *
869 * Function         BTA_JvRfcommStopServer
870 *
871 * Description      This function stops the RFCOMM server. If the server has an
872 *                  active connection, it would be closed.
873 *
874 * Returns          BTA_JV_SUCCESS, if the request is being processed.
875 *                  BTA_JV_FAILURE, otherwise.
876 *
877 ******************************************************************************/
878tBTA_JV_STATUS BTA_JvRfcommStopServer(uint32_t handle,
879                                      uint32_t rfcomm_slot_id) {
880  tBTA_JV_API_RFCOMM_SERVER* p_msg =
881      (tBTA_JV_API_RFCOMM_SERVER*)osi_malloc(sizeof(tBTA_JV_API_RFCOMM_SERVER));
882
883  APPL_TRACE_API("%s", __func__);
884
885  p_msg->hdr.event = BTA_JV_API_RFCOMM_STOP_SERVER_EVT;
886  p_msg->handle = handle;
887  p_msg->rfcomm_slot_id = rfcomm_slot_id;  // caller's private data
888
889  bta_sys_sendmsg(p_msg);
890
891  return BTA_JV_SUCCESS;
892}
893
894/*******************************************************************************
895 *
896 * Function         BTA_JvRfcommGetPortHdl
897 *
898 * Description    This function fetches the rfcomm port handle
899 *
900 * Returns          BTA_JV_SUCCESS, if the request is being processed.
901 *                  BTA_JV_FAILURE, otherwise.
902 *
903 ******************************************************************************/
904uint16_t BTA_JvRfcommGetPortHdl(uint32_t handle) {
905  uint32_t hi = ((handle & BTA_JV_RFC_HDL_MASK) & ~BTA_JV_RFCOMM_MASK) - 1;
906  uint32_t si = BTA_JV_RFC_HDL_TO_SIDX(handle);
907
908  if (hi < BTA_JV_MAX_RFC_CONN && si < BTA_JV_MAX_RFC_SR_SESSION &&
909      bta_jv_cb.rfc_cb[hi].rfc_hdl[si])
910    return bta_jv_cb.port_cb[bta_jv_cb.rfc_cb[hi].rfc_hdl[si] - 1].port_handle;
911  else
912    return 0xffff;
913}
914
915/*******************************************************************************
916 *
917 * Function         BTA_JvRfcommWrite
918 *
919 * Description      This function writes data to an RFCOMM connection
920 *
921 * Returns          BTA_JV_SUCCESS, if the request is being processed.
922 *                  BTA_JV_FAILURE, otherwise.
923 *
924 ******************************************************************************/
925tBTA_JV_STATUS BTA_JvRfcommWrite(uint32_t handle, uint32_t req_id) {
926  tBTA_JV_STATUS status = BTA_JV_FAILURE;
927  uint32_t hi = ((handle & BTA_JV_RFC_HDL_MASK) & ~BTA_JV_RFCOMM_MASK) - 1;
928  uint32_t si = BTA_JV_RFC_HDL_TO_SIDX(handle);
929
930  APPL_TRACE_API("%s", __func__);
931
932  APPL_TRACE_DEBUG("handle:0x%x, hi:%d, si:%d", handle, hi, si);
933  if (hi < BTA_JV_MAX_RFC_CONN && bta_jv_cb.rfc_cb[hi].p_cback &&
934      si < BTA_JV_MAX_RFC_SR_SESSION && bta_jv_cb.rfc_cb[hi].rfc_hdl[si]) {
935    tBTA_JV_API_RFCOMM_WRITE* p_msg =
936        (tBTA_JV_API_RFCOMM_WRITE*)osi_malloc(sizeof(tBTA_JV_API_RFCOMM_WRITE));
937    p_msg->hdr.event = BTA_JV_API_RFCOMM_WRITE_EVT;
938    p_msg->handle = handle;
939    p_msg->req_id = req_id;
940    p_msg->p_cb = &bta_jv_cb.rfc_cb[hi];
941    p_msg->p_pcb = &bta_jv_cb.port_cb[p_msg->p_cb->rfc_hdl[si] - 1];
942    APPL_TRACE_API("write ok");
943
944    bta_sys_sendmsg(p_msg);
945    status = BTA_JV_SUCCESS;
946  }
947
948  return status;
949}
950
951/*******************************************************************************
952 *
953 * Function    BTA_JVSetPmProfile
954 *
955 * Description: This function set or free power mode profile for different JV
956 *              application.
957 *
958 * Parameters:  handle,  JV handle from RFCOMM or L2CAP
959 *              app_id:  app specific pm ID, can be BTA_JV_PM_ALL, see
960 *                       bta_dm_cfg.c for details
961 *              BTA_JV_PM_ID_CLEAR: removes pm management on the handle. init_st
962 *              is ignored and BTA_JV_CONN_CLOSE is called implicitly
963 *              init_st:  state after calling this API. typically it should be
964 *                        BTA_JV_CONN_OPEN
965 *
966 * Returns      BTA_JV_SUCCESS, if the request is being processed.
967 *              BTA_JV_FAILURE, otherwise.
968 *
969 * NOTE:        BTA_JV_PM_ID_CLEAR: In general no need to be called as jv pm
970 *                                  calls automatically
971 *              BTA_JV_CONN_CLOSE to remove in case of connection close!
972 *
973 ******************************************************************************/
974tBTA_JV_STATUS BTA_JvSetPmProfile(uint32_t handle, tBTA_JV_PM_ID app_id,
975                                  tBTA_JV_CONN_STATE init_st) {
976  tBTA_JV_API_SET_PM_PROFILE* p_msg = (tBTA_JV_API_SET_PM_PROFILE*)osi_malloc(
977      sizeof(tBTA_JV_API_SET_PM_PROFILE));
978
979  APPL_TRACE_API("%s handle:0x%x, app_id:%d", __func__, handle, app_id);
980
981  p_msg->hdr.event = BTA_JV_API_SET_PM_PROFILE_EVT;
982  p_msg->handle = handle;
983  p_msg->app_id = app_id;
984  p_msg->init_st = init_st;
985
986  bta_sys_sendmsg(p_msg);
987
988  return BTA_JV_SUCCESS;
989}
990