MicrohardSettings.cc 3.05 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
{
Gus Grubba's avatar
Gus Grubba committed
37
    if (_loggedIn && _tcpSocket) {
38
        _tcpSocket->write("AT+MWSTATUS\n");
39 40 41
    }
}

42 43 44 45
//-----------------------------------------------------------------------------
void
MicrohardSettings::setEncryptionKey(QString key)
{
Gus Grubba's avatar
Gus Grubba committed
46 47 48
    if (!_tcpSocket) {
        return;
    }
49 50
    QString cmd = "AT+MWVENCRYPT=1," + key + "\n";
    _tcpSocket->write(cmd.toStdString().c_str());
Gus Grubba's avatar
Gus Grubba committed
51 52 53 54
    cmd = "AT&W\n";
    _tcpSocket->write(cmd.toStdString().c_str());

    qCDebug(MicrohardLog) << "Set encryption key: " << key;
55 56
}

57 58 59 60
//-----------------------------------------------------------------------------
void
MicrohardSettings::_readBytes()
{
Gus Grubba's avatar
Gus Grubba committed
61 62 63
    if (!_tcpSocket) {
        return;
    }
64
    QByteArray bytesIn = _tcpSocket->read(_tcpSocket->bytesAvailable());
65

66
    //qCDebug(MicrohardLog) << "Read bytes: " << bytesIn;
67

68
    if (_loggedIn) {
69 70 71 72 73 74 75 76 77 78 79
        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;
                }
            }
80
        }
81 82 83
    } else if (bytesIn.contains("login:")) {
        std::string userName = qgcApp()->toolbox()->microhardManager()->configUserName().toStdString() + "\n";
        _tcpSocket->write(userName.c_str());
84 85 86
    } else if (bytesIn.contains("Password:")) {
        std::string pwd = qgcApp()->toolbox()->microhardManager()->configPassword().toStdString() + "\n";
        _tcpSocket->write(pwd.c_str());
87 88 89
    } else if (bytesIn.contains("Login incorrect")) {
        emit connected(-1);
    } else if (bytesIn.contains("Entering")) {
90
        if (!loggedIn() && _setEncryptionKey) {
Gus Grubba's avatar
Gus Grubba committed
91
            qgcApp()->toolbox()->microhardManager()->setEncryptionKey();
92 93
        }
        _loggedIn = true;
94
        emit connected(1);
95
    }
96 97

    emit rssiUpdated(_rssiVal);
98 99
}