MicrohardSettings.cc 2.69 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 "MicrohardSettings.h"
11
#include "MicrohardManager.h"
12 13 14 15 16
#include "SettingsManager.h"
#include "QGCApplication.h"
#include "VideoManager.h"

//-----------------------------------------------------------------------------
17
MicrohardSettings::MicrohardSettings(QString address_, QObject* parent, bool setEncryptionKey)
18 19
    : MicrohardHandler(parent)
{
20 21
    _address = address_;
    _setEncryptionKey = setEncryptionKey;
22 23 24
}

//-----------------------------------------------------------------------------
25 26
bool
MicrohardSettings::start()
27 28
{
    qCDebug(MicrohardLog) << "Start Microhard Settings";
29
    _loggedIn = false;
30
    return _start(MICROHARD_SETTINGS_PORT, QHostAddress(_address));
31 32 33
}

//-----------------------------------------------------------------------------
34 35
void
MicrohardSettings::getStatus()
36
{
37
    if (_loggedIn) {
38
        _tcpSocket->write("AT+MWSTATUS\n");
39 40 41
    }
}

42 43 44 45 46 47 48 49 50
//-----------------------------------------------------------------------------
void
MicrohardSettings::setEncryptionKey(QString key)
{
    QString cmd = "AT+MWVENCRYPT=1," + key + "\n";
    _tcpSocket->write(cmd.toStdString().c_str());
    qCDebug(MicrohardLog) << "setEncryptionKey: " << cmd;
}

51 52 53 54 55
//-----------------------------------------------------------------------------
void
MicrohardSettings::_readBytes()
{
    QByteArray bytesIn = _tcpSocket->read(_tcpSocket->bytesAvailable());
56

57
//    qCDebug(MicrohardVerbose) << "Read bytes: " << bytesIn;
58

59
    if (_loggedIn) {
60 61 62 63 64 65 66 67 68 69 70
        int i1 = bytesIn.indexOf("RSSI (dBm)");
        if (i1 > 0) {
            int i2 = bytesIn.indexOf(": ", i1);
            if (i2 > 0) {
                i2 += 2;
                int i3 = bytesIn.indexOf(" ", i2);
                int val = bytesIn.mid(i2, i3 - i2).toInt();
                if (val < 0) {
                    _rssiVal = val;
                }
            }
71
        }
72 73 74 75 76 77
    } else if (bytesIn.contains("UserDevice login:")) {
        _tcpSocket->write("admin\n");
    } else if (bytesIn.contains("Password:")) {
        std::string pwd = qgcApp()->toolbox()->microhardManager()->configPassword().toStdString() + "\n";
        _tcpSocket->write(pwd.c_str());
    }  else if (bytesIn.contains("UserDevice>")) {
78 79 80 81
        if (!loggedIn() && _setEncryptionKey) {
            setEncryptionKey(qgcApp()->toolbox()->microhardManager()->encryptionKey());
        }
        _loggedIn = true;
82
    }
83 84

    emit rssiUpdated(_rssiVal);
85 86
}