TCPLink.cc 6.86 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
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
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009 - 2011 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 
 This file is part of the QGROUNDCONTROL project
 
 QGROUNDCONTROL is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 QGROUNDCONTROL is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
 
 ======================================================================*/

#include <QTimer>
#include <QList>
#include <QDebug>
#include <QMutexLocker>
#include <iostream>
#include "TCPLink.h"
#include "LinkManager.h"
#include "QGC.h"
#include <QHostInfo>
33
#include <QSignalSpy>
Don Gagne's avatar
Don Gagne committed
34

35 36 37 38
/// @file
///     @brief TCP link type for SITL support
///
///     @author Don Gagne <don@thegagnes.com>
Don Gagne's avatar
Don Gagne committed
39

40 41 42 43 44
TCPLink::TCPLink(QHostAddress hostAddress, quint16 socketPort) :
    _hostAddress(hostAddress),
    _port(socketPort),
    _socket(NULL),
    _socketIsConnected(false)
Don Gagne's avatar
Don Gagne committed
45
{
46 47 48 49
    // We're doing it wrong - because the Qt folks got the API wrong:
    // http://blog.qt.digia.com/blog/2010/06/17/youre-doing-it-wrong/
    moveToThread(this);

50 51
    _linkId = getNextLinkId();
    _resetName();
Don Gagne's avatar
Don Gagne committed
52
    
53
    qDebug() << "TCP Created " << _name;
Don Gagne's avatar
Don Gagne committed
54 55 56 57 58
}

TCPLink::~TCPLink()
{
    disconnect();
59
	deleteLater();
Don Gagne's avatar
Don Gagne committed
60 61 62 63 64 65 66
}

void TCPLink::run()
{
	exec();
}

67
void TCPLink::setHostAddress(QHostAddress hostAddress)
Don Gagne's avatar
Don Gagne committed
68
{
69 70 71
    bool reconnect = false;
    
	if (this->isConnected()) {
Don Gagne's avatar
Don Gagne committed
72 73 74
		disconnect();
		reconnect = true;
	}
75 76 77 78 79
    
	_hostAddress = hostAddress;
    _resetName();
    
	if (reconnect) {
Don Gagne's avatar
Don Gagne committed
80 81 82 83
		connect();
	}
}

84 85 86 87 88
void TCPLink::setHostAddress(const QString& hostAddress)
{
    setHostAddress(QHostAddress(hostAddress));
}

Don Gagne's avatar
Don Gagne committed
89 90
void TCPLink::setPort(int port)
{
91 92 93
    bool reconnect = false;
    
	if (this->isConnected()) {
Don Gagne's avatar
Don Gagne committed
94 95 96
		disconnect();
		reconnect = true;
	}
97 98 99 100 101
    
	_port = port;
    _resetName();
    
	if (reconnect) {
Don Gagne's avatar
Don Gagne committed
102 103 104 105
		connect();
	}
}

Don Gagne's avatar
Don Gagne committed
106
#ifdef TCPLINK_READWRITE_DEBUG
107
void TCPLink::_writeDebugBytes(const char *data, qint16 size)
Don Gagne's avatar
Don Gagne committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
{
    QString bytes;
    QString ascii;
    for (int i=0; i<size; i++)
    {
        unsigned char v = data[i];
        bytes.append(QString().sprintf("%02x ", v));
        if (data[i] > 31 && data[i] < 127)
        {
            ascii.append(data[i]);
        }
        else
        {
            ascii.append(219);
        }
    }
124
    qDebug() << "Sent" << size << "bytes to" << _hostAddress.toString() << ":" << _port << "data:";
Don Gagne's avatar
Don Gagne committed
125 126
    qDebug() << bytes;
    qDebug() << "ASCII:" << ascii;
Don Gagne's avatar
Don Gagne committed
127 128 129 130 131 132
}
#endif

void TCPLink::writeBytes(const char* data, qint64 size)
{
#ifdef TCPLINK_READWRITE_DEBUG
133
    _writeDebugBytes(data, size);
Don Gagne's avatar
Don Gagne committed
134
#endif
135
    _socket->write(data, size);
136 137 138 139

    // Log the amount and time written out for future data rate calculations.
    QMutexLocker dataRateLocker(&dataRateMutex);
    logDataRateToBuffer(outDataWriteAmounts, outDataWriteTimes, &outDataIndex, size, QDateTime::currentMSecsSinceEpoch());
Don Gagne's avatar
Don Gagne committed
140 141 142 143 144 145 146 147 148 149
}

/**
 * @brief Read a number of bytes from the interface.
 *
 * @param data Pointer to the data byte array to write the bytes to
 * @param maxLength The maximum number of bytes to write
 **/
void TCPLink::readBytes()
{
150
    qint64 byteCount = _socket->bytesAvailable();
Don Gagne's avatar
Don Gagne committed
151 152 153 154 155 156
    
    if (byteCount)
    {
        QByteArray buffer;
        buffer.resize(byteCount);
        
157
        _socket->read(buffer.data(), buffer.size());
Don Gagne's avatar
Don Gagne committed
158 159
        
        emit bytesReceived(this, buffer);
Don Gagne's avatar
Don Gagne committed
160

161 162 163 164
        // Log the amount and time received for future data rate calculations.
        QMutexLocker dataRateLocker(&dataRateMutex);
        logDataRateToBuffer(inDataWriteAmounts, inDataWriteTimes, &inDataIndex, byteCount, QDateTime::currentMSecsSinceEpoch());

Don Gagne's avatar
Don Gagne committed
165 166 167
#ifdef TCPLINK_READWRITE_DEBUG
        writeDebugBytes(buffer.data(), buffer.size());
#endif
Don Gagne's avatar
Don Gagne committed
168 169 170 171 172 173 174 175 176 177
    }
}

/**
 * @brief Get the number of bytes to read.
 *
 * @return The number of bytes to read
 **/
qint64 TCPLink::bytesAvailable()
{
178
    return _socket->bytesAvailable();
Don Gagne's avatar
Don Gagne committed
179 180 181 182 183 184 185 186 187
}

/**
 * @brief Disconnect the connection.
 *
 * @return True if connection has been disconnected, false if connection couldn't be disconnected.
 **/
bool TCPLink::disconnect()
{
188 189
	quit();
	wait();
Don Gagne's avatar
Don Gagne committed
190
    
191
    if (_socket)
Don Gagne's avatar
Don Gagne committed
192
	{
193 194 195 196 197 198 199
        _socket->disconnectFromHost();
        _socketIsConnected = false;
		delete _socket;
		_socket = NULL;

        emit disconnected();
        emit connected(false);
Don Gagne's avatar
Don Gagne committed
200 201 202 203 204 205 206 207 208 209 210 211
	}
    
    return true;
}

/**
 * @brief Connect the connection.
 *
 * @return True if connection has been established, false if connection couldn't be established.
 **/
bool TCPLink::connect()
{
212
	if (isRunning())
Don Gagne's avatar
Don Gagne committed
213
	{
214 215
		quit();
		wait();
Don Gagne's avatar
Don Gagne committed
216
	}
217 218 219 220
    bool connected = _hardwareConnect();
    if (connected) {
        start(HighPriority);
    }
Don Gagne's avatar
Don Gagne committed
221 222 223
    return connected;
}

224
bool TCPLink::_hardwareConnect(void)
Don Gagne's avatar
Don Gagne committed
225
{
226 227
    Q_ASSERT(_socket == NULL);
	_socket = new QTcpSocket();
Don Gagne's avatar
Don Gagne committed
228
    
229
    QSignalSpy errorSpy(_socket, SIGNAL(error(QAbstractSocket::SocketError)));
Don Gagne's avatar
Don Gagne committed
230
    
231 232 233 234
    _socket->connectToHost(_hostAddress, _port);
    
    QObject::connect(_socket, SIGNAL(readyRead()), this, SLOT(readBytes()));
    QObject::connect(_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(_socketError(QAbstractSocket::SocketError)));
Don Gagne's avatar
Don Gagne committed
235 236
    
    // Give the socket a second to connect to the other side otherwise error out
237
    if (!_socket->waitForConnected(1000))
Don Gagne's avatar
Don Gagne committed
238
    {
239 240 241 242 243 244 245
        // Whether a failed connection emits an error signal or not is platform specific.
        // So in cases where it is not emitted, we emit one ourselves.
        if (errorSpy.count() == 0) {
            emit communicationError(getName(), "Connection failed");
        }
        delete _socket;
        _socket = NULL;
Don Gagne's avatar
Don Gagne committed
246 247
        return false;
    }
Don Gagne's avatar
Don Gagne committed
248
    
249
    _socketIsConnected = true;
Don Gagne's avatar
Don Gagne committed
250
    emit connected(true);
251
    emit connected();
Don Gagne's avatar
Don Gagne committed
252

Don Gagne's avatar
Don Gagne committed
253 254 255
    return true;
}

256
void TCPLink::_socketError(QAbstractSocket::SocketError socketError)
Don Gagne's avatar
Don Gagne committed
257
{
258
    Q_UNUSED(socketError);
259
    emit communicationError(getName(), "Error on socket: " + _socket->errorString());
Don Gagne's avatar
Don Gagne committed
260 261 262 263 264 265 266 267 268
}

/**
 * @brief Check if connection is active.
 *
 * @return True if link is connected, false otherwise.
 **/
bool TCPLink::isConnected() const
{
269
    return _socketIsConnected;
Don Gagne's avatar
Don Gagne committed
270 271 272 273
}

int TCPLink::getId() const
{
274
    return _linkId;
Don Gagne's avatar
Don Gagne committed
275 276 277 278
}

QString TCPLink::getName() const
{
279
    return _name;
Don Gagne's avatar
Don Gagne committed
280 281
}

282
qint64 TCPLink::getConnectionSpeed() const
Don Gagne's avatar
Don Gagne committed
283 284
{
    return 54000000; // 54 Mbit
285 286 287 288 289 290 291 292 293 294 295
}

qint64 TCPLink::getCurrentInDataRate() const
{
    return 0;
}

qint64 TCPLink::getCurrentOutDataRate() const
{
    return 0;
}
296 297 298 299 300

void TCPLink::_resetName(void)
{
    _name = QString("TCP Link (host:%1 port:%2)").arg(_hostAddress.toString()).arg(_port);
    emit nameChanged(_name);
Don Gagne's avatar
Don Gagne committed
301
}