1 /** @file
2  *  @brief Bluetooth connection handling
3  */
4 
5 /*
6  * Copyright (c) 2015-2016 Intel Corporation
7  *
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 #ifndef ZEPHYR_INCLUDE_BLUETOOTH_CONN_H_
11 #define ZEPHYR_INCLUDE_BLUETOOTH_CONN_H_
12 
13 /**
14  * @brief Connection management
15  * @defgroup bt_conn Connection management
16  * @ingroup bluetooth
17  * @{
18  */
19 
20 #include <stdbool.h>
21 #include <stdint.h>
22 
23 #include <zephyr/bluetooth/bluetooth.h>
24 #include <zephyr/bluetooth/hci_types.h>
25 #include <zephyr/bluetooth/addr.h>
26 #include <zephyr/bluetooth/gap.h>
27 #include <zephyr/bluetooth/direction.h>
28 #include <zephyr/sys/iterable_sections.h>
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /** Opaque type representing a connection to a remote device */
35 struct bt_conn;
36 
37 /** Connection parameters for LE connections */
38 struct bt_le_conn_param {
39 	uint16_t interval_min;
40 	uint16_t interval_max;
41 	uint16_t latency;
42 	uint16_t timeout;
43 };
44 
45 /** @brief Initialize connection parameters
46  *
47  *  @param int_min  Minimum Connection Interval (N * 1.25 ms)
48  *  @param int_max  Maximum Connection Interval (N * 1.25 ms)
49  *  @param lat      Connection Latency
50  *  @param to       Supervision Timeout (N * 10 ms)
51  */
52 #define BT_LE_CONN_PARAM_INIT(int_min, int_max, lat, to) \
53 { \
54 	.interval_min = (int_min), \
55 	.interval_max = (int_max), \
56 	.latency = (lat), \
57 	.timeout = (to), \
58 }
59 
60 /** Helper to declare connection parameters inline
61  *
62  *  @param int_min  Minimum Connection Interval (N * 1.25 ms)
63  *  @param int_max  Maximum Connection Interval (N * 1.25 ms)
64  *  @param lat      Connection Latency
65  *  @param to       Supervision Timeout (N * 10 ms)
66  */
67 #define BT_LE_CONN_PARAM(int_min, int_max, lat, to) \
68 	((struct bt_le_conn_param[]) { \
69 		BT_LE_CONN_PARAM_INIT(int_min, int_max, lat, to) \
70 	 })
71 
72 /** Default LE connection parameters:
73  *    Connection Interval: 30-50 ms
74  *    Latency: 0
75  *    Timeout: 4 s
76  */
77 #define BT_LE_CONN_PARAM_DEFAULT BT_LE_CONN_PARAM(BT_GAP_INIT_CONN_INT_MIN, \
78 						  BT_GAP_INIT_CONN_INT_MAX, \
79 						  0, 400)
80 
81 /** Connection PHY information for LE connections */
82 struct bt_conn_le_phy_info {
83 	uint8_t tx_phy; /** Connection transmit PHY */
84 	uint8_t rx_phy; /** Connection receive PHY */
85 };
86 
87 /** Connection PHY options */
88 enum {
89 	/** Convenience value when no options are specified. */
90 	BT_CONN_LE_PHY_OPT_NONE = 0,
91 
92 	/** LE Coded using S=2 coding preferred when transmitting. */
93 	BT_CONN_LE_PHY_OPT_CODED_S2  = BIT(0),
94 
95 	/** LE Coded using S=8 coding preferred when transmitting. */
96 	BT_CONN_LE_PHY_OPT_CODED_S8  = BIT(1),
97 };
98 
99 /** Preferred PHY parameters for LE connections */
100 struct bt_conn_le_phy_param {
101 	uint16_t options; /**< Connection PHY options. */
102 	uint8_t  pref_tx_phy; /**< Bitmask of preferred transmit PHYs */
103 	uint8_t  pref_rx_phy; /**< Bitmask of preferred receive PHYs */
104 };
105 
106 /** Initialize PHY parameters
107  *
108  * @param _pref_tx_phy Bitmask of preferred transmit PHYs.
109  * @param _pref_rx_phy Bitmask of preferred receive PHYs.
110  */
111 #define BT_CONN_LE_PHY_PARAM_INIT(_pref_tx_phy, _pref_rx_phy) \
112 { \
113 	.options = BT_CONN_LE_PHY_OPT_NONE, \
114 	.pref_tx_phy = (_pref_tx_phy), \
115 	.pref_rx_phy = (_pref_rx_phy), \
116 }
117 
118 /** Helper to declare PHY parameters inline
119  *
120  * @param _pref_tx_phy Bitmask of preferred transmit PHYs.
121  * @param _pref_rx_phy Bitmask of preferred receive PHYs.
122  */
123 #define BT_CONN_LE_PHY_PARAM(_pref_tx_phy, _pref_rx_phy) \
124 	((struct bt_conn_le_phy_param []) { \
125 		BT_CONN_LE_PHY_PARAM_INIT(_pref_tx_phy, _pref_rx_phy) \
126 	 })
127 
128 /** Only LE 1M PHY */
129 #define BT_CONN_LE_PHY_PARAM_1M BT_CONN_LE_PHY_PARAM(BT_GAP_LE_PHY_1M, \
130 						     BT_GAP_LE_PHY_1M)
131 
132 /** Only LE 2M PHY */
133 #define BT_CONN_LE_PHY_PARAM_2M BT_CONN_LE_PHY_PARAM(BT_GAP_LE_PHY_2M, \
134 						     BT_GAP_LE_PHY_2M)
135 
136 /** Only LE Coded PHY. */
137 #define BT_CONN_LE_PHY_PARAM_CODED BT_CONN_LE_PHY_PARAM(BT_GAP_LE_PHY_CODED, \
138 							BT_GAP_LE_PHY_CODED)
139 
140 /** All LE PHYs. */
141 #define BT_CONN_LE_PHY_PARAM_ALL BT_CONN_LE_PHY_PARAM(BT_GAP_LE_PHY_1M |   \
142 						      BT_GAP_LE_PHY_2M |   \
143 						      BT_GAP_LE_PHY_CODED, \
144 						      BT_GAP_LE_PHY_1M |   \
145 						      BT_GAP_LE_PHY_2M |   \
146 						      BT_GAP_LE_PHY_CODED)
147 
148 /** Connection data length information for LE connections */
149 struct bt_conn_le_data_len_info {
150 	/** Maximum Link Layer transmission payload size in bytes. */
151 	uint16_t tx_max_len;
152 	/** Maximum Link Layer transmission payload time in us. */
153 	uint16_t tx_max_time;
154 	/** Maximum Link Layer reception payload size in bytes. */
155 	uint16_t rx_max_len;
156 	/** Maximum Link Layer reception payload time in us. */
157 	uint16_t rx_max_time;
158 };
159 
160 /** Connection data length parameters for LE connections */
161 struct bt_conn_le_data_len_param {
162 	/** Maximum Link Layer transmission payload size in bytes. */
163 	uint16_t tx_max_len;
164 	/** Maximum Link Layer transmission payload time in us. */
165 	uint16_t tx_max_time;
166 };
167 
168 /** Initialize transmit data length parameters
169  *
170  * @param  _tx_max_len  Maximum Link Layer transmission payload size in bytes.
171  * @param  _tx_max_time Maximum Link Layer transmission payload time in us.
172  */
173 #define BT_CONN_LE_DATA_LEN_PARAM_INIT(_tx_max_len, _tx_max_time) \
174 { \
175 	.tx_max_len = (_tx_max_len), \
176 	.tx_max_time = (_tx_max_time), \
177 }
178 
179 /** Helper to declare transmit data length parameters inline
180  *
181  * @param  _tx_max_len  Maximum Link Layer transmission payload size in bytes.
182  * @param  _tx_max_time Maximum Link Layer transmission payload time in us.
183  */
184 #define BT_CONN_LE_DATA_LEN_PARAM(_tx_max_len, _tx_max_time) \
185 	((struct bt_conn_le_data_len_param[]) { \
186 		BT_CONN_LE_DATA_LEN_PARAM_INIT(_tx_max_len, _tx_max_time) \
187 	 })
188 
189 /** Default LE data length parameters. */
190 #define BT_LE_DATA_LEN_PARAM_DEFAULT \
191 	BT_CONN_LE_DATA_LEN_PARAM(BT_GAP_DATA_LEN_DEFAULT, \
192 				  BT_GAP_DATA_TIME_DEFAULT)
193 
194 /** Maximum LE data length parameters. */
195 #define BT_LE_DATA_LEN_PARAM_MAX \
196 	BT_CONN_LE_DATA_LEN_PARAM(BT_GAP_DATA_LEN_MAX, \
197 				  BT_GAP_DATA_TIME_MAX)
198 
199 /** Connection Type */
200 enum __packed bt_conn_type {
201 	/** LE Connection Type */
202 	BT_CONN_TYPE_LE = BIT(0),
203 	/** BR/EDR Connection Type */
204 	BT_CONN_TYPE_BR = BIT(1),
205 	/** SCO Connection Type */
206 	BT_CONN_TYPE_SCO = BIT(2),
207 	/** ISO Connection Type */
208 	BT_CONN_TYPE_ISO = BIT(3),
209 	/** All Connection Type */
210 	BT_CONN_TYPE_ALL = BT_CONN_TYPE_LE | BT_CONN_TYPE_BR |
211 			   BT_CONN_TYPE_SCO | BT_CONN_TYPE_ISO,
212 };
213 
214 /** @brief Increment a connection's reference count.
215  *
216  *  Increment the reference count of a connection object.
217  *
218  *  @note Will return NULL if the reference count is zero.
219  *
220  *  @param conn Connection object.
221  *
222  *  @return Connection object with incremented reference count, or NULL if the
223  *          reference count is zero.
224  */
225 struct bt_conn *bt_conn_ref(struct bt_conn *conn);
226 
227 /** @brief Decrement a connection's reference count.
228  *
229  *  Decrement the reference count of a connection object.
230  *
231  *  @param conn Connection object.
232  */
233 void bt_conn_unref(struct bt_conn *conn);
234 
235 /** @brief Iterate through all bt_conn objects.
236  *
237  * Iterates trough all bt_conn objects that are alive in the Host allocator.
238  *
239  * To find established connections, combine this with @ref bt_conn_get_info.
240  * Check that @ref bt_conn_info.state is @ref BT_CONN_STATE_CONNECTED.
241  *
242  * Thread safety: This API is thread safe, but it does not guarantee a
243  * sequentially-consistent view for objects allocated during the current
244  * invocation of this API. E.g. If preempted while allocations A then B then C
245  * happen then results may include A and C but miss B.
246  *
247  * @param type  Connection Type
248  * @param func  Function to call for each connection.
249  * @param data  Data to pass to the callback function.
250  */
251 void bt_conn_foreach(enum bt_conn_type type,
252 		     void (*func)(struct bt_conn *conn, void *data),
253 		     void *data);
254 
255 /** @brief Look up an existing connection by address.
256  *
257  *  Look up an existing connection based on the remote address.
258  *
259  *  The caller gets a new reference to the connection object which must be
260  *  released with bt_conn_unref() once done using the object.
261  *
262  *  @param id   Local identity (in most cases BT_ID_DEFAULT).
263  *  @param peer Remote address.
264  *
265  *  @return Connection object or NULL if not found.
266  */
267 struct bt_conn *bt_conn_lookup_addr_le(uint8_t id, const bt_addr_le_t *peer);
268 
269 /** @brief Get destination (peer) address of a connection.
270  *
271  *  @param conn Connection object.
272  *
273  *  @return Destination address.
274  */
275 const bt_addr_le_t *bt_conn_get_dst(const struct bt_conn *conn);
276 
277 /** @brief Get array index of a connection
278  *
279  *  This function is used to map bt_conn to index of an array of
280  *  connections. The array has CONFIG_BT_MAX_CONN elements.
281  *
282  *  @param conn Connection object.
283  *
284  *  @return Index of the connection object.
285  *          The range of the returned value is 0..CONFIG_BT_MAX_CONN-1
286  */
287 uint8_t bt_conn_index(const struct bt_conn *conn);
288 
289 /** LE Connection Info Structure */
290 struct bt_conn_le_info {
291 	/** Source (Local) Identity Address */
292 	const bt_addr_le_t *src;
293 	/** Destination (Remote) Identity Address or remote Resolvable Private
294 	 *  Address (RPA) before identity has been resolved.
295 	 */
296 	const bt_addr_le_t *dst;
297 	/** Local device address used during connection setup. */
298 	const bt_addr_le_t *local;
299 	/** Remote device address used during connection setup. */
300 	const bt_addr_le_t *remote;
301 	uint16_t interval; /**< Connection interval */
302 	uint16_t latency; /**< Connection peripheral latency */
303 	uint16_t timeout; /**< Connection supervision timeout */
304 
305 #if defined(CONFIG_BT_USER_PHY_UPDATE)
306 	const struct bt_conn_le_phy_info      *phy;
307 #endif /* defined(CONFIG_BT_USER_PHY_UPDATE) */
308 
309 #if defined(CONFIG_BT_USER_DATA_LEN_UPDATE)
310 	/* Connection maximum single fragment parameters */
311 	const struct bt_conn_le_data_len_info *data_len;
312 #endif /* defined(CONFIG_BT_USER_DATA_LEN_UPDATE) */
313 };
314 
315 /** @brief Convert connection interval to milliseconds
316  *
317  *  Multiply by 1.25 to get milliseconds.
318  *
319  *  Note that this may be inaccurate, as something like 7.5 ms cannot be
320  *  accurately presented with integers.
321  */
322 #define BT_CONN_INTERVAL_TO_MS(interval) ((interval) * 5U / 4U)
323 
324 /** @brief Convert connection interval to microseconds
325  *
326  *  Multiply by 1250 to get microseconds.
327  */
328 #define BT_CONN_INTERVAL_TO_US(interval) ((interval) * 1250U)
329 
330 /** BR/EDR Connection Info Structure */
331 struct bt_conn_br_info {
332 	const bt_addr_t *dst; /**< Destination (Remote) BR/EDR address */
333 };
334 
335 enum {
336 	BT_CONN_ROLE_CENTRAL = 0,
337 	BT_CONN_ROLE_PERIPHERAL = 1,
338 };
339 
340 enum bt_conn_state {
341 	/** Channel disconnected */
342 	BT_CONN_STATE_DISCONNECTED,
343 	/** Channel in connecting state */
344 	BT_CONN_STATE_CONNECTING,
345 	/** Channel connected and ready for upper layer traffic on it */
346 	BT_CONN_STATE_CONNECTED,
347 	/** Channel in disconnecting state */
348 	BT_CONN_STATE_DISCONNECTING,
349 };
350 
351 /** Security level. */
352 typedef enum __packed {
353 	/** Level 0: Only for BR/EDR special cases, like SDP */
354 	BT_SECURITY_L0,
355 	/** Level 1: No encryption and no authentication. */
356 	BT_SECURITY_L1,
357 	/** Level 2: Encryption and no authentication (no MITM). */
358 	BT_SECURITY_L2,
359 	/** Level 3: Encryption and authentication (MITM). */
360 	BT_SECURITY_L3,
361 	/** Level 4: Authenticated Secure Connections and 128-bit key. */
362 	BT_SECURITY_L4,
363 	/** Bit to force new pairing procedure, bit-wise OR with requested
364 	 *  security level.
365 	 */
366 	BT_SECURITY_FORCE_PAIR = BIT(7),
367 } bt_security_t;
368 
369 /** Security Info Flags. */
370 enum bt_security_flag {
371 	/** Paired with Secure Connections. */
372 	BT_SECURITY_FLAG_SC = BIT(0),
373 	/** Paired with Out of Band method. */
374 	BT_SECURITY_FLAG_OOB = BIT(1),
375 };
376 
377 /** Security Info Structure. */
378 struct bt_security_info {
379 	/** Security Level. */
380 	bt_security_t level;
381 	/** Encryption Key Size. */
382 	uint8_t enc_key_size;
383 	/** Flags. */
384 	enum bt_security_flag flags;
385 };
386 
387 /** Connection role (central or peripheral) */
388 #define BT_CONN_ROLE_MASTER __DEPRECATED_MACRO BT_CONN_ROLE_CENTRAL
389 #define BT_CONN_ROLE_SLAVE __DEPRECATED_MACRO BT_CONN_ROLE_PERIPHERAL
390 
391 /** Connection Info Structure */
392 struct bt_conn_info {
393 	/** Connection Type. */
394 	enum bt_conn_type type;
395 	/** Connection Role. */
396 	uint8_t role;
397 	/** Which local identity the connection was created with */
398 	uint8_t id;
399 	/** Connection Type specific Info.*/
400 	union {
401 		/** LE Connection specific Info. */
402 		struct bt_conn_le_info le;
403 		/** BR/EDR Connection specific Info. */
404 		struct bt_conn_br_info br;
405 	};
406 	/** Connection state. */
407 	enum bt_conn_state state;
408 	/** Security specific info. */
409 	struct bt_security_info security;
410 };
411 
412 /** LE Connection Remote Info Structure */
413 struct bt_conn_le_remote_info {
414 
415 	/** Remote LE feature set (bitmask). */
416 	const uint8_t *features;
417 };
418 
419 /** BR/EDR Connection Remote Info structure */
420 struct bt_conn_br_remote_info {
421 
422 	/** Remote feature set (pages of bitmasks). */
423 	const uint8_t *features;
424 
425 	/** Number of pages in the remote feature set. */
426 	uint8_t num_pages;
427 };
428 
429 /** @brief Connection Remote Info Structure
430  *
431  *  @note The version, manufacturer and subversion fields will only contain
432  *        valid data if @kconfig{CONFIG_BT_REMOTE_VERSION} is enabled.
433  */
434 struct bt_conn_remote_info {
435 	/** Connection Type */
436 	uint8_t  type;
437 
438 	/** Remote Link Layer version */
439 	uint8_t  version;
440 
441 	/** Remote manufacturer identifier */
442 	uint16_t manufacturer;
443 
444 	/** Per-manufacturer unique revision */
445 	uint16_t subversion;
446 
447 	union {
448 		/** LE connection remote info */
449 		struct bt_conn_le_remote_info le;
450 
451 		/** BR/EDR connection remote info */
452 		struct bt_conn_br_remote_info br;
453 	};
454 };
455 
456 enum bt_conn_le_tx_power_phy {
457 	/** Convenience macro for when no PHY is set. */
458 	BT_CONN_LE_TX_POWER_PHY_NONE,
459 	/** LE 1M PHY */
460 	BT_CONN_LE_TX_POWER_PHY_1M,
461 	 /** LE 2M PHY */
462 	BT_CONN_LE_TX_POWER_PHY_2M,
463 	/** LE Coded PHY using S=8 coding. */
464 	BT_CONN_LE_TX_POWER_PHY_CODED_S8,
465 	/** LE Coded PHY using S=2 coding. */
466 	BT_CONN_LE_TX_POWER_PHY_CODED_S2,
467 };
468 
469 /** LE Transmit Power Level Structure */
470 struct bt_conn_le_tx_power {
471 
472 	/** Input: 1M, 2M, Coded S2 or Coded S8 */
473 	uint8_t phy;
474 
475 	/** Output: current transmit power level */
476 	int8_t current_level;
477 
478 	/** Output: maximum transmit power level */
479 	int8_t max_level;
480 };
481 
482 
483 /** LE Transmit Power Reporting Structure */
484 struct bt_conn_le_tx_power_report {
485 
486 	/** Reason for Transmit power reporting,
487 	 * as documented in Core Spec. Version 5.4 Vol. 4, Part E, 7.7.65.33.
488 	 */
489 	uint8_t reason;
490 
491 	/** Phy of Transmit power reporting. */
492 	enum bt_conn_le_tx_power_phy phy;
493 
494 	/** Transmit power level
495 	 * - 0xXX - Transmit power level
496 	 *  + Range: -127 to 20
497 	 *  + Units: dBm
498 	 *
499 	 * - 0x7E - Remote device is not managing power levels on this PHY.
500 	 * - 0x7F - Transmit power level is not available
501 	 */
502 	int8_t tx_power_level;
503 
504 	/** Bit 0: Transmit power level is at minimum level.
505 	 *  Bit 1: Transmit power level is at maximum level.
506 	 */
507 	uint8_t tx_power_level_flag;
508 
509 	/** Change in transmit power level
510 	 * - 0xXX - Change in transmit power level (positive indicates increased
511 	 *   power, negative indicates decreased power, zero indicates unchanged)
512 	 *   Units: dB
513 	 * - 0x7F - Change is not available or is out of range.
514 	 */
515 	int8_t delta;
516 };
517 
518 /** @brief Passkey Keypress Notification type
519  *
520  *  The numeric values are the same as in the Core specification for Pairing
521  *  Keypress Notification PDU.
522  */
523 enum bt_conn_auth_keypress {
524 	BT_CONN_AUTH_KEYPRESS_ENTRY_STARTED = 0x00,
525 	BT_CONN_AUTH_KEYPRESS_DIGIT_ENTERED = 0x01,
526 	BT_CONN_AUTH_KEYPRESS_DIGIT_ERASED = 0x02,
527 	BT_CONN_AUTH_KEYPRESS_CLEARED = 0x03,
528 	BT_CONN_AUTH_KEYPRESS_ENTRY_COMPLETED = 0x04,
529 };
530 
531 /** @brief Get connection info
532  *
533  *  @param conn Connection object.
534  *  @param info Connection info object.
535  *
536  *  @return Zero on success or (negative) error code on failure.
537  */
538 int bt_conn_get_info(const struct bt_conn *conn, struct bt_conn_info *info);
539 
540 /** @brief Get connection info for the remote device.
541  *
542  *  @param conn Connection object.
543  *  @param remote_info Connection remote info object.
544  *
545  *  @note In order to retrieve the remote version (version, manufacturer
546  *  and subversion) @kconfig{CONFIG_BT_REMOTE_VERSION} must be enabled
547  *
548  *  @note The remote information is exchanged directly after the connection has
549  *  been established. The application can be notified about when the remote
550  *  information is available through the remote_info_available callback.
551  *
552  *  @return Zero on success or (negative) error code on failure.
553  *  @return -EBUSY The remote information is not yet available.
554  */
555 int bt_conn_get_remote_info(struct bt_conn *conn,
556 			    struct bt_conn_remote_info *remote_info);
557 
558 /** @brief Get connection transmit power level.
559  *
560  *  @param conn           Connection object.
561  *  @param tx_power_level Transmit power level descriptor.
562  *
563  *  @return Zero on success or (negative) error code on failure.
564  *  @return -ENOBUFS HCI command buffer is not available.
565  */
566 int bt_conn_le_get_tx_power_level(struct bt_conn *conn,
567 				  struct bt_conn_le_tx_power *tx_power_level);
568 
569 /** @brief Get local enhanced connection transmit power level.
570  *
571  *  @param conn           Connection object.
572  *  @param tx_power       Transmit power level descriptor.
573  *
574  *  @return Zero on success or (negative) error code on failure.
575  *  @retval -ENOBUFS HCI command buffer is not available.
576  */
577 int bt_conn_le_enhanced_get_tx_power_level(struct bt_conn *conn,
578 					   struct bt_conn_le_tx_power *tx_power);
579 
580 /** @brief Get remote (peer) transmit power level.
581  *
582  *  @param conn           Connection object.
583  *  @param phy            PHY information.
584  *
585  *  @return Zero on success or (negative) error code on failure.
586  *  @retval -ENOBUFS HCI command buffer is not available.
587  */
588 int bt_conn_le_get_remote_tx_power_level(struct bt_conn *conn,
589 					 enum bt_conn_le_tx_power_phy phy);
590 
591 /** @brief Enable transmit power reporting.
592  *
593  *  @param conn           Connection object.
594  *  @param local_enable   Enable/disable reporting for local.
595  *  @param remote_enable  Enable/disable reporting for remote.
596  *
597  *  @return Zero on success or (negative) error code on failure.
598  *  @retval -ENOBUFS HCI command buffer is not available.
599  */
600 int bt_conn_le_set_tx_power_report_enable(struct bt_conn *conn,
601 					  bool local_enable,
602 					  bool remote_enable);
603 
604 /** @brief Update the connection parameters.
605  *
606  *  If the local device is in the peripheral role then updating the connection
607  *  parameters will be delayed. This delay can be configured by through the
608  *  @kconfig{CONFIG_BT_CONN_PARAM_UPDATE_TIMEOUT} option.
609  *
610  *  @param conn Connection object.
611  *  @param param Updated connection parameters.
612  *
613  *  @return Zero on success or (negative) error code on failure.
614  */
615 int bt_conn_le_param_update(struct bt_conn *conn,
616 			    const struct bt_le_conn_param *param);
617 
618 /** @brief Update the connection transmit data length parameters.
619  *
620  *  @param conn  Connection object.
621  *  @param param Updated data length parameters.
622  *
623  *  @return Zero on success or (negative) error code on failure.
624  */
625 int bt_conn_le_data_len_update(struct bt_conn *conn,
626 			       const struct bt_conn_le_data_len_param *param);
627 
628 /** @brief Update the connection PHY parameters.
629  *
630  *  Update the preferred transmit and receive PHYs of the connection.
631  *  Use @ref BT_GAP_LE_PHY_NONE to indicate no preference.
632  *
633  *  @param conn Connection object.
634  *  @param param Updated connection parameters.
635  *
636  *  @return Zero on success or (negative) error code on failure.
637  */
638 int bt_conn_le_phy_update(struct bt_conn *conn,
639 			  const struct bt_conn_le_phy_param *param);
640 
641 /** @brief Disconnect from a remote device or cancel pending connection.
642  *
643  *  Disconnect an active connection with the specified reason code or cancel
644  *  pending outgoing connection.
645  *
646  *  The disconnect reason for a normal disconnect should be:
647  *  @ref BT_HCI_ERR_REMOTE_USER_TERM_CONN.
648  *
649  *  The following disconnect reasons are accepted:
650  *   - @ref BT_HCI_ERR_AUTH_FAIL
651  *   - @ref BT_HCI_ERR_REMOTE_USER_TERM_CONN
652  *   - @ref BT_HCI_ERR_REMOTE_LOW_RESOURCES
653  *   - @ref BT_HCI_ERR_REMOTE_POWER_OFF
654  *   - @ref BT_HCI_ERR_UNSUPP_REMOTE_FEATURE
655  *   - @ref BT_HCI_ERR_PAIRING_NOT_SUPPORTED
656  *   - @ref BT_HCI_ERR_UNACCEPT_CONN_PARAM
657  *
658  *  @param conn Connection to disconnect.
659  *  @param reason Reason code for the disconnection.
660  *
661  *  @return Zero on success or (negative) error code on failure.
662  */
663 int bt_conn_disconnect(struct bt_conn *conn, uint8_t reason);
664 
665 enum {
666 	/** Convenience value when no options are specified. */
667 	BT_CONN_LE_OPT_NONE = 0,
668 
669 	/** @brief Enable LE Coded PHY.
670 	 *
671 	 *  Enable scanning on the LE Coded PHY.
672 	 */
673 	BT_CONN_LE_OPT_CODED = BIT(0),
674 
675 	/** @brief Disable LE 1M PHY.
676 	 *
677 	 *  Disable scanning on the LE 1M PHY.
678 	 *
679 	 *  @note Requires @ref BT_CONN_LE_OPT_CODED.
680 	 */
681 	BT_CONN_LE_OPT_NO_1M = BIT(1),
682 };
683 
684 struct bt_conn_le_create_param {
685 
686 	/** Bit-field of create connection options. */
687 	uint32_t options;
688 
689 	/** Scan interval (N * 0.625 ms) */
690 	uint16_t interval;
691 
692 	/** Scan window (N * 0.625 ms) */
693 	uint16_t window;
694 
695 	/** @brief Scan interval LE Coded PHY (N * 0.625 MS)
696 	 *
697 	 *  Set zero to use same as LE 1M PHY scan interval
698 	 */
699 	uint16_t interval_coded;
700 
701 	/** @brief Scan window LE Coded PHY (N * 0.625 MS)
702 	 *
703 	 *  Set zero to use same as LE 1M PHY scan window.
704 	 */
705 	uint16_t window_coded;
706 
707 	/** @brief Connection initiation timeout (N * 10 MS)
708 	 *
709 	 *  Set zero to use the default @kconfig{CONFIG_BT_CREATE_CONN_TIMEOUT}
710 	 *  timeout.
711 	 *
712 	 *  @note Unused in @ref bt_conn_le_create_auto
713 	 */
714 	uint16_t timeout;
715 };
716 
717 /** @brief Initialize create connection parameters
718  *
719  *  @param _options  Create connection options.
720  *  @param _interval Create connection scan interval (N * 0.625 ms).
721  *  @param _window   Create connection scan window (N * 0.625 ms).
722  */
723 #define BT_CONN_LE_CREATE_PARAM_INIT(_options, _interval, _window) \
724 { \
725 	.options = (_options), \
726 	.interval = (_interval), \
727 	.window = (_window), \
728 	.interval_coded = 0, \
729 	.window_coded = 0, \
730 	.timeout = 0, \
731 }
732 
733 /** Helper to declare create connection parameters inline
734  *
735  *  @param _options  Create connection options.
736  *  @param _interval Create connection scan interval (N * 0.625 ms).
737  *  @param _window   Create connection scan window (N * 0.625 ms).
738  */
739 #define BT_CONN_LE_CREATE_PARAM(_options, _interval, _window) \
740 	((struct bt_conn_le_create_param[]) { \
741 		BT_CONN_LE_CREATE_PARAM_INIT(_options, _interval, _window) \
742 	 })
743 
744 /** Default LE create connection parameters.
745  *  Scan continuously by setting scan interval equal to scan window.
746  */
747 #define BT_CONN_LE_CREATE_CONN \
748 	BT_CONN_LE_CREATE_PARAM(BT_CONN_LE_OPT_NONE, \
749 				BT_GAP_SCAN_FAST_INTERVAL, \
750 				BT_GAP_SCAN_FAST_INTERVAL)
751 
752 /** Default LE create connection using filter accept list parameters.
753  *  Scan window:   30 ms.
754  *  Scan interval: 60 ms.
755  */
756 #define BT_CONN_LE_CREATE_CONN_AUTO \
757 	BT_CONN_LE_CREATE_PARAM(BT_CONN_LE_OPT_NONE, \
758 				BT_GAP_SCAN_FAST_INTERVAL, \
759 				BT_GAP_SCAN_FAST_WINDOW)
760 
761 /** @brief Initiate an LE connection to a remote device.
762  *
763  *  Allows initiate new LE link to remote peer using its address.
764  *
765  *  The caller gets a new reference to the connection object which must be
766  *  released with bt_conn_unref() once done using the object.
767  *
768  *  This uses the General Connection Establishment procedure.
769  *
770  *  The application must disable explicit scanning before initiating
771  *  a new LE connection.
772  *
773  *  @param[in]  peer         Remote address.
774  *  @param[in]  create_param Create connection parameters.
775  *  @param[in]  conn_param   Initial connection parameters.
776  *  @param[out] conn         Valid connection object on success.
777  *
778  *  @return Zero on success or (negative) error code on failure.
779  */
780 int bt_conn_le_create(const bt_addr_le_t *peer,
781 		      const struct bt_conn_le_create_param *create_param,
782 		      const struct bt_le_conn_param *conn_param,
783 		      struct bt_conn **conn);
784 
785 struct bt_conn_le_create_synced_param {
786 
787 	/** @brief Remote address
788 	 *
789 	 * The peer must be synchronized to the PAwR train.
790 	 *
791 	 */
792 	const bt_addr_le_t *peer;
793 
794 	/** The subevent where the connection will be initiated. */
795 	uint8_t subevent;
796 };
797 
798 /** @brief Create a connection to a synced device
799  *
800  *  Initiate a connection to a synced device from a Periodic Advertising
801  *  with Responses (PAwR) train.
802  *
803  *  The caller gets a new reference to the connection object which must be
804  *  released with bt_conn_unref() once done using the object.
805  *
806  *  This uses the Periodic Advertising Connection Procedure.
807  *
808  *  @param[in]  adv          The adverting set the PAwR advertiser belongs to.
809  *  @param[in]  synced_param Create connection parameters.
810  *  @param[in]  conn_param   Initial connection parameters.
811  *  @param[out] conn         Valid connection object on success.
812  *
813  *  @return Zero on success or (negative) error code on failure.
814  */
815 int bt_conn_le_create_synced(const struct bt_le_ext_adv *adv,
816 			     const struct bt_conn_le_create_synced_param *synced_param,
817 			     const struct bt_le_conn_param *conn_param, struct bt_conn **conn);
818 
819 /** @brief Automatically connect to remote devices in the filter accept list.
820  *
821  *  This uses the Auto Connection Establishment procedure.
822  *  The procedure will continue until a single connection is established or the
823  *  procedure is stopped through @ref bt_conn_create_auto_stop.
824  *  To establish connections to all devices in the the filter accept list the
825  *  procedure should be started again in the connected callback after a
826  *  new connection has been established.
827  *
828  *  @param create_param Create connection parameters
829  *  @param conn_param   Initial connection parameters.
830  *
831  *  @return Zero on success or (negative) error code on failure.
832  *  @return -ENOMEM No free connection object available.
833  */
834 int bt_conn_le_create_auto(const struct bt_conn_le_create_param *create_param,
835 			   const struct bt_le_conn_param *conn_param);
836 
837 /** @brief Stop automatic connect creation.
838  *
839  *  @return Zero on success or (negative) error code on failure.
840  */
841 int bt_conn_create_auto_stop(void);
842 
843 /** @brief Automatically connect to remote device if it's in range.
844  *
845  *  This function enables/disables automatic connection initiation.
846  *  Every time the device loses the connection with peer, this connection
847  *  will be re-established if connectable advertisement from peer is received.
848  *
849  *  @note Auto connect is disabled during explicit scanning.
850  *
851  *  @param addr Remote Bluetooth address.
852  *  @param param If non-NULL, auto connect is enabled with the given
853  *  parameters. If NULL, auto connect is disabled.
854  *
855  *  @return Zero on success or error code otherwise.
856  */
857 int bt_le_set_auto_conn(const bt_addr_le_t *addr,
858 			const struct bt_le_conn_param *param);
859 
860 /** @brief Set security level for a connection.
861  *
862  *  This function enable security (encryption) for a connection. If the device
863  *  has bond information for the peer with sufficiently strong key encryption
864  *  will be enabled. If the connection is already encrypted with sufficiently
865  *  strong key this function does nothing.
866  *
867  *  If the device has no bond information for the peer and is not already paired
868  *  then the pairing procedure will be initiated. Note that @p sec has no effect
869  *  on the security level selected for the pairing process. The selection is
870  *  instead controlled by the values of the registered @ref bt_conn_auth_cb. If
871  *  the device has bond information or is already paired and the keys are too
872  *  weak then the pairing procedure will be initiated.
873  *
874  *  This function may return an error if the required level of security defined using
875  *  @p sec is not possible to achieve due to local or remote device limitation
876  *  (e.g., input output capabilities), or if the maximum number of paired devices
877  *  has been reached.
878  *
879  *  This function may return an error if the pairing procedure has already been
880  *  initiated by the local device or the peer device.
881  *
882  *  @note When @kconfig{CONFIG_BT_SMP_SC_ONLY} is enabled then the security
883  *        level will always be level 4.
884  *
885  *  @note When @kconfig{CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY} is enabled then the
886  *        security level will always be level 3.
887  *
888  *  @note When @ref BT_SECURITY_FORCE_PAIR within @p sec is enabled then the pairing
889  *        procedure will always be initiated.
890  *
891  *  @param conn Connection object.
892  *  @param sec Requested minimum security level.
893  *
894  *  @return 0 on success or negative error
895  */
896 int bt_conn_set_security(struct bt_conn *conn, bt_security_t sec);
897 
898 /** @brief Get security level for a connection.
899  *
900  *  @return Connection security level
901  */
902 bt_security_t bt_conn_get_security(const struct bt_conn *conn);
903 
904 /** @brief Get encryption key size.
905  *
906  *  This function gets encryption key size.
907  *  If there is no security (encryption) enabled 0 will be returned.
908  *
909  *  @param conn Existing connection object.
910  *
911  *  @return Encryption key size.
912  */
913 uint8_t bt_conn_enc_key_size(const struct bt_conn *conn);
914 
915 enum bt_security_err {
916 	/** Security procedure successful. */
917 	BT_SECURITY_ERR_SUCCESS,
918 
919 	/** Authentication failed. */
920 	BT_SECURITY_ERR_AUTH_FAIL,
921 
922 	/** PIN or encryption key is missing. */
923 	BT_SECURITY_ERR_PIN_OR_KEY_MISSING,
924 
925 	/** OOB data is not available.  */
926 	BT_SECURITY_ERR_OOB_NOT_AVAILABLE,
927 
928 	/** The requested security level could not be reached. */
929 	BT_SECURITY_ERR_AUTH_REQUIREMENT,
930 
931 	/** Pairing is not supported */
932 	BT_SECURITY_ERR_PAIR_NOT_SUPPORTED,
933 
934 	/** Pairing is not allowed. */
935 	BT_SECURITY_ERR_PAIR_NOT_ALLOWED,
936 
937 	/** Invalid parameters. */
938 	BT_SECURITY_ERR_INVALID_PARAM,
939 
940 	/** Distributed Key Rejected */
941 	BT_SECURITY_ERR_KEY_REJECTED,
942 
943 	/** Pairing failed but the exact reason could not be specified. */
944 	BT_SECURITY_ERR_UNSPECIFIED,
945 };
946 
947 /** @brief Connection callback structure.
948  *
949  *  This structure is used for tracking the state of a connection.
950  *  It is registered with the help of the bt_conn_cb_register() API.
951  *  It's permissible to register multiple instances of this @ref bt_conn_cb
952  *  type, in case different modules of an application are interested in
953  *  tracking the connection state. If a callback is not of interest for
954  *  an instance, it may be set to NULL and will as a consequence not be
955  *  used for that instance.
956  */
957 struct bt_conn_cb {
958 	/** @brief A new connection has been established.
959 	 *
960 	 *  This callback notifies the application of a new connection.
961 	 *  In case the err parameter is non-zero it means that the
962 	 *  connection establishment failed.
963 	 *
964 	 *  @note If the connection was established from an advertising set then
965 	 *        the advertising set cannot be restarted directly from this
966 	 *        callback. Instead use the connected callback of the
967 	 *        advertising set.
968 	 *
969 	 *  @param conn New connection object.
970 	 *  @param err HCI error. Zero for success, non-zero otherwise.
971 	 *
972 	 *  @p err can mean either of the following:
973 	 *  - @ref BT_HCI_ERR_UNKNOWN_CONN_ID Creating the connection started by
974 	 *    @ref bt_conn_le_create was canceled either by the user through
975 	 *    @ref bt_conn_disconnect or by the timeout in the host through
976 	 *    @ref bt_conn_le_create_param timeout parameter, which defaults to
977 	 *    @kconfig{CONFIG_BT_CREATE_CONN_TIMEOUT} seconds.
978 	 *  - @p BT_HCI_ERR_ADV_TIMEOUT High duty cycle directed connectable
979 	 *    advertiser started by @ref bt_le_adv_start failed to be connected
980 	 *    within the timeout.
981 	 */
982 	void (*connected)(struct bt_conn *conn, uint8_t err);
983 
984 	/** @brief A connection has been disconnected.
985 	 *
986 	 *  This callback notifies the application that a connection
987 	 *  has been disconnected.
988 	 *
989 	 *  When this callback is called the stack still has one reference to
990 	 *  the connection object. If the application in this callback tries to
991 	 *  start either a connectable advertiser or create a new connection
992 	 *  this might fail because there are no free connection objects
993 	 *  available.
994 	 *  To avoid this issue it is recommended to either start connectable
995 	 *  advertise or create a new connection using @ref k_work_submit or
996 	 *  increase @kconfig{CONFIG_BT_MAX_CONN}.
997 	 *
998 	 *  @param conn Connection object.
999 	 *  @param reason BT_HCI_ERR_* reason for the disconnection.
1000 	 */
1001 	void (*disconnected)(struct bt_conn *conn, uint8_t reason);
1002 
1003 	/** @brief A connection object has been returned to the pool.
1004 	 *
1005 	 * This callback notifies the application that it might be able to
1006 	 * allocate a connection object. No guarantee, first come, first serve.
1007 	 *
1008 	 * Use this to e.g. re-start connectable advertising or scanning.
1009 	 *
1010 	 * Treat this callback as an ISR, as it originates from
1011 	 * @ref bt_conn_unref which is used by the BT stack. Making
1012 	 * Bluetooth API calls in this context is error-prone and strongly
1013 	 * discouraged.
1014 	 */
1015 	void (*recycled)(void);
1016 
1017 	/** @brief LE connection parameter update request.
1018 	 *
1019 	 *  This callback notifies the application that a remote device
1020 	 *  is requesting to update the connection parameters. The
1021 	 *  application accepts the parameters by returning true, or
1022 	 *  rejects them by returning false. Before accepting, the
1023 	 *  application may also adjust the parameters to better suit
1024 	 *  its needs.
1025 	 *
1026 	 *  It is recommended for an application to have just one of these
1027 	 *  callbacks for simplicity. However, if an application registers
1028 	 *  multiple it needs to manage the potentially different
1029 	 *  requirements for each callback. Each callback gets the
1030 	 *  parameters as returned by previous callbacks, i.e. they are not
1031 	 *  necessarily the same ones as the remote originally sent.
1032 	 *
1033 	 *  If the application does not have this callback then the default
1034 	 *  is to accept the parameters.
1035 	 *
1036 	 *  @param conn Connection object.
1037 	 *  @param param Proposed connection parameters.
1038 	 *
1039 	 *  @return true to accept the parameters, or false to reject them.
1040 	 */
1041 	bool (*le_param_req)(struct bt_conn *conn,
1042 			     struct bt_le_conn_param *param);
1043 
1044 	/** @brief The parameters for an LE connection have been updated.
1045 	 *
1046 	 *  This callback notifies the application that the connection
1047 	 *  parameters for an LE connection have been updated.
1048 	 *
1049 	 *  @param conn Connection object.
1050 	 *  @param interval Connection interval.
1051 	 *  @param latency Connection latency.
1052 	 *  @param timeout Connection supervision timeout.
1053 	 */
1054 	void (*le_param_updated)(struct bt_conn *conn, uint16_t interval,
1055 				 uint16_t latency, uint16_t timeout);
1056 #if defined(CONFIG_BT_SMP)
1057 	/** @brief Remote Identity Address has been resolved.
1058 	 *
1059 	 *  This callback notifies the application that a remote
1060 	 *  Identity Address has been resolved
1061 	 *
1062 	 *  @param conn Connection object.
1063 	 *  @param rpa Resolvable Private Address.
1064 	 *  @param identity Identity Address.
1065 	 */
1066 	void (*identity_resolved)(struct bt_conn *conn,
1067 				  const bt_addr_le_t *rpa,
1068 				  const bt_addr_le_t *identity);
1069 #endif /* CONFIG_BT_SMP */
1070 #if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_BREDR)
1071 	/** @brief The security level of a connection has changed.
1072 	 *
1073 	 *  This callback notifies the application that the security of a
1074 	 *  connection has changed.
1075 	 *
1076 	 *  The security level of the connection can either have been increased
1077 	 *  or remain unchanged. An increased security level means that the
1078 	 *  pairing procedure has been performed or the bond information from
1079 	 *  a previous connection has been applied. If the security level
1080 	 *  remains unchanged this means that the encryption key has been
1081 	 *  refreshed for the connection.
1082 	 *
1083 	 *  @param conn Connection object.
1084 	 *  @param level New security level of the connection.
1085 	 *  @param err Security error. Zero for success, non-zero otherwise.
1086 	 */
1087 	void (*security_changed)(struct bt_conn *conn, bt_security_t level,
1088 				 enum bt_security_err err);
1089 #endif /* defined(CONFIG_BT_SMP) || defined(CONFIG_BT_BREDR) */
1090 
1091 #if defined(CONFIG_BT_REMOTE_INFO)
1092 	/** @brief Remote information procedures has completed.
1093 	 *
1094 	 *  This callback notifies the application that the remote information
1095 	 *  has been retrieved from the remote peer.
1096 	 *
1097 	 *  @param conn Connection object.
1098 	 *  @param remote_info Connection information of remote device.
1099 	 */
1100 	void (*remote_info_available)(struct bt_conn *conn,
1101 				      struct bt_conn_remote_info *remote_info);
1102 #endif /* defined(CONFIG_BT_REMOTE_INFO) */
1103 
1104 #if defined(CONFIG_BT_USER_PHY_UPDATE)
1105 	/** @brief The PHY of the connection has changed.
1106 	 *
1107 	 *  This callback notifies the application that the PHY of the
1108 	 *  connection has changed.
1109 	 *
1110 	 *  @param conn Connection object.
1111 	 *  @param info Connection LE PHY information.
1112 	 */
1113 	void (*le_phy_updated)(struct bt_conn *conn,
1114 			       struct bt_conn_le_phy_info *param);
1115 #endif /* defined(CONFIG_BT_USER_PHY_UPDATE) */
1116 
1117 #if defined(CONFIG_BT_USER_DATA_LEN_UPDATE)
1118 	/** @brief The data length parameters of the connection has changed.
1119 	 *
1120 	 *  This callback notifies the application that the maximum Link Layer
1121 	 *  payload length or transmission time has changed.
1122 	 *
1123 	 *  @param conn Connection object.
1124 	 *  @param info Connection data length information.
1125 	 */
1126 	void (*le_data_len_updated)(struct bt_conn *conn,
1127 				    struct bt_conn_le_data_len_info *info);
1128 #endif /* defined(CONFIG_BT_USER_DATA_LEN_UPDATE) */
1129 
1130 #if defined(CONFIG_BT_DF_CONNECTION_CTE_RX)
1131 	/** @brief Callback for IQ samples report collected when sampling
1132 	 *        CTE received by data channel PDU.
1133 	 *
1134 	 * @param conn      The connection object.
1135 	 * @param iq_report Report data for collected IQ samples.
1136 	 */
1137 	void (*cte_report_cb)(struct bt_conn *conn,
1138 			      const struct bt_df_conn_iq_samples_report *iq_report);
1139 #endif /* CONFIG_BT_DF_CONNECTION_CTE_RX */
1140 
1141 #if defined(CONFIG_BT_TRANSMIT_POWER_CONTROL)
1142 	/** @brief LE Read Remote Transmit Power Level procedure has completed or LE
1143 	 *  Transmit Power Reporting event.
1144 	 *
1145 	 *  This callback notifies the application that either the remote transmit power level
1146 	 *  has been read from the peer or transmit power level has changed for the local or
1147 	 *  remote controller when transmit power reporting is enabled for the respective side
1148 	 *  using @ref bt_conn_le_set_tx_power_report_enable.
1149 	 *
1150 	 *  @param conn Connection object.
1151 	 *  @param report Transmit power report.
1152 	 */
1153 	void (*tx_power_report)(struct bt_conn *conn,
1154 				const struct bt_conn_le_tx_power_report *report);
1155 #endif /* CONFIG_BT_TRANSMIT_POWER_CONTROL */
1156 
1157 	struct bt_conn_cb *_next;
1158 };
1159 
1160 /** @brief Register connection callbacks.
1161  *
1162  *  Register callbacks to monitor the state of connections.
1163  *
1164  *  @param cb Callback struct. Must point to memory that remains valid.
1165  */
1166 void bt_conn_cb_register(struct bt_conn_cb *cb);
1167 
1168 /**
1169  * @brief Unregister connection callbacks.
1170  *
1171  * Unregister the state of connections callbacks.
1172  *
1173  * @param cb Callback struct point to memory that remains valid.
1174  *
1175  * @retval 0 Success
1176  * @retval -EINVAL If @p cb is NULL
1177  * @retval -ENOENT if @p cb was not registered
1178  */
1179 int bt_conn_cb_unregister(struct bt_conn_cb *cb);
1180 
1181 /**
1182  *  @brief Register a callback structure for connection events.
1183  *
1184  *  @param _name Name of callback structure.
1185  */
1186 #define BT_CONN_CB_DEFINE(_name)					\
1187 	static const STRUCT_SECTION_ITERABLE(bt_conn_cb,		\
1188 						_CONCAT(bt_conn_cb_,	\
1189 							_name))
1190 
1191 /** @brief Enable/disable bonding.
1192  *
1193  *  Set/clear the Bonding flag in the Authentication Requirements of
1194  *  SMP Pairing Request/Response data.
1195  *  The initial value of this flag depends on BT_BONDABLE Kconfig setting.
1196  *  For the vast majority of applications calling this function shouldn't be
1197  *  needed.
1198  *
1199  *  @param enable Value allowing/disallowing to be bondable.
1200  */
1201 void bt_set_bondable(bool enable);
1202 
1203 /** @brief Set/clear the bonding flag for a given connection.
1204  *
1205  *  Set/clear the Bonding flag in the Authentication Requirements of
1206  *  SMP Pairing Request/Response data for a given connection.
1207  *
1208  *  The bonding flag for a given connection cannot be set/cleared if
1209  *  security procedures in the SMP module have already started.
1210  *  This function can be called only once per connection.
1211  *
1212  *  If the bonding flag is not set/cleared for a given connection,
1213  *  the value will depend on global configuration which is set using
1214  *  bt_set_bondable.
1215  *  The default value of the global configuration is defined using
1216  *  CONFIG_BT_BONDABLE Kconfig option.
1217  *
1218  *  @param conn Connection object.
1219  *  @param enable Value allowing/disallowing to be bondable.
1220  */
1221 int bt_conn_set_bondable(struct bt_conn *conn, bool enable);
1222 
1223 /** @brief Allow/disallow remote LE SC OOB data to be used for pairing.
1224  *
1225  *  Set/clear the OOB data flag for LE SC SMP Pairing Request/Response data.
1226  *
1227  *  @param enable Value allowing/disallowing remote LE SC OOB data.
1228  */
1229 void bt_le_oob_set_sc_flag(bool enable);
1230 
1231 /** @brief Allow/disallow remote legacy OOB data to be used for pairing.
1232  *
1233  *  Set/clear the OOB data flag for legacy SMP Pairing Request/Response data.
1234  *
1235  *  @param enable Value allowing/disallowing remote legacy OOB data.
1236  */
1237 void bt_le_oob_set_legacy_flag(bool enable);
1238 
1239 /** @brief Set OOB Temporary Key to be used for pairing
1240  *
1241  *  This function allows to set OOB data for the LE legacy pairing procedure.
1242  *  The function should only be called in response to the oob_data_request()
1243  *  callback provided that the legacy method is user pairing.
1244  *
1245  *  @param conn Connection object
1246  *  @param tk Pointer to 16 byte long TK array
1247  *
1248  *  @return Zero on success or -EINVAL if NULL
1249  */
1250 int bt_le_oob_set_legacy_tk(struct bt_conn *conn, const uint8_t *tk);
1251 
1252 /** @brief Set OOB data during LE Secure Connections (SC) pairing procedure
1253  *
1254  *  This function allows to set OOB data during the LE SC pairing procedure.
1255  *  The function should only be called in response to the oob_data_request()
1256  *  callback provided that LE SC method is used for pairing.
1257  *
1258  *  The user should submit OOB data according to the information received in the
1259  *  callback. This may yield three different configurations: with only local OOB
1260  *  data present, with only remote OOB data present or with both local and
1261  *  remote OOB data present.
1262  *
1263  *  @param conn Connection object
1264  *  @param oobd_local Local OOB data or NULL if not present
1265  *  @param oobd_remote Remote OOB data or NULL if not present
1266  *
1267  *  @return Zero on success or error code otherwise, positive in case of
1268  *          protocol error or negative (POSIX) in case of stack internal error.
1269  */
1270 int bt_le_oob_set_sc_data(struct bt_conn *conn,
1271 			  const struct bt_le_oob_sc_data *oobd_local,
1272 			  const struct bt_le_oob_sc_data *oobd_remote);
1273 
1274 /** @brief Get OOB data used for LE Secure Connections (SC) pairing procedure
1275  *
1276  *  This function allows to get OOB data during the LE SC pairing procedure that
1277  *  were set by the bt_le_oob_set_sc_data() API.
1278  *
1279  *  @note The OOB data will only be available as long as the connection object
1280  *  associated with it is valid.
1281  *
1282  *  @param conn Connection object
1283  *  @param oobd_local Local OOB data or NULL if not set
1284  *  @param oobd_remote Remote OOB data or NULL if not set
1285  *
1286  *  @return Zero on success or error code otherwise, positive in case of
1287  *          protocol error or negative (POSIX) in case of stack internal error.
1288  */
1289 int bt_le_oob_get_sc_data(struct bt_conn *conn,
1290 			  const struct bt_le_oob_sc_data **oobd_local,
1291 			  const struct bt_le_oob_sc_data **oobd_remote);
1292 
1293 /**
1294  *  Special passkey value that can be used to disable a previously
1295  *  set fixed passkey.
1296  */
1297 #define BT_PASSKEY_INVALID 0xffffffff
1298 
1299 /** @brief Set a fixed passkey to be used for pairing.
1300  *
1301  *  This API is only available when the CONFIG_BT_FIXED_PASSKEY
1302  *  configuration option has been enabled.
1303  *
1304  *  Sets a fixed passkey to be used for pairing. If set, the
1305  *  pairing_confirm() callback will be called for all incoming pairings.
1306  *
1307  *  @param passkey A valid passkey (0 - 999999) or BT_PASSKEY_INVALID
1308  *                 to disable a previously set fixed passkey.
1309  *
1310  *  @return 0 on success or a negative error code on failure.
1311  */
1312 int bt_passkey_set(unsigned int passkey);
1313 
1314 /** Info Structure for OOB pairing */
1315 struct bt_conn_oob_info {
1316 	/** Type of OOB pairing method */
1317 	enum {
1318 		/** LE legacy pairing */
1319 		BT_CONN_OOB_LE_LEGACY,
1320 
1321 		/** LE SC pairing */
1322 		BT_CONN_OOB_LE_SC,
1323 	} type;
1324 
1325 	union {
1326 		/** LE Secure Connections OOB pairing parameters */
1327 		struct {
1328 			/** OOB data configuration */
1329 			enum {
1330 				/** Local OOB data requested */
1331 				BT_CONN_OOB_LOCAL_ONLY,
1332 
1333 				/** Remote OOB data requested */
1334 				BT_CONN_OOB_REMOTE_ONLY,
1335 
1336 				/** Both local and remote OOB data requested */
1337 				BT_CONN_OOB_BOTH_PEERS,
1338 
1339 				/** No OOB data requested */
1340 				BT_CONN_OOB_NO_DATA,
1341 			} oob_config;
1342 		} lesc;
1343 	};
1344 };
1345 
1346 #if defined(CONFIG_BT_SMP_APP_PAIRING_ACCEPT)
1347 /** @brief Pairing request and pairing response info structure.
1348  *
1349  *  This structure is the same for both smp_pairing_req and smp_pairing_rsp
1350  *  and a subset of the packet data, except for the initial Code octet.
1351  *  It is documented in Core Spec. Vol. 3, Part H, 3.5.1 and 3.5.2.
1352  */
1353 struct bt_conn_pairing_feat {
1354 	/** IO Capability, Core Spec. Vol 3, Part H, 3.5.1, Table 3.4 */
1355 	uint8_t io_capability;
1356 
1357 	/** OOB data flag, Core Spec. Vol 3, Part H, 3.5.1, Table 3.5 */
1358 	uint8_t oob_data_flag;
1359 
1360 	/** AuthReq, Core Spec. Vol 3, Part H, 3.5.1, Fig. 3.3 */
1361 	uint8_t auth_req;
1362 
1363 	/** Maximum Encryption Key Size, Core Spec. Vol 3, Part H, 3.5.1 */
1364 	uint8_t max_enc_key_size;
1365 
1366 	/** Initiator Key Distribution/Generation, Core Spec. Vol 3, Part H,
1367 	 *  3.6.1, Fig. 3.11
1368 	 */
1369 	uint8_t init_key_dist;
1370 
1371 	/** Responder Key Distribution/Generation, Core Spec. Vol 3, Part H
1372 	 *  3.6.1, Fig. 3.11
1373 	 */
1374 	uint8_t resp_key_dist;
1375 };
1376 #endif /* CONFIG_BT_SMP_APP_PAIRING_ACCEPT */
1377 
1378 /** Authenticated pairing callback structure */
1379 struct bt_conn_auth_cb {
1380 #if defined(CONFIG_BT_SMP_APP_PAIRING_ACCEPT)
1381 	/** @brief Query to proceed incoming pairing or not.
1382 	 *
1383 	 *  On any incoming pairing req/rsp this callback will be called for
1384 	 *  the application to decide whether to allow for the pairing to
1385 	 *  continue.
1386 	 *
1387 	 *  The pairing info received from the peer is passed to assist
1388 	 *  making the decision.
1389 	 *
1390 	 *  As this callback is synchronous the application should return
1391 	 *  a response value immediately. Otherwise it may affect the
1392 	 *  timing during pairing. Hence, this information should not be
1393 	 *  conveyed to the user to take action.
1394 	 *
1395 	 *  The remaining callbacks are not affected by this, but do notice
1396 	 *  that other callbacks can be called during the pairing. Eg. if
1397 	 *  pairing_confirm is registered both will be called for Just-Works
1398 	 *  pairings.
1399 	 *
1400 	 *  This callback may be unregistered in which case pairing continues
1401 	 *  as if the Kconfig flag was not set.
1402 	 *
1403 	 *  This callback is not called for BR/EDR Secure Simple Pairing (SSP).
1404 	 *
1405 	 *  @param conn Connection where pairing is initiated.
1406 	 *  @param feat Pairing req/resp info.
1407 	 */
1408 	enum bt_security_err (*pairing_accept)(struct bt_conn *conn,
1409 			      const struct bt_conn_pairing_feat *const feat);
1410 #endif /* CONFIG_BT_SMP_APP_PAIRING_ACCEPT */
1411 
1412 	/** @brief Display a passkey to the user.
1413 	 *
1414 	 *  When called the application is expected to display the given
1415 	 *  passkey to the user, with the expectation that the passkey will
1416 	 *  then be entered on the peer device. The passkey will be in the
1417 	 *  range of 0 - 999999, and is expected to be padded with zeroes so
1418 	 *  that six digits are always shown. E.g. the value 37 should be
1419 	 *  shown as 000037.
1420 	 *
1421 	 *  This callback may be set to NULL, which means that the local
1422 	 *  device lacks the ability do display a passkey. If set
1423 	 *  to non-NULL the cancel callback must also be provided, since
1424 	 *  this is the only way the application can find out that it should
1425 	 *  stop displaying the passkey.
1426 	 *
1427 	 *  @param conn Connection where pairing is currently active.
1428 	 *  @param passkey Passkey to show to the user.
1429 	 */
1430 	void (*passkey_display)(struct bt_conn *conn, unsigned int passkey);
1431 
1432 #if defined(CONFIG_BT_PASSKEY_KEYPRESS)
1433 	/** @brief Receive Passkey Keypress Notification during pairing
1434 	 *
1435 	 *  This allows the remote device to use the local device to give users
1436 	 *  feedback on the progress of entering the passkey over there. This is
1437 	 *  useful when the remote device itself has no display suitable for
1438 	 *  showing the progress.
1439 	 *
1440 	 *  The handler of this callback is expected to keep track of the number
1441 	 *  of digits entered and show a password-field-like feedback to the
1442 	 *  user.
1443 	 *
1444 	 *  This callback is only relevant while the local side does Passkey
1445 	 *  Display.
1446 	 *
1447 	 *  The event type is verified to be in range of the enum. No other
1448 	 *  sanitization has been done. The remote could send a large number of
1449 	 *  events of any type in any order.
1450 	 *
1451 	 *  @param conn The related connection.
1452 	 *  @param type Type of event. Verified in range of the enum.
1453 	 */
1454 	void (*passkey_display_keypress)(struct bt_conn *conn,
1455 					 enum bt_conn_auth_keypress type);
1456 #endif
1457 
1458 	/** @brief Request the user to enter a passkey.
1459 	 *
1460 	 *  When called the user is expected to enter a passkey. The passkey
1461 	 *  must be in the range of 0 - 999999, and should be expected to
1462 	 *  be zero-padded, as that's how the peer device will typically be
1463 	 *  showing it (e.g. 37 would be shown as 000037).
1464 	 *
1465 	 *  Once the user has entered the passkey its value should be given
1466 	 *  to the stack using the bt_conn_auth_passkey_entry() API.
1467 	 *
1468 	 *  This callback may be set to NULL, which means that the local
1469 	 *  device lacks the ability to enter a passkey. If set to non-NULL
1470 	 *  the cancel callback must also be provided, since this is the
1471 	 *  only way the application can find out that it should stop
1472 	 *  requesting the user to enter a passkey.
1473 	 *
1474 	 *  @param conn Connection where pairing is currently active.
1475 	 */
1476 	void (*passkey_entry)(struct bt_conn *conn);
1477 
1478 	/** @brief Request the user to confirm a passkey.
1479 	 *
1480 	 *  When called the user is expected to confirm that the given
1481 	 *  passkey is also shown on the peer device.. The passkey will
1482 	 *  be in the range of 0 - 999999, and should be zero-padded to
1483 	 *  always be six digits (e.g. 37 would be shown as 000037).
1484 	 *
1485 	 *  Once the user has confirmed the passkey to match, the
1486 	 *  bt_conn_auth_passkey_confirm() API should be called. If the
1487 	 *  user concluded that the passkey doesn't match the
1488 	 *  bt_conn_auth_cancel() API should be called.
1489 	 *
1490 	 *  This callback may be set to NULL, which means that the local
1491 	 *  device lacks the ability to confirm a passkey. If set to non-NULL
1492 	 *  the cancel callback must also be provided, since this is the
1493 	 *  only way the application can find out that it should stop
1494 	 *  requesting the user to confirm a passkey.
1495 	 *
1496 	 *  @param conn Connection where pairing is currently active.
1497 	 *  @param passkey Passkey to be confirmed.
1498 	 */
1499 	void (*passkey_confirm)(struct bt_conn *conn, unsigned int passkey);
1500 
1501 	/** @brief Request the user to provide Out of Band (OOB) data.
1502 	 *
1503 	 *  When called the user is expected to provide OOB data. The required
1504 	 *  data are indicated by the information structure.
1505 	 *
1506 	 *  For LE Secure Connections OOB pairing, the user should provide
1507 	 *  local OOB data, remote OOB data or both depending on their
1508 	 *  availability. Their value should be given to the stack using the
1509 	 *  bt_le_oob_set_sc_data() API.
1510 	 *
1511 	 *  This callback must be set to non-NULL in order to support OOB
1512 	 *  pairing.
1513 	 *
1514 	 *  @param conn Connection where pairing is currently active.
1515 	 *  @param info OOB pairing information.
1516 	 */
1517 	void (*oob_data_request)(struct bt_conn *conn,
1518 				 struct bt_conn_oob_info *info);
1519 
1520 	/** @brief Cancel the ongoing user request.
1521 	 *
1522 	 *  This callback will be called to notify the application that it
1523 	 *  should cancel any previous user request (passkey display, entry
1524 	 *  or confirmation).
1525 	 *
1526 	 *  This may be set to NULL, but must always be provided whenever the
1527 	 *  passkey_display, passkey_entry passkey_confirm or pairing_confirm
1528 	 *  callback has been provided.
1529 	 *
1530 	 *  @param conn Connection where pairing is currently active.
1531 	 */
1532 	void (*cancel)(struct bt_conn *conn);
1533 
1534 	/** @brief Request confirmation for an incoming pairing.
1535 	 *
1536 	 *  This callback will be called to confirm an incoming pairing
1537 	 *  request where none of the other user callbacks is applicable.
1538 	 *
1539 	 *  If the user decides to accept the pairing the
1540 	 *  bt_conn_auth_pairing_confirm() API should be called. If the
1541 	 *  user decides to reject the pairing the bt_conn_auth_cancel() API
1542 	 *  should be called.
1543 	 *
1544 	 *  This callback may be set to NULL, which means that the local
1545 	 *  device lacks the ability to confirm a pairing request. If set
1546 	 *  to non-NULL the cancel callback must also be provided, since
1547 	 *  this is the only way the application can find out that it should
1548 	 *  stop requesting the user to confirm a pairing request.
1549 	 *
1550 	 *  @param conn Connection where pairing is currently active.
1551 	 */
1552 	void (*pairing_confirm)(struct bt_conn *conn);
1553 
1554 #if defined(CONFIG_BT_BREDR)
1555 	/** @brief Request the user to enter a passkey.
1556 	 *
1557 	 *  This callback will be called for a BR/EDR (Bluetooth Classic)
1558 	 *  connection where pairing is being performed. Once called the
1559 	 *  user is expected to enter a PIN code with a length between
1560 	 *  1 and 16 digits. If the @a highsec parameter is set to true
1561 	 *  the PIN code must be 16 digits long.
1562 	 *
1563 	 *  Once entered, the PIN code should be given to the stack using
1564 	 *  the bt_conn_auth_pincode_entry() API.
1565 	 *
1566 	 *  This callback may be set to NULL, however in that case pairing
1567 	 *  over BR/EDR will not be possible. If provided, the cancel
1568 	 *  callback must be provided as well.
1569 	 *
1570 	 *  @param conn Connection where pairing is currently active.
1571 	 *  @param highsec true if 16 digit PIN is required.
1572 	 */
1573 	void (*pincode_entry)(struct bt_conn *conn, bool highsec);
1574 #endif
1575 };
1576 
1577 /** Authenticated pairing information callback structure */
1578 struct bt_conn_auth_info_cb {
1579 	/** @brief notify that pairing procedure was complete.
1580 	 *
1581 	 *  This callback notifies the application that the pairing procedure
1582 	 *  has been completed.
1583 	 *
1584 	 *  @param conn Connection object.
1585 	 *  @param bonded Bond information has been distributed during the
1586 	 *                pairing procedure.
1587 	 */
1588 	void (*pairing_complete)(struct bt_conn *conn, bool bonded);
1589 
1590 	/** @brief notify that pairing process has failed.
1591 	 *
1592 	 *  @param conn Connection object.
1593 	 *  @param reason Pairing failed reason
1594 	 */
1595 	void (*pairing_failed)(struct bt_conn *conn,
1596 			       enum bt_security_err reason);
1597 
1598 	/** @brief Notify that bond has been deleted.
1599 	 *
1600 	 *  This callback notifies the application that the bond information
1601 	 *  for the remote peer has been deleted
1602 	 *
1603 	 *  @param id   Which local identity had the bond.
1604 	 *  @param peer Remote address.
1605 	 */
1606 	void (*bond_deleted)(uint8_t id, const bt_addr_le_t *peer);
1607 
1608 	/** Internally used field for list handling */
1609 	sys_snode_t node;
1610 };
1611 
1612 /** @brief Register authentication callbacks.
1613  *
1614  *  Register callbacks to handle authenticated pairing. Passing NULL
1615  *  unregisters a previous callbacks structure.
1616  *
1617  *  @param cb Callback struct.
1618  *
1619  *  @return Zero on success or negative error code otherwise
1620  */
1621 int bt_conn_auth_cb_register(const struct bt_conn_auth_cb *cb);
1622 
1623 /** @brief Overlay authentication callbacks used for a given connection.
1624  *
1625  *  This function can be used only for Bluetooth LE connections.
1626  *  The @kconfig{CONFIG_BT_SMP} must be enabled for this function.
1627  *
1628  *  The authentication callbacks for a given connection cannot be overlaid if
1629  *  security procedures in the SMP module have already started. This function
1630  *  can be called only once per connection.
1631  *
1632  *  @param conn	Connection object.
1633  *  @param cb	Callback struct.
1634  *
1635  *  @return Zero on success or negative error code otherwise
1636  */
1637 int bt_conn_auth_cb_overlay(struct bt_conn *conn, const struct bt_conn_auth_cb *cb);
1638 
1639 /** @brief Register authentication information callbacks.
1640  *
1641  *  Register callbacks to get authenticated pairing information. Multiple
1642  *  registrations can be done.
1643  *
1644  *  @param cb Callback struct.
1645  *
1646  *  @return Zero on success or negative error code otherwise
1647  */
1648 int bt_conn_auth_info_cb_register(struct bt_conn_auth_info_cb *cb);
1649 
1650 /** @brief Unregister authentication information callbacks.
1651  *
1652  *  Unregister callbacks to stop getting authenticated pairing information.
1653  *
1654  *  @param cb Callback struct.
1655  *
1656  *  @return Zero on success or negative error code otherwise
1657  */
1658 int bt_conn_auth_info_cb_unregister(struct bt_conn_auth_info_cb *cb);
1659 
1660 /** @brief Reply with entered passkey.
1661  *
1662  *  This function should be called only after passkey_entry callback from
1663  *  bt_conn_auth_cb structure was called.
1664  *
1665  *  @param conn Connection object.
1666  *  @param passkey Entered passkey.
1667  *
1668  *  @return Zero on success or negative error code otherwise
1669  */
1670 int bt_conn_auth_passkey_entry(struct bt_conn *conn, unsigned int passkey);
1671 
1672 /** @brief Send Passkey Keypress Notification during pairing
1673  *
1674  *  This function may be called only after passkey_entry callback from
1675  *  bt_conn_auth_cb structure was called.
1676  *
1677  *  Requires @kconfig{CONFIG_BT_PASSKEY_KEYPRESS}.
1678  *
1679  *  @param conn Destination for the notification.
1680  *  @param type What keypress event type to send. @see bt_conn_auth_keypress.
1681  *
1682  *  @retval 0 Success
1683  *  @retval -EINVAL Improper use of the API.
1684  *  @retval -ENOMEM Failed to allocate.
1685  *  @retval -ENOBUFS Failed to allocate.
1686  */
1687 int bt_conn_auth_keypress_notify(struct bt_conn *conn, enum bt_conn_auth_keypress type);
1688 
1689 /** @brief Cancel ongoing authenticated pairing.
1690  *
1691  *  This function allows to cancel ongoing authenticated pairing.
1692  *
1693  *  @param conn Connection object.
1694  *
1695  *  @return Zero on success or negative error code otherwise
1696  */
1697 int bt_conn_auth_cancel(struct bt_conn *conn);
1698 
1699 /** @brief Reply if passkey was confirmed to match by user.
1700  *
1701  *  This function should be called only after passkey_confirm callback from
1702  *  bt_conn_auth_cb structure was called.
1703  *
1704  *  @param conn Connection object.
1705  *
1706  *  @return Zero on success or negative error code otherwise
1707  */
1708 int bt_conn_auth_passkey_confirm(struct bt_conn *conn);
1709 
1710 /** @brief Reply if incoming pairing was confirmed by user.
1711  *
1712  *  This function should be called only after pairing_confirm callback from
1713  *  bt_conn_auth_cb structure was called if user confirmed incoming pairing.
1714  *
1715  *  @param conn Connection object.
1716  *
1717  *  @return Zero on success or negative error code otherwise
1718  */
1719 int bt_conn_auth_pairing_confirm(struct bt_conn *conn);
1720 
1721 /** @brief Reply with entered PIN code.
1722  *
1723  *  This function should be called only after PIN code callback from
1724  *  bt_conn_auth_cb structure was called. It's for legacy 2.0 devices.
1725  *
1726  *  @param conn Connection object.
1727  *  @param pin Entered PIN code.
1728  *
1729  *  @return Zero on success or negative error code otherwise
1730  */
1731 int bt_conn_auth_pincode_entry(struct bt_conn *conn, const char *pin);
1732 
1733 /** Connection parameters for BR/EDR connections */
1734 struct bt_br_conn_param {
1735 	bool allow_role_switch;
1736 };
1737 
1738 /** @brief Initialize BR/EDR connection parameters
1739  *
1740  *  @param role_switch True if role switch is allowed
1741  */
1742 #define BT_BR_CONN_PARAM_INIT(role_switch) \
1743 { \
1744 	.allow_role_switch = (role_switch), \
1745 }
1746 
1747 /** Helper to declare BR/EDR connection parameters inline
1748   *
1749   * @param role_switch True if role switch is allowed
1750   */
1751 #define BT_BR_CONN_PARAM(role_switch) \
1752 	((struct bt_br_conn_param[]) { \
1753 		BT_BR_CONN_PARAM_INIT(role_switch) \
1754 	 })
1755 
1756 /** Default BR/EDR connection parameters:
1757  *    Role switch allowed
1758  */
1759 #define BT_BR_CONN_PARAM_DEFAULT BT_BR_CONN_PARAM(true)
1760 
1761 
1762 /** @brief Initiate an BR/EDR connection to a remote device.
1763  *
1764  *  Allows initiate new BR/EDR link to remote peer using its address.
1765  *
1766  *  The caller gets a new reference to the connection object which must be
1767  *  released with bt_conn_unref() once done using the object.
1768  *
1769  *  @param peer  Remote address.
1770  *  @param param Initial connection parameters.
1771  *
1772  *  @return Valid connection object on success or NULL otherwise.
1773  */
1774 struct bt_conn *bt_conn_create_br(const bt_addr_t *peer,
1775 				  const struct bt_br_conn_param *param);
1776 
1777 /** @brief Initiate an SCO connection to a remote device.
1778  *
1779  *  Allows initiate new SCO link to remote peer using its address.
1780  *
1781  *  The caller gets a new reference to the connection object which must be
1782  *  released with bt_conn_unref() once done using the object.
1783  *
1784  *  @param peer  Remote address.
1785  *
1786  *  @return Valid connection object on success or NULL otherwise.
1787  */
1788 struct bt_conn *bt_conn_create_sco(const bt_addr_t *peer);
1789 
1790 #ifdef __cplusplus
1791 }
1792 #endif
1793 
1794 /**
1795  * @}
1796  */
1797 
1798 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_CONN_H_ */
1799