MicrohardManager.cc 8.64 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 117 118
    }
    _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
119
MicrohardManager::setIPSettings(QString localIP_, QString remoteIP_, QString netMask_, QString cfgUserName_, QString cfgPassword_, QString encryptionKey_)
120
{
121
    if (_localIPAddr != localIP_ || _remoteIPAddr != remoteIP_ || _netMask != netMask_ ||
122
        _configUserName != cfgUserName_ || _configPassword != cfgPassword_ || _encryptionKey != encryptionKey_)
123 124 125 126 127
    {
        if (_mhSettingsLoc && _encryptionKey != encryptionKey_) {
            _mhSettingsLoc->setEncryptionKey(encryptionKey_);
        }

128 129 130
        _localIPAddr    = localIP_;
        _remoteIPAddr   = remoteIP_;
        _netMask        = netMask_;
131
        _configUserName = cfgUserName_;
132
        _configPassword = cfgPassword_;
133
        _encryptionKey  = encryptionKey_;
134 135 136 137 138 139

        QSettings settings;
        settings.beginGroup(kMICROHARD_GROUP);
        settings.setValue(kLOCAL_IP, localIP_);
        settings.setValue(kREMOTE_IP, remoteIP_);
        settings.setValue(kNET_MASK, netMask_);
140
        settings.setValue(kCFG_USERNAME, cfgUserName_);
141
        settings.setValue(kCFG_PASSWORD, cfgPassword_);
142
        settings.setValue(kENC_KEY, encryptionKey_);
143 144 145 146 147
        settings.endGroup();

        _reset();

        return true;
148
    }
149 150

    return false;
151 152 153 154 155 156 157 158
}

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

//-----------------------------------------------------------------------------
void
179
MicrohardManager::_connectedLoc(int status)
180
{
181
    qCDebug(MicrohardLog) << "GND Microhard Settings Connected";
182
    _connectedStatus = status;
183
    _locTimer.start(LONG_TIMEOUT);
184 185 186 187 188
    emit connectedChanged();
}

//-----------------------------------------------------------------------------
void
189
MicrohardManager::_connectedRem(int status)
190
{
191
    qCDebug(MicrohardLog) << "AIR Microhard Settings Connected";
192
    _linkConnectedStatus = status;
193
    _remTimer.start(LONG_TIMEOUT);
194 195 196 197 198
    emit linkConnectedChanged();
}

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

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

//-----------------------------------------------------------------------------
void
MicrohardManager::_locTimeout()
{
    _locTimer.stop();
224
    _connectedStatus = 0;
225 226 227 228
    if(_mhSettingsLoc) {
        _mhSettingsLoc->close();
        _mhSettingsLoc->deleteLater();
        _mhSettingsLoc = nullptr;
229
    }
230
    emit connectedChanged();
231 232 233 234
}

//-----------------------------------------------------------------------------
void
235
MicrohardManager::_remTimeout()
236
{
237
    _remTimer.stop();
238
    _linkConnectedStatus = 0;
239 240 241 242
    if(_mhSettingsRem) {
        _mhSettingsRem->close();
        _mhSettingsRem->deleteLater();
        _mhSettingsRem = nullptr;
243
    }
244 245 246 247 248 249 250 251 252 253 254
    emit linkConnectedChanged();
}

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

257
        if(_connectedStatus <= 0) {
258 259 260
            _mhSettingsLoc->start();
        } else {
            _mhSettingsLoc->getStatus();
261
        }
262
        if(_linkConnectedStatus <= 0) {
263 264 265
            _mhSettingsRem->start();
        } else {
            _mhSettingsRem->getStatus();
266 267
        }
    }
268
    _workTimer.start(_connectedStatus > 0 ? SHORT_TIMEOUT : LONG_TIMEOUT);
269
}