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(BD_ADDR 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(BD_ADDR bd_addr, uint16_t num_uuid,
217                                    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  bdcpy(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, BD_ADDR peer_bd_addr,
305                                    tBTA_JV_L2CAP_CBACK* p_cback,
306                                    uint32_t l2cap_socket_id) {
307  APPL_TRACE_API("%s", __func__);
308
309  if (p_cback == NULL) return BTA_JV_FAILURE; /* Nothing to do */
310
311  tBTA_JV_API_L2CAP_CONNECT* p_msg =
312      (tBTA_JV_API_L2CAP_CONNECT*)osi_malloc(sizeof(tBTA_JV_API_L2CAP_CONNECT));
313  p_msg->hdr.event = BTA_JV_API_L2CAP_CONNECT_LE_EVT;
314  p_msg->sec_mask = sec_mask;
315  p_msg->role = role;
316  p_msg->remote_chan = remote_chan;
317  p_msg->rx_mtu = rx_mtu;
318  if (cfg != NULL) {
319    p_msg->has_cfg = true;
320    p_msg->cfg = *cfg;
321  } else {
322    p_msg->has_cfg = false;
323  }
324  if (ertm_info != NULL) {
325    p_msg->has_ertm_info = true;
326    p_msg->ertm_info = *ertm_info;
327  } else {
328    p_msg->has_ertm_info = false;
329  }
330  memcpy(p_msg->peer_bd_addr, peer_bd_addr, sizeof(BD_ADDR));
331  p_msg->p_cback = p_cback;
332  p_msg->l2cap_socket_id = l2cap_socket_id;
333
334  bta_sys_sendmsg(p_msg);
335
336  return BTA_JV_SUCCESS;
337}
338
339/*******************************************************************************
340 *
341 * Function         BTA_JvL2capConnect
342 *
343 * Description      Initiate a connection as a L2CAP client to the given BD
344 *                  Address.
345 *                  When the connection is initiated or failed to initiate,
346 *                  tBTA_JV_L2CAP_CBACK is called with BTA_JV_L2CAP_CL_INIT_EVT
347 *                  When the connection is established or failed,
348 *                  tBTA_JV_L2CAP_CBACK is called with BTA_JV_L2CAP_OPEN_EVT
349 *
350 * Returns          BTA_JV_SUCCESS, if the request is being processed.
351 *                  BTA_JV_FAILURE, otherwise.
352 *
353 ******************************************************************************/
354tBTA_JV_STATUS BTA_JvL2capConnect(int conn_type, tBTA_SEC sec_mask,
355                                  tBTA_JV_ROLE role,
356                                  const tL2CAP_ERTM_INFO* ertm_info,
357                                  uint16_t remote_psm, uint16_t rx_mtu,
358                                  tL2CAP_CFG_INFO* cfg, BD_ADDR peer_bd_addr,
359                                  tBTA_JV_L2CAP_CBACK* p_cback,
360                                  uint32_t l2cap_socket_id) {
361  APPL_TRACE_API("%s", __func__);
362
363  if (p_cback == NULL) return BTA_JV_FAILURE; /* Nothing to do */
364
365  tBTA_JV_API_L2CAP_CONNECT* p_msg =
366      (tBTA_JV_API_L2CAP_CONNECT*)osi_malloc(sizeof(tBTA_JV_API_L2CAP_CONNECT));
367  p_msg->hdr.event = BTA_JV_API_L2CAP_CONNECT_EVT;
368  p_msg->type = conn_type;
369  p_msg->sec_mask = sec_mask;
370  p_msg->role = role;
371  p_msg->remote_psm = remote_psm;
372  p_msg->rx_mtu = rx_mtu;
373  if (cfg != NULL) {
374    p_msg->has_cfg = true;
375    p_msg->cfg = *cfg;
376  } else {
377    p_msg->has_cfg = false;
378  }
379  if (ertm_info != NULL) {
380    p_msg->has_ertm_info = true;
381    p_msg->ertm_info = *ertm_info;
382  } else {
383    p_msg->has_ertm_info = false;
384  }
385  memcpy(p_msg->peer_bd_addr, peer_bd_addr, sizeof(BD_ADDR));
386  p_msg->p_cback = p_cback;
387  p_msg->l2cap_socket_id = l2cap_socket_id;
388
389  bta_sys_sendmsg(p_msg);
390
391  return BTA_JV_SUCCESS;
392}
393
394/*******************************************************************************
395 *
396 * Function         BTA_JvL2capClose
397 *
398 * Description      This function closes an L2CAP client connection
399 *
400 * Returns          BTA_JV_SUCCESS, if the request is being processed.
401 *                  BTA_JV_FAILURE, otherwise.
402 *
403 ******************************************************************************/
404tBTA_JV_STATUS BTA_JvL2capClose(uint32_t handle) {
405  tBTA_JV_STATUS status = BTA_JV_FAILURE;
406
407  APPL_TRACE_API("%s", __func__);
408
409  if (handle < BTA_JV_MAX_L2C_CONN && bta_jv_cb.l2c_cb[handle].p_cback) {
410    tBTA_JV_API_L2CAP_CLOSE* p_msg =
411        (tBTA_JV_API_L2CAP_CLOSE*)osi_malloc(sizeof(tBTA_JV_API_L2CAP_CLOSE));
412    p_msg->hdr.event = BTA_JV_API_L2CAP_CLOSE_EVT;
413    p_msg->handle = handle;
414    p_msg->p_cb = &bta_jv_cb.l2c_cb[handle];
415
416    bta_sys_sendmsg(p_msg);
417    status = BTA_JV_SUCCESS;
418  }
419
420  return status;
421}
422
423/*******************************************************************************
424 *
425 * Function         BTA_JvL2capCloseLE
426 *
427 * Description      This function closes an L2CAP client connection for Fixed
428 *                  Channels Function is idempotent and no callbacks are called!
429 *
430 * Returns          BTA_JV_SUCCESS, if the request is being processed.
431 *                  BTA_JV_FAILURE, otherwise.
432 *
433 ******************************************************************************/
434tBTA_JV_STATUS BTA_JvL2capCloseLE(uint32_t handle) {
435  tBTA_JV_API_L2CAP_CLOSE* p_msg =
436      (tBTA_JV_API_L2CAP_CLOSE*)osi_malloc(sizeof(tBTA_JV_API_L2CAP_CLOSE));
437
438  APPL_TRACE_API("%s", __func__);
439
440  p_msg->hdr.event = BTA_JV_API_L2CAP_CLOSE_FIXED_EVT;
441  p_msg->handle = handle;
442
443  bta_sys_sendmsg(p_msg);
444
445  return BTA_JV_SUCCESS;
446}
447
448/*******************************************************************************
449 *
450 * Function         BTA_JvL2capStartServer
451 *
452 * Description      This function starts an L2CAP server and listens for an
453 *                  L2CAP connection from a remote Bluetooth device.  When the
454 *                  server is started successfully, tBTA_JV_L2CAP_CBACK is
455 *                  called with BTA_JV_L2CAP_START_EVT.  When the connection is
456 *                  established tBTA_JV_L2CAP_CBACK is called with
457 *                  BTA_JV_L2CAP_OPEN_EVT.
458 *
459 * Returns          BTA_JV_SUCCESS, if the request is being processed.
460 *                  BTA_JV_FAILURE, otherwise.
461 *
462 ******************************************************************************/
463tBTA_JV_STATUS BTA_JvL2capStartServer(int conn_type, tBTA_SEC sec_mask,
464                                      tBTA_JV_ROLE role,
465                                      const tL2CAP_ERTM_INFO* ertm_info,
466                                      uint16_t local_psm, uint16_t rx_mtu,
467                                      tL2CAP_CFG_INFO* cfg,
468                                      tBTA_JV_L2CAP_CBACK* p_cback,
469                                      uint32_t l2cap_socket_id) {
470  APPL_TRACE_API("%s", __func__);
471
472  if (p_cback == NULL) return BTA_JV_FAILURE; /* Nothing to do */
473
474  tBTA_JV_API_L2CAP_SERVER* p_msg =
475      (tBTA_JV_API_L2CAP_SERVER*)osi_malloc(sizeof(tBTA_JV_API_L2CAP_SERVER));
476  p_msg->hdr.event = BTA_JV_API_L2CAP_START_SERVER_EVT;
477  p_msg->type = conn_type;
478  p_msg->sec_mask = sec_mask;
479  p_msg->role = role;
480  p_msg->local_psm = local_psm;
481  p_msg->rx_mtu = rx_mtu;
482  if (cfg != NULL) {
483    p_msg->has_cfg = true;
484    p_msg->cfg = *cfg;
485  } else {
486    p_msg->has_cfg = false;
487  }
488  if (ertm_info != NULL) {
489    p_msg->has_ertm_info = true;
490    p_msg->ertm_info = *ertm_info;
491  } else {
492    p_msg->has_ertm_info = false;
493  }
494  p_msg->p_cback = p_cback;
495  p_msg->l2cap_socket_id = l2cap_socket_id;
496
497  bta_sys_sendmsg(p_msg);
498
499  return BTA_JV_SUCCESS;
500}
501
502/*******************************************************************************
503 *
504 * Function         BTA_JvL2capStartServerLE
505 *
506 * Description      This function starts an LE L2CAP server and listens for an
507 *                  L2CAP connection from a remote Bluetooth device.  When the
508 *                  server is started successfully, tBTA_JV_L2CAP_CBACK is
509 *                  called with BTA_JV_L2CAP_START_EVT.  When the connection is
510 *                  established, tBTA_JV_L2CAP_CBACK is called with
511 *                  BTA_JV_L2CAP_OPEN_EVT.
512 *
513 * Returns          BTA_JV_SUCCESS, if the request is being processed.
514 *                  BTA_JV_FAILURE, otherwise.
515 *
516 ******************************************************************************/
517tBTA_JV_STATUS BTA_JvL2capStartServerLE(tBTA_SEC sec_mask, tBTA_JV_ROLE role,
518                                        const tL2CAP_ERTM_INFO* ertm_info,
519                                        uint16_t local_chan, uint16_t rx_mtu,
520                                        tL2CAP_CFG_INFO* cfg,
521                                        tBTA_JV_L2CAP_CBACK* p_cback,
522                                        uint32_t l2cap_socket_id) {
523  APPL_TRACE_API("%s", __func__);
524
525  if (p_cback == NULL) return BTA_JV_FAILURE; /* Nothing to do */
526
527  tBTA_JV_API_L2CAP_SERVER* p_msg =
528      (tBTA_JV_API_L2CAP_SERVER*)osi_malloc(sizeof(tBTA_JV_API_L2CAP_SERVER));
529  p_msg->hdr.event = BTA_JV_API_L2CAP_START_SERVER_LE_EVT;
530  p_msg->sec_mask = sec_mask;
531  p_msg->role = role;
532  p_msg->local_chan = local_chan;
533  p_msg->rx_mtu = rx_mtu;
534  if (cfg != NULL) {
535    p_msg->has_cfg = true;
536    p_msg->cfg = *cfg;
537  } else {
538    p_msg->has_cfg = false;
539  }
540  if (ertm_info != NULL) {
541    p_msg->has_ertm_info = true;
542    p_msg->ertm_info = *ertm_info;
543  } else {
544    p_msg->has_ertm_info = false;
545  }
546  p_msg->p_cback = p_cback;
547  p_msg->l2cap_socket_id = l2cap_socket_id;
548
549  bta_sys_sendmsg(p_msg);
550
551  return BTA_JV_SUCCESS;
552}
553
554/*******************************************************************************
555 *
556 * Function         BTA_JvL2capStopServer
557 *
558 * Description      This function stops the L2CAP server. If the server has an
559 *                  active connection, it would be closed.
560 *
561 * Returns          BTA_JV_SUCCESS, if the request is being processed.
562 *                  BTA_JV_FAILURE, otherwise.
563 *
564 ******************************************************************************/
565tBTA_JV_STATUS BTA_JvL2capStopServer(uint16_t local_psm,
566                                     uint32_t l2cap_socket_id) {
567  APPL_TRACE_API("%s", __func__);
568
569  tBTA_JV_API_L2CAP_SERVER* p_msg =
570      (tBTA_JV_API_L2CAP_SERVER*)osi_malloc(sizeof(tBTA_JV_API_L2CAP_SERVER));
571  p_msg->hdr.event = BTA_JV_API_L2CAP_STOP_SERVER_EVT;
572  p_msg->local_psm = local_psm;
573  p_msg->l2cap_socket_id = l2cap_socket_id;
574
575  bta_sys_sendmsg(p_msg);
576
577  return BTA_JV_SUCCESS;
578}
579
580/*******************************************************************************
581 *
582 * Function         BTA_JvL2capStopServerLE
583 *
584 * Description      This function stops the LE L2CAP server. If the server has
585 *                  an active connection, it would be closed.
586 *
587 * Returns          BTA_JV_SUCCESS, if the request is being processed.
588 *                  BTA_JV_FAILURE, otherwise.
589 *
590 ******************************************************************************/
591tBTA_JV_STATUS BTA_JvL2capStopServerLE(uint16_t local_chan,
592                                       uint32_t l2cap_socket_id) {
593  APPL_TRACE_API("%s", __func__);
594
595  tBTA_JV_API_L2CAP_SERVER* p_msg =
596      (tBTA_JV_API_L2CAP_SERVER*)osi_malloc(sizeof(tBTA_JV_API_L2CAP_SERVER));
597  p_msg->hdr.event = BTA_JV_API_L2CAP_STOP_SERVER_LE_EVT;
598  p_msg->local_chan = local_chan;
599  p_msg->l2cap_socket_id = l2cap_socket_id;
600
601  bta_sys_sendmsg(p_msg);
602
603  return BTA_JV_SUCCESS;
604}
605
606/*******************************************************************************
607 *
608 * Function         BTA_JvL2capRead
609 *
610 * Description      This function reads data from an L2CAP connecti;
611    tBTA_JV_RFC_CB  *p_cb = rc->p_cb;
612on
613 *                  When the operation is complete, tBTA_JV_L2CAP_CBACK is
614 *                  called with BTA_JV_L2CAP_READ_EVT.
615 *
616 * Returns          BTA_JV_SUCCESS, if the request is being processed.
617 *                  BTA_JV_FAILURE, otherwise.
618 *
619 ******************************************************************************/
620tBTA_JV_STATUS BTA_JvL2capRead(uint32_t handle, uint32_t req_id,
621                               uint8_t* p_data, uint16_t len) {
622  tBTA_JV_STATUS status = BTA_JV_FAILURE;
623  tBTA_JV_L2CAP_READ evt_data;
624
625  APPL_TRACE_API("%s", __func__);
626
627  if (handle < BTA_JV_MAX_L2C_CONN && bta_jv_cb.l2c_cb[handle].p_cback) {
628    status = BTA_JV_SUCCESS;
629    evt_data.status = BTA_JV_FAILURE;
630    evt_data.handle = handle;
631    evt_data.req_id = req_id;
632    evt_data.p_data = p_data;
633    evt_data.len = 0;
634
635    if (BT_PASS ==
636        GAP_ConnReadData((uint16_t)handle, p_data, len, &evt_data.len)) {
637      evt_data.status = BTA_JV_SUCCESS;
638    }
639    bta_jv_cb.l2c_cb[handle].p_cback(BTA_JV_L2CAP_READ_EVT, (tBTA_JV*)&evt_data,
640                                     bta_jv_cb.l2c_cb[handle].l2cap_socket_id);
641  }
642
643  return (status);
644}
645
646/*******************************************************************************
647 *
648 * Function         BTA_JvL2capReady
649 *
650 * Description      This function determined if there is data to read from
651 *                    an L2CAP connection
652 *
653 * Returns          BTA_JV_SUCCESS, if data queue size is in *p_data_size.
654 *                  BTA_JV_FAILURE, if error.
655 *
656 ******************************************************************************/
657tBTA_JV_STATUS BTA_JvL2capReady(uint32_t handle, uint32_t* p_data_size) {
658  tBTA_JV_STATUS status = BTA_JV_FAILURE;
659
660  APPL_TRACE_API("%s: %d", __func__, handle);
661  if (p_data_size && handle < BTA_JV_MAX_L2C_CONN &&
662      bta_jv_cb.l2c_cb[handle].p_cback) {
663    *p_data_size = 0;
664    if (BT_PASS == GAP_GetRxQueueCnt((uint16_t)handle, p_data_size)) {
665      status = BTA_JV_SUCCESS;
666    }
667  }
668
669  return (status);
670}
671
672/*******************************************************************************
673 *
674 * Function         BTA_JvL2capWrite
675 *
676 * Description      This function writes data to an L2CAP connection
677 *                  When the operation is complete, tBTA_JV_L2CAP_CBACK is
678 *                  called with BTA_JV_L2CAP_WRITE_EVT. Works for
679 *                  PSM-based connections
680 *
681 * Returns          BTA_JV_SUCCESS, if the request is being processed.
682 *                  BTA_JV_FAILURE, otherwise.
683 *
684 ******************************************************************************/
685tBTA_JV_STATUS BTA_JvL2capWrite(uint32_t handle, uint32_t req_id,
686                                uint8_t* p_data, uint16_t len,
687                                uint32_t user_id) {
688  tBTA_JV_STATUS status = BTA_JV_FAILURE;
689
690  APPL_TRACE_API("%s", __func__);
691
692  if (handle < BTA_JV_MAX_L2C_CONN && bta_jv_cb.l2c_cb[handle].p_cback) {
693    tBTA_JV_API_L2CAP_WRITE* p_msg =
694        (tBTA_JV_API_L2CAP_WRITE*)osi_malloc(sizeof(tBTA_JV_API_L2CAP_WRITE));
695    p_msg->hdr.event = BTA_JV_API_L2CAP_WRITE_EVT;
696    p_msg->handle = handle;
697    p_msg->req_id = req_id;
698    p_msg->p_data = p_data;
699    p_msg->p_cb = &bta_jv_cb.l2c_cb[handle];
700    p_msg->len = len;
701    p_msg->user_id = user_id;
702
703    bta_sys_sendmsg(p_msg);
704
705    status = BTA_JV_SUCCESS;
706  }
707
708  return status;
709}
710
711/*******************************************************************************
712 *
713 * Function         BTA_JvL2capWriteFixed
714 *
715 * Description      This function writes data to an L2CAP connection
716 *                  When the operation is complete, tBTA_JV_L2CAP_CBACK is
717 *                  called with BTA_JV_L2CAP_WRITE_EVT. Works for
718 *                  fixed-channel connections
719 *
720 * Returns          BTA_JV_SUCCESS, if the request is being processed.
721 *                  BTA_JV_FAILURE, otherwise.
722 *
723 ******************************************************************************/
724tBTA_JV_STATUS BTA_JvL2capWriteFixed(uint16_t channel, BD_ADDR* addr,
725                                     uint32_t req_id,
726                                     tBTA_JV_L2CAP_CBACK* p_cback,
727                                     uint8_t* p_data, uint16_t len,
728                                     uint32_t user_id) {
729  tBTA_JV_API_L2CAP_WRITE_FIXED* p_msg =
730      (tBTA_JV_API_L2CAP_WRITE_FIXED*)osi_malloc(
731          sizeof(tBTA_JV_API_L2CAP_WRITE_FIXED));
732
733  APPL_TRACE_API("%s", __func__);
734
735  p_msg->hdr.event = BTA_JV_API_L2CAP_WRITE_FIXED_EVT;
736  p_msg->channel = channel;
737  memcpy(p_msg->addr, addr, sizeof(p_msg->addr));
738  p_msg->req_id = req_id;
739  p_msg->p_data = p_data;
740  p_msg->p_cback = p_cback;
741  p_msg->len = len;
742  p_msg->user_id = user_id;
743
744  bta_sys_sendmsg(p_msg);
745
746  return BTA_JV_SUCCESS;
747}
748
749/*******************************************************************************
750 *
751 * Function         BTA_JvRfcommConnect
752 *
753 * Description      This function makes an RFCOMM conection to a remote BD
754 *                  Address.
755 *                  When the connection is initiated or failed to initiate,
756 *                  tBTA_JV_RFCOMM_CBACK is called with
757 *                  BTA_JV_RFCOMM_CL_INIT_EVT
758 *                  When the connection is established or failed,
759 *                  tBTA_JV_RFCOMM_CBACK is called with BTA_JV_RFCOMM_OPEN_EVT
760 *
761 * Returns          BTA_JV_SUCCESS, if the request is being processed.
762 *                  BTA_JV_FAILURE, otherwise.
763 *
764 ******************************************************************************/
765tBTA_JV_STATUS BTA_JvRfcommConnect(tBTA_SEC sec_mask, tBTA_JV_ROLE role,
766                                   uint8_t remote_scn, BD_ADDR 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  memcpy(p_msg->peer_bd_addr, peer_bd_addr, sizeof(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