1 /******************************************************************************
2  *
3  *  Copyright (C) 2009-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 file contains interfaces which are internal to MCAP.
22  *
23  ******************************************************************************/
24 #ifndef MCA_INT_H
25 #define MCA_INT_H
26 #include "gki.h"
27 #include "mca_api.h"
28 
29 /*****************************************************************************
30 ** constants
31 *****************************************************************************/
32 
33 /* INT initiates the L2CAP channel */
34 #define MCA_ACP     0
35 #define MCA_INT     1
36 
37 /*****************************************************************************
38 **  Type Definitions
39 *****************************************************************************/
40 
41 /* Header structure for api/received request/response. */
42 typedef struct {
43     BT_HDR          hdr;        /* layer specific information */
44     UINT8           op_code;    /* the request/response opcode */
45     UINT8           rsp_code;   /* valid only if op_code is a response */
46     UINT16          mdl_id;     /* the MDL ID associated with this request/response */
47     UINT8           param;      /* other parameter */
48     UINT8           mdep_id;    /* the MDEP ID associated with this request/response */
49     /* tMCA_HANDLE     rcb_idx;    For internal use only */
50     /* tMCA_CL         ccb_idx;    For internal use only */
51     tMCA_DL         dcb_idx;    /* For internal use only */
52 } tMCA_CCB_MSG;
53 
54 /* This data structure is associated with the AVDT_OPEN_IND_EVT and AVDT_OPEN_CFM_EVT. */
55 typedef struct {
56     BT_HDR          hdr;                /* Event header */
57     UINT16          peer_mtu;           /* Transport channel L2CAP MTU of the peer */
58     UINT16          lcid;               /* L2CAP LCID  */
59     UINT8           param;
60 } tMCA_OPEN;
61 
62 typedef struct {
63     UINT16          reason;     /* disconnect reason from L2CAP */
64     UINT8           param;      /* MCA_INT or MCA_ACP */
65     UINT16          lcid;       /* L2CAP LCID  */
66 } tMCA_CLOSE;
67 
68 /* Header structure for state machine event parameters. */
69 typedef union {
70     BT_HDR          hdr;        /* layer specific information */
71     tMCA_CCB_MSG    api;
72     BOOLEAN         llcong;
73     UINT8           param;
74     tMCA_OPEN       open;
75     tMCA_CLOSE      close;
76 } tMCA_CCB_EVT;
77 
78 /* control channel states */
79 enum
80 {
81     MCA_CCB_NULL_ST,        /* not allocated */
82     MCA_CCB_OPENING_ST,
83     MCA_CCB_OPEN_ST,        /* open */
84     MCA_CCB_CLOSING_ST,		/* disconnecting */
85     MCA_CCB_MAX_ST
86 };
87 typedef UINT8 tMCA_CCB_STATE;
88 
89 /* control channel events */
90 enum
91 {
92     MCA_CCB_API_CONNECT_EVT,    /* application initiates a connect request. */
93     MCA_CCB_API_DISCONNECT_EVT, /* application initiates a disconnect request. */
94     MCA_CCB_API_REQ_EVT,        /* application initiates a request. The request may be create_mdl, delete_mdl, reconnect_mdl or abort_mdl. */
95     MCA_CCB_API_RSP_EVT,        /* application initiates a create_mdl or reconnect_mdl response. */
96     MCA_CCB_MSG_REQ_EVT,        /* a create_mdl, delete_mdl, reconnect_mdl or abort_mdl request message is received from the peer. */
97     MCA_CCB_MSG_RSP_EVT,        /* Response received event.  This event is sent whenever a response message is received for an outstanding request message. */
98     MCA_CCB_DL_OPEN_EVT,        /* data channel open. */
99     MCA_CCB_LL_OPEN_EVT,        /* Lower layer open.  This event is sent when the lower layer channel is open.  */
100     MCA_CCB_LL_CLOSE_EVT,       /* Lower layer close.  This event is sent when the lower layer channel is closed. */
101     MCA_CCB_LL_CONG_EVT,        /* Lower layer congestion.  This event is sent when the lower layer is congested. */
102     MCA_CCB_RSP_TOUT_EVT        /* time out for waiting the message response on the control channel */
103 };
104 
105 /* Header structure for callback event parameters. */
106 typedef union {
107     tMCA_OPEN       open;
108     tMCA_CLOSE      close;
109     BT_HDR          hdr;        /* layer specific information */
110     BT_HDR          *p_pkt;
111     BOOLEAN         llcong;
112     UINT16          mdl_id;     /* the MDL ID associated with this request/response */
113     /* tMCA_HANDLE     rcb_idx;    For internal use only */
114     /* tMCA_CL         ccb_idx;    For internal use only */
115     /* tMCA_DL         dcb_idx;    For internal use only */
116 } tMCA_DCB_EVT;
117 
118 /* data channel states */
119 enum
120 {
121     MCA_DCB_NULL_ST,        /* not allocated */
122     MCA_DCB_OPENING_ST,     /* create/reconnect sequence is successful, waiting for data channel connection */
123     MCA_DCB_OPEN_ST,        /* open */
124     MCA_DCB_CLOSING_ST,     /* disconnecting */
125     MCA_DCB_MAX_ST
126 };
127 typedef UINT8 tMCA_DCB_STATE;
128 
129 /* data channel events */
130 enum
131 {
132     MCA_DCB_API_CLOSE_EVT,      /* This event is sent when the application wants to disconnect the data channel.*/
133     MCA_DCB_API_WRITE_EVT,      /* This event is sent when the application wants to send a data packet to the peer.*/
134     MCA_DCB_TC_OPEN_EVT,        /* Transport Channel open.  This event is sent when the channel is open.*/
135     MCA_DCB_TC_CLOSE_EVT,       /* Transport Channel close.*/
136     MCA_DCB_TC_CONG_EVT,        /* Transport Channel congestion status.*/
137     MCA_DCB_TC_DATA_EVT         /* This event is sent when a data packet is received from the peer.*/
138 };
139 
140 
141 
142 
143 /* "states" used in transport channel table */
144 #define MCA_TC_ST_UNUSED   0       /* Unused - unallocated */
145 #define MCA_TC_ST_IDLE     1       /* No connection */
146 #define MCA_TC_ST_ACP      2       /* Waiting to accept a connection */
147 #define MCA_TC_ST_INT      3       /* Initiating a connection */
148 #define MCA_TC_ST_CONN     4       /* Waiting for connection confirm */
149 #define MCA_TC_ST_CFG      5       /* Waiting for configuration complete */
150 #define MCA_TC_ST_OPEN     6       /* Channel opened */
151 #define MCA_TC_ST_SEC_INT  7       /* Security process as INT */
152 #define MCA_TC_ST_SEC_ACP  8       /* Security process as ACP */
153 
154 /* Configuration flags. tMCA_TC_TBL.cfg_flags */
155 #define MCA_L2C_CFG_IND_DONE   (1<<0)
156 #define MCA_L2C_CFG_CFM_DONE   (1<<1)
157 #define MCA_L2C_CFG_CONN_INT   (1<<2)
158 #define MCA_L2C_CFG_CONN_ACP   (1<<3)
159 #define MCA_L2C_CFG_DISCN_INT  (1<<4)
160 #define MCA_L2C_CFG_DISCN_ACP  (1<<5)
161 
162 
163 #define MCA_CTRL_TCID       0   /* to identify control channel by tMCA_TC_TBL.tcid */
164 
165 /* transport channel table */
166 typedef struct {
167     UINT16  peer_mtu;       /* L2CAP mtu of the peer device */
168     UINT16  my_mtu;         /* Our MTU for this channel */
169     UINT16  lcid;           /* L2CAP LCID */
170     UINT8   tcid;           /* transport channel id (0, for control channel. (MDEP ID + 1) for data channel) */
171     tMCA_DL cb_idx;         /* 1-based index to ccb or dcb */
172     UINT8   state;          /* transport channel state */
173     UINT8   cfg_flags;      /* L2CAP configuration flags */
174     UINT8   id;             /* L2CAP id sent by peer device (need this to handle security pending) */
175 } tMCA_TC_TBL;
176 
177 /* transport control block */
178 typedef struct {
179     tMCA_TC_TBL     tc_tbl[MCA_NUM_TC_TBL];
180     UINT8           lcid_tbl[MAX_L2CAP_CHANNELS];   /* map LCID to tc_tbl index */
181 } tMCA_TC;
182 
183 /* registration control block */
184 typedef struct {
185     tMCA_REG        reg;                /* the parameter at register */
186     tMCA_CS         dep[MCA_NUM_DEPS];  /* the registration info for each MDEP */
187     tMCA_CTRL_CBACK *p_cback;           /* control callback function */
188 } tMCA_RCB;
189 
190 enum
191 {
192     MCA_CCB_STAT_NORM,      /* normal operation (based on ccb state) */
193     MCA_CCB_STAT_PENDING,   /* waiting for data channel  */
194     MCA_CCB_STAT_RECONN,    /* reinitiate connection after transitioning from CLOSING to IDLE state  */
195     MCA_CCB_STAT_DISC       /* MCA_DisconnectReq or MCA_Deregister is called. waiting for all associated CL and DL to detach */
196 };
197 typedef UINT8 tMCA_CCB_STAT;
198 
199 /* control channel control block */
200 /* the ccbs association with the rcbs
201  * ccb[0]              ...ccb[MCA_NUM_LINKS*1-1] -> rcb[0]
202  * ccb[MCA_NUM_LINKS*1]...ccb[MCA_NUM_LINKS*2-1] -> rcb[1]
203  * ccb[MCA_NUM_LINKS*2]...ccb[MCA_NUM_LINKS*3-1] -> rcb[2]
204  */
205 typedef struct {
206     tMCA_RCB        *p_rcb;             /* the associated registration control block */
207     TIMER_LIST_ENT  timer_entry;        /* CCB timer list entry */
208     tMCA_CCB_MSG    *p_tx_req;          /* Current request being sent/awaiting response */
209     tMCA_CCB_MSG    *p_rx_msg;          /* Current message received/being processed */
210     BD_ADDR         peer_addr;          /* BD address of peer */
211     UINT16          sec_mask;           /* Security mask for connections as initiator */
212     UINT16          ctrl_vpsm;          /* The virtual PSM that peer is listening for control channel */
213     UINT16          data_vpsm;          /* The virtual PSM that peer is listening for data channel. */
214     UINT16          lcid;               /* L2CAP lcid for this control channel */
215     UINT8           state;              /* The CCB state machine state */
216     BOOLEAN         cong;               /* Whether control channel is congested */
217     tMCA_CCB_STAT   status;             /* see tMCA_CCB_STAT */
218 } tMCA_CCB;
219 typedef void (*tMCA_CCB_ACTION)(tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data);
220 
221 enum
222 {
223     MCA_DCB_STAT_NORM,      /* normal operation (based on dcb state) */
224     MCA_DCB_STAT_DEL,       /* MCA_Delete is called. waiting for the DL to detach */
225     MCA_DCB_STAT_DISC       /* MCA_CloseReq is called. waiting for the DL to detach */
226 };
227 typedef UINT8 tMCA_DCB_STAT;
228 
229 /* data channel control block */
230 /* the dcbs association with the ccbs
231  * dcb[0]             ...dcb[MCA_NUM_MDLS*1-1] -> ccb[0]
232  * dcb[MCA_NUM_MDLS*1]...dcb[MCA_NUM_MDLS*2-1] -> ccb[1]
233  * dcb[MCA_NUM_MDLS*2]...dcb[MCA_NUM_MDLS*3-1] -> ccb[2]
234  *
235  * the dcbs association with the rcbs
236  * dcb[0]                             ...dcb[MCA_NUM_MDLS*1*MCA_NUM_LINKS*1-1] -> rcb[0]
237  * dcb[MCA_NUM_MDLS*1*MCA_NUM_LINKS*1]...dcb[MCA_NUM_MDLS*2*MCA_NUM_LINKS*2-1] -> rcb[1]
238  * dcb[MCA_NUM_MDLS*2*MCA_NUM_LINKS*2]...dcb[MCA_NUM_MDLS*3*MCA_NUM_LINKS*3-1] -> rcb[2]
239  */
240 typedef struct {
241     tMCA_CCB            *p_ccb;         /* the associated control control block */
242     BT_HDR              *p_data;        /* data packet held due to L2CAP channel congestion */
243     tMCA_CS             *p_cs;          /* the associated MDEP info. p_cs->type is the mdep id(internal use) */
244     const tMCA_CHNL_CFG *p_chnl_cfg;    /* cfg params for L2CAP channel */
245     UINT16              mdl_id;         /* the MDL ID for this data channel */
246     UINT16              lcid;           /* L2CAP lcid */
247     UINT8               state;          /* The DCB state machine state */
248     BOOLEAN             cong;           /* Whether data channel is congested */
249     tMCA_DCB_STAT       status;         /* see tMCA_DCB_STAT */
250 } tMCA_DCB;
251 
252 typedef void (*tMCA_DCB_ACTION)(tMCA_DCB *p_ccb, tMCA_DCB_EVT *p_data);
253 
254 /* Control block for MCA */
255 typedef struct {
256     tMCA_RCB        rcb[MCA_NUM_REGS];  /* registration control block */
257     tMCA_CCB        ccb[MCA_NUM_CCBS];  /* control channel control blocks */
258     tMCA_DCB        dcb[MCA_NUM_DCBS];  /* data channel control blocks */
259     tMCA_TC         tc;                 /* transport control block */
260     UINT8           trace_level;        /* trace level */
261 } tMCA_CB;
262 
263 /* csm functions */
264 extern void mca_ccb_event(tMCA_CCB *p_ccb, UINT8 event, tMCA_CCB_EVT *p_data);
265 extern tMCA_CCB *mca_ccb_by_bd(tMCA_HANDLE handle, BD_ADDR bd_addr);
266 extern tMCA_CCB *mca_ccb_alloc(tMCA_HANDLE handle, BD_ADDR bd_addr);
267 extern void mca_ccb_rsp_tout(tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data);
268 extern void mca_ccb_dealloc(tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data);
269 extern tMCA_CL mca_ccb_to_hdl(tMCA_CCB *p_ccb);
270 extern tMCA_CCB *mca_ccb_by_hdl(tMCA_CL mcl);
271 extern BOOLEAN mca_ccb_uses_mdl_id(tMCA_CCB *p_ccb, UINT16 mdl_id);
272 
273 /* cact functions */
274 extern void mca_ccb_report_event(tMCA_CCB *p_ccb, UINT8 event, tMCA_CTRL *p_data);
275 extern void mca_ccb_free_msg(tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data);
276 extern void mca_ccb_snd_req(tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data);
277 extern void mca_ccb_snd_rsp(tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data);
278 extern void mca_ccb_do_disconn (tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data);
279 extern void mca_ccb_cong(tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data);
280 extern void mca_ccb_hdl_req(tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data);
281 extern void mca_ccb_hdl_rsp(tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data);
282 extern void mca_ccb_ll_open (tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data);
283 extern void mca_ccb_dl_open (tMCA_CCB *p_ccb, tMCA_CCB_EVT *p_data);
284 
285 /* dsm functions */
286 extern void mca_dcb_event(tMCA_DCB *p_dcb, UINT8 event, tMCA_DCB_EVT *p_data);
287 extern tMCA_DCB *mca_dcb_alloc(tMCA_CCB*p_ccb, tMCA_DEP dep);
288 extern UINT8 mca_dep_free_mdl(tMCA_CCB*p_ccb, tMCA_DEP dep);
289 extern void mca_dcb_dealloc(tMCA_DCB *p_dcb, tMCA_DCB_EVT *p_data);
290 extern tMCA_DL mca_dcb_to_hdl(tMCA_DCB *p_dcb);
291 extern tMCA_DCB *mca_dcb_by_hdl(tMCA_DL hdl);
292 extern void mca_dcb_close_by_mdl_id(tMCA_CCB*p_ccb, UINT16 mdl_id);
293 
294 /* dact functions */
295 extern void mca_dcb_tc_open (tMCA_DCB *p_dcb, tMCA_DCB_EVT *p_data);
296 extern void mca_dcb_cong (tMCA_DCB *p_dcb, tMCA_DCB_EVT *p_data);
297 extern void mca_dcb_free_data (tMCA_DCB *p_dcb, tMCA_DCB_EVT *p_data);
298 extern void mca_dcb_do_disconn (tMCA_DCB *p_dcb, tMCA_DCB_EVT *p_data);
299 extern void mca_dcb_snd_data (tMCA_DCB *p_dcb, tMCA_DCB_EVT *p_data);
300 extern void mca_dcb_hdl_data (tMCA_DCB *p_dcb, tMCA_DCB_EVT *p_data);
301 
302 
303 /* main/utils functions */
304 extern tMCA_HANDLE mca_handle_by_cpsm(UINT16 psm);
305 extern tMCA_HANDLE mca_handle_by_dpsm(UINT16 psm);
306 extern tMCA_TC_TBL * mca_tc_tbl_calloc(tMCA_CCB *p_ccb);
307 extern tMCA_TC_TBL * mca_tc_tbl_dalloc(tMCA_DCB *p_dcb);
308 extern tMCA_TC_TBL * mca_tc_tbl_by_lcid(UINT16 lcid);
309 extern void mca_free_tc_tbl_by_lcid(UINT16 lcid);
310 extern void mca_set_cfg_by_tbl(tL2CAP_CFG_INFO *p_cfg, tMCA_TC_TBL *p_tbl);
311 extern void mca_tc_close_ind(tMCA_TC_TBL *p_tbl, UINT16 reason);
312 extern void mca_tc_open_ind(tMCA_TC_TBL *p_tbl);
313 extern void mca_tc_cong_ind(tMCA_TC_TBL *p_tbl, BOOLEAN is_congested);
314 extern void mca_tc_data_ind(tMCA_TC_TBL *p_tbl, BT_HDR *p_buf);
315 extern tMCA_RCB * mca_rcb_alloc(tMCA_REG *p_reg);
316 extern void mca_rcb_dealloc(tMCA_HANDLE handle);
317 extern tMCA_HANDLE mca_rcb_to_handle(tMCA_RCB *p_rcb);
318 extern tMCA_RCB *mca_rcb_by_handle(tMCA_HANDLE handle);
319 extern BOOLEAN mca_is_valid_dep_id(tMCA_RCB *p_rcb, tMCA_DEP dep);
320 extern void mca_free_buf(void **p_buf);
321 extern void mca_process_timeout(TIMER_LIST_ENT *p_tle);
322 extern void mca_stop_timer(tMCA_CCB *p_ccb);
323 
324 /* l2c functions */
325 extern UINT16 mca_l2c_open_req(BD_ADDR bd_addr, UINT16 PSM, const tMCA_CHNL_CFG *p_chnl_cfg);
326 
327 /* callback function declarations */
328 extern void mca_l2c_cconn_ind_cback(BD_ADDR bd_addr, UINT16 lcid, UINT16 psm, UINT8 id);
329 extern void mca_l2c_dconn_ind_cback(BD_ADDR bd_addr, UINT16 lcid, UINT16 psm, UINT8 id);
330 extern void mca_l2c_connect_cfm_cback(UINT16 lcid, UINT16 result);
331 extern void mca_l2c_config_cfm_cback(UINT16 lcid, tL2CAP_CFG_INFO *p_cfg);
332 extern void mca_l2c_config_ind_cback(UINT16 lcid, tL2CAP_CFG_INFO *p_cfg);
333 extern void mca_l2c_disconnect_ind_cback(UINT16 lcid, BOOLEAN ack_needed);
334 extern void mca_l2c_disconnect_cfm_cback(UINT16 lcid, UINT16 result);
335 extern void mca_l2c_congestion_ind_cback(UINT16 lcid, BOOLEAN is_congested);
336 extern void mca_l2c_data_ind_cback(UINT16 lcid, BT_HDR *p_buf);
337 
338 /*****************************************************************************
339 ** global data
340 *****************************************************************************/
341 #ifdef __cplusplus
342 extern "C"
343 {
344 #endif
345 
346 /******************************************************************************
347 ** Main Control Block
348 *******************************************************************************/
349 #if MCA_DYNAMIC_MEMORY == FALSE
350 extern tMCA_CB  mca_cb;
351 #else
352 extern tMCA_CB *mca_cb_ptr;
353 #define mca_cb (*mca_cb_ptr)
354 #endif
355 
356 /* L2CAP callback registration structure */
357 extern const tL2CAP_APPL_INFO mca_l2c_int_appl;
358 extern const tL2CAP_FCR_OPTS mca_l2c_fcr_opts_def;
359 extern const UINT8 mca_std_msg_len[];
360 
361 #ifdef __cplusplus
362 }
363 #endif
364 
365 #endif /* MCA_INT_H */
366