1 /** @file
2 * @brief Internal APIs for Bluetooth connection handling.
3 */
4
5 /*
6 * Copyright (c) 2015 Intel Corporation
7 *
8 * SPDX-License-Identifier: Apache-2.0
9 */
10 typedef enum __packed {
11 BT_CONN_DISCONNECTED,
12 BT_CONN_DISCONNECT_COMPLETE,
13 BT_CONN_CONNECT_SCAN,
14 BT_CONN_CONNECT_AUTO,
15 BT_CONN_CONNECT_ADV,
16 BT_CONN_CONNECT_DIR_ADV,
17 BT_CONN_CONNECT,
18 BT_CONN_CONNECTED,
19 BT_CONN_DISCONNECT,
20 } bt_conn_state_t;
21
22 /* bt_conn flags: the flags defined here represent connection parameters */
23 enum {
24 BT_CONN_AUTO_CONNECT,
25 BT_CONN_BR_LEGACY_SECURE, /* 16 digits legacy PIN tracker */
26 BT_CONN_USER, /* user I/O when pairing */
27 BT_CONN_BR_PAIRING, /* BR connection in pairing context */
28 BT_CONN_BR_NOBOND, /* SSP no bond pairing tracker */
29 BT_CONN_BR_PAIRING_INITIATOR, /* local host starts authentication */
30 BT_CONN_CLEANUP, /* Disconnected, pending cleanup */
31 BT_CONN_AUTO_PHY_UPDATE, /* Auto-update PHY */
32 BT_CONN_PERIPHERAL_PARAM_UPDATE,/* If periph param update timer fired */
33 BT_CONN_PERIPHERAL_PARAM_SET, /* If periph param were set from app */
34 BT_CONN_PERIPHERAL_PARAM_L2CAP, /* If should force L2CAP for CPUP */
35 BT_CONN_FORCE_PAIR, /* Pairing even with existing keys. */
36
37 BT_CONN_AUTO_PHY_COMPLETE, /* Auto-initiated PHY procedure done */
38 BT_CONN_AUTO_FEATURE_EXCH, /* Auto-initiated LE Feat done */
39 BT_CONN_AUTO_VERSION_INFO, /* Auto-initiated LE version done */
40
41 /* Auto-initiated Data Length done. Auto-initiated Data Length Update
42 * is only needed for controllers with BT_QUIRK_NO_AUTO_DLE. */
43 BT_CONN_AUTO_DATA_LEN_COMPLETE,
44
45 /* Total number of flags - must be at the end of the enum */
46 BT_CONN_NUM_FLAGS,
47 };
48
49 struct bt_conn_le {
50 bt_addr_le_t dst;
51
52 bt_addr_le_t init_addr;
53 bt_addr_le_t resp_addr;
54
55 uint16_t interval;
56 uint16_t interval_min;
57 uint16_t interval_max;
58
59 uint16_t latency;
60 uint16_t timeout;
61 uint16_t pending_latency;
62 uint16_t pending_timeout;
63
64 uint8_t features[8];
65
66 struct bt_keys *keys;
67
68 #if defined(CONFIG_BT_USER_PHY_UPDATE)
69 struct bt_conn_le_phy_info phy;
70 #endif
71
72 #if defined(CONFIG_BT_USER_DATA_LEN_UPDATE)
73 struct bt_conn_le_data_len_info data_len;
74 #endif
75 };
76
77 #if defined(CONFIG_BT_BREDR)
78 /* For now reserve space for 2 pages of LMP remote features */
79 #define LMP_MAX_PAGES 2
80
81 struct bt_conn_br {
82 bt_addr_t dst;
83 uint8_t remote_io_capa;
84 uint8_t remote_auth;
85 uint8_t pairing_method;
86 /* remote LMP features pages per 8 bytes each */
87 uint8_t features[LMP_MAX_PAGES][8];
88
89 struct bt_keys_link_key *link_key;
90 };
91
92 struct bt_conn_sco {
93 /* Reference to ACL Connection */
94 struct bt_conn *acl;
95 uint16_t pkt_type;
96 };
97 #endif
98
99 struct bt_conn_iso {
100 /* Reference to ACL Connection */
101 struct bt_conn *acl;
102
103 /* Reference to the struct bt_iso_chan */
104 struct bt_iso_chan *chan;
105
106 union {
107 /* CIG ID */
108 uint8_t cig_id;
109 /* BIG handle */
110 uint8_t big_handle;
111 };
112
113 union {
114 /* CIS ID within the CIG */
115 uint8_t cis_id;
116
117 /* BIS ID within the BIG*/
118 uint8_t bis_id;
119 };
120
121 /** If true, this is a ISO for a BIS, else it is a ISO for a CIS */
122 bool is_bis;
123 };
124
125 typedef void (*bt_conn_tx_cb_t)(struct bt_conn *conn, void *user_data);
126
127 struct bt_conn_tx {
128 sys_snode_t node;
129
130 bt_conn_tx_cb_t cb;
131 void *user_data;
132
133 /* Number of pending packets without a callback after this one */
134 uint32_t pending_no_cb;
135 };
136
137 struct acl_data {
138 /* Extend the bt_buf user data */
139 struct bt_buf_data buf_data;
140
141 /* Index into the bt_conn storage array */
142 uint8_t index;
143
144 /** ACL connection handle */
145 uint16_t handle;
146 };
147
148 struct bt_conn {
149 uint16_t handle;
150 uint8_t type;
151 uint8_t role;
152
153 ATOMIC_DEFINE(flags, BT_CONN_NUM_FLAGS);
154
155 /* Which local identity address this connection uses */
156 uint8_t id;
157
158 #if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_BREDR)
159 bt_security_t sec_level;
160 bt_security_t required_sec_level;
161 uint8_t encrypt;
162 #endif /* CONFIG_BT_SMP || CONFIG_BT_BREDR */
163
164 /* Connection error or reason for disconnect */
165 uint8_t err;
166
167 bt_conn_state_t state;
168 uint16_t rx_len;
169 struct net_buf *rx;
170
171 /* Sent but not acknowledged TX packets with a callback */
172 sys_slist_t tx_pending;
173 /* Sent but not acknowledged TX packets without a callback before
174 * the next packet (if any) in tx_pending.
175 */
176 uint32_t pending_no_cb;
177
178 /* Completed TX for which we need to call the callback */
179 sys_slist_t tx_complete;
180 struct k_work tx_complete_work;
181
182
183 /* Queue for outgoing ACL data */
184 struct k_fifo tx_queue;
185
186 /* Active L2CAP channels */
187 sys_slist_t channels;
188
189 /* Delayed work deferred tasks:
190 * - Peripheral delayed connection update.
191 * - Initiator connect create cancel.
192 * - Connection cleanup.
193 */
194 struct k_work_delayable deferred_work;
195
196 union {
197 struct bt_conn_le le;
198 #if defined(CONFIG_BT_BREDR)
199 struct bt_conn_br br;
200 struct bt_conn_sco sco;
201 #endif
202 #if defined(CONFIG_BT_ISO)
203 struct bt_conn_iso iso;
204 #endif
205 };
206
207 #if defined(CONFIG_BT_REMOTE_VERSION)
208 struct bt_conn_rv {
209 uint8_t version;
210 uint16_t manufacturer;
211 uint16_t subversion;
212 } rv;
213 #endif
214 /* Must be at the end so that everything else in the structure can be
215 * memset to zero without affecting the ref.
216 */
217 atomic_t ref;
218 };
219
220 void bt_conn_reset_rx_state(struct bt_conn *conn);
221
222 /* Process incoming data for a connection */
223 void bt_conn_recv(struct bt_conn *conn, struct net_buf *buf, uint8_t flags);
224
225 /* Send data over a connection
226 *
227 * Buffer ownership is transferred to stack in case of success.
228 *
229 * Calling this from RX thread is assumed to never fail so the return can be
230 * ignored.
231 */
232 int bt_conn_send_cb(struct bt_conn *conn, struct net_buf *buf,
233 bt_conn_tx_cb_t cb, void *user_data);
234
bt_conn_send(struct bt_conn * conn,struct net_buf * buf)235 static inline int bt_conn_send(struct bt_conn *conn, struct net_buf *buf)
236 {
237 return bt_conn_send_cb(conn, buf, NULL, NULL);
238 }
239
240 /* Check if a connection object with the peer already exists */
241 bool bt_conn_exists_le(uint8_t id, const bt_addr_le_t *peer);
242
243 /* Add a new LE connection */
244 struct bt_conn *bt_conn_add_le(uint8_t id, const bt_addr_le_t *peer);
245
246 /** Connection parameters for ISO connections */
247 struct bt_iso_create_param {
248 uint8_t id;
249 uint8_t num_conns;
250 struct bt_conn **conns;
251 struct bt_iso_chan **chans;
252 };
253
254 int bt_conn_iso_init(void);
255
256 /* Add a new ISO connection */
257 struct bt_conn *bt_conn_add_iso(struct bt_conn *acl);
258
259 /* Cleanup ISO references */
260 void bt_iso_cleanup_acl(struct bt_conn *iso_conn);
261
262 /* Add a new BR/EDR connection */
263 struct bt_conn *bt_conn_add_br(const bt_addr_t *peer);
264
265 /* Add a new SCO connection */
266 struct bt_conn *bt_conn_add_sco(const bt_addr_t *peer, int link_type);
267
268 /* Cleanup SCO references */
269 void bt_sco_cleanup(struct bt_conn *sco_conn);
270
271 /* Look up an existing sco connection by BT address */
272 struct bt_conn *bt_conn_lookup_addr_sco(const bt_addr_t *peer);
273
274 /* Look up an existing connection by BT address */
275 struct bt_conn *bt_conn_lookup_addr_br(const bt_addr_t *peer);
276
277 void bt_conn_disconnect_all(uint8_t id);
278
279 /* Allocate new connection object */
280 struct bt_conn *bt_conn_new(struct bt_conn *conns, size_t size);
281
282 /* Look up an existing connection */
283 struct bt_conn *bt_conn_lookup_handle(uint16_t handle);
284
bt_conn_is_handle_valid(struct bt_conn * conn)285 static inline bool bt_conn_is_handle_valid(struct bt_conn *conn)
286 {
287 switch (conn->state) {
288 case BT_CONN_CONNECTED:
289 case BT_CONN_DISCONNECT:
290 case BT_CONN_DISCONNECT_COMPLETE:
291 return true;
292 case BT_CONN_CONNECT:
293 /* ISO connection handle assigned at connect state */
294 if (IS_ENABLED(CONFIG_BT_ISO) &&
295 conn->type == BT_CONN_TYPE_ISO) {
296 return true;
297 }
298 __fallthrough;
299 default:
300 return false;
301 }
302 }
303
304 /* Check if the connection is with the given peer. */
305 bool bt_conn_is_peer_addr_le(const struct bt_conn *conn, uint8_t id,
306 const bt_addr_le_t *peer);
307
308 /* Helpers for identifying & looking up connections based on the the index to
309 * the connection list. This is useful for O(1) lookups, but can't be used
310 * e.g. as the handle since that's assigned to us by the controller.
311 */
312 #define BT_CONN_INDEX_INVALID 0xff
313 struct bt_conn *bt_conn_lookup_index(uint8_t index);
314
315 /* Look up a connection state. For BT_ADDR_LE_ANY, returns the first connection
316 * with the specific state
317 */
318 struct bt_conn *bt_conn_lookup_state_le(uint8_t id, const bt_addr_le_t *peer,
319 const bt_conn_state_t state);
320
321 /* Set connection object in certain state and perform action related to state */
322 void bt_conn_set_state(struct bt_conn *conn, bt_conn_state_t state);
323
324 void bt_conn_connected(struct bt_conn *conn);
325
326 int bt_conn_le_conn_update(struct bt_conn *conn,
327 const struct bt_le_conn_param *param);
328
329 void notify_remote_info(struct bt_conn *conn);
330
331 void notify_le_param_updated(struct bt_conn *conn);
332
333 void notify_le_data_len_updated(struct bt_conn *conn);
334
335 void notify_le_phy_updated(struct bt_conn *conn);
336
337 bool le_param_req(struct bt_conn *conn, struct bt_le_conn_param *param);
338
339 #if defined(CONFIG_BT_SMP)
340 /* rand and ediv should be in BT order */
341 int bt_conn_le_start_encryption(struct bt_conn *conn, uint8_t rand[8],
342 uint8_t ediv[2], const uint8_t *ltk, size_t len);
343
344 /* Notify higher layers that RPA was resolved */
345 void bt_conn_identity_resolved(struct bt_conn *conn);
346 #endif /* CONFIG_BT_SMP */
347
348 #if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_BREDR)
349 /* Notify higher layers that connection security changed */
350 void bt_conn_security_changed(struct bt_conn *conn, uint8_t hci_err,
351 enum bt_security_err err);
352 #endif /* CONFIG_BT_SMP || CONFIG_BT_BREDR */
353
354 /* Prepare a PDU to be sent over a connection */
355 #if defined(CONFIG_NET_BUF_LOG)
356 struct net_buf *bt_conn_create_pdu_timeout_debug(struct net_buf_pool *pool,
357 size_t reserve,
358 k_timeout_t timeout,
359 const char *func, int line);
360 #define bt_conn_create_pdu_timeout(_pool, _reserve, _timeout) \
361 bt_conn_create_pdu_timeout_debug(_pool, _reserve, _timeout, \
362 __func__, __LINE__)
363
364 #define bt_conn_create_pdu(_pool, _reserve) \
365 bt_conn_create_pdu_timeout_debug(_pool, _reserve, K_FOREVER, \
366 __func__, __line__)
367 #else
368 struct net_buf *bt_conn_create_pdu_timeout(struct net_buf_pool *pool,
369 size_t reserve, k_timeout_t timeout);
370
371 #define bt_conn_create_pdu(_pool, _reserve) \
372 bt_conn_create_pdu_timeout(_pool, _reserve, K_FOREVER)
373 #endif
374
375 /* Prepare a PDU to be sent over a connection */
376 #if defined(CONFIG_NET_BUF_LOG)
377 struct net_buf *bt_conn_create_frag_timeout_debug(size_t reserve,
378 k_timeout_t timeout,
379 const char *func, int line);
380
381 #define bt_conn_create_frag_timeout(_reserve, _timeout) \
382 bt_conn_create_frag_timeout_debug(_reserve, _timeout, \
383 __func__, __LINE__)
384
385 #define bt_conn_create_frag(_reserve) \
386 bt_conn_create_frag_timeout_debug(_reserve, K_FOREVER, \
387 __func__, __LINE__)
388 #else
389 struct net_buf *bt_conn_create_frag_timeout(size_t reserve,
390 k_timeout_t timeout);
391
392 #define bt_conn_create_frag(_reserve) \
393 bt_conn_create_frag_timeout(_reserve, K_FOREVER)
394 #endif
395
396 /* Initialize connection management */
397 int bt_conn_init(void);
398
399 /* Selects based on connecton type right semaphore for ACL packets */
400 struct k_sem *bt_conn_get_pkts(struct bt_conn *conn);
401
402 /* k_poll related helpers for the TX thread */
403 int bt_conn_prepare_events(struct k_poll_event events[]);
404 void bt_conn_process_tx(struct bt_conn *conn);
405