SerialLink.cc 16.8 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12
/*=====================================================================
======================================================================*/
/**
 * @file
 *   @brief Cross-platform support for serial ports
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#include <QTimer>
#include <QDebug>
13
#include <QSettings>
pixhawk's avatar
pixhawk committed
14
#include <QMutexLocker>
dogmaphobic's avatar
dogmaphobic committed
15 16 17 18

#ifdef __android__
#include "qserialport.h"
#else
19
#include <QSerialPort>
dogmaphobic's avatar
dogmaphobic committed
20
#endif
21

pixhawk's avatar
pixhawk committed
22
#include "SerialLink.h"
23
#include "QGC.h"
24
#include "MG.h"
25
#include "QGCLoggingCategory.h"
Don Gagne's avatar
Don Gagne committed
26
#include "QGCApplication.h"
27
#include "QGCSerialPortInfo.h"
28

29
QGC_LOGGING_CATEGORY(SerialLinkLog, "SerialLinkLog")
pixhawk's avatar
pixhawk committed
30

31 32
static QStringList kSupportedBaudRates;

33 34 35 36 37 38 39 40 41
SerialLink::SerialLink(SerialConfiguration* config)
{
    _bytesRead = 0;
    _port     = Q_NULLPTR;
    _stopp    = false;
    _reqReset = false;
    Q_ASSERT(config != NULL);
    _config = config;
    _config->setLink(this);
Bill Bonney's avatar
Bill Bonney committed
42

43
    qCDebug(SerialLinkLog) << "Create SerialLink " << config->portName() << config->baud() << config->flowControl()
44
             << config->parity() << config->dataBits() << config->stopBits();
45
    qCDebug(SerialLinkLog) << "portName: " << config->portName();
pixhawk's avatar
pixhawk committed
46
}
47

48 49
void SerialLink::requestReset()
{
50 51
    QMutexLocker locker(&this->_stoppMutex);
    _reqReset = true;
52
}
pixhawk's avatar
pixhawk committed
53 54 55

SerialLink::~SerialLink()
{
56 57
    // Disconnect link from configuration
    _config->setLink(NULL);
58
    _disconnect();
59 60
    if(_port) delete _port;
    _port = NULL;
61 62
}

63
bool SerialLink::_isBootloader()
64
{
65
    QList<QSerialPortInfo> portList = QSerialPortInfo::availablePorts();
66 67 68 69 70
    if( portList.count() == 0){
        return false;
    }
    foreach (const QSerialPortInfo &info, portList)
    {
71 72 73 74 75 76 77 78
        qCDebug(SerialLinkLog) << "PortName    : " << info.portName() << "Description : " << info.description();
        qCDebug(SerialLinkLog) << "Manufacturer: " << info.manufacturer();
        if (info.portName().trimmed() == _config->portName() &&
                (info.description().toLower().contains("bootloader") ||
                 info.description().toLower().contains("px4 bl") ||
                 info.description().toLower().contains("px4 fmu v1.6"))) {
            qCDebug(SerialLinkLog) << "BOOTLOADER FOUND";
            return true;
79 80 81 82 83 84
       }
    }
    // Not found
    return false;
}

85 86
void SerialLink::writeBytes(const char* data, qint64 size)
{
87
    if(_port && _port->isOpen()) {
88 89
        _logOutputDataRate(size, QDateTime::currentMSecsSinceEpoch());
        _port->write(data, size);
90 91
    } else {
        // Error occured
92
        _emitLinkError(tr("Could not send data - link %1 is disconnected!").arg(getName()));
pixhawk's avatar
pixhawk committed
93 94 95 96 97 98 99 100 101
    }
}

/**
 * @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
 **/
102 103
void SerialLink::readBytes()
{
104
    if(_port && _port->isOpen()) {
105 106
        const qint64 maxLength = 2048;
        char data[maxLength];
107 108
        _dataMutex.lock();
        qint64 numBytes = _port->bytesAvailable();
109

110
        if (numBytes > 0) {
pixhawk's avatar
pixhawk committed
111 112 113
            /* Read as much data in buffer as possible without overflow */
            if(maxLength < numBytes) numBytes = maxLength;

114
            _logInputDataRate(numBytes, QDateTime::currentMSecsSinceEpoch());
115

116
            _port->read(data, numBytes);
pixhawk's avatar
pixhawk committed
117 118 119
            QByteArray b(data, numBytes);
            emit bytesReceived(this, b);
        }
120
        _dataMutex.unlock();
pixhawk's avatar
pixhawk committed
121 122 123 124 125 126 127 128
    }
}

/**
 * @brief Disconnect the connection.
 *
 * @return True if connection has been disconnected, false if connection couldn't be disconnected.
 **/
Don Gagne's avatar
Don Gagne committed
129
void SerialLink::_disconnect(void)
130
{
131 132 133 134
    if (_port) {
        _port->close();
        delete _port;
        _port = NULL;
135
    }
136

dogmaphobic's avatar
dogmaphobic committed
137
#ifdef __android__
138
    qgcApp()->toolbox()->linkManager()->suspendConfigurationUpdates(false);
dogmaphobic's avatar
dogmaphobic committed
139
#endif
pixhawk's avatar
pixhawk committed
140 141 142 143 144 145 146
}

/**
 * @brief Connect the connection.
 *
 * @return True if connection has been established, false if connection couldn't be established.
 **/
147
bool SerialLink::_connect(void)
148
{
149
    qCDebug(SerialLinkLog) << "CONNECT CALLED";
150 151

    _disconnect();
152

dogmaphobic's avatar
dogmaphobic committed
153
#ifdef __android__
154
    qgcApp()->toolbox()->linkManager()->suspendConfigurationUpdates(true);
155
#endif
156

157 158 159
    QSerialPort::SerialPortError    error;
    QString                         errorString;

dogmaphobic's avatar
dogmaphobic committed
160
    // Initialize the connection
161 162 163 164 165 166 167
    if (!_hardwareConnect(error, errorString)) {
        if (qgcApp()->toolbox()->linkManager()->isAutoconnectLink(this)) {
            // Be careful with spitting out open error related to trying to open a busy port using autoconnect
            if (error == QSerialPort::PermissionError) {
                // Device already open, ignore and fail connect
                return false;
            }
dogmaphobic's avatar
dogmaphobic committed
168
        }
169 170

        _emitLinkError(QString("Error connecting: Could not create port. %1").arg(errorString));
dogmaphobic's avatar
dogmaphobic committed
171 172
        return false;
    }
173
    return true;
pixhawk's avatar
pixhawk committed
174 175
}

176 177 178 179 180
/// Performs the actual hardware port connection.
///     @param[out] error if failed
///     @param[out] error string if failed
/// @return success/fail
bool SerialLink::_hardwareConnect(QSerialPort::SerialPortError& error, QString& errorString)
181
{
182
    if (_port) {
183
        qCDebug(SerialLinkLog) << "SerialLink:" << QString::number((long)this, 16) << "closing port";
184
        _port->close();
185
        QGC::SLEEP::usleep(50000);
186 187
        delete _port;
        _port = NULL;
188
    }
pixhawk's avatar
pixhawk committed
189

190
    qCDebug(SerialLinkLog) << "SerialLink: hardwareConnect to " << _config->portName();
191

192
    // If we are in the Pixhawk bootloader code wait for it to timeout
193
    if (_isBootloader()) {
194
        qCDebug(SerialLinkLog) << "Not connecting to a bootloader, waiting for 2nd chance";
195 196 197
        const unsigned retry_limit = 12;
        unsigned retries;
        for (retries = 0; retries < retry_limit; retries++) {
198 199
            if (!_isBootloader()) {
                QGC::SLEEP::msleep(500);
200 201 202 203 204 205 206
                break;
            }
            QGC::SLEEP::msleep(500);
        }
        // Check limit
        if (retries == retry_limit) {
            // bail out
207
            qWarning() << "Timeout waiting for something other than booloader";
208 209 210 211
            return false;
        }
    }

212 213 214
    _port = new QSerialPort(_config->portName());
    if (!_port) {
        emit communicationUpdate(getName(),"Error opening port: " + _config->portName());
Bill Bonney's avatar
Bill Bonney committed
215
        return false; // couldn't create serial port.
216
    }
Bill Bonney's avatar
Bill Bonney committed
217

218
    QObject::connect(_port, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(linkError(QSerialPort::SerialPortError)));
219
    QObject::connect(_port, &QIODevice::readyRead, this, &SerialLink::_readBytes);
Bill Bonney's avatar
Bill Bonney committed
220

221
    //  port->setCommTimeouts(QSerialPort::CtScheme_NonBlockingRead);
pixhawk's avatar
pixhawk committed
222

223
    // TODO This needs a bit of TLC still...
224

225
    // After the bootloader times out, it still can take a second or so for the Pixhawk USB driver to come up and make
226
    // the port available for open. So we retry a few times to wait for it.
dogmaphobic's avatar
dogmaphobic committed
227 228 229
#ifdef __android__
    _port->open(QIODevice::ReadWrite);
#else
230 231
    for (int openRetries = 0; openRetries < 4; openRetries++) {
        if (!_port->open(QIODevice::ReadWrite)) {
232
            qCDebug(SerialLinkLog) << "Port open failed, retrying";
233 234 235 236
            QGC::SLEEP::msleep(500);
        } else {
            break;
        }
237
    }
dogmaphobic's avatar
dogmaphobic committed
238
#endif
239
    if (!_port->isOpen() ) {
240 241 242
        qDebug() << "open failed" << _port->errorString() << _port->error() << getName() << qgcApp()->toolbox()->linkManager()->isAutoconnectLink(this);
        error = _port->error();
        errorString = _port->errorString();
243 244
        emit communicationUpdate(getName(),"Error opening port: " + _port->errorString());
        _port->close();
245 246
        delete _port;
        _port = NULL;
247
        return false; // couldn't open serial port
248 249
    }

250
    qCDebug(SerialLinkLog) << "Configuring port";
251 252 253 254 255
    _port->setBaudRate     (_config->baud());
    _port->setDataBits     (static_cast<QSerialPort::DataBits>     (_config->dataBits()));
    _port->setFlowControl  (static_cast<QSerialPort::FlowControl>  (_config->flowControl()));
    _port->setStopBits     (static_cast<QSerialPort::StopBits>     (_config->stopBits()));
    _port->setParity       (static_cast<QSerialPort::Parity>       (_config->parity()));
256

257
    emit communicationUpdate(getName(), "Opened port!");
Bill Bonney's avatar
Bill Bonney committed
258
    emit connected();
259

260
    qCDebug(SerialLinkLog) << "Connection SeriaLink: " << "with settings" << _config->portName()
261
             << _config->baud() << _config->dataBits() << _config->parity() << _config->stopBits();
262

Bill Bonney's avatar
Bill Bonney committed
263
    return true; // successful connection
pixhawk's avatar
pixhawk committed
264
}
265

266 267 268 269 270 271 272 273 274 275 276 277
void SerialLink::_readBytes(void)
{
    qint64 byteCount = _port->bytesAvailable();
    if (byteCount)
    {
        QByteArray buffer;
        buffer.resize(byteCount);
        _port->read(buffer.data(), buffer.size());
        emit bytesReceived(this, buffer);
    }
}

278
void SerialLink::linkError(QSerialPort::SerialPortError error)
279
{
280
    if (error != QSerialPort::NoError) {
281 282 283 284
        // You can use the following qDebug output as needed during development. Make sure to comment it back out
        // when you are done. The reason for this is that this signal is very noisy. For example if you try to
        // connect to a PixHawk before it is ready to accept the connection it will output a continuous stream
        // of errors until the Pixhawk responds.
285
        //qCDebug(SerialLinkLog) << "SerialLink::linkError" << error;
286
    }
287 288
}

pixhawk's avatar
pixhawk committed
289 290 291 292 293
/**
 * @brief Check if connection is active.
 *
 * @return True if link is connected, false otherwise.
 **/
294
bool SerialLink::isConnected() const
295
{
296
    bool isConnected = false;
Bill Bonney's avatar
Bill Bonney committed
297

298
    if (_port) {
299
        isConnected = _port->isOpen();
lm's avatar
lm committed
300
    }
301

302
    return isConnected;
pixhawk's avatar
pixhawk committed
303 304
}

305
QString SerialLink::getName() const
pixhawk's avatar
pixhawk committed
306
{
307
    return _config->portName();
pixhawk's avatar
pixhawk committed
308 309
}

310 311 312 313
/**
  * This function maps baud rate constants to numerical equivalents.
  * It relies on the mapping given in qportsettings.h from the QSerialPort library.
  */
314
qint64 SerialLink::getConnectionSpeed() const
315
{
Bill Bonney's avatar
Bill Bonney committed
316
    int baudRate;
317 318
    if (_port) {
        baudRate = _port->baudRate();
Bill Bonney's avatar
Bill Bonney committed
319
    } else {
320
        baudRate = _config->baud();
Bill Bonney's avatar
Bill Bonney committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    }
    qint64 dataRate;
    switch (baudRate)
    {
        case QSerialPort::Baud1200:
            dataRate = 1200;
            break;
        case QSerialPort::Baud2400:
            dataRate = 2400;
            break;
        case QSerialPort::Baud4800:
            dataRate = 4800;
            break;
        case QSerialPort::Baud9600:
            dataRate = 9600;
            break;
        case QSerialPort::Baud19200:
            dataRate = 19200;
            break;
        case QSerialPort::Baud38400:
            dataRate = 38400;
            break;
        case QSerialPort::Baud57600:
            dataRate = 57600;
            break;
        case QSerialPort::Baud115200:
            dataRate = 115200;
            break;
            // Otherwise do nothing.
        default:
            dataRate = -1;
            break;
pixhawk's avatar
pixhawk committed
353 354 355 356
    }
    return dataRate;
}

357
void SerialLink::_resetConfiguration()
358
{
359
    if (_port) {
Don Gagne's avatar
Don Gagne committed
360 361 362 363 364
        _port->setBaudRate      (_config->baud());
        _port->setDataBits      (static_cast<QSerialPort::DataBits>    (_config->dataBits()));
        _port->setFlowControl   (static_cast<QSerialPort::FlowControl> (_config->flowControl()));
        _port->setStopBits      (static_cast<QSerialPort::StopBits>    (_config->stopBits()));
        _port->setParity        (static_cast<QSerialPort::Parity>      (_config->parity()));
365
    }
pixhawk's avatar
pixhawk committed
366 367
}

368
void SerialLink::_emitLinkError(const QString& errorMsg)
369
{
370
    QString msg("Error on link %1. %2");
371
    qDebug() << errorMsg;
372
    emit communicationError(tr("Link Error"), msg.arg(getName()).arg(errorMsg));
pixhawk's avatar
pixhawk committed
373 374
}

375
LinkConfiguration* SerialLink::getLinkConfiguration()
376
{
377
    return _config;
pixhawk's avatar
pixhawk committed
378 379
}

380 381 382 383 384 385 386 387 388
bool SerialLink::requiresUSBMavlinkStart(void) const
{
    if (_port) {
        return QGCSerialPortInfo(*_port).boardTypePixhawk();
    } else {
        return false;
    }
}

389 390
//--------------------------------------------------------------------------
//-- SerialConfiguration
pixhawk's avatar
pixhawk committed
391

392
SerialConfiguration::SerialConfiguration(const QString& name) : LinkConfiguration(name)
393
{
394 395 396 397 398
    _baud       = 57600;
    _flowControl= QSerialPort::NoFlowControl;
    _parity     = QSerialPort::NoParity;
    _dataBits   = 8;
    _stopBits   = 1;
pixhawk's avatar
pixhawk committed
399 400
}

401
SerialConfiguration::SerialConfiguration(SerialConfiguration* copy) : LinkConfiguration(copy)
402
{
403 404 405 406 407 408 409
    _baud               = copy->baud();
    _flowControl        = copy->flowControl();
    _parity             = copy->parity();
    _dataBits           = copy->dataBits();
    _stopBits           = copy->stopBits();
    _portName           = copy->portName();
    _portDisplayName    = copy->portDisplayName();
pixhawk's avatar
pixhawk committed
410 411
}

412
void SerialConfiguration::copyFrom(LinkConfiguration *source)
413
{
414 415 416
    LinkConfiguration::copyFrom(source);
    SerialConfiguration* ssource = dynamic_cast<SerialConfiguration*>(source);
    Q_ASSERT(ssource != NULL);
417 418 419 420 421 422 423
    _baud               = ssource->baud();
    _flowControl        = ssource->flowControl();
    _parity             = ssource->parity();
    _dataBits           = ssource->dataBits();
    _stopBits           = ssource->stopBits();
    _portName           = ssource->portName();
    _portDisplayName    = ssource->portDisplayName();
424 425
}

426
void SerialConfiguration::updateSettings()
427
{
428 429 430 431 432
    if(_link) {
        SerialLink* serialLink = dynamic_cast<SerialLink*>(_link);
        if(serialLink) {
            serialLink->_resetConfiguration();
        }
433
    }
434 435
}

436
void SerialConfiguration::setBaud(int baud)
pixhawk's avatar
pixhawk committed
437
{
438
    _baud = baud;
pixhawk's avatar
pixhawk committed
439 440
}

441
void SerialConfiguration::setDataBits(int databits)
pixhawk's avatar
pixhawk committed
442
{
443
    _dataBits = databits;
pixhawk's avatar
pixhawk committed
444 445
}

446
void SerialConfiguration::setFlowControl(int flowControl)
pixhawk's avatar
pixhawk committed
447
{
448
    _flowControl = flowControl;
pixhawk's avatar
pixhawk committed
449 450
}

451
void SerialConfiguration::setStopBits(int stopBits)
452
{
453
    _stopBits = stopBits;
pixhawk's avatar
pixhawk committed
454 455
}

456
void SerialConfiguration::setParity(int parity)
457
{
458
    _parity = parity;
pixhawk's avatar
pixhawk committed
459 460
}

461
void SerialConfiguration::setPortName(const QString& portName)
462
{
463 464 465 466
    // No effect on a running connection
    QString pname = portName.trimmed();
    if (!pname.isEmpty() && pname != _portName) {
        _portName = pname;
467
        _portDisplayName = cleanPortDisplayname(pname);
468 469 470
    }
}

471 472 473
QString SerialConfiguration::cleanPortDisplayname(const QString name)
{
    QString pname = name.trimmed();
474
#ifdef Q_OS_WIN
475 476 477 478 479 480 481 482
    pname.replace("\\\\.\\", "");
#else
    pname.replace("/dev/cu.", "");
    pname.replace("/dev/", "");
#endif
    return pname;
}

483
void SerialConfiguration::saveSettings(QSettings& settings, const QString& root)
484
{
485
    settings.beginGroup(root);
486 487 488 489 490 491 492
    settings.setValue("baud",           _baud);
    settings.setValue("dataBits",       _dataBits);
    settings.setValue("flowControl",    _flowControl);
    settings.setValue("stopBits",       _stopBits);
    settings.setValue("parity",         _parity);
    settings.setValue("portName",       _portName);
    settings.setValue("portDisplayName",_portDisplayName);
493
    settings.endGroup();
494
}
495

496
void SerialConfiguration::loadSettings(QSettings& settings, const QString& root)
497
{
498
    settings.beginGroup(root);
499 500 501 502 503 504 505
    if(settings.contains("baud"))           _baud           = settings.value("baud").toInt();
    if(settings.contains("dataBits"))       _dataBits       = settings.value("dataBits").toInt();
    if(settings.contains("flowControl"))    _flowControl    = settings.value("flowControl").toInt();
    if(settings.contains("stopBits"))       _stopBits       = settings.value("stopBits").toInt();
    if(settings.contains("parity"))         _parity         = settings.value("parity").toInt();
    if(settings.contains("portName"))       _portName       = settings.value("portName").toString();
    if(settings.contains("portDisplayName"))_portDisplayName= settings.value("portDisplayName").toString();
506
    settings.endGroup();
507
}
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564

QStringList SerialConfiguration::supportedBaudRates()
{
    if(!kSupportedBaudRates.size())
        _initBaudRates();
    return kSupportedBaudRates;
}

void SerialConfiguration::_initBaudRates()
{
    kSupportedBaudRates.clear();
#if USE_ANCIENT_RATES
#if defined(Q_OS_UNIX) || defined(Q_OS_LINUX) || defined(Q_OS_DARWIN)
    kSupportedBaudRates << "50";
    kSupportedBaudRates << "75";
#endif
    kSupportedBaudRates << "110";
#if defined(Q_OS_UNIX) || defined(Q_OS_LINUX) || defined(Q_OS_DARWIN)
    kSupportedBaudRates << "134";
    kSupportedBaudRates << "150";
    kSupportedBaudRates << "200";
#endif
    kSupportedBaudRates << "300";
    kSupportedBaudRates << "600";
    kSupportedBaudRates << "1200";
#if defined(Q_OS_UNIX) || defined(Q_OS_LINUX) || defined(Q_OS_DARWIN)
    kSupportedBaudRates << "1800";
#endif
#endif
    kSupportedBaudRates << "2400";
    kSupportedBaudRates << "4800";
    kSupportedBaudRates << "9600";
#if defined(Q_OS_WIN)
    kSupportedBaudRates << "14400";
#endif
    kSupportedBaudRates << "19200";
    kSupportedBaudRates << "38400";
#if defined(Q_OS_WIN)
    kSupportedBaudRates << "56000";
#endif
    kSupportedBaudRates << "57600";
    kSupportedBaudRates << "115200";
#if defined(Q_OS_WIN)
    kSupportedBaudRates << "128000";
#endif
    kSupportedBaudRates << "230400";
#if defined(Q_OS_WIN)
    kSupportedBaudRates << "256000";
#endif
    kSupportedBaudRates << "460800";
#if defined(Q_OS_LINUX)
    kSupportedBaudRates << "500000";
    kSupportedBaudRates << "576000";
#endif
    kSupportedBaudRates << "921600";
}