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