xref: /btstack/src/gap.h (revision b45b7749fd0a3efec18073ae84f893078d0216d0)
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_type
561  * @param address
562  * @returns 0 if ok
563  */
564 uint8_t gap_auto_connection_start(bd_addr_type_t address_type, 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_type
570  * @param address
571  * @returns 0 if ok
572  */
573 uint8_t gap_auto_connection_stop(bd_addr_type_t address_type, 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  * @param con_handle
596  * @return connection interval, otherwise 0 if error
597  */
598 uint16_t gap_le_connection_interval(hci_con_handle_t con_handle);
599 
600 /**
601  *
602  * @brief Get encryption key size.
603  * @param con_handle
604  * @return 0 if not encrypted, 7-16 otherwise
605  */
606 int gap_encryption_key_size(hci_con_handle_t con_handle);
607 
608 /**
609  * @brief Get authentication property.
610  * @param con_handle
611  * @return 1 if bonded with OOB/Passkey (AND MITM protection)
612  */
613 int gap_authenticated(hci_con_handle_t con_handle);
614 
615 /**
616  * @brief Get secure connection property
617  * @param con_handle
618  * @return 1 if bonded usiung LE Secure Connections
619  */
620 int gap_secure_connection(hci_con_handle_t con_handle);
621 
622 /**
623  * @brief Queries authorization state.
624  * @param con_handle
625  * @return authorization_state for the current session
626  */
627 authorization_state_t gap_authorization_state(hci_con_handle_t con_handle);
628 
629 /**
630  * @brief Get bonded property (BR/EDR/LE)
631  * @note LE: has to be called after identity resolving is complete
632  * @param con_handle
633  * @return true if bonded
634  */
635 bool gap_bonded(hci_con_handle_t con_handle);
636 
637 // Classic
638 #ifdef ENABLE_CLASSIC
639 
640 /**
641  * @brief Override page scan mode. Page scan mode enabled by l2cap when services are registered
642  * @note Might be used to reduce power consumption while Bluetooth module stays powered but no (new)
643  *       connections are expected
644  */
645 void gap_connectable_control(uint8_t enable);
646 
647 /**
648  * @brief Allows to control if device is discoverable. OFF by default.
649  */
650 void gap_discoverable_control(uint8_t enable);
651 
652 /**
653  * @brief Deletes link key for remote device with baseband address.
654  * @param addr
655  * @note On most desktop ports, the Link Key DB uses a TLV and there is one TLV storage per
656  *       Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during
657  *       power up, this function only works, when the stack is in working state for these ports.
658  */
659 void gap_drop_link_key_for_bd_addr(bd_addr_t addr);
660 
661 /**
662  * @brief Delete all stored link keys
663  * @note On most desktop ports, the Link Key DB uses a TLV and there is one TLV storage per
664  *       Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during
665  *       power up, this function only works, when the stack is in working state for these ports.
666  */
667 void gap_delete_all_link_keys(void);
668 
669 /**
670  * @brief Store link key for remote device with baseband address
671  * @param addr
672  * @param link_key
673  * @param link_key_type
674  * @note On most desktop ports, the Link Key DB uses a TLV and there is one TLV storage per
675  *       Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during
676  *       power up, this function only works, when the stack is in working state for these ports.
677  */
678 void gap_store_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t type);
679 
680 /**
681  * @brief Get link for remote device with basband address
682  * @param addr
683  * @param link_key (out) is stored here
684  * @param link_key_type (out) is stored here
685  * @note On most desktop ports, the Link Key DB uses a TLV and there is one TLV storage per
686  *       Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during
687  *       power up, this function only works, when the stack is in working state for these ports.
688  */
689 bool gap_get_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t * type);
690 
691 /**
692  * @brief Setup Link Key iterator
693  * @param it
694  * @returns 1 on success
695  * @note On most desktop ports, the Link Key DB uses a TLV and there is one TLV storage per
696  *       Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during
697  *       power up, this function only works, when the stack is in working state for these ports.
698  */
699 int gap_link_key_iterator_init(btstack_link_key_iterator_t * it);
700 
701 /**
702  * @brief Get next Link Key
703  * @param it
704  * @brief addr
705  * @brief link_key
706  * @brief type of link key
707  * @returns 1, if valid link key found
708  * @see note on gap_link_key_iterator_init
709  */
710 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);
711 
712 /**
713  * @brief Frees resources allocated by iterator_init
714  * @note Must be called after iteration to free resources
715  * @param it
716  * @see note on gap_link_key_iterator_init
717  */
718 void gap_link_key_iterator_done(btstack_link_key_iterator_t * it);
719 
720 /**
721  * @brief Start GAP Classic Inquiry
722  * @param duration in 1.28s units
723  * @return 0 if ok
724  * @events: GAP_EVENT_INQUIRY_RESULT, GAP_EVENT_INQUIRY_COMPLETE
725  */
726 int gap_inquiry_start(uint8_t duration_in_1280ms_units);
727 
728 /**
729  * @brief Stop GAP Classic Inquiry
730  * @brief Stop GAP Classic Inquiry
731  * @returns 0 if ok
732  * @events: GAP_EVENT_INQUIRY_COMPLETE
733  */
734 int gap_inquiry_stop(void);
735 
736 /**
737  * @brief Set LAP for GAP Classic Inquiry
738  * @param lap GAP_IAC_GENERAL_INQUIRY (default), GAP_IAC_LIMITED_INQUIRY
739  */
740 void gap_inquiry_set_lap(uint32_t lap);
741 
742 /**
743  * @brief Remote Name Request
744  * @param addr
745  * @param page_scan_repetition_mode
746  * @param clock_offset only used when bit 15 is set - pass 0 if not known
747  * @events: HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
748  */
749 int gap_remote_name_request(const bd_addr_t addr, uint8_t page_scan_repetition_mode, uint16_t clock_offset);
750 
751 /**
752  * @brief Legacy Pairing Pin Code Response
753  * @note data is not copied, pointer has to stay valid
754  * @param addr
755  * @param pin
756  * @return 0 if ok
757  */
758 int gap_pin_code_response(const bd_addr_t addr, const char * pin);
759 
760 /**
761  * @brief Legacy Pairing Pin Code Response for binary data / non-strings
762  * @note data is not copied, pointer has to stay valid
763  * @param addr
764  * @param pin_data
765  * @param pin_len
766  * @return 0 if ok
767  */
768 int gap_pin_code_response_binary(const bd_addr_t addr, const uint8_t * pin_data, uint8_t pin_len);
769 
770 /**
771  * @brief Abort Legacy Pairing
772  * @param addr
773  * @param pin
774  * @return 0 if ok
775  */
776 int gap_pin_code_negative(bd_addr_t addr);
777 
778 /**
779  * @brief SSP Passkey Response
780  * @param addr
781  * @param passkey [0..999999]
782  * @return 0 if ok
783  */
784 int gap_ssp_passkey_response(const bd_addr_t addr, uint32_t passkey);
785 
786 /**
787  * @brief Abort SSP Passkey Entry/Pairing
788  * @param addr
789  * @param pin
790  * @return 0 if ok
791  */
792 int gap_ssp_passkey_negative(const bd_addr_t addr);
793 
794 /**
795  * @brief Accept SSP Numeric Comparison
796  * @param addr
797  * @param passkey
798  * @return 0 if ok
799  */
800 int gap_ssp_confirmation_response(const bd_addr_t addr);
801 
802 /**
803  * @brief Abort SSP Numeric Comparison/Pairing
804  * @param addr
805  * @param pin
806  * @return 0 if ok
807  */
808 int gap_ssp_confirmation_negative(const bd_addr_t addr);
809 
810 /**
811  * @brief Generate new OOB data
812  * @note OOB data will be provided in GAP_EVENT_LOCAL_OOB_DATA and be used in future pairing procedures
813  */
814 void gap_ssp_generate_oob_data(void);
815 
816 /**
817  * @brief Report Remote OOB Data
818  * @param bd_addr
819  * @param c_192 Simple Pairing Hash C derived from P-192 public key
820  * @param r_192 Simple Pairing Randomizer derived from P-192 public key
821  * @param c_256 Simple Pairing Hash C derived from P-256 public key
822  * @param r_256 Simple Pairing Randomizer derived from P-256 public key
823  */
824 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);
825 
826 /**
827  * Send SSP IO Capabilities Reply
828  * @note IO Capabilities (Negative) Reply is sent automatically unless ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY
829  * @param addr
830  * @return 0 if ok
831  */
832 uint8_t gap_ssp_io_capabilities_response(const bd_addr_t addr);
833 
834 /**
835  * Send SSP IO Capabilities Negative Reply
836  * @note IO Capabilities (Negative) Reply is sent automatically unless ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY
837  * @param addr
838  * @return 0 if ok
839  */
840 uint8_t gap_ssp_io_capabilities_negative(const bd_addr_t addr);
841 
842 /**
843  * @brief Enter Sniff mode
844  * @param con_handle
845  * @param sniff_min_interval range: 0x0002 to 0xFFFE; only even values are valid, Time = N * 0.625 ms
846  * @param sniff_max_interval range: 0x0002 to 0xFFFE; only even values are valid, Time = N * 0.625 ms
847  * @param sniff_attempt Number of Baseband receive slots for sniff attempt.
848  * @param sniff_timeout Number of Baseband receive slots for sniff timeout.
849  @ @return 0 if ok
850  */
851 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);
852 
853 /**
854  * @brief Exit Sniff mode
855  * @param con_handle
856  @ @return 0 if ok
857  */
858 uint8_t gap_sniff_mode_exit(hci_con_handle_t con_handle);
859 
860 /**
861  * @brief Configure Sniff Subrating
862  * @param con_handle
863  * @param max_latency range: 0x0002 to 0xFFFE; Time = N * 0.625 ms
864  * @param min_remote_timeout range:  0x0002 to 0xFFFE; Time = N * 0.625 ms
865  * @param min_local_timeout range:  0x0002 to 0xFFFE; Time = N * 0.625 ms
866  @ @return 0 if ok
867  */
868 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);
869 
870 /**
871  * @Brief Set QoS
872  * @param con_handle
873  * @param service_type
874  * @param token_rate
875  * @param peak_bandwidth
876  * @param latency
877  * @param delay_variation
878  @ @return 0 if ok
879  */
880 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);
881 
882 #endif
883 
884 // LE
885 
886 /**
887  * @brief Get own addr type and address used for LE
888  */
889 void gap_le_get_own_address(uint8_t * addr_type, bd_addr_t addr);
890 
891 
892 /**
893  * @brief Get state of connection re-encryption for bonded devices when in central role
894  * @note used by gatt_client and att_server to wait for re-encryption
895  * @param con_handle
896  * @return 1 if security setup is active
897  */
898 int gap_reconnect_security_setup_active(hci_con_handle_t con_handle);
899 
900 /**
901  * @brief Delete bonding information for remote device
902  * @note On most desktop ports, the LE Device DB uses a TLV and there is one TLV storage per
903  *       Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during
904  *       power up, this function only works, when the stack is in working state for these ports.
905  * @param address_type
906  * @param address
907  */
908 void gap_delete_bonding(bd_addr_type_t address_type, bd_addr_t address);
909 
910 /**
911  * LE Privacy 1.2 - requires support by Controller and ENABLE_LE_RESOLVING_LIST to be defined
912  */
913 
914 /**
915  * @brief Load LE Device DB entries into Controller Resolving List to allow filtering on
916  *        bonded devies with resolvable private addresses
917  * @return EROOR_CODE_SUCCESS if supported by Controller
918  */
919 uint8_t gap_load_resolving_list_from_le_device_db(void);
920 
921 /**
922  * @brief Get local persistent IRK
923  */
924 const uint8_t * gap_get_persistent_irk(void);
925 
926 /* API_END*/
927 
928 #if defined __cplusplus
929 }
930 #endif
931 
932 #endif // GAP_H
933