Commit abb57e6e authored by Matej Frančeškin's avatar Matej Frančeškin

Microhard - encryption key

parent e2291251
......@@ -22,6 +22,7 @@ static const char *kLOCAL_IP = "LocalIP";
static const char *kREMOTE_IP = "RemoteIP";
static const char *kNET_MASK = "NetMask";
static const char *kCFG_PASSWORD = "ConfigPassword";
static const char *kENC_KEY = "EncryptionKey";
//-----------------------------------------------------------------------------
MicrohardManager::MicrohardManager(QGCApplication* app, QGCToolbox* toolbox)
......@@ -37,6 +38,7 @@ MicrohardManager::MicrohardManager(QGCApplication* app, QGCToolbox* toolbox)
_remoteIPAddr = settings.value(kREMOTE_IP, QString("192.168.168.2")).toString();
_netMask = settings.value(kNET_MASK, QString("255.255.255.0")).toString();
_configPassword = settings.value(kCFG_PASSWORD, QString("admin")).toString();
_encryptionKey = settings.value(kENC_KEY, QString("1234567890")).toString();
settings.endGroup();
}
......@@ -50,6 +52,9 @@ MicrohardManager::~MicrohardManager()
void
MicrohardManager::_close()
{
_workTimer.stop();
_locTimer.stop();
_remTimer.stop();
if(_mhSettingsLoc) {
_mhSettingsLoc->close();
_mhSettingsLoc->deleteLater();
......@@ -108,13 +113,20 @@ MicrohardManager::setToolbox(QGCToolbox* toolbox)
//-----------------------------------------------------------------------------
bool
MicrohardManager::setIPSettings(QString localIP_, QString remoteIP_, QString netMask_, QString cfgPassword_)
MicrohardManager::setIPSettings(QString localIP_, QString remoteIP_, QString netMask_, QString cfgPassword_, QString encryptionKey_)
{
if (_localIPAddr != localIP_ || _remoteIPAddr != remoteIP_ || _netMask != netMask_ || _configPassword != cfgPassword_) {
if (_localIPAddr != localIP_ || _remoteIPAddr != remoteIP_ || _netMask != netMask_ ||
_configPassword != cfgPassword_ || _encryptionKey != encryptionKey_)
{
if (_mhSettingsLoc && _encryptionKey != encryptionKey_) {
_mhSettingsLoc->setEncryptionKey(encryptionKey_);
}
_localIPAddr = localIP_;
_remoteIPAddr = remoteIP_;
_netMask = netMask_;
_configPassword = cfgPassword_;
_encryptionKey = encryptionKey_;
QSettings settings;
settings.beginGroup(kMICROHARD_GROUP);
......@@ -122,6 +134,7 @@ MicrohardManager::setIPSettings(QString localIP_, QString remoteIP_, QString net
settings.setValue(kREMOTE_IP, remoteIP_);
settings.setValue(kNET_MASK, netMask_);
settings.setValue(kCFG_PASSWORD, cfgPassword_);
settings.setValue(kENC_KEY, encryptionKey_);
settings.endGroup();
_reset();
......@@ -139,7 +152,7 @@ MicrohardManager::_setEnabled()
bool enable = _appSettings->enableMicrohard()->rawValue().toBool();
if(enable) {
if(!_mhSettingsLoc) {
_mhSettingsLoc = new MicrohardSettings(localIPAddr(), this);
_mhSettingsLoc = new MicrohardSettings(localIPAddr(), this, true);
connect(_mhSettingsLoc, &MicrohardSettings::connected, this, &MicrohardManager::_connectedLoc);
connect(_mhSettingsLoc, &MicrohardSettings::rssiUpdated, this, &MicrohardManager::_rssiUpdatedLoc);
}
......@@ -151,7 +164,6 @@ MicrohardManager::_setEnabled()
_workTimer.start(1000);
} else {
//-- Stop everything
_workTimer.stop();
_close();
}
_enabled = enable;
......@@ -184,6 +196,7 @@ MicrohardManager::_rssiUpdatedLoc(int rssi)
_downlinkRSSI = rssi;
_locTimer.stop();
_locTimer.start(LONG_TIMEOUT);
emit connectedChanged();
emit linkChanged();
}
......@@ -194,6 +207,7 @@ MicrohardManager::_rssiUpdatedRem(int rssi)
_uplinkRSSI = rssi;
_remTimer.stop();
_remTimer.start(LONG_TIMEOUT);
emit linkConnectedChanged();
emit linkChanged();
}
......
......@@ -34,34 +34,34 @@ public:
Q_PROPERTY(QString remoteIPAddr READ remoteIPAddr NOTIFY remoteIPAddrChanged)
Q_PROPERTY(QString netMask READ netMask NOTIFY netMaskChanged)
Q_PROPERTY(QString configPassword READ configPassword NOTIFY configPasswordChanged)
Q_PROPERTY(QString encryptionKey READ encryptionKey NOTIFY encryptionKeyChanged)
Q_INVOKABLE bool setIPSettings (QString localIP, QString remoteIP, QString netMask, QString cfgPassword);
Q_INVOKABLE bool setIPSettings (QString localIP, QString remoteIP, QString netMask, QString cfgPassword, QString encyrptionKey);
explicit MicrohardManager (QGCApplication* app, QGCToolbox* toolbox);
~MicrohardManager () override;
void setToolbox (QGCToolbox* toolbox) override;
bool connected () { return _isConnected; }
bool linkConnected () { return _linkConnected; }
bool connected () { return _isConnected && _mhSettingsLoc && _mhSettingsLoc->loggedIn(); }
bool linkConnected () { return _linkConnected && _mhSettingsRem && _mhSettingsRem->loggedIn(); }
int uplinkRSSI () { return _downlinkRSSI; }
int downlinkRSSI () { return _uplinkRSSI; }
QString localIPAddr () { return _localIPAddr; }
QString remoteIPAddr () { return _remoteIPAddr; }
QString netMask () { return _netMask; }
QString configPassword () { return _configPassword; }
QString encryptionKey () { return _encryptionKey; }
signals:
void linkChanged ();
void linkConnectedChanged ();
void infoChanged ();
void connectedChanged ();
void decodeIndexChanged ();
void rateIndexChanged ();
void localIPAddrChanged ();
void remoteIPAddrChanged ();
void netMaskChanged ();
void configPasswordChanged ();
void encryptionKeyChanged ();
private slots:
void _connectedLoc ();
......@@ -79,7 +79,6 @@ private:
FactMetaData *_createMetadata (const char *name, QStringList enums);
private:
bool _running = false;
bool _isConnected = false;
AppSettings* _appSettings = nullptr;
MicrohardSettings* _mhSettingsLoc = nullptr;
......@@ -95,5 +94,6 @@ private:
QString _remoteIPAddr;
QString _netMask;
QString _configPassword;
QString _encryptionKey;
QTime _timeoutTimer;
};
......@@ -14,10 +14,11 @@
#include "VideoManager.h"
//-----------------------------------------------------------------------------
MicrohardSettings::MicrohardSettings(QString address, QObject* parent)
MicrohardSettings::MicrohardSettings(QString address_, QObject* parent, bool setEncryptionKey)
: MicrohardHandler(parent)
{
_address = address;
_address = address_;
_setEncryptionKey = setEncryptionKey;
}
//-----------------------------------------------------------------------------
......@@ -25,7 +26,7 @@ bool
MicrohardSettings::start()
{
qCDebug(MicrohardLog) << "Start Microhard Settings";
_connectionState = 0;
_loggedIn = false;
return _start(MICROHARD_SETTINGS_PORT, QHostAddress(_address));
}
......@@ -33,11 +34,20 @@ MicrohardSettings::start()
void
MicrohardSettings::getStatus()
{
if (_connectionState == 1) {
if (_loggedIn) {
_tcpSocket->write("AT+MWSTATUS\n");
}
}
//-----------------------------------------------------------------------------
void
MicrohardSettings::setEncryptionKey(QString key)
{
QString cmd = "AT+MWVENCRYPT=1," + key + "\n";
_tcpSocket->write(cmd.toStdString().c_str());
qCDebug(MicrohardLog) << "setEncryptionKey: " << cmd;
}
//-----------------------------------------------------------------------------
void
MicrohardSettings::_readBytes()
......@@ -46,7 +56,7 @@ MicrohardSettings::_readBytes()
qCDebug(MicrohardVerbose) << "Read bytes: " << bytesIn;
if (_connectionState == 1) {
if (_loggedIn) {
int i1 = bytesIn.indexOf("RSSI (dBm)");
if (i1 > 0) {
int i2 = bytesIn.indexOf(": ", i1);
......@@ -65,7 +75,10 @@ MicrohardSettings::_readBytes()
std::string pwd = qgcApp()->toolbox()->microhardManager()->configPassword().toStdString() + "\n";
_tcpSocket->write(pwd.c_str());
} else if (bytesIn.contains("UserDevice>")) {
_connectionState = 1;
if (!loggedIn() && _setEncryptionKey) {
setEncryptionKey(qgcApp()->toolbox()->microhardManager()->encryptionKey());
}
_loggedIn = true;
}
emit rssiUpdated(_rssiVal);
......
......@@ -15,10 +15,11 @@ class MicrohardSettings : public MicrohardHandler
{
Q_OBJECT
public:
explicit MicrohardSettings (QString address, QObject* parent = nullptr);
explicit MicrohardSettings (QString address, QObject* parent = nullptr, bool setEncryptionKey = false);
bool start () override;
void getStatus ();
void setEncryptionKey (QString key);
bool loggedIn () { return _loggedIn; }
protected slots:
void _readBytes () override;
......@@ -27,7 +28,8 @@ signals:
void updateRSSI (int rssi);
private:
int _connectionState;
bool _loggedIn;
int _rssiVal;
QString _address;
bool _setEncryptionKey;
};
......@@ -142,13 +142,13 @@ QGCView {
text: qsTr("Uplink RSSI:")
}
QGCLabel {
text: QGroundControl.microhardManager.linkConnected ? QGroundControl.microhardManager.uplinkRSSI : ""
text: QGroundControl.microhardManager.linkConnected && QGroundControl.microhardManager.uplinkRSSI < 0 ? QGroundControl.microhardManager.uplinkRSSI : ""
}
QGCLabel {
text: qsTr("Downlink RSSI:")
}
QGCLabel {
text: QGroundControl.microhardManager.linkConnected ? QGroundControl.microhardManager.downlinkRSSI : ""
text: QGroundControl.microhardManager.linkConnected && QGroundControl.microhardManager.downlinkRSSI < 0 ? QGroundControl.microhardManager.downlinkRSSI : ""
}
}
}
......@@ -225,6 +225,16 @@ QGCView {
inputMethodHints: Qt.ImhHiddenText
Layout.minimumWidth: _valueWidth
}
QGCLabel {
text: qsTr("Encryption key:")
}
QGCTextField {
id: encryptionKey
text: QGroundControl.microhardManager.encryptionKey
enabled: true
inputMethodHints: Qt.ImhHiddenText
Layout.minimumWidth: _valueWidth
}
}
Item {
width: 1
......@@ -240,7 +250,8 @@ QGCView {
if(localIP.text === QGroundControl.microhardManager.localIPAddr &&
remoteIP.text === QGroundControl.microhardManager.remoteIPAddr &&
netMask.text === QGroundControl.microhardManager.netMask &&
configPassword.text === QGroundControl.microhardManager.configPassword)
configPassword.text === QGroundControl.microhardManager.configPassword &&
encryptionKey.text === QGroundControl.microhardManager.encryptionKey)
return false
if(!validateIPaddress(localIP.text)) return false
if(!validateIPaddress(remoteIP.text)) return false
......@@ -250,6 +261,10 @@ QGCView {
enabled: testEnabled()
text: qsTr("Apply")
anchors.horizontalCenter: parent.horizontalCenter
onClicked: {
QGroundControl.microhardManager.setIPSettings(localIP.text, remoteIP.text, netMask.text, configPassword.text, encryptionKey.text)
}
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment