shared-libraries/ScStwLibraries/headers/ScStw.hpp

254 lines
7.2 KiB
C++

/****************************************************************************
** ScStw Libraries
** Copyright (C) 2020 Itsblue development
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#ifndef SCSTW_HPP
#define SCSTW_HPP
#include <QObject>
#include <QMap>
#include <QMetaEnum>
/*!
* \mainpage ScStw Libraries documentation
*
* \section intro_sec Introduction
*
* This library is meant for usage with the Speed climbing stopwatch project.
* It contains some helper classes to build a client application for the ScStw basestation with Qt.
*
* \section section Installation
* \code{.sh}
* cd yourRepo
* git submodule add https://git.itsblue.de/ScStw/shared-libraries/
* git submodule update --init --recursive
* \endcode
*
* And in your MyProject.pro include the .pri file:
* \code{.pro}
* include($$PWD/shared-libraries/ScStwLibraries/ScStwLibraries.pri)
* \endcode
*/
/*!
* \brief The ScStw class provides some shared functions and enums for use in the ScStw project.
*/
class ScStw : public QObject {
Q_OBJECT
public:
/*!
* \brief The SignalKey enum contains all signal keys a client can subscribe to
*
* \see ScStw::signalKeyFromInt()
*/
enum SignalKey {
InvalidSignal = -1,
RaceStateChanged = 9000,
TimersChanged = 9001,
ExtensionsChanged = 9002,
CurrentStartDelayChanged = 9003, /*, ProfilesChanged*/
SettingChanged = 9004,
RaceDetailsChanged = 9005
};
Q_ENUM(SignalKey)
/*!
* \brief The SocketCommand enum contains all commands the base station can handle
*
* \see ScStw::socketCommandFromInt()
*/
enum SocketCommand {
InvalidCommand = -1,
InitializeSessionCommand = 1,
StartRaceCommand = 1000,
StopRaceCommand = 1001,
ResetRaceCommand = 1002,
CancelRaceCommand = 1003,
SetTimerDisabledCommand = 1004,
GetRaceStateCommand = 2000,
GetRaceDetailsCommand = 2001,
GetExtensionsCommand = 2006,
GetTimersCommand = 2007,
GetCurrentStartDelayCommand = 2009,
WriteSettingCommand = 3000,
ReadSettingCommand = 3001,
LoginAthleteCommand = 4000,
CreateAthleteCommand = 4001,
DeleteAthleteCommand = 4002,
GetAtheletesCommand = 4003,
GetAthleteResultsCommand = 4004,
UpdateFirmwareCommand = 5000,
UpdateSystemTimeCommand = 5001,
PairExtensionsCommand = 5002
};
Q_ENUM(SocketCommand);
/*!
* \brief The ErrorCode enum contains all error codes that can occur when sending a command to the basestation
*/
enum StatusCode {
Success = 200,
FirmwareAlreadyUpToDateInfo = 304,
AccessDeniedError = 401,
UpdateSignatureInvalidError = 402,
CurrentStateNotVaildForOperationError = 403,
CommandNotFoundError = 404,
RequiredParameterNotGivenError = 405,
TimestampTooSmallError = 406,
ClientSessionAlreadyActiveError = 407,
NoSessionActiveError = 408,
ItemNotFoundError = 409,
LastTimerCannotBeDisabledError = 410,
UpdateFailedError = 500,
Error = 900,
NotConnectedError = 910,
TimeoutError = 911,
SettingNotAccessibleError = 901,
InternalError = 950,
InternalErrorTimerOperationFailed = 951,
ApiVersionNotSupportedError = 952,
CompetitionModeProhibitsThisError = 953,
FirmwareUpdateFormatInvalidError = 954,
TimersNotReadyError = 501 /*!< One or more timer is not ready */
};
Q_ENUM(ScStw::StatusCode)
/*!
* \brief The ExtensionType enum contains all types of extensions
*/
enum ExtensionType {
StartPad,
TopPad
};
Q_ENUM(ExtensionType);
/*!
* \brief The ExtensionState enum contains all possible states of an extension
*/
enum ExtensionState {
ExtensionDisconnected = 0,
ExtensionConnecting = 1,
ExtensionInitialising = 2,
ExtensionConnected = 3
};
Q_ENUM(ExtensionState);
/*!
* \brief The ExtensionBatteryState enum contains all possible battery states of an extension
*/
enum ExtensionBatteryState {
BatteryUnknown = -1,
BatteryCritical = 0,
BatteryWarning = 1,
BatteryFine = 2,
BatteryCharging = 3,
BatteryNotCharging = 4
};
Q_ENUM(ExtensionBatteryState);
/*!
* \brief The PadState enum contains whether a pad is currently pressed or not
*/
enum PadState {
PadNotPressed = 0,
PadPressed = 1
};
Q_ENUM(PadState);
/*!
* \brief SOCKET_MESSAGE_START_KEY contains the key, a message is supposed to start with
*/
static const char* SOCKET_MESSAGE_START_KEY;
/*!
* \brief SOCKET_MESSAGE_END_KEY contains the key, a message is supposed to end with
*/
static const char* SOCKET_MESSAGE_END_KEY;
/*!
* \brief Function to convert an int to a SignalKey
* \param i the int to convert
* \return a SignalKey
*
* \see ScStw::SignalKey
*/
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 convert an ExtensionType to a string
* \param t the ExtensionType to convert
* \return String
*
* \see ScStwExtensionType
*/
static QString extensionTypeToString(ExtensionType t);
/*!
* \brief Function to compare to string firmware versions in <major>.<minor>.<patch> 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);
/*!
* \brief Function to convert a value to an enum
*/
template <typename Enum>
static Enum toEnumValue(const int &value, bool *ok)
{
QMetaEnum enumeration = QMetaEnum::fromType<Enum>();
return static_cast<Enum>(enumeration.keyToValue(enumeration.valueToKey(value), ok));
}
ScStw() : QObject(nullptr) {};
private:
};
#endif // SCSTW_HPP