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