SerialLink.h 6.96 KB
Newer Older
pixhawk's avatar
pixhawk committed
1
/*=====================================================================
pixhawk's avatar
pixhawk committed
2 3 4 5 6 7 8 9

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
pixhawk's avatar
pixhawk committed
10 11 12
    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.
pixhawk's avatar
pixhawk committed
13 14

    QGROUNDCONTROL is distributed in the hope that it will be useful,
pixhawk's avatar
pixhawk committed
15 16 17
    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.
pixhawk's avatar
pixhawk committed
18

pixhawk's avatar
pixhawk committed
19
    You should have received a copy of the GNU General Public License
pixhawk's avatar
pixhawk committed
20 21
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

pixhawk's avatar
pixhawk committed
22
======================================================================*/
23

pixhawk's avatar
pixhawk committed
24 25 26 27 28 29 30 31 32 33 34
/**
 * @file
 *   @brief Brief Description
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#ifndef SERIALLINK_H
#define SERIALLINK_H

35 36 37 38
class LinkInterface;
class SerialConfiguration;
class SerialLink;

pixhawk's avatar
pixhawk committed
39 40 41 42
#include <QObject>
#include <QThread>
#include <QMutex>
#include <QString>
dogmaphobic's avatar
dogmaphobic committed
43

dogmaphobic's avatar
dogmaphobic committed
44 45 46
#ifdef __android__
#include "qserialport.h"
#else
47
#include <QSerialPort>
dogmaphobic's avatar
dogmaphobic committed
48
#endif
49
#include <QMetaType>
50
#include <QLoggingCategory>
51 52

// We use QSerialPort::SerialPortError in a signal so we must declare it as a meta type
53 54
Q_DECLARE_METATYPE(QSerialPort::SerialPortError)

55 56 57
#include "QGCConfig.h"
#include "LinkManager.h"

58 59
Q_DECLARE_LOGGING_CATEGORY(SerialLinkLog)

60 61
class SerialConfiguration : public LinkConfiguration
{
Don Gagne's avatar
Don Gagne committed
62 63
    Q_OBJECT

64 65 66 67 68
public:

    SerialConfiguration(const QString& name);
    SerialConfiguration(SerialConfiguration* copy);

69 70 71 72 73 74 75
    Q_PROPERTY(int      baud            READ baud               WRITE setBaud               NOTIFY baudChanged)
    Q_PROPERTY(int      dataBits        READ dataBits           WRITE setDataBits           NOTIFY dataBitsChanged)
    Q_PROPERTY(int      flowControl     READ flowControl        WRITE setFlowControl        NOTIFY flowControlChanged)
    Q_PROPERTY(int      stopBits        READ stopBits           WRITE setStopBits           NOTIFY stopBitsChanged)
    Q_PROPERTY(int      parity          READ parity             WRITE setParity             NOTIFY parityChanged)
    Q_PROPERTY(QString  portName        READ portName           WRITE setPortName           NOTIFY portNameChanged)
    Q_PROPERTY(QString  portDisplayName READ portDisplayName                                NOTIFY portDisplayNameChanged)
76
    Q_PROPERTY(bool     usbDirect       READ usbDirect          WRITE setUsbDirect          NOTIFY usbDirectChanged)        ///< true: direct usb connection to board
77

78 79 80 81 82
    int  baud()         { return _baud; }
    int  dataBits()     { return _dataBits; }
    int  flowControl()  { return _flowControl; }    ///< QSerialPort Enums
    int  stopBits()     { return _stopBits; }
    int  parity()       { return _parity; }         ///< QSerialPort Enums
83
    bool usbDirect()    { return _usbDirect; }
84

85 86
    const QString portName          () { return _portName; }
    const QString portDisplayName   () { return _portDisplayName; }
87

88 89 90 91 92 93
    void setBaud            (int baud);
    void setDataBits        (int databits);
    void setFlowControl     (int flowControl);          ///< QSerialPort Enums
    void setStopBits        (int stopBits);
    void setParity          (int parity);               ///< QSerialPort Enums
    void setPortName        (const QString& portName);
94
    void setUsbDirect       (bool usbDirect);
95

96
    static QStringList supportedBaudRates();
97
    static QString cleanPortDisplayname(const QString name);
98

99
    /// From LinkConfiguration
100 101 102 103 104 105
    LinkType    type            () { return LinkConfiguration::TypeSerial; }
    void        copyFrom        (LinkConfiguration* source);
    void        loadSettings    (QSettings& settings, const QString& root);
    void        saveSettings    (QSettings& settings, const QString& root);
    void        updateSettings  ();
    QString     settingsURL     () { return "SerialSettings.qml"; }
106

107 108 109 110 111 112 113 114
signals:
    void baudChanged            ();
    void dataBitsChanged        ();
    void flowControlChanged     ();
    void stopBitsChanged        ();
    void parityChanged          ();
    void portNameChanged        ();
    void portDisplayNameChanged ();
115
    void usbDirectChanged       (bool usbDirect);
116

117 118 119
private:
    static void _initBaudRates();

120 121 122 123 124 125 126
private:
    int _baud;
    int _dataBits;
    int _flowControl;
    int _stopBits;
    int _parity;
    QString _portName;
127
    QString _portDisplayName;
128
    bool _usbDirect;
129 130
};

pixhawk's avatar
pixhawk committed
131 132 133 134 135 136 137 138
/**
 * @brief The SerialLink class provides cross-platform access to serial links.
 * It takes care of the link management and provides a common API to higher
 * level communication layers. It is implemented as a wrapper class for a thread
 * that handles the serial communication. All methods have therefore to be thread-
 * safe.
 *
 */
139
class SerialLink : public LinkInterface
140
{
pixhawk's avatar
pixhawk committed
141
    Q_OBJECT
142

143
    friend class SerialConfiguration;
144
    friend class LinkManager;
145

pixhawk's avatar
pixhawk committed
146
public:
147
    // LinkInterface
pixhawk's avatar
pixhawk committed
148

149
    LinkConfiguration* getLinkConfiguration();
150
    QString getName() const;
151 152 153
    void    requestReset();
    bool    isConnected() const;
    qint64  getConnectionSpeed() const;
154

155 156 157 158
    // These are left unimplemented in order to cause linker errors which indicate incorrect usage of
    // connect/disconnect on link directly. All connect/disconnect calls should be made through LinkManager.
    bool    connect(void);
    bool    disconnect(void);
159

160
private slots:
pixhawk's avatar
pixhawk committed
161 162 163 164 165 166
    /**
     * @brief Write a number of bytes to the interface.
     *
     * @param data Pointer to the data byte array
     * @param size The size of the bytes array
     **/
167
    void writeBytes(const QByteArray data);
pixhawk's avatar
pixhawk committed
168

169
public slots:
170
    void linkError(QSerialPort::SerialPortError error);
171

pixhawk's avatar
pixhawk committed
172
protected:
173 174 175 176 177 178
    QSerialPort* _port;
    quint64 _bytesRead;
    int     _timeout;
    QMutex  _dataMutex;       // Mutex for reading data from _port
    QMutex  _writeMutex;      // Mutex for accessing the _transmitBuffer.

179
private slots:
180
    void _readBytes(void);
pixhawk's avatar
pixhawk committed
181

oberion's avatar
oberion committed
182
private:
183 184 185
    // Links are only created/destroyed by LinkManager so constructor/destructor is not public
    SerialLink(SerialConfiguration* config);
    ~SerialLink();
186

187
    // From LinkInterface
188
    virtual bool _connect(void);
Don Gagne's avatar
Don Gagne committed
189
    virtual void _disconnect(void);
190

191 192
    // Internal methods
    void _emitLinkError(const QString& errorMsg);
193
    bool _hardwareConnect(QSerialPort::SerialPortError& error, QString& errorString);
194 195 196 197 198 199 200 201 202
    bool _isBootloader();
    void _resetConfiguration();

    // Local data
    volatile bool        _stopp;
    volatile bool        _reqReset;
    QMutex               _stoppMutex;      // Mutex for accessing _stopp
    QByteArray           _transmitBuffer;  // An internal buffer for receiving data from member functions and actually transmitting them via the serial port.
    SerialConfiguration* _config;
pixhawk's avatar
pixhawk committed
203 204

signals:
205
    void aboutToCloseFlag();
pixhawk's avatar
pixhawk committed
206 207 208 209

};

#endif // SERIALLINK_H