• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   bool local_busy;  /* true if we have flowed off the peer */
196 
197   bool rej_sent;       /* Reject was sent */
198   bool srej_sent;      /* Selective Reject was sent */
199   bool wait_ack;       /* Transmitter is waiting ack (poll sent) */
200   bool rej_after_srej; /* Send a REJ when SREJ clears */
201 
202   bool send_f_rsp; /* We need to send an F-bit response */
203 
204   uint16_t rx_sdu_len; /* Length of the SDU being received */
205   BT_HDR* p_rx_sdu;    /* Buffer holding the SDU being received */
206   fixed_queue_t*
207       waiting_for_ack_q;          /* Buffers sent and waiting for peer to ack */
208   fixed_queue_t* srej_rcv_hold_q; /* Buffers rcvd but held pending SREJ rsp */
209   fixed_queue_t* retrans_q;       /* Buffers being retransmitted */
210 
211   alarm_t* ack_timer;         /* Timer delaying RR */
212   alarm_t* mon_retrans_timer; /* Timer Monitor or Retransmission */
213 
214 #if (L2CAP_ERTM_STATS == TRUE)
215   uint64_t connect_tick_count;  /* Time channel was established */
216   uint32_t ertm_pkt_counts[2];  /* Packets sent and received */
217   uint32_t ertm_byte_counts[2]; /* Bytes   sent and received */
218   uint32_t s_frames_sent[4];    /* S-frames sent (RR, REJ, RNR, SREJ) */
219   uint32_t s_frames_rcvd[4];    /* S-frames rcvd (RR, REJ, RNR, SREJ) */
220   uint32_t xmit_window_closed;  /* # of times the xmit window was closed */
221   uint32_t controller_idle; /* # of times less than 2 packets in controller */
222                             /* when the xmit window was closed */
223   uint32_t pkts_retransmitted; /* # of packets that were retransmitted */
224   uint32_t retrans_touts;      /* # of retransmission timouts */
225   uint32_t xmit_ack_touts;     /* # of xmit ack timouts */
226 
227 #define L2CAP_ERTM_STATS_NUM_AVG 10
228 #define L2CAP_ERTM_STATS_AVG_NUM_SAMPLES 100
229   uint32_t ack_delay_avg_count;
230   uint32_t ack_delay_avg_index;
231   uint32_t throughput_start;
232   uint32_t throughput[L2CAP_ERTM_STATS_NUM_AVG];
233   uint32_t ack_delay_avg[L2CAP_ERTM_STATS_NUM_AVG];
234   uint32_t ack_delay_min[L2CAP_ERTM_STATS_NUM_AVG];
235   uint32_t ack_delay_max[L2CAP_ERTM_STATS_NUM_AVG];
236   uint32_t ack_q_count_avg[L2CAP_ERTM_STATS_NUM_AVG];
237   uint32_t ack_q_count_min[L2CAP_ERTM_STATS_NUM_AVG];
238   uint32_t ack_q_count_max[L2CAP_ERTM_STATS_NUM_AVG];
239 #endif
240 } tL2C_FCRB;
241 
242 typedef struct {
243   bool in_use;
244   bool log_packets;
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   tL2CAP_APPL_INFO api;
249 } tL2C_RCB;
250 
251 #ifndef L2CAP_CBB_DEFAULT_DATA_RATE_BUFF_QUOTA
252 #define L2CAP_CBB_DEFAULT_DATA_RATE_BUFF_QUOTA 100
253 #endif
254 
255 typedef void(tL2CAP_SEC_CBACK)(const RawAddress& bd_addr,
256                                tBT_TRANSPORT trasnport, void* p_ref_data,
257                                tBTM_STATUS result);
258 
259 typedef struct {
260   uint16_t psm;
261   tBT_TRANSPORT transport;
262   bool is_originator;
263   tL2CAP_SEC_CBACK* p_callback;
264   void* p_ref_data;
265 } tL2CAP_SEC_DATA;
266 
267 /* Define a channel control block (CCB). There may be many channel control
268  * blocks between the same two Bluetooth devices (i.e. on the same link).
269  * Each CCB has unique local and remote CIDs. All channel control blocks on
270  * the same physical link and are chained together.
271 */
272 typedef struct t_l2c_ccb {
273   bool in_use;                /* true when in use, false when not */
274   tL2C_CHNL_STATE chnl_state; /* Channel state */
275   tL2CAP_LE_CFG_INFO
276       local_conn_cfg; /* Our config for ble conn oriented channel */
277   tL2CAP_LE_CFG_INFO
278       peer_conn_cfg;       /* Peer device config ble conn oriented channel */
279   bool is_first_seg;       /* Dtermine whether the received packet is the first
280                               segment or not */
281   BT_HDR* ble_sdu;         /* Buffer for storing unassembled sdu*/
282   uint16_t ble_sdu_length; /* Length of unassembled sdu length*/
283   struct t_l2c_ccb* p_next_ccb; /* Next CCB in the chain */
284   struct t_l2c_ccb* p_prev_ccb; /* Previous CCB in the chain */
285   struct t_l2c_linkcb* p_lcb;   /* Link this CCB is assigned to */
286 
287   uint16_t local_cid;  /* Local CID */
288   uint16_t remote_cid; /* Remote CID */
289 
290   alarm_t* l2c_ccb_timer; /* CCB Timer Entry */
291 
292   tL2C_RCB* p_rcb;      /* Registration CB for this Channel */
293   bool should_free_rcb; /* True if RCB was allocated on the heap */
294 
295 #define IB_CFG_DONE 0x01
296 #define OB_CFG_DONE 0x02
297 #define RECONFIG_FLAG 0x04 /* True after initial configuration */
298 #define CFG_DONE_MASK (IB_CFG_DONE | OB_CFG_DONE)
299 
300   uint8_t config_done; /* Configuration flag word */
301   uint8_t local_id;    /* Transaction ID for local trans */
302   uint8_t remote_id;   /* Transaction ID for local */
303 
304 #define CCB_FLAG_NO_RETRY 0x01     /* no more retry */
305 #define CCB_FLAG_SENT_PENDING 0x02 /* already sent pending response */
306   uint8_t flags;
307 
308   tL2CAP_CFG_INFO our_cfg;          /* Our saved configuration options */
309   tL2CAP_CH_CFG_BITS peer_cfg_bits; /* Store what peer wants to configure */
310   tL2CAP_CFG_INFO peer_cfg;         /* Peer's saved configuration options */
311 
312   fixed_queue_t* xmit_hold_q; /* Transmit data hold queue */
313   bool cong_sent;             /* Set when congested status sent */
314   uint16_t buff_quota;        /* Buffer quota before sending congestion */
315 
316   tL2CAP_CHNL_PRIORITY ccb_priority;  /* Channel priority */
317   tL2CAP_CHNL_DATA_RATE tx_data_rate; /* Channel Tx data rate */
318   tL2CAP_CHNL_DATA_RATE rx_data_rate; /* Channel Rx data rate */
319 
320   /* Fields used for eL2CAP */
321   tL2CAP_ERTM_INFO ertm_info;
322   tL2C_FCRB fcrb;
323   uint16_t tx_mps; /* TX MPS adjusted based on current controller */
324   uint16_t max_rx_mtu;
325   uint8_t fcr_cfg_tries;          /* Max number of negotiation attempts */
326   bool peer_cfg_already_rejected; /* If mode rejected once, set to true */
327   bool out_cfg_fcr_present; /* true if cfg response shoulkd include fcr options
328                                */
329 
330 #define L2CAP_CFG_FCS_OUR 0x01  /* Our desired config FCS option */
331 #define L2CAP_CFG_FCS_PEER 0x02 /* Peer's desired config FCS option */
332 #define L2CAP_BYPASS_FCS (L2CAP_CFG_FCS_OUR | L2CAP_CFG_FCS_PEER)
333   uint8_t bypass_fcs;
334 
335 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
336   bool is_flushable; /* true if channel is flushable */
337 #endif
338 
339 #if (L2CAP_NUM_FIXED_CHNLS > 0)
340   uint16_t fixed_chnl_idle_tout; /* Idle timeout to use for the fixed channel */
341 #endif
342   uint16_t tx_data_len;
343 
344   /* Number of LE frames that the remote can send to us (credit count in
345    * remote). Valid only for LE CoC */
346   uint16_t remote_credit_count;
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 
419   BT_HDR* p_hcit_rcv_acl;   /* Current HCIT ACL buf being rcvd */
420   uint16_t idle_timeout_sv; /* Save current Idle timeout */
421   uint8_t acl_priority;     /* L2C_PRIORITY_NORMAL or L2C_PRIORITY_HIGH */
422   tL2CA_NOCP_CB* p_nocp_cb; /* Num Cmpl pkts callback */
423 
424 #if (L2CAP_NUM_FIXED_CHNLS > 0)
425   tL2C_CCB* p_fixed_ccbs[L2CAP_NUM_FIXED_CHNLS];
426   uint16_t disc_reason;
427 #endif
428 
429   tBT_TRANSPORT transport;
430   uint8_t initiating_phys;  // LE PHY used for connection initiation
431   tBLE_ADDR_TYPE ble_addr_type;
432   uint16_t tx_data_len; /* tx data length used in data length extension */
433   fixed_queue_t* le_sec_pending_q; /* LE coc channels waiting for security check
434                                       completion */
435   uint8_t sec_act;
436 #define L2C_BLE_CONN_UPDATE_DISABLE \
437   0x1                              /* disable update connection parameters */
438 #define L2C_BLE_NEW_CONN_PARAM 0x2 /* new connection parameter to be set */
439 #define L2C_BLE_UPDATE_PENDING                  \
440   0x4 /* waiting for connection update finished \
441          */
442 #define L2C_BLE_NOT_DEFAULT_PARAM \
443   0x8 /* not using default connection parameters */
444   uint8_t conn_update_mask;
445 
446   uint16_t min_interval; /* parameters as requested by peripheral */
447   uint16_t max_interval;
448   uint16_t latency;
449   uint16_t timeout;
450   uint16_t min_ce_len;
451   uint16_t max_ce_len;
452 
453 #if (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE)
454   /* each priority group is limited burst transmission */
455   /* round robin service for the same priority channels */
456   tL2C_RR_SERV rr_serv[L2CAP_NUM_CHNL_PRIORITY];
457   uint8_t rr_pri; /* current serving priority group */
458 #endif
459 
460 } tL2C_LCB;
461 
462 /* Define the L2CAP control structure
463 */
464 typedef struct {
465   uint8_t l2cap_trace_level;
466   uint16_t controller_xmit_window; /* Total ACL window for all links */
467 
468   uint16_t round_robin_quota;   /* Round-robin link quota */
469   uint16_t round_robin_unacked; /* Round-robin unacked */
470   bool check_round_robin;       /* Do a round robin check */
471 
472   bool is_cong_cback_context;
473 
474   tL2C_LCB lcb_pool[MAX_L2CAP_LINKS];    /* Link Control Block pool */
475   tL2C_CCB ccb_pool[MAX_L2CAP_CHANNELS]; /* Channel Control Block pool */
476   tL2C_RCB rcb_pool[MAX_L2CAP_CLIENTS];  /* Registration info pool */
477 
478   tL2C_CCB* p_free_ccb_first; /* Pointer to first free CCB */
479   tL2C_CCB* p_free_ccb_last;  /* Pointer to last  free CCB */
480 
481   uint8_t
482       desire_role; /* desire to be master/slave when accepting a connection */
483   bool disallow_switch;     /* false, to allow switch at create conn */
484   uint16_t num_lm_acl_bufs; /* # of ACL buffers on controller */
485   uint16_t idle_timeout;    /* Idle timeout */
486 
487   list_t* rcv_pending_q;       /* Recv pending queue */
488   alarm_t* receive_hold_timer; /* Timer entry for rcv hold */
489 
490   tL2C_LCB* p_cur_hcit_lcb;  /* Current HCI Transport buffer */
491   uint16_t num_links_active; /* Number of links active */
492 
493 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
494   uint16_t non_flushable_pbf; /* L2CAP_PKT_START_NON_FLUSHABLE if controller
495                                  supports */
496   /* Otherwise, L2CAP_PKT_START */
497   bool is_flush_active; /* true if an HCI_Enhanced_Flush has been sent */
498 #endif
499 
500 #if (L2CAP_CONFORMANCE_TESTING == TRUE)
501   uint32_t test_info_resp; /* Conformance testing needs a dynamic response */
502 #endif
503 
504 #if (L2CAP_NUM_FIXED_CHNLS > 0)
505   tL2CAP_FIXED_CHNL_REG
506       fixed_reg[L2CAP_NUM_FIXED_CHNLS]; /* Reg info for fixed channels */
507 #endif
508 
509   uint16_t num_ble_links_active; /* Number of LE links active */
510   uint16_t controller_le_xmit_window; /* Total ACL window for all links */
511   tL2C_BLE_FIXED_CHNLS_MASK l2c_ble_fixed_chnls_mask;  // LE fixed channels mask
512   uint16_t num_lm_ble_bufs;         /* # of ACL buffers on controller */
513   uint16_t ble_round_robin_quota;   /* Round-robin link quota */
514   uint16_t ble_round_robin_unacked; /* Round-robin unacked */
515   bool ble_check_round_robin;       /* Do a round robin check */
516   tL2C_RCB ble_rcb_pool[BLE_MAX_L2CAP_CLIENTS]; /* Registration info pool */
517 
518   tL2CA_ECHO_DATA_CB* p_echo_data_cb; /* Echo data callback */
519 
520 #if (L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE == TRUE)
521   uint16_t high_pri_min_xmit_quota; /* Minimum number of ACL credit for high
522                                        priority link */
523 #endif /* (L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE == TRUE) */
524 
525   uint16_t dyn_psm;
526 
527   uint16_t le_dyn_psm; /* Next LE dynamic PSM value to try to assign */
528   bool le_dyn_psm_assigned[LE_DYNAMIC_PSM_RANGE]; /* Table of assigned LE PSM */
529 
530 } tL2C_CB;
531 
532 /* Define a structure that contains the information about a connection.
533  * This structure is used to pass between functions, and not all the
534  * fields will always be filled in.
535 */
536 typedef struct {
537   RawAddress bd_addr;    /* Remote BD address */
538   uint8_t status;        /* Connection status */
539   uint16_t psm;          /* PSM of the connection */
540   uint16_t l2cap_result; /* L2CAP result */
541   uint16_t l2cap_status; /* L2CAP status */
542   uint16_t remote_cid;   /* Remote CID */
543 } tL2C_CONN_INFO;
544 
545 typedef void(tL2C_FCR_MGMT_EVT_HDLR)(uint8_t, tL2C_CCB*);
546 
547 /* Necessary info for postponed TX completion callback
548 */
549 typedef struct {
550   uint16_t local_cid;
551   uint16_t num_sdu;
552   tL2CA_TX_COMPLETE_CB* cb;
553 } tL2C_TX_COMPLETE_CB_INFO;
554 
555 /* The offset in a buffer that L2CAP will use when building commands.
556 */
557 #define L2CAP_SEND_CMD_OFFSET 0
558 
559 /* Number of ACL buffers to use for high priority channel
560 */
561 #if (L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE == FALSE)
562 #define L2CAP_HIGH_PRI_MIN_XMIT_QUOTA_A (L2CAP_HIGH_PRI_MIN_XMIT_QUOTA)
563 #else
564 #define L2CAP_HIGH_PRI_MIN_XMIT_QUOTA_A (l2cb.high_pri_min_xmit_quota)
565 #endif
566 
567 /* L2CAP global data
568  ***********************************
569 */
570 extern tL2C_CB l2cb;
571 
572 /* Functions provided by l2c_main.cc
573  ***********************************
574 */
575 void l2c_init(void);
576 void l2c_free(void);
577 
578 extern void l2c_receive_hold_timer_timeout(void* data);
579 extern void l2c_ccb_timer_timeout(void* data);
580 extern void l2c_lcb_timer_timeout(void* data);
581 extern void l2c_fcrb_ack_timer_timeout(void* data);
582 extern uint8_t l2c_data_write(uint16_t cid, BT_HDR* p_data, uint16_t flag);
583 extern void l2c_rcv_acl_data(BT_HDR* p_msg);
584 extern void l2c_process_held_packets(bool timed_out);
585 
586 /* Functions provided by l2c_utils.cc
587  ***********************************
588 */
589 extern bool l2cu_can_allocate_lcb(void);
590 extern tL2C_LCB* l2cu_allocate_lcb(const RawAddress& p_bd_addr, bool is_bonding,
591                                    tBT_TRANSPORT transport);
592 extern bool l2cu_start_post_bond_timer(uint16_t handle);
593 extern void l2cu_release_lcb(tL2C_LCB* p_lcb);
594 extern tL2C_LCB* l2cu_find_lcb_by_bd_addr(const RawAddress& p_bd_addr,
595                                           tBT_TRANSPORT transport);
596 extern tL2C_LCB* l2cu_find_lcb_by_handle(uint16_t handle);
597 extern void l2cu_update_lcb_4_bonding(const RawAddress& p_bd_addr,
598                                       bool is_bonding);
599 
600 extern uint8_t l2cu_get_conn_role(tL2C_LCB* p_this_lcb);
601 extern bool l2cu_set_acl_priority(const RawAddress& bd_addr, uint8_t priority,
602                                   bool reset_after_rs);
603 
604 extern void l2cu_enqueue_ccb(tL2C_CCB* p_ccb);
605 extern void l2cu_dequeue_ccb(tL2C_CCB* p_ccb);
606 extern void l2cu_change_pri_ccb(tL2C_CCB* p_ccb, tL2CAP_CHNL_PRIORITY priority);
607 
608 extern tL2C_CCB* l2cu_allocate_ccb(tL2C_LCB* p_lcb, uint16_t cid);
609 extern void l2cu_release_ccb(tL2C_CCB* p_ccb);
610 extern tL2C_CCB* l2cu_find_ccb_by_cid(tL2C_LCB* p_lcb, uint16_t local_cid);
611 extern tL2C_CCB* l2cu_find_ccb_by_remote_cid(tL2C_LCB* p_lcb,
612                                              uint16_t remote_cid);
613 extern void l2cu_adj_id(tL2C_LCB* p_lcb, uint8_t adj_mask);
614 extern bool l2c_is_cmd_rejected(uint8_t cmd_code, uint8_t id, tL2C_LCB* p_lcb);
615 
616 extern void l2cu_send_peer_cmd_reject(tL2C_LCB* p_lcb, uint16_t reason,
617                                       uint8_t rem_id, uint16_t p1, uint16_t p2);
618 extern void l2cu_send_peer_connect_req(tL2C_CCB* p_ccb);
619 extern void l2cu_send_peer_connect_rsp(tL2C_CCB* p_ccb, uint16_t result,
620                                        uint16_t status);
621 extern void l2cu_send_peer_config_req(tL2C_CCB* p_ccb, tL2CAP_CFG_INFO* p_cfg);
622 extern void l2cu_send_peer_config_rsp(tL2C_CCB* p_ccb, tL2CAP_CFG_INFO* p_cfg);
623 extern void l2cu_send_peer_config_rej(tL2C_CCB* p_ccb, uint8_t* p_data,
624                                       uint16_t data_len, uint16_t rej_len);
625 extern void l2cu_send_peer_disc_req(tL2C_CCB* p_ccb);
626 extern void l2cu_send_peer_disc_rsp(tL2C_LCB* p_lcb, uint8_t remote_id,
627                                     uint16_t local_cid, uint16_t remote_cid);
628 extern void l2cu_send_peer_echo_req(tL2C_LCB* p_lcb, uint8_t* p_data,
629                                     uint16_t data_len);
630 extern void l2cu_send_peer_echo_rsp(tL2C_LCB* p_lcb, uint8_t id,
631                                     uint8_t* p_data, uint16_t data_len);
632 extern void l2cu_send_peer_info_rsp(tL2C_LCB* p_lcb, uint8_t id,
633                                     uint16_t info_type);
634 extern void l2cu_reject_connection(tL2C_LCB* p_lcb, uint16_t remote_cid,
635                                    uint8_t rem_id, uint16_t result);
636 extern void l2cu_send_peer_info_req(tL2C_LCB* p_lcb, uint16_t info_type);
637 extern void l2cu_set_acl_hci_header(BT_HDR* p_buf, tL2C_CCB* p_ccb);
638 extern void l2cu_check_channel_congestion(tL2C_CCB* p_ccb);
639 extern void l2cu_disconnect_chnl(tL2C_CCB* p_ccb);
640 
641 extern void l2cu_tx_complete(tL2C_TX_COMPLETE_CB_INFO* p_cbi);
642 
643 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
644 extern void l2cu_set_non_flushable_pbf(bool);
645 #endif
646 
647 extern void l2cu_send_peer_ble_par_req(tL2C_LCB* p_lcb, uint16_t min_int,
648                                        uint16_t max_int, uint16_t latency,
649                                        uint16_t timeout);
650 extern void l2cu_send_peer_ble_par_rsp(tL2C_LCB* p_lcb, uint16_t reason,
651                                        uint8_t rem_id);
652 extern void l2cu_reject_ble_connection(tL2C_LCB* p_lcb, uint8_t rem_id,
653                                        uint16_t result);
654 extern void l2cu_send_peer_ble_credit_based_conn_res(tL2C_CCB* p_ccb,
655                                                      uint16_t result);
656 extern void l2cu_send_peer_ble_credit_based_conn_req(tL2C_CCB* p_ccb);
657 extern void l2cu_send_peer_ble_flow_control_credit(tL2C_CCB* p_ccb,
658                                                    uint16_t credit_value);
659 extern void l2cu_send_peer_ble_credit_based_disconn_req(tL2C_CCB* p_ccb);
660 
661 extern bool l2cu_initialize_fixed_ccb(tL2C_LCB* p_lcb, uint16_t fixed_cid,
662                                       tL2CAP_FCR_OPTS* p_fcr);
663 extern void l2cu_no_dynamic_ccbs(tL2C_LCB* p_lcb);
664 extern void l2cu_process_fixed_chnl_resp(tL2C_LCB* p_lcb);
665 extern bool l2cu_is_ccb_active(tL2C_CCB* p_ccb);
666 
667 /* Functions provided for Broadcom Aware
668  ***************************************
669 */
670 extern bool l2cu_check_feature_req(tL2C_LCB* p_lcb, uint8_t id, uint8_t* p_data,
671                                    uint16_t data_len);
672 extern void l2cu_check_feature_rsp(tL2C_LCB* p_lcb, uint8_t id, uint8_t* p_data,
673                                    uint16_t data_len);
674 extern void l2cu_send_feature_req(tL2C_CCB* p_ccb);
675 
676 extern tL2C_RCB* l2cu_allocate_rcb(uint16_t psm);
677 extern tL2C_RCB* l2cu_find_rcb_by_psm(uint16_t psm);
678 extern void l2cu_release_rcb(tL2C_RCB* p_rcb);
679 extern void l2cu_release_ble_rcb(tL2C_RCB* p_rcb);
680 extern tL2C_RCB* l2cu_allocate_ble_rcb(uint16_t psm);
681 extern tL2C_RCB* l2cu_find_ble_rcb_by_psm(uint16_t psm);
682 
683 extern uint8_t l2cu_process_peer_cfg_req(tL2C_CCB* p_ccb,
684                                          tL2CAP_CFG_INFO* p_cfg);
685 extern void l2cu_process_peer_cfg_rsp(tL2C_CCB* p_ccb, tL2CAP_CFG_INFO* p_cfg);
686 extern void l2cu_process_our_cfg_req(tL2C_CCB* p_ccb, tL2CAP_CFG_INFO* p_cfg);
687 extern void l2cu_process_our_cfg_rsp(tL2C_CCB* p_ccb, tL2CAP_CFG_INFO* p_cfg);
688 
689 extern void l2cu_device_reset(void);
690 extern tL2C_LCB* l2cu_find_lcb_by_state(tL2C_LINK_STATE state);
691 extern bool l2cu_lcb_disconnecting(void);
692 
693 extern bool l2cu_create_conn_br_edr(tL2C_LCB* p_lcb);
694 extern bool l2cu_create_conn_le(tL2C_LCB* p_lcb);
695 extern bool l2cu_create_conn_le(tL2C_LCB* p_lcb, uint8_t initiating_phys);
696 extern bool l2cu_create_conn_after_switch(tL2C_LCB* p_lcb);
697 extern BT_HDR* l2cu_get_next_buffer_to_send(tL2C_LCB* p_lcb,
698                                             tL2C_TX_COMPLETE_CB_INFO* p_cbi);
699 extern void l2cu_resubmit_pending_sec_req(const RawAddress* p_bda);
700 extern void l2cu_initialize_amp_ccb(tL2C_LCB* p_lcb);
701 extern void l2cu_adjust_out_mps(tL2C_CCB* p_ccb);
702 
703 /* Functions provided by l2c_link.cc
704  ***********************************
705 */
706 extern bool l2c_link_hci_conn_req(const RawAddress& bd_addr);
707 extern bool l2c_link_hci_conn_comp(uint8_t status, uint16_t handle,
708                                    const RawAddress& p_bda);
709 extern bool l2c_link_hci_disc_comp(uint16_t handle, uint8_t reason);
710 extern bool l2c_link_hci_qos_violation(uint16_t handle);
711 extern void l2c_link_timeout(tL2C_LCB* p_lcb);
712 extern void l2c_info_resp_timer_timeout(void* data);
713 extern void l2c_link_check_send_pkts(tL2C_LCB* p_lcb, tL2C_CCB* p_ccb,
714                                      BT_HDR* p_buf);
715 extern void l2c_link_adjust_allocation(void);
716 extern void l2c_link_process_num_completed_pkts(uint8_t* p);
717 extern void l2c_link_process_num_completed_blocks(uint8_t controller_id,
718                                                   uint8_t* p, uint16_t evt_len);
719 extern void l2c_link_processs_num_bufs(uint16_t num_lm_acl_bufs);
720 extern uint8_t l2c_link_pkts_rcvd(uint16_t* num_pkts, uint16_t* handles);
721 extern void l2c_link_role_changed(const RawAddress* bd_addr, uint8_t new_role,
722                                   uint8_t hci_status);
723 extern void l2c_link_sec_comp(const RawAddress* p_bda, tBT_TRANSPORT trasnport,
724                               void* p_ref_data, uint8_t status);
725 extern void l2c_link_sec_comp2(const RawAddress& p_bda, tBT_TRANSPORT trasnport,
726                                void* p_ref_data, uint8_t status);
727 extern void l2c_link_segments_xmitted(BT_HDR* p_msg);
728 extern void l2c_pin_code_request(const RawAddress& bd_addr);
729 extern void l2c_link_adjust_chnl_allocation(void);
730 
731 extern void l2c_link_processs_ble_num_bufs(uint16_t num_lm_acl_bufs);
732 
733 #if (L2CAP_WAKE_PARKED_LINK == TRUE)
734 extern bool l2c_link_check_power_mode(tL2C_LCB* p_lcb);
735 #define L2C_LINK_CHECK_POWER_MODE(x) l2c_link_check_power_mode((x))
736 #else  // L2CAP_WAKE_PARKED_LINK
737 #define L2C_LINK_CHECK_POWER_MODE(x) (false)
738 #endif  // L2CAP_WAKE_PARKED_LINK
739 
740 #if (L2CAP_CONFORMANCE_TESTING == TRUE)
741 /* Used only for conformance testing */
742 extern void l2cu_set_info_rsp_mask(uint32_t mask);
743 #endif
744 
745 /* Functions provided by l2c_csm.cc
746  ***********************************
747 */
748 extern void l2c_csm_execute(tL2C_CCB* p_ccb, uint16_t event, void* p_data);
749 
750 extern void l2c_enqueue_peer_data(tL2C_CCB* p_ccb, BT_HDR* p_buf);
751 
752 /* Functions provided by l2c_fcr.cc
753  ***********************************
754 */
755 extern void l2c_fcr_cleanup(tL2C_CCB* p_ccb);
756 extern void l2c_fcr_proc_pdu(tL2C_CCB* p_ccb, BT_HDR* p_buf);
757 extern void l2c_fcr_proc_tout(tL2C_CCB* p_ccb);
758 extern void l2c_fcr_proc_ack_tout(tL2C_CCB* p_ccb);
759 extern void l2c_fcr_send_S_frame(tL2C_CCB* p_ccb, uint16_t function_code,
760                                  uint16_t pf_bit);
761 extern BT_HDR* l2c_fcr_clone_buf(BT_HDR* p_buf, uint16_t new_offset,
762                                  uint16_t no_of_bytes);
763 extern bool l2c_fcr_is_flow_controlled(tL2C_CCB* p_ccb);
764 extern BT_HDR* l2c_fcr_get_next_xmit_sdu_seg(tL2C_CCB* p_ccb,
765                                              uint16_t max_packet_length);
766 extern void l2c_fcr_start_timer(tL2C_CCB* p_ccb);
767 extern void l2c_lcc_proc_pdu(tL2C_CCB* p_ccb, BT_HDR* p_buf);
768 extern BT_HDR* l2c_lcc_get_next_xmit_sdu_seg(tL2C_CCB* p_ccb,
769                                              bool* last_piece_of_sdu);
770 
771 /* Configuration negotiation */
772 extern uint8_t l2c_fcr_chk_chan_modes(tL2C_CCB* p_ccb);
773 extern bool l2c_fcr_adj_our_req_options(tL2C_CCB* p_ccb,
774                                         tL2CAP_CFG_INFO* p_cfg);
775 extern void l2c_fcr_adj_our_rsp_options(tL2C_CCB* p_ccb,
776                                         tL2CAP_CFG_INFO* p_peer_cfg);
777 extern bool l2c_fcr_renegotiate_chan(tL2C_CCB* p_ccb, tL2CAP_CFG_INFO* p_cfg);
778 extern uint8_t l2c_fcr_process_peer_cfg_req(tL2C_CCB* p_ccb,
779                                             tL2CAP_CFG_INFO* p_cfg);
780 extern void l2c_fcr_adj_monitor_retran_timeout(tL2C_CCB* p_ccb);
781 extern void l2c_fcr_stop_timer(tL2C_CCB* p_ccb);
782 
783 /* Functions provided by l2c_ble.cc
784  ***********************************
785 */
786 extern bool l2cble_create_conn(tL2C_LCB* p_lcb);
787 extern void l2cble_process_sig_cmd(tL2C_LCB* p_lcb, uint8_t* p,
788                                    uint16_t pkt_len);
789 extern void l2cble_conn_comp(uint16_t handle, uint8_t role,
790                              const RawAddress& bda, tBLE_ADDR_TYPE type,
791                              uint16_t conn_interval, uint16_t conn_latency,
792                              uint16_t conn_timeout);
793 extern void l2cble_notify_le_connection(const RawAddress& bda);
794 extern void l2c_ble_link_adjust_allocation(void);
795 extern void l2cble_process_conn_update_evt(uint16_t handle, uint8_t status,
796                                            uint16_t interval, uint16_t latency,
797                                            uint16_t timeout);
798 
799 extern void l2cble_credit_based_conn_req(tL2C_CCB* p_ccb);
800 extern void l2cble_credit_based_conn_res(tL2C_CCB* p_ccb, uint16_t result);
801 extern void l2cble_send_peer_disc_req(tL2C_CCB* p_ccb);
802 extern void l2cble_send_flow_control_credit(tL2C_CCB* p_ccb,
803                                             uint16_t credit_value);
804 extern tL2CAP_LE_RESULT_CODE l2ble_sec_access_req(const RawAddress& bd_addr,
805                                                   uint16_t psm,
806                                                   bool is_originator,
807                                                   tL2CAP_SEC_CBACK* p_callback,
808                                                   void* p_ref_data);
809 
810 #if (BLE_LLT_INCLUDED == TRUE)
811 extern void l2cble_process_rc_param_request_evt(uint16_t handle,
812                                                 uint16_t int_min,
813                                                 uint16_t int_max,
814                                                 uint16_t latency,
815                                                 uint16_t timeout);
816 #endif
817 
818 extern void l2cble_update_data_length(tL2C_LCB* p_lcb);
819 extern void l2cble_set_fixed_channel_tx_data_length(
820     const RawAddress& remote_bda, uint16_t fix_cid, uint16_t tx_mtu);
821 extern void l2cble_process_data_length_change_event(uint16_t handle,
822                                                     uint16_t tx_data_len,
823                                                     uint16_t rx_data_len);
824 
825 extern void l2cu_process_fixed_disc_cback(tL2C_LCB* p_lcb);
826 
827 #endif
828