Skip to content

Commit

Permalink
Merge pull request emqx#102 from avsdev-cw/fix_002
Browse files Browse the repository at this point in the history
Added a version switch between MQTT V3.1.0 and V3.1.1
  • Loading branch information
mwallnoefer authored Jul 6, 2017
2 parents a9bdd73 + e295b54 commit 19cf202
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
12 changes: 12 additions & 0 deletions src/mqtt/qmqtt_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ void QMQTT::Client::setUsername(const QString& username)
d->setUsername(username);
}

QMQTT::MQTTVersion QMQTT::Client::version() const
{
Q_D(const Client);
return d->version();
}

void QMQTT::Client::setVersion(const QMQTT::MQTTVersion version)
{
Q_D(Client);
d->setVersion(version);
}

QString QMQTT::Client::password() const
{
Q_D(const Client);
Expand Down
12 changes: 8 additions & 4 deletions src/mqtt/qmqtt_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ static const quint8 LIBRARY_VERSION_MINOR = 3;
static const quint8 LIBRARY_VERSION_REVISION = 1;
//static const char* LIBRARY_VERSION = "0.3.1";

static const quint8 PROTOCOL_VERSION_MAJOR = 3;
static const quint8 PROTOCOL_VERSION_MINOR = 1;
static const quint8 PROTOCOL_VERSION_REVISION = 1;
//static const char* PROTOCOL_VERSION = "MQTT/3.1";
enum MQTTVersion
{
V3_1_0 = 3,
V3_1_1 = 4
};

enum ConnectionState
{
Expand Down Expand Up @@ -106,6 +107,7 @@ class Q_MQTT_EXPORT Client : public QObject
Q_PROPERTY(QString _username READ username WRITE setUsername)
Q_PROPERTY(QString _password READ password WRITE setPassword)
Q_PROPERTY(quint16 _keepAlive READ keepAlive WRITE setKeepAlive)
Q_PROPERTY(MQTTVersion _version READ version WRITE setVersion)
Q_PROPERTY(bool _autoReconnect READ autoReconnect WRITE setAutoReconnect)
Q_PROPERTY(int _autoReconnectInterval READ autoReconnectInterval WRITE setAutoReconnectInterval)
Q_PROPERTY(bool _cleanSession READ cleanSession WRITE setCleanSession)
Expand Down Expand Up @@ -152,6 +154,7 @@ class Q_MQTT_EXPORT Client : public QObject
QString clientId() const;
QString username() const;
QString password() const;
QMQTT::MQTTVersion version() const;
quint16 keepAlive() const;
bool cleanSession() const;
bool autoReconnect() const;
Expand All @@ -171,6 +174,7 @@ public slots:
void setClientId(const QString& clientId);
void setUsername(const QString& username);
void setPassword(const QString& password);
void setVersion(const MQTTVersion version);
void setKeepAlive(const quint16 keepAlive);
void setCleanSession(const bool cleanSession);
void setAutoReconnect(const bool value);
Expand Down
22 changes: 20 additions & 2 deletions src/mqtt/qmqtt_client_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ QMQTT::ClientPrivate::ClientPrivate(Client* qq_ptr)
: _host(QHostAddress::LocalHost)
, _port(1883)
, _gmid(1)
, _version(MQTTVersion::V3_1_0)
, _clientId(QUuid::createUuid().toString())
, _cleanSession(false)
, _keepAlive(300)
Expand Down Expand Up @@ -207,8 +208,15 @@ void QMQTT::ClientPrivate::sendConnect()
}

//payload
frame.writeString(QStringLiteral(PROTOCOL_MAGIC));
frame.writeChar(PROTOCOL_VERSION_MAJOR);
if(_version == V3_1_1)
{
frame.writeString(QStringLiteral(PROTOCOL_MAGIC_3_1_1));
}
else
{
frame.writeString(QStringLiteral(PROTOCOL_MAGIC_3_1_0));
}
frame.writeChar(_version);
frame.writeChar(flags);
frame.writeInt(_keepAlive);
frame.writeString(_clientId);
Expand Down Expand Up @@ -558,6 +566,16 @@ QString QMQTT::ClientPrivate::username() const
return _username;
}

void QMQTT::ClientPrivate::setVersion(const MQTTVersion version)
{
_version = version;
}

QMQTT::MQTTVersion QMQTT::ClientPrivate::version() const
{
return _version;
}

void QMQTT::ClientPrivate::setClientId(const QString& clientId)
{
if(clientId.isEmpty())
Expand Down
3 changes: 3 additions & 0 deletions src/mqtt/qmqtt_client_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class ClientPrivate
QString _hostName;
quint16 _port;
quint16 _gmid;
MQTTVersion _version;
QString _clientId;
QString _username;
QString _password;
Expand Down Expand Up @@ -120,6 +121,8 @@ class ClientPrivate
QString password() const;
void setUsername(const QString& username);
QString username() const;
void setVersion(const MQTTVersion);
MQTTVersion version() const;
void setClientId(const QString& clientId);
QString clientId() const;
void setPort(const quint16 port);
Expand Down
4 changes: 3 additions & 1 deletion src/mqtt/qmqtt_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@

#include <QObject>

#define PROTOCOL_MAGIC "MQIsdp"
#define PROTOCOL_MAGIC_3_1_0 "MQIsdp"
#define PROTOCOL_MAGIC_3_1_1 "MQTT"

#define RANDOM_CLIENT_PREFIX "QMQTT-"

#define CONNECT 0x10
Expand Down

0 comments on commit 19cf202

Please sign in to comment.