mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-27 21:16:50 +03:00
improve error handling and return values
This commit is contained in:
parent
0ed104f028
commit
640d0bb65e
@ -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<char*>(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<uint32_t>(gateway);
|
||||
info.netmask.addr = static_cast<uint32_t>(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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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<uint32_t>(subnet);
|
||||
|
||||
wifi_station_dhcpc_stop();
|
||||
wifi_set_ip_info(STATION_IF, &info);
|
||||
|
||||
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<uint32_t>(local_ip);
|
||||
info.gw.addr = static_cast<uint32_t>(gateway);
|
||||
info.netmask.addr = static_cast<uint32_t>(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<uint32_t>(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,17 +402,19 @@ 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) {
|
||||
switch(status) {
|
||||
case STATION_GOT_IP:
|
||||
return WL_CONNECTED;
|
||||
} else if(status == STATION_NO_AP_FOUND) {
|
||||
case STATION_NO_AP_FOUND:
|
||||
return WL_NO_SSID_AVAIL;
|
||||
} else if(status == STATION_CONNECT_FAIL || status == STATION_WRONG_PASSWORD) {
|
||||
case STATION_CONNECT_FAIL:
|
||||
case STATION_WRONG_PASSWORD:
|
||||
return WL_CONNECT_FAILED;
|
||||
} else if(status == STATION_IDLE) {
|
||||
case STATION_IDLE:
|
||||
return WL_IDLE_STATUS;
|
||||
} else {
|
||||
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;
|
||||
}
|
||||
|
||||
if(smartconfig_start(reinterpret_cast<sc_callback_t>(&ESP8266WiFiSTAClass::_smartConfigCallback), 1)) {
|
||||
_smartConfigStarted = true;
|
||||
_smartConfigDone = false;
|
||||
|
||||
smartconfig_start(reinterpret_cast<sc_callback_t>(&ESP8266WiFiSTAClass::_smartConfigCallback), 1);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Stop SmartConfig
|
||||
*/
|
||||
void ESP8266WiFiSTAClass::stopSmartConfig() {
|
||||
bool ESP8266WiFiSTAClass::stopSmartConfig() {
|
||||
if(!_smartConfigStarted) {
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
|
||||
smartconfig_stop();
|
||||
if(smartconfig_stop()) {
|
||||
_smartConfigStarted = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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:
|
||||
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user