MicrohardSettings.cc 3.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
/****************************************************************************
 *
 *   (c) 2019 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#include "MicrohardSettings.h"
#include "MicrohardManager.h"
#include "SettingsManager.h"
#include "QGCApplication.h"
#include "VideoManager.h"

//-----------------------------------------------------------------------------
MicrohardSettings::MicrohardSettings(QString address_, QObject* parent, bool setEncryptionKey)
    : MicrohardHandler(parent)
{
    _address = address_;
    _setEncryptionKey = setEncryptionKey;
}

//-----------------------------------------------------------------------------
bool
MicrohardSettings::start()
{
    qCDebug(MicrohardLog) << "Start Microhard Settings";
    _loggedIn = false;
    _start(MICROHARD_SETTINGS_PORT, QHostAddress(_address));
    return true;
}

//-----------------------------------------------------------------------------
void
MicrohardSettings::getStatus()
{
    if (_loggedIn && _tcpSocket) {
        _tcpSocket->write("AT+MWSTATUS\n");
    }
}

//-----------------------------------------------------------------------------
void
MicrohardSettings::setEncryptionKey(QString key)
{
    if (!_tcpSocket) {
        return;
    }
    QString cmd = "AT+MWVENCRYPT=1," + key + "\n";
    _tcpSocket->write(cmd.toStdString().c_str());
    cmd = "AT&W\n";
    _tcpSocket->write(cmd.toStdString().c_str());

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

//-----------------------------------------------------------------------------
void
MicrohardSettings::_readBytes()
{
    if (!_tcpSocket) {
        return;
    }
    QByteArray bytesIn = _tcpSocket->read(_tcpSocket->bytesAvailable());

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

    if (_loggedIn) {
        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;
                }
            }
        }
    } else if (bytesIn.contains("login:")) {
        std::string userName = qgcApp()->toolbox()->microhardManager()->configUserName().toStdString() + "\n";
        _tcpSocket->write(userName.c_str());
    } else if (bytesIn.contains("Password:")) {
        std::string pwd = qgcApp()->toolbox()->microhardManager()->configPassword().toStdString() + "\n";
        _tcpSocket->write(pwd.c_str());
    } else if (bytesIn.contains("Login incorrect")) {
        emit connected(-1);
    } else if (bytesIn.contains("Entering")) {
        if (!loggedIn() && _setEncryptionKey) {
            qgcApp()->toolbox()->microhardManager()->setEncryptionKey();
        }
        _loggedIn = true;
        emit connected(1);
    }

    emit rssiUpdated(_rssiVal);
}