Skip to content

Commit 32a5a06

Browse files
committed
add basic WPS function
1 parent 1f36203 commit 32a5a06

File tree

4 files changed

+102
-8
lines changed

4 files changed

+102
-8
lines changed

hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp

+88-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121

2222
#include "ESP8266WiFi.h"
23+
2324
extern "C" {
2425
#include "c_types.h"
2526
#include "ets_sys.h"
@@ -155,7 +156,7 @@ void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress s
155156
_useStaticIp = true;
156157
}
157158

158-
int ESP8266WiFiClass::disconnect()
159+
int ESP8266WiFiClass::disconnect(bool wifioff)
159160
{
160161
struct station_config conf;
161162
*conf.ssid = 0;
@@ -164,6 +165,19 @@ int ESP8266WiFiClass::disconnect()
164165
wifi_station_set_config(&conf);
165166
wifi_station_disconnect();
166167
ETS_UART_INTR_ENABLE();
168+
169+
if(wifioff) {
170+
_useClientMode = false;
171+
172+
if(_useApMode) {
173+
// turn on AP
174+
mode(WIFI_AP);
175+
} else {
176+
// turn wifi off
177+
mode(WIFI_OFF);
178+
}
179+
}
180+
167181
return 0;
168182
}
169183

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

618+
//--------------------------------------------------------------
619+
620+
void wifi_wps_status_cb(WPS_CB_STATUS_t status)
621+
{
622+
DEBUGV("wps cb status: %d\r\n", status);
623+
switch (status) {
624+
case WPS_CB_ST_SUCCESS:
625+
if(!wifi_wps_disable()) {
626+
DEBUGV("wps disable faild\n");
627+
}
628+
wifi_station_connect();
629+
break;
630+
case WPS_CB_ST_FAILED:
631+
DEBUGV("wps FAILD\n");
632+
break;
633+
case WPS_CB_ST_TIMEOUT:
634+
DEBUGV("wps TIMEOUT\n");
635+
break;
636+
}
637+
// todo user function to get status
638+
639+
esp_schedule(); // resume the beginWPSConfig function
640+
}
641+
642+
bool ESP8266WiFiClass::beginWPSConfig(void) {
643+
644+
_useClientMode = true;
645+
646+
if(_useApMode) {
647+
// turn on AP+STA mode
648+
mode(WIFI_AP_STA);
649+
} else {
650+
// turn on STA mode
651+
mode(WIFI_STA);
652+
}
653+
654+
disconnect();
655+
656+
DEBUGV("wps begin: %d\n", wps_type);
657+
658+
if(!wifi_wps_disable()) {
659+
DEBUGV("wps disable faild\n");
660+
return false;
661+
}
662+
663+
// so far only WPS_TYPE_PBC is supported (SDK 1.2.0)
664+
if(!wifi_wps_enable(WPS_TYPE_PBC)) {
665+
DEBUGV("wps enable faild\n");
666+
return false;
667+
}
668+
669+
if(!wifi_set_wps_cb(&wifi_wps_status_cb)) {
670+
DEBUGV("wps cb faild\n");
671+
return false;
672+
}
673+
674+
if(!wifi_wps_start()) {
675+
DEBUGV("wps start faild\n");
676+
return false;
677+
}
678+
679+
esp_yield();
680+
// will return here when wifi_wps_status_cb fires
681+
682+
return true;
683+
}
684+
685+
//--------------------------------------------------------------
686+
687+
688+
604689
void ESP8266WiFiClass::beginSmartConfig()
605690
{
606691
if (_smartConfigStarted)
@@ -655,6 +740,8 @@ void ESP8266WiFiClass::_smartConfigCallback(uint32_t st, void* result)
655740
}
656741
}
657742

743+
//--------------------------------------------------------------
744+
658745
void ESP8266WiFiClass::_eventCallback(void* arg)
659746
{
660747
System_Event_t* event = reinterpret_cast<System_Event_t*>(arg);

hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class ESP8266WiFiClass
109109
*
110110
* return: one value of wl_status_t enum
111111
*/
112-
int disconnect(void);
112+
int disconnect(bool wifioff = false);
113113

114114
/*
115115
* Get the station interface MAC address.
@@ -313,6 +313,13 @@ class ESP8266WiFiClass
313313
bool hostname(const char* aHostname);
314314
bool hostname(String aHostname);
315315

316+
317+
/**
318+
* WPS config
319+
* so far only WPS_TYPE_PBC is supported (SDK 1.2.0)
320+
*/
321+
bool beginWPSConfig(void);
322+
316323
/*
317324
* Output WiFi settings to an object derived from Print interface (like Serial).
318325
*

hardware/esp8266com/esp8266/platform.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ compiler.S.flags=-c -g -x assembler-with-cpp -MMD
2424
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
2525

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

2929
compiler.cpp.cmd=xtensa-lx106-elf-g++
3030
compiler.cpp.flags=-c -Os -g -mlongcalls -mtext-section-literals -fno-exceptions -fno-rtti -falign-functions=4 -std=c++11 -MMD

hardware/esp8266com/esp8266/tools/sdk/include/user_interface.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -427,20 +427,20 @@ typedef enum wps_type {
427427
WPS_TYPE_PBC,
428428
WPS_TYPE_PIN,
429429
WPS_TYPE_DISPLAY,
430-
WPS_TYPE_MAX,
430+
WPS_TYPE_MAX
431431
} WPS_TYPE_t;
432432

433-
enum wps_cb_status {
433+
typedef enum wps_cb_status {
434434
WPS_CB_ST_SUCCESS = 0,
435435
WPS_CB_ST_FAILED,
436-
WPS_CB_ST_TIMEOUT,
437-
};
436+
WPS_CB_ST_TIMEOUT
437+
} WPS_CB_STATUS_t;
438438

439439
bool wifi_wps_enable(WPS_TYPE_t wps_type);
440440
bool wifi_wps_disable(void);
441441
bool wifi_wps_start(void);
442442

443-
typedef void (*wps_st_cb_t)(int status);
443+
typedef void (*wps_st_cb_t)(WPS_CB_STATUS_t status);
444444
bool wifi_set_wps_cb(wps_st_cb_t cb);
445445

446446
#endif

0 commit comments

Comments
 (0)