Skip to content

Commit 643cef0

Browse files
committed
Add wifiApIsRunning
1 parent 0909cb7 commit 643cef0

File tree

2 files changed

+77
-53
lines changed

2 files changed

+77
-53
lines changed

Firmware/RTK_Everywhere/Developer.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ void sendStringToWebsocket(const char* stringToSend) {}
170170
void menuWiFi() {systemPrintln("**WiFi not compiled**");}
171171
bool wifiConnect(bool startWiFiStation, bool startWiFiAP, unsigned long timeout) {return false;}
172172
int wifiNetworkCount() {return 0;}
173-
bool wifiIsRunning() {return false;}
173+
bool wifiStationIsRunning() {return false;}
174+
bool wifiApIsRunning() {return false;}
174175
uint32_t wifiGetStartTimeout() {return 0;}
175176
#define WIFI_STOP() {}
176177

Firmware/RTK_Everywhere/WiFi.ino

Lines changed: 75 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ static unsigned long wifiDisplayTimer;
5555
// DNS server for Captive Portal
5656
static DNSServer dnsServer;
5757

58-
static bool wifiRunning;
58+
static bool wifiStationRunning;
59+
static bool wifiApRunning;
5960

6061
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6162
// WiFi Routines
@@ -154,7 +155,8 @@ bool wifiConnect(bool startWiFiStation, bool startWiFiAP, unsigned long timeout)
154155
if (startWiFiAP && WiFi.getMode() == WIFI_AP)
155156
return (true); // There is nothing needing to be changed
156157

157-
wifiRunning = false; // Mark it as offline while we mess about
158+
wifiStationRunning = false; // Mark it as offline while we mess about
159+
wifiApRunning = false; // Mark it as offline while we mess about
158160

159161
wifi_mode_t wifiMode = WIFI_OFF;
160162
wifi_interface_t wifiInterface = WIFI_IF_STA;
@@ -248,8 +250,6 @@ bool wifiConnect(bool startWiFiStation, bool startWiFiAP, unsigned long timeout)
248250
systemPrintf("WiFi AP '%s' started with IP: ", softApSsid);
249251
systemPrintln(WiFi.softAPIP());
250252

251-
networkMarkOnline(NETWORK_WIFI); // Regardless of WiFi STA success of failure, we have some network access
252-
253253
// Start DNS Server
254254
if (dnsServer.start(53, "*", WiFi.softAPIP()) == false)
255255
{
@@ -262,12 +262,11 @@ bool wifiConnect(bool startWiFiStation, bool startWiFiAP, unsigned long timeout)
262262
systemPrintln("DNS Server started");
263263
}
264264

265+
wifiApRunning = true;
266+
265267
// If we're only here to start the AP, then we're done
266268
if (wifiMode == WIFI_AP)
267-
{
268-
wifiRunning = true;
269269
return true;
270-
}
271270
}
272271

273272
systemPrintln("Connecting to WiFi... ");
@@ -288,7 +287,7 @@ bool wifiConnect(bool startWiFiStation, bool startWiFiAP, unsigned long timeout)
288287
if (wifiStatus == WL_CONNECTED)
289288
{
290289
wifiResetTimeout(); // If we successfully connected then reset the throttling timeout
291-
wifiRunning = true;
290+
wifiStationRunning = true;
292291
return true;
293292
}
294293
if (wifiStatus == WL_DISCONNECTED)
@@ -297,7 +296,7 @@ bool wifiConnect(bool startWiFiStation, bool startWiFiAP, unsigned long timeout)
297296
{
298297
systemPrintf("WiFi failed to connect: error #%d - %s\r\n", wifiStatus, wifiPrintState((wl_status_t)wifiStatus));
299298
}
300-
wifiRunning = false;
299+
wifiStationRunning = false;
301300
return false;
302301
}
303302

@@ -306,10 +305,10 @@ bool wifiConnect(bool startWiFiStation, bool startWiFiAP, unsigned long timeout)
306305
//----------------------------------------
307306
void wifiDisplayState()
308307
{
309-
systemPrintf("WiFi: %s\r\n", networkIsInterfaceOnline(NETWORK_WIFI) ? "Online" : "Offline");
308+
systemPrintf("WiFi: %s\r\n", networkInterfaceHasInternet(NETWORK_WIFI) ? "Online" : "Offline");
310309
systemPrintf(" MAC Address: %02X:%02X:%02X:%02X:%02X:%02X\r\n", wifiMACAddress[0], wifiMACAddress[1],
311310
wifiMACAddress[2], wifiMACAddress[3], wifiMACAddress[4], wifiMACAddress[5]);
312-
if (networkIsInterfaceOnline(NETWORK_WIFI))
311+
if (networkInterfaceHasInternet(NETWORK_WIFI))
313312
{
314313
// Get the DNS addresses
315314
IPAddress dns1 = WiFi.STA.dnsIP(0);
@@ -351,7 +350,7 @@ void wifiEvent(arduino_event_id_t event, arduino_event_info_t info)
351350
if (WiFi.getMode() == WIFI_MODE_STA)
352351
{
353352
// Take the network offline if necessary
354-
if (networkIsInterfaceOnline(NETWORK_WIFI) && (event != ARDUINO_EVENT_WIFI_STA_GOT_IP) &&
353+
if (networkInterfaceHasInternet(NETWORK_WIFI) && (event != ARDUINO_EVENT_WIFI_STA_GOT_IP) &&
355354
(event != ARDUINO_EVENT_WIFI_STA_GOT_IP6))
356355
{
357356
if (settings.debugWifiState)
@@ -422,13 +421,13 @@ void wifiEvent(arduino_event_id_t event, arduino_event_info_t info)
422421
ipAddress = WiFi.localIP();
423422
systemPrint("WiFi STA Got IPv4: ");
424423
systemPrintln(ipAddress);
425-
networkMarkOnline(NETWORK_WIFI);
424+
networkMarkHasInternet(NETWORK_WIFI);
426425
break;
427426

428427
case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
429428
systemPrint("WiFi STA Got IPv6: ");
430429
systemPrintln(ipAddress);
431-
networkMarkOnline(NETWORK_WIFI);
430+
networkMarkHasInternet(NETWORK_WIFI);
432431
break;
433432

434433
case ARDUINO_EVENT_WIFI_STA_LOST_IP:
@@ -449,11 +448,29 @@ bool wifiIsConnected()
449448
}
450449

451450
//----------------------------------------
452-
// Determine if WIFI is running
451+
// Determine if WiFi Station is running
452+
//----------------------------------------
453+
bool wifiStationIsRunning()
454+
{
455+
return wifiStationRunning;
456+
}
457+
458+
//----------------------------------------
459+
// Determine if WiFi AP is running
460+
//----------------------------------------
461+
bool wifiApIsRunning()
462+
{
463+
return wifiApRunning;
464+
}
465+
466+
//----------------------------------------
467+
// Determine if either Station or AP is running
453468
//----------------------------------------
454469
bool wifiIsRunning()
455470
{
456-
return wifiRunning;
471+
if (wifiStationRunning || wifiApRunning)
472+
return true;
473+
return false;
457474
}
458475

459476
//----------------------------------------
@@ -511,54 +528,59 @@ bool wifiStart()
511528
{
512529
int wifiStatus;
513530

514-
// Determine if WiFi is already running
515-
if (!wifiRunning)
516-
{
517-
// Determine which parts of WiFi need to be started
518-
bool startWiFiStation = false;
519-
bool startWiFiAP = false;
531+
// Determine which parts of WiFi need to be started
532+
bool startWiFiStation = false;
533+
bool startWiFiAp = false;
520534

521-
uint16_t consumerTypes = networkGetConsumerTypes();
535+
uint16_t consumerTypes = networkGetConsumerTypes();
522536

523-
// The consumers need station
524-
if (consumerTypes & (1 << NETCONSUMER_WIFI_STA))
525-
startWiFiStation = true;
537+
// The consumers need station
538+
if (consumerTypes & (1 << NETCONSUMER_WIFI_STA))
539+
startWiFiStation = true;
526540

527-
// The consumers need AP
528-
if (consumerTypes & (1 << NETCONSUMER_WIFI_AP))
529-
startWiFiAP = true;
541+
// The consumers need AP
542+
if (consumerTypes & (1 << NETCONSUMER_WIFI_AP))
543+
startWiFiAp = true;
530544

531-
if (startWiFiStation == false && startWiFiAP == false)
545+
if (startWiFiStation == false && startWiFiAp == false)
546+
{
547+
systemPrintln("wifiStart() requested without any NETCONSUMER combination");
548+
WIFI_STOP();
549+
return (false);
550+
}
551+
552+
// Determine if WiFi is already running
553+
if (startWiFiStation == wifiStationRunning && startWiFiAp == wifiApRunning)
554+
{
555+
if (settings.debugWifiState == true)
556+
systemPrintln("WiFi is already running with requested setup");
557+
return (true);
558+
}
559+
560+
// Handle special cases if no networks have been entered
561+
if (wifiNetworkCount() == 0)
562+
{
563+
if (startWiFiStation == true && startWiFiAp == false)
532564
{
533-
systemPrintln("wifiStart() requested without any NETCONSUMER combination");
565+
systemPrintln("Error: Please enter at least one SSID before using WiFi");
566+
displayNoSSIDs(2000);
534567
WIFI_STOP();
535-
return (false);
568+
return false;
536569
}
537-
538-
if (wifiNetworkCount() == 0)
570+
else if (startWiFiStation == true && startWiFiAp == true)
539571
{
540-
if (startWiFiStation == true && startWiFiAP == false)
541-
{
542-
systemPrintln("Error: Please enter at least one SSID before using WiFi");
543-
displayNoSSIDs(2000);
544-
WIFI_STOP();
545-
return false;
546-
}
547-
else if (startWiFiStation == true && startWiFiAP == true)
548-
{
549-
systemPrintln("Error: No SSID available to start WiFi Station during AP");
550-
// Allow the system to continue in AP only mode
551-
startWiFiStation = false;
552-
}
572+
systemPrintln("Error: No SSID available to start WiFi Station during AP");
573+
// Allow the system to continue in AP only mode
574+
startWiFiStation = false;
553575
}
554-
555-
// Start WiFi
556-
wifiConnect(startWiFiStation, startWiFiAP, settings.wifiConnectTimeoutMs);
557576
}
558577

578+
// Start WiFi
579+
wifiConnect(startWiFiStation, startWiFiAp, settings.wifiConnectTimeoutMs);
580+
559581
// If we are in AP only mode, as long as the AP is started, return true
560582
if (WiFi.getMode() == WIFI_MODE_AP)
561-
return (true);
583+
return (wifiApIsRunning);
562584

563585
// If we are in STA or AP+STA mode, return if the station connected successfully
564586
wifiStatus = WiFi.status();
@@ -690,7 +712,8 @@ void wifiStop()
690712

691713
// Display the heap state
692714
reportHeapNow(settings.debugWifiState);
693-
wifiRunning = false;
715+
wifiStationRunning = false;
716+
wifiApRunning = false;
694717
}
695718

696719
// Needed for wifiStopSequence

0 commit comments

Comments
 (0)