LinkInterface.h 11.7 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.
 *
 ****************************************************************************/
9

10
#pragma once
pixhawk's avatar
pixhawk committed
11 12

#include <QThread>
13 14 15
#include <QDateTime>
#include <QMutex>
#include <QMutexLocker>
Lorenz Meier's avatar
Lorenz Meier committed
16
#include <QMetaType>
17
#include <QSharedPointer>
18
#include <QDebug>
19
#include <QTimer>
pixhawk's avatar
pixhawk committed
20

Don Gagne's avatar
Don Gagne committed
21
#include "QGCMAVLink.h"
22
#include "LinkConfiguration.h"
23
#include "MavlinkMessagesTimer.h"
Don Gagne's avatar
Don Gagne committed
24

25 26
class LinkManager;

pixhawk's avatar
pixhawk committed
27
/**
28 29
* @brief The link interface defines the interface for all links used to communicate
* with the ground station application.
30
**/
31 32
class LinkInterface : public QThread
{
33
    Q_OBJECT
34

35
    // Only LinkManager is allowed to create/delete or _connect/_disconnect a link
36
    friend class LinkManager;
37

38
public:    
39
    virtual ~LinkInterface() {
40
        stopMavlinkMessagesTimer();
41
        _config->setLink(nullptr);
42
    }
43

44 45
    Q_PROPERTY(bool active      READ active     NOTIFY activeChanged)
    Q_PROPERTY(bool isPX4Flow   READ isPX4Flow  CONSTANT)
Don Gagne's avatar
Don Gagne committed
46

47 48
    Q_INVOKABLE bool link_active(int vehicle_id) const;
    Q_INVOKABLE bool getHighLatency(void) const { return _highLatency; }
Don Gagne's avatar
Don Gagne committed
49

50 51 52 53
    // Property accessors
    bool active() const;
    bool isPX4Flow(void) const { return _isPX4Flow; }

54
    LinkConfiguration* getLinkConfiguration(void) { return _config.data(); }
55

56 57 58 59 60
    /* Connection management */

    /**
     * @brief Get the human readable name of this link
     */
61
    Q_INVOKABLE virtual QString getName() const = 0;
62

63 64
    virtual void requestReset() = 0;

65 66 67 68 69
    /**
     * @brief Determine the connection status
     *
     * @return True if the connection is established, false otherwise
     **/
70
    virtual bool isConnected() const = 0;
71 72 73 74

    /* Connection characteristics */

    /**
75
     * @Brief Get the maximum connection speed for this interface.
76 77 78 79 80 81 82
     *
     * The nominal data rate is the theoretical maximum data rate of the
     * interface. For 100Base-T Ethernet this would be 100 Mbit/s (100'000'000
     * Bit/s, NOT 104'857'600 Bit/s).
     *
     * @return The nominal data rate of the interface in bit per second, 0 if unknown
     **/
83
    virtual qint64 getConnectionSpeed() const = 0;
Don Gagne's avatar
Don Gagne committed
84 85 86
    
    /// @return true: This link is replaying a log file, false: Normal two-way communication link
    virtual bool isLogReplay(void) { return false; }
87

dogmaphobic's avatar
dogmaphobic committed
88
    /**
Gus Grubba's avatar
Gus Grubba committed
89
     * @Brief Enable/Disable data rate collection
dogmaphobic's avatar
dogmaphobic committed
90 91 92 93 94 95
     **/
    void enableDataRate(bool enable)
    {
        _enableRateCollection = enable;
    }

96 97 98 99 100 101 102 103
    /**
     * @Brief Get the current incoming data rate.
     *
     * This should be over a short timespan, something like 100ms. A precise value isn't necessary,
     * and this can be filtered, but should be a reasonable estimate of current data rate.
     *
     * @return The data rate of the interface in bits per second, 0 if unknown
     **/
104
    qint64 getCurrentInputDataRate() const
105
    {
106
        return _getCurrentDataRate(_inDataIndex, _inDataWriteTimes, _inDataWriteAmounts);
107
    }
108 109 110 111 112 113 114 115 116

    /**
     * @Brief Get the current outgoing data rate.
     *
     * This should be over a short timespan, something like 100ms. A precise value isn't necessary,
     * and this can be filtered, but should be a reasonable estimate of current data rate.
     *
     * @return The data rate of the interface in bits per second, 0 if unknown
     **/
117
    qint64 getCurrentOutputDataRate() const
118
    {
119
        return _getCurrentDataRate(_outDataIndex, _outDataWriteTimes, _outDataWriteAmounts);
120
    }
121 122 123
    
    /// mavlink channel to use for this link, as used by mavlink_parse_char. The mavlink channel is only
    /// set into the link when it is added to LinkManager
124
    uint8_t mavlinkChannel(void) const;
125

126 127 128 129 130
    /// Returns whether this link is high latency or not. High latency links should only perform
    /// minimal communication with vehicle.
    ///     signals: highLatencyChanged
    bool highLatency(void) const { return _highLatency; }

131 132 133
    bool decodedFirstMavlinkPacket(void) const { return _decodedFirstMavlinkPacket; }
    bool setDecodedFirstMavlinkPacket(bool decodedFirstMavlinkPacket) { return _decodedFirstMavlinkPacket = decodedFirstMavlinkPacket; }

Don Gagne's avatar
Don Gagne committed
134 135 136 137
    // 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);
138

pixhawk's avatar
pixhawk committed
139 140
public slots:

141 142 143 144 145
    /**
     * @brief This method allows to write bytes to the interface.
     *
     * If the underlying communication is packet oriented,
     * one write command equals a datagram. In case of serial
146 147
     * communication arbitrary byte lengths can be written. The method ensures
     * thread safety regardless of the underlying LinkInterface implementation.
148
     *
Gus Grubba's avatar
Gus Grubba committed
149 150
     * @param bytes:  The pointer to the byte array containing the data
     * @param length: The length of the data array
151
     **/
152 153 154 155 156 157
    void writeBytesSafe(const char *bytes, int length)
    {
        emit _invokeWriteBytes(QByteArray(bytes, length));
    }

private slots:
158
    virtual void _writeBytes(const QByteArray) = 0;
159

160
    void _activeChanged(bool active, int vehicle_id);
161
    
pixhawk's avatar
pixhawk committed
162
signals:
Don Gagne's avatar
Don Gagne committed
163
    void autoconnectChanged(bool autoconnect);
164
    void activeChanged(LinkInterface* link, bool active, int vehicle_id);
165
    void _invokeWriteBytes(QByteArray);
166
    void highLatencyChanged(bool highLatency);
167

168 169 170
    /// Signalled when a link suddenly goes away due to it being removed by for example pulling the cable to the connection.
    void connectionRemoved(LinkInterface* link);

171 172 173 174 175 176 177 178
    /**
     * @brief New data arrived
     *
     * The new data is contained in the QByteArray data. The data is copied for each
     * receiving protocol. For high-speed links like image transmission this might
     * affect performance, for control links it is however desirable to directly
     * forward the link data.
     *
Gus Grubba's avatar
Gus Grubba committed
179 180
     * @param link: Link where the data is coming from
     * @param data: The data received
181 182 183
     */
    void bytesReceived(LinkInterface* link, QByteArray data);

184 185 186 187 188 189
    /**
     * @brief New data has been sent
     * *
     * The new data is contained in the QByteArray data.
     * The data is logged into telemetry logging system
     *
Gus Grubba's avatar
Gus Grubba committed
190 191
     * @param link: Link used
     * @param data: The data sent
192 193 194
     */
    void bytesSent(LinkInterface* link, QByteArray data);

195 196 197 198 199 200 201 202 203 204 205 206 207 208
    /**
     * @brief This signal is emitted instantly when the link is connected
     **/
    void connected();

    /**
     * @brief This signal is emitted instantly when the link is disconnected
     **/
    void disconnected();

    /**
     * @brief This signal is emitted if the human readable name of this link changes
     */
    void nameChanged(QString name);
pixhawk's avatar
pixhawk committed
209

Ricardo de Almeida Gonzaga's avatar
Ricardo de Almeida Gonzaga committed
210
    /** @brief Communication error occurred */
211
    void communicationError(const QString& title, const QString& error);
212

213 214
    void communicationUpdate(const QString& linkname, const QString& text);

pixhawk's avatar
pixhawk committed
215
protected:
216
    // Links are only created by LinkManager so constructor is not public
217
    LinkInterface(SharedLinkConfigurationPointer& config, bool isPX4Flow = false);
218

219 220 221
    /// This function logs the send times and amounts of datas for input. Data is used for calculating
    /// the transmission rate.
    ///     @param byteCount Number of bytes received
Ricardo de Almeida Gonzaga's avatar
Ricardo de Almeida Gonzaga committed
222
    ///     @param time Time in ms send occurred
223
    void _logInputDataRate(quint64 byteCount, qint64 time);
224 225 226 227
    
    /// This function logs the send times and amounts of datas for output. Data is used for calculating
    /// the transmission rate.
    ///     @param byteCount Number of bytes sent
Ricardo de Almeida Gonzaga's avatar
Ricardo de Almeida Gonzaga committed
228
    ///     @param time Time in ms receive occurred
229 230 231
    void _logOutputDataRate(quint64 byteCount, qint64 time);

    SharedLinkConfigurationPointer _config;
232 233
    bool _highLatency;

234
private:
235 236 237 238 239 240 241 242 243 244 245 246
    /**
     * @brief logDataRateToBuffer Stores transmission times/amounts for statistics
     *
     * This function logs the send times and amounts of datas to the given circular buffers.
     * This data is used for calculating the transmission rate.
     *
     * @param bytesBuffer[out] The buffer to write the bytes value into.
     * @param timeBuffer[out] The buffer to write the time value into
     * @param writeIndex[out] The write index used for this buffer.
     * @param bytes The amount of bytes transmit.
     * @param time The time (in ms) this transmission occurred.
     */
247
    void _logDataRateToBuffer(quint64 *bytesBuffer, qint64 *timeBuffer, int *writeIndex, quint64 bytes, qint64 time);
248 249 250 251 252 253

    /**
     * @brief getCurrentDataRate Get the current data rate given a data rate buffer.
     *
     * This function attempts to use the times and number of bytes transmit into a current data rate
     * estimation. Since it needs to use timestamps to get the timeperiods over when the data was sent,
254
     * this is effectively a global data rate over the last _dataRateBufferSize - 1 data points. Also note
255 256 257 258 259 260 261
     * that data points older than NOW - dataRateCurrentTimespan are ignored.
     *
     * @param index The first valid sample in the data rate buffer. Refers to the oldest time sample.
     * @param dataWriteTimes The time, in ms since epoch, that each data sample took place.
     * @param dataWriteAmounts The amount of data (in bits) that was transferred.
     * @return The bits per second of data transferrence of the interface over the last [-statsCurrentTimespan, 0] timespan.
     */
262
    qint64 _getCurrentDataRate(int index, const qint64 dataWriteTimes[], const quint64 dataWriteAmounts[]) const;
263

264 265 266 267 268 269
    /**
     * @brief Connect this interface logically
     *
     * @return True if connection could be established, false otherwise
     **/
    virtual bool _connect(void) = 0;
270

Don Gagne's avatar
Don Gagne committed
271
    virtual void _disconnect(void) = 0;
272

273
    /// Sets the mavlink channel to use for this link
274
    void _setMavlinkChannel(uint8_t channel);
275
    
276
    /**
277
     * @brief startMavlinkMessagesTimer
278
     *
279
     * Start/restart the mavlink messages timer for the specific vehicle.
acfloria's avatar
acfloria committed
280
     * If no timer exists an instance is allocated.
281
     */
282
    void startMavlinkMessagesTimer(int vehicle_id);
283 284

    /**
285
     * @brief stopMavlinkMessagesTimer
286
     *
287
     * Stop and deallocate the mavlink messages timers for all vehicles if any exists.
288
     */
289
    void stopMavlinkMessagesTimer();
290

291 292
    bool _mavlinkChannelSet;    ///< true: _mavlinkChannel has been set
    uint8_t _mavlinkChannel;    ///< mavlink channel to use for this link, as used by mavlink_parse_char
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    
    static const int _dataRateBufferSize = 20; ///< Specify how many data points to capture for data rate calculations.
    
    static const qint64 _dataRateCurrentTimespan = 500; ///< Set the maximum age of samples to use for data calculations (ms).
    
    // Implement a simple circular buffer for storing when and how much data was received.
    // Used for calculating the incoming data rate. Use with *StatsBuffer() functions.
    int     _inDataIndex;
    quint64 _inDataWriteAmounts[_dataRateBufferSize]; // In bytes
    qint64  _inDataWriteTimes[_dataRateBufferSize]; // in ms
    
    // Implement a simple circular buffer for storing when and how much data was transmit.
    // Used for calculating the outgoing data rate. Use with *StatsBuffer() functions.
    int     _outDataIndex;
    quint64 _outDataWriteAmounts[_dataRateBufferSize]; // In bytes
    qint64  _outDataWriteTimes[_dataRateBufferSize]; // in ms
    
    mutable QMutex _dataRateMutex; // Mutex for accessing the data rate member variables
Don Gagne's avatar
Don Gagne committed
311

dogmaphobic's avatar
dogmaphobic committed
312
    bool _enableRateCollection;
313
    bool _decodedFirstMavlinkPacket;    ///< true: link has correctly decoded it's first mavlink packet
314
    bool _isPX4Flow;
315

316
    QMap<int /* vehicle id */, MavlinkMessagesTimer*> _mavlinkMessagesTimers;
pixhawk's avatar
pixhawk committed
317 318
};

319
typedef QSharedPointer<LinkInterface> SharedLinkInterfacePointer;
320