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