Skip to content

Commit 640d0bb

Browse files
committed
improve error handling and return values
1 parent 0ed104f commit 640d0bb

File tree

5 files changed

+97
-73
lines changed

5 files changed

+97
-73
lines changed

libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp

+26-20
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,24 @@ static bool softap_config_equal(const softap_config& lhs, const softap_config& r
7777
* @param channel WiFi channel number, 1 - 13.
7878
* @param ssid_hidden Network cloaking (0 = broadcast SSID, 1 = hide SSID)
7979
*/
80-
void ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden) {
80+
bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden) {
8181

8282
if(!WiFi.enableAP(true)) {
8383
// enable AP failed
84-
return;
84+
return false;
8585
}
8686

8787
if(!ssid || *ssid == 0 || strlen(ssid) > 31) {
8888
// fail SSID too long or missing!
89-
return;
89+
return false;
9090
}
9191

9292
if(passphrase && strlen(passphrase) > 63) {
9393
// fail passphrase to long!
94-
return;
94+
return false;
9595
}
9696

9797
struct softap_config conf;
98-
wifi_softap_get_config(&conf);
9998
strcpy(reinterpret_cast<char*>(conf.ssid), ssid);
10099
conf.channel = channel;
101100
conf.ssid_len = strlen(ssid);
@@ -115,15 +114,20 @@ void ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
115114
wifi_softap_get_config(&conf_current);
116115
if(softap_config_equal(conf, conf_current)) {
117116
DEBUGV("softap config unchanged");
118-
return;
117+
return true;
119118
}
120119

120+
bool ret;
121+
121122
ETS_UART_INTR_DISABLE();
122-
if(WiFi._persistent)
123-
wifi_softap_set_config(&conf);
124-
else
125-
wifi_softap_set_config_current(&conf);
123+
if(WiFi._persistent) {
124+
ret = wifi_softap_set_config(&conf);
125+
} else {
126+
ret = wifi_softap_set_config_current(&conf);
127+
}
126128
ETS_UART_INTR_ENABLE();
129+
130+
return ret;
127131
}
128132

129133

@@ -133,20 +137,22 @@ void ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
133137
* @param gateway gateway IP
134138
* @param subnet subnet mask
135139
*/
136-
void ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) {
140+
bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) {
137141

138142
if(!WiFi.enableAP(true)) {
139143
// enable AP failed
140-
return;
144+
return false;
141145
}
142146

143147
struct ip_info info;
144148
info.ip.addr = static_cast<uint32_t>(local_ip);
145149
info.gw.addr = static_cast<uint32_t>(gateway);
146150
info.netmask.addr = static_cast<uint32_t>(subnet);
147151
wifi_softap_dhcps_stop();
148-
wifi_set_ip_info(SOFTAP_IF, &info);
149-
wifi_softap_dhcps_start();
152+
if(wifi_set_ip_info(SOFTAP_IF, &info)) {
153+
return wifi_softap_dhcps_start();
154+
}
155+
return false;
150156
}
151157

152158

@@ -156,24 +162,24 @@ void ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA
156162
* @param wifioff disable mode?
157163
* @return one value of wl_status_t enum
158164
*/
159-
int ESP8266WiFiAPClass::softAPdisconnect(bool wifioff) {
165+
bool ESP8266WiFiAPClass::softAPdisconnect(bool wifioff) {
166+
bool ret;
160167
struct softap_config conf;
161168
*conf.ssid = 0;
162169
*conf.password = 0;
163170
ETS_UART_INTR_DISABLE();
164171
if(WiFi._persistent) {
165-
wifi_softap_set_config(&conf);
172+
ret = wifi_softap_set_config(&conf);
166173
} else {
167-
wifi_softap_set_config_current(&conf);
174+
ret = wifi_softap_set_config_current(&conf);
168175
}
169176
ETS_UART_INTR_ENABLE();
170177

171178
if(wifioff) {
172-
WiFi.enableAP(false);
179+
ret = WiFi.enableAP(false);
173180
}
174181

175-
//TODO return with more meaning ?
176-
return 0;
182+
return ret;
177183
}
178184

179185
/**

libraries/ESP8266WiFi/src/ESP8266WiFiAP.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ class ESP8266WiFiAPClass {
3636

3737
public:
3838

39-
void softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0);
40-
void softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet);
41-
int softAPdisconnect(bool wifioff = false);
39+
bool softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0);
40+
bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet);
41+
bool softAPdisconnect(bool wifioff = false);
4242

4343
IPAddress softAPIP();
4444

libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp

+57-38
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ bool ESP8266WiFiSTAClass::_useStaticIp = false;
9292
* @param channel Optional. Channel of AP
9393
* @return
9494
*/
95-
int ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid) {
95+
wl_status_t ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid) {
9696

9797
if(!WiFi.enableSTA(true)) {
9898
// enable STA failed
@@ -152,15 +152,15 @@ int ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase, int32_t
152152
return status();
153153
}
154154

155-
int ESP8266WiFiSTAClass::begin(char* ssid, char *passphrase, int32_t channel, const uint8_t* bssid) {
155+
wl_status_t ESP8266WiFiSTAClass::begin(char* ssid, char *passphrase, int32_t channel, const uint8_t* bssid) {
156156
return begin((const char*) ssid, (const char*) passphrase, channel, bssid);
157157
}
158158

159159
/**
160160
* Use to connect to SDK config.
161161
* @return wl_status_t
162162
*/
163-
int ESP8266WiFiSTAClass::begin() {
163+
wl_status_t ESP8266WiFiSTAClass::begin() {
164164

165165
if(!WiFi.enableSTA(true)) {
166166
// enable STA failed
@@ -184,10 +184,10 @@ int ESP8266WiFiSTAClass::begin() {
184184
* @param gateway Static gateway configuration
185185
* @param subnet Static Subnet mask
186186
*/
187-
void ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) {
187+
bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) {
188188

189189
if(!WiFi.enableSTA(true)) {
190-
return;
190+
return false;
191191
}
192192

193193
struct ip_info info;
@@ -196,9 +196,11 @@ void ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddres
196196
info.netmask.addr = static_cast<uint32_t>(subnet);
197197

198198
wifi_station_dhcpc_stop();
199-
wifi_set_ip_info(STATION_IF, &info);
200-
201-
_useStaticIp = true;
199+
if(wifi_set_ip_info(STATION_IF, &info)) {
200+
_useStaticIp = true;
201+
return true;
202+
}
203+
return false;
202204
}
203205

204206
/**
@@ -208,21 +210,30 @@ void ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddres
208210
* @param subnet Static Subnet mask
209211
* @param dns Static DNS server
210212
*/
211-
void ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns) {
213+
bool ESP8266WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns) {
214+
215+
if(!WiFi.enableSTA(true)) {
216+
return false;
217+
}
218+
212219
struct ip_info info;
213220
info.ip.addr = static_cast<uint32_t>(local_ip);
214221
info.gw.addr = static_cast<uint32_t>(gateway);
215222
info.netmask.addr = static_cast<uint32_t>(subnet);
216223

217224
wifi_station_dhcpc_stop();
218-
wifi_set_ip_info(STATION_IF, &info);
225+
if(wifi_set_ip_info(STATION_IF, &info)) {
226+
_useStaticIp = true;
227+
} else {
228+
return false;
229+
}
219230

220231
// Set DNS-Server
221232
ip_addr_t d;
222233
d.addr = static_cast<uint32_t>(dns);
223234
dns_setserver(0, &d);
224235

225-
_useStaticIp = true;
236+
return true;
226237
}
227238

228239
/**
@@ -243,7 +254,8 @@ bool ESP8266WiFiSTAClass::reconnect() {
243254
* @param wifioff
244255
* @return one value of wl_status_t enum
245256
*/
246-
int ESP8266WiFiSTAClass::disconnect(bool wifioff) {
257+
bool ESP8266WiFiSTAClass::disconnect(bool wifioff) {
258+
bool ret;
247259
struct station_config conf;
248260
*conf.ssid = 0;
249261
*conf.password = 0;
@@ -254,15 +266,14 @@ int ESP8266WiFiSTAClass::disconnect(bool wifioff) {
254266
} else {
255267
wifi_station_set_config_current(&conf);
256268
}
257-
wifi_station_disconnect();
269+
ret = wifi_station_disconnect();
258270
ETS_UART_INTR_ENABLE();
259271

260272
if(wifioff) {
261273
WiFi.enableSTA(false);
262274
}
263275

264-
//TODO return with more meaning ?
265-
return 0;
276+
return ret;
266277
}
267278

268279
/**
@@ -391,18 +402,20 @@ bool ESP8266WiFiSTAClass::hostname(String aHostname) {
391402
*
392403
*/
393404
wl_status_t ESP8266WiFiSTAClass::status() {
394-
int status = wifi_station_get_connect_status();
405+
station_status_t status = wifi_station_get_connect_status();
395406

396-
if(status == STATION_GOT_IP) {
397-
return WL_CONNECTED;
398-
} else if(status == STATION_NO_AP_FOUND) {
399-
return WL_NO_SSID_AVAIL;
400-
} else if(status == STATION_CONNECT_FAIL || status == STATION_WRONG_PASSWORD) {
401-
return WL_CONNECT_FAILED;
402-
} else if(status == STATION_IDLE) {
403-
return WL_IDLE_STATUS;
404-
} else {
405-
return WL_DISCONNECTED;
407+
switch(status) {
408+
case STATION_GOT_IP:
409+
return WL_CONNECTED;
410+
case STATION_NO_AP_FOUND:
411+
return WL_NO_SSID_AVAIL;
412+
case STATION_CONNECT_FAIL:
413+
case STATION_WRONG_PASSWORD:
414+
return WL_CONNECT_FAILED;
415+
case STATION_IDLE:
416+
return WL_IDLE_STATUS;
417+
default:
418+
return WL_DISCONNECTED;
406419
}
407420
}
408421

@@ -543,32 +556,38 @@ bool ESP8266WiFiSTAClass::_smartConfigDone = false;
543556
/**
544557
* Start SmartConfig
545558
*/
546-
void ESP8266WiFiSTAClass::beginSmartConfig() {
547-
if(_smartConfigStarted)
548-
return;
559+
bool ESP8266WiFiSTAClass::beginSmartConfig() {
560+
if(_smartConfigStarted) {
561+
return false;
562+
}
549563

550564
if(!WiFi.enableSTA(true)) {
551565
// enable STA failed
552-
return;
566+
return false;
553567
}
554568

555-
_smartConfigStarted = true;
556-
_smartConfigDone = false;
557-
558-
smartconfig_start(reinterpret_cast<sc_callback_t>(&ESP8266WiFiSTAClass::_smartConfigCallback), 1);
569+
if(smartconfig_start(reinterpret_cast<sc_callback_t>(&ESP8266WiFiSTAClass::_smartConfigCallback), 1)) {
570+
_smartConfigStarted = true;
571+
_smartConfigDone = false;
572+
return true;
573+
}
574+
return false;
559575
}
560576

561577

562578
/**
563579
* Stop SmartConfig
564580
*/
565-
void ESP8266WiFiSTAClass::stopSmartConfig() {
581+
bool ESP8266WiFiSTAClass::stopSmartConfig() {
566582
if(!_smartConfigStarted) {
567-
return;
583+
return true;
568584
}
569585

570-
smartconfig_stop();
571-
_smartConfigStarted = false;
586+
if(smartconfig_stop()) {
587+
_smartConfigStarted = false;
588+
return true;
589+
}
590+
return false;
572591
}
573592

574593
/**

libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h

+8-9
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ class ESP8266WiFiSTAClass {
3535

3636
public:
3737

38-
int begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL);
39-
int begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL);
40-
int begin();
38+
wl_status_t begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL);
39+
wl_status_t begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL);
40+
wl_status_t begin();
4141

42-
void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet);
43-
void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns);
42+
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet);
43+
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns);
4444

4545
bool reconnect();
46-
int disconnect(bool wifioff = false);
46+
bool disconnect(bool wifioff = false);
4747

4848
uint8_t waitForConnectResult();
4949

@@ -84,10 +84,9 @@ class ESP8266WiFiSTAClass {
8484

8585
bool beginWPSConfig(void);
8686

87-
void beginSmartConfig();
87+
bool beginSmartConfig();
88+
bool stopSmartConfig();
8889
bool smartConfigDone();
89-
void stopSmartConfig();
90-
9190

9291
protected:
9392

tools/sdk/include/user_interface.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -222,21 +222,21 @@ bool wifi_station_set_auto_connect(uint8 set);
222222

223223
bool wifi_station_set_reconnect_policy(bool set);
224224

225-
enum {
225+
typedef enum {
226226
STATION_IDLE = 0,
227227
STATION_CONNECTING,
228228
STATION_WRONG_PASSWORD,
229229
STATION_NO_AP_FOUND,
230230
STATION_CONNECT_FAIL,
231231
STATION_GOT_IP
232-
};
232+
} station_status_t;
233233

234234
enum dhcp_status {
235235
DHCP_STOPPED,
236236
DHCP_STARTED
237237
};
238238

239-
uint8 wifi_station_get_connect_status(void);
239+
station_status_t wifi_station_get_connect_status(void);
240240

241241
uint8 wifi_station_get_current_ap_id(void);
242242
bool wifi_station_ap_change(uint8 current_ap_id);

0 commit comments

Comments
 (0)