Skip to content

Commit

Permalink
Update wifi interface OM fields
Browse files Browse the repository at this point in the history
  • Loading branch information
rechrtb committed Nov 14, 2022
1 parent 695f2bf commit 5af5522
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 17 deletions.
102 changes: 85 additions & 17 deletions src/Networking/ESP8266WiFi/WiFiInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const uint32_t WiFiFastResponseTimeoutMillis = 20; // SPI timeout when when the
const uint32_t WiFiWaitReadyMillis = 100;
const uint32_t WiFiStartupMillis = 15000; // Formatting the SPIFFS partition can take up to 10s.
const uint32_t WiFiStableMillis = 100;
const uint32_t WiFiStatusPollMillis = 500; // Poll interval for status details of the WiFi module

const unsigned int MaxHttpConnections = 4;

Expand Down Expand Up @@ -287,7 +288,7 @@ WiFiInterface::WiFiInterface(Platform& p) noexcept
: platform(p), bufferOut(nullptr), bufferIn(nullptr), uploader(nullptr), espWaitingTask(nullptr),
ftpDataPort(0), closeDataPort(false),
requestedMode(WiFiState::disabled), currentMode(WiFiState::disabled), activated(false),
espStatusChanged(false), spiTxUnderruns(0), spiRxOverruns(0), serialRunning(false), debugMessageChars(0)
espStatusChanged(false), rssi(INT8_MIN), spiTxUnderruns(0), spiRxOverruns(0), serialRunning(false), debugMessageChars(0)
{
wifiInterface = this;

Expand Down Expand Up @@ -326,16 +327,18 @@ WiFiInterface::WiFiInterface(Platform& p) noexcept
constexpr ObjectModelTableEntry WiFiInterface::objectModelTable[] =
{
// These entries must be in alphabetical order
{ "actualIP", OBJECT_MODEL_FUNC(self->ipAddress), ObjectModelEntryFlags::none },
{ "firmwareVersion", OBJECT_MODEL_FUNC(self->wiFiServerVersion), ObjectModelEntryFlags::none },
{ "gateway", OBJECT_MODEL_FUNC(self->gateway), ObjectModelEntryFlags::none },
{ "mac", OBJECT_MODEL_FUNC(self->macAddress), ObjectModelEntryFlags::none },
{ "state", OBJECT_MODEL_FUNC(self->GetStateName()), ObjectModelEntryFlags::none },
{ "subnet", OBJECT_MODEL_FUNC(self->netmask), ObjectModelEntryFlags::none },
{ "type", OBJECT_MODEL_FUNC_NOSELF("wifi"), ObjectModelEntryFlags::none },
{ "actualIP", OBJECT_MODEL_FUNC(self->ipAddress), ObjectModelEntryFlags::none },
{ "firmwareVersion", OBJECT_MODEL_FUNC(self->wiFiServerVersion), ObjectModelEntryFlags::none },
{ "gateway", OBJECT_MODEL_FUNC(self->gateway), ObjectModelEntryFlags::none },
{ "mac", OBJECT_MODEL_FUNC(self->macAddress), ObjectModelEntryFlags::none },
{ "numReconnects", OBJECT_MODEL_FUNC((uint32_t)self->reconnectCount), ObjectModelEntryFlags::none },
{ "signal", OBJECT_MODEL_FUNC((int32_t)self->rssi), ObjectModelEntryFlags::none },
{ "state", OBJECT_MODEL_FUNC(self->GetStateName()), ObjectModelEntryFlags::none },
{ "subnet", OBJECT_MODEL_FUNC(self->netmask), ObjectModelEntryFlags::none },
{ "type", OBJECT_MODEL_FUNC_NOSELF("wifi"), ObjectModelEntryFlags::none },
};

constexpr uint8_t WiFiInterface::objectModelTableDescriptor[] = { 1, 7 };
constexpr uint8_t WiFiInterface::objectModelTableDescriptor[] = { 1, 9 };

DEFINE_GET_OBJECT_MODEL_TABLE(WiFiInterface)

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

// Set the hostname before anything else is done
rc = SendCommand(NetworkCommand::networkSetHostName, 0, 0, 0, reprap.GetNetwork().GetHostname(), HostNameLength, nullptr, 0);
Expand Down Expand Up @@ -846,6 +848,19 @@ void WiFiInterface::Spin() noexcept
}
}
}

// Update details that change constantly about the WiFi module
if (millis() - lastStatusPoll >= WiFiStatusPollMillis)
{
Receiver<NetworkStatusResponse> status;
const uint32_t rc = SendCommand(NetworkCommand::networkGetStatus, 0, 0, nullptr, 0, status);
rssi = status.Value().rssi;
if (rc > offsetof(NetworkStatusResponse, netmask))
{
reconnectCount = status.Value().numReconnects;
}
lastStatusPoll = millis();
}
}
break;

Expand All @@ -867,19 +882,41 @@ void WiFiInterface::Spin() noexcept
{
// Get our IP address, this needs to be correct for FTP to work
Receiver<NetworkStatusResponse> status;
if (SendCommand(NetworkCommand::networkGetStatus, 0, 0, nullptr, 0, status) > 0)
const uint32_t rc = SendCommand(NetworkCommand::networkGetStatus, 0, 0, nullptr, 0, status);
if (rc > 0)
{
ipAddress.SetV4LittleEndian(status.Value().ipAddress);
macAddress.SetFromBytes(status.Value().macAddress); // MAC address for AP and STA are separate and different
SafeStrncpy(actualSsid, status.Value().ssid, SsidLength);

if (rc > offsetof(NetworkStatusResponse, netmask))
{
netmask.SetV4LittleEndian(status.Value().netmask);
gateway.SetV4LittleEndian(status.Value().gateway);
usingDhcp = status.Value().usingDhcpc;
}
}
InitSockets();
reconnectCount = 0;
platform.MessageF(NetworkInfoMessage, "WiFi module is %s%s, IP address %s\n",
TranslateWiFiState(currentMode),
actualSsid,
IP4String(ipAddress).c_str());

lastStatusPoll = 0;
}
break;
case WiFiState::idle:
{
uint8_t zero[6];
memset(zero, 0, sizeof(zero));
macAddress.SetFromBytes(zero);
SetIPAddress(DefaultIpAddress, DefaultNetMask, DefaultGateway);
strcpy(actualSsid, "(unknown)");
reconnectCount = 0;
rssi = INT8_MIN;
usingDhcp = false;
}

default:
if (requestedMode != WiFiState::connected)
Expand Down Expand Up @@ -969,7 +1006,8 @@ void WiFiInterface::Diagnostics(MessageType mtype) noexcept
{
Receiver<NetworkStatusResponse> status;
status.Value().clockReg = 0xFFFFFFFF; // older WiFi firmware doesn't return this value, so preset it
if (SendCommand(NetworkCommand::networkGetStatus, 0, 0, nullptr, 0, status) > 0)
const uint32_t rc = SendCommand(NetworkCommand::networkGetStatus, 0, 0, nullptr, 0, status);
if (rc > 0)
{
NetworkStatusResponse& r = status.Value();
r.versionText[ARRAY_UPB(r.versionText)] = 0;
Expand All @@ -981,17 +1019,47 @@ void WiFiInterface::Diagnostics(MessageType mtype) noexcept

if (currentMode == WiFiState::connected || currentMode == WiFiState::runningAsAccessPoint)
{
platform.MessageF(mtype, "WiFi IP address %s\n", IP4String(r.ipAddress).c_str());
if (rc > offsetof(NetworkStatusResponse, netmask))
{
platform.MessageF(mtype, "WiFi IP address %s, netmask %s, gateway %s%s\n",
IP4String(r.ipAddress).c_str(), IP4String(r.netmask).c_str(), IP4String(r.gateway).c_str(),
currentMode == WiFiState::connected ? (r.usingDhcpc ? " (via DHCP)" : " (set manually)") : "");
}
else
{
platform.MessageF(mtype, "WiFi IP address %s\n", IP4String(r.ipAddress).c_str());
}
}

constexpr const char* SleepModes[4] = { "unknown", "none", "light", "modem" };
constexpr const char* ConnectionModes[4] = { "none", "802.11b", "802.11g", "802.11n" };

if (currentMode == WiFiState::connected)
{
constexpr const char* SleepModes[4] = { "unknown", "none", "light", "modem" };
constexpr const char* ConnectionModes[4] = { "none", "802.11b", "802.11g", "802.11n" };
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]);
if (rc > offsetof(NetworkStatusResponse, netmask))
{
platform.MessageF(mtype, "WiFi signal strength %ddBm, mode %s, reconnections %u, sleep mode %s, channel %u (%s), auth %s\n",
(int)r.rssi, ConnectionModes[r.phyMode], reconnectCount, SleepModes[r.sleepMode],
r.channel, static_cast<HTMode>(r.ht) == HTMode::HT20 ? "20 MHz" : "40 MHz", GetWiFiAuthFriendlyStr(r.auth));
}
else
{
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]);
}
}
else if (currentMode == WiFiState::runningAsAccessPoint)
{
if (rc > offsetof(NetworkStatusResponse, netmask))
{
platform.MessageF(mtype, "WiFi mode %s, sleep mode %s, channel %u (%s), auth %s\n",
ConnectionModes[r.phyMode], SleepModes[r.sleepMode], r.channel,
static_cast<HTMode>(r.ht) == HTMode::HT20 ? "20 MHz" : "40 MHz", GetWiFiAuthFriendlyStr(r.auth));
}
else
{
platform.MessageF(mtype, "WiFi mode %s, sleep mode %s\n", ConnectionModes[r.phyMode], SleepModes[r.sleepMode]);
}

platform.MessageF(mtype, "Connected clients %u\n", (unsigned int)r.numClients);
}
// status, ssid and hostName not displayed
Expand Down Expand Up @@ -1105,7 +1173,7 @@ void WiFiInterface::EspRequestsTransfer() noexcept
void WiFiInterface::SetIPAddress(IPAddress p_ip, IPAddress p_netmask, IPAddress p_gateway) noexcept
{
ipAddress = p_ip;
usingDhcp = ipAddress.IsNull();
usingDhcp = false; // Mirror the value returned by the ESP module
netmask = p_netmask;
gateway = p_gateway;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Networking/ESP8266WiFi/WiFiInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ class WiFiInterface : public NetworkInterface
char requestedSsid[SsidLength + 1];
char actualSsid[SsidLength + 1];

int8_t rssi;
unsigned int spiTxUnderruns;
unsigned int spiRxOverruns;
unsigned int reconnectCount;
Expand All @@ -171,12 +172,14 @@ class WiFiInterface : public NetworkInterface
char wiFiServerVersion[16];

bool usingDhcp = true;
uint32_t lastStatusPoll;

// For processing debug messages from the WiFi module
bool serialRunning;
bool debugPrintPending;
char debugMessageBuffer[200];
size_t debugMessageChars;

};

#endif // HAS_WIFI_NETWORKING
Expand Down

0 comments on commit 5af5522

Please sign in to comment.