1 /** @file
2  *  @brief Bluetooth connection handling
3  */
4 
5 /*
6  * Copyright (c) 2015-2016 Intel Corporation
7  * Copyright (c) 2025 Nordic Semiconductor ASA
8  *
9  * SPDX-License-Identifier: Apache-2.0
10  */
11 #ifndef ZEPHYR_INCLUDE_BLUETOOTH_CONN_H_
12 #define ZEPHYR_INCLUDE_BLUETOOTH_CONN_H_
13 
14 /**
15  * @brief Connection management
16  * @defgroup bt_conn Connection management
17  * @ingroup bluetooth
18  * @{
19  */
20 
21 #include <stdbool.h>
22 #include <stdint.h>
23 
24 #include <zephyr/bluetooth/bluetooth.h>
25 #include <zephyr/bluetooth/hci_types.h>
26 #include <zephyr/bluetooth/addr.h>
27 #include <zephyr/bluetooth/gap.h>
28 #include <zephyr/bluetooth/direction.h>
29 #include <zephyr/sys/iterable_sections.h>
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /** Opaque type representing a connection to a remote device */
36 struct bt_conn;
37 
38 /** Connection parameters for LE connections */
39 struct bt_le_conn_param {
40 	uint16_t interval_min;
41 	uint16_t interval_max;
42 	uint16_t latency;
43 	uint16_t timeout;
44 };
45 
46 /** @brief Initialize connection parameters
47  *
48  *  @param int_min  Minimum Connection Interval (N * 1.25 ms)
49  *  @param int_max  Maximum Connection Interval (N * 1.25 ms)
50  *  @param lat      Connection Latency
51  *  @param to       Supervision Timeout (N * 10 ms)
52  */
53 #define BT_LE_CONN_PARAM_INIT(int_min, int_max, lat, to) \
54 { \
55 	.interval_min = (int_min), \
56 	.interval_max = (int_max), \
57 	.latency = (lat), \
58 	.timeout = (to), \
59 }
60 
61 /** Helper to declare connection parameters inline
62  *
63  *  @param int_min  Minimum Connection Interval (N * 1.25 ms)
64  *  @param int_max  Maximum Connection Interval (N * 1.25 ms)
65  *  @param lat      Connection Latency
66  *  @param to       Supervision Timeout (N * 10 ms)
67  */
68 #define BT_LE_CONN_PARAM(int_min, int_max, lat, to) \
69 	((struct bt_le_conn_param[]) { \
70 		BT_LE_CONN_PARAM_INIT(int_min, int_max, lat, to) \
71 	 })
72 
73 /** Default LE connection parameters:
74  *    Connection Interval: 30-50 ms
75  *    Latency: 0
76  *    Timeout: 4 s
77  */
78 #define BT_LE_CONN_PARAM_DEFAULT                                                                   \
79 	BT_LE_CONN_PARAM(BT_GAP_INIT_CONN_INT_MIN, BT_GAP_INIT_CONN_INT_MAX, 0,                    \
80 			 BT_GAP_MS_TO_CONN_TIMEOUT(4000))
81 
82 /** Connection PHY information for LE connections */
83 struct bt_conn_le_phy_info {
84 	uint8_t tx_phy; /** Connection transmit PHY */
85 	uint8_t rx_phy; /** Connection receive PHY */
86 };
87 
88 /** Connection PHY options */
89 enum {
90 	/** Convenience value when no options are specified. */
91 	BT_CONN_LE_PHY_OPT_NONE = 0,
92 
93 	/** LE Coded using S=2 coding preferred when transmitting. */
94 	BT_CONN_LE_PHY_OPT_CODED_S2  = BIT(0),
95 
96 	/** LE Coded using S=8 coding preferred when transmitting. */
97 	BT_CONN_LE_PHY_OPT_CODED_S8  = BIT(1),
98 };
99 
100 /** Preferred PHY parameters for LE connections */
101 struct bt_conn_le_phy_param {
102 	uint16_t options; /**< Connection PHY options. */
103 	uint8_t  pref_tx_phy; /**< Bitmask of preferred transmit PHYs */
104 	uint8_t  pref_rx_phy; /**< Bitmask of preferred receive PHYs */
105 };
106 
107 /** Initialize PHY parameters
108  *
109  * @param _pref_tx_phy Bitmask of preferred transmit PHYs.
110  * @param _pref_rx_phy Bitmask of preferred receive PHYs.
111  */
112 #define BT_CONN_LE_PHY_PARAM_INIT(_pref_tx_phy, _pref_rx_phy) \
113 { \
114 	.options = BT_CONN_LE_PHY_OPT_NONE, \
115 	.pref_tx_phy = (_pref_tx_phy), \
116 	.pref_rx_phy = (_pref_rx_phy), \
117 }
118 
119 /** Helper to declare PHY parameters inline
120  *
121  * @param _pref_tx_phy Bitmask of preferred transmit PHYs.
122  * @param _pref_rx_phy Bitmask of preferred receive PHYs.
123  */
124 #define BT_CONN_LE_PHY_PARAM(_pref_tx_phy, _pref_rx_phy) \
125 	((struct bt_conn_le_phy_param []) { \
126 		BT_CONN_LE_PHY_PARAM_INIT(_pref_tx_phy, _pref_rx_phy) \
127 	 })
128 
129 /** Only LE 1M PHY */
130 #define BT_CONN_LE_PHY_PARAM_1M BT_CONN_LE_PHY_PARAM(BT_GAP_LE_PHY_1M, \
131 						     BT_GAP_LE_PHY_1M)
132 
133 /** Only LE 2M PHY */
134 #define BT_CONN_LE_PHY_PARAM_2M BT_CONN_LE_PHY_PARAM(BT_GAP_LE_PHY_2M, \
135 						     BT_GAP_LE_PHY_2M)
136 
137 /** Only LE Coded PHY. */
138 #define BT_CONN_LE_PHY_PARAM_CODED BT_CONN_LE_PHY_PARAM(BT_GAP_LE_PHY_CODED, \
139 							BT_GAP_LE_PHY_CODED)
140 
141 /** All LE PHYs. */
142 #define BT_CONN_LE_PHY_PARAM_ALL BT_CONN_LE_PHY_PARAM(BT_GAP_LE_PHY_1M |   \
143 						      BT_GAP_LE_PHY_2M |   \
144 						      BT_GAP_LE_PHY_CODED, \
145 						      BT_GAP_LE_PHY_1M |   \
146 						      BT_GAP_LE_PHY_2M |   \
147 						      BT_GAP_LE_PHY_CODED)
148 
149 /** Connection data length information for LE connections */
150 struct bt_conn_le_data_len_info {
151 	/** Maximum Link Layer transmission payload size in bytes. */
152 	uint16_t tx_max_len;
153 	/** Maximum Link Layer transmission payload time in us. */
154 	uint16_t tx_max_time;
155 	/** Maximum Link Layer reception payload size in bytes. */
156 	uint16_t rx_max_len;
157 	/** Maximum Link Layer reception payload time in us. */
158 	uint16_t rx_max_time;
159 };
160 
161 /** Connection data length parameters for LE connections */
162 struct bt_conn_le_data_len_param {
163 	/** Maximum Link Layer transmission payload size in bytes. */
164 	uint16_t tx_max_len;
165 	/** Maximum Link Layer transmission payload time in us. */
166 	uint16_t tx_max_time;
167 };
168 
169 /** Initialize transmit data length parameters
170  *
171  * @param  _tx_max_len  Maximum Link Layer transmission payload size in bytes.
172  * @param  _tx_max_time Maximum Link Layer transmission payload time in us.
173  */
174 #define BT_CONN_LE_DATA_LEN_PARAM_INIT(_tx_max_len, _tx_max_time) \
175 { \
176 	.tx_max_len = (_tx_max_len), \
177 	.tx_max_time = (_tx_max_time), \
178 }
179 
180 /** Helper to declare transmit data length parameters inline
181  *
182  * @param  _tx_max_len  Maximum Link Layer transmission payload size in bytes.
183  * @param  _tx_max_time Maximum Link Layer transmission payload time in us.
184  */
185 #define BT_CONN_LE_DATA_LEN_PARAM(_tx_max_len, _tx_max_time) \
186 	((struct bt_conn_le_data_len_param[]) { \
187 		BT_CONN_LE_DATA_LEN_PARAM_INIT(_tx_max_len, _tx_max_time) \
188 	 })
189 
190 /** Default LE data length parameters. */
191 #define BT_LE_DATA_LEN_PARAM_DEFAULT \
192 	BT_CONN_LE_DATA_LEN_PARAM(BT_GAP_DATA_LEN_DEFAULT, \
193 				  BT_GAP_DATA_TIME_DEFAULT)
194 
195 /** Maximum LE data length parameters. */
196 #define BT_LE_DATA_LEN_PARAM_MAX \
197 	BT_CONN_LE_DATA_LEN_PARAM(BT_GAP_DATA_LEN_MAX, \
198 				  BT_GAP_DATA_TIME_MAX)
199 
200 /** Connection subrating parameters for LE connections */
201 struct bt_conn_le_subrate_param {
202 	/** Minimum subrate factor. */
203 	uint16_t subrate_min;
204 	/** Maximum subrate factor. */
205 	uint16_t subrate_max;
206 	/** Maximum Peripheral latency in units of subrated connection intervals. */
207 	uint16_t max_latency;
208 	/** Minimum number of underlying connection events to remain active
209 	 *  after a packet containing a Link Layer PDU with a non-zero Length
210 	 *  field is sent or received.
211 	 */
212 	uint16_t continuation_number;
213 	/** Connection Supervision timeout (N * 10 ms).
214 	 *  If using @ref bt_conn_le_subrate_set_defaults, this is the
215 	 *  maximum supervision timeout allowed in requests by a peripheral.
216 	 */
217 	uint16_t supervision_timeout;
218 };
219 
220 /** Subrating information for LE connections */
221 struct bt_conn_le_subrating_info {
222 	/** Connection subrate factor. */
223 	uint16_t factor;
224 	/** Number of underlying connection events to remain active after
225 	 *  a packet containing a Link Layer PDU with a non-zero Length
226 	 *  field is sent or received.
227 	 */
228 	uint16_t continuation_number;
229 };
230 
231 /** Updated subrating connection parameters for LE connections */
232 struct bt_conn_le_subrate_changed {
233 	/** HCI Status from LE Subrate Changed event.
234 	 *  The remaining parameters will be unchanged if status is not
235 	 *  BT_HCI_ERR_SUCCESS.
236 	 */
237 	uint8_t status;
238 	/** Connection subrate factor. */
239 	uint16_t factor;
240 	/** Number of underlying connection events to remain active after
241 	 *  a packet containing a Link Layer PDU with a non-zero Length
242 	 *  field is sent or received.
243 	 */
244 	uint16_t continuation_number;
245 	/** Peripheral latency in units of subrated connection intervals. */
246 	uint16_t peripheral_latency;
247 	/** Connection Supervision timeout (N * 10 ms). */
248 	uint16_t supervision_timeout;
249 };
250 
251 /** Connection Type */
252 enum __packed bt_conn_type {
253 	/** LE Connection Type */
254 	BT_CONN_TYPE_LE = BIT(0),
255 	/** BR/EDR Connection Type */
256 	BT_CONN_TYPE_BR = BIT(1),
257 	/** SCO Connection Type */
258 	BT_CONN_TYPE_SCO = BIT(2),
259 	/** ISO Connection Type */
260 	BT_CONN_TYPE_ISO = BIT(3),
261 	/** All Connection Type */
262 	BT_CONN_TYPE_ALL = BT_CONN_TYPE_LE | BT_CONN_TYPE_BR |
263 			   BT_CONN_TYPE_SCO | BT_CONN_TYPE_ISO,
264 };
265 
266 /** Supported AA-Only RTT precision. */
267 enum bt_conn_le_cs_capability_rtt_aa_only {
268 	/** AA-Only RTT variant is not supported. */
269 	BT_CONN_LE_CS_RTT_AA_ONLY_NOT_SUPP = 0,
270 	/** 10ns time-of-flight accuracy. */
271 	BT_CONN_LE_CS_RTT_AA_ONLY_10NS,
272 	/** 150ns time-of-flight accuracy. */
273 	BT_CONN_LE_CS_RTT_AA_ONLY_150NS,
274 };
275 
276 /** Supported Sounding Sequence RTT precision. */
277 enum bt_conn_le_cs_capability_rtt_sounding {
278 	/** Sounding Sequence RTT variant is not supported. */
279 	BT_CONN_LE_CS_RTT_SOUNDING_NOT_SUPP = 0,
280 	/** 10ns time-of-flight accuracy. */
281 	BT_CONN_LE_CS_RTT_SOUNDING_10NS,
282 	/** 150ns time-of-flight accuracy. */
283 	BT_CONN_LE_CS_RTT_SOUNDING_150NS,
284 };
285 
286 /** Supported Random Payload RTT precision. */
287 enum bt_conn_le_cs_capability_rtt_random_payload {
288 	/** Random Payload RTT variant is not supported. */
289 	BT_CONN_LE_CS_RTT_RANDOM_PAYLOAD_NOT_SUPP = 0,
290 	/** 10ns time-of-flight accuracy. */
291 	BT_CONN_LE_CS_RTT_RANDOM_PAYLOAD_10NS,
292 	/** 150ns time-of-flight accuracy. */
293 	BT_CONN_LE_CS_RTT_RANDOM_PAYLOAD_150NS,
294 };
295 
296 /** Remote channel sounding capabilities for LE connections supporting CS */
297 struct bt_conn_le_cs_capabilities {
298 	/** Number of CS configurations */
299 	uint8_t num_config_supported;
300 	/** Maximum number of consecutive CS procedures.
301 	 *
302 	 * When set to zero, indicates support for both fixed and indefinite
303 	 * numbers of CS procedures before termination.
304 	 */
305 	uint16_t max_consecutive_procedures_supported;
306 	/** Number of antennas. */
307 	uint8_t num_antennas_supported;
308 	/** Maximum number of antenna paths. */
309 	uint8_t max_antenna_paths_supported;
310 	/** Initiator role. */
311 	bool initiator_supported;
312 	/** Reflector role. */
313 	bool reflector_supported;
314 	/** Mode-3 */
315 	bool mode_3_supported;
316 	/** RTT AA-Only */
317 	enum bt_conn_le_cs_capability_rtt_aa_only rtt_aa_only_precision;
318 	/** RTT Sounding */
319 	enum bt_conn_le_cs_capability_rtt_sounding rtt_sounding_precision;
320 	/** RTT Random Payload */
321 	enum bt_conn_le_cs_capability_rtt_random_payload rtt_random_payload_precision;
322 	/** Number of CS steps needed to achieve the
323 	 * accuracy requirements for RTT AA Only.
324 	 *
325 	 * Set to 0 if RTT AA Only isn't supported.
326 	 */
327 	uint8_t rtt_aa_only_n;
328 	/** Number of CS steps needed to achieve the
329 	 * accuracy requirements for RTT Sounding.
330 	 *
331 	 * Set to 0 if RTT Sounding isn't supported
332 	 */
333 	uint8_t rtt_sounding_n;
334 	/** Number of CS steps needed to achieve the
335 	 * accuracy requirements for RTT Random Payload.
336 	 *
337 	 * Set to 0 if RTT Random Payload isn't supported.
338 	 */
339 	uint8_t rtt_random_payload_n;
340 	/** Phase-based normalized attack detector metric
341 	 * when a CS_SYNC with sounding sequence is received.
342 	 */
343 	bool phase_based_nadm_sounding_supported;
344 	/** Phase-based normalized attack detector metric
345 	 * when a CS_SYNC with random sequence is received.
346 	 */
347 	bool phase_based_nadm_random_supported;
348 	/** CS_SYNC LE 2M PHY. */
349 	bool cs_sync_2m_phy_supported;
350 	/** CS_SYNC LE 2M 2BT PHY. */
351 	bool cs_sync_2m_2bt_phy_supported;
352 	/** Subfeature: CS with no Frequency Actuation Error. */
353 	bool cs_without_fae_supported;
354 	/** Subfeature: Channel Selection Algorithm #3c */
355 	bool chsel_alg_3c_supported;
356 	/** Subfeature: Phase-based Ranging from RTT sounding sequence. */
357 	bool pbr_from_rtt_sounding_seq_supported;
358 	/** Optional T_IP1 time durations during CS steps.
359 	 *
360 	 *  - Bit 0: 10 us
361 	 *  - Bit 1: 20 us
362 	 *  - Bit 2: 30 us
363 	 *  - Bit 3: 40 us
364 	 *  - Bit 4: 50 us
365 	 *  - Bit 5: 60 us
366 	 *  - Bit 6: 80 us
367 	 */
368 	uint16_t t_ip1_times_supported;
369 	/** Optional T_IP2 time durations during CS steps.
370 	 *
371 	 *  - Bit 0: 10 us
372 	 *  - Bit 1: 20 us
373 	 *  - Bit 2: 30 us
374 	 *  - Bit 3: 40 us
375 	 *  - Bit 4: 50 us
376 	 *  - Bit 5: 60 us
377 	 *  - Bit 6: 80 us
378 	 */
379 	uint16_t t_ip2_times_supported;
380 	/** Optional T_FCS time durations during CS steps.
381 	 *
382 	 *  - Bit 0: 15 us
383 	 *  - Bit 1: 20 us
384 	 *  - Bit 2: 30 us
385 	 *  - Bit 3: 40 us
386 	 *  - Bit 4: 50 us
387 	 *  - Bit 5: 60 us
388 	 *  - Bit 6: 80 us
389 	 *  - Bit 7: 100 us
390 	 *  - Bit 8: 120 us
391 	 */
392 	uint16_t t_fcs_times_supported;
393 	/** Optional T_PM time durations during CS steps.
394 	 *
395 	 *  - Bit 0: 10 us
396 	 *  - Bit 1: 20 us
397 	 */
398 	uint16_t t_pm_times_supported;
399 	/** Time in microseconds for the antenna switch period of the CS tones. */
400 	uint8_t t_sw_time;
401 	/** Supported SNR levels used in RTT packets.
402 	 *
403 	 *  - Bit 0: 18dB
404 	 *  - Bit 1: 21dB
405 	 *  - Bit 2: 24dB
406 	 *  - Bit 3: 27dB
407 	 *  - Bit 4: 30dB
408 	 */
409 	uint8_t tx_snr_capability;
410 };
411 
412 /** Remote FAE Table for LE connections supporting CS */
413 struct bt_conn_le_cs_fae_table {
414 	int8_t *remote_fae_table;
415 };
416 
417 /** Channel sounding main mode */
418 enum bt_conn_le_cs_main_mode {
419 	/** Mode-1 (RTT) */
420 	BT_CONN_LE_CS_MAIN_MODE_1 = BT_HCI_OP_LE_CS_MAIN_MODE_1,
421 	/** Mode-2 (PBR) */
422 	BT_CONN_LE_CS_MAIN_MODE_2 = BT_HCI_OP_LE_CS_MAIN_MODE_2,
423 	/** Mode-3 (RTT and PBR) */
424 	BT_CONN_LE_CS_MAIN_MODE_3 = BT_HCI_OP_LE_CS_MAIN_MODE_3,
425 };
426 
427 /** Channel sounding sub mode */
428 enum bt_conn_le_cs_sub_mode {
429 	/** Unused */
430 	BT_CONN_LE_CS_SUB_MODE_UNUSED = BT_HCI_OP_LE_CS_SUB_MODE_UNUSED,
431 	/** Mode-1 (RTT) */
432 	BT_CONN_LE_CS_SUB_MODE_1 = BT_HCI_OP_LE_CS_SUB_MODE_1,
433 	/** Mode-2 (PBR) */
434 	BT_CONN_LE_CS_SUB_MODE_2 = BT_HCI_OP_LE_CS_SUB_MODE_2,
435 	/** Mode-3 (RTT and PBR) */
436 	BT_CONN_LE_CS_SUB_MODE_3 = BT_HCI_OP_LE_CS_SUB_MODE_3,
437 };
438 
439 /** Channel sounding role */
440 enum bt_conn_le_cs_role {
441 	/** CS initiator role */
442 	BT_CONN_LE_CS_ROLE_INITIATOR,
443 	/** CS reflector role */
444 	BT_CONN_LE_CS_ROLE_REFLECTOR,
445 };
446 
447 /** Channel sounding RTT type */
448 enum bt_conn_le_cs_rtt_type {
449 	/** RTT AA only */
450 	BT_CONN_LE_CS_RTT_TYPE_AA_ONLY = BT_HCI_OP_LE_CS_RTT_TYPE_AA_ONLY,
451 	/** RTT with 32-bit sounding sequence */
452 	BT_CONN_LE_CS_RTT_TYPE_32_BIT_SOUNDING = BT_HCI_OP_LE_CS_RTT_TYPE_32BIT_SOUND,
453 	/** RTT with 96-bit sounding sequence */
454 	BT_CONN_LE_CS_RTT_TYPE_96_BIT_SOUNDING = BT_HCI_OP_LE_CS_RTT_TYPE_96BIT_SOUND,
455 	/** RTT with 32-bit random sequence */
456 	BT_CONN_LE_CS_RTT_TYPE_32_BIT_RANDOM = BT_HCI_OP_LE_CS_RTT_TYPE_32BIT_RAND,
457 	/** RTT with 64-bit random sequence */
458 	BT_CONN_LE_CS_RTT_TYPE_64_BIT_RANDOM = BT_HCI_OP_LE_CS_RTT_TYPE_64BIT_RAND,
459 	/** RTT with 96-bit random sequence */
460 	BT_CONN_LE_CS_RTT_TYPE_96_BIT_RANDOM = BT_HCI_OP_LE_CS_RTT_TYPE_96BIT_RAND,
461 	/** RTT with 128-bit random sequence */
462 	BT_CONN_LE_CS_RTT_TYPE_128_BIT_RANDOM = BT_HCI_OP_LE_CS_RTT_TYPE_128BIT_RAND,
463 };
464 
465 /** Channel sounding PHY used for CS sync */
466 enum bt_conn_le_cs_sync_phy {
467 	/** LE 1M PHY */
468 	BT_CONN_LE_CS_SYNC_1M_PHY = BT_HCI_OP_LE_CS_CS_SYNC_1M,
469 	/** LE 2M PHY */
470 	BT_CONN_LE_CS_SYNC_2M_PHY = BT_HCI_OP_LE_CS_CS_SYNC_2M,
471 	/** LE 2M 2BT PHY */
472 	BT_CONN_LE_CS_SYNC_2M_2BT_PHY = BT_HCI_OP_LE_CS_CS_SYNC_2M_2BT,
473 };
474 
475 /** Channel sounding channel selection type */
476 enum bt_conn_le_cs_chsel_type {
477 	/** Use Channel Selection Algorithm #3b for non-mode-0 CS steps */
478 	BT_CONN_LE_CS_CHSEL_TYPE_3B = BT_HCI_OP_LE_CS_TEST_CHSEL_TYPE_3B,
479 	/** Use Channel Selection Algorithm #3c for non-mode-0 CS steps */
480 	BT_CONN_LE_CS_CHSEL_TYPE_3C = BT_HCI_OP_LE_CS_TEST_CHSEL_TYPE_3C,
481 };
482 
483 /** Channel sounding channel sequence shape */
484 enum bt_conn_le_cs_ch3c_shape {
485 	/** Use Hat shape for user-specified channel sequence */
486 	BT_CONN_LE_CS_CH3C_SHAPE_HAT = BT_HCI_OP_LE_CS_TEST_CH3C_SHAPE_HAT,
487 	/** Use X shape for user-specified channel sequence */
488 	BT_CONN_LE_CS_CH3C_SHAPE_X = BT_HCI_OP_LE_CS_TEST_CH3C_SHAPE_X,
489 };
490 
491 /** Channel sounding configuration */
492 struct bt_conn_le_cs_config {
493 	/** CS configuration ID */
494 	uint8_t id;
495 	/** Main CS mode type */
496 	enum bt_conn_le_cs_main_mode main_mode_type;
497 	/** Sub CS mode type */
498 	enum bt_conn_le_cs_sub_mode sub_mode_type;
499 	/** Minimum number of CS main mode steps to be executed before a submode step is executed */
500 	uint8_t min_main_mode_steps;
501 	/** Maximum number of CS main mode steps to be executed before a submode step is executed */
502 	uint8_t max_main_mode_steps;
503 	/** Number of main mode steps taken from the end of the last CS subevent to be repeated
504 	 *  at the beginning of the current CS subevent directly after the last mode-0 step of that
505 	 *  event
506 	 */
507 	uint8_t main_mode_repetition;
508 	/** Number of CS mode-0 steps to be included at the beginning of each CS subevent */
509 	uint8_t mode_0_steps;
510 	/** CS role */
511 	enum bt_conn_le_cs_role role;
512 	/** RTT type */
513 	enum bt_conn_le_cs_rtt_type rtt_type;
514 	/** CS Sync PHY */
515 	enum bt_conn_le_cs_sync_phy cs_sync_phy;
516 	/** The number of times the Channel_Map field will be cycled through for non-mode-0 steps
517 	 *  within a CS procedure
518 	 */
519 	uint8_t channel_map_repetition;
520 	/** Channel selection type */
521 	enum bt_conn_le_cs_chsel_type channel_selection_type;
522 	/** User-specified channel sequence shape */
523 	enum bt_conn_le_cs_ch3c_shape ch3c_shape;
524 	/** Number of channels skipped in each rising and falling sequence  */
525 	uint8_t ch3c_jump;
526 	/** Interlude time in microseconds between the RTT packets */
527 	uint8_t t_ip1_time_us;
528 	/** Interlude time in microseconds between the CS tones */
529 	uint8_t t_ip2_time_us;
530 	/** Time in microseconds for frequency changes */
531 	uint8_t t_fcs_time_us;
532 	/** Time in microseconds for the phase measurement period of the CS tones */
533 	uint8_t t_pm_time_us;
534 	/** Channel map used for CS procedure
535 	 *  Channels n = 0, 1, 23, 24, 25, 77, and 78 are not allowed and shall be set to zero.
536 	 *  Channel 79 is reserved for future use and shall be set to zero.
537 	 *  At least 15 channels shall be enabled.
538 	 */
539 	uint8_t channel_map[10];
540 };
541 
542 /** Procedure done status */
543 enum bt_conn_le_cs_procedure_done_status {
544 	BT_CONN_LE_CS_PROCEDURE_COMPLETE = BT_HCI_LE_CS_PROCEDURE_DONE_STATUS_COMPLETE,
545 	BT_CONN_LE_CS_PROCEDURE_INCOMPLETE = BT_HCI_LE_CS_PROCEDURE_DONE_STATUS_PARTIAL,
546 	BT_CONN_LE_CS_PROCEDURE_ABORTED = BT_HCI_LE_CS_PROCEDURE_DONE_STATUS_ABORTED,
547 };
548 
549 /** Subevent done status */
550 enum bt_conn_le_cs_subevent_done_status {
551 	BT_CONN_LE_CS_SUBEVENT_COMPLETE = BT_HCI_LE_CS_SUBEVENT_DONE_STATUS_COMPLETE,
552 	BT_CONN_LE_CS_SUBEVENT_ABORTED = BT_HCI_LE_CS_SUBEVENT_DONE_STATUS_ABORTED,
553 };
554 
555 /** Procedure abort reason */
556 enum bt_conn_le_cs_procedure_abort_reason {
557 	BT_CONN_LE_CS_PROCEDURE_NOT_ABORTED = BT_HCI_LE_CS_PROCEDURE_ABORT_REASON_NO_ABORT,
558 	BT_CONN_LE_CS_PROCEDURE_ABORT_REQUESTED =
559 		BT_HCI_LE_CS_PROCEDURE_ABORT_REASON_LOCAL_HOST_OR_REMOTE_REQUEST,
560 	BT_CONN_LE_CS_PROCEDURE_ABORT_TOO_FEW_CHANNELS =
561 		BT_HCI_LE_CS_PROCEDURE_ABORT_REASON_TOO_FEW_CHANNELS,
562 	BT_CONN_LE_CS_PROCEDURE_ABORT_CHMAP_INSTANT_PASSED =
563 		BT_HCI_LE_CS_PROCEDURE_ABORT_REASON_CHMAP_INSTANT_PASSED,
564 	BT_CONN_LE_CS_PROCEDURE_ABORT_UNSPECIFIED = BT_HCI_LE_CS_PROCEDURE_ABORT_REASON_UNSPECIFIED,
565 };
566 
567 /** Subevent abort reason */
568 enum bt_conn_le_cs_subevent_abort_reason {
569 	BT_CONN_LE_CS_SUBEVENT_NOT_ABORTED = BT_HCI_LE_CS_SUBEVENT_ABORT_REASON_NO_ABORT,
570 	BT_CONN_LE_CS_SUBEVENT_ABORT_REQUESTED =
571 		BT_HCI_LE_CS_SUBEVENT_ABORT_REASON_LOCAL_HOST_OR_REMOTE_REQUEST,
572 	BT_CONN_LE_CS_SUBEVENT_ABORT_NO_CS_SYNC =
573 		BT_HCI_LE_CS_SUBEVENT_ABORT_REASON_NO_CS_SYNC_RECEIVED,
574 	BT_CONN_LE_CS_SUBEVENT_ABORT_SCHED_CONFLICT =
575 		BT_HCI_LE_CS_SUBEVENT_ABORT_REASON_SCHED_CONFLICT,
576 	BT_CONN_LE_CS_SUBEVENT_ABORT_UNSPECIFIED = BT_HCI_LE_CS_SUBEVENT_ABORT_REASON_UNSPECIFIED,
577 };
578 
579 /** Subevent data for LE connections supporting CS */
580 struct bt_conn_le_cs_subevent_result {
581 	struct {
582 		/** CS configuration identifier.
583 		 *
584 		 *  Range: 0 to 3
585 		 *
586 		 *  If these results were generated by a CS Test,
587 		 *  this value will be set to 0 and has no meaning.
588 		 */
589 		uint8_t config_id;
590 		/** Starting ACL connection event counter.
591 		 *
592 		 *  If these results were generated by a CS Test,
593 		 *  this value will be set to 0 and has no meaning.
594 		 */
595 		uint16_t start_acl_conn_event;
596 		/** CS procedure count associated with these results.
597 		 *
598 		 *  This is the CS procedure count since the completion of
599 		 *  the Channel Sounding Security Start procedure.
600 		 */
601 		uint16_t procedure_counter;
602 		/** Frequency compensation value in units of 0.01 ppm.
603 		 *
604 		 *  This is a 15-bit signed integer in the range [-100, 100] ppm.
605 		 *
606 		 *  A value of @ref BT_HCI_LE_CS_SUBEVENT_RESULT_FREQ_COMPENSATION_NOT_AVAILABLE
607 		 *  indicates that the role is not the initiator, or that the
608 		 *  frequency compensation value is unavailable.
609 		 */
610 		uint16_t frequency_compensation;
611 		/** Reference power level in dBm.
612 		 *
613 		 *  Range: -127 to 20
614 		 *
615 		 *  A value of @ref BT_HCI_LE_CS_REF_POWER_LEVEL_UNAVAILABLE indicates
616 		 *  that the reference power level was not available during a subevent.
617 		 */
618 		int8_t reference_power_level;
619 		/** Procedure status. */
620 		enum bt_conn_le_cs_procedure_done_status procedure_done_status;
621 		/** Subevent status
622 		 *
623 		 *  For aborted subevents, this will be set to @ref BT_CONN_LE_CS_SUBEVENT_ABORTED
624 		 *  and abort_step will contain the step number on which the subevent was aborted.
625 		 *  Consider the following example:
626 		 *
627 		 *  subevent_done_status = @ref BT_CONN_LE_CS_SUBEVENT_ABORTED
628 		 *  num_steps_reported = 160
629 		 *  abort_step = 100
630 		 *
631 		 *  this would mean that steps from 0 to 99 are complete and steps from 100 to 159
632 		 *  are aborted.
633 		 */
634 		enum bt_conn_le_cs_subevent_done_status subevent_done_status;
635 		/** Abort reason.
636 		 *
637 		 *  If the procedure status is
638 		 *  @ref BT_CONN_LE_CS_PROCEDURE_ABORTED, this field will
639 		 *  specify the reason for the abortion.
640 		 */
641 		enum bt_conn_le_cs_procedure_abort_reason procedure_abort_reason;
642 		/** Abort reason.
643 		 *
644 		 *  If the subevent status is
645 		 *  @ref BT_CONN_LE_CS_SUBEVENT_ABORTED, this field will
646 		 *  specify the reason for the abortion.
647 		 */
648 		enum bt_conn_le_cs_subevent_abort_reason subevent_abort_reason;
649 		/** Number of antenna paths used during the phase measurement stage.
650 		 */
651 		uint8_t num_antenna_paths;
652 		/** Number of CS steps in the subevent.
653 		 */
654 		uint8_t num_steps_reported;
655 		/** Step number, on which the subevent was aborted
656 		 *  if subevent_done_status is @ref BT_CONN_LE_CS_SUBEVENT_COMPLETE
657 		 *  then abort_step will be unused and set to 255
658 		 */
659 		uint8_t abort_step;
660 	} header;
661 	/** Pointer to buffer containing step data.
662 	 *  NULL if num_steps_reported is 0.
663 	 */
664 	struct net_buf_simple *step_data_buf;
665 };
666 
667 /** @brief Increment a connection's reference count.
668  *
669  *  Increment the reference count of a connection object.
670  *
671  *  @note Will return NULL if the reference count is zero.
672  *
673  *  @param conn Connection object.
674  *
675  *  @return Connection object with incremented reference count, or NULL if the
676  *          reference count is zero.
677  */
678 struct bt_conn *bt_conn_ref(struct bt_conn *conn);
679 
680 /** @brief Decrement a connection's reference count.
681  *
682  *  Decrement the reference count of a connection object.
683  *
684  *  @param conn Connection object.
685  */
686 void bt_conn_unref(struct bt_conn *conn);
687 
688 /** @brief Iterate through all bt_conn objects.
689  *
690  * Iterates through all bt_conn objects that are alive in the Host allocator.
691  *
692  * To find established connections, combine this with @ref bt_conn_get_info.
693  * Check that @ref bt_conn_info.state is @ref BT_CONN_STATE_CONNECTED.
694  *
695  * Thread safety: This API is thread safe, but it does not guarantee a
696  * sequentially-consistent view for objects allocated during the current
697  * invocation of this API. E.g. If preempted while allocations A then B then C
698  * happen then results may include A and C but miss B.
699  *
700  * @param type  Connection Type
701  * @param func  Function to call for each connection.
702  * @param data  Data to pass to the callback function.
703  */
704 void bt_conn_foreach(enum bt_conn_type type,
705 		     void (*func)(struct bt_conn *conn, void *data),
706 		     void *data);
707 
708 /** @brief Look up an existing connection by address.
709  *
710  *  Look up an existing connection based on the remote address.
711  *
712  *  The caller gets a new reference to the connection object which must be
713  *  released with bt_conn_unref() once done using the object.
714  *
715  *  @param id   Local identity (in most cases BT_ID_DEFAULT).
716  *  @param peer Remote address.
717  *
718  *  @return Connection object or NULL if not found.
719  */
720 struct bt_conn *bt_conn_lookup_addr_le(uint8_t id, const bt_addr_le_t *peer);
721 
722 /** @brief Get destination (peer) address of a connection.
723  *
724  *  @param conn Connection object.
725  *
726  *  @return Destination address if @p conn is a valid @ref BT_CONN_TYPE_LE connection
727  */
728 const bt_addr_le_t *bt_conn_get_dst(const struct bt_conn *conn);
729 
730 /** @brief Get array index of a connection
731  *
732  *  This function is used to map bt_conn to index of an array of
733  *  connections. The array has CONFIG_BT_MAX_CONN elements.
734  *
735  *  @param conn Connection object.
736  *
737  *  @return Index of the connection object.
738  *          The range of the returned value is 0..CONFIG_BT_MAX_CONN-1
739  */
740 uint8_t bt_conn_index(const struct bt_conn *conn);
741 
742 /** LE Connection Info Structure */
743 struct bt_conn_le_info {
744 	/** Source (Local) Identity Address */
745 	const bt_addr_le_t *src;
746 	/** Destination (Remote) Identity Address or remote Resolvable Private
747 	 *  Address (RPA) before identity has been resolved.
748 	 */
749 	const bt_addr_le_t *dst;
750 	/** Local device address used during connection setup. */
751 	const bt_addr_le_t *local;
752 	/** Remote device address used during connection setup. */
753 	const bt_addr_le_t *remote;
754 	uint16_t interval; /**< Connection interval */
755 	uint16_t latency; /**< Connection peripheral latency */
756 	uint16_t timeout; /**< Connection supervision timeout */
757 
758 #if defined(CONFIG_BT_USER_PHY_UPDATE)
759 	const struct bt_conn_le_phy_info      *phy;
760 #endif /* defined(CONFIG_BT_USER_PHY_UPDATE) */
761 
762 #if defined(CONFIG_BT_USER_DATA_LEN_UPDATE)
763 	/* Connection maximum single fragment parameters */
764 	const struct bt_conn_le_data_len_info *data_len;
765 #endif /* defined(CONFIG_BT_USER_DATA_LEN_UPDATE) */
766 
767 #if defined(CONFIG_BT_SUBRATING)
768 	/* Connection subrating parameters */
769 	const struct bt_conn_le_subrating_info *subrate;
770 #endif /* defined(CONFIG_BT_SUBRATING) */
771 };
772 
773 /** @brief Convert connection interval to milliseconds
774  *
775  *  Multiply by 1.25 to get milliseconds.
776  *
777  *  Note that this may be inaccurate, as something like 7.5 ms cannot be
778  *  accurately presented with integers.
779  */
780 #define BT_CONN_INTERVAL_TO_MS(interval) ((interval) * 5U / 4U)
781 
782 /** @brief Convert connection interval to microseconds
783  *
784  *  Multiply by 1250 to get microseconds.
785  */
786 #define BT_CONN_INTERVAL_TO_US(interval) ((interval) * 1250U)
787 
788 /** BR/EDR Connection Info Structure */
789 struct bt_conn_br_info {
790 	const bt_addr_t *dst; /**< Destination (Remote) BR/EDR address */
791 };
792 
793 enum {
794 	BT_CONN_ROLE_CENTRAL = 0,
795 	BT_CONN_ROLE_PERIPHERAL = 1,
796 };
797 
798 enum bt_conn_state {
799 	/** Channel disconnected */
800 	BT_CONN_STATE_DISCONNECTED,
801 	/** Channel in connecting state */
802 	BT_CONN_STATE_CONNECTING,
803 	/** Channel connected and ready for upper layer traffic on it */
804 	BT_CONN_STATE_CONNECTED,
805 	/** Channel in disconnecting state */
806 	BT_CONN_STATE_DISCONNECTING,
807 };
808 
809 /** Security level. */
810 typedef enum __packed {
811 	/** Level 0: Only for BR/EDR special cases, like SDP */
812 	BT_SECURITY_L0,
813 	/** Level 1: No encryption and no authentication. */
814 	BT_SECURITY_L1,
815 	/** Level 2: Encryption and no authentication (no MITM). */
816 	BT_SECURITY_L2,
817 	/** Level 3: Encryption and authentication (MITM). */
818 	BT_SECURITY_L3,
819 	/** Level 4: Authenticated Secure Connections and 128-bit key. */
820 	BT_SECURITY_L4,
821 	/** Bit to force new pairing procedure, bit-wise OR with requested
822 	 *  security level.
823 	 */
824 	BT_SECURITY_FORCE_PAIR = BIT(7),
825 } bt_security_t;
826 
827 /** Security Info Flags. */
828 enum bt_security_flag {
829 	/** Paired with Secure Connections. */
830 	BT_SECURITY_FLAG_SC = BIT(0),
831 	/** Paired with Out of Band method. */
832 	BT_SECURITY_FLAG_OOB = BIT(1),
833 };
834 
835 /** Security Info Structure. */
836 struct bt_security_info {
837 	/** Security Level. */
838 	bt_security_t level;
839 	/** Encryption Key Size. */
840 	uint8_t enc_key_size;
841 	/** Flags. */
842 	enum bt_security_flag flags;
843 };
844 
845 /** Connection Info Structure */
846 struct bt_conn_info {
847 	/** Connection Type. */
848 	enum bt_conn_type type;
849 	/** Connection Role. */
850 	uint8_t role;
851 	/** Which local identity the connection was created with */
852 	uint8_t id;
853 	/** Connection Type specific Info.*/
854 	union {
855 		/** LE Connection specific Info. */
856 		struct bt_conn_le_info le;
857 		/** BR/EDR Connection specific Info. */
858 		struct bt_conn_br_info br;
859 	};
860 	/** Connection state. */
861 	enum bt_conn_state state;
862 	/** Security specific info. */
863 	struct bt_security_info security;
864 };
865 
866 /** LE Connection Remote Info Structure */
867 struct bt_conn_le_remote_info {
868 
869 	/** Remote LE feature set (bitmask). */
870 	const uint8_t *features;
871 };
872 
873 /** BR/EDR Connection Remote Info structure */
874 struct bt_conn_br_remote_info {
875 
876 	/** Remote feature set (pages of bitmasks). */
877 	const uint8_t *features;
878 
879 	/** Number of pages in the remote feature set. */
880 	uint8_t num_pages;
881 };
882 
883 /** @brief Connection Remote Info Structure
884  *
885  *  @note The version, manufacturer and subversion fields will only contain
886  *        valid data if @kconfig{CONFIG_BT_REMOTE_VERSION} is enabled.
887  */
888 struct bt_conn_remote_info {
889 	/** Connection Type */
890 	uint8_t  type;
891 
892 	/** Remote Link Layer version */
893 	uint8_t  version;
894 
895 	/** Remote manufacturer identifier */
896 	uint16_t manufacturer;
897 
898 	/** Per-manufacturer unique revision */
899 	uint16_t subversion;
900 
901 	union {
902 		/** LE connection remote info */
903 		struct bt_conn_le_remote_info le;
904 
905 		/** BR/EDR connection remote info */
906 		struct bt_conn_br_remote_info br;
907 	};
908 };
909 
910 enum bt_conn_le_tx_power_phy {
911 	/** Convenience macro for when no PHY is set. */
912 	BT_CONN_LE_TX_POWER_PHY_NONE,
913 	/** LE 1M PHY */
914 	BT_CONN_LE_TX_POWER_PHY_1M,
915 	 /** LE 2M PHY */
916 	BT_CONN_LE_TX_POWER_PHY_2M,
917 	/** LE Coded PHY using S=8 coding. */
918 	BT_CONN_LE_TX_POWER_PHY_CODED_S8,
919 	/** LE Coded PHY using S=2 coding. */
920 	BT_CONN_LE_TX_POWER_PHY_CODED_S2,
921 };
922 
923 /** LE Transmit Power Level Structure */
924 struct bt_conn_le_tx_power {
925 
926 	/** Input: 1M, 2M, Coded S2 or Coded S8 */
927 	uint8_t phy;
928 
929 	/** Output: current transmit power level */
930 	int8_t current_level;
931 
932 	/** Output: maximum transmit power level */
933 	int8_t max_level;
934 };
935 
936 
937 /** LE Transmit Power Reporting Structure */
938 struct bt_conn_le_tx_power_report {
939 
940 	/** Reason for Transmit power reporting,
941 	 * as documented in Core Spec. Version 5.4 Vol. 4, Part E, 7.7.65.33.
942 	 */
943 	uint8_t reason;
944 
945 	/** Phy of Transmit power reporting. */
946 	enum bt_conn_le_tx_power_phy phy;
947 
948 	/** Transmit power level
949 	 * - 0xXX - Transmit power level
950 	 *  + Range: -127 to 20
951 	 *  + Units: dBm
952 	 *
953 	 * - 0x7E - Remote device is not managing power levels on this PHY.
954 	 * - 0x7F - Transmit power level is not available
955 	 */
956 	int8_t tx_power_level;
957 
958 	/** Bit 0: Transmit power level is at minimum level.
959 	 *  Bit 1: Transmit power level is at maximum level.
960 	 */
961 	uint8_t tx_power_level_flag;
962 
963 	/** Change in transmit power level
964 	 * - 0xXX - Change in transmit power level (positive indicates increased
965 	 *   power, negative indicates decreased power, zero indicates unchanged)
966 	 *   Units: dB
967 	 * - 0x7F - Change is not available or is out of range.
968 	 */
969 	int8_t delta;
970 };
971 
972 /** @brief Path Loss zone that has been entered.
973  *
974  *  The path loss zone that has been entered in the most recent LE Path Loss Monitoring
975  *  Threshold Change event as documented in Core Spec. Version 5.4 Vol.4, Part E, 7.7.65.32.
976  *
977  *  @note BT_CONN_LE_PATH_LOSS_ZONE_UNAVAILABLE has been added to notify when path loss becomes
978  *        unavailable.
979  */
980 enum bt_conn_le_path_loss_zone {
981 	/** Low path loss zone entered. */
982 	BT_CONN_LE_PATH_LOSS_ZONE_ENTERED_LOW,
983 	/** Middle path loss zone entered. */
984 	BT_CONN_LE_PATH_LOSS_ZONE_ENTERED_MIDDLE,
985 	/** High path loss zone entered. */
986 	BT_CONN_LE_PATH_LOSS_ZONE_ENTERED_HIGH,
987 	/** Path loss has become unavailable. */
988 	BT_CONN_LE_PATH_LOSS_ZONE_UNAVAILABLE,
989 };
990 
991 BUILD_ASSERT(BT_CONN_LE_PATH_LOSS_ZONE_ENTERED_LOW == BT_HCI_LE_ZONE_ENTERED_LOW);
992 BUILD_ASSERT(BT_CONN_LE_PATH_LOSS_ZONE_ENTERED_MIDDLE == BT_HCI_LE_ZONE_ENTERED_MIDDLE);
993 BUILD_ASSERT(BT_CONN_LE_PATH_LOSS_ZONE_ENTERED_HIGH == BT_HCI_LE_ZONE_ENTERED_HIGH);
994 
995 /** @brief LE Path Loss Monitoring Threshold Change Report Structure. */
996 struct bt_conn_le_path_loss_threshold_report {
997 
998 	/** Path Loss zone as documented in Core Spec. Version 5.4 Vol.4, Part E, 7.7.65.32. */
999 	enum bt_conn_le_path_loss_zone zone;
1000 
1001 	/** Current path loss (dB). */
1002 	uint8_t path_loss;
1003 };
1004 
1005 /** @brief LE Path Loss Monitoring Parameters Structure as defined in Core Spec. Version 5.4
1006  *         Vol.4, Part E, 7.8.119 LE Set Path Loss Reporting Parameters command.
1007  */
1008 struct bt_conn_le_path_loss_reporting_param {
1009 	/** High threshold for the path loss (dB). */
1010 	uint8_t high_threshold;
1011 	/** Hysteresis value for the high threshold (dB). */
1012 	uint8_t high_hysteresis;
1013 	/** Low threshold for the path loss (dB). */
1014 	uint8_t low_threshold;
1015 	/** Hysteresis value for the low threshold (dB). */
1016 	uint8_t low_hysteresis;
1017 	/** Minimum time in number of connection events to be observed once the
1018 	 *  path loss crosses the threshold before an event is generated.
1019 	 */
1020 	uint16_t min_time_spent;
1021 };
1022 
1023 /** @brief Passkey Keypress Notification type
1024  *
1025  *  The numeric values are the same as in the Core specification for Pairing
1026  *  Keypress Notification PDU.
1027  */
1028 enum bt_conn_auth_keypress {
1029 	BT_CONN_AUTH_KEYPRESS_ENTRY_STARTED = 0x00,
1030 	BT_CONN_AUTH_KEYPRESS_DIGIT_ENTERED = 0x01,
1031 	BT_CONN_AUTH_KEYPRESS_DIGIT_ERASED = 0x02,
1032 	BT_CONN_AUTH_KEYPRESS_CLEARED = 0x03,
1033 	BT_CONN_AUTH_KEYPRESS_ENTRY_COMPLETED = 0x04,
1034 };
1035 
1036 /** @brief Get connection info
1037  *
1038  *  @param conn Connection object.
1039  *  @param info Connection info object.
1040  *
1041  *  @return Zero on success or (negative) error code on failure.
1042  */
1043 int bt_conn_get_info(const struct bt_conn *conn, struct bt_conn_info *info);
1044 
1045 /** @brief Function to determine the type of a connection
1046  *
1047  *  @param conn The connection object
1048  *  @param type The types to check against. It is possible to supply multiple types,
1049  *              e.g. BT_CONN_TYPE_LE | BT_CONN_TYPE_SCO.
1050  *
1051  *  @retval true @p conn is of type @p type
1052  *  @retval false @p conn is either NULL or not of type @p type
1053  */
1054 bool bt_conn_is_type(const struct bt_conn *conn, enum bt_conn_type type);
1055 
1056 /** @brief Get connection info for the remote device.
1057  *
1058  *  @param conn @ref BT_CONN_TYPE_LE or @ref BT_CONN_TYPE_BR connection object.
1059  *  @param remote_info Connection remote info object.
1060  *
1061  *  @note In order to retrieve the remote version (version, manufacturer
1062  *  and subversion) @kconfig{CONFIG_BT_REMOTE_VERSION} must be enabled
1063  *
1064  *  @note The remote information is exchanged directly after the connection has
1065  *  been established. The application can be notified about when the remote
1066  *  information is available through the remote_info_available callback.
1067  *
1068  *  @return Zero on success or (negative) error code on failure.
1069  *  @return -EBUSY The remote information is not yet available.
1070  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE or @ref BT_CONN_TYPE_BR connection.
1071  */
1072 int bt_conn_get_remote_info(const struct bt_conn *conn, struct bt_conn_remote_info *remote_info);
1073 
1074 /** @brief Get connection transmit power level.
1075  *
1076  *  @param conn           @ref BT_CONN_TYPE_LE connection object.
1077  *  @param tx_power_level Transmit power level descriptor.
1078  *
1079  *  @return Zero on success or (negative) error code on failure.
1080  *  @return -ENOBUFS HCI command buffer is not available.
1081  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE connection.
1082  */
1083 int bt_conn_le_get_tx_power_level(struct bt_conn *conn,
1084 				  struct bt_conn_le_tx_power *tx_power_level);
1085 
1086 /** @brief Get local enhanced connection transmit power level.
1087  *
1088  *  @param conn           @ref BT_CONN_TYPE_LE connection object.
1089  *  @param tx_power       Transmit power level descriptor.
1090  *
1091  *  @return Zero on success or (negative) error code on failure.
1092  *  @retval -ENOBUFS HCI command buffer is not available.
1093  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE connection.
1094  */
1095 int bt_conn_le_enhanced_get_tx_power_level(struct bt_conn *conn,
1096 					   struct bt_conn_le_tx_power *tx_power);
1097 
1098 /** @brief Get remote (peer) transmit power level.
1099  *
1100  *  @param conn           @ref BT_CONN_TYPE_LE connection object.
1101  *  @param phy            PHY information.
1102  *
1103  *  @return Zero on success or (negative) error code on failure.
1104  *  @retval -ENOBUFS HCI command buffer is not available.
1105  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE connection.
1106  */
1107 int bt_conn_le_get_remote_tx_power_level(struct bt_conn *conn,
1108 					 enum bt_conn_le_tx_power_phy phy);
1109 
1110 /** @brief Enable transmit power reporting.
1111  *
1112  *  @param conn           @ref BT_CONN_TYPE_LE connection object.
1113  *  @param local_enable   Enable/disable reporting for local.
1114  *  @param remote_enable  Enable/disable reporting for remote.
1115  *
1116  *  @return Zero on success or (negative) error code on failure.
1117  *  @retval -ENOBUFS HCI command buffer is not available.
1118  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE connection.
1119  */
1120 int bt_conn_le_set_tx_power_report_enable(struct bt_conn *conn,
1121 					  bool local_enable,
1122 					  bool remote_enable);
1123 
1124 /** @brief Set Path Loss Monitoring Parameters.
1125  *
1126  *  Change the configuration for path loss threshold change events for a given conn handle.
1127  *
1128  *  @note To use this API @kconfig{CONFIG_BT_PATH_LOSS_MONITORING} must be set.
1129  *
1130  *  @param conn  @ref BT_CONN_TYPE_LE connection object.
1131  *  @param param Path Loss Monitoring parameters
1132  *
1133  *  @return Zero on success or (negative) error code on failure.
1134  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE connection.
1135  */
1136 int bt_conn_le_set_path_loss_mon_param(struct bt_conn *conn,
1137 				       const struct bt_conn_le_path_loss_reporting_param *param);
1138 
1139 /** @brief Enable or Disable Path Loss Monitoring.
1140  *
1141  * Enable or disable Path Loss Monitoring, which will decide whether Path Loss Threshold events
1142  * are sent from the controller to the host.
1143  *
1144  * @note To use this API @kconfig{CONFIG_BT_PATH_LOSS_MONITORING} must be set.
1145  *
1146  * @param conn  @ref BT_CONN_TYPE_LE connection object.
1147  * @param enable Enable/disable path loss reporting.
1148  *
1149  * @return Zero on success or (negative) error code on failure.
1150  * @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE connection.
1151  */
1152 int bt_conn_le_set_path_loss_mon_enable(struct bt_conn *conn, bool enable);
1153 
1154 /** @brief Set Default Connection Subrating Parameters.
1155  *
1156  *  Change the default subrating parameters for all future
1157  *  ACL connections where the local device is the central.
1158  *  This command does not affect any existing connection.
1159  *  Parameters set for specific connection will always have precedence.
1160  *
1161  *  @note To use this API @kconfig{CONFIG_BT_SUBRATING} and
1162  *        @kconfig{CONFIG_BT_CENTRAL} must be set.
1163  *
1164  *  @param params Subrating parameters.
1165  *
1166  *  @return Zero on success or (negative) error code on failure.
1167  */
1168 int bt_conn_le_subrate_set_defaults(const struct bt_conn_le_subrate_param *params);
1169 
1170 /** @brief Request New Subrating Parameters.
1171  *
1172  *  Request a change to the subrating parameters of a connection.
1173  *
1174  *  @note To use this API @kconfig{CONFIG_BT_SUBRATING} must be set.
1175  *
1176  *  @param conn   @ref BT_CONN_TYPE_LE connection object.
1177  *  @param params Subrating parameters.
1178  *
1179  *  @return Zero on success or (negative) error code on failure.
1180  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE connection.
1181  */
1182 int bt_conn_le_subrate_request(struct bt_conn *conn,
1183 			       const struct bt_conn_le_subrate_param *params);
1184 
1185 /** @brief Update the connection parameters.
1186  *
1187  *  If the local device is in the peripheral role then updating the connection
1188  *  parameters will be delayed. This delay can be configured by through the
1189  *  @kconfig{CONFIG_BT_CONN_PARAM_UPDATE_TIMEOUT} option.
1190  *
1191  *  @param conn  @ref BT_CONN_TYPE_LE connection object.
1192  *  @param param Updated connection parameters.
1193  *
1194  *  @return Zero on success or (negative) error code on failure.
1195  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE connection.
1196  */
1197 int bt_conn_le_param_update(struct bt_conn *conn,
1198 			    const struct bt_le_conn_param *param);
1199 
1200 /** @brief Update the connection transmit data length parameters.
1201  *
1202  *  @param conn  @ref BT_CONN_TYPE_LE connection object.
1203  *  @param param Updated data length parameters.
1204  *
1205  *  @return Zero on success or (negative) error code on failure.
1206  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE connection.
1207  */
1208 int bt_conn_le_data_len_update(struct bt_conn *conn,
1209 			       const struct bt_conn_le_data_len_param *param);
1210 
1211 /** @brief Update the connection PHY parameters.
1212  *
1213  *  Update the preferred transmit and receive PHYs of the connection.
1214  *  Use @ref BT_GAP_LE_PHY_NONE to indicate no preference.
1215  *
1216  *  @param conn  @ref BT_CONN_TYPE_LE connection object.
1217  *  @param param Updated connection parameters.
1218  *
1219  *  @return Zero on success or (negative) error code on failure.
1220  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE connection.
1221  */
1222 int bt_conn_le_phy_update(struct bt_conn *conn,
1223 			  const struct bt_conn_le_phy_param *param);
1224 
1225 /** @brief Disconnect from a remote device or cancel pending connection.
1226  *
1227  *  Disconnect an active connection with the specified reason code or cancel
1228  *  pending outgoing connection.
1229  *
1230  *  The disconnect reason for a normal disconnect should be:
1231  *  @ref BT_HCI_ERR_REMOTE_USER_TERM_CONN.
1232  *
1233  *  The following disconnect reasons are accepted:
1234  *   - @ref BT_HCI_ERR_AUTH_FAIL
1235  *   - @ref BT_HCI_ERR_REMOTE_USER_TERM_CONN
1236  *   - @ref BT_HCI_ERR_REMOTE_LOW_RESOURCES
1237  *   - @ref BT_HCI_ERR_REMOTE_POWER_OFF
1238  *   - @ref BT_HCI_ERR_UNSUPP_REMOTE_FEATURE
1239  *   - @ref BT_HCI_ERR_PAIRING_NOT_SUPPORTED
1240  *   - @ref BT_HCI_ERR_UNACCEPT_CONN_PARAM
1241  *
1242  *  @param conn Connection to disconnect.
1243  *  @param reason Reason code for the disconnection.
1244  *
1245  *  @return Zero on success or (negative) error code on failure.
1246  */
1247 int bt_conn_disconnect(struct bt_conn *conn, uint8_t reason);
1248 
1249 enum {
1250 	/** Convenience value when no options are specified. */
1251 	BT_CONN_LE_OPT_NONE = 0,
1252 
1253 	/** @brief Enable LE Coded PHY.
1254 	 *
1255 	 *  Enable scanning on the LE Coded PHY.
1256 	 */
1257 	BT_CONN_LE_OPT_CODED = BIT(0),
1258 
1259 	/** @brief Disable LE 1M PHY.
1260 	 *
1261 	 *  Disable scanning on the LE 1M PHY.
1262 	 *
1263 	 *  @note Requires @ref BT_CONN_LE_OPT_CODED.
1264 	 */
1265 	BT_CONN_LE_OPT_NO_1M = BIT(1),
1266 };
1267 
1268 struct bt_conn_le_create_param {
1269 
1270 	/** Bit-field of create connection options. */
1271 	uint32_t options;
1272 
1273 	/** Scan interval (N * 0.625 ms)
1274 	 *
1275 	 * @note When @kconfig{CONFIG_BT_SCAN_AND_INITIATE_IN_PARALLEL} is enabled
1276 	 *       and the application wants to scan and connect in parallel,
1277 	 *       the Bluetooth Controller may require the scan interval used
1278 	 *       for scanning and connection establishment to be equal to
1279 	 *       obtain the best performance.
1280 	 */
1281 	uint16_t interval;
1282 
1283 	/** Scan window (N * 0.625 ms)
1284 	 *
1285 	 * @note When @kconfig{CONFIG_BT_SCAN_AND_INITIATE_IN_PARALLEL} is enabled
1286 	 *       and the application wants to scan and connect in parallel,
1287 	 *       the Bluetooth Controller may require the scan window used
1288 	 *       for scanning and connection establishment to be equal to
1289 	 *       obtain the best performance.
1290 	 */
1291 	uint16_t window;
1292 
1293 	/** @brief Scan interval LE Coded PHY (N * 0.625 MS)
1294 	 *
1295 	 *  Set zero to use same as LE 1M PHY scan interval
1296 	 */
1297 	uint16_t interval_coded;
1298 
1299 	/** @brief Scan window LE Coded PHY (N * 0.625 MS)
1300 	 *
1301 	 *  Set zero to use same as LE 1M PHY scan window.
1302 	 */
1303 	uint16_t window_coded;
1304 
1305 	/** @brief Connection initiation timeout (N * 10 MS)
1306 	 *
1307 	 *  Set zero to use the default @kconfig{CONFIG_BT_CREATE_CONN_TIMEOUT}
1308 	 *  timeout.
1309 	 *
1310 	 *  @note Unused in @ref bt_conn_le_create_auto
1311 	 */
1312 	uint16_t timeout;
1313 };
1314 
1315 /** @brief Initialize create connection parameters
1316  *
1317  *  @param _options  Create connection options.
1318  *  @param _interval Create connection scan interval (N * 0.625 ms).
1319  *  @param _window   Create connection scan window (N * 0.625 ms).
1320  */
1321 #define BT_CONN_LE_CREATE_PARAM_INIT(_options, _interval, _window) \
1322 { \
1323 	.options = (_options), \
1324 	.interval = (_interval), \
1325 	.window = (_window), \
1326 	.interval_coded = 0, \
1327 	.window_coded = 0, \
1328 	.timeout = 0, \
1329 }
1330 
1331 /** Helper to declare create connection parameters inline
1332  *
1333  *  @param _options  Create connection options.
1334  *  @param _interval Create connection scan interval (N * 0.625 ms).
1335  *  @param _window   Create connection scan window (N * 0.625 ms).
1336  */
1337 #define BT_CONN_LE_CREATE_PARAM(_options, _interval, _window) \
1338 	((struct bt_conn_le_create_param[]) { \
1339 		BT_CONN_LE_CREATE_PARAM_INIT(_options, _interval, _window) \
1340 	 })
1341 
1342 /** Default LE create connection parameters.
1343  *  Scan continuously by setting scan interval equal to scan window.
1344  */
1345 #define BT_CONN_LE_CREATE_CONN \
1346 	BT_CONN_LE_CREATE_PARAM(BT_CONN_LE_OPT_NONE, \
1347 				BT_GAP_SCAN_FAST_INTERVAL, \
1348 				BT_GAP_SCAN_FAST_INTERVAL)
1349 
1350 /** Default LE create connection using filter accept list parameters.
1351  *  Scan window:   30 ms.
1352  *  Scan interval: 60 ms.
1353  */
1354 #define BT_CONN_LE_CREATE_CONN_AUTO \
1355 	BT_CONN_LE_CREATE_PARAM(BT_CONN_LE_OPT_NONE, \
1356 				BT_GAP_SCAN_FAST_INTERVAL, \
1357 				BT_GAP_SCAN_FAST_WINDOW)
1358 
1359 /** @brief Initiate an LE connection to a remote device.
1360  *
1361  *  Allows initiate new LE link to remote peer using its address.
1362  *
1363  *  The caller gets a new reference to the connection object which must be
1364  *  released with bt_conn_unref() once done using the object. If
1365  *  @kconfig{CONFIG_BT_CONN_CHECK_NULL_BEFORE_CREATE} is enabled, this function
1366  *  will return -EINVAL if dereferenced @p conn is not NULL.
1367  *
1368  *  This uses the General Connection Establishment procedure.
1369  *
1370  *  The application must disable explicit scanning before initiating
1371  *  a new LE connection if @kconfig{CONFIG_BT_SCAN_AND_INITIATE_IN_PARALLEL}
1372  *  is not enabled.
1373  *
1374  *  @param[in]  peer         Remote address.
1375  *  @param[in]  create_param Create connection parameters.
1376  *  @param[in]  conn_param   Initial connection parameters.
1377  *  @param[out] conn         Valid connection object on success.
1378  *
1379  *  @return Zero on success or (negative) error code on failure.
1380  */
1381 int bt_conn_le_create(const bt_addr_le_t *peer,
1382 		      const struct bt_conn_le_create_param *create_param,
1383 		      const struct bt_le_conn_param *conn_param,
1384 		      struct bt_conn **conn);
1385 
1386 struct bt_conn_le_create_synced_param {
1387 
1388 	/** @brief Remote address
1389 	 *
1390 	 * The peer must be synchronized to the PAwR train.
1391 	 *
1392 	 */
1393 	const bt_addr_le_t *peer;
1394 
1395 	/** The subevent where the connection will be initiated. */
1396 	uint8_t subevent;
1397 };
1398 
1399 /** @brief Create a connection to a synced device
1400  *
1401  *  Initiate a connection to a synced device from a Periodic Advertising
1402  *  with Responses (PAwR) train.
1403  *
1404  *  The caller gets a new reference to the connection object which must be
1405  *  released with bt_conn_unref() once done using the object. If
1406  *  @kconfig{CONFIG_BT_CONN_CHECK_NULL_BEFORE_CREATE} is enabled, this function
1407  *  will return -EINVAL if dereferenced @p conn is not NULL.
1408  *
1409  *  This uses the Periodic Advertising Connection Procedure.
1410  *
1411  *  @param[in]  adv          The adverting set the PAwR advertiser belongs to.
1412  *  @param[in]  synced_param Create connection parameters.
1413  *  @param[in]  conn_param   Initial connection parameters.
1414  *  @param[out] conn         Valid connection object on success.
1415  *
1416  *  @return Zero on success or (negative) error code on failure.
1417  */
1418 int bt_conn_le_create_synced(const struct bt_le_ext_adv *adv,
1419 			     const struct bt_conn_le_create_synced_param *synced_param,
1420 			     const struct bt_le_conn_param *conn_param, struct bt_conn **conn);
1421 
1422 /** @brief Automatically connect to remote devices in the filter accept list.
1423  *
1424  *  This uses the Auto Connection Establishment procedure.
1425  *  The procedure will continue until a single connection is established or the
1426  *  procedure is stopped through @ref bt_conn_create_auto_stop.
1427  *  To establish connections to all devices in the filter accept list the
1428  *  procedure should be started again in the connected callback after a
1429  *  new connection has been established.
1430  *
1431  *  @param create_param Create connection parameters
1432  *  @param conn_param   Initial connection parameters.
1433  *
1434  *  @return Zero on success or (negative) error code on failure.
1435  *  @return -ENOMEM No free connection object available.
1436  */
1437 int bt_conn_le_create_auto(const struct bt_conn_le_create_param *create_param,
1438 			   const struct bt_le_conn_param *conn_param);
1439 
1440 /** @brief Stop automatic connect creation.
1441  *
1442  *  @return Zero on success or (negative) error code on failure.
1443  */
1444 int bt_conn_create_auto_stop(void);
1445 
1446 /** @brief Automatically connect to remote device if it's in range.
1447  *
1448  *  This function enables/disables automatic connection initiation.
1449  *  Every time the device loses the connection with peer, this connection
1450  *  will be re-established if connectable advertisement from peer is received.
1451  *
1452  *  @note Auto connect is disabled during explicit scanning.
1453  *
1454  *  @param addr Remote Bluetooth address.
1455  *  @param param If non-NULL, auto connect is enabled with the given
1456  *  parameters. If NULL, auto connect is disabled.
1457  *
1458  *  @return Zero on success or error code otherwise.
1459  */
1460 __deprecated int bt_le_set_auto_conn(const bt_addr_le_t *addr,
1461 				     const struct bt_le_conn_param *param);
1462 
1463 /** @brief Set security level for a connection.
1464  *
1465  *  This function enable security (encryption) for a connection. If the device
1466  *  has bond information for the peer with sufficiently strong key encryption
1467  *  will be enabled. If the connection is already encrypted with sufficiently
1468  *  strong key this function does nothing.
1469  *
1470  *  If the device has no bond information for the peer and is not already paired
1471  *  then the pairing procedure will be initiated. Note that @p sec has no effect
1472  *  on the security level selected for the pairing process. The selection is
1473  *  instead controlled by the values of the registered @ref bt_conn_auth_cb. If
1474  *  the device has bond information or is already paired and the keys are too
1475  *  weak then the pairing procedure will be initiated.
1476  *
1477  *  This function may return an error if the required level of security defined using
1478  *  @p sec is not possible to achieve due to local or remote device limitation
1479  *  (e.g., input output capabilities), or if the maximum number of paired devices
1480  *  has been reached.
1481  *
1482  *  This function may return an error if the pairing procedure has already been
1483  *  initiated by the local device or the peer device.
1484  *
1485  *  @note When @kconfig{CONFIG_BT_SMP_SC_ONLY} is enabled then the security
1486  *        level will always be level 4.
1487  *
1488  *  @note When @kconfig{CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY} is enabled then the
1489  *        security level will always be level 3.
1490  *
1491  *  @note When @ref BT_SECURITY_FORCE_PAIR within @p sec is enabled then the pairing
1492  *        procedure will always be initiated.
1493  *
1494  *  @param conn @ref BT_CONN_TYPE_LE or @ref BT_CONN_TYPE_BR connection object.
1495  *  @param sec Requested minimum security level.
1496  *
1497  *  @return 0 on success or negative error
1498  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE or @ref BT_CONN_TYPE_BR connection.
1499  */
1500 int bt_conn_set_security(struct bt_conn *conn, bt_security_t sec);
1501 
1502 /** @brief Get security level for a connection.
1503  *
1504  *  @param conn @ref BT_CONN_TYPE_LE or @ref BT_CONN_TYPE_BR connection object.
1505  *
1506  *  @return Connection security level if @kconfig{CONFIG_BT_SMP} or @kconfig{CONFIG_BT_CLASSIC} is
1507  *          enabled, else @ref BT_SECURITY_L1
1508  *  @return @ref BT_SECURITY_L0 @p conn is not a valid @ref BT_CONN_TYPE_LE or @ref BT_CONN_TYPE_BR
1509  *          connection.
1510  */
1511 bt_security_t bt_conn_get_security(const struct bt_conn *conn);
1512 
1513 /** @brief Get encryption key size.
1514  *
1515  *  This function gets encryption key size.
1516  *  If there is no security (encryption) enabled 0 will be returned.
1517  *
1518  *  @param conn @ref BT_CONN_TYPE_LE or @ref BT_CONN_TYPE_BR connection object.
1519  *
1520  *  @return Encryption key size.
1521  *  @return 0 @p conn is not a valid @ref BT_CONN_TYPE_LE or @ref BT_CONN_TYPE_BR connection.
1522  */
1523 uint8_t bt_conn_enc_key_size(const struct bt_conn *conn);
1524 
1525 enum bt_security_err {
1526 	/** Security procedure successful. */
1527 	BT_SECURITY_ERR_SUCCESS,
1528 
1529 	/** Authentication failed. */
1530 	BT_SECURITY_ERR_AUTH_FAIL,
1531 
1532 	/** PIN or encryption key is missing. */
1533 	BT_SECURITY_ERR_PIN_OR_KEY_MISSING,
1534 
1535 	/** OOB data is not available.  */
1536 	BT_SECURITY_ERR_OOB_NOT_AVAILABLE,
1537 
1538 	/** The requested security level could not be reached. */
1539 	BT_SECURITY_ERR_AUTH_REQUIREMENT,
1540 
1541 	/** Pairing is not supported */
1542 	BT_SECURITY_ERR_PAIR_NOT_SUPPORTED,
1543 
1544 	/** Pairing is not allowed. */
1545 	BT_SECURITY_ERR_PAIR_NOT_ALLOWED,
1546 
1547 	/** Invalid parameters. */
1548 	BT_SECURITY_ERR_INVALID_PARAM,
1549 
1550 	/** Distributed Key Rejected */
1551 	BT_SECURITY_ERR_KEY_REJECTED,
1552 
1553 	/** Pairing failed but the exact reason could not be specified. */
1554 	BT_SECURITY_ERR_UNSPECIFIED,
1555 };
1556 
1557 enum bt_conn_le_cs_procedure_enable_state {
1558 	BT_CONN_LE_CS_PROCEDURES_DISABLED = BT_HCI_OP_LE_CS_PROCEDURES_DISABLED,
1559 	BT_CONN_LE_CS_PROCEDURES_ENABLED = BT_HCI_OP_LE_CS_PROCEDURES_ENABLED,
1560 };
1561 
1562 /** CS Test Tone Antennna Config Selection.
1563  *
1564  *  These enum values are indices in the following table, where N_AP is the maximum
1565  *  number of antenna paths (in the range [1, 4]).
1566  *
1567  * +--------------+-------------+-------------------+-------------------+--------+
1568  * | Config Index | Total Paths | Dev A: # Antennas | Dev B: # Antennas | Config |
1569  * +--------------+-------------+-------------------+-------------------+--------+
1570  * |            0 |           1 |                 1 |                 1 | 1:1    |
1571  * |            1 |           2 |                 2 |                 1 | N_AP:1 |
1572  * |            2 |           3 |                 3 |                 1 | N_AP:1 |
1573  * |            3 |           4 |                 4 |                 1 | N_AP:1 |
1574  * |            4 |           2 |                 1 |                 2 | 1:N_AP |
1575  * |            5 |           3 |                 1 |                 3 | 1:N_AP |
1576  * |            6 |           4 |                 1 |                 4 | 1:N_AP |
1577  * |            7 |           4 |                 2 |                 2 | 2:2    |
1578  * +--------------+-------------+-------------------+-------------------+--------+
1579  *
1580  *  There are therefore four groups of possible antenna configurations:
1581  *
1582  *  - 1:1 configuration, where both A and B support 1 antenna each
1583  *  - 1:N_AP configuration, where A supports 1 antenna, B supports N_AP antennas, and
1584  *    N_AP is a value in the range [2, 4]
1585  *  - N_AP:1 configuration, where A supports N_AP antennas, B supports 1 antenna, and
1586  *    N_AP is a value in the range [2, 4]
1587  *  - 2:2 configuration, where both A and B support 2 antennas and N_AP = 4
1588  */
1589 enum bt_conn_le_cs_tone_antenna_config_selection {
1590 	BT_LE_CS_TONE_ANTENNA_CONFIGURATION_A1_B1 = BT_HCI_OP_LE_CS_ACI_0,
1591 	BT_LE_CS_TONE_ANTENNA_CONFIGURATION_A2_B1 = BT_HCI_OP_LE_CS_ACI_1,
1592 	BT_LE_CS_TONE_ANTENNA_CONFIGURATION_A3_B1 = BT_HCI_OP_LE_CS_ACI_2,
1593 	BT_LE_CS_TONE_ANTENNA_CONFIGURATION_A4_B1 = BT_HCI_OP_LE_CS_ACI_3,
1594 	BT_LE_CS_TONE_ANTENNA_CONFIGURATION_A1_B2 = BT_HCI_OP_LE_CS_ACI_4,
1595 	BT_LE_CS_TONE_ANTENNA_CONFIGURATION_A1_B3 = BT_HCI_OP_LE_CS_ACI_5,
1596 	BT_LE_CS_TONE_ANTENNA_CONFIGURATION_A1_B4 = BT_HCI_OP_LE_CS_ACI_6,
1597 	BT_LE_CS_TONE_ANTENNA_CONFIGURATION_A2_B2 = BT_HCI_OP_LE_CS_ACI_7,
1598 };
1599 
1600 struct bt_conn_le_cs_procedure_enable_complete {
1601 	/* The ID associated with the desired configuration (0 to 3) */
1602 	uint8_t config_id;
1603 
1604 	/* State of the CS procedure */
1605 	enum bt_conn_le_cs_procedure_enable_state state;
1606 
1607 	/* Antenna configuration index */
1608 	enum bt_conn_le_cs_tone_antenna_config_selection tone_antenna_config_selection;
1609 
1610 	/* Transmit power level used for CS procedures (-127 to 20 dB; 0x7F if unavailable) */
1611 	int8_t selected_tx_power;
1612 
1613 	/* Duration of each CS subevent in microseconds (1250 us to 4 s) */
1614 	uint32_t subevent_len;
1615 
1616 	/* Number of CS subevents anchored off the same ACL connection event (0x01 to 0x20) */
1617 	uint8_t subevents_per_event;
1618 
1619 	/* Time between consecutive CS subevents anchored off the same ACL connection event in
1620 	 * units of 0.625 ms
1621 	 */
1622 	uint16_t subevent_interval;
1623 
1624 	/* Number of ACL connection events between consecutive CS event anchor points */
1625 	uint16_t event_interval;
1626 
1627 	/* Number of ACL connection events between consecutive CS procedure anchor points */
1628 	uint16_t procedure_interval;
1629 
1630 	/* Number of CS procedures to be scheduled (0 if procedures to continue until disabled) */
1631 	uint16_t procedure_count;
1632 
1633 	/* Maximum duration for each procedure in units of 0.625 ms (0x0001 to 0xFFFF) */
1634 	uint16_t max_procedure_len;
1635 };
1636 
1637 /** @brief Connection callback structure.
1638  *
1639  *  This structure is used for tracking the state of a connection.
1640  *  It is registered with the help of the bt_conn_cb_register() API.
1641  *  It's permissible to register multiple instances of this @ref bt_conn_cb
1642  *  type, in case different modules of an application are interested in
1643  *  tracking the connection state. If a callback is not of interest for
1644  *  an instance, it may be set to NULL and will as a consequence not be
1645  *  used for that instance.
1646  */
1647 struct bt_conn_cb {
1648 	/** @brief A new connection has been established.
1649 	 *
1650 	 *  This callback notifies the application of a new connection.
1651 	 *  In case the err parameter is non-zero it means that the
1652 	 *  connection establishment failed.
1653 	 *
1654 	 *  @note If the connection was established from an advertising set then
1655 	 *        the advertising set cannot be restarted directly from this
1656 	 *        callback. Instead use the connected callback of the
1657 	 *        advertising set.
1658 	 *
1659 	 *  @param conn New connection object.
1660 	 *  @param err HCI error. Zero for success, non-zero otherwise.
1661 	 *
1662 	 *  @p err can mean either of the following:
1663 	 *  - @ref BT_HCI_ERR_UNKNOWN_CONN_ID Creating the connection started by
1664 	 *    @ref bt_conn_le_create was canceled either by the user through
1665 	 *    @ref bt_conn_disconnect or by the timeout in the host through
1666 	 *    @ref bt_conn_le_create_param timeout parameter, which defaults to
1667 	 *    @kconfig{CONFIG_BT_CREATE_CONN_TIMEOUT} seconds.
1668 	 *  - @p BT_HCI_ERR_ADV_TIMEOUT High duty cycle directed connectable
1669 	 *    advertiser started by @ref bt_le_adv_start failed to be connected
1670 	 *    within the timeout.
1671 	 */
1672 	void (*connected)(struct bt_conn *conn, uint8_t err);
1673 
1674 	/** @brief A connection has been disconnected.
1675 	 *
1676 	 *  This callback notifies the application that a connection
1677 	 *  has been disconnected.
1678 	 *
1679 	 *  When this callback is called the stack still has one reference to
1680 	 *  the connection object. If the application in this callback tries to
1681 	 *  start either a connectable advertiser or create a new connection
1682 	 *  this might fail because there are no free connection objects
1683 	 *  available.
1684 	 *  To avoid this issue it is recommended to either start connectable
1685 	 *  advertise or create a new connection using @ref k_work_submit or
1686 	 *  increase @kconfig{CONFIG_BT_MAX_CONN}.
1687 	 *
1688 	 *  @param conn Connection object.
1689 	 *  @param reason BT_HCI_ERR_* reason for the disconnection.
1690 	 */
1691 	void (*disconnected)(struct bt_conn *conn, uint8_t reason);
1692 
1693 	/** @brief A connection object has been returned to the pool.
1694 	 *
1695 	 * This callback notifies the application that it might be able to
1696 	 * allocate a connection object. No guarantee, first come, first serve.
1697 	 *
1698 	 * Use this to e.g. re-start connectable advertising or scanning.
1699 	 *
1700 	 * Treat this callback as an ISR, as it originates from
1701 	 * @ref bt_conn_unref which is used by the BT stack. Making
1702 	 * Bluetooth API calls in this context is error-prone and strongly
1703 	 * discouraged.
1704 	 */
1705 	void (*recycled)(void);
1706 
1707 	/** @brief LE connection parameter update request.
1708 	 *
1709 	 *  This callback notifies the application that a remote device
1710 	 *  is requesting to update the connection parameters. The
1711 	 *  application accepts the parameters by returning true, or
1712 	 *  rejects them by returning false. Before accepting, the
1713 	 *  application may also adjust the parameters to better suit
1714 	 *  its needs.
1715 	 *
1716 	 *  It is recommended for an application to have just one of these
1717 	 *  callbacks for simplicity. However, if an application registers
1718 	 *  multiple it needs to manage the potentially different
1719 	 *  requirements for each callback. Each callback gets the
1720 	 *  parameters as returned by previous callbacks, i.e. they are not
1721 	 *  necessarily the same ones as the remote originally sent.
1722 	 *
1723 	 *  If the application does not have this callback then the default
1724 	 *  is to accept the parameters.
1725 	 *
1726 	 *  @param conn Connection object.
1727 	 *  @param param Proposed connection parameters.
1728 	 *
1729 	 *  @return true to accept the parameters, or false to reject them.
1730 	 */
1731 	bool (*le_param_req)(struct bt_conn *conn,
1732 			     struct bt_le_conn_param *param);
1733 
1734 	/** @brief The parameters for an LE connection have been updated.
1735 	 *
1736 	 *  This callback notifies the application that the connection
1737 	 *  parameters for an LE connection have been updated.
1738 	 *
1739 	 *  @param conn Connection object.
1740 	 *  @param interval Connection interval.
1741 	 *  @param latency Connection latency.
1742 	 *  @param timeout Connection supervision timeout.
1743 	 */
1744 	void (*le_param_updated)(struct bt_conn *conn, uint16_t interval,
1745 				 uint16_t latency, uint16_t timeout);
1746 #if defined(CONFIG_BT_SMP)
1747 	/** @brief Remote Identity Address has been resolved.
1748 	 *
1749 	 *  This callback notifies the application that a remote
1750 	 *  Identity Address has been resolved
1751 	 *
1752 	 *  @param conn Connection object.
1753 	 *  @param rpa Resolvable Private Address.
1754 	 *  @param identity Identity Address.
1755 	 */
1756 	void (*identity_resolved)(struct bt_conn *conn,
1757 				  const bt_addr_le_t *rpa,
1758 				  const bt_addr_le_t *identity);
1759 #endif /* CONFIG_BT_SMP */
1760 #if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_CLASSIC)
1761 	/** @brief The security level of a connection has changed.
1762 	 *
1763 	 *  This callback notifies the application that the security of a
1764 	 *  connection has changed.
1765 	 *
1766 	 *  The security level of the connection can either have been increased
1767 	 *  or remain unchanged. An increased security level means that the
1768 	 *  pairing procedure has been performed or the bond information from
1769 	 *  a previous connection has been applied. If the security level
1770 	 *  remains unchanged this means that the encryption key has been
1771 	 *  refreshed for the connection.
1772 	 *
1773 	 *  @param conn Connection object.
1774 	 *  @param level New security level of the connection.
1775 	 *  @param err Security error. Zero for success, non-zero otherwise.
1776 	 */
1777 	void (*security_changed)(struct bt_conn *conn, bt_security_t level,
1778 				 enum bt_security_err err);
1779 #endif /* defined(CONFIG_BT_SMP) || defined(CONFIG_BT_CLASSIC) */
1780 
1781 #if defined(CONFIG_BT_REMOTE_INFO)
1782 	/** @brief Remote information procedures has completed.
1783 	 *
1784 	 *  This callback notifies the application that the remote information
1785 	 *  has been retrieved from the remote peer.
1786 	 *
1787 	 *  @param conn Connection object.
1788 	 *  @param remote_info Connection information of remote device.
1789 	 */
1790 	void (*remote_info_available)(struct bt_conn *conn,
1791 				      struct bt_conn_remote_info *remote_info);
1792 #endif /* defined(CONFIG_BT_REMOTE_INFO) */
1793 
1794 #if defined(CONFIG_BT_USER_PHY_UPDATE)
1795 	/** @brief The PHY of the connection has changed.
1796 	 *
1797 	 *  This callback notifies the application that the PHY of the
1798 	 *  connection has changed.
1799 	 *
1800 	 *  @param conn Connection object.
1801 	 *  @param info Connection LE PHY information.
1802 	 */
1803 	void (*le_phy_updated)(struct bt_conn *conn,
1804 			       struct bt_conn_le_phy_info *param);
1805 #endif /* defined(CONFIG_BT_USER_PHY_UPDATE) */
1806 
1807 #if defined(CONFIG_BT_USER_DATA_LEN_UPDATE)
1808 	/** @brief The data length parameters of the connection has changed.
1809 	 *
1810 	 *  This callback notifies the application that the maximum Link Layer
1811 	 *  payload length or transmission time has changed.
1812 	 *
1813 	 *  @param conn Connection object.
1814 	 *  @param info Connection data length information.
1815 	 */
1816 	void (*le_data_len_updated)(struct bt_conn *conn,
1817 				    struct bt_conn_le_data_len_info *info);
1818 #endif /* defined(CONFIG_BT_USER_DATA_LEN_UPDATE) */
1819 
1820 #if defined(CONFIG_BT_DF_CONNECTION_CTE_RX)
1821 	/** @brief Callback for IQ samples report collected when sampling
1822 	 *        CTE received by data channel PDU.
1823 	 *
1824 	 * @param conn      The connection object.
1825 	 * @param iq_report Report data for collected IQ samples.
1826 	 */
1827 	void (*cte_report_cb)(struct bt_conn *conn,
1828 			      const struct bt_df_conn_iq_samples_report *iq_report);
1829 #endif /* CONFIG_BT_DF_CONNECTION_CTE_RX */
1830 
1831 #if defined(CONFIG_BT_TRANSMIT_POWER_CONTROL)
1832 	/** @brief LE Read Remote Transmit Power Level procedure has completed or LE
1833 	 *  Transmit Power Reporting event.
1834 	 *
1835 	 *  This callback notifies the application that either the remote transmit power level
1836 	 *  has been read from the peer or transmit power level has changed for the local or
1837 	 *  remote controller when transmit power reporting is enabled for the respective side
1838 	 *  using @ref bt_conn_le_set_tx_power_report_enable.
1839 	 *
1840 	 *  @param conn Connection object.
1841 	 *  @param report Transmit power report.
1842 	 */
1843 	void (*tx_power_report)(struct bt_conn *conn,
1844 				const struct bt_conn_le_tx_power_report *report);
1845 #endif /* CONFIG_BT_TRANSMIT_POWER_CONTROL */
1846 
1847 #if defined(CONFIG_BT_PATH_LOSS_MONITORING)
1848 	/** @brief LE Path Loss Threshold event.
1849 	 *
1850 	 *  This callback notifies the application that there has been a path loss threshold
1851 	 *  crossing or reporting the initial path loss threshold zone after using
1852 	 *  @ref bt_conn_le_set_path_loss_mon_enable.
1853 	 *
1854 	 *  @param conn Connection object.
1855 	 *  @param report Path loss threshold report.
1856 	 */
1857 	void (*path_loss_threshold_report)(struct bt_conn *conn,
1858 				const struct bt_conn_le_path_loss_threshold_report *report);
1859 #endif /* CONFIG_BT_PATH_LOSS_MONITORING */
1860 
1861 #if defined(CONFIG_BT_SUBRATING)
1862 	/** @brief LE Subrate Changed event.
1863 	 *
1864 	 *  This callback notifies the application that the subrating parameters
1865 	 *  of the connection may have changed.
1866 	 *  The connection subrating parameters will be unchanged
1867 	 *  if status is not BT_HCI_ERR_SUCCESS.
1868 	 *
1869 	 *  @param conn   Connection object.
1870 	 *  @param params New subrating parameters.
1871 	 */
1872 	void (*subrate_changed)(struct bt_conn *conn,
1873 				const struct bt_conn_le_subrate_changed *params);
1874 #endif /* CONFIG_BT_SUBRATING */
1875 
1876 #if defined(CONFIG_BT_CHANNEL_SOUNDING)
1877 	/** @brief LE CS Read Remote Supported Capabilities Complete event.
1878 	 *
1879 	 *  This callback notifies the application that the remote channel
1880 	 *  sounding capabilities have been received from the peer.
1881 	 *
1882 	 *  @param conn Connection object.
1883 	 *  @param remote_cs_capabilities Remote Channel Sounding Capabilities.
1884 	 */
1885 	void (*le_cs_remote_capabilities_available)(struct bt_conn *conn,
1886 						    struct bt_conn_le_cs_capabilities *params);
1887 
1888 	/** @brief LE CS Read Remote FAE Table Complete event.
1889 	 *
1890 	 *  This callback notifies the application that the remote mode-0
1891 	 *  FAE Table has been received from the peer.
1892 	 *
1893 	 *  @param conn Connection object.
1894 	 *  @param params FAE Table.
1895 	 */
1896 	void (*le_cs_remote_fae_table_available)(struct bt_conn *conn,
1897 						 struct bt_conn_le_cs_fae_table *params);
1898 
1899 	/** @brief LE CS Config created.
1900 	 *
1901 	 *  This callback notifies the application that a Channel Sounding
1902 	 *  Configuration procedure has completed and a new CS config is created
1903 	 *
1904 	 *  @param conn Connection object.
1905 	 *  @param config CS configuration.
1906 	 */
1907 	void (*le_cs_config_created)(struct bt_conn *conn, struct bt_conn_le_cs_config *config);
1908 
1909 	/** @brief LE CS Config removed.
1910 	 *
1911 	 *  This callback notifies the application that a Channel Sounding
1912 	 *  Configuration procedure has completed and a CS config is removed
1913 	 *
1914 	 *  @param conn Connection object.
1915 	 *  @param config_id ID of the CS configuration that was removed.
1916 	 */
1917 	void (*le_cs_config_removed)(struct bt_conn *conn, uint8_t config_id);
1918 
1919 	/** @brief Subevent Results from a CS procedure are available.
1920 	 *
1921 	 * This callback notifies the user that CS subevent results are
1922 	 * available for the given connection object.
1923 	 *
1924 	 * @param conn Connection objects.
1925 	 * @param result Subevent results
1926 	 */
1927 	void (*le_cs_subevent_data_available)(struct bt_conn *conn,
1928 					      struct bt_conn_le_cs_subevent_result *result);
1929 
1930 	/** @brief LE CS Security Enabled.
1931 	 *
1932 	 *  This callback notifies the application that a Channel Sounding
1933 	 *  Security Enable procedure has completed
1934 	 *
1935 	 *  @param conn Connection object.
1936 	 */
1937 	void (*le_cs_security_enabled)(struct bt_conn *conn);
1938 
1939 	/** @brief LE CS Procedure Enabled.
1940 	 *
1941 	 *  This callback notifies the application that a Channel Sounding
1942 	 *  Procedure Enable procedure has completed
1943 	 *
1944 	 *  @param conn Connection object.
1945 	 *  @param params CS Procedure Enable parameters
1946 	 */
1947 	void (*le_cs_procedure_enabled)(
1948 		struct bt_conn *conn, struct bt_conn_le_cs_procedure_enable_complete *params);
1949 
1950 #endif
1951 
1952 	/** @internal Internally used field for list handling */
1953 	sys_snode_t _node;
1954 };
1955 
1956 /** @brief Register connection callbacks.
1957  *
1958  *  Register callbacks to monitor the state of connections.
1959  *
1960  *  @param cb Callback struct. Must point to memory that remains valid.
1961  *
1962  * @retval 0 Success.
1963  * @retval -EEXIST if @p cb was already registered.
1964  */
1965 int bt_conn_cb_register(struct bt_conn_cb *cb);
1966 
1967 /**
1968  * @brief Unregister connection callbacks.
1969  *
1970  * Unregister the state of connections callbacks.
1971  *
1972  * @param cb Callback struct point to memory that remains valid.
1973  *
1974  * @retval 0 Success
1975  * @retval -EINVAL If @p cb is NULL
1976  * @retval -ENOENT if @p cb was not registered
1977  */
1978 int bt_conn_cb_unregister(struct bt_conn_cb *cb);
1979 
1980 /**
1981  *  @brief Register a callback structure for connection events.
1982  *
1983  *  @param _name Name of callback structure.
1984  */
1985 #define BT_CONN_CB_DEFINE(_name)					\
1986 	static const STRUCT_SECTION_ITERABLE(bt_conn_cb,		\
1987 						_CONCAT(bt_conn_cb_,	\
1988 							_name))
1989 
1990 /** Converts a security error to string.
1991  *
1992  * @return The string representation of the security error code.
1993  *         If @kconfig{CONFIG_BT_SECURITY_ERR_TO_STR} is not enabled,
1994  *         this just returns the empty string
1995  */
1996 #if defined(CONFIG_BT_SECURITY_ERR_TO_STR)
1997 const char *bt_security_err_to_str(enum bt_security_err err);
1998 #else
bt_security_err_to_str(enum bt_security_err err)1999 static inline const char *bt_security_err_to_str(enum bt_security_err err)
2000 {
2001 	ARG_UNUSED(err);
2002 
2003 	return "";
2004 }
2005 #endif
2006 
2007 /** @brief Enable/disable bonding.
2008  *
2009  *  Set/clear the Bonding flag in the Authentication Requirements of
2010  *  SMP Pairing Request/Response data.
2011  *  The initial value of this flag depends on BT_BONDABLE Kconfig setting.
2012  *  For the vast majority of applications calling this function shouldn't be
2013  *  needed.
2014  *
2015  *  @param enable Value allowing/disallowing to be bondable.
2016  */
2017 void bt_set_bondable(bool enable);
2018 
2019 /** @brief Get bonding flag.
2020  *
2021  *  Get current bonding flag.
2022  *  The initial value of this flag depends on @kconfig{CONFIG_BT_BONDABLE} Kconfig
2023  *  setting.
2024  *  The Bonding flag can be updated using bt_set_bondable().
2025  *
2026  *  @return Current bonding flag.
2027  */
2028 bool bt_get_bondable(void);
2029 
2030 /** @brief Set/clear the bonding flag for a given connection.
2031  *
2032  *  Set/clear the Bonding flag in the Authentication Requirements of
2033  *  SMP Pairing Request/Response data for a given connection.
2034  *
2035  *  The bonding flag for a given connection cannot be set/cleared if
2036  *  security procedures in the SMP module have already started.
2037  *  This function can be called only once per connection.
2038  *
2039  *  If the bonding flag is not set/cleared for a given connection,
2040  *  the value will depend on global configuration which is set using
2041  *  bt_set_bondable.
2042  *  The default value of the global configuration is defined using
2043  *  CONFIG_BT_BONDABLE Kconfig option.
2044  *
2045  *  @param conn @ref BT_CONN_TYPE_LE or @ref BT_CONN_TYPE_BR connection object.
2046  *  @param enable Value allowing/disallowing to be bondable.
2047  *
2048  *  @retval 0 Success
2049  *  @retval -EALREADY Already in the requested state
2050  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE or @ref BT_CONN_TYPE_BR connection
2051  *  @return -EINVAL @p conn is a valid @ref BT_CONN_TYPE_LE but could not get SMP context
2052  */
2053 int bt_conn_set_bondable(struct bt_conn *conn, bool enable);
2054 
2055 /** @brief Allow/disallow remote LE SC OOB data to be used for pairing.
2056  *
2057  *  Set/clear the OOB data flag for LE SC SMP Pairing Request/Response data.
2058  *
2059  *  @param enable Value allowing/disallowing remote LE SC OOB data.
2060  */
2061 void bt_le_oob_set_sc_flag(bool enable);
2062 
2063 /** @brief Allow/disallow remote legacy OOB data to be used for pairing.
2064  *
2065  *  Set/clear the OOB data flag for legacy SMP Pairing Request/Response data.
2066  *
2067  *  @param enable Value allowing/disallowing remote legacy OOB data.
2068  */
2069 void bt_le_oob_set_legacy_flag(bool enable);
2070 
2071 /** @brief Set OOB Temporary Key to be used for pairing
2072  *
2073  *  This function allows to set OOB data for the LE legacy pairing procedure.
2074  *  The function should only be called in response to the oob_data_request()
2075  *  callback provided that the legacy method is user pairing.
2076  *
2077  *  @param conn  @ref BT_CONN_TYPE_LE connection object.
2078  *  @param tk Pointer to 16 byte long TK array
2079  *
2080  *  @retval 0 Success
2081  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE connection.
2082  *  @return -EINVAL @p tk is NULL.
2083  */
2084 int bt_le_oob_set_legacy_tk(struct bt_conn *conn, const uint8_t *tk);
2085 
2086 /** @brief Set OOB data during LE Secure Connections (SC) pairing procedure
2087  *
2088  *  This function allows to set OOB data during the LE SC pairing procedure.
2089  *  The function should only be called in response to the oob_data_request()
2090  *  callback provided that LE SC method is used for pairing.
2091  *
2092  *  The user should submit OOB data according to the information received in the
2093  *  callback. This may yield three different configurations: with only local OOB
2094  *  data present, with only remote OOB data present or with both local and
2095  *  remote OOB data present.
2096  *
2097  *  @param conn  @ref BT_CONN_TYPE_LE connection object.
2098  *  @param oobd_local Local OOB data or NULL if not present
2099  *  @param oobd_remote Remote OOB data or NULL if not present
2100  *
2101  *  @return Zero on success or error code otherwise, positive in case of
2102  *          protocol error or negative (POSIX) in case of stack internal error.
2103  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE connection.
2104  */
2105 int bt_le_oob_set_sc_data(struct bt_conn *conn,
2106 			  const struct bt_le_oob_sc_data *oobd_local,
2107 			  const struct bt_le_oob_sc_data *oobd_remote);
2108 
2109 /** @brief Get OOB data used for LE Secure Connections (SC) pairing procedure
2110  *
2111  *  This function allows to get OOB data during the LE SC pairing procedure that
2112  *  were set by the bt_le_oob_set_sc_data() API.
2113  *
2114  *  @note The OOB data will only be available as long as the connection object
2115  *  associated with it is valid.
2116  *
2117  *  @param conn  @ref BT_CONN_TYPE_LE connection object.
2118  *  @param oobd_local Local OOB data or NULL if not set
2119  *  @param oobd_remote Remote OOB data or NULL if not set
2120  *
2121  *  @return Zero on success or error code otherwise, positive in case of
2122  *          protocol error or negative (POSIX) in case of stack internal error.
2123  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE connection.
2124  */
2125 int bt_le_oob_get_sc_data(struct bt_conn *conn,
2126 			  const struct bt_le_oob_sc_data **oobd_local,
2127 			  const struct bt_le_oob_sc_data **oobd_remote);
2128 
2129 /**
2130  *  Special passkey value that can be used to disable a previously
2131  *  set fixed passkey.
2132  */
2133 #define BT_PASSKEY_INVALID 0xffffffff
2134 
2135 /** @brief Set a fixed passkey to be used for pairing.
2136  *
2137  *  This API is only available when the CONFIG_BT_FIXED_PASSKEY
2138  *  configuration option has been enabled.
2139  *
2140  *  Sets a fixed passkey to be used for pairing. If set, the
2141  *  pairing_confirm() callback will be called for all incoming pairings.
2142  *
2143  *  @param passkey A valid passkey (0 - 999999) or BT_PASSKEY_INVALID
2144  *                 to disable a previously set fixed passkey.
2145  *
2146  *  @return 0 on success or a negative error code on failure.
2147  */
2148 int bt_passkey_set(unsigned int passkey);
2149 
2150 /** Info Structure for OOB pairing */
2151 struct bt_conn_oob_info {
2152 	/** Type of OOB pairing method */
2153 	enum {
2154 		/** LE legacy pairing */
2155 		BT_CONN_OOB_LE_LEGACY,
2156 
2157 		/** LE SC pairing */
2158 		BT_CONN_OOB_LE_SC,
2159 	} type;
2160 
2161 	union {
2162 		/** LE Secure Connections OOB pairing parameters */
2163 		struct {
2164 			/** OOB data configuration */
2165 			enum {
2166 				/** Local OOB data requested */
2167 				BT_CONN_OOB_LOCAL_ONLY,
2168 
2169 				/** Remote OOB data requested */
2170 				BT_CONN_OOB_REMOTE_ONLY,
2171 
2172 				/** Both local and remote OOB data requested */
2173 				BT_CONN_OOB_BOTH_PEERS,
2174 
2175 				/** No OOB data requested */
2176 				BT_CONN_OOB_NO_DATA,
2177 			} oob_config;
2178 		} lesc;
2179 	};
2180 };
2181 
2182 #if defined(CONFIG_BT_SMP_APP_PAIRING_ACCEPT)
2183 /** @brief Pairing request and pairing response info structure.
2184  *
2185  *  This structure is the same for both smp_pairing_req and smp_pairing_rsp
2186  *  and a subset of the packet data, except for the initial Code octet.
2187  *  It is documented in Core Spec. Vol. 3, Part H, 3.5.1 and 3.5.2.
2188  */
2189 struct bt_conn_pairing_feat {
2190 	/** IO Capability, Core Spec. Vol 3, Part H, 3.5.1, Table 3.4 */
2191 	uint8_t io_capability;
2192 
2193 	/** OOB data flag, Core Spec. Vol 3, Part H, 3.5.1, Table 3.5 */
2194 	uint8_t oob_data_flag;
2195 
2196 	/** AuthReq, Core Spec. Vol 3, Part H, 3.5.1, Fig. 3.3 */
2197 	uint8_t auth_req;
2198 
2199 	/** Maximum Encryption Key Size, Core Spec. Vol 3, Part H, 3.5.1 */
2200 	uint8_t max_enc_key_size;
2201 
2202 	/** Initiator Key Distribution/Generation, Core Spec. Vol 3, Part H,
2203 	 *  3.6.1, Fig. 3.11
2204 	 */
2205 	uint8_t init_key_dist;
2206 
2207 	/** Responder Key Distribution/Generation, Core Spec. Vol 3, Part H
2208 	 *  3.6.1, Fig. 3.11
2209 	 */
2210 	uint8_t resp_key_dist;
2211 };
2212 #endif /* CONFIG_BT_SMP_APP_PAIRING_ACCEPT */
2213 
2214 /** Authenticated pairing callback structure */
2215 struct bt_conn_auth_cb {
2216 #if defined(CONFIG_BT_SMP_APP_PAIRING_ACCEPT)
2217 	/** @brief Query to proceed incoming pairing or not.
2218 	 *
2219 	 *  On any incoming pairing req/rsp this callback will be called for
2220 	 *  the application to decide whether to allow for the pairing to
2221 	 *  continue.
2222 	 *
2223 	 *  The pairing info received from the peer is passed to assist
2224 	 *  making the decision.
2225 	 *
2226 	 *  As this callback is synchronous the application should return
2227 	 *  a response value immediately. Otherwise it may affect the
2228 	 *  timing during pairing. Hence, this information should not be
2229 	 *  conveyed to the user to take action.
2230 	 *
2231 	 *  The remaining callbacks are not affected by this, but do notice
2232 	 *  that other callbacks can be called during the pairing. Eg. if
2233 	 *  pairing_confirm is registered both will be called for Just-Works
2234 	 *  pairings.
2235 	 *
2236 	 *  This callback may be unregistered in which case pairing continues
2237 	 *  as if the Kconfig flag was not set.
2238 	 *
2239 	 *  For BR/EDR Secure Simple Pairing (SSP), this callback is called
2240 	 *  when receiving the BT_HCI_EVT_IO_CAPA_REQ hci event.
2241 	 *
2242 	 *  @param conn Connection where pairing is initiated.
2243 	 *  @param feat Pairing req/resp info.
2244 	 */
2245 	enum bt_security_err (*pairing_accept)(struct bt_conn *conn,
2246 			      const struct bt_conn_pairing_feat *const feat);
2247 #endif /* CONFIG_BT_SMP_APP_PAIRING_ACCEPT */
2248 
2249 	/** @brief Display a passkey to the user.
2250 	 *
2251 	 *  When called the application is expected to display the given
2252 	 *  passkey to the user, with the expectation that the passkey will
2253 	 *  then be entered on the peer device. The passkey will be in the
2254 	 *  range of 0 - 999999, and is expected to be padded with zeroes so
2255 	 *  that six digits are always shown. E.g. the value 37 should be
2256 	 *  shown as 000037.
2257 	 *
2258 	 *  This callback may be set to NULL, which means that the local
2259 	 *  device lacks the ability do display a passkey. If set
2260 	 *  to non-NULL the cancel callback must also be provided, since
2261 	 *  this is the only way the application can find out that it should
2262 	 *  stop displaying the passkey.
2263 	 *
2264 	 *  @param conn Connection where pairing is currently active.
2265 	 *  @param passkey Passkey to show to the user.
2266 	 */
2267 	void (*passkey_display)(struct bt_conn *conn, unsigned int passkey);
2268 
2269 #if defined(CONFIG_BT_PASSKEY_KEYPRESS)
2270 	/** @brief Receive Passkey Keypress Notification during pairing
2271 	 *
2272 	 *  This allows the remote device to use the local device to give users
2273 	 *  feedback on the progress of entering the passkey over there. This is
2274 	 *  useful when the remote device itself has no display suitable for
2275 	 *  showing the progress.
2276 	 *
2277 	 *  The handler of this callback is expected to keep track of the number
2278 	 *  of digits entered and show a password-field-like feedback to the
2279 	 *  user.
2280 	 *
2281 	 *  This callback is only relevant while the local side does Passkey
2282 	 *  Display.
2283 	 *
2284 	 *  The event type is verified to be in range of the enum. No other
2285 	 *  sanitization has been done. The remote could send a large number of
2286 	 *  events of any type in any order.
2287 	 *
2288 	 *  @param conn The related connection.
2289 	 *  @param type Type of event. Verified in range of the enum.
2290 	 */
2291 	void (*passkey_display_keypress)(struct bt_conn *conn,
2292 					 enum bt_conn_auth_keypress type);
2293 #endif
2294 
2295 	/** @brief Request the user to enter a passkey.
2296 	 *
2297 	 *  When called the user is expected to enter a passkey. The passkey
2298 	 *  must be in the range of 0 - 999999, and should be expected to
2299 	 *  be zero-padded, as that's how the peer device will typically be
2300 	 *  showing it (e.g. 37 would be shown as 000037).
2301 	 *
2302 	 *  Once the user has entered the passkey its value should be given
2303 	 *  to the stack using the bt_conn_auth_passkey_entry() API.
2304 	 *
2305 	 *  This callback may be set to NULL, which means that the local
2306 	 *  device lacks the ability to enter a passkey. If set to non-NULL
2307 	 *  the cancel callback must also be provided, since this is the
2308 	 *  only way the application can find out that it should stop
2309 	 *  requesting the user to enter a passkey.
2310 	 *
2311 	 *  @param conn Connection where pairing is currently active.
2312 	 */
2313 	void (*passkey_entry)(struct bt_conn *conn);
2314 
2315 	/** @brief Request the user to confirm a passkey.
2316 	 *
2317 	 *  When called the user is expected to confirm that the given
2318 	 *  passkey is also shown on the peer device.. The passkey will
2319 	 *  be in the range of 0 - 999999, and should be zero-padded to
2320 	 *  always be six digits (e.g. 37 would be shown as 000037).
2321 	 *
2322 	 *  Once the user has confirmed the passkey to match, the
2323 	 *  bt_conn_auth_passkey_confirm() API should be called. If the
2324 	 *  user concluded that the passkey doesn't match the
2325 	 *  bt_conn_auth_cancel() API should be called.
2326 	 *
2327 	 *  This callback may be set to NULL, which means that the local
2328 	 *  device lacks the ability to confirm a passkey. If set to non-NULL
2329 	 *  the cancel callback must also be provided, since this is the
2330 	 *  only way the application can find out that it should stop
2331 	 *  requesting the user to confirm a passkey.
2332 	 *
2333 	 *  @param conn Connection where pairing is currently active.
2334 	 *  @param passkey Passkey to be confirmed.
2335 	 */
2336 	void (*passkey_confirm)(struct bt_conn *conn, unsigned int passkey);
2337 
2338 	/** @brief Request the user to provide Out of Band (OOB) data.
2339 	 *
2340 	 *  When called the user is expected to provide OOB data. The required
2341 	 *  data are indicated by the information structure.
2342 	 *
2343 	 *  For LE Secure Connections OOB pairing, the user should provide
2344 	 *  local OOB data, remote OOB data or both depending on their
2345 	 *  availability. Their value should be given to the stack using the
2346 	 *  bt_le_oob_set_sc_data() API.
2347 	 *
2348 	 *  This callback must be set to non-NULL in order to support OOB
2349 	 *  pairing.
2350 	 *
2351 	 *  @param conn Connection where pairing is currently active.
2352 	 *  @param info OOB pairing information.
2353 	 */
2354 	void (*oob_data_request)(struct bt_conn *conn,
2355 				 struct bt_conn_oob_info *info);
2356 
2357 	/** @brief Cancel the ongoing user request.
2358 	 *
2359 	 *  This callback will be called to notify the application that it
2360 	 *  should cancel any previous user request (passkey display, entry
2361 	 *  or confirmation).
2362 	 *
2363 	 *  This may be set to NULL, but must always be provided whenever the
2364 	 *  passkey_display, passkey_entry passkey_confirm or pairing_confirm
2365 	 *  callback has been provided.
2366 	 *
2367 	 *  @param conn Connection where pairing is currently active.
2368 	 */
2369 	void (*cancel)(struct bt_conn *conn);
2370 
2371 	/** @brief Request confirmation for an incoming pairing.
2372 	 *
2373 	 *  This callback will be called to confirm an incoming pairing
2374 	 *  request where none of the other user callbacks is applicable.
2375 	 *
2376 	 *  If the user decides to accept the pairing the
2377 	 *  bt_conn_auth_pairing_confirm() API should be called. If the
2378 	 *  user decides to reject the pairing the bt_conn_auth_cancel() API
2379 	 *  should be called.
2380 	 *
2381 	 *  This callback may be set to NULL, which means that the local
2382 	 *  device lacks the ability to confirm a pairing request. If set
2383 	 *  to non-NULL the cancel callback must also be provided, since
2384 	 *  this is the only way the application can find out that it should
2385 	 *  stop requesting the user to confirm a pairing request.
2386 	 *
2387 	 *  @param conn Connection where pairing is currently active.
2388 	 */
2389 	void (*pairing_confirm)(struct bt_conn *conn);
2390 
2391 #if defined(CONFIG_BT_CLASSIC)
2392 	/** @brief Request the user to enter a passkey.
2393 	 *
2394 	 *  This callback will be called for a BR/EDR (Bluetooth Classic)
2395 	 *  connection where pairing is being performed. Once called the
2396 	 *  user is expected to enter a PIN code with a length between
2397 	 *  1 and 16 digits. If the @a highsec parameter is set to true
2398 	 *  the PIN code must be 16 digits long.
2399 	 *
2400 	 *  Once entered, the PIN code should be given to the stack using
2401 	 *  the bt_conn_auth_pincode_entry() API.
2402 	 *
2403 	 *  This callback may be set to NULL, however in that case pairing
2404 	 *  over BR/EDR will not be possible. If provided, the cancel
2405 	 *  callback must be provided as well.
2406 	 *
2407 	 *  @param conn Connection where pairing is currently active.
2408 	 *  @param highsec true if 16 digit PIN is required.
2409 	 */
2410 	void (*pincode_entry)(struct bt_conn *conn, bool highsec);
2411 #endif
2412 };
2413 
2414 /** Authenticated pairing information callback structure */
2415 struct bt_conn_auth_info_cb {
2416 	/** @brief notify that pairing procedure was complete.
2417 	 *
2418 	 *  This callback notifies the application that the pairing procedure
2419 	 *  has been completed.
2420 	 *
2421 	 *  @param conn Connection object.
2422 	 *  @param bonded Bond information has been distributed during the
2423 	 *                pairing procedure.
2424 	 */
2425 	void (*pairing_complete)(struct bt_conn *conn, bool bonded);
2426 
2427 	/** @brief notify that pairing process has failed.
2428 	 *
2429 	 *  @param conn Connection object.
2430 	 *  @param reason Pairing failed reason
2431 	 */
2432 	void (*pairing_failed)(struct bt_conn *conn,
2433 			       enum bt_security_err reason);
2434 
2435 	/** @brief Notify that bond has been deleted.
2436 	 *
2437 	 *  This callback notifies the application that the bond information
2438 	 *  for the remote peer has been deleted
2439 	 *
2440 	 *  @param id   Which local identity had the bond.
2441 	 *  @param peer Remote address.
2442 	 */
2443 	void (*bond_deleted)(uint8_t id, const bt_addr_le_t *peer);
2444 
2445 	/** Internally used field for list handling */
2446 	sys_snode_t node;
2447 };
2448 
2449 /** @brief Register authentication callbacks.
2450  *
2451  *  Register callbacks to handle authenticated pairing. Passing NULL
2452  *  unregisters a previous callbacks structure.
2453  *
2454  *  @param cb Callback struct.
2455  *
2456  *  @return Zero on success or negative error code otherwise
2457  */
2458 int bt_conn_auth_cb_register(const struct bt_conn_auth_cb *cb);
2459 
2460 /** @brief Overlay authentication callbacks used for a given connection.
2461  *
2462  *  This function can be used only for Bluetooth LE connections.
2463  *  The @kconfig{CONFIG_BT_SMP} must be enabled for this function.
2464  *
2465  *  The authentication callbacks for a given connection cannot be overlaid if
2466  *  security procedures in the SMP module have already started. This function
2467  *  can be called only once per connection.
2468  *
2469  *  @param conn @ref BT_CONN_TYPE_LE or @ref BT_CONN_TYPE_BR connection object.
2470  *  @param cb	Callback struct.
2471  *
2472  *  @return Zero on success or negative error code otherwise
2473  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE or @ref BT_CONN_TYPE_BR connection
2474  */
2475 int bt_conn_auth_cb_overlay(struct bt_conn *conn, const struct bt_conn_auth_cb *cb);
2476 
2477 /** @brief Register authentication information callbacks.
2478  *
2479  *  Register callbacks to get authenticated pairing information. Multiple
2480  *  registrations can be done.
2481  *
2482  *  @param cb Callback struct.
2483  *
2484  *  @return Zero on success or negative error code otherwise
2485  */
2486 int bt_conn_auth_info_cb_register(struct bt_conn_auth_info_cb *cb);
2487 
2488 /** @brief Unregister authentication information callbacks.
2489  *
2490  *  Unregister callbacks to stop getting authenticated pairing information.
2491  *
2492  *  @param cb Callback struct.
2493  *
2494  *  @return Zero on success or negative error code otherwise
2495  */
2496 int bt_conn_auth_info_cb_unregister(struct bt_conn_auth_info_cb *cb);
2497 
2498 /** @brief Reply with entered passkey.
2499  *
2500  *  This function should be called only after passkey_entry callback from
2501  *  bt_conn_auth_cb structure was called.
2502  *
2503  *  @param conn @ref BT_CONN_TYPE_LE or @ref BT_CONN_TYPE_BR connection object.
2504  *  @param passkey Entered passkey.
2505  *
2506  *  @return Zero on success or negative error code otherwise
2507  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE or @ref BT_CONN_TYPE_BR connection
2508  */
2509 int bt_conn_auth_passkey_entry(struct bt_conn *conn, unsigned int passkey);
2510 
2511 /** @brief Send Passkey Keypress Notification during pairing
2512  *
2513  *  This function may be called only after passkey_entry callback from
2514  *  bt_conn_auth_cb structure was called.
2515  *
2516  *  Requires @kconfig{CONFIG_BT_PASSKEY_KEYPRESS}.
2517  *
2518  *  @param conn @ref BT_CONN_TYPE_LE destination connection for the notification.
2519  *  @param type What keypress event type to send. @see bt_conn_auth_keypress.
2520  *
2521  *  @retval 0 Success
2522  *  @retval -EINVAL Improper use of the API.
2523  *  @retval -ENOMEM Failed to allocate.
2524  *  @retval -ENOBUFS Failed to allocate.
2525  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE connection
2526  */
2527 int bt_conn_auth_keypress_notify(struct bt_conn *conn, enum bt_conn_auth_keypress type);
2528 
2529 /** @brief Cancel ongoing authenticated pairing.
2530  *
2531  *  This function allows to cancel ongoing authenticated pairing.
2532  *
2533  *  @param conn @ref BT_CONN_TYPE_LE or @ref BT_CONN_TYPE_BR connection object.
2534  *
2535  *  @return Zero on success or negative error code otherwise
2536  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE or @ref BT_CONN_TYPE_BR connection
2537  */
2538 int bt_conn_auth_cancel(struct bt_conn *conn);
2539 
2540 /** @brief Reply if passkey was confirmed to match by user.
2541  *
2542  *  This function should be called only after passkey_confirm callback from
2543  *  bt_conn_auth_cb structure was called.
2544  *
2545  *  @param conn @ref BT_CONN_TYPE_LE or @ref BT_CONN_TYPE_BR connection object.
2546  *
2547  *  @return Zero on success or negative error code otherwise
2548  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE or @ref BT_CONN_TYPE_BR connection
2549  */
2550 int bt_conn_auth_passkey_confirm(struct bt_conn *conn);
2551 
2552 /** @brief Reply if incoming pairing was confirmed by user.
2553  *
2554  *  This function should be called only after pairing_confirm callback from
2555  *  bt_conn_auth_cb structure was called if user confirmed incoming pairing.
2556  *
2557  *  @param conn @ref BT_CONN_TYPE_LE or @ref BT_CONN_TYPE_BR connection object.
2558  *
2559  *  @return Zero on success or negative error code otherwise
2560  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_LE or @ref BT_CONN_TYPE_BR connection
2561  */
2562 int bt_conn_auth_pairing_confirm(struct bt_conn *conn);
2563 
2564 /** @brief Reply with entered PIN code.
2565  *
2566  *  This function should be called only after PIN code callback from
2567  *  bt_conn_auth_cb structure was called. It's for legacy 2.0 devices.
2568  *
2569  *  @param conn @ref BT_CONN_TYPE_BR connection object.
2570  *  @param pin Entered PIN code.
2571  *
2572  *  @return Zero on success or negative error code otherwise
2573  *  @return -EINVAL @p conn is not a valid @ref BT_CONN_TYPE_BR connection
2574  */
2575 int bt_conn_auth_pincode_entry(struct bt_conn *conn, const char *pin);
2576 
2577 /** Connection parameters for BR/EDR connections */
2578 struct bt_br_conn_param {
2579 	bool allow_role_switch;
2580 };
2581 
2582 /** @brief Initialize BR/EDR connection parameters
2583  *
2584  *  @param role_switch True if role switch is allowed
2585  */
2586 #define BT_BR_CONN_PARAM_INIT(role_switch) \
2587 { \
2588 	.allow_role_switch = (role_switch), \
2589 }
2590 
2591 /** Helper to declare BR/EDR connection parameters inline
2592   *
2593   * @param role_switch True if role switch is allowed
2594   */
2595 #define BT_BR_CONN_PARAM(role_switch) \
2596 	((struct bt_br_conn_param[]) { \
2597 		BT_BR_CONN_PARAM_INIT(role_switch) \
2598 	 })
2599 
2600 /** Default BR/EDR connection parameters:
2601  *    Role switch allowed
2602  */
2603 #define BT_BR_CONN_PARAM_DEFAULT BT_BR_CONN_PARAM(true)
2604 
2605 
2606 /** @brief Initiate an BR/EDR connection to a remote device.
2607  *
2608  *  Allows initiate new BR/EDR link to remote peer using its address.
2609  *
2610  *  The caller gets a new reference to the connection object which must be
2611  *  released with bt_conn_unref() once done using the object.
2612  *
2613  *  @param peer  Remote address.
2614  *  @param param Initial connection parameters.
2615  *
2616  *  @return Valid connection object on success or NULL otherwise.
2617  */
2618 struct bt_conn *bt_conn_create_br(const bt_addr_t *peer,
2619 				  const struct bt_br_conn_param *param);
2620 
2621 #ifdef __cplusplus
2622 }
2623 #endif
2624 
2625 /**
2626  * @}
2627  */
2628 
2629 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_CONN_H_ */
2630