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