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