esp_wifi.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. /* Notes about WiFi Programming
  14. *
  15. * The esp32 WiFi programming model can be depicted as following picture:
  16. *
  17. *
  18. * default handler user handler
  19. * ------------- --------------- ---------------
  20. * | | event | | callback or | |
  21. * | tcpip | ---------> | event | ----------> | application |
  22. * | stack | | task | event | task |
  23. * |-----------| |-------------| |-------------|
  24. * /|\ |
  25. * | |
  26. * event | |
  27. * | |
  28. * | |
  29. * --------------- |
  30. * | | |
  31. * | WiFi Driver |/__________________|
  32. * | |\ API call
  33. * | |
  34. * |-------------|
  35. *
  36. * The WiFi driver can be consider as black box, it knows nothing about the high layer code, such as
  37. * TCPIP stack, application task, event task etc, all it can do is to receive API call from high layer
  38. * or post event queue to a specified Queue, which is initialized by API esp_wifi_init().
  39. *
  40. * The event task is a daemon task, which receives events from WiFi driver or from other subsystem, such
  41. * as TCPIP stack, event task will call the default callback function on receiving the event. For example,
  42. * on receiving event SYSTEM_EVENT_STA_CONNECTED, it will call tcpip_adapter_start() to start the DHCP
  43. * client in it's default handler.
  44. *
  45. * Application can register it's own event callback function by API esp_event_init, then the application callback
  46. * function will be called after the default callback. Also, if application doesn't want to execute the callback
  47. * in the event task, what it needs to do is to post the related event to application task in the application callback function.
  48. *
  49. * The application task (code) generally mixes all these thing together, it calls APIs to init the system/WiFi and
  50. * handle the events when necessary.
  51. *
  52. */
  53. #ifndef __ESP_WIFI_H__
  54. #define __ESP_WIFI_H__
  55. #include <stdint.h>
  56. #include <stdbool.h>
  57. #include "freertos/FreeRTOS.h"
  58. #include "freertos/queue.h"
  59. #include "rom/queue.h"
  60. #include "sdkconfig.h"
  61. #include "esp_err.h"
  62. #include "esp_wifi_types.h"
  63. #include "esp_event.h"
  64. #ifdef __cplusplus
  65. extern "C" {
  66. #endif
  67. #define ESP_ERR_WIFI_OK ESP_OK /*!< No error */
  68. #define ESP_ERR_WIFI_FAIL ESP_FAIL /*!< General fail code */
  69. #define ESP_ERR_WIFI_NO_MEM ESP_ERR_NO_MEM /*!< Out of memory */
  70. #define ESP_ERR_WIFI_ARG ESP_ERR_INVALID_ARG /*!< Invalid argument */
  71. #define ESP_ERR_WIFI_NOT_SUPPORT ESP_ERR_NOT_SUPPORTED /*!< Indicates that API is not supported yet */
  72. #define ESP_ERR_WIFI_NOT_INIT (ESP_ERR_WIFI_BASE + 1) /*!< WiFi driver was not installed by esp_wifi_init */
  73. #define ESP_ERR_WIFI_NOT_STARTED (ESP_ERR_WIFI_BASE + 2) /*!< WiFi driver was not started by esp_wifi_start */
  74. #define ESP_ERR_WIFI_NOT_STOPPED (ESP_ERR_WIFI_BASE + 3) /*!< WiFi driver was not stopped by esp_wifi_stop */
  75. #define ESP_ERR_WIFI_IF (ESP_ERR_WIFI_BASE + 4) /*!< WiFi interface error */
  76. #define ESP_ERR_WIFI_MODE (ESP_ERR_WIFI_BASE + 5) /*!< WiFi mode error */
  77. #define ESP_ERR_WIFI_STATE (ESP_ERR_WIFI_BASE + 6) /*!< WiFi internal state error */
  78. #define ESP_ERR_WIFI_CONN (ESP_ERR_WIFI_BASE + 7) /*!< WiFi internal control block of station or soft-AP error */
  79. #define ESP_ERR_WIFI_NVS (ESP_ERR_WIFI_BASE + 8) /*!< WiFi internal NVS module error */
  80. #define ESP_ERR_WIFI_MAC (ESP_ERR_WIFI_BASE + 9) /*!< MAC address is invalid */
  81. #define ESP_ERR_WIFI_SSID (ESP_ERR_WIFI_BASE + 10) /*!< SSID is invalid */
  82. #define ESP_ERR_WIFI_PASSWORD (ESP_ERR_WIFI_BASE + 11) /*!< Password is invalid */
  83. #define ESP_ERR_WIFI_TIMEOUT (ESP_ERR_WIFI_BASE + 12) /*!< Timeout error */
  84. #define ESP_ERR_WIFI_WAKE_FAIL (ESP_ERR_WIFI_BASE + 13) /*!< WiFi is in sleep state(RF closed) and wakeup fail */
  85. /**
  86. * @brief WiFi stack configuration parameters passed to esp_wifi_init call.
  87. */
  88. typedef struct {
  89. system_event_handler_t event_handler; /**< WiFi event handler */
  90. int static_rx_buf_num; /**< WiFi static RX buffer number */
  91. int dynamic_rx_buf_num; /**< WiFi dynamic RX buffer number */
  92. int tx_buf_type; /**< WiFi TX buffer type */
  93. int static_tx_buf_num; /**< WiFi static TX buffer number */
  94. int dynamic_tx_buf_num; /**< WiFi dynamic TX buffer number */
  95. int ampdu_enable; /**< WiFi AMPDU feature enable flag */
  96. int nvs_enable; /**< WiFi NVS flash enable flag */
  97. int nano_enable; /**< Nano option for printf/scan family enable flag */
  98. int magic; /**< WiFi init magic number, it should be the last field */
  99. } wifi_init_config_t;
  100. #ifdef CONFIG_ESP32_WIFI_STATIC_TX_BUFFER_NUM
  101. #define WIFI_STATIC_TX_BUFFER_NUM CONFIG_ESP32_WIFI_STATIC_TX_BUFFER_NUM
  102. #else
  103. #define WIFI_STATIC_TX_BUFFER_NUM 0
  104. #endif
  105. #ifdef CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM
  106. #define WIFI_DYNAMIC_TX_BUFFER_NUM CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM
  107. #else
  108. #define WIFI_DYNAMIC_TX_BUFFER_NUM 0
  109. #endif
  110. #if CONFIG_ESP32_WIFI_AMPDU_ENABLED
  111. #define WIFI_AMPDU_ENABLED 1
  112. #else
  113. #define WIFI_AMPDU_ENABLED 0
  114. #endif
  115. #if CONFIG_ESP32_WIFI_NVS_ENABLED
  116. #define WIFI_NVS_ENABLED 1
  117. #else
  118. #define WIFI_NVS_ENABLED 0
  119. #endif
  120. #if CONFIG_NEWLIB_NANO_FORMAT
  121. #define WIFI_NANO_FORMAT_ENABLED 1
  122. #else
  123. #define WIFI_NANO_FORMAT_ENABLED 0
  124. #endif
  125. #define WIFI_INIT_CONFIG_MAGIC 0x1F2F3F4F
  126. #ifdef CONFIG_WIFI_ENABLED
  127. #define WIFI_INIT_CONFIG_DEFAULT() { \
  128. .event_handler = &esp_event_send, \
  129. .static_rx_buf_num = CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM,\
  130. .dynamic_rx_buf_num = CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM,\
  131. .tx_buf_type = CONFIG_ESP32_WIFI_TX_BUFFER_TYPE,\
  132. .static_tx_buf_num = WIFI_STATIC_TX_BUFFER_NUM,\
  133. .dynamic_tx_buf_num = WIFI_DYNAMIC_TX_BUFFER_NUM,\
  134. .ampdu_enable = WIFI_AMPDU_ENABLED,\
  135. .nvs_enable = WIFI_NVS_ENABLED,\
  136. .nano_enable = WIFI_NANO_FORMAT_ENABLED,\
  137. .magic = WIFI_INIT_CONFIG_MAGIC\
  138. };
  139. #else
  140. #define WIFI_INIT_CONFIG_DEFAULT() {0}; _Static_assert(0, "please enable wifi in menuconfig to use esp_wifi.h");
  141. #endif
  142. /**
  143. * @brief Init WiFi
  144. * Alloc resource for WiFi driver, such as WiFi control structure, RX/TX buffer,
  145. * WiFi NVS structure etc, this WiFi also start WiFi task
  146. *
  147. * @attention 1. This API must be called before all other WiFi API can be called
  148. * @attention 2. Always use WIFI_INIT_CONFIG_DEFAULT macro to init the config to default values, this can
  149. * guarantee all the fields got correct value when more fields are added into wifi_init_config_t
  150. * in future release. If you want to set your owner initial values, overwrite the default values
  151. * which are set by WIFI_INIT_CONFIG_DEFAULT, please be notified that the field 'magic' of
  152. * wifi_init_config_t should always be WIFI_INIT_CONFIG_MAGIC!
  153. *
  154. * @param config provide WiFi init configuration
  155. *
  156. * @return
  157. * - ESP_OK: succeed
  158. * - ESP_ERR_WIFI_NO_MEM: out of memory
  159. * - others: refer to error code esp_err.h
  160. */
  161. esp_err_t esp_wifi_init(wifi_init_config_t *config);
  162. /**
  163. * @brief Deinit WiFi
  164. * Free all resource allocated in esp_wifi_init and stop WiFi task
  165. *
  166. * @attention 1. This API should be called if you want to remove WiFi driver from the system
  167. *
  168. * @return ESP_OK: succeed
  169. */
  170. esp_err_t esp_wifi_deinit(void);
  171. /**
  172. * @brief Set the WiFi operating mode
  173. *
  174. * Set the WiFi operating mode as station, soft-AP or station+soft-AP,
  175. * The default mode is soft-AP mode.
  176. *
  177. * @param mode WiFi operating mode
  178. *
  179. * @return
  180. * - ESP_OK: succeed
  181. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  182. * - ESP_ERR_WIFI_ARG: invalid argument
  183. * - others: refer to error code in esp_err.h
  184. */
  185. esp_err_t esp_wifi_set_mode(wifi_mode_t mode);
  186. /**
  187. * @brief Get current operating mode of WiFi
  188. *
  189. * @param[out] mode store current WiFi mode
  190. *
  191. * @return
  192. * - ESP_OK: succeed
  193. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  194. * - ESP_ERR_WIFI_ARG: invalid argument
  195. */
  196. esp_err_t esp_wifi_get_mode(wifi_mode_t *mode);
  197. /**
  198. * @brief Start WiFi according to current configuration
  199. * If mode is WIFI_MODE_STA, it create station control block and start station
  200. * If mode is WIFI_MODE_AP, it create soft-AP control block and start soft-AP
  201. * If mode is WIFI_MODE_APSTA, it create soft-AP and station control block and start soft-AP and station
  202. *
  203. * @return
  204. * - ESP_OK: succeed
  205. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  206. * - ESP_ERR_WIFI_ARG: invalid argument
  207. * - ESP_ERR_WIFI_NO_MEM: out of memory
  208. * - ESP_ERR_WIFI_CONN: WiFi internal error, station or soft-AP control block wrong
  209. * - ESP_ERR_WIFI_FAIL: other WiFi internal errors
  210. */
  211. esp_err_t esp_wifi_start(void);
  212. /**
  213. * @brief Stop WiFi
  214. * If mode is WIFI_MODE_STA, it stop station and free station control block
  215. * If mode is WIFI_MODE_AP, it stop soft-AP and free soft-AP control block
  216. * If mode is WIFI_MODE_APSTA, it stop station/soft-AP and free station/soft-AP control block
  217. *
  218. * @return
  219. * - ESP_OK: succeed
  220. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  221. */
  222. esp_err_t esp_wifi_stop(void);
  223. /**
  224. * @brief Restore WiFi stack persistent settings to default values
  225. *
  226. * This function will reset settings made using the following APIs:
  227. * - esp_wifi_get_auto_connect,
  228. * - esp_wifi_set_protocol,
  229. * - esp_wifi_set_config related
  230. * - esp_wifi_set_mode
  231. *
  232. * @return
  233. * - ESP_OK: succeed
  234. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  235. */
  236. esp_err_t esp_wifi_restore(void);
  237. /**
  238. * @brief Connect the ESP32 WiFi station to the AP.
  239. *
  240. * @attention 1. This API only impact WIFI_MODE_STA or WIFI_MODE_APSTA mode
  241. * @attention 2. If the ESP32 is connected to an AP, call esp_wifi_disconnect to disconnect.
  242. *
  243. * @return
  244. * - ESP_OK: succeed
  245. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  246. * - ESP_ERR_WIFI_NOT_START: WiFi is not started by esp_wifi_start
  247. * - ESP_ERR_WIFI_CONN: WiFi internal error, station or soft-AP control block wrong
  248. * - ESP_ERR_WIFI_SSID: SSID of AP which station connects is invalid
  249. */
  250. esp_err_t esp_wifi_connect(void);
  251. /**
  252. * @brief Disconnect the ESP32 WiFi station from the AP.
  253. *
  254. * @return
  255. * - ESP_OK: succeed
  256. * - ESP_ERR_WIFI_NOT_INIT: WiFi was not initialized by eps_wifi_init
  257. * - ESP_ERR_WIFI_NOT_STARTED: WiFi was not started by esp_wifi_start
  258. * - ESP_ERR_WIFI_FAIL: other WiFi internal errors
  259. */
  260. esp_err_t esp_wifi_disconnect(void);
  261. /**
  262. * @brief Currently this API is just an stub API
  263. *
  264. * @return
  265. * - ESP_OK: succeed
  266. * - others: fail
  267. */
  268. esp_err_t esp_wifi_clear_fast_connect(void);
  269. /**
  270. * @brief deauthenticate all stations or associated id equals to aid
  271. *
  272. * @param aid when aid is 0, deauthenticate all stations, otherwise deauthenticate station whose associated id is aid
  273. *
  274. * @return
  275. * - ESP_OK: succeed
  276. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  277. * - ESP_ERR_WIFI_NOT_STARTED: WiFi was not started by esp_wifi_start
  278. * - ESP_ERR_WIFI_ARG: invalid argument
  279. * - ESP_ERR_WIFI_MODE: WiFi mode is wrong
  280. */
  281. esp_err_t esp_wifi_deauth_sta(uint16_t aid);
  282. /**
  283. * @brief Scan all available APs.
  284. *
  285. * @attention If this API is called, the found APs are stored in WiFi driver dynamic allocated memory and the
  286. * will be freed in esp_wifi_get_ap_list, so generally, call esp_wifi_get_ap_list to cause
  287. * the memory to be freed once the scan is done
  288. * @attention The values of maximum active scan time and passive scan time per channel are limited to 1500 milliseconds.
  289. * Values above 1500ms may cause station to disconnect from AP and are not recommended.
  290. *
  291. * @param config configuration of scanning
  292. * @param block if block is true, this API will block the caller until the scan is done, otherwise
  293. * it will return immediately
  294. *
  295. * @return
  296. * - ESP_OK: succeed
  297. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  298. * - ESP_ERR_WIFI_NOT_STARTED: WiFi was not started by esp_wifi_start
  299. * - ESP_ERR_WIFI_TIMEOUT: blocking scan is timeout
  300. * - others: refer to error code in esp_err.h
  301. */
  302. esp_err_t esp_wifi_scan_start(wifi_scan_config_t *config, bool block);
  303. /**
  304. * @brief Stop the scan in process
  305. *
  306. * @return
  307. * - ESP_OK: succeed
  308. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  309. * - ESP_ERR_WIFI_NOT_STARTED: WiFi is not started by esp_wifi_start
  310. */
  311. esp_err_t esp_wifi_scan_stop(void);
  312. /**
  313. * @brief Get number of APs found in last scan
  314. *
  315. * @param[out] number store number of APIs found in last scan
  316. *
  317. * @attention This API can only be called when the scan is completed, otherwise it may get wrong value.
  318. *
  319. * @return
  320. * - ESP_OK: succeed
  321. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  322. * - ESP_ERR_WIFI_NOT_STARTED: WiFi is not started by esp_wifi_start
  323. * - ESP_ERR_WIFI_ARG: invalid argument
  324. */
  325. esp_err_t esp_wifi_scan_get_ap_num(uint16_t *number);
  326. /**
  327. * @brief Get AP list found in last scan
  328. *
  329. * @param[inout] number As input param, it stores max AP number ap_records can hold.
  330. * As output param, it receives the actual AP number this API returns.
  331. * @param ap_records wifi_ap_record_t array to hold the found APs
  332. *
  333. * @return
  334. * - ESP_OK: succeed
  335. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  336. * - ESP_ERR_WIFI_NOT_STARTED: WiFi is not started by esp_wifi_start
  337. * - ESP_ERR_WIFI_ARG: invalid argument
  338. * - ESP_ERR_WIFI_NO_MEM: out of memory
  339. */
  340. esp_err_t esp_wifi_scan_get_ap_records(uint16_t *number, wifi_ap_record_t *ap_records);
  341. /**
  342. * @brief Get information of AP which the ESP32 station is associated with
  343. *
  344. * @param ap_info the wifi_ap_record_t to hold AP information
  345. *
  346. * @return
  347. * - ESP_OK: succeed
  348. * - others: fail
  349. */
  350. esp_err_t esp_wifi_sta_get_ap_info(wifi_ap_record_t *ap_info);
  351. /**
  352. * @brief Set current power save type
  353. *
  354. * @attention Default power save type is WIFI_PS_NONE.
  355. *
  356. * @param type power save type
  357. *
  358. * @return ESP_ERR_WIFI_NOT_SUPPORT: not supported yet
  359. */
  360. esp_err_t esp_wifi_set_ps(wifi_ps_type_t type);
  361. /**
  362. * @brief Get current power save type
  363. *
  364. * @attention Default power save type is WIFI_PS_NONE.
  365. *
  366. * @param[out] type: store current power save type
  367. *
  368. * @return ESP_ERR_WIFI_NOT_SUPPORT: not supported yet
  369. */
  370. esp_err_t esp_wifi_get_ps(wifi_ps_type_t *type);
  371. /**
  372. * @brief Set protocol type of specified interface
  373. * The default protocol is (WIFI_PROTOCOL_11B|WIFI_PROTOCOL_11G|WIFI_PROTOCOL_11N)
  374. *
  375. * @attention Currently we only support 802.11b or 802.11bg or 802.11bgn mode
  376. *
  377. * @param ifx interfaces
  378. * @param protocol_bitmap WiFi protocol bitmap
  379. *
  380. * @return
  381. * - ESP_OK: succeed
  382. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  383. * - ESP_ERR_WIFI_IF: invalid interface
  384. * - others: refer to error codes in esp_err.h
  385. */
  386. esp_err_t esp_wifi_set_protocol(wifi_interface_t ifx, uint8_t protocol_bitmap);
  387. /**
  388. * @brief Get the current protocol bitmap of the specified interface
  389. *
  390. * @param ifx interface
  391. * @param[out] protocol_bitmap store current WiFi protocol bitmap of interface ifx
  392. *
  393. * @return
  394. * - ESP_OK: succeed
  395. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  396. * - ESP_ERR_WIFI_IF: invalid interface
  397. * - ESP_ERR_WIFI_ARG: invalid argument
  398. * - others: refer to error codes in esp_err.h
  399. */
  400. esp_err_t esp_wifi_get_protocol(wifi_interface_t ifx, uint8_t *protocol_bitmap);
  401. /**
  402. * @brief Set the bandwidth of ESP32 specified interface
  403. *
  404. * @attention 1. API return false if try to configure an interface that is not enabled
  405. * @attention 2. WIFI_BW_HT40 is supported only when the interface support 11N
  406. *
  407. * @param ifx interface to be configured
  408. * @param bw bandwidth
  409. *
  410. * @return
  411. * - ESP_OK: succeed
  412. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  413. * - ESP_ERR_WIFI_IF: invalid interface
  414. * - ESP_ERR_WIFI_ARG: invalid argument
  415. * - others: refer to error codes in esp_err.h
  416. */
  417. esp_err_t esp_wifi_set_bandwidth(wifi_interface_t ifx, wifi_bandwidth_t bw);
  418. /**
  419. * @brief Get the bandwidth of ESP32 specified interface
  420. *
  421. * @attention 1. API return false if try to get a interface that is not enable
  422. *
  423. * @param ifx interface to be configured
  424. * @param[out] bw store bandwidth of interface ifx
  425. *
  426. * @return
  427. * - ESP_OK: succeed
  428. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  429. * - ESP_ERR_WIFI_IF: invalid interface
  430. * - ESP_ERR_WIFI_ARG: invalid argument
  431. */
  432. esp_err_t esp_wifi_get_bandwidth(wifi_interface_t ifx, wifi_bandwidth_t *bw);
  433. /**
  434. * @brief Set primary/secondary channel of ESP32
  435. *
  436. * @attention 1. This is a special API for sniffer
  437. * @attention 2. This API should be called after esp_wifi_start() or esp_wifi_set_promiscuous()
  438. *
  439. * @param primary for HT20, primary is the channel number, for HT40, primary is the primary channel
  440. * @param second for HT20, second is ignored, for HT40, second is the second channel
  441. *
  442. * @return
  443. * - ESP_OK: succeed
  444. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  445. * - ESP_ERR_WIFI_IF: invalid interface
  446. * - ESP_ERR_WIFI_ARG: invalid argument
  447. */
  448. esp_err_t esp_wifi_set_channel(uint8_t primary, wifi_second_chan_t second);
  449. /**
  450. * @brief Get the primary/secondary channel of ESP32
  451. *
  452. * @attention 1. API return false if try to get a interface that is not enable
  453. *
  454. * @param primary store current primary channel
  455. * @param[out] second store current second channel
  456. *
  457. * @return
  458. * - ESP_OK: succeed
  459. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  460. * - ESP_ERR_WIFI_ARG: invalid argument
  461. */
  462. esp_err_t esp_wifi_get_channel(uint8_t *primary, wifi_second_chan_t *second);
  463. /**
  464. * @brief Set country code
  465. * The default value is WIFI_COUNTRY_CN
  466. *
  467. * @param country country type
  468. *
  469. * @return
  470. * - ESP_OK: succeed
  471. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  472. * - ESP_ERR_WIFI_ARG: invalid argument
  473. * - others: refer to error code in esp_err.h
  474. */
  475. esp_err_t esp_wifi_set_country(wifi_country_t country);
  476. /**
  477. * @brief Get country code
  478. *
  479. * @param country store current country
  480. *
  481. * @return
  482. * - ESP_OK: succeed
  483. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  484. * - ESP_ERR_WIFI_ARG: invalid argument
  485. */
  486. esp_err_t esp_wifi_get_country(wifi_country_t *country);
  487. /**
  488. * @brief Set MAC address of the ESP32 WiFi station or the soft-AP interface.
  489. *
  490. * @attention 1. This API can only be called when the interface is disabled
  491. * @attention 2. ESP32 soft-AP and station have different MAC addresses, do not set them to be the same.
  492. * @attention 3. The bit 0 of the first byte of ESP32 MAC address can not be 1. For example, the MAC address
  493. * can set to be "1a:XX:XX:XX:XX:XX", but can not be "15:XX:XX:XX:XX:XX".
  494. *
  495. * @param ifx interface
  496. * @param mac the MAC address
  497. *
  498. * @return
  499. * - ESP_OK: succeed
  500. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  501. * - ESP_ERR_WIFI_ARG: invalid argument
  502. * - ESP_ERR_WIFI_IF: invalid interface
  503. * - ESP_ERR_WIFI_MAC: invalid mac address
  504. * - ESP_ERR_WIFI_MODE: WiFi mode is wrong
  505. * - others: refer to error codes in esp_err.h
  506. */
  507. esp_err_t esp_wifi_set_mac(wifi_interface_t ifx, uint8_t mac[6]);
  508. /**
  509. * @brief Get mac of specified interface
  510. *
  511. * @param ifx interface
  512. * @param[out] mac store mac of the interface ifx
  513. *
  514. * @return
  515. * - ESP_OK: succeed
  516. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  517. * - ESP_ERR_WIFI_ARG: invalid argument
  518. * - ESP_ERR_WIFI_IF: invalid interface
  519. */
  520. esp_err_t esp_wifi_get_mac(wifi_interface_t ifx, uint8_t mac[6]);
  521. /**
  522. * @brief The RX callback function in the promiscuous mode.
  523. * Each time a packet is received, the callback function will be called.
  524. *
  525. * @param buf Data received. Type of data in buffer (wifi_promiscuous_pkt_t or wifi_pkt_rx_ctrl_t) indicated by 'type' parameter.
  526. * @param type promiscuous packet type.
  527. *
  528. */
  529. typedef void (* wifi_promiscuous_cb_t)(void *buf, wifi_promiscuous_pkt_type_t type);
  530. /**
  531. * @brief Register the RX callback function in the promiscuous mode.
  532. *
  533. * Each time a packet is received, the registered callback function will be called.
  534. *
  535. * @param cb callback
  536. *
  537. * @return
  538. * - ESP_OK: succeed
  539. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  540. */
  541. esp_err_t esp_wifi_set_promiscuous_rx_cb(wifi_promiscuous_cb_t cb);
  542. /**
  543. * @brief Enable the promiscuous mode.
  544. *
  545. * @param en false - disable, true - enable
  546. *
  547. * @return
  548. * - ESP_OK: succeed
  549. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  550. */
  551. esp_err_t esp_wifi_set_promiscuous(bool en);
  552. /**
  553. * @brief Get the promiscuous mode.
  554. *
  555. * @param[out] en store the current status of promiscuous mode
  556. *
  557. * @return
  558. * - ESP_OK: succeed
  559. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  560. * - ESP_ERR_WIFI_ARG: invalid argument
  561. */
  562. esp_err_t esp_wifi_get_promiscuous(bool *en);
  563. /**
  564. * @brief Set the configuration of the ESP32 STA or AP
  565. *
  566. * @attention 1. This API can be called only when specified interface is enabled, otherwise, API fail
  567. * @attention 2. For station configuration, bssid_set needs to be 0; and it needs to be 1 only when users need to check the MAC address of the AP.
  568. * @attention 3. ESP32 is limited to only one channel, so when in the soft-AP+station mode, the soft-AP will adjust its channel automatically to be the same as
  569. * the channel of the ESP32 station.
  570. *
  571. * @param ifx interface
  572. * @param conf station or soft-AP configuration
  573. *
  574. * @return
  575. * - ESP_OK: succeed
  576. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  577. * - ESP_ERR_WIFI_ARG: invalid argument
  578. * - ESP_ERR_WIFI_IF: invalid interface
  579. * - ESP_ERR_WIFI_MODE: invalid mode
  580. * - ESP_ERR_WIFI_PASSWORD: invalid password
  581. * - ESP_ERR_WIFI_NVS: WiFi internal NVS error
  582. * - others: refer to the erro code in esp_err.h
  583. */
  584. esp_err_t esp_wifi_set_config(wifi_interface_t ifx, wifi_config_t *conf);
  585. /**
  586. * @brief Get configuration of specified interface
  587. *
  588. * @param ifx interface
  589. * @param[out] conf station or soft-AP configuration
  590. *
  591. * @return
  592. * - ESP_OK: succeed
  593. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  594. * - ESP_ERR_WIFI_ARG: invalid argument
  595. * - ESP_ERR_WIFI_IF: invalid interface
  596. */
  597. esp_err_t esp_wifi_get_config(wifi_interface_t ifx, wifi_config_t *conf);
  598. /**
  599. * @brief Get STAs associated with soft-AP
  600. *
  601. * @attention SSC only API
  602. *
  603. * @param[out] sta station list
  604. *
  605. * @return
  606. * - ESP_OK: succeed
  607. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  608. * - ESP_ERR_WIFI_ARG: invalid argument
  609. * - ESP_ERR_WIFI_MODE: WiFi mode is wrong
  610. * - ESP_ERR_WIFI_CONN: WiFi internal error, the station/soft-AP control block is invalid
  611. */
  612. esp_err_t esp_wifi_ap_get_sta_list(wifi_sta_list_t *sta);
  613. /**
  614. * @brief Set the WiFi API configuration storage type
  615. *
  616. * @attention 1. The default value is WIFI_STORAGE_FLASH
  617. *
  618. * @param storage : storage type
  619. *
  620. * @return
  621. * - ESP_OK: succeed
  622. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  623. * - ESP_ERR_WIFI_ARG: invalid argument
  624. */
  625. esp_err_t esp_wifi_set_storage(wifi_storage_t storage);
  626. /**
  627. * @brief Set auto connect
  628. * The default value is true
  629. *
  630. * @param en : true - enable auto connect / false - disable auto connect
  631. *
  632. * @return
  633. * - ESP_OK: succeed
  634. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  635. * - ESP_ERR_WIFI_MODE: WiFi internal error, the station/soft-AP control block is invalid
  636. * - others: refer to error code in esp_err.h
  637. */
  638. esp_err_t esp_wifi_set_auto_connect(bool en);
  639. /**
  640. * @brief Get the auto connect flag
  641. *
  642. * @param[out] en store current auto connect configuration
  643. *
  644. * @return
  645. * - ESP_OK: succeed
  646. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  647. * - ESP_ERR_WIFI_ARG: invalid argument
  648. */
  649. esp_err_t esp_wifi_get_auto_connect(bool *en);
  650. /**
  651. * @brief Set vendor specific element
  652. *
  653. * @param enable enable or not
  654. * @param type information element type
  655. * @param idx information element index
  656. * @param vnd_ie pointer to a vendor specific element
  657. *
  658. * @return
  659. * - ESP_OK: succeed
  660. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  661. * - ESP_ERR_WIFI_ARG: invalid argument
  662. * - ESP_ERR_WIFI_NO_MEM: out of memory
  663. */
  664. esp_err_t esp_wifi_set_vendor_ie(bool enable, wifi_vendor_ie_type_t type, wifi_vendor_ie_id_t idx, uint8_t *vnd_ie);
  665. /**
  666. * @brief Define function pointer for vendor specific element callback
  667. * @param ctx reserved
  668. * @param type information element type
  669. * @param sa source address
  670. * @param vnd_ie pointer to a vendor specific element
  671. * @param rssi received signal strength indication
  672. */
  673. typedef void (*esp_vendor_ie_cb_t) (void *ctx, wifi_vendor_ie_type_t type, const uint8_t sa[6], const uint8_t *vnd_ie, int rssi);
  674. /**
  675. * @brief Set vendor specific element callback
  676. *
  677. * @param cb callback function
  678. * @param ctx reserved
  679. *
  680. * @return
  681. * - ESP_OK: succeed
  682. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by eps_wifi_init
  683. */
  684. esp_err_t esp_wifi_set_vendor_ie_cb(esp_vendor_ie_cb_t cb, void *ctx);
  685. #ifdef __cplusplus
  686. }
  687. #endif
  688. #endif /* __ESP_WIFI_H__ */