Skip to content

Commit

Permalink
add basic WPS function
Browse files Browse the repository at this point in the history
  • Loading branch information
Links2004 authored and igrr committed Jul 7, 2015
1 parent 8efa1d4 commit 0cc1925
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

#include "ESP8266WiFi.h"

extern "C" {
#include "c_types.h"
#include "ets_sys.h"
Expand Down Expand Up @@ -155,7 +156,7 @@ void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress s
_useStaticIp = true;
}

int ESP8266WiFiClass::disconnect()
int ESP8266WiFiClass::disconnect(bool wifioff)
{
struct station_config conf;
*conf.ssid = 0;
Expand All @@ -164,6 +165,19 @@ int ESP8266WiFiClass::disconnect()
wifi_station_set_config(&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);
}
}

return 0;
}

Expand Down Expand Up @@ -601,6 +615,77 @@ bool ESP8266WiFiClass::hostname(String aHostname) {
return hostname((char*) aHostname.c_str());
}

//--------------------------------------------------------------

void wifi_wps_status_cb(WPS_CB_STATUS_t status)
{
DEBUGV("wps cb status: %d\r\n", status);
switch (status) {
case WPS_CB_ST_SUCCESS:
if(!wifi_wps_disable()) {
DEBUGV("wps disable faild\n");
}
wifi_station_connect();
break;
case WPS_CB_ST_FAILED:
DEBUGV("wps FAILD\n");
break;
case WPS_CB_ST_TIMEOUT:
DEBUGV("wps TIMEOUT\n");
break;
}
// todo user function to get status

esp_schedule(); // resume the beginWPSConfig function
}

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: %d\n", wps_type);

if(!wifi_wps_disable()) {
DEBUGV("wps disable faild\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 faild\n");
return false;
}

if(!wifi_set_wps_cb(&wifi_wps_status_cb)) {
DEBUGV("wps cb faild\n");
return false;
}

if(!wifi_wps_start()) {
DEBUGV("wps start faild\n");
return false;
}

esp_yield();
// will return here when wifi_wps_status_cb fires

return true;
}

//--------------------------------------------------------------



void ESP8266WiFiClass::beginSmartConfig()
{
if (_smartConfigStarted)
Expand Down Expand Up @@ -655,6 +740,8 @@ void ESP8266WiFiClass::_smartConfigCallback(uint32_t st, void* result)
}
}

//--------------------------------------------------------------

void ESP8266WiFiClass::_eventCallback(void* arg)
{
System_Event_t* event = reinterpret_cast<System_Event_t*>(arg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class ESP8266WiFiClass
*
* return: one value of wl_status_t enum
*/
int disconnect(void);
int disconnect(bool wifioff = false);

/*
* Get the station interface MAC address.
Expand Down Expand Up @@ -313,6 +313,13 @@ class ESP8266WiFiClass
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).
*
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 @@ -24,7 +24,7 @@ compiler.S.flags=-c -g -x assembler-with-cpp -MMD
compiler.c.elf.flags=-g -Os -nostdlib -Wl,--no-check-sections -u call_user_start -Wl,-static "-L{compiler.sdk.path}/lib" "-L{compiler.sdk.path}/ld" "-T{build.flash_ld}" -Wl,-wrap,system_restart_local -Wl,-wrap,register_chipv6_phy

compiler.c.elf.cmd=xtensa-lx106-elf-gcc
compiler.c.elf.libs=-lm -lgcc -lhal -lphy -lnet80211 -llwip -lwpa -lmain -lpp -lsmartconfig -lwps
compiler.c.elf.libs=-lm -lgcc -lhal -lphy -lnet80211 -llwip -lwpa -lmain -lpp -lsmartconfig -lwps -lcrypto

compiler.cpp.cmd=xtensa-lx106-elf-g++
compiler.cpp.flags=-c -Os -g -mlongcalls -mtext-section-literals -fno-exceptions -fno-rtti -falign-functions=4 -std=c++11 -MMD
Expand Down
10 changes: 5 additions & 5 deletions hardware/esp8266com/esp8266/tools/sdk/include/user_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,20 +427,20 @@ typedef enum wps_type {
WPS_TYPE_PBC,
WPS_TYPE_PIN,
WPS_TYPE_DISPLAY,
WPS_TYPE_MAX,
WPS_TYPE_MAX
} WPS_TYPE_t;

enum wps_cb_status {
typedef enum wps_cb_status {
WPS_CB_ST_SUCCESS = 0,
WPS_CB_ST_FAILED,
WPS_CB_ST_TIMEOUT,
};
WPS_CB_ST_TIMEOUT
} WPS_CB_STATUS_t;

bool wifi_wps_enable(WPS_TYPE_t wps_type);
bool wifi_wps_disable(void);
bool wifi_wps_start(void);

typedef void (*wps_st_cb_t)(int status);
typedef void (*wps_st_cb_t)(WPS_CB_STATUS_t status);
bool wifi_set_wps_cb(wps_st_cb_t cb);

#endif

0 comments on commit 0cc1925

Please sign in to comment.