Skip to content

Commit 3961f18

Browse files
committed
WiFi: Move wifiStart
1 parent a441447 commit 3961f18

File tree

1 file changed

+69
-70
lines changed

1 file changed

+69
-70
lines changed

Firmware/RTK_Everywhere/WiFi.ino

Lines changed: 69 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,75 @@ void wifiResetTimeout()
622622
systemPrintln("WiFi: Start timeout reset to zero");
623623
}
624624

625+
//*********************************************************************
626+
// Starts the WiFi connection state machine (moves from WIFI_STATE_OFF to WIFI_STATE_CONNECTING)
627+
// Sets the appropriate protocols (WiFi + ESP-Now)
628+
// If radio is off entirely, start WiFi
629+
// If ESP-Now is active, only add the LR protocol
630+
// Returns true if WiFi has connected and false otherwise
631+
bool wifiStart()
632+
{
633+
int wifiStatus;
634+
635+
// Determine which parts of WiFi need to be started
636+
bool startWiFiStation = false;
637+
bool startWiFiAp = false;
638+
639+
uint16_t consumerTypes = networkGetConsumerTypes();
640+
641+
// The consumers need station
642+
if (consumerTypes & (1 << NETCONSUMER_WIFI_STA))
643+
startWiFiStation = true;
644+
645+
// The consumers need AP
646+
if (consumerTypes & (1 << NETCONSUMER_WIFI_AP))
647+
startWiFiAp = true;
648+
649+
if (startWiFiStation == false && startWiFiAp == false)
650+
{
651+
systemPrintln("wifiStart() requested without any NETCONSUMER combination");
652+
WIFI_STOP();
653+
return (false);
654+
}
655+
656+
// Determine if WiFi is already running
657+
if (startWiFiStation == wifiStationRunning && startWiFiAp == wifiApRunning)
658+
{
659+
if (settings.debugWifiState == true)
660+
systemPrintln("WiFi is already running with requested setup");
661+
return (true);
662+
}
663+
664+
// Handle special cases if no networks have been entered
665+
if (wifiNetworkCount() == 0)
666+
{
667+
if (startWiFiStation == true && startWiFiAp == false)
668+
{
669+
systemPrintln("Error: Please enter at least one SSID before using WiFi");
670+
displayNoSSIDs(2000);
671+
WIFI_STOP();
672+
return false;
673+
}
674+
else if (startWiFiStation == true && startWiFiAp == true)
675+
{
676+
systemPrintln("Error: No SSID available to start WiFi Station during AP");
677+
// Allow the system to continue in AP only mode
678+
startWiFiStation = false;
679+
}
680+
}
681+
682+
// Start WiFi
683+
wifiConnect(startWiFiStation, startWiFiAp, settings.wifiConnectTimeoutMs);
684+
685+
// If we are in AP only mode, as long as the AP is started, return true
686+
if (WiFi.getMode() == WIFI_MODE_AP)
687+
return WIFI_SOFT_AP_RUNNING();
688+
689+
// If we are in STA or AP+STA mode, return if the station connected successfully
690+
wifiStatus = WiFi.status();
691+
return (wifiStatus == WL_CONNECTED);
692+
}
693+
625694
//*********************************************************************
626695
// Constructor
627696
// Inputs:
@@ -3058,76 +3127,6 @@ bool wifiIsRunning()
30583127
return false;
30593128
}
30603129

3061-
//----------------------------------------
3062-
// Starts the WiFi connection state machine (moves from WIFI_STATE_OFF to WIFI_STATE_CONNECTING)
3063-
// Sets the appropriate protocols (WiFi + ESP-Now)
3064-
// If radio is off entirely, start WiFi
3065-
// If ESP-Now is active, only add the LR protocol
3066-
// Returns true if WiFi has connected and false otherwise
3067-
//----------------------------------------
3068-
bool wifiStart()
3069-
{
3070-
int wifiStatus;
3071-
3072-
// Determine which parts of WiFi need to be started
3073-
bool startWiFiStation = false;
3074-
bool startWiFiAp = false;
3075-
3076-
uint16_t consumerTypes = networkGetConsumerTypes();
3077-
3078-
// The consumers need station
3079-
if (consumerTypes & (1 << NETCONSUMER_WIFI_STA))
3080-
startWiFiStation = true;
3081-
3082-
// The consumers need AP
3083-
if (consumerTypes & (1 << NETCONSUMER_WIFI_AP))
3084-
startWiFiAp = true;
3085-
3086-
if (startWiFiStation == false && startWiFiAp == false)
3087-
{
3088-
systemPrintln("wifiStart() requested without any NETCONSUMER combination");
3089-
WIFI_STOP();
3090-
return (false);
3091-
}
3092-
3093-
// Determine if WiFi is already running
3094-
if (startWiFiStation == wifiStationRunning && startWiFiAp == wifiApRunning)
3095-
{
3096-
if (settings.debugWifiState == true)
3097-
systemPrintln("WiFi is already running with requested setup");
3098-
return (true);
3099-
}
3100-
3101-
// Handle special cases if no networks have been entered
3102-
if (wifiNetworkCount() == 0)
3103-
{
3104-
if (startWiFiStation == true && startWiFiAp == false)
3105-
{
3106-
systemPrintln("Error: Please enter at least one SSID before using WiFi");
3107-
displayNoSSIDs(2000);
3108-
WIFI_STOP();
3109-
return false;
3110-
}
3111-
else if (startWiFiStation == true && startWiFiAp == true)
3112-
{
3113-
systemPrintln("Error: No SSID available to start WiFi Station during AP");
3114-
// Allow the system to continue in AP only mode
3115-
startWiFiStation = false;
3116-
}
3117-
}
3118-
3119-
// Start WiFi
3120-
wifiConnect(startWiFiStation, startWiFiAp, settings.wifiConnectTimeoutMs);
3121-
3122-
// If we are in AP only mode, as long as the AP is started, return true
3123-
if (WiFi.getMode() == WIFI_MODE_AP)
3124-
return WIFI_SOFT_AP_RUNNING();
3125-
3126-
// If we are in STA or AP+STA mode, return if the station connected successfully
3127-
wifiStatus = WiFi.status();
3128-
return (wifiStatus == WL_CONNECTED);
3129-
}
3130-
31313130
//----------------------------------------
31323131
//----------------------------------------
31333132
uint32_t wifiGetStartTimeout()

0 commit comments

Comments
 (0)