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