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 MATTHIAS 24 * RINGWALD 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 #ifndef __GAP_H 39 #define __GAP_H 40 41 #if defined __cplusplus 42 extern "C" { 43 #endif 44 45 #include "btstack_defines.h" 46 #include "btstack_util.h" 47 #include "classic/btstack_link_key_db.h" 48 49 typedef enum { 50 51 // MITM protection not required 52 // No encryption required 53 // No user interaction required 54 LEVEL_0 = 0, 55 56 // MITM protection not required 57 // No encryption required 58 // Minimal user interaction desired 59 LEVEL_1, 60 61 // MITM protection not required 62 // Encryption required 63 LEVEL_2, 64 65 // MITM protection required 66 // Encryption required 67 // User interaction acceptable 68 LEVEL_3, 69 70 // MITM protection required 71 // Encryption required 72 // 128-bit equivalent strength for link and encryption keys required (P-192 is not enough) 73 // User interaction acceptable 74 LEVEL_4, 75 } gap_security_level_t; 76 77 typedef enum { 78 GAP_SECURITY_NONE, 79 GAP_SECUIRTY_ENCRYPTED, // SSP: JUST WORKS 80 GAP_SECURITY_AUTHENTICATED, // SSP: numeric comparison, passkey, OOB 81 // GAP_SECURITY_AUTHORIZED 82 } gap_security_state; 83 84 typedef enum { 85 GAP_CONNECTION_INVALID, 86 GAP_CONNECTION_ACL, 87 GAP_CONNECTION_SCO, 88 GAP_CONNECTION_LE 89 } gap_connection_type_t; 90 91 typedef struct le_connection_parameter_range{ 92 uint16_t le_conn_interval_min; 93 uint16_t le_conn_interval_max; 94 uint16_t le_conn_latency_min; 95 uint16_t le_conn_latency_max; 96 uint16_t le_supervision_timeout_min; 97 uint16_t le_supervision_timeout_max; 98 } le_connection_parameter_range_t; 99 100 typedef enum { 101 GAP_RANDOM_ADDRESS_TYPE_OFF = 0, 102 GAP_RANDOM_ADDRESS_TYPE_STATIC, 103 GAP_RANDOM_ADDRESS_NON_RESOLVABLE, 104 GAP_RANDOM_ADDRESS_RESOLVABLE, 105 } gap_random_address_type_t; 106 107 // Authorization state 108 typedef enum { 109 AUTHORIZATION_UNKNOWN, 110 AUTHORIZATION_PENDING, 111 AUTHORIZATION_DECLINED, 112 AUTHORIZATION_GRANTED 113 } authorization_state_t; 114 115 116 /* API_START */ 117 118 // Classic + LE 119 120 /** 121 * @brief Disconnect connection with handle 122 * @param handle 123 */ 124 uint8_t gap_disconnect(hci_con_handle_t handle); 125 126 /** 127 * @brief Get connection type 128 * @param con_handle 129 * @result connection_type 130 */ 131 gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle); 132 133 // Classic 134 135 /** 136 * @brief Sets local name. 137 * @note has to be done before stack starts up 138 * @note Default name is 'BTstack 00:00:00:00:00:00' 139 * @note '00:00:00:00:00:00' in local_name will be replaced with actual bd addr 140 * @param name is not copied, make sure memory is accessible during stack startup 141 */ 142 void gap_set_local_name(const char * local_name); 143 144 /** 145 * @brief Set Extended Inquiry Response data 146 * @note has to be done before stack starts up 147 * @note If not set, local name will be used for EIR data (see gap_set_local_name) 148 * @note '00:00:00:00:00:00' in local_name will be replaced with actual bd addr 149 * @param eir_data size 240 bytes, is not copied make sure memory is accessible during stack startup 150 */ 151 void gap_set_extended_inquiry_response(const uint8_t * data); 152 153 /** 154 * @brief Set class of device that will be set during Bluetooth init. 155 * @note has to be done before stack starts up 156 */ 157 void gap_set_class_of_device(uint32_t class_of_device); 158 159 /** 160 * @brief Enable/disable bonding. Default is enabled. 161 * @param enabled 162 */ 163 void gap_set_bondable_mode(int enabled); 164 165 /** 166 * @brief Get bondable mode. 167 * @return 1 if bondable 168 */ 169 int gap_get_bondable_mode(void); 170 171 /* Configure Secure Simple Pairing */ 172 173 /** 174 * @brief Enable will enable SSP during init. 175 */ 176 void gap_ssp_set_enable(int enable); 177 178 /** 179 * @brief Set IO Capability. BTstack will return capability to SSP requests 180 */ 181 void gap_ssp_set_io_capability(int ssp_io_capability); 182 183 /** 184 * @brief Set Authentication Requirements using during SSP 185 */ 186 void gap_ssp_set_authentication_requirement(int authentication_requirement); 187 188 /** 189 * @brief If set, BTstack will confirm a numeric comparison and enter '000000' if requested. 190 */ 191 void gap_ssp_set_auto_accept(int auto_accept); 192 193 /** 194 * @brief Start dedicated bonding with device. Disconnect after bonding. 195 * @param device 196 * @param request MITM protection 197 * @return error, if max num acl connections active 198 * @result GAP_DEDICATED_BONDING_COMPLETE 199 */ 200 int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required); 201 202 gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type); 203 gap_security_level_t gap_security_level(hci_con_handle_t con_handle); 204 205 void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t level); 206 207 int gap_mitm_protection_required_for_security_level(gap_security_level_t level); 208 209 // LE 210 211 /** 212 * @brief Set parameters for LE Scan 213 */ 214 void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window); 215 216 /** 217 * @brief Start LE Scan 218 */ 219 void gap_start_scan(void); 220 221 /** 222 * @brief Stop LE Scan 223 */ 224 void gap_stop_scan(void); 225 226 /** 227 * @brief Enable privacy by using random addresses 228 * @param random_address_type to use (incl. OFF) 229 */ 230 void gap_random_address_set_mode(gap_random_address_type_t random_address_type); 231 232 /** 233 * @brief Get privacy mode 234 */ 235 gap_random_address_type_t gap_random_address_get_mode(void); 236 237 /** 238 * @brief Sets update period for random address 239 * @param period_ms in ms 240 */ 241 void gap_random_address_set_update_period(int period_ms); 242 243 /** 244 * @brief Sets a fixed random address for advertising 245 * @param addr 246 * @note Sets random address mode to type off 247 */ 248 void gap_random_address_set(bd_addr_t addr); 249 250 /** 251 * @brief Set Advertisement Data 252 * @param advertising_data_length 253 * @param advertising_data (max 31 octets) 254 * @note data is not copied, pointer has to stay valid 255 * @note '00:00:00:00:00:00' in advertising_data will be replaced with actual bd addr 256 */ 257 void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data); 258 259 /** 260 * @brief Set Advertisement Paramters 261 * @param adv_int_min 262 * @param adv_int_max 263 * @param adv_type 264 * @param direct_address_type 265 * @param direct_address 266 * @param channel_map 267 * @param filter_policy 268 * @note own_address_type is used from gap_random_address_set_mode 269 */ 270 void gap_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 271 uint8_t direct_address_typ, bd_addr_t direct_address, uint8_t channel_map, uint8_t filter_policy); 272 273 /** 274 * @brief Enable/Disable Advertisements. OFF by default. 275 * @param enabled 276 */ 277 void gap_advertisements_enable(int enabled); 278 279 /** 280 * @brief Set Scan Response Data 281 * 282 * @note For scan response data, scannable undirected advertising (ADV_SCAN_IND) need to be used 283 * 284 * @param advertising_data_length 285 * @param advertising_data (max 31 octets) 286 * @note data is not copied, pointer has to stay valid 287 * @note '00:00:00:00:00:00' in scan_response_data will be replaced with actual bd addr 288 */ 289 void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data); 290 291 /** 292 * @brief Set connection parameters for outgoing connections 293 * @param conn_scan_interval (unit: 0.625 msec), default: 60 ms 294 * @param conn_scan_window (unit: 0.625 msec), default: 30 ms 295 * @param conn_interval_min (unit: 1.25ms), default: 10 ms 296 * @param conn_interval_max (unit: 1.25ms), default: 30 ms 297 * @param conn_latency, default: 4 298 * @param supervision_timeout (unit: 10ms), default: 720 ms 299 * @param min_ce_length (unit: 0.625ms), default: 10 ms 300 * @param max_ce_length (unit: 0.625ms), default: 30 ms 301 */ 302 void gap_set_connection_parameters(uint16_t conn_scan_interval, uint16_t conn_scan_window, 303 uint16_t conn_interval_min, uint16_t conn_interval_max, uint16_t conn_latency, 304 uint16_t supervision_timeout, uint16_t min_ce_length, uint16_t max_ce_length); 305 306 /** 307 * @brief Request an update of the connection parameter for a given LE connection 308 * @param handle 309 * @param conn_interval_min (unit: 1.25ms) 310 * @param conn_interval_max (unit: 1.25ms) 311 * @param conn_latency 312 * @param supervision_timeout (unit: 10ms) 313 * @returns 0 if ok 314 */ 315 int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min, 316 uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout); 317 318 /** 319 * @brief Updates the connection parameters for a given LE connection 320 * @param handle 321 * @param conn_interval_min (unit: 1.25ms) 322 * @param conn_interval_max (unit: 1.25ms) 323 * @param conn_latency 324 * @param supervision_timeout (unit: 10ms) 325 * @returns 0 if ok 326 */ 327 int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min, 328 uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout); 329 330 /** 331 * @brief Set accepted connection parameter range 332 * @param range 333 */ 334 void gap_get_connection_parameter_range(le_connection_parameter_range_t * range); 335 336 /** 337 * @brief Get accepted connection parameter range 338 * @param range 339 */ 340 void gap_set_connection_parameter_range(le_connection_parameter_range_t * range); 341 342 /** 343 * @brief Set max number of connections in LE Peripheral role (if Bluetooth Controller supports it) 344 * @note: default: 1 345 * @param max_peripheral_connections 346 */ 347 void gap_set_max_number_peripheral_connections(int max_peripheral_connections); 348 349 /** 350 * @brief Connect to remote LE device 351 */ 352 uint8_t gap_connect(bd_addr_t addr, bd_addr_type_t addr_type); 353 354 /** 355 * @brief Cancel connection process initiated by gap_connect 356 */ 357 uint8_t gap_connect_cancel(void); 358 359 /** 360 * @brief Auto Connection Establishment - Start Connecting to device 361 * @param address_typ 362 * @param address 363 * @returns 0 if ok 364 */ 365 int gap_auto_connection_start(bd_addr_type_t address_typ, bd_addr_t address); 366 367 /** 368 * @brief Auto Connection Establishment - Stop Connecting to device 369 * @param address_typ 370 * @param address 371 * @returns 0 if ok 372 */ 373 int gap_auto_connection_stop(bd_addr_type_t address_typ, bd_addr_t address); 374 375 /** 376 * @brief Auto Connection Establishment - Stop everything 377 * @note Convenience function to stop all active auto connection attempts 378 */ 379 void gap_auto_connection_stop_all(void); 380 381 /** 382 * 383 * @brief Get encryption key size. 384 * @param con_handle 385 * @return 0 if not encrypted, 7-16 otherwise 386 */ 387 int gap_encryption_key_size(hci_con_handle_t con_handle); 388 389 /** 390 * @brief Get authentication property. 391 * @param con_handle 392 * @return 1 if bonded with OOB/Passkey (AND MITM protection) 393 */ 394 int gap_authenticated(hci_con_handle_t con_handle); 395 396 /** 397 * @brief Queries authorization state. 398 * @param con_handle 399 * @return authorization_state for the current session 400 */ 401 authorization_state_t gap_authorization_state(hci_con_handle_t con_handle); 402 403 // Classic 404 405 /** 406 * @brief Override page scan mode. Page scan mode enabled by l2cap when services are registered 407 * @note Might be used to reduce power consumption while Bluetooth module stays powered but no (new) 408 * connections are expected 409 */ 410 void gap_connectable_control(uint8_t enable); 411 412 /** 413 * @brief Allows to control if device is discoverable. OFF by default. 414 */ 415 void gap_discoverable_control(uint8_t enable); 416 417 /** 418 * @brief Gets local address. 419 */ 420 void gap_local_bd_addr(bd_addr_t address_buffer); 421 422 /** 423 * @brief Deletes link key for remote device with baseband address. 424 * @param addr 425 */ 426 void gap_drop_link_key_for_bd_addr(bd_addr_t addr); 427 428 /** 429 * @brief Delete all stored link keys 430 */ 431 void gap_delete_all_link_keys(void); 432 433 /** 434 * @brief Store link key for remote device with baseband address 435 * @param addr 436 * @param link_key 437 * @param link_key_type 438 */ 439 void gap_store_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t type); 440 441 /** 442 * @brief Setup Link Key iterator 443 * @param it 444 * @returns 1 on success 445 */ 446 int gap_link_key_iterator_init(btstack_link_key_iterator_t * it); 447 448 /** 449 * @brief Get next Link Key 450 * @param it 451 * @brief addr 452 * @brief link_key 453 * @brief type of link key 454 * @returns 1, if valid link key found 455 */ 456 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); 457 458 /** 459 * @brief Frees resources allocated by iterator_init 460 * @note Must be called after iteration to free resources 461 * @param it 462 */ 463 void gap_link_key_iterator_done(btstack_link_key_iterator_t * it); 464 465 /** 466 * @brief Start GAP Classic Inquiry 467 * @param duration in 1.28s units 468 * @return 0 if ok 469 * @events: GAP_EVENT_INQUIRY_RESULT, GAP_EVENT_INQUIRY_COMPLETE 470 */ 471 int gap_inquiry_start(uint8_t duration_in_1280ms_units); 472 473 /** 474 * @brief Stop GAP Classic Inquiry 475 * @brief Stop GAP Classic Inquiry 476 * @returns 0 if ok 477 * @events: GAP_EVENT_INQUIRY_COMPLETE 478 */ 479 int gap_inquiry_stop(void); 480 481 /** 482 * @brief Remote Name Request 483 * @param addr 484 * @param page_scan_repetition_mode 485 * @param clock_offset only used when bit 15 is set - pass 0 if not known 486 * @events: HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 487 */ 488 int gap_remote_name_request(bd_addr_t addr, uint8_t page_scan_repetition_mode, uint16_t clock_offset); 489 490 /** 491 * @brief Legacy Pairing Pin Code Response 492 * @param addr 493 * @param pin 494 * @return 0 if ok 495 */ 496 int gap_pin_code_response(bd_addr_t addr, const char * pin); 497 498 /** 499 * @brief Abort Legacy Pairing 500 * @param addr 501 * @param pin 502 * @return 0 if ok 503 */ 504 int gap_pin_code_negative(bd_addr_t addr); 505 506 /** 507 * @brief SSP Passkey Response 508 * @param addr 509 * @param passkey [0..999999] 510 * @return 0 if ok 511 */ 512 int gap_ssp_passkey_response(bd_addr_t addr, uint32_t passkey); 513 514 /** 515 * @brief Abort SSP Passkey Entry/Pairing 516 * @param addr 517 * @param pin 518 * @return 0 if ok 519 */ 520 int gap_ssp_passkey_negative(bd_addr_t addr); 521 522 /** 523 * @brief Accept SSP Numeric Comparison 524 * @param addr 525 * @param passkey 526 * @return 0 if ok 527 */ 528 int gap_ssp_confirmation_response(bd_addr_t addr); 529 530 /** 531 * @brief Abort SSP Numeric Comparison/Pairing 532 * @param addr 533 * @param pin 534 * @return 0 if ok 535 */ 536 int gap_ssp_confirmation_negative(bd_addr_t addr); 537 538 539 540 // LE 541 542 /** 543 * @brief Get own addr type and address used for LE 544 */ 545 void gap_le_get_own_address(uint8_t * addr_type, bd_addr_t addr); 546 547 548 /* API_END*/ 549 550 #if defined __cplusplus 551 } 552 #endif 553 554 #endif // __GAP_H 555