1 /** @file 2 * @brief Bluetooth subsystem core APIs. 3 */ 4 5 /* 6 * Copyright (c) 2017 Nordic Semiconductor ASA 7 * Copyright (c) 2015-2016 Intel Corporation 8 * 9 * SPDX-License-Identifier: Apache-2.0 10 */ 11 #ifndef ZEPHYR_INCLUDE_BLUETOOTH_BLUETOOTH_H_ 12 #define ZEPHYR_INCLUDE_BLUETOOTH_BLUETOOTH_H_ 13 14 /** 15 * @brief Bluetooth APIs 16 * @details The Bluetooth Subsystem Core APIs provide essential functionalities 17 * to use and manage Bluetooth based communication. These APIs include 18 * APIs for Bluetooth stack initialization, device discovery, 19 * connection management, data transmission, profiles and services. 20 * These APIs support both classic Bluetooth and Bluetooth Low Energy 21 * (LE) operations. 22 * @defgroup bluetooth Bluetooth APIs 23 * @ingroup connectivity 24 * @{ 25 */ 26 27 #include <stdbool.h> 28 #include <stdint.h> 29 #include <string.h> 30 31 #include <zephyr/sys/util.h> 32 #include <zephyr/net_buf.h> 33 #include <zephyr/bluetooth/gap.h> 34 #include <zephyr/bluetooth/addr.h> 35 #include <zephyr/bluetooth/crypto.h> 36 #include <zephyr/bluetooth/classic/classic.h> 37 38 #ifdef __cplusplus 39 extern "C" { 40 #endif 41 42 /** 43 * @brief Generic Access Profile (GAP) 44 * @details The Generic Access Profile (GAP) defines fundamental Bluetooth 45 * operations, including device discovery, pairing, and connection 46 * management. Zephyr's GAP implementation supports both classic 47 * Bluetooth and Bluetooth Low Energy (LE) functionalities, enabling 48 * roles such as Broadcaster, Observer, Peripheral, and Central. These 49 * roles define the device's behavior in advertising, scanning, and 50 * establishing connections within Bluetooth networks. 51 * @defgroup bt_gap Generic Access Profile (GAP) 52 * @since 1.0 53 * @version 1.0.0 54 * @ingroup bluetooth 55 * @{ 56 */ 57 58 /** 59 * Convenience macro for specifying the default identity. This helps 60 * make the code more readable, especially when only one identity is 61 * supported. 62 */ 63 #define BT_ID_DEFAULT 0 64 65 /** 66 * @brief Number of octets for local supported 67 * 68 * The value of 8 correspond to page 0 in the LE Controller supported features 69 */ 70 #define BT_LE_LOCAL_SUPPORTED_FEATURES_SIZE 8 71 72 /** Opaque type representing an advertiser. */ 73 struct bt_le_ext_adv; 74 75 /** Opaque type representing an periodic advertising sync. */ 76 struct bt_le_per_adv_sync; 77 78 /* Don't require everyone to include conn.h */ 79 struct bt_conn; 80 81 /* Don't require everyone to include iso.h */ 82 struct bt_iso_biginfo; 83 84 /* Don't require everyone to include direction.h */ 85 struct bt_df_per_adv_sync_iq_samples_report; 86 87 struct bt_le_ext_adv_sent_info { 88 /** The number of advertising events completed. */ 89 uint8_t num_sent; 90 }; 91 92 struct bt_le_ext_adv_connected_info { 93 /** Connection object of the new connection */ 94 struct bt_conn *conn; 95 }; 96 97 struct bt_le_ext_adv_scanned_info { 98 /** Active scanner LE address and type */ 99 bt_addr_le_t *addr; 100 }; 101 102 struct bt_le_per_adv_data_request { 103 /** The first subevent data can be set for */ 104 uint8_t start; 105 106 /** The number of subevents data can be set for */ 107 uint8_t count; 108 }; 109 110 struct bt_le_per_adv_response_info { 111 /** The subevent the response was received in */ 112 uint8_t subevent; 113 114 /** @brief Status of the subevent indication. 115 * 116 * 0 if subevent indication was transmitted. 117 * 1 if subevent indication was not transmitted. 118 * All other values RFU. 119 */ 120 uint8_t tx_status; 121 122 /** The TX power of the response in dBm */ 123 int8_t tx_power; 124 125 /** The RSSI of the response in dBm */ 126 int8_t rssi; 127 128 /** The Constant Tone Extension (CTE) of the advertisement (@ref bt_df_cte_type) */ 129 uint8_t cte_type; 130 131 /** The slot the response was received in */ 132 uint8_t response_slot; 133 }; 134 135 struct bt_le_ext_adv_cb { 136 /** 137 * @brief The advertising set has finished sending adv data. 138 * 139 * This callback notifies the application that the advertising set has 140 * finished sending advertising data. 141 * The advertising set can either have been stopped by a timeout or 142 * because the specified number of advertising events has been reached. 143 * 144 * @param adv The advertising set object. 145 * @param info Information about the sent event. 146 */ 147 void (*sent)(struct bt_le_ext_adv *adv, 148 struct bt_le_ext_adv_sent_info *info); 149 150 /** 151 * @brief The advertising set has accepted a new connection. 152 * 153 * This callback notifies the application that the advertising set has 154 * accepted a new connection. 155 * 156 * @param adv The advertising set object. 157 * @param info Information about the connected event. 158 */ 159 void (*connected)(struct bt_le_ext_adv *adv, 160 struct bt_le_ext_adv_connected_info *info); 161 162 /** 163 * @brief The advertising set has sent scan response data. 164 * 165 * This callback notifies the application that the advertising set has 166 * has received a Scan Request packet, and has sent a Scan Response 167 * packet. 168 * 169 * @param adv The advertising set object. 170 * @param addr Information about the scanned event. 171 */ 172 void (*scanned)(struct bt_le_ext_adv *adv, 173 struct bt_le_ext_adv_scanned_info *info); 174 175 #if defined(CONFIG_BT_PRIVACY) 176 /** 177 * @brief The RPA validity of the advertising set has expired. 178 * 179 * This callback notifies the application that the RPA validity of 180 * the advertising set has expired. The user can use this callback 181 * to synchronize the advertising payload update with the RPA rotation. 182 * 183 * If rpa sharing is enabled and rpa expired cb of any adv-sets belonging 184 * to same adv id returns false, then adv-sets will continue with old rpa 185 * through out the rpa rotations. 186 * 187 * @param adv The advertising set object. 188 * 189 * @return true to rotate the current RPA, or false to use it for the 190 * next rotation period. 191 */ 192 bool (*rpa_expired)(struct bt_le_ext_adv *adv); 193 #endif /* defined(CONFIG_BT_PRIVACY) */ 194 195 #if defined(CONFIG_BT_PER_ADV_RSP) 196 /** 197 * @brief The Controller indicates it is ready to transmit one or more subevent. 198 * 199 * This callback notifies the application that the controller has requested 200 * data for upcoming subevents. 201 * 202 * @param adv The advertising set object. 203 * @param request Information about the upcoming subevents. 204 */ 205 void (*pawr_data_request)(struct bt_le_ext_adv *adv, 206 const struct bt_le_per_adv_data_request *request); 207 /** 208 * @brief The Controller indicates that one or more synced devices have 209 * responded to a periodic advertising subevent indication. 210 * 211 * @param adv The advertising set object. 212 * @param info Information about the responses received. 213 * @param buf The received data. NULL if the controller reported 214 * that it did not receive any response. 215 */ 216 void (*pawr_response)(struct bt_le_ext_adv *adv, struct bt_le_per_adv_response_info *info, 217 struct net_buf_simple *buf); 218 219 #endif /* defined(CONFIG_BT_PER_ADV_RSP) */ 220 }; 221 222 /** 223 * @typedef bt_ready_cb_t 224 * @brief Callback for notifying that Bluetooth has been enabled. 225 * 226 * @param err zero on success or (negative) error code otherwise. 227 */ 228 typedef void (*bt_ready_cb_t)(int err); 229 230 /** 231 * @brief Enable Bluetooth 232 * 233 * Enable Bluetooth. Must be the called before any calls that 234 * require communication with the local Bluetooth hardware. 235 * 236 * When @kconfig{CONFIG_BT_SETTINGS} is enabled, the application must load the 237 * Bluetooth settings after this API call successfully completes before 238 * Bluetooth APIs can be used. Loading the settings before calling this function 239 * is insufficient. Bluetooth settings can be loaded with settings_load() or 240 * settings_load_subtree() with argument "bt". The latter selectively loads only 241 * Bluetooth settings and is recommended if settings_load() has been called 242 * earlier. 243 * 244 * @param cb Callback to notify completion or NULL to perform the 245 * enabling synchronously. The callback is called from the system workqueue. 246 * 247 * @return Zero on success or (negative) error code otherwise. 248 */ 249 int bt_enable(bt_ready_cb_t cb); 250 251 /** 252 * @brief Disable Bluetooth 253 * 254 * Disable Bluetooth. Can't be called before bt_enable has completed. 255 * 256 * This API will clear all configured identities and keys that are not persistently 257 * stored with @kconfig{CONFIG_BT_SETTINGS}. These can be restored 258 * with settings_load() before reenabling the stack. 259 * 260 * This API does _not_ clear previously registered callbacks 261 * like @ref bt_le_scan_cb_register, @ref bt_conn_cb_register 262 * AND @ref bt_br_discovery_cb_register. 263 * That is, the application shall not re-register them when 264 * the Bluetooth subsystem is re-enabled later. 265 * 266 * Close and release HCI resources. Result is architecture dependent. 267 * 268 * @return Zero on success or (negative) error code otherwise. 269 */ 270 int bt_disable(void); 271 272 /** 273 * @brief Check if Bluetooth is ready 274 * 275 * @return true when Bluetooth is ready, false otherwise 276 */ 277 bool bt_is_ready(void); 278 279 /** 280 * @brief Set Bluetooth Device Name 281 * 282 * Set Bluetooth GAP Device Name. 283 * 284 * When advertising with device name in the advertising data the name should 285 * be updated by calling @ref bt_le_adv_update_data or 286 * @ref bt_le_ext_adv_set_data. 287 * 288 * @note Requires @kconfig{CONFIG_BT_DEVICE_NAME_DYNAMIC}. 289 * 290 * @sa @kconfig{CONFIG_BT_DEVICE_NAME_MAX}. 291 * 292 * @param name New name, must be null terminated 293 * 294 * @return Zero on success or (negative) error code otherwise. 295 */ 296 int bt_set_name(const char *name); 297 298 /** 299 * @brief Get Bluetooth Device Name 300 * 301 * Get Bluetooth GAP Device Name. 302 * 303 * @return Bluetooth Device Name 304 */ 305 const char *bt_get_name(void); 306 307 /** 308 * @brief Get local Bluetooth appearance 309 * 310 * Bluetooth Appearance is a description of the external appearance of a device 311 * in terms of an Appearance Value. 312 * 313 * @see https://specificationrefs.bluetooth.com/assigned-values/Appearance%20Values.pdf 314 * 315 * @returns Appearance Value of local Bluetooth host. 316 */ 317 uint16_t bt_get_appearance(void); 318 319 /** 320 * @brief Set local Bluetooth appearance 321 * 322 * Automatically preserves the new appearance across reboots if 323 * @kconfig{CONFIG_BT_SETTINGS} is enabled. 324 * 325 * This symbol is linkable if @kconfig{CONFIG_BT_DEVICE_APPEARANCE_DYNAMIC} is 326 * enabled. 327 * 328 * @param new_appearance Appearance Value 329 * 330 * @retval 0 Success. 331 * @retval other Persistent storage failed. Appearance was not updated. 332 */ 333 int bt_set_appearance(uint16_t new_appearance); 334 335 /** 336 * @brief Get the currently configured identities. 337 * 338 * Returns an array of the currently configured identity addresses. To 339 * make sure all available identities can be retrieved, the number of 340 * elements in the @a addrs array should be CONFIG_BT_ID_MAX. The identity 341 * identifier that some APIs expect (such as advertising parameters) is 342 * simply the index of the identity in the @a addrs array. 343 * 344 * If @a addrs is passed as NULL, then returned @a count contains the 345 * count of all available identities that can be retrieved with a 346 * subsequent call to this function with non-NULL @a addrs parameter. 347 * 348 * @note Deleted identities may show up as @ref BT_ADDR_LE_ANY in the returned 349 * array. 350 * 351 * @param addrs Array where to store the configured identities. 352 * @param count Should be initialized to the array size. Once the function 353 * returns it will contain the number of returned identities. 354 */ 355 void bt_id_get(bt_addr_le_t *addrs, size_t *count); 356 357 /** 358 * @brief Create a new identity. 359 * 360 * Create a new identity using the given address and IRK. This function can be 361 * called before calling bt_enable(). However, the new identity will only be 362 * stored persistently in flash when this API is used after bt_enable(). The 363 * reason is that the persistent settings are loaded after bt_enable() and would 364 * therefore cause potential conflicts with the stack blindly overwriting what's 365 * stored in flash. The identity will also not be written to flash in case a 366 * pre-defined address is provided, since in such a situation the app clearly 367 * has some place it got the address from and will be able to repeat the 368 * procedure on every power cycle, i.e. it would be redundant to also store the 369 * information in flash. 370 * 371 * Generating random static address or random IRK is not supported when calling 372 * this function before bt_enable(). 373 * 374 * If the application wants to have the stack randomly generate identities 375 * and store them in flash for later recovery, the way to do it would be 376 * to first initialize the stack (using bt_enable), then call settings_load(), 377 * and after that check with bt_id_get() how many identities were recovered. 378 * If an insufficient amount of identities were recovered the app may then 379 * call bt_id_create() to create new ones. 380 * 381 * If supported by the HCI driver (indicated by setting 382 * @kconfig{CONFIG_BT_HCI_SET_PUBLIC_ADDR}), the first call to this function can be 383 * used to set the controller's public identity address. This call must happen 384 * before calling bt_enable(). Subsequent calls always add/generate random 385 * static addresses. 386 * 387 * @param addr Address to use for the new identity. If NULL or initialized 388 * to BT_ADDR_LE_ANY the stack will generate a new random 389 * static address for the identity and copy it to the given 390 * parameter upon return from this function (in case the 391 * parameter was non-NULL). 392 * @param irk Identity Resolving Key (16 bytes) to be used with this 393 * identity. If set to all zeroes or NULL, the stack will 394 * generate a random IRK for the identity and copy it back 395 * to the parameter upon return from this function (in case 396 * the parameter was non-NULL). If privacy 397 * @kconfig{CONFIG_BT_PRIVACY} is not enabled this parameter must 398 * be NULL. 399 * 400 * @return Identity identifier (>= 0) in case of success, or a negative 401 * error code on failure. 402 */ 403 int bt_id_create(bt_addr_le_t *addr, uint8_t *irk); 404 405 /** 406 * @brief Reset/reclaim an identity for reuse. 407 * 408 * The semantics of the @a addr and @a irk parameters of this function 409 * are the same as with bt_id_create(). The difference is the first 410 * @a id parameter that needs to be an existing identity (if it doesn't 411 * exist this function will return an error). When given an existing 412 * identity this function will disconnect any connections created using it, 413 * remove any pairing keys or other data associated with it, and then create 414 * a new identity in the same slot, based on the @a addr and @a irk 415 * parameters. 416 * 417 * @note the default identity (BT_ID_DEFAULT) cannot be reset, i.e. this 418 * API will return an error if asked to do that. 419 * 420 * @param id Existing identity identifier. 421 * @param addr Address to use for the new identity. If NULL or initialized 422 * to BT_ADDR_LE_ANY the stack will generate a new static 423 * random address for the identity and copy it to the given 424 * parameter upon return from this function (in case the 425 * parameter was non-NULL). 426 * @param irk Identity Resolving Key (16 bytes) to be used with this 427 * identity. If set to all zeroes or NULL, the stack will 428 * generate a random IRK for the identity and copy it back 429 * to the parameter upon return from this function (in case 430 * the parameter was non-NULL). If privacy 431 * @kconfig{CONFIG_BT_PRIVACY} is not enabled this parameter must 432 * be NULL. 433 * 434 * @return Identity identifier (>= 0) in case of success, or a negative 435 * error code on failure. 436 */ 437 int bt_id_reset(uint8_t id, bt_addr_le_t *addr, uint8_t *irk); 438 439 /** 440 * @brief Delete an identity. 441 * 442 * When given a valid identity this function will disconnect any connections 443 * created using it, remove any pairing keys or other data associated with 444 * it, and then flag is as deleted, so that it can not be used for any 445 * operations. To take back into use the slot the identity was occupying the 446 * bt_id_reset() API needs to be used. 447 * 448 * @note the default identity (BT_ID_DEFAULT) cannot be deleted, i.e. this 449 * API will return an error if asked to do that. 450 * 451 * @param id Existing identity identifier. 452 * 453 * @return 0 in case of success, or a negative error code on failure. 454 */ 455 int bt_id_delete(uint8_t id); 456 457 /** 458 * @brief Bluetooth data serialized size. 459 * 460 * Get the size of a serialized @ref bt_data given its data length. 461 * 462 * Size of 'AD Structure'->'Length' field, equal to 1. 463 * Size of 'AD Structure'->'Data'->'AD Type' field, equal to 1. 464 * Size of 'AD Structure'->'Data'->'AD Data' field, equal to data_len. 465 * 466 * See Core Specification Version 5.4 Vol. 3 Part C, 11, Figure 11.1. 467 */ 468 #define BT_DATA_SERIALIZED_SIZE(data_len) ((data_len) + 2) 469 470 /** 471 * @brief Bluetooth data. 472 * 473 * Description of different data types that can be encoded into 474 * advertising data. Used to form arrays that are passed to the 475 * bt_le_adv_start() function. 476 */ 477 struct bt_data { 478 uint8_t type; 479 uint8_t data_len; 480 const uint8_t *data; 481 }; 482 483 /** 484 * @brief Helper to declare elements of bt_data arrays 485 * 486 * This macro is mainly for creating an array of struct bt_data 487 * elements which is then passed to e.g. @ref bt_le_adv_start(). 488 * 489 * @param _type Type of advertising data field 490 * @param _data Pointer to the data field payload 491 * @param _data_len Number of bytes behind the _data pointer 492 */ 493 #define BT_DATA(_type, _data, _data_len) \ 494 { \ 495 .type = (_type), \ 496 .data_len = (_data_len), \ 497 .data = (const uint8_t *)(_data), \ 498 } 499 500 /** 501 * @brief Helper to declare elements of bt_data arrays 502 * 503 * This macro is mainly for creating an array of struct bt_data 504 * elements which is then passed to e.g. @ref bt_le_adv_start(). 505 * 506 * @param _type Type of advertising data field 507 * @param _bytes Variable number of single-byte parameters 508 */ 509 #define BT_DATA_BYTES(_type, _bytes...) \ 510 BT_DATA(_type, ((uint8_t []) { _bytes }), \ 511 sizeof((uint8_t []) { _bytes })) 512 513 /** 514 * @brief Get the total size (in bytes) of a given set of @ref bt_data 515 * structures. 516 * 517 * @param[in] data Array of @ref bt_data structures. 518 * @param[in] data_count Number of @ref bt_data structures in @p data. 519 * 520 * @return Size of the concatenated data, built from the @ref bt_data structure 521 * set. 522 */ 523 size_t bt_data_get_len(const struct bt_data data[], size_t data_count); 524 525 /** 526 * @brief Serialize a @ref bt_data struct into an advertising structure (a flat 527 * byte array). 528 * 529 * The data are formatted according to the Bluetooth Core Specification v. 5.4, 530 * vol. 3, part C, 11. 531 * 532 * @param[in] input Single @ref bt_data structure to read from. 533 * @param[out] output Buffer large enough to store the advertising structure in 534 * @p input. The size of it must be at least the size of the 535 * `input->data_len + 2` (for the type and the length). 536 * 537 * @return Number of bytes written in @p output. 538 */ 539 size_t bt_data_serialize(const struct bt_data *input, uint8_t *output); 540 541 struct bt_le_local_features { 542 /** 543 * @brief Local LE controller supported features. 544 * 545 * Refer to BT_LE_FEAT_BIT_* for values. 546 * Refer to the BT_FEAT_LE_* macros for value comparionson. 547 * See Bluetooth Core Specification, Vol 6, Part B, Section 4.6. 548 */ 549 uint8_t features[BT_LE_LOCAL_SUPPORTED_FEATURES_SIZE]; 550 551 /** 552 * @brief Local LE controller supported states 553 * 554 * Refer to BT_LE_STATES_* for values. 555 * See Bluetooth Core Specification 6.0, Vol 4, Part E, Section 7.8.27 556 */ 557 uint64_t states; 558 559 /** 560 * @brief ACL data packet length 561 * 562 * This represents the maximum ACL HCI Data packet which can be sent from the Host to the 563 * Controller. 564 * The Host may support L2CAP and ATT MTUs larger than this value. 565 * See Bluetooth Core Specification, Vol 6, Part E, Section 7.8.2. 566 */ 567 uint16_t acl_mtu; 568 /** Total number of ACL data packets */ 569 uint8_t acl_pkts; 570 571 /** 572 * @brief ISO data packet length 573 * 574 * This represents the maximum ISO HCI Data packet which can be sent from the Host to the 575 * Controller. 576 * ISO SDUs above this size can be fragmented assuming that the number of 577 * @ref bt_le_local_features.iso_pkts support the maximum size. 578 */ 579 uint16_t iso_mtu; 580 /** Total number of ISO data packets */ 581 uint8_t iso_pkts; 582 583 /** 584 * @brief Maximum size of the controller resolving list. 585 * 586 * See Bluetooth Core Specification, Vol 6, Part E, Section 7.8.41. 587 */ 588 uint8_t rl_size; 589 590 /** 591 * @brief Maximum advertising data length 592 * 593 * @note The maximum advertising data length also depends on advertising type. 594 * 595 * See Bluetooth Core Specification, Vol 6, Part E, Section 7.8.57. 596 */ 597 uint16_t max_adv_data_len; 598 }; 599 600 /** 601 * @brief Get local Bluetooth LE controller features 602 * 603 * Can only be called after bt_enable() 604 * 605 * @param local_features Local features struct to be populated with information. 606 * 607 * @retval 0 Success 608 * @retval -EAGAIN The information is not yet available. 609 * @retval -EINVAL @p local_features is NULL. 610 */ 611 int bt_le_get_local_features(struct bt_le_local_features *local_features); 612 613 /** Advertising options */ 614 enum { 615 /** Convenience value when no options are specified. */ 616 BT_LE_ADV_OPT_NONE = 0, 617 618 /** 619 * @brief Advertise as connectable. 620 * 621 * @deprecated Use @ref BT_LE_ADV_OPT_CONN instead. 622 * 623 * Advertise as connectable. If not connectable then the type of 624 * advertising is determined by providing scan response data. 625 * The advertiser address is determined by the type of advertising 626 * and/or enabling privacy @kconfig{CONFIG_BT_PRIVACY}. 627 * 628 * Starting connectable advertising preallocates a connection 629 * object. If this fails, the API returns @c -ENOMEM. 630 * 631 * When an advertiser set results in a connection creation, the 632 * controller automatically disables that advertising set. 633 * 634 * If the advertising set was started with @ref bt_le_adv_start 635 * without @ref BT_LE_ADV_OPT_ONE_TIME, the host will attempt to 636 * resume the advertiser under some conditions. 637 */ 638 BT_LE_ADV_OPT_CONNECTABLE __deprecated = BIT(0), 639 640 /** 641 * @internal 642 * 643 * Internal access to the deprecated value to maintain the 644 * implementation of the deprecated feature. 645 * 646 * At the end of the deprecation period, ABI will change so 647 * `BT_LE_ADV_OPT_CONN` is just `BIT(0)`, removing the need for this 648 * symbol. 649 */ 650 _BT_LE_ADV_OPT_CONNECTABLE = BIT(0), 651 652 /** 653 * @brief Advertise one time. 654 * 655 * @deprecated Use @ref BT_LE_ADV_OPT_CONN instead. 656 * 657 * Don't try to resume connectable advertising after a connection. 658 * This option is only meaningful when used together with 659 * BT_LE_ADV_OPT_CONNECTABLE. If set the advertising will be stopped 660 * when bt_le_adv_stop() is called or when an incoming (peripheral) 661 * connection happens. If this option is not set the stack will 662 * take care of keeping advertising enabled even as connections 663 * occur. 664 * If Advertising directed or the advertiser was started with 665 * @ref bt_le_ext_adv_start then this behavior is the default behavior 666 * and this flag has no effect. 667 */ 668 BT_LE_ADV_OPT_ONE_TIME __deprecated = BIT(1), 669 670 /** 671 * @internal 672 * 673 * Internal access to the deprecated value to maintain 674 * the implementation of the deprecated feature. 675 */ 676 _BT_LE_ADV_OPT_ONE_TIME = BIT(1), 677 678 /** 679 * @brief Connectable advertising 680 * 681 * Starting connectable advertising preallocates a connection 682 * object. If this fails, the API returns @c -ENOMEM. 683 * 684 * The advertising set stops immediately after it creates a 685 * connection. This happens automatically in the controller. 686 * 687 * @note To continue advertising after a connection is created, 688 * the application should listen for the @ref bt_conn_cb.connected 689 * event and start the advertising set again. Note that the 690 * advertiser cannot be started when all connection objects are 691 * in use. In that case, defer starting the advertiser until 692 * @ref bt_conn_cb.recycled. To continue after a disconnection, 693 * listen for @ref bt_conn_cb.recycled. 694 695 */ 696 BT_LE_ADV_OPT_CONN = BIT(0) | BIT(1), 697 698 /** 699 * @brief Advertise using identity address. 700 * 701 * Advertise using the identity address as the advertiser address. 702 * @warning This will compromise the privacy of the device, so care 703 * must be taken when using this option. 704 * @note The address used for advertising will not be the same as 705 * returned by @ref bt_le_oob_get_local, instead @ref bt_id_get 706 * should be used to get the LE address. 707 */ 708 BT_LE_ADV_OPT_USE_IDENTITY = BIT(2), 709 710 /** 711 * @deprecated This option will be removed in the near future, see 712 * https://github.com/zephyrproject-rtos/zephyr/issues/71686 713 * 714 * @brief Advertise using GAP device name. 715 * 716 * Include the GAP device name automatically when advertising. 717 * By default the GAP device name is put at the end of the scan 718 * response data. 719 * When advertising using @ref BT_LE_ADV_OPT_EXT_ADV and not 720 * @ref BT_LE_ADV_OPT_SCANNABLE then it will be put at the end of the 721 * advertising data. 722 * If the GAP device name does not fit into advertising data it will be 723 * converted to a shortened name if possible. 724 * @ref BT_LE_ADV_OPT_FORCE_NAME_IN_AD can be used to force the device 725 * name to appear in the advertising data of an advert with scan 726 * response data. 727 * 728 * The application can set the device name itself by including the 729 * following in the advertising data. 730 * @code 731 * BT_DATA(BT_DATA_NAME_COMPLETE, name, sizeof(name) - 1) 732 * @endcode 733 */ 734 BT_LE_ADV_OPT_USE_NAME = BIT(3), 735 736 /** 737 * @brief Low duty cycle directed advertising. 738 * 739 * Use low duty directed advertising mode, otherwise high duty mode 740 * will be used. 741 */ 742 BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY = BIT(4), 743 744 /** 745 * @brief Directed advertising to privacy-enabled peer. 746 * 747 * Enable use of Resolvable Private Address (RPA) as the target address 748 * in directed advertisements. 749 * This is required if the remote device is privacy-enabled and 750 * supports address resolution of the target address in directed 751 * advertisement. 752 * It is the responsibility of the application to check that the remote 753 * device supports address resolution of directed advertisements by 754 * reading its Central Address Resolution characteristic. 755 */ 756 BT_LE_ADV_OPT_DIR_ADDR_RPA = BIT(5), 757 758 /** Use filter accept list to filter devices that can request scan 759 * response data. 760 */ 761 BT_LE_ADV_OPT_FILTER_SCAN_REQ = BIT(6), 762 763 /** Use filter accept list to filter devices that can connect. */ 764 BT_LE_ADV_OPT_FILTER_CONN = BIT(7), 765 766 /** Notify the application when a scan response data has been sent to an 767 * active scanner. 768 */ 769 BT_LE_ADV_OPT_NOTIFY_SCAN_REQ = BIT(8), 770 771 /** 772 * @brief Support scan response data. 773 * 774 * When used together with @ref BT_LE_ADV_OPT_EXT_ADV then this option 775 * cannot be used together with the @ref BT_LE_ADV_OPT_CONN option. 776 * When used together with @ref BT_LE_ADV_OPT_EXT_ADV then scan 777 * response data must be set. 778 */ 779 BT_LE_ADV_OPT_SCANNABLE = BIT(9), 780 781 /** 782 * @brief Advertise with extended advertising. 783 * 784 * This options enables extended advertising in the advertising set. 785 * In extended advertising the advertising set will send a small header 786 * packet on the three primary advertising channels. This small header 787 * points to the advertising data packet that will be sent on one of 788 * the 37 secondary advertising channels. 789 * The advertiser will send primary advertising on LE 1M PHY, and 790 * secondary advertising on LE 2M PHY. 791 * Connections will be established on LE 2M PHY. 792 * 793 * Without this option the advertiser will send advertising data on the 794 * three primary advertising channels. 795 * 796 * @note Enabling this option requires extended advertising support in 797 * the peer devices scanning for advertisement packets. 798 * 799 * @note This cannot be used with bt_le_adv_start(). 800 */ 801 BT_LE_ADV_OPT_EXT_ADV = BIT(10), 802 803 /** 804 * @brief Disable use of LE 2M PHY on the secondary advertising 805 * channel. 806 * 807 * Disabling the use of LE 2M PHY could be necessary if scanners don't 808 * support the LE 2M PHY. 809 * The advertiser will send primary advertising on LE 1M PHY, and 810 * secondary advertising on LE 1M PHY. 811 * Connections will be established on LE 1M PHY. 812 * 813 * @note Cannot be set if BT_LE_ADV_OPT_CODED is set. 814 * 815 * @note Requires @ref BT_LE_ADV_OPT_EXT_ADV. 816 */ 817 BT_LE_ADV_OPT_NO_2M = BIT(11), 818 819 /** 820 * @brief Advertise on the LE Coded PHY (Long Range). 821 * 822 * The advertiser will send both primary and secondary advertising 823 * on the LE Coded PHY. This gives the advertiser increased range with 824 * the trade-off of lower data rate and higher power consumption. 825 * Connections will be established on LE Coded PHY. 826 * 827 * @note Requires @ref BT_LE_ADV_OPT_EXT_ADV 828 */ 829 BT_LE_ADV_OPT_CODED = BIT(12), 830 831 /** 832 * @brief Advertise without a device address (identity or RPA). 833 * 834 * @note Requires @ref BT_LE_ADV_OPT_EXT_ADV 835 */ 836 BT_LE_ADV_OPT_ANONYMOUS = BIT(13), 837 838 /** 839 * @brief Advertise with transmit power. 840 * 841 * @note Requires @ref BT_LE_ADV_OPT_EXT_ADV 842 */ 843 BT_LE_ADV_OPT_USE_TX_POWER = BIT(14), 844 845 /** Disable advertising on channel index 37. */ 846 BT_LE_ADV_OPT_DISABLE_CHAN_37 = BIT(15), 847 848 /** Disable advertising on channel index 38. */ 849 BT_LE_ADV_OPT_DISABLE_CHAN_38 = BIT(16), 850 851 /** Disable advertising on channel index 39. */ 852 BT_LE_ADV_OPT_DISABLE_CHAN_39 = BIT(17), 853 854 /** 855 * @deprecated This option will be removed in the near future, see 856 * https://github.com/zephyrproject-rtos/zephyr/issues/71686 857 * 858 * @brief Put GAP device name into advert data 859 * 860 * Will place the GAP device name into the advertising data rather than 861 * the scan response data. 862 * 863 * @note Requires @ref BT_LE_ADV_OPT_USE_NAME 864 */ 865 BT_LE_ADV_OPT_FORCE_NAME_IN_AD = BIT(18), 866 867 /** 868 * @brief Advertise using a Non-Resolvable Private Address. 869 * 870 * A new NRPA is set when updating the advertising parameters. 871 * 872 * This is an advanced feature; most users will want to enable 873 * @kconfig{CONFIG_BT_EXT_ADV} instead. 874 * 875 * @note Not implemented when @kconfig{CONFIG_BT_PRIVACY}. 876 * 877 * @note Mutually exclusive with BT_LE_ADV_OPT_USE_IDENTITY. 878 */ 879 BT_LE_ADV_OPT_USE_NRPA = BIT(19), 880 881 /** 882 * @brief Configures the advertiser to use the S=2 coding scheme for 883 * LE Coded PHY. 884 * 885 * Sets the advertiser's required coding scheme to S=2, which is one 886 * of the coding options available for LE Coded PHY. The S=2 coding 887 * scheme offers higher data rates compared to S=8, with a trade-off 888 * of reduced range. The coding scheme will only be set if both the 889 * primary and secondary advertising channels indicate LE Coded Phy. 890 * Additionally, the Controller must support the LE Feature Advertising 891 * Coding Selection. If these conditions are not met, it will default to 892 * no required coding scheme. 893 * 894 * @note Requires @kconfig{BT_EXT_ADV_CODING_SELECTION} 895 */ 896 BT_LE_ADV_OPT_REQUIRE_S2_CODING = BIT(20), 897 898 /** 899 * @brief Configures the advertiser to use the S=8 coding scheme for 900 * LE Coded PHY. 901 * 902 * Sets the advertiser's required coding scheme to S=8, which is one 903 * of the coding options available for LE Coded PHY. The S=8 coding 904 * scheme offers increased range compared to S=2, with a trade-off 905 * of lower data rates. The coding scheme will only be set if both the 906 * primary and secondary advertising channels indicate LE Coded Phy. 907 * Additionally, the Controller must support the LE Feature Advertising 908 * Coding Selection. If these conditions are not met, it will default to 909 * no required coding scheme. 910 * 911 * @note Requires @kconfig{BT_EXT_ADV_CODING_SELECTION} 912 */ 913 BT_LE_ADV_OPT_REQUIRE_S8_CODING = BIT(21), 914 }; 915 916 /** LE Advertising Parameters. */ 917 struct bt_le_adv_param { 918 /** 919 * @brief Local identity. 920 * 921 * @note When extended advertising @kconfig{CONFIG_BT_EXT_ADV} is not 922 * enabled or not supported by the controller it is not possible 923 * to scan and advertise simultaneously using two different 924 * random addresses. 925 */ 926 uint8_t id; 927 928 /** 929 * @brief Advertising Set Identifier, valid range 0x00 - 0x0f. 930 * 931 * @note Requires @ref BT_LE_ADV_OPT_EXT_ADV 932 **/ 933 uint8_t sid; 934 935 /** 936 * @brief Secondary channel maximum skip count. 937 * 938 * Maximum advertising events the advertiser can skip before it must 939 * send advertising data on the secondary advertising channel. 940 * 941 * @note Requires @ref BT_LE_ADV_OPT_EXT_ADV 942 */ 943 uint8_t secondary_max_skip; 944 945 /** Bit-field of advertising options */ 946 uint32_t options; 947 948 /** Minimum Advertising Interval (N * 0.625 milliseconds) 949 * Minimum Advertising Interval shall be less than or equal to the 950 * Maximum Advertising Interval. The Minimum Advertising Interval and 951 * Maximum Advertising Interval should not be the same value (as stated 952 * in Bluetooth Core Spec 5.2, section 7.8.5) 953 * Range: 0x0020 to 0x4000 954 */ 955 uint32_t interval_min; 956 957 /** Maximum Advertising Interval (N * 0.625 milliseconds) 958 * Minimum Advertising Interval shall be less than or equal to the 959 * Maximum Advertising Interval. The Minimum Advertising Interval and 960 * Maximum Advertising Interval should not be the same value (as stated 961 * in Bluetooth Core Spec 5.2, section 7.8.5) 962 * Range: 0x0020 to 0x4000 963 */ 964 uint32_t interval_max; 965 966 /** 967 * @brief Directed advertising to peer 968 * 969 * When this parameter is set the advertiser will send directed 970 * advertising to the remote device. 971 * 972 * The advertising type will either be high duty cycle, or low duty 973 * cycle if the BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY option is enabled. 974 * When using @ref BT_LE_ADV_OPT_EXT_ADV then only low duty cycle is 975 * allowed. 976 * 977 * In case of connectable high duty cycle if the connection could not 978 * be established within the timeout the connected() callback will be 979 * called with the status set to @ref BT_HCI_ERR_ADV_TIMEOUT. 980 */ 981 const bt_addr_le_t *peer; 982 }; 983 984 985 /** Periodic Advertising options */ 986 enum { 987 /** Convenience value when no options are specified. */ 988 BT_LE_PER_ADV_OPT_NONE = 0, 989 990 /** 991 * @brief Advertise with transmit power. 992 * 993 * @note Requires @ref BT_LE_ADV_OPT_EXT_ADV 994 */ 995 BT_LE_PER_ADV_OPT_USE_TX_POWER = BIT(1), 996 997 /** 998 * @brief Advertise with included AdvDataInfo (ADI). 999 * 1000 * @note Requires @ref BT_LE_ADV_OPT_EXT_ADV 1001 */ 1002 BT_LE_PER_ADV_OPT_INCLUDE_ADI = BIT(2), 1003 }; 1004 1005 struct bt_le_per_adv_param { 1006 /** 1007 * @brief Minimum Periodic Advertising Interval (N * 1.25 ms) 1008 * 1009 * Shall be greater or equal to BT_GAP_PER_ADV_MIN_INTERVAL and 1010 * less or equal to interval_max. 1011 */ 1012 uint16_t interval_min; 1013 1014 /** 1015 * @brief Maximum Periodic Advertising Interval (N * 1.25 ms) 1016 * 1017 * Shall be less or equal to BT_GAP_PER_ADV_MAX_INTERVAL and 1018 * greater or equal to interval_min. 1019 */ 1020 uint16_t interval_max; 1021 1022 /** Bit-field of periodic advertising options */ 1023 uint32_t options; 1024 1025 #if defined(CONFIG_BT_PER_ADV_RSP) 1026 /** 1027 * @brief Number of subevents 1028 * 1029 * If zero, the periodic advertiser will be a broadcaster, without responses. 1030 */ 1031 uint8_t num_subevents; 1032 1033 /** 1034 * @brief Interval between subevents (N * 1.25 ms) 1035 * 1036 * Shall be between 7.5ms and 318.75 ms. 1037 */ 1038 uint8_t subevent_interval; 1039 1040 /** 1041 * @brief Time between the advertising packet in a subevent and the 1042 * first response slot (N * 1.25 ms) 1043 * 1044 */ 1045 uint8_t response_slot_delay; 1046 1047 /** 1048 * @brief Time between response slots (N * 0.125 ms) 1049 * 1050 * Shall be between 0.25 and 31.875 ms. 1051 */ 1052 uint8_t response_slot_spacing; 1053 1054 /** 1055 * @brief Number of subevent response slots 1056 * 1057 * If zero, response_slot_delay and response_slot_spacing are ignored. 1058 */ 1059 uint8_t num_response_slots; 1060 #endif /* CONFIG_BT_PER_ADV_RSP */ 1061 }; 1062 1063 /** 1064 * @brief Initialize advertising parameters 1065 * 1066 * @param _options Advertising Options 1067 * @param _int_min Minimum advertising interval 1068 * @param _int_max Maximum advertising interval 1069 * @param _peer Peer address, set to NULL for undirected advertising or 1070 * address of peer for directed advertising. 1071 */ 1072 #define BT_LE_ADV_PARAM_INIT(_options, _int_min, _int_max, _peer) \ 1073 { \ 1074 .id = BT_ID_DEFAULT, \ 1075 .sid = 0, \ 1076 .secondary_max_skip = 0, \ 1077 .options = (_options), \ 1078 .interval_min = (_int_min), \ 1079 .interval_max = (_int_max), \ 1080 .peer = (_peer), \ 1081 } 1082 1083 /** 1084 * @brief Helper to declare advertising parameters inline 1085 * 1086 * @param _options Advertising Options 1087 * @param _int_min Minimum advertising interval 1088 * @param _int_max Maximum advertising interval 1089 * @param _peer Peer address, set to NULL for undirected advertising or 1090 * address of peer for directed advertising. 1091 */ 1092 #define BT_LE_ADV_PARAM(_options, _int_min, _int_max, _peer) \ 1093 ((const struct bt_le_adv_param[]) { \ 1094 BT_LE_ADV_PARAM_INIT(_options, _int_min, _int_max, _peer) \ 1095 }) 1096 1097 #define BT_LE_ADV_CONN_DIR(_peer) BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONN, 0, 0, _peer) 1098 1099 /** 1100 * @deprecated This is a convenience macro for @ref 1101 * BT_LE_ADV_OPT_CONNECTABLE, which is deprecated. Please use 1102 * @ref BT_LE_ADV_CONN_FAST_1 or @ref BT_LE_ADV_CONN_FAST_2 1103 * instead. 1104 */ 1105 #define BT_LE_ADV_CONN \ 1106 BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE, BT_GAP_ADV_FAST_INT_MIN_2, \ 1107 BT_GAP_ADV_FAST_INT_MAX_2, NULL) \ 1108 __DEPRECATED_MACRO 1109 1110 /** @brief GAP recommended connectable advertising 1111 * 1112 * This is the recommended default for when a person is likely 1113 * to be waiting the device to connect or be discovered. 1114 * 1115 * Use a longer interval to conserve battery at the cost of 1116 * responsiveness. Consider entering a lower power state with 1117 * longer intervals after a timeout. 1118 * 1119 * GAP recommends advertisers use this "when user-initiated". 1120 * The application developer decides what this means. It can by 1121 * any time the user interacts with the device, a press on a 1122 * dedicated Bluetooth wakeup button, or anything in-between. 1123 * 1124 * This is the recommended setting for limited discoverable 1125 * mode. 1126 * 1127 * See Bluetooth Core Specification: 1128 * - 3.C.A "Timers and Constants", T_GAP(adv_fast_interval1) 1129 * - 3.C.9.3.11 "Connection Establishment Timing parameters" 1130 */ 1131 #define BT_LE_ADV_CONN_FAST_1 \ 1132 BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONN, BT_GAP_ADV_FAST_INT_MIN_1, BT_GAP_ADV_FAST_INT_MAX_1, \ 1133 NULL) 1134 1135 /** @brief Connectable advertising with 1136 * T_GAP(adv_fast_interval2) 1137 * 1138 * The advertising interval corresponds to what was offered as 1139 * `BT_LE_ADV_CONN` in Zephyr 3.6 and earlier, but unlike 1140 * `BT_LE_ADV_CONN`, the host does not automatically resume the 1141 * advertiser after it results in a connection. 1142 * 1143 * See Bluetooth Core Specification: 1144 * - 3.C.A "Timers and Constants", T_GAP(adv_fast_interval1) 1145 * - 3.C.9.3.11 "Connection Establishment Timing parameters" 1146 */ 1147 #define BT_LE_ADV_CONN_FAST_2 \ 1148 BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONN, BT_GAP_ADV_FAST_INT_MIN_2, BT_GAP_ADV_FAST_INT_MAX_2, \ 1149 NULL) 1150 1151 #define BT_LE_ADV_CONN_ONE_TIME BT_LE_ADV_CONN_FAST_2 __DEPRECATED_MACRO 1152 1153 /** 1154 * @deprecated This macro will be removed in the near future, see 1155 * https://github.com/zephyrproject-rtos/zephyr/issues/71686 1156 */ 1157 #define BT_LE_ADV_CONN_NAME BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE | \ 1158 BT_LE_ADV_OPT_USE_NAME, \ 1159 BT_GAP_ADV_FAST_INT_MIN_2, \ 1160 BT_GAP_ADV_FAST_INT_MAX_2, NULL) \ 1161 __DEPRECATED_MACRO 1162 1163 /** 1164 * @deprecated This macro will be removed in the near future, see 1165 * https://github.com/zephyrproject-rtos/zephyr/issues/71686 1166 */ 1167 #define BT_LE_ADV_CONN_NAME_AD BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE | \ 1168 BT_LE_ADV_OPT_USE_NAME | \ 1169 BT_LE_ADV_OPT_FORCE_NAME_IN_AD, \ 1170 BT_GAP_ADV_FAST_INT_MIN_2, \ 1171 BT_GAP_ADV_FAST_INT_MAX_2, NULL) \ 1172 __DEPRECATED_MACRO 1173 1174 #define BT_LE_ADV_CONN_DIR_LOW_DUTY(_peer) \ 1175 BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONN | BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY, \ 1176 BT_GAP_ADV_FAST_INT_MIN_2, BT_GAP_ADV_FAST_INT_MAX_2, _peer) 1177 1178 /** Non-connectable advertising with private address */ 1179 #define BT_LE_ADV_NCONN BT_LE_ADV_PARAM(0, BT_GAP_ADV_FAST_INT_MIN_2, \ 1180 BT_GAP_ADV_FAST_INT_MAX_2, NULL) 1181 1182 /** 1183 * @deprecated This macro will be removed in the near future, see 1184 * https://github.com/zephyrproject-rtos/zephyr/issues/71686 1185 * 1186 * Non-connectable advertising with @ref BT_LE_ADV_OPT_USE_NAME 1187 */ 1188 #define BT_LE_ADV_NCONN_NAME BT_LE_ADV_PARAM(BT_LE_ADV_OPT_USE_NAME, \ 1189 BT_GAP_ADV_FAST_INT_MIN_2, \ 1190 BT_GAP_ADV_FAST_INT_MAX_2, NULL) \ 1191 __DEPRECATED_MACRO 1192 1193 /** Non-connectable advertising with @ref BT_LE_ADV_OPT_USE_IDENTITY */ 1194 #define BT_LE_ADV_NCONN_IDENTITY BT_LE_ADV_PARAM(BT_LE_ADV_OPT_USE_IDENTITY, \ 1195 BT_GAP_ADV_FAST_INT_MIN_2, \ 1196 BT_GAP_ADV_FAST_INT_MAX_2, \ 1197 NULL) 1198 1199 /** Connectable extended advertising */ 1200 #define BT_LE_EXT_ADV_CONN \ 1201 BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV | BT_LE_ADV_OPT_CONN, BT_GAP_ADV_FAST_INT_MIN_2, \ 1202 BT_GAP_ADV_FAST_INT_MAX_2, NULL) 1203 1204 /** 1205 * @deprecated This macro will be removed in the near future, see 1206 * https://github.com/zephyrproject-rtos/zephyr/issues/71686 1207 * 1208 * Connectable extended advertising with @ref BT_LE_ADV_OPT_USE_NAME 1209 */ 1210 #define BT_LE_EXT_ADV_CONN_NAME BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV | \ 1211 BT_LE_ADV_OPT_CONNECTABLE | \ 1212 BT_LE_ADV_OPT_USE_NAME, \ 1213 BT_GAP_ADV_FAST_INT_MIN_2, \ 1214 BT_GAP_ADV_FAST_INT_MAX_2, \ 1215 NULL) \ 1216 __DEPRECATED_MACRO 1217 1218 /** Scannable extended advertising */ 1219 #define BT_LE_EXT_ADV_SCAN BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV | \ 1220 BT_LE_ADV_OPT_SCANNABLE, \ 1221 BT_GAP_ADV_FAST_INT_MIN_2, \ 1222 BT_GAP_ADV_FAST_INT_MAX_2, \ 1223 NULL) 1224 1225 /** 1226 * @deprecated This macro will be removed in the near future, see 1227 * https://github.com/zephyrproject-rtos/zephyr/issues/71686 1228 * 1229 * Scannable extended advertising with @ref BT_LE_ADV_OPT_USE_NAME 1230 */ 1231 #define BT_LE_EXT_ADV_SCAN_NAME BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV | \ 1232 BT_LE_ADV_OPT_SCANNABLE | \ 1233 BT_LE_ADV_OPT_USE_NAME, \ 1234 BT_GAP_ADV_FAST_INT_MIN_2, \ 1235 BT_GAP_ADV_FAST_INT_MAX_2, \ 1236 NULL) \ 1237 __DEPRECATED_MACRO 1238 1239 /** Non-connectable extended advertising with private address */ 1240 #define BT_LE_EXT_ADV_NCONN BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV, \ 1241 BT_GAP_ADV_FAST_INT_MIN_2, \ 1242 BT_GAP_ADV_FAST_INT_MAX_2, NULL) 1243 1244 /** 1245 * @deprecated This macro will be removed in the near future, see 1246 * https://github.com/zephyrproject-rtos/zephyr/issues/71686 1247 * 1248 * Non-connectable extended advertising with @ref BT_LE_ADV_OPT_USE_NAME 1249 */ 1250 #define BT_LE_EXT_ADV_NCONN_NAME BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV | \ 1251 BT_LE_ADV_OPT_USE_NAME, \ 1252 BT_GAP_ADV_FAST_INT_MIN_2, \ 1253 BT_GAP_ADV_FAST_INT_MAX_2, \ 1254 NULL) \ 1255 __DEPRECATED_MACRO 1256 1257 /** Non-connectable extended advertising with @ref BT_LE_ADV_OPT_USE_IDENTITY */ 1258 #define BT_LE_EXT_ADV_NCONN_IDENTITY \ 1259 BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV | \ 1260 BT_LE_ADV_OPT_USE_IDENTITY, \ 1261 BT_GAP_ADV_FAST_INT_MIN_2, \ 1262 BT_GAP_ADV_FAST_INT_MAX_2, NULL) 1263 1264 /** Non-connectable extended advertising on coded PHY with private address */ 1265 #define BT_LE_EXT_ADV_CODED_NCONN BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV | \ 1266 BT_LE_ADV_OPT_CODED, \ 1267 BT_GAP_ADV_FAST_INT_MIN_2, \ 1268 BT_GAP_ADV_FAST_INT_MAX_2, \ 1269 NULL) 1270 1271 /** 1272 * @deprecated This macro will be removed in the near future, see 1273 * https://github.com/zephyrproject-rtos/zephyr/issues/71686 1274 * 1275 * Non-connectable extended advertising on coded PHY with 1276 * @ref BT_LE_ADV_OPT_USE_NAME 1277 */ 1278 #define BT_LE_EXT_ADV_CODED_NCONN_NAME \ 1279 BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV | BT_LE_ADV_OPT_CODED | \ 1280 BT_LE_ADV_OPT_USE_NAME, \ 1281 BT_GAP_ADV_FAST_INT_MIN_2, \ 1282 BT_GAP_ADV_FAST_INT_MAX_2, NULL) \ 1283 __DEPRECATED_MACRO 1284 1285 /** Non-connectable extended advertising on coded PHY with 1286 * @ref BT_LE_ADV_OPT_USE_IDENTITY 1287 */ 1288 #define BT_LE_EXT_ADV_CODED_NCONN_IDENTITY \ 1289 BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV | BT_LE_ADV_OPT_CODED | \ 1290 BT_LE_ADV_OPT_USE_IDENTITY, \ 1291 BT_GAP_ADV_FAST_INT_MIN_2, \ 1292 BT_GAP_ADV_FAST_INT_MAX_2, NULL) 1293 1294 /** 1295 * Helper to initialize extended advertising start parameters inline 1296 * 1297 * @param _timeout Advertiser timeout 1298 * @param _n_evts Number of advertising events 1299 */ 1300 #define BT_LE_EXT_ADV_START_PARAM_INIT(_timeout, _n_evts) \ 1301 { \ 1302 .timeout = (_timeout), \ 1303 .num_events = (_n_evts), \ 1304 } 1305 1306 /** 1307 * Helper to declare extended advertising start parameters inline 1308 * 1309 * @param _timeout Advertiser timeout 1310 * @param _n_evts Number of advertising events 1311 */ 1312 #define BT_LE_EXT_ADV_START_PARAM(_timeout, _n_evts) \ 1313 ((const struct bt_le_ext_adv_start_param[]) { \ 1314 BT_LE_EXT_ADV_START_PARAM_INIT((_timeout), (_n_evts)) \ 1315 }) 1316 1317 #define BT_LE_EXT_ADV_START_DEFAULT BT_LE_EXT_ADV_START_PARAM(0, 0) 1318 1319 /** 1320 * Helper to declare periodic advertising parameters inline 1321 * 1322 * @param _int_min Minimum periodic advertising interval 1323 * @param _int_max Maximum periodic advertising interval 1324 * @param _options Periodic advertising properties bitfield. 1325 */ 1326 #define BT_LE_PER_ADV_PARAM_INIT(_int_min, _int_max, _options) \ 1327 { \ 1328 .interval_min = (_int_min), \ 1329 .interval_max = (_int_max), \ 1330 .options = (_options), \ 1331 } 1332 1333 /** 1334 * Helper to declare periodic advertising parameters inline 1335 * 1336 * @param _int_min Minimum periodic advertising interval 1337 * @param _int_max Maximum periodic advertising interval 1338 * @param _options Periodic advertising properties bitfield. 1339 */ 1340 #define BT_LE_PER_ADV_PARAM(_int_min, _int_max, _options) \ 1341 ((struct bt_le_per_adv_param[]) { \ 1342 BT_LE_PER_ADV_PARAM_INIT(_int_min, _int_max, _options) \ 1343 }) 1344 1345 #define BT_LE_PER_ADV_DEFAULT BT_LE_PER_ADV_PARAM(BT_GAP_PER_ADV_SLOW_INT_MIN, \ 1346 BT_GAP_PER_ADV_SLOW_INT_MAX, \ 1347 BT_LE_PER_ADV_OPT_NONE) 1348 1349 /** 1350 * @brief Start advertising 1351 * 1352 * Set advertisement data, scan response data, advertisement parameters 1353 * and start advertising. 1354 * 1355 * When the advertisement parameter peer address has been set the advertising 1356 * will be directed to the peer. In this case advertisement data and scan 1357 * response data parameters are ignored. If the mode is high duty cycle 1358 * the timeout will be @ref BT_GAP_ADV_HIGH_DUTY_CYCLE_MAX_TIMEOUT. 1359 * 1360 * This function cannot be used with @ref BT_LE_ADV_OPT_EXT_ADV in the @p param.options. 1361 * For extended advertising, the bt_le_ext_adv_* functions must be used. 1362 * 1363 * @param param Advertising parameters. 1364 * @param ad Data to be used in advertisement packets. 1365 * @param ad_len Number of elements in ad 1366 * @param sd Data to be used in scan response packets. 1367 * @param sd_len Number of elements in sd 1368 * 1369 * @return Zero on success or (negative) error code otherwise. 1370 * @return -ENOMEM No free connection objects available for connectable 1371 * advertiser. 1372 * @return -ECONNREFUSED When connectable advertising is requested and there 1373 * is already maximum number of connections established 1374 * in the controller. 1375 * This error code is only guaranteed when using Zephyr 1376 * controller, for other controllers code returned in 1377 * this case may be -EIO. 1378 */ 1379 int bt_le_adv_start(const struct bt_le_adv_param *param, 1380 const struct bt_data *ad, size_t ad_len, 1381 const struct bt_data *sd, size_t sd_len); 1382 1383 /** 1384 * @brief Update advertising 1385 * 1386 * Update advertisement and scan response data. 1387 * 1388 * @param ad Data to be used in advertisement packets. 1389 * @param ad_len Number of elements in ad 1390 * @param sd Data to be used in scan response packets. 1391 * @param sd_len Number of elements in sd 1392 * 1393 * @return Zero on success or (negative) error code otherwise. 1394 */ 1395 int bt_le_adv_update_data(const struct bt_data *ad, size_t ad_len, 1396 const struct bt_data *sd, size_t sd_len); 1397 1398 /** 1399 * @brief Stop advertising 1400 * 1401 * Stops ongoing advertising. 1402 * 1403 * @return Zero on success or (negative) error code otherwise. 1404 */ 1405 int bt_le_adv_stop(void); 1406 1407 /** 1408 * @brief Create advertising set. 1409 * 1410 * Create a new advertising set and set advertising parameters. 1411 * Advertising parameters can be updated with @ref bt_le_ext_adv_update_param. 1412 * 1413 * @param[in] param Advertising parameters. 1414 * @param[in] cb Callback struct to notify about advertiser activity. Can be 1415 * NULL. Must point to valid memory during the lifetime of the 1416 * advertising set. 1417 * @param[out] adv Valid advertising set object on success. 1418 * 1419 * @return Zero on success or (negative) error code otherwise. 1420 */ 1421 int bt_le_ext_adv_create(const struct bt_le_adv_param *param, 1422 const struct bt_le_ext_adv_cb *cb, 1423 struct bt_le_ext_adv **adv); 1424 1425 struct bt_le_ext_adv_start_param { 1426 /** 1427 * @brief Advertiser timeout (N * 10 ms). 1428 * 1429 * Application will be notified by the advertiser sent callback. 1430 * Set to zero for no timeout. 1431 * 1432 * When using high duty cycle directed connectable advertising then 1433 * this parameters must be set to a non-zero value less than or equal 1434 * to the maximum of @ref BT_GAP_ADV_HIGH_DUTY_CYCLE_MAX_TIMEOUT. 1435 * 1436 * If privacy @kconfig{CONFIG_BT_PRIVACY} is enabled then the timeout 1437 * must be less than @kconfig{CONFIG_BT_RPA_TIMEOUT}. 1438 */ 1439 uint16_t timeout; 1440 /** 1441 * @brief Number of advertising events. 1442 * 1443 * Application will be notified by the advertiser sent callback. 1444 * Set to zero for no limit. 1445 */ 1446 uint8_t num_events; 1447 }; 1448 1449 /** 1450 * @brief Start advertising with the given advertising set 1451 * 1452 * If the advertiser is limited by either the timeout or number of advertising 1453 * events the application will be notified by the advertiser sent callback once 1454 * the limit is reached. 1455 * If the advertiser is limited by both the timeout and the number of 1456 * advertising events then the limit that is reached first will stop the 1457 * advertiser. 1458 * 1459 * @param adv Advertising set object. 1460 * @param param Advertise start parameters. 1461 */ 1462 int bt_le_ext_adv_start(struct bt_le_ext_adv *adv, 1463 const struct bt_le_ext_adv_start_param *param); 1464 1465 /** 1466 * @brief Stop advertising with the given advertising set 1467 * 1468 * Stop advertising with a specific advertising set. When using this function 1469 * the advertising sent callback will not be called. 1470 * 1471 * @param adv Advertising set object. 1472 * 1473 * @return Zero on success or (negative) error code otherwise. 1474 */ 1475 int bt_le_ext_adv_stop(struct bt_le_ext_adv *adv); 1476 1477 /** 1478 * @brief Set an advertising set's advertising or scan response data. 1479 * 1480 * Set advertisement data or scan response data. If the advertising set is 1481 * currently advertising then the advertising data will be updated in 1482 * subsequent advertising events. 1483 * 1484 * When both @ref BT_LE_ADV_OPT_EXT_ADV and @ref BT_LE_ADV_OPT_SCANNABLE are 1485 * enabled then advertising data is ignored. 1486 * When @ref BT_LE_ADV_OPT_SCANNABLE is not enabled then scan response data is 1487 * ignored. 1488 * 1489 * If the advertising set has been configured to send advertising data on the 1490 * primary advertising channels then the maximum data length is 1491 * @ref BT_GAP_ADV_MAX_ADV_DATA_LEN bytes. 1492 * If the advertising set has been configured for extended advertising, 1493 * then the maximum data length is defined by the controller with the maximum 1494 * possible of @ref BT_GAP_ADV_MAX_EXT_ADV_DATA_LEN bytes. 1495 * 1496 * @note Not all scanners support extended data length advertising data. 1497 * 1498 * @note When updating the advertising data while advertising the advertising 1499 * data and scan response data length must be smaller or equal to what 1500 * can be fit in a single advertising packet. Otherwise the 1501 * advertiser must be stopped. 1502 * 1503 * @param adv Advertising set object. 1504 * @param ad Data to be used in advertisement packets. 1505 * @param ad_len Number of elements in ad 1506 * @param sd Data to be used in scan response packets. 1507 * @param sd_len Number of elements in sd 1508 * 1509 * @return Zero on success or (negative) error code otherwise. 1510 */ 1511 int bt_le_ext_adv_set_data(struct bt_le_ext_adv *adv, 1512 const struct bt_data *ad, size_t ad_len, 1513 const struct bt_data *sd, size_t sd_len); 1514 1515 /** 1516 * @brief Update advertising parameters. 1517 * 1518 * Update the advertising parameters. The function will return an error if the 1519 * advertiser set is currently advertising. Stop the advertising set before 1520 * calling this function. 1521 * 1522 * @note When changing the option @ref BT_LE_ADV_OPT_USE_NAME then 1523 * @ref bt_le_ext_adv_set_data needs to be called in order to update the 1524 * advertising data and scan response data. 1525 * 1526 * @param adv Advertising set object. 1527 * @param param Advertising parameters. 1528 * 1529 * @return Zero on success or (negative) error code otherwise. 1530 */ 1531 int bt_le_ext_adv_update_param(struct bt_le_ext_adv *adv, 1532 const struct bt_le_adv_param *param); 1533 1534 /** 1535 * @brief Delete advertising set. 1536 * 1537 * Delete advertising set. This will free up the advertising set and make it 1538 * possible to create a new advertising set. 1539 * 1540 * @return Zero on success or (negative) error code otherwise. 1541 */ 1542 int bt_le_ext_adv_delete(struct bt_le_ext_adv *adv); 1543 1544 /** 1545 * @brief Get array index of an advertising set. 1546 * 1547 * This function is used to map bt_adv to index of an array of 1548 * advertising sets. The array has CONFIG_BT_EXT_ADV_MAX_ADV_SET elements. 1549 * 1550 * @param adv Advertising set. 1551 * 1552 * @return Index of the advertising set object. 1553 * The range of the returned value is 0..CONFIG_BT_EXT_ADV_MAX_ADV_SET-1 1554 */ 1555 uint8_t bt_le_ext_adv_get_index(struct bt_le_ext_adv *adv); 1556 1557 /** @brief Advertising set info structure. */ 1558 struct bt_le_ext_adv_info { 1559 /* Local identity */ 1560 uint8_t id; 1561 1562 /** Currently selected Transmit Power (dBM). */ 1563 int8_t tx_power; 1564 1565 /** Current local advertising address used. */ 1566 const bt_addr_le_t *addr; 1567 }; 1568 1569 /** 1570 * @brief Get advertising set info 1571 * 1572 * @param adv Advertising set object 1573 * @param info Advertising set info object 1574 * 1575 * @return Zero on success or (negative) error code on failure. 1576 */ 1577 int bt_le_ext_adv_get_info(const struct bt_le_ext_adv *adv, 1578 struct bt_le_ext_adv_info *info); 1579 1580 /** 1581 * @typedef bt_le_scan_cb_t 1582 * @brief Callback type for reporting LE scan results. 1583 * 1584 * A function of this type is given to the bt_le_scan_start() function 1585 * and will be called for any discovered LE device. 1586 * 1587 * @param addr Advertiser LE address and type. 1588 * @param rssi Strength of advertiser signal. 1589 * @param adv_type Type of advertising response from advertiser. 1590 * Uses the BT_GAP_ADV_TYPE_* values. 1591 * @param buf Buffer containing advertiser data. 1592 */ 1593 typedef void bt_le_scan_cb_t(const bt_addr_le_t *addr, int8_t rssi, 1594 uint8_t adv_type, struct net_buf_simple *buf); 1595 1596 /** 1597 * @brief Set or update the periodic advertising parameters. 1598 * 1599 * The periodic advertising parameters can only be set or updated on an 1600 * extended advertisement set which is neither scannable, connectable nor 1601 * anonymous. 1602 * 1603 * @param adv Advertising set object. 1604 * @param param Advertising parameters. 1605 * 1606 * @return Zero on success or (negative) error code otherwise. 1607 */ 1608 int bt_le_per_adv_set_param(struct bt_le_ext_adv *adv, 1609 const struct bt_le_per_adv_param *param); 1610 1611 /** 1612 * @brief Set or update the periodic advertising data. 1613 * 1614 * The periodic advertisement data can only be set or updated on an 1615 * extended advertisement set which is neither scannable, connectable nor 1616 * anonymous. 1617 * 1618 * @param adv Advertising set object. 1619 * @param ad Advertising data. 1620 * @param ad_len Advertising data length. 1621 * 1622 * @return Zero on success or (negative) error code otherwise. 1623 */ 1624 int bt_le_per_adv_set_data(const struct bt_le_ext_adv *adv, 1625 const struct bt_data *ad, size_t ad_len); 1626 1627 struct bt_le_per_adv_subevent_data_params { 1628 /** The subevent to set data for */ 1629 uint8_t subevent; 1630 1631 /** The first response slot to listen to */ 1632 uint8_t response_slot_start; 1633 1634 /** The number of response slots to listen to */ 1635 uint8_t response_slot_count; 1636 1637 /** The data to send */ 1638 const struct net_buf_simple *data; 1639 }; 1640 1641 /** 1642 * @brief Set the periodic advertising with response subevent data. 1643 * 1644 * Set the data for one or more subevents of a Periodic Advertising with 1645 * Responses Advertiser in reply data request. 1646 * 1647 * @pre There are @p num_subevents elements in @p params. 1648 * @pre The controller has requested data for the subevents in @p params. 1649 * 1650 * @param adv The extended advertiser the PAwR train belongs to. 1651 * @param num_subevents The number of subevents to set data for. 1652 * @param params Subevent parameters. 1653 * 1654 * @return Zero on success or (negative) error code otherwise. 1655 */ 1656 int bt_le_per_adv_set_subevent_data(const struct bt_le_ext_adv *adv, uint8_t num_subevents, 1657 const struct bt_le_per_adv_subevent_data_params *params); 1658 1659 /** 1660 * @brief Starts periodic advertising. 1661 * 1662 * Enabling the periodic advertising can be done independently of extended 1663 * advertising, but both periodic advertising and extended advertising 1664 * shall be enabled before any periodic advertising data is sent. The 1665 * periodic advertising and extended advertising can be enabled in any order. 1666 * 1667 * Once periodic advertising has been enabled, it will continue advertising 1668 * until @ref bt_le_per_adv_stop() has been called, or if the advertising set 1669 * is deleted by @ref bt_le_ext_adv_delete(). Calling @ref bt_le_ext_adv_stop() 1670 * will not stop the periodic advertising. 1671 * 1672 * @param adv Advertising set object. 1673 * 1674 * @return Zero on success or (negative) error code otherwise. 1675 */ 1676 int bt_le_per_adv_start(struct bt_le_ext_adv *adv); 1677 1678 /** 1679 * @brief Stops periodic advertising. 1680 * 1681 * Disabling the periodic advertising can be done independently of extended 1682 * advertising. Disabling periodic advertising will not disable extended 1683 * advertising. 1684 * 1685 * @param adv Advertising set object. 1686 * 1687 * @return Zero on success or (negative) error code otherwise. 1688 */ 1689 int bt_le_per_adv_stop(struct bt_le_ext_adv *adv); 1690 1691 struct bt_le_per_adv_sync_synced_info { 1692 /** Advertiser LE address and type. */ 1693 const bt_addr_le_t *addr; 1694 1695 /** Advertiser SID */ 1696 uint8_t sid; 1697 1698 /** Periodic advertising interval (N * 1.25 ms) */ 1699 uint16_t interval; 1700 1701 /** Advertiser PHY */ 1702 uint8_t phy; 1703 1704 /** True if receiving periodic advertisements, false otherwise. */ 1705 bool recv_enabled; 1706 1707 /** 1708 * @brief Service Data provided by the peer when sync is transferred 1709 * 1710 * Will always be 0 when the sync is locally created. 1711 */ 1712 uint16_t service_data; 1713 1714 /** 1715 * @brief Peer that transferred the periodic advertising sync 1716 * 1717 * Will always be 0 when the sync is locally created. 1718 * 1719 */ 1720 struct bt_conn *conn; 1721 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP) 1722 /** Number of subevents */ 1723 uint8_t num_subevents; 1724 1725 /** Subevent interval (N * 1.25 ms) */ 1726 uint8_t subevent_interval; 1727 1728 /** Response slot delay (N * 1.25 ms) */ 1729 uint8_t response_slot_delay; 1730 1731 /** Response slot spacing (N * 1.25 ms) */ 1732 uint8_t response_slot_spacing; 1733 1734 #endif /* CONFIG_BT_PER_ADV_SYNC_RSP */ 1735 }; 1736 1737 struct bt_le_per_adv_sync_term_info { 1738 /** Advertiser LE address and type. */ 1739 const bt_addr_le_t *addr; 1740 1741 /** Advertiser SID */ 1742 uint8_t sid; 1743 1744 /** Cause of periodic advertising termination */ 1745 uint8_t reason; 1746 }; 1747 1748 struct bt_le_per_adv_sync_recv_info { 1749 /** Advertiser LE address and type. */ 1750 const bt_addr_le_t *addr; 1751 1752 /** Advertiser SID */ 1753 uint8_t sid; 1754 1755 /** The TX power of the advertisement. */ 1756 int8_t tx_power; 1757 1758 /** The RSSI of the advertisement excluding any CTE. */ 1759 int8_t rssi; 1760 1761 /** The Constant Tone Extension (CTE) of the advertisement (@ref bt_df_cte_type) */ 1762 uint8_t cte_type; 1763 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP) 1764 /** The value of the event counter where the subevent indication was received. */ 1765 uint16_t periodic_event_counter; 1766 1767 /** The subevent where the subevent indication was received. */ 1768 uint8_t subevent; 1769 #endif /* CONFIG_BT_PER_ADV_SYNC_RSP */ 1770 }; 1771 1772 1773 struct bt_le_per_adv_sync_state_info { 1774 /** True if receiving periodic advertisements, false otherwise. */ 1775 bool recv_enabled; 1776 }; 1777 1778 struct bt_le_per_adv_sync_cb { 1779 /** 1780 * @brief The periodic advertising has been successfully synced. 1781 * 1782 * This callback notifies the application that the periodic advertising 1783 * set has been successfully synced, and will now start to 1784 * receive periodic advertising reports. 1785 * 1786 * @param sync The periodic advertising sync object. 1787 * @param info Information about the sync event. 1788 */ 1789 void (*synced)(struct bt_le_per_adv_sync *sync, 1790 struct bt_le_per_adv_sync_synced_info *info); 1791 1792 /** 1793 * @brief The periodic advertising sync has been terminated. 1794 * 1795 * This callback notifies the application that the periodic advertising 1796 * sync has been terminated, either by local request, remote request or 1797 * because due to missing data, e.g. by being out of range or sync. 1798 * 1799 * @param sync The periodic advertising sync object. 1800 */ 1801 void (*term)(struct bt_le_per_adv_sync *sync, 1802 const struct bt_le_per_adv_sync_term_info *info); 1803 1804 /** 1805 * @brief Periodic advertising data received. 1806 * 1807 * This callback notifies the application of an periodic advertising 1808 * report. 1809 * 1810 * @param sync The advertising set object. 1811 * @param info Information about the periodic advertising event. 1812 * @param buf Buffer containing the periodic advertising data. 1813 * NULL if the controller failed to receive a subevent 1814 * indication. Only happens if 1815 * @kconfig{CONFIG_BT_PER_ADV_SYNC_RSP} is enabled. 1816 */ 1817 void (*recv)(struct bt_le_per_adv_sync *sync, 1818 const struct bt_le_per_adv_sync_recv_info *info, 1819 struct net_buf_simple *buf); 1820 1821 /** 1822 * @brief The periodic advertising sync state has changed. 1823 * 1824 * This callback notifies the application about changes to the sync 1825 * state. Initialize sync and termination is handled by their individual 1826 * callbacks, and won't be notified here. 1827 * 1828 * @param sync The periodic advertising sync object. 1829 * @param info Information about the state change. 1830 */ 1831 void (*state_changed)(struct bt_le_per_adv_sync *sync, 1832 const struct bt_le_per_adv_sync_state_info *info); 1833 1834 /** 1835 * @brief BIGInfo advertising report received. 1836 * 1837 * This callback notifies the application of a BIGInfo advertising report. 1838 * This is received if the advertiser is broadcasting isochronous streams in a BIG. 1839 * See iso.h for more information. 1840 * 1841 * @param sync The advertising set object. 1842 * @param biginfo The BIGInfo report. 1843 */ 1844 void (*biginfo)(struct bt_le_per_adv_sync *sync, const struct bt_iso_biginfo *biginfo); 1845 1846 /** 1847 * @brief Callback for IQ samples report collected when sampling 1848 * CTE received with periodic advertising PDU. 1849 * 1850 * @param sync The periodic advertising sync object. 1851 * @param info Information about the sync event. 1852 */ 1853 void (*cte_report_cb)(struct bt_le_per_adv_sync *sync, 1854 struct bt_df_per_adv_sync_iq_samples_report const *info); 1855 1856 sys_snode_t node; 1857 }; 1858 1859 /** Periodic advertising sync options */ 1860 enum { 1861 /** Convenience value when no options are specified. */ 1862 BT_LE_PER_ADV_SYNC_OPT_NONE = 0, 1863 1864 /** 1865 * @brief Use the periodic advertising list to sync with advertiser 1866 * 1867 * When this option is set, the address and SID of the parameters 1868 * are ignored. 1869 */ 1870 BT_LE_PER_ADV_SYNC_OPT_USE_PER_ADV_LIST = BIT(0), 1871 1872 /** 1873 * @brief Disables periodic advertising reports 1874 * 1875 * No advertisement reports will be handled until enabled. 1876 */ 1877 BT_LE_PER_ADV_SYNC_OPT_REPORTING_INITIALLY_DISABLED = BIT(1), 1878 1879 /** Filter duplicate Periodic Advertising reports */ 1880 BT_LE_PER_ADV_SYNC_OPT_FILTER_DUPLICATE = BIT(2), 1881 1882 /** Sync with Angle of Arrival (AoA) constant tone extension */ 1883 BT_LE_PER_ADV_SYNC_OPT_DONT_SYNC_AOA = BIT(3), 1884 1885 /** Sync with Angle of Departure (AoD) 1 us constant tone extension */ 1886 BT_LE_PER_ADV_SYNC_OPT_DONT_SYNC_AOD_1US = BIT(4), 1887 1888 /** Sync with Angle of Departure (AoD) 2 us constant tone extension */ 1889 BT_LE_PER_ADV_SYNC_OPT_DONT_SYNC_AOD_2US = BIT(5), 1890 1891 /** Do not sync to packets without a constant tone extension */ 1892 BT_LE_PER_ADV_SYNC_OPT_SYNC_ONLY_CONST_TONE_EXT = BIT(6), 1893 }; 1894 1895 struct bt_le_per_adv_sync_param { 1896 /** 1897 * @brief Periodic Advertiser Address 1898 * 1899 * Only valid if not using the periodic advertising list 1900 * (BT_LE_PER_ADV_SYNC_OPT_USE_PER_ADV_LIST) 1901 */ 1902 bt_addr_le_t addr; 1903 1904 /** 1905 * @brief Advertiser SID 1906 * 1907 * Only valid if not using the periodic advertising list 1908 * (BT_LE_PER_ADV_SYNC_OPT_USE_PER_ADV_LIST) 1909 */ 1910 uint8_t sid; 1911 1912 /** Bit-field of periodic advertising sync options. */ 1913 uint32_t options; 1914 1915 /** 1916 * @brief Maximum event skip 1917 * 1918 * Maximum number of periodic advertising events that can be 1919 * skipped after a successful receive. 1920 * Range: 0x0000 to 0x01F3 1921 */ 1922 uint16_t skip; 1923 1924 /** 1925 * @brief Synchronization timeout (N * 10 ms) 1926 * 1927 * Synchronization timeout for the periodic advertising sync. 1928 * Range 0x000A to 0x4000 (100 ms to 163840 ms) 1929 */ 1930 uint16_t timeout; 1931 }; 1932 1933 /** 1934 * @brief Get array index of an periodic advertising sync object. 1935 * 1936 * This function is get the index of an array of periodic advertising sync 1937 * objects. The array has CONFIG_BT_PER_ADV_SYNC_MAX elements. 1938 * 1939 * @param per_adv_sync The periodic advertising sync object. 1940 * 1941 * @return Index of the periodic advertising sync object. 1942 * The range of the returned value is 0..CONFIG_BT_PER_ADV_SYNC_MAX-1 1943 */ 1944 uint8_t bt_le_per_adv_sync_get_index(struct bt_le_per_adv_sync *per_adv_sync); 1945 1946 /** 1947 * @brief Get a periodic advertising sync object from the array index. 1948 * 1949 * This function is to get the periodic advertising sync object from 1950 * the array index. 1951 * The array has CONFIG_BT_PER_ADV_SYNC_MAX elements. 1952 * 1953 * @param index The index of the periodic advertising sync object. 1954 * The range of the index value is 0..CONFIG_BT_PER_ADV_SYNC_MAX-1 1955 * 1956 * @return The periodic advertising sync object of the array index or NULL if invalid index. 1957 */ 1958 struct bt_le_per_adv_sync *bt_le_per_adv_sync_lookup_index(uint8_t index); 1959 1960 /** @brief Advertising set info structure. */ 1961 struct bt_le_per_adv_sync_info { 1962 /** Periodic Advertiser Address */ 1963 bt_addr_le_t addr; 1964 1965 /** Advertiser SID */ 1966 uint8_t sid; 1967 1968 /** Periodic advertising interval (N * 1.25 ms) */ 1969 uint16_t interval; 1970 1971 /** Advertiser PHY */ 1972 uint8_t phy; 1973 }; 1974 1975 /** 1976 * @brief Get periodic adv sync information. 1977 * 1978 * @param per_adv_sync Periodic advertising sync object. 1979 * @param info Periodic advertising sync info object 1980 * 1981 * @return Zero on success or (negative) error code on failure. 1982 */ 1983 int bt_le_per_adv_sync_get_info(struct bt_le_per_adv_sync *per_adv_sync, 1984 struct bt_le_per_adv_sync_info *info); 1985 1986 /** 1987 * @brief Look up an existing periodic advertising sync object by advertiser address. 1988 * 1989 * @param adv_addr Advertiser address. 1990 * @param sid The advertising set ID. 1991 * 1992 * @return Periodic advertising sync object or NULL if not found. 1993 */ 1994 struct bt_le_per_adv_sync *bt_le_per_adv_sync_lookup_addr(const bt_addr_le_t *adv_addr, 1995 uint8_t sid); 1996 1997 /** 1998 * @brief Create a periodic advertising sync object. 1999 * 2000 * Create a periodic advertising sync object that can try to synchronize 2001 * to periodic advertising reports from an advertiser. Scan shall either be 2002 * disabled or extended scan shall be enabled. 2003 * 2004 * This function does not timeout, and will continue to look for an advertiser until it either 2005 * finds it or bt_le_per_adv_sync_delete() is called. It is thus suggested to implement a timeout 2006 * when using this, if it is expected to find the advertiser within a reasonable timeframe. 2007 * 2008 * @param[in] param Periodic advertising sync parameters. 2009 * @param[out] out_sync Periodic advertising sync object on. 2010 * 2011 * @return Zero on success or (negative) error code otherwise. 2012 */ 2013 int bt_le_per_adv_sync_create(const struct bt_le_per_adv_sync_param *param, 2014 struct bt_le_per_adv_sync **out_sync); 2015 2016 /** 2017 * @brief Delete periodic advertising sync. 2018 * 2019 * Delete the periodic advertising sync object. Can be called regardless of the 2020 * state of the sync. If the syncing is currently syncing, the syncing is 2021 * cancelled. If the sync has been established, it is terminated. The 2022 * periodic advertising sync object will be invalidated afterwards. 2023 * 2024 * If the state of the sync object is syncing, then a new periodic advertising 2025 * sync object may not be created until the controller has finished canceling 2026 * this object. 2027 * 2028 * @param per_adv_sync The periodic advertising sync object. 2029 * 2030 * @return Zero on success or (negative) error code otherwise. 2031 */ 2032 int bt_le_per_adv_sync_delete(struct bt_le_per_adv_sync *per_adv_sync); 2033 2034 /** 2035 * @brief Register periodic advertising sync callbacks. 2036 * 2037 * Adds the callback structure to the list of callback structures for periodic 2038 * advertising syncs. 2039 * 2040 * This callback will be called for all periodic advertising sync activity, 2041 * such as synced, terminated and when data is received. 2042 * 2043 * @param cb Callback struct. Must point to memory that remains valid. 2044 * 2045 * @retval 0 Success. 2046 * @retval -EEXIST if @p cb was already registered. 2047 */ 2048 int bt_le_per_adv_sync_cb_register(struct bt_le_per_adv_sync_cb *cb); 2049 2050 /** 2051 * @brief Enables receiving periodic advertising reports for a sync. 2052 * 2053 * If the sync is already receiving the reports, -EALREADY is returned. 2054 * 2055 * @param per_adv_sync The periodic advertising sync object. 2056 * 2057 * @return Zero on success or (negative) error code otherwise. 2058 */ 2059 int bt_le_per_adv_sync_recv_enable(struct bt_le_per_adv_sync *per_adv_sync); 2060 2061 /** 2062 * @brief Disables receiving periodic advertising reports for a sync. 2063 * 2064 * If the sync report receiving is already disabled, -EALREADY is returned. 2065 * 2066 * @param per_adv_sync The periodic advertising sync object. 2067 * 2068 * @return Zero on success or (negative) error code otherwise. 2069 */ 2070 int bt_le_per_adv_sync_recv_disable(struct bt_le_per_adv_sync *per_adv_sync); 2071 2072 /** Periodic Advertising Sync Transfer options */ 2073 enum { 2074 /** Convenience value when no options are specified. */ 2075 BT_LE_PER_ADV_SYNC_TRANSFER_OPT_NONE = 0, 2076 2077 /** 2078 * @brief No Angle of Arrival (AoA) 2079 * 2080 * Do not sync with Angle of Arrival (AoA) constant tone extension 2081 **/ 2082 BT_LE_PER_ADV_SYNC_TRANSFER_OPT_SYNC_NO_AOA = BIT(0), 2083 2084 /** 2085 * @brief No Angle of Departure (AoD) 1 us 2086 * 2087 * Do not sync with Angle of Departure (AoD) 1 us 2088 * constant tone extension 2089 */ 2090 BT_LE_PER_ADV_SYNC_TRANSFER_OPT_SYNC_NO_AOD_1US = BIT(1), 2091 2092 /** 2093 * @brief No Angle of Departure (AoD) 2 2094 * 2095 * Do not sync with Angle of Departure (AoD) 2 us 2096 * constant tone extension 2097 */ 2098 BT_LE_PER_ADV_SYNC_TRANSFER_OPT_SYNC_NO_AOD_2US = BIT(2), 2099 2100 /** Only sync to packets with constant tone extension */ 2101 BT_LE_PER_ADV_SYNC_TRANSFER_OPT_SYNC_ONLY_CTE = BIT(3), 2102 2103 /** 2104 * @brief Sync to received PAST packets but don't generate sync reports 2105 * 2106 * This option must not be set at the same time as 2107 * @ref BT_LE_PER_ADV_SYNC_TRANSFER_OPT_FILTER_DUPLICATES. 2108 */ 2109 BT_LE_PER_ADV_SYNC_TRANSFER_OPT_REPORTING_INITIALLY_DISABLED = BIT(4), 2110 2111 /** 2112 * @brief Sync to received PAST packets and generate sync reports with duplicate filtering 2113 * 2114 * This option must not be set at the same time as 2115 * @ref BT_LE_PER_ADV_SYNC_TRANSFER_OPT_REPORTING_INITIALLY_DISABLED. 2116 */ 2117 BT_LE_PER_ADV_SYNC_TRANSFER_OPT_FILTER_DUPLICATES = BIT(5), 2118 }; 2119 2120 struct bt_le_per_adv_sync_transfer_param { 2121 /** 2122 * @brief Maximum event skip 2123 * 2124 * The number of periodic advertising packets that can be skipped 2125 * after a successful receive. 2126 */ 2127 uint16_t skip; 2128 2129 /** 2130 * @brief Synchronization timeout (N * 10 ms) 2131 * 2132 * Synchronization timeout for the periodic advertising sync. 2133 * Range 0x000A to 0x4000 (100 ms to 163840 ms) 2134 */ 2135 uint16_t timeout; 2136 2137 /** Periodic Advertising Sync Transfer options */ 2138 uint32_t options; 2139 }; 2140 2141 /** 2142 * @brief Transfer the periodic advertising sync information to a peer device. 2143 * 2144 * This will allow another device to quickly synchronize to the same periodic 2145 * advertising train that this device is currently synced to. 2146 * 2147 * @param per_adv_sync The periodic advertising sync to transfer. 2148 * @param conn The peer device that will receive the sync information. 2149 * @param service_data Application service data provided to the remote host. 2150 * 2151 * @return Zero on success or (negative) error code otherwise. 2152 */ 2153 int bt_le_per_adv_sync_transfer(const struct bt_le_per_adv_sync *per_adv_sync, 2154 const struct bt_conn *conn, 2155 uint16_t service_data); 2156 2157 2158 /** 2159 * @brief Transfer the information about a periodic advertising set. 2160 * 2161 * This will allow another device to quickly synchronize to periodic 2162 * advertising set from this device. 2163 * 2164 * @param adv The periodic advertising set to transfer info of. 2165 * @param conn The peer device that will receive the information. 2166 * @param service_data Application service data provided to the remote host. 2167 * 2168 * @return Zero on success or (negative) error code otherwise. 2169 */ 2170 int bt_le_per_adv_set_info_transfer(const struct bt_le_ext_adv *adv, 2171 const struct bt_conn *conn, 2172 uint16_t service_data); 2173 2174 /** 2175 * @brief Subscribe to periodic advertising sync transfers (PASTs). 2176 * 2177 * Sets the parameters and allow other devices to transfer periodic advertising 2178 * syncs. 2179 * 2180 * @param conn The connection to set the parameters for. If NULL default 2181 * parameters for all connections will be set. Parameters set 2182 * for specific connection will always have precedence. 2183 * @param param The periodic advertising sync transfer parameters. 2184 * 2185 * @return Zero on success or (negative) error code otherwise. 2186 */ 2187 int bt_le_per_adv_sync_transfer_subscribe( 2188 const struct bt_conn *conn, 2189 const struct bt_le_per_adv_sync_transfer_param *param); 2190 2191 /** 2192 * @brief Unsubscribe from periodic advertising sync transfers (PASTs). 2193 * 2194 * Remove the parameters that allow other devices to transfer periodic 2195 * advertising syncs. 2196 * 2197 * @param conn The connection to remove the parameters for. If NULL default 2198 * parameters for all connections will be removed. Unsubscribing 2199 * for a specific device, will still allow other devices to 2200 * transfer periodic advertising syncs. 2201 * 2202 * @return Zero on success or (negative) error code otherwise. 2203 */ 2204 int bt_le_per_adv_sync_transfer_unsubscribe(const struct bt_conn *conn); 2205 2206 /** 2207 * @brief Add a device to the periodic advertising list. 2208 * 2209 * Add peer device LE address to the periodic advertising list. This will make 2210 * it possibly to automatically create a periodic advertising sync to this 2211 * device. 2212 * 2213 * @param addr Bluetooth LE identity address. 2214 * @param sid The advertising set ID. This value is obtained from the 2215 * @ref bt_le_scan_recv_info in the scan callback. 2216 * 2217 * @return Zero on success or (negative) error code otherwise. 2218 */ 2219 int bt_le_per_adv_list_add(const bt_addr_le_t *addr, uint8_t sid); 2220 2221 /** 2222 * @brief Remove a device from the periodic advertising list. 2223 * 2224 * Removes peer device LE address from the periodic advertising list. 2225 * 2226 * @param addr Bluetooth LE identity address. 2227 * @param sid The advertising set ID. This value is obtained from the 2228 * @ref bt_le_scan_recv_info in the scan callback. 2229 * 2230 * @return Zero on success or (negative) error code otherwise. 2231 */ 2232 int bt_le_per_adv_list_remove(const bt_addr_le_t *addr, uint8_t sid); 2233 2234 /** 2235 * @brief Clear the periodic advertising list. 2236 * 2237 * Clears the entire periodic advertising list. 2238 * 2239 * @return Zero on success or (negative) error code otherwise. 2240 */ 2241 int bt_le_per_adv_list_clear(void); 2242 2243 2244 enum { 2245 /** Convenience value when no options are specified. */ 2246 BT_LE_SCAN_OPT_NONE = 0, 2247 2248 /** Filter duplicates. */ 2249 BT_LE_SCAN_OPT_FILTER_DUPLICATE = BIT(0), 2250 2251 /** Filter using filter accept list. */ 2252 BT_LE_SCAN_OPT_FILTER_ACCEPT_LIST = BIT(1), 2253 2254 /** Enable scan on coded PHY (Long Range).*/ 2255 BT_LE_SCAN_OPT_CODED = BIT(2), 2256 2257 /** 2258 * @brief Disable scan on 1M phy. 2259 * 2260 * @note Requires @ref BT_LE_SCAN_OPT_CODED. 2261 */ 2262 BT_LE_SCAN_OPT_NO_1M = BIT(3), 2263 }; 2264 2265 enum { 2266 /** Scan without requesting additional information from advertisers. */ 2267 BT_LE_SCAN_TYPE_PASSIVE = 0x00, 2268 2269 /** 2270 * @brief Scan and request additional information from advertisers. 2271 * 2272 * Using this scan type will automatically send scan requests to all 2273 * devices. Scan responses are received in the same manner and using the 2274 * same callbacks as advertising reports. 2275 */ 2276 BT_LE_SCAN_TYPE_ACTIVE = 0x01, 2277 }; 2278 2279 /** LE scan parameters */ 2280 struct bt_le_scan_param { 2281 /** Scan type (BT_LE_SCAN_TYPE_ACTIVE or BT_LE_SCAN_TYPE_PASSIVE) */ 2282 uint8_t type; 2283 2284 /** Bit-field of scanning options. */ 2285 uint8_t options; 2286 2287 /** Scan interval (N * 0.625 ms). 2288 * 2289 * @note When @kconfig{CONFIG_BT_SCAN_AND_INITIATE_IN_PARALLEL} is enabled 2290 * and the application wants to scan and connect in parallel, 2291 * the Bluetooth Controller may require the scan interval used 2292 * for scanning and connection establishment to be equal to 2293 * obtain the best performance. 2294 */ 2295 uint16_t interval; 2296 2297 /** Scan window (N * 0.625 ms) 2298 * 2299 * @note When @kconfig{CONFIG_BT_SCAN_AND_INITIATE_IN_PARALLEL} is enabled 2300 * and the application wants to scan and connect in parallel, 2301 * the Bluetooth Controller may require the scan window used 2302 * for scanning and connection establishment to be equal to 2303 * obtain the best performance. 2304 */ 2305 uint16_t window; 2306 2307 /** 2308 * @brief Scan timeout (N * 10 ms) 2309 * 2310 * Application will be notified by the scan timeout callback. 2311 * Set zero to disable timeout. 2312 */ 2313 uint16_t timeout; 2314 2315 /** 2316 * @brief Scan interval LE Coded PHY (N * 0.625 MS) 2317 * 2318 * Set zero to use same as LE 1M PHY scan interval. 2319 */ 2320 uint16_t interval_coded; 2321 2322 /** 2323 * @brief Scan window LE Coded PHY (N * 0.625 MS) 2324 * 2325 * Set zero to use same as LE 1M PHY scan window. 2326 */ 2327 uint16_t window_coded; 2328 }; 2329 2330 /** LE advertisement and scan response packet information */ 2331 struct bt_le_scan_recv_info { 2332 /** 2333 * @brief Advertiser LE address and type. 2334 * 2335 * If advertiser is anonymous then this address will be 2336 * @ref BT_ADDR_LE_ANY. 2337 */ 2338 const bt_addr_le_t *addr; 2339 2340 /** Advertising Set Identifier. */ 2341 uint8_t sid; 2342 2343 /** Strength of advertiser signal. */ 2344 int8_t rssi; 2345 2346 /** Transmit power of the advertiser. */ 2347 int8_t tx_power; 2348 2349 /** 2350 * @brief Advertising packet type. 2351 * 2352 * Uses the BT_GAP_ADV_TYPE_* value. 2353 * 2354 * May indicate that this is a scan response if the type is 2355 * @ref BT_GAP_ADV_TYPE_SCAN_RSP. 2356 */ 2357 uint8_t adv_type; 2358 2359 /** 2360 * @brief Advertising packet properties bitfield. 2361 * 2362 * Uses the BT_GAP_ADV_PROP_* values. 2363 * May indicate that this is a scan response if the value contains the 2364 * @ref BT_GAP_ADV_PROP_SCAN_RESPONSE bit. 2365 * 2366 */ 2367 uint16_t adv_props; 2368 2369 /** 2370 * @brief Periodic advertising interval (N * 1.25 ms). 2371 * 2372 * If 0 there is no periodic advertising. 2373 */ 2374 uint16_t interval; 2375 2376 /** Primary advertising channel PHY. */ 2377 uint8_t primary_phy; 2378 2379 /** Secondary advertising channel PHY. */ 2380 uint8_t secondary_phy; 2381 }; 2382 2383 /** Listener context for (LE) scanning. */ 2384 struct bt_le_scan_cb { 2385 2386 /** 2387 * @brief Advertisement packet and scan response received callback. 2388 * 2389 * @param info Advertiser packet and scan response information. 2390 * @param buf Buffer containing advertiser data. 2391 */ 2392 void (*recv)(const struct bt_le_scan_recv_info *info, 2393 struct net_buf_simple *buf); 2394 2395 /** @brief The scanner has stopped scanning after scan timeout. */ 2396 void (*timeout)(void); 2397 2398 sys_snode_t node; 2399 }; 2400 2401 /** 2402 * @brief Initialize scan parameters 2403 * 2404 * @param _type Scan Type, BT_LE_SCAN_TYPE_ACTIVE or 2405 * BT_LE_SCAN_TYPE_PASSIVE. 2406 * @param _options Scan options 2407 * @param _interval Scan Interval (N * 0.625 ms) 2408 * @param _window Scan Window (N * 0.625 ms) 2409 */ 2410 #define BT_LE_SCAN_PARAM_INIT(_type, _options, _interval, _window) \ 2411 { \ 2412 .type = (_type), \ 2413 .options = (_options), \ 2414 .interval = (_interval), \ 2415 .window = (_window), \ 2416 .timeout = 0, \ 2417 .interval_coded = 0, \ 2418 .window_coded = 0, \ 2419 } 2420 2421 /** 2422 * @brief Helper to declare scan parameters inline 2423 * 2424 * @param _type Scan Type, BT_LE_SCAN_TYPE_ACTIVE or 2425 * BT_LE_SCAN_TYPE_PASSIVE. 2426 * @param _options Scan options 2427 * @param _interval Scan Interval (N * 0.625 ms) 2428 * @param _window Scan Window (N * 0.625 ms) 2429 */ 2430 #define BT_LE_SCAN_PARAM(_type, _options, _interval, _window) \ 2431 ((struct bt_le_scan_param[]) { \ 2432 BT_LE_SCAN_PARAM_INIT(_type, _options, _interval, _window) \ 2433 }) 2434 2435 /** 2436 * @brief Helper macro to enable active scanning to discover new devices. 2437 */ 2438 #define BT_LE_SCAN_ACTIVE BT_LE_SCAN_PARAM(BT_LE_SCAN_TYPE_ACTIVE, \ 2439 BT_LE_SCAN_OPT_FILTER_DUPLICATE, \ 2440 BT_GAP_SCAN_FAST_INTERVAL, \ 2441 BT_GAP_SCAN_FAST_WINDOW) 2442 2443 /** 2444 * @brief Helper macro to enable active scanning to discover new devices with window == interval. 2445 * 2446 * Continuous scanning should be used to maximize the chances of receiving advertising packets. 2447 */ 2448 #define BT_LE_SCAN_ACTIVE_CONTINUOUS BT_LE_SCAN_PARAM(BT_LE_SCAN_TYPE_ACTIVE, \ 2449 BT_LE_SCAN_OPT_FILTER_DUPLICATE, \ 2450 BT_GAP_SCAN_FAST_INTERVAL_MIN, \ 2451 BT_GAP_SCAN_FAST_WINDOW) 2452 BUILD_ASSERT(BT_GAP_SCAN_FAST_WINDOW == BT_GAP_SCAN_FAST_INTERVAL_MIN, 2453 "Continuous scanning is requested by setting window and interval equal."); 2454 2455 /** 2456 * @brief Helper macro to enable passive scanning to discover new devices. 2457 * 2458 * This macro should be used if information required for device identification 2459 * (e.g., UUID) are known to be placed in Advertising Data. 2460 */ 2461 #define BT_LE_SCAN_PASSIVE BT_LE_SCAN_PARAM(BT_LE_SCAN_TYPE_PASSIVE, \ 2462 BT_LE_SCAN_OPT_FILTER_DUPLICATE, \ 2463 BT_GAP_SCAN_FAST_INTERVAL, \ 2464 BT_GAP_SCAN_FAST_WINDOW) 2465 2466 /** 2467 * @brief Helper macro to enable passive scanning to discover new devices with window==interval. 2468 * 2469 * This macro should be used if information required for device identification 2470 * (e.g., UUID) are known to be placed in Advertising Data. 2471 */ 2472 #define BT_LE_SCAN_PASSIVE_CONTINUOUS BT_LE_SCAN_PARAM(BT_LE_SCAN_TYPE_PASSIVE, \ 2473 BT_LE_SCAN_OPT_FILTER_DUPLICATE, \ 2474 BT_GAP_SCAN_FAST_INTERVAL_MIN, \ 2475 BT_GAP_SCAN_FAST_WINDOW) 2476 BUILD_ASSERT(BT_GAP_SCAN_FAST_WINDOW == BT_GAP_SCAN_FAST_INTERVAL_MIN, 2477 "Continuous scanning is requested by setting window and interval equal."); 2478 2479 /** 2480 * @brief Helper macro to enable active scanning to discover new devices. 2481 * Include scanning on Coded PHY in addition to 1M PHY. 2482 */ 2483 #define BT_LE_SCAN_CODED_ACTIVE \ 2484 BT_LE_SCAN_PARAM(BT_LE_SCAN_TYPE_ACTIVE, \ 2485 BT_LE_SCAN_OPT_CODED | \ 2486 BT_LE_SCAN_OPT_FILTER_DUPLICATE, \ 2487 BT_GAP_SCAN_FAST_INTERVAL, \ 2488 BT_GAP_SCAN_FAST_WINDOW) 2489 2490 /** 2491 * @brief Helper macro to enable passive scanning to discover new devices. 2492 * Include scanning on Coded PHY in addition to 1M PHY. 2493 * 2494 * This macro should be used if information required for device identification 2495 * (e.g., UUID) are known to be placed in Advertising Data. 2496 */ 2497 #define BT_LE_SCAN_CODED_PASSIVE \ 2498 BT_LE_SCAN_PARAM(BT_LE_SCAN_TYPE_PASSIVE, \ 2499 BT_LE_SCAN_OPT_CODED | \ 2500 BT_LE_SCAN_OPT_FILTER_DUPLICATE, \ 2501 BT_GAP_SCAN_FAST_INTERVAL, \ 2502 BT_GAP_SCAN_FAST_WINDOW) 2503 2504 /** 2505 * @brief Start (LE) scanning 2506 * 2507 * Start LE scanning with given parameters and provide results through 2508 * the specified callback. 2509 * 2510 * @note The LE scanner by default does not use the Identity Address of the 2511 * local device when @kconfig{CONFIG_BT_PRIVACY} is disabled. This is to 2512 * prevent the active scanner from disclosing the identity information 2513 * when requesting additional information from advertisers. 2514 * In order to enable directed advertiser reports then 2515 * @kconfig{CONFIG_BT_SCAN_WITH_IDENTITY} must be enabled. 2516 * 2517 * @note Setting the `param.timeout` parameter is not supported when 2518 * @kconfig{CONFIG_BT_PRIVACY} is enabled, when the param.type is @ref 2519 * BT_LE_SCAN_TYPE_ACTIVE. Supplying a non-zero timeout will result in an 2520 * -EINVAL error code. 2521 * 2522 * @param param Scan parameters. 2523 * @param cb Callback to notify scan results. May be NULL if callback 2524 * registration through @ref bt_le_scan_cb_register is preferred. 2525 * 2526 * @return Zero on success or error code otherwise, positive in case of 2527 * protocol error or negative (POSIX) in case of stack internal error. 2528 * @retval -EBUSY if the scanner is already being started in a different thread. 2529 */ 2530 int bt_le_scan_start(const struct bt_le_scan_param *param, bt_le_scan_cb_t cb); 2531 2532 /** 2533 * @brief Stop (LE) scanning. 2534 * 2535 * Stops ongoing LE scanning. 2536 * 2537 * @return Zero on success or error code otherwise, positive in case of 2538 * protocol error or negative (POSIX) in case of stack internal error. 2539 */ 2540 int bt_le_scan_stop(void); 2541 2542 /** 2543 * @brief Register scanner packet callbacks. 2544 * 2545 * Adds the callback structure to the list of callback structures that monitors 2546 * scanner activity. 2547 * 2548 * This callback will be called for all scanner activity, regardless of what 2549 * API was used to start the scanner. 2550 * 2551 * @param cb Callback struct. Must point to memory that remains valid. 2552 * 2553 * @retval 0 Success. 2554 * @retval -EEXIST if @p cb was already registered. 2555 */ 2556 int bt_le_scan_cb_register(struct bt_le_scan_cb *cb); 2557 2558 /** 2559 * @brief Unregister scanner packet callbacks. 2560 * 2561 * Remove the callback structure from the list of scanner callbacks. 2562 * 2563 * @param cb Callback struct. Must point to memory that remains valid. 2564 */ 2565 void bt_le_scan_cb_unregister(struct bt_le_scan_cb *cb); 2566 2567 /** 2568 * @brief Add device (LE) to filter accept list. 2569 * 2570 * Add peer device LE address to the filter accept list. 2571 * 2572 * @note The filter accept list cannot be modified when an LE role is using 2573 * the filter accept list, i.e advertiser or scanner using a filter accept list 2574 * or automatic connecting to devices using filter accept list. 2575 * 2576 * @param addr Bluetooth LE identity address. 2577 * 2578 * @return Zero on success or error code otherwise, positive in case of 2579 * protocol error or negative (POSIX) in case of stack internal error. 2580 */ 2581 int bt_le_filter_accept_list_add(const bt_addr_le_t *addr); 2582 2583 /** 2584 * @brief Remove device (LE) from filter accept list. 2585 * 2586 * Remove peer device LE address from the filter accept list. 2587 * 2588 * @note The filter accept list cannot be modified when an LE role is using 2589 * the filter accept list, i.e advertiser or scanner using a filter accept list 2590 * or automatic connecting to devices using filter accept list. 2591 * 2592 * @param addr Bluetooth LE identity address. 2593 * 2594 * @return Zero on success or error code otherwise, positive in case of 2595 * protocol error or negative (POSIX) in case of stack internal error. 2596 */ 2597 int bt_le_filter_accept_list_remove(const bt_addr_le_t *addr); 2598 2599 /** 2600 * @brief Clear filter accept list. 2601 * 2602 * Clear all devices from the filter accept list. 2603 * 2604 * @note The filter accept list cannot be modified when an LE role is using 2605 * the filter accept list, i.e advertiser or scanner using a filter accept 2606 * list or automatic connecting to devices using filter accept list. 2607 * 2608 * @return Zero on success or error code otherwise, positive in case of 2609 * protocol error or negative (POSIX) in case of stack internal error. 2610 */ 2611 int bt_le_filter_accept_list_clear(void); 2612 2613 /** 2614 * @brief Set (LE) channel map. 2615 * 2616 * @param chan_map Channel map. 2617 * 2618 * @return Zero on success or error code otherwise, positive in case of 2619 * protocol error or negative (POSIX) in case of stack internal error. 2620 */ 2621 int bt_le_set_chan_map(uint8_t chan_map[5]); 2622 2623 /** 2624 * @brief Set the Resolvable Private Address timeout in runtime 2625 * 2626 * The new RPA timeout value will be used for the next RPA rotation 2627 * and all subsequent rotations until another override is scheduled 2628 * with this API. 2629 * 2630 * Initially, the if @kconfig{CONFIG_BT_RPA_TIMEOUT} is used as the 2631 * RPA timeout. 2632 * 2633 * This symbol is linkable if @kconfig{CONFIG_BT_RPA_TIMEOUT_DYNAMIC} 2634 * is enabled. 2635 * 2636 * @param new_rpa_timeout Resolvable Private Address timeout in seconds 2637 * 2638 * @retval 0 Success. 2639 * @retval -EINVAL RPA timeout value is invalid. Valid range is 1s - 3600s. 2640 */ 2641 int bt_le_set_rpa_timeout(uint16_t new_rpa_timeout); 2642 2643 /** 2644 * @brief Helper for parsing advertising (or EIR or OOB) data. 2645 * 2646 * A helper for parsing the basic data types used for Extended Inquiry 2647 * Response (EIR), Advertising Data (AD), and OOB data blocks. The most 2648 * common scenario is to call this helper on the advertising data 2649 * received in the callback that was given to bt_le_scan_start(). 2650 * 2651 * @warning This helper function will consume `ad` when parsing. The user should 2652 * make a copy if the original data is to be used afterwards 2653 * 2654 * @param ad Advertising data as given to the bt_le_scan_cb_t callback. 2655 * @param func Callback function which will be called for each element 2656 * that's found in the data. The callback should return 2657 * true to continue parsing, or false to stop parsing. 2658 * @param user_data User data to be passed to the callback. 2659 */ 2660 void bt_data_parse(struct net_buf_simple *ad, 2661 bool (*func)(struct bt_data *data, void *user_data), 2662 void *user_data); 2663 2664 /** LE Secure Connections pairing Out of Band data. */ 2665 struct bt_le_oob_sc_data { 2666 /** Random Number. */ 2667 uint8_t r[16]; 2668 2669 /** Confirm Value. */ 2670 uint8_t c[16]; 2671 }; 2672 2673 /** LE Out of Band information. */ 2674 struct bt_le_oob { 2675 /** LE address. If privacy is enabled this is a Resolvable Private 2676 * Address. 2677 */ 2678 bt_addr_le_t addr; 2679 2680 /** LE Secure Connections pairing Out of Band data. */ 2681 struct bt_le_oob_sc_data le_sc_data; 2682 }; 2683 2684 /** 2685 * @brief Get local LE Out of Band (OOB) information. 2686 * 2687 * This function allows to get local information that are useful for 2688 * Out of Band pairing or connection creation. 2689 * 2690 * If privacy @kconfig{CONFIG_BT_PRIVACY} is enabled this will result in 2691 * generating new Resolvable Private Address (RPA) that is valid for 2692 * @kconfig{CONFIG_BT_RPA_TIMEOUT} seconds. This address will be used for 2693 * advertising started by @ref bt_le_adv_start, active scanning and 2694 * connection creation. 2695 * 2696 * @note If privacy is enabled the RPA cannot be refreshed in the following 2697 * cases: 2698 * - Creating a connection in progress, wait for the connected callback. 2699 * In addition when extended advertising @kconfig{CONFIG_BT_EXT_ADV} is 2700 * not enabled or not supported by the controller: 2701 * - Advertiser is enabled using a Random Static Identity Address for a 2702 * different local identity. 2703 * - The local identity conflicts with the local identity used by other 2704 * roles. 2705 * 2706 * @param[in] id Local identity, in most cases BT_ID_DEFAULT. 2707 * @param[out] oob LE OOB information 2708 * 2709 * @return Zero on success or error code otherwise, positive in case of 2710 * protocol error or negative (POSIX) in case of stack internal error. 2711 */ 2712 int bt_le_oob_get_local(uint8_t id, struct bt_le_oob *oob); 2713 2714 /** 2715 * @brief Get local LE Out of Band (OOB) information. 2716 * 2717 * This function allows to get local information that are useful for 2718 * Out of Band pairing or connection creation. 2719 * 2720 * If privacy @kconfig{CONFIG_BT_PRIVACY} is enabled this will result in 2721 * generating new Resolvable Private Address (RPA) that is valid for 2722 * @kconfig{CONFIG_BT_RPA_TIMEOUT} seconds. This address will be used by the 2723 * advertising set. 2724 * 2725 * @note When generating OOB information for multiple advertising set all 2726 * OOB information needs to be generated at the same time. 2727 * 2728 * @note If privacy is enabled the RPA cannot be refreshed in the following 2729 * cases: 2730 * - Creating a connection in progress, wait for the connected callback. 2731 * 2732 * @param[in] adv The advertising set object 2733 * @param[out] oob LE OOB information 2734 * 2735 * @return Zero on success or error code otherwise, positive in case 2736 * of protocol error or negative (POSIX) in case of stack internal error. 2737 */ 2738 int bt_le_ext_adv_oob_get_local(struct bt_le_ext_adv *adv, 2739 struct bt_le_oob *oob); 2740 2741 /** 2742 * @brief Clear pairing information. 2743 * 2744 * @param id Local identity (mostly just BT_ID_DEFAULT). 2745 * @param addr Remote address, NULL or BT_ADDR_LE_ANY to clear all remote 2746 * devices. 2747 * 2748 * @return 0 on success or negative error value on failure. 2749 */ 2750 int bt_unpair(uint8_t id, const bt_addr_le_t *addr); 2751 2752 /** Information about a bond with a remote device. */ 2753 struct bt_bond_info { 2754 /** Address of the remote device. */ 2755 bt_addr_le_t addr; 2756 }; 2757 2758 /** 2759 * @brief Iterate through all existing bonds. 2760 * 2761 * @param id Local identity (mostly just BT_ID_DEFAULT). 2762 * @param func Function to call for each bond. 2763 * @param user_data Data to pass to the callback function. 2764 */ 2765 void bt_foreach_bond(uint8_t id, void (*func)(const struct bt_bond_info *info, 2766 void *user_data), 2767 void *user_data); 2768 2769 /** @brief Configure vendor data path 2770 * 2771 * Request the Controller to configure the data transport path in a given direction between 2772 * the Controller and the Host. 2773 * 2774 * @param dir Direction to be configured, BT_HCI_DATAPATH_DIR_HOST_TO_CTLR or 2775 * BT_HCI_DATAPATH_DIR_CTLR_TO_HOST 2776 * @param id Vendor specific logical transport channel ID, range 2777 * [BT_HCI_DATAPATH_ID_VS..BT_HCI_DATAPATH_ID_VS_END] 2778 * @param vs_config_len Length of additional vendor specific configuration data 2779 * @param vs_config Pointer to additional vendor specific configuration data 2780 * 2781 * @return 0 in case of success or negative value in case of error. 2782 */ 2783 int bt_configure_data_path(uint8_t dir, uint8_t id, uint8_t vs_config_len, 2784 const uint8_t *vs_config); 2785 2786 struct bt_le_per_adv_sync_subevent_params { 2787 /** @brief Periodic Advertising Properties. 2788 * 2789 * Bit 6 is include TxPower, all others RFU. 2790 * 2791 */ 2792 uint16_t properties; 2793 2794 /** Number of subevents to sync to */ 2795 uint8_t num_subevents; 2796 2797 /** @brief The subevent(s) to synchronize with 2798 * 2799 * The array must have @ref num_subevents elements. 2800 * 2801 */ 2802 uint8_t *subevents; 2803 }; 2804 2805 /** @brief Synchronize with a subset of subevents 2806 * 2807 * Until this command is issued, the subevent(s) the controller is synchronized 2808 * to is unspecified. 2809 * 2810 * @param per_adv_sync The periodic advertising sync object. 2811 * @param params Parameters. 2812 * 2813 * @return 0 in case of success or negative value in case of error. 2814 */ 2815 int bt_le_per_adv_sync_subevent(struct bt_le_per_adv_sync *per_adv_sync, 2816 struct bt_le_per_adv_sync_subevent_params *params); 2817 2818 struct bt_le_per_adv_response_params { 2819 /** @brief The periodic event counter of the request the response is sent to. 2820 * 2821 * @ref bt_le_per_adv_sync_recv_info 2822 * 2823 * @note The response can be sent up to one periodic interval after 2824 * the request was received. 2825 * 2826 */ 2827 uint16_t request_event; 2828 2829 /** @brief The subevent counter of the request the response is sent to. 2830 * 2831 * @ref bt_le_per_adv_sync_recv_info 2832 * 2833 */ 2834 uint8_t request_subevent; 2835 2836 /** The subevent the response shall be sent in */ 2837 uint8_t response_subevent; 2838 2839 /** The response slot the response shall be sent in */ 2840 uint8_t response_slot; 2841 }; 2842 2843 /** 2844 * @brief Set the data for a response slot in a specific subevent of the PAwR. 2845 * 2846 * This function is called by the application to set the response data. 2847 * The data for a response slot shall be transmitted only once. 2848 * 2849 * @param per_adv_sync The periodic advertising sync object. 2850 * @param params Parameters. 2851 * @param data The response data to send. 2852 * 2853 * @return Zero on success or (negative) error code otherwise. 2854 */ 2855 int bt_le_per_adv_set_response_data(struct bt_le_per_adv_sync *per_adv_sync, 2856 const struct bt_le_per_adv_response_params *params, 2857 const struct net_buf_simple *data); 2858 2859 /** @brief Check if a device identified by a Bluetooth LE address is bonded. 2860 * 2861 * Valid Bluetooth LE identity addresses are either public address or 2862 * random static address. 2863 * 2864 * @param id Local identity (typically @ref BT_ID_DEFAULT). 2865 * @param addr Bluetooth LE device address. 2866 * 2867 * @return true if @p addr is bonded with local @p id 2868 */ 2869 bool bt_le_bond_exists(uint8_t id, const bt_addr_le_t *addr); 2870 2871 /** 2872 * @} 2873 */ 2874 2875 #ifdef __cplusplus 2876 } 2877 #endif 2878 /** 2879 * @} 2880 */ 2881 2882 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_BLUETOOTH_H_ */ 2883