• 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 <base/strings/stringprintf.h>
28 #include <stdbool.h>
29 
30 #include <string>
31 
32 #include "btm_api.h"
33 #include "btm_ble_api.h"
34 #include "l2c_api.h"
35 #include "l2cap_acl_interface.h"
36 #include "l2cap_controller_interface.h"
37 #include "l2cap_hci_link_interface.h"
38 #include "l2cap_security_interface.h"
39 #include "l2cdefs.h"
40 #include "osi/include/alarm.h"
41 #include "osi/include/fixed_queue.h"
42 #include "osi/include/list.h"
43 #include "stack/include/bt_hdr.h"
44 #include "stack/include/hci_error_code.h"
45 #include "types/hci_role.h"
46 #include "types/raw_address.h"
47 
48 #define L2CAP_MIN_MTU 48 /* Minimum acceptable MTU is 48 bytes */
49 
50 #define MAX_ACTIVE_AVDT_CONN 2
51 
52 constexpr uint16_t L2CAP_CREDIT_BASED_MIN_MTU = 64;
53 constexpr uint16_t L2CAP_CREDIT_BASED_MIN_MPS = 64;
54 
55 /*
56  * Timeout values (in milliseconds).
57  */
58 #define L2CAP_LINK_ROLE_SWITCH_TIMEOUT_MS (10 * 1000)  /* 10 seconds */
59 #define L2CAP_LINK_CONNECT_TIMEOUT_MS (60 * 1000)      /* 30 seconds */
60 #define L2CAP_LINK_CONNECT_EXT_TIMEOUT_MS (120 * 1000) /* 120 seconds */
61 #define L2CAP_LINK_FLOW_CONTROL_TIMEOUT_MS (2 * 1000)  /* 2 seconds */
62 #define L2CAP_LINK_DISCONNECT_TIMEOUT_MS (30 * 1000)   /* 30 seconds */
63 #define L2CAP_CHNL_CONNECT_TIMEOUT_MS (60 * 1000)      /* 60 seconds */
64 #define L2CAP_CHNL_CONNECT_EXT_TIMEOUT_MS (120 * 1000) /* 120 seconds */
65 #define L2CAP_CHNL_CFG_TIMEOUT_MS (30 * 1000)          /* 30 seconds */
66 #define L2CAP_CHNL_DISCONNECT_TIMEOUT_MS (10 * 1000)   /* 10 seconds */
67 #define L2CAP_DELAY_CHECK_SM4_TIMEOUT_MS (2 * 1000)    /* 2 seconds */
68 #define L2CAP_WAIT_INFO_RSP_TIMEOUT_MS (3 * 1000)      /* 3 seconds */
69 #define L2CAP_BLE_LINK_CONNECT_TIMEOUT_MS (30 * 1000)  /* 30 seconds */
70 #define L2CAP_FCR_ACK_TIMEOUT_MS 200                   /* 200 milliseconds */
71 
72 /* Define the possible L2CAP channel states. The names of
73  * the states may seem a bit strange, but they are taken from
74  * the Bluetooth specification.
75 */
76 typedef enum {
77   CST_CLOSED,                  /* Channel is in closed state */
78   CST_ORIG_W4_SEC_COMP,        /* Originator waits security clearence */
79   CST_TERM_W4_SEC_COMP,        /* Acceptor waits security clearence */
80   CST_W4_L2CAP_CONNECT_RSP,    /* Waiting for peer conenct response */
81   CST_W4_L2CA_CONNECT_RSP,     /* Waiting for upper layer connect rsp */
82   CST_CONFIG,                  /* Negotiating configuration */
83   CST_OPEN,                    /* Data transfer state */
84   CST_W4_L2CAP_DISCONNECT_RSP, /* Waiting for peer disconnect rsp */
85   CST_W4_L2CA_DISCONNECT_RSP   /* Waiting for upper layer disc rsp */
86 } tL2C_CHNL_STATE;
87 
88 #define CASE_RETURN_TEXT(code) \
89   case code:                   \
90     return #code
91 
channel_state_text(const tL2C_CHNL_STATE & state)92 inline std::string channel_state_text(const tL2C_CHNL_STATE& state) {
93   switch (state) {
94     CASE_RETURN_TEXT(CST_CLOSED);
95     CASE_RETURN_TEXT(CST_ORIG_W4_SEC_COMP);
96     CASE_RETURN_TEXT(CST_TERM_W4_SEC_COMP);
97     CASE_RETURN_TEXT(CST_W4_L2CAP_CONNECT_RSP);
98     CASE_RETURN_TEXT(CST_W4_L2CA_CONNECT_RSP);
99     CASE_RETURN_TEXT(CST_CONFIG);
100     CASE_RETURN_TEXT(CST_OPEN);
101     CASE_RETURN_TEXT(CST_W4_L2CAP_DISCONNECT_RSP);
102     CASE_RETURN_TEXT(CST_W4_L2CA_DISCONNECT_RSP);
103     default:
104       return base::StringPrintf("UNKNOWN[%d]", state);
105   }
106 }
107 #undef CASE_RETURN_TEXT
108 
109 /* Define the possible L2CAP link states
110 */
111 typedef enum {
112   LST_DISCONNECTED,
113   LST_CONNECT_HOLDING,
114   LST_CONNECTING_WAIT_SWITCH,
115   LST_CONNECTING,
116   LST_CONNECTED,
117   LST_DISCONNECTING
118 } tL2C_LINK_STATE;
119 
link_state_text(const tL2C_LINK_STATE & state)120 inline std::string link_state_text(const tL2C_LINK_STATE& state) {
121   switch (state) {
122     case LST_DISCONNECTED:
123       return std::string("LST_DISCONNECTED");
124     case LST_CONNECT_HOLDING:
125       return std::string("LST_CONNECT_HOLDING");
126     case LST_CONNECTING_WAIT_SWITCH:
127       return std::string("LST_CONNECTING_WAIT_SWITCH");
128     case LST_CONNECTING:
129       return std::string("LST_CONNECTING");
130     case LST_CONNECTED:
131       return std::string("LST_CONNECTED");
132     case LST_DISCONNECTING:
133       return std::string("LST_DISCONNECTING");
134     default:
135       return std::string("UNKNOWN");
136   }
137 }
138 
139 /* Define input events to the L2CAP link and channel state machines. The names
140  * of the events may seem a bit strange, but they are taken from
141  * the Bluetooth specification.
142 */
143 typedef enum : uint16_t {
144   /* Lower layer */
145   L2CEVT_LP_CONNECT_CFM = 0,     /* connect confirm */
146   L2CEVT_LP_CONNECT_CFM_NEG = 1, /* connect confirm (failed) */
147   L2CEVT_LP_CONNECT_IND = 2,     /* connect indication */
148   L2CEVT_LP_DISCONNECT_IND = 3,  /* disconnect indication */
149 
150   /* Security */
151   L2CEVT_SEC_COMP = 7,     /* cleared successfully */
152   L2CEVT_SEC_COMP_NEG = 8, /* procedure failed */
153 
154   /* Peer connection */
155   L2CEVT_L2CAP_CONNECT_REQ = 10,     /* request */
156   L2CEVT_L2CAP_CONNECT_RSP = 11,     /* response */
157   L2CEVT_L2CAP_CONNECT_RSP_PND = 12, /* response pending */
158   L2CEVT_L2CAP_CONNECT_RSP_NEG = 13, /* response (failed) */
159 
160   /* Peer configuration */
161   L2CEVT_L2CAP_CONFIG_REQ = 14,     /* request */
162   L2CEVT_L2CAP_CONFIG_RSP = 15,     /* response */
163   L2CEVT_L2CAP_CONFIG_RSP_NEG = 16, /* response (failed) */
164 
165   L2CEVT_L2CAP_DISCONNECT_REQ = 17, /* Peer disconnect request */
166   L2CEVT_L2CAP_DISCONNECT_RSP = 18, /* Peer disconnect response */
167   L2CEVT_L2CAP_INFO_RSP = 19,       /* Peer information response */
168   L2CEVT_L2CAP_DATA = 20,           /* Peer data */
169 
170   /* Upper layer */
171   L2CEVT_L2CA_CONNECT_REQ = 21,     /* connect request */
172   L2CEVT_L2CA_CONNECT_RSP = 22,     /* connect response */
173   L2CEVT_L2CA_CONNECT_RSP_NEG = 23, /* connect response (failed)*/
174   L2CEVT_L2CA_CONFIG_REQ = 24,      /* config request */
175   L2CEVT_L2CA_CONFIG_RSP = 25,      /* config response */
176   L2CEVT_L2CA_DISCONNECT_REQ = 27,  /* disconnect request */
177   L2CEVT_L2CA_DISCONNECT_RSP = 28,  /* disconnect response */
178   L2CEVT_L2CA_DATA_READ = 29,       /* data read */
179   L2CEVT_L2CA_DATA_WRITE = 30,      /* data write */
180 
181   L2CEVT_TIMEOUT = 32,         /* Timeout */
182   L2CEVT_SEC_RE_SEND_CMD = 33, /* btm_sec has enough info to proceed */
183 
184   L2CEVT_ACK_TIMEOUT = 34, /* RR delay timeout */
185 
186   L2CEVT_L2CA_SEND_FLOW_CONTROL_CREDIT = 35, /* Upper layer credit packet \
187                                               */
188   /* Peer credit based connection */
189   L2CEVT_L2CAP_RECV_FLOW_CONTROL_CREDIT = 36, /* credit packet */
190   L2CEVT_L2CAP_CREDIT_BASED_CONNECT_REQ =
191       37, /* credit based connection request */
192   L2CEVT_L2CAP_CREDIT_BASED_CONNECT_RSP =
193       38, /* accepted credit based connection */
194   L2CEVT_L2CAP_CREDIT_BASED_CONNECT_RSP_NEG =
195       39, /* rejected credit based connection */
196   L2CEVT_L2CAP_CREDIT_BASED_RECONFIG_REQ =
197       40, /* credit based reconfig request*/
198   L2CEVT_L2CAP_CREDIT_BASED_RECONFIG_RSP =
199       41, /* credit based reconfig response */
200 
201   /* Upper layer credit based connection */
202   L2CEVT_L2CA_CREDIT_BASED_CONNECT_REQ = 42,     /* connect request */
203   L2CEVT_L2CA_CREDIT_BASED_CONNECT_RSP = 43,     /* connect response */
204   L2CEVT_L2CA_CREDIT_BASED_CONNECT_RSP_NEG = 44, /* connect response (failed)*/
205   L2CEVT_L2CA_CREDIT_BASED_RECONFIG_REQ = 45,    /* reconfig request */
206 } tL2CEVT;
207 
208 /* Constants for LE Dynamic PSM values */
209 #define LE_DYNAMIC_PSM_START 0x0080
210 #define LE_DYNAMIC_PSM_END 0x00FF
211 #define LE_DYNAMIC_PSM_RANGE (LE_DYNAMIC_PSM_END - LE_DYNAMIC_PSM_START + 1)
212 
213 /* Return values for l2cu_process_peer_cfg_req() */
214 #define L2CAP_PEER_CFG_UNACCEPTABLE 0
215 #define L2CAP_PEER_CFG_OK 1
216 #define L2CAP_PEER_CFG_DISCONNECT 2
217 
218 /* eL2CAP option constants */
219 /* Min retransmission timeout if no flush timeout or PBF */
220 #define L2CAP_MIN_RETRANS_TOUT 2000
221 /* Min monitor timeout if no flush timeout or PBF */
222 #define L2CAP_MIN_MONITOR_TOUT 12000
223 
224 #define L2CAP_MAX_FCR_CFG_TRIES 2 /* Config attempts before disconnecting */
225 
226 typedef uint8_t tL2C_BLE_FIXED_CHNLS_MASK;
227 
228 typedef struct {
229   uint8_t next_tx_seq;       /* Next sequence number to be Tx'ed */
230   uint8_t last_rx_ack;       /* Last sequence number ack'ed by the peer */
231   uint8_t next_seq_expected; /* Next peer sequence number expected */
232   uint8_t last_ack_sent;     /* Last peer sequence number ack'ed */
233   uint8_t num_tries;         /* Number of retries to send a packet */
234   uint8_t max_held_acks;     /* Max acks we can hold before sending */
235 
236   bool remote_busy; /* true if peer has flowed us off */
237 
238   bool rej_sent;       /* Reject was sent */
239   bool srej_sent;      /* Selective Reject was sent */
240   bool wait_ack;       /* Transmitter is waiting ack (poll sent) */
241   bool rej_after_srej; /* Send a REJ when SREJ clears */
242 
243   bool send_f_rsp; /* We need to send an F-bit response */
244 
245   uint16_t rx_sdu_len; /* Length of the SDU being received */
246   BT_HDR* p_rx_sdu;    /* Buffer holding the SDU being received */
247   fixed_queue_t*
248       waiting_for_ack_q;          /* Buffers sent and waiting for peer to ack */
249   fixed_queue_t* srej_rcv_hold_q; /* Buffers rcvd but held pending SREJ rsp */
250   fixed_queue_t* retrans_q;       /* Buffers being retransmitted */
251 
252   alarm_t* ack_timer;         /* Timer delaying RR */
253   alarm_t* mon_retrans_timer; /* Timer Monitor or Retransmission */
254 
255 } tL2C_FCRB;
256 
257 typedef struct {
258   bool in_use;
259   bool log_packets;
260   uint16_t psm;
261   uint16_t real_psm; /* This may be a dummy RCB for an o/b connection but */
262                      /* this is the real PSM that we need to connect to */
263   tL2CAP_APPL_INFO api;
264   tL2CAP_ERTM_INFO ertm_info;
265   tL2CAP_LE_CFG_INFO coc_cfg;
266   uint16_t my_mtu;
267   uint16_t required_remote_mtu;
268 } tL2C_RCB;
269 
270 #ifndef L2CAP_CBB_DEFAULT_DATA_RATE_BUFF_QUOTA
271 #define L2CAP_CBB_DEFAULT_DATA_RATE_BUFF_QUOTA 100
272 #endif
273 
274 typedef void(tL2CAP_SEC_CBACK)(const RawAddress& bd_addr,
275                                tBT_TRANSPORT trasnport, void* p_ref_data,
276                                tBTM_STATUS result);
277 
278 typedef struct {
279   uint16_t psm;
280   tBT_TRANSPORT transport;
281   bool is_originator;
282   tL2CAP_SEC_CBACK* p_callback;
283   void* p_ref_data;
284 } tL2CAP_SEC_DATA;
285 
286 /* Define a channel control block (CCB). There may be many channel control
287  * blocks between the same two Bluetooth devices (i.e. on the same link).
288  * Each CCB has unique local and remote CIDs. All channel control blocks on
289  * the same physical link and are chained together.
290 */
291 typedef struct t_l2c_ccb {
292   bool in_use;                /* true when in use, false when not */
293   tL2C_CHNL_STATE chnl_state; /* Channel state */
294   tL2CAP_LE_CFG_INFO
295       local_conn_cfg; /* Our config for ble conn oriented channel */
296   tL2CAP_LE_CFG_INFO
297       peer_conn_cfg;       /* Peer device config ble conn oriented channel */
298   bool is_first_seg;       /* Dtermine whether the received packet is the first
299                               segment or not */
300   BT_HDR* ble_sdu;         /* Buffer for storing unassembled sdu*/
301   uint16_t ble_sdu_length; /* Length of unassembled sdu length*/
302   struct t_l2c_ccb* p_next_ccb; /* Next CCB in the chain */
303   struct t_l2c_ccb* p_prev_ccb; /* Previous CCB in the chain */
304   struct t_l2c_linkcb* p_lcb;   /* Link this CCB is assigned to */
305 
306   uint16_t local_cid;  /* Local CID */
307   uint16_t remote_cid; /* Remote CID */
308 
309   alarm_t* l2c_ccb_timer; /* CCB Timer Entry */
310 
311   tL2C_RCB* p_rcb;      /* Registration CB for this Channel */
312 
313 #define IB_CFG_DONE 0x01
314 #define OB_CFG_DONE 0x02
315 #define RECONFIG_FLAG 0x04 /* True after initial configuration */
316 
317   uint8_t config_done; /* Configuration flag word */
318   uint16_t remote_config_rsp_result; /* The config rsp result from remote */
319   uint8_t local_id;    /* Transaction ID for local trans */
320   uint8_t remote_id;   /* Transaction ID for local */
321 
322 #define CCB_FLAG_NO_RETRY 0x01     /* no more retry */
323 #define CCB_FLAG_SENT_PENDING 0x02 /* already sent pending response */
324   uint8_t flags;
325 
326   bool connection_initiator; /* true if we sent ConnectReq */
327 
328   tL2CAP_CFG_INFO our_cfg;          /* Our saved configuration options */
329   tL2CAP_CFG_INFO peer_cfg;         /* Peer's saved configuration options */
330 
331   fixed_queue_t* xmit_hold_q; /* Transmit data hold queue */
332   bool cong_sent;             /* Set when congested status sent */
333   uint16_t buff_quota;        /* Buffer quota before sending congestion */
334 
335   tL2CAP_CHNL_PRIORITY ccb_priority;  /* Channel priority */
336   tL2CAP_CHNL_DATA_RATE tx_data_rate; /* Channel Tx data rate */
337   tL2CAP_CHNL_DATA_RATE rx_data_rate; /* Channel Rx data rate */
338 
339   /* Fields used for eL2CAP */
340   tL2CAP_ERTM_INFO ertm_info;
341   tL2C_FCRB fcrb;
342   uint16_t tx_mps; /* TX MPS adjusted based on current controller */
343   uint16_t max_rx_mtu;
344   uint8_t fcr_cfg_tries;          /* Max number of negotiation attempts */
345   bool peer_cfg_already_rejected; /* If mode rejected once, set to true */
346   bool out_cfg_fcr_present; /* true if cfg response should include fcr options
347                              */
348 
349   bool is_flushable; /* true if channel is flushable */
350 
351   uint16_t fixed_chnl_idle_tout; /* Idle timeout to use for the fixed channel */
352   uint16_t tx_data_len;
353 
354   /* Number of LE frames that the remote can send to us (credit count in
355    * remote). Valid only for LE CoC */
356   uint16_t remote_credit_count;
357 
358   /* used to indicate that ECOC is used */
359   bool ecoc{false};
360   bool reconfig_started;
361 
362   struct {
363     struct {
364       unsigned bytes{0};
365       unsigned packets{0};
operatort_l2c_ccb::__anonaddf187a0708::__anonaddf187a0808366       void operator()(unsigned bytes) {
367         this->bytes += bytes;
368         this->packets++;
369       }
370     } rx, tx;
371     struct {
372       struct {
373         unsigned bytes{0};
374         unsigned packets{0};
operatort_l2c_ccb::__anonaddf187a0708::__anonaddf187a0908::__anonaddf187a0a08375         void operator()(unsigned bytes) {
376           this->bytes += bytes;
377           this->packets++;
378         }
379       } rx, tx;
380     } dropped;
381   } metrics;
382 
383 } tL2C_CCB;
384 
385 /***********************************************************************
386  * Define a queue of linked CCBs.
387 */
388 typedef struct {
389   tL2C_CCB* p_first_ccb; /* The first channel in this queue */
390   tL2C_CCB* p_last_ccb;  /* The last  channel in this queue */
391 } tL2C_CCB_Q;
392 
393 /* Round-Robin service for the same priority channels */
394 #define L2CAP_NUM_CHNL_PRIORITY \
395   3 /* Total number of priority group (high, medium, low)*/
396 #define L2CAP_CHNL_PRIORITY_WEIGHT \
397   5 /* weight per priority for burst transmission quota */
398 #define L2CAP_GET_PRIORITY_QUOTA(pri) \
399   ((L2CAP_NUM_CHNL_PRIORITY - (pri)) * L2CAP_CHNL_PRIORITY_WEIGHT)
400 
401 /* CCBs within the same LCB are served in round robin with priority It will make
402  * sure that low priority channel (for example, HF signaling on RFCOMM) can be
403  * sent to the headset even if higher priority channel (for example, AV media
404  * channel) is congested.
405  */
406 
407 typedef struct {
408   tL2C_CCB* p_serve_ccb; /* current serving ccb within priority group */
409   tL2C_CCB* p_first_ccb; /* first ccb of priority group */
410   uint8_t num_ccb;       /* number of channels in priority group */
411   uint8_t quota;         /* burst transmission quota */
412 } tL2C_RR_SERV;
413 
414 typedef enum : uint8_t {
415   /* disable update connection parameters */
416   L2C_BLE_CONN_UPDATE_DISABLE = (1u << 0),
417   /* new connection parameter to be set */
418   L2C_BLE_NEW_CONN_PARAM = (1u << 1),
419   /* waiting for connection update finished */
420   L2C_BLE_UPDATE_PENDING = (1u << 2),
421   /* not using default connection parameters */
422   L2C_BLE_NOT_DEFAULT_PARAM = (1u << 3),
423 } tCONN_UPDATE_MASK;
424 
425 /* Define a link control block. There is one link control block between
426  * this device and any other device (i.e. BD ADDR).
427 */
428 typedef struct t_l2c_linkcb {
429   bool in_use; /* true when in use, false when not */
430   tL2C_LINK_STATE link_state;
431 
432   alarm_t* l2c_lcb_timer; /* Timer entry for timeout evt */
433 
434   //  This tracks if the link has ever either (a)
435   //  been used for a dynamic channel (EATT or L2CAP CoC), or (b) has been a
436   //  GATT client. If false, the local device is just a GATT server, so for
437   //  backwards compatibility we never do a link timeout.
438   bool with_active_local_clients{false};
439 
440  private:
441   uint16_t handle_; /* The handle used with LM */
442   friend void l2cu_set_lcb_handle(struct t_l2c_linkcb& p_lcb, uint16_t handle);
SetHandlet_l2c_linkcb443   void SetHandle(uint16_t handle) { handle_ = handle; }
444 
445  public:
Handlet_l2c_linkcb446   uint16_t Handle() const { return handle_; }
InvalidateHandlet_l2c_linkcb447   void InvalidateHandle() { handle_ = HCI_INVALID_HANDLE; }
448 
449   tL2C_CCB_Q ccb_queue; /* Queue of CCBs on this LCB */
450 
451   tL2C_CCB* p_pending_ccb;  /* ccb of waiting channel during link disconnect */
452   alarm_t* info_resp_timer; /* Timer entry for info resp timeout evt */
453   RawAddress remote_bd_addr; /* The BD address of the remote */
454 
455  private:
456   tHCI_ROLE link_role_{HCI_ROLE_CENTRAL}; /* Central or peripheral */
457  public:
LinkRolet_l2c_linkcb458   tHCI_ROLE LinkRole() const { return link_role_; }
IsLinkRoleCentralt_l2c_linkcb459   bool IsLinkRoleCentral() const { return link_role_ == HCI_ROLE_CENTRAL; }
IsLinkRolePeripheralt_l2c_linkcb460   bool IsLinkRolePeripheral() const {
461     return link_role_ == HCI_ROLE_PERIPHERAL;
462   }
SetLinkRoleAsCentralt_l2c_linkcb463   void SetLinkRoleAsCentral() { link_role_ = HCI_ROLE_CENTRAL; }
SetLinkRoleAsPeripheralt_l2c_linkcb464   void SetLinkRoleAsPeripheral() { link_role_ = HCI_ROLE_PERIPHERAL; }
465 
466   uint8_t signal_id;                /* Signalling channel id */
467   uint8_t cur_echo_id;              /* Current id value for echo request */
468   uint16_t idle_timeout;            /* Idle timeout */
469  private:
470   bool is_bonding_{false};          /* True - link active only for bonding */
471  public:
IsBondingt_l2c_linkcb472   bool IsBonding() const { return is_bonding_; }
SetBondingt_l2c_linkcb473   void SetBonding() { is_bonding_ = true; }
ResetBondingt_l2c_linkcb474   void ResetBonding() { is_bonding_ = false; }
475 
476   uint16_t link_xmit_quota; /* Num outstanding pkts allowed */
is_round_robin_schedulingt_l2c_linkcb477   bool is_round_robin_scheduling() const { return link_xmit_quota == 0; }
478 
479   uint16_t sent_not_acked;  /* Num packets sent but not acked */
update_outstanding_packetst_l2c_linkcb480   void update_outstanding_packets(uint16_t packets_acked) {
481     if (sent_not_acked > packets_acked)
482       sent_not_acked -= packets_acked;
483     else
484       sent_not_acked = 0;
485   }
486 
487   bool w4_info_rsp;                /* true when info request is active */
488   uint32_t peer_ext_fea;           /* Peer's extended features mask */
489   list_t* link_xmit_data_q;        /* Link transmit data buffer queue */
490 
491   uint8_t peer_chnl_mask[L2CAP_FIXED_CHNL_ARRAY_SIZE];
492 
493   tL2CAP_PRIORITY acl_priority;
is_normal_priorityt_l2c_linkcb494   bool is_normal_priority() const {
495     return acl_priority == L2CAP_PRIORITY_NORMAL;
496   }
is_high_priorityt_l2c_linkcb497   bool is_high_priority() const { return acl_priority == L2CAP_PRIORITY_HIGH; }
set_priorityt_l2c_linkcb498   bool set_priority(tL2CAP_PRIORITY priority) {
499     if (acl_priority != priority) {
500       acl_priority = priority;
501       return true;
502     }
503     return false;
504   }
505 
506   bool use_latency_mode = false;
507   tL2CAP_LATENCY preset_acl_latency = L2CAP_LATENCY_NORMAL;
508   tL2CAP_LATENCY acl_latency = L2CAP_LATENCY_NORMAL;
is_normal_latencyt_l2c_linkcb509   bool is_normal_latency() const { return acl_latency == L2CAP_LATENCY_NORMAL; }
is_low_latencyt_l2c_linkcb510   bool is_low_latency() const { return acl_latency == L2CAP_LATENCY_LOW; }
set_latencyt_l2c_linkcb511   bool set_latency(tL2CAP_LATENCY latency) {
512     if (acl_latency != latency) {
513       acl_latency = latency;
514       return true;
515     }
516     return false;
517   }
518 
519   tL2C_CCB* p_fixed_ccbs[L2CAP_NUM_FIXED_CHNLS];
520 
521  private:
522   tHCI_REASON disc_reason_{HCI_ERR_UNDEFINED};
523 
524  public:
DisconnectReasont_l2c_linkcb525   tHCI_REASON DisconnectReason() const { return disc_reason_; }
SetDisconnectReasont_l2c_linkcb526   void SetDisconnectReason(tHCI_REASON disc_reason) {
527     disc_reason_ = disc_reason;
528   }
529 
530   tBT_TRANSPORT transport;
is_transport_br_edrt_l2c_linkcb531   bool is_transport_br_edr() const { return transport == BT_TRANSPORT_BR_EDR; }
is_transport_blet_l2c_linkcb532   bool is_transport_ble() const { return transport == BT_TRANSPORT_LE; }
533 
534   uint16_t tx_data_len; /* tx data length used in data length extension */
535   fixed_queue_t* le_sec_pending_q; /* LE coc channels waiting for security check
536                                       completion */
537   uint8_t sec_act;
538 
539   uint8_t conn_update_mask;
540 
541   uint16_t min_interval; /* parameters as requested by peripheral */
542   uint16_t max_interval;
543   uint16_t latency;
544   uint16_t timeout;
545   uint16_t min_ce_len;
546   uint16_t max_ce_len;
547 
548 #define L2C_BLE_SUBRATE_REQ_DISABLE 0x1  // disable subrate req
549 #define L2C_BLE_NEW_SUBRATE_PARAM 0x2    // new subrate req parameter to be set
550 #define L2C_BLE_SUBRATE_REQ_PENDING 0x4  // waiting for subrate to be completed
551 
552   /* subrate req params */
553   uint16_t subrate_min;
554   uint16_t subrate_max;
555   uint16_t max_latency;
556   uint16_t cont_num;
557   uint16_t supervision_tout;
558 
559   uint8_t subrate_req_mask;
560 
561   /* each priority group is limited burst transmission */
562   /* round robin service for the same priority channels */
563   tL2C_RR_SERV rr_serv[L2CAP_NUM_CHNL_PRIORITY];
564   uint8_t rr_pri; /* current serving priority group */
565 
566   /* Pending ECOC reconfiguration data */
567   tL2CAP_LE_CFG_INFO pending_ecoc_reconfig_cfg;
568   uint8_t pending_ecoc_reconfig_cnt;
569 
570   /* This is to keep list of local cids use in the
571    * credit based connection response.
572    */
573   uint16_t pending_ecoc_connection_cids[L2CAP_CREDIT_BASED_MAX_CIDS];
574   uint8_t pending_ecoc_conn_cnt;
575 
576   uint16_t pending_lead_cid;
577   uint16_t pending_l2cap_result;
578 
number_of_active_dynamic_channelst_l2c_linkcb579   unsigned number_of_active_dynamic_channels() const {
580     unsigned cnt = 0;
581     const tL2C_CCB* cur = ccb_queue.p_first_ccb;
582     while (cur != nullptr) {
583       cnt++;
584       cur = cur->p_next_ccb;
585     }
586     return cnt;
587   }
588 } tL2C_LCB;
589 
590 /* Define the L2CAP control structure
591 */
592 typedef struct {
593   uint8_t l2cap_trace_level;
594   uint16_t controller_xmit_window; /* Total ACL window for all links */
595 
596   uint16_t round_robin_quota;   /* Round-robin link quota */
597   uint16_t round_robin_unacked; /* Round-robin unacked */
is_classic_round_robin_quota_available__anonaddf187a0e08598   bool is_classic_round_robin_quota_available() const {
599     return round_robin_unacked < round_robin_quota;
600   }
update_outstanding_classic_packets__anonaddf187a0e08601   void update_outstanding_classic_packets(uint16_t num_packets_acked) {
602     if (round_robin_unacked > num_packets_acked)
603       round_robin_unacked -= num_packets_acked;
604     else
605       round_robin_unacked = 0;
606   }
607 
608   bool check_round_robin;       /* Do a round robin check */
609 
610   bool is_cong_cback_context;
611 
612   tL2C_LCB lcb_pool[MAX_L2CAP_LINKS];    /* Link Control Block pool */
613   tL2C_CCB ccb_pool[MAX_L2CAP_CHANNELS]; /* Channel Control Block pool */
614   tL2C_RCB rcb_pool[MAX_L2CAP_CLIENTS];  /* Registration info pool */
615 
616   tL2C_CCB* p_free_ccb_first; /* Pointer to first free CCB */
617   tL2C_CCB* p_free_ccb_last;  /* Pointer to last  free CCB */
618 
619   bool disallow_switch;     /* false, to allow switch at create conn */
620   uint16_t num_lm_acl_bufs; /* # of ACL buffers on controller */
621   uint16_t idle_timeout;    /* Idle timeout */
622 
623   tL2C_LCB* p_cur_hcit_lcb;  /* Current HCI Transport buffer */
624   uint16_t num_used_lcbs;    /* Number of active link control blocks */
625 
626   uint16_t non_flushable_pbf; /* L2CAP_PKT_START_NON_FLUSHABLE if controller
627                                  supports */
628   /* Otherwise, L2CAP_PKT_START */
629 
630 #if (L2CAP_CONFORMANCE_TESTING == TRUE)
631   uint32_t test_info_resp; /* Conformance testing needs a dynamic response */
632 #endif
633 
634   tL2CAP_FIXED_CHNL_REG
635       fixed_reg[L2CAP_NUM_FIXED_CHNLS]; /* Reg info for fixed channels */
636 
637   uint16_t num_ble_links_active; /* Number of LE links active */
638   uint16_t controller_le_xmit_window; /* Total ACL window for all links */
639   tL2C_BLE_FIXED_CHNLS_MASK l2c_ble_fixed_chnls_mask;  // LE fixed channels mask
640   uint16_t num_lm_ble_bufs;         /* # of ACL buffers on controller */
641   uint16_t ble_round_robin_quota;   /* Round-robin link quota */
642   uint16_t ble_round_robin_unacked; /* Round-robin unacked */
is_ble_round_robin_quota_available__anonaddf187a0e08643   bool is_ble_round_robin_quota_available() const {
644     return ble_round_robin_unacked < ble_round_robin_quota;
645   }
update_outstanding_le_packets__anonaddf187a0e08646   void update_outstanding_le_packets(uint16_t num_packets_acked) {
647     if (ble_round_robin_unacked > num_packets_acked)
648       ble_round_robin_unacked -= num_packets_acked;
649     else
650       ble_round_robin_unacked = 0;
651   }
652 
653   bool ble_check_round_robin;       /* Do a round robin check */
654   tL2C_RCB ble_rcb_pool[BLE_MAX_L2CAP_CLIENTS]; /* Registration info pool */
655 
656   uint16_t le_dyn_psm; /* Next LE dynamic PSM value to try to assign */
657   bool le_dyn_psm_assigned[LE_DYNAMIC_PSM_RANGE]; /* Table of assigned LE PSM */
658 
659 } tL2C_CB;
660 
661 /* Define a structure that contains the information about a connection.
662  * This structure is used to pass between functions, and not all the
663  * fields will always be filled in.
664 */
665 typedef struct {
666   RawAddress bd_addr;    /* Remote BD address */
667   uint8_t status;        /* Connection status */
668   uint16_t psm;          /* PSM of the connection */
669   uint16_t l2cap_result; /* L2CAP result */
670   uint16_t l2cap_status; /* L2CAP status */
671   uint16_t remote_cid;   /* Remote CID */
672   std::vector<uint16_t> lcids; /* Used when credit based is used*/
673   uint16_t peer_mtu;     /* Peer MTU */
674 } tL2C_CONN_INFO;
675 
676 typedef struct {
677   bool is_active;     /* is channel active */
678   uint16_t local_cid; /* Remote CID */
679   tL2C_CCB* p_ccb;    /* CCB */
680 } tL2C_AVDT_CHANNEL_INFO;
681 
682 typedef void(tL2C_FCR_MGMT_EVT_HDLR)(uint8_t, tL2C_CCB*);
683 
684 /* The offset in a buffer that L2CAP will use when building commands.
685 */
686 #define L2CAP_SEND_CMD_OFFSET 0
687 
688 /* Number of ACL buffers to use for high priority channel
689 */
690 #define L2CAP_HIGH_PRI_MIN_XMIT_QUOTA_A (L2CAP_HIGH_PRI_MIN_XMIT_QUOTA)
691 
692 /* L2CAP global data
693  ***********************************
694 */
695 extern tL2C_CB l2cb;
696 
697 /* Functions provided by l2c_main.cc
698  ***********************************
699 */
700 
701 void l2c_receive_hold_timer_timeout(void* data);
702 void l2c_ccb_timer_timeout(void* data);
703 void l2c_lcb_timer_timeout(void* data);
704 void l2c_fcrb_ack_timer_timeout(void* data);
705 uint8_t l2c_data_write(uint16_t cid, BT_HDR* p_data, uint16_t flag);
706 
707 tL2C_LCB* l2cu_allocate_lcb(const RawAddress& p_bd_addr, bool is_bonding,
708                             tBT_TRANSPORT transport);
709 void l2cu_release_lcb(tL2C_LCB* p_lcb);
710 tL2C_LCB* l2cu_find_lcb_by_bd_addr(const RawAddress& p_bd_addr,
711                                    tBT_TRANSPORT transport);
712 tL2C_LCB* l2cu_find_lcb_by_handle(uint16_t handle);
713 
714 bool l2cu_set_acl_priority(const RawAddress& bd_addr, tL2CAP_PRIORITY priority,
715                            bool reset_after_rs);
716 bool l2cu_set_acl_latency(const RawAddress& bd_addr, tL2CAP_LATENCY latency);
717 
718 void l2cu_enqueue_ccb(tL2C_CCB* p_ccb);
719 void l2cu_dequeue_ccb(tL2C_CCB* p_ccb);
720 void l2cu_change_pri_ccb(tL2C_CCB* p_ccb, tL2CAP_CHNL_PRIORITY priority);
721 
722 tL2C_CCB* l2cu_allocate_ccb(tL2C_LCB* p_lcb, uint16_t cid);
723 void l2cu_release_ccb(tL2C_CCB* p_ccb);
724 tL2C_CCB* l2cu_find_ccb_by_cid(tL2C_LCB* p_lcb, uint16_t local_cid);
725 tL2C_CCB* l2cu_find_ccb_by_remote_cid(tL2C_LCB* p_lcb, uint16_t remote_cid);
726 bool l2c_is_cmd_rejected(uint8_t cmd_code, uint8_t id, tL2C_LCB* p_lcb);
727 
728 void l2cu_send_peer_cmd_reject(tL2C_LCB* p_lcb, uint16_t reason, uint8_t rem_id,
729                                uint16_t p1, uint16_t p2);
730 void l2cu_send_peer_connect_req(tL2C_CCB* p_ccb);
731 void l2cu_send_peer_connect_rsp(tL2C_CCB* p_ccb, uint16_t result,
732                                 uint16_t status);
733 void l2cu_send_peer_config_req(tL2C_CCB* p_ccb, tL2CAP_CFG_INFO* p_cfg);
734 void l2cu_send_peer_config_rsp(tL2C_CCB* p_ccb, tL2CAP_CFG_INFO* p_cfg);
735 void l2cu_send_peer_config_rej(tL2C_CCB* p_ccb, uint8_t* p_data,
736                                uint16_t data_len, uint16_t rej_len);
737 void l2cu_send_peer_disc_req(tL2C_CCB* p_ccb);
738 void l2cu_send_peer_disc_rsp(tL2C_LCB* p_lcb, uint8_t remote_id,
739                              uint16_t local_cid, uint16_t remote_cid);
740 void l2cu_send_peer_echo_rsp(tL2C_LCB* p_lcb, uint8_t id, uint8_t* p_data,
741                              uint16_t data_len);
742 void l2cu_send_peer_info_rsp(tL2C_LCB* p_lcb, uint8_t id, uint16_t info_type);
743 void l2cu_reject_connection(tL2C_LCB* p_lcb, uint16_t remote_cid,
744                             uint8_t rem_id, uint16_t result);
745 void l2cu_send_peer_info_req(tL2C_LCB* p_lcb, uint16_t info_type);
746 void l2cu_set_acl_hci_header(BT_HDR* p_buf, tL2C_CCB* p_ccb);
747 void l2cu_check_channel_congestion(tL2C_CCB* p_ccb);
748 void l2cu_disconnect_chnl(tL2C_CCB* p_ccb);
749 
750 void l2cu_send_peer_ble_par_req(tL2C_LCB* p_lcb, uint16_t min_int,
751                                 uint16_t max_int, uint16_t latency,
752                                 uint16_t timeout);
753 void l2cu_send_peer_ble_par_rsp(tL2C_LCB* p_lcb, uint16_t reason,
754                                 uint8_t rem_id);
755 void l2cu_reject_ble_connection(tL2C_CCB* p_ccb, uint8_t rem_id,
756                                 uint16_t result);
757 void l2cu_reject_credit_based_conn_req(tL2C_LCB* p_lcb, uint8_t rem_id,
758                                        uint8_t num_of_channels,
759                                        uint16_t result);
760 void l2cu_reject_ble_coc_connection(tL2C_LCB* p_lcb, uint8_t rem_id,
761                                     uint16_t result);
762 void l2cu_send_peer_ble_credit_based_conn_res(tL2C_CCB* p_ccb, uint16_t result);
763 void l2cu_send_peer_credit_based_conn_res(tL2C_CCB* p_ccb,
764                                           std::vector<uint16_t>& accepted_lcids,
765                                           uint16_t result);
766 
767 void l2cu_send_peer_ble_credit_based_conn_req(tL2C_CCB* p_ccb);
768 void l2cu_send_peer_credit_based_conn_req(tL2C_CCB* p_ccb);
769 
770 void l2cu_send_ble_reconfig_rsp(tL2C_LCB* p_lcb, uint8_t rem_id,
771                                 uint16_t result);
772 void l2cu_send_credit_based_reconfig_req(tL2C_CCB* p_ccb,
773                                          tL2CAP_LE_CFG_INFO* p_data);
774 
775 void l2cu_send_peer_ble_flow_control_credit(tL2C_CCB* p_ccb,
776                                             uint16_t credit_value);
777 void l2cu_send_peer_ble_credit_based_disconn_req(tL2C_CCB* p_ccb);
778 
779 bool l2cu_initialize_fixed_ccb(tL2C_LCB* p_lcb, uint16_t fixed_cid);
780 void l2cu_no_dynamic_ccbs(tL2C_LCB* p_lcb);
781 void l2cu_process_fixed_chnl_resp(tL2C_LCB* p_lcb);
782 bool l2cu_is_ccb_active(tL2C_CCB* p_ccb);
783 uint16_t le_result_to_l2c_conn(uint16_t result);
784 
785 /* Functions provided for Broadcom Aware
786  ***************************************
787 */
788 
789 tL2C_RCB* l2cu_allocate_rcb(uint16_t psm);
790 tL2C_RCB* l2cu_find_rcb_by_psm(uint16_t psm);
791 void l2cu_release_rcb(tL2C_RCB* p_rcb);
792 void l2cu_release_ble_rcb(tL2C_RCB* p_rcb);
793 tL2C_RCB* l2cu_allocate_ble_rcb(uint16_t psm);
794 tL2C_RCB* l2cu_find_ble_rcb_by_psm(uint16_t psm);
795 
796 uint8_t l2cu_process_peer_cfg_req(tL2C_CCB* p_ccb, tL2CAP_CFG_INFO* p_cfg);
797 void l2cu_process_peer_cfg_rsp(tL2C_CCB* p_ccb, tL2CAP_CFG_INFO* p_cfg);
798 void l2cu_process_our_cfg_req(tL2C_CCB* p_ccb, tL2CAP_CFG_INFO* p_cfg);
799 void l2cu_process_our_cfg_rsp(tL2C_CCB* p_ccb, tL2CAP_CFG_INFO* p_cfg);
800 
801 tL2C_LCB* l2cu_find_lcb_by_state(tL2C_LINK_STATE state);
802 bool l2cu_lcb_disconnecting(void);
803 
804 void l2cu_create_conn_br_edr(tL2C_LCB* p_lcb);
805 bool l2cu_create_conn_le(tL2C_LCB* p_lcb);
806 void l2cu_create_conn_after_switch(tL2C_LCB* p_lcb);
807 void l2cu_adjust_out_mps(tL2C_CCB* p_ccb);
808 
809 /* Functions provided by l2c_link.cc
810  ***********************************
811 */
812 void l2c_link_timeout(tL2C_LCB* p_lcb);
813 void l2c_info_resp_timer_timeout(void* data);
814 void l2c_link_check_send_pkts(tL2C_LCB* p_lcb, uint16_t local_cid,
815                               BT_HDR* p_buf);
816 void l2c_link_adjust_allocation(void);
817 
818 void l2c_link_sec_comp(const RawAddress* p_bda, tBT_TRANSPORT trasnport,
819                        void* p_ref_data, tBTM_STATUS status);
820 void l2c_link_sec_comp2(const RawAddress& p_bda, tBT_TRANSPORT trasnport,
821                         void* p_ref_data, tBTM_STATUS status);
822 void l2c_link_adjust_chnl_allocation(void);
823 
824 #if (L2CAP_CONFORMANCE_TESTING == TRUE)
825 /* Used only for conformance testing */
826 void l2cu_set_info_rsp_mask(uint32_t mask);
827 #endif
828 
829 /* Functions provided by l2c_csm.cc
830  ***********************************
831 */
832 void l2c_csm_execute(tL2C_CCB* p_ccb, tL2CEVT event, void* p_data);
833 
834 void l2c_enqueue_peer_data(tL2C_CCB* p_ccb, BT_HDR* p_buf);
835 
836 /* Functions provided by l2c_fcr.cc
837  ***********************************
838 */
839 void l2c_fcr_cleanup(tL2C_CCB* p_ccb);
840 void l2c_fcr_proc_pdu(tL2C_CCB* p_ccb, BT_HDR* p_buf);
841 void l2c_fcr_proc_tout(tL2C_CCB* p_ccb);
842 void l2c_fcr_proc_ack_tout(tL2C_CCB* p_ccb);
843 void l2c_fcr_send_S_frame(tL2C_CCB* p_ccb, uint16_t function_code,
844                           uint16_t pf_bit);
845 BT_HDR* l2c_fcr_clone_buf(BT_HDR* p_buf, uint16_t new_offset,
846                           uint16_t no_of_bytes);
847 bool l2c_fcr_is_flow_controlled(tL2C_CCB* p_ccb);
848 BT_HDR* l2c_fcr_get_next_xmit_sdu_seg(tL2C_CCB* p_ccb,
849                                       uint16_t max_packet_length);
850 void l2c_fcr_start_timer(tL2C_CCB* p_ccb);
851 void l2c_lcc_proc_pdu(tL2C_CCB* p_ccb, BT_HDR* p_buf);
852 BT_HDR* l2c_lcc_get_next_xmit_sdu_seg(tL2C_CCB* p_ccb, bool* last_piece_of_sdu);
853 
854 /* Configuration negotiation */
855 uint8_t l2c_fcr_chk_chan_modes(tL2C_CCB* p_ccb);
856 
857 void l2c_fcr_adj_our_rsp_options(tL2C_CCB* p_ccb, tL2CAP_CFG_INFO* p_peer_cfg);
858 bool l2c_fcr_renegotiate_chan(tL2C_CCB* p_ccb, tL2CAP_CFG_INFO* p_cfg);
859 uint8_t l2c_fcr_process_peer_cfg_req(tL2C_CCB* p_ccb, tL2CAP_CFG_INFO* p_cfg);
860 void l2c_fcr_adj_monitor_retran_timeout(tL2C_CCB* p_ccb);
861 void l2c_fcr_stop_timer(tL2C_CCB* p_ccb);
862 
863 /* Functions provided by l2c_ble.cc
864  ***********************************
865 */
866 bool l2cble_create_conn(tL2C_LCB* p_lcb);
867 void l2cble_process_sig_cmd(tL2C_LCB* p_lcb, uint8_t* p, uint16_t pkt_len);
868 void l2c_ble_link_adjust_allocation(void);
869 
870 void l2cble_credit_based_conn_req(tL2C_CCB* p_ccb);
871 void l2cble_credit_based_conn_res(tL2C_CCB* p_ccb, uint16_t result);
872 void l2cble_send_peer_disc_req(tL2C_CCB* p_ccb);
873 void l2cble_send_flow_control_credit(tL2C_CCB* p_ccb, uint16_t credit_value);
874 tL2CAP_LE_RESULT_CODE l2ble_sec_access_req(const RawAddress& bd_addr,
875                                            uint16_t psm, bool is_originator,
876                                            tL2CAP_SEC_CBACK* p_callback,
877                                            void* p_ref_data);
878 
879 void l2cble_update_data_length(tL2C_LCB* p_lcb);
880 
881 void l2cu_process_fixed_disc_cback(tL2C_LCB* p_lcb);
882 
883 void l2cble_process_subrate_change_evt(uint16_t handle, uint8_t status,
884                                        uint16_t subrate_factor,
885                                        uint16_t peripheral_latency,
886                                        uint16_t cont_num, uint16_t timeout);
887 
888 #endif
889