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