SerialLink.h 5.63 KB
Newer Older
1 2
/****************************************************************************
 *
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
#pragma once
pixhawk's avatar
pixhawk committed
11 12 13 14 15

#include <QObject>
#include <QThread>
#include <QMutex>
#include <QString>
dogmaphobic's avatar
dogmaphobic committed
16

dogmaphobic's avatar
dogmaphobic committed
17 18 19
#ifdef __android__
#include "qserialport.h"
#else
20
#include <QSerialPort>
dogmaphobic's avatar
dogmaphobic committed
21
#endif
22
#include <QMetaType>
23
#include <QLoggingCategory>
24 25

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

28
#include "QGCConfig.h"
29 30
#include "LinkConfiguration.h"
#include "LinkInterface.h"
31

32 33
Q_DECLARE_LOGGING_CATEGORY(SerialLinkLog)

34 35 36
class LinkManager;

/// SerialLink configuration
37 38
class SerialConfiguration : public LinkConfiguration
{
Don Gagne's avatar
Don Gagne committed
39 40
    Q_OBJECT

41 42 43 44 45
public:

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

46 47 48 49 50 51 52
    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)
53
    Q_PROPERTY(bool     usbDirect       READ usbDirect          WRITE setUsbDirect          NOTIFY usbDirectChanged)        ///< true: direct usb connection to board
54

55 56 57 58 59
    int  baud()         { return _baud; }
    int  dataBits()     { return _dataBits; }
    int  flowControl()  { return _flowControl; }    ///< QSerialPort Enums
    int  stopBits()     { return _stopBits; }
    int  parity()       { return _parity; }         ///< QSerialPort Enums
60
    bool usbDirect()    { return _usbDirect; }
61

62 63
    const QString portName          () { return _portName; }
    const QString portDisplayName   () { return _portDisplayName; }
64

65 66 67 68 69 70
    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);
71
    void setUsbDirect       (bool usbDirect);
72

73
    static QStringList supportedBaudRates();
74
    static QString cleanPortDisplayname(const QString name);
75

76
    /// From LinkConfiguration
77 78 79 80 81 82
    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"; }
83
    QString     settingsTitle   () { return tr("Serial Link Settings"); }
84

85 86 87 88 89 90 91 92
signals:
    void baudChanged            ();
    void dataBitsChanged        ();
    void flowControlChanged     ();
    void stopBitsChanged        ();
    void parityChanged          ();
    void portNameChanged        ();
    void portDisplayNameChanged ();
93
    void usbDirectChanged       (bool usbDirect);
94

95 96 97
private:
    static void _initBaudRates();

98 99 100 101 102 103
    int _baud;
    int _dataBits;
    int _flowControl;
    int _stopBits;
    int _parity;
    QString _portName;
104
    QString _portDisplayName;
105
    bool _usbDirect;
106 107 108
};

class SerialLink : public LinkInterface
109
{
pixhawk's avatar
pixhawk committed
110
    Q_OBJECT
111

112
    friend class SerialConfiguration;
113
    friend class LinkManager;
114

pixhawk's avatar
pixhawk committed
115
public:
116 117 118
    // LinkInterface overrides
    bool isConnected(void) const override;
    void disconnect (void) override;
119

120 121
    /// Don't even think of calling this method!
    QSerialPort* _hackAccessToPort(void) { return _port; }
122

123
private slots:
124
    void _writeBytes(const QByteArray data) override;
pixhawk's avatar
pixhawk committed
125

126
public slots:
127
    void linkError(QSerialPort::SerialPortError error);
128

129
private slots:
130
    void _readBytes     (void);
pixhawk's avatar
pixhawk committed
131

oberion's avatar
oberion committed
132
private:
133
    // Links are only created/destroyed by LinkManager so constructor/destructor is not public
134
    SerialLink(SharedLinkConfigurationPtr& config, bool isPX4Flow = false);
135
    ~SerialLink();
136

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    // LinkInterface overrides
    bool _connect(void) override;

    void _emitLinkError     (const QString& errorMsg);
    bool _hardwareConnect   (QSerialPort::SerialPortError& error, QString& errorString);
    bool _isBootloader      (void);

    QSerialPort*            _port               = nullptr;
    quint64                 _bytesRead          = 0;
    int                     _timeout;
    QMutex                  _dataMutex;                     ///< Mutex for reading data from _port
    QMutex                  _writeMutex;                    ///< Mutex for accessing the _transmitBuffer.
    volatile bool           _stopp              = false;
    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*    _serialConfig       = nullptr;
pixhawk's avatar
pixhawk committed
153 154 155

};