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