xref: /btstack/src/gap.h (revision 34fd36da1820bede7fdeb1f09f53c180688c8c25)
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 /**
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 
139 /* API_START */
140 
141 // Classic + LE
142 
143 /**
144  * @brief Read RSSI
145  * @param con_handle
146  * @events: GAP_EVENT_RSSI_MEASUREMENT
147  */
148 int gap_read_rssi(hci_con_handle_t con_handle);
149 
150 
151 /**
152  * @brief Gets local address.
153  */
154 void gap_local_bd_addr(bd_addr_t address_buffer);
155 
156 /**
157  * @brief Disconnect connection with handle
158  * @param handle
159  */
160 uint8_t gap_disconnect(hci_con_handle_t handle);
161 
162 /**
163  * @brief Get connection type
164  * @param con_handle
165  * @result connection_type
166  */
167 gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle);
168 
169 /**
170  * @brief Get HCI connection role
171  * @param con_handle
172  * @result hci_role_t HCI_ROLE_MASTER / HCI_ROLE_SLAVE / HCI_ROLE_INVALID (if connection does not exist)
173  */
174 hci_role_t gap_get_role(hci_con_handle_t connection_handle);
175 
176 // Classic
177 
178 /**
179  * @brief Request role switch
180  * @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
181  * @param addr
182  * @param hci_role_t HCI_ROLE_MASTER / HCI_ROLE_SLAVE
183  * @result status
184  */
185 uint8_t gap_request_role(const bd_addr_t addr, hci_role_t role);
186 
187 /**
188  * @brief Sets local name.
189  * @note has to be done before stack starts up
190  * @note Default name is 'BTstack 00:00:00:00:00:00'
191  * @note '00:00:00:00:00:00' in local_name will be replaced with actual bd addr
192  * @param name is not copied, make sure memory is accessible during stack startup
193  */
194 void gap_set_local_name(const char * local_name);
195 
196 /**
197  * @brief Set Extended Inquiry Response data
198  * @note has to be done before stack starts up
199  * @note If not set, local name will be used for EIR data (see gap_set_local_name)
200  * @note '00:00:00:00:00:00' in local_name will be replaced with actual bd addr
201  * @param eir_data size HCI_EXTENDED_INQUIRY_RESPONSE_DATA_LEN (240) bytes, is not copied make sure memory is accessible during stack startup
202  */
203 void gap_set_extended_inquiry_response(const uint8_t * data);
204 
205 /**
206  * @brief Set class of device that will be set during Bluetooth init.
207  * @note has to be done before stack starts up
208  */
209 void gap_set_class_of_device(uint32_t class_of_device);
210 
211 /**
212  * @brief Set default link policy settings for all classic ACL links
213  * @param default_link_policy_settings - see LM_LINK_POLICY_* in bluetooth.h
214  * @note common value: LM_LINK_POLICY_ENABLE_ROLE_SWITCH | LM_LINK_POLICY_ENABLE_SNIFF_MODE to enable role switch and sniff mode
215  * @note has to be done before stack starts up
216  */
217 void gap_set_default_link_policy_settings(uint16_t default_link_policy_settings);
218 
219 /**
220  * @brief Set Allow Role Switch param for outgoing classic ACL links
221  * @param allow_role_switch - true: allow remote device to request role switch, false: stay master
222  */
223 void gap_set_allow_role_switch(bool allow_role_switch);
224 
225 /**
226  * @brief Set  link supervision timeout for outgoing classic ACL links
227  * @param default_link_supervision_timeout * 0.625 ms, default 0x7d00 = 20 seconds, 0 = no link supervision timeout
228  */
229 void gap_set_link_supervision_timeout(uint16_t link_supervision_timeout);
230 
231 /**
232  * @brief Enable/disable bonding. Default is enabled.
233  * @param enabled
234  */
235 void gap_set_bondable_mode(int enabled);
236 
237 /**
238  * @brief Get bondable mode.
239  * @return 1 if bondable
240  */
241 int gap_get_bondable_mode(void);
242 
243 /**
244  * @brief Set security mode for all outgoing and incoming connections. Default: GAP_SECURITY_MODE_4
245  * @param security_mode is GAP_SECURITY_MODE_2 or GAP_SECURITY_MODE_4
246  */
247 void gap_set_security_mode(gap_security_mode_t security_mode);
248 
249 /**
250  * @brief Get security mode
251  * @return security_mode
252  */
253 gap_security_mode_t gap_get_security_mode(void);
254 
255 /**
256  * @brief Set security level for all outgoing and incoming connections. Default: LEVEL_2
257  * @param security_level
258  * @note has to be called before services or profiles are initialized
259  */
260 void gap_set_security_level(gap_security_level_t security_level);
261 
262 /**
263  * @brief Get security level
264  * @return security_level
265  */
266 gap_security_level_t gap_get_security_level(void);
267 
268 /**
269  * @brief Set Secure Connections Only Mode for BR/EDR (Classic) Default: false
270  * @param enable
271  */
272 void gap_set_secure_connections_only_mode(bool enable);
273 
274 /**
275  * @breif Get Secure Connections Only Mode
276  * @param enabled
277  */
278 bool gap_get_secure_connections_only_mode(void);
279 
280 /**
281  * @brief Register filter for rejecting classic connections. Callback will return 1 accept connection, 0 on reject.
282  */
283 void gap_register_classic_connection_filter(int (*accept_callback)(bd_addr_t addr, hci_link_type_t link_type));
284 
285 /* Configure Secure Simple Pairing */
286 
287 /**
288  * @brief Enable will enable SSP during init. Default: true
289  */
290 void gap_ssp_set_enable(int enable);
291 
292 /**
293  * @brief Set IO Capability. BTstack will return capability to SSP requests
294  */
295 void gap_ssp_set_io_capability(int ssp_io_capability);
296 
297 /**
298  * @brief Set Authentication Requirements using during SSP
299  */
300 void gap_ssp_set_authentication_requirement(int authentication_requirement);
301 
302 /**
303  * @brief Enable/disable Secure Connections. Default: true if supported by Controller
304  */
305 void gap_secure_connections_enable(bool enable);
306 
307 /**
308  * @brief If set, BTstack will confirm a numeric comparison and enter '000000' if requested.
309  */
310 void gap_ssp_set_auto_accept(int auto_accept);
311 
312 /**
313  * @brief Set required encryption key size for GAP Levels 1-3 on ccassic connections. Default: 16 bytes
314  * @param encryption_key_size in bytes. Valid 7..16
315  */
316 void gap_set_required_encryption_key_size(uint8_t encryption_key_size);
317 
318 /**
319  * @brief Start dedicated bonding with device. Disconnect after bonding.
320  * @param device
321  * @param request MITM protection
322  * @return error, if max num acl connections active
323  * @result GAP_DEDICATED_BONDING_COMPLETE
324  */
325 int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required);
326 
327 gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type);
328 
329 /**
330  * @brief map link keys to secure connection yes/no
331  */
332 int gap_secure_connection_for_link_key_type(link_key_type_t link_key_type);
333 
334 /**
335  * @brief map link keys to authenticated
336  */
337 int gap_authenticated_for_link_key_type(link_key_type_t link_key_type);
338 
339 gap_security_level_t gap_security_level(hci_con_handle_t con_handle);
340 
341 void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t level);
342 
343 int  gap_mitm_protection_required_for_security_level(gap_security_level_t level);
344 
345 /**
346  * @brief Set Page Scan Type
347  * @param page_scan_interval * 0.625 ms, range: 0x0012..0x1000, default: 0x0800
348  * @param page_scan_windows  * 0.625 ms, range: 0x0011..page_scan_interval, default: 0x0012
349  */
350 void gap_set_page_scan_activity(uint16_t page_scan_interval, uint16_t page_scan_window);
351 
352 /**
353  * @brief Set Page Scan Type
354  * @param page_scan_mode
355  */
356 void gap_set_page_scan_type(page_scan_type_t page_scan_type);
357 
358 // LE
359 
360 /**
361  * @brief Set parameters for LE Scan
362  * @param scan_type 0 = passive, 1 = active
363  * @param scan_interval range 0x0004..0x4000, unit 0.625 ms
364  * @param scan_window range 0x0004..0x4000, unit 0.625 ms
365  * @param scanning_filter_policy 0 = all devices, 1 = all from whitelist
366  */
367 void gap_set_scan_params(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window, uint8_t scanning_filter_policy);
368 
369 /**
370  * @brief Set parameters for LE Scan
371  * @deprecated Use gap_set_scan_params instead
372  */
373 void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window);
374 
375 /**
376  * @brief Start LE Scan
377  */
378 void gap_start_scan(void);
379 
380 /**
381  * @brief Stop LE Scan
382  */
383 void gap_stop_scan(void);
384 
385 /**
386  * @brief Enable privacy by using random addresses
387  * @param random_address_type to use (incl. OFF)
388  */
389 void gap_random_address_set_mode(gap_random_address_type_t random_address_type);
390 
391 /**
392  * @brief Get privacy mode
393  */
394 gap_random_address_type_t gap_random_address_get_mode(void);
395 
396 /**
397  * @brief Sets update period for random address
398  * @param period_ms in ms
399  */
400  void gap_random_address_set_update_period(int period_ms);
401 
402 /**
403  * @brief Sets a fixed random address for advertising
404  * @param addr
405  * @note Sets random address mode to type off
406  */
407 void gap_random_address_set(const bd_addr_t addr);
408 
409 /**
410  * @brief Set Advertisement Data
411  * @param advertising_data_length
412  * @param advertising_data (max 31 octets)
413  * @note data is not copied, pointer has to stay valid
414  * @note '00:00:00:00:00:00' in advertising_data will be replaced with actual bd addr
415  */
416 void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data);
417 
418 /**
419  * @brief Set Advertisement Paramters
420  * @param adv_int_min
421  * @param adv_int_max
422  * @param adv_type
423  * @param direct_address_type
424  * @param direct_address
425  * @param channel_map
426  * @param filter_policy
427  * @note own_address_type is used from gap_random_address_set_mode
428  */
429 void gap_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type,
430 	uint8_t direct_address_typ, bd_addr_t direct_address, uint8_t channel_map, uint8_t filter_policy);
431 
432 /**
433  * @brief Enable/Disable Advertisements. OFF by default.
434  * @param enabled
435  */
436 void gap_advertisements_enable(int enabled);
437 
438 /**
439  * @brief Set Scan Response Data
440  *
441  * @note For scan response data, scannable undirected advertising (ADV_SCAN_IND) need to be used
442  *
443  * @param advertising_data_length
444  * @param advertising_data (max 31 octets)
445  * @note data is not copied, pointer has to stay valid
446  * @note '00:00:00:00:00:00' in scan_response_data will be replaced with actual bd addr
447  */
448 void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data);
449 
450 /**
451  * @brief Set connection parameters for outgoing connections
452  * @param conn_scan_interval (unit: 0.625 msec), default: 60 ms
453  * @param conn_scan_window (unit: 0.625 msec), default: 30 ms
454  * @param conn_interval_min (unit: 1.25ms), default: 10 ms
455  * @param conn_interval_max (unit: 1.25ms), default: 30 ms
456  * @param conn_latency, default: 4
457  * @param supervision_timeout (unit: 10ms), default: 720 ms
458  * @param min_ce_length (unit: 0.625ms), default: 10 ms
459  * @param max_ce_length (unit: 0.625ms), default: 30 ms
460  */
461 void gap_set_connection_parameters(uint16_t conn_scan_interval, uint16_t conn_scan_window,
462     uint16_t conn_interval_min, uint16_t conn_interval_max, uint16_t conn_latency,
463     uint16_t supervision_timeout, uint16_t min_ce_length, uint16_t max_ce_length);
464 
465 /**
466  * @brief Request an update of the connection parameter for a given LE connection
467  * @param handle
468  * @param conn_interval_min (unit: 1.25ms)
469  * @param conn_interval_max (unit: 1.25ms)
470  * @param conn_latency
471  * @param supervision_timeout (unit: 10ms)
472  * @returns 0 if ok
473  */
474 int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min,
475 	uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout);
476 
477 /**
478  * @brief Updates the connection parameters for a given LE connection
479  * @param handle
480  * @param conn_interval_min (unit: 1.25ms)
481  * @param conn_interval_max (unit: 1.25ms)
482  * @param conn_latency
483  * @param supervision_timeout (unit: 10ms)
484  * @returns 0 if ok
485  */
486 int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min,
487 	uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout);
488 
489 /**
490  * @brief Set accepted connection parameter range
491  * @param range
492  */
493 void gap_get_connection_parameter_range(le_connection_parameter_range_t * range);
494 
495 /**
496  * @brief Get accepted connection parameter range
497  * @param range
498  */
499 void gap_set_connection_parameter_range(le_connection_parameter_range_t * range);
500 
501 /**
502  * @brief Test if connection parameters are inside in existing rage
503  * @param conn_interval_min (unit: 1.25ms)
504  * @param conn_interval_max (unit: 1.25ms)
505  * @param conn_latency
506  * @param supervision_timeout (unit: 10ms)
507  * @returns 1 if included
508  */
509 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);
510 
511 /**
512  * @brief Set max number of connections in LE Peripheral role (if Bluetooth Controller supports it)
513  * @note: default: 1
514  * @param max_peripheral_connections
515  */
516 void gap_set_max_number_peripheral_connections(int max_peripheral_connections);
517 
518 /**
519  * @brief Add Device to Whitelist
520  * @param address_typ
521  * @param address
522  * @returns 0 if ok
523  */
524 uint8_t gap_whitelist_add(bd_addr_type_t address_type, const bd_addr_t address);
525 
526 /**
527  * @brief Remove Device from Whitelist
528  * @param address_typ
529  * @param address
530  * @returns 0 if ok
531  */
532 uint8_t gap_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address);
533 
534 /**
535  * @brief Clear Whitelist
536  * @returns 0 if ok
537  */
538 uint8_t gap_whitelist_clear(void);
539 
540 /**
541  * @brief Connect to remote LE device
542  */
543 uint8_t gap_connect(const bd_addr_t addr, bd_addr_type_t addr_type);
544 
545 /**
546  *  @brief Connect with Whitelist
547  *  @note Explicit whitelist management and this connect with whitelist replace deprecated gap_auto_connection_* functions
548  *  @returns - if ok
549  */
550 uint8_t gap_connect_with_whitelist(void);
551 
552 /**
553  * @brief Cancel connection process initiated by gap_connect
554  */
555 uint8_t gap_connect_cancel(void);
556 
557 /**
558  * @brief Auto Connection Establishment - Start Connecting to device
559  * @deprecated Please setup Whitelist with gap_whitelist_* and start connecting with gap_connect_with_whitelist
560  * @param address_typ
561  * @param address
562  * @returns 0 if ok
563  */
564 uint8_t gap_auto_connection_start(bd_addr_type_t address_typ, const bd_addr_t address);
565 
566 /**
567  * @brief Auto Connection Establishment - Stop Connecting to device
568  * @deprecated Please setup Whitelist with gap_whitelist_* and start connecting with gap_connect_with_whitelist
569  * @param address_typ
570  * @param address
571  * @returns 0 if ok
572  */
573 uint8_t gap_auto_connection_stop(bd_addr_type_t address_typ, const bd_addr_t address);
574 
575 /**
576  * @brief Auto Connection Establishment - Stop everything
577  * @deprecated Please setup Whitelist with gap_whitelist_* and start connecting with gap_connect_with_whitelist
578  * @note  Convenience function to stop all active auto connection attempts
579  */
580 uint8_t gap_auto_connection_stop_all(void);
581 
582 /**
583  * @brief Set LE PHY
584  * @param con_handle
585  * @param all_phys 0 = set rx/tx, 1 = set only rx, 2 = set only tx
586  * @param tx_phys 1 = 1M, 2 = 2M, 4 = Coded
587  * @param rx_phys 1 = 1M, 2 = 2M, 4 = Coded
588  * @param phy_options 0 = no preferred coding for Coded, 1 = S=2 coding (500 kbit), 2 = S=8 coding (125 kbit)
589  * @returns 0 if ok
590  */
591 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);
592 
593 /**
594  * @brief Get connection interval
595  * @return connection interval, otherwise 0 if error
596  */
597 uint16_t gap_le_connection_interval(hci_con_handle_t connection_handle);
598 
599 /**
600  *
601  * @brief Get encryption key size.
602  * @param con_handle
603  * @return 0 if not encrypted, 7-16 otherwise
604  */
605 int gap_encryption_key_size(hci_con_handle_t con_handle);
606 
607 /**
608  * @brief Get authentication property.
609  * @param con_handle
610  * @return 1 if bonded with OOB/Passkey (AND MITM protection)
611  */
612 int gap_authenticated(hci_con_handle_t con_handle);
613 
614 /**
615  * @brief Get secure connection property
616  * @param con_handle
617  * @return 1 if bonded usiung LE Secure Connections
618  */
619 int gap_secure_connection(hci_con_handle_t con_handle);
620 
621 /**
622  * @brief Queries authorization state.
623  * @param con_handle
624  * @return authorization_state for the current session
625  */
626 authorization_state_t gap_authorization_state(hci_con_handle_t con_handle);
627 
628 /**
629  * @brief Get bonded property (BR/EDR/LE)
630  * @note LE: has to be called after identity resolving is complete
631  * @param con_handle
632  * @return true if bonded
633  */
634 bool gap_bonded(hci_con_handle_t con_handle);
635 
636 // Classic
637 #ifdef ENABLE_CLASSIC
638 
639 /**
640  * @brief Override page scan mode. Page scan mode enabled by l2cap when services are registered
641  * @note Might be used to reduce power consumption while Bluetooth module stays powered but no (new)
642  *       connections are expected
643  */
644 void gap_connectable_control(uint8_t enable);
645 
646 /**
647  * @brief Allows to control if device is discoverable. OFF by default.
648  */
649 void gap_discoverable_control(uint8_t enable);
650 
651 /**
652  * @brief Deletes link key for remote device with baseband address.
653  * @param addr
654  * @note On most desktop ports, the Link Key DB uses a TLV and there is one TLV storage per
655  *       Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during
656  *       power up, this function only works, when the stack is in working state for these ports.
657  */
658 void gap_drop_link_key_for_bd_addr(bd_addr_t addr);
659 
660 /**
661  * @brief Delete all stored link keys
662  * @note On most desktop ports, the Link Key DB uses a TLV and there is one TLV storage per
663  *       Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during
664  *       power up, this function only works, when the stack is in working state for these ports.
665  */
666 void gap_delete_all_link_keys(void);
667 
668 /**
669  * @brief Store link key for remote device with baseband address
670  * @param addr
671  * @param link_key
672  * @param link_key_type
673  * @note On most desktop ports, the Link Key DB uses a TLV and there is one TLV storage per
674  *       Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during
675  *       power up, this function only works, when the stack is in working state for these ports.
676  */
677 void gap_store_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t type);
678 
679 /**
680  * @brief Get link for remote device with basband address
681  * @param addr
682  * @param link_key (out) is stored here
683  * @param link_key_type (out) is stored here
684  * @note On most desktop ports, the Link Key DB uses a TLV and there is one TLV storage per
685  *       Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during
686  *       power up, this function only works, when the stack is in working state for these ports.
687  */
688 bool gap_get_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t * type);
689 
690 /**
691  * @brief Setup Link Key iterator
692  * @param it
693  * @returns 1 on success
694  * @note On most desktop ports, the Link Key DB uses a TLV and there is one TLV storage per
695  *       Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during
696  *       power up, this function only works, when the stack is in working state for these ports.
697  */
698 int gap_link_key_iterator_init(btstack_link_key_iterator_t * it);
699 
700 /**
701  * @brief Get next Link Key
702  * @param it
703  * @brief addr
704  * @brief link_key
705  * @brief type of link key
706  * @returns 1, if valid link key found
707  * @see note on gap_link_key_iterator_init
708  */
709 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);
710 
711 /**
712  * @brief Frees resources allocated by iterator_init
713  * @note Must be called after iteration to free resources
714  * @param it
715  * @see note on gap_link_key_iterator_init
716  */
717 void gap_link_key_iterator_done(btstack_link_key_iterator_t * it);
718 
719 /**
720  * @brief Start GAP Classic Inquiry
721  * @param duration in 1.28s units
722  * @return 0 if ok
723  * @events: GAP_EVENT_INQUIRY_RESULT, GAP_EVENT_INQUIRY_COMPLETE
724  */
725 int gap_inquiry_start(uint8_t duration_in_1280ms_units);
726 
727 /**
728  * @brief Stop GAP Classic Inquiry
729  * @brief Stop GAP Classic Inquiry
730  * @returns 0 if ok
731  * @events: GAP_EVENT_INQUIRY_COMPLETE
732  */
733 int gap_inquiry_stop(void);
734 
735 /**
736  * @brief Set LAP for GAP Classic Inquiry
737  * @param lap GAP_IAC_GENERAL_INQUIRY (default), GAP_IAC_LIMITED_INQUIRY
738  */
739 void gap_inquiry_set_lap(uint32_t lap);
740 
741 /**
742  * @brief Remote Name Request
743  * @param addr
744  * @param page_scan_repetition_mode
745  * @param clock_offset only used when bit 15 is set - pass 0 if not known
746  * @events: HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
747  */
748 int gap_remote_name_request(const bd_addr_t addr, uint8_t page_scan_repetition_mode, uint16_t clock_offset);
749 
750 /**
751  * @brief Legacy Pairing Pin Code Response
752  * @note data is not copied, pointer has to stay valid
753  * @param addr
754  * @param pin
755  * @return 0 if ok
756  */
757 int gap_pin_code_response(const bd_addr_t addr, const char * pin);
758 
759 /**
760  * @brief Legacy Pairing Pin Code Response for binary data / non-strings
761  * @note data is not copied, pointer has to stay valid
762  * @param addr
763  * @param pin_data
764  * @param pin_len
765  * @return 0 if ok
766  */
767 int gap_pin_code_response_binary(const bd_addr_t addr, const uint8_t * pin_data, uint8_t pin_len);
768 
769 /**
770  * @brief Abort Legacy Pairing
771  * @param addr
772  * @param pin
773  * @return 0 if ok
774  */
775 int gap_pin_code_negative(bd_addr_t addr);
776 
777 /**
778  * @brief SSP Passkey Response
779  * @param addr
780  * @param passkey [0..999999]
781  * @return 0 if ok
782  */
783 int gap_ssp_passkey_response(const bd_addr_t addr, uint32_t passkey);
784 
785 /**
786  * @brief Abort SSP Passkey Entry/Pairing
787  * @param addr
788  * @param pin
789  * @return 0 if ok
790  */
791 int gap_ssp_passkey_negative(const bd_addr_t addr);
792 
793 /**
794  * @brief Accept SSP Numeric Comparison
795  * @param addr
796  * @param passkey
797  * @return 0 if ok
798  */
799 int gap_ssp_confirmation_response(const bd_addr_t addr);
800 
801 /**
802  * @brief Abort SSP Numeric Comparison/Pairing
803  * @param addr
804  * @param pin
805  * @return 0 if ok
806  */
807 int gap_ssp_confirmation_negative(const bd_addr_t addr);
808 
809 /**
810  * @brief Generate new OOB data
811  * @note OOB data will be provided in GAP_EVENT_LOCAL_OOB_DATA and be used in future pairing procedures
812  */
813 void gap_ssp_generate_oob_data(void);
814 
815 /**
816  * @brief Report Remote OOB Data
817  * @param bd_addr
818  * @param c_192 Simple Pairing Hash C derived from P-192 public key
819  * @param r_192 Simple Pairing Randomizer derived from P-192 public key
820  * @param c_256 Simple Pairing Hash C derived from P-256 public key
821  * @param r_256 Simple Pairing Randomizer derived from P-256 public key
822  */
823 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);
824 
825 /**
826  * Send SSP IO Capabilities Reply
827  * @note IO Capabilities (Negative) Reply is sent automatically unless ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY
828  * @param addr
829  * @return 0 if ok
830  */
831 uint8_t gap_ssp_io_capabilities_response(const bd_addr_t addr);
832 
833 /**
834  * Send SSP IO Capabilities Negative Reply
835  * @note IO Capabilities (Negative) Reply is sent automatically unless ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY
836  * @param addr
837  * @return 0 if ok
838  */
839 uint8_t gap_ssp_io_capabilities_negative(const bd_addr_t addr);
840 
841 /**
842  * @brief Enter Sniff mode
843  * @param con_handle
844  * @param sniff_min_interval range: 0x0002 to 0xFFFE; only even values are valid, Time = N * 0.625 ms
845  * @param sniff_max_interval range: 0x0002 to 0xFFFE; only even values are valid, Time = N * 0.625 ms
846  * @param sniff_attempt Number of Baseband receive slots for sniff attempt.
847  * @param sniff_timeout Number of Baseband receive slots for sniff timeout.
848  @ @return 0 if ok
849  */
850 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);
851 
852 /**
853  * @brief Exit Sniff mode
854  * @param con_handle
855  @ @return 0 if ok
856  */
857 uint8_t gap_sniff_mode_exit(hci_con_handle_t con_handle);
858 
859 /**
860  * @brief Configure Sniff Subrating
861  * @param con_handle
862  * @param max_latency range: 0x0002 to 0xFFFE; Time = N * 0.625 ms
863  * @param min_remote_timeout range:  0x0002 to 0xFFFE; Time = N * 0.625 ms
864  * @param min_local_timeout range:  0x0002 to 0xFFFE; Time = N * 0.625 ms
865  @ @return 0 if ok
866  */
867 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);
868 
869 /**
870  * @Brief Set QoS
871  * @param con_handle
872  * @param service_type
873  * @param token_rate
874  * @param peak_bandwidth
875  * @param latency
876  * @param delay_variation
877  @ @return 0 if ok
878  */
879 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);
880 
881 #endif
882 
883 // LE
884 
885 /**
886  * @brief Get own addr type and address used for LE
887  */
888 void gap_le_get_own_address(uint8_t * addr_type, bd_addr_t addr);
889 
890 
891 /**
892  * @brief Get state of connection re-encryption for bonded devices when in central role
893  * @note used by gatt_client and att_server to wait for re-encryption
894  * @param con_handle
895  * @return 1 if security setup is active
896  */
897 int gap_reconnect_security_setup_active(hci_con_handle_t con_handle);
898 
899 /**
900  * @brief Delete bonding information for remote device
901  * @note On most desktop ports, the LE Device DB uses a TLV and there is one TLV storage per
902  *       Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during
903  *       power up, this function only works, when the stack is in working state for these ports.
904  * @param address_type
905  * @param address
906  */
907 void gap_delete_bonding(bd_addr_type_t address_type, bd_addr_t address);
908 
909 /**
910  * LE Privacy 1.2 - requires support by Controller and ENABLE_LE_RESOLVING_LIST to be defined
911  */
912 
913 /**
914  * @brief Load LE Device DB entries into Controller Resolving List to allow filtering on
915  *        bonded devies with resolvable private addresses
916  * @return EROOR_CODE_SUCCESS if supported by Controller
917  */
918 uint8_t gap_load_resolving_list_from_le_device_db(void);
919 
920 /**
921  * @brief Get local persistent IRK
922  */
923 const uint8_t * gap_get_persistent_irk(void);
924 
925 /* API_END*/
926 
927 #if defined __cplusplus
928 }
929 #endif
930 
931 #endif // GAP_H
932