wifi.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. /*
  14. * All the APIs declared here are internal only APIs, it can only be used by
  15. * espressif internal modules, such as SSC, LWIP, TCPIP adapter etc, espressif
  16. * customers are not recommended to use them.
  17. *
  18. * If someone really want to use specified APIs declared in here, please contact
  19. * espressif AE/developer to make sure you know the limitations or risk of
  20. * the API, otherwise you may get unexpected behavior!!!
  21. *
  22. */
  23. #ifndef __ESP_WIFI_INTERNAL_H__
  24. #define __ESP_WIFI_INTERNAL_H__
  25. #include <stdint.h>
  26. #include <stdbool.h>
  27. #include "freertos/FreeRTOS.h"
  28. #include "freertos/queue.h"
  29. #include "sys/queue.h"
  30. #include "esp_err.h"
  31. #include "esp_wifi_types.h"
  32. #include "esp_event.h"
  33. #include "esp_wifi.h"
  34. #include "esp_smartconfig.h"
  35. #include "wifi_types.h"
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. typedef struct {
  40. QueueHandle_t handle; /**< FreeRTOS queue handler */
  41. void *storage; /**< storage for FreeRTOS queue */
  42. } wifi_static_queue_t;
  43. /**
  44. * @brief WiFi log level
  45. *
  46. */
  47. typedef enum {
  48. WIFI_LOG_NONE = 0,
  49. WIFI_LOG_ERROR , /*enabled by default*/
  50. WIFI_LOG_WARNING, /*enabled by default*/
  51. WIFI_LOG_INFO, /*enabled by default*/
  52. WIFI_LOG_DEBUG, /*can be set in menuconfig*/
  53. WIFI_LOG_VERBOSE, /*can be set in menuconfig*/
  54. } wifi_log_level_t;
  55. /**
  56. * @brief WiFi log module definition
  57. *
  58. */
  59. typedef enum {
  60. WIFI_LOG_MODULE_ALL = 0, /*all log modules */
  61. WIFI_LOG_MODULE_WIFI, /*logs related to WiFi*/
  62. WIFI_LOG_MODULE_COEX, /*logs related to WiFi and BT(or BLE) coexist*/
  63. WIFI_LOG_MODULE_MESH, /*logs related to Mesh*/
  64. } wifi_log_module_t;
  65. /**
  66. * @brief WiFi log submodule definition
  67. *
  68. */
  69. #define WIFI_LOG_SUBMODULE_ALL (0) /*all log submodules*/
  70. #define WIFI_LOG_SUBMODULE_INIT (1) /*logs related to initialization*/
  71. #define WIFI_LOG_SUBMODULE_IOCTL (1<<1) /*logs related to API calling*/
  72. #define WIFI_LOG_SUBMODULE_CONN (1<<2) /*logs related to connecting*/
  73. #define WIFI_LOG_SUBMODULE_SCAN (1<<3) /*logs related to scaning*/
  74. /**
  75. * @brief Initialize Wi-Fi Driver
  76. * Alloc resource for WiFi driver, such as WiFi control structure, RX/TX buffer,
  77. * WiFi NVS structure among others.
  78. *
  79. * For the most part, you need not call this function directly. It gets called
  80. * from esp_wifi_init().
  81. *
  82. * This function may be called, if you only need to initialize the Wi-Fi driver
  83. * without having to use the network stack on top.
  84. *
  85. * @param config provide WiFi init configuration
  86. *
  87. * @return
  88. * - ESP_OK: succeed
  89. * - ESP_ERR_NO_MEM: out of memory
  90. * - others: refer to error code esp_err.h
  91. */
  92. esp_err_t esp_wifi_init_internal(const wifi_init_config_t *config);
  93. /**
  94. * @brief Deinitialize Wi-Fi Driver
  95. * Free resource for WiFi driver, such as WiFi control structure, RX/TX buffer,
  96. * WiFi NVS structure among others.
  97. *
  98. * For the most part, you need not call this function directly. It gets called
  99. * from esp_wifi_deinit().
  100. *
  101. * This function may be called, if you call esp_wifi_init_internal to initialize
  102. * WiFi driver.
  103. *
  104. * @return
  105. * - ESP_OK: succeed
  106. * - others: refer to error code esp_err.h
  107. */
  108. esp_err_t esp_wifi_deinit_internal(void);
  109. /**
  110. * @brief free the rx buffer which allocated by wifi driver
  111. *
  112. * @param void* buffer: rx buffer pointer
  113. */
  114. void esp_wifi_internal_free_rx_buffer(void* buffer);
  115. /**
  116. * @brief transmit the buffer via wifi driver
  117. *
  118. * This API makes a copy of the input buffer and then forwards the buffer
  119. * copy to WiFi driver.
  120. *
  121. * @param wifi_interface_t wifi_if : wifi interface id
  122. * @param void *buffer : the buffer to be tansmit
  123. * @param uint16_t len : the length of buffer
  124. *
  125. * @return
  126. * - ESP_OK : Successfully transmit the buffer to wifi driver
  127. * - ESP_ERR_NO_MEM: out of memory
  128. * - ESP_ERR_WIFI_ARG: invalid argument
  129. * - ESP_ERR_WIFI_IF : WiFi interface is invalid
  130. * - ESP_ERR_WIFI_CONN : WiFi interface is not created, e.g. send the data to STA while WiFi mode is AP mode
  131. * - ESP_ERR_WIFI_NOT_STARTED : WiFi is not started
  132. * - ESP_ERR_WIFI_STATE : WiFi internal state is not ready, e.g. WiFi is not started
  133. * - ESP_ERR_WIFI_NOT_ASSOC : WiFi is not associated
  134. * - ESP_ERR_WIFI_TX_DISALLOW : WiFi TX is disallowed, e.g. WiFi hasn't pass the authentication
  135. * - ESP_ERR_WIFI_POST : caller fails to post event to WiFi task
  136. */
  137. int esp_wifi_internal_tx(wifi_interface_t wifi_if, void *buffer, uint16_t len);
  138. /**
  139. * @brief The net stack buffer reference counter callback function
  140. *
  141. */
  142. typedef void (*wifi_netstack_buf_ref_cb_t)(void *netstack_buf);
  143. /**
  144. * @brief The net stack buffer free callback function
  145. *
  146. */
  147. typedef void (*wifi_netstack_buf_free_cb_t)(void *netstack_buf);
  148. /**
  149. * @brief transmit the buffer by reference via wifi driver
  150. *
  151. * This API firstly increases the reference counter of the input buffer and
  152. * then forwards the buffer to WiFi driver. The WiFi driver will free the buffer
  153. * after processing it. Use esp_wifi_internal_tx() if the uplayer buffer doesn't
  154. * supports reference counter.
  155. *
  156. * @param wifi_if : wifi interface id
  157. * @param buffer : the buffer to be tansmit
  158. * @param len : the length of buffer
  159. * @param netstack_buf : the netstack buffer related to bufffer
  160. *
  161. * @return
  162. * - ESP_OK : Successfully transmit the buffer to wifi driver
  163. * - ESP_ERR_NO_MEM: out of memory
  164. * - ESP_ERR_WIFI_ARG: invalid argument
  165. * - ESP_ERR_WIFI_IF : WiFi interface is invalid
  166. * - ESP_ERR_WIFI_CONN : WiFi interface is not created, e.g. send the data to STA while WiFi mode is AP mode
  167. * - ESP_ERR_WIFI_NOT_STARTED : WiFi is not started
  168. * - ESP_ERR_WIFI_STATE : WiFi internal state is not ready, e.g. WiFi is not started
  169. * - ESP_ERR_WIFI_NOT_ASSOC : WiFi is not associated
  170. * - ESP_ERR_WIFI_TX_DISALLOW : WiFi TX is disallowed, e.g. WiFi hasn't pass the authentication
  171. * - ESP_ERR_WIFI_POST : caller fails to post event to WiFi task
  172. */
  173. esp_err_t esp_wifi_internal_tx_by_ref(wifi_interface_t ifx, void *buffer, size_t len, void *netstack_buf);
  174. /**
  175. * @brief register the net stack buffer reference increasing and free callback
  176. *
  177. * @param ref : net stack buffer reference callback
  178. * @param free: net stack buffer free callback
  179. *
  180. * @return
  181. * - ESP_OK : Successfully transmit the buffer to wifi driver
  182. * - others : failed to register the callback
  183. */
  184. esp_err_t esp_wifi_internal_reg_netstack_buf_cb(wifi_netstack_buf_ref_cb_t ref, wifi_netstack_buf_free_cb_t free);
  185. /**
  186. * @brief The WiFi RX callback function
  187. *
  188. * Each time the WiFi need to forward the packets to high layer, the callback function will be called
  189. */
  190. typedef esp_err_t (*wifi_rxcb_t)(void *buffer, uint16_t len, void *eb);
  191. /**
  192. * @brief Set the WiFi RX callback
  193. *
  194. * @attention 1. Currently we support only one RX callback for each interface
  195. *
  196. * @param wifi_interface_t ifx : interface
  197. * @param wifi_rxcb_t fn : WiFi RX callback
  198. *
  199. * @return
  200. * - ESP_OK : succeed
  201. * - others : fail
  202. */
  203. esp_err_t esp_wifi_internal_reg_rxcb(wifi_interface_t ifx, wifi_rxcb_t fn);
  204. /**
  205. * @brief Notify WIFI driver that the station got ip successfully
  206. *
  207. * @return
  208. * - ESP_OK : succeed
  209. * - others : fail
  210. */
  211. esp_err_t esp_wifi_internal_set_sta_ip(void);
  212. /**
  213. * @brief enable or disable transmitting WiFi MAC frame with fixed rate
  214. *
  215. * @attention 1. If fixed rate is enabled, both management and data frame are transmitted with fixed rate
  216. * @attention 2. Make sure that the receiver is able to receive the frame with the fixed rate if you want the frame to be received
  217. *
  218. * @param ifx : wifi interface
  219. * @param en : false - disable, true - enable
  220. * @param rate : PHY rate
  221. *
  222. * @return
  223. * - ERR_OK : succeed
  224. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  225. * - ESP_ERR_WIFI_NOT_STARTED: WiFi was not started by esp_wifi_start
  226. * - ESP_ERR_WIFI_IF : invalid WiFi interface
  227. * - ESP_ERR_INVALID_ARG : invalid rate
  228. * - ESP_ERR_NOT_SUPPORTED : do not support to set fixed rate if TX AMPDU is enabled
  229. */
  230. esp_err_t esp_wifi_internal_set_fix_rate(wifi_interface_t ifx, bool en, wifi_phy_rate_t rate);
  231. /**
  232. * @brief Start SmartConfig, config ESP device to connect AP. You need to broadcast information by phone APP.
  233. * Device sniffer special packets from the air that containing SSID and password of target AP.
  234. *
  235. * @attention 1. This API can be called in station or softAP-station mode.
  236. * @attention 2. Can not call esp_smartconfig_start twice before it finish, please call
  237. * esp_smartconfig_stop first.
  238. *
  239. * @param config pointer to smartconfig start configure structure
  240. *
  241. * @return
  242. * - ESP_OK: succeed
  243. * - others: fail
  244. */
  245. esp_err_t esp_smartconfig_internal_start(const smartconfig_start_config_t *config);
  246. /**
  247. * @brief Stop SmartConfig, free the buffer taken by esp_smartconfig_start.
  248. *
  249. * @attention Whether connect to AP succeed or not, this API should be called to free
  250. * memory taken by smartconfig_start.
  251. *
  252. * @return
  253. * - ESP_OK: succeed
  254. * - others: fail
  255. */
  256. esp_err_t esp_smartconfig_internal_stop(void);
  257. /**
  258. * @brief Check the MD5 values of the OS adapter header files in IDF and WiFi library
  259. *
  260. * @attention 1. It is used for internal CI version check
  261. *
  262. * @return
  263. * - ESP_OK : succeed
  264. * - ESP_WIFI_INVALID_ARG : MD5 check fail
  265. */
  266. esp_err_t esp_wifi_internal_osi_funcs_md5_check(const char *md5);
  267. /**
  268. * @brief Check the MD5 values of the crypto types header files in IDF and WiFi library
  269. *
  270. * @attention 1. It is used for internal CI version check
  271. *
  272. * @return
  273. * - ESP_OK : succeed
  274. * - ESP_WIFI_INVALID_ARG : MD5 check fail
  275. */
  276. esp_err_t esp_wifi_internal_crypto_funcs_md5_check(const char *md5);
  277. /**
  278. * @brief Check the MD5 values of the esp_wifi_types.h in IDF and WiFi library
  279. *
  280. * @attention 1. It is used for internal CI version check
  281. *
  282. * @return
  283. * - ESP_OK : succeed
  284. * - ESP_WIFI_INVALID_ARG : MD5 check fail
  285. */
  286. esp_err_t esp_wifi_internal_wifi_type_md5_check(const char *md5);
  287. /**
  288. * @brief Check the MD5 values of the esp_wifi.h in IDF and WiFi library
  289. *
  290. * @attention 1. It is used for internal CI version check
  291. *
  292. * @return
  293. * - ESP_OK : succeed
  294. * - ESP_WIFI_INVALID_ARG : MD5 check fail
  295. */
  296. esp_err_t esp_wifi_internal_esp_wifi_md5_check(const char *md5);
  297. /**
  298. * @brief Allocate a chunk of memory for WiFi driver
  299. *
  300. * @attention This API is not used for DMA memory allocation.
  301. *
  302. * @param size_t size : Size, in bytes, of the amount of memory to allocate
  303. *
  304. * @return A pointer to the memory allocated on success, NULL on failure
  305. */
  306. void *wifi_malloc( size_t size );
  307. /**
  308. * @brief Reallocate a chunk of memory for WiFi driver
  309. *
  310. * @attention This API is not used for DMA memory allocation.
  311. *
  312. * @param void * ptr : Pointer to previously allocated memory, or NULL for a new allocation.
  313. * @param size_t size : Size, in bytes, of the amount of memory to allocate
  314. *
  315. * @return A pointer to the memory allocated on success, NULL on failure
  316. */
  317. void *wifi_realloc( void *ptr, size_t size );
  318. /**
  319. * @brief Callocate memory for WiFi driver
  320. *
  321. * @attention This API is not used for DMA memory allocation.
  322. *
  323. * @param size_t n : Number of continuing chunks of memory to allocate
  324. * @param size_t size : Size, in bytes, of the amount of memory to allocate
  325. *
  326. * @return A pointer to the memory allocated on success, NULL on failure
  327. */
  328. void *wifi_calloc( size_t n, size_t size );
  329. /**
  330. * @brief Update WiFi MAC time
  331. *
  332. * @param uint32_t time_delta : time duration since the WiFi/BT common clock is disabled
  333. *
  334. * @return Always returns ESP_OK
  335. */
  336. typedef esp_err_t (* wifi_mac_time_update_cb_t)( uint32_t time_delta );
  337. /**
  338. * @brief Update WiFi MAC time
  339. *
  340. * @param uint32_t time_delta : time duration since the WiFi/BT common clock is disabled
  341. *
  342. * @return Always returns ESP_OK
  343. */
  344. esp_err_t esp_wifi_internal_update_mac_time( uint32_t time_delta );
  345. /**
  346. * @brief Set current WiFi log level
  347. *
  348. * @param level Log level.
  349. *
  350. * @return
  351. * - ESP_OK: succeed
  352. * - ESP_FAIL: level is invalid
  353. */
  354. esp_err_t esp_wifi_internal_set_log_level(wifi_log_level_t level);
  355. /**
  356. * @brief Set current log module and submodule
  357. *
  358. * @param module Log module
  359. * @param submodule Log submodule
  360. * @param enable enable or disable
  361. * If module == 0 && enable == 0, all log modules are disabled.
  362. * If module == 0 && enable == 1, all log modules are enabled.
  363. * If submodule == 0 && enable == 0, all log submodules are disabled.
  364. * If submodule == 0 && enable == 1, all log submodules are enabled.
  365. *
  366. * @return
  367. * - ESP_OK: succeed
  368. * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  369. * - ESP_ERR_WIFI_ARG: invalid argument
  370. */
  371. esp_err_t esp_wifi_internal_set_log_mod(wifi_log_module_t module, uint32_t submodule, bool enable);
  372. /**
  373. * @brief Get current WiFi log info
  374. *
  375. * @param log_level the return log level.
  376. * @param log_mod the return log module and submodule
  377. *
  378. * @return
  379. * - ESP_OK: succeed
  380. */
  381. esp_err_t esp_wifi_internal_get_log(wifi_log_level_t *log_level, uint32_t *log_mod);
  382. /**
  383. * @brief A general API to set/get WiFi internal configuration, it's for debug only
  384. *
  385. * @param cmd : ioctl command type
  386. * @param cfg : configuration for the command
  387. *
  388. * @return
  389. * - ESP_OK: succeed
  390. * - others: failed
  391. */
  392. esp_err_t esp_wifi_internal_ioctl(int cmd, wifi_ioctl_config_t *cfg);
  393. /**
  394. * @brief Get the user-configured channel info
  395. *
  396. * @param ifx : WiFi interface
  397. * @param primary : store the configured primary channel
  398. * @param second : store the configured second channel
  399. *
  400. * @return
  401. * - ESP_OK: succeed
  402. */
  403. esp_err_t esp_wifi_internal_get_config_channel(wifi_interface_t ifx, uint8_t *primary, uint8_t *second);
  404. /**
  405. * @brief Get the negotiated channel info after WiFi connection established
  406. *
  407. * @param ifx : WiFi interface
  408. * @param aid : the connection number when a STA connects to the softAP
  409. * @param primary : store the negotiated primary channel
  410. * @param second : store the negotiated second channel
  411. * @attention the aid param is only works when the ESP32 in softAP/softAP+STA mode
  412. *
  413. * @return
  414. * - ESP_OK: succeed
  415. */
  416. esp_err_t esp_wifi_internal_get_negotiated_channel(wifi_interface_t ifx, uint8_t aid, uint8_t *primary, uint8_t *second);
  417. /**
  418. * @brief Get the negotiated bandwidth info after WiFi connection established
  419. *
  420. * @param ifx : WiFi interface
  421. * @param bw : store the negotiated bandwidth
  422. *
  423. * @return
  424. * - ESP_OK: succeed
  425. */
  426. esp_err_t esp_wifi_internal_get_negotiated_bandwidth(wifi_interface_t ifx, uint8_t aid, uint8_t *bw);
  427. #if CONFIG_IDF_TARGET_ESP32S2
  428. /**
  429. * @brief Check if WiFi TSF is active
  430. *
  431. * @return
  432. * - true: Active
  433. * - false: Not active
  434. */
  435. bool esp_wifi_internal_is_tsf_active(void);
  436. #endif
  437. #ifdef __cplusplus
  438. }
  439. #endif
  440. #endif /* __ESP_WIFI_H__ */