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