MicrohardManager.cc 8.78 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 LONG_TIMEOUT 5000

20 21 22
static const char *kMICROHARD_GROUP     = "Microhard";
static const char *kLOCAL_IP            = "LocalIP";
static const char *kREMOTE_IP           = "RemoteIP";
23 24
static const char *kGROUND_IP           = "GroundIP";
static const char *kAIR_IP              = "AirIP";
25
static const char *kNET_MASK            = "NetMask";
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
    _localIPAddr    = settings.value(kLOCAL_IP,       QString("192.168.168.1")).toString();
    _remoteIPAddr   = settings.value(kREMOTE_IP,      QString("192.168.168.2")).toString();
41 42
    _groundIPAddr   = settings.value(kGROUND_IP,      QString("192.168.168.101")).toString();
    _airIPAddr      = settings.value(kAIR_IP,         QString("192.168.168.213")).toString();
43 44
    _netMask        = settings.value(kNET_MASK,       QString("255.255.255.0")).toString();
    _configPassword = settings.value(kCFG_PASSWORD,   QString("admin")).toString();
45
    _encryptionKey  = settings.value(kENC_KEY,        QString("1234567890")).toString();
46 47 48 49 50 51 52 53 54 55 56 57 58
    settings.endGroup();
}

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

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

//-----------------------------------------------------------------------------
void
MicrohardManager::_reset()
{
    _close();
    _isConnected = false;
    emit connectedChanged();
    _linkConnected = false;
    emit linkConnectedChanged();
    if(!_appSettings) {
        _appSettings = _toolbox->settingsManager()->appSettings();
85
        connect(_appSettings->enableMicrohard(), &Fact::rawValueChanged, this, &MicrohardManager::_setEnabled);
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 119
    }
    _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
120
MicrohardManager::setIPSettings(QString localIP_, QString remoteIP_, QString groundIP_, QString airIP_, QString netMask_, QString cfgPassword_, QString encryptionKey_)
121
{
122
    if (_localIPAddr != localIP_ || _remoteIPAddr != remoteIP_ || _netMask != netMask_ ||
123
        _configPassword != cfgPassword_ || _encryptionKey != encryptionKey_ || _groundIPAddr != groundIP_ || _airIPAddr != airIP_)
124 125 126 127 128
    {
        if (_mhSettingsLoc && _encryptionKey != encryptionKey_) {
            _mhSettingsLoc->setEncryptionKey(encryptionKey_);
        }

129 130
        _localIPAddr    = localIP_;
        _remoteIPAddr   = remoteIP_;
131 132
        _groundIPAddr   = groundIP_;
        _airIPAddr      = airIP_;
133 134
        _netMask        = netMask_;
        _configPassword = cfgPassword_;
135
        _encryptionKey  = encryptionKey_;
136 137 138 139 140

        QSettings settings;
        settings.beginGroup(kMICROHARD_GROUP);
        settings.setValue(kLOCAL_IP, localIP_);
        settings.setValue(kREMOTE_IP, remoteIP_);
141 142
        settings.setValue(kGROUND_IP, groundIP_);
        settings.setValue(kAIR_IP, airIP_);
143 144
        settings.setValue(kNET_MASK, netMask_);
        settings.setValue(kCFG_PASSWORD, cfgPassword_);
145
        settings.setValue(kENC_KEY, encryptionKey_);
146 147 148 149 150
        settings.endGroup();

        _reset();

        return true;
151
    }
152 153

    return false;
154 155 156 157 158 159 160 161
}

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

//-----------------------------------------------------------------------------
void
182
MicrohardManager::_connectedLoc()
183
{
184
    qCDebug(MicrohardLog) << "GND Microhard Settings Connected";
185
    _isConnected = true;
186
    _locTimer.start(LONG_TIMEOUT);
187 188 189 190 191
    emit connectedChanged();
}

//-----------------------------------------------------------------------------
void
192
MicrohardManager::_connectedRem()
193
{
194 195 196
    qCDebug(MicrohardLog) << "AIR Microhard Settings Connected";
    _linkConnected = true;
    _remTimer.start(LONG_TIMEOUT);
197 198 199 200 201
    emit linkConnectedChanged();
}

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

//-----------------------------------------------------------------------------
void
MicrohardManager::_rssiUpdatedRem(int rssi)
{
    _uplinkRSSI = rssi;
    _remTimer.stop();
    _remTimer.start(LONG_TIMEOUT);
218
    emit linkConnectedChanged();
219 220 221 222 223 224 225 226 227 228 229 230 231
    emit linkChanged();
}

//-----------------------------------------------------------------------------
void
MicrohardManager::_locTimeout()
{
    _locTimer.stop();
    _isConnected = false;
    if(_mhSettingsLoc) {
        _mhSettingsLoc->close();
        _mhSettingsLoc->deleteLater();
        _mhSettingsLoc = nullptr;
232
    }
233
    emit connectedChanged();
234 235 236 237
}

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

//-----------------------------------------------------------------------------
void
MicrohardManager::_checkMicrohard()
{
    if(_enabled) {
        if(!_mhSettingsLoc || !_mhSettingsRem) {
            _setEnabled();
            return;
258
        }
259 260 261 262 263

        if(!_isConnected) {
            _mhSettingsLoc->start();
        } else {
            _mhSettingsLoc->getStatus();
264
        }
265 266 267 268
        if(!_linkConnected) {
            _mhSettingsRem->start();
        } else {
            _mhSettingsRem->getStatus();
269 270
        }
    }
271
    _workTimer.start(_isConnected ? 1000 : LONG_TIMEOUT);
272
}