xref: /btstack/src/gap.h (revision 9afaa4d426555cd46725e0caea74f010a121ef1c)
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 link watchdog. If no ACL packet is sent within timeout_ms, the link will get disconnected
275  * note: current implementation uses the automatic flush timeout controller feature with a max timeout of 1.28s
276  * @param timeout_ms
277  */
278 void gap_enable_link_watchdog(uint16_t timeout_ms);
279 
280 /**
281  * @brief Enable/disable bonding. Default is enabled.
282  * @param enabled
283  */
284 void gap_set_bondable_mode(int enabled);
285 
286 /**
287  * @brief Get bondable mode.
288  * @return 1 if bondable
289  */
290 int gap_get_bondable_mode(void);
291 
292 /**
293  * @brief Set security mode for all outgoing and incoming connections. Default: GAP_SECURITY_MODE_4
294  * @param security_mode is GAP_SECURITY_MODE_2 or GAP_SECURITY_MODE_4
295  * @return status ERROR_CODE_SUCCESS or ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE
296  */
297 uint8_t gap_set_security_mode(gap_security_mode_t security_mode);
298 
299 /**
300  * @brief Get security mode
301  * @return security_mode
302  */
303 gap_security_mode_t gap_get_security_mode(void);
304 
305 /**
306  * @brief Set security level for all outgoing and incoming connections. Default: LEVEL_2
307  * @param security_level
308  * @note has to be called before services or profiles are initialized
309  */
310 void gap_set_security_level(gap_security_level_t security_level);
311 
312 /**
313  * @brief Get security level
314  * @return security_level
315  */
316 gap_security_level_t gap_get_security_level(void);
317 
318 /**
319  * @brief Set Secure Connections Only Mode for BR/EDR (Classic) Default: false
320  * @param enable
321  */
322 void gap_set_secure_connections_only_mode(bool enable);
323 
324 /**
325  * @breif Get Secure Connections Only Mode
326  * @param enabled
327  */
328 bool gap_get_secure_connections_only_mode(void);
329 
330 /**
331  * @brief Set minimal security level for registered services
332  * @param security_level
333  * @note Called by L2CAP based on registered services
334  */
335 void gap_set_minimal_service_security_level(gap_security_level_t security_level);
336 
337 /**
338  * @brief Register filter for rejecting classic connections. Callback will return 1 accept connection, 0 on reject.
339  */
340 void gap_register_classic_connection_filter(int (*accept_callback)(bd_addr_t addr, hci_link_type_t link_type));
341 
342 /* Configure Secure Simple Pairing */
343 
344 /**
345  * @brief Enable will enable SSP during init. Default: true
346  */
347 void gap_ssp_set_enable(int enable);
348 
349 /**
350  * @brief Set IO Capability. BTstack will return capability to SSP requests
351  */
352 void gap_ssp_set_io_capability(int ssp_io_capability);
353 
354 /**
355  * @brief Set Authentication Requirements using during SSP
356  */
357 void gap_ssp_set_authentication_requirement(int authentication_requirement);
358 
359 /**
360  * @brief Enable/disable Secure Connections. Default: true if supported by Controller
361  */
362 void gap_secure_connections_enable(bool enable);
363 
364 /**
365  * @brief If set, BTstack will confirm a numeric comparison and enter '000000' if requested.
366  */
367 void gap_ssp_set_auto_accept(int auto_accept);
368 
369 /**
370  * @brief Set required encryption key size for GAP Levels 1-3 on ccassic connections. Default: 16 bytes
371  * @param encryption_key_size in bytes. Valid 7..16
372  */
373 void gap_set_required_encryption_key_size(uint8_t encryption_key_size);
374 
375 /**
376  * @brief Start dedicated bonding with device. Disconnect after bonding.
377  * @param device
378  * @param request MITM protection
379  * @return error, if max num acl connections active
380  * @result GAP_DEDICATED_BONDING_COMPLETE
381  */
382 int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required);
383 
384 gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type);
385 
386 /**
387  * @brief map link keys to secure connection yes/no
388  */
389 bool gap_secure_connection_for_link_key_type(link_key_type_t link_key_type);
390 
391 /**
392  * @brief map link keys to authenticated
393  */
394 bool gap_authenticated_for_link_key_type(link_key_type_t link_key_type);
395 
396 gap_security_level_t gap_security_level(hci_con_handle_t con_handle);
397 
398 void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t level);
399 
400 bool gap_mitm_protection_required_for_security_level(gap_security_level_t level);
401 
402 /**
403  * @brief Set Page Scan Type
404  * @param page_scan_interval * 0.625 ms, range: 0x0012..0x1000, default: 0x0800
405  * @param page_scan_windows  * 0.625 ms, range: 0x0011..page_scan_interval, default: 0x0012
406  */
407 void gap_set_page_scan_activity(uint16_t page_scan_interval, uint16_t page_scan_window);
408 
409 /**
410  * @brief Set Page Scan Type
411  * @param page_scan_mode
412  */
413 void gap_set_page_scan_type(page_scan_type_t page_scan_type);
414 
415 /**
416  * @brief Set Page Timeout
417  * @param page_timeout * 0.625 ms, range: 0x0001..0xffff, default: 0x6000 (ca 15 seconds)
418  */
419 void gap_set_page_timeout(uint16_t page_timeout);
420 
421 // LE
422 
423 /**
424  * @brief Set parameters for LE Scan
425  * @param scan_type 0 = passive, 1 = active
426  * @param scan_interval range 0x0004..0x4000, unit 0.625 ms
427  * @param scan_window range 0x0004..0x4000, unit 0.625 ms
428  * @param scanning_filter_policy 0 = all devices, 1 = all from whitelist
429  */
430 void gap_set_scan_params(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window, uint8_t scanning_filter_policy);
431 
432 /**
433  * @brief Set parameters for LE Scan
434  * @deprecated Use gap_set_scan_params instead
435  */
436 void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window);
437 
438 /**
439  * @brief Start LE Scan
440  */
441 void gap_start_scan(void);
442 
443 /**
444  * @brief Stop LE Scan
445  */
446 void gap_stop_scan(void);
447 
448 /**
449  * @brief Enable privacy by using random addresses
450  * @param random_address_type to use (incl. OFF)
451  */
452 void gap_random_address_set_mode(gap_random_address_type_t random_address_type);
453 
454 /**
455  * @brief Get privacy mode
456  */
457 gap_random_address_type_t gap_random_address_get_mode(void);
458 
459 /**
460  * @brief Sets update period for random address
461  * @param period_ms in ms
462  */
463  void gap_random_address_set_update_period(int period_ms);
464 
465 /**
466  * @brief Sets a fixed random address for advertising
467  * @param addr
468  * @note Sets random address mode to type off
469  */
470 void gap_random_address_set(const bd_addr_t addr);
471 
472 /**
473  * @brief Set Advertisement Data
474  * @param advertising_data_length
475  * @param advertising_data (max 31 octets)
476  * @note data is not copied, pointer has to stay valid
477  * @note '00:00:00:00:00:00' in advertising_data will be replaced with actual bd addr
478  */
479 void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data);
480 
481 /**
482  * @brief Set Advertisement Parameters
483  * @param adv_int_min
484  * @param adv_int_max
485  * @param adv_type
486  * @param direct_address_type
487  * @param direct_address
488  * @param channel_map
489  * @param filter_policy
490  * @note own_address_type is used from gap_random_address_set_mode
491  */
492 void gap_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type,
493 	uint8_t direct_address_typ, bd_addr_t direct_address, uint8_t channel_map, uint8_t filter_policy);
494 
495 /**
496  * @brief Enable/Disable Advertisements. OFF by default.
497  * @param enabled
498  */
499 void gap_advertisements_enable(int enabled);
500 
501 /**
502  * @brief Set Scan Response Data
503  *
504  * @note For scan response data, scannable undirected advertising (ADV_SCAN_IND) need to be used
505  *
506  * @param advertising_data_length
507  * @param advertising_data (max 31 octets)
508  * @note data is not copied, pointer has to stay valid
509  * @note '00:00:00:00:00:00' in scan_response_data will be replaced with actual bd addr
510  */
511 void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data);
512 
513 /**
514  * @brief Provide storage for new advertising set and setup on Controller
515  * @param storage to use by stack, needs to stay valid until adv set is removed with gap_extended_advertising_remove
516  * @param advertising_parameters
517  * @param out_advertising_handle to use with other adv config commands
518  * @return status
519  * @events: GAP_SUBEVENT_ADVERTISING_SET_INSTALLED
520  */
521 uint8_t gap_extended_advertising_setup(le_advertising_set_t * storage, const le_extended_advertising_parameters_t * advertising_parameters, uint8_t * out_advertising_handle);
522 
523 /**
524  * @param Set advertising params for advertising set
525  * @param advertising_handle
526  * @param advertising_parameters
527  * @return status
528  */
529 uint8_t gap_extended_advertising_set_params(uint8_t advertising_handle, const le_extended_advertising_parameters_t * advertising_parameters);
530 
531 /**
532  * @param Get advertising params for advertising set, e.g. to update params
533  * @param advertising_handle
534  * @param advertising_parameters
535  * @return status
536  */
537 uint8_t gap_extended_advertising_get_params(uint8_t advertising_handle, le_extended_advertising_parameters_t * advertising_parameters);
538 
539 /**
540  * @param Set periodic advertising params for advertising set
541  * @param advertising_handle
542  * @param advertising_parameters
543  * @return status
544  */
545 uint8_t gap_periodic_advertising_set_params(uint8_t advertising_handle, const le_periodic_advertising_parameters_t * advertising_parameters);
546 
547 /**
548  * @param Get params for periodic advertising set, e.g. to update params
549  * @param advertising_handle
550  * @param advertising_parameters
551  * @return status
552  */
553 uint8_t gap_periodic_advertising_get_params(uint8_t advertising_handle, le_periodic_advertising_parameters_t * advertising_parameters);
554 
555 /**
556  * @param Set random addrress for advertising set
557  * @param advertising_handle
558  * @param random_address
559  * @return status
560  */
561 uint8_t gap_extended_advertising_set_random_address(uint8_t advertising_handle, bd_addr_t random_address);
562 
563 /**
564  * @brief Set Advertising Data for a advertisement set
565  * @param advertising_handle
566  * @param advertising_data_length
567  * @param advertising_data
568  * @return status
569  */
570 uint8_t gap_extended_advertising_set_adv_data(uint8_t advertising_handle, uint16_t advertising_data_length, const uint8_t * advertising_data);
571 
572 /**
573  * @brief Set Scan Response Data for a advertisement set
574  * @param advertising_handle
575  * @param scan_response_data_length
576  * @param scan_response_data
577  * @return status
578  */
579 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);
580 
581 /**
582  * @brief Set data for periodic advertisement set
583  * @param advertising_handle
584  * @param periodic_data_length
585  * @param periodic_data
586  * @return status
587  */
588 uint8_t gap_periodic_advertising_set_data(uint8_t advertising_handle, uint16_t periodic_data_length, const uint8_t * periodic_data);
589 
590 /**
591  * @brief Start advertising advertising set
592  * @param advertising_handle
593  * @param timeout in 10ms, or 0 == no timeout
594  * @param num_extended_advertising_events Controller shall send, or 0 == no max number
595  * @return status
596  */
597 uint8_t gap_extended_advertising_start(uint8_t advertising_handle, uint16_t timeout, uint8_t num_extended_advertising_events);
598 
599 /**
600  * @brief Stop advertising
601  * @param advertising_handle
602  * @return status
603  */
604 uint8_t gap_extended_advertising_stop(uint8_t advertising_handle);
605 
606 /**
607  * @brief Start periodic advertising for given advertising set
608  * @param advertising_handle
609  * @param include_adi
610  * @return status
611  */
612 uint8_t gap_periodic_advertising_start(uint8_t advertising_handle, bool include_adi);
613 
614 /**
615  * @brief Stop periodic advertising for given advertising set
616  * @param advertising_handle
617  * @return status
618  */
619 uint8_t gap_periodic_advertising_stop(uint8_t advertising_handle);
620 
621 /**
622  * @brief Remove advertising set from Controller
623  * @param advertising_handle
624  * @return status
625  * @events: GAP_SUBEVENT_ADVERTISING_SET_REMOVED
626  */
627 uint8_t gap_extended_advertising_remove(uint8_t advertising_handle);
628 
629 /**
630  * @brief Set connection parameters for outgoing connections
631  * @param conn_scan_interval (unit: 0.625 msec), default: 60 ms
632  * @param conn_scan_window (unit: 0.625 msec), default: 30 ms
633  * @param conn_interval_min (unit: 1.25ms), default: 10 ms
634  * @param conn_interval_max (unit: 1.25ms), default: 30 ms
635  * @param conn_latency, default: 4
636  * @param supervision_timeout (unit: 10ms), default: 720 ms
637  * @param min_ce_length (unit: 0.625ms), default: 10 ms
638  * @param max_ce_length (unit: 0.625ms), default: 30 ms
639  */
640 void gap_set_connection_parameters(uint16_t conn_scan_interval, uint16_t conn_scan_window,
641     uint16_t conn_interval_min, uint16_t conn_interval_max, uint16_t conn_latency,
642     uint16_t supervision_timeout, uint16_t min_ce_length, uint16_t max_ce_length);
643 
644 /**
645  * @brief Request an update of the connection parameter for a given LE connection
646  * @param handle
647  * @param conn_interval_min (unit: 1.25ms)
648  * @param conn_interval_max (unit: 1.25ms)
649  * @param conn_latency
650  * @param supervision_timeout (unit: 10ms)
651  * @return 0 if ok
652  */
653 int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min,
654 	uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout);
655 
656 /**
657  * @brief Updates the connection parameters for a given LE connection
658  * @param handle
659  * @param conn_interval_min (unit: 1.25ms)
660  * @param conn_interval_max (unit: 1.25ms)
661  * @param conn_latency
662  * @param supervision_timeout (unit: 10ms)
663  * @return 0 if ok
664  */
665 int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min,
666 	uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout);
667 
668 /**
669  * @brief Set accepted connection parameter range
670  * @param range
671  */
672 void gap_get_connection_parameter_range(le_connection_parameter_range_t * range);
673 
674 /**
675  * @brief Get accepted connection parameter range
676  * @param range
677  */
678 void gap_set_connection_parameter_range(le_connection_parameter_range_t * range);
679 
680 /**
681  * @brief Test if connection parameters are inside in existing rage
682  * @param conn_interval_min (unit: 1.25ms)
683  * @param conn_interval_max (unit: 1.25ms)
684  * @param conn_latency
685  * @param supervision_timeout (unit: 10ms)
686  * @return 1 if included
687  */
688 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);
689 
690 /**
691  * @brief Set max number of connections in LE Peripheral role (if Bluetooth Controller supports it)
692  * @note: default: 1
693  * @param max_peripheral_connections
694  */
695 void gap_set_max_number_peripheral_connections(int max_peripheral_connections);
696 
697 /**
698  * @brief Add Device to Whitelist
699  * @param address_typ
700  * @param address
701  * @return 0 if ok
702  */
703 uint8_t gap_whitelist_add(bd_addr_type_t address_type, const bd_addr_t address);
704 
705 /**
706  * @brief Remove Device from Whitelist
707  * @param address_typ
708  * @param address
709  * @return 0 if ok
710  */
711 uint8_t gap_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address);
712 
713 /**
714  * @brief Clear Whitelist
715  * @return 0 if ok
716  */
717 uint8_t gap_whitelist_clear(void);
718 
719 /**
720  * @brief Connect to remote LE device
721  */
722 uint8_t gap_connect(const bd_addr_t addr, bd_addr_type_t addr_type);
723 
724 /**
725  *  @brief Connect with Whitelist
726  *  @note Explicit whitelist management and this connect with whitelist replace deprecated gap_auto_connection_* functions
727  *  @return - if ok
728  */
729 uint8_t gap_connect_with_whitelist(void);
730 
731 /**
732  * @brief Cancel connection process initiated by gap_connect
733  */
734 uint8_t gap_connect_cancel(void);
735 
736 /**
737  * @brief Auto Connection Establishment - Start Connecting to device
738  * @deprecated Please setup Whitelist with gap_whitelist_* and start connecting with gap_connect_with_whitelist
739  * @param address_type
740  * @param address
741  * @return 0 if ok
742  */
743 uint8_t gap_auto_connection_start(bd_addr_type_t address_type, const bd_addr_t address);
744 
745 /**
746  * @brief Auto Connection Establishment - Stop Connecting to device
747  * @deprecated Please setup Whitelist with gap_whitelist_* and start connecting with gap_connect_with_whitelist
748  * @param address_type
749  * @param address
750  * @return 0 if ok
751  */
752 uint8_t gap_auto_connection_stop(bd_addr_type_t address_type, const bd_addr_t address);
753 
754 /**
755  * @brief Auto Connection Establishment - Stop everything
756  * @deprecated Please setup Whitelist with gap_whitelist_* and start connecting with gap_connect_with_whitelist
757  * @note  Convenience function to stop all active auto connection attempts
758  */
759 uint8_t gap_auto_connection_stop_all(void);
760 
761 /**
762  * @brief Set LE PHY
763  * @param con_handle
764  * @param all_phys 0 = set rx/tx, 1 = set only rx, 2 = set only tx
765  * @param tx_phys 1 = 1M, 2 = 2M, 4 = Coded
766  * @param rx_phys 1 = 1M, 2 = 2M, 4 = Coded
767  * @param phy_options 0 = no preferred coding for Coded, 1 = S=2 coding (500 kbit), 2 = S=8 coding (125 kbit)
768  * @return 0 if ok
769  */
770 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);
771 
772 /**
773  * @brief Get connection interval
774  * @param con_handle
775  * @return connection interval, otherwise 0 if error
776  */
777 uint16_t gap_le_connection_interval(hci_con_handle_t con_handle);
778 
779 /**
780  *
781  * @brief Get encryption key size.
782  * @param con_handle
783  * @return 0 if not encrypted, 7-16 otherwise
784  */
785 uint8_t gap_encryption_key_size(hci_con_handle_t con_handle);
786 
787 /**
788  * @brief Get authentication property.
789  * @param con_handle
790  * @return 1 if bonded with OOB/Passkey (AND MITM protection)
791  */
792 bool gap_authenticated(hci_con_handle_t con_handle);
793 
794 /**
795  * @brief Get secure connection property
796  * @param con_handle
797  * @return 1 if bonded usiung LE Secure Connections
798  */
799 bool gap_secure_connection(hci_con_handle_t con_handle);
800 
801 /**
802  * @brief Queries authorization state.
803  * @param con_handle
804  * @return authorization_state for the current session
805  */
806 authorization_state_t gap_authorization_state(hci_con_handle_t con_handle);
807 
808 /**
809  * @brief Get bonded property (BR/EDR/LE)
810  * @note LE: has to be called after identity resolving is complete
811  * @param con_handle
812  * @return true if bonded
813  */
814 bool gap_bonded(hci_con_handle_t con_handle);
815 
816 // Classic
817 #ifdef ENABLE_CLASSIC
818 
819 /**
820  * @brief Override page scan mode. Page scan mode enabled by l2cap when services are registered
821  * @note Might be used to reduce power consumption while Bluetooth module stays powered but no (new)
822  *       connections are expected
823  */
824 void gap_connectable_control(uint8_t enable);
825 
826 /**
827  * @brief Allows to control if device is discoverable. OFF by default.
828  */
829 void gap_discoverable_control(uint8_t enable);
830 
831 /**
832  * @brief Deletes link key for remote device with baseband address.
833  * @param addr
834  * @note On most desktop ports, the Link Key DB uses a TLV and there is one TLV storage per
835  *       Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during
836  *       power up, this function only works, when the stack is in working state for these ports.
837  */
838 void gap_drop_link_key_for_bd_addr(bd_addr_t addr);
839 
840 /**
841  * @brief Delete all stored link keys
842  * @note On most desktop ports, the Link Key DB uses a TLV and there is one TLV storage per
843  *       Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during
844  *       power up, this function only works, when the stack is in working state for these ports.
845  */
846 void gap_delete_all_link_keys(void);
847 
848 /**
849  * @brief Store link key for remote device with baseband address
850  * @param addr
851  * @param link_key
852  * @param link_key_type
853  * @note On most desktop ports, the Link Key DB uses a TLV and there is one TLV storage per
854  *       Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during
855  *       power up, this function only works, when the stack is in working state for these ports.
856  */
857 void gap_store_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t type);
858 
859 /**
860  * @brief Get link for remote device with basband address
861  * @param addr
862  * @param link_key (out) is stored here
863  * @param link_key_type (out) is stored here
864  * @note On most desktop ports, the Link Key DB uses a TLV and there is one TLV storage per
865  *       Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during
866  *       power up, this function only works, when the stack is in working state for these ports.
867  */
868 bool gap_get_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t * type);
869 
870 /**
871  * @brief Setup Link Key iterator
872  * @param it
873  * @return 1 on success
874  * @note On most desktop ports, the Link Key DB uses a TLV and there is one TLV storage per
875  *       Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during
876  *       power up, this function only works, when the stack is in working state for these ports.
877  */
878 int gap_link_key_iterator_init(btstack_link_key_iterator_t * it);
879 
880 /**
881  * @brief Get next Link Key
882  * @param it
883  * @brief addr
884  * @brief link_key
885  * @brief type of link key
886  * @return 1, if valid link key found
887  * @see note on gap_link_key_iterator_init
888  */
889 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);
890 
891 /**
892  * @brief Frees resources allocated by iterator_init
893  * @note Must be called after iteration to free resources
894  * @param it
895  * @see note on gap_link_key_iterator_init
896  */
897 void gap_link_key_iterator_done(btstack_link_key_iterator_t * it);
898 
899 /**
900  * @brief Start GAP Classic Inquiry
901  * @param duration in 1.28s units
902  * @return 0 if ok
903  * @events: GAP_EVENT_INQUIRY_RESULT, GAP_EVENT_INQUIRY_COMPLETE
904  */
905 int gap_inquiry_start(uint8_t duration_in_1280ms_units);
906 
907 /**
908  * @brief Stop GAP Classic Inquiry
909  * @brief Stop GAP Classic Inquiry
910  * @return 0 if ok
911  * @events: GAP_EVENT_INQUIRY_COMPLETE
912  */
913 int gap_inquiry_stop(void);
914 
915 /**
916  * @brief Set LAP for GAP Classic Inquiry
917  * @param lap GAP_IAC_GENERAL_INQUIRY (default), GAP_IAC_LIMITED_INQUIRY
918  */
919 void gap_inquiry_set_lap(uint32_t lap);
920 
921 /**
922  * @brief Set Inquiry Scan Activity
923  * @param inquiry_scan_interval range: 0x0012 to 0x1000; only even values are valid, Time = N * 0.625 ms
924  * @param inquiry_scan_window range: 0x0011 to 0x1000; Time = N * 0.625 ms
925  */
926 void gap_inquiry_set_scan_activity(uint16_t inquiry_scan_interval, uint16_t inquiry_scan_window);
927 
928 /**
929  * @brief Remote Name Request
930  * @param addr
931  * @param page_scan_repetition_mode
932  * @param clock_offset only used when bit 15 is set - pass 0 if not known
933  * @events: HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
934  */
935 int gap_remote_name_request(const bd_addr_t addr, uint8_t page_scan_repetition_mode, uint16_t clock_offset);
936 
937 /**
938  * @brief Legacy Pairing Pin Code Response
939  * @note data is not copied, pointer has to stay valid
940  * @param addr
941  * @param pin
942  * @return 0 if ok
943  */
944 int gap_pin_code_response(const bd_addr_t addr, const char * pin);
945 
946 /**
947  * @brief Legacy Pairing Pin Code Response for binary data / non-strings
948  * @note data is not copied, pointer has to stay valid
949  * @param addr
950  * @param pin_data
951  * @param pin_len
952  * @return 0 if ok
953  */
954 int gap_pin_code_response_binary(const bd_addr_t addr, const uint8_t * pin_data, uint8_t pin_len);
955 
956 /**
957  * @brief Abort Legacy Pairing
958  * @param addr
959  * @param pin
960  * @return 0 if ok
961  */
962 int gap_pin_code_negative(bd_addr_t addr);
963 
964 /**
965  * @brief SSP Passkey Response
966  * @param addr
967  * @param passkey [0..999999]
968  * @return 0 if ok
969  */
970 int gap_ssp_passkey_response(const bd_addr_t addr, uint32_t passkey);
971 
972 /**
973  * @brief Abort SSP Passkey Entry/Pairing
974  * @param addr
975  * @param pin
976  * @return 0 if ok
977  */
978 int gap_ssp_passkey_negative(const bd_addr_t addr);
979 
980 /**
981  * @brief Accept SSP Numeric Comparison
982  * @param addr
983  * @param passkey
984  * @return 0 if ok
985  */
986 int gap_ssp_confirmation_response(const bd_addr_t addr);
987 
988 /**
989  * @brief Abort SSP Numeric Comparison/Pairing
990  * @param addr
991  * @param pin
992  * @return 0 if ok
993  */
994 int gap_ssp_confirmation_negative(const bd_addr_t addr);
995 
996 /**
997  * @brief Generate new OOB data
998  * @note OOB data will be provided in GAP_EVENT_LOCAL_OOB_DATA and be used in future pairing procedures
999  */
1000 void gap_ssp_generate_oob_data(void);
1001 
1002 /**
1003  * @brief Report Remote OOB Data
1004  * @note Pairing Hash and Randomizer are expected in big-endian byte format
1005  * @param bd_addr
1006  * @param c_192 Simple Pairing Hash C derived from P-192 public key
1007  * @param r_192 Simple Pairing Randomizer derived from P-192 public key
1008  * @param c_256 Simple Pairing Hash C derived from P-256 public key
1009  * @param r_256 Simple Pairing Randomizer derived from P-256 public key
1010  */
1011 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);
1012 
1013 /**
1014  * Send SSP IO Capabilities Reply
1015  * @note IO Capabilities (Negative) Reply is sent automatically unless ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY
1016  * @param addr
1017  * @return 0 if ok
1018  */
1019 uint8_t gap_ssp_io_capabilities_response(const bd_addr_t addr);
1020 
1021 /**
1022  * Send SSP IO Capabilities Negative Reply
1023  * @note IO Capabilities (Negative) Reply is sent automatically unless ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY
1024  * @param addr
1025  * @return 0 if ok
1026  */
1027 uint8_t gap_ssp_io_capabilities_negative(const bd_addr_t addr);
1028 
1029 /**
1030  * Send Link Key Reponse
1031  * @note Link Key (Negative) Reply is sent automatically unless ENABLE_EXPLICIT_LINK_KEY_RESPONSE
1032  * @param addr
1033  * @param link_key
1034  * @param type or INVALID_LINK_KEY if link key not available
1035  * @return 0 if ok
1036  */
1037  uint8_t gap_send_link_key_response(const bd_addr_t addr, link_key_t link_key, link_key_type_t type);
1038 
1039 /**
1040  * @brief Enter Sniff mode
1041  * @param con_handle
1042  * @param sniff_min_interval range: 0x0002 to 0xFFFE; only even values are valid, Time = N * 0.625 ms
1043  * @param sniff_max_interval range: 0x0002 to 0xFFFE; only even values are valid, Time = N * 0.625 ms
1044  * @param sniff_attempt Number of Baseband receive slots for sniff attempt.
1045  * @param sniff_timeout Number of Baseband receive slots for sniff timeout.
1046  @ @return 0 if ok
1047  */
1048 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);
1049 
1050 /**
1051  * @brief Exit Sniff mode
1052  * @param con_handle
1053  @ @return 0 if ok
1054  */
1055 uint8_t gap_sniff_mode_exit(hci_con_handle_t con_handle);
1056 
1057 /**
1058  * @brief Configure Sniff Subrating
1059  * @param con_handle
1060  * @param max_latency range: 0x0002 to 0xFFFE; Time = N * 0.625 ms
1061  * @param min_remote_timeout range:  0x0002 to 0xFFFE; Time = N * 0.625 ms
1062  * @param min_local_timeout range:  0x0002 to 0xFFFE; Time = N * 0.625 ms
1063  @ @return 0 if ok
1064  */
1065 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);
1066 
1067 /**
1068  * @Brief Set QoS
1069  * @param con_handle
1070  * @param service_type
1071  * @param token_rate
1072  * @param peak_bandwidth
1073  * @param latency
1074  * @param delay_variation
1075  @ @return 0 if ok
1076  */
1077 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);
1078 
1079 #endif
1080 
1081 // LE
1082 
1083 /**
1084  * @brief Get own addr type and address used for LE for next scan/advertisement/connect operation
1085  */
1086 void gap_le_get_own_address(uint8_t * addr_type, bd_addr_t addr);
1087 
1088 /**
1089  * @brief Get own addr type and address used for LE advertisements (Peripheral)
1090  */
1091 void gap_le_get_own_advertisements_address(uint8_t * addr_type, bd_addr_t addr);
1092 
1093 /**
1094  * @brief Get own addr type and address used for LE connections (Central)
1095  */
1096 void gap_le_get_own_connection_address(uint8_t * addr_type, bd_addr_t addr);
1097 
1098 /**
1099  * @brief Get state of connection re-encryption for bonded devices when in central role
1100  * @note used by gatt_client and att_server to wait for re-encryption
1101  * @param con_handle
1102  * @return 1 if security setup is active
1103  */
1104 int gap_reconnect_security_setup_active(hci_con_handle_t con_handle);
1105 
1106 /**
1107  * @brief Delete bonding information for remote device
1108  * @note On most desktop ports, the LE Device DB uses a TLV and there is one TLV storage per
1109  *       Controller resp. its Bluetooth Address. As the Bluetooth Address is retrieved during
1110  *       power up, this function only works, when the stack is in working state for these ports.
1111  * @param address_type
1112  * @param address
1113  */
1114 void gap_delete_bonding(bd_addr_type_t address_type, bd_addr_t address);
1115 
1116 /**
1117  * LE Privacy 1.2 - requires support by Controller and ENABLE_LE_RESOLVING_LIST to be defined
1118  */
1119 
1120 /**
1121  * @brief Load LE Device DB entries into Controller Resolving List to allow filtering on
1122  *        bonded devies with resolvable private addresses
1123  * @return EROOR_CODE_SUCCESS if supported by Controller
1124  */
1125 uint8_t gap_load_resolving_list_from_le_device_db(void);
1126 
1127 /**
1128  * @brief Get local persistent IRK
1129  */
1130 const uint8_t * gap_get_persistent_irk(void);
1131 
1132 /* API_END*/
1133 
1134 #if defined __cplusplus
1135 }
1136 #endif
1137 
1138 #endif // GAP_H
1139