1 /* hci_core.h - Bluetooth HCI core access */
2 
3 /*
4  * Copyright (c) 2021 Nordic Semiconductor ASA
5  * Copyright (c) 2015-2016 Intel Corporation
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 /* LL connection parameters */
11 #define LE_CONN_LATENCY		0x0000
12 #define LE_CONN_TIMEOUT		0x002a
13 
14 #if defined(CONFIG_BT_BREDR)
15 #define LMP_FEAT_PAGES_COUNT	3
16 #else
17 #define LMP_FEAT_PAGES_COUNT	1
18 #endif
19 
20 /* SCO  settings */
21 #define BT_VOICE_CVSD_16BIT     0x0060
22 
23 /* k_poll event tags */
24 enum {
25 	BT_EVENT_CMD_TX,
26 	BT_EVENT_CONN_TX_QUEUE,
27 };
28 
29 /* bt_dev flags: the flags defined here represent BT controller state */
30 enum {
31 	BT_DEV_ENABLE,
32 	BT_DEV_READY,
33 	BT_DEV_PRESET_ID,
34 	BT_DEV_HAS_PUB_KEY,
35 	BT_DEV_PUB_KEY_BUSY,
36 
37 	BT_DEV_SCANNING,
38 	BT_DEV_EXPLICIT_SCAN,
39 	BT_DEV_ACTIVE_SCAN,
40 	BT_DEV_SCAN_FILTER_DUP,
41 	BT_DEV_SCAN_FILTERED,
42 	BT_DEV_SCAN_LIMITED,
43 	BT_DEV_INITIATING,
44 
45 	BT_DEV_RPA_VALID,
46 
47 	BT_DEV_ID_PENDING,
48 	BT_DEV_STORE_ID,
49 
50 #if defined(CONFIG_BT_BREDR)
51 	BT_DEV_ISCAN,
52 	BT_DEV_PSCAN,
53 	BT_DEV_INQUIRY,
54 #endif /* CONFIG_BT_BREDR */
55 
56 	/* Total number of flags - must be at the end of the enum */
57 	BT_DEV_NUM_FLAGS,
58 };
59 
60 /* Flags which should not be cleared upon HCI_Reset */
61 #define BT_DEV_PERSISTENT_FLAGS (BIT(BT_DEV_ENABLE) | \
62 				 BIT(BT_DEV_PRESET_ID))
63 
64 #if defined(CONFIG_BT_EXT_ADV_LEGACY_SUPPORT)
65 /* Check the feature bit for extended or legacy advertising commands */
66 #define BT_DEV_FEAT_LE_EXT_ADV(feat) BT_FEAT_LE_EXT_ADV(feat)
67 #else
68 /* Always use extended advertising commands. */
69 #define BT_DEV_FEAT_LE_EXT_ADV(feat)  1
70 #endif
71 
72 enum {
73 	/* Advertising set has been created in the host. */
74 	BT_ADV_CREATED,
75 	/* Advertising parameters has been set in the controller.
76 	 * This implies that the advertising set has been created in the
77 	 * controller.
78 	 */
79 	BT_ADV_PARAMS_SET,
80 	/* Advertising data has been set in the controller. */
81 	BT_ADV_DATA_SET,
82 	/* Advertising random address pending to be set in the controller. */
83 	BT_ADV_RANDOM_ADDR_PENDING,
84 	/* The private random address of the advertiser is valid for this cycle
85 	 * of the RPA timeout.
86 	 */
87 	BT_ADV_RPA_VALID,
88 	/* The advertiser set is limited by a timeout, or number of advertising
89 	 * events, or both.
90 	 */
91 	BT_ADV_LIMITED,
92 	/* Advertiser set is currently advertising in the controller. */
93 	BT_ADV_ENABLED,
94 	/* Advertiser should include name in advertising data */
95 	BT_ADV_INCLUDE_NAME_AD,
96 	/* Advertiser should include name in scan response data */
97 	BT_ADV_INCLUDE_NAME_SD,
98 	/* Advertiser set is connectable */
99 	BT_ADV_CONNECTABLE,
100 	/* Advertiser set is scannable */
101 	BT_ADV_SCANNABLE,
102 	/* Advertiser set is using extended advertising */
103 	BT_ADV_EXT_ADV,
104 	/* Advertiser set has disabled the use of private addresses and is using
105 	 * the identity address instead.
106 	 */
107 	BT_ADV_USE_IDENTITY,
108 	/* Advertiser has been configured to keep advertising after a connection
109 	 * has been established as long as there are connections available.
110 	 */
111 	BT_ADV_PERSIST,
112 	/* Advertiser has been temporarily disabled. */
113 	BT_ADV_PAUSED,
114 	/* Periodic Advertising has been enabled in the controller. */
115 	BT_PER_ADV_ENABLED,
116 	/* Periodic Advertising parameters has been set in the controller. */
117 	BT_PER_ADV_PARAMS_SET,
118 	/* Constant Tone Extension parameters for Periodic Advertising
119 	 * has been set in the controller.
120 	 */
121 	BT_PER_ADV_CTE_PARAMS_SET,
122 	/* Constant Tone Extension for Periodic Advertising has been enabled
123 	 * in the controller.
124 	 */
125 	BT_PER_ADV_CTE_ENABLED,
126 
127 	BT_ADV_NUM_FLAGS,
128 };
129 
130 struct bt_le_ext_adv {
131 	/* ID Address used for advertising */
132 	uint8_t                 id;
133 
134 	/* Advertising handle */
135 	uint8_t                 handle;
136 
137 	/* Current local Random Address */
138 	bt_addr_le_t            random_addr;
139 
140 	/* Current target address */
141 	bt_addr_le_t            target_addr;
142 
143 	ATOMIC_DEFINE(flags, BT_ADV_NUM_FLAGS);
144 
145 #if defined(CONFIG_BT_EXT_ADV)
146 	const struct bt_le_ext_adv_cb *cb;
147 
148 	/* TX Power in use by the controller */
149 	int8_t                    tx_power;
150 #endif /* defined(CONFIG_BT_EXT_ADV) */
151 
152 	struct k_work_delayable	lim_adv_timeout_work;
153 };
154 
155 enum {
156 	/** Periodic Advertising Sync has been created in the host. */
157 	BT_PER_ADV_SYNC_CREATED,
158 
159 	/** Periodic Advertising Sync is established and can be terminated */
160 	BT_PER_ADV_SYNC_SYNCED,
161 
162 	/** Periodic Advertising Sync is attempting to create sync */
163 	BT_PER_ADV_SYNC_SYNCING,
164 
165 	/** Periodic Advertising Sync is attempting to create sync using
166 	 *  Advertiser List
167 	 */
168 	BT_PER_ADV_SYNC_SYNCING_USE_LIST,
169 
170 	/** Periodic Advertising Sync established with reporting disabled */
171 	BT_PER_ADV_SYNC_RECV_DISABLED,
172 
173 	/** Constant Tone Extension for Periodic Advertising has been enabled
174 	 * in the Controller.
175 	 */
176 	BT_PER_ADV_SYNC_CTE_ENABLED,
177 
178 	BT_PER_ADV_SYNC_NUM_FLAGS,
179 };
180 
181 struct bt_le_per_adv_sync {
182 	/** Periodic Advertiser Address */
183 	bt_addr_le_t addr;
184 
185 	/** Advertiser SID */
186 	uint8_t sid;
187 
188 	/** Sync handle */
189 	uint16_t handle;
190 
191 	/** Periodic advertising interval (N * 1.25 ms) */
192 	uint16_t interval;
193 
194 	/** Periodic advertising advertiser clock accuracy (ppm) */
195 	uint16_t clock_accuracy;
196 
197 	/** Advertiser PHY */
198 	uint8_t phy;
199 
200 #if defined(CONFIG_BT_DF_CONNECTIONLESS_CTE_RX)
201 	/** Accepted CTE type */
202 	uint8_t cte_type;
203 #endif /* CONFIG_BT_DF_CONNECTIONLESS_CTE_RX */
204 
205 	/** Flags */
206 	ATOMIC_DEFINE(flags, BT_PER_ADV_SYNC_NUM_FLAGS);
207 };
208 
209 struct bt_dev_le {
210 	/* LE features */
211 	uint8_t			features[8];
212 	/* LE states */
213 	uint64_t			states;
214 
215 #if defined(CONFIG_BT_CONN)
216 	/* Controller buffer information */
217 	uint16_t		mtu;
218 	struct k_sem		pkts;
219 	uint16_t		acl_mtu;
220 	struct k_sem		acl_pkts;
221 #endif /* CONFIG_BT_CONN */
222 #if defined(CONFIG_BT_ISO)
223 	uint16_t		iso_mtu;
224 	struct k_sem		iso_pkts;
225 #endif /* CONFIG_BT_ISO */
226 
227 #if defined(CONFIG_BT_SMP)
228 	/* Size of the the controller resolving list */
229 	uint8_t                    rl_size;
230 	/* Number of entries in the resolving list. rl_entries > rl_size
231 	 * means that host-side resolving is used.
232 	 */
233 	uint8_t                    rl_entries;
234 #endif /* CONFIG_BT_SMP */
235 };
236 
237 #if defined(CONFIG_BT_BREDR)
238 struct bt_dev_br {
239 	/* Max controller's acceptable ACL packet length */
240 	uint16_t         mtu;
241 	struct k_sem  pkts;
242 	uint16_t         esco_pkt_type;
243 };
244 #endif
245 
246 /* The theoretical max for these is 8 and 64, but there's no point
247  * in allocating the full memory if we only support a small subset.
248  * These values must be updated whenever the host implementation is
249  * extended beyond the current values.
250  */
251 #define BT_DEV_VS_FEAT_MAX  1
252 #define BT_DEV_VS_CMDS_MAX  2
253 
254 /* State tracking for the local Bluetooth controller */
255 struct bt_dev {
256 	/* Local Identity Address(es) */
257 	bt_addr_le_t            id_addr[CONFIG_BT_ID_MAX];
258 	uint8_t                    id_count;
259 
260 	struct bt_conn_le_create_param create_param;
261 
262 #if !defined(CONFIG_BT_EXT_ADV)
263 	/* Legacy advertiser */
264 	struct bt_le_ext_adv    adv;
265 #else
266 	/* Pointer to reserved advertising set */
267 	struct bt_le_ext_adv    *adv;
268 #if (CONFIG_BT_ID_MAX > 1) && (CONFIG_BT_EXT_ADV_MAX_ADV_SET > 1)
269 	/* When supporting multiple concurrent connectable advertising sets
270 	 * with multiple identities, we need to know the identity of
271 	 * the terminating advertising set to identify the connection object.
272 	 * The identity of the advertising set is determined by its
273 	 * advertising handle, which is part of the
274 	 * LE Set Advertising Set Terminated event which is always sent
275 	 * _after_ the LE Enhanced Connection complete event.
276 	 * Therefore we need cache this event until its identity is known.
277 	 */
278 	struct {
279 		bool valid;
280 		struct bt_hci_evt_le_enh_conn_complete evt;
281 	} cached_conn_complete[MIN(CONFIG_BT_MAX_CONN,
282 				CONFIG_BT_EXT_ADV_MAX_ADV_SET)];
283 #endif
284 #endif
285 	/* Current local Random Address */
286 	bt_addr_le_t            random_addr;
287 	uint8_t                    adv_conn_id;
288 
289 	/* Controller version & manufacturer information */
290 	uint8_t			hci_version;
291 	uint8_t			lmp_version;
292 	uint16_t			hci_revision;
293 	uint16_t			lmp_subversion;
294 	uint16_t			manufacturer;
295 
296 	/* LMP features (pages 0, 1, 2) */
297 	uint8_t			features[LMP_FEAT_PAGES_COUNT][8];
298 
299 	/* Supported commands */
300 	uint8_t			supported_commands[64];
301 
302 #if defined(CONFIG_BT_HCI_VS_EXT)
303 	/* Vendor HCI support */
304 	uint8_t                    vs_features[BT_DEV_VS_FEAT_MAX];
305 	uint8_t                    vs_commands[BT_DEV_VS_CMDS_MAX];
306 #endif
307 
308 	struct k_work           init;
309 
310 	ATOMIC_DEFINE(flags, BT_DEV_NUM_FLAGS);
311 
312 	/* LE controller specific features */
313 	struct bt_dev_le	le;
314 
315 #if defined(CONFIG_BT_BREDR)
316 	/* BR/EDR controller specific features */
317 	struct bt_dev_br	br;
318 #endif
319 
320 	/* Number of commands controller can accept */
321 	struct k_sem		ncmd_sem;
322 
323 	/* Last sent HCI command */
324 	struct net_buf		*sent_cmd;
325 
326 #if !defined(CONFIG_BT_RECV_IS_RX_THREAD)
327 	/* Queue for incoming HCI events & ACL data */
328 	struct k_fifo		rx_queue;
329 #endif
330 
331 	/* Queue for outgoing HCI commands */
332 	struct k_fifo		cmd_tx_queue;
333 
334 	/* Registered HCI driver */
335 	const struct bt_hci_driver *drv;
336 
337 #if defined(CONFIG_BT_PRIVACY)
338 	/* Local Identity Resolving Key */
339 	uint8_t			irk[CONFIG_BT_ID_MAX][16];
340 
341 	/* Work used for RPA rotation */
342 	struct k_work_delayable rpa_update;
343 #endif
344 
345 	/* Local Name */
346 #if defined(CONFIG_BT_DEVICE_NAME_DYNAMIC)
347 	char			name[CONFIG_BT_DEVICE_NAME_MAX + 1];
348 #endif
349 };
350 
351 extern struct bt_dev bt_dev;
352 #if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_BREDR)
353 extern const struct bt_conn_auth_cb *bt_auth;
354 
355 enum bt_security_err bt_security_err_get(uint8_t hci_err);
356 #endif /* CONFIG_BT_SMP || CONFIG_BT_BREDR */
357 
358 /* Data type to store state related with command to be updated
359  * when command completes successfully.
360  */
361 struct bt_hci_cmd_state_set {
362 	/* Target memory to be updated */
363 	atomic_t *target;
364 	/* Bit number to be updated in target memory */
365 	int bit;
366 	/* Value to determine if enable or disable bit */
367 	bool val;
368 };
369 
370 /* Set command state related with the command buffer */
371 void bt_hci_cmd_state_set_init(struct net_buf *buf,
372 			       struct bt_hci_cmd_state_set *state,
373 			       atomic_t *target, int bit, bool val);
374 
375 int bt_hci_disconnect(uint16_t handle, uint8_t reason);
376 
377 bool bt_le_conn_params_valid(const struct bt_le_conn_param *param);
378 int bt_le_set_data_len(struct bt_conn *conn, uint16_t tx_octets, uint16_t tx_time);
379 int bt_le_set_phy(struct bt_conn *conn, uint8_t all_phys,
380 		  uint8_t pref_tx_phy, uint8_t pref_rx_phy, uint8_t phy_opts);
381 uint8_t bt_get_phy(uint8_t hci_phy);
382 
383 int bt_le_scan_update(bool fast_scan);
384 
385 int bt_le_create_conn(const struct bt_conn *conn);
386 int bt_le_create_conn_cancel(void);
387 
388 bool bt_addr_le_is_bonded(uint8_t id, const bt_addr_le_t *addr);
389 const bt_addr_le_t *bt_lookup_id_addr(uint8_t id, const bt_addr_le_t *addr);
390 
391 int bt_send(struct net_buf *buf);
392 
393 /* Don't require everyone to include keys.h */
394 struct bt_keys;
395 void bt_id_add(struct bt_keys *keys);
396 void bt_id_del(struct bt_keys *keys);
397 
398 int bt_setup_random_id_addr(void);
399 int bt_setup_public_id_addr(void);
400 
401 void bt_finalize_init(void);
402 
403 void bt_hci_host_num_completed_packets(struct net_buf *buf);
404 
405 /* HCI event handlers */
406 void bt_hci_pin_code_req(struct net_buf *buf);
407 void bt_hci_link_key_notify(struct net_buf *buf);
408 void bt_hci_link_key_req(struct net_buf *buf);
409 void bt_hci_io_capa_resp(struct net_buf *buf);
410 void bt_hci_io_capa_req(struct net_buf *buf);
411 void bt_hci_ssp_complete(struct net_buf *buf);
412 void bt_hci_user_confirm_req(struct net_buf *buf);
413 void bt_hci_user_passkey_notify(struct net_buf *buf);
414 void bt_hci_user_passkey_req(struct net_buf *buf);
415 void bt_hci_auth_complete(struct net_buf *buf);
416 
417 /* ECC HCI event handlers */
418 void bt_hci_evt_le_pkey_complete(struct net_buf *buf);
419 void bt_hci_evt_le_dhkey_complete(struct net_buf *buf);
420 
421 /* Common HCI event handlers */
422 void bt_hci_le_enh_conn_complete(struct bt_hci_evt_le_enh_conn_complete *evt);
423 
424 /* Scan HCI event handlers */
425 void bt_hci_le_adv_report(struct net_buf *buf);
426 void bt_hci_le_scan_timeout(struct net_buf *buf);
427 void bt_hci_le_adv_ext_report(struct net_buf *buf);
428 void bt_hci_le_per_adv_sync_established(struct net_buf *buf);
429 void bt_hci_le_per_adv_report(struct net_buf *buf);
430 void bt_hci_le_per_adv_sync_lost(struct net_buf *buf);
431 void bt_hci_le_biginfo_adv_report(struct net_buf *buf);
432 void bt_hci_le_df_connectionless_iq_report(struct net_buf *buf);
433 void bt_hci_le_past_received(struct net_buf *buf);
434 
435 /* Adv HCI event handlers */
436 void bt_hci_le_adv_set_terminated(struct net_buf *buf);
437 void bt_hci_le_scan_req_received(struct net_buf *buf);
438 
439 /* BR/EDR HCI event handlers */
440 void bt_hci_conn_req(struct net_buf *buf);
441 void bt_hci_conn_complete(struct net_buf *buf);
442 
443 
444 void bt_hci_inquiry_complete(struct net_buf *buf);
445 void bt_hci_inquiry_result_with_rssi(struct net_buf *buf);
446 void bt_hci_extended_inquiry_result(struct net_buf *buf);
447 void bt_hci_remote_name_request_complete(struct net_buf *buf);
448 
449 void bt_hci_read_remote_features_complete(struct net_buf *buf);
450 void bt_hci_read_remote_ext_features_complete(struct net_buf *buf);
451 void bt_hci_role_change(struct net_buf *buf);
452 void bt_hci_synchronous_conn_complete(struct net_buf *buf);
453