Skip to content

Commit

Permalink
Parse WiFi version string for major, minor values
Browse files Browse the repository at this point in the history
  • Loading branch information
rechrtb committed Nov 25, 2022
1 parent df86be6 commit 8086361
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/Networking/ESP8266WiFi/WiFiInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -719,8 +719,24 @@ void WiFiInterface::Spin() noexcept
int32_t rc = SendCommand(NetworkCommand::networkGetStatus, 0, 0, nullptr, 0, status);
if (rc > 0)
{
memset(wiFiServerVersion, 0, sizeof(wiFiServerVersion));
SafeStrncpy(wiFiServerVersion, status.Value().versionText, ARRAY_SIZE(wiFiServerVersion));

// Parse the version string to obtain major and minor versions. Expecting a string of the format
// x.yn where x is the major version, y the minor version and n a development stage descriptor string.
const char *verStr = wiFiServerVersion;
majorVersion = minorVersion = 0;
majorVersion = StrToI32(verStr, &verStr);

if (majorVersion >= 1 && majorVersion <= 2) // The server versions valid for this RRF version
{
minorVersion = StrToI32(verStr + 1);
}
else
{
reprap.GetPlatform().MessageF(NetworkErrorMessage, "unable to parse wifi server version string");
}

// Set the hostname before anything else is done
rc = SendCommand(NetworkCommand::networkSetHostName, 0, 0, 0, reprap.GetNetwork().GetHostname(), HostNameLength, nullptr, 0);
if (rc != ResponseEmpty)
Expand All @@ -730,7 +746,7 @@ void WiFiInterface::Spin() noexcept
#if SAME5x
// If running the RTOS-based WiFi module code, tell the module to increase SPI clock speed to 40MHz.
// This is safe on SAME5x processors but not on SAM4 processors.
if (isdigit(wiFiServerVersion[0]) && wiFiServerVersion[0] >= '2')
if (majorVersion >= 2)
{
rc = SendCommand(NetworkCommand::networkSetClockControl, 0, 0, 0x2001, nullptr, 0, nullptr, 0);
if (rc != ResponseEmpty)
Expand Down Expand Up @@ -857,7 +873,7 @@ void WiFiInterface::Spin() noexcept
if (rc > 0)
{
rssi = status.Value().rssi;
if (rc >= offsetof(NetworkStatusResponse, netmask))
if (majorVersion >= 2)
{
reconnectCount = status.Value().numReconnects;
}
Expand Down Expand Up @@ -892,7 +908,7 @@ void WiFiInterface::Spin() noexcept
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))
if (majorVersion >= 2)
{
netmask.SetV4LittleEndian(status.Value().netmask);
gateway.SetV4LittleEndian(status.Value().gateway);
Expand Down Expand Up @@ -1022,7 +1038,7 @@ void WiFiInterface::Diagnostics(MessageType mtype) noexcept

if (currentMode == WiFiState::connected || currentMode == WiFiState::runningAsAccessPoint)
{
if (rc > offsetof(NetworkStatusResponse, netmask))
if (majorVersion >= 2)
{
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(),
Expand All @@ -1039,7 +1055,7 @@ void WiFiInterface::Diagnostics(MessageType mtype) noexcept

if (currentMode == WiFiState::connected)
{
if (rc > offsetof(NetworkStatusResponse, netmask))
if (majorVersion >= 2)
{
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],
Expand All @@ -1052,7 +1068,7 @@ void WiFiInterface::Diagnostics(MessageType mtype) noexcept
}
else if (currentMode == WiFiState::runningAsAccessPoint)
{
if (rc > offsetof(NetworkStatusResponse, netmask))
if (majorVersion >= 2)
{
platform.MessageF(mtype, "WiFi mode %s, sleep mode %s, channel %u (%s), auth %s\n",
ConnectionModes[r.phyMode], SleepModes[r.sleepMode], r.channel,
Expand Down
2 changes: 2 additions & 0 deletions src/Networking/ESP8266WiFi/WiFiInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ class WiFiInterface : public NetworkInterface
unsigned int responseTimeoutCount;

char wiFiServerVersion[16];
uint8_t majorVersion;
uint8_t minorVersion;

bool usingDhcp = true;
uint32_t lastStatusPoll;
Expand Down

0 comments on commit 8086361

Please sign in to comment.