1/******************************************************************************
2 *
3 *  Copyright 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_api.h"
28#include "bta_jv_api.h"
29#include "bta_sys.h"
30#include "port_api.h"
31#include "rfcdefs.h"
32
33#include <memory>
34
35/*****************************************************************************
36 *  Constants
37 ****************************************************************************/
38
39#ifndef BTA_JV_RFC_EV_MASK
40#define BTA_JV_RFC_EV_MASK \
41  (PORT_EV_RXCHAR | PORT_EV_TXEMPTY | PORT_EV_FC | PORT_EV_FCS)
42#endif
43
44enum {
45  BTA_JV_PM_FREE_ST = 0, /* empty PM slot */
46  BTA_JV_PM_IDLE_ST,
47  BTA_JV_PM_BUSY_ST
48};
49
50/* BTA JV PM control block */
51typedef struct {
52  uint32_t handle;      /* The connection handle */
53  uint8_t state;        /* state: see above enum */
54  tBTA_JV_PM_ID app_id; /* JV app specific id indicating power table to use */
55  RawAddress peer_bd_addr; /* Peer BD address */
56} tBTA_JV_PM_CB;
57
58enum {
59  BTA_JV_ST_NONE = 0,
60  BTA_JV_ST_CL_OPENING,
61  BTA_JV_ST_CL_OPEN,
62  BTA_JV_ST_CL_CLOSING,
63  BTA_JV_ST_SR_LISTEN,
64  BTA_JV_ST_SR_OPEN,
65  BTA_JV_ST_SR_CLOSING
66};
67typedef uint8_t tBTA_JV_STATE;
68#define BTA_JV_ST_CL_MAX BTA_JV_ST_CL_CLOSING
69/* JV L2CAP control block */
70typedef struct {
71  tBTA_JV_L2CAP_CBACK* p_cback; /* the callback function */
72  uint16_t psm;                 /* the psm used for this server connection */
73  tBTA_JV_STATE state;          /* the state of this control block */
74  tBTA_SERVICE_ID sec_id;       /* service id */
75  uint32_t handle; /* the handle reported to java app (same as gap handle) */
76  bool cong;       /* true, if congested */
77  tBTA_JV_PM_CB* p_pm_cb; /* ptr to pm control block, NULL: unused */
78  uint32_t l2cap_socket_id;
79} tBTA_JV_L2C_CB;
80
81#define BTA_JV_RFC_HDL_MASK 0xFF
82#define BTA_JV_RFCOMM_MASK 0x80
83#define BTA_JV_ALL_APP_ID 0xFF
84#define BTA_JV_RFC_HDL_TO_SIDX(r) (((r)&0xFF00) >> 8)
85#define BTA_JV_RFC_H_S_TO_HDL(h, s) ((h) | ((s) << 8))
86
87/* port control block */
88typedef struct {
89  uint32_t handle;      /* the rfcomm session handle at jv */
90  uint16_t port_handle; /* port handle */
91  tBTA_JV_STATE state;  /* the state of this control block */
92  uint8_t max_sess;     /* max sessions */
93  uint32_t rfcomm_slot_id;
94  bool cong;              /* true, if congested */
95  tBTA_JV_PM_CB* p_pm_cb; /* ptr to pm control block, NULL: unused */
96} tBTA_JV_PCB;
97
98/* JV RFCOMM control block */
99typedef struct {
100  tBTA_JV_RFCOMM_CBACK* p_cback; /* the callback function */
101  uint16_t rfc_hdl[BTA_JV_MAX_RFC_SR_SESSION];
102  tBTA_SERVICE_ID sec_id; /* service id */
103  uint8_t handle;         /* index: the handle reported to java app */
104  uint8_t scn;            /* the scn of the server */
105  uint8_t max_sess;       /* max sessions */
106  int curr_sess;          /* current sessions count*/
107} tBTA_JV_RFC_CB;
108
109/* JV control block */
110typedef struct {
111  /* the SDP handle reported to JV user is the (index + 1) to sdp_handle[].
112   * if sdp_handle[i]==0, it's not used.
113   * otherwise sdp_handle[i] is the stack SDP handle. */
114  uint32_t sdp_handle[BTA_JV_MAX_SDP_REC]; /* SDP records created */
115  uint8_t* p_sel_raw_data; /* the raw data of last service select */
116  tBTA_JV_DM_CBACK* p_dm_cback;
117  tBTA_JV_L2C_CB l2c_cb[BTA_JV_MAX_L2C_CONN]; /* index is GAP handle (index) */
118  tBTA_JV_RFC_CB rfc_cb[BTA_JV_MAX_RFC_CONN];
119  tBTA_JV_PCB port_cb[MAX_RFC_PORTS];          /* index of this array is
120                                                  the port_handle, */
121  uint8_t sec_id[BTA_JV_NUM_SERVICE_ID];       /* service ID */
122  bool scn[BTA_JV_MAX_SCN];                    /* SCN allocated by java */
123  uint16_t free_psm_list[BTA_JV_MAX_L2C_CONN]; /* PSMs freed by java
124                                                (can be reused) */
125  uint8_t sdp_active;                          /* see BTA_JV_SDP_ACT_* */
126  bluetooth::Uuid uuid;                   /* current uuid of sdp discovery*/
127  tBTA_JV_PM_CB pm_cb[BTA_JV_PM_MAX_NUM]; /* PM on a per JV handle bases */
128} tBTA_JV_CB;
129
130enum {
131  BTA_JV_SDP_ACT_NONE = 0,
132  BTA_JV_SDP_ACT_YES,   /* waiting for SDP result */
133  BTA_JV_SDP_ACT_CANCEL /* waiting for cancel complete */
134};
135
136/* JV control block */
137extern tBTA_JV_CB bta_jv_cb;
138
139/* config struct */
140extern tBTA_JV_CFG* p_bta_jv_cfg;
141
142extern void bta_jv_enable(tBTA_JV_DM_CBACK* p_cback);
143extern void bta_jv_disable();
144extern void bta_jv_get_channel_id(int32_t type, int32_t channel,
145                                  uint32_t l2cap_socket_id,
146                                  uint32_t rfcomm_slot_id);
147extern void bta_jv_free_scn(int32_t type, uint16_t scn);
148extern void bta_jv_start_discovery(const RawAddress& bd_addr, uint16_t num_uuid,
149                                   bluetooth::Uuid* uuid_list,
150                                   uint32_t rfcomm_slot_id);
151extern void bta_jv_create_record(uint32_t rfcomm_slot_id);
152extern void bta_jv_delete_record(uint32_t handle);
153extern void bta_jv_l2cap_connect(int32_t type, tBTA_SEC sec_mask,
154                                 tBTA_JV_ROLE role, uint16_t remote_psm,
155                                 uint16_t rx_mtu,
156                                 const RawAddress& peer_bd_addr,
157                                 std::unique_ptr<tL2CAP_CFG_INFO> cfg,
158                                 std::unique_ptr<tL2CAP_ERTM_INFO> ertm_info,
159                                 tBTA_JV_L2CAP_CBACK* p_cback,
160                                 uint32_t l2cap_socket_id);
161extern void bta_jv_l2cap_close(uint32_t handle, tBTA_JV_L2C_CB* p_cb);
162extern void bta_jv_l2cap_start_server(
163    int32_t type, tBTA_SEC sec_mask, tBTA_JV_ROLE role, uint16_t local_psm,
164    uint16_t rx_mtu, std::unique_ptr<tL2CAP_CFG_INFO> cfg_param,
165    std::unique_ptr<tL2CAP_ERTM_INFO> ertm_info, tBTA_JV_L2CAP_CBACK* p_cback,
166    uint32_t l2cap_socket_id);
167extern void bta_jv_l2cap_stop_server(uint16_t local_psm,
168                                     uint32_t l2cap_socket_id);
169extern void bta_jv_l2cap_write(uint32_t handle, uint32_t req_id, BT_HDR* msg,
170                               uint32_t user_id, tBTA_JV_L2C_CB* p_cb);
171extern void bta_jv_rfcomm_connect(tBTA_SEC sec_mask, tBTA_JV_ROLE role,
172                                  uint8_t remote_scn,
173                                  const RawAddress& peer_bd_addr,
174                                  tBTA_JV_RFCOMM_CBACK* p_cback,
175                                  uint32_t rfcomm_slot_id);
176extern void bta_jv_rfcomm_close(uint32_t handle, uint32_t rfcomm_slot_id);
177extern void bta_jv_rfcomm_start_server(tBTA_SEC sec_mask, tBTA_JV_ROLE role,
178                                       uint8_t local_scn, uint8_t max_session,
179                                       tBTA_JV_RFCOMM_CBACK* p_cback,
180                                       uint32_t rfcomm_slot_id);
181extern void bta_jv_rfcomm_stop_server(uint32_t handle, uint32_t rfcomm_slot_id);
182extern void bta_jv_rfcomm_write(uint32_t handle, uint32_t req_id,
183                                tBTA_JV_RFC_CB* p_cb, tBTA_JV_PCB* p_pcb);
184extern void bta_jv_set_pm_profile(uint32_t handle, tBTA_JV_PM_ID app_id,
185                                  tBTA_JV_CONN_STATE init_st);
186extern void bta_jv_l2cap_connect_le(uint16_t remote_chan,
187                                    const RawAddress& peer_bd_addr,
188                                    tBTA_JV_L2CAP_CBACK* p_cback,
189                                    uint32_t l2cap_socket_id);
190extern void bta_jv_l2cap_start_server_le(uint16_t local_chan,
191                                         tBTA_JV_L2CAP_CBACK* p_cback,
192                                         uint32_t l2cap_socket_id);
193extern void bta_jv_l2cap_stop_server_le(uint16_t local_chan);
194extern void bta_jv_l2cap_write_fixed(uint16_t channel, const RawAddress& addr,
195                                     uint32_t req_id, BT_HDR* msg,
196                                     uint32_t user_id,
197                                     tBTA_JV_L2CAP_CBACK* p_cback);
198extern void bta_jv_l2cap_close_fixed(uint32_t handle);
199
200#endif /* BTA_JV_INT_H */
201