]> granicus.if.org Git - esp-idf/blob - components/bt/bluedroid/stack/l2cap/include/l2c_int.h
Merge branch 'bugfix/vfs_write_return_type' into 'master'
[esp-idf] / components / bt / bluedroid / stack / l2cap / include / l2c_int.h
1 /******************************************************************************
2  *
3  *  Copyright (C) 1999-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 L2CAP internal definitions
22  *
23  ******************************************************************************/
24 #ifndef L2C_INT_H
25 #define L2C_INT_H
26
27 #include <stdbool.h>
28
29 #include "btm_api.h"
30 #include "gki.h"
31 #include "l2c_api.h"
32 #include "l2cdefs.h"
33 #include "list.h"
34
35 #define L2CAP_MIN_MTU   48      /* Minimum acceptable MTU is 48 bytes */
36
37 /* Timeouts. Since L2CAP works off a 1-second list, all are in seconds.
38 */
39 #define L2CAP_LINK_ROLE_SWITCH_TOUT  10           /* 10 seconds */
40 #define L2CAP_LINK_CONNECT_TOUT      60           /* 30 seconds */
41 #define L2CAP_LINK_CONNECT_TOUT_EXT  120          /* 120 seconds */
42 #define L2CAP_ECHO_RSP_TOUT          30           /* 30 seconds */
43 #define L2CAP_LINK_FLOW_CONTROL_TOUT 2            /* 2  seconds */
44 #define L2CAP_LINK_DISCONNECT_TOUT   30           /* 30 seconds */
45
46 #ifndef L2CAP_CHNL_CONNECT_TOUT      /* BTIF needs to override for internal project needs */
47 #define L2CAP_CHNL_CONNECT_TOUT      60           /* 60 seconds */
48 #endif
49
50 #define L2CAP_CHNL_CONNECT_TOUT_EXT  120          /* 120 seconds */
51 #define L2CAP_CHNL_CFG_TIMEOUT       30           /* 30 seconds */
52 #define L2CAP_CHNL_DISCONNECT_TOUT   10           /* 10 seconds */
53 #define L2CAP_DELAY_CHECK_SM4        2            /* 2 seconds */
54 #define L2CAP_WAIT_INFO_RSP_TOUT     3            /* 3 seconds */
55 #define L2CAP_WAIT_UNPARK_TOUT       2            /* 2 seconds */
56 #define L2CAP_LINK_INFO_RESP_TOUT    2            /* 2 seconds */
57 #define L2CAP_UPDATE_CONN_PARAM_TOUT 6            /* 6 seconds */
58 #define L2CAP_BLE_LINK_CONNECT_TOUT  30           /* 30 seconds */
59 #define L2CAP_BLE_CONN_PARAM_UPD_TOUT   30        /* 30 seconds */
60
61 /* quick timer uses millisecond unit */
62 #define L2CAP_DEFAULT_RETRANS_TOUT   2000         /* 2000 milliseconds */
63 #define L2CAP_DEFAULT_MONITOR_TOUT   12000        /* 12000 milliseconds */
64 #define L2CAP_FCR_ACK_TOUT           200          /* 200 milliseconds */
65
66 /* Define the possible L2CAP channel states. The names of
67 ** the states may seem a bit strange, but they are taken from
68 ** the Bluetooth specification.
69 */
70 typedef enum {
71     CST_CLOSED,                           /* Channel is in clodes state           */
72     CST_ORIG_W4_SEC_COMP,                 /* Originator waits security clearence  */
73     CST_TERM_W4_SEC_COMP,                 /* Acceptor waits security clearence    */
74     CST_W4_L2CAP_CONNECT_RSP,             /* Waiting for peer conenct response    */
75     CST_W4_L2CA_CONNECT_RSP,              /* Waiting for upper layer connect rsp  */
76     CST_CONFIG,                           /* Negotiating configuration            */
77     CST_OPEN,                             /* Data transfer state                  */
78     CST_W4_L2CAP_DISCONNECT_RSP,          /* Waiting for peer disconnect rsp      */
79     CST_W4_L2CA_DISCONNECT_RSP            /* Waiting for upper layer disc rsp     */
80 } tL2C_CHNL_STATE;
81
82 /* Define the possible L2CAP link states
83 */
84 typedef enum {
85     LST_DISCONNECTED,
86     LST_CONNECT_HOLDING,
87     LST_CONNECTING_WAIT_SWITCH,
88     LST_CONNECTING,
89     LST_CONNECTED,
90     LST_DISCONNECTING
91 } tL2C_LINK_STATE;
92
93
94
95 /* Define input events to the L2CAP link and channel state machines. The names
96 ** of the events may seem a bit strange, but they are taken from
97 ** the Bluetooth specification.
98 */
99 #define L2CEVT_LP_CONNECT_CFM          0          /* Lower layer connect confirm          */
100 #define L2CEVT_LP_CONNECT_CFM_NEG      1          /* Lower layer connect confirm (failed) */
101 #define L2CEVT_LP_CONNECT_IND          2          /* Lower layer connect indication       */
102 #define L2CEVT_LP_DISCONNECT_IND       3          /* Lower layer disconnect indication    */
103 #define L2CEVT_LP_QOS_CFM              4          /* Lower layer QOS confirmation         */
104 #define L2CEVT_LP_QOS_CFM_NEG          5          /* Lower layer QOS confirmation (failed)*/
105 #define L2CEVT_LP_QOS_VIOLATION_IND    6          /* Lower layer QOS violation indication */
106
107 #define L2CEVT_SEC_COMP                7          /* Security cleared successfully        */
108 #define L2CEVT_SEC_COMP_NEG            8          /* Security procedure failed            */
109
110 #define L2CEVT_L2CAP_CONNECT_REQ      10          /* Peer connection request              */
111 #define L2CEVT_L2CAP_CONNECT_RSP      11          /* Peer connection response             */
112 #define L2CEVT_L2CAP_CONNECT_RSP_PND  12          /* Peer connection response pending     */
113 #define L2CEVT_L2CAP_CONNECT_RSP_NEG  13          /* Peer connection response (failed)    */
114 #define L2CEVT_L2CAP_CONFIG_REQ       14          /* Peer configuration request           */
115 #define L2CEVT_L2CAP_CONFIG_RSP       15          /* Peer configuration response          */
116 #define L2CEVT_L2CAP_CONFIG_RSP_NEG   16          /* Peer configuration response (failed) */
117 #define L2CEVT_L2CAP_DISCONNECT_REQ   17          /* Peer disconnect request              */
118 #define L2CEVT_L2CAP_DISCONNECT_RSP   18          /* Peer disconnect response             */
119 #define L2CEVT_L2CAP_INFO_RSP         19          /* Peer information response            */
120 #define L2CEVT_L2CAP_DATA             20          /* Peer data                            */
121
122 #define L2CEVT_L2CA_CONNECT_REQ       21          /* Upper layer connect request          */
123 #define L2CEVT_L2CA_CONNECT_RSP       22          /* Upper layer connect response         */
124 #define L2CEVT_L2CA_CONNECT_RSP_NEG   23          /* Upper layer connect response (failed)*/
125 #define L2CEVT_L2CA_CONFIG_REQ        24          /* Upper layer config request           */
126 #define L2CEVT_L2CA_CONFIG_RSP        25          /* Upper layer config response          */
127 #define L2CEVT_L2CA_CONFIG_RSP_NEG    26          /* Upper layer config response (failed) */
128 #define L2CEVT_L2CA_DISCONNECT_REQ    27          /* Upper layer disconnect request       */
129 #define L2CEVT_L2CA_DISCONNECT_RSP    28          /* Upper layer disconnect response      */
130 #define L2CEVT_L2CA_DATA_READ         29          /* Upper layer data read                */
131 #define L2CEVT_L2CA_DATA_WRITE        30          /* Upper layer data write               */
132 #define L2CEVT_L2CA_FLUSH_REQ         31          /* Upper layer flush                    */
133
134 #define L2CEVT_TIMEOUT                32          /* Timeout                              */
135 #define L2CEVT_SEC_RE_SEND_CMD        33          /* btm_sec has enough info to proceed   */
136
137 #define L2CEVT_ACK_TIMEOUT            34          /* RR delay timeout                     */
138
139
140 /* Bitmask to skip over Broadcom feature reserved (ID) to avoid sending two
141    successive ID values, '0' id only or both */
142 #define L2CAP_ADJ_BRCM_ID           0x1
143 #define L2CAP_ADJ_ZERO_ID           0x2
144 #define L2CAP_ADJ_ID                0x3
145
146 /* Return values for l2cu_process_peer_cfg_req() */
147 #define L2CAP_PEER_CFG_UNACCEPTABLE     0
148 #define L2CAP_PEER_CFG_OK               1
149 #define L2CAP_PEER_CFG_DISCONNECT       2
150
151 /* eL2CAP option constants */
152 #define L2CAP_MIN_RETRANS_TOUT          2000    /* Min retransmission timeout if no flush timeout or PBF */
153 #define L2CAP_MIN_MONITOR_TOUT          12000   /* Min monitor timeout if no flush timeout or PBF */
154
155 #define L2CAP_MAX_FCR_CFG_TRIES         2       /* Config attempts before disconnecting */
156
157 typedef uint8_t tL2C_BLE_FIXED_CHNLS_MASK;
158
159 typedef struct {
160     UINT8       next_tx_seq;                /* Next sequence number to be Tx'ed         */
161     UINT8       last_rx_ack;                /* Last sequence number ack'ed by the peer  */
162     UINT8       next_seq_expected;          /* Next peer sequence number expected       */
163     UINT8       last_ack_sent;              /* Last peer sequence number ack'ed         */
164     UINT8       num_tries;                  /* Number of retries to send a packet       */
165     UINT8       max_held_acks;              /* Max acks we can hold before sending      */
166
167     BOOLEAN     remote_busy;                /* TRUE if peer has flowed us off           */
168     BOOLEAN     local_busy;                 /* TRUE if we have flowed off the peer      */
169
170     BOOLEAN     rej_sent;                   /* Reject was sent                          */
171     BOOLEAN     srej_sent;                  /* Selective Reject was sent                */
172     BOOLEAN     wait_ack;                   /* Transmitter is waiting ack (poll sent)   */
173     BOOLEAN     rej_after_srej;             /* Send a REJ when SREJ clears              */
174
175     BOOLEAN     send_f_rsp;                 /* We need to send an F-bit response        */
176
177     UINT16      rx_sdu_len;                 /* Length of the SDU being received         */
178     BT_HDR      *p_rx_sdu;                  /* Buffer holding the SDU being received    */
179     BUFFER_Q    waiting_for_ack_q;          /* Buffers sent and waiting for peer to ack */
180     BUFFER_Q    srej_rcv_hold_q;            /* Buffers rcvd but held pending SREJ rsp   */
181     BUFFER_Q    retrans_q;                  /* Buffers being retransmitted              */
182
183     TIMER_LIST_ENT ack_timer;               /* Timer delaying RR                        */
184     TIMER_LIST_ENT mon_retrans_timer;       /* Timer Monitor or Retransmission          */
185
186 #if (L2CAP_ERTM_STATS == TRUE)
187     UINT32      connect_tick_count;         /* Time channel was established             */
188     UINT32      ertm_pkt_counts[2];         /* Packets sent and received                */
189     UINT32      ertm_byte_counts[2];        /* Bytes   sent and received                */
190     UINT32      s_frames_sent[4];           /* S-frames sent (RR, REJ, RNR, SREJ)       */
191     UINT32      s_frames_rcvd[4];           /* S-frames rcvd (RR, REJ, RNR, SREJ)       */
192     UINT32      xmit_window_closed;         /* # of times the xmit window was closed    */
193     UINT32      controller_idle;            /* # of times less than 2 packets in controller */
194     /* when the xmit window was closed          */
195     UINT32      pkts_retransmitted;         /* # of packets that were retransmitted     */
196     UINT32      retrans_touts;              /* # of retransmission timouts              */
197     UINT32      xmit_ack_touts;             /* # of xmit ack timouts                    */
198
199 #define L2CAP_ERTM_STATS_NUM_AVG 10
200 #define L2CAP_ERTM_STATS_AVG_NUM_SAMPLES 100
201     UINT32      ack_delay_avg_count;
202     UINT32      ack_delay_avg_index;
203     UINT32      throughput_start;
204     UINT32      throughput[L2CAP_ERTM_STATS_NUM_AVG];
205     UINT32      ack_delay_avg[L2CAP_ERTM_STATS_NUM_AVG];
206     UINT32      ack_delay_min[L2CAP_ERTM_STATS_NUM_AVG];
207     UINT32      ack_delay_max[L2CAP_ERTM_STATS_NUM_AVG];
208     UINT32      ack_q_count_avg[L2CAP_ERTM_STATS_NUM_AVG];
209     UINT32      ack_q_count_min[L2CAP_ERTM_STATS_NUM_AVG];
210     UINT32      ack_q_count_max[L2CAP_ERTM_STATS_NUM_AVG];
211 #endif
212 } tL2C_FCRB;
213
214
215 /* Define a registration control block. Every application (e.g. RFCOMM, SDP,
216 ** TCS etc) that registers with L2CAP is assigned one of these.
217 */
218 #if (L2CAP_UCD_INCLUDED == TRUE)
219 #define L2C_UCD_RCB_ID              0x00
220 #define L2C_UCD_STATE_UNUSED        0x00
221 #define L2C_UCD_STATE_W4_DATA       0x01
222 #define L2C_UCD_STATE_W4_RECEPTION  0x02
223 #define L2C_UCD_STATE_W4_MTU        0x04
224
225 typedef struct {
226     UINT8               state;
227     tL2CAP_UCD_CB_INFO  cb_info;
228 } tL2C_UCD_REG;
229 #endif
230
231 typedef struct {
232     BOOLEAN                 in_use;
233     UINT16                  psm;
234     UINT16                  real_psm;               /* This may be a dummy RCB for an o/b connection but */
235     /* this is the real PSM that we need to connect to   */
236 #if (L2CAP_UCD_INCLUDED == TRUE)
237     tL2C_UCD_REG            ucd;
238 #endif
239
240     tL2CAP_APPL_INFO        api;
241 } tL2C_RCB;
242
243
244 /* Define a channel control block (CCB). There may be many channel control blocks
245 ** between the same two Bluetooth devices (i.e. on the same link).
246 ** Each CCB has unique local and remote CIDs. All channel control blocks on
247 ** the same physical link and are chained together.
248 */
249 typedef struct t_l2c_ccb {
250     BOOLEAN             in_use;                 /* TRUE when in use, FALSE when not */
251     tL2C_CHNL_STATE     chnl_state;             /* Channel state                    */
252
253     struct t_l2c_ccb    *p_next_ccb;            /* Next CCB in the chain            */
254     struct t_l2c_ccb    *p_prev_ccb;            /* Previous CCB in the chain        */
255     struct t_l2c_linkcb *p_lcb;                 /* Link this CCB is assigned to     */
256
257     UINT16              local_cid;              /* Local CID                        */
258     UINT16              remote_cid;             /* Remote CID                       */
259
260     TIMER_LIST_ENT      timer_entry;            /* CCB Timer List Entry             */
261
262     tL2C_RCB            *p_rcb;                 /* Registration CB for this Channel */
263     bool                should_free_rcb;        /* True if RCB was allocated on the heap */
264
265 #define IB_CFG_DONE     0x01
266 #define OB_CFG_DONE     0x02
267 #define RECONFIG_FLAG   0x04                    /* True after initial configuration */
268 #define CFG_DONE_MASK   (IB_CFG_DONE | OB_CFG_DONE)
269
270     UINT8               config_done;            /* Configuration flag word         */
271     UINT8               local_id;               /* Transaction ID for local trans  */
272     UINT8               remote_id;              /* Transaction ID for local  */
273
274 #define CCB_FLAG_NO_RETRY       0x01            /* no more retry */
275 #define CCB_FLAG_SENT_PENDING   0x02            /* already sent pending response */
276     UINT8               flags;
277
278     tL2CAP_CFG_INFO     our_cfg;                /* Our saved configuration options    */
279     tL2CAP_CH_CFG_BITS  peer_cfg_bits;          /* Store what peer wants to configure */
280     tL2CAP_CFG_INFO     peer_cfg;               /* Peer's saved configuration options */
281
282     BUFFER_Q            xmit_hold_q;            /* Transmit data hold queue         */
283     BOOLEAN             cong_sent;              /* Set when congested status sent   */
284     UINT16              buff_quota;             /* Buffer quota before sending congestion   */
285
286     tL2CAP_CHNL_PRIORITY ccb_priority;          /* Channel priority                 */
287     tL2CAP_CHNL_DATA_RATE tx_data_rate;         /* Channel Tx data rate             */
288     tL2CAP_CHNL_DATA_RATE rx_data_rate;         /* Channel Rx data rate             */
289
290     /* Fields used for eL2CAP */
291     tL2CAP_ERTM_INFO    ertm_info;
292     tL2C_FCRB           fcrb;
293     UINT16              tx_mps;                 /* TX MPS adjusted based on current controller */
294     UINT16              max_rx_mtu;
295     UINT8               fcr_cfg_tries;          /* Max number of negotiation attempts */
296     BOOLEAN             peer_cfg_already_rejected; /* If mode rejected once, set to TRUE */
297     BOOLEAN             out_cfg_fcr_present;    /* TRUE if cfg response shoulkd include fcr options */
298
299 #define L2CAP_CFG_FCS_OUR   0x01                /* Our desired config FCS option */
300 #define L2CAP_CFG_FCS_PEER  0x02                /* Peer's desired config FCS option */
301 #define L2CAP_BYPASS_FCS    (L2CAP_CFG_FCS_OUR | L2CAP_CFG_FCS_PEER)
302     UINT8               bypass_fcs;
303
304 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
305     BOOLEAN             is_flushable;                   /* TRUE if channel is flushable     */
306 #endif
307
308 #if (L2CAP_NUM_FIXED_CHNLS > 0) || (L2CAP_UCD_INCLUDED == TRUE)
309     UINT16              fixed_chnl_idle_tout;   /* Idle timeout to use for the fixed channel       */
310 #endif
311     UINT16              tx_data_len;
312 } tL2C_CCB;
313
314 /***********************************************************************
315 ** Define a queue of linked CCBs.
316 */
317 typedef struct {
318     tL2C_CCB        *p_first_ccb;               /* The first channel in this queue */
319     tL2C_CCB        *p_last_ccb;                /* The last  channel in this queue */
320 } tL2C_CCB_Q;
321
322 #if (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE)
323
324 /* Round-Robin service for the same priority channels */
325 #define L2CAP_NUM_CHNL_PRIORITY     3           /* Total number of priority group (high, medium, low)*/
326 #define L2CAP_CHNL_PRIORITY_WEIGHT  5           /* weight per priority for burst transmission quota */
327 #define L2CAP_GET_PRIORITY_QUOTA(pri) ((L2CAP_NUM_CHNL_PRIORITY - (pri)) * L2CAP_CHNL_PRIORITY_WEIGHT)
328
329 /* CCBs within the same LCB are served in round robin with priority                       */
330 /* It will make sure that low priority channel (for example, HF signaling on RFCOMM)      */
331 /* can be sent to headset even if higher priority channel (for example, AV media channel) */
332 /* is congested.                                                                          */
333
334 typedef struct {
335     tL2C_CCB        *p_serve_ccb;               /* current serving ccb within priority group */
336     tL2C_CCB        *p_first_ccb;               /* first ccb of priority group */
337     UINT8           num_ccb;                    /* number of channels in priority group */
338     UINT8           quota;                      /* burst transmission quota */
339 } tL2C_RR_SERV;
340
341 #endif /* (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE) */
342
343 /* Define a link control block. There is one link control block between
344 ** this device and any other device (i.e. BD ADDR).
345 */
346 typedef struct t_l2c_linkcb {
347     BOOLEAN             in_use;                     /* TRUE when in use, FALSE when not */
348     tL2C_LINK_STATE     link_state;
349
350     TIMER_LIST_ENT      timer_entry;                /* Timer list entry for timeout evt */
351     UINT16              handle;                     /* The handle used with LM          */
352
353     tL2C_CCB_Q          ccb_queue;                  /* Queue of CCBs on this LCB        */
354
355     tL2C_CCB            *p_pending_ccb;             /* ccb of waiting channel during link disconnect */
356     TIMER_LIST_ENT      info_timer_entry;           /* Timer entry for info resp timeout evt */
357     TIMER_LIST_ENT      upda_con_timer;             /* Timer entry for update connection parametr */
358     BD_ADDR             remote_bd_addr;             /* The BD address of the remote     */
359
360     UINT8               link_role;                  /* Master or slave                  */
361     UINT8               id;
362     UINT8               cur_echo_id;                /* Current id value for echo request */
363     tL2CA_ECHO_RSP_CB   *p_echo_rsp_cb;             /* Echo response callback           */
364     UINT16              idle_timeout;               /* Idle timeout                     */
365     BOOLEAN             is_bonding;                 /* True - link active only for bonding */
366
367     UINT16              link_flush_tout;            /* Flush timeout used               */
368
369     UINT16              link_xmit_quota;            /* Num outstanding pkts allowed     */
370     UINT16              sent_not_acked;             /* Num packets sent but not acked   */
371
372     BOOLEAN             partial_segment_being_sent; /* Set TRUE when a partial segment  */
373     /* is being sent.                   */
374     BOOLEAN             w4_info_rsp;                /* TRUE when info request is active */
375     UINT8               info_rx_bits;               /* set 1 if received info type */
376     UINT32              peer_ext_fea;               /* Peer's extended features mask    */
377     list_t              *link_xmit_data_q;          /* Link transmit data buffer queue  */
378
379     UINT8               peer_chnl_mask[L2CAP_FIXED_CHNL_ARRAY_SIZE];
380 #if (L2CAP_UCD_INCLUDED == TRUE)
381     UINT16              ucd_mtu;                    /* peer MTU on UCD */
382     BUFFER_Q            ucd_out_sec_pending_q;      /* Security pending outgoing UCD packet  */
383     BUFFER_Q            ucd_in_sec_pending_q;       /* Security pending incoming UCD packet  */
384 #endif
385
386     BT_HDR              *p_hcit_rcv_acl;            /* Current HCIT ACL buf being rcvd  */
387     UINT16              idle_timeout_sv;            /* Save current Idle timeout        */
388     UINT8               acl_priority;               /* L2C_PRIORITY_NORMAL or L2C_PRIORITY_HIGH */
389     tL2CA_NOCP_CB       *p_nocp_cb;                 /* Num Cmpl pkts callback           */
390
391 #if (L2CAP_NUM_FIXED_CHNLS > 0)
392     tL2C_CCB            *p_fixed_ccbs[L2CAP_NUM_FIXED_CHNLS];
393     UINT16              disc_reason;
394 #endif
395
396     tBT_TRANSPORT       transport;
397 #if (BLE_INCLUDED == TRUE)
398     tBLE_ADDR_TYPE      ble_addr_type;
399     UINT16              tx_data_len;            /* tx data length used in data length extension */
400
401 #define L2C_BLE_CONN_UPDATE_DISABLE 0x1  /* disable update connection parameters */
402 #define L2C_BLE_NEW_CONN_PARAM      0x2  /* new connection parameter to be set */
403 #define L2C_BLE_UPDATE_PENDING      0x4  /* waiting for connection update finished */
404 #define L2C_BLE_NOT_DEFAULT_PARAM   0x8  /* not using default connection parameters */
405     UINT8               conn_update_mask;
406
407     UINT16              min_interval; /* parameters as requested by peripheral */
408     UINT16              max_interval;
409     UINT16              conn_int;
410     UINT16              latency;
411     UINT16              timeout;
412
413 #endif
414
415 #if (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE)
416     /* each priority group is limited burst transmission  */
417     /* round robin service for the same priority channels */
418     tL2C_RR_SERV        rr_serv[L2CAP_NUM_CHNL_PRIORITY];
419     UINT8               rr_pri;                             /* current serving priority group */
420 #endif
421
422 } tL2C_LCB;
423
424 /* Define the L2CAP control structure
425 */
426 typedef struct {
427     UINT8           l2cap_trace_level;
428     UINT16          controller_xmit_window;         /* Total ACL window for all links   */
429
430     UINT16          round_robin_quota;              /* Round-robin link quota           */
431     UINT16          round_robin_unacked;            /* Round-robin unacked              */
432     BOOLEAN         check_round_robin;              /* Do a round robin check           */
433
434     BOOLEAN         is_cong_cback_context;
435
436     tL2C_LCB        lcb_pool[MAX_L2CAP_LINKS];      /* Link Control Block pool          */
437     tL2C_CCB        ccb_pool[MAX_L2CAP_CHANNELS];   /* Channel Control Block pool       */
438     tL2C_RCB        rcb_pool[MAX_L2CAP_CLIENTS];    /* Registration info pool           */
439
440     tL2C_CCB        *p_free_ccb_first;              /* Pointer to first free CCB        */
441     tL2C_CCB        *p_free_ccb_last;               /* Pointer to last  free CCB        */
442
443     UINT8           desire_role;                    /* desire to be master/slave when accepting a connection */
444     BOOLEAN         disallow_switch;                /* FALSE, to allow switch at create conn */
445     UINT16          num_lm_acl_bufs;                /* # of ACL buffers on controller   */
446     UINT16          idle_timeout;                   /* Idle timeout                     */
447
448     list_t          *rcv_pending_q;                 /* Recv pending queue               */
449     TIMER_LIST_ENT  rcv_hold_tle;                   /* Timer list entry for rcv hold    */
450
451     tL2C_LCB        *p_cur_hcit_lcb;                /* Current HCI Transport buffer     */
452     UINT16          num_links_active;               /* Number of links active           */
453
454 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
455     UINT16          non_flushable_pbf;              /* L2CAP_PKT_START_NON_FLUSHABLE if controller supports */
456     /* Otherwise, L2CAP_PKT_START */
457     BOOLEAN         is_flush_active;                /* TRUE if an HCI_Enhanced_Flush has been sent */
458 #endif
459
460 #if L2CAP_CONFORMANCE_TESTING == TRUE
461     UINT32          test_info_resp;                 /* Conformance testing needs a dynamic response */
462 #endif
463
464 #if (L2CAP_NUM_FIXED_CHNLS > 0)
465     tL2CAP_FIXED_CHNL_REG   fixed_reg[L2CAP_NUM_FIXED_CHNLS];   /* Reg info for fixed channels */
466 #endif
467
468 #if (BLE_INCLUDED == TRUE)
469     UINT16                   num_ble_links_active;               /* Number of LE links active           */
470     BOOLEAN                  is_ble_connecting;
471     BD_ADDR                  ble_connecting_bda;
472     UINT16                   controller_le_xmit_window;         /* Total ACL window for all links   */
473     tL2C_BLE_FIXED_CHNLS_MASK l2c_ble_fixed_chnls_mask;         // LE fixed channels mask
474     UINT16                   num_lm_ble_bufs;                   /* # of ACL buffers on controller   */
475     UINT16                   ble_round_robin_quota;              /* Round-robin link quota           */
476     UINT16                   ble_round_robin_unacked;            /* Round-robin unacked              */
477     BOOLEAN                  ble_check_round_robin;              /* Do a round robin check           */
478 #endif
479
480     tL2CA_ECHO_DATA_CB      *p_echo_data_cb;                /* Echo data callback */
481
482 #if (defined(L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE) && (L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE == TRUE))
483     UINT16              high_pri_min_xmit_quota;    /* Minimum number of ACL credit for high priority link */
484 #endif /* (L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE == TRUE) */
485
486     UINT16          dyn_psm;
487 } tL2C_CB;
488
489
490
491 /* Define a structure that contains the information about a connection.
492 ** This structure is used to pass between functions, and not all the
493 ** fields will always be filled in.
494 */
495 typedef struct {
496     BD_ADDR         bd_addr;                        /* Remote BD address        */
497     UINT8           status;                         /* Connection status        */
498     UINT16          psm;                            /* PSM of the connection    */
499     UINT16          l2cap_result;                   /* L2CAP result             */
500     UINT16          l2cap_status;                   /* L2CAP status             */
501     UINT16          remote_cid;                     /* Remote CID               */
502 } tL2C_CONN_INFO;
503
504
505 typedef void (tL2C_FCR_MGMT_EVT_HDLR) (UINT8, tL2C_CCB *);
506
507 /* The offset in a buffer that L2CAP will use when building commands.
508 */
509 #define L2CAP_SEND_CMD_OFFSET       0
510
511
512 /* Number of ACL buffers to use for high priority channel
513 */
514 #if (!defined(L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE) || (L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE == FALSE))
515 #define L2CAP_HIGH_PRI_MIN_XMIT_QUOTA_A     (L2CAP_HIGH_PRI_MIN_XMIT_QUOTA)
516 #else
517 #define L2CAP_HIGH_PRI_MIN_XMIT_QUOTA_A     (l2cb.high_pri_min_xmit_quota)
518 #endif
519
520 #ifdef __cplusplus
521 extern "C" {
522 #endif
523
524
525 /* L2CAP global data
526 ************************************
527 */
528 #if (!defined L2C_DYNAMIC_MEMORY) || (L2C_DYNAMIC_MEMORY == FALSE)
529 extern tL2C_CB  l2cb;
530 #else
531 extern tL2C_CB *l2c_cb_ptr;
532 #define l2cb (*l2c_cb_ptr)
533 #endif
534
535
536 /* Functions provided by l2c_main.c
537 ************************************
538 */
539 void l2c_init(void);
540 void l2c_free(void);
541
542 extern void     l2c_process_timeout (TIMER_LIST_ENT *p_tle);
543 extern UINT8    l2c_data_write (UINT16 cid, BT_HDR *p_data, UINT16 flag);
544 extern void     l2c_rcv_acl_data (BT_HDR *p_msg);
545 extern void     l2c_process_held_packets (BOOLEAN timed_out);
546
547 /* Functions provided by l2c_utils.c
548 ************************************
549 */
550 extern tL2C_LCB *l2cu_allocate_lcb (BD_ADDR p_bd_addr, BOOLEAN is_bonding, tBT_TRANSPORT transport);
551 extern BOOLEAN  l2cu_start_post_bond_timer (UINT16 handle);
552 extern void     l2cu_release_lcb (tL2C_LCB *p_lcb);
553 extern tL2C_LCB *l2cu_find_lcb_by_bd_addr (BD_ADDR p_bd_addr, tBT_TRANSPORT transport);
554 extern tL2C_LCB *l2cu_find_lcb_by_handle (UINT16 handle);
555 extern void     l2cu_update_lcb_4_bonding (BD_ADDR p_bd_addr, BOOLEAN is_bonding);
556
557 extern UINT8    l2cu_get_conn_role (tL2C_LCB *p_this_lcb);
558 extern BOOLEAN  l2cu_set_acl_priority (BD_ADDR bd_addr, UINT8 priority, BOOLEAN reset_after_rs);
559
560 extern void     l2cu_enqueue_ccb (tL2C_CCB *p_ccb);
561 extern void     l2cu_dequeue_ccb (tL2C_CCB *p_ccb);
562 extern void     l2cu_change_pri_ccb (tL2C_CCB *p_ccb, tL2CAP_CHNL_PRIORITY priority);
563
564 extern tL2C_CCB *l2cu_allocate_ccb (tL2C_LCB *p_lcb, UINT16 cid);
565 extern void     l2cu_release_ccb (tL2C_CCB *p_ccb);
566 extern tL2C_CCB *l2cu_find_ccb_by_cid (tL2C_LCB *p_lcb, UINT16 local_cid);
567 extern tL2C_CCB *l2cu_find_ccb_by_remote_cid (tL2C_LCB *p_lcb, UINT16 remote_cid);
568 extern void     l2cu_adj_id (tL2C_LCB *p_lcb, UINT8 adj_mask);
569 extern BOOLEAN  l2c_is_cmd_rejected (UINT8 cmd_code, UINT8 id, tL2C_LCB *p_lcb);
570
571 extern void     l2cu_send_peer_cmd_reject (tL2C_LCB *p_lcb, UINT16 reason,
572         UINT8 rem_id, UINT16 p1, UINT16 p2);
573 extern void     l2cu_send_peer_connect_req (tL2C_CCB *p_ccb);
574 extern void     l2cu_send_peer_connect_rsp (tL2C_CCB *p_ccb, UINT16 result, UINT16 status);
575 extern void     l2cu_send_peer_config_req (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg);
576 extern void     l2cu_send_peer_config_rsp (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg);
577 extern void     l2cu_send_peer_config_rej (tL2C_CCB *p_ccb, UINT8 *p_data, UINT16 data_len, UINT16 rej_len);
578 extern void     l2cu_send_peer_disc_req (tL2C_CCB *p_ccb);
579 extern void     l2cu_send_peer_disc_rsp (tL2C_LCB *p_lcb, UINT8 remote_id, UINT16 local_cid, UINT16 remote_cid);
580 extern void     l2cu_send_peer_echo_req (tL2C_LCB *p_lcb, UINT8 *p_data, UINT16 data_len);
581 extern void     l2cu_send_peer_echo_rsp (tL2C_LCB *p_lcb, UINT8 id, UINT8 *p_data, UINT16 data_len);
582 extern void     l2cu_send_peer_info_rsp (tL2C_LCB *p_lcb, UINT8 id, UINT16 info_type);
583 extern void     l2cu_reject_connection (tL2C_LCB *p_lcb, UINT16 remote_cid, UINT8 rem_id, UINT16 result);
584 extern void     l2cu_send_peer_info_req (tL2C_LCB *p_lcb, UINT16 info_type);
585 extern void     l2cu_set_acl_hci_header (BT_HDR *p_buf, tL2C_CCB *p_ccb);
586 extern void     l2cu_check_channel_congestion (tL2C_CCB *p_ccb);
587 extern void     l2cu_disconnect_chnl (tL2C_CCB *p_ccb);
588
589 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
590 extern void     l2cu_set_non_flushable_pbf(BOOLEAN);
591 #endif
592
593 #if (BLE_INCLUDED == TRUE)
594 extern void l2cu_send_peer_ble_par_req (tL2C_LCB *p_lcb, UINT16 min_int, UINT16 max_int, UINT16 latency, UINT16 timeout);
595 extern void l2cu_send_peer_ble_par_rsp (tL2C_LCB *p_lcb, UINT16 reason, UINT8 rem_id);
596 #endif
597
598 extern BOOLEAN l2cu_initialize_fixed_ccb (tL2C_LCB *p_lcb, UINT16 fixed_cid, tL2CAP_FCR_OPTS *p_fcr);
599 extern void    l2cu_no_dynamic_ccbs (tL2C_LCB *p_lcb);
600 extern void    l2cu_process_fixed_chnl_resp (tL2C_LCB *p_lcb);
601
602 /* Functions provided by l2c_ucd.c
603 ************************************
604 */
605 #if (L2CAP_UCD_INCLUDED == TRUE)
606 void l2c_ucd_delete_sec_pending_q(tL2C_LCB  *p_lcb);
607 void l2c_ucd_enqueue_pending_out_sec_q(tL2C_CCB  *p_ccb, void *p_data);
608 BOOLEAN l2c_ucd_check_pending_info_req(tL2C_CCB  *p_ccb);
609 BOOLEAN l2c_ucd_check_pending_out_sec_q(tL2C_CCB  *p_ccb);
610 void l2c_ucd_send_pending_out_sec_q(tL2C_CCB  *p_ccb);
611 void l2c_ucd_discard_pending_out_sec_q(tL2C_CCB  *p_ccb);
612 BOOLEAN l2c_ucd_check_pending_in_sec_q(tL2C_CCB  *p_ccb);
613 void l2c_ucd_send_pending_in_sec_q(tL2C_CCB  *p_ccb);
614 void l2c_ucd_discard_pending_in_sec_q(tL2C_CCB  *p_ccb);
615 BOOLEAN l2c_ucd_check_rx_pkts(tL2C_LCB  *p_lcb, BT_HDR *p_msg);
616 BOOLEAN l2c_ucd_process_event(tL2C_CCB *p_ccb, UINT16 event, void *p_data);
617 #endif
618
619 #if (BLE_INCLUDED == TRUE)
620 extern void l2cu_send_peer_ble_par_req (tL2C_LCB *p_lcb, UINT16 min_int, UINT16 max_int, UINT16 latency, UINT16 timeout);
621 extern void l2cu_send_peer_ble_par_rsp (tL2C_LCB *p_lcb, UINT16 reason, UINT8 rem_id);
622 #endif
623
624 extern BOOLEAN l2cu_initialize_fixed_ccb (tL2C_LCB *p_lcb, UINT16 fixed_cid, tL2CAP_FCR_OPTS *p_fcr);
625 extern void    l2cu_no_dynamic_ccbs (tL2C_LCB *p_lcb);
626 extern void    l2cu_process_fixed_chnl_resp (tL2C_LCB *p_lcb);
627
628
629 /* Functions provided for Broadcom Aware
630 ****************************************
631 */
632 extern BOOLEAN  l2cu_check_feature_req (tL2C_LCB *p_lcb, UINT8 id, UINT8 *p_data, UINT16 data_len);
633 extern void     l2cu_check_feature_rsp (tL2C_LCB *p_lcb, UINT8 id, UINT8 *p_data, UINT16 data_len);
634 extern void     l2cu_send_feature_req (tL2C_CCB *p_ccb);
635
636 extern tL2C_RCB *l2cu_allocate_rcb (UINT16 psm);
637 extern tL2C_RCB *l2cu_find_rcb_by_psm (UINT16 psm);
638 extern void     l2cu_release_rcb (tL2C_RCB *p_rcb);
639
640 extern UINT8    l2cu_process_peer_cfg_req (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg);
641 extern void     l2cu_process_peer_cfg_rsp (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg);
642 extern void     l2cu_process_our_cfg_req (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg);
643 extern void     l2cu_process_our_cfg_rsp (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg);
644
645 extern void     l2cu_device_reset (void);
646 extern tL2C_LCB *l2cu_find_lcb_by_state (tL2C_LINK_STATE state);
647 extern BOOLEAN  l2cu_lcb_disconnecting (void);
648
649 extern BOOLEAN l2cu_create_conn (tL2C_LCB *p_lcb, tBT_TRANSPORT transport);
650 extern BOOLEAN l2cu_create_conn_after_switch (tL2C_LCB *p_lcb);
651 extern BT_HDR *l2cu_get_next_buffer_to_send (tL2C_LCB *p_lcb);
652 extern void    l2cu_resubmit_pending_sec_req (BD_ADDR p_bda);
653 extern void    l2cu_initialize_amp_ccb (tL2C_LCB *p_lcb);
654 extern void    l2cu_adjust_out_mps (tL2C_CCB *p_ccb);
655
656 /* Functions provided by l2c_link.c
657 ************************************
658 */
659 extern BOOLEAN  l2c_link_hci_conn_req (BD_ADDR bd_addr);
660 extern BOOLEAN  l2c_link_hci_conn_comp (UINT8 status, UINT16 handle, BD_ADDR p_bda);
661 extern BOOLEAN  l2c_link_hci_disc_comp (UINT16 handle, UINT8 reason);
662 extern BOOLEAN  l2c_link_hci_qos_violation (UINT16 handle);
663 extern void     l2c_link_timeout (tL2C_LCB *p_lcb);
664 extern void     l2c_info_timeout (tL2C_LCB *p_lcb);
665 extern void     l2c_link_check_send_pkts (tL2C_LCB *p_lcb, tL2C_CCB *p_ccb, BT_HDR *p_buf);
666 extern void     l2c_link_adjust_allocation (void);
667 extern void     l2c_link_process_num_completed_pkts (UINT8 *p);
668 extern void     l2c_link_process_num_completed_blocks (UINT8 controller_id, UINT8 *p, UINT16 evt_len);
669 extern void     l2c_link_processs_num_bufs (UINT16 num_lm_acl_bufs);
670 extern UINT8    l2c_link_pkts_rcvd (UINT16 *num_pkts, UINT16 *handles);
671 extern void     l2c_link_role_changed (BD_ADDR bd_addr, UINT8 new_role, UINT8 hci_status);
672 extern void     l2c_link_sec_comp (BD_ADDR p_bda, tBT_TRANSPORT trasnport, void *p_ref_data, UINT8 status);
673 extern void     l2c_link_segments_xmitted (BT_HDR *p_msg);
674 extern void     l2c_pin_code_request (BD_ADDR bd_addr);
675 extern void     l2c_link_adjust_chnl_allocation (void);
676
677 #if (BLE_INCLUDED == TRUE)
678 extern void     l2c_link_processs_ble_num_bufs (UINT16 num_lm_acl_bufs);
679 #endif
680
681 #if L2CAP_WAKE_PARKED_LINK == TRUE
682 extern BOOLEAN  l2c_link_check_power_mode ( tL2C_LCB *p_lcb );
683 #define L2C_LINK_CHECK_POWER_MODE(x) l2c_link_check_power_mode ((x))
684 #else  // L2CAP_WAKE_PARKED_LINK
685 #define L2C_LINK_CHECK_POWER_MODE(x) (FALSE)
686 #endif  // L2CAP_WAKE_PARKED_LINK
687
688 #if L2CAP_CONFORMANCE_TESTING == TRUE
689 /* Used only for conformance testing */
690 extern void l2cu_set_info_rsp_mask (UINT32 mask);
691 #endif
692
693 /* Functions provided by l2c_csm.c
694 ************************************
695 */
696 extern void l2c_csm_execute (tL2C_CCB *p_ccb, UINT16 event, void *p_data);
697
698 extern void l2c_enqueue_peer_data (tL2C_CCB *p_ccb, BT_HDR *p_buf);
699
700
701 /* Functions provided by l2c_fcr.c
702 ************************************
703 */
704 extern void     l2c_fcr_cleanup (tL2C_CCB *p_ccb);
705 extern void     l2c_fcr_proc_pdu (tL2C_CCB *p_ccb, BT_HDR *p_buf);
706 extern void     l2c_fcr_proc_tout (tL2C_CCB *p_ccb);
707 extern void     l2c_fcr_proc_ack_tout (tL2C_CCB *p_ccb);
708 extern void     l2c_fcr_send_S_frame (tL2C_CCB *p_ccb, UINT16 function_code, UINT16 pf_bit);
709 extern BT_HDR   *l2c_fcr_clone_buf (BT_HDR *p_buf, UINT16 new_offset, UINT16 no_of_bytes, UINT8 pool);
710 extern BOOLEAN  l2c_fcr_is_flow_controlled (tL2C_CCB *p_ccb);
711 extern BT_HDR   *l2c_fcr_get_next_xmit_sdu_seg (tL2C_CCB *p_ccb, UINT16 max_packet_length);
712 extern void     l2c_fcr_start_timer (tL2C_CCB *p_ccb);
713
714 /* Configuration negotiation */
715 extern UINT8    l2c_fcr_chk_chan_modes (tL2C_CCB *p_ccb);
716 extern BOOLEAN  l2c_fcr_adj_our_req_options (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg);
717 extern void     l2c_fcr_adj_our_rsp_options (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_peer_cfg);
718 extern BOOLEAN  l2c_fcr_renegotiate_chan(tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg);
719 extern UINT8    l2c_fcr_process_peer_cfg_req(tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg);
720 extern void     l2c_fcr_adj_monitor_retran_timeout (tL2C_CCB *p_ccb);
721 extern void     l2c_fcr_stop_timer (tL2C_CCB *p_ccb);
722
723 /* Functions provided by l2c_ble.c
724 ************************************
725 */
726 #if (BLE_INCLUDED == TRUE)
727 extern BOOLEAN l2cble_create_conn (tL2C_LCB *p_lcb);
728 extern void l2cble_process_sig_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len);
729 extern void l2cble_conn_comp (UINT16 handle, UINT8 role, BD_ADDR bda, tBLE_ADDR_TYPE type,
730                               UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout);
731 extern BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb);
732 extern void l2cble_notify_le_connection (BD_ADDR bda);
733 extern void l2c_ble_link_adjust_allocation (void);
734 extern void l2cble_process_conn_update_evt (UINT16 handle, UINT8 status, UINT16 conn_interval,
735                                                               UINT16 conn_latency, UINT16 conn_timeout);
736
737 #if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE)
738 extern void l2cble_process_rc_param_request_evt(UINT16 handle, UINT16 int_min, UINT16 int_max,
739         UINT16 latency, UINT16 timeout);
740 #endif
741
742 extern void l2cble_update_data_length(tL2C_LCB *p_lcb);
743 extern void l2cble_set_fixed_channel_tx_data_length(BD_ADDR remote_bda, UINT16 fix_cid,
744         UINT16 tx_mtu);
745 extern void l2c_send_update_conn_params_cb(tL2C_LCB *p_lcb, UINT8 status);
746 extern void l2cble_process_data_length_change_event(UINT16 handle, UINT16 tx_data_len,
747         UINT16 rx_data_len);
748
749 #endif
750 extern void l2cu_process_fixed_disc_cback (tL2C_LCB *p_lcb);
751
752 #ifdef __cplusplus
753 }
754 #endif
755
756 #endif