1// Copyright 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#include "esp_event.h" 16#include "esp_log.h" 17#include "esp_event_legacy.h" 18#include "esp_wifi_types.h" 19#include "esp_netif.h" 20#if CONFIG_ETH_ENABLED 21#include "esp_eth.h" 22#endif 23 24/** 25 * The purpose of this file is to provide an "esp_event_send_to_default_loop" 26 * function, which is used to forward legacy events (system_event_t) sent using 27 * esp_event_send, to the new default event loop (esp_event_post). 28 * 29 * For each of the events in system_event_id_t, we extract the event data from 30 * the corresponding system_event_info_t member, and forward that to 31 * esp_event_post function. 32 * 33 * Some macros are used to reduce the amount of boilerplate. 34 * 35 * Note that this function only needs to be included into the output file if 36 * the new default event loop is used. This function is in a separate file for 37 * readability reasons. In order to be linked if the contents of 38 * default_event_loop.c is linked, this file is #include-ed into default_event_loop.c. 39 */ 40 41#if LOG_LOCAL_LEVEL >= 4 /* ESP_LOG_DEBUG */ 42#define WITH_EVENT_DEBUG 43#endif 44 45#ifdef WITH_EVENT_DEBUG 46static void esp_system_event_debug(const system_event_t* event); 47#endif 48 49#define HANDLE_SYS_EVENT(base_, name_) \ 50 case SYSTEM_EVENT_ ## name_: \ 51 return esp_event_post(base_ ## _EVENT, base_ ## _EVENT_ ## name_, \ 52 NULL, 0, send_timeout) 53 54#define HANDLE_SYS_EVENT_ARG(base_, name_, member_) \ 55 case SYSTEM_EVENT_ ## name_: \ 56 return esp_event_post(base_ ## _EVENT, base_ ## _EVENT_ ## name_, \ 57 &event->event_info.member_, sizeof(event->event_info.member_), \ 58 send_timeout) 59 60esp_err_t esp_event_send_to_default_loop(system_event_t *event) 61{ 62#ifdef WITH_EVENT_DEBUG 63 esp_system_event_debug(event); 64#endif // WITH_EVENT_DEBUG 65 66 const TickType_t send_timeout = 0; 67 switch (event->event_id) { 68 /* Wi-Fi common events */ 69 HANDLE_SYS_EVENT(WIFI, WIFI_READY); 70 HANDLE_SYS_EVENT_ARG(WIFI, SCAN_DONE, scan_done); 71 HANDLE_SYS_EVENT(WIFI, STA_START); 72 HANDLE_SYS_EVENT(WIFI, STA_STOP); 73 HANDLE_SYS_EVENT_ARG(WIFI, FTM_REPORT, ftm_report); 74 75 /* STA events */ 76 HANDLE_SYS_EVENT_ARG(WIFI, STA_CONNECTED, connected); 77 HANDLE_SYS_EVENT_ARG(WIFI, STA_DISCONNECTED, disconnected); 78 HANDLE_SYS_EVENT_ARG(WIFI, STA_AUTHMODE_CHANGE, auth_change); 79 80 /* WPS events */ 81 HANDLE_SYS_EVENT(WIFI, STA_WPS_ER_SUCCESS); 82 HANDLE_SYS_EVENT(WIFI, STA_WPS_ER_TIMEOUT); 83 HANDLE_SYS_EVENT_ARG(WIFI, STA_WPS_ER_FAILED, sta_er_fail_reason); 84 HANDLE_SYS_EVENT_ARG(WIFI, STA_WPS_ER_PIN, sta_er_pin); 85 HANDLE_SYS_EVENT(WIFI, STA_WPS_ER_PBC_OVERLAP); 86 87 /* AP events */ 88 HANDLE_SYS_EVENT(WIFI, AP_START); 89 HANDLE_SYS_EVENT(WIFI, AP_STOP); 90 HANDLE_SYS_EVENT_ARG(WIFI, AP_STACONNECTED, sta_connected); 91 HANDLE_SYS_EVENT_ARG(WIFI, AP_STADISCONNECTED, sta_disconnected); 92 HANDLE_SYS_EVENT_ARG(WIFI, AP_PROBEREQRECVED, ap_probereqrecved); 93#if CONFIG_ETH_ENABLED 94 /* Ethernet events */ 95 /* Some extra defines to fit the old naming scheme... */ 96#define ETH_EVENT_ETH_START ETHERNET_EVENT_START 97#define ETH_EVENT_ETH_STOP ETHERNET_EVENT_STOP 98#define ETH_EVENT_ETH_CONNECTED ETHERNET_EVENT_CONNECTED 99#define ETH_EVENT_ETH_DISCONNECTED ETHERNET_EVENT_DISCONNECTED 100 101 HANDLE_SYS_EVENT(ETH, ETH_START); 102 HANDLE_SYS_EVENT(ETH, ETH_STOP); 103 HANDLE_SYS_EVENT(ETH, ETH_CONNECTED); 104 HANDLE_SYS_EVENT(ETH, ETH_DISCONNECTED); 105#endif 106 /* IP events */ 107 HANDLE_SYS_EVENT_ARG(IP, STA_GOT_IP, got_ip); 108 HANDLE_SYS_EVENT_ARG(IP, ETH_GOT_IP, got_ip); 109 HANDLE_SYS_EVENT(IP, STA_LOST_IP); 110 HANDLE_SYS_EVENT_ARG(IP, GOT_IP6, got_ip6); 111 HANDLE_SYS_EVENT_ARG(IP, AP_STAIPASSIGNED,ap_staipassigned); 112 default: 113 return ESP_ERR_NOT_SUPPORTED; 114 } 115} 116 117#ifdef WITH_EVENT_DEBUG 118 119static const char* TAG = "system_event"; 120 121typedef struct { 122 int err; 123 const char *reason; 124} wifi_reason_t; 125 126static const wifi_reason_t wifi_reason[] = 127{ 128 {0, "other reason"}, 129 {WIFI_REASON_UNSPECIFIED, "unspecified"}, 130 {WIFI_REASON_AUTH_EXPIRE, "auth expire"}, 131 {WIFI_REASON_AUTH_LEAVE, "auth leave"}, 132 {WIFI_REASON_ASSOC_EXPIRE, "assoc expire"}, 133 {WIFI_REASON_ASSOC_TOOMANY, "assoc too many"}, 134 {WIFI_REASON_NOT_AUTHED, "not authed"}, 135 {WIFI_REASON_NOT_ASSOCED, "not assoced"}, 136 {WIFI_REASON_ASSOC_LEAVE, "assoc leave"}, 137 {WIFI_REASON_ASSOC_NOT_AUTHED, "assoc not authed"}, 138 {WIFI_REASON_BEACON_TIMEOUT, "beacon timeout"}, 139 {WIFI_REASON_NO_AP_FOUND, "no ap found"}, 140 {WIFI_REASON_AUTH_FAIL, "auth fail"}, 141 {WIFI_REASON_ASSOC_FAIL, "assoc fail"}, 142 {WIFI_REASON_HANDSHAKE_TIMEOUT, "hanshake timeout"}, 143 {WIFI_REASON_DISASSOC_PWRCAP_BAD, "bad Power Capability, disassoc"}, 144 {WIFI_REASON_DISASSOC_SUPCHAN_BAD, "bad Supported Channels, disassoc"}, 145 {WIFI_REASON_IE_INVALID, "invalid IE"}, 146 {WIFI_REASON_MIC_FAILURE, "MIC failure"}, 147 {WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT, "4-way keying handshake timeout"}, 148 {WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT, "Group key handshake"}, 149 {WIFI_REASON_IE_IN_4WAY_DIFFERS, "IE in 4-way differs"}, 150 {WIFI_REASON_GROUP_CIPHER_INVALID, "invalid group cipher"}, 151 {WIFI_REASON_PAIRWISE_CIPHER_INVALID, "invalid pairwise cipher"}, 152 {WIFI_REASON_AKMP_INVALID, "invalid AKMP"}, 153 {WIFI_REASON_UNSUPP_RSN_IE_VERSION, "unsupported RSN IE version"}, 154 {WIFI_REASON_INVALID_RSN_IE_CAP, "invalid RSN IE capability"}, 155 {WIFI_REASON_802_1X_AUTH_FAILED, "802.1x auth failed"}, 156 {WIFI_REASON_CIPHER_SUITE_REJECTED, "cipher suite rejected"} 157}; 158 159static const char* wifi_disconnect_reason_to_str(int err) 160{ 161 for (int i=0; i< sizeof(wifi_reason)/sizeof(wifi_reason[0]); i++){ 162 if (err == wifi_reason[i].err){ 163 return wifi_reason[i].reason; 164 } 165 } 166 return wifi_reason[0].reason; 167} 168 169static void esp_system_event_debug(const system_event_t* event) 170{ 171 if (event == NULL) { 172 return; 173 } 174 175 switch (event->event_id) { 176 case SYSTEM_EVENT_WIFI_READY: { 177 ESP_LOGD(TAG, "SYSTEM_EVENT_WIFI_READY"); 178 break; 179 } 180 case SYSTEM_EVENT_SCAN_DONE: { 181 const system_event_sta_scan_done_t *scan_done = &event->event_info.scan_done; 182 ESP_LOGD(TAG, "SYSTEM_EVENT_SCAN_DONE, status:%d, number:%d", scan_done->status, scan_done->number); 183 break; 184 } 185 case SYSTEM_EVENT_STA_START: { 186 ESP_LOGD(TAG, "SYSTEM_EVENT_STA_START"); 187 break; 188 } 189 case SYSTEM_EVENT_STA_STOP: { 190 ESP_LOGD(TAG, "SYSTEM_EVENT_STA_STOP"); 191 break; 192 } 193 case SYSTEM_EVENT_STA_CONNECTED: { 194 const system_event_sta_connected_t *connected = &event->event_info.connected; 195 ESP_LOGD(TAG, "SYSTEM_EVENT_STA_CONNECTED, ssid:%s, ssid_len:%d, bssid:" MACSTR ", channel:%d, authmode:%d", \ 196 connected->ssid, connected->ssid_len, MAC2STR(connected->bssid), connected->channel, connected->authmode); 197 break; 198 } 199 case SYSTEM_EVENT_STA_DISCONNECTED: { 200 const system_event_sta_disconnected_t *disconnected = &event->event_info.disconnected; 201 ESP_LOGD(TAG, "SYSTEM_EVENT_STA_DISCONNECTED, ssid:%s, ssid_len:%d, bssid:" MACSTR ", reason:%d (%s)", \ 202 disconnected->ssid, disconnected->ssid_len, MAC2STR(disconnected->bssid), disconnected->reason, 203 wifi_disconnect_reason_to_str(disconnected->reason)); 204 break; 205 } 206 case SYSTEM_EVENT_STA_AUTHMODE_CHANGE: { 207 const system_event_sta_authmode_change_t *auth_change = &event->event_info.auth_change; 208 ESP_LOGD(TAG, "SYSTEM_EVENT_STA_AUTHMODE_CHNAGE, old_mode:%d, new_mode:%d", auth_change->old_mode, auth_change->new_mode); 209 break; 210 } 211 case SYSTEM_EVENT_FTM_REPORT: { 212 ESP_LOGD(TAG, "SYSTEM_EVENT_FTM_REPORT"); 213 break; 214 } 215 case SYSTEM_EVENT_STA_GOT_IP: { 216 const system_event_sta_got_ip_t *got_ip = &event->event_info.got_ip; 217 ESP_LOGD(TAG, "SYSTEM_EVENT_STA_GOT_IP, ip:" IPSTR ", mask:" IPSTR ", gw:" IPSTR, 218 IP2STR(&got_ip->ip_info.ip), 219 IP2STR(&got_ip->ip_info.netmask), 220 IP2STR(&got_ip->ip_info.gw)); 221 break; 222 } 223 case SYSTEM_EVENT_STA_LOST_IP: { 224 ESP_LOGD(TAG, "SYSTEM_EVENT_STA_LOST_IP"); 225 break; 226 } 227 case SYSTEM_EVENT_STA_WPS_ER_SUCCESS: { 228 ESP_LOGD(TAG, "SYSTEM_EVENT_STA_WPS_ER_SUCCESS"); 229 break; 230 } 231 case SYSTEM_EVENT_STA_WPS_ER_FAILED: { 232 ESP_LOGD(TAG, "SYSTEM_EVENT_STA_WPS_ER_FAILED"); 233 break; 234 } 235 case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT: { 236 ESP_LOGD(TAG, "SYSTEM_EVENT_STA_WPS_ER_TIMEOUT"); 237 break; 238 } 239 case SYSTEM_EVENT_STA_WPS_ER_PIN: { 240 ESP_LOGD(TAG, "SYSTEM_EVENT_STA_WPS_ER_PIN"); 241 break; 242 } 243 case SYSTEM_EVENT_STA_WPS_ER_PBC_OVERLAP: { 244 ESP_LOGD(TAG, "SYSTEM_EVENT_STA_WPS_ER_PBC_OVERLAP"); 245 break; 246 } 247 case SYSTEM_EVENT_AP_START: { 248 ESP_LOGD(TAG, "SYSTEM_EVENT_AP_START"); 249 break; 250 } 251 case SYSTEM_EVENT_AP_STOP: { 252 ESP_LOGD(TAG, "SYSTEM_EVENT_AP_STOP"); 253 break; 254 } 255 case SYSTEM_EVENT_AP_STACONNECTED: { 256 const system_event_ap_staconnected_t *staconnected = &event->event_info.sta_connected; 257 ESP_LOGD(TAG, "SYSTEM_EVENT_AP_STACONNECTED, mac:" MACSTR ", aid:%d", \ 258 MAC2STR(staconnected->mac), staconnected->aid); 259 break; 260 } 261 case SYSTEM_EVENT_AP_STADISCONNECTED: { 262 const system_event_ap_stadisconnected_t *stadisconnected = &event->event_info.sta_disconnected; 263 ESP_LOGD(TAG, "SYSTEM_EVENT_AP_STADISCONNECTED, mac:" MACSTR ", aid:%d", \ 264 MAC2STR(stadisconnected->mac), stadisconnected->aid); 265 break; 266 } 267 case SYSTEM_EVENT_AP_STAIPASSIGNED: { 268 ESP_LOGD(TAG, "SYSTEM_EVENT_AP_STAIPASSIGNED"); 269 break; 270 } 271 case SYSTEM_EVENT_AP_PROBEREQRECVED: { 272 const system_event_ap_probe_req_rx_t *ap_probereqrecved = &event->event_info.ap_probereqrecved; 273 ESP_LOGD(TAG, "SYSTEM_EVENT_AP_PROBEREQRECVED, rssi:%d, mac:" MACSTR, \ 274 ap_probereqrecved->rssi, \ 275 MAC2STR(ap_probereqrecved->mac)); 276 break; 277 } 278 case SYSTEM_EVENT_GOT_IP6: { 279 const esp_ip6_addr_t *addr = &event->event_info.got_ip6.ip6_info.ip; 280 ESP_LOGD(TAG, "SYSTEM_EVENT_AP_STA_GOT_IP6 address " IPV6STR, IPV62STR(*addr)); 281 break; 282 } 283#if CONFIG_IDF_TARGET_ESP32 284 case SYSTEM_EVENT_ETH_START: { 285 ESP_LOGD(TAG, "SYSTEM_EVENT_ETH_START"); 286 break; 287 } 288 case SYSTEM_EVENT_ETH_STOP: { 289 ESP_LOGD(TAG, "SYSTEM_EVENT_ETH_STOP"); 290 break; 291 } 292 case SYSTEM_EVENT_ETH_CONNECTED: { 293 ESP_LOGD(TAG, "SYSTEM_EVENT_ETH_CONNECETED"); 294 break; 295 } 296 case SYSTEM_EVENT_ETH_DISCONNECTED: { 297 ESP_LOGD(TAG, "SYSTEM_EVENT_ETH_DISCONNECETED"); 298 break; 299 } 300 case SYSTEM_EVENT_ETH_GOT_IP: { 301 const system_event_sta_got_ip_t *got_ip = &event->event_info.got_ip; 302 ESP_LOGD(TAG, "SYSTEM_EVENT_ETH_GOT_IP, ip:" IPSTR ", mask:" IPSTR ", gw:" IPSTR, 303 IP2STR(&got_ip->ip_info.ip), 304 IP2STR(&got_ip->ip_info.netmask), 305 IP2STR(&got_ip->ip_info.gw)); 306 break; 307 } 308#endif 309 default: { 310 ESP_LOGW(TAG, "unexpected system event %d!", event->event_id); 311 break; 312 } 313 } 314} 315 316#endif // WITH_EVENT_DEBUG 317