SerialLink.h 6.64 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
pixhawk's avatar
pixhawk committed
9

10

pixhawk's avatar
pixhawk committed
11 12 13 14 15 16 17 18
/**
 * @file
 *   @brief Brief Description
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

19
#pragma once
pixhawk's avatar
pixhawk committed
20

21 22 23 24
class LinkInterface;
class SerialConfiguration;
class SerialLink;

pixhawk's avatar
pixhawk committed
25 26 27 28
#include <QObject>
#include <QThread>
#include <QMutex>
#include <QString>
dogmaphobic's avatar
dogmaphobic committed
29

dogmaphobic's avatar
dogmaphobic committed
30 31 32
#ifdef __android__
#include "qserialport.h"
#else
33
#include <QSerialPort>
dogmaphobic's avatar
dogmaphobic committed
34
#endif
35
#include <QMetaType>
36
#include <QLoggingCategory>
37 38

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

41 42 43
#include "QGCConfig.h"
#include "LinkManager.h"

44 45
Q_DECLARE_LOGGING_CATEGORY(SerialLinkLog)

46
/// SerialLink configuration
47 48
class SerialConfiguration : public LinkConfiguration
{
Don Gagne's avatar
Don Gagne committed
49 50
    Q_OBJECT

51 52 53 54 55
public:

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

56 57 58 59 60 61 62
    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)
63
    Q_PROPERTY(bool     usbDirect       READ usbDirect          WRITE setUsbDirect          NOTIFY usbDirectChanged)        ///< true: direct usb connection to board
64

65 66 67 68 69
    int  baud()         { return _baud; }
    int  dataBits()     { return _dataBits; }
    int  flowControl()  { return _flowControl; }    ///< QSerialPort Enums
    int  stopBits()     { return _stopBits; }
    int  parity()       { return _parity; }         ///< QSerialPort Enums
70
    bool usbDirect()    { return _usbDirect; }
71

72 73
    const QString portName          () { return _portName; }
    const QString portDisplayName   () { return _portDisplayName; }
74

75 76 77 78 79 80
    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);
81
    void setUsbDirect       (bool usbDirect);
82

83
    static QStringList supportedBaudRates();
84
    static QString cleanPortDisplayname(const QString name);
85

86
    /// From LinkConfiguration
87 88
    LinkType    type            () { return LinkConfiguration::TypeSerial; }
    void        copyFrom        (LinkConfiguration* source);
89
    bool        isHighLatencyAllowed () { return true; }
90 91 92 93
    void        loadSettings    (QSettings& settings, const QString& root);
    void        saveSettings    (QSettings& settings, const QString& root);
    void        updateSettings  ();
    QString     settingsURL     () { return "SerialSettings.qml"; }
94
    QString     settingsTitle   () { return tr("Serial Link Settings"); }
95

96 97 98 99 100 101 102 103
signals:
    void baudChanged            ();
    void dataBitsChanged        ();
    void flowControlChanged     ();
    void stopBitsChanged        ();
    void parityChanged          ();
    void portNameChanged        ();
    void portDisplayNameChanged ();
104
    void usbDirectChanged       (bool usbDirect);
105

106 107 108
private:
    static void _initBaudRates();

109 110 111 112 113 114 115
private:
    int _baud;
    int _dataBits;
    int _flowControl;
    int _stopBits;
    int _parity;
    QString _portName;
116
    QString _portDisplayName;
117
    bool _usbDirect;
118 119
};

pixhawk's avatar
pixhawk committed
120 121 122 123 124 125 126 127
/**
 * @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.
 *
 */
128
class SerialLink : public LinkInterface
129
{
pixhawk's avatar
pixhawk committed
130
    Q_OBJECT
131

132
    friend class SerialConfiguration;
133
    friend class LinkManager;
134

pixhawk's avatar
pixhawk committed
135
public:
136
    // LinkInterface
pixhawk's avatar
pixhawk committed
137

138
    QString getName() const;
139 140 141
    void    requestReset();
    bool    isConnected() const;
    qint64  getConnectionSpeed() const;
142
    SerialConfiguration* getSerialConfig() const { return _serialConfig; }
143

144 145 146 147
    // 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);
148

149 150 151
    /// Don't even think of calling this method!
    QSerialPort* _hackAccessToPort(void) { return _port; }

152
private slots:
pixhawk's avatar
pixhawk committed
153 154 155 156 157 158
    /**
     * @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
     **/
159
    void _writeBytes(const QByteArray data);
pixhawk's avatar
pixhawk committed
160

161
public slots:
162
    void linkError(QSerialPort::SerialPortError error);
163

pixhawk's avatar
pixhawk committed
164
protected:
165 166 167 168 169 170
    QSerialPort* _port;
    quint64 _bytesRead;
    int     _timeout;
    QMutex  _dataMutex;       // Mutex for reading data from _port
    QMutex  _writeMutex;      // Mutex for accessing the _transmitBuffer.

171
private slots:
172
    void _readBytes(void);
pixhawk's avatar
pixhawk committed
173

oberion's avatar
oberion committed
174
private:
175
    // Links are only created/destroyed by LinkManager so constructor/destructor is not public
176
    SerialLink(SharedLinkConfigurationPointer& config, bool isPX4Flow = false);
177
    ~SerialLink();
178

179
    // From LinkInterface
180
    virtual bool _connect(void);
Don Gagne's avatar
Don Gagne committed
181
    virtual void _disconnect(void);
182

183 184
    // Internal methods
    void _emitLinkError(const QString& errorMsg);
185
    bool _hardwareConnect(QSerialPort::SerialPortError& error, QString& errorString);
186 187 188 189 190 191 192 193
    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.
194
    SerialConfiguration* _serialConfig;
pixhawk's avatar
pixhawk committed
195 196

signals:
197
    void aboutToCloseFlag();
pixhawk's avatar
pixhawk committed
198 199 200

};