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 "bd.h"
27#include "bta_sys.h"
28#include "bta_jv_api.h"
29#include "bta_jv_int.h"
30#include "gki.h"
31#include <string.h>
32#include "port_api.h"
33#include "sdp_api.h"
34#include "utl.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    tBTA_JV_API_ENABLE  *p_buf;
64    int i;
65
66    APPL_TRACE_API( "BTA_JvEnable");
67    if(p_cback && FALSE == bta_sys_is_register(BTA_ID_JV))
68    {
69        memset(&bta_jv_cb, 0, sizeof(tBTA_JV_CB));
70        /* set handle to invalid value by default */
71        for (i=0; i<BTA_JV_PM_MAX_NUM; i++)
72        {
73            bta_jv_cb.pm_cb[i].handle = BTA_JV_PM_HANDLE_CLEAR;
74        }
75
76        /* register with BTA system manager */
77        bta_sys_register(BTA_ID_JV, &bta_jv_reg);
78
79        if (p_cback && (p_buf = (tBTA_JV_API_ENABLE *) GKI_getbuf(sizeof(tBTA_JV_API_ENABLE))) != NULL)
80        {
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;
106
107    APPL_TRACE_API( "BTA_JvDisable");
108    bta_sys_deregister(BTA_ID_JV);
109    if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
110    {
111        p_buf->event = BTA_JV_API_DISABLE_EVT;
112        bta_sys_sendmsg(p_buf);
113    }
114}
115
116/*******************************************************************************
117**
118** Function         BTA_JvIsEnable
119**
120** Description      Get the JV registration status.
121**
122** Returns          TRUE, if registered
123**
124*******************************************************************************/
125BOOLEAN BTA_JvIsEnable(void)
126{
127    return bta_sys_is_register(BTA_ID_JV);
128}
129
130/*******************************************************************************
131**
132** Function         BTA_JvSetDiscoverability
133**
134** Description      This function sets the Bluetooth  discoverable modes
135**                  of the local device.  This controls whether other
136**                  Bluetooth devices can find the local device.
137**
138**                  When the operation is complete the tBTA_JV_DM_CBACK callback
139**                  function will be called with a BTA_JV_SET_DISCOVER_EVT.
140**
141** Returns          BTA_JV_SUCCESS if successful.
142**                  BTA_JV_FAIL if internal failure.
143**
144*******************************************************************************/
145tBTA_JV_STATUS BTA_JvSetDiscoverability(tBTA_JV_DISC disc_mode)
146{
147    tBTA_JV_STATUS status = BTA_JV_FAILURE;
148    tBTA_JV_API_SET_DISCOVERABILITY *p_msg;
149
150    APPL_TRACE_API( "BTA_JvSetDiscoverability");
151    if ((p_msg = (tBTA_JV_API_SET_DISCOVERABILITY *)GKI_getbuf(sizeof(tBTA_JV_MSG))) != NULL)
152    {
153        p_msg->hdr.event = BTA_JV_API_SET_DISCOVERABILITY_EVT;
154        p_msg->disc_mode = disc_mode;
155        bta_sys_sendmsg(p_msg);
156        status = BTA_JV_SUCCESS;
157    }
158
159    return(status);
160}
161
162/*******************************************************************************
163**
164** Function         BTA_JvGetDiscoverability
165**
166** Description      This function gets the Bluetooth
167**                  discoverable modes of local device
168**
169** Returns          The current Bluetooth discoverable mode.
170**
171*******************************************************************************/
172tBTA_JV_DISC BTA_JvGetDiscoverability(void)
173{
174    APPL_TRACE_API( "BTA_JvGetDiscoverability");
175    return BTM_ReadDiscoverability(0, 0);
176}
177
178/*******************************************************************************
179**
180** Function         BTA_JvGetLocalDeviceAddr
181**
182** Description      This function obtains the local Bluetooth device address.
183**                  The local Bluetooth device address is reported by the
184**                  tBTA_JV_DM_CBACK callback with a BTA_JV_LOCAL_ADDR_EVT.
185**
186** Returns          BTA_JV_SUCCESS if successful.
187**                  BTA_JV_FAIL if internal failure.
188**
189*******************************************************************************/
190tBTA_JV_STATUS BTA_JvGetLocalDeviceAddr(void)
191{
192    tBTA_JV_STATUS ret = BTA_JV_FAILURE;
193    BT_HDR *p_msg;
194
195    APPL_TRACE_API( "BTA_JvGetLocalDeviceAddr");
196    if ((p_msg = (BT_HDR *)GKI_getbuf(sizeof(BT_HDR))) != NULL)
197    {
198        p_msg->event = BTA_JV_API_GET_LOCAL_DEVICE_ADDR_EVT;
199        bta_sys_sendmsg(p_msg);
200        ret = BTA_JV_SUCCESS;
201    }
202
203    return(ret);
204}
205
206/*******************************************************************************
207**
208** Function         BTA_JvGetLocalDeviceName
209**
210** Description      This function obtains the name of the local device
211**                  The local Bluetooth device name is reported by the
212**                  tBTA_JV_DM_CBACK callback with a BTA_JV_LOCAL_NAME_EVT.
213**
214** Returns          BTA_JV_SUCCESS if successful.
215**                  BTA_JV_FAIL if internal failure.
216**
217*******************************************************************************/
218tBTA_JV_STATUS BTA_JvGetLocalDeviceName(void)
219{
220    tBTA_JV_STATUS ret = BTA_JV_FAILURE;
221    BT_HDR *p_msg;
222
223    APPL_TRACE_API( "BTA_JvGetLocalDeviceName");
224    if ((p_msg = (BT_HDR *)GKI_getbuf(sizeof(BT_HDR))) != NULL)
225    {
226        p_msg->event = BTA_JV_API_GET_LOCAL_DEVICE_NAME_EVT;
227        bta_sys_sendmsg(p_msg);
228        ret = BTA_JV_SUCCESS;
229    }
230
231    return(ret);
232}
233
234/*******************************************************************************
235**
236** Function         BTA_JvGetRemoteDeviceName
237**
238** Description      This function obtains the name of the specified device.
239**                  The Bluetooth device name is reported by the
240**                  tBTA_JV_DM_CBACK callback with a BTA_JV_REMOTE_NAME_EVT.
241**
242** Returns          BTA_JV_SUCCESS if successful.
243**                  BTA_JV_FAIL if internal failure.
244**
245*******************************************************************************/
246tBTA_JV_STATUS BTA_JvGetRemoteDeviceName(BD_ADDR bd_addr)
247{
248    tBTA_JV_STATUS ret = BTA_JV_FAILURE;
249    tBTA_JV_API_GET_REMOTE_NAME *p_msg;
250
251    APPL_TRACE_API( "BTA_JvGetRemoteDeviceName");
252    if ((p_msg = (tBTA_JV_API_GET_REMOTE_NAME *)GKI_getbuf(sizeof(tBTA_JV_API_GET_REMOTE_NAME))) != NULL)
253    {
254        p_msg->hdr.event = BTA_JV_API_GET_REMOTE_DEVICE_NAME_EVT;
255        bdcpy(p_msg->bd_addr, bd_addr);
256        bta_sys_sendmsg(p_msg);
257        ret = BTA_JV_SUCCESS;
258    }
259
260    return(ret);
261}
262
263/*******************************************************************************
264**
265** Function         BTA_JvGetPreknownDevice
266**
267** Description      This function obtains the Bluetooth address in the inquiry
268**                  database collected via the previous call to BTA_DmSearch().
269**
270** Returns          The number of preknown devices if p_bd_addr is NULL
271**                  BTA_JV_SUCCESS if successful.
272**                  BTA_JV_INTERNAL_ERR(-1) if internal failure.
273**
274*******************************************************************************/
275INT32 BTA_JvGetPreknownDevice(UINT8 * p_bd_addr, UINT32 index)
276{
277    tBTA_JV_STATUS status = BTA_JV_FAILURE;
278    tBTM_INQ_INFO *p_info;
279    UINT32  count = 0;
280    INT32   ret = BTA_JV_INTERNAL_ERR;
281
282    APPL_TRACE_API( "BTA_JvGetPreknownDevice");
283    p_info = BTM_InqFirstResult();
284    if(p_info)
285    {
286        status = BTA_JV_SUCCESS;
287        /* the database is valid */
288        if(NULL == p_bd_addr)
289        {
290            /* p_bd_addr is NULL: count the number of preknown devices */
291            /* set the index to an invalid size (too big) */
292            index = BTM_INQ_DB_SIZE;
293        }
294        else if(index >= BTM_INQ_DB_SIZE)
295        {
296            /* invalid index - error */
297            status = (tBTA_JV_STATUS)BTA_JV_INTERNAL_ERR;
298        }
299
300        if(BTA_JV_SUCCESS == status)
301        {
302            while(p_info && index > count)
303            {
304                count++;
305                p_info = BTM_InqNextResult(p_info);
306            }
307
308            if(p_bd_addr)
309            {
310                if(index == count && p_info)
311                {
312                    count = BTA_JV_SUCCESS;
313                    bdcpy(p_bd_addr, p_info->results.remote_bd_addr);
314                }
315                else
316                    status = (tBTA_JV_STATUS)BTA_JV_INTERNAL_ERR;
317            }
318            /*
319            else report the count
320            */
321        }
322        /*
323        else error had happened.
324        */
325    }
326
327    if(BTA_JV_SUCCESS == status)
328    {
329        ret = count;
330    }
331    return ret;
332}
333
334
335/*******************************************************************************
336**
337** Function         BTA_JvGetDeviceClass
338**
339** Description      This function obtains the local Class of Device. This
340**                  function executes in place. The result is returned right away.
341**
342** Returns          DEV_CLASS, A three-byte array of UINT8 that contains the
343**                  Class of Device information. The definitions are in the
344**                  "Bluetooth Assigned Numbers".
345**
346*******************************************************************************/
347UINT8 * BTA_JvGetDeviceClass(void)
348{
349    APPL_TRACE_API( "BTA_JvGetDeviceClass");
350    return BTM_ReadDeviceClass();
351}
352
353/*******************************************************************************
354**
355** Function         BTA_JvSetServiceClass
356**
357** Description      This function sets the service class of local Class of Device
358**
359** Returns          BTA_JV_SUCCESS if successful.
360**                  BTA_JV_FAIL if internal failure.
361**
362*******************************************************************************/
363tBTA_JV_STATUS BTA_JvSetServiceClass(UINT32 service)
364{
365    tBTA_JV_STATUS status = BTA_JV_FAILURE;
366    tBTA_JV_API_SET_SERVICE_CLASS *p_msg;
367
368    APPL_TRACE_API( "BTA_JvSetServiceClass");
369    if ((p_msg = (tBTA_JV_API_SET_SERVICE_CLASS *)GKI_getbuf(sizeof(tBTA_JV_API_SET_SERVICE_CLASS))) != NULL)
370    {
371        p_msg->hdr.event = BTA_JV_API_SET_SERVICE_CLASS_EVT;
372        p_msg->service = service;
373        bta_sys_sendmsg(p_msg);
374        status = BTA_JV_SUCCESS;
375    }
376
377    return(status);
378}
379
380/*******************************************************************************
381**
382** Function         BTA_JvSetEncryption
383**
384** Description      This function ensures that the connection to the given device
385**                  is encrypted.
386**                  When the operation is complete the tBTA_JV_DM_CBACK callback
387**                  function will be called with a BTA_JV_SET_ENCRYPTION_EVT.
388**
389** Returns          BTA_JV_SUCCESS, if the request is being processed.
390**                  BTA_JV_FAILURE, otherwise.
391**
392*******************************************************************************/
393tBTA_JV_STATUS BTA_JvSetEncryption(BD_ADDR bd_addr)
394{
395    tBTA_JV_STATUS ret = BTA_JV_FAILURE;
396    tBTA_JV_API_SET_ENCRYPTION *p_msg;
397
398    APPL_TRACE_API( "BTA_JvSetEncryption");
399    if ((p_msg = (tBTA_JV_API_SET_ENCRYPTION *)GKI_getbuf(sizeof(tBTA_JV_API_SET_ENCRYPTION))) != NULL)
400    {
401        p_msg->hdr.event = BTA_JV_API_SET_ENCRYPTION_EVT;
402        bdcpy(p_msg->bd_addr, bd_addr);
403        bta_sys_sendmsg(p_msg);
404        ret = BTA_JV_SUCCESS;
405    }
406
407    return(ret);
408}
409
410/*******************************************************************************
411**
412** Function         BTA_JvIsAuthenticated
413**
414** Description      This function checks if the peer device is authenticated
415**
416** Returns          TRUE if authenticated.
417**                  FALSE if not.
418**
419*******************************************************************************/
420BOOLEAN BTA_JvIsAuthenticated(BD_ADDR bd_addr)
421{
422    BOOLEAN is_authenticated = FALSE;
423    UINT8 sec_flags;
424
425    if(BTM_GetSecurityFlags(bd_addr, &sec_flags))
426    {
427        if(sec_flags&BTM_SEC_FLAG_AUTHENTICATED)
428            is_authenticated = TRUE;
429    }
430    return is_authenticated;
431}
432
433/*******************************************************************************
434**
435** Function         BTA_JvIsTrusted
436**
437** Description      This function checks if the peer device is trusted
438**                  (previously paired)
439**
440** Returns          TRUE if trusted.
441**                  FALSE if not.
442**
443*******************************************************************************/
444BOOLEAN BTA_JvIsTrusted(BD_ADDR bd_addr)
445{
446    BOOLEAN is_trusted = FALSE;
447    UINT8 sec_flags;
448
449    if(BTM_GetSecurityFlags(bd_addr, &sec_flags))
450    {
451        if ((sec_flags&BTM_SEC_FLAG_AUTHENTICATED) ||
452            (sec_flags&BTM_SEC_FLAG_LKEY_KNOWN))
453        {
454            is_trusted = TRUE;
455        }
456    }
457    return is_trusted;
458}
459
460/*******************************************************************************
461**
462** Function         BTA_JvIsAuthorized
463**
464** Description      This function checks if the peer device is authorized
465**
466** Returns          TRUE if authorized.
467**                  FALSE if not.
468**
469*******************************************************************************/
470BOOLEAN BTA_JvIsAuthorized(BD_ADDR bd_addr)
471{
472    BOOLEAN is_authorized = FALSE;
473    UINT8 sec_flags;
474
475    if(BTM_GetSecurityFlags(bd_addr, &sec_flags))
476    {
477        if(sec_flags&BTM_SEC_FLAG_AUTHORIZED)
478            is_authorized = TRUE;
479    }
480    return is_authorized;
481}
482
483/*******************************************************************************
484**
485** Function         BTA_JvIsEncrypted
486**
487** Description      This function checks if the link to peer device is encrypted
488**
489** Returns          TRUE if encrypted.
490**                  FALSE if not.
491**
492*******************************************************************************/
493BOOLEAN BTA_JvIsEncrypted(BD_ADDR bd_addr)
494{
495    BOOLEAN is_encrypted = FALSE;
496    UINT8 sec_flags, le_flags;
497
498    if (BTM_GetSecurityFlags(bd_addr, &sec_flags) &&
499        BTM_GetSecurityFlagsByTransport(bd_addr, &le_flags, BT_TRANSPORT_LE))
500    {
501        if(sec_flags & BTM_SEC_FLAG_ENCRYPTED ||
502           le_flags & BTM_SEC_FLAG_ENCRYPTED)
503            is_encrypted = TRUE;
504    }
505    return is_encrypted;
506}
507
508/*******************************************************************************
509**
510** Function         BTA_JvGetSecurityMode
511**
512** Description      This function returns the current Bluetooth security mode
513**                  of the local device
514**
515** Returns          The current Bluetooth security mode.
516**
517*******************************************************************************/
518tBTA_JV_SEC_MODE BTA_JvGetSecurityMode(void)
519{
520   return BTM_GetSecurityMode();
521}
522
523/*******************************************************************************
524**
525** Function         BTA_JvGetSCN
526**
527** Description      This function reserves a SCN (server channel number) for
528**                  applications running over RFCOMM. It is primarily called by
529**                  server profiles/applications to register their SCN into the
530**                  SDP database. The SCN is reported by the tBTA_JV_DM_CBACK
531**                  callback with a BTA_JV_GET_SCN_EVT.
532**                  If the SCN reported is 0, that means all SCN resources are
533**                  exhausted.
534**
535** Returns          BTA_JV_SUCCESS, if the request is being processed.
536**                  BTA_JV_FAILURE, otherwise.
537**
538*******************************************************************************/
539tBTA_JV_STATUS BTA_JvGetSCN(void)
540{
541    tBTA_JV_STATUS status = BTA_JV_FAILURE;
542    BT_HDR *p_msg;
543
544    APPL_TRACE_API( "BTA_JvGetSCN");
545    if ((p_msg = (BT_HDR *)GKI_getbuf(sizeof(BT_HDR))) != NULL)
546    {
547        p_msg->event = BTA_JV_API_GET_SCN_EVT;
548        bta_sys_sendmsg(p_msg);
549        status = BTA_JV_SUCCESS;
550    }
551
552    return(status);
553}
554
555/*******************************************************************************
556**
557** Function         BTA_JvFreeSCN
558**
559** Description      This function frees a server channel number that was used
560**                  by an application running over RFCOMM.
561**
562** Returns          BTA_JV_SUCCESS, if the request is being processed.
563**                  BTA_JV_FAILURE, otherwise.
564**
565*******************************************************************************/
566tBTA_JV_STATUS BTA_JvFreeSCN(UINT8 scn)
567{
568    tBTA_JV_STATUS status = BTA_JV_FAILURE;
569    tBTA_JV_API_FREE_SCN *p_msg;
570
571    APPL_TRACE_API( "BTA_JvFreeSCN");
572    if ((p_msg = (tBTA_JV_API_FREE_SCN *)GKI_getbuf(sizeof(tBTA_JV_API_FREE_SCN))) != NULL)
573    {
574        p_msg->hdr.event = BTA_JV_API_FREE_SCN_EVT;
575        p_msg->scn       = scn;
576        bta_sys_sendmsg(p_msg);
577        status = BTA_JV_SUCCESS;
578    }
579
580    return(status);
581}
582
583/*******************************************************************************
584**
585** Function         BTA_JvGetPSM
586**
587** Description      This function reserves a PSM (Protocol Service Multiplexer)
588**                  applications running over L2CAP. It is primarily called by
589**                  server profiles/applications to register their PSM into the
590**                  SDP database.
591**
592** Returns          The next free PSM
593**
594*******************************************************************************/
595UINT16 BTA_JvGetPSM(void)
596{
597#if 0
598    APPL_TRACE_API( "BTA_JvGetPSM");
599
600    return (L2CA_AllocatePSM());
601#endif
602    return 0;
603}
604
605
606/*******************************************************************************
607**
608** Function         BTA_JvStartDiscovery
609**
610** Description      This function performs service discovery for the services
611**                  provided by the given peer device. When the operation is
612**                  complete the tBTA_JV_DM_CBACK callback function will be
613**                  called with a BTA_JV_DISCOVERY_COMP_EVT.
614**
615** Returns          BTA_JV_SUCCESS, if the request is being processed.
616**                  BTA_JV_FAILURE, otherwise.
617**
618*******************************************************************************/
619tBTA_JV_STATUS BTA_JvStartDiscovery(BD_ADDR bd_addr, UINT16 num_uuid,
620            tSDP_UUID *p_uuid_list, void * user_data)
621{
622    tBTA_JV_STATUS status = BTA_JV_FAILURE;
623    tBTA_JV_API_START_DISCOVERY *p_msg;
624
625    APPL_TRACE_API( "BTA_JvStartDiscovery");
626    if ((p_msg = (tBTA_JV_API_START_DISCOVERY *)GKI_getbuf(sizeof(tBTA_JV_API_START_DISCOVERY))) != NULL)
627    {
628        p_msg->hdr.event = BTA_JV_API_START_DISCOVERY_EVT;
629        bdcpy(p_msg->bd_addr, bd_addr);
630        p_msg->num_uuid = num_uuid;
631        memcpy(p_msg->uuid_list, p_uuid_list, num_uuid * sizeof(tSDP_UUID));
632        p_msg->num_attr = 0;
633        p_msg->user_data = user_data;
634        bta_sys_sendmsg(p_msg);
635        status = BTA_JV_SUCCESS;
636    }
637
638    return(status);
639}
640
641/*******************************************************************************
642**
643** Function         BTA_JvCancelDiscovery
644**
645** Description      This function cancels an active service discovery.
646**                  When the operation is
647**                  complete the tBTA_JV_DM_CBACK callback function will be
648**                  called with a BTA_JV_CANCEL_DISCVRY_EVT.
649**
650** Returns          BTA_JV_SUCCESS, if the request is being processed.
651**                  BTA_JV_FAILURE, otherwise.
652**
653*******************************************************************************/
654tBTA_JV_STATUS BTA_JvCancelDiscovery(void * user_data)
655{
656    tBTA_JV_STATUS status = BTA_JV_FAILURE;
657    tBTA_JV_API_CANCEL_DISCOVERY *p_msg;
658
659    APPL_TRACE_API( "BTA_JvCancelDiscovery");
660    if ((p_msg = (tBTA_JV_API_CANCEL_DISCOVERY *)GKI_getbuf(sizeof(tBTA_JV_API_CANCEL_DISCOVERY))) != NULL)
661    {
662        p_msg->hdr.event = BTA_JV_API_CANCEL_DISCOVERY_EVT;
663        p_msg->user_data = user_data;
664        bta_sys_sendmsg(p_msg);
665        status = BTA_JV_SUCCESS;
666    }
667
668    return(status);
669}
670
671/*******************************************************************************
672**
673** Function         BTA_JvGetServicesLength
674**
675** Description      This function obtains the number of services and the length
676**                  of each service found in the SDP database (result of last
677**                  BTA_JvStartDiscovery().When the operation is complete the
678**                  tBTA_JV_DM_CBACK callback function will be called with a
679**                  BTA_JV_SERVICES_LEN_EVT.
680**
681** Returns          BTA_JV_SUCCESS, if the request is being processed.
682**                  BTA_JV_FAILURE, otherwise.
683**
684*******************************************************************************/
685tBTA_JV_STATUS BTA_JvGetServicesLength(BOOLEAN inc_hdr, UINT16 *p_services_len)
686{
687    tBTA_JV_STATUS status = BTA_JV_FAILURE;
688    tBTA_JV_API_GET_SERVICES_LENGTH *p_msg;
689
690    APPL_TRACE_API( "BTA_JvGetServicesLength");
691    if ((p_msg = (tBTA_JV_API_GET_SERVICES_LENGTH *)GKI_getbuf(sizeof(tBTA_JV_API_GET_SERVICES_LENGTH))) != NULL)
692    {
693        p_msg->hdr.event = BTA_JV_API_GET_SERVICES_LENGTH_EVT;
694        p_msg->p_services_len = p_services_len;
695        p_msg->inc_hdr = inc_hdr;
696        bta_sys_sendmsg(p_msg);
697        status = BTA_JV_SUCCESS;
698    }
699
700    return(status);
701}
702/*******************************************************************************
703**
704** Function         BTA_JvGetServicesResult
705**
706** Description      This function returns a number of service records found
707**                  during current service search, equals to the number returned
708**                  by previous call to BTA_JvGetServicesLength.
709**                  The contents of each SDP record will be returned under a
710**                  TLV (type, len, value) representation in the data buffer
711**                  provided by the caller.
712**
713** Returns          -1, if error. Otherwise, the number of services
714**
715*******************************************************************************/
716INT32 BTA_JvGetServicesResult(BOOLEAN inc_hdr, UINT8 **TLVs)
717{
718    UNUSED(inc_hdr);
719    UNUSED(TLVs);
720#if 0
721    INT32 num_services = -1;
722    UINT8   *p, *np, *op, type;
723    UINT32  raw_used, raw_cur;
724    UINT32  len;
725    UINT32  hdr_len;
726
727    APPL_TRACE_API( "BTA_JvGetServicesResult");
728    if(p_bta_jv_cfg->p_sdp_db->p_first_rec)
729    {
730        /* the database is valid */
731        num_services = 0;
732        p = p_bta_jv_cfg->p_sdp_db->raw_data;
733        raw_used = p_bta_jv_cfg->p_sdp_db->raw_used;
734        while(raw_used && p)
735        {
736            op = p;
737            type = *p++;
738            np = sdpu_get_len_from_type(p, type, &len);
739            p = np + len;
740            raw_cur = p - op;
741            if(raw_used >= raw_cur)
742            {
743                raw_used -= raw_cur;
744            }
745            else
746            {
747                /* error. can not continue */
748                break;
749            }
750            if(inc_hdr)
751            {
752                hdr_len = np - op;
753                memcpy(TLVs[num_services++], op, len+hdr_len);
754            }
755            else
756            {
757                memcpy(TLVs[num_services++], np, len);
758            }
759        } /* end of while */
760    }
761    return(num_services);
762#endif
763    return 0;
764}
765/*******************************************************************************
766**
767** Function         BTA_JvServiceSelect
768**
769** Description      This function checks if the SDP database contains the given
770**                  service UUID. When the operation is complete the
771**                  tBTA_JV_DM_CBACK callback function will be called with a
772**                  BTA_JV_SERVICE_SEL_EVT with the length of the service record.
773**                  If the service is not found or error, -1 is reported.
774**
775** Returns          BTA_JV_SUCCESS, if the request is being processed.
776**                  BTA_JV_FAILURE, otherwise.
777**
778*******************************************************************************/
779tBTA_JV_STATUS BTA_JvServiceSelect(UINT16 uuid)
780{
781    tBTA_JV_STATUS status = BTA_JV_FAILURE;
782    tBTA_JV_API_SERVICE_SELECT *p_msg;
783
784    APPL_TRACE_API( "BTA_JvServiceSelect");
785    if ((p_msg = (tBTA_JV_API_SERVICE_SELECT *)GKI_getbuf(sizeof(tBTA_JV_API_SERVICE_SELECT))) != NULL)
786    {
787        p_msg->hdr.event = BTA_JV_API_SERVICE_SELECT_EVT;
788        p_msg->uuid = uuid;
789        bta_sys_sendmsg(p_msg);
790        status = BTA_JV_SUCCESS;
791    }
792    return(status);
793}
794
795/*******************************************************************************
796**
797** Function         BTA_JvServiceResult
798**
799** Description      This function returns the contents of the SDP record from
800**                  last BTA_JvServiceSelect. The contents will be returned under
801**                  a TLV (type, len, value) representation in the data buffer
802**                  provided by the caller.
803**
804** Returns          -1, if error. Otherwise, the length of service record.
805**
806*******************************************************************************/
807INT32 BTA_JvServiceResult(UINT8 *TLV)
808{
809    INT32   serv_sel = -1;
810
811    APPL_TRACE_API( "BTA_JvServiceResult");
812    if(bta_jv_cb.p_sel_raw_data)
813    {
814        serv_sel = bta_jv_cb.sel_len;
815        memcpy(TLV, bta_jv_cb.p_sel_raw_data, serv_sel);
816    }
817
818    return serv_sel;
819}
820
821
822/*******************************************************************************
823**
824** Function         BTA_JvCreateRecord
825**
826** Description      Create a service record in the local SDP database.
827**                  When the operation is complete the tBTA_JV_DM_CBACK callback
828**                  function will be called with a BTA_JV_CREATE_RECORD_EVT.
829**
830** Returns          BTA_JV_SUCCESS, if the request is being processed.
831**                  BTA_JV_FAILURE, otherwise.
832**
833*******************************************************************************/
834tBTA_JV_STATUS BTA_JvCreateRecordByUser(void *user_data)
835{
836    tBTA_JV_STATUS status = BTA_JV_FAILURE;
837    tBTA_JV_API_CREATE_RECORD *p_msg;
838
839    APPL_TRACE_API( "BTA_JvCreateRecordByUser");
840    if ((p_msg = (tBTA_JV_API_CREATE_RECORD *)GKI_getbuf(sizeof(tBTA_JV_API_CREATE_RECORD))) != NULL)
841    {
842        p_msg->hdr.event = BTA_JV_API_CREATE_RECORD_EVT;
843        p_msg->user_data = user_data;
844        bta_sys_sendmsg(p_msg);
845        status = BTA_JV_SUCCESS;
846    }
847
848    return(status);
849}
850
851/*******************************************************************************
852**
853** Function         BTA_JvUpdateRecord
854**
855** Description      Update a service record in the local SDP database.
856**                  When the operation is complete the tBTA_JV_DM_CBACK callback
857**                  function will be called with a BTA_JV_UPDATE_RECORD_EVT.
858**
859** Returns          BTA_JV_SUCCESS, if the request is being processed.
860**                  BTA_JV_FAILURE, otherwise.
861**
862*******************************************************************************/
863tBTA_JV_STATUS BTA_JvUpdateRecord(UINT32 handle, UINT16 *p_ids,
864    UINT8 **p_values, INT32 *p_value_sizes, INT32 array_len)
865{
866    UNUSED(handle);
867    UNUSED(p_ids);
868    UNUSED(p_values);
869    UNUSED(p_value_sizes);
870    UNUSED(array_len);
871#if 0
872    tBTA_JV_STATUS status = BTA_JV_FAILURE;
873    tBTA_JV_API_UPDATE_RECORD *p_msg;
874
875    APPL_TRACE_API( "BTA_JvUpdateRecord");
876    if ((p_msg = (tBTA_JV_API_UPDATE_RECORD *)GKI_getbuf(sizeof(tBTA_JV_API_UPDATE_RECORD))) != NULL)
877    {
878        p_msg->hdr.event = BTA_JV_API_UPDATE_RECORD_EVT;
879        p_msg->handle = handle;
880        p_msg->p_ids = p_ids;
881        p_msg->p_values = p_values;
882        p_msg->p_value_sizes = p_value_sizes;
883        p_msg->array_len = array_len;
884        bta_sys_sendmsg(p_msg);
885        status = BTA_JV_SUCCESS;
886    }
887    return(status);
888#endif
889    return -1;
890}
891
892/*******************************************************************************
893**
894** Function         BTA_JvAddAttribute
895**
896** Description      Add an attribute to a service record in the local SDP database.
897**                  When the operation is complete the tBTA_JV_DM_CBACK callback
898**                  function will be called with a BTA_JV_ADD_ATTR_EVT.
899**
900** Returns          BTA_JV_SUCCESS, if the request is being processed.
901**                  BTA_JV_FAILURE, otherwise.
902**
903*******************************************************************************/
904tBTA_JV_STATUS BTA_JvAddAttribute(UINT32 handle, UINT16 attr_id,
905    UINT8 *p_value, INT32 value_size)
906{
907    tBTA_JV_STATUS status = BTA_JV_FAILURE;
908    tBTA_JV_API_ADD_ATTRIBUTE *p_msg;
909
910    APPL_TRACE_API( "BTA_JvAddAttribute");
911    if ((p_msg = (tBTA_JV_API_ADD_ATTRIBUTE *)GKI_getbuf(sizeof(tBTA_JV_API_ADD_ATTRIBUTE))) != NULL)
912    {
913        p_msg->hdr.event = BTA_JV_API_ADD_ATTRIBUTE_EVT;
914        p_msg->handle = handle;
915        p_msg->attr_id = attr_id;
916        p_msg->p_value = p_value;
917        p_msg->value_size = value_size;
918        bta_sys_sendmsg(p_msg);
919        status = BTA_JV_SUCCESS;
920    }
921    return(status);
922}
923
924/*******************************************************************************
925**
926** Function         BTA_JvDeleteAttribute
927**
928** Description      Delete an attribute from a service record in the local SDP database.
929**                  When the operation is complete the tBTA_JV_DM_CBACK callback
930**                  function will be called with a BTA_JV_DELETE_ATTR_EVT.
931**
932** Returns          BTA_JV_SUCCESS, if the request is being processed.
933**                  BTA_JV_FAILURE, otherwise.
934**
935*******************************************************************************/
936tBTA_JV_STATUS BTA_JvDeleteAttribute(UINT32 handle, UINT16 attr_id)
937{
938    tBTA_JV_STATUS status = BTA_JV_FAILURE;
939    tBTA_JV_API_ADD_ATTRIBUTE *p_msg;
940
941    APPL_TRACE_API( "BTA_JvDeleteAttribute");
942    if ((p_msg = (tBTA_JV_API_ADD_ATTRIBUTE *)GKI_getbuf(sizeof(tBTA_JV_API_ADD_ATTRIBUTE))) != NULL)
943    {
944        p_msg->hdr.event = BTA_JV_API_DELETE_ATTRIBUTE_EVT;
945        p_msg->handle = handle;
946        p_msg->attr_id = attr_id;
947        bta_sys_sendmsg(p_msg);
948        status = BTA_JV_SUCCESS;
949    }
950    return(status);
951}
952
953/*******************************************************************************
954**
955** Function         BTA_JvDeleteRecord
956**
957** Description      Delete a service record in the local SDP database.
958**
959** Returns          BTA_JV_SUCCESS, if the request is being processed.
960**                  BTA_JV_FAILURE, otherwise.
961**
962*******************************************************************************/
963tBTA_JV_STATUS BTA_JvDeleteRecord(UINT32 handle)
964{
965    tBTA_JV_STATUS status = BTA_JV_FAILURE;
966    tBTA_JV_API_ADD_ATTRIBUTE *p_msg;
967
968    APPL_TRACE_API( "BTA_JvDeleteRecord");
969    if ((p_msg = (tBTA_JV_API_ADD_ATTRIBUTE *)GKI_getbuf(sizeof(tBTA_JV_API_ADD_ATTRIBUTE))) != NULL)
970    {
971        p_msg->hdr.event = BTA_JV_API_DELETE_RECORD_EVT;
972        p_msg->handle = handle;
973        bta_sys_sendmsg(p_msg);
974        status = BTA_JV_SUCCESS;
975    }
976    return(status);
977}
978
979/*******************************************************************************
980**
981** Function         BTA_JvReadRecord
982**
983** Description      Read a service record in the local SDP database.
984**
985** Returns          -1, if the record is not found.
986**                  Otherwise, the offset (0 or 1) to start of data in p_data.
987**
988**                  The size of data copied into p_data is in *p_data_len.
989**
990*******************************************************************************/
991INT32 BTA_JvReadRecord(UINT32 handle, UINT8 *p_data, INT32 *p_data_len)
992{
993    UINT32 sdp_handle;
994
995    sdp_handle = bta_jv_get_sdp_handle(handle);
996
997    if(sdp_handle)
998    {
999        return SDP_ReadRecord(sdp_handle, p_data, p_data_len);
1000    }
1001
1002    return -1;
1003}
1004
1005/*******************************************************************************
1006**
1007** Function         BTA_JvL2capConnect
1008**
1009** Description      Initiate a connection as a L2CAP client to the given BD
1010**                  Address.
1011**                  When the connection is initiated or failed to initiate,
1012**                  tBTA_JV_L2CAP_CBACK is called with BTA_JV_L2CAP_CL_INIT_EVT
1013**                  When the connection is established or failed,
1014**                  tBTA_JV_L2CAP_CBACK is called with BTA_JV_L2CAP_OPEN_EVT
1015**
1016** Returns          BTA_JV_SUCCESS, if the request is being processed.
1017**                  BTA_JV_FAILURE, otherwise.
1018**
1019*******************************************************************************/
1020tBTA_JV_STATUS BTA_JvL2capConnect(tBTA_SEC sec_mask,
1021                           tBTA_JV_ROLE role, UINT16 remote_psm, UINT16 rx_mtu,
1022                           BD_ADDR peer_bd_addr, tBTA_JV_L2CAP_CBACK *p_cback)
1023{
1024    tBTA_JV_STATUS status = BTA_JV_FAILURE;
1025    tBTA_JV_API_L2CAP_CONNECT *p_msg;
1026
1027    APPL_TRACE_API( "BTA_JvL2capConnect");
1028    if (p_cback &&
1029        (p_msg = (tBTA_JV_API_L2CAP_CONNECT *)GKI_getbuf(sizeof(tBTA_JV_API_L2CAP_CONNECT))) != NULL)
1030    {
1031        p_msg->hdr.event    = BTA_JV_API_L2CAP_CONNECT_EVT;
1032        p_msg->sec_mask     = sec_mask;
1033        p_msg->role         = role;
1034        p_msg->remote_psm   = remote_psm;
1035        p_msg->rx_mtu       = rx_mtu;
1036        memcpy(p_msg->peer_bd_addr, peer_bd_addr, sizeof(BD_ADDR));
1037        p_msg->p_cback      = p_cback;
1038        bta_sys_sendmsg(p_msg);
1039        status = BTA_JV_SUCCESS;
1040    }
1041
1042    return(status);
1043}
1044
1045/*******************************************************************************
1046**
1047** Function         BTA_JvL2capClose
1048**
1049** Description      This function closes an L2CAP client connection
1050**
1051** Returns          BTA_JV_SUCCESS, if the request is being processed.
1052**                  BTA_JV_FAILURE, otherwise.
1053**
1054*******************************************************************************/
1055tBTA_JV_STATUS BTA_JvL2capClose(UINT32 handle)
1056{
1057    tBTA_JV_STATUS status = BTA_JV_FAILURE;
1058    tBTA_JV_API_L2CAP_CLOSE *p_msg;
1059
1060    APPL_TRACE_API( "BTA_JvL2capClose");
1061    if (handle < BTA_JV_MAX_L2C_CONN && bta_jv_cb.l2c_cb[handle].p_cback &&
1062        (p_msg = (tBTA_JV_API_L2CAP_CLOSE *)GKI_getbuf(sizeof(tBTA_JV_API_L2CAP_CLOSE))) != NULL)
1063    {
1064        p_msg->hdr.event = BTA_JV_API_L2CAP_CLOSE_EVT;
1065        p_msg->handle = handle;
1066        p_msg->p_cb = &bta_jv_cb.l2c_cb[handle];
1067        bta_sys_sendmsg(p_msg);
1068        status = BTA_JV_SUCCESS;
1069    }
1070
1071    return(status);
1072}
1073
1074/*******************************************************************************
1075**
1076** Function         BTA_JvL2capStartServer
1077**
1078** Description      This function starts an L2CAP server and listens for an L2CAP
1079**                  connection from a remote Bluetooth device.  When the server
1080**                  is started successfully, tBTA_JV_L2CAP_CBACK is called with
1081**                  BTA_JV_L2CAP_START_EVT.  When the connection is established,
1082**                  tBTA_JV_L2CAP_CBACK is called with BTA_JV_L2CAP_OPEN_EVT.
1083**
1084** Returns          BTA_JV_SUCCESS, if the request is being processed.
1085**                  BTA_JV_FAILURE, otherwise.
1086**
1087*******************************************************************************/
1088tBTA_JV_STATUS BTA_JvL2capStartServer(tBTA_SEC sec_mask, tBTA_JV_ROLE role,
1089                           UINT16 local_psm, UINT16 rx_mtu,
1090                           tBTA_JV_L2CAP_CBACK *p_cback)
1091{
1092    tBTA_JV_STATUS status = BTA_JV_FAILURE;
1093    tBTA_JV_API_L2CAP_SERVER *p_msg;
1094
1095    APPL_TRACE_API( "BTA_JvL2capStartServer");
1096    if (p_cback &&
1097        (p_msg = (tBTA_JV_API_L2CAP_SERVER *)GKI_getbuf(sizeof(tBTA_JV_API_L2CAP_SERVER))) != NULL)
1098    {
1099        p_msg->hdr.event = BTA_JV_API_L2CAP_START_SERVER_EVT;
1100        p_msg->sec_mask = sec_mask;
1101        p_msg->role = role;
1102        p_msg->local_psm = local_psm;
1103        p_msg->rx_mtu = rx_mtu;
1104        p_msg->p_cback = p_cback;
1105        bta_sys_sendmsg(p_msg);
1106        status = BTA_JV_SUCCESS;
1107    }
1108
1109    return(status);
1110}
1111
1112/*******************************************************************************
1113**
1114** Function         BTA_JvL2capStopServer
1115**
1116** Description      This function stops the L2CAP server. If the server has an
1117**                  active connection, it would be closed.
1118**
1119** Returns          BTA_JV_SUCCESS, if the request is being processed.
1120**                  BTA_JV_FAILURE, otherwise.
1121**
1122*******************************************************************************/
1123tBTA_JV_STATUS BTA_JvL2capStopServer(UINT16 local_psm)
1124{
1125    tBTA_JV_STATUS status = BTA_JV_FAILURE;
1126    tBTA_JV_API_L2CAP_SERVER *p_msg;
1127
1128    APPL_TRACE_API( "BTA_JvL2capStopServer");
1129    if ((p_msg = (tBTA_JV_API_L2CAP_SERVER *)GKI_getbuf(sizeof(tBTA_JV_API_L2CAP_SERVER))) != NULL)
1130    {
1131        p_msg->hdr.event = BTA_JV_API_L2CAP_STOP_SERVER_EVT;
1132        p_msg->local_psm = local_psm;
1133        bta_sys_sendmsg(p_msg);
1134        status = BTA_JV_SUCCESS;
1135    }
1136
1137    return(status);
1138}
1139
1140/*******************************************************************************
1141**
1142** Function         BTA_JvL2capRead
1143**
1144** Description      This function reads data from an L2CAP connecti;
1145    tBTA_JV_RFC_CB  *p_cb = rc->p_cb;
1146on
1147**                  When the operation is complete, tBTA_JV_L2CAP_CBACK is
1148**                  called with BTA_JV_L2CAP_READ_EVT.
1149**
1150** Returns          BTA_JV_SUCCESS, if the request is being processed.
1151**                  BTA_JV_FAILURE, otherwise.
1152**
1153*******************************************************************************/
1154tBTA_JV_STATUS BTA_JvL2capRead(UINT32 handle, UINT32 req_id, UINT8 *p_data, UINT16 len)
1155{
1156    UNUSED(handle);
1157    UNUSED(req_id);
1158    UNUSED(p_data);
1159    UNUSED(len);
1160
1161#if 0
1162    tBTA_JV_STATUS status = BTA_JV_FAILURE;
1163#if SDP_FOR_JV_INCLUDED == TRUE
1164    tBTA_JV_API_L2CAP_READ *p_msg;
1165#endif
1166    tBTA_JV_L2CAP_READ evt_data;
1167
1168    APPL_TRACE_API( "BTA_JvL2capRead");
1169
1170#if SDP_FOR_JV_INCLUDED == TRUE
1171    if(BTA_JV_L2C_FOR_SDP_HDL == handle)
1172    {
1173        if (bta_jv_cb.l2c_cb[handle].p_cback &&
1174            (p_msg = (tBTA_JV_API_L2CAP_READ *)GKI_getbuf(sizeof(tBTA_JV_API_L2CAP_READ))) != NULL)
1175        {
1176            p_msg->hdr.event = BTA_JV_API_L2CAP_READ_EVT;
1177            p_msg->handle = handle;
1178            p_msg->req_id = req_id;
1179            p_msg->p_data = p_data;
1180            p_msg->len = len;
1181            p_msg->p_cback = bta_jv_cb.l2c_cb[handle].p_cback;
1182            bta_sys_sendmsg(p_msg);
1183            status = BTA_JV_SUCCESS;
1184        }
1185    }
1186    else
1187#endif
1188    if (handle < BTA_JV_MAX_L2C_CONN && bta_jv_cb.l2c_cb[handle].p_cback)
1189    {
1190        status = BTA_JV_SUCCESS;
1191        evt_data.status = BTA_JV_FAILURE;
1192        evt_data.handle = handle;
1193        evt_data.req_id = req_id;
1194        evt_data.p_data = p_data;
1195        evt_data.len    = 0;
1196
1197        if (BT_PASS == GAP_ConnReadData((UINT16)handle, p_data, len, &evt_data.len))
1198        {
1199            evt_data.status = BTA_JV_SUCCESS;
1200        }
1201        bta_jv_cb.l2c_cb[handle].p_cback(BTA_JV_L2CAP_READ_EVT, (tBTA_JV *)&evt_data);
1202    }
1203
1204    return(status);
1205#endif
1206    return -1;
1207}
1208
1209/*******************************************************************************
1210**
1211** Function         BTA_JvL2capReceive
1212**
1213** Description      This function reads data from an L2CAP connection
1214**                  When the operation is complete, tBTA_JV_L2CAP_CBACK is
1215**                  called with BTA_JV_L2CAP_RECEIVE_EVT.
1216**                  If there are more data queued in L2CAP than len, the extra data will be discarded.
1217**
1218** Returns          BTA_JV_SUCCESS, if the request is being processed.
1219**                  BTA_JV_FAILURE, otherwise.
1220**
1221*******************************************************************************/
1222tBTA_JV_STATUS BTA_JvL2capReceive(UINT32 handle, UINT32 req_id, UINT8 *p_data, UINT16 len)
1223{
1224    UNUSED(handle);
1225    UNUSED(req_id);
1226    UNUSED(p_data);
1227    UNUSED(len);
1228#if 0
1229    tBTA_JV_STATUS status = BTA_JV_FAILURE;
1230    tBTA_JV_L2CAP_RECEIVE evt_data;
1231    UINT32  left_over = 0;
1232    UINT16  max_len, read_len;
1233
1234    APPL_TRACE_API( "BTA_JvL2capReceive");
1235
1236    if (handle < BTA_JV_MAX_L2C_CONN && bta_jv_cb.l2c_cb[handle].p_cback)
1237    {
1238        status = BTA_JV_SUCCESS;
1239        evt_data.status = BTA_JV_FAILURE;
1240        evt_data.handle = handle;
1241        evt_data.req_id = req_id;
1242        evt_data.p_data = p_data;
1243        evt_data.len    = 0;
1244
1245        if (BT_PASS == GAP_ConnReadData((UINT16)handle, p_data, len, &evt_data.len))
1246        {
1247            evt_data.status = BTA_JV_SUCCESS;
1248            GAP_GetRxQueueCnt ((UINT16)handle, &left_over);
1249            while (left_over)
1250            {
1251                max_len = (left_over > 0xFFFF)?0xFFFF:left_over;
1252                GAP_ConnReadData ((UINT16)handle, NULL, max_len, &read_len);
1253                left_over -= read_len;
1254            }
1255        }
1256        bta_jv_cb.l2c_cb[handle].p_cback(BTA_JV_L2CAP_RECEIVE_EVT, (tBTA_JV *)&evt_data);
1257    }
1258
1259    return(status);
1260#endif
1261    return -1;
1262}
1263/*******************************************************************************
1264**
1265** Function         BTA_JvL2capReady
1266**
1267** Description      This function determined if there is data to read from
1268**                    an L2CAP connection
1269**
1270** Returns          BTA_JV_SUCCESS, if data queue size is in *p_data_size.
1271**                  BTA_JV_FAILURE, if error.
1272**
1273*******************************************************************************/
1274tBTA_JV_STATUS BTA_JvL2capReady(UINT32 handle, UINT32 *p_data_size)
1275{
1276    UNUSED(handle);
1277    UNUSED(p_data_size);
1278#if 0
1279    tBTA_JV_STATUS status = BTA_JV_FAILURE;
1280
1281    APPL_TRACE_API( "BTA_JvL2capReady: %d", handle);
1282    if (p_data_size && handle < BTA_JV_MAX_L2C_CONN && bta_jv_cb.l2c_cb[handle].p_cback)
1283    {
1284        *p_data_size = 0;
1285#if SDP_FOR_JV_INCLUDED == TRUE
1286        if(BTA_JV_L2C_FOR_SDP_HDL == handle)
1287        {
1288            *p_data_size = bta_jv_cb.sdp_data_size;
1289            status = BTA_JV_SUCCESS;
1290        }
1291        else
1292#endif
1293        if(BT_PASS == GAP_GetRxQueueCnt((UINT16)handle, p_data_size) )
1294        {
1295            status = BTA_JV_SUCCESS;
1296        }
1297    }
1298
1299    return(status);
1300#endif
1301    return -1;
1302}
1303
1304
1305/*******************************************************************************
1306**
1307** Function         BTA_JvL2capWrite
1308**
1309** Description      This function writes data to an L2CAP connection
1310**                  When the operation is complete, tBTA_JV_L2CAP_CBACK is
1311**                  called with BTA_JV_L2CAP_WRITE_EVT.
1312**
1313** Returns          BTA_JV_SUCCESS, if the request is being processed.
1314**                  BTA_JV_FAILURE, otherwise.
1315**
1316*******************************************************************************/
1317tBTA_JV_STATUS BTA_JvL2capWrite(UINT32 handle, UINT32 req_id, UINT8 *p_data, UINT16 len)
1318{
1319    tBTA_JV_STATUS status = BTA_JV_FAILURE;
1320    tBTA_JV_API_L2CAP_WRITE *p_msg;
1321
1322    APPL_TRACE_API( "BTA_JvL2capWrite");
1323    if (handle < BTA_JV_MAX_L2C_CONN && bta_jv_cb.l2c_cb[handle].p_cback &&
1324        (p_msg = (tBTA_JV_API_L2CAP_WRITE *)GKI_getbuf(sizeof(tBTA_JV_API_L2CAP_WRITE))) != NULL)
1325    {
1326        p_msg->hdr.event = BTA_JV_API_L2CAP_WRITE_EVT;
1327        p_msg->handle = handle;
1328        p_msg->req_id = req_id;
1329        p_msg->p_data = p_data;
1330        p_msg->p_cb = &bta_jv_cb.l2c_cb[handle];
1331        p_msg->len = len;
1332        bta_sys_sendmsg(p_msg);
1333        status = BTA_JV_SUCCESS;
1334    }
1335
1336    return(status);
1337}
1338
1339/*******************************************************************************
1340**
1341** Function         BTA_JvRfcommConnect
1342**
1343** Description      This function makes an RFCOMM conection to a remote BD
1344**                  Address.
1345**                  When the connection is initiated or failed to initiate,
1346**                  tBTA_JV_RFCOMM_CBACK is called with BTA_JV_RFCOMM_CL_INIT_EVT
1347**                  When the connection is established or failed,
1348**                  tBTA_JV_RFCOMM_CBACK is called with BTA_JV_RFCOMM_OPEN_EVT
1349**
1350** Returns          BTA_JV_SUCCESS, if the request is being processed.
1351**                  BTA_JV_FAILURE, otherwise.
1352**
1353*******************************************************************************/
1354tBTA_JV_STATUS BTA_JvRfcommConnect(tBTA_SEC sec_mask,
1355                           tBTA_JV_ROLE role, UINT8 remote_scn, BD_ADDR peer_bd_addr,
1356                           tBTA_JV_RFCOMM_CBACK *p_cback, void* user_data)
1357{
1358    tBTA_JV_STATUS status = BTA_JV_FAILURE;
1359    tBTA_JV_API_RFCOMM_CONNECT *p_msg;
1360
1361    APPL_TRACE_API( "BTA_JvRfcommConnect");
1362    if (p_cback &&
1363        (p_msg = (tBTA_JV_API_RFCOMM_CONNECT *)GKI_getbuf(sizeof(tBTA_JV_API_RFCOMM_CONNECT))) != NULL)
1364    {
1365        p_msg->hdr.event    = BTA_JV_API_RFCOMM_CONNECT_EVT;
1366        p_msg->sec_mask     = sec_mask;
1367        p_msg->role         = role;
1368        p_msg->remote_scn   = remote_scn;
1369        memcpy(p_msg->peer_bd_addr, peer_bd_addr, sizeof(BD_ADDR));
1370        p_msg->p_cback      = p_cback;
1371        p_msg->user_data    = user_data;
1372        bta_sys_sendmsg(p_msg);
1373        status = BTA_JV_SUCCESS;
1374    }
1375
1376    return(status);
1377}
1378
1379/*******************************************************************************
1380**
1381** Function         BTA_JvRfcommClose
1382**
1383** Description      This function closes an RFCOMM connection
1384**
1385** Returns          BTA_JV_SUCCESS, if the request is being processed.
1386**                  BTA_JV_FAILURE, otherwise.
1387**
1388*******************************************************************************/
1389tBTA_JV_STATUS BTA_JvRfcommClose(UINT32 handle, void *user_data)
1390{
1391    tBTA_JV_STATUS status = BTA_JV_FAILURE;
1392    tBTA_JV_API_RFCOMM_CLOSE *p_msg;
1393    UINT32  hi = ((handle & BTA_JV_RFC_HDL_MASK)&~BTA_JV_RFCOMM_MASK) - 1;
1394    UINT32  si = BTA_JV_RFC_HDL_TO_SIDX(handle);
1395
1396    APPL_TRACE_API( "BTA_JvRfcommClose");
1397    if (hi < BTA_JV_MAX_RFC_CONN && bta_jv_cb.rfc_cb[hi].p_cback &&
1398        si < BTA_JV_MAX_RFC_SR_SESSION && bta_jv_cb.rfc_cb[hi].rfc_hdl[si] &&
1399        (p_msg = (tBTA_JV_API_RFCOMM_CLOSE *)GKI_getbuf(sizeof(tBTA_JV_API_RFCOMM_CLOSE))) != NULL)
1400    {
1401        p_msg->hdr.event = BTA_JV_API_RFCOMM_CLOSE_EVT;
1402        p_msg->handle = handle;
1403        p_msg->p_cb = &bta_jv_cb.rfc_cb[hi];
1404        p_msg->p_pcb = &bta_jv_cb.port_cb[p_msg->p_cb->rfc_hdl[si] - 1];
1405        p_msg->user_data = user_data;
1406        bta_sys_sendmsg(p_msg);
1407        status = BTA_JV_SUCCESS;
1408    }
1409
1410    return(status);
1411}
1412
1413/*******************************************************************************
1414**
1415** Function         BTA_JvRfcommStartServer
1416**
1417** Description      This function starts listening for an RFCOMM connection
1418**                  request from a remote Bluetooth device.  When the server is
1419**                  started successfully, tBTA_JV_RFCOMM_CBACK is called
1420**                  with BTA_JV_RFCOMM_START_EVT.
1421**                  When the connection is established, tBTA_JV_RFCOMM_CBACK
1422**                  is called with BTA_JV_RFCOMM_OPEN_EVT.
1423**
1424** Returns          BTA_JV_SUCCESS, if the request is being processed.
1425**                  BTA_JV_FAILURE, otherwise.
1426**
1427*******************************************************************************/
1428tBTA_JV_STATUS BTA_JvRfcommStartServer(tBTA_SEC sec_mask,
1429                           tBTA_JV_ROLE role, UINT8 local_scn, UINT8 max_session,
1430                           tBTA_JV_RFCOMM_CBACK *p_cback, void* user_data)
1431{
1432    tBTA_JV_STATUS status = BTA_JV_FAILURE;
1433    tBTA_JV_API_RFCOMM_SERVER *p_msg;
1434
1435    APPL_TRACE_API( "BTA_JvRfcommStartServer");
1436    if (p_cback &&
1437        (p_msg = (tBTA_JV_API_RFCOMM_SERVER *)GKI_getbuf(sizeof(tBTA_JV_API_RFCOMM_SERVER))) != NULL)
1438    {
1439        if (max_session == 0)
1440            max_session = 1;
1441        if (max_session > BTA_JV_MAX_RFC_SR_SESSION)
1442        {
1443            APPL_TRACE_DEBUG( "max_session is too big. use max (%d)", max_session, BTA_JV_MAX_RFC_SR_SESSION);
1444            max_session = BTA_JV_MAX_RFC_SR_SESSION;
1445        }
1446        p_msg->hdr.event = BTA_JV_API_RFCOMM_START_SERVER_EVT;
1447        p_msg->sec_mask = sec_mask;
1448        p_msg->role = role;
1449        p_msg->local_scn = local_scn;
1450        p_msg->max_session = max_session;
1451        p_msg->p_cback = p_cback;
1452        p_msg->user_data = user_data; //caller's private data
1453        bta_sys_sendmsg(p_msg);
1454        status = BTA_JV_SUCCESS;
1455    }
1456
1457    return(status);
1458}
1459
1460/*******************************************************************************
1461**
1462** Function         BTA_JvRfcommStopServer
1463**
1464** Description      This function stops the RFCOMM server. If the server has an
1465**                  active connection, it would be closed.
1466**
1467** Returns          BTA_JV_SUCCESS, if the request is being processed.
1468**                  BTA_JV_FAILURE, otherwise.
1469**
1470*******************************************************************************/
1471tBTA_JV_STATUS BTA_JvRfcommStopServer(UINT32 handle, void * user_data)
1472{
1473    tBTA_JV_STATUS status = BTA_JV_FAILURE;
1474    tBTA_JV_API_RFCOMM_SERVER *p_msg;
1475    APPL_TRACE_API( "BTA_JvRfcommStopServer");
1476    if ((p_msg = (tBTA_JV_API_RFCOMM_SERVER *)GKI_getbuf(sizeof(tBTA_JV_API_RFCOMM_SERVER))) != NULL)
1477    {
1478        p_msg->hdr.event = BTA_JV_API_RFCOMM_STOP_SERVER_EVT;
1479        p_msg->handle = handle;
1480        p_msg->user_data = user_data; //caller's private data
1481        bta_sys_sendmsg(p_msg);
1482        status = BTA_JV_SUCCESS;
1483    }
1484
1485    return(status);
1486}
1487
1488/*******************************************************************************
1489**
1490** Function         BTA_JvRfcommRead
1491**
1492** Description      This function reads data from an RFCOMM connection
1493**                  The actual size of data read is returned in p_len.
1494**
1495** Returns          BTA_JV_SUCCESS, if the request is being processed.
1496**                  BTA_JV_FAILURE, otherwise.
1497**
1498*******************************************************************************/
1499tBTA_JV_STATUS BTA_JvRfcommRead(UINT32 handle, UINT32 req_id, UINT8 *p_data, UINT16 len)
1500{
1501    tBTA_JV_STATUS status = BTA_JV_FAILURE;
1502    tBTA_JV_API_RFCOMM_READ *p_msg;
1503    UINT32  hi = ((handle & BTA_JV_RFC_HDL_MASK)&~BTA_JV_RFCOMM_MASK) - 1;
1504    UINT32  si = BTA_JV_RFC_HDL_TO_SIDX(handle);
1505
1506    APPL_TRACE_API( "BTA_JvRfcommRead");
1507    if (hi < BTA_JV_MAX_RFC_CONN && bta_jv_cb.rfc_cb[hi].p_cback &&
1508        si < BTA_JV_MAX_RFC_SR_SESSION && bta_jv_cb.rfc_cb[hi].rfc_hdl[si] &&
1509        (p_msg = (tBTA_JV_API_RFCOMM_READ *)GKI_getbuf(sizeof(tBTA_JV_API_RFCOMM_READ))) != NULL)
1510    {
1511        p_msg->hdr.event = BTA_JV_API_RFCOMM_READ_EVT;
1512        p_msg->handle = handle;
1513        p_msg->req_id = req_id;
1514        p_msg->p_data = p_data;
1515        p_msg->len = len;
1516        p_msg->p_cb = &bta_jv_cb.rfc_cb[hi];
1517        p_msg->p_pcb = &bta_jv_cb.port_cb[p_msg->p_cb->rfc_hdl[si] - 1];
1518        bta_sys_sendmsg(p_msg);
1519        status = BTA_JV_SUCCESS;
1520    }
1521
1522    return(status);
1523}
1524
1525/*******************************************************************************
1526**
1527** Function         BTA_JvRfcommGetPortHdl
1528**
1529** Description    This function fetches the rfcomm port handle
1530**
1531** Returns          BTA_JV_SUCCESS, if the request is being processed.
1532**                  BTA_JV_FAILURE, otherwise.
1533**
1534*******************************************************************************/
1535UINT16 BTA_JvRfcommGetPortHdl(UINT32 handle)
1536{
1537    UINT32  hi = ((handle & BTA_JV_RFC_HDL_MASK) & ~BTA_JV_RFCOMM_MASK) - 1;
1538    UINT32  si = BTA_JV_RFC_HDL_TO_SIDX(handle);
1539
1540    if (hi < BTA_JV_MAX_RFC_CONN &&
1541        si < BTA_JV_MAX_RFC_SR_SESSION && bta_jv_cb.rfc_cb[hi].rfc_hdl[si])
1542        return bta_jv_cb.port_cb[bta_jv_cb.rfc_cb[hi].rfc_hdl[si] - 1].port_handle;
1543    else
1544        return 0xffff;
1545}
1546
1547
1548/*******************************************************************************
1549**
1550** Function         BTA_JvRfcommReady
1551**
1552** Description      This function determined if there is data to read from
1553**                  an RFCOMM connection
1554**
1555** Returns          BTA_JV_SUCCESS, if data queue size is in *p_data_size.
1556**                  BTA_JV_FAILURE, if error.
1557**
1558*******************************************************************************/
1559tBTA_JV_STATUS BTA_JvRfcommReady(UINT32 handle, UINT32 *p_data_size)
1560{
1561    tBTA_JV_STATUS status = BTA_JV_FAILURE;
1562    UINT16          size = 0;
1563    UINT32  hi = ((handle & BTA_JV_RFC_HDL_MASK)&~BTA_JV_RFCOMM_MASK) - 1;
1564    UINT32  si = BTA_JV_RFC_HDL_TO_SIDX(handle);
1565
1566    APPL_TRACE_API( "BTA_JvRfcommReady");
1567    if (hi < BTA_JV_MAX_RFC_CONN && bta_jv_cb.rfc_cb[hi].p_cback &&
1568        si < BTA_JV_MAX_RFC_SR_SESSION && bta_jv_cb.rfc_cb[hi].rfc_hdl[si])
1569    {
1570        if(PORT_GetRxQueueCnt(bta_jv_cb.rfc_cb[hi].rfc_hdl[si], &size) == PORT_SUCCESS)
1571        {
1572            status = BTA_JV_SUCCESS;
1573        }
1574    }
1575    *p_data_size = size;
1576    return(status);
1577}
1578
1579/*******************************************************************************
1580**
1581** Function         BTA_JvRfcommWrite
1582**
1583** Description      This function writes data to an RFCOMM connection
1584**
1585** Returns          BTA_JV_SUCCESS, if the request is being processed.
1586**                  BTA_JV_FAILURE, otherwise.
1587**
1588*******************************************************************************/
1589tBTA_JV_STATUS BTA_JvRfcommWrite(UINT32 handle, UINT32 req_id)
1590{
1591    tBTA_JV_STATUS status = BTA_JV_FAILURE;
1592    tBTA_JV_API_RFCOMM_WRITE *p_msg;
1593    UINT32  hi = ((handle & BTA_JV_RFC_HDL_MASK)&~BTA_JV_RFCOMM_MASK) - 1;
1594    UINT32  si = BTA_JV_RFC_HDL_TO_SIDX(handle);
1595
1596    APPL_TRACE_API( "BTA_JvRfcommWrite");
1597    APPL_TRACE_DEBUG( "handle:0x%x, hi:%d, si:%d", handle, hi, si);
1598    if (hi < BTA_JV_MAX_RFC_CONN && bta_jv_cb.rfc_cb[hi].p_cback &&
1599        si < BTA_JV_MAX_RFC_SR_SESSION && bta_jv_cb.rfc_cb[hi].rfc_hdl[si] &&
1600        (p_msg = (tBTA_JV_API_RFCOMM_WRITE *)GKI_getbuf(sizeof(tBTA_JV_API_RFCOMM_WRITE))) != NULL)
1601    {
1602        p_msg->hdr.event = BTA_JV_API_RFCOMM_WRITE_EVT;
1603        p_msg->handle = handle;
1604        p_msg->req_id = req_id;
1605        p_msg->p_cb = &bta_jv_cb.rfc_cb[hi];
1606        p_msg->p_pcb = &bta_jv_cb.port_cb[p_msg->p_cb->rfc_hdl[si] - 1];
1607        APPL_TRACE_API( "write ok");
1608        bta_sys_sendmsg(p_msg);
1609        status = BTA_JV_SUCCESS;
1610    }
1611
1612    return(status);
1613}
1614
1615
1616/*******************************************************************************
1617 **
1618 ** Function    BTA_JVSetPmProfile
1619 **
1620 ** Description This function set or free power mode profile for different JV application
1621 **
1622 ** Parameters:  handle,  JV handle from RFCOMM or L2CAP
1623 **              app_id:  app specific pm ID, can be BTA_JV_PM_ALL, see bta_dm_cfg.c for details
1624 **              BTA_JV_PM_ID_CLEAR: removes pm management on the handle. init_st is ignored and
1625 **              BTA_JV_CONN_CLOSE is called implicitely
1626 **              init_st:  state after calling this API. typically it should be BTA_JV_CONN_OPEN
1627 **
1628 ** Returns      BTA_JV_SUCCESS, if the request is being processed.
1629 **              BTA_JV_FAILURE, otherwise.
1630 **
1631 ** NOTE:        BTA_JV_PM_ID_CLEAR: In general no need to be called as jv pm calls automatically
1632 **              BTA_JV_CONN_CLOSE to remove in case of connection close!
1633 **
1634 *******************************************************************************/
1635tBTA_JV_STATUS BTA_JvSetPmProfile(UINT32 handle, tBTA_JV_PM_ID app_id, tBTA_JV_CONN_STATE init_st)
1636{
1637    tBTA_JV_STATUS status = BTA_JV_FAILURE;
1638    tBTA_JV_API_SET_PM_PROFILE *p_msg;
1639
1640    APPL_TRACE_API("BTA_JVSetPmProfile handle:0x%x, app_id:%d", handle, app_id);
1641    if ((p_msg = (tBTA_JV_API_SET_PM_PROFILE *)GKI_getbuf(sizeof(tBTA_JV_API_SET_PM_PROFILE)))
1642        != NULL)
1643    {
1644        p_msg->hdr.event = BTA_JV_API_SET_PM_PROFILE_EVT;
1645        p_msg->handle = handle;
1646        p_msg->app_id = app_id;
1647        p_msg->init_st = init_st;
1648        bta_sys_sendmsg(p_msg);
1649        status = BTA_JV_SUCCESS;
1650    }
1651
1652    return (status);
1653}
1654