SerialLink.h 5.32 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>
43
#include <QSerialPort>
44
#include <QMetaType>
45
#include <QLoggingCategory>
46 47

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

50 51 52
#include "QGCConfig.h"
#include "LinkManager.h"

53 54
Q_DECLARE_LOGGING_CATEGORY(SerialLinkLog)

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
class SerialConfiguration : public LinkConfiguration
{
public:

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

    int  baud()         { return _baud; }
    int  dataBits()     { return _dataBits; }
    int  flowControl()  { return _flowControl; }    ///< QSerialPort Enums
    int  stopBits()     { return _stopBits; }
    int  parity()       { return _parity; }         ///< QSerialPort Enums

    const QString portName() { return _portName; }

    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);

    /// From LinkConfiguration
    int  type() { return LinkConfiguration::TypeSerial; }
    void copyFrom(LinkConfiguration* source);
    void loadSettings(QSettings& settings, const QString& root);
    void saveSettings(QSettings& settings, const QString& root);
    void updateSettings();

    /*! @brief Get a list of the currently available ports */
    static QList<QString> getCurrentPorts();

private:
    int _baud;
    int _dataBits;
    int _flowControl;
    int _stopBits;
    int _parity;
    QString _portName;
};


pixhawk's avatar
pixhawk committed
97 98 99 100 101 102 103 104
/**
 * @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.
 *
 */
105
class SerialLink : public LinkInterface
106
{
pixhawk's avatar
pixhawk committed
107
    Q_OBJECT
108
    friend class SerialConfiguration;
pixhawk's avatar
pixhawk committed
109 110
public:

111 112
    SerialLink(SerialConfiguration* config);
    ~SerialLink();
113

114
    // LinkInterface
pixhawk's avatar
pixhawk committed
115

116 117
    LinkConfiguration* getLinkConfiguration();
    int     getId() const;
118
    QString getName() const;
119 120 121 122 123 124 125
    void    requestReset();
    bool    isConnected() const;
    qint64  getConnectionSpeed() const;
    // 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);
126

pixhawk's avatar
pixhawk committed
127 128
    void run();

129
    static const int poll_interval = SERIAL_POLL_INTERVAL; ///< Polling interval, defined in QGCConfig.h
pixhawk's avatar
pixhawk committed
130

131 132 133
signals: //[TODO] Refactor to Linkinterface
    void updateLink(LinkInterface*);

pixhawk's avatar
pixhawk committed
134 135
public slots:

136
    void readBytes();
pixhawk's avatar
pixhawk committed
137 138 139 140 141 142 143 144
    /**
     * @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
     **/
    void writeBytes(const char* data, qint64 length);

145
    void linkError(QSerialPort::SerialPortError error);
146

pixhawk's avatar
pixhawk committed
147
protected:
148 149 150 151 152 153 154 155
    QSerialPort* _port;
    quint64 _bytesRead;
    int     _timeout;
    int     _id;
    QMutex  _dataMutex;       // Mutex for reading data from _port
    QMutex  _writeMutex;      // Mutex for accessing the _transmitBuffer.
    QString _type;

156 157
private slots:
    void _rerouteDisconnected(void);
pixhawk's avatar
pixhawk committed
158

oberion's avatar
oberion committed
159
private:
160
    // From LinkInterface
161 162
    bool _connect(void);
    bool _disconnect(void);
163

164 165 166 167 168 169 170 171 172 173 174 175
    // Internal methods
    void _emitLinkError(const QString& errorMsg);
    bool _hardwareConnect(QString &_type);
    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
176 177

signals:
178
    void aboutToCloseFlag();
pixhawk's avatar
pixhawk committed
179 180 181 182

};

#endif // SERIALLINK_H