esp_blufi_api.h 19 KB

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