esp_blufi_api.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef __ESP_BLUFI_API_H__
  7. #define __ESP_BLUFI_API_H__
  8. #include "esp_err.h"
  9. #include "esp_wifi_types.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. typedef enum {
  14. ESP_BLUFI_EVENT_INIT_FINISH = 0, /*<! When BLUFI init complete, this event happen */
  15. ESP_BLUFI_EVENT_DEINIT_FINISH, /*<! When BLUFI deinit complete, this event happen */
  16. ESP_BLUFI_EVENT_SET_WIFI_OPMODE, /*<! When Phone set ESP32 wifi operation mode(AP/STA/AP_STA), this event happen */
  17. ESP_BLUFI_EVENT_BLE_CONNECT, /*<! When Phone connect to ESP32 with BLE, this event happen */
  18. ESP_BLUFI_EVENT_BLE_DISCONNECT, /*<! When Phone disconnect with BLE, this event happen */
  19. ESP_BLUFI_EVENT_REQ_CONNECT_TO_AP, /*<! When Phone request ESP32's STA connect to AP, this event happen */
  20. ESP_BLUFI_EVENT_REQ_DISCONNECT_FROM_AP, /*<! When Phone request ESP32's STA disconnect from AP, this event happen */
  21. ESP_BLUFI_EVENT_GET_WIFI_STATUS, /*<! When Phone get ESP32 wifi status, this event happen */
  22. ESP_BLUFI_EVENT_DEAUTHENTICATE_STA, /*<! When Phone deauthenticate sta from SOFTAP, this event happen */
  23. /* recv data */
  24. ESP_BLUFI_EVENT_RECV_STA_BSSID, /*<! When Phone send STA BSSID to ESP32 to connect, this event happen */
  25. ESP_BLUFI_EVENT_RECV_STA_SSID, /*<! When Phone send STA SSID to ESP32 to connect, this event happen */
  26. ESP_BLUFI_EVENT_RECV_STA_PASSWD, /*<! When Phone send STA PASSWORD to ESP32 to connect, this event happen */
  27. ESP_BLUFI_EVENT_RECV_SOFTAP_SSID, /*<! When Phone send SOFTAP SSID to ESP32 to start SOFTAP, this event happen */
  28. ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD, /*<! When Phone send SOFTAP PASSWORD to ESP32 to start SOFTAP, this event happen */
  29. ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM, /*<! When Phone send SOFTAP max connection number to ESP32 to start SOFTAP, this event happen */
  30. ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE, /*<! When Phone send SOFTAP authentication mode to ESP32 to start SOFTAP, this event happen */
  31. ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL, /*<! When Phone send SOFTAP channel to ESP32 to start SOFTAP, this event happen */
  32. ESP_BLUFI_EVENT_RECV_USERNAME, /*<! When Phone send username to ESP32, this event happen */
  33. ESP_BLUFI_EVENT_RECV_CA_CERT, /*<! When Phone send CA certificate to ESP32, this event happen */
  34. ESP_BLUFI_EVENT_RECV_CLIENT_CERT, /*<! When Phone send Client certificate to ESP32, this event happen */
  35. ESP_BLUFI_EVENT_RECV_SERVER_CERT, /*<! When Phone send Server certificate to ESP32, this event happen */
  36. ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY, /*<! When Phone send Client Private key to ESP32, this event happen */
  37. ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY, /*<! When Phone send Server Private key to ESP32, this event happen */
  38. ESP_BLUFI_EVENT_RECV_SLAVE_DISCONNECT_BLE, /*<! When Phone send Disconnect key to ESP32, this event happen */
  39. ESP_BLUFI_EVENT_GET_WIFI_LIST, /*<! When Phone send get wifi list command to ESP32, this event happen */
  40. ESP_BLUFI_EVENT_REPORT_ERROR, /*<! When Blufi report error, this event happen */
  41. ESP_BLUFI_EVENT_RECV_CUSTOM_DATA, /*<! When Phone send custom data to ESP32, this event happen */
  42. } esp_blufi_cb_event_t;
  43. /// BLUFI config status
  44. typedef enum {
  45. ESP_BLUFI_STA_CONN_SUCCESS = 0x00,
  46. ESP_BLUFI_STA_CONN_FAIL = 0x01,
  47. } esp_blufi_sta_conn_state_t;
  48. /// BLUFI init status
  49. typedef enum {
  50. ESP_BLUFI_INIT_OK = 0,
  51. ESP_BLUFI_INIT_FAILED,
  52. } esp_blufi_init_state_t;
  53. /// BLUFI deinit status
  54. typedef enum {
  55. ESP_BLUFI_DEINIT_OK = 0,
  56. ESP_BLUFI_DEINIT_FAILED,
  57. } esp_blufi_deinit_state_t;
  58. typedef enum {
  59. ESP_BLUFI_SEQUENCE_ERROR = 0,
  60. ESP_BLUFI_CHECKSUM_ERROR,
  61. ESP_BLUFI_DECRYPT_ERROR,
  62. ESP_BLUFI_ENCRYPT_ERROR,
  63. ESP_BLUFI_INIT_SECURITY_ERROR,
  64. ESP_BLUFI_DH_MALLOC_ERROR,
  65. ESP_BLUFI_DH_PARAM_ERROR,
  66. ESP_BLUFI_READ_PARAM_ERROR,
  67. ESP_BLUFI_MAKE_PUBLIC_ERROR,
  68. ESP_BLUFI_DATA_FORMAT_ERROR,
  69. ESP_BLUFI_CALC_MD5_ERROR,
  70. } esp_blufi_error_state_t;
  71. /**
  72. * @brief BLUFI extra information structure
  73. */
  74. typedef struct {
  75. //station
  76. uint8_t sta_bssid[6]; /*!< BSSID of station interface */
  77. bool sta_bssid_set; /*!< is BSSID of station interface set */
  78. uint8_t *sta_ssid; /*!< SSID of station interface */
  79. int sta_ssid_len; /*!< length of SSID of station interface */
  80. uint8_t *sta_passwd; /*!< password of station interface */
  81. int sta_passwd_len; /*!< length of password of station interface */
  82. uint8_t *softap_ssid; /*!< SSID of softap interface */
  83. int softap_ssid_len; /*!< length of SSID of softap interface */
  84. uint8_t *softap_passwd; /*!< password of station interface */
  85. int softap_passwd_len; /*!< length of password of station interface */
  86. uint8_t softap_authmode; /*!< authentication mode of softap interface */
  87. bool softap_authmode_set; /*!< is authentication mode of softap interface set */
  88. uint8_t softap_max_conn_num; /*!< max connection number of softap interface */
  89. bool softap_max_conn_num_set; /*!< is max connection number of softap interface set */
  90. uint8_t softap_channel; /*!< channel of softap interface */
  91. bool softap_channel_set; /*!< is channel of softap interface set */
  92. } esp_blufi_extra_info_t;
  93. /** @brief Description of an WiFi AP */
  94. typedef struct {
  95. uint8_t ssid[33]; /**< SSID of AP */
  96. int8_t rssi; /**< signal strength of AP */
  97. } esp_blufi_ap_record_t;
  98. /// Bluetooth address length
  99. #define ESP_BLUFI_BD_ADDR_LEN 6
  100. /// Bluetooth device address
  101. typedef uint8_t esp_blufi_bd_addr_t[ESP_BLUFI_BD_ADDR_LEN];
  102. /**
  103. * @brief BLUFI callback parameters union
  104. */
  105. typedef union {
  106. /**
  107. * @brief ESP_BLUFI_EVENT_INIT_FINISH
  108. */
  109. struct blufi_init_finish_evt_param {
  110. esp_blufi_init_state_t state; /*!< Initial status */
  111. } init_finish; /*!< Blufi callback param of ESP_BLUFI_EVENT_INIT_FINISH */
  112. /**
  113. * @brief ESP_BLUFI_EVENT_DEINIT_FINISH
  114. */
  115. struct blufi_deinit_finish_evt_param {
  116. esp_blufi_deinit_state_t state; /*!< De-initial status */
  117. } deinit_finish; /*!< Blufi callback param of ESP_BLUFI_EVENT_DEINIT_FINISH */
  118. /**
  119. * @brief ESP_BLUFI_EVENT_SET_WIFI_MODE
  120. */
  121. struct blufi_set_wifi_mode_evt_param {
  122. wifi_mode_t op_mode; /*!< Wifi operation mode */
  123. } wifi_mode; /*!< Blufi callback param of ESP_BLUFI_EVENT_INIT_FINISH */
  124. /**
  125. * @brief ESP_BLUFI_EVENT_CONNECT
  126. */
  127. struct blufi_connect_evt_param {
  128. esp_blufi_bd_addr_t remote_bda; /*!< Blufi Remote bluetooth device address */
  129. uint8_t server_if; /*!< server interface */
  130. uint16_t conn_id; /*!< Connection id */
  131. } connect; /*!< Blufi callback param of ESP_BLUFI_EVENT_CONNECT */
  132. /**
  133. * @brief ESP_BLUFI_EVENT_DISCONNECT
  134. */
  135. struct blufi_disconnect_evt_param {
  136. esp_blufi_bd_addr_t remote_bda; /*!< Blufi Remote bluetooth device address */
  137. } disconnect; /*!< Blufi callback param of ESP_BLUFI_EVENT_DISCONNECT */
  138. /* ESP_BLUFI_EVENT_REQ_WIFI_CONNECT */ /* No callback param */
  139. /* ESP_BLUFI_EVENT_REQ_WIFI_DISCONNECT */ /* No callback param */
  140. /**
  141. * @brief ESP_BLUFI_EVENT_RECV_STA_BSSID
  142. */
  143. struct blufi_recv_sta_bssid_evt_param {
  144. uint8_t bssid[6]; /*!< BSSID */
  145. } sta_bssid; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_STA_BSSID */
  146. /**
  147. * @brief ESP_BLUFI_EVENT_RECV_STA_SSID
  148. */
  149. struct blufi_recv_sta_ssid_evt_param {
  150. uint8_t *ssid; /*!< SSID */
  151. int ssid_len; /*!< SSID length */
  152. } sta_ssid; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_STA_SSID */
  153. /**
  154. * @brief
  155. * ESP_BLUFI_EVENT_RECV_STA_PASSWD
  156. */
  157. struct blufi_recv_sta_passwd_evt_param {
  158. uint8_t *passwd; /*!< Password */
  159. int passwd_len; /*!< Password Length */
  160. } sta_passwd; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_STA_PASSWD */
  161. /**
  162. * @brief ESP_BLUFI_EVENT_RECV_SOFTAP_SSID
  163. */
  164. struct blufi_recv_softap_ssid_evt_param {
  165. uint8_t *ssid; /*!< SSID */
  166. int ssid_len; /*!< SSID length */
  167. } softap_ssid; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SOFTAP_SSID */
  168. /**
  169. * @brief
  170. * ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD
  171. */
  172. struct blufi_recv_softap_passwd_evt_param {
  173. uint8_t *passwd; /*!< Password */
  174. int passwd_len; /*!< Password Length */
  175. } softap_passwd; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD */
  176. /**
  177. * @brief ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM
  178. */
  179. struct blufi_recv_softap_max_conn_num_evt_param {
  180. int max_conn_num; /*!< SSID */
  181. } softap_max_conn_num; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM */
  182. /**
  183. * @brief
  184. * ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE
  185. */
  186. struct blufi_recv_softap_auth_mode_evt_param {
  187. wifi_auth_mode_t auth_mode; /*!< Authentication mode */
  188. } softap_auth_mode; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE */
  189. /**
  190. * @brief
  191. * ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL
  192. */
  193. struct blufi_recv_softap_channel_evt_param {
  194. uint8_t channel; /*!< Authentication mode */
  195. } softap_channel; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL */
  196. /**
  197. * @brief ESP_BLUFI_EVENT_RECV_USERNAME
  198. */
  199. struct blufi_recv_username_evt_param {
  200. uint8_t *name; /*!< Username point */
  201. int name_len; /*!< Username length */
  202. } username; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_USERNAME*/
  203. /**
  204. * @brief ESP_BLUFI_EVENT_RECV_CA_CERT
  205. */
  206. struct blufi_recv_ca_evt_param {
  207. uint8_t *cert; /*!< CA certificate point */
  208. int cert_len; /*!< CA certificate length */
  209. } ca; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_CA_CERT */
  210. /**
  211. * ESP_BLUFI_EVENT_RECV_CLIENT_CERT
  212. */
  213. struct blufi_recv_client_cert_evt_param {
  214. uint8_t *cert; /*!< Client certificate point */
  215. int cert_len; /*!< Client certificate length */
  216. } client_cert; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_CLIENT_CERT */
  217. /**
  218. * ESP_BLUFI_EVENT_RECV_SERVER_CERT
  219. */
  220. struct blufi_recv_server_cert_evt_param {
  221. uint8_t *cert; /*!< Client certificate point */
  222. int cert_len; /*!< Client certificate length */
  223. } server_cert; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SERVER_CERT */
  224. /**
  225. * ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY
  226. */
  227. struct blufi_recv_client_pkey_evt_param {
  228. uint8_t *pkey; /*!< Client Private Key point, if Client certificate not contain Key */
  229. int pkey_len; /*!< Client Private key length */
  230. } client_pkey; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY */
  231. /**
  232. * ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY
  233. */
  234. struct blufi_recv_server_pkey_evt_param {
  235. uint8_t *pkey; /*!< Client Private Key point, if Client certificate not contain Key */
  236. int pkey_len; /*!< Client Private key length */
  237. } server_pkey; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY */
  238. /**
  239. * @brief
  240. * ESP_BLUFI_EVENT_REPORT_ERROR
  241. */
  242. struct blufi_get_error_evt_param {
  243. esp_blufi_error_state_t state; /*!< Blufi error state */
  244. } report_error; /*!< Blufi callback param of ESP_BLUFI_EVENT_REPORT_ERROR */
  245. /**
  246. * @brief
  247. * ESP_BLUFI_EVENT_RECV_CUSTOM_DATA
  248. */
  249. struct blufi_recv_custom_data_evt_param {
  250. uint8_t *data; /*!< Custom data */
  251. uint32_t data_len; /*!< Custom data Length */
  252. } custom_data; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_CUSTOM_DATA */
  253. } esp_blufi_cb_param_t;
  254. /**
  255. * @brief BLUFI event callback function type
  256. * @param event : Event type
  257. * @param param : Point to callback parameter, currently is union type
  258. */
  259. typedef void (* esp_blufi_event_cb_t)(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param);
  260. /* security function declare */
  261. /**
  262. * @brief BLUFI negotiate data handler
  263. * @param data : data from phone
  264. * @param len : length of data from phone
  265. * @param output_data : data want to send to phone
  266. * @param output_len : length of data want to send to phone
  267. * @param need_free : output reporting if memory needs to be freed or not *
  268. */
  269. typedef void (*esp_blufi_negotiate_data_handler_t)(uint8_t *data, int len, uint8_t **output_data, int *output_len, bool *need_free);
  270. /**
  271. * @brief BLUFI encrypt the data after negotiate a share key
  272. * @param iv8 : initial vector(8bit), normally, blufi core will input packet sequence number
  273. * @param crypt_data : plain text and encrypted data, the encrypt function must support autochthonous encrypt
  274. * @param crypt_len : length of plain text
  275. * @return Nonnegative number is encrypted length, if error, return negative number;
  276. */
  277. typedef int (* esp_blufi_encrypt_func_t)(uint8_t iv8, uint8_t *crypt_data, int crypt_len);
  278. /**
  279. * @brief BLUFI decrypt the data after negotiate a share key
  280. * @param iv8 : initial vector(8bit), normally, blufi core will input packet sequence number
  281. * @param crypt_data : encrypted data and plain text, the encrypt function must support autochthonous decrypt
  282. * @param crypt_len : length of encrypted text
  283. * @return Nonnegative number is decrypted length, if error, return negative number;
  284. */
  285. typedef int (* esp_blufi_decrypt_func_t)(uint8_t iv8, uint8_t *crypt_data, int crypt_len);
  286. /**
  287. * @brief BLUFI checksum
  288. * @param iv8 : initial vector(8bit), normally, blufi core will input packet sequence number
  289. * @param data : data need to checksum
  290. * @param len : length of data
  291. */
  292. typedef uint16_t (*esp_blufi_checksum_func_t)(uint8_t iv8, uint8_t *data, int len);
  293. /**
  294. * @brief BLUFI callback functions type
  295. */
  296. typedef struct {
  297. esp_blufi_event_cb_t event_cb; /*!< BLUFI event callback */
  298. esp_blufi_negotiate_data_handler_t negotiate_data_handler; /*!< BLUFI negotiate data function for negotiate share key */
  299. esp_blufi_encrypt_func_t encrypt_func; /*!< BLUFI encrypt data function with share key generated by negotiate_data_handler */
  300. esp_blufi_decrypt_func_t decrypt_func; /*!< BLUFI decrypt data function with share key generated by negotiate_data_handler */
  301. esp_blufi_checksum_func_t checksum_func; /*!< BLUFI check sum function (FCS) */
  302. } esp_blufi_callbacks_t;
  303. /**
  304. *
  305. * @brief This function is called to receive blufi callback event
  306. *
  307. * @param[in] callbacks: callback functions
  308. *
  309. * @return ESP_OK - success, other - failed
  310. *
  311. */
  312. esp_err_t esp_blufi_register_callbacks(esp_blufi_callbacks_t *callbacks);
  313. /**
  314. *
  315. * @brief This function is called to initialize blufi_profile
  316. *
  317. * @return ESP_OK - success, other - failed
  318. *
  319. */
  320. esp_err_t esp_blufi_profile_init(void);
  321. /**
  322. *
  323. * @brief This function is called to de-initialize blufi_profile
  324. *
  325. * @return ESP_OK - success, other - failed
  326. *
  327. */
  328. esp_err_t esp_blufi_profile_deinit(void);
  329. /**
  330. *
  331. * @brief This function is called to send wifi connection report
  332. * @param opmode : wifi opmode
  333. * @param sta_conn_state : station is already in connection or not
  334. * @param softap_conn_num : softap connection number
  335. * @param extra_info : extra information, such as sta_ssid, softap_ssid and etc.
  336. *
  337. * @return ESP_OK - success, other - failed
  338. *
  339. */
  340. esp_err_t esp_blufi_send_wifi_conn_report(wifi_mode_t opmode, esp_blufi_sta_conn_state_t sta_conn_state, uint8_t softap_conn_num, esp_blufi_extra_info_t *extra_info);
  341. /**
  342. *
  343. * @brief This function is called to send wifi list
  344. * @param apCount : wifi list count
  345. * @param list : wifi list
  346. *
  347. * @return ESP_OK - success, other - failed
  348. *
  349. */
  350. esp_err_t esp_blufi_send_wifi_list(uint16_t apCount, esp_blufi_ap_record_t *list);
  351. /**
  352. *
  353. * @brief Get BLUFI profile version
  354. *
  355. * @return Most 8bit significant is Great version, Least 8bit is Sub version
  356. *
  357. */
  358. uint16_t esp_blufi_get_version(void);
  359. /**
  360. *
  361. * @brief This function is called to send blufi error information
  362. * @param state : error state
  363. *
  364. * @return ESP_OK - success, other - failed
  365. *
  366. */
  367. esp_err_t esp_blufi_send_error_info(esp_blufi_error_state_t state);
  368. /**
  369. *
  370. * @brief This function is called to custom data
  371. * @param data : custom data value
  372. * @param data_len : the length of custom data
  373. *
  374. * @return ESP_OK - success, other - failed
  375. *
  376. */
  377. esp_err_t esp_blufi_send_custom_data(uint8_t *data, uint32_t data_len);
  378. #ifdef __cplusplus
  379. }
  380. #endif
  381. #endif /* _ESP_BLUFI_API_ */