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