From f0cf0bec7478ac2f03c70bc86dd9a62cbafef9aa Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 21 Apr 2015 20:30:22 +0800 Subject: [PATCH] SmartConfig refactoring --- .../WiFiSmartConfig.ino} | 7 ++- .../libraries/ESP8266WiFi/src/ESP8266WiFi.cpp | 45 +++++++++++-------- .../libraries/ESP8266WiFi/src/ESP8266WiFi.h | 23 ++++------ hardware/esp8266com/esp8266/platform.txt | 2 +- 4 files changed, 40 insertions(+), 37 deletions(-) rename hardware/esp8266com/esp8266/libraries/ESP8266WiFi/examples/{SmartConfig => WiFiSmartConfig/WiFiSmartConfig.ino} (85%) diff --git a/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/examples/SmartConfig b/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/examples/WiFiSmartConfig/WiFiSmartConfig.ino similarity index 85% rename from hardware/esp8266com/esp8266/libraries/ESP8266WiFi/examples/SmartConfig rename to hardware/esp8266com/esp8266/libraries/ESP8266WiFi/examples/WiFiSmartConfig/WiFiSmartConfig.ino index 7ae7e53e21..4a62d5a2e2 100644 --- a/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/examples/SmartConfig +++ b/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/examples/WiFiSmartConfig/WiFiSmartConfig.ino @@ -4,21 +4,20 @@ WiFiUDP Udp; void setup() { - uint8_t cnt = 0; + int cnt = 0; Serial.begin(115200); WiFi.mode(WIFI_STA); - WiFi.begin(); while(WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); if(cnt++ >= 10){ - WiFi.setSmartLink(); + WiFi.beginSmartConfig(); while(1){ delay(1000); - if(WiFi.getSmartlinkStatus() == SC_STATUS_LINK_OVER){ + if(WiFi.smartConfigDone()){ Serial.println("SmartConfig Success"); break; } diff --git a/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp b/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp index 7e3995c41f..2e27d53dcf 100644 --- a/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp +++ b/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp @@ -27,6 +27,7 @@ extern "C" { #include "osapi.h" #include "mem.h" #include "user_interface.h" +#include "smartconfig.h" #include "lwip/opt.h" #include "lwip/err.h" #include "lwip/dns.h" @@ -48,17 +49,6 @@ void ESP8266WiFiClass::mode(WiFiMode m) ETS_UART_INTR_ENABLE(); } -int ESP8266WiFiClass::begin() -{ - static struct station_config conf; - wifi_station_get_config(&conf); - - const char* ssid = reinterpret_cast(conf.ssid); - const char* passphrase = reinterpret_cast(conf.password); - - begin(ssid,passphrase); - -} int ESP8266WiFiClass::begin(const char* ssid) { @@ -377,23 +367,42 @@ int ESP8266WiFiClass::hostByName(const char* aHostname, IPAddress& aResult) return (aResult != 0) ? 1 : 0; } -void ESP8266WiFiClass::setSmartLink() +void ESP8266WiFiClass::beginSmartConfig() { - smartconfig_start(SC_TYPE_ESPTOUCH,smartconfig_call_back);//SC_TYPE_ESPTOUCH use ESPTOUCH for smartconfig, or use SC_TYPE_AIRKISS for AIRKISS + if (_smartConfigStarted) + return; + + WiFi.mode(WIFI_STA); + + _smartConfigStarted = true; + + //SC_TYPE_ESPTOUCH use ESPTOUCH for smartconfig, or use SC_TYPE_AIRKISS for AIRKISS + smartconfig_start(SC_TYPE_ESPTOUCH, &ESP8266WiFiClass::_smartConfigDone); } -sc_status ESP8266WiFiClass::getSmartlinkStatus(){ - return smartconfig_get_status();//when smartconfig complete, return SC_STATUS_LINK_OVER +void ESP8266WiFiClass::stopSmartConfig() +{ + if (!_smartConfigStarted) + return; + + smartconfig_stop(); + _smartConfigStarted = false; } -void smartconfig_call_back(void *data) +bool ESP8266WiFiClass::smartConfigDone(){ + if (!_smartConfigStarted) + return false; + + return smartconfig_get_status() == SC_STATUS_LINK_OVER; +} + +void ESP8266WiFiClass::_smartConfigDone(void* result) { - struct station_config *sta_conf = (struct station_config *)data; + station_config* sta_conf = reinterpret_cast(result); wifi_station_set_config(sta_conf); wifi_station_disconnect(); wifi_station_connect(); - } diff --git a/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.h b/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.h index 76a450e32d..7267662db3 100644 --- a/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.h +++ b/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.h @@ -26,15 +26,12 @@ extern "C" { #include "include/wl_definitions.h" - #include "smartconfig.h" } #include "IPAddress.h" #include "WiFiClient.h" #include "WiFiServer.h" -void smartconfig_call_back(void *data); - enum WiFiMode { WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3 }; class ESP8266WiFiClass @@ -44,13 +41,7 @@ class ESP8266WiFiClass ESP8266WiFiClass(); void mode(WiFiMode); - - /* Start Wifi reconnection - * used for Smartconfig - * no param. - */ - int begin(); - + /* Start Wifi connection for OPEN networks * @@ -216,15 +207,16 @@ class ESP8266WiFiClass /* * Start SmartConfig * - */ - void setSmartLink(); + */ + void beginSmartConfig(); /* * Query SmartConfig status, to decide when stop config * */ - sc_status getSmartlinkStatus(); - + bool smartConfigDone(); + + void stopSmartConfig(); friend class WiFiClient; friend class WiFiServer; @@ -232,6 +224,9 @@ class ESP8266WiFiClass protected: static void _scanDone(void* result, int status); void * _getScanInfoByIndex(int i); + static void _smartConfigDone(void* result); + bool _smartConfigStarted = false; + static size_t _scanCount; static void* _scanResult; diff --git a/hardware/esp8266com/esp8266/platform.txt b/hardware/esp8266com/esp8266/platform.txt index 3490ef4167..feb2b15592 100644 --- a/hardware/esp8266com/esp8266/platform.txt +++ b/hardware/esp8266com/esp8266/platform.txt @@ -23,7 +23,7 @@ compiler.S.flags=-c -g -x assembler-with-cpp -MMD compiler.c.elf.ldscript=eagle.app.v6.ld compiler.c.elf.flags=-nostdlib -Wl,--no-check-sections -u call_user_start -Wl,-static "-L{compiler.sdk.path}/lib" "-L{compiler.sdk.path}/ld" "-T{compiler.c.elf.ldscript}" compiler.c.elf.cmd=xtensa-lx106-elf-gcc -compiler.c.elf.libs=-lgcc -lm -lhal -lphy -lnet80211 -llwip -lwpa -lmain -lpp +compiler.c.elf.libs=-lgcc -lm -lhal -lphy -lnet80211 -llwip -lwpa -lmain -lpp -lsmartconfig compiler.cpp.cmd=xtensa-lx106-elf-g++ compiler.cpp.flags=-c -Os -mlongcalls -mtext-section-literals -fno-exceptions -fno-rtti -std=c++11 -MMD