esp_blufi_api.h 17 KB

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