MicrohardManager.cc 9.7 KB
Newer Older
1 2
/****************************************************************************
 *
3
 *   (c) 2019 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9 10
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#include "MicrohardManager.h"
11
#include "MicrohardSettings.h"
12 13 14 15 16 17
#include "SettingsManager.h"
#include "QGCApplication.h"
#include "QGCCorePlugin.h"

#include <QSettings>

18 19
#define SHORT_TIMEOUT 2500
#define LONG_TIMEOUT  5000
20

21 22 23 24
static const char *kMICROHARD_GROUP     = "Microhard";
static const char *kLOCAL_IP            = "LocalIP";
static const char *kREMOTE_IP           = "RemoteIP";
static const char *kNET_MASK            = "NetMask";
25
static const char *kCFG_USERNAME        = "ConfigUserName";
26
static const char *kCFG_PASSWORD        = "ConfigPassword";
27
static const char *kENC_KEY             = "EncryptionKey";
28 29 30 31 32 33 34

//-----------------------------------------------------------------------------
MicrohardManager::MicrohardManager(QGCApplication* app, QGCToolbox* toolbox)
    : QGCTool(app, toolbox)
{
    connect(&_workTimer, &QTimer::timeout, this, &MicrohardManager::_checkMicrohard);
    _workTimer.setSingleShot(true);
35 36
    connect(&_locTimer, &QTimer::timeout, this, &MicrohardManager::_locTimeout);
    connect(&_remTimer, &QTimer::timeout, this, &MicrohardManager::_remTimeout);
37 38
    QSettings settings;
    settings.beginGroup(kMICROHARD_GROUP);
39 40 41
    _localIPAddr    = settings.value(kLOCAL_IP,       QString("192.168.168.1")).toString();
    _remoteIPAddr   = settings.value(kREMOTE_IP,      QString("192.168.168.2")).toString();
    _netMask        = settings.value(kNET_MASK,       QString("255.255.255.0")).toString();
42
    _configUserName = settings.value(kCFG_USERNAME,   QString("admin")).toString();
43
    _configPassword = settings.value(kCFG_PASSWORD,   QString("admin")).toString();
44
    _encryptionKey  = settings.value(kENC_KEY,        QString("1234567890")).toString();
45 46 47 48 49 50 51 52 53 54 55 56 57
    settings.endGroup();
}

//-----------------------------------------------------------------------------
MicrohardManager::~MicrohardManager()
{
    _close();
}

//-----------------------------------------------------------------------------
void
MicrohardManager::_close()
{
58 59 60
    _workTimer.stop();
    _locTimer.stop();
    _remTimer.stop();
61 62 63 64 65 66 67 68 69
    if(_mhSettingsLoc) {
        _mhSettingsLoc->close();
        _mhSettingsLoc->deleteLater();
        _mhSettingsLoc = nullptr;
    }
    if(_mhSettingsRem) {
        _mhSettingsRem->close();
        _mhSettingsRem->deleteLater();
        _mhSettingsRem = nullptr;
70 71 72 73 74 75 76 77
    }
}

//-----------------------------------------------------------------------------
void
MicrohardManager::_reset()
{
    _close();
78
    _connectedStatus = 0;
79
    emit connectedChanged();
80
    _linkConnectedStatus = 0;
81 82 83
    emit linkConnectedChanged();
    if(!_appSettings) {
        _appSettings = _toolbox->settingsManager()->appSettings();
84
        connect(_appSettings->enableMicrohard(), &Fact::rawValueChanged, this, &MicrohardManager::_setEnabled);
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    }
    _setEnabled();
}

//-----------------------------------------------------------------------------
FactMetaData*
MicrohardManager::_createMetadata(const char* name, QStringList enums)
{
    FactMetaData* metaData = new FactMetaData(FactMetaData::valueTypeUint32, name, this);
    QQmlEngine::setObjectOwnership(metaData, QQmlEngine::CppOwnership);
    metaData->setShortDescription(name);
    metaData->setLongDescription(name);
    metaData->setRawDefaultValue(QVariant(0));
    metaData->setHasControl(true);
    metaData->setReadOnly(false);
    for(int i = 0; i < enums.size(); i++) {
        metaData->addEnumInfo(enums[i], QVariant(i));
    }
    metaData->setRawMin(0);
    metaData->setRawMin(enums.size() - 1);
    return metaData;
}

//-----------------------------------------------------------------------------
void
MicrohardManager::setToolbox(QGCToolbox* toolbox)
{
    QGCTool::setToolbox(toolbox);
    //-- Start it all
    _reset();
}

Gus Grubba's avatar
Gus Grubba committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
//-----------------------------------------------------------------------------
void
MicrohardManager::switchToConnectionEncryptionKey(QString encryptionKey)
{
    _communicationEncryptionKey = encryptionKey;
    _useCommunicationEncryptionKey = true;
}

//-----------------------------------------------------------------------------
void
MicrohardManager::switchToPairingEncryptionKey()
{
    _useCommunicationEncryptionKey = false;
}

//-----------------------------------------------------------------------------
void
MicrohardManager::setEncryptionKey()
{
    if (_mhSettingsLoc) {
        _mhSettingsLoc->setEncryptionKey(_useCommunicationEncryptionKey ? _communicationEncryptionKey : _encryptionKey);
    }
}

//-----------------------------------------------------------------------------
void
MicrohardManager::updateSettings()
{
    setEncryptionKey();
    QSettings settings;
    settings.beginGroup(kMICROHARD_GROUP);
    settings.setValue(kLOCAL_IP, _localIPAddr);
    settings.setValue(kREMOTE_IP, _remoteIPAddr);
    settings.setValue(kNET_MASK, _netMask);
    settings.setValue(kCFG_PASSWORD, _configPassword);
    settings.setValue(kENC_KEY, _encryptionKey);
    settings.endGroup();

    _reset();
}

158 159
//-----------------------------------------------------------------------------
bool
160
MicrohardManager::setIPSettings(QString localIP_, QString remoteIP_, QString netMask_, QString cfgUserName_, QString cfgPassword_, QString encryptionKey_)
161
{
162
    if (_localIPAddr != localIP_ || _remoteIPAddr != remoteIP_ || _netMask != netMask_ ||
163
        _configUserName != cfgUserName_ || _configPassword != cfgPassword_ || _encryptionKey != encryptionKey_)
164
    {
165 166 167
        _localIPAddr    = localIP_;
        _remoteIPAddr   = remoteIP_;
        _netMask        = netMask_;
168
        _configUserName = cfgUserName_;
169
        _configPassword = cfgPassword_;
170
        _encryptionKey  = encryptionKey_;
171

Gus Grubba's avatar
Gus Grubba committed
172
        updateSettings();
173 174

        return true;
175
    }
176 177

    return false;
178 179 180 181 182 183 184 185
}

//-----------------------------------------------------------------------------
void
MicrohardManager::_setEnabled()
{
    bool enable = _appSettings->enableMicrohard()->rawValue().toBool();
    if(enable) {
186
        if(!_mhSettingsLoc) {
187
            _mhSettingsLoc = new MicrohardSettings(localIPAddr(), this, true);
188 189 190 191 192 193 194
            connect(_mhSettingsLoc, &MicrohardSettings::connected,      this, &MicrohardManager::_connectedLoc);
            connect(_mhSettingsLoc, &MicrohardSettings::rssiUpdated,    this, &MicrohardManager::_rssiUpdatedLoc);
        }
        if(!_mhSettingsRem) {
            _mhSettingsRem = new MicrohardSettings(remoteIPAddr(), this);
            connect(_mhSettingsRem, &MicrohardSettings::connected,      this, &MicrohardManager::_connectedRem);
            connect(_mhSettingsRem, &MicrohardSettings::rssiUpdated,    this, &MicrohardManager::_rssiUpdatedRem);
195
        }
196
        _workTimer.start(SHORT_TIMEOUT);
197 198 199 200 201 202 203 204 205
    } else {
        //-- Stop everything
        _close();
    }
    _enabled = enable;
}

//-----------------------------------------------------------------------------
void
206
MicrohardManager::_connectedLoc(int status)
207
{
208 209 210 211 212 213 214
    static const char* msg = "GND Microhard Settings: ";
    if(status > 0)
        qCDebug(MicrohardLog) << msg << "Connected";
    else if(status < 0)
        qCDebug(MicrohardLog) << msg << "Error";
    else
        qCDebug(MicrohardLog) << msg << "Not Connected";
215
    _connectedStatus = status;
216
    _locTimer.start(LONG_TIMEOUT);
217 218 219 220 221
    emit connectedChanged();
}

//-----------------------------------------------------------------------------
void
222
MicrohardManager::_connectedRem(int status)
223
{
224 225 226 227 228 229 230
    static const char* msg = "AIR Microhard Settings: ";
    if(status > 0)
        qCDebug(MicrohardLog) << msg << "Connected";
    else if(status < 0)
        qCDebug(MicrohardLog) << msg << "Error";
    else
        qCDebug(MicrohardLog) << msg << "Not Connected";
231
    _linkConnectedStatus = status;
232
    _remTimer.start(LONG_TIMEOUT);
233 234 235 236 237
    emit linkConnectedChanged();
}

//-----------------------------------------------------------------------------
void
238
MicrohardManager::_rssiUpdatedLoc(int rssi)
239
{
240 241 242
    _downlinkRSSI = rssi;
    _locTimer.stop();
    _locTimer.start(LONG_TIMEOUT);
243
    emit connectedChanged();
244 245 246 247 248 249 250 251 252 253
    emit linkChanged();
}

//-----------------------------------------------------------------------------
void
MicrohardManager::_rssiUpdatedRem(int rssi)
{
    _uplinkRSSI = rssi;
    _remTimer.stop();
    _remTimer.start(LONG_TIMEOUT);
254
    emit linkConnectedChanged();
255 256 257 258 259 260 261 262
    emit linkChanged();
}

//-----------------------------------------------------------------------------
void
MicrohardManager::_locTimeout()
{
    _locTimer.stop();
263
    _connectedStatus = 0;
264 265 266 267
    if(_mhSettingsLoc) {
        _mhSettingsLoc->close();
        _mhSettingsLoc->deleteLater();
        _mhSettingsLoc = nullptr;
268
    }
269
    emit connectedChanged();
270 271 272 273
}

//-----------------------------------------------------------------------------
void
274
MicrohardManager::_remTimeout()
275
{
276
    _remTimer.stop();
277
    _linkConnectedStatus = 0;
278 279 280 281
    if(_mhSettingsRem) {
        _mhSettingsRem->close();
        _mhSettingsRem->deleteLater();
        _mhSettingsRem = nullptr;
282
    }
283 284 285 286 287 288 289 290 291 292 293
    emit linkConnectedChanged();
}

//-----------------------------------------------------------------------------
void
MicrohardManager::_checkMicrohard()
{
    if(_enabled) {
        if(!_mhSettingsLoc || !_mhSettingsRem) {
            _setEnabled();
            return;
294
        }
295

296
        if(_connectedStatus <= 0) {
297 298 299
            _mhSettingsLoc->start();
        } else {
            _mhSettingsLoc->getStatus();
300
        }
301
        if(_linkConnectedStatus <= 0) {
302 303 304
            _mhSettingsRem->start();
        } else {
            _mhSettingsRem->getStatus();
305 306
        }
    }
307
    _workTimer.start(_connectedStatus > 0 ? SHORT_TIMEOUT : LONG_TIMEOUT);
308
}