esp_blufi_api.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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_cb_event_t;
  48. /// BLUFI config status
  49. typedef enum {
  50. ESP_BLUFI_STA_CONN_SUCCESS = 0x00,
  51. ESP_BLUFI_STA_CONN_FAIL = 0x01,
  52. } esp_blufi_sta_conn_state_t;
  53. /// BLUFI init status
  54. typedef enum {
  55. ESP_BLUFI_INIT_OK = 0,
  56. ESP_BLUFI_INIT_FAILED = 0,
  57. } esp_blufi_init_state_t;
  58. /// BLUFI deinit status
  59. typedef enum {
  60. ESP_BLUFI_DEINIT_OK = 0,
  61. ESP_BLUFI_DEINIT_FAILED = 0,
  62. } esp_blufi_deinit_state_t;
  63. /**
  64. * @brief BLUFI extra information structure
  65. */
  66. typedef struct {
  67. //station
  68. uint8_t sta_bssid[6]; /*!< BSSID of station interface */
  69. bool sta_bssid_set; /*!< is BSSID of station interface set */
  70. uint8_t *sta_ssid; /*!< SSID of station interface */
  71. int sta_ssid_len; /*!< length of SSID of station interface */
  72. uint8_t *sta_passwd; /*!< password of station interface */
  73. int sta_passwd_len; /*!< length of password of station interface */
  74. uint8_t *softap_ssid; /*!< SSID of softap interface */
  75. int softap_ssid_len; /*!< length of SSID of softap interface */
  76. uint8_t *softap_passwd; /*!< password of station interface */
  77. int softap_passwd_len; /*!< length of password of station interface */
  78. uint8_t softap_authmode; /*!< authentication mode of softap interface */
  79. bool softap_authmode_set; /*!< is authentication mode of softap interface set */
  80. uint8_t softap_max_conn_num; /*!< max connection number of softap interface */
  81. bool softap_max_conn_num_set; /*!< is max connection number of softap interface set */
  82. uint8_t softap_channel; /*!< channel of softap interface */
  83. bool softap_channel_set; /*!< is channel of softap interface set */
  84. } esp_blufi_extra_info_t;
  85. /**
  86. * @brief BLUFI callback parameters union
  87. */
  88. typedef union {
  89. /**
  90. * @brief ESP_BLUFI_EVENT_INIT_FINISH
  91. */
  92. struct blufi_init_finish_evt_param {
  93. esp_blufi_init_state_t state; /*!< Initial status */
  94. } init_finish; /*!< Blufi callback param of ESP_BLUFI_EVENT_INIT_FINISH */
  95. /**
  96. * @brief ESP_BLUFI_EVENT_DEINIT_FINISH
  97. */
  98. struct blufi_deinit_finish_evt_param {
  99. esp_blufi_deinit_state_t state; /*!< De-initial status */
  100. } deinit_finish; /*!< Blufi callback param of ESP_BLUFI_EVENT_DEINIT_FINISH */
  101. /**
  102. * @brief ESP_BLUFI_EVENT_SET_WIFI_MODE
  103. */
  104. struct blufi_set_wifi_mode_evt_param {
  105. wifi_mode_t op_mode; /*!< Wifi operation mode */
  106. } wifi_mode; /*!< Blufi callback param of ESP_BLUFI_EVENT_INIT_FINISH */
  107. /**
  108. * @brief ESP_BLUFI_EVENT_CONNECT
  109. */
  110. struct blufi_connect_evt_param {
  111. esp_bd_addr_t remote_bda; /*!< Blufi Remote bluetooth device address */
  112. } connect; /*!< Blufi callback param of ESP_BLUFI_EVENT_CONNECT */
  113. /**
  114. * @brief ESP_BLUFI_EVENT_DISCONNECT
  115. */
  116. struct blufi_disconnect_evt_param {
  117. esp_bd_addr_t remote_bda; /*!< Blufi Remote bluetooth device address */
  118. } disconnect; /*!< Blufi callback param of ESP_BLUFI_EVENT_DISCONNECT */
  119. /* ESP_BLUFI_EVENT_REQ_WIFI_CONNECT */ /* No callback param */
  120. /* ESP_BLUFI_EVENT_REQ_WIFI_DISCONNECT */ /* No callback param */
  121. /**
  122. * @brief ESP_BLUFI_EVENT_RECV_STA_BSSID
  123. */
  124. struct blufi_recv_sta_bssid_evt_param {
  125. uint8_t bssid[6]; /*!< BSSID */
  126. } sta_bssid; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_STA_BSSID */
  127. /**
  128. * @brief ESP_BLUFI_EVENT_RECV_STA_SSID
  129. */
  130. struct blufi_recv_sta_ssid_evt_param {
  131. uint8_t *ssid; /*!< SSID */
  132. int ssid_len; /*!< SSID length */
  133. } sta_ssid; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_STA_SSID */
  134. /**
  135. * @brief
  136. * ESP_BLUFI_EVENT_RECV_STA_PASSWD
  137. */
  138. struct blufi_recv_sta_passwd_evt_param {
  139. uint8_t *passwd; /*!< Password */
  140. int passwd_len; /*!< Password Length */
  141. } sta_passwd; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_STA_PASSWD */
  142. /**
  143. * @brief ESP_BLUFI_EVENT_RECV_SOFTAP_SSID
  144. */
  145. struct blufi_recv_softap_ssid_evt_param {
  146. uint8_t *ssid; /*!< SSID */
  147. int ssid_len; /*!< SSID length */
  148. } softap_ssid; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SOFTAP_SSID */
  149. /**
  150. * @brief
  151. * ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD
  152. */
  153. struct blufi_recv_softap_passwd_evt_param {
  154. uint8_t *passwd; /*!< Password */
  155. int passwd_len; /*!< Password Length */
  156. } softap_passwd; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD */
  157. /**
  158. * @brief ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM
  159. */
  160. struct blufi_recv_softap_max_conn_num_evt_param {
  161. int max_conn_num; /*!< SSID */
  162. } softap_max_conn_num; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM */
  163. /**
  164. * @brief
  165. * ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE
  166. */
  167. struct blufi_recv_softap_auth_mode_evt_param {
  168. wifi_auth_mode_t auth_mode; /*!< Authentication mode */
  169. } softap_auth_mode; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE */
  170. /**
  171. * @brief
  172. * ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL
  173. */
  174. struct blufi_recv_softap_channel_evt_param {
  175. uint8_t channel; /*!< Authentication mode */
  176. } softap_channel; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL */
  177. /**
  178. * @brief ESP_BLUFI_EVENT_RECV_USERNAME
  179. */
  180. struct blufi_recv_username_evt_param {
  181. uint8_t *name; /*!< Username point */
  182. int name_len; /*!< Username length */
  183. } username; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_USERNAME*/
  184. /**
  185. * @brief ESP_BLUFI_EVENT_RECV_CA_CERT
  186. */
  187. struct blufi_recv_ca_evt_param {
  188. uint8_t *cert; /*!< CA certificate point */
  189. int cert_len; /*!< CA certificate length */
  190. } ca; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_CA_CERT */
  191. /**
  192. * ESP_BLUFI_EVENT_RECV_CLIENT_CERT
  193. */
  194. struct blufi_recv_client_cert_evt_param {
  195. uint8_t *cert; /*!< Client certificate point */
  196. int cert_len; /*!< Client certificate length */
  197. } client_cert; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_CLIENT_CERT */
  198. /**
  199. * ESP_BLUFI_EVENT_RECV_SERVER_CERT
  200. */
  201. struct blufi_recv_server_cert_evt_param {
  202. uint8_t *cert; /*!< Client certificate point */
  203. int cert_len; /*!< Client certificate length */
  204. } server_cert; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SERVER_CERT */
  205. /**
  206. * ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY
  207. */
  208. struct blufi_recv_client_pkey_evt_param {
  209. uint8_t *pkey; /*!< Client Private Key point, if Client certificate not contain Key */
  210. int pkey_len; /*!< Client Private key length */
  211. } client_pkey; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY */
  212. /**
  213. * ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY
  214. */
  215. struct blufi_recv_server_pkey_evt_param {
  216. uint8_t *pkey; /*!< Client Private Key point, if Client certificate not contain Key */
  217. int pkey_len; /*!< Client Private key length */
  218. } server_pkey; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY */
  219. } esp_blufi_cb_param_t;
  220. /**
  221. * @brief BLUFI event callback function type
  222. * @param event : Event type
  223. * @param param : Point to callback parameter, currently is union type
  224. */
  225. typedef void (* esp_blufi_event_cb_t)(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param);
  226. /* security function declare */
  227. /**
  228. * @brief BLUFI negotiate data handler
  229. * @param data : data from phone
  230. * @param len : length of data from phone
  231. * @param output_data : data want to send to phone
  232. * @param output_len : length of data want to send to phone
  233. */
  234. typedef void (*esp_blufi_negotiate_data_handler_t)(uint8_t *data, int len, uint8_t **output_data, int *output_len, bool *need_free);
  235. /**
  236. * @brief BLUFI encrypt the data after negotiate a share key
  237. * @param iv8 : initial vector(8bit), normally, blufi core will input packet sequence number
  238. * @param crypt_data : plain text and encrypted data, the encrypt function must support autochthonous encrypt
  239. * @param crypt_len : length of plain text
  240. * @return Nonnegative number is encrypted length, if error, return negative number;
  241. */
  242. typedef int (* esp_blufi_encrypt_func_t)(uint8_t iv8, uint8_t *crypt_data, int cyprt_len);
  243. /**
  244. * @brief BLUFI decrypt the data after negotiate a share key
  245. * @param iv8 : initial vector(8bit), normally, blufi core will input packet sequence number
  246. * @param crypt_data : encrypted data and plain text, the encrypt function must support autochthonous decrypt
  247. * @param crypt_len : length of encrypted text
  248. * @return Nonnegative number is decrypted length, if error, return negative number;
  249. */
  250. typedef int (* esp_blufi_decrypt_func_t)(uint8_t iv8, uint8_t *crypt_data, int crypt_len);
  251. /**
  252. * @brief BLUFI checksum
  253. * @param iv8 : initial vector(8bit), normally, blufi core will input packet sequence number
  254. * @param data : data need to checksum
  255. * @param len : length of data
  256. */
  257. typedef uint16_t (*esp_blufi_checksum_func_t)(uint8_t iv8, uint8_t *data, int len);
  258. /**
  259. * @brief BLUFI callback functions type
  260. */
  261. typedef struct {
  262. esp_blufi_event_cb_t event_cb; /*!< BLUFI event callback */
  263. esp_blufi_negotiate_data_handler_t negotiate_data_handler; /*!< BLUFI negotiate data function for negotiate share key */
  264. esp_blufi_encrypt_func_t encrypt_func; /*!< BLUFI encrypt data function with share key generated by negotiate_data_handler */
  265. esp_blufi_decrypt_func_t decrypt_func; /*!< BLUFI decrypt data function with share key generated by negotiate_data_handler */
  266. esp_blufi_checksum_func_t checksum_func; /*!< BLUFI check sum function (FCS) */
  267. } esp_blufi_callbacks_t;
  268. /**
  269. *
  270. * @brief This function is called to receive blufi callback event
  271. *
  272. * @param[in] callbacks: callback functions
  273. *
  274. * @return ESP_OK - success, other - failed
  275. *
  276. */
  277. esp_err_t esp_blufi_register_callbacks(esp_blufi_callbacks_t *callbacks);
  278. /**
  279. *
  280. * @brief This function is called to initialize blufi_profile
  281. *
  282. * @return ESP_OK - success, other - failed
  283. *
  284. */
  285. esp_err_t esp_blufi_profile_init(void);
  286. /**
  287. *
  288. * @brief This function is called to de-initialize blufi_profile
  289. *
  290. * @return ESP_OK - success, other - failed
  291. *
  292. */
  293. esp_err_t esp_blufi_profile_deinit(void);
  294. /**
  295. *
  296. * @brief This function is called to send wifi connection report
  297. * @param opmode : wifi opmode
  298. * @param sta_conn_state : station is already in connection or not
  299. * @param softap_conn_num : softap connection number
  300. * @param extra_info : extra information, such as sta_ssid, softap_ssid and etc.
  301. *
  302. * @return ESP_OK - success, other - failed
  303. *
  304. */
  305. 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);
  306. /**
  307. *
  308. * @brief Get BLUFI profile version
  309. *
  310. * @return Most 8bit significant is Great version, Least 8bit is Sub version
  311. *
  312. */
  313. uint16_t esp_blufi_get_version(void);
  314. #ifdef __cplusplus
  315. }
  316. #endif
  317. #endif /* _ESP_BLUFI_API_ */