From 09a7940006c9672e5d2247f9872f9b0f9c96444e Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Mon, 28 Dec 2015 16:36:55 +0100 Subject: [PATCH 01/21] code style --- libraries/ESP8266WiFi/src/ESP8266WiFi.cpp | 439 ++++++-------- libraries/ESP8266WiFi/src/ESP8266WiFi.h | 680 +++++++++++----------- 2 files changed, 511 insertions(+), 608 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp index a95bd1f7d..b946d5bdf 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp @@ -1,23 +1,23 @@ /* - ESP8266WiFi.cpp - WiFi library for esp8266 + ESP8266WiFi.cpp - WiFi library for esp8266 - Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. - This file is part of the esp8266 core for Arduino environment. + Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ #include "ESP8266WiFi.h" @@ -41,27 +41,20 @@ extern "C" { extern "C" void esp_schedule(); extern "C" void esp_yield(); -ESP8266WiFiClass::ESP8266WiFiClass() -: _smartConfigStarted(false) -, _smartConfigDone(false) -, _useStaticIp(false) -, _persistent(true) -{ +ESP8266WiFiClass::ESP8266WiFiClass() : + _smartConfigStarted(false), _smartConfigDone(false), _useStaticIp(false), _persistent(true) { uint8 m = wifi_get_opmode(); _useClientMode = (m & WIFI_STA); _useApMode = (m & WIFI_AP); - wifi_set_event_handler_cb((wifi_event_handler_cb_t)&ESP8266WiFiClass::_eventCallback); + wifi_set_event_handler_cb((wifi_event_handler_cb_t) &ESP8266WiFiClass::_eventCallback); } -void ESP8266WiFiClass::persistent(bool persistent) -{ +void ESP8266WiFiClass::persistent(bool persistent) { _persistent = persistent; } - -void ESP8266WiFiClass::mode(WiFiMode m) -{ - if(wifi_get_opmode() == (uint8)m) { +void ESP8266WiFiClass::mode(WiFiMode m) { + if(wifi_get_opmode() == (uint8) m) { return; } @@ -80,19 +73,17 @@ void ESP8266WiFiClass::mode(WiFiMode m) _mode(m); } -WiFiMode ESP8266WiFiClass::getMode() -{ - return (WiFiMode)wifi_get_opmode(); +WiFiMode ESP8266WiFiClass::getMode() { + return (WiFiMode) wifi_get_opmode(); } -void ESP8266WiFiClass::_mode(WiFiMode m) -{ - if(wifi_get_opmode() == (uint8)m) { +void ESP8266WiFiClass::_mode(WiFiMode m) { + if(wifi_get_opmode() == (uint8) m) { return; } ETS_UART_INTR_DISABLE(); - if (_persistent) + if(_persistent) wifi_set_opmode(m); else wifi_set_opmode_current(m); @@ -100,36 +91,32 @@ void ESP8266WiFiClass::_mode(WiFiMode m) } -static bool sta_config_equal(const station_config& lhs, const station_config& rhs) -{ - if (strcmp(reinterpret_cast(lhs.ssid), reinterpret_cast(rhs.ssid)) != 0) +static bool sta_config_equal(const station_config& lhs, const station_config& rhs) { + if(strcmp(reinterpret_cast(lhs.ssid), reinterpret_cast(rhs.ssid)) != 0) return false; - if (strcmp(reinterpret_cast(lhs.password), reinterpret_cast(rhs.password)) != 0) + if(strcmp(reinterpret_cast(lhs.password), reinterpret_cast(rhs.password)) != 0) return false; - if (lhs.bssid_set) { - if (!rhs.bssid_set) + if(lhs.bssid_set) { + if(!rhs.bssid_set) return false; - if (memcmp(lhs.bssid, rhs.bssid, 6) != 0) + if(memcmp(lhs.bssid, rhs.bssid, 6) != 0) return false; - } - else { - if (rhs.bssid_set) + } else { + if(rhs.bssid_set) return false; } return true; } -int ESP8266WiFiClass::begin(char* ssid, char *passphrase, int32_t channel, const uint8_t* bssid) -{ +int ESP8266WiFiClass::begin(char* ssid, char *passphrase, int32_t channel, const uint8_t* bssid) { return begin((const char*) ssid, (const char*) passphrase, channel, bssid); } -int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid) -{ +int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid) { _useClientMode = true; if(_useApMode) { @@ -153,13 +140,13 @@ int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t ch struct station_config conf; strcpy(reinterpret_cast(conf.ssid), ssid); - if (passphrase) { + if(passphrase) { strcpy(reinterpret_cast(conf.password), passphrase); } else { *conf.password = 0; } - if (bssid) { + if(bssid) { conf.bssid_set = 1; memcpy((void *) &conf.bssid[0], (void *) bssid, 6); } else { @@ -168,13 +155,13 @@ int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t ch struct station_config current_conf; wifi_station_get_config(¤t_conf); - if (sta_config_equal(current_conf, conf)) { + if(sta_config_equal(current_conf, conf)) { DEBUGV("sta config unchanged"); return status(); } ETS_UART_INTR_DISABLE(); - if (_persistent) + if(_persistent) wifi_station_set_config(&conf); else wifi_station_set_config_current(&conf); @@ -190,8 +177,7 @@ int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t ch return status(); } -int ESP8266WiFiClass::begin() -{ +int ESP8266WiFiClass::begin() { ETS_UART_INTR_DISABLE(); wifi_station_connect(); ETS_UART_INTR_ENABLE(); @@ -201,18 +187,16 @@ int ESP8266WiFiClass::begin() return status(); } -uint8_t ESP8266WiFiClass::waitForConnectResult(){ - if ((wifi_get_opmode() & 1) == 0)//1 and 3 have STA enabled - return WL_DISCONNECTED; - while (status() == WL_DISCONNECTED) - delay(100); - return status(); +uint8_t ESP8266WiFiClass::waitForConnectResult() { + if((wifi_get_opmode() & 1) == 0) //1 and 3 have STA enabled + return WL_DISCONNECTED; + while(status() == WL_DISCONNECTED) + delay(100); + return status(); } - // You will have to set the DNS-Server manually later since this will not enable DHCP2 -void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) -{ +void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { struct ip_info info; info.ip.addr = static_cast(local_ip); info.gw.addr = static_cast(gateway); @@ -224,8 +208,7 @@ void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress s _useStaticIp = true; } -void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns) -{ +void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns) { struct ip_info info; info.ip.addr = static_cast(local_ip); info.gw.addr = static_cast(gateway); @@ -237,18 +220,17 @@ void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress s // Set DNS-Server ip_addr_t d; d.addr = static_cast(dns); - dns_setserver(0,&d); + dns_setserver(0, &d); _useStaticIp = true; } -int ESP8266WiFiClass::softAPdisconnect(bool wifioff) -{ +int ESP8266WiFiClass::softAPdisconnect(bool wifioff) { struct softap_config conf; *conf.ssid = 0; *conf.password = 0; ETS_UART_INTR_DISABLE(); - if (_persistent) + if(_persistent) wifi_softap_set_config(&conf); else wifi_softap_set_config_current(&conf); @@ -257,7 +239,7 @@ int ESP8266WiFiClass::softAPdisconnect(bool wifioff) if(wifioff) { _useApMode = false; - if( _useClientMode) { + if(_useClientMode) { // turn on STA _mode(WIFI_STA); } else { @@ -269,13 +251,12 @@ int ESP8266WiFiClass::softAPdisconnect(bool wifioff) return 0; } -int ESP8266WiFiClass::disconnect(bool wifioff) -{ +int ESP8266WiFiClass::disconnect(bool wifioff) { struct station_config conf; *conf.ssid = 0; *conf.password = 0; ETS_UART_INTR_DISABLE(); - if (_persistent) + if(_persistent) wifi_station_set_config(&conf); else wifi_station_set_config_current(&conf); @@ -297,28 +278,23 @@ int ESP8266WiFiClass::disconnect(bool wifioff) return 0; } -static bool softap_config_equal(const softap_config& lhs, const softap_config& rhs) -{ - if (strcmp(reinterpret_cast(lhs.ssid), reinterpret_cast(rhs.ssid)) != 0) +static bool softap_config_equal(const softap_config& lhs, const softap_config& rhs) { + if(strcmp(reinterpret_cast(lhs.ssid), reinterpret_cast(rhs.ssid)) != 0) return false; - if (strcmp(reinterpret_cast(lhs.password), reinterpret_cast(rhs.password)) != 0) + if(strcmp(reinterpret_cast(lhs.password), reinterpret_cast(rhs.password)) != 0) return false; - if (lhs.channel != rhs.channel) + if(lhs.channel != rhs.channel) return false; - if (lhs.ssid_hidden != rhs.ssid_hidden) + if(lhs.ssid_hidden != rhs.ssid_hidden) return false; return true; } - -void ESP8266WiFiClass::softAP(const char* ssid) -{ +void ESP8266WiFiClass::softAP(const char* ssid) { softAP(ssid, 0); } - -void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden) -{ +void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden) { _useApMode = true; if(_useClientMode) { // turn on AP+STA mode @@ -347,35 +323,30 @@ void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase, int chan conf.max_connection = 4; conf.beacon_interval = 100; - if (!passphrase || strlen(passphrase) == 0) - { + if(!passphrase || strlen(passphrase) == 0) { conf.authmode = AUTH_OPEN; *conf.password = 0; - } - else - { + } else { conf.authmode = AUTH_WPA2_PSK; strcpy(reinterpret_cast(conf.password), passphrase); } struct softap_config conf_current; wifi_softap_get_config(&conf_current); - if (softap_config_equal(conf, conf_current)) - { + if(softap_config_equal(conf, conf_current)) { DEBUGV("softap config unchanged"); return; } ETS_UART_INTR_DISABLE(); - if (_persistent) + if(_persistent) wifi_softap_set_config(&conf); else wifi_softap_set_config_current(&conf); ETS_UART_INTR_ENABLE(); } -void ESP8266WiFiClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) -{ +void ESP8266WiFiClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { struct ip_info info; info.ip.addr = static_cast(local_ip); info.gw.addr = static_cast(gateway); @@ -385,142 +356,120 @@ void ESP8266WiFiClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAdd wifi_softap_dhcps_start(); } -uint8_t* ESP8266WiFiClass::macAddress(uint8_t* mac) -{ +uint8_t* ESP8266WiFiClass::macAddress(uint8_t* mac) { wifi_get_macaddr(STATION_IF, mac); return mac; } -String ESP8266WiFiClass::macAddress(void) -{ +String ESP8266WiFiClass::macAddress(void) { uint8_t mac[6]; - char macStr[18] = {0}; + char macStr[18] = { 0 }; wifi_get_macaddr(STATION_IF, mac); - sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); return String(macStr); } -uint8_t* ESP8266WiFiClass::softAPmacAddress(uint8_t* mac) -{ +uint8_t* ESP8266WiFiClass::softAPmacAddress(uint8_t* mac) { wifi_get_macaddr(SOFTAP_IF, mac); return mac; } -String ESP8266WiFiClass::softAPmacAddress(void) -{ +String ESP8266WiFiClass::softAPmacAddress(void) { uint8_t mac[6]; - char macStr[18] = {0}; + char macStr[18] = { 0 }; wifi_get_macaddr(SOFTAP_IF, mac); - sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); return String(macStr); } -IPAddress ESP8266WiFiClass::localIP() -{ +IPAddress ESP8266WiFiClass::localIP() { struct ip_info ip; wifi_get_ip_info(STATION_IF, &ip); return IPAddress(ip.ip.addr); } -IPAddress ESP8266WiFiClass::softAPIP() -{ +IPAddress ESP8266WiFiClass::softAPIP() { struct ip_info ip; wifi_get_ip_info(SOFTAP_IF, &ip); return IPAddress(ip.ip.addr); } -IPAddress ESP8266WiFiClass::subnetMask() -{ +IPAddress ESP8266WiFiClass::subnetMask() { struct ip_info ip; wifi_get_ip_info(STATION_IF, &ip); return IPAddress(ip.netmask.addr); } -IPAddress ESP8266WiFiClass::gatewayIP() -{ +IPAddress ESP8266WiFiClass::gatewayIP() { struct ip_info ip; wifi_get_ip_info(STATION_IF, &ip); return IPAddress(ip.gw.addr); } -IPAddress ESP8266WiFiClass::dnsIP(int dns_no) -{ +IPAddress ESP8266WiFiClass::dnsIP(int dns_no) { ip_addr_t dns_ip = dns_getserver(dns_no); return IPAddress(dns_ip.addr); } -String ESP8266WiFiClass::SSID() const -{ +String ESP8266WiFiClass::SSID() const { static struct station_config conf; wifi_station_get_config(&conf); return String(reinterpret_cast(conf.ssid)); } -String ESP8266WiFiClass::psk() const -{ +String ESP8266WiFiClass::psk() const { static struct station_config conf; wifi_station_get_config(&conf); - return String(reinterpret_cast(conf.password)); + return String(reinterpret_cast(conf.password)); } -uint8_t* ESP8266WiFiClass::BSSID(void) -{ +uint8_t* ESP8266WiFiClass::BSSID(void) { static struct station_config conf; wifi_station_get_config(&conf); return reinterpret_cast(conf.bssid); } -String ESP8266WiFiClass::BSSIDstr(void) -{ +String ESP8266WiFiClass::BSSIDstr(void) { static struct station_config conf; - char mac[18] = {0}; + char mac[18] = { 0 }; wifi_station_get_config(&conf); - sprintf(mac,"%02X:%02X:%02X:%02X:%02X:%02X", conf.bssid[0], conf.bssid[1], conf.bssid[2], conf.bssid[3], conf.bssid[4], conf.bssid[5]); + sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", conf.bssid[0], conf.bssid[1], conf.bssid[2], conf.bssid[3], conf.bssid[4], conf.bssid[5]); return String(mac); } - int32_t ESP8266WiFiClass::channel(void) { return wifi_get_channel(); } - int32_t ESP8266WiFiClass::RSSI(void) { return wifi_station_get_rssi(); } -extern "C" -{ - typedef STAILQ_HEAD(, bss_info) bss_info_head_t; +extern "C" { +typedef STAILQ_HEAD(, bss_info) +bss_info_head_t; } -void ESP8266WiFiClass::_scanDone(void* result, int status) -{ - if (status != OK) - { +void ESP8266WiFiClass::_scanDone(void* result, int status) { + if(status != OK) { ESP8266WiFiClass::_scanCount = 0; ESP8266WiFiClass::_scanResult = 0; - } - else - { + } else { int i = 0; bss_info_head_t* head = reinterpret_cast(result); - for (bss_info* it = STAILQ_FIRST(head); it; it = STAILQ_NEXT(it, next), ++i); + for(bss_info* it = STAILQ_FIRST(head); it; it = STAILQ_NEXT(it, next), ++i) + ; ESP8266WiFiClass::_scanCount = i; - if (i == 0) - { + if(i == 0) { ESP8266WiFiClass::_scanResult = 0; - } - else - { + } else { bss_info* copied_info = new bss_info[i]; i = 0; - for (bss_info* it = STAILQ_FIRST(head); it; it = STAILQ_NEXT(it, next), ++i) - { + for(bss_info* it = STAILQ_FIRST(head); it; it = STAILQ_NEXT(it, next), ++i) { memcpy(copied_info + i, it, sizeof(bss_info)); } @@ -550,10 +499,8 @@ int8_t ESP8266WiFiClass::scanComplete() { return WIFI_SCAN_FAILED; } -void ESP8266WiFiClass::scanDelete() -{ - if (ESP8266WiFiClass::_scanResult) - { +void ESP8266WiFiClass::scanDelete() { + if(ESP8266WiFiClass::_scanResult) { delete[] reinterpret_cast(ESP8266WiFiClass::_scanResult); ESP8266WiFiClass::_scanResult = 0; ESP8266WiFiClass::_scanCount = 0; @@ -561,8 +508,7 @@ void ESP8266WiFiClass::scanDelete() _scanComplete = false; } -int8_t ESP8266WiFiClass::scanNetworks(bool async, bool show_hidden) -{ +int8_t ESP8266WiFiClass::scanNetworks(bool async, bool show_hidden) { if(ESP8266WiFiClass::_scanStarted) { return WIFI_SCAN_RUNNING; } @@ -578,8 +524,7 @@ int8_t ESP8266WiFiClass::scanNetworks(bool async, bool show_hidden) } int status = wifi_station_get_connect_status(); - if (status != STATION_GOT_IP && status != STATION_IDLE) - { + if(status != STATION_GOT_IP && status != STATION_IDLE) { disconnect(); } @@ -607,142 +552,126 @@ int8_t ESP8266WiFiClass::scanNetworks(bool async, bool show_hidden) } -void * ESP8266WiFiClass::_getScanInfoByIndex(int i) -{ - if (!ESP8266WiFiClass::_scanResult || (size_t)i > ESP8266WiFiClass::_scanCount) - { +void * ESP8266WiFiClass::_getScanInfoByIndex(int i) { + if(!ESP8266WiFiClass::_scanResult || (size_t) i > ESP8266WiFiClass::_scanCount) { return 0; } return reinterpret_cast(ESP8266WiFiClass::_scanResult) + i; } -String ESP8266WiFiClass::SSID(uint8_t i) -{ +String ESP8266WiFiClass::SSID(uint8_t i) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if (!it) + if(!it) return ""; return String(reinterpret_cast(it->ssid)); } -uint8_t * ESP8266WiFiClass::BSSID(uint8_t i) -{ +uint8_t * ESP8266WiFiClass::BSSID(uint8_t i) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if (!it) + if(!it) return 0; return it->bssid; } -String ESP8266WiFiClass::BSSIDstr(uint8_t i) -{ - char mac[18] = {0}; +String ESP8266WiFiClass::BSSIDstr(uint8_t i) { + char mac[18] = { 0 }; struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if (!it) + if(!it) return String(""); - sprintf(mac,"%02X:%02X:%02X:%02X:%02X:%02X", it->bssid[0], it->bssid[1], it->bssid[2], it->bssid[3], it->bssid[4], it->bssid[5]); + sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", it->bssid[0], it->bssid[1], it->bssid[2], it->bssid[3], it->bssid[4], it->bssid[5]); return String(mac); } -int32_t ESP8266WiFiClass::channel(uint8_t i) -{ +int32_t ESP8266WiFiClass::channel(uint8_t i) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if (!it) + if(!it) return 0; return it->channel; } -bool ESP8266WiFiClass::isHidden(uint8_t i) -{ +bool ESP8266WiFiClass::isHidden(uint8_t i) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if (!it) + if(!it) return false; return (it->is_hidden != 0); } -bool ESP8266WiFiClass::getNetworkInfo(uint8_t i, String &ssid, uint8_t &encType, int32_t &rssi, uint8_t* &bssid, int32_t &channel, bool &isHidden) -{ +bool ESP8266WiFiClass::getNetworkInfo(uint8_t i, String &ssid, uint8_t &encType, int32_t &rssi, uint8_t* &bssid, int32_t &channel, bool &isHidden) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if (!it) - return false; + if(!it) + return false; - ssid = (const char*)it->ssid; - encType = encryptionType(i); - rssi = it->rssi; - bssid = it->bssid; // move ptr - channel = it->channel; - isHidden = (it->is_hidden != 0); + ssid = (const char*) it->ssid; + encType = encryptionType(i); + rssi = it->rssi; + bssid = it->bssid; // move ptr + channel = it->channel; + isHidden = (it->is_hidden != 0); - return true; + return true; } -int32_t ESP8266WiFiClass::RSSI(uint8_t i) -{ +int32_t ESP8266WiFiClass::RSSI(uint8_t i) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if (!it) + if(!it) return 0; return it->rssi; } -uint8_t ESP8266WiFiClass::encryptionType(uint8_t i) -{ +uint8_t ESP8266WiFiClass::encryptionType(uint8_t i) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if (!it) + if(!it) return -1; int authmode = it->authmode; - if (authmode == AUTH_OPEN) + if(authmode == AUTH_OPEN) return ENC_TYPE_NONE; - if (authmode == AUTH_WEP) + if(authmode == AUTH_WEP) return ENC_TYPE_WEP; - if (authmode == AUTH_WPA_PSK) + if(authmode == AUTH_WPA_PSK) return ENC_TYPE_TKIP; - if (authmode == AUTH_WPA2_PSK) + if(authmode == AUTH_WPA2_PSK) return ENC_TYPE_CCMP; - if (authmode == AUTH_WPA_WPA2_PSK) + if(authmode == AUTH_WPA_WPA2_PSK) return ENC_TYPE_AUTO; return -1; } -wl_status_t ESP8266WiFiClass::status() -{ +wl_status_t ESP8266WiFiClass::status() { int status = wifi_station_get_connect_status(); - if (status == STATION_GOT_IP) - return WL_CONNECTED; - else if (status == STATION_NO_AP_FOUND) - return WL_NO_SSID_AVAIL; - else if (status == STATION_CONNECT_FAIL || status == STATION_WRONG_PASSWORD) - return WL_CONNECT_FAILED; - else if (status == STATION_IDLE) - return WL_IDLE_STATUS; + if(status == STATION_GOT_IP) + return WL_CONNECTED; + else if(status == STATION_NO_AP_FOUND) + return WL_NO_SSID_AVAIL; + else if(status == STATION_CONNECT_FAIL || status == STATION_WRONG_PASSWORD) + return WL_CONNECT_FAILED; + else if(status == STATION_IDLE) + return WL_IDLE_STATUS; else - return WL_DISCONNECTED; + return WL_DISCONNECTED; } -void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback_arg) -{ - if (ipaddr) +void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback_arg) { + if(ipaddr) (*reinterpret_cast(callback_arg)) = ipaddr->addr; esp_schedule(); // resume the hostByName function } -int ESP8266WiFiClass::hostByName(const char* aHostname, IPAddress& aResult) -{ +int ESP8266WiFiClass::hostByName(const char* aHostname, IPAddress& aResult) { ip_addr_t addr; aResult = static_cast(0); err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult); - if (err == ERR_OK) - { + if(err == ERR_OK) { aResult = addr.addr; - } - else if (err == ERR_INPROGRESS) - { + } else if(err == ERR_INPROGRESS) { esp_yield(); // will return here when dns_found_callback fires } @@ -771,13 +700,12 @@ bool ESP8266WiFiClass::hostname(String aHostname) { //-------------------------------------------------------------- -void wifi_wps_status_cb(wps_cb_status status) -{ +void wifi_wps_status_cb(wps_cb_status status) { DEBUGV("wps cb status: %d\r\n", status); - switch (status) { + switch(status) { case WPS_CB_ST_SUCCESS: if(!wifi_wps_disable()) { - DEBUGV("wps disable failed\n"); + DEBUGV("wps disable failed\n"); } wifi_station_connect(); break; @@ -800,13 +728,13 @@ bool ESP8266WiFiClass::beginWPSConfig(void) { _useClientMode = true; - if(_useApMode) { - // turn on AP+STA mode - _mode(WIFI_AP_STA); - } else { - // turn on STA mode - _mode(WIFI_STA); - } + if(_useApMode) { + // turn on AP+STA mode + _mode(WIFI_AP_STA); + } else { + // turn on STA mode + _mode(WIFI_STA); + } disconnect(); @@ -841,11 +769,8 @@ bool ESP8266WiFiClass::beginWPSConfig(void) { //-------------------------------------------------------------- - - -void ESP8266WiFiClass::beginSmartConfig() -{ - if (_smartConfigStarted) +void ESP8266WiFiClass::beginSmartConfig() { + if(_smartConfigStarted) return; if(_useApMode) { @@ -863,27 +788,24 @@ void ESP8266WiFiClass::beginSmartConfig() smartconfig_start(reinterpret_cast(&ESP8266WiFiClass::_smartConfigCallback), 1); } -void ESP8266WiFiClass::stopSmartConfig() -{ - if (!_smartConfigStarted) +void ESP8266WiFiClass::stopSmartConfig() { + if(!_smartConfigStarted) return; smartconfig_stop(); _smartConfigStarted = false; } -bool ESP8266WiFiClass::smartConfigDone() -{ - if (!_smartConfigStarted) +bool ESP8266WiFiClass::smartConfigDone() { + if(!_smartConfigStarted) return false; return _smartConfigDone; } -void ESP8266WiFiClass::_smartConfigCallback(uint32_t st, void* result) -{ +void ESP8266WiFiClass::_smartConfigCallback(uint32_t st, void* result) { sc_status status = (sc_status) st; - if (status == SC_STATUS_LINK) { + if(status == SC_STATUS_LINK) { station_config* sta_conf = reinterpret_cast(result); wifi_station_set_config(sta_conf); @@ -891,13 +813,11 @@ void ESP8266WiFiClass::_smartConfigCallback(uint32_t st, void* result) wifi_station_connect(); WiFi._smartConfigDone = true; - } - else if (status == SC_STATUS_LINK_OVER) { + } else if(status == SC_STATUS_LINK_OVER) { WiFi.stopSmartConfig(); } } - //-------------------------------------------------------------- /** @@ -906,7 +826,7 @@ void ESP8266WiFiClass::_smartConfigCallback(uint32_t st, void* result) * @return bool */ bool ESP8266WiFiClass::setSleepMode(WiFiSleepType_t type) { - return wifi_set_sleep_type((sleep_type_t)type); + return wifi_set_sleep_type((sleep_type_t) type); } /** @@ -914,7 +834,7 @@ bool ESP8266WiFiClass::setSleepMode(WiFiSleepType_t type) { * @return sleep_type_t */ WiFiSleepType_t ESP8266WiFiClass::getSleepMode() { - return (WiFiSleepType_t)wifi_get_sleep_type(); + return (WiFiSleepType_t) wifi_get_sleep_type(); } /** @@ -923,7 +843,7 @@ WiFiSleepType_t ESP8266WiFiClass::getSleepMode() { * @return bool */ bool ESP8266WiFiClass::setPhyMode(WiFiPhyMode_t mode) { - return wifi_set_phy_mode((phy_mode_t)mode); + return wifi_set_phy_mode((phy_mode_t) mode); } /** @@ -931,28 +851,26 @@ bool ESP8266WiFiClass::setPhyMode(WiFiPhyMode_t mode) { * @return phy_mode_t */ WiFiPhyMode_t ESP8266WiFiClass::getPhyMode() { - return (WiFiPhyMode_t)wifi_get_phy_mode(); + return (WiFiPhyMode_t) wifi_get_phy_mode(); } //-------------------------------------------------------------- -void ESP8266WiFiClass::_eventCallback(void* arg) -{ +void ESP8266WiFiClass::_eventCallback(void* arg) { System_Event_t* event = reinterpret_cast(arg); DEBUGV("wifi evt: %d\r\n", event->event); - if (event->event == EVENT_STAMODE_DISCONNECTED) { + if(event->event == EVENT_STAMODE_DISCONNECTED) { WiFiClient::stopAll(); } } -void ESP8266WiFiClass::printDiag(Print& p) -{ - const char* modes[] = {"NULL", "STA", "AP", "STA+AP"}; +void ESP8266WiFiClass::printDiag(Print& p) { + const char* modes[] = { "NULL", "STA", "AP", "STA+AP" }; p.print("Mode: "); p.println(modes[wifi_get_opmode()]); - const char* phymodes[] = {"", "B", "G", "N"}; + const char* phymodes[] = { "", "B", "G", "N" }; p.print("PHY mode: "); p.println(phymodes[(int) wifi_get_phy_mode()]); @@ -995,5 +913,4 @@ bool ESP8266WiFiClass::_scanComplete = false; size_t ESP8266WiFiClass::_scanCount = 0; void* ESP8266WiFiClass::_scanResult = 0; - ESP8266WiFiClass WiFi; diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.h b/libraries/ESP8266WiFi/src/ESP8266WiFi.h index 5a7fe54c1..8e65af3f7 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.h @@ -1,23 +1,23 @@ /* - ESP8266WiFi.h - esp8266 Wifi support. - Based on WiFi.h from Ardiono WiFi shield library. - Copyright (c) 2011-2014 Arduino. All right reserved. - Modified by Ivan Grokhotkov, December 2014 + ESP8266WiFi.h - esp8266 Wifi support. + Based on WiFi.h from Ardiono WiFi shield library. + Copyright (c) 2011-2014 Arduino. All right reserved. + Modified by Ivan Grokhotkov, December 2014 - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ #ifndef WiFi_h #define WiFi_h @@ -25,7 +25,7 @@ #include extern "C" { - #include "include/wl_definitions.h" +#include "include/wl_definitions.h" } #include "IPAddress.h" @@ -36,11 +36,12 @@ extern "C" { #define WIFI_SCAN_RUNNING (-1) #define WIFI_SCAN_FAILED (-2) - // Note: // this enums need to be in sync with the SDK! -enum WiFiMode { WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3 }; +enum WiFiMode { + WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3 +}; typedef enum { WIFI_PHY_MODE_11B = 1, WIFI_PHY_MODE_11G = 2, WIFI_PHY_MODE_11N = 3 @@ -50,390 +51,375 @@ typedef enum { WIFI_NONE_SLEEP = 0, WIFI_LIGHT_SLEEP = 2, WIFI_MODEM_SLEEP = 3 } WiFiSleepType_t; +class ESP8266WiFiClass { + public: -class ESP8266WiFiClass -{ -public: + ESP8266WiFiClass(); - ESP8266WiFiClass(); + void persistent(bool persistent); - void persistent(bool persistent); + void mode(WiFiMode); + WiFiMode getMode(); - void mode(WiFiMode); - WiFiMode getMode(); + /** + * Start Wifi connection + * if passphrase is set the most secure supported mode will be automatically selected + * @param ssid const char* Pointer to the SSID string. + * @param passphrase const char * Optional. Passphrase. Valid characters in a passphrase must be between ASCII 32-126 (decimal). + * @param bssid uint8_t[6] Optional. BSSID / MAC of AP + * @param channel Optional. Channel of AP + * @return + */ + int begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL); + int begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL); - /** - * Start Wifi connection - * if passphrase is set the most secure supported mode will be automatically selected - * @param ssid const char* Pointer to the SSID string. - * @param passphrase const char * Optional. Passphrase. Valid characters in a passphrase must be between ASCII 32-126 (decimal). - * @param bssid uint8_t[6] Optional. BSSID / MAC of AP - * @param channel Optional. Channel of AP - * @return - */ - int begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL); - int begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL); + // Use sdk config to connect. + int begin(); - // Use sdk config to connect. - int begin(); + /* Wait for Wifi connection to reach a result + * returns the status reached or disconnect if STA is off + */ + uint8_t waitForConnectResult(); + /* Set up an open access point + * + * param ssid: Pointer to the SSID string. + */ + void softAP(const char* ssid); - /* Wait for Wifi connection to reach a result - * returns the status reached or disconnect if STA is off - */ - uint8_t waitForConnectResult(); + /* Set up a WPA2-secured access point + * + * param ssid: Pointer to the SSID string. + * param passphrase: Pointer to passphrase, 8 characters min. + * param channel: WiFi channel number, 1 - 13. + * param ssid_hidden: Network cloaking? 0 = broadcast SSID, 1 = hide SSID + */ + void softAP(const char* ssid, const char* passphrase, int channel = 1, int ssid_hidden = 0); - /* Set up an open access point - * - * param ssid: Pointer to the SSID string. - */ - void softAP(const char* ssid); + /* Change Ip configuration settings disabling the dhcp client + * + * param local_ip: Static ip configuration + * param gateway: Static gateway configuration + * param subnet: Static Subnet mask + */ + void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet); + /* Change Ip configuration settings disabling the dhcp client + * + * param local_ip: Static ip configuration + * param gateway: Static gateway configuration + * param subnet: Static Subnet mask + * param dns: Defined DNS + */ + void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns); - /* Set up a WPA2-secured access point - * - * param ssid: Pointer to the SSID string. - * param passphrase: Pointer to passphrase, 8 characters min. - * param channel: WiFi channel number, 1 - 13. - * param ssid_hidden: Network cloaking? 0 = broadcast SSID, 1 = hide SSID - */ - void softAP(const char* ssid, const char* passphrase, int channel = 1, int ssid_hidden = 0); + /* Configure access point + * + * param local_ip: access point IP + * param gateway: gateway IP + * param subnet: subnet mask + */ + void softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet); - /* Change Ip configuration settings disabling the dhcp client - * - * param local_ip: Static ip configuration - * param gateway: Static gateway configuration - * param subnet: Static Subnet mask - */ - void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet); + /* + * Disconnect from the network (close AP) + * + * return: one value of wl_status_t enum + */ + int softAPdisconnect(bool wifioff = false); - /* Change Ip configuration settings disabling the dhcp client - * - * param local_ip: Static ip configuration - * param gateway: Static gateway configuration - * param subnet: Static Subnet mask - * param dns: Defined DNS - */ - void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns); + /* + * Disconnect from the network + * + * return: one value of wl_status_t enum + */ + int disconnect(bool wifioff = false); - /* Configure access point - * - * param local_ip: access point IP - * param gateway: gateway IP - * param subnet: subnet mask - */ - void softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet); + /* + * Get the station interface MAC address. + * + * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH + * return: String + */ + uint8_t* macAddress(uint8_t* mac); + String macAddress(void); - /* - * Disconnect from the network (close AP) - * - * return: one value of wl_status_t enum - */ - int softAPdisconnect(bool wifioff = false); + /* + * Get the softAP interface MAC address. + * + * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH + * return: String + */ + uint8_t* softAPmacAddress(uint8_t* mac); + String softAPmacAddress(void); - /* - * Disconnect from the network - * - * return: one value of wl_status_t enum - */ - int disconnect(bool wifioff = false); + /* + * Get the station interface IP address. + * + * return: Ip address value + */ + IPAddress localIP(); - /* - * Get the station interface MAC address. - * - * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH - * return: String - */ - uint8_t* macAddress(uint8_t* mac); - String macAddress(void); + /* + * Get the softAP interface IP address. + * + * return: Ip address value + */ + IPAddress softAPIP(); - /* - * Get the softAP interface MAC address. - * - * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH - * return: String - */ - uint8_t* softAPmacAddress(uint8_t* mac); - String softAPmacAddress(void); + /* + * Get the interface subnet mask address. + * + * return: subnet mask address value + */ + IPAddress subnetMask(); - /* - * Get the station interface IP address. - * - * return: Ip address value - */ - IPAddress localIP(); + /* + * Get the gateway ip address. + * + * return: gateway ip address value + */ + IPAddress gatewayIP(); - /* - * Get the softAP interface IP address. - * - * return: Ip address value - */ - IPAddress softAPIP(); + /* + * Get the DNS ip address. + * + * return: DNS ip address value + */ + IPAddress dnsIP(int dns_no = 0); - /* - * Get the interface subnet mask address. - * - * return: subnet mask address value - */ - IPAddress subnetMask(); + /* + * Return the current SSID associated with the network + * + * return: ssid string + */ + String SSID() const; - /* - * Get the gateway ip address. - * - * return: gateway ip address value - */ - IPAddress gatewayIP(); + /* + * Return the current pre shared key associated with the network + * + * return: psk string + */ + String psk() const; - /* - * Get the DNS ip address. - * - * return: DNS ip address value - */ - IPAddress dnsIP(int dns_no = 0); + /* + * Return the current bssid / mac associated with the network if configured + * + * return: bssid uint8_t * + */ + uint8_t *BSSID(void); - /* - * Return the current SSID associated with the network - * - * return: ssid string - */ - String SSID() const; + /* + * Return the current bssid / mac associated with the network if configured + * + * return: bssid string + */ + String BSSIDstr(void); - /* - * Return the current pre shared key associated with the network - * - * return: psk string - */ - String psk() const; + /* + * Return the current channel associated with the network + * + * return: channel + */ + int32_t channel(void); - /* - * Return the current bssid / mac associated with the network if configured - * - * return: bssid uint8_t * - */ - uint8_t *BSSID(void); + /* + * Return the current network RSSI. + * + * return: RSSI value + */ - /* - * Return the current bssid / mac associated with the network if configured - * - * return: bssid string - */ - String BSSIDstr(void); + int32_t RSSI(); - /* - * Return the current channel associated with the network - * - * return: channel - */ - int32_t channel(void); + /* + * called to get the scan state in Async mode + * + * return -1 if scan not fin + * return -2 if scan not triggered + */ + int8_t scanComplete(); - /* - * Return the current network RSSI. - * - * return: RSSI value - */ + /* + * delete last scan result from RAM + */ + void scanDelete(); - int32_t RSSI(); + /* + * Start scan WiFi networks available + * + * return: Number of discovered networks + */ + int8_t scanNetworks(bool async = false, bool show_hidden = false); + /* + * Return the SSID discovered during the network scan. + * + * param networkItem: specify from which network item want to get the information + * + * return: ssid string of the specified item on the networks scanned list + */ + String SSID(uint8_t networkItem); - /* - * called to get the scan state in Async mode - * - * return -1 if scan not fin - * return -2 if scan not triggered - */ - int8_t scanComplete(); + /* + * Return the encryption type of the networks discovered during the scanNetworks + * + * param networkItem: specify from which network item want to get the information + * + * return: encryption type (enum wl_enc_type) of the specified item on the networks scanned list + */ + uint8_t encryptionType(uint8_t networkItem); - /* - * delete last scan result from RAM - */ - void scanDelete(); + /* + * Return the RSSI of the networks discovered during the scanNetworks + * + * param networkItem: specify from which network item want to get the information + * + * return: signed value of RSSI of the specified item on the networks scanned list + */ + int32_t RSSI(uint8_t networkItem); - /* - * Start scan WiFi networks available - * - * return: Number of discovered networks - */ - int8_t scanNetworks(bool async = false, bool show_hidden = false); + /** + * return MAC / BSSID of scanned wifi + * @param networkItem specify from which network item want to get the information + * @return uint8_t * MAC / BSSID of scanned wifi + */ + uint8_t * BSSID(uint8_t networkItem); - /* - * Return the SSID discovered during the network scan. - * - * param networkItem: specify from which network item want to get the information - * - * return: ssid string of the specified item on the networks scanned list - */ - String SSID(uint8_t networkItem); + /** + * return MAC / BSSID of scanned wifi + * @param networkItem specify from which network item want to get the information + * @return String MAC / BSSID of scanned wifi + */ + String BSSIDstr(uint8_t networkItem); - /* - * Return the encryption type of the networks discovered during the scanNetworks - * - * param networkItem: specify from which network item want to get the information - * - * return: encryption type (enum wl_enc_type) of the specified item on the networks scanned list - */ - uint8_t encryptionType(uint8_t networkItem); + /** + * return channel of scanned wifi + * @param networkItem specify from which network item want to get the information + * @return uint32_t channel of scanned wifi + */ + int32_t channel(uint8_t networkItem); - /* - * Return the RSSI of the networks discovered during the scanNetworks - * - * param networkItem: specify from which network item want to get the information - * - * return: signed value of RSSI of the specified item on the networks scanned list - */ - int32_t RSSI(uint8_t networkItem); + /** + * return if the scanned wifi is Hidden (no SSID) + * @param networkItem specify from which network item want to get the information + * @return bool (true == hidden) + */ + bool isHidden(uint8_t networkItem); + /** + * loads all infos from a scanned wifi in to the ptr parameters + * @param networkItem uint8_t + * @param ssid const char** + * @param encryptionType uint8_t * + * @param RSSI int32_t * + * @param BSSID uint8_t ** + * @param channel int32_t * + * @param isHidden bool * + * @return (true if ok) + */ + bool getNetworkInfo(uint8_t networkItem, String &ssid, uint8_t &encryptionType, int32_t &RSSI, uint8_t* &BSSID, int32_t &channel, bool &isHidden); - /** - * return MAC / BSSID of scanned wifi - * @param networkItem specify from which network item want to get the information - * @return uint8_t * MAC / BSSID of scanned wifi - */ - uint8_t * BSSID(uint8_t networkItem); + /* + * Return Connection status. + * + * return: one of the value defined in wl_status_t + */ + wl_status_t status(); - /** - * return MAC / BSSID of scanned wifi - * @param networkItem specify from which network item want to get the information - * @return String MAC / BSSID of scanned wifi - */ - String BSSIDstr(uint8_t networkItem); + /* + * Resolve the given hostname to an IP address. + * param aHostname: Name to be resolved + * param aResult: IPAddress structure to store the returned IP address + * result: 1 if aIPAddrString was successfully converted to an IP address, + * else error code + */ + int hostByName(const char* aHostname, IPAddress& aResult); - /** - * return channel of scanned wifi - * @param networkItem specify from which network item want to get the information - * @return uint32_t channel of scanned wifi - */ - int32_t channel(uint8_t networkItem); + /* + * Get ESP8266 station DHCP hostname + */ + String hostname(void); - /** - * return if the scanned wifi is Hidden (no SSID) - * @param networkItem specify from which network item want to get the information - * @return bool (true == hidden) - */ - bool isHidden(uint8_t networkItem); + /* + * Set ESP8266 station DHCP hostname + * hostname, max length:32 + */ + bool hostname(char* aHostname);bool hostname(const char* aHostname);bool hostname(String aHostname); - /** - * loads all infos from a scanned wifi in to the ptr parameters - * @param networkItem uint8_t - * @param ssid const char** - * @param encryptionType uint8_t * - * @param RSSI int32_t * - * @param BSSID uint8_t ** - * @param channel int32_t * - * @param isHidden bool * - * @return (true if ok) - */ - bool getNetworkInfo(uint8_t networkItem, String &ssid, uint8_t &encryptionType, int32_t &RSSI, uint8_t* &BSSID, int32_t &channel, bool &isHidden); + /** + * WPS config + * so far only WPS_TYPE_PBC is supported (SDK 1.2.0) + */ + bool beginWPSConfig(void); + /* + * Output WiFi settings to an object derived from Print interface (like Serial). + * + */ + void printDiag(Print& dest); - /* - * Return Connection status. - * - * return: one of the value defined in wl_status_t - */ - wl_status_t status(); + /* + * Start SmartConfig + * + */ + void beginSmartConfig(); - /* - * Resolve the given hostname to an IP address. - * param aHostname: Name to be resolved - * param aResult: IPAddress structure to store the returned IP address - * result: 1 if aIPAddrString was successfully converted to an IP address, - * else error code - */ - int hostByName(const char* aHostname, IPAddress& aResult); + /* + * Query SmartConfig status, to decide when stop config + * + */ + bool smartConfigDone(); - /* - * Get ESP8266 station DHCP hostname - */ - String hostname(void); + /* + * Stop SmartConfig + * + */ + void stopSmartConfig(); - /* - * Set ESP8266 station DHCP hostname - * hostname, max length:32 - */ - bool hostname(char* aHostname); - bool hostname(const char* aHostname); - bool hostname(String aHostname); + friend class WiFiClient; + friend class WiFiServer; + /** + * set Sleep mode + * @param type WiFiPhyMode_t + * @return bool + */ + bool setSleepMode(WiFiSleepType_t type); - /** - * WPS config - * so far only WPS_TYPE_PBC is supported (SDK 1.2.0) - */ - bool beginWPSConfig(void); + /** + * get Sleep mode + * @return sleep_type_t + */ + WiFiSleepType_t getSleepMode(); - /* - * Output WiFi settings to an object derived from Print interface (like Serial). - * - */ - void printDiag(Print& dest); + /** + * set phy Mode + * @param mode phy_mode_t + * @return bool + */ + bool setPhyMode(WiFiPhyMode_t mode); - /* - * Start SmartConfig - * - */ - void beginSmartConfig(); + /** + * get phy Mode + * @return phy_mode_t + */ + WiFiPhyMode_t getPhyMode(); - /* - * Query SmartConfig status, to decide when stop config - * - */ - bool smartConfigDone(); + protected: + void _mode(WiFiMode); + static void _scanDone(void* result, int status); + void * _getScanInfoByIndex(int i); + static void _smartConfigCallback(uint32_t status, void* result); + static void _eventCallback(void *event);bool _smartConfigStarted;bool _smartConfigDone; - /* - * Stop SmartConfig - * - */ - void stopSmartConfig(); + bool _useApMode;bool _useClientMode;bool _useStaticIp;bool _persistent; - friend class WiFiClient; - friend class WiFiServer; + static bool _scanAsync; + static bool _scanStarted; + static bool _scanComplete; - /** - * set Sleep mode - * @param type WiFiPhyMode_t - * @return bool - */ - bool setSleepMode(WiFiSleepType_t type); - - /** - * get Sleep mode - * @return sleep_type_t - */ - WiFiSleepType_t getSleepMode(); - - /** - * set phy Mode - * @param mode phy_mode_t - * @return bool - */ - bool setPhyMode(WiFiPhyMode_t mode); - - /** - * get phy Mode - * @return phy_mode_t - */ - WiFiPhyMode_t getPhyMode(); - -protected: - void _mode(WiFiMode); - static void _scanDone(void* result, int status); - void * _getScanInfoByIndex(int i); - static void _smartConfigCallback(uint32_t status, void* result); - static void _eventCallback(void *event); - bool _smartConfigStarted; - bool _smartConfigDone; - - bool _useApMode; - bool _useClientMode; - bool _useStaticIp; - bool _persistent; - - static bool _scanAsync; - static bool _scanStarted; - static bool _scanComplete; - - static size_t _scanCount; - static void* _scanResult; + static size_t _scanCount; + static void* _scanResult; }; From b1b19299bbe5f508f88872cc57b6ecff25b22245 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Mon, 28 Dec 2015 17:02:09 +0100 Subject: [PATCH 02/21] mode docu to cpp and make it doxygen compatible --- libraries/ESP8266WiFi/src/ESP8266WiFi.cpp | 241 +++++++++++++++++- libraries/ESP8266WiFi/src/ESP8266WiFi.h | 285 ++-------------------- 2 files changed, 253 insertions(+), 273 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp index b946d5bdf..3b85344b7 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp @@ -112,6 +112,15 @@ static bool sta_config_equal(const station_config& lhs, const station_config& rh return true; } +/** + * Start Wifi connection + * if passphrase is set the most secure supported mode will be automatically selected + * @param ssid const char* Pointer to the SSID string. + * @param passphrase const char * Optional. Passphrase. Valid characters in a passphrase must be between ASCII 32-126 (decimal). + * @param bssid uint8_t[6] Optional. BSSID / MAC of AP + * @param channel Optional. Channel of AP + * @return + */ int ESP8266WiFiClass::begin(char* ssid, char *passphrase, int32_t channel, const uint8_t* bssid) { return begin((const char*) ssid, (const char*) passphrase, channel, bssid); } @@ -177,16 +186,26 @@ int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t ch return status(); } +/** + * Use to connect to SDK config. + * @return wl_status_t + */ int ESP8266WiFiClass::begin() { ETS_UART_INTR_DISABLE(); wifi_station_connect(); ETS_UART_INTR_ENABLE(); + // TODO is static ip not stored in SDK? if(!_useStaticIp) wifi_station_dhcpc_start(); return status(); } +/** + * Wait for WiFi connection to reach a result + * returns the status reached or disconnect if STA is off + * @return wl_status_t + */ uint8_t ESP8266WiFiClass::waitForConnectResult() { if((wifi_get_opmode() & 1) == 0) //1 and 3 have STA enabled return WL_DISCONNECTED; @@ -195,7 +214,12 @@ uint8_t ESP8266WiFiClass::waitForConnectResult() { return status(); } -// You will have to set the DNS-Server manually later since this will not enable DHCP2 +/** + * Change IP configuration settings disabling the dhcp client + * @param local_ip Static ip configuration + * @param gateway Static gateway configuration + * @param subnet Static Subnet mask + */ void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { struct ip_info info; info.ip.addr = static_cast(local_ip); @@ -208,6 +232,13 @@ void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress s _useStaticIp = true; } +/** + * Change IP configuration settings disabling the dhcp client + * @param local_ip Static ip configuration + * @param gateway Static gateway configuration + * @param subnet Static Subnet mask + * @param dns Static DNS server + */ void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns) { struct ip_info info; info.ip.addr = static_cast(local_ip); @@ -225,6 +256,11 @@ void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress s _useStaticIp = true; } +/** + * Disconnect from the network (close AP) + * @param wifioff disable mode? + * @return one value of wl_status_t enum + */ int ESP8266WiFiClass::softAPdisconnect(bool wifioff) { struct softap_config conf; *conf.ssid = 0; @@ -248,9 +284,15 @@ int ESP8266WiFiClass::softAPdisconnect(bool wifioff) { } } + //TODO return with more meaning ? return 0; } +/** + * Disconnect from the network + * @param wifioff + * @return one value of wl_status_t enum + */ int ESP8266WiFiClass::disconnect(bool wifioff) { struct station_config conf; *conf.ssid = 0; @@ -274,7 +316,7 @@ int ESP8266WiFiClass::disconnect(bool wifioff) { _mode(WIFI_OFF); } } - + //TODO return with more meaning ? return 0; } @@ -290,10 +332,13 @@ static bool softap_config_equal(const softap_config& lhs, const softap_config& r return true; } -void ESP8266WiFiClass::softAP(const char* ssid) { - softAP(ssid, 0); -} - +/** + * 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) + */ void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden) { _useApMode = true; if(_useClientMode) { @@ -346,6 +391,12 @@ void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase, int chan ETS_UART_INTR_ENABLE(); } +/** + * Configure access point + * @param local_ip access point IP + * @param gateway gateway IP + * @param subnet subnet mask + */ void ESP8266WiFiClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { struct ip_info info; info.ip.addr = static_cast(local_ip); @@ -356,11 +407,20 @@ void ESP8266WiFiClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAdd wifi_softap_dhcps_start(); } +/** + * Get the station interface MAC address. + * @param mac pointer to uint8_t array with length WL_MAC_ADDR_LENGTH + * @return pointer to uint8_t * + */ uint8_t* ESP8266WiFiClass::macAddress(uint8_t* mac) { wifi_get_macaddr(STATION_IF, mac); return mac; } +/** + * Get the station interface MAC address. + * @return String mac + */ String ESP8266WiFiClass::macAddress(void) { uint8_t mac[6]; char macStr[18] = { 0 }; @@ -370,11 +430,21 @@ String ESP8266WiFiClass::macAddress(void) { return String(macStr); } + +/** + * Get the softAP interface MAC address. + * @param mac pointer to uint8_t array with length WL_MAC_ADDR_LENGTH + * @return pointer to uint8_t* + */ uint8_t* ESP8266WiFiClass::softAPmacAddress(uint8_t* mac) { wifi_get_macaddr(SOFTAP_IF, mac); return mac; } +/** + * Get the softAP interface MAC address. + * @return String mac + */ String ESP8266WiFiClass::softAPmacAddress(void) { uint8_t mac[6]; char macStr[18] = { 0 }; @@ -384,53 +454,93 @@ String ESP8266WiFiClass::softAPmacAddress(void) { return String(macStr); } +/** + * Get the station interface IP address. + * @return IPAddress station IP + */ IPAddress ESP8266WiFiClass::localIP() { struct ip_info ip; wifi_get_ip_info(STATION_IF, &ip); return IPAddress(ip.ip.addr); } +/** + * Get the softAP interface IP address. + * @return IPAddress softAP IP + */ IPAddress ESP8266WiFiClass::softAPIP() { struct ip_info ip; wifi_get_ip_info(SOFTAP_IF, &ip); return IPAddress(ip.ip.addr); } +/** + * Get the interface subnet mask address. + * @return IPAddress subnetMask + */ IPAddress ESP8266WiFiClass::subnetMask() { struct ip_info ip; wifi_get_ip_info(STATION_IF, &ip); return IPAddress(ip.netmask.addr); } +/** + * Get the gateway ip address. + * @return IPAddress gatewayIP + */ IPAddress ESP8266WiFiClass::gatewayIP() { struct ip_info ip; wifi_get_ip_info(STATION_IF, &ip); return IPAddress(ip.gw.addr); } -IPAddress ESP8266WiFiClass::dnsIP(int dns_no) { +/** + * Get the DNS ip address. + * @param dns_no + * @return IPAddress DNS Server IP + */ +IPAddress ESP8266WiFiClass::dnsIP(uint8_t dns_no) { ip_addr_t dns_ip = dns_getserver(dns_no); return IPAddress(dns_ip.addr); } + +/** + * Return the current SSID associated with the network + * @return SSID + */ String ESP8266WiFiClass::SSID() const { + // TODO why static, needs RAM for nothing? static struct station_config conf; wifi_station_get_config(&conf); return String(reinterpret_cast(conf.ssid)); } +/** + * Return the current pre shared key associated with the network + * @return psk string + */ String ESP8266WiFiClass::psk() const { static struct station_config conf; wifi_station_get_config(&conf); return String(reinterpret_cast(conf.password)); } + +/** + * Return the current bssid / mac associated with the network if configured + * @return bssid uint8_t * + */ uint8_t* ESP8266WiFiClass::BSSID(void) { static struct station_config conf; wifi_station_get_config(&conf); return reinterpret_cast(conf.bssid); } +/** + * Return the current bssid / mac associated with the network if configured + * @return String bssid mac + */ String ESP8266WiFiClass::BSSIDstr(void) { static struct station_config conf; char mac[18] = { 0 }; @@ -439,10 +549,18 @@ String ESP8266WiFiClass::BSSIDstr(void) { return String(mac); } +/** + * Return the current channel associated with the network + * @return channel (1-13) + */ int32_t ESP8266WiFiClass::channel(void) { return wifi_get_channel(); } +/** + * Return the current network RSSI. + * @return RSSI value + */ int32_t ESP8266WiFiClass::RSSI(void) { return wifi_station_get_rssi(); } @@ -486,6 +604,12 @@ void ESP8266WiFiClass::_scanDone(void* result, int status) { } } +/** + * called to get the scan state in Async mode + * @return scan result or status + * -1 if scan not fin + * -2 if scan not triggered + */ int8_t ESP8266WiFiClass::scanComplete() { if(_scanStarted) { @@ -499,6 +623,9 @@ int8_t ESP8266WiFiClass::scanComplete() { return WIFI_SCAN_FAILED; } +/** + * delete last scan result from RAM + */ void ESP8266WiFiClass::scanDelete() { if(ESP8266WiFiClass::_scanResult) { delete[] reinterpret_cast(ESP8266WiFiClass::_scanResult); @@ -508,6 +635,12 @@ void ESP8266WiFiClass::scanDelete() { _scanComplete = false; } +/** + * Start scan WiFi networks available + * @param async run in async mode + * @param show_hidden show hidden networks + * @return Number of discovered networks + */ int8_t ESP8266WiFiClass::scanNetworks(bool async, bool show_hidden) { if(ESP8266WiFiClass::_scanStarted) { return WIFI_SCAN_RUNNING; @@ -560,6 +693,11 @@ void * ESP8266WiFiClass::_getScanInfoByIndex(int i) { return reinterpret_cast(ESP8266WiFiClass::_scanResult) + i; } +/** + * Return the SSID discovered during the network scan. + * @param i specify from which network item want to get the information + * @return ssid string of the specified item on the networks scanned list + */ String ESP8266WiFiClass::SSID(uint8_t i) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); if(!it) @@ -568,6 +706,11 @@ String ESP8266WiFiClass::SSID(uint8_t i) { return String(reinterpret_cast(it->ssid)); } +/** + * return MAC / BSSID of scanned wifi + * @param i specify from which network item want to get the information + * @return uint8_t * MAC / BSSID of scanned wifi + */ uint8_t * ESP8266WiFiClass::BSSID(uint8_t i) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); if(!it) @@ -576,6 +719,11 @@ uint8_t * ESP8266WiFiClass::BSSID(uint8_t i) { return it->bssid; } +/** + * return MAC / BSSID of scanned wifi + * @param networkItem specify from which network item want to get the information + * @return String MAC / BSSID of scanned wifi + */ String ESP8266WiFiClass::BSSIDstr(uint8_t i) { char mac[18] = { 0 }; struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); @@ -594,6 +742,11 @@ int32_t ESP8266WiFiClass::channel(uint8_t i) { return it->channel; } +/** + * return if the scanned wifi is Hidden (no SSID) + * @param networkItem specify from which network item want to get the information + * @return bool (true == hidden) + */ bool ESP8266WiFiClass::isHidden(uint8_t i) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); if(!it) @@ -602,6 +755,17 @@ bool ESP8266WiFiClass::isHidden(uint8_t i) { return (it->is_hidden != 0); } +/** + * loads all infos from a scanned wifi in to the ptr parameters + * @param networkItem uint8_t + * @param ssid const char** + * @param encryptionType uint8_t * + * @param RSSI int32_t * + * @param BSSID uint8_t ** + * @param channel int32_t * + * @param isHidden bool * + * @return (true if ok) + */ bool ESP8266WiFiClass::getNetworkInfo(uint8_t i, String &ssid, uint8_t &encType, int32_t &rssi, uint8_t* &bssid, int32_t &channel, bool &isHidden) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); if(!it) @@ -617,6 +781,11 @@ bool ESP8266WiFiClass::getNetworkInfo(uint8_t i, String &ssid, uint8_t &encType, return true; } +/** + * Return the RSSI of the networks discovered during the scanNetworks + * @param i specify from which network item want to get the information + * @return signed value of RSSI of the specified item on the networks scanned list + */ int32_t ESP8266WiFiClass::RSSI(uint8_t i) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); if(!it) @@ -625,6 +794,11 @@ int32_t ESP8266WiFiClass::RSSI(uint8_t i) { return it->rssi; } +/** + * Return the encryption type of the networks discovered during the scanNetworks + * @param i specify from which network item want to get the information + * @return encryption type (enum wl_enc_type) of the specified item on the networks scanned list + */ uint8_t ESP8266WiFiClass::encryptionType(uint8_t i) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); if(!it) @@ -644,6 +818,11 @@ uint8_t ESP8266WiFiClass::encryptionType(uint8_t i) { return -1; } +/** + * Return Connection status. + * @return one of the value defined in wl_status_t + * + */ wl_status_t ESP8266WiFiClass::status() { int status = wifi_station_get_connect_status(); @@ -665,6 +844,13 @@ void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback esp_schedule(); // resume the hostByName function } +/** + * Resolve the given hostname to an IP address. + * @param aHostname Name to be resolved + * @param aResult IPAddress structure to store the returned IP address + * @return 1 if aIPAddrString was successfully converted to an IP address, + * else error code + */ int ESP8266WiFiClass::hostByName(const char* aHostname, IPAddress& aResult) { ip_addr_t addr; aResult = static_cast(0); @@ -679,10 +865,20 @@ int ESP8266WiFiClass::hostByName(const char* aHostname, IPAddress& aResult) { return (aResult != 0) ? 1 : 0; } +/** + * Get ESP8266 station DHCP hostname + * @return hostname + */ String ESP8266WiFiClass::hostname(void) { return String(wifi_station_get_hostname()); } + +/** + * Set ESP8266 station DHCP hostname + * @param aHostname max length:32 + * @return ok + */ bool ESP8266WiFiClass::hostname(char* aHostname) { if(strlen(aHostname) > 32) { return false; @@ -690,10 +886,20 @@ bool ESP8266WiFiClass::hostname(char* aHostname) { return wifi_station_set_hostname(aHostname); } +/** + * Set ESP8266 station DHCP hostname + * @param aHostname max length:32 + * @return ok + */ bool ESP8266WiFiClass::hostname(const char* aHostname) { return hostname((char*) aHostname); } +/** + * Set ESP8266 station DHCP hostname + * @param aHostname max length:32 + * @return ok + */ bool ESP8266WiFiClass::hostname(String aHostname) { return hostname((char*) aHostname.c_str()); } @@ -724,6 +930,11 @@ void wifi_wps_status_cb(wps_cb_status status) { esp_schedule(); // resume the beginWPSConfig function } +/** + * WPS config + * so far only WPS_TYPE_PBC is supported (SDK 1.2.0) + * @return ok + */ bool ESP8266WiFiClass::beginWPSConfig(void) { _useClientMode = true; @@ -769,6 +980,9 @@ bool ESP8266WiFiClass::beginWPSConfig(void) { //-------------------------------------------------------------- +/** + * Start SmartConfig + */ void ESP8266WiFiClass::beginSmartConfig() { if(_smartConfigStarted) return; @@ -788,6 +1002,10 @@ void ESP8266WiFiClass::beginSmartConfig() { smartconfig_start(reinterpret_cast(&ESP8266WiFiClass::_smartConfigCallback), 1); } + +/** + * Stop SmartConfig + */ void ESP8266WiFiClass::stopSmartConfig() { if(!_smartConfigStarted) return; @@ -796,6 +1014,10 @@ void ESP8266WiFiClass::stopSmartConfig() { _smartConfigStarted = false; } +/** + * Query SmartConfig status, to decide when stop config + * @return smartConfig Done + */ bool ESP8266WiFiClass::smartConfigDone() { if(!_smartConfigStarted) return false; @@ -865,6 +1087,11 @@ void ESP8266WiFiClass::_eventCallback(void* arg) { } } + +/** + * Output WiFi settings to an object derived from Print interface (like Serial). + * @param p Print interface + */ void ESP8266WiFiClass::printDiag(Print& p) { const char* modes[] = { "NULL", "STA", "AP", "STA+AP" }; p.print("Mode: "); diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.h b/libraries/ESP8266WiFi/src/ESP8266WiFi.h index 8e65af3f7..c3915a932 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.h @@ -61,348 +61,101 @@ class ESP8266WiFiClass { void mode(WiFiMode); WiFiMode getMode(); - /** - * Start Wifi connection - * if passphrase is set the most secure supported mode will be automatically selected - * @param ssid const char* Pointer to the SSID string. - * @param passphrase const char * Optional. Passphrase. Valid characters in a passphrase must be between ASCII 32-126 (decimal). - * @param bssid uint8_t[6] Optional. BSSID / MAC of AP - * @param channel Optional. Channel of AP - * @return - */ int begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL); int begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL); - - // Use sdk config to connect. int begin(); - /* Wait for Wifi connection to reach a result - * returns the status reached or disconnect if STA is off - */ uint8_t waitForConnectResult(); - /* Set up an open access point - * - * param ssid: Pointer to the SSID string. - */ - void softAP(const char* ssid); + void softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0); - /* Set up a WPA2-secured access point - * - * param ssid: Pointer to the SSID string. - * param passphrase: Pointer to passphrase, 8 characters min. - * param channel: WiFi channel number, 1 - 13. - * param ssid_hidden: Network cloaking? 0 = broadcast SSID, 1 = hide SSID - */ - void softAP(const char* ssid, const char* passphrase, int channel = 1, int ssid_hidden = 0); - /* Change Ip configuration settings disabling the dhcp client - * - * param local_ip: Static ip configuration - * param gateway: Static gateway configuration - * param subnet: Static Subnet mask - */ void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet); - - /* Change Ip configuration settings disabling the dhcp client - * - * param local_ip: Static ip configuration - * param gateway: Static gateway configuration - * param subnet: Static Subnet mask - * param dns: Defined DNS - */ void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns); - /* Configure access point - * - * param local_ip: access point IP - * param gateway: gateway IP - * param subnet: subnet mask - */ void softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet); - /* - * Disconnect from the network (close AP) - * - * return: one value of wl_status_t enum - */ - int softAPdisconnect(bool wifioff = false); - /* - * Disconnect from the network - * - * return: one value of wl_status_t enum - */ + int softAPdisconnect(bool wifioff = false); int disconnect(bool wifioff = false); - /* - * Get the station interface MAC address. - * - * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH - * return: String - */ + uint8_t* macAddress(uint8_t* mac); String macAddress(void); - /* - * Get the softAP interface MAC address. - * - * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH - * return: String - */ uint8_t* softAPmacAddress(uint8_t* mac); String softAPmacAddress(void); - /* - * Get the station interface IP address. - * - * return: Ip address value - */ - IPAddress localIP(); - /* - * Get the softAP interface IP address. - * - * return: Ip address value - */ + IPAddress localIP(); IPAddress softAPIP(); - /* - * Get the interface subnet mask address. - * - * return: subnet mask address value - */ + IPAddress subnetMask(); - - /* - * Get the gateway ip address. - * - * return: gateway ip address value - */ IPAddress gatewayIP(); + IPAddress dnsIP(uint8_t dns_no = 0); - /* - * Get the DNS ip address. - * - * return: DNS ip address value - */ - IPAddress dnsIP(int dns_no = 0); - /* - * Return the current SSID associated with the network - * - * return: ssid string - */ String SSID() const; - - /* - * Return the current pre shared key associated with the network - * - * return: psk string - */ String psk() const; - /* - * Return the current bssid / mac associated with the network if configured - * - * return: bssid uint8_t * - */ + uint8_t *BSSID(void); - - /* - * Return the current bssid / mac associated with the network if configured - * - * return: bssid string - */ String BSSIDstr(void); - - /* - * Return the current channel associated with the network - * - * return: channel - */ int32_t channel(void); - /* - * Return the current network RSSI. - * - * return: RSSI value - */ int32_t RSSI(); - /* - * called to get the scan state in Async mode - * - * return -1 if scan not fin - * return -2 if scan not triggered - */ - int8_t scanComplete(); - /* - * delete last scan result from RAM - */ + int8_t scanComplete(); void scanDelete(); - /* - * Start scan WiFi networks available - * - * return: Number of discovered networks - */ + int8_t scanNetworks(bool async = false, bool show_hidden = false); - /* - * Return the SSID discovered during the network scan. - * - * param networkItem: specify from which network item want to get the information - * - * return: ssid string of the specified item on the networks scanned list - */ + String SSID(uint8_t networkItem); - - /* - * Return the encryption type of the networks discovered during the scanNetworks - * - * param networkItem: specify from which network item want to get the information - * - * return: encryption type (enum wl_enc_type) of the specified item on the networks scanned list - */ uint8_t encryptionType(uint8_t networkItem); - - /* - * Return the RSSI of the networks discovered during the scanNetworks - * - * param networkItem: specify from which network item want to get the information - * - * return: signed value of RSSI of the specified item on the networks scanned list - */ int32_t RSSI(uint8_t networkItem); - - /** - * return MAC / BSSID of scanned wifi - * @param networkItem specify from which network item want to get the information - * @return uint8_t * MAC / BSSID of scanned wifi - */ uint8_t * BSSID(uint8_t networkItem); - /** - * return MAC / BSSID of scanned wifi - * @param networkItem specify from which network item want to get the information - * @return String MAC / BSSID of scanned wifi - */ + String BSSIDstr(uint8_t networkItem); - - /** - * return channel of scanned wifi - * @param networkItem specify from which network item want to get the information - * @return uint32_t channel of scanned wifi - */ int32_t channel(uint8_t networkItem); - - /** - * return if the scanned wifi is Hidden (no SSID) - * @param networkItem specify from which network item want to get the information - * @return bool (true == hidden) - */ bool isHidden(uint8_t networkItem); - - /** - * loads all infos from a scanned wifi in to the ptr parameters - * @param networkItem uint8_t - * @param ssid const char** - * @param encryptionType uint8_t * - * @param RSSI int32_t * - * @param BSSID uint8_t ** - * @param channel int32_t * - * @param isHidden bool * - * @return (true if ok) - */ bool getNetworkInfo(uint8_t networkItem, String &ssid, uint8_t &encryptionType, int32_t &RSSI, uint8_t* &BSSID, int32_t &channel, bool &isHidden); - /* - * Return Connection status. - * - * return: one of the value defined in wl_status_t - */ + wl_status_t status(); - /* - * Resolve the given hostname to an IP address. - * param aHostname: Name to be resolved - * param aResult: IPAddress structure to store the returned IP address - * result: 1 if aIPAddrString was successfully converted to an IP address, - * else error code - */ + int hostByName(const char* aHostname, IPAddress& aResult); - /* - * Get ESP8266 station DHCP hostname - */ + String hostname(void); + bool hostname(char* aHostname); + bool hostname(const char* aHostname); + bool hostname(String aHostname); - /* - * Set ESP8266 station DHCP hostname - * hostname, max length:32 - */ - bool hostname(char* aHostname);bool hostname(const char* aHostname);bool hostname(String aHostname); - /** - * WPS config - * so far only WPS_TYPE_PBC is supported (SDK 1.2.0) - */ bool beginWPSConfig(void); - /* - * Output WiFi settings to an object derived from Print interface (like Serial). - * - */ void printDiag(Print& dest); - /* - * Start SmartConfig - * - */ + void beginSmartConfig(); - - /* - * Query SmartConfig status, to decide when stop config - * - */ bool smartConfigDone(); - - /* - * Stop SmartConfig - * - */ void stopSmartConfig(); friend class WiFiClient; friend class WiFiServer; - /** - * set Sleep mode - * @param type WiFiPhyMode_t - * @return bool - */ - bool setSleepMode(WiFiSleepType_t type); - /** - * get Sleep mode - * @return sleep_type_t - */ + bool setSleepMode(WiFiSleepType_t type); WiFiSleepType_t getSleepMode(); - /** - * set phy Mode - * @param mode phy_mode_t - * @return bool - */ bool setPhyMode(WiFiPhyMode_t mode); - - /** - * get phy Mode - * @return phy_mode_t - */ WiFiPhyMode_t getPhyMode(); protected: From 49b0821beb52fc3d6cdf180d5a6fcd71089c2d5e Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Mon, 28 Dec 2015 17:22:36 +0100 Subject: [PATCH 03/21] reorder part one --- libraries/ESP8266WiFi/src/ESP8266WiFi.cpp | 1863 +++++++++++---------- libraries/ESP8266WiFi/src/ESP8266WiFi.h | 138 +- 2 files changed, 1066 insertions(+), 935 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp index 3b85344b7..65dc97564 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp @@ -48,6 +48,996 @@ ESP8266WiFiClass::ESP8266WiFiClass() : _useApMode = (m & WIFI_AP); wifi_set_event_handler_cb((wifi_event_handler_cb_t) &ESP8266WiFiClass::_eventCallback); } + +// ----------------------------------------------------------------------------------------------------------------------- +// ---------------------------------------------------- STA function ----------------------------------------------------- +// ----------------------------------------------------------------------------------------------------------------------- + +/** + * Start Wifi connection + * if passphrase is set the most secure supported mode will be automatically selected + * @param ssid const char* Pointer to the SSID string. + * @param passphrase const char * Optional. Passphrase. Valid characters in a passphrase must be between ASCII 32-126 (decimal). + * @param bssid uint8_t[6] Optional. BSSID / MAC of AP + * @param channel Optional. Channel of AP + * @return + */ +int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid) { + _useClientMode = true; + + if(_useApMode) { + // turn on AP+STA mode + _mode(WIFI_AP_STA); + } else { + // turn on STA mode + _mode(WIFI_STA); + } + + if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) { + // fail SSID too long or missing! + return WL_CONNECT_FAILED; + } + + if(passphrase && strlen(passphrase) > 63) { + // fail passphrase too long! + return WL_CONNECT_FAILED; + } + + struct station_config conf; + strcpy(reinterpret_cast(conf.ssid), ssid); + + if(passphrase) { + strcpy(reinterpret_cast(conf.password), passphrase); + } else { + *conf.password = 0; + } + + if(bssid) { + conf.bssid_set = 1; + memcpy((void *) &conf.bssid[0], (void *) bssid, 6); + } else { + conf.bssid_set = 0; + } + + struct station_config current_conf; + wifi_station_get_config(¤t_conf); + if(sta_config_equal(current_conf, conf)) { + DEBUGV("sta config unchanged"); + return status(); + } + + ETS_UART_INTR_DISABLE(); + if(_persistent) + wifi_station_set_config(&conf); + else + wifi_station_set_config_current(&conf); + wifi_station_connect(); + ETS_UART_INTR_ENABLE(); + + if(channel > 0 && channel <= 13) { + wifi_set_channel(channel); + } + + if(!_useStaticIp) + wifi_station_dhcpc_start(); + return status(); +} + +int ESP8266WiFiClass::begin(char* ssid, char *passphrase, int32_t channel, const uint8_t* bssid) { + return begin((const char*) ssid, (const char*) passphrase, channel, bssid); +} + +/** + * Use to connect to SDK config. + * @return wl_status_t + */ +int ESP8266WiFiClass::begin() { + ETS_UART_INTR_DISABLE(); + wifi_station_connect(); + ETS_UART_INTR_ENABLE(); + + // TODO is static ip not stored in SDK? + if(!_useStaticIp) + wifi_station_dhcpc_start(); + return status(); +} + + +/** + * Change IP configuration settings disabling the dhcp client + * @param local_ip Static ip configuration + * @param gateway Static gateway configuration + * @param subnet Static Subnet mask + */ +void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { + struct ip_info info; + info.ip.addr = static_cast(local_ip); + info.gw.addr = static_cast(gateway); + info.netmask.addr = static_cast(subnet); + + wifi_station_dhcpc_stop(); + wifi_set_ip_info(STATION_IF, &info); + + _useStaticIp = true; +} + +/** + * Change IP configuration settings disabling the dhcp client + * @param local_ip Static ip configuration + * @param gateway Static gateway configuration + * @param subnet Static Subnet mask + * @param dns Static DNS server + */ +void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns) { + struct ip_info info; + info.ip.addr = static_cast(local_ip); + info.gw.addr = static_cast(gateway); + info.netmask.addr = static_cast(subnet); + + wifi_station_dhcpc_stop(); + wifi_set_ip_info(STATION_IF, &info); + + // Set DNS-Server + ip_addr_t d; + d.addr = static_cast(dns); + dns_setserver(0, &d); + + _useStaticIp = true; +} + + +/** + * Disconnect from the network + * @param wifioff + * @return one value of wl_status_t enum + */ +int ESP8266WiFiClass::disconnect(bool wifioff) { + struct station_config conf; + *conf.ssid = 0; + *conf.password = 0; + ETS_UART_INTR_DISABLE(); + if(_persistent) + wifi_station_set_config(&conf); + else + wifi_station_set_config_current(&conf); + wifi_station_disconnect(); + ETS_UART_INTR_ENABLE(); + + if(wifioff) { + _useClientMode = false; + + if(_useApMode) { + // turn on AP + _mode(WIFI_AP); + } else { + // turn wifi off + _mode(WIFI_OFF); + } + } + //TODO return with more meaning ? + return 0; +} + +/** + * Wait for WiFi connection to reach a result + * returns the status reached or disconnect if STA is off + * @return wl_status_t + */ +uint8_t ESP8266WiFiClass::waitForConnectResult() { + //1 and 3 have STA enabled + if((wifi_get_opmode() & 1) == 0) { + return WL_DISCONNECTED; + } + while(status() == WL_DISCONNECTED) { + delay(100); + } + return status(); +} + +/** + * Get the station interface IP address. + * @return IPAddress station IP + */ +IPAddress ESP8266WiFiClass::localIP() { + struct ip_info ip; + wifi_get_ip_info(STATION_IF, &ip); + return IPAddress(ip.ip.addr); +} + + +/** + * Get the station interface MAC address. + * @param mac pointer to uint8_t array with length WL_MAC_ADDR_LENGTH + * @return pointer to uint8_t * + */ +uint8_t* ESP8266WiFiClass::macAddress(uint8_t* mac) { + wifi_get_macaddr(STATION_IF, mac); + return mac; +} + +/** + * Get the station interface MAC address. + * @return String mac + */ +String ESP8266WiFiClass::macAddress(void) { + uint8_t mac[6]; + char macStr[18] = { 0 }; + wifi_get_macaddr(STATION_IF, mac); + + sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + return String(macStr); +} + +/** + * Get the interface subnet mask address. + * @return IPAddress subnetMask + */ +IPAddress ESP8266WiFiClass::subnetMask() { + struct ip_info ip; + wifi_get_ip_info(STATION_IF, &ip); + return IPAddress(ip.netmask.addr); +} + +/** + * Get the gateway ip address. + * @return IPAddress gatewayIP + */ +IPAddress ESP8266WiFiClass::gatewayIP() { + struct ip_info ip; + wifi_get_ip_info(STATION_IF, &ip); + return IPAddress(ip.gw.addr); +} + +/** + * Get the DNS ip address. + * @param dns_no + * @return IPAddress DNS Server IP + */ +IPAddress ESP8266WiFiClass::dnsIP(uint8_t dns_no) { + ip_addr_t dns_ip = dns_getserver(dns_no); + return IPAddress(dns_ip.addr); +} + + +/** + * Get ESP8266 station DHCP hostname + * @return hostname + */ +String ESP8266WiFiClass::hostname(void) { + return String(wifi_station_get_hostname()); +} + + +/** + * Set ESP8266 station DHCP hostname + * @param aHostname max length:32 + * @return ok + */ +bool ESP8266WiFiClass::hostname(char* aHostname) { + if(strlen(aHostname) > 32) { + return false; + } + return wifi_station_set_hostname(aHostname); +} + +/** + * Set ESP8266 station DHCP hostname + * @param aHostname max length:32 + * @return ok + */ +bool ESP8266WiFiClass::hostname(const char* aHostname) { + return hostname((char*) aHostname); +} + +/** + * Set ESP8266 station DHCP hostname + * @param aHostname max length:32 + * @return ok + */ +bool ESP8266WiFiClass::hostname(String aHostname) { + return hostname((char*) aHostname.c_str()); +} + +/** + * Return Connection status. + * @return one of the value defined in wl_status_t + * + */ +wl_status_t ESP8266WiFiClass::status() { + int status = wifi_station_get_connect_status(); + + if(status == STATION_GOT_IP) + return WL_CONNECTED; + else if(status == STATION_NO_AP_FOUND) + return WL_NO_SSID_AVAIL; + else if(status == STATION_CONNECT_FAIL || status == STATION_WRONG_PASSWORD) + return WL_CONNECT_FAILED; + else if(status == STATION_IDLE) + return WL_IDLE_STATUS; + else + return WL_DISCONNECTED; +} + +/** + * Return the current SSID associated with the network + * @return SSID + */ +String ESP8266WiFiClass::SSID() const { + // TODO why static, needs RAM for nothing? + static struct station_config conf; + wifi_station_get_config(&conf); + return String(reinterpret_cast(conf.ssid)); +} + +/** + * Return the current pre shared key associated with the network + * @return psk string + */ +String ESP8266WiFiClass::psk() const { + static struct station_config conf; + wifi_station_get_config(&conf); + return String(reinterpret_cast(conf.password)); +} + +/** + * Return the current bssid / mac associated with the network if configured + * @return bssid uint8_t * + */ +uint8_t* ESP8266WiFiClass::BSSID(void) { + static struct station_config conf; + wifi_station_get_config(&conf); + return reinterpret_cast(conf.bssid); +} + +/** + * Return the current bssid / mac associated with the network if configured + * @return String bssid mac + */ +String ESP8266WiFiClass::BSSIDstr(void) { + static struct station_config conf; + char mac[18] = { 0 }; + wifi_station_get_config(&conf); + sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", conf.bssid[0], conf.bssid[1], conf.bssid[2], conf.bssid[3], conf.bssid[4], conf.bssid[5]); + return String(mac); +} + +/** + * Return the current network RSSI. + * @return RSSI value + */ +int32_t ESP8266WiFiClass::RSSI(void) { + return wifi_station_get_rssi(); +} + +// ----------------------------------------------------------------------------------------------------------------------- +// ----------------------------------------------------- AP function ----------------------------------------------------- +// ----------------------------------------------------------------------------------------------------------------------- + + +/** + * 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) + */ +void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden) { + _useApMode = true; + if(_useClientMode) { + // turn on AP+STA mode + _mode(WIFI_AP_STA); + } else { + // turn on STA mode + _mode(WIFI_AP); + } + + if(!ssid || *ssid == 0 || strlen(ssid) > 31) { + // fail SSID too long or missing! + return; + } + + if(passphrase && strlen(passphrase) > 63) { + // fail passphrase to long! + return; + } + + struct softap_config conf; + wifi_softap_get_config(&conf); + strcpy(reinterpret_cast(conf.ssid), ssid); + conf.channel = channel; + conf.ssid_len = strlen(ssid); + conf.ssid_hidden = ssid_hidden; + conf.max_connection = 4; + 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(conf.password), passphrase); + } + + struct softap_config conf_current; + wifi_softap_get_config(&conf_current); + if(softap_config_equal(conf, conf_current)) { + DEBUGV("softap config unchanged"); + return; + } + + ETS_UART_INTR_DISABLE(); + if(_persistent) + wifi_softap_set_config(&conf); + else + wifi_softap_set_config_current(&conf); + ETS_UART_INTR_ENABLE(); +} + + +/** + * Configure access point + * @param local_ip access point IP + * @param gateway gateway IP + * @param subnet subnet mask + */ +void ESP8266WiFiClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { + struct ip_info info; + info.ip.addr = static_cast(local_ip); + info.gw.addr = static_cast(gateway); + info.netmask.addr = static_cast(subnet); + wifi_softap_dhcps_stop(); + wifi_set_ip_info(SOFTAP_IF, &info); + wifi_softap_dhcps_start(); +} + + + +/** + * Disconnect from the network (close AP) + * @param wifioff disable mode? + * @return one value of wl_status_t enum + */ +int ESP8266WiFiClass::softAPdisconnect(bool wifioff) { + struct softap_config conf; + *conf.ssid = 0; + *conf.password = 0; + ETS_UART_INTR_DISABLE(); + if(_persistent) + wifi_softap_set_config(&conf); + else + wifi_softap_set_config_current(&conf); + ETS_UART_INTR_ENABLE(); + + if(wifioff) { + _useApMode = false; + + if(_useClientMode) { + // turn on STA + _mode(WIFI_STA); + } else { + // turn wifi off + _mode(WIFI_OFF); + } + } + + //TODO return with more meaning ? + return 0; +} + +/** + * Get the softAP interface IP address. + * @return IPAddress softAP IP + */ +IPAddress ESP8266WiFiClass::softAPIP() { + struct ip_info ip; + wifi_get_ip_info(SOFTAP_IF, &ip); + return IPAddress(ip.ip.addr); +} + + +/** + * Get the softAP interface MAC address. + * @param mac pointer to uint8_t array with length WL_MAC_ADDR_LENGTH + * @return pointer to uint8_t* + */ +uint8_t* ESP8266WiFiClass::softAPmacAddress(uint8_t* mac) { + wifi_get_macaddr(SOFTAP_IF, mac); + return mac; +} + +/** + * Get the softAP interface MAC address. + * @return String mac + */ +String ESP8266WiFiClass::softAPmacAddress(void) { + uint8_t mac[6]; + char macStr[18] = { 0 }; + wifi_get_macaddr(SOFTAP_IF, mac); + + sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + return String(macStr); +} + + +// ----------------------------------------------------------------------------------------------------------------------- +// ----------------------------------------------------- scan function --------------------------------------------------- +// ----------------------------------------------------------------------------------------------------------------------- + + +/** + * Start scan WiFi networks available + * @param async run in async mode + * @param show_hidden show hidden networks + * @return Number of discovered networks + */ +int8_t ESP8266WiFiClass::scanNetworks(bool async, bool show_hidden) { + if(ESP8266WiFiClass::_scanStarted) { + return WIFI_SCAN_RUNNING; + } + + ESP8266WiFiClass::_scanAsync = async; + + if(_useApMode) { + // turn on AP+STA mode + _mode(WIFI_AP_STA); + } else { + // turn on STA mode + _mode(WIFI_STA); + } + + int status = wifi_station_get_connect_status(); + if(status != STATION_GOT_IP && status != STATION_IDLE) { + disconnect(); + } + + scanDelete(); + + struct scan_config config; + config.ssid = 0; + config.bssid = 0; + config.channel = 0; + config.show_hidden = show_hidden; + if(wifi_station_scan(&config, reinterpret_cast(&ESP8266WiFiClass::_scanDone))) { + ESP8266WiFiClass::_scanComplete = false; + ESP8266WiFiClass::_scanStarted = true; + + if(ESP8266WiFiClass::_scanAsync) { + delay(0); // time for the OS to trigger the scan + return WIFI_SCAN_RUNNING; + } + + esp_yield(); + return ESP8266WiFiClass::_scanCount; + } else { + return WIFI_SCAN_FAILED; + } + +} + + +/** + * called to get the scan state in Async mode + * @return scan result or status + * -1 if scan not fin + * -2 if scan not triggered + */ +int8_t ESP8266WiFiClass::scanComplete() { + + if(_scanStarted) { + return WIFI_SCAN_RUNNING; + } + + if(_scanComplete) { + return ESP8266WiFiClass::_scanCount; + } + + return WIFI_SCAN_FAILED; +} + +/** + * delete last scan result from RAM + */ +void ESP8266WiFiClass::scanDelete() { + if(ESP8266WiFiClass::_scanResult) { + delete[] reinterpret_cast(ESP8266WiFiClass::_scanResult); + ESP8266WiFiClass::_scanResult = 0; + ESP8266WiFiClass::_scanCount = 0; + } + _scanComplete = false; +} + + +/** + * loads all infos from a scanned wifi in to the ptr parameters + * @param networkItem uint8_t + * @param ssid const char** + * @param encryptionType uint8_t * + * @param RSSI int32_t * + * @param BSSID uint8_t ** + * @param channel int32_t * + * @param isHidden bool * + * @return (true if ok) + */ +bool ESP8266WiFiClass::getNetworkInfo(uint8_t i, String &ssid, uint8_t &encType, int32_t &rssi, uint8_t* &bssid, int32_t &channel, bool &isHidden) { + struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); + if(!it) + return false; + + ssid = (const char*) it->ssid; + encType = encryptionType(i); + rssi = it->rssi; + bssid = it->bssid; // move ptr + channel = it->channel; + isHidden = (it->is_hidden != 0); + + return true; +} + + +/** + * Return the SSID discovered during the network scan. + * @param i specify from which network item want to get the information + * @return ssid string of the specified item on the networks scanned list + */ +String ESP8266WiFiClass::SSID(uint8_t i) { + struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); + if(!it) + return ""; + + return String(reinterpret_cast(it->ssid)); +} + + +/** + * Return the encryption type of the networks discovered during the scanNetworks + * @param i specify from which network item want to get the information + * @return encryption type (enum wl_enc_type) of the specified item on the networks scanned list + */ +uint8_t ESP8266WiFiClass::encryptionType(uint8_t i) { + struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); + if(!it) + return -1; + + int authmode = it->authmode; + if(authmode == AUTH_OPEN) + return ENC_TYPE_NONE; + if(authmode == AUTH_WEP) + return ENC_TYPE_WEP; + if(authmode == AUTH_WPA_PSK) + return ENC_TYPE_TKIP; + if(authmode == AUTH_WPA2_PSK) + return ENC_TYPE_CCMP; + if(authmode == AUTH_WPA_WPA2_PSK) + return ENC_TYPE_AUTO; + return -1; +} + +/** + * Return the RSSI of the networks discovered during the scanNetworks + * @param i specify from which network item want to get the information + * @return signed value of RSSI of the specified item on the networks scanned list + */ +int32_t ESP8266WiFiClass::RSSI(uint8_t i) { + struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); + if(!it) + return 0; + + return it->rssi; +} + + +/** + * return MAC / BSSID of scanned wifi + * @param i specify from which network item want to get the information + * @return uint8_t * MAC / BSSID of scanned wifi + */ +uint8_t * ESP8266WiFiClass::BSSID(uint8_t i) { + struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); + if(!it) + return 0; + + return it->bssid; +} + +/** + * return MAC / BSSID of scanned wifi + * @param networkItem specify from which network item want to get the information + * @return String MAC / BSSID of scanned wifi + */ +String ESP8266WiFiClass::BSSIDstr(uint8_t i) { + char mac[18] = { 0 }; + struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); + if(!it) + return String(""); + + sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", it->bssid[0], it->bssid[1], it->bssid[2], it->bssid[3], it->bssid[4], it->bssid[5]); + return String(mac); +} + +int32_t ESP8266WiFiClass::channel(uint8_t i) { + struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); + if(!it) + return 0; + + return it->channel; +} + +/** + * return if the scanned wifi is Hidden (no SSID) + * @param networkItem specify from which network item want to get the information + * @return bool (true == hidden) + */ +bool ESP8266WiFiClass::isHidden(uint8_t i) { + struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); + if(!it) + return false; + + return (it->is_hidden != 0); +} + + +// ----------------------------------------------------------------------------------------------------------------------- +// ------------------------------------------------- Generic WiFi function ----------------------------------------------- +// ----------------------------------------------------------------------------------------------------------------------- + + +/** + * Return the current channel associated with the network + * @return channel (1-13) + */ +int32_t ESP8266WiFiClass::channel(void) { + return wifi_get_channel(); +} + +/** + * set Sleep mode + * @param type sleep_type_t + * @return bool + */ +bool ESP8266WiFiClass::setSleepMode(WiFiSleepType_t type) { + return wifi_set_sleep_type((sleep_type_t) type); +} + +/** + * get Sleep mode + * @return sleep_type_t + */ +WiFiSleepType_t ESP8266WiFiClass::getSleepMode() { + return (WiFiSleepType_t) wifi_get_sleep_type(); +} + +/** + * set phy Mode + * @param mode phy_mode_t + * @return bool + */ +bool ESP8266WiFiClass::setPhyMode(WiFiPhyMode_t mode) { + return wifi_set_phy_mode((phy_mode_t) mode); +} + +/** + * get phy Mode + * @return phy_mode_t + */ +WiFiPhyMode_t ESP8266WiFiClass::getPhyMode() { + return (WiFiPhyMode_t) wifi_get_phy_mode(); +} + +// ----------------------------------------------------------------------------------------------------------------------- +// ------------------------------------------------ Generic Network function --------------------------------------------- +// ----------------------------------------------------------------------------------------------------------------------- + + +/** + * Resolve the given hostname to an IP address. + * @param aHostname Name to be resolved + * @param aResult IPAddress structure to store the returned IP address + * @return 1 if aIPAddrString was successfully converted to an IP address, + * else error code + */ +int ESP8266WiFiClass::hostByName(const char* aHostname, IPAddress& aResult) { + ip_addr_t addr; + aResult = static_cast(0); + err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult); + if(err == ERR_OK) { + aResult = addr.addr; + } else if(err == ERR_INPROGRESS) { + esp_yield(); + // will return here when dns_found_callback fires + } + + return (aResult != 0) ? 1 : 0; +} + + +// ----------------------------------------------------------------------------------------------------------------------- +// -------------------------------------------------- STA remote configure ----------------------------------------------- +// ----------------------------------------------------------------------------------------------------------------------- + + +/** + * WPS config + * so far only WPS_TYPE_PBC is supported (SDK 1.2.0) + * @return ok + */ +bool ESP8266WiFiClass::beginWPSConfig(void) { + + _useClientMode = true; + + if(_useApMode) { + // turn on AP+STA mode + _mode(WIFI_AP_STA); + } else { + // turn on STA mode + _mode(WIFI_STA); + } + + disconnect(); + + DEBUGV("wps begin\n"); + + if(!wifi_wps_disable()) { + DEBUGV("wps disable failed\n"); + return false; + } + + // so far only WPS_TYPE_PBC is supported (SDK 1.2.0) + if(!wifi_wps_enable(WPS_TYPE_PBC)) { + DEBUGV("wps enable failed\n"); + return false; + } + + if(!wifi_set_wps_cb((wps_st_cb_t) &wifi_wps_status_cb)) { + DEBUGV("wps cb failed\n"); + return false; + } + + if(!wifi_wps_start()) { + DEBUGV("wps start failed\n"); + return false; + } + + esp_yield(); + // will return here when wifi_wps_status_cb fires + + return true; +} + + +/** + * Start SmartConfig + */ +void ESP8266WiFiClass::beginSmartConfig() { + if(_smartConfigStarted) + return; + + if(_useApMode) { + // turn on AP+STA mode + _mode(WIFI_AP_STA); + } else { + // turn on STA mode + _mode(WIFI_STA); + } + + _smartConfigStarted = true; + _smartConfigDone = false; + + //SC_TYPE_ESPTOUCH use ESPTOUCH for smartconfig, or use SC_TYPE_AIRKISS for AIRKISS + smartconfig_start(reinterpret_cast(&ESP8266WiFiClass::_smartConfigCallback), 1); +} + + +/** + * Stop SmartConfig + */ +void ESP8266WiFiClass::stopSmartConfig() { + if(!_smartConfigStarted) + return; + + smartconfig_stop(); + _smartConfigStarted = false; +} + +/** + * Query SmartConfig status, to decide when stop config + * @return smartConfig Done + */ +bool ESP8266WiFiClass::smartConfigDone() { + if(!_smartConfigStarted) + return false; + + return _smartConfigDone; +} + + +// ----------------------------------------------------------------------------------------------------------------------- +// ---------------------------------------------------------- Debug ------------------------------------------------------ +// ----------------------------------------------------------------------------------------------------------------------- + + +/** + * Output WiFi settings to an object derived from Print interface (like Serial). + * @param p Print interface + */ +void ESP8266WiFiClass::printDiag(Print& p) { + const char* modes[] = { "NULL", "STA", "AP", "STA+AP" }; + p.print("Mode: "); + p.println(modes[wifi_get_opmode()]); + + const char* phymodes[] = { "", "B", "G", "N" }; + p.print("PHY mode: "); + p.println(phymodes[(int) wifi_get_phy_mode()]); + + p.print("Channel: "); + p.println(wifi_get_channel()); + + p.print("AP id: "); + p.println(wifi_station_get_current_ap_id()); + + p.print("Status: "); + p.println(wifi_station_get_connect_status()); + + p.print("Auto connect: "); + p.println(wifi_station_get_auto_connect()); + + static struct station_config conf; + wifi_station_get_config(&conf); + + const char* ssid = reinterpret_cast(conf.ssid); + p.print("SSID ("); + p.print(strlen(ssid)); + p.print("): "); + p.println(ssid); + + const char* passphrase = reinterpret_cast(conf.password); + p.print("Passphrase ("); + p.print(strlen(passphrase)); + p.print("): "); + p.println(passphrase); + + p.print("BSSID set: "); + p.println(conf.bssid_set); + +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + void ESP8266WiFiClass::persistent(bool persistent) { _persistent = persistent; @@ -112,213 +1102,8 @@ static bool sta_config_equal(const station_config& lhs, const station_config& rh return true; } -/** - * Start Wifi connection - * if passphrase is set the most secure supported mode will be automatically selected - * @param ssid const char* Pointer to the SSID string. - * @param passphrase const char * Optional. Passphrase. Valid characters in a passphrase must be between ASCII 32-126 (decimal). - * @param bssid uint8_t[6] Optional. BSSID / MAC of AP - * @param channel Optional. Channel of AP - * @return - */ -int ESP8266WiFiClass::begin(char* ssid, char *passphrase, int32_t channel, const uint8_t* bssid) { - return begin((const char*) ssid, (const char*) passphrase, channel, bssid); -} -int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid) { - _useClientMode = true; - if(_useApMode) { - // turn on AP+STA mode - _mode(WIFI_AP_STA); - } else { - // turn on STA mode - _mode(WIFI_STA); - } - - if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) { - // fail SSID too long or missing! - return WL_CONNECT_FAILED; - } - - if(passphrase && strlen(passphrase) > 63) { - // fail passphrase too long! - return WL_CONNECT_FAILED; - } - - struct station_config conf; - strcpy(reinterpret_cast(conf.ssid), ssid); - - if(passphrase) { - strcpy(reinterpret_cast(conf.password), passphrase); - } else { - *conf.password = 0; - } - - if(bssid) { - conf.bssid_set = 1; - memcpy((void *) &conf.bssid[0], (void *) bssid, 6); - } else { - conf.bssid_set = 0; - } - - struct station_config current_conf; - wifi_station_get_config(¤t_conf); - if(sta_config_equal(current_conf, conf)) { - DEBUGV("sta config unchanged"); - return status(); - } - - ETS_UART_INTR_DISABLE(); - if(_persistent) - wifi_station_set_config(&conf); - else - wifi_station_set_config_current(&conf); - wifi_station_connect(); - ETS_UART_INTR_ENABLE(); - - if(channel > 0 && channel <= 13) { - wifi_set_channel(channel); - } - - if(!_useStaticIp) - wifi_station_dhcpc_start(); - return status(); -} - -/** - * Use to connect to SDK config. - * @return wl_status_t - */ -int ESP8266WiFiClass::begin() { - ETS_UART_INTR_DISABLE(); - wifi_station_connect(); - ETS_UART_INTR_ENABLE(); - - // TODO is static ip not stored in SDK? - if(!_useStaticIp) - wifi_station_dhcpc_start(); - return status(); -} - -/** - * Wait for WiFi connection to reach a result - * returns the status reached or disconnect if STA is off - * @return wl_status_t - */ -uint8_t ESP8266WiFiClass::waitForConnectResult() { - if((wifi_get_opmode() & 1) == 0) //1 and 3 have STA enabled - return WL_DISCONNECTED; - while(status() == WL_DISCONNECTED) - delay(100); - return status(); -} - -/** - * Change IP configuration settings disabling the dhcp client - * @param local_ip Static ip configuration - * @param gateway Static gateway configuration - * @param subnet Static Subnet mask - */ -void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { - struct ip_info info; - info.ip.addr = static_cast(local_ip); - info.gw.addr = static_cast(gateway); - info.netmask.addr = static_cast(subnet); - - wifi_station_dhcpc_stop(); - wifi_set_ip_info(STATION_IF, &info); - - _useStaticIp = true; -} - -/** - * Change IP configuration settings disabling the dhcp client - * @param local_ip Static ip configuration - * @param gateway Static gateway configuration - * @param subnet Static Subnet mask - * @param dns Static DNS server - */ -void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns) { - struct ip_info info; - info.ip.addr = static_cast(local_ip); - info.gw.addr = static_cast(gateway); - info.netmask.addr = static_cast(subnet); - - wifi_station_dhcpc_stop(); - wifi_set_ip_info(STATION_IF, &info); - - // Set DNS-Server - ip_addr_t d; - d.addr = static_cast(dns); - dns_setserver(0, &d); - - _useStaticIp = true; -} - -/** - * Disconnect from the network (close AP) - * @param wifioff disable mode? - * @return one value of wl_status_t enum - */ -int ESP8266WiFiClass::softAPdisconnect(bool wifioff) { - struct softap_config conf; - *conf.ssid = 0; - *conf.password = 0; - ETS_UART_INTR_DISABLE(); - if(_persistent) - wifi_softap_set_config(&conf); - else - wifi_softap_set_config_current(&conf); - ETS_UART_INTR_ENABLE(); - - if(wifioff) { - _useApMode = false; - - if(_useClientMode) { - // turn on STA - _mode(WIFI_STA); - } else { - // turn wifi off - _mode(WIFI_OFF); - } - } - - //TODO return with more meaning ? - return 0; -} - -/** - * Disconnect from the network - * @param wifioff - * @return one value of wl_status_t enum - */ -int ESP8266WiFiClass::disconnect(bool wifioff) { - struct station_config conf; - *conf.ssid = 0; - *conf.password = 0; - ETS_UART_INTR_DISABLE(); - if(_persistent) - wifi_station_set_config(&conf); - else - wifi_station_set_config_current(&conf); - wifi_station_disconnect(); - ETS_UART_INTR_ENABLE(); - - if(wifioff) { - _useClientMode = false; - - if(_useApMode) { - // turn on AP - _mode(WIFI_AP); - } else { - // turn wifi off - _mode(WIFI_OFF); - } - } - //TODO return with more meaning ? - return 0; -} static bool softap_config_equal(const softap_config& lhs, const softap_config& rhs) { if(strcmp(reinterpret_cast(lhs.ssid), reinterpret_cast(rhs.ssid)) != 0) @@ -332,238 +1117,15 @@ static bool softap_config_equal(const softap_config& lhs, const softap_config& r return true; } -/** - * 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) - */ -void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden) { - _useApMode = true; - if(_useClientMode) { - // turn on AP+STA mode - _mode(WIFI_AP_STA); - } else { - // turn on STA mode - _mode(WIFI_AP); - } - - if(!ssid || *ssid == 0 || strlen(ssid) > 31) { - // fail SSID too long or missing! - return; - } - - if(passphrase && strlen(passphrase) > 63) { - // fail passphrase to long! - return; - } - - struct softap_config conf; - wifi_softap_get_config(&conf); - strcpy(reinterpret_cast(conf.ssid), ssid); - conf.channel = channel; - conf.ssid_len = strlen(ssid); - conf.ssid_hidden = ssid_hidden; - conf.max_connection = 4; - 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(conf.password), passphrase); - } - - struct softap_config conf_current; - wifi_softap_get_config(&conf_current); - if(softap_config_equal(conf, conf_current)) { - DEBUGV("softap config unchanged"); - return; - } - - ETS_UART_INTR_DISABLE(); - if(_persistent) - wifi_softap_set_config(&conf); - else - wifi_softap_set_config_current(&conf); - ETS_UART_INTR_ENABLE(); -} - -/** - * Configure access point - * @param local_ip access point IP - * @param gateway gateway IP - * @param subnet subnet mask - */ -void ESP8266WiFiClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { - struct ip_info info; - info.ip.addr = static_cast(local_ip); - info.gw.addr = static_cast(gateway); - info.netmask.addr = static_cast(subnet); - wifi_softap_dhcps_stop(); - wifi_set_ip_info(SOFTAP_IF, &info); - wifi_softap_dhcps_start(); -} - -/** - * Get the station interface MAC address. - * @param mac pointer to uint8_t array with length WL_MAC_ADDR_LENGTH - * @return pointer to uint8_t * - */ -uint8_t* ESP8266WiFiClass::macAddress(uint8_t* mac) { - wifi_get_macaddr(STATION_IF, mac); - return mac; -} - -/** - * Get the station interface MAC address. - * @return String mac - */ -String ESP8266WiFiClass::macAddress(void) { - uint8_t mac[6]; - char macStr[18] = { 0 }; - wifi_get_macaddr(STATION_IF, mac); - - sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); - return String(macStr); -} -/** - * Get the softAP interface MAC address. - * @param mac pointer to uint8_t array with length WL_MAC_ADDR_LENGTH - * @return pointer to uint8_t* - */ -uint8_t* ESP8266WiFiClass::softAPmacAddress(uint8_t* mac) { - wifi_get_macaddr(SOFTAP_IF, mac); - return mac; -} - -/** - * Get the softAP interface MAC address. - * @return String mac - */ -String ESP8266WiFiClass::softAPmacAddress(void) { - uint8_t mac[6]; - char macStr[18] = { 0 }; - wifi_get_macaddr(SOFTAP_IF, mac); - - sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); - return String(macStr); -} - -/** - * Get the station interface IP address. - * @return IPAddress station IP - */ -IPAddress ESP8266WiFiClass::localIP() { - struct ip_info ip; - wifi_get_ip_info(STATION_IF, &ip); - return IPAddress(ip.ip.addr); -} - -/** - * Get the softAP interface IP address. - * @return IPAddress softAP IP - */ -IPAddress ESP8266WiFiClass::softAPIP() { - struct ip_info ip; - wifi_get_ip_info(SOFTAP_IF, &ip); - return IPAddress(ip.ip.addr); -} - -/** - * Get the interface subnet mask address. - * @return IPAddress subnetMask - */ -IPAddress ESP8266WiFiClass::subnetMask() { - struct ip_info ip; - wifi_get_ip_info(STATION_IF, &ip); - return IPAddress(ip.netmask.addr); -} - -/** - * Get the gateway ip address. - * @return IPAddress gatewayIP - */ -IPAddress ESP8266WiFiClass::gatewayIP() { - struct ip_info ip; - wifi_get_ip_info(STATION_IF, &ip); - return IPAddress(ip.gw.addr); -} - -/** - * Get the DNS ip address. - * @param dns_no - * @return IPAddress DNS Server IP - */ -IPAddress ESP8266WiFiClass::dnsIP(uint8_t dns_no) { - ip_addr_t dns_ip = dns_getserver(dns_no); - return IPAddress(dns_ip.addr); -} -/** - * Return the current SSID associated with the network - * @return SSID - */ -String ESP8266WiFiClass::SSID() const { - // TODO why static, needs RAM for nothing? - static struct station_config conf; - wifi_station_get_config(&conf); - return String(reinterpret_cast(conf.ssid)); -} - -/** - * Return the current pre shared key associated with the network - * @return psk string - */ -String ESP8266WiFiClass::psk() const { - static struct station_config conf; - wifi_station_get_config(&conf); - return String(reinterpret_cast(conf.password)); -} -/** - * Return the current bssid / mac associated with the network if configured - * @return bssid uint8_t * - */ -uint8_t* ESP8266WiFiClass::BSSID(void) { - static struct station_config conf; - wifi_station_get_config(&conf); - return reinterpret_cast(conf.bssid); -} -/** - * Return the current bssid / mac associated with the network if configured - * @return String bssid mac - */ -String ESP8266WiFiClass::BSSIDstr(void) { - static struct station_config conf; - char mac[18] = { 0 }; - wifi_station_get_config(&conf); - sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", conf.bssid[0], conf.bssid[1], conf.bssid[2], conf.bssid[3], conf.bssid[4], conf.bssid[5]); - return String(mac); -} -/** - * Return the current channel associated with the network - * @return channel (1-13) - */ -int32_t ESP8266WiFiClass::channel(void) { - return wifi_get_channel(); -} -/** - * Return the current network RSSI. - * @return RSSI value - */ -int32_t ESP8266WiFiClass::RSSI(void) { - return wifi_station_get_rssi(); -} extern "C" { typedef STAILQ_HEAD(, bss_info) @@ -604,86 +1166,6 @@ void ESP8266WiFiClass::_scanDone(void* result, int status) { } } -/** - * called to get the scan state in Async mode - * @return scan result or status - * -1 if scan not fin - * -2 if scan not triggered - */ -int8_t ESP8266WiFiClass::scanComplete() { - - if(_scanStarted) { - return WIFI_SCAN_RUNNING; - } - - if(_scanComplete) { - return ESP8266WiFiClass::_scanCount; - } - - return WIFI_SCAN_FAILED; -} - -/** - * delete last scan result from RAM - */ -void ESP8266WiFiClass::scanDelete() { - if(ESP8266WiFiClass::_scanResult) { - delete[] reinterpret_cast(ESP8266WiFiClass::_scanResult); - ESP8266WiFiClass::_scanResult = 0; - ESP8266WiFiClass::_scanCount = 0; - } - _scanComplete = false; -} - -/** - * Start scan WiFi networks available - * @param async run in async mode - * @param show_hidden show hidden networks - * @return Number of discovered networks - */ -int8_t ESP8266WiFiClass::scanNetworks(bool async, bool show_hidden) { - if(ESP8266WiFiClass::_scanStarted) { - return WIFI_SCAN_RUNNING; - } - - ESP8266WiFiClass::_scanAsync = async; - - if(_useApMode) { - // turn on AP+STA mode - _mode(WIFI_AP_STA); - } else { - // turn on STA mode - _mode(WIFI_STA); - } - - int status = wifi_station_get_connect_status(); - if(status != STATION_GOT_IP && status != STATION_IDLE) { - disconnect(); - } - - scanDelete(); - - struct scan_config config; - config.ssid = 0; - config.bssid = 0; - config.channel = 0; - config.show_hidden = show_hidden; - if(wifi_station_scan(&config, reinterpret_cast(&ESP8266WiFiClass::_scanDone))) { - ESP8266WiFiClass::_scanComplete = false; - ESP8266WiFiClass::_scanStarted = true; - - if(ESP8266WiFiClass::_scanAsync) { - delay(0); // time for the OS to trigger the scan - return WIFI_SCAN_RUNNING; - } - - esp_yield(); - return ESP8266WiFiClass::_scanCount; - } else { - return WIFI_SCAN_FAILED; - } - -} void * ESP8266WiFiClass::_getScanInfoByIndex(int i) { if(!ESP8266WiFiClass::_scanResult || (size_t) i > ESP8266WiFiClass::_scanCount) { @@ -693,150 +1175,11 @@ void * ESP8266WiFiClass::_getScanInfoByIndex(int i) { return reinterpret_cast(ESP8266WiFiClass::_scanResult) + i; } -/** - * Return the SSID discovered during the network scan. - * @param i specify from which network item want to get the information - * @return ssid string of the specified item on the networks scanned list - */ -String ESP8266WiFiClass::SSID(uint8_t i) { - struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) - return ""; - return String(reinterpret_cast(it->ssid)); -} -/** - * return MAC / BSSID of scanned wifi - * @param i specify from which network item want to get the information - * @return uint8_t * MAC / BSSID of scanned wifi - */ -uint8_t * ESP8266WiFiClass::BSSID(uint8_t i) { - struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) - return 0; - return it->bssid; -} -/** - * return MAC / BSSID of scanned wifi - * @param networkItem specify from which network item want to get the information - * @return String MAC / BSSID of scanned wifi - */ -String ESP8266WiFiClass::BSSIDstr(uint8_t i) { - char mac[18] = { 0 }; - struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) - return String(""); - sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", it->bssid[0], it->bssid[1], it->bssid[2], it->bssid[3], it->bssid[4], it->bssid[5]); - return String(mac); -} - -int32_t ESP8266WiFiClass::channel(uint8_t i) { - struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) - return 0; - - return it->channel; -} - -/** - * return if the scanned wifi is Hidden (no SSID) - * @param networkItem specify from which network item want to get the information - * @return bool (true == hidden) - */ -bool ESP8266WiFiClass::isHidden(uint8_t i) { - struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) - return false; - - return (it->is_hidden != 0); -} - -/** - * loads all infos from a scanned wifi in to the ptr parameters - * @param networkItem uint8_t - * @param ssid const char** - * @param encryptionType uint8_t * - * @param RSSI int32_t * - * @param BSSID uint8_t ** - * @param channel int32_t * - * @param isHidden bool * - * @return (true if ok) - */ -bool ESP8266WiFiClass::getNetworkInfo(uint8_t i, String &ssid, uint8_t &encType, int32_t &rssi, uint8_t* &bssid, int32_t &channel, bool &isHidden) { - struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) - return false; - - ssid = (const char*) it->ssid; - encType = encryptionType(i); - rssi = it->rssi; - bssid = it->bssid; // move ptr - channel = it->channel; - isHidden = (it->is_hidden != 0); - - return true; -} - -/** - * Return the RSSI of the networks discovered during the scanNetworks - * @param i specify from which network item want to get the information - * @return signed value of RSSI of the specified item on the networks scanned list - */ -int32_t ESP8266WiFiClass::RSSI(uint8_t i) { - struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) - return 0; - - return it->rssi; -} - -/** - * Return the encryption type of the networks discovered during the scanNetworks - * @param i specify from which network item want to get the information - * @return encryption type (enum wl_enc_type) of the specified item on the networks scanned list - */ -uint8_t ESP8266WiFiClass::encryptionType(uint8_t i) { - struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) - return -1; - - int authmode = it->authmode; - if(authmode == AUTH_OPEN) - return ENC_TYPE_NONE; - if(authmode == AUTH_WEP) - return ENC_TYPE_WEP; - if(authmode == AUTH_WPA_PSK) - return ENC_TYPE_TKIP; - if(authmode == AUTH_WPA2_PSK) - return ENC_TYPE_CCMP; - if(authmode == AUTH_WPA_WPA2_PSK) - return ENC_TYPE_AUTO; - return -1; -} - -/** - * Return Connection status. - * @return one of the value defined in wl_status_t - * - */ -wl_status_t ESP8266WiFiClass::status() { - int status = wifi_station_get_connect_status(); - - if(status == STATION_GOT_IP) - return WL_CONNECTED; - else if(status == STATION_NO_AP_FOUND) - return WL_NO_SSID_AVAIL; - else if(status == STATION_CONNECT_FAIL || status == STATION_WRONG_PASSWORD) - return WL_CONNECT_FAILED; - else if(status == STATION_IDLE) - return WL_IDLE_STATUS; - else - return WL_DISCONNECTED; -} void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback_arg) { if(ipaddr) @@ -844,65 +1187,7 @@ void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback esp_schedule(); // resume the hostByName function } -/** - * Resolve the given hostname to an IP address. - * @param aHostname Name to be resolved - * @param aResult IPAddress structure to store the returned IP address - * @return 1 if aIPAddrString was successfully converted to an IP address, - * else error code - */ -int ESP8266WiFiClass::hostByName(const char* aHostname, IPAddress& aResult) { - ip_addr_t addr; - aResult = static_cast(0); - err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult); - if(err == ERR_OK) { - aResult = addr.addr; - } else if(err == ERR_INPROGRESS) { - esp_yield(); - // will return here when dns_found_callback fires - } - return (aResult != 0) ? 1 : 0; -} - -/** - * Get ESP8266 station DHCP hostname - * @return hostname - */ -String ESP8266WiFiClass::hostname(void) { - return String(wifi_station_get_hostname()); -} - - -/** - * Set ESP8266 station DHCP hostname - * @param aHostname max length:32 - * @return ok - */ -bool ESP8266WiFiClass::hostname(char* aHostname) { - if(strlen(aHostname) > 32) { - return false; - } - return wifi_station_set_hostname(aHostname); -} - -/** - * Set ESP8266 station DHCP hostname - * @param aHostname max length:32 - * @return ok - */ -bool ESP8266WiFiClass::hostname(const char* aHostname) { - return hostname((char*) aHostname); -} - -/** - * Set ESP8266 station DHCP hostname - * @param aHostname max length:32 - * @return ok - */ -bool ESP8266WiFiClass::hostname(String aHostname) { - return hostname((char*) aHostname.c_str()); -} //-------------------------------------------------------------- @@ -930,100 +1215,9 @@ void wifi_wps_status_cb(wps_cb_status status) { esp_schedule(); // resume the beginWPSConfig function } -/** - * WPS config - * so far only WPS_TYPE_PBC is supported (SDK 1.2.0) - * @return ok - */ -bool ESP8266WiFiClass::beginWPSConfig(void) { - - _useClientMode = true; - - if(_useApMode) { - // turn on AP+STA mode - _mode(WIFI_AP_STA); - } else { - // turn on STA mode - _mode(WIFI_STA); - } - - disconnect(); - - DEBUGV("wps begin\n"); - - if(!wifi_wps_disable()) { - DEBUGV("wps disable failed\n"); - return false; - } - - // so far only WPS_TYPE_PBC is supported (SDK 1.2.0) - if(!wifi_wps_enable(WPS_TYPE_PBC)) { - DEBUGV("wps enable failed\n"); - return false; - } - - if(!wifi_set_wps_cb((wps_st_cb_t) &wifi_wps_status_cb)) { - DEBUGV("wps cb failed\n"); - return false; - } - - if(!wifi_wps_start()) { - DEBUGV("wps start failed\n"); - return false; - } - - esp_yield(); - // will return here when wifi_wps_status_cb fires - - return true; -} //-------------------------------------------------------------- -/** - * Start SmartConfig - */ -void ESP8266WiFiClass::beginSmartConfig() { - if(_smartConfigStarted) - return; - - if(_useApMode) { - // turn on AP+STA mode - _mode(WIFI_AP_STA); - } else { - // turn on STA mode - _mode(WIFI_STA); - } - - _smartConfigStarted = true; - _smartConfigDone = false; - - //SC_TYPE_ESPTOUCH use ESPTOUCH for smartconfig, or use SC_TYPE_AIRKISS for AIRKISS - smartconfig_start(reinterpret_cast(&ESP8266WiFiClass::_smartConfigCallback), 1); -} - - -/** - * Stop SmartConfig - */ -void ESP8266WiFiClass::stopSmartConfig() { - if(!_smartConfigStarted) - return; - - smartconfig_stop(); - _smartConfigStarted = false; -} - -/** - * Query SmartConfig status, to decide when stop config - * @return smartConfig Done - */ -bool ESP8266WiFiClass::smartConfigDone() { - if(!_smartConfigStarted) - return false; - - return _smartConfigDone; -} void ESP8266WiFiClass::_smartConfigCallback(uint32_t st, void* result) { sc_status status = (sc_status) st; @@ -1042,39 +1236,6 @@ void ESP8266WiFiClass::_smartConfigCallback(uint32_t st, void* result) { //-------------------------------------------------------------- -/** - * set Sleep mode - * @param type sleep_type_t - * @return bool - */ -bool ESP8266WiFiClass::setSleepMode(WiFiSleepType_t type) { - return wifi_set_sleep_type((sleep_type_t) type); -} - -/** - * get Sleep mode - * @return sleep_type_t - */ -WiFiSleepType_t ESP8266WiFiClass::getSleepMode() { - return (WiFiSleepType_t) wifi_get_sleep_type(); -} - -/** - * set phy Mode - * @param mode phy_mode_t - * @return bool - */ -bool ESP8266WiFiClass::setPhyMode(WiFiPhyMode_t mode) { - return wifi_set_phy_mode((phy_mode_t) mode); -} - -/** - * get phy Mode - * @return phy_mode_t - */ -WiFiPhyMode_t ESP8266WiFiClass::getPhyMode() { - return (WiFiPhyMode_t) wifi_get_phy_mode(); -} //-------------------------------------------------------------- @@ -1088,50 +1249,6 @@ void ESP8266WiFiClass::_eventCallback(void* arg) { } -/** - * Output WiFi settings to an object derived from Print interface (like Serial). - * @param p Print interface - */ -void ESP8266WiFiClass::printDiag(Print& p) { - const char* modes[] = { "NULL", "STA", "AP", "STA+AP" }; - p.print("Mode: "); - p.println(modes[wifi_get_opmode()]); - - const char* phymodes[] = { "", "B", "G", "N" }; - p.print("PHY mode: "); - p.println(phymodes[(int) wifi_get_phy_mode()]); - - p.print("Channel: "); - p.println(wifi_get_channel()); - - p.print("AP id: "); - p.println(wifi_station_get_current_ap_id()); - - p.print("Status: "); - p.println(wifi_station_get_connect_status()); - - p.print("Auto connect: "); - p.println(wifi_station_get_auto_connect()); - - static struct station_config conf; - wifi_station_get_config(&conf); - - const char* ssid = reinterpret_cast(conf.ssid); - p.print("SSID ("); - p.print(strlen(ssid)); - p.print("): "); - p.println(ssid); - - const char* passphrase = reinterpret_cast(conf.password); - p.print("Passphrase ("); - p.print(strlen(passphrase)); - p.print("): "); - p.println(passphrase); - - p.print("BSSID set: "); - p.println(conf.bssid_set); - -} bool ESP8266WiFiClass::_scanAsync = false; bool ESP8266WiFiClass::_scanStarted = false; diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.h b/libraries/ESP8266WiFi/src/ESP8266WiFi.h index c3915a932..815cd675a 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.h @@ -61,96 +61,82 @@ class ESP8266WiFiClass { void mode(WiFiMode); WiFiMode getMode(); + // ---------------------------------------------------------------------------------------------- + // ---------------------------------------- STA function ---------------------------------------- + // ---------------------------------------------------------------------------------------------- int begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL); int begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL); int begin(); - uint8_t waitForConnectResult(); - - void softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0); - - void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet); void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns); - void softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet); - - - int softAPdisconnect(bool wifioff = false); int disconnect(bool wifioff = false); + uint8_t waitForConnectResult(); + + // STA network info + IPAddress localIP(); uint8_t* macAddress(uint8_t* mac); String macAddress(void); - uint8_t* softAPmacAddress(uint8_t* mac); - String softAPmacAddress(void); - - - IPAddress localIP(); - IPAddress softAPIP(); - - IPAddress subnetMask(); IPAddress gatewayIP(); IPAddress dnsIP(uint8_t dns_no = 0); - - String SSID() const; - String psk() const; - - - uint8_t *BSSID(void); - String BSSIDstr(void); - int32_t channel(void); - - - int32_t RSSI(); - - - int8_t scanComplete(); - void scanDelete(); - - - int8_t scanNetworks(bool async = false, bool show_hidden = false); - - - String SSID(uint8_t networkItem); - uint8_t encryptionType(uint8_t networkItem); - int32_t RSSI(uint8_t networkItem); - uint8_t * BSSID(uint8_t networkItem); - - - String BSSIDstr(uint8_t networkItem); - int32_t channel(uint8_t networkItem); - bool isHidden(uint8_t networkItem); - bool getNetworkInfo(uint8_t networkItem, String &ssid, uint8_t &encryptionType, int32_t &RSSI, uint8_t* &BSSID, int32_t &channel, bool &isHidden); - - - wl_status_t status(); - - - int hostByName(const char* aHostname, IPAddress& aResult); - - String hostname(void); bool hostname(char* aHostname); bool hostname(const char* aHostname); bool hostname(String aHostname); + // STA WiFi info + wl_status_t status(); + String SSID() const; + String psk() const; - bool beginWPSConfig(void); + uint8_t *BSSID(void); + String BSSIDstr(void); - void printDiag(Print& dest); + int32_t RSSI(); + // ---------------------------------------------------------------------------------------------- + // ----------------------------------------- AP function ---------------------------------------- + // ---------------------------------------------------------------------------------------------- + void softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0); + void softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet); + int softAPdisconnect(bool wifioff = false); - void beginSmartConfig(); - bool smartConfigDone(); - void stopSmartConfig(); + IPAddress softAPIP(); - friend class WiFiClient; - friend class WiFiServer; + uint8_t* softAPmacAddress(uint8_t* mac); + String softAPmacAddress(void); + // ---------------------------------------------------------------------------------------------- + // ----------------------------------------- scan function -------------------------------------- + // ---------------------------------------------------------------------------------------------- + + int8_t scanNetworks(bool async = false, bool show_hidden = false); + + int8_t scanComplete(); + void scanDelete(); + + // scan result + bool getNetworkInfo(uint8_t networkItem, String &ssid, uint8_t &encryptionType, int32_t &RSSI, uint8_t* &BSSID, int32_t &channel, bool &isHidden); + + String SSID(uint8_t networkItem); + uint8_t encryptionType(uint8_t networkItem); + int32_t RSSI(uint8_t networkItem); + uint8_t * BSSID(uint8_t networkItem); + String BSSIDstr(uint8_t networkItem); + int32_t channel(uint8_t networkItem); + bool isHidden(uint8_t networkItem); + + // ---------------------------------------------------------------------------------------------- + // -------------------------------------- Generic WiFi function --------------------------------- + // ---------------------------------------------------------------------------------------------- + + int32_t channel(void); bool setSleepMode(WiFiSleepType_t type); WiFiSleepType_t getSleepMode(); @@ -158,6 +144,34 @@ class ESP8266WiFiClass { bool setPhyMode(WiFiPhyMode_t mode); WiFiPhyMode_t getPhyMode(); + + // ---------------------------------------------------------------------------------------------- + // ------------------------------------ Generic Network function -------------------------------- + // ---------------------------------------------------------------------------------------------- + + int hostByName(const char* aHostname, IPAddress& aResult); + + + // ---------------------------------------------------------------------------------------------- + // ------------------------------------ STA remote configure ----------------------------------- + // ---------------------------------------------------------------------------------------------- + + bool beginWPSConfig(void); + + void beginSmartConfig(); + bool smartConfigDone(); + void stopSmartConfig(); + + + // ---------------------------------------------------------------------------------------------- + // ------------------------------------------- Debug -------------------------------------------- + // ---------------------------------------------------------------------------------------------- + + void printDiag(Print& dest); + + friend class WiFiClient; + friend class WiFiServer; + protected: void _mode(WiFiMode); static void _scanDone(void* result, int status); From 20de9a5673b5cf5de15afeb23ed0ee42ba3dd1be Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Mon, 28 Dec 2015 17:38:58 +0100 Subject: [PATCH 04/21] reorder part two --- libraries/ESP8266WiFi/src/ESP8266WiFi.cpp | 523 +++++++++++----------- libraries/ESP8266WiFi/src/ESP8266WiFi.h | 9 +- 2 files changed, 268 insertions(+), 264 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp index 65dc97564..ee52e8ab4 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp @@ -41,6 +41,71 @@ extern "C" { extern "C" void esp_schedule(); extern "C" void esp_yield(); + + +extern "C" { +typedef STAILQ_HEAD(, bss_info) +bss_info_head_t; +} + + +// ----------------------------------------------------------------------------------------------------------------------- +// ---------------------------------------------------- Private functions ------------------------------------------------ +// ----------------------------------------------------------------------------------------------------------------------- + +static bool sta_config_equal(const station_config& lhs, const station_config& rhs); +static bool softap_config_equal(const softap_config& lhs, const softap_config& rhs); + + +/** + * compare two STA configurations + * @param lhs station_config + * @param rhs station_config + * @return equal + */ +static bool sta_config_equal(const station_config& lhs, const station_config& rhs) { + if(strcmp(reinterpret_cast(lhs.ssid), reinterpret_cast(rhs.ssid)) != 0) + return false; + + if(strcmp(reinterpret_cast(lhs.password), reinterpret_cast(rhs.password)) != 0) + return false; + + if(lhs.bssid_set) { + if(!rhs.bssid_set) + return false; + + if(memcmp(lhs.bssid, rhs.bssid, 6) != 0) + return false; + } else { + if(rhs.bssid_set) + return false; + } + + return true; +} + +/** + * compare two AP configurations + * @param lhs softap_config + * @param rhs softap_config + * @return equal + */ +static bool softap_config_equal(const softap_config& lhs, const softap_config& rhs) { + if(strcmp(reinterpret_cast(lhs.ssid), reinterpret_cast(rhs.ssid)) != 0) + return false; + if(strcmp(reinterpret_cast(lhs.password), reinterpret_cast(rhs.password)) != 0) + return false; + if(lhs.channel != rhs.channel) + return false; + if(lhs.ssid_hidden != rhs.ssid_hidden) + return false; + return true; +} + +// ----------------------------------------------------------------------------------------------------------------------- +// ---------------------------------------------------- ESP8266WiFiClass ------------------------------------------------- +// ----------------------------------------------------------------------------------------------------------------------- + ESP8266WiFiClass::ESP8266WiFiClass() : _smartConfigStarted(false), _smartConfigDone(false), _useStaticIp(false), _persistent(true) { uint8 m = wifi_get_opmode(); @@ -49,6 +114,22 @@ ESP8266WiFiClass::ESP8266WiFiClass() : wifi_set_event_handler_cb((wifi_event_handler_cb_t) &ESP8266WiFiClass::_eventCallback); } +/** + * callback for WiFi events + * @param arg + */ +void ESP8266WiFiClass::_eventCallback(void* arg) { + System_Event_t* event = reinterpret_cast(arg); + DEBUGV("wifi evt: %d\r\n", event->event); + + if(event->event == EVENT_STAMODE_DISCONNECTED) { + WiFiClient::stopAll(); + } + + //TODO allow user to hook this event +} + + // ----------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------- STA function ----------------------------------------------------- // ----------------------------------------------------------------------------------------------------------------------- @@ -563,6 +644,12 @@ String ESP8266WiFiClass::softAPmacAddress(void) { // ----------------------------------------------------- scan function --------------------------------------------------- // ----------------------------------------------------------------------------------------------------------------------- +bool ESP8266WiFiClass::_scanAsync = false; +bool ESP8266WiFiClass::_scanStarted = false; +bool ESP8266WiFiClass::_scanComplete = false; + +size_t ESP8266WiFiClass::_scanCount = 0; +void* ESP8266WiFiClass::_scanResult = 0; /** * Start scan WiFi networks available @@ -741,7 +828,7 @@ uint8_t * ESP8266WiFiClass::BSSID(uint8_t i) { /** * return MAC / BSSID of scanned wifi - * @param networkItem specify from which network item want to get the information + * @param i specify from which network item want to get the information * @return String MAC / BSSID of scanned wifi */ String ESP8266WiFiClass::BSSIDstr(uint8_t i) { @@ -775,6 +862,60 @@ bool ESP8266WiFiClass::isHidden(uint8_t i) { return (it->is_hidden != 0); } +/** + * private + * scan callback + * @param result void *arg + * @param status STATUS + */ +void ESP8266WiFiClass::_scanDone(void* result, int status) { + if(status != OK) { + ESP8266WiFiClass::_scanCount = 0; + ESP8266WiFiClass::_scanResult = 0; + } else { + + int i = 0; + bss_info_head_t* head = reinterpret_cast(result); + + for(bss_info* it = STAILQ_FIRST(head); it; it = STAILQ_NEXT(it, next), ++i) + ; + ESP8266WiFiClass::_scanCount = i; + if(i == 0) { + ESP8266WiFiClass::_scanResult = 0; + } else { + bss_info* copied_info = new bss_info[i]; + i = 0; + for(bss_info* it = STAILQ_FIRST(head); it; it = STAILQ_NEXT(it, next), ++i) { + memcpy(copied_info + i, it, sizeof(bss_info)); + } + + ESP8266WiFiClass::_scanResult = copied_info; + } + + } + + ESP8266WiFiClass::_scanStarted = false; + ESP8266WiFiClass::_scanComplete = true; + + if(!ESP8266WiFiClass::_scanAsync) { + esp_schedule(); + } +} + +/** + * + * @param i specify from which network item want to get the information + * @return bss_info * + */ +void * ESP8266WiFiClass::_getScanInfoByIndex(int i) { + //TODO why its void * and not bss_info * ? + if(!ESP8266WiFiClass::_scanResult || (size_t) i > ESP8266WiFiClass::_scanCount) { + return 0; + } + return reinterpret_cast(ESP8266WiFiClass::_scanResult) + i; +} + + // ----------------------------------------------------------------------------------------------------------------------- // ------------------------------------------------- Generic WiFi function ----------------------------------------------- @@ -823,10 +964,72 @@ WiFiPhyMode_t ESP8266WiFiClass::getPhyMode() { return (WiFiPhyMode_t) wifi_get_phy_mode(); } +/** + * store WiFi condif in SDK config area in flash + * @param persistent + */ +void ESP8266WiFiClass::persistent(bool persistent) { + _persistent = persistent; +} + + +/** + * set new mode + * @param m WiFiMode + */ +void ESP8266WiFiClass::mode(WiFiMode m) { + if(wifi_get_opmode() == (uint8) m) { + return; + } + + if((m & WIFI_AP)) { + _useApMode = true; + } else { + _useApMode = false; + } + + if((m & WIFI_STA)) { + _useClientMode = true; + } else { + _useClientMode = false; + } + + _mode(m); +} + +/** + * get WiFi mode + * @return WiFiMode + */ +WiFiMode ESP8266WiFiClass::getMode() { + return (WiFiMode) wifi_get_opmode(); +} + + +/** + * private + * internal mode handling. + * @param m WiFiMode + */ +void ESP8266WiFiClass::_mode(WiFiMode m) { + if(wifi_get_opmode() == (uint8) m) { + return; + } + + ETS_UART_INTR_DISABLE(); + if(_persistent) + wifi_set_opmode(m); + else + wifi_set_opmode_current(m); + ETS_UART_INTR_ENABLE(); + +} + // ----------------------------------------------------------------------------------------------------------------------- // ------------------------------------------------ Generic Network function --------------------------------------------- // ----------------------------------------------------------------------------------------------------------------------- +void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback_arg); /** * Resolve the given hostname to an IP address. @@ -849,11 +1052,24 @@ int ESP8266WiFiClass::hostByName(const char* aHostname, IPAddress& aResult) { return (aResult != 0) ? 1 : 0; } +/** + * DNS callback + * @param name + * @param ipaddr + * @param callback_arg + */ +void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback_arg) { + if(ipaddr) + (*reinterpret_cast(callback_arg)) = ipaddr->addr; + esp_schedule(); // resume the hostByName function +} + // ----------------------------------------------------------------------------------------------------------------------- // -------------------------------------------------- STA remote configure ----------------------------------------------- // ----------------------------------------------------------------------------------------------------------------------- +void wifi_wps_status_cb(wps_cb_status status); /** * WPS config @@ -903,6 +1119,33 @@ bool ESP8266WiFiClass::beginWPSConfig(void) { return true; } +/** + * WPS callback + * @param status wps_cb_status + */ +void wifi_wps_status_cb(wps_cb_status status) { + DEBUGV("wps cb status: %d\r\n", status); + switch(status) { + case WPS_CB_ST_SUCCESS: + if(!wifi_wps_disable()) { + DEBUGV("wps disable failed\n"); + } + wifi_station_connect(); + break; + case WPS_CB_ST_FAILED: + DEBUGV("wps FAILED\n"); + break; + case WPS_CB_ST_TIMEOUT: + DEBUGV("wps TIMEOUT\n"); + break; + case WPS_CB_ST_WEP: + DEBUGV("wps WEP\n"); + break; + } + // TODO user function to get status + + esp_schedule(); // resume the beginWPSConfig function +} /** * Start SmartConfig @@ -950,6 +1193,27 @@ bool ESP8266WiFiClass::smartConfigDone() { } +/** + * _smartConfigCallback + * @param st + * @param result + */ +void ESP8266WiFiClass::_smartConfigCallback(uint32_t st, void* result) { + sc_status status = (sc_status) st; + if(status == SC_STATUS_LINK) { + station_config* sta_conf = reinterpret_cast(result); + + wifi_station_set_config(sta_conf); + wifi_station_disconnect(); + wifi_station_connect(); + + WiFi._smartConfigDone = true; + } else if(status == SC_STATUS_LINK_OVER) { + WiFi.stopSmartConfig(); + } +} + + // ----------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------- Debug ------------------------------------------------------ // ----------------------------------------------------------------------------------------------------------------------- @@ -1000,261 +1264,4 @@ void ESP8266WiFiClass::printDiag(Print& p) { } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void ESP8266WiFiClass::persistent(bool persistent) { - _persistent = persistent; -} - -void ESP8266WiFiClass::mode(WiFiMode m) { - if(wifi_get_opmode() == (uint8) m) { - return; - } - - if((m & WIFI_AP)) { - _useApMode = true; - } else { - _useApMode = false; - } - - if((m & WIFI_STA)) { - _useClientMode = true; - } else { - _useClientMode = false; - } - - _mode(m); -} - -WiFiMode ESP8266WiFiClass::getMode() { - return (WiFiMode) wifi_get_opmode(); -} - -void ESP8266WiFiClass::_mode(WiFiMode m) { - if(wifi_get_opmode() == (uint8) m) { - return; - } - - ETS_UART_INTR_DISABLE(); - if(_persistent) - wifi_set_opmode(m); - else - wifi_set_opmode_current(m); - ETS_UART_INTR_ENABLE(); - -} - -static bool sta_config_equal(const station_config& lhs, const station_config& rhs) { - if(strcmp(reinterpret_cast(lhs.ssid), reinterpret_cast(rhs.ssid)) != 0) - return false; - - if(strcmp(reinterpret_cast(lhs.password), reinterpret_cast(rhs.password)) != 0) - return false; - - if(lhs.bssid_set) { - if(!rhs.bssid_set) - return false; - - if(memcmp(lhs.bssid, rhs.bssid, 6) != 0) - return false; - } else { - if(rhs.bssid_set) - return false; - } - - return true; -} - - - - -static bool softap_config_equal(const softap_config& lhs, const softap_config& rhs) { - if(strcmp(reinterpret_cast(lhs.ssid), reinterpret_cast(rhs.ssid)) != 0) - return false; - if(strcmp(reinterpret_cast(lhs.password), reinterpret_cast(rhs.password)) != 0) - return false; - if(lhs.channel != rhs.channel) - return false; - if(lhs.ssid_hidden != rhs.ssid_hidden) - return false; - return true; -} - - - - - - - - - - - -extern "C" { -typedef STAILQ_HEAD(, bss_info) -bss_info_head_t; -} - -void ESP8266WiFiClass::_scanDone(void* result, int status) { - if(status != OK) { - ESP8266WiFiClass::_scanCount = 0; - ESP8266WiFiClass::_scanResult = 0; - } else { - - int i = 0; - bss_info_head_t* head = reinterpret_cast(result); - - for(bss_info* it = STAILQ_FIRST(head); it; it = STAILQ_NEXT(it, next), ++i) - ; - ESP8266WiFiClass::_scanCount = i; - if(i == 0) { - ESP8266WiFiClass::_scanResult = 0; - } else { - bss_info* copied_info = new bss_info[i]; - i = 0; - for(bss_info* it = STAILQ_FIRST(head); it; it = STAILQ_NEXT(it, next), ++i) { - memcpy(copied_info + i, it, sizeof(bss_info)); - } - - ESP8266WiFiClass::_scanResult = copied_info; - } - - } - - ESP8266WiFiClass::_scanStarted = false; - ESP8266WiFiClass::_scanComplete = true; - - if(!ESP8266WiFiClass::_scanAsync) { - esp_schedule(); - } -} - - -void * ESP8266WiFiClass::_getScanInfoByIndex(int i) { - if(!ESP8266WiFiClass::_scanResult || (size_t) i > ESP8266WiFiClass::_scanCount) { - return 0; - } - - return reinterpret_cast(ESP8266WiFiClass::_scanResult) + i; -} - - - - - - - -void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback_arg) { - if(ipaddr) - (*reinterpret_cast(callback_arg)) = ipaddr->addr; - esp_schedule(); // resume the hostByName function -} - - - -//-------------------------------------------------------------- - -void wifi_wps_status_cb(wps_cb_status status) { - DEBUGV("wps cb status: %d\r\n", status); - switch(status) { - case WPS_CB_ST_SUCCESS: - if(!wifi_wps_disable()) { - DEBUGV("wps disable failed\n"); - } - wifi_station_connect(); - break; - case WPS_CB_ST_FAILED: - DEBUGV("wps FAILED\n"); - break; - case WPS_CB_ST_TIMEOUT: - DEBUGV("wps TIMEOUT\n"); - break; - case WPS_CB_ST_WEP: - DEBUGV("wps WEP\n"); - break; - } - // todo user function to get status - - esp_schedule(); // resume the beginWPSConfig function -} - - -//-------------------------------------------------------------- - - -void ESP8266WiFiClass::_smartConfigCallback(uint32_t st, void* result) { - sc_status status = (sc_status) st; - if(status == SC_STATUS_LINK) { - station_config* sta_conf = reinterpret_cast(result); - - wifi_station_set_config(sta_conf); - wifi_station_disconnect(); - wifi_station_connect(); - - WiFi._smartConfigDone = true; - } else if(status == SC_STATUS_LINK_OVER) { - WiFi.stopSmartConfig(); - } -} - -//-------------------------------------------------------------- - - -//-------------------------------------------------------------- - -void ESP8266WiFiClass::_eventCallback(void* arg) { - System_Event_t* event = reinterpret_cast(arg); - DEBUGV("wifi evt: %d\r\n", event->event); - - if(event->event == EVENT_STAMODE_DISCONNECTED) { - WiFiClient::stopAll(); - } -} - - - -bool ESP8266WiFiClass::_scanAsync = false; -bool ESP8266WiFiClass::_scanStarted = false; -bool ESP8266WiFiClass::_scanComplete = false; - -size_t ESP8266WiFiClass::_scanCount = 0; -void* ESP8266WiFiClass::_scanResult = 0; - ESP8266WiFiClass WiFi; diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.h b/libraries/ESP8266WiFi/src/ESP8266WiFi.h index 815cd675a..9a9e433ac 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.h @@ -53,14 +53,8 @@ typedef enum { class ESP8266WiFiClass { public: - ESP8266WiFiClass(); - void persistent(bool persistent); - - void mode(WiFiMode); - WiFiMode getMode(); - // ---------------------------------------------------------------------------------------------- // ---------------------------------------- STA function ---------------------------------------- // ---------------------------------------------------------------------------------------------- @@ -144,6 +138,9 @@ class ESP8266WiFiClass { bool setPhyMode(WiFiPhyMode_t mode); WiFiPhyMode_t getPhyMode(); + void persistent(bool persistent); + void mode(WiFiMode); + WiFiMode getMode(); // ---------------------------------------------------------------------------------------------- // ------------------------------------ Generic Network function -------------------------------- From d521cea2322e4ad61e94144b5a72d46c33ca9a53 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Mon, 28 Dec 2015 17:55:38 +0100 Subject: [PATCH 05/21] reorder part 3 --- libraries/ESP8266WiFi/src/ESP8266WiFi.cpp | 15 ++++- libraries/ESP8266WiFi/src/ESP8266WiFi.h | 72 ++++++++++++++++++----- 2 files changed, 68 insertions(+), 19 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp index ee52e8ab4..0207f2ea8 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp @@ -106,12 +106,21 @@ static bool softap_config_equal(const softap_config& lhs, const softap_config& r // ---------------------------------------------------- ESP8266WiFiClass ------------------------------------------------- // ----------------------------------------------------------------------------------------------------------------------- -ESP8266WiFiClass::ESP8266WiFiClass() : - _smartConfigStarted(false), _smartConfigDone(false), _useStaticIp(false), _persistent(true) { +ESP8266WiFiClass::ESP8266WiFiClass() { + + _useStaticIp = false; + uint8 m = wifi_get_opmode(); - _useClientMode = (m & WIFI_STA); _useApMode = (m & WIFI_AP); + _useClientMode = (m & WIFI_STA); + + _persistent = true; + + _smartConfigStarted = false; + _smartConfigDone = false; + wifi_set_event_handler_cb((wifi_event_handler_cb_t) &ESP8266WiFiClass::_eventCallback); + } /** diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.h b/libraries/ESP8266WiFi/src/ESP8266WiFi.h index 9a9e433ac..bd2696213 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.h @@ -52,12 +52,25 @@ typedef enum { } WiFiSleepType_t; class ESP8266WiFiClass { + + // ---------------------------------------------------------------------------------------------- + // -------------------------------------- ESP8266WiFiClass -------------------------------------- + // ---------------------------------------------------------------------------------------------- + public: + ESP8266WiFiClass(); + protected: + + static void _eventCallback(void *event); + // ---------------------------------------------------------------------------------------------- // ---------------------------------------- STA function ---------------------------------------- // ---------------------------------------------------------------------------------------------- + + public: + int begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL); int begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL); int begin(); @@ -94,9 +107,16 @@ class ESP8266WiFiClass { int32_t RSSI(); + protected: + + bool _useStaticIp; + // ---------------------------------------------------------------------------------------------- // ----------------------------------------- AP function ---------------------------------------- // ---------------------------------------------------------------------------------------------- + + public: + void softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0); void softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet); int softAPdisconnect(bool wifioff = false); @@ -106,10 +126,14 @@ class ESP8266WiFiClass { uint8_t* softAPmacAddress(uint8_t* mac); String softAPmacAddress(void); + protected: + // ---------------------------------------------------------------------------------------------- // ----------------------------------------- scan function -------------------------------------- // ---------------------------------------------------------------------------------------------- + public: + int8_t scanNetworks(bool async = false, bool show_hidden = false); int8_t scanComplete(); @@ -126,10 +150,24 @@ class ESP8266WiFiClass { int32_t channel(uint8_t networkItem); bool isHidden(uint8_t networkItem); + protected: + + static bool _scanAsync; + static bool _scanStarted; + static bool _scanComplete; + + static size_t _scanCount; + static void* _scanResult; + + static void _scanDone(void* result, int status); + void * _getScanInfoByIndex(int i); + // ---------------------------------------------------------------------------------------------- // -------------------------------------- Generic WiFi function --------------------------------- // ---------------------------------------------------------------------------------------------- + public: + int32_t channel(void); bool setSleepMode(WiFiSleepType_t type); @@ -142,49 +180,51 @@ class ESP8266WiFiClass { void mode(WiFiMode); WiFiMode getMode(); + protected: + bool _useApMode; + bool _useClientMode; + bool _persistent; + void _mode(WiFiMode); + // ---------------------------------------------------------------------------------------------- // ------------------------------------ Generic Network function -------------------------------- // ---------------------------------------------------------------------------------------------- + public: + int hostByName(const char* aHostname, IPAddress& aResult); + protected: // ---------------------------------------------------------------------------------------------- // ------------------------------------ STA remote configure ----------------------------------- // ---------------------------------------------------------------------------------------------- + public: + bool beginWPSConfig(void); void beginSmartConfig(); bool smartConfigDone(); void stopSmartConfig(); + protected: + + bool _smartConfigStarted; + bool _smartConfigDone; + static void _smartConfigCallback(uint32_t status, void* result); // ---------------------------------------------------------------------------------------------- // ------------------------------------------- Debug -------------------------------------------- // ---------------------------------------------------------------------------------------------- + public: + void printDiag(Print& dest); friend class WiFiClient; friend class WiFiServer; - protected: - void _mode(WiFiMode); - static void _scanDone(void* result, int status); - void * _getScanInfoByIndex(int i); - static void _smartConfigCallback(uint32_t status, void* result); - static void _eventCallback(void *event);bool _smartConfigStarted;bool _smartConfigDone; - - bool _useApMode;bool _useClientMode;bool _useStaticIp;bool _persistent; - - static bool _scanAsync; - static bool _scanStarted; - static bool _scanComplete; - - static size_t _scanCount; - static void* _scanResult; - }; extern ESP8266WiFiClass WiFi; From 64326f9573cfcb45f5d64e749433024734b4bdd3 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Mon, 28 Dec 2015 19:30:56 +0100 Subject: [PATCH 06/21] rework mode management may fixes: #1138 --- libraries/ESP8266WiFi/src/ESP8266WiFi.cpp | 275 +++++++++++----------- libraries/ESP8266WiFi/src/ESP8266WiFi.h | 12 +- 2 files changed, 151 insertions(+), 136 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp index 0207f2ea8..b8742d9be 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp @@ -17,6 +17,9 @@ You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Reworked on 28 Dec 2015 by Markus Sattler + */ #include "ESP8266WiFi.h" @@ -110,10 +113,6 @@ ESP8266WiFiClass::ESP8266WiFiClass() { _useStaticIp = false; - uint8 m = wifi_get_opmode(); - _useApMode = (m & WIFI_AP); - _useClientMode = (m & WIFI_STA); - _persistent = true; _smartConfigStarted = false; @@ -153,14 +152,10 @@ void ESP8266WiFiClass::_eventCallback(void* arg) { * @return */ int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid) { - _useClientMode = true; - if(_useApMode) { - // turn on AP+STA mode - _mode(WIFI_AP_STA); - } else { - // turn on STA mode - _mode(WIFI_STA); + if(!enableSTA(true)) { + // enable STA failed + return WL_CONNECT_FAILED; } if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) { @@ -197,10 +192,11 @@ int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t ch } ETS_UART_INTR_DISABLE(); - if(_persistent) + if(_persistent) { wifi_station_set_config(&conf); - else + } else { wifi_station_set_config_current(&conf); + } wifi_station_connect(); ETS_UART_INTR_ENABLE(); @@ -208,8 +204,10 @@ int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t ch wifi_set_channel(channel); } - if(!_useStaticIp) + if(!_useStaticIp) { wifi_station_dhcpc_start(); + } + return status(); } @@ -222,13 +220,20 @@ int ESP8266WiFiClass::begin(char* ssid, char *passphrase, int32_t channel, const * @return wl_status_t */ int ESP8266WiFiClass::begin() { + + if(!enableSTA(true)) { + // enable STA failed + return WL_CONNECT_FAILED; + } + ETS_UART_INTR_DISABLE(); wifi_station_connect(); ETS_UART_INTR_ENABLE(); // TODO is static ip not stored in SDK? - if(!_useStaticIp) + if(!_useStaticIp) { wifi_station_dhcpc_start(); + } return status(); } @@ -240,6 +245,11 @@ int ESP8266WiFiClass::begin() { * @param subnet Static Subnet mask */ void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { + + if(!enableSTA(true)) { + return; + } + struct ip_info info; info.ip.addr = static_cast(local_ip); info.gw.addr = static_cast(gateway); @@ -285,25 +295,20 @@ int ESP8266WiFiClass::disconnect(bool wifioff) { struct station_config conf; *conf.ssid = 0; *conf.password = 0; + ETS_UART_INTR_DISABLE(); - if(_persistent) + if(_persistent) { wifi_station_set_config(&conf); - else + } else { wifi_station_set_config_current(&conf); + } wifi_station_disconnect(); ETS_UART_INTR_ENABLE(); if(wifioff) { - _useClientMode = false; - - if(_useApMode) { - // turn on AP - _mode(WIFI_AP); - } else { - // turn wifi off - _mode(WIFI_OFF); - } + enableSTA(false); } + //TODO return with more meaning ? return 0; } @@ -436,16 +441,17 @@ bool ESP8266WiFiClass::hostname(String aHostname) { wl_status_t ESP8266WiFiClass::status() { int status = wifi_station_get_connect_status(); - if(status == STATION_GOT_IP) + if(status == STATION_GOT_IP) { return WL_CONNECTED; - else if(status == STATION_NO_AP_FOUND) + } else if(status == STATION_NO_AP_FOUND) { return WL_NO_SSID_AVAIL; - else if(status == STATION_CONNECT_FAIL || status == STATION_WRONG_PASSWORD) + } else if(status == STATION_CONNECT_FAIL || status == STATION_WRONG_PASSWORD) { return WL_CONNECT_FAILED; - else if(status == STATION_IDLE) + } else if(status == STATION_IDLE) { return WL_IDLE_STATUS; - else + } else { return WL_DISCONNECTED; + } } /** @@ -453,8 +459,7 @@ wl_status_t ESP8266WiFiClass::status() { * @return SSID */ String ESP8266WiFiClass::SSID() const { - // TODO why static, needs RAM for nothing? - static struct station_config conf; + struct station_config conf; wifi_station_get_config(&conf); return String(reinterpret_cast(conf.ssid)); } @@ -464,7 +469,7 @@ String ESP8266WiFiClass::SSID() const { * @return psk string */ String ESP8266WiFiClass::psk() const { - static struct station_config conf; + struct station_config conf; wifi_station_get_config(&conf); return String(reinterpret_cast(conf.password)); } @@ -484,7 +489,7 @@ uint8_t* ESP8266WiFiClass::BSSID(void) { * @return String bssid mac */ String ESP8266WiFiClass::BSSIDstr(void) { - static struct station_config conf; + struct station_config conf; char mac[18] = { 0 }; wifi_station_get_config(&conf); sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", conf.bssid[0], conf.bssid[1], conf.bssid[2], conf.bssid[3], conf.bssid[4], conf.bssid[5]); @@ -512,13 +517,10 @@ int32_t ESP8266WiFiClass::RSSI(void) { * @param ssid_hidden Network cloaking (0 = broadcast SSID, 1 = hide SSID) */ void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden) { - _useApMode = true; - if(_useClientMode) { - // turn on AP+STA mode - _mode(WIFI_AP_STA); - } else { - // turn on STA mode - _mode(WIFI_AP); + + if(!enableAP(true)) { + // enable AP failed + return; } if(!ssid || *ssid == 0 || strlen(ssid) > 31) { @@ -571,6 +573,12 @@ void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase, int chan * @param subnet subnet mask */ void ESP8266WiFiClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { + + if(!enableAP(true)) { + // enable AP failed + return; + } + struct ip_info info; info.ip.addr = static_cast(local_ip); info.gw.addr = static_cast(gateway); @@ -599,15 +607,7 @@ int ESP8266WiFiClass::softAPdisconnect(bool wifioff) { ETS_UART_INTR_ENABLE(); if(wifioff) { - _useApMode = false; - - if(_useClientMode) { - // turn on STA - _mode(WIFI_STA); - } else { - // turn wifi off - _mode(WIFI_OFF); - } + enableAP(false); } //TODO return with more meaning ? @@ -673,17 +673,11 @@ int8_t ESP8266WiFiClass::scanNetworks(bool async, bool show_hidden) { ESP8266WiFiClass::_scanAsync = async; - if(_useApMode) { - // turn on AP+STA mode - _mode(WIFI_AP_STA); - } else { - // turn on STA mode - _mode(WIFI_STA); - } + enableSTA(true); int status = wifi_station_get_connect_status(); if(status != STATION_GOT_IP && status != STATION_IDLE) { - disconnect(); + disconnect(false); } scanDelete(); @@ -756,8 +750,9 @@ void ESP8266WiFiClass::scanDelete() { */ bool ESP8266WiFiClass::getNetworkInfo(uint8_t i, String &ssid, uint8_t &encType, int32_t &rssi, uint8_t* &bssid, int32_t &channel, bool &isHidden) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) + if(!it) { return false; + } ssid = (const char*) it->ssid; encType = encryptionType(i); @@ -777,8 +772,9 @@ bool ESP8266WiFiClass::getNetworkInfo(uint8_t i, String &ssid, uint8_t &encType, */ String ESP8266WiFiClass::SSID(uint8_t i) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) + if(!it) { return ""; + } return String(reinterpret_cast(it->ssid)); } @@ -791,21 +787,24 @@ String ESP8266WiFiClass::SSID(uint8_t i) { */ uint8_t ESP8266WiFiClass::encryptionType(uint8_t i) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) + if(!it) { return -1; + } - int authmode = it->authmode; - if(authmode == AUTH_OPEN) - return ENC_TYPE_NONE; - if(authmode == AUTH_WEP) - return ENC_TYPE_WEP; - if(authmode == AUTH_WPA_PSK) - return ENC_TYPE_TKIP; - if(authmode == AUTH_WPA2_PSK) - return ENC_TYPE_CCMP; - if(authmode == AUTH_WPA_WPA2_PSK) - return ENC_TYPE_AUTO; - return -1; + switch(it->authmode) { + case AUTH_OPEN: + return ENC_TYPE_NONE; + case AUTH_WEP: + return ENC_TYPE_WEP; + case AUTH_WPA_PSK: + return ENC_TYPE_TKIP; + case AUTH_WPA2_PSK: + return ENC_TYPE_CCMP; + case AUTH_WPA_WPA2_PSK: + return ENC_TYPE_AUTO; + default: + return -1; + } } /** @@ -815,9 +814,9 @@ uint8_t ESP8266WiFiClass::encryptionType(uint8_t i) { */ int32_t ESP8266WiFiClass::RSSI(uint8_t i) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) + if(!it) { return 0; - + } return it->rssi; } @@ -829,9 +828,9 @@ int32_t ESP8266WiFiClass::RSSI(uint8_t i) { */ uint8_t * ESP8266WiFiClass::BSSID(uint8_t i) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) + if(!it) { return 0; - + } return it->bssid; } @@ -843,18 +842,18 @@ uint8_t * ESP8266WiFiClass::BSSID(uint8_t i) { String ESP8266WiFiClass::BSSIDstr(uint8_t i) { char mac[18] = { 0 }; struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) + if(!it) { return String(""); - + } sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", it->bssid[0], it->bssid[1], it->bssid[2], it->bssid[3], it->bssid[4], it->bssid[5]); return String(mac); } int32_t ESP8266WiFiClass::channel(uint8_t i) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) + if(!it) { return 0; - + } return it->channel; } @@ -865,9 +864,9 @@ int32_t ESP8266WiFiClass::channel(uint8_t i) { */ bool ESP8266WiFiClass::isHidden(uint8_t i) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) + if(!it) { return false; - + } return (it->is_hidden != 0); } @@ -984,56 +983,77 @@ void ESP8266WiFiClass::persistent(bool persistent) { /** * set new mode - * @param m WiFiMode + * @param m WiFiMode_t */ -void ESP8266WiFiClass::mode(WiFiMode m) { +bool ESP8266WiFiClass::mode(WiFiMode_t m) { if(wifi_get_opmode() == (uint8) m) { - return; + return true; } - if((m & WIFI_AP)) { - _useApMode = true; + bool ret = false; + + ETS_UART_INTR_DISABLE(); + if(_persistent) { + ret = wifi_set_opmode(m); } else { - _useApMode = false; + ret = wifi_set_opmode_current(m); } + ETS_UART_INTR_ENABLE(); - if((m & WIFI_STA)) { - _useClientMode = true; - } else { - _useClientMode = false; - } - - _mode(m); + return ret; } /** * get WiFi mode * @return WiFiMode */ -WiFiMode ESP8266WiFiClass::getMode() { - return (WiFiMode) wifi_get_opmode(); +WiFiMode_t ESP8266WiFiClass::getMode() { + return (WiFiMode_t) wifi_get_opmode(); } - /** - * private - * internal mode handling. - * @param m WiFiMode + * control STA mode + * @param enable bool + * @return ok */ -void ESP8266WiFiClass::_mode(WiFiMode m) { - if(wifi_get_opmode() == (uint8) m) { - return; +bool ESP8266WiFiClass::enableSTA(bool enable) { + + WiFiMode_t currentMode = (WiFiMode_t) wifi_get_opmode(); + bool isEnabled = ((currentMode & WIFI_STA) != 0); + + if(isEnabled != enable) { + if(enable) { + return mode((WiFiMode_t)(currentMode & WIFI_STA)); + } else { + return mode((WiFiMode_t)(currentMode & (~WIFI_STA))); + } + } else { + return true; } - - ETS_UART_INTR_DISABLE(); - if(_persistent) - wifi_set_opmode(m); - else - wifi_set_opmode_current(m); - ETS_UART_INTR_ENABLE(); - } +/** + * control AP mode + * @param enable bool + * @return ok + */ +bool ESP8266WiFiClass::enableAP(bool enable){ + + WiFiMode_t currentMode = (WiFiMode_t) wifi_get_opmode(); + bool isEnabled = ((currentMode & WIFI_AP) != 0); + + if(isEnabled != enable) { + if(enable) { + return mode((WiFiMode_t)(currentMode & WIFI_AP)); + } else { + return mode((WiFiMode_t)(currentMode & (~WIFI_AP))); + } + } else { + return true; + } +} + + // ----------------------------------------------------------------------------------------------------------------------- // ------------------------------------------------ Generic Network function --------------------------------------------- // ----------------------------------------------------------------------------------------------------------------------- @@ -1087,14 +1107,9 @@ void wifi_wps_status_cb(wps_cb_status status); */ bool ESP8266WiFiClass::beginWPSConfig(void) { - _useClientMode = true; - - if(_useApMode) { - // turn on AP+STA mode - _mode(WIFI_AP_STA); - } else { - // turn on STA mode - _mode(WIFI_STA); + if(!enableSTA(true)) { + // enable STA failed + return false; } disconnect(); @@ -1163,18 +1178,14 @@ void ESP8266WiFiClass::beginSmartConfig() { if(_smartConfigStarted) return; - if(_useApMode) { - // turn on AP+STA mode - _mode(WIFI_AP_STA); - } else { - // turn on STA mode - _mode(WIFI_STA); + if(!enableSTA(true)) { + // enable STA failed + return; } _smartConfigStarted = true; _smartConfigDone = false; - //SC_TYPE_ESPTOUCH use ESPTOUCH for smartconfig, or use SC_TYPE_AIRKISS for AIRKISS smartconfig_start(reinterpret_cast(&ESP8266WiFiClass::_smartConfigCallback), 1); } @@ -1183,8 +1194,9 @@ void ESP8266WiFiClass::beginSmartConfig() { * Stop SmartConfig */ void ESP8266WiFiClass::stopSmartConfig() { - if(!_smartConfigStarted) + if(!_smartConfigStarted) { return; + } smartconfig_stop(); _smartConfigStarted = false; @@ -1195,8 +1207,9 @@ void ESP8266WiFiClass::stopSmartConfig() { * @return smartConfig Done */ bool ESP8266WiFiClass::smartConfigDone() { - if(!_smartConfigStarted) + if(!_smartConfigStarted) { return false; + } return _smartConfigDone; } diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.h b/libraries/ESP8266WiFi/src/ESP8266WiFi.h index bd2696213..3bd23b016 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.h @@ -43,6 +43,8 @@ enum WiFiMode { WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3 }; +typedef enum WiFiMode WiFiMode_t; + typedef enum { WIFI_PHY_MODE_11B = 1, WIFI_PHY_MODE_11G = 2, WIFI_PHY_MODE_11N = 3 } WiFiPhyMode_t; @@ -177,14 +179,14 @@ class ESP8266WiFiClass { WiFiPhyMode_t getPhyMode(); void persistent(bool persistent); - void mode(WiFiMode); - WiFiMode getMode(); + bool mode(WiFiMode_t); + WiFiMode_t getMode(); + + bool enableSTA(bool enable); + bool enableAP(bool enable); protected: - bool _useApMode; - bool _useClientMode; bool _persistent; - void _mode(WiFiMode); // ---------------------------------------------------------------------------------------------- // ------------------------------------ Generic Network function -------------------------------- From cee4ef307856858eb624a2dcd9f236ef3ca9ab50 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Mon, 28 Dec 2015 19:46:20 +0100 Subject: [PATCH 07/21] less static stuff --- libraries/ESP8266WiFi/src/ESP8266WiFi.cpp | 8 ++++---- libraries/ESP8266WiFi/src/ESP8266WiFi.h | 1 + tools/sdk/include/user_interface.h | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp index b8742d9be..ee38217ef 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp @@ -230,7 +230,6 @@ int ESP8266WiFiClass::begin() { wifi_station_connect(); ETS_UART_INTR_ENABLE(); - // TODO is static ip not stored in SDK? if(!_useStaticIp) { wifi_station_dhcpc_start(); } @@ -600,10 +599,11 @@ int ESP8266WiFiClass::softAPdisconnect(bool wifioff) { *conf.ssid = 0; *conf.password = 0; ETS_UART_INTR_DISABLE(); - if(_persistent) + if(_persistent) { wifi_softap_set_config(&conf); - else + } else { wifi_softap_set_config_current(&conf); + } ETS_UART_INTR_ENABLE(); if(wifioff) { @@ -1266,7 +1266,7 @@ void ESP8266WiFiClass::printDiag(Print& p) { p.print("Auto connect: "); p.println(wifi_station_get_auto_connect()); - static struct station_config conf; + struct station_config conf; wifi_station_get_config(&conf); const char* ssid = reinterpret_cast(conf.ssid); diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.h b/libraries/ESP8266WiFi/src/ESP8266WiFi.h index 3bd23b016..ebdfe4b65 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.h @@ -179,6 +179,7 @@ class ESP8266WiFiClass { WiFiPhyMode_t getPhyMode(); void persistent(bool persistent); + bool mode(WiFiMode_t); WiFiMode_t getMode(); diff --git a/tools/sdk/include/user_interface.h b/tools/sdk/include/user_interface.h index c29443e54..5ecf3a868 100644 --- a/tools/sdk/include/user_interface.h +++ b/tools/sdk/include/user_interface.h @@ -164,7 +164,7 @@ bool wifi_set_opmode_current(uint8 opmode); uint8 wifi_get_broadcast_if(void); bool wifi_set_broadcast_if(uint8 interface); -struct bss_info { +typedef struct bss_info { STAILQ_ENTRY(bss_info) next; uint8 bssid[6]; @@ -177,7 +177,7 @@ struct bss_info { sint16 freq_offset; sint16 freqcal_val; uint8 *esp_mesh_ie; -}; +} bss_info_t; typedef struct _scaninfo { STAILQ_HEAD(, bss_info) *pbss; From 6f00503bc37acd89fd4305151ac02e817fd06124 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Mon, 28 Dec 2015 20:19:36 +0100 Subject: [PATCH 08/21] add reconnect function for STA --- libraries/ESP8266WiFi/src/ESP8266WiFi.cpp | 12 ++++++++++++ libraries/ESP8266WiFi/src/ESP8266WiFi.h | 2 ++ 2 files changed, 14 insertions(+) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp index ee38217ef..870e99cec 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp @@ -284,6 +284,18 @@ void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress s _useStaticIp = true; } +/** + * will force a disconnect an then start reconnecting to AP + * @return ok + */ +bool ESP8266WiFiClass::reconnect() { + if((getMode() & WIFI_STA) != 0) { + if(wifi_station_disconnect()) { + return wifi_station_connect(); + } + } + return false; +} /** * Disconnect from the network diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.h b/libraries/ESP8266WiFi/src/ESP8266WiFi.h index ebdfe4b65..dbadb135e 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.h @@ -80,6 +80,8 @@ class ESP8266WiFiClass { void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet); void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns); + + bool reconnect(); int disconnect(bool wifioff = false); uint8_t waitForConnectResult(); From fd443d4e17ea7f896c7968153ca30795244c3c0d Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Tue, 29 Dec 2015 14:03:15 +0100 Subject: [PATCH 09/21] split ESP8266WiFiClass in different sub classes for better overview. --- libraries/ESP8266WiFi/src/ESP8266WiFi.cpp | 1209 ----------------- libraries/ESP8266WiFi/src/ESP8266WiFi.h | 202 +-- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 211 +++ libraries/ESP8266WiFi/src/ESP8266WiFiAP.h | 52 + .../ESP8266WiFi/src/ESP8266WiFiGeneric.cpp | 244 ++++ .../ESP8266WiFi/src/ESP8266WiFiGeneric.h | 73 + libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp | 606 +++++++++ libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h | 101 ++ libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp | 323 +++++ libraries/ESP8266WiFi/src/ESP8266WiFiScan.h | 70 + libraries/ESP8266WiFi/src/ESP8266WiFiType.h | 52 + 11 files changed, 1753 insertions(+), 1390 deletions(-) create mode 100644 libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp create mode 100644 libraries/ESP8266WiFi/src/ESP8266WiFiAP.h create mode 100644 libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp create mode 100644 libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h create mode 100644 libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp create mode 100644 libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h create mode 100644 libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp create mode 100644 libraries/ESP8266WiFi/src/ESP8266WiFiScan.h create mode 100644 libraries/ESP8266WiFi/src/ESP8266WiFiType.h diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp index 870e99cec..5ce0dab93 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp @@ -37,1217 +37,8 @@ extern "C" { #include "lwip/dns.h" } -#include "WiFiClient.h" -#include "WiFiUdp.h" #include "debug.h" -extern "C" void esp_schedule(); -extern "C" void esp_yield(); - - - -extern "C" { -typedef STAILQ_HEAD(, bss_info) -bss_info_head_t; -} - - -// ----------------------------------------------------------------------------------------------------------------------- -// ---------------------------------------------------- Private functions ------------------------------------------------ -// ----------------------------------------------------------------------------------------------------------------------- - -static bool sta_config_equal(const station_config& lhs, const station_config& rhs); -static bool softap_config_equal(const softap_config& lhs, const softap_config& rhs); - - -/** - * compare two STA configurations - * @param lhs station_config - * @param rhs station_config - * @return equal - */ -static bool sta_config_equal(const station_config& lhs, const station_config& rhs) { - if(strcmp(reinterpret_cast(lhs.ssid), reinterpret_cast(rhs.ssid)) != 0) - return false; - - if(strcmp(reinterpret_cast(lhs.password), reinterpret_cast(rhs.password)) != 0) - return false; - - if(lhs.bssid_set) { - if(!rhs.bssid_set) - return false; - - if(memcmp(lhs.bssid, rhs.bssid, 6) != 0) - return false; - } else { - if(rhs.bssid_set) - return false; - } - - return true; -} - -/** - * compare two AP configurations - * @param lhs softap_config - * @param rhs softap_config - * @return equal - */ -static bool softap_config_equal(const softap_config& lhs, const softap_config& rhs) { - if(strcmp(reinterpret_cast(lhs.ssid), reinterpret_cast(rhs.ssid)) != 0) - return false; - if(strcmp(reinterpret_cast(lhs.password), reinterpret_cast(rhs.password)) != 0) - return false; - if(lhs.channel != rhs.channel) - return false; - if(lhs.ssid_hidden != rhs.ssid_hidden) - return false; - return true; -} - -// ----------------------------------------------------------------------------------------------------------------------- -// ---------------------------------------------------- ESP8266WiFiClass ------------------------------------------------- -// ----------------------------------------------------------------------------------------------------------------------- - -ESP8266WiFiClass::ESP8266WiFiClass() { - - _useStaticIp = false; - - _persistent = true; - - _smartConfigStarted = false; - _smartConfigDone = false; - - wifi_set_event_handler_cb((wifi_event_handler_cb_t) &ESP8266WiFiClass::_eventCallback); - -} - -/** - * callback for WiFi events - * @param arg - */ -void ESP8266WiFiClass::_eventCallback(void* arg) { - System_Event_t* event = reinterpret_cast(arg); - DEBUGV("wifi evt: %d\r\n", event->event); - - if(event->event == EVENT_STAMODE_DISCONNECTED) { - WiFiClient::stopAll(); - } - - //TODO allow user to hook this event -} - - -// ----------------------------------------------------------------------------------------------------------------------- -// ---------------------------------------------------- STA function ----------------------------------------------------- -// ----------------------------------------------------------------------------------------------------------------------- - -/** - * Start Wifi connection - * if passphrase is set the most secure supported mode will be automatically selected - * @param ssid const char* Pointer to the SSID string. - * @param passphrase const char * Optional. Passphrase. Valid characters in a passphrase must be between ASCII 32-126 (decimal). - * @param bssid uint8_t[6] Optional. BSSID / MAC of AP - * @param channel Optional. Channel of AP - * @return - */ -int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid) { - - if(!enableSTA(true)) { - // enable STA failed - return WL_CONNECT_FAILED; - } - - if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) { - // fail SSID too long or missing! - return WL_CONNECT_FAILED; - } - - if(passphrase && strlen(passphrase) > 63) { - // fail passphrase too long! - return WL_CONNECT_FAILED; - } - - struct station_config conf; - strcpy(reinterpret_cast(conf.ssid), ssid); - - if(passphrase) { - strcpy(reinterpret_cast(conf.password), passphrase); - } else { - *conf.password = 0; - } - - if(bssid) { - conf.bssid_set = 1; - memcpy((void *) &conf.bssid[0], (void *) bssid, 6); - } else { - conf.bssid_set = 0; - } - - struct station_config current_conf; - wifi_station_get_config(¤t_conf); - if(sta_config_equal(current_conf, conf)) { - DEBUGV("sta config unchanged"); - return status(); - } - - ETS_UART_INTR_DISABLE(); - if(_persistent) { - wifi_station_set_config(&conf); - } else { - wifi_station_set_config_current(&conf); - } - wifi_station_connect(); - ETS_UART_INTR_ENABLE(); - - if(channel > 0 && channel <= 13) { - wifi_set_channel(channel); - } - - if(!_useStaticIp) { - wifi_station_dhcpc_start(); - } - - return status(); -} - -int ESP8266WiFiClass::begin(char* ssid, char *passphrase, int32_t channel, const uint8_t* bssid) { - return begin((const char*) ssid, (const char*) passphrase, channel, bssid); -} - -/** - * Use to connect to SDK config. - * @return wl_status_t - */ -int ESP8266WiFiClass::begin() { - - if(!enableSTA(true)) { - // enable STA failed - return WL_CONNECT_FAILED; - } - - ETS_UART_INTR_DISABLE(); - wifi_station_connect(); - ETS_UART_INTR_ENABLE(); - - if(!_useStaticIp) { - wifi_station_dhcpc_start(); - } - return status(); -} - - -/** - * Change IP configuration settings disabling the dhcp client - * @param local_ip Static ip configuration - * @param gateway Static gateway configuration - * @param subnet Static Subnet mask - */ -void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { - - if(!enableSTA(true)) { - return; - } - - struct ip_info info; - info.ip.addr = static_cast(local_ip); - info.gw.addr = static_cast(gateway); - info.netmask.addr = static_cast(subnet); - - wifi_station_dhcpc_stop(); - wifi_set_ip_info(STATION_IF, &info); - - _useStaticIp = true; -} - -/** - * Change IP configuration settings disabling the dhcp client - * @param local_ip Static ip configuration - * @param gateway Static gateway configuration - * @param subnet Static Subnet mask - * @param dns Static DNS server - */ -void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns) { - struct ip_info info; - info.ip.addr = static_cast(local_ip); - info.gw.addr = static_cast(gateway); - info.netmask.addr = static_cast(subnet); - - wifi_station_dhcpc_stop(); - wifi_set_ip_info(STATION_IF, &info); - - // Set DNS-Server - ip_addr_t d; - d.addr = static_cast(dns); - dns_setserver(0, &d); - - _useStaticIp = true; -} - -/** - * will force a disconnect an then start reconnecting to AP - * @return ok - */ -bool ESP8266WiFiClass::reconnect() { - if((getMode() & WIFI_STA) != 0) { - if(wifi_station_disconnect()) { - return wifi_station_connect(); - } - } - return false; -} - -/** - * Disconnect from the network - * @param wifioff - * @return one value of wl_status_t enum - */ -int ESP8266WiFiClass::disconnect(bool wifioff) { - struct station_config conf; - *conf.ssid = 0; - *conf.password = 0; - - ETS_UART_INTR_DISABLE(); - if(_persistent) { - wifi_station_set_config(&conf); - } else { - wifi_station_set_config_current(&conf); - } - wifi_station_disconnect(); - ETS_UART_INTR_ENABLE(); - - if(wifioff) { - enableSTA(false); - } - - //TODO return with more meaning ? - return 0; -} - -/** - * Wait for WiFi connection to reach a result - * returns the status reached or disconnect if STA is off - * @return wl_status_t - */ -uint8_t ESP8266WiFiClass::waitForConnectResult() { - //1 and 3 have STA enabled - if((wifi_get_opmode() & 1) == 0) { - return WL_DISCONNECTED; - } - while(status() == WL_DISCONNECTED) { - delay(100); - } - return status(); -} - -/** - * Get the station interface IP address. - * @return IPAddress station IP - */ -IPAddress ESP8266WiFiClass::localIP() { - struct ip_info ip; - wifi_get_ip_info(STATION_IF, &ip); - return IPAddress(ip.ip.addr); -} - - -/** - * Get the station interface MAC address. - * @param mac pointer to uint8_t array with length WL_MAC_ADDR_LENGTH - * @return pointer to uint8_t * - */ -uint8_t* ESP8266WiFiClass::macAddress(uint8_t* mac) { - wifi_get_macaddr(STATION_IF, mac); - return mac; -} - -/** - * Get the station interface MAC address. - * @return String mac - */ -String ESP8266WiFiClass::macAddress(void) { - uint8_t mac[6]; - char macStr[18] = { 0 }; - wifi_get_macaddr(STATION_IF, mac); - - sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); - return String(macStr); -} - -/** - * Get the interface subnet mask address. - * @return IPAddress subnetMask - */ -IPAddress ESP8266WiFiClass::subnetMask() { - struct ip_info ip; - wifi_get_ip_info(STATION_IF, &ip); - return IPAddress(ip.netmask.addr); -} - -/** - * Get the gateway ip address. - * @return IPAddress gatewayIP - */ -IPAddress ESP8266WiFiClass::gatewayIP() { - struct ip_info ip; - wifi_get_ip_info(STATION_IF, &ip); - return IPAddress(ip.gw.addr); -} - -/** - * Get the DNS ip address. - * @param dns_no - * @return IPAddress DNS Server IP - */ -IPAddress ESP8266WiFiClass::dnsIP(uint8_t dns_no) { - ip_addr_t dns_ip = dns_getserver(dns_no); - return IPAddress(dns_ip.addr); -} - - -/** - * Get ESP8266 station DHCP hostname - * @return hostname - */ -String ESP8266WiFiClass::hostname(void) { - return String(wifi_station_get_hostname()); -} - - -/** - * Set ESP8266 station DHCP hostname - * @param aHostname max length:32 - * @return ok - */ -bool ESP8266WiFiClass::hostname(char* aHostname) { - if(strlen(aHostname) > 32) { - return false; - } - return wifi_station_set_hostname(aHostname); -} - -/** - * Set ESP8266 station DHCP hostname - * @param aHostname max length:32 - * @return ok - */ -bool ESP8266WiFiClass::hostname(const char* aHostname) { - return hostname((char*) aHostname); -} - -/** - * Set ESP8266 station DHCP hostname - * @param aHostname max length:32 - * @return ok - */ -bool ESP8266WiFiClass::hostname(String aHostname) { - return hostname((char*) aHostname.c_str()); -} - -/** - * Return Connection status. - * @return one of the value defined in wl_status_t - * - */ -wl_status_t ESP8266WiFiClass::status() { - int status = wifi_station_get_connect_status(); - - if(status == STATION_GOT_IP) { - return WL_CONNECTED; - } else if(status == STATION_NO_AP_FOUND) { - return WL_NO_SSID_AVAIL; - } else if(status == STATION_CONNECT_FAIL || status == STATION_WRONG_PASSWORD) { - return WL_CONNECT_FAILED; - } else if(status == STATION_IDLE) { - return WL_IDLE_STATUS; - } else { - return WL_DISCONNECTED; - } -} - -/** - * Return the current SSID associated with the network - * @return SSID - */ -String ESP8266WiFiClass::SSID() const { - struct station_config conf; - wifi_station_get_config(&conf); - return String(reinterpret_cast(conf.ssid)); -} - -/** - * Return the current pre shared key associated with the network - * @return psk string - */ -String ESP8266WiFiClass::psk() const { - struct station_config conf; - wifi_station_get_config(&conf); - return String(reinterpret_cast(conf.password)); -} - -/** - * Return the current bssid / mac associated with the network if configured - * @return bssid uint8_t * - */ -uint8_t* ESP8266WiFiClass::BSSID(void) { - static struct station_config conf; - wifi_station_get_config(&conf); - return reinterpret_cast(conf.bssid); -} - -/** - * Return the current bssid / mac associated with the network if configured - * @return String bssid mac - */ -String ESP8266WiFiClass::BSSIDstr(void) { - struct station_config conf; - char mac[18] = { 0 }; - wifi_station_get_config(&conf); - sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", conf.bssid[0], conf.bssid[1], conf.bssid[2], conf.bssid[3], conf.bssid[4], conf.bssid[5]); - return String(mac); -} - -/** - * Return the current network RSSI. - * @return RSSI value - */ -int32_t ESP8266WiFiClass::RSSI(void) { - return wifi_station_get_rssi(); -} - -// ----------------------------------------------------------------------------------------------------------------------- -// ----------------------------------------------------- AP function ----------------------------------------------------- -// ----------------------------------------------------------------------------------------------------------------------- - - -/** - * 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) - */ -void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden) { - - if(!enableAP(true)) { - // enable AP failed - return; - } - - if(!ssid || *ssid == 0 || strlen(ssid) > 31) { - // fail SSID too long or missing! - return; - } - - if(passphrase && strlen(passphrase) > 63) { - // fail passphrase to long! - return; - } - - struct softap_config conf; - wifi_softap_get_config(&conf); - strcpy(reinterpret_cast(conf.ssid), ssid); - conf.channel = channel; - conf.ssid_len = strlen(ssid); - conf.ssid_hidden = ssid_hidden; - conf.max_connection = 4; - 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(conf.password), passphrase); - } - - struct softap_config conf_current; - wifi_softap_get_config(&conf_current); - if(softap_config_equal(conf, conf_current)) { - DEBUGV("softap config unchanged"); - return; - } - - ETS_UART_INTR_DISABLE(); - if(_persistent) - wifi_softap_set_config(&conf); - else - wifi_softap_set_config_current(&conf); - ETS_UART_INTR_ENABLE(); -} - - -/** - * Configure access point - * @param local_ip access point IP - * @param gateway gateway IP - * @param subnet subnet mask - */ -void ESP8266WiFiClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { - - if(!enableAP(true)) { - // enable AP failed - return; - } - - struct ip_info info; - info.ip.addr = static_cast(local_ip); - info.gw.addr = static_cast(gateway); - info.netmask.addr = static_cast(subnet); - wifi_softap_dhcps_stop(); - wifi_set_ip_info(SOFTAP_IF, &info); - wifi_softap_dhcps_start(); -} - - - -/** - * Disconnect from the network (close AP) - * @param wifioff disable mode? - * @return one value of wl_status_t enum - */ -int ESP8266WiFiClass::softAPdisconnect(bool wifioff) { - struct softap_config conf; - *conf.ssid = 0; - *conf.password = 0; - ETS_UART_INTR_DISABLE(); - if(_persistent) { - wifi_softap_set_config(&conf); - } else { - wifi_softap_set_config_current(&conf); - } - ETS_UART_INTR_ENABLE(); - - if(wifioff) { - enableAP(false); - } - - //TODO return with more meaning ? - return 0; -} - -/** - * Get the softAP interface IP address. - * @return IPAddress softAP IP - */ -IPAddress ESP8266WiFiClass::softAPIP() { - struct ip_info ip; - wifi_get_ip_info(SOFTAP_IF, &ip); - return IPAddress(ip.ip.addr); -} - - -/** - * Get the softAP interface MAC address. - * @param mac pointer to uint8_t array with length WL_MAC_ADDR_LENGTH - * @return pointer to uint8_t* - */ -uint8_t* ESP8266WiFiClass::softAPmacAddress(uint8_t* mac) { - wifi_get_macaddr(SOFTAP_IF, mac); - return mac; -} - -/** - * Get the softAP interface MAC address. - * @return String mac - */ -String ESP8266WiFiClass::softAPmacAddress(void) { - uint8_t mac[6]; - char macStr[18] = { 0 }; - wifi_get_macaddr(SOFTAP_IF, mac); - - sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); - return String(macStr); -} - - -// ----------------------------------------------------------------------------------------------------------------------- -// ----------------------------------------------------- scan function --------------------------------------------------- -// ----------------------------------------------------------------------------------------------------------------------- - -bool ESP8266WiFiClass::_scanAsync = false; -bool ESP8266WiFiClass::_scanStarted = false; -bool ESP8266WiFiClass::_scanComplete = false; - -size_t ESP8266WiFiClass::_scanCount = 0; -void* ESP8266WiFiClass::_scanResult = 0; - -/** - * Start scan WiFi networks available - * @param async run in async mode - * @param show_hidden show hidden networks - * @return Number of discovered networks - */ -int8_t ESP8266WiFiClass::scanNetworks(bool async, bool show_hidden) { - if(ESP8266WiFiClass::_scanStarted) { - return WIFI_SCAN_RUNNING; - } - - ESP8266WiFiClass::_scanAsync = async; - - enableSTA(true); - - int status = wifi_station_get_connect_status(); - if(status != STATION_GOT_IP && status != STATION_IDLE) { - disconnect(false); - } - - scanDelete(); - - struct scan_config config; - config.ssid = 0; - config.bssid = 0; - config.channel = 0; - config.show_hidden = show_hidden; - if(wifi_station_scan(&config, reinterpret_cast(&ESP8266WiFiClass::_scanDone))) { - ESP8266WiFiClass::_scanComplete = false; - ESP8266WiFiClass::_scanStarted = true; - - if(ESP8266WiFiClass::_scanAsync) { - delay(0); // time for the OS to trigger the scan - return WIFI_SCAN_RUNNING; - } - - esp_yield(); - return ESP8266WiFiClass::_scanCount; - } else { - return WIFI_SCAN_FAILED; - } - -} - - -/** - * called to get the scan state in Async mode - * @return scan result or status - * -1 if scan not fin - * -2 if scan not triggered - */ -int8_t ESP8266WiFiClass::scanComplete() { - - if(_scanStarted) { - return WIFI_SCAN_RUNNING; - } - - if(_scanComplete) { - return ESP8266WiFiClass::_scanCount; - } - - return WIFI_SCAN_FAILED; -} - -/** - * delete last scan result from RAM - */ -void ESP8266WiFiClass::scanDelete() { - if(ESP8266WiFiClass::_scanResult) { - delete[] reinterpret_cast(ESP8266WiFiClass::_scanResult); - ESP8266WiFiClass::_scanResult = 0; - ESP8266WiFiClass::_scanCount = 0; - } - _scanComplete = false; -} - - -/** - * loads all infos from a scanned wifi in to the ptr parameters - * @param networkItem uint8_t - * @param ssid const char** - * @param encryptionType uint8_t * - * @param RSSI int32_t * - * @param BSSID uint8_t ** - * @param channel int32_t * - * @param isHidden bool * - * @return (true if ok) - */ -bool ESP8266WiFiClass::getNetworkInfo(uint8_t i, String &ssid, uint8_t &encType, int32_t &rssi, uint8_t* &bssid, int32_t &channel, bool &isHidden) { - struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) { - return false; - } - - ssid = (const char*) it->ssid; - encType = encryptionType(i); - rssi = it->rssi; - bssid = it->bssid; // move ptr - channel = it->channel; - isHidden = (it->is_hidden != 0); - - return true; -} - - -/** - * Return the SSID discovered during the network scan. - * @param i specify from which network item want to get the information - * @return ssid string of the specified item on the networks scanned list - */ -String ESP8266WiFiClass::SSID(uint8_t i) { - struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) { - return ""; - } - - return String(reinterpret_cast(it->ssid)); -} - - -/** - * Return the encryption type of the networks discovered during the scanNetworks - * @param i specify from which network item want to get the information - * @return encryption type (enum wl_enc_type) of the specified item on the networks scanned list - */ -uint8_t ESP8266WiFiClass::encryptionType(uint8_t i) { - struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) { - return -1; - } - - switch(it->authmode) { - case AUTH_OPEN: - return ENC_TYPE_NONE; - case AUTH_WEP: - return ENC_TYPE_WEP; - case AUTH_WPA_PSK: - return ENC_TYPE_TKIP; - case AUTH_WPA2_PSK: - return ENC_TYPE_CCMP; - case AUTH_WPA_WPA2_PSK: - return ENC_TYPE_AUTO; - default: - return -1; - } -} - -/** - * Return the RSSI of the networks discovered during the scanNetworks - * @param i specify from which network item want to get the information - * @return signed value of RSSI of the specified item on the networks scanned list - */ -int32_t ESP8266WiFiClass::RSSI(uint8_t i) { - struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) { - return 0; - } - return it->rssi; -} - - -/** - * return MAC / BSSID of scanned wifi - * @param i specify from which network item want to get the information - * @return uint8_t * MAC / BSSID of scanned wifi - */ -uint8_t * ESP8266WiFiClass::BSSID(uint8_t i) { - struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) { - return 0; - } - return it->bssid; -} - -/** - * return MAC / BSSID of scanned wifi - * @param i specify from which network item want to get the information - * @return String MAC / BSSID of scanned wifi - */ -String ESP8266WiFiClass::BSSIDstr(uint8_t i) { - char mac[18] = { 0 }; - struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) { - return String(""); - } - sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", it->bssid[0], it->bssid[1], it->bssid[2], it->bssid[3], it->bssid[4], it->bssid[5]); - return String(mac); -} - -int32_t ESP8266WiFiClass::channel(uint8_t i) { - struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) { - return 0; - } - return it->channel; -} - -/** - * return if the scanned wifi is Hidden (no SSID) - * @param networkItem specify from which network item want to get the information - * @return bool (true == hidden) - */ -bool ESP8266WiFiClass::isHidden(uint8_t i) { - struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); - if(!it) { - return false; - } - return (it->is_hidden != 0); -} - -/** - * private - * scan callback - * @param result void *arg - * @param status STATUS - */ -void ESP8266WiFiClass::_scanDone(void* result, int status) { - if(status != OK) { - ESP8266WiFiClass::_scanCount = 0; - ESP8266WiFiClass::_scanResult = 0; - } else { - - int i = 0; - bss_info_head_t* head = reinterpret_cast(result); - - for(bss_info* it = STAILQ_FIRST(head); it; it = STAILQ_NEXT(it, next), ++i) - ; - ESP8266WiFiClass::_scanCount = i; - if(i == 0) { - ESP8266WiFiClass::_scanResult = 0; - } else { - bss_info* copied_info = new bss_info[i]; - i = 0; - for(bss_info* it = STAILQ_FIRST(head); it; it = STAILQ_NEXT(it, next), ++i) { - memcpy(copied_info + i, it, sizeof(bss_info)); - } - - ESP8266WiFiClass::_scanResult = copied_info; - } - - } - - ESP8266WiFiClass::_scanStarted = false; - ESP8266WiFiClass::_scanComplete = true; - - if(!ESP8266WiFiClass::_scanAsync) { - esp_schedule(); - } -} - -/** - * - * @param i specify from which network item want to get the information - * @return bss_info * - */ -void * ESP8266WiFiClass::_getScanInfoByIndex(int i) { - //TODO why its void * and not bss_info * ? - if(!ESP8266WiFiClass::_scanResult || (size_t) i > ESP8266WiFiClass::_scanCount) { - return 0; - } - return reinterpret_cast(ESP8266WiFiClass::_scanResult) + i; -} - - - -// ----------------------------------------------------------------------------------------------------------------------- -// ------------------------------------------------- Generic WiFi function ----------------------------------------------- -// ----------------------------------------------------------------------------------------------------------------------- - - -/** - * Return the current channel associated with the network - * @return channel (1-13) - */ -int32_t ESP8266WiFiClass::channel(void) { - return wifi_get_channel(); -} - -/** - * set Sleep mode - * @param type sleep_type_t - * @return bool - */ -bool ESP8266WiFiClass::setSleepMode(WiFiSleepType_t type) { - return wifi_set_sleep_type((sleep_type_t) type); -} - -/** - * get Sleep mode - * @return sleep_type_t - */ -WiFiSleepType_t ESP8266WiFiClass::getSleepMode() { - return (WiFiSleepType_t) wifi_get_sleep_type(); -} - -/** - * set phy Mode - * @param mode phy_mode_t - * @return bool - */ -bool ESP8266WiFiClass::setPhyMode(WiFiPhyMode_t mode) { - return wifi_set_phy_mode((phy_mode_t) mode); -} - -/** - * get phy Mode - * @return phy_mode_t - */ -WiFiPhyMode_t ESP8266WiFiClass::getPhyMode() { - return (WiFiPhyMode_t) wifi_get_phy_mode(); -} - -/** - * store WiFi condif in SDK config area in flash - * @param persistent - */ -void ESP8266WiFiClass::persistent(bool persistent) { - _persistent = persistent; -} - - -/** - * set new mode - * @param m WiFiMode_t - */ -bool ESP8266WiFiClass::mode(WiFiMode_t m) { - if(wifi_get_opmode() == (uint8) m) { - return true; - } - - bool ret = false; - - ETS_UART_INTR_DISABLE(); - if(_persistent) { - ret = wifi_set_opmode(m); - } else { - ret = wifi_set_opmode_current(m); - } - ETS_UART_INTR_ENABLE(); - - return ret; -} - -/** - * get WiFi mode - * @return WiFiMode - */ -WiFiMode_t ESP8266WiFiClass::getMode() { - return (WiFiMode_t) wifi_get_opmode(); -} - -/** - * control STA mode - * @param enable bool - * @return ok - */ -bool ESP8266WiFiClass::enableSTA(bool enable) { - - WiFiMode_t currentMode = (WiFiMode_t) wifi_get_opmode(); - bool isEnabled = ((currentMode & WIFI_STA) != 0); - - if(isEnabled != enable) { - if(enable) { - return mode((WiFiMode_t)(currentMode & WIFI_STA)); - } else { - return mode((WiFiMode_t)(currentMode & (~WIFI_STA))); - } - } else { - return true; - } -} - -/** - * control AP mode - * @param enable bool - * @return ok - */ -bool ESP8266WiFiClass::enableAP(bool enable){ - - WiFiMode_t currentMode = (WiFiMode_t) wifi_get_opmode(); - bool isEnabled = ((currentMode & WIFI_AP) != 0); - - if(isEnabled != enable) { - if(enable) { - return mode((WiFiMode_t)(currentMode & WIFI_AP)); - } else { - return mode((WiFiMode_t)(currentMode & (~WIFI_AP))); - } - } else { - return true; - } -} - - -// ----------------------------------------------------------------------------------------------------------------------- -// ------------------------------------------------ Generic Network function --------------------------------------------- -// ----------------------------------------------------------------------------------------------------------------------- - -void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback_arg); - -/** - * Resolve the given hostname to an IP address. - * @param aHostname Name to be resolved - * @param aResult IPAddress structure to store the returned IP address - * @return 1 if aIPAddrString was successfully converted to an IP address, - * else error code - */ -int ESP8266WiFiClass::hostByName(const char* aHostname, IPAddress& aResult) { - ip_addr_t addr; - aResult = static_cast(0); - err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult); - if(err == ERR_OK) { - aResult = addr.addr; - } else if(err == ERR_INPROGRESS) { - esp_yield(); - // will return here when dns_found_callback fires - } - - return (aResult != 0) ? 1 : 0; -} - -/** - * DNS callback - * @param name - * @param ipaddr - * @param callback_arg - */ -void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback_arg) { - if(ipaddr) - (*reinterpret_cast(callback_arg)) = ipaddr->addr; - esp_schedule(); // resume the hostByName function -} - - -// ----------------------------------------------------------------------------------------------------------------------- -// -------------------------------------------------- STA remote configure ----------------------------------------------- -// ----------------------------------------------------------------------------------------------------------------------- - -void wifi_wps_status_cb(wps_cb_status status); - -/** - * WPS config - * so far only WPS_TYPE_PBC is supported (SDK 1.2.0) - * @return ok - */ -bool ESP8266WiFiClass::beginWPSConfig(void) { - - if(!enableSTA(true)) { - // enable STA failed - return false; - } - - disconnect(); - - DEBUGV("wps begin\n"); - - if(!wifi_wps_disable()) { - DEBUGV("wps disable failed\n"); - return false; - } - - // so far only WPS_TYPE_PBC is supported (SDK 1.2.0) - if(!wifi_wps_enable(WPS_TYPE_PBC)) { - DEBUGV("wps enable failed\n"); - return false; - } - - if(!wifi_set_wps_cb((wps_st_cb_t) &wifi_wps_status_cb)) { - DEBUGV("wps cb failed\n"); - return false; - } - - if(!wifi_wps_start()) { - DEBUGV("wps start failed\n"); - return false; - } - - esp_yield(); - // will return here when wifi_wps_status_cb fires - - return true; -} - -/** - * WPS callback - * @param status wps_cb_status - */ -void wifi_wps_status_cb(wps_cb_status status) { - DEBUGV("wps cb status: %d\r\n", status); - switch(status) { - case WPS_CB_ST_SUCCESS: - if(!wifi_wps_disable()) { - DEBUGV("wps disable failed\n"); - } - wifi_station_connect(); - break; - case WPS_CB_ST_FAILED: - DEBUGV("wps FAILED\n"); - break; - case WPS_CB_ST_TIMEOUT: - DEBUGV("wps TIMEOUT\n"); - break; - case WPS_CB_ST_WEP: - DEBUGV("wps WEP\n"); - break; - } - // TODO user function to get status - - esp_schedule(); // resume the beginWPSConfig function -} - -/** - * Start SmartConfig - */ -void ESP8266WiFiClass::beginSmartConfig() { - if(_smartConfigStarted) - return; - - if(!enableSTA(true)) { - // enable STA failed - return; - } - - _smartConfigStarted = true; - _smartConfigDone = false; - - smartconfig_start(reinterpret_cast(&ESP8266WiFiClass::_smartConfigCallback), 1); -} - - -/** - * Stop SmartConfig - */ -void ESP8266WiFiClass::stopSmartConfig() { - if(!_smartConfigStarted) { - return; - } - - smartconfig_stop(); - _smartConfigStarted = false; -} - -/** - * Query SmartConfig status, to decide when stop config - * @return smartConfig Done - */ -bool ESP8266WiFiClass::smartConfigDone() { - if(!_smartConfigStarted) { - return false; - } - - return _smartConfigDone; -} - - -/** - * _smartConfigCallback - * @param st - * @param result - */ -void ESP8266WiFiClass::_smartConfigCallback(uint32_t st, void* result) { - sc_status status = (sc_status) st; - if(status == SC_STATUS_LINK) { - station_config* sta_conf = reinterpret_cast(result); - - wifi_station_set_config(sta_conf); - wifi_station_disconnect(); - wifi_station_connect(); - - WiFi._smartConfigDone = true; - } else if(status == SC_STATUS_LINK_OVER) { - WiFi.stopSmartConfig(); - } -} - - // ----------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------- Debug ------------------------------------------------------ // ----------------------------------------------------------------------------------------------------------------------- diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.h b/libraries/ESP8266WiFi/src/ESP8266WiFi.h index dbadb135e..4e6ce8e04 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.h @@ -29,195 +29,35 @@ extern "C" { } #include "IPAddress.h" + +#include "ESP8266WiFiType.h" +#include "ESP8266WiFiSTA.h" +#include "ESP8266WiFiAP.h" +#include "ESP8266WiFiScan.h" +#include "ESP8266WiFiGeneric.h" + #include "WiFiClient.h" #include "WiFiServer.h" #include "WiFiClientSecure.h" -#define WIFI_SCAN_RUNNING (-1) -#define WIFI_SCAN_FAILED (-2) - -// Note: -// this enums need to be in sync with the SDK! - -enum WiFiMode { - WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3 -}; - -typedef enum WiFiMode WiFiMode_t; - -typedef enum { - WIFI_PHY_MODE_11B = 1, WIFI_PHY_MODE_11G = 2, WIFI_PHY_MODE_11N = 3 -} WiFiPhyMode_t; - -typedef enum { - WIFI_NONE_SLEEP = 0, WIFI_LIGHT_SLEEP = 2, WIFI_MODEM_SLEEP = 3 -} WiFiSleepType_t; - -class ESP8266WiFiClass { - - // ---------------------------------------------------------------------------------------------- - // -------------------------------------- ESP8266WiFiClass -------------------------------------- - // ---------------------------------------------------------------------------------------------- - +class ESP8266WiFiClass : public ESP8266WiFiGenericClass, public ESP8266WiFiSTAClass, public ESP8266WiFiScanClass, public ESP8266WiFiAPClass { public: - ESP8266WiFiClass(); + // workaround same function name with different signature + using ESP8266WiFiGenericClass::channel; - protected: + using ESP8266WiFiSTAClass::SSID; + using ESP8266WiFiSTAClass::RSSI; + using ESP8266WiFiSTAClass::BSSID; + using ESP8266WiFiSTAClass::BSSIDstr; - static void _eventCallback(void *event); - - // ---------------------------------------------------------------------------------------------- - // ---------------------------------------- STA function ---------------------------------------- - // ---------------------------------------------------------------------------------------------- - - public: - - int begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL); - int begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL); - int begin(); - - void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet); - void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns); - - - bool reconnect(); - int disconnect(bool wifioff = false); - - uint8_t waitForConnectResult(); - - // STA network info - IPAddress localIP(); - - uint8_t* macAddress(uint8_t* mac); - String macAddress(void); - - IPAddress subnetMask(); - IPAddress gatewayIP(); - IPAddress dnsIP(uint8_t dns_no = 0); - - String hostname(void); - bool hostname(char* aHostname); - bool hostname(const char* aHostname); - bool hostname(String aHostname); - - // STA WiFi info - wl_status_t status(); - String SSID() const; - String psk() const; - - uint8_t *BSSID(void); - String BSSIDstr(void); - - int32_t RSSI(); - - protected: - - bool _useStaticIp; - - // ---------------------------------------------------------------------------------------------- - // ----------------------------------------- AP function ---------------------------------------- - // ---------------------------------------------------------------------------------------------- - - public: - - void softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0); - void softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet); - int softAPdisconnect(bool wifioff = false); - - IPAddress softAPIP(); - - uint8_t* softAPmacAddress(uint8_t* mac); - String softAPmacAddress(void); - - protected: - - // ---------------------------------------------------------------------------------------------- - // ----------------------------------------- scan function -------------------------------------- - // ---------------------------------------------------------------------------------------------- - - public: - - int8_t scanNetworks(bool async = false, bool show_hidden = false); - - int8_t scanComplete(); - void scanDelete(); - - // scan result - bool getNetworkInfo(uint8_t networkItem, String &ssid, uint8_t &encryptionType, int32_t &RSSI, uint8_t* &BSSID, int32_t &channel, bool &isHidden); - - String SSID(uint8_t networkItem); - uint8_t encryptionType(uint8_t networkItem); - int32_t RSSI(uint8_t networkItem); - uint8_t * BSSID(uint8_t networkItem); - String BSSIDstr(uint8_t networkItem); - int32_t channel(uint8_t networkItem); - bool isHidden(uint8_t networkItem); - - protected: - - static bool _scanAsync; - static bool _scanStarted; - static bool _scanComplete; - - static size_t _scanCount; - static void* _scanResult; - - static void _scanDone(void* result, int status); - void * _getScanInfoByIndex(int i); - - // ---------------------------------------------------------------------------------------------- - // -------------------------------------- Generic WiFi function --------------------------------- - // ---------------------------------------------------------------------------------------------- - - public: - - int32_t channel(void); - - bool setSleepMode(WiFiSleepType_t type); - WiFiSleepType_t getSleepMode(); - - bool setPhyMode(WiFiPhyMode_t mode); - WiFiPhyMode_t getPhyMode(); - - void persistent(bool persistent); - - bool mode(WiFiMode_t); - WiFiMode_t getMode(); - - bool enableSTA(bool enable); - bool enableAP(bool enable); - - protected: - bool _persistent; - - // ---------------------------------------------------------------------------------------------- - // ------------------------------------ Generic Network function -------------------------------- - // ---------------------------------------------------------------------------------------------- - - public: - - int hostByName(const char* aHostname, IPAddress& aResult); - - protected: - - // ---------------------------------------------------------------------------------------------- - // ------------------------------------ STA remote configure ----------------------------------- - // ---------------------------------------------------------------------------------------------- - - public: - - bool beginWPSConfig(void); - - void beginSmartConfig(); - bool smartConfigDone(); - void stopSmartConfig(); - - protected: - - bool _smartConfigStarted; - bool _smartConfigDone; - static void _smartConfigCallback(uint32_t status, void* result); + using ESP8266WiFiScanClass::SSID; + using ESP8266WiFiScanClass::encryptionType; + using ESP8266WiFiScanClass::RSSI; + using ESP8266WiFiScanClass::BSSID; + using ESP8266WiFiScanClass::BSSIDstr; + using ESP8266WiFiScanClass::channel; + using ESP8266WiFiScanClass::isHidden; // ---------------------------------------------------------------------------------------------- // ------------------------------------------- Debug -------------------------------------------- diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp new file mode 100644 index 000000000..05b6b1177 --- /dev/null +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -0,0 +1,211 @@ +/* + ESP8266WiFiSTA.cpp - WiFi library for esp8266 + + Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Reworked on 28 Dec 2015 by Markus Sattler + + */ + +#include "ESP8266WiFi.h" +#include "ESP8266WiFiGeneric.h" +#include "ESP8266WiFiAP.h" + +extern "C" { +#include "c_types.h" +#include "ets_sys.h" +#include "os_type.h" +#include "osapi.h" +#include "mem.h" +#include "user_interface.h" +} + +#include "debug.h" + + + +// ----------------------------------------------------------------------------------------------------------------------- +// ---------------------------------------------------- Private functions ------------------------------------------------ +// ----------------------------------------------------------------------------------------------------------------------- + +static bool softap_config_equal(const softap_config& lhs, const softap_config& rhs); + + + +/** + * compare two AP configurations + * @param lhs softap_config + * @param rhs softap_config + * @return equal + */ +static bool softap_config_equal(const softap_config& lhs, const softap_config& rhs) { + if(strcmp(reinterpret_cast(lhs.ssid), reinterpret_cast(rhs.ssid)) != 0) + return false; + if(strcmp(reinterpret_cast(lhs.password), reinterpret_cast(rhs.password)) != 0) + return false; + if(lhs.channel != rhs.channel) + return false; + if(lhs.ssid_hidden != rhs.ssid_hidden) + return false; + return true; +} + +// ----------------------------------------------------------------------------------------------------------------------- +// ----------------------------------------------------- AP function ----------------------------------------------------- +// ----------------------------------------------------------------------------------------------------------------------- + + +/** + * 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) + */ +void ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden) { + + if(!WiFi.enableAP(true)) { + // enable AP failed + return; + } + + if(!ssid || *ssid == 0 || strlen(ssid) > 31) { + // fail SSID too long or missing! + return; + } + + if(passphrase && strlen(passphrase) > 63) { + // fail passphrase to long! + return; + } + + struct softap_config conf; + wifi_softap_get_config(&conf); + strcpy(reinterpret_cast(conf.ssid), ssid); + conf.channel = channel; + conf.ssid_len = strlen(ssid); + conf.ssid_hidden = ssid_hidden; + conf.max_connection = 4; + 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(conf.password), passphrase); + } + + struct softap_config conf_current; + wifi_softap_get_config(&conf_current); + if(softap_config_equal(conf, conf_current)) { + DEBUGV("softap config unchanged"); + return; + } + + ETS_UART_INTR_DISABLE(); + if(WiFi._persistent) + wifi_softap_set_config(&conf); + else + wifi_softap_set_config_current(&conf); + ETS_UART_INTR_ENABLE(); +} + + +/** + * Configure access point + * @param local_ip access point IP + * @param gateway gateway IP + * @param subnet subnet mask + */ +void ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { + + if(!WiFi.enableAP(true)) { + // enable AP failed + return; + } + + struct ip_info info; + info.ip.addr = static_cast(local_ip); + info.gw.addr = static_cast(gateway); + info.netmask.addr = static_cast(subnet); + wifi_softap_dhcps_stop(); + wifi_set_ip_info(SOFTAP_IF, &info); + wifi_softap_dhcps_start(); +} + + + +/** + * Disconnect from the network (close AP) + * @param wifioff disable mode? + * @return one value of wl_status_t enum + */ +int ESP8266WiFiAPClass::softAPdisconnect(bool wifioff) { + struct softap_config conf; + *conf.ssid = 0; + *conf.password = 0; + ETS_UART_INTR_DISABLE(); + if(WiFi._persistent) { + wifi_softap_set_config(&conf); + } else { + wifi_softap_set_config_current(&conf); + } + ETS_UART_INTR_ENABLE(); + + if(wifioff) { + WiFi.enableAP(false); + } + + //TODO return with more meaning ? + return 0; +} + +/** + * Get the softAP interface IP address. + * @return IPAddress softAP IP + */ +IPAddress ESP8266WiFiAPClass::softAPIP() { + struct ip_info ip; + wifi_get_ip_info(SOFTAP_IF, &ip); + return IPAddress(ip.ip.addr); +} + + +/** + * Get the softAP interface MAC address. + * @param mac pointer to uint8_t array with length WL_MAC_ADDR_LENGTH + * @return pointer to uint8_t* + */ +uint8_t* ESP8266WiFiAPClass::softAPmacAddress(uint8_t* mac) { + wifi_get_macaddr(SOFTAP_IF, mac); + return mac; +} + +/** + * Get the softAP interface MAC address. + * @return String mac + */ +String ESP8266WiFiAPClass::softAPmacAddress(void) { + uint8_t mac[6]; + char macStr[18] = { 0 }; + wifi_get_macaddr(SOFTAP_IF, mac); + + sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + return String(macStr); +} diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h new file mode 100644 index 000000000..e23878f66 --- /dev/null +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h @@ -0,0 +1,52 @@ +/* + ESP8266WiFiAP.h - esp8266 Wifi support. + Based on WiFi.h from Ardiono WiFi shield library. + Copyright (c) 2011-2014 Arduino. All right reserved. + Modified by Ivan Grokhotkov, December 2014 + Reworked by Markus Sattler, December 2015 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef ESP8266WIFIAP_H_ +#define ESP8266WIFIAP_H_ + + +#include "ESP8266WiFiType.h" +#include "ESP8266WiFiGeneric.h" + + +class ESP8266WiFiAPClass { + + // ---------------------------------------------------------------------------------------------- + // ----------------------------------------- AP function ---------------------------------------- + // ---------------------------------------------------------------------------------------------- + + public: + + void softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0); + void softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet); + int softAPdisconnect(bool wifioff = false); + + IPAddress softAPIP(); + + uint8_t* softAPmacAddress(uint8_t* mac); + String softAPmacAddress(void); + + protected: + +}; + +#endif /* ESP8266WIFIAP_H_*/ diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp new file mode 100644 index 000000000..89eb38078 --- /dev/null +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp @@ -0,0 +1,244 @@ +/* + ESP8266WiFiGeneric.cpp - WiFi library for esp8266 + + Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Reworked on 28 Dec 2015 by Markus Sattler + + */ + +#include "ESP8266WiFi.h" +#include "ESP8266WiFiGeneric.h" + +extern "C" { +#include "c_types.h" +#include "ets_sys.h" +#include "os_type.h" +#include "osapi.h" +#include "mem.h" +#include "user_interface.h" + +#include "lwip/opt.h" +#include "lwip/err.h" +#include "lwip/dns.h" +} + +#include "WiFiClient.h" +#include "WiFiUdp.h" + +#include "debug.h" + +extern "C" void esp_schedule(); +extern "C" void esp_yield(); + +// ----------------------------------------------------------------------------------------------------------------------- +// ------------------------------------------------- Generic WiFi function ----------------------------------------------- +// ----------------------------------------------------------------------------------------------------------------------- + +bool ESP8266WiFiGenericClass::_persistent = true; + + + +ESP8266WiFiGenericClass::ESP8266WiFiGenericClass() { + wifi_set_event_handler_cb((wifi_event_handler_cb_t) &ESP8266WiFiGenericClass::_eventCallback); +} + +/** + * callback for WiFi events + * @param arg + */ +void ESP8266WiFiGenericClass::_eventCallback(void* arg) { + System_Event_t* event = reinterpret_cast(arg); + DEBUGV("wifi evt: %d\r\n", event->event); + + if(event->event == EVENT_STAMODE_DISCONNECTED) { + WiFiClient::stopAll(); + } + + //TODO allow user to hook this event +} + + + +/** + * Return the current channel associated with the network + * @return channel (1-13) + */ +int32_t ESP8266WiFiGenericClass::channel(void) { + return wifi_get_channel(); +} + +/** + * set Sleep mode + * @param type sleep_type_t + * @return bool + */ +bool ESP8266WiFiGenericClass::setSleepMode(WiFiSleepType_t type) { + return wifi_set_sleep_type((sleep_type_t) type); +} + +/** + * get Sleep mode + * @return sleep_type_t + */ +WiFiSleepType_t ESP8266WiFiGenericClass::getSleepMode() { + return (WiFiSleepType_t) wifi_get_sleep_type(); +} + +/** + * set phy Mode + * @param mode phy_mode_t + * @return bool + */ +bool ESP8266WiFiGenericClass::setPhyMode(WiFiPhyMode_t mode) { + return wifi_set_phy_mode((phy_mode_t) mode); +} + +/** + * get phy Mode + * @return phy_mode_t + */ +WiFiPhyMode_t ESP8266WiFiGenericClass::getPhyMode() { + return (WiFiPhyMode_t) wifi_get_phy_mode(); +} + +/** + * store WiFi config in SDK flash area + * @param persistent + */ +void ESP8266WiFiGenericClass::persistent(bool persistent) { + _persistent = persistent; +} + + +/** + * set new mode + * @param m WiFiMode_t + */ +bool ESP8266WiFiGenericClass::mode(WiFiMode_t m) { + if(wifi_get_opmode() == (uint8) m) { + return true; + } + + bool ret = false; + + ETS_UART_INTR_DISABLE(); + if(_persistent) { + ret = wifi_set_opmode(m); + } else { + ret = wifi_set_opmode_current(m); + } + ETS_UART_INTR_ENABLE(); + + return ret; +} + +/** + * get WiFi mode + * @return WiFiMode + */ +WiFiMode_t ESP8266WiFiGenericClass::getMode() { + return (WiFiMode_t) wifi_get_opmode(); +} + +/** + * control STA mode + * @param enable bool + * @return ok + */ +bool ESP8266WiFiGenericClass::enableSTA(bool enable) { + + WiFiMode_t currentMode = (WiFiMode_t) wifi_get_opmode(); + bool isEnabled = ((currentMode & WIFI_STA) != 0); + + if(isEnabled != enable) { + if(enable) { + return mode((WiFiMode_t)(currentMode & WIFI_STA)); + } else { + return mode((WiFiMode_t)(currentMode & (~WIFI_STA))); + } + } else { + return true; + } +} + +/** + * control AP mode + * @param enable bool + * @return ok + */ +bool ESP8266WiFiGenericClass::enableAP(bool enable){ + + WiFiMode_t currentMode = (WiFiMode_t) wifi_get_opmode(); + bool isEnabled = ((currentMode & WIFI_AP) != 0); + + if(isEnabled != enable) { + if(enable) { + return mode((WiFiMode_t)(currentMode & WIFI_AP)); + } else { + return mode((WiFiMode_t)(currentMode & (~WIFI_AP))); + } + } else { + return true; + } +} + + + + + +// ----------------------------------------------------------------------------------------------------------------------- +// ------------------------------------------------ Generic Network function --------------------------------------------- +// ----------------------------------------------------------------------------------------------------------------------- + +void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback_arg); + +/** + * Resolve the given hostname to an IP address. + * @param aHostname Name to be resolved + * @param aResult IPAddress structure to store the returned IP address + * @return 1 if aIPAddrString was successfully converted to an IP address, + * else error code + */ +int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult) { + ip_addr_t addr; + aResult = static_cast(0); + err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult); + if(err == ERR_OK) { + aResult = addr.addr; + } else if(err == ERR_INPROGRESS) { + esp_yield(); + // will return here when dns_found_callback fires + } + + return (aResult != 0) ? 1 : 0; +} + +/** + * DNS callback + * @param name + * @param ipaddr + * @param callback_arg + */ +void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback_arg) { + if(ipaddr) + (*reinterpret_cast(callback_arg)) = ipaddr->addr; + esp_schedule(); // resume the hostByName function +} + + diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h new file mode 100644 index 000000000..c2df1026d --- /dev/null +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h @@ -0,0 +1,73 @@ +/* + ESP8266WiFiGeneric.h - esp8266 Wifi support. + Based on WiFi.h from Ardiono WiFi shield library. + Copyright (c) 2011-2014 Arduino. All right reserved. + Modified by Ivan Grokhotkov, December 2014 + Reworked by Markus Sattler, December 2015 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef ESP8266WIFIGENERIC_H_ +#define ESP8266WIFIGENERIC_H_ + +#include "ESP8266WiFiType.h" + +class ESP8266WiFiGenericClass { + // ---------------------------------------------------------------------------------------------- + // -------------------------------------- Generic WiFi function --------------------------------- + // ---------------------------------------------------------------------------------------------- + + public: + ESP8266WiFiGenericClass(); + + int32_t channel(void); + + bool setSleepMode(WiFiSleepType_t type); + WiFiSleepType_t getSleepMode(); + + bool setPhyMode(WiFiPhyMode_t mode); + WiFiPhyMode_t getPhyMode(); + + void persistent(bool persistent); + + bool mode(WiFiMode_t); + WiFiMode_t getMode(); + + bool enableSTA(bool enable); + bool enableAP(bool enable); + + protected: + static bool _persistent; + + static void _eventCallback(void *event); + + // ---------------------------------------------------------------------------------------------- + // ------------------------------------ Generic Network function -------------------------------- + // ---------------------------------------------------------------------------------------------- + + public: + + int hostByName(const char* aHostname, IPAddress& aResult); + + protected: + + friend class ESP8266WiFiSTAClass; + friend class ESP8266WiFiScanClass; + friend class ESP8266WiFiAPClass; +}; + + +#endif /* ESP8266WIFIGENERIC_H_ */ diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp new file mode 100644 index 000000000..218247ca6 --- /dev/null +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp @@ -0,0 +1,606 @@ +/* + ESP8266WiFiSTA.cpp - WiFi library for esp8266 + + Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Reworked on 28 Dec 2015 by Markus Sattler + + */ + +#include "ESP8266WiFi.h" +#include "ESP8266WiFiGeneric.h" +#include "ESP8266WiFiSTA.h" + +extern "C" { +#include "c_types.h" +#include "ets_sys.h" +#include "os_type.h" +#include "osapi.h" +#include "mem.h" +#include "user_interface.h" +#include "smartconfig.h" +#include "lwip/err.h" +#include "lwip/dns.h" +} + +#include "debug.h" + +extern "C" void esp_schedule(); +extern "C" void esp_yield(); + +// ----------------------------------------------------------------------------------------------------------------------- +// ---------------------------------------------------- Private functions ------------------------------------------------ +// ----------------------------------------------------------------------------------------------------------------------- + +static bool sta_config_equal(const station_config& lhs, const station_config& rhs); + + +/** + * compare two STA configurations + * @param lhs station_config + * @param rhs station_config + * @return equal + */ +static bool sta_config_equal(const station_config& lhs, const station_config& rhs) { + if(strcmp(reinterpret_cast(lhs.ssid), reinterpret_cast(rhs.ssid)) != 0) + return false; + + if(strcmp(reinterpret_cast(lhs.password), reinterpret_cast(rhs.password)) != 0) + return false; + + if(lhs.bssid_set) { + if(!rhs.bssid_set) + return false; + + if(memcmp(lhs.bssid, rhs.bssid, 6) != 0) + return false; + } else { + if(rhs.bssid_set) + return false; + } + + return true; +} + +// ----------------------------------------------------------------------------------------------------------------------- +// ---------------------------------------------------- STA function ----------------------------------------------------- +// ----------------------------------------------------------------------------------------------------------------------- + +bool ESP8266WiFiSTAClass::_useStaticIp = false; + +/** + * Start Wifi connection + * if passphrase is set the most secure supported mode will be automatically selected + * @param ssid const char* Pointer to the SSID string. + * @param passphrase const char * Optional. Passphrase. Valid characters in a passphrase must be between ASCII 32-126 (decimal). + * @param bssid uint8_t[6] Optional. BSSID / MAC of AP + * @param channel Optional. Channel of AP + * @return + */ +int ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid) { + + if(!WiFi.enableSTA(true)) { + // enable STA failed + return WL_CONNECT_FAILED; + } + + if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) { + // fail SSID too long or missing! + return WL_CONNECT_FAILED; + } + + if(passphrase && strlen(passphrase) > 63) { + // fail passphrase too long! + return WL_CONNECT_FAILED; + } + + struct station_config conf; + strcpy(reinterpret_cast(conf.ssid), ssid); + + if(passphrase) { + strcpy(reinterpret_cast(conf.password), passphrase); + } else { + *conf.password = 0; + } + + if(bssid) { + conf.bssid_set = 1; + memcpy((void *) &conf.bssid[0], (void *) bssid, 6); + } else { + conf.bssid_set = 0; + } + + struct station_config current_conf; + wifi_station_get_config(¤t_conf); + if(sta_config_equal(current_conf, conf)) { + DEBUGV("sta config unchanged"); + return status(); + } + + ETS_UART_INTR_DISABLE(); + if(WiFi._persistent) { + wifi_station_set_config(&conf); + } else { + wifi_station_set_config_current(&conf); + } + wifi_station_connect(); + ETS_UART_INTR_ENABLE(); + + if(channel > 0 && channel <= 13) { + wifi_set_channel(channel); + } + + if(!_useStaticIp) { + wifi_station_dhcpc_start(); + } + + return status(); +} + +int ESP8266WiFiSTAClass::begin(char* ssid, char *passphrase, int32_t channel, const uint8_t* bssid) { + return begin((const char*) ssid, (const char*) passphrase, channel, bssid); +} + +/** + * Use to connect to SDK config. + * @return wl_status_t + */ +int ESP8266WiFiSTAClass::begin() { + + if(!WiFi.enableSTA(true)) { + // enable STA failed + return WL_CONNECT_FAILED; + } + + ETS_UART_INTR_DISABLE(); + wifi_station_connect(); + ETS_UART_INTR_ENABLE(); + + if(!_useStaticIp) { + wifi_station_dhcpc_start(); + } + return status(); +} + + +/** + * Change IP configuration settings disabling the dhcp client + * @param local_ip Static ip configuration + * @param gateway Static gateway configuration + * @param subnet Static Subnet mask + */ +void ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { + + if(!WiFi.enableSTA(true)) { + return; + } + + struct ip_info info; + info.ip.addr = static_cast(local_ip); + info.gw.addr = static_cast(gateway); + info.netmask.addr = static_cast(subnet); + + wifi_station_dhcpc_stop(); + wifi_set_ip_info(STATION_IF, &info); + + _useStaticIp = true; +} + +/** + * Change IP configuration settings disabling the dhcp client + * @param local_ip Static ip configuration + * @param gateway Static gateway configuration + * @param subnet Static Subnet mask + * @param dns Static DNS server + */ +void ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns) { + struct ip_info info; + info.ip.addr = static_cast(local_ip); + info.gw.addr = static_cast(gateway); + info.netmask.addr = static_cast(subnet); + + wifi_station_dhcpc_stop(); + wifi_set_ip_info(STATION_IF, &info); + + // Set DNS-Server + ip_addr_t d; + d.addr = static_cast(dns); + dns_setserver(0, &d); + + _useStaticIp = true; +} + +/** + * will force a disconnect an then start reconnecting to AP + * @return ok + */ +bool ESP8266WiFiSTAClass::reconnect() { + if((WiFi.getMode() & WIFI_STA) != 0) { + if(wifi_station_disconnect()) { + return wifi_station_connect(); + } + } + return false; +} + +/** + * Disconnect from the network + * @param wifioff + * @return one value of wl_status_t enum + */ +int ESP8266WiFiSTAClass::disconnect(bool wifioff) { + struct station_config conf; + *conf.ssid = 0; + *conf.password = 0; + + ETS_UART_INTR_DISABLE(); + if(WiFi._persistent) { + wifi_station_set_config(&conf); + } else { + wifi_station_set_config_current(&conf); + } + wifi_station_disconnect(); + ETS_UART_INTR_ENABLE(); + + if(wifioff) { + WiFi.enableSTA(false); + } + + //TODO return with more meaning ? + return 0; +} + +/** + * Wait for WiFi connection to reach a result + * returns the status reached or disconnect if STA is off + * @return wl_status_t + */ +uint8_t ESP8266WiFiSTAClass::waitForConnectResult() { + //1 and 3 have STA enabled + if((wifi_get_opmode() & 1) == 0) { + return WL_DISCONNECTED; + } + while(status() == WL_DISCONNECTED) { + delay(100); + } + return status(); +} + +/** + * Get the station interface IP address. + * @return IPAddress station IP + */ +IPAddress ESP8266WiFiSTAClass::localIP() { + struct ip_info ip; + wifi_get_ip_info(STATION_IF, &ip); + return IPAddress(ip.ip.addr); +} + + +/** + * Get the station interface MAC address. + * @param mac pointer to uint8_t array with length WL_MAC_ADDR_LENGTH + * @return pointer to uint8_t * + */ +uint8_t* ESP8266WiFiSTAClass::macAddress(uint8_t* mac) { + wifi_get_macaddr(STATION_IF, mac); + return mac; +} + +/** + * Get the station interface MAC address. + * @return String mac + */ +String ESP8266WiFiSTAClass::macAddress(void) { + uint8_t mac[6]; + char macStr[18] = { 0 }; + wifi_get_macaddr(STATION_IF, mac); + + sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + return String(macStr); +} + +/** + * Get the interface subnet mask address. + * @return IPAddress subnetMask + */ +IPAddress ESP8266WiFiSTAClass::subnetMask() { + struct ip_info ip; + wifi_get_ip_info(STATION_IF, &ip); + return IPAddress(ip.netmask.addr); +} + +/** + * Get the gateway ip address. + * @return IPAddress gatewayIP + */ +IPAddress ESP8266WiFiSTAClass::gatewayIP() { + struct ip_info ip; + wifi_get_ip_info(STATION_IF, &ip); + return IPAddress(ip.gw.addr); +} + +/** + * Get the DNS ip address. + * @param dns_no + * @return IPAddress DNS Server IP + */ +IPAddress ESP8266WiFiSTAClass::dnsIP(uint8_t dns_no) { + ip_addr_t dns_ip = dns_getserver(dns_no); + return IPAddress(dns_ip.addr); +} + + +/** + * Get ESP8266 station DHCP hostname + * @return hostname + */ +String ESP8266WiFiSTAClass::hostname(void) { + return String(wifi_station_get_hostname()); +} + + +/** + * Set ESP8266 station DHCP hostname + * @param aHostname max length:32 + * @return ok + */ +bool ESP8266WiFiSTAClass::hostname(char* aHostname) { + if(strlen(aHostname) > 32) { + return false; + } + return wifi_station_set_hostname(aHostname); +} + +/** + * Set ESP8266 station DHCP hostname + * @param aHostname max length:32 + * @return ok + */ +bool ESP8266WiFiSTAClass::hostname(const char* aHostname) { + return hostname((char*) aHostname); +} + +/** + * Set ESP8266 station DHCP hostname + * @param aHostname max length:32 + * @return ok + */ +bool ESP8266WiFiSTAClass::hostname(String aHostname) { + return hostname((char*) aHostname.c_str()); +} + +/** + * Return Connection status. + * @return one of the value defined in wl_status_t + * + */ +wl_status_t ESP8266WiFiSTAClass::status() { + int status = wifi_station_get_connect_status(); + + if(status == STATION_GOT_IP) { + return WL_CONNECTED; + } else if(status == STATION_NO_AP_FOUND) { + return WL_NO_SSID_AVAIL; + } else if(status == STATION_CONNECT_FAIL || status == STATION_WRONG_PASSWORD) { + return WL_CONNECT_FAILED; + } else if(status == STATION_IDLE) { + return WL_IDLE_STATUS; + } else { + return WL_DISCONNECTED; + } +} + +/** + * Return the current SSID associated with the network + * @return SSID + */ +String ESP8266WiFiSTAClass::SSID() const { + struct station_config conf; + wifi_station_get_config(&conf); + return String(reinterpret_cast(conf.ssid)); +} + +/** + * Return the current pre shared key associated with the network + * @return psk string + */ +String ESP8266WiFiSTAClass::psk() const { + struct station_config conf; + wifi_station_get_config(&conf); + return String(reinterpret_cast(conf.password)); +} + +/** + * Return the current bssid / mac associated with the network if configured + * @return bssid uint8_t * + */ +uint8_t* ESP8266WiFiSTAClass::BSSID(void) { + static struct station_config conf; + wifi_station_get_config(&conf); + return reinterpret_cast(conf.bssid); +} + +/** + * Return the current bssid / mac associated with the network if configured + * @return String bssid mac + */ +String ESP8266WiFiSTAClass::BSSIDstr(void) { + struct station_config conf; + char mac[18] = { 0 }; + wifi_station_get_config(&conf); + sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", conf.bssid[0], conf.bssid[1], conf.bssid[2], conf.bssid[3], conf.bssid[4], conf.bssid[5]); + return String(mac); +} + +/** + * Return the current network RSSI. + * @return RSSI value + */ +int32_t ESP8266WiFiSTAClass::RSSI(void) { + return wifi_station_get_rssi(); +} + + + +// ----------------------------------------------------------------------------------------------------------------------- +// -------------------------------------------------- STA remote configure ----------------------------------------------- +// ----------------------------------------------------------------------------------------------------------------------- + +void wifi_wps_status_cb(wps_cb_status status); + +/** + * WPS config + * so far only WPS_TYPE_PBC is supported (SDK 1.2.0) + * @return ok + */ +bool ESP8266WiFiSTAClass::beginWPSConfig(void) { + + if(!WiFi.enableSTA(true)) { + // enable STA failed + return false; + } + + disconnect(); + + DEBUGV("wps begin\n"); + + if(!wifi_wps_disable()) { + DEBUGV("wps disable failed\n"); + return false; + } + + // so far only WPS_TYPE_PBC is supported (SDK 1.2.0) + if(!wifi_wps_enable(WPS_TYPE_PBC)) { + DEBUGV("wps enable failed\n"); + return false; + } + + if(!wifi_set_wps_cb((wps_st_cb_t) &wifi_wps_status_cb)) { + DEBUGV("wps cb failed\n"); + return false; + } + + if(!wifi_wps_start()) { + DEBUGV("wps start failed\n"); + return false; + } + + esp_yield(); + // will return here when wifi_wps_status_cb fires + + return true; +} + +/** + * WPS callback + * @param status wps_cb_status + */ +void wifi_wps_status_cb(wps_cb_status status) { + DEBUGV("wps cb status: %d\r\n", status); + switch(status) { + case WPS_CB_ST_SUCCESS: + if(!wifi_wps_disable()) { + DEBUGV("wps disable failed\n"); + } + wifi_station_connect(); + break; + case WPS_CB_ST_FAILED: + DEBUGV("wps FAILED\n"); + break; + case WPS_CB_ST_TIMEOUT: + DEBUGV("wps TIMEOUT\n"); + break; + case WPS_CB_ST_WEP: + DEBUGV("wps WEP\n"); + break; + } + // TODO user function to get status + + esp_schedule(); // resume the beginWPSConfig function +} + + + +bool ESP8266WiFiSTAClass::_smartConfigStarted = false; +bool ESP8266WiFiSTAClass::_smartConfigDone = false; + +/** + * Start SmartConfig + */ +void ESP8266WiFiSTAClass::beginSmartConfig() { + if(_smartConfigStarted) + return; + + if(!WiFi.enableSTA(true)) { + // enable STA failed + return; + } + + _smartConfigStarted = true; + _smartConfigDone = false; + + smartconfig_start(reinterpret_cast(&ESP8266WiFiSTAClass::_smartConfigCallback), 1); +} + + +/** + * Stop SmartConfig + */ +void ESP8266WiFiSTAClass::stopSmartConfig() { + if(!_smartConfigStarted) { + return; + } + + smartconfig_stop(); + _smartConfigStarted = false; +} + +/** + * Query SmartConfig status, to decide when stop config + * @return smartConfig Done + */ +bool ESP8266WiFiSTAClass::smartConfigDone() { + if(!_smartConfigStarted) { + return false; + } + + return _smartConfigDone; +} + + +/** + * _smartConfigCallback + * @param st + * @param result + */ +void ESP8266WiFiSTAClass::_smartConfigCallback(uint32_t st, void* result) { + sc_status status = (sc_status) st; + if(status == SC_STATUS_LINK) { + station_config* sta_conf = reinterpret_cast(result); + + wifi_station_set_config(sta_conf); + wifi_station_disconnect(); + wifi_station_connect(); + + WiFi._smartConfigDone = true; + } else if(status == SC_STATUS_LINK_OVER) { + WiFi.stopSmartConfig(); + } +} + diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h new file mode 100644 index 000000000..409577707 --- /dev/null +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h @@ -0,0 +1,101 @@ +/* + ESP8266WiFiSTA.h - esp8266 Wifi support. + Based on WiFi.h from Ardiono WiFi shield library. + Copyright (c) 2011-2014 Arduino. All right reserved. + Modified by Ivan Grokhotkov, December 2014 + Reworked by Markus Sattler, December 2015 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef ESP8266WIFISTA_H_ +#define ESP8266WIFISTA_H_ + + +#include "ESP8266WiFiType.h" +#include "ESP8266WiFiGeneric.h" + + +class ESP8266WiFiSTAClass { + // ---------------------------------------------------------------------------------------------- + // ---------------------------------------- STA function ---------------------------------------- + // ---------------------------------------------------------------------------------------------- + + public: + + int begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL); + int begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL); + int begin(); + + void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet); + void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns); + + bool reconnect(); + int disconnect(bool wifioff = false); + + uint8_t waitForConnectResult(); + + // STA network info + IPAddress localIP(); + + uint8_t * macAddress(uint8_t* mac); + String macAddress(); + + IPAddress subnetMask(); + IPAddress gatewayIP(); + IPAddress dnsIP(uint8_t dns_no = 0); + + String hostname(); + bool hostname(char* aHostname); + bool hostname(const char* aHostname); + bool hostname(String aHostname); + + // STA WiFi info + wl_status_t status(); + String SSID() const; + String psk() const; + + uint8_t * BSSID(); + String BSSIDstr(); + + int32_t RSSI(); + + protected: + + static bool _useStaticIp; + + // ---------------------------------------------------------------------------------------------- + // ------------------------------------ STA remote configure ----------------------------------- + // ---------------------------------------------------------------------------------------------- + + public: + + bool beginWPSConfig(void); + + void beginSmartConfig(); + bool smartConfigDone(); + void stopSmartConfig(); + + + protected: + + static bool _smartConfigStarted; + static bool _smartConfigDone; + static void _smartConfigCallback(uint32_t status, void* result); + +}; + + +#endif /* ESP8266WIFISTA_H_ */ diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp new file mode 100644 index 000000000..223f4ae26 --- /dev/null +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp @@ -0,0 +1,323 @@ +/* + ESP8266WiFiScan.cpp - WiFi library for esp8266 + + Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Reworked on 28 Dec 2015 by Markus Sattler + + */ + +#include "ESP8266WiFi.h" +#include "ESP8266WiFiGeneric.h" +#include "ESP8266WiFiScan.h" + +extern "C" { +#include "c_types.h" +#include "ets_sys.h" +#include "os_type.h" +#include "osapi.h" +#include "mem.h" +#include "user_interface.h" +} + +#include "debug.h" + +extern "C" void esp_schedule(); +extern "C" void esp_yield(); + +// ----------------------------------------------------------------------------------------------------------------------- +// ---------------------------------------------------- Private functions ------------------------------------------------ +// ----------------------------------------------------------------------------------------------------------------------- + + + + +// ----------------------------------------------------------------------------------------------------------------------- +// ----------------------------------------------------- scan function --------------------------------------------------- +// ----------------------------------------------------------------------------------------------------------------------- + +bool ESP8266WiFiScanClass::_scanAsync = false; +bool ESP8266WiFiScanClass::_scanStarted = false; +bool ESP8266WiFiScanClass::_scanComplete = false; + +size_t ESP8266WiFiScanClass::_scanCount = 0; +void* ESP8266WiFiScanClass::_scanResult = 0; + +/** + * Start scan WiFi networks available + * @param async run in async mode + * @param show_hidden show hidden networks + * @return Number of discovered networks + */ +int8_t ESP8266WiFiScanClass::scanNetworks(bool async, bool show_hidden) { + if(ESP8266WiFiScanClass::_scanStarted) { + return WIFI_SCAN_RUNNING; + } + + ESP8266WiFiScanClass::_scanAsync = async; + + WiFi.enableSTA(true); + + int status = wifi_station_get_connect_status(); + if(status != STATION_GOT_IP && status != STATION_IDLE) { + WiFi.disconnect(false); + } + + scanDelete(); + + struct scan_config config; + config.ssid = 0; + config.bssid = 0; + config.channel = 0; + config.show_hidden = show_hidden; + if(wifi_station_scan(&config, reinterpret_cast(&ESP8266WiFiScanClass::_scanDone))) { + ESP8266WiFiScanClass::_scanComplete = false; + ESP8266WiFiScanClass::_scanStarted = true; + + if(ESP8266WiFiScanClass::_scanAsync) { + delay(0); // time for the OS to trigger the scan + return WIFI_SCAN_RUNNING; + } + + esp_yield(); + return ESP8266WiFiScanClass::_scanCount; + } else { + return WIFI_SCAN_FAILED; + } + +} + + +/** + * called to get the scan state in Async mode + * @return scan result or status + * -1 if scan not fin + * -2 if scan not triggered + */ +int8_t ESP8266WiFiScanClass::scanComplete() { + + if(_scanStarted) { + return WIFI_SCAN_RUNNING; + } + + if(_scanComplete) { + return ESP8266WiFiScanClass::_scanCount; + } + + return WIFI_SCAN_FAILED; +} + +/** + * delete last scan result from RAM + */ +void ESP8266WiFiScanClass::scanDelete() { + if(ESP8266WiFiScanClass::_scanResult) { + delete[] reinterpret_cast(ESP8266WiFiScanClass::_scanResult); + ESP8266WiFiScanClass::_scanResult = 0; + ESP8266WiFiScanClass::_scanCount = 0; + } + _scanComplete = false; +} + + +/** + * loads all infos from a scanned wifi in to the ptr parameters + * @param networkItem uint8_t + * @param ssid const char** + * @param encryptionType uint8_t * + * @param RSSI int32_t * + * @param BSSID uint8_t ** + * @param channel int32_t * + * @param isHidden bool * + * @return (true if ok) + */ +bool ESP8266WiFiScanClass::getNetworkInfo(uint8_t i, String &ssid, uint8_t &encType, int32_t &rssi, uint8_t* &bssid, int32_t &channel, bool &isHidden) { + struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); + if(!it) { + return false; + } + + ssid = (const char*) it->ssid; + encType = encryptionType(i); + rssi = it->rssi; + bssid = it->bssid; // move ptr + channel = it->channel; + isHidden = (it->is_hidden != 0); + + return true; +} + + +/** + * Return the SSID discovered during the network scan. + * @param i specify from which network item want to get the information + * @return ssid string of the specified item on the networks scanned list + */ +String ESP8266WiFiScanClass::SSID(uint8_t i) { + struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); + if(!it) { + return ""; + } + + return String(reinterpret_cast(it->ssid)); +} + + +/** + * Return the encryption type of the networks discovered during the scanNetworks + * @param i specify from which network item want to get the information + * @return encryption type (enum wl_enc_type) of the specified item on the networks scanned list + */ +uint8_t ESP8266WiFiScanClass::encryptionType(uint8_t i) { + struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); + if(!it) { + return -1; + } + + switch(it->authmode) { + case AUTH_OPEN: + return ENC_TYPE_NONE; + case AUTH_WEP: + return ENC_TYPE_WEP; + case AUTH_WPA_PSK: + return ENC_TYPE_TKIP; + case AUTH_WPA2_PSK: + return ENC_TYPE_CCMP; + case AUTH_WPA_WPA2_PSK: + return ENC_TYPE_AUTO; + default: + return -1; + } +} + +/** + * Return the RSSI of the networks discovered during the scanNetworks + * @param i specify from which network item want to get the information + * @return signed value of RSSI of the specified item on the networks scanned list + */ +int32_t ESP8266WiFiScanClass::RSSI(uint8_t i) { + struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); + if(!it) { + return 0; + } + return it->rssi; +} + + +/** + * return MAC / BSSID of scanned wifi + * @param i specify from which network item want to get the information + * @return uint8_t * MAC / BSSID of scanned wifi + */ +uint8_t * ESP8266WiFiScanClass::BSSID(uint8_t i) { + struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); + if(!it) { + return 0; + } + return it->bssid; +} + +/** + * return MAC / BSSID of scanned wifi + * @param i specify from which network item want to get the information + * @return String MAC / BSSID of scanned wifi + */ +String ESP8266WiFiScanClass::BSSIDstr(uint8_t i) { + char mac[18] = { 0 }; + struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); + if(!it) { + return String(""); + } + sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", it->bssid[0], it->bssid[1], it->bssid[2], it->bssid[3], it->bssid[4], it->bssid[5]); + return String(mac); +} + +int32_t ESP8266WiFiScanClass::channel(uint8_t i) { + struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); + if(!it) { + return 0; + } + return it->channel; +} + +/** + * return if the scanned wifi is Hidden (no SSID) + * @param networkItem specify from which network item want to get the information + * @return bool (true == hidden) + */ +bool ESP8266WiFiScanClass::isHidden(uint8_t i) { + struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); + if(!it) { + return false; + } + return (it->is_hidden != 0); +} + +/** + * private + * scan callback + * @param result void *arg + * @param status STATUS + */ +void ESP8266WiFiScanClass::_scanDone(void* result, int status) { + if(status != OK) { + ESP8266WiFiScanClass::_scanCount = 0; + ESP8266WiFiScanClass::_scanResult = 0; + } else { + + int i = 0; + bss_info_head_t* head = reinterpret_cast(result); + + for(bss_info* it = STAILQ_FIRST(head); it; it = STAILQ_NEXT(it, next), ++i) + ; + ESP8266WiFiScanClass::_scanCount = i; + if(i == 0) { + ESP8266WiFiScanClass::_scanResult = 0; + } else { + bss_info* copied_info = new bss_info[i]; + i = 0; + for(bss_info* it = STAILQ_FIRST(head); it; it = STAILQ_NEXT(it, next), ++i) { + memcpy(copied_info + i, it, sizeof(bss_info)); + } + + ESP8266WiFiScanClass::_scanResult = copied_info; + } + + } + + ESP8266WiFiScanClass::_scanStarted = false; + ESP8266WiFiScanClass::_scanComplete = true; + + if(!ESP8266WiFiScanClass::_scanAsync) { + esp_schedule(); + } +} + +/** + * + * @param i specify from which network item want to get the information + * @return bss_info * + */ +void * ESP8266WiFiScanClass::_getScanInfoByIndex(int i) { + //TODO why its void * and not bss_info * ? + if(!ESP8266WiFiScanClass::_scanResult || (size_t) i > ESP8266WiFiScanClass::_scanCount) { + return 0; + } + return reinterpret_cast(ESP8266WiFiScanClass::_scanResult) + i; +} + diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiScan.h b/libraries/ESP8266WiFi/src/ESP8266WiFiScan.h new file mode 100644 index 000000000..21c65774f --- /dev/null +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiScan.h @@ -0,0 +1,70 @@ +/* + ESP8266WiFiScan.h - esp8266 Wifi support. + Based on WiFi.h from Ardiono WiFi shield library. + Copyright (c) 2011-2014 Arduino. All right reserved. + Modified by Ivan Grokhotkov, December 2014 + Reworked by Markus Sattler, December 2015 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef ESP8266WIFISCAN_H_ +#define ESP8266WIFISCAN_H_ + +#include "ESP8266WiFiType.h" +#include "ESP8266WiFiGeneric.h" + +class ESP8266WiFiScanClass { + + // ---------------------------------------------------------------------------------------------- + // ----------------------------------------- scan function -------------------------------------- + // ---------------------------------------------------------------------------------------------- + + public: + + int8_t scanNetworks(bool async = false, bool show_hidden = false); + + int8_t scanComplete(); + void scanDelete(); + + // scan result + bool getNetworkInfo(uint8_t networkItem, String &ssid, uint8_t &encryptionType, int32_t &RSSI, uint8_t* &BSSID, int32_t &channel, bool &isHidden); + + String SSID(uint8_t networkItem); + uint8_t encryptionType(uint8_t networkItem); + int32_t RSSI(uint8_t networkItem); + uint8_t * BSSID(uint8_t networkItem); + String BSSIDstr(uint8_t networkItem); + int32_t channel(uint8_t networkItem); + bool isHidden(uint8_t networkItem); + + protected: + + static bool _scanAsync; + static bool _scanStarted; + static bool _scanComplete; + + static size_t _scanCount; + static void* _scanResult; + + static void _scanDone(void* result, int status); + void * _getScanInfoByIndex(int i); + + friend class ESP8266WiFiClass; + +}; + + +#endif /* ESP8266WIFISCAN_H_ */ diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiType.h b/libraries/ESP8266WiFi/src/ESP8266WiFiType.h new file mode 100644 index 000000000..2774277d7 --- /dev/null +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiType.h @@ -0,0 +1,52 @@ +/* + ESP8266WiFiType.h - esp8266 Wifi support. + Copyright (c) 2011-2014 Arduino. All right reserved. + Modified by Ivan Grokhotkov, December 2014 + Reworked by Markus Sattler, December 2015 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +#ifndef ESP8266WIFITYPE_H_ +#define ESP8266WIFITYPE_H_ + +#include + +#define WIFI_SCAN_RUNNING (-1) +#define WIFI_SCAN_FAILED (-2) + +// Note: +// this enums need to be in sync with the SDK! + +typedef enum WiFiMode { + WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3 +} WiFiMode_t; + +typedef enum { + WIFI_PHY_MODE_11B = 1, WIFI_PHY_MODE_11G = 2, WIFI_PHY_MODE_11N = 3 +} WiFiPhyMode_t; + +typedef enum { + WIFI_NONE_SLEEP = 0, WIFI_LIGHT_SLEEP = 2, WIFI_MODEM_SLEEP = 3 +} WiFiSleepType_t; + + +extern "C" { +typedef STAILQ_HEAD(, bss_info) +bss_info_head_t; +} + +#endif /* ESP8266WIFITYPE_H_ */ From 373da3df6b27420efadc3c25ddac83cafe443429 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Tue, 29 Dec 2015 14:33:10 +0100 Subject: [PATCH 10/21] allow hook WiFi events from sketch --- .../ESP8266WiFi/src/ESP8266WiFiGeneric.cpp | 20 +++++++++++++------ .../ESP8266WiFi/src/ESP8266WiFiGeneric.h | 11 +++++++--- libraries/ESP8266WiFi/src/ESP8266WiFiScan.h | 4 +--- libraries/ESP8266WiFi/src/ESP8266WiFiType.h | 14 +++++++++++++ 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp index 89eb38078..01e137190 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp @@ -51,13 +51,20 @@ extern "C" void esp_yield(); // ----------------------------------------------------------------------------------------------------------------------- bool ESP8266WiFiGenericClass::_persistent = true; - - +WiFiEventCb ESP8266WiFiGenericClass::_cbEvent = NULL; ESP8266WiFiGenericClass::ESP8266WiFiGenericClass() { wifi_set_event_handler_cb((wifi_event_handler_cb_t) &ESP8266WiFiGenericClass::_eventCallback); } +/** + * set callback function + * @param cbEvent WiFiEventCb + */ +void ESP8266WiFiGenericClass::onEvent(WiFiEventCb cbEvent) { + _cbEvent = cbEvent; +} + /** * callback for WiFi events * @param arg @@ -70,11 +77,11 @@ void ESP8266WiFiGenericClass::_eventCallback(void* arg) { WiFiClient::stopAll(); } - //TODO allow user to hook this event + if(_cbEvent) { + _cbEvent((WiFiEvent_t) event->event); + } } - - /** * Return the current channel associated with the network * @return channel (1-13) @@ -236,8 +243,9 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul * @param callback_arg */ void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback_arg) { - if(ipaddr) + if(ipaddr) { (*reinterpret_cast(callback_arg)) = ipaddr->addr; + } esp_schedule(); // resume the hostByName function } diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h index c2df1026d..26381b43b 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h @@ -25,14 +25,19 @@ #include "ESP8266WiFiType.h" +typedef void (*WiFiEventCb)(WiFiEvent_t event); + class ESP8266WiFiGenericClass { // ---------------------------------------------------------------------------------------------- // -------------------------------------- Generic WiFi function --------------------------------- // ---------------------------------------------------------------------------------------------- public: + ESP8266WiFiGenericClass(); + void onEvent(WiFiEventCb cbEvent); + int32_t channel(void); bool setSleepMode(WiFiSleepType_t type); @@ -50,9 +55,10 @@ class ESP8266WiFiGenericClass { bool enableAP(bool enable); protected: - static bool _persistent; + static bool _persistent; + static WiFiEventCb _cbEvent; - static void _eventCallback(void *event); + static void _eventCallback(void *event); // ---------------------------------------------------------------------------------------------- // ------------------------------------ Generic Network function -------------------------------- @@ -69,5 +75,4 @@ class ESP8266WiFiGenericClass { friend class ESP8266WiFiAPClass; }; - #endif /* ESP8266WIFIGENERIC_H_ */ diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiScan.h b/libraries/ESP8266WiFi/src/ESP8266WiFiScan.h index 21c65774f..9d0c964a2 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiScan.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiScan.h @@ -60,9 +60,7 @@ class ESP8266WiFiScanClass { static void* _scanResult; static void _scanDone(void* result, int status); - void * _getScanInfoByIndex(int i); - - friend class ESP8266WiFiClass; + static void * _getScanInfoByIndex(int i); }; diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiType.h b/libraries/ESP8266WiFi/src/ESP8266WiFiType.h index 2774277d7..a5b10e93e 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiType.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiType.h @@ -44,6 +44,20 @@ typedef enum { } WiFiSleepType_t; +typedef enum { + WIFI_EVENT_STAMODE_CONNECTED = 0, + WIFI_EVENT_STAMODE_DISCONNECTED, + WIFI_EVENT_STAMODE_AUTHMODE_CHANGE, + WIFI_EVENT_STAMODE_GOT_IP, + WIFI_EVENT_STAMODE_DHCP_TIMEOUT, + WIFI_EVENT_SOFTAPMODE_STACONNECTED, + WIFI_EVENT_SOFTAPMODE_STADISCONNECTED, + WIFI_EVENT_SOFTAPMODE_PROBEREQRECVED, + WIFI_EVENT_MAX +} WiFiEvent_t; + + + extern "C" { typedef STAILQ_HEAD(, bss_info) bss_info_head_t; From 0ed104f028fc160f4bf06cdc9aa6bf2e67b55c52 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Tue, 29 Dec 2015 14:38:13 +0100 Subject: [PATCH 11/21] add WiFi Event example --- .../WiFiClientEvents/WiFiClientEvents.ino | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 libraries/ESP8266WiFi/examples/WiFiClientEvents/WiFiClientEvents.ino diff --git a/libraries/ESP8266WiFi/examples/WiFiClientEvents/WiFiClientEvents.ino b/libraries/ESP8266WiFi/examples/WiFiClientEvents/WiFiClientEvents.ino new file mode 100644 index 000000000..bdad5be30 --- /dev/null +++ b/libraries/ESP8266WiFi/examples/WiFiClientEvents/WiFiClientEvents.ino @@ -0,0 +1,48 @@ +/* + * This sketch shows the WiFi event usage + * + */ + +#include + +const char* ssid = "your-ssid"; +const char* password = "your-password"; + + +void WiFiEvent(WiFiEvent_t event) { + Serial.printf("[WiFi-event] event: %d\n", event); + + switch(event) { + case WIFI_EVENT_STAMODE_GOT_IP: + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + break; + case WIFI_EVENT_STAMODE_DISCONNECTED: + Serial.println("WiFi lost connection"); + break; + } +} + +void setup() { + Serial.begin(115200); + + // delete old config + WiFi.disconnect(true); + + delay(1000); + + WiFi.onEvent(WiFiEvent); + + WiFi.begin(ssid, password); + + Serial.println(); + Serial.println(); + Serial.println("Wait for WiFi... "); +} + + +void loop() { + delay(1000); +} + From 640d0bb65e2e891162f4fed617324d4ede59913e Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Tue, 29 Dec 2015 15:00:47 +0100 Subject: [PATCH 12/21] improve error handling and return values --- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 46 +++++----- libraries/ESP8266WiFi/src/ESP8266WiFiAP.h | 6 +- libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp | 95 ++++++++++++-------- libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h | 17 ++-- tools/sdk/include/user_interface.h | 6 +- 5 files changed, 97 insertions(+), 73 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index 05b6b1177..b598707c7 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -77,25 +77,24 @@ static bool softap_config_equal(const softap_config& lhs, const softap_config& r * @param channel WiFi channel number, 1 - 13. * @param ssid_hidden Network cloaking (0 = broadcast SSID, 1 = hide SSID) */ -void ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden) { +bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden) { if(!WiFi.enableAP(true)) { // enable AP failed - return; + return false; } if(!ssid || *ssid == 0 || strlen(ssid) > 31) { // fail SSID too long or missing! - return; + return false; } if(passphrase && strlen(passphrase) > 63) { // fail passphrase to long! - return; + return false; } struct softap_config conf; - wifi_softap_get_config(&conf); strcpy(reinterpret_cast(conf.ssid), ssid); conf.channel = channel; conf.ssid_len = strlen(ssid); @@ -115,15 +114,20 @@ void ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch wifi_softap_get_config(&conf_current); if(softap_config_equal(conf, conf_current)) { DEBUGV("softap config unchanged"); - return; + return true; } + bool ret; + ETS_UART_INTR_DISABLE(); - if(WiFi._persistent) - wifi_softap_set_config(&conf); - else - wifi_softap_set_config_current(&conf); + if(WiFi._persistent) { + ret = wifi_softap_set_config(&conf); + } else { + ret = wifi_softap_set_config_current(&conf); + } ETS_UART_INTR_ENABLE(); + + return ret; } @@ -133,11 +137,11 @@ void ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch * @param gateway gateway IP * @param subnet subnet mask */ -void ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { +bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { if(!WiFi.enableAP(true)) { // enable AP failed - return; + return false; } struct ip_info info; @@ -145,8 +149,10 @@ void ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA info.gw.addr = static_cast(gateway); info.netmask.addr = static_cast(subnet); wifi_softap_dhcps_stop(); - wifi_set_ip_info(SOFTAP_IF, &info); - wifi_softap_dhcps_start(); + if(wifi_set_ip_info(SOFTAP_IF, &info)) { + return wifi_softap_dhcps_start(); + } + return false; } @@ -156,24 +162,24 @@ void ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA * @param wifioff disable mode? * @return one value of wl_status_t enum */ -int ESP8266WiFiAPClass::softAPdisconnect(bool wifioff) { +bool ESP8266WiFiAPClass::softAPdisconnect(bool wifioff) { + bool ret; struct softap_config conf; *conf.ssid = 0; *conf.password = 0; ETS_UART_INTR_DISABLE(); if(WiFi._persistent) { - wifi_softap_set_config(&conf); + ret = wifi_softap_set_config(&conf); } else { - wifi_softap_set_config_current(&conf); + ret = wifi_softap_set_config_current(&conf); } ETS_UART_INTR_ENABLE(); if(wifioff) { - WiFi.enableAP(false); + ret = WiFi.enableAP(false); } - //TODO return with more meaning ? - return 0; + return ret; } /** diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h index e23878f66..4b378200a 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h @@ -36,9 +36,9 @@ class ESP8266WiFiAPClass { public: - void softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0); - void softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet); - int softAPdisconnect(bool wifioff = false); + bool softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0); + bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet); + bool softAPdisconnect(bool wifioff = false); IPAddress softAPIP(); diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp index 218247ca6..1419e34a8 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp @@ -92,7 +92,7 @@ bool ESP8266WiFiSTAClass::_useStaticIp = false; * @param channel Optional. Channel of AP * @return */ -int ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid) { +wl_status_t ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid) { if(!WiFi.enableSTA(true)) { // enable STA failed @@ -152,7 +152,7 @@ int ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase, int32_t return status(); } -int ESP8266WiFiSTAClass::begin(char* ssid, char *passphrase, int32_t channel, const uint8_t* bssid) { +wl_status_t ESP8266WiFiSTAClass::begin(char* ssid, char *passphrase, int32_t channel, const uint8_t* bssid) { return begin((const char*) ssid, (const char*) passphrase, channel, bssid); } @@ -160,7 +160,7 @@ int ESP8266WiFiSTAClass::begin(char* ssid, char *passphrase, int32_t channel, co * Use to connect to SDK config. * @return wl_status_t */ -int ESP8266WiFiSTAClass::begin() { +wl_status_t ESP8266WiFiSTAClass::begin() { if(!WiFi.enableSTA(true)) { // enable STA failed @@ -184,10 +184,10 @@ int ESP8266WiFiSTAClass::begin() { * @param gateway Static gateway configuration * @param subnet Static Subnet mask */ -void ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { +bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { if(!WiFi.enableSTA(true)) { - return; + return false; } struct ip_info info; @@ -196,9 +196,11 @@ void ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddres info.netmask.addr = static_cast(subnet); wifi_station_dhcpc_stop(); - wifi_set_ip_info(STATION_IF, &info); - - _useStaticIp = true; + if(wifi_set_ip_info(STATION_IF, &info)) { + _useStaticIp = true; + return true; + } + return false; } /** @@ -208,21 +210,30 @@ void ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddres * @param subnet Static Subnet mask * @param dns Static DNS server */ -void ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns) { +bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns) { + + if(!WiFi.enableSTA(true)) { + return false; + } + struct ip_info info; info.ip.addr = static_cast(local_ip); info.gw.addr = static_cast(gateway); info.netmask.addr = static_cast(subnet); wifi_station_dhcpc_stop(); - wifi_set_ip_info(STATION_IF, &info); + if(wifi_set_ip_info(STATION_IF, &info)) { + _useStaticIp = true; + } else { + return false; + } // Set DNS-Server ip_addr_t d; d.addr = static_cast(dns); dns_setserver(0, &d); - _useStaticIp = true; + return true; } /** @@ -243,7 +254,8 @@ bool ESP8266WiFiSTAClass::reconnect() { * @param wifioff * @return one value of wl_status_t enum */ -int ESP8266WiFiSTAClass::disconnect(bool wifioff) { +bool ESP8266WiFiSTAClass::disconnect(bool wifioff) { + bool ret; struct station_config conf; *conf.ssid = 0; *conf.password = 0; @@ -254,15 +266,14 @@ int ESP8266WiFiSTAClass::disconnect(bool wifioff) { } else { wifi_station_set_config_current(&conf); } - wifi_station_disconnect(); + ret = wifi_station_disconnect(); ETS_UART_INTR_ENABLE(); if(wifioff) { WiFi.enableSTA(false); } - //TODO return with more meaning ? - return 0; + return ret; } /** @@ -391,18 +402,20 @@ bool ESP8266WiFiSTAClass::hostname(String aHostname) { * */ wl_status_t ESP8266WiFiSTAClass::status() { - int status = wifi_station_get_connect_status(); + station_status_t status = wifi_station_get_connect_status(); - if(status == STATION_GOT_IP) { - return WL_CONNECTED; - } else if(status == STATION_NO_AP_FOUND) { - return WL_NO_SSID_AVAIL; - } else if(status == STATION_CONNECT_FAIL || status == STATION_WRONG_PASSWORD) { - return WL_CONNECT_FAILED; - } else if(status == STATION_IDLE) { - return WL_IDLE_STATUS; - } else { - return WL_DISCONNECTED; + switch(status) { + case STATION_GOT_IP: + return WL_CONNECTED; + case STATION_NO_AP_FOUND: + return WL_NO_SSID_AVAIL; + case STATION_CONNECT_FAIL: + case STATION_WRONG_PASSWORD: + return WL_CONNECT_FAILED; + case STATION_IDLE: + return WL_IDLE_STATUS; + default: + return WL_DISCONNECTED; } } @@ -543,32 +556,38 @@ bool ESP8266WiFiSTAClass::_smartConfigDone = false; /** * Start SmartConfig */ -void ESP8266WiFiSTAClass::beginSmartConfig() { - if(_smartConfigStarted) - return; +bool ESP8266WiFiSTAClass::beginSmartConfig() { + if(_smartConfigStarted) { + return false; + } if(!WiFi.enableSTA(true)) { // enable STA failed - return; + return false; } - _smartConfigStarted = true; - _smartConfigDone = false; - - smartconfig_start(reinterpret_cast(&ESP8266WiFiSTAClass::_smartConfigCallback), 1); + if(smartconfig_start(reinterpret_cast(&ESP8266WiFiSTAClass::_smartConfigCallback), 1)) { + _smartConfigStarted = true; + _smartConfigDone = false; + return true; + } + return false; } /** * Stop SmartConfig */ -void ESP8266WiFiSTAClass::stopSmartConfig() { +bool ESP8266WiFiSTAClass::stopSmartConfig() { if(!_smartConfigStarted) { - return; + return true; } - smartconfig_stop(); - _smartConfigStarted = false; + if(smartconfig_stop()) { + _smartConfigStarted = false; + return true; + } + return false; } /** diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h index 409577707..cfc15af3a 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h @@ -35,15 +35,15 @@ class ESP8266WiFiSTAClass { public: - int begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL); - int begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL); - int begin(); + wl_status_t begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL); + wl_status_t begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL); + wl_status_t begin(); - void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet); - void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns); + bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet); + bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns); bool reconnect(); - int disconnect(bool wifioff = false); + bool disconnect(bool wifioff = false); uint8_t waitForConnectResult(); @@ -84,10 +84,9 @@ class ESP8266WiFiSTAClass { bool beginWPSConfig(void); - void beginSmartConfig(); + bool beginSmartConfig(); + bool stopSmartConfig(); bool smartConfigDone(); - void stopSmartConfig(); - protected: diff --git a/tools/sdk/include/user_interface.h b/tools/sdk/include/user_interface.h index 5ecf3a868..470556c70 100644 --- a/tools/sdk/include/user_interface.h +++ b/tools/sdk/include/user_interface.h @@ -222,21 +222,21 @@ bool wifi_station_set_auto_connect(uint8 set); bool wifi_station_set_reconnect_policy(bool set); -enum { +typedef enum { STATION_IDLE = 0, STATION_CONNECTING, STATION_WRONG_PASSWORD, STATION_NO_AP_FOUND, STATION_CONNECT_FAIL, STATION_GOT_IP -}; +} station_status_t; enum dhcp_status { DHCP_STOPPED, DHCP_STARTED }; -uint8 wifi_station_get_connect_status(void); +station_status_t wifi_station_get_connect_status(void); uint8 wifi_station_get_current_ap_id(void); bool wifi_station_ap_change(uint8 current_ap_id); From 7edcda4a0f9beb23101e710643e1a1d001d1b082 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Tue, 29 Dec 2015 15:26:14 +0100 Subject: [PATCH 13/21] IPAddress allow uint32_t compare --- cores/esp8266/IPAddress.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cores/esp8266/IPAddress.h b/cores/esp8266/IPAddress.h index b563c7044..4876358fc 100644 --- a/cores/esp8266/IPAddress.h +++ b/cores/esp8266/IPAddress.h @@ -56,6 +56,9 @@ class IPAddress: public Printable { bool operator==(const IPAddress& addr) const { return _address.dword == addr._address.dword; } + bool operator==(uint32_t addr) const { + return _address.dword == addr; + } bool operator==(const uint8_t* addr) const; // Overloaded index operator to allow getting and setting individual octets of the address From 293e55c6903f5f9de3e5c3d69016c86af3b41398 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Tue, 29 Dec 2015 15:28:29 +0100 Subject: [PATCH 14/21] check for WiFi pass < 8 (not allowed for WPA2) simplify STA config and allow setting of second DNS server for fallback code style --- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 16 +++-- .../ESP8266WiFi/src/ESP8266WiFiGeneric.cpp | 6 +- libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp | 67 +++++++------------ libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h | 4 +- libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp | 1 - 5 files changed, 41 insertions(+), 53 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index b598707c7..8a0626530 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -54,14 +54,18 @@ static bool softap_config_equal(const softap_config& lhs, const softap_config& r * @return equal */ static bool softap_config_equal(const softap_config& lhs, const softap_config& rhs) { - if(strcmp(reinterpret_cast(lhs.ssid), reinterpret_cast(rhs.ssid)) != 0) + if(strcmp(reinterpret_cast(lhs.ssid), reinterpret_cast(rhs.ssid)) != 0) { return false; - if(strcmp(reinterpret_cast(lhs.password), reinterpret_cast(rhs.password)) != 0) + } + if(strcmp(reinterpret_cast(lhs.password), reinterpret_cast(rhs.password)) != 0) { return false; - if(lhs.channel != rhs.channel) + } + if(lhs.channel != rhs.channel) { return false; - if(lhs.ssid_hidden != rhs.ssid_hidden) + } + if(lhs.ssid_hidden != rhs.ssid_hidden) { return false; + } return true; } @@ -89,8 +93,8 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch return false; } - if(passphrase && strlen(passphrase) > 63) { - // fail passphrase to long! + if(passphrase && (strlen(passphrase) > 63 || strlen(passphrase) < 8)) { + // fail passphrase to long or short! return false; } diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp index 01e137190..3b13549f7 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp @@ -71,7 +71,7 @@ void ESP8266WiFiGenericClass::onEvent(WiFiEventCb cbEvent) { */ void ESP8266WiFiGenericClass::_eventCallback(void* arg) { System_Event_t* event = reinterpret_cast(arg); - DEBUGV("wifi evt: %d\r\n", event->event); + DEBUGV("wifi evt: %d\n", event->event); if(event->event == EVENT_STAMODE_DISCONNECTED) { WiFiClient::stopAll(); @@ -170,7 +170,7 @@ WiFiMode_t ESP8266WiFiGenericClass::getMode() { */ bool ESP8266WiFiGenericClass::enableSTA(bool enable) { - WiFiMode_t currentMode = (WiFiMode_t) wifi_get_opmode(); + WiFiMode_t currentMode = getMode(); bool isEnabled = ((currentMode & WIFI_STA) != 0); if(isEnabled != enable) { @@ -191,7 +191,7 @@ bool ESP8266WiFiGenericClass::enableSTA(bool enable) { */ bool ESP8266WiFiGenericClass::enableAP(bool enable){ - WiFiMode_t currentMode = (WiFiMode_t) wifi_get_opmode(); + WiFiMode_t currentMode = getMode(); bool isEnabled = ((currentMode & WIFI_AP) != 0); if(isEnabled != enable) { diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp index 1419e34a8..822ba21f8 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp @@ -57,21 +57,22 @@ static bool sta_config_equal(const station_config& lhs, const station_config& rh * @return equal */ static bool sta_config_equal(const station_config& lhs, const station_config& rhs) { - if(strcmp(reinterpret_cast(lhs.ssid), reinterpret_cast(rhs.ssid)) != 0) + if(strcmp(reinterpret_cast(lhs.ssid), reinterpret_cast(rhs.ssid)) != 0) { return false; + } - if(strcmp(reinterpret_cast(lhs.password), reinterpret_cast(rhs.password)) != 0) + if(strcmp(reinterpret_cast(lhs.password), reinterpret_cast(rhs.password)) != 0) { return false; + } + + if(lhs.bssid_set != rhs.bssid_set) { + return false; + } if(lhs.bssid_set) { - if(!rhs.bssid_set) - return false; - - if(memcmp(lhs.bssid, rhs.bssid, 6) != 0) - return false; - } else { - if(rhs.bssid_set) + if(memcmp(lhs.bssid, rhs.bssid, 6) != 0) { return false; + } } return true; @@ -178,39 +179,15 @@ wl_status_t ESP8266WiFiSTAClass::begin() { } -/** - * Change IP configuration settings disabling the dhcp client - * @param local_ip Static ip configuration - * @param gateway Static gateway configuration - * @param subnet Static Subnet mask - */ -bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) { - - if(!WiFi.enableSTA(true)) { - return false; - } - - struct ip_info info; - info.ip.addr = static_cast(local_ip); - info.gw.addr = static_cast(gateway); - info.netmask.addr = static_cast(subnet); - - wifi_station_dhcpc_stop(); - if(wifi_set_ip_info(STATION_IF, &info)) { - _useStaticIp = true; - return true; - } - return false; -} - /** * Change IP configuration settings disabling the dhcp client * @param local_ip Static ip configuration * @param gateway Static gateway configuration * @param subnet Static Subnet mask - * @param dns Static DNS server + * @param dns1 Static DNS server 1 + * @param dns2 Static DNS server 2 */ -bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns) { +bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2) { if(!WiFi.enableSTA(true)) { return false; @@ -227,11 +204,19 @@ bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddres } else { return false; } - - // Set DNS-Server ip_addr_t d; - d.addr = static_cast(dns); - dns_setserver(0, &d); + + if(dns1 != (uint32_t)0x00000000) { + // Set DNS1-Server + d.addr = static_cast(dns1); + dns_setserver(0, &d); + } + + if(dns2 != (uint32_t)0x00000000) { + // Set DNS2-Server + d.addr = static_cast(dns2); + dns_setserver(1, &d); + } return true; } @@ -617,7 +602,7 @@ void ESP8266WiFiSTAClass::_smartConfigCallback(uint32_t st, void* result) { wifi_station_disconnect(); wifi_station_connect(); - WiFi._smartConfigDone = true; + _smartConfigDone = true; } else if(status == SC_STATUS_LINK_OVER) { WiFi.stopSmartConfig(); } diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h index cfc15af3a..d854db5c4 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h @@ -39,8 +39,7 @@ class ESP8266WiFiSTAClass { wl_status_t begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL); wl_status_t begin(); - bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet); - bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns); + bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000); bool reconnect(); bool disconnect(bool wifioff = false); @@ -92,6 +91,7 @@ class ESP8266WiFiSTAClass { static bool _smartConfigStarted; static bool _smartConfigDone; + static void _smartConfigCallback(uint32_t status, void* result); }; diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp index 223f4ae26..bf9edd261 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp @@ -314,7 +314,6 @@ void ESP8266WiFiScanClass::_scanDone(void* result, int status) { * @return bss_info * */ void * ESP8266WiFiScanClass::_getScanInfoByIndex(int i) { - //TODO why its void * and not bss_info * ? if(!ESP8266WiFiScanClass::_scanResult || (size_t) i > ESP8266WiFiScanClass::_scanCount) { return 0; } From 8943ac402cbdf16364864af7a442b7088dae061a Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Tue, 29 Dec 2015 15:40:54 +0100 Subject: [PATCH 15/21] add softAPgetStationNum --- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 9 +++++++++ libraries/ESP8266WiFi/src/ESP8266WiFiAP.h | 2 ++ 2 files changed, 11 insertions(+) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index 8a0626530..6c911263a 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -186,6 +186,15 @@ bool ESP8266WiFiAPClass::softAPdisconnect(bool wifioff) { return ret; } + +/** + * Get the count of the Station / client that are connected to the softAP interface + * @return Stations count + */ +uint8_t ESP8266WiFiAPClass::softAPgetStationNum() { + return wifi_softap_get_station_num(); +} + /** * Get the softAP interface IP address. * @return IPAddress softAP IP diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h index 4b378200a..e248d81f8 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h @@ -40,6 +40,8 @@ class ESP8266WiFiAPClass { bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet); bool softAPdisconnect(bool wifioff = false); + uint8_t softAPgetStationNum(); + IPAddress softAPIP(); uint8_t* softAPmacAddress(uint8_t* mac); From bbeaeaad54c4295e8f94a7d61f866f910a95d3a2 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Tue, 29 Dec 2015 15:56:53 +0100 Subject: [PATCH 16/21] add WiFi TX power control --- .../ESP8266WiFi/src/ESP8266WiFiGeneric.cpp | 17 +++++++++++++++++ libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h | 2 ++ 2 files changed, 19 insertions(+) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp index 3b13549f7..78c55f76f 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp @@ -124,6 +124,23 @@ WiFiPhyMode_t ESP8266WiFiGenericClass::getPhyMode() { return (WiFiPhyMode_t) wifi_get_phy_mode(); } +/** + * set the output power of WiFi + * @param dBm max: +20.5dBm min: 0dBm + */ +void ESP8266WiFiGenericClass::setOutputPower(float_t dBm) { + + if(dBm > 20.5) { + dBm = 20.5; + } else if(dBm < 0) { + dBm = 0; + } + + uint8_t val = (dBm*4.0f); + system_phy_set_max_tpw(val); +} + + /** * store WiFi config in SDK flash area * @param persistent diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h index 26381b43b..2232941c9 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h @@ -46,6 +46,8 @@ class ESP8266WiFiGenericClass { bool setPhyMode(WiFiPhyMode_t mode); WiFiPhyMode_t getPhyMode(); + void setOutputPower(float_t dBm); + void persistent(bool persistent); bool mode(WiFiMode_t); From 61440d9e2b7b0a1d4fce83e970a7c9ff6daccdba Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Tue, 29 Dec 2015 16:14:27 +0100 Subject: [PATCH 17/21] add WiFi sleep management Note: testing needed --- .../ESP8266WiFi/src/ESP8266WiFiGeneric.cpp | 39 ++++++++++++++++++- .../ESP8266WiFi/src/ESP8266WiFiGeneric.h | 6 ++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp index 78c55f76f..241206148 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp @@ -52,6 +52,7 @@ extern "C" void esp_yield(); bool ESP8266WiFiGenericClass::_persistent = true; WiFiEventCb ESP8266WiFiGenericClass::_cbEvent = NULL; +WiFiMode_t ESP8266WiFiGenericClass::_forceSleepLastMode = WIFI_OFF; ESP8266WiFiGenericClass::ESP8266WiFiGenericClass() { wifi_set_event_handler_cb((wifi_event_handler_cb_t) &ESP8266WiFiGenericClass::_eventCallback); @@ -128,7 +129,7 @@ WiFiPhyMode_t ESP8266WiFiGenericClass::getPhyMode() { * set the output power of WiFi * @param dBm max: +20.5dBm min: 0dBm */ -void ESP8266WiFiGenericClass::setOutputPower(float_t dBm) { +void ESP8266WiFiGenericClass::setOutputPower(float dBm) { if(dBm > 20.5) { dBm = 20.5; @@ -223,7 +224,43 @@ bool ESP8266WiFiGenericClass::enableAP(bool enable){ } +/** + * Disable WiFi for x us when value is not 0 + * @param sleep_time_in_us + * @return ok + */ +bool ESP8266WiFiGenericClass::forceSleepBegin(uint32 sleepUs) { + _forceSleepLastMode = getMode(); + if(!mode(WIFI_OFF)) { + return false; + } + if(sleepUs == 0) { + sleepUs = 0xFFFFFFF; + } + + wifi_fpm_set_sleep_type(MODEM_SLEEP_T); + wifi_fpm_open(); + return (wifi_fpm_do_sleep(sleepUs) == 0); +} + +/** + * wake up WiFi Modem + * @return ok + */ +bool ESP8266WiFiGenericClass::forceSleepWake() { + wifi_fpm_do_wakeup(); + wifi_fpm_close(); + + // restore last mode + if(mode(_forceSleepLastMode)) { + if((_forceSleepLastMode & WIFI_STA) != 0){ + wifi_station_connect(); + } + return true; + } + return false; +} // ----------------------------------------------------------------------------------------------------------------------- diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h index 2232941c9..83b4c3fc5 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h @@ -46,7 +46,7 @@ class ESP8266WiFiGenericClass { bool setPhyMode(WiFiPhyMode_t mode); WiFiPhyMode_t getPhyMode(); - void setOutputPower(float_t dBm); + void setOutputPower(float dBm); void persistent(bool persistent); @@ -56,9 +56,13 @@ class ESP8266WiFiGenericClass { bool enableSTA(bool enable); bool enableAP(bool enable); + bool forceSleepBegin(uint32 sleepUs = 0); + bool forceSleepWake(); + protected: static bool _persistent; static WiFiEventCb _cbEvent; + static WiFiMode_t _forceSleepLastMode; static void _eventCallback(void *event); From 85905c12f2f84298977c4000ed004187cbd32582 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Tue, 29 Dec 2015 17:31:57 +0100 Subject: [PATCH 18/21] allow multiple event callbacks and add filter option --- cores/esp8266/Arduino.h | 4 ++ .../ESP8266WiFi/src/ESP8266WiFiGeneric.cpp | 46 +++++++++++++++++-- .../ESP8266WiFi/src/ESP8266WiFiGeneric.h | 9 +++- 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/cores/esp8266/Arduino.h b/cores/esp8266/Arduino.h index 10c6ebd44..2a88be9dc 100644 --- a/cores/esp8266/Arduino.h +++ b/cores/esp8266/Arduino.h @@ -247,8 +247,12 @@ void optimistic_yield(uint32_t interval_us); #include "Updater.h" #include "debug.h" +#ifndef _GLIBCXX_VECTOR +// arduino is not compatible with std::vector #define min(a,b) ((a)<(b)?(a):(b)) #define max(a,b) ((a)>(b)?(a):(b)) +#endif + #define _min(a,b) ((a)<(b)?(a):(b)) #define _max(a,b) ((a)>(b)?(a):(b)) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp index 241206148..2d1b10818 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp @@ -43,6 +43,10 @@ extern "C" { #include "debug.h" +#undef min +#undef max +#include + extern "C" void esp_schedule(); extern "C" void esp_yield(); @@ -50,8 +54,10 @@ extern "C" void esp_yield(); // ------------------------------------------------- Generic WiFi function ----------------------------------------------- // ----------------------------------------------------------------------------------------------------------------------- +// arduino dont like std::vectors move static here +static std::vector cbEventList; + bool ESP8266WiFiGenericClass::_persistent = true; -WiFiEventCb ESP8266WiFiGenericClass::_cbEvent = NULL; WiFiMode_t ESP8266WiFiGenericClass::_forceSleepLastMode = WIFI_OFF; ESP8266WiFiGenericClass::ESP8266WiFiGenericClass() { @@ -61,9 +67,34 @@ ESP8266WiFiGenericClass::ESP8266WiFiGenericClass() { /** * set callback function * @param cbEvent WiFiEventCb + * @param event optional filter (WIFI_EVENT_MAX is all events) */ -void ESP8266WiFiGenericClass::onEvent(WiFiEventCb cbEvent) { - _cbEvent = cbEvent; +void ESP8266WiFiGenericClass::onEvent(WiFiEventCb cbEvent, WiFiEvent_t event) { + if(!cbEvent) { + return; + } + WiFiEventCbList_t newEventHandler; + newEventHandler.cb = cbEvent; + newEventHandler.event = event; + cbEventList.push_back(newEventHandler); +} + +/** + * removes a callback form event handler + * @param cbEvent WiFiEventCb + * @param event optional filter (WIFI_EVENT_MAX is all events) + */ +void ESP8266WiFiGenericClass::removeEvent(WiFiEventCb cbEvent, WiFiEvent_t event) { + if(!cbEvent) { + return; + } + + for(uint32_t i = 0; i < cbEventList.size(); i++) { + WiFiEventCbList_t entry = cbEventList[i]; + if(entry.cb == cbEvent && entry.event == event) { + cbEventList.erase(cbEventList.begin() + i); + } + } } /** @@ -78,8 +109,13 @@ void ESP8266WiFiGenericClass::_eventCallback(void* arg) { WiFiClient::stopAll(); } - if(_cbEvent) { - _cbEvent((WiFiEvent_t) event->event); + 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); + } + } } } diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h index 83b4c3fc5..80a7c946f 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h @@ -27,6 +27,11 @@ typedef void (*WiFiEventCb)(WiFiEvent_t event); +typedef struct { + WiFiEventCb cb; + WiFiEvent_t event; +} WiFiEventCbList_t; + class ESP8266WiFiGenericClass { // ---------------------------------------------------------------------------------------------- // -------------------------------------- Generic WiFi function --------------------------------- @@ -36,7 +41,8 @@ class ESP8266WiFiGenericClass { ESP8266WiFiGenericClass(); - void onEvent(WiFiEventCb cbEvent); + void onEvent(WiFiEventCb cbEvent, WiFiEvent_t event = WIFI_EVENT_MAX); + void removeEvent(WiFiEventCb cbEvent, WiFiEvent_t event = WIFI_EVENT_MAX); int32_t channel(void); @@ -61,7 +67,6 @@ class ESP8266WiFiGenericClass { protected: static bool _persistent; - static WiFiEventCb _cbEvent; static WiFiMode_t _forceSleepLastMode; static void _eventCallback(void *event); From d9a7a816d51535b21fec38cb5db30cc8511075c3 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Tue, 29 Dec 2015 17:44:00 +0100 Subject: [PATCH 19/21] include core_esp8266_features.h in arduino.h for easier usage, and add WIFI_HAS_EVENT_CALLBACK --- cores/esp8266/Arduino.h | 1 + cores/esp8266/core_esp8266_features.h | 2 ++ 2 files changed, 3 insertions(+) diff --git a/cores/esp8266/Arduino.h b/cores/esp8266/Arduino.h index 2a88be9dc..b6f442e5c 100644 --- a/cores/esp8266/Arduino.h +++ b/cores/esp8266/Arduino.h @@ -37,6 +37,7 @@ extern "C" { #include "binary.h" #include "esp8266_peri.h" #include "twi.h" +#include "core_esp8266_features.h" #define HIGH 0x1 #define LOW 0x0 diff --git a/cores/esp8266/core_esp8266_features.h b/cores/esp8266/core_esp8266_features.h index ae5d58539..0dc202383 100644 --- a/cores/esp8266/core_esp8266_features.h +++ b/cores/esp8266/core_esp8266_features.h @@ -28,6 +28,8 @@ #define CORE_HAS_LIBB64 #define CORE_HAS_BASE64_CLASS +#define WIFI_HAS_EVENT_CALLBACK + #endif From 4a68612af862e0f11ea226bd57927e6aee1d991d Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Tue, 29 Dec 2015 21:01:48 +0100 Subject: [PATCH 20/21] add API for connect / reconnect management --- libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp | 46 ++++++++++++++++++-- libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h | 9 +++- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp index 822ba21f8..522776061 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp @@ -91,9 +91,10 @@ bool ESP8266WiFiSTAClass::_useStaticIp = false; * @param passphrase const char * Optional. Passphrase. Valid characters in a passphrase must be between ASCII 32-126 (decimal). * @param bssid uint8_t[6] Optional. BSSID / MAC of AP * @param channel Optional. Channel of AP + * @param connect Optional. call connect * @return */ -wl_status_t ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid) { +wl_status_t ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid, bool connect) { if(!WiFi.enableSTA(true)) { // enable STA failed @@ -134,12 +135,17 @@ wl_status_t ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase, } ETS_UART_INTR_DISABLE(); + if(WiFi._persistent) { wifi_station_set_config(&conf); } else { wifi_station_set_config_current(&conf); } - wifi_station_connect(); + + if(connect) { + wifi_station_connect(); + } + ETS_UART_INTR_ENABLE(); if(channel > 0 && channel <= 13) { @@ -153,8 +159,8 @@ wl_status_t ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase, return status(); } -wl_status_t ESP8266WiFiSTAClass::begin(char* ssid, char *passphrase, int32_t channel, const uint8_t* bssid) { - return begin((const char*) ssid, (const char*) passphrase, channel, bssid); +wl_status_t ESP8266WiFiSTAClass::begin(char* ssid, char *passphrase, int32_t channel, const uint8_t* bssid, bool connect) { + return begin((const char*) ssid, (const char*) passphrase, channel, bssid, connect); } /** @@ -261,6 +267,38 @@ bool ESP8266WiFiSTAClass::disconnect(bool wifioff) { return ret; } +/** + * Setting the ESP8266 station to connect to the AP (which is recorded) + * automatically or not when powered on. Enable auto-connect by default. + * @param autoConnect bool + * @return if saved + */ +bool ESP8266WiFiSTAClass::setAutoConnect(bool autoConnect) { + bool ret; + ETS_UART_INTR_DISABLE(); + ret = wifi_station_set_auto_connect(autoConnect); + ETS_UART_INTR_ENABLE(); + return ret; +} + +/** + * Checks if ESP8266 station mode will connect to AP + * automatically or not when it is powered on. + * @return auto connect + */ +bool ESP8266WiFiSTAClass::getAutoConnect() { + return (wifi_station_get_auto_connect() != 0); +} + +/** + * Set whether reconnect or not when the ESP8266 station is disconnected from AP. + * @param autoReconnect + * @return + */ +bool ESP8266WiFiSTAClass::setAutoReconnect(bool autoReconnect) { + return wifi_station_set_reconnect_policy(autoReconnect); +} + /** * Wait for WiFi connection to reach a result * returns the status reached or disconnect if STA is off diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h index d854db5c4..c4d10046f 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h @@ -35,8 +35,8 @@ class ESP8266WiFiSTAClass { public: - wl_status_t begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL); - wl_status_t begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL); + wl_status_t begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true); + wl_status_t begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true); wl_status_t begin(); bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000); @@ -44,6 +44,11 @@ class ESP8266WiFiSTAClass { bool reconnect(); bool disconnect(bool wifioff = false); + bool setAutoConnect(bool autoConnect); + bool getAutoConnect(); + + bool setAutoReconnect(bool autoReconnect); + uint8_t waitForConnectResult(); // STA network info From db4076fd41c21f97792425ce068e0f6de21d9fe1 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Wed, 30 Dec 2015 17:40:36 +0100 Subject: [PATCH 21/21] add more debug for disconnect add isConnected function for easier usage --- libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp | 1 + libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp | 9 +++++++++ libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h | 2 ++ 3 files changed, 12 insertions(+) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp index 2d1b10818..79f80c1af 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp @@ -106,6 +106,7 @@ void ESP8266WiFiGenericClass::_eventCallback(void* arg) { DEBUGV("wifi evt: %d\n", event->event); if(event->event == EVENT_STAMODE_DISCONNECTED) { + DEBUGV("STA disconnect: %d\n", event->event_info.disconnected.reason); WiFiClient::stopAll(); } diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp index 522776061..0d34a5a64 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp @@ -267,6 +267,15 @@ bool ESP8266WiFiSTAClass::disconnect(bool wifioff) { return ret; } +/** + * is STA interface connected? + * @return true if STA is connected to an AD + */ +bool ESP8266WiFiSTAClass::isConnected() { + return (status() == WL_CONNECTED); +} + + /** * Setting the ESP8266 station to connect to the AP (which is recorded) * automatically or not when powered on. Enable auto-connect by default. diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h index c4d10046f..4e87f8826 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h @@ -44,6 +44,8 @@ class ESP8266WiFiSTAClass { bool reconnect(); bool disconnect(bool wifioff = false); + bool isConnected(); + bool setAutoConnect(bool autoConnect); bool getAutoConnect();