1 // Copyright 2015-2018 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 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <stdint.h>
18 #include <stdbool.h>
19 
20 #include "esp_err.h"
21 #include "esp_wifi_types.h"
22 #include "esp_netif.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 /** System event types enumeration */
29 typedef enum {
30     SYSTEM_EVENT_WIFI_READY = 0,           /*!< ESP32 WiFi ready */
31     SYSTEM_EVENT_SCAN_DONE,                /*!< ESP32 finish scanning AP */
32     SYSTEM_EVENT_STA_START,                /*!< ESP32 station start */
33     SYSTEM_EVENT_STA_STOP,                 /*!< ESP32 station stop */
34     SYSTEM_EVENT_STA_CONNECTED,            /*!< ESP32 station connected to AP */
35     SYSTEM_EVENT_STA_DISCONNECTED,         /*!< ESP32 station disconnected from AP */
36     SYSTEM_EVENT_STA_AUTHMODE_CHANGE,      /*!< the auth mode of AP connected by ESP32 station changed */
37     SYSTEM_EVENT_STA_GOT_IP,               /*!< ESP32 station got IP from connected AP */
38     SYSTEM_EVENT_STA_LOST_IP,              /*!< ESP32 station lost IP and the IP is reset to 0 */
39     SYSTEM_EVENT_STA_BSS_RSSI_LOW,         /*!< ESP32 station connected BSS rssi goes below threshold */
40     SYSTEM_EVENT_STA_WPS_ER_SUCCESS,       /*!< ESP32 station wps succeeds in enrollee mode */
41     SYSTEM_EVENT_STA_WPS_ER_FAILED,        /*!< ESP32 station wps fails in enrollee mode */
42     SYSTEM_EVENT_STA_WPS_ER_TIMEOUT,       /*!< ESP32 station wps timeout in enrollee mode */
43     SYSTEM_EVENT_STA_WPS_ER_PIN,           /*!< ESP32 station wps pin code in enrollee mode */
44     SYSTEM_EVENT_STA_WPS_ER_PBC_OVERLAP,   /*!< ESP32 station wps overlap in enrollee mode */
45     SYSTEM_EVENT_AP_START,                 /*!< ESP32 soft-AP start */
46     SYSTEM_EVENT_AP_STOP,                  /*!< ESP32 soft-AP stop */
47     SYSTEM_EVENT_AP_STACONNECTED,          /*!< a station connected to ESP32 soft-AP */
48     SYSTEM_EVENT_AP_STADISCONNECTED,       /*!< a station disconnected from ESP32 soft-AP */
49     SYSTEM_EVENT_AP_STAIPASSIGNED,         /*!< ESP32 soft-AP assign an IP to a connected station */
50     SYSTEM_EVENT_AP_PROBEREQRECVED,        /*!< Receive probe request packet in soft-AP interface */
51     SYSTEM_EVENT_ACTION_TX_STATUS,         /*!< Receive status of Action frame transmitted */
52     SYSTEM_EVENT_ROC_DONE,                 /*!< Indicates the completion of Remain-on-Channel operation status */
53     SYSTEM_EVENT_STA_BEACON_TIMEOUT,       /*!< ESP32 station beacon timeout */
54     SYSTEM_EVENT_FTM_REPORT,               /*!< Receive report of FTM procedure */
55     SYSTEM_EVENT_GOT_IP6,                  /*!< ESP32 station or ap or ethernet interface v6IP addr is preferred */
56     SYSTEM_EVENT_ETH_START,                /*!< ESP32 ethernet start */
57     SYSTEM_EVENT_ETH_STOP,                 /*!< ESP32 ethernet stop */
58     SYSTEM_EVENT_ETH_CONNECTED,            /*!< ESP32 ethernet phy link up */
59     SYSTEM_EVENT_ETH_DISCONNECTED,         /*!< ESP32 ethernet phy link down */
60     SYSTEM_EVENT_ETH_GOT_IP,               /*!< ESP32 ethernet got IP from connected AP */
61     SYSTEM_EVENT_ETH_LOST_IP,              /*!< ESP32 ethernet lost IP and the IP is reset to 0 */
62     SYSTEM_EVENT_MAX                       /*!< Number of members in this enum */
63 } system_event_id_t;
64 
65 typedef uint32_t TickType_t;
66 
67 /* add this macro define for compatible with old IDF version */
68 #ifndef SYSTEM_EVENT_AP_STA_GOT_IP6
69 #define SYSTEM_EVENT_AP_STA_GOT_IP6 SYSTEM_EVENT_GOT_IP6
70 #endif
71 
72 
73 /** Argument structure of SYSTEM_EVENT_STA_WPS_ER_FAILED event */
74 typedef wifi_event_sta_wps_fail_reason_t system_event_sta_wps_fail_reason_t;
75 
76 /** Argument structure of SYSTEM_EVENT_SCAN_DONE event */
77 typedef wifi_event_sta_scan_done_t system_event_sta_scan_done_t;
78 
79 /** Argument structure of SYSTEM_EVENT_STA_CONNECTED event */
80 typedef wifi_event_sta_connected_t system_event_sta_connected_t;
81 
82 /** Argument structure of SYSTEM_EVENT_STA_DISCONNECTED event */
83 typedef wifi_event_sta_disconnected_t system_event_sta_disconnected_t;
84 
85 /** Argument structure of SYSTEM_EVENT_STA_AUTHMODE_CHANGE event */
86 typedef wifi_event_sta_authmode_change_t system_event_sta_authmode_change_t;
87 
88 /** Argument structure of SYSTEM_EVENT_STA_WPS_ER_PIN event */
89 typedef wifi_event_sta_wps_er_pin_t system_event_sta_wps_er_pin_t;
90 
91 /** Argument structure of SYSTEM_EVENT_STA_WPS_ER_PIN event */
92 typedef wifi_event_sta_wps_er_success_t system_event_sta_wps_er_success_t;
93 
94 /** Argument structure of  event */
95 typedef wifi_event_ap_staconnected_t system_event_ap_staconnected_t;
96 
97 /** Argument structure of  event */
98 typedef wifi_event_ap_stadisconnected_t system_event_ap_stadisconnected_t;
99 
100 /** Argument structure of  event */
101 typedef wifi_event_ap_probe_req_rx_t system_event_ap_probe_req_rx_t;
102 
103 /** Argument structure of SYSTEM_EVENT_FTM_REPORT event */
104 typedef wifi_event_ftm_report_t system_event_ftm_report_t;
105 
106 /** Argument structure of  event */
107 typedef ip_event_ap_staipassigned_t system_event_ap_staipassigned_t;
108 
109 /** Argument structure of  event */
110 typedef ip_event_got_ip_t system_event_sta_got_ip_t;
111 
112 /** Argument structure of  event */
113 typedef ip_event_got_ip6_t system_event_got_ip6_t;
114 
115 /** Union of all possible system_event argument structures */
116 typedef union {
117     system_event_sta_connected_t               connected;          /*!< ESP32 station connected to AP */
118     system_event_sta_disconnected_t            disconnected;       /*!< ESP32 station disconnected to AP */
119     system_event_sta_scan_done_t               scan_done;          /*!< ESP32 station scan (APs) done */
120     system_event_sta_authmode_change_t         auth_change;        /*!< the auth mode of AP ESP32 station connected to changed */
121     system_event_sta_got_ip_t                  got_ip;             /*!< ESP32 station got IP, first time got IP or when IP is changed */
122     system_event_sta_wps_er_pin_t              sta_er_pin;         /*!< ESP32 station WPS enrollee mode PIN code received */
123     system_event_sta_wps_fail_reason_t         sta_er_fail_reason; /*!< ESP32 station WPS enrollee mode failed reason code received */
124     system_event_sta_wps_er_success_t          sta_er_success;     /*!< ESP32 station WPS enrollee success */
125     system_event_ap_staconnected_t             sta_connected;      /*!< a station connected to ESP32 soft-AP */
126     system_event_ap_stadisconnected_t          sta_disconnected;   /*!< a station disconnected to ESP32 soft-AP */
127     system_event_ap_probe_req_rx_t             ap_probereqrecved;  /*!< ESP32 soft-AP receive probe request packet */
128     system_event_ftm_report_t                  ftm_report;         /*!< Report of FTM procedure */
129     system_event_ap_staipassigned_t            ap_staipassigned;   /**< ESP32 soft-AP assign an IP to the station*/
130     system_event_got_ip6_t                     got_ip6;            /*!< ESP32 station or ap or ethernet ipv6 addr state change to preferred */
131 } system_event_info_t;
132 
133 /** Event, as a tagged enum */
134 typedef struct {
135     system_event_id_t     event_id;      /*!< event ID */
136     system_event_info_t   event_info;    /*!< event information */
137 } system_event_t;
138 
139 /** Event handler function type */
140 typedef esp_err_t (*system_event_handler_t)(esp_event_base_t event_base,
141                                             int32_t event_id,
142                                             void* event_data,
143                                             size_t event_data_size,
144                                             TickType_t ticks_to_wait);
145 
146 /**
147   * @brief  Send a event to event task
148   *
149   * @note This API is part of the legacy event system. New code should use event library API in esp_event.h
150   *
151   * Other task/modules, such as the tcpip_adapter, can call this API to send an event to event task
152   *
153   * @param event Event to send
154   *
155   * @return ESP_OK : succeed
156   * @return others : fail
157   */
158 esp_err_t esp_event_send(system_event_t *event) __attribute__ ((deprecated));
159 
160 /**
161   * @brief  Send a event to event task
162   *
163   * @note This API is used by WiFi Driver only.
164   *
165   * Other task/modules, such as the tcpip_adapter, can call this API to send an event to event task
166   *
167   * @param[in] event_base the event base that identifies the event
168   * @param[in] event_id the event id that identifies the event
169   * @param[in] event_data the data, specific to the event occurence, that gets passed to the handler
170   * @param[in] event_data_size the size of the event data
171   * @param[in] ticks_to_wait number of ticks to block on a full event queue
172   *
173   * @return ESP_OK : succeed
174   * @return others : fail
175   */
176 esp_err_t esp_event_send_internal(esp_event_base_t event_base,
177                             int32_t event_id,
178                             void* event_data,
179                             size_t event_data_size,
180                             TickType_t ticks_to_wait);
181 
182 /**
183  * @brief  Default event handler for system events
184  *
185  * @note This API is part of the legacy event system. New code should use event library API in esp_event.h
186  *
187  * This function performs default handling of system events.
188  * When using esp_event_loop APIs, it is called automatically before invoking the user-provided
189  * callback function.
190  *
191  * Applications which implement a custom event loop must call this function
192  * as part of event processing.
193  *
194  * @param  event   pointer to event to be handled
195  * @return ESP_OK if an event was handled successfully
196  */
197 esp_err_t esp_event_process_default(system_event_t *event) __attribute__ ((deprecated));
198 
199 /**
200   * @brief  Install default event handlers for Ethernet interface
201   *
202   * @note This API is part of the legacy event system. New code should use event library API in esp_event.h
203   *
204   */
205 void esp_event_set_default_eth_handlers(void);
206 
207 /**
208   * @brief  Install default event handlers for Wi-Fi interfaces (station and AP)
209   *
210   * @note This API is part of the legacy event system. New code should use event library API in esp_event.h
211   */
212 void esp_event_set_default_wifi_handlers(void) __attribute__ ((deprecated));
213 
214 /**
215  * @brief  Application specified event callback function
216  *
217  * @note This API is part of the legacy event system. New code should use event library API in esp_event.h
218  *
219  *
220  * @param  ctx    reserved for user
221  * @param  event  event type defined in this file
222  *
223  * @return
224  *    - ESP_OK: succeed
225  *    - others: fail
226  */
227 typedef esp_err_t (*system_event_cb_t)(void *ctx, system_event_t *event);
228 
229 /**
230  * @brief  Initialize event loop
231  *
232  * @note This API is part of the legacy event system. New code should use event library API in esp_event.h
233  *
234  * Create the event handler and task
235  *
236  * @param  cb   application specified event callback, it can be modified by call esp_event_set_cb
237  * @param  ctx  reserved for user
238  *
239  * @return
240  *    - ESP_OK: succeed
241  *    - others: fail
242  */
243 esp_err_t esp_event_loop_init(system_event_cb_t cb, void *ctx) __attribute__ ((deprecated));
244 
245 /**
246  * @brief  Set application specified event callback function
247  *
248  * @note This API is part of the legacy event system. New code should use event library API in esp_event.h
249  *
250  * @attention 1. If cb is NULL, means application don't need to handle
251  *               If cb is not NULL, it will be call when an event is received, after the default event callback is completed
252  *
253  * @param  cb   application callback function
254  * @param  ctx  argument to be passed to callback
255  *
256  *
257  * @return old callback
258  */
259 system_event_cb_t esp_event_loop_set_cb(system_event_cb_t cb, void *ctx) __attribute__ ((deprecated));
260 
261 #ifdef __cplusplus
262 }
263 #endif
264