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