From 9d6cf5ab38a6ee05a81b7bbb9921448e42bc4382 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Sun, 10 May 2020 18:29:27 +0200 Subject: [PATCH] - added api firmware check before connection is initialized --- ScStwLibraries/headers/ScStw.hpp | 26 ++++++++++++++++ ScStwLibraries/headers/scstwclient.h | 3 ++ ScStwLibraries/sources/ScStw.cpp | 42 ++++++++++++++++++++++++++ ScStwLibraries/sources/scstwclient.cpp | 29 ++++++++++++------ 4 files changed, 91 insertions(+), 9 deletions(-) diff --git a/ScStwLibraries/headers/ScStw.hpp b/ScStwLibraries/headers/ScStw.hpp index e87160b..341ac36 100644 --- a/ScStwLibraries/headers/ScStw.hpp +++ b/ScStwLibraries/headers/ScStw.hpp @@ -80,6 +80,8 @@ public: /*! * \brief The SocketCommand enum contains all commands the base station can handle + * + * \see ScStw::socketCommandFromInt() */ enum SocketCommand { InvalidCommand = -1, @@ -194,8 +196,32 @@ public: */ static SignalKey signalKeyFromInt(int i); + /*! + * \brief Function to convert an int to a SocketCommand + * \param i the int to convert + * \return a SocketCommand + * + * \see ScStw::SocketCommand + */ static SocketCommand socketCommandFromInt(int i); + /*! + * \brief Function to compare to string firmware versions in .. formar + * \param a version a + * \param b version b + * \return -4: a is of invalid format + * -3: major of a is lower than b + * -2: minor of a is lower than b + * -1: patch of a is lower than b + * 0: a and b are identical + * 1: patch b is lower than a + * 2: minor of b is lower than a + * 3: major of b is lower than a + * 4: b is of invalid format + */ + static int firmwareCompare(QString a, QString b); + + ScStw() : QObject(nullptr) {}; private: }; diff --git a/ScStwLibraries/headers/scstwclient.h b/ScStwLibraries/headers/scstwclient.h index 5addc04..f1ec27f 100644 --- a/ScStwLibraries/headers/scstwclient.h +++ b/ScStwLibraries/headers/scstwclient.h @@ -45,6 +45,8 @@ public: enum State {DISCONNECTED, CONNECTING, INITIALISING, CONNECTED}; Q_ENUM(State); + const QString API_VERSION = "1.0.0"; + private: // values for the socket connection QString ip; @@ -58,6 +60,7 @@ private: // some meta data of the base station QString firmwareVersion; + QString apiVersion; double timeOffset; // the current state diff --git a/ScStwLibraries/sources/ScStw.cpp b/ScStwLibraries/sources/ScStw.cpp index fbbd0da..ded9515 100644 --- a/ScStwLibraries/sources/ScStw.cpp +++ b/ScStwLibraries/sources/ScStw.cpp @@ -73,3 +73,45 @@ ScStw::SocketCommand ScStw::socketCommandFromInt(int i) { else return SocketCommand(i); } + +int ScStw::firmwareCompare(QString a, QString b) { + /* + * * \return -3: major of a is lower than b + * -2: minor of a is lower than b + * -1: patch of a is lower than b + * 0: a and b are identical + * 1: patch b is lower than a + * 2: minor of b is lower than a + * 3: major of b is lower than a + */ + + if(a.count(".") != 3 || a.length() != 5) + return -4; + int aMajor = a.split(".")[0].toInt(); + int aMinor = a.split(".")[1].toInt(); + int aPatch = a.split(".")[2].toInt(); + + if(b.count(".") != 3 || a.length() != 5) + return 4; + int bMajor = b.split(".")[0].toInt(); + int bMinor = b.split(".")[1].toInt(); + int bPatch = b.split(".")[2].toInt(); + + if(a == b) + return 0; + + if(aMajor < bMajor) + return -3; + else + return 3; + + if(aMinor < bMinor) + return -2; + else + return 2; + + if(aPatch < bPatch) + return -1; + else + return 1; +} diff --git a/ScStwLibraries/sources/scstwclient.cpp b/ScStwLibraries/sources/scstwclient.cpp index 8937e6a..e011442 100644 --- a/ScStwLibraries/sources/scstwclient.cpp +++ b/ScStwLibraries/sources/scstwclient.cpp @@ -65,7 +65,25 @@ bool ScStwClient::init() { return false; } - this->firmwareVersion = initResponse["data"].toMap()["version"].toString(); + this->apiVersion = initResponse["data"].toMap()["apiVersion"].toString(); + int compareResult = ScStw::firmwareCompare(this->apiVersion, this->API_VERSION); + if( compareResult == -3 ){ + // the client version is out of date!! + this->closeConnection(); + return false; + } + else if(compareResult == 3){ + // the server version is out of date!! + this->closeConnection(); + return false; + } + else if(compareResult == -4){ + // the server sent an invalid version + this->closeConnection(); + return false; + } + + this->firmwareVersion = initResponse["data"].toMap()["firmwareVersion"].toString(); this->timeOffset = initResponse["data"].toMap()["time"].toDouble() - this->date->currentMSecsSinceEpoch(); qDebug() << "[INFO][BaseStation] Init done! firmware: version: " << this->firmwareVersion << " up-to-date: " << this->isFirmwareUpToDate() << " time offset: " << this->timeOffset; @@ -393,16 +411,9 @@ bool ScStwClient::isFirmwareUpToDate() { QString fileContents = f.readAll(); QString newFirmwareVersion = fileContents.split("")[1].split("")[0]; - int newFirmwareVersionMajor = newFirmwareVersion.split(".")[0].toInt(); - int newFirmwareVersionMinor = newFirmwareVersion.split(".")[1].toInt(); - int newFirmwareVersionPatch = newFirmwareVersion.split(".")[2].toInt(); - QString currentFirmwareVersion = this->firmwareVersion; - int currentFirmwareVersionMajor = currentFirmwareVersion.split(".")[0].toInt(); - int currentFirmwareVersionMinor = currentFirmwareVersion.split(".")[1].toInt(); - int currentFirmwareVersionPatch = currentFirmwareVersion.split(".")[2].toInt(); - return newFirmwareVersionMajor < currentFirmwareVersionMajor || newFirmwareVersionMinor < currentFirmwareVersionMinor || newFirmwareVersionPatch <= currentFirmwareVersionPatch; + return ScStw::firmwareCompare(currentFirmwareVersion, newFirmwareVersion) < 0; } // ------------------------