Skip to content

Commit bad5390

Browse files
committed
Prevent exceptions in WiFi if not yet started
1 parent 95b8754 commit bad5390

File tree

2 files changed

+74
-5
lines changed

2 files changed

+74
-5
lines changed

libraries/WiFi/src/WiFiAP.cpp

+31-3
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ bool WiFiAPClass::softAPdisconnect(bool wifioff)
184184
{
185185
bool ret;
186186
wifi_config_t conf;
187+
188+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
189+
return ESP_ERR_INVALID_STATE;
190+
}
191+
187192
*conf.ap.ssid = 0;
188193
*conf.ap.password = 0;
189194
conf.ap.authmode = WIFI_AUTH_OPEN; // auth must be open if pass=0
@@ -204,6 +209,9 @@ bool WiFiAPClass::softAPdisconnect(bool wifioff)
204209
uint8_t WiFiAPClass::softAPgetStationNum()
205210
{
206211
wifi_sta_list_t clients;
212+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
213+
return 0;
214+
}
207215
if(esp_wifi_ap_get_sta_list(&clients) == ESP_OK) {
208216
return clients.num;
209217
}
@@ -217,6 +225,9 @@ uint8_t WiFiAPClass::softAPgetStationNum()
217225
IPAddress WiFiAPClass::softAPIP()
218226
{
219227
tcpip_adapter_ip_info_t ip;
228+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
229+
return IPAddress();
230+
}
220231
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &ip);
221232
return IPAddress(ip.ip.addr);
222233
}
@@ -229,7 +240,9 @@ IPAddress WiFiAPClass::softAPIP()
229240
*/
230241
uint8_t* WiFiAPClass::softAPmacAddress(uint8_t* mac)
231242
{
232-
esp_wifi_get_mac(WIFI_IF_AP, mac);
243+
if(WiFiGenericClass::getMode() != WIFI_MODE_NULL){
244+
esp_wifi_get_mac(WIFI_IF_AP, mac);
245+
}
233246
return mac;
234247
}
235248

@@ -241,6 +254,9 @@ String WiFiAPClass::softAPmacAddress(void)
241254
{
242255
uint8_t mac[6];
243256
char macStr[18] = { 0 };
257+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
258+
return String();
259+
}
244260
esp_wifi_get_mac(WIFI_IF_AP, mac);
245261

246262
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
@@ -253,9 +269,12 @@ String WiFiAPClass::softAPmacAddress(void)
253269
*/
254270
const char * WiFiAPClass::softAPgetHostname()
255271
{
256-
const char * hostname;
272+
const char * hostname = NULL;
273+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
274+
return hostname;
275+
}
257276
if(tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_AP, &hostname)) {
258-
return NULL;
277+
return hostname;
259278
}
260279
return hostname;
261280
}
@@ -267,6 +286,9 @@ const char * WiFiAPClass::softAPgetHostname()
267286
*/
268287
bool WiFiAPClass::softAPsetHostname(const char * hostname)
269288
{
289+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
290+
return false;
291+
}
270292
return tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_AP, hostname) == ESP_OK;
271293
}
272294

@@ -276,6 +298,9 @@ bool WiFiAPClass::softAPsetHostname(const char * hostname)
276298
*/
277299
bool WiFiAPClass::softAPenableIpV6()
278300
{
301+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
302+
return false;
303+
}
279304
return tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_AP) == ESP_OK;
280305
}
281306

@@ -286,6 +311,9 @@ bool WiFiAPClass::softAPenableIpV6()
286311
IPv6Address WiFiAPClass::softAPIPv6()
287312
{
288313
static ip6_addr_t addr;
314+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
315+
return IPv6Address();
316+
}
289317
if(tcpip_adapter_get_ip6_linklocal(TCPIP_ADAPTER_IF_AP, &addr)) {
290318
return IPv6Address();
291319
}

libraries/WiFi/src/WiFiSTA.cpp

+43-2
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,9 @@ uint8_t WiFiSTAClass::waitForConnectResult()
399399
*/
400400
IPAddress WiFiSTAClass::localIP()
401401
{
402+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
403+
return IPAddress();
404+
}
402405
tcpip_adapter_ip_info_t ip;
403406
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip);
404407
return IPAddress(ip.ip.addr);
@@ -412,7 +415,9 @@ IPAddress WiFiSTAClass::localIP()
412415
*/
413416
uint8_t* WiFiSTAClass::macAddress(uint8_t* mac)
414417
{
415-
esp_wifi_get_mac(WIFI_IF_STA, mac);
418+
if(WiFiGenericClass::getMode() != WIFI_MODE_NULL){
419+
esp_wifi_get_mac(WIFI_IF_STA, mac);
420+
}
416421
return mac;
417422
}
418423

@@ -424,6 +429,9 @@ String WiFiSTAClass::macAddress(void)
424429
{
425430
uint8_t mac[6];
426431
char macStr[18] = { 0 };
432+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
433+
return String();
434+
}
427435
esp_wifi_get_mac(WIFI_IF_STA, mac);
428436

429437
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
@@ -436,6 +444,9 @@ String WiFiSTAClass::macAddress(void)
436444
*/
437445
IPAddress WiFiSTAClass::subnetMask()
438446
{
447+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
448+
return IPAddress();
449+
}
439450
tcpip_adapter_ip_info_t ip;
440451
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip);
441452
return IPAddress(ip.netmask.addr);
@@ -447,6 +458,9 @@ IPAddress WiFiSTAClass::subnetMask()
447458
*/
448459
IPAddress WiFiSTAClass::gatewayIP()
449460
{
461+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
462+
return IPAddress();
463+
}
450464
tcpip_adapter_ip_info_t ip;
451465
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip);
452466
return IPAddress(ip.gw.addr);
@@ -459,6 +473,9 @@ IPAddress WiFiSTAClass::gatewayIP()
459473
*/
460474
IPAddress WiFiSTAClass::dnsIP(uint8_t dns_no)
461475
{
476+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
477+
return IPAddress();
478+
}
462479
ip_addr_t dns_ip = dns_getserver(dns_no);
463480
return IPAddress(dns_ip.u_addr.ip4.addr);
464481
}
@@ -469,6 +486,9 @@ IPAddress WiFiSTAClass::dnsIP(uint8_t dns_no)
469486
*/
470487
String WiFiSTAClass::SSID() const
471488
{
489+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
490+
return String();
491+
}
472492
wifi_ap_record_t info;
473493
if(!esp_wifi_sta_get_ap_info(&info)) {
474494
return String(reinterpret_cast<char*>(info.ssid));
@@ -482,6 +502,9 @@ String WiFiSTAClass::SSID() const
482502
*/
483503
String WiFiSTAClass::psk() const
484504
{
505+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
506+
return String();
507+
}
485508
wifi_config_t conf;
486509
esp_wifi_get_config(WIFI_IF_STA, &conf);
487510
return String(reinterpret_cast<char*>(conf.sta.password));
@@ -495,6 +518,9 @@ uint8_t* WiFiSTAClass::BSSID(void)
495518
{
496519
static uint8_t bssid[6];
497520
wifi_ap_record_t info;
521+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
522+
return NULL;
523+
}
498524
if(!esp_wifi_sta_get_ap_info(&info)) {
499525
memcpy(bssid, info.bssid, 6);
500526
return reinterpret_cast<uint8_t*>(bssid);
@@ -523,6 +549,9 @@ String WiFiSTAClass::BSSIDstr(void)
523549
*/
524550
int8_t WiFiSTAClass::RSSI(void)
525551
{
552+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
553+
return 0;
554+
}
526555
wifi_ap_record_t info;
527556
if(!esp_wifi_sta_get_ap_info(&info)) {
528557
return info.rssi;
@@ -536,7 +565,10 @@ int8_t WiFiSTAClass::RSSI(void)
536565
*/
537566
const char * WiFiSTAClass::getHostname()
538567
{
539-
const char * hostname;
568+
const char * hostname = NULL;
569+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
570+
return hostname;
571+
}
540572
if(tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &hostname)){
541573
return NULL;
542574
}
@@ -550,6 +582,9 @@ const char * WiFiSTAClass::getHostname()
550582
*/
551583
bool WiFiSTAClass::setHostname(const char * hostname)
552584
{
585+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
586+
return false;
587+
}
553588
return tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, hostname) == 0;
554589
}
555590

@@ -559,6 +594,9 @@ bool WiFiSTAClass::setHostname(const char * hostname)
559594
*/
560595
bool WiFiSTAClass::enableIpV6()
561596
{
597+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
598+
return false;
599+
}
562600
return tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA) == 0;
563601
}
564602

@@ -569,6 +607,9 @@ bool WiFiSTAClass::enableIpV6()
569607
IPv6Address WiFiSTAClass::localIPv6()
570608
{
571609
static ip6_addr_t addr;
610+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
611+
return IPv6Address();
612+
}
572613
if(tcpip_adapter_get_ip6_linklocal(TCPIP_ADAPTER_IF_STA, &addr)){
573614
return IPv6Address();
574615
}

0 commit comments

Comments
 (0)