/** * callback for WiFi events * @param arg */ void ESP8266WiFiGenericClass::_eventCallback(void* arg) { System_Event_t* event = reinterpret_cast<System_Event_t*>(arg); DEBUG_WIFI("wifi evt: %d\n", event->event); if(event->event == EVENT_STAMODE_DISCONNECTED) { DEBUG_WIFI("STA disconnect: %d\n", event->event_info.disconnected.reason); WiFiClient::stopAll(); } for(uint32_t i = 0; i < cbEventList.size(); i++) { WiFiEventCbList_t entry = cbEventList[i]; if(entry.cb) { if(entry.event == (WiFiEvent_t) event->event || entry.event == WIFI_EVENT_MAX) { entry.cb((WiFiEvent_t) event->event); } } } }
/** * callback for WiFi events * @param arg */ void ESP8266WiFiGenericClass::_eventCallback(void* arg) { System_Event_t* event = reinterpret_cast<System_Event_t*>(arg); DEBUG_WIFI("wifi evt: %d\n", event->event); if(event->event == EVENT_STAMODE_DISCONNECTED) { DEBUG_WIFI("STA disconnect: %d\n", event->event_info.disconnected.reason); WiFiClient::stopAll(); } for(auto it = std::begin(sCbEventList); it != std::end(sCbEventList); ) { WiFiEventHandler &handler = *it; if (handler->canExpire() && handler.unique()) { it = sCbEventList.erase(it); } else { (*handler)(event); ++it; } } }
/** * Disconnect from the network (close AP) * @param wifioff disable mode? * @return one value of wl_status_t enum */ bool ESP8266WiFiAPClass::softAPdisconnect(bool wifioff) { bool ret; struct softap_config conf; *conf.ssid = 0; *conf.password = 0; conf.authmode = AUTH_OPEN; ETS_UART_INTR_DISABLE(); if(WiFi._persistent) { ret = wifi_softap_set_config(&conf); } else { ret = wifi_softap_set_config_current(&conf); } ETS_UART_INTR_ENABLE(); if(!ret) { DEBUG_WIFI("[APdisconnect] set_config failed!\n"); } if(ret && wifioff) { ret = WiFi.enableAP(false); } return ret; }
/** * Set up an access point * @param ssid Pointer to the SSID (max 63 char). * @param passphrase (for WPA2 min 8 char, for open use NULL) * @param channel WiFi channel number, 1 - 13. * @param ssid_hidden Network cloaking (0 = broadcast SSID, 1 = hide SSID) * @param max_connection Max simultaneous connected clients, 1 - 4. */ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden, int max_connection) { if(!WiFi.enableAP(true)) { // enable AP failed DEBUG_WIFI("[AP] enableAP failed!\n"); return false; } if(!ssid || strlen(ssid) == 0 || strlen(ssid) > 31) { // fail SSID too long or missing! DEBUG_WIFI("[AP] SSID too long or missing!\n"); return false; } if(passphrase && strlen(passphrase) > 0 && (strlen(passphrase) > 63 || strlen(passphrase) < 8)) { // fail passphrase to long or short! DEBUG_WIFI("[AP] fail passphrase to long or short!\n"); return false; } bool ret = true; struct softap_config conf; strcpy(reinterpret_cast<char*>(conf.ssid), ssid); conf.channel = channel; conf.ssid_len = strlen(ssid); conf.ssid_hidden = ssid_hidden; conf.max_connection = max_connection; conf.beacon_interval = 100; if(!passphrase || strlen(passphrase) == 0) { conf.authmode = AUTH_OPEN; *conf.password = 0; } else { conf.authmode = AUTH_WPA2_PSK; strcpy(reinterpret_cast<char*>(conf.password), passphrase); } struct softap_config conf_compare; if(WiFi._persistent){ wifi_softap_get_config_default(&conf_compare); } else { wifi_softap_get_config(&conf_compare); } if(!softap_config_equal(conf, conf_compare)) { ETS_UART_INTR_DISABLE(); if(WiFi._persistent) { ret = wifi_softap_set_config(&conf); } else { ret = wifi_softap_set_config_current(&conf); } ETS_UART_INTR_ENABLE(); if(!ret) { DEBUG_WIFI("[AP] set_config failed!\n"); return false; } } else { DEBUG_WIFI("[AP] softap config unchanged\n"); } if(wifi_softap_dhcps_status() != DHCP_STARTED) { DEBUG_WIFI("[AP] DHCP not started, starting...\n"); if(!wifi_softap_dhcps_start()) { DEBUG_WIFI("[AP] wifi_softap_dhcps_start failed!\n"); ret = false; } } // check IP config struct ip_info ip; if(wifi_get_ip_info(SOFTAP_IF, &ip)) { if(ip.ip.addr == 0x00000000) { // Invalid config DEBUG_WIFI("[AP] IP config Invalid resetting...\n"); //192.168.244.1 , 192.168.244.1 , 255.255.255.0 ret = softAPConfig(0x01F4A8C0, 0x01F4A8C0, 0x00FFFFFF); if(!ret) { DEBUG_WIFI("[AP] softAPConfig failed!\n"); ret = false; } } } else { DEBUG_WIFI("[AP] wifi_get_ip_info failed!\n"); ret = false; } return ret; }
/** * Configure access point * @param local_ip access point IP * @param gateway gateway IP * @param subnet subnet mask */ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { DEBUG_WIFI("[APConfig] local_ip: %s gateway: %s subnet: %s\n", local_ip.toString().c_str(), gateway.toString().c_str(), subnet.toString().c_str()); if(!WiFi.enableAP(true)) { // enable AP failed DEBUG_WIFI("[APConfig] enableAP failed!\n"); return false; } bool ret = true; struct ip_info info; info.ip.addr = static_cast<uint32_t>(local_ip); info.gw.addr = static_cast<uint32_t>(gateway); info.netmask.addr = static_cast<uint32_t>(subnet); if(!wifi_softap_dhcps_stop()) { DEBUG_WIFI("[APConfig] wifi_softap_dhcps_stop failed!\n"); } if(!wifi_set_ip_info(SOFTAP_IF, &info)) { DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n"); ret = false; } struct dhcps_lease dhcp_lease; IPAddress ip = local_ip; ip[3] += 99; dhcp_lease.start_ip.addr = static_cast<uint32_t>(ip); DEBUG_WIFI("[APConfig] DHCP IP start: %s\n", ip.toString().c_str()); ip[3] += 100; dhcp_lease.end_ip.addr = static_cast<uint32_t>(ip); DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str()); if(!wifi_softap_set_dhcps_lease(&dhcp_lease)) { DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n"); ret = false; } // set lease time to 720min --> 12h if(!wifi_softap_set_dhcps_lease_time(720)) { DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_lease_time failed!\n"); ret = false; } uint8 mode = 1; if(!wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &mode)) { DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_offer_option failed!\n"); ret = false; } if(!wifi_softap_dhcps_start()) { DEBUG_WIFI("[APConfig] wifi_softap_dhcps_start failed!\n"); ret = false; } // check config if(wifi_get_ip_info(SOFTAP_IF, &info)) { if(info.ip.addr == 0x00000000) { DEBUG_WIFI("[APConfig] IP config Invalid?!\n"); ret = false; } else if(local_ip != info.ip.addr) { ip = info.ip.addr; DEBUG_WIFI("[APConfig] IP config not set correct?! new IP: %s\n", ip.toString().c_str()); ret = false; } } else { DEBUG_WIFI("[APConfig] wifi_get_ip_info failed!\n"); ret = false; } return ret; }