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 private interface file for the BTA Java I/F
22  *
23  ******************************************************************************/
24 #ifndef BTA_JV_INT_H
25 #define BTA_JV_INT_H
26 
27 #include "bta/bta_sys.h"
28 #include "bta/bta_api.h"
29 #include "bta/bta_jv_api.h"
30 #include "stack/rfcdefs.h"
31 #include "stack/port_api.h"
32 #include "stack/sdp_api.h"
33 
34 #include "common/bt_target.h"
35 #if (defined BTA_JV_INCLUDED && BTA_JV_INCLUDED == TRUE)
36 /*****************************************************************************
37 **  Constants
38 *****************************************************************************/
39 
40 #define SPP_VERSION             0x0102
41 
42 enum {
43     /* these events are handled by the state machine */
44     BTA_JV_API_ENABLE_EVT = BTA_SYS_EVT_START(BTA_ID_JV),
45     BTA_JV_API_DISABLE_EVT,
46     BTA_JV_API_GET_CHANNEL_EVT,
47     BTA_JV_API_FREE_SCN_EVT,
48     BTA_JV_API_START_DISCOVERY_EVT,
49     BTA_JV_API_CREATE_RECORD_EVT,
50     BTA_JV_API_DELETE_RECORD_EVT,
51 #if BTA_JV_L2CAP_INCLUDED
52     BTA_JV_API_L2CAP_CONNECT_EVT,
53     BTA_JV_API_L2CAP_CLOSE_EVT,
54     BTA_JV_API_L2CAP_START_SERVER_EVT,
55     BTA_JV_API_L2CAP_STOP_SERVER_EVT,
56     BTA_JV_API_L2CAP_READ_EVT,
57     BTA_JV_API_L2CAP_WRITE_EVT,
58 #endif /* BTA_JV_L2CAP_INCLUDED */
59     BTA_JV_API_RFCOMM_CONNECT_EVT,
60     BTA_JV_API_RFCOMM_CLOSE_EVT,
61     BTA_JV_API_RFCOMM_START_SERVER_EVT,
62     BTA_JV_API_RFCOMM_STOP_SERVER_EVT,
63     BTA_JV_API_RFCOMM_READ_EVT,
64     BTA_JV_API_RFCOMM_WRITE_EVT,
65     BTA_JV_API_RFCOMM_FLOW_CONTROL_EVT,
66     BTA_JV_API_SET_PM_PROFILE_EVT,
67     BTA_JV_API_PM_STATE_CHANGE_EVT,
68 #if BTA_JV_L2CAP_INCLUDED
69     BTA_JV_API_L2CAP_CONNECT_LE_EVT,
70     BTA_JV_API_L2CAP_START_SERVER_LE_EVT,
71     BTA_JV_API_L2CAP_STOP_SERVER_LE_EVT,
72     BTA_JV_API_L2CAP_WRITE_FIXED_EVT,
73     BTA_JV_API_L2CAP_CLOSE_FIXED_EVT,
74 #endif /* BTA_JV_L2CAP_INCLUDED */
75     BTA_JV_MAX_INT_EVT
76 };
77 
78 #ifndef BTA_JV_RFC_EV_MASK
79 #define BTA_JV_RFC_EV_MASK (PORT_EV_RXCHAR | PORT_EV_TXEMPTY | PORT_EV_FC | PORT_EV_FCS)
80 #endif
81 
82 /* data type for BTA_JV_API_ENABLE_EVT */
83 typedef struct {
84     BT_HDR          hdr;
85     tBTA_JV_DM_CBACK   *p_cback;
86 } tBTA_JV_API_ENABLE;
87 
88 /* data type for BTA_JV_API_DISABLE_EVT */
89 typedef struct {
90     BT_HDR          hdr;
91     tBTA_JV_RFCOMM_CBACK   *p_cback;
92 } tBTA_JV_API_DISABLE;
93 
94 /* data type for BTA_JV_API_START_DISCOVERY_EVT */
95 typedef struct {
96     BT_HDR      hdr;
97     BD_ADDR bd_addr;
98     UINT16 num_uuid;
99     tSDP_UUID uuid_list[BTA_JV_MAX_UUIDS];
100     UINT16 num_attr;
101     UINT16 attr_list[BTA_JV_MAX_ATTRS];
102     void            *user_data;      /* piggyback caller's private data*/
103 } tBTA_JV_API_START_DISCOVERY;
104 
105 enum {
106     BTA_JV_PM_FREE_ST = 0, /* empty PM slot */
107     BTA_JV_PM_IDLE_ST,
108     BTA_JV_PM_BUSY_ST
109 };
110 
111 /* BTA JV PM control block */
112 typedef struct {
113     UINT32          handle;     /* The connection handle */
114     UINT8           state;      /* state: see above enum */
115     tBTA_JV_PM_ID   app_id;     /* JV app specific id indicating power table to use */
116     BD_ADDR         peer_bd_addr;    /* Peer BD address */
117 } tBTA_JV_PM_CB;
118 
119 enum {
120     BTA_JV_ST_NONE = 0,
121     BTA_JV_ST_CL_OPENING,
122     BTA_JV_ST_CL_OPEN,
123     BTA_JV_ST_CL_CLOSING,
124     BTA_JV_ST_SR_LISTEN,
125     BTA_JV_ST_SR_OPEN,
126     BTA_JV_ST_SR_CLOSING
127 } ;
128 typedef UINT8  tBTA_JV_STATE;
129 #define BTA_JV_ST_CL_MAX    BTA_JV_ST_CL_CLOSING
130 
131 #if BTA_JV_L2CAP_INCLUDED
132 /* JV L2CAP control block */
133 typedef struct {
134     tBTA_JV_L2CAP_CBACK *p_cback;   /* the callback function */
135     UINT16              psm;        /* the psm used for this server connection */
136     tBTA_JV_STATE       state;      /* the state of this control block */
137     tBTA_SERVICE_ID     sec_id;     /* service id */
138     UINT32              handle;     /* the handle reported to java app (same as gap handle) */
139     BOOLEAN             cong;       /* TRUE, if congested */
140     tBTA_JV_PM_CB      *p_pm_cb;    /* ptr to pm control block, NULL: unused */
141     void                *user_data; /* user data for callback from higher layers */
142 } tBTA_JV_L2C_CB;
143 #endif /* BTA_JV_L2CAP_INCLUDED */
144 
145 #define BTA_JV_RFC_HDL_MASK         0xFF
146 #define BTA_JV_RFCOMM_MASK          0x80
147 #define BTA_JV_ALL_APP_ID           0xFF
148 #define BTA_JV_RFC_HDL_TO_SIDX(r)   (((r)&0xFF00) >> 8)
149 #define BTA_JV_RFC_H_S_TO_HDL(h, s) ((h)|(s<<8))
150 
151 /* port control block */
152 typedef struct {
153     UINT32              handle;     /* the rfcomm session handle at jv */
154     UINT16              port_handle;/* port handle */
155     tBTA_JV_STATE       state;      /* the state of this control block */
156     UINT8               max_sess;   /* max sessions */
157     void                *user_data; /* piggyback caller's private data*/
158     BOOLEAN             cong;       /* TRUE, if congested */
159     tBTA_JV_PM_CB       *p_pm_cb;   /* ptr to pm control block, NULL: unused */
160 } tBTA_JV_PCB;
161 
162 /* JV RFCOMM control block */
163 typedef struct {
164     tBTA_JV_RFCOMM_CBACK *p_cback;  /* the callback function */
165     UINT16              rfc_hdl[BTA_JV_MAX_RFC_SR_SESSION];
166     tBTA_SERVICE_ID     sec_id;     /* service id */
167     UINT8               handle;     /* index: the handle reported to java app */
168     UINT8               scn;        /* the scn of the server */
169     UINT8               max_sess;   /* max sessions */
170     int                 curr_sess;   /* current sessions count*/
171 } tBTA_JV_RFC_CB;
172 
173 #if BTA_JV_L2CAP_INCLUDED
174 /* data type for BTA_JV_API_L2CAP_CONNECT_EVT & BTA_JV_API_L2CAP_CONNECT_LE_EVT */
175 typedef struct {
176     BT_HDR              hdr;
177     tBTA_SEC            sec_mask;
178     tBTA_JV_ROLE        role;
179     union {
180         UINT16          remote_psm;
181         UINT16          remote_chan;
182     };
183     UINT16              rx_mtu;
184     BD_ADDR             peer_bd_addr;
185     INT32               has_cfg;
186     tL2CAP_CFG_INFO     cfg;
187     INT32               has_ertm_info;
188     tL2CAP_ERTM_INFO    ertm_info;
189     tBTA_JV_L2CAP_CBACK *p_cback;
190     void                *user_data;
191 } tBTA_JV_API_L2CAP_CONNECT;
192 
193 /* data type for BTA_JV_API_L2CAP_SERVER_EVT */
194 typedef struct {
195     BT_HDR              hdr;
196     tBTA_SEC            sec_mask;
197     tBTA_JV_ROLE        role;
198     union {
199         UINT16          local_psm;
200         UINT16          local_chan;
201     };
202     UINT16              rx_mtu;
203     INT32               has_cfg;
204     tL2CAP_CFG_INFO     cfg;
205     INT32               has_ertm_info;
206     tL2CAP_ERTM_INFO    ertm_info;
207     tBTA_JV_L2CAP_CBACK *p_cback;
208     void                *user_data;
209 } tBTA_JV_API_L2CAP_SERVER;
210 
211 /* data type for BTA_JV_API_L2CAP_CLOSE_EVT */
212 typedef struct {
213     BT_HDR          hdr;
214     UINT32          handle;
215     tBTA_JV_L2C_CB  *p_cb;
216 } tBTA_JV_API_L2CAP_CLOSE;
217 
218 /* data type for BTA_JV_API_L2CAP_READ_EVT */
219 typedef struct {
220     BT_HDR              hdr;
221     UINT32              handle;
222     UINT32              req_id;
223     tBTA_JV_L2CAP_CBACK *p_cback;
224     UINT8              *p_data;
225     UINT16              len;
226     void                *user_data;
227 } tBTA_JV_API_L2CAP_READ;
228 
229 /* data type for BTA_JV_API_L2CAP_WRITE_EVT */
230 typedef struct {
231     BT_HDR              hdr;
232     UINT32              handle;
233     UINT32              req_id;
234     tBTA_JV_L2C_CB      *p_cb;
235     UINT8               *p_data;
236     UINT16              len;
237     void                *user_data;
238 } tBTA_JV_API_L2CAP_WRITE;
239 
240 /* data type for BTA_JV_API_L2CAP_WRITE_FIXED_EVT */
241 typedef struct {
242     BT_HDR              hdr;
243     UINT16              channel;
244     BD_ADDR             addr;
245     UINT32              req_id;
246     tBTA_JV_L2CAP_CBACK *p_cback;
247     UINT8               *p_data;
248     UINT16              len;
249     void                *user_data;
250 } tBTA_JV_API_L2CAP_WRITE_FIXED;
251 #endif /* BTA_JV_L2CAP_INCLUDED */
252 
253 /* data type for BTA_JV_API_RFCOMM_CONNECT_EVT */
254 typedef struct {
255     BT_HDR          hdr;
256     tBTA_SEC        sec_mask;
257     tBTA_JV_ROLE    role;
258     UINT8           remote_scn;
259     BD_ADDR         peer_bd_addr;
260     tBTA_JV_RFCOMM_CBACK *p_cback;
261     void            *user_data;
262 } tBTA_JV_API_RFCOMM_CONNECT;
263 
264 /* data type for BTA_JV_API_RFCOMM_SERVER_EVT */
265 typedef struct {
266     BT_HDR          hdr;
267     tBTA_SEC        sec_mask;
268     tBTA_JV_ROLE    role;
269     UINT8           local_scn;
270     UINT8           max_session;
271     UINT32          handle;
272     tBTA_JV_RFCOMM_CBACK *p_cback;
273     void            *user_data;
274 } tBTA_JV_API_RFCOMM_SERVER;
275 
276 /* data type for BTA_JV_API_RFCOMM_READ_EVT */
277 typedef struct {
278     BT_HDR          hdr;
279     UINT32          handle;
280     UINT32          req_id;
281     UINT8           *p_data;
282     UINT16          len;
283     tBTA_JV_RFC_CB  *p_cb;
284     tBTA_JV_PCB     *p_pcb;
285 } tBTA_JV_API_RFCOMM_READ;
286 
287 /* data type for BTA_JV_API_SET_PM_PROFILE_EVT */
288 typedef struct {
289     BT_HDR              hdr;
290     UINT32              handle;
291     tBTA_JV_PM_ID       app_id;
292     tBTA_JV_CONN_STATE  init_st;
293 } tBTA_JV_API_SET_PM_PROFILE;
294 
295 /* data type for BTA_JV_API_PM_STATE_CHANGE_EVT */
296 typedef struct {
297     BT_HDR              hdr;
298     tBTA_JV_PM_CB       *p_cb;
299     tBTA_JV_CONN_STATE  state;
300 } tBTA_JV_API_PM_STATE_CHANGE;
301 
302 /* data type for BTA_JV_API_RFCOMM_WRITE_EVT */
303 typedef struct {
304     BT_HDR          hdr;
305     UINT32          handle;
306     UINT32          req_id;
307     UINT8           *p_data;
308     int             len;
309     tBTA_JV_RFC_CB  *p_cb;
310     tBTA_JV_PCB     *p_pcb;
311 } tBTA_JV_API_RFCOMM_WRITE;
312 
313 /* data type for BTA_JV_API_RFCOMM_FLOW_CONTROL_EVT */
314 typedef struct {
315     BT_HDR      hdr;
316     tBTA_JV_RFC_CB  *p_cb;
317     tBTA_JV_PCB     *p_pcb;
318     UINT16      credits_given;
319 } tBTA_JV_API_RFCOMM_FLOW_CONTROL;
320 
321 /* data type for BTA_JV_API_RFCOMM_CLOSE_EVT */
322 typedef struct {
323     BT_HDR          hdr;
324     UINT32          handle;
325     tBTA_JV_RFC_CB  *p_cb;
326     tBTA_JV_PCB     *p_pcb;
327     tBTA_JV_RFCOMM_CBACK *p_cback;
328     void            *user_data;
329 } tBTA_JV_API_RFCOMM_CLOSE;
330 
331 /* data type for BTA_JV_API_CREATE_RECORD_EVT */
332 typedef struct {
333     BT_HDR      hdr;
334 #define ESP_SDP_SERVER_NAME_MAX (32)
335     char name[ESP_SDP_SERVER_NAME_MAX + 1];
336     INT32       channel;
337     void        *user_data;
338 } tBTA_JV_API_CREATE_RECORD;
339 
340 /* data type for BTA_JV_API_ADD_ATTRIBUTE_EVT */
341 typedef struct {
342     BT_HDR      hdr;
343     UINT32      handle;
344     UINT16      attr_id;
345     UINT8       *p_value;
346     INT32       value_size;
347 } tBTA_JV_API_ADD_ATTRIBUTE;
348 
349 /* data type for BTA_JV_API_FREE_SCN_EVT */
350 typedef struct {
351     BT_HDR      hdr;
352     INT32       type;       /* One of BTA_JV_CONN_TYPE_ */
353     UINT16      scn;
354     tBTA_JV_RFCOMM_CBACK *p_cback;
355     void        *user_data;
356 } tBTA_JV_API_FREE_CHANNEL;
357 
358 /* data type for BTA_JV_API_ALLOC_CHANNEL_EVT */
359 typedef struct {
360     BT_HDR      hdr;
361     INT32       type;       /* One of BTA_JV_CONN_TYPE_ */
362     INT32       channel;    /* optionally request a specific channel */
363     void        *user_data;
364 } tBTA_JV_API_ALLOC_CHANNEL;
365 /* union of all data types */
366 typedef union {
367     /* GKI event buffer header */
368     BT_HDR                          hdr;
369     tBTA_JV_API_ENABLE              enable;
370     tBTA_JV_API_DISABLE             disable;
371     tBTA_JV_API_START_DISCOVERY     start_discovery;
372     tBTA_JV_API_ALLOC_CHANNEL       alloc_channel;
373     tBTA_JV_API_FREE_CHANNEL        free_channel;
374     tBTA_JV_API_CREATE_RECORD       create_record;
375     tBTA_JV_API_ADD_ATTRIBUTE       add_attr;
376 #if BTA_JV_L2CAP_INCLUDED
377     tBTA_JV_API_L2CAP_CONNECT       l2cap_connect;
378     tBTA_JV_API_L2CAP_READ          l2cap_read;
379     tBTA_JV_API_L2CAP_WRITE         l2cap_write;
380     tBTA_JV_API_L2CAP_CLOSE         l2cap_close;
381     tBTA_JV_API_L2CAP_SERVER        l2cap_server;
382     tBTA_JV_API_L2CAP_WRITE_FIXED   l2cap_write_fixed;
383 #endif /* BTA_JV_L2CAP_INCLUDED */
384     tBTA_JV_API_RFCOMM_CONNECT      rfcomm_connect;
385     tBTA_JV_API_RFCOMM_READ         rfcomm_read;
386     tBTA_JV_API_RFCOMM_WRITE        rfcomm_write;
387     tBTA_JV_API_RFCOMM_FLOW_CONTROL rfcomm_fc;
388     tBTA_JV_API_SET_PM_PROFILE      set_pm;
389     tBTA_JV_API_PM_STATE_CHANGE     change_pm_state;
390     tBTA_JV_API_RFCOMM_CLOSE        rfcomm_close;
391     tBTA_JV_API_RFCOMM_SERVER       rfcomm_server;
392 } tBTA_JV_MSG;
393 
394 /* JV control block */
395 typedef struct {
396     /* the SDP handle reported to JV user is the (index + 1) to sdp_handle[].
397      * if sdp_handle[i]==0, it's not used.
398      * otherwise sdp_handle[i] is the stack SDP handle. */
399     UINT32                  sdp_handle[BTA_JV_MAX_SDP_REC]; /* SDP records created */
400     UINT8                   *p_sel_raw_data;/* the raw data of last service select */
401     tBTA_JV_DM_CBACK        *p_dm_cback;
402 #if BTA_JV_L2CAP_INCLUDED
403     tBTA_JV_L2C_CB          l2c_cb[BTA_JV_MAX_L2C_CONN];    /* index is GAP handle (index) */
404 #endif /* BTA_JV_L2CAP_INCLUDED */
405     tBTA_JV_RFC_CB          rfc_cb[BTA_JV_MAX_RFC_CONN];
406     tBTA_JV_PCB             port_cb[MAX_RFC_PORTS];         /* index of this array is
407                                                                the port_handle, */
408     UINT8                   sec_id[BTA_JV_NUM_SERVICE_ID];  /* service ID */
409     BOOLEAN                 scn[BTA_JV_MAX_SCN];            /* SCN allocated by java */
410     UINT16                  free_psm_list[BTA_JV_MAX_L2C_CONN];  /* PSMs freed by java
411                                                                     (can be reused) */
412     UINT8                   sdp_active;                     /* see BTA_JV_SDP_ACT_* */
413     tSDP_UUID               uuid;                           /* current uuid of sdp discovery*/
414     tBTA_JV_PM_CB           pm_cb[BTA_JV_PM_MAX_NUM];       /* PM on a per JV handle bases */
415 } tBTA_JV_CB;
416 
417 enum {
418     BTA_JV_SDP_ACT_NONE = 0,
419     BTA_JV_SDP_ACT_YES,     /* waiting for SDP result */
420     BTA_JV_SDP_ACT_CANCEL   /* waiting for cancel complete */
421 };
422 
423 /* JV control block */
424 #if BTA_DYNAMIC_MEMORY == FALSE
425 extern tBTA_JV_CB bta_jv_cb;
426 #else
427 extern tBTA_JV_CB *bta_jv_cb_ptr;
428 #define bta_jv_cb (*bta_jv_cb_ptr)
429 #endif
430 
431 /* config struct */
432 extern tBTA_JV_CFG *p_bta_jv_cfg;
433 
434 extern BOOLEAN bta_jv_sm_execute(BT_HDR *p_msg);
435 
436 extern void bta_jv_enable (tBTA_JV_MSG *p_data);
437 extern void bta_jv_disable (tBTA_JV_MSG *p_data);
438 extern void bta_jv_get_channel_id (tBTA_JV_MSG *p_data);
439 extern void bta_jv_free_scn (tBTA_JV_MSG *p_data);
440 extern void bta_jv_start_discovery (tBTA_JV_MSG *p_data);
441 extern void bta_jv_create_record (tBTA_JV_MSG *p_data);
442 extern void bta_jv_delete_record (tBTA_JV_MSG *p_data);
443 #if BTA_JV_L2CAP_INCLUDED
444 extern void bta_jv_l2cap_connect (tBTA_JV_MSG *p_data);
445 extern void bta_jv_l2cap_close (tBTA_JV_MSG *p_data);
446 extern void bta_jv_l2cap_start_server (tBTA_JV_MSG *p_data);
447 extern void bta_jv_l2cap_stop_server (tBTA_JV_MSG *p_data);
448 extern void bta_jv_l2cap_read (tBTA_JV_MSG *p_data);
449 extern void bta_jv_l2cap_write (tBTA_JV_MSG *p_data);
450 #endif /* BTA_JV_L2CAP_INCLUDED */
451 extern void bta_jv_rfcomm_connect (tBTA_JV_MSG *p_data);
452 extern void bta_jv_rfcomm_close (tBTA_JV_MSG *p_data);
453 extern void bta_jv_rfcomm_start_server (tBTA_JV_MSG *p_data);
454 extern void bta_jv_rfcomm_stop_server (tBTA_JV_MSG *p_data);
455 extern void bta_jv_rfcomm_read (tBTA_JV_MSG *p_data);
456 extern void bta_jv_rfcomm_write (tBTA_JV_MSG *p_data);
457 extern void bta_jv_rfcomm_flow_control(tBTA_JV_MSG *p_data);
458 extern void bta_jv_set_pm_profile (tBTA_JV_MSG *p_data);
459 extern void bta_jv_change_pm_state(tBTA_JV_MSG *p_data);
460 #if BTA_JV_L2CAP_INCLUDED
461 extern void bta_jv_l2cap_connect_le (tBTA_JV_MSG *p_data);
462 extern void bta_jv_l2cap_start_server_le (tBTA_JV_MSG *p_data);
463 extern void bta_jv_l2cap_stop_server_le (tBTA_JV_MSG *p_data);
464 extern void bta_jv_l2cap_write_fixed (tBTA_JV_MSG *p_data);
465 extern void bta_jv_l2cap_close_fixed (tBTA_JV_MSG *p_data);
466 #endif /* BTA_JV_L2CAP_INCLUDED */
467 
468 #endif  ///defined BTA_JV_INCLUDED && BTA_JV_INCLUDED == TRUE
469 #endif /* BTA_JV_INT_H */
470