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