esp_gap_ble_api.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef __ESP_GAP_BLE_API_H__
  14. #define __ESP_GAP_BLE_API_H__
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include "esp_err.h"
  18. #include "esp_bt_defs.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /**@{
  23. * BLE_ADV_DATA_FLAG data flag bit definition used for advertising data flag
  24. */
  25. #define ESP_BLE_ADV_FLAG_LIMIT_DISC (0x01 << 0)
  26. #define ESP_BLE_ADV_FLAG_GEN_DISC (0x01 << 1)
  27. #define ESP_BLE_ADV_FLAG_BREDR_NOT_SPT (0x01 << 2)
  28. #define ESP_BLE_ADV_FLAG_DMT_CONTROLLER_SPT (0x01 << 3)
  29. #define ESP_BLE_ADV_FLAG_DMT_HOST_SPT (0x01 << 4)
  30. #define ESP_BLE_ADV_FLAG_NON_LIMIT_DISC (0x00 )
  31. /**
  32. * @}
  33. */
  34. #define ESP_LE_KEY_NONE 0
  35. #define ESP_LE_KEY_PENC (1 << 0) /*!< encryption key, encryption information of peer device */
  36. #define ESP_LE_KEY_PID (1 << 1) /*!< identity key of the peer device */
  37. #define ESP_LE_KEY_PCSRK (1 << 2) /*!< peer SRK */
  38. #define ESP_LE_KEY_PLK (1 << 3) /*!< Link key*/
  39. #define ESP_LE_KEY_LLK (ESP_LE_KEY_PLK << 4)
  40. #define ESP_LE_KEY_LENC (ESP_LE_KEY_PENC << 4) /*!< master role security information:div */
  41. #define ESP_LE_KEY_LID (ESP_LE_KEY_PID << 4) /*!< master device ID key */
  42. #define ESP_LE_KEY_LCSRK (ESP_LE_KEY_PCSRK << 4) /*!< local CSRK has been deliver to peer */
  43. typedef uint8_t esp_ble_key_type_t;
  44. #define ESP_LE_AUTH_NO_BOND 0x00 /*!< 0*/
  45. #define ESP_LE_AUTH_BOND 0x01 /*!< 1 << 0 */
  46. #define ESP_LE_AUTH_REQ_MITM (1 << 2) /*!< 1 << 2 */
  47. #define ESP_LE_AUTH_REQ_SC_ONLY (1 << 3) /*!< 1 << 3 */
  48. #define ESP_LE_AUTH_REQ_SC_BOND (ESP_LE_AUTH_BOND | ESP_LE_AUTH_REQ_SC_ONLY) /*!< 1001 */
  49. #define ESP_LE_AUTH_REQ_SC_MITM (ESP_LE_AUTH_REQ_MITM | ESP_LE_AUTH_REQ_SC_ONLY) /*!< 1100 */
  50. #define ESP_LE_AUTH_REQ_SC_MITM_BOND (ESP_LE_AUTH_REQ_MITM | ESP_LE_AUTH_REQ_SC_ONLY | ESP_LE_AUTH_BOND) /*!< 1101 */
  51. typedef uint8_t esp_ble_auth_req_t; /*!< combination of the above bit pattern */
  52. #define ESP_IO_CAP_OUT 0 /*!< DisplayOnly */
  53. #define ESP_IO_CAP_IO 1 /*!< DisplayYesNo */
  54. #define ESP_IO_CAP_IN 2 /*!< KeyboardOnly */
  55. #define ESP_IO_CAP_NONE 3 /*!< NoInputNoOutput */
  56. #define ESP_IO_CAP_KBDISP 4 /*!< Keyboard display */
  57. typedef uint8_t esp_ble_io_cap_t; /*!< combination of the io capability */
  58. /// GAP BLE callback event type
  59. typedef enum {
  60. ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT = 0, /*!< When advertising data set complete, the event comes */
  61. ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT, /*!< When scan response data set complete, the event comes */
  62. ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT, /*!< When scan parameters set complete, the event comes */
  63. ESP_GAP_BLE_SCAN_RESULT_EVT, /*!< When one scan result ready, the event comes each time */
  64. ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT, /*!< When raw advertising data set complete, the event comes */
  65. ESP_GAP_BLE_SCAN_RSP_DATA_RAW_SET_COMPLETE_EVT, /*!< When raw advertising data set complete, the event comes */
  66. ESP_GAP_BLE_ADV_START_COMPLETE_EVT, /*!< When start advertising complete, the event comes */
  67. ESP_GAP_BLE_SCAN_START_COMPLETE_EVT, /*!< When start scan complete, the event comes */
  68. ESP_GAP_BLE_AUTH_CMPL_EVT, /* Authentication complete indication. */
  69. ESP_GAP_BLE_KEY_EVT, /* BLE key event for peer device keys */
  70. ESP_GAP_BLE_SEC_REQ_EVT, /* BLE security request */
  71. ESP_GAP_BLE_PASSKEY_NOTIF_EVT, /* passkey notification event */
  72. ESP_GAP_BLE_PASSKEY_REQ_EVT, /* passkey request event */
  73. ESP_GAP_BLE_OOB_REQ_EVT, /* OOB request event */
  74. ESP_GAP_BLE_LOCAL_IR_EVT, /* BLE local IR event */
  75. ESP_GAP_BLE_LOCAL_ER_EVT, /* BLE local ER event */
  76. ESP_GAP_BLE_NC_REQ_EVT, /* Numeric Comparison request event */
  77. ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT, /*!< When stop adv complete, the event comes */
  78. ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT, /*!< When stop scan complete, the event comes */
  79. } esp_gap_ble_cb_event_t;
  80. /// Advertising data maximum length
  81. #define ESP_BLE_ADV_DATA_LEN_MAX 31
  82. /// Scan response data maximum length
  83. #define ESP_BLE_SCAN_RSP_DATA_LEN_MAX 31
  84. /// The type of advertising data(not adv_type)
  85. typedef enum {
  86. ESP_BLE_AD_TYPE_FLAG = 0x01,
  87. ESP_BLE_AD_TYPE_16SRV_PART = 0x02,
  88. ESP_BLE_AD_TYPE_16SRV_CMPL = 0x03,
  89. ESP_BLE_AD_TYPE_32SRV_PART = 0x04,
  90. ESP_BLE_AD_TYPE_32SRV_CMPL = 0x05,
  91. ESP_BLE_AD_TYPE_128SRV_PART = 0x06,
  92. ESP_BLE_AD_TYPE_128SRV_CMPL = 0x07,
  93. ESP_BLE_AD_TYPE_NAME_SHORT = 0x08,
  94. ESP_BLE_AD_TYPE_NAME_CMPL = 0x09,
  95. ESP_BLE_AD_TYPE_TX_PWR = 0x0A,
  96. ESP_BLE_AD_TYPE_DEV_CLASS = 0x0D,
  97. ESP_BLE_AD_TYPE_SM_TK = 0x10,
  98. ESP_BLE_AD_TYPE_SM_OOB_FLAG = 0x11,
  99. ESP_BLE_AD_TYPE_INT_RANGE = 0x12,
  100. ESP_BLE_AD_TYPE_SOL_SRV_UUID = 0x14,
  101. ESP_BLE_AD_TYPE_128SOL_SRV_UUID = 0x15,
  102. ESP_BLE_AD_TYPE_SERVICE_DATA = 0x16,
  103. ESP_BLE_AD_TYPE_PUBLIC_TARGET = 0x17,
  104. ESP_BLE_AD_TYPE_RANDOM_TARGET = 0x18,
  105. ESP_BLE_AD_TYPE_APPEARANCE = 0x19,
  106. ESP_BLE_AD_TYPE_ADV_INT = 0x1A,
  107. ESP_BLE_AD_TYPE_32SOL_SRV_UUID = 0x1B,
  108. ESP_BLE_AD_TYPE_32SERVICE_DATA = 0x1C,
  109. ESP_BLE_AD_TYPE_128SERVICE_DATA = 0x1D,
  110. ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE = 0xFF,
  111. } esp_ble_adv_data_type;
  112. /// Advertising mode
  113. typedef enum {
  114. ADV_TYPE_IND = 0x00,
  115. ADV_TYPE_DIRECT_IND_HIGH = 0x01,
  116. ADV_TYPE_SCAN_IND = 0x02,
  117. ADV_TYPE_NONCONN_IND = 0x03,
  118. ADV_TYPE_DIRECT_IND_LOW = 0x04,
  119. } esp_ble_adv_type_t;
  120. /// Advertising channel mask
  121. typedef enum {
  122. ADV_CHNL_37 = 0x01,
  123. ADV_CHNL_38 = 0x02,
  124. ADV_CHNL_39 = 0x04,
  125. ADV_CHNL_ALL = 0x07,
  126. } esp_ble_adv_channel_t;
  127. typedef enum {
  128. ///Allow both scan and connection requests from anyone
  129. ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY = 0x00,
  130. ///Allow both scan req from White List devices only and connection req from anyone
  131. ADV_FILTER_ALLOW_SCAN_WLST_CON_ANY,
  132. ///Allow both scan req from anyone and connection req from White List devices only
  133. ADV_FILTER_ALLOW_SCAN_ANY_CON_WLST,
  134. ///Allow scan and connection requests from White List devices only
  135. ADV_FILTER_ALLOW_SCAN_WLST_CON_WLST,
  136. ///Enumeration end value for advertising filter policy value check
  137. } esp_ble_adv_filter_t;
  138. typedef enum {
  139. ESP_BLE_SEC_NONE,
  140. ESP_BLE_SEC_ENCRYPT,
  141. ESP_BLE_SEC_ENCRYPT_NO_MITM,
  142. ESP_BLE_SEC_ENCRYPT_MITM,
  143. }esp_ble_sec_act_t;
  144. typedef enum {
  145. ESP_BLE_SM_PASSKEY,
  146. ESP_BLE_SM_AUTHEN_REQ_MODE,
  147. ESP_BLE_SM_IOCAP_MODE,
  148. ESP_BLE_SM_SET_INIT_KEY,
  149. ESP_BLE_SM_SET_RSP_KEK,
  150. ESP_BLE_SM_MAX_KEY_SIZE,
  151. } esp_ble_sm_param_t;
  152. /// Advertising parameters
  153. typedef struct {
  154. uint16_t adv_int_min; /*!< Minimum advertising interval for
  155. undirected and low duty cycle directed advertising.
  156. Range: 0x0020 to 0x4000 Default: N = 0x0800 (1.28 second)
  157. Time = N * 0.625 msec Time Range: 20 ms to 10.24 sec */
  158. uint16_t adv_int_max; /*!< Maximum advertising interval for
  159. undirected and low duty cycle directed advertising.
  160. Range: 0x0020 to 0x4000 Default: N = 0x0800 (1.28 second)
  161. Time = N * 0.625 msec Time Range: 20 ms to 10.24 sec Advertising max interval */
  162. esp_ble_adv_type_t adv_type; /*!< Advertising type */
  163. esp_ble_addr_type_t own_addr_type; /*!< Owner bluetooth device address type */
  164. esp_bd_addr_t peer_addr; /*!< Peer device bluetooth device address */
  165. esp_ble_addr_type_t peer_addr_type; /*!< Peer device bluetooth device address type */
  166. esp_ble_adv_channel_t channel_map; /*!< Advertising channel map */
  167. esp_ble_adv_filter_t adv_filter_policy; /*!< Advertising filter policy */
  168. } esp_ble_adv_params_t;
  169. /// Advertising data content, according to "Supplement to the Bluetooth Core Specification"
  170. typedef struct {
  171. bool set_scan_rsp; /*!< Set this advertising data as scan response or not*/
  172. bool include_name; /*!< Advertising data include device name or not */
  173. bool include_txpower; /*!< Advertising data include TX power */
  174. int min_interval; /*!< Advertising data show advertising min interval */
  175. int max_interval; /*!< Advertising data show advertising max interval */
  176. int appearance; /*!< External appearance of device */
  177. uint16_t manufacturer_len; /*!< Manufacturer data length */
  178. uint8_t *p_manufacturer_data; /*!< Manufacturer data point */
  179. uint16_t service_data_len; /*!< Service data length */
  180. uint8_t *p_service_data; /*!< Service data point */
  181. uint16_t service_uuid_len; /*!< Service uuid length */
  182. uint8_t *p_service_uuid; /*!< Service uuid array point */
  183. uint8_t flag; /*!< Advertising flag of discovery mode, see BLE_ADV_DATA_FLAG detail */
  184. } esp_ble_adv_data_t;
  185. /// Own BD address source of the device
  186. typedef enum {
  187. /// Public Address
  188. ESP_PUBLIC_ADDR,
  189. /// Provided random address
  190. ESP_PROVIDED_RND_ADDR,
  191. /// Provided static random address
  192. ESP_GEN_STATIC_RND_ADDR,
  193. /// Generated resolvable private random address
  194. ESP_GEN_RSLV_ADDR,
  195. /// Generated non-resolvable private random address
  196. ESP_GEN_NON_RSLV_ADDR,
  197. /// Provided Reconnection address
  198. ESP_PROVIDED_RECON_ADDR,
  199. } esp_ble_own_addr_src_t;
  200. /// Ble scan type
  201. typedef enum {
  202. BLE_SCAN_TYPE_PASSIVE = 0x0, /*!< Passive scan */
  203. BLE_SCAN_TYPE_ACTIVE = 0x1, /*!< Active scan */
  204. } esp_ble_scan_type_t;
  205. /// Ble scan filter type
  206. typedef enum {
  207. BLE_SCAN_FILTER_ALLOW_ALL = 0x0, /*!< Accept all :
  208. 1. advertisement packets except directed advertising packets not addressed to this device (default). */
  209. BLE_SCAN_FILTER_ALLOW_ONLY_WLST = 0x1, /*!< Accept only :
  210. 1. advertisement packets from devices where the advertiser’s address is in the White list.
  211. 2. Directed advertising packets which are not addressed for this device shall be ignored. */
  212. BLE_SCAN_FILTER_ALLOW_UND_RPA_DIR = 0x2, /*!< Accept all :
  213. 1. undirected advertisement packets, and
  214. 2. directed advertising packets where the initiator address is a resolvable private address, and
  215. 3. directed advertising packets addressed to this device. */
  216. BLE_SCAN_FILTER_ALLOW_WLIST_PRA_DIR = 0x3, /*!< Accept all :
  217. 1. advertisement packets from devices where the advertiser’s address is in the White list, and
  218. 2. directed advertising packets where the initiator address is a resolvable private address, and
  219. 3. directed advertising packets addressed to this device.*/
  220. } esp_ble_scan_filter_t;
  221. /// Ble scan parameters
  222. typedef struct {
  223. esp_ble_scan_type_t scan_type; /*!< Scan type */
  224. esp_ble_addr_type_t own_addr_type; /*!< Owner address type */
  225. esp_ble_scan_filter_t scan_filter_policy; /*!< Scan filter policy */
  226. uint16_t scan_interval; /*!< Scan interval. This is defined as the time interval from
  227. when the Controller started its last LE scan until it begins the subsequent LE scan.
  228. Range: 0x0004 to 0x4000 Default: 0x0010 (10 ms)
  229. Time = N * 0.625 msec
  230. Time Range: 2.5 msec to 10.24 seconds*/
  231. uint16_t scan_window; /*!< Scan window. The duration of the LE scan. LE_Scan_Window
  232. shall be less than or equal to LE_Scan_Interval
  233. Range: 0x0004 to 0x4000 Default: 0x0010 (10 ms)
  234. Time = N * 0.625 msec
  235. Time Range: 2.5 msec to 10240 msec */
  236. } esp_ble_scan_params_t;
  237. /// Connection update parameters
  238. typedef struct {
  239. esp_bd_addr_t bda; /*!< Bluetooth device address */
  240. uint16_t min_int; /*!< Min connection interval */
  241. uint16_t max_int; /*!< Max connection interval */
  242. uint16_t latency; /*!< Slave latency for the connection in number of connection events. Range: 0x0000 to 0x01F3 */
  243. uint16_t timeout; /*!< Supervision timeout for the LE Link. Range: 0x000A to 0x0C80.
  244. Mandatory Range: 0x000A to 0x0C80 Time = N * 10 msec
  245. Time Range: 100 msec to 32 seconds */
  246. } esp_ble_conn_update_params_t;
  247. /**
  248. * @brief BLE encryption keys
  249. */
  250. typedef struct
  251. {
  252. esp_bt_octet16_t ltk; /*!< The long term key*/
  253. esp_bt_octet8_t rand; /*!< The random number*/
  254. uint16_t ediv; /*!< The ediv value*/
  255. uint8_t sec_level; /*!< The security level of the security link*/
  256. uint8_t key_size; /*!< The key size(7~16) of the security link*/
  257. }esp_ble_penc_keys_t; /*!< The key type*/
  258. /**
  259. * @brief BLE CSRK keys
  260. */
  261. typedef struct
  262. {
  263. uint32_t counter; /*!< The counter */
  264. esp_bt_octet16_t csrk; /*!< The csrk key */
  265. uint8_t sec_level; /*!< The security level */
  266. }esp_ble_pcsrk_keys_t; /*!< The pcsrk key type */
  267. /**
  268. * @brief BLE pid keys
  269. */
  270. typedef struct
  271. {
  272. esp_bt_octet16_t irk; /*!< The irk value */
  273. esp_bd_addr_type_t addr_type; /*!< The address type */
  274. esp_bd_addr_t static_addr; /*!< The static address */
  275. }esp_ble_pid_keys_t; /*!< The pid key type */
  276. /**
  277. * @brief BLE Encryption reproduction keys
  278. */
  279. typedef struct
  280. {
  281. esp_bt_octet16_t ltk; /*!< The long term key */
  282. uint16_t div; /*!< The div value */
  283. uint8_t key_size; /*!< The key size of the security link */
  284. uint8_t sec_level; /*!< The security level of the security link */
  285. }esp_ble_lenc_keys_t; /*!< The key type */
  286. /**
  287. * @brief BLE SRK keys
  288. */
  289. typedef struct
  290. {
  291. uint32_t counter; /*!< The counter value */
  292. uint16_t div; /*!< The div value */
  293. uint8_t sec_level; /*!< The security level of the security link */
  294. esp_bt_octet16_t csrk; /*!< The csrk key value */
  295. }esp_ble_lcsrk_keys; /*!< The csrk key type */
  296. /**
  297. * @brief Structure associated with ESP_KEY_NOTIF_EVT
  298. */
  299. typedef struct
  300. {
  301. esp_bd_addr_t bd_addr; /*!< peer address */
  302. uint32_t passkey; /*!< the numeric value for comparison. If just_works, do not show this number to UI */
  303. } esp_ble_sec_key_notif_t; /*!< BLE key notify type*/
  304. /**
  305. * @brief Structure of the security request
  306. */
  307. typedef struct
  308. {
  309. esp_bd_addr_t bd_addr; /*!< peer address */
  310. }esp_ble_sec_req_t; /*!< BLE security request type*/
  311. /**
  312. * @brief union type of the security key value
  313. */
  314. typedef union
  315. {
  316. esp_ble_penc_keys_t penc_key; /*!< received peer encryption key */
  317. esp_ble_pcsrk_keys_t pcsrk_key; /*!< received peer device SRK */
  318. esp_ble_pid_keys_t pid_key; /*!< peer device ID key */
  319. esp_ble_lenc_keys_t lenc_key; /*!< local encryption reproduction keys LTK = = d1(ER,DIV,0)*/
  320. esp_ble_lcsrk_keys lcsrk_key; /*!< local device CSRK = d1(ER,DIV,1)*/
  321. }esp_ble_key_value_t; /*!< ble key value type*/
  322. /**
  323. * @brief union type of the security key value
  324. */
  325. typedef struct
  326. {
  327. esp_bd_addr_t bd_addr; /*!< peer address */
  328. esp_ble_key_type_t key_type; /*!< key type of the security link */
  329. esp_ble_key_value_t p_key_value; /*!< the pointer to the key value */
  330. }esp_ble_key_t; /*!< the union to the ble key value type*/
  331. /**
  332. * @brief structure type of the ble local id keys value
  333. */
  334. typedef struct {
  335. esp_bt_octet16_t ir; /*!< the 16 bits of the ir value */
  336. esp_bt_octet16_t irk; /*!< the 16 bits of the ir key value */
  337. esp_bt_octet16_t dhk; /*!< the 16 bits of the dh key value */
  338. }esp_ble_local_id_keys_t; /*!< the structure of the ble local id keys value type*/
  339. /**
  340. * @brief Structure associated with ESP_AUTH_CMPL_EVT
  341. */
  342. typedef struct
  343. {
  344. esp_bd_addr_t bd_addr; /*!< BD address peer device. */
  345. bool key_present; /*!< Valid link key value in key element */
  346. esp_link_key key; /*!< Link key associated with peer device. */
  347. uint8_t key_type; /*!< The type of Link Key */
  348. bool success; /*!< TRUE of authentication succeeded, FALSE if failed. */
  349. uint8_t fail_reason; /*!< The HCI reason/error code for when success=FALSE */
  350. esp_bd_addr_type_t addr_type; /*!< Peer device address type */
  351. esp_bt_dev_type_t dev_type; /*!< Device type */
  352. }esp_ble_auth_cmpl_t; /*!< The ble authentication complite cb type */
  353. /**
  354. * @brief union associated with ble security
  355. */
  356. typedef union
  357. {
  358. esp_ble_sec_key_notif_t key_notif; /*!< passkey notification */
  359. esp_ble_sec_req_t ble_req; /*!< BLE SMP related request */
  360. esp_ble_key_t ble_key; /*!< BLE SMP keys used when pairing */
  361. esp_ble_local_id_keys_t ble_id_keys; /*!< BLE IR event */
  362. esp_ble_auth_cmpl_t auth_cmpl; /*!< Authentication complete indication. */
  363. }esp_ble_sec_t; /*!< Ble secutity type */
  364. /// Sub Event of ESP_GAP_BLE_SCAN_RESULT_EVT
  365. typedef enum {
  366. ESP_GAP_SEARCH_INQ_RES_EVT = 0, /*!< Inquiry result for a peer device. */
  367. ESP_GAP_SEARCH_INQ_CMPL_EVT = 1, /*!< Inquiry complete. */
  368. ESP_GAP_SEARCH_DISC_RES_EVT = 2, /*!< Discovery result for a peer device. */
  369. ESP_GAP_SEARCH_DISC_BLE_RES_EVT = 3, /*!< Discovery result for BLE GATT based service on a peer device. */
  370. ESP_GAP_SEARCH_DISC_CMPL_EVT = 4, /*!< Discovery complete. */
  371. ESP_GAP_SEARCH_DI_DISC_CMPL_EVT = 5, /*!< Discovery complete. */
  372. ESP_GAP_SEARCH_SEARCH_CANCEL_CMPL_EVT = 6, /*!< Search cancelled */
  373. } esp_gap_search_evt_t;
  374. /**
  375. * @brief Ble scan result event type, to indicate the
  376. * result is scan response or advertising data or other
  377. */
  378. typedef enum {
  379. ESP_BLE_EVT_CONN_ADV = 0x00, /*!< Connectable undirected advertising (ADV_IND) */
  380. ESP_BLE_EVT_CONN_DIR_ADV = 0x01, /*!< Connectable directed advertising (ADV_DIRECT_IND) */
  381. ESP_BLE_EVT_DISC_ADV = 0x02, /*!< Scannable undirected advertising (ADV_SCAN_IND) */
  382. ESP_BLE_EVT_NON_CONN_ADV = 0x03, /*!< Non connectable undirected advertising (ADV_NONCONN_IND) */
  383. ESP_BLE_EVT_SCAN_RSP = 0x04, /*!< Scan Response (SCAN_RSP) */
  384. } esp_ble_evt_type_t;
  385. /**
  386. * @brief Gap callback parameters union
  387. */
  388. typedef union {
  389. /**
  390. * @brief ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT
  391. */
  392. struct ble_adv_data_cmpl_evt_param {
  393. esp_bt_status_t status; /*!< Indicate the set advertising data operation success status */
  394. } adv_data_cmpl; /*!< Event parameter of ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT */
  395. /**
  396. * @brief ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT
  397. */
  398. struct ble_scan_rsp_data_cmpl_evt_param {
  399. esp_bt_status_t status; /*!< Indicate the set scan response data operation success status */
  400. } scan_rsp_data_cmpl; /*!< Event parameter of ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT */
  401. /**
  402. * @brief ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT
  403. */
  404. struct ble_scan_param_cmpl_evt_param {
  405. esp_bt_status_t status; /*!< Indicate the set scan param operation success status */
  406. } scan_param_cmpl; /*!< Event parameter of ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT */
  407. /**
  408. * @brief ESP_GAP_BLE_SCAN_RESULT_EVT
  409. */
  410. struct ble_scan_result_evt_param {
  411. esp_gap_search_evt_t search_evt; /*!< Search event type */
  412. esp_bd_addr_t bda; /*!< Bluetooth device address which has been searched */
  413. esp_bt_dev_type_t dev_type; /*!< Device type */
  414. esp_ble_addr_type_t ble_addr_type; /*!< Ble device address type */
  415. esp_ble_evt_type_t ble_evt_type; /*!< Ble scan result event type */
  416. int rssi; /*!< Searched device's RSSI */
  417. uint8_t ble_adv[ESP_BLE_ADV_DATA_LEN_MAX + ESP_BLE_SCAN_RSP_DATA_LEN_MAX]; /*!< Received EIR */
  418. int flag; /*!< Advertising data flag bit */
  419. int num_resps; /*!< Scan result number */
  420. uint8_t adv_data_len; /*!< Adv data length */
  421. uint8_t scan_rsp_len; /*!< Scan response length */
  422. } scan_rst; /*!< Event parameter of ESP_GAP_BLE_SCAN_RESULT_EVT */
  423. /**
  424. * @brief ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT
  425. */
  426. struct ble_adv_data_raw_cmpl_evt_param {
  427. esp_bt_status_t status; /*!< Indicate the set raw advertising data operation success status */
  428. } adv_data_raw_cmpl; /*!< Event parameter of ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT */
  429. /**
  430. * @brief ESP_GAP_BLE_SCAN_RSP_DATA_RAW_SET_COMPLETE_EVT
  431. */
  432. struct ble_scan_rsp_data_raw_cmpl_evt_param {
  433. esp_bt_status_t status; /*!< Indicate the set raw advertising data operation success status */
  434. } scan_rsp_data_raw_cmpl; /*!< Event parameter of ESP_GAP_BLE_SCAN_RSP_DATA_RAW_SET_COMPLETE_EVT */
  435. /**
  436. * @brief ESP_GAP_BLE_ADV_START_COMPLETE_EVT
  437. */
  438. struct ble_adv_start_cmpl_evt_param {
  439. esp_bt_status_t status; /*!< Indicate advertising start operation success status */
  440. } adv_start_cmpl; /*!< Event parameter of ESP_GAP_BLE_ADV_START_COMPLETE_EVT */
  441. /**
  442. * @brief ESP_GAP_BLE_SCAN_START_COMPLETE_EVT
  443. */
  444. struct ble_scan_start_cmpl_evt_param {
  445. esp_bt_status_t status; /*!< Indicate scan start operation success status */
  446. } scan_start_cmpl; /*!< Event parameter of ESP_GAP_BLE_SCAN_START_COMPLETE_EVT */
  447. esp_ble_sec_t ble_security; /*!< ble gap security union type */
  448. /**
  449. * @brief ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT
  450. */
  451. struct ble_scan_stop_cmpl_evt_param {
  452. esp_bt_status_t status; /*!< Indicate scan stop operation success status */
  453. } scan_stop_cmpl; /*!< Event parameter of ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT */
  454. /**
  455. * @brief ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT
  456. */
  457. struct ble_adv_stop_cmpl_evt_param {
  458. esp_bt_status_t status; /*!< Indicate adv stop operation success status */
  459. } adv_stop_cmpl; /*!< Event parameter of ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT */
  460. } esp_ble_gap_cb_param_t;
  461. /**
  462. * @brief GAP callback function type
  463. * @param event : Event type
  464. * @param param : Point to callback parameter, currently is union type
  465. */
  466. typedef void (* esp_gap_ble_cb_t)(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
  467. /**
  468. * @brief This function is called to occur gap event, such as scan result
  469. *
  470. * @param[in] callback: callback function
  471. *
  472. * @return
  473. * - ESP_OK : success
  474. * - other : failed
  475. *
  476. */
  477. esp_err_t esp_ble_gap_register_callback(esp_gap_ble_cb_t callback);
  478. /**
  479. * @brief This function is called to override the BTA default ADV parameters.
  480. *
  481. * @param[in] adv_data: Pointer to User defined ADV data structure. This
  482. * memory space can not be freed until callback of config_adv_data
  483. * is received.
  484. *
  485. * @return
  486. * - ESP_OK : success
  487. * - other : failed
  488. *
  489. */
  490. esp_err_t esp_ble_gap_config_adv_data (esp_ble_adv_data_t *adv_data);
  491. /**
  492. * @brief This function is called to set scan parameters
  493. *
  494. * @param[in] scan_params: Pointer to User defined scan_params data structure. This
  495. * memory space can not be freed until callback of set_scan_params
  496. *
  497. * @return
  498. * - ESP_OK : success
  499. * - other : failed
  500. *
  501. */
  502. esp_err_t esp_ble_gap_set_scan_params(esp_ble_scan_params_t *scan_params);
  503. /**
  504. * @brief This procedure keep the device scanning the peer device which advertising on the air
  505. *
  506. * @param[in] duration: Keeping the scanning time, the unit is second.
  507. *
  508. * @return
  509. * - ESP_OK : success
  510. * - other : failed
  511. *
  512. */
  513. esp_err_t esp_ble_gap_start_scanning(uint32_t duration);
  514. /**
  515. * @brief This function call to stop the device scanning the peer device which advertising on the air
  516. * @return
  517. * - ESP_OK : success
  518. * - other : failed
  519. *
  520. */
  521. esp_err_t esp_ble_gap_stop_scanning(void);
  522. /**
  523. * @brief This function is called to start advertising.
  524. *
  525. * @param[in] adv_params: pointer to User defined adv_params data structure.
  526. * @return
  527. * - ESP_OK : success
  528. * - other : failed
  529. *
  530. */
  531. esp_err_t esp_ble_gap_start_advertising (esp_ble_adv_params_t *adv_params);
  532. /**
  533. * @brief This function is called to stop advertising.
  534. *
  535. * @return
  536. * - ESP_OK : success
  537. * - other : failed
  538. *
  539. */
  540. esp_err_t esp_ble_gap_stop_advertising(void);
  541. /**
  542. * @brief Update connection parameters, can only be used when connection is up.
  543. *
  544. * @param[in] params - connection update parameters
  545. *
  546. * @return
  547. * - ESP_OK : success
  548. * - other : failed
  549. *
  550. */
  551. esp_err_t esp_ble_gap_update_conn_params(esp_ble_conn_update_params_t *params);
  552. /**
  553. * @brief This function is to set maximum LE data packet size
  554. *
  555. * @return
  556. * - ESP_OK : success
  557. * - other : failed
  558. *
  559. */
  560. esp_err_t esp_ble_gap_set_pkt_data_len(esp_bd_addr_t remote_device, uint16_t tx_data_length);
  561. /**
  562. * @brief This function set the random address for the application
  563. *
  564. * @param[in] rand_addr: the random address which should be setting
  565. *
  566. * @return
  567. * - ESP_OK : success
  568. * - other : failed
  569. *
  570. */
  571. esp_err_t esp_ble_gap_set_rand_addr(esp_bd_addr_t rand_addr);
  572. /**
  573. * @brief Enable/disable privacy on the local device
  574. *
  575. * @param[in] privacy_enable - enable/disable privacy on remote device.
  576. *
  577. * @return
  578. * - ESP_OK : success
  579. * - other : failed
  580. *
  581. */
  582. esp_err_t esp_ble_gap_config_local_privacy (bool privacy_enable);
  583. /**
  584. * @brief Set device name to the local device
  585. *
  586. * @param[in] name - device name.
  587. *
  588. * @return
  589. * - ESP_OK : success
  590. * - other : failed
  591. *
  592. */
  593. esp_err_t esp_ble_gap_set_device_name(const char *name);
  594. /**
  595. * @brief This function is called to get ADV data for a specific type.
  596. *
  597. * @param[in] adv_data - pointer of ADV data which to be resolved
  598. * @param[in] type - finding ADV data type
  599. * @param[out] length - return the length of ADV data not including type
  600. *
  601. * @return - ESP_OK : success
  602. * - other : failed
  603. *
  604. */
  605. uint8_t *esp_ble_resolve_adv_data(uint8_t *adv_data, uint8_t type, uint8_t *length);
  606. /**
  607. * @brief This function is called to set raw advertising data. User need to fill
  608. * ADV data by self.
  609. *
  610. * @param[in] raw_data : raw advertising data
  611. * @param[in] raw_data_len : raw advertising data length , less than 31 bytes
  612. *
  613. * @return
  614. * - ESP_OK : success
  615. * - other : failed
  616. *
  617. */
  618. esp_err_t esp_ble_gap_config_adv_data_raw(uint8_t *raw_data, uint32_t raw_data_len);
  619. /**
  620. * @brief This function is called to set raw scan response data. User need to fill
  621. * scan response data by self.
  622. *
  623. * @param[in] raw_data : raw scan response data
  624. * @param[in] raw_data_len : raw scan response data length , less than 31 bytes
  625. *
  626. * @return
  627. * - ESP_OK : success
  628. * - other : failed
  629. */
  630. esp_err_t esp_ble_gap_config_scan_rsp_data_raw(uint8_t *raw_data, uint32_t raw_data_len);
  631. /**
  632. * @brief Set a GAP security parameter value. Overrides the default value.
  633. *
  634. * @param[in] param_type :L the type of the param which to be set
  635. * @param[in] value : the param value
  636. * @param[out] len : the length of the param value
  637. *
  638. * @return - ESP_OK : success
  639. * - other : failed
  640. *
  641. */
  642. esp_err_t esp_ble_gap_set_security_param(esp_ble_sm_param_t param_type,
  643. void *value, uint8_t len);
  644. /**
  645. * @brief Grant security request access.
  646. *
  647. * @param[in] bd_addr : BD address of the peer
  648. * @param[in] accept : accept the security request or not
  649. *
  650. * @return - ESP_OK : success
  651. * - other : failed
  652. *
  653. */
  654. esp_err_t esp_ble_gap_security_rsp(esp_bd_addr_t bd_addr, bool accept);
  655. /**
  656. * @brief Set a gap parameter value. Use this function to change
  657. * the default GAP parameter values.
  658. *
  659. * @param[in] bd_addr : the address of the peer device need to encryption
  660. * @param[in] sec_act : This is the security action to indicate
  661. * what kind of BLE security level is required for
  662. * the BLE link if the BLE is supported
  663. *
  664. * @return - ESP_OK : success
  665. * - other : failed
  666. *
  667. */
  668. esp_err_t esp_ble_set_encryption(esp_bd_addr_t bd_addr, esp_ble_sec_act_t sec_act);
  669. /**
  670. * @brief Reply the key value to the peer device in the lagecy connection stage.
  671. *
  672. * @param[in] bd_addr : BD address of the peer
  673. * @param[in] accept : passkey entry sucessful or declined.
  674. * @param[in] passkey : passkey value, must be a 6 digit number,
  675. * can be lead by 0.
  676. *
  677. * @return - ESP_OK : success
  678. * - other : failed
  679. *
  680. */
  681. esp_err_t esp_ble_passkey_reply(esp_bd_addr_t bd_addr, bool accept, uint32_t passkey);
  682. /**
  683. * @brief Reply the comfirm value to the peer device in the lagecy connection stage.
  684. *
  685. * @param[in] bd_addr : BD address of the peer device
  686. * @param[in] accept : numbers to compare are the same or different.
  687. *
  688. * @return - ESP_OK : success
  689. * - other : failed
  690. *
  691. */
  692. esp_err_t esp_ble_confirm_reply(esp_bd_addr_t bd_addr, bool accept);
  693. #ifdef __cplusplus
  694. }
  695. #endif
  696. #endif /* __ESP_GAP_BLE_API_H__ */