esp_event.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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_EVENT_H__
  14. #define __ESP_EVENT_H__
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include "esp_err.h"
  18. #include "esp_wifi_types.h"
  19. #include "tcpip_adapter.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. typedef enum {
  24. SYSTEM_EVENT_WIFI_READY = 0, /**< ESP32 WiFi ready */
  25. SYSTEM_EVENT_SCAN_DONE, /**< ESP32 finish scanning AP */
  26. SYSTEM_EVENT_STA_START, /**< ESP32 station start */
  27. SYSTEM_EVENT_STA_STOP, /**< ESP32 station stop */
  28. SYSTEM_EVENT_STA_CONNECTED, /**< ESP32 station connected to AP */
  29. SYSTEM_EVENT_STA_DISCONNECTED, /**< ESP32 station disconnected from AP */
  30. SYSTEM_EVENT_STA_AUTHMODE_CHANGE, /**< the auth mode of AP connected by ESP32 station changed */
  31. SYSTEM_EVENT_STA_GOT_IP, /**< ESP32 station got IP from connected AP */
  32. SYSTEM_EVENT_STA_LOST_IP, /**< ESP32 station lost IP and the IP is reset to 0 */
  33. SYSTEM_EVENT_STA_WPS_ER_SUCCESS, /**< ESP32 station wps succeeds in enrollee mode */
  34. SYSTEM_EVENT_STA_WPS_ER_FAILED, /**< ESP32 station wps fails in enrollee mode */
  35. SYSTEM_EVENT_STA_WPS_ER_TIMEOUT, /**< ESP32 station wps timeout in enrollee mode */
  36. SYSTEM_EVENT_STA_WPS_ER_PIN, /**< ESP32 station wps pin code in enrollee mode */
  37. SYSTEM_EVENT_AP_START, /**< ESP32 soft-AP start */
  38. SYSTEM_EVENT_AP_STOP, /**< ESP32 soft-AP stop */
  39. SYSTEM_EVENT_AP_STACONNECTED, /**< a station connected to ESP32 soft-AP */
  40. SYSTEM_EVENT_AP_STADISCONNECTED, /**< a station disconnected from ESP32 soft-AP */
  41. SYSTEM_EVENT_AP_PROBEREQRECVED, /**< Receive probe request packet in soft-AP interface */
  42. SYSTEM_EVENT_GOT_IP6, /**< ESP32 station or ap or ethernet interface v6IP addr is preferred */
  43. SYSTEM_EVENT_ETH_START, /**< ESP32 ethernet start */
  44. SYSTEM_EVENT_ETH_STOP, /**< ESP32 ethernet stop */
  45. SYSTEM_EVENT_ETH_CONNECTED, /**< ESP32 ethernet phy link up */
  46. SYSTEM_EVENT_ETH_DISCONNECTED, /**< ESP32 ethernet phy link down */
  47. SYSTEM_EVENT_ETH_GOT_IP, /**< ESP32 ethernet got IP from connected AP */
  48. SYSTEM_EVENT_MAX
  49. } system_event_id_t;
  50. /* add this macro define for compatible with old IDF version */
  51. #ifndef SYSTEM_EVENT_AP_STA_GOT_IP6
  52. #define SYSTEM_EVENT_AP_STA_GOT_IP6 SYSTEM_EVENT_GOT_IP6
  53. #endif
  54. typedef enum {
  55. WPS_FAIL_REASON_NORMAL = 0, /**< ESP32 WPS normal fail reason */
  56. WPS_FAIL_REASON_RECV_M2D, /**< ESP32 WPS receive M2D frame */
  57. WPS_FAIL_REASON_MAX
  58. }system_event_sta_wps_fail_reason_t;
  59. typedef struct {
  60. uint32_t status; /**< status of scanning APs */
  61. uint8_t number;
  62. uint8_t scan_id;
  63. } system_event_sta_scan_done_t;
  64. typedef struct {
  65. uint8_t ssid[32]; /**< SSID of connected AP */
  66. uint8_t ssid_len; /**< SSID length of connected AP */
  67. uint8_t bssid[6]; /**< BSSID of connected AP*/
  68. uint8_t channel; /**< channel of connected AP*/
  69. wifi_auth_mode_t authmode;
  70. } system_event_sta_connected_t;
  71. typedef struct {
  72. uint8_t ssid[32]; /**< SSID of disconnected AP */
  73. uint8_t ssid_len; /**< SSID length of disconnected AP */
  74. uint8_t bssid[6]; /**< BSSID of disconnected AP */
  75. uint8_t reason; /**< reason of disconnection */
  76. } system_event_sta_disconnected_t;
  77. typedef struct {
  78. wifi_auth_mode_t old_mode; /**< the old auth mode of AP */
  79. wifi_auth_mode_t new_mode; /**< the new auth mode of AP */
  80. } system_event_sta_authmode_change_t;
  81. typedef struct {
  82. tcpip_adapter_ip_info_t ip_info;
  83. bool ip_changed;
  84. } system_event_sta_got_ip_t;
  85. typedef struct {
  86. uint8_t pin_code[8]; /**< PIN code of station in enrollee mode */
  87. } system_event_sta_wps_er_pin_t;
  88. typedef struct {
  89. tcpip_adapter_if_t if_index;
  90. tcpip_adapter_ip6_info_t ip6_info;
  91. } system_event_got_ip6_t;
  92. typedef struct {
  93. uint8_t mac[6]; /**< MAC address of the station connected to ESP32 soft-AP */
  94. uint8_t aid; /**< the aid that ESP32 soft-AP gives to the station connected to */
  95. } system_event_ap_staconnected_t;
  96. typedef struct {
  97. uint8_t mac[6]; /**< MAC address of the station disconnects to ESP32 soft-AP */
  98. uint8_t aid; /**< the aid that ESP32 soft-AP gave to the station disconnects to */
  99. } system_event_ap_stadisconnected_t;
  100. typedef struct {
  101. int rssi; /**< Received probe request signal strength */
  102. uint8_t mac[6]; /**< MAC address of the station which send probe request */
  103. } system_event_ap_probe_req_rx_t;
  104. typedef union {
  105. system_event_sta_connected_t connected; /**< ESP32 station connected to AP */
  106. system_event_sta_disconnected_t disconnected; /**< ESP32 station disconnected to AP */
  107. system_event_sta_scan_done_t scan_done; /**< ESP32 station scan (APs) done */
  108. system_event_sta_authmode_change_t auth_change; /**< the auth mode of AP ESP32 station connected to changed */
  109. system_event_sta_got_ip_t got_ip; /**< ESP32 station got IP, first time got IP or when IP is changed */
  110. system_event_sta_wps_er_pin_t sta_er_pin; /**< ESP32 station WPS enrollee mode PIN code received */
  111. system_event_sta_wps_fail_reason_t sta_er_fail_reason;/**< ESP32 station WPS enrollee mode failed reason code received */
  112. system_event_ap_staconnected_t sta_connected; /**< a station connected to ESP32 soft-AP */
  113. system_event_ap_stadisconnected_t sta_disconnected; /**< a station disconnected to ESP32 soft-AP */
  114. system_event_ap_probe_req_rx_t ap_probereqrecved; /**< ESP32 soft-AP receive probe request packet */
  115. system_event_got_ip6_t got_ip6; /**< ESP32 station or ap or ethernet ipv6 addr state change to preferred */
  116. } system_event_info_t;
  117. typedef struct {
  118. system_event_id_t event_id; /**< event ID */
  119. system_event_info_t event_info; /**< event information */
  120. } system_event_t;
  121. typedef esp_err_t (*system_event_handler_t)(system_event_t *event);
  122. /**
  123. * @brief Send a event to event task
  124. *
  125. * @attention 1. Other task/modules, such as the TCPIP module, can call this API to send an event to event task
  126. *
  127. * @param system_event_t * event : event
  128. *
  129. * @return ESP_OK : succeed
  130. * @return others : fail
  131. */
  132. esp_err_t esp_event_send(system_event_t *event);
  133. /**
  134. * @brief Default event handler for system events
  135. *
  136. * This function performs default handling of system events.
  137. * When using esp_event_loop APIs, it is called automatically before invoking the user-provided
  138. * callback function.
  139. *
  140. * Applications which implement a custom event loop must call this function
  141. * as part of event processing.
  142. *
  143. * @param event pointer to event to be handled
  144. * @return ESP_OK if an event was handled successfully
  145. */
  146. esp_err_t esp_event_process_default(system_event_t *event);
  147. /**
  148. * @brief Install default event handlers for Ethernet interface
  149. *
  150. */
  151. void esp_event_set_default_eth_handlers();
  152. /**
  153. * @brief Install default event handlers for Wi-Fi interfaces (station and AP)
  154. *
  155. */
  156. void esp_event_set_default_wifi_handlers();
  157. #ifdef __cplusplus
  158. }
  159. #endif
  160. #endif /* __ESP_EVENT_H__ */