BluetoothLink.h 4.85 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.
 *
 ****************************************************************************/
dogmaphobic's avatar
dogmaphobic committed
9

10
#pragma once
dogmaphobic's avatar
dogmaphobic committed
11 12 13 14 15 16 17 18 19

#include <QString>
#include <QList>
#include <QMutex>
#include <QMutexLocker>
#include <QQueue>
#include <QByteArray>
#include <QBluetoothDeviceInfo>
#include <QtBluetooth/QBluetoothSocket>
dogmaphobic's avatar
dogmaphobic committed
20 21
#include <qbluetoothserviceinfo.h>
#include <qbluetoothservicediscoveryagent.h>
dogmaphobic's avatar
dogmaphobic committed
22 23

#include "QGCConfig.h"
24 25
#include "LinkConfiguration.h"
#include "LinkInterface.h"
dogmaphobic's avatar
dogmaphobic committed
26 27

class QBluetoothDeviceDiscoveryAgent;
dogmaphobic's avatar
dogmaphobic committed
28
class QBluetoothServiceDiscoveryAgent;
29
class LinkManager;
dogmaphobic's avatar
dogmaphobic committed
30

31 32 33 34 35 36 37 38 39 40 41 42
class BluetoothData
{
public:
    BluetoothData()
    {
    }
    BluetoothData(const BluetoothData& other)
    {
        *this = other;
    }
    bool operator==(const BluetoothData& other)
    {
dogmaphobic's avatar
dogmaphobic committed
43 44 45 46 47
#ifdef __ios__
        return uuid == other.uuid && name == other.name;
#else
        return name == other.name && address == other.address;
#endif
48 49 50 51
    }
    BluetoothData& operator=(const BluetoothData& other)
    {
        name = other.name;
dogmaphobic's avatar
dogmaphobic committed
52 53 54
#ifdef __ios__
        uuid = other.uuid;
#else
55
        address = other.address;
dogmaphobic's avatar
dogmaphobic committed
56
#endif
57 58 59
        return *this;
    }
    QString name;
dogmaphobic's avatar
dogmaphobic committed
60 61 62
#ifdef __ios__
    QBluetoothUuid uuid;
#else
63
    QString address;
dogmaphobic's avatar
dogmaphobic committed
64
#endif
65 66
};

dogmaphobic's avatar
dogmaphobic committed
67 68 69 70 71 72 73 74 75 76
class BluetoothConfiguration : public LinkConfiguration
{
    Q_OBJECT

public:

    BluetoothConfiguration(const QString& name);
    BluetoothConfiguration(BluetoothConfiguration* source);
    ~BluetoothConfiguration();

77 78 79
    Q_PROPERTY(QString      devName     READ devName    WRITE setDevName  NOTIFY devNameChanged)
    Q_PROPERTY(QString      address     READ address                      NOTIFY addressChanged)
    Q_PROPERTY(QStringList  nameList    READ nameList                     NOTIFY nameListChanged)
dogmaphobic's avatar
dogmaphobic committed
80 81
    Q_PROPERTY(bool         scanning    READ scanning                     NOTIFY scanningChanged)

82 83
    Q_INVOKABLE void startScan  (void);
    Q_INVOKABLE void stopScan   (void);
84

85 86 87 88 89 90
    QString         devName     (void)                  { return _device.name; }
    QString         address     (void);
    QStringList     nameList    (void)                  { return _nameList; }
    bool            scanning    (void)                  { return _deviceDiscover != nullptr; }
    BluetoothData   device      (void)                  { return _device; }
    void            setDevName  (const QString& name);
dogmaphobic's avatar
dogmaphobic committed
91

92 93 94 95 96 97 98
    /// LinkConfiguration overrides
    LinkType    type            (void) override                                         { return LinkConfiguration::TypeBluetooth; }
    void        copyFrom        (LinkConfiguration* source) override;
    void        loadSettings    (QSettings& settings, const QString& root) override;
    void        saveSettings    (QSettings& settings, const QString& root) override;
    QString     settingsURL     (void) override                                         { return "BluetoothSettings.qml"; }
    QString     settingsTitle   (void) override;
dogmaphobic's avatar
dogmaphobic committed
99 100

public slots:
101 102
    void deviceDiscovered   (QBluetoothDeviceInfo info);
    void doneScanning       (void);
dogmaphobic's avatar
dogmaphobic committed
103 104

signals:
105 106 107 108 109
    void newDevice      (QBluetoothDeviceInfo info);
    void devNameChanged (void);
    void addressChanged (void);
    void nameListChanged(void);
    void scanningChanged(void);
dogmaphobic's avatar
dogmaphobic committed
110 111

private:
112 113 114 115
    QBluetoothDeviceDiscoveryAgent* _deviceDiscover = nullptr;
    BluetoothData                   _device;
    QStringList                     _nameList;
    QList<BluetoothData>            _deviceList;
dogmaphobic's avatar
dogmaphobic committed
116 117 118 119 120 121 122 123 124 125
};

class BluetoothLink : public LinkInterface
{
    Q_OBJECT

    friend class BluetoothConfiguration;
    friend class LinkManager;

public:
126
    ~BluetoothLink();
dogmaphobic's avatar
dogmaphobic committed
127

128 129
    // Overrides from QThread
    void run(void) override;
dogmaphobic's avatar
dogmaphobic committed
130

131 132 133
    // LinkConfiguration overrides
    bool isConnected(void) const override;
    void disconnect (void) override;
dogmaphobic's avatar
dogmaphobic committed
134 135

public slots:
136 137 138 139
    void    readBytes           (void);
    void    deviceConnected     (void);
    void    deviceDisconnected  (void);
    void    deviceError         (QBluetoothSocket::SocketError error);
dogmaphobic's avatar
dogmaphobic committed
140
#ifdef __ios__
141 142
    void    serviceDiscovered   (const QBluetoothServiceInfo &info);
    void    discoveryFinished   (void);
dogmaphobic's avatar
dogmaphobic committed
143
#endif
dogmaphobic's avatar
dogmaphobic committed
144

145
private slots:
146 147
    // LinkInterface overrides
    void _writeBytes(const QByteArray bytes) override;
148 149

private:
150
    BluetoothLink(SharedLinkConfigurationPtr& config);
dogmaphobic's avatar
dogmaphobic committed
151

152 153 154 155 156
    // LinkInterface overrides
    bool _connect(void) override;

    bool _hardwareConnect   (void);
    void _createSocket      (void);
dogmaphobic's avatar
dogmaphobic committed
157

158
    QBluetoothSocket*                   _targetSocket    = nullptr;
dogmaphobic's avatar
dogmaphobic committed
159
#ifdef __ios__
160
    QBluetoothServiceDiscoveryAgent*    _discoveryAgent = nullptr;
dogmaphobic's avatar
dogmaphobic committed
161
#endif
162 163
    bool                                _shutDown       = false;
    bool                                _connectState   = false;
dogmaphobic's avatar
dogmaphobic committed
164 165
};