@@ -92,6 +92,7 @@ const uint32_t WiFiFastResponseTimeoutMillis = 20; // SPI timeout when when the
92
92
const uint32_t WiFiWaitReadyMillis = 100 ;
93
93
const uint32_t WiFiStartupMillis = 15000 ; // Formatting the SPIFFS partition can take up to 10s.
94
94
const uint32_t WiFiStableMillis = 100 ;
95
+ const uint32_t WiFiStatusPollMillis = 500 ; // Poll interval for status details of the WiFi module
95
96
96
97
const unsigned int MaxHttpConnections = 4 ;
97
98
@@ -287,7 +288,7 @@ WiFiInterface::WiFiInterface(Platform& p) noexcept
287
288
: platform(p), bufferOut(nullptr ), bufferIn(nullptr ), uploader(nullptr ), espWaitingTask(nullptr ),
288
289
ftpDataPort(0 ), closeDataPort(false ),
289
290
requestedMode(WiFiState::disabled), currentMode(WiFiState::disabled), activated(false ),
290
- espStatusChanged(false ), spiTxUnderruns(0 ), spiRxOverruns(0 ), serialRunning(false ), debugMessageChars(0 )
291
+ espStatusChanged(false ), rssi(INT8_MIN), spiTxUnderruns(0 ), spiRxOverruns(0 ), serialRunning(false ), debugMessageChars(0 )
291
292
{
292
293
wifiInterface = this ;
293
294
@@ -326,16 +327,18 @@ WiFiInterface::WiFiInterface(Platform& p) noexcept
326
327
constexpr ObjectModelTableEntry WiFiInterface::objectModelTable[] =
327
328
{
328
329
// These entries must be in alphabetical order
329
- { " actualIP" , OBJECT_MODEL_FUNC (self->ipAddress ), ObjectModelEntryFlags::none },
330
- { " firmwareVersion" , OBJECT_MODEL_FUNC (self->wiFiServerVersion ), ObjectModelEntryFlags::none },
331
- { " gateway" , OBJECT_MODEL_FUNC (self->gateway ), ObjectModelEntryFlags::none },
332
- { " mac" , OBJECT_MODEL_FUNC (self->macAddress ), ObjectModelEntryFlags::none },
333
- { " state" , OBJECT_MODEL_FUNC (self->GetStateName ()), ObjectModelEntryFlags::none },
334
- { " subnet" , OBJECT_MODEL_FUNC (self->netmask ), ObjectModelEntryFlags::none },
335
- { " type" , OBJECT_MODEL_FUNC_NOSELF (" wifi" ), ObjectModelEntryFlags::none },
330
+ { " actualIP" , OBJECT_MODEL_FUNC (self->ipAddress ), ObjectModelEntryFlags::none },
331
+ { " firmwareVersion" , OBJECT_MODEL_FUNC (self->wiFiServerVersion ), ObjectModelEntryFlags::none },
332
+ { " gateway" , OBJECT_MODEL_FUNC (self->gateway ), ObjectModelEntryFlags::none },
333
+ { " mac" , OBJECT_MODEL_FUNC (self->macAddress ), ObjectModelEntryFlags::none },
334
+ { " numReconnects" , OBJECT_MODEL_FUNC ((uint32_t )self->reconnectCount ), ObjectModelEntryFlags::none },
335
+ { " signal" , OBJECT_MODEL_FUNC ((int32_t )self->rssi ), ObjectModelEntryFlags::none },
336
+ { " state" , OBJECT_MODEL_FUNC (self->GetStateName ()), ObjectModelEntryFlags::none },
337
+ { " subnet" , OBJECT_MODEL_FUNC (self->netmask ), ObjectModelEntryFlags::none },
338
+ { " type" , OBJECT_MODEL_FUNC_NOSELF (" wifi" ), ObjectModelEntryFlags::none },
336
339
};
337
340
338
- constexpr uint8_t WiFiInterface::objectModelTableDescriptor[] = { 1 , 7 };
341
+ constexpr uint8_t WiFiInterface::objectModelTableDescriptor[] = { 1 , 9 };
339
342
340
343
DEFINE_GET_OBJECT_MODEL_TABLE (WiFiInterface)
341
344
@@ -717,7 +720,6 @@ void WiFiInterface::Spin() noexcept
717
720
if (rc > 0 )
718
721
{
719
722
SafeStrncpy (wiFiServerVersion, status.Value ().versionText , ARRAY_SIZE (wiFiServerVersion));
720
- macAddress.SetFromBytes (status.Value ().macAddress );
721
723
722
724
// Set the hostname before anything else is done
723
725
rc = SendCommand (NetworkCommand::networkSetHostName, 0 , 0 , 0 , reprap.GetNetwork ().GetHostname (), HostNameLength, nullptr , 0 );
@@ -846,6 +848,19 @@ void WiFiInterface::Spin() noexcept
846
848
}
847
849
}
848
850
}
851
+
852
+ // Update details that change constantly about the WiFi module
853
+ if (millis () - lastStatusPoll >= WiFiStatusPollMillis)
854
+ {
855
+ Receiver<NetworkStatusResponse> status;
856
+ const uint32_t rc = SendCommand (NetworkCommand::networkGetStatus, 0 , 0 , nullptr , 0 , status);
857
+ rssi = status.Value ().rssi ;
858
+ if (rc > offsetof (NetworkStatusResponse, netmask))
859
+ {
860
+ reconnectCount = status.Value ().numReconnects ;
861
+ }
862
+ lastStatusPoll = millis ();
863
+ }
849
864
}
850
865
break ;
851
866
@@ -867,19 +882,41 @@ void WiFiInterface::Spin() noexcept
867
882
{
868
883
// Get our IP address, this needs to be correct for FTP to work
869
884
Receiver<NetworkStatusResponse> status;
870
- if (SendCommand (NetworkCommand::networkGetStatus, 0 , 0 , nullptr , 0 , status) > 0 )
885
+ const uint32_t rc = SendCommand (NetworkCommand::networkGetStatus, 0 , 0 , nullptr , 0 , status);
886
+ if (rc > 0 )
871
887
{
872
888
ipAddress.SetV4LittleEndian (status.Value ().ipAddress );
889
+ macAddress.SetFromBytes (status.Value ().macAddress ); // MAC address for AP and STA are separate and different
873
890
SafeStrncpy (actualSsid, status.Value ().ssid , SsidLength);
891
+
892
+ if (rc > offsetof (NetworkStatusResponse, netmask))
893
+ {
894
+ netmask.SetV4LittleEndian (status.Value ().netmask );
895
+ gateway.SetV4LittleEndian (status.Value ().gateway );
896
+ usingDhcp = status.Value ().usingDhcpc ;
897
+ }
874
898
}
875
899
InitSockets ();
876
900
reconnectCount = 0 ;
877
901
platform.MessageF (NetworkInfoMessage, " WiFi module is %s%s, IP address %s\n " ,
878
902
TranslateWiFiState (currentMode),
879
903
actualSsid,
880
904
IP4String (ipAddress).c_str ());
905
+
906
+ lastStatusPoll = 0 ;
881
907
}
882
908
break ;
909
+ case WiFiState::idle:
910
+ {
911
+ uint8_t zero[6 ];
912
+ memset (zero, 0 , sizeof (zero));
913
+ macAddress.SetFromBytes (zero);
914
+ SetIPAddress (DefaultIpAddress, DefaultNetMask, DefaultGateway);
915
+ strcpy (actualSsid, " (unknown)" );
916
+ reconnectCount = 0 ;
917
+ rssi = INT8_MIN;
918
+ usingDhcp = false ;
919
+ }
883
920
884
921
default :
885
922
if (requestedMode != WiFiState::connected)
@@ -969,7 +1006,8 @@ void WiFiInterface::Diagnostics(MessageType mtype) noexcept
969
1006
{
970
1007
Receiver<NetworkStatusResponse> status;
971
1008
status.Value ().clockReg = 0xFFFFFFFF ; // older WiFi firmware doesn't return this value, so preset it
972
- if (SendCommand (NetworkCommand::networkGetStatus, 0 , 0 , nullptr , 0 , status) > 0 )
1009
+ const uint32_t rc = SendCommand (NetworkCommand::networkGetStatus, 0 , 0 , nullptr , 0 , status);
1010
+ if (rc > 0 )
973
1011
{
974
1012
NetworkStatusResponse& r = status.Value ();
975
1013
r.versionText [ARRAY_UPB (r.versionText )] = 0 ;
@@ -981,17 +1019,47 @@ void WiFiInterface::Diagnostics(MessageType mtype) noexcept
981
1019
982
1020
if (currentMode == WiFiState::connected || currentMode == WiFiState::runningAsAccessPoint)
983
1021
{
984
- platform.MessageF (mtype, " WiFi IP address %s\n " , IP4String (r.ipAddress ).c_str ());
1022
+ if (rc > offsetof (NetworkStatusResponse, netmask))
1023
+ {
1024
+ platform.MessageF (mtype, " WiFi IP address %s, netmask %s, gateway %s%s\n " ,
1025
+ IP4String (r.ipAddress ).c_str (), IP4String (r.netmask ).c_str (), IP4String (r.gateway ).c_str (),
1026
+ currentMode == WiFiState::connected ? (r.usingDhcpc ? " (via DHCP)" : " (set manually)" ) : " " );
1027
+ }
1028
+ else
1029
+ {
1030
+ platform.MessageF (mtype, " WiFi IP address %s\n " , IP4String (r.ipAddress ).c_str ());
1031
+ }
985
1032
}
986
1033
1034
+ constexpr const char * SleepModes[4 ] = { " unknown" , " none" , " light" , " modem" };
1035
+ constexpr const char * ConnectionModes[4 ] = { " none" , " 802.11b" , " 802.11g" , " 802.11n" };
1036
+
987
1037
if (currentMode == WiFiState::connected)
988
1038
{
989
- constexpr const char * SleepModes[4 ] = { " unknown" , " none" , " light" , " modem" };
990
- constexpr const char * ConnectionModes[4 ] = { " none" , " 802.11b" , " 802.11g" , " 802.11n" };
991
- platform.MessageF (mtype, " WiFi signal strength %ddBm, mode %s, reconnections %u, sleep mode %s\n " , (int )r.rssi , ConnectionModes[r.phyMode ], reconnectCount, SleepModes[r.sleepMode ]);
1039
+ if (rc > offsetof (NetworkStatusResponse, netmask))
1040
+ {
1041
+ platform.MessageF (mtype, " WiFi signal strength %ddBm, mode %s, reconnections %u, sleep mode %s, channel %u (%s), auth %s\n " ,
1042
+ (int )r.rssi , ConnectionModes[r.phyMode ], reconnectCount, SleepModes[r.sleepMode ],
1043
+ r.channel , static_cast <HTMode>(r.ht ) == HTMode::HT20 ? " 20 MHz" : " 40 MHz" , GetWiFiAuthFriendlyStr (r.auth ));
1044
+ }
1045
+ else
1046
+ {
1047
+ platform.MessageF (mtype, " WiFi signal strength %ddBm, mode %s, reconnections %u, sleep mode %s\n " , (int )r.rssi , ConnectionModes[r.phyMode ], reconnectCount, SleepModes[r.sleepMode ]);
1048
+ }
992
1049
}
993
1050
else if (currentMode == WiFiState::runningAsAccessPoint)
994
1051
{
1052
+ if (rc > offsetof (NetworkStatusResponse, netmask))
1053
+ {
1054
+ platform.MessageF (mtype, " WiFi mode %s, sleep mode %s, channel %u (%s), auth %s\n " ,
1055
+ ConnectionModes[r.phyMode ], SleepModes[r.sleepMode ], r.channel ,
1056
+ static_cast <HTMode>(r.ht ) == HTMode::HT20 ? " 20 MHz" : " 40 MHz" , GetWiFiAuthFriendlyStr (r.auth ));
1057
+ }
1058
+ else
1059
+ {
1060
+ platform.MessageF (mtype, " WiFi mode %s, sleep mode %s\n " , ConnectionModes[r.phyMode ], SleepModes[r.sleepMode ]);
1061
+ }
1062
+
995
1063
platform.MessageF (mtype, " Connected clients %u\n " , (unsigned int )r.numClients );
996
1064
}
997
1065
// status, ssid and hostName not displayed
@@ -1105,7 +1173,7 @@ void WiFiInterface::EspRequestsTransfer() noexcept
1105
1173
void WiFiInterface::SetIPAddress (IPAddress p_ip, IPAddress p_netmask, IPAddress p_gateway) noexcept
1106
1174
{
1107
1175
ipAddress = p_ip;
1108
- usingDhcp = ipAddress. IsNull ();
1176
+ usingDhcp = false ; // Mirror the value returned by the ESP module
1109
1177
netmask = p_netmask;
1110
1178
gateway = p_gateway;
1111
1179
}
0 commit comments