Skip to content

Commit cabc8d6

Browse files
committed
WiFi: Update stopStart
Changes: * Define allOnline constant * Add more debug output * Determine which components are being started * Clear the saved soft AP SSID when the soft AP is stopped * Append the last 4 digits of the MAC address to the soft AP name * Set the soft AP host name * Handle nullptr for _apSsid, don't crash during print * Set the WiFi station host name * Determine if a WiFi reconnection should occur * Set the online flags for (ESP-NOW, soft AP and WiFi station) * Return status indicating if all the requested WiFi modes were started
1 parent aa5989d commit cabc8d6

File tree

1 file changed

+59
-17
lines changed

1 file changed

+59
-17
lines changed

Firmware/RTK_Everywhere/WiFi.ino

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,29 +2213,33 @@ const char * RTK_WIFI::stationSsid()
22132213
// Returns true if the modes were successfully configured
22142214
bool RTK_WIFI::stopStart(WIFI_ACTION_t stopping, WIFI_ACTION_t starting)
22152215
{
2216+
const WIFI_ACTION_t allOnline = WIFI_AP_ONLINE | WIFI_EN_ESP_NOW_ONLINE | WIFI_STA_ONLINE;
22162217
int authIndex;
22172218
WIFI_CHANNEL_t channel;
22182219
bool defaultChannel;
22192220
WIFI_ACTION_t delta;
2220-
bool enabled;
2221+
WIFI_ACTION_t expected;
22212222
WIFI_ACTION_t mask;
22222223
WIFI_ACTION_t notStarted;
22232224
uint8_t primaryChannel;
22242225
WIFI_ACTION_t restarting;
2226+
bool restartWiFiStation;
22252227
wifi_second_chan_t secondaryChannel;
22262228
WIFI_ACTION_t startingNow;
22272229
esp_err_t status;
22282230
WIFI_ACTION_t stillRunning;
22292231

22302232
// Determine the next actions
22312233
notStarted = 0;
2232-
enabled = true;
2234+
restartWiFiStation = false;
22332235

22342236
// Display the parameters
22352237
if (settings.debugWifiState && _verbose)
22362238
{
2239+
systemPrintf("WiFi: RTK_WIFI::stopStart called\r\n");
22372240
systemPrintf("stopping: 0x%08x\r\n", stopping);
22382241
systemPrintf("starting: 0x%08x\r\n", starting);
2242+
reportHeapNow(true);
22392243
}
22402244

22412245
//****************************************
@@ -2253,8 +2257,7 @@ bool RTK_WIFI::stopStart(WIFI_ACTION_t stopping, WIFI_ACTION_t starting)
22532257
// Determine if there is an active channel
22542258
defaultChannel = _usingDefaultChannel;
22552259
_usingDefaultChannel = false;
2256-
if (((_started & ~stopping) & (WIFI_AP_ONLINE | WIFI_EN_ESP_NOW_ONLINE | WIFI_STA_ONLINE))
2257-
&& wifiChannel && !defaultChannel)
2260+
if ((allOnline & _started & ~stopping) && wifiChannel && !defaultChannel)
22582261
{
22592262
// Continue to use the active channel
22602263
channel = wifiChannel;
@@ -2329,14 +2332,18 @@ bool RTK_WIFI::stopStart(WIFI_ACTION_t stopping, WIFI_ACTION_t starting)
23292332
// Only stop the started components
23302333
stopping &= _started;
23312334

2335+
// Determine the components that are being started
2336+
expected = starting & allOnline;
2337+
23322338
// Determine which components are being restarted
23332339
restarting = _started & stopping & starting;
23342340
if (settings.debugWifiState && _verbose)
23352341
{
23362342
systemPrintf("0x%08x: _started\r\n", _started);
23372343
systemPrintf("0x%08x: stopping\r\n", stopping);
23382344
systemPrintf("0x%08x: starting\r\n", starting);
2339-
systemPrintf("0x%08x: restarting\r\n", starting);
2345+
systemPrintf("0x%08x: restarting\r\n", restarting);
2346+
systemPrintf("0x%08x: expected\r\n", expected);
23402347
}
23412348

23422349
// Don't start components that are already running and are not being
@@ -2564,7 +2571,11 @@ bool RTK_WIFI::stopStart(WIFI_ACTION_t stopping, WIFI_ACTION_t starting)
25642571

25652572
// Stop use of SSID and password
25662573
if (stopping & WIFI_AP_SET_SSID_PASSWORD)
2574+
{
25672575
_started = _started & ~WIFI_AP_SET_SSID_PASSWORD;
2576+
if (_apSsid)
2577+
_apSsid[0] = 0;
2578+
}
25682579

25692580
stillRunning = _started;
25702581

@@ -2639,6 +2650,8 @@ bool RTK_WIFI::stopStart(WIFI_ACTION_t stopping, WIFI_ACTION_t starting)
26392650
systemPrintf("channel: %d\r\n", channel);
26402651
_started = _started | WIFI_STA_START_SCAN;
26412652

2653+
displayWiFiConnect();
2654+
26422655
// Determine if WiFi scan failed, stop WiFi station startup
26432656
if (wifi.stationScanForAPs(channel) < 0)
26442657
{
@@ -2655,9 +2668,11 @@ bool RTK_WIFI::stopStart(WIFI_ACTION_t stopping, WIFI_ACTION_t starting)
26552668
if (channel == 0)
26562669
{
26572670
if (wifiChannel)
2658-
systemPrintf("WiFi STA: No compatible remote AP found on channel %d!\r\n", wifiChannel);
2671+
systemPrintf("WiFi STA: No matching remote AP found on channel %d!\r\n", wifiChannel);
26592672
else
2660-
systemPrintf("WiFi STA: No compatible remote AP found!\r\n");
2673+
systemPrintf("WiFi STA: No matching remote AP found!\r\n");
2674+
2675+
displayNoWiFi(2000);
26612676

26622677
// Stop bringing up WiFi station
26632678
starting &= ~WIFI_STA_NO_REMOTE_AP;
@@ -2693,7 +2708,15 @@ bool RTK_WIFI::stopStart(WIFI_ACTION_t stopping, WIFI_ACTION_t starting)
26932708
// Set the soft AP SSID and password
26942709
if (starting & WIFI_AP_SET_SSID_PASSWORD)
26952710
{
2696-
if (!softApSetSsidPassword(wifiSoftApSsid, wifiSoftApPassword))
2711+
// Append the last four digits of the MAC address
2712+
if (strlen(_apSsid) == 0)
2713+
{
2714+
snprintf(_apSsid, SSID_LENGTH, "%s %02X%02X", wifiSoftApSsid, btMACAddress[4], btMACAddress[5]);
2715+
_apSsid[SSID_LENGTH - 1] = 0;
2716+
}
2717+
2718+
// Set the soft AP SSID and password
2719+
if (!softApSetSsidPassword(_apSsid, wifiSoftApPassword))
26972720
break;
26982721
_started = _started | WIFI_AP_SET_SSID_PASSWORD;
26992722
}
@@ -2718,12 +2741,14 @@ bool RTK_WIFI::stopStart(WIFI_ACTION_t stopping, WIFI_ACTION_t starting)
27182741
// Set the soft AP host name
27192742
if (starting & WIFI_AP_SET_HOST_NAME)
27202743
{
2744+
const char * hostName = &settings.mdnsHostName[0];
2745+
27212746
// Display the host name
27222747
if (settings.debugWifiState && _verbose)
2723-
systemPrintf("Host name: %s\r\n", &settings.mdnsHostName[0]);
2748+
systemPrintf("Host name: %s\r\n", hostName);
27242749

27252750
// Set the host name
2726-
if (!softApSetHostName(&settings.mdnsHostName[0]))
2751+
if (!softApSetHostName(hostName))
27272752
break;
27282753
_started = _started | WIFI_AP_SET_HOST_NAME;
27292754
}
@@ -2750,7 +2775,7 @@ bool RTK_WIFI::stopStart(WIFI_ACTION_t stopping, WIFI_ACTION_t starting)
27502775

27512776
// Display the soft AP status
27522777
systemPrintf("WiFi: Soft AP online, SSID: %s (%s)%s%s\r\n",
2753-
wifiSoftApSsid,
2778+
_apSsid ? _apSsid : "",
27542779
_apIpAddress.toString().c_str(),
27552780
wifiSoftApPassword ? ", Password: " : "",
27562781
wifiSoftApPassword ? wifiSoftApPassword : "");
@@ -2760,15 +2785,19 @@ bool RTK_WIFI::stopStart(WIFI_ACTION_t stopping, WIFI_ACTION_t starting)
27602785
// Start the WiFi station components
27612786
//****************************************
27622787

2788+
restartWiFiStation = true;
2789+
27632790
// Set the host name
27642791
if (starting & WIFI_STA_SET_HOST_NAME)
27652792
{
2793+
const char * hostName = &settings.mdnsHostName[0];
2794+
27662795
// Display the host name
27672796
if (settings.debugWifiState && _verbose)
2768-
systemPrintf("Host name: %s\r\n", &settings.mdnsHostName[0]);
2797+
systemPrintf("Host name: %s\r\n", hostName);
27692798

27702799
// Set the host name
2771-
if (!stationHostName(&settings.mdnsHostName[0]))
2800+
if (!stationHostName(hostName))
27722801
break;
27732802
_started = _started | WIFI_STA_SET_HOST_NAME;
27742803
}
@@ -2824,6 +2853,7 @@ bool RTK_WIFI::stopStart(WIFI_ACTION_t stopping, WIFI_ACTION_t starting)
28242853
// Mark the station online
28252854
if (starting & WIFI_STA_ONLINE)
28262855
{
2856+
restartWiFiStation = false;
28272857
_started = _started | WIFI_STA_ONLINE;
28282858
systemPrintf("WiFi: Station online (%s: %s)\r\n",
28292859
_staRemoteApSsid, _staIpAddress.toString().c_str());
@@ -2931,9 +2961,6 @@ bool RTK_WIFI::stopStart(WIFI_ACTION_t stopping, WIFI_ACTION_t starting)
29312961
_staMacAddress[3], _staMacAddress[4], _staMacAddress[5],
29322962
wifiChannel);
29332963
}
2934-
2935-
// All components started successfully
2936-
enabled = true;
29372964
} while (0);
29382965

29392966
//****************************************
@@ -2979,9 +3006,24 @@ bool RTK_WIFI::stopStart(WIFI_ACTION_t stopping, WIFI_ACTION_t starting)
29793006
if (settings.debugWifiState && _verbose && _started)
29803007
displayComponents("Started items", _started);
29813008

3009+
// Restart WiFi if necessary
3010+
if (restartWiFiStation)
3011+
wifiReconnectRequest = true;
3012+
3013+
// Set the online flags
3014+
wifiEspNowOnline = espNowOnline();
3015+
wifiSoftApOnline = softApOnline();
3016+
wifiStationOnline = stationOnline();
3017+
29823018
// Return the enable status
3019+
bool enabled = ((_started & allOnline) == expected);
29833020
if (!enabled)
2984-
systemPrintf("ERROR: RTK_WIFI::enable failed!\r\n");
3021+
systemPrintf("ERROR: RTK_WIFI::stopStart failed!\r\n");
3022+
if (settings.debugWifiState && _verbose)
3023+
{
3024+
reportHeapNow(true);
3025+
systemPrintf("WiFi: RTK_WIFI::stopStart returning; %s\r\n", enabled ? "true" : "false");
3026+
}
29853027
return enabled;
29863028
}
29873029

0 commit comments

Comments
 (0)