1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 24 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 /** 39 * @title Genral Access Profile (GAP) 40 * 41 */ 42 43 #ifndef GAP_H 44 #define GAP_H 45 46 #if defined __cplusplus 47 extern "C" { 48 #endif 49 50 #include "btstack_defines.h" 51 #include "btstack_util.h" 52 53 #ifdef ENABLE_CLASSIC 54 #include "classic/btstack_link_key_db.h" 55 #endif 56 57 typedef enum { 58 59 // MITM protection not required 60 // No encryption required 61 // No user interaction required 62 LEVEL_0 = 0, 63 64 // MITM protection not required 65 // No encryption required 66 // Minimal user interaction desired 67 LEVEL_1, 68 69 // MITM protection not required 70 // Encryption required 71 LEVEL_2, 72 73 // MITM protection required 74 // Encryption required 75 // User interaction acceptable 76 LEVEL_3, 77 78 // MITM protection required 79 // Encryption required 80 // 128-bit equivalent strength for link and encryption keys required (P-192 is not enough) 81 // User interaction acceptable 82 LEVEL_4, 83 } gap_security_level_t; 84 85 86 typedef enum { 87 // non-secure 88 GAP_SECURITY_MODE_1 = 1, 89 90 // service level enforced security 91 GAP_SECURITY_MODE_2, 92 93 // link level enforced security 94 GAP_SECURITY_MODE_3, 95 96 // service level enforced security 97 GAP_SECURITY_MODE_4 98 } gap_security_mode_t; 99 100 typedef enum { 101 GAP_SECURITY_NONE, 102 GAP_SECURITY_ENCRYPTED, // SSP: JUST WORKS 103 GAP_SECURITY_AUTHENTICATED, // SSP: numeric comparison, passkey, OOB 104 // GAP_SECURITY_AUTHORIZED 105 } gap_security_state; 106 107 typedef enum { 108 GAP_CONNECTION_INVALID, 109 GAP_CONNECTION_ACL, 110 GAP_CONNECTION_SCO, 111 GAP_CONNECTION_LE 112 } gap_connection_type_t; 113 114 typedef struct le_connection_parameter_range{ 115 uint16_t le_conn_interval_min; 116 uint16_t le_conn_interval_max; 117 uint16_t le_conn_latency_min; 118 uint16_t le_conn_latency_max; 119 uint16_t le_supervision_timeout_min; 120 uint16_t le_supervision_timeout_max; 121 } le_connection_parameter_range_t; 122 123 typedef enum { 124 GAP_RANDOM_ADDRESS_TYPE_OFF = 0, 125 GAP_RANDOM_ADDRESS_TYPE_STATIC, 126 GAP_RANDOM_ADDRESS_NON_RESOLVABLE, 127 GAP_RANDOM_ADDRESS_RESOLVABLE, 128 } gap_random_address_type_t; 129 130 // Authorization state 131 typedef enum { 132 AUTHORIZATION_UNKNOWN, 133 AUTHORIZATION_PENDING, 134 AUTHORIZATION_DECLINED, 135 AUTHORIZATION_GRANTED 136 } authorization_state_t; 137 138 // Extended Advertising Parameters 139 typedef struct { 140 uint16_t advertising_event_properties; 141 uint16_t primary_advertising_interval_min; 142 uint16_t primary_advertising_interval_max; 143 uint8_t primary_advertising_channel_map; 144 bd_addr_type_t own_address_type; 145 bd_addr_type_t peer_address_type; 146 bd_addr_t peer_address; 147 uint8_t advertising_filter_policy; 148 int8_t advertising_tx_power; 149 uint8_t primary_advertising_phy; 150 uint8_t secondary_advertising_max_skip; 151 uint8_t secondary_advertising_phy; 152 uint8_t advertising_sid; 153 uint8_t scan_request_notification_enable; 154 } le_extended_advertising_parameters_t; 155 156 typedef struct { 157 uint16_t periodic_advertising_interval_min; 158 uint16_t periodic_advertising_interval_max; 159 uint16_t periodic_advertising_properties; 160 } le_periodic_advertising_parameters_t; 161 162 // Extended Advertising Set State 163 typedef struct { 164 btstack_linked_item_t item; 165 le_extended_advertising_parameters_t extended_params; 166 le_periodic_advertising_parameters_t periodic_params; 167 bd_addr_t random_address; 168 const uint8_t * adv_data; 169 const uint8_t * scan_data; 170 const uint8_t * periodic_data; 171 uint16_t adv_data_len; 172 uint16_t scan_data_len; 173 uint16_t periodic_data_len; 174 uint16_t adv_data_pos; 175 uint16_t scan_data_pos; 176 uint16_t periodic_data_pos; 177 uint16_t enable_timeout; 178 uint8_t advertising_handle; 179 uint8_t enable_max_scan_events; 180 bool periodic_include_adi; 181 uint8_t state; 182 uint8_t tasks; 183 } le_advertising_set_t; 184 185 /* API_START */ 186 187 // Classic + LE 188 189 /** 190 * @brief Read RSSI 191 * @param con_handle 192 * @events: GAP_EVENT_RSSI_MEASUREMENT 193 */ 194 int gap_read_rssi(hci_con_handle_t con_handle); 195 196 197 /** 198 * @brief Gets local address. 199 */ 200 void gap_local_bd_addr(bd_addr_t address_buffer); 201 202 /** 203 * @brief Disconnect connection with handle 204 * @param handle 205 */ 206 uint8_t gap_disconnect(hci_con_handle_t handle); 207 208 /** 209 * @brief Get connection type 210 * @param con_handle 211 * @result connection_type 212 */ 213 gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle); 214 215 /** 216 * @brief Get HCI connection role 217 * @param con_handle 218 * @result hci_role_t HCI_ROLE_MASTER / HCI_ROLE_SLAVE / HCI_ROLE_INVALID (if connection does not exist) 219 */ 220 hci_role_t gap_get_role(hci_con_handle_t connection_handle); 221 222 // Classic 223 224 /** 225 * @brief Request role switch 226 * @note this only requests the role switch. A HCI_EVENT_ROLE_CHANGE is emitted and its status field will indicate if the switch was succesful 227 * @param addr 228 * @param hci_role_t HCI_ROLE_MASTER / HCI_ROLE_SLAVE 229 * @result status 230 */ 231 uint8_t gap_request_role(const bd_addr_t addr, hci_role_t role); 232 233 /** 234 * @brief Sets local name. 235 * @note Default name is 'BTstack 00:00:00:00:00:00' 236 * @note '00:00:00:00:00:00' in local_name will be replaced with actual bd addr 237 * @param name is not copied, make sure memory stays valid 238 */ 239 void gap_set_local_name(const char * local_name); 240 241 /** 242 * @brief Set Extended Inquiry Response data 243 * @note If not set, local name will be used for EIR data (see gap_set_local_name) 244 * @note '00:00:00:00:00:00' in local_name will be replaced with actual bd addr 245 * @param eir_data size HCI_EXTENDED_INQUIRY_RESPONSE_DATA_LEN (240) bytes, is not copied make sure memory stays valid 246 */ 247 void gap_set_extended_inquiry_response(const uint8_t * data); 248 249 /** 250 * @brief Set class of device 251 */ 252 void gap_set_class_of_device(uint32_t class_of_device); 253 254 /** 255 * @brief Set default link policy settings for all classic ACL links 256 * @param default_link_policy_settings - see LM_LINK_POLICY_* in bluetooth.h 257 * @note common value: LM_LINK_POLICY_ENABLE_ROLE_SWITCH | LM_LINK_POLICY_ENABLE_SNIFF_MODE to enable role switch and sniff mode 258 */ 259 void gap_set_default_link_policy_settings(uint16_t default_link_policy_settings); 260 261 /** 262 * @brief Set Allow Role Switch param for outgoing classic ACL links 263 * @param allow_role_switch - true: allow remote device to request role switch, false: stay master 264 */ 265 void gap_set_allow_role_switch(bool allow_role_switch); 266 267 /** 268 * @brief Set link supervision timeout for outgoing classic ACL links 269 * @param default_link_supervision_timeout * 0.625 ms, default 0x7d00 = 20 seconds, 0 = no link supervision timeout 270 */ 271 void gap_set_link_supervision_timeout(uint16_t link_supervision_timeout); 272 273 /** 274 * @brief Enable/disable bonding. Default is enabled. 275 * @param enabled 276 */ 277 void gap_set_bondable_mode(int enabled); 278 279 /** 280 * @brief Get bondable mode. 281 * @return 1 if bondable 282 */ 283 int gap_get_bondable_mode(void); 284 285 /** 286 * @brief Set security mode for all outgoing and incoming connections. Default: GAP_SECURITY_MODE_4 287 * @param security_mode is GAP_SECURITY_MODE_2 or GAP_SECURITY_MODE_4 288 * @return status ERROR_CODE_SUCCESS or ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE 289 */ 290 uint8_t gap_set_security_mode(gap_security_mode_t security_mode); 291 292 /** 293 * @brief Get security mode 294 * @return security_mode 295 */ 296 gap_security_mode_t gap_get_security_mode(void); 297 298 /** 299 * @brief Set security level for all outgoing and incoming connections. Default: LEVEL_2 300 * @param security_level 301 * @note has to be called before services or profiles are initialized 302 */ 303 void gap_set_security_level(gap_security_level_t security_level); 304 305 /** 306 * @brief Get security level 307 * @return security_level 308 */ 309 gap_security_level_t gap_get_security_level(void); 310 311 /** 312 * @brief Set Secure Connections Only Mode for BR/EDR (Classic) Default: false 313 * @param enable 314 */ 315 void gap_set_secure_connections_only_mode(bool enable); 316 317 /** 318 * @breif Get Secure Connections Only Mode 319 * @param enabled 320 */ 321 bool gap_get_secure_connections_only_mode(void); 322 323 /** 324 * @brief Set minimal security level for registered services 325 * @param security_level 326 * @note Called by L2CAP based on registered services 327 */ 328 void gap_set_minimal_service_security_level(gap_security_level_t security_level); 329 330 /** 331 * @brief Register filter for rejecting classic connections. Callback will return 1 accept connection, 0 on reject. 332 */ 333 void gap_register_classic_connection_filter(int (*accept_callback)(bd_addr_t addr, hci_link_type_t link_type)); 334 335 /* Configure Secure Simple Pairing */ 336 337 /** 338 * @brief Enable will enable SSP during init. Default: true 339 */ 340 void gap_ssp_set_enable(int enable); 341 342 /** 343 * @brief Set IO Capability. BTstack will return capability to SSP requests 344 */ 345 void gap_ssp_set_io_capability(int ssp_io_capability); 346 347 /** 348 * @brief Set Authentication Requirements using during SSP 349 */ 350 void gap_ssp_set_authentication_requirement(int authentication_requirement); 351 352 /** 353 * @brief Enable/disable Secure Connections. Default: true if supported by Controller 354 */ 355 void gap_secure_connections_enable(bool enable); 356 357 /** 358 * @brief If set, BTstack will confirm a numeric comparison and enter '000000' if requested. 359 */ 360 void gap_ssp_set_auto_accept(int auto_accept); 361 362 /** 363 * @brief Set required encryption key size for GAP Levels 1-3 on ccassic connections. Default: 16 bytes 364 * @param encryption_key_size in bytes. Valid 7..16 365 */ 366 void gap_set_required_encryption_key_size(uint8_t encryption_key_size); 367 368 /** 369 * @brief Start dedicated bonding with device. Disconnect after bonding. 370 * @param device 371 * @param request MITM protection 372 * @return error, if max num acl connections active 373 * @result GAP_DEDICATED_BONDING_COMPLETE 374 */ 375 int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required); 376 377 gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type); 378 379 /** 380 * @brief map link keys to secure connection yes/no 381 */ 382 bool gap_secure_connection_for_link_key_type(link_key_type_t link_key_type); 383 384 /** 385 * @brief map link keys to authenticated 386 */ 387 bool gap_authenticated_for_link_key_type(link_key_type_t link_key_type); 388 389 gap_security_level_t gap_security_level(hci_con_handle_t con_handle); 390 391 void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t level); 392 393 bool gap_mitm_protection_required_for_security_level(gap_security_level_t level); 394 395 /** 396 * @brief Set Page Scan Type 397 * @param page_scan_interval * 0.625 ms, range: 0x0012..0x1000, default: 0x0800 398 * @param page_scan_windows * 0.625 ms, range: 0x0011..page_scan_interval, default: 0x0012 399 */ 400 void gap_set_page_scan_activity(uint16_t page_scan_interval, uint16_t page_scan_window); 401 402 /** 403 * @brief Set Page Scan Type 404 * @param page_scan_mode 405 */ 406 void gap_set_page_scan_type(page_scan_type_t page_scan_type); 407 408 /** 409 * @brief Set Page Timeout 410 * @param page_timeout * 0.625 ms, range: 0x0001..0xffff, default: 0x6000 (ca 15 seconds) 411 */ 412 void gap_set_page_timeout(uint16_t page_timeout); 413 414 // LE 415 416 /** 417 * @brief Set parameters for LE Scan 418 * @param scan_type 0 = passive, 1 = active 419 * @param scan_interval range 0x0004..0x4000, unit 0.625 ms 420 * @param scan_window range 0x0004..0x4000, unit 0.625 ms 421 * @param scanning_filter_policy 0 = all devices, 1 = all from whitelist 422 */ 423 void gap_set_scan_params(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window, uint8_t scanning_filter_policy); 424 425 /** 426 * @brief Set parameters for LE Scan 427 * @deprecated Use gap_set_scan_params instead 428 */ 429 void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window); 430 431 /** 432 * @brief Start LE Scan 433 */ 434 void gap_start_scan(void); 435 436 /** 437 * @brief Stop LE Scan 438 */ 439 void gap_stop_scan(void); 440 441 /** 442 * @brief Enable privacy by using random addresses 443 * @param random_address_type to use (incl. OFF) 444 */ 445 void gap_random_address_set_mode(gap_random_address_type_t random_address_type); 446 447 /** 448 * @brief Get privacy mode 449 */ 450 gap_random_address_type_t gap_random_address_get_mode(void); 451 452 /** 453 * @brief Sets update period for random address 454 * @param period_ms in ms 455 */ 456 void gap_random_address_set_update_period(int period_ms); 457 458 /** 459 * @brief Sets a fixed random address for advertising 460 * @param addr 461 * @note Sets random address mode to type off 462 */ 463 void gap_random_address_set(const bd_addr_t addr); 464 465 /** 466 * @brief Set Advertisement Data 467 * @param advertising_data_length 468 * @param advertising_data (max 31 octets) 469 * @note data is not copied, pointer has to stay valid 470 * @note '00:00:00:00:00:00' in advertising_data will be replaced with actual bd addr 471 */ 472 void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data); 473 474 /** 475 * @brief Set Advertisement Parameters 476 * @param adv_int_min 477 * @param adv_int_max 478 * @param adv_type 479 * @param direct_address_type 480 * @param direct_address 481 * @param channel_map 482 * @param filter_policy 483 * @note own_address_type is used from gap_random_address_set_mode 484 */ 485 void gap_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 486 uint8_t direct_address_typ, bd_addr_t direct_address, uint8_t channel_map, uint8_t filter_policy); 487 488 /** 489 * @brief Enable/Disable Advertisements. OFF by default. 490 * @param enabled 491 */ 492 void gap_advertisements_enable(int enabled); 493 494 /** 495 * @brief Set Scan Response Data 496 * 497 * @note For scan response data, scannable undirected advertising (ADV_SCAN_IND) need to be used 498 * 499 * @param advertising_data_length 500 * @param advertising_data (max 31 octets) 501 * @note data is not copied, pointer has to stay valid 502 * @note '00:00:00:00:00:00' in scan_response_data will be replaced with actual bd addr 503 */ 504 void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data); 505 506 /** 507 * @brief Provide storage for new advertising set and setup on Controller 508 * @param storage to use by stack, needs to stay valid until adv set is removed with gap_extended_advertising_remove 509 * @param advertising_parameters 510 * @param out_advertising_handle to use with other adv config commands 511 * @return status 512 * @events: GAP_SUBEVENT_ADVERTISING_SET_INSTALLED 513 */ 514 uint8_t gap_extended_advertising_setup(le_advertising_set_t * storage, const le_extended_advertising_parameters_t * advertising_parameters, uint8_t * out_advertising_handle); 515 516 /** 517 * @param Set advertising params for advertising set 518 * @param advertising_handle 519 * @param advertising_parameters 520 * @return status 521 */ 522 uint8_t gap_extended_advertising_set_params(uint8_t advertising_handle, const le_extended_advertising_parameters_t * advertising_parameters); 523 524 /** 525 * @param Get advertising params for advertising set, e.g. to update params 526 * @param advertising_handle 527 * @param advertising_parameters 528 * @return status 529 */ 530 uint8_t gap_extended_advertising_get_params(uint8_t advertising_handle, le_extended_advertising_parameters_t * advertising_parameters); 531 532 /** 533 * @param Set periodic advertising params for advertising set 534 * @param advertising_handle 535 * @param advertising_parameters 536 * @return status 537 */ 538 uint8_t gap_periodic_advertising_set_params(uint8_t advertising_handle, const le_periodic_advertising_parameters_t * advertising_parameters); 539 540 /** 541 * @param Get params for periodic advertising set, e.g. to update params 542 * @param advertising_handle 543 * @param advertising_parameters 544 * @return status 545 */ 546 uint8_t gap_periodic_advertising_get_params(uint8_t advertising_handle, le_periodic_advertising_parameters_t * advertising_parameters); 547 548 /** 549 * @param Set random addrress for advertising set 550 * @param advertising_handle 551 * @param random_address 552 * @return status 553 */ 554 uint8_t gap_extended_advertising_set_random_address(uint8_t advertising_handle, bd_addr_t random_address); 555 556 /** 557 * @brief Set Advertising Data for a advertisement set 558 * @param advertising_handle 559 * @param advertising_data_length 560 * @param advertising_data 561 * @return status 562 */ 563 uint8_t gap_extended_advertising_set_adv_data(uint8_t advertising_handle, uint16_t advertising_data_length, const uint8_t * advertising_data); 564 565 /** 566 * @brief Set Scan Response Data for a advertisement set 567 * @param advertising_handle 568 * @param scan_response_data_length 569 * @param scan_response_data 570 * @return status 571 */ 572 uint8_t gap_extended_advertising_set_scan_response_data(uint8_t advertising_handle, uint16_t scan_response_data_length, const uint8_t * scan_response_data); 573 574 /** 575 * @brief Set data for periodic advertisement set 576 * @param advertising_handle 577 * @param periodic_data_length 578 * @param periodic_data 579 * @return status 580 */ 581 uint8_t gap_periodic_advertising_set_data(uint8_t advertising_handle, uint16_t periodic_data_length, const uint8_t * periodic_data); 582 583 /** 584 * @brief Start advertising advertising set 585 * @param advertising_handle 586 * @param timeout in 10ms, or 0 == no timeout 587 * @param num_extended_advertising_events Controller shall send, or 0 == no max number 588 * @return status 589 */ 590 uint8_t gap_extended_advertising_start(uint8_t advertising_handle, uint16_t timeout, uint8_t num_extended_advertising_events); 591 592 /** 593 * @brief Stop advertising 594 * @param advertising_handle 595 * @return status 596 */ 597 uint8_t gap_extended_advertising_stop(uint8_t advertising_handle); 598 599 /** 600 * @brief Start periodic advertising for given advertising set 601 * @param advertising_handle 602 * @param include_adi 603 * @return status 604 */ 605 uint8_t gap_periodic_advertising_start(uint8_t advertising_handle, bool include_adi); 606 607 /** 608 * @brief Stop periodic advertising for given advertising set 609 * @param advertising_handle 610 * @return status 611 */ 612 uint8_t gap_periodic_advertising_stop(uint8_t advertising_handle); 613 614 /** 615 * @brief Remove advertising set from Controller 616 * @param advertising_handle 617 * @return status 618 * @events: GAP_SUBEVENT_ADVERTISING_SET_REMOVED 619 */ 620 uint8_t gap_extended_advertising_remove(uint8_t advertising_handle); 621 622 /** 623 * @brief Set connection parameters for outgoing connections 624 * @param conn_scan_interval (unit: 0.625 msec), default: 60 ms 625 * @param conn_scan_window (unit: 0.625 msec), default: 30 ms 626 * @param conn_interval_min (unit: 1.25ms), default: 10 ms 627 * @param conn_interval_max (unit: 1.25ms), default: 30 ms 628 * @param conn_latency, default: 4 629 * @param supervision_timeout (unit: 10ms), default: 720 ms 630 * @param min_ce_length (unit: 0.625ms), default: 10 ms 631 * @param max_ce_length (unit: 0.625ms), default: 30 ms 632 */ 633 void gap_set_connection_parameters(uint16_t conn_scan_interval, uint16_t conn_scan_window, 634 uint16_t conn_interval_min, uint16_t conn_interval_max, uint16_t conn_latency, 635 uint16_t supervision_timeout, uint16_t min_ce_length, uint16_t max_ce_length); 636 637 /** 638 * @brief Request an update of the connection parameter for a given LE connection 639 * @param handle 640 * @param conn_interval_min (unit: 1.25ms) 641 * @param conn_interval_max (unit: 1.25ms) 642 * @param conn_latency 643 * @param supervision_timeout (unit: 10ms) 644 * @return 0 if ok 645 */ 646 int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min, 647 uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout); 648 649 /** 650 * @brief Updates the connection parameters for a given LE connection 651 * @param handle 652 * @param conn_interval_min (unit: 1.25ms) 653 * @param conn_interval_max (unit: 1.25ms) 654 * @param conn_latency 655 * @param supervision_timeout (unit: 10ms) 656 * @return 0 if ok 657 */ 658 int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min, 659 uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout); 660 661 /** 662 * @brief Set accepted connection parameter range 663 * @param range 664 */ 665 void gap_get_connection_parameter_range(le_connection_parameter_range_t * range); 666 667 /** 668 * @brief Get accepted connection parameter range 669 * @param range 670 */ 671 void gap_set_connection_parameter_range(le_connection_parameter_range_t * range); 672 673 /** 674 * @brief Test if connection parameters are inside in existing rage 675 * @param conn_interval_min (unit: 1.25ms) 676 * @param conn_interval_max (unit: 1.25ms) 677 * @param conn_latency 678 * @param supervision_timeout (unit: 10ms) 679 * @return 1 if included 680 */ 681 int gap_connection_parameter_range_included(le_connection_parameter_range_t * existing_range, uint16_t le_conn_interval_min, uint16_t le_conn_interval_max, uint16_t le_conn_latency, uint16_t le_supervision_timeout); 682 683 /** 684 * @brief Set max number of connections in LE Peripheral role (if Bluetooth Controller supports it) 685 * @note: default: 1 686 * @param max_peripheral_connections 687 */ 688 void gap_set_max_number_peripheral_connections(int max_peripheral_connections); 689 690 /** 691 * @brief Add Device to Whitelist 692 * @param address_typ 693 * @param address 694 * @return 0 if ok 695 */ 696 uint8_t gap_whitelist_add(bd_addr_type_t address_type, const bd_addr_t address); 697 698 /** 699 * @brief Remove Device from Whitelist 700 * @param address_typ 701 * @param address 702 * @return 0 if ok 703 */ 704 uint8_t gap_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address); 705 706 /** 707 * @brief Clear Whitelist 708 * @return 0 if ok 709 */ 710 uint8_t gap_whitelist_clear(void); 711 712 /** 713 * @brief Connect to remote LE device 714 */ 715 uint8_t gap_connect(const bd_addr_t addr, bd_addr_type_t addr_type); 716 717 /** 718 * @brief Connect with Whitelist 719 * @note Explicit whitelist management and this connect with whitelist replace deprecated gap_auto_connection_* functions 720 * @return - if ok 721 */ 722 uint8_t gap_connect_with_whitelist(void); 723 724 /** 725 * @brief Cancel connection process initiated by gap_connect 726 */ 727 uint8_t gap_connect_cancel(void); 728 729 /** 730 * @brief Auto Connection Establishment - Start Connecting to device 731 * @deprecated Please setup Whitelist with gap_whitelist_* and start connecting with gap_connect_with_whitelist 732 * @param address_type 733 * @param address 734 * @return 0 if ok 735 */ 736 uint8_t gap_auto_connection_start(bd_addr_type_t address_type, const bd_addr_t address); 737 738 /** 739 * @brief Auto Connection Establishment - Stop Connecting to device 740 * @deprecated Please setup Whitelist with gap_whitelist_* and start connecting with gap_connect_with_whitelist 741 * @param address_type 742 * @param address 743 * @return 0 if ok 744 */ 745 uint8_t gap_auto_connection_stop(bd_addr_type_t address_type, const bd_addr_t address); 746 747 /** 748 * @brief Auto Connection Establishment - Stop everything 749 * @deprecated Please setup Whitelist with gap_whitelist_* and start connecting with gap_connect_with_whitelist 750 * @note Convenience function to stop all active auto connection attempts 751 */ 752 uint8_t gap_auto_connection_stop_all(void); 753 754 /** 755 * @brief Set LE PHY 756 * @param con_handle 757 * @param all_phys 0 = set rx/tx, 1 = set only rx, 2 = set only tx 758 * @param tx_phys 1 = 1M, 2 = 2M, 4 = Coded 759 * @param rx_phys 1 = 1M, 2 = 2M, 4 = Coded 760 * @param phy_options 0 = no preferred coding for Coded, 1 = S=2 coding (500 kbit), 2 = S=8 coding (125 kbit) 761 * @return 0 if ok 762 */ 763 uint8_t gap_le_set_phy(hci_con_handle_t con_handle, uint8_t all_phys, uint8_t tx_phys, uint8_t rx_phys, uint8_t phy_options); 764 765 /** 766 * @brief Get connection interval 767 * @param con_handle 768 * @return connection interval, otherwise 0 if error 769 */ 770 uint16_t gap_le_connection_interval(hci_con_handle_t con_handle); 771 772 /** 773 * 774 * @brief Get encryption key size. 775 * @param con_handle 776 * @return 0 if not encrypted, 7-16 otherwise 777 */ 778 uint8_t gap_encryption_key_size(hci_con_handle_t con_handle); 779 780 /** 781 * @brief Get authentication property. 782 * @param con_handle 783 * @return 1 if bonded with OOB/Passkey (AND MITM protection) 784 */ 785 bool gap_authenticated(hci_con_handle_t con_handle); 786 787 /** 788 * @brief Get secure connection property 789 * @param con_handle 790 * @return 1 if bonded usiung LE Secure Connections 791 */ 792 bool gap_secure_connection(hci_con_handle_t con_handle); 793 794 /** 795 * @brief Queries authorization state. 796 * @param con_handle 797 * @return authorization_state for the current session 798 */ 799 authorization_state_t gap_authorization_state(hci_con_handle_t con_handle); 800 801 /** 802 * @brief Get bonded property (BR/EDR/LE) 803 * @note LE: has to be called after identity resolving is complete 804 * @param con_handle 805 * @return true if bonded 806 */ 807 bool gap_bonded(hci_con_handle_t con_handle); 808 809 // Classic 810 #ifdef ENABLE_CLASSIC 811 812 /** 813 * @brief Override page scan mode. Page scan mode enabled by l2cap when services are registered 814 * @note Might be used to reduce power consumption while Bluetooth module stays powered but no (new) 815 * connections are expected 816 */ 817 void gap_connectable_control(uint8_t enable); 818 819 /** 820 * @brief Allows to control if device is discoverable. OFF by default. 821 */ 822 void gap_discoverable_control(uint8_t enable); 823 824 /** 825 * @brief Deletes link key for remote device with baseband address. 826 * @param addr 827 * @note On most desktop ports, the Link Key DB uses a TLV and there is one TLV storage per 828 * Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during 829 * power up, this function only works, when the stack is in working state for these ports. 830 */ 831 void gap_drop_link_key_for_bd_addr(bd_addr_t addr); 832 833 /** 834 * @brief Delete all stored link keys 835 * @note On most desktop ports, the Link Key DB uses a TLV and there is one TLV storage per 836 * Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during 837 * power up, this function only works, when the stack is in working state for these ports. 838 */ 839 void gap_delete_all_link_keys(void); 840 841 /** 842 * @brief Store link key for remote device with baseband address 843 * @param addr 844 * @param link_key 845 * @param link_key_type 846 * @note On most desktop ports, the Link Key DB uses a TLV and there is one TLV storage per 847 * Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during 848 * power up, this function only works, when the stack is in working state for these ports. 849 */ 850 void gap_store_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t type); 851 852 /** 853 * @brief Get link for remote device with basband address 854 * @param addr 855 * @param link_key (out) is stored here 856 * @param link_key_type (out) is stored here 857 * @note On most desktop ports, the Link Key DB uses a TLV and there is one TLV storage per 858 * Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during 859 * power up, this function only works, when the stack is in working state for these ports. 860 */ 861 bool gap_get_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t * type); 862 863 /** 864 * @brief Setup Link Key iterator 865 * @param it 866 * @return 1 on success 867 * @note On most desktop ports, the Link Key DB uses a TLV and there is one TLV storage per 868 * Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during 869 * power up, this function only works, when the stack is in working state for these ports. 870 */ 871 int gap_link_key_iterator_init(btstack_link_key_iterator_t * it); 872 873 /** 874 * @brief Get next Link Key 875 * @param it 876 * @brief addr 877 * @brief link_key 878 * @brief type of link key 879 * @return 1, if valid link key found 880 * @see note on gap_link_key_iterator_init 881 */ 882 int gap_link_key_iterator_get_next(btstack_link_key_iterator_t * it, bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * type); 883 884 /** 885 * @brief Frees resources allocated by iterator_init 886 * @note Must be called after iteration to free resources 887 * @param it 888 * @see note on gap_link_key_iterator_init 889 */ 890 void gap_link_key_iterator_done(btstack_link_key_iterator_t * it); 891 892 /** 893 * @brief Start GAP Classic Inquiry 894 * @param duration in 1.28s units 895 * @return 0 if ok 896 * @events: GAP_EVENT_INQUIRY_RESULT, GAP_EVENT_INQUIRY_COMPLETE 897 */ 898 int gap_inquiry_start(uint8_t duration_in_1280ms_units); 899 900 /** 901 * @brief Stop GAP Classic Inquiry 902 * @brief Stop GAP Classic Inquiry 903 * @return 0 if ok 904 * @events: GAP_EVENT_INQUIRY_COMPLETE 905 */ 906 int gap_inquiry_stop(void); 907 908 /** 909 * @brief Set LAP for GAP Classic Inquiry 910 * @param lap GAP_IAC_GENERAL_INQUIRY (default), GAP_IAC_LIMITED_INQUIRY 911 */ 912 void gap_inquiry_set_lap(uint32_t lap); 913 914 /** 915 * @brief Set Inquiry Scan Activity 916 * @param inquiry_scan_interval range: 0x0012 to 0x1000; only even values are valid, Time = N * 0.625 ms 917 * @param inquiry_scan_window range: 0x0011 to 0x1000; Time = N * 0.625 ms 918 */ 919 void gap_inquiry_set_scan_activity(uint16_t inquiry_scan_interval, uint16_t inquiry_scan_window); 920 921 /** 922 * @brief Remote Name Request 923 * @param addr 924 * @param page_scan_repetition_mode 925 * @param clock_offset only used when bit 15 is set - pass 0 if not known 926 * @events: HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 927 */ 928 int gap_remote_name_request(const bd_addr_t addr, uint8_t page_scan_repetition_mode, uint16_t clock_offset); 929 930 /** 931 * @brief Legacy Pairing Pin Code Response 932 * @note data is not copied, pointer has to stay valid 933 * @param addr 934 * @param pin 935 * @return 0 if ok 936 */ 937 int gap_pin_code_response(const bd_addr_t addr, const char * pin); 938 939 /** 940 * @brief Legacy Pairing Pin Code Response for binary data / non-strings 941 * @note data is not copied, pointer has to stay valid 942 * @param addr 943 * @param pin_data 944 * @param pin_len 945 * @return 0 if ok 946 */ 947 int gap_pin_code_response_binary(const bd_addr_t addr, const uint8_t * pin_data, uint8_t pin_len); 948 949 /** 950 * @brief Abort Legacy Pairing 951 * @param addr 952 * @param pin 953 * @return 0 if ok 954 */ 955 int gap_pin_code_negative(bd_addr_t addr); 956 957 /** 958 * @brief SSP Passkey Response 959 * @param addr 960 * @param passkey [0..999999] 961 * @return 0 if ok 962 */ 963 int gap_ssp_passkey_response(const bd_addr_t addr, uint32_t passkey); 964 965 /** 966 * @brief Abort SSP Passkey Entry/Pairing 967 * @param addr 968 * @param pin 969 * @return 0 if ok 970 */ 971 int gap_ssp_passkey_negative(const bd_addr_t addr); 972 973 /** 974 * @brief Accept SSP Numeric Comparison 975 * @param addr 976 * @param passkey 977 * @return 0 if ok 978 */ 979 int gap_ssp_confirmation_response(const bd_addr_t addr); 980 981 /** 982 * @brief Abort SSP Numeric Comparison/Pairing 983 * @param addr 984 * @param pin 985 * @return 0 if ok 986 */ 987 int gap_ssp_confirmation_negative(const bd_addr_t addr); 988 989 /** 990 * @brief Generate new OOB data 991 * @note OOB data will be provided in GAP_EVENT_LOCAL_OOB_DATA and be used in future pairing procedures 992 */ 993 void gap_ssp_generate_oob_data(void); 994 995 /** 996 * @brief Report Remote OOB Data 997 * @note Pairing Hash and Randomizer are expected in big-endian byte format 998 * @param bd_addr 999 * @param c_192 Simple Pairing Hash C derived from P-192 public key 1000 * @param r_192 Simple Pairing Randomizer derived from P-192 public key 1001 * @param c_256 Simple Pairing Hash C derived from P-256 public key 1002 * @param r_256 Simple Pairing Randomizer derived from P-256 public key 1003 */ 1004 uint8_t gap_ssp_remote_oob_data(const bd_addr_t addr, const uint8_t * c_192, const uint8_t * r_192, const uint8_t * c_256, const uint8_t * r_256); 1005 1006 /** 1007 * Send SSP IO Capabilities Reply 1008 * @note IO Capabilities (Negative) Reply is sent automatically unless ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 1009 * @param addr 1010 * @return 0 if ok 1011 */ 1012 uint8_t gap_ssp_io_capabilities_response(const bd_addr_t addr); 1013 1014 /** 1015 * Send SSP IO Capabilities Negative Reply 1016 * @note IO Capabilities (Negative) Reply is sent automatically unless ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 1017 * @param addr 1018 * @return 0 if ok 1019 */ 1020 uint8_t gap_ssp_io_capabilities_negative(const bd_addr_t addr); 1021 1022 /** 1023 * Send Link Key Reponse 1024 * @note Link Key (Negative) Reply is sent automaticallyu unless ENABLE_EXPLICIT_LINK_KEY_RESPONSE 1025 * @param addr 1026 * @param link_key 1027 * @param type or INVALID_LINK_KEY if link key not available 1028 * @return 0 if ok 1029 */ 1030 uint8_t gap_send_link_key_response(const bd_addr_t addr, link_key_t link_key, link_key_type_t type); 1031 1032 /** 1033 * @brief Enter Sniff mode 1034 * @param con_handle 1035 * @param sniff_min_interval range: 0x0002 to 0xFFFE; only even values are valid, Time = N * 0.625 ms 1036 * @param sniff_max_interval range: 0x0002 to 0xFFFE; only even values are valid, Time = N * 0.625 ms 1037 * @param sniff_attempt Number of Baseband receive slots for sniff attempt. 1038 * @param sniff_timeout Number of Baseband receive slots for sniff timeout. 1039 @ @return 0 if ok 1040 */ 1041 uint8_t gap_sniff_mode_enter(hci_con_handle_t con_handle, uint16_t sniff_min_interval, uint16_t sniff_max_interval, uint16_t sniff_attempt, uint16_t sniff_timeout); 1042 1043 /** 1044 * @brief Exit Sniff mode 1045 * @param con_handle 1046 @ @return 0 if ok 1047 */ 1048 uint8_t gap_sniff_mode_exit(hci_con_handle_t con_handle); 1049 1050 /** 1051 * @brief Configure Sniff Subrating 1052 * @param con_handle 1053 * @param max_latency range: 0x0002 to 0xFFFE; Time = N * 0.625 ms 1054 * @param min_remote_timeout range: 0x0002 to 0xFFFE; Time = N * 0.625 ms 1055 * @param min_local_timeout range: 0x0002 to 0xFFFE; Time = N * 0.625 ms 1056 @ @return 0 if ok 1057 */ 1058 uint8_t gap_sniff_subrating_configure(hci_con_handle_t con_handle, uint16_t max_latency, uint16_t min_remote_timeout, uint16_t min_local_timeout); 1059 1060 /** 1061 * @Brief Set QoS 1062 * @param con_handle 1063 * @param service_type 1064 * @param token_rate 1065 * @param peak_bandwidth 1066 * @param latency 1067 * @param delay_variation 1068 @ @return 0 if ok 1069 */ 1070 uint8_t gap_qos_set(hci_con_handle_t con_handle, hci_service_type_t service_type, uint32_t token_rate, uint32_t peak_bandwidth, uint32_t latency, uint32_t delay_variation); 1071 1072 #endif 1073 1074 // LE 1075 1076 /** 1077 * @brief Get own addr type and address used for LE for next scan/advertisement/connect operation 1078 */ 1079 void gap_le_get_own_address(uint8_t * addr_type, bd_addr_t addr); 1080 1081 /** 1082 * @brief Get own addr type and address used for LE advertisements (Peripheral) 1083 */ 1084 void gap_le_get_own_advertisements_address(uint8_t * addr_type, bd_addr_t addr); 1085 1086 /** 1087 * @brief Get own addr type and address used for LE connections (Central) 1088 */ 1089 void gap_le_get_own_connection_address(uint8_t * addr_type, bd_addr_t addr); 1090 1091 /** 1092 * @brief Get state of connection re-encryption for bonded devices when in central role 1093 * @note used by gatt_client and att_server to wait for re-encryption 1094 * @param con_handle 1095 * @return 1 if security setup is active 1096 */ 1097 int gap_reconnect_security_setup_active(hci_con_handle_t con_handle); 1098 1099 /** 1100 * @brief Delete bonding information for remote device 1101 * @note On most desktop ports, the LE Device DB uses a TLV and there is one TLV storage per 1102 * Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during 1103 * power up, this function only works, when the stack is in working state for these ports. 1104 * @param address_type 1105 * @param address 1106 */ 1107 void gap_delete_bonding(bd_addr_type_t address_type, bd_addr_t address); 1108 1109 /** 1110 * LE Privacy 1.2 - requires support by Controller and ENABLE_LE_RESOLVING_LIST to be defined 1111 */ 1112 1113 /** 1114 * @brief Load LE Device DB entries into Controller Resolving List to allow filtering on 1115 * bonded devies with resolvable private addresses 1116 * @return EROOR_CODE_SUCCESS if supported by Controller 1117 */ 1118 uint8_t gap_load_resolving_list_from_le_device_db(void); 1119 1120 /** 1121 * @brief Get local persistent IRK 1122 */ 1123 const uint8_t * gap_get_persistent_irk(void); 1124 1125 /* API_END*/ 1126 1127 #if defined __cplusplus 1128 } 1129 #endif 1130 1131 #endif // GAP_H 1132