MicrohardSettings.cc 3.06 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 31
    _start(MICROHARD_SETTINGS_PORT, QHostAddress(_address));
    return true;
32 33 34
}

//-----------------------------------------------------------------------------
35 36
void
MicrohardSettings::getStatus()
37
{
Gus Grubba's avatar
Gus Grubba committed
38
    if (_loggedIn && _tcpSocket) {
39
        _tcpSocket->write("AT+MWSTATUS\n");
40 41 42
    }
}

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

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

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

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

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

    emit rssiUpdated(_rssiVal);
99 100
}