Skip to content

Commit

Permalink
SmartConfig refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
igrr committed Apr 21, 2015
1 parent 6558d8e commit f0cf0be
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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<const char*>(conf.ssid);
const char* passphrase = reinterpret_cast<const char*>(conf.password);

begin(ssid,passphrase);

}

int ESP8266WiFiClass::begin(const char* ssid)
{
Expand Down Expand Up @@ -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<station_config*>(result);

wifi_station_set_config(sta_conf);
wifi_station_disconnect();
wifi_station_connect();

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*
Expand Down Expand Up @@ -216,22 +207,26 @@ 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;

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;

Expand Down
2 changes: 1 addition & 1 deletion hardware/esp8266com/esp8266/platform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f0cf0be

Please sign in to comment.