Skip to content

Commit a096525

Browse files
committed
Parse wifi firmware version
1 parent fe7f4a6 commit a096525

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

src/Networking/ESP8266WiFi/WiFiInterface.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,40 @@ void WiFiInterface::Stop() noexcept
622622
}
623623
}
624624

625+
bool WiFiInterface::GetFirmwareVersion(int &major, int &minor, int &patch)
626+
{
627+
// Simple parser for WiFi module firmware version string.
628+
// Parse the format (x=major, y=minor, and z=patch):
629+
// - x.y.z (from 2.1.0)
630+
// - x.y (prior to 2.1.0), returns -1 for z
631+
// Should be able to handle appended string after the version string.
632+
major = minor = patch = -1;
633+
auto numbers = {&major, &minor, &patch};
634+
635+
const char *curr = wiFiServerVersion.c_str();
636+
const char *end = nullptr;
637+
638+
for (auto num : numbers)
639+
{
640+
int temp = StrToI32(curr, &end);
641+
642+
if (curr == end)
643+
{
644+
if (num != &patch)
645+
{
646+
return false;
647+
}
648+
break;
649+
}
650+
651+
*num = temp;
652+
curr = end + 1;
653+
end = nullptr;
654+
}
655+
656+
return true;
657+
}
658+
625659
void WiFiInterface::Spin() noexcept
626660
{
627661
// Main state machine.
@@ -685,9 +719,10 @@ void WiFiInterface::Spin() noexcept
685719
reprap.GetPlatform().MessageF(NetworkErrorMessage, "failed to set WiFi hostname: %s\n", TranslateWiFiResponse(rc));
686720
}
687721
#if SAME5x
722+
int majorVer = 0, dummy = -1;
688723
// If running the RTOS-based WiFi module code, tell the module to increase SPI clock speed to 40MHz.
689724
// This is safe on SAME5x processors but not on SAM4 processors.
690-
if (isdigit(wiFiServerVersion[0]) && wiFiServerVersion[0] >= '2')
725+
if (GetFirmwareVersion(majorVer, dummy, dummy) && (majorVer >= 2))
691726
{
692727
rc = SendCommand(NetworkCommand::networkSetClockControl, 0, 0, 0x2001, nullptr, 0, nullptr, 0);
693728
if (rc != ResponseEmpty)

src/Networking/ESP8266WiFi/WiFiInterface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ class WiFiInterface : public NetworkInterface
9191
void EspRequestsTransfer() noexcept;
9292
void UpdateSocketStatus(uint16_t connectedSockets, uint16_t otherEndClosedSockets) noexcept;
9393

94+
bool GetFirmwareVersion(int &major, int &minor, int &patch);
95+
9496
protected:
9597
DECLARE_OBJECT_MODEL
9698

src/Networking/ESP8266WiFi/WiFiSocket.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ void WiFiSocket::Poll() noexcept
201201
if (state != SocketState::waitingForResponder)
202202
{
203203
WiFiInterface *iface = static_cast<WiFiInterface *>(interface);
204-
if (isdigit(iface->wiFiServerVersion[0]) && iface->wiFiServerVersion[0] >= '2')
204+
int majorVer = 0, dummy = -1;
205+
if (iface->GetFirmwareVersion(majorVer, dummy, dummy) && majorVer >= 2)
205206
{
206207
// On version 2 onwards, this is a valid field ConnStatusResponse.
207208
protocol = resp.Value().protocol;

0 commit comments

Comments
 (0)