MicrohardManager.cc 8.29 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_PASSWORD        = "ConfigPassword";
26
static const char *kENC_KEY             = "EncryptionKey";
27 28 29 30 31 32 33

//-----------------------------------------------------------------------------
MicrohardManager::MicrohardManager(QGCApplication* app, QGCToolbox* toolbox)
    : QGCTool(app, toolbox)
{
    connect(&_workTimer, &QTimer::timeout, this, &MicrohardManager::_checkMicrohard);
    _workTimer.setSingleShot(true);
34 35
    connect(&_locTimer, &QTimer::timeout, this, &MicrohardManager::_locTimeout);
    connect(&_remTimer, &QTimer::timeout, this, &MicrohardManager::_remTimeout);
36 37
    QSettings settings;
    settings.beginGroup(kMICROHARD_GROUP);
38 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();
    _configPassword = settings.value(kCFG_PASSWORD,   QString("admin")).toString();
42
    _encryptionKey  = settings.value(kENC_KEY,        QString("1234567890")).toString();
43 44 45 46 47 48 49 50 51 52 53 54 55
    settings.endGroup();
}

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

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

//-----------------------------------------------------------------------------
void
MicrohardManager::_reset()
{
    _close();
    _isConnected = false;
    emit connectedChanged();
    _linkConnected = false;
    emit linkConnectedChanged();
    if(!_appSettings) {
        _appSettings = _toolbox->settingsManager()->appSettings();
82
        connect(_appSettings->enableMicrohard(), &Fact::rawValueChanged, this, &MicrohardManager::_setEnabled);
83 84 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();
}

//-----------------------------------------------------------------------------
bool
117
MicrohardManager::setIPSettings(QString localIP_, QString remoteIP_, QString netMask_, QString cfgPassword_, QString encryptionKey_)
118
{
119
    if (_localIPAddr != localIP_ || _remoteIPAddr != remoteIP_ || _netMask != netMask_ ||
120
        _configPassword != cfgPassword_ || _encryptionKey != encryptionKey_)
121 122 123 124 125
    {
        if (_mhSettingsLoc && _encryptionKey != encryptionKey_) {
            _mhSettingsLoc->setEncryptionKey(encryptionKey_);
        }

126 127 128 129
        _localIPAddr    = localIP_;
        _remoteIPAddr   = remoteIP_;
        _netMask        = netMask_;
        _configPassword = cfgPassword_;
130
        _encryptionKey  = encryptionKey_;
131 132 133 134 135 136 137

        QSettings settings;
        settings.beginGroup(kMICROHARD_GROUP);
        settings.setValue(kLOCAL_IP, localIP_);
        settings.setValue(kREMOTE_IP, remoteIP_);
        settings.setValue(kNET_MASK, netMask_);
        settings.setValue(kCFG_PASSWORD, cfgPassword_);
138
        settings.setValue(kENC_KEY, encryptionKey_);
139 140 141 142 143
        settings.endGroup();

        _reset();

        return true;
144
    }
145 146

    return false;
147 148 149 150 151 152 153 154
}

//-----------------------------------------------------------------------------
void
MicrohardManager::_setEnabled()
{
    bool enable = _appSettings->enableMicrohard()->rawValue().toBool();
    if(enable) {
155
        if(!_mhSettingsLoc) {
156
            _mhSettingsLoc = new MicrohardSettings(localIPAddr(), this, true);
157 158 159 160 161 162 163
            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);
164
        }
165
        _workTimer.start(SHORT_TIMEOUT);
166 167 168 169 170 171 172 173 174
    } else {
        //-- Stop everything
        _close();
    }
    _enabled = enable;
}

//-----------------------------------------------------------------------------
void
175
MicrohardManager::_connectedLoc()
176
{
177
    qCDebug(MicrohardLog) << "GND Microhard Settings Connected";
178
    _isConnected = true;
179
    _locTimer.start(LONG_TIMEOUT);
180 181 182 183 184
    emit connectedChanged();
}

//-----------------------------------------------------------------------------
void
185
MicrohardManager::_connectedRem()
186
{
187 188 189
    qCDebug(MicrohardLog) << "AIR Microhard Settings Connected";
    _linkConnected = true;
    _remTimer.start(LONG_TIMEOUT);
190 191 192 193 194
    emit linkConnectedChanged();
}

//-----------------------------------------------------------------------------
void
195
MicrohardManager::_rssiUpdatedLoc(int rssi)
196
{
197 198 199
    _downlinkRSSI = rssi;
    _locTimer.stop();
    _locTimer.start(LONG_TIMEOUT);
200
    emit connectedChanged();
201 202 203 204 205 206 207 208 209 210
    emit linkChanged();
}

//-----------------------------------------------------------------------------
void
MicrohardManager::_rssiUpdatedRem(int rssi)
{
    _uplinkRSSI = rssi;
    _remTimer.stop();
    _remTimer.start(LONG_TIMEOUT);
211
    emit linkConnectedChanged();
212 213 214 215 216 217 218 219 220 221 222 223 224
    emit linkChanged();
}

//-----------------------------------------------------------------------------
void
MicrohardManager::_locTimeout()
{
    _locTimer.stop();
    _isConnected = false;
    if(_mhSettingsLoc) {
        _mhSettingsLoc->close();
        _mhSettingsLoc->deleteLater();
        _mhSettingsLoc = nullptr;
225
    }
226
    emit connectedChanged();
227 228 229 230
}

//-----------------------------------------------------------------------------
void
231
MicrohardManager::_remTimeout()
232
{
233 234 235 236 237 238
    _remTimer.stop();
    _linkConnected = false;
    if(_mhSettingsRem) {
        _mhSettingsRem->close();
        _mhSettingsRem->deleteLater();
        _mhSettingsRem = nullptr;
239
    }
240 241 242 243 244 245 246 247 248 249 250
    emit linkConnectedChanged();
}

//-----------------------------------------------------------------------------
void
MicrohardManager::_checkMicrohard()
{
    if(_enabled) {
        if(!_mhSettingsLoc || !_mhSettingsRem) {
            _setEnabled();
            return;
251
        }
252 253 254 255 256

        if(!_isConnected) {
            _mhSettingsLoc->start();
        } else {
            _mhSettingsLoc->getStatus();
257
        }
258 259 260 261
        if(!_linkConnected) {
            _mhSettingsRem->start();
        } else {
            _mhSettingsRem->getStatus();
262 263
        }
    }
264
    _workTimer.start(_isConnected ? SHORT_TIMEOUT : LONG_TIMEOUT);
265
}