TaisyncSettings.cc 5.42 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (c) 2009-2016 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.
 *
 ****************************************************************************/

10
#include "TaisyncManager.h"
11 12 13 14 15
#include "TaisyncSettings.h"
#include "SettingsManager.h"
#include "QGCApplication.h"
#include "VideoManager.h"

16 17 18 19 20 21
static const char* kPostReq =
    "POST %1 HTTP/1.1\r\n"
    "Content-Type: application/json\r\n"
    "Content-Length: %2\r\n\r\n"
    "%3";

22 23
static const char* kGetReq      = "GET %1 HTTP/1.1\r\n\r\n";
static const char* kRadioURI    = "/v1/radio.json";
Gus Grubba's avatar
Gus Grubba committed
24
static const char* kVideoURI    = "/v1/video.json";
Gus Grubba's avatar
Gus Grubba committed
25 26
static const char* kRTSPURI     = "/v1/rtspuri.json";
static const char* kIPAddrURI   = "/v1/ipaddr.json";
27

28 29 30 31 32 33 34
//-----------------------------------------------------------------------------
TaisyncSettings::TaisyncSettings(QObject* parent)
    : TaisyncHandler(parent)
{
}

//-----------------------------------------------------------------------------
35
bool TaisyncSettings::start()
36
{
37 38 39 40
    qCDebug(TaisyncLog) << "Start Taisync Settings";
#if defined(__ios__) || defined(__android__)
    return _start(TAISYNC_SETTINGS_PORT);
#else
Gus Grubba's avatar
Gus Grubba committed
41
    return _start(TAISYNC_SETTINGS_PORT, QHostAddress(qgcApp()->toolbox()->taisyncManager()->remoteIPAddr()));
42
#endif
43 44
}

45 46
//-----------------------------------------------------------------------------
bool
47
TaisyncSettings::requestLinkStatus()
48
{
Gus Grubba's avatar
Gus Grubba committed
49
    return _request("/v1/baseband.json");
50 51 52 53 54 55
}

//-----------------------------------------------------------------------------
bool
TaisyncSettings::requestDevInfo()
{
Gus Grubba's avatar
Gus Grubba committed
56
    return _request("/v1/device.json");
57 58 59 60 61 62
}

//-----------------------------------------------------------------------------
bool
TaisyncSettings::requestFreqScan()
{
Gus Grubba's avatar
Gus Grubba committed
63
    return _request("/v1/freqscan.json");
64 65
}

66 67 68
//-----------------------------------------------------------------------------
bool
TaisyncSettings::requestVideoSettings()
Gus Grubba's avatar
Gus Grubba committed
69
{
Gus Grubba's avatar
Gus Grubba committed
70
    return _request(kVideoURI);
Gus Grubba's avatar
Gus Grubba committed
71 72 73 74 75 76
}

//-----------------------------------------------------------------------------
bool
TaisyncSettings::requestRadioSettings()
{
77
    return _request(kRadioURI);
Gus Grubba's avatar
Gus Grubba committed
78 79
}

Gus Grubba's avatar
Gus Grubba committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93
//-----------------------------------------------------------------------------
bool
TaisyncSettings::requestIPSettings()
{
    return _request(kIPAddrURI);
}

//-----------------------------------------------------------------------------
bool
TaisyncSettings::requestRTSPURISettings()
{
    return _request(kRTSPURI);
}

Gus Grubba's avatar
Gus Grubba committed
94 95 96
//-----------------------------------------------------------------------------
bool
TaisyncSettings::_request(const QString& request)
97 98
{
    if(_tcpSocket) {
Gus Grubba's avatar
Gus Grubba committed
99
        QString req = QString(kGetReq).arg(request);
100 101 102 103 104 105 106
        //qCDebug(TaisyncVerbose) << "Request" << req;
        _tcpSocket->write(req.toUtf8());
        return true;
    }
    return false;
}

107 108 109 110 111 112
//-----------------------------------------------------------------------------
bool
TaisyncSettings::_post(const QString& post, const QString &postPayload)
{
    if(_tcpSocket) {
        QString req = QString(kPostReq).arg(post).arg(postPayload.size()).arg(postPayload);
Gus Grubba's avatar
Gus Grubba committed
113
        qCDebug(TaisyncVerbose) << "Post" << req;
114 115 116 117 118 119 120 121 122 123 124
        _tcpSocket->write(req.toUtf8());
        return true;
    }
    return false;
}

//-----------------------------------------------------------------------------
bool
TaisyncSettings::setRadioSettings(const QString& mode, const QString& channel)
{
    static const char* kRadioPost = "{\"mode\":\"%1\",\"freq\":\"%2\"}";
Gus Grubba's avatar
Gus Grubba committed
125
    QString post = QString(kRadioPost).arg(mode).arg(channel);
126 127 128
    return _post(kRadioURI, post);
}

Gus Grubba's avatar
Gus Grubba committed
129 130 131 132 133 134 135 136 137
//-----------------------------------------------------------------------------
bool
TaisyncSettings::setVideoSettings(const QString& output, const QString& mode, const QString& rate)
{
    static const char* kVideoPost = "{\"decode\":\"%1\",\"mode\":\"%2\",\"maxbitrate\":\"%3\"}";
    QString post = QString(kVideoPost).arg(output).arg(mode).arg(rate);
    return _post(kVideoURI, post);
}

Gus Grubba's avatar
Gus Grubba committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
//-----------------------------------------------------------------------------
bool
TaisyncSettings::setRTSPSettings(const QString& uri, const QString& account, const QString& password)
{
    static const char* kRTSPPost = "{\"rtspURI\":\"%1\",\"account\":\"%2\",\"passwd\":\"%3\"}";
    QString post = QString(kRTSPPost).arg(uri).arg(account).arg(password);
    return _post(kRTSPURI, post);
}

//-----------------------------------------------------------------------------
bool
TaisyncSettings::setIPSettings(const QString& localIP, const QString& remoteIP, const QString& netMask)
{
    static const char* kRTSPPost = "{\"ipaddr\":\"%1\",\"netmask\":\"%2\",\"usbEthIp\":\"%3\"}";
    QString post = QString(kRTSPPost).arg(localIP).arg(netMask).arg(remoteIP);
    return _post(kIPAddrURI, post);
}

156 157 158 159 160
//-----------------------------------------------------------------------------
void
TaisyncSettings::_readBytes()
{
    QByteArray bytesIn = _tcpSocket->read(_tcpSocket->bytesAvailable());
161 162
    //-- Go straight to Json payload
    int idx = bytesIn.indexOf('{');
Gus Grubba's avatar
Gus Grubba committed
163 164 165 166 167 168 169 170 171 172
    //-- We may receive more than one response within one TCP packet.
    while(idx >= 0) {
        bytesIn = bytesIn.mid(idx);
        idx = bytesIn.indexOf('}');
        if(idx > 0) {
            QByteArray data = bytesIn.left(idx + 1);
            emit updateSettings(data);
            bytesIn = bytesIn.mid(idx+1);
            idx = bytesIn.indexOf('{');
        }
173
    }
174 175
}