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