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 /** @brief Passkey Keypress Notification type 483 * 484 * The numeric values are the same as in the Core specification for Pairing 485 * Keypress Notification PDU. 486 */ 487 enum bt_conn_auth_keypress { 488 BT_CONN_AUTH_KEYPRESS_ENTRY_STARTED = 0x00, 489 BT_CONN_AUTH_KEYPRESS_DIGIT_ENTERED = 0x01, 490 BT_CONN_AUTH_KEYPRESS_DIGIT_ERASED = 0x02, 491 BT_CONN_AUTH_KEYPRESS_CLEARED = 0x03, 492 BT_CONN_AUTH_KEYPRESS_ENTRY_COMPLETED = 0x04, 493 }; 494 495 /** @brief Get connection info 496 * 497 * @param conn Connection object. 498 * @param info Connection info object. 499 * 500 * @return Zero on success or (negative) error code on failure. 501 */ 502 int bt_conn_get_info(const struct bt_conn *conn, struct bt_conn_info *info); 503 504 /** @brief Get connection info for the remote device. 505 * 506 * @param conn Connection object. 507 * @param remote_info Connection remote info object. 508 * 509 * @note In order to retrieve the remote version (version, manufacturer 510 * and subversion) @kconfig{CONFIG_BT_REMOTE_VERSION} must be enabled 511 * 512 * @note The remote information is exchanged directly after the connection has 513 * been established. The application can be notified about when the remote 514 * information is available through the remote_info_available callback. 515 * 516 * @return Zero on success or (negative) error code on failure. 517 * @return -EBUSY The remote information is not yet available. 518 */ 519 int bt_conn_get_remote_info(struct bt_conn *conn, 520 struct bt_conn_remote_info *remote_info); 521 522 /** @brief Get connection transmit power level. 523 * 524 * @param conn Connection object. 525 * @param tx_power_level Transmit power level descriptor. 526 * 527 * @return Zero on success or (negative) error code on failure. 528 * @return -ENOBUFS HCI command buffer is not available. 529 */ 530 int bt_conn_le_get_tx_power_level(struct bt_conn *conn, 531 struct bt_conn_le_tx_power *tx_power_level); 532 533 /** @brief Update the connection parameters. 534 * 535 * If the local device is in the peripheral role then updating the connection 536 * parameters will be delayed. This delay can be configured by through the 537 * @kconfig{CONFIG_BT_CONN_PARAM_UPDATE_TIMEOUT} option. 538 * 539 * @param conn Connection object. 540 * @param param Updated connection parameters. 541 * 542 * @return Zero on success or (negative) error code on failure. 543 */ 544 int bt_conn_le_param_update(struct bt_conn *conn, 545 const struct bt_le_conn_param *param); 546 547 /** @brief Update the connection transmit data length parameters. 548 * 549 * @param conn Connection object. 550 * @param param Updated data length parameters. 551 * 552 * @return Zero on success or (negative) error code on failure. 553 */ 554 int bt_conn_le_data_len_update(struct bt_conn *conn, 555 const struct bt_conn_le_data_len_param *param); 556 557 /** @brief Update the connection PHY parameters. 558 * 559 * Update the preferred transmit and receive PHYs of the connection. 560 * Use @ref BT_GAP_LE_PHY_NONE to indicate no preference. 561 * 562 * @param conn Connection object. 563 * @param param Updated connection parameters. 564 * 565 * @return Zero on success or (negative) error code on failure. 566 */ 567 int bt_conn_le_phy_update(struct bt_conn *conn, 568 const struct bt_conn_le_phy_param *param); 569 570 /** @brief Disconnect from a remote device or cancel pending connection. 571 * 572 * Disconnect an active connection with the specified reason code or cancel 573 * pending outgoing connection. 574 * 575 * The disconnect reason for a normal disconnect should be: 576 * @ref BT_HCI_ERR_REMOTE_USER_TERM_CONN. 577 * 578 * The following disconnect reasons are accepted: 579 * - @ref BT_HCI_ERR_AUTH_FAIL 580 * - @ref BT_HCI_ERR_REMOTE_USER_TERM_CONN 581 * - @ref BT_HCI_ERR_REMOTE_LOW_RESOURCES 582 * - @ref BT_HCI_ERR_REMOTE_POWER_OFF 583 * - @ref BT_HCI_ERR_UNSUPP_REMOTE_FEATURE 584 * - @ref BT_HCI_ERR_PAIRING_NOT_SUPPORTED 585 * - @ref BT_HCI_ERR_UNACCEPT_CONN_PARAM 586 * 587 * @param conn Connection to disconnect. 588 * @param reason Reason code for the disconnection. 589 * 590 * @return Zero on success or (negative) error code on failure. 591 */ 592 int bt_conn_disconnect(struct bt_conn *conn, uint8_t reason); 593 594 enum { 595 /** Convenience value when no options are specified. */ 596 BT_CONN_LE_OPT_NONE = 0, 597 598 /** @brief Enable LE Coded PHY. 599 * 600 * Enable scanning on the LE Coded PHY. 601 */ 602 BT_CONN_LE_OPT_CODED = BIT(0), 603 604 /** @brief Disable LE 1M PHY. 605 * 606 * Disable scanning on the LE 1M PHY. 607 * 608 * @note Requires @ref BT_CONN_LE_OPT_CODED. 609 */ 610 BT_CONN_LE_OPT_NO_1M = BIT(1), 611 }; 612 613 struct bt_conn_le_create_param { 614 615 /** Bit-field of create connection options. */ 616 uint32_t options; 617 618 /** Scan interval (N * 0.625 ms) */ 619 uint16_t interval; 620 621 /** Scan window (N * 0.625 ms) */ 622 uint16_t window; 623 624 /** @brief Scan interval LE Coded PHY (N * 0.625 MS) 625 * 626 * Set zero to use same as LE 1M PHY scan interval 627 */ 628 uint16_t interval_coded; 629 630 /** @brief Scan window LE Coded PHY (N * 0.625 MS) 631 * 632 * Set zero to use same as LE 1M PHY scan window. 633 */ 634 uint16_t window_coded; 635 636 /** @brief Connection initiation timeout (N * 10 MS) 637 * 638 * Set zero to use the default @kconfig{CONFIG_BT_CREATE_CONN_TIMEOUT} 639 * timeout. 640 * 641 * @note Unused in @ref bt_conn_le_create_auto 642 */ 643 uint16_t timeout; 644 }; 645 646 /** @brief Initialize create connection parameters 647 * 648 * @param _options Create connection options. 649 * @param _interval Create connection scan interval (N * 0.625 ms). 650 * @param _window Create connection scan window (N * 0.625 ms). 651 */ 652 #define BT_CONN_LE_CREATE_PARAM_INIT(_options, _interval, _window) \ 653 { \ 654 .options = (_options), \ 655 .interval = (_interval), \ 656 .window = (_window), \ 657 .interval_coded = 0, \ 658 .window_coded = 0, \ 659 .timeout = 0, \ 660 } 661 662 /** Helper to declare create connection parameters inline 663 * 664 * @param _options Create connection options. 665 * @param _interval Create connection scan interval (N * 0.625 ms). 666 * @param _window Create connection scan window (N * 0.625 ms). 667 */ 668 #define BT_CONN_LE_CREATE_PARAM(_options, _interval, _window) \ 669 ((struct bt_conn_le_create_param[]) { \ 670 BT_CONN_LE_CREATE_PARAM_INIT(_options, _interval, _window) \ 671 }) 672 673 /** Default LE create connection parameters. 674 * Scan continuously by setting scan interval equal to scan window. 675 */ 676 #define BT_CONN_LE_CREATE_CONN \ 677 BT_CONN_LE_CREATE_PARAM(BT_CONN_LE_OPT_NONE, \ 678 BT_GAP_SCAN_FAST_INTERVAL, \ 679 BT_GAP_SCAN_FAST_INTERVAL) 680 681 /** Default LE create connection using filter accept list parameters. 682 * Scan window: 30 ms. 683 * Scan interval: 60 ms. 684 */ 685 #define BT_CONN_LE_CREATE_CONN_AUTO \ 686 BT_CONN_LE_CREATE_PARAM(BT_CONN_LE_OPT_NONE, \ 687 BT_GAP_SCAN_FAST_INTERVAL, \ 688 BT_GAP_SCAN_FAST_WINDOW) 689 690 /** @brief Initiate an LE connection to a remote device. 691 * 692 * Allows initiate new LE link to remote peer using its address. 693 * 694 * The caller gets a new reference to the connection object which must be 695 * released with bt_conn_unref() once done using the object. 696 * 697 * This uses the General Connection Establishment procedure. 698 * 699 * The application must disable explicit scanning before initiating 700 * a new LE connection. 701 * 702 * @param[in] peer Remote address. 703 * @param[in] create_param Create connection parameters. 704 * @param[in] conn_param Initial connection parameters. 705 * @param[out] conn Valid connection object on success. 706 * 707 * @return Zero on success or (negative) error code on failure. 708 */ 709 int bt_conn_le_create(const bt_addr_le_t *peer, 710 const struct bt_conn_le_create_param *create_param, 711 const struct bt_le_conn_param *conn_param, 712 struct bt_conn **conn); 713 714 struct bt_conn_le_create_synced_param { 715 716 /** @brief Remote address 717 * 718 * The peer must be synchronized to the PAwR train. 719 * 720 */ 721 const bt_addr_le_t *peer; 722 723 /** The subevent where the connection will be initiated. */ 724 uint8_t subevent; 725 }; 726 727 /** @brief Create a connection to a synced device 728 * 729 * Initiate a connection to a synced device from a Periodic Advertising 730 * with Responses (PAwR) train. 731 * 732 * The caller gets a new reference to the connection object which must be 733 * released with bt_conn_unref() once done using the object. 734 * 735 * This uses the Periodic Advertising Connection Procedure. 736 * 737 * @param[in] adv The adverting set the PAwR advertiser belongs to. 738 * @param[in] synced_param Create connection parameters. 739 * @param[in] conn_param Initial connection parameters. 740 * @param[out] conn Valid connection object on success. 741 * 742 * @return Zero on success or (negative) error code on failure. 743 */ 744 int bt_conn_le_create_synced(const struct bt_le_ext_adv *adv, 745 const struct bt_conn_le_create_synced_param *synced_param, 746 const struct bt_le_conn_param *conn_param, struct bt_conn **conn); 747 748 /** @brief Automatically connect to remote devices in the filter accept list. 749 * 750 * This uses the Auto Connection Establishment procedure. 751 * The procedure will continue until a single connection is established or the 752 * procedure is stopped through @ref bt_conn_create_auto_stop. 753 * To establish connections to all devices in the the filter accept list the 754 * procedure should be started again in the connected callback after a 755 * new connection has been established. 756 * 757 * @param create_param Create connection parameters 758 * @param conn_param Initial connection parameters. 759 * 760 * @return Zero on success or (negative) error code on failure. 761 * @return -ENOMEM No free connection object available. 762 */ 763 int bt_conn_le_create_auto(const struct bt_conn_le_create_param *create_param, 764 const struct bt_le_conn_param *conn_param); 765 766 /** @brief Stop automatic connect creation. 767 * 768 * @return Zero on success or (negative) error code on failure. 769 */ 770 int bt_conn_create_auto_stop(void); 771 772 /** @brief Automatically connect to remote device if it's in range. 773 * 774 * This function enables/disables automatic connection initiation. 775 * Every time the device loses the connection with peer, this connection 776 * will be re-established if connectable advertisement from peer is received. 777 * 778 * @note Auto connect is disabled during explicit scanning. 779 * 780 * @param addr Remote Bluetooth address. 781 * @param param If non-NULL, auto connect is enabled with the given 782 * parameters. If NULL, auto connect is disabled. 783 * 784 * @return Zero on success or error code otherwise. 785 */ 786 int bt_le_set_auto_conn(const bt_addr_le_t *addr, 787 const struct bt_le_conn_param *param); 788 789 /** @brief Set security level for a connection. 790 * 791 * This function enable security (encryption) for a connection. If the device 792 * has bond information for the peer with sufficiently strong key encryption 793 * will be enabled. If the connection is already encrypted with sufficiently 794 * strong key this function does nothing. 795 * 796 * If the device has no bond information for the peer and is not already paired 797 * then the pairing procedure will be initiated. Note that @p sec has no effect 798 * on the security level selected for the pairing process. The selection is 799 * instead controlled by the values of the registered @ref bt_conn_auth_cb. If 800 * the device has bond information or is already paired and the keys are too 801 * weak then the pairing procedure will be initiated. 802 * 803 * This function may return error if required level of security is not possible 804 * to achieve due to local or remote device limitation (e.g., input output 805 * capabilities), or if the maximum number of paired devices has been reached. 806 * 807 * This function may return error if the pairing procedure has already been 808 * initiated by the local device or the peer device. 809 * 810 * @note When @kconfig{CONFIG_BT_SMP_SC_ONLY} is enabled then the security 811 * level will always be level 4. 812 * 813 * @note When @kconfig{CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY} is enabled then the 814 * security level will always be level 3. 815 * 816 * @note When @ref BT_SECURITY_FORCE_PAIR within @p sec is enabled then the pairing 817 * procedure will always be initiated. 818 * 819 * @param conn Connection object. 820 * @param sec Requested security level. 821 * 822 * @return 0 on success or negative error 823 */ 824 int bt_conn_set_security(struct bt_conn *conn, bt_security_t sec); 825 826 /** @brief Get security level for a connection. 827 * 828 * @return Connection security level 829 */ 830 bt_security_t bt_conn_get_security(const struct bt_conn *conn); 831 832 /** @brief Get encryption key size. 833 * 834 * This function gets encryption key size. 835 * If there is no security (encryption) enabled 0 will be returned. 836 * 837 * @param conn Existing connection object. 838 * 839 * @return Encryption key size. 840 */ 841 uint8_t bt_conn_enc_key_size(const struct bt_conn *conn); 842 843 enum bt_security_err { 844 /** Security procedure successful. */ 845 BT_SECURITY_ERR_SUCCESS, 846 847 /** Authentication failed. */ 848 BT_SECURITY_ERR_AUTH_FAIL, 849 850 /** PIN or encryption key is missing. */ 851 BT_SECURITY_ERR_PIN_OR_KEY_MISSING, 852 853 /** OOB data is not available. */ 854 BT_SECURITY_ERR_OOB_NOT_AVAILABLE, 855 856 /** The requested security level could not be reached. */ 857 BT_SECURITY_ERR_AUTH_REQUIREMENT, 858 859 /** Pairing is not supported */ 860 BT_SECURITY_ERR_PAIR_NOT_SUPPORTED, 861 862 /** Pairing is not allowed. */ 863 BT_SECURITY_ERR_PAIR_NOT_ALLOWED, 864 865 /** Invalid parameters. */ 866 BT_SECURITY_ERR_INVALID_PARAM, 867 868 /** Distributed Key Rejected */ 869 BT_SECURITY_ERR_KEY_REJECTED, 870 871 /** Pairing failed but the exact reason could not be specified. */ 872 BT_SECURITY_ERR_UNSPECIFIED, 873 }; 874 875 /** @brief Connection callback structure. 876 * 877 * This structure is used for tracking the state of a connection. 878 * It is registered with the help of the bt_conn_cb_register() API. 879 * It's permissible to register multiple instances of this @ref bt_conn_cb 880 * type, in case different modules of an application are interested in 881 * tracking the connection state. If a callback is not of interest for 882 * an instance, it may be set to NULL and will as a consequence not be 883 * used for that instance. 884 */ 885 struct bt_conn_cb { 886 /** @brief A new connection has been established. 887 * 888 * This callback notifies the application of a new connection. 889 * In case the err parameter is non-zero it means that the 890 * connection establishment failed. 891 * 892 * @note If the connection was established from an advertising set then 893 * the advertising set cannot be restarted directly from this 894 * callback. Instead use the connected callback of the 895 * advertising set. 896 * 897 * @param conn New connection object. 898 * @param err HCI error. Zero for success, non-zero otherwise. 899 * 900 * @p err can mean either of the following: 901 * - @ref BT_HCI_ERR_UNKNOWN_CONN_ID Creating the connection started by 902 * @ref bt_conn_le_create was canceled either by the user through 903 * @ref bt_conn_disconnect or by the timeout in the host through 904 * @ref bt_conn_le_create_param timeout parameter, which defaults to 905 * @kconfig{CONFIG_BT_CREATE_CONN_TIMEOUT} seconds. 906 * - @p BT_HCI_ERR_ADV_TIMEOUT High duty cycle directed connectable 907 * advertiser started by @ref bt_le_adv_start failed to be connected 908 * within the timeout. 909 */ 910 void (*connected)(struct bt_conn *conn, uint8_t err); 911 912 /** @brief A connection has been disconnected. 913 * 914 * This callback notifies the application that a connection 915 * has been disconnected. 916 * 917 * When this callback is called the stack still has one reference to 918 * the connection object. If the application in this callback tries to 919 * start either a connectable advertiser or create a new connection 920 * this might fail because there are no free connection objects 921 * available. 922 * To avoid this issue it is recommended to either start connectable 923 * advertise or create a new connection using @ref k_work_submit or 924 * increase @kconfig{CONFIG_BT_MAX_CONN}. 925 * 926 * @param conn Connection object. 927 * @param reason BT_HCI_ERR_* reason for the disconnection. 928 */ 929 void (*disconnected)(struct bt_conn *conn, uint8_t reason); 930 931 /** @brief LE connection parameter update request. 932 * 933 * This callback notifies the application that a remote device 934 * is requesting to update the connection parameters. The 935 * application accepts the parameters by returning true, or 936 * rejects them by returning false. Before accepting, the 937 * application may also adjust the parameters to better suit 938 * its needs. 939 * 940 * It is recommended for an application to have just one of these 941 * callbacks for simplicity. However, if an application registers 942 * multiple it needs to manage the potentially different 943 * requirements for each callback. Each callback gets the 944 * parameters as returned by previous callbacks, i.e. they are not 945 * necessarily the same ones as the remote originally sent. 946 * 947 * If the application does not have this callback then the default 948 * is to accept the parameters. 949 * 950 * @param conn Connection object. 951 * @param param Proposed connection parameters. 952 * 953 * @return true to accept the parameters, or false to reject them. 954 */ 955 bool (*le_param_req)(struct bt_conn *conn, 956 struct bt_le_conn_param *param); 957 958 /** @brief The parameters for an LE connection have been updated. 959 * 960 * This callback notifies the application that the connection 961 * parameters for an LE connection have been updated. 962 * 963 * @param conn Connection object. 964 * @param interval Connection interval. 965 * @param latency Connection latency. 966 * @param timeout Connection supervision timeout. 967 */ 968 void (*le_param_updated)(struct bt_conn *conn, uint16_t interval, 969 uint16_t latency, uint16_t timeout); 970 #if defined(CONFIG_BT_SMP) 971 /** @brief Remote Identity Address has been resolved. 972 * 973 * This callback notifies the application that a remote 974 * Identity Address has been resolved 975 * 976 * @param conn Connection object. 977 * @param rpa Resolvable Private Address. 978 * @param identity Identity Address. 979 */ 980 void (*identity_resolved)(struct bt_conn *conn, 981 const bt_addr_le_t *rpa, 982 const bt_addr_le_t *identity); 983 #endif /* CONFIG_BT_SMP */ 984 #if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_BREDR) 985 /** @brief The security level of a connection has changed. 986 * 987 * This callback notifies the application that the security of a 988 * connection has changed. 989 * 990 * The security level of the connection can either have been increased 991 * or remain unchanged. An increased security level means that the 992 * pairing procedure has been performed or the bond information from 993 * a previous connection has been applied. If the security level 994 * remains unchanged this means that the encryption key has been 995 * refreshed for the connection. 996 * 997 * @param conn Connection object. 998 * @param level New security level of the connection. 999 * @param err Security error. Zero for success, non-zero otherwise. 1000 */ 1001 void (*security_changed)(struct bt_conn *conn, bt_security_t level, 1002 enum bt_security_err err); 1003 #endif /* defined(CONFIG_BT_SMP) || defined(CONFIG_BT_BREDR) */ 1004 1005 #if defined(CONFIG_BT_REMOTE_INFO) 1006 /** @brief Remote information procedures has completed. 1007 * 1008 * This callback notifies the application that the remote information 1009 * has been retrieved from the remote peer. 1010 * 1011 * @param conn Connection object. 1012 * @param remote_info Connection information of remote device. 1013 */ 1014 void (*remote_info_available)(struct bt_conn *conn, 1015 struct bt_conn_remote_info *remote_info); 1016 #endif /* defined(CONFIG_BT_REMOTE_INFO) */ 1017 1018 #if defined(CONFIG_BT_USER_PHY_UPDATE) 1019 /** @brief The PHY of the connection has changed. 1020 * 1021 * This callback notifies the application that the PHY of the 1022 * connection has changed. 1023 * 1024 * @param conn Connection object. 1025 * @param info Connection LE PHY information. 1026 */ 1027 void (*le_phy_updated)(struct bt_conn *conn, 1028 struct bt_conn_le_phy_info *param); 1029 #endif /* defined(CONFIG_BT_USER_PHY_UPDATE) */ 1030 1031 #if defined(CONFIG_BT_USER_DATA_LEN_UPDATE) 1032 /** @brief The data length parameters of the connection has changed. 1033 * 1034 * This callback notifies the application that the maximum Link Layer 1035 * payload length or transmission time has changed. 1036 * 1037 * @param conn Connection object. 1038 * @param info Connection data length information. 1039 */ 1040 void (*le_data_len_updated)(struct bt_conn *conn, 1041 struct bt_conn_le_data_len_info *info); 1042 #endif /* defined(CONFIG_BT_USER_DATA_LEN_UPDATE) */ 1043 1044 #if defined(CONFIG_BT_DF_CONNECTION_CTE_RX) 1045 /** @brief Callback for IQ samples report collected when sampling 1046 * CTE received by data channel PDU. 1047 * 1048 * @param conn The connection object. 1049 * @param iq_report Report data for collected IQ samples. 1050 */ 1051 void (*cte_report_cb)(struct bt_conn *conn, 1052 const struct bt_df_conn_iq_samples_report *iq_report); 1053 #endif /* CONFIG_BT_DF_CONNECTION_CTE_RX */ 1054 1055 struct bt_conn_cb *_next; 1056 }; 1057 1058 /** @brief Register connection callbacks. 1059 * 1060 * Register callbacks to monitor the state of connections. 1061 * 1062 * @param cb Callback struct. Must point to memory that remains valid. 1063 */ 1064 void bt_conn_cb_register(struct bt_conn_cb *cb); 1065 1066 /** 1067 * @brief Register a callback structure for connection events. 1068 * 1069 * @param _name Name of callback structure. 1070 */ 1071 #define BT_CONN_CB_DEFINE(_name) \ 1072 static const STRUCT_SECTION_ITERABLE(bt_conn_cb, \ 1073 _CONCAT(bt_conn_cb_, \ 1074 _name)) 1075 1076 /** @brief Enable/disable bonding. 1077 * 1078 * Set/clear the Bonding flag in the Authentication Requirements of 1079 * SMP Pairing Request/Response data. 1080 * The initial value of this flag depends on BT_BONDABLE Kconfig setting. 1081 * For the vast majority of applications calling this function shouldn't be 1082 * needed. 1083 * 1084 * @param enable Value allowing/disallowing to be bondable. 1085 */ 1086 void bt_set_bondable(bool enable); 1087 1088 /** @brief Set/clear the bonding flag for a given connection. 1089 * 1090 * Set/clear the Bonding flag in the Authentication Requirements of 1091 * SMP Pairing Request/Response data for a given connection. 1092 * 1093 * The bonding flag for a given connection cannot be set/cleared if 1094 * security procedures in the SMP module have already started. 1095 * This function can be called only once per connection. 1096 * 1097 * If the bonding flag is not set/cleared for a given connection, 1098 * the value will depend on global configuration which is set using 1099 * bt_set_bondable. 1100 * The default value of the global configuration is defined using 1101 * CONFIG_BT_BONDABLE Kconfig option. 1102 * 1103 * @param conn Connection object. 1104 * @param enable Value allowing/disallowing to be bondable. 1105 */ 1106 int bt_conn_set_bondable(struct bt_conn *conn, bool enable); 1107 1108 /** @brief Allow/disallow remote LE SC OOB data to be used for pairing. 1109 * 1110 * Set/clear the OOB data flag for LE SC SMP Pairing Request/Response data. 1111 * 1112 * @param enable Value allowing/disallowing remote LE SC OOB data. 1113 */ 1114 void bt_le_oob_set_sc_flag(bool enable); 1115 1116 /** @brief Allow/disallow remote legacy OOB data to be used for pairing. 1117 * 1118 * Set/clear the OOB data flag for legacy SMP Pairing Request/Response data. 1119 * 1120 * @param enable Value allowing/disallowing remote legacy OOB data. 1121 */ 1122 void bt_le_oob_set_legacy_flag(bool enable); 1123 1124 /** @brief Set OOB Temporary Key to be used for pairing 1125 * 1126 * This function allows to set OOB data for the LE legacy pairing procedure. 1127 * The function should only be called in response to the oob_data_request() 1128 * callback provided that the legacy method is user pairing. 1129 * 1130 * @param conn Connection object 1131 * @param tk Pointer to 16 byte long TK array 1132 * 1133 * @return Zero on success or -EINVAL if NULL 1134 */ 1135 int bt_le_oob_set_legacy_tk(struct bt_conn *conn, const uint8_t *tk); 1136 1137 /** @brief Set OOB data during LE Secure Connections (SC) pairing procedure 1138 * 1139 * This function allows to set OOB data during the LE SC pairing procedure. 1140 * The function should only be called in response to the oob_data_request() 1141 * callback provided that LE SC method is used for pairing. 1142 * 1143 * The user should submit OOB data according to the information received in the 1144 * callback. This may yield three different configurations: with only local OOB 1145 * data present, with only remote OOB data present or with both local and 1146 * remote OOB data present. 1147 * 1148 * @param conn Connection object 1149 * @param oobd_local Local OOB data or NULL if not present 1150 * @param oobd_remote Remote OOB data or NULL if not present 1151 * 1152 * @return Zero on success or error code otherwise, positive in case of 1153 * protocol error or negative (POSIX) in case of stack internal error. 1154 */ 1155 int bt_le_oob_set_sc_data(struct bt_conn *conn, 1156 const struct bt_le_oob_sc_data *oobd_local, 1157 const struct bt_le_oob_sc_data *oobd_remote); 1158 1159 /** @brief Get OOB data used for LE Secure Connections (SC) pairing procedure 1160 * 1161 * This function allows to get OOB data during the LE SC pairing procedure that 1162 * were set by the bt_le_oob_set_sc_data() API. 1163 * 1164 * @note The OOB data will only be available as long as the connection object 1165 * associated with it is valid. 1166 * 1167 * @param conn Connection object 1168 * @param oobd_local Local OOB data or NULL if not set 1169 * @param oobd_remote Remote OOB data or NULL if not set 1170 * 1171 * @return Zero on success or error code otherwise, positive in case of 1172 * protocol error or negative (POSIX) in case of stack internal error. 1173 */ 1174 int bt_le_oob_get_sc_data(struct bt_conn *conn, 1175 const struct bt_le_oob_sc_data **oobd_local, 1176 const struct bt_le_oob_sc_data **oobd_remote); 1177 1178 /** 1179 * Special passkey value that can be used to disable a previously 1180 * set fixed passkey. 1181 */ 1182 #define BT_PASSKEY_INVALID 0xffffffff 1183 1184 /** @brief Set a fixed passkey to be used for pairing. 1185 * 1186 * This API is only available when the CONFIG_BT_FIXED_PASSKEY 1187 * configuration option has been enabled. 1188 * 1189 * Sets a fixed passkey to be used for pairing. If set, the 1190 * pairing_confirm() callback will be called for all incoming pairings. 1191 * 1192 * @param passkey A valid passkey (0 - 999999) or BT_PASSKEY_INVALID 1193 * to disable a previously set fixed passkey. 1194 * 1195 * @return 0 on success or a negative error code on failure. 1196 */ 1197 int bt_passkey_set(unsigned int passkey); 1198 1199 /** Info Structure for OOB pairing */ 1200 struct bt_conn_oob_info { 1201 /** Type of OOB pairing method */ 1202 enum { 1203 /** LE legacy pairing */ 1204 BT_CONN_OOB_LE_LEGACY, 1205 1206 /** LE SC pairing */ 1207 BT_CONN_OOB_LE_SC, 1208 } type; 1209 1210 union { 1211 /** LE Secure Connections OOB pairing parameters */ 1212 struct { 1213 /** OOB data configuration */ 1214 enum { 1215 /** Local OOB data requested */ 1216 BT_CONN_OOB_LOCAL_ONLY, 1217 1218 /** Remote OOB data requested */ 1219 BT_CONN_OOB_REMOTE_ONLY, 1220 1221 /** Both local and remote OOB data requested */ 1222 BT_CONN_OOB_BOTH_PEERS, 1223 1224 /** No OOB data requested */ 1225 BT_CONN_OOB_NO_DATA, 1226 } oob_config; 1227 } lesc; 1228 }; 1229 }; 1230 1231 #if defined(CONFIG_BT_SMP_APP_PAIRING_ACCEPT) 1232 /** @brief Pairing request and pairing response info structure. 1233 * 1234 * This structure is the same for both smp_pairing_req and smp_pairing_rsp 1235 * and a subset of the packet data, except for the initial Code octet. 1236 * It is documented in Core Spec. Vol. 3, Part H, 3.5.1 and 3.5.2. 1237 */ 1238 struct bt_conn_pairing_feat { 1239 /** IO Capability, Core Spec. Vol 3, Part H, 3.5.1, Table 3.4 */ 1240 uint8_t io_capability; 1241 1242 /** OOB data flag, Core Spec. Vol 3, Part H, 3.5.1, Table 3.5 */ 1243 uint8_t oob_data_flag; 1244 1245 /** AuthReq, Core Spec. Vol 3, Part H, 3.5.1, Fig. 3.3 */ 1246 uint8_t auth_req; 1247 1248 /** Maximum Encryption Key Size, Core Spec. Vol 3, Part H, 3.5.1 */ 1249 uint8_t max_enc_key_size; 1250 1251 /** Initiator Key Distribution/Generation, Core Spec. Vol 3, Part H, 1252 * 3.6.1, Fig. 3.11 1253 */ 1254 uint8_t init_key_dist; 1255 1256 /** Responder Key Distribution/Generation, Core Spec. Vol 3, Part H 1257 * 3.6.1, Fig. 3.11 1258 */ 1259 uint8_t resp_key_dist; 1260 }; 1261 #endif /* CONFIG_BT_SMP_APP_PAIRING_ACCEPT */ 1262 1263 /** Authenticated pairing callback structure */ 1264 struct bt_conn_auth_cb { 1265 #if defined(CONFIG_BT_SMP_APP_PAIRING_ACCEPT) 1266 /** @brief Query to proceed incoming pairing or not. 1267 * 1268 * On any incoming pairing req/rsp this callback will be called for 1269 * the application to decide whether to allow for the pairing to 1270 * continue. 1271 * 1272 * The pairing info received from the peer is passed to assist 1273 * making the decision. 1274 * 1275 * As this callback is synchronous the application should return 1276 * a response value immediately. Otherwise it may affect the 1277 * timing during pairing. Hence, this information should not be 1278 * conveyed to the user to take action. 1279 * 1280 * The remaining callbacks are not affected by this, but do notice 1281 * that other callbacks can be called during the pairing. Eg. if 1282 * pairing_confirm is registered both will be called for Just-Works 1283 * pairings. 1284 * 1285 * This callback may be unregistered in which case pairing continues 1286 * as if the Kconfig flag was not set. 1287 * 1288 * This callback is not called for BR/EDR Secure Simple Pairing (SSP). 1289 * 1290 * @param conn Connection where pairing is initiated. 1291 * @param feat Pairing req/resp info. 1292 */ 1293 enum bt_security_err (*pairing_accept)(struct bt_conn *conn, 1294 const struct bt_conn_pairing_feat *const feat); 1295 #endif /* CONFIG_BT_SMP_APP_PAIRING_ACCEPT */ 1296 1297 /** @brief Display a passkey to the user. 1298 * 1299 * When called the application is expected to display the given 1300 * passkey to the user, with the expectation that the passkey will 1301 * then be entered on the peer device. The passkey will be in the 1302 * range of 0 - 999999, and is expected to be padded with zeroes so 1303 * that six digits are always shown. E.g. the value 37 should be 1304 * shown as 000037. 1305 * 1306 * This callback may be set to NULL, which means that the local 1307 * device lacks the ability do display a passkey. If set 1308 * to non-NULL the cancel callback must also be provided, since 1309 * this is the only way the application can find out that it should 1310 * stop displaying the passkey. 1311 * 1312 * @param conn Connection where pairing is currently active. 1313 * @param passkey Passkey to show to the user. 1314 */ 1315 void (*passkey_display)(struct bt_conn *conn, unsigned int passkey); 1316 1317 #if defined(CONFIG_BT_PASSKEY_KEYPRESS) 1318 /** @brief Receive Passkey Keypress Notification during pairing 1319 * 1320 * This allows the remote device to use the local device to give users 1321 * feedback on the progress of entering the passkey over there. This is 1322 * useful when the remote device itself has no display suitable for 1323 * showing the progress. 1324 * 1325 * The handler of this callback is expected to keep track of the number 1326 * of digits entered and show a password-field-like feedback to the 1327 * user. 1328 * 1329 * This callback is only relevant while the local side does Passkey 1330 * Display. 1331 * 1332 * The event type is verified to be in range of the enum. No other 1333 * sanitization has been done. The remote could send a large number of 1334 * events of any type in any order. 1335 * 1336 * @param conn The related connection. 1337 * @param type Type of event. Verified in range of the enum. 1338 */ 1339 void (*passkey_display_keypress)(struct bt_conn *conn, 1340 enum bt_conn_auth_keypress type); 1341 #endif 1342 1343 /** @brief Request the user to enter a passkey. 1344 * 1345 * When called the user is expected to enter a passkey. The passkey 1346 * must be in the range of 0 - 999999, and should be expected to 1347 * be zero-padded, as that's how the peer device will typically be 1348 * showing it (e.g. 37 would be shown as 000037). 1349 * 1350 * Once the user has entered the passkey its value should be given 1351 * to the stack using the bt_conn_auth_passkey_entry() API. 1352 * 1353 * This callback may be set to NULL, which means that the local 1354 * device lacks the ability to enter a passkey. If set to non-NULL 1355 * the cancel callback must also be provided, since this is the 1356 * only way the application can find out that it should stop 1357 * requesting the user to enter a passkey. 1358 * 1359 * @param conn Connection where pairing is currently active. 1360 */ 1361 void (*passkey_entry)(struct bt_conn *conn); 1362 1363 /** @brief Request the user to confirm a passkey. 1364 * 1365 * When called the user is expected to confirm that the given 1366 * passkey is also shown on the peer device.. The passkey will 1367 * be in the range of 0 - 999999, and should be zero-padded to 1368 * always be six digits (e.g. 37 would be shown as 000037). 1369 * 1370 * Once the user has confirmed the passkey to match, the 1371 * bt_conn_auth_passkey_confirm() API should be called. If the 1372 * user concluded that the passkey doesn't match the 1373 * bt_conn_auth_cancel() API should be called. 1374 * 1375 * This callback may be set to NULL, which means that the local 1376 * device lacks the ability to confirm a passkey. If set to non-NULL 1377 * the cancel callback must also be provided, since this is the 1378 * only way the application can find out that it should stop 1379 * requesting the user to confirm a passkey. 1380 * 1381 * @param conn Connection where pairing is currently active. 1382 * @param passkey Passkey to be confirmed. 1383 */ 1384 void (*passkey_confirm)(struct bt_conn *conn, unsigned int passkey); 1385 1386 /** @brief Request the user to provide Out of Band (OOB) data. 1387 * 1388 * When called the user is expected to provide OOB data. The required 1389 * data are indicated by the information structure. 1390 * 1391 * For LE Secure Connections OOB pairing, the user should provide 1392 * local OOB data, remote OOB data or both depending on their 1393 * availability. Their value should be given to the stack using the 1394 * bt_le_oob_set_sc_data() API. 1395 * 1396 * This callback must be set to non-NULL in order to support OOB 1397 * pairing. 1398 * 1399 * @param conn Connection where pairing is currently active. 1400 * @param info OOB pairing information. 1401 */ 1402 void (*oob_data_request)(struct bt_conn *conn, 1403 struct bt_conn_oob_info *info); 1404 1405 /** @brief Cancel the ongoing user request. 1406 * 1407 * This callback will be called to notify the application that it 1408 * should cancel any previous user request (passkey display, entry 1409 * or confirmation). 1410 * 1411 * This may be set to NULL, but must always be provided whenever the 1412 * passkey_display, passkey_entry passkey_confirm or pairing_confirm 1413 * callback has been provided. 1414 * 1415 * @param conn Connection where pairing is currently active. 1416 */ 1417 void (*cancel)(struct bt_conn *conn); 1418 1419 /** @brief Request confirmation for an incoming pairing. 1420 * 1421 * This callback will be called to confirm an incoming pairing 1422 * request where none of the other user callbacks is applicable. 1423 * 1424 * If the user decides to accept the pairing the 1425 * bt_conn_auth_pairing_confirm() API should be called. If the 1426 * user decides to reject the pairing the bt_conn_auth_cancel() API 1427 * should be called. 1428 * 1429 * This callback may be set to NULL, which means that the local 1430 * device lacks the ability to confirm a pairing request. If set 1431 * to non-NULL the cancel callback must also be provided, since 1432 * this is the only way the application can find out that it should 1433 * stop requesting the user to confirm a pairing request. 1434 * 1435 * @param conn Connection where pairing is currently active. 1436 */ 1437 void (*pairing_confirm)(struct bt_conn *conn); 1438 1439 #if defined(CONFIG_BT_BREDR) 1440 /** @brief Request the user to enter a passkey. 1441 * 1442 * This callback will be called for a BR/EDR (Bluetooth Classic) 1443 * connection where pairing is being performed. Once called the 1444 * user is expected to enter a PIN code with a length between 1445 * 1 and 16 digits. If the @a highsec parameter is set to true 1446 * the PIN code must be 16 digits long. 1447 * 1448 * Once entered, the PIN code should be given to the stack using 1449 * the bt_conn_auth_pincode_entry() API. 1450 * 1451 * This callback may be set to NULL, however in that case pairing 1452 * over BR/EDR will not be possible. If provided, the cancel 1453 * callback must be provided as well. 1454 * 1455 * @param conn Connection where pairing is currently active. 1456 * @param highsec true if 16 digit PIN is required. 1457 */ 1458 void (*pincode_entry)(struct bt_conn *conn, bool highsec); 1459 #endif 1460 }; 1461 1462 /** Authenticated pairing information callback structure */ 1463 struct bt_conn_auth_info_cb { 1464 /** @brief notify that pairing procedure was complete. 1465 * 1466 * This callback notifies the application that the pairing procedure 1467 * has been completed. 1468 * 1469 * @param conn Connection object. 1470 * @param bonded Bond information has been distributed during the 1471 * pairing procedure. 1472 */ 1473 void (*pairing_complete)(struct bt_conn *conn, bool bonded); 1474 1475 /** @brief notify that pairing process has failed. 1476 * 1477 * @param conn Connection object. 1478 * @param reason Pairing failed reason 1479 */ 1480 void (*pairing_failed)(struct bt_conn *conn, 1481 enum bt_security_err reason); 1482 1483 /** @brief Notify that bond has been deleted. 1484 * 1485 * This callback notifies the application that the bond information 1486 * for the remote peer has been deleted 1487 * 1488 * @param id Which local identity had the bond. 1489 * @param peer Remote address. 1490 */ 1491 void (*bond_deleted)(uint8_t id, const bt_addr_le_t *peer); 1492 1493 /** Internally used field for list handling */ 1494 sys_snode_t node; 1495 }; 1496 1497 /** @brief Register authentication callbacks. 1498 * 1499 * Register callbacks to handle authenticated pairing. Passing NULL 1500 * unregisters a previous callbacks structure. 1501 * 1502 * @param cb Callback struct. 1503 * 1504 * @return Zero on success or negative error code otherwise 1505 */ 1506 int bt_conn_auth_cb_register(const struct bt_conn_auth_cb *cb); 1507 1508 /** @brief Overlay authentication callbacks used for a given connection. 1509 * 1510 * This function can be used only for Bluetooth LE connections. 1511 * The @kconfig{CONFIG_BT_SMP} must be enabled for this function. 1512 * 1513 * The authentication callbacks for a given connection cannot be overlaid if 1514 * security procedures in the SMP module have already started. This function 1515 * can be called only once per connection. 1516 * 1517 * @param conn Connection object. 1518 * @param cb Callback struct. 1519 * 1520 * @return Zero on success or negative error code otherwise 1521 */ 1522 int bt_conn_auth_cb_overlay(struct bt_conn *conn, const struct bt_conn_auth_cb *cb); 1523 1524 /** @brief Register authentication information callbacks. 1525 * 1526 * Register callbacks to get authenticated pairing information. Multiple 1527 * registrations can be done. 1528 * 1529 * @param cb Callback struct. 1530 * 1531 * @return Zero on success or negative error code otherwise 1532 */ 1533 int bt_conn_auth_info_cb_register(struct bt_conn_auth_info_cb *cb); 1534 1535 /** @brief Unregister authentication information callbacks. 1536 * 1537 * Unregister callbacks to stop getting authenticated pairing information. 1538 * 1539 * @param cb Callback struct. 1540 * 1541 * @return Zero on success or negative error code otherwise 1542 */ 1543 int bt_conn_auth_info_cb_unregister(struct bt_conn_auth_info_cb *cb); 1544 1545 /** @brief Reply with entered passkey. 1546 * 1547 * This function should be called only after passkey_entry callback from 1548 * bt_conn_auth_cb structure was called. 1549 * 1550 * @param conn Connection object. 1551 * @param passkey Entered passkey. 1552 * 1553 * @return Zero on success or negative error code otherwise 1554 */ 1555 int bt_conn_auth_passkey_entry(struct bt_conn *conn, unsigned int passkey); 1556 1557 /** @brief Send Passkey Keypress Notification during pairing 1558 * 1559 * This function may be called only after passkey_entry callback from 1560 * bt_conn_auth_cb structure was called. 1561 * 1562 * Requires @kconfig{CONFIG_BT_PASSKEY_KEYPRESS}. 1563 * 1564 * @param conn Destination for the notification. 1565 * @param type What keypress event type to send. @see bt_conn_auth_keypress. 1566 * 1567 * @retval 0 Success 1568 * @retval -EINVAL Improper use of the API. 1569 * @retval -ENOMEM Failed to allocate. 1570 * @retval -ENOBUFS Failed to allocate. 1571 */ 1572 int bt_conn_auth_keypress_notify(struct bt_conn *conn, enum bt_conn_auth_keypress type); 1573 1574 /** @brief Cancel ongoing authenticated pairing. 1575 * 1576 * This function allows to cancel ongoing authenticated pairing. 1577 * 1578 * @param conn Connection object. 1579 * 1580 * @return Zero on success or negative error code otherwise 1581 */ 1582 int bt_conn_auth_cancel(struct bt_conn *conn); 1583 1584 /** @brief Reply if passkey was confirmed to match by user. 1585 * 1586 * This function should be called only after passkey_confirm callback from 1587 * bt_conn_auth_cb structure was called. 1588 * 1589 * @param conn Connection object. 1590 * 1591 * @return Zero on success or negative error code otherwise 1592 */ 1593 int bt_conn_auth_passkey_confirm(struct bt_conn *conn); 1594 1595 /** @brief Reply if incoming pairing was confirmed by user. 1596 * 1597 * This function should be called only after pairing_confirm callback from 1598 * bt_conn_auth_cb structure was called if user confirmed incoming pairing. 1599 * 1600 * @param conn Connection object. 1601 * 1602 * @return Zero on success or negative error code otherwise 1603 */ 1604 int bt_conn_auth_pairing_confirm(struct bt_conn *conn); 1605 1606 /** @brief Reply with entered PIN code. 1607 * 1608 * This function should be called only after PIN code callback from 1609 * bt_conn_auth_cb structure was called. It's for legacy 2.0 devices. 1610 * 1611 * @param conn Connection object. 1612 * @param pin Entered PIN code. 1613 * 1614 * @return Zero on success or negative error code otherwise 1615 */ 1616 int bt_conn_auth_pincode_entry(struct bt_conn *conn, const char *pin); 1617 1618 /** Connection parameters for BR/EDR connections */ 1619 struct bt_br_conn_param { 1620 bool allow_role_switch; 1621 }; 1622 1623 /** @brief Initialize BR/EDR connection parameters 1624 * 1625 * @param role_switch True if role switch is allowed 1626 */ 1627 #define BT_BR_CONN_PARAM_INIT(role_switch) \ 1628 { \ 1629 .allow_role_switch = (role_switch), \ 1630 } 1631 1632 /** Helper to declare BR/EDR connection parameters inline 1633 * 1634 * @param role_switch True if role switch is allowed 1635 */ 1636 #define BT_BR_CONN_PARAM(role_switch) \ 1637 ((struct bt_br_conn_param[]) { \ 1638 BT_BR_CONN_PARAM_INIT(role_switch) \ 1639 }) 1640 1641 /** Default BR/EDR connection parameters: 1642 * Role switch allowed 1643 */ 1644 #define BT_BR_CONN_PARAM_DEFAULT BT_BR_CONN_PARAM(true) 1645 1646 1647 /** @brief Initiate an BR/EDR connection to a remote device. 1648 * 1649 * Allows initiate new BR/EDR link to remote peer using its address. 1650 * 1651 * The caller gets a new reference to the connection object which must be 1652 * released with bt_conn_unref() once done using the object. 1653 * 1654 * @param peer Remote address. 1655 * @param param Initial connection parameters. 1656 * 1657 * @return Valid connection object on success or NULL otherwise. 1658 */ 1659 struct bt_conn *bt_conn_create_br(const bt_addr_t *peer, 1660 const struct bt_br_conn_param *param); 1661 1662 /** @brief Initiate an SCO connection to a remote device. 1663 * 1664 * Allows initiate new SCO link to remote peer using its address. 1665 * 1666 * The caller gets a new reference to the connection object which must be 1667 * released with bt_conn_unref() once done using the object. 1668 * 1669 * @param peer Remote address. 1670 * 1671 * @return Valid connection object on success or NULL otherwise. 1672 */ 1673 struct bt_conn *bt_conn_create_sco(const bt_addr_t *peer); 1674 1675 #ifdef __cplusplus 1676 } 1677 #endif 1678 1679 /** 1680 * @} 1681 */ 1682 1683 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_CONN_H_ */ 1684