Skip to content

Commit 5af5522

Browse files
committed
Update wifi interface OM fields
1 parent 695f2bf commit 5af5522

File tree

2 files changed

+88
-17
lines changed

2 files changed

+88
-17
lines changed

src/Networking/ESP8266WiFi/WiFiInterface.cpp

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ const uint32_t WiFiFastResponseTimeoutMillis = 20; // SPI timeout when when the
9292
const uint32_t WiFiWaitReadyMillis = 100;
9393
const uint32_t WiFiStartupMillis = 15000; // Formatting the SPIFFS partition can take up to 10s.
9494
const uint32_t WiFiStableMillis = 100;
95+
const uint32_t WiFiStatusPollMillis = 500; // Poll interval for status details of the WiFi module
9596

9697
const unsigned int MaxHttpConnections = 4;
9798

@@ -287,7 +288,7 @@ WiFiInterface::WiFiInterface(Platform& p) noexcept
287288
: platform(p), bufferOut(nullptr), bufferIn(nullptr), uploader(nullptr), espWaitingTask(nullptr),
288289
ftpDataPort(0), closeDataPort(false),
289290
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)
291292
{
292293
wifiInterface = this;
293294

@@ -326,16 +327,18 @@ WiFiInterface::WiFiInterface(Platform& p) noexcept
326327
constexpr ObjectModelTableEntry WiFiInterface::objectModelTable[] =
327328
{
328329
// 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 },
336339
};
337340

338-
constexpr uint8_t WiFiInterface::objectModelTableDescriptor[] = { 1, 7 };
341+
constexpr uint8_t WiFiInterface::objectModelTableDescriptor[] = { 1, 9 };
339342

340343
DEFINE_GET_OBJECT_MODEL_TABLE(WiFiInterface)
341344

@@ -717,7 +720,6 @@ void WiFiInterface::Spin() noexcept
717720
if (rc > 0)
718721
{
719722
SafeStrncpy(wiFiServerVersion, status.Value().versionText, ARRAY_SIZE(wiFiServerVersion));
720-
macAddress.SetFromBytes(status.Value().macAddress);
721723

722724
// Set the hostname before anything else is done
723725
rc = SendCommand(NetworkCommand::networkSetHostName, 0, 0, 0, reprap.GetNetwork().GetHostname(), HostNameLength, nullptr, 0);
@@ -846,6 +848,19 @@ void WiFiInterface::Spin() noexcept
846848
}
847849
}
848850
}
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+
}
849864
}
850865
break;
851866

@@ -867,19 +882,41 @@ void WiFiInterface::Spin() noexcept
867882
{
868883
// Get our IP address, this needs to be correct for FTP to work
869884
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)
871887
{
872888
ipAddress.SetV4LittleEndian(status.Value().ipAddress);
889+
macAddress.SetFromBytes(status.Value().macAddress); // MAC address for AP and STA are separate and different
873890
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+
}
874898
}
875899
InitSockets();
876900
reconnectCount = 0;
877901
platform.MessageF(NetworkInfoMessage, "WiFi module is %s%s, IP address %s\n",
878902
TranslateWiFiState(currentMode),
879903
actualSsid,
880904
IP4String(ipAddress).c_str());
905+
906+
lastStatusPoll = 0;
881907
}
882908
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+
}
883920

884921
default:
885922
if (requestedMode != WiFiState::connected)
@@ -969,7 +1006,8 @@ void WiFiInterface::Diagnostics(MessageType mtype) noexcept
9691006
{
9701007
Receiver<NetworkStatusResponse> status;
9711008
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)
9731011
{
9741012
NetworkStatusResponse& r = status.Value();
9751013
r.versionText[ARRAY_UPB(r.versionText)] = 0;
@@ -981,17 +1019,47 @@ void WiFiInterface::Diagnostics(MessageType mtype) noexcept
9811019

9821020
if (currentMode == WiFiState::connected || currentMode == WiFiState::runningAsAccessPoint)
9831021
{
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+
}
9851032
}
9861033

1034+
constexpr const char* SleepModes[4] = { "unknown", "none", "light", "modem" };
1035+
constexpr const char* ConnectionModes[4] = { "none", "802.11b", "802.11g", "802.11n" };
1036+
9871037
if (currentMode == WiFiState::connected)
9881038
{
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+
}
9921049
}
9931050
else if (currentMode == WiFiState::runningAsAccessPoint)
9941051
{
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+
9951063
platform.MessageF(mtype, "Connected clients %u\n", (unsigned int)r.numClients);
9961064
}
9971065
// status, ssid and hostName not displayed
@@ -1105,7 +1173,7 @@ void WiFiInterface::EspRequestsTransfer() noexcept
11051173
void WiFiInterface::SetIPAddress(IPAddress p_ip, IPAddress p_netmask, IPAddress p_gateway) noexcept
11061174
{
11071175
ipAddress = p_ip;
1108-
usingDhcp = ipAddress.IsNull();
1176+
usingDhcp = false; // Mirror the value returned by the ESP module
11091177
netmask = p_netmask;
11101178
gateway = p_gateway;
11111179
}

src/Networking/ESP8266WiFi/WiFiInterface.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class WiFiInterface : public NetworkInterface
161161
char requestedSsid[SsidLength + 1];
162162
char actualSsid[SsidLength + 1];
163163

164+
int8_t rssi;
164165
unsigned int spiTxUnderruns;
165166
unsigned int spiRxOverruns;
166167
unsigned int reconnectCount;
@@ -171,12 +172,14 @@ class WiFiInterface : public NetworkInterface
171172
char wiFiServerVersion[16];
172173

173174
bool usingDhcp = true;
175+
uint32_t lastStatusPoll;
174176

175177
// For processing debug messages from the WiFi module
176178
bool serialRunning;
177179
bool debugPrintPending;
178180
char debugMessageBuffer[200];
179181
size_t debugMessageChars;
182+
180183
};
181184

182185
#endif // HAS_WIFI_NETWORKING

0 commit comments

Comments
 (0)